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