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