1 /* Perform non-arithmetic operations on values, for GDB.
3 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
6 Free Software Foundation, Inc.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 Boston, MA 02110-1301, USA. */
40 #include "dictionary.h"
41 #include "cp-support.h"
44 #include "gdb_string.h"
45 #include "gdb_assert.h"
46 #include "cp-support.h"
49 extern int overload_debug
;
50 /* Local functions. */
52 static int typecmp (int staticp
, int varargs
, int nargs
,
53 struct field t1
[], struct value
*t2
[]);
55 static struct value
*search_struct_field (char *, struct value
*, int,
58 static struct value
*search_struct_method (char *, struct value
**,
60 int, int *, struct type
*);
62 static int find_oload_champ_namespace (struct type
**arg_types
, int nargs
,
63 const char *func_name
,
64 const char *qualified_name
,
65 struct symbol
***oload_syms
,
66 struct badness_vector
**oload_champ_bv
);
69 int find_oload_champ_namespace_loop (struct type
**arg_types
, int nargs
,
70 const char *func_name
,
71 const char *qualified_name
,
73 struct symbol
***oload_syms
,
74 struct badness_vector
**oload_champ_bv
,
77 static int find_oload_champ (struct type
**arg_types
, int nargs
, int method
,
79 struct fn_field
*fns_ptr
,
80 struct symbol
**oload_syms
,
81 struct badness_vector
**oload_champ_bv
);
83 static int oload_method_static (int method
, struct fn_field
*fns_ptr
,
86 enum oload_classification
{ STANDARD
, NON_STANDARD
, INCOMPATIBLE
};
89 oload_classification
classify_oload_match (struct badness_vector
94 static int check_field_in (struct type
*, const char *);
96 static struct value
*value_struct_elt_for_reference (struct type
*domain
,
104 static struct value
*value_namespace_elt (const struct type
*curtype
,
105 char *name
, int want_address
,
108 static struct value
*value_maybe_namespace_elt (const struct type
*curtype
,
109 char *name
, int want_address
,
112 static CORE_ADDR
allocate_space_in_inferior (int);
114 static struct value
*cast_into_complex (struct type
*, struct value
*);
116 static struct fn_field
*find_method_list (struct value
** argp
, char *method
,
118 struct type
*type
, int *num_fns
,
119 struct type
**basetype
,
122 void _initialize_valops (void);
124 /* Flag for whether we want to abandon failed expression evals by default. */
127 static int auto_abandon
= 0;
130 int overload_resolution
= 0;
132 show_overload_resolution (struct ui_file
*file
, int from_tty
,
133 struct cmd_list_element
*c
, const char *value
)
135 fprintf_filtered (file
, _("\
136 Overload resolution in evaluating C++ functions is %s.\n"),
140 /* Find the address of function name NAME in the inferior. */
143 find_function_in_inferior (const char *name
)
146 sym
= lookup_symbol (name
, 0, VAR_DOMAIN
, 0, NULL
);
149 if (SYMBOL_CLASS (sym
) != LOC_BLOCK
)
151 error (_("\"%s\" exists in this program but is not a function."),
154 return value_of_variable (sym
, NULL
);
158 struct minimal_symbol
*msymbol
= lookup_minimal_symbol (name
, NULL
, NULL
);
163 type
= lookup_pointer_type (builtin_type_char
);
164 type
= lookup_function_type (type
);
165 type
= lookup_pointer_type (type
);
166 maddr
= SYMBOL_VALUE_ADDRESS (msymbol
);
167 return value_from_pointer (type
, maddr
);
171 if (!target_has_execution
)
172 error (_("evaluation of this expression requires the target program to be active"));
174 error (_("evaluation of this expression requires the program to have a function \"%s\"."), name
);
179 /* Allocate NBYTES of space in the inferior using the inferior's malloc
180 and return a value that is a pointer to the allocated space. */
183 value_allocate_space_in_inferior (int len
)
185 struct value
*blocklen
;
186 struct value
*val
= find_function_in_inferior (NAME_OF_MALLOC
);
188 blocklen
= value_from_longest (builtin_type_int
, (LONGEST
) len
);
189 val
= call_function_by_hand (val
, 1, &blocklen
);
190 if (value_logical_not (val
))
192 if (!target_has_execution
)
193 error (_("No memory available to program now: you need to start the target first"));
195 error (_("No memory available to program: call to malloc failed"));
201 allocate_space_in_inferior (int len
)
203 return value_as_long (value_allocate_space_in_inferior (len
));
206 /* Cast one pointer or reference type to another. Both TYPE and
207 the type of ARG2 should be pointer types, or else both should be
208 reference types. Returns the new pointer or reference. */
211 value_cast_pointers (struct type
*type
, struct value
*arg2
)
213 struct type
*type2
= check_typedef (value_type (arg2
));
214 struct type
*t1
= check_typedef (TYPE_TARGET_TYPE (type
));
215 struct type
*t2
= check_typedef (TYPE_TARGET_TYPE (type2
));
217 if (TYPE_CODE (t1
) == TYPE_CODE_STRUCT
218 && TYPE_CODE (t2
) == TYPE_CODE_STRUCT
219 && !value_logical_not (arg2
))
223 /* Look in the type of the source to see if it contains the
224 type of the target as a superclass. If so, we'll need to
225 offset the pointer rather than just change its type. */
226 if (TYPE_NAME (t1
) != NULL
)
230 if (TYPE_CODE (type2
) == TYPE_CODE_REF
)
231 v2
= coerce_ref (arg2
);
233 v2
= value_ind (arg2
);
234 v
= search_struct_field (type_name_no_tag (t1
),
239 deprecated_set_value_type (v
, type
);
244 /* Look in the type of the target to see if it contains the
245 type of the source as a superclass. If so, we'll need to
246 offset the pointer rather than just change its type.
247 FIXME: This fails silently with virtual inheritance. */
248 if (TYPE_NAME (t2
) != NULL
)
250 v
= search_struct_field (type_name_no_tag (t2
),
251 value_zero (t1
, not_lval
), 0, t1
, 1);
254 CORE_ADDR addr2
= value_as_address (arg2
);
255 addr2
-= (VALUE_ADDRESS (v
)
257 + value_embedded_offset (v
));
258 return value_from_pointer (type
, addr2
);
263 /* No superclass found, just change the pointer type. */
264 arg2
= value_copy (arg2
);
265 deprecated_set_value_type (arg2
, type
);
266 arg2
= value_change_enclosing_type (arg2
, type
);
267 set_value_pointed_to_offset (arg2
, 0); /* pai: chk_val */
271 /* Cast value ARG2 to type TYPE and return as a value.
272 More general than a C cast: accepts any two types of the same length,
273 and if ARG2 is an lvalue it can be cast into anything at all. */
274 /* In C++, casts may change pointer or object representations. */
277 value_cast (struct type
*type
, struct value
*arg2
)
279 enum type_code code1
;
280 enum type_code code2
;
284 int convert_to_boolean
= 0;
286 if (value_type (arg2
) == type
)
289 CHECK_TYPEDEF (type
);
290 code1
= TYPE_CODE (type
);
291 arg2
= coerce_ref (arg2
);
292 type2
= check_typedef (value_type (arg2
));
294 /* You can't cast to a reference type. See value_cast_pointers
296 gdb_assert (code1
!= TYPE_CODE_REF
);
298 /* A cast to an undetermined-length array_type, such as (TYPE [])OBJECT,
299 is treated like a cast to (TYPE [N])OBJECT,
300 where N is sizeof(OBJECT)/sizeof(TYPE). */
301 if (code1
== TYPE_CODE_ARRAY
)
303 struct type
*element_type
= TYPE_TARGET_TYPE (type
);
304 unsigned element_length
= TYPE_LENGTH (check_typedef (element_type
));
305 if (element_length
> 0
306 && TYPE_ARRAY_UPPER_BOUND_TYPE (type
) == BOUND_CANNOT_BE_DETERMINED
)
308 struct type
*range_type
= TYPE_INDEX_TYPE (type
);
309 int val_length
= TYPE_LENGTH (type2
);
310 LONGEST low_bound
, high_bound
, new_length
;
311 if (get_discrete_bounds (range_type
, &low_bound
, &high_bound
) < 0)
312 low_bound
= 0, high_bound
= 0;
313 new_length
= val_length
/ element_length
;
314 if (val_length
% element_length
!= 0)
315 warning (_("array element type size does not divide object size in cast"));
316 /* FIXME-type-allocation: need a way to free this type when we are
318 range_type
= create_range_type ((struct type
*) NULL
,
319 TYPE_TARGET_TYPE (range_type
),
321 new_length
+ low_bound
- 1);
322 deprecated_set_value_type (arg2
, create_array_type ((struct type
*) NULL
,
323 element_type
, range_type
));
328 if (current_language
->c_style_arrays
329 && TYPE_CODE (type2
) == TYPE_CODE_ARRAY
)
330 arg2
= value_coerce_array (arg2
);
332 if (TYPE_CODE (type2
) == TYPE_CODE_FUNC
)
333 arg2
= value_coerce_function (arg2
);
335 type2
= check_typedef (value_type (arg2
));
336 code2
= TYPE_CODE (type2
);
338 if (code1
== TYPE_CODE_COMPLEX
)
339 return cast_into_complex (type
, arg2
);
340 if (code1
== TYPE_CODE_BOOL
)
342 code1
= TYPE_CODE_INT
;
343 convert_to_boolean
= 1;
345 if (code1
== TYPE_CODE_CHAR
)
346 code1
= TYPE_CODE_INT
;
347 if (code2
== TYPE_CODE_BOOL
|| code2
== TYPE_CODE_CHAR
)
348 code2
= TYPE_CODE_INT
;
350 scalar
= (code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_FLT
351 || code2
== TYPE_CODE_ENUM
|| code2
== TYPE_CODE_RANGE
);
353 if (code1
== TYPE_CODE_STRUCT
354 && code2
== TYPE_CODE_STRUCT
355 && TYPE_NAME (type
) != 0)
357 /* Look in the type of the source to see if it contains the
358 type of the target as a superclass. If so, we'll need to
359 offset the object in addition to changing its type. */
360 struct value
*v
= search_struct_field (type_name_no_tag (type
),
364 deprecated_set_value_type (v
, type
);
368 if (code1
== TYPE_CODE_FLT
&& scalar
)
369 return value_from_double (type
, value_as_double (arg2
));
370 else if ((code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_ENUM
371 || code1
== TYPE_CODE_RANGE
)
372 && (scalar
|| code2
== TYPE_CODE_PTR
373 || code2
== TYPE_CODE_MEMBERPTR
))
377 /* If target compiled by HP aCC. */
378 if (deprecated_hp_som_som_object_present
379 && code2
== TYPE_CODE_MEMBERPTR
)
382 struct value
*retvalp
;
384 /* With HP aCC, pointers to data members have a bias. */
385 retvalp
= value_from_longest (type
, value_as_long (arg2
));
386 /* force evaluation */
387 ptr
= (unsigned int *) value_contents (retvalp
);
388 *ptr
&= ~0x20000000; /* zap 29th bit to remove bias */
392 /* When we cast pointers to integers, we mustn't use
393 POINTER_TO_ADDRESS to find the address the pointer
394 represents, as value_as_long would. GDB should evaluate
395 expressions just as the compiler would --- and the compiler
396 sees a cast as a simple reinterpretation of the pointer's
398 if (code2
== TYPE_CODE_PTR
)
399 longest
= extract_unsigned_integer (value_contents (arg2
),
400 TYPE_LENGTH (type2
));
402 longest
= value_as_long (arg2
);
403 return value_from_longest (type
, convert_to_boolean
?
404 (LONGEST
) (longest
? 1 : 0) : longest
);
406 else if (code1
== TYPE_CODE_PTR
&& (code2
== TYPE_CODE_INT
||
407 code2
== TYPE_CODE_ENUM
||
408 code2
== TYPE_CODE_RANGE
))
410 /* TYPE_LENGTH (type) is the length of a pointer, but we really
411 want the length of an address! -- we are really dealing with
412 addresses (i.e., gdb representations) not pointers (i.e.,
413 target representations) here.
415 This allows things like "print *(int *)0x01000234" to work
416 without printing a misleading message -- which would
417 otherwise occur when dealing with a target having two byte
418 pointers and four byte addresses. */
420 int addr_bit
= TARGET_ADDR_BIT
;
422 LONGEST longest
= value_as_long (arg2
);
423 if (addr_bit
< sizeof (LONGEST
) * HOST_CHAR_BIT
)
425 if (longest
>= ((LONGEST
) 1 << addr_bit
)
426 || longest
<= -((LONGEST
) 1 << addr_bit
))
427 warning (_("value truncated"));
429 return value_from_longest (type
, longest
);
431 else if (code1
== TYPE_CODE_METHODPTR
&& code2
== TYPE_CODE_INT
432 && value_as_long (arg2
) == 0)
434 struct value
*result
= allocate_value (type
);
435 cplus_make_method_ptr (value_contents_writeable (result
), 0, 0);
438 else if (code1
== TYPE_CODE_MEMBERPTR
&& code2
== TYPE_CODE_INT
439 && value_as_long (arg2
) == 0)
441 /* The Itanium C++ ABI represents NULL pointers to members as
442 minus one, instead of biasing the normal case. */
443 return value_from_longest (type
, -1);
445 else if (TYPE_LENGTH (type
) == TYPE_LENGTH (type2
))
447 if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_PTR
)
448 return value_cast_pointers (type
, arg2
);
450 arg2
= value_copy (arg2
);
451 deprecated_set_value_type (arg2
, type
);
452 arg2
= value_change_enclosing_type (arg2
, type
);
453 set_value_pointed_to_offset (arg2
, 0); /* pai: chk_val */
456 else if (VALUE_LVAL (arg2
) == lval_memory
)
457 return value_at_lazy (type
, VALUE_ADDRESS (arg2
) + value_offset (arg2
));
458 else if (code1
== TYPE_CODE_VOID
)
460 return value_zero (builtin_type_void
, not_lval
);
464 error (_("Invalid cast."));
469 /* Create a value of type TYPE that is zero, and return it. */
472 value_zero (struct type
*type
, enum lval_type lv
)
474 struct value
*val
= allocate_value (type
);
475 VALUE_LVAL (val
) = lv
;
480 /* Return a value with type TYPE located at ADDR.
482 Call value_at only if the data needs to be fetched immediately;
483 if we can be 'lazy' and defer the fetch, perhaps indefinately, call
484 value_at_lazy instead. value_at_lazy simply records the address of
485 the data and sets the lazy-evaluation-required flag. The lazy flag
486 is tested in the value_contents macro, which is used if and when
487 the contents are actually required.
489 Note: value_at does *NOT* handle embedded offsets; perform such
490 adjustments before or after calling it. */
493 value_at (struct type
*type
, CORE_ADDR addr
)
497 if (TYPE_CODE (check_typedef (type
)) == TYPE_CODE_VOID
)
498 error (_("Attempt to dereference a generic pointer."));
500 val
= allocate_value (type
);
502 read_memory (addr
, value_contents_all_raw (val
), TYPE_LENGTH (type
));
504 VALUE_LVAL (val
) = lval_memory
;
505 VALUE_ADDRESS (val
) = addr
;
510 /* Return a lazy value with type TYPE located at ADDR (cf. value_at). */
513 value_at_lazy (struct type
*type
, CORE_ADDR addr
)
517 if (TYPE_CODE (check_typedef (type
)) == TYPE_CODE_VOID
)
518 error (_("Attempt to dereference a generic pointer."));
520 val
= allocate_value (type
);
522 VALUE_LVAL (val
) = lval_memory
;
523 VALUE_ADDRESS (val
) = addr
;
524 set_value_lazy (val
, 1);
529 /* Called only from the value_contents and value_contents_all()
530 macros, if the current data for a variable needs to be loaded into
531 value_contents(VAL). Fetches the data from the user's process, and
532 clears the lazy flag to indicate that the data in the buffer is
535 If the value is zero-length, we avoid calling read_memory, which would
536 abort. We mark the value as fetched anyway -- all 0 bytes of it.
538 This function returns a value because it is used in the value_contents
539 macro as part of an expression, where a void would not work. The
543 value_fetch_lazy (struct value
*val
)
545 CORE_ADDR addr
= VALUE_ADDRESS (val
) + value_offset (val
);
546 int length
= TYPE_LENGTH (value_enclosing_type (val
));
548 struct type
*type
= value_type (val
);
550 read_memory (addr
, value_contents_all_raw (val
), length
);
552 set_value_lazy (val
, 0);
557 /* Store the contents of FROMVAL into the location of TOVAL.
558 Return a new value with the location of TOVAL and contents of FROMVAL. */
561 value_assign (struct value
*toval
, struct value
*fromval
)
565 struct frame_id old_frame
;
567 if (!deprecated_value_modifiable (toval
))
568 error (_("Left operand of assignment is not a modifiable lvalue."));
570 toval
= coerce_ref (toval
);
572 type
= value_type (toval
);
573 if (VALUE_LVAL (toval
) != lval_internalvar
)
574 fromval
= value_cast (type
, fromval
);
576 fromval
= coerce_array (fromval
);
577 CHECK_TYPEDEF (type
);
579 /* Since modifying a register can trash the frame chain, and modifying memory
580 can trash the frame cache, we save the old frame and then restore the new
582 old_frame
= get_frame_id (deprecated_selected_frame
);
584 switch (VALUE_LVAL (toval
))
586 case lval_internalvar
:
587 set_internalvar (VALUE_INTERNALVAR (toval
), fromval
);
588 val
= value_copy (VALUE_INTERNALVAR (toval
)->value
);
589 val
= value_change_enclosing_type (val
, value_enclosing_type (fromval
));
590 set_value_embedded_offset (val
, value_embedded_offset (fromval
));
591 set_value_pointed_to_offset (val
, value_pointed_to_offset (fromval
));
594 case lval_internalvar_component
:
595 set_internalvar_component (VALUE_INTERNALVAR (toval
),
596 value_offset (toval
),
597 value_bitpos (toval
),
598 value_bitsize (toval
),
604 const gdb_byte
*dest_buffer
;
605 CORE_ADDR changed_addr
;
607 gdb_byte buffer
[sizeof (LONGEST
)];
609 if (value_bitsize (toval
))
611 /* We assume that the argument to read_memory is in units of
612 host chars. FIXME: Is that correct? */
613 changed_len
= (value_bitpos (toval
)
614 + value_bitsize (toval
)
618 if (changed_len
> (int) sizeof (LONGEST
))
619 error (_("Can't handle bitfields which don't fit in a %d bit word."),
620 (int) sizeof (LONGEST
) * HOST_CHAR_BIT
);
622 read_memory (VALUE_ADDRESS (toval
) + value_offset (toval
),
623 buffer
, changed_len
);
624 modify_field (buffer
, value_as_long (fromval
),
625 value_bitpos (toval
), value_bitsize (toval
));
626 changed_addr
= VALUE_ADDRESS (toval
) + value_offset (toval
);
627 dest_buffer
= buffer
;
631 changed_addr
= VALUE_ADDRESS (toval
) + value_offset (toval
);
632 changed_len
= TYPE_LENGTH (type
);
633 dest_buffer
= value_contents (fromval
);
636 write_memory (changed_addr
, dest_buffer
, changed_len
);
637 if (deprecated_memory_changed_hook
)
638 deprecated_memory_changed_hook (changed_addr
, changed_len
);
644 struct frame_info
*frame
;
647 /* Figure out which frame this is in currently. */
648 frame
= frame_find_by_id (VALUE_FRAME_ID (toval
));
649 value_reg
= VALUE_REGNUM (toval
);
652 error (_("Value being assigned to is no longer active."));
654 if (VALUE_LVAL (toval
) == lval_register
655 && CONVERT_REGISTER_P (VALUE_REGNUM (toval
), type
))
657 /* If TOVAL is a special machine register requiring
658 conversion of program values to a special raw format. */
659 VALUE_TO_REGISTER (frame
, VALUE_REGNUM (toval
),
660 type
, value_contents (fromval
));
664 /* TOVAL is stored in a series of registers in the frame
665 specified by the structure. Copy that value out,
666 modify it, and copy it back in. */
674 /* Locate the first register that falls in the value that
675 needs to be transfered. Compute the offset of the
676 value in that register. */
679 for (reg_offset
= value_reg
, offset
= 0;
680 offset
+ register_size (current_gdbarch
, reg_offset
) <= value_offset (toval
);
682 byte_offset
= value_offset (toval
) - offset
;
685 /* Compute the number of register aligned values that need
687 if (value_bitsize (toval
))
688 amount_to_copy
= byte_offset
+ 1;
690 amount_to_copy
= byte_offset
+ TYPE_LENGTH (type
);
692 /* And a bounce buffer. Be slightly over generous. */
693 buffer
= alloca (amount_to_copy
+ MAX_REGISTER_SIZE
);
696 for (regno
= reg_offset
, amount_copied
= 0;
697 amount_copied
< amount_to_copy
;
698 amount_copied
+= register_size (current_gdbarch
, regno
), regno
++)
699 frame_register_read (frame
, regno
, buffer
+ amount_copied
);
701 /* Modify what needs to be modified. */
702 if (value_bitsize (toval
))
703 modify_field (buffer
+ byte_offset
,
704 value_as_long (fromval
),
705 value_bitpos (toval
), value_bitsize (toval
));
707 memcpy (buffer
+ byte_offset
, value_contents (fromval
),
711 for (regno
= reg_offset
, amount_copied
= 0;
712 amount_copied
< amount_to_copy
;
713 amount_copied
+= register_size (current_gdbarch
, regno
), regno
++)
714 put_frame_register (frame
, regno
, buffer
+ amount_copied
);
717 if (deprecated_register_changed_hook
)
718 deprecated_register_changed_hook (-1);
719 observer_notify_target_changed (¤t_target
);
724 error (_("Left operand of assignment is not an lvalue."));
727 /* Assigning to the stack pointer, frame pointer, and other
728 (architecture and calling convention specific) registers may
729 cause the frame cache to be out of date. Assigning to memory
730 also can. We just do this on all assignments to registers or
731 memory, for simplicity's sake; I doubt the slowdown matters. */
732 switch (VALUE_LVAL (toval
))
737 reinit_frame_cache ();
739 /* Having destoroyed the frame cache, restore the selected frame. */
741 /* FIXME: cagney/2002-11-02: There has to be a better way of
742 doing this. Instead of constantly saving/restoring the
743 frame. Why not create a get_selected_frame() function that,
744 having saved the selected frame's ID can automatically
745 re-find the previously selected frame automatically. */
748 struct frame_info
*fi
= frame_find_by_id (old_frame
);
758 /* If the field does not entirely fill a LONGEST, then zero the sign bits.
759 If the field is signed, and is negative, then sign extend. */
760 if ((value_bitsize (toval
) > 0)
761 && (value_bitsize (toval
) < 8 * (int) sizeof (LONGEST
)))
763 LONGEST fieldval
= value_as_long (fromval
);
764 LONGEST valmask
= (((ULONGEST
) 1) << value_bitsize (toval
)) - 1;
767 if (!TYPE_UNSIGNED (type
) && (fieldval
& (valmask
^ (valmask
>> 1))))
768 fieldval
|= ~valmask
;
770 fromval
= value_from_longest (type
, fieldval
);
773 val
= value_copy (toval
);
774 memcpy (value_contents_raw (val
), value_contents (fromval
),
776 deprecated_set_value_type (val
, type
);
777 val
= value_change_enclosing_type (val
, value_enclosing_type (fromval
));
778 set_value_embedded_offset (val
, value_embedded_offset (fromval
));
779 set_value_pointed_to_offset (val
, value_pointed_to_offset (fromval
));
784 /* Extend a value VAL to COUNT repetitions of its type. */
787 value_repeat (struct value
*arg1
, int count
)
791 if (VALUE_LVAL (arg1
) != lval_memory
)
792 error (_("Only values in memory can be extended with '@'."));
794 error (_("Invalid number %d of repetitions."), count
);
796 val
= allocate_repeat_value (value_enclosing_type (arg1
), count
);
798 read_memory (VALUE_ADDRESS (arg1
) + value_offset (arg1
),
799 value_contents_all_raw (val
),
800 TYPE_LENGTH (value_enclosing_type (val
)));
801 VALUE_LVAL (val
) = lval_memory
;
802 VALUE_ADDRESS (val
) = VALUE_ADDRESS (arg1
) + value_offset (arg1
);
808 value_of_variable (struct symbol
*var
, struct block
*b
)
811 struct frame_info
*frame
= NULL
;
814 frame
= NULL
; /* Use selected frame. */
815 else if (symbol_read_needs_frame (var
))
817 frame
= block_innermost_frame (b
);
820 if (BLOCK_FUNCTION (b
)
821 && SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b
)))
822 error (_("No frame is currently executing in block %s."),
823 SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b
)));
825 error (_("No frame is currently executing in specified block"));
829 val
= read_var_value (var
, frame
);
831 error (_("Address of symbol \"%s\" is unknown."), SYMBOL_PRINT_NAME (var
));
836 /* Given a value which is an array, return a value which is a pointer to its
837 first element, regardless of whether or not the array has a nonzero lower
840 FIXME: A previous comment here indicated that this routine should be
841 substracting the array's lower bound. It's not clear to me that this
842 is correct. Given an array subscripting operation, it would certainly
843 work to do the adjustment here, essentially computing:
845 (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0])
847 However I believe a more appropriate and logical place to account for
848 the lower bound is to do so in value_subscript, essentially computing:
850 (&array[0] + ((index - lowerbound) * sizeof array[0]))
852 As further evidence consider what would happen with operations other
853 than array subscripting, where the caller would get back a value that
854 had an address somewhere before the actual first element of the array,
855 and the information about the lower bound would be lost because of
856 the coercion to pointer type.
860 value_coerce_array (struct value
*arg1
)
862 struct type
*type
= check_typedef (value_type (arg1
));
864 if (VALUE_LVAL (arg1
) != lval_memory
)
865 error (_("Attempt to take address of value not located in memory."));
867 return value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type
)),
868 (VALUE_ADDRESS (arg1
) + value_offset (arg1
)));
871 /* Given a value which is a function, return a value which is a pointer
875 value_coerce_function (struct value
*arg1
)
877 struct value
*retval
;
879 if (VALUE_LVAL (arg1
) != lval_memory
)
880 error (_("Attempt to take address of value not located in memory."));
882 retval
= value_from_pointer (lookup_pointer_type (value_type (arg1
)),
883 (VALUE_ADDRESS (arg1
) + value_offset (arg1
)));
887 /* Return a pointer value for the object for which ARG1 is the contents. */
890 value_addr (struct value
*arg1
)
894 struct type
*type
= check_typedef (value_type (arg1
));
895 if (TYPE_CODE (type
) == TYPE_CODE_REF
)
897 /* Copy the value, but change the type from (T&) to (T*).
898 We keep the same location information, which is efficient,
899 and allows &(&X) to get the location containing the reference. */
900 arg2
= value_copy (arg1
);
901 deprecated_set_value_type (arg2
, lookup_pointer_type (TYPE_TARGET_TYPE (type
)));
904 if (TYPE_CODE (type
) == TYPE_CODE_FUNC
)
905 return value_coerce_function (arg1
);
907 if (VALUE_LVAL (arg1
) != lval_memory
)
908 error (_("Attempt to take address of value not located in memory."));
910 /* Get target memory address */
911 arg2
= value_from_pointer (lookup_pointer_type (value_type (arg1
)),
912 (VALUE_ADDRESS (arg1
)
913 + value_offset (arg1
)
914 + value_embedded_offset (arg1
)));
916 /* This may be a pointer to a base subobject; so remember the
917 full derived object's type ... */
918 arg2
= value_change_enclosing_type (arg2
, lookup_pointer_type (value_enclosing_type (arg1
)));
919 /* ... and also the relative position of the subobject in the full object */
920 set_value_pointed_to_offset (arg2
, value_embedded_offset (arg1
));
924 /* Return a reference value for the object for which ARG1 is the contents. */
927 value_ref (struct value
*arg1
)
931 struct type
*type
= check_typedef (value_type (arg1
));
932 if (TYPE_CODE (type
) == TYPE_CODE_REF
)
935 arg2
= value_addr (arg1
);
936 deprecated_set_value_type (arg2
, lookup_reference_type (type
));
940 /* Given a value of a pointer type, apply the C unary * operator to it. */
943 value_ind (struct value
*arg1
)
945 struct type
*base_type
;
948 arg1
= coerce_array (arg1
);
950 base_type
= check_typedef (value_type (arg1
));
952 /* Allow * on an integer so we can cast it to whatever we want.
953 This returns an int, which seems like the most C-like thing
954 to do. "long long" variables are rare enough that
955 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
956 if (TYPE_CODE (base_type
) == TYPE_CODE_INT
)
957 return value_at_lazy (builtin_type_int
,
958 (CORE_ADDR
) value_as_long (arg1
));
959 else if (TYPE_CODE (base_type
) == TYPE_CODE_PTR
)
961 struct type
*enc_type
;
962 /* We may be pointing to something embedded in a larger object */
963 /* Get the real type of the enclosing object */
964 enc_type
= check_typedef (value_enclosing_type (arg1
));
965 enc_type
= TYPE_TARGET_TYPE (enc_type
);
967 if (TYPE_CODE (check_typedef (enc_type
)) == TYPE_CODE_FUNC
968 || TYPE_CODE (check_typedef (enc_type
)) == TYPE_CODE_METHOD
)
969 /* For functions, go through find_function_addr, which knows
970 how to handle function descriptors. */
971 arg2
= value_at_lazy (enc_type
, find_function_addr (arg1
, NULL
));
973 /* Retrieve the enclosing object pointed to */
974 arg2
= value_at_lazy (enc_type
, (value_as_address (arg1
)
975 - value_pointed_to_offset (arg1
)));
978 deprecated_set_value_type (arg2
, TYPE_TARGET_TYPE (base_type
));
979 /* Add embedding info */
980 arg2
= value_change_enclosing_type (arg2
, enc_type
);
981 set_value_embedded_offset (arg2
, value_pointed_to_offset (arg1
));
983 /* We may be pointing to an object of some derived type */
984 arg2
= value_full_object (arg2
, NULL
, 0, 0, 0);
988 error (_("Attempt to take contents of a non-pointer value."));
989 return 0; /* For lint -- never reached */
992 /* Create a value for an array by allocating space in the inferior, copying
993 the data into that space, and then setting up an array value.
995 The array bounds are set from LOWBOUND and HIGHBOUND, and the array is
996 populated from the values passed in ELEMVEC.
998 The element type of the array is inherited from the type of the
999 first element, and all elements must have the same size (though we
1000 don't currently enforce any restriction on their types). */
1003 value_array (int lowbound
, int highbound
, struct value
**elemvec
)
1007 unsigned int typelength
;
1009 struct type
*rangetype
;
1010 struct type
*arraytype
;
1013 /* Validate that the bounds are reasonable and that each of the elements
1014 have the same size. */
1016 nelem
= highbound
- lowbound
+ 1;
1019 error (_("bad array bounds (%d, %d)"), lowbound
, highbound
);
1021 typelength
= TYPE_LENGTH (value_enclosing_type (elemvec
[0]));
1022 for (idx
= 1; idx
< nelem
; idx
++)
1024 if (TYPE_LENGTH (value_enclosing_type (elemvec
[idx
])) != typelength
)
1026 error (_("array elements must all be the same size"));
1030 rangetype
= create_range_type ((struct type
*) NULL
, builtin_type_int
,
1031 lowbound
, highbound
);
1032 arraytype
= create_array_type ((struct type
*) NULL
,
1033 value_enclosing_type (elemvec
[0]), rangetype
);
1035 if (!current_language
->c_style_arrays
)
1037 val
= allocate_value (arraytype
);
1038 for (idx
= 0; idx
< nelem
; idx
++)
1040 memcpy (value_contents_all_raw (val
) + (idx
* typelength
),
1041 value_contents_all (elemvec
[idx
]),
1047 /* Allocate space to store the array in the inferior, and then initialize
1048 it by copying in each element. FIXME: Is it worth it to create a
1049 local buffer in which to collect each value and then write all the
1050 bytes in one operation? */
1052 addr
= allocate_space_in_inferior (nelem
* typelength
);
1053 for (idx
= 0; idx
< nelem
; idx
++)
1055 write_memory (addr
+ (idx
* typelength
),
1056 value_contents_all (elemvec
[idx
]),
1060 /* Create the array type and set up an array value to be evaluated lazily. */
1062 val
= value_at_lazy (arraytype
, addr
);
1066 /* Create a value for a string constant by allocating space in the inferior,
1067 copying the data into that space, and returning the address with type
1068 TYPE_CODE_STRING. PTR points to the string constant data; LEN is number
1070 Note that string types are like array of char types with a lower bound of
1071 zero and an upper bound of LEN - 1. Also note that the string may contain
1072 embedded null bytes. */
1075 value_string (char *ptr
, int len
)
1078 int lowbound
= current_language
->string_lower_bound
;
1079 struct type
*rangetype
= create_range_type ((struct type
*) NULL
,
1081 lowbound
, len
+ lowbound
- 1);
1082 struct type
*stringtype
1083 = create_string_type ((struct type
*) NULL
, rangetype
);
1086 if (current_language
->c_style_arrays
== 0)
1088 val
= allocate_value (stringtype
);
1089 memcpy (value_contents_raw (val
), ptr
, len
);
1094 /* Allocate space to store the string in the inferior, and then
1095 copy LEN bytes from PTR in gdb to that address in the inferior. */
1097 addr
= allocate_space_in_inferior (len
);
1098 write_memory (addr
, (gdb_byte
*) ptr
, len
);
1100 val
= value_at_lazy (stringtype
, addr
);
1105 value_bitstring (char *ptr
, int len
)
1108 struct type
*domain_type
= create_range_type (NULL
, builtin_type_int
,
1110 struct type
*type
= create_set_type ((struct type
*) NULL
, domain_type
);
1111 TYPE_CODE (type
) = TYPE_CODE_BITSTRING
;
1112 val
= allocate_value (type
);
1113 memcpy (value_contents_raw (val
), ptr
, TYPE_LENGTH (type
));
1117 /* See if we can pass arguments in T2 to a function which takes arguments
1118 of types T1. T1 is a list of NARGS arguments, and T2 is a NULL-terminated
1119 vector. If some arguments need coercion of some sort, then the coerced
1120 values are written into T2. Return value is 0 if the arguments could be
1121 matched, or the position at which they differ if not.
1123 STATICP is nonzero if the T1 argument list came from a
1124 static member function. T2 will still include the ``this'' pointer,
1125 but it will be skipped.
1127 For non-static member functions, we ignore the first argument,
1128 which is the type of the instance variable. This is because we want
1129 to handle calls with objects from derived classes. This is not
1130 entirely correct: we should actually check to make sure that a
1131 requested operation is type secure, shouldn't we? FIXME. */
1134 typecmp (int staticp
, int varargs
, int nargs
,
1135 struct field t1
[], struct value
*t2
[])
1140 internal_error (__FILE__
, __LINE__
, _("typecmp: no argument list"));
1142 /* Skip ``this'' argument if applicable. T2 will always include THIS. */
1147 (i
< nargs
) && TYPE_CODE (t1
[i
].type
) != TYPE_CODE_VOID
;
1150 struct type
*tt1
, *tt2
;
1155 tt1
= check_typedef (t1
[i
].type
);
1156 tt2
= check_typedef (value_type (t2
[i
]));
1158 if (TYPE_CODE (tt1
) == TYPE_CODE_REF
1159 /* We should be doing hairy argument matching, as below. */
1160 && (TYPE_CODE (check_typedef (TYPE_TARGET_TYPE (tt1
))) == TYPE_CODE (tt2
)))
1162 if (TYPE_CODE (tt2
) == TYPE_CODE_ARRAY
)
1163 t2
[i
] = value_coerce_array (t2
[i
]);
1165 t2
[i
] = value_ref (t2
[i
]);
1169 /* djb - 20000715 - Until the new type structure is in the
1170 place, and we can attempt things like implicit conversions,
1171 we need to do this so you can take something like a map<const
1172 char *>, and properly access map["hello"], because the
1173 argument to [] will be a reference to a pointer to a char,
1174 and the argument will be a pointer to a char. */
1175 while ( TYPE_CODE(tt1
) == TYPE_CODE_REF
||
1176 TYPE_CODE (tt1
) == TYPE_CODE_PTR
)
1178 tt1
= check_typedef( TYPE_TARGET_TYPE(tt1
) );
1180 while ( TYPE_CODE(tt2
) == TYPE_CODE_ARRAY
||
1181 TYPE_CODE(tt2
) == TYPE_CODE_PTR
||
1182 TYPE_CODE(tt2
) == TYPE_CODE_REF
)
1184 tt2
= check_typedef( TYPE_TARGET_TYPE(tt2
) );
1186 if (TYPE_CODE (tt1
) == TYPE_CODE (tt2
))
1188 /* Array to pointer is a `trivial conversion' according to the ARM. */
1190 /* We should be doing much hairier argument matching (see section 13.2
1191 of the ARM), but as a quick kludge, just check for the same type
1193 if (TYPE_CODE (t1
[i
].type
) != TYPE_CODE (value_type (t2
[i
])))
1196 if (varargs
|| t2
[i
] == NULL
)
1201 /* Helper function used by value_struct_elt to recurse through baseclasses.
1202 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1203 and search in it assuming it has (class) type TYPE.
1204 If found, return value, else return NULL.
1206 If LOOKING_FOR_BASECLASS, then instead of looking for struct fields,
1207 look for a baseclass named NAME. */
1209 static struct value
*
1210 search_struct_field (char *name
, struct value
*arg1
, int offset
,
1211 struct type
*type
, int looking_for_baseclass
)
1214 int nbases
= TYPE_N_BASECLASSES (type
);
1216 CHECK_TYPEDEF (type
);
1218 if (!looking_for_baseclass
)
1219 for (i
= TYPE_NFIELDS (type
) - 1; i
>= nbases
; i
--)
1221 char *t_field_name
= TYPE_FIELD_NAME (type
, i
);
1223 if (t_field_name
&& (strcmp_iw (t_field_name
, name
) == 0))
1226 if (TYPE_FIELD_STATIC (type
, i
))
1228 v
= value_static_field (type
, i
);
1230 error (_("field %s is nonexistent or has been optimised out"),
1235 v
= value_primitive_field (arg1
, offset
, i
, type
);
1237 error (_("there is no field named %s"), name
);
1243 && (t_field_name
[0] == '\0'
1244 || (TYPE_CODE (type
) == TYPE_CODE_UNION
1245 && (strcmp_iw (t_field_name
, "else") == 0))))
1247 struct type
*field_type
= TYPE_FIELD_TYPE (type
, i
);
1248 if (TYPE_CODE (field_type
) == TYPE_CODE_UNION
1249 || TYPE_CODE (field_type
) == TYPE_CODE_STRUCT
)
1251 /* Look for a match through the fields of an anonymous union,
1252 or anonymous struct. C++ provides anonymous unions.
1254 In the GNU Chill (now deleted from GDB)
1255 implementation of variant record types, each
1256 <alternative field> has an (anonymous) union type,
1257 each member of the union represents a <variant
1258 alternative>. Each <variant alternative> is
1259 represented as a struct, with a member for each
1263 int new_offset
= offset
;
1265 /* This is pretty gross. In G++, the offset in an
1266 anonymous union is relative to the beginning of the
1267 enclosing struct. In the GNU Chill (now deleted
1268 from GDB) implementation of variant records, the
1269 bitpos is zero in an anonymous union field, so we
1270 have to add the offset of the union here. */
1271 if (TYPE_CODE (field_type
) == TYPE_CODE_STRUCT
1272 || (TYPE_NFIELDS (field_type
) > 0
1273 && TYPE_FIELD_BITPOS (field_type
, 0) == 0))
1274 new_offset
+= TYPE_FIELD_BITPOS (type
, i
) / 8;
1276 v
= search_struct_field (name
, arg1
, new_offset
, field_type
,
1277 looking_for_baseclass
);
1284 for (i
= 0; i
< nbases
; i
++)
1287 struct type
*basetype
= check_typedef (TYPE_BASECLASS (type
, i
));
1288 /* If we are looking for baseclasses, this is what we get when we
1289 hit them. But it could happen that the base part's member name
1290 is not yet filled in. */
1291 int found_baseclass
= (looking_for_baseclass
1292 && TYPE_BASECLASS_NAME (type
, i
) != NULL
1293 && (strcmp_iw (name
, TYPE_BASECLASS_NAME (type
, i
)) == 0));
1295 if (BASETYPE_VIA_VIRTUAL (type
, i
))
1298 struct value
*v2
= allocate_value (basetype
);
1300 boffset
= baseclass_offset (type
, i
,
1301 value_contents (arg1
) + offset
,
1302 VALUE_ADDRESS (arg1
)
1303 + value_offset (arg1
) + offset
);
1305 error (_("virtual baseclass botch"));
1307 /* The virtual base class pointer might have been clobbered by the
1308 user program. Make sure that it still points to a valid memory
1312 if (boffset
< 0 || boffset
>= TYPE_LENGTH (type
))
1314 CORE_ADDR base_addr
;
1316 base_addr
= VALUE_ADDRESS (arg1
) + value_offset (arg1
) + boffset
;
1317 if (target_read_memory (base_addr
, value_contents_raw (v2
),
1318 TYPE_LENGTH (basetype
)) != 0)
1319 error (_("virtual baseclass botch"));
1320 VALUE_LVAL (v2
) = lval_memory
;
1321 VALUE_ADDRESS (v2
) = base_addr
;
1325 VALUE_LVAL (v2
) = VALUE_LVAL (arg1
);
1326 VALUE_ADDRESS (v2
) = VALUE_ADDRESS (arg1
);
1327 VALUE_FRAME_ID (v2
) = VALUE_FRAME_ID (arg1
);
1328 set_value_offset (v2
, value_offset (arg1
) + boffset
);
1329 if (value_lazy (arg1
))
1330 set_value_lazy (v2
, 1);
1332 memcpy (value_contents_raw (v2
),
1333 value_contents_raw (arg1
) + boffset
,
1334 TYPE_LENGTH (basetype
));
1337 if (found_baseclass
)
1339 v
= search_struct_field (name
, v2
, 0, TYPE_BASECLASS (type
, i
),
1340 looking_for_baseclass
);
1342 else if (found_baseclass
)
1343 v
= value_primitive_field (arg1
, offset
, i
, type
);
1345 v
= search_struct_field (name
, arg1
,
1346 offset
+ TYPE_BASECLASS_BITPOS (type
, i
) / 8,
1347 basetype
, looking_for_baseclass
);
1355 /* Return the offset (in bytes) of the virtual base of type BASETYPE
1356 * in an object pointed to by VALADDR (on the host), assumed to be of
1357 * type TYPE. OFFSET is number of bytes beyond start of ARG to start
1358 * looking (in case VALADDR is the contents of an enclosing object).
1360 * This routine recurses on the primary base of the derived class because
1361 * the virtual base entries of the primary base appear before the other
1362 * virtual base entries.
1364 * If the virtual base is not found, a negative integer is returned.
1365 * The magnitude of the negative integer is the number of entries in
1366 * the virtual table to skip over (entries corresponding to various
1367 * ancestral classes in the chain of primary bases).
1369 * Important: This assumes the HP / Taligent C++ runtime
1370 * conventions. Use baseclass_offset() instead to deal with g++
1374 find_rt_vbase_offset (struct type
*type
, struct type
*basetype
,
1375 const gdb_byte
*valaddr
, int offset
, int *boffset_p
,
1378 int boffset
; /* offset of virtual base */
1379 int index
; /* displacement to use in virtual table */
1383 CORE_ADDR vtbl
; /* the virtual table pointer */
1384 struct type
*pbc
; /* the primary base class */
1386 /* Look for the virtual base recursively in the primary base, first.
1387 * This is because the derived class object and its primary base
1388 * subobject share the primary virtual table. */
1391 pbc
= TYPE_PRIMARY_BASE (type
);
1394 find_rt_vbase_offset (pbc
, basetype
, valaddr
, offset
, &boffset
, &skip
);
1397 *boffset_p
= boffset
;
1406 /* Find the index of the virtual base according to HP/Taligent
1407 runtime spec. (Depth-first, left-to-right.) */
1408 index
= virtual_base_index_skip_primaries (basetype
, type
);
1412 *skip_p
= skip
+ virtual_base_list_length_skip_primaries (type
);
1417 /* pai: FIXME -- 32x64 possible problem */
1418 /* First word (4 bytes) in object layout is the vtable pointer */
1419 vtbl
= *(CORE_ADDR
*) (valaddr
+ offset
);
1421 /* Before the constructor is invoked, things are usually zero'd out. */
1423 error (_("Couldn't find virtual table -- object may not be constructed yet."));
1426 /* Find virtual base's offset -- jump over entries for primary base
1427 * ancestors, then use the index computed above. But also adjust by
1428 * HP_ACC_VBASE_START for the vtable slots before the start of the
1429 * virtual base entries. Offset is negative -- virtual base entries
1430 * appear _before_ the address point of the virtual table. */
1432 /* pai: FIXME -- 32x64 problem, if word = 8 bytes, change multiplier
1435 /* epstein : FIXME -- added param for overlay section. May not be correct */
1436 vp
= value_at (builtin_type_int
, vtbl
+ 4 * (-skip
- index
- HP_ACC_VBASE_START
));
1437 boffset
= value_as_long (vp
);
1439 *boffset_p
= boffset
;
1444 /* Helper function used by value_struct_elt to recurse through baseclasses.
1445 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1446 and search in it assuming it has (class) type TYPE.
1447 If found, return value, else if name matched and args not return (value)-1,
1448 else return NULL. */
1450 static struct value
*
1451 search_struct_method (char *name
, struct value
**arg1p
,
1452 struct value
**args
, int offset
,
1453 int *static_memfuncp
, struct type
*type
)
1457 int name_matched
= 0;
1458 char dem_opname
[64];
1460 CHECK_TYPEDEF (type
);
1461 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; i
--)
1463 char *t_field_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
1464 /* FIXME! May need to check for ARM demangling here */
1465 if (strncmp (t_field_name
, "__", 2) == 0 ||
1466 strncmp (t_field_name
, "op", 2) == 0 ||
1467 strncmp (t_field_name
, "type", 4) == 0)
1469 if (cplus_demangle_opname (t_field_name
, dem_opname
, DMGL_ANSI
))
1470 t_field_name
= dem_opname
;
1471 else if (cplus_demangle_opname (t_field_name
, dem_opname
, 0))
1472 t_field_name
= dem_opname
;
1474 if (t_field_name
&& (strcmp_iw (t_field_name
, name
) == 0))
1476 int j
= TYPE_FN_FIELDLIST_LENGTH (type
, i
) - 1;
1477 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
1480 check_stub_method_group (type
, i
);
1481 if (j
> 0 && args
== 0)
1482 error (_("cannot resolve overloaded method `%s': no arguments supplied"), name
);
1483 else if (j
== 0 && args
== 0)
1485 v
= value_fn_field (arg1p
, f
, j
, type
, offset
);
1492 if (!typecmp (TYPE_FN_FIELD_STATIC_P (f
, j
),
1493 TYPE_VARARGS (TYPE_FN_FIELD_TYPE (f
, j
)),
1494 TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f
, j
)),
1495 TYPE_FN_FIELD_ARGS (f
, j
), args
))
1497 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
1498 return value_virtual_fn_field (arg1p
, f
, j
, type
, offset
);
1499 if (TYPE_FN_FIELD_STATIC_P (f
, j
) && static_memfuncp
)
1500 *static_memfuncp
= 1;
1501 v
= value_fn_field (arg1p
, f
, j
, type
, offset
);
1510 for (i
= TYPE_N_BASECLASSES (type
) - 1; i
>= 0; i
--)
1514 if (BASETYPE_VIA_VIRTUAL (type
, i
))
1516 if (TYPE_HAS_VTABLE (type
))
1518 /* HP aCC compiled type, search for virtual base offset
1519 according to HP/Taligent runtime spec. */
1521 find_rt_vbase_offset (type
, TYPE_BASECLASS (type
, i
),
1522 value_contents_all (*arg1p
),
1523 offset
+ value_embedded_offset (*arg1p
),
1524 &base_offset
, &skip
);
1526 error (_("Virtual base class offset not found in vtable"));
1530 struct type
*baseclass
= check_typedef (TYPE_BASECLASS (type
, i
));
1531 const gdb_byte
*base_valaddr
;
1533 /* The virtual base class pointer might have been clobbered by the
1534 user program. Make sure that it still points to a valid memory
1537 if (offset
< 0 || offset
>= TYPE_LENGTH (type
))
1539 gdb_byte
*tmp
= alloca (TYPE_LENGTH (baseclass
));
1540 if (target_read_memory (VALUE_ADDRESS (*arg1p
)
1541 + value_offset (*arg1p
) + offset
,
1542 tmp
, TYPE_LENGTH (baseclass
)) != 0)
1543 error (_("virtual baseclass botch"));
1547 base_valaddr
= value_contents (*arg1p
) + offset
;
1550 baseclass_offset (type
, i
, base_valaddr
,
1551 VALUE_ADDRESS (*arg1p
)
1552 + value_offset (*arg1p
) + offset
);
1553 if (base_offset
== -1)
1554 error (_("virtual baseclass botch"));
1559 base_offset
= TYPE_BASECLASS_BITPOS (type
, i
) / 8;
1561 v
= search_struct_method (name
, arg1p
, args
, base_offset
+ offset
,
1562 static_memfuncp
, TYPE_BASECLASS (type
, i
));
1563 if (v
== (struct value
*) - 1)
1569 /* FIXME-bothner: Why is this commented out? Why is it here? */
1570 /* *arg1p = arg1_tmp; */
1575 return (struct value
*) - 1;
1580 /* Given *ARGP, a value of type (pointer to a)* structure/union,
1581 extract the component named NAME from the ultimate target structure/union
1582 and return it as a value with its appropriate type.
1583 ERR is used in the error message if *ARGP's type is wrong.
1585 C++: ARGS is a list of argument types to aid in the selection of
1586 an appropriate method. Also, handle derived types.
1588 STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
1589 where the truthvalue of whether the function that was resolved was
1590 a static member function or not is stored.
1592 ERR is an error message to be printed in case the field is not found. */
1595 value_struct_elt (struct value
**argp
, struct value
**args
,
1596 char *name
, int *static_memfuncp
, char *err
)
1601 *argp
= coerce_array (*argp
);
1603 t
= check_typedef (value_type (*argp
));
1605 /* Follow pointers until we get to a non-pointer. */
1607 while (TYPE_CODE (t
) == TYPE_CODE_PTR
|| TYPE_CODE (t
) == TYPE_CODE_REF
)
1609 *argp
= value_ind (*argp
);
1610 /* Don't coerce fn pointer to fn and then back again! */
1611 if (TYPE_CODE (value_type (*argp
)) != TYPE_CODE_FUNC
)
1612 *argp
= coerce_array (*argp
);
1613 t
= check_typedef (value_type (*argp
));
1616 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
1617 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
1618 error (_("Attempt to extract a component of a value that is not a %s."), err
);
1620 /* Assume it's not, unless we see that it is. */
1621 if (static_memfuncp
)
1622 *static_memfuncp
= 0;
1626 /* if there are no arguments ...do this... */
1628 /* Try as a field first, because if we succeed, there
1629 is less work to be done. */
1630 v
= search_struct_field (name
, *argp
, 0, t
, 0);
1634 /* C++: If it was not found as a data field, then try to
1635 return it as a pointer to a method. */
1637 if (destructor_name_p (name
, t
))
1638 error (_("Cannot get value of destructor"));
1640 v
= search_struct_method (name
, argp
, args
, 0, static_memfuncp
, t
);
1642 if (v
== (struct value
*) - 1)
1643 error (_("Cannot take address of method %s."), name
);
1646 if (TYPE_NFN_FIELDS (t
))
1647 error (_("There is no member or method named %s."), name
);
1649 error (_("There is no member named %s."), name
);
1654 if (destructor_name_p (name
, t
))
1658 /* Destructors are a special case. */
1659 int m_index
, f_index
;
1662 if (get_destructor_fn_field (t
, &m_index
, &f_index
))
1664 v
= value_fn_field (NULL
, TYPE_FN_FIELDLIST1 (t
, m_index
),
1668 error (_("could not find destructor function named %s."), name
);
1674 error (_("destructor should not have any argument"));
1678 v
= search_struct_method (name
, argp
, args
, 0, static_memfuncp
, t
);
1680 if (v
== (struct value
*) - 1)
1682 error (_("One of the arguments you tried to pass to %s could not be converted to what the function wants."), name
);
1686 /* See if user tried to invoke data as function. If so,
1687 hand it back. If it's not callable (i.e., a pointer to function),
1688 gdb should give an error. */
1689 v
= search_struct_field (name
, *argp
, 0, t
, 0);
1693 error (_("Structure has no component named %s."), name
);
1697 /* Search through the methods of an object (and its bases)
1698 * to find a specified method. Return the pointer to the
1699 * fn_field list of overloaded instances.
1700 * Helper function for value_find_oload_list.
1701 * ARGP is a pointer to a pointer to a value (the object)
1702 * METHOD is a string containing the method name
1703 * OFFSET is the offset within the value
1704 * TYPE is the assumed type of the object
1705 * NUM_FNS is the number of overloaded instances
1706 * BASETYPE is set to the actual type of the subobject where the method is found
1707 * BOFFSET is the offset of the base subobject where the method is found */
1709 static struct fn_field
*
1710 find_method_list (struct value
**argp
, char *method
, int offset
,
1711 struct type
*type
, int *num_fns
,
1712 struct type
**basetype
, int *boffset
)
1716 CHECK_TYPEDEF (type
);
1720 /* First check in object itself */
1721 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; i
--)
1723 /* pai: FIXME What about operators and type conversions? */
1724 char *fn_field_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
1725 if (fn_field_name
&& (strcmp_iw (fn_field_name
, method
) == 0))
1727 int len
= TYPE_FN_FIELDLIST_LENGTH (type
, i
);
1728 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
1734 /* Resolve any stub methods. */
1735 check_stub_method_group (type
, i
);
1741 /* Not found in object, check in base subobjects */
1742 for (i
= TYPE_N_BASECLASSES (type
) - 1; i
>= 0; i
--)
1745 if (BASETYPE_VIA_VIRTUAL (type
, i
))
1747 if (TYPE_HAS_VTABLE (type
))
1749 /* HP aCC compiled type, search for virtual base offset
1750 * according to HP/Taligent runtime spec. */
1752 find_rt_vbase_offset (type
, TYPE_BASECLASS (type
, i
),
1753 value_contents_all (*argp
),
1754 offset
+ value_embedded_offset (*argp
),
1755 &base_offset
, &skip
);
1757 error (_("Virtual base class offset not found in vtable"));
1761 /* probably g++ runtime model */
1762 base_offset
= value_offset (*argp
) + offset
;
1764 baseclass_offset (type
, i
,
1765 value_contents (*argp
) + base_offset
,
1766 VALUE_ADDRESS (*argp
) + base_offset
);
1767 if (base_offset
== -1)
1768 error (_("virtual baseclass botch"));
1772 /* non-virtual base, simply use bit position from debug info */
1774 base_offset
= TYPE_BASECLASS_BITPOS (type
, i
) / 8;
1776 f
= find_method_list (argp
, method
, base_offset
+ offset
,
1777 TYPE_BASECLASS (type
, i
), num_fns
, basetype
,
1785 /* Return the list of overloaded methods of a specified name.
1786 * ARGP is a pointer to a pointer to a value (the object)
1787 * METHOD is the method name
1788 * OFFSET is the offset within the value contents
1789 * NUM_FNS is the number of overloaded instances
1790 * BASETYPE is set to the type of the base subobject that defines the method
1791 * BOFFSET is the offset of the base subobject which defines the method */
1794 value_find_oload_method_list (struct value
**argp
, char *method
, int offset
,
1795 int *num_fns
, struct type
**basetype
,
1800 t
= check_typedef (value_type (*argp
));
1802 /* code snarfed from value_struct_elt */
1803 while (TYPE_CODE (t
) == TYPE_CODE_PTR
|| TYPE_CODE (t
) == TYPE_CODE_REF
)
1805 *argp
= value_ind (*argp
);
1806 /* Don't coerce fn pointer to fn and then back again! */
1807 if (TYPE_CODE (value_type (*argp
)) != TYPE_CODE_FUNC
)
1808 *argp
= coerce_array (*argp
);
1809 t
= check_typedef (value_type (*argp
));
1812 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
1813 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
1814 error (_("Attempt to extract a component of a value that is not a struct or union"));
1816 return find_method_list (argp
, method
, 0, t
, num_fns
, basetype
, boffset
);
1819 /* Given an array of argument types (ARGTYPES) (which includes an
1820 entry for "this" in the case of C++ methods), the number of
1821 arguments NARGS, the NAME of a function whether it's a method or
1822 not (METHOD), and the degree of laxness (LAX) in conforming to
1823 overload resolution rules in ANSI C++, find the best function that
1824 matches on the argument types according to the overload resolution
1827 In the case of class methods, the parameter OBJ is an object value
1828 in which to search for overloaded methods.
1830 In the case of non-method functions, the parameter FSYM is a symbol
1831 corresponding to one of the overloaded functions.
1833 Return value is an integer: 0 -> good match, 10 -> debugger applied
1834 non-standard coercions, 100 -> incompatible.
1836 If a method is being searched for, VALP will hold the value.
1837 If a non-method is being searched for, SYMP will hold the symbol for it.
1839 If a method is being searched for, and it is a static method,
1840 then STATICP will point to a non-zero value.
1842 Note: This function does *not* check the value of
1843 overload_resolution. Caller must check it to see whether overload
1844 resolution is permitted.
1848 find_overload_match (struct type
**arg_types
, int nargs
, char *name
, int method
,
1849 int lax
, struct value
**objp
, struct symbol
*fsym
,
1850 struct value
**valp
, struct symbol
**symp
, int *staticp
)
1852 struct value
*obj
= (objp
? *objp
: NULL
);
1854 int oload_champ
; /* Index of best overloaded function */
1856 struct badness_vector
*oload_champ_bv
= NULL
; /* The measure for the current best match */
1858 struct value
*temp
= obj
;
1859 struct fn_field
*fns_ptr
= NULL
; /* For methods, the list of overloaded methods */
1860 struct symbol
**oload_syms
= NULL
; /* For non-methods, the list of overloaded function symbols */
1861 int num_fns
= 0; /* Number of overloaded instances being considered */
1862 struct type
*basetype
= NULL
;
1866 struct cleanup
*old_cleanups
= NULL
;
1868 const char *obj_type_name
= NULL
;
1869 char *func_name
= NULL
;
1870 enum oload_classification match_quality
;
1872 /* Get the list of overloaded methods or functions */
1875 obj_type_name
= TYPE_NAME (value_type (obj
));
1876 /* Hack: evaluate_subexp_standard often passes in a pointer
1877 value rather than the object itself, so try again */
1878 if ((!obj_type_name
|| !*obj_type_name
) &&
1879 (TYPE_CODE (value_type (obj
)) == TYPE_CODE_PTR
))
1880 obj_type_name
= TYPE_NAME (TYPE_TARGET_TYPE (value_type (obj
)));
1882 fns_ptr
= value_find_oload_method_list (&temp
, name
, 0,
1884 &basetype
, &boffset
);
1885 if (!fns_ptr
|| !num_fns
)
1886 error (_("Couldn't find method %s%s%s"),
1888 (obj_type_name
&& *obj_type_name
) ? "::" : "",
1890 /* If we are dealing with stub method types, they should have
1891 been resolved by find_method_list via value_find_oload_method_list
1893 gdb_assert (TYPE_DOMAIN_TYPE (fns_ptr
[0].type
) != NULL
);
1894 oload_champ
= find_oload_champ (arg_types
, nargs
, method
, num_fns
,
1895 fns_ptr
, oload_syms
, &oload_champ_bv
);
1899 const char *qualified_name
= SYMBOL_CPLUS_DEMANGLED_NAME (fsym
);
1901 /* If we have a C++ name, try to extract just the function
1904 func_name
= cp_func_name (qualified_name
);
1906 /* If there was no C++ name, this must be a C-style function.
1907 Just return the same symbol. Do the same if cp_func_name
1908 fails for some reason. */
1909 if (func_name
== NULL
)
1915 old_cleanups
= make_cleanup (xfree
, func_name
);
1916 make_cleanup (xfree
, oload_syms
);
1917 make_cleanup (xfree
, oload_champ_bv
);
1919 oload_champ
= find_oload_champ_namespace (arg_types
, nargs
,
1926 /* Check how bad the best match is. */
1929 = classify_oload_match (oload_champ_bv
, nargs
,
1930 oload_method_static (method
, fns_ptr
,
1933 if (match_quality
== INCOMPATIBLE
)
1936 error (_("Cannot resolve method %s%s%s to any overloaded instance"),
1938 (obj_type_name
&& *obj_type_name
) ? "::" : "",
1941 error (_("Cannot resolve function %s to any overloaded instance"),
1944 else if (match_quality
== NON_STANDARD
)
1947 warning (_("Using non-standard conversion to match method %s%s%s to supplied arguments"),
1949 (obj_type_name
&& *obj_type_name
) ? "::" : "",
1952 warning (_("Using non-standard conversion to match function %s to supplied arguments"),
1958 if (staticp
!= NULL
)
1959 *staticp
= oload_method_static (method
, fns_ptr
, oload_champ
);
1960 if (TYPE_FN_FIELD_VIRTUAL_P (fns_ptr
, oload_champ
))
1961 *valp
= value_virtual_fn_field (&temp
, fns_ptr
, oload_champ
, basetype
, boffset
);
1963 *valp
= value_fn_field (&temp
, fns_ptr
, oload_champ
, basetype
, boffset
);
1967 *symp
= oload_syms
[oload_champ
];
1972 if (TYPE_CODE (value_type (temp
)) != TYPE_CODE_PTR
1973 && TYPE_CODE (value_type (*objp
)) == TYPE_CODE_PTR
)
1975 temp
= value_addr (temp
);
1979 if (old_cleanups
!= NULL
)
1980 do_cleanups (old_cleanups
);
1982 switch (match_quality
)
1988 default: /* STANDARD */
1993 /* Find the best overload match, searching for FUNC_NAME in namespaces
1994 contained in QUALIFIED_NAME until it either finds a good match or
1995 runs out of namespaces. It stores the overloaded functions in
1996 *OLOAD_SYMS, and the badness vector in *OLOAD_CHAMP_BV. The
1997 calling function is responsible for freeing *OLOAD_SYMS and
2001 find_oload_champ_namespace (struct type
**arg_types
, int nargs
,
2002 const char *func_name
,
2003 const char *qualified_name
,
2004 struct symbol
***oload_syms
,
2005 struct badness_vector
**oload_champ_bv
)
2009 find_oload_champ_namespace_loop (arg_types
, nargs
,
2012 oload_syms
, oload_champ_bv
,
2018 /* Helper function for find_oload_champ_namespace; NAMESPACE_LEN is
2019 how deep we've looked for namespaces, and the champ is stored in
2020 OLOAD_CHAMP. The return value is 1 if the champ is a good one, 0
2023 It is the caller's responsibility to free *OLOAD_SYMS and
2027 find_oload_champ_namespace_loop (struct type
**arg_types
, int nargs
,
2028 const char *func_name
,
2029 const char *qualified_name
,
2031 struct symbol
***oload_syms
,
2032 struct badness_vector
**oload_champ_bv
,
2035 int next_namespace_len
= namespace_len
;
2036 int searched_deeper
= 0;
2038 struct cleanup
*old_cleanups
;
2039 int new_oload_champ
;
2040 struct symbol
**new_oload_syms
;
2041 struct badness_vector
*new_oload_champ_bv
;
2042 char *new_namespace
;
2044 if (next_namespace_len
!= 0)
2046 gdb_assert (qualified_name
[next_namespace_len
] == ':');
2047 next_namespace_len
+= 2;
2050 += cp_find_first_component (qualified_name
+ next_namespace_len
);
2052 /* Initialize these to values that can safely be xfree'd. */
2054 *oload_champ_bv
= NULL
;
2056 /* First, see if we have a deeper namespace we can search in. If we
2057 get a good match there, use it. */
2059 if (qualified_name
[next_namespace_len
] == ':')
2061 searched_deeper
= 1;
2063 if (find_oload_champ_namespace_loop (arg_types
, nargs
,
2064 func_name
, qualified_name
,
2066 oload_syms
, oload_champ_bv
,
2073 /* If we reach here, either we're in the deepest namespace or we
2074 didn't find a good match in a deeper namespace. But, in the
2075 latter case, we still have a bad match in a deeper namespace;
2076 note that we might not find any match at all in the current
2077 namespace. (There's always a match in the deepest namespace,
2078 because this overload mechanism only gets called if there's a
2079 function symbol to start off with.) */
2081 old_cleanups
= make_cleanup (xfree
, *oload_syms
);
2082 old_cleanups
= make_cleanup (xfree
, *oload_champ_bv
);
2083 new_namespace
= alloca (namespace_len
+ 1);
2084 strncpy (new_namespace
, qualified_name
, namespace_len
);
2085 new_namespace
[namespace_len
] = '\0';
2086 new_oload_syms
= make_symbol_overload_list (func_name
,
2088 while (new_oload_syms
[num_fns
])
2091 new_oload_champ
= find_oload_champ (arg_types
, nargs
, 0, num_fns
,
2092 NULL
, new_oload_syms
,
2093 &new_oload_champ_bv
);
2095 /* Case 1: We found a good match. Free earlier matches (if any),
2096 and return it. Case 2: We didn't find a good match, but we're
2097 not the deepest function. Then go with the bad match that the
2098 deeper function found. Case 3: We found a bad match, and we're
2099 the deepest function. Then return what we found, even though
2100 it's a bad match. */
2102 if (new_oload_champ
!= -1
2103 && classify_oload_match (new_oload_champ_bv
, nargs
, 0) == STANDARD
)
2105 *oload_syms
= new_oload_syms
;
2106 *oload_champ
= new_oload_champ
;
2107 *oload_champ_bv
= new_oload_champ_bv
;
2108 do_cleanups (old_cleanups
);
2111 else if (searched_deeper
)
2113 xfree (new_oload_syms
);
2114 xfree (new_oload_champ_bv
);
2115 discard_cleanups (old_cleanups
);
2120 gdb_assert (new_oload_champ
!= -1);
2121 *oload_syms
= new_oload_syms
;
2122 *oload_champ
= new_oload_champ
;
2123 *oload_champ_bv
= new_oload_champ_bv
;
2124 discard_cleanups (old_cleanups
);
2129 /* Look for a function to take NARGS args of types ARG_TYPES. Find
2130 the best match from among the overloaded methods or functions
2131 (depending on METHOD) given by FNS_PTR or OLOAD_SYMS, respectively.
2132 The number of methods/functions in the list is given by NUM_FNS.
2133 Return the index of the best match; store an indication of the
2134 quality of the match in OLOAD_CHAMP_BV.
2136 It is the caller's responsibility to free *OLOAD_CHAMP_BV. */
2139 find_oload_champ (struct type
**arg_types
, int nargs
, int method
,
2140 int num_fns
, struct fn_field
*fns_ptr
,
2141 struct symbol
**oload_syms
,
2142 struct badness_vector
**oload_champ_bv
)
2145 struct badness_vector
*bv
; /* A measure of how good an overloaded instance is */
2146 int oload_champ
= -1; /* Index of best overloaded function */
2147 int oload_ambiguous
= 0; /* Current ambiguity state for overload resolution */
2148 /* 0 => no ambiguity, 1 => two good funcs, 2 => incomparable funcs */
2150 *oload_champ_bv
= NULL
;
2152 /* Consider each candidate in turn */
2153 for (ix
= 0; ix
< num_fns
; ix
++)
2156 int static_offset
= oload_method_static (method
, fns_ptr
, ix
);
2158 struct type
**parm_types
;
2162 nparms
= TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (fns_ptr
, ix
));
2166 /* If it's not a method, this is the proper place */
2167 nparms
=TYPE_NFIELDS(SYMBOL_TYPE(oload_syms
[ix
]));
2170 /* Prepare array of parameter types */
2171 parm_types
= (struct type
**) xmalloc (nparms
* (sizeof (struct type
*)));
2172 for (jj
= 0; jj
< nparms
; jj
++)
2173 parm_types
[jj
] = (method
2174 ? (TYPE_FN_FIELD_ARGS (fns_ptr
, ix
)[jj
].type
)
2175 : TYPE_FIELD_TYPE (SYMBOL_TYPE (oload_syms
[ix
]), jj
));
2177 /* Compare parameter types to supplied argument types. Skip THIS for
2179 bv
= rank_function (parm_types
, nparms
, arg_types
+ static_offset
,
2180 nargs
- static_offset
);
2182 if (!*oload_champ_bv
)
2184 *oload_champ_bv
= bv
;
2188 /* See whether current candidate is better or worse than previous best */
2189 switch (compare_badness (bv
, *oload_champ_bv
))
2192 oload_ambiguous
= 1; /* top two contenders are equally good */
2195 oload_ambiguous
= 2; /* incomparable top contenders */
2198 *oload_champ_bv
= bv
; /* new champion, record details */
2199 oload_ambiguous
= 0;
2210 fprintf_filtered (gdb_stderr
,"Overloaded method instance %s, # of parms %d\n", fns_ptr
[ix
].physname
, nparms
);
2212 fprintf_filtered (gdb_stderr
,"Overloaded function instance %s # of parms %d\n", SYMBOL_DEMANGLED_NAME (oload_syms
[ix
]), nparms
);
2213 for (jj
= 0; jj
< nargs
- static_offset
; jj
++)
2214 fprintf_filtered (gdb_stderr
,"...Badness @ %d : %d\n", jj
, bv
->rank
[jj
]);
2215 fprintf_filtered (gdb_stderr
,"Overload resolution champion is %d, ambiguous? %d\n", oload_champ
, oload_ambiguous
);
2222 /* Return 1 if we're looking at a static method, 0 if we're looking at
2223 a non-static method or a function that isn't a method. */
2226 oload_method_static (int method
, struct fn_field
*fns_ptr
, int index
)
2228 if (method
&& TYPE_FN_FIELD_STATIC_P (fns_ptr
, index
))
2234 /* Check how good an overload match OLOAD_CHAMP_BV represents. */
2236 static enum oload_classification
2237 classify_oload_match (struct badness_vector
*oload_champ_bv
,
2243 for (ix
= 1; ix
<= nargs
- static_offset
; ix
++)
2245 if (oload_champ_bv
->rank
[ix
] >= 100)
2246 return INCOMPATIBLE
; /* truly mismatched types */
2247 else if (oload_champ_bv
->rank
[ix
] >= 10)
2248 return NON_STANDARD
; /* non-standard type conversions needed */
2251 return STANDARD
; /* Only standard conversions needed. */
2254 /* C++: return 1 is NAME is a legitimate name for the destructor
2255 of type TYPE. If TYPE does not have a destructor, or
2256 if NAME is inappropriate for TYPE, an error is signaled. */
2258 destructor_name_p (const char *name
, const struct type
*type
)
2260 /* destructors are a special case. */
2264 char *dname
= type_name_no_tag (type
);
2265 char *cp
= strchr (dname
, '<');
2268 /* Do not compare the template part for template classes. */
2270 len
= strlen (dname
);
2273 if (strlen (name
+ 1) != len
|| strncmp (dname
, name
+ 1, len
) != 0)
2274 error (_("name of destructor must equal name of class"));
2281 /* Helper function for check_field: Given TYPE, a structure/union,
2282 return 1 if the component named NAME from the ultimate
2283 target structure/union is defined, otherwise, return 0. */
2286 check_field_in (struct type
*type
, const char *name
)
2290 for (i
= TYPE_NFIELDS (type
) - 1; i
>= TYPE_N_BASECLASSES (type
); i
--)
2292 char *t_field_name
= TYPE_FIELD_NAME (type
, i
);
2293 if (t_field_name
&& (strcmp_iw (t_field_name
, name
) == 0))
2297 /* C++: If it was not found as a data field, then try to
2298 return it as a pointer to a method. */
2300 /* Destructors are a special case. */
2301 if (destructor_name_p (name
, type
))
2303 int m_index
, f_index
;
2305 return get_destructor_fn_field (type
, &m_index
, &f_index
);
2308 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; --i
)
2310 if (strcmp_iw (TYPE_FN_FIELDLIST_NAME (type
, i
), name
) == 0)
2314 for (i
= TYPE_N_BASECLASSES (type
) - 1; i
>= 0; i
--)
2315 if (check_field_in (TYPE_BASECLASS (type
, i
), name
))
2322 /* C++: Given ARG1, a value of type (pointer to a)* structure/union,
2323 return 1 if the component named NAME from the ultimate
2324 target structure/union is defined, otherwise, return 0. */
2327 check_field (struct value
*arg1
, const char *name
)
2331 arg1
= coerce_array (arg1
);
2333 t
= value_type (arg1
);
2335 /* Follow pointers until we get to a non-pointer. */
2340 if (TYPE_CODE (t
) != TYPE_CODE_PTR
&& TYPE_CODE (t
) != TYPE_CODE_REF
)
2342 t
= TYPE_TARGET_TYPE (t
);
2345 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
2346 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
2347 error (_("Internal error: `this' is not an aggregate"));
2349 return check_field_in (t
, name
);
2352 /* C++: Given an aggregate type CURTYPE, and a member name NAME,
2353 return the appropriate member (or the address of the member, if
2354 WANT_ADDRESS). This function is used to resolve user expressions
2355 of the form "DOMAIN::NAME". For more details on what happens, see
2356 the comment before value_struct_elt_for_reference. */
2359 value_aggregate_elt (struct type
*curtype
,
2360 char *name
, int want_address
,
2363 switch (TYPE_CODE (curtype
))
2365 case TYPE_CODE_STRUCT
:
2366 case TYPE_CODE_UNION
:
2367 return value_struct_elt_for_reference (curtype
, 0, curtype
, name
, NULL
,
2368 want_address
, noside
);
2369 case TYPE_CODE_NAMESPACE
:
2370 return value_namespace_elt (curtype
, name
, want_address
, noside
);
2372 internal_error (__FILE__
, __LINE__
,
2373 _("non-aggregate type in value_aggregate_elt"));
2377 /* C++: Given an aggregate type CURTYPE, and a member name NAME,
2378 return the address of this member as a "pointer to member"
2379 type. If INTYPE is non-null, then it will be the type
2380 of the member we are looking for. This will help us resolve
2381 "pointers to member functions". This function is used
2382 to resolve user expressions of the form "DOMAIN::NAME". */
2384 static struct value
*
2385 value_struct_elt_for_reference (struct type
*domain
, int offset
,
2386 struct type
*curtype
, char *name
,
2387 struct type
*intype
, int want_address
,
2390 struct type
*t
= curtype
;
2392 struct value
*v
, *result
;
2394 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
2395 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
2396 error (_("Internal error: non-aggregate type to value_struct_elt_for_reference"));
2398 for (i
= TYPE_NFIELDS (t
) - 1; i
>= TYPE_N_BASECLASSES (t
); i
--)
2400 char *t_field_name
= TYPE_FIELD_NAME (t
, i
);
2402 if (t_field_name
&& strcmp (t_field_name
, name
) == 0)
2404 if (TYPE_FIELD_STATIC (t
, i
))
2406 v
= value_static_field (t
, i
);
2408 error (_("static field %s has been optimized out"),
2414 if (TYPE_FIELD_PACKED (t
, i
))
2415 error (_("pointers to bitfield members not allowed"));
2418 return value_from_longest
2419 (lookup_memberptr_type (TYPE_FIELD_TYPE (t
, i
), domain
),
2420 offset
+ (LONGEST
) (TYPE_FIELD_BITPOS (t
, i
) >> 3));
2421 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2422 return allocate_value (TYPE_FIELD_TYPE (t
, i
));
2424 error (_("Cannot reference non-static field \"%s\""), name
);
2428 /* C++: If it was not found as a data field, then try to
2429 return it as a pointer to a method. */
2431 /* Destructors are a special case. */
2432 if (destructor_name_p (name
, t
))
2434 error (_("member pointers to destructors not implemented yet"));
2437 /* Perform all necessary dereferencing. */
2438 while (intype
&& TYPE_CODE (intype
) == TYPE_CODE_PTR
)
2439 intype
= TYPE_TARGET_TYPE (intype
);
2441 for (i
= TYPE_NFN_FIELDS (t
) - 1; i
>= 0; --i
)
2443 char *t_field_name
= TYPE_FN_FIELDLIST_NAME (t
, i
);
2444 char dem_opname
[64];
2446 if (strncmp (t_field_name
, "__", 2) == 0 ||
2447 strncmp (t_field_name
, "op", 2) == 0 ||
2448 strncmp (t_field_name
, "type", 4) == 0)
2450 if (cplus_demangle_opname (t_field_name
, dem_opname
, DMGL_ANSI
))
2451 t_field_name
= dem_opname
;
2452 else if (cplus_demangle_opname (t_field_name
, dem_opname
, 0))
2453 t_field_name
= dem_opname
;
2455 if (t_field_name
&& strcmp (t_field_name
, name
) == 0)
2457 int j
= TYPE_FN_FIELDLIST_LENGTH (t
, i
);
2458 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (t
, i
);
2460 check_stub_method_group (t
, i
);
2462 if (intype
== 0 && j
> 1)
2463 error (_("non-unique member `%s' requires type instantiation"), name
);
2467 if (TYPE_FN_FIELD_TYPE (f
, j
) == intype
)
2470 error (_("no member function matches that type instantiation"));
2475 if (TYPE_FN_FIELD_STATIC_P (f
, j
))
2477 struct symbol
*s
= lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f
, j
),
2478 0, VAR_DOMAIN
, 0, NULL
);
2483 return value_addr (read_var_value (s
, 0));
2485 return read_var_value (s
, 0);
2488 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
2492 result
= allocate_value
2493 (lookup_methodptr_type (TYPE_FN_FIELD_TYPE (f
, j
)));
2494 cplus_make_method_ptr (value_contents_writeable (result
),
2495 TYPE_FN_FIELD_VOFFSET (f
, j
), 1);
2497 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2498 return allocate_value (TYPE_FN_FIELD_TYPE (f
, j
));
2500 error (_("Cannot reference virtual member function \"%s\""),
2505 struct symbol
*s
= lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f
, j
),
2506 0, VAR_DOMAIN
, 0, NULL
);
2510 v
= read_var_value (s
, 0);
2515 result
= allocate_value (lookup_methodptr_type (TYPE_FN_FIELD_TYPE (f
, j
)));
2516 cplus_make_method_ptr (value_contents_writeable (result
),
2517 VALUE_ADDRESS (v
), 0);
2523 for (i
= TYPE_N_BASECLASSES (t
) - 1; i
>= 0; i
--)
2528 if (BASETYPE_VIA_VIRTUAL (t
, i
))
2531 base_offset
= TYPE_BASECLASS_BITPOS (t
, i
) / 8;
2532 v
= value_struct_elt_for_reference (domain
,
2533 offset
+ base_offset
,
2534 TYPE_BASECLASS (t
, i
),
2536 intype
, want_address
,
2542 /* As a last chance, pretend that CURTYPE is a namespace, and look
2543 it up that way; this (frequently) works for types nested inside
2546 return value_maybe_namespace_elt (curtype
, name
, want_address
, noside
);
2549 /* C++: Return the member NAME of the namespace given by the type
2552 static struct value
*
2553 value_namespace_elt (const struct type
*curtype
,
2554 char *name
, int want_address
,
2557 struct value
*retval
= value_maybe_namespace_elt (curtype
, name
,
2558 want_address
, noside
);
2561 error (_("No symbol \"%s\" in namespace \"%s\"."), name
,
2562 TYPE_TAG_NAME (curtype
));
2567 /* A helper function used by value_namespace_elt and
2568 value_struct_elt_for_reference. It looks up NAME inside the
2569 context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
2570 is a class and NAME refers to a type in CURTYPE itself (as opposed
2571 to, say, some base class of CURTYPE). */
2573 static struct value
*
2574 value_maybe_namespace_elt (const struct type
*curtype
,
2575 char *name
, int want_address
,
2578 const char *namespace_name
= TYPE_TAG_NAME (curtype
);
2580 struct value
*result
;
2582 sym
= cp_lookup_symbol_namespace (namespace_name
, name
, NULL
,
2583 get_selected_block (0), VAR_DOMAIN
,
2588 else if ((noside
== EVAL_AVOID_SIDE_EFFECTS
)
2589 && (SYMBOL_CLASS (sym
) == LOC_TYPEDEF
))
2590 result
= allocate_value (SYMBOL_TYPE (sym
));
2592 result
= value_of_variable (sym
, get_selected_block (0));
2594 if (result
&& want_address
)
2595 result
= value_addr (result
);
2600 /* Given a pointer value V, find the real (RTTI) type
2601 of the object it points to.
2602 Other parameters FULL, TOP, USING_ENC as with value_rtti_type()
2603 and refer to the values computed for the object pointed to. */
2606 value_rtti_target_type (struct value
*v
, int *full
, int *top
, int *using_enc
)
2608 struct value
*target
;
2610 target
= value_ind (v
);
2612 return value_rtti_type (target
, full
, top
, using_enc
);
2615 /* Given a value pointed to by ARGP, check its real run-time type, and
2616 if that is different from the enclosing type, create a new value
2617 using the real run-time type as the enclosing type (and of the same
2618 type as ARGP) and return it, with the embedded offset adjusted to
2619 be the correct offset to the enclosed object
2620 RTYPE is the type, and XFULL, XTOP, and XUSING_ENC are the other
2621 parameters, computed by value_rtti_type(). If these are available,
2622 they can be supplied and a second call to value_rtti_type() is avoided.
2623 (Pass RTYPE == NULL if they're not available */
2626 value_full_object (struct value
*argp
, struct type
*rtype
, int xfull
, int xtop
,
2629 struct type
*real_type
;
2633 struct value
*new_val
;
2640 using_enc
= xusing_enc
;
2643 real_type
= value_rtti_type (argp
, &full
, &top
, &using_enc
);
2645 /* If no RTTI data, or if object is already complete, do nothing */
2646 if (!real_type
|| real_type
== value_enclosing_type (argp
))
2649 /* If we have the full object, but for some reason the enclosing
2650 type is wrong, set it *//* pai: FIXME -- sounds iffy */
2653 argp
= value_change_enclosing_type (argp
, real_type
);
2657 /* Check if object is in memory */
2658 if (VALUE_LVAL (argp
) != lval_memory
)
2660 warning (_("Couldn't retrieve complete object of RTTI type %s; object may be in register(s)."), TYPE_NAME (real_type
));
2665 /* All other cases -- retrieve the complete object */
2666 /* Go back by the computed top_offset from the beginning of the object,
2667 adjusting for the embedded offset of argp if that's what value_rtti_type
2668 used for its computation. */
2669 new_val
= value_at_lazy (real_type
, VALUE_ADDRESS (argp
) - top
+
2670 (using_enc
? 0 : value_embedded_offset (argp
)));
2671 deprecated_set_value_type (new_val
, value_type (argp
));
2672 set_value_embedded_offset (new_val
, (using_enc
2673 ? top
+ value_embedded_offset (argp
)
2681 /* Return the value of the local variable, if one exists.
2682 Flag COMPLAIN signals an error if the request is made in an
2683 inappropriate context. */
2686 value_of_local (const char *name
, int complain
)
2688 struct symbol
*func
, *sym
;
2692 if (deprecated_selected_frame
== 0)
2695 error (_("no frame selected"));
2700 func
= get_frame_function (deprecated_selected_frame
);
2704 error (_("no `%s' in nameless context"), name
);
2709 b
= SYMBOL_BLOCK_VALUE (func
);
2710 if (dict_empty (BLOCK_DICT (b
)))
2713 error (_("no args, no `%s'"), name
);
2718 /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
2719 symbol instead of the LOC_ARG one (if both exist). */
2720 sym
= lookup_block_symbol (b
, name
, NULL
, VAR_DOMAIN
);
2724 error (_("current stack frame does not contain a variable named `%s'"), name
);
2729 ret
= read_var_value (sym
, deprecated_selected_frame
);
2730 if (ret
== 0 && complain
)
2731 error (_("`%s' argument unreadable"), name
);
2735 /* C++/Objective-C: return the value of the class instance variable,
2736 if one exists. Flag COMPLAIN signals an error if the request is
2737 made in an inappropriate context. */
2740 value_of_this (int complain
)
2742 if (current_language
->la_language
== language_objc
)
2743 return value_of_local ("self", complain
);
2745 return value_of_local ("this", complain
);
2748 /* Create a slice (sub-string, sub-array) of ARRAY, that is LENGTH elements
2749 long, starting at LOWBOUND. The result has the same lower bound as
2750 the original ARRAY. */
2753 value_slice (struct value
*array
, int lowbound
, int length
)
2755 struct type
*slice_range_type
, *slice_type
, *range_type
;
2756 LONGEST lowerbound
, upperbound
;
2757 struct value
*slice
;
2758 struct type
*array_type
;
2759 array_type
= check_typedef (value_type (array
));
2760 if (TYPE_CODE (array_type
) != TYPE_CODE_ARRAY
2761 && TYPE_CODE (array_type
) != TYPE_CODE_STRING
2762 && TYPE_CODE (array_type
) != TYPE_CODE_BITSTRING
)
2763 error (_("cannot take slice of non-array"));
2764 range_type
= TYPE_INDEX_TYPE (array_type
);
2765 if (get_discrete_bounds (range_type
, &lowerbound
, &upperbound
) < 0)
2766 error (_("slice from bad array or bitstring"));
2767 if (lowbound
< lowerbound
|| length
< 0
2768 || lowbound
+ length
- 1 > upperbound
)
2769 error (_("slice out of range"));
2770 /* FIXME-type-allocation: need a way to free this type when we are
2772 slice_range_type
= create_range_type ((struct type
*) NULL
,
2773 TYPE_TARGET_TYPE (range_type
),
2774 lowbound
, lowbound
+ length
- 1);
2775 if (TYPE_CODE (array_type
) == TYPE_CODE_BITSTRING
)
2778 slice_type
= create_set_type ((struct type
*) NULL
, slice_range_type
);
2779 TYPE_CODE (slice_type
) = TYPE_CODE_BITSTRING
;
2780 slice
= value_zero (slice_type
, not_lval
);
2781 for (i
= 0; i
< length
; i
++)
2783 int element
= value_bit_index (array_type
,
2784 value_contents (array
),
2787 error (_("internal error accessing bitstring"));
2788 else if (element
> 0)
2790 int j
= i
% TARGET_CHAR_BIT
;
2791 if (BITS_BIG_ENDIAN
)
2792 j
= TARGET_CHAR_BIT
- 1 - j
;
2793 value_contents_raw (slice
)[i
/ TARGET_CHAR_BIT
] |= (1 << j
);
2796 /* We should set the address, bitssize, and bitspos, so the clice
2797 can be used on the LHS, but that may require extensions to
2798 value_assign. For now, just leave as a non_lval. FIXME. */
2802 struct type
*element_type
= TYPE_TARGET_TYPE (array_type
);
2804 = (lowbound
- lowerbound
) * TYPE_LENGTH (check_typedef (element_type
));
2805 slice_type
= create_array_type ((struct type
*) NULL
, element_type
,
2807 TYPE_CODE (slice_type
) = TYPE_CODE (array_type
);
2808 slice
= allocate_value (slice_type
);
2809 if (value_lazy (array
))
2810 set_value_lazy (slice
, 1);
2812 memcpy (value_contents_writeable (slice
),
2813 value_contents (array
) + offset
,
2814 TYPE_LENGTH (slice_type
));
2815 if (VALUE_LVAL (array
) == lval_internalvar
)
2816 VALUE_LVAL (slice
) = lval_internalvar_component
;
2818 VALUE_LVAL (slice
) = VALUE_LVAL (array
);
2819 VALUE_ADDRESS (slice
) = VALUE_ADDRESS (array
);
2820 VALUE_FRAME_ID (slice
) = VALUE_FRAME_ID (array
);
2821 set_value_offset (slice
, value_offset (array
) + offset
);
2826 /* Create a value for a FORTRAN complex number. Currently most of
2827 the time values are coerced to COMPLEX*16 (i.e. a complex number
2828 composed of 2 doubles. This really should be a smarter routine
2829 that figures out precision inteligently as opposed to assuming
2830 doubles. FIXME: fmb */
2833 value_literal_complex (struct value
*arg1
, struct value
*arg2
, struct type
*type
)
2836 struct type
*real_type
= TYPE_TARGET_TYPE (type
);
2838 val
= allocate_value (type
);
2839 arg1
= value_cast (real_type
, arg1
);
2840 arg2
= value_cast (real_type
, arg2
);
2842 memcpy (value_contents_raw (val
),
2843 value_contents (arg1
), TYPE_LENGTH (real_type
));
2844 memcpy (value_contents_raw (val
) + TYPE_LENGTH (real_type
),
2845 value_contents (arg2
), TYPE_LENGTH (real_type
));
2849 /* Cast a value into the appropriate complex data type. */
2851 static struct value
*
2852 cast_into_complex (struct type
*type
, struct value
*val
)
2854 struct type
*real_type
= TYPE_TARGET_TYPE (type
);
2855 if (TYPE_CODE (value_type (val
)) == TYPE_CODE_COMPLEX
)
2857 struct type
*val_real_type
= TYPE_TARGET_TYPE (value_type (val
));
2858 struct value
*re_val
= allocate_value (val_real_type
);
2859 struct value
*im_val
= allocate_value (val_real_type
);
2861 memcpy (value_contents_raw (re_val
),
2862 value_contents (val
), TYPE_LENGTH (val_real_type
));
2863 memcpy (value_contents_raw (im_val
),
2864 value_contents (val
) + TYPE_LENGTH (val_real_type
),
2865 TYPE_LENGTH (val_real_type
));
2867 return value_literal_complex (re_val
, im_val
, type
);
2869 else if (TYPE_CODE (value_type (val
)) == TYPE_CODE_FLT
2870 || TYPE_CODE (value_type (val
)) == TYPE_CODE_INT
)
2871 return value_literal_complex (val
, value_zero (real_type
, not_lval
), type
);
2873 error (_("cannot cast non-number to complex"));
2877 _initialize_valops (void)
2879 add_setshow_boolean_cmd ("overload-resolution", class_support
,
2880 &overload_resolution
, _("\
2881 Set overload resolution in evaluating C++ functions."), _("\
2882 Show overload resolution in evaluating C++ functions."), NULL
,
2884 show_overload_resolution
,
2885 &setlist
, &showlist
);
2886 overload_resolution
= 1;