1 /* Perform non-arithmetic operations on values, for GDB.
3 Copyright (C) 1986-2018 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
35 #include "dictionary.h"
36 #include "cp-support.h"
37 #include "target-float.h"
38 #include "tracepoint.h"
39 #include "observable.h"
41 #include "extension.h"
42 #include "byte-vector.h"
44 extern unsigned int overload_debug
;
45 /* Local functions. */
47 static int typecmp (int staticp
, int varargs
, int nargs
,
48 struct field t1
[], struct value
*t2
[]);
50 static struct value
*search_struct_field (const char *, struct value
*,
53 static struct value
*search_struct_method (const char *, struct value
**,
55 LONGEST
, int *, struct type
*);
57 static int find_oload_champ_namespace (gdb::array_view
<value
*> args
,
58 const char *, const char *,
59 std::vector
<symbol
*> *oload_syms
,
60 struct badness_vector
**,
63 static int find_oload_champ_namespace_loop (gdb::array_view
<value
*> args
,
64 const char *, const char *,
65 int, std::vector
<symbol
*> *oload_syms
,
66 struct badness_vector
**, int *,
69 static int find_oload_champ (gdb::array_view
<value
*> args
, int,
71 const std::vector
<xmethod_worker_up
> *,
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 std::vector
<xmethod_worker_up
> *,
102 struct type
**, LONGEST
*);
104 int overload_resolution
= 0;
106 show_overload_resolution (struct ui_file
*file
, int from_tty
,
107 struct cmd_list_element
*c
,
110 fprintf_filtered (file
, _("Overload resolution in evaluating "
111 "C++ functions is %s.\n"),
115 /* Find the address of function name NAME in the inferior. If OBJF_P
116 is non-NULL, *OBJF_P will be set to the OBJFILE where the function
120 find_function_in_inferior (const char *name
, struct objfile
**objf_p
)
122 struct block_symbol sym
;
124 sym
= lookup_symbol (name
, 0, VAR_DOMAIN
, 0);
125 if (sym
.symbol
!= NULL
)
127 if (SYMBOL_CLASS (sym
.symbol
) != LOC_BLOCK
)
129 error (_("\"%s\" exists in this program but is not a function."),
134 *objf_p
= symbol_objfile (sym
.symbol
);
136 return value_of_variable (sym
.symbol
, sym
.block
);
140 struct bound_minimal_symbol msymbol
=
141 lookup_bound_minimal_symbol (name
);
143 if (msymbol
.minsym
!= NULL
)
145 struct objfile
*objfile
= msymbol
.objfile
;
146 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
150 type
= lookup_pointer_type (builtin_type (gdbarch
)->builtin_char
);
151 type
= lookup_function_type (type
);
152 type
= lookup_pointer_type (type
);
153 maddr
= BMSYMBOL_VALUE_ADDRESS (msymbol
);
158 return value_from_pointer (type
, maddr
);
162 if (!target_has_execution
)
163 error (_("evaluation of this expression "
164 "requires the target program to be active"));
166 error (_("evaluation of this expression requires the "
167 "program to have a function \"%s\"."),
173 /* Allocate NBYTES of space in the inferior using the inferior's
174 malloc and return a value that is a pointer to the allocated
178 value_allocate_space_in_inferior (int len
)
180 struct objfile
*objf
;
181 struct value
*val
= find_function_in_inferior ("malloc", &objf
);
182 struct gdbarch
*gdbarch
= get_objfile_arch (objf
);
183 struct value
*blocklen
;
185 blocklen
= value_from_longest (builtin_type (gdbarch
)->builtin_int
, len
);
186 val
= call_function_by_hand (val
, NULL
, blocklen
);
187 if (value_logical_not (val
))
189 if (!target_has_execution
)
190 error (_("No memory available to program now: "
191 "you need to start the target first"));
193 error (_("No memory available to program: call to malloc failed"));
199 allocate_space_in_inferior (int len
)
201 return value_as_long (value_allocate_space_in_inferior (len
));
204 /* Cast struct value VAL to type TYPE and return as a value.
205 Both type and val must be of TYPE_CODE_STRUCT or TYPE_CODE_UNION
206 for this to work. Typedef to one of the codes is permitted.
207 Returns NULL if the cast is neither an upcast nor a downcast. */
209 static struct value
*
210 value_cast_structs (struct type
*type
, struct value
*v2
)
216 gdb_assert (type
!= NULL
&& v2
!= NULL
);
218 t1
= check_typedef (type
);
219 t2
= check_typedef (value_type (v2
));
221 /* Check preconditions. */
222 gdb_assert ((TYPE_CODE (t1
) == TYPE_CODE_STRUCT
223 || TYPE_CODE (t1
) == TYPE_CODE_UNION
)
224 && !!"Precondition is that type is of STRUCT or UNION kind.");
225 gdb_assert ((TYPE_CODE (t2
) == TYPE_CODE_STRUCT
226 || TYPE_CODE (t2
) == TYPE_CODE_UNION
)
227 && !!"Precondition is that value is of STRUCT or UNION kind");
229 if (TYPE_NAME (t1
) != NULL
230 && TYPE_NAME (t2
) != NULL
231 && !strcmp (TYPE_NAME (t1
), TYPE_NAME (t2
)))
234 /* Upcasting: look in the type of the source to see if it contains the
235 type of the target as a superclass. If so, we'll need to
236 offset the pointer rather than just change its type. */
237 if (TYPE_NAME (t1
) != NULL
)
239 v
= search_struct_field (TYPE_NAME (t1
),
245 /* Downcasting: look in the type of the target to see if it contains the
246 type of the source as a superclass. If so, we'll need to
247 offset the pointer rather than just change its type. */
248 if (TYPE_NAME (t2
) != NULL
)
250 /* Try downcasting using the run-time type of the value. */
253 struct type
*real_type
;
255 real_type
= value_rtti_type (v2
, &full
, &top
, &using_enc
);
258 v
= value_full_object (v2
, real_type
, full
, top
, using_enc
);
259 v
= value_at_lazy (real_type
, value_address (v
));
260 real_type
= value_type (v
);
262 /* We might be trying to cast to the outermost enclosing
263 type, in which case search_struct_field won't work. */
264 if (TYPE_NAME (real_type
) != NULL
265 && !strcmp (TYPE_NAME (real_type
), TYPE_NAME (t1
)))
268 v
= search_struct_field (TYPE_NAME (t2
), v
, real_type
, 1);
273 /* Try downcasting using information from the destination type
274 T2. This wouldn't work properly for classes with virtual
275 bases, but those were handled above. */
276 v
= search_struct_field (TYPE_NAME (t2
),
277 value_zero (t1
, not_lval
), t1
, 1);
280 /* Downcasting is possible (t1 is superclass of v2). */
281 CORE_ADDR addr2
= value_address (v2
);
283 addr2
-= value_address (v
) + value_embedded_offset (v
);
284 return value_at (type
, addr2
);
291 /* Cast one pointer or reference type to another. Both TYPE and
292 the type of ARG2 should be pointer types, or else both should be
293 reference types. If SUBCLASS_CHECK is non-zero, this will force a
294 check to see whether TYPE is a superclass of ARG2's type. If
295 SUBCLASS_CHECK is zero, then the subclass check is done only when
296 ARG2 is itself non-zero. Returns the new pointer or reference. */
299 value_cast_pointers (struct type
*type
, struct value
*arg2
,
302 struct type
*type1
= check_typedef (type
);
303 struct type
*type2
= check_typedef (value_type (arg2
));
304 struct type
*t1
= check_typedef (TYPE_TARGET_TYPE (type1
));
305 struct type
*t2
= check_typedef (TYPE_TARGET_TYPE (type2
));
307 if (TYPE_CODE (t1
) == TYPE_CODE_STRUCT
308 && TYPE_CODE (t2
) == TYPE_CODE_STRUCT
309 && (subclass_check
|| !value_logical_not (arg2
)))
313 if (TYPE_IS_REFERENCE (type2
))
314 v2
= coerce_ref (arg2
);
316 v2
= value_ind (arg2
);
317 gdb_assert (TYPE_CODE (check_typedef (value_type (v2
)))
318 == TYPE_CODE_STRUCT
&& !!"Why did coercion fail?");
319 v2
= value_cast_structs (t1
, v2
);
320 /* At this point we have what we can have, un-dereference if needed. */
323 struct value
*v
= value_addr (v2
);
325 deprecated_set_value_type (v
, type
);
330 /* No superclass found, just change the pointer type. */
331 arg2
= value_copy (arg2
);
332 deprecated_set_value_type (arg2
, type
);
333 set_value_enclosing_type (arg2
, type
);
334 set_value_pointed_to_offset (arg2
, 0); /* pai: chk_val */
338 /* Cast value ARG2 to type TYPE and return as a value.
339 More general than a C cast: accepts any two types of the same length,
340 and if ARG2 is an lvalue it can be cast into anything at all. */
341 /* In C++, casts may change pointer or object representations. */
344 value_cast (struct type
*type
, struct value
*arg2
)
346 enum type_code code1
;
347 enum type_code code2
;
351 int convert_to_boolean
= 0;
353 if (value_type (arg2
) == type
)
356 /* Check if we are casting struct reference to struct reference. */
357 if (TYPE_IS_REFERENCE (check_typedef (type
)))
359 /* We dereference type; then we recurse and finally
360 we generate value of the given reference. Nothing wrong with
362 struct type
*t1
= check_typedef (type
);
363 struct type
*dereftype
= check_typedef (TYPE_TARGET_TYPE (t1
));
364 struct value
*val
= value_cast (dereftype
, arg2
);
366 return value_ref (val
, TYPE_CODE (t1
));
369 if (TYPE_IS_REFERENCE (check_typedef (value_type (arg2
))))
370 /* We deref the value and then do the cast. */
371 return value_cast (type
, coerce_ref (arg2
));
373 /* Strip typedefs / resolve stubs in order to get at the type's
374 code/length, but remember the original type, to use as the
375 resulting type of the cast, in case it was a typedef. */
376 struct type
*to_type
= type
;
378 type
= check_typedef (type
);
379 code1
= TYPE_CODE (type
);
380 arg2
= coerce_ref (arg2
);
381 type2
= check_typedef (value_type (arg2
));
383 /* You can't cast to a reference type. See value_cast_pointers
385 gdb_assert (!TYPE_IS_REFERENCE (type
));
387 /* A cast to an undetermined-length array_type, such as
388 (TYPE [])OBJECT, is treated like a cast to (TYPE [N])OBJECT,
389 where N is sizeof(OBJECT)/sizeof(TYPE). */
390 if (code1
== TYPE_CODE_ARRAY
)
392 struct type
*element_type
= TYPE_TARGET_TYPE (type
);
393 unsigned element_length
= TYPE_LENGTH (check_typedef (element_type
));
395 if (element_length
> 0 && TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type
))
397 struct type
*range_type
= TYPE_INDEX_TYPE (type
);
398 int val_length
= TYPE_LENGTH (type2
);
399 LONGEST low_bound
, high_bound
, new_length
;
401 if (get_discrete_bounds (range_type
, &low_bound
, &high_bound
) < 0)
402 low_bound
= 0, high_bound
= 0;
403 new_length
= val_length
/ element_length
;
404 if (val_length
% element_length
!= 0)
405 warning (_("array element type size does not "
406 "divide object size in cast"));
407 /* FIXME-type-allocation: need a way to free this type when
408 we are done with it. */
409 range_type
= create_static_range_type ((struct type
*) NULL
,
410 TYPE_TARGET_TYPE (range_type
),
412 new_length
+ low_bound
- 1);
413 deprecated_set_value_type (arg2
,
414 create_array_type ((struct type
*) NULL
,
421 if (current_language
->c_style_arrays
422 && TYPE_CODE (type2
) == TYPE_CODE_ARRAY
423 && !TYPE_VECTOR (type2
))
424 arg2
= value_coerce_array (arg2
);
426 if (TYPE_CODE (type2
) == TYPE_CODE_FUNC
)
427 arg2
= value_coerce_function (arg2
);
429 type2
= check_typedef (value_type (arg2
));
430 code2
= TYPE_CODE (type2
);
432 if (code1
== TYPE_CODE_COMPLEX
)
433 return cast_into_complex (to_type
, arg2
);
434 if (code1
== TYPE_CODE_BOOL
)
436 code1
= TYPE_CODE_INT
;
437 convert_to_boolean
= 1;
439 if (code1
== TYPE_CODE_CHAR
)
440 code1
= TYPE_CODE_INT
;
441 if (code2
== TYPE_CODE_BOOL
|| code2
== TYPE_CODE_CHAR
)
442 code2
= TYPE_CODE_INT
;
444 scalar
= (code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_FLT
445 || code2
== TYPE_CODE_DECFLOAT
|| code2
== TYPE_CODE_ENUM
446 || code2
== TYPE_CODE_RANGE
);
448 if ((code1
== TYPE_CODE_STRUCT
|| code1
== TYPE_CODE_UNION
)
449 && (code2
== TYPE_CODE_STRUCT
|| code2
== TYPE_CODE_UNION
)
450 && TYPE_NAME (type
) != 0)
452 struct value
*v
= value_cast_structs (to_type
, arg2
);
458 if (is_floating_type (type
) && scalar
)
460 if (is_floating_value (arg2
))
462 struct value
*v
= allocate_value (to_type
);
463 target_float_convert (value_contents (arg2
), type2
,
464 value_contents_raw (v
), type
);
468 /* The only option left is an integral type. */
469 if (TYPE_UNSIGNED (type2
))
470 return value_from_ulongest (to_type
, value_as_long (arg2
));
472 return value_from_longest (to_type
, value_as_long (arg2
));
474 else if ((code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_ENUM
475 || code1
== TYPE_CODE_RANGE
)
476 && (scalar
|| code2
== TYPE_CODE_PTR
477 || code2
== TYPE_CODE_MEMBERPTR
))
481 /* When we cast pointers to integers, we mustn't use
482 gdbarch_pointer_to_address to find the address the pointer
483 represents, as value_as_long would. GDB should evaluate
484 expressions just as the compiler would --- and the compiler
485 sees a cast as a simple reinterpretation of the pointer's
487 if (code2
== TYPE_CODE_PTR
)
488 longest
= extract_unsigned_integer
489 (value_contents (arg2
), TYPE_LENGTH (type2
),
490 gdbarch_byte_order (get_type_arch (type2
)));
492 longest
= value_as_long (arg2
);
493 return value_from_longest (to_type
, convert_to_boolean
?
494 (LONGEST
) (longest
? 1 : 0) : longest
);
496 else if (code1
== TYPE_CODE_PTR
&& (code2
== TYPE_CODE_INT
497 || code2
== TYPE_CODE_ENUM
498 || code2
== TYPE_CODE_RANGE
))
500 /* TYPE_LENGTH (type) is the length of a pointer, but we really
501 want the length of an address! -- we are really dealing with
502 addresses (i.e., gdb representations) not pointers (i.e.,
503 target representations) here.
505 This allows things like "print *(int *)0x01000234" to work
506 without printing a misleading message -- which would
507 otherwise occur when dealing with a target having two byte
508 pointers and four byte addresses. */
510 int addr_bit
= gdbarch_addr_bit (get_type_arch (type2
));
511 LONGEST longest
= value_as_long (arg2
);
513 if (addr_bit
< sizeof (LONGEST
) * HOST_CHAR_BIT
)
515 if (longest
>= ((LONGEST
) 1 << addr_bit
)
516 || longest
<= -((LONGEST
) 1 << addr_bit
))
517 warning (_("value truncated"));
519 return value_from_longest (to_type
, longest
);
521 else if (code1
== TYPE_CODE_METHODPTR
&& code2
== TYPE_CODE_INT
522 && value_as_long (arg2
) == 0)
524 struct value
*result
= allocate_value (to_type
);
526 cplus_make_method_ptr (to_type
, value_contents_writeable (result
), 0, 0);
529 else if (code1
== TYPE_CODE_MEMBERPTR
&& code2
== TYPE_CODE_INT
530 && value_as_long (arg2
) == 0)
532 /* The Itanium C++ ABI represents NULL pointers to members as
533 minus one, instead of biasing the normal case. */
534 return value_from_longest (to_type
, -1);
536 else if (code1
== TYPE_CODE_ARRAY
&& TYPE_VECTOR (type
)
537 && code2
== TYPE_CODE_ARRAY
&& TYPE_VECTOR (type2
)
538 && TYPE_LENGTH (type
) != TYPE_LENGTH (type2
))
539 error (_("Cannot convert between vector values of different sizes"));
540 else if (code1
== TYPE_CODE_ARRAY
&& TYPE_VECTOR (type
) && scalar
541 && TYPE_LENGTH (type
) != TYPE_LENGTH (type2
))
542 error (_("can only cast scalar to vector of same size"));
543 else if (code1
== TYPE_CODE_VOID
)
545 return value_zero (to_type
, not_lval
);
547 else if (TYPE_LENGTH (type
) == TYPE_LENGTH (type2
))
549 if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_PTR
)
550 return value_cast_pointers (to_type
, arg2
, 0);
552 arg2
= value_copy (arg2
);
553 deprecated_set_value_type (arg2
, to_type
);
554 set_value_enclosing_type (arg2
, to_type
);
555 set_value_pointed_to_offset (arg2
, 0); /* pai: chk_val */
558 else if (VALUE_LVAL (arg2
) == lval_memory
)
559 return value_at_lazy (to_type
, value_address (arg2
));
562 error (_("Invalid cast."));
567 /* The C++ reinterpret_cast operator. */
570 value_reinterpret_cast (struct type
*type
, struct value
*arg
)
572 struct value
*result
;
573 struct type
*real_type
= check_typedef (type
);
574 struct type
*arg_type
, *dest_type
;
576 enum type_code dest_code
, arg_code
;
578 /* Do reference, function, and array conversion. */
579 arg
= coerce_array (arg
);
581 /* Attempt to preserve the type the user asked for. */
584 /* If we are casting to a reference type, transform
585 reinterpret_cast<T&[&]>(V) to *reinterpret_cast<T*>(&V). */
586 if (TYPE_IS_REFERENCE (real_type
))
589 arg
= value_addr (arg
);
590 dest_type
= lookup_pointer_type (TYPE_TARGET_TYPE (dest_type
));
591 real_type
= lookup_pointer_type (real_type
);
594 arg_type
= value_type (arg
);
596 dest_code
= TYPE_CODE (real_type
);
597 arg_code
= TYPE_CODE (arg_type
);
599 /* We can convert pointer types, or any pointer type to int, or int
601 if ((dest_code
== TYPE_CODE_PTR
&& arg_code
== TYPE_CODE_INT
)
602 || (dest_code
== TYPE_CODE_INT
&& arg_code
== TYPE_CODE_PTR
)
603 || (dest_code
== TYPE_CODE_METHODPTR
&& arg_code
== TYPE_CODE_INT
)
604 || (dest_code
== TYPE_CODE_INT
&& arg_code
== TYPE_CODE_METHODPTR
)
605 || (dest_code
== TYPE_CODE_MEMBERPTR
&& arg_code
== TYPE_CODE_INT
)
606 || (dest_code
== TYPE_CODE_INT
&& arg_code
== TYPE_CODE_MEMBERPTR
)
607 || (dest_code
== arg_code
608 && (dest_code
== TYPE_CODE_PTR
609 || dest_code
== TYPE_CODE_METHODPTR
610 || dest_code
== TYPE_CODE_MEMBERPTR
)))
611 result
= value_cast (dest_type
, arg
);
613 error (_("Invalid reinterpret_cast"));
616 result
= value_cast (type
, value_ref (value_ind (result
),
622 /* A helper for value_dynamic_cast. This implements the first of two
623 runtime checks: we iterate over all the base classes of the value's
624 class which are equal to the desired class; if only one of these
625 holds the value, then it is the answer. */
628 dynamic_cast_check_1 (struct type
*desired_type
,
629 const gdb_byte
*valaddr
,
630 LONGEST embedded_offset
,
633 struct type
*search_type
,
635 struct type
*arg_type
,
636 struct value
**result
)
638 int i
, result_count
= 0;
640 for (i
= 0; i
< TYPE_N_BASECLASSES (search_type
) && result_count
< 2; ++i
)
642 LONGEST offset
= baseclass_offset (search_type
, i
, valaddr
,
646 if (class_types_same_p (desired_type
, TYPE_BASECLASS (search_type
, i
)))
648 if (address
+ embedded_offset
+ offset
>= arg_addr
649 && address
+ embedded_offset
+ offset
< arg_addr
+ TYPE_LENGTH (arg_type
))
653 *result
= value_at_lazy (TYPE_BASECLASS (search_type
, i
),
654 address
+ embedded_offset
+ offset
);
658 result_count
+= dynamic_cast_check_1 (desired_type
,
660 embedded_offset
+ offset
,
662 TYPE_BASECLASS (search_type
, i
),
671 /* A helper for value_dynamic_cast. This implements the second of two
672 runtime checks: we look for a unique public sibling class of the
673 argument's declared class. */
676 dynamic_cast_check_2 (struct type
*desired_type
,
677 const gdb_byte
*valaddr
,
678 LONGEST embedded_offset
,
681 struct type
*search_type
,
682 struct value
**result
)
684 int i
, result_count
= 0;
686 for (i
= 0; i
< TYPE_N_BASECLASSES (search_type
) && result_count
< 2; ++i
)
690 if (! BASETYPE_VIA_PUBLIC (search_type
, i
))
693 offset
= baseclass_offset (search_type
, i
, valaddr
, embedded_offset
,
695 if (class_types_same_p (desired_type
, TYPE_BASECLASS (search_type
, i
)))
699 *result
= value_at_lazy (TYPE_BASECLASS (search_type
, i
),
700 address
+ embedded_offset
+ offset
);
703 result_count
+= dynamic_cast_check_2 (desired_type
,
705 embedded_offset
+ offset
,
707 TYPE_BASECLASS (search_type
, i
),
714 /* The C++ dynamic_cast operator. */
717 value_dynamic_cast (struct type
*type
, struct value
*arg
)
721 struct type
*resolved_type
= check_typedef (type
);
722 struct type
*arg_type
= check_typedef (value_type (arg
));
723 struct type
*class_type
, *rtti_type
;
724 struct value
*result
, *tem
, *original_arg
= arg
;
726 int is_ref
= TYPE_IS_REFERENCE (resolved_type
);
728 if (TYPE_CODE (resolved_type
) != TYPE_CODE_PTR
729 && !TYPE_IS_REFERENCE (resolved_type
))
730 error (_("Argument to dynamic_cast must be a pointer or reference type"));
731 if (TYPE_CODE (TYPE_TARGET_TYPE (resolved_type
)) != TYPE_CODE_VOID
732 && TYPE_CODE (TYPE_TARGET_TYPE (resolved_type
)) != TYPE_CODE_STRUCT
)
733 error (_("Argument to dynamic_cast must be pointer to class or `void *'"));
735 class_type
= check_typedef (TYPE_TARGET_TYPE (resolved_type
));
736 if (TYPE_CODE (resolved_type
) == TYPE_CODE_PTR
)
738 if (TYPE_CODE (arg_type
) != TYPE_CODE_PTR
739 && ! (TYPE_CODE (arg_type
) == TYPE_CODE_INT
740 && value_as_long (arg
) == 0))
741 error (_("Argument to dynamic_cast does not have pointer type"));
742 if (TYPE_CODE (arg_type
) == TYPE_CODE_PTR
)
744 arg_type
= check_typedef (TYPE_TARGET_TYPE (arg_type
));
745 if (TYPE_CODE (arg_type
) != TYPE_CODE_STRUCT
)
746 error (_("Argument to dynamic_cast does "
747 "not have pointer to class type"));
750 /* Handle NULL pointers. */
751 if (value_as_long (arg
) == 0)
752 return value_zero (type
, not_lval
);
754 arg
= value_ind (arg
);
758 if (TYPE_CODE (arg_type
) != TYPE_CODE_STRUCT
)
759 error (_("Argument to dynamic_cast does not have class type"));
762 /* If the classes are the same, just return the argument. */
763 if (class_types_same_p (class_type
, arg_type
))
764 return value_cast (type
, arg
);
766 /* If the target type is a unique base class of the argument's
767 declared type, just cast it. */
768 if (is_ancestor (class_type
, arg_type
))
770 if (is_unique_ancestor (class_type
, arg
))
771 return value_cast (type
, original_arg
);
772 error (_("Ambiguous dynamic_cast"));
775 rtti_type
= value_rtti_type (arg
, &full
, &top
, &using_enc
);
777 error (_("Couldn't determine value's most derived type for dynamic_cast"));
779 /* Compute the most derived object's address. */
780 addr
= value_address (arg
);
788 addr
+= top
+ value_embedded_offset (arg
);
790 /* dynamic_cast<void *> means to return a pointer to the
791 most-derived object. */
792 if (TYPE_CODE (resolved_type
) == TYPE_CODE_PTR
793 && TYPE_CODE (TYPE_TARGET_TYPE (resolved_type
)) == TYPE_CODE_VOID
)
794 return value_at_lazy (type
, addr
);
796 tem
= value_at (type
, addr
);
797 type
= value_type (tem
);
799 /* The first dynamic check specified in 5.2.7. */
800 if (is_public_ancestor (arg_type
, TYPE_TARGET_TYPE (resolved_type
)))
802 if (class_types_same_p (rtti_type
, TYPE_TARGET_TYPE (resolved_type
)))
805 if (dynamic_cast_check_1 (TYPE_TARGET_TYPE (resolved_type
),
806 value_contents_for_printing (tem
),
807 value_embedded_offset (tem
),
808 value_address (tem
), tem
,
812 return value_cast (type
,
814 ? value_ref (result
, TYPE_CODE (resolved_type
))
815 : value_addr (result
));
818 /* The second dynamic check specified in 5.2.7. */
820 if (is_public_ancestor (arg_type
, rtti_type
)
821 && dynamic_cast_check_2 (TYPE_TARGET_TYPE (resolved_type
),
822 value_contents_for_printing (tem
),
823 value_embedded_offset (tem
),
824 value_address (tem
), tem
,
825 rtti_type
, &result
) == 1)
826 return value_cast (type
,
828 ? value_ref (result
, TYPE_CODE (resolved_type
))
829 : value_addr (result
));
831 if (TYPE_CODE (resolved_type
) == TYPE_CODE_PTR
)
832 return value_zero (type
, not_lval
);
834 error (_("dynamic_cast failed"));
837 /* Create a value of type TYPE that is zero, and return it. */
840 value_zero (struct type
*type
, enum lval_type lv
)
842 struct value
*val
= allocate_value (type
);
844 VALUE_LVAL (val
) = (lv
== lval_computed
? not_lval
: lv
);
848 /* Create a not_lval value of numeric type TYPE that is one, and return it. */
851 value_one (struct type
*type
)
853 struct type
*type1
= check_typedef (type
);
856 if (is_integral_type (type1
) || is_floating_type (type1
))
858 val
= value_from_longest (type
, (LONGEST
) 1);
860 else if (TYPE_CODE (type1
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type1
))
862 struct type
*eltype
= check_typedef (TYPE_TARGET_TYPE (type1
));
864 LONGEST low_bound
, high_bound
;
867 if (!get_array_bounds (type1
, &low_bound
, &high_bound
))
868 error (_("Could not determine the vector bounds"));
870 val
= allocate_value (type
);
871 for (i
= 0; i
< high_bound
- low_bound
+ 1; i
++)
873 tmp
= value_one (eltype
);
874 memcpy (value_contents_writeable (val
) + i
* TYPE_LENGTH (eltype
),
875 value_contents_all (tmp
), TYPE_LENGTH (eltype
));
880 error (_("Not a numeric type."));
883 /* value_one result is never used for assignments to. */
884 gdb_assert (VALUE_LVAL (val
) == not_lval
);
889 /* Helper function for value_at, value_at_lazy, and value_at_lazy_stack.
890 The type of the created value may differ from the passed type TYPE.
891 Make sure to retrieve the returned values's new type after this call
892 e.g. in case the type is a variable length array. */
894 static struct value
*
895 get_value_at (struct type
*type
, CORE_ADDR addr
, int lazy
)
899 if (TYPE_CODE (check_typedef (type
)) == TYPE_CODE_VOID
)
900 error (_("Attempt to dereference a generic pointer."));
902 val
= value_from_contents_and_address (type
, NULL
, addr
);
905 value_fetch_lazy (val
);
910 /* Return a value with type TYPE located at ADDR.
912 Call value_at only if the data needs to be fetched immediately;
913 if we can be 'lazy' and defer the fetch, perhaps indefinately, call
914 value_at_lazy instead. value_at_lazy simply records the address of
915 the data and sets the lazy-evaluation-required flag. The lazy flag
916 is tested in the value_contents macro, which is used if and when
917 the contents are actually required. The type of the created value
918 may differ from the passed type TYPE. Make sure to retrieve the
919 returned values's new type after this call e.g. in case the type
920 is a variable length array.
922 Note: value_at does *NOT* handle embedded offsets; perform such
923 adjustments before or after calling it. */
926 value_at (struct type
*type
, CORE_ADDR addr
)
928 return get_value_at (type
, addr
, 0);
931 /* Return a lazy value with type TYPE located at ADDR (cf. value_at).
932 The type of the created value may differ from the passed type TYPE.
933 Make sure to retrieve the returned values's new type after this call
934 e.g. in case the type is a variable length array. */
937 value_at_lazy (struct type
*type
, CORE_ADDR addr
)
939 return get_value_at (type
, addr
, 1);
943 read_value_memory (struct value
*val
, LONGEST bit_offset
,
944 int stack
, CORE_ADDR memaddr
,
945 gdb_byte
*buffer
, size_t length
)
947 ULONGEST xfered_total
= 0;
948 struct gdbarch
*arch
= get_value_arch (val
);
949 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
950 enum target_object object
;
952 object
= stack
? TARGET_OBJECT_STACK_MEMORY
: TARGET_OBJECT_MEMORY
;
954 while (xfered_total
< length
)
956 enum target_xfer_status status
;
957 ULONGEST xfered_partial
;
959 status
= target_xfer_partial (current_top_target (),
961 buffer
+ xfered_total
* unit_size
, NULL
,
962 memaddr
+ xfered_total
,
963 length
- xfered_total
,
966 if (status
== TARGET_XFER_OK
)
968 else if (status
== TARGET_XFER_UNAVAILABLE
)
969 mark_value_bits_unavailable (val
, (xfered_total
* HOST_CHAR_BIT
971 xfered_partial
* HOST_CHAR_BIT
);
972 else if (status
== TARGET_XFER_EOF
)
973 memory_error (TARGET_XFER_E_IO
, memaddr
+ xfered_total
);
975 memory_error (status
, memaddr
+ xfered_total
);
977 xfered_total
+= xfered_partial
;
982 /* Store the contents of FROMVAL into the location of TOVAL.
983 Return a new value with the location of TOVAL and contents of FROMVAL. */
986 value_assign (struct value
*toval
, struct value
*fromval
)
990 struct frame_id old_frame
;
992 if (!deprecated_value_modifiable (toval
))
993 error (_("Left operand of assignment is not a modifiable lvalue."));
995 toval
= coerce_ref (toval
);
997 type
= value_type (toval
);
998 if (VALUE_LVAL (toval
) != lval_internalvar
)
999 fromval
= value_cast (type
, fromval
);
1002 /* Coerce arrays and functions to pointers, except for arrays
1003 which only live in GDB's storage. */
1004 if (!value_must_coerce_to_target (fromval
))
1005 fromval
= coerce_array (fromval
);
1008 type
= check_typedef (type
);
1010 /* Since modifying a register can trash the frame chain, and
1011 modifying memory can trash the frame cache, we save the old frame
1012 and then restore the new frame afterwards. */
1013 old_frame
= get_frame_id (deprecated_safe_get_selected_frame ());
1015 switch (VALUE_LVAL (toval
))
1017 case lval_internalvar
:
1018 set_internalvar (VALUE_INTERNALVAR (toval
), fromval
);
1019 return value_of_internalvar (get_type_arch (type
),
1020 VALUE_INTERNALVAR (toval
));
1022 case lval_internalvar_component
:
1024 LONGEST offset
= value_offset (toval
);
1026 /* Are we dealing with a bitfield?
1028 It is important to mention that `value_parent (toval)' is
1029 non-NULL iff `value_bitsize (toval)' is non-zero. */
1030 if (value_bitsize (toval
))
1032 /* VALUE_INTERNALVAR below refers to the parent value, while
1033 the offset is relative to this parent value. */
1034 gdb_assert (value_parent (value_parent (toval
)) == NULL
);
1035 offset
+= value_offset (value_parent (toval
));
1038 set_internalvar_component (VALUE_INTERNALVAR (toval
),
1040 value_bitpos (toval
),
1041 value_bitsize (toval
),
1048 const gdb_byte
*dest_buffer
;
1049 CORE_ADDR changed_addr
;
1051 gdb_byte buffer
[sizeof (LONGEST
)];
1053 if (value_bitsize (toval
))
1055 struct value
*parent
= value_parent (toval
);
1057 changed_addr
= value_address (parent
) + value_offset (toval
);
1058 changed_len
= (value_bitpos (toval
)
1059 + value_bitsize (toval
)
1060 + HOST_CHAR_BIT
- 1)
1063 /* If we can read-modify-write exactly the size of the
1064 containing type (e.g. short or int) then do so. This
1065 is safer for volatile bitfields mapped to hardware
1067 if (changed_len
< TYPE_LENGTH (type
)
1068 && TYPE_LENGTH (type
) <= (int) sizeof (LONGEST
)
1069 && ((LONGEST
) changed_addr
% TYPE_LENGTH (type
)) == 0)
1070 changed_len
= TYPE_LENGTH (type
);
1072 if (changed_len
> (int) sizeof (LONGEST
))
1073 error (_("Can't handle bitfields which "
1074 "don't fit in a %d bit word."),
1075 (int) sizeof (LONGEST
) * HOST_CHAR_BIT
);
1077 read_memory (changed_addr
, buffer
, changed_len
);
1078 modify_field (type
, buffer
, value_as_long (fromval
),
1079 value_bitpos (toval
), value_bitsize (toval
));
1080 dest_buffer
= buffer
;
1084 changed_addr
= value_address (toval
);
1085 changed_len
= type_length_units (type
);
1086 dest_buffer
= value_contents (fromval
);
1089 write_memory_with_notification (changed_addr
, dest_buffer
, changed_len
);
1095 struct frame_info
*frame
;
1096 struct gdbarch
*gdbarch
;
1099 /* Figure out which frame this is in currently.
1101 We use VALUE_FRAME_ID for obtaining the value's frame id instead of
1102 VALUE_NEXT_FRAME_ID due to requiring a frame which may be passed to
1103 put_frame_register_bytes() below. That function will (eventually)
1104 perform the necessary unwind operation by first obtaining the next
1106 frame
= frame_find_by_id (VALUE_FRAME_ID (toval
));
1108 value_reg
= VALUE_REGNUM (toval
);
1111 error (_("Value being assigned to is no longer active."));
1113 gdbarch
= get_frame_arch (frame
);
1115 if (value_bitsize (toval
))
1117 struct value
*parent
= value_parent (toval
);
1118 LONGEST offset
= value_offset (parent
) + value_offset (toval
);
1120 gdb_byte buffer
[sizeof (LONGEST
)];
1123 changed_len
= (value_bitpos (toval
)
1124 + value_bitsize (toval
)
1125 + HOST_CHAR_BIT
- 1)
1128 if (changed_len
> (int) sizeof (LONGEST
))
1129 error (_("Can't handle bitfields which "
1130 "don't fit in a %d bit word."),
1131 (int) sizeof (LONGEST
) * HOST_CHAR_BIT
);
1133 if (!get_frame_register_bytes (frame
, value_reg
, offset
,
1134 changed_len
, buffer
,
1138 throw_error (OPTIMIZED_OUT_ERROR
,
1139 _("value has been optimized out"));
1141 throw_error (NOT_AVAILABLE_ERROR
,
1142 _("value is not available"));
1145 modify_field (type
, buffer
, value_as_long (fromval
),
1146 value_bitpos (toval
), value_bitsize (toval
));
1148 put_frame_register_bytes (frame
, value_reg
, offset
,
1149 changed_len
, buffer
);
1153 if (gdbarch_convert_register_p (gdbarch
, VALUE_REGNUM (toval
),
1156 /* If TOVAL is a special machine register requiring
1157 conversion of program values to a special raw
1159 gdbarch_value_to_register (gdbarch
, frame
,
1160 VALUE_REGNUM (toval
), type
,
1161 value_contents (fromval
));
1165 put_frame_register_bytes (frame
, value_reg
,
1166 value_offset (toval
),
1168 value_contents (fromval
));
1172 gdb::observers::register_changed
.notify (frame
, value_reg
);
1178 const struct lval_funcs
*funcs
= value_computed_funcs (toval
);
1180 if (funcs
->write
!= NULL
)
1182 funcs
->write (toval
, fromval
);
1189 error (_("Left operand of assignment is not an lvalue."));
1192 /* Assigning to the stack pointer, frame pointer, and other
1193 (architecture and calling convention specific) registers may
1194 cause the frame cache and regcache to be out of date. Assigning to memory
1195 also can. We just do this on all assignments to registers or
1196 memory, for simplicity's sake; I doubt the slowdown matters. */
1197 switch (VALUE_LVAL (toval
))
1203 gdb::observers::target_changed
.notify (current_top_target ());
1205 /* Having destroyed the frame cache, restore the selected
1208 /* FIXME: cagney/2002-11-02: There has to be a better way of
1209 doing this. Instead of constantly saving/restoring the
1210 frame. Why not create a get_selected_frame() function that,
1211 having saved the selected frame's ID can automatically
1212 re-find the previously selected frame automatically. */
1215 struct frame_info
*fi
= frame_find_by_id (old_frame
);
1226 /* If the field does not entirely fill a LONGEST, then zero the sign
1227 bits. If the field is signed, and is negative, then sign
1229 if ((value_bitsize (toval
) > 0)
1230 && (value_bitsize (toval
) < 8 * (int) sizeof (LONGEST
)))
1232 LONGEST fieldval
= value_as_long (fromval
);
1233 LONGEST valmask
= (((ULONGEST
) 1) << value_bitsize (toval
)) - 1;
1235 fieldval
&= valmask
;
1236 if (!TYPE_UNSIGNED (type
)
1237 && (fieldval
& (valmask
^ (valmask
>> 1))))
1238 fieldval
|= ~valmask
;
1240 fromval
= value_from_longest (type
, fieldval
);
1243 /* The return value is a copy of TOVAL so it shares its location
1244 information, but its contents are updated from FROMVAL. This
1245 implies the returned value is not lazy, even if TOVAL was. */
1246 val
= value_copy (toval
);
1247 set_value_lazy (val
, 0);
1248 memcpy (value_contents_raw (val
), value_contents (fromval
),
1249 TYPE_LENGTH (type
));
1251 /* We copy over the enclosing type and pointed-to offset from FROMVAL
1252 in the case of pointer types. For object types, the enclosing type
1253 and embedded offset must *not* be copied: the target object refered
1254 to by TOVAL retains its original dynamic type after assignment. */
1255 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1257 set_value_enclosing_type (val
, value_enclosing_type (fromval
));
1258 set_value_pointed_to_offset (val
, value_pointed_to_offset (fromval
));
1264 /* Extend a value VAL to COUNT repetitions of its type. */
1267 value_repeat (struct value
*arg1
, int count
)
1271 if (VALUE_LVAL (arg1
) != lval_memory
)
1272 error (_("Only values in memory can be extended with '@'."));
1274 error (_("Invalid number %d of repetitions."), count
);
1276 val
= allocate_repeat_value (value_enclosing_type (arg1
), count
);
1278 VALUE_LVAL (val
) = lval_memory
;
1279 set_value_address (val
, value_address (arg1
));
1281 read_value_memory (val
, 0, value_stack (val
), value_address (val
),
1282 value_contents_all_raw (val
),
1283 type_length_units (value_enclosing_type (val
)));
1289 value_of_variable (struct symbol
*var
, const struct block
*b
)
1291 struct frame_info
*frame
= NULL
;
1293 if (symbol_read_needs_frame (var
))
1294 frame
= get_selected_frame (_("No frame selected."));
1296 return read_var_value (var
, b
, frame
);
1300 address_of_variable (struct symbol
*var
, const struct block
*b
)
1302 struct type
*type
= SYMBOL_TYPE (var
);
1305 /* Evaluate it first; if the result is a memory address, we're fine.
1306 Lazy evaluation pays off here. */
1308 val
= value_of_variable (var
, b
);
1309 type
= value_type (val
);
1311 if ((VALUE_LVAL (val
) == lval_memory
&& value_lazy (val
))
1312 || TYPE_CODE (type
) == TYPE_CODE_FUNC
)
1314 CORE_ADDR addr
= value_address (val
);
1316 return value_from_pointer (lookup_pointer_type (type
), addr
);
1319 /* Not a memory address; check what the problem was. */
1320 switch (VALUE_LVAL (val
))
1324 struct frame_info
*frame
;
1325 const char *regname
;
1327 frame
= frame_find_by_id (VALUE_NEXT_FRAME_ID (val
));
1330 regname
= gdbarch_register_name (get_frame_arch (frame
),
1331 VALUE_REGNUM (val
));
1332 gdb_assert (regname
&& *regname
);
1334 error (_("Address requested for identifier "
1335 "\"%s\" which is in register $%s"),
1336 SYMBOL_PRINT_NAME (var
), regname
);
1341 error (_("Can't take address of \"%s\" which isn't an lvalue."),
1342 SYMBOL_PRINT_NAME (var
));
1349 /* Return one if VAL does not live in target memory, but should in order
1350 to operate on it. Otherwise return zero. */
1353 value_must_coerce_to_target (struct value
*val
)
1355 struct type
*valtype
;
1357 /* The only lval kinds which do not live in target memory. */
1358 if (VALUE_LVAL (val
) != not_lval
1359 && VALUE_LVAL (val
) != lval_internalvar
1360 && VALUE_LVAL (val
) != lval_xcallable
)
1363 valtype
= check_typedef (value_type (val
));
1365 switch (TYPE_CODE (valtype
))
1367 case TYPE_CODE_ARRAY
:
1368 return TYPE_VECTOR (valtype
) ? 0 : 1;
1369 case TYPE_CODE_STRING
:
1376 /* Make sure that VAL lives in target memory if it's supposed to. For
1377 instance, strings are constructed as character arrays in GDB's
1378 storage, and this function copies them to the target. */
1381 value_coerce_to_target (struct value
*val
)
1386 if (!value_must_coerce_to_target (val
))
1389 length
= TYPE_LENGTH (check_typedef (value_type (val
)));
1390 addr
= allocate_space_in_inferior (length
);
1391 write_memory (addr
, value_contents (val
), length
);
1392 return value_at_lazy (value_type (val
), addr
);
1395 /* Given a value which is an array, return a value which is a pointer
1396 to its first element, regardless of whether or not the array has a
1397 nonzero lower bound.
1399 FIXME: A previous comment here indicated that this routine should
1400 be substracting the array's lower bound. It's not clear to me that
1401 this is correct. Given an array subscripting operation, it would
1402 certainly work to do the adjustment here, essentially computing:
1404 (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0])
1406 However I believe a more appropriate and logical place to account
1407 for the lower bound is to do so in value_subscript, essentially
1410 (&array[0] + ((index - lowerbound) * sizeof array[0]))
1412 As further evidence consider what would happen with operations
1413 other than array subscripting, where the caller would get back a
1414 value that had an address somewhere before the actual first element
1415 of the array, and the information about the lower bound would be
1416 lost because of the coercion to pointer type. */
1419 value_coerce_array (struct value
*arg1
)
1421 struct type
*type
= check_typedef (value_type (arg1
));
1423 /* If the user tries to do something requiring a pointer with an
1424 array that has not yet been pushed to the target, then this would
1425 be a good time to do so. */
1426 arg1
= value_coerce_to_target (arg1
);
1428 if (VALUE_LVAL (arg1
) != lval_memory
)
1429 error (_("Attempt to take address of value not located in memory."));
1431 return value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type
)),
1432 value_address (arg1
));
1435 /* Given a value which is a function, return a value which is a pointer
1439 value_coerce_function (struct value
*arg1
)
1441 struct value
*retval
;
1443 if (VALUE_LVAL (arg1
) != lval_memory
)
1444 error (_("Attempt to take address of value not located in memory."));
1446 retval
= value_from_pointer (lookup_pointer_type (value_type (arg1
)),
1447 value_address (arg1
));
1451 /* Return a pointer value for the object for which ARG1 is the
1455 value_addr (struct value
*arg1
)
1458 struct type
*type
= check_typedef (value_type (arg1
));
1460 if (TYPE_IS_REFERENCE (type
))
1462 if (value_bits_synthetic_pointer (arg1
, value_embedded_offset (arg1
),
1463 TARGET_CHAR_BIT
* TYPE_LENGTH (type
)))
1464 arg1
= coerce_ref (arg1
);
1467 /* Copy the value, but change the type from (T&) to (T*). We
1468 keep the same location information, which is efficient, and
1469 allows &(&X) to get the location containing the reference.
1470 Do the same to its enclosing type for consistency. */
1471 struct type
*type_ptr
1472 = lookup_pointer_type (TYPE_TARGET_TYPE (type
));
1473 struct type
*enclosing_type
1474 = check_typedef (value_enclosing_type (arg1
));
1475 struct type
*enclosing_type_ptr
1476 = lookup_pointer_type (TYPE_TARGET_TYPE (enclosing_type
));
1478 arg2
= value_copy (arg1
);
1479 deprecated_set_value_type (arg2
, type_ptr
);
1480 set_value_enclosing_type (arg2
, enclosing_type_ptr
);
1485 if (TYPE_CODE (type
) == TYPE_CODE_FUNC
)
1486 return value_coerce_function (arg1
);
1488 /* If this is an array that has not yet been pushed to the target,
1489 then this would be a good time to force it to memory. */
1490 arg1
= value_coerce_to_target (arg1
);
1492 if (VALUE_LVAL (arg1
) != lval_memory
)
1493 error (_("Attempt to take address of value not located in memory."));
1495 /* Get target memory address. */
1496 arg2
= value_from_pointer (lookup_pointer_type (value_type (arg1
)),
1497 (value_address (arg1
)
1498 + value_embedded_offset (arg1
)));
1500 /* This may be a pointer to a base subobject; so remember the
1501 full derived object's type ... */
1502 set_value_enclosing_type (arg2
,
1503 lookup_pointer_type (value_enclosing_type (arg1
)));
1504 /* ... and also the relative position of the subobject in the full
1506 set_value_pointed_to_offset (arg2
, value_embedded_offset (arg1
));
1510 /* Return a reference value for the object for which ARG1 is the
1514 value_ref (struct value
*arg1
, enum type_code refcode
)
1517 struct type
*type
= check_typedef (value_type (arg1
));
1519 gdb_assert (refcode
== TYPE_CODE_REF
|| refcode
== TYPE_CODE_RVALUE_REF
);
1521 if ((TYPE_CODE (type
) == TYPE_CODE_REF
1522 || TYPE_CODE (type
) == TYPE_CODE_RVALUE_REF
)
1523 && TYPE_CODE (type
) == refcode
)
1526 arg2
= value_addr (arg1
);
1527 deprecated_set_value_type (arg2
, lookup_reference_type (type
, refcode
));
1531 /* Given a value of a pointer type, apply the C unary * operator to
1535 value_ind (struct value
*arg1
)
1537 struct type
*base_type
;
1540 arg1
= coerce_array (arg1
);
1542 base_type
= check_typedef (value_type (arg1
));
1544 if (VALUE_LVAL (arg1
) == lval_computed
)
1546 const struct lval_funcs
*funcs
= value_computed_funcs (arg1
);
1548 if (funcs
->indirect
)
1550 struct value
*result
= funcs
->indirect (arg1
);
1557 if (TYPE_CODE (base_type
) == TYPE_CODE_PTR
)
1559 struct type
*enc_type
;
1561 /* We may be pointing to something embedded in a larger object.
1562 Get the real type of the enclosing object. */
1563 enc_type
= check_typedef (value_enclosing_type (arg1
));
1564 enc_type
= TYPE_TARGET_TYPE (enc_type
);
1566 if (TYPE_CODE (check_typedef (enc_type
)) == TYPE_CODE_FUNC
1567 || TYPE_CODE (check_typedef (enc_type
)) == TYPE_CODE_METHOD
)
1568 /* For functions, go through find_function_addr, which knows
1569 how to handle function descriptors. */
1570 arg2
= value_at_lazy (enc_type
,
1571 find_function_addr (arg1
, NULL
));
1573 /* Retrieve the enclosing object pointed to. */
1574 arg2
= value_at_lazy (enc_type
,
1575 (value_as_address (arg1
)
1576 - value_pointed_to_offset (arg1
)));
1578 enc_type
= value_type (arg2
);
1579 return readjust_indirect_value_type (arg2
, enc_type
, base_type
, arg1
);
1582 error (_("Attempt to take contents of a non-pointer value."));
1585 /* Create a value for an array by allocating space in GDB, copying the
1586 data into that space, and then setting up an array value.
1588 The array bounds are set from LOWBOUND and HIGHBOUND, and the array
1589 is populated from the values passed in ELEMVEC.
1591 The element type of the array is inherited from the type of the
1592 first element, and all elements must have the same size (though we
1593 don't currently enforce any restriction on their types). */
1596 value_array (int lowbound
, int highbound
, struct value
**elemvec
)
1600 ULONGEST typelength
;
1602 struct type
*arraytype
;
1604 /* Validate that the bounds are reasonable and that each of the
1605 elements have the same size. */
1607 nelem
= highbound
- lowbound
+ 1;
1610 error (_("bad array bounds (%d, %d)"), lowbound
, highbound
);
1612 typelength
= type_length_units (value_enclosing_type (elemvec
[0]));
1613 for (idx
= 1; idx
< nelem
; idx
++)
1615 if (type_length_units (value_enclosing_type (elemvec
[idx
]))
1618 error (_("array elements must all be the same size"));
1622 arraytype
= lookup_array_range_type (value_enclosing_type (elemvec
[0]),
1623 lowbound
, highbound
);
1625 if (!current_language
->c_style_arrays
)
1627 val
= allocate_value (arraytype
);
1628 for (idx
= 0; idx
< nelem
; idx
++)
1629 value_contents_copy (val
, idx
* typelength
, elemvec
[idx
], 0,
1634 /* Allocate space to store the array, and then initialize it by
1635 copying in each element. */
1637 val
= allocate_value (arraytype
);
1638 for (idx
= 0; idx
< nelem
; idx
++)
1639 value_contents_copy (val
, idx
* typelength
, elemvec
[idx
], 0, typelength
);
1644 value_cstring (const char *ptr
, ssize_t len
, struct type
*char_type
)
1647 int lowbound
= current_language
->string_lower_bound
;
1648 ssize_t highbound
= len
/ TYPE_LENGTH (char_type
);
1649 struct type
*stringtype
1650 = lookup_array_range_type (char_type
, lowbound
, highbound
+ lowbound
- 1);
1652 val
= allocate_value (stringtype
);
1653 memcpy (value_contents_raw (val
), ptr
, len
);
1657 /* Create a value for a string constant by allocating space in the
1658 inferior, copying the data into that space, and returning the
1659 address with type TYPE_CODE_STRING. PTR points to the string
1660 constant data; LEN is number of characters.
1662 Note that string types are like array of char types with a lower
1663 bound of zero and an upper bound of LEN - 1. Also note that the
1664 string may contain embedded null bytes. */
1667 value_string (const char *ptr
, ssize_t len
, struct type
*char_type
)
1670 int lowbound
= current_language
->string_lower_bound
;
1671 ssize_t highbound
= len
/ TYPE_LENGTH (char_type
);
1672 struct type
*stringtype
1673 = lookup_string_range_type (char_type
, lowbound
, highbound
+ lowbound
- 1);
1675 val
= allocate_value (stringtype
);
1676 memcpy (value_contents_raw (val
), ptr
, len
);
1681 /* See if we can pass arguments in T2 to a function which takes
1682 arguments of types T1. T1 is a list of NARGS arguments, and T2 is
1683 a NULL-terminated vector. If some arguments need coercion of some
1684 sort, then the coerced values are written into T2. Return value is
1685 0 if the arguments could be matched, or the position at which they
1688 STATICP is nonzero if the T1 argument list came from a static
1689 member function. T2 will still include the ``this'' pointer, but
1692 For non-static member functions, we ignore the first argument,
1693 which is the type of the instance variable. This is because we
1694 want to handle calls with objects from derived classes. This is
1695 not entirely correct: we should actually check to make sure that a
1696 requested operation is type secure, shouldn't we? FIXME. */
1699 typecmp (int staticp
, int varargs
, int nargs
,
1700 struct field t1
[], struct value
*t2
[])
1705 internal_error (__FILE__
, __LINE__
,
1706 _("typecmp: no argument list"));
1708 /* Skip ``this'' argument if applicable. T2 will always include
1714 (i
< nargs
) && TYPE_CODE (t1
[i
].type
) != TYPE_CODE_VOID
;
1717 struct type
*tt1
, *tt2
;
1722 tt1
= check_typedef (t1
[i
].type
);
1723 tt2
= check_typedef (value_type (t2
[i
]));
1725 if (TYPE_IS_REFERENCE (tt1
)
1726 /* We should be doing hairy argument matching, as below. */
1727 && (TYPE_CODE (check_typedef (TYPE_TARGET_TYPE (tt1
)))
1728 == TYPE_CODE (tt2
)))
1730 if (TYPE_CODE (tt2
) == TYPE_CODE_ARRAY
)
1731 t2
[i
] = value_coerce_array (t2
[i
]);
1733 t2
[i
] = value_ref (t2
[i
], TYPE_CODE (tt1
));
1737 /* djb - 20000715 - Until the new type structure is in the
1738 place, and we can attempt things like implicit conversions,
1739 we need to do this so you can take something like a map<const
1740 char *>, and properly access map["hello"], because the
1741 argument to [] will be a reference to a pointer to a char,
1742 and the argument will be a pointer to a char. */
1743 while (TYPE_IS_REFERENCE (tt1
) || TYPE_CODE (tt1
) == TYPE_CODE_PTR
)
1745 tt1
= check_typedef( TYPE_TARGET_TYPE(tt1
) );
1747 while (TYPE_CODE(tt2
) == TYPE_CODE_ARRAY
1748 || TYPE_CODE(tt2
) == TYPE_CODE_PTR
1749 || TYPE_IS_REFERENCE (tt2
))
1751 tt2
= check_typedef (TYPE_TARGET_TYPE(tt2
));
1753 if (TYPE_CODE (tt1
) == TYPE_CODE (tt2
))
1755 /* Array to pointer is a `trivial conversion' according to the
1758 /* We should be doing much hairier argument matching (see
1759 section 13.2 of the ARM), but as a quick kludge, just check
1760 for the same type code. */
1761 if (TYPE_CODE (t1
[i
].type
) != TYPE_CODE (value_type (t2
[i
])))
1764 if (varargs
|| t2
[i
] == NULL
)
1769 /* Helper class for do_search_struct_field that updates *RESULT_PTR
1770 and *LAST_BOFFSET, and possibly throws an exception if the field
1771 search has yielded ambiguous results. */
1774 update_search_result (struct value
**result_ptr
, struct value
*v
,
1775 LONGEST
*last_boffset
, LONGEST boffset
,
1776 const char *name
, struct type
*type
)
1780 if (*result_ptr
!= NULL
1781 /* The result is not ambiguous if all the classes that are
1782 found occupy the same space. */
1783 && *last_boffset
!= boffset
)
1784 error (_("base class '%s' is ambiguous in type '%s'"),
1785 name
, TYPE_SAFE_NAME (type
));
1787 *last_boffset
= boffset
;
1791 /* A helper for search_struct_field. This does all the work; most
1792 arguments are as passed to search_struct_field. The result is
1793 stored in *RESULT_PTR, which must be initialized to NULL.
1794 OUTERMOST_TYPE is the type of the initial type passed to
1795 search_struct_field; this is used for error reporting when the
1796 lookup is ambiguous. */
1799 do_search_struct_field (const char *name
, struct value
*arg1
, LONGEST offset
,
1800 struct type
*type
, int looking_for_baseclass
,
1801 struct value
**result_ptr
,
1802 LONGEST
*last_boffset
,
1803 struct type
*outermost_type
)
1808 type
= check_typedef (type
);
1809 nbases
= TYPE_N_BASECLASSES (type
);
1811 if (!looking_for_baseclass
)
1812 for (i
= TYPE_NFIELDS (type
) - 1; i
>= nbases
; i
--)
1814 const char *t_field_name
= TYPE_FIELD_NAME (type
, i
);
1816 if (t_field_name
&& (strcmp_iw (t_field_name
, name
) == 0))
1820 if (field_is_static (&TYPE_FIELD (type
, i
)))
1821 v
= value_static_field (type
, i
);
1823 v
= value_primitive_field (arg1
, offset
, i
, type
);
1829 && t_field_name
[0] == '\0')
1831 struct type
*field_type
= TYPE_FIELD_TYPE (type
, i
);
1833 if (TYPE_CODE (field_type
) == TYPE_CODE_UNION
1834 || TYPE_CODE (field_type
) == TYPE_CODE_STRUCT
)
1836 /* Look for a match through the fields of an anonymous
1837 union, or anonymous struct. C++ provides anonymous
1840 In the GNU Chill (now deleted from GDB)
1841 implementation of variant record types, each
1842 <alternative field> has an (anonymous) union type,
1843 each member of the union represents a <variant
1844 alternative>. Each <variant alternative> is
1845 represented as a struct, with a member for each
1848 struct value
*v
= NULL
;
1849 LONGEST new_offset
= offset
;
1851 /* This is pretty gross. In G++, the offset in an
1852 anonymous union is relative to the beginning of the
1853 enclosing struct. In the GNU Chill (now deleted
1854 from GDB) implementation of variant records, the
1855 bitpos is zero in an anonymous union field, so we
1856 have to add the offset of the union here. */
1857 if (TYPE_CODE (field_type
) == TYPE_CODE_STRUCT
1858 || (TYPE_NFIELDS (field_type
) > 0
1859 && TYPE_FIELD_BITPOS (field_type
, 0) == 0))
1860 new_offset
+= TYPE_FIELD_BITPOS (type
, i
) / 8;
1862 do_search_struct_field (name
, arg1
, new_offset
,
1864 looking_for_baseclass
, &v
,
1876 for (i
= 0; i
< nbases
; i
++)
1878 struct value
*v
= NULL
;
1879 struct type
*basetype
= check_typedef (TYPE_BASECLASS (type
, i
));
1880 /* If we are looking for baseclasses, this is what we get when
1881 we hit them. But it could happen that the base part's member
1882 name is not yet filled in. */
1883 int found_baseclass
= (looking_for_baseclass
1884 && TYPE_BASECLASS_NAME (type
, i
) != NULL
1885 && (strcmp_iw (name
,
1886 TYPE_BASECLASS_NAME (type
,
1888 LONGEST boffset
= value_embedded_offset (arg1
) + offset
;
1890 if (BASETYPE_VIA_VIRTUAL (type
, i
))
1894 boffset
= baseclass_offset (type
, i
,
1895 value_contents_for_printing (arg1
),
1896 value_embedded_offset (arg1
) + offset
,
1897 value_address (arg1
),
1900 /* The virtual base class pointer might have been clobbered
1901 by the user program. Make sure that it still points to a
1902 valid memory location. */
1904 boffset
+= value_embedded_offset (arg1
) + offset
;
1906 || boffset
>= TYPE_LENGTH (value_enclosing_type (arg1
)))
1908 CORE_ADDR base_addr
;
1910 base_addr
= value_address (arg1
) + boffset
;
1911 v2
= value_at_lazy (basetype
, base_addr
);
1912 if (target_read_memory (base_addr
,
1913 value_contents_raw (v2
),
1914 TYPE_LENGTH (value_type (v2
))) != 0)
1915 error (_("virtual baseclass botch"));
1919 v2
= value_copy (arg1
);
1920 deprecated_set_value_type (v2
, basetype
);
1921 set_value_embedded_offset (v2
, boffset
);
1924 if (found_baseclass
)
1928 do_search_struct_field (name
, v2
, 0,
1929 TYPE_BASECLASS (type
, i
),
1930 looking_for_baseclass
,
1931 result_ptr
, last_boffset
,
1935 else if (found_baseclass
)
1936 v
= value_primitive_field (arg1
, offset
, i
, type
);
1939 do_search_struct_field (name
, arg1
,
1940 offset
+ TYPE_BASECLASS_BITPOS (type
,
1942 basetype
, looking_for_baseclass
,
1943 result_ptr
, last_boffset
,
1947 update_search_result (result_ptr
, v
, last_boffset
,
1948 boffset
, name
, outermost_type
);
1952 /* Helper function used by value_struct_elt to recurse through
1953 baseclasses. Look for a field NAME in ARG1. Search in it assuming
1954 it has (class) type TYPE. If found, return value, else return NULL.
1956 If LOOKING_FOR_BASECLASS, then instead of looking for struct
1957 fields, look for a baseclass named NAME. */
1959 static struct value
*
1960 search_struct_field (const char *name
, struct value
*arg1
,
1961 struct type
*type
, int looking_for_baseclass
)
1963 struct value
*result
= NULL
;
1964 LONGEST boffset
= 0;
1966 do_search_struct_field (name
, arg1
, 0, type
, looking_for_baseclass
,
1967 &result
, &boffset
, type
);
1971 /* Helper function used by value_struct_elt to recurse through
1972 baseclasses. Look for a field NAME in ARG1. Adjust the address of
1973 ARG1 by OFFSET bytes, and search in it assuming it has (class) type
1976 If found, return value, else if name matched and args not return
1977 (value) -1, else return NULL. */
1979 static struct value
*
1980 search_struct_method (const char *name
, struct value
**arg1p
,
1981 struct value
**args
, LONGEST offset
,
1982 int *static_memfuncp
, struct type
*type
)
1986 int name_matched
= 0;
1987 char dem_opname
[64];
1989 type
= check_typedef (type
);
1990 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; i
--)
1992 const char *t_field_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
1994 /* FIXME! May need to check for ARM demangling here. */
1995 if (startswith (t_field_name
, "__") ||
1996 startswith (t_field_name
, "op") ||
1997 startswith (t_field_name
, "type"))
1999 if (cplus_demangle_opname (t_field_name
, dem_opname
, DMGL_ANSI
))
2000 t_field_name
= dem_opname
;
2001 else if (cplus_demangle_opname (t_field_name
, dem_opname
, 0))
2002 t_field_name
= dem_opname
;
2004 if (t_field_name
&& (strcmp_iw (t_field_name
, name
) == 0))
2006 int j
= TYPE_FN_FIELDLIST_LENGTH (type
, i
) - 1;
2007 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
2010 check_stub_method_group (type
, i
);
2011 if (j
> 0 && args
== 0)
2012 error (_("cannot resolve overloaded method "
2013 "`%s': no arguments supplied"), name
);
2014 else if (j
== 0 && args
== 0)
2016 v
= value_fn_field (arg1p
, f
, j
, type
, offset
);
2023 if (!typecmp (TYPE_FN_FIELD_STATIC_P (f
, j
),
2024 TYPE_VARARGS (TYPE_FN_FIELD_TYPE (f
, j
)),
2025 TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f
, j
)),
2026 TYPE_FN_FIELD_ARGS (f
, j
), args
))
2028 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
2029 return value_virtual_fn_field (arg1p
, f
, j
,
2031 if (TYPE_FN_FIELD_STATIC_P (f
, j
)
2033 *static_memfuncp
= 1;
2034 v
= value_fn_field (arg1p
, f
, j
, type
, offset
);
2043 for (i
= TYPE_N_BASECLASSES (type
) - 1; i
>= 0; i
--)
2045 LONGEST base_offset
;
2046 LONGEST this_offset
;
2048 if (BASETYPE_VIA_VIRTUAL (type
, i
))
2050 struct type
*baseclass
= check_typedef (TYPE_BASECLASS (type
, i
));
2051 struct value
*base_val
;
2052 const gdb_byte
*base_valaddr
;
2054 /* The virtual base class pointer might have been
2055 clobbered by the user program. Make sure that it
2056 still points to a valid memory location. */
2058 if (offset
< 0 || offset
>= TYPE_LENGTH (type
))
2062 gdb::byte_vector
tmp (TYPE_LENGTH (baseclass
));
2063 address
= value_address (*arg1p
);
2065 if (target_read_memory (address
+ offset
,
2066 tmp
.data (), TYPE_LENGTH (baseclass
)) != 0)
2067 error (_("virtual baseclass botch"));
2069 base_val
= value_from_contents_and_address (baseclass
,
2072 base_valaddr
= value_contents_for_printing (base_val
);
2078 base_valaddr
= value_contents_for_printing (*arg1p
);
2079 this_offset
= offset
;
2082 base_offset
= baseclass_offset (type
, i
, base_valaddr
,
2083 this_offset
, value_address (base_val
),
2088 base_offset
= TYPE_BASECLASS_BITPOS (type
, i
) / 8;
2090 v
= search_struct_method (name
, arg1p
, args
, base_offset
+ offset
,
2091 static_memfuncp
, TYPE_BASECLASS (type
, i
));
2092 if (v
== (struct value
*) - 1)
2098 /* FIXME-bothner: Why is this commented out? Why is it here? */
2099 /* *arg1p = arg1_tmp; */
2104 return (struct value
*) - 1;
2109 /* Given *ARGP, a value of type (pointer to a)* structure/union,
2110 extract the component named NAME from the ultimate target
2111 structure/union and return it as a value with its appropriate type.
2112 ERR is used in the error message if *ARGP's type is wrong.
2114 C++: ARGS is a list of argument types to aid in the selection of
2115 an appropriate method. Also, handle derived types.
2117 STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
2118 where the truthvalue of whether the function that was resolved was
2119 a static member function or not is stored.
2121 ERR is an error message to be printed in case the field is not
2125 value_struct_elt (struct value
**argp
, struct value
**args
,
2126 const char *name
, int *static_memfuncp
, const char *err
)
2131 *argp
= coerce_array (*argp
);
2133 t
= check_typedef (value_type (*argp
));
2135 /* Follow pointers until we get to a non-pointer. */
2137 while (TYPE_CODE (t
) == TYPE_CODE_PTR
|| TYPE_IS_REFERENCE (t
))
2139 *argp
= value_ind (*argp
);
2140 /* Don't coerce fn pointer to fn and then back again! */
2141 if (TYPE_CODE (check_typedef (value_type (*argp
))) != TYPE_CODE_FUNC
)
2142 *argp
= coerce_array (*argp
);
2143 t
= check_typedef (value_type (*argp
));
2146 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
2147 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
2148 error (_("Attempt to extract a component of a value that is not a %s."),
2151 /* Assume it's not, unless we see that it is. */
2152 if (static_memfuncp
)
2153 *static_memfuncp
= 0;
2157 /* if there are no arguments ...do this... */
2159 /* Try as a field first, because if we succeed, there is less
2161 v
= search_struct_field (name
, *argp
, t
, 0);
2165 /* C++: If it was not found as a data field, then try to
2166 return it as a pointer to a method. */
2167 v
= search_struct_method (name
, argp
, args
, 0,
2168 static_memfuncp
, t
);
2170 if (v
== (struct value
*) - 1)
2171 error (_("Cannot take address of method %s."), name
);
2174 if (TYPE_NFN_FIELDS (t
))
2175 error (_("There is no member or method named %s."), name
);
2177 error (_("There is no member named %s."), name
);
2182 v
= search_struct_method (name
, argp
, args
, 0,
2183 static_memfuncp
, t
);
2185 if (v
== (struct value
*) - 1)
2187 error (_("One of the arguments you tried to pass to %s could not "
2188 "be converted to what the function wants."), name
);
2192 /* See if user tried to invoke data as function. If so, hand it
2193 back. If it's not callable (i.e., a pointer to function),
2194 gdb should give an error. */
2195 v
= search_struct_field (name
, *argp
, t
, 0);
2196 /* If we found an ordinary field, then it is not a method call.
2197 So, treat it as if it were a static member function. */
2198 if (v
&& static_memfuncp
)
2199 *static_memfuncp
= 1;
2203 throw_error (NOT_FOUND_ERROR
,
2204 _("Structure has no component named %s."), name
);
2208 /* Given *ARGP, a value of type structure or union, or a pointer/reference
2209 to a structure or union, extract and return its component (field) of
2210 type FTYPE at the specified BITPOS.
2211 Throw an exception on error. */
2214 value_struct_elt_bitpos (struct value
**argp
, int bitpos
, struct type
*ftype
,
2220 *argp
= coerce_array (*argp
);
2222 t
= check_typedef (value_type (*argp
));
2224 while (TYPE_CODE (t
) == TYPE_CODE_PTR
|| TYPE_IS_REFERENCE (t
))
2226 *argp
= value_ind (*argp
);
2227 if (TYPE_CODE (check_typedef (value_type (*argp
))) != TYPE_CODE_FUNC
)
2228 *argp
= coerce_array (*argp
);
2229 t
= check_typedef (value_type (*argp
));
2232 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
2233 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
2234 error (_("Attempt to extract a component of a value that is not a %s."),
2237 for (i
= TYPE_N_BASECLASSES (t
); i
< TYPE_NFIELDS (t
); i
++)
2239 if (!field_is_static (&TYPE_FIELD (t
, i
))
2240 && bitpos
== TYPE_FIELD_BITPOS (t
, i
)
2241 && types_equal (ftype
, TYPE_FIELD_TYPE (t
, i
)))
2242 return value_primitive_field (*argp
, 0, i
, t
);
2245 error (_("No field with matching bitpos and type."));
2254 value_union_variant (struct type
*union_type
, const gdb_byte
*contents
)
2256 gdb_assert (TYPE_CODE (union_type
) == TYPE_CODE_UNION
2257 && TYPE_FLAG_DISCRIMINATED_UNION (union_type
));
2259 struct dynamic_prop
*discriminant_prop
2260 = get_dyn_prop (DYN_PROP_DISCRIMINATED
, union_type
);
2261 gdb_assert (discriminant_prop
!= nullptr);
2263 struct discriminant_info
*info
2264 = (struct discriminant_info
*) discriminant_prop
->data
.baton
;
2265 gdb_assert (info
!= nullptr);
2267 /* If this is a univariant union, just return the sole field. */
2268 if (TYPE_NFIELDS (union_type
) == 1)
2270 /* This should only happen for univariants, which we already dealt
2272 gdb_assert (info
->discriminant_index
!= -1);
2274 /* Compute the discriminant. Note that unpack_field_as_long handles
2275 sign extension when necessary, as does the DWARF reader -- so
2276 signed discriminants will be handled correctly despite the use of
2277 an unsigned type here. */
2278 ULONGEST discriminant
= unpack_field_as_long (union_type
, contents
,
2279 info
->discriminant_index
);
2281 for (int i
= 0; i
< TYPE_NFIELDS (union_type
); ++i
)
2283 if (i
!= info
->default_index
2284 && i
!= info
->discriminant_index
2285 && discriminant
== info
->discriminants
[i
])
2289 if (info
->default_index
== -1)
2290 error (_("Could not find variant corresponding to discriminant %s"),
2291 pulongest (discriminant
));
2292 return info
->default_index
;
2295 /* Search through the methods of an object (and its bases) to find a
2296 specified method. Return the pointer to the fn_field list FN_LIST of
2297 overloaded instances defined in the source language. If available
2298 and matching, a vector of matching xmethods defined in extension
2299 languages are also returned in XM_WORKER_VEC
2301 Helper function for value_find_oload_list.
2302 ARGP is a pointer to a pointer to a value (the object).
2303 METHOD is a string containing the method name.
2304 OFFSET is the offset within the value.
2305 TYPE is the assumed type of the object.
2306 FN_LIST is the pointer to matching overloaded instances defined in
2307 source language. Since this is a recursive function, *FN_LIST
2308 should be set to NULL when calling this function.
2309 NUM_FNS is the number of overloaded instances. *NUM_FNS should be set to
2310 0 when calling this function.
2311 XM_WORKER_VEC is the vector of matching xmethod workers. *XM_WORKER_VEC
2312 should also be set to NULL when calling this function.
2313 BASETYPE is set to the actual type of the subobject where the
2315 BOFFSET is the offset of the base subobject where the method is found. */
2318 find_method_list (struct value
**argp
, const char *method
,
2319 LONGEST offset
, struct type
*type
,
2320 struct fn_field
**fn_list
, int *num_fns
,
2321 std::vector
<xmethod_worker_up
> *xm_worker_vec
,
2322 struct type
**basetype
, LONGEST
*boffset
)
2325 struct fn_field
*f
= NULL
;
2327 gdb_assert (fn_list
!= NULL
&& xm_worker_vec
!= NULL
);
2328 type
= check_typedef (type
);
2330 /* First check in object itself.
2331 This function is called recursively to search through base classes.
2332 If there is a source method match found at some stage, then we need not
2333 look for source methods in consequent recursive calls. */
2334 if ((*fn_list
) == NULL
)
2336 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; i
--)
2338 /* pai: FIXME What about operators and type conversions? */
2339 const char *fn_field_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
2341 if (fn_field_name
&& (strcmp_iw (fn_field_name
, method
) == 0))
2343 int len
= TYPE_FN_FIELDLIST_LENGTH (type
, i
);
2344 f
= TYPE_FN_FIELDLIST1 (type
, i
);
2351 /* Resolve any stub methods. */
2352 check_stub_method_group (type
, i
);
2359 /* Unlike source methods, xmethods can be accumulated over successive
2360 recursive calls. In other words, an xmethod named 'm' in a class
2361 will not hide an xmethod named 'm' in its base class(es). We want
2362 it to be this way because xmethods are after all convenience functions
2363 and hence there is no point restricting them with something like method
2364 hiding. Moreover, if hiding is done for xmethods as well, then we will
2365 have to provide a mechanism to un-hide (like the 'using' construct). */
2366 get_matching_xmethod_workers (type
, method
, xm_worker_vec
);
2368 /* If source methods are not found in current class, look for them in the
2369 base classes. We also have to go through the base classes to gather
2370 extension methods. */
2371 for (i
= TYPE_N_BASECLASSES (type
) - 1; i
>= 0; i
--)
2373 LONGEST base_offset
;
2375 if (BASETYPE_VIA_VIRTUAL (type
, i
))
2377 base_offset
= baseclass_offset (type
, i
,
2378 value_contents_for_printing (*argp
),
2379 value_offset (*argp
) + offset
,
2380 value_address (*argp
), *argp
);
2382 else /* Non-virtual base, simply use bit position from debug
2385 base_offset
= TYPE_BASECLASS_BITPOS (type
, i
) / 8;
2388 find_method_list (argp
, method
, base_offset
+ offset
,
2389 TYPE_BASECLASS (type
, i
), fn_list
, num_fns
,
2390 xm_worker_vec
, basetype
, boffset
);
2394 /* Return the list of overloaded methods of a specified name. The methods
2395 could be those GDB finds in the binary, or xmethod. Methods found in
2396 the binary are returned in FN_LIST, and xmethods are returned in
2399 ARGP is a pointer to a pointer to a value (the object).
2400 METHOD is the method name.
2401 OFFSET is the offset within the value contents.
2402 FN_LIST is the pointer to matching overloaded instances defined in
2404 NUM_FNS is the number of overloaded instances.
2405 XM_WORKER_VEC is the vector of matching xmethod workers defined in
2406 extension languages.
2407 BASETYPE is set to the type of the base subobject that defines the
2409 BOFFSET is the offset of the base subobject which defines the method. */
2412 value_find_oload_method_list (struct value
**argp
, const char *method
,
2413 LONGEST offset
, struct fn_field
**fn_list
,
2415 std::vector
<xmethod_worker_up
> *xm_worker_vec
,
2416 struct type
**basetype
, LONGEST
*boffset
)
2420 t
= check_typedef (value_type (*argp
));
2422 /* Code snarfed from value_struct_elt. */
2423 while (TYPE_CODE (t
) == TYPE_CODE_PTR
|| TYPE_IS_REFERENCE (t
))
2425 *argp
= value_ind (*argp
);
2426 /* Don't coerce fn pointer to fn and then back again! */
2427 if (TYPE_CODE (check_typedef (value_type (*argp
))) != TYPE_CODE_FUNC
)
2428 *argp
= coerce_array (*argp
);
2429 t
= check_typedef (value_type (*argp
));
2432 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
2433 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
2434 error (_("Attempt to extract a component of a "
2435 "value that is not a struct or union"));
2437 gdb_assert (fn_list
!= NULL
&& xm_worker_vec
!= NULL
);
2439 /* Clear the lists. */
2442 xm_worker_vec
->clear ();
2444 find_method_list (argp
, method
, 0, t
, fn_list
, num_fns
, xm_worker_vec
,
2448 /* Given an array of arguments (ARGS) (which includes an entry for
2449 "this" in the case of C++ methods), the NAME of a function, and
2450 whether it's a method or not (METHOD), find the best function that
2451 matches on the argument types according to the overload resolution
2454 METHOD can be one of three values:
2455 NON_METHOD for non-member functions.
2456 METHOD: for member functions.
2457 BOTH: used for overload resolution of operators where the
2458 candidates are expected to be either member or non member
2459 functions. In this case the first argument ARGTYPES
2460 (representing 'this') is expected to be a reference to the
2461 target object, and will be dereferenced when attempting the
2464 In the case of class methods, the parameter OBJ is an object value
2465 in which to search for overloaded methods.
2467 In the case of non-method functions, the parameter FSYM is a symbol
2468 corresponding to one of the overloaded functions.
2470 Return value is an integer: 0 -> good match, 10 -> debugger applied
2471 non-standard coercions, 100 -> incompatible.
2473 If a method is being searched for, VALP will hold the value.
2474 If a non-method is being searched for, SYMP will hold the symbol
2477 If a method is being searched for, and it is a static method,
2478 then STATICP will point to a non-zero value.
2480 If NO_ADL argument dependent lookup is disabled. This is used to prevent
2481 ADL overload candidates when performing overload resolution for a fully
2484 If NOSIDE is EVAL_AVOID_SIDE_EFFECTS, then OBJP's memory cannot be
2485 read while picking the best overload match (it may be all zeroes and thus
2486 not have a vtable pointer), in which case skip virtual function lookup.
2487 This is ok as typically EVAL_AVOID_SIDE_EFFECTS is only used to determine
2490 Note: This function does *not* check the value of
2491 overload_resolution. Caller must check it to see whether overload
2492 resolution is permitted. */
2495 find_overload_match (gdb::array_view
<value
*> args
,
2496 const char *name
, enum oload_search_type method
,
2497 struct value
**objp
, struct symbol
*fsym
,
2498 struct value
**valp
, struct symbol
**symp
,
2499 int *staticp
, const int no_adl
,
2500 const enum noside noside
)
2502 struct value
*obj
= (objp
? *objp
: NULL
);
2503 struct type
*obj_type
= obj
? value_type (obj
) : NULL
;
2504 /* Index of best overloaded function. */
2505 int func_oload_champ
= -1;
2506 int method_oload_champ
= -1;
2507 int src_method_oload_champ
= -1;
2508 int ext_method_oload_champ
= -1;
2510 /* The measure for the current best match. */
2511 struct badness_vector
*method_badness
= NULL
;
2512 struct badness_vector
*func_badness
= NULL
;
2513 struct badness_vector
*ext_method_badness
= NULL
;
2514 struct badness_vector
*src_method_badness
= NULL
;
2516 struct value
*temp
= obj
;
2517 /* For methods, the list of overloaded methods. */
2518 struct fn_field
*fns_ptr
= NULL
;
2519 /* For non-methods, the list of overloaded function symbols. */
2520 std::vector
<symbol
*> oload_syms
;
2521 /* For xmethods, the vector of xmethod workers. */
2522 std::vector
<xmethod_worker_up
> xm_worker_vec
;
2523 /* Number of overloaded instances being considered. */
2525 struct type
*basetype
= NULL
;
2528 struct cleanup
*all_cleanups
= make_cleanup (null_cleanup
, NULL
);
2530 const char *obj_type_name
= NULL
;
2531 const char *func_name
= NULL
;
2532 enum oload_classification match_quality
;
2533 enum oload_classification method_match_quality
= INCOMPATIBLE
;
2534 enum oload_classification src_method_match_quality
= INCOMPATIBLE
;
2535 enum oload_classification ext_method_match_quality
= INCOMPATIBLE
;
2536 enum oload_classification func_match_quality
= INCOMPATIBLE
;
2538 /* Get the list of overloaded methods or functions. */
2539 if (method
== METHOD
|| method
== BOTH
)
2543 /* OBJ may be a pointer value rather than the object itself. */
2544 obj
= coerce_ref (obj
);
2545 while (TYPE_CODE (check_typedef (value_type (obj
))) == TYPE_CODE_PTR
)
2546 obj
= coerce_ref (value_ind (obj
));
2547 obj_type_name
= TYPE_NAME (value_type (obj
));
2549 /* First check whether this is a data member, e.g. a pointer to
2551 if (TYPE_CODE (check_typedef (value_type (obj
))) == TYPE_CODE_STRUCT
)
2553 *valp
= search_struct_field (name
, obj
,
2554 check_typedef (value_type (obj
)), 0);
2558 do_cleanups (all_cleanups
);
2563 /* Retrieve the list of methods with the name NAME. */
2564 value_find_oload_method_list (&temp
, name
, 0, &fns_ptr
, &num_fns
,
2565 &xm_worker_vec
, &basetype
, &boffset
);
2566 /* If this is a method only search, and no methods were found
2567 the search has failed. */
2568 if (method
== METHOD
&& (!fns_ptr
|| !num_fns
) && xm_worker_vec
.empty ())
2569 error (_("Couldn't find method %s%s%s"),
2571 (obj_type_name
&& *obj_type_name
) ? "::" : "",
2573 /* If we are dealing with stub method types, they should have
2574 been resolved by find_method_list via
2575 value_find_oload_method_list above. */
2578 gdb_assert (TYPE_SELF_TYPE (fns_ptr
[0].type
) != NULL
);
2580 src_method_oload_champ
= find_oload_champ (args
,
2581 num_fns
, fns_ptr
, NULL
,
2582 NULL
, &src_method_badness
);
2584 src_method_match_quality
= classify_oload_match
2585 (src_method_badness
, args
.size (),
2586 oload_method_static_p (fns_ptr
, src_method_oload_champ
));
2588 make_cleanup (xfree
, src_method_badness
);
2591 if (!xm_worker_vec
.empty ())
2593 ext_method_oload_champ
= find_oload_champ (args
, 0, NULL
, &xm_worker_vec
,
2594 NULL
, &ext_method_badness
);
2595 ext_method_match_quality
= classify_oload_match (ext_method_badness
,
2597 make_cleanup (xfree
, ext_method_badness
);
2600 if (src_method_oload_champ
>= 0 && ext_method_oload_champ
>= 0)
2602 switch (compare_badness (ext_method_badness
, src_method_badness
))
2604 case 0: /* Src method and xmethod are equally good. */
2605 /* If src method and xmethod are equally good, then
2606 xmethod should be the winner. Hence, fall through to the
2607 case where a xmethod is better than the source
2608 method, except when the xmethod match quality is
2611 case 1: /* Src method and ext method are incompatible. */
2612 /* If ext method match is not standard, then let source method
2613 win. Otherwise, fallthrough to let xmethod win. */
2614 if (ext_method_match_quality
!= STANDARD
)
2616 method_oload_champ
= src_method_oload_champ
;
2617 method_badness
= src_method_badness
;
2618 ext_method_oload_champ
= -1;
2619 method_match_quality
= src_method_match_quality
;
2623 case 2: /* Ext method is champion. */
2624 method_oload_champ
= ext_method_oload_champ
;
2625 method_badness
= ext_method_badness
;
2626 src_method_oload_champ
= -1;
2627 method_match_quality
= ext_method_match_quality
;
2629 case 3: /* Src method is champion. */
2630 method_oload_champ
= src_method_oload_champ
;
2631 method_badness
= src_method_badness
;
2632 ext_method_oload_champ
= -1;
2633 method_match_quality
= src_method_match_quality
;
2636 gdb_assert_not_reached ("Unexpected overload comparison "
2641 else if (src_method_oload_champ
>= 0)
2643 method_oload_champ
= src_method_oload_champ
;
2644 method_badness
= src_method_badness
;
2645 method_match_quality
= src_method_match_quality
;
2647 else if (ext_method_oload_champ
>= 0)
2649 method_oload_champ
= ext_method_oload_champ
;
2650 method_badness
= ext_method_badness
;
2651 method_match_quality
= ext_method_match_quality
;
2655 if (method
== NON_METHOD
|| method
== BOTH
)
2657 const char *qualified_name
= NULL
;
2659 /* If the overload match is being search for both as a method
2660 and non member function, the first argument must now be
2663 args
[0] = value_ind (args
[0]);
2667 qualified_name
= SYMBOL_NATURAL_NAME (fsym
);
2669 /* If we have a function with a C++ name, try to extract just
2670 the function part. Do not try this for non-functions (e.g.
2671 function pointers). */
2673 && TYPE_CODE (check_typedef (SYMBOL_TYPE (fsym
)))
2678 temp_func
= cp_func_name (qualified_name
);
2680 /* If cp_func_name did not remove anything, the name of the
2681 symbol did not include scope or argument types - it was
2682 probably a C-style function. */
2685 make_cleanup (xfree
, temp_func
);
2686 if (strcmp (temp_func
, qualified_name
) == 0)
2689 func_name
= temp_func
;
2696 qualified_name
= name
;
2699 /* If there was no C++ name, this must be a C-style function or
2700 not a function at all. Just return the same symbol. Do the
2701 same if cp_func_name fails for some reason. */
2702 if (func_name
== NULL
)
2705 do_cleanups (all_cleanups
);
2709 func_oload_champ
= find_oload_champ_namespace (args
,
2716 if (func_oload_champ
>= 0)
2717 func_match_quality
= classify_oload_match (func_badness
,
2720 make_cleanup (xfree
, func_badness
);
2723 /* Did we find a match ? */
2724 if (method_oload_champ
== -1 && func_oload_champ
== -1)
2725 throw_error (NOT_FOUND_ERROR
,
2726 _("No symbol \"%s\" in current context."),
2729 /* If we have found both a method match and a function
2730 match, find out which one is better, and calculate match
2732 if (method_oload_champ
>= 0 && func_oload_champ
>= 0)
2734 switch (compare_badness (func_badness
, method_badness
))
2736 case 0: /* Top two contenders are equally good. */
2737 /* FIXME: GDB does not support the general ambiguous case.
2738 All candidates should be collected and presented the
2740 error (_("Ambiguous overload resolution"));
2742 case 1: /* Incomparable top contenders. */
2743 /* This is an error incompatible candidates
2744 should not have been proposed. */
2745 error (_("Internal error: incompatible "
2746 "overload candidates proposed"));
2748 case 2: /* Function champion. */
2749 method_oload_champ
= -1;
2750 match_quality
= func_match_quality
;
2752 case 3: /* Method champion. */
2753 func_oload_champ
= -1;
2754 match_quality
= method_match_quality
;
2757 error (_("Internal error: unexpected overload comparison result"));
2763 /* We have either a method match or a function match. */
2764 if (method_oload_champ
>= 0)
2765 match_quality
= method_match_quality
;
2767 match_quality
= func_match_quality
;
2770 if (match_quality
== INCOMPATIBLE
)
2772 if (method
== METHOD
)
2773 error (_("Cannot resolve method %s%s%s to any overloaded instance"),
2775 (obj_type_name
&& *obj_type_name
) ? "::" : "",
2778 error (_("Cannot resolve function %s to any overloaded instance"),
2781 else if (match_quality
== NON_STANDARD
)
2783 if (method
== METHOD
)
2784 warning (_("Using non-standard conversion to match "
2785 "method %s%s%s to supplied arguments"),
2787 (obj_type_name
&& *obj_type_name
) ? "::" : "",
2790 warning (_("Using non-standard conversion to match "
2791 "function %s to supplied arguments"),
2795 if (staticp
!= NULL
)
2796 *staticp
= oload_method_static_p (fns_ptr
, method_oload_champ
);
2798 if (method_oload_champ
>= 0)
2800 if (src_method_oload_champ
>= 0)
2802 if (TYPE_FN_FIELD_VIRTUAL_P (fns_ptr
, method_oload_champ
)
2803 && noside
!= EVAL_AVOID_SIDE_EFFECTS
)
2805 *valp
= value_virtual_fn_field (&temp
, fns_ptr
,
2806 method_oload_champ
, basetype
,
2810 *valp
= value_fn_field (&temp
, fns_ptr
, method_oload_champ
,
2814 *valp
= value_from_xmethod
2815 (std::move (xm_worker_vec
[ext_method_oload_champ
]));
2818 *symp
= oload_syms
[func_oload_champ
];
2822 struct type
*temp_type
= check_typedef (value_type (temp
));
2823 struct type
*objtype
= check_typedef (obj_type
);
2825 if (TYPE_CODE (temp_type
) != TYPE_CODE_PTR
2826 && (TYPE_CODE (objtype
) == TYPE_CODE_PTR
2827 || TYPE_IS_REFERENCE (objtype
)))
2829 temp
= value_addr (temp
);
2834 do_cleanups (all_cleanups
);
2836 switch (match_quality
)
2842 default: /* STANDARD */
2847 /* Find the best overload match, searching for FUNC_NAME in namespaces
2848 contained in QUALIFIED_NAME until it either finds a good match or
2849 runs out of namespaces. It stores the overloaded functions in
2850 *OLOAD_SYMS, and the badness vector in *OLOAD_CHAMP_BV. The
2851 calling function is responsible for freeing *OLOAD_SYMS and
2852 *OLOAD_CHAMP_BV. If NO_ADL, argument dependent lookup is not
2856 find_oload_champ_namespace (gdb::array_view
<value
*> args
,
2857 const char *func_name
,
2858 const char *qualified_name
,
2859 std::vector
<symbol
*> *oload_syms
,
2860 struct badness_vector
**oload_champ_bv
,
2865 find_oload_champ_namespace_loop (args
,
2868 oload_syms
, oload_champ_bv
,
2875 /* Helper function for find_oload_champ_namespace; NAMESPACE_LEN is
2876 how deep we've looked for namespaces, and the champ is stored in
2877 OLOAD_CHAMP. The return value is 1 if the champ is a good one, 0
2878 if it isn't. Other arguments are the same as in
2879 find_oload_champ_namespace
2881 It is the caller's responsibility to free *OLOAD_SYMS and
2885 find_oload_champ_namespace_loop (gdb::array_view
<value
*> args
,
2886 const char *func_name
,
2887 const char *qualified_name
,
2889 std::vector
<symbol
*> *oload_syms
,
2890 struct badness_vector
**oload_champ_bv
,
2894 int next_namespace_len
= namespace_len
;
2895 int searched_deeper
= 0;
2896 struct cleanup
*old_cleanups
;
2897 int new_oload_champ
;
2898 struct badness_vector
*new_oload_champ_bv
;
2899 char *new_namespace
;
2901 if (next_namespace_len
!= 0)
2903 gdb_assert (qualified_name
[next_namespace_len
] == ':');
2904 next_namespace_len
+= 2;
2906 next_namespace_len
+=
2907 cp_find_first_component (qualified_name
+ next_namespace_len
);
2909 /* Initialize these to values that can safely be xfree'd. */
2910 *oload_champ_bv
= NULL
;
2912 /* First, see if we have a deeper namespace we can search in.
2913 If we get a good match there, use it. */
2915 if (qualified_name
[next_namespace_len
] == ':')
2917 searched_deeper
= 1;
2919 if (find_oload_champ_namespace_loop (args
,
2920 func_name
, qualified_name
,
2922 oload_syms
, oload_champ_bv
,
2923 oload_champ
, no_adl
))
2929 /* If we reach here, either we're in the deepest namespace or we
2930 didn't find a good match in a deeper namespace. But, in the
2931 latter case, we still have a bad match in a deeper namespace;
2932 note that we might not find any match at all in the current
2933 namespace. (There's always a match in the deepest namespace,
2934 because this overload mechanism only gets called if there's a
2935 function symbol to start off with.) */
2937 old_cleanups
= make_cleanup (xfree
, *oload_champ_bv
);
2938 new_namespace
= (char *) alloca (namespace_len
+ 1);
2939 strncpy (new_namespace
, qualified_name
, namespace_len
);
2940 new_namespace
[namespace_len
] = '\0';
2942 std::vector
<symbol
*> new_oload_syms
2943 = make_symbol_overload_list (func_name
, new_namespace
);
2945 /* If we have reached the deepest level perform argument
2946 determined lookup. */
2947 if (!searched_deeper
&& !no_adl
)
2950 struct type
**arg_types
;
2952 /* Prepare list of argument types for overload resolution. */
2953 arg_types
= (struct type
**)
2954 alloca (args
.size () * (sizeof (struct type
*)));
2955 for (ix
= 0; ix
< args
.size (); ix
++)
2956 arg_types
[ix
] = value_type (args
[ix
]);
2957 add_symbol_overload_list_adl ({arg_types
, args
.size ()}, func_name
,
2961 new_oload_champ
= find_oload_champ (args
, new_oload_syms
.size (),
2962 NULL
, NULL
, new_oload_syms
.data (),
2963 &new_oload_champ_bv
);
2965 /* Case 1: We found a good match. Free earlier matches (if any),
2966 and return it. Case 2: We didn't find a good match, but we're
2967 not the deepest function. Then go with the bad match that the
2968 deeper function found. Case 3: We found a bad match, and we're
2969 the deepest function. Then return what we found, even though
2970 it's a bad match. */
2972 if (new_oload_champ
!= -1
2973 && classify_oload_match (new_oload_champ_bv
, args
.size (), 0) == STANDARD
)
2975 *oload_syms
= std::move (new_oload_syms
);
2976 *oload_champ
= new_oload_champ
;
2977 *oload_champ_bv
= new_oload_champ_bv
;
2978 do_cleanups (old_cleanups
);
2981 else if (searched_deeper
)
2983 xfree (new_oload_champ_bv
);
2984 discard_cleanups (old_cleanups
);
2989 *oload_syms
= std::move (new_oload_syms
);
2990 *oload_champ
= new_oload_champ
;
2991 *oload_champ_bv
= new_oload_champ_bv
;
2992 do_cleanups (old_cleanups
);
2997 /* Look for a function to take ARGS. Find the best match from among
2998 the overloaded methods or functions given by FNS_PTR or OLOAD_SYMS
2999 or XM_WORKER_VEC, respectively. One, and only one of FNS_PTR,
3000 OLOAD_SYMS and XM_WORKER_VEC can be non-NULL.
3002 If XM_WORKER_VEC is NULL, then the length of the arrays FNS_PTR
3003 or OLOAD_SYMS (whichever is non-NULL) is specified in NUM_FNS.
3005 Return the index of the best match; store an indication of the
3006 quality of the match in OLOAD_CHAMP_BV.
3008 It is the caller's responsibility to free *OLOAD_CHAMP_BV. */
3011 find_oload_champ (gdb::array_view
<value
*> args
,
3012 int num_fns
, struct fn_field
*fns_ptr
,
3013 const std::vector
<xmethod_worker_up
> *xm_worker_vec
,
3014 struct symbol
**oload_syms
,
3015 struct badness_vector
**oload_champ_bv
)
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 int fn_count
= xm_worker_vec
!= NULL
? xm_worker_vec
->size () : num_fns
;
3036 /* Consider each candidate in turn. */
3037 for (ix
= 0; ix
< fn_count
; ix
++)
3040 int static_offset
= 0;
3041 std::vector
<type
*> parm_types
;
3043 if (xm_worker_vec
!= NULL
)
3045 xmethod_worker
*worker
= (*xm_worker_vec
)[ix
].get ();
3046 parm_types
= worker
->get_arg_types ();
3052 if (fns_ptr
!= NULL
)
3054 nparms
= TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (fns_ptr
, ix
));
3055 static_offset
= oload_method_static_p (fns_ptr
, ix
);
3058 nparms
= TYPE_NFIELDS (SYMBOL_TYPE (oload_syms
[ix
]));
3060 parm_types
.reserve (nparms
);
3061 for (jj
= 0; jj
< nparms
; jj
++)
3063 type
*t
= (fns_ptr
!= NULL
3064 ? (TYPE_FN_FIELD_ARGS (fns_ptr
, ix
)[jj
].type
)
3065 : TYPE_FIELD_TYPE (SYMBOL_TYPE (oload_syms
[ix
]),
3067 parm_types
.push_back (t
);
3071 /* Compare parameter types to supplied argument types. Skip
3072 THIS for static methods. */
3073 bv
= rank_function (parm_types
,
3074 args
.slice (static_offset
));
3076 if (!*oload_champ_bv
)
3078 *oload_champ_bv
= bv
;
3081 else /* See whether current candidate is better or worse than
3083 switch (compare_badness (bv
, *oload_champ_bv
))
3085 case 0: /* Top two contenders are equally good. */
3086 oload_ambiguous
= 1;
3088 case 1: /* Incomparable top contenders. */
3089 oload_ambiguous
= 2;
3091 case 2: /* New champion, record details. */
3092 *oload_champ_bv
= bv
;
3093 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
, (int) parm_types
.size ());
3106 else if (xm_worker_vec
!= NULL
)
3107 fprintf_filtered (gdb_stderr
,
3108 "Xmethod worker, # of parms %d\n",
3109 (int) parm_types
.size ());
3111 fprintf_filtered (gdb_stderr
,
3112 "Overloaded function instance "
3113 "%s # of parms %d\n",
3114 SYMBOL_DEMANGLED_NAME (oload_syms
[ix
]),
3115 (int) parm_types
.size ());
3116 for (jj
= 0; jj
< args
.size () - static_offset
; jj
++)
3117 fprintf_filtered (gdb_stderr
,
3118 "...Badness @ %d : %d\n",
3119 jj
, bv
->rank
[jj
].rank
);
3120 fprintf_filtered (gdb_stderr
, "Overload resolution "
3121 "champion is %d, ambiguous? %d\n",
3122 oload_champ
, oload_ambiguous
);
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_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_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 VT, and a class type CLS, search
3318 recursively for CLS using value V; If found, store the offset
3319 which is either fetched from the virtual base pointer if CLS
3320 is virtual or accumulated offset of its parent classes if
3321 CLS is non-virtual in *BOFFS, set ISVIRT to indicate if CLS
3322 is virtual, and return true. If not found, return false. */
3325 get_baseclass_offset (struct type
*vt
, struct type
*cls
,
3326 struct value
*v
, int *boffs
, bool *isvirt
)
3328 for (int i
= 0; i
< TYPE_N_BASECLASSES (vt
); i
++)
3330 struct type
*t
= TYPE_FIELD_TYPE (vt
, i
);
3331 if (types_equal (t
, cls
))
3333 if (BASETYPE_VIA_VIRTUAL (vt
, i
))
3335 const gdb_byte
*adr
= value_contents_for_printing (v
);
3336 *boffs
= baseclass_offset (vt
, i
, adr
, value_offset (v
),
3337 value_as_long (v
), v
);
3345 if (get_baseclass_offset (check_typedef (t
), cls
, v
, boffs
, isvirt
))
3347 if (*isvirt
== false) /* Add non-virtual base offset. */
3349 const gdb_byte
*adr
= value_contents_for_printing (v
);
3350 *boffs
+= baseclass_offset (vt
, i
, adr
, value_offset (v
),
3351 value_as_long (v
), v
);
3360 /* C++: Given an aggregate type CURTYPE, and a member name NAME,
3361 return the address of this member as a "pointer to member" type.
3362 If INTYPE is non-null, then it will be the type of the member we
3363 are looking for. This will help us resolve "pointers to member
3364 functions". This function is used to resolve user expressions of
3365 the form "DOMAIN::NAME". */
3367 static struct value
*
3368 value_struct_elt_for_reference (struct type
*domain
, int offset
,
3369 struct type
*curtype
, const char *name
,
3370 struct type
*intype
,
3374 struct type
*t
= check_typedef (curtype
);
3376 struct value
*result
;
3378 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
3379 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
3380 error (_("Internal error: non-aggregate type "
3381 "to value_struct_elt_for_reference"));
3383 for (i
= TYPE_NFIELDS (t
) - 1; i
>= TYPE_N_BASECLASSES (t
); i
--)
3385 const char *t_field_name
= TYPE_FIELD_NAME (t
, i
);
3387 if (t_field_name
&& strcmp (t_field_name
, name
) == 0)
3389 if (field_is_static (&TYPE_FIELD (t
, i
)))
3391 struct value
*v
= value_static_field (t
, i
);
3396 if (TYPE_FIELD_PACKED (t
, i
))
3397 error (_("pointers to bitfield members not allowed"));
3400 return value_from_longest
3401 (lookup_memberptr_type (TYPE_FIELD_TYPE (t
, i
), domain
),
3402 offset
+ (LONGEST
) (TYPE_FIELD_BITPOS (t
, i
) >> 3));
3403 else if (noside
!= EVAL_NORMAL
)
3404 return allocate_value (TYPE_FIELD_TYPE (t
, i
));
3407 /* Try to evaluate NAME as a qualified name with implicit
3408 this pointer. In this case, attempt to return the
3409 equivalent to `this->*(&TYPE::NAME)'. */
3410 struct value
*v
= value_of_this_silent (current_language
);
3413 struct value
*ptr
, *this_v
= v
;
3415 struct type
*type
, *tmp
;
3417 ptr
= value_aggregate_elt (domain
, name
, NULL
, 1, noside
);
3418 type
= check_typedef (value_type (ptr
));
3419 gdb_assert (type
!= NULL
3420 && TYPE_CODE (type
) == TYPE_CODE_MEMBERPTR
);
3421 tmp
= lookup_pointer_type (TYPE_SELF_TYPE (type
));
3422 v
= value_cast_pointers (tmp
, v
, 1);
3423 mem_offset
= value_as_long (ptr
);
3424 if (domain
!= curtype
)
3426 /* Find class offset of type CURTYPE from either its
3427 parent type DOMAIN or the type of implied this. */
3429 bool isvirt
= false;
3430 if (get_baseclass_offset (domain
, curtype
, v
, &boff
,
3435 struct type
*p
= check_typedef (value_type (this_v
));
3436 p
= check_typedef (TYPE_TARGET_TYPE (p
));
3437 if (get_baseclass_offset (p
, curtype
, this_v
,
3442 tmp
= lookup_pointer_type (TYPE_TARGET_TYPE (type
));
3443 result
= value_from_pointer (tmp
,
3444 value_as_long (v
) + mem_offset
);
3445 return value_ind (result
);
3448 error (_("Cannot reference non-static field \"%s\""), name
);
3453 /* C++: If it was not found as a data field, then try to return it
3454 as a pointer to a method. */
3456 /* Perform all necessary dereferencing. */
3457 while (intype
&& TYPE_CODE (intype
) == TYPE_CODE_PTR
)
3458 intype
= TYPE_TARGET_TYPE (intype
);
3460 for (i
= TYPE_NFN_FIELDS (t
) - 1; i
>= 0; --i
)
3462 const char *t_field_name
= TYPE_FN_FIELDLIST_NAME (t
, i
);
3463 char dem_opname
[64];
3465 if (startswith (t_field_name
, "__")
3466 || startswith (t_field_name
, "op")
3467 || startswith (t_field_name
, "type"))
3469 if (cplus_demangle_opname (t_field_name
,
3470 dem_opname
, DMGL_ANSI
))
3471 t_field_name
= dem_opname
;
3472 else if (cplus_demangle_opname (t_field_name
,
3474 t_field_name
= dem_opname
;
3476 if (t_field_name
&& strcmp (t_field_name
, name
) == 0)
3479 int len
= TYPE_FN_FIELDLIST_LENGTH (t
, i
);
3480 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (t
, i
);
3482 check_stub_method_group (t
, i
);
3486 for (j
= 0; j
< len
; ++j
)
3488 if (TYPE_CONST (intype
) != TYPE_FN_FIELD_CONST (f
, j
))
3490 if (TYPE_VOLATILE (intype
) != TYPE_FN_FIELD_VOLATILE (f
, j
))
3493 if (compare_parameters (TYPE_FN_FIELD_TYPE (f
, j
), intype
, 0)
3494 || compare_parameters (TYPE_FN_FIELD_TYPE (f
, j
),
3500 error (_("no member function matches "
3501 "that type instantiation"));
3508 for (ii
= 0; ii
< len
; ++ii
)
3510 /* Skip artificial methods. This is necessary if,
3511 for example, the user wants to "print
3512 subclass::subclass" with only one user-defined
3513 constructor. There is no ambiguity in this case.
3514 We are careful here to allow artificial methods
3515 if they are the unique result. */
3516 if (TYPE_FN_FIELD_ARTIFICIAL (f
, ii
))
3523 /* Desired method is ambiguous if more than one
3524 method is defined. */
3525 if (j
!= -1 && !TYPE_FN_FIELD_ARTIFICIAL (f
, j
))
3526 error (_("non-unique member `%s' requires "
3527 "type instantiation"), name
);
3533 error (_("no matching member function"));
3536 if (TYPE_FN_FIELD_STATIC_P (f
, j
))
3539 lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f
, j
),
3540 0, VAR_DOMAIN
, 0).symbol
;
3546 return value_addr (read_var_value (s
, 0, 0));
3548 return read_var_value (s
, 0, 0);
3551 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
3555 result
= allocate_value
3556 (lookup_methodptr_type (TYPE_FN_FIELD_TYPE (f
, j
)));
3557 cplus_make_method_ptr (value_type (result
),
3558 value_contents_writeable (result
),
3559 TYPE_FN_FIELD_VOFFSET (f
, j
), 1);
3561 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
3562 return allocate_value (TYPE_FN_FIELD_TYPE (f
, j
));
3564 error (_("Cannot reference virtual member function \"%s\""),
3570 lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f
, j
),
3571 0, VAR_DOMAIN
, 0).symbol
;
3576 struct value
*v
= read_var_value (s
, 0, 0);
3581 result
= allocate_value (lookup_methodptr_type (TYPE_FN_FIELD_TYPE (f
, j
)));
3582 cplus_make_method_ptr (value_type (result
),
3583 value_contents_writeable (result
),
3584 value_address (v
), 0);
3590 for (i
= TYPE_N_BASECLASSES (t
) - 1; i
>= 0; i
--)
3595 if (BASETYPE_VIA_VIRTUAL (t
, i
))
3598 base_offset
= TYPE_BASECLASS_BITPOS (t
, i
) / 8;
3599 v
= value_struct_elt_for_reference (domain
,
3600 offset
+ base_offset
,
3601 TYPE_BASECLASS (t
, i
),
3603 want_address
, noside
);
3608 /* As a last chance, pretend that CURTYPE is a namespace, and look
3609 it up that way; this (frequently) works for types nested inside
3612 return value_maybe_namespace_elt (curtype
, name
,
3613 want_address
, noside
);
3616 /* C++: Return the member NAME of the namespace given by the type
3619 static struct value
*
3620 value_namespace_elt (const struct type
*curtype
,
3621 const char *name
, int want_address
,
3624 struct value
*retval
= value_maybe_namespace_elt (curtype
, name
,
3629 error (_("No symbol \"%s\" in namespace \"%s\"."),
3630 name
, TYPE_NAME (curtype
));
3635 /* A helper function used by value_namespace_elt and
3636 value_struct_elt_for_reference. It looks up NAME inside the
3637 context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
3638 is a class and NAME refers to a type in CURTYPE itself (as opposed
3639 to, say, some base class of CURTYPE). */
3641 static struct value
*
3642 value_maybe_namespace_elt (const struct type
*curtype
,
3643 const char *name
, int want_address
,
3646 const char *namespace_name
= TYPE_NAME (curtype
);
3647 struct block_symbol sym
;
3648 struct value
*result
;
3650 sym
= cp_lookup_symbol_namespace (namespace_name
, name
,
3651 get_selected_block (0), VAR_DOMAIN
);
3653 if (sym
.symbol
== NULL
)
3655 else if ((noside
== EVAL_AVOID_SIDE_EFFECTS
)
3656 && (SYMBOL_CLASS (sym
.symbol
) == LOC_TYPEDEF
))
3657 result
= allocate_value (SYMBOL_TYPE (sym
.symbol
));
3659 result
= value_of_variable (sym
.symbol
, sym
.block
);
3662 result
= value_addr (result
);
3667 /* Given a pointer or a reference value V, find its real (RTTI) type.
3669 Other parameters FULL, TOP, USING_ENC as with value_rtti_type()
3670 and refer to the values computed for the object pointed to. */
3673 value_rtti_indirect_type (struct value
*v
, int *full
,
3674 LONGEST
*top
, int *using_enc
)
3676 struct value
*target
= NULL
;
3677 struct type
*type
, *real_type
, *target_type
;
3679 type
= value_type (v
);
3680 type
= check_typedef (type
);
3681 if (TYPE_IS_REFERENCE (type
))
3682 target
= coerce_ref (v
);
3683 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
3688 target
= value_ind (v
);
3690 CATCH (except
, RETURN_MASK_ERROR
)
3692 if (except
.error
== MEMORY_ERROR
)
3694 /* value_ind threw a memory error. The pointer is NULL or
3695 contains an uninitialized value: we can't determine any
3699 throw_exception (except
);
3706 real_type
= value_rtti_type (target
, full
, top
, using_enc
);
3710 /* Copy qualifiers to the referenced object. */
3711 target_type
= value_type (target
);
3712 real_type
= make_cv_type (TYPE_CONST (target_type
),
3713 TYPE_VOLATILE (target_type
), real_type
, NULL
);
3714 if (TYPE_IS_REFERENCE (type
))
3715 real_type
= lookup_reference_type (real_type
, TYPE_CODE (type
));
3716 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
3717 real_type
= lookup_pointer_type (real_type
);
3719 internal_error (__FILE__
, __LINE__
, _("Unexpected value type."));
3721 /* Copy qualifiers to the pointer/reference. */
3722 real_type
= make_cv_type (TYPE_CONST (type
), TYPE_VOLATILE (type
),
3729 /* Given a value pointed to by ARGP, check its real run-time type, and
3730 if that is different from the enclosing type, create a new value
3731 using the real run-time type as the enclosing type (and of the same
3732 type as ARGP) and return it, with the embedded offset adjusted to
3733 be the correct offset to the enclosed object. RTYPE is the type,
3734 and XFULL, XTOP, and XUSING_ENC are the other parameters, computed
3735 by value_rtti_type(). If these are available, they can be supplied
3736 and a second call to value_rtti_type() is avoided. (Pass RTYPE ==
3737 NULL if they're not available. */
3740 value_full_object (struct value
*argp
,
3742 int xfull
, int xtop
,
3745 struct type
*real_type
;
3749 struct value
*new_val
;
3756 using_enc
= xusing_enc
;
3759 real_type
= value_rtti_type (argp
, &full
, &top
, &using_enc
);
3761 /* If no RTTI data, or if object is already complete, do nothing. */
3762 if (!real_type
|| real_type
== value_enclosing_type (argp
))
3765 /* In a destructor we might see a real type that is a superclass of
3766 the object's type. In this case it is better to leave the object
3769 && TYPE_LENGTH (real_type
) < TYPE_LENGTH (value_enclosing_type (argp
)))
3772 /* If we have the full object, but for some reason the enclosing
3773 type is wrong, set it. */
3774 /* pai: FIXME -- sounds iffy */
3777 argp
= value_copy (argp
);
3778 set_value_enclosing_type (argp
, real_type
);
3782 /* Check if object is in memory. */
3783 if (VALUE_LVAL (argp
) != lval_memory
)
3785 warning (_("Couldn't retrieve complete object of RTTI "
3786 "type %s; object may be in register(s)."),
3787 TYPE_NAME (real_type
));
3792 /* All other cases -- retrieve the complete object. */
3793 /* Go back by the computed top_offset from the beginning of the
3794 object, adjusting for the embedded offset of argp if that's what
3795 value_rtti_type used for its computation. */
3796 new_val
= value_at_lazy (real_type
, value_address (argp
) - top
+
3797 (using_enc
? 0 : value_embedded_offset (argp
)));
3798 deprecated_set_value_type (new_val
, value_type (argp
));
3799 set_value_embedded_offset (new_val
, (using_enc
3800 ? top
+ value_embedded_offset (argp
)
3806 /* Return the value of the local variable, if one exists. Throw error
3807 otherwise, such as if the request is made in an inappropriate context. */
3810 value_of_this (const struct language_defn
*lang
)
3812 struct block_symbol sym
;
3813 const struct block
*b
;
3814 struct frame_info
*frame
;
3816 if (!lang
->la_name_of_this
)
3817 error (_("no `this' in current language"));
3819 frame
= get_selected_frame (_("no frame selected"));
3821 b
= get_frame_block (frame
, NULL
);
3823 sym
= lookup_language_this (lang
, b
);
3824 if (sym
.symbol
== NULL
)
3825 error (_("current stack frame does not contain a variable named `%s'"),
3826 lang
->la_name_of_this
);
3828 return read_var_value (sym
.symbol
, sym
.block
, frame
);
3831 /* Return the value of the local variable, if one exists. Return NULL
3832 otherwise. Never throw error. */
3835 value_of_this_silent (const struct language_defn
*lang
)
3837 struct value
*ret
= NULL
;
3841 ret
= value_of_this (lang
);
3843 CATCH (except
, RETURN_MASK_ERROR
)
3851 /* Create a slice (sub-string, sub-array) of ARRAY, that is LENGTH
3852 elements long, starting at LOWBOUND. The result has the same lower
3853 bound as the original ARRAY. */
3856 value_slice (struct value
*array
, int lowbound
, int length
)
3858 struct type
*slice_range_type
, *slice_type
, *range_type
;
3859 LONGEST lowerbound
, upperbound
;
3860 struct value
*slice
;
3861 struct type
*array_type
;
3863 array_type
= check_typedef (value_type (array
));
3864 if (TYPE_CODE (array_type
) != TYPE_CODE_ARRAY
3865 && TYPE_CODE (array_type
) != TYPE_CODE_STRING
)
3866 error (_("cannot take slice of non-array"));
3868 range_type
= TYPE_INDEX_TYPE (array_type
);
3869 if (get_discrete_bounds (range_type
, &lowerbound
, &upperbound
) < 0)
3870 error (_("slice from bad array or bitstring"));
3872 if (lowbound
< lowerbound
|| length
< 0
3873 || lowbound
+ length
- 1 > upperbound
)
3874 error (_("slice out of range"));
3876 /* FIXME-type-allocation: need a way to free this type when we are
3878 slice_range_type
= create_static_range_type ((struct type
*) NULL
,
3879 TYPE_TARGET_TYPE (range_type
),
3881 lowbound
+ length
- 1);
3884 struct type
*element_type
= TYPE_TARGET_TYPE (array_type
);
3886 = (lowbound
- lowerbound
) * TYPE_LENGTH (check_typedef (element_type
));
3888 slice_type
= create_array_type ((struct type
*) NULL
,
3891 TYPE_CODE (slice_type
) = TYPE_CODE (array_type
);
3893 if (VALUE_LVAL (array
) == lval_memory
&& value_lazy (array
))
3894 slice
= allocate_value_lazy (slice_type
);
3897 slice
= allocate_value (slice_type
);
3898 value_contents_copy (slice
, 0, array
, offset
,
3899 type_length_units (slice_type
));
3902 set_value_component_location (slice
, array
);
3903 set_value_offset (slice
, value_offset (array
) + offset
);
3909 /* Create a value for a FORTRAN complex number. Currently most of the
3910 time values are coerced to COMPLEX*16 (i.e. a complex number
3911 composed of 2 doubles. This really should be a smarter routine
3912 that figures out precision inteligently as opposed to assuming
3913 doubles. FIXME: fmb */
3916 value_literal_complex (struct value
*arg1
,
3921 struct type
*real_type
= TYPE_TARGET_TYPE (type
);
3923 val
= allocate_value (type
);
3924 arg1
= value_cast (real_type
, arg1
);
3925 arg2
= value_cast (real_type
, arg2
);
3927 memcpy (value_contents_raw (val
),
3928 value_contents (arg1
), TYPE_LENGTH (real_type
));
3929 memcpy (value_contents_raw (val
) + TYPE_LENGTH (real_type
),
3930 value_contents (arg2
), TYPE_LENGTH (real_type
));
3934 /* Cast a value into the appropriate complex data type. */
3936 static struct value
*
3937 cast_into_complex (struct type
*type
, struct value
*val
)
3939 struct type
*real_type
= TYPE_TARGET_TYPE (type
);
3941 if (TYPE_CODE (value_type (val
)) == TYPE_CODE_COMPLEX
)
3943 struct type
*val_real_type
= TYPE_TARGET_TYPE (value_type (val
));
3944 struct value
*re_val
= allocate_value (val_real_type
);
3945 struct value
*im_val
= allocate_value (val_real_type
);
3947 memcpy (value_contents_raw (re_val
),
3948 value_contents (val
), TYPE_LENGTH (val_real_type
));
3949 memcpy (value_contents_raw (im_val
),
3950 value_contents (val
) + TYPE_LENGTH (val_real_type
),
3951 TYPE_LENGTH (val_real_type
));
3953 return value_literal_complex (re_val
, im_val
, type
);
3955 else if (TYPE_CODE (value_type (val
)) == TYPE_CODE_FLT
3956 || TYPE_CODE (value_type (val
)) == TYPE_CODE_INT
)
3957 return value_literal_complex (val
,
3958 value_zero (real_type
, not_lval
),
3961 error (_("cannot cast non-number to complex"));
3965 _initialize_valops (void)
3967 add_setshow_boolean_cmd ("overload-resolution", class_support
,
3968 &overload_resolution
, _("\
3969 Set overload resolution in evaluating C++ functions."), _("\
3970 Show overload resolution in evaluating C++ functions."),
3972 show_overload_resolution
,
3973 &setlist
, &showlist
);
3974 overload_resolution
= 1;