1 /* Perform non-arithmetic operations on values, for GDB.
3 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
5 2008 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
37 #include "dictionary.h"
38 #include "cp-support.h"
42 #include "gdb_string.h"
43 #include "gdb_assert.h"
44 #include "cp-support.h"
47 extern int overload_debug
;
48 /* Local functions. */
50 static int typecmp (int staticp
, int varargs
, int nargs
,
51 struct field t1
[], struct value
*t2
[]);
53 static struct value
*search_struct_field (char *, struct value
*,
54 int, struct type
*, int);
56 static struct value
*search_struct_method (char *, struct value
**,
58 int, int *, struct type
*);
60 static int find_oload_champ_namespace (struct type
**, int,
61 const char *, const char *,
63 struct badness_vector
**);
66 int find_oload_champ_namespace_loop (struct type
**, int,
67 const char *, const char *,
68 int, struct symbol
***,
69 struct badness_vector
**, int *);
71 static int find_oload_champ (struct type
**, int, int, int,
72 struct fn_field
*, struct symbol
**,
73 struct badness_vector
**);
75 static int oload_method_static (int, struct fn_field
*, int);
77 enum oload_classification
{ STANDARD
, NON_STANDARD
, INCOMPATIBLE
};
80 oload_classification
classify_oload_match (struct badness_vector
*,
83 static int check_field_in (struct type
*, const char *);
85 static struct value
*value_struct_elt_for_reference (struct type
*,
91 static struct value
*value_namespace_elt (const struct type
*,
92 char *, int , enum noside
);
94 static struct value
*value_maybe_namespace_elt (const struct type
*,
98 static CORE_ADDR
allocate_space_in_inferior (int);
100 static struct value
*cast_into_complex (struct type
*, struct value
*);
102 static struct fn_field
*find_method_list (struct value
**, char *,
103 int, struct type
*, int *,
104 struct type
**, int *);
106 void _initialize_valops (void);
109 /* Flag for whether we want to abandon failed expression evals by
112 static int auto_abandon
= 0;
115 int overload_resolution
= 0;
117 show_overload_resolution (struct ui_file
*file
, int from_tty
,
118 struct cmd_list_element
*c
,
121 fprintf_filtered (file
, _("\
122 Overload resolution in evaluating C++ functions is %s.\n"),
126 /* Find the address of function name NAME in the inferior. */
129 find_function_in_inferior (const char *name
)
132 sym
= lookup_symbol (name
, 0, VAR_DOMAIN
, 0, NULL
);
135 if (SYMBOL_CLASS (sym
) != LOC_BLOCK
)
137 error (_("\"%s\" exists in this program but is not a function."),
140 return value_of_variable (sym
, NULL
);
144 struct minimal_symbol
*msymbol
=
145 lookup_minimal_symbol (name
, NULL
, NULL
);
150 type
= lookup_pointer_type (builtin_type_char
);
151 type
= lookup_function_type (type
);
152 type
= lookup_pointer_type (type
);
153 maddr
= SYMBOL_VALUE_ADDRESS (msymbol
);
154 return value_from_pointer (type
, maddr
);
158 if (!target_has_execution
)
159 error (_("evaluation of this expression requires the target program to be active"));
161 error (_("evaluation of this expression requires the program to have a function \"%s\"."), name
);
166 /* Allocate NBYTES of space in the inferior using the inferior's
167 malloc and return a value that is a pointer to the allocated
171 value_allocate_space_in_inferior (int len
)
173 struct value
*blocklen
;
175 find_function_in_inferior (gdbarch_name_of_malloc (current_gdbarch
));
177 blocklen
= value_from_longest (builtin_type_int
, (LONGEST
) len
);
178 val
= call_function_by_hand (val
, 1, &blocklen
);
179 if (value_logical_not (val
))
181 if (!target_has_execution
)
182 error (_("No memory available to program now: you need to start the target first"));
184 error (_("No memory available to program: call to malloc failed"));
190 allocate_space_in_inferior (int len
)
192 return value_as_long (value_allocate_space_in_inferior (len
));
195 /* Cast struct value VAL to type TYPE and return as a value.
196 Both type and val must be of TYPE_CODE_STRUCT or TYPE_CODE_UNION
197 for this to work. Typedef to one of the codes is permitted. */
199 static struct value
*
200 value_cast_structs (struct type
*type
, struct value
*v2
)
206 gdb_assert (type
!= NULL
&& v2
!= NULL
);
208 t1
= check_typedef (type
);
209 t2
= check_typedef (value_type (v2
));
211 /* Check preconditions. */
212 gdb_assert ((TYPE_CODE (t1
) == TYPE_CODE_STRUCT
213 || TYPE_CODE (t1
) == TYPE_CODE_UNION
)
214 && !!"Precondition is that type is of STRUCT or UNION kind.");
215 gdb_assert ((TYPE_CODE (t2
) == TYPE_CODE_STRUCT
216 || TYPE_CODE (t2
) == TYPE_CODE_UNION
)
217 && !!"Precondition is that value is of STRUCT or UNION kind");
219 /* Upcasting: look in the type of the source to see if it contains the
220 type of the target as a superclass. If so, we'll need to
221 offset the pointer rather than just change its type. */
222 if (TYPE_NAME (t1
) != NULL
)
224 v
= search_struct_field (type_name_no_tag (t1
),
230 /* Downcasting: look in the type of the target to see if it contains the
231 type of the source as a superclass. If so, we'll need to
232 offset the pointer rather than just change its type.
233 FIXME: This fails silently with virtual inheritance. */
234 if (TYPE_NAME (t2
) != NULL
)
236 v
= search_struct_field (type_name_no_tag (t2
),
237 value_zero (t1
, not_lval
), 0, t1
, 1);
240 /* Downcasting is possible (t1 is superclass of v2). */
241 CORE_ADDR addr2
= VALUE_ADDRESS (v2
);
242 addr2
-= (VALUE_ADDRESS (v
)
244 + value_embedded_offset (v
));
245 return value_at (type
, addr2
);
251 /* Cast one pointer or reference type to another. Both TYPE and
252 the type of ARG2 should be pointer types, or else both should be
253 reference types. Returns the new pointer or reference. */
256 value_cast_pointers (struct type
*type
, struct value
*arg2
)
258 struct type
*type1
= check_typedef (type
);
259 struct type
*type2
= check_typedef (value_type (arg2
));
260 struct type
*t1
= check_typedef (TYPE_TARGET_TYPE (type
));
261 struct type
*t2
= check_typedef (TYPE_TARGET_TYPE (type2
));
263 if (TYPE_CODE (t1
) == TYPE_CODE_STRUCT
264 && TYPE_CODE (t2
) == TYPE_CODE_STRUCT
265 && !value_logical_not (arg2
))
269 if (TYPE_CODE (type2
) == TYPE_CODE_REF
)
270 v2
= coerce_ref (arg2
);
272 v2
= value_ind (arg2
);
273 gdb_assert (TYPE_CODE (value_type (v2
)) == TYPE_CODE_STRUCT
274 && !!"Why did coercion fail?");
275 v2
= value_cast_structs (t1
, v2
);
276 /* At this point we have what we can have, un-dereference if needed. */
279 struct value
*v
= value_addr (v2
);
280 deprecated_set_value_type (v
, type
);
285 /* No superclass found, just change the pointer type. */
286 arg2
= value_copy (arg2
);
287 deprecated_set_value_type (arg2
, type
);
288 arg2
= value_change_enclosing_type (arg2
, type
);
289 set_value_pointed_to_offset (arg2
, 0); /* pai: chk_val */
293 /* Cast value ARG2 to type TYPE and return as a value.
294 More general than a C cast: accepts any two types of the same length,
295 and if ARG2 is an lvalue it can be cast into anything at all. */
296 /* In C++, casts may change pointer or object representations. */
299 value_cast (struct type
*type
, struct value
*arg2
)
301 enum type_code code1
;
302 enum type_code code2
;
306 int convert_to_boolean
= 0;
308 if (value_type (arg2
) == type
)
311 code1
= TYPE_CODE (check_typedef (type
));
313 /* Check if we are casting struct reference to struct reference. */
314 if (code1
== TYPE_CODE_REF
)
316 /* We dereference type; then we recurse and finally
317 we generate value of the given reference. Nothing wrong with
319 struct type
*t1
= check_typedef (type
);
320 struct type
*dereftype
= check_typedef (TYPE_TARGET_TYPE (t1
));
321 struct value
*val
= value_cast (dereftype
, arg2
);
322 return value_ref (val
);
325 code2
= TYPE_CODE (check_typedef (value_type (arg2
)));
327 if (code2
== TYPE_CODE_REF
)
328 /* We deref the value and then do the cast. */
329 return value_cast (type
, coerce_ref (arg2
));
331 CHECK_TYPEDEF (type
);
332 code1
= TYPE_CODE (type
);
333 arg2
= coerce_ref (arg2
);
334 type2
= check_typedef (value_type (arg2
));
336 /* You can't cast to a reference type. See value_cast_pointers
338 gdb_assert (code1
!= TYPE_CODE_REF
);
340 /* A cast to an undetermined-length array_type, such as
341 (TYPE [])OBJECT, is treated like a cast to (TYPE [N])OBJECT,
342 where N is sizeof(OBJECT)/sizeof(TYPE). */
343 if (code1
== TYPE_CODE_ARRAY
)
345 struct type
*element_type
= TYPE_TARGET_TYPE (type
);
346 unsigned element_length
= TYPE_LENGTH (check_typedef (element_type
));
347 if (element_length
> 0
348 && TYPE_ARRAY_UPPER_BOUND_TYPE (type
) == BOUND_CANNOT_BE_DETERMINED
)
350 struct type
*range_type
= TYPE_INDEX_TYPE (type
);
351 int val_length
= TYPE_LENGTH (type2
);
352 LONGEST low_bound
, high_bound
, new_length
;
353 if (get_discrete_bounds (range_type
, &low_bound
, &high_bound
) < 0)
354 low_bound
= 0, high_bound
= 0;
355 new_length
= val_length
/ element_length
;
356 if (val_length
% element_length
!= 0)
357 warning (_("array element type size does not divide object size in cast"));
358 /* FIXME-type-allocation: need a way to free this type when
359 we are done with it. */
360 range_type
= create_range_type ((struct type
*) NULL
,
361 TYPE_TARGET_TYPE (range_type
),
363 new_length
+ low_bound
- 1);
364 deprecated_set_value_type (arg2
,
365 create_array_type ((struct type
*) NULL
,
372 if (current_language
->c_style_arrays
373 && TYPE_CODE (type2
) == TYPE_CODE_ARRAY
)
374 arg2
= value_coerce_array (arg2
);
376 if (TYPE_CODE (type2
) == TYPE_CODE_FUNC
)
377 arg2
= value_coerce_function (arg2
);
379 type2
= check_typedef (value_type (arg2
));
380 code2
= TYPE_CODE (type2
);
382 if (code1
== TYPE_CODE_COMPLEX
)
383 return cast_into_complex (type
, arg2
);
384 if (code1
== TYPE_CODE_BOOL
)
386 code1
= TYPE_CODE_INT
;
387 convert_to_boolean
= 1;
389 if (code1
== TYPE_CODE_CHAR
)
390 code1
= TYPE_CODE_INT
;
391 if (code2
== TYPE_CODE_BOOL
|| code2
== TYPE_CODE_CHAR
)
392 code2
= TYPE_CODE_INT
;
394 scalar
= (code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_FLT
395 || code2
== TYPE_CODE_DECFLOAT
|| code2
== TYPE_CODE_ENUM
396 || code2
== TYPE_CODE_RANGE
);
398 if ((code1
== TYPE_CODE_STRUCT
|| code1
== TYPE_CODE_UNION
)
399 && (code2
== TYPE_CODE_STRUCT
|| code2
== TYPE_CODE_UNION
)
400 && TYPE_NAME (type
) != 0)
401 return value_cast_structs (type
, arg2
);
402 if (code1
== TYPE_CODE_FLT
&& scalar
)
403 return value_from_double (type
, value_as_double (arg2
));
404 else if (code1
== TYPE_CODE_DECFLOAT
&& scalar
)
406 int dec_len
= TYPE_LENGTH (type
);
409 if (code2
== TYPE_CODE_FLT
)
410 decimal_from_floating (arg2
, dec
, dec_len
);
411 else if (code2
== TYPE_CODE_DECFLOAT
)
412 decimal_convert (value_contents (arg2
), TYPE_LENGTH (type2
),
415 /* The only option left is an integral type. */
416 decimal_from_integral (arg2
, dec
, dec_len
);
418 return value_from_decfloat (type
, dec
);
420 else if ((code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_ENUM
421 || code1
== TYPE_CODE_RANGE
)
422 && (scalar
|| code2
== TYPE_CODE_PTR
423 || code2
== TYPE_CODE_MEMBERPTR
))
427 /* When we cast pointers to integers, we mustn't use
428 gdbarch_pointer_to_address to find the address the pointer
429 represents, as value_as_long would. GDB should evaluate
430 expressions just as the compiler would --- and the compiler
431 sees a cast as a simple reinterpretation of the pointer's
433 if (code2
== TYPE_CODE_PTR
)
434 longest
= extract_unsigned_integer (value_contents (arg2
),
435 TYPE_LENGTH (type2
));
437 longest
= value_as_long (arg2
);
438 return value_from_longest (type
, convert_to_boolean
?
439 (LONGEST
) (longest
? 1 : 0) : longest
);
441 else if (code1
== TYPE_CODE_PTR
&& (code2
== TYPE_CODE_INT
442 || code2
== TYPE_CODE_ENUM
443 || code2
== TYPE_CODE_RANGE
))
445 /* TYPE_LENGTH (type) is the length of a pointer, but we really
446 want the length of an address! -- we are really dealing with
447 addresses (i.e., gdb representations) not pointers (i.e.,
448 target representations) here.
450 This allows things like "print *(int *)0x01000234" to work
451 without printing a misleading message -- which would
452 otherwise occur when dealing with a target having two byte
453 pointers and four byte addresses. */
455 int addr_bit
= gdbarch_addr_bit (current_gdbarch
);
457 LONGEST longest
= value_as_long (arg2
);
458 if (addr_bit
< sizeof (LONGEST
) * HOST_CHAR_BIT
)
460 if (longest
>= ((LONGEST
) 1 << addr_bit
)
461 || longest
<= -((LONGEST
) 1 << addr_bit
))
462 warning (_("value truncated"));
464 return value_from_longest (type
, longest
);
466 else if (code1
== TYPE_CODE_METHODPTR
&& code2
== TYPE_CODE_INT
467 && value_as_long (arg2
) == 0)
469 struct value
*result
= allocate_value (type
);
470 cplus_make_method_ptr (value_contents_writeable (result
), 0, 0);
473 else if (code1
== TYPE_CODE_MEMBERPTR
&& code2
== TYPE_CODE_INT
474 && value_as_long (arg2
) == 0)
476 /* The Itanium C++ ABI represents NULL pointers to members as
477 minus one, instead of biasing the normal case. */
478 return value_from_longest (type
, -1);
480 else if (TYPE_LENGTH (type
) == TYPE_LENGTH (type2
))
482 if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_PTR
)
483 return value_cast_pointers (type
, arg2
);
485 arg2
= value_copy (arg2
);
486 deprecated_set_value_type (arg2
, type
);
487 arg2
= value_change_enclosing_type (arg2
, type
);
488 set_value_pointed_to_offset (arg2
, 0); /* pai: chk_val */
491 else if (VALUE_LVAL (arg2
) == lval_memory
)
492 return value_at_lazy (type
,
493 VALUE_ADDRESS (arg2
) + value_offset (arg2
));
494 else if (code1
== TYPE_CODE_VOID
)
496 return value_zero (builtin_type_void
, not_lval
);
500 error (_("Invalid cast."));
505 /* Create a value of type TYPE that is zero, and return it. */
508 value_zero (struct type
*type
, enum lval_type lv
)
510 struct value
*val
= allocate_value (type
);
511 VALUE_LVAL (val
) = lv
;
516 /* Create a value of numeric type TYPE that is one, and return it. */
519 value_one (struct type
*type
, enum lval_type lv
)
521 struct type
*type1
= check_typedef (type
);
522 struct value
*val
= NULL
; /* avoid -Wall warning */
524 if (TYPE_CODE (type1
) == TYPE_CODE_DECFLOAT
)
526 struct value
*int_one
= value_from_longest (builtin_type_int
, 1);
530 decimal_from_integral (int_one
, v
, TYPE_LENGTH (builtin_type_int
));
531 val
= value_from_decfloat (type
, v
);
533 else if (TYPE_CODE (type1
) == TYPE_CODE_FLT
)
535 val
= value_from_double (type
, (DOUBLEST
) 1);
537 else if (is_integral_type (type1
))
539 val
= value_from_longest (type
, (LONGEST
) 1);
543 error (_("Not a numeric type."));
546 VALUE_LVAL (val
) = lv
;
550 /* Return a value with type TYPE located at ADDR.
552 Call value_at only if the data needs to be fetched immediately;
553 if we can be 'lazy' and defer the fetch, perhaps indefinately, call
554 value_at_lazy instead. value_at_lazy simply records the address of
555 the data and sets the lazy-evaluation-required flag. The lazy flag
556 is tested in the value_contents macro, which is used if and when
557 the contents are actually required.
559 Note: value_at does *NOT* handle embedded offsets; perform such
560 adjustments before or after calling it. */
563 value_at (struct type
*type
, CORE_ADDR addr
)
567 if (TYPE_CODE (check_typedef (type
)) == TYPE_CODE_VOID
)
568 error (_("Attempt to dereference a generic pointer."));
570 val
= allocate_value (type
);
572 read_memory (addr
, value_contents_all_raw (val
), TYPE_LENGTH (type
));
574 VALUE_LVAL (val
) = lval_memory
;
575 VALUE_ADDRESS (val
) = addr
;
580 /* Return a lazy value with type TYPE located at ADDR (cf. value_at). */
583 value_at_lazy (struct type
*type
, CORE_ADDR addr
)
587 if (TYPE_CODE (check_typedef (type
)) == TYPE_CODE_VOID
)
588 error (_("Attempt to dereference a generic pointer."));
590 val
= allocate_value (type
);
592 VALUE_LVAL (val
) = lval_memory
;
593 VALUE_ADDRESS (val
) = addr
;
594 set_value_lazy (val
, 1);
599 /* Called only from the value_contents and value_contents_all()
600 macros, if the current data for a variable needs to be loaded into
601 value_contents(VAL). Fetches the data from the user's process, and
602 clears the lazy flag to indicate that the data in the buffer is
605 If the value is zero-length, we avoid calling read_memory, which
606 would abort. We mark the value as fetched anyway -- all 0 bytes of
609 This function returns a value because it is used in the
610 value_contents macro as part of an expression, where a void would
611 not work. The value is ignored. */
614 value_fetch_lazy (struct value
*val
)
616 CORE_ADDR addr
= VALUE_ADDRESS (val
) + value_offset (val
);
617 int length
= TYPE_LENGTH (value_enclosing_type (val
));
619 struct type
*type
= value_type (val
);
621 read_memory (addr
, value_contents_all_raw (val
), length
);
623 set_value_lazy (val
, 0);
628 /* Store the contents of FROMVAL into the location of TOVAL.
629 Return a new value with the location of TOVAL and contents of FROMVAL. */
632 value_assign (struct value
*toval
, struct value
*fromval
)
636 struct frame_id old_frame
;
638 if (!deprecated_value_modifiable (toval
))
639 error (_("Left operand of assignment is not a modifiable lvalue."));
641 toval
= coerce_ref (toval
);
643 type
= value_type (toval
);
644 if (VALUE_LVAL (toval
) != lval_internalvar
)
646 toval
= value_coerce_to_target (toval
);
647 fromval
= value_cast (type
, fromval
);
651 /* Coerce arrays and functions to pointers, except for arrays
652 which only live in GDB's storage. */
653 if (!value_must_coerce_to_target (fromval
))
654 fromval
= coerce_array (fromval
);
657 CHECK_TYPEDEF (type
);
659 /* Since modifying a register can trash the frame chain, and
660 modifying memory can trash the frame cache, we save the old frame
661 and then restore the new frame afterwards. */
662 old_frame
= get_frame_id (deprecated_safe_get_selected_frame ());
664 switch (VALUE_LVAL (toval
))
666 case lval_internalvar
:
667 set_internalvar (VALUE_INTERNALVAR (toval
), fromval
);
668 val
= value_copy (VALUE_INTERNALVAR (toval
)->value
);
669 val
= value_change_enclosing_type (val
,
670 value_enclosing_type (fromval
));
671 set_value_embedded_offset (val
, value_embedded_offset (fromval
));
672 set_value_pointed_to_offset (val
,
673 value_pointed_to_offset (fromval
));
676 case lval_internalvar_component
:
677 set_internalvar_component (VALUE_INTERNALVAR (toval
),
678 value_offset (toval
),
679 value_bitpos (toval
),
680 value_bitsize (toval
),
686 const gdb_byte
*dest_buffer
;
687 CORE_ADDR changed_addr
;
689 gdb_byte buffer
[sizeof (LONGEST
)];
691 if (value_bitsize (toval
))
693 /* We assume that the argument to read_memory is in units
694 of host chars. FIXME: Is that correct? */
695 changed_len
= (value_bitpos (toval
)
696 + value_bitsize (toval
)
700 if (changed_len
> (int) sizeof (LONGEST
))
701 error (_("Can't handle bitfields which don't fit in a %d bit word."),
702 (int) sizeof (LONGEST
) * HOST_CHAR_BIT
);
704 read_memory (VALUE_ADDRESS (toval
) + value_offset (toval
),
705 buffer
, changed_len
);
706 modify_field (buffer
, value_as_long (fromval
),
707 value_bitpos (toval
), value_bitsize (toval
));
708 changed_addr
= VALUE_ADDRESS (toval
) + value_offset (toval
);
709 dest_buffer
= buffer
;
713 changed_addr
= VALUE_ADDRESS (toval
) + value_offset (toval
);
714 changed_len
= TYPE_LENGTH (type
);
715 dest_buffer
= value_contents (fromval
);
718 write_memory (changed_addr
, dest_buffer
, changed_len
);
719 if (deprecated_memory_changed_hook
)
720 deprecated_memory_changed_hook (changed_addr
, changed_len
);
726 struct frame_info
*frame
;
729 /* Figure out which frame this is in currently. */
730 frame
= frame_find_by_id (VALUE_FRAME_ID (toval
));
731 value_reg
= VALUE_REGNUM (toval
);
734 error (_("Value being assigned to is no longer active."));
736 if (gdbarch_convert_register_p
737 (current_gdbarch
, VALUE_REGNUM (toval
), type
))
739 /* If TOVAL is a special machine register requiring
740 conversion of program values to a special raw
742 gdbarch_value_to_register (current_gdbarch
, frame
,
743 VALUE_REGNUM (toval
), type
,
744 value_contents (fromval
));
748 if (value_bitsize (toval
))
751 gdb_byte buffer
[sizeof (LONGEST
)];
753 changed_len
= (value_bitpos (toval
)
754 + value_bitsize (toval
)
758 if (changed_len
> (int) sizeof (LONGEST
))
759 error (_("Can't handle bitfields which don't fit in a %d bit word."),
760 (int) sizeof (LONGEST
) * HOST_CHAR_BIT
);
762 get_frame_register_bytes (frame
, value_reg
,
763 value_offset (toval
),
764 changed_len
, buffer
);
766 modify_field (buffer
, value_as_long (fromval
),
767 value_bitpos (toval
),
768 value_bitsize (toval
));
770 put_frame_register_bytes (frame
, value_reg
,
771 value_offset (toval
),
772 changed_len
, buffer
);
776 put_frame_register_bytes (frame
, value_reg
,
777 value_offset (toval
),
779 value_contents (fromval
));
783 if (deprecated_register_changed_hook
)
784 deprecated_register_changed_hook (-1);
785 observer_notify_target_changed (¤t_target
);
790 error (_("Left operand of assignment is not an lvalue."));
793 /* Assigning to the stack pointer, frame pointer, and other
794 (architecture and calling convention specific) registers may
795 cause the frame cache to be out of date. Assigning to memory
796 also can. We just do this on all assignments to registers or
797 memory, for simplicity's sake; I doubt the slowdown matters. */
798 switch (VALUE_LVAL (toval
))
803 reinit_frame_cache ();
805 /* Having destroyed the frame cache, restore the selected
808 /* FIXME: cagney/2002-11-02: There has to be a better way of
809 doing this. Instead of constantly saving/restoring the
810 frame. Why not create a get_selected_frame() function that,
811 having saved the selected frame's ID can automatically
812 re-find the previously selected frame automatically. */
815 struct frame_info
*fi
= frame_find_by_id (old_frame
);
825 /* If the field does not entirely fill a LONGEST, then zero the sign
826 bits. If the field is signed, and is negative, then sign
828 if ((value_bitsize (toval
) > 0)
829 && (value_bitsize (toval
) < 8 * (int) sizeof (LONGEST
)))
831 LONGEST fieldval
= value_as_long (fromval
);
832 LONGEST valmask
= (((ULONGEST
) 1) << value_bitsize (toval
)) - 1;
835 if (!TYPE_UNSIGNED (type
)
836 && (fieldval
& (valmask
^ (valmask
>> 1))))
837 fieldval
|= ~valmask
;
839 fromval
= value_from_longest (type
, fieldval
);
842 val
= value_copy (toval
);
843 memcpy (value_contents_raw (val
), value_contents (fromval
),
845 deprecated_set_value_type (val
, type
);
846 val
= value_change_enclosing_type (val
,
847 value_enclosing_type (fromval
));
848 set_value_embedded_offset (val
, value_embedded_offset (fromval
));
849 set_value_pointed_to_offset (val
, value_pointed_to_offset (fromval
));
854 /* Extend a value VAL to COUNT repetitions of its type. */
857 value_repeat (struct value
*arg1
, int count
)
861 if (VALUE_LVAL (arg1
) != lval_memory
)
862 error (_("Only values in memory can be extended with '@'."));
864 error (_("Invalid number %d of repetitions."), count
);
866 val
= allocate_repeat_value (value_enclosing_type (arg1
), count
);
868 read_memory (VALUE_ADDRESS (arg1
) + value_offset (arg1
),
869 value_contents_all_raw (val
),
870 TYPE_LENGTH (value_enclosing_type (val
)));
871 VALUE_LVAL (val
) = lval_memory
;
872 VALUE_ADDRESS (val
) = VALUE_ADDRESS (arg1
) + value_offset (arg1
);
878 value_of_variable (struct symbol
*var
, struct block
*b
)
881 struct frame_info
*frame
= NULL
;
884 frame
= NULL
; /* Use selected frame. */
885 else if (symbol_read_needs_frame (var
))
887 frame
= block_innermost_frame (b
);
890 if (BLOCK_FUNCTION (b
)
891 && SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b
)))
892 error (_("No frame is currently executing in block %s."),
893 SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b
)));
895 error (_("No frame is currently executing in specified block"));
899 val
= read_var_value (var
, frame
);
901 error (_("Address of symbol \"%s\" is unknown."), SYMBOL_PRINT_NAME (var
));
906 /* Return one if VAL does not live in target memory, but should in order
907 to operate on it. Otherwise return zero. */
910 value_must_coerce_to_target (struct value
*val
)
912 struct type
*valtype
;
914 /* The only lval kinds which do not live in target memory. */
915 if (VALUE_LVAL (val
) != not_lval
916 && VALUE_LVAL (val
) != lval_internalvar
)
919 valtype
= check_typedef (value_type (val
));
921 switch (TYPE_CODE (valtype
))
923 case TYPE_CODE_ARRAY
:
924 case TYPE_CODE_STRING
:
931 /* Make sure that VAL lives in target memory if it's supposed to. For instance,
932 strings are constructed as character arrays in GDB's storage, and this
933 function copies them to the target. */
936 value_coerce_to_target (struct value
*val
)
941 if (!value_must_coerce_to_target (val
))
944 length
= TYPE_LENGTH (check_typedef (value_type (val
)));
945 addr
= allocate_space_in_inferior (length
);
946 write_memory (addr
, value_contents (val
), length
);
947 return value_at_lazy (value_type (val
), addr
);
950 /* Given a value which is an array, return a value which is a pointer
951 to its first element, regardless of whether or not the array has a
954 FIXME: A previous comment here indicated that this routine should
955 be substracting the array's lower bound. It's not clear to me that
956 this is correct. Given an array subscripting operation, it would
957 certainly work to do the adjustment here, essentially computing:
959 (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0])
961 However I believe a more appropriate and logical place to account
962 for the lower bound is to do so in value_subscript, essentially
965 (&array[0] + ((index - lowerbound) * sizeof array[0]))
967 As further evidence consider what would happen with operations
968 other than array subscripting, where the caller would get back a
969 value that had an address somewhere before the actual first element
970 of the array, and the information about the lower bound would be
971 lost because of the coercion to pointer type.
975 value_coerce_array (struct value
*arg1
)
977 struct type
*type
= check_typedef (value_type (arg1
));
979 /* If the user tries to do something requiring a pointer with an
980 array that has not yet been pushed to the target, then this would
981 be a good time to do so. */
982 arg1
= value_coerce_to_target (arg1
);
984 if (VALUE_LVAL (arg1
) != lval_memory
)
985 error (_("Attempt to take address of value not located in memory."));
987 return value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type
)),
988 (VALUE_ADDRESS (arg1
) + value_offset (arg1
)));
991 /* Given a value which is a function, return a value which is a pointer
995 value_coerce_function (struct value
*arg1
)
997 struct value
*retval
;
999 if (VALUE_LVAL (arg1
) != lval_memory
)
1000 error (_("Attempt to take address of value not located in memory."));
1002 retval
= value_from_pointer (lookup_pointer_type (value_type (arg1
)),
1003 (VALUE_ADDRESS (arg1
) + value_offset (arg1
)));
1007 /* Return a pointer value for the object for which ARG1 is the
1011 value_addr (struct value
*arg1
)
1015 struct type
*type
= check_typedef (value_type (arg1
));
1016 if (TYPE_CODE (type
) == TYPE_CODE_REF
)
1018 /* Copy the value, but change the type from (T&) to (T*). We
1019 keep the same location information, which is efficient, and
1020 allows &(&X) to get the location containing the reference. */
1021 arg2
= value_copy (arg1
);
1022 deprecated_set_value_type (arg2
,
1023 lookup_pointer_type (TYPE_TARGET_TYPE (type
)));
1026 if (TYPE_CODE (type
) == TYPE_CODE_FUNC
)
1027 return value_coerce_function (arg1
);
1029 /* If this is an array that has not yet been pushed to the target,
1030 then this would be a good time to force it to memory. */
1031 arg1
= value_coerce_to_target (arg1
);
1033 if (VALUE_LVAL (arg1
) != lval_memory
)
1034 error (_("Attempt to take address of value not located in memory."));
1036 /* Get target memory address */
1037 arg2
= value_from_pointer (lookup_pointer_type (value_type (arg1
)),
1038 (VALUE_ADDRESS (arg1
)
1039 + value_offset (arg1
)
1040 + value_embedded_offset (arg1
)));
1042 /* This may be a pointer to a base subobject; so remember the
1043 full derived object's type ... */
1044 arg2
= value_change_enclosing_type (arg2
, lookup_pointer_type (value_enclosing_type (arg1
)));
1045 /* ... and also the relative position of the subobject in the full
1047 set_value_pointed_to_offset (arg2
, value_embedded_offset (arg1
));
1051 /* Return a reference value for the object for which ARG1 is the
1055 value_ref (struct value
*arg1
)
1059 struct type
*type
= check_typedef (value_type (arg1
));
1060 if (TYPE_CODE (type
) == TYPE_CODE_REF
)
1063 arg2
= value_addr (arg1
);
1064 deprecated_set_value_type (arg2
, lookup_reference_type (type
));
1068 /* Given a value of a pointer type, apply the C unary * operator to
1072 value_ind (struct value
*arg1
)
1074 struct type
*base_type
;
1077 arg1
= coerce_array (arg1
);
1079 base_type
= check_typedef (value_type (arg1
));
1081 /* Allow * on an integer so we can cast it to whatever we want.
1082 This returns an int, which seems like the most C-like thing to
1083 do. "long long" variables are rare enough that
1084 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
1085 if (TYPE_CODE (base_type
) == TYPE_CODE_INT
)
1086 return value_at_lazy (builtin_type_int
,
1087 (CORE_ADDR
) value_as_address (arg1
));
1088 else if (TYPE_CODE (base_type
) == TYPE_CODE_PTR
)
1090 struct type
*enc_type
;
1091 /* We may be pointing to something embedded in a larger object.
1092 Get the real type of the enclosing object. */
1093 enc_type
= check_typedef (value_enclosing_type (arg1
));
1094 enc_type
= TYPE_TARGET_TYPE (enc_type
);
1096 if (TYPE_CODE (check_typedef (enc_type
)) == TYPE_CODE_FUNC
1097 || TYPE_CODE (check_typedef (enc_type
)) == TYPE_CODE_METHOD
)
1098 /* For functions, go through find_function_addr, which knows
1099 how to handle function descriptors. */
1100 arg2
= value_at_lazy (enc_type
,
1101 find_function_addr (arg1
, NULL
));
1103 /* Retrieve the enclosing object pointed to */
1104 arg2
= value_at_lazy (enc_type
,
1105 (value_as_address (arg1
)
1106 - value_pointed_to_offset (arg1
)));
1108 /* Re-adjust type. */
1109 deprecated_set_value_type (arg2
, TYPE_TARGET_TYPE (base_type
));
1110 /* Add embedding info. */
1111 arg2
= value_change_enclosing_type (arg2
, enc_type
);
1112 set_value_embedded_offset (arg2
, value_pointed_to_offset (arg1
));
1114 /* We may be pointing to an object of some derived type. */
1115 arg2
= value_full_object (arg2
, NULL
, 0, 0, 0);
1119 error (_("Attempt to take contents of a non-pointer value."));
1120 return 0; /* For lint -- never reached. */
1123 /* Create a value for an array by allocating space in GDB, copying
1124 copying the data into that space, and then setting up an array
1127 The array bounds are set from LOWBOUND and HIGHBOUND, and the array
1128 is populated from the values passed in ELEMVEC.
1130 The element type of the array is inherited from the type of the
1131 first element, and all elements must have the same size (though we
1132 don't currently enforce any restriction on their types). */
1135 value_array (int lowbound
, int highbound
, struct value
**elemvec
)
1139 unsigned int typelength
;
1141 struct type
*rangetype
;
1142 struct type
*arraytype
;
1145 /* Validate that the bounds are reasonable and that each of the
1146 elements have the same size. */
1148 nelem
= highbound
- lowbound
+ 1;
1151 error (_("bad array bounds (%d, %d)"), lowbound
, highbound
);
1153 typelength
= TYPE_LENGTH (value_enclosing_type (elemvec
[0]));
1154 for (idx
= 1; idx
< nelem
; idx
++)
1156 if (TYPE_LENGTH (value_enclosing_type (elemvec
[idx
])) != typelength
)
1158 error (_("array elements must all be the same size"));
1162 rangetype
= create_range_type ((struct type
*) NULL
,
1164 lowbound
, highbound
);
1165 arraytype
= create_array_type ((struct type
*) NULL
,
1166 value_enclosing_type (elemvec
[0]),
1169 if (!current_language
->c_style_arrays
)
1171 val
= allocate_value (arraytype
);
1172 for (idx
= 0; idx
< nelem
; idx
++)
1174 memcpy (value_contents_all_raw (val
) + (idx
* typelength
),
1175 value_contents_all (elemvec
[idx
]),
1181 /* Allocate space to store the array, and then initialize it by
1182 copying in each element. */
1184 val
= allocate_value (arraytype
);
1185 for (idx
= 0; idx
< nelem
; idx
++)
1186 memcpy (value_contents_writeable (val
) + (idx
* typelength
),
1187 value_contents_all (elemvec
[idx
]),
1192 /* Create a value for a string constant by allocating space in the
1193 inferior, copying the data into that space, and returning the
1194 address with type TYPE_CODE_STRING. PTR points to the string
1195 constant data; LEN is number of characters.
1197 Note that string types are like array of char types with a lower
1198 bound of zero and an upper bound of LEN - 1. Also note that the
1199 string may contain embedded null bytes. */
1202 value_string (char *ptr
, int len
)
1205 int lowbound
= current_language
->string_lower_bound
;
1206 struct type
*rangetype
= create_range_type ((struct type
*) NULL
,
1209 len
+ lowbound
- 1);
1210 struct type
*stringtype
1211 = create_string_type ((struct type
*) NULL
, rangetype
);
1214 if (current_language
->c_style_arrays
== 0)
1216 val
= allocate_value (stringtype
);
1217 memcpy (value_contents_raw (val
), ptr
, len
);
1222 /* Allocate space to store the string in the inferior, and then copy
1223 LEN bytes from PTR in gdb to that address in the inferior. */
1225 addr
= allocate_space_in_inferior (len
);
1226 write_memory (addr
, (gdb_byte
*) ptr
, len
);
1228 val
= value_at_lazy (stringtype
, addr
);
1233 value_bitstring (char *ptr
, int len
)
1236 struct type
*domain_type
= create_range_type (NULL
,
1239 struct type
*type
= create_set_type ((struct type
*) NULL
,
1241 TYPE_CODE (type
) = TYPE_CODE_BITSTRING
;
1242 val
= allocate_value (type
);
1243 memcpy (value_contents_raw (val
), ptr
, TYPE_LENGTH (type
));
1247 /* See if we can pass arguments in T2 to a function which takes
1248 arguments of types T1. T1 is a list of NARGS arguments, and T2 is
1249 a NULL-terminated vector. If some arguments need coercion of some
1250 sort, then the coerced values are written into T2. Return value is
1251 0 if the arguments could be matched, or the position at which they
1254 STATICP is nonzero if the T1 argument list came from a static
1255 member function. T2 will still include the ``this'' pointer, but
1258 For non-static member functions, we ignore the first argument,
1259 which is the type of the instance variable. This is because we
1260 want to handle calls with objects from derived classes. This is
1261 not entirely correct: we should actually check to make sure that a
1262 requested operation is type secure, shouldn't we? FIXME. */
1265 typecmp (int staticp
, int varargs
, int nargs
,
1266 struct field t1
[], struct value
*t2
[])
1271 internal_error (__FILE__
, __LINE__
,
1272 _("typecmp: no argument list"));
1274 /* Skip ``this'' argument if applicable. T2 will always include
1280 (i
< nargs
) && TYPE_CODE (t1
[i
].type
) != TYPE_CODE_VOID
;
1283 struct type
*tt1
, *tt2
;
1288 tt1
= check_typedef (t1
[i
].type
);
1289 tt2
= check_typedef (value_type (t2
[i
]));
1291 if (TYPE_CODE (tt1
) == TYPE_CODE_REF
1292 /* We should be doing hairy argument matching, as below. */
1293 && (TYPE_CODE (check_typedef (TYPE_TARGET_TYPE (tt1
))) == TYPE_CODE (tt2
)))
1295 if (TYPE_CODE (tt2
) == TYPE_CODE_ARRAY
)
1296 t2
[i
] = value_coerce_array (t2
[i
]);
1298 t2
[i
] = value_ref (t2
[i
]);
1302 /* djb - 20000715 - Until the new type structure is in the
1303 place, and we can attempt things like implicit conversions,
1304 we need to do this so you can take something like a map<const
1305 char *>, and properly access map["hello"], because the
1306 argument to [] will be a reference to a pointer to a char,
1307 and the argument will be a pointer to a char. */
1308 while (TYPE_CODE(tt1
) == TYPE_CODE_REF
1309 || TYPE_CODE (tt1
) == TYPE_CODE_PTR
)
1311 tt1
= check_typedef( TYPE_TARGET_TYPE(tt1
) );
1313 while (TYPE_CODE(tt2
) == TYPE_CODE_ARRAY
1314 || TYPE_CODE(tt2
) == TYPE_CODE_PTR
1315 || TYPE_CODE(tt2
) == TYPE_CODE_REF
)
1317 tt2
= check_typedef (TYPE_TARGET_TYPE(tt2
));
1319 if (TYPE_CODE (tt1
) == TYPE_CODE (tt2
))
1321 /* Array to pointer is a `trivial conversion' according to the
1324 /* We should be doing much hairier argument matching (see
1325 section 13.2 of the ARM), but as a quick kludge, just check
1326 for the same type code. */
1327 if (TYPE_CODE (t1
[i
].type
) != TYPE_CODE (value_type (t2
[i
])))
1330 if (varargs
|| t2
[i
] == NULL
)
1335 /* Helper function used by value_struct_elt to recurse through
1336 baseclasses. Look for a field NAME in ARG1. Adjust the address of
1337 ARG1 by OFFSET bytes, and search in it assuming it has (class) type
1338 TYPE. If found, return value, else return NULL.
1340 If LOOKING_FOR_BASECLASS, then instead of looking for struct
1341 fields, look for a baseclass named NAME. */
1343 static struct value
*
1344 search_struct_field (char *name
, struct value
*arg1
, int offset
,
1345 struct type
*type
, int looking_for_baseclass
)
1348 int nbases
= TYPE_N_BASECLASSES (type
);
1350 CHECK_TYPEDEF (type
);
1352 if (!looking_for_baseclass
)
1353 for (i
= TYPE_NFIELDS (type
) - 1; i
>= nbases
; i
--)
1355 char *t_field_name
= TYPE_FIELD_NAME (type
, i
);
1357 if (t_field_name
&& (strcmp_iw (t_field_name
, name
) == 0))
1360 if (TYPE_FIELD_STATIC (type
, i
))
1362 v
= value_static_field (type
, i
);
1364 error (_("field %s is nonexistent or has been optimised out"),
1369 v
= value_primitive_field (arg1
, offset
, i
, type
);
1371 error (_("there is no field named %s"), name
);
1377 && (t_field_name
[0] == '\0'
1378 || (TYPE_CODE (type
) == TYPE_CODE_UNION
1379 && (strcmp_iw (t_field_name
, "else") == 0))))
1381 struct type
*field_type
= TYPE_FIELD_TYPE (type
, i
);
1382 if (TYPE_CODE (field_type
) == TYPE_CODE_UNION
1383 || TYPE_CODE (field_type
) == TYPE_CODE_STRUCT
)
1385 /* Look for a match through the fields of an anonymous
1386 union, or anonymous struct. C++ provides anonymous
1389 In the GNU Chill (now deleted from GDB)
1390 implementation of variant record types, each
1391 <alternative field> has an (anonymous) union type,
1392 each member of the union represents a <variant
1393 alternative>. Each <variant alternative> is
1394 represented as a struct, with a member for each
1398 int new_offset
= offset
;
1400 /* This is pretty gross. In G++, the offset in an
1401 anonymous union is relative to the beginning of the
1402 enclosing struct. In the GNU Chill (now deleted
1403 from GDB) implementation of variant records, the
1404 bitpos is zero in an anonymous union field, so we
1405 have to add the offset of the union here. */
1406 if (TYPE_CODE (field_type
) == TYPE_CODE_STRUCT
1407 || (TYPE_NFIELDS (field_type
) > 0
1408 && TYPE_FIELD_BITPOS (field_type
, 0) == 0))
1409 new_offset
+= TYPE_FIELD_BITPOS (type
, i
) / 8;
1411 v
= search_struct_field (name
, arg1
, new_offset
,
1413 looking_for_baseclass
);
1420 for (i
= 0; i
< nbases
; i
++)
1423 struct type
*basetype
= check_typedef (TYPE_BASECLASS (type
, i
));
1424 /* If we are looking for baseclasses, this is what we get when
1425 we hit them. But it could happen that the base part's member
1426 name is not yet filled in. */
1427 int found_baseclass
= (looking_for_baseclass
1428 && TYPE_BASECLASS_NAME (type
, i
) != NULL
1429 && (strcmp_iw (name
,
1430 TYPE_BASECLASS_NAME (type
,
1433 if (BASETYPE_VIA_VIRTUAL (type
, i
))
1436 struct value
*v2
= allocate_value (basetype
);
1438 boffset
= baseclass_offset (type
, i
,
1439 value_contents (arg1
) + offset
,
1440 VALUE_ADDRESS (arg1
)
1441 + value_offset (arg1
) + offset
);
1443 error (_("virtual baseclass botch"));
1445 /* The virtual base class pointer might have been clobbered
1446 by the user program. Make sure that it still points to a
1447 valid memory location. */
1450 if (boffset
< 0 || boffset
>= TYPE_LENGTH (type
))
1452 CORE_ADDR base_addr
;
1455 VALUE_ADDRESS (arg1
) + value_offset (arg1
) + boffset
;
1456 if (target_read_memory (base_addr
,
1457 value_contents_raw (v2
),
1458 TYPE_LENGTH (basetype
)) != 0)
1459 error (_("virtual baseclass botch"));
1460 VALUE_LVAL (v2
) = lval_memory
;
1461 VALUE_ADDRESS (v2
) = base_addr
;
1465 VALUE_LVAL (v2
) = VALUE_LVAL (arg1
);
1466 VALUE_ADDRESS (v2
) = VALUE_ADDRESS (arg1
);
1467 VALUE_FRAME_ID (v2
) = VALUE_FRAME_ID (arg1
);
1468 set_value_offset (v2
, value_offset (arg1
) + boffset
);
1469 if (value_lazy (arg1
))
1470 set_value_lazy (v2
, 1);
1472 memcpy (value_contents_raw (v2
),
1473 value_contents_raw (arg1
) + boffset
,
1474 TYPE_LENGTH (basetype
));
1477 if (found_baseclass
)
1479 v
= search_struct_field (name
, v2
, 0,
1480 TYPE_BASECLASS (type
, i
),
1481 looking_for_baseclass
);
1483 else if (found_baseclass
)
1484 v
= value_primitive_field (arg1
, offset
, i
, type
);
1486 v
= search_struct_field (name
, arg1
,
1487 offset
+ TYPE_BASECLASS_BITPOS (type
,
1489 basetype
, looking_for_baseclass
);
1496 /* Helper function used by value_struct_elt to recurse through
1497 baseclasses. Look for a field NAME in ARG1. Adjust the address of
1498 ARG1 by OFFSET bytes, and search in it assuming it has (class) type
1501 If found, return value, else if name matched and args not return
1502 (value) -1, else return NULL. */
1504 static struct value
*
1505 search_struct_method (char *name
, struct value
**arg1p
,
1506 struct value
**args
, int offset
,
1507 int *static_memfuncp
, struct type
*type
)
1511 int name_matched
= 0;
1512 char dem_opname
[64];
1514 CHECK_TYPEDEF (type
);
1515 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; i
--)
1517 char *t_field_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
1518 /* FIXME! May need to check for ARM demangling here */
1519 if (strncmp (t_field_name
, "__", 2) == 0 ||
1520 strncmp (t_field_name
, "op", 2) == 0 ||
1521 strncmp (t_field_name
, "type", 4) == 0)
1523 if (cplus_demangle_opname (t_field_name
, dem_opname
, DMGL_ANSI
))
1524 t_field_name
= dem_opname
;
1525 else if (cplus_demangle_opname (t_field_name
, dem_opname
, 0))
1526 t_field_name
= dem_opname
;
1528 if (t_field_name
&& (strcmp_iw (t_field_name
, name
) == 0))
1530 int j
= TYPE_FN_FIELDLIST_LENGTH (type
, i
) - 1;
1531 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
1534 check_stub_method_group (type
, i
);
1535 if (j
> 0 && args
== 0)
1536 error (_("cannot resolve overloaded method `%s': no arguments supplied"), name
);
1537 else if (j
== 0 && args
== 0)
1539 v
= value_fn_field (arg1p
, f
, j
, type
, offset
);
1546 if (!typecmp (TYPE_FN_FIELD_STATIC_P (f
, j
),
1547 TYPE_VARARGS (TYPE_FN_FIELD_TYPE (f
, j
)),
1548 TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f
, j
)),
1549 TYPE_FN_FIELD_ARGS (f
, j
), args
))
1551 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
1552 return value_virtual_fn_field (arg1p
, f
, j
,
1554 if (TYPE_FN_FIELD_STATIC_P (f
, j
)
1556 *static_memfuncp
= 1;
1557 v
= value_fn_field (arg1p
, f
, j
, type
, offset
);
1566 for (i
= TYPE_N_BASECLASSES (type
) - 1; i
>= 0; i
--)
1570 if (BASETYPE_VIA_VIRTUAL (type
, i
))
1572 struct type
*baseclass
= check_typedef (TYPE_BASECLASS (type
, i
));
1573 const gdb_byte
*base_valaddr
;
1575 /* The virtual base class pointer might have been
1576 clobbered by the user program. Make sure that it
1577 still points to a valid memory location. */
1579 if (offset
< 0 || offset
>= TYPE_LENGTH (type
))
1581 gdb_byte
*tmp
= alloca (TYPE_LENGTH (baseclass
));
1582 if (target_read_memory (VALUE_ADDRESS (*arg1p
)
1583 + value_offset (*arg1p
) + offset
,
1584 tmp
, TYPE_LENGTH (baseclass
)) != 0)
1585 error (_("virtual baseclass botch"));
1589 base_valaddr
= value_contents (*arg1p
) + offset
;
1591 base_offset
= baseclass_offset (type
, i
, base_valaddr
,
1592 VALUE_ADDRESS (*arg1p
)
1593 + value_offset (*arg1p
) + offset
);
1594 if (base_offset
== -1)
1595 error (_("virtual baseclass botch"));
1599 base_offset
= TYPE_BASECLASS_BITPOS (type
, i
) / 8;
1601 v
= search_struct_method (name
, arg1p
, args
, base_offset
+ offset
,
1602 static_memfuncp
, TYPE_BASECLASS (type
, i
));
1603 if (v
== (struct value
*) - 1)
1609 /* FIXME-bothner: Why is this commented out? Why is it here? */
1610 /* *arg1p = arg1_tmp; */
1615 return (struct value
*) - 1;
1620 /* Given *ARGP, a value of type (pointer to a)* structure/union,
1621 extract the component named NAME from the ultimate target
1622 structure/union and return it as a value with its appropriate type.
1623 ERR is used in the error message if *ARGP's type is wrong.
1625 C++: ARGS is a list of argument types to aid in the selection of
1626 an appropriate method. Also, handle derived types.
1628 STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
1629 where the truthvalue of whether the function that was resolved was
1630 a static member function or not is stored.
1632 ERR is an error message to be printed in case the field is not
1636 value_struct_elt (struct value
**argp
, struct value
**args
,
1637 char *name
, int *static_memfuncp
, char *err
)
1642 *argp
= coerce_array (*argp
);
1644 t
= check_typedef (value_type (*argp
));
1646 /* Follow pointers until we get to a non-pointer. */
1648 while (TYPE_CODE (t
) == TYPE_CODE_PTR
|| TYPE_CODE (t
) == TYPE_CODE_REF
)
1650 *argp
= value_ind (*argp
);
1651 /* Don't coerce fn pointer to fn and then back again! */
1652 if (TYPE_CODE (value_type (*argp
)) != TYPE_CODE_FUNC
)
1653 *argp
= coerce_array (*argp
);
1654 t
= check_typedef (value_type (*argp
));
1657 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
1658 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
1659 error (_("Attempt to extract a component of a value that is not a %s."), err
);
1661 /* Assume it's not, unless we see that it is. */
1662 if (static_memfuncp
)
1663 *static_memfuncp
= 0;
1667 /* if there are no arguments ...do this... */
1669 /* Try as a field first, because if we succeed, there is less
1671 v
= search_struct_field (name
, *argp
, 0, t
, 0);
1675 /* C++: If it was not found as a data field, then try to
1676 return it as a pointer to a method. */
1678 if (destructor_name_p (name
, t
))
1679 error (_("Cannot get value of destructor"));
1681 v
= search_struct_method (name
, argp
, args
, 0,
1682 static_memfuncp
, t
);
1684 if (v
== (struct value
*) - 1)
1685 error (_("Cannot take address of method %s."), name
);
1688 if (TYPE_NFN_FIELDS (t
))
1689 error (_("There is no member or method named %s."), name
);
1691 error (_("There is no member named %s."), name
);
1696 if (destructor_name_p (name
, t
))
1700 /* Destructors are a special case. */
1701 int m_index
, f_index
;
1704 if (get_destructor_fn_field (t
, &m_index
, &f_index
))
1706 v
= value_fn_field (NULL
,
1707 TYPE_FN_FIELDLIST1 (t
, m_index
),
1711 error (_("could not find destructor function named %s."),
1718 error (_("destructor should not have any argument"));
1722 v
= search_struct_method (name
, argp
, args
, 0,
1723 static_memfuncp
, t
);
1725 if (v
== (struct value
*) - 1)
1727 error (_("One of the arguments you tried to pass to %s could not be converted to what the function wants."), name
);
1731 /* See if user tried to invoke data as function. If so, hand it
1732 back. If it's not callable (i.e., a pointer to function),
1733 gdb should give an error. */
1734 v
= search_struct_field (name
, *argp
, 0, t
, 0);
1738 error (_("Structure has no component named %s."), name
);
1742 /* Search through the methods of an object (and its bases) to find a
1743 specified method. Return the pointer to the fn_field list of
1744 overloaded instances.
1746 Helper function for value_find_oload_list.
1747 ARGP is a pointer to a pointer to a value (the object).
1748 METHOD is a string containing the method name.
1749 OFFSET is the offset within the value.
1750 TYPE is the assumed type of the object.
1751 NUM_FNS is the number of overloaded instances.
1752 BASETYPE is set to the actual type of the subobject where the
1754 BOFFSET is the offset of the base subobject where the method is found.
1757 static struct fn_field
*
1758 find_method_list (struct value
**argp
, char *method
,
1759 int offset
, struct type
*type
, int *num_fns
,
1760 struct type
**basetype
, int *boffset
)
1764 CHECK_TYPEDEF (type
);
1768 /* First check in object itself. */
1769 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; i
--)
1771 /* pai: FIXME What about operators and type conversions? */
1772 char *fn_field_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
1773 if (fn_field_name
&& (strcmp_iw (fn_field_name
, method
) == 0))
1775 int len
= TYPE_FN_FIELDLIST_LENGTH (type
, i
);
1776 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
1782 /* Resolve any stub methods. */
1783 check_stub_method_group (type
, i
);
1789 /* Not found in object, check in base subobjects. */
1790 for (i
= TYPE_N_BASECLASSES (type
) - 1; i
>= 0; i
--)
1793 if (BASETYPE_VIA_VIRTUAL (type
, i
))
1795 base_offset
= value_offset (*argp
) + offset
;
1796 base_offset
= baseclass_offset (type
, i
,
1797 value_contents (*argp
) + base_offset
,
1798 VALUE_ADDRESS (*argp
) + base_offset
);
1799 if (base_offset
== -1)
1800 error (_("virtual baseclass botch"));
1802 else /* Non-virtual base, simply use bit position from debug
1805 base_offset
= TYPE_BASECLASS_BITPOS (type
, i
) / 8;
1807 f
= find_method_list (argp
, method
, base_offset
+ offset
,
1808 TYPE_BASECLASS (type
, i
), num_fns
,
1816 /* Return the list of overloaded methods of a specified name.
1818 ARGP is a pointer to a pointer to a value (the object).
1819 METHOD is the method name.
1820 OFFSET is the offset within the value contents.
1821 NUM_FNS is the number of overloaded instances.
1822 BASETYPE is set to the type of the base subobject that defines the
1824 BOFFSET is the offset of the base subobject which defines the method.
1828 value_find_oload_method_list (struct value
**argp
, char *method
,
1829 int offset
, int *num_fns
,
1830 struct type
**basetype
, int *boffset
)
1834 t
= check_typedef (value_type (*argp
));
1836 /* Code snarfed from value_struct_elt. */
1837 while (TYPE_CODE (t
) == TYPE_CODE_PTR
|| TYPE_CODE (t
) == TYPE_CODE_REF
)
1839 *argp
= value_ind (*argp
);
1840 /* Don't coerce fn pointer to fn and then back again! */
1841 if (TYPE_CODE (value_type (*argp
)) != TYPE_CODE_FUNC
)
1842 *argp
= coerce_array (*argp
);
1843 t
= check_typedef (value_type (*argp
));
1846 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
1847 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
1848 error (_("Attempt to extract a component of a value that is not a struct or union"));
1850 return find_method_list (argp
, method
, 0, t
, num_fns
,
1854 /* Given an array of argument types (ARGTYPES) (which includes an
1855 entry for "this" in the case of C++ methods), the number of
1856 arguments NARGS, the NAME of a function whether it's a method or
1857 not (METHOD), and the degree of laxness (LAX) in conforming to
1858 overload resolution rules in ANSI C++, find the best function that
1859 matches on the argument types according to the overload resolution
1862 In the case of class methods, the parameter OBJ is an object value
1863 in which to search for overloaded methods.
1865 In the case of non-method functions, the parameter FSYM is a symbol
1866 corresponding to one of the overloaded functions.
1868 Return value is an integer: 0 -> good match, 10 -> debugger applied
1869 non-standard coercions, 100 -> incompatible.
1871 If a method is being searched for, VALP will hold the value.
1872 If a non-method is being searched for, SYMP will hold the symbol
1875 If a method is being searched for, and it is a static method,
1876 then STATICP will point to a non-zero value.
1878 Note: This function does *not* check the value of
1879 overload_resolution. Caller must check it to see whether overload
1880 resolution is permitted.
1884 find_overload_match (struct type
**arg_types
, int nargs
,
1885 char *name
, int method
, int lax
,
1886 struct value
**objp
, struct symbol
*fsym
,
1887 struct value
**valp
, struct symbol
**symp
,
1890 struct value
*obj
= (objp
? *objp
: NULL
);
1891 /* Index of best overloaded function. */
1893 /* The measure for the current best match. */
1894 struct badness_vector
*oload_champ_bv
= NULL
;
1895 struct value
*temp
= obj
;
1896 /* For methods, the list of overloaded methods. */
1897 struct fn_field
*fns_ptr
= NULL
;
1898 /* For non-methods, the list of overloaded function symbols. */
1899 struct symbol
**oload_syms
= NULL
;
1900 /* Number of overloaded instances being considered. */
1902 struct type
*basetype
= NULL
;
1906 struct cleanup
*old_cleanups
= NULL
;
1908 const char *obj_type_name
= NULL
;
1909 char *func_name
= NULL
;
1910 enum oload_classification match_quality
;
1912 /* Get the list of overloaded methods or functions. */
1916 obj_type_name
= TYPE_NAME (value_type (obj
));
1917 /* Hack: evaluate_subexp_standard often passes in a pointer
1918 value rather than the object itself, so try again. */
1919 if ((!obj_type_name
|| !*obj_type_name
)
1920 && (TYPE_CODE (value_type (obj
)) == TYPE_CODE_PTR
))
1921 obj_type_name
= TYPE_NAME (TYPE_TARGET_TYPE (value_type (obj
)));
1923 fns_ptr
= value_find_oload_method_list (&temp
, name
,
1925 &basetype
, &boffset
);
1926 if (!fns_ptr
|| !num_fns
)
1927 error (_("Couldn't find method %s%s%s"),
1929 (obj_type_name
&& *obj_type_name
) ? "::" : "",
1931 /* If we are dealing with stub method types, they should have
1932 been resolved by find_method_list via
1933 value_find_oload_method_list above. */
1934 gdb_assert (TYPE_DOMAIN_TYPE (fns_ptr
[0].type
) != NULL
);
1935 oload_champ
= find_oload_champ (arg_types
, nargs
, method
,
1937 oload_syms
, &oload_champ_bv
);
1941 const char *qualified_name
= SYMBOL_CPLUS_DEMANGLED_NAME (fsym
);
1943 /* If we have a C++ name, try to extract just the function
1946 func_name
= cp_func_name (qualified_name
);
1948 /* If there was no C++ name, this must be a C-style function.
1949 Just return the same symbol. Do the same if cp_func_name
1950 fails for some reason. */
1951 if (func_name
== NULL
)
1957 old_cleanups
= make_cleanup (xfree
, func_name
);
1958 make_cleanup (xfree
, oload_syms
);
1959 make_cleanup (xfree
, oload_champ_bv
);
1961 oload_champ
= find_oload_champ_namespace (arg_types
, nargs
,
1968 /* Check how bad the best match is. */
1971 classify_oload_match (oload_champ_bv
, nargs
,
1972 oload_method_static (method
, fns_ptr
,
1975 if (match_quality
== INCOMPATIBLE
)
1978 error (_("Cannot resolve method %s%s%s to any overloaded instance"),
1980 (obj_type_name
&& *obj_type_name
) ? "::" : "",
1983 error (_("Cannot resolve function %s to any overloaded instance"),
1986 else if (match_quality
== NON_STANDARD
)
1989 warning (_("Using non-standard conversion to match method %s%s%s to supplied arguments"),
1991 (obj_type_name
&& *obj_type_name
) ? "::" : "",
1994 warning (_("Using non-standard conversion to match function %s to supplied arguments"),
2000 if (staticp
!= NULL
)
2001 *staticp
= oload_method_static (method
, fns_ptr
, oload_champ
);
2002 if (TYPE_FN_FIELD_VIRTUAL_P (fns_ptr
, oload_champ
))
2003 *valp
= value_virtual_fn_field (&temp
, fns_ptr
, oload_champ
,
2006 *valp
= value_fn_field (&temp
, fns_ptr
, oload_champ
,
2011 *symp
= oload_syms
[oload_champ
];
2016 if (TYPE_CODE (value_type (temp
)) != TYPE_CODE_PTR
2017 && TYPE_CODE (value_type (*objp
)) == TYPE_CODE_PTR
)
2019 temp
= value_addr (temp
);
2023 if (old_cleanups
!= NULL
)
2024 do_cleanups (old_cleanups
);
2026 switch (match_quality
)
2032 default: /* STANDARD */
2037 /* Find the best overload match, searching for FUNC_NAME in namespaces
2038 contained in QUALIFIED_NAME until it either finds a good match or
2039 runs out of namespaces. It stores the overloaded functions in
2040 *OLOAD_SYMS, and the badness vector in *OLOAD_CHAMP_BV. The
2041 calling function is responsible for freeing *OLOAD_SYMS and
2045 find_oload_champ_namespace (struct type
**arg_types
, int nargs
,
2046 const char *func_name
,
2047 const char *qualified_name
,
2048 struct symbol
***oload_syms
,
2049 struct badness_vector
**oload_champ_bv
)
2053 find_oload_champ_namespace_loop (arg_types
, nargs
,
2056 oload_syms
, oload_champ_bv
,
2062 /* Helper function for find_oload_champ_namespace; NAMESPACE_LEN is
2063 how deep we've looked for namespaces, and the champ is stored in
2064 OLOAD_CHAMP. The return value is 1 if the champ is a good one, 0
2067 It is the caller's responsibility to free *OLOAD_SYMS and
2071 find_oload_champ_namespace_loop (struct type
**arg_types
, int nargs
,
2072 const char *func_name
,
2073 const char *qualified_name
,
2075 struct symbol
***oload_syms
,
2076 struct badness_vector
**oload_champ_bv
,
2079 int next_namespace_len
= namespace_len
;
2080 int searched_deeper
= 0;
2082 struct cleanup
*old_cleanups
;
2083 int new_oload_champ
;
2084 struct symbol
**new_oload_syms
;
2085 struct badness_vector
*new_oload_champ_bv
;
2086 char *new_namespace
;
2088 if (next_namespace_len
!= 0)
2090 gdb_assert (qualified_name
[next_namespace_len
] == ':');
2091 next_namespace_len
+= 2;
2093 next_namespace_len
+=
2094 cp_find_first_component (qualified_name
+ next_namespace_len
);
2096 /* Initialize these to values that can safely be xfree'd. */
2098 *oload_champ_bv
= NULL
;
2100 /* First, see if we have a deeper namespace we can search in.
2101 If we get a good match there, use it. */
2103 if (qualified_name
[next_namespace_len
] == ':')
2105 searched_deeper
= 1;
2107 if (find_oload_champ_namespace_loop (arg_types
, nargs
,
2108 func_name
, qualified_name
,
2110 oload_syms
, oload_champ_bv
,
2117 /* If we reach here, either we're in the deepest namespace or we
2118 didn't find a good match in a deeper namespace. But, in the
2119 latter case, we still have a bad match in a deeper namespace;
2120 note that we might not find any match at all in the current
2121 namespace. (There's always a match in the deepest namespace,
2122 because this overload mechanism only gets called if there's a
2123 function symbol to start off with.) */
2125 old_cleanups
= make_cleanup (xfree
, *oload_syms
);
2126 old_cleanups
= make_cleanup (xfree
, *oload_champ_bv
);
2127 new_namespace
= alloca (namespace_len
+ 1);
2128 strncpy (new_namespace
, qualified_name
, namespace_len
);
2129 new_namespace
[namespace_len
] = '\0';
2130 new_oload_syms
= make_symbol_overload_list (func_name
,
2132 while (new_oload_syms
[num_fns
])
2135 new_oload_champ
= find_oload_champ (arg_types
, nargs
, 0, num_fns
,
2136 NULL
, new_oload_syms
,
2137 &new_oload_champ_bv
);
2139 /* Case 1: We found a good match. Free earlier matches (if any),
2140 and return it. Case 2: We didn't find a good match, but we're
2141 not the deepest function. Then go with the bad match that the
2142 deeper function found. Case 3: We found a bad match, and we're
2143 the deepest function. Then return what we found, even though
2144 it's a bad match. */
2146 if (new_oload_champ
!= -1
2147 && classify_oload_match (new_oload_champ_bv
, nargs
, 0) == STANDARD
)
2149 *oload_syms
= new_oload_syms
;
2150 *oload_champ
= new_oload_champ
;
2151 *oload_champ_bv
= new_oload_champ_bv
;
2152 do_cleanups (old_cleanups
);
2155 else if (searched_deeper
)
2157 xfree (new_oload_syms
);
2158 xfree (new_oload_champ_bv
);
2159 discard_cleanups (old_cleanups
);
2164 gdb_assert (new_oload_champ
!= -1);
2165 *oload_syms
= new_oload_syms
;
2166 *oload_champ
= new_oload_champ
;
2167 *oload_champ_bv
= new_oload_champ_bv
;
2168 discard_cleanups (old_cleanups
);
2173 /* Look for a function to take NARGS args of types ARG_TYPES. Find
2174 the best match from among the overloaded methods or functions
2175 (depending on METHOD) given by FNS_PTR or OLOAD_SYMS, respectively.
2176 The number of methods/functions in the list is given by NUM_FNS.
2177 Return the index of the best match; store an indication of the
2178 quality of the match in OLOAD_CHAMP_BV.
2180 It is the caller's responsibility to free *OLOAD_CHAMP_BV. */
2183 find_oload_champ (struct type
**arg_types
, int nargs
, int method
,
2184 int num_fns
, struct fn_field
*fns_ptr
,
2185 struct symbol
**oload_syms
,
2186 struct badness_vector
**oload_champ_bv
)
2189 /* A measure of how good an overloaded instance is. */
2190 struct badness_vector
*bv
;
2191 /* Index of best overloaded function. */
2192 int oload_champ
= -1;
2193 /* Current ambiguity state for overload resolution. */
2194 int oload_ambiguous
= 0;
2195 /* 0 => no ambiguity, 1 => two good funcs, 2 => incomparable funcs. */
2197 *oload_champ_bv
= NULL
;
2199 /* Consider each candidate in turn. */
2200 for (ix
= 0; ix
< num_fns
; ix
++)
2203 int static_offset
= oload_method_static (method
, fns_ptr
, ix
);
2205 struct type
**parm_types
;
2209 nparms
= TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (fns_ptr
, ix
));
2213 /* If it's not a method, this is the proper place. */
2214 nparms
= TYPE_NFIELDS (SYMBOL_TYPE (oload_syms
[ix
]));
2217 /* Prepare array of parameter types. */
2218 parm_types
= (struct type
**)
2219 xmalloc (nparms
* (sizeof (struct type
*)));
2220 for (jj
= 0; jj
< nparms
; jj
++)
2221 parm_types
[jj
] = (method
2222 ? (TYPE_FN_FIELD_ARGS (fns_ptr
, ix
)[jj
].type
)
2223 : TYPE_FIELD_TYPE (SYMBOL_TYPE (oload_syms
[ix
]),
2226 /* Compare parameter types to supplied argument types. Skip
2227 THIS for static methods. */
2228 bv
= rank_function (parm_types
, nparms
,
2229 arg_types
+ static_offset
,
2230 nargs
- static_offset
);
2232 if (!*oload_champ_bv
)
2234 *oload_champ_bv
= bv
;
2237 else /* See whether current candidate is better or worse than
2239 switch (compare_badness (bv
, *oload_champ_bv
))
2241 case 0: /* Top two contenders are equally good. */
2242 oload_ambiguous
= 1;
2244 case 1: /* Incomparable top contenders. */
2245 oload_ambiguous
= 2;
2247 case 2: /* New champion, record details. */
2248 *oload_champ_bv
= bv
;
2249 oload_ambiguous
= 0;
2260 fprintf_filtered (gdb_stderr
,
2261 "Overloaded method instance %s, # of parms %d\n",
2262 fns_ptr
[ix
].physname
, nparms
);
2264 fprintf_filtered (gdb_stderr
,
2265 "Overloaded function instance %s # of parms %d\n",
2266 SYMBOL_DEMANGLED_NAME (oload_syms
[ix
]),
2268 for (jj
= 0; jj
< nargs
- static_offset
; jj
++)
2269 fprintf_filtered (gdb_stderr
,
2270 "...Badness @ %d : %d\n",
2272 fprintf_filtered (gdb_stderr
,
2273 "Overload resolution champion is %d, ambiguous? %d\n",
2274 oload_champ
, oload_ambiguous
);
2281 /* Return 1 if we're looking at a static method, 0 if we're looking at
2282 a non-static method or a function that isn't a method. */
2285 oload_method_static (int method
, struct fn_field
*fns_ptr
, int index
)
2287 if (method
&& TYPE_FN_FIELD_STATIC_P (fns_ptr
, index
))
2293 /* Check how good an overload match OLOAD_CHAMP_BV represents. */
2295 static enum oload_classification
2296 classify_oload_match (struct badness_vector
*oload_champ_bv
,
2302 for (ix
= 1; ix
<= nargs
- static_offset
; ix
++)
2304 if (oload_champ_bv
->rank
[ix
] >= 100)
2305 return INCOMPATIBLE
; /* Truly mismatched types. */
2306 else if (oload_champ_bv
->rank
[ix
] >= 10)
2307 return NON_STANDARD
; /* Non-standard type conversions
2311 return STANDARD
; /* Only standard conversions needed. */
2314 /* C++: return 1 is NAME is a legitimate name for the destructor of
2315 type TYPE. If TYPE does not have a destructor, or if NAME is
2316 inappropriate for TYPE, an error is signaled. */
2318 destructor_name_p (const char *name
, const struct type
*type
)
2320 /* Destructors are a special case. */
2324 char *dname
= type_name_no_tag (type
);
2325 char *cp
= strchr (dname
, '<');
2328 /* Do not compare the template part for template classes. */
2330 len
= strlen (dname
);
2333 if (strlen (name
+ 1) != len
|| strncmp (dname
, name
+ 1, len
) != 0)
2334 error (_("name of destructor must equal name of class"));
2341 /* Helper function for check_field: Given TYPE, a structure/union,
2342 return 1 if the component named NAME from the ultimate target
2343 structure/union is defined, otherwise, return 0. */
2346 check_field_in (struct type
*type
, const char *name
)
2350 for (i
= TYPE_NFIELDS (type
) - 1; i
>= TYPE_N_BASECLASSES (type
); i
--)
2352 char *t_field_name
= TYPE_FIELD_NAME (type
, i
);
2353 if (t_field_name
&& (strcmp_iw (t_field_name
, name
) == 0))
2357 /* C++: If it was not found as a data field, then try to return it
2358 as a pointer to a method. */
2360 /* Destructors are a special case. */
2361 if (destructor_name_p (name
, type
))
2363 int m_index
, f_index
;
2365 return get_destructor_fn_field (type
, &m_index
, &f_index
);
2368 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; --i
)
2370 if (strcmp_iw (TYPE_FN_FIELDLIST_NAME (type
, i
), name
) == 0)
2374 for (i
= TYPE_N_BASECLASSES (type
) - 1; i
>= 0; i
--)
2375 if (check_field_in (TYPE_BASECLASS (type
, i
), name
))
2382 /* C++: Given ARG1, a value of type (pointer to a)* structure/union,
2383 return 1 if the component named NAME from the ultimate target
2384 structure/union is defined, otherwise, return 0. */
2387 check_field (struct value
*arg1
, const char *name
)
2391 arg1
= coerce_array (arg1
);
2393 t
= value_type (arg1
);
2395 /* Follow pointers until we get to a non-pointer. */
2400 if (TYPE_CODE (t
) != TYPE_CODE_PTR
2401 && TYPE_CODE (t
) != TYPE_CODE_REF
)
2403 t
= TYPE_TARGET_TYPE (t
);
2406 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
2407 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
2408 error (_("Internal error: `this' is not an aggregate"));
2410 return check_field_in (t
, name
);
2413 /* C++: Given an aggregate type CURTYPE, and a member name NAME,
2414 return the appropriate member (or the address of the member, if
2415 WANT_ADDRESS). This function is used to resolve user expressions
2416 of the form "DOMAIN::NAME". For more details on what happens, see
2417 the comment before value_struct_elt_for_reference. */
2420 value_aggregate_elt (struct type
*curtype
,
2421 char *name
, int want_address
,
2424 switch (TYPE_CODE (curtype
))
2426 case TYPE_CODE_STRUCT
:
2427 case TYPE_CODE_UNION
:
2428 return value_struct_elt_for_reference (curtype
, 0, curtype
,
2430 want_address
, noside
);
2431 case TYPE_CODE_NAMESPACE
:
2432 return value_namespace_elt (curtype
, name
,
2433 want_address
, noside
);
2435 internal_error (__FILE__
, __LINE__
,
2436 _("non-aggregate type in value_aggregate_elt"));
2440 /* C++: Given an aggregate type CURTYPE, and a member name NAME,
2441 return the address of this member as a "pointer to member" type.
2442 If INTYPE is non-null, then it will be the type of the member we
2443 are looking for. This will help us resolve "pointers to member
2444 functions". This function is used to resolve user expressions of
2445 the form "DOMAIN::NAME". */
2447 static struct value
*
2448 value_struct_elt_for_reference (struct type
*domain
, int offset
,
2449 struct type
*curtype
, char *name
,
2450 struct type
*intype
,
2454 struct type
*t
= curtype
;
2456 struct value
*v
, *result
;
2458 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
2459 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
2460 error (_("Internal error: non-aggregate type to value_struct_elt_for_reference"));
2462 for (i
= TYPE_NFIELDS (t
) - 1; i
>= TYPE_N_BASECLASSES (t
); i
--)
2464 char *t_field_name
= TYPE_FIELD_NAME (t
, i
);
2466 if (t_field_name
&& strcmp (t_field_name
, name
) == 0)
2468 if (TYPE_FIELD_STATIC (t
, i
))
2470 v
= value_static_field (t
, i
);
2472 error (_("static field %s has been optimized out"),
2478 if (TYPE_FIELD_PACKED (t
, i
))
2479 error (_("pointers to bitfield members not allowed"));
2482 return value_from_longest
2483 (lookup_memberptr_type (TYPE_FIELD_TYPE (t
, i
), domain
),
2484 offset
+ (LONGEST
) (TYPE_FIELD_BITPOS (t
, i
) >> 3));
2485 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2486 return allocate_value (TYPE_FIELD_TYPE (t
, i
));
2488 error (_("Cannot reference non-static field \"%s\""), name
);
2492 /* C++: If it was not found as a data field, then try to return it
2493 as a pointer to a method. */
2495 /* Destructors are a special case. */
2496 if (destructor_name_p (name
, t
))
2498 error (_("member pointers to destructors not implemented yet"));
2501 /* Perform all necessary dereferencing. */
2502 while (intype
&& TYPE_CODE (intype
) == TYPE_CODE_PTR
)
2503 intype
= TYPE_TARGET_TYPE (intype
);
2505 for (i
= TYPE_NFN_FIELDS (t
) - 1; i
>= 0; --i
)
2507 char *t_field_name
= TYPE_FN_FIELDLIST_NAME (t
, i
);
2508 char dem_opname
[64];
2510 if (strncmp (t_field_name
, "__", 2) == 0
2511 || strncmp (t_field_name
, "op", 2) == 0
2512 || strncmp (t_field_name
, "type", 4) == 0)
2514 if (cplus_demangle_opname (t_field_name
,
2515 dem_opname
, DMGL_ANSI
))
2516 t_field_name
= dem_opname
;
2517 else if (cplus_demangle_opname (t_field_name
,
2519 t_field_name
= dem_opname
;
2521 if (t_field_name
&& strcmp (t_field_name
, name
) == 0)
2523 int j
= TYPE_FN_FIELDLIST_LENGTH (t
, i
);
2524 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (t
, i
);
2526 check_stub_method_group (t
, i
);
2528 if (intype
== 0 && j
> 1)
2529 error (_("non-unique member `%s' requires type instantiation"), name
);
2533 if (TYPE_FN_FIELD_TYPE (f
, j
) == intype
)
2536 error (_("no member function matches that type instantiation"));
2541 if (TYPE_FN_FIELD_STATIC_P (f
, j
))
2544 lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f
, j
),
2545 0, VAR_DOMAIN
, 0, NULL
);
2550 return value_addr (read_var_value (s
, 0));
2552 return read_var_value (s
, 0);
2555 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
2559 result
= allocate_value
2560 (lookup_methodptr_type (TYPE_FN_FIELD_TYPE (f
, j
)));
2561 cplus_make_method_ptr (value_contents_writeable (result
),
2562 TYPE_FN_FIELD_VOFFSET (f
, j
), 1);
2564 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2565 return allocate_value (TYPE_FN_FIELD_TYPE (f
, j
));
2567 error (_("Cannot reference virtual member function \"%s\""),
2573 lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f
, j
),
2574 0, VAR_DOMAIN
, 0, NULL
);
2578 v
= read_var_value (s
, 0);
2583 result
= allocate_value (lookup_methodptr_type (TYPE_FN_FIELD_TYPE (f
, j
)));
2584 cplus_make_method_ptr (value_contents_writeable (result
),
2585 VALUE_ADDRESS (v
), 0);
2591 for (i
= TYPE_N_BASECLASSES (t
) - 1; i
>= 0; i
--)
2596 if (BASETYPE_VIA_VIRTUAL (t
, i
))
2599 base_offset
= TYPE_BASECLASS_BITPOS (t
, i
) / 8;
2600 v
= value_struct_elt_for_reference (domain
,
2601 offset
+ base_offset
,
2602 TYPE_BASECLASS (t
, i
),
2604 want_address
, noside
);
2609 /* As a last chance, pretend that CURTYPE is a namespace, and look
2610 it up that way; this (frequently) works for types nested inside
2613 return value_maybe_namespace_elt (curtype
, name
,
2614 want_address
, noside
);
2617 /* C++: Return the member NAME of the namespace given by the type
2620 static struct value
*
2621 value_namespace_elt (const struct type
*curtype
,
2622 char *name
, int want_address
,
2625 struct value
*retval
= value_maybe_namespace_elt (curtype
, name
,
2630 error (_("No symbol \"%s\" in namespace \"%s\"."),
2631 name
, TYPE_TAG_NAME (curtype
));
2636 /* A helper function used by value_namespace_elt and
2637 value_struct_elt_for_reference. It looks up NAME inside the
2638 context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
2639 is a class and NAME refers to a type in CURTYPE itself (as opposed
2640 to, say, some base class of CURTYPE). */
2642 static struct value
*
2643 value_maybe_namespace_elt (const struct type
*curtype
,
2644 char *name
, int want_address
,
2647 const char *namespace_name
= TYPE_TAG_NAME (curtype
);
2649 struct value
*result
;
2651 sym
= cp_lookup_symbol_namespace (namespace_name
, name
, NULL
,
2652 get_selected_block (0),
2657 else if ((noside
== EVAL_AVOID_SIDE_EFFECTS
)
2658 && (SYMBOL_CLASS (sym
) == LOC_TYPEDEF
))
2659 result
= allocate_value (SYMBOL_TYPE (sym
));
2661 result
= value_of_variable (sym
, get_selected_block (0));
2663 if (result
&& want_address
)
2664 result
= value_addr (result
);
2669 /* Given a pointer value V, find the real (RTTI) type of the object it
2672 Other parameters FULL, TOP, USING_ENC as with value_rtti_type()
2673 and refer to the values computed for the object pointed to. */
2676 value_rtti_target_type (struct value
*v
, int *full
,
2677 int *top
, int *using_enc
)
2679 struct value
*target
;
2681 target
= value_ind (v
);
2683 return value_rtti_type (target
, full
, top
, using_enc
);
2686 /* Given a value pointed to by ARGP, check its real run-time type, and
2687 if that is different from the enclosing type, create a new value
2688 using the real run-time type as the enclosing type (and of the same
2689 type as ARGP) and return it, with the embedded offset adjusted to
2690 be the correct offset to the enclosed object. RTYPE is the type,
2691 and XFULL, XTOP, and XUSING_ENC are the other parameters, computed
2692 by value_rtti_type(). If these are available, they can be supplied
2693 and a second call to value_rtti_type() is avoided. (Pass RTYPE ==
2694 NULL if they're not available. */
2697 value_full_object (struct value
*argp
,
2699 int xfull
, int xtop
,
2702 struct type
*real_type
;
2706 struct value
*new_val
;
2713 using_enc
= xusing_enc
;
2716 real_type
= value_rtti_type (argp
, &full
, &top
, &using_enc
);
2718 /* If no RTTI data, or if object is already complete, do nothing. */
2719 if (!real_type
|| real_type
== value_enclosing_type (argp
))
2722 /* If we have the full object, but for some reason the enclosing
2723 type is wrong, set it. */
2724 /* pai: FIXME -- sounds iffy */
2727 argp
= value_change_enclosing_type (argp
, real_type
);
2731 /* Check if object is in memory */
2732 if (VALUE_LVAL (argp
) != lval_memory
)
2734 warning (_("Couldn't retrieve complete object of RTTI type %s; object may be in register(s)."),
2735 TYPE_NAME (real_type
));
2740 /* All other cases -- retrieve the complete object. */
2741 /* Go back by the computed top_offset from the beginning of the
2742 object, adjusting for the embedded offset of argp if that's what
2743 value_rtti_type used for its computation. */
2744 new_val
= value_at_lazy (real_type
, VALUE_ADDRESS (argp
) - top
+
2745 (using_enc
? 0 : value_embedded_offset (argp
)));
2746 deprecated_set_value_type (new_val
, value_type (argp
));
2747 set_value_embedded_offset (new_val
, (using_enc
2748 ? top
+ value_embedded_offset (argp
)
2754 /* Return the value of the local variable, if one exists.
2755 Flag COMPLAIN signals an error if the request is made in an
2756 inappropriate context. */
2759 value_of_local (const char *name
, int complain
)
2761 struct symbol
*func
, *sym
;
2764 struct frame_info
*frame
;
2767 frame
= get_selected_frame (_("no frame selected"));
2770 frame
= deprecated_safe_get_selected_frame ();
2775 func
= get_frame_function (frame
);
2779 error (_("no `%s' in nameless context"), name
);
2784 b
= SYMBOL_BLOCK_VALUE (func
);
2785 if (dict_empty (BLOCK_DICT (b
)))
2788 error (_("no args, no `%s'"), name
);
2793 /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
2794 symbol instead of the LOC_ARG one (if both exist). */
2795 sym
= lookup_block_symbol (b
, name
, NULL
, VAR_DOMAIN
);
2799 error (_("current stack frame does not contain a variable named `%s'"),
2805 ret
= read_var_value (sym
, frame
);
2806 if (ret
== 0 && complain
)
2807 error (_("`%s' argument unreadable"), name
);
2811 /* C++/Objective-C: return the value of the class instance variable,
2812 if one exists. Flag COMPLAIN signals an error if the request is
2813 made in an inappropriate context. */
2816 value_of_this (int complain
)
2818 if (current_language
->la_language
== language_objc
)
2819 return value_of_local ("self", complain
);
2821 return value_of_local ("this", complain
);
2824 /* Create a slice (sub-string, sub-array) of ARRAY, that is LENGTH
2825 elements long, starting at LOWBOUND. The result has the same lower
2826 bound as the original ARRAY. */
2829 value_slice (struct value
*array
, int lowbound
, int length
)
2831 struct type
*slice_range_type
, *slice_type
, *range_type
;
2832 LONGEST lowerbound
, upperbound
;
2833 struct value
*slice
;
2834 struct type
*array_type
;
2836 array_type
= check_typedef (value_type (array
));
2837 if (TYPE_CODE (array_type
) != TYPE_CODE_ARRAY
2838 && TYPE_CODE (array_type
) != TYPE_CODE_STRING
2839 && TYPE_CODE (array_type
) != TYPE_CODE_BITSTRING
)
2840 error (_("cannot take slice of non-array"));
2842 range_type
= TYPE_INDEX_TYPE (array_type
);
2843 if (get_discrete_bounds (range_type
, &lowerbound
, &upperbound
) < 0)
2844 error (_("slice from bad array or bitstring"));
2846 if (lowbound
< lowerbound
|| length
< 0
2847 || lowbound
+ length
- 1 > upperbound
)
2848 error (_("slice out of range"));
2850 /* FIXME-type-allocation: need a way to free this type when we are
2852 slice_range_type
= create_range_type ((struct type
*) NULL
,
2853 TYPE_TARGET_TYPE (range_type
),
2855 lowbound
+ length
- 1);
2856 if (TYPE_CODE (array_type
) == TYPE_CODE_BITSTRING
)
2860 slice_type
= create_set_type ((struct type
*) NULL
,
2862 TYPE_CODE (slice_type
) = TYPE_CODE_BITSTRING
;
2863 slice
= value_zero (slice_type
, not_lval
);
2865 for (i
= 0; i
< length
; i
++)
2867 int element
= value_bit_index (array_type
,
2868 value_contents (array
),
2871 error (_("internal error accessing bitstring"));
2872 else if (element
> 0)
2874 int j
= i
% TARGET_CHAR_BIT
;
2875 if (gdbarch_bits_big_endian (current_gdbarch
))
2876 j
= TARGET_CHAR_BIT
- 1 - j
;
2877 value_contents_raw (slice
)[i
/ TARGET_CHAR_BIT
] |= (1 << j
);
2880 /* We should set the address, bitssize, and bitspos, so the
2881 slice can be used on the LHS, but that may require extensions
2882 to value_assign. For now, just leave as a non_lval.
2887 struct type
*element_type
= TYPE_TARGET_TYPE (array_type
);
2889 (lowbound
- lowerbound
) * TYPE_LENGTH (check_typedef (element_type
));
2891 slice_type
= create_array_type ((struct type
*) NULL
,
2894 TYPE_CODE (slice_type
) = TYPE_CODE (array_type
);
2896 slice
= allocate_value (slice_type
);
2897 if (value_lazy (array
))
2898 set_value_lazy (slice
, 1);
2900 memcpy (value_contents_writeable (slice
),
2901 value_contents (array
) + offset
,
2902 TYPE_LENGTH (slice_type
));
2904 if (VALUE_LVAL (array
) == lval_internalvar
)
2905 VALUE_LVAL (slice
) = lval_internalvar_component
;
2907 VALUE_LVAL (slice
) = VALUE_LVAL (array
);
2909 VALUE_ADDRESS (slice
) = VALUE_ADDRESS (array
);
2910 VALUE_FRAME_ID (slice
) = VALUE_FRAME_ID (array
);
2911 set_value_offset (slice
, value_offset (array
) + offset
);
2916 /* Create a value for a FORTRAN complex number. Currently most of the
2917 time values are coerced to COMPLEX*16 (i.e. a complex number
2918 composed of 2 doubles. This really should be a smarter routine
2919 that figures out precision inteligently as opposed to assuming
2920 doubles. FIXME: fmb */
2923 value_literal_complex (struct value
*arg1
,
2928 struct type
*real_type
= TYPE_TARGET_TYPE (type
);
2930 val
= allocate_value (type
);
2931 arg1
= value_cast (real_type
, arg1
);
2932 arg2
= value_cast (real_type
, arg2
);
2934 memcpy (value_contents_raw (val
),
2935 value_contents (arg1
), TYPE_LENGTH (real_type
));
2936 memcpy (value_contents_raw (val
) + TYPE_LENGTH (real_type
),
2937 value_contents (arg2
), TYPE_LENGTH (real_type
));
2941 /* Cast a value into the appropriate complex data type. */
2943 static struct value
*
2944 cast_into_complex (struct type
*type
, struct value
*val
)
2946 struct type
*real_type
= TYPE_TARGET_TYPE (type
);
2948 if (TYPE_CODE (value_type (val
)) == TYPE_CODE_COMPLEX
)
2950 struct type
*val_real_type
= TYPE_TARGET_TYPE (value_type (val
));
2951 struct value
*re_val
= allocate_value (val_real_type
);
2952 struct value
*im_val
= allocate_value (val_real_type
);
2954 memcpy (value_contents_raw (re_val
),
2955 value_contents (val
), TYPE_LENGTH (val_real_type
));
2956 memcpy (value_contents_raw (im_val
),
2957 value_contents (val
) + TYPE_LENGTH (val_real_type
),
2958 TYPE_LENGTH (val_real_type
));
2960 return value_literal_complex (re_val
, im_val
, type
);
2962 else if (TYPE_CODE (value_type (val
)) == TYPE_CODE_FLT
2963 || TYPE_CODE (value_type (val
)) == TYPE_CODE_INT
)
2964 return value_literal_complex (val
,
2965 value_zero (real_type
, not_lval
),
2968 error (_("cannot cast non-number to complex"));
2972 _initialize_valops (void)
2974 add_setshow_boolean_cmd ("overload-resolution", class_support
,
2975 &overload_resolution
, _("\
2976 Set overload resolution in evaluating C++ functions."), _("\
2977 Show overload resolution in evaluating C++ functions."),
2979 show_overload_resolution
,
2980 &setlist
, &showlist
);
2981 overload_resolution
= 1;