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