1 /* Perform non-arithmetic operations on values, for GDB.
3 Copyright (C) 1986-2018 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
35 #include "dictionary.h"
36 #include "cp-support.h"
37 #include "target-float.h"
38 #include "tracepoint.h"
39 #include "observable.h"
41 #include "extension.h"
42 #include "byte-vector.h"
44 extern unsigned int overload_debug;
45 /* Local functions. */
47 static int typecmp (int staticp, int varargs, int nargs,
48 struct field t1[], struct value *t2[]);
50 static struct value *search_struct_field (const char *, struct value *,
53 static struct value *search_struct_method (const char *, struct value **,
55 LONGEST, int *, struct type *);
57 static int find_oload_champ_namespace (struct value **, int,
58 const char *, const char *,
60 struct badness_vector **,
64 int find_oload_champ_namespace_loop (struct value **, int,
65 const char *, const char *,
66 int, struct symbol ***,
67 struct badness_vector **, int *,
70 static int find_oload_champ (struct value **, int, int,
72 const std::vector<xmethod_worker_up> *,
73 struct symbol **, struct badness_vector **);
75 static int oload_method_static_p (struct fn_field *, int);
77 enum oload_classification { STANDARD, NON_STANDARD, INCOMPATIBLE };
80 oload_classification classify_oload_match (struct badness_vector *,
83 static struct value *value_struct_elt_for_reference (struct type *,
89 static struct value *value_namespace_elt (const struct type *,
90 const char *, int , enum noside);
92 static struct value *value_maybe_namespace_elt (const struct type *,
96 static CORE_ADDR allocate_space_in_inferior (int);
98 static struct value *cast_into_complex (struct type *, struct value *);
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 *);
105 int overload_resolution = 0;
107 show_overload_resolution (struct ui_file *file, int from_tty,
108 struct cmd_list_element *c,
111 fprintf_filtered (file, _("Overload resolution in evaluating "
112 "C++ functions is %s.\n"),
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
121 find_function_in_inferior (const char *name, struct objfile **objf_p)
123 struct block_symbol sym;
125 sym = lookup_symbol (name, 0, VAR_DOMAIN, 0);
126 if (sym.symbol != NULL)
128 if (SYMBOL_CLASS (sym.symbol) != LOC_BLOCK)
130 error (_("\"%s\" exists in this program but is not a function."),
135 *objf_p = symbol_objfile (sym.symbol);
137 return value_of_variable (sym.symbol, sym.block);
141 struct bound_minimal_symbol msymbol =
142 lookup_bound_minimal_symbol (name);
144 if (msymbol.minsym != NULL)
146 struct objfile *objfile = msymbol.objfile;
147 struct gdbarch *gdbarch = get_objfile_arch (objfile);
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);
159 return value_from_pointer (type, maddr);
163 if (!target_has_execution)
164 error (_("evaluation of this expression "
165 "requires the target program to be active"));
167 error (_("evaluation of this expression requires the "
168 "program to have a function \"%s\"."),
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
179 value_allocate_space_in_inferior (int len)
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;
186 blocklen = value_from_longest (builtin_type (gdbarch)->builtin_int, len);
187 val = call_function_by_hand (val, NULL, 1, &blocklen);
188 if (value_logical_not (val))
190 if (!target_has_execution)
191 error (_("No memory available to program now: "
192 "you need to start the target first"));
194 error (_("No memory available to program: call to malloc failed"));
200 allocate_space_in_inferior (int len)
202 return value_as_long (value_allocate_space_in_inferior (len));
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. */
210 static struct value *
211 value_cast_structs (struct type *type, struct value *v2)
217 gdb_assert (type != NULL && v2 != NULL);
219 t1 = check_typedef (type);
220 t2 = check_typedef (value_type (v2));
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");
230 if (TYPE_NAME (t1) != NULL
231 && TYPE_NAME (t2) != NULL
232 && !strcmp (TYPE_NAME (t1), TYPE_NAME (t2)))
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)
240 v = search_struct_field (TYPE_NAME (t1),
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)
251 /* Try downcasting using the run-time type of the value. */
254 struct type *real_type;
256 real_type = value_rtti_type (v2, &full, &top, &using_enc);
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);
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)))
269 v = search_struct_field (TYPE_NAME (t2), v, real_type, 1);
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);
281 /* Downcasting is possible (t1 is superclass of v2). */
282 CORE_ADDR addr2 = value_address (v2);
284 addr2 -= value_address (v) + value_embedded_offset (v);
285 return value_at (type, addr2);
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. */
300 value_cast_pointers (struct type *type, struct value *arg2,
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));
308 if (TYPE_CODE (t1) == TYPE_CODE_STRUCT
309 && TYPE_CODE (t2) == TYPE_CODE_STRUCT
310 && (subclass_check || !value_logical_not (arg2)))
314 if (TYPE_IS_REFERENCE (type2))
315 v2 = coerce_ref (arg2);
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. */
324 struct value *v = value_addr (v2);
326 deprecated_set_value_type (v, type);
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 */
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. */
345 value_cast (struct type *type, struct value *arg2)
347 enum type_code code1;
348 enum type_code code2;
352 int convert_to_boolean = 0;
354 if (value_type (arg2) == type)
357 /* Check if we are casting struct reference to struct reference. */
358 if (TYPE_IS_REFERENCE (check_typedef (type)))
360 /* We dereference type; then we recurse and finally
361 we generate value of the given reference. Nothing wrong with
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);
367 return value_ref (val, TYPE_CODE (t1));
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));
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;
379 type = check_typedef (type);
380 code1 = TYPE_CODE (type);
381 arg2 = coerce_ref (arg2);
382 type2 = check_typedef (value_type (arg2));
384 /* You can't cast to a reference type. See value_cast_pointers
386 gdb_assert (!TYPE_IS_REFERENCE (type));
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)
393 struct type *element_type = TYPE_TARGET_TYPE (type);
394 unsigned element_length = TYPE_LENGTH (check_typedef (element_type));
396 if (element_length > 0 && TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
398 struct type *range_type = TYPE_INDEX_TYPE (type);
399 int val_length = TYPE_LENGTH (type2);
400 LONGEST low_bound, high_bound, new_length;
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),
413 new_length + low_bound - 1);
414 deprecated_set_value_type (arg2,
415 create_array_type ((struct type *) NULL,
422 if (current_language->c_style_arrays
423 && TYPE_CODE (type2) == TYPE_CODE_ARRAY
424 && !TYPE_VECTOR (type2))
425 arg2 = value_coerce_array (arg2);
427 if (TYPE_CODE (type2) == TYPE_CODE_FUNC)
428 arg2 = value_coerce_function (arg2);
430 type2 = check_typedef (value_type (arg2));
431 code2 = TYPE_CODE (type2);
433 if (code1 == TYPE_CODE_COMPLEX)
434 return cast_into_complex (to_type, arg2);
435 if (code1 == TYPE_CODE_BOOL)
437 code1 = TYPE_CODE_INT;
438 convert_to_boolean = 1;
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;
445 scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
446 || code2 == TYPE_CODE_DECFLOAT || code2 == TYPE_CODE_ENUM
447 || code2 == TYPE_CODE_RANGE);
449 if ((code1 == TYPE_CODE_STRUCT || code1 == TYPE_CODE_UNION)
450 && (code2 == TYPE_CODE_STRUCT || code2 == TYPE_CODE_UNION)
451 && TYPE_NAME (type) != 0)
453 struct value *v = value_cast_structs (to_type, arg2);
459 if (is_floating_type (type) && scalar)
461 if (is_floating_value (arg2))
463 struct value *v = allocate_value (to_type);
464 target_float_convert (value_contents (arg2), type2,
465 value_contents_raw (v), type);
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));
473 return value_from_longest (to_type, value_as_long (arg2));
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))
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
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)));
493 longest = value_as_long (arg2);
494 return value_from_longest (to_type, convert_to_boolean ?
495 (LONGEST) (longest ? 1 : 0) : longest);
497 else if (code1 == TYPE_CODE_PTR && (code2 == TYPE_CODE_INT
498 || code2 == TYPE_CODE_ENUM
499 || code2 == TYPE_CODE_RANGE))
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.
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. */
511 int addr_bit = gdbarch_addr_bit (get_type_arch (type2));
512 LONGEST longest = value_as_long (arg2);
514 if (addr_bit < sizeof (LONGEST) * HOST_CHAR_BIT)
516 if (longest >= ((LONGEST) 1 << addr_bit)
517 || longest <= -((LONGEST) 1 << addr_bit))
518 warning (_("value truncated"));
520 return value_from_longest (to_type, longest);
522 else if (code1 == TYPE_CODE_METHODPTR && code2 == TYPE_CODE_INT
523 && value_as_long (arg2) == 0)
525 struct value *result = allocate_value (to_type);
527 cplus_make_method_ptr (to_type, value_contents_writeable (result), 0, 0);
530 else if (code1 == TYPE_CODE_MEMBERPTR && code2 == TYPE_CODE_INT
531 && value_as_long (arg2) == 0)
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);
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)
546 return value_zero (to_type, not_lval);
548 else if (TYPE_LENGTH (type) == TYPE_LENGTH (type2))
550 if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
551 return value_cast_pointers (to_type, arg2, 0);
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 */
559 else if (VALUE_LVAL (arg2) == lval_memory)
560 return value_at_lazy (to_type, value_address (arg2));
563 error (_("Invalid cast."));
568 /* The C++ reinterpret_cast operator. */
571 value_reinterpret_cast (struct type *type, struct value *arg)
573 struct value *result;
574 struct type *real_type = check_typedef (type);
575 struct type *arg_type, *dest_type;
577 enum type_code dest_code, arg_code;
579 /* Do reference, function, and array conversion. */
580 arg = coerce_array (arg);
582 /* Attempt to preserve the type the user asked for. */
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))
590 arg = value_addr (arg);
591 dest_type = lookup_pointer_type (TYPE_TARGET_TYPE (dest_type));
592 real_type = lookup_pointer_type (real_type);
595 arg_type = value_type (arg);
597 dest_code = TYPE_CODE (real_type);
598 arg_code = TYPE_CODE (arg_type);
600 /* We can convert pointer types, or any pointer type to int, or int
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);
614 error (_("Invalid reinterpret_cast"));
617 result = value_cast (type, value_ref (value_ind (result),
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. */
629 dynamic_cast_check_1 (struct type *desired_type,
630 const gdb_byte *valaddr,
631 LONGEST embedded_offset,
634 struct type *search_type,
636 struct type *arg_type,
637 struct value **result)
639 int i, result_count = 0;
641 for (i = 0; i < TYPE_N_BASECLASSES (search_type) && result_count < 2; ++i)
643 LONGEST offset = baseclass_offset (search_type, i, valaddr,
647 if (class_types_same_p (desired_type, TYPE_BASECLASS (search_type, i)))
649 if (address + embedded_offset + offset >= arg_addr
650 && address + embedded_offset + offset < arg_addr + TYPE_LENGTH (arg_type))
654 *result = value_at_lazy (TYPE_BASECLASS (search_type, i),
655 address + embedded_offset + offset);
659 result_count += dynamic_cast_check_1 (desired_type,
661 embedded_offset + offset,
663 TYPE_BASECLASS (search_type, i),
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. */
677 dynamic_cast_check_2 (struct type *desired_type,
678 const gdb_byte *valaddr,
679 LONGEST embedded_offset,
682 struct type *search_type,
683 struct value **result)
685 int i, result_count = 0;
687 for (i = 0; i < TYPE_N_BASECLASSES (search_type) && result_count < 2; ++i)
691 if (! BASETYPE_VIA_PUBLIC (search_type, i))
694 offset = baseclass_offset (search_type, i, valaddr, embedded_offset,
696 if (class_types_same_p (desired_type, TYPE_BASECLASS (search_type, i)))
700 *result = value_at_lazy (TYPE_BASECLASS (search_type, i),
701 address + embedded_offset + offset);
704 result_count += dynamic_cast_check_2 (desired_type,
706 embedded_offset + offset,
708 TYPE_BASECLASS (search_type, i),
715 /* The C++ dynamic_cast operator. */
718 value_dynamic_cast (struct type *type, struct value *arg)
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;
727 int is_ref = TYPE_IS_REFERENCE (resolved_type);
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 *'"));
736 class_type = check_typedef (TYPE_TARGET_TYPE (resolved_type));
737 if (TYPE_CODE (resolved_type) == TYPE_CODE_PTR)
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)
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"));
751 /* Handle NULL pointers. */
752 if (value_as_long (arg) == 0)
753 return value_zero (type, not_lval);
755 arg = value_ind (arg);
759 if (TYPE_CODE (arg_type) != TYPE_CODE_STRUCT)
760 error (_("Argument to dynamic_cast does not have class type"));
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);
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))
771 if (is_unique_ancestor (class_type, arg))
772 return value_cast (type, original_arg);
773 error (_("Ambiguous dynamic_cast"));
776 rtti_type = value_rtti_type (arg, &full, &top, &using_enc);
778 error (_("Couldn't determine value's most derived type for dynamic_cast"));
780 /* Compute the most derived object's address. */
781 addr = value_address (arg);
789 addr += top + value_embedded_offset (arg);
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);
797 tem = value_at (type, addr);
798 type = value_type (tem);
800 /* The first dynamic check specified in 5.2.7. */
801 if (is_public_ancestor (arg_type, TYPE_TARGET_TYPE (resolved_type)))
803 if (class_types_same_p (rtti_type, TYPE_TARGET_TYPE (resolved_type)))
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,
813 return value_cast (type,
815 ? value_ref (result, TYPE_CODE (resolved_type))
816 : value_addr (result));
819 /* The second dynamic check specified in 5.2.7. */
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,
829 ? value_ref (result, TYPE_CODE (resolved_type))
830 : value_addr (result));
832 if (TYPE_CODE (resolved_type) == TYPE_CODE_PTR)
833 return value_zero (type, not_lval);
835 error (_("dynamic_cast failed"));
838 /* Create a value of type TYPE that is zero, and return it. */
841 value_zero (struct type *type, enum lval_type lv)
843 struct value *val = allocate_value (type);
845 VALUE_LVAL (val) = (lv == lval_computed ? not_lval : lv);
849 /* Create a not_lval value of numeric type TYPE that is one, and return it. */
852 value_one (struct type *type)
854 struct type *type1 = check_typedef (type);
857 if (is_integral_type (type1) || is_floating_type (type1))
859 val = value_from_longest (type, (LONGEST) 1);
861 else if (TYPE_CODE (type1) == TYPE_CODE_ARRAY && TYPE_VECTOR (type1))
863 struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type1));
865 LONGEST low_bound, high_bound;
868 if (!get_array_bounds (type1, &low_bound, &high_bound))
869 error (_("Could not determine the vector bounds"));
871 val = allocate_value (type);
872 for (i = 0; i < high_bound - low_bound + 1; i++)
874 tmp = value_one (eltype);
875 memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype),
876 value_contents_all (tmp), TYPE_LENGTH (eltype));
881 error (_("Not a numeric type."));
884 /* value_one result is never used for assignments to. */
885 gdb_assert (VALUE_LVAL (val) == not_lval);
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. */
895 static struct value *
896 get_value_at (struct type *type, CORE_ADDR addr, int lazy)
900 if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
901 error (_("Attempt to dereference a generic pointer."));
903 val = value_from_contents_and_address (type, NULL, addr);
906 value_fetch_lazy (val);
911 /* Return a value with type TYPE located at ADDR.
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.
923 Note: value_at does *NOT* handle embedded offsets; perform such
924 adjustments before or after calling it. */
927 value_at (struct type *type, CORE_ADDR addr)
929 return get_value_at (type, addr, 0);
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. */
938 value_at_lazy (struct type *type, CORE_ADDR addr)
940 return get_value_at (type, addr, 1);
944 read_value_memory (struct value *val, LONGEST bit_offset,
945 int stack, CORE_ADDR memaddr,
946 gdb_byte *buffer, size_t length)
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;
953 object = stack ? TARGET_OBJECT_STACK_MEMORY : TARGET_OBJECT_MEMORY;
955 while (xfered_total < length)
957 enum target_xfer_status status;
958 ULONGEST xfered_partial;
960 status = target_xfer_partial (current_top_target (),
962 buffer + xfered_total * unit_size, NULL,
963 memaddr + xfered_total,
964 length - xfered_total,
967 if (status == TARGET_XFER_OK)
969 else if (status == TARGET_XFER_UNAVAILABLE)
970 mark_value_bits_unavailable (val, (xfered_total * HOST_CHAR_BIT
972 xfered_partial * HOST_CHAR_BIT);
973 else if (status == TARGET_XFER_EOF)
974 memory_error (TARGET_XFER_E_IO, memaddr + xfered_total);
976 memory_error (status, memaddr + xfered_total);
978 xfered_total += xfered_partial;
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. */
987 value_assign (struct value *toval, struct value *fromval)
991 struct frame_id old_frame;
993 if (!deprecated_value_modifiable (toval))
994 error (_("Left operand of assignment is not a modifiable lvalue."));
996 toval = coerce_ref (toval);
998 type = value_type (toval);
999 if (VALUE_LVAL (toval) != lval_internalvar)
1000 fromval = value_cast (type, fromval);
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);
1009 type = check_typedef (type);
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 ());
1016 switch (VALUE_LVAL (toval))
1018 case lval_internalvar:
1019 set_internalvar (VALUE_INTERNALVAR (toval), fromval);
1020 return value_of_internalvar (get_type_arch (type),
1021 VALUE_INTERNALVAR (toval));
1023 case lval_internalvar_component:
1025 LONGEST offset = value_offset (toval);
1027 /* Are we dealing with a bitfield?
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))
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));
1039 set_internalvar_component (VALUE_INTERNALVAR (toval),
1041 value_bitpos (toval),
1042 value_bitsize (toval),
1049 const gdb_byte *dest_buffer;
1050 CORE_ADDR changed_addr;
1052 gdb_byte buffer[sizeof (LONGEST)];
1054 if (value_bitsize (toval))
1056 struct value *parent = value_parent (toval);
1058 changed_addr = value_address (parent) + value_offset (toval);
1059 changed_len = (value_bitpos (toval)
1060 + value_bitsize (toval)
1061 + HOST_CHAR_BIT - 1)
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
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);
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);
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;
1085 changed_addr = value_address (toval);
1086 changed_len = type_length_units (type);
1087 dest_buffer = value_contents (fromval);
1090 write_memory_with_notification (changed_addr, dest_buffer, changed_len);
1096 struct frame_info *frame;
1097 struct gdbarch *gdbarch;
1100 /* Figure out which frame this is in currently.
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
1107 frame = frame_find_by_id (VALUE_FRAME_ID (toval));
1109 value_reg = VALUE_REGNUM (toval);
1112 error (_("Value being assigned to is no longer active."));
1114 gdbarch = get_frame_arch (frame);
1116 if (value_bitsize (toval))
1118 struct value *parent = value_parent (toval);
1119 LONGEST offset = value_offset (parent) + value_offset (toval);
1121 gdb_byte buffer[sizeof (LONGEST)];
1124 changed_len = (value_bitpos (toval)
1125 + value_bitsize (toval)
1126 + HOST_CHAR_BIT - 1)
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);
1134 if (!get_frame_register_bytes (frame, value_reg, offset,
1135 changed_len, buffer,
1139 throw_error (OPTIMIZED_OUT_ERROR,
1140 _("value has been optimized out"));
1142 throw_error (NOT_AVAILABLE_ERROR,
1143 _("value is not available"));
1146 modify_field (type, buffer, value_as_long (fromval),
1147 value_bitpos (toval), value_bitsize (toval));
1149 put_frame_register_bytes (frame, value_reg, offset,
1150 changed_len, buffer);
1154 if (gdbarch_convert_register_p (gdbarch, VALUE_REGNUM (toval),
1157 /* If TOVAL is a special machine register requiring
1158 conversion of program values to a special raw
1160 gdbarch_value_to_register (gdbarch, frame,
1161 VALUE_REGNUM (toval), type,
1162 value_contents (fromval));
1166 put_frame_register_bytes (frame, value_reg,
1167 value_offset (toval),
1169 value_contents (fromval));
1173 gdb::observers::register_changed.notify (frame, value_reg);
1179 const struct lval_funcs *funcs = value_computed_funcs (toval);
1181 if (funcs->write != NULL)
1183 funcs->write (toval, fromval);
1190 error (_("Left operand of assignment is not an lvalue."));
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))
1204 gdb::observers::target_changed.notify (current_top_target ());
1206 /* Having destroyed the frame cache, restore the selected
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. */
1216 struct frame_info *fi = frame_find_by_id (old_frame);
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
1230 if ((value_bitsize (toval) > 0)
1231 && (value_bitsize (toval) < 8 * (int) sizeof (LONGEST)))
1233 LONGEST fieldval = value_as_long (fromval);
1234 LONGEST valmask = (((ULONGEST) 1) << value_bitsize (toval)) - 1;
1236 fieldval &= valmask;
1237 if (!TYPE_UNSIGNED (type)
1238 && (fieldval & (valmask ^ (valmask >> 1))))
1239 fieldval |= ~valmask;
1241 fromval = value_from_longest (type, fieldval);
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));
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)
1258 set_value_enclosing_type (val, value_enclosing_type (fromval));
1259 set_value_pointed_to_offset (val, value_pointed_to_offset (fromval));
1265 /* Extend a value VAL to COUNT repetitions of its type. */
1268 value_repeat (struct value *arg1, int count)
1272 if (VALUE_LVAL (arg1) != lval_memory)
1273 error (_("Only values in memory can be extended with '@'."));
1275 error (_("Invalid number %d of repetitions."), count);
1277 val = allocate_repeat_value (value_enclosing_type (arg1), count);
1279 VALUE_LVAL (val) = lval_memory;
1280 set_value_address (val, value_address (arg1));
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)));
1290 value_of_variable (struct symbol *var, const struct block *b)
1292 struct frame_info *frame = NULL;
1294 if (symbol_read_needs_frame (var))
1295 frame = get_selected_frame (_("No frame selected."));
1297 return read_var_value (var, b, frame);
1301 address_of_variable (struct symbol *var, const struct block *b)
1303 struct type *type = SYMBOL_TYPE (var);
1306 /* Evaluate it first; if the result is a memory address, we're fine.
1307 Lazy evaluation pays off here. */
1309 val = value_of_variable (var, b);
1310 type = value_type (val);
1312 if ((VALUE_LVAL (val) == lval_memory && value_lazy (val))
1313 || TYPE_CODE (type) == TYPE_CODE_FUNC)
1315 CORE_ADDR addr = value_address (val);
1317 return value_from_pointer (lookup_pointer_type (type), addr);
1320 /* Not a memory address; check what the problem was. */
1321 switch (VALUE_LVAL (val))
1325 struct frame_info *frame;
1326 const char *regname;
1328 frame = frame_find_by_id (VALUE_NEXT_FRAME_ID (val));
1331 regname = gdbarch_register_name (get_frame_arch (frame),
1332 VALUE_REGNUM (val));
1333 gdb_assert (regname && *regname);
1335 error (_("Address requested for identifier "
1336 "\"%s\" which is in register $%s"),
1337 SYMBOL_PRINT_NAME (var), regname);
1342 error (_("Can't take address of \"%s\" which isn't an lvalue."),
1343 SYMBOL_PRINT_NAME (var));
1350 /* Return one if VAL does not live in target memory, but should in order
1351 to operate on it. Otherwise return zero. */
1354 value_must_coerce_to_target (struct value *val)
1356 struct type *valtype;
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)
1364 valtype = check_typedef (value_type (val));
1366 switch (TYPE_CODE (valtype))
1368 case TYPE_CODE_ARRAY:
1369 return TYPE_VECTOR (valtype) ? 0 : 1;
1370 case TYPE_CODE_STRING:
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. */
1382 value_coerce_to_target (struct value *val)
1387 if (!value_must_coerce_to_target (val))
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);
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.
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:
1405 (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0])
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
1411 (&array[0] + ((index - lowerbound) * sizeof array[0]))
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. */
1420 value_coerce_array (struct value *arg1)
1422 struct type *type = check_typedef (value_type (arg1));
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);
1429 if (VALUE_LVAL (arg1) != lval_memory)
1430 error (_("Attempt to take address of value not located in memory."));
1432 return value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
1433 value_address (arg1));
1436 /* Given a value which is a function, return a value which is a pointer
1440 value_coerce_function (struct value *arg1)
1442 struct value *retval;
1444 if (VALUE_LVAL (arg1) != lval_memory)
1445 error (_("Attempt to take address of value not located in memory."));
1447 retval = value_from_pointer (lookup_pointer_type (value_type (arg1)),
1448 value_address (arg1));
1452 /* Return a pointer value for the object for which ARG1 is the
1456 value_addr (struct value *arg1)
1459 struct type *type = check_typedef (value_type (arg1));
1461 if (TYPE_IS_REFERENCE (type))
1463 if (value_bits_synthetic_pointer (arg1, value_embedded_offset (arg1),
1464 TARGET_CHAR_BIT * TYPE_LENGTH (type)))
1465 arg1 = coerce_ref (arg1);
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));
1479 arg2 = value_copy (arg1);
1480 deprecated_set_value_type (arg2, type_ptr);
1481 set_value_enclosing_type (arg2, enclosing_type_ptr);
1486 if (TYPE_CODE (type) == TYPE_CODE_FUNC)
1487 return value_coerce_function (arg1);
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);
1493 if (VALUE_LVAL (arg1) != lval_memory)
1494 error (_("Attempt to take address of value not located in memory."));
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)));
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
1507 set_value_pointed_to_offset (arg2, value_embedded_offset (arg1));
1511 /* Return a reference value for the object for which ARG1 is the
1515 value_ref (struct value *arg1, enum type_code refcode)
1518 struct type *type = check_typedef (value_type (arg1));
1520 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
1522 if ((TYPE_CODE (type) == TYPE_CODE_REF
1523 || TYPE_CODE (type) == TYPE_CODE_RVALUE_REF)
1524 && TYPE_CODE (type) == refcode)
1527 arg2 = value_addr (arg1);
1528 deprecated_set_value_type (arg2, lookup_reference_type (type, refcode));
1532 /* Given a value of a pointer type, apply the C unary * operator to
1536 value_ind (struct value *arg1)
1538 struct type *base_type;
1541 arg1 = coerce_array (arg1);
1543 base_type = check_typedef (value_type (arg1));
1545 if (VALUE_LVAL (arg1) == lval_computed)
1547 const struct lval_funcs *funcs = value_computed_funcs (arg1);
1549 if (funcs->indirect)
1551 struct value *result = funcs->indirect (arg1);
1558 if (TYPE_CODE (base_type) == TYPE_CODE_PTR)
1560 struct type *enc_type;
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);
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));
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)));
1579 enc_type = value_type (arg2);
1580 return readjust_indirect_value_type (arg2, enc_type, base_type, arg1);
1583 error (_("Attempt to take contents of a non-pointer value."));
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.
1589 The array bounds are set from LOWBOUND and HIGHBOUND, and the array
1590 is populated from the values passed in ELEMVEC.
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). */
1597 value_array (int lowbound, int highbound, struct value **elemvec)
1601 ULONGEST typelength;
1603 struct type *arraytype;
1605 /* Validate that the bounds are reasonable and that each of the
1606 elements have the same size. */
1608 nelem = highbound - lowbound + 1;
1611 error (_("bad array bounds (%d, %d)"), lowbound, highbound);
1613 typelength = type_length_units (value_enclosing_type (elemvec[0]));
1614 for (idx = 1; idx < nelem; idx++)
1616 if (type_length_units (value_enclosing_type (elemvec[idx]))
1619 error (_("array elements must all be the same size"));
1623 arraytype = lookup_array_range_type (value_enclosing_type (elemvec[0]),
1624 lowbound, highbound);
1626 if (!current_language->c_style_arrays)
1628 val = allocate_value (arraytype);
1629 for (idx = 0; idx < nelem; idx++)
1630 value_contents_copy (val, idx * typelength, elemvec[idx], 0,
1635 /* Allocate space to store the array, and then initialize it by
1636 copying in each element. */
1638 val = allocate_value (arraytype);
1639 for (idx = 0; idx < nelem; idx++)
1640 value_contents_copy (val, idx * typelength, elemvec[idx], 0, typelength);
1645 value_cstring (const char *ptr, ssize_t len, struct type *char_type)
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);
1653 val = allocate_value (stringtype);
1654 memcpy (value_contents_raw (val), ptr, len);
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.
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. */
1668 value_string (const char *ptr, ssize_t len, struct type *char_type)
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);
1676 val = allocate_value (stringtype);
1677 memcpy (value_contents_raw (val), ptr, len);
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
1689 STATICP is nonzero if the T1 argument list came from a static
1690 member function. T2 will still include the ``this'' pointer, but
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. */
1700 typecmp (int staticp, int varargs, int nargs,
1701 struct field t1[], struct value *t2[])
1706 internal_error (__FILE__, __LINE__,
1707 _("typecmp: no argument list"));
1709 /* Skip ``this'' argument if applicable. T2 will always include
1715 (i < nargs) && TYPE_CODE (t1[i].type) != TYPE_CODE_VOID;
1718 struct type *tt1, *tt2;
1723 tt1 = check_typedef (t1[i].type);
1724 tt2 = check_typedef (value_type (t2[i]));
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)))
1731 if (TYPE_CODE (tt2) == TYPE_CODE_ARRAY)
1732 t2[i] = value_coerce_array (t2[i]);
1734 t2[i] = value_ref (t2[i], TYPE_CODE (tt1));
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)
1746 tt1 = check_typedef( TYPE_TARGET_TYPE(tt1) );
1748 while (TYPE_CODE(tt2) == TYPE_CODE_ARRAY
1749 || TYPE_CODE(tt2) == TYPE_CODE_PTR
1750 || TYPE_IS_REFERENCE (tt2))
1752 tt2 = check_typedef (TYPE_TARGET_TYPE(tt2));
1754 if (TYPE_CODE (tt1) == TYPE_CODE (tt2))
1756 /* Array to pointer is a `trivial conversion' according to the
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])))
1765 if (varargs || t2[i] == NULL)
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. */
1775 update_search_result (struct value **result_ptr, struct value *v,
1776 LONGEST *last_boffset, LONGEST boffset,
1777 const char *name, struct type *type)
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));
1788 *last_boffset = boffset;
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. */
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)
1809 type = check_typedef (type);
1810 nbases = TYPE_N_BASECLASSES (type);
1812 if (!looking_for_baseclass)
1813 for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
1815 const char *t_field_name = TYPE_FIELD_NAME (type, i);
1817 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
1821 if (field_is_static (&TYPE_FIELD (type, i)))
1822 v = value_static_field (type, i);
1824 v = value_primitive_field (arg1, offset, i, type);
1830 && t_field_name[0] == '\0')
1832 struct type *field_type = TYPE_FIELD_TYPE (type, i);
1834 if (TYPE_CODE (field_type) == TYPE_CODE_UNION
1835 || TYPE_CODE (field_type) == TYPE_CODE_STRUCT)
1837 /* Look for a match through the fields of an anonymous
1838 union, or anonymous struct. C++ provides anonymous
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
1849 struct value *v = NULL;
1850 LONGEST new_offset = offset;
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;
1863 do_search_struct_field (name, arg1, new_offset,
1865 looking_for_baseclass, &v,
1877 for (i = 0; i < nbases; i++)
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,
1889 LONGEST boffset = value_embedded_offset (arg1) + offset;
1891 if (BASETYPE_VIA_VIRTUAL (type, i))
1895 boffset = baseclass_offset (type, i,
1896 value_contents_for_printing (arg1),
1897 value_embedded_offset (arg1) + offset,
1898 value_address (arg1),
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. */
1905 boffset += value_embedded_offset (arg1) + offset;
1907 || boffset >= TYPE_LENGTH (value_enclosing_type (arg1)))
1909 CORE_ADDR base_addr;
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"));
1920 v2 = value_copy (arg1);
1921 deprecated_set_value_type (v2, basetype);
1922 set_value_embedded_offset (v2, boffset);
1925 if (found_baseclass)
1929 do_search_struct_field (name, v2, 0,
1930 TYPE_BASECLASS (type, i),
1931 looking_for_baseclass,
1932 result_ptr, last_boffset,
1936 else if (found_baseclass)
1937 v = value_primitive_field (arg1, offset, i, type);
1940 do_search_struct_field (name, arg1,
1941 offset + TYPE_BASECLASS_BITPOS (type,
1943 basetype, looking_for_baseclass,
1944 result_ptr, last_boffset,
1948 update_search_result (result_ptr, v, last_boffset,
1949 boffset, name, outermost_type);
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.
1957 If LOOKING_FOR_BASECLASS, then instead of looking for struct
1958 fields, look for a baseclass named NAME. */
1960 static struct value *
1961 search_struct_field (const char *name, struct value *arg1,
1962 struct type *type, int looking_for_baseclass)
1964 struct value *result = NULL;
1965 LONGEST boffset = 0;
1967 do_search_struct_field (name, arg1, 0, type, looking_for_baseclass,
1968 &result, &boffset, type);
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
1977 If found, return value, else if name matched and args not return
1978 (value) -1, else return NULL. */
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)
1987 int name_matched = 0;
1988 char dem_opname[64];
1990 type = check_typedef (type);
1991 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
1993 const char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
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"))
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;
2005 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
2007 int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1;
2008 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
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)
2017 v = value_fn_field (arg1p, f, j, type, offset);
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))
2029 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
2030 return value_virtual_fn_field (arg1p, f, j,
2032 if (TYPE_FN_FIELD_STATIC_P (f, j)
2034 *static_memfuncp = 1;
2035 v = value_fn_field (arg1p, f, j, type, offset);
2044 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
2046 LONGEST base_offset;
2047 LONGEST this_offset;
2049 if (BASETYPE_VIA_VIRTUAL (type, i))
2051 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
2052 struct value *base_val;
2053 const gdb_byte *base_valaddr;
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. */
2059 if (offset < 0 || offset >= TYPE_LENGTH (type))
2063 gdb::byte_vector tmp (TYPE_LENGTH (baseclass));
2064 address = value_address (*arg1p);
2066 if (target_read_memory (address + offset,
2067 tmp.data (), TYPE_LENGTH (baseclass)) != 0)
2068 error (_("virtual baseclass botch"));
2070 base_val = value_from_contents_and_address (baseclass,
2073 base_valaddr = value_contents_for_printing (base_val);
2079 base_valaddr = value_contents_for_printing (*arg1p);
2080 this_offset = offset;
2083 base_offset = baseclass_offset (type, i, base_valaddr,
2084 this_offset, value_address (base_val),
2089 base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
2091 v = search_struct_method (name, arg1p, args, base_offset + offset,
2092 static_memfuncp, TYPE_BASECLASS (type, i));
2093 if (v == (struct value *) - 1)
2099 /* FIXME-bothner: Why is this commented out? Why is it here? */
2100 /* *arg1p = arg1_tmp; */
2105 return (struct value *) - 1;
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.
2115 C++: ARGS is a list of argument types to aid in the selection of
2116 an appropriate method. Also, handle derived types.
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.
2122 ERR is an error message to be printed in case the field is not
2126 value_struct_elt (struct value **argp, struct value **args,
2127 const char *name, int *static_memfuncp, const char *err)
2132 *argp = coerce_array (*argp);
2134 t = check_typedef (value_type (*argp));
2136 /* Follow pointers until we get to a non-pointer. */
2138 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_IS_REFERENCE (t))
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));
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."),
2152 /* Assume it's not, unless we see that it is. */
2153 if (static_memfuncp)
2154 *static_memfuncp = 0;
2158 /* if there are no arguments ...do this... */
2160 /* Try as a field first, because if we succeed, there is less
2162 v = search_struct_field (name, *argp, t, 0);
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);
2171 if (v == (struct value *) - 1)
2172 error (_("Cannot take address of method %s."), name);
2175 if (TYPE_NFN_FIELDS (t))
2176 error (_("There is no member or method named %s."), name);
2178 error (_("There is no member named %s."), name);
2183 v = search_struct_method (name, argp, args, 0,
2184 static_memfuncp, t);
2186 if (v == (struct value *) - 1)
2188 error (_("One of the arguments you tried to pass to %s could not "
2189 "be converted to what the function wants."), name);
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;
2204 throw_error (NOT_FOUND_ERROR,
2205 _("Structure has no component named %s."), name);
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. */
2215 value_struct_elt_bitpos (struct value **argp, int bitpos, struct type *ftype,
2221 *argp = coerce_array (*argp);
2223 t = check_typedef (value_type (*argp));
2225 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_IS_REFERENCE (t))
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));
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."),
2238 for (i = TYPE_N_BASECLASSES (t); i < TYPE_NFIELDS (t); i++)
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);
2246 error (_("No field with matching bitpos and type."));
2255 value_union_variant (struct type *union_type, const gdb_byte *contents)
2257 gdb_assert (TYPE_CODE (union_type) == TYPE_CODE_UNION
2258 && TYPE_FLAG_DISCRIMINATED_UNION (union_type));
2260 struct dynamic_prop *discriminant_prop
2261 = get_dyn_prop (DYN_PROP_DISCRIMINATED, union_type);
2262 gdb_assert (discriminant_prop != nullptr);
2264 struct discriminant_info *info
2265 = (struct discriminant_info *) discriminant_prop->data.baton;
2266 gdb_assert (info != nullptr);
2268 /* If this is a univariant union, just return the sole field. */
2269 if (TYPE_NFIELDS (union_type) == 1)
2271 /* This should only happen for univariants, which we already dealt
2273 gdb_assert (info->discriminant_index != -1);
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);
2282 for (int i = 0; i < TYPE_NFIELDS (union_type); ++i)
2284 if (i != info->default_index
2285 && i != info->discriminant_index
2286 && discriminant == info->discriminants[i])
2290 if (info->default_index == -1)
2291 error (_("Could not find variant corresponding to discriminant %s"),
2292 pulongest (discriminant));
2293 return info->default_index;
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
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
2316 BOFFSET is the offset of the base subobject where the method is found. */
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)
2326 struct fn_field *f = NULL;
2328 gdb_assert (fn_list != NULL && xm_worker_vec != NULL);
2329 type = check_typedef (type);
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)
2337 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
2339 /* pai: FIXME What about operators and type conversions? */
2340 const char *fn_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
2342 if (fn_field_name && (strcmp_iw (fn_field_name, method) == 0))
2344 int len = TYPE_FN_FIELDLIST_LENGTH (type, i);
2345 f = TYPE_FN_FIELDLIST1 (type, i);
2352 /* Resolve any stub methods. */
2353 check_stub_method_group (type, i);
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);
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--)
2374 LONGEST base_offset;
2376 if (BASETYPE_VIA_VIRTUAL (type, i))
2378 base_offset = baseclass_offset (type, i,
2379 value_contents_for_printing (*argp),
2380 value_offset (*argp) + offset,
2381 value_address (*argp), *argp);
2383 else /* Non-virtual base, simply use bit position from debug
2386 base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
2389 find_method_list (argp, method, base_offset + offset,
2390 TYPE_BASECLASS (type, i), fn_list, num_fns,
2391 xm_worker_vec, basetype, boffset);
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
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
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
2410 BOFFSET is the offset of the base subobject which defines the method. */
2413 value_find_oload_method_list (struct value **argp, const char *method,
2414 LONGEST offset, struct fn_field **fn_list,
2416 std::vector<xmethod_worker_up> *xm_worker_vec,
2417 struct type **basetype, LONGEST *boffset)
2421 t = check_typedef (value_type (*argp));
2423 /* Code snarfed from value_struct_elt. */
2424 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_IS_REFERENCE (t))
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));
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"));
2438 gdb_assert (fn_list != NULL && xm_worker_vec != NULL);
2440 /* Clear the lists. */
2443 xm_worker_vec->clear ();
2445 find_method_list (argp, method, 0, t, fn_list, num_fns, xm_worker_vec,
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.
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
2465 In the case of class methods, the parameter OBJ is an object value
2466 in which to search for overloaded methods.
2468 In the case of non-method functions, the parameter FSYM is a symbol
2469 corresponding to one of the overloaded functions.
2471 Return value is an integer: 0 -> good match, 10 -> debugger applied
2472 non-standard coercions, 100 -> incompatible.
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
2478 If a method is being searched for, and it is a static method,
2479 then STATICP will point to a non-zero value.
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
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
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. */
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)
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;
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;
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. */
2526 struct type *basetype = NULL;
2529 struct cleanup *all_cleanups = make_cleanup (null_cleanup, NULL);
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;
2539 /* Get the list of overloaded methods or functions. */
2540 if (method == METHOD || method == BOTH)
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));
2550 /* First check whether this is a data member, e.g. a pointer to
2552 if (TYPE_CODE (check_typedef (value_type (obj))) == TYPE_CODE_STRUCT)
2554 *valp = search_struct_field (name, obj,
2555 check_typedef (value_type (obj)), 0);
2559 do_cleanups (all_cleanups);
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"),
2572 (obj_type_name && *obj_type_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. */
2579 gdb_assert (TYPE_SELF_TYPE (fns_ptr[0].type) != NULL);
2581 src_method_oload_champ = find_oload_champ (args, nargs,
2582 num_fns, fns_ptr, NULL,
2583 NULL, &src_method_badness);
2585 src_method_match_quality = classify_oload_match
2586 (src_method_badness, nargs,
2587 oload_method_static_p (fns_ptr, src_method_oload_champ));
2589 make_cleanup (xfree, src_method_badness);
2592 if (!xm_worker_vec.empty ())
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,
2599 make_cleanup (xfree, ext_method_badness);
2602 if (src_method_oload_champ >= 0 && ext_method_oload_champ >= 0)
2604 switch (compare_badness (ext_method_badness, src_method_badness))
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
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)
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;
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;
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;
2638 gdb_assert_not_reached ("Unexpected overload comparison "
2643 else if (src_method_oload_champ >= 0)
2645 method_oload_champ = src_method_oload_champ;
2646 method_badness = src_method_badness;
2647 method_match_quality = src_method_match_quality;
2649 else if (ext_method_oload_champ >= 0)
2651 method_oload_champ = ext_method_oload_champ;
2652 method_badness = ext_method_badness;
2653 method_match_quality = ext_method_match_quality;
2657 if (method == NON_METHOD || method == BOTH)
2659 const char *qualified_name = NULL;
2661 /* If the overload match is being search for both as a method
2662 and non member function, the first argument must now be
2665 args[0] = value_ind (args[0]);
2669 qualified_name = SYMBOL_NATURAL_NAME (fsym);
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). */
2675 && TYPE_CODE (check_typedef (SYMBOL_TYPE (fsym)))
2680 temp_func = cp_func_name (qualified_name);
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. */
2687 make_cleanup (xfree, temp_func);
2688 if (strcmp (temp_func, qualified_name) == 0)
2691 func_name = temp_func;
2698 qualified_name = name;
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)
2707 do_cleanups (all_cleanups);
2711 func_oload_champ = find_oload_champ_namespace (args, nargs,
2718 if (func_oload_champ >= 0)
2719 func_match_quality = classify_oload_match (func_badness, nargs, 0);
2721 make_cleanup (xfree, oload_syms);
2722 make_cleanup (xfree, func_badness);
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."),
2731 /* If we have found both a method match and a function
2732 match, find out which one is better, and calculate match
2734 if (method_oload_champ >= 0 && func_oload_champ >= 0)
2736 switch (compare_badness (func_badness, method_badness))
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
2742 error (_("Ambiguous overload resolution"));
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"));
2750 case 2: /* Function champion. */
2751 method_oload_champ = -1;
2752 match_quality = func_match_quality;
2754 case 3: /* Method champion. */
2755 func_oload_champ = -1;
2756 match_quality = method_match_quality;
2759 error (_("Internal error: unexpected overload comparison result"));
2765 /* We have either a method match or a function match. */
2766 if (method_oload_champ >= 0)
2767 match_quality = method_match_quality;
2769 match_quality = func_match_quality;
2772 if (match_quality == INCOMPATIBLE)
2774 if (method == METHOD)
2775 error (_("Cannot resolve method %s%s%s to any overloaded instance"),
2777 (obj_type_name && *obj_type_name) ? "::" : "",
2780 error (_("Cannot resolve function %s to any overloaded instance"),
2783 else if (match_quality == NON_STANDARD)
2785 if (method == METHOD)
2786 warning (_("Using non-standard conversion to match "
2787 "method %s%s%s to supplied arguments"),
2789 (obj_type_name && *obj_type_name) ? "::" : "",
2792 warning (_("Using non-standard conversion to match "
2793 "function %s to supplied arguments"),
2797 if (staticp != NULL)
2798 *staticp = oload_method_static_p (fns_ptr, method_oload_champ);
2800 if (method_oload_champ >= 0)
2802 if (src_method_oload_champ >= 0)
2804 if (TYPE_FN_FIELD_VIRTUAL_P (fns_ptr, method_oload_champ)
2805 && noside != EVAL_AVOID_SIDE_EFFECTS)
2807 *valp = value_virtual_fn_field (&temp, fns_ptr,
2808 method_oload_champ, basetype,
2812 *valp = value_fn_field (&temp, fns_ptr, method_oload_champ,
2816 *valp = value_from_xmethod
2817 (std::move (xm_worker_vec[ext_method_oload_champ]));
2820 *symp = oload_syms[func_oload_champ];
2824 struct type *temp_type = check_typedef (value_type (temp));
2825 struct type *objtype = check_typedef (obj_type);
2827 if (TYPE_CODE (temp_type) != TYPE_CODE_PTR
2828 && (TYPE_CODE (objtype) == TYPE_CODE_PTR
2829 || TYPE_IS_REFERENCE (objtype)))
2831 temp = value_addr (temp);
2836 do_cleanups (all_cleanups);
2838 switch (match_quality)
2844 default: /* STANDARD */
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
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,
2867 find_oload_champ_namespace_loop (args, nargs,
2870 oload_syms, oload_champ_bv,
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
2883 It is the caller's responsibility to free *OLOAD_SYMS and
2887 find_oload_champ_namespace_loop (struct value **args, int nargs,
2888 const char *func_name,
2889 const char *qualified_name,
2891 struct symbol ***oload_syms,
2892 struct badness_vector **oload_champ_bv,
2896 int next_namespace_len = namespace_len;
2897 int searched_deeper = 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;
2905 if (next_namespace_len != 0)
2907 gdb_assert (qualified_name[next_namespace_len] == ':');
2908 next_namespace_len += 2;
2910 next_namespace_len +=
2911 cp_find_first_component (qualified_name + next_namespace_len);
2913 /* Initialize these to values that can safely be xfree'd. */
2915 *oload_champ_bv = NULL;
2917 /* First, see if we have a deeper namespace we can search in.
2918 If we get a good match there, use it. */
2920 if (qualified_name[next_namespace_len] == ':')
2922 searched_deeper = 1;
2924 if (find_oload_champ_namespace_loop (args, nargs,
2925 func_name, qualified_name,
2927 oload_syms, oload_champ_bv,
2928 oload_champ, no_adl))
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.) */
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,
2950 /* If we have reached the deepest level perform argument
2951 determined lookup. */
2952 if (!searched_deeper && !no_adl)
2955 struct type **arg_types;
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);
2965 while (new_oload_syms[num_fns])
2968 new_oload_champ = find_oload_champ (args, nargs, num_fns,
2969 NULL, NULL, new_oload_syms,
2970 &new_oload_champ_bv);
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. */
2979 if (new_oload_champ != -1
2980 && classify_oload_match (new_oload_champ_bv, nargs, 0) == STANDARD)
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);
2988 else if (searched_deeper)
2990 xfree (new_oload_syms);
2991 xfree (new_oload_champ_bv);
2992 discard_cleanups (old_cleanups);
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);
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
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.
3014 Return the index of the best match; store an indication of the
3015 quality of the match in OLOAD_CHAMP_BV.
3017 It is the caller's responsibility to free *OLOAD_CHAMP_BV. */
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)
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. */
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
3038 gdb_assert ((fns_ptr != NULL) + (oload_syms != NULL) + (xm_worker_vec != NULL)
3041 *oload_champ_bv = NULL;
3043 int fn_count = xm_worker_vec != NULL ? xm_worker_vec->size () : num_fns;
3045 /* Consider each candidate in turn. */
3046 for (ix = 0; ix < fn_count; ix++)
3049 int static_offset = 0;
3051 struct type **parm_types;
3053 if (xm_worker_vec != NULL)
3055 xmethod_worker *worker = (*xm_worker_vec)[ix].get ();
3056 parm_types = worker->get_arg_types (&nparms);
3060 if (fns_ptr != NULL)
3062 nparms = TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (fns_ptr, ix));
3063 static_offset = oload_method_static_p (fns_ptr, ix);
3066 nparms = TYPE_NFIELDS (SYMBOL_TYPE (oload_syms[ix]));
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]),
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);
3082 if (!*oload_champ_bv)
3084 *oload_champ_bv = bv;
3087 else /* See whether current candidate is better or worse than
3089 switch (compare_badness (bv, *oload_champ_bv))
3091 case 0: /* Top two contenders are equally good. */
3092 oload_ambiguous = 1;
3094 case 1: /* Incomparable top contenders. */
3095 oload_ambiguous = 2;
3097 case 2: /* New champion, record details. */
3098 *oload_champ_bv = bv;
3099 oload_ambiguous = 0;
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",
3118 fprintf_filtered (gdb_stderr,
3119 "Overloaded function instance "
3120 "%s # of parms %d\n",
3121 SYMBOL_DEMANGLED_NAME (oload_syms[ix]),
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);
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. */
3140 oload_method_static_p (struct fn_field *fns_ptr, int index)
3142 if (fns_ptr && index >= 0 && TYPE_FN_FIELD_STATIC_P (fns_ptr, index))
3148 /* Check how good an overload match OLOAD_CHAMP_BV represents. */
3150 static enum oload_classification
3151 classify_oload_match (struct badness_vector *oload_champ_bv,
3156 enum oload_classification worst = STANDARD;
3158 for (ix = 1; ix <= nargs - static_offset; ix++)
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
3173 /* If no INCOMPATIBLE classification was found, return the worst one
3174 that was found (if any). */
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. */
3184 destructor_name_p (const char *name, struct type *type)
3188 const char *dname = type_name_or_error (type);
3189 const char *cp = strchr (dname, '<');
3192 /* Do not compare the template part for template classes. */
3194 len = strlen (dname);
3197 if (strlen (name + 1) != len || strncmp (dname, name + 1, len) != 0)
3198 error (_("name of destructor must equal name of class"));
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. */
3209 static struct value *
3210 enum_constant_from_type (struct type *type, const char *name)
3213 int name_len = strlen (name);
3215 gdb_assert (TYPE_CODE (type) == TYPE_CODE_ENUM
3216 && TYPE_DECLARED_CLASS (type));
3218 for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); ++i)
3220 const char *fname = TYPE_FIELD_NAME (type, i);
3223 if (TYPE_FIELD_LOC_KIND (type, i) != FIELD_LOC_KIND_ENUMVAL
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));
3237 error (_("no constant named \"%s\" in enum \"%s\""),
3238 name, TYPE_NAME (type));
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. */
3248 value_aggregate_elt (struct type *curtype, const char *name,
3249 struct type *expect_type, int want_address,
3252 switch (TYPE_CODE (curtype))
3254 case TYPE_CODE_STRUCT:
3255 case TYPE_CODE_UNION:
3256 return value_struct_elt_for_reference (curtype, 0, curtype,
3258 want_address, noside);
3259 case TYPE_CODE_NAMESPACE:
3260 return value_namespace_elt (curtype, name,
3261 want_address, noside);
3263 case TYPE_CODE_ENUM:
3264 return enum_constant_from_type (curtype, name);
3267 internal_error (__FILE__, __LINE__,
3268 _("non-aggregate type in value_aggregate_elt"));
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.
3279 The type T2 is expected to have come from make_params (in eval.c). */
3282 compare_parameters (struct type *t1, struct type *t2, int skip_artificial)
3286 if (TYPE_NFIELDS (t1) > 0 && TYPE_FIELD_ARTIFICIAL (t1, 0))
3289 /* If skipping artificial fields, find the first real field
3291 if (skip_artificial)
3293 while (start < TYPE_NFIELDS (t1)
3294 && TYPE_FIELD_ARTIFICIAL (t1, start))
3298 /* Now compare parameters. */
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)
3306 if ((TYPE_NFIELDS (t1) - start) == TYPE_NFIELDS (t2))
3310 for (i = 0; i < TYPE_NFIELDS (t2); ++i)
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)
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. */
3332 get_baseclass_offset (struct type *vt, struct type *cls,
3333 struct value *v, int *boffs, bool *isvirt)
3335 for (int i = 0; i < TYPE_N_BASECLASSES (vt); i++)
3337 struct type *t = TYPE_FIELD_TYPE (vt, i);
3338 if (types_equal (t, cls))
3340 if (BASETYPE_VIA_VIRTUAL (vt, i))
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);
3352 if (get_baseclass_offset (check_typedef (t), cls, v, boffs, isvirt))
3354 if (*isvirt == false) /* Add non-virtual base offset. */
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);
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". */
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,
3381 struct type *t = check_typedef (curtype);
3383 struct value *result;
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"));
3390 for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
3392 const char *t_field_name = TYPE_FIELD_NAME (t, i);
3394 if (t_field_name && strcmp (t_field_name, name) == 0)
3396 if (field_is_static (&TYPE_FIELD (t, i)))
3398 struct value *v = value_static_field (t, i);
3403 if (TYPE_FIELD_PACKED (t, i))
3404 error (_("pointers to bitfield members not allowed"));
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));
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);
3420 struct value *ptr, *this_v = v;
3422 struct type *type, *tmp;
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)
3433 /* Find class offset of type CURTYPE from either its
3434 parent type DOMAIN or the type of implied this. */
3436 bool isvirt = false;
3437 if (get_baseclass_offset (domain, curtype, v, &boff,
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,
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);
3455 error (_("Cannot reference non-static field \"%s\""), name);
3460 /* C++: If it was not found as a data field, then try to return it
3461 as a pointer to a method. */
3463 /* Perform all necessary dereferencing. */
3464 while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR)
3465 intype = TYPE_TARGET_TYPE (intype);
3467 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
3469 const char *t_field_name = TYPE_FN_FIELDLIST_NAME (t, i);
3470 char dem_opname[64];
3472 if (startswith (t_field_name, "__")
3473 || startswith (t_field_name, "op")
3474 || startswith (t_field_name, "type"))
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,
3481 t_field_name = dem_opname;
3483 if (t_field_name && strcmp (t_field_name, name) == 0)
3486 int len = TYPE_FN_FIELDLIST_LENGTH (t, i);
3487 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
3489 check_stub_method_group (t, i);
3493 for (j = 0; j < len; ++j)
3495 if (TYPE_CONST (intype) != TYPE_FN_FIELD_CONST (f, j))
3497 if (TYPE_VOLATILE (intype) != TYPE_FN_FIELD_VOLATILE (f, j))
3500 if (compare_parameters (TYPE_FN_FIELD_TYPE (f, j), intype, 0)
3501 || compare_parameters (TYPE_FN_FIELD_TYPE (f, j),
3507 error (_("no member function matches "
3508 "that type instantiation"));
3515 for (ii = 0; ii < len; ++ii)
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))
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);
3540 error (_("no matching member function"));
3543 if (TYPE_FN_FIELD_STATIC_P (f, j))
3546 lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
3547 0, VAR_DOMAIN, 0).symbol;
3553 return value_addr (read_var_value (s, 0, 0));
3555 return read_var_value (s, 0, 0);
3558 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
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);
3568 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
3569 return allocate_value (TYPE_FN_FIELD_TYPE (f, j));
3571 error (_("Cannot reference virtual member function \"%s\""),
3577 lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
3578 0, VAR_DOMAIN, 0).symbol;
3583 struct value *v = read_var_value (s, 0, 0);
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);
3597 for (i = TYPE_N_BASECLASSES (t) - 1; i >= 0; i--)
3602 if (BASETYPE_VIA_VIRTUAL (t, i))
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),
3610 want_address, noside);
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
3619 return value_maybe_namespace_elt (curtype, name,
3620 want_address, noside);
3623 /* C++: Return the member NAME of the namespace given by the type
3626 static struct value *
3627 value_namespace_elt (const struct type *curtype,
3628 const char *name, int want_address,
3631 struct value *retval = value_maybe_namespace_elt (curtype, name,
3636 error (_("No symbol \"%s\" in namespace \"%s\"."),
3637 name, TYPE_NAME (curtype));
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). */
3648 static struct value *
3649 value_maybe_namespace_elt (const struct type *curtype,
3650 const char *name, int want_address,
3653 const char *namespace_name = TYPE_NAME (curtype);
3654 struct block_symbol sym;
3655 struct value *result;
3657 sym = cp_lookup_symbol_namespace (namespace_name, name,
3658 get_selected_block (0), VAR_DOMAIN);
3660 if (sym.symbol == NULL)
3662 else if ((noside == EVAL_AVOID_SIDE_EFFECTS)
3663 && (SYMBOL_CLASS (sym.symbol) == LOC_TYPEDEF))
3664 result = allocate_value (SYMBOL_TYPE (sym.symbol));
3666 result = value_of_variable (sym.symbol, sym.block);
3669 result = value_addr (result);
3674 /* Given a pointer or a reference value V, find its real (RTTI) type.
3676 Other parameters FULL, TOP, USING_ENC as with value_rtti_type()
3677 and refer to the values computed for the object pointed to. */
3680 value_rtti_indirect_type (struct value *v, int *full,
3681 LONGEST *top, int *using_enc)
3683 struct value *target = NULL;
3684 struct type *type, *real_type, *target_type;
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)
3695 target = value_ind (v);
3697 CATCH (except, RETURN_MASK_ERROR)
3699 if (except.error == MEMORY_ERROR)
3701 /* value_ind threw a memory error. The pointer is NULL or
3702 contains an uninitialized value: we can't determine any
3706 throw_exception (except);
3713 real_type = value_rtti_type (target, full, top, using_enc);
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);
3726 internal_error (__FILE__, __LINE__, _("Unexpected value type."));
3728 /* Copy qualifiers to the pointer/reference. */
3729 real_type = make_cv_type (TYPE_CONST (type), TYPE_VOLATILE (type),
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. */
3747 value_full_object (struct value *argp,
3749 int xfull, int xtop,
3752 struct type *real_type;
3756 struct value *new_val;
3763 using_enc = xusing_enc;
3766 real_type = value_rtti_type (argp, &full, &top, &using_enc);
3768 /* If no RTTI data, or if object is already complete, do nothing. */
3769 if (!real_type || real_type == value_enclosing_type (argp))
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
3776 && TYPE_LENGTH (real_type) < TYPE_LENGTH (value_enclosing_type (argp)))
3779 /* If we have the full object, but for some reason the enclosing
3780 type is wrong, set it. */
3781 /* pai: FIXME -- sounds iffy */
3784 argp = value_copy (argp);
3785 set_value_enclosing_type (argp, real_type);
3789 /* Check if object is in memory. */
3790 if (VALUE_LVAL (argp) != lval_memory)
3792 warning (_("Couldn't retrieve complete object of RTTI "
3793 "type %s; object may be in register(s)."),
3794 TYPE_NAME (real_type));
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)
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. */
3817 value_of_this (const struct language_defn *lang)
3819 struct block_symbol sym;
3820 const struct block *b;
3821 struct frame_info *frame;
3823 if (!lang->la_name_of_this)
3824 error (_("no `this' in current language"));
3826 frame = get_selected_frame (_("no frame selected"));
3828 b = get_frame_block (frame, NULL);
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);
3835 return read_var_value (sym.symbol, sym.block, frame);
3838 /* Return the value of the local variable, if one exists. Return NULL
3839 otherwise. Never throw error. */
3842 value_of_this_silent (const struct language_defn *lang)
3844 struct value *ret = NULL;
3848 ret = value_of_this (lang);
3850 CATCH (except, RETURN_MASK_ERROR)
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. */
3863 value_slice (struct value *array, int lowbound, int length)
3865 struct type *slice_range_type, *slice_type, *range_type;
3866 LONGEST lowerbound, upperbound;
3867 struct value *slice;
3868 struct type *array_type;
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"));
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"));
3879 if (lowbound < lowerbound || length < 0
3880 || lowbound + length - 1 > upperbound)
3881 error (_("slice out of range"));
3883 /* FIXME-type-allocation: need a way to free this type when we are
3885 slice_range_type = create_static_range_type ((struct type *) NULL,
3886 TYPE_TARGET_TYPE (range_type),
3888 lowbound + length - 1);
3891 struct type *element_type = TYPE_TARGET_TYPE (array_type);
3893 = (lowbound - lowerbound) * TYPE_LENGTH (check_typedef (element_type));
3895 slice_type = create_array_type ((struct type *) NULL,
3898 TYPE_CODE (slice_type) = TYPE_CODE (array_type);
3900 if (VALUE_LVAL (array) == lval_memory && value_lazy (array))
3901 slice = allocate_value_lazy (slice_type);
3904 slice = allocate_value (slice_type);
3905 value_contents_copy (slice, 0, array, offset,
3906 type_length_units (slice_type));
3909 set_value_component_location (slice, array);
3910 set_value_offset (slice, value_offset (array) + offset);
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 */
3923 value_literal_complex (struct value *arg1,
3928 struct type *real_type = TYPE_TARGET_TYPE (type);
3930 val = allocate_value (type);
3931 arg1 = value_cast (real_type, arg1);
3932 arg2 = value_cast (real_type, arg2);
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));
3941 /* Cast a value into the appropriate complex data type. */
3943 static struct value *
3944 cast_into_complex (struct type *type, struct value *val)
3946 struct type *real_type = TYPE_TARGET_TYPE (type);
3948 if (TYPE_CODE (value_type (val)) == TYPE_CODE_COMPLEX)
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);
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));
3960 return value_literal_complex (re_val, im_val, type);
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),
3968 error (_("cannot cast non-number to complex"));
3972 _initialize_valops (void)
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."),
3979 show_overload_resolution,
3980 &setlist, &showlist);
3981 overload_resolution = 1;