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