* gdb.c++/classes.exp (test_nonexistant_members): Fix name
[deliverable/binutils-gdb.git] / gdb / valops.c
CommitLineData
c906108c
SS
1/* Perform non-arithmetic operations on values, for GDB.
2 Copyright 1986, 87, 89, 91, 92, 93, 94, 95, 96, 97, 1998
3 Free Software Foundation, Inc.
4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b
JM
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
c906108c
SS
21
22#include "defs.h"
23#include "symtab.h"
24#include "gdbtypes.h"
25#include "value.h"
26#include "frame.h"
27#include "inferior.h"
28#include "gdbcore.h"
29#include "target.h"
30#include "demangle.h"
31#include "language.h"
32#include "gdbcmd.h"
33
34#include <errno.h>
35#include "gdb_string.h"
36
c906108c
SS
37/* Flag indicating HP compilers were used; needed to correctly handle some
38 value operations with HP aCC code/runtime. */
39extern int hp_som_som_object_present;
40
070ad9f0 41extern int overload_debug;
c906108c
SS
42/* Local functions. */
43
a14ed312 44static int typecmp (int staticp, struct type *t1[], value_ptr t2[]);
c906108c 45
a14ed312
KB
46static CORE_ADDR find_function_addr (value_ptr, struct type **);
47static value_ptr value_arg_coerce (value_ptr, struct type *, int);
c906108c
SS
48
49
a14ed312 50static CORE_ADDR value_push (CORE_ADDR, value_ptr);
c906108c 51
a14ed312
KB
52static value_ptr search_struct_field (char *, value_ptr, int,
53 struct type *, int);
c906108c 54
a14ed312
KB
55static value_ptr search_struct_method (char *, value_ptr *,
56 value_ptr *,
57 int, int *, struct type *);
c906108c 58
a14ed312 59static int check_field_in (struct type *, const char *);
c906108c 60
a14ed312 61static CORE_ADDR allocate_space_in_inferior (int);
c906108c 62
a14ed312 63static value_ptr cast_into_complex (struct type *, value_ptr);
c906108c 64
a14ed312
KB
65static struct fn_field *find_method_list (value_ptr * argp, char *method,
66 int offset, int *static_memfuncp,
67 struct type *type, int *num_fns,
68 struct type **basetype,
69 int *boffset);
7a292a7a 70
a14ed312 71void _initialize_valops (void);
c906108c
SS
72
73#define VALUE_SUBSTRING_START(VAL) VALUE_FRAME(VAL)
74
75/* Flag for whether we want to abandon failed expression evals by default. */
76
77#if 0
78static int auto_abandon = 0;
79#endif
80
81int overload_resolution = 0;
242bfc55
FN
82
83/* This boolean tells what gdb should do if a signal is received while in
84 a function called from gdb (call dummy). If set, gdb unwinds the stack
85 and restore the context to what as it was before the call.
86 The default is to stop in the frame where the signal was received. */
87
88int unwind_on_signal_p = 0;
c5aa993b 89\f
c906108c
SS
90
91
c906108c
SS
92/* Find the address of function name NAME in the inferior. */
93
94value_ptr
95find_function_in_inferior (name)
96 char *name;
97{
98 register struct symbol *sym;
99 sym = lookup_symbol (name, 0, VAR_NAMESPACE, 0, NULL);
100 if (sym != NULL)
101 {
102 if (SYMBOL_CLASS (sym) != LOC_BLOCK)
103 {
104 error ("\"%s\" exists in this program but is not a function.",
105 name);
106 }
107 return value_of_variable (sym, NULL);
108 }
109 else
110 {
c5aa993b 111 struct minimal_symbol *msymbol = lookup_minimal_symbol (name, NULL, NULL);
c906108c
SS
112 if (msymbol != NULL)
113 {
114 struct type *type;
4478b372 115 CORE_ADDR maddr;
c906108c
SS
116 type = lookup_pointer_type (builtin_type_char);
117 type = lookup_function_type (type);
118 type = lookup_pointer_type (type);
4478b372
JB
119 maddr = SYMBOL_VALUE_ADDRESS (msymbol);
120 return value_from_pointer (type, maddr);
c906108c
SS
121 }
122 else
123 {
c5aa993b 124 if (!target_has_execution)
c906108c 125 error ("evaluation of this expression requires the target program to be active");
c5aa993b 126 else
c906108c
SS
127 error ("evaluation of this expression requires the program to have a function \"%s\".", name);
128 }
129 }
130}
131
132/* Allocate NBYTES of space in the inferior using the inferior's malloc
133 and return a value that is a pointer to the allocated space. */
134
135value_ptr
136value_allocate_space_in_inferior (len)
137 int len;
138{
139 value_ptr blocklen;
140 register value_ptr val = find_function_in_inferior ("malloc");
141
142 blocklen = value_from_longest (builtin_type_int, (LONGEST) len);
143 val = call_function_by_hand (val, 1, &blocklen);
144 if (value_logical_not (val))
145 {
146 if (!target_has_execution)
c5aa993b
JM
147 error ("No memory available to program now: you need to start the target first");
148 else
149 error ("No memory available to program: call to malloc failed");
c906108c
SS
150 }
151 return val;
152}
153
154static CORE_ADDR
155allocate_space_in_inferior (len)
156 int len;
157{
158 return value_as_long (value_allocate_space_in_inferior (len));
159}
160
161/* Cast value ARG2 to type TYPE and return as a value.
162 More general than a C cast: accepts any two types of the same length,
163 and if ARG2 is an lvalue it can be cast into anything at all. */
164/* In C++, casts may change pointer or object representations. */
165
166value_ptr
167value_cast (type, arg2)
168 struct type *type;
169 register value_ptr arg2;
170{
171 register enum type_code code1;
172 register enum type_code code2;
173 register int scalar;
174 struct type *type2;
175
176 int convert_to_boolean = 0;
c5aa993b 177
c906108c
SS
178 if (VALUE_TYPE (arg2) == type)
179 return arg2;
180
181 CHECK_TYPEDEF (type);
182 code1 = TYPE_CODE (type);
c5aa993b 183 COERCE_REF (arg2);
c906108c
SS
184 type2 = check_typedef (VALUE_TYPE (arg2));
185
186 /* A cast to an undetermined-length array_type, such as (TYPE [])OBJECT,
187 is treated like a cast to (TYPE [N])OBJECT,
188 where N is sizeof(OBJECT)/sizeof(TYPE). */
189 if (code1 == TYPE_CODE_ARRAY)
190 {
191 struct type *element_type = TYPE_TARGET_TYPE (type);
192 unsigned element_length = TYPE_LENGTH (check_typedef (element_type));
193 if (element_length > 0
c5aa993b 194 && TYPE_ARRAY_UPPER_BOUND_TYPE (type) == BOUND_CANNOT_BE_DETERMINED)
c906108c
SS
195 {
196 struct type *range_type = TYPE_INDEX_TYPE (type);
197 int val_length = TYPE_LENGTH (type2);
198 LONGEST low_bound, high_bound, new_length;
199 if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
200 low_bound = 0, high_bound = 0;
201 new_length = val_length / element_length;
202 if (val_length % element_length != 0)
c5aa993b 203 warning ("array element type size does not divide object size in cast");
c906108c
SS
204 /* FIXME-type-allocation: need a way to free this type when we are
205 done with it. */
206 range_type = create_range_type ((struct type *) NULL,
207 TYPE_TARGET_TYPE (range_type),
208 low_bound,
209 new_length + low_bound - 1);
210 VALUE_TYPE (arg2) = create_array_type ((struct type *) NULL,
211 element_type, range_type);
212 return arg2;
213 }
214 }
215
216 if (current_language->c_style_arrays
217 && TYPE_CODE (type2) == TYPE_CODE_ARRAY)
218 arg2 = value_coerce_array (arg2);
219
220 if (TYPE_CODE (type2) == TYPE_CODE_FUNC)
221 arg2 = value_coerce_function (arg2);
222
223 type2 = check_typedef (VALUE_TYPE (arg2));
224 COERCE_VARYING_ARRAY (arg2, type2);
225 code2 = TYPE_CODE (type2);
226
227 if (code1 == TYPE_CODE_COMPLEX)
228 return cast_into_complex (type, arg2);
229 if (code1 == TYPE_CODE_BOOL)
230 {
231 code1 = TYPE_CODE_INT;
232 convert_to_boolean = 1;
233 }
234 if (code1 == TYPE_CODE_CHAR)
235 code1 = TYPE_CODE_INT;
236 if (code2 == TYPE_CODE_BOOL || code2 == TYPE_CODE_CHAR)
237 code2 = TYPE_CODE_INT;
238
239 scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
240 || code2 == TYPE_CODE_ENUM || code2 == TYPE_CODE_RANGE);
241
c5aa993b 242 if (code1 == TYPE_CODE_STRUCT
c906108c
SS
243 && code2 == TYPE_CODE_STRUCT
244 && TYPE_NAME (type) != 0)
245 {
246 /* Look in the type of the source to see if it contains the
7b83ea04
AC
247 type of the target as a superclass. If so, we'll need to
248 offset the object in addition to changing its type. */
c906108c
SS
249 value_ptr v = search_struct_field (type_name_no_tag (type),
250 arg2, 0, type2, 1);
251 if (v)
252 {
253 VALUE_TYPE (v) = type;
254 return v;
255 }
256 }
257 if (code1 == TYPE_CODE_FLT && scalar)
258 return value_from_double (type, value_as_double (arg2));
259 else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM
260 || code1 == TYPE_CODE_RANGE)
261 && (scalar || code2 == TYPE_CODE_PTR))
262 {
263 LONGEST longest;
c5aa993b
JM
264
265 if (hp_som_som_object_present && /* if target compiled by HP aCC */
266 (code2 == TYPE_CODE_PTR))
267 {
268 unsigned int *ptr;
269 value_ptr retvalp;
270
271 switch (TYPE_CODE (TYPE_TARGET_TYPE (type2)))
272 {
273 /* With HP aCC, pointers to data members have a bias */
274 case TYPE_CODE_MEMBER:
275 retvalp = value_from_longest (type, value_as_long (arg2));
716c501e
MS
276 /* force evaluation */
277 ptr = (unsigned int *) VALUE_CONTENTS (retvalp);
c5aa993b
JM
278 *ptr &= ~0x20000000; /* zap 29th bit to remove bias */
279 return retvalp;
280
281 /* While pointers to methods don't really point to a function */
282 case TYPE_CODE_METHOD:
283 error ("Pointers to methods not supported with HP aCC");
284
285 default:
286 break; /* fall out and go to normal handling */
287 }
288 }
c906108c 289 longest = value_as_long (arg2);
716c501e
MS
290 return value_from_longest (type, convert_to_boolean ?
291 (LONGEST) (longest ? 1 : 0) : longest);
c906108c
SS
292 }
293 else if (TYPE_LENGTH (type) == TYPE_LENGTH (type2))
294 {
295 if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
296 {
297 struct type *t1 = check_typedef (TYPE_TARGET_TYPE (type));
298 struct type *t2 = check_typedef (TYPE_TARGET_TYPE (type2));
c5aa993b 299 if (TYPE_CODE (t1) == TYPE_CODE_STRUCT
c906108c
SS
300 && TYPE_CODE (t2) == TYPE_CODE_STRUCT
301 && !value_logical_not (arg2))
302 {
303 value_ptr v;
304
305 /* Look in the type of the source to see if it contains the
7b83ea04
AC
306 type of the target as a superclass. If so, we'll need to
307 offset the pointer rather than just change its type. */
c906108c
SS
308 if (TYPE_NAME (t1) != NULL)
309 {
310 v = search_struct_field (type_name_no_tag (t1),
311 value_ind (arg2), 0, t2, 1);
312 if (v)
313 {
314 v = value_addr (v);
315 VALUE_TYPE (v) = type;
316 return v;
317 }
318 }
319
320 /* Look in the type of the target to see if it contains the
7b83ea04
AC
321 type of the source as a superclass. If so, we'll need to
322 offset the pointer rather than just change its type.
323 FIXME: This fails silently with virtual inheritance. */
c906108c
SS
324 if (TYPE_NAME (t2) != NULL)
325 {
326 v = search_struct_field (type_name_no_tag (t2),
c5aa993b 327 value_zero (t1, not_lval), 0, t1, 1);
c906108c
SS
328 if (v)
329 {
330 value_ptr v2 = value_ind (arg2);
331 VALUE_ADDRESS (v2) -= VALUE_ADDRESS (v)
c5aa993b 332 + VALUE_OFFSET (v);
070ad9f0
DB
333
334 /* JYG: adjust the new pointer value and
335 embedded offset. */
336 v2->aligner.contents[0] -= VALUE_EMBEDDED_OFFSET (v);
337 VALUE_EMBEDDED_OFFSET (v2) = 0;
338
c906108c
SS
339 v2 = value_addr (v2);
340 VALUE_TYPE (v2) = type;
341 return v2;
342 }
343 }
344 }
345 /* No superclass found, just fall through to change ptr type. */
346 }
347 VALUE_TYPE (arg2) = type;
c5aa993b
JM
348 VALUE_ENCLOSING_TYPE (arg2) = type; /* pai: chk_val */
349 VALUE_POINTED_TO_OFFSET (arg2) = 0; /* pai: chk_val */
c906108c
SS
350 return arg2;
351 }
352 else if (chill_varying_type (type))
353 {
354 struct type *range1, *range2, *eltype1, *eltype2;
355 value_ptr val;
356 int count1, count2;
357 LONGEST low_bound, high_bound;
358 char *valaddr, *valaddr_data;
359 /* For lint warning about eltype2 possibly uninitialized: */
360 eltype2 = NULL;
361 if (code2 == TYPE_CODE_BITSTRING)
362 error ("not implemented: converting bitstring to varying type");
363 if ((code2 != TYPE_CODE_ARRAY && code2 != TYPE_CODE_STRING)
364 || (eltype1 = check_typedef (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 1))),
365 eltype2 = check_typedef (TYPE_TARGET_TYPE (type2)),
366 (TYPE_LENGTH (eltype1) != TYPE_LENGTH (eltype2)
c5aa993b 367 /* || TYPE_CODE (eltype1) != TYPE_CODE (eltype2) */ )))
c906108c
SS
368 error ("Invalid conversion to varying type");
369 range1 = TYPE_FIELD_TYPE (TYPE_FIELD_TYPE (type, 1), 0);
370 range2 = TYPE_FIELD_TYPE (type2, 0);
371 if (get_discrete_bounds (range1, &low_bound, &high_bound) < 0)
372 count1 = -1;
373 else
374 count1 = high_bound - low_bound + 1;
375 if (get_discrete_bounds (range2, &low_bound, &high_bound) < 0)
c5aa993b 376 count1 = -1, count2 = 0; /* To force error before */
c906108c
SS
377 else
378 count2 = high_bound - low_bound + 1;
379 if (count2 > count1)
380 error ("target varying type is too small");
381 val = allocate_value (type);
382 valaddr = VALUE_CONTENTS_RAW (val);
383 valaddr_data = valaddr + TYPE_FIELD_BITPOS (type, 1) / 8;
384 /* Set val's __var_length field to count2. */
385 store_signed_integer (valaddr, TYPE_LENGTH (TYPE_FIELD_TYPE (type, 0)),
386 count2);
387 /* Set the __var_data field to count2 elements copied from arg2. */
388 memcpy (valaddr_data, VALUE_CONTENTS (arg2),
389 count2 * TYPE_LENGTH (eltype2));
390 /* Zero the rest of the __var_data field of val. */
391 memset (valaddr_data + count2 * TYPE_LENGTH (eltype2), '\0',
392 (count1 - count2) * TYPE_LENGTH (eltype2));
393 return val;
394 }
395 else if (VALUE_LVAL (arg2) == lval_memory)
396 {
397 return value_at_lazy (type, VALUE_ADDRESS (arg2) + VALUE_OFFSET (arg2),
398 VALUE_BFD_SECTION (arg2));
399 }
400 else if (code1 == TYPE_CODE_VOID)
401 {
402 return value_zero (builtin_type_void, not_lval);
403 }
404 else
405 {
406 error ("Invalid cast.");
407 return 0;
408 }
409}
410
411/* Create a value of type TYPE that is zero, and return it. */
412
413value_ptr
414value_zero (type, lv)
415 struct type *type;
416 enum lval_type lv;
417{
418 register value_ptr val = allocate_value (type);
419
420 memset (VALUE_CONTENTS (val), 0, TYPE_LENGTH (check_typedef (type)));
421 VALUE_LVAL (val) = lv;
422
423 return val;
424}
425
070ad9f0 426/* Return a value with type TYPE located at ADDR.
c906108c
SS
427
428 Call value_at only if the data needs to be fetched immediately;
429 if we can be 'lazy' and defer the fetch, perhaps indefinately, call
430 value_at_lazy instead. value_at_lazy simply records the address of
070ad9f0
DB
431 the data and sets the lazy-evaluation-required flag. The lazy flag
432 is tested in the VALUE_CONTENTS macro, which is used if and when
433 the contents are actually required.
c906108c
SS
434
435 Note: value_at does *NOT* handle embedded offsets; perform such
436 adjustments before or after calling it. */
437
438value_ptr
439value_at (type, addr, sect)
440 struct type *type;
441 CORE_ADDR addr;
442 asection *sect;
443{
444 register value_ptr val;
445
446 if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
447 error ("Attempt to dereference a generic pointer.");
448
449 val = allocate_value (type);
450
7a292a7a
SS
451 if (GDB_TARGET_IS_D10V
452 && TYPE_CODE (type) == TYPE_CODE_PTR
c906108c
SS
453 && TYPE_TARGET_TYPE (type)
454 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_FUNC))
455 {
456 /* pointer to function */
457 unsigned long num;
458 unsigned short snum;
459 snum = read_memory_unsigned_integer (addr, 2);
7a292a7a
SS
460 num = D10V_MAKE_IADDR (snum);
461 store_address (VALUE_CONTENTS_RAW (val), 4, num);
c906108c 462 }
7a292a7a 463 else if (GDB_TARGET_IS_D10V
c5aa993b 464 && TYPE_CODE (type) == TYPE_CODE_PTR)
c906108c
SS
465 {
466 /* pointer to data */
467 unsigned long num;
468 unsigned short snum;
469 snum = read_memory_unsigned_integer (addr, 2);
7a292a7a 470 num = D10V_MAKE_DADDR (snum);
c5aa993b 471 store_address (VALUE_CONTENTS_RAW (val), 4, num);
c906108c
SS
472 }
473 else
d4b2399a 474 read_memory (addr, VALUE_CONTENTS_ALL_RAW (val), TYPE_LENGTH (type));
c906108c
SS
475
476 VALUE_LVAL (val) = lval_memory;
477 VALUE_ADDRESS (val) = addr;
478 VALUE_BFD_SECTION (val) = sect;
479
480 return val;
481}
482
483/* Return a lazy value with type TYPE located at ADDR (cf. value_at). */
484
485value_ptr
486value_at_lazy (type, addr, sect)
487 struct type *type;
488 CORE_ADDR addr;
489 asection *sect;
490{
491 register value_ptr val;
492
493 if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
494 error ("Attempt to dereference a generic pointer.");
495
496 val = allocate_value (type);
497
498 VALUE_LVAL (val) = lval_memory;
499 VALUE_ADDRESS (val) = addr;
500 VALUE_LAZY (val) = 1;
501 VALUE_BFD_SECTION (val) = sect;
502
503 return val;
504}
505
070ad9f0
DB
506/* Called only from the VALUE_CONTENTS and VALUE_CONTENTS_ALL macros,
507 if the current data for a variable needs to be loaded into
508 VALUE_CONTENTS(VAL). Fetches the data from the user's process, and
c906108c
SS
509 clears the lazy flag to indicate that the data in the buffer is valid.
510
511 If the value is zero-length, we avoid calling read_memory, which would
512 abort. We mark the value as fetched anyway -- all 0 bytes of it.
513
514 This function returns a value because it is used in the VALUE_CONTENTS
515 macro as part of an expression, where a void would not work. The
516 value is ignored. */
517
518int
519value_fetch_lazy (val)
520 register value_ptr val;
521{
522 CORE_ADDR addr = VALUE_ADDRESS (val) + VALUE_OFFSET (val);
523 int length = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (val));
524
c5aa993b 525 struct type *type = VALUE_TYPE (val);
7a292a7a
SS
526 if (GDB_TARGET_IS_D10V
527 && TYPE_CODE (type) == TYPE_CODE_PTR
c906108c
SS
528 && TYPE_TARGET_TYPE (type)
529 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_FUNC))
530 {
531 /* pointer to function */
532 unsigned long num;
533 unsigned short snum;
534 snum = read_memory_unsigned_integer (addr, 2);
c5aa993b
JM
535 num = D10V_MAKE_IADDR (snum);
536 store_address (VALUE_CONTENTS_RAW (val), 4, num);
c906108c 537 }
7a292a7a 538 else if (GDB_TARGET_IS_D10V
c5aa993b 539 && TYPE_CODE (type) == TYPE_CODE_PTR)
c906108c
SS
540 {
541 /* pointer to data */
542 unsigned long num;
543 unsigned short snum;
544 snum = read_memory_unsigned_integer (addr, 2);
c5aa993b
JM
545 num = D10V_MAKE_DADDR (snum);
546 store_address (VALUE_CONTENTS_RAW (val), 4, num);
c906108c 547 }
7a292a7a 548 else if (length)
d4b2399a
C
549 read_memory (addr, VALUE_CONTENTS_ALL_RAW (val), length);
550
c906108c
SS
551 VALUE_LAZY (val) = 0;
552 return 0;
553}
554
555
556/* Store the contents of FROMVAL into the location of TOVAL.
557 Return a new value with the location of TOVAL and contents of FROMVAL. */
558
559value_ptr
560value_assign (toval, fromval)
561 register value_ptr toval, fromval;
562{
563 register struct type *type;
564 register value_ptr val;
565 char raw_buffer[MAX_REGISTER_RAW_SIZE];
566 int use_buffer = 0;
567
568 if (!toval->modifiable)
569 error ("Left operand of assignment is not a modifiable lvalue.");
570
571 COERCE_REF (toval);
572
573 type = VALUE_TYPE (toval);
574 if (VALUE_LVAL (toval) != lval_internalvar)
575 fromval = value_cast (type, fromval);
576 else
577 COERCE_ARRAY (fromval);
578 CHECK_TYPEDEF (type);
579
580 /* If TOVAL is a special machine register requiring conversion
581 of program values to a special raw format,
582 convert FROMVAL's contents now, with result in `raw_buffer',
583 and set USE_BUFFER to the number of bytes to write. */
584
ac9a91a7 585 if (VALUE_REGNO (toval) >= 0)
c906108c
SS
586 {
587 int regno = VALUE_REGNO (toval);
588 if (REGISTER_CONVERTIBLE (regno))
589 {
590 struct type *fromtype = check_typedef (VALUE_TYPE (fromval));
591 REGISTER_CONVERT_TO_RAW (fromtype, regno,
592 VALUE_CONTENTS (fromval), raw_buffer);
593 use_buffer = REGISTER_RAW_SIZE (regno);
594 }
595 }
c906108c
SS
596
597 switch (VALUE_LVAL (toval))
598 {
599 case lval_internalvar:
600 set_internalvar (VALUE_INTERNALVAR (toval), fromval);
601 val = value_copy (VALUE_INTERNALVAR (toval)->value);
602 VALUE_ENCLOSING_TYPE (val) = VALUE_ENCLOSING_TYPE (fromval);
603 VALUE_EMBEDDED_OFFSET (val) = VALUE_EMBEDDED_OFFSET (fromval);
604 VALUE_POINTED_TO_OFFSET (val) = VALUE_POINTED_TO_OFFSET (fromval);
605 return val;
606
607 case lval_internalvar_component:
608 set_internalvar_component (VALUE_INTERNALVAR (toval),
609 VALUE_OFFSET (toval),
610 VALUE_BITPOS (toval),
611 VALUE_BITSIZE (toval),
612 fromval);
613 break;
614
615 case lval_memory:
616 {
617 char *dest_buffer;
c5aa993b
JM
618 CORE_ADDR changed_addr;
619 int changed_len;
c906108c 620
c5aa993b
JM
621 if (VALUE_BITSIZE (toval))
622 {
c906108c
SS
623 char buffer[sizeof (LONGEST)];
624 /* We assume that the argument to read_memory is in units of
625 host chars. FIXME: Is that correct? */
626 changed_len = (VALUE_BITPOS (toval)
c5aa993b
JM
627 + VALUE_BITSIZE (toval)
628 + HOST_CHAR_BIT - 1)
629 / HOST_CHAR_BIT;
c906108c
SS
630
631 if (changed_len > (int) sizeof (LONGEST))
632 error ("Can't handle bitfields which don't fit in a %d bit word.",
633 sizeof (LONGEST) * HOST_CHAR_BIT);
634
635 read_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
636 buffer, changed_len);
637 modify_field (buffer, value_as_long (fromval),
638 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
639 changed_addr = VALUE_ADDRESS (toval) + VALUE_OFFSET (toval);
640 dest_buffer = buffer;
641 }
642 else if (use_buffer)
643 {
644 changed_addr = VALUE_ADDRESS (toval) + VALUE_OFFSET (toval);
645 changed_len = use_buffer;
646 dest_buffer = raw_buffer;
647 }
648 else
649 {
650 changed_addr = VALUE_ADDRESS (toval) + VALUE_OFFSET (toval);
651 changed_len = TYPE_LENGTH (type);
652 dest_buffer = VALUE_CONTENTS (fromval);
653 }
654
655 write_memory (changed_addr, dest_buffer, changed_len);
656 if (memory_changed_hook)
657 memory_changed_hook (changed_addr, changed_len);
658 }
659 break;
660
661 case lval_register:
662 if (VALUE_BITSIZE (toval))
663 {
664 char buffer[sizeof (LONGEST)];
8903de4f
FN
665 int len =
666 REGISTER_RAW_SIZE (VALUE_REGNO (toval)) - VALUE_OFFSET (toval);
c906108c
SS
667
668 if (len > (int) sizeof (LONGEST))
669 error ("Can't handle bitfields in registers larger than %d bits.",
670 sizeof (LONGEST) * HOST_CHAR_BIT);
671
672 if (VALUE_BITPOS (toval) + VALUE_BITSIZE (toval)
673 > len * HOST_CHAR_BIT)
674 /* Getting this right would involve being very careful about
675 byte order. */
c2d11a7d
JM
676 error ("Can't assign to bitfields that cross register "
677 "boundaries.");
c906108c 678
c5aa993b
JM
679 read_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
680 buffer, len);
681 modify_field (buffer, value_as_long (fromval),
682 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
683 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
684 buffer, len);
c906108c
SS
685 }
686 else if (use_buffer)
687 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
688 raw_buffer, use_buffer);
689 else
c5aa993b 690 {
c906108c
SS
691 /* Do any conversion necessary when storing this type to more
692 than one register. */
693#ifdef REGISTER_CONVERT_FROM_TYPE
694 memcpy (raw_buffer, VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
c5aa993b 695 REGISTER_CONVERT_FROM_TYPE (VALUE_REGNO (toval), type, raw_buffer);
c906108c
SS
696 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
697 raw_buffer, TYPE_LENGTH (type));
698#else
699 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
c5aa993b 700 VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
c906108c
SS
701#endif
702 }
703 /* Assigning to the stack pointer, frame pointer, and other
7b83ea04
AC
704 (architecture and calling convention specific) registers may
705 cause the frame cache to be out of date. We just do this
706 on all assignments to registers for simplicity; I doubt the slowdown
707 matters. */
c906108c
SS
708 reinit_frame_cache ();
709 break;
710
711 case lval_reg_frame_relative:
712 {
713 /* value is stored in a series of registers in the frame
714 specified by the structure. Copy that value out, modify
715 it, and copy it back in. */
716 int amount_to_copy = (VALUE_BITSIZE (toval) ? 1 : TYPE_LENGTH (type));
717 int reg_size = REGISTER_RAW_SIZE (VALUE_FRAME_REGNUM (toval));
718 int byte_offset = VALUE_OFFSET (toval) % reg_size;
719 int reg_offset = VALUE_OFFSET (toval) / reg_size;
720 int amount_copied;
721
722 /* Make the buffer large enough in all cases. */
723 char *buffer = (char *) alloca (amount_to_copy
724 + sizeof (LONGEST)
725 + MAX_REGISTER_RAW_SIZE);
726
727 int regno;
728 struct frame_info *frame;
729
730 /* Figure out which frame this is in currently. */
731 for (frame = get_current_frame ();
732 frame && FRAME_FP (frame) != VALUE_FRAME (toval);
733 frame = get_prev_frame (frame))
734 ;
735
736 if (!frame)
737 error ("Value being assigned to is no longer active.");
738
739 amount_to_copy += (reg_size - amount_to_copy % reg_size);
740
741 /* Copy it out. */
742 for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset,
743 amount_copied = 0);
744 amount_copied < amount_to_copy;
745 amount_copied += reg_size, regno++)
746 {
747 get_saved_register (buffer + amount_copied,
c5aa993b
JM
748 (int *) NULL, (CORE_ADDR *) NULL,
749 frame, regno, (enum lval_type *) NULL);
c906108c
SS
750 }
751
752 /* Modify what needs to be modified. */
753 if (VALUE_BITSIZE (toval))
754 modify_field (buffer + byte_offset,
755 value_as_long (fromval),
756 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
757 else if (use_buffer)
758 memcpy (buffer + byte_offset, raw_buffer, use_buffer);
759 else
760 memcpy (buffer + byte_offset, VALUE_CONTENTS (fromval),
761 TYPE_LENGTH (type));
762
763 /* Copy it back. */
764 for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset,
765 amount_copied = 0);
766 amount_copied < amount_to_copy;
767 amount_copied += reg_size, regno++)
768 {
769 enum lval_type lval;
770 CORE_ADDR addr;
771 int optim;
772
773 /* Just find out where to put it. */
c5aa993b
JM
774 get_saved_register ((char *) NULL,
775 &optim, &addr, frame, regno, &lval);
776
c906108c
SS
777 if (optim)
778 error ("Attempt to assign to a value that was optimized out.");
779 if (lval == lval_memory)
780 write_memory (addr, buffer + amount_copied, reg_size);
781 else if (lval == lval_register)
782 write_register_bytes (addr, buffer + amount_copied, reg_size);
783 else
784 error ("Attempt to assign to an unmodifiable value.");
785 }
786
787 if (register_changed_hook)
788 register_changed_hook (-1);
789 }
790 break;
c5aa993b 791
c906108c
SS
792
793 default:
794 error ("Left operand of assignment is not an lvalue.");
795 }
796
797 /* If the field does not entirely fill a LONGEST, then zero the sign bits.
798 If the field is signed, and is negative, then sign extend. */
799 if ((VALUE_BITSIZE (toval) > 0)
800 && (VALUE_BITSIZE (toval) < 8 * (int) sizeof (LONGEST)))
801 {
802 LONGEST fieldval = value_as_long (fromval);
803 LONGEST valmask = (((ULONGEST) 1) << VALUE_BITSIZE (toval)) - 1;
804
805 fieldval &= valmask;
806 if (!TYPE_UNSIGNED (type) && (fieldval & (valmask ^ (valmask >> 1))))
807 fieldval |= ~valmask;
808
809 fromval = value_from_longest (type, fieldval);
810 }
811
812 val = value_copy (toval);
813 memcpy (VALUE_CONTENTS_RAW (val), VALUE_CONTENTS (fromval),
814 TYPE_LENGTH (type));
815 VALUE_TYPE (val) = type;
816 VALUE_ENCLOSING_TYPE (val) = VALUE_ENCLOSING_TYPE (fromval);
817 VALUE_EMBEDDED_OFFSET (val) = VALUE_EMBEDDED_OFFSET (fromval);
818 VALUE_POINTED_TO_OFFSET (val) = VALUE_POINTED_TO_OFFSET (fromval);
c5aa993b 819
c906108c
SS
820 return val;
821}
822
823/* Extend a value VAL to COUNT repetitions of its type. */
824
825value_ptr
826value_repeat (arg1, count)
827 value_ptr arg1;
828 int count;
829{
830 register value_ptr val;
831
832 if (VALUE_LVAL (arg1) != lval_memory)
833 error ("Only values in memory can be extended with '@'.");
834 if (count < 1)
835 error ("Invalid number %d of repetitions.", count);
836
837 val = allocate_repeat_value (VALUE_ENCLOSING_TYPE (arg1), count);
838
839 read_memory (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1),
840 VALUE_CONTENTS_ALL_RAW (val),
841 TYPE_LENGTH (VALUE_ENCLOSING_TYPE (val)));
842 VALUE_LVAL (val) = lval_memory;
843 VALUE_ADDRESS (val) = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1);
844
845 return val;
846}
847
848value_ptr
849value_of_variable (var, b)
850 struct symbol *var;
851 struct block *b;
852{
853 value_ptr val;
854 struct frame_info *frame = NULL;
855
856 if (!b)
857 frame = NULL; /* Use selected frame. */
858 else if (symbol_read_needs_frame (var))
859 {
860 frame = block_innermost_frame (b);
861 if (!frame)
c5aa993b 862 {
c906108c
SS
863 if (BLOCK_FUNCTION (b)
864 && SYMBOL_SOURCE_NAME (BLOCK_FUNCTION (b)))
865 error ("No frame is currently executing in block %s.",
866 SYMBOL_SOURCE_NAME (BLOCK_FUNCTION (b)));
867 else
868 error ("No frame is currently executing in specified block");
c5aa993b 869 }
c906108c
SS
870 }
871
872 val = read_var_value (var, frame);
873 if (!val)
874 error ("Address of symbol \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var));
875
876 return val;
877}
878
879/* Given a value which is an array, return a value which is a pointer to its
880 first element, regardless of whether or not the array has a nonzero lower
881 bound.
882
883 FIXME: A previous comment here indicated that this routine should be
884 substracting the array's lower bound. It's not clear to me that this
885 is correct. Given an array subscripting operation, it would certainly
886 work to do the adjustment here, essentially computing:
887
888 (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0])
889
890 However I believe a more appropriate and logical place to account for
891 the lower bound is to do so in value_subscript, essentially computing:
892
893 (&array[0] + ((index - lowerbound) * sizeof array[0]))
894
895 As further evidence consider what would happen with operations other
896 than array subscripting, where the caller would get back a value that
897 had an address somewhere before the actual first element of the array,
898 and the information about the lower bound would be lost because of
899 the coercion to pointer type.
c5aa993b 900 */
c906108c
SS
901
902value_ptr
903value_coerce_array (arg1)
904 value_ptr arg1;
905{
906 register struct type *type = check_typedef (VALUE_TYPE (arg1));
907
908 if (VALUE_LVAL (arg1) != lval_memory)
909 error ("Attempt to take address of value not located in memory.");
910
4478b372
JB
911 return value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
912 (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
c906108c
SS
913}
914
915/* Given a value which is a function, return a value which is a pointer
916 to it. */
917
918value_ptr
919value_coerce_function (arg1)
920 value_ptr arg1;
921{
922 value_ptr retval;
923
924 if (VALUE_LVAL (arg1) != lval_memory)
925 error ("Attempt to take address of value not located in memory.");
926
4478b372
JB
927 retval = value_from_pointer (lookup_pointer_type (VALUE_TYPE (arg1)),
928 (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
c906108c
SS
929 VALUE_BFD_SECTION (retval) = VALUE_BFD_SECTION (arg1);
930 return retval;
c5aa993b 931}
c906108c
SS
932
933/* Return a pointer value for the object for which ARG1 is the contents. */
934
935value_ptr
936value_addr (arg1)
937 value_ptr arg1;
938{
939 value_ptr arg2;
940
941 struct type *type = check_typedef (VALUE_TYPE (arg1));
942 if (TYPE_CODE (type) == TYPE_CODE_REF)
943 {
944 /* Copy the value, but change the type from (T&) to (T*).
7b83ea04
AC
945 We keep the same location information, which is efficient,
946 and allows &(&X) to get the location containing the reference. */
c906108c
SS
947 arg2 = value_copy (arg1);
948 VALUE_TYPE (arg2) = lookup_pointer_type (TYPE_TARGET_TYPE (type));
949 return arg2;
950 }
951 if (TYPE_CODE (type) == TYPE_CODE_FUNC)
952 return value_coerce_function (arg1);
953
954 if (VALUE_LVAL (arg1) != lval_memory)
955 error ("Attempt to take address of value not located in memory.");
956
c5aa993b 957 /* Get target memory address */
4478b372
JB
958 arg2 = value_from_pointer (lookup_pointer_type (VALUE_TYPE (arg1)),
959 (VALUE_ADDRESS (arg1)
960 + VALUE_OFFSET (arg1)
961 + VALUE_EMBEDDED_OFFSET (arg1)));
c906108c
SS
962
963 /* This may be a pointer to a base subobject; so remember the
c5aa993b 964 full derived object's type ... */
c906108c 965 VALUE_ENCLOSING_TYPE (arg2) = lookup_pointer_type (VALUE_ENCLOSING_TYPE (arg1));
c5aa993b
JM
966 /* ... and also the relative position of the subobject in the full object */
967 VALUE_POINTED_TO_OFFSET (arg2) = VALUE_EMBEDDED_OFFSET (arg1);
c906108c
SS
968 VALUE_BFD_SECTION (arg2) = VALUE_BFD_SECTION (arg1);
969 return arg2;
970}
971
972/* Given a value of a pointer type, apply the C unary * operator to it. */
973
974value_ptr
975value_ind (arg1)
976 value_ptr arg1;
977{
978 struct type *base_type;
979 value_ptr arg2;
c906108c
SS
980
981 COERCE_ARRAY (arg1);
982
983 base_type = check_typedef (VALUE_TYPE (arg1));
984
985 if (TYPE_CODE (base_type) == TYPE_CODE_MEMBER)
986 error ("not implemented: member types in value_ind");
987
988 /* Allow * on an integer so we can cast it to whatever we want.
989 This returns an int, which seems like the most C-like thing
990 to do. "long long" variables are rare enough that
991 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
992 if (TYPE_CODE (base_type) == TYPE_CODE_INT)
993 return value_at (builtin_type_int,
994 (CORE_ADDR) value_as_long (arg1),
995 VALUE_BFD_SECTION (arg1));
996 else if (TYPE_CODE (base_type) == TYPE_CODE_PTR)
997 {
998 struct type *enc_type;
999 /* We may be pointing to something embedded in a larger object */
c5aa993b 1000 /* Get the real type of the enclosing object */
c906108c
SS
1001 enc_type = check_typedef (VALUE_ENCLOSING_TYPE (arg1));
1002 enc_type = TYPE_TARGET_TYPE (enc_type);
c5aa993b
JM
1003 /* Retrieve the enclosing object pointed to */
1004 arg2 = value_at_lazy (enc_type,
1005 value_as_pointer (arg1) - VALUE_POINTED_TO_OFFSET (arg1),
1006 VALUE_BFD_SECTION (arg1));
1007 /* Re-adjust type */
c906108c
SS
1008 VALUE_TYPE (arg2) = TYPE_TARGET_TYPE (base_type);
1009 /* Add embedding info */
1010 VALUE_ENCLOSING_TYPE (arg2) = enc_type;
1011 VALUE_EMBEDDED_OFFSET (arg2) = VALUE_POINTED_TO_OFFSET (arg1);
1012
1013 /* We may be pointing to an object of some derived type */
1014 arg2 = value_full_object (arg2, NULL, 0, 0, 0);
1015 return arg2;
1016 }
1017
1018 error ("Attempt to take contents of a non-pointer value.");
c5aa993b 1019 return 0; /* For lint -- never reached */
c906108c
SS
1020}
1021\f
1022/* Pushing small parts of stack frames. */
1023
1024/* Push one word (the size of object that a register holds). */
1025
1026CORE_ADDR
1027push_word (sp, word)
1028 CORE_ADDR sp;
1029 ULONGEST word;
1030{
1031 register int len = REGISTER_SIZE;
1032 char buffer[MAX_REGISTER_RAW_SIZE];
1033
1034 store_unsigned_integer (buffer, len, word);
1035 if (INNER_THAN (1, 2))
1036 {
1037 /* stack grows downward */
1038 sp -= len;
1039 write_memory (sp, buffer, len);
1040 }
1041 else
1042 {
1043 /* stack grows upward */
1044 write_memory (sp, buffer, len);
1045 sp += len;
1046 }
1047
1048 return sp;
1049}
1050
1051/* Push LEN bytes with data at BUFFER. */
1052
1053CORE_ADDR
1054push_bytes (sp, buffer, len)
1055 CORE_ADDR sp;
1056 char *buffer;
1057 int len;
1058{
1059 if (INNER_THAN (1, 2))
1060 {
1061 /* stack grows downward */
1062 sp -= len;
1063 write_memory (sp, buffer, len);
1064 }
1065 else
1066 {
1067 /* stack grows upward */
1068 write_memory (sp, buffer, len);
1069 sp += len;
1070 }
1071
1072 return sp;
1073}
1074
2df3850c
JM
1075#ifndef PARM_BOUNDARY
1076#define PARM_BOUNDARY (0)
1077#endif
1078
1079/* Push onto the stack the specified value VALUE. Pad it correctly for
1080 it to be an argument to a function. */
c906108c 1081
c906108c
SS
1082static CORE_ADDR
1083value_push (sp, arg)
1084 register CORE_ADDR sp;
1085 value_ptr arg;
1086{
1087 register int len = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (arg));
917317f4 1088 register int container_len = len;
2df3850c
JM
1089 register int offset;
1090
1091 /* How big is the container we're going to put this value in? */
1092 if (PARM_BOUNDARY)
1093 container_len = ((len + PARM_BOUNDARY / TARGET_CHAR_BIT - 1)
1094 & ~(PARM_BOUNDARY / TARGET_CHAR_BIT - 1));
1095
1096 /* Are we going to put it at the high or low end of the container? */
1097 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1098 offset = container_len - len;
1099 else
1100 offset = 0;
c906108c
SS
1101
1102 if (INNER_THAN (1, 2))
1103 {
1104 /* stack grows downward */
2df3850c
JM
1105 sp -= container_len;
1106 write_memory (sp + offset, VALUE_CONTENTS_ALL (arg), len);
c906108c
SS
1107 }
1108 else
1109 {
1110 /* stack grows upward */
2df3850c
JM
1111 write_memory (sp + offset, VALUE_CONTENTS_ALL (arg), len);
1112 sp += container_len;
c906108c
SS
1113 }
1114
1115 return sp;
1116}
1117
392a587b
JM
1118#ifndef PUSH_ARGUMENTS
1119#define PUSH_ARGUMENTS default_push_arguments
1120#endif
1121
1122CORE_ADDR
ac9a91a7 1123default_push_arguments (nargs, args, sp, struct_return, struct_addr)
392a587b
JM
1124 int nargs;
1125 value_ptr *args;
392a587b 1126 CORE_ADDR sp;
ac9a91a7 1127 int struct_return;
392a587b
JM
1128 CORE_ADDR struct_addr;
1129{
1130 /* ASSERT ( !struct_return); */
1131 int i;
1132 for (i = nargs - 1; i >= 0; i--)
1133 sp = value_push (sp, args[i]);
1134 return sp;
1135}
1136
c906108c 1137
b9a8e3bf
JB
1138/* A default function for COERCE_FLOAT_TO_DOUBLE: do the coercion only
1139 when we don't have any type for the argument at hand. This occurs
1140 when we have no debug info, or when passing varargs.
1141
1142 This is an annoying default: the rule the compiler follows is to do
1143 the standard promotions whenever there is no prototype in scope,
1144 and almost all targets want this behavior. But there are some old
1145 architectures which want this odd behavior. If you want to go
1146 through them all and fix them, please do. Modern gdbarch-style
1147 targets may find it convenient to use standard_coerce_float_to_double. */
1148int
1149default_coerce_float_to_double (struct type *formal, struct type *actual)
1150{
1151 return formal == NULL;
1152}
1153
1154
1155/* Always coerce floats to doubles when there is no prototype in scope.
1156 If your architecture follows the standard type promotion rules for
1157 calling unprototyped functions, your gdbarch init function can pass
1158 this function to set_gdbarch_coerce_float_to_double to use its logic. */
1159int
1160standard_coerce_float_to_double (struct type *formal, struct type *actual)
1161{
1162 return 1;
1163}
1164
1165
c906108c
SS
1166/* Perform the standard coercions that are specified
1167 for arguments to be passed to C functions.
1168
1169 If PARAM_TYPE is non-NULL, it is the expected parameter type.
1170 IS_PROTOTYPED is non-zero if the function declaration is prototyped. */
1171
1172static value_ptr
1173value_arg_coerce (arg, param_type, is_prototyped)
1174 value_ptr arg;
1175 struct type *param_type;
1176 int is_prototyped;
1177{
1178 register struct type *arg_type = check_typedef (VALUE_TYPE (arg));
1179 register struct type *type
c5aa993b 1180 = param_type ? check_typedef (param_type) : arg_type;
c906108c
SS
1181
1182 switch (TYPE_CODE (type))
1183 {
1184 case TYPE_CODE_REF:
1185 if (TYPE_CODE (arg_type) != TYPE_CODE_REF)
1186 {
1187 arg = value_addr (arg);
1188 VALUE_TYPE (arg) = param_type;
1189 return arg;
1190 }
1191 break;
1192 case TYPE_CODE_INT:
1193 case TYPE_CODE_CHAR:
1194 case TYPE_CODE_BOOL:
1195 case TYPE_CODE_ENUM:
1196 /* If we don't have a prototype, coerce to integer type if necessary. */
1197 if (!is_prototyped)
1198 {
1199 if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
1200 type = builtin_type_int;
1201 }
1202 /* Currently all target ABIs require at least the width of an integer
7b83ea04
AC
1203 type for an argument. We may have to conditionalize the following
1204 type coercion for future targets. */
c906108c
SS
1205 if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
1206 type = builtin_type_int;
1207 break;
1208 case TYPE_CODE_FLT:
1209 /* FIXME: We should always convert floats to doubles in the
7b83ea04
AC
1210 non-prototyped case. As many debugging formats include
1211 no information about prototyping, we have to live with
1212 COERCE_FLOAT_TO_DOUBLE for now. */
b9a8e3bf 1213 if (!is_prototyped && COERCE_FLOAT_TO_DOUBLE (param_type, arg_type))
c906108c
SS
1214 {
1215 if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_double))
1216 type = builtin_type_double;
1217 else if (TYPE_LENGTH (type) > TYPE_LENGTH (builtin_type_double))
1218 type = builtin_type_long_double;
1219 }
1220 break;
1221 case TYPE_CODE_FUNC:
1222 type = lookup_pointer_type (type);
1223 break;
1224 case TYPE_CODE_ARRAY:
1225 if (current_language->c_style_arrays)
1226 type = lookup_pointer_type (TYPE_TARGET_TYPE (type));
1227 break;
1228 case TYPE_CODE_UNDEF:
1229 case TYPE_CODE_PTR:
1230 case TYPE_CODE_STRUCT:
1231 case TYPE_CODE_UNION:
1232 case TYPE_CODE_VOID:
1233 case TYPE_CODE_SET:
1234 case TYPE_CODE_RANGE:
1235 case TYPE_CODE_STRING:
1236 case TYPE_CODE_BITSTRING:
1237 case TYPE_CODE_ERROR:
1238 case TYPE_CODE_MEMBER:
1239 case TYPE_CODE_METHOD:
1240 case TYPE_CODE_COMPLEX:
1241 default:
1242 break;
1243 }
1244
1245 return value_cast (type, arg);
1246}
1247
070ad9f0 1248/* Determine a function's address and its return type from its value.
c906108c
SS
1249 Calls error() if the function is not valid for calling. */
1250
1251static CORE_ADDR
1252find_function_addr (function, retval_type)
1253 value_ptr function;
1254 struct type **retval_type;
1255{
1256 register struct type *ftype = check_typedef (VALUE_TYPE (function));
1257 register enum type_code code = TYPE_CODE (ftype);
1258 struct type *value_type;
1259 CORE_ADDR funaddr;
1260
1261 /* If it's a member function, just look at the function
1262 part of it. */
1263
1264 /* Determine address to call. */
1265 if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD)
1266 {
1267 funaddr = VALUE_ADDRESS (function);
1268 value_type = TYPE_TARGET_TYPE (ftype);
1269 }
1270 else if (code == TYPE_CODE_PTR)
1271 {
1272 funaddr = value_as_pointer (function);
1273 ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
1274 if (TYPE_CODE (ftype) == TYPE_CODE_FUNC
1275 || TYPE_CODE (ftype) == TYPE_CODE_METHOD)
1276 {
1277#ifdef CONVERT_FROM_FUNC_PTR_ADDR
1278 /* FIXME: This is a workaround for the unusual function
1279 pointer representation on the RS/6000, see comment
1280 in config/rs6000/tm-rs6000.h */
1281 funaddr = CONVERT_FROM_FUNC_PTR_ADDR (funaddr);
1282#endif
1283 value_type = TYPE_TARGET_TYPE (ftype);
1284 }
1285 else
1286 value_type = builtin_type_int;
1287 }
1288 else if (code == TYPE_CODE_INT)
1289 {
1290 /* Handle the case of functions lacking debugging info.
7b83ea04 1291 Their values are characters since their addresses are char */
c906108c
SS
1292 if (TYPE_LENGTH (ftype) == 1)
1293 funaddr = value_as_pointer (value_addr (function));
1294 else
1295 /* Handle integer used as address of a function. */
1296 funaddr = (CORE_ADDR) value_as_long (function);
1297
1298 value_type = builtin_type_int;
1299 }
1300 else
1301 error ("Invalid data type for function to be called.");
1302
1303 *retval_type = value_type;
1304 return funaddr;
1305}
1306
1307/* All this stuff with a dummy frame may seem unnecessarily complicated
1308 (why not just save registers in GDB?). The purpose of pushing a dummy
1309 frame which looks just like a real frame is so that if you call a
1310 function and then hit a breakpoint (get a signal, etc), "backtrace"
1311 will look right. Whether the backtrace needs to actually show the
1312 stack at the time the inferior function was called is debatable, but
1313 it certainly needs to not display garbage. So if you are contemplating
1314 making dummy frames be different from normal frames, consider that. */
1315
1316/* Perform a function call in the inferior.
1317 ARGS is a vector of values of arguments (NARGS of them).
1318 FUNCTION is a value, the function to be called.
1319 Returns a value representing what the function returned.
1320 May fail to return, if a breakpoint or signal is hit
1321 during the execution of the function.
1322
1323 ARGS is modified to contain coerced values. */
1324
a14ed312
KB
1325static value_ptr hand_function_call (value_ptr function, int nargs,
1326 value_ptr * args);
7a292a7a
SS
1327static value_ptr
1328hand_function_call (function, nargs, args)
c906108c
SS
1329 value_ptr function;
1330 int nargs;
1331 value_ptr *args;
1332{
1333 register CORE_ADDR sp;
1334 register int i;
da59e081 1335 int rc;
c906108c
SS
1336 CORE_ADDR start_sp;
1337 /* CALL_DUMMY is an array of words (REGISTER_SIZE), but each word
1338 is in host byte order. Before calling FIX_CALL_DUMMY, we byteswap it
1339 and remove any extra bytes which might exist because ULONGEST is
070ad9f0 1340 bigger than REGISTER_SIZE.
c906108c
SS
1341
1342 NOTE: This is pretty wierd, as the call dummy is actually a
c5aa993b
JM
1343 sequence of instructions. But CISC machines will have
1344 to pack the instructions into REGISTER_SIZE units (and
1345 so will RISC machines for which INSTRUCTION_SIZE is not
1346 REGISTER_SIZE).
7a292a7a
SS
1347
1348 NOTE: This is pretty stupid. CALL_DUMMY should be in strict
c5aa993b 1349 target byte order. */
c906108c 1350
7a292a7a
SS
1351 static ULONGEST *dummy;
1352 int sizeof_dummy1;
1353 char *dummy1;
c906108c
SS
1354 CORE_ADDR old_sp;
1355 struct type *value_type;
1356 unsigned char struct_return;
1357 CORE_ADDR struct_addr = 0;
7a292a7a 1358 struct inferior_status *inf_status;
c906108c
SS
1359 struct cleanup *old_chain;
1360 CORE_ADDR funaddr;
c5aa993b 1361 int using_gcc; /* Set to version of gcc in use, or zero if not gcc */
c906108c
SS
1362 CORE_ADDR real_pc;
1363 struct type *param_type = NULL;
1364 struct type *ftype = check_typedef (SYMBOL_TYPE (function));
1365
7a292a7a
SS
1366 dummy = alloca (SIZEOF_CALL_DUMMY_WORDS);
1367 sizeof_dummy1 = REGISTER_SIZE * SIZEOF_CALL_DUMMY_WORDS / sizeof (ULONGEST);
1368 dummy1 = alloca (sizeof_dummy1);
1369 memcpy (dummy, CALL_DUMMY_WORDS, SIZEOF_CALL_DUMMY_WORDS);
1370
c906108c 1371 if (!target_has_execution)
c5aa993b 1372 noprocess ();
c906108c 1373
7a292a7a 1374 inf_status = save_inferior_status (1);
74b7792f 1375 old_chain = make_cleanup_restore_inferior_status (inf_status);
c906108c
SS
1376
1377 /* PUSH_DUMMY_FRAME is responsible for saving the inferior registers
1378 (and POP_FRAME for restoring them). (At least on most machines)
1379 they are saved on the stack in the inferior. */
1380 PUSH_DUMMY_FRAME;
1381
1382 old_sp = sp = read_sp ();
1383
1384 if (INNER_THAN (1, 2))
1385 {
1386 /* Stack grows down */
7a292a7a 1387 sp -= sizeof_dummy1;
c906108c
SS
1388 start_sp = sp;
1389 }
1390 else
1391 {
1392 /* Stack grows up */
1393 start_sp = sp;
7a292a7a 1394 sp += sizeof_dummy1;
c906108c
SS
1395 }
1396
1397 funaddr = find_function_addr (function, &value_type);
1398 CHECK_TYPEDEF (value_type);
1399
1400 {
1401 struct block *b = block_for_pc (funaddr);
1402 /* If compiled without -g, assume GCC 2. */
1403 using_gcc = (b == NULL ? 2 : BLOCK_GCC_COMPILED (b));
1404 }
1405
1406 /* Are we returning a value using a structure return or a normal
1407 value return? */
1408
1409 struct_return = using_struct_return (function, funaddr, value_type,
1410 using_gcc);
1411
1412 /* Create a call sequence customized for this function
1413 and the number of arguments for it. */
7a292a7a 1414 for (i = 0; i < (int) (SIZEOF_CALL_DUMMY_WORDS / sizeof (dummy[0])); i++)
c906108c
SS
1415 store_unsigned_integer (&dummy1[i * REGISTER_SIZE],
1416 REGISTER_SIZE,
c5aa993b 1417 (ULONGEST) dummy[i]);
c906108c
SS
1418
1419#ifdef GDB_TARGET_IS_HPPA
1420 real_pc = FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, args,
1421 value_type, using_gcc);
1422#else
1423 FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, args,
1424 value_type, using_gcc);
1425 real_pc = start_sp;
1426#endif
1427
7a292a7a
SS
1428 if (CALL_DUMMY_LOCATION == ON_STACK)
1429 {
c5aa993b 1430 write_memory (start_sp, (char *) dummy1, sizeof_dummy1);
7a292a7a 1431 }
c906108c 1432
7a292a7a
SS
1433 if (CALL_DUMMY_LOCATION == BEFORE_TEXT_END)
1434 {
1435 /* Convex Unix prohibits executing in the stack segment. */
1436 /* Hope there is empty room at the top of the text segment. */
1437 extern CORE_ADDR text_end;
392a587b 1438 static int checked = 0;
7a292a7a
SS
1439 if (!checked)
1440 for (start_sp = text_end - sizeof_dummy1; start_sp < text_end; ++start_sp)
1441 if (read_memory_integer (start_sp, 1) != 0)
1442 error ("text segment full -- no place to put call");
1443 checked = 1;
1444 sp = old_sp;
1445 real_pc = text_end - sizeof_dummy1;
c5aa993b 1446 write_memory (real_pc, (char *) dummy1, sizeof_dummy1);
7a292a7a 1447 }
c5aa993b 1448
7a292a7a
SS
1449 if (CALL_DUMMY_LOCATION == AFTER_TEXT_END)
1450 {
1451 extern CORE_ADDR text_end;
1452 int errcode;
1453 sp = old_sp;
1454 real_pc = text_end;
c5aa993b 1455 errcode = target_write_memory (real_pc, (char *) dummy1, sizeof_dummy1);
7a292a7a
SS
1456 if (errcode != 0)
1457 error ("Cannot write text segment -- call_function failed");
1458 }
c906108c 1459
7a292a7a
SS
1460 if (CALL_DUMMY_LOCATION == AT_ENTRY_POINT)
1461 {
1462 real_pc = funaddr;
1463 }
c906108c
SS
1464
1465#ifdef lint
c5aa993b 1466 sp = old_sp; /* It really is used, for some ifdef's... */
c906108c
SS
1467#endif
1468
1469 if (nargs < TYPE_NFIELDS (ftype))
1470 error ("too few arguments in function call");
1471
1472 for (i = nargs - 1; i >= 0; i--)
1473 {
1474 /* If we're off the end of the known arguments, do the standard
7b83ea04
AC
1475 promotions. FIXME: if we had a prototype, this should only
1476 be allowed if ... were present. */
c906108c
SS
1477 if (i >= TYPE_NFIELDS (ftype))
1478 args[i] = value_arg_coerce (args[i], NULL, 0);
1479
c5aa993b 1480 else
c906108c
SS
1481 {
1482 int is_prototyped = TYPE_FLAGS (ftype) & TYPE_FLAG_PROTOTYPED;
1483 param_type = TYPE_FIELD_TYPE (ftype, i);
1484
1485 args[i] = value_arg_coerce (args[i], param_type, is_prototyped);
1486 }
1487
070ad9f0
DB
1488 /*elz: this code is to handle the case in which the function to be called
1489 has a pointer to function as parameter and the corresponding actual argument
7b83ea04
AC
1490 is the address of a function and not a pointer to function variable.
1491 In aCC compiled code, the calls through pointers to functions (in the body
1492 of the function called by hand) are made via $$dyncall_external which
070ad9f0
DB
1493 requires some registers setting, this is taken care of if we call
1494 via a function pointer variable, but not via a function address.
7b83ea04 1495 In cc this is not a problem. */
c906108c
SS
1496
1497 if (using_gcc == 0)
1498 if (param_type)
c5aa993b 1499 /* if this parameter is a pointer to function */
c906108c
SS
1500 if (TYPE_CODE (param_type) == TYPE_CODE_PTR)
1501 if (TYPE_CODE (param_type->target_type) == TYPE_CODE_FUNC)
070ad9f0 1502 /* elz: FIXME here should go the test about the compiler used
7b83ea04 1503 to compile the target. We want to issue the error
070ad9f0
DB
1504 message only if the compiler used was HP's aCC.
1505 If we used HP's cc, then there is no problem and no need
7b83ea04 1506 to return at this point */
c5aa993b 1507 if (using_gcc == 0) /* && compiler == aCC */
c906108c 1508 /* go see if the actual parameter is a variable of type
c5aa993b 1509 pointer to function or just a function */
c906108c
SS
1510 if (args[i]->lval == not_lval)
1511 {
1512 char *arg_name;
c5aa993b
JM
1513 if (find_pc_partial_function ((CORE_ADDR) args[i]->aligner.contents[0], &arg_name, NULL, NULL))
1514 error ("\
c906108c
SS
1515You cannot use function <%s> as argument. \n\
1516You must use a pointer to function type variable. Command ignored.", arg_name);
c5aa993b 1517 }
c906108c
SS
1518 }
1519
d03e67c9
AC
1520 if (REG_STRUCT_HAS_ADDR_P ())
1521 {
1522 /* This is a machine like the sparc, where we may need to pass a
1523 pointer to the structure, not the structure itself. */
1524 for (i = nargs - 1; i >= 0; i--)
1525 {
1526 struct type *arg_type = check_typedef (VALUE_TYPE (args[i]));
1527 if ((TYPE_CODE (arg_type) == TYPE_CODE_STRUCT
1528 || TYPE_CODE (arg_type) == TYPE_CODE_UNION
1529 || TYPE_CODE (arg_type) == TYPE_CODE_ARRAY
1530 || TYPE_CODE (arg_type) == TYPE_CODE_STRING
1531 || TYPE_CODE (arg_type) == TYPE_CODE_BITSTRING
1532 || TYPE_CODE (arg_type) == TYPE_CODE_SET
1533 || (TYPE_CODE (arg_type) == TYPE_CODE_FLT
1534 && TYPE_LENGTH (arg_type) > 8)
1535 )
1536 && REG_STRUCT_HAS_ADDR (using_gcc, arg_type))
1537 {
1538 CORE_ADDR addr;
1539 int len; /* = TYPE_LENGTH (arg_type); */
1540 int aligned_len;
1541 arg_type = check_typedef (VALUE_ENCLOSING_TYPE (args[i]));
1542 len = TYPE_LENGTH (arg_type);
1543
1544 if (STACK_ALIGN_P ())
1545 /* MVS 11/22/96: I think at least some of this
1546 stack_align code is really broken. Better to let
1547 PUSH_ARGUMENTS adjust the stack in a target-defined
1548 manner. */
1549 aligned_len = STACK_ALIGN (len);
1550 else
1551 aligned_len = len;
1552 if (INNER_THAN (1, 2))
1553 {
1554 /* stack grows downward */
1555 sp -= aligned_len;
1556 }
1557 else
1558 {
1559 /* The stack grows up, so the address of the thing
1560 we push is the stack pointer before we push it. */
1561 addr = sp;
1562 }
1563 /* Push the structure. */
1564 write_memory (sp, VALUE_CONTENTS_ALL (args[i]), len);
1565 if (INNER_THAN (1, 2))
1566 {
1567 /* The stack grows down, so the address of the thing
1568 we push is the stack pointer after we push it. */
1569 addr = sp;
1570 }
1571 else
1572 {
1573 /* stack grows upward */
1574 sp += aligned_len;
1575 }
1576 /* The value we're going to pass is the address of the
1577 thing we just pushed. */
1578 /*args[i] = value_from_longest (lookup_pointer_type (value_type),
1579 (LONGEST) addr); */
1580 args[i] = value_from_pointer (lookup_pointer_type (arg_type),
1581 addr);
1582 }
1583 }
1584 }
1585
c906108c
SS
1586
1587 /* Reserve space for the return structure to be written on the
1588 stack, if necessary */
1589
1590 if (struct_return)
1591 {
1592 int len = TYPE_LENGTH (value_type);
2ada493a
AC
1593 if (STACK_ALIGN_P ())
1594 /* MVS 11/22/96: I think at least some of this stack_align
1595 code is really broken. Better to let PUSH_ARGUMENTS adjust
1596 the stack in a target-defined manner. */
1597 len = STACK_ALIGN (len);
c906108c
SS
1598 if (INNER_THAN (1, 2))
1599 {
1600 /* stack grows downward */
1601 sp -= len;
1602 struct_addr = sp;
1603 }
1604 else
1605 {
1606 /* stack grows upward */
1607 struct_addr = sp;
1608 sp += len;
1609 }
1610 }
1611
1612/* elz: on HPPA no need for this extra alignment, maybe it is needed
1613 on other architectures. This is because all the alignment is taken care
070ad9f0 1614 of in the above code (ifdef REG_STRUCT_HAS_ADDR) and in
c5aa993b 1615 hppa_push_arguments */
c906108c
SS
1616#ifndef NO_EXTRA_ALIGNMENT_NEEDED
1617
c906108c
SS
1618 /* MVS 11/22/96: I think at least some of this stack_align code is
1619 really broken. Better to let PUSH_ARGUMENTS adjust the stack in
1620 a target-defined manner. */
2ada493a 1621 if (STACK_ALIGN_P () && INNER_THAN (1, 2))
c906108c
SS
1622 {
1623 /* If stack grows down, we must leave a hole at the top. */
1624 int len = 0;
1625
1626 for (i = nargs - 1; i >= 0; i--)
1627 len += TYPE_LENGTH (VALUE_ENCLOSING_TYPE (args[i]));
7a292a7a
SS
1628 if (CALL_DUMMY_STACK_ADJUST_P)
1629 len += CALL_DUMMY_STACK_ADJUST;
c906108c
SS
1630 sp -= STACK_ALIGN (len) - len;
1631 }
c906108c
SS
1632#endif /* NO_EXTRA_ALIGNMENT_NEEDED */
1633
392a587b 1634 sp = PUSH_ARGUMENTS (nargs, args, sp, struct_return, struct_addr);
c906108c
SS
1635
1636#ifdef PUSH_RETURN_ADDRESS /* for targets that use no CALL_DUMMY */
1637 /* There are a number of targets now which actually don't write any
1638 CALL_DUMMY instructions into the target, but instead just save the
1639 machine state, push the arguments, and jump directly to the callee
1640 function. Since this doesn't actually involve executing a JSR/BSR
1641 instruction, the return address must be set up by hand, either by
1642 pushing onto the stack or copying into a return-address register
070ad9f0 1643 as appropriate. Formerly this has been done in PUSH_ARGUMENTS,
c906108c
SS
1644 but that's overloading its functionality a bit, so I'm making it
1645 explicit to do it here. */
c5aa993b
JM
1646 sp = PUSH_RETURN_ADDRESS (real_pc, sp);
1647#endif /* PUSH_RETURN_ADDRESS */
c906108c 1648
2ada493a 1649 if (STACK_ALIGN_P () && !INNER_THAN (1, 2))
c906108c
SS
1650 {
1651 /* If stack grows up, we must leave a hole at the bottom, note
7b83ea04 1652 that sp already has been advanced for the arguments! */
7a292a7a
SS
1653 if (CALL_DUMMY_STACK_ADJUST_P)
1654 sp += CALL_DUMMY_STACK_ADJUST;
c906108c
SS
1655 sp = STACK_ALIGN (sp);
1656 }
c906108c
SS
1657
1658/* XXX This seems wrong. For stacks that grow down we shouldn't do
1659 anything here! */
1660 /* MVS 11/22/96: I think at least some of this stack_align code is
1661 really broken. Better to let PUSH_ARGUMENTS adjust the stack in
1662 a target-defined manner. */
7a292a7a
SS
1663 if (CALL_DUMMY_STACK_ADJUST_P)
1664 if (INNER_THAN (1, 2))
1665 {
1666 /* stack grows downward */
1667 sp -= CALL_DUMMY_STACK_ADJUST;
1668 }
c906108c
SS
1669
1670 /* Store the address at which the structure is supposed to be
1671 written. Note that this (and the code which reserved the space
1672 above) assumes that gcc was used to compile this function. Since
1673 it doesn't cost us anything but space and if the function is pcc
1674 it will ignore this value, we will make that assumption.
1675
070ad9f0 1676 Also note that on some machines (like the sparc) pcc uses a
c906108c
SS
1677 convention like gcc's. */
1678
1679 if (struct_return)
1680 STORE_STRUCT_RETURN (struct_addr, sp);
1681
1682 /* Write the stack pointer. This is here because the statements above
1683 might fool with it. On SPARC, this write also stores the register
1684 window into the right place in the new stack frame, which otherwise
1685 wouldn't happen. (See store_inferior_registers in sparc-nat.c.) */
1686 write_sp (sp);
1687
d1e3cf49
AC
1688 if (SAVE_DUMMY_FRAME_TOS_P ())
1689 SAVE_DUMMY_FRAME_TOS (sp);
43ff13b4 1690
c906108c
SS
1691 {
1692 char retbuf[REGISTER_BYTES];
1693 char *name;
1694 struct symbol *symbol;
1695
1696 name = NULL;
1697 symbol = find_pc_function (funaddr);
1698 if (symbol)
1699 {
1700 name = SYMBOL_SOURCE_NAME (symbol);
1701 }
1702 else
1703 {
1704 /* Try the minimal symbols. */
1705 struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (funaddr);
1706
1707 if (msymbol)
1708 {
1709 name = SYMBOL_SOURCE_NAME (msymbol);
1710 }
1711 }
1712 if (name == NULL)
1713 {
1714 char format[80];
1715 sprintf (format, "at %s", local_hex_format ());
1716 name = alloca (80);
1717 /* FIXME-32x64: assumes funaddr fits in a long. */
1718 sprintf (name, format, (unsigned long) funaddr);
1719 }
1720
1721 /* Execute the stack dummy routine, calling FUNCTION.
1722 When it is done, discard the empty frame
1723 after storing the contents of all regs into retbuf. */
da59e081
JM
1724 rc = run_stack_dummy (real_pc + CALL_DUMMY_START_OFFSET, retbuf);
1725
1726 if (rc == 1)
1727 {
1728 /* We stopped inside the FUNCTION because of a random signal.
1729 Further execution of the FUNCTION is not allowed. */
1730
7b83ea04 1731 if (unwind_on_signal_p)
242bfc55
FN
1732 {
1733 /* The user wants the context restored. */
da59e081 1734
7b83ea04
AC
1735 /* We must get back to the frame we were before the dummy call. */
1736 POP_FRAME;
242bfc55
FN
1737
1738 /* FIXME: Insert a bunch of wrap_here; name can be very long if it's
1739 a C++ name with arguments and stuff. */
1740 error ("\
1741The program being debugged was signaled while in a function called from GDB.\n\
1742GDB has restored the context to what it was before the call.\n\
1743To change this behavior use \"set unwindonsignal off\"\n\
da59e081 1744Evaluation of the expression containing the function (%s) will be abandoned.",
242bfc55
FN
1745 name);
1746 }
1747 else
1748 {
1749 /* The user wants to stay in the frame where we stopped (default).*/
1750
1751 /* If we did the cleanups, we would print a spurious error
1752 message (Unable to restore previously selected frame),
1753 would write the registers from the inf_status (which is
1754 wrong), and would do other wrong things. */
1755 discard_cleanups (old_chain);
1756 discard_inferior_status (inf_status);
1757
1758 /* FIXME: Insert a bunch of wrap_here; name can be very long if it's
1759 a C++ name with arguments and stuff. */
1760 error ("\
1761The program being debugged was signaled while in a function called from GDB.\n\
1762GDB remains in the frame where the signal was received.\n\
1763To change this behavior use \"set unwindonsignal on\"\n\
1764Evaluation of the expression containing the function (%s) will be abandoned.",
1765 name);
1766 }
da59e081
JM
1767 }
1768
1769 if (rc == 2)
c906108c 1770 {
da59e081 1771 /* We hit a breakpoint inside the FUNCTION. */
c906108c 1772
7a292a7a
SS
1773 /* If we did the cleanups, we would print a spurious error
1774 message (Unable to restore previously selected frame),
1775 would write the registers from the inf_status (which is
1776 wrong), and would do other wrong things. */
c906108c 1777 discard_cleanups (old_chain);
7a292a7a 1778 discard_inferior_status (inf_status);
c906108c
SS
1779
1780 /* The following error message used to say "The expression
1781 which contained the function call has been discarded." It
1782 is a hard concept to explain in a few words. Ideally, GDB
1783 would be able to resume evaluation of the expression when
1784 the function finally is done executing. Perhaps someday
1785 this will be implemented (it would not be easy). */
1786
1787 /* FIXME: Insert a bunch of wrap_here; name can be very long if it's
1788 a C++ name with arguments and stuff. */
1789 error ("\
1790The program being debugged stopped while in a function called from GDB.\n\
1791When the function (%s) is done executing, GDB will silently\n\
1792stop (instead of continuing to evaluate the expression containing\n\
1793the function call).", name);
1794 }
1795
da59e081 1796 /* If we get here the called FUNCTION run to completion. */
c906108c
SS
1797 do_cleanups (old_chain);
1798
1799 /* Figure out the value returned by the function. */
1800/* elz: I defined this new macro for the hppa architecture only.
1801 this gives us a way to get the value returned by the function from the stack,
1802 at the same address we told the function to put it.
1803 We cannot assume on the pa that r28 still contains the address of the returned
1804 structure. Usually this will be overwritten by the callee.
1805 I don't know about other architectures, so I defined this macro
c5aa993b 1806 */
c906108c
SS
1807
1808#ifdef VALUE_RETURNED_FROM_STACK
1809 if (struct_return)
1810 return (value_ptr) VALUE_RETURNED_FROM_STACK (value_type, struct_addr);
1811#endif
1812
1813 return value_being_returned (value_type, retbuf, struct_return);
1814 }
1815}
7a292a7a 1816
c906108c
SS
1817value_ptr
1818call_function_by_hand (function, nargs, args)
1819 value_ptr function;
1820 int nargs;
1821 value_ptr *args;
1822{
7a292a7a
SS
1823 if (CALL_DUMMY_P)
1824 {
1825 return hand_function_call (function, nargs, args);
1826 }
1827 else
1828 {
1829 error ("Cannot invoke functions on this machine.");
1830 }
c906108c 1831}
c5aa993b 1832\f
7a292a7a 1833
c906108c 1834
c906108c
SS
1835/* Create a value for an array by allocating space in the inferior, copying
1836 the data into that space, and then setting up an array value.
1837
1838 The array bounds are set from LOWBOUND and HIGHBOUND, and the array is
1839 populated from the values passed in ELEMVEC.
1840
1841 The element type of the array is inherited from the type of the
1842 first element, and all elements must have the same size (though we
1843 don't currently enforce any restriction on their types). */
1844
1845value_ptr
1846value_array (lowbound, highbound, elemvec)
1847 int lowbound;
1848 int highbound;
1849 value_ptr *elemvec;
1850{
1851 int nelem;
1852 int idx;
1853 unsigned int typelength;
1854 value_ptr val;
1855 struct type *rangetype;
1856 struct type *arraytype;
1857 CORE_ADDR addr;
1858
1859 /* Validate that the bounds are reasonable and that each of the elements
1860 have the same size. */
1861
1862 nelem = highbound - lowbound + 1;
1863 if (nelem <= 0)
1864 {
1865 error ("bad array bounds (%d, %d)", lowbound, highbound);
1866 }
1867 typelength = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (elemvec[0]));
1868 for (idx = 1; idx < nelem; idx++)
1869 {
1870 if (TYPE_LENGTH (VALUE_ENCLOSING_TYPE (elemvec[idx])) != typelength)
1871 {
1872 error ("array elements must all be the same size");
1873 }
1874 }
1875
1876 rangetype = create_range_type ((struct type *) NULL, builtin_type_int,
1877 lowbound, highbound);
c5aa993b
JM
1878 arraytype = create_array_type ((struct type *) NULL,
1879 VALUE_ENCLOSING_TYPE (elemvec[0]), rangetype);
c906108c
SS
1880
1881 if (!current_language->c_style_arrays)
1882 {
1883 val = allocate_value (arraytype);
1884 for (idx = 0; idx < nelem; idx++)
1885 {
1886 memcpy (VALUE_CONTENTS_ALL_RAW (val) + (idx * typelength),
1887 VALUE_CONTENTS_ALL (elemvec[idx]),
1888 typelength);
1889 }
1890 VALUE_BFD_SECTION (val) = VALUE_BFD_SECTION (elemvec[0]);
1891 return val;
1892 }
1893
1894 /* Allocate space to store the array in the inferior, and then initialize
1895 it by copying in each element. FIXME: Is it worth it to create a
1896 local buffer in which to collect each value and then write all the
1897 bytes in one operation? */
1898
1899 addr = allocate_space_in_inferior (nelem * typelength);
1900 for (idx = 0; idx < nelem; idx++)
1901 {
1902 write_memory (addr + (idx * typelength), VALUE_CONTENTS_ALL (elemvec[idx]),
1903 typelength);
1904 }
1905
1906 /* Create the array type and set up an array value to be evaluated lazily. */
1907
1908 val = value_at_lazy (arraytype, addr, VALUE_BFD_SECTION (elemvec[0]));
1909 return (val);
1910}
1911
1912/* Create a value for a string constant by allocating space in the inferior,
1913 copying the data into that space, and returning the address with type
1914 TYPE_CODE_STRING. PTR points to the string constant data; LEN is number
1915 of characters.
1916 Note that string types are like array of char types with a lower bound of
1917 zero and an upper bound of LEN - 1. Also note that the string may contain
1918 embedded null bytes. */
1919
1920value_ptr
1921value_string (ptr, len)
1922 char *ptr;
1923 int len;
1924{
1925 value_ptr val;
1926 int lowbound = current_language->string_lower_bound;
1927 struct type *rangetype = create_range_type ((struct type *) NULL,
1928 builtin_type_int,
1929 lowbound, len + lowbound - 1);
1930 struct type *stringtype
c5aa993b 1931 = create_string_type ((struct type *) NULL, rangetype);
c906108c
SS
1932 CORE_ADDR addr;
1933
1934 if (current_language->c_style_arrays == 0)
1935 {
1936 val = allocate_value (stringtype);
1937 memcpy (VALUE_CONTENTS_RAW (val), ptr, len);
1938 return val;
1939 }
1940
1941
1942 /* Allocate space to store the string in the inferior, and then
1943 copy LEN bytes from PTR in gdb to that address in the inferior. */
1944
1945 addr = allocate_space_in_inferior (len);
1946 write_memory (addr, ptr, len);
1947
1948 val = value_at_lazy (stringtype, addr, NULL);
1949 return (val);
1950}
1951
1952value_ptr
1953value_bitstring (ptr, len)
1954 char *ptr;
1955 int len;
1956{
1957 value_ptr val;
1958 struct type *domain_type = create_range_type (NULL, builtin_type_int,
1959 0, len - 1);
c5aa993b 1960 struct type *type = create_set_type ((struct type *) NULL, domain_type);
c906108c
SS
1961 TYPE_CODE (type) = TYPE_CODE_BITSTRING;
1962 val = allocate_value (type);
1963 memcpy (VALUE_CONTENTS_RAW (val), ptr, TYPE_LENGTH (type));
1964 return val;
1965}
1966\f
1967/* See if we can pass arguments in T2 to a function which takes arguments
1968 of types T1. Both t1 and t2 are NULL-terminated vectors. If some
1969 arguments need coercion of some sort, then the coerced values are written
1970 into T2. Return value is 0 if the arguments could be matched, or the
1971 position at which they differ if not.
1972
1973 STATICP is nonzero if the T1 argument list came from a
1974 static member function.
1975
1976 For non-static member functions, we ignore the first argument,
1977 which is the type of the instance variable. This is because we want
1978 to handle calls with objects from derived classes. This is not
1979 entirely correct: we should actually check to make sure that a
1980 requested operation is type secure, shouldn't we? FIXME. */
1981
1982static int
1983typecmp (staticp, t1, t2)
1984 int staticp;
1985 struct type *t1[];
1986 value_ptr t2[];
1987{
1988 int i;
1989
1990 if (t2 == 0)
1991 return 1;
1992 if (staticp && t1 == 0)
1993 return t2[1] != 0;
1994 if (t1 == 0)
1995 return 1;
c5aa993b
JM
1996 if (TYPE_CODE (t1[0]) == TYPE_CODE_VOID)
1997 return 0;
1998 if (t1[!staticp] == 0)
1999 return 0;
c906108c
SS
2000 for (i = !staticp; t1[i] && TYPE_CODE (t1[i]) != TYPE_CODE_VOID; i++)
2001 {
c5aa993b
JM
2002 struct type *tt1, *tt2;
2003 if (!t2[i])
2004 return i + 1;
c906108c 2005 tt1 = check_typedef (t1[i]);
c5aa993b 2006 tt2 = check_typedef (VALUE_TYPE (t2[i]));
c906108c 2007 if (TYPE_CODE (tt1) == TYPE_CODE_REF
c5aa993b 2008 /* We should be doing hairy argument matching, as below. */
c906108c
SS
2009 && (TYPE_CODE (check_typedef (TYPE_TARGET_TYPE (tt1))) == TYPE_CODE (tt2)))
2010 {
2011 if (TYPE_CODE (tt2) == TYPE_CODE_ARRAY)
2012 t2[i] = value_coerce_array (t2[i]);
2013 else
2014 t2[i] = value_addr (t2[i]);
2015 continue;
2016 }
2017
2018 while (TYPE_CODE (tt1) == TYPE_CODE_PTR
c5aa993b
JM
2019 && (TYPE_CODE (tt2) == TYPE_CODE_ARRAY
2020 || TYPE_CODE (tt2) == TYPE_CODE_PTR))
c906108c 2021 {
c5aa993b
JM
2022 tt1 = check_typedef (TYPE_TARGET_TYPE (tt1));
2023 tt2 = check_typedef (TYPE_TARGET_TYPE (tt2));
c906108c 2024 }
c5aa993b
JM
2025 if (TYPE_CODE (tt1) == TYPE_CODE (tt2))
2026 continue;
c906108c
SS
2027 /* Array to pointer is a `trivial conversion' according to the ARM. */
2028
2029 /* We should be doing much hairier argument matching (see section 13.2
7b83ea04
AC
2030 of the ARM), but as a quick kludge, just check for the same type
2031 code. */
c906108c 2032 if (TYPE_CODE (t1[i]) != TYPE_CODE (VALUE_TYPE (t2[i])))
c5aa993b 2033 return i + 1;
c906108c 2034 }
c5aa993b
JM
2035 if (!t1[i])
2036 return 0;
2037 return t2[i] ? i + 1 : 0;
c906108c
SS
2038}
2039
2040/* Helper function used by value_struct_elt to recurse through baseclasses.
2041 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
2042 and search in it assuming it has (class) type TYPE.
2043 If found, return value, else return NULL.
2044
2045 If LOOKING_FOR_BASECLASS, then instead of looking for struct fields,
2046 look for a baseclass named NAME. */
2047
2048static value_ptr
2049search_struct_field (name, arg1, offset, type, looking_for_baseclass)
2050 char *name;
2051 register value_ptr arg1;
2052 int offset;
2053 register struct type *type;
2054 int looking_for_baseclass;
2055{
2056 int i;
2057 int nbases = TYPE_N_BASECLASSES (type);
2058
2059 CHECK_TYPEDEF (type);
2060
c5aa993b 2061 if (!looking_for_baseclass)
c906108c
SS
2062 for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
2063 {
2064 char *t_field_name = TYPE_FIELD_NAME (type, i);
2065
db577aea 2066 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
c906108c
SS
2067 {
2068 value_ptr v;
2069 if (TYPE_FIELD_STATIC (type, i))
2070 v = value_static_field (type, i);
2071 else
2072 v = value_primitive_field (arg1, offset, i, type);
2073 if (v == 0)
c5aa993b 2074 error ("there is no field named %s", name);
c906108c
SS
2075 return v;
2076 }
2077
2078 if (t_field_name
2079 && (t_field_name[0] == '\0'
2080 || (TYPE_CODE (type) == TYPE_CODE_UNION
db577aea 2081 && (strcmp_iw (t_field_name, "else") == 0))))
c906108c
SS
2082 {
2083 struct type *field_type = TYPE_FIELD_TYPE (type, i);
2084 if (TYPE_CODE (field_type) == TYPE_CODE_UNION
2085 || TYPE_CODE (field_type) == TYPE_CODE_STRUCT)
2086 {
2087 /* Look for a match through the fields of an anonymous union,
2088 or anonymous struct. C++ provides anonymous unions.
2089
2090 In the GNU Chill implementation of variant record types,
2091 each <alternative field> has an (anonymous) union type,
2092 each member of the union represents a <variant alternative>.
2093 Each <variant alternative> is represented as a struct,
2094 with a member for each <variant field>. */
c5aa993b 2095
c906108c
SS
2096 value_ptr v;
2097 int new_offset = offset;
2098
2099 /* This is pretty gross. In G++, the offset in an anonymous
2100 union is relative to the beginning of the enclosing struct.
2101 In the GNU Chill implementation of variant records,
2102 the bitpos is zero in an anonymous union field, so we
2103 have to add the offset of the union here. */
2104 if (TYPE_CODE (field_type) == TYPE_CODE_STRUCT
2105 || (TYPE_NFIELDS (field_type) > 0
2106 && TYPE_FIELD_BITPOS (field_type, 0) == 0))
2107 new_offset += TYPE_FIELD_BITPOS (type, i) / 8;
2108
2109 v = search_struct_field (name, arg1, new_offset, field_type,
2110 looking_for_baseclass);
2111 if (v)
2112 return v;
2113 }
2114 }
2115 }
2116
c5aa993b 2117 for (i = 0; i < nbases; i++)
c906108c
SS
2118 {
2119 value_ptr v;
2120 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
2121 /* If we are looking for baseclasses, this is what we get when we
7b83ea04
AC
2122 hit them. But it could happen that the base part's member name
2123 is not yet filled in. */
c906108c
SS
2124 int found_baseclass = (looking_for_baseclass
2125 && TYPE_BASECLASS_NAME (type, i) != NULL
db577aea 2126 && (strcmp_iw (name, TYPE_BASECLASS_NAME (type, i)) == 0));
c906108c
SS
2127
2128 if (BASETYPE_VIA_VIRTUAL (type, i))
2129 {
2130 int boffset;
2131 value_ptr v2 = allocate_value (basetype);
2132
2133 boffset = baseclass_offset (type, i,
2134 VALUE_CONTENTS (arg1) + offset,
2135 VALUE_ADDRESS (arg1)
c5aa993b 2136 + VALUE_OFFSET (arg1) + offset);
c906108c
SS
2137 if (boffset == -1)
2138 error ("virtual baseclass botch");
2139
2140 /* The virtual base class pointer might have been clobbered by the
2141 user program. Make sure that it still points to a valid memory
2142 location. */
2143
2144 boffset += offset;
2145 if (boffset < 0 || boffset >= TYPE_LENGTH (type))
2146 {
2147 CORE_ADDR base_addr;
c5aa993b 2148
c906108c
SS
2149 base_addr = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1) + boffset;
2150 if (target_read_memory (base_addr, VALUE_CONTENTS_RAW (v2),
2151 TYPE_LENGTH (basetype)) != 0)
2152 error ("virtual baseclass botch");
2153 VALUE_LVAL (v2) = lval_memory;
2154 VALUE_ADDRESS (v2) = base_addr;
2155 }
2156 else
2157 {
2158 VALUE_LVAL (v2) = VALUE_LVAL (arg1);
2159 VALUE_ADDRESS (v2) = VALUE_ADDRESS (arg1);
2160 VALUE_OFFSET (v2) = VALUE_OFFSET (arg1) + boffset;
2161 if (VALUE_LAZY (arg1))
2162 VALUE_LAZY (v2) = 1;
2163 else
2164 memcpy (VALUE_CONTENTS_RAW (v2),
2165 VALUE_CONTENTS_RAW (arg1) + boffset,
2166 TYPE_LENGTH (basetype));
2167 }
2168
2169 if (found_baseclass)
2170 return v2;
2171 v = search_struct_field (name, v2, 0, TYPE_BASECLASS (type, i),
2172 looking_for_baseclass);
2173 }
2174 else if (found_baseclass)
2175 v = value_primitive_field (arg1, offset, i, type);
2176 else
2177 v = search_struct_field (name, arg1,
c5aa993b 2178 offset + TYPE_BASECLASS_BITPOS (type, i) / 8,
c906108c 2179 basetype, looking_for_baseclass);
c5aa993b
JM
2180 if (v)
2181 return v;
c906108c
SS
2182 }
2183 return NULL;
2184}
2185
2186
2187/* Return the offset (in bytes) of the virtual base of type BASETYPE
2188 * in an object pointed to by VALADDR (on the host), assumed to be of
2189 * type TYPE. OFFSET is number of bytes beyond start of ARG to start
2190 * looking (in case VALADDR is the contents of an enclosing object).
2191 *
2192 * This routine recurses on the primary base of the derived class because
2193 * the virtual base entries of the primary base appear before the other
2194 * virtual base entries.
2195 *
2196 * If the virtual base is not found, a negative integer is returned.
2197 * The magnitude of the negative integer is the number of entries in
2198 * the virtual table to skip over (entries corresponding to various
2199 * ancestral classes in the chain of primary bases).
2200 *
2201 * Important: This assumes the HP / Taligent C++ runtime
2202 * conventions. Use baseclass_offset() instead to deal with g++
2203 * conventions. */
2204
2205void
c5aa993b
JM
2206find_rt_vbase_offset (type, basetype, valaddr, offset, boffset_p, skip_p)
2207 struct type *type;
2208 struct type *basetype;
2209 char *valaddr;
2210 int offset;
2211 int *boffset_p;
2212 int *skip_p;
c906108c 2213{
c5aa993b
JM
2214 int boffset; /* offset of virtual base */
2215 int index; /* displacement to use in virtual table */
c906108c 2216 int skip;
c5aa993b
JM
2217
2218 value_ptr vp;
2219 CORE_ADDR vtbl; /* the virtual table pointer */
2220 struct type *pbc; /* the primary base class */
c906108c
SS
2221
2222 /* Look for the virtual base recursively in the primary base, first.
2223 * This is because the derived class object and its primary base
2224 * subobject share the primary virtual table. */
c5aa993b 2225
c906108c 2226 boffset = 0;
c5aa993b 2227 pbc = TYPE_PRIMARY_BASE (type);
c906108c
SS
2228 if (pbc)
2229 {
2230 find_rt_vbase_offset (pbc, basetype, valaddr, offset, &boffset, &skip);
2231 if (skip < 0)
c5aa993b
JM
2232 {
2233 *boffset_p = boffset;
2234 *skip_p = -1;
2235 return;
2236 }
c906108c
SS
2237 }
2238 else
2239 skip = 0;
2240
2241
2242 /* Find the index of the virtual base according to HP/Taligent
2243 runtime spec. (Depth-first, left-to-right.) */
2244 index = virtual_base_index_skip_primaries (basetype, type);
2245
c5aa993b
JM
2246 if (index < 0)
2247 {
2248 *skip_p = skip + virtual_base_list_length_skip_primaries (type);
2249 *boffset_p = 0;
2250 return;
2251 }
c906108c 2252
c5aa993b 2253 /* pai: FIXME -- 32x64 possible problem */
c906108c 2254 /* First word (4 bytes) in object layout is the vtable pointer */
c5aa993b 2255 vtbl = *(CORE_ADDR *) (valaddr + offset);
c906108c 2256
c5aa993b 2257 /* Before the constructor is invoked, things are usually zero'd out. */
c906108c
SS
2258 if (vtbl == 0)
2259 error ("Couldn't find virtual table -- object may not be constructed yet.");
2260
2261
2262 /* Find virtual base's offset -- jump over entries for primary base
2263 * ancestors, then use the index computed above. But also adjust by
2264 * HP_ACC_VBASE_START for the vtable slots before the start of the
2265 * virtual base entries. Offset is negative -- virtual base entries
2266 * appear _before_ the address point of the virtual table. */
c5aa993b 2267
070ad9f0 2268 /* pai: FIXME -- 32x64 problem, if word = 8 bytes, change multiplier
c5aa993b 2269 & use long type */
c906108c
SS
2270
2271 /* epstein : FIXME -- added param for overlay section. May not be correct */
c5aa993b 2272 vp = value_at (builtin_type_int, vtbl + 4 * (-skip - index - HP_ACC_VBASE_START), NULL);
c906108c
SS
2273 boffset = value_as_long (vp);
2274 *skip_p = -1;
2275 *boffset_p = boffset;
2276 return;
2277}
2278
2279
2280/* Helper function used by value_struct_elt to recurse through baseclasses.
2281 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
2282 and search in it assuming it has (class) type TYPE.
2283 If found, return value, else if name matched and args not return (value)-1,
2284 else return NULL. */
2285
2286static value_ptr
2287search_struct_method (name, arg1p, args, offset, static_memfuncp, type)
2288 char *name;
2289 register value_ptr *arg1p, *args;
2290 int offset, *static_memfuncp;
2291 register struct type *type;
2292{
2293 int i;
2294 value_ptr v;
2295 int name_matched = 0;
2296 char dem_opname[64];
2297
2298 CHECK_TYPEDEF (type);
2299 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
2300 {
2301 char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
2302 /* FIXME! May need to check for ARM demangling here */
c5aa993b
JM
2303 if (strncmp (t_field_name, "__", 2) == 0 ||
2304 strncmp (t_field_name, "op", 2) == 0 ||
2305 strncmp (t_field_name, "type", 4) == 0)
c906108c 2306 {
c5aa993b
JM
2307 if (cplus_demangle_opname (t_field_name, dem_opname, DMGL_ANSI))
2308 t_field_name = dem_opname;
2309 else if (cplus_demangle_opname (t_field_name, dem_opname, 0))
c906108c 2310 t_field_name = dem_opname;
c906108c 2311 }
db577aea 2312 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
c906108c
SS
2313 {
2314 int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1;
2315 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
c5aa993b 2316 name_matched = 1;
c906108c
SS
2317
2318 if (j > 0 && args == 0)
2319 error ("cannot resolve overloaded method `%s': no arguments supplied", name);
2320 while (j >= 0)
2321 {
2322 if (TYPE_FN_FIELD_STUB (f, j))
2323 check_stub_method (type, i, j);
2324 if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j),
2325 TYPE_FN_FIELD_ARGS (f, j), args))
2326 {
2327 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
2328 return value_virtual_fn_field (arg1p, f, j, type, offset);
2329 if (TYPE_FN_FIELD_STATIC_P (f, j) && static_memfuncp)
2330 *static_memfuncp = 1;
2331 v = value_fn_field (arg1p, f, j, type, offset);
c5aa993b
JM
2332 if (v != NULL)
2333 return v;
c906108c
SS
2334 }
2335 j--;
2336 }
2337 }
2338 }
2339
2340 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
2341 {
2342 int base_offset;
2343
2344 if (BASETYPE_VIA_VIRTUAL (type, i))
2345 {
c5aa993b
JM
2346 if (TYPE_HAS_VTABLE (type))
2347 {
2348 /* HP aCC compiled type, search for virtual base offset
7b83ea04 2349 according to HP/Taligent runtime spec. */
c5aa993b
JM
2350 int skip;
2351 find_rt_vbase_offset (type, TYPE_BASECLASS (type, i),
2352 VALUE_CONTENTS_ALL (*arg1p),
2353 offset + VALUE_EMBEDDED_OFFSET (*arg1p),
2354 &base_offset, &skip);
2355 if (skip >= 0)
2356 error ("Virtual base class offset not found in vtable");
2357 }
2358 else
2359 {
2360 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
2361 char *base_valaddr;
2362
2363 /* The virtual base class pointer might have been clobbered by the
7b83ea04
AC
2364 user program. Make sure that it still points to a valid memory
2365 location. */
c5aa993b
JM
2366
2367 if (offset < 0 || offset >= TYPE_LENGTH (type))
2368 {
2369 base_valaddr = (char *) alloca (TYPE_LENGTH (baseclass));
2370 if (target_read_memory (VALUE_ADDRESS (*arg1p)
2371 + VALUE_OFFSET (*arg1p) + offset,
2372 base_valaddr,
2373 TYPE_LENGTH (baseclass)) != 0)
2374 error ("virtual baseclass botch");
2375 }
2376 else
2377 base_valaddr = VALUE_CONTENTS (*arg1p) + offset;
2378
2379 base_offset =
2380 baseclass_offset (type, i, base_valaddr,
2381 VALUE_ADDRESS (*arg1p)
2382 + VALUE_OFFSET (*arg1p) + offset);
2383 if (base_offset == -1)
2384 error ("virtual baseclass botch");
2385 }
2386 }
c906108c
SS
2387 else
2388 {
2389 base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
c5aa993b 2390 }
c906108c
SS
2391 v = search_struct_method (name, arg1p, args, base_offset + offset,
2392 static_memfuncp, TYPE_BASECLASS (type, i));
c5aa993b 2393 if (v == (value_ptr) - 1)
c906108c
SS
2394 {
2395 name_matched = 1;
2396 }
2397 else if (v)
2398 {
2399/* FIXME-bothner: Why is this commented out? Why is it here? */
c5aa993b 2400/* *arg1p = arg1_tmp; */
c906108c 2401 return v;
c5aa993b 2402 }
c906108c 2403 }
c5aa993b
JM
2404 if (name_matched)
2405 return (value_ptr) - 1;
2406 else
2407 return NULL;
c906108c
SS
2408}
2409
2410/* Given *ARGP, a value of type (pointer to a)* structure/union,
2411 extract the component named NAME from the ultimate target structure/union
2412 and return it as a value with its appropriate type.
2413 ERR is used in the error message if *ARGP's type is wrong.
2414
2415 C++: ARGS is a list of argument types to aid in the selection of
2416 an appropriate method. Also, handle derived types.
2417
2418 STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
2419 where the truthvalue of whether the function that was resolved was
2420 a static member function or not is stored.
2421
2422 ERR is an error message to be printed in case the field is not found. */
2423
2424value_ptr
2425value_struct_elt (argp, args, name, static_memfuncp, err)
2426 register value_ptr *argp, *args;
2427 char *name;
2428 int *static_memfuncp;
2429 char *err;
2430{
2431 register struct type *t;
2432 value_ptr v;
2433
2434 COERCE_ARRAY (*argp);
2435
2436 t = check_typedef (VALUE_TYPE (*argp));
2437
2438 /* Follow pointers until we get to a non-pointer. */
2439
2440 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
2441 {
2442 *argp = value_ind (*argp);
2443 /* Don't coerce fn pointer to fn and then back again! */
2444 if (TYPE_CODE (VALUE_TYPE (*argp)) != TYPE_CODE_FUNC)
2445 COERCE_ARRAY (*argp);
2446 t = check_typedef (VALUE_TYPE (*argp));
2447 }
2448
2449 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
2450 error ("not implemented: member type in value_struct_elt");
2451
c5aa993b 2452 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
c906108c
SS
2453 && TYPE_CODE (t) != TYPE_CODE_UNION)
2454 error ("Attempt to extract a component of a value that is not a %s.", err);
2455
2456 /* Assume it's not, unless we see that it is. */
2457 if (static_memfuncp)
c5aa993b 2458 *static_memfuncp = 0;
c906108c
SS
2459
2460 if (!args)
2461 {
2462 /* if there are no arguments ...do this... */
2463
2464 /* Try as a field first, because if we succeed, there
7b83ea04 2465 is less work to be done. */
c906108c
SS
2466 v = search_struct_field (name, *argp, 0, t, 0);
2467 if (v)
2468 return v;
2469
2470 /* C++: If it was not found as a data field, then try to
7b83ea04 2471 return it as a pointer to a method. */
c906108c
SS
2472
2473 if (destructor_name_p (name, t))
2474 error ("Cannot get value of destructor");
2475
2476 v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
2477
c5aa993b 2478 if (v == (value_ptr) - 1)
c906108c
SS
2479 error ("Cannot take address of a method");
2480 else if (v == 0)
2481 {
2482 if (TYPE_NFN_FIELDS (t))
2483 error ("There is no member or method named %s.", name);
2484 else
2485 error ("There is no member named %s.", name);
2486 }
2487 return v;
2488 }
2489
2490 if (destructor_name_p (name, t))
2491 {
2492 if (!args[1])
2493 {
2494 /* Destructors are a special case. */
2495 int m_index, f_index;
2496
2497 v = NULL;
2498 if (get_destructor_fn_field (t, &m_index, &f_index))
2499 {
2500 v = value_fn_field (NULL, TYPE_FN_FIELDLIST1 (t, m_index),
2501 f_index, NULL, 0);
2502 }
2503 if (v == NULL)
2504 error ("could not find destructor function named %s.", name);
2505 else
2506 return v;
2507 }
2508 else
2509 {
2510 error ("destructor should not have any argument");
2511 }
2512 }
2513 else
2514 v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
2515
c5aa993b 2516 if (v == (value_ptr) - 1)
c906108c 2517 {
c5aa993b 2518 error ("Argument list of %s mismatch with component in the structure.", name);
c906108c
SS
2519 }
2520 else if (v == 0)
2521 {
2522 /* See if user tried to invoke data as function. If so,
7b83ea04
AC
2523 hand it back. If it's not callable (i.e., a pointer to function),
2524 gdb should give an error. */
c906108c
SS
2525 v = search_struct_field (name, *argp, 0, t, 0);
2526 }
2527
2528 if (!v)
2529 error ("Structure has no component named %s.", name);
2530 return v;
2531}
2532
2533/* Search through the methods of an object (and its bases)
2534 * to find a specified method. Return the pointer to the
2535 * fn_field list of overloaded instances.
2536 * Helper function for value_find_oload_list.
2537 * ARGP is a pointer to a pointer to a value (the object)
2538 * METHOD is a string containing the method name
2539 * OFFSET is the offset within the value
2540 * STATIC_MEMFUNCP is set if the method is static
2541 * TYPE is the assumed type of the object
2542 * NUM_FNS is the number of overloaded instances
2543 * BASETYPE is set to the actual type of the subobject where the method is found
2544 * BOFFSET is the offset of the base subobject where the method is found */
2545
7a292a7a 2546static struct fn_field *
c906108c 2547find_method_list (argp, method, offset, static_memfuncp, type, num_fns, basetype, boffset)
7a292a7a 2548 value_ptr *argp;
c5aa993b 2549 char *method;
7a292a7a 2550 int offset;
c5aa993b
JM
2551 int *static_memfuncp;
2552 struct type *type;
2553 int *num_fns;
2554 struct type **basetype;
2555 int *boffset;
c906108c
SS
2556{
2557 int i;
c5aa993b 2558 struct fn_field *f;
c906108c
SS
2559 CHECK_TYPEDEF (type);
2560
2561 *num_fns = 0;
2562
c5aa993b
JM
2563 /* First check in object itself */
2564 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
c906108c
SS
2565 {
2566 /* pai: FIXME What about operators and type conversions? */
c5aa993b 2567 char *fn_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
db577aea 2568 if (fn_field_name && (strcmp_iw (fn_field_name, method) == 0))
c5aa993b
JM
2569 {
2570 *num_fns = TYPE_FN_FIELDLIST_LENGTH (type, i);
2571 *basetype = type;
2572 *boffset = offset;
2573 return TYPE_FN_FIELDLIST1 (type, i);
2574 }
2575 }
2576
c906108c
SS
2577 /* Not found in object, check in base subobjects */
2578 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
2579 {
2580 int base_offset;
2581 if (BASETYPE_VIA_VIRTUAL (type, i))
2582 {
c5aa993b
JM
2583 if (TYPE_HAS_VTABLE (type))
2584 {
2585 /* HP aCC compiled type, search for virtual base offset
2586 * according to HP/Taligent runtime spec. */
2587 int skip;
2588 find_rt_vbase_offset (type, TYPE_BASECLASS (type, i),
2589 VALUE_CONTENTS_ALL (*argp),
2590 offset + VALUE_EMBEDDED_OFFSET (*argp),
2591 &base_offset, &skip);
2592 if (skip >= 0)
2593 error ("Virtual base class offset not found in vtable");
2594 }
2595 else
2596 {
2597 /* probably g++ runtime model */
2598 base_offset = VALUE_OFFSET (*argp) + offset;
2599 base_offset =
2600 baseclass_offset (type, i,
2601 VALUE_CONTENTS (*argp) + base_offset,
2602 VALUE_ADDRESS (*argp) + base_offset);
2603 if (base_offset == -1)
2604 error ("virtual baseclass botch");
2605 }
2606 }
2607 else
2608 /* non-virtual base, simply use bit position from debug info */
c906108c
SS
2609 {
2610 base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
c5aa993b 2611 }
c906108c 2612 f = find_method_list (argp, method, base_offset + offset,
c5aa993b 2613 static_memfuncp, TYPE_BASECLASS (type, i), num_fns, basetype, boffset);
c906108c 2614 if (f)
c5aa993b 2615 return f;
c906108c 2616 }
c5aa993b 2617 return NULL;
c906108c
SS
2618}
2619
2620/* Return the list of overloaded methods of a specified name.
2621 * ARGP is a pointer to a pointer to a value (the object)
2622 * METHOD is the method name
2623 * OFFSET is the offset within the value contents
2624 * STATIC_MEMFUNCP is set if the method is static
2625 * NUM_FNS is the number of overloaded instances
2626 * BASETYPE is set to the type of the base subobject that defines the method
2627 * BOFFSET is the offset of the base subobject which defines the method */
2628
2629struct fn_field *
2630value_find_oload_method_list (argp, method, offset, static_memfuncp, num_fns, basetype, boffset)
c5aa993b
JM
2631 value_ptr *argp;
2632 char *method;
2633 int offset;
2634 int *static_memfuncp;
2635 int *num_fns;
2636 struct type **basetype;
2637 int *boffset;
c906108c 2638{
c5aa993b 2639 struct type *t;
c906108c
SS
2640
2641 t = check_typedef (VALUE_TYPE (*argp));
2642
c5aa993b 2643 /* code snarfed from value_struct_elt */
c906108c
SS
2644 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
2645 {
2646 *argp = value_ind (*argp);
2647 /* Don't coerce fn pointer to fn and then back again! */
2648 if (TYPE_CODE (VALUE_TYPE (*argp)) != TYPE_CODE_FUNC)
2649 COERCE_ARRAY (*argp);
2650 t = check_typedef (VALUE_TYPE (*argp));
2651 }
c5aa993b 2652
c906108c
SS
2653 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
2654 error ("Not implemented: member type in value_find_oload_lis");
c5aa993b
JM
2655
2656 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
2657 && TYPE_CODE (t) != TYPE_CODE_UNION)
c906108c 2658 error ("Attempt to extract a component of a value that is not a struct or union");
c5aa993b 2659
c906108c
SS
2660 /* Assume it's not static, unless we see that it is. */
2661 if (static_memfuncp)
c5aa993b 2662 *static_memfuncp = 0;
c906108c
SS
2663
2664 return find_method_list (argp, method, 0, static_memfuncp, t, num_fns, basetype, boffset);
c5aa993b 2665
c906108c
SS
2666}
2667
2668/* Given an array of argument types (ARGTYPES) (which includes an
2669 entry for "this" in the case of C++ methods), the number of
2670 arguments NARGS, the NAME of a function whether it's a method or
2671 not (METHOD), and the degree of laxness (LAX) in conforming to
2672 overload resolution rules in ANSI C++, find the best function that
2673 matches on the argument types according to the overload resolution
2674 rules.
2675
2676 In the case of class methods, the parameter OBJ is an object value
2677 in which to search for overloaded methods.
2678
2679 In the case of non-method functions, the parameter FSYM is a symbol
2680 corresponding to one of the overloaded functions.
2681
2682 Return value is an integer: 0 -> good match, 10 -> debugger applied
2683 non-standard coercions, 100 -> incompatible.
2684
2685 If a method is being searched for, VALP will hold the value.
2686 If a non-method is being searched for, SYMP will hold the symbol for it.
2687
2688 If a method is being searched for, and it is a static method,
2689 then STATICP will point to a non-zero value.
2690
2691 Note: This function does *not* check the value of
2692 overload_resolution. Caller must check it to see whether overload
2693 resolution is permitted.
c5aa993b 2694 */
c906108c
SS
2695
2696int
2697find_overload_match (arg_types, nargs, name, method, lax, obj, fsym, valp, symp, staticp)
c5aa993b
JM
2698 struct type **arg_types;
2699 int nargs;
2700 char *name;
2701 int method;
2702 int lax;
2703 value_ptr obj;
2704 struct symbol *fsym;
2705 value_ptr *valp;
2706 struct symbol **symp;
2707 int *staticp;
c906108c
SS
2708{
2709 int nparms;
c5aa993b 2710 struct type **parm_types;
c906108c 2711 int champ_nparms = 0;
c5aa993b
JM
2712
2713 short oload_champ = -1; /* Index of best overloaded function */
2714 short oload_ambiguous = 0; /* Current ambiguity state for overload resolution */
2715 /* 0 => no ambiguity, 1 => two good funcs, 2 => incomparable funcs */
2716 short oload_ambig_champ = -1; /* 2nd contender for best match */
2717 short oload_non_standard = 0; /* did we have to use non-standard conversions? */
2718 short oload_incompatible = 0; /* are args supplied incompatible with any function? */
2719
2720 struct badness_vector *bv; /* A measure of how good an overloaded instance is */
2721 struct badness_vector *oload_champ_bv = NULL; /* The measure for the current best match */
2722
c906108c 2723 value_ptr temp = obj;
c5aa993b
JM
2724 struct fn_field *fns_ptr = NULL; /* For methods, the list of overloaded methods */
2725 struct symbol **oload_syms = NULL; /* For non-methods, the list of overloaded function symbols */
2726 int num_fns = 0; /* Number of overloaded instances being considered */
2727 struct type *basetype = NULL;
c906108c
SS
2728 int boffset;
2729 register int jj;
2730 register int ix;
2731
c5aa993b
JM
2732 char *obj_type_name = NULL;
2733 char *func_name = NULL;
c906108c
SS
2734
2735 /* Get the list of overloaded methods or functions */
2736 if (method)
2737 {
db577aea
AC
2738 int i;
2739 int len;
2740 struct type *domain;
c906108c
SS
2741 obj_type_name = TYPE_NAME (VALUE_TYPE (obj));
2742 /* Hack: evaluate_subexp_standard often passes in a pointer
7b83ea04 2743 value rather than the object itself, so try again */
c906108c 2744 if ((!obj_type_name || !*obj_type_name) &&
c5aa993b
JM
2745 (TYPE_CODE (VALUE_TYPE (obj)) == TYPE_CODE_PTR))
2746 obj_type_name = TYPE_NAME (TYPE_TARGET_TYPE (VALUE_TYPE (obj)));
c906108c
SS
2747
2748 fns_ptr = value_find_oload_method_list (&temp, name, 0,
c5aa993b
JM
2749 staticp,
2750 &num_fns,
2751 &basetype, &boffset);
c906108c 2752 if (!fns_ptr || !num_fns)
c5aa993b
JM
2753 error ("Couldn't find method %s%s%s",
2754 obj_type_name,
2755 (obj_type_name && *obj_type_name) ? "::" : "",
2756 name);
db577aea
AC
2757 domain = TYPE_DOMAIN_TYPE (fns_ptr[0].type);
2758 len = TYPE_NFN_FIELDS (domain);
2759 /* NOTE: dan/2000-03-10: This stuff is for STABS, which won't
2760 give us the info we need directly in the types. We have to
2761 use the method stub conversion to get it. Be aware that this
2762 is by no means perfect, and if you use STABS, please move to
2763 DWARF-2, or something like it, because trying to improve
2764 overloading using STABS is really a waste of time. */
2765 for (i = 0; i < len; i++)
2766 {
2767 int j;
2768 struct fn_field *f = TYPE_FN_FIELDLIST1 (domain, i);
2769 int len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
2770
2771 for (j = 0; j < len2; j++)
2772 {
070ad9f0 2773 if (TYPE_FN_FIELD_STUB (f, j) && (!strcmp_iw (TYPE_FN_FIELDLIST_NAME (domain,i),name)))
db577aea
AC
2774 check_stub_method (domain, i, j);
2775 }
2776 }
c906108c
SS
2777 }
2778 else
2779 {
2780 int i = -1;
2781 func_name = cplus_demangle (SYMBOL_NAME (fsym), DMGL_NO_OPTS);
2782
917317f4 2783 /* If the name is NULL this must be a C-style function.
7b83ea04 2784 Just return the same symbol. */
917317f4 2785 if (!func_name)
7b83ea04 2786 {
917317f4 2787 *symp = fsym;
7b83ea04
AC
2788 return 0;
2789 }
917317f4 2790
c906108c
SS
2791 oload_syms = make_symbol_overload_list (fsym);
2792 while (oload_syms[++i])
c5aa993b 2793 num_fns++;
c906108c 2794 if (!num_fns)
c5aa993b 2795 error ("Couldn't find function %s", func_name);
c906108c 2796 }
c5aa993b 2797
c906108c
SS
2798 oload_champ_bv = NULL;
2799
c5aa993b 2800 /* Consider each candidate in turn */
c906108c
SS
2801 for (ix = 0; ix < num_fns; ix++)
2802 {
db577aea
AC
2803 if (method)
2804 {
2805 /* For static member functions, we won't have a this pointer, but nothing
2806 else seems to handle them right now, so we just pretend ourselves */
2807 nparms=0;
2808
2809 if (TYPE_FN_FIELD_ARGS(fns_ptr,ix))
2810 {
2811 while (TYPE_CODE(TYPE_FN_FIELD_ARGS(fns_ptr,ix)[nparms]) != TYPE_CODE_VOID)
2812 nparms++;
2813 }
2814 }
2815 else
2816 {
2817 /* If it's not a method, this is the proper place */
2818 nparms=TYPE_NFIELDS(SYMBOL_TYPE(oload_syms[ix]));
2819 }
c906108c 2820
c5aa993b 2821 /* Prepare array of parameter types */
c906108c
SS
2822 parm_types = (struct type **) xmalloc (nparms * (sizeof (struct type *)));
2823 for (jj = 0; jj < nparms; jj++)
db577aea
AC
2824 parm_types[jj] = (method
2825 ? (TYPE_FN_FIELD_ARGS (fns_ptr, ix)[jj])
2826 : TYPE_FIELD_TYPE (SYMBOL_TYPE (oload_syms[ix]), jj));
c906108c
SS
2827
2828 /* Compare parameter types to supplied argument types */
2829 bv = rank_function (parm_types, nparms, arg_types, nargs);
c5aa993b 2830
c906108c 2831 if (!oload_champ_bv)
c5aa993b
JM
2832 {
2833 oload_champ_bv = bv;
2834 oload_champ = 0;
2835 champ_nparms = nparms;
2836 }
c906108c 2837 else
c5aa993b
JM
2838 /* See whether current candidate is better or worse than previous best */
2839 switch (compare_badness (bv, oload_champ_bv))
2840 {
2841 case 0:
2842 oload_ambiguous = 1; /* top two contenders are equally good */
2843 oload_ambig_champ = ix;
2844 break;
2845 case 1:
2846 oload_ambiguous = 2; /* incomparable top contenders */
2847 oload_ambig_champ = ix;
2848 break;
2849 case 2:
2850 oload_champ_bv = bv; /* new champion, record details */
2851 oload_ambiguous = 0;
2852 oload_champ = ix;
2853 oload_ambig_champ = -1;
2854 champ_nparms = nparms;
2855 break;
2856 case 3:
2857 default:
2858 break;
2859 }
c906108c 2860 free (parm_types);
070ad9f0
DB
2861if (overload_debug)
2862{
c906108c 2863 if (method)
070ad9f0 2864 fprintf_filtered (gdb_stderr,"Overloaded method instance %s, # of parms %d\n", fns_ptr[ix].physname, nparms);
c906108c 2865 else
070ad9f0 2866 fprintf_filtered (gdb_stderr,"Overloaded function instance %s # of parms %d\n", SYMBOL_DEMANGLED_NAME (oload_syms[ix]), nparms);
db577aea 2867 for (jj = 0; jj < nargs; jj++)
070ad9f0
DB
2868 fprintf_filtered (gdb_stderr,"...Badness @ %d : %d\n", jj, bv->rank[jj]);
2869 fprintf_filtered (gdb_stderr,"Overload resolution champion is %d, ambiguous? %d\n", oload_champ, oload_ambiguous);
2870}
c5aa993b 2871 } /* end loop over all candidates */
db577aea
AC
2872 /* NOTE: dan/2000-03-10: Seems to be a better idea to just pick one
2873 if they have the exact same goodness. This is because there is no
2874 way to differentiate based on return type, which we need to in
2875 cases like overloads of .begin() <It's both const and non-const> */
2876#if 0
c906108c
SS
2877 if (oload_ambiguous)
2878 {
2879 if (method)
c5aa993b
JM
2880 error ("Cannot resolve overloaded method %s%s%s to unique instance; disambiguate by specifying function signature",
2881 obj_type_name,
2882 (obj_type_name && *obj_type_name) ? "::" : "",
2883 name);
c906108c 2884 else
c5aa993b
JM
2885 error ("Cannot resolve overloaded function %s to unique instance; disambiguate by specifying function signature",
2886 func_name);
c906108c 2887 }
db577aea 2888#endif
c906108c 2889
c5aa993b 2890 /* Check how bad the best match is */
c906108c
SS
2891 for (ix = 1; ix <= nargs; ix++)
2892 {
2893 switch (oload_champ_bv->rank[ix])
c5aa993b
JM
2894 {
2895 case 10:
2896 oload_non_standard = 1; /* non-standard type conversions needed */
2897 break;
2898 case 100:
2899 oload_incompatible = 1; /* truly mismatched types */
2900 break;
2901 }
c906108c
SS
2902 }
2903 if (oload_incompatible)
2904 {
2905 if (method)
c5aa993b
JM
2906 error ("Cannot resolve method %s%s%s to any overloaded instance",
2907 obj_type_name,
2908 (obj_type_name && *obj_type_name) ? "::" : "",
2909 name);
c906108c 2910 else
c5aa993b
JM
2911 error ("Cannot resolve function %s to any overloaded instance",
2912 func_name);
c906108c
SS
2913 }
2914 else if (oload_non_standard)
2915 {
2916 if (method)
c5aa993b
JM
2917 warning ("Using non-standard conversion to match method %s%s%s to supplied arguments",
2918 obj_type_name,
2919 (obj_type_name && *obj_type_name) ? "::" : "",
2920 name);
c906108c 2921 else
c5aa993b
JM
2922 warning ("Using non-standard conversion to match function %s to supplied arguments",
2923 func_name);
c906108c
SS
2924 }
2925
2926 if (method)
2927 {
2928 if (TYPE_FN_FIELD_VIRTUAL_P (fns_ptr, oload_champ))
c5aa993b 2929 *valp = value_virtual_fn_field (&temp, fns_ptr, oload_champ, basetype, boffset);
c906108c 2930 else
c5aa993b 2931 *valp = value_fn_field (&temp, fns_ptr, oload_champ, basetype, boffset);
c906108c
SS
2932 }
2933 else
2934 {
2935 *symp = oload_syms[oload_champ];
2936 free (func_name);
2937 }
2938
2939 return oload_incompatible ? 100 : (oload_non_standard ? 10 : 0);
2940}
2941
2942/* C++: return 1 is NAME is a legitimate name for the destructor
2943 of type TYPE. If TYPE does not have a destructor, or
2944 if NAME is inappropriate for TYPE, an error is signaled. */
2945int
2946destructor_name_p (name, type)
2947 const char *name;
2948 const struct type *type;
2949{
2950 /* destructors are a special case. */
2951
2952 if (name[0] == '~')
2953 {
2954 char *dname = type_name_no_tag (type);
2955 char *cp = strchr (dname, '<');
2956 unsigned int len;
2957
2958 /* Do not compare the template part for template classes. */
2959 if (cp == NULL)
2960 len = strlen (dname);
2961 else
2962 len = cp - dname;
2963 if (strlen (name + 1) != len || !STREQN (dname, name + 1, len))
2964 error ("name of destructor must equal name of class");
2965 else
2966 return 1;
2967 }
2968 return 0;
2969}
2970
2971/* Helper function for check_field: Given TYPE, a structure/union,
2972 return 1 if the component named NAME from the ultimate
2973 target structure/union is defined, otherwise, return 0. */
2974
2975static int
2976check_field_in (type, name)
2977 register struct type *type;
2978 const char *name;
2979{
2980 register int i;
2981
2982 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
2983 {
2984 char *t_field_name = TYPE_FIELD_NAME (type, i);
db577aea 2985 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
c906108c
SS
2986 return 1;
2987 }
2988
2989 /* C++: If it was not found as a data field, then try to
2990 return it as a pointer to a method. */
2991
2992 /* Destructors are a special case. */
2993 if (destructor_name_p (name, type))
2994 {
2995 int m_index, f_index;
2996
2997 return get_destructor_fn_field (type, &m_index, &f_index);
2998 }
2999
3000 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
3001 {
db577aea 3002 if (strcmp_iw (TYPE_FN_FIELDLIST_NAME (type, i), name) == 0)
c906108c
SS
3003 return 1;
3004 }
3005
3006 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
3007 if (check_field_in (TYPE_BASECLASS (type, i), name))
3008 return 1;
c5aa993b 3009
c906108c
SS
3010 return 0;
3011}
3012
3013
3014/* C++: Given ARG1, a value of type (pointer to a)* structure/union,
3015 return 1 if the component named NAME from the ultimate
3016 target structure/union is defined, otherwise, return 0. */
3017
3018int
3019check_field (arg1, name)
3020 register value_ptr arg1;
3021 const char *name;
3022{
3023 register struct type *t;
3024
3025 COERCE_ARRAY (arg1);
3026
3027 t = VALUE_TYPE (arg1);
3028
3029 /* Follow pointers until we get to a non-pointer. */
3030
3031 for (;;)
3032 {
3033 CHECK_TYPEDEF (t);
3034 if (TYPE_CODE (t) != TYPE_CODE_PTR && TYPE_CODE (t) != TYPE_CODE_REF)
3035 break;
3036 t = TYPE_TARGET_TYPE (t);
3037 }
3038
3039 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
3040 error ("not implemented: member type in check_field");
3041
c5aa993b 3042 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
c906108c
SS
3043 && TYPE_CODE (t) != TYPE_CODE_UNION)
3044 error ("Internal error: `this' is not an aggregate");
3045
3046 return check_field_in (t, name);
3047}
3048
3049/* C++: Given an aggregate type CURTYPE, and a member name NAME,
3050 return the address of this member as a "pointer to member"
3051 type. If INTYPE is non-null, then it will be the type
3052 of the member we are looking for. This will help us resolve
3053 "pointers to member functions". This function is used
3054 to resolve user expressions of the form "DOMAIN::NAME". */
3055
3056value_ptr
3057value_struct_elt_for_reference (domain, offset, curtype, name, intype)
3058 struct type *domain, *curtype, *intype;
3059 int offset;
3060 char *name;
3061{
3062 register struct type *t = curtype;
3063 register int i;
3064 value_ptr v;
3065
c5aa993b 3066 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
c906108c
SS
3067 && TYPE_CODE (t) != TYPE_CODE_UNION)
3068 error ("Internal error: non-aggregate type to value_struct_elt_for_reference");
3069
3070 for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
3071 {
3072 char *t_field_name = TYPE_FIELD_NAME (t, i);
c5aa993b 3073
c906108c
SS
3074 if (t_field_name && STREQ (t_field_name, name))
3075 {
3076 if (TYPE_FIELD_STATIC (t, i))
3077 {
3078 v = value_static_field (t, i);
3079 if (v == NULL)
3080 error ("Internal error: could not find static variable %s",
3081 name);
3082 return v;
3083 }
3084 if (TYPE_FIELD_PACKED (t, i))
3085 error ("pointers to bitfield members not allowed");
c5aa993b 3086
c906108c
SS
3087 return value_from_longest
3088 (lookup_reference_type (lookup_member_type (TYPE_FIELD_TYPE (t, i),
3089 domain)),
3090 offset + (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3));
3091 }
3092 }
3093
3094 /* C++: If it was not found as a data field, then try to
3095 return it as a pointer to a method. */
3096
3097 /* Destructors are a special case. */
3098 if (destructor_name_p (name, t))
3099 {
3100 error ("member pointers to destructors not implemented yet");
3101 }
3102
3103 /* Perform all necessary dereferencing. */
3104 while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR)
3105 intype = TYPE_TARGET_TYPE (intype);
3106
3107 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
3108 {
3109 char *t_field_name = TYPE_FN_FIELDLIST_NAME (t, i);
3110 char dem_opname[64];
3111
c5aa993b
JM
3112 if (strncmp (t_field_name, "__", 2) == 0 ||
3113 strncmp (t_field_name, "op", 2) == 0 ||
3114 strncmp (t_field_name, "type", 4) == 0)
c906108c 3115 {
c5aa993b
JM
3116 if (cplus_demangle_opname (t_field_name, dem_opname, DMGL_ANSI))
3117 t_field_name = dem_opname;
3118 else if (cplus_demangle_opname (t_field_name, dem_opname, 0))
c906108c 3119 t_field_name = dem_opname;
c906108c
SS
3120 }
3121 if (t_field_name && STREQ (t_field_name, name))
3122 {
3123 int j = TYPE_FN_FIELDLIST_LENGTH (t, i);
3124 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
c5aa993b 3125
c906108c
SS
3126 if (intype == 0 && j > 1)
3127 error ("non-unique member `%s' requires type instantiation", name);
3128 if (intype)
3129 {
3130 while (j--)
3131 if (TYPE_FN_FIELD_TYPE (f, j) == intype)
3132 break;
3133 if (j < 0)
3134 error ("no member function matches that type instantiation");
3135 }
3136 else
3137 j = 0;
c5aa993b 3138
c906108c
SS
3139 if (TYPE_FN_FIELD_STUB (f, j))
3140 check_stub_method (t, i, j);
3141 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
3142 {
3143 return value_from_longest
3144 (lookup_reference_type
3145 (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
3146 domain)),
3147 (LONGEST) METHOD_PTR_FROM_VOFFSET (TYPE_FN_FIELD_VOFFSET (f, j)));
3148 }
3149 else
3150 {
3151 struct symbol *s = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
3152 0, VAR_NAMESPACE, 0, NULL);
3153 if (s == NULL)
3154 {
3155 v = 0;
3156 }
3157 else
3158 {
3159 v = read_var_value (s, 0);
3160#if 0
3161 VALUE_TYPE (v) = lookup_reference_type
3162 (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
3163 domain));
3164#endif
3165 }
3166 return v;
3167 }
3168 }
3169 }
3170 for (i = TYPE_N_BASECLASSES (t) - 1; i >= 0; i--)
3171 {
3172 value_ptr v;
3173 int base_offset;
3174
3175 if (BASETYPE_VIA_VIRTUAL (t, i))
3176 base_offset = 0;
3177 else
3178 base_offset = TYPE_BASECLASS_BITPOS (t, i) / 8;
3179 v = value_struct_elt_for_reference (domain,
3180 offset + base_offset,
3181 TYPE_BASECLASS (t, i),
3182 name,
3183 intype);
3184 if (v)
3185 return v;
3186 }
3187 return 0;
3188}
3189
3190
3191/* Find the real run-time type of a value using RTTI.
3192 * V is a pointer to the value.
3193 * A pointer to the struct type entry of the run-time type
3194 * is returneed.
3195 * FULL is a flag that is set only if the value V includes
3196 * the entire contents of an object of the RTTI type.
3197 * TOP is the offset to the top of the enclosing object of
3198 * the real run-time type. This offset may be for the embedded
3199 * object, or for the enclosing object of V.
3200 * USING_ENC is the flag that distinguishes the two cases.
3201 * If it is 1, then the offset is for the enclosing object,
3202 * otherwise for the embedded object.
070ad9f0
DB
3203 *
3204 */
c906108c
SS
3205
3206struct type *
3207value_rtti_type (v, full, top, using_enc)
c5aa993b
JM
3208 value_ptr v;
3209 int *full;
3210 int *top;
3211 int *using_enc;
c906108c 3212{
c5aa993b
JM
3213 struct type *known_type;
3214 struct type *rtti_type;
c906108c
SS
3215 CORE_ADDR coreptr;
3216 value_ptr vp;
3217 int using_enclosing = 0;
3218 long top_offset = 0;
3219 char rtti_type_name[256];
3220
3221 if (full)
3222 *full = 0;
3223 if (top)
3224 *top = -1;
3225 if (using_enc)
3226 *using_enc = 0;
3227
c5aa993b 3228 /* Get declared type */
c906108c
SS
3229 known_type = VALUE_TYPE (v);
3230 CHECK_TYPEDEF (known_type);
c5aa993b 3231 /* RTTI works only or class objects */
c906108c
SS
3232 if (TYPE_CODE (known_type) != TYPE_CODE_CLASS)
3233 return NULL;
070ad9f0
DB
3234 if (TYPE_HAS_VTABLE(known_type))
3235 {
3236 /* If neither the declared type nor the enclosing type of the
3237 * value structure has a HP ANSI C++ style virtual table,
3238 * we can't do anything. */
3239 if (!TYPE_HAS_VTABLE (known_type))
3240 {
3241 known_type = VALUE_ENCLOSING_TYPE (v);
3242 CHECK_TYPEDEF (known_type);
3243 if ((TYPE_CODE (known_type) != TYPE_CODE_CLASS) ||
3244 !TYPE_HAS_VTABLE (known_type))
3245 return NULL; /* No RTTI, or not HP-compiled types */
3246 CHECK_TYPEDEF (known_type);
3247 using_enclosing = 1;
3248 }
c906108c 3249
070ad9f0
DB
3250 if (using_enclosing && using_enc)
3251 *using_enc = 1;
3252
3253 /* First get the virtual table address */
3254 coreptr = *(CORE_ADDR *) ((VALUE_CONTENTS_ALL (v))
3255 + VALUE_OFFSET (v)
3256 + (using_enclosing ? 0 : VALUE_EMBEDDED_OFFSET (v)));
3257 if (coreptr == 0)
3258 return NULL; /* return silently -- maybe called on gdb-generated value */
3259
3260 /* Fetch the top offset of the object */
3261 /* FIXME possible 32x64 problem with pointer size & arithmetic */
3262 vp = value_at (builtin_type_int,
3263 coreptr + 4 * HP_ACC_TOP_OFFSET_OFFSET,
3264 VALUE_BFD_SECTION (v));
3265 top_offset = value_as_long (vp);
3266 if (top)
3267 *top = top_offset;
3268
3269 /* Fetch the typeinfo pointer */
3270 /* FIXME possible 32x64 problem with pointer size & arithmetic */
3271 vp = value_at (builtin_type_int, coreptr + 4 * HP_ACC_TYPEINFO_OFFSET, VALUE_BFD_SECTION (v));
3272 /* Indirect through the typeinfo pointer and retrieve the pointer
3273 * to the string name */
3274 coreptr = *(CORE_ADDR *) (VALUE_CONTENTS (vp));
3275 if (!coreptr)
3276 error ("Retrieved null typeinfo pointer in trying to determine run-time type");
3277 vp = value_at (builtin_type_int, coreptr + 4, VALUE_BFD_SECTION (v)); /* 4 -> offset of name field */
3278 /* FIXME possible 32x64 problem */
3279
3280 coreptr = *(CORE_ADDR *) (VALUE_CONTENTS (vp));
3281
3282 read_memory_string (coreptr, rtti_type_name, 256);
3283
3284 if (strlen (rtti_type_name) == 0)
3285 error ("Retrieved null type name from typeinfo");
3286
3287 /* search for type */
3288 rtti_type = lookup_typename (rtti_type_name, (struct block *) 0, 1);
3289
3290 if (!rtti_type)
3291 error ("Could not find run-time type: invalid type name %s in typeinfo??", rtti_type_name);
3292 CHECK_TYPEDEF (rtti_type);
3293#if 0
3294 printf ("RTTI type name %s, tag %s, full? %d\n", TYPE_NAME (rtti_type), TYPE_TAG_NAME (rtti_type), full ? *full : -1);
3295#endif
3296 /* Check whether we have the entire object */
3297 if (full /* Non-null pointer passed */
3298 &&
3299 /* Either we checked on the whole object in hand and found the
3300 top offset to be zero */
3301 (((top_offset == 0) &&
3302 using_enclosing &&
3303 TYPE_LENGTH (known_type) == TYPE_LENGTH (rtti_type))
3304 ||
3305 /* Or we checked on the embedded object and top offset was the
3306 same as the embedded offset */
3307 ((top_offset == VALUE_EMBEDDED_OFFSET (v)) &&
3308 !using_enclosing &&
3309 TYPE_LENGTH (VALUE_ENCLOSING_TYPE (v)) == TYPE_LENGTH (rtti_type))))
3310
3311 *full = 1;
3312 }
3313 else
3314 /*
3315 Right now this is G++ RTTI. Plan on this changing in the
3316 future as i get around to setting the vtables properly for G++
3317 compiled stuff. Also, i'll be using the type info functions,
3318 which are always right. Deal with it until then.
3319 */
3320 {
3321 CORE_ADDR vtbl;
3322 struct minimal_symbol *minsym;
3323 struct symbol *sym;
3324 char *demangled_name;
3325 struct type *btype;
3326 /* If the type has no vptr fieldno, try to get it filled in */
3327 if (TYPE_VPTR_FIELDNO(known_type) < 0)
3328 fill_in_vptr_fieldno(known_type);
3329
3330 /* If we still can't find one, give up */
3331 if (TYPE_VPTR_FIELDNO(known_type) < 0)
3332 return NULL;
c5aa993b 3333
070ad9f0
DB
3334 /* Make sure our basetype and known type match, otherwise, cast
3335 so we can get at the vtable properly.
3336 */
3337 btype = TYPE_VPTR_BASETYPE (known_type);
3338 CHECK_TYPEDEF (btype);
3339 if (btype != known_type )
3340 {
3341 v = value_cast (btype, v);
3342 if (using_enc)
3343 *using_enc=1;
3344 }
3345 /*
3346 We can't use value_ind here, because it would want to use RTTI, and
3347 we'd waste a bunch of time figuring out we already know the type.
3348 Besides, we don't care about the type, just the actual pointer
3349 */
85c9a9d5 3350 if (VALUE_ADDRESS (value_field (v, TYPE_VPTR_FIELDNO (known_type))) == 0)
070ad9f0 3351 return NULL;
c5aa993b 3352
070ad9f0
DB
3353 /*
3354 If we are enclosed by something that isn't us, adjust the
3355 address properly and set using_enclosing.
3356 */
3357 if (VALUE_ENCLOSING_TYPE(v) != VALUE_TYPE(v))
3358 {
3359 value_ptr tempval;
3360 tempval=value_field(v,TYPE_VPTR_FIELDNO(known_type));
3361 VALUE_ADDRESS(tempval)+=(TYPE_BASECLASS_BITPOS(known_type,TYPE_VPTR_FIELDNO(known_type))/8);
3362 vtbl=value_as_pointer(tempval);
3363 using_enclosing=1;
3364 }
3365 else
3366 {
3367 vtbl=value_as_pointer(value_field(v,TYPE_VPTR_FIELDNO(known_type)));
3368 using_enclosing=0;
3369 }
c906108c 3370
070ad9f0
DB
3371 /* Try to find a symbol that is the vtable */
3372 minsym=lookup_minimal_symbol_by_pc(vtbl);
3373 if (minsym==NULL || (demangled_name=SYMBOL_NAME(minsym))==NULL || !VTBL_PREFIX_P(demangled_name))
3374 return NULL;
c906108c 3375
070ad9f0
DB
3376 /* If we just skip the prefix, we get screwed by namespaces */
3377 demangled_name=cplus_demangle(demangled_name,DMGL_PARAMS|DMGL_ANSI);
3378 *(strchr(demangled_name,' '))=0;
c906108c 3379
070ad9f0
DB
3380 /* Lookup the type for the name */
3381 rtti_type=lookup_typename(demangled_name, (struct block *)0,1);
c5aa993b 3382
070ad9f0
DB
3383 if (rtti_type==NULL)
3384 return NULL;
c5aa993b 3385
070ad9f0
DB
3386 if (TYPE_N_BASECLASSES(rtti_type) > 1 && full && (*full) != 1)
3387 {
3388 if (top)
3389 *top=TYPE_BASECLASS_BITPOS(rtti_type,TYPE_VPTR_FIELDNO(rtti_type))/8;
3390 if (top && ((*top) >0))
3391 {
3392 if (TYPE_LENGTH(rtti_type) > TYPE_LENGTH(known_type))
3393 {
3394 if (full)
3395 *full=0;
3396 }
3397 else
3398 {
3399 if (full)
3400 *full=1;
3401 }
3402 }
3403 }
3404 else
3405 {
3406 if (full)
3407 *full=1;
3408 }
3409 if (using_enc)
3410 *using_enc=using_enclosing;
3411 }
c906108c
SS
3412 return rtti_type;
3413}
3414
3415/* Given a pointer value V, find the real (RTTI) type
3416 of the object it points to.
3417 Other parameters FULL, TOP, USING_ENC as with value_rtti_type()
3418 and refer to the values computed for the object pointed to. */
3419
3420struct type *
3421value_rtti_target_type (v, full, top, using_enc)
c5aa993b
JM
3422 value_ptr v;
3423 int *full;
3424 int *top;
3425 int *using_enc;
c906108c
SS
3426{
3427 value_ptr target;
3428
3429 target = value_ind (v);
3430
3431 return value_rtti_type (target, full, top, using_enc);
3432}
3433
3434/* Given a value pointed to by ARGP, check its real run-time type, and
3435 if that is different from the enclosing type, create a new value
3436 using the real run-time type as the enclosing type (and of the same
3437 type as ARGP) and return it, with the embedded offset adjusted to
3438 be the correct offset to the enclosed object
3439 RTYPE is the type, and XFULL, XTOP, and XUSING_ENC are the other
3440 parameters, computed by value_rtti_type(). If these are available,
3441 they can be supplied and a second call to value_rtti_type() is avoided.
3442 (Pass RTYPE == NULL if they're not available */
3443
3444value_ptr
3445value_full_object (argp, rtype, xfull, xtop, xusing_enc)
c5aa993b
JM
3446 value_ptr argp;
3447 struct type *rtype;
3448 int xfull;
3449 int xtop;
3450 int xusing_enc;
3451
c906108c 3452{
c5aa993b 3453 struct type *real_type;
c906108c
SS
3454 int full = 0;
3455 int top = -1;
3456 int using_enc = 0;
3457 value_ptr new_val;
3458
3459 if (rtype)
3460 {
3461 real_type = rtype;
3462 full = xfull;
3463 top = xtop;
3464 using_enc = xusing_enc;
3465 }
3466 else
3467 real_type = value_rtti_type (argp, &full, &top, &using_enc);
3468
3469 /* If no RTTI data, or if object is already complete, do nothing */
3470 if (!real_type || real_type == VALUE_ENCLOSING_TYPE (argp))
3471 return argp;
3472
3473 /* If we have the full object, but for some reason the enclosing
c5aa993b 3474 type is wrong, set it *//* pai: FIXME -- sounds iffy */
c906108c
SS
3475 if (full)
3476 {
3477 VALUE_ENCLOSING_TYPE (argp) = real_type;
3478 return argp;
3479 }
3480
3481 /* Check if object is in memory */
3482 if (VALUE_LVAL (argp) != lval_memory)
3483 {
3484 warning ("Couldn't retrieve complete object of RTTI type %s; object may be in register(s).", TYPE_NAME (real_type));
c5aa993b 3485
c906108c
SS
3486 return argp;
3487 }
c5aa993b 3488
c906108c
SS
3489 /* All other cases -- retrieve the complete object */
3490 /* Go back by the computed top_offset from the beginning of the object,
3491 adjusting for the embedded offset of argp if that's what value_rtti_type
3492 used for its computation. */
3493 new_val = value_at_lazy (real_type, VALUE_ADDRESS (argp) - top +
c5aa993b
JM
3494 (using_enc ? 0 : VALUE_EMBEDDED_OFFSET (argp)),
3495 VALUE_BFD_SECTION (argp));
c906108c
SS
3496 VALUE_TYPE (new_val) = VALUE_TYPE (argp);
3497 VALUE_EMBEDDED_OFFSET (new_val) = using_enc ? top + VALUE_EMBEDDED_OFFSET (argp) : top;
3498 return new_val;
3499}
3500
3501
3502
3503
3504/* C++: return the value of the class instance variable, if one exists.
3505 Flag COMPLAIN signals an error if the request is made in an
3506 inappropriate context. */
3507
3508value_ptr
3509value_of_this (complain)
3510 int complain;
3511{
3512 struct symbol *func, *sym;
3513 struct block *b;
3514 int i;
3515 static const char funny_this[] = "this";
3516 value_ptr this;
3517
3518 if (selected_frame == 0)
3519 {
3520 if (complain)
c5aa993b
JM
3521 error ("no frame selected");
3522 else
3523 return 0;
c906108c
SS
3524 }
3525
3526 func = get_frame_function (selected_frame);
3527 if (!func)
3528 {
3529 if (complain)
3530 error ("no `this' in nameless context");
c5aa993b
JM
3531 else
3532 return 0;
c906108c
SS
3533 }
3534
3535 b = SYMBOL_BLOCK_VALUE (func);
3536 i = BLOCK_NSYMS (b);
3537 if (i <= 0)
3538 {
3539 if (complain)
c5aa993b
JM
3540 error ("no args, no `this'");
3541 else
3542 return 0;
c906108c
SS
3543 }
3544
3545 /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
3546 symbol instead of the LOC_ARG one (if both exist). */
3547 sym = lookup_block_symbol (b, funny_this, VAR_NAMESPACE);
3548 if (sym == NULL)
3549 {
3550 if (complain)
3551 error ("current stack frame not in method");
3552 else
3553 return NULL;
3554 }
3555
3556 this = read_var_value (sym, selected_frame);
3557 if (this == 0 && complain)
3558 error ("`this' argument at unknown address");
3559 return this;
3560}
3561
3562/* Create a slice (sub-string, sub-array) of ARRAY, that is LENGTH elements
3563 long, starting at LOWBOUND. The result has the same lower bound as
3564 the original ARRAY. */
3565
3566value_ptr
3567value_slice (array, lowbound, length)
3568 value_ptr array;
3569 int lowbound, length;
3570{
3571 struct type *slice_range_type, *slice_type, *range_type;
3572 LONGEST lowerbound, upperbound, offset;
3573 value_ptr slice;
3574 struct type *array_type;
3575 array_type = check_typedef (VALUE_TYPE (array));
3576 COERCE_VARYING_ARRAY (array, array_type);
3577 if (TYPE_CODE (array_type) != TYPE_CODE_ARRAY
3578 && TYPE_CODE (array_type) != TYPE_CODE_STRING
3579 && TYPE_CODE (array_type) != TYPE_CODE_BITSTRING)
3580 error ("cannot take slice of non-array");
3581 range_type = TYPE_INDEX_TYPE (array_type);
3582 if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0)
3583 error ("slice from bad array or bitstring");
3584 if (lowbound < lowerbound || length < 0
3585 || lowbound + length - 1 > upperbound
c5aa993b 3586 /* Chill allows zero-length strings but not arrays. */
c906108c
SS
3587 || (current_language->la_language == language_chill
3588 && length == 0 && TYPE_CODE (array_type) == TYPE_CODE_ARRAY))
3589 error ("slice out of range");
3590 /* FIXME-type-allocation: need a way to free this type when we are
3591 done with it. */
c5aa993b 3592 slice_range_type = create_range_type ((struct type *) NULL,
c906108c
SS
3593 TYPE_TARGET_TYPE (range_type),
3594 lowbound, lowbound + length - 1);
3595 if (TYPE_CODE (array_type) == TYPE_CODE_BITSTRING)
3596 {
3597 int i;
c5aa993b 3598 slice_type = create_set_type ((struct type *) NULL, slice_range_type);
c906108c
SS
3599 TYPE_CODE (slice_type) = TYPE_CODE_BITSTRING;
3600 slice = value_zero (slice_type, not_lval);
3601 for (i = 0; i < length; i++)
3602 {
3603 int element = value_bit_index (array_type,
3604 VALUE_CONTENTS (array),
3605 lowbound + i);
3606 if (element < 0)
3607 error ("internal error accessing bitstring");
3608 else if (element > 0)
3609 {
3610 int j = i % TARGET_CHAR_BIT;
3611 if (BITS_BIG_ENDIAN)
3612 j = TARGET_CHAR_BIT - 1 - j;
3613 VALUE_CONTENTS_RAW (slice)[i / TARGET_CHAR_BIT] |= (1 << j);
3614 }
3615 }
3616 /* We should set the address, bitssize, and bitspos, so the clice
7b83ea04
AC
3617 can be used on the LHS, but that may require extensions to
3618 value_assign. For now, just leave as a non_lval. FIXME. */
c906108c
SS
3619 }
3620 else
3621 {
3622 struct type *element_type = TYPE_TARGET_TYPE (array_type);
3623 offset
3624 = (lowbound - lowerbound) * TYPE_LENGTH (check_typedef (element_type));
c5aa993b 3625 slice_type = create_array_type ((struct type *) NULL, element_type,
c906108c
SS
3626 slice_range_type);
3627 TYPE_CODE (slice_type) = TYPE_CODE (array_type);
3628 slice = allocate_value (slice_type);
3629 if (VALUE_LAZY (array))
3630 VALUE_LAZY (slice) = 1;
3631 else
3632 memcpy (VALUE_CONTENTS (slice), VALUE_CONTENTS (array) + offset,
3633 TYPE_LENGTH (slice_type));
3634 if (VALUE_LVAL (array) == lval_internalvar)
3635 VALUE_LVAL (slice) = lval_internalvar_component;
3636 else
3637 VALUE_LVAL (slice) = VALUE_LVAL (array);
3638 VALUE_ADDRESS (slice) = VALUE_ADDRESS (array);
3639 VALUE_OFFSET (slice) = VALUE_OFFSET (array) + offset;
3640 }
3641 return slice;
3642}
3643
3644/* Assuming chill_varying_type (VARRAY) is true, return an equivalent
3645 value as a fixed-length array. */
3646
3647value_ptr
3648varying_to_slice (varray)
3649 value_ptr varray;
3650{
3651 struct type *vtype = check_typedef (VALUE_TYPE (varray));
3652 LONGEST length = unpack_long (TYPE_FIELD_TYPE (vtype, 0),
3653 VALUE_CONTENTS (varray)
3654 + TYPE_FIELD_BITPOS (vtype, 0) / 8);
3655 return value_slice (value_primitive_field (varray, 0, 1, vtype), 0, length);
3656}
3657
070ad9f0
DB
3658/* Create a value for a FORTRAN complex number. Currently most of
3659 the time values are coerced to COMPLEX*16 (i.e. a complex number
3660 composed of 2 doubles. This really should be a smarter routine
3661 that figures out precision inteligently as opposed to assuming
c5aa993b 3662 doubles. FIXME: fmb */
c906108c
SS
3663
3664value_ptr
3665value_literal_complex (arg1, arg2, type)
3666 value_ptr arg1;
3667 value_ptr arg2;
3668 struct type *type;
3669{
3670 register value_ptr val;
3671 struct type *real_type = TYPE_TARGET_TYPE (type);
3672
3673 val = allocate_value (type);
3674 arg1 = value_cast (real_type, arg1);
3675 arg2 = value_cast (real_type, arg2);
3676
3677 memcpy (VALUE_CONTENTS_RAW (val),
3678 VALUE_CONTENTS (arg1), TYPE_LENGTH (real_type));
3679 memcpy (VALUE_CONTENTS_RAW (val) + TYPE_LENGTH (real_type),
3680 VALUE_CONTENTS (arg2), TYPE_LENGTH (real_type));
3681 return val;
3682}
3683
3684/* Cast a value into the appropriate complex data type. */
3685
3686static value_ptr
3687cast_into_complex (type, val)
3688 struct type *type;
3689 register value_ptr val;
3690{
3691 struct type *real_type = TYPE_TARGET_TYPE (type);
3692 if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_COMPLEX)
3693 {
3694 struct type *val_real_type = TYPE_TARGET_TYPE (VALUE_TYPE (val));
3695 value_ptr re_val = allocate_value (val_real_type);
3696 value_ptr im_val = allocate_value (val_real_type);
3697
3698 memcpy (VALUE_CONTENTS_RAW (re_val),
3699 VALUE_CONTENTS (val), TYPE_LENGTH (val_real_type));
3700 memcpy (VALUE_CONTENTS_RAW (im_val),
3701 VALUE_CONTENTS (val) + TYPE_LENGTH (val_real_type),
c5aa993b 3702 TYPE_LENGTH (val_real_type));
c906108c
SS
3703
3704 return value_literal_complex (re_val, im_val, type);
3705 }
3706 else if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FLT
3707 || TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_INT)
3708 return value_literal_complex (val, value_zero (real_type, not_lval), type);
3709 else
3710 error ("cannot cast non-number to complex");
3711}
3712
3713void
3714_initialize_valops ()
3715{
3716#if 0
3717 add_show_from_set
c5aa993b 3718 (add_set_cmd ("abandon", class_support, var_boolean, (char *) &auto_abandon,
c906108c
SS
3719 "Set automatic abandonment of expressions upon failure.",
3720 &setlist),
3721 &showlist);
3722#endif
3723
3724 add_show_from_set
c5aa993b 3725 (add_set_cmd ("overload-resolution", class_support, var_boolean, (char *) &overload_resolution,
c906108c
SS
3726 "Set overload resolution in evaluating C++ functions.",
3727 &setlist),
3728 &showlist);
3729 overload_resolution = 1;
3730
242bfc55
FN
3731 add_show_from_set (
3732 add_set_cmd ("unwindonsignal", no_class, var_boolean,
3733 (char *) &unwind_on_signal_p,
3734"Set unwinding of stack if a signal is received while in a call dummy.\n\
3735The unwindonsignal lets the user determine what gdb should do if a signal\n\
3736is received while in a function called from gdb (call dummy). If set, gdb\n\
3737unwinds the stack and restore the context to what as it was before the call.\n\
3738The default is to stop in the frame where the signal was received.", &setlist),
3739 &showlist);
c906108c 3740}
This page took 0.457744 seconds and 4 git commands to generate.