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