* dwarf2-frame.c (execute_cfa_program): Move DWA_CFA_nop before
[deliverable/binutils-gdb.git] / gdb / valops.c
CommitLineData
c906108c 1/* Perform non-arithmetic operations on values, for GDB.
f23631e4 2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
63d06c5c 3 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
f23631e4 4 Free Software Foundation, Inc.
c906108c 5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
c906108c 12
c5aa993b
JM
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
c906108c 17
c5aa993b
JM
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
c906108c
SS
22
23#include "defs.h"
24#include "symtab.h"
25#include "gdbtypes.h"
26#include "value.h"
27#include "frame.h"
28#include "inferior.h"
29#include "gdbcore.h"
30#include "target.h"
31#include "demangle.h"
32#include "language.h"
33#include "gdbcmd.h"
4e052eda 34#include "regcache.h"
015a42b4 35#include "cp-abi.h"
fe898f56 36#include "block.h"
04714b91 37#include "infcall.h"
de4f826b 38#include "dictionary.h"
b6429628 39#include "cp-support.h"
c906108c
SS
40
41#include <errno.h>
42#include "gdb_string.h"
4a1970e4 43#include "gdb_assert.h"
79c2c32d 44#include "cp-support.h"
c906108c 45
c906108c
SS
46/* Flag indicating HP compilers were used; needed to correctly handle some
47 value operations with HP aCC code/runtime. */
48extern int hp_som_som_object_present;
49
070ad9f0 50extern int overload_debug;
c906108c
SS
51/* Local functions. */
52
ad2f7632
DJ
53static int typecmp (int staticp, int varargs, int nargs,
54 struct field t1[], struct value *t2[]);
c906108c 55
f23631e4 56static CORE_ADDR value_push (CORE_ADDR, struct value *);
c906108c 57
f23631e4 58static struct value *search_struct_field (char *, struct value *, int,
a14ed312 59 struct type *, int);
c906108c 60
f23631e4
AC
61static struct value *search_struct_method (char *, struct value **,
62 struct value **,
a14ed312 63 int, int *, struct type *);
c906108c 64
a14ed312 65static int check_field_in (struct type *, const char *);
c906108c 66
79c2c32d
DC
67static struct value *value_struct_elt_for_reference (struct type *domain,
68 int offset,
69 struct type *curtype,
70 char *name,
63d06c5c
DC
71 struct type *intype,
72 enum noside noside);
79c2c32d
DC
73
74static struct value *value_namespace_elt (const struct type *curtype,
63d06c5c 75 char *name,
79c2c32d
DC
76 enum noside noside);
77
63d06c5c
DC
78static struct value *value_maybe_namespace_elt (const struct type *curtype,
79 char *name,
80 enum noside noside);
81
a14ed312 82static CORE_ADDR allocate_space_in_inferior (int);
c906108c 83
f23631e4 84static struct value *cast_into_complex (struct type *, struct value *);
c906108c 85
f23631e4 86static struct fn_field *find_method_list (struct value ** argp, char *method,
4a1970e4 87 int offset,
a14ed312
KB
88 struct type *type, int *num_fns,
89 struct type **basetype,
90 int *boffset);
7a292a7a 91
a14ed312 92void _initialize_valops (void);
c906108c 93
c906108c
SS
94/* Flag for whether we want to abandon failed expression evals by default. */
95
96#if 0
97static int auto_abandon = 0;
98#endif
99
100int overload_resolution = 0;
242bfc55 101
c906108c
SS
102/* Find the address of function name NAME in the inferior. */
103
f23631e4 104struct value *
3bada2a2 105find_function_in_inferior (const char *name)
c906108c 106{
52f0bd74 107 struct symbol *sym;
176620f1 108 sym = lookup_symbol (name, 0, VAR_DOMAIN, 0, NULL);
c906108c
SS
109 if (sym != NULL)
110 {
111 if (SYMBOL_CLASS (sym) != LOC_BLOCK)
112 {
113 error ("\"%s\" exists in this program but is not a function.",
114 name);
115 }
116 return value_of_variable (sym, NULL);
117 }
118 else
119 {
c5aa993b 120 struct minimal_symbol *msymbol = lookup_minimal_symbol (name, NULL, NULL);
c906108c
SS
121 if (msymbol != NULL)
122 {
123 struct type *type;
4478b372 124 CORE_ADDR maddr;
c906108c
SS
125 type = lookup_pointer_type (builtin_type_char);
126 type = lookup_function_type (type);
127 type = lookup_pointer_type (type);
4478b372
JB
128 maddr = SYMBOL_VALUE_ADDRESS (msymbol);
129 return value_from_pointer (type, maddr);
c906108c
SS
130 }
131 else
132 {
c5aa993b 133 if (!target_has_execution)
c906108c 134 error ("evaluation of this expression requires the target program to be active");
c5aa993b 135 else
c906108c
SS
136 error ("evaluation of this expression requires the program to have a function \"%s\".", name);
137 }
138 }
139}
140
141/* Allocate NBYTES of space in the inferior using the inferior's malloc
142 and return a value that is a pointer to the allocated space. */
143
f23631e4 144struct value *
fba45db2 145value_allocate_space_in_inferior (int len)
c906108c 146{
f23631e4 147 struct value *blocklen;
5720643c 148 struct value *val = find_function_in_inferior (NAME_OF_MALLOC);
c906108c
SS
149
150 blocklen = value_from_longest (builtin_type_int, (LONGEST) len);
151 val = call_function_by_hand (val, 1, &blocklen);
152 if (value_logical_not (val))
153 {
154 if (!target_has_execution)
c5aa993b
JM
155 error ("No memory available to program now: you need to start the target first");
156 else
157 error ("No memory available to program: call to malloc failed");
c906108c
SS
158 }
159 return val;
160}
161
162static CORE_ADDR
fba45db2 163allocate_space_in_inferior (int len)
c906108c
SS
164{
165 return value_as_long (value_allocate_space_in_inferior (len));
166}
167
168/* Cast value ARG2 to type TYPE and return as a value.
169 More general than a C cast: accepts any two types of the same length,
170 and if ARG2 is an lvalue it can be cast into anything at all. */
171/* In C++, casts may change pointer or object representations. */
172
f23631e4
AC
173struct value *
174value_cast (struct type *type, struct value *arg2)
c906108c 175{
52f0bd74
AC
176 enum type_code code1;
177 enum type_code code2;
178 int scalar;
c906108c
SS
179 struct type *type2;
180
181 int convert_to_boolean = 0;
c5aa993b 182
c906108c
SS
183 if (VALUE_TYPE (arg2) == type)
184 return arg2;
185
186 CHECK_TYPEDEF (type);
187 code1 = TYPE_CODE (type);
c5aa993b 188 COERCE_REF (arg2);
c906108c
SS
189 type2 = check_typedef (VALUE_TYPE (arg2));
190
191 /* A cast to an undetermined-length array_type, such as (TYPE [])OBJECT,
192 is treated like a cast to (TYPE [N])OBJECT,
193 where N is sizeof(OBJECT)/sizeof(TYPE). */
194 if (code1 == TYPE_CODE_ARRAY)
195 {
196 struct type *element_type = TYPE_TARGET_TYPE (type);
197 unsigned element_length = TYPE_LENGTH (check_typedef (element_type));
198 if (element_length > 0
c5aa993b 199 && TYPE_ARRAY_UPPER_BOUND_TYPE (type) == BOUND_CANNOT_BE_DETERMINED)
c906108c
SS
200 {
201 struct type *range_type = TYPE_INDEX_TYPE (type);
202 int val_length = TYPE_LENGTH (type2);
203 LONGEST low_bound, high_bound, new_length;
204 if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
205 low_bound = 0, high_bound = 0;
206 new_length = val_length / element_length;
207 if (val_length % element_length != 0)
c5aa993b 208 warning ("array element type size does not divide object size in cast");
c906108c
SS
209 /* FIXME-type-allocation: need a way to free this type when we are
210 done with it. */
211 range_type = create_range_type ((struct type *) NULL,
212 TYPE_TARGET_TYPE (range_type),
213 low_bound,
214 new_length + low_bound - 1);
215 VALUE_TYPE (arg2) = create_array_type ((struct type *) NULL,
216 element_type, range_type);
217 return arg2;
218 }
219 }
220
221 if (current_language->c_style_arrays
222 && TYPE_CODE (type2) == TYPE_CODE_ARRAY)
223 arg2 = value_coerce_array (arg2);
224
225 if (TYPE_CODE (type2) == TYPE_CODE_FUNC)
226 arg2 = value_coerce_function (arg2);
227
228 type2 = check_typedef (VALUE_TYPE (arg2));
229 COERCE_VARYING_ARRAY (arg2, type2);
230 code2 = TYPE_CODE (type2);
231
232 if (code1 == TYPE_CODE_COMPLEX)
233 return cast_into_complex (type, arg2);
234 if (code1 == TYPE_CODE_BOOL)
235 {
236 code1 = TYPE_CODE_INT;
237 convert_to_boolean = 1;
238 }
239 if (code1 == TYPE_CODE_CHAR)
240 code1 = TYPE_CODE_INT;
241 if (code2 == TYPE_CODE_BOOL || code2 == TYPE_CODE_CHAR)
242 code2 = TYPE_CODE_INT;
243
244 scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
245 || code2 == TYPE_CODE_ENUM || code2 == TYPE_CODE_RANGE);
246
c5aa993b 247 if (code1 == TYPE_CODE_STRUCT
c906108c
SS
248 && code2 == TYPE_CODE_STRUCT
249 && TYPE_NAME (type) != 0)
250 {
251 /* Look in the type of the source to see if it contains the
7b83ea04
AC
252 type of the target as a superclass. If so, we'll need to
253 offset the object in addition to changing its type. */
f23631e4 254 struct value *v = search_struct_field (type_name_no_tag (type),
c906108c
SS
255 arg2, 0, type2, 1);
256 if (v)
257 {
258 VALUE_TYPE (v) = type;
259 return v;
260 }
261 }
262 if (code1 == TYPE_CODE_FLT && scalar)
263 return value_from_double (type, value_as_double (arg2));
264 else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM
265 || code1 == TYPE_CODE_RANGE)
266 && (scalar || code2 == TYPE_CODE_PTR))
267 {
268 LONGEST longest;
c5aa993b
JM
269
270 if (hp_som_som_object_present && /* if target compiled by HP aCC */
271 (code2 == TYPE_CODE_PTR))
272 {
273 unsigned int *ptr;
f23631e4 274 struct value *retvalp;
c5aa993b
JM
275
276 switch (TYPE_CODE (TYPE_TARGET_TYPE (type2)))
277 {
278 /* With HP aCC, pointers to data members have a bias */
279 case TYPE_CODE_MEMBER:
280 retvalp = value_from_longest (type, value_as_long (arg2));
716c501e 281 /* force evaluation */
802db21b 282 ptr = (unsigned int *) VALUE_CONTENTS (retvalp);
c5aa993b
JM
283 *ptr &= ~0x20000000; /* zap 29th bit to remove bias */
284 return retvalp;
285
286 /* While pointers to methods don't really point to a function */
287 case TYPE_CODE_METHOD:
288 error ("Pointers to methods not supported with HP aCC");
289
290 default:
291 break; /* fall out and go to normal handling */
292 }
293 }
2bf1f4a1
JB
294
295 /* When we cast pointers to integers, we mustn't use
296 POINTER_TO_ADDRESS to find the address the pointer
297 represents, as value_as_long would. GDB should evaluate
298 expressions just as the compiler would --- and the compiler
299 sees a cast as a simple reinterpretation of the pointer's
300 bits. */
301 if (code2 == TYPE_CODE_PTR)
302 longest = extract_unsigned_integer (VALUE_CONTENTS (arg2),
303 TYPE_LENGTH (type2));
304 else
305 longest = value_as_long (arg2);
802db21b 306 return value_from_longest (type, convert_to_boolean ?
716c501e 307 (LONGEST) (longest ? 1 : 0) : longest);
c906108c 308 }
802db21b 309 else if (code1 == TYPE_CODE_PTR && (code2 == TYPE_CODE_INT ||
23e04971
MS
310 code2 == TYPE_CODE_ENUM ||
311 code2 == TYPE_CODE_RANGE))
634acd5f 312 {
4603e466
DT
313 /* TYPE_LENGTH (type) is the length of a pointer, but we really
314 want the length of an address! -- we are really dealing with
315 addresses (i.e., gdb representations) not pointers (i.e.,
316 target representations) here.
317
318 This allows things like "print *(int *)0x01000234" to work
319 without printing a misleading message -- which would
320 otherwise occur when dealing with a target having two byte
321 pointers and four byte addresses. */
322
323 int addr_bit = TARGET_ADDR_BIT;
324
634acd5f 325 LONGEST longest = value_as_long (arg2);
4603e466 326 if (addr_bit < sizeof (LONGEST) * HOST_CHAR_BIT)
634acd5f 327 {
4603e466
DT
328 if (longest >= ((LONGEST) 1 << addr_bit)
329 || longest <= -((LONGEST) 1 << addr_bit))
634acd5f
AC
330 warning ("value truncated");
331 }
332 return value_from_longest (type, longest);
333 }
c906108c
SS
334 else if (TYPE_LENGTH (type) == TYPE_LENGTH (type2))
335 {
336 if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
337 {
338 struct type *t1 = check_typedef (TYPE_TARGET_TYPE (type));
339 struct type *t2 = check_typedef (TYPE_TARGET_TYPE (type2));
c5aa993b 340 if (TYPE_CODE (t1) == TYPE_CODE_STRUCT
c906108c
SS
341 && TYPE_CODE (t2) == TYPE_CODE_STRUCT
342 && !value_logical_not (arg2))
343 {
f23631e4 344 struct value *v;
c906108c
SS
345
346 /* Look in the type of the source to see if it contains the
7b83ea04
AC
347 type of the target as a superclass. If so, we'll need to
348 offset the pointer rather than just change its type. */
c906108c
SS
349 if (TYPE_NAME (t1) != NULL)
350 {
351 v = search_struct_field (type_name_no_tag (t1),
352 value_ind (arg2), 0, t2, 1);
353 if (v)
354 {
355 v = value_addr (v);
356 VALUE_TYPE (v) = type;
357 return v;
358 }
359 }
360
361 /* Look in the type of the target to see if it contains the
7b83ea04
AC
362 type of the source as a superclass. If so, we'll need to
363 offset the pointer rather than just change its type.
364 FIXME: This fails silently with virtual inheritance. */
c906108c
SS
365 if (TYPE_NAME (t2) != NULL)
366 {
367 v = search_struct_field (type_name_no_tag (t2),
c5aa993b 368 value_zero (t1, not_lval), 0, t1, 1);
c906108c
SS
369 if (v)
370 {
d174216d
JB
371 CORE_ADDR addr2 = value_as_address (arg2);
372 addr2 -= (VALUE_ADDRESS (v)
373 + VALUE_OFFSET (v)
374 + VALUE_EMBEDDED_OFFSET (v));
375 return value_from_pointer (type, addr2);
c906108c
SS
376 }
377 }
378 }
379 /* No superclass found, just fall through to change ptr type. */
380 }
381 VALUE_TYPE (arg2) = type;
2b127877 382 arg2 = value_change_enclosing_type (arg2, type);
c5aa993b 383 VALUE_POINTED_TO_OFFSET (arg2) = 0; /* pai: chk_val */
c906108c
SS
384 return arg2;
385 }
c906108c
SS
386 else if (VALUE_LVAL (arg2) == lval_memory)
387 {
388 return value_at_lazy (type, VALUE_ADDRESS (arg2) + VALUE_OFFSET (arg2),
389 VALUE_BFD_SECTION (arg2));
390 }
391 else if (code1 == TYPE_CODE_VOID)
392 {
393 return value_zero (builtin_type_void, not_lval);
394 }
395 else
396 {
397 error ("Invalid cast.");
398 return 0;
399 }
400}
401
402/* Create a value of type TYPE that is zero, and return it. */
403
f23631e4 404struct value *
fba45db2 405value_zero (struct type *type, enum lval_type lv)
c906108c 406{
f23631e4 407 struct value *val = allocate_value (type);
c906108c
SS
408
409 memset (VALUE_CONTENTS (val), 0, TYPE_LENGTH (check_typedef (type)));
410 VALUE_LVAL (val) = lv;
411
412 return val;
413}
414
070ad9f0 415/* Return a value with type TYPE located at ADDR.
c906108c
SS
416
417 Call value_at only if the data needs to be fetched immediately;
418 if we can be 'lazy' and defer the fetch, perhaps indefinately, call
419 value_at_lazy instead. value_at_lazy simply records the address of
070ad9f0
DB
420 the data and sets the lazy-evaluation-required flag. The lazy flag
421 is tested in the VALUE_CONTENTS macro, which is used if and when
422 the contents are actually required.
c906108c
SS
423
424 Note: value_at does *NOT* handle embedded offsets; perform such
425 adjustments before or after calling it. */
426
f23631e4 427struct value *
fba45db2 428value_at (struct type *type, CORE_ADDR addr, asection *sect)
c906108c 429{
f23631e4 430 struct value *val;
c906108c
SS
431
432 if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
433 error ("Attempt to dereference a generic pointer.");
434
435 val = allocate_value (type);
436
75af7f68 437 read_memory (addr, VALUE_CONTENTS_ALL_RAW (val), TYPE_LENGTH (type));
c906108c
SS
438
439 VALUE_LVAL (val) = lval_memory;
440 VALUE_ADDRESS (val) = addr;
441 VALUE_BFD_SECTION (val) = sect;
442
443 return val;
444}
445
446/* Return a lazy value with type TYPE located at ADDR (cf. value_at). */
447
f23631e4 448struct value *
fba45db2 449value_at_lazy (struct type *type, CORE_ADDR addr, asection *sect)
c906108c 450{
f23631e4 451 struct value *val;
c906108c
SS
452
453 if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
454 error ("Attempt to dereference a generic pointer.");
455
456 val = allocate_value (type);
457
458 VALUE_LVAL (val) = lval_memory;
459 VALUE_ADDRESS (val) = addr;
460 VALUE_LAZY (val) = 1;
461 VALUE_BFD_SECTION (val) = sect;
462
463 return val;
464}
465
070ad9f0
DB
466/* Called only from the VALUE_CONTENTS and VALUE_CONTENTS_ALL macros,
467 if the current data for a variable needs to be loaded into
468 VALUE_CONTENTS(VAL). Fetches the data from the user's process, and
c906108c
SS
469 clears the lazy flag to indicate that the data in the buffer is valid.
470
471 If the value is zero-length, we avoid calling read_memory, which would
472 abort. We mark the value as fetched anyway -- all 0 bytes of it.
473
474 This function returns a value because it is used in the VALUE_CONTENTS
475 macro as part of an expression, where a void would not work. The
476 value is ignored. */
477
478int
f23631e4 479value_fetch_lazy (struct value *val)
c906108c
SS
480{
481 CORE_ADDR addr = VALUE_ADDRESS (val) + VALUE_OFFSET (val);
482 int length = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (val));
483
c5aa993b 484 struct type *type = VALUE_TYPE (val);
75af7f68 485 if (length)
d4b2399a 486 read_memory (addr, VALUE_CONTENTS_ALL_RAW (val), length);
802db21b 487
c906108c
SS
488 VALUE_LAZY (val) = 0;
489 return 0;
490}
491
492
493/* Store the contents of FROMVAL into the location of TOVAL.
494 Return a new value with the location of TOVAL and contents of FROMVAL. */
495
f23631e4
AC
496struct value *
497value_assign (struct value *toval, struct value *fromval)
c906108c 498{
52f0bd74 499 struct type *type;
f23631e4 500 struct value *val;
d9d9c31f 501 char raw_buffer[MAX_REGISTER_SIZE];
c906108c 502 int use_buffer = 0;
cb741690 503 struct frame_id old_frame;
c906108c
SS
504
505 if (!toval->modifiable)
506 error ("Left operand of assignment is not a modifiable lvalue.");
507
508 COERCE_REF (toval);
509
510 type = VALUE_TYPE (toval);
511 if (VALUE_LVAL (toval) != lval_internalvar)
512 fromval = value_cast (type, fromval);
513 else
514 COERCE_ARRAY (fromval);
515 CHECK_TYPEDEF (type);
516
cb741690
DJ
517 /* Since modifying a register can trash the frame chain, and modifying memory
518 can trash the frame cache, we save the old frame and then restore the new
519 frame afterwards. */
520 old_frame = get_frame_id (deprecated_selected_frame);
521
c906108c
SS
522 switch (VALUE_LVAL (toval))
523 {
524 case lval_internalvar:
525 set_internalvar (VALUE_INTERNALVAR (toval), fromval);
526 val = value_copy (VALUE_INTERNALVAR (toval)->value);
2b127877 527 val = value_change_enclosing_type (val, VALUE_ENCLOSING_TYPE (fromval));
c906108c
SS
528 VALUE_EMBEDDED_OFFSET (val) = VALUE_EMBEDDED_OFFSET (fromval);
529 VALUE_POINTED_TO_OFFSET (val) = VALUE_POINTED_TO_OFFSET (fromval);
530 return val;
531
532 case lval_internalvar_component:
533 set_internalvar_component (VALUE_INTERNALVAR (toval),
534 VALUE_OFFSET (toval),
535 VALUE_BITPOS (toval),
536 VALUE_BITSIZE (toval),
537 fromval);
538 break;
539
540 case lval_memory:
541 {
542 char *dest_buffer;
c5aa993b
JM
543 CORE_ADDR changed_addr;
544 int changed_len;
c906108c 545
c5aa993b
JM
546 if (VALUE_BITSIZE (toval))
547 {
c906108c
SS
548 char buffer[sizeof (LONGEST)];
549 /* We assume that the argument to read_memory is in units of
550 host chars. FIXME: Is that correct? */
551 changed_len = (VALUE_BITPOS (toval)
c5aa993b
JM
552 + VALUE_BITSIZE (toval)
553 + HOST_CHAR_BIT - 1)
554 / HOST_CHAR_BIT;
c906108c
SS
555
556 if (changed_len > (int) sizeof (LONGEST))
557 error ("Can't handle bitfields which don't fit in a %d bit word.",
baa6f10b 558 (int) sizeof (LONGEST) * HOST_CHAR_BIT);
c906108c
SS
559
560 read_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
561 buffer, changed_len);
562 modify_field (buffer, value_as_long (fromval),
563 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
564 changed_addr = VALUE_ADDRESS (toval) + VALUE_OFFSET (toval);
565 dest_buffer = buffer;
566 }
567 else if (use_buffer)
568 {
569 changed_addr = VALUE_ADDRESS (toval) + VALUE_OFFSET (toval);
570 changed_len = use_buffer;
571 dest_buffer = raw_buffer;
572 }
573 else
574 {
575 changed_addr = VALUE_ADDRESS (toval) + VALUE_OFFSET (toval);
576 changed_len = TYPE_LENGTH (type);
577 dest_buffer = VALUE_CONTENTS (fromval);
578 }
579
580 write_memory (changed_addr, dest_buffer, changed_len);
581 if (memory_changed_hook)
582 memory_changed_hook (changed_addr, changed_len);
e23792cc 583 target_changed_event ();
c906108c
SS
584 }
585 break;
586
c906108c 587 case lval_reg_frame_relative:
492254e9 588 case lval_register:
c906108c 589 {
c906108c 590 struct frame_info *frame;
ff2e87ac 591 int value_reg;
c906108c
SS
592
593 /* Figure out which frame this is in currently. */
492254e9
AC
594 if (VALUE_LVAL (toval) == lval_register)
595 {
596 frame = get_current_frame ();
597 value_reg = VALUE_REGNO (toval);
598 }
599 else
600 {
1df6926e 601 frame = frame_find_by_id (VALUE_FRAME_ID (toval));
492254e9
AC
602 value_reg = VALUE_FRAME_REGNUM (toval);
603 }
c906108c
SS
604
605 if (!frame)
606 error ("Value being assigned to is no longer active.");
492254e9 607
ff2e87ac
AC
608 if (VALUE_LVAL (toval) == lval_reg_frame_relative
609 && CONVERT_REGISTER_P (VALUE_FRAME_REGNUM (toval), type))
492254e9 610 {
ff2e87ac
AC
611 /* If TOVAL is a special machine register requiring
612 conversion of program values to a special raw format. */
613 VALUE_TO_REGISTER (frame, VALUE_FRAME_REGNUM (toval),
614 type, VALUE_CONTENTS (fromval));
492254e9 615 }
c906108c 616 else
492254e9 617 {
ff2e87ac
AC
618 /* TOVAL is stored in a series of registers in the frame
619 specified by the structure. Copy that value out,
620 modify it, and copy it back in. */
621 int amount_copied;
622 int amount_to_copy;
623 char *buffer;
624 int reg_offset;
625 int byte_offset;
626 int regno;
627
628 /* Locate the first register that falls in the value that
629 needs to be transfered. Compute the offset of the
630 value in that register. */
631 {
632 int offset;
633 for (reg_offset = value_reg, offset = 0;
12c266ea 634 offset + DEPRECATED_REGISTER_RAW_SIZE (reg_offset) <= VALUE_OFFSET (toval);
ff2e87ac
AC
635 reg_offset++);
636 byte_offset = VALUE_OFFSET (toval) - offset;
637 }
c906108c 638
ff2e87ac
AC
639 /* Compute the number of register aligned values that need
640 to be copied. */
641 if (VALUE_BITSIZE (toval))
642 amount_to_copy = byte_offset + 1;
643 else
644 amount_to_copy = byte_offset + TYPE_LENGTH (type);
492254e9 645
ff2e87ac
AC
646 /* And a bounce buffer. Be slightly over generous. */
647 buffer = (char *) alloca (amount_to_copy + MAX_REGISTER_SIZE);
648
649 /* Copy it in. */
650 for (regno = reg_offset, amount_copied = 0;
651 amount_copied < amount_to_copy;
12c266ea 652 amount_copied += DEPRECATED_REGISTER_RAW_SIZE (regno), regno++)
ff2e87ac 653 frame_register_read (frame, regno, buffer + amount_copied);
492254e9 654
ff2e87ac
AC
655 /* Modify what needs to be modified. */
656 if (VALUE_BITSIZE (toval))
657 modify_field (buffer + byte_offset,
658 value_as_long (fromval),
659 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
660 else if (use_buffer)
661 memcpy (buffer + VALUE_OFFSET (toval), raw_buffer, use_buffer);
c906108c 662 else
ff2e87ac
AC
663 memcpy (buffer + byte_offset, VALUE_CONTENTS (fromval),
664 TYPE_LENGTH (type));
665
666 /* Copy it out. */
667 for (regno = reg_offset, amount_copied = 0;
668 amount_copied < amount_to_copy;
12c266ea 669 amount_copied += DEPRECATED_REGISTER_RAW_SIZE (regno), regno++)
ff2e87ac 670 put_frame_register (frame, regno, buffer + amount_copied);
c906108c 671
ff2e87ac 672 }
c906108c
SS
673 if (register_changed_hook)
674 register_changed_hook (-1);
e23792cc 675 target_changed_event ();
ff2e87ac 676 break;
c906108c 677 }
492254e9 678
c906108c
SS
679 default:
680 error ("Left operand of assignment is not an lvalue.");
681 }
682
cb741690
DJ
683 /* Assigning to the stack pointer, frame pointer, and other
684 (architecture and calling convention specific) registers may
685 cause the frame cache to be out of date. Assigning to memory
686 also can. We just do this on all assignments to registers or
687 memory, for simplicity's sake; I doubt the slowdown matters. */
688 switch (VALUE_LVAL (toval))
689 {
690 case lval_memory:
691 case lval_register:
692 case lval_reg_frame_relative:
693
694 reinit_frame_cache ();
695
696 /* Having destoroyed the frame cache, restore the selected frame. */
697
698 /* FIXME: cagney/2002-11-02: There has to be a better way of
699 doing this. Instead of constantly saving/restoring the
700 frame. Why not create a get_selected_frame() function that,
701 having saved the selected frame's ID can automatically
702 re-find the previously selected frame automatically. */
703
704 {
705 struct frame_info *fi = frame_find_by_id (old_frame);
706 if (fi != NULL)
707 select_frame (fi);
708 }
709
710 break;
711 default:
712 break;
713 }
714
c906108c
SS
715 /* If the field does not entirely fill a LONGEST, then zero the sign bits.
716 If the field is signed, and is negative, then sign extend. */
717 if ((VALUE_BITSIZE (toval) > 0)
718 && (VALUE_BITSIZE (toval) < 8 * (int) sizeof (LONGEST)))
719 {
720 LONGEST fieldval = value_as_long (fromval);
721 LONGEST valmask = (((ULONGEST) 1) << VALUE_BITSIZE (toval)) - 1;
722
723 fieldval &= valmask;
724 if (!TYPE_UNSIGNED (type) && (fieldval & (valmask ^ (valmask >> 1))))
725 fieldval |= ~valmask;
726
727 fromval = value_from_longest (type, fieldval);
728 }
729
730 val = value_copy (toval);
731 memcpy (VALUE_CONTENTS_RAW (val), VALUE_CONTENTS (fromval),
732 TYPE_LENGTH (type));
733 VALUE_TYPE (val) = type;
2b127877 734 val = value_change_enclosing_type (val, VALUE_ENCLOSING_TYPE (fromval));
c906108c
SS
735 VALUE_EMBEDDED_OFFSET (val) = VALUE_EMBEDDED_OFFSET (fromval);
736 VALUE_POINTED_TO_OFFSET (val) = VALUE_POINTED_TO_OFFSET (fromval);
c5aa993b 737
c906108c
SS
738 return val;
739}
740
741/* Extend a value VAL to COUNT repetitions of its type. */
742
f23631e4
AC
743struct value *
744value_repeat (struct value *arg1, int count)
c906108c 745{
f23631e4 746 struct value *val;
c906108c
SS
747
748 if (VALUE_LVAL (arg1) != lval_memory)
749 error ("Only values in memory can be extended with '@'.");
750 if (count < 1)
751 error ("Invalid number %d of repetitions.", count);
752
753 val = allocate_repeat_value (VALUE_ENCLOSING_TYPE (arg1), count);
754
755 read_memory (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1),
756 VALUE_CONTENTS_ALL_RAW (val),
757 TYPE_LENGTH (VALUE_ENCLOSING_TYPE (val)));
758 VALUE_LVAL (val) = lval_memory;
759 VALUE_ADDRESS (val) = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1);
760
761 return val;
762}
763
f23631e4 764struct value *
fba45db2 765value_of_variable (struct symbol *var, struct block *b)
c906108c 766{
f23631e4 767 struct value *val;
c906108c
SS
768 struct frame_info *frame = NULL;
769
770 if (!b)
771 frame = NULL; /* Use selected frame. */
772 else if (symbol_read_needs_frame (var))
773 {
774 frame = block_innermost_frame (b);
775 if (!frame)
c5aa993b 776 {
c906108c 777 if (BLOCK_FUNCTION (b)
de5ad195 778 && SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)))
c906108c 779 error ("No frame is currently executing in block %s.",
de5ad195 780 SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)));
c906108c
SS
781 else
782 error ("No frame is currently executing in specified block");
c5aa993b 783 }
c906108c
SS
784 }
785
786 val = read_var_value (var, frame);
787 if (!val)
de5ad195 788 error ("Address of symbol \"%s\" is unknown.", SYMBOL_PRINT_NAME (var));
c906108c
SS
789
790 return val;
791}
792
793/* Given a value which is an array, return a value which is a pointer to its
794 first element, regardless of whether or not the array has a nonzero lower
795 bound.
796
797 FIXME: A previous comment here indicated that this routine should be
798 substracting the array's lower bound. It's not clear to me that this
799 is correct. Given an array subscripting operation, it would certainly
800 work to do the adjustment here, essentially computing:
801
802 (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0])
803
804 However I believe a more appropriate and logical place to account for
805 the lower bound is to do so in value_subscript, essentially computing:
806
807 (&array[0] + ((index - lowerbound) * sizeof array[0]))
808
809 As further evidence consider what would happen with operations other
810 than array subscripting, where the caller would get back a value that
811 had an address somewhere before the actual first element of the array,
812 and the information about the lower bound would be lost because of
813 the coercion to pointer type.
c5aa993b 814 */
c906108c 815
f23631e4
AC
816struct value *
817value_coerce_array (struct value *arg1)
c906108c 818{
52f0bd74 819 struct type *type = check_typedef (VALUE_TYPE (arg1));
c906108c
SS
820
821 if (VALUE_LVAL (arg1) != lval_memory)
822 error ("Attempt to take address of value not located in memory.");
823
4478b372
JB
824 return value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
825 (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
c906108c
SS
826}
827
828/* Given a value which is a function, return a value which is a pointer
829 to it. */
830
f23631e4
AC
831struct value *
832value_coerce_function (struct value *arg1)
c906108c 833{
f23631e4 834 struct value *retval;
c906108c
SS
835
836 if (VALUE_LVAL (arg1) != lval_memory)
837 error ("Attempt to take address of value not located in memory.");
838
4478b372
JB
839 retval = value_from_pointer (lookup_pointer_type (VALUE_TYPE (arg1)),
840 (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
c906108c
SS
841 VALUE_BFD_SECTION (retval) = VALUE_BFD_SECTION (arg1);
842 return retval;
c5aa993b 843}
c906108c
SS
844
845/* Return a pointer value for the object for which ARG1 is the contents. */
846
f23631e4
AC
847struct value *
848value_addr (struct value *arg1)
c906108c 849{
f23631e4 850 struct value *arg2;
c906108c
SS
851
852 struct type *type = check_typedef (VALUE_TYPE (arg1));
853 if (TYPE_CODE (type) == TYPE_CODE_REF)
854 {
855 /* Copy the value, but change the type from (T&) to (T*).
7b83ea04
AC
856 We keep the same location information, which is efficient,
857 and allows &(&X) to get the location containing the reference. */
c906108c
SS
858 arg2 = value_copy (arg1);
859 VALUE_TYPE (arg2) = lookup_pointer_type (TYPE_TARGET_TYPE (type));
860 return arg2;
861 }
862 if (TYPE_CODE (type) == TYPE_CODE_FUNC)
863 return value_coerce_function (arg1);
864
865 if (VALUE_LVAL (arg1) != lval_memory)
866 error ("Attempt to take address of value not located in memory.");
867
c5aa993b 868 /* Get target memory address */
4478b372
JB
869 arg2 = value_from_pointer (lookup_pointer_type (VALUE_TYPE (arg1)),
870 (VALUE_ADDRESS (arg1)
871 + VALUE_OFFSET (arg1)
872 + VALUE_EMBEDDED_OFFSET (arg1)));
c906108c
SS
873
874 /* This may be a pointer to a base subobject; so remember the
c5aa993b 875 full derived object's type ... */
2b127877 876 arg2 = value_change_enclosing_type (arg2, lookup_pointer_type (VALUE_ENCLOSING_TYPE (arg1)));
c5aa993b
JM
877 /* ... and also the relative position of the subobject in the full object */
878 VALUE_POINTED_TO_OFFSET (arg2) = VALUE_EMBEDDED_OFFSET (arg1);
c906108c
SS
879 VALUE_BFD_SECTION (arg2) = VALUE_BFD_SECTION (arg1);
880 return arg2;
881}
882
883/* Given a value of a pointer type, apply the C unary * operator to it. */
884
f23631e4
AC
885struct value *
886value_ind (struct value *arg1)
c906108c
SS
887{
888 struct type *base_type;
f23631e4 889 struct value *arg2;
c906108c
SS
890
891 COERCE_ARRAY (arg1);
892
893 base_type = check_typedef (VALUE_TYPE (arg1));
894
895 if (TYPE_CODE (base_type) == TYPE_CODE_MEMBER)
896 error ("not implemented: member types in value_ind");
897
898 /* Allow * on an integer so we can cast it to whatever we want.
899 This returns an int, which seems like the most C-like thing
900 to do. "long long" variables are rare enough that
901 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
902 if (TYPE_CODE (base_type) == TYPE_CODE_INT)
56468235
DH
903 return value_at_lazy (builtin_type_int,
904 (CORE_ADDR) value_as_long (arg1),
905 VALUE_BFD_SECTION (arg1));
c906108c
SS
906 else if (TYPE_CODE (base_type) == TYPE_CODE_PTR)
907 {
908 struct type *enc_type;
909 /* We may be pointing to something embedded in a larger object */
c5aa993b 910 /* Get the real type of the enclosing object */
c906108c
SS
911 enc_type = check_typedef (VALUE_ENCLOSING_TYPE (arg1));
912 enc_type = TYPE_TARGET_TYPE (enc_type);
c5aa993b
JM
913 /* Retrieve the enclosing object pointed to */
914 arg2 = value_at_lazy (enc_type,
1aa20aa8 915 value_as_address (arg1) - VALUE_POINTED_TO_OFFSET (arg1),
c5aa993b
JM
916 VALUE_BFD_SECTION (arg1));
917 /* Re-adjust type */
c906108c
SS
918 VALUE_TYPE (arg2) = TYPE_TARGET_TYPE (base_type);
919 /* Add embedding info */
2b127877 920 arg2 = value_change_enclosing_type (arg2, enc_type);
c906108c
SS
921 VALUE_EMBEDDED_OFFSET (arg2) = VALUE_POINTED_TO_OFFSET (arg1);
922
923 /* We may be pointing to an object of some derived type */
924 arg2 = value_full_object (arg2, NULL, 0, 0, 0);
925 return arg2;
926 }
927
928 error ("Attempt to take contents of a non-pointer value.");
c5aa993b 929 return 0; /* For lint -- never reached */
c906108c
SS
930}
931\f
932/* Pushing small parts of stack frames. */
933
934/* Push one word (the size of object that a register holds). */
935
936CORE_ADDR
fba45db2 937push_word (CORE_ADDR sp, ULONGEST word)
c906108c 938{
52f0bd74 939 int len = DEPRECATED_REGISTER_SIZE;
eb294659 940 char buffer[MAX_REGISTER_SIZE];
c906108c
SS
941
942 store_unsigned_integer (buffer, len, word);
943 if (INNER_THAN (1, 2))
944 {
945 /* stack grows downward */
946 sp -= len;
947 write_memory (sp, buffer, len);
948 }
949 else
950 {
951 /* stack grows upward */
952 write_memory (sp, buffer, len);
953 sp += len;
954 }
955
956 return sp;
957}
958
959/* Push LEN bytes with data at BUFFER. */
960
961CORE_ADDR
fba45db2 962push_bytes (CORE_ADDR sp, char *buffer, int len)
c906108c
SS
963{
964 if (INNER_THAN (1, 2))
965 {
966 /* stack grows downward */
967 sp -= len;
968 write_memory (sp, buffer, len);
969 }
970 else
971 {
972 /* stack grows upward */
973 write_memory (sp, buffer, len);
974 sp += len;
975 }
976
977 return sp;
978}
979
2df3850c
JM
980#ifndef PARM_BOUNDARY
981#define PARM_BOUNDARY (0)
982#endif
983
984/* Push onto the stack the specified value VALUE. Pad it correctly for
985 it to be an argument to a function. */
c906108c 986
c906108c 987static CORE_ADDR
aa1ee363 988value_push (CORE_ADDR sp, struct value *arg)
c906108c 989{
52f0bd74
AC
990 int len = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (arg));
991 int container_len = len;
992 int offset;
2df3850c
JM
993
994 /* How big is the container we're going to put this value in? */
995 if (PARM_BOUNDARY)
996 container_len = ((len + PARM_BOUNDARY / TARGET_CHAR_BIT - 1)
997 & ~(PARM_BOUNDARY / TARGET_CHAR_BIT - 1));
998
999 /* Are we going to put it at the high or low end of the container? */
d7449b42 1000 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
2df3850c
JM
1001 offset = container_len - len;
1002 else
1003 offset = 0;
c906108c
SS
1004
1005 if (INNER_THAN (1, 2))
1006 {
1007 /* stack grows downward */
2df3850c
JM
1008 sp -= container_len;
1009 write_memory (sp + offset, VALUE_CONTENTS_ALL (arg), len);
c906108c
SS
1010 }
1011 else
1012 {
1013 /* stack grows upward */
2df3850c
JM
1014 write_memory (sp + offset, VALUE_CONTENTS_ALL (arg), len);
1015 sp += container_len;
c906108c
SS
1016 }
1017
1018 return sp;
1019}
1020
392a587b 1021CORE_ADDR
b81774d8
AC
1022legacy_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
1023 int struct_return, CORE_ADDR struct_addr)
392a587b
JM
1024{
1025 /* ASSERT ( !struct_return); */
1026 int i;
1027 for (i = nargs - 1; i >= 0; i--)
1028 sp = value_push (sp, args[i]);
1029 return sp;
1030}
1031
c906108c
SS
1032/* Create a value for an array by allocating space in the inferior, copying
1033 the data into that space, and then setting up an array value.
1034
1035 The array bounds are set from LOWBOUND and HIGHBOUND, and the array is
1036 populated from the values passed in ELEMVEC.
1037
1038 The element type of the array is inherited from the type of the
1039 first element, and all elements must have the same size (though we
1040 don't currently enforce any restriction on their types). */
1041
f23631e4
AC
1042struct value *
1043value_array (int lowbound, int highbound, struct value **elemvec)
c906108c
SS
1044{
1045 int nelem;
1046 int idx;
1047 unsigned int typelength;
f23631e4 1048 struct value *val;
c906108c
SS
1049 struct type *rangetype;
1050 struct type *arraytype;
1051 CORE_ADDR addr;
1052
1053 /* Validate that the bounds are reasonable and that each of the elements
1054 have the same size. */
1055
1056 nelem = highbound - lowbound + 1;
1057 if (nelem <= 0)
1058 {
1059 error ("bad array bounds (%d, %d)", lowbound, highbound);
1060 }
1061 typelength = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (elemvec[0]));
1062 for (idx = 1; idx < nelem; idx++)
1063 {
1064 if (TYPE_LENGTH (VALUE_ENCLOSING_TYPE (elemvec[idx])) != typelength)
1065 {
1066 error ("array elements must all be the same size");
1067 }
1068 }
1069
1070 rangetype = create_range_type ((struct type *) NULL, builtin_type_int,
1071 lowbound, highbound);
c5aa993b
JM
1072 arraytype = create_array_type ((struct type *) NULL,
1073 VALUE_ENCLOSING_TYPE (elemvec[0]), rangetype);
c906108c
SS
1074
1075 if (!current_language->c_style_arrays)
1076 {
1077 val = allocate_value (arraytype);
1078 for (idx = 0; idx < nelem; idx++)
1079 {
1080 memcpy (VALUE_CONTENTS_ALL_RAW (val) + (idx * typelength),
1081 VALUE_CONTENTS_ALL (elemvec[idx]),
1082 typelength);
1083 }
1084 VALUE_BFD_SECTION (val) = VALUE_BFD_SECTION (elemvec[0]);
1085 return val;
1086 }
1087
1088 /* Allocate space to store the array in the inferior, and then initialize
1089 it by copying in each element. FIXME: Is it worth it to create a
1090 local buffer in which to collect each value and then write all the
1091 bytes in one operation? */
1092
1093 addr = allocate_space_in_inferior (nelem * typelength);
1094 for (idx = 0; idx < nelem; idx++)
1095 {
1096 write_memory (addr + (idx * typelength), VALUE_CONTENTS_ALL (elemvec[idx]),
1097 typelength);
1098 }
1099
1100 /* Create the array type and set up an array value to be evaluated lazily. */
1101
1102 val = value_at_lazy (arraytype, addr, VALUE_BFD_SECTION (elemvec[0]));
1103 return (val);
1104}
1105
1106/* Create a value for a string constant by allocating space in the inferior,
1107 copying the data into that space, and returning the address with type
1108 TYPE_CODE_STRING. PTR points to the string constant data; LEN is number
1109 of characters.
1110 Note that string types are like array of char types with a lower bound of
1111 zero and an upper bound of LEN - 1. Also note that the string may contain
1112 embedded null bytes. */
1113
f23631e4 1114struct value *
fba45db2 1115value_string (char *ptr, int len)
c906108c 1116{
f23631e4 1117 struct value *val;
c906108c
SS
1118 int lowbound = current_language->string_lower_bound;
1119 struct type *rangetype = create_range_type ((struct type *) NULL,
1120 builtin_type_int,
1121 lowbound, len + lowbound - 1);
1122 struct type *stringtype
c5aa993b 1123 = create_string_type ((struct type *) NULL, rangetype);
c906108c
SS
1124 CORE_ADDR addr;
1125
1126 if (current_language->c_style_arrays == 0)
1127 {
1128 val = allocate_value (stringtype);
1129 memcpy (VALUE_CONTENTS_RAW (val), ptr, len);
1130 return val;
1131 }
1132
1133
1134 /* Allocate space to store the string in the inferior, and then
1135 copy LEN bytes from PTR in gdb to that address in the inferior. */
1136
1137 addr = allocate_space_in_inferior (len);
1138 write_memory (addr, ptr, len);
1139
1140 val = value_at_lazy (stringtype, addr, NULL);
1141 return (val);
1142}
1143
f23631e4 1144struct value *
fba45db2 1145value_bitstring (char *ptr, int len)
c906108c 1146{
f23631e4 1147 struct value *val;
c906108c
SS
1148 struct type *domain_type = create_range_type (NULL, builtin_type_int,
1149 0, len - 1);
c5aa993b 1150 struct type *type = create_set_type ((struct type *) NULL, domain_type);
c906108c
SS
1151 TYPE_CODE (type) = TYPE_CODE_BITSTRING;
1152 val = allocate_value (type);
1153 memcpy (VALUE_CONTENTS_RAW (val), ptr, TYPE_LENGTH (type));
1154 return val;
1155}
1156\f
1157/* See if we can pass arguments in T2 to a function which takes arguments
ad2f7632
DJ
1158 of types T1. T1 is a list of NARGS arguments, and T2 is a NULL-terminated
1159 vector. If some arguments need coercion of some sort, then the coerced
1160 values are written into T2. Return value is 0 if the arguments could be
1161 matched, or the position at which they differ if not.
c906108c
SS
1162
1163 STATICP is nonzero if the T1 argument list came from a
ad2f7632
DJ
1164 static member function. T2 will still include the ``this'' pointer,
1165 but it will be skipped.
c906108c
SS
1166
1167 For non-static member functions, we ignore the first argument,
1168 which is the type of the instance variable. This is because we want
1169 to handle calls with objects from derived classes. This is not
1170 entirely correct: we should actually check to make sure that a
1171 requested operation is type secure, shouldn't we? FIXME. */
1172
1173static int
ad2f7632
DJ
1174typecmp (int staticp, int varargs, int nargs,
1175 struct field t1[], struct value *t2[])
c906108c
SS
1176{
1177 int i;
1178
1179 if (t2 == 0)
ad2f7632
DJ
1180 internal_error (__FILE__, __LINE__, "typecmp: no argument list");
1181
4a1970e4
DJ
1182 /* Skip ``this'' argument if applicable. T2 will always include THIS. */
1183 if (staticp)
ad2f7632
DJ
1184 t2 ++;
1185
1186 for (i = 0;
1187 (i < nargs) && TYPE_CODE (t1[i].type) != TYPE_CODE_VOID;
1188 i++)
c906108c 1189 {
c5aa993b 1190 struct type *tt1, *tt2;
ad2f7632 1191
c5aa993b
JM
1192 if (!t2[i])
1193 return i + 1;
ad2f7632
DJ
1194
1195 tt1 = check_typedef (t1[i].type);
c5aa993b 1196 tt2 = check_typedef (VALUE_TYPE (t2[i]));
ad2f7632 1197
c906108c 1198 if (TYPE_CODE (tt1) == TYPE_CODE_REF
c5aa993b 1199 /* We should be doing hairy argument matching, as below. */
c906108c
SS
1200 && (TYPE_CODE (check_typedef (TYPE_TARGET_TYPE (tt1))) == TYPE_CODE (tt2)))
1201 {
1202 if (TYPE_CODE (tt2) == TYPE_CODE_ARRAY)
1203 t2[i] = value_coerce_array (t2[i]);
1204 else
1205 t2[i] = value_addr (t2[i]);
1206 continue;
1207 }
1208
802db21b
DB
1209 /* djb - 20000715 - Until the new type structure is in the
1210 place, and we can attempt things like implicit conversions,
1211 we need to do this so you can take something like a map<const
1212 char *>, and properly access map["hello"], because the
1213 argument to [] will be a reference to a pointer to a char,
7168a814 1214 and the argument will be a pointer to a char. */
802db21b
DB
1215 while ( TYPE_CODE(tt1) == TYPE_CODE_REF ||
1216 TYPE_CODE (tt1) == TYPE_CODE_PTR)
1217 {
1218 tt1 = check_typedef( TYPE_TARGET_TYPE(tt1) );
1219 }
1220 while ( TYPE_CODE(tt2) == TYPE_CODE_ARRAY ||
1221 TYPE_CODE(tt2) == TYPE_CODE_PTR ||
1222 TYPE_CODE(tt2) == TYPE_CODE_REF)
c906108c 1223 {
802db21b 1224 tt2 = check_typedef( TYPE_TARGET_TYPE(tt2) );
c906108c 1225 }
c5aa993b
JM
1226 if (TYPE_CODE (tt1) == TYPE_CODE (tt2))
1227 continue;
c906108c
SS
1228 /* Array to pointer is a `trivial conversion' according to the ARM. */
1229
1230 /* We should be doing much hairier argument matching (see section 13.2
7b83ea04
AC
1231 of the ARM), but as a quick kludge, just check for the same type
1232 code. */
ad2f7632 1233 if (TYPE_CODE (t1[i].type) != TYPE_CODE (VALUE_TYPE (t2[i])))
c5aa993b 1234 return i + 1;
c906108c 1235 }
ad2f7632 1236 if (varargs || t2[i] == NULL)
c5aa993b 1237 return 0;
ad2f7632 1238 return i + 1;
c906108c
SS
1239}
1240
1241/* Helper function used by value_struct_elt to recurse through baseclasses.
1242 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1243 and search in it assuming it has (class) type TYPE.
1244 If found, return value, else return NULL.
1245
1246 If LOOKING_FOR_BASECLASS, then instead of looking for struct fields,
1247 look for a baseclass named NAME. */
1248
f23631e4
AC
1249static struct value *
1250search_struct_field (char *name, struct value *arg1, int offset,
aa1ee363 1251 struct type *type, int looking_for_baseclass)
c906108c
SS
1252{
1253 int i;
1254 int nbases = TYPE_N_BASECLASSES (type);
1255
1256 CHECK_TYPEDEF (type);
1257
c5aa993b 1258 if (!looking_for_baseclass)
c906108c
SS
1259 for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
1260 {
1261 char *t_field_name = TYPE_FIELD_NAME (type, i);
1262
db577aea 1263 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
c906108c 1264 {
f23631e4 1265 struct value *v;
c906108c 1266 if (TYPE_FIELD_STATIC (type, i))
2c2738a0
DC
1267 {
1268 v = value_static_field (type, i);
1269 if (v == 0)
1270 error ("field %s is nonexistent or has been optimised out",
1271 name);
1272 }
c906108c 1273 else
2c2738a0
DC
1274 {
1275 v = value_primitive_field (arg1, offset, i, type);
1276 if (v == 0)
1277 error ("there is no field named %s", name);
1278 }
c906108c
SS
1279 return v;
1280 }
1281
1282 if (t_field_name
1283 && (t_field_name[0] == '\0'
1284 || (TYPE_CODE (type) == TYPE_CODE_UNION
db577aea 1285 && (strcmp_iw (t_field_name, "else") == 0))))
c906108c
SS
1286 {
1287 struct type *field_type = TYPE_FIELD_TYPE (type, i);
1288 if (TYPE_CODE (field_type) == TYPE_CODE_UNION
1289 || TYPE_CODE (field_type) == TYPE_CODE_STRUCT)
1290 {
1291 /* Look for a match through the fields of an anonymous union,
1292 or anonymous struct. C++ provides anonymous unions.
1293
1b831c93
AC
1294 In the GNU Chill (now deleted from GDB)
1295 implementation of variant record types, each
1296 <alternative field> has an (anonymous) union type,
1297 each member of the union represents a <variant
1298 alternative>. Each <variant alternative> is
1299 represented as a struct, with a member for each
1300 <variant field>. */
c5aa993b 1301
f23631e4 1302 struct value *v;
c906108c
SS
1303 int new_offset = offset;
1304
db034ac5
AC
1305 /* This is pretty gross. In G++, the offset in an
1306 anonymous union is relative to the beginning of the
1b831c93
AC
1307 enclosing struct. In the GNU Chill (now deleted
1308 from GDB) implementation of variant records, the
1309 bitpos is zero in an anonymous union field, so we
1310 have to add the offset of the union here. */
c906108c
SS
1311 if (TYPE_CODE (field_type) == TYPE_CODE_STRUCT
1312 || (TYPE_NFIELDS (field_type) > 0
1313 && TYPE_FIELD_BITPOS (field_type, 0) == 0))
1314 new_offset += TYPE_FIELD_BITPOS (type, i) / 8;
1315
1316 v = search_struct_field (name, arg1, new_offset, field_type,
1317 looking_for_baseclass);
1318 if (v)
1319 return v;
1320 }
1321 }
1322 }
1323
c5aa993b 1324 for (i = 0; i < nbases; i++)
c906108c 1325 {
f23631e4 1326 struct value *v;
c906108c
SS
1327 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
1328 /* If we are looking for baseclasses, this is what we get when we
7b83ea04
AC
1329 hit them. But it could happen that the base part's member name
1330 is not yet filled in. */
c906108c
SS
1331 int found_baseclass = (looking_for_baseclass
1332 && TYPE_BASECLASS_NAME (type, i) != NULL
db577aea 1333 && (strcmp_iw (name, TYPE_BASECLASS_NAME (type, i)) == 0));
c906108c
SS
1334
1335 if (BASETYPE_VIA_VIRTUAL (type, i))
1336 {
1337 int boffset;
f23631e4 1338 struct value *v2 = allocate_value (basetype);
c906108c
SS
1339
1340 boffset = baseclass_offset (type, i,
1341 VALUE_CONTENTS (arg1) + offset,
1342 VALUE_ADDRESS (arg1)
c5aa993b 1343 + VALUE_OFFSET (arg1) + offset);
c906108c
SS
1344 if (boffset == -1)
1345 error ("virtual baseclass botch");
1346
1347 /* The virtual base class pointer might have been clobbered by the
1348 user program. Make sure that it still points to a valid memory
1349 location. */
1350
1351 boffset += offset;
1352 if (boffset < 0 || boffset >= TYPE_LENGTH (type))
1353 {
1354 CORE_ADDR base_addr;
c5aa993b 1355
c906108c
SS
1356 base_addr = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1) + boffset;
1357 if (target_read_memory (base_addr, VALUE_CONTENTS_RAW (v2),
1358 TYPE_LENGTH (basetype)) != 0)
1359 error ("virtual baseclass botch");
1360 VALUE_LVAL (v2) = lval_memory;
1361 VALUE_ADDRESS (v2) = base_addr;
1362 }
1363 else
1364 {
1365 VALUE_LVAL (v2) = VALUE_LVAL (arg1);
1366 VALUE_ADDRESS (v2) = VALUE_ADDRESS (arg1);
1367 VALUE_OFFSET (v2) = VALUE_OFFSET (arg1) + boffset;
1368 if (VALUE_LAZY (arg1))
1369 VALUE_LAZY (v2) = 1;
1370 else
1371 memcpy (VALUE_CONTENTS_RAW (v2),
1372 VALUE_CONTENTS_RAW (arg1) + boffset,
1373 TYPE_LENGTH (basetype));
1374 }
1375
1376 if (found_baseclass)
1377 return v2;
1378 v = search_struct_field (name, v2, 0, TYPE_BASECLASS (type, i),
1379 looking_for_baseclass);
1380 }
1381 else if (found_baseclass)
1382 v = value_primitive_field (arg1, offset, i, type);
1383 else
1384 v = search_struct_field (name, arg1,
c5aa993b 1385 offset + TYPE_BASECLASS_BITPOS (type, i) / 8,
c906108c 1386 basetype, looking_for_baseclass);
c5aa993b
JM
1387 if (v)
1388 return v;
c906108c
SS
1389 }
1390 return NULL;
1391}
1392
1393
1394/* Return the offset (in bytes) of the virtual base of type BASETYPE
1395 * in an object pointed to by VALADDR (on the host), assumed to be of
1396 * type TYPE. OFFSET is number of bytes beyond start of ARG to start
1397 * looking (in case VALADDR is the contents of an enclosing object).
1398 *
1399 * This routine recurses on the primary base of the derived class because
1400 * the virtual base entries of the primary base appear before the other
1401 * virtual base entries.
1402 *
1403 * If the virtual base is not found, a negative integer is returned.
1404 * The magnitude of the negative integer is the number of entries in
1405 * the virtual table to skip over (entries corresponding to various
1406 * ancestral classes in the chain of primary bases).
1407 *
1408 * Important: This assumes the HP / Taligent C++ runtime
1409 * conventions. Use baseclass_offset() instead to deal with g++
1410 * conventions. */
1411
1412void
fba45db2
KB
1413find_rt_vbase_offset (struct type *type, struct type *basetype, char *valaddr,
1414 int offset, int *boffset_p, int *skip_p)
c906108c 1415{
c5aa993b
JM
1416 int boffset; /* offset of virtual base */
1417 int index; /* displacement to use in virtual table */
c906108c 1418 int skip;
c5aa993b 1419
f23631e4 1420 struct value *vp;
c5aa993b
JM
1421 CORE_ADDR vtbl; /* the virtual table pointer */
1422 struct type *pbc; /* the primary base class */
c906108c
SS
1423
1424 /* Look for the virtual base recursively in the primary base, first.
1425 * This is because the derived class object and its primary base
1426 * subobject share the primary virtual table. */
c5aa993b 1427
c906108c 1428 boffset = 0;
c5aa993b 1429 pbc = TYPE_PRIMARY_BASE (type);
c906108c
SS
1430 if (pbc)
1431 {
1432 find_rt_vbase_offset (pbc, basetype, valaddr, offset, &boffset, &skip);
1433 if (skip < 0)
c5aa993b
JM
1434 {
1435 *boffset_p = boffset;
1436 *skip_p = -1;
1437 return;
1438 }
c906108c
SS
1439 }
1440 else
1441 skip = 0;
1442
1443
1444 /* Find the index of the virtual base according to HP/Taligent
1445 runtime spec. (Depth-first, left-to-right.) */
1446 index = virtual_base_index_skip_primaries (basetype, type);
1447
c5aa993b
JM
1448 if (index < 0)
1449 {
1450 *skip_p = skip + virtual_base_list_length_skip_primaries (type);
1451 *boffset_p = 0;
1452 return;
1453 }
c906108c 1454
c5aa993b 1455 /* pai: FIXME -- 32x64 possible problem */
c906108c 1456 /* First word (4 bytes) in object layout is the vtable pointer */
c5aa993b 1457 vtbl = *(CORE_ADDR *) (valaddr + offset);
c906108c 1458
c5aa993b 1459 /* Before the constructor is invoked, things are usually zero'd out. */
c906108c
SS
1460 if (vtbl == 0)
1461 error ("Couldn't find virtual table -- object may not be constructed yet.");
1462
1463
1464 /* Find virtual base's offset -- jump over entries for primary base
1465 * ancestors, then use the index computed above. But also adjust by
1466 * HP_ACC_VBASE_START for the vtable slots before the start of the
1467 * virtual base entries. Offset is negative -- virtual base entries
1468 * appear _before_ the address point of the virtual table. */
c5aa993b 1469
070ad9f0 1470 /* pai: FIXME -- 32x64 problem, if word = 8 bytes, change multiplier
c5aa993b 1471 & use long type */
c906108c
SS
1472
1473 /* epstein : FIXME -- added param for overlay section. May not be correct */
c5aa993b 1474 vp = value_at (builtin_type_int, vtbl + 4 * (-skip - index - HP_ACC_VBASE_START), NULL);
c906108c
SS
1475 boffset = value_as_long (vp);
1476 *skip_p = -1;
1477 *boffset_p = boffset;
1478 return;
1479}
1480
1481
1482/* Helper function used by value_struct_elt to recurse through baseclasses.
1483 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1484 and search in it assuming it has (class) type TYPE.
1485 If found, return value, else if name matched and args not return (value)-1,
1486 else return NULL. */
1487
f23631e4
AC
1488static struct value *
1489search_struct_method (char *name, struct value **arg1p,
1490 struct value **args, int offset,
aa1ee363 1491 int *static_memfuncp, struct type *type)
c906108c
SS
1492{
1493 int i;
f23631e4 1494 struct value *v;
c906108c
SS
1495 int name_matched = 0;
1496 char dem_opname[64];
1497
1498 CHECK_TYPEDEF (type);
1499 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
1500 {
1501 char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
1502 /* FIXME! May need to check for ARM demangling here */
c5aa993b
JM
1503 if (strncmp (t_field_name, "__", 2) == 0 ||
1504 strncmp (t_field_name, "op", 2) == 0 ||
1505 strncmp (t_field_name, "type", 4) == 0)
c906108c 1506 {
c5aa993b
JM
1507 if (cplus_demangle_opname (t_field_name, dem_opname, DMGL_ANSI))
1508 t_field_name = dem_opname;
1509 else if (cplus_demangle_opname (t_field_name, dem_opname, 0))
c906108c 1510 t_field_name = dem_opname;
c906108c 1511 }
db577aea 1512 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
c906108c
SS
1513 {
1514 int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1;
1515 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
c5aa993b 1516 name_matched = 1;
c906108c 1517
de17c821 1518 check_stub_method_group (type, i);
c906108c
SS
1519 if (j > 0 && args == 0)
1520 error ("cannot resolve overloaded method `%s': no arguments supplied", name);
acf5ed49 1521 else if (j == 0 && args == 0)
c906108c 1522 {
acf5ed49
DJ
1523 v = value_fn_field (arg1p, f, j, type, offset);
1524 if (v != NULL)
1525 return v;
c906108c 1526 }
acf5ed49
DJ
1527 else
1528 while (j >= 0)
1529 {
acf5ed49 1530 if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j),
ad2f7632
DJ
1531 TYPE_VARARGS (TYPE_FN_FIELD_TYPE (f, j)),
1532 TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f, j)),
acf5ed49
DJ
1533 TYPE_FN_FIELD_ARGS (f, j), args))
1534 {
1535 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1536 return value_virtual_fn_field (arg1p, f, j, type, offset);
1537 if (TYPE_FN_FIELD_STATIC_P (f, j) && static_memfuncp)
1538 *static_memfuncp = 1;
1539 v = value_fn_field (arg1p, f, j, type, offset);
1540 if (v != NULL)
1541 return v;
1542 }
1543 j--;
1544 }
c906108c
SS
1545 }
1546 }
1547
1548 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1549 {
1550 int base_offset;
1551
1552 if (BASETYPE_VIA_VIRTUAL (type, i))
1553 {
c5aa993b
JM
1554 if (TYPE_HAS_VTABLE (type))
1555 {
1556 /* HP aCC compiled type, search for virtual base offset
7b83ea04 1557 according to HP/Taligent runtime spec. */
c5aa993b
JM
1558 int skip;
1559 find_rt_vbase_offset (type, TYPE_BASECLASS (type, i),
1560 VALUE_CONTENTS_ALL (*arg1p),
1561 offset + VALUE_EMBEDDED_OFFSET (*arg1p),
1562 &base_offset, &skip);
1563 if (skip >= 0)
1564 error ("Virtual base class offset not found in vtable");
1565 }
1566 else
1567 {
1568 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
1569 char *base_valaddr;
1570
1571 /* The virtual base class pointer might have been clobbered by the
7b83ea04
AC
1572 user program. Make sure that it still points to a valid memory
1573 location. */
c5aa993b
JM
1574
1575 if (offset < 0 || offset >= TYPE_LENGTH (type))
1576 {
1577 base_valaddr = (char *) alloca (TYPE_LENGTH (baseclass));
1578 if (target_read_memory (VALUE_ADDRESS (*arg1p)
1579 + VALUE_OFFSET (*arg1p) + offset,
1580 base_valaddr,
1581 TYPE_LENGTH (baseclass)) != 0)
1582 error ("virtual baseclass botch");
1583 }
1584 else
1585 base_valaddr = VALUE_CONTENTS (*arg1p) + offset;
1586
1587 base_offset =
1588 baseclass_offset (type, i, base_valaddr,
1589 VALUE_ADDRESS (*arg1p)
1590 + VALUE_OFFSET (*arg1p) + offset);
1591 if (base_offset == -1)
1592 error ("virtual baseclass botch");
1593 }
1594 }
c906108c
SS
1595 else
1596 {
1597 base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
c5aa993b 1598 }
c906108c
SS
1599 v = search_struct_method (name, arg1p, args, base_offset + offset,
1600 static_memfuncp, TYPE_BASECLASS (type, i));
f23631e4 1601 if (v == (struct value *) - 1)
c906108c
SS
1602 {
1603 name_matched = 1;
1604 }
1605 else if (v)
1606 {
1607/* FIXME-bothner: Why is this commented out? Why is it here? */
c5aa993b 1608/* *arg1p = arg1_tmp; */
c906108c 1609 return v;
c5aa993b 1610 }
c906108c 1611 }
c5aa993b 1612 if (name_matched)
f23631e4 1613 return (struct value *) - 1;
c5aa993b
JM
1614 else
1615 return NULL;
c906108c
SS
1616}
1617
1618/* Given *ARGP, a value of type (pointer to a)* structure/union,
1619 extract the component named NAME from the ultimate target structure/union
1620 and return it as a value with its appropriate type.
1621 ERR is used in the error message if *ARGP's type is wrong.
1622
1623 C++: ARGS is a list of argument types to aid in the selection of
1624 an appropriate method. Also, handle derived types.
1625
1626 STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
1627 where the truthvalue of whether the function that was resolved was
1628 a static member function or not is stored.
1629
1630 ERR is an error message to be printed in case the field is not found. */
1631
f23631e4
AC
1632struct value *
1633value_struct_elt (struct value **argp, struct value **args,
fba45db2 1634 char *name, int *static_memfuncp, char *err)
c906108c 1635{
52f0bd74 1636 struct type *t;
f23631e4 1637 struct value *v;
c906108c
SS
1638
1639 COERCE_ARRAY (*argp);
1640
1641 t = check_typedef (VALUE_TYPE (*argp));
1642
1643 /* Follow pointers until we get to a non-pointer. */
1644
1645 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1646 {
1647 *argp = value_ind (*argp);
1648 /* Don't coerce fn pointer to fn and then back again! */
1649 if (TYPE_CODE (VALUE_TYPE (*argp)) != TYPE_CODE_FUNC)
1650 COERCE_ARRAY (*argp);
1651 t = check_typedef (VALUE_TYPE (*argp));
1652 }
1653
1654 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1655 error ("not implemented: member type in value_struct_elt");
1656
c5aa993b 1657 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
c906108c
SS
1658 && TYPE_CODE (t) != TYPE_CODE_UNION)
1659 error ("Attempt to extract a component of a value that is not a %s.", err);
1660
1661 /* Assume it's not, unless we see that it is. */
1662 if (static_memfuncp)
c5aa993b 1663 *static_memfuncp = 0;
c906108c
SS
1664
1665 if (!args)
1666 {
1667 /* if there are no arguments ...do this... */
1668
1669 /* Try as a field first, because if we succeed, there
7b83ea04 1670 is less work to be done. */
c906108c
SS
1671 v = search_struct_field (name, *argp, 0, t, 0);
1672 if (v)
1673 return v;
1674
1675 /* C++: If it was not found as a data field, then try to
7b83ea04 1676 return it as a pointer to a method. */
c906108c
SS
1677
1678 if (destructor_name_p (name, t))
1679 error ("Cannot get value of destructor");
1680
1681 v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
1682
f23631e4 1683 if (v == (struct value *) - 1)
c906108c
SS
1684 error ("Cannot take address of a method");
1685 else if (v == 0)
1686 {
1687 if (TYPE_NFN_FIELDS (t))
1688 error ("There is no member or method named %s.", name);
1689 else
1690 error ("There is no member named %s.", name);
1691 }
1692 return v;
1693 }
1694
1695 if (destructor_name_p (name, t))
1696 {
1697 if (!args[1])
1698 {
1699 /* Destructors are a special case. */
1700 int m_index, f_index;
1701
1702 v = NULL;
1703 if (get_destructor_fn_field (t, &m_index, &f_index))
1704 {
1705 v = value_fn_field (NULL, TYPE_FN_FIELDLIST1 (t, m_index),
1706 f_index, NULL, 0);
1707 }
1708 if (v == NULL)
1709 error ("could not find destructor function named %s.", name);
1710 else
1711 return v;
1712 }
1713 else
1714 {
1715 error ("destructor should not have any argument");
1716 }
1717 }
1718 else
1719 v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
7168a814 1720
f23631e4 1721 if (v == (struct value *) - 1)
c906108c 1722 {
7168a814 1723 error ("One of the arguments you tried to pass to %s could not be converted to what the function wants.", name);
c906108c
SS
1724 }
1725 else if (v == 0)
1726 {
1727 /* See if user tried to invoke data as function. If so,
7b83ea04
AC
1728 hand it back. If it's not callable (i.e., a pointer to function),
1729 gdb should give an error. */
c906108c
SS
1730 v = search_struct_field (name, *argp, 0, t, 0);
1731 }
1732
1733 if (!v)
1734 error ("Structure has no component named %s.", name);
1735 return v;
1736}
1737
1738/* Search through the methods of an object (and its bases)
1739 * to find a specified method. Return the pointer to the
1740 * fn_field list of overloaded instances.
1741 * Helper function for value_find_oload_list.
1742 * ARGP is a pointer to a pointer to a value (the object)
1743 * METHOD is a string containing the method name
1744 * OFFSET is the offset within the value
c906108c
SS
1745 * TYPE is the assumed type of the object
1746 * NUM_FNS is the number of overloaded instances
1747 * BASETYPE is set to the actual type of the subobject where the method is found
1748 * BOFFSET is the offset of the base subobject where the method is found */
1749
7a292a7a 1750static struct fn_field *
f23631e4 1751find_method_list (struct value **argp, char *method, int offset,
4a1970e4 1752 struct type *type, int *num_fns,
fba45db2 1753 struct type **basetype, int *boffset)
c906108c
SS
1754{
1755 int i;
c5aa993b 1756 struct fn_field *f;
c906108c
SS
1757 CHECK_TYPEDEF (type);
1758
1759 *num_fns = 0;
1760
c5aa993b
JM
1761 /* First check in object itself */
1762 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
c906108c
SS
1763 {
1764 /* pai: FIXME What about operators and type conversions? */
c5aa993b 1765 char *fn_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
db577aea 1766 if (fn_field_name && (strcmp_iw (fn_field_name, method) == 0))
c5aa993b 1767 {
4a1970e4
DJ
1768 int len = TYPE_FN_FIELDLIST_LENGTH (type, i);
1769 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
4a1970e4
DJ
1770
1771 *num_fns = len;
c5aa993b
JM
1772 *basetype = type;
1773 *boffset = offset;
4a1970e4 1774
de17c821
DJ
1775 /* Resolve any stub methods. */
1776 check_stub_method_group (type, i);
4a1970e4
DJ
1777
1778 return f;
c5aa993b
JM
1779 }
1780 }
1781
c906108c
SS
1782 /* Not found in object, check in base subobjects */
1783 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1784 {
1785 int base_offset;
1786 if (BASETYPE_VIA_VIRTUAL (type, i))
1787 {
c5aa993b
JM
1788 if (TYPE_HAS_VTABLE (type))
1789 {
1790 /* HP aCC compiled type, search for virtual base offset
1791 * according to HP/Taligent runtime spec. */
1792 int skip;
1793 find_rt_vbase_offset (type, TYPE_BASECLASS (type, i),
1794 VALUE_CONTENTS_ALL (*argp),
1795 offset + VALUE_EMBEDDED_OFFSET (*argp),
1796 &base_offset, &skip);
1797 if (skip >= 0)
1798 error ("Virtual base class offset not found in vtable");
1799 }
1800 else
1801 {
1802 /* probably g++ runtime model */
1803 base_offset = VALUE_OFFSET (*argp) + offset;
1804 base_offset =
1805 baseclass_offset (type, i,
1806 VALUE_CONTENTS (*argp) + base_offset,
1807 VALUE_ADDRESS (*argp) + base_offset);
1808 if (base_offset == -1)
1809 error ("virtual baseclass botch");
1810 }
1811 }
1812 else
1813 /* non-virtual base, simply use bit position from debug info */
c906108c
SS
1814 {
1815 base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
c5aa993b 1816 }
c906108c 1817 f = find_method_list (argp, method, base_offset + offset,
4a1970e4
DJ
1818 TYPE_BASECLASS (type, i), num_fns, basetype,
1819 boffset);
c906108c 1820 if (f)
c5aa993b 1821 return f;
c906108c 1822 }
c5aa993b 1823 return NULL;
c906108c
SS
1824}
1825
1826/* Return the list of overloaded methods of a specified name.
1827 * ARGP is a pointer to a pointer to a value (the object)
1828 * METHOD is the method name
1829 * OFFSET is the offset within the value contents
c906108c
SS
1830 * NUM_FNS is the number of overloaded instances
1831 * BASETYPE is set to the type of the base subobject that defines the method
1832 * BOFFSET is the offset of the base subobject which defines the method */
1833
1834struct fn_field *
f23631e4 1835value_find_oload_method_list (struct value **argp, char *method, int offset,
4a1970e4
DJ
1836 int *num_fns, struct type **basetype,
1837 int *boffset)
c906108c 1838{
c5aa993b 1839 struct type *t;
c906108c
SS
1840
1841 t = check_typedef (VALUE_TYPE (*argp));
1842
c5aa993b 1843 /* code snarfed from value_struct_elt */
c906108c
SS
1844 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1845 {
1846 *argp = value_ind (*argp);
1847 /* Don't coerce fn pointer to fn and then back again! */
1848 if (TYPE_CODE (VALUE_TYPE (*argp)) != TYPE_CODE_FUNC)
1849 COERCE_ARRAY (*argp);
1850 t = check_typedef (VALUE_TYPE (*argp));
1851 }
c5aa993b 1852
c906108c
SS
1853 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1854 error ("Not implemented: member type in value_find_oload_lis");
c5aa993b
JM
1855
1856 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1857 && TYPE_CODE (t) != TYPE_CODE_UNION)
c906108c 1858 error ("Attempt to extract a component of a value that is not a struct or union");
c5aa993b 1859
4a1970e4 1860 return find_method_list (argp, method, 0, t, num_fns, basetype, boffset);
c906108c
SS
1861}
1862
1863/* Given an array of argument types (ARGTYPES) (which includes an
1864 entry for "this" in the case of C++ methods), the number of
1865 arguments NARGS, the NAME of a function whether it's a method or
1866 not (METHOD), and the degree of laxness (LAX) in conforming to
1867 overload resolution rules in ANSI C++, find the best function that
1868 matches on the argument types according to the overload resolution
1869 rules.
1870
1871 In the case of class methods, the parameter OBJ is an object value
1872 in which to search for overloaded methods.
1873
1874 In the case of non-method functions, the parameter FSYM is a symbol
1875 corresponding to one of the overloaded functions.
1876
1877 Return value is an integer: 0 -> good match, 10 -> debugger applied
1878 non-standard coercions, 100 -> incompatible.
1879
1880 If a method is being searched for, VALP will hold the value.
1881 If a non-method is being searched for, SYMP will hold the symbol for it.
1882
1883 If a method is being searched for, and it is a static method,
1884 then STATICP will point to a non-zero value.
1885
1886 Note: This function does *not* check the value of
1887 overload_resolution. Caller must check it to see whether overload
1888 resolution is permitted.
c5aa993b 1889 */
c906108c
SS
1890
1891int
fba45db2 1892find_overload_match (struct type **arg_types, int nargs, char *name, int method,
7f8c9282 1893 int lax, struct value **objp, struct symbol *fsym,
f23631e4 1894 struct value **valp, struct symbol **symp, int *staticp)
c906108c
SS
1895{
1896 int nparms;
c5aa993b 1897 struct type **parm_types;
c906108c 1898 int champ_nparms = 0;
7f8c9282 1899 struct value *obj = (objp ? *objp : NULL);
c5aa993b
JM
1900
1901 short oload_champ = -1; /* Index of best overloaded function */
1902 short oload_ambiguous = 0; /* Current ambiguity state for overload resolution */
1903 /* 0 => no ambiguity, 1 => two good funcs, 2 => incomparable funcs */
1904 short oload_ambig_champ = -1; /* 2nd contender for best match */
1905 short oload_non_standard = 0; /* did we have to use non-standard conversions? */
1906 short oload_incompatible = 0; /* are args supplied incompatible with any function? */
1907
1908 struct badness_vector *bv; /* A measure of how good an overloaded instance is */
1909 struct badness_vector *oload_champ_bv = NULL; /* The measure for the current best match */
1910
f23631e4 1911 struct value *temp = obj;
c5aa993b
JM
1912 struct fn_field *fns_ptr = NULL; /* For methods, the list of overloaded methods */
1913 struct symbol **oload_syms = NULL; /* For non-methods, the list of overloaded function symbols */
1914 int num_fns = 0; /* Number of overloaded instances being considered */
1915 struct type *basetype = NULL;
c906108c 1916 int boffset;
52f0bd74
AC
1917 int jj;
1918 int ix;
4a1970e4 1919 int static_offset;
02f0d45d 1920 struct cleanup *cleanups = NULL;
c906108c 1921
c5aa993b
JM
1922 char *obj_type_name = NULL;
1923 char *func_name = NULL;
c906108c
SS
1924
1925 /* Get the list of overloaded methods or functions */
1926 if (method)
1927 {
1928 obj_type_name = TYPE_NAME (VALUE_TYPE (obj));
1929 /* Hack: evaluate_subexp_standard often passes in a pointer
7b83ea04 1930 value rather than the object itself, so try again */
c906108c 1931 if ((!obj_type_name || !*obj_type_name) &&
c5aa993b
JM
1932 (TYPE_CODE (VALUE_TYPE (obj)) == TYPE_CODE_PTR))
1933 obj_type_name = TYPE_NAME (TYPE_TARGET_TYPE (VALUE_TYPE (obj)));
c906108c
SS
1934
1935 fns_ptr = value_find_oload_method_list (&temp, name, 0,
c5aa993b
JM
1936 &num_fns,
1937 &basetype, &boffset);
c906108c 1938 if (!fns_ptr || !num_fns)
c5aa993b
JM
1939 error ("Couldn't find method %s%s%s",
1940 obj_type_name,
1941 (obj_type_name && *obj_type_name) ? "::" : "",
1942 name);
4a1970e4
DJ
1943 /* If we are dealing with stub method types, they should have
1944 been resolved by find_method_list via value_find_oload_method_list
1945 above. */
1946 gdb_assert (TYPE_DOMAIN_TYPE (fns_ptr[0].type) != NULL);
c906108c
SS
1947 }
1948 else
1949 {
1950 int i = -1;
22abf04a 1951 func_name = cplus_demangle (DEPRECATED_SYMBOL_NAME (fsym), DMGL_NO_OPTS);
c906108c 1952
917317f4 1953 /* If the name is NULL this must be a C-style function.
7b83ea04 1954 Just return the same symbol. */
917317f4 1955 if (!func_name)
7b83ea04 1956 {
917317f4 1957 *symp = fsym;
7b83ea04
AC
1958 return 0;
1959 }
917317f4 1960
c906108c 1961 oload_syms = make_symbol_overload_list (fsym);
02f0d45d 1962 cleanups = make_cleanup (xfree, oload_syms);
c906108c 1963 while (oload_syms[++i])
c5aa993b 1964 num_fns++;
c906108c 1965 if (!num_fns)
c5aa993b 1966 error ("Couldn't find function %s", func_name);
c906108c 1967 }
c5aa993b 1968
c906108c
SS
1969 oload_champ_bv = NULL;
1970
c5aa993b 1971 /* Consider each candidate in turn */
c906108c
SS
1972 for (ix = 0; ix < num_fns; ix++)
1973 {
4a1970e4 1974 static_offset = 0;
db577aea
AC
1975 if (method)
1976 {
4a1970e4
DJ
1977 if (TYPE_FN_FIELD_STATIC_P (fns_ptr, ix))
1978 static_offset = 1;
ad2f7632 1979 nparms = TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (fns_ptr, ix));
db577aea
AC
1980 }
1981 else
1982 {
1983 /* If it's not a method, this is the proper place */
1984 nparms=TYPE_NFIELDS(SYMBOL_TYPE(oload_syms[ix]));
1985 }
c906108c 1986
c5aa993b 1987 /* Prepare array of parameter types */
c906108c
SS
1988 parm_types = (struct type **) xmalloc (nparms * (sizeof (struct type *)));
1989 for (jj = 0; jj < nparms; jj++)
db577aea 1990 parm_types[jj] = (method
ad2f7632 1991 ? (TYPE_FN_FIELD_ARGS (fns_ptr, ix)[jj].type)
db577aea 1992 : TYPE_FIELD_TYPE (SYMBOL_TYPE (oload_syms[ix]), jj));
c906108c 1993
4a1970e4
DJ
1994 /* Compare parameter types to supplied argument types. Skip THIS for
1995 static methods. */
1996 bv = rank_function (parm_types, nparms, arg_types + static_offset,
1997 nargs - static_offset);
c5aa993b 1998
c906108c 1999 if (!oload_champ_bv)
c5aa993b
JM
2000 {
2001 oload_champ_bv = bv;
2002 oload_champ = 0;
2003 champ_nparms = nparms;
2004 }
c906108c 2005 else
c5aa993b
JM
2006 /* See whether current candidate is better or worse than previous best */
2007 switch (compare_badness (bv, oload_champ_bv))
2008 {
2009 case 0:
2010 oload_ambiguous = 1; /* top two contenders are equally good */
2011 oload_ambig_champ = ix;
2012 break;
2013 case 1:
2014 oload_ambiguous = 2; /* incomparable top contenders */
2015 oload_ambig_champ = ix;
2016 break;
2017 case 2:
2018 oload_champ_bv = bv; /* new champion, record details */
2019 oload_ambiguous = 0;
2020 oload_champ = ix;
2021 oload_ambig_champ = -1;
2022 champ_nparms = nparms;
2023 break;
2024 case 3:
2025 default:
2026 break;
2027 }
b8c9b27d 2028 xfree (parm_types);
6b1ba9a0
ND
2029 if (overload_debug)
2030 {
2031 if (method)
2032 fprintf_filtered (gdb_stderr,"Overloaded method instance %s, # of parms %d\n", fns_ptr[ix].physname, nparms);
2033 else
2034 fprintf_filtered (gdb_stderr,"Overloaded function instance %s # of parms %d\n", SYMBOL_DEMANGLED_NAME (oload_syms[ix]), nparms);
4a1970e4 2035 for (jj = 0; jj < nargs - static_offset; jj++)
6b1ba9a0
ND
2036 fprintf_filtered (gdb_stderr,"...Badness @ %d : %d\n", jj, bv->rank[jj]);
2037 fprintf_filtered (gdb_stderr,"Overload resolution champion is %d, ambiguous? %d\n", oload_champ, oload_ambiguous);
2038 }
c5aa993b 2039 } /* end loop over all candidates */
db577aea
AC
2040 /* NOTE: dan/2000-03-10: Seems to be a better idea to just pick one
2041 if they have the exact same goodness. This is because there is no
2042 way to differentiate based on return type, which we need to in
2043 cases like overloads of .begin() <It's both const and non-const> */
2044#if 0
c906108c
SS
2045 if (oload_ambiguous)
2046 {
2047 if (method)
c5aa993b
JM
2048 error ("Cannot resolve overloaded method %s%s%s to unique instance; disambiguate by specifying function signature",
2049 obj_type_name,
2050 (obj_type_name && *obj_type_name) ? "::" : "",
2051 name);
c906108c 2052 else
c5aa993b
JM
2053 error ("Cannot resolve overloaded function %s to unique instance; disambiguate by specifying function signature",
2054 func_name);
c906108c 2055 }
db577aea 2056#endif
c906108c 2057
4a1970e4
DJ
2058 /* Check how bad the best match is. */
2059 static_offset = 0;
2060 if (method && TYPE_FN_FIELD_STATIC_P (fns_ptr, oload_champ))
2061 static_offset = 1;
2062 for (ix = 1; ix <= nargs - static_offset; ix++)
c906108c 2063 {
6b1ba9a0
ND
2064 if (oload_champ_bv->rank[ix] >= 100)
2065 oload_incompatible = 1; /* truly mismatched types */
2066
2067 else if (oload_champ_bv->rank[ix] >= 10)
2068 oload_non_standard = 1; /* non-standard type conversions needed */
c906108c
SS
2069 }
2070 if (oload_incompatible)
2071 {
2072 if (method)
c5aa993b
JM
2073 error ("Cannot resolve method %s%s%s to any overloaded instance",
2074 obj_type_name,
2075 (obj_type_name && *obj_type_name) ? "::" : "",
2076 name);
c906108c 2077 else
c5aa993b
JM
2078 error ("Cannot resolve function %s to any overloaded instance",
2079 func_name);
c906108c
SS
2080 }
2081 else if (oload_non_standard)
2082 {
2083 if (method)
c5aa993b
JM
2084 warning ("Using non-standard conversion to match method %s%s%s to supplied arguments",
2085 obj_type_name,
2086 (obj_type_name && *obj_type_name) ? "::" : "",
2087 name);
c906108c 2088 else
c5aa993b
JM
2089 warning ("Using non-standard conversion to match function %s to supplied arguments",
2090 func_name);
c906108c
SS
2091 }
2092
2093 if (method)
2094 {
4a1970e4
DJ
2095 if (staticp && TYPE_FN_FIELD_STATIC_P (fns_ptr, oload_champ))
2096 *staticp = 1;
2097 else if (staticp)
2098 *staticp = 0;
c906108c 2099 if (TYPE_FN_FIELD_VIRTUAL_P (fns_ptr, oload_champ))
c5aa993b 2100 *valp = value_virtual_fn_field (&temp, fns_ptr, oload_champ, basetype, boffset);
c906108c 2101 else
c5aa993b 2102 *valp = value_fn_field (&temp, fns_ptr, oload_champ, basetype, boffset);
c906108c
SS
2103 }
2104 else
2105 {
2106 *symp = oload_syms[oload_champ];
b8c9b27d 2107 xfree (func_name);
c906108c
SS
2108 }
2109
7f8c9282
DJ
2110 if (objp)
2111 {
2112 if (TYPE_CODE (VALUE_TYPE (temp)) != TYPE_CODE_PTR
2113 && TYPE_CODE (VALUE_TYPE (*objp)) == TYPE_CODE_PTR)
2114 {
2115 temp = value_addr (temp);
2116 }
2117 *objp = temp;
2118 }
02f0d45d
DJ
2119 if (cleanups != NULL)
2120 do_cleanups (cleanups);
2121
c906108c
SS
2122 return oload_incompatible ? 100 : (oload_non_standard ? 10 : 0);
2123}
2124
2125/* C++: return 1 is NAME is a legitimate name for the destructor
2126 of type TYPE. If TYPE does not have a destructor, or
2127 if NAME is inappropriate for TYPE, an error is signaled. */
2128int
fba45db2 2129destructor_name_p (const char *name, const struct type *type)
c906108c
SS
2130{
2131 /* destructors are a special case. */
2132
2133 if (name[0] == '~')
2134 {
2135 char *dname = type_name_no_tag (type);
2136 char *cp = strchr (dname, '<');
2137 unsigned int len;
2138
2139 /* Do not compare the template part for template classes. */
2140 if (cp == NULL)
2141 len = strlen (dname);
2142 else
2143 len = cp - dname;
bf896cb0 2144 if (strlen (name + 1) != len || strncmp (dname, name + 1, len) != 0)
c906108c
SS
2145 error ("name of destructor must equal name of class");
2146 else
2147 return 1;
2148 }
2149 return 0;
2150}
2151
2152/* Helper function for check_field: Given TYPE, a structure/union,
2153 return 1 if the component named NAME from the ultimate
2154 target structure/union is defined, otherwise, return 0. */
2155
2156static int
aa1ee363 2157check_field_in (struct type *type, const char *name)
c906108c 2158{
52f0bd74 2159 int i;
c906108c
SS
2160
2161 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
2162 {
2163 char *t_field_name = TYPE_FIELD_NAME (type, i);
db577aea 2164 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
c906108c
SS
2165 return 1;
2166 }
2167
2168 /* C++: If it was not found as a data field, then try to
2169 return it as a pointer to a method. */
2170
2171 /* Destructors are a special case. */
2172 if (destructor_name_p (name, type))
2173 {
2174 int m_index, f_index;
2175
2176 return get_destructor_fn_field (type, &m_index, &f_index);
2177 }
2178
2179 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
2180 {
db577aea 2181 if (strcmp_iw (TYPE_FN_FIELDLIST_NAME (type, i), name) == 0)
c906108c
SS
2182 return 1;
2183 }
2184
2185 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
2186 if (check_field_in (TYPE_BASECLASS (type, i), name))
2187 return 1;
c5aa993b 2188
c906108c
SS
2189 return 0;
2190}
2191
2192
2193/* C++: Given ARG1, a value of type (pointer to a)* structure/union,
2194 return 1 if the component named NAME from the ultimate
2195 target structure/union is defined, otherwise, return 0. */
2196
2197int
f23631e4 2198check_field (struct value *arg1, const char *name)
c906108c 2199{
52f0bd74 2200 struct type *t;
c906108c
SS
2201
2202 COERCE_ARRAY (arg1);
2203
2204 t = VALUE_TYPE (arg1);
2205
2206 /* Follow pointers until we get to a non-pointer. */
2207
2208 for (;;)
2209 {
2210 CHECK_TYPEDEF (t);
2211 if (TYPE_CODE (t) != TYPE_CODE_PTR && TYPE_CODE (t) != TYPE_CODE_REF)
2212 break;
2213 t = TYPE_TARGET_TYPE (t);
2214 }
2215
2216 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
2217 error ("not implemented: member type in check_field");
2218
c5aa993b 2219 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
c906108c
SS
2220 && TYPE_CODE (t) != TYPE_CODE_UNION)
2221 error ("Internal error: `this' is not an aggregate");
2222
2223 return check_field_in (t, name);
2224}
2225
79c2c32d
DC
2226/* C++: Given an aggregate type CURTYPE, and a member name NAME,
2227 return the appropriate member. This function is used to resolve
2228 user expressions of the form "DOMAIN::NAME". For more details on
2229 what happens, see the comment before
2230 value_struct_elt_for_reference. */
2231
2232struct value *
2233value_aggregate_elt (struct type *curtype,
2234 char *name,
2235 enum noside noside)
2236{
2237 switch (TYPE_CODE (curtype))
2238 {
2239 case TYPE_CODE_STRUCT:
2240 case TYPE_CODE_UNION:
63d06c5c
DC
2241 return value_struct_elt_for_reference (curtype, 0, curtype, name, NULL,
2242 noside);
79c2c32d
DC
2243 case TYPE_CODE_NAMESPACE:
2244 return value_namespace_elt (curtype, name, noside);
2245 default:
2246 internal_error (__FILE__, __LINE__,
2247 "non-aggregate type in value_aggregate_elt");
2248 }
2249}
2250
c906108c
SS
2251/* C++: Given an aggregate type CURTYPE, and a member name NAME,
2252 return the address of this member as a "pointer to member"
2253 type. If INTYPE is non-null, then it will be the type
2254 of the member we are looking for. This will help us resolve
2255 "pointers to member functions". This function is used
2256 to resolve user expressions of the form "DOMAIN::NAME". */
2257
63d06c5c 2258static struct value *
fba45db2
KB
2259value_struct_elt_for_reference (struct type *domain, int offset,
2260 struct type *curtype, char *name,
63d06c5c
DC
2261 struct type *intype,
2262 enum noside noside)
c906108c 2263{
52f0bd74
AC
2264 struct type *t = curtype;
2265 int i;
f23631e4 2266 struct value *v;
c906108c 2267
c5aa993b 2268 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
c906108c
SS
2269 && TYPE_CODE (t) != TYPE_CODE_UNION)
2270 error ("Internal error: non-aggregate type to value_struct_elt_for_reference");
2271
2272 for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
2273 {
2274 char *t_field_name = TYPE_FIELD_NAME (t, i);
c5aa993b 2275
6314a349 2276 if (t_field_name && strcmp (t_field_name, name) == 0)
c906108c
SS
2277 {
2278 if (TYPE_FIELD_STATIC (t, i))
2279 {
2280 v = value_static_field (t, i);
2281 if (v == NULL)
2c2738a0 2282 error ("static field %s has been optimized out",
c906108c
SS
2283 name);
2284 return v;
2285 }
2286 if (TYPE_FIELD_PACKED (t, i))
2287 error ("pointers to bitfield members not allowed");
c5aa993b 2288
c906108c
SS
2289 return value_from_longest
2290 (lookup_reference_type (lookup_member_type (TYPE_FIELD_TYPE (t, i),
2291 domain)),
2292 offset + (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3));
2293 }
2294 }
2295
2296 /* C++: If it was not found as a data field, then try to
2297 return it as a pointer to a method. */
2298
2299 /* Destructors are a special case. */
2300 if (destructor_name_p (name, t))
2301 {
2302 error ("member pointers to destructors not implemented yet");
2303 }
2304
2305 /* Perform all necessary dereferencing. */
2306 while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR)
2307 intype = TYPE_TARGET_TYPE (intype);
2308
2309 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
2310 {
2311 char *t_field_name = TYPE_FN_FIELDLIST_NAME (t, i);
2312 char dem_opname[64];
2313
c5aa993b
JM
2314 if (strncmp (t_field_name, "__", 2) == 0 ||
2315 strncmp (t_field_name, "op", 2) == 0 ||
2316 strncmp (t_field_name, "type", 4) == 0)
c906108c 2317 {
c5aa993b
JM
2318 if (cplus_demangle_opname (t_field_name, dem_opname, DMGL_ANSI))
2319 t_field_name = dem_opname;
2320 else if (cplus_demangle_opname (t_field_name, dem_opname, 0))
c906108c 2321 t_field_name = dem_opname;
c906108c 2322 }
6314a349 2323 if (t_field_name && strcmp (t_field_name, name) == 0)
c906108c
SS
2324 {
2325 int j = TYPE_FN_FIELDLIST_LENGTH (t, i);
2326 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
c5aa993b 2327
de17c821
DJ
2328 check_stub_method_group (t, i);
2329
c906108c
SS
2330 if (intype == 0 && j > 1)
2331 error ("non-unique member `%s' requires type instantiation", name);
2332 if (intype)
2333 {
2334 while (j--)
2335 if (TYPE_FN_FIELD_TYPE (f, j) == intype)
2336 break;
2337 if (j < 0)
2338 error ("no member function matches that type instantiation");
2339 }
2340 else
2341 j = 0;
c5aa993b 2342
c906108c
SS
2343 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
2344 {
2345 return value_from_longest
2346 (lookup_reference_type
2347 (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
2348 domain)),
2349 (LONGEST) METHOD_PTR_FROM_VOFFSET (TYPE_FN_FIELD_VOFFSET (f, j)));
2350 }
2351 else
2352 {
2353 struct symbol *s = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
176620f1 2354 0, VAR_DOMAIN, 0, NULL);
c906108c
SS
2355 if (s == NULL)
2356 {
2357 v = 0;
2358 }
2359 else
2360 {
2361 v = read_var_value (s, 0);
2362#if 0
2363 VALUE_TYPE (v) = lookup_reference_type
2364 (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
2365 domain));
2366#endif
2367 }
2368 return v;
2369 }
2370 }
2371 }
2372 for (i = TYPE_N_BASECLASSES (t) - 1; i >= 0; i--)
2373 {
f23631e4 2374 struct value *v;
c906108c
SS
2375 int base_offset;
2376
2377 if (BASETYPE_VIA_VIRTUAL (t, i))
2378 base_offset = 0;
2379 else
2380 base_offset = TYPE_BASECLASS_BITPOS (t, i) / 8;
2381 v = value_struct_elt_for_reference (domain,
2382 offset + base_offset,
2383 TYPE_BASECLASS (t, i),
2384 name,
63d06c5c
DC
2385 intype,
2386 noside);
c906108c
SS
2387 if (v)
2388 return v;
2389 }
63d06c5c
DC
2390
2391 /* As a last chance, pretend that CURTYPE is a namespace, and look
2392 it up that way; this (frequently) works for types nested inside
2393 classes. */
2394
2395 return value_maybe_namespace_elt (curtype, name, noside);
c906108c
SS
2396}
2397
79c2c32d
DC
2398/* C++: Return the member NAME of the namespace given by the type
2399 CURTYPE. */
2400
2401static struct value *
2402value_namespace_elt (const struct type *curtype,
63d06c5c 2403 char *name,
79c2c32d 2404 enum noside noside)
63d06c5c
DC
2405{
2406 struct value *retval = value_maybe_namespace_elt (curtype, name,
2407 noside);
2408
2409 if (retval == NULL)
2410 error ("No symbol \"%s\" in namespace \"%s\".", name,
2411 TYPE_TAG_NAME (curtype));
2412
2413 return retval;
2414}
2415
2416/* A helper function used by value_namespace_elt and
2417 value_struct_elt_for_reference. It looks up NAME inside the
2418 context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
2419 is a class and NAME refers to a type in CURTYPE itself (as opposed
2420 to, say, some base class of CURTYPE). */
2421
2422static struct value *
2423value_maybe_namespace_elt (const struct type *curtype,
2424 char *name,
2425 enum noside noside)
79c2c32d
DC
2426{
2427 const char *namespace_name = TYPE_TAG_NAME (curtype);
2428 struct symbol *sym;
79c2c32d
DC
2429
2430 sym = cp_lookup_symbol_namespace (namespace_name, name, NULL,
2431 get_selected_block (0), VAR_DOMAIN,
2432 NULL);
2433
2434 if (sym == NULL)
63d06c5c 2435 return NULL;
79c2c32d
DC
2436 else if ((noside == EVAL_AVOID_SIDE_EFFECTS)
2437 && (SYMBOL_CLASS (sym) == LOC_TYPEDEF))
63d06c5c 2438 return allocate_value (SYMBOL_TYPE (sym));
79c2c32d 2439 else
63d06c5c 2440 return value_of_variable (sym, get_selected_block (0));
79c2c32d
DC
2441}
2442
c906108c
SS
2443/* Given a pointer value V, find the real (RTTI) type
2444 of the object it points to.
2445 Other parameters FULL, TOP, USING_ENC as with value_rtti_type()
2446 and refer to the values computed for the object pointed to. */
2447
2448struct type *
f23631e4 2449value_rtti_target_type (struct value *v, int *full, int *top, int *using_enc)
c906108c 2450{
f23631e4 2451 struct value *target;
c906108c
SS
2452
2453 target = value_ind (v);
2454
2455 return value_rtti_type (target, full, top, using_enc);
2456}
2457
2458/* Given a value pointed to by ARGP, check its real run-time type, and
2459 if that is different from the enclosing type, create a new value
2460 using the real run-time type as the enclosing type (and of the same
2461 type as ARGP) and return it, with the embedded offset adjusted to
2462 be the correct offset to the enclosed object
2463 RTYPE is the type, and XFULL, XTOP, and XUSING_ENC are the other
2464 parameters, computed by value_rtti_type(). If these are available,
2465 they can be supplied and a second call to value_rtti_type() is avoided.
2466 (Pass RTYPE == NULL if they're not available */
2467
f23631e4
AC
2468struct value *
2469value_full_object (struct value *argp, struct type *rtype, int xfull, int xtop,
fba45db2 2470 int xusing_enc)
c906108c 2471{
c5aa993b 2472 struct type *real_type;
c906108c
SS
2473 int full = 0;
2474 int top = -1;
2475 int using_enc = 0;
f23631e4 2476 struct value *new_val;
c906108c
SS
2477
2478 if (rtype)
2479 {
2480 real_type = rtype;
2481 full = xfull;
2482 top = xtop;
2483 using_enc = xusing_enc;
2484 }
2485 else
2486 real_type = value_rtti_type (argp, &full, &top, &using_enc);
2487
2488 /* If no RTTI data, or if object is already complete, do nothing */
2489 if (!real_type || real_type == VALUE_ENCLOSING_TYPE (argp))
2490 return argp;
2491
2492 /* If we have the full object, but for some reason the enclosing
c5aa993b 2493 type is wrong, set it *//* pai: FIXME -- sounds iffy */
c906108c
SS
2494 if (full)
2495 {
2b127877 2496 argp = value_change_enclosing_type (argp, real_type);
c906108c
SS
2497 return argp;
2498 }
2499
2500 /* Check if object is in memory */
2501 if (VALUE_LVAL (argp) != lval_memory)
2502 {
2503 warning ("Couldn't retrieve complete object of RTTI type %s; object may be in register(s).", TYPE_NAME (real_type));
c5aa993b 2504
c906108c
SS
2505 return argp;
2506 }
c5aa993b 2507
c906108c
SS
2508 /* All other cases -- retrieve the complete object */
2509 /* Go back by the computed top_offset from the beginning of the object,
2510 adjusting for the embedded offset of argp if that's what value_rtti_type
2511 used for its computation. */
2512 new_val = value_at_lazy (real_type, VALUE_ADDRESS (argp) - top +
c5aa993b
JM
2513 (using_enc ? 0 : VALUE_EMBEDDED_OFFSET (argp)),
2514 VALUE_BFD_SECTION (argp));
c906108c
SS
2515 VALUE_TYPE (new_val) = VALUE_TYPE (argp);
2516 VALUE_EMBEDDED_OFFSET (new_val) = using_enc ? top + VALUE_EMBEDDED_OFFSET (argp) : top;
2517 return new_val;
2518}
2519
389e51db
AC
2520
2521
2522
d069f99d 2523/* Return the value of the local variable, if one exists.
c906108c
SS
2524 Flag COMPLAIN signals an error if the request is made in an
2525 inappropriate context. */
2526
f23631e4 2527struct value *
d069f99d 2528value_of_local (const char *name, int complain)
c906108c
SS
2529{
2530 struct symbol *func, *sym;
2531 struct block *b;
d069f99d 2532 struct value * ret;
c906108c 2533
6e7f8b9c 2534 if (deprecated_selected_frame == 0)
c906108c
SS
2535 {
2536 if (complain)
c5aa993b
JM
2537 error ("no frame selected");
2538 else
2539 return 0;
c906108c
SS
2540 }
2541
6e7f8b9c 2542 func = get_frame_function (deprecated_selected_frame);
c906108c
SS
2543 if (!func)
2544 {
2545 if (complain)
2625d86c 2546 error ("no `%s' in nameless context", name);
c5aa993b
JM
2547 else
2548 return 0;
c906108c
SS
2549 }
2550
2551 b = SYMBOL_BLOCK_VALUE (func);
de4f826b 2552 if (dict_empty (BLOCK_DICT (b)))
c906108c
SS
2553 {
2554 if (complain)
2625d86c 2555 error ("no args, no `%s'", name);
c5aa993b
JM
2556 else
2557 return 0;
c906108c
SS
2558 }
2559
2560 /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
2561 symbol instead of the LOC_ARG one (if both exist). */
176620f1 2562 sym = lookup_block_symbol (b, name, NULL, VAR_DOMAIN);
c906108c
SS
2563 if (sym == NULL)
2564 {
2565 if (complain)
2625d86c 2566 error ("current stack frame does not contain a variable named `%s'", name);
c906108c
SS
2567 else
2568 return NULL;
2569 }
2570
6e7f8b9c 2571 ret = read_var_value (sym, deprecated_selected_frame);
d069f99d 2572 if (ret == 0 && complain)
2625d86c 2573 error ("`%s' argument unreadable", name);
d069f99d
AF
2574 return ret;
2575}
2576
2577/* C++/Objective-C: return the value of the class instance variable,
2578 if one exists. Flag COMPLAIN signals an error if the request is
2579 made in an inappropriate context. */
2580
2581struct value *
2582value_of_this (int complain)
2583{
2584 if (current_language->la_language == language_objc)
2585 return value_of_local ("self", complain);
2586 else
2587 return value_of_local ("this", complain);
c906108c
SS
2588}
2589
2590/* Create a slice (sub-string, sub-array) of ARRAY, that is LENGTH elements
2591 long, starting at LOWBOUND. The result has the same lower bound as
2592 the original ARRAY. */
2593
f23631e4
AC
2594struct value *
2595value_slice (struct value *array, int lowbound, int length)
c906108c
SS
2596{
2597 struct type *slice_range_type, *slice_type, *range_type;
7a67d0fe 2598 LONGEST lowerbound, upperbound;
f23631e4 2599 struct value *slice;
c906108c
SS
2600 struct type *array_type;
2601 array_type = check_typedef (VALUE_TYPE (array));
2602 COERCE_VARYING_ARRAY (array, array_type);
2603 if (TYPE_CODE (array_type) != TYPE_CODE_ARRAY
2604 && TYPE_CODE (array_type) != TYPE_CODE_STRING
2605 && TYPE_CODE (array_type) != TYPE_CODE_BITSTRING)
2606 error ("cannot take slice of non-array");
2607 range_type = TYPE_INDEX_TYPE (array_type);
2608 if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0)
2609 error ("slice from bad array or bitstring");
2610 if (lowbound < lowerbound || length < 0
db034ac5 2611 || lowbound + length - 1 > upperbound)
c906108c
SS
2612 error ("slice out of range");
2613 /* FIXME-type-allocation: need a way to free this type when we are
2614 done with it. */
c5aa993b 2615 slice_range_type = create_range_type ((struct type *) NULL,
c906108c
SS
2616 TYPE_TARGET_TYPE (range_type),
2617 lowbound, lowbound + length - 1);
2618 if (TYPE_CODE (array_type) == TYPE_CODE_BITSTRING)
2619 {
2620 int i;
c5aa993b 2621 slice_type = create_set_type ((struct type *) NULL, slice_range_type);
c906108c
SS
2622 TYPE_CODE (slice_type) = TYPE_CODE_BITSTRING;
2623 slice = value_zero (slice_type, not_lval);
2624 for (i = 0; i < length; i++)
2625 {
2626 int element = value_bit_index (array_type,
2627 VALUE_CONTENTS (array),
2628 lowbound + i);
2629 if (element < 0)
2630 error ("internal error accessing bitstring");
2631 else if (element > 0)
2632 {
2633 int j = i % TARGET_CHAR_BIT;
2634 if (BITS_BIG_ENDIAN)
2635 j = TARGET_CHAR_BIT - 1 - j;
2636 VALUE_CONTENTS_RAW (slice)[i / TARGET_CHAR_BIT] |= (1 << j);
2637 }
2638 }
2639 /* We should set the address, bitssize, and bitspos, so the clice
7b83ea04
AC
2640 can be used on the LHS, but that may require extensions to
2641 value_assign. For now, just leave as a non_lval. FIXME. */
c906108c
SS
2642 }
2643 else
2644 {
2645 struct type *element_type = TYPE_TARGET_TYPE (array_type);
7a67d0fe 2646 LONGEST offset
c906108c 2647 = (lowbound - lowerbound) * TYPE_LENGTH (check_typedef (element_type));
c5aa993b 2648 slice_type = create_array_type ((struct type *) NULL, element_type,
c906108c
SS
2649 slice_range_type);
2650 TYPE_CODE (slice_type) = TYPE_CODE (array_type);
2651 slice = allocate_value (slice_type);
2652 if (VALUE_LAZY (array))
2653 VALUE_LAZY (slice) = 1;
2654 else
2655 memcpy (VALUE_CONTENTS (slice), VALUE_CONTENTS (array) + offset,
2656 TYPE_LENGTH (slice_type));
2657 if (VALUE_LVAL (array) == lval_internalvar)
2658 VALUE_LVAL (slice) = lval_internalvar_component;
2659 else
2660 VALUE_LVAL (slice) = VALUE_LVAL (array);
2661 VALUE_ADDRESS (slice) = VALUE_ADDRESS (array);
2662 VALUE_OFFSET (slice) = VALUE_OFFSET (array) + offset;
2663 }
2664 return slice;
2665}
2666
070ad9f0
DB
2667/* Create a value for a FORTRAN complex number. Currently most of
2668 the time values are coerced to COMPLEX*16 (i.e. a complex number
2669 composed of 2 doubles. This really should be a smarter routine
2670 that figures out precision inteligently as opposed to assuming
c5aa993b 2671 doubles. FIXME: fmb */
c906108c 2672
f23631e4
AC
2673struct value *
2674value_literal_complex (struct value *arg1, struct value *arg2, struct type *type)
c906108c 2675{
f23631e4 2676 struct value *val;
c906108c
SS
2677 struct type *real_type = TYPE_TARGET_TYPE (type);
2678
2679 val = allocate_value (type);
2680 arg1 = value_cast (real_type, arg1);
2681 arg2 = value_cast (real_type, arg2);
2682
2683 memcpy (VALUE_CONTENTS_RAW (val),
2684 VALUE_CONTENTS (arg1), TYPE_LENGTH (real_type));
2685 memcpy (VALUE_CONTENTS_RAW (val) + TYPE_LENGTH (real_type),
2686 VALUE_CONTENTS (arg2), TYPE_LENGTH (real_type));
2687 return val;
2688}
2689
2690/* Cast a value into the appropriate complex data type. */
2691
f23631e4
AC
2692static struct value *
2693cast_into_complex (struct type *type, struct value *val)
c906108c
SS
2694{
2695 struct type *real_type = TYPE_TARGET_TYPE (type);
2696 if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_COMPLEX)
2697 {
2698 struct type *val_real_type = TYPE_TARGET_TYPE (VALUE_TYPE (val));
f23631e4
AC
2699 struct value *re_val = allocate_value (val_real_type);
2700 struct value *im_val = allocate_value (val_real_type);
c906108c
SS
2701
2702 memcpy (VALUE_CONTENTS_RAW (re_val),
2703 VALUE_CONTENTS (val), TYPE_LENGTH (val_real_type));
2704 memcpy (VALUE_CONTENTS_RAW (im_val),
2705 VALUE_CONTENTS (val) + TYPE_LENGTH (val_real_type),
c5aa993b 2706 TYPE_LENGTH (val_real_type));
c906108c
SS
2707
2708 return value_literal_complex (re_val, im_val, type);
2709 }
2710 else if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FLT
2711 || TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_INT)
2712 return value_literal_complex (val, value_zero (real_type, not_lval), type);
2713 else
2714 error ("cannot cast non-number to complex");
2715}
2716
2717void
fba45db2 2718_initialize_valops (void)
c906108c
SS
2719{
2720#if 0
2721 add_show_from_set
c5aa993b 2722 (add_set_cmd ("abandon", class_support, var_boolean, (char *) &auto_abandon,
c906108c
SS
2723 "Set automatic abandonment of expressions upon failure.",
2724 &setlist),
2725 &showlist);
2726#endif
2727
2728 add_show_from_set
c5aa993b 2729 (add_set_cmd ("overload-resolution", class_support, var_boolean, (char *) &overload_resolution,
c906108c
SS
2730 "Set overload resolution in evaluating C++ functions.",
2731 &setlist),
2732 &showlist);
2733 overload_resolution = 1;
c906108c 2734}
This page took 0.659924 seconds and 4 git commands to generate.