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