1 /* Perform non-arithmetic operations on values, for GDB.
3 Copyright (C) 1986-2017 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"
38 #include "tracepoint.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,
71 struct fn_field *, VEC (xmethod_worker_ptr) *,
72 struct symbol **, struct badness_vector **);
74 static int oload_method_static_p (struct fn_field *, int);
76 enum oload_classification { STANDARD, NON_STANDARD, INCOMPATIBLE };
79 oload_classification classify_oload_match (struct badness_vector *,
82 static struct value *value_struct_elt_for_reference (struct type *,
88 static struct value *value_namespace_elt (const struct type *,
89 const char *, int , enum noside);
91 static struct value *value_maybe_namespace_elt (const struct type *,
95 static CORE_ADDR allocate_space_in_inferior (int);
97 static struct value *cast_into_complex (struct type *, struct value *);
99 static void find_method_list (struct value **, const char *,
100 LONGEST, struct type *, struct fn_field **, int *,
101 VEC (xmethod_worker_ptr) **,
102 struct type **, LONGEST *);
104 void _initialize_valops (void);
107 /* Flag for whether we want to abandon failed expression evals by
110 static int auto_abandon = 0;
113 int overload_resolution = 0;
115 show_overload_resolution (struct ui_file *file, int from_tty,
116 struct cmd_list_element *c,
119 fprintf_filtered (file, _("Overload resolution in evaluating "
120 "C++ functions is %s.\n"),
124 /* Find the address of function name NAME in the inferior. If OBJF_P
125 is non-NULL, *OBJF_P will be set to the OBJFILE where the function
129 find_function_in_inferior (const char *name, struct objfile **objf_p)
131 struct block_symbol sym;
133 sym = lookup_symbol (name, 0, VAR_DOMAIN, 0);
134 if (sym.symbol != NULL)
136 if (SYMBOL_CLASS (sym.symbol) != LOC_BLOCK)
138 error (_("\"%s\" exists in this program but is not a function."),
143 *objf_p = symbol_objfile (sym.symbol);
145 return value_of_variable (sym.symbol, sym.block);
149 struct bound_minimal_symbol msymbol =
150 lookup_bound_minimal_symbol (name);
152 if (msymbol.minsym != NULL)
154 struct objfile *objfile = msymbol.objfile;
155 struct gdbarch *gdbarch = get_objfile_arch (objfile);
159 type = lookup_pointer_type (builtin_type (gdbarch)->builtin_char);
160 type = lookup_function_type (type);
161 type = lookup_pointer_type (type);
162 maddr = BMSYMBOL_VALUE_ADDRESS (msymbol);
167 return value_from_pointer (type, maddr);
171 if (!target_has_execution)
172 error (_("evaluation of this expression "
173 "requires the target program to be active"));
175 error (_("evaluation of this expression requires the "
176 "program to have a function \"%s\"."),
182 /* Allocate NBYTES of space in the inferior using the inferior's
183 malloc and return a value that is a pointer to the allocated
187 value_allocate_space_in_inferior (int len)
189 struct objfile *objf;
190 struct value *val = find_function_in_inferior ("malloc", &objf);
191 struct gdbarch *gdbarch = get_objfile_arch (objf);
192 struct value *blocklen;
194 blocklen = value_from_longest (builtin_type (gdbarch)->builtin_int, len);
195 val = call_function_by_hand (val, NULL, 1, &blocklen);
196 if (value_logical_not (val))
198 if (!target_has_execution)
199 error (_("No memory available to program now: "
200 "you need to start the target first"));
202 error (_("No memory available to program: call to malloc failed"));
208 allocate_space_in_inferior (int len)
210 return value_as_long (value_allocate_space_in_inferior (len));
213 /* Cast struct value VAL to type TYPE and return as a value.
214 Both type and val must be of TYPE_CODE_STRUCT or TYPE_CODE_UNION
215 for this to work. Typedef to one of the codes is permitted.
216 Returns NULL if the cast is neither an upcast nor a downcast. */
218 static struct value *
219 value_cast_structs (struct type *type, struct value *v2)
225 gdb_assert (type != NULL && v2 != NULL);
227 t1 = check_typedef (type);
228 t2 = check_typedef (value_type (v2));
230 /* Check preconditions. */
231 gdb_assert ((TYPE_CODE (t1) == TYPE_CODE_STRUCT
232 || TYPE_CODE (t1) == TYPE_CODE_UNION)
233 && !!"Precondition is that type is of STRUCT or UNION kind.");
234 gdb_assert ((TYPE_CODE (t2) == TYPE_CODE_STRUCT
235 || TYPE_CODE (t2) == TYPE_CODE_UNION)
236 && !!"Precondition is that value is of STRUCT or UNION kind");
238 if (TYPE_NAME (t1) != NULL
239 && TYPE_NAME (t2) != NULL
240 && !strcmp (TYPE_NAME (t1), TYPE_NAME (t2)))
243 /* Upcasting: look in the type of the source to see if it contains the
244 type of the target as a superclass. If so, we'll need to
245 offset the pointer rather than just change its type. */
246 if (TYPE_NAME (t1) != NULL)
248 v = search_struct_field (type_name_no_tag (t1),
254 /* Downcasting: look in the type of the target to see if it contains the
255 type of the source as a superclass. If so, we'll need to
256 offset the pointer rather than just change its type. */
257 if (TYPE_NAME (t2) != NULL)
259 /* Try downcasting using the run-time type of the value. */
262 struct type *real_type;
264 real_type = value_rtti_type (v2, &full, &top, &using_enc);
267 v = value_full_object (v2, real_type, full, top, using_enc);
268 v = value_at_lazy (real_type, value_address (v));
269 real_type = value_type (v);
271 /* We might be trying to cast to the outermost enclosing
272 type, in which case search_struct_field won't work. */
273 if (TYPE_NAME (real_type) != NULL
274 && !strcmp (TYPE_NAME (real_type), TYPE_NAME (t1)))
277 v = search_struct_field (type_name_no_tag (t2), v, real_type, 1);
282 /* Try downcasting using information from the destination type
283 T2. This wouldn't work properly for classes with virtual
284 bases, but those were handled above. */
285 v = search_struct_field (type_name_no_tag (t2),
286 value_zero (t1, not_lval), t1, 1);
289 /* Downcasting is possible (t1 is superclass of v2). */
290 CORE_ADDR addr2 = value_address (v2);
292 addr2 -= value_address (v) + value_embedded_offset (v);
293 return value_at (type, addr2);
300 /* Cast one pointer or reference type to another. Both TYPE and
301 the type of ARG2 should be pointer types, or else both should be
302 reference types. If SUBCLASS_CHECK is non-zero, this will force a
303 check to see whether TYPE is a superclass of ARG2's type. If
304 SUBCLASS_CHECK is zero, then the subclass check is done only when
305 ARG2 is itself non-zero. Returns the new pointer or reference. */
308 value_cast_pointers (struct type *type, struct value *arg2,
311 struct type *type1 = check_typedef (type);
312 struct type *type2 = check_typedef (value_type (arg2));
313 struct type *t1 = check_typedef (TYPE_TARGET_TYPE (type1));
314 struct type *t2 = check_typedef (TYPE_TARGET_TYPE (type2));
316 if (TYPE_CODE (t1) == TYPE_CODE_STRUCT
317 && TYPE_CODE (t2) == TYPE_CODE_STRUCT
318 && (subclass_check || !value_logical_not (arg2)))
322 if (TYPE_IS_REFERENCE (type2))
323 v2 = coerce_ref (arg2);
325 v2 = value_ind (arg2);
326 gdb_assert (TYPE_CODE (check_typedef (value_type (v2)))
327 == TYPE_CODE_STRUCT && !!"Why did coercion fail?");
328 v2 = value_cast_structs (t1, v2);
329 /* At this point we have what we can have, un-dereference if needed. */
332 struct value *v = value_addr (v2);
334 deprecated_set_value_type (v, type);
339 /* No superclass found, just change the pointer type. */
340 arg2 = value_copy (arg2);
341 deprecated_set_value_type (arg2, type);
342 set_value_enclosing_type (arg2, type);
343 set_value_pointed_to_offset (arg2, 0); /* pai: chk_val */
347 /* Cast value ARG2 to type TYPE and return as a value.
348 More general than a C cast: accepts any two types of the same length,
349 and if ARG2 is an lvalue it can be cast into anything at all. */
350 /* In C++, casts may change pointer or object representations. */
353 value_cast (struct type *type, struct value *arg2)
355 enum type_code code1;
356 enum type_code code2;
360 int convert_to_boolean = 0;
362 if (value_type (arg2) == type)
365 /* Check if we are casting struct reference to struct reference. */
366 if (TYPE_IS_REFERENCE (check_typedef (type)))
368 /* We dereference type; then we recurse and finally
369 we generate value of the given reference. Nothing wrong with
371 struct type *t1 = check_typedef (type);
372 struct type *dereftype = check_typedef (TYPE_TARGET_TYPE (t1));
373 struct value *val = value_cast (dereftype, arg2);
375 return value_ref (val, TYPE_CODE (t1));
378 if (TYPE_IS_REFERENCE (check_typedef (value_type (arg2))))
379 /* We deref the value and then do the cast. */
380 return value_cast (type, coerce_ref (arg2));
382 /* Strip typedefs / resolve stubs in order to get at the type's
383 code/length, but remember the original type, to use as the
384 resulting type of the cast, in case it was a typedef. */
385 struct type *to_type = type;
387 type = check_typedef (type);
388 code1 = TYPE_CODE (type);
389 arg2 = coerce_ref (arg2);
390 type2 = check_typedef (value_type (arg2));
392 /* You can't cast to a reference type. See value_cast_pointers
394 gdb_assert (!TYPE_IS_REFERENCE (type));
396 /* A cast to an undetermined-length array_type, such as
397 (TYPE [])OBJECT, is treated like a cast to (TYPE [N])OBJECT,
398 where N is sizeof(OBJECT)/sizeof(TYPE). */
399 if (code1 == TYPE_CODE_ARRAY)
401 struct type *element_type = TYPE_TARGET_TYPE (type);
402 unsigned element_length = TYPE_LENGTH (check_typedef (element_type));
404 if (element_length > 0 && TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
406 struct type *range_type = TYPE_INDEX_TYPE (type);
407 int val_length = TYPE_LENGTH (type2);
408 LONGEST low_bound, high_bound, new_length;
410 if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
411 low_bound = 0, high_bound = 0;
412 new_length = val_length / element_length;
413 if (val_length % element_length != 0)
414 warning (_("array element type size does not "
415 "divide object size in cast"));
416 /* FIXME-type-allocation: need a way to free this type when
417 we are done with it. */
418 range_type = create_static_range_type ((struct type *) NULL,
419 TYPE_TARGET_TYPE (range_type),
421 new_length + low_bound - 1);
422 deprecated_set_value_type (arg2,
423 create_array_type ((struct type *) NULL,
430 if (current_language->c_style_arrays
431 && TYPE_CODE (type2) == TYPE_CODE_ARRAY
432 && !TYPE_VECTOR (type2))
433 arg2 = value_coerce_array (arg2);
435 if (TYPE_CODE (type2) == TYPE_CODE_FUNC)
436 arg2 = value_coerce_function (arg2);
438 type2 = check_typedef (value_type (arg2));
439 code2 = TYPE_CODE (type2);
441 if (code1 == TYPE_CODE_COMPLEX)
442 return cast_into_complex (to_type, arg2);
443 if (code1 == TYPE_CODE_BOOL)
445 code1 = TYPE_CODE_INT;
446 convert_to_boolean = 1;
448 if (code1 == TYPE_CODE_CHAR)
449 code1 = TYPE_CODE_INT;
450 if (code2 == TYPE_CODE_BOOL || code2 == TYPE_CODE_CHAR)
451 code2 = TYPE_CODE_INT;
453 scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
454 || code2 == TYPE_CODE_DECFLOAT || code2 == TYPE_CODE_ENUM
455 || code2 == TYPE_CODE_RANGE);
457 if ((code1 == TYPE_CODE_STRUCT || code1 == TYPE_CODE_UNION)
458 && (code2 == TYPE_CODE_STRUCT || code2 == TYPE_CODE_UNION)
459 && TYPE_NAME (type) != 0)
461 struct value *v = value_cast_structs (to_type, arg2);
467 if (code1 == TYPE_CODE_FLT && scalar)
468 return value_from_double (to_type, value_as_double (arg2));
469 else if (code1 == TYPE_CODE_DECFLOAT && scalar)
471 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
472 int dec_len = TYPE_LENGTH (type);
475 if (code2 == TYPE_CODE_FLT)
476 decimal_from_floating (arg2, dec, dec_len, byte_order);
477 else if (code2 == TYPE_CODE_DECFLOAT)
478 decimal_convert (value_contents (arg2), TYPE_LENGTH (type2),
479 byte_order, dec, dec_len, byte_order);
481 /* The only option left is an integral type. */
482 decimal_from_integral (arg2, dec, dec_len, byte_order);
484 return value_from_decfloat (to_type, dec);
486 else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM
487 || code1 == TYPE_CODE_RANGE)
488 && (scalar || code2 == TYPE_CODE_PTR
489 || code2 == TYPE_CODE_MEMBERPTR))
493 /* When we cast pointers to integers, we mustn't use
494 gdbarch_pointer_to_address to find the address the pointer
495 represents, as value_as_long would. GDB should evaluate
496 expressions just as the compiler would --- and the compiler
497 sees a cast as a simple reinterpretation of the pointer's
499 if (code2 == TYPE_CODE_PTR)
500 longest = extract_unsigned_integer
501 (value_contents (arg2), TYPE_LENGTH (type2),
502 gdbarch_byte_order (get_type_arch (type2)));
504 longest = value_as_long (arg2);
505 return value_from_longest (to_type, convert_to_boolean ?
506 (LONGEST) (longest ? 1 : 0) : longest);
508 else if (code1 == TYPE_CODE_PTR && (code2 == TYPE_CODE_INT
509 || code2 == TYPE_CODE_ENUM
510 || code2 == TYPE_CODE_RANGE))
512 /* TYPE_LENGTH (type) is the length of a pointer, but we really
513 want the length of an address! -- we are really dealing with
514 addresses (i.e., gdb representations) not pointers (i.e.,
515 target representations) here.
517 This allows things like "print *(int *)0x01000234" to work
518 without printing a misleading message -- which would
519 otherwise occur when dealing with a target having two byte
520 pointers and four byte addresses. */
522 int addr_bit = gdbarch_addr_bit (get_type_arch (type2));
523 LONGEST longest = value_as_long (arg2);
525 if (addr_bit < sizeof (LONGEST) * HOST_CHAR_BIT)
527 if (longest >= ((LONGEST) 1 << addr_bit)
528 || longest <= -((LONGEST) 1 << addr_bit))
529 warning (_("value truncated"));
531 return value_from_longest (to_type, longest);
533 else if (code1 == TYPE_CODE_METHODPTR && code2 == TYPE_CODE_INT
534 && value_as_long (arg2) == 0)
536 struct value *result = allocate_value (to_type);
538 cplus_make_method_ptr (to_type, value_contents_writeable (result), 0, 0);
541 else if (code1 == TYPE_CODE_MEMBERPTR && code2 == TYPE_CODE_INT
542 && value_as_long (arg2) == 0)
544 /* The Itanium C++ ABI represents NULL pointers to members as
545 minus one, instead of biasing the normal case. */
546 return value_from_longest (to_type, -1);
548 else if (code1 == TYPE_CODE_ARRAY && TYPE_VECTOR (type)
549 && code2 == TYPE_CODE_ARRAY && TYPE_VECTOR (type2)
550 && TYPE_LENGTH (type) != TYPE_LENGTH (type2))
551 error (_("Cannot convert between vector values of different sizes"));
552 else if (code1 == TYPE_CODE_ARRAY && TYPE_VECTOR (type) && scalar
553 && TYPE_LENGTH (type) != TYPE_LENGTH (type2))
554 error (_("can only cast scalar to vector of same size"));
555 else if (code1 == TYPE_CODE_VOID)
557 return value_zero (to_type, not_lval);
559 else if (TYPE_LENGTH (type) == TYPE_LENGTH (type2))
561 if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
562 return value_cast_pointers (to_type, arg2, 0);
564 arg2 = value_copy (arg2);
565 deprecated_set_value_type (arg2, to_type);
566 set_value_enclosing_type (arg2, to_type);
567 set_value_pointed_to_offset (arg2, 0); /* pai: chk_val */
570 else if (VALUE_LVAL (arg2) == lval_memory)
571 return value_at_lazy (to_type, value_address (arg2));
574 error (_("Invalid cast."));
579 /* The C++ reinterpret_cast operator. */
582 value_reinterpret_cast (struct type *type, struct value *arg)
584 struct value *result;
585 struct type *real_type = check_typedef (type);
586 struct type *arg_type, *dest_type;
588 enum type_code dest_code, arg_code;
590 /* Do reference, function, and array conversion. */
591 arg = coerce_array (arg);
593 /* Attempt to preserve the type the user asked for. */
596 /* If we are casting to a reference type, transform
597 reinterpret_cast<T&[&]>(V) to *reinterpret_cast<T*>(&V). */
598 if (TYPE_IS_REFERENCE (real_type))
601 arg = value_addr (arg);
602 dest_type = lookup_pointer_type (TYPE_TARGET_TYPE (dest_type));
603 real_type = lookup_pointer_type (real_type);
606 arg_type = value_type (arg);
608 dest_code = TYPE_CODE (real_type);
609 arg_code = TYPE_CODE (arg_type);
611 /* We can convert pointer types, or any pointer type to int, or int
613 if ((dest_code == TYPE_CODE_PTR && arg_code == TYPE_CODE_INT)
614 || (dest_code == TYPE_CODE_INT && arg_code == TYPE_CODE_PTR)
615 || (dest_code == TYPE_CODE_METHODPTR && arg_code == TYPE_CODE_INT)
616 || (dest_code == TYPE_CODE_INT && arg_code == TYPE_CODE_METHODPTR)
617 || (dest_code == TYPE_CODE_MEMBERPTR && arg_code == TYPE_CODE_INT)
618 || (dest_code == TYPE_CODE_INT && arg_code == TYPE_CODE_MEMBERPTR)
619 || (dest_code == arg_code
620 && (dest_code == TYPE_CODE_PTR
621 || dest_code == TYPE_CODE_METHODPTR
622 || dest_code == TYPE_CODE_MEMBERPTR)))
623 result = value_cast (dest_type, arg);
625 error (_("Invalid reinterpret_cast"));
628 result = value_cast (type, value_ref (value_ind (result),
634 /* A helper for value_dynamic_cast. This implements the first of two
635 runtime checks: we iterate over all the base classes of the value's
636 class which are equal to the desired class; if only one of these
637 holds the value, then it is the answer. */
640 dynamic_cast_check_1 (struct type *desired_type,
641 const gdb_byte *valaddr,
642 LONGEST embedded_offset,
645 struct type *search_type,
647 struct type *arg_type,
648 struct value **result)
650 int i, result_count = 0;
652 for (i = 0; i < TYPE_N_BASECLASSES (search_type) && result_count < 2; ++i)
654 LONGEST offset = baseclass_offset (search_type, i, valaddr,
658 if (class_types_same_p (desired_type, TYPE_BASECLASS (search_type, i)))
660 if (address + embedded_offset + offset >= arg_addr
661 && address + embedded_offset + offset < arg_addr + TYPE_LENGTH (arg_type))
665 *result = value_at_lazy (TYPE_BASECLASS (search_type, i),
666 address + embedded_offset + offset);
670 result_count += dynamic_cast_check_1 (desired_type,
672 embedded_offset + offset,
674 TYPE_BASECLASS (search_type, i),
683 /* A helper for value_dynamic_cast. This implements the second of two
684 runtime checks: we look for a unique public sibling class of the
685 argument's declared class. */
688 dynamic_cast_check_2 (struct type *desired_type,
689 const gdb_byte *valaddr,
690 LONGEST embedded_offset,
693 struct type *search_type,
694 struct value **result)
696 int i, result_count = 0;
698 for (i = 0; i < TYPE_N_BASECLASSES (search_type) && result_count < 2; ++i)
702 if (! BASETYPE_VIA_PUBLIC (search_type, i))
705 offset = baseclass_offset (search_type, i, valaddr, embedded_offset,
707 if (class_types_same_p (desired_type, TYPE_BASECLASS (search_type, i)))
711 *result = value_at_lazy (TYPE_BASECLASS (search_type, i),
712 address + embedded_offset + offset);
715 result_count += dynamic_cast_check_2 (desired_type,
717 embedded_offset + offset,
719 TYPE_BASECLASS (search_type, i),
726 /* The C++ dynamic_cast operator. */
729 value_dynamic_cast (struct type *type, struct value *arg)
733 struct type *resolved_type = check_typedef (type);
734 struct type *arg_type = check_typedef (value_type (arg));
735 struct type *class_type, *rtti_type;
736 struct value *result, *tem, *original_arg = arg;
738 int is_ref = TYPE_IS_REFERENCE (resolved_type);
740 if (TYPE_CODE (resolved_type) != TYPE_CODE_PTR
741 && !TYPE_IS_REFERENCE (resolved_type))
742 error (_("Argument to dynamic_cast must be a pointer or reference type"));
743 if (TYPE_CODE (TYPE_TARGET_TYPE (resolved_type)) != TYPE_CODE_VOID
744 && TYPE_CODE (TYPE_TARGET_TYPE (resolved_type)) != TYPE_CODE_STRUCT)
745 error (_("Argument to dynamic_cast must be pointer to class or `void *'"));
747 class_type = check_typedef (TYPE_TARGET_TYPE (resolved_type));
748 if (TYPE_CODE (resolved_type) == TYPE_CODE_PTR)
750 if (TYPE_CODE (arg_type) != TYPE_CODE_PTR
751 && ! (TYPE_CODE (arg_type) == TYPE_CODE_INT
752 && value_as_long (arg) == 0))
753 error (_("Argument to dynamic_cast does not have pointer type"));
754 if (TYPE_CODE (arg_type) == TYPE_CODE_PTR)
756 arg_type = check_typedef (TYPE_TARGET_TYPE (arg_type));
757 if (TYPE_CODE (arg_type) != TYPE_CODE_STRUCT)
758 error (_("Argument to dynamic_cast does "
759 "not have pointer to class type"));
762 /* Handle NULL pointers. */
763 if (value_as_long (arg) == 0)
764 return value_zero (type, not_lval);
766 arg = value_ind (arg);
770 if (TYPE_CODE (arg_type) != TYPE_CODE_STRUCT)
771 error (_("Argument to dynamic_cast does not have class type"));
774 /* If the classes are the same, just return the argument. */
775 if (class_types_same_p (class_type, arg_type))
776 return value_cast (type, arg);
778 /* If the target type is a unique base class of the argument's
779 declared type, just cast it. */
780 if (is_ancestor (class_type, arg_type))
782 if (is_unique_ancestor (class_type, arg))
783 return value_cast (type, original_arg);
784 error (_("Ambiguous dynamic_cast"));
787 rtti_type = value_rtti_type (arg, &full, &top, &using_enc);
789 error (_("Couldn't determine value's most derived type for dynamic_cast"));
791 /* Compute the most derived object's address. */
792 addr = value_address (arg);
800 addr += top + value_embedded_offset (arg);
802 /* dynamic_cast<void *> means to return a pointer to the
803 most-derived object. */
804 if (TYPE_CODE (resolved_type) == TYPE_CODE_PTR
805 && TYPE_CODE (TYPE_TARGET_TYPE (resolved_type)) == TYPE_CODE_VOID)
806 return value_at_lazy (type, addr);
808 tem = value_at (type, addr);
809 type = value_type (tem);
811 /* The first dynamic check specified in 5.2.7. */
812 if (is_public_ancestor (arg_type, TYPE_TARGET_TYPE (resolved_type)))
814 if (class_types_same_p (rtti_type, TYPE_TARGET_TYPE (resolved_type)))
817 if (dynamic_cast_check_1 (TYPE_TARGET_TYPE (resolved_type),
818 value_contents_for_printing (tem),
819 value_embedded_offset (tem),
820 value_address (tem), tem,
824 return value_cast (type,
826 ? value_ref (result, TYPE_CODE (resolved_type))
827 : value_addr (result));
830 /* The second dynamic check specified in 5.2.7. */
832 if (is_public_ancestor (arg_type, rtti_type)
833 && dynamic_cast_check_2 (TYPE_TARGET_TYPE (resolved_type),
834 value_contents_for_printing (tem),
835 value_embedded_offset (tem),
836 value_address (tem), tem,
837 rtti_type, &result) == 1)
838 return value_cast (type,
840 ? value_ref (result, TYPE_CODE (resolved_type))
841 : value_addr (result));
843 if (TYPE_CODE (resolved_type) == TYPE_CODE_PTR)
844 return value_zero (type, not_lval);
846 error (_("dynamic_cast failed"));
849 /* Create a value of type TYPE that is zero, and return it. */
852 value_zero (struct type *type, enum lval_type lv)
854 struct value *val = allocate_value (type);
856 VALUE_LVAL (val) = (lv == lval_computed ? not_lval : lv);
860 /* Create a not_lval value of numeric type TYPE that is one, and return it. */
863 value_one (struct type *type)
865 struct type *type1 = check_typedef (type);
868 if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
870 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
873 decimal_from_string (v, TYPE_LENGTH (type), byte_order, "1");
874 val = value_from_decfloat (type, v);
876 else if (TYPE_CODE (type1) == TYPE_CODE_FLT)
878 val = value_from_double (type, (DOUBLEST) 1);
880 else if (is_integral_type (type1))
882 val = value_from_longest (type, (LONGEST) 1);
884 else if (TYPE_CODE (type1) == TYPE_CODE_ARRAY && TYPE_VECTOR (type1))
886 struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type1));
888 LONGEST low_bound, high_bound;
891 if (!get_array_bounds (type1, &low_bound, &high_bound))
892 error (_("Could not determine the vector bounds"));
894 val = allocate_value (type);
895 for (i = 0; i < high_bound - low_bound + 1; i++)
897 tmp = value_one (eltype);
898 memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype),
899 value_contents_all (tmp), TYPE_LENGTH (eltype));
904 error (_("Not a numeric type."));
907 /* value_one result is never used for assignments to. */
908 gdb_assert (VALUE_LVAL (val) == not_lval);
913 /* Helper function for value_at, value_at_lazy, and value_at_lazy_stack.
914 The type of the created value may differ from the passed type TYPE.
915 Make sure to retrieve the returned values's new type after this call
916 e.g. in case the type is a variable length array. */
918 static struct value *
919 get_value_at (struct type *type, CORE_ADDR addr, int lazy)
923 if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
924 error (_("Attempt to dereference a generic pointer."));
926 val = value_from_contents_and_address (type, NULL, addr);
929 value_fetch_lazy (val);
934 /* Return a value with type TYPE located at ADDR.
936 Call value_at only if the data needs to be fetched immediately;
937 if we can be 'lazy' and defer the fetch, perhaps indefinately, call
938 value_at_lazy instead. value_at_lazy simply records the address of
939 the data and sets the lazy-evaluation-required flag. The lazy flag
940 is tested in the value_contents macro, which is used if and when
941 the contents are actually required. The type of the created value
942 may differ from the passed type TYPE. Make sure to retrieve the
943 returned values's new type after this call e.g. in case the type
944 is a variable length array.
946 Note: value_at does *NOT* handle embedded offsets; perform such
947 adjustments before or after calling it. */
950 value_at (struct type *type, CORE_ADDR addr)
952 return get_value_at (type, addr, 0);
955 /* Return a lazy value with type TYPE located at ADDR (cf. value_at).
956 The type of the created value may differ from the passed type TYPE.
957 Make sure to retrieve the returned values's new type after this call
958 e.g. in case the type is a variable length array. */
961 value_at_lazy (struct type *type, CORE_ADDR addr)
963 return get_value_at (type, addr, 1);
967 read_value_memory (struct value *val, LONGEST bit_offset,
968 int stack, CORE_ADDR memaddr,
969 gdb_byte *buffer, size_t length)
971 ULONGEST xfered_total = 0;
972 struct gdbarch *arch = get_value_arch (val);
973 int unit_size = gdbarch_addressable_memory_unit_size (arch);
974 enum target_object object;
976 object = stack ? TARGET_OBJECT_STACK_MEMORY : TARGET_OBJECT_MEMORY;
978 while (xfered_total < length)
980 enum target_xfer_status status;
981 ULONGEST xfered_partial;
983 status = target_xfer_partial (current_target.beneath,
985 buffer + xfered_total * unit_size, NULL,
986 memaddr + xfered_total,
987 length - xfered_total,
990 if (status == TARGET_XFER_OK)
992 else if (status == TARGET_XFER_UNAVAILABLE)
993 mark_value_bits_unavailable (val, (xfered_total * HOST_CHAR_BIT
995 xfered_partial * HOST_CHAR_BIT);
996 else if (status == TARGET_XFER_EOF)
997 memory_error (TARGET_XFER_E_IO, memaddr + xfered_total);
999 memory_error (status, memaddr + xfered_total);
1001 xfered_total += xfered_partial;
1006 /* Store the contents of FROMVAL into the location of TOVAL.
1007 Return a new value with the location of TOVAL and contents of FROMVAL. */
1010 value_assign (struct value *toval, struct value *fromval)
1014 struct frame_id old_frame;
1016 if (!deprecated_value_modifiable (toval))
1017 error (_("Left operand of assignment is not a modifiable lvalue."));
1019 toval = coerce_ref (toval);
1021 type = value_type (toval);
1022 if (VALUE_LVAL (toval) != lval_internalvar)
1023 fromval = value_cast (type, fromval);
1026 /* Coerce arrays and functions to pointers, except for arrays
1027 which only live in GDB's storage. */
1028 if (!value_must_coerce_to_target (fromval))
1029 fromval = coerce_array (fromval);
1032 type = check_typedef (type);
1034 /* Since modifying a register can trash the frame chain, and
1035 modifying memory can trash the frame cache, we save the old frame
1036 and then restore the new frame afterwards. */
1037 old_frame = get_frame_id (deprecated_safe_get_selected_frame ());
1039 switch (VALUE_LVAL (toval))
1041 case lval_internalvar:
1042 set_internalvar (VALUE_INTERNALVAR (toval), fromval);
1043 return value_of_internalvar (get_type_arch (type),
1044 VALUE_INTERNALVAR (toval));
1046 case lval_internalvar_component:
1048 LONGEST offset = value_offset (toval);
1050 /* Are we dealing with a bitfield?
1052 It is important to mention that `value_parent (toval)' is
1053 non-NULL iff `value_bitsize (toval)' is non-zero. */
1054 if (value_bitsize (toval))
1056 /* VALUE_INTERNALVAR below refers to the parent value, while
1057 the offset is relative to this parent value. */
1058 gdb_assert (value_parent (value_parent (toval)) == NULL);
1059 offset += value_offset (value_parent (toval));
1062 set_internalvar_component (VALUE_INTERNALVAR (toval),
1064 value_bitpos (toval),
1065 value_bitsize (toval),
1072 const gdb_byte *dest_buffer;
1073 CORE_ADDR changed_addr;
1075 gdb_byte buffer[sizeof (LONGEST)];
1077 if (value_bitsize (toval))
1079 struct value *parent = value_parent (toval);
1081 changed_addr = value_address (parent) + value_offset (toval);
1082 changed_len = (value_bitpos (toval)
1083 + value_bitsize (toval)
1084 + HOST_CHAR_BIT - 1)
1087 /* If we can read-modify-write exactly the size of the
1088 containing type (e.g. short or int) then do so. This
1089 is safer for volatile bitfields mapped to hardware
1091 if (changed_len < TYPE_LENGTH (type)
1092 && TYPE_LENGTH (type) <= (int) sizeof (LONGEST)
1093 && ((LONGEST) changed_addr % TYPE_LENGTH (type)) == 0)
1094 changed_len = TYPE_LENGTH (type);
1096 if (changed_len > (int) sizeof (LONGEST))
1097 error (_("Can't handle bitfields which "
1098 "don't fit in a %d bit word."),
1099 (int) sizeof (LONGEST) * HOST_CHAR_BIT);
1101 read_memory (changed_addr, buffer, changed_len);
1102 modify_field (type, buffer, value_as_long (fromval),
1103 value_bitpos (toval), value_bitsize (toval));
1104 dest_buffer = buffer;
1108 changed_addr = value_address (toval);
1109 changed_len = type_length_units (type);
1110 dest_buffer = value_contents (fromval);
1113 write_memory_with_notification (changed_addr, dest_buffer, changed_len);
1119 struct frame_info *frame;
1120 struct gdbarch *gdbarch;
1123 /* Figure out which frame this is in currently.
1125 We use VALUE_FRAME_ID for obtaining the value's frame id instead of
1126 VALUE_NEXT_FRAME_ID due to requiring a frame which may be passed to
1127 put_frame_register_bytes() below. That function will (eventually)
1128 perform the necessary unwind operation by first obtaining the next
1130 frame = frame_find_by_id (VALUE_FRAME_ID (toval));
1132 value_reg = VALUE_REGNUM (toval);
1135 error (_("Value being assigned to is no longer active."));
1137 gdbarch = get_frame_arch (frame);
1139 if (value_bitsize (toval))
1141 struct value *parent = value_parent (toval);
1142 LONGEST offset = value_offset (parent) + value_offset (toval);
1144 gdb_byte buffer[sizeof (LONGEST)];
1147 changed_len = (value_bitpos (toval)
1148 + value_bitsize (toval)
1149 + HOST_CHAR_BIT - 1)
1152 if (changed_len > (int) sizeof (LONGEST))
1153 error (_("Can't handle bitfields which "
1154 "don't fit in a %d bit word."),
1155 (int) sizeof (LONGEST) * HOST_CHAR_BIT);
1157 if (!get_frame_register_bytes (frame, value_reg, offset,
1158 changed_len, buffer,
1162 throw_error (OPTIMIZED_OUT_ERROR,
1163 _("value has been optimized out"));
1165 throw_error (NOT_AVAILABLE_ERROR,
1166 _("value is not available"));
1169 modify_field (type, buffer, value_as_long (fromval),
1170 value_bitpos (toval), value_bitsize (toval));
1172 put_frame_register_bytes (frame, value_reg, offset,
1173 changed_len, buffer);
1177 if (gdbarch_convert_register_p (gdbarch, VALUE_REGNUM (toval),
1180 /* If TOVAL is a special machine register requiring
1181 conversion of program values to a special raw
1183 gdbarch_value_to_register (gdbarch, frame,
1184 VALUE_REGNUM (toval), type,
1185 value_contents (fromval));
1189 put_frame_register_bytes (frame, value_reg,
1190 value_offset (toval),
1192 value_contents (fromval));
1196 observer_notify_register_changed (frame, value_reg);
1202 const struct lval_funcs *funcs = value_computed_funcs (toval);
1204 if (funcs->write != NULL)
1206 funcs->write (toval, fromval);
1213 error (_("Left operand of assignment is not an lvalue."));
1216 /* Assigning to the stack pointer, frame pointer, and other
1217 (architecture and calling convention specific) registers may
1218 cause the frame cache and regcache to be out of date. Assigning to memory
1219 also can. We just do this on all assignments to registers or
1220 memory, for simplicity's sake; I doubt the slowdown matters. */
1221 switch (VALUE_LVAL (toval))
1227 observer_notify_target_changed (¤t_target);
1229 /* Having destroyed the frame cache, restore the selected
1232 /* FIXME: cagney/2002-11-02: There has to be a better way of
1233 doing this. Instead of constantly saving/restoring the
1234 frame. Why not create a get_selected_frame() function that,
1235 having saved the selected frame's ID can automatically
1236 re-find the previously selected frame automatically. */
1239 struct frame_info *fi = frame_find_by_id (old_frame);
1250 /* If the field does not entirely fill a LONGEST, then zero the sign
1251 bits. If the field is signed, and is negative, then sign
1253 if ((value_bitsize (toval) > 0)
1254 && (value_bitsize (toval) < 8 * (int) sizeof (LONGEST)))
1256 LONGEST fieldval = value_as_long (fromval);
1257 LONGEST valmask = (((ULONGEST) 1) << value_bitsize (toval)) - 1;
1259 fieldval &= valmask;
1260 if (!TYPE_UNSIGNED (type)
1261 && (fieldval & (valmask ^ (valmask >> 1))))
1262 fieldval |= ~valmask;
1264 fromval = value_from_longest (type, fieldval);
1267 /* The return value is a copy of TOVAL so it shares its location
1268 information, but its contents are updated from FROMVAL. This
1269 implies the returned value is not lazy, even if TOVAL was. */
1270 val = value_copy (toval);
1271 set_value_lazy (val, 0);
1272 memcpy (value_contents_raw (val), value_contents (fromval),
1273 TYPE_LENGTH (type));
1275 /* We copy over the enclosing type and pointed-to offset from FROMVAL
1276 in the case of pointer types. For object types, the enclosing type
1277 and embedded offset must *not* be copied: the target object refered
1278 to by TOVAL retains its original dynamic type after assignment. */
1279 if (TYPE_CODE (type) == TYPE_CODE_PTR)
1281 set_value_enclosing_type (val, value_enclosing_type (fromval));
1282 set_value_pointed_to_offset (val, value_pointed_to_offset (fromval));
1288 /* Extend a value VAL to COUNT repetitions of its type. */
1291 value_repeat (struct value *arg1, int count)
1295 if (VALUE_LVAL (arg1) != lval_memory)
1296 error (_("Only values in memory can be extended with '@'."));
1298 error (_("Invalid number %d of repetitions."), count);
1300 val = allocate_repeat_value (value_enclosing_type (arg1), count);
1302 VALUE_LVAL (val) = lval_memory;
1303 set_value_address (val, value_address (arg1));
1305 read_value_memory (val, 0, value_stack (val), value_address (val),
1306 value_contents_all_raw (val),
1307 type_length_units (value_enclosing_type (val)));
1313 value_of_variable (struct symbol *var, const struct block *b)
1315 struct frame_info *frame = NULL;
1317 if (symbol_read_needs_frame (var))
1318 frame = get_selected_frame (_("No frame selected."));
1320 return read_var_value (var, b, frame);
1324 address_of_variable (struct symbol *var, const struct block *b)
1326 struct type *type = SYMBOL_TYPE (var);
1329 /* Evaluate it first; if the result is a memory address, we're fine.
1330 Lazy evaluation pays off here. */
1332 val = value_of_variable (var, b);
1333 type = value_type (val);
1335 if ((VALUE_LVAL (val) == lval_memory && value_lazy (val))
1336 || TYPE_CODE (type) == TYPE_CODE_FUNC)
1338 CORE_ADDR addr = value_address (val);
1340 return value_from_pointer (lookup_pointer_type (type), addr);
1343 /* Not a memory address; check what the problem was. */
1344 switch (VALUE_LVAL (val))
1348 struct frame_info *frame;
1349 const char *regname;
1351 frame = frame_find_by_id (VALUE_NEXT_FRAME_ID (val));
1354 regname = gdbarch_register_name (get_frame_arch (frame),
1355 VALUE_REGNUM (val));
1356 gdb_assert (regname && *regname);
1358 error (_("Address requested for identifier "
1359 "\"%s\" which is in register $%s"),
1360 SYMBOL_PRINT_NAME (var), regname);
1365 error (_("Can't take address of \"%s\" which isn't an lvalue."),
1366 SYMBOL_PRINT_NAME (var));
1373 /* Return one if VAL does not live in target memory, but should in order
1374 to operate on it. Otherwise return zero. */
1377 value_must_coerce_to_target (struct value *val)
1379 struct type *valtype;
1381 /* The only lval kinds which do not live in target memory. */
1382 if (VALUE_LVAL (val) != not_lval
1383 && VALUE_LVAL (val) != lval_internalvar
1384 && VALUE_LVAL (val) != lval_xcallable)
1387 valtype = check_typedef (value_type (val));
1389 switch (TYPE_CODE (valtype))
1391 case TYPE_CODE_ARRAY:
1392 return TYPE_VECTOR (valtype) ? 0 : 1;
1393 case TYPE_CODE_STRING:
1400 /* Make sure that VAL lives in target memory if it's supposed to. For
1401 instance, strings are constructed as character arrays in GDB's
1402 storage, and this function copies them to the target. */
1405 value_coerce_to_target (struct value *val)
1410 if (!value_must_coerce_to_target (val))
1413 length = TYPE_LENGTH (check_typedef (value_type (val)));
1414 addr = allocate_space_in_inferior (length);
1415 write_memory (addr, value_contents (val), length);
1416 return value_at_lazy (value_type (val), addr);
1419 /* Given a value which is an array, return a value which is a pointer
1420 to its first element, regardless of whether or not the array has a
1421 nonzero lower bound.
1423 FIXME: A previous comment here indicated that this routine should
1424 be substracting the array's lower bound. It's not clear to me that
1425 this is correct. Given an array subscripting operation, it would
1426 certainly work to do the adjustment here, essentially computing:
1428 (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0])
1430 However I believe a more appropriate and logical place to account
1431 for the lower bound is to do so in value_subscript, essentially
1434 (&array[0] + ((index - lowerbound) * sizeof array[0]))
1436 As further evidence consider what would happen with operations
1437 other than array subscripting, where the caller would get back a
1438 value that had an address somewhere before the actual first element
1439 of the array, and the information about the lower bound would be
1440 lost because of the coercion to pointer type. */
1443 value_coerce_array (struct value *arg1)
1445 struct type *type = check_typedef (value_type (arg1));
1447 /* If the user tries to do something requiring a pointer with an
1448 array that has not yet been pushed to the target, then this would
1449 be a good time to do so. */
1450 arg1 = value_coerce_to_target (arg1);
1452 if (VALUE_LVAL (arg1) != lval_memory)
1453 error (_("Attempt to take address of value not located in memory."));
1455 return value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
1456 value_address (arg1));
1459 /* Given a value which is a function, return a value which is a pointer
1463 value_coerce_function (struct value *arg1)
1465 struct value *retval;
1467 if (VALUE_LVAL (arg1) != lval_memory)
1468 error (_("Attempt to take address of value not located in memory."));
1470 retval = value_from_pointer (lookup_pointer_type (value_type (arg1)),
1471 value_address (arg1));
1475 /* Return a pointer value for the object for which ARG1 is the
1479 value_addr (struct value *arg1)
1482 struct type *type = check_typedef (value_type (arg1));
1484 if (TYPE_IS_REFERENCE (type))
1486 if (value_bits_synthetic_pointer (arg1, value_embedded_offset (arg1),
1487 TARGET_CHAR_BIT * TYPE_LENGTH (type)))
1488 arg1 = coerce_ref (arg1);
1491 /* Copy the value, but change the type from (T&) to (T*). We
1492 keep the same location information, which is efficient, and
1493 allows &(&X) to get the location containing the reference.
1494 Do the same to its enclosing type for consistency. */
1495 struct type *type_ptr
1496 = lookup_pointer_type (TYPE_TARGET_TYPE (type));
1497 struct type *enclosing_type
1498 = check_typedef (value_enclosing_type (arg1));
1499 struct type *enclosing_type_ptr
1500 = lookup_pointer_type (TYPE_TARGET_TYPE (enclosing_type));
1502 arg2 = value_copy (arg1);
1503 deprecated_set_value_type (arg2, type_ptr);
1504 set_value_enclosing_type (arg2, enclosing_type_ptr);
1509 if (TYPE_CODE (type) == TYPE_CODE_FUNC)
1510 return value_coerce_function (arg1);
1512 /* If this is an array that has not yet been pushed to the target,
1513 then this would be a good time to force it to memory. */
1514 arg1 = value_coerce_to_target (arg1);
1516 if (VALUE_LVAL (arg1) != lval_memory)
1517 error (_("Attempt to take address of value not located in memory."));
1519 /* Get target memory address. */
1520 arg2 = value_from_pointer (lookup_pointer_type (value_type (arg1)),
1521 (value_address (arg1)
1522 + value_embedded_offset (arg1)));
1524 /* This may be a pointer to a base subobject; so remember the
1525 full derived object's type ... */
1526 set_value_enclosing_type (arg2,
1527 lookup_pointer_type (value_enclosing_type (arg1)));
1528 /* ... and also the relative position of the subobject in the full
1530 set_value_pointed_to_offset (arg2, value_embedded_offset (arg1));
1534 /* Return a reference value for the object for which ARG1 is the
1538 value_ref (struct value *arg1, enum type_code refcode)
1541 struct type *type = check_typedef (value_type (arg1));
1543 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
1545 if ((TYPE_CODE (type) == TYPE_CODE_REF
1546 || TYPE_CODE (type) == TYPE_CODE_RVALUE_REF)
1547 && TYPE_CODE (type) == refcode)
1550 arg2 = value_addr (arg1);
1551 deprecated_set_value_type (arg2, lookup_reference_type (type, refcode));
1555 /* Given a value of a pointer type, apply the C unary * operator to
1559 value_ind (struct value *arg1)
1561 struct type *base_type;
1564 arg1 = coerce_array (arg1);
1566 base_type = check_typedef (value_type (arg1));
1568 if (VALUE_LVAL (arg1) == lval_computed)
1570 const struct lval_funcs *funcs = value_computed_funcs (arg1);
1572 if (funcs->indirect)
1574 struct value *result = funcs->indirect (arg1);
1581 if (TYPE_CODE (base_type) == TYPE_CODE_PTR)
1583 struct type *enc_type;
1585 /* We may be pointing to something embedded in a larger object.
1586 Get the real type of the enclosing object. */
1587 enc_type = check_typedef (value_enclosing_type (arg1));
1588 enc_type = TYPE_TARGET_TYPE (enc_type);
1590 if (TYPE_CODE (check_typedef (enc_type)) == TYPE_CODE_FUNC
1591 || TYPE_CODE (check_typedef (enc_type)) == TYPE_CODE_METHOD)
1592 /* For functions, go through find_function_addr, which knows
1593 how to handle function descriptors. */
1594 arg2 = value_at_lazy (enc_type,
1595 find_function_addr (arg1, NULL));
1597 /* Retrieve the enclosing object pointed to. */
1598 arg2 = value_at_lazy (enc_type,
1599 (value_as_address (arg1)
1600 - value_pointed_to_offset (arg1)));
1602 enc_type = value_type (arg2);
1603 return readjust_indirect_value_type (arg2, enc_type, base_type, arg1);
1606 error (_("Attempt to take contents of a non-pointer value."));
1607 return 0; /* For lint -- never reached. */
1610 /* Create a value for an array by allocating space in GDB, copying the
1611 data into that space, and then setting up an array value.
1613 The array bounds are set from LOWBOUND and HIGHBOUND, and the array
1614 is populated from the values passed in ELEMVEC.
1616 The element type of the array is inherited from the type of the
1617 first element, and all elements must have the same size (though we
1618 don't currently enforce any restriction on their types). */
1621 value_array (int lowbound, int highbound, struct value **elemvec)
1625 ULONGEST typelength;
1627 struct type *arraytype;
1629 /* Validate that the bounds are reasonable and that each of the
1630 elements have the same size. */
1632 nelem = highbound - lowbound + 1;
1635 error (_("bad array bounds (%d, %d)"), lowbound, highbound);
1637 typelength = type_length_units (value_enclosing_type (elemvec[0]));
1638 for (idx = 1; idx < nelem; idx++)
1640 if (type_length_units (value_enclosing_type (elemvec[idx]))
1643 error (_("array elements must all be the same size"));
1647 arraytype = lookup_array_range_type (value_enclosing_type (elemvec[0]),
1648 lowbound, highbound);
1650 if (!current_language->c_style_arrays)
1652 val = allocate_value (arraytype);
1653 for (idx = 0; idx < nelem; idx++)
1654 value_contents_copy (val, idx * typelength, elemvec[idx], 0,
1659 /* Allocate space to store the array, and then initialize it by
1660 copying in each element. */
1662 val = allocate_value (arraytype);
1663 for (idx = 0; idx < nelem; idx++)
1664 value_contents_copy (val, idx * typelength, elemvec[idx], 0, typelength);
1669 value_cstring (const char *ptr, ssize_t len, struct type *char_type)
1672 int lowbound = current_language->string_lower_bound;
1673 ssize_t highbound = len / TYPE_LENGTH (char_type);
1674 struct type *stringtype
1675 = lookup_array_range_type (char_type, lowbound, highbound + lowbound - 1);
1677 val = allocate_value (stringtype);
1678 memcpy (value_contents_raw (val), ptr, len);
1682 /* Create a value for a string constant by allocating space in the
1683 inferior, copying the data into that space, and returning the
1684 address with type TYPE_CODE_STRING. PTR points to the string
1685 constant data; LEN is number of characters.
1687 Note that string types are like array of char types with a lower
1688 bound of zero and an upper bound of LEN - 1. Also note that the
1689 string may contain embedded null bytes. */
1692 value_string (const char *ptr, ssize_t len, struct type *char_type)
1695 int lowbound = current_language->string_lower_bound;
1696 ssize_t highbound = len / TYPE_LENGTH (char_type);
1697 struct type *stringtype
1698 = lookup_string_range_type (char_type, lowbound, highbound + lowbound - 1);
1700 val = allocate_value (stringtype);
1701 memcpy (value_contents_raw (val), ptr, len);
1706 /* See if we can pass arguments in T2 to a function which takes
1707 arguments of types T1. T1 is a list of NARGS arguments, and T2 is
1708 a NULL-terminated vector. If some arguments need coercion of some
1709 sort, then the coerced values are written into T2. Return value is
1710 0 if the arguments could be matched, or the position at which they
1713 STATICP is nonzero if the T1 argument list came from a static
1714 member function. T2 will still include the ``this'' pointer, but
1717 For non-static member functions, we ignore the first argument,
1718 which is the type of the instance variable. This is because we
1719 want to handle calls with objects from derived classes. This is
1720 not entirely correct: we should actually check to make sure that a
1721 requested operation is type secure, shouldn't we? FIXME. */
1724 typecmp (int staticp, int varargs, int nargs,
1725 struct field t1[], struct value *t2[])
1730 internal_error (__FILE__, __LINE__,
1731 _("typecmp: no argument list"));
1733 /* Skip ``this'' argument if applicable. T2 will always include
1739 (i < nargs) && TYPE_CODE (t1[i].type) != TYPE_CODE_VOID;
1742 struct type *tt1, *tt2;
1747 tt1 = check_typedef (t1[i].type);
1748 tt2 = check_typedef (value_type (t2[i]));
1750 if (TYPE_IS_REFERENCE (tt1)
1751 /* We should be doing hairy argument matching, as below. */
1752 && (TYPE_CODE (check_typedef (TYPE_TARGET_TYPE (tt1)))
1753 == TYPE_CODE (tt2)))
1755 if (TYPE_CODE (tt2) == TYPE_CODE_ARRAY)
1756 t2[i] = value_coerce_array (t2[i]);
1758 t2[i] = value_ref (t2[i], TYPE_CODE (tt1));
1762 /* djb - 20000715 - Until the new type structure is in the
1763 place, and we can attempt things like implicit conversions,
1764 we need to do this so you can take something like a map<const
1765 char *>, and properly access map["hello"], because the
1766 argument to [] will be a reference to a pointer to a char,
1767 and the argument will be a pointer to a char. */
1768 while (TYPE_IS_REFERENCE (tt1) || TYPE_CODE (tt1) == TYPE_CODE_PTR)
1770 tt1 = check_typedef( TYPE_TARGET_TYPE(tt1) );
1772 while (TYPE_CODE(tt2) == TYPE_CODE_ARRAY
1773 || TYPE_CODE(tt2) == TYPE_CODE_PTR
1774 || TYPE_IS_REFERENCE (tt2))
1776 tt2 = check_typedef (TYPE_TARGET_TYPE(tt2));
1778 if (TYPE_CODE (tt1) == TYPE_CODE (tt2))
1780 /* Array to pointer is a `trivial conversion' according to the
1783 /* We should be doing much hairier argument matching (see
1784 section 13.2 of the ARM), but as a quick kludge, just check
1785 for the same type code. */
1786 if (TYPE_CODE (t1[i].type) != TYPE_CODE (value_type (t2[i])))
1789 if (varargs || t2[i] == NULL)
1794 /* Helper class for do_search_struct_field that updates *RESULT_PTR
1795 and *LAST_BOFFSET, and possibly throws an exception if the field
1796 search has yielded ambiguous results. */
1799 update_search_result (struct value **result_ptr, struct value *v,
1800 LONGEST *last_boffset, LONGEST boffset,
1801 const char *name, struct type *type)
1805 if (*result_ptr != NULL
1806 /* The result is not ambiguous if all the classes that are
1807 found occupy the same space. */
1808 && *last_boffset != boffset)
1809 error (_("base class '%s' is ambiguous in type '%s'"),
1810 name, TYPE_SAFE_NAME (type));
1812 *last_boffset = boffset;
1816 /* A helper for search_struct_field. This does all the work; most
1817 arguments are as passed to search_struct_field. The result is
1818 stored in *RESULT_PTR, which must be initialized to NULL.
1819 OUTERMOST_TYPE is the type of the initial type passed to
1820 search_struct_field; this is used for error reporting when the
1821 lookup is ambiguous. */
1824 do_search_struct_field (const char *name, struct value *arg1, LONGEST offset,
1825 struct type *type, int looking_for_baseclass,
1826 struct value **result_ptr,
1827 LONGEST *last_boffset,
1828 struct type *outermost_type)
1833 type = check_typedef (type);
1834 nbases = TYPE_N_BASECLASSES (type);
1836 if (!looking_for_baseclass)
1837 for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
1839 const char *t_field_name = TYPE_FIELD_NAME (type, i);
1841 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
1845 if (field_is_static (&TYPE_FIELD (type, i)))
1846 v = value_static_field (type, i);
1848 v = value_primitive_field (arg1, offset, i, type);
1854 && t_field_name[0] == '\0')
1856 struct type *field_type = TYPE_FIELD_TYPE (type, i);
1858 if (TYPE_CODE (field_type) == TYPE_CODE_UNION
1859 || TYPE_CODE (field_type) == TYPE_CODE_STRUCT)
1861 /* Look for a match through the fields of an anonymous
1862 union, or anonymous struct. C++ provides anonymous
1865 In the GNU Chill (now deleted from GDB)
1866 implementation of variant record types, each
1867 <alternative field> has an (anonymous) union type,
1868 each member of the union represents a <variant
1869 alternative>. Each <variant alternative> is
1870 represented as a struct, with a member for each
1873 struct value *v = NULL;
1874 LONGEST new_offset = offset;
1876 /* This is pretty gross. In G++, the offset in an
1877 anonymous union is relative to the beginning of the
1878 enclosing struct. In the GNU Chill (now deleted
1879 from GDB) implementation of variant records, the
1880 bitpos is zero in an anonymous union field, so we
1881 have to add the offset of the union here. */
1882 if (TYPE_CODE (field_type) == TYPE_CODE_STRUCT
1883 || (TYPE_NFIELDS (field_type) > 0
1884 && TYPE_FIELD_BITPOS (field_type, 0) == 0))
1885 new_offset += TYPE_FIELD_BITPOS (type, i) / 8;
1887 do_search_struct_field (name, arg1, new_offset,
1889 looking_for_baseclass, &v,
1901 for (i = 0; i < nbases; i++)
1903 struct value *v = NULL;
1904 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
1905 /* If we are looking for baseclasses, this is what we get when
1906 we hit them. But it could happen that the base part's member
1907 name is not yet filled in. */
1908 int found_baseclass = (looking_for_baseclass
1909 && TYPE_BASECLASS_NAME (type, i) != NULL
1910 && (strcmp_iw (name,
1911 TYPE_BASECLASS_NAME (type,
1913 LONGEST boffset = value_embedded_offset (arg1) + offset;
1915 if (BASETYPE_VIA_VIRTUAL (type, i))
1919 boffset = baseclass_offset (type, i,
1920 value_contents_for_printing (arg1),
1921 value_embedded_offset (arg1) + offset,
1922 value_address (arg1),
1925 /* The virtual base class pointer might have been clobbered
1926 by the user program. Make sure that it still points to a
1927 valid memory location. */
1929 boffset += value_embedded_offset (arg1) + offset;
1931 || boffset >= TYPE_LENGTH (value_enclosing_type (arg1)))
1933 CORE_ADDR base_addr;
1935 base_addr = value_address (arg1) + boffset;
1936 v2 = value_at_lazy (basetype, base_addr);
1937 if (target_read_memory (base_addr,
1938 value_contents_raw (v2),
1939 TYPE_LENGTH (value_type (v2))) != 0)
1940 error (_("virtual baseclass botch"));
1944 v2 = value_copy (arg1);
1945 deprecated_set_value_type (v2, basetype);
1946 set_value_embedded_offset (v2, boffset);
1949 if (found_baseclass)
1953 do_search_struct_field (name, v2, 0,
1954 TYPE_BASECLASS (type, i),
1955 looking_for_baseclass,
1956 result_ptr, last_boffset,
1960 else if (found_baseclass)
1961 v = value_primitive_field (arg1, offset, i, type);
1964 do_search_struct_field (name, arg1,
1965 offset + TYPE_BASECLASS_BITPOS (type,
1967 basetype, looking_for_baseclass,
1968 result_ptr, last_boffset,
1972 update_search_result (result_ptr, v, last_boffset,
1973 boffset, name, outermost_type);
1977 /* Helper function used by value_struct_elt to recurse through
1978 baseclasses. Look for a field NAME in ARG1. Search in it assuming
1979 it has (class) type TYPE. If found, return value, else return NULL.
1981 If LOOKING_FOR_BASECLASS, then instead of looking for struct
1982 fields, look for a baseclass named NAME. */
1984 static struct value *
1985 search_struct_field (const char *name, struct value *arg1,
1986 struct type *type, int looking_for_baseclass)
1988 struct value *result = NULL;
1989 LONGEST boffset = 0;
1991 do_search_struct_field (name, arg1, 0, type, looking_for_baseclass,
1992 &result, &boffset, type);
1996 /* Helper function used by value_struct_elt to recurse through
1997 baseclasses. Look for a field NAME in ARG1. Adjust the address of
1998 ARG1 by OFFSET bytes, and search in it assuming it has (class) type
2001 If found, return value, else if name matched and args not return
2002 (value) -1, else return NULL. */
2004 static struct value *
2005 search_struct_method (const char *name, struct value **arg1p,
2006 struct value **args, LONGEST offset,
2007 int *static_memfuncp, struct type *type)
2011 int name_matched = 0;
2012 char dem_opname[64];
2014 type = check_typedef (type);
2015 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
2017 const char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
2019 /* FIXME! May need to check for ARM demangling here. */
2020 if (startswith (t_field_name, "__") ||
2021 startswith (t_field_name, "op") ||
2022 startswith (t_field_name, "type"))
2024 if (cplus_demangle_opname (t_field_name, dem_opname, DMGL_ANSI))
2025 t_field_name = dem_opname;
2026 else if (cplus_demangle_opname (t_field_name, dem_opname, 0))
2027 t_field_name = dem_opname;
2029 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
2031 int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1;
2032 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
2035 check_stub_method_group (type, i);
2036 if (j > 0 && args == 0)
2037 error (_("cannot resolve overloaded method "
2038 "`%s': no arguments supplied"), name);
2039 else if (j == 0 && args == 0)
2041 v = value_fn_field (arg1p, f, j, type, offset);
2048 if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j),
2049 TYPE_VARARGS (TYPE_FN_FIELD_TYPE (f, j)),
2050 TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f, j)),
2051 TYPE_FN_FIELD_ARGS (f, j), args))
2053 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
2054 return value_virtual_fn_field (arg1p, f, j,
2056 if (TYPE_FN_FIELD_STATIC_P (f, j)
2058 *static_memfuncp = 1;
2059 v = value_fn_field (arg1p, f, j, type, offset);
2068 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
2070 LONGEST base_offset;
2071 LONGEST this_offset;
2073 if (BASETYPE_VIA_VIRTUAL (type, i))
2075 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
2076 struct value *base_val;
2077 const gdb_byte *base_valaddr;
2079 /* The virtual base class pointer might have been
2080 clobbered by the user program. Make sure that it
2081 still points to a valid memory location. */
2083 if (offset < 0 || offset >= TYPE_LENGTH (type))
2087 gdb::byte_vector tmp (TYPE_LENGTH (baseclass));
2088 address = value_address (*arg1p);
2090 if (target_read_memory (address + offset,
2091 tmp.data (), TYPE_LENGTH (baseclass)) != 0)
2092 error (_("virtual baseclass botch"));
2094 base_val = value_from_contents_and_address (baseclass,
2097 base_valaddr = value_contents_for_printing (base_val);
2103 base_valaddr = value_contents_for_printing (*arg1p);
2104 this_offset = offset;
2107 base_offset = baseclass_offset (type, i, base_valaddr,
2108 this_offset, value_address (base_val),
2113 base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
2115 v = search_struct_method (name, arg1p, args, base_offset + offset,
2116 static_memfuncp, TYPE_BASECLASS (type, i));
2117 if (v == (struct value *) - 1)
2123 /* FIXME-bothner: Why is this commented out? Why is it here? */
2124 /* *arg1p = arg1_tmp; */
2129 return (struct value *) - 1;
2134 /* Given *ARGP, a value of type (pointer to a)* structure/union,
2135 extract the component named NAME from the ultimate target
2136 structure/union and return it as a value with its appropriate type.
2137 ERR is used in the error message if *ARGP's type is wrong.
2139 C++: ARGS is a list of argument types to aid in the selection of
2140 an appropriate method. Also, handle derived types.
2142 STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
2143 where the truthvalue of whether the function that was resolved was
2144 a static member function or not is stored.
2146 ERR is an error message to be printed in case the field is not
2150 value_struct_elt (struct value **argp, struct value **args,
2151 const char *name, int *static_memfuncp, const char *err)
2156 *argp = coerce_array (*argp);
2158 t = check_typedef (value_type (*argp));
2160 /* Follow pointers until we get to a non-pointer. */
2162 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_IS_REFERENCE (t))
2164 *argp = value_ind (*argp);
2165 /* Don't coerce fn pointer to fn and then back again! */
2166 if (TYPE_CODE (check_typedef (value_type (*argp))) != TYPE_CODE_FUNC)
2167 *argp = coerce_array (*argp);
2168 t = check_typedef (value_type (*argp));
2171 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
2172 && TYPE_CODE (t) != TYPE_CODE_UNION)
2173 error (_("Attempt to extract a component of a value that is not a %s."),
2176 /* Assume it's not, unless we see that it is. */
2177 if (static_memfuncp)
2178 *static_memfuncp = 0;
2182 /* if there are no arguments ...do this... */
2184 /* Try as a field first, because if we succeed, there is less
2186 v = search_struct_field (name, *argp, t, 0);
2190 /* C++: If it was not found as a data field, then try to
2191 return it as a pointer to a method. */
2192 v = search_struct_method (name, argp, args, 0,
2193 static_memfuncp, t);
2195 if (v == (struct value *) - 1)
2196 error (_("Cannot take address of method %s."), name);
2199 if (TYPE_NFN_FIELDS (t))
2200 error (_("There is no member or method named %s."), name);
2202 error (_("There is no member named %s."), name);
2207 v = search_struct_method (name, argp, args, 0,
2208 static_memfuncp, t);
2210 if (v == (struct value *) - 1)
2212 error (_("One of the arguments you tried to pass to %s could not "
2213 "be converted to what the function wants."), name);
2217 /* See if user tried to invoke data as function. If so, hand it
2218 back. If it's not callable (i.e., a pointer to function),
2219 gdb should give an error. */
2220 v = search_struct_field (name, *argp, t, 0);
2221 /* If we found an ordinary field, then it is not a method call.
2222 So, treat it as if it were a static member function. */
2223 if (v && static_memfuncp)
2224 *static_memfuncp = 1;
2228 throw_error (NOT_FOUND_ERROR,
2229 _("Structure has no component named %s."), name);
2233 /* Given *ARGP, a value of type structure or union, or a pointer/reference
2234 to a structure or union, extract and return its component (field) of
2235 type FTYPE at the specified BITPOS.
2236 Throw an exception on error. */
2239 value_struct_elt_bitpos (struct value **argp, int bitpos, struct type *ftype,
2245 *argp = coerce_array (*argp);
2247 t = check_typedef (value_type (*argp));
2249 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_IS_REFERENCE (t))
2251 *argp = value_ind (*argp);
2252 if (TYPE_CODE (check_typedef (value_type (*argp))) != TYPE_CODE_FUNC)
2253 *argp = coerce_array (*argp);
2254 t = check_typedef (value_type (*argp));
2257 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
2258 && TYPE_CODE (t) != TYPE_CODE_UNION)
2259 error (_("Attempt to extract a component of a value that is not a %s."),
2262 for (i = TYPE_N_BASECLASSES (t); i < TYPE_NFIELDS (t); i++)
2264 if (!field_is_static (&TYPE_FIELD (t, i))
2265 && bitpos == TYPE_FIELD_BITPOS (t, i)
2266 && types_equal (ftype, TYPE_FIELD_TYPE (t, i)))
2267 return value_primitive_field (*argp, 0, i, t);
2270 error (_("No field with matching bitpos and type."));
2276 /* Search through the methods of an object (and its bases) to find a
2277 specified method. Return the pointer to the fn_field list FN_LIST of
2278 overloaded instances defined in the source language. If available
2279 and matching, a vector of matching xmethods defined in extension
2280 languages are also returned in XM_WORKER_VEC
2282 Helper function for value_find_oload_list.
2283 ARGP is a pointer to a pointer to a value (the object).
2284 METHOD is a string containing the method name.
2285 OFFSET is the offset within the value.
2286 TYPE is the assumed type of the object.
2287 FN_LIST is the pointer to matching overloaded instances defined in
2288 source language. Since this is a recursive function, *FN_LIST
2289 should be set to NULL when calling this function.
2290 NUM_FNS is the number of overloaded instances. *NUM_FNS should be set to
2291 0 when calling this function.
2292 XM_WORKER_VEC is the vector of matching xmethod workers. *XM_WORKER_VEC
2293 should also be set to NULL when calling this function.
2294 BASETYPE is set to the actual type of the subobject where the
2296 BOFFSET is the offset of the base subobject where the method is found. */
2299 find_method_list (struct value **argp, const char *method,
2300 LONGEST offset, struct type *type,
2301 struct fn_field **fn_list, int *num_fns,
2302 VEC (xmethod_worker_ptr) **xm_worker_vec,
2303 struct type **basetype, LONGEST *boffset)
2306 struct fn_field *f = NULL;
2307 VEC (xmethod_worker_ptr) *worker_vec = NULL, *new_vec = NULL;
2309 gdb_assert (fn_list != NULL && xm_worker_vec != NULL);
2310 type = check_typedef (type);
2312 /* First check in object itself.
2313 This function is called recursively to search through base classes.
2314 If there is a source method match found at some stage, then we need not
2315 look for source methods in consequent recursive calls. */
2316 if ((*fn_list) == NULL)
2318 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
2320 /* pai: FIXME What about operators and type conversions? */
2321 const char *fn_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
2323 if (fn_field_name && (strcmp_iw (fn_field_name, method) == 0))
2325 int len = TYPE_FN_FIELDLIST_LENGTH (type, i);
2326 f = TYPE_FN_FIELDLIST1 (type, i);
2333 /* Resolve any stub methods. */
2334 check_stub_method_group (type, i);
2341 /* Unlike source methods, xmethods can be accumulated over successive
2342 recursive calls. In other words, an xmethod named 'm' in a class
2343 will not hide an xmethod named 'm' in its base class(es). We want
2344 it to be this way because xmethods are after all convenience functions
2345 and hence there is no point restricting them with something like method
2346 hiding. Moreover, if hiding is done for xmethods as well, then we will
2347 have to provide a mechanism to un-hide (like the 'using' construct). */
2348 worker_vec = get_matching_xmethod_workers (type, method);
2349 new_vec = VEC_merge (xmethod_worker_ptr, *xm_worker_vec, worker_vec);
2351 VEC_free (xmethod_worker_ptr, *xm_worker_vec);
2352 VEC_free (xmethod_worker_ptr, worker_vec);
2353 *xm_worker_vec = new_vec;
2355 /* If source methods are not found in current class, look for them in the
2356 base classes. We also have to go through the base classes to gather
2357 extension methods. */
2358 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
2360 LONGEST base_offset;
2362 if (BASETYPE_VIA_VIRTUAL (type, i))
2364 base_offset = baseclass_offset (type, i,
2365 value_contents_for_printing (*argp),
2366 value_offset (*argp) + offset,
2367 value_address (*argp), *argp);
2369 else /* Non-virtual base, simply use bit position from debug
2372 base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
2375 find_method_list (argp, method, base_offset + offset,
2376 TYPE_BASECLASS (type, i), fn_list, num_fns,
2377 xm_worker_vec, basetype, boffset);
2381 /* Return the list of overloaded methods of a specified name. The methods
2382 could be those GDB finds in the binary, or xmethod. Methods found in
2383 the binary are returned in FN_LIST, and xmethods are returned in
2386 ARGP is a pointer to a pointer to a value (the object).
2387 METHOD is the method name.
2388 OFFSET is the offset within the value contents.
2389 FN_LIST is the pointer to matching overloaded instances defined in
2391 NUM_FNS is the number of overloaded instances.
2392 XM_WORKER_VEC is the vector of matching xmethod workers defined in
2393 extension languages.
2394 BASETYPE is set to the type of the base subobject that defines the
2396 BOFFSET is the offset of the base subobject which defines the method. */
2399 value_find_oload_method_list (struct value **argp, const char *method,
2400 LONGEST offset, struct fn_field **fn_list,
2402 VEC (xmethod_worker_ptr) **xm_worker_vec,
2403 struct type **basetype, LONGEST *boffset)
2407 t = check_typedef (value_type (*argp));
2409 /* Code snarfed from value_struct_elt. */
2410 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_IS_REFERENCE (t))
2412 *argp = value_ind (*argp);
2413 /* Don't coerce fn pointer to fn and then back again! */
2414 if (TYPE_CODE (check_typedef (value_type (*argp))) != TYPE_CODE_FUNC)
2415 *argp = coerce_array (*argp);
2416 t = check_typedef (value_type (*argp));
2419 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
2420 && TYPE_CODE (t) != TYPE_CODE_UNION)
2421 error (_("Attempt to extract a component of a "
2422 "value that is not a struct or union"));
2424 gdb_assert (fn_list != NULL && xm_worker_vec != NULL);
2426 /* Clear the lists. */
2429 *xm_worker_vec = NULL;
2431 find_method_list (argp, method, 0, t, fn_list, num_fns, xm_worker_vec,
2435 /* Given an array of arguments (ARGS) (which includes an
2436 entry for "this" in the case of C++ methods), the number of
2437 arguments NARGS, the NAME of a function, and whether it's a method or
2438 not (METHOD), find the best function that matches on the argument types
2439 according to the overload resolution rules.
2441 METHOD can be one of three values:
2442 NON_METHOD for non-member functions.
2443 METHOD: for member functions.
2444 BOTH: used for overload resolution of operators where the
2445 candidates are expected to be either member or non member
2446 functions. In this case the first argument ARGTYPES
2447 (representing 'this') is expected to be a reference to the
2448 target object, and will be dereferenced when attempting the
2451 In the case of class methods, the parameter OBJ is an object value
2452 in which to search for overloaded methods.
2454 In the case of non-method functions, the parameter FSYM is a symbol
2455 corresponding to one of the overloaded functions.
2457 Return value is an integer: 0 -> good match, 10 -> debugger applied
2458 non-standard coercions, 100 -> incompatible.
2460 If a method is being searched for, VALP will hold the value.
2461 If a non-method is being searched for, SYMP will hold the symbol
2464 If a method is being searched for, and it is a static method,
2465 then STATICP will point to a non-zero value.
2467 If NO_ADL argument dependent lookup is disabled. This is used to prevent
2468 ADL overload candidates when performing overload resolution for a fully
2471 If NOSIDE is EVAL_AVOID_SIDE_EFFECTS, then OBJP's memory cannot be
2472 read while picking the best overload match (it may be all zeroes and thus
2473 not have a vtable pointer), in which case skip virtual function lookup.
2474 This is ok as typically EVAL_AVOID_SIDE_EFFECTS is only used to determine
2477 Note: This function does *not* check the value of
2478 overload_resolution. Caller must check it to see whether overload
2479 resolution is permitted. */
2482 find_overload_match (struct value **args, int nargs,
2483 const char *name, enum oload_search_type method,
2484 struct value **objp, struct symbol *fsym,
2485 struct value **valp, struct symbol **symp,
2486 int *staticp, const int no_adl,
2487 const enum noside noside)
2489 struct value *obj = (objp ? *objp : NULL);
2490 struct type *obj_type = obj ? value_type (obj) : NULL;
2491 /* Index of best overloaded function. */
2492 int func_oload_champ = -1;
2493 int method_oload_champ = -1;
2494 int src_method_oload_champ = -1;
2495 int ext_method_oload_champ = -1;
2497 /* The measure for the current best match. */
2498 struct badness_vector *method_badness = NULL;
2499 struct badness_vector *func_badness = NULL;
2500 struct badness_vector *ext_method_badness = NULL;
2501 struct badness_vector *src_method_badness = NULL;
2503 struct value *temp = obj;
2504 /* For methods, the list of overloaded methods. */
2505 struct fn_field *fns_ptr = NULL;
2506 /* For non-methods, the list of overloaded function symbols. */
2507 struct symbol **oload_syms = NULL;
2508 /* For xmethods, the VEC of xmethod workers. */
2509 VEC (xmethod_worker_ptr) *xm_worker_vec = NULL;
2510 /* Number of overloaded instances being considered. */
2512 struct type *basetype = NULL;
2515 struct cleanup *all_cleanups = make_cleanup (null_cleanup, NULL);
2517 const char *obj_type_name = NULL;
2518 const char *func_name = NULL;
2519 enum oload_classification match_quality;
2520 enum oload_classification method_match_quality = INCOMPATIBLE;
2521 enum oload_classification src_method_match_quality = INCOMPATIBLE;
2522 enum oload_classification ext_method_match_quality = INCOMPATIBLE;
2523 enum oload_classification func_match_quality = INCOMPATIBLE;
2525 /* Get the list of overloaded methods or functions. */
2526 if (method == METHOD || method == BOTH)
2530 /* OBJ may be a pointer value rather than the object itself. */
2531 obj = coerce_ref (obj);
2532 while (TYPE_CODE (check_typedef (value_type (obj))) == TYPE_CODE_PTR)
2533 obj = coerce_ref (value_ind (obj));
2534 obj_type_name = TYPE_NAME (value_type (obj));
2536 /* First check whether this is a data member, e.g. a pointer to
2538 if (TYPE_CODE (check_typedef (value_type (obj))) == TYPE_CODE_STRUCT)
2540 *valp = search_struct_field (name, obj,
2541 check_typedef (value_type (obj)), 0);
2545 do_cleanups (all_cleanups);
2550 /* Retrieve the list of methods with the name NAME. */
2551 value_find_oload_method_list (&temp, name, 0, &fns_ptr, &num_fns,
2552 &xm_worker_vec, &basetype, &boffset);
2553 /* If this is a method only search, and no methods were found
2554 the search has faild. */
2555 if (method == METHOD && (!fns_ptr || !num_fns) && !xm_worker_vec)
2556 error (_("Couldn't find method %s%s%s"),
2558 (obj_type_name && *obj_type_name) ? "::" : "",
2560 /* If we are dealing with stub method types, they should have
2561 been resolved by find_method_list via
2562 value_find_oload_method_list above. */
2565 gdb_assert (TYPE_SELF_TYPE (fns_ptr[0].type) != NULL);
2567 src_method_oload_champ = find_oload_champ (args, nargs,
2568 num_fns, fns_ptr, NULL,
2569 NULL, &src_method_badness);
2571 src_method_match_quality = classify_oload_match
2572 (src_method_badness, nargs,
2573 oload_method_static_p (fns_ptr, src_method_oload_champ));
2575 make_cleanup (xfree, src_method_badness);
2578 if (VEC_length (xmethod_worker_ptr, xm_worker_vec) > 0)
2580 ext_method_oload_champ = find_oload_champ (args, nargs,
2581 0, NULL, xm_worker_vec,
2582 NULL, &ext_method_badness);
2583 ext_method_match_quality = classify_oload_match (ext_method_badness,
2585 make_cleanup (xfree, ext_method_badness);
2586 make_cleanup (free_xmethod_worker_vec, xm_worker_vec);
2589 if (src_method_oload_champ >= 0 && ext_method_oload_champ >= 0)
2591 switch (compare_badness (ext_method_badness, src_method_badness))
2593 case 0: /* Src method and xmethod are equally good. */
2594 /* If src method and xmethod are equally good, then
2595 xmethod should be the winner. Hence, fall through to the
2596 case where a xmethod is better than the source
2597 method, except when the xmethod match quality is
2600 case 1: /* Src method and ext method are incompatible. */
2601 /* If ext method match is not standard, then let source method
2602 win. Otherwise, fallthrough to let xmethod win. */
2603 if (ext_method_match_quality != STANDARD)
2605 method_oload_champ = src_method_oload_champ;
2606 method_badness = src_method_badness;
2607 ext_method_oload_champ = -1;
2608 method_match_quality = src_method_match_quality;
2612 case 2: /* Ext method is champion. */
2613 method_oload_champ = ext_method_oload_champ;
2614 method_badness = ext_method_badness;
2615 src_method_oload_champ = -1;
2616 method_match_quality = ext_method_match_quality;
2618 case 3: /* Src method is champion. */
2619 method_oload_champ = src_method_oload_champ;
2620 method_badness = src_method_badness;
2621 ext_method_oload_champ = -1;
2622 method_match_quality = src_method_match_quality;
2625 gdb_assert_not_reached ("Unexpected overload comparison "
2630 else if (src_method_oload_champ >= 0)
2632 method_oload_champ = src_method_oload_champ;
2633 method_badness = src_method_badness;
2634 method_match_quality = src_method_match_quality;
2636 else if (ext_method_oload_champ >= 0)
2638 method_oload_champ = ext_method_oload_champ;
2639 method_badness = ext_method_badness;
2640 method_match_quality = ext_method_match_quality;
2644 if (method == NON_METHOD || method == BOTH)
2646 const char *qualified_name = NULL;
2648 /* If the overload match is being search for both as a method
2649 and non member function, the first argument must now be
2652 args[0] = value_ind (args[0]);
2656 qualified_name = SYMBOL_NATURAL_NAME (fsym);
2658 /* If we have a function with a C++ name, try to extract just
2659 the function part. Do not try this for non-functions (e.g.
2660 function pointers). */
2662 && TYPE_CODE (check_typedef (SYMBOL_TYPE (fsym)))
2667 temp = cp_func_name (qualified_name);
2669 /* If cp_func_name did not remove anything, the name of the
2670 symbol did not include scope or argument types - it was
2671 probably a C-style function. */
2674 make_cleanup (xfree, temp);
2675 if (strcmp (temp, qualified_name) == 0)
2685 qualified_name = name;
2688 /* If there was no C++ name, this must be a C-style function or
2689 not a function at all. Just return the same symbol. Do the
2690 same if cp_func_name fails for some reason. */
2691 if (func_name == NULL)
2694 do_cleanups (all_cleanups);
2698 func_oload_champ = find_oload_champ_namespace (args, nargs,
2705 if (func_oload_champ >= 0)
2706 func_match_quality = classify_oload_match (func_badness, nargs, 0);
2708 make_cleanup (xfree, oload_syms);
2709 make_cleanup (xfree, func_badness);
2712 /* Did we find a match ? */
2713 if (method_oload_champ == -1 && func_oload_champ == -1)
2714 throw_error (NOT_FOUND_ERROR,
2715 _("No symbol \"%s\" in current context."),
2718 /* If we have found both a method match and a function
2719 match, find out which one is better, and calculate match
2721 if (method_oload_champ >= 0 && func_oload_champ >= 0)
2723 switch (compare_badness (func_badness, method_badness))
2725 case 0: /* Top two contenders are equally good. */
2726 /* FIXME: GDB does not support the general ambiguous case.
2727 All candidates should be collected and presented the
2729 error (_("Ambiguous overload resolution"));
2731 case 1: /* Incomparable top contenders. */
2732 /* This is an error incompatible candidates
2733 should not have been proposed. */
2734 error (_("Internal error: incompatible "
2735 "overload candidates proposed"));
2737 case 2: /* Function champion. */
2738 method_oload_champ = -1;
2739 match_quality = func_match_quality;
2741 case 3: /* Method champion. */
2742 func_oload_champ = -1;
2743 match_quality = method_match_quality;
2746 error (_("Internal error: unexpected overload comparison result"));
2752 /* We have either a method match or a function match. */
2753 if (method_oload_champ >= 0)
2754 match_quality = method_match_quality;
2756 match_quality = func_match_quality;
2759 if (match_quality == INCOMPATIBLE)
2761 if (method == METHOD)
2762 error (_("Cannot resolve method %s%s%s to any overloaded instance"),
2764 (obj_type_name && *obj_type_name) ? "::" : "",
2767 error (_("Cannot resolve function %s to any overloaded instance"),
2770 else if (match_quality == NON_STANDARD)
2772 if (method == METHOD)
2773 warning (_("Using non-standard conversion to match "
2774 "method %s%s%s to supplied arguments"),
2776 (obj_type_name && *obj_type_name) ? "::" : "",
2779 warning (_("Using non-standard conversion to match "
2780 "function %s to supplied arguments"),
2784 if (staticp != NULL)
2785 *staticp = oload_method_static_p (fns_ptr, method_oload_champ);
2787 if (method_oload_champ >= 0)
2789 if (src_method_oload_champ >= 0)
2791 if (TYPE_FN_FIELD_VIRTUAL_P (fns_ptr, method_oload_champ)
2792 && noside != EVAL_AVOID_SIDE_EFFECTS)
2794 *valp = value_virtual_fn_field (&temp, fns_ptr,
2795 method_oload_champ, basetype,
2799 *valp = value_fn_field (&temp, fns_ptr, method_oload_champ,
2804 *valp = value_of_xmethod (clone_xmethod_worker
2805 (VEC_index (xmethod_worker_ptr, xm_worker_vec,
2806 ext_method_oload_champ)));
2810 *symp = oload_syms[func_oload_champ];
2814 struct type *temp_type = check_typedef (value_type (temp));
2815 struct type *objtype = check_typedef (obj_type);
2817 if (TYPE_CODE (temp_type) != TYPE_CODE_PTR
2818 && (TYPE_CODE (objtype) == TYPE_CODE_PTR
2819 || TYPE_IS_REFERENCE (objtype)))
2821 temp = value_addr (temp);
2826 do_cleanups (all_cleanups);
2828 switch (match_quality)
2834 default: /* STANDARD */
2839 /* Find the best overload match, searching for FUNC_NAME in namespaces
2840 contained in QUALIFIED_NAME until it either finds a good match or
2841 runs out of namespaces. It stores the overloaded functions in
2842 *OLOAD_SYMS, and the badness vector in *OLOAD_CHAMP_BV. The
2843 calling function is responsible for freeing *OLOAD_SYMS and
2844 *OLOAD_CHAMP_BV. If NO_ADL, argument dependent lookup is not
2848 find_oload_champ_namespace (struct value **args, int nargs,
2849 const char *func_name,
2850 const char *qualified_name,
2851 struct symbol ***oload_syms,
2852 struct badness_vector **oload_champ_bv,
2857 find_oload_champ_namespace_loop (args, nargs,
2860 oload_syms, oload_champ_bv,
2867 /* Helper function for find_oload_champ_namespace; NAMESPACE_LEN is
2868 how deep we've looked for namespaces, and the champ is stored in
2869 OLOAD_CHAMP. The return value is 1 if the champ is a good one, 0
2870 if it isn't. Other arguments are the same as in
2871 find_oload_champ_namespace
2873 It is the caller's responsibility to free *OLOAD_SYMS and
2877 find_oload_champ_namespace_loop (struct value **args, int nargs,
2878 const char *func_name,
2879 const char *qualified_name,
2881 struct symbol ***oload_syms,
2882 struct badness_vector **oload_champ_bv,
2886 int next_namespace_len = namespace_len;
2887 int searched_deeper = 0;
2889 struct cleanup *old_cleanups;
2890 int new_oload_champ;
2891 struct symbol **new_oload_syms;
2892 struct badness_vector *new_oload_champ_bv;
2893 char *new_namespace;
2895 if (next_namespace_len != 0)
2897 gdb_assert (qualified_name[next_namespace_len] == ':');
2898 next_namespace_len += 2;
2900 next_namespace_len +=
2901 cp_find_first_component (qualified_name + next_namespace_len);
2903 /* Initialize these to values that can safely be xfree'd. */
2905 *oload_champ_bv = NULL;
2907 /* First, see if we have a deeper namespace we can search in.
2908 If we get a good match there, use it. */
2910 if (qualified_name[next_namespace_len] == ':')
2912 searched_deeper = 1;
2914 if (find_oload_champ_namespace_loop (args, nargs,
2915 func_name, qualified_name,
2917 oload_syms, oload_champ_bv,
2918 oload_champ, no_adl))
2924 /* If we reach here, either we're in the deepest namespace or we
2925 didn't find a good match in a deeper namespace. But, in the
2926 latter case, we still have a bad match in a deeper namespace;
2927 note that we might not find any match at all in the current
2928 namespace. (There's always a match in the deepest namespace,
2929 because this overload mechanism only gets called if there's a
2930 function symbol to start off with.) */
2932 old_cleanups = make_cleanup (xfree, *oload_syms);
2933 make_cleanup (xfree, *oload_champ_bv);
2934 new_namespace = (char *) alloca (namespace_len + 1);
2935 strncpy (new_namespace, qualified_name, namespace_len);
2936 new_namespace[namespace_len] = '\0';
2937 new_oload_syms = make_symbol_overload_list (func_name,
2940 /* If we have reached the deepest level perform argument
2941 determined lookup. */
2942 if (!searched_deeper && !no_adl)
2945 struct type **arg_types;
2947 /* Prepare list of argument types for overload resolution. */
2948 arg_types = (struct type **)
2949 alloca (nargs * (sizeof (struct type *)));
2950 for (ix = 0; ix < nargs; ix++)
2951 arg_types[ix] = value_type (args[ix]);
2952 make_symbol_overload_list_adl (arg_types, nargs, func_name);
2955 while (new_oload_syms[num_fns])
2958 new_oload_champ = find_oload_champ (args, nargs, num_fns,
2959 NULL, NULL, new_oload_syms,
2960 &new_oload_champ_bv);
2962 /* Case 1: We found a good match. Free earlier matches (if any),
2963 and return it. Case 2: We didn't find a good match, but we're
2964 not the deepest function. Then go with the bad match that the
2965 deeper function found. Case 3: We found a bad match, and we're
2966 the deepest function. Then return what we found, even though
2967 it's a bad match. */
2969 if (new_oload_champ != -1
2970 && classify_oload_match (new_oload_champ_bv, nargs, 0) == STANDARD)
2972 *oload_syms = new_oload_syms;
2973 *oload_champ = new_oload_champ;
2974 *oload_champ_bv = new_oload_champ_bv;
2975 do_cleanups (old_cleanups);
2978 else if (searched_deeper)
2980 xfree (new_oload_syms);
2981 xfree (new_oload_champ_bv);
2982 discard_cleanups (old_cleanups);
2987 *oload_syms = new_oload_syms;
2988 *oload_champ = new_oload_champ;
2989 *oload_champ_bv = new_oload_champ_bv;
2990 do_cleanups (old_cleanups);
2995 /* Look for a function to take NARGS args of ARGS. Find
2996 the best match from among the overloaded methods or functions
2997 given by FNS_PTR or OLOAD_SYMS or XM_WORKER_VEC, respectively.
2998 One, and only one of FNS_PTR, OLOAD_SYMS and XM_WORKER_VEC can be
3001 If XM_WORKER_VEC is NULL, then the length of the arrays FNS_PTR
3002 or OLOAD_SYMS (whichever is non-NULL) is specified in NUM_FNS.
3004 Return the index of the best match; store an indication of the
3005 quality of the match in OLOAD_CHAMP_BV.
3007 It is the caller's responsibility to free *OLOAD_CHAMP_BV. */
3010 find_oload_champ (struct value **args, int nargs,
3011 int num_fns, struct fn_field *fns_ptr,
3012 VEC (xmethod_worker_ptr) *xm_worker_vec,
3013 struct symbol **oload_syms,
3014 struct badness_vector **oload_champ_bv)
3018 /* A measure of how good an overloaded instance is. */
3019 struct badness_vector *bv;
3020 /* Index of best overloaded function. */
3021 int oload_champ = -1;
3022 /* Current ambiguity state for overload resolution. */
3023 int oload_ambiguous = 0;
3024 /* 0 => no ambiguity, 1 => two good funcs, 2 => incomparable funcs. */
3026 /* A champion can be found among methods alone, or among functions
3027 alone, or in xmethods alone, but not in more than one of these
3029 gdb_assert ((fns_ptr != NULL) + (oload_syms != NULL) + (xm_worker_vec != NULL)
3032 *oload_champ_bv = NULL;
3034 fn_count = (xm_worker_vec != NULL
3035 ? VEC_length (xmethod_worker_ptr, xm_worker_vec)
3037 /* Consider each candidate in turn. */
3038 for (ix = 0; ix < fn_count; ix++)
3041 int static_offset = 0;
3043 struct type **parm_types;
3044 struct xmethod_worker *worker = NULL;
3046 if (xm_worker_vec != NULL)
3048 worker = VEC_index (xmethod_worker_ptr, xm_worker_vec, ix);
3049 parm_types = get_xmethod_arg_types (worker, &nparms);
3053 if (fns_ptr != NULL)
3055 nparms = TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (fns_ptr, ix));
3056 static_offset = oload_method_static_p (fns_ptr, ix);
3059 nparms = TYPE_NFIELDS (SYMBOL_TYPE (oload_syms[ix]));
3061 parm_types = XNEWVEC (struct type *, nparms);
3062 for (jj = 0; jj < nparms; jj++)
3063 parm_types[jj] = (fns_ptr != NULL
3064 ? (TYPE_FN_FIELD_ARGS (fns_ptr, ix)[jj].type)
3065 : TYPE_FIELD_TYPE (SYMBOL_TYPE (oload_syms[ix]),
3069 /* Compare parameter types to supplied argument types. Skip
3070 THIS for static methods. */
3071 bv = rank_function (parm_types, nparms,
3072 args + static_offset,
3073 nargs - static_offset);
3075 if (!*oload_champ_bv)
3077 *oload_champ_bv = bv;
3080 else /* See whether current candidate is better or worse than
3082 switch (compare_badness (bv, *oload_champ_bv))
3084 case 0: /* Top two contenders are equally good. */
3085 oload_ambiguous = 1;
3087 case 1: /* Incomparable top contenders. */
3088 oload_ambiguous = 2;
3090 case 2: /* New champion, record details. */
3091 *oload_champ_bv = bv;
3092 oload_ambiguous = 0;
3102 if (fns_ptr != NULL)
3103 fprintf_filtered (gdb_stderr,
3104 "Overloaded method instance %s, # of parms %d\n",
3105 fns_ptr[ix].physname, nparms);
3106 else if (xm_worker_vec != NULL)
3107 fprintf_filtered (gdb_stderr,
3108 "Xmethod worker, # of parms %d\n",
3111 fprintf_filtered (gdb_stderr,
3112 "Overloaded function instance "
3113 "%s # of parms %d\n",
3114 SYMBOL_DEMANGLED_NAME (oload_syms[ix]),
3116 for (jj = 0; jj < nargs - static_offset; jj++)
3117 fprintf_filtered (gdb_stderr,
3118 "...Badness @ %d : %d\n",
3119 jj, bv->rank[jj].rank);
3120 fprintf_filtered (gdb_stderr, "Overload resolution "
3121 "champion is %d, ambiguous? %d\n",
3122 oload_champ, oload_ambiguous);
3129 /* Return 1 if we're looking at a static method, 0 if we're looking at
3130 a non-static method or a function that isn't a method. */
3133 oload_method_static_p (struct fn_field *fns_ptr, int index)
3135 if (fns_ptr && index >= 0 && TYPE_FN_FIELD_STATIC_P (fns_ptr, index))
3141 /* Check how good an overload match OLOAD_CHAMP_BV represents. */
3143 static enum oload_classification
3144 classify_oload_match (struct badness_vector *oload_champ_bv,
3149 enum oload_classification worst = STANDARD;
3151 for (ix = 1; ix <= nargs - static_offset; ix++)
3153 /* If this conversion is as bad as INCOMPATIBLE_TYPE_BADNESS
3154 or worse return INCOMPATIBLE. */
3155 if (compare_ranks (oload_champ_bv->rank[ix],
3156 INCOMPATIBLE_TYPE_BADNESS) <= 0)
3157 return INCOMPATIBLE; /* Truly mismatched types. */
3158 /* Otherwise If this conversion is as bad as
3159 NS_POINTER_CONVERSION_BADNESS or worse return NON_STANDARD. */
3160 else if (compare_ranks (oload_champ_bv->rank[ix],
3161 NS_POINTER_CONVERSION_BADNESS) <= 0)
3162 worst = NON_STANDARD; /* Non-standard type conversions
3166 /* If no INCOMPATIBLE classification was found, return the worst one
3167 that was found (if any). */
3171 /* C++: return 1 is NAME is a legitimate name for the destructor of
3172 type TYPE. If TYPE does not have a destructor, or if NAME is
3173 inappropriate for TYPE, an error is signaled. Parameter TYPE should not yet
3174 have CHECK_TYPEDEF applied, this function will apply it itself. */
3177 destructor_name_p (const char *name, struct type *type)
3181 const char *dname = type_name_no_tag_or_error (type);
3182 const char *cp = strchr (dname, '<');
3185 /* Do not compare the template part for template classes. */
3187 len = strlen (dname);
3190 if (strlen (name + 1) != len || strncmp (dname, name + 1, len) != 0)
3191 error (_("name of destructor must equal name of class"));
3198 /* Find an enum constant named NAME in TYPE. TYPE must be an "enum
3199 class". If the name is found, return a value representing it;
3200 otherwise throw an exception. */
3202 static struct value *
3203 enum_constant_from_type (struct type *type, const char *name)
3206 int name_len = strlen (name);
3208 gdb_assert (TYPE_CODE (type) == TYPE_CODE_ENUM
3209 && TYPE_DECLARED_CLASS (type));
3211 for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); ++i)
3213 const char *fname = TYPE_FIELD_NAME (type, i);
3216 if (TYPE_FIELD_LOC_KIND (type, i) != FIELD_LOC_KIND_ENUMVAL
3220 /* Look for the trailing "::NAME", since enum class constant
3221 names are qualified here. */
3222 len = strlen (fname);
3223 if (len + 2 >= name_len
3224 && fname[len - name_len - 2] == ':'
3225 && fname[len - name_len - 1] == ':'
3226 && strcmp (&fname[len - name_len], name) == 0)
3227 return value_from_longest (type, TYPE_FIELD_ENUMVAL (type, i));
3230 error (_("no constant named \"%s\" in enum \"%s\""),
3231 name, TYPE_TAG_NAME (type));
3234 /* C++: Given an aggregate type CURTYPE, and a member name NAME,
3235 return the appropriate member (or the address of the member, if
3236 WANT_ADDRESS). This function is used to resolve user expressions
3237 of the form "DOMAIN::NAME". For more details on what happens, see
3238 the comment before value_struct_elt_for_reference. */
3241 value_aggregate_elt (struct type *curtype, const char *name,
3242 struct type *expect_type, int want_address,
3245 switch (TYPE_CODE (curtype))
3247 case TYPE_CODE_STRUCT:
3248 case TYPE_CODE_UNION:
3249 return value_struct_elt_for_reference (curtype, 0, curtype,
3251 want_address, noside);
3252 case TYPE_CODE_NAMESPACE:
3253 return value_namespace_elt (curtype, name,
3254 want_address, noside);
3256 case TYPE_CODE_ENUM:
3257 return enum_constant_from_type (curtype, name);
3260 internal_error (__FILE__, __LINE__,
3261 _("non-aggregate type in value_aggregate_elt"));
3265 /* Compares the two method/function types T1 and T2 for "equality"
3266 with respect to the methods' parameters. If the types of the
3267 two parameter lists are the same, returns 1; 0 otherwise. This
3268 comparison may ignore any artificial parameters in T1 if
3269 SKIP_ARTIFICIAL is non-zero. This function will ALWAYS skip
3270 the first artificial parameter in T1, assumed to be a 'this' pointer.
3272 The type T2 is expected to have come from make_params (in eval.c). */
3275 compare_parameters (struct type *t1, struct type *t2, int skip_artificial)
3279 if (TYPE_NFIELDS (t1) > 0 && TYPE_FIELD_ARTIFICIAL (t1, 0))
3282 /* If skipping artificial fields, find the first real field
3284 if (skip_artificial)
3286 while (start < TYPE_NFIELDS (t1)
3287 && TYPE_FIELD_ARTIFICIAL (t1, start))
3291 /* Now compare parameters. */
3293 /* Special case: a method taking void. T1 will contain no
3294 non-artificial fields, and T2 will contain TYPE_CODE_VOID. */
3295 if ((TYPE_NFIELDS (t1) - start) == 0 && TYPE_NFIELDS (t2) == 1
3296 && TYPE_CODE (TYPE_FIELD_TYPE (t2, 0)) == TYPE_CODE_VOID)
3299 if ((TYPE_NFIELDS (t1) - start) == TYPE_NFIELDS (t2))
3303 for (i = 0; i < TYPE_NFIELDS (t2); ++i)
3305 if (compare_ranks (rank_one_type (TYPE_FIELD_TYPE (t1, start + i),
3306 TYPE_FIELD_TYPE (t2, i), NULL),
3307 EXACT_MATCH_BADNESS) != 0)
3317 /* C++: Given an aggregate type CURTYPE, and a member name NAME,
3318 return the address of this member as a "pointer to member" type.
3319 If INTYPE is non-null, then it will be the type of the member we
3320 are looking for. This will help us resolve "pointers to member
3321 functions". This function is used to resolve user expressions of
3322 the form "DOMAIN::NAME". */
3324 static struct value *
3325 value_struct_elt_for_reference (struct type *domain, int offset,
3326 struct type *curtype, const char *name,
3327 struct type *intype,
3331 struct type *t = curtype;
3333 struct value *v, *result;
3335 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
3336 && TYPE_CODE (t) != TYPE_CODE_UNION)
3337 error (_("Internal error: non-aggregate type "
3338 "to value_struct_elt_for_reference"));
3340 for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
3342 const char *t_field_name = TYPE_FIELD_NAME (t, i);
3344 if (t_field_name && strcmp (t_field_name, name) == 0)
3346 if (field_is_static (&TYPE_FIELD (t, i)))
3348 v = value_static_field (t, i);
3353 if (TYPE_FIELD_PACKED (t, i))
3354 error (_("pointers to bitfield members not allowed"));
3357 return value_from_longest
3358 (lookup_memberptr_type (TYPE_FIELD_TYPE (t, i), domain),
3359 offset + (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3));
3360 else if (noside != EVAL_NORMAL)
3361 return allocate_value (TYPE_FIELD_TYPE (t, i));
3364 /* Try to evaluate NAME as a qualified name with implicit
3365 this pointer. In this case, attempt to return the
3366 equivalent to `this->*(&TYPE::NAME)'. */
3367 v = value_of_this_silent (current_language);
3372 struct type *type, *tmp;
3374 ptr = value_aggregate_elt (domain, name, NULL, 1, noside);
3375 type = check_typedef (value_type (ptr));
3376 gdb_assert (type != NULL
3377 && TYPE_CODE (type) == TYPE_CODE_MEMBERPTR);
3378 tmp = lookup_pointer_type (TYPE_SELF_TYPE (type));
3379 v = value_cast_pointers (tmp, v, 1);
3380 mem_offset = value_as_long (ptr);
3381 tmp = lookup_pointer_type (TYPE_TARGET_TYPE (type));
3382 result = value_from_pointer (tmp,
3383 value_as_long (v) + mem_offset);
3384 return value_ind (result);
3387 error (_("Cannot reference non-static field \"%s\""), name);
3392 /* C++: If it was not found as a data field, then try to return it
3393 as a pointer to a method. */
3395 /* Perform all necessary dereferencing. */
3396 while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR)
3397 intype = TYPE_TARGET_TYPE (intype);
3399 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
3401 const char *t_field_name = TYPE_FN_FIELDLIST_NAME (t, i);
3402 char dem_opname[64];
3404 if (startswith (t_field_name, "__")
3405 || startswith (t_field_name, "op")
3406 || startswith (t_field_name, "type"))
3408 if (cplus_demangle_opname (t_field_name,
3409 dem_opname, DMGL_ANSI))
3410 t_field_name = dem_opname;
3411 else if (cplus_demangle_opname (t_field_name,
3413 t_field_name = dem_opname;
3415 if (t_field_name && strcmp (t_field_name, name) == 0)
3418 int len = TYPE_FN_FIELDLIST_LENGTH (t, i);
3419 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
3421 check_stub_method_group (t, i);
3425 for (j = 0; j < len; ++j)
3427 if (TYPE_CONST (intype) != TYPE_FN_FIELD_CONST (f, j))
3429 if (TYPE_VOLATILE (intype) != TYPE_FN_FIELD_VOLATILE (f, j))
3432 if (compare_parameters (TYPE_FN_FIELD_TYPE (f, j), intype, 0)
3433 || compare_parameters (TYPE_FN_FIELD_TYPE (f, j),
3439 error (_("no member function matches "
3440 "that type instantiation"));
3447 for (ii = 0; ii < len; ++ii)
3449 /* Skip artificial methods. This is necessary if,
3450 for example, the user wants to "print
3451 subclass::subclass" with only one user-defined
3452 constructor. There is no ambiguity in this case.
3453 We are careful here to allow artificial methods
3454 if they are the unique result. */
3455 if (TYPE_FN_FIELD_ARTIFICIAL (f, ii))
3462 /* Desired method is ambiguous if more than one
3463 method is defined. */
3464 if (j != -1 && !TYPE_FN_FIELD_ARTIFICIAL (f, j))
3465 error (_("non-unique member `%s' requires "
3466 "type instantiation"), name);
3472 error (_("no matching member function"));
3475 if (TYPE_FN_FIELD_STATIC_P (f, j))
3478 lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
3479 0, VAR_DOMAIN, 0).symbol;
3485 return value_addr (read_var_value (s, 0, 0));
3487 return read_var_value (s, 0, 0);
3490 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
3494 result = allocate_value
3495 (lookup_methodptr_type (TYPE_FN_FIELD_TYPE (f, j)));
3496 cplus_make_method_ptr (value_type (result),
3497 value_contents_writeable (result),
3498 TYPE_FN_FIELD_VOFFSET (f, j), 1);
3500 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
3501 return allocate_value (TYPE_FN_FIELD_TYPE (f, j));
3503 error (_("Cannot reference virtual member function \"%s\""),
3509 lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
3510 0, VAR_DOMAIN, 0).symbol;
3515 v = read_var_value (s, 0, 0);
3520 result = allocate_value (lookup_methodptr_type (TYPE_FN_FIELD_TYPE (f, j)));
3521 cplus_make_method_ptr (value_type (result),
3522 value_contents_writeable (result),
3523 value_address (v), 0);
3529 for (i = TYPE_N_BASECLASSES (t) - 1; i >= 0; i--)
3534 if (BASETYPE_VIA_VIRTUAL (t, i))
3537 base_offset = TYPE_BASECLASS_BITPOS (t, i) / 8;
3538 v = value_struct_elt_for_reference (domain,
3539 offset + base_offset,
3540 TYPE_BASECLASS (t, i),
3542 want_address, noside);
3547 /* As a last chance, pretend that CURTYPE is a namespace, and look
3548 it up that way; this (frequently) works for types nested inside
3551 return value_maybe_namespace_elt (curtype, name,
3552 want_address, noside);
3555 /* C++: Return the member NAME of the namespace given by the type
3558 static struct value *
3559 value_namespace_elt (const struct type *curtype,
3560 const char *name, int want_address,
3563 struct value *retval = value_maybe_namespace_elt (curtype, name,
3568 error (_("No symbol \"%s\" in namespace \"%s\"."),
3569 name, TYPE_TAG_NAME (curtype));
3574 /* A helper function used by value_namespace_elt and
3575 value_struct_elt_for_reference. It looks up NAME inside the
3576 context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
3577 is a class and NAME refers to a type in CURTYPE itself (as opposed
3578 to, say, some base class of CURTYPE). */
3580 static struct value *
3581 value_maybe_namespace_elt (const struct type *curtype,
3582 const char *name, int want_address,
3585 const char *namespace_name = TYPE_TAG_NAME (curtype);
3586 struct block_symbol sym;
3587 struct value *result;
3589 sym = cp_lookup_symbol_namespace (namespace_name, name,
3590 get_selected_block (0), VAR_DOMAIN);
3592 if (sym.symbol == NULL)
3594 else if ((noside == EVAL_AVOID_SIDE_EFFECTS)
3595 && (SYMBOL_CLASS (sym.symbol) == LOC_TYPEDEF))
3596 result = allocate_value (SYMBOL_TYPE (sym.symbol));
3598 result = value_of_variable (sym.symbol, sym.block);
3601 result = value_addr (result);
3606 /* Given a pointer or a reference value V, find its real (RTTI) type.
3608 Other parameters FULL, TOP, USING_ENC as with value_rtti_type()
3609 and refer to the values computed for the object pointed to. */
3612 value_rtti_indirect_type (struct value *v, int *full,
3613 LONGEST *top, int *using_enc)
3615 struct value *target = NULL;
3616 struct type *type, *real_type, *target_type;
3618 type = value_type (v);
3619 type = check_typedef (type);
3620 if (TYPE_IS_REFERENCE (type))
3621 target = coerce_ref (v);
3622 else if (TYPE_CODE (type) == TYPE_CODE_PTR)
3627 target = value_ind (v);
3629 CATCH (except, RETURN_MASK_ERROR)
3631 if (except.error == MEMORY_ERROR)
3633 /* value_ind threw a memory error. The pointer is NULL or
3634 contains an uninitialized value: we can't determine any
3638 throw_exception (except);
3645 real_type = value_rtti_type (target, full, top, using_enc);
3649 /* Copy qualifiers to the referenced object. */
3650 target_type = value_type (target);
3651 real_type = make_cv_type (TYPE_CONST (target_type),
3652 TYPE_VOLATILE (target_type), real_type, NULL);
3653 if (TYPE_IS_REFERENCE (type))
3654 real_type = lookup_reference_type (real_type, TYPE_CODE (type));
3655 else if (TYPE_CODE (type) == TYPE_CODE_PTR)
3656 real_type = lookup_pointer_type (real_type);
3658 internal_error (__FILE__, __LINE__, _("Unexpected value type."));
3660 /* Copy qualifiers to the pointer/reference. */
3661 real_type = make_cv_type (TYPE_CONST (type), TYPE_VOLATILE (type),
3668 /* Given a value pointed to by ARGP, check its real run-time type, and
3669 if that is different from the enclosing type, create a new value
3670 using the real run-time type as the enclosing type (and of the same
3671 type as ARGP) and return it, with the embedded offset adjusted to
3672 be the correct offset to the enclosed object. RTYPE is the type,
3673 and XFULL, XTOP, and XUSING_ENC are the other parameters, computed
3674 by value_rtti_type(). If these are available, they can be supplied
3675 and a second call to value_rtti_type() is avoided. (Pass RTYPE ==
3676 NULL if they're not available. */
3679 value_full_object (struct value *argp,
3681 int xfull, int xtop,
3684 struct type *real_type;
3688 struct value *new_val;
3695 using_enc = xusing_enc;
3698 real_type = value_rtti_type (argp, &full, &top, &using_enc);
3700 /* If no RTTI data, or if object is already complete, do nothing. */
3701 if (!real_type || real_type == value_enclosing_type (argp))
3704 /* In a destructor we might see a real type that is a superclass of
3705 the object's type. In this case it is better to leave the object
3708 && TYPE_LENGTH (real_type) < TYPE_LENGTH (value_enclosing_type (argp)))
3711 /* If we have the full object, but for some reason the enclosing
3712 type is wrong, set it. */
3713 /* pai: FIXME -- sounds iffy */
3716 argp = value_copy (argp);
3717 set_value_enclosing_type (argp, real_type);
3721 /* Check if object is in memory. */
3722 if (VALUE_LVAL (argp) != lval_memory)
3724 warning (_("Couldn't retrieve complete object of RTTI "
3725 "type %s; object may be in register(s)."),
3726 TYPE_NAME (real_type));
3731 /* All other cases -- retrieve the complete object. */
3732 /* Go back by the computed top_offset from the beginning of the
3733 object, adjusting for the embedded offset of argp if that's what
3734 value_rtti_type used for its computation. */
3735 new_val = value_at_lazy (real_type, value_address (argp) - top +
3736 (using_enc ? 0 : value_embedded_offset (argp)));
3737 deprecated_set_value_type (new_val, value_type (argp));
3738 set_value_embedded_offset (new_val, (using_enc
3739 ? top + value_embedded_offset (argp)
3745 /* Return the value of the local variable, if one exists. Throw error
3746 otherwise, such as if the request is made in an inappropriate context. */
3749 value_of_this (const struct language_defn *lang)
3751 struct block_symbol sym;
3752 const struct block *b;
3753 struct frame_info *frame;
3755 if (!lang->la_name_of_this)
3756 error (_("no `this' in current language"));
3758 frame = get_selected_frame (_("no frame selected"));
3760 b = get_frame_block (frame, NULL);
3762 sym = lookup_language_this (lang, b);
3763 if (sym.symbol == NULL)
3764 error (_("current stack frame does not contain a variable named `%s'"),
3765 lang->la_name_of_this);
3767 return read_var_value (sym.symbol, sym.block, frame);
3770 /* Return the value of the local variable, if one exists. Return NULL
3771 otherwise. Never throw error. */
3774 value_of_this_silent (const struct language_defn *lang)
3776 struct value *ret = NULL;
3780 ret = value_of_this (lang);
3782 CATCH (except, RETURN_MASK_ERROR)
3790 /* Create a slice (sub-string, sub-array) of ARRAY, that is LENGTH
3791 elements long, starting at LOWBOUND. The result has the same lower
3792 bound as the original ARRAY. */
3795 value_slice (struct value *array, int lowbound, int length)
3797 struct type *slice_range_type, *slice_type, *range_type;
3798 LONGEST lowerbound, upperbound;
3799 struct value *slice;
3800 struct type *array_type;
3802 array_type = check_typedef (value_type (array));
3803 if (TYPE_CODE (array_type) != TYPE_CODE_ARRAY
3804 && TYPE_CODE (array_type) != TYPE_CODE_STRING)
3805 error (_("cannot take slice of non-array"));
3807 range_type = TYPE_INDEX_TYPE (array_type);
3808 if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0)
3809 error (_("slice from bad array or bitstring"));
3811 if (lowbound < lowerbound || length < 0
3812 || lowbound + length - 1 > upperbound)
3813 error (_("slice out of range"));
3815 /* FIXME-type-allocation: need a way to free this type when we are
3817 slice_range_type = create_static_range_type ((struct type *) NULL,
3818 TYPE_TARGET_TYPE (range_type),
3820 lowbound + length - 1);
3823 struct type *element_type = TYPE_TARGET_TYPE (array_type);
3825 = (lowbound - lowerbound) * TYPE_LENGTH (check_typedef (element_type));
3827 slice_type = create_array_type ((struct type *) NULL,
3830 TYPE_CODE (slice_type) = TYPE_CODE (array_type);
3832 if (VALUE_LVAL (array) == lval_memory && value_lazy (array))
3833 slice = allocate_value_lazy (slice_type);
3836 slice = allocate_value (slice_type);
3837 value_contents_copy (slice, 0, array, offset,
3838 type_length_units (slice_type));
3841 set_value_component_location (slice, array);
3842 set_value_offset (slice, value_offset (array) + offset);
3848 /* Create a value for a FORTRAN complex number. Currently most of the
3849 time values are coerced to COMPLEX*16 (i.e. a complex number
3850 composed of 2 doubles. This really should be a smarter routine
3851 that figures out precision inteligently as opposed to assuming
3852 doubles. FIXME: fmb */
3855 value_literal_complex (struct value *arg1,
3860 struct type *real_type = TYPE_TARGET_TYPE (type);
3862 val = allocate_value (type);
3863 arg1 = value_cast (real_type, arg1);
3864 arg2 = value_cast (real_type, arg2);
3866 memcpy (value_contents_raw (val),
3867 value_contents (arg1), TYPE_LENGTH (real_type));
3868 memcpy (value_contents_raw (val) + TYPE_LENGTH (real_type),
3869 value_contents (arg2), TYPE_LENGTH (real_type));
3873 /* Cast a value into the appropriate complex data type. */
3875 static struct value *
3876 cast_into_complex (struct type *type, struct value *val)
3878 struct type *real_type = TYPE_TARGET_TYPE (type);
3880 if (TYPE_CODE (value_type (val)) == TYPE_CODE_COMPLEX)
3882 struct type *val_real_type = TYPE_TARGET_TYPE (value_type (val));
3883 struct value *re_val = allocate_value (val_real_type);
3884 struct value *im_val = allocate_value (val_real_type);
3886 memcpy (value_contents_raw (re_val),
3887 value_contents (val), TYPE_LENGTH (val_real_type));
3888 memcpy (value_contents_raw (im_val),
3889 value_contents (val) + TYPE_LENGTH (val_real_type),
3890 TYPE_LENGTH (val_real_type));
3892 return value_literal_complex (re_val, im_val, type);
3894 else if (TYPE_CODE (value_type (val)) == TYPE_CODE_FLT
3895 || TYPE_CODE (value_type (val)) == TYPE_CODE_INT)
3896 return value_literal_complex (val,
3897 value_zero (real_type, not_lval),
3900 error (_("cannot cast non-number to complex"));
3904 _initialize_valops (void)
3906 add_setshow_boolean_cmd ("overload-resolution", class_support,
3907 &overload_resolution, _("\
3908 Set overload resolution in evaluating C++ functions."), _("\
3909 Show overload resolution in evaluating C++ functions."),
3911 show_overload_resolution,
3912 &setlist, &showlist);
3913 overload_resolution = 1;