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