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