Fix undefined behavior, don't pass NULL to fwrite
[deliverable/binutils-gdb.git] / gdb / valops.c
CommitLineData
c906108c 1/* Perform non-arithmetic operations on values, for GDB.
990a07ab 2
e2882c85 3 Copyright (C) 1986-2018 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
21#include "symtab.h"
22#include "gdbtypes.h"
23#include "value.h"
24#include "frame.h"
25#include "inferior.h"
26#include "gdbcore.h"
27#include "target.h"
28#include "demangle.h"
29#include "language.h"
30#include "gdbcmd.h"
4e052eda 31#include "regcache.h"
015a42b4 32#include "cp-abi.h"
fe898f56 33#include "block.h"
04714b91 34#include "infcall.h"
de4f826b 35#include "dictionary.h"
b6429628 36#include "cp-support.h"
50637b26 37#include "target-float.h"
e6ca34fc 38#include "tracepoint.h"
76727919 39#include "observable.h"
3e3b026f 40#include "objfiles.h"
233e8b28 41#include "extension.h"
26fcd5d7 42#include "byte-vector.h"
c906108c 43
ccce17b0 44extern unsigned int overload_debug;
c906108c
SS
45/* Local functions. */
46
ad2f7632
DJ
47static int typecmp (int staticp, int varargs, int nargs,
48 struct field t1[], struct value *t2[]);
c906108c 49
714f19d5 50static struct value *search_struct_field (const char *, struct value *,
8a13d42d 51 struct type *, int);
c906108c 52
714f19d5
TT
53static struct value *search_struct_method (const char *, struct value **,
54 struct value **,
6b850546 55 LONGEST, int *, struct type *);
c906108c 56
da096638 57static int find_oload_champ_namespace (struct value **, int,
ac3eeb49
MS
58 const char *, const char *,
59 struct symbol ***,
7322dca9
SW
60 struct badness_vector **,
61 const int no_adl);
8d577d32
DC
62
63static
da096638 64int find_oload_champ_namespace_loop (struct value **, int,
ac3eeb49
MS
65 const char *, const char *,
66 int, struct symbol ***,
7322dca9
SW
67 struct badness_vector **, int *,
68 const int no_adl);
ac3eeb49 69
9cf95373 70static int find_oload_champ (struct value **, int, int,
ba18742c
SM
71 struct fn_field *,
72 const std::vector<xmethod_worker_up> *,
233e8b28 73 struct symbol **, struct badness_vector **);
ac3eeb49 74
2bca57ba 75static int oload_method_static_p (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
ac3eeb49
MS
83static struct value *value_struct_elt_for_reference (struct type *,
84 int, struct type *,
c848d642 85 const char *,
ac3eeb49
MS
86 struct type *,
87 int, enum noside);
79c2c32d 88
ac3eeb49 89static struct value *value_namespace_elt (const struct type *,
c848d642 90 const char *, int , enum noside);
79c2c32d 91
ac3eeb49 92static struct value *value_maybe_namespace_elt (const struct type *,
c848d642 93 const char *, int,
ac3eeb49 94 enum noside);
63d06c5c 95
a14ed312 96static CORE_ADDR allocate_space_in_inferior (int);
c906108c 97
f23631e4 98static struct value *cast_into_complex (struct type *, struct value *);
c906108c 99
233e8b28 100static void find_method_list (struct value **, const char *,
6b850546 101 LONGEST, struct type *, struct fn_field **, int *,
ba18742c 102 std::vector<xmethod_worker_up> *,
6b850546 103 struct type **, LONGEST *);
7a292a7a 104
c906108c 105int overload_resolution = 0;
920d2a44
AC
106static void
107show_overload_resolution (struct ui_file *file, int from_tty,
ac3eeb49
MS
108 struct cmd_list_element *c,
109 const char *value)
920d2a44 110{
3e43a32a
MS
111 fprintf_filtered (file, _("Overload resolution in evaluating "
112 "C++ functions is %s.\n"),
920d2a44
AC
113 value);
114}
242bfc55 115
3e3b026f
UW
116/* Find the address of function name NAME in the inferior. If OBJF_P
117 is non-NULL, *OBJF_P will be set to the OBJFILE where the function
118 is defined. */
c906108c 119
f23631e4 120struct value *
3e3b026f 121find_function_in_inferior (const char *name, struct objfile **objf_p)
c906108c 122{
d12307c1 123 struct block_symbol sym;
a109c7c1 124
2570f2b7 125 sym = lookup_symbol (name, 0, VAR_DOMAIN, 0);
d12307c1 126 if (sym.symbol != NULL)
c906108c 127 {
d12307c1 128 if (SYMBOL_CLASS (sym.symbol) != LOC_BLOCK)
c906108c 129 {
8a3fe4f8 130 error (_("\"%s\" exists in this program but is not a function."),
c906108c
SS
131 name);
132 }
3e3b026f
UW
133
134 if (objf_p)
d12307c1 135 *objf_p = symbol_objfile (sym.symbol);
3e3b026f 136
d12307c1 137 return value_of_variable (sym.symbol, sym.block);
c906108c
SS
138 }
139 else
140 {
7c7b6655
TT
141 struct bound_minimal_symbol msymbol =
142 lookup_bound_minimal_symbol (name);
a109c7c1 143
7c7b6655 144 if (msymbol.minsym != NULL)
c906108c 145 {
7c7b6655 146 struct objfile *objfile = msymbol.objfile;
3e3b026f
UW
147 struct gdbarch *gdbarch = get_objfile_arch (objfile);
148
c906108c 149 struct type *type;
4478b372 150 CORE_ADDR maddr;
3e3b026f 151 type = lookup_pointer_type (builtin_type (gdbarch)->builtin_char);
c906108c
SS
152 type = lookup_function_type (type);
153 type = lookup_pointer_type (type);
77e371c0 154 maddr = BMSYMBOL_VALUE_ADDRESS (msymbol);
3e3b026f
UW
155
156 if (objf_p)
157 *objf_p = objfile;
158
4478b372 159 return value_from_pointer (type, maddr);
c906108c
SS
160 }
161 else
162 {
c5aa993b 163 if (!target_has_execution)
3e43a32a
MS
164 error (_("evaluation of this expression "
165 "requires the target program to be active"));
c5aa993b 166 else
3e43a32a
MS
167 error (_("evaluation of this expression requires the "
168 "program to have a function \"%s\"."),
169 name);
c906108c
SS
170 }
171 }
172}
173
ac3eeb49
MS
174/* Allocate NBYTES of space in the inferior using the inferior's
175 malloc and return a value that is a pointer to the allocated
176 space. */
c906108c 177
f23631e4 178struct value *
fba45db2 179value_allocate_space_in_inferior (int len)
c906108c 180{
3e3b026f
UW
181 struct objfile *objf;
182 struct value *val = find_function_in_inferior ("malloc", &objf);
183 struct gdbarch *gdbarch = get_objfile_arch (objf);
f23631e4 184 struct value *blocklen;
c906108c 185
3e3b026f 186 blocklen = value_from_longest (builtin_type (gdbarch)->builtin_int, len);
7022349d 187 val = call_function_by_hand (val, NULL, 1, &blocklen);
c906108c
SS
188 if (value_logical_not (val))
189 {
190 if (!target_has_execution)
3e43a32a
MS
191 error (_("No memory available to program now: "
192 "you need to start the target first"));
c5aa993b 193 else
8a3fe4f8 194 error (_("No memory available to program: call to malloc failed"));
c906108c
SS
195 }
196 return val;
197}
198
199static CORE_ADDR
fba45db2 200allocate_space_in_inferior (int len)
c906108c
SS
201{
202 return value_as_long (value_allocate_space_in_inferior (len));
203}
204
6af87b03
AR
205/* Cast struct value VAL to type TYPE and return as a value.
206 Both type and val must be of TYPE_CODE_STRUCT or TYPE_CODE_UNION
694182d2
DJ
207 for this to work. Typedef to one of the codes is permitted.
208 Returns NULL if the cast is neither an upcast nor a downcast. */
6af87b03
AR
209
210static struct value *
211value_cast_structs (struct type *type, struct value *v2)
212{
213 struct type *t1;
214 struct type *t2;
215 struct value *v;
216
217 gdb_assert (type != NULL && v2 != NULL);
218
219 t1 = check_typedef (type);
220 t2 = check_typedef (value_type (v2));
221
222 /* Check preconditions. */
223 gdb_assert ((TYPE_CODE (t1) == TYPE_CODE_STRUCT
224 || TYPE_CODE (t1) == TYPE_CODE_UNION)
225 && !!"Precondition is that type is of STRUCT or UNION kind.");
226 gdb_assert ((TYPE_CODE (t2) == TYPE_CODE_STRUCT
227 || TYPE_CODE (t2) == TYPE_CODE_UNION)
228 && !!"Precondition is that value is of STRUCT or UNION kind");
229
191ca0a1
CM
230 if (TYPE_NAME (t1) != NULL
231 && TYPE_NAME (t2) != NULL
232 && !strcmp (TYPE_NAME (t1), TYPE_NAME (t2)))
233 return NULL;
234
6af87b03
AR
235 /* Upcasting: look in the type of the source to see if it contains the
236 type of the target as a superclass. If so, we'll need to
237 offset the pointer rather than just change its type. */
238 if (TYPE_NAME (t1) != NULL)
239 {
a737d952 240 v = search_struct_field (TYPE_NAME (t1),
8a13d42d 241 v2, t2, 1);
6af87b03
AR
242 if (v)
243 return v;
244 }
245
246 /* Downcasting: look in the type of the target to see if it contains the
247 type of the source as a superclass. If so, we'll need to
9c3c02fd 248 offset the pointer rather than just change its type. */
6af87b03
AR
249 if (TYPE_NAME (t2) != NULL)
250 {
9c3c02fd 251 /* Try downcasting using the run-time type of the value. */
6b850546
DT
252 int full, using_enc;
253 LONGEST top;
9c3c02fd
TT
254 struct type *real_type;
255
256 real_type = value_rtti_type (v2, &full, &top, &using_enc);
257 if (real_type)
258 {
259 v = value_full_object (v2, real_type, full, top, using_enc);
260 v = value_at_lazy (real_type, value_address (v));
9f1f738a 261 real_type = value_type (v);
9c3c02fd
TT
262
263 /* We might be trying to cast to the outermost enclosing
264 type, in which case search_struct_field won't work. */
265 if (TYPE_NAME (real_type) != NULL
266 && !strcmp (TYPE_NAME (real_type), TYPE_NAME (t1)))
267 return v;
268
a737d952 269 v = search_struct_field (TYPE_NAME (t2), v, real_type, 1);
9c3c02fd
TT
270 if (v)
271 return v;
272 }
273
274 /* Try downcasting using information from the destination type
275 T2. This wouldn't work properly for classes with virtual
276 bases, but those were handled above. */
a737d952 277 v = search_struct_field (TYPE_NAME (t2),
8a13d42d 278 value_zero (t1, not_lval), t1, 1);
6af87b03
AR
279 if (v)
280 {
281 /* Downcasting is possible (t1 is superclass of v2). */
42ae5230 282 CORE_ADDR addr2 = value_address (v2);
a109c7c1 283
42ae5230 284 addr2 -= value_address (v) + value_embedded_offset (v);
6af87b03
AR
285 return value_at (type, addr2);
286 }
287 }
694182d2
DJ
288
289 return NULL;
6af87b03
AR
290}
291
fb933624
DJ
292/* Cast one pointer or reference type to another. Both TYPE and
293 the type of ARG2 should be pointer types, or else both should be
b1af9e97
TT
294 reference types. If SUBCLASS_CHECK is non-zero, this will force a
295 check to see whether TYPE is a superclass of ARG2's type. If
296 SUBCLASS_CHECK is zero, then the subclass check is done only when
297 ARG2 is itself non-zero. Returns the new pointer or reference. */
fb933624
DJ
298
299struct value *
b1af9e97
TT
300value_cast_pointers (struct type *type, struct value *arg2,
301 int subclass_check)
fb933624 302{
d160942f 303 struct type *type1 = check_typedef (type);
fb933624 304 struct type *type2 = check_typedef (value_type (arg2));
d160942f 305 struct type *t1 = check_typedef (TYPE_TARGET_TYPE (type1));
fb933624
DJ
306 struct type *t2 = check_typedef (TYPE_TARGET_TYPE (type2));
307
308 if (TYPE_CODE (t1) == TYPE_CODE_STRUCT
309 && TYPE_CODE (t2) == TYPE_CODE_STRUCT
b1af9e97 310 && (subclass_check || !value_logical_not (arg2)))
fb933624 311 {
6af87b03 312 struct value *v2;
fb933624 313
aa006118 314 if (TYPE_IS_REFERENCE (type2))
6af87b03
AR
315 v2 = coerce_ref (arg2);
316 else
317 v2 = value_ind (arg2);
3e43a32a
MS
318 gdb_assert (TYPE_CODE (check_typedef (value_type (v2)))
319 == TYPE_CODE_STRUCT && !!"Why did coercion fail?");
6af87b03
AR
320 v2 = value_cast_structs (t1, v2);
321 /* At this point we have what we can have, un-dereference if needed. */
322 if (v2)
fb933624 323 {
6af87b03 324 struct value *v = value_addr (v2);
a109c7c1 325
6af87b03
AR
326 deprecated_set_value_type (v, type);
327 return v;
fb933624 328 }
8301c89e 329 }
fb933624
DJ
330
331 /* No superclass found, just change the pointer type. */
0d5de010 332 arg2 = value_copy (arg2);
fb933624 333 deprecated_set_value_type (arg2, type);
4dfea560 334 set_value_enclosing_type (arg2, type);
fb933624
DJ
335 set_value_pointed_to_offset (arg2, 0); /* pai: chk_val */
336 return arg2;
337}
338
c906108c
SS
339/* Cast value ARG2 to type TYPE and return as a value.
340 More general than a C cast: accepts any two types of the same length,
341 and if ARG2 is an lvalue it can be cast into anything at all. */
342/* In C++, casts may change pointer or object representations. */
343
f23631e4
AC
344struct value *
345value_cast (struct type *type, struct value *arg2)
c906108c 346{
52f0bd74
AC
347 enum type_code code1;
348 enum type_code code2;
349 int scalar;
c906108c
SS
350 struct type *type2;
351
352 int convert_to_boolean = 0;
c5aa993b 353
df407dfe 354 if (value_type (arg2) == type)
c906108c
SS
355 return arg2;
356
6af87b03 357 /* Check if we are casting struct reference to struct reference. */
aa006118 358 if (TYPE_IS_REFERENCE (check_typedef (type)))
6af87b03
AR
359 {
360 /* We dereference type; then we recurse and finally
581e13c1 361 we generate value of the given reference. Nothing wrong with
6af87b03
AR
362 that. */
363 struct type *t1 = check_typedef (type);
364 struct type *dereftype = check_typedef (TYPE_TARGET_TYPE (t1));
aa006118 365 struct value *val = value_cast (dereftype, arg2);
a109c7c1 366
a65cfae5 367 return value_ref (val, TYPE_CODE (t1));
6af87b03
AR
368 }
369
aa006118 370 if (TYPE_IS_REFERENCE (check_typedef (value_type (arg2))))
6af87b03
AR
371 /* We deref the value and then do the cast. */
372 return value_cast (type, coerce_ref (arg2));
373
c973d0aa
PA
374 /* Strip typedefs / resolve stubs in order to get at the type's
375 code/length, but remember the original type, to use as the
376 resulting type of the cast, in case it was a typedef. */
377 struct type *to_type = type;
378
f168693b 379 type = check_typedef (type);
c906108c 380 code1 = TYPE_CODE (type);
994b9211 381 arg2 = coerce_ref (arg2);
df407dfe 382 type2 = check_typedef (value_type (arg2));
c906108c 383
fb933624
DJ
384 /* You can't cast to a reference type. See value_cast_pointers
385 instead. */
aa006118 386 gdb_assert (!TYPE_IS_REFERENCE (type));
fb933624 387
ac3eeb49
MS
388 /* A cast to an undetermined-length array_type, such as
389 (TYPE [])OBJECT, is treated like a cast to (TYPE [N])OBJECT,
390 where N is sizeof(OBJECT)/sizeof(TYPE). */
c906108c
SS
391 if (code1 == TYPE_CODE_ARRAY)
392 {
393 struct type *element_type = TYPE_TARGET_TYPE (type);
394 unsigned element_length = TYPE_LENGTH (check_typedef (element_type));
a109c7c1 395
d78df370 396 if (element_length > 0 && TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
c906108c
SS
397 {
398 struct type *range_type = TYPE_INDEX_TYPE (type);
399 int val_length = TYPE_LENGTH (type2);
400 LONGEST low_bound, high_bound, new_length;
a109c7c1 401
c906108c
SS
402 if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
403 low_bound = 0, high_bound = 0;
404 new_length = val_length / element_length;
405 if (val_length % element_length != 0)
3e43a32a
MS
406 warning (_("array element type size does not "
407 "divide object size in cast"));
ac3eeb49
MS
408 /* FIXME-type-allocation: need a way to free this type when
409 we are done with it. */
0c9c3474
SA
410 range_type = create_static_range_type ((struct type *) NULL,
411 TYPE_TARGET_TYPE (range_type),
412 low_bound,
413 new_length + low_bound - 1);
ac3eeb49
MS
414 deprecated_set_value_type (arg2,
415 create_array_type ((struct type *) NULL,
416 element_type,
417 range_type));
c906108c
SS
418 return arg2;
419 }
420 }
421
422 if (current_language->c_style_arrays
3bdf2bbd
KW
423 && TYPE_CODE (type2) == TYPE_CODE_ARRAY
424 && !TYPE_VECTOR (type2))
c906108c
SS
425 arg2 = value_coerce_array (arg2);
426
427 if (TYPE_CODE (type2) == TYPE_CODE_FUNC)
428 arg2 = value_coerce_function (arg2);
429
df407dfe 430 type2 = check_typedef (value_type (arg2));
c906108c
SS
431 code2 = TYPE_CODE (type2);
432
433 if (code1 == TYPE_CODE_COMPLEX)
c973d0aa 434 return cast_into_complex (to_type, arg2);
c906108c
SS
435 if (code1 == TYPE_CODE_BOOL)
436 {
437 code1 = TYPE_CODE_INT;
438 convert_to_boolean = 1;
439 }
440 if (code1 == TYPE_CODE_CHAR)
441 code1 = TYPE_CODE_INT;
442 if (code2 == TYPE_CODE_BOOL || code2 == TYPE_CODE_CHAR)
443 code2 = TYPE_CODE_INT;
444
445 scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
4ef30785
TJB
446 || code2 == TYPE_CODE_DECFLOAT || code2 == TYPE_CODE_ENUM
447 || code2 == TYPE_CODE_RANGE);
c906108c 448
6af87b03
AR
449 if ((code1 == TYPE_CODE_STRUCT || code1 == TYPE_CODE_UNION)
450 && (code2 == TYPE_CODE_STRUCT || code2 == TYPE_CODE_UNION)
c906108c 451 && TYPE_NAME (type) != 0)
694182d2 452 {
c973d0aa 453 struct value *v = value_cast_structs (to_type, arg2);
a109c7c1 454
694182d2
DJ
455 if (v)
456 return v;
457 }
458
50637b26 459 if (is_floating_type (type) && scalar)
4ef30785 460 {
50637b26
UW
461 if (is_floating_value (arg2))
462 {
463 struct value *v = allocate_value (to_type);
464 target_float_convert (value_contents (arg2), type2,
465 value_contents_raw (v), type);
466 return v;
467 }
468
3b4b2f16 469 /* The only option left is an integral type. */
50637b26
UW
470 if (TYPE_UNSIGNED (type2))
471 return value_from_ulongest (to_type, value_as_long (arg2));
4ef30785 472 else
50637b26 473 return value_from_longest (to_type, value_as_long (arg2));
4ef30785 474 }
c906108c
SS
475 else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM
476 || code1 == TYPE_CODE_RANGE)
0d5de010
DJ
477 && (scalar || code2 == TYPE_CODE_PTR
478 || code2 == TYPE_CODE_MEMBERPTR))
c906108c
SS
479 {
480 LONGEST longest;
c5aa993b 481
2bf1f4a1 482 /* When we cast pointers to integers, we mustn't use
76e71323 483 gdbarch_pointer_to_address to find the address the pointer
2bf1f4a1
JB
484 represents, as value_as_long would. GDB should evaluate
485 expressions just as the compiler would --- and the compiler
486 sees a cast as a simple reinterpretation of the pointer's
487 bits. */
488 if (code2 == TYPE_CODE_PTR)
e17a4113
UW
489 longest = extract_unsigned_integer
490 (value_contents (arg2), TYPE_LENGTH (type2),
491 gdbarch_byte_order (get_type_arch (type2)));
2bf1f4a1
JB
492 else
493 longest = value_as_long (arg2);
c973d0aa 494 return value_from_longest (to_type, convert_to_boolean ?
716c501e 495 (LONGEST) (longest ? 1 : 0) : longest);
c906108c 496 }
ac3eeb49
MS
497 else if (code1 == TYPE_CODE_PTR && (code2 == TYPE_CODE_INT
498 || code2 == TYPE_CODE_ENUM
499 || code2 == TYPE_CODE_RANGE))
634acd5f 500 {
4603e466
DT
501 /* TYPE_LENGTH (type) is the length of a pointer, but we really
502 want the length of an address! -- we are really dealing with
503 addresses (i.e., gdb representations) not pointers (i.e.,
504 target representations) here.
505
506 This allows things like "print *(int *)0x01000234" to work
507 without printing a misleading message -- which would
508 otherwise occur when dealing with a target having two byte
509 pointers and four byte addresses. */
510
50810684 511 int addr_bit = gdbarch_addr_bit (get_type_arch (type2));
634acd5f 512 LONGEST longest = value_as_long (arg2);
a109c7c1 513
4603e466 514 if (addr_bit < sizeof (LONGEST) * HOST_CHAR_BIT)
634acd5f 515 {
4603e466
DT
516 if (longest >= ((LONGEST) 1 << addr_bit)
517 || longest <= -((LONGEST) 1 << addr_bit))
8a3fe4f8 518 warning (_("value truncated"));
634acd5f 519 }
c973d0aa 520 return value_from_longest (to_type, longest);
634acd5f 521 }
0d5de010
DJ
522 else if (code1 == TYPE_CODE_METHODPTR && code2 == TYPE_CODE_INT
523 && value_as_long (arg2) == 0)
524 {
c973d0aa 525 struct value *result = allocate_value (to_type);
a109c7c1 526
c973d0aa 527 cplus_make_method_ptr (to_type, value_contents_writeable (result), 0, 0);
0d5de010
DJ
528 return result;
529 }
530 else if (code1 == TYPE_CODE_MEMBERPTR && code2 == TYPE_CODE_INT
531 && value_as_long (arg2) == 0)
532 {
533 /* The Itanium C++ ABI represents NULL pointers to members as
534 minus one, instead of biasing the normal case. */
c973d0aa 535 return value_from_longest (to_type, -1);
0d5de010 536 }
8954db33
AB
537 else if (code1 == TYPE_CODE_ARRAY && TYPE_VECTOR (type)
538 && code2 == TYPE_CODE_ARRAY && TYPE_VECTOR (type2)
539 && TYPE_LENGTH (type) != TYPE_LENGTH (type2))
540 error (_("Cannot convert between vector values of different sizes"));
541 else if (code1 == TYPE_CODE_ARRAY && TYPE_VECTOR (type) && scalar
542 && TYPE_LENGTH (type) != TYPE_LENGTH (type2))
543 error (_("can only cast scalar to vector of same size"));
0ba2eb0f
TT
544 else if (code1 == TYPE_CODE_VOID)
545 {
c973d0aa 546 return value_zero (to_type, not_lval);
0ba2eb0f 547 }
c906108c
SS
548 else if (TYPE_LENGTH (type) == TYPE_LENGTH (type2))
549 {
550 if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
c973d0aa 551 return value_cast_pointers (to_type, arg2, 0);
fb933624 552
0d5de010 553 arg2 = value_copy (arg2);
c973d0aa
PA
554 deprecated_set_value_type (arg2, to_type);
555 set_value_enclosing_type (arg2, to_type);
b44d461b 556 set_value_pointed_to_offset (arg2, 0); /* pai: chk_val */
c906108c
SS
557 return arg2;
558 }
c906108c 559 else if (VALUE_LVAL (arg2) == lval_memory)
c973d0aa 560 return value_at_lazy (to_type, value_address (arg2));
c906108c
SS
561 else
562 {
8a3fe4f8 563 error (_("Invalid cast."));
c906108c
SS
564 return 0;
565 }
566}
567
4e8f195d
TT
568/* The C++ reinterpret_cast operator. */
569
570struct value *
571value_reinterpret_cast (struct type *type, struct value *arg)
572{
573 struct value *result;
574 struct type *real_type = check_typedef (type);
575 struct type *arg_type, *dest_type;
576 int is_ref = 0;
577 enum type_code dest_code, arg_code;
578
579 /* Do reference, function, and array conversion. */
580 arg = coerce_array (arg);
581
582 /* Attempt to preserve the type the user asked for. */
583 dest_type = type;
584
585 /* If we are casting to a reference type, transform
aa006118
AV
586 reinterpret_cast<T&[&]>(V) to *reinterpret_cast<T*>(&V). */
587 if (TYPE_IS_REFERENCE (real_type))
4e8f195d
TT
588 {
589 is_ref = 1;
590 arg = value_addr (arg);
591 dest_type = lookup_pointer_type (TYPE_TARGET_TYPE (dest_type));
592 real_type = lookup_pointer_type (real_type);
593 }
594
595 arg_type = value_type (arg);
596
597 dest_code = TYPE_CODE (real_type);
598 arg_code = TYPE_CODE (arg_type);
599
600 /* We can convert pointer types, or any pointer type to int, or int
601 type to pointer. */
602 if ((dest_code == TYPE_CODE_PTR && arg_code == TYPE_CODE_INT)
603 || (dest_code == TYPE_CODE_INT && arg_code == TYPE_CODE_PTR)
604 || (dest_code == TYPE_CODE_METHODPTR && arg_code == TYPE_CODE_INT)
605 || (dest_code == TYPE_CODE_INT && arg_code == TYPE_CODE_METHODPTR)
606 || (dest_code == TYPE_CODE_MEMBERPTR && arg_code == TYPE_CODE_INT)
607 || (dest_code == TYPE_CODE_INT && arg_code == TYPE_CODE_MEMBERPTR)
608 || (dest_code == arg_code
609 && (dest_code == TYPE_CODE_PTR
610 || dest_code == TYPE_CODE_METHODPTR
611 || dest_code == TYPE_CODE_MEMBERPTR)))
612 result = value_cast (dest_type, arg);
613 else
614 error (_("Invalid reinterpret_cast"));
615
616 if (is_ref)
a65cfae5
AV
617 result = value_cast (type, value_ref (value_ind (result),
618 TYPE_CODE (type)));
4e8f195d
TT
619
620 return result;
621}
622
623/* A helper for value_dynamic_cast. This implements the first of two
624 runtime checks: we iterate over all the base classes of the value's
625 class which are equal to the desired class; if only one of these
626 holds the value, then it is the answer. */
627
628static int
629dynamic_cast_check_1 (struct type *desired_type,
8af8e3bc 630 const gdb_byte *valaddr,
6b850546 631 LONGEST embedded_offset,
4e8f195d 632 CORE_ADDR address,
8af8e3bc 633 struct value *val,
4e8f195d
TT
634 struct type *search_type,
635 CORE_ADDR arg_addr,
636 struct type *arg_type,
637 struct value **result)
638{
639 int i, result_count = 0;
640
641 for (i = 0; i < TYPE_N_BASECLASSES (search_type) && result_count < 2; ++i)
642 {
6b850546
DT
643 LONGEST offset = baseclass_offset (search_type, i, valaddr,
644 embedded_offset,
645 address, val);
a109c7c1 646
4e8f195d
TT
647 if (class_types_same_p (desired_type, TYPE_BASECLASS (search_type, i)))
648 {
8af8e3bc
PA
649 if (address + embedded_offset + offset >= arg_addr
650 && address + embedded_offset + offset < arg_addr + TYPE_LENGTH (arg_type))
4e8f195d
TT
651 {
652 ++result_count;
653 if (!*result)
654 *result = value_at_lazy (TYPE_BASECLASS (search_type, i),
8af8e3bc 655 address + embedded_offset + offset);
4e8f195d
TT
656 }
657 }
658 else
659 result_count += dynamic_cast_check_1 (desired_type,
8af8e3bc
PA
660 valaddr,
661 embedded_offset + offset,
662 address, val,
4e8f195d
TT
663 TYPE_BASECLASS (search_type, i),
664 arg_addr,
665 arg_type,
666 result);
667 }
668
669 return result_count;
670}
671
672/* A helper for value_dynamic_cast. This implements the second of two
673 runtime checks: we look for a unique public sibling class of the
674 argument's declared class. */
675
676static int
677dynamic_cast_check_2 (struct type *desired_type,
8af8e3bc 678 const gdb_byte *valaddr,
6b850546 679 LONGEST embedded_offset,
4e8f195d 680 CORE_ADDR address,
8af8e3bc 681 struct value *val,
4e8f195d
TT
682 struct type *search_type,
683 struct value **result)
684{
685 int i, result_count = 0;
686
687 for (i = 0; i < TYPE_N_BASECLASSES (search_type) && result_count < 2; ++i)
688 {
6b850546 689 LONGEST offset;
4e8f195d
TT
690
691 if (! BASETYPE_VIA_PUBLIC (search_type, i))
692 continue;
693
8af8e3bc
PA
694 offset = baseclass_offset (search_type, i, valaddr, embedded_offset,
695 address, val);
4e8f195d
TT
696 if (class_types_same_p (desired_type, TYPE_BASECLASS (search_type, i)))
697 {
698 ++result_count;
699 if (*result == NULL)
700 *result = value_at_lazy (TYPE_BASECLASS (search_type, i),
8af8e3bc 701 address + embedded_offset + offset);
4e8f195d
TT
702 }
703 else
704 result_count += dynamic_cast_check_2 (desired_type,
8af8e3bc
PA
705 valaddr,
706 embedded_offset + offset,
707 address, val,
4e8f195d
TT
708 TYPE_BASECLASS (search_type, i),
709 result);
710 }
711
712 return result_count;
713}
714
715/* The C++ dynamic_cast operator. */
716
717struct value *
718value_dynamic_cast (struct type *type, struct value *arg)
719{
6b850546
DT
720 int full, using_enc;
721 LONGEST top;
4e8f195d
TT
722 struct type *resolved_type = check_typedef (type);
723 struct type *arg_type = check_typedef (value_type (arg));
724 struct type *class_type, *rtti_type;
725 struct value *result, *tem, *original_arg = arg;
726 CORE_ADDR addr;
aa006118 727 int is_ref = TYPE_IS_REFERENCE (resolved_type);
4e8f195d
TT
728
729 if (TYPE_CODE (resolved_type) != TYPE_CODE_PTR
aa006118 730 && !TYPE_IS_REFERENCE (resolved_type))
4e8f195d
TT
731 error (_("Argument to dynamic_cast must be a pointer or reference type"));
732 if (TYPE_CODE (TYPE_TARGET_TYPE (resolved_type)) != TYPE_CODE_VOID
4753d33b 733 && TYPE_CODE (TYPE_TARGET_TYPE (resolved_type)) != TYPE_CODE_STRUCT)
4e8f195d
TT
734 error (_("Argument to dynamic_cast must be pointer to class or `void *'"));
735
736 class_type = check_typedef (TYPE_TARGET_TYPE (resolved_type));
737 if (TYPE_CODE (resolved_type) == TYPE_CODE_PTR)
738 {
739 if (TYPE_CODE (arg_type) != TYPE_CODE_PTR
740 && ! (TYPE_CODE (arg_type) == TYPE_CODE_INT
741 && value_as_long (arg) == 0))
742 error (_("Argument to dynamic_cast does not have pointer type"));
743 if (TYPE_CODE (arg_type) == TYPE_CODE_PTR)
744 {
745 arg_type = check_typedef (TYPE_TARGET_TYPE (arg_type));
4753d33b 746 if (TYPE_CODE (arg_type) != TYPE_CODE_STRUCT)
3e43a32a
MS
747 error (_("Argument to dynamic_cast does "
748 "not have pointer to class type"));
4e8f195d
TT
749 }
750
751 /* Handle NULL pointers. */
752 if (value_as_long (arg) == 0)
753 return value_zero (type, not_lval);
754
755 arg = value_ind (arg);
756 }
757 else
758 {
4753d33b 759 if (TYPE_CODE (arg_type) != TYPE_CODE_STRUCT)
4e8f195d
TT
760 error (_("Argument to dynamic_cast does not have class type"));
761 }
762
763 /* If the classes are the same, just return the argument. */
764 if (class_types_same_p (class_type, arg_type))
765 return value_cast (type, arg);
766
767 /* If the target type is a unique base class of the argument's
768 declared type, just cast it. */
769 if (is_ancestor (class_type, arg_type))
770 {
771 if (is_unique_ancestor (class_type, arg))
772 return value_cast (type, original_arg);
773 error (_("Ambiguous dynamic_cast"));
774 }
775
776 rtti_type = value_rtti_type (arg, &full, &top, &using_enc);
777 if (! rtti_type)
778 error (_("Couldn't determine value's most derived type for dynamic_cast"));
779
780 /* Compute the most derived object's address. */
781 addr = value_address (arg);
782 if (full)
783 {
784 /* Done. */
785 }
786 else if (using_enc)
787 addr += top;
788 else
789 addr += top + value_embedded_offset (arg);
790
791 /* dynamic_cast<void *> means to return a pointer to the
792 most-derived object. */
793 if (TYPE_CODE (resolved_type) == TYPE_CODE_PTR
794 && TYPE_CODE (TYPE_TARGET_TYPE (resolved_type)) == TYPE_CODE_VOID)
795 return value_at_lazy (type, addr);
796
797 tem = value_at (type, addr);
9f1f738a 798 type = value_type (tem);
4e8f195d
TT
799
800 /* The first dynamic check specified in 5.2.7. */
801 if (is_public_ancestor (arg_type, TYPE_TARGET_TYPE (resolved_type)))
802 {
803 if (class_types_same_p (rtti_type, TYPE_TARGET_TYPE (resolved_type)))
804 return tem;
805 result = NULL;
806 if (dynamic_cast_check_1 (TYPE_TARGET_TYPE (resolved_type),
8af8e3bc
PA
807 value_contents_for_printing (tem),
808 value_embedded_offset (tem),
809 value_address (tem), tem,
4e8f195d
TT
810 rtti_type, addr,
811 arg_type,
812 &result) == 1)
813 return value_cast (type,
a65cfae5
AV
814 is_ref
815 ? value_ref (result, TYPE_CODE (resolved_type))
816 : value_addr (result));
4e8f195d
TT
817 }
818
819 /* The second dynamic check specified in 5.2.7. */
820 result = NULL;
821 if (is_public_ancestor (arg_type, rtti_type)
822 && dynamic_cast_check_2 (TYPE_TARGET_TYPE (resolved_type),
8af8e3bc
PA
823 value_contents_for_printing (tem),
824 value_embedded_offset (tem),
825 value_address (tem), tem,
4e8f195d
TT
826 rtti_type, &result) == 1)
827 return value_cast (type,
a65cfae5
AV
828 is_ref
829 ? value_ref (result, TYPE_CODE (resolved_type))
830 : value_addr (result));
4e8f195d
TT
831
832 if (TYPE_CODE (resolved_type) == TYPE_CODE_PTR)
833 return value_zero (type, not_lval);
834
835 error (_("dynamic_cast failed"));
836}
837
c906108c
SS
838/* Create a value of type TYPE that is zero, and return it. */
839
f23631e4 840struct value *
fba45db2 841value_zero (struct type *type, enum lval_type lv)
c906108c 842{
f23631e4 843 struct value *val = allocate_value (type);
c906108c 844
bb7da2bf 845 VALUE_LVAL (val) = (lv == lval_computed ? not_lval : lv);
c906108c
SS
846 return val;
847}
848
18a46dbe 849/* Create a not_lval value of numeric type TYPE that is one, and return it. */
301f0ecf
DE
850
851struct value *
18a46dbe 852value_one (struct type *type)
301f0ecf
DE
853{
854 struct type *type1 = check_typedef (type);
4e608b4f 855 struct value *val;
301f0ecf 856
50637b26 857 if (is_integral_type (type1) || is_floating_type (type1))
301f0ecf
DE
858 {
859 val = value_from_longest (type, (LONGEST) 1);
860 }
120bd360
KW
861 else if (TYPE_CODE (type1) == TYPE_CODE_ARRAY && TYPE_VECTOR (type1))
862 {
863 struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type1));
cfa6f054
KW
864 int i;
865 LONGEST low_bound, high_bound;
120bd360
KW
866 struct value *tmp;
867
cfa6f054
KW
868 if (!get_array_bounds (type1, &low_bound, &high_bound))
869 error (_("Could not determine the vector bounds"));
870
120bd360 871 val = allocate_value (type);
cfa6f054 872 for (i = 0; i < high_bound - low_bound + 1; i++)
120bd360 873 {
18a46dbe 874 tmp = value_one (eltype);
120bd360
KW
875 memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype),
876 value_contents_all (tmp), TYPE_LENGTH (eltype));
877 }
878 }
301f0ecf
DE
879 else
880 {
881 error (_("Not a numeric type."));
882 }
883
18a46dbe
JK
884 /* value_one result is never used for assignments to. */
885 gdb_assert (VALUE_LVAL (val) == not_lval);
886
301f0ecf
DE
887 return val;
888}
889
80180f79
SA
890/* Helper function for value_at, value_at_lazy, and value_at_lazy_stack.
891 The type of the created value may differ from the passed type TYPE.
892 Make sure to retrieve the returned values's new type after this call
893 e.g. in case the type is a variable length array. */
4e5d721f
DE
894
895static struct value *
896get_value_at (struct type *type, CORE_ADDR addr, int lazy)
897{
898 struct value *val;
899
900 if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
901 error (_("Attempt to dereference a generic pointer."));
902
a3d34bf4 903 val = value_from_contents_and_address (type, NULL, addr);
4e5d721f 904
a3d34bf4
PA
905 if (!lazy)
906 value_fetch_lazy (val);
4e5d721f
DE
907
908 return val;
909}
910
070ad9f0 911/* Return a value with type TYPE located at ADDR.
c906108c
SS
912
913 Call value_at only if the data needs to be fetched immediately;
914 if we can be 'lazy' and defer the fetch, perhaps indefinately, call
915 value_at_lazy instead. value_at_lazy simply records the address of
070ad9f0 916 the data and sets the lazy-evaluation-required flag. The lazy flag
0fd88904 917 is tested in the value_contents macro, which is used if and when
80180f79
SA
918 the contents are actually required. The type of the created value
919 may differ from the passed type TYPE. Make sure to retrieve the
920 returned values's new type after this call e.g. in case the type
921 is a variable length array.
c906108c
SS
922
923 Note: value_at does *NOT* handle embedded offsets; perform such
ac3eeb49 924 adjustments before or after calling it. */
c906108c 925
f23631e4 926struct value *
00a4c844 927value_at (struct type *type, CORE_ADDR addr)
c906108c 928{
4e5d721f 929 return get_value_at (type, addr, 0);
c906108c
SS
930}
931
80180f79
SA
932/* Return a lazy value with type TYPE located at ADDR (cf. value_at).
933 The type of the created value may differ from the passed type TYPE.
934 Make sure to retrieve the returned values's new type after this call
935 e.g. in case the type is a variable length array. */
c906108c 936
f23631e4 937struct value *
00a4c844 938value_at_lazy (struct type *type, CORE_ADDR addr)
c906108c 939{
4e5d721f 940 return get_value_at (type, addr, 1);
c906108c
SS
941}
942
e6ca34fc 943void
23f945bf 944read_value_memory (struct value *val, LONGEST bit_offset,
e6ca34fc
PA
945 int stack, CORE_ADDR memaddr,
946 gdb_byte *buffer, size_t length)
947{
3ae385af
SM
948 ULONGEST xfered_total = 0;
949 struct gdbarch *arch = get_value_arch (val);
950 int unit_size = gdbarch_addressable_memory_unit_size (arch);
6d7e9d3b
YQ
951 enum target_object object;
952
953 object = stack ? TARGET_OBJECT_STACK_MEMORY : TARGET_OBJECT_MEMORY;
5a2eb0ef 954
3ae385af 955 while (xfered_total < length)
5a2eb0ef
YQ
956 {
957 enum target_xfer_status status;
3ae385af 958 ULONGEST xfered_partial;
5a2eb0ef 959
8b88a78e 960 status = target_xfer_partial (current_top_target (),
6d7e9d3b 961 object, NULL,
3ae385af
SM
962 buffer + xfered_total * unit_size, NULL,
963 memaddr + xfered_total,
964 length - xfered_total,
965 &xfered_partial);
5a2eb0ef
YQ
966
967 if (status == TARGET_XFER_OK)
968 /* nothing */;
bc113b4e 969 else if (status == TARGET_XFER_UNAVAILABLE)
23f945bf
AA
970 mark_value_bits_unavailable (val, (xfered_total * HOST_CHAR_BIT
971 + bit_offset),
972 xfered_partial * HOST_CHAR_BIT);
5a2eb0ef 973 else if (status == TARGET_XFER_EOF)
3ae385af 974 memory_error (TARGET_XFER_E_IO, memaddr + xfered_total);
e6ca34fc 975 else
3ae385af 976 memory_error (status, memaddr + xfered_total);
e6ca34fc 977
3ae385af 978 xfered_total += xfered_partial;
5a2eb0ef 979 QUIT;
e6ca34fc
PA
980 }
981}
c906108c
SS
982
983/* Store the contents of FROMVAL into the location of TOVAL.
984 Return a new value with the location of TOVAL and contents of FROMVAL. */
985
f23631e4
AC
986struct value *
987value_assign (struct value *toval, struct value *fromval)
c906108c 988{
52f0bd74 989 struct type *type;
f23631e4 990 struct value *val;
cb741690 991 struct frame_id old_frame;
c906108c 992
88e3b34b 993 if (!deprecated_value_modifiable (toval))
8a3fe4f8 994 error (_("Left operand of assignment is not a modifiable lvalue."));
c906108c 995
994b9211 996 toval = coerce_ref (toval);
c906108c 997
df407dfe 998 type = value_type (toval);
c906108c 999 if (VALUE_LVAL (toval) != lval_internalvar)
3cbaedff 1000 fromval = value_cast (type, fromval);
c906108c 1001 else
63092375
DJ
1002 {
1003 /* Coerce arrays and functions to pointers, except for arrays
1004 which only live in GDB's storage. */
1005 if (!value_must_coerce_to_target (fromval))
1006 fromval = coerce_array (fromval);
1007 }
1008
f168693b 1009 type = check_typedef (type);
c906108c 1010
ac3eeb49
MS
1011 /* Since modifying a register can trash the frame chain, and
1012 modifying memory can trash the frame cache, we save the old frame
1013 and then restore the new frame afterwards. */
206415a3 1014 old_frame = get_frame_id (deprecated_safe_get_selected_frame ());
cb741690 1015
c906108c
SS
1016 switch (VALUE_LVAL (toval))
1017 {
1018 case lval_internalvar:
1019 set_internalvar (VALUE_INTERNALVAR (toval), fromval);
4aac0db7
UW
1020 return value_of_internalvar (get_type_arch (type),
1021 VALUE_INTERNALVAR (toval));
c906108c
SS
1022
1023 case lval_internalvar_component:
d9e98382 1024 {
6b850546 1025 LONGEST offset = value_offset (toval);
d9e98382
SDJ
1026
1027 /* Are we dealing with a bitfield?
1028
1029 It is important to mention that `value_parent (toval)' is
1030 non-NULL iff `value_bitsize (toval)' is non-zero. */
1031 if (value_bitsize (toval))
1032 {
1033 /* VALUE_INTERNALVAR below refers to the parent value, while
1034 the offset is relative to this parent value. */
1035 gdb_assert (value_parent (value_parent (toval)) == NULL);
1036 offset += value_offset (value_parent (toval));
1037 }
1038
1039 set_internalvar_component (VALUE_INTERNALVAR (toval),
1040 offset,
1041 value_bitpos (toval),
1042 value_bitsize (toval),
1043 fromval);
1044 }
c906108c
SS
1045 break;
1046
1047 case lval_memory:
1048 {
fc1a4b47 1049 const gdb_byte *dest_buffer;
c5aa993b
JM
1050 CORE_ADDR changed_addr;
1051 int changed_len;
10c42a71 1052 gdb_byte buffer[sizeof (LONGEST)];
c906108c 1053
df407dfe 1054 if (value_bitsize (toval))
c5aa993b 1055 {
2d88202a 1056 struct value *parent = value_parent (toval);
2d88202a 1057
a109c7c1 1058 changed_addr = value_address (parent) + value_offset (toval);
df407dfe
AC
1059 changed_len = (value_bitpos (toval)
1060 + value_bitsize (toval)
c5aa993b
JM
1061 + HOST_CHAR_BIT - 1)
1062 / HOST_CHAR_BIT;
c906108c 1063
4ea48cc1
DJ
1064 /* If we can read-modify-write exactly the size of the
1065 containing type (e.g. short or int) then do so. This
1066 is safer for volatile bitfields mapped to hardware
1067 registers. */
1068 if (changed_len < TYPE_LENGTH (type)
1069 && TYPE_LENGTH (type) <= (int) sizeof (LONGEST)
2d88202a 1070 && ((LONGEST) changed_addr % TYPE_LENGTH (type)) == 0)
4ea48cc1
DJ
1071 changed_len = TYPE_LENGTH (type);
1072
c906108c 1073 if (changed_len > (int) sizeof (LONGEST))
3e43a32a
MS
1074 error (_("Can't handle bitfields which "
1075 "don't fit in a %d bit word."),
baa6f10b 1076 (int) sizeof (LONGEST) * HOST_CHAR_BIT);
c906108c 1077
2d88202a 1078 read_memory (changed_addr, buffer, changed_len);
50810684 1079 modify_field (type, buffer, value_as_long (fromval),
df407dfe 1080 value_bitpos (toval), value_bitsize (toval));
c906108c
SS
1081 dest_buffer = buffer;
1082 }
c906108c
SS
1083 else
1084 {
42ae5230 1085 changed_addr = value_address (toval);
3ae385af 1086 changed_len = type_length_units (type);
0fd88904 1087 dest_buffer = value_contents (fromval);
c906108c
SS
1088 }
1089
972daa01 1090 write_memory_with_notification (changed_addr, dest_buffer, changed_len);
c906108c
SS
1091 }
1092 break;
1093
492254e9 1094 case lval_register:
c906108c 1095 {
c906108c 1096 struct frame_info *frame;
d80b854b 1097 struct gdbarch *gdbarch;
ff2e87ac 1098 int value_reg;
c906108c 1099
41b56feb
KB
1100 /* Figure out which frame this is in currently.
1101
1102 We use VALUE_FRAME_ID for obtaining the value's frame id instead of
1103 VALUE_NEXT_FRAME_ID due to requiring a frame which may be passed to
1104 put_frame_register_bytes() below. That function will (eventually)
1105 perform the necessary unwind operation by first obtaining the next
1106 frame. */
0c16dd26 1107 frame = frame_find_by_id (VALUE_FRAME_ID (toval));
41b56feb 1108
0c16dd26 1109 value_reg = VALUE_REGNUM (toval);
c906108c
SS
1110
1111 if (!frame)
8a3fe4f8 1112 error (_("Value being assigned to is no longer active."));
d80b854b
UW
1113
1114 gdbarch = get_frame_arch (frame);
3e871532
LM
1115
1116 if (value_bitsize (toval))
492254e9 1117 {
3e871532 1118 struct value *parent = value_parent (toval);
6b850546 1119 LONGEST offset = value_offset (parent) + value_offset (toval);
3e871532
LM
1120 int changed_len;
1121 gdb_byte buffer[sizeof (LONGEST)];
1122 int optim, unavail;
1123
1124 changed_len = (value_bitpos (toval)
1125 + value_bitsize (toval)
1126 + HOST_CHAR_BIT - 1)
1127 / HOST_CHAR_BIT;
1128
1129 if (changed_len > (int) sizeof (LONGEST))
1130 error (_("Can't handle bitfields which "
1131 "don't fit in a %d bit word."),
1132 (int) sizeof (LONGEST) * HOST_CHAR_BIT);
1133
1134 if (!get_frame_register_bytes (frame, value_reg, offset,
1135 changed_len, buffer,
1136 &optim, &unavail))
1137 {
1138 if (optim)
1139 throw_error (OPTIMIZED_OUT_ERROR,
1140 _("value has been optimized out"));
1141 if (unavail)
1142 throw_error (NOT_AVAILABLE_ERROR,
1143 _("value is not available"));
1144 }
1145
1146 modify_field (type, buffer, value_as_long (fromval),
1147 value_bitpos (toval), value_bitsize (toval));
1148
1149 put_frame_register_bytes (frame, value_reg, offset,
1150 changed_len, buffer);
492254e9 1151 }
c906108c 1152 else
492254e9 1153 {
3e871532
LM
1154 if (gdbarch_convert_register_p (gdbarch, VALUE_REGNUM (toval),
1155 type))
00fa51f6 1156 {
3e871532
LM
1157 /* If TOVAL is a special machine register requiring
1158 conversion of program values to a special raw
1159 format. */
1160 gdbarch_value_to_register (gdbarch, frame,
1161 VALUE_REGNUM (toval), type,
1162 value_contents (fromval));
00fa51f6 1163 }
c906108c 1164 else
00fa51f6
UW
1165 {
1166 put_frame_register_bytes (frame, value_reg,
1167 value_offset (toval),
1168 TYPE_LENGTH (type),
1169 value_contents (fromval));
1170 }
ff2e87ac 1171 }
00fa51f6 1172
76727919 1173 gdb::observers::register_changed.notify (frame, value_reg);
ff2e87ac 1174 break;
c906108c 1175 }
5f5233d4
PA
1176
1177 case lval_computed:
1178 {
c8f2448a 1179 const struct lval_funcs *funcs = value_computed_funcs (toval);
5f5233d4 1180
ac71a68c
JK
1181 if (funcs->write != NULL)
1182 {
1183 funcs->write (toval, fromval);
1184 break;
1185 }
5f5233d4 1186 }
ac71a68c 1187 /* Fall through. */
5f5233d4 1188
c906108c 1189 default:
8a3fe4f8 1190 error (_("Left operand of assignment is not an lvalue."));
c906108c
SS
1191 }
1192
cb741690
DJ
1193 /* Assigning to the stack pointer, frame pointer, and other
1194 (architecture and calling convention specific) registers may
d649a38e 1195 cause the frame cache and regcache to be out of date. Assigning to memory
cb741690
DJ
1196 also can. We just do this on all assignments to registers or
1197 memory, for simplicity's sake; I doubt the slowdown matters. */
1198 switch (VALUE_LVAL (toval))
1199 {
1200 case lval_memory:
1201 case lval_register:
0e03807e 1202 case lval_computed:
cb741690 1203
8b88a78e 1204 gdb::observers::target_changed.notify (current_top_target ());
cb741690 1205
ac3eeb49
MS
1206 /* Having destroyed the frame cache, restore the selected
1207 frame. */
cb741690
DJ
1208
1209 /* FIXME: cagney/2002-11-02: There has to be a better way of
1210 doing this. Instead of constantly saving/restoring the
1211 frame. Why not create a get_selected_frame() function that,
1212 having saved the selected frame's ID can automatically
1213 re-find the previously selected frame automatically. */
1214
1215 {
1216 struct frame_info *fi = frame_find_by_id (old_frame);
a109c7c1 1217
cb741690
DJ
1218 if (fi != NULL)
1219 select_frame (fi);
1220 }
1221
1222 break;
1223 default:
1224 break;
1225 }
1226
ac3eeb49
MS
1227 /* If the field does not entirely fill a LONGEST, then zero the sign
1228 bits. If the field is signed, and is negative, then sign
1229 extend. */
df407dfe
AC
1230 if ((value_bitsize (toval) > 0)
1231 && (value_bitsize (toval) < 8 * (int) sizeof (LONGEST)))
c906108c
SS
1232 {
1233 LONGEST fieldval = value_as_long (fromval);
df407dfe 1234 LONGEST valmask = (((ULONGEST) 1) << value_bitsize (toval)) - 1;
c906108c
SS
1235
1236 fieldval &= valmask;
ac3eeb49
MS
1237 if (!TYPE_UNSIGNED (type)
1238 && (fieldval & (valmask ^ (valmask >> 1))))
c906108c
SS
1239 fieldval |= ~valmask;
1240
1241 fromval = value_from_longest (type, fieldval);
1242 }
1243
4aac0db7
UW
1244 /* The return value is a copy of TOVAL so it shares its location
1245 information, but its contents are updated from FROMVAL. This
1246 implies the returned value is not lazy, even if TOVAL was. */
c906108c 1247 val = value_copy (toval);
4aac0db7 1248 set_value_lazy (val, 0);
0fd88904 1249 memcpy (value_contents_raw (val), value_contents (fromval),
c906108c 1250 TYPE_LENGTH (type));
4aac0db7
UW
1251
1252 /* We copy over the enclosing type and pointed-to offset from FROMVAL
1253 in the case of pointer types. For object types, the enclosing type
1254 and embedded offset must *not* be copied: the target object refered
1255 to by TOVAL retains its original dynamic type after assignment. */
1256 if (TYPE_CODE (type) == TYPE_CODE_PTR)
1257 {
1258 set_value_enclosing_type (val, value_enclosing_type (fromval));
1259 set_value_pointed_to_offset (val, value_pointed_to_offset (fromval));
1260 }
c5aa993b 1261
c906108c
SS
1262 return val;
1263}
1264
1265/* Extend a value VAL to COUNT repetitions of its type. */
1266
f23631e4
AC
1267struct value *
1268value_repeat (struct value *arg1, int count)
c906108c 1269{
f23631e4 1270 struct value *val;
c906108c
SS
1271
1272 if (VALUE_LVAL (arg1) != lval_memory)
8a3fe4f8 1273 error (_("Only values in memory can be extended with '@'."));
c906108c 1274 if (count < 1)
8a3fe4f8 1275 error (_("Invalid number %d of repetitions."), count);
c906108c 1276
4754a64e 1277 val = allocate_repeat_value (value_enclosing_type (arg1), count);
c906108c 1278
c906108c 1279 VALUE_LVAL (val) = lval_memory;
42ae5230 1280 set_value_address (val, value_address (arg1));
c906108c 1281
24e6bcee
PA
1282 read_value_memory (val, 0, value_stack (val), value_address (val),
1283 value_contents_all_raw (val),
3ae385af 1284 type_length_units (value_enclosing_type (val)));
24e6bcee 1285
c906108c
SS
1286 return val;
1287}
1288
f23631e4 1289struct value *
9df2fbc4 1290value_of_variable (struct symbol *var, const struct block *b)
c906108c 1291{
63e43d3a 1292 struct frame_info *frame = NULL;
c906108c 1293
63e43d3a 1294 if (symbol_read_needs_frame (var))
61212c0f 1295 frame = get_selected_frame (_("No frame selected."));
c906108c 1296
63e43d3a 1297 return read_var_value (var, b, frame);
c906108c
SS
1298}
1299
61212c0f 1300struct value *
270140bd 1301address_of_variable (struct symbol *var, const struct block *b)
61212c0f
UW
1302{
1303 struct type *type = SYMBOL_TYPE (var);
1304 struct value *val;
1305
1306 /* Evaluate it first; if the result is a memory address, we're fine.
581e13c1 1307 Lazy evaluation pays off here. */
61212c0f
UW
1308
1309 val = value_of_variable (var, b);
9f1f738a 1310 type = value_type (val);
61212c0f
UW
1311
1312 if ((VALUE_LVAL (val) == lval_memory && value_lazy (val))
1313 || TYPE_CODE (type) == TYPE_CODE_FUNC)
1314 {
42ae5230 1315 CORE_ADDR addr = value_address (val);
a109c7c1 1316
61212c0f
UW
1317 return value_from_pointer (lookup_pointer_type (type), addr);
1318 }
1319
1320 /* Not a memory address; check what the problem was. */
1321 switch (VALUE_LVAL (val))
1322 {
1323 case lval_register:
1324 {
1325 struct frame_info *frame;
1326 const char *regname;
1327
41b56feb 1328 frame = frame_find_by_id (VALUE_NEXT_FRAME_ID (val));
61212c0f
UW
1329 gdb_assert (frame);
1330
1331 regname = gdbarch_register_name (get_frame_arch (frame),
1332 VALUE_REGNUM (val));
1333 gdb_assert (regname && *regname);
1334
1335 error (_("Address requested for identifier "
1336 "\"%s\" which is in register $%s"),
1337 SYMBOL_PRINT_NAME (var), regname);
1338 break;
1339 }
1340
1341 default:
1342 error (_("Can't take address of \"%s\" which isn't an lvalue."),
1343 SYMBOL_PRINT_NAME (var));
1344 break;
1345 }
1346
1347 return val;
1348}
1349
63092375
DJ
1350/* Return one if VAL does not live in target memory, but should in order
1351 to operate on it. Otherwise return zero. */
1352
1353int
1354value_must_coerce_to_target (struct value *val)
1355{
1356 struct type *valtype;
1357
1358 /* The only lval kinds which do not live in target memory. */
1359 if (VALUE_LVAL (val) != not_lval
e81e7f5e
SC
1360 && VALUE_LVAL (val) != lval_internalvar
1361 && VALUE_LVAL (val) != lval_xcallable)
63092375
DJ
1362 return 0;
1363
1364 valtype = check_typedef (value_type (val));
1365
1366 switch (TYPE_CODE (valtype))
1367 {
1368 case TYPE_CODE_ARRAY:
3cbaedff 1369 return TYPE_VECTOR (valtype) ? 0 : 1;
63092375
DJ
1370 case TYPE_CODE_STRING:
1371 return 1;
1372 default:
1373 return 0;
1374 }
1375}
1376
3e43a32a
MS
1377/* Make sure that VAL lives in target memory if it's supposed to. For
1378 instance, strings are constructed as character arrays in GDB's
1379 storage, and this function copies them to the target. */
63092375
DJ
1380
1381struct value *
1382value_coerce_to_target (struct value *val)
1383{
1384 LONGEST length;
1385 CORE_ADDR addr;
1386
1387 if (!value_must_coerce_to_target (val))
1388 return val;
1389
1390 length = TYPE_LENGTH (check_typedef (value_type (val)));
1391 addr = allocate_space_in_inferior (length);
1392 write_memory (addr, value_contents (val), length);
1393 return value_at_lazy (value_type (val), addr);
1394}
1395
ac3eeb49
MS
1396/* Given a value which is an array, return a value which is a pointer
1397 to its first element, regardless of whether or not the array has a
1398 nonzero lower bound.
c906108c 1399
ac3eeb49
MS
1400 FIXME: A previous comment here indicated that this routine should
1401 be substracting the array's lower bound. It's not clear to me that
1402 this is correct. Given an array subscripting operation, it would
1403 certainly work to do the adjustment here, essentially computing:
c906108c
SS
1404
1405 (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0])
1406
ac3eeb49
MS
1407 However I believe a more appropriate and logical place to account
1408 for the lower bound is to do so in value_subscript, essentially
1409 computing:
c906108c
SS
1410
1411 (&array[0] + ((index - lowerbound) * sizeof array[0]))
1412
ac3eeb49
MS
1413 As further evidence consider what would happen with operations
1414 other than array subscripting, where the caller would get back a
1415 value that had an address somewhere before the actual first element
1416 of the array, and the information about the lower bound would be
581e13c1 1417 lost because of the coercion to pointer type. */
c906108c 1418
f23631e4
AC
1419struct value *
1420value_coerce_array (struct value *arg1)
c906108c 1421{
df407dfe 1422 struct type *type = check_typedef (value_type (arg1));
c906108c 1423
63092375
DJ
1424 /* If the user tries to do something requiring a pointer with an
1425 array that has not yet been pushed to the target, then this would
1426 be a good time to do so. */
1427 arg1 = value_coerce_to_target (arg1);
1428
c906108c 1429 if (VALUE_LVAL (arg1) != lval_memory)
8a3fe4f8 1430 error (_("Attempt to take address of value not located in memory."));
c906108c 1431
4478b372 1432 return value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
42ae5230 1433 value_address (arg1));
c906108c
SS
1434}
1435
1436/* Given a value which is a function, return a value which is a pointer
1437 to it. */
1438
f23631e4
AC
1439struct value *
1440value_coerce_function (struct value *arg1)
c906108c 1441{
f23631e4 1442 struct value *retval;
c906108c
SS
1443
1444 if (VALUE_LVAL (arg1) != lval_memory)
8a3fe4f8 1445 error (_("Attempt to take address of value not located in memory."));
c906108c 1446
df407dfe 1447 retval = value_from_pointer (lookup_pointer_type (value_type (arg1)),
42ae5230 1448 value_address (arg1));
c906108c 1449 return retval;
c5aa993b 1450}
c906108c 1451
ac3eeb49
MS
1452/* Return a pointer value for the object for which ARG1 is the
1453 contents. */
c906108c 1454
f23631e4
AC
1455struct value *
1456value_addr (struct value *arg1)
c906108c 1457{
f23631e4 1458 struct value *arg2;
df407dfe 1459 struct type *type = check_typedef (value_type (arg1));
a109c7c1 1460
aa006118 1461 if (TYPE_IS_REFERENCE (type))
c906108c 1462 {
3326303b
MG
1463 if (value_bits_synthetic_pointer (arg1, value_embedded_offset (arg1),
1464 TARGET_CHAR_BIT * TYPE_LENGTH (type)))
1465 arg1 = coerce_ref (arg1);
1466 else
1467 {
1468 /* Copy the value, but change the type from (T&) to (T*). We
1469 keep the same location information, which is efficient, and
1470 allows &(&X) to get the location containing the reference.
1471 Do the same to its enclosing type for consistency. */
1472 struct type *type_ptr
1473 = lookup_pointer_type (TYPE_TARGET_TYPE (type));
1474 struct type *enclosing_type
1475 = check_typedef (value_enclosing_type (arg1));
1476 struct type *enclosing_type_ptr
1477 = lookup_pointer_type (TYPE_TARGET_TYPE (enclosing_type));
1478
1479 arg2 = value_copy (arg1);
1480 deprecated_set_value_type (arg2, type_ptr);
1481 set_value_enclosing_type (arg2, enclosing_type_ptr);
a22df60a 1482
3326303b
MG
1483 return arg2;
1484 }
c906108c
SS
1485 }
1486 if (TYPE_CODE (type) == TYPE_CODE_FUNC)
1487 return value_coerce_function (arg1);
1488
63092375
DJ
1489 /* If this is an array that has not yet been pushed to the target,
1490 then this would be a good time to force it to memory. */
1491 arg1 = value_coerce_to_target (arg1);
1492
c906108c 1493 if (VALUE_LVAL (arg1) != lval_memory)
8a3fe4f8 1494 error (_("Attempt to take address of value not located in memory."));
c906108c 1495
581e13c1 1496 /* Get target memory address. */
df407dfe 1497 arg2 = value_from_pointer (lookup_pointer_type (value_type (arg1)),
42ae5230 1498 (value_address (arg1)
13c3b5f5 1499 + value_embedded_offset (arg1)));
c906108c
SS
1500
1501 /* This may be a pointer to a base subobject; so remember the
ac3eeb49 1502 full derived object's type ... */
4dfea560
DE
1503 set_value_enclosing_type (arg2,
1504 lookup_pointer_type (value_enclosing_type (arg1)));
ac3eeb49
MS
1505 /* ... and also the relative position of the subobject in the full
1506 object. */
b44d461b 1507 set_value_pointed_to_offset (arg2, value_embedded_offset (arg1));
c906108c
SS
1508 return arg2;
1509}
1510
ac3eeb49
MS
1511/* Return a reference value for the object for which ARG1 is the
1512 contents. */
fb933624
DJ
1513
1514struct value *
a65cfae5 1515value_ref (struct value *arg1, enum type_code refcode)
fb933624
DJ
1516{
1517 struct value *arg2;
fb933624 1518 struct type *type = check_typedef (value_type (arg1));
a109c7c1 1519
a65cfae5
AV
1520 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
1521
1522 if ((TYPE_CODE (type) == TYPE_CODE_REF
1523 || TYPE_CODE (type) == TYPE_CODE_RVALUE_REF)
1524 && TYPE_CODE (type) == refcode)
fb933624
DJ
1525 return arg1;
1526
1527 arg2 = value_addr (arg1);
a65cfae5 1528 deprecated_set_value_type (arg2, lookup_reference_type (type, refcode));
fb933624
DJ
1529 return arg2;
1530}
1531
ac3eeb49
MS
1532/* Given a value of a pointer type, apply the C unary * operator to
1533 it. */
c906108c 1534
f23631e4
AC
1535struct value *
1536value_ind (struct value *arg1)
c906108c
SS
1537{
1538 struct type *base_type;
f23631e4 1539 struct value *arg2;
c906108c 1540
994b9211 1541 arg1 = coerce_array (arg1);
c906108c 1542
df407dfe 1543 base_type = check_typedef (value_type (arg1));
c906108c 1544
8cf6f0b1
TT
1545 if (VALUE_LVAL (arg1) == lval_computed)
1546 {
c8f2448a 1547 const struct lval_funcs *funcs = value_computed_funcs (arg1);
8cf6f0b1
TT
1548
1549 if (funcs->indirect)
1550 {
1551 struct value *result = funcs->indirect (arg1);
1552
1553 if (result)
1554 return result;
1555 }
1556 }
1557
22fe0fbb 1558 if (TYPE_CODE (base_type) == TYPE_CODE_PTR)
c906108c
SS
1559 {
1560 struct type *enc_type;
a109c7c1 1561
ac3eeb49
MS
1562 /* We may be pointing to something embedded in a larger object.
1563 Get the real type of the enclosing object. */
4754a64e 1564 enc_type = check_typedef (value_enclosing_type (arg1));
c906108c 1565 enc_type = TYPE_TARGET_TYPE (enc_type);
0d5de010
DJ
1566
1567 if (TYPE_CODE (check_typedef (enc_type)) == TYPE_CODE_FUNC
1568 || TYPE_CODE (check_typedef (enc_type)) == TYPE_CODE_METHOD)
1569 /* For functions, go through find_function_addr, which knows
1570 how to handle function descriptors. */
ac3eeb49
MS
1571 arg2 = value_at_lazy (enc_type,
1572 find_function_addr (arg1, NULL));
0d5de010 1573 else
581e13c1 1574 /* Retrieve the enclosing object pointed to. */
ac3eeb49
MS
1575 arg2 = value_at_lazy (enc_type,
1576 (value_as_address (arg1)
1577 - value_pointed_to_offset (arg1)));
0d5de010 1578
9f1f738a 1579 enc_type = value_type (arg2);
dfcee124 1580 return readjust_indirect_value_type (arg2, enc_type, base_type, arg1);
c906108c
SS
1581 }
1582
8a3fe4f8 1583 error (_("Attempt to take contents of a non-pointer value."));
c906108c
SS
1584}
1585\f
39d37385
PA
1586/* Create a value for an array by allocating space in GDB, copying the
1587 data into that space, and then setting up an array value.
c906108c 1588
ac3eeb49
MS
1589 The array bounds are set from LOWBOUND and HIGHBOUND, and the array
1590 is populated from the values passed in ELEMVEC.
c906108c
SS
1591
1592 The element type of the array is inherited from the type of the
1593 first element, and all elements must have the same size (though we
ac3eeb49 1594 don't currently enforce any restriction on their types). */
c906108c 1595
f23631e4
AC
1596struct value *
1597value_array (int lowbound, int highbound, struct value **elemvec)
c906108c
SS
1598{
1599 int nelem;
1600 int idx;
6b850546 1601 ULONGEST typelength;
f23631e4 1602 struct value *val;
c906108c 1603 struct type *arraytype;
c906108c 1604
ac3eeb49
MS
1605 /* Validate that the bounds are reasonable and that each of the
1606 elements have the same size. */
c906108c
SS
1607
1608 nelem = highbound - lowbound + 1;
1609 if (nelem <= 0)
1610 {
8a3fe4f8 1611 error (_("bad array bounds (%d, %d)"), lowbound, highbound);
c906108c 1612 }
3ae385af 1613 typelength = type_length_units (value_enclosing_type (elemvec[0]));
c906108c
SS
1614 for (idx = 1; idx < nelem; idx++)
1615 {
3ae385af
SM
1616 if (type_length_units (value_enclosing_type (elemvec[idx]))
1617 != typelength)
c906108c 1618 {
8a3fe4f8 1619 error (_("array elements must all be the same size"));
c906108c
SS
1620 }
1621 }
1622
e3506a9f
UW
1623 arraytype = lookup_array_range_type (value_enclosing_type (elemvec[0]),
1624 lowbound, highbound);
c906108c
SS
1625
1626 if (!current_language->c_style_arrays)
1627 {
1628 val = allocate_value (arraytype);
1629 for (idx = 0; idx < nelem; idx++)
39d37385
PA
1630 value_contents_copy (val, idx * typelength, elemvec[idx], 0,
1631 typelength);
c906108c
SS
1632 return val;
1633 }
1634
63092375
DJ
1635 /* Allocate space to store the array, and then initialize it by
1636 copying in each element. */
c906108c 1637
63092375 1638 val = allocate_value (arraytype);
c906108c 1639 for (idx = 0; idx < nelem; idx++)
39d37385 1640 value_contents_copy (val, idx * typelength, elemvec[idx], 0, typelength);
63092375 1641 return val;
c906108c
SS
1642}
1643
6c7a06a3 1644struct value *
e3a3797e 1645value_cstring (const char *ptr, ssize_t len, struct type *char_type)
6c7a06a3
TT
1646{
1647 struct value *val;
1648 int lowbound = current_language->string_lower_bound;
63375b74 1649 ssize_t highbound = len / TYPE_LENGTH (char_type);
6c7a06a3 1650 struct type *stringtype
e3506a9f 1651 = lookup_array_range_type (char_type, lowbound, highbound + lowbound - 1);
6c7a06a3
TT
1652
1653 val = allocate_value (stringtype);
1654 memcpy (value_contents_raw (val), ptr, len);
1655 return val;
1656}
1657
ac3eeb49
MS
1658/* Create a value for a string constant by allocating space in the
1659 inferior, copying the data into that space, and returning the
1660 address with type TYPE_CODE_STRING. PTR points to the string
1661 constant data; LEN is number of characters.
1662
1663 Note that string types are like array of char types with a lower
1664 bound of zero and an upper bound of LEN - 1. Also note that the
1665 string may contain embedded null bytes. */
c906108c 1666
f23631e4 1667struct value *
7cc3f8e2 1668value_string (const char *ptr, ssize_t len, struct type *char_type)
c906108c 1669{
f23631e4 1670 struct value *val;
c906108c 1671 int lowbound = current_language->string_lower_bound;
63375b74 1672 ssize_t highbound = len / TYPE_LENGTH (char_type);
c906108c 1673 struct type *stringtype
e3506a9f 1674 = lookup_string_range_type (char_type, lowbound, highbound + lowbound - 1);
c906108c 1675
3b7538c0
UW
1676 val = allocate_value (stringtype);
1677 memcpy (value_contents_raw (val), ptr, len);
1678 return val;
c906108c
SS
1679}
1680
c906108c 1681\f
ac3eeb49
MS
1682/* See if we can pass arguments in T2 to a function which takes
1683 arguments of types T1. T1 is a list of NARGS arguments, and T2 is
1684 a NULL-terminated vector. If some arguments need coercion of some
1685 sort, then the coerced values are written into T2. Return value is
1686 0 if the arguments could be matched, or the position at which they
1687 differ if not.
c906108c 1688
ac3eeb49
MS
1689 STATICP is nonzero if the T1 argument list came from a static
1690 member function. T2 will still include the ``this'' pointer, but
1691 it will be skipped.
c906108c
SS
1692
1693 For non-static member functions, we ignore the first argument,
ac3eeb49
MS
1694 which is the type of the instance variable. This is because we
1695 want to handle calls with objects from derived classes. This is
1696 not entirely correct: we should actually check to make sure that a
c906108c
SS
1697 requested operation is type secure, shouldn't we? FIXME. */
1698
1699static int
ad2f7632
DJ
1700typecmp (int staticp, int varargs, int nargs,
1701 struct field t1[], struct value *t2[])
c906108c
SS
1702{
1703 int i;
1704
1705 if (t2 == 0)
ac3eeb49
MS
1706 internal_error (__FILE__, __LINE__,
1707 _("typecmp: no argument list"));
ad2f7632 1708
ac3eeb49
MS
1709 /* Skip ``this'' argument if applicable. T2 will always include
1710 THIS. */
4a1970e4 1711 if (staticp)
ad2f7632
DJ
1712 t2 ++;
1713
1714 for (i = 0;
1715 (i < nargs) && TYPE_CODE (t1[i].type) != TYPE_CODE_VOID;
1716 i++)
c906108c 1717 {
c5aa993b 1718 struct type *tt1, *tt2;
ad2f7632 1719
c5aa993b
JM
1720 if (!t2[i])
1721 return i + 1;
ad2f7632
DJ
1722
1723 tt1 = check_typedef (t1[i].type);
df407dfe 1724 tt2 = check_typedef (value_type (t2[i]));
ad2f7632 1725
aa006118 1726 if (TYPE_IS_REFERENCE (tt1)
8301c89e 1727 /* We should be doing hairy argument matching, as below. */
3e43a32a
MS
1728 && (TYPE_CODE (check_typedef (TYPE_TARGET_TYPE (tt1)))
1729 == TYPE_CODE (tt2)))
c906108c
SS
1730 {
1731 if (TYPE_CODE (tt2) == TYPE_CODE_ARRAY)
1732 t2[i] = value_coerce_array (t2[i]);
1733 else
a65cfae5 1734 t2[i] = value_ref (t2[i], TYPE_CODE (tt1));
c906108c
SS
1735 continue;
1736 }
1737
802db21b
DB
1738 /* djb - 20000715 - Until the new type structure is in the
1739 place, and we can attempt things like implicit conversions,
1740 we need to do this so you can take something like a map<const
1741 char *>, and properly access map["hello"], because the
1742 argument to [] will be a reference to a pointer to a char,
ac3eeb49 1743 and the argument will be a pointer to a char. */
aa006118 1744 while (TYPE_IS_REFERENCE (tt1) || TYPE_CODE (tt1) == TYPE_CODE_PTR)
802db21b
DB
1745 {
1746 tt1 = check_typedef( TYPE_TARGET_TYPE(tt1) );
1747 }
ac3eeb49
MS
1748 while (TYPE_CODE(tt2) == TYPE_CODE_ARRAY
1749 || TYPE_CODE(tt2) == TYPE_CODE_PTR
aa006118 1750 || TYPE_IS_REFERENCE (tt2))
c906108c 1751 {
ac3eeb49 1752 tt2 = check_typedef (TYPE_TARGET_TYPE(tt2));
c906108c 1753 }
c5aa993b
JM
1754 if (TYPE_CODE (tt1) == TYPE_CODE (tt2))
1755 continue;
ac3eeb49
MS
1756 /* Array to pointer is a `trivial conversion' according to the
1757 ARM. */
c906108c 1758
ac3eeb49
MS
1759 /* We should be doing much hairier argument matching (see
1760 section 13.2 of the ARM), but as a quick kludge, just check
1761 for the same type code. */
df407dfe 1762 if (TYPE_CODE (t1[i].type) != TYPE_CODE (value_type (t2[i])))
c5aa993b 1763 return i + 1;
c906108c 1764 }
ad2f7632 1765 if (varargs || t2[i] == NULL)
c5aa993b 1766 return 0;
ad2f7632 1767 return i + 1;
c906108c
SS
1768}
1769
b1af9e97
TT
1770/* Helper class for do_search_struct_field that updates *RESULT_PTR
1771 and *LAST_BOFFSET, and possibly throws an exception if the field
1772 search has yielded ambiguous results. */
c906108c 1773
b1af9e97
TT
1774static void
1775update_search_result (struct value **result_ptr, struct value *v,
6b850546 1776 LONGEST *last_boffset, LONGEST boffset,
b1af9e97
TT
1777 const char *name, struct type *type)
1778{
1779 if (v != NULL)
1780 {
1781 if (*result_ptr != NULL
1782 /* The result is not ambiguous if all the classes that are
1783 found occupy the same space. */
1784 && *last_boffset != boffset)
1785 error (_("base class '%s' is ambiguous in type '%s'"),
1786 name, TYPE_SAFE_NAME (type));
1787 *result_ptr = v;
1788 *last_boffset = boffset;
1789 }
1790}
c906108c 1791
b1af9e97
TT
1792/* A helper for search_struct_field. This does all the work; most
1793 arguments are as passed to search_struct_field. The result is
1794 stored in *RESULT_PTR, which must be initialized to NULL.
1795 OUTERMOST_TYPE is the type of the initial type passed to
1796 search_struct_field; this is used for error reporting when the
1797 lookup is ambiguous. */
1798
1799static void
6b850546 1800do_search_struct_field (const char *name, struct value *arg1, LONGEST offset,
b1af9e97
TT
1801 struct type *type, int looking_for_baseclass,
1802 struct value **result_ptr,
6b850546 1803 LONGEST *last_boffset,
b1af9e97 1804 struct type *outermost_type)
c906108c
SS
1805{
1806 int i;
edf3d5f3 1807 int nbases;
c906108c 1808
f168693b 1809 type = check_typedef (type);
edf3d5f3 1810 nbases = TYPE_N_BASECLASSES (type);
c906108c 1811
c5aa993b 1812 if (!looking_for_baseclass)
c906108c
SS
1813 for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
1814 {
0d5cff50 1815 const char *t_field_name = TYPE_FIELD_NAME (type, i);
c906108c 1816
db577aea 1817 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
c906108c 1818 {
f23631e4 1819 struct value *v;
a109c7c1 1820
d6a843b5 1821 if (field_is_static (&TYPE_FIELD (type, i)))
686d4def 1822 v = value_static_field (type, i);
c906108c 1823 else
b1af9e97
TT
1824 v = value_primitive_field (arg1, offset, i, type);
1825 *result_ptr = v;
1826 return;
c906108c
SS
1827 }
1828
1829 if (t_field_name
47c6ee49 1830 && t_field_name[0] == '\0')
c906108c
SS
1831 {
1832 struct type *field_type = TYPE_FIELD_TYPE (type, i);
a109c7c1 1833
c906108c
SS
1834 if (TYPE_CODE (field_type) == TYPE_CODE_UNION
1835 || TYPE_CODE (field_type) == TYPE_CODE_STRUCT)
1836 {
ac3eeb49
MS
1837 /* Look for a match through the fields of an anonymous
1838 union, or anonymous struct. C++ provides anonymous
1839 unions.
c906108c 1840
1b831c93
AC
1841 In the GNU Chill (now deleted from GDB)
1842 implementation of variant record types, each
1843 <alternative field> has an (anonymous) union type,
1844 each member of the union represents a <variant
1845 alternative>. Each <variant alternative> is
1846 represented as a struct, with a member for each
1847 <variant field>. */
c5aa993b 1848
b1af9e97 1849 struct value *v = NULL;
6b850546 1850 LONGEST new_offset = offset;
c906108c 1851
db034ac5
AC
1852 /* This is pretty gross. In G++, the offset in an
1853 anonymous union is relative to the beginning of the
1b831c93
AC
1854 enclosing struct. In the GNU Chill (now deleted
1855 from GDB) implementation of variant records, the
1856 bitpos is zero in an anonymous union field, so we
ac3eeb49 1857 have to add the offset of the union here. */
c906108c
SS
1858 if (TYPE_CODE (field_type) == TYPE_CODE_STRUCT
1859 || (TYPE_NFIELDS (field_type) > 0
1860 && TYPE_FIELD_BITPOS (field_type, 0) == 0))
1861 new_offset += TYPE_FIELD_BITPOS (type, i) / 8;
1862
b1af9e97
TT
1863 do_search_struct_field (name, arg1, new_offset,
1864 field_type,
1865 looking_for_baseclass, &v,
1866 last_boffset,
1867 outermost_type);
c906108c 1868 if (v)
b1af9e97
TT
1869 {
1870 *result_ptr = v;
1871 return;
1872 }
c906108c
SS
1873 }
1874 }
1875 }
1876
c5aa993b 1877 for (i = 0; i < nbases; i++)
c906108c 1878 {
b1af9e97 1879 struct value *v = NULL;
c906108c 1880 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
ac3eeb49
MS
1881 /* If we are looking for baseclasses, this is what we get when
1882 we hit them. But it could happen that the base part's member
1883 name is not yet filled in. */
c906108c
SS
1884 int found_baseclass = (looking_for_baseclass
1885 && TYPE_BASECLASS_NAME (type, i) != NULL
ac3eeb49
MS
1886 && (strcmp_iw (name,
1887 TYPE_BASECLASS_NAME (type,
1888 i)) == 0));
6b850546 1889 LONGEST boffset = value_embedded_offset (arg1) + offset;
c906108c
SS
1890
1891 if (BASETYPE_VIA_VIRTUAL (type, i))
1892 {
3e3d7139 1893 struct value *v2;
c906108c
SS
1894
1895 boffset = baseclass_offset (type, i,
8af8e3bc
PA
1896 value_contents_for_printing (arg1),
1897 value_embedded_offset (arg1) + offset,
1898 value_address (arg1),
1899 arg1);
c906108c 1900
ac3eeb49 1901 /* The virtual base class pointer might have been clobbered
581e13c1 1902 by the user program. Make sure that it still points to a
ac3eeb49 1903 valid memory location. */
c906108c 1904
1a334831
TT
1905 boffset += value_embedded_offset (arg1) + offset;
1906 if (boffset < 0
1907 || boffset >= TYPE_LENGTH (value_enclosing_type (arg1)))
c906108c
SS
1908 {
1909 CORE_ADDR base_addr;
c5aa993b 1910
42ae5230 1911 base_addr = value_address (arg1) + boffset;
08039c9e 1912 v2 = value_at_lazy (basetype, base_addr);
ac3eeb49
MS
1913 if (target_read_memory (base_addr,
1914 value_contents_raw (v2),
acc900c2 1915 TYPE_LENGTH (value_type (v2))) != 0)
8a3fe4f8 1916 error (_("virtual baseclass botch"));
c906108c
SS
1917 }
1918 else
1919 {
1a334831
TT
1920 v2 = value_copy (arg1);
1921 deprecated_set_value_type (v2, basetype);
1922 set_value_embedded_offset (v2, boffset);
c906108c
SS
1923 }
1924
1925 if (found_baseclass)
b1af9e97
TT
1926 v = v2;
1927 else
1928 {
1929 do_search_struct_field (name, v2, 0,
1930 TYPE_BASECLASS (type, i),
1931 looking_for_baseclass,
1932 result_ptr, last_boffset,
1933 outermost_type);
1934 }
c906108c
SS
1935 }
1936 else if (found_baseclass)
1937 v = value_primitive_field (arg1, offset, i, type);
1938 else
b1af9e97
TT
1939 {
1940 do_search_struct_field (name, arg1,
1941 offset + TYPE_BASECLASS_BITPOS (type,
1942 i) / 8,
1943 basetype, looking_for_baseclass,
1944 result_ptr, last_boffset,
1945 outermost_type);
1946 }
1947
1948 update_search_result (result_ptr, v, last_boffset,
1949 boffset, name, outermost_type);
c906108c 1950 }
b1af9e97
TT
1951}
1952
1953/* Helper function used by value_struct_elt to recurse through
8a13d42d
SM
1954 baseclasses. Look for a field NAME in ARG1. Search in it assuming
1955 it has (class) type TYPE. If found, return value, else return NULL.
b1af9e97
TT
1956
1957 If LOOKING_FOR_BASECLASS, then instead of looking for struct
1958 fields, look for a baseclass named NAME. */
1959
1960static struct value *
8a13d42d 1961search_struct_field (const char *name, struct value *arg1,
b1af9e97
TT
1962 struct type *type, int looking_for_baseclass)
1963{
1964 struct value *result = NULL;
6b850546 1965 LONGEST boffset = 0;
b1af9e97 1966
8a13d42d 1967 do_search_struct_field (name, arg1, 0, type, looking_for_baseclass,
b1af9e97
TT
1968 &result, &boffset, type);
1969 return result;
c906108c
SS
1970}
1971
ac3eeb49 1972/* Helper function used by value_struct_elt to recurse through
581e13c1 1973 baseclasses. Look for a field NAME in ARG1. Adjust the address of
ac3eeb49
MS
1974 ARG1 by OFFSET bytes, and search in it assuming it has (class) type
1975 TYPE.
1976
1977 If found, return value, else if name matched and args not return
1978 (value) -1, else return NULL. */
c906108c 1979
f23631e4 1980static struct value *
714f19d5 1981search_struct_method (const char *name, struct value **arg1p,
6b850546 1982 struct value **args, LONGEST offset,
aa1ee363 1983 int *static_memfuncp, struct type *type)
c906108c
SS
1984{
1985 int i;
f23631e4 1986 struct value *v;
c906108c
SS
1987 int name_matched = 0;
1988 char dem_opname[64];
1989
f168693b 1990 type = check_typedef (type);
c906108c
SS
1991 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
1992 {
0d5cff50 1993 const char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
a109c7c1 1994
581e13c1 1995 /* FIXME! May need to check for ARM demangling here. */
61012eef
GB
1996 if (startswith (t_field_name, "__") ||
1997 startswith (t_field_name, "op") ||
1998 startswith (t_field_name, "type"))
c906108c 1999 {
c5aa993b
JM
2000 if (cplus_demangle_opname (t_field_name, dem_opname, DMGL_ANSI))
2001 t_field_name = dem_opname;
2002 else if (cplus_demangle_opname (t_field_name, dem_opname, 0))
c906108c 2003 t_field_name = dem_opname;
c906108c 2004 }
db577aea 2005 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
c906108c
SS
2006 {
2007 int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1;
2008 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
c906108c 2009
a109c7c1 2010 name_matched = 1;
de17c821 2011 check_stub_method_group (type, i);
c906108c 2012 if (j > 0 && args == 0)
3e43a32a
MS
2013 error (_("cannot resolve overloaded method "
2014 "`%s': no arguments supplied"), name);
acf5ed49 2015 else if (j == 0 && args == 0)
c906108c 2016 {
acf5ed49
DJ
2017 v = value_fn_field (arg1p, f, j, type, offset);
2018 if (v != NULL)
2019 return v;
c906108c 2020 }
acf5ed49
DJ
2021 else
2022 while (j >= 0)
2023 {
acf5ed49 2024 if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j),
ad2f7632
DJ
2025 TYPE_VARARGS (TYPE_FN_FIELD_TYPE (f, j)),
2026 TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f, j)),
acf5ed49
DJ
2027 TYPE_FN_FIELD_ARGS (f, j), args))
2028 {
2029 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
ac3eeb49
MS
2030 return value_virtual_fn_field (arg1p, f, j,
2031 type, offset);
2032 if (TYPE_FN_FIELD_STATIC_P (f, j)
2033 && static_memfuncp)
acf5ed49
DJ
2034 *static_memfuncp = 1;
2035 v = value_fn_field (arg1p, f, j, type, offset);
2036 if (v != NULL)
2037 return v;
2038 }
2039 j--;
2040 }
c906108c
SS
2041 }
2042 }
2043
2044 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
2045 {
6b850546
DT
2046 LONGEST base_offset;
2047 LONGEST this_offset;
c906108c
SS
2048
2049 if (BASETYPE_VIA_VIRTUAL (type, i))
2050 {
086280be 2051 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
8af8e3bc 2052 struct value *base_val;
086280be
UW
2053 const gdb_byte *base_valaddr;
2054
2055 /* The virtual base class pointer might have been
581e13c1 2056 clobbered by the user program. Make sure that it
8301c89e 2057 still points to a valid memory location. */
086280be
UW
2058
2059 if (offset < 0 || offset >= TYPE_LENGTH (type))
c5aa993b 2060 {
6c18f3e0
SP
2061 CORE_ADDR address;
2062
26fcd5d7 2063 gdb::byte_vector tmp (TYPE_LENGTH (baseclass));
6c18f3e0 2064 address = value_address (*arg1p);
a109c7c1 2065
8af8e3bc 2066 if (target_read_memory (address + offset,
26fcd5d7 2067 tmp.data (), TYPE_LENGTH (baseclass)) != 0)
086280be 2068 error (_("virtual baseclass botch"));
8af8e3bc
PA
2069
2070 base_val = value_from_contents_and_address (baseclass,
26fcd5d7 2071 tmp.data (),
8af8e3bc
PA
2072 address + offset);
2073 base_valaddr = value_contents_for_printing (base_val);
2074 this_offset = 0;
c5aa993b
JM
2075 }
2076 else
8af8e3bc
PA
2077 {
2078 base_val = *arg1p;
2079 base_valaddr = value_contents_for_printing (*arg1p);
2080 this_offset = offset;
2081 }
c5aa993b 2082
086280be 2083 base_offset = baseclass_offset (type, i, base_valaddr,
8af8e3bc
PA
2084 this_offset, value_address (base_val),
2085 base_val);
c5aa993b 2086 }
c906108c
SS
2087 else
2088 {
2089 base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
c5aa993b 2090 }
c906108c
SS
2091 v = search_struct_method (name, arg1p, args, base_offset + offset,
2092 static_memfuncp, TYPE_BASECLASS (type, i));
f23631e4 2093 if (v == (struct value *) - 1)
c906108c
SS
2094 {
2095 name_matched = 1;
2096 }
2097 else if (v)
2098 {
ac3eeb49
MS
2099 /* FIXME-bothner: Why is this commented out? Why is it here? */
2100 /* *arg1p = arg1_tmp; */
c906108c 2101 return v;
c5aa993b 2102 }
c906108c 2103 }
c5aa993b 2104 if (name_matched)
f23631e4 2105 return (struct value *) - 1;
c5aa993b
JM
2106 else
2107 return NULL;
c906108c
SS
2108}
2109
2110/* Given *ARGP, a value of type (pointer to a)* structure/union,
ac3eeb49
MS
2111 extract the component named NAME from the ultimate target
2112 structure/union and return it as a value with its appropriate type.
c906108c
SS
2113 ERR is used in the error message if *ARGP's type is wrong.
2114
2115 C++: ARGS is a list of argument types to aid in the selection of
581e13c1 2116 an appropriate method. Also, handle derived types.
c906108c
SS
2117
2118 STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
2119 where the truthvalue of whether the function that was resolved was
2120 a static member function or not is stored.
2121
ac3eeb49
MS
2122 ERR is an error message to be printed in case the field is not
2123 found. */
c906108c 2124
f23631e4
AC
2125struct value *
2126value_struct_elt (struct value **argp, struct value **args,
714f19d5 2127 const char *name, int *static_memfuncp, const char *err)
c906108c 2128{
52f0bd74 2129 struct type *t;
f23631e4 2130 struct value *v;
c906108c 2131
994b9211 2132 *argp = coerce_array (*argp);
c906108c 2133
df407dfe 2134 t = check_typedef (value_type (*argp));
c906108c
SS
2135
2136 /* Follow pointers until we get to a non-pointer. */
2137
aa006118 2138 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_IS_REFERENCE (t))
c906108c
SS
2139 {
2140 *argp = value_ind (*argp);
2141 /* Don't coerce fn pointer to fn and then back again! */
b846d303 2142 if (TYPE_CODE (check_typedef (value_type (*argp))) != TYPE_CODE_FUNC)
994b9211 2143 *argp = coerce_array (*argp);
df407dfe 2144 t = check_typedef (value_type (*argp));
c906108c
SS
2145 }
2146
c5aa993b 2147 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
c906108c 2148 && TYPE_CODE (t) != TYPE_CODE_UNION)
3e43a32a
MS
2149 error (_("Attempt to extract a component of a value that is not a %s."),
2150 err);
c906108c
SS
2151
2152 /* Assume it's not, unless we see that it is. */
2153 if (static_memfuncp)
c5aa993b 2154 *static_memfuncp = 0;
c906108c
SS
2155
2156 if (!args)
2157 {
2158 /* if there are no arguments ...do this... */
2159
ac3eeb49
MS
2160 /* Try as a field first, because if we succeed, there is less
2161 work to be done. */
8a13d42d 2162 v = search_struct_field (name, *argp, t, 0);
c906108c
SS
2163 if (v)
2164 return v;
2165
2166 /* C++: If it was not found as a data field, then try to
7b83ea04 2167 return it as a pointer to a method. */
ac3eeb49
MS
2168 v = search_struct_method (name, argp, args, 0,
2169 static_memfuncp, t);
c906108c 2170
f23631e4 2171 if (v == (struct value *) - 1)
55b39184 2172 error (_("Cannot take address of method %s."), name);
c906108c
SS
2173 else if (v == 0)
2174 {
2175 if (TYPE_NFN_FIELDS (t))
8a3fe4f8 2176 error (_("There is no member or method named %s."), name);
c906108c 2177 else
8a3fe4f8 2178 error (_("There is no member named %s."), name);
c906108c
SS
2179 }
2180 return v;
2181 }
2182
8301c89e
DE
2183 v = search_struct_method (name, argp, args, 0,
2184 static_memfuncp, t);
7168a814 2185
f23631e4 2186 if (v == (struct value *) - 1)
c906108c 2187 {
3e43a32a
MS
2188 error (_("One of the arguments you tried to pass to %s could not "
2189 "be converted to what the function wants."), name);
c906108c
SS
2190 }
2191 else if (v == 0)
2192 {
ac3eeb49
MS
2193 /* See if user tried to invoke data as function. If so, hand it
2194 back. If it's not callable (i.e., a pointer to function),
7b83ea04 2195 gdb should give an error. */
8a13d42d 2196 v = search_struct_field (name, *argp, t, 0);
fa8de41e
TT
2197 /* If we found an ordinary field, then it is not a method call.
2198 So, treat it as if it were a static member function. */
2199 if (v && static_memfuncp)
2200 *static_memfuncp = 1;
c906108c
SS
2201 }
2202
2203 if (!v)
79afc5ef
SW
2204 throw_error (NOT_FOUND_ERROR,
2205 _("Structure has no component named %s."), name);
c906108c
SS
2206 return v;
2207}
2208
b5b08fb4
SC
2209/* Given *ARGP, a value of type structure or union, or a pointer/reference
2210 to a structure or union, extract and return its component (field) of
2211 type FTYPE at the specified BITPOS.
2212 Throw an exception on error. */
2213
2214struct value *
2215value_struct_elt_bitpos (struct value **argp, int bitpos, struct type *ftype,
2216 const char *err)
2217{
2218 struct type *t;
b5b08fb4 2219 int i;
b5b08fb4
SC
2220
2221 *argp = coerce_array (*argp);
2222
2223 t = check_typedef (value_type (*argp));
2224
aa006118 2225 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_IS_REFERENCE (t))
b5b08fb4
SC
2226 {
2227 *argp = value_ind (*argp);
2228 if (TYPE_CODE (check_typedef (value_type (*argp))) != TYPE_CODE_FUNC)
2229 *argp = coerce_array (*argp);
2230 t = check_typedef (value_type (*argp));
2231 }
2232
2233 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
2234 && TYPE_CODE (t) != TYPE_CODE_UNION)
2235 error (_("Attempt to extract a component of a value that is not a %s."),
2236 err);
2237
2238 for (i = TYPE_N_BASECLASSES (t); i < TYPE_NFIELDS (t); i++)
2239 {
2240 if (!field_is_static (&TYPE_FIELD (t, i))
2241 && bitpos == TYPE_FIELD_BITPOS (t, i)
2242 && types_equal (ftype, TYPE_FIELD_TYPE (t, i)))
2243 return value_primitive_field (*argp, 0, i, t);
2244 }
2245
2246 error (_("No field with matching bitpos and type."));
2247
2248 /* Never hit. */
2249 return NULL;
2250}
2251
7c22600a
TT
2252/* See value.h. */
2253
2254int
2255value_union_variant (struct type *union_type, const gdb_byte *contents)
2256{
2257 gdb_assert (TYPE_CODE (union_type) == TYPE_CODE_UNION
2258 && TYPE_FLAG_DISCRIMINATED_UNION (union_type));
2259
2260 struct dynamic_prop *discriminant_prop
2261 = get_dyn_prop (DYN_PROP_DISCRIMINATED, union_type);
2262 gdb_assert (discriminant_prop != nullptr);
2263
2264 struct discriminant_info *info
2265 = (struct discriminant_info *) discriminant_prop->data.baton;
2266 gdb_assert (info != nullptr);
2267
2268 /* If this is a univariant union, just return the sole field. */
2269 if (TYPE_NFIELDS (union_type) == 1)
2270 return 0;
2271 /* This should only happen for univariants, which we already dealt
2272 with. */
2273 gdb_assert (info->discriminant_index != -1);
2274
2275 /* Compute the discriminant. Note that unpack_field_as_long handles
2276 sign extension when necessary, as does the DWARF reader -- so
2277 signed discriminants will be handled correctly despite the use of
2278 an unsigned type here. */
2279 ULONGEST discriminant = unpack_field_as_long (union_type, contents,
2280 info->discriminant_index);
2281
2282 for (int i = 0; i < TYPE_NFIELDS (union_type); ++i)
2283 {
2284 if (i != info->default_index
2285 && i != info->discriminant_index
2286 && discriminant == info->discriminants[i])
2287 return i;
2288 }
2289
2290 if (info->default_index == -1)
2291 error (_("Could not find variant corresponding to discriminant %s"),
2292 pulongest (discriminant));
2293 return info->default_index;
2294}
2295
ac3eeb49 2296/* Search through the methods of an object (and its bases) to find a
233e8b28
SC
2297 specified method. Return the pointer to the fn_field list FN_LIST of
2298 overloaded instances defined in the source language. If available
2299 and matching, a vector of matching xmethods defined in extension
2300 languages are also returned in XM_WORKER_VEC
ac3eeb49
MS
2301
2302 Helper function for value_find_oload_list.
2303 ARGP is a pointer to a pointer to a value (the object).
2304 METHOD is a string containing the method name.
2305 OFFSET is the offset within the value.
2306 TYPE is the assumed type of the object.
233e8b28
SC
2307 FN_LIST is the pointer to matching overloaded instances defined in
2308 source language. Since this is a recursive function, *FN_LIST
2309 should be set to NULL when calling this function.
2310 NUM_FNS is the number of overloaded instances. *NUM_FNS should be set to
2311 0 when calling this function.
2312 XM_WORKER_VEC is the vector of matching xmethod workers. *XM_WORKER_VEC
2313 should also be set to NULL when calling this function.
ac3eeb49
MS
2314 BASETYPE is set to the actual type of the subobject where the
2315 method is found.
581e13c1 2316 BOFFSET is the offset of the base subobject where the method is found. */
c906108c 2317
233e8b28 2318static void
714f19d5 2319find_method_list (struct value **argp, const char *method,
6b850546 2320 LONGEST offset, struct type *type,
233e8b28 2321 struct fn_field **fn_list, int *num_fns,
ba18742c 2322 std::vector<xmethod_worker_up> *xm_worker_vec,
6b850546 2323 struct type **basetype, LONGEST *boffset)
c906108c
SS
2324{
2325 int i;
233e8b28 2326 struct fn_field *f = NULL;
c906108c 2327
233e8b28 2328 gdb_assert (fn_list != NULL && xm_worker_vec != NULL);
f168693b 2329 type = check_typedef (type);
c906108c 2330
233e8b28
SC
2331 /* First check in object itself.
2332 This function is called recursively to search through base classes.
2333 If there is a source method match found at some stage, then we need not
2334 look for source methods in consequent recursive calls. */
2335 if ((*fn_list) == NULL)
c906108c 2336 {
233e8b28 2337 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
c5aa993b 2338 {
233e8b28
SC
2339 /* pai: FIXME What about operators and type conversions? */
2340 const char *fn_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
2341
2342 if (fn_field_name && (strcmp_iw (fn_field_name, method) == 0))
2343 {
2344 int len = TYPE_FN_FIELDLIST_LENGTH (type, i);
2345 f = TYPE_FN_FIELDLIST1 (type, i);
2346 *fn_list = f;
4a1970e4 2347
233e8b28
SC
2348 *num_fns = len;
2349 *basetype = type;
2350 *boffset = offset;
4a1970e4 2351
233e8b28
SC
2352 /* Resolve any stub methods. */
2353 check_stub_method_group (type, i);
4a1970e4 2354
233e8b28
SC
2355 break;
2356 }
c5aa993b
JM
2357 }
2358 }
2359
233e8b28
SC
2360 /* Unlike source methods, xmethods can be accumulated over successive
2361 recursive calls. In other words, an xmethod named 'm' in a class
2362 will not hide an xmethod named 'm' in its base class(es). We want
2363 it to be this way because xmethods are after all convenience functions
2364 and hence there is no point restricting them with something like method
2365 hiding. Moreover, if hiding is done for xmethods as well, then we will
2366 have to provide a mechanism to un-hide (like the 'using' construct). */
ba18742c 2367 get_matching_xmethod_workers (type, method, xm_worker_vec);
233e8b28
SC
2368
2369 /* If source methods are not found in current class, look for them in the
2370 base classes. We also have to go through the base classes to gather
2371 extension methods. */
c906108c
SS
2372 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
2373 {
6b850546 2374 LONGEST base_offset;
a109c7c1 2375
c906108c
SS
2376 if (BASETYPE_VIA_VIRTUAL (type, i))
2377 {
086280be 2378 base_offset = baseclass_offset (type, i,
8af8e3bc
PA
2379 value_contents_for_printing (*argp),
2380 value_offset (*argp) + offset,
2381 value_address (*argp), *argp);
c5aa993b 2382 }
ac3eeb49
MS
2383 else /* Non-virtual base, simply use bit position from debug
2384 info. */
c906108c
SS
2385 {
2386 base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
c5aa993b 2387 }
233e8b28
SC
2388
2389 find_method_list (argp, method, base_offset + offset,
2390 TYPE_BASECLASS (type, i), fn_list, num_fns,
2391 xm_worker_vec, basetype, boffset);
c906108c 2392 }
c906108c
SS
2393}
2394
233e8b28
SC
2395/* Return the list of overloaded methods of a specified name. The methods
2396 could be those GDB finds in the binary, or xmethod. Methods found in
2397 the binary are returned in FN_LIST, and xmethods are returned in
2398 XM_WORKER_VEC.
ac3eeb49
MS
2399
2400 ARGP is a pointer to a pointer to a value (the object).
2401 METHOD is the method name.
2402 OFFSET is the offset within the value contents.
233e8b28
SC
2403 FN_LIST is the pointer to matching overloaded instances defined in
2404 source language.
ac3eeb49 2405 NUM_FNS is the number of overloaded instances.
233e8b28
SC
2406 XM_WORKER_VEC is the vector of matching xmethod workers defined in
2407 extension languages.
ac3eeb49
MS
2408 BASETYPE is set to the type of the base subobject that defines the
2409 method.
581e13c1 2410 BOFFSET is the offset of the base subobject which defines the method. */
c906108c 2411
233e8b28 2412static void
714f19d5 2413value_find_oload_method_list (struct value **argp, const char *method,
6b850546 2414 LONGEST offset, struct fn_field **fn_list,
233e8b28 2415 int *num_fns,
ba18742c 2416 std::vector<xmethod_worker_up> *xm_worker_vec,
6b850546 2417 struct type **basetype, LONGEST *boffset)
c906108c 2418{
c5aa993b 2419 struct type *t;
c906108c 2420
df407dfe 2421 t = check_typedef (value_type (*argp));
c906108c 2422
ac3eeb49 2423 /* Code snarfed from value_struct_elt. */
aa006118 2424 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_IS_REFERENCE (t))
c906108c
SS
2425 {
2426 *argp = value_ind (*argp);
2427 /* Don't coerce fn pointer to fn and then back again! */
b846d303 2428 if (TYPE_CODE (check_typedef (value_type (*argp))) != TYPE_CODE_FUNC)
994b9211 2429 *argp = coerce_array (*argp);
df407dfe 2430 t = check_typedef (value_type (*argp));
c906108c 2431 }
c5aa993b 2432
c5aa993b
JM
2433 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
2434 && TYPE_CODE (t) != TYPE_CODE_UNION)
3e43a32a
MS
2435 error (_("Attempt to extract a component of a "
2436 "value that is not a struct or union"));
c5aa993b 2437
233e8b28
SC
2438 gdb_assert (fn_list != NULL && xm_worker_vec != NULL);
2439
2440 /* Clear the lists. */
2441 *fn_list = NULL;
2442 *num_fns = 0;
ba18742c 2443 xm_worker_vec->clear ();
233e8b28
SC
2444
2445 find_method_list (argp, method, 0, t, fn_list, num_fns, xm_worker_vec,
2446 basetype, boffset);
c906108c
SS
2447}
2448
da096638 2449/* Given an array of arguments (ARGS) (which includes an
c906108c 2450 entry for "this" in the case of C++ methods), the number of
28c64fc2
SCR
2451 arguments NARGS, the NAME of a function, and whether it's a method or
2452 not (METHOD), find the best function that matches on the argument types
2453 according to the overload resolution rules.
c906108c 2454
4c3376c8
SW
2455 METHOD can be one of three values:
2456 NON_METHOD for non-member functions.
2457 METHOD: for member functions.
2458 BOTH: used for overload resolution of operators where the
2459 candidates are expected to be either member or non member
581e13c1 2460 functions. In this case the first argument ARGTYPES
4c3376c8
SW
2461 (representing 'this') is expected to be a reference to the
2462 target object, and will be dereferenced when attempting the
2463 non-member search.
2464
c906108c
SS
2465 In the case of class methods, the parameter OBJ is an object value
2466 in which to search for overloaded methods.
2467
2468 In the case of non-method functions, the parameter FSYM is a symbol
2469 corresponding to one of the overloaded functions.
2470
2471 Return value is an integer: 0 -> good match, 10 -> debugger applied
2472 non-standard coercions, 100 -> incompatible.
2473
2474 If a method is being searched for, VALP will hold the value.
ac3eeb49
MS
2475 If a non-method is being searched for, SYMP will hold the symbol
2476 for it.
c906108c
SS
2477
2478 If a method is being searched for, and it is a static method,
2479 then STATICP will point to a non-zero value.
2480
7322dca9
SW
2481 If NO_ADL argument dependent lookup is disabled. This is used to prevent
2482 ADL overload candidates when performing overload resolution for a fully
2483 qualified name.
2484
e66d4446
SC
2485 If NOSIDE is EVAL_AVOID_SIDE_EFFECTS, then OBJP's memory cannot be
2486 read while picking the best overload match (it may be all zeroes and thus
2487 not have a vtable pointer), in which case skip virtual function lookup.
2488 This is ok as typically EVAL_AVOID_SIDE_EFFECTS is only used to determine
2489 the result type.
2490
c906108c
SS
2491 Note: This function does *not* check the value of
2492 overload_resolution. Caller must check it to see whether overload
581e13c1 2493 resolution is permitted. */
c906108c
SS
2494
2495int
da096638 2496find_overload_match (struct value **args, int nargs,
4c3376c8 2497 const char *name, enum oload_search_type method,
28c64fc2 2498 struct value **objp, struct symbol *fsym,
ac3eeb49 2499 struct value **valp, struct symbol **symp,
e66d4446
SC
2500 int *staticp, const int no_adl,
2501 const enum noside noside)
c906108c 2502{
7f8c9282 2503 struct value *obj = (objp ? *objp : NULL);
da096638 2504 struct type *obj_type = obj ? value_type (obj) : NULL;
ac3eeb49 2505 /* Index of best overloaded function. */
4c3376c8
SW
2506 int func_oload_champ = -1;
2507 int method_oload_champ = -1;
233e8b28
SC
2508 int src_method_oload_champ = -1;
2509 int ext_method_oload_champ = -1;
4c3376c8 2510
ac3eeb49 2511 /* The measure for the current best match. */
4c3376c8
SW
2512 struct badness_vector *method_badness = NULL;
2513 struct badness_vector *func_badness = NULL;
233e8b28
SC
2514 struct badness_vector *ext_method_badness = NULL;
2515 struct badness_vector *src_method_badness = NULL;
4c3376c8 2516
f23631e4 2517 struct value *temp = obj;
ac3eeb49
MS
2518 /* For methods, the list of overloaded methods. */
2519 struct fn_field *fns_ptr = NULL;
2520 /* For non-methods, the list of overloaded function symbols. */
2521 struct symbol **oload_syms = NULL;
ba18742c
SM
2522 /* For xmethods, the vector of xmethod workers. */
2523 std::vector<xmethod_worker_up> xm_worker_vec;
ac3eeb49
MS
2524 /* Number of overloaded instances being considered. */
2525 int num_fns = 0;
c5aa993b 2526 struct type *basetype = NULL;
6b850546 2527 LONGEST boffset;
7322dca9
SW
2528
2529 struct cleanup *all_cleanups = make_cleanup (null_cleanup, NULL);
c906108c 2530
8d577d32 2531 const char *obj_type_name = NULL;
7322dca9 2532 const char *func_name = NULL;
8d577d32 2533 enum oload_classification match_quality;
4c3376c8 2534 enum oload_classification method_match_quality = INCOMPATIBLE;
233e8b28
SC
2535 enum oload_classification src_method_match_quality = INCOMPATIBLE;
2536 enum oload_classification ext_method_match_quality = INCOMPATIBLE;
4c3376c8 2537 enum oload_classification func_match_quality = INCOMPATIBLE;
c906108c 2538
ac3eeb49 2539 /* Get the list of overloaded methods or functions. */
4c3376c8 2540 if (method == METHOD || method == BOTH)
c906108c 2541 {
a2ca50ae 2542 gdb_assert (obj);
94af9270
KS
2543
2544 /* OBJ may be a pointer value rather than the object itself. */
2545 obj = coerce_ref (obj);
2546 while (TYPE_CODE (check_typedef (value_type (obj))) == TYPE_CODE_PTR)
2547 obj = coerce_ref (value_ind (obj));
df407dfe 2548 obj_type_name = TYPE_NAME (value_type (obj));
94af9270
KS
2549
2550 /* First check whether this is a data member, e.g. a pointer to
2551 a function. */
2552 if (TYPE_CODE (check_typedef (value_type (obj))) == TYPE_CODE_STRUCT)
2553 {
8a13d42d 2554 *valp = search_struct_field (name, obj,
94af9270
KS
2555 check_typedef (value_type (obj)), 0);
2556 if (*valp)
2557 {
2558 *staticp = 1;
f748fb40 2559 do_cleanups (all_cleanups);
94af9270
KS
2560 return 0;
2561 }
2562 }
c906108c 2563
4c3376c8 2564 /* Retrieve the list of methods with the name NAME. */
233e8b28
SC
2565 value_find_oload_method_list (&temp, name, 0, &fns_ptr, &num_fns,
2566 &xm_worker_vec, &basetype, &boffset);
4c3376c8 2567 /* If this is a method only search, and no methods were found
ba18742c
SM
2568 the search has failed. */
2569 if (method == METHOD && (!fns_ptr || !num_fns) && xm_worker_vec.empty ())
8a3fe4f8 2570 error (_("Couldn't find method %s%s%s"),
c5aa993b
JM
2571 obj_type_name,
2572 (obj_type_name && *obj_type_name) ? "::" : "",
2573 name);
4a1970e4 2574 /* If we are dealing with stub method types, they should have
ac3eeb49
MS
2575 been resolved by find_method_list via
2576 value_find_oload_method_list above. */
4c3376c8
SW
2577 if (fns_ptr)
2578 {
4bfb94b8 2579 gdb_assert (TYPE_SELF_TYPE (fns_ptr[0].type) != NULL);
4c3376c8 2580
233e8b28
SC
2581 src_method_oload_champ = find_oload_champ (args, nargs,
2582 num_fns, fns_ptr, NULL,
2583 NULL, &src_method_badness);
2584
2585 src_method_match_quality = classify_oload_match
2586 (src_method_badness, nargs,
2587 oload_method_static_p (fns_ptr, src_method_oload_champ));
2588
2589 make_cleanup (xfree, src_method_badness);
2590 }
4c3376c8 2591
ba18742c 2592 if (!xm_worker_vec.empty ())
233e8b28
SC
2593 {
2594 ext_method_oload_champ = find_oload_champ (args, nargs,
ba18742c 2595 0, NULL, &xm_worker_vec,
233e8b28
SC
2596 NULL, &ext_method_badness);
2597 ext_method_match_quality = classify_oload_match (ext_method_badness,
2598 nargs, 0);
2599 make_cleanup (xfree, ext_method_badness);
4c3376c8
SW
2600 }
2601
233e8b28
SC
2602 if (src_method_oload_champ >= 0 && ext_method_oload_champ >= 0)
2603 {
2604 switch (compare_badness (ext_method_badness, src_method_badness))
2605 {
2606 case 0: /* Src method and xmethod are equally good. */
233e8b28
SC
2607 /* If src method and xmethod are equally good, then
2608 xmethod should be the winner. Hence, fall through to the
2609 case where a xmethod is better than the source
2610 method, except when the xmethod match quality is
2611 non-standard. */
2612 /* FALLTHROUGH */
2613 case 1: /* Src method and ext method are incompatible. */
2614 /* If ext method match is not standard, then let source method
2615 win. Otherwise, fallthrough to let xmethod win. */
2616 if (ext_method_match_quality != STANDARD)
2617 {
2618 method_oload_champ = src_method_oload_champ;
2619 method_badness = src_method_badness;
2620 ext_method_oload_champ = -1;
2621 method_match_quality = src_method_match_quality;
2622 break;
2623 }
2624 /* FALLTHROUGH */
2625 case 2: /* Ext method is champion. */
2626 method_oload_champ = ext_method_oload_champ;
2627 method_badness = ext_method_badness;
2628 src_method_oload_champ = -1;
2629 method_match_quality = ext_method_match_quality;
2630 break;
2631 case 3: /* Src method is champion. */
2632 method_oload_champ = src_method_oload_champ;
2633 method_badness = src_method_badness;
2634 ext_method_oload_champ = -1;
2635 method_match_quality = src_method_match_quality;
2636 break;
2637 default:
2638 gdb_assert_not_reached ("Unexpected overload comparison "
2639 "result");
2640 break;
2641 }
2642 }
2643 else if (src_method_oload_champ >= 0)
2644 {
2645 method_oload_champ = src_method_oload_champ;
2646 method_badness = src_method_badness;
2647 method_match_quality = src_method_match_quality;
2648 }
2649 else if (ext_method_oload_champ >= 0)
2650 {
2651 method_oload_champ = ext_method_oload_champ;
2652 method_badness = ext_method_badness;
2653 method_match_quality = ext_method_match_quality;
2654 }
c906108c 2655 }
4c3376c8
SW
2656
2657 if (method == NON_METHOD || method == BOTH)
c906108c 2658 {
7322dca9 2659 const char *qualified_name = NULL;
c906108c 2660
b021a221
MS
2661 /* If the overload match is being search for both as a method
2662 and non member function, the first argument must now be
2663 dereferenced. */
4c3376c8 2664 if (method == BOTH)
2b214ea6 2665 args[0] = value_ind (args[0]);
4c3376c8 2666
7322dca9
SW
2667 if (fsym)
2668 {
2669 qualified_name = SYMBOL_NATURAL_NAME (fsym);
2670
2671 /* If we have a function with a C++ name, try to extract just
2672 the function part. Do not try this for non-functions (e.g.
2673 function pointers). */
2674 if (qualified_name
3e43a32a
MS
2675 && TYPE_CODE (check_typedef (SYMBOL_TYPE (fsym)))
2676 == TYPE_CODE_FUNC)
7322dca9
SW
2677 {
2678 char *temp;
2679
2680 temp = cp_func_name (qualified_name);
2681
2682 /* If cp_func_name did not remove anything, the name of the
2683 symbol did not include scope or argument types - it was
2684 probably a C-style function. */
2685 if (temp)
2686 {
2687 make_cleanup (xfree, temp);
2688 if (strcmp (temp, qualified_name) == 0)
2689 func_name = NULL;
2690 else
2691 func_name = temp;
2692 }
2693 }
2694 }
2695 else
94af9270 2696 {
7322dca9
SW
2697 func_name = name;
2698 qualified_name = name;
94af9270 2699 }
d9639e13 2700
94af9270
KS
2701 /* If there was no C++ name, this must be a C-style function or
2702 not a function at all. Just return the same symbol. Do the
2703 same if cp_func_name fails for some reason. */
8d577d32 2704 if (func_name == NULL)
7b83ea04 2705 {
917317f4 2706 *symp = fsym;
5fe41fbf 2707 do_cleanups (all_cleanups);
7b83ea04
AC
2708 return 0;
2709 }
917317f4 2710
da096638 2711 func_oload_champ = find_oload_champ_namespace (args, nargs,
4c3376c8
SW
2712 func_name,
2713 qualified_name,
2714 &oload_syms,
2715 &func_badness,
2716 no_adl);
8d577d32 2717
4c3376c8
SW
2718 if (func_oload_champ >= 0)
2719 func_match_quality = classify_oload_match (func_badness, nargs, 0);
2720
2721 make_cleanup (xfree, oload_syms);
2722 make_cleanup (xfree, func_badness);
8d577d32
DC
2723 }
2724
7322dca9 2725 /* Did we find a match ? */
4c3376c8 2726 if (method_oload_champ == -1 && func_oload_champ == -1)
79afc5ef
SW
2727 throw_error (NOT_FOUND_ERROR,
2728 _("No symbol \"%s\" in current context."),
2729 name);
8d577d32 2730
4c3376c8
SW
2731 /* If we have found both a method match and a function
2732 match, find out which one is better, and calculate match
2733 quality. */
2734 if (method_oload_champ >= 0 && func_oload_champ >= 0)
2735 {
2736 switch (compare_badness (func_badness, method_badness))
2737 {
2738 case 0: /* Top two contenders are equally good. */
b021a221
MS
2739 /* FIXME: GDB does not support the general ambiguous case.
2740 All candidates should be collected and presented the
2741 user. */
4c3376c8
SW
2742 error (_("Ambiguous overload resolution"));
2743 break;
2744 case 1: /* Incomparable top contenders. */
2745 /* This is an error incompatible candidates
2746 should not have been proposed. */
3e43a32a
MS
2747 error (_("Internal error: incompatible "
2748 "overload candidates proposed"));
4c3376c8
SW
2749 break;
2750 case 2: /* Function champion. */
2751 method_oload_champ = -1;
2752 match_quality = func_match_quality;
2753 break;
2754 case 3: /* Method champion. */
2755 func_oload_champ = -1;
2756 match_quality = method_match_quality;
2757 break;
2758 default:
2759 error (_("Internal error: unexpected overload comparison result"));
2760 break;
2761 }
2762 }
2763 else
2764 {
2765 /* We have either a method match or a function match. */
2766 if (method_oload_champ >= 0)
2767 match_quality = method_match_quality;
2768 else
2769 match_quality = func_match_quality;
2770 }
8d577d32
DC
2771
2772 if (match_quality == INCOMPATIBLE)
2773 {
4c3376c8 2774 if (method == METHOD)
8a3fe4f8 2775 error (_("Cannot resolve method %s%s%s to any overloaded instance"),
8d577d32
DC
2776 obj_type_name,
2777 (obj_type_name && *obj_type_name) ? "::" : "",
2778 name);
2779 else
8a3fe4f8 2780 error (_("Cannot resolve function %s to any overloaded instance"),
8d577d32
DC
2781 func_name);
2782 }
2783 else if (match_quality == NON_STANDARD)
2784 {
4c3376c8 2785 if (method == METHOD)
3e43a32a
MS
2786 warning (_("Using non-standard conversion to match "
2787 "method %s%s%s to supplied arguments"),
8d577d32
DC
2788 obj_type_name,
2789 (obj_type_name && *obj_type_name) ? "::" : "",
2790 name);
2791 else
3e43a32a
MS
2792 warning (_("Using non-standard conversion to match "
2793 "function %s to supplied arguments"),
8d577d32
DC
2794 func_name);
2795 }
2796
4c3376c8 2797 if (staticp != NULL)
2bca57ba 2798 *staticp = oload_method_static_p (fns_ptr, method_oload_champ);
4c3376c8
SW
2799
2800 if (method_oload_champ >= 0)
8d577d32 2801 {
233e8b28
SC
2802 if (src_method_oload_champ >= 0)
2803 {
e66d4446
SC
2804 if (TYPE_FN_FIELD_VIRTUAL_P (fns_ptr, method_oload_champ)
2805 && noside != EVAL_AVOID_SIDE_EFFECTS)
2806 {
2807 *valp = value_virtual_fn_field (&temp, fns_ptr,
2808 method_oload_champ, basetype,
2809 boffset);
2810 }
233e8b28
SC
2811 else
2812 *valp = value_fn_field (&temp, fns_ptr, method_oload_champ,
2813 basetype, boffset);
2814 }
8d577d32 2815 else
ba18742c 2816 *valp = value_from_xmethod
f979c73f 2817 (std::move (xm_worker_vec[ext_method_oload_champ]));
8d577d32
DC
2818 }
2819 else
4c3376c8 2820 *symp = oload_syms[func_oload_champ];
8d577d32
DC
2821
2822 if (objp)
2823 {
a4295225 2824 struct type *temp_type = check_typedef (value_type (temp));
da096638 2825 struct type *objtype = check_typedef (obj_type);
a109c7c1 2826
a4295225 2827 if (TYPE_CODE (temp_type) != TYPE_CODE_PTR
da096638 2828 && (TYPE_CODE (objtype) == TYPE_CODE_PTR
aa006118 2829 || TYPE_IS_REFERENCE (objtype)))
8d577d32
DC
2830 {
2831 temp = value_addr (temp);
2832 }
2833 *objp = temp;
2834 }
7322dca9
SW
2835
2836 do_cleanups (all_cleanups);
8d577d32
DC
2837
2838 switch (match_quality)
2839 {
2840 case INCOMPATIBLE:
2841 return 100;
2842 case NON_STANDARD:
2843 return 10;
2844 default: /* STANDARD */
2845 return 0;
2846 }
2847}
2848
2849/* Find the best overload match, searching for FUNC_NAME in namespaces
2850 contained in QUALIFIED_NAME until it either finds a good match or
2851 runs out of namespaces. It stores the overloaded functions in
2852 *OLOAD_SYMS, and the badness vector in *OLOAD_CHAMP_BV. The
2853 calling function is responsible for freeing *OLOAD_SYMS and
7322dca9
SW
2854 *OLOAD_CHAMP_BV. If NO_ADL, argument dependent lookup is not
2855 performned. */
8d577d32
DC
2856
2857static int
da096638 2858find_oload_champ_namespace (struct value **args, int nargs,
8d577d32
DC
2859 const char *func_name,
2860 const char *qualified_name,
2861 struct symbol ***oload_syms,
7322dca9
SW
2862 struct badness_vector **oload_champ_bv,
2863 const int no_adl)
8d577d32
DC
2864{
2865 int oload_champ;
2866
da096638 2867 find_oload_champ_namespace_loop (args, nargs,
8d577d32
DC
2868 func_name,
2869 qualified_name, 0,
2870 oload_syms, oload_champ_bv,
7322dca9
SW
2871 &oload_champ,
2872 no_adl);
8d577d32
DC
2873
2874 return oload_champ;
2875}
2876
2877/* Helper function for find_oload_champ_namespace; NAMESPACE_LEN is
2878 how deep we've looked for namespaces, and the champ is stored in
2879 OLOAD_CHAMP. The return value is 1 if the champ is a good one, 0
7322dca9
SW
2880 if it isn't. Other arguments are the same as in
2881 find_oload_champ_namespace
8d577d32
DC
2882
2883 It is the caller's responsibility to free *OLOAD_SYMS and
2884 *OLOAD_CHAMP_BV. */
2885
2886static int
da096638 2887find_oload_champ_namespace_loop (struct value **args, int nargs,
8d577d32
DC
2888 const char *func_name,
2889 const char *qualified_name,
2890 int namespace_len,
2891 struct symbol ***oload_syms,
2892 struct badness_vector **oload_champ_bv,
7322dca9
SW
2893 int *oload_champ,
2894 const int no_adl)
8d577d32
DC
2895{
2896 int next_namespace_len = namespace_len;
2897 int searched_deeper = 0;
2898 int num_fns = 0;
2899 struct cleanup *old_cleanups;
2900 int new_oload_champ;
2901 struct symbol **new_oload_syms;
2902 struct badness_vector *new_oload_champ_bv;
2903 char *new_namespace;
2904
2905 if (next_namespace_len != 0)
2906 {
2907 gdb_assert (qualified_name[next_namespace_len] == ':');
2908 next_namespace_len += 2;
c906108c 2909 }
ac3eeb49
MS
2910 next_namespace_len +=
2911 cp_find_first_component (qualified_name + next_namespace_len);
8d577d32
DC
2912
2913 /* Initialize these to values that can safely be xfree'd. */
2914 *oload_syms = NULL;
2915 *oload_champ_bv = NULL;
c5aa993b 2916
581e13c1 2917 /* First, see if we have a deeper namespace we can search in.
ac3eeb49 2918 If we get a good match there, use it. */
8d577d32
DC
2919
2920 if (qualified_name[next_namespace_len] == ':')
2921 {
2922 searched_deeper = 1;
2923
da096638 2924 if (find_oload_champ_namespace_loop (args, nargs,
8d577d32
DC
2925 func_name, qualified_name,
2926 next_namespace_len,
2927 oload_syms, oload_champ_bv,
7322dca9 2928 oload_champ, no_adl))
8d577d32
DC
2929 {
2930 return 1;
2931 }
2932 };
2933
2934 /* If we reach here, either we're in the deepest namespace or we
2935 didn't find a good match in a deeper namespace. But, in the
2936 latter case, we still have a bad match in a deeper namespace;
2937 note that we might not find any match at all in the current
2938 namespace. (There's always a match in the deepest namespace,
2939 because this overload mechanism only gets called if there's a
2940 function symbol to start off with.) */
2941
2942 old_cleanups = make_cleanup (xfree, *oload_syms);
ec322823 2943 make_cleanup (xfree, *oload_champ_bv);
224c3ddb 2944 new_namespace = (char *) alloca (namespace_len + 1);
8d577d32
DC
2945 strncpy (new_namespace, qualified_name, namespace_len);
2946 new_namespace[namespace_len] = '\0';
2947 new_oload_syms = make_symbol_overload_list (func_name,
2948 new_namespace);
7322dca9
SW
2949
2950 /* If we have reached the deepest level perform argument
2951 determined lookup. */
2952 if (!searched_deeper && !no_adl)
da096638
KS
2953 {
2954 int ix;
2955 struct type **arg_types;
2956
2957 /* Prepare list of argument types for overload resolution. */
2958 arg_types = (struct type **)
2959 alloca (nargs * (sizeof (struct type *)));
2960 for (ix = 0; ix < nargs; ix++)
2961 arg_types[ix] = value_type (args[ix]);
2962 make_symbol_overload_list_adl (arg_types, nargs, func_name);
2963 }
7322dca9 2964
8d577d32
DC
2965 while (new_oload_syms[num_fns])
2966 ++num_fns;
2967
9cf95373 2968 new_oload_champ = find_oload_champ (args, nargs, num_fns,
233e8b28 2969 NULL, NULL, new_oload_syms,
8d577d32
DC
2970 &new_oload_champ_bv);
2971
2972 /* Case 1: We found a good match. Free earlier matches (if any),
2973 and return it. Case 2: We didn't find a good match, but we're
2974 not the deepest function. Then go with the bad match that the
2975 deeper function found. Case 3: We found a bad match, and we're
2976 the deepest function. Then return what we found, even though
2977 it's a bad match. */
2978
2979 if (new_oload_champ != -1
2980 && classify_oload_match (new_oload_champ_bv, nargs, 0) == STANDARD)
2981 {
2982 *oload_syms = new_oload_syms;
2983 *oload_champ = new_oload_champ;
2984 *oload_champ_bv = new_oload_champ_bv;
2985 do_cleanups (old_cleanups);
2986 return 1;
2987 }
2988 else if (searched_deeper)
2989 {
2990 xfree (new_oload_syms);
2991 xfree (new_oload_champ_bv);
2992 discard_cleanups (old_cleanups);
2993 return 0;
2994 }
2995 else
2996 {
8d577d32
DC
2997 *oload_syms = new_oload_syms;
2998 *oload_champ = new_oload_champ;
2999 *oload_champ_bv = new_oload_champ_bv;
2a7d6a25 3000 do_cleanups (old_cleanups);
8d577d32
DC
3001 return 0;
3002 }
3003}
3004
da096638 3005/* Look for a function to take NARGS args of ARGS. Find
8d577d32 3006 the best match from among the overloaded methods or functions
233e8b28
SC
3007 given by FNS_PTR or OLOAD_SYMS or XM_WORKER_VEC, respectively.
3008 One, and only one of FNS_PTR, OLOAD_SYMS and XM_WORKER_VEC can be
3009 non-NULL.
3010
3011 If XM_WORKER_VEC is NULL, then the length of the arrays FNS_PTR
3012 or OLOAD_SYMS (whichever is non-NULL) is specified in NUM_FNS.
3013
8d577d32
DC
3014 Return the index of the best match; store an indication of the
3015 quality of the match in OLOAD_CHAMP_BV.
3016
3017 It is the caller's responsibility to free *OLOAD_CHAMP_BV. */
3018
3019static int
9cf95373 3020find_oload_champ (struct value **args, int nargs,
8d577d32 3021 int num_fns, struct fn_field *fns_ptr,
ba18742c 3022 const std::vector<xmethod_worker_up> *xm_worker_vec,
8d577d32
DC
3023 struct symbol **oload_syms,
3024 struct badness_vector **oload_champ_bv)
3025{
3026 int ix;
ac3eeb49
MS
3027 /* A measure of how good an overloaded instance is. */
3028 struct badness_vector *bv;
3029 /* Index of best overloaded function. */
3030 int oload_champ = -1;
3031 /* Current ambiguity state for overload resolution. */
3032 int oload_ambiguous = 0;
3033 /* 0 => no ambiguity, 1 => two good funcs, 2 => incomparable funcs. */
8d577d32 3034
9cf95373 3035 /* A champion can be found among methods alone, or among functions
233e8b28
SC
3036 alone, or in xmethods alone, but not in more than one of these
3037 groups. */
3038 gdb_assert ((fns_ptr != NULL) + (oload_syms != NULL) + (xm_worker_vec != NULL)
3039 == 1);
9cf95373 3040
8d577d32 3041 *oload_champ_bv = NULL;
c906108c 3042
ba18742c
SM
3043 int fn_count = xm_worker_vec != NULL ? xm_worker_vec->size () : num_fns;
3044
ac3eeb49 3045 /* Consider each candidate in turn. */
233e8b28 3046 for (ix = 0; ix < fn_count; ix++)
c906108c 3047 {
8d577d32 3048 int jj;
233e8b28 3049 int static_offset = 0;
8d577d32
DC
3050 int nparms;
3051 struct type **parm_types;
3052
233e8b28 3053 if (xm_worker_vec != NULL)
db577aea 3054 {
ba18742c
SM
3055 xmethod_worker *worker = (*xm_worker_vec)[ix].get ();
3056 parm_types = worker->get_arg_types (&nparms);
db577aea
AC
3057 }
3058 else
3059 {
233e8b28
SC
3060 if (fns_ptr != NULL)
3061 {
3062 nparms = TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (fns_ptr, ix));
3063 static_offset = oload_method_static_p (fns_ptr, ix);
3064 }
3065 else
3066 nparms = TYPE_NFIELDS (SYMBOL_TYPE (oload_syms[ix]));
3067
8d749320 3068 parm_types = XNEWVEC (struct type *, nparms);
233e8b28
SC
3069 for (jj = 0; jj < nparms; jj++)
3070 parm_types[jj] = (fns_ptr != NULL
3071 ? (TYPE_FN_FIELD_ARGS (fns_ptr, ix)[jj].type)
3072 : TYPE_FIELD_TYPE (SYMBOL_TYPE (oload_syms[ix]),
8301c89e 3073 jj));
db577aea 3074 }
c906108c 3075
ac3eeb49
MS
3076 /* Compare parameter types to supplied argument types. Skip
3077 THIS for static methods. */
3078 bv = rank_function (parm_types, nparms,
da096638 3079 args + static_offset,
4a1970e4 3080 nargs - static_offset);
c5aa993b 3081
8d577d32 3082 if (!*oload_champ_bv)
c5aa993b 3083 {
8d577d32 3084 *oload_champ_bv = bv;
c5aa993b 3085 oload_champ = 0;
c5aa993b 3086 }
ac3eeb49
MS
3087 else /* See whether current candidate is better or worse than
3088 previous best. */
8d577d32 3089 switch (compare_badness (bv, *oload_champ_bv))
c5aa993b 3090 {
ac3eeb49
MS
3091 case 0: /* Top two contenders are equally good. */
3092 oload_ambiguous = 1;
c5aa993b 3093 break;
ac3eeb49
MS
3094 case 1: /* Incomparable top contenders. */
3095 oload_ambiguous = 2;
c5aa993b 3096 break;
ac3eeb49
MS
3097 case 2: /* New champion, record details. */
3098 *oload_champ_bv = bv;
c5aa993b
JM
3099 oload_ambiguous = 0;
3100 oload_champ = ix;
c5aa993b
JM
3101 break;
3102 case 3:
3103 default:
3104 break;
3105 }
b8c9b27d 3106 xfree (parm_types);
6b1ba9a0
ND
3107 if (overload_debug)
3108 {
233e8b28 3109 if (fns_ptr != NULL)
ac3eeb49 3110 fprintf_filtered (gdb_stderr,
3e43a32a 3111 "Overloaded method instance %s, # of parms %d\n",
ac3eeb49 3112 fns_ptr[ix].physname, nparms);
233e8b28
SC
3113 else if (xm_worker_vec != NULL)
3114 fprintf_filtered (gdb_stderr,
3115 "Xmethod worker, # of parms %d\n",
3116 nparms);
6b1ba9a0 3117 else
ac3eeb49 3118 fprintf_filtered (gdb_stderr,
3e43a32a
MS
3119 "Overloaded function instance "
3120 "%s # of parms %d\n",
ac3eeb49
MS
3121 SYMBOL_DEMANGLED_NAME (oload_syms[ix]),
3122 nparms);
4a1970e4 3123 for (jj = 0; jj < nargs - static_offset; jj++)
ac3eeb49
MS
3124 fprintf_filtered (gdb_stderr,
3125 "...Badness @ %d : %d\n",
6403aeea 3126 jj, bv->rank[jj].rank);
3e43a32a
MS
3127 fprintf_filtered (gdb_stderr, "Overload resolution "
3128 "champion is %d, ambiguous? %d\n",
ac3eeb49 3129 oload_champ, oload_ambiguous);
6b1ba9a0 3130 }
c906108c
SS
3131 }
3132
8d577d32
DC
3133 return oload_champ;
3134}
6b1ba9a0 3135
8d577d32
DC
3136/* Return 1 if we're looking at a static method, 0 if we're looking at
3137 a non-static method or a function that isn't a method. */
c906108c 3138
8d577d32 3139static int
2bca57ba 3140oload_method_static_p (struct fn_field *fns_ptr, int index)
8d577d32 3141{
2bca57ba 3142 if (fns_ptr && index >= 0 && TYPE_FN_FIELD_STATIC_P (fns_ptr, index))
8d577d32 3143 return 1;
c906108c 3144 else
8d577d32
DC
3145 return 0;
3146}
c906108c 3147
8d577d32
DC
3148/* Check how good an overload match OLOAD_CHAMP_BV represents. */
3149
3150static enum oload_classification
3151classify_oload_match (struct badness_vector *oload_champ_bv,
3152 int nargs,
3153 int static_offset)
3154{
3155 int ix;
da096638 3156 enum oload_classification worst = STANDARD;
8d577d32
DC
3157
3158 for (ix = 1; ix <= nargs - static_offset; ix++)
7f8c9282 3159 {
6403aeea
SW
3160 /* If this conversion is as bad as INCOMPATIBLE_TYPE_BADNESS
3161 or worse return INCOMPATIBLE. */
3162 if (compare_ranks (oload_champ_bv->rank[ix],
3163 INCOMPATIBLE_TYPE_BADNESS) <= 0)
ac3eeb49 3164 return INCOMPATIBLE; /* Truly mismatched types. */
6403aeea
SW
3165 /* Otherwise If this conversion is as bad as
3166 NS_POINTER_CONVERSION_BADNESS or worse return NON_STANDARD. */
3167 else if (compare_ranks (oload_champ_bv->rank[ix],
3168 NS_POINTER_CONVERSION_BADNESS) <= 0)
da096638 3169 worst = NON_STANDARD; /* Non-standard type conversions
ac3eeb49 3170 needed. */
7f8c9282 3171 }
02f0d45d 3172
da096638
KS
3173 /* If no INCOMPATIBLE classification was found, return the worst one
3174 that was found (if any). */
3175 return worst;
c906108c
SS
3176}
3177
ac3eeb49
MS
3178/* C++: return 1 is NAME is a legitimate name for the destructor of
3179 type TYPE. If TYPE does not have a destructor, or if NAME is
d8228535
JK
3180 inappropriate for TYPE, an error is signaled. Parameter TYPE should not yet
3181 have CHECK_TYPEDEF applied, this function will apply it itself. */
3182
c906108c 3183int
d8228535 3184destructor_name_p (const char *name, struct type *type)
c906108c 3185{
c906108c
SS
3186 if (name[0] == '~')
3187 {
a737d952 3188 const char *dname = type_name_or_error (type);
d8228535 3189 const char *cp = strchr (dname, '<');
c906108c
SS
3190 unsigned int len;
3191
3192 /* Do not compare the template part for template classes. */
3193 if (cp == NULL)
3194 len = strlen (dname);
3195 else
3196 len = cp - dname;
bf896cb0 3197 if (strlen (name + 1) != len || strncmp (dname, name + 1, len) != 0)
8a3fe4f8 3198 error (_("name of destructor must equal name of class"));
c906108c
SS
3199 else
3200 return 1;
3201 }
3202 return 0;
3203}
3204
3d567982
TT
3205/* Find an enum constant named NAME in TYPE. TYPE must be an "enum
3206 class". If the name is found, return a value representing it;
3207 otherwise throw an exception. */
3208
3209static struct value *
3210enum_constant_from_type (struct type *type, const char *name)
3211{
3212 int i;
3213 int name_len = strlen (name);
3214
3215 gdb_assert (TYPE_CODE (type) == TYPE_CODE_ENUM
3216 && TYPE_DECLARED_CLASS (type));
3217
3218 for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); ++i)
3219 {
3220 const char *fname = TYPE_FIELD_NAME (type, i);
3221 int len;
3222
3223 if (TYPE_FIELD_LOC_KIND (type, i) != FIELD_LOC_KIND_ENUMVAL
3224 || fname == NULL)
3225 continue;
3226
3227 /* Look for the trailing "::NAME", since enum class constant
3228 names are qualified here. */
3229 len = strlen (fname);
3230 if (len + 2 >= name_len
3231 && fname[len - name_len - 2] == ':'
3232 && fname[len - name_len - 1] == ':'
3233 && strcmp (&fname[len - name_len], name) == 0)
3234 return value_from_longest (type, TYPE_FIELD_ENUMVAL (type, i));
3235 }
3236
3237 error (_("no constant named \"%s\" in enum \"%s\""),
e86ca25f 3238 name, TYPE_NAME (type));
3d567982
TT
3239}
3240
79c2c32d 3241/* C++: Given an aggregate type CURTYPE, and a member name NAME,
0d5de010
DJ
3242 return the appropriate member (or the address of the member, if
3243 WANT_ADDRESS). This function is used to resolve user expressions
3244 of the form "DOMAIN::NAME". For more details on what happens, see
3245 the comment before value_struct_elt_for_reference. */
79c2c32d
DC
3246
3247struct value *
c848d642 3248value_aggregate_elt (struct type *curtype, const char *name,
072bba3b 3249 struct type *expect_type, int want_address,
79c2c32d
DC
3250 enum noside noside)
3251{
3252 switch (TYPE_CODE (curtype))
3253 {
3254 case TYPE_CODE_STRUCT:
3255 case TYPE_CODE_UNION:
ac3eeb49 3256 return value_struct_elt_for_reference (curtype, 0, curtype,
072bba3b 3257 name, expect_type,
0d5de010 3258 want_address, noside);
79c2c32d 3259 case TYPE_CODE_NAMESPACE:
ac3eeb49
MS
3260 return value_namespace_elt (curtype, name,
3261 want_address, noside);
3d567982
TT
3262
3263 case TYPE_CODE_ENUM:
3264 return enum_constant_from_type (curtype, name);
3265
79c2c32d
DC
3266 default:
3267 internal_error (__FILE__, __LINE__,
e2e0b3e5 3268 _("non-aggregate type in value_aggregate_elt"));
79c2c32d
DC
3269 }
3270}
3271
072bba3b 3272/* Compares the two method/function types T1 and T2 for "equality"
b021a221 3273 with respect to the methods' parameters. If the types of the
072bba3b
KS
3274 two parameter lists are the same, returns 1; 0 otherwise. This
3275 comparison may ignore any artificial parameters in T1 if
3276 SKIP_ARTIFICIAL is non-zero. This function will ALWAYS skip
3277 the first artificial parameter in T1, assumed to be a 'this' pointer.
3278
3279 The type T2 is expected to have come from make_params (in eval.c). */
3280
3281static int
3282compare_parameters (struct type *t1, struct type *t2, int skip_artificial)
3283{
3284 int start = 0;
3285
80b23b6a 3286 if (TYPE_NFIELDS (t1) > 0 && TYPE_FIELD_ARTIFICIAL (t1, 0))
072bba3b
KS
3287 ++start;
3288
3289 /* If skipping artificial fields, find the first real field
581e13c1 3290 in T1. */
072bba3b
KS
3291 if (skip_artificial)
3292 {
3293 while (start < TYPE_NFIELDS (t1)
3294 && TYPE_FIELD_ARTIFICIAL (t1, start))
3295 ++start;
3296 }
3297
581e13c1 3298 /* Now compare parameters. */
072bba3b
KS
3299
3300 /* Special case: a method taking void. T1 will contain no
3301 non-artificial fields, and T2 will contain TYPE_CODE_VOID. */
3302 if ((TYPE_NFIELDS (t1) - start) == 0 && TYPE_NFIELDS (t2) == 1
3303 && TYPE_CODE (TYPE_FIELD_TYPE (t2, 0)) == TYPE_CODE_VOID)
3304 return 1;
3305
3306 if ((TYPE_NFIELDS (t1) - start) == TYPE_NFIELDS (t2))
3307 {
3308 int i;
a109c7c1 3309
072bba3b
KS
3310 for (i = 0; i < TYPE_NFIELDS (t2); ++i)
3311 {
6403aeea 3312 if (compare_ranks (rank_one_type (TYPE_FIELD_TYPE (t1, start + i),
da096638 3313 TYPE_FIELD_TYPE (t2, i), NULL),
6403aeea 3314 EXACT_MATCH_BADNESS) != 0)
072bba3b
KS
3315 return 0;
3316 }
3317
3318 return 1;
3319 }
3320
3321 return 0;
3322}
3323
c906108c 3324/* C++: Given an aggregate type CURTYPE, and a member name NAME,
ac3eeb49
MS
3325 return the address of this member as a "pointer to member" type.
3326 If INTYPE is non-null, then it will be the type of the member we
3327 are looking for. This will help us resolve "pointers to member
3328 functions". This function is used to resolve user expressions of
3329 the form "DOMAIN::NAME". */
c906108c 3330
63d06c5c 3331static struct value *
fba45db2 3332value_struct_elt_for_reference (struct type *domain, int offset,
c848d642 3333 struct type *curtype, const char *name,
ac3eeb49
MS
3334 struct type *intype,
3335 int want_address,
63d06c5c 3336 enum noside noside)
c906108c 3337{
bf2977b5 3338 struct type *t = check_typedef (curtype);
52f0bd74 3339 int i;
0d5de010 3340 struct value *v, *result;
c906108c 3341
c5aa993b 3342 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
c906108c 3343 && TYPE_CODE (t) != TYPE_CODE_UNION)
3e43a32a
MS
3344 error (_("Internal error: non-aggregate type "
3345 "to value_struct_elt_for_reference"));
c906108c
SS
3346
3347 for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
3348 {
0d5cff50 3349 const char *t_field_name = TYPE_FIELD_NAME (t, i);
c5aa993b 3350
6314a349 3351 if (t_field_name && strcmp (t_field_name, name) == 0)
c906108c 3352 {
d6a843b5 3353 if (field_is_static (&TYPE_FIELD (t, i)))
c906108c
SS
3354 {
3355 v = value_static_field (t, i);
0d5de010
DJ
3356 if (want_address)
3357 v = value_addr (v);
c906108c
SS
3358 return v;
3359 }
3360 if (TYPE_FIELD_PACKED (t, i))
8a3fe4f8 3361 error (_("pointers to bitfield members not allowed"));
c5aa993b 3362
0d5de010
DJ
3363 if (want_address)
3364 return value_from_longest
3365 (lookup_memberptr_type (TYPE_FIELD_TYPE (t, i), domain),
3366 offset + (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3));
f7e3ecae 3367 else if (noside != EVAL_NORMAL)
0d5de010
DJ
3368 return allocate_value (TYPE_FIELD_TYPE (t, i));
3369 else
f7e3ecae
KS
3370 {
3371 /* Try to evaluate NAME as a qualified name with implicit
3372 this pointer. In this case, attempt to return the
3373 equivalent to `this->*(&TYPE::NAME)'. */
3374 v = value_of_this_silent (current_language);
3375 if (v != NULL)
3376 {
3377 struct value *ptr;
3378 long mem_offset;
3379 struct type *type, *tmp;
3380
3381 ptr = value_aggregate_elt (domain, name, NULL, 1, noside);
3382 type = check_typedef (value_type (ptr));
3383 gdb_assert (type != NULL
3384 && TYPE_CODE (type) == TYPE_CODE_MEMBERPTR);
4bfb94b8 3385 tmp = lookup_pointer_type (TYPE_SELF_TYPE (type));
f7e3ecae
KS
3386 v = value_cast_pointers (tmp, v, 1);
3387 mem_offset = value_as_long (ptr);
3388 tmp = lookup_pointer_type (TYPE_TARGET_TYPE (type));
3389 result = value_from_pointer (tmp,
3390 value_as_long (v) + mem_offset);
3391 return value_ind (result);
3392 }
3393
3394 error (_("Cannot reference non-static field \"%s\""), name);
3395 }
c906108c
SS
3396 }
3397 }
3398
ac3eeb49
MS
3399 /* C++: If it was not found as a data field, then try to return it
3400 as a pointer to a method. */
c906108c 3401
c906108c
SS
3402 /* Perform all necessary dereferencing. */
3403 while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR)
3404 intype = TYPE_TARGET_TYPE (intype);
3405
3406 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
3407 {
0d5cff50 3408 const char *t_field_name = TYPE_FN_FIELDLIST_NAME (t, i);
c906108c
SS
3409 char dem_opname[64];
3410
61012eef
GB
3411 if (startswith (t_field_name, "__")
3412 || startswith (t_field_name, "op")
3413 || startswith (t_field_name, "type"))
c906108c 3414 {
ac3eeb49
MS
3415 if (cplus_demangle_opname (t_field_name,
3416 dem_opname, DMGL_ANSI))
c5aa993b 3417 t_field_name = dem_opname;
ac3eeb49
MS
3418 else if (cplus_demangle_opname (t_field_name,
3419 dem_opname, 0))
c906108c 3420 t_field_name = dem_opname;
c906108c 3421 }
6314a349 3422 if (t_field_name && strcmp (t_field_name, name) == 0)
c906108c 3423 {
072bba3b
KS
3424 int j;
3425 int len = TYPE_FN_FIELDLIST_LENGTH (t, i);
c906108c 3426 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
c5aa993b 3427
de17c821
DJ
3428 check_stub_method_group (t, i);
3429
c906108c
SS
3430 if (intype)
3431 {
072bba3b
KS
3432 for (j = 0; j < len; ++j)
3433 {
3693fdb3
PA
3434 if (TYPE_CONST (intype) != TYPE_FN_FIELD_CONST (f, j))
3435 continue;
3436 if (TYPE_VOLATILE (intype) != TYPE_FN_FIELD_VOLATILE (f, j))
3437 continue;
3438
072bba3b 3439 if (compare_parameters (TYPE_FN_FIELD_TYPE (f, j), intype, 0)
3e43a32a
MS
3440 || compare_parameters (TYPE_FN_FIELD_TYPE (f, j),
3441 intype, 1))
072bba3b
KS
3442 break;
3443 }
3444
3445 if (j == len)
3e43a32a
MS
3446 error (_("no member function matches "
3447 "that type instantiation"));
7f79b1c5 3448 }
c906108c 3449 else
072bba3b
KS
3450 {
3451 int ii;
7f79b1c5
DJ
3452
3453 j = -1;
53832f31 3454 for (ii = 0; ii < len; ++ii)
072bba3b 3455 {
7f79b1c5
DJ
3456 /* Skip artificial methods. This is necessary if,
3457 for example, the user wants to "print
3458 subclass::subclass" with only one user-defined
53832f31
TT
3459 constructor. There is no ambiguity in this case.
3460 We are careful here to allow artificial methods
3461 if they are the unique result. */
072bba3b 3462 if (TYPE_FN_FIELD_ARTIFICIAL (f, ii))
53832f31
TT
3463 {
3464 if (j == -1)
3465 j = ii;
3466 continue;
3467 }
072bba3b 3468
7f79b1c5
DJ
3469 /* Desired method is ambiguous if more than one
3470 method is defined. */
53832f31 3471 if (j != -1 && !TYPE_FN_FIELD_ARTIFICIAL (f, j))
3e43a32a
MS
3472 error (_("non-unique member `%s' requires "
3473 "type instantiation"), name);
072bba3b 3474
7f79b1c5
DJ
3475 j = ii;
3476 }
53832f31
TT
3477
3478 if (j == -1)
3479 error (_("no matching member function"));
072bba3b 3480 }
c5aa993b 3481
0d5de010
DJ
3482 if (TYPE_FN_FIELD_STATIC_P (f, j))
3483 {
ac3eeb49
MS
3484 struct symbol *s =
3485 lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
d12307c1 3486 0, VAR_DOMAIN, 0).symbol;
a109c7c1 3487
0d5de010
DJ
3488 if (s == NULL)
3489 return NULL;
3490
3491 if (want_address)
63e43d3a 3492 return value_addr (read_var_value (s, 0, 0));
0d5de010 3493 else
63e43d3a 3494 return read_var_value (s, 0, 0);
0d5de010
DJ
3495 }
3496
c906108c
SS
3497 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
3498 {
0d5de010
DJ
3499 if (want_address)
3500 {
3501 result = allocate_value
3502 (lookup_methodptr_type (TYPE_FN_FIELD_TYPE (f, j)));
ad4820ab
UW
3503 cplus_make_method_ptr (value_type (result),
3504 value_contents_writeable (result),
0d5de010
DJ
3505 TYPE_FN_FIELD_VOFFSET (f, j), 1);
3506 }
3507 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
3508 return allocate_value (TYPE_FN_FIELD_TYPE (f, j));
3509 else
3510 error (_("Cannot reference virtual member function \"%s\""),
3511 name);
c906108c
SS
3512 }
3513 else
3514 {
ac3eeb49
MS
3515 struct symbol *s =
3516 lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
d12307c1 3517 0, VAR_DOMAIN, 0).symbol;
a109c7c1 3518
c906108c 3519 if (s == NULL)
0d5de010
DJ
3520 return NULL;
3521
63e43d3a 3522 v = read_var_value (s, 0, 0);
0d5de010
DJ
3523 if (!want_address)
3524 result = v;
c906108c
SS
3525 else
3526 {
0d5de010 3527 result = allocate_value (lookup_methodptr_type (TYPE_FN_FIELD_TYPE (f, j)));
ad4820ab
UW
3528 cplus_make_method_ptr (value_type (result),
3529 value_contents_writeable (result),
42ae5230 3530 value_address (v), 0);
c906108c 3531 }
c906108c 3532 }
0d5de010 3533 return result;
c906108c
SS
3534 }
3535 }
3536 for (i = TYPE_N_BASECLASSES (t) - 1; i >= 0; i--)
3537 {
f23631e4 3538 struct value *v;
c906108c
SS
3539 int base_offset;
3540
3541 if (BASETYPE_VIA_VIRTUAL (t, i))
3542 base_offset = 0;
3543 else
3544 base_offset = TYPE_BASECLASS_BITPOS (t, i) / 8;
3545 v = value_struct_elt_for_reference (domain,
3546 offset + base_offset,
3547 TYPE_BASECLASS (t, i),
ac3eeb49
MS
3548 name, intype,
3549 want_address, noside);
c906108c
SS
3550 if (v)
3551 return v;
3552 }
63d06c5c
DC
3553
3554 /* As a last chance, pretend that CURTYPE is a namespace, and look
3555 it up that way; this (frequently) works for types nested inside
3556 classes. */
3557
ac3eeb49
MS
3558 return value_maybe_namespace_elt (curtype, name,
3559 want_address, noside);
c906108c
SS
3560}
3561
79c2c32d
DC
3562/* C++: Return the member NAME of the namespace given by the type
3563 CURTYPE. */
3564
3565static struct value *
3566value_namespace_elt (const struct type *curtype,
c848d642 3567 const char *name, int want_address,
79c2c32d 3568 enum noside noside)
63d06c5c
DC
3569{
3570 struct value *retval = value_maybe_namespace_elt (curtype, name,
ac3eeb49
MS
3571 want_address,
3572 noside);
63d06c5c
DC
3573
3574 if (retval == NULL)
ac3eeb49 3575 error (_("No symbol \"%s\" in namespace \"%s\"."),
e86ca25f 3576 name, TYPE_NAME (curtype));
63d06c5c
DC
3577
3578 return retval;
3579}
3580
3581/* A helper function used by value_namespace_elt and
3582 value_struct_elt_for_reference. It looks up NAME inside the
3583 context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
3584 is a class and NAME refers to a type in CURTYPE itself (as opposed
3585 to, say, some base class of CURTYPE). */
3586
3587static struct value *
3588value_maybe_namespace_elt (const struct type *curtype,
c848d642 3589 const char *name, int want_address,
63d06c5c 3590 enum noside noside)
79c2c32d 3591{
e86ca25f 3592 const char *namespace_name = TYPE_NAME (curtype);
d12307c1 3593 struct block_symbol sym;
0d5de010 3594 struct value *result;
79c2c32d 3595
13387711 3596 sym = cp_lookup_symbol_namespace (namespace_name, name,
41f62f39
JK
3597 get_selected_block (0), VAR_DOMAIN);
3598
d12307c1 3599 if (sym.symbol == NULL)
63d06c5c 3600 return NULL;
79c2c32d 3601 else if ((noside == EVAL_AVOID_SIDE_EFFECTS)
d12307c1
PMR
3602 && (SYMBOL_CLASS (sym.symbol) == LOC_TYPEDEF))
3603 result = allocate_value (SYMBOL_TYPE (sym.symbol));
79c2c32d 3604 else
d12307c1 3605 result = value_of_variable (sym.symbol, sym.block);
0d5de010 3606
ae6a105d 3607 if (want_address)
0d5de010
DJ
3608 result = value_addr (result);
3609
3610 return result;
79c2c32d
DC
3611}
3612
dfcee124 3613/* Given a pointer or a reference value V, find its real (RTTI) type.
ac3eeb49 3614
c906108c 3615 Other parameters FULL, TOP, USING_ENC as with value_rtti_type()
ac3eeb49 3616 and refer to the values computed for the object pointed to. */
c906108c
SS
3617
3618struct type *
dfcee124 3619value_rtti_indirect_type (struct value *v, int *full,
6b850546 3620 LONGEST *top, int *using_enc)
c906108c 3621{
f7e5394d 3622 struct value *target = NULL;
dfcee124
AG
3623 struct type *type, *real_type, *target_type;
3624
3625 type = value_type (v);
3626 type = check_typedef (type);
aa006118 3627 if (TYPE_IS_REFERENCE (type))
dfcee124
AG
3628 target = coerce_ref (v);
3629 else if (TYPE_CODE (type) == TYPE_CODE_PTR)
f7e5394d 3630 {
f7e5394d 3631
492d29ea 3632 TRY
f7e5394d
SM
3633 {
3634 target = value_ind (v);
3635 }
492d29ea 3636 CATCH (except, RETURN_MASK_ERROR)
f7e5394d
SM
3637 {
3638 if (except.error == MEMORY_ERROR)
3639 {
3640 /* value_ind threw a memory error. The pointer is NULL or
3641 contains an uninitialized value: we can't determine any
3642 type. */
3643 return NULL;
3644 }
3645 throw_exception (except);
3646 }
492d29ea 3647 END_CATCH
f7e5394d 3648 }
dfcee124
AG
3649 else
3650 return NULL;
c906108c 3651
dfcee124
AG
3652 real_type = value_rtti_type (target, full, top, using_enc);
3653
3654 if (real_type)
3655 {
3656 /* Copy qualifiers to the referenced object. */
3657 target_type = value_type (target);
3658 real_type = make_cv_type (TYPE_CONST (target_type),
3659 TYPE_VOLATILE (target_type), real_type, NULL);
aa006118
AV
3660 if (TYPE_IS_REFERENCE (type))
3661 real_type = lookup_reference_type (real_type, TYPE_CODE (type));
dfcee124
AG
3662 else if (TYPE_CODE (type) == TYPE_CODE_PTR)
3663 real_type = lookup_pointer_type (real_type);
3664 else
3665 internal_error (__FILE__, __LINE__, _("Unexpected value type."));
3666
3667 /* Copy qualifiers to the pointer/reference. */
3668 real_type = make_cv_type (TYPE_CONST (type), TYPE_VOLATILE (type),
3669 real_type, NULL);
3670 }
c906108c 3671
dfcee124 3672 return real_type;
c906108c
SS
3673}
3674
3675/* Given a value pointed to by ARGP, check its real run-time type, and
3676 if that is different from the enclosing type, create a new value
3677 using the real run-time type as the enclosing type (and of the same
3678 type as ARGP) and return it, with the embedded offset adjusted to
ac3eeb49
MS
3679 be the correct offset to the enclosed object. RTYPE is the type,
3680 and XFULL, XTOP, and XUSING_ENC are the other parameters, computed
3681 by value_rtti_type(). If these are available, they can be supplied
3682 and a second call to value_rtti_type() is avoided. (Pass RTYPE ==
3683 NULL if they're not available. */
c906108c 3684
f23631e4 3685struct value *
ac3eeb49
MS
3686value_full_object (struct value *argp,
3687 struct type *rtype,
3688 int xfull, int xtop,
fba45db2 3689 int xusing_enc)
c906108c 3690{
c5aa993b 3691 struct type *real_type;
c906108c 3692 int full = 0;
6b850546 3693 LONGEST top = -1;
c906108c 3694 int using_enc = 0;
f23631e4 3695 struct value *new_val;
c906108c
SS
3696
3697 if (rtype)
3698 {
3699 real_type = rtype;
3700 full = xfull;
3701 top = xtop;
3702 using_enc = xusing_enc;
3703 }
3704 else
3705 real_type = value_rtti_type (argp, &full, &top, &using_enc);
3706
ac3eeb49 3707 /* If no RTTI data, or if object is already complete, do nothing. */
4754a64e 3708 if (!real_type || real_type == value_enclosing_type (argp))
c906108c
SS
3709 return argp;
3710
a7860e76
TT
3711 /* In a destructor we might see a real type that is a superclass of
3712 the object's type. In this case it is better to leave the object
3713 as-is. */
3714 if (full
3715 && TYPE_LENGTH (real_type) < TYPE_LENGTH (value_enclosing_type (argp)))
3716 return argp;
3717
c906108c 3718 /* If we have the full object, but for some reason the enclosing
ac3eeb49
MS
3719 type is wrong, set it. */
3720 /* pai: FIXME -- sounds iffy */
c906108c
SS
3721 if (full)
3722 {
4dfea560
DE
3723 argp = value_copy (argp);
3724 set_value_enclosing_type (argp, real_type);
c906108c
SS
3725 return argp;
3726 }
3727
581e13c1 3728 /* Check if object is in memory. */
c906108c
SS
3729 if (VALUE_LVAL (argp) != lval_memory)
3730 {
3e43a32a
MS
3731 warning (_("Couldn't retrieve complete object of RTTI "
3732 "type %s; object may be in register(s)."),
ac3eeb49 3733 TYPE_NAME (real_type));
c5aa993b 3734
c906108c
SS
3735 return argp;
3736 }
c5aa993b 3737
ac3eeb49
MS
3738 /* All other cases -- retrieve the complete object. */
3739 /* Go back by the computed top_offset from the beginning of the
3740 object, adjusting for the embedded offset of argp if that's what
3741 value_rtti_type used for its computation. */
42ae5230 3742 new_val = value_at_lazy (real_type, value_address (argp) - top +
13c3b5f5 3743 (using_enc ? 0 : value_embedded_offset (argp)));
04624583 3744 deprecated_set_value_type (new_val, value_type (argp));
13c3b5f5
AC
3745 set_value_embedded_offset (new_val, (using_enc
3746 ? top + value_embedded_offset (argp)
3747 : top));
c906108c
SS
3748 return new_val;
3749}
3750
389e51db 3751
85bc8cb7
JK
3752/* Return the value of the local variable, if one exists. Throw error
3753 otherwise, such as if the request is made in an inappropriate context. */
c906108c 3754
f23631e4 3755struct value *
85bc8cb7 3756value_of_this (const struct language_defn *lang)
c906108c 3757{
63e43d3a 3758 struct block_symbol sym;
3977b71f 3759 const struct block *b;
206415a3 3760 struct frame_info *frame;
c906108c 3761
66a17cb6 3762 if (!lang->la_name_of_this)
85bc8cb7 3763 error (_("no `this' in current language"));
aee28ec6 3764
85bc8cb7 3765 frame = get_selected_frame (_("no frame selected"));
c906108c 3766
66a17cb6 3767 b = get_frame_block (frame, NULL);
c906108c 3768
63e43d3a
PMR
3769 sym = lookup_language_this (lang, b);
3770 if (sym.symbol == NULL)
85bc8cb7
JK
3771 error (_("current stack frame does not contain a variable named `%s'"),
3772 lang->la_name_of_this);
3773
63e43d3a 3774 return read_var_value (sym.symbol, sym.block, frame);
85bc8cb7
JK
3775}
3776
3777/* Return the value of the local variable, if one exists. Return NULL
3778 otherwise. Never throw error. */
3779
3780struct value *
3781value_of_this_silent (const struct language_defn *lang)
3782{
3783 struct value *ret = NULL;
85bc8cb7 3784
492d29ea 3785 TRY
c906108c 3786 {
85bc8cb7 3787 ret = value_of_this (lang);
c906108c 3788 }
492d29ea
PA
3789 CATCH (except, RETURN_MASK_ERROR)
3790 {
3791 }
3792 END_CATCH
c906108c 3793
d069f99d
AF
3794 return ret;
3795}
3796
ac3eeb49
MS
3797/* Create a slice (sub-string, sub-array) of ARRAY, that is LENGTH
3798 elements long, starting at LOWBOUND. The result has the same lower
3799 bound as the original ARRAY. */
c906108c 3800
f23631e4
AC
3801struct value *
3802value_slice (struct value *array, int lowbound, int length)
c906108c
SS
3803{
3804 struct type *slice_range_type, *slice_type, *range_type;
7a67d0fe 3805 LONGEST lowerbound, upperbound;
f23631e4 3806 struct value *slice;
c906108c 3807 struct type *array_type;
ac3eeb49 3808
df407dfe 3809 array_type = check_typedef (value_type (array));
c906108c 3810 if (TYPE_CODE (array_type) != TYPE_CODE_ARRAY
6b1755ce 3811 && TYPE_CODE (array_type) != TYPE_CODE_STRING)
8a3fe4f8 3812 error (_("cannot take slice of non-array"));
ac3eeb49 3813
c906108c
SS
3814 range_type = TYPE_INDEX_TYPE (array_type);
3815 if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0)
8a3fe4f8 3816 error (_("slice from bad array or bitstring"));
ac3eeb49 3817
c906108c 3818 if (lowbound < lowerbound || length < 0
db034ac5 3819 || lowbound + length - 1 > upperbound)
8a3fe4f8 3820 error (_("slice out of range"));
ac3eeb49 3821
c906108c
SS
3822 /* FIXME-type-allocation: need a way to free this type when we are
3823 done with it. */
0c9c3474
SA
3824 slice_range_type = create_static_range_type ((struct type *) NULL,
3825 TYPE_TARGET_TYPE (range_type),
3826 lowbound,
3827 lowbound + length - 1);
ac3eeb49 3828
a7c88acd
JB
3829 {
3830 struct type *element_type = TYPE_TARGET_TYPE (array_type);
3831 LONGEST offset
3832 = (lowbound - lowerbound) * TYPE_LENGTH (check_typedef (element_type));
ac3eeb49 3833
a7c88acd
JB
3834 slice_type = create_array_type ((struct type *) NULL,
3835 element_type,
3836 slice_range_type);
3837 TYPE_CODE (slice_type) = TYPE_CODE (array_type);
ac3eeb49 3838
a7c88acd
JB
3839 if (VALUE_LVAL (array) == lval_memory && value_lazy (array))
3840 slice = allocate_value_lazy (slice_type);
3841 else
3842 {
3843 slice = allocate_value (slice_type);
3844 value_contents_copy (slice, 0, array, offset,
3ae385af 3845 type_length_units (slice_type));
a7c88acd
JB
3846 }
3847
3848 set_value_component_location (slice, array);
a7c88acd
JB
3849 set_value_offset (slice, value_offset (array) + offset);
3850 }
ac3eeb49 3851
c906108c
SS
3852 return slice;
3853}
3854
ac3eeb49
MS
3855/* Create a value for a FORTRAN complex number. Currently most of the
3856 time values are coerced to COMPLEX*16 (i.e. a complex number
070ad9f0
DB
3857 composed of 2 doubles. This really should be a smarter routine
3858 that figures out precision inteligently as opposed to assuming
ac3eeb49 3859 doubles. FIXME: fmb */
c906108c 3860
f23631e4 3861struct value *
ac3eeb49
MS
3862value_literal_complex (struct value *arg1,
3863 struct value *arg2,
3864 struct type *type)
c906108c 3865{
f23631e4 3866 struct value *val;
c906108c
SS
3867 struct type *real_type = TYPE_TARGET_TYPE (type);
3868
3869 val = allocate_value (type);
3870 arg1 = value_cast (real_type, arg1);
3871 arg2 = value_cast (real_type, arg2);
3872
990a07ab 3873 memcpy (value_contents_raw (val),
0fd88904 3874 value_contents (arg1), TYPE_LENGTH (real_type));
990a07ab 3875 memcpy (value_contents_raw (val) + TYPE_LENGTH (real_type),
0fd88904 3876 value_contents (arg2), TYPE_LENGTH (real_type));
c906108c
SS
3877 return val;
3878}
3879
ac3eeb49 3880/* Cast a value into the appropriate complex data type. */
c906108c 3881
f23631e4
AC
3882static struct value *
3883cast_into_complex (struct type *type, struct value *val)
c906108c
SS
3884{
3885 struct type *real_type = TYPE_TARGET_TYPE (type);
ac3eeb49 3886
df407dfe 3887 if (TYPE_CODE (value_type (val)) == TYPE_CODE_COMPLEX)
c906108c 3888 {
df407dfe 3889 struct type *val_real_type = TYPE_TARGET_TYPE (value_type (val));
f23631e4
AC
3890 struct value *re_val = allocate_value (val_real_type);
3891 struct value *im_val = allocate_value (val_real_type);
c906108c 3892
990a07ab 3893 memcpy (value_contents_raw (re_val),
0fd88904 3894 value_contents (val), TYPE_LENGTH (val_real_type));
990a07ab 3895 memcpy (value_contents_raw (im_val),
0fd88904 3896 value_contents (val) + TYPE_LENGTH (val_real_type),
c5aa993b 3897 TYPE_LENGTH (val_real_type));
c906108c
SS
3898
3899 return value_literal_complex (re_val, im_val, type);
3900 }
df407dfe
AC
3901 else if (TYPE_CODE (value_type (val)) == TYPE_CODE_FLT
3902 || TYPE_CODE (value_type (val)) == TYPE_CODE_INT)
ac3eeb49
MS
3903 return value_literal_complex (val,
3904 value_zero (real_type, not_lval),
3905 type);
c906108c 3906 else
8a3fe4f8 3907 error (_("cannot cast non-number to complex"));
c906108c
SS
3908}
3909
3910void
fba45db2 3911_initialize_valops (void)
c906108c 3912{
5bf193a2
AC
3913 add_setshow_boolean_cmd ("overload-resolution", class_support,
3914 &overload_resolution, _("\
3915Set overload resolution in evaluating C++ functions."), _("\
ac3eeb49
MS
3916Show overload resolution in evaluating C++ functions."),
3917 NULL, NULL,
920d2a44 3918 show_overload_resolution,
5bf193a2 3919 &setlist, &showlist);
c906108c 3920 overload_resolution = 1;
c906108c 3921}
This page took 2.27025 seconds and 4 git commands to generate.