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