1 /* Perform non-arithmetic operations on values, for GDB.
3 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
5 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 2 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, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
39 #include "dictionary.h"
40 #include "cp-support.h"
43 #include "gdb_string.h"
44 #include "gdb_assert.h"
45 #include "cp-support.h"
48 extern int overload_debug
;
49 /* Local functions. */
51 static int typecmp (int staticp
, int varargs
, int nargs
,
52 struct field t1
[], struct value
*t2
[]);
54 static struct value
*search_struct_field (char *, struct value
*, int,
57 static struct value
*search_struct_method (char *, struct value
**,
59 int, int *, struct type
*);
61 static int find_oload_champ_namespace (struct type
**arg_types
, int nargs
,
62 const char *func_name
,
63 const char *qualified_name
,
64 struct symbol
***oload_syms
,
65 struct badness_vector
**oload_champ_bv
);
68 int find_oload_champ_namespace_loop (struct type
**arg_types
, int nargs
,
69 const char *func_name
,
70 const char *qualified_name
,
72 struct symbol
***oload_syms
,
73 struct badness_vector
**oload_champ_bv
,
76 static int find_oload_champ (struct type
**arg_types
, int nargs
, int method
,
78 struct fn_field
*fns_ptr
,
79 struct symbol
**oload_syms
,
80 struct badness_vector
**oload_champ_bv
);
82 static int oload_method_static (int method
, struct fn_field
*fns_ptr
,
85 enum oload_classification
{ STANDARD
, NON_STANDARD
, INCOMPATIBLE
};
88 oload_classification
classify_oload_match (struct badness_vector
93 static int check_field_in (struct type
*, const char *);
95 static struct value
*value_struct_elt_for_reference (struct type
*domain
,
102 static struct value
*value_namespace_elt (const struct type
*curtype
,
106 static struct value
*value_maybe_namespace_elt (const struct type
*curtype
,
110 static CORE_ADDR
allocate_space_in_inferior (int);
112 static struct value
*cast_into_complex (struct type
*, struct value
*);
114 static struct fn_field
*find_method_list (struct value
** argp
, char *method
,
116 struct type
*type
, int *num_fns
,
117 struct type
**basetype
,
120 void _initialize_valops (void);
122 /* Flag for whether we want to abandon failed expression evals by default. */
125 static int auto_abandon
= 0;
128 int overload_resolution
= 0;
130 /* Find the address of function name NAME in the inferior. */
133 find_function_in_inferior (const char *name
)
136 sym
= lookup_symbol (name
, 0, VAR_DOMAIN
, 0, NULL
);
139 if (SYMBOL_CLASS (sym
) != LOC_BLOCK
)
141 error ("\"%s\" exists in this program but is not a function.",
144 return value_of_variable (sym
, NULL
);
148 struct minimal_symbol
*msymbol
= lookup_minimal_symbol (name
, NULL
, NULL
);
153 type
= lookup_pointer_type (builtin_type_char
);
154 type
= lookup_function_type (type
);
155 type
= lookup_pointer_type (type
);
156 maddr
= SYMBOL_VALUE_ADDRESS (msymbol
);
157 return value_from_pointer (type
, maddr
);
161 if (!target_has_execution
)
162 error ("evaluation of this expression requires the target program to be active");
164 error ("evaluation of this expression requires the program to have a function \"%s\".", name
);
169 /* Allocate NBYTES of space in the inferior using the inferior's malloc
170 and return a value that is a pointer to the allocated space. */
173 value_allocate_space_in_inferior (int len
)
175 struct value
*blocklen
;
176 struct value
*val
= find_function_in_inferior (NAME_OF_MALLOC
);
178 blocklen
= value_from_longest (builtin_type_int
, (LONGEST
) len
);
179 val
= call_function_by_hand (val
, 1, &blocklen
);
180 if (value_logical_not (val
))
182 if (!target_has_execution
)
183 error ("No memory available to program now: you need to start the target first");
185 error ("No memory available to program: call to malloc failed");
191 allocate_space_in_inferior (int len
)
193 return value_as_long (value_allocate_space_in_inferior (len
));
196 /* Cast value ARG2 to type TYPE and return as a value.
197 More general than a C cast: accepts any two types of the same length,
198 and if ARG2 is an lvalue it can be cast into anything at all. */
199 /* In C++, casts may change pointer or object representations. */
202 value_cast (struct type
*type
, struct value
*arg2
)
204 enum type_code code1
;
205 enum type_code code2
;
209 int convert_to_boolean
= 0;
211 if (value_type (arg2
) == type
)
214 CHECK_TYPEDEF (type
);
215 code1
= TYPE_CODE (type
);
216 arg2
= coerce_ref (arg2
);
217 type2
= check_typedef (value_type (arg2
));
219 /* A cast to an undetermined-length array_type, such as (TYPE [])OBJECT,
220 is treated like a cast to (TYPE [N])OBJECT,
221 where N is sizeof(OBJECT)/sizeof(TYPE). */
222 if (code1
== TYPE_CODE_ARRAY
)
224 struct type
*element_type
= TYPE_TARGET_TYPE (type
);
225 unsigned element_length
= TYPE_LENGTH (check_typedef (element_type
));
226 if (element_length
> 0
227 && TYPE_ARRAY_UPPER_BOUND_TYPE (type
) == BOUND_CANNOT_BE_DETERMINED
)
229 struct type
*range_type
= TYPE_INDEX_TYPE (type
);
230 int val_length
= TYPE_LENGTH (type2
);
231 LONGEST low_bound
, high_bound
, new_length
;
232 if (get_discrete_bounds (range_type
, &low_bound
, &high_bound
) < 0)
233 low_bound
= 0, high_bound
= 0;
234 new_length
= val_length
/ element_length
;
235 if (val_length
% element_length
!= 0)
236 warning ("array element type size does not divide object size in cast");
237 /* FIXME-type-allocation: need a way to free this type when we are
239 range_type
= create_range_type ((struct type
*) NULL
,
240 TYPE_TARGET_TYPE (range_type
),
242 new_length
+ low_bound
- 1);
243 arg2
->type
= create_array_type ((struct type
*) NULL
,
244 element_type
, range_type
);
249 if (current_language
->c_style_arrays
250 && TYPE_CODE (type2
) == TYPE_CODE_ARRAY
)
251 arg2
= value_coerce_array (arg2
);
253 if (TYPE_CODE (type2
) == TYPE_CODE_FUNC
)
254 arg2
= value_coerce_function (arg2
);
256 type2
= check_typedef (value_type (arg2
));
257 code2
= TYPE_CODE (type2
);
259 if (code1
== TYPE_CODE_COMPLEX
)
260 return cast_into_complex (type
, arg2
);
261 if (code1
== TYPE_CODE_BOOL
)
263 code1
= TYPE_CODE_INT
;
264 convert_to_boolean
= 1;
266 if (code1
== TYPE_CODE_CHAR
)
267 code1
= TYPE_CODE_INT
;
268 if (code2
== TYPE_CODE_BOOL
|| code2
== TYPE_CODE_CHAR
)
269 code2
= TYPE_CODE_INT
;
271 scalar
= (code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_FLT
272 || code2
== TYPE_CODE_ENUM
|| code2
== TYPE_CODE_RANGE
);
274 if (code1
== TYPE_CODE_STRUCT
275 && code2
== TYPE_CODE_STRUCT
276 && TYPE_NAME (type
) != 0)
278 /* Look in the type of the source to see if it contains the
279 type of the target as a superclass. If so, we'll need to
280 offset the object in addition to changing its type. */
281 struct value
*v
= search_struct_field (type_name_no_tag (type
),
289 if (code1
== TYPE_CODE_FLT
&& scalar
)
290 return value_from_double (type
, value_as_double (arg2
));
291 else if ((code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_ENUM
292 || code1
== TYPE_CODE_RANGE
)
293 && (scalar
|| code2
== TYPE_CODE_PTR
))
297 if (deprecated_hp_som_som_object_present
/* if target compiled by HP aCC */
298 && (code2
== TYPE_CODE_PTR
))
301 struct value
*retvalp
;
303 switch (TYPE_CODE (TYPE_TARGET_TYPE (type2
)))
305 /* With HP aCC, pointers to data members have a bias */
306 case TYPE_CODE_MEMBER
:
307 retvalp
= value_from_longest (type
, value_as_long (arg2
));
308 /* force evaluation */
309 ptr
= (unsigned int *) VALUE_CONTENTS (retvalp
);
310 *ptr
&= ~0x20000000; /* zap 29th bit to remove bias */
313 /* While pointers to methods don't really point to a function */
314 case TYPE_CODE_METHOD
:
315 error ("Pointers to methods not supported with HP aCC");
318 break; /* fall out and go to normal handling */
322 /* When we cast pointers to integers, we mustn't use
323 POINTER_TO_ADDRESS to find the address the pointer
324 represents, as value_as_long would. GDB should evaluate
325 expressions just as the compiler would --- and the compiler
326 sees a cast as a simple reinterpretation of the pointer's
328 if (code2
== TYPE_CODE_PTR
)
329 longest
= extract_unsigned_integer (VALUE_CONTENTS (arg2
),
330 TYPE_LENGTH (type2
));
332 longest
= value_as_long (arg2
);
333 return value_from_longest (type
, convert_to_boolean
?
334 (LONGEST
) (longest
? 1 : 0) : longest
);
336 else if (code1
== TYPE_CODE_PTR
&& (code2
== TYPE_CODE_INT
||
337 code2
== TYPE_CODE_ENUM
||
338 code2
== TYPE_CODE_RANGE
))
340 /* TYPE_LENGTH (type) is the length of a pointer, but we really
341 want the length of an address! -- we are really dealing with
342 addresses (i.e., gdb representations) not pointers (i.e.,
343 target representations) here.
345 This allows things like "print *(int *)0x01000234" to work
346 without printing a misleading message -- which would
347 otherwise occur when dealing with a target having two byte
348 pointers and four byte addresses. */
350 int addr_bit
= TARGET_ADDR_BIT
;
352 LONGEST longest
= value_as_long (arg2
);
353 if (addr_bit
< sizeof (LONGEST
) * HOST_CHAR_BIT
)
355 if (longest
>= ((LONGEST
) 1 << addr_bit
)
356 || longest
<= -((LONGEST
) 1 << addr_bit
))
357 warning ("value truncated");
359 return value_from_longest (type
, longest
);
361 else if (TYPE_LENGTH (type
) == TYPE_LENGTH (type2
))
363 if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_PTR
)
365 struct type
*t1
= check_typedef (TYPE_TARGET_TYPE (type
));
366 struct type
*t2
= check_typedef (TYPE_TARGET_TYPE (type2
));
367 if (TYPE_CODE (t1
) == TYPE_CODE_STRUCT
368 && TYPE_CODE (t2
) == TYPE_CODE_STRUCT
369 && !value_logical_not (arg2
))
373 /* Look in the type of the source to see if it contains the
374 type of the target as a superclass. If so, we'll need to
375 offset the pointer rather than just change its type. */
376 if (TYPE_NAME (t1
) != NULL
)
378 v
= search_struct_field (type_name_no_tag (t1
),
379 value_ind (arg2
), 0, t2
, 1);
388 /* Look in the type of the target to see if it contains the
389 type of the source as a superclass. If so, we'll need to
390 offset the pointer rather than just change its type.
391 FIXME: This fails silently with virtual inheritance. */
392 if (TYPE_NAME (t2
) != NULL
)
394 v
= search_struct_field (type_name_no_tag (t2
),
395 value_zero (t1
, not_lval
), 0, t1
, 1);
398 CORE_ADDR addr2
= value_as_address (arg2
);
399 addr2
-= (VALUE_ADDRESS (v
)
401 + VALUE_EMBEDDED_OFFSET (v
));
402 return value_from_pointer (type
, addr2
);
406 /* No superclass found, just fall through to change ptr type. */
409 arg2
= value_change_enclosing_type (arg2
, type
);
410 VALUE_POINTED_TO_OFFSET (arg2
) = 0; /* pai: chk_val */
413 else if (VALUE_LVAL (arg2
) == lval_memory
)
414 return value_at_lazy (type
, VALUE_ADDRESS (arg2
) + value_offset (arg2
));
415 else if (code1
== TYPE_CODE_VOID
)
417 return value_zero (builtin_type_void
, not_lval
);
421 error ("Invalid cast.");
426 /* Create a value of type TYPE that is zero, and return it. */
429 value_zero (struct type
*type
, enum lval_type lv
)
431 struct value
*val
= allocate_value (type
);
433 memset (VALUE_CONTENTS (val
), 0, TYPE_LENGTH (check_typedef (type
)));
434 VALUE_LVAL (val
) = lv
;
439 /* Return a value with type TYPE located at ADDR.
441 Call value_at only if the data needs to be fetched immediately;
442 if we can be 'lazy' and defer the fetch, perhaps indefinately, call
443 value_at_lazy instead. value_at_lazy simply records the address of
444 the data and sets the lazy-evaluation-required flag. The lazy flag
445 is tested in the VALUE_CONTENTS macro, which is used if and when
446 the contents are actually required.
448 Note: value_at does *NOT* handle embedded offsets; perform such
449 adjustments before or after calling it. */
452 value_at (struct type
*type
, CORE_ADDR addr
)
456 if (TYPE_CODE (check_typedef (type
)) == TYPE_CODE_VOID
)
457 error ("Attempt to dereference a generic pointer.");
459 val
= allocate_value (type
);
461 read_memory (addr
, value_contents_all_raw (val
), TYPE_LENGTH (type
));
463 VALUE_LVAL (val
) = lval_memory
;
464 VALUE_ADDRESS (val
) = addr
;
469 /* Return a lazy value with type TYPE located at ADDR (cf. value_at). */
472 value_at_lazy (struct type
*type
, CORE_ADDR addr
)
476 if (TYPE_CODE (check_typedef (type
)) == TYPE_CODE_VOID
)
477 error ("Attempt to dereference a generic pointer.");
479 val
= allocate_value (type
);
481 VALUE_LVAL (val
) = lval_memory
;
482 VALUE_ADDRESS (val
) = addr
;
483 VALUE_LAZY (val
) = 1;
488 /* Called only from the VALUE_CONTENTS and VALUE_CONTENTS_ALL macros,
489 if the current data for a variable needs to be loaded into
490 VALUE_CONTENTS(VAL). Fetches the data from the user's process, and
491 clears the lazy flag to indicate that the data in the buffer is valid.
493 If the value is zero-length, we avoid calling read_memory, which would
494 abort. We mark the value as fetched anyway -- all 0 bytes of it.
496 This function returns a value because it is used in the VALUE_CONTENTS
497 macro as part of an expression, where a void would not work. The
501 value_fetch_lazy (struct value
*val
)
503 CORE_ADDR addr
= VALUE_ADDRESS (val
) + value_offset (val
);
504 int length
= TYPE_LENGTH (value_enclosing_type (val
));
506 struct type
*type
= value_type (val
);
508 read_memory (addr
, value_contents_all_raw (val
), length
);
510 VALUE_LAZY (val
) = 0;
515 /* Store the contents of FROMVAL into the location of TOVAL.
516 Return a new value with the location of TOVAL and contents of FROMVAL. */
519 value_assign (struct value
*toval
, struct value
*fromval
)
523 struct frame_id old_frame
;
525 if (!toval
->modifiable
)
526 error ("Left operand of assignment is not a modifiable lvalue.");
528 toval
= coerce_ref (toval
);
530 type
= value_type (toval
);
531 if (VALUE_LVAL (toval
) != lval_internalvar
)
532 fromval
= value_cast (type
, fromval
);
534 fromval
= coerce_array (fromval
);
535 CHECK_TYPEDEF (type
);
537 /* Since modifying a register can trash the frame chain, and modifying memory
538 can trash the frame cache, we save the old frame and then restore the new
540 old_frame
= get_frame_id (deprecated_selected_frame
);
542 switch (VALUE_LVAL (toval
))
544 case lval_internalvar
:
545 set_internalvar (VALUE_INTERNALVAR (toval
), fromval
);
546 val
= value_copy (VALUE_INTERNALVAR (toval
)->value
);
547 val
= value_change_enclosing_type (val
, value_enclosing_type (fromval
));
548 VALUE_EMBEDDED_OFFSET (val
) = VALUE_EMBEDDED_OFFSET (fromval
);
549 VALUE_POINTED_TO_OFFSET (val
) = VALUE_POINTED_TO_OFFSET (fromval
);
552 case lval_internalvar_component
:
553 set_internalvar_component (VALUE_INTERNALVAR (toval
),
554 value_offset (toval
),
555 value_bitpos (toval
),
556 value_bitsize (toval
),
563 CORE_ADDR changed_addr
;
565 char buffer
[sizeof (LONGEST
)];
567 if (value_bitsize (toval
))
569 /* We assume that the argument to read_memory is in units of
570 host chars. FIXME: Is that correct? */
571 changed_len
= (value_bitpos (toval
)
572 + value_bitsize (toval
)
576 if (changed_len
> (int) sizeof (LONGEST
))
577 error ("Can't handle bitfields which don't fit in a %d bit word.",
578 (int) sizeof (LONGEST
) * HOST_CHAR_BIT
);
580 read_memory (VALUE_ADDRESS (toval
) + value_offset (toval
),
581 buffer
, changed_len
);
582 modify_field (buffer
, value_as_long (fromval
),
583 value_bitpos (toval
), value_bitsize (toval
));
584 changed_addr
= VALUE_ADDRESS (toval
) + value_offset (toval
);
585 dest_buffer
= buffer
;
589 changed_addr
= VALUE_ADDRESS (toval
) + value_offset (toval
);
590 changed_len
= TYPE_LENGTH (type
);
591 dest_buffer
= VALUE_CONTENTS (fromval
);
594 write_memory (changed_addr
, dest_buffer
, changed_len
);
595 if (deprecated_memory_changed_hook
)
596 deprecated_memory_changed_hook (changed_addr
, changed_len
);
602 struct frame_info
*frame
;
605 /* Figure out which frame this is in currently. */
606 frame
= frame_find_by_id (VALUE_FRAME_ID (toval
));
607 value_reg
= VALUE_REGNUM (toval
);
610 error ("Value being assigned to is no longer active.");
612 if (VALUE_LVAL (toval
) == lval_register
613 && CONVERT_REGISTER_P (VALUE_REGNUM (toval
), type
))
615 /* If TOVAL is a special machine register requiring
616 conversion of program values to a special raw format. */
617 VALUE_TO_REGISTER (frame
, VALUE_REGNUM (toval
),
618 type
, VALUE_CONTENTS (fromval
));
622 /* TOVAL is stored in a series of registers in the frame
623 specified by the structure. Copy that value out,
624 modify it, and copy it back in. */
632 /* Locate the first register that falls in the value that
633 needs to be transfered. Compute the offset of the
634 value in that register. */
637 for (reg_offset
= value_reg
, offset
= 0;
638 offset
+ register_size (current_gdbarch
, reg_offset
) <= value_offset (toval
);
640 byte_offset
= value_offset (toval
) - offset
;
643 /* Compute the number of register aligned values that need
645 if (value_bitsize (toval
))
646 amount_to_copy
= byte_offset
+ 1;
648 amount_to_copy
= byte_offset
+ TYPE_LENGTH (type
);
650 /* And a bounce buffer. Be slightly over generous. */
651 buffer
= (char *) alloca (amount_to_copy
+ MAX_REGISTER_SIZE
);
654 for (regno
= reg_offset
, amount_copied
= 0;
655 amount_copied
< amount_to_copy
;
656 amount_copied
+= register_size (current_gdbarch
, regno
), regno
++)
657 frame_register_read (frame
, regno
, buffer
+ amount_copied
);
659 /* Modify what needs to be modified. */
660 if (value_bitsize (toval
))
661 modify_field (buffer
+ byte_offset
,
662 value_as_long (fromval
),
663 value_bitpos (toval
), value_bitsize (toval
));
665 memcpy (buffer
+ byte_offset
, VALUE_CONTENTS (fromval
),
669 for (regno
= reg_offset
, amount_copied
= 0;
670 amount_copied
< amount_to_copy
;
671 amount_copied
+= register_size (current_gdbarch
, regno
), regno
++)
672 put_frame_register (frame
, regno
, buffer
+ amount_copied
);
675 if (deprecated_register_changed_hook
)
676 deprecated_register_changed_hook (-1);
677 observer_notify_target_changed (¤t_target
);
682 error ("Left operand of assignment is not an lvalue.");
685 /* Assigning to the stack pointer, frame pointer, and other
686 (architecture and calling convention specific) registers may
687 cause the frame cache to be out of date. Assigning to memory
688 also can. We just do this on all assignments to registers or
689 memory, for simplicity's sake; I doubt the slowdown matters. */
690 switch (VALUE_LVAL (toval
))
695 reinit_frame_cache ();
697 /* Having destoroyed the frame cache, restore the selected frame. */
699 /* FIXME: cagney/2002-11-02: There has to be a better way of
700 doing this. Instead of constantly saving/restoring the
701 frame. Why not create a get_selected_frame() function that,
702 having saved the selected frame's ID can automatically
703 re-find the previously selected frame automatically. */
706 struct frame_info
*fi
= frame_find_by_id (old_frame
);
716 /* If the field does not entirely fill a LONGEST, then zero the sign bits.
717 If the field is signed, and is negative, then sign extend. */
718 if ((value_bitsize (toval
) > 0)
719 && (value_bitsize (toval
) < 8 * (int) sizeof (LONGEST
)))
721 LONGEST fieldval
= value_as_long (fromval
);
722 LONGEST valmask
= (((ULONGEST
) 1) << value_bitsize (toval
)) - 1;
725 if (!TYPE_UNSIGNED (type
) && (fieldval
& (valmask
^ (valmask
>> 1))))
726 fieldval
|= ~valmask
;
728 fromval
= value_from_longest (type
, fieldval
);
731 val
= value_copy (toval
);
732 memcpy (value_contents_raw (val
), VALUE_CONTENTS (fromval
),
735 val
= value_change_enclosing_type (val
, value_enclosing_type (fromval
));
736 VALUE_EMBEDDED_OFFSET (val
) = VALUE_EMBEDDED_OFFSET (fromval
);
737 VALUE_POINTED_TO_OFFSET (val
) = VALUE_POINTED_TO_OFFSET (fromval
);
742 /* Extend a value VAL to COUNT repetitions of its type. */
745 value_repeat (struct value
*arg1
, int count
)
749 if (VALUE_LVAL (arg1
) != lval_memory
)
750 error ("Only values in memory can be extended with '@'.");
752 error ("Invalid number %d of repetitions.", count
);
754 val
= allocate_repeat_value (value_enclosing_type (arg1
), count
);
756 read_memory (VALUE_ADDRESS (arg1
) + value_offset (arg1
),
757 value_contents_all_raw (val
),
758 TYPE_LENGTH (value_enclosing_type (val
)));
759 VALUE_LVAL (val
) = lval_memory
;
760 VALUE_ADDRESS (val
) = VALUE_ADDRESS (arg1
) + value_offset (arg1
);
766 value_of_variable (struct symbol
*var
, struct block
*b
)
769 struct frame_info
*frame
= NULL
;
772 frame
= NULL
; /* Use selected frame. */
773 else if (symbol_read_needs_frame (var
))
775 frame
= block_innermost_frame (b
);
778 if (BLOCK_FUNCTION (b
)
779 && SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b
)))
780 error ("No frame is currently executing in block %s.",
781 SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b
)));
783 error ("No frame is currently executing in specified block");
787 val
= read_var_value (var
, frame
);
789 error ("Address of symbol \"%s\" is unknown.", SYMBOL_PRINT_NAME (var
));
794 /* Given a value which is an array, return a value which is a pointer to its
795 first element, regardless of whether or not the array has a nonzero lower
798 FIXME: A previous comment here indicated that this routine should be
799 substracting the array's lower bound. It's not clear to me that this
800 is correct. Given an array subscripting operation, it would certainly
801 work to do the adjustment here, essentially computing:
803 (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0])
805 However I believe a more appropriate and logical place to account for
806 the lower bound is to do so in value_subscript, essentially computing:
808 (&array[0] + ((index - lowerbound) * sizeof array[0]))
810 As further evidence consider what would happen with operations other
811 than array subscripting, where the caller would get back a value that
812 had an address somewhere before the actual first element of the array,
813 and the information about the lower bound would be lost because of
814 the coercion to pointer type.
818 value_coerce_array (struct value
*arg1
)
820 struct type
*type
= check_typedef (value_type (arg1
));
822 if (VALUE_LVAL (arg1
) != lval_memory
)
823 error ("Attempt to take address of value not located in memory.");
825 return value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type
)),
826 (VALUE_ADDRESS (arg1
) + value_offset (arg1
)));
829 /* Given a value which is a function, return a value which is a pointer
833 value_coerce_function (struct value
*arg1
)
835 struct value
*retval
;
837 if (VALUE_LVAL (arg1
) != lval_memory
)
838 error ("Attempt to take address of value not located in memory.");
840 retval
= value_from_pointer (lookup_pointer_type (value_type (arg1
)),
841 (VALUE_ADDRESS (arg1
) + value_offset (arg1
)));
845 /* Return a pointer value for the object for which ARG1 is the contents. */
848 value_addr (struct value
*arg1
)
852 struct type
*type
= check_typedef (value_type (arg1
));
853 if (TYPE_CODE (type
) == TYPE_CODE_REF
)
855 /* Copy the value, but change the type from (T&) to (T*).
856 We keep the same location information, which is efficient,
857 and allows &(&X) to get the location containing the reference. */
858 arg2
= value_copy (arg1
);
859 arg2
->type
= lookup_pointer_type (TYPE_TARGET_TYPE (type
));
862 if (TYPE_CODE (type
) == TYPE_CODE_FUNC
)
863 return value_coerce_function (arg1
);
865 if (VALUE_LVAL (arg1
) != lval_memory
)
866 error ("Attempt to take address of value not located in memory.");
868 /* Get target memory address */
869 arg2
= value_from_pointer (lookup_pointer_type (value_type (arg1
)),
870 (VALUE_ADDRESS (arg1
)
871 + value_offset (arg1
)
872 + VALUE_EMBEDDED_OFFSET (arg1
)));
874 /* This may be a pointer to a base subobject; so remember the
875 full derived object's type ... */
876 arg2
= value_change_enclosing_type (arg2
, lookup_pointer_type (value_enclosing_type (arg1
)));
877 /* ... and also the relative position of the subobject in the full object */
878 VALUE_POINTED_TO_OFFSET (arg2
) = VALUE_EMBEDDED_OFFSET (arg1
);
882 /* Given a value of a pointer type, apply the C unary * operator to it. */
885 value_ind (struct value
*arg1
)
887 struct type
*base_type
;
890 arg1
= coerce_array (arg1
);
892 base_type
= check_typedef (value_type (arg1
));
894 if (TYPE_CODE (base_type
) == TYPE_CODE_MEMBER
)
895 error ("not implemented: member types in value_ind");
897 /* Allow * on an integer so we can cast it to whatever we want.
898 This returns an int, which seems like the most C-like thing
899 to do. "long long" variables are rare enough that
900 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
901 if (TYPE_CODE (base_type
) == TYPE_CODE_INT
)
902 return value_at_lazy (builtin_type_int
,
903 (CORE_ADDR
) value_as_long (arg1
));
904 else if (TYPE_CODE (base_type
) == TYPE_CODE_PTR
)
906 struct type
*enc_type
;
907 /* We may be pointing to something embedded in a larger object */
908 /* Get the real type of the enclosing object */
909 enc_type
= check_typedef (value_enclosing_type (arg1
));
910 enc_type
= TYPE_TARGET_TYPE (enc_type
);
911 /* Retrieve the enclosing object pointed to */
912 arg2
= value_at_lazy (enc_type
, (value_as_address (arg1
)
913 - VALUE_POINTED_TO_OFFSET (arg1
)));
915 arg2
->type
= TYPE_TARGET_TYPE (base_type
);
916 /* Add embedding info */
917 arg2
= value_change_enclosing_type (arg2
, enc_type
);
918 VALUE_EMBEDDED_OFFSET (arg2
) = VALUE_POINTED_TO_OFFSET (arg1
);
920 /* We may be pointing to an object of some derived type */
921 arg2
= value_full_object (arg2
, NULL
, 0, 0, 0);
925 error ("Attempt to take contents of a non-pointer value.");
926 return 0; /* For lint -- never reached */
929 /* Pushing small parts of stack frames. */
931 /* Push one word (the size of object that a register holds). */
934 push_word (CORE_ADDR sp
, ULONGEST word
)
936 int len
= DEPRECATED_REGISTER_SIZE
;
937 char buffer
[MAX_REGISTER_SIZE
];
939 store_unsigned_integer (buffer
, len
, word
);
940 if (INNER_THAN (1, 2))
942 /* stack grows downward */
944 write_memory (sp
, buffer
, len
);
948 /* stack grows upward */
949 write_memory (sp
, buffer
, len
);
956 /* Push LEN bytes with data at BUFFER. */
959 push_bytes (CORE_ADDR sp
, char *buffer
, int len
)
961 if (INNER_THAN (1, 2))
963 /* stack grows downward */
965 write_memory (sp
, buffer
, len
);
969 /* stack grows upward */
970 write_memory (sp
, buffer
, len
);
977 /* Create a value for an array by allocating space in the inferior, copying
978 the data into that space, and then setting up an array value.
980 The array bounds are set from LOWBOUND and HIGHBOUND, and the array is
981 populated from the values passed in ELEMVEC.
983 The element type of the array is inherited from the type of the
984 first element, and all elements must have the same size (though we
985 don't currently enforce any restriction on their types). */
988 value_array (int lowbound
, int highbound
, struct value
**elemvec
)
992 unsigned int typelength
;
994 struct type
*rangetype
;
995 struct type
*arraytype
;
998 /* Validate that the bounds are reasonable and that each of the elements
999 have the same size. */
1001 nelem
= highbound
- lowbound
+ 1;
1004 error ("bad array bounds (%d, %d)", lowbound
, highbound
);
1006 typelength
= TYPE_LENGTH (value_enclosing_type (elemvec
[0]));
1007 for (idx
= 1; idx
< nelem
; idx
++)
1009 if (TYPE_LENGTH (value_enclosing_type (elemvec
[idx
])) != typelength
)
1011 error ("array elements must all be the same size");
1015 rangetype
= create_range_type ((struct type
*) NULL
, builtin_type_int
,
1016 lowbound
, highbound
);
1017 arraytype
= create_array_type ((struct type
*) NULL
,
1018 value_enclosing_type (elemvec
[0]), rangetype
);
1020 if (!current_language
->c_style_arrays
)
1022 val
= allocate_value (arraytype
);
1023 for (idx
= 0; idx
< nelem
; idx
++)
1025 memcpy (value_contents_all_raw (val
) + (idx
* typelength
),
1026 VALUE_CONTENTS_ALL (elemvec
[idx
]),
1032 /* Allocate space to store the array in the inferior, and then initialize
1033 it by copying in each element. FIXME: Is it worth it to create a
1034 local buffer in which to collect each value and then write all the
1035 bytes in one operation? */
1037 addr
= allocate_space_in_inferior (nelem
* typelength
);
1038 for (idx
= 0; idx
< nelem
; idx
++)
1040 write_memory (addr
+ (idx
* typelength
), VALUE_CONTENTS_ALL (elemvec
[idx
]),
1044 /* Create the array type and set up an array value to be evaluated lazily. */
1046 val
= value_at_lazy (arraytype
, addr
);
1050 /* Create a value for a string constant by allocating space in the inferior,
1051 copying the data into that space, and returning the address with type
1052 TYPE_CODE_STRING. PTR points to the string constant data; LEN is number
1054 Note that string types are like array of char types with a lower bound of
1055 zero and an upper bound of LEN - 1. Also note that the string may contain
1056 embedded null bytes. */
1059 value_string (char *ptr
, int len
)
1062 int lowbound
= current_language
->string_lower_bound
;
1063 struct type
*rangetype
= create_range_type ((struct type
*) NULL
,
1065 lowbound
, len
+ lowbound
- 1);
1066 struct type
*stringtype
1067 = create_string_type ((struct type
*) NULL
, rangetype
);
1070 if (current_language
->c_style_arrays
== 0)
1072 val
= allocate_value (stringtype
);
1073 memcpy (value_contents_raw (val
), ptr
, len
);
1078 /* Allocate space to store the string in the inferior, and then
1079 copy LEN bytes from PTR in gdb to that address in the inferior. */
1081 addr
= allocate_space_in_inferior (len
);
1082 write_memory (addr
, ptr
, len
);
1084 val
= value_at_lazy (stringtype
, addr
);
1089 value_bitstring (char *ptr
, int len
)
1092 struct type
*domain_type
= create_range_type (NULL
, builtin_type_int
,
1094 struct type
*type
= create_set_type ((struct type
*) NULL
, domain_type
);
1095 TYPE_CODE (type
) = TYPE_CODE_BITSTRING
;
1096 val
= allocate_value (type
);
1097 memcpy (value_contents_raw (val
), ptr
, TYPE_LENGTH (type
));
1101 /* See if we can pass arguments in T2 to a function which takes arguments
1102 of types T1. T1 is a list of NARGS arguments, and T2 is a NULL-terminated
1103 vector. If some arguments need coercion of some sort, then the coerced
1104 values are written into T2. Return value is 0 if the arguments could be
1105 matched, or the position at which they differ if not.
1107 STATICP is nonzero if the T1 argument list came from a
1108 static member function. T2 will still include the ``this'' pointer,
1109 but it will be skipped.
1111 For non-static member functions, we ignore the first argument,
1112 which is the type of the instance variable. This is because we want
1113 to handle calls with objects from derived classes. This is not
1114 entirely correct: we should actually check to make sure that a
1115 requested operation is type secure, shouldn't we? FIXME. */
1118 typecmp (int staticp
, int varargs
, int nargs
,
1119 struct field t1
[], struct value
*t2
[])
1124 internal_error (__FILE__
, __LINE__
, "typecmp: no argument list");
1126 /* Skip ``this'' argument if applicable. T2 will always include THIS. */
1131 (i
< nargs
) && TYPE_CODE (t1
[i
].type
) != TYPE_CODE_VOID
;
1134 struct type
*tt1
, *tt2
;
1139 tt1
= check_typedef (t1
[i
].type
);
1140 tt2
= check_typedef (value_type (t2
[i
]));
1142 if (TYPE_CODE (tt1
) == TYPE_CODE_REF
1143 /* We should be doing hairy argument matching, as below. */
1144 && (TYPE_CODE (check_typedef (TYPE_TARGET_TYPE (tt1
))) == TYPE_CODE (tt2
)))
1146 if (TYPE_CODE (tt2
) == TYPE_CODE_ARRAY
)
1147 t2
[i
] = value_coerce_array (t2
[i
]);
1149 t2
[i
] = value_addr (t2
[i
]);
1153 /* djb - 20000715 - Until the new type structure is in the
1154 place, and we can attempt things like implicit conversions,
1155 we need to do this so you can take something like a map<const
1156 char *>, and properly access map["hello"], because the
1157 argument to [] will be a reference to a pointer to a char,
1158 and the argument will be a pointer to a char. */
1159 while ( TYPE_CODE(tt1
) == TYPE_CODE_REF
||
1160 TYPE_CODE (tt1
) == TYPE_CODE_PTR
)
1162 tt1
= check_typedef( TYPE_TARGET_TYPE(tt1
) );
1164 while ( TYPE_CODE(tt2
) == TYPE_CODE_ARRAY
||
1165 TYPE_CODE(tt2
) == TYPE_CODE_PTR
||
1166 TYPE_CODE(tt2
) == TYPE_CODE_REF
)
1168 tt2
= check_typedef( TYPE_TARGET_TYPE(tt2
) );
1170 if (TYPE_CODE (tt1
) == TYPE_CODE (tt2
))
1172 /* Array to pointer is a `trivial conversion' according to the ARM. */
1174 /* We should be doing much hairier argument matching (see section 13.2
1175 of the ARM), but as a quick kludge, just check for the same type
1177 if (TYPE_CODE (t1
[i
].type
) != TYPE_CODE (value_type (t2
[i
])))
1180 if (varargs
|| t2
[i
] == NULL
)
1185 /* Helper function used by value_struct_elt to recurse through baseclasses.
1186 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1187 and search in it assuming it has (class) type TYPE.
1188 If found, return value, else return NULL.
1190 If LOOKING_FOR_BASECLASS, then instead of looking for struct fields,
1191 look for a baseclass named NAME. */
1193 static struct value
*
1194 search_struct_field (char *name
, struct value
*arg1
, int offset
,
1195 struct type
*type
, int looking_for_baseclass
)
1198 int nbases
= TYPE_N_BASECLASSES (type
);
1200 CHECK_TYPEDEF (type
);
1202 if (!looking_for_baseclass
)
1203 for (i
= TYPE_NFIELDS (type
) - 1; i
>= nbases
; i
--)
1205 char *t_field_name
= TYPE_FIELD_NAME (type
, i
);
1207 if (t_field_name
&& (strcmp_iw (t_field_name
, name
) == 0))
1210 if (TYPE_FIELD_STATIC (type
, i
))
1212 v
= value_static_field (type
, i
);
1214 error ("field %s is nonexistent or has been optimised out",
1219 v
= value_primitive_field (arg1
, offset
, i
, type
);
1221 error ("there is no field named %s", name
);
1227 && (t_field_name
[0] == '\0'
1228 || (TYPE_CODE (type
) == TYPE_CODE_UNION
1229 && (strcmp_iw (t_field_name
, "else") == 0))))
1231 struct type
*field_type
= TYPE_FIELD_TYPE (type
, i
);
1232 if (TYPE_CODE (field_type
) == TYPE_CODE_UNION
1233 || TYPE_CODE (field_type
) == TYPE_CODE_STRUCT
)
1235 /* Look for a match through the fields of an anonymous union,
1236 or anonymous struct. C++ provides anonymous unions.
1238 In the GNU Chill (now deleted from GDB)
1239 implementation of variant record types, each
1240 <alternative field> has an (anonymous) union type,
1241 each member of the union represents a <variant
1242 alternative>. Each <variant alternative> is
1243 represented as a struct, with a member for each
1247 int new_offset
= offset
;
1249 /* This is pretty gross. In G++, the offset in an
1250 anonymous union is relative to the beginning of the
1251 enclosing struct. In the GNU Chill (now deleted
1252 from GDB) implementation of variant records, the
1253 bitpos is zero in an anonymous union field, so we
1254 have to add the offset of the union here. */
1255 if (TYPE_CODE (field_type
) == TYPE_CODE_STRUCT
1256 || (TYPE_NFIELDS (field_type
) > 0
1257 && TYPE_FIELD_BITPOS (field_type
, 0) == 0))
1258 new_offset
+= TYPE_FIELD_BITPOS (type
, i
) / 8;
1260 v
= search_struct_field (name
, arg1
, new_offset
, field_type
,
1261 looking_for_baseclass
);
1268 for (i
= 0; i
< nbases
; i
++)
1271 struct type
*basetype
= check_typedef (TYPE_BASECLASS (type
, i
));
1272 /* If we are looking for baseclasses, this is what we get when we
1273 hit them. But it could happen that the base part's member name
1274 is not yet filled in. */
1275 int found_baseclass
= (looking_for_baseclass
1276 && TYPE_BASECLASS_NAME (type
, i
) != NULL
1277 && (strcmp_iw (name
, TYPE_BASECLASS_NAME (type
, i
)) == 0));
1279 if (BASETYPE_VIA_VIRTUAL (type
, i
))
1282 struct value
*v2
= allocate_value (basetype
);
1284 boffset
= baseclass_offset (type
, i
,
1285 VALUE_CONTENTS (arg1
) + offset
,
1286 VALUE_ADDRESS (arg1
)
1287 + value_offset (arg1
) + offset
);
1289 error ("virtual baseclass botch");
1291 /* The virtual base class pointer might have been clobbered by the
1292 user program. Make sure that it still points to a valid memory
1296 if (boffset
< 0 || boffset
>= TYPE_LENGTH (type
))
1298 CORE_ADDR base_addr
;
1300 base_addr
= VALUE_ADDRESS (arg1
) + value_offset (arg1
) + boffset
;
1301 if (target_read_memory (base_addr
, value_contents_raw (v2
),
1302 TYPE_LENGTH (basetype
)) != 0)
1303 error ("virtual baseclass botch");
1304 VALUE_LVAL (v2
) = lval_memory
;
1305 VALUE_ADDRESS (v2
) = base_addr
;
1309 VALUE_LVAL (v2
) = VALUE_LVAL (arg1
);
1310 VALUE_ADDRESS (v2
) = VALUE_ADDRESS (arg1
);
1311 VALUE_FRAME_ID (v2
) = VALUE_FRAME_ID (arg1
);
1312 v2
->offset
= value_offset (arg1
) + boffset
;
1313 if (VALUE_LAZY (arg1
))
1314 VALUE_LAZY (v2
) = 1;
1316 memcpy (value_contents_raw (v2
),
1317 value_contents_raw (arg1
) + boffset
,
1318 TYPE_LENGTH (basetype
));
1321 if (found_baseclass
)
1323 v
= search_struct_field (name
, v2
, 0, TYPE_BASECLASS (type
, i
),
1324 looking_for_baseclass
);
1326 else if (found_baseclass
)
1327 v
= value_primitive_field (arg1
, offset
, i
, type
);
1329 v
= search_struct_field (name
, arg1
,
1330 offset
+ TYPE_BASECLASS_BITPOS (type
, i
) / 8,
1331 basetype
, looking_for_baseclass
);
1339 /* Return the offset (in bytes) of the virtual base of type BASETYPE
1340 * in an object pointed to by VALADDR (on the host), assumed to be of
1341 * type TYPE. OFFSET is number of bytes beyond start of ARG to start
1342 * looking (in case VALADDR is the contents of an enclosing object).
1344 * This routine recurses on the primary base of the derived class because
1345 * the virtual base entries of the primary base appear before the other
1346 * virtual base entries.
1348 * If the virtual base is not found, a negative integer is returned.
1349 * The magnitude of the negative integer is the number of entries in
1350 * the virtual table to skip over (entries corresponding to various
1351 * ancestral classes in the chain of primary bases).
1353 * Important: This assumes the HP / Taligent C++ runtime
1354 * conventions. Use baseclass_offset() instead to deal with g++
1358 find_rt_vbase_offset (struct type
*type
, struct type
*basetype
,
1359 const bfd_byte
*valaddr
, int offset
, int *boffset_p
,
1362 int boffset
; /* offset of virtual base */
1363 int index
; /* displacement to use in virtual table */
1367 CORE_ADDR vtbl
; /* the virtual table pointer */
1368 struct type
*pbc
; /* the primary base class */
1370 /* Look for the virtual base recursively in the primary base, first.
1371 * This is because the derived class object and its primary base
1372 * subobject share the primary virtual table. */
1375 pbc
= TYPE_PRIMARY_BASE (type
);
1378 find_rt_vbase_offset (pbc
, basetype
, valaddr
, offset
, &boffset
, &skip
);
1381 *boffset_p
= boffset
;
1390 /* Find the index of the virtual base according to HP/Taligent
1391 runtime spec. (Depth-first, left-to-right.) */
1392 index
= virtual_base_index_skip_primaries (basetype
, type
);
1396 *skip_p
= skip
+ virtual_base_list_length_skip_primaries (type
);
1401 /* pai: FIXME -- 32x64 possible problem */
1402 /* First word (4 bytes) in object layout is the vtable pointer */
1403 vtbl
= *(CORE_ADDR
*) (valaddr
+ offset
);
1405 /* Before the constructor is invoked, things are usually zero'd out. */
1407 error ("Couldn't find virtual table -- object may not be constructed yet.");
1410 /* Find virtual base's offset -- jump over entries for primary base
1411 * ancestors, then use the index computed above. But also adjust by
1412 * HP_ACC_VBASE_START for the vtable slots before the start of the
1413 * virtual base entries. Offset is negative -- virtual base entries
1414 * appear _before_ the address point of the virtual table. */
1416 /* pai: FIXME -- 32x64 problem, if word = 8 bytes, change multiplier
1419 /* epstein : FIXME -- added param for overlay section. May not be correct */
1420 vp
= value_at (builtin_type_int
, vtbl
+ 4 * (-skip
- index
- HP_ACC_VBASE_START
));
1421 boffset
= value_as_long (vp
);
1423 *boffset_p
= boffset
;
1428 /* Helper function used by value_struct_elt to recurse through baseclasses.
1429 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1430 and search in it assuming it has (class) type TYPE.
1431 If found, return value, else if name matched and args not return (value)-1,
1432 else return NULL. */
1434 static struct value
*
1435 search_struct_method (char *name
, struct value
**arg1p
,
1436 struct value
**args
, int offset
,
1437 int *static_memfuncp
, struct type
*type
)
1441 int name_matched
= 0;
1442 char dem_opname
[64];
1444 CHECK_TYPEDEF (type
);
1445 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; i
--)
1447 char *t_field_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
1448 /* FIXME! May need to check for ARM demangling here */
1449 if (strncmp (t_field_name
, "__", 2) == 0 ||
1450 strncmp (t_field_name
, "op", 2) == 0 ||
1451 strncmp (t_field_name
, "type", 4) == 0)
1453 if (cplus_demangle_opname (t_field_name
, dem_opname
, DMGL_ANSI
))
1454 t_field_name
= dem_opname
;
1455 else if (cplus_demangle_opname (t_field_name
, dem_opname
, 0))
1456 t_field_name
= dem_opname
;
1458 if (t_field_name
&& (strcmp_iw (t_field_name
, name
) == 0))
1460 int j
= TYPE_FN_FIELDLIST_LENGTH (type
, i
) - 1;
1461 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
1464 check_stub_method_group (type
, i
);
1465 if (j
> 0 && args
== 0)
1466 error ("cannot resolve overloaded method `%s': no arguments supplied", name
);
1467 else if (j
== 0 && args
== 0)
1469 v
= value_fn_field (arg1p
, f
, j
, type
, offset
);
1476 if (!typecmp (TYPE_FN_FIELD_STATIC_P (f
, j
),
1477 TYPE_VARARGS (TYPE_FN_FIELD_TYPE (f
, j
)),
1478 TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f
, j
)),
1479 TYPE_FN_FIELD_ARGS (f
, j
), args
))
1481 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
1482 return value_virtual_fn_field (arg1p
, f
, j
, type
, offset
);
1483 if (TYPE_FN_FIELD_STATIC_P (f
, j
) && static_memfuncp
)
1484 *static_memfuncp
= 1;
1485 v
= value_fn_field (arg1p
, f
, j
, type
, offset
);
1494 for (i
= TYPE_N_BASECLASSES (type
) - 1; i
>= 0; i
--)
1498 if (BASETYPE_VIA_VIRTUAL (type
, i
))
1500 if (TYPE_HAS_VTABLE (type
))
1502 /* HP aCC compiled type, search for virtual base offset
1503 according to HP/Taligent runtime spec. */
1505 find_rt_vbase_offset (type
, TYPE_BASECLASS (type
, i
),
1506 VALUE_CONTENTS_ALL (*arg1p
),
1507 offset
+ VALUE_EMBEDDED_OFFSET (*arg1p
),
1508 &base_offset
, &skip
);
1510 error ("Virtual base class offset not found in vtable");
1514 struct type
*baseclass
= check_typedef (TYPE_BASECLASS (type
, i
));
1517 /* The virtual base class pointer might have been clobbered by the
1518 user program. Make sure that it still points to a valid memory
1521 if (offset
< 0 || offset
>= TYPE_LENGTH (type
))
1523 base_valaddr
= (char *) alloca (TYPE_LENGTH (baseclass
));
1524 if (target_read_memory (VALUE_ADDRESS (*arg1p
)
1525 + value_offset (*arg1p
) + offset
,
1527 TYPE_LENGTH (baseclass
)) != 0)
1528 error ("virtual baseclass botch");
1531 base_valaddr
= VALUE_CONTENTS (*arg1p
) + offset
;
1534 baseclass_offset (type
, i
, base_valaddr
,
1535 VALUE_ADDRESS (*arg1p
)
1536 + value_offset (*arg1p
) + offset
);
1537 if (base_offset
== -1)
1538 error ("virtual baseclass botch");
1543 base_offset
= TYPE_BASECLASS_BITPOS (type
, i
) / 8;
1545 v
= search_struct_method (name
, arg1p
, args
, base_offset
+ offset
,
1546 static_memfuncp
, TYPE_BASECLASS (type
, i
));
1547 if (v
== (struct value
*) - 1)
1553 /* FIXME-bothner: Why is this commented out? Why is it here? */
1554 /* *arg1p = arg1_tmp; */
1559 return (struct value
*) - 1;
1564 /* Given *ARGP, a value of type (pointer to a)* structure/union,
1565 extract the component named NAME from the ultimate target structure/union
1566 and return it as a value with its appropriate type.
1567 ERR is used in the error message if *ARGP's type is wrong.
1569 C++: ARGS is a list of argument types to aid in the selection of
1570 an appropriate method. Also, handle derived types.
1572 STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
1573 where the truthvalue of whether the function that was resolved was
1574 a static member function or not is stored.
1576 ERR is an error message to be printed in case the field is not found. */
1579 value_struct_elt (struct value
**argp
, struct value
**args
,
1580 char *name
, int *static_memfuncp
, char *err
)
1585 *argp
= coerce_array (*argp
);
1587 t
= check_typedef (value_type (*argp
));
1589 /* Follow pointers until we get to a non-pointer. */
1591 while (TYPE_CODE (t
) == TYPE_CODE_PTR
|| TYPE_CODE (t
) == TYPE_CODE_REF
)
1593 *argp
= value_ind (*argp
);
1594 /* Don't coerce fn pointer to fn and then back again! */
1595 if (TYPE_CODE (value_type (*argp
)) != TYPE_CODE_FUNC
)
1596 *argp
= coerce_array (*argp
);
1597 t
= check_typedef (value_type (*argp
));
1600 if (TYPE_CODE (t
) == TYPE_CODE_MEMBER
)
1601 error ("not implemented: member type in value_struct_elt");
1603 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
1604 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
1605 error ("Attempt to extract a component of a value that is not a %s.", err
);
1607 /* Assume it's not, unless we see that it is. */
1608 if (static_memfuncp
)
1609 *static_memfuncp
= 0;
1613 /* if there are no arguments ...do this... */
1615 /* Try as a field first, because if we succeed, there
1616 is less work to be done. */
1617 v
= search_struct_field (name
, *argp
, 0, t
, 0);
1621 /* C++: If it was not found as a data field, then try to
1622 return it as a pointer to a method. */
1624 if (destructor_name_p (name
, t
))
1625 error ("Cannot get value of destructor");
1627 v
= search_struct_method (name
, argp
, args
, 0, static_memfuncp
, t
);
1629 if (v
== (struct value
*) - 1)
1630 error ("Cannot take address of a method");
1633 if (TYPE_NFN_FIELDS (t
))
1634 error ("There is no member or method named %s.", name
);
1636 error ("There is no member named %s.", name
);
1641 if (destructor_name_p (name
, t
))
1645 /* Destructors are a special case. */
1646 int m_index
, f_index
;
1649 if (get_destructor_fn_field (t
, &m_index
, &f_index
))
1651 v
= value_fn_field (NULL
, TYPE_FN_FIELDLIST1 (t
, m_index
),
1655 error ("could not find destructor function named %s.", name
);
1661 error ("destructor should not have any argument");
1665 v
= search_struct_method (name
, argp
, args
, 0, static_memfuncp
, t
);
1667 if (v
== (struct value
*) - 1)
1669 error ("One of the arguments you tried to pass to %s could not be converted to what the function wants.", name
);
1673 /* See if user tried to invoke data as function. If so,
1674 hand it back. If it's not callable (i.e., a pointer to function),
1675 gdb should give an error. */
1676 v
= search_struct_field (name
, *argp
, 0, t
, 0);
1680 error ("Structure has no component named %s.", name
);
1684 /* Search through the methods of an object (and its bases)
1685 * to find a specified method. Return the pointer to the
1686 * fn_field list of overloaded instances.
1687 * Helper function for value_find_oload_list.
1688 * ARGP is a pointer to a pointer to a value (the object)
1689 * METHOD is a string containing the method name
1690 * OFFSET is the offset within the value
1691 * TYPE is the assumed type of the object
1692 * NUM_FNS is the number of overloaded instances
1693 * BASETYPE is set to the actual type of the subobject where the method is found
1694 * BOFFSET is the offset of the base subobject where the method is found */
1696 static struct fn_field
*
1697 find_method_list (struct value
**argp
, char *method
, int offset
,
1698 struct type
*type
, int *num_fns
,
1699 struct type
**basetype
, int *boffset
)
1703 CHECK_TYPEDEF (type
);
1707 /* First check in object itself */
1708 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; i
--)
1710 /* pai: FIXME What about operators and type conversions? */
1711 char *fn_field_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
1712 if (fn_field_name
&& (strcmp_iw (fn_field_name
, method
) == 0))
1714 int len
= TYPE_FN_FIELDLIST_LENGTH (type
, i
);
1715 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
1721 /* Resolve any stub methods. */
1722 check_stub_method_group (type
, i
);
1728 /* Not found in object, check in base subobjects */
1729 for (i
= TYPE_N_BASECLASSES (type
) - 1; i
>= 0; i
--)
1732 if (BASETYPE_VIA_VIRTUAL (type
, i
))
1734 if (TYPE_HAS_VTABLE (type
))
1736 /* HP aCC compiled type, search for virtual base offset
1737 * according to HP/Taligent runtime spec. */
1739 find_rt_vbase_offset (type
, TYPE_BASECLASS (type
, i
),
1740 VALUE_CONTENTS_ALL (*argp
),
1741 offset
+ VALUE_EMBEDDED_OFFSET (*argp
),
1742 &base_offset
, &skip
);
1744 error ("Virtual base class offset not found in vtable");
1748 /* probably g++ runtime model */
1749 base_offset
= value_offset (*argp
) + offset
;
1751 baseclass_offset (type
, i
,
1752 VALUE_CONTENTS (*argp
) + base_offset
,
1753 VALUE_ADDRESS (*argp
) + base_offset
);
1754 if (base_offset
== -1)
1755 error ("virtual baseclass botch");
1759 /* non-virtual base, simply use bit position from debug info */
1761 base_offset
= TYPE_BASECLASS_BITPOS (type
, i
) / 8;
1763 f
= find_method_list (argp
, method
, base_offset
+ offset
,
1764 TYPE_BASECLASS (type
, i
), num_fns
, basetype
,
1772 /* Return the list of overloaded methods of a specified name.
1773 * ARGP is a pointer to a pointer to a value (the object)
1774 * METHOD is the method name
1775 * OFFSET is the offset within the value contents
1776 * NUM_FNS is the number of overloaded instances
1777 * BASETYPE is set to the type of the base subobject that defines the method
1778 * BOFFSET is the offset of the base subobject which defines the method */
1781 value_find_oload_method_list (struct value
**argp
, char *method
, int offset
,
1782 int *num_fns
, struct type
**basetype
,
1787 t
= check_typedef (value_type (*argp
));
1789 /* code snarfed from value_struct_elt */
1790 while (TYPE_CODE (t
) == TYPE_CODE_PTR
|| TYPE_CODE (t
) == TYPE_CODE_REF
)
1792 *argp
= value_ind (*argp
);
1793 /* Don't coerce fn pointer to fn and then back again! */
1794 if (TYPE_CODE (value_type (*argp
)) != TYPE_CODE_FUNC
)
1795 *argp
= coerce_array (*argp
);
1796 t
= check_typedef (value_type (*argp
));
1799 if (TYPE_CODE (t
) == TYPE_CODE_MEMBER
)
1800 error ("Not implemented: member type in value_find_oload_lis");
1802 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
1803 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
1804 error ("Attempt to extract a component of a value that is not a struct or union");
1806 return find_method_list (argp
, method
, 0, t
, num_fns
, basetype
, boffset
);
1809 /* Given an array of argument types (ARGTYPES) (which includes an
1810 entry for "this" in the case of C++ methods), the number of
1811 arguments NARGS, the NAME of a function whether it's a method or
1812 not (METHOD), and the degree of laxness (LAX) in conforming to
1813 overload resolution rules in ANSI C++, find the best function that
1814 matches on the argument types according to the overload resolution
1817 In the case of class methods, the parameter OBJ is an object value
1818 in which to search for overloaded methods.
1820 In the case of non-method functions, the parameter FSYM is a symbol
1821 corresponding to one of the overloaded functions.
1823 Return value is an integer: 0 -> good match, 10 -> debugger applied
1824 non-standard coercions, 100 -> incompatible.
1826 If a method is being searched for, VALP will hold the value.
1827 If a non-method is being searched for, SYMP will hold the symbol for it.
1829 If a method is being searched for, and it is a static method,
1830 then STATICP will point to a non-zero value.
1832 Note: This function does *not* check the value of
1833 overload_resolution. Caller must check it to see whether overload
1834 resolution is permitted.
1838 find_overload_match (struct type
**arg_types
, int nargs
, char *name
, int method
,
1839 int lax
, struct value
**objp
, struct symbol
*fsym
,
1840 struct value
**valp
, struct symbol
**symp
, int *staticp
)
1842 struct value
*obj
= (objp
? *objp
: NULL
);
1844 int oload_champ
; /* Index of best overloaded function */
1846 struct badness_vector
*oload_champ_bv
= NULL
; /* The measure for the current best match */
1848 struct value
*temp
= obj
;
1849 struct fn_field
*fns_ptr
= NULL
; /* For methods, the list of overloaded methods */
1850 struct symbol
**oload_syms
= NULL
; /* For non-methods, the list of overloaded function symbols */
1851 int num_fns
= 0; /* Number of overloaded instances being considered */
1852 struct type
*basetype
= NULL
;
1856 struct cleanup
*old_cleanups
= NULL
;
1858 const char *obj_type_name
= NULL
;
1859 char *func_name
= NULL
;
1860 enum oload_classification match_quality
;
1862 /* Get the list of overloaded methods or functions */
1865 obj_type_name
= TYPE_NAME (value_type (obj
));
1866 /* Hack: evaluate_subexp_standard often passes in a pointer
1867 value rather than the object itself, so try again */
1868 if ((!obj_type_name
|| !*obj_type_name
) &&
1869 (TYPE_CODE (value_type (obj
)) == TYPE_CODE_PTR
))
1870 obj_type_name
= TYPE_NAME (TYPE_TARGET_TYPE (value_type (obj
)));
1872 fns_ptr
= value_find_oload_method_list (&temp
, name
, 0,
1874 &basetype
, &boffset
);
1875 if (!fns_ptr
|| !num_fns
)
1876 error ("Couldn't find method %s%s%s",
1878 (obj_type_name
&& *obj_type_name
) ? "::" : "",
1880 /* If we are dealing with stub method types, they should have
1881 been resolved by find_method_list via value_find_oload_method_list
1883 gdb_assert (TYPE_DOMAIN_TYPE (fns_ptr
[0].type
) != NULL
);
1884 oload_champ
= find_oload_champ (arg_types
, nargs
, method
, num_fns
,
1885 fns_ptr
, oload_syms
, &oload_champ_bv
);
1889 const char *qualified_name
= SYMBOL_CPLUS_DEMANGLED_NAME (fsym
);
1890 func_name
= cp_func_name (qualified_name
);
1892 /* If the name is NULL this must be a C-style function.
1893 Just return the same symbol. */
1894 if (func_name
== NULL
)
1900 old_cleanups
= make_cleanup (xfree
, func_name
);
1901 make_cleanup (xfree
, oload_syms
);
1902 make_cleanup (xfree
, oload_champ_bv
);
1904 oload_champ
= find_oload_champ_namespace (arg_types
, nargs
,
1911 /* Check how bad the best match is. */
1914 = classify_oload_match (oload_champ_bv
, nargs
,
1915 oload_method_static (method
, fns_ptr
,
1918 if (match_quality
== INCOMPATIBLE
)
1921 error ("Cannot resolve method %s%s%s to any overloaded instance",
1923 (obj_type_name
&& *obj_type_name
) ? "::" : "",
1926 error ("Cannot resolve function %s to any overloaded instance",
1929 else if (match_quality
== NON_STANDARD
)
1932 warning ("Using non-standard conversion to match method %s%s%s to supplied arguments",
1934 (obj_type_name
&& *obj_type_name
) ? "::" : "",
1937 warning ("Using non-standard conversion to match function %s to supplied arguments",
1943 if (staticp
!= NULL
)
1944 *staticp
= oload_method_static (method
, fns_ptr
, oload_champ
);
1945 if (TYPE_FN_FIELD_VIRTUAL_P (fns_ptr
, oload_champ
))
1946 *valp
= value_virtual_fn_field (&temp
, fns_ptr
, oload_champ
, basetype
, boffset
);
1948 *valp
= value_fn_field (&temp
, fns_ptr
, oload_champ
, basetype
, boffset
);
1952 *symp
= oload_syms
[oload_champ
];
1957 if (TYPE_CODE (value_type (temp
)) != TYPE_CODE_PTR
1958 && TYPE_CODE (value_type (*objp
)) == TYPE_CODE_PTR
)
1960 temp
= value_addr (temp
);
1964 if (old_cleanups
!= NULL
)
1965 do_cleanups (old_cleanups
);
1967 switch (match_quality
)
1973 default: /* STANDARD */
1978 /* Find the best overload match, searching for FUNC_NAME in namespaces
1979 contained in QUALIFIED_NAME until it either finds a good match or
1980 runs out of namespaces. It stores the overloaded functions in
1981 *OLOAD_SYMS, and the badness vector in *OLOAD_CHAMP_BV. The
1982 calling function is responsible for freeing *OLOAD_SYMS and
1986 find_oload_champ_namespace (struct type
**arg_types
, int nargs
,
1987 const char *func_name
,
1988 const char *qualified_name
,
1989 struct symbol
***oload_syms
,
1990 struct badness_vector
**oload_champ_bv
)
1994 find_oload_champ_namespace_loop (arg_types
, nargs
,
1997 oload_syms
, oload_champ_bv
,
2003 /* Helper function for find_oload_champ_namespace; NAMESPACE_LEN is
2004 how deep we've looked for namespaces, and the champ is stored in
2005 OLOAD_CHAMP. The return value is 1 if the champ is a good one, 0
2008 It is the caller's responsibility to free *OLOAD_SYMS and
2012 find_oload_champ_namespace_loop (struct type
**arg_types
, int nargs
,
2013 const char *func_name
,
2014 const char *qualified_name
,
2016 struct symbol
***oload_syms
,
2017 struct badness_vector
**oload_champ_bv
,
2020 int next_namespace_len
= namespace_len
;
2021 int searched_deeper
= 0;
2023 struct cleanup
*old_cleanups
;
2024 int new_oload_champ
;
2025 struct symbol
**new_oload_syms
;
2026 struct badness_vector
*new_oload_champ_bv
;
2027 char *new_namespace
;
2029 if (next_namespace_len
!= 0)
2031 gdb_assert (qualified_name
[next_namespace_len
] == ':');
2032 next_namespace_len
+= 2;
2035 += cp_find_first_component (qualified_name
+ next_namespace_len
);
2037 /* Initialize these to values that can safely be xfree'd. */
2039 *oload_champ_bv
= NULL
;
2041 /* First, see if we have a deeper namespace we can search in. If we
2042 get a good match there, use it. */
2044 if (qualified_name
[next_namespace_len
] == ':')
2046 searched_deeper
= 1;
2048 if (find_oload_champ_namespace_loop (arg_types
, nargs
,
2049 func_name
, qualified_name
,
2051 oload_syms
, oload_champ_bv
,
2058 /* If we reach here, either we're in the deepest namespace or we
2059 didn't find a good match in a deeper namespace. But, in the
2060 latter case, we still have a bad match in a deeper namespace;
2061 note that we might not find any match at all in the current
2062 namespace. (There's always a match in the deepest namespace,
2063 because this overload mechanism only gets called if there's a
2064 function symbol to start off with.) */
2066 old_cleanups
= make_cleanup (xfree
, *oload_syms
);
2067 old_cleanups
= make_cleanup (xfree
, *oload_champ_bv
);
2068 new_namespace
= alloca (namespace_len
+ 1);
2069 strncpy (new_namespace
, qualified_name
, namespace_len
);
2070 new_namespace
[namespace_len
] = '\0';
2071 new_oload_syms
= make_symbol_overload_list (func_name
,
2073 while (new_oload_syms
[num_fns
])
2076 new_oload_champ
= find_oload_champ (arg_types
, nargs
, 0, num_fns
,
2077 NULL
, new_oload_syms
,
2078 &new_oload_champ_bv
);
2080 /* Case 1: We found a good match. Free earlier matches (if any),
2081 and return it. Case 2: We didn't find a good match, but we're
2082 not the deepest function. Then go with the bad match that the
2083 deeper function found. Case 3: We found a bad match, and we're
2084 the deepest function. Then return what we found, even though
2085 it's a bad match. */
2087 if (new_oload_champ
!= -1
2088 && classify_oload_match (new_oload_champ_bv
, nargs
, 0) == STANDARD
)
2090 *oload_syms
= new_oload_syms
;
2091 *oload_champ
= new_oload_champ
;
2092 *oload_champ_bv
= new_oload_champ_bv
;
2093 do_cleanups (old_cleanups
);
2096 else if (searched_deeper
)
2098 xfree (new_oload_syms
);
2099 xfree (new_oload_champ_bv
);
2100 discard_cleanups (old_cleanups
);
2105 gdb_assert (new_oload_champ
!= -1);
2106 *oload_syms
= new_oload_syms
;
2107 *oload_champ
= new_oload_champ
;
2108 *oload_champ_bv
= new_oload_champ_bv
;
2109 discard_cleanups (old_cleanups
);
2114 /* Look for a function to take NARGS args of types ARG_TYPES. Find
2115 the best match from among the overloaded methods or functions
2116 (depending on METHOD) given by FNS_PTR or OLOAD_SYMS, respectively.
2117 The number of methods/functions in the list is given by NUM_FNS.
2118 Return the index of the best match; store an indication of the
2119 quality of the match in OLOAD_CHAMP_BV.
2121 It is the caller's responsibility to free *OLOAD_CHAMP_BV. */
2124 find_oload_champ (struct type
**arg_types
, int nargs
, int method
,
2125 int num_fns
, struct fn_field
*fns_ptr
,
2126 struct symbol
**oload_syms
,
2127 struct badness_vector
**oload_champ_bv
)
2130 struct badness_vector
*bv
; /* A measure of how good an overloaded instance is */
2131 int oload_champ
= -1; /* Index of best overloaded function */
2132 int oload_ambiguous
= 0; /* Current ambiguity state for overload resolution */
2133 /* 0 => no ambiguity, 1 => two good funcs, 2 => incomparable funcs */
2135 *oload_champ_bv
= NULL
;
2137 /* Consider each candidate in turn */
2138 for (ix
= 0; ix
< num_fns
; ix
++)
2141 int static_offset
= oload_method_static (method
, fns_ptr
, ix
);
2143 struct type
**parm_types
;
2147 nparms
= TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (fns_ptr
, ix
));
2151 /* If it's not a method, this is the proper place */
2152 nparms
=TYPE_NFIELDS(SYMBOL_TYPE(oload_syms
[ix
]));
2155 /* Prepare array of parameter types */
2156 parm_types
= (struct type
**) xmalloc (nparms
* (sizeof (struct type
*)));
2157 for (jj
= 0; jj
< nparms
; jj
++)
2158 parm_types
[jj
] = (method
2159 ? (TYPE_FN_FIELD_ARGS (fns_ptr
, ix
)[jj
].type
)
2160 : TYPE_FIELD_TYPE (SYMBOL_TYPE (oload_syms
[ix
]), jj
));
2162 /* Compare parameter types to supplied argument types. Skip THIS for
2164 bv
= rank_function (parm_types
, nparms
, arg_types
+ static_offset
,
2165 nargs
- static_offset
);
2167 if (!*oload_champ_bv
)
2169 *oload_champ_bv
= bv
;
2173 /* See whether current candidate is better or worse than previous best */
2174 switch (compare_badness (bv
, *oload_champ_bv
))
2177 oload_ambiguous
= 1; /* top two contenders are equally good */
2180 oload_ambiguous
= 2; /* incomparable top contenders */
2183 *oload_champ_bv
= bv
; /* new champion, record details */
2184 oload_ambiguous
= 0;
2195 fprintf_filtered (gdb_stderr
,"Overloaded method instance %s, # of parms %d\n", fns_ptr
[ix
].physname
, nparms
);
2197 fprintf_filtered (gdb_stderr
,"Overloaded function instance %s # of parms %d\n", SYMBOL_DEMANGLED_NAME (oload_syms
[ix
]), nparms
);
2198 for (jj
= 0; jj
< nargs
- static_offset
; jj
++)
2199 fprintf_filtered (gdb_stderr
,"...Badness @ %d : %d\n", jj
, bv
->rank
[jj
]);
2200 fprintf_filtered (gdb_stderr
,"Overload resolution champion is %d, ambiguous? %d\n", oload_champ
, oload_ambiguous
);
2207 /* Return 1 if we're looking at a static method, 0 if we're looking at
2208 a non-static method or a function that isn't a method. */
2211 oload_method_static (int method
, struct fn_field
*fns_ptr
, int index
)
2213 if (method
&& TYPE_FN_FIELD_STATIC_P (fns_ptr
, index
))
2219 /* Check how good an overload match OLOAD_CHAMP_BV represents. */
2221 static enum oload_classification
2222 classify_oload_match (struct badness_vector
*oload_champ_bv
,
2228 for (ix
= 1; ix
<= nargs
- static_offset
; ix
++)
2230 if (oload_champ_bv
->rank
[ix
] >= 100)
2231 return INCOMPATIBLE
; /* truly mismatched types */
2232 else if (oload_champ_bv
->rank
[ix
] >= 10)
2233 return NON_STANDARD
; /* non-standard type conversions needed */
2236 return STANDARD
; /* Only standard conversions needed. */
2239 /* C++: return 1 is NAME is a legitimate name for the destructor
2240 of type TYPE. If TYPE does not have a destructor, or
2241 if NAME is inappropriate for TYPE, an error is signaled. */
2243 destructor_name_p (const char *name
, const struct type
*type
)
2245 /* destructors are a special case. */
2249 char *dname
= type_name_no_tag (type
);
2250 char *cp
= strchr (dname
, '<');
2253 /* Do not compare the template part for template classes. */
2255 len
= strlen (dname
);
2258 if (strlen (name
+ 1) != len
|| strncmp (dname
, name
+ 1, len
) != 0)
2259 error ("name of destructor must equal name of class");
2266 /* Helper function for check_field: Given TYPE, a structure/union,
2267 return 1 if the component named NAME from the ultimate
2268 target structure/union is defined, otherwise, return 0. */
2271 check_field_in (struct type
*type
, const char *name
)
2275 for (i
= TYPE_NFIELDS (type
) - 1; i
>= TYPE_N_BASECLASSES (type
); i
--)
2277 char *t_field_name
= TYPE_FIELD_NAME (type
, i
);
2278 if (t_field_name
&& (strcmp_iw (t_field_name
, name
) == 0))
2282 /* C++: If it was not found as a data field, then try to
2283 return it as a pointer to a method. */
2285 /* Destructors are a special case. */
2286 if (destructor_name_p (name
, type
))
2288 int m_index
, f_index
;
2290 return get_destructor_fn_field (type
, &m_index
, &f_index
);
2293 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; --i
)
2295 if (strcmp_iw (TYPE_FN_FIELDLIST_NAME (type
, i
), name
) == 0)
2299 for (i
= TYPE_N_BASECLASSES (type
) - 1; i
>= 0; i
--)
2300 if (check_field_in (TYPE_BASECLASS (type
, i
), name
))
2307 /* C++: Given ARG1, a value of type (pointer to a)* structure/union,
2308 return 1 if the component named NAME from the ultimate
2309 target structure/union is defined, otherwise, return 0. */
2312 check_field (struct value
*arg1
, const char *name
)
2316 arg1
= coerce_array (arg1
);
2318 t
= value_type (arg1
);
2320 /* Follow pointers until we get to a non-pointer. */
2325 if (TYPE_CODE (t
) != TYPE_CODE_PTR
&& TYPE_CODE (t
) != TYPE_CODE_REF
)
2327 t
= TYPE_TARGET_TYPE (t
);
2330 if (TYPE_CODE (t
) == TYPE_CODE_MEMBER
)
2331 error ("not implemented: member type in check_field");
2333 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
2334 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
2335 error ("Internal error: `this' is not an aggregate");
2337 return check_field_in (t
, name
);
2340 /* C++: Given an aggregate type CURTYPE, and a member name NAME,
2341 return the appropriate member. This function is used to resolve
2342 user expressions of the form "DOMAIN::NAME". For more details on
2343 what happens, see the comment before
2344 value_struct_elt_for_reference. */
2347 value_aggregate_elt (struct type
*curtype
,
2351 switch (TYPE_CODE (curtype
))
2353 case TYPE_CODE_STRUCT
:
2354 case TYPE_CODE_UNION
:
2355 return value_struct_elt_for_reference (curtype
, 0, curtype
, name
, NULL
,
2357 case TYPE_CODE_NAMESPACE
:
2358 return value_namespace_elt (curtype
, name
, noside
);
2360 internal_error (__FILE__
, __LINE__
,
2361 "non-aggregate type in value_aggregate_elt");
2365 /* C++: Given an aggregate type CURTYPE, and a member name NAME,
2366 return the address of this member as a "pointer to member"
2367 type. If INTYPE is non-null, then it will be the type
2368 of the member we are looking for. This will help us resolve
2369 "pointers to member functions". This function is used
2370 to resolve user expressions of the form "DOMAIN::NAME". */
2372 static struct value
*
2373 value_struct_elt_for_reference (struct type
*domain
, int offset
,
2374 struct type
*curtype
, char *name
,
2375 struct type
*intype
,
2378 struct type
*t
= curtype
;
2382 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
2383 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
2384 error ("Internal error: non-aggregate type to value_struct_elt_for_reference");
2386 for (i
= TYPE_NFIELDS (t
) - 1; i
>= TYPE_N_BASECLASSES (t
); i
--)
2388 char *t_field_name
= TYPE_FIELD_NAME (t
, i
);
2390 if (t_field_name
&& strcmp (t_field_name
, name
) == 0)
2392 if (TYPE_FIELD_STATIC (t
, i
))
2394 v
= value_static_field (t
, i
);
2396 error ("static field %s has been optimized out",
2400 if (TYPE_FIELD_PACKED (t
, i
))
2401 error ("pointers to bitfield members not allowed");
2403 return value_from_longest
2404 (lookup_reference_type (lookup_member_type (TYPE_FIELD_TYPE (t
, i
),
2406 offset
+ (LONGEST
) (TYPE_FIELD_BITPOS (t
, i
) >> 3));
2410 /* C++: If it was not found as a data field, then try to
2411 return it as a pointer to a method. */
2413 /* Destructors are a special case. */
2414 if (destructor_name_p (name
, t
))
2416 error ("member pointers to destructors not implemented yet");
2419 /* Perform all necessary dereferencing. */
2420 while (intype
&& TYPE_CODE (intype
) == TYPE_CODE_PTR
)
2421 intype
= TYPE_TARGET_TYPE (intype
);
2423 for (i
= TYPE_NFN_FIELDS (t
) - 1; i
>= 0; --i
)
2425 char *t_field_name
= TYPE_FN_FIELDLIST_NAME (t
, i
);
2426 char dem_opname
[64];
2428 if (strncmp (t_field_name
, "__", 2) == 0 ||
2429 strncmp (t_field_name
, "op", 2) == 0 ||
2430 strncmp (t_field_name
, "type", 4) == 0)
2432 if (cplus_demangle_opname (t_field_name
, dem_opname
, DMGL_ANSI
))
2433 t_field_name
= dem_opname
;
2434 else if (cplus_demangle_opname (t_field_name
, dem_opname
, 0))
2435 t_field_name
= dem_opname
;
2437 if (t_field_name
&& strcmp (t_field_name
, name
) == 0)
2439 int j
= TYPE_FN_FIELDLIST_LENGTH (t
, i
);
2440 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (t
, i
);
2442 check_stub_method_group (t
, i
);
2444 if (intype
== 0 && j
> 1)
2445 error ("non-unique member `%s' requires type instantiation", name
);
2449 if (TYPE_FN_FIELD_TYPE (f
, j
) == intype
)
2452 error ("no member function matches that type instantiation");
2457 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
2459 return value_from_longest
2460 (lookup_reference_type
2461 (lookup_member_type (TYPE_FN_FIELD_TYPE (f
, j
),
2463 (LONGEST
) METHOD_PTR_FROM_VOFFSET (TYPE_FN_FIELD_VOFFSET (f
, j
)));
2467 struct symbol
*s
= lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f
, j
),
2468 0, VAR_DOMAIN
, 0, NULL
);
2475 v
= read_var_value (s
, 0);
2477 VALUE_TYPE (v
) = lookup_reference_type
2478 (lookup_member_type (TYPE_FN_FIELD_TYPE (f
, j
),
2486 for (i
= TYPE_N_BASECLASSES (t
) - 1; i
>= 0; i
--)
2491 if (BASETYPE_VIA_VIRTUAL (t
, i
))
2494 base_offset
= TYPE_BASECLASS_BITPOS (t
, i
) / 8;
2495 v
= value_struct_elt_for_reference (domain
,
2496 offset
+ base_offset
,
2497 TYPE_BASECLASS (t
, i
),
2505 /* As a last chance, pretend that CURTYPE is a namespace, and look
2506 it up that way; this (frequently) works for types nested inside
2509 return value_maybe_namespace_elt (curtype
, name
, noside
);
2512 /* C++: Return the member NAME of the namespace given by the type
2515 static struct value
*
2516 value_namespace_elt (const struct type
*curtype
,
2520 struct value
*retval
= value_maybe_namespace_elt (curtype
, name
,
2524 error ("No symbol \"%s\" in namespace \"%s\".", name
,
2525 TYPE_TAG_NAME (curtype
));
2530 /* A helper function used by value_namespace_elt and
2531 value_struct_elt_for_reference. It looks up NAME inside the
2532 context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
2533 is a class and NAME refers to a type in CURTYPE itself (as opposed
2534 to, say, some base class of CURTYPE). */
2536 static struct value
*
2537 value_maybe_namespace_elt (const struct type
*curtype
,
2541 const char *namespace_name
= TYPE_TAG_NAME (curtype
);
2544 sym
= cp_lookup_symbol_namespace (namespace_name
, name
, NULL
,
2545 get_selected_block (0), VAR_DOMAIN
,
2550 else if ((noside
== EVAL_AVOID_SIDE_EFFECTS
)
2551 && (SYMBOL_CLASS (sym
) == LOC_TYPEDEF
))
2552 return allocate_value (SYMBOL_TYPE (sym
));
2554 return value_of_variable (sym
, get_selected_block (0));
2557 /* Given a pointer value V, find the real (RTTI) type
2558 of the object it points to.
2559 Other parameters FULL, TOP, USING_ENC as with value_rtti_type()
2560 and refer to the values computed for the object pointed to. */
2563 value_rtti_target_type (struct value
*v
, int *full
, int *top
, int *using_enc
)
2565 struct value
*target
;
2567 target
= value_ind (v
);
2569 return value_rtti_type (target
, full
, top
, using_enc
);
2572 /* Given a value pointed to by ARGP, check its real run-time type, and
2573 if that is different from the enclosing type, create a new value
2574 using the real run-time type as the enclosing type (and of the same
2575 type as ARGP) and return it, with the embedded offset adjusted to
2576 be the correct offset to the enclosed object
2577 RTYPE is the type, and XFULL, XTOP, and XUSING_ENC are the other
2578 parameters, computed by value_rtti_type(). If these are available,
2579 they can be supplied and a second call to value_rtti_type() is avoided.
2580 (Pass RTYPE == NULL if they're not available */
2583 value_full_object (struct value
*argp
, struct type
*rtype
, int xfull
, int xtop
,
2586 struct type
*real_type
;
2590 struct value
*new_val
;
2597 using_enc
= xusing_enc
;
2600 real_type
= value_rtti_type (argp
, &full
, &top
, &using_enc
);
2602 /* If no RTTI data, or if object is already complete, do nothing */
2603 if (!real_type
|| real_type
== value_enclosing_type (argp
))
2606 /* If we have the full object, but for some reason the enclosing
2607 type is wrong, set it *//* pai: FIXME -- sounds iffy */
2610 argp
= value_change_enclosing_type (argp
, real_type
);
2614 /* Check if object is in memory */
2615 if (VALUE_LVAL (argp
) != lval_memory
)
2617 warning ("Couldn't retrieve complete object of RTTI type %s; object may be in register(s).", TYPE_NAME (real_type
));
2622 /* All other cases -- retrieve the complete object */
2623 /* Go back by the computed top_offset from the beginning of the object,
2624 adjusting for the embedded offset of argp if that's what value_rtti_type
2625 used for its computation. */
2626 new_val
= value_at_lazy (real_type
, VALUE_ADDRESS (argp
) - top
+
2627 (using_enc
? 0 : VALUE_EMBEDDED_OFFSET (argp
)));
2628 new_val
->type
= value_type (argp
);
2629 VALUE_EMBEDDED_OFFSET (new_val
) = using_enc
? top
+ VALUE_EMBEDDED_OFFSET (argp
) : top
;
2636 /* Return the value of the local variable, if one exists.
2637 Flag COMPLAIN signals an error if the request is made in an
2638 inappropriate context. */
2641 value_of_local (const char *name
, int complain
)
2643 struct symbol
*func
, *sym
;
2647 if (deprecated_selected_frame
== 0)
2650 error ("no frame selected");
2655 func
= get_frame_function (deprecated_selected_frame
);
2659 error ("no `%s' in nameless context", name
);
2664 b
= SYMBOL_BLOCK_VALUE (func
);
2665 if (dict_empty (BLOCK_DICT (b
)))
2668 error ("no args, no `%s'", name
);
2673 /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
2674 symbol instead of the LOC_ARG one (if both exist). */
2675 sym
= lookup_block_symbol (b
, name
, NULL
, VAR_DOMAIN
);
2679 error ("current stack frame does not contain a variable named `%s'", name
);
2684 ret
= read_var_value (sym
, deprecated_selected_frame
);
2685 if (ret
== 0 && complain
)
2686 error ("`%s' argument unreadable", name
);
2690 /* C++/Objective-C: return the value of the class instance variable,
2691 if one exists. Flag COMPLAIN signals an error if the request is
2692 made in an inappropriate context. */
2695 value_of_this (int complain
)
2697 if (current_language
->la_language
== language_objc
)
2698 return value_of_local ("self", complain
);
2700 return value_of_local ("this", complain
);
2703 /* Create a slice (sub-string, sub-array) of ARRAY, that is LENGTH elements
2704 long, starting at LOWBOUND. The result has the same lower bound as
2705 the original ARRAY. */
2708 value_slice (struct value
*array
, int lowbound
, int length
)
2710 struct type
*slice_range_type
, *slice_type
, *range_type
;
2711 LONGEST lowerbound
, upperbound
;
2712 struct value
*slice
;
2713 struct type
*array_type
;
2714 array_type
= check_typedef (value_type (array
));
2715 if (TYPE_CODE (array_type
) != TYPE_CODE_ARRAY
2716 && TYPE_CODE (array_type
) != TYPE_CODE_STRING
2717 && TYPE_CODE (array_type
) != TYPE_CODE_BITSTRING
)
2718 error ("cannot take slice of non-array");
2719 range_type
= TYPE_INDEX_TYPE (array_type
);
2720 if (get_discrete_bounds (range_type
, &lowerbound
, &upperbound
) < 0)
2721 error ("slice from bad array or bitstring");
2722 if (lowbound
< lowerbound
|| length
< 0
2723 || lowbound
+ length
- 1 > upperbound
)
2724 error ("slice out of range");
2725 /* FIXME-type-allocation: need a way to free this type when we are
2727 slice_range_type
= create_range_type ((struct type
*) NULL
,
2728 TYPE_TARGET_TYPE (range_type
),
2729 lowbound
, lowbound
+ length
- 1);
2730 if (TYPE_CODE (array_type
) == TYPE_CODE_BITSTRING
)
2733 slice_type
= create_set_type ((struct type
*) NULL
, slice_range_type
);
2734 TYPE_CODE (slice_type
) = TYPE_CODE_BITSTRING
;
2735 slice
= value_zero (slice_type
, not_lval
);
2736 for (i
= 0; i
< length
; i
++)
2738 int element
= value_bit_index (array_type
,
2739 VALUE_CONTENTS (array
),
2742 error ("internal error accessing bitstring");
2743 else if (element
> 0)
2745 int j
= i
% TARGET_CHAR_BIT
;
2746 if (BITS_BIG_ENDIAN
)
2747 j
= TARGET_CHAR_BIT
- 1 - j
;
2748 value_contents_raw (slice
)[i
/ TARGET_CHAR_BIT
] |= (1 << j
);
2751 /* We should set the address, bitssize, and bitspos, so the clice
2752 can be used on the LHS, but that may require extensions to
2753 value_assign. For now, just leave as a non_lval. FIXME. */
2757 struct type
*element_type
= TYPE_TARGET_TYPE (array_type
);
2759 = (lowbound
- lowerbound
) * TYPE_LENGTH (check_typedef (element_type
));
2760 slice_type
= create_array_type ((struct type
*) NULL
, element_type
,
2762 TYPE_CODE (slice_type
) = TYPE_CODE (array_type
);
2763 slice
= allocate_value (slice_type
);
2764 if (VALUE_LAZY (array
))
2765 VALUE_LAZY (slice
) = 1;
2767 memcpy (VALUE_CONTENTS (slice
), VALUE_CONTENTS (array
) + offset
,
2768 TYPE_LENGTH (slice_type
));
2769 if (VALUE_LVAL (array
) == lval_internalvar
)
2770 VALUE_LVAL (slice
) = lval_internalvar_component
;
2772 VALUE_LVAL (slice
) = VALUE_LVAL (array
);
2773 VALUE_ADDRESS (slice
) = VALUE_ADDRESS (array
);
2774 VALUE_FRAME_ID (slice
) = VALUE_FRAME_ID (array
);
2775 slice
->offset
= value_offset (array
) + offset
;
2780 /* Create a value for a FORTRAN complex number. Currently most of
2781 the time values are coerced to COMPLEX*16 (i.e. a complex number
2782 composed of 2 doubles. This really should be a smarter routine
2783 that figures out precision inteligently as opposed to assuming
2784 doubles. FIXME: fmb */
2787 value_literal_complex (struct value
*arg1
, struct value
*arg2
, struct type
*type
)
2790 struct type
*real_type
= TYPE_TARGET_TYPE (type
);
2792 val
= allocate_value (type
);
2793 arg1
= value_cast (real_type
, arg1
);
2794 arg2
= value_cast (real_type
, arg2
);
2796 memcpy (value_contents_raw (val
),
2797 VALUE_CONTENTS (arg1
), TYPE_LENGTH (real_type
));
2798 memcpy (value_contents_raw (val
) + TYPE_LENGTH (real_type
),
2799 VALUE_CONTENTS (arg2
), TYPE_LENGTH (real_type
));
2803 /* Cast a value into the appropriate complex data type. */
2805 static struct value
*
2806 cast_into_complex (struct type
*type
, struct value
*val
)
2808 struct type
*real_type
= TYPE_TARGET_TYPE (type
);
2809 if (TYPE_CODE (value_type (val
)) == TYPE_CODE_COMPLEX
)
2811 struct type
*val_real_type
= TYPE_TARGET_TYPE (value_type (val
));
2812 struct value
*re_val
= allocate_value (val_real_type
);
2813 struct value
*im_val
= allocate_value (val_real_type
);
2815 memcpy (value_contents_raw (re_val
),
2816 VALUE_CONTENTS (val
), TYPE_LENGTH (val_real_type
));
2817 memcpy (value_contents_raw (im_val
),
2818 VALUE_CONTENTS (val
) + TYPE_LENGTH (val_real_type
),
2819 TYPE_LENGTH (val_real_type
));
2821 return value_literal_complex (re_val
, im_val
, type
);
2823 else if (TYPE_CODE (value_type (val
)) == TYPE_CODE_FLT
2824 || TYPE_CODE (value_type (val
)) == TYPE_CODE_INT
)
2825 return value_literal_complex (val
, value_zero (real_type
, not_lval
), type
);
2827 error ("cannot cast non-number to complex");
2831 _initialize_valops (void)
2834 deprecated_add_show_from_set
2835 (add_set_cmd ("abandon", class_support
, var_boolean
, (char *) &auto_abandon
,
2836 "Set automatic abandonment of expressions upon failure.",
2841 deprecated_add_show_from_set
2842 (add_set_cmd ("overload-resolution", class_support
, var_boolean
, (char *) &overload_resolution
,
2843 "Set overload resolution in evaluating C++ functions.",
2846 overload_resolution
= 1;