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