1 /* Perform non-arithmetic operations on values, for GDB.
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
3 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
38 #include "dictionary.h"
39 #include "cp-support.h"
42 #include "gdb_string.h"
43 #include "gdb_assert.h"
44 #include "cp-support.h"
47 extern int overload_debug;
48 /* Local functions. */
50 static int typecmp (int staticp, int varargs, int nargs,
51 struct field t1[], struct value *t2[]);
53 static struct value *search_struct_field (char *, struct value *, int,
56 static struct value *search_struct_method (char *, struct value **,
58 int, int *, struct type *);
60 static int find_oload_champ_namespace (struct type **arg_types, int nargs,
61 const char *func_name,
62 const char *qualified_name,
63 struct symbol ***oload_syms,
64 struct badness_vector **oload_champ_bv);
67 int find_oload_champ_namespace_loop (struct type **arg_types, int nargs,
68 const char *func_name,
69 const char *qualified_name,
71 struct symbol ***oload_syms,
72 struct badness_vector **oload_champ_bv,
75 static int find_oload_champ (struct type **arg_types, int nargs, int method,
77 struct fn_field *fns_ptr,
78 struct symbol **oload_syms,
79 struct badness_vector **oload_champ_bv);
81 static int oload_method_static (int method, struct fn_field *fns_ptr,
84 enum oload_classification { STANDARD, NON_STANDARD, INCOMPATIBLE };
87 oload_classification classify_oload_match (struct badness_vector
92 static int check_field_in (struct type *, const char *);
94 static struct value *value_struct_elt_for_reference (struct type *domain,
101 static struct value *value_namespace_elt (const struct type *curtype,
105 static struct value *value_maybe_namespace_elt (const struct type *curtype,
109 static CORE_ADDR allocate_space_in_inferior (int);
111 static struct value *cast_into_complex (struct type *, struct value *);
113 static struct fn_field *find_method_list (struct value ** argp, char *method,
115 struct type *type, int *num_fns,
116 struct type **basetype,
119 void _initialize_valops (void);
121 /* Flag for whether we want to abandon failed expression evals by default. */
124 static int auto_abandon = 0;
127 int overload_resolution = 0;
129 /* Find the address of function name NAME in the inferior. */
132 find_function_in_inferior (const char *name)
135 sym = lookup_symbol (name, 0, VAR_DOMAIN, 0, NULL);
138 if (SYMBOL_CLASS (sym) != LOC_BLOCK)
140 error ("\"%s\" exists in this program but is not a function.",
143 return value_of_variable (sym, NULL);
147 struct minimal_symbol *msymbol = lookup_minimal_symbol (name, NULL, NULL);
152 type = lookup_pointer_type (builtin_type_char);
153 type = lookup_function_type (type);
154 type = lookup_pointer_type (type);
155 maddr = SYMBOL_VALUE_ADDRESS (msymbol);
156 return value_from_pointer (type, maddr);
160 if (!target_has_execution)
161 error ("evaluation of this expression requires the target program to be active");
163 error ("evaluation of this expression requires the program to have a function \"%s\".", name);
168 /* Allocate NBYTES of space in the inferior using the inferior's malloc
169 and return a value that is a pointer to the allocated space. */
172 value_allocate_space_in_inferior (int len)
174 struct value *blocklen;
175 struct value *val = find_function_in_inferior (NAME_OF_MALLOC);
177 blocklen = value_from_longest (builtin_type_int, (LONGEST) len);
178 val = call_function_by_hand (val, 1, &blocklen);
179 if (value_logical_not (val))
181 if (!target_has_execution)
182 error ("No memory available to program now: you need to start the target first");
184 error ("No memory available to program: call to malloc failed");
190 allocate_space_in_inferior (int len)
192 return value_as_long (value_allocate_space_in_inferior (len));
195 /* Cast value ARG2 to type TYPE and return as a value.
196 More general than a C cast: accepts any two types of the same length,
197 and if ARG2 is an lvalue it can be cast into anything at all. */
198 /* In C++, casts may change pointer or object representations. */
201 value_cast (struct type *type, struct value *arg2)
203 enum type_code code1;
204 enum type_code code2;
208 int convert_to_boolean = 0;
210 if (VALUE_TYPE (arg2) == type)
213 CHECK_TYPEDEF (type);
214 code1 = TYPE_CODE (type);
216 type2 = check_typedef (VALUE_TYPE (arg2));
218 /* A cast to an undetermined-length array_type, such as (TYPE [])OBJECT,
219 is treated like a cast to (TYPE [N])OBJECT,
220 where N is sizeof(OBJECT)/sizeof(TYPE). */
221 if (code1 == TYPE_CODE_ARRAY)
223 struct type *element_type = TYPE_TARGET_TYPE (type);
224 unsigned element_length = TYPE_LENGTH (check_typedef (element_type));
225 if (element_length > 0
226 && TYPE_ARRAY_UPPER_BOUND_TYPE (type) == BOUND_CANNOT_BE_DETERMINED)
228 struct type *range_type = TYPE_INDEX_TYPE (type);
229 int val_length = TYPE_LENGTH (type2);
230 LONGEST low_bound, high_bound, new_length;
231 if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
232 low_bound = 0, high_bound = 0;
233 new_length = val_length / element_length;
234 if (val_length % element_length != 0)
235 warning ("array element type size does not divide object size in cast");
236 /* FIXME-type-allocation: need a way to free this type when we are
238 range_type = create_range_type ((struct type *) NULL,
239 TYPE_TARGET_TYPE (range_type),
241 new_length + low_bound - 1);
242 VALUE_TYPE (arg2) = create_array_type ((struct type *) NULL,
243 element_type, range_type);
248 if (current_language->c_style_arrays
249 && TYPE_CODE (type2) == TYPE_CODE_ARRAY)
250 arg2 = value_coerce_array (arg2);
252 if (TYPE_CODE (type2) == TYPE_CODE_FUNC)
253 arg2 = value_coerce_function (arg2);
255 type2 = check_typedef (VALUE_TYPE (arg2));
256 COERCE_VARYING_ARRAY (arg2, type2);
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),
285 VALUE_TYPE (v) = 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);
383 VALUE_TYPE (v) = type;
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. */
408 VALUE_TYPE (arg2) = 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)
415 return value_at_lazy (type, VALUE_ADDRESS (arg2) + VALUE_OFFSET (arg2),
416 VALUE_BFD_SECTION (arg2));
418 else if (code1 == TYPE_CODE_VOID)
420 return value_zero (builtin_type_void, not_lval);
424 error ("Invalid cast.");
429 /* Create a value of type TYPE that is zero, and return it. */
432 value_zero (struct type *type, enum lval_type lv)
434 struct value *val = allocate_value (type);
436 memset (VALUE_CONTENTS (val), 0, TYPE_LENGTH (check_typedef (type)));
437 VALUE_LVAL (val) = lv;
442 /* Return a value with type TYPE located at ADDR.
444 Call value_at only if the data needs to be fetched immediately;
445 if we can be 'lazy' and defer the fetch, perhaps indefinately, call
446 value_at_lazy instead. value_at_lazy simply records the address of
447 the data and sets the lazy-evaluation-required flag. The lazy flag
448 is tested in the VALUE_CONTENTS macro, which is used if and when
449 the contents are actually required.
451 Note: value_at does *NOT* handle embedded offsets; perform such
452 adjustments before or after calling it. */
455 value_at (struct type *type, CORE_ADDR addr, asection *sect)
459 if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
460 error ("Attempt to dereference a generic pointer.");
462 val = allocate_value (type);
464 read_memory (addr, VALUE_CONTENTS_ALL_RAW (val), TYPE_LENGTH (type));
466 VALUE_LVAL (val) = lval_memory;
467 VALUE_ADDRESS (val) = addr;
468 VALUE_BFD_SECTION (val) = sect;
473 /* Return a lazy value with type TYPE located at ADDR (cf. value_at). */
476 value_at_lazy (struct type *type, CORE_ADDR addr, asection *sect)
480 if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
481 error ("Attempt to dereference a generic pointer.");
483 val = allocate_value (type);
485 VALUE_LVAL (val) = lval_memory;
486 VALUE_ADDRESS (val) = addr;
487 VALUE_LAZY (val) = 1;
488 VALUE_BFD_SECTION (val) = sect;
493 /* Called only from the VALUE_CONTENTS and VALUE_CONTENTS_ALL macros,
494 if the current data for a variable needs to be loaded into
495 VALUE_CONTENTS(VAL). Fetches the data from the user's process, and
496 clears the lazy flag to indicate that the data in the buffer is valid.
498 If the value is zero-length, we avoid calling read_memory, which would
499 abort. We mark the value as fetched anyway -- all 0 bytes of it.
501 This function returns a value because it is used in the VALUE_CONTENTS
502 macro as part of an expression, where a void would not work. The
506 value_fetch_lazy (struct value *val)
508 CORE_ADDR addr = VALUE_ADDRESS (val) + VALUE_OFFSET (val);
509 int length = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (val));
511 struct type *type = VALUE_TYPE (val);
513 read_memory (addr, VALUE_CONTENTS_ALL_RAW (val), length);
515 VALUE_LAZY (val) = 0;
520 /* Store the contents of FROMVAL into the location of TOVAL.
521 Return a new value with the location of TOVAL and contents of FROMVAL. */
524 value_assign (struct value *toval, struct value *fromval)
528 struct frame_id old_frame;
530 if (!toval->modifiable)
531 error ("Left operand of assignment is not a modifiable lvalue.");
535 type = VALUE_TYPE (toval);
536 if (VALUE_LVAL (toval) != lval_internalvar)
537 fromval = value_cast (type, fromval);
539 COERCE_ARRAY (fromval);
540 CHECK_TYPEDEF (type);
542 /* Since modifying a register can trash the frame chain, and modifying memory
543 can trash the frame cache, we save the old frame and then restore the new
545 old_frame = get_frame_id (deprecated_selected_frame);
547 switch (VALUE_LVAL (toval))
549 case lval_internalvar:
550 set_internalvar (VALUE_INTERNALVAR (toval), fromval);
551 val = value_copy (VALUE_INTERNALVAR (toval)->value);
552 val = value_change_enclosing_type (val, VALUE_ENCLOSING_TYPE (fromval));
553 VALUE_EMBEDDED_OFFSET (val) = VALUE_EMBEDDED_OFFSET (fromval);
554 VALUE_POINTED_TO_OFFSET (val) = VALUE_POINTED_TO_OFFSET (fromval);
557 case lval_internalvar_component:
558 set_internalvar_component (VALUE_INTERNALVAR (toval),
559 VALUE_OFFSET (toval),
560 VALUE_BITPOS (toval),
561 VALUE_BITSIZE (toval),
568 CORE_ADDR changed_addr;
570 char buffer[sizeof (LONGEST)];
572 if (VALUE_BITSIZE (toval))
574 /* We assume that the argument to read_memory is in units of
575 host chars. FIXME: Is that correct? */
576 changed_len = (VALUE_BITPOS (toval)
577 + VALUE_BITSIZE (toval)
581 if (changed_len > (int) sizeof (LONGEST))
582 error ("Can't handle bitfields which don't fit in a %d bit word.",
583 (int) sizeof (LONGEST) * HOST_CHAR_BIT);
585 read_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
586 buffer, changed_len);
587 modify_field (buffer, value_as_long (fromval),
588 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
589 changed_addr = VALUE_ADDRESS (toval) + VALUE_OFFSET (toval);
590 dest_buffer = buffer;
594 changed_addr = VALUE_ADDRESS (toval) + VALUE_OFFSET (toval);
595 changed_len = TYPE_LENGTH (type);
596 dest_buffer = VALUE_CONTENTS (fromval);
599 write_memory (changed_addr, dest_buffer, changed_len);
600 if (deprecated_memory_changed_hook)
601 deprecated_memory_changed_hook (changed_addr, changed_len);
605 case lval_reg_frame_relative:
608 struct frame_info *frame;
611 /* Figure out which frame this is in currently. */
612 if (VALUE_LVAL (toval) == lval_register)
614 frame = get_current_frame ();
615 value_reg = VALUE_REGNO (toval);
619 frame = frame_find_by_id (VALUE_FRAME_ID (toval));
620 value_reg = VALUE_FRAME_REGNUM (toval);
624 error ("Value being assigned to is no longer active.");
626 if (VALUE_LVAL (toval) == lval_reg_frame_relative
627 && CONVERT_REGISTER_P (VALUE_FRAME_REGNUM (toval), type))
629 /* If TOVAL is a special machine register requiring
630 conversion of program values to a special raw format. */
631 VALUE_TO_REGISTER (frame, VALUE_FRAME_REGNUM (toval),
632 type, VALUE_CONTENTS (fromval));
636 /* TOVAL is stored in a series of registers in the frame
637 specified by the structure. Copy that value out,
638 modify it, and copy it back in. */
646 /* Locate the first register that falls in the value that
647 needs to be transfered. Compute the offset of the
648 value in that register. */
651 for (reg_offset = value_reg, offset = 0;
652 offset + register_size (current_gdbarch, reg_offset) <= VALUE_OFFSET (toval);
654 byte_offset = VALUE_OFFSET (toval) - offset;
657 /* Compute the number of register aligned values that need
659 if (VALUE_BITSIZE (toval))
660 amount_to_copy = byte_offset + 1;
662 amount_to_copy = byte_offset + TYPE_LENGTH (type);
664 /* And a bounce buffer. Be slightly over generous. */
665 buffer = (char *) alloca (amount_to_copy + MAX_REGISTER_SIZE);
668 for (regno = reg_offset, amount_copied = 0;
669 amount_copied < amount_to_copy;
670 amount_copied += register_size (current_gdbarch, regno), regno++)
671 frame_register_read (frame, regno, buffer + amount_copied);
673 /* Modify what needs to be modified. */
674 if (VALUE_BITSIZE (toval))
675 modify_field (buffer + byte_offset,
676 value_as_long (fromval),
677 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
679 memcpy (buffer + byte_offset, VALUE_CONTENTS (fromval),
683 for (regno = reg_offset, amount_copied = 0;
684 amount_copied < amount_to_copy;
685 amount_copied += register_size (current_gdbarch, regno), regno++)
686 put_frame_register (frame, regno, buffer + amount_copied);
689 if (deprecated_register_changed_hook)
690 deprecated_register_changed_hook (-1);
691 observer_notify_target_changed (¤t_target);
696 error ("Left operand of assignment is not an lvalue.");
699 /* Assigning to the stack pointer, frame pointer, and other
700 (architecture and calling convention specific) registers may
701 cause the frame cache to be out of date. Assigning to memory
702 also can. We just do this on all assignments to registers or
703 memory, for simplicity's sake; I doubt the slowdown matters. */
704 switch (VALUE_LVAL (toval))
708 case lval_reg_frame_relative:
710 reinit_frame_cache ();
712 /* Having destoroyed the frame cache, restore the selected frame. */
714 /* FIXME: cagney/2002-11-02: There has to be a better way of
715 doing this. Instead of constantly saving/restoring the
716 frame. Why not create a get_selected_frame() function that,
717 having saved the selected frame's ID can automatically
718 re-find the previously selected frame automatically. */
721 struct frame_info *fi = frame_find_by_id (old_frame);
731 /* If the field does not entirely fill a LONGEST, then zero the sign bits.
732 If the field is signed, and is negative, then sign extend. */
733 if ((VALUE_BITSIZE (toval) > 0)
734 && (VALUE_BITSIZE (toval) < 8 * (int) sizeof (LONGEST)))
736 LONGEST fieldval = value_as_long (fromval);
737 LONGEST valmask = (((ULONGEST) 1) << VALUE_BITSIZE (toval)) - 1;
740 if (!TYPE_UNSIGNED (type) && (fieldval & (valmask ^ (valmask >> 1))))
741 fieldval |= ~valmask;
743 fromval = value_from_longest (type, fieldval);
746 val = value_copy (toval);
747 memcpy (VALUE_CONTENTS_RAW (val), VALUE_CONTENTS (fromval),
749 VALUE_TYPE (val) = type;
750 val = value_change_enclosing_type (val, VALUE_ENCLOSING_TYPE (fromval));
751 VALUE_EMBEDDED_OFFSET (val) = VALUE_EMBEDDED_OFFSET (fromval);
752 VALUE_POINTED_TO_OFFSET (val) = VALUE_POINTED_TO_OFFSET (fromval);
757 /* Extend a value VAL to COUNT repetitions of its type. */
760 value_repeat (struct value *arg1, int count)
764 if (VALUE_LVAL (arg1) != lval_memory)
765 error ("Only values in memory can be extended with '@'.");
767 error ("Invalid number %d of repetitions.", count);
769 val = allocate_repeat_value (VALUE_ENCLOSING_TYPE (arg1), count);
771 read_memory (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1),
772 VALUE_CONTENTS_ALL_RAW (val),
773 TYPE_LENGTH (VALUE_ENCLOSING_TYPE (val)));
774 VALUE_LVAL (val) = lval_memory;
775 VALUE_ADDRESS (val) = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1);
781 value_of_variable (struct symbol *var, struct block *b)
784 struct frame_info *frame = NULL;
787 frame = NULL; /* Use selected frame. */
788 else if (symbol_read_needs_frame (var))
790 frame = block_innermost_frame (b);
793 if (BLOCK_FUNCTION (b)
794 && SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)))
795 error ("No frame is currently executing in block %s.",
796 SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)));
798 error ("No frame is currently executing in specified block");
802 val = read_var_value (var, frame);
804 error ("Address of symbol \"%s\" is unknown.", SYMBOL_PRINT_NAME (var));
809 /* Given a value which is an array, return a value which is a pointer to its
810 first element, regardless of whether or not the array has a nonzero lower
813 FIXME: A previous comment here indicated that this routine should be
814 substracting the array's lower bound. It's not clear to me that this
815 is correct. Given an array subscripting operation, it would certainly
816 work to do the adjustment here, essentially computing:
818 (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0])
820 However I believe a more appropriate and logical place to account for
821 the lower bound is to do so in value_subscript, essentially computing:
823 (&array[0] + ((index - lowerbound) * sizeof array[0]))
825 As further evidence consider what would happen with operations other
826 than array subscripting, where the caller would get back a value that
827 had an address somewhere before the actual first element of the array,
828 and the information about the lower bound would be lost because of
829 the coercion to pointer type.
833 value_coerce_array (struct value *arg1)
835 struct type *type = check_typedef (VALUE_TYPE (arg1));
837 if (VALUE_LVAL (arg1) != lval_memory)
838 error ("Attempt to take address of value not located in memory.");
840 return value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
841 (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
844 /* Given a value which is a function, return a value which is a pointer
848 value_coerce_function (struct value *arg1)
850 struct value *retval;
852 if (VALUE_LVAL (arg1) != lval_memory)
853 error ("Attempt to take address of value not located in memory.");
855 retval = value_from_pointer (lookup_pointer_type (VALUE_TYPE (arg1)),
856 (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
857 VALUE_BFD_SECTION (retval) = VALUE_BFD_SECTION (arg1);
861 /* Return a pointer value for the object for which ARG1 is the contents. */
864 value_addr (struct value *arg1)
868 struct type *type = check_typedef (VALUE_TYPE (arg1));
869 if (TYPE_CODE (type) == TYPE_CODE_REF)
871 /* Copy the value, but change the type from (T&) to (T*).
872 We keep the same location information, which is efficient,
873 and allows &(&X) to get the location containing the reference. */
874 arg2 = value_copy (arg1);
875 VALUE_TYPE (arg2) = lookup_pointer_type (TYPE_TARGET_TYPE (type));
878 if (TYPE_CODE (type) == TYPE_CODE_FUNC)
879 return value_coerce_function (arg1);
881 if (VALUE_LVAL (arg1) != lval_memory)
882 error ("Attempt to take address of value not located in memory.");
884 /* Get target memory address */
885 arg2 = value_from_pointer (lookup_pointer_type (VALUE_TYPE (arg1)),
886 (VALUE_ADDRESS (arg1)
887 + VALUE_OFFSET (arg1)
888 + VALUE_EMBEDDED_OFFSET (arg1)));
890 /* This may be a pointer to a base subobject; so remember the
891 full derived object's type ... */
892 arg2 = value_change_enclosing_type (arg2, lookup_pointer_type (VALUE_ENCLOSING_TYPE (arg1)));
893 /* ... and also the relative position of the subobject in the full object */
894 VALUE_POINTED_TO_OFFSET (arg2) = VALUE_EMBEDDED_OFFSET (arg1);
895 VALUE_BFD_SECTION (arg2) = VALUE_BFD_SECTION (arg1);
899 /* Given a value of a pointer type, apply the C unary * operator to it. */
902 value_ind (struct value *arg1)
904 struct type *base_type;
909 base_type = check_typedef (VALUE_TYPE (arg1));
911 if (TYPE_CODE (base_type) == TYPE_CODE_MEMBER)
912 error ("not implemented: member types in value_ind");
914 /* Allow * on an integer so we can cast it to whatever we want.
915 This returns an int, which seems like the most C-like thing
916 to do. "long long" variables are rare enough that
917 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
918 if (TYPE_CODE (base_type) == TYPE_CODE_INT)
919 return value_at_lazy (builtin_type_int,
920 (CORE_ADDR) value_as_long (arg1),
921 VALUE_BFD_SECTION (arg1));
922 else if (TYPE_CODE (base_type) == TYPE_CODE_PTR)
924 struct type *enc_type;
925 /* We may be pointing to something embedded in a larger object */
926 /* Get the real type of the enclosing object */
927 enc_type = check_typedef (VALUE_ENCLOSING_TYPE (arg1));
928 enc_type = TYPE_TARGET_TYPE (enc_type);
929 /* Retrieve the enclosing object pointed to */
930 arg2 = value_at_lazy (enc_type,
931 value_as_address (arg1) - VALUE_POINTED_TO_OFFSET (arg1),
932 VALUE_BFD_SECTION (arg1));
934 VALUE_TYPE (arg2) = TYPE_TARGET_TYPE (base_type);
935 /* Add embedding info */
936 arg2 = value_change_enclosing_type (arg2, enc_type);
937 VALUE_EMBEDDED_OFFSET (arg2) = VALUE_POINTED_TO_OFFSET (arg1);
939 /* We may be pointing to an object of some derived type */
940 arg2 = value_full_object (arg2, NULL, 0, 0, 0);
944 error ("Attempt to take contents of a non-pointer value.");
945 return 0; /* For lint -- never reached */
948 /* Pushing small parts of stack frames. */
950 /* Push one word (the size of object that a register holds). */
953 push_word (CORE_ADDR sp, ULONGEST word)
955 int len = DEPRECATED_REGISTER_SIZE;
956 char buffer[MAX_REGISTER_SIZE];
958 store_unsigned_integer (buffer, len, word);
959 if (INNER_THAN (1, 2))
961 /* stack grows downward */
963 write_memory (sp, buffer, len);
967 /* stack grows upward */
968 write_memory (sp, buffer, len);
975 /* Push LEN bytes with data at BUFFER. */
978 push_bytes (CORE_ADDR sp, char *buffer, int len)
980 if (INNER_THAN (1, 2))
982 /* stack grows downward */
984 write_memory (sp, buffer, len);
988 /* stack grows upward */
989 write_memory (sp, buffer, len);
996 /* Create a value for an array by allocating space in the inferior, copying
997 the data into that space, and then setting up an array value.
999 The array bounds are set from LOWBOUND and HIGHBOUND, and the array is
1000 populated from the values passed in ELEMVEC.
1002 The element type of the array is inherited from the type of the
1003 first element, and all elements must have the same size (though we
1004 don't currently enforce any restriction on their types). */
1007 value_array (int lowbound, int highbound, struct value **elemvec)
1011 unsigned int typelength;
1013 struct type *rangetype;
1014 struct type *arraytype;
1017 /* Validate that the bounds are reasonable and that each of the elements
1018 have the same size. */
1020 nelem = highbound - lowbound + 1;
1023 error ("bad array bounds (%d, %d)", lowbound, highbound);
1025 typelength = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (elemvec[0]));
1026 for (idx = 1; idx < nelem; idx++)
1028 if (TYPE_LENGTH (VALUE_ENCLOSING_TYPE (elemvec[idx])) != typelength)
1030 error ("array elements must all be the same size");
1034 rangetype = create_range_type ((struct type *) NULL, builtin_type_int,
1035 lowbound, highbound);
1036 arraytype = create_array_type ((struct type *) NULL,
1037 VALUE_ENCLOSING_TYPE (elemvec[0]), rangetype);
1039 if (!current_language->c_style_arrays)
1041 val = allocate_value (arraytype);
1042 for (idx = 0; idx < nelem; idx++)
1044 memcpy (VALUE_CONTENTS_ALL_RAW (val) + (idx * typelength),
1045 VALUE_CONTENTS_ALL (elemvec[idx]),
1048 VALUE_BFD_SECTION (val) = VALUE_BFD_SECTION (elemvec[0]);
1052 /* Allocate space to store the array in the inferior, and then initialize
1053 it by copying in each element. FIXME: Is it worth it to create a
1054 local buffer in which to collect each value and then write all the
1055 bytes in one operation? */
1057 addr = allocate_space_in_inferior (nelem * typelength);
1058 for (idx = 0; idx < nelem; idx++)
1060 write_memory (addr + (idx * typelength), VALUE_CONTENTS_ALL (elemvec[idx]),
1064 /* Create the array type and set up an array value to be evaluated lazily. */
1066 val = value_at_lazy (arraytype, addr, VALUE_BFD_SECTION (elemvec[0]));
1070 /* Create a value for a string constant by allocating space in the inferior,
1071 copying the data into that space, and returning the address with type
1072 TYPE_CODE_STRING. PTR points to the string constant data; LEN is number
1074 Note that string types are like array of char types with a lower bound of
1075 zero and an upper bound of LEN - 1. Also note that the string may contain
1076 embedded null bytes. */
1079 value_string (char *ptr, int len)
1082 int lowbound = current_language->string_lower_bound;
1083 struct type *rangetype = create_range_type ((struct type *) NULL,
1085 lowbound, len + lowbound - 1);
1086 struct type *stringtype
1087 = create_string_type ((struct type *) NULL, rangetype);
1090 if (current_language->c_style_arrays == 0)
1092 val = allocate_value (stringtype);
1093 memcpy (VALUE_CONTENTS_RAW (val), ptr, len);
1098 /* Allocate space to store the string in the inferior, and then
1099 copy LEN bytes from PTR in gdb to that address in the inferior. */
1101 addr = allocate_space_in_inferior (len);
1102 write_memory (addr, ptr, len);
1104 val = value_at_lazy (stringtype, addr, NULL);
1109 value_bitstring (char *ptr, int len)
1112 struct type *domain_type = create_range_type (NULL, builtin_type_int,
1114 struct type *type = create_set_type ((struct type *) NULL, domain_type);
1115 TYPE_CODE (type) = TYPE_CODE_BITSTRING;
1116 val = allocate_value (type);
1117 memcpy (VALUE_CONTENTS_RAW (val), ptr, TYPE_LENGTH (type));
1121 /* See if we can pass arguments in T2 to a function which takes arguments
1122 of types T1. T1 is a list of NARGS arguments, and T2 is a NULL-terminated
1123 vector. If some arguments need coercion of some sort, then the coerced
1124 values are written into T2. Return value is 0 if the arguments could be
1125 matched, or the position at which they differ if not.
1127 STATICP is nonzero if the T1 argument list came from a
1128 static member function. T2 will still include the ``this'' pointer,
1129 but it will be skipped.
1131 For non-static member functions, we ignore the first argument,
1132 which is the type of the instance variable. This is because we want
1133 to handle calls with objects from derived classes. This is not
1134 entirely correct: we should actually check to make sure that a
1135 requested operation is type secure, shouldn't we? FIXME. */
1138 typecmp (int staticp, int varargs, int nargs,
1139 struct field t1[], struct value *t2[])
1144 internal_error (__FILE__, __LINE__, "typecmp: no argument list");
1146 /* Skip ``this'' argument if applicable. T2 will always include THIS. */
1151 (i < nargs) && TYPE_CODE (t1[i].type) != TYPE_CODE_VOID;
1154 struct type *tt1, *tt2;
1159 tt1 = check_typedef (t1[i].type);
1160 tt2 = check_typedef (VALUE_TYPE (t2[i]));
1162 if (TYPE_CODE (tt1) == TYPE_CODE_REF
1163 /* We should be doing hairy argument matching, as below. */
1164 && (TYPE_CODE (check_typedef (TYPE_TARGET_TYPE (tt1))) == TYPE_CODE (tt2)))
1166 if (TYPE_CODE (tt2) == TYPE_CODE_ARRAY)
1167 t2[i] = value_coerce_array (t2[i]);
1169 t2[i] = value_addr (t2[i]);
1173 /* djb - 20000715 - Until the new type structure is in the
1174 place, and we can attempt things like implicit conversions,
1175 we need to do this so you can take something like a map<const
1176 char *>, and properly access map["hello"], because the
1177 argument to [] will be a reference to a pointer to a char,
1178 and the argument will be a pointer to a char. */
1179 while ( TYPE_CODE(tt1) == TYPE_CODE_REF ||
1180 TYPE_CODE (tt1) == TYPE_CODE_PTR)
1182 tt1 = check_typedef( TYPE_TARGET_TYPE(tt1) );
1184 while ( TYPE_CODE(tt2) == TYPE_CODE_ARRAY ||
1185 TYPE_CODE(tt2) == TYPE_CODE_PTR ||
1186 TYPE_CODE(tt2) == TYPE_CODE_REF)
1188 tt2 = check_typedef( TYPE_TARGET_TYPE(tt2) );
1190 if (TYPE_CODE (tt1) == TYPE_CODE (tt2))
1192 /* Array to pointer is a `trivial conversion' according to the ARM. */
1194 /* We should be doing much hairier argument matching (see section 13.2
1195 of the ARM), but as a quick kludge, just check for the same type
1197 if (TYPE_CODE (t1[i].type) != TYPE_CODE (VALUE_TYPE (t2[i])))
1200 if (varargs || t2[i] == NULL)
1205 /* Helper function used by value_struct_elt to recurse through baseclasses.
1206 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1207 and search in it assuming it has (class) type TYPE.
1208 If found, return value, else return NULL.
1210 If LOOKING_FOR_BASECLASS, then instead of looking for struct fields,
1211 look for a baseclass named NAME. */
1213 static struct value *
1214 search_struct_field (char *name, struct value *arg1, int offset,
1215 struct type *type, int looking_for_baseclass)
1218 int nbases = TYPE_N_BASECLASSES (type);
1220 CHECK_TYPEDEF (type);
1222 if (!looking_for_baseclass)
1223 for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
1225 char *t_field_name = TYPE_FIELD_NAME (type, i);
1227 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
1230 if (TYPE_FIELD_STATIC (type, i))
1232 v = value_static_field (type, i);
1234 error ("field %s is nonexistent or has been optimised out",
1239 v = value_primitive_field (arg1, offset, i, type);
1241 error ("there is no field named %s", name);
1247 && (t_field_name[0] == '\0'
1248 || (TYPE_CODE (type) == TYPE_CODE_UNION
1249 && (strcmp_iw (t_field_name, "else") == 0))))
1251 struct type *field_type = TYPE_FIELD_TYPE (type, i);
1252 if (TYPE_CODE (field_type) == TYPE_CODE_UNION
1253 || TYPE_CODE (field_type) == TYPE_CODE_STRUCT)
1255 /* Look for a match through the fields of an anonymous union,
1256 or anonymous struct. C++ provides anonymous unions.
1258 In the GNU Chill (now deleted from GDB)
1259 implementation of variant record types, each
1260 <alternative field> has an (anonymous) union type,
1261 each member of the union represents a <variant
1262 alternative>. Each <variant alternative> is
1263 represented as a struct, with a member for each
1267 int new_offset = offset;
1269 /* This is pretty gross. In G++, the offset in an
1270 anonymous union is relative to the beginning of the
1271 enclosing struct. In the GNU Chill (now deleted
1272 from GDB) implementation of variant records, the
1273 bitpos is zero in an anonymous union field, so we
1274 have to add the offset of the union here. */
1275 if (TYPE_CODE (field_type) == TYPE_CODE_STRUCT
1276 || (TYPE_NFIELDS (field_type) > 0
1277 && TYPE_FIELD_BITPOS (field_type, 0) == 0))
1278 new_offset += TYPE_FIELD_BITPOS (type, i) / 8;
1280 v = search_struct_field (name, arg1, new_offset, field_type,
1281 looking_for_baseclass);
1288 for (i = 0; i < nbases; i++)
1291 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
1292 /* If we are looking for baseclasses, this is what we get when we
1293 hit them. But it could happen that the base part's member name
1294 is not yet filled in. */
1295 int found_baseclass = (looking_for_baseclass
1296 && TYPE_BASECLASS_NAME (type, i) != NULL
1297 && (strcmp_iw (name, TYPE_BASECLASS_NAME (type, i)) == 0));
1299 if (BASETYPE_VIA_VIRTUAL (type, i))
1302 struct value *v2 = allocate_value (basetype);
1304 boffset = baseclass_offset (type, i,
1305 VALUE_CONTENTS (arg1) + offset,
1306 VALUE_ADDRESS (arg1)
1307 + VALUE_OFFSET (arg1) + offset);
1309 error ("virtual baseclass botch");
1311 /* The virtual base class pointer might have been clobbered by the
1312 user program. Make sure that it still points to a valid memory
1316 if (boffset < 0 || boffset >= TYPE_LENGTH (type))
1318 CORE_ADDR base_addr;
1320 base_addr = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1) + boffset;
1321 if (target_read_memory (base_addr, VALUE_CONTENTS_RAW (v2),
1322 TYPE_LENGTH (basetype)) != 0)
1323 error ("virtual baseclass botch");
1324 VALUE_LVAL (v2) = lval_memory;
1325 VALUE_ADDRESS (v2) = base_addr;
1329 VALUE_LVAL (v2) = VALUE_LVAL (arg1);
1330 VALUE_ADDRESS (v2) = VALUE_ADDRESS (arg1);
1331 VALUE_OFFSET (v2) = VALUE_OFFSET (arg1) + boffset;
1332 if (VALUE_LAZY (arg1))
1333 VALUE_LAZY (v2) = 1;
1335 memcpy (VALUE_CONTENTS_RAW (v2),
1336 VALUE_CONTENTS_RAW (arg1) + boffset,
1337 TYPE_LENGTH (basetype));
1340 if (found_baseclass)
1342 v = search_struct_field (name, v2, 0, TYPE_BASECLASS (type, i),
1343 looking_for_baseclass);
1345 else if (found_baseclass)
1346 v = value_primitive_field (arg1, offset, i, type);
1348 v = search_struct_field (name, arg1,
1349 offset + TYPE_BASECLASS_BITPOS (type, i) / 8,
1350 basetype, looking_for_baseclass);
1358 /* Return the offset (in bytes) of the virtual base of type BASETYPE
1359 * in an object pointed to by VALADDR (on the host), assumed to be of
1360 * type TYPE. OFFSET is number of bytes beyond start of ARG to start
1361 * looking (in case VALADDR is the contents of an enclosing object).
1363 * This routine recurses on the primary base of the derived class because
1364 * the virtual base entries of the primary base appear before the other
1365 * virtual base entries.
1367 * If the virtual base is not found, a negative integer is returned.
1368 * The magnitude of the negative integer is the number of entries in
1369 * the virtual table to skip over (entries corresponding to various
1370 * ancestral classes in the chain of primary bases).
1372 * Important: This assumes the HP / Taligent C++ runtime
1373 * conventions. Use baseclass_offset() instead to deal with g++
1377 find_rt_vbase_offset (struct type *type, struct type *basetype, char *valaddr,
1378 int offset, int *boffset_p, int *skip_p)
1380 int boffset; /* offset of virtual base */
1381 int index; /* displacement to use in virtual table */
1385 CORE_ADDR vtbl; /* the virtual table pointer */
1386 struct type *pbc; /* the primary base class */
1388 /* Look for the virtual base recursively in the primary base, first.
1389 * This is because the derived class object and its primary base
1390 * subobject share the primary virtual table. */
1393 pbc = TYPE_PRIMARY_BASE (type);
1396 find_rt_vbase_offset (pbc, basetype, valaddr, offset, &boffset, &skip);
1399 *boffset_p = boffset;
1408 /* Find the index of the virtual base according to HP/Taligent
1409 runtime spec. (Depth-first, left-to-right.) */
1410 index = virtual_base_index_skip_primaries (basetype, type);
1414 *skip_p = skip + virtual_base_list_length_skip_primaries (type);
1419 /* pai: FIXME -- 32x64 possible problem */
1420 /* First word (4 bytes) in object layout is the vtable pointer */
1421 vtbl = *(CORE_ADDR *) (valaddr + offset);
1423 /* Before the constructor is invoked, things are usually zero'd out. */
1425 error ("Couldn't find virtual table -- object may not be constructed yet.");
1428 /* Find virtual base's offset -- jump over entries for primary base
1429 * ancestors, then use the index computed above. But also adjust by
1430 * HP_ACC_VBASE_START for the vtable slots before the start of the
1431 * virtual base entries. Offset is negative -- virtual base entries
1432 * appear _before_ the address point of the virtual table. */
1434 /* pai: FIXME -- 32x64 problem, if word = 8 bytes, change multiplier
1437 /* epstein : FIXME -- added param for overlay section. May not be correct */
1438 vp = value_at (builtin_type_int, vtbl + 4 * (-skip - index - HP_ACC_VBASE_START), NULL);
1439 boffset = value_as_long (vp);
1441 *boffset_p = boffset;
1446 /* Helper function used by value_struct_elt to recurse through baseclasses.
1447 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1448 and search in it assuming it has (class) type TYPE.
1449 If found, return value, else if name matched and args not return (value)-1,
1450 else return NULL. */
1452 static struct value *
1453 search_struct_method (char *name, struct value **arg1p,
1454 struct value **args, int offset,
1455 int *static_memfuncp, struct type *type)
1459 int name_matched = 0;
1460 char dem_opname[64];
1462 CHECK_TYPEDEF (type);
1463 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
1465 char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
1466 /* FIXME! May need to check for ARM demangling here */
1467 if (strncmp (t_field_name, "__", 2) == 0 ||
1468 strncmp (t_field_name, "op", 2) == 0 ||
1469 strncmp (t_field_name, "type", 4) == 0)
1471 if (cplus_demangle_opname (t_field_name, dem_opname, DMGL_ANSI))
1472 t_field_name = dem_opname;
1473 else if (cplus_demangle_opname (t_field_name, dem_opname, 0))
1474 t_field_name = dem_opname;
1476 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
1478 int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1;
1479 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1482 check_stub_method_group (type, i);
1483 if (j > 0 && args == 0)
1484 error ("cannot resolve overloaded method `%s': no arguments supplied", name);
1485 else if (j == 0 && args == 0)
1487 v = value_fn_field (arg1p, f, j, type, offset);
1494 if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j),
1495 TYPE_VARARGS (TYPE_FN_FIELD_TYPE (f, j)),
1496 TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f, j)),
1497 TYPE_FN_FIELD_ARGS (f, j), args))
1499 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1500 return value_virtual_fn_field (arg1p, f, j, type, offset);
1501 if (TYPE_FN_FIELD_STATIC_P (f, j) && static_memfuncp)
1502 *static_memfuncp = 1;
1503 v = value_fn_field (arg1p, f, j, type, offset);
1512 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1516 if (BASETYPE_VIA_VIRTUAL (type, i))
1518 if (TYPE_HAS_VTABLE (type))
1520 /* HP aCC compiled type, search for virtual base offset
1521 according to HP/Taligent runtime spec. */
1523 find_rt_vbase_offset (type, TYPE_BASECLASS (type, i),
1524 VALUE_CONTENTS_ALL (*arg1p),
1525 offset + VALUE_EMBEDDED_OFFSET (*arg1p),
1526 &base_offset, &skip);
1528 error ("Virtual base class offset not found in vtable");
1532 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
1535 /* The virtual base class pointer might have been clobbered by the
1536 user program. Make sure that it still points to a valid memory
1539 if (offset < 0 || offset >= TYPE_LENGTH (type))
1541 base_valaddr = (char *) alloca (TYPE_LENGTH (baseclass));
1542 if (target_read_memory (VALUE_ADDRESS (*arg1p)
1543 + VALUE_OFFSET (*arg1p) + offset,
1545 TYPE_LENGTH (baseclass)) != 0)
1546 error ("virtual baseclass botch");
1549 base_valaddr = VALUE_CONTENTS (*arg1p) + offset;
1552 baseclass_offset (type, i, base_valaddr,
1553 VALUE_ADDRESS (*arg1p)
1554 + VALUE_OFFSET (*arg1p) + offset);
1555 if (base_offset == -1)
1556 error ("virtual baseclass botch");
1561 base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
1563 v = search_struct_method (name, arg1p, args, base_offset + offset,
1564 static_memfuncp, TYPE_BASECLASS (type, i));
1565 if (v == (struct value *) - 1)
1571 /* FIXME-bothner: Why is this commented out? Why is it here? */
1572 /* *arg1p = arg1_tmp; */
1577 return (struct value *) - 1;
1582 /* Given *ARGP, a value of type (pointer to a)* structure/union,
1583 extract the component named NAME from the ultimate target structure/union
1584 and return it as a value with its appropriate type.
1585 ERR is used in the error message if *ARGP's type is wrong.
1587 C++: ARGS is a list of argument types to aid in the selection of
1588 an appropriate method. Also, handle derived types.
1590 STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
1591 where the truthvalue of whether the function that was resolved was
1592 a static member function or not is stored.
1594 ERR is an error message to be printed in case the field is not found. */
1597 value_struct_elt (struct value **argp, struct value **args,
1598 char *name, int *static_memfuncp, char *err)
1603 COERCE_ARRAY (*argp);
1605 t = check_typedef (VALUE_TYPE (*argp));
1607 /* Follow pointers until we get to a non-pointer. */
1609 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1611 *argp = value_ind (*argp);
1612 /* Don't coerce fn pointer to fn and then back again! */
1613 if (TYPE_CODE (VALUE_TYPE (*argp)) != TYPE_CODE_FUNC)
1614 COERCE_ARRAY (*argp);
1615 t = check_typedef (VALUE_TYPE (*argp));
1618 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1619 error ("not implemented: member type in value_struct_elt");
1621 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1622 && TYPE_CODE (t) != TYPE_CODE_UNION)
1623 error ("Attempt to extract a component of a value that is not a %s.", err);
1625 /* Assume it's not, unless we see that it is. */
1626 if (static_memfuncp)
1627 *static_memfuncp = 0;
1631 /* if there are no arguments ...do this... */
1633 /* Try as a field first, because if we succeed, there
1634 is less work to be done. */
1635 v = search_struct_field (name, *argp, 0, t, 0);
1639 /* C++: If it was not found as a data field, then try to
1640 return it as a pointer to a method. */
1642 if (destructor_name_p (name, t))
1643 error ("Cannot get value of destructor");
1645 v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
1647 if (v == (struct value *) - 1)
1648 error ("Cannot take address of a method");
1651 if (TYPE_NFN_FIELDS (t))
1652 error ("There is no member or method named %s.", name);
1654 error ("There is no member named %s.", name);
1659 if (destructor_name_p (name, t))
1663 /* Destructors are a special case. */
1664 int m_index, f_index;
1667 if (get_destructor_fn_field (t, &m_index, &f_index))
1669 v = value_fn_field (NULL, TYPE_FN_FIELDLIST1 (t, m_index),
1673 error ("could not find destructor function named %s.", name);
1679 error ("destructor should not have any argument");
1683 v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
1685 if (v == (struct value *) - 1)
1687 error ("One of the arguments you tried to pass to %s could not be converted to what the function wants.", name);
1691 /* See if user tried to invoke data as function. If so,
1692 hand it back. If it's not callable (i.e., a pointer to function),
1693 gdb should give an error. */
1694 v = search_struct_field (name, *argp, 0, t, 0);
1698 error ("Structure has no component named %s.", name);
1702 /* Search through the methods of an object (and its bases)
1703 * to find a specified method. Return the pointer to the
1704 * fn_field list of overloaded instances.
1705 * Helper function for value_find_oload_list.
1706 * ARGP is a pointer to a pointer to a value (the object)
1707 * METHOD is a string containing the method name
1708 * OFFSET is the offset within the value
1709 * TYPE is the assumed type of the object
1710 * NUM_FNS is the number of overloaded instances
1711 * BASETYPE is set to the actual type of the subobject where the method is found
1712 * BOFFSET is the offset of the base subobject where the method is found */
1714 static struct fn_field *
1715 find_method_list (struct value **argp, char *method, int offset,
1716 struct type *type, int *num_fns,
1717 struct type **basetype, int *boffset)
1721 CHECK_TYPEDEF (type);
1725 /* First check in object itself */
1726 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
1728 /* pai: FIXME What about operators and type conversions? */
1729 char *fn_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
1730 if (fn_field_name && (strcmp_iw (fn_field_name, method) == 0))
1732 int len = TYPE_FN_FIELDLIST_LENGTH (type, i);
1733 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1739 /* Resolve any stub methods. */
1740 check_stub_method_group (type, i);
1746 /* Not found in object, check in base subobjects */
1747 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1750 if (BASETYPE_VIA_VIRTUAL (type, i))
1752 if (TYPE_HAS_VTABLE (type))
1754 /* HP aCC compiled type, search for virtual base offset
1755 * according to HP/Taligent runtime spec. */
1757 find_rt_vbase_offset (type, TYPE_BASECLASS (type, i),
1758 VALUE_CONTENTS_ALL (*argp),
1759 offset + VALUE_EMBEDDED_OFFSET (*argp),
1760 &base_offset, &skip);
1762 error ("Virtual base class offset not found in vtable");
1766 /* probably g++ runtime model */
1767 base_offset = VALUE_OFFSET (*argp) + offset;
1769 baseclass_offset (type, i,
1770 VALUE_CONTENTS (*argp) + base_offset,
1771 VALUE_ADDRESS (*argp) + base_offset);
1772 if (base_offset == -1)
1773 error ("virtual baseclass botch");
1777 /* non-virtual base, simply use bit position from debug info */
1779 base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
1781 f = find_method_list (argp, method, base_offset + offset,
1782 TYPE_BASECLASS (type, i), num_fns, basetype,
1790 /* Return the list of overloaded methods of a specified name.
1791 * ARGP is a pointer to a pointer to a value (the object)
1792 * METHOD is the method name
1793 * OFFSET is the offset within the value contents
1794 * NUM_FNS is the number of overloaded instances
1795 * BASETYPE is set to the type of the base subobject that defines the method
1796 * BOFFSET is the offset of the base subobject which defines the method */
1799 value_find_oload_method_list (struct value **argp, char *method, int offset,
1800 int *num_fns, struct type **basetype,
1805 t = check_typedef (VALUE_TYPE (*argp));
1807 /* code snarfed from value_struct_elt */
1808 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1810 *argp = value_ind (*argp);
1811 /* Don't coerce fn pointer to fn and then back again! */
1812 if (TYPE_CODE (VALUE_TYPE (*argp)) != TYPE_CODE_FUNC)
1813 COERCE_ARRAY (*argp);
1814 t = check_typedef (VALUE_TYPE (*argp));
1817 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1818 error ("Not implemented: member type in value_find_oload_lis");
1820 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1821 && TYPE_CODE (t) != TYPE_CODE_UNION)
1822 error ("Attempt to extract a component of a value that is not a struct or union");
1824 return find_method_list (argp, method, 0, t, num_fns, basetype, boffset);
1827 /* Given an array of argument types (ARGTYPES) (which includes an
1828 entry for "this" in the case of C++ methods), the number of
1829 arguments NARGS, the NAME of a function whether it's a method or
1830 not (METHOD), and the degree of laxness (LAX) in conforming to
1831 overload resolution rules in ANSI C++, find the best function that
1832 matches on the argument types according to the overload resolution
1835 In the case of class methods, the parameter OBJ is an object value
1836 in which to search for overloaded methods.
1838 In the case of non-method functions, the parameter FSYM is a symbol
1839 corresponding to one of the overloaded functions.
1841 Return value is an integer: 0 -> good match, 10 -> debugger applied
1842 non-standard coercions, 100 -> incompatible.
1844 If a method is being searched for, VALP will hold the value.
1845 If a non-method is being searched for, SYMP will hold the symbol for it.
1847 If a method is being searched for, and it is a static method,
1848 then STATICP will point to a non-zero value.
1850 Note: This function does *not* check the value of
1851 overload_resolution. Caller must check it to see whether overload
1852 resolution is permitted.
1856 find_overload_match (struct type **arg_types, int nargs, char *name, int method,
1857 int lax, struct value **objp, struct symbol *fsym,
1858 struct value **valp, struct symbol **symp, int *staticp)
1860 struct value *obj = (objp ? *objp : NULL);
1862 int oload_champ; /* Index of best overloaded function */
1864 struct badness_vector *oload_champ_bv = NULL; /* The measure for the current best match */
1866 struct value *temp = obj;
1867 struct fn_field *fns_ptr = NULL; /* For methods, the list of overloaded methods */
1868 struct symbol **oload_syms = NULL; /* For non-methods, the list of overloaded function symbols */
1869 int num_fns = 0; /* Number of overloaded instances being considered */
1870 struct type *basetype = NULL;
1874 struct cleanup *old_cleanups = NULL;
1876 const char *obj_type_name = NULL;
1877 char *func_name = NULL;
1878 enum oload_classification match_quality;
1880 /* Get the list of overloaded methods or functions */
1883 obj_type_name = TYPE_NAME (VALUE_TYPE (obj));
1884 /* Hack: evaluate_subexp_standard often passes in a pointer
1885 value rather than the object itself, so try again */
1886 if ((!obj_type_name || !*obj_type_name) &&
1887 (TYPE_CODE (VALUE_TYPE (obj)) == TYPE_CODE_PTR))
1888 obj_type_name = TYPE_NAME (TYPE_TARGET_TYPE (VALUE_TYPE (obj)));
1890 fns_ptr = value_find_oload_method_list (&temp, name, 0,
1892 &basetype, &boffset);
1893 if (!fns_ptr || !num_fns)
1894 error ("Couldn't find method %s%s%s",
1896 (obj_type_name && *obj_type_name) ? "::" : "",
1898 /* If we are dealing with stub method types, they should have
1899 been resolved by find_method_list via value_find_oload_method_list
1901 gdb_assert (TYPE_DOMAIN_TYPE (fns_ptr[0].type) != NULL);
1902 oload_champ = find_oload_champ (arg_types, nargs, method, num_fns,
1903 fns_ptr, oload_syms, &oload_champ_bv);
1907 const char *qualified_name = SYMBOL_CPLUS_DEMANGLED_NAME (fsym);
1908 func_name = cp_func_name (qualified_name);
1910 /* If the name is NULL this must be a C-style function.
1911 Just return the same symbol. */
1912 if (func_name == NULL)
1918 old_cleanups = make_cleanup (xfree, func_name);
1919 make_cleanup (xfree, oload_syms);
1920 make_cleanup (xfree, oload_champ_bv);
1922 oload_champ = find_oload_champ_namespace (arg_types, nargs,
1929 /* Check how bad the best match is. */
1932 = classify_oload_match (oload_champ_bv, nargs,
1933 oload_method_static (method, fns_ptr,
1936 if (match_quality == INCOMPATIBLE)
1939 error ("Cannot resolve method %s%s%s to any overloaded instance",
1941 (obj_type_name && *obj_type_name) ? "::" : "",
1944 error ("Cannot resolve function %s to any overloaded instance",
1947 else if (match_quality == NON_STANDARD)
1950 warning ("Using non-standard conversion to match method %s%s%s to supplied arguments",
1952 (obj_type_name && *obj_type_name) ? "::" : "",
1955 warning ("Using non-standard conversion to match function %s to supplied arguments",
1961 if (staticp != NULL)
1962 *staticp = oload_method_static (method, fns_ptr, oload_champ);
1963 if (TYPE_FN_FIELD_VIRTUAL_P (fns_ptr, oload_champ))
1964 *valp = value_virtual_fn_field (&temp, fns_ptr, oload_champ, basetype, boffset);
1966 *valp = value_fn_field (&temp, fns_ptr, oload_champ, basetype, boffset);
1970 *symp = oload_syms[oload_champ];
1975 if (TYPE_CODE (VALUE_TYPE (temp)) != TYPE_CODE_PTR
1976 && TYPE_CODE (VALUE_TYPE (*objp)) == TYPE_CODE_PTR)
1978 temp = value_addr (temp);
1982 if (old_cleanups != NULL)
1983 do_cleanups (old_cleanups);
1985 switch (match_quality)
1991 default: /* STANDARD */
1996 /* Find the best overload match, searching for FUNC_NAME in namespaces
1997 contained in QUALIFIED_NAME until it either finds a good match or
1998 runs out of namespaces. It stores the overloaded functions in
1999 *OLOAD_SYMS, and the badness vector in *OLOAD_CHAMP_BV. The
2000 calling function is responsible for freeing *OLOAD_SYMS and
2004 find_oload_champ_namespace (struct type **arg_types, int nargs,
2005 const char *func_name,
2006 const char *qualified_name,
2007 struct symbol ***oload_syms,
2008 struct badness_vector **oload_champ_bv)
2012 find_oload_champ_namespace_loop (arg_types, nargs,
2015 oload_syms, oload_champ_bv,
2021 /* Helper function for find_oload_champ_namespace; NAMESPACE_LEN is
2022 how deep we've looked for namespaces, and the champ is stored in
2023 OLOAD_CHAMP. The return value is 1 if the champ is a good one, 0
2026 It is the caller's responsibility to free *OLOAD_SYMS and
2030 find_oload_champ_namespace_loop (struct type **arg_types, int nargs,
2031 const char *func_name,
2032 const char *qualified_name,
2034 struct symbol ***oload_syms,
2035 struct badness_vector **oload_champ_bv,
2038 int next_namespace_len = namespace_len;
2039 int searched_deeper = 0;
2041 struct cleanup *old_cleanups;
2042 int new_oload_champ;
2043 struct symbol **new_oload_syms;
2044 struct badness_vector *new_oload_champ_bv;
2045 char *new_namespace;
2047 if (next_namespace_len != 0)
2049 gdb_assert (qualified_name[next_namespace_len] == ':');
2050 next_namespace_len += 2;
2053 += cp_find_first_component (qualified_name + next_namespace_len);
2055 /* Initialize these to values that can safely be xfree'd. */
2057 *oload_champ_bv = NULL;
2059 /* First, see if we have a deeper namespace we can search in. If we
2060 get a good match there, use it. */
2062 if (qualified_name[next_namespace_len] == ':')
2064 searched_deeper = 1;
2066 if (find_oload_champ_namespace_loop (arg_types, nargs,
2067 func_name, qualified_name,
2069 oload_syms, oload_champ_bv,
2076 /* If we reach here, either we're in the deepest namespace or we
2077 didn't find a good match in a deeper namespace. But, in the
2078 latter case, we still have a bad match in a deeper namespace;
2079 note that we might not find any match at all in the current
2080 namespace. (There's always a match in the deepest namespace,
2081 because this overload mechanism only gets called if there's a
2082 function symbol to start off with.) */
2084 old_cleanups = make_cleanup (xfree, *oload_syms);
2085 old_cleanups = make_cleanup (xfree, *oload_champ_bv);
2086 new_namespace = alloca (namespace_len + 1);
2087 strncpy (new_namespace, qualified_name, namespace_len);
2088 new_namespace[namespace_len] = '\0';
2089 new_oload_syms = make_symbol_overload_list (func_name,
2091 while (new_oload_syms[num_fns])
2094 new_oload_champ = find_oload_champ (arg_types, nargs, 0, num_fns,
2095 NULL, new_oload_syms,
2096 &new_oload_champ_bv);
2098 /* Case 1: We found a good match. Free earlier matches (if any),
2099 and return it. Case 2: We didn't find a good match, but we're
2100 not the deepest function. Then go with the bad match that the
2101 deeper function found. Case 3: We found a bad match, and we're
2102 the deepest function. Then return what we found, even though
2103 it's a bad match. */
2105 if (new_oload_champ != -1
2106 && classify_oload_match (new_oload_champ_bv, nargs, 0) == STANDARD)
2108 *oload_syms = new_oload_syms;
2109 *oload_champ = new_oload_champ;
2110 *oload_champ_bv = new_oload_champ_bv;
2111 do_cleanups (old_cleanups);
2114 else if (searched_deeper)
2116 xfree (new_oload_syms);
2117 xfree (new_oload_champ_bv);
2118 discard_cleanups (old_cleanups);
2123 gdb_assert (new_oload_champ != -1);
2124 *oload_syms = new_oload_syms;
2125 *oload_champ = new_oload_champ;
2126 *oload_champ_bv = new_oload_champ_bv;
2127 discard_cleanups (old_cleanups);
2132 /* Look for a function to take NARGS args of types ARG_TYPES. Find
2133 the best match from among the overloaded methods or functions
2134 (depending on METHOD) given by FNS_PTR or OLOAD_SYMS, respectively.
2135 The number of methods/functions in the list is given by NUM_FNS.
2136 Return the index of the best match; store an indication of the
2137 quality of the match in OLOAD_CHAMP_BV.
2139 It is the caller's responsibility to free *OLOAD_CHAMP_BV. */
2142 find_oload_champ (struct type **arg_types, int nargs, int method,
2143 int num_fns, struct fn_field *fns_ptr,
2144 struct symbol **oload_syms,
2145 struct badness_vector **oload_champ_bv)
2148 struct badness_vector *bv; /* A measure of how good an overloaded instance is */
2149 int oload_champ = -1; /* Index of best overloaded function */
2150 int oload_ambiguous = 0; /* Current ambiguity state for overload resolution */
2151 /* 0 => no ambiguity, 1 => two good funcs, 2 => incomparable funcs */
2153 *oload_champ_bv = NULL;
2155 /* Consider each candidate in turn */
2156 for (ix = 0; ix < num_fns; ix++)
2159 int static_offset = oload_method_static (method, fns_ptr, ix);
2161 struct type **parm_types;
2165 nparms = TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (fns_ptr, ix));
2169 /* If it's not a method, this is the proper place */
2170 nparms=TYPE_NFIELDS(SYMBOL_TYPE(oload_syms[ix]));
2173 /* Prepare array of parameter types */
2174 parm_types = (struct type **) xmalloc (nparms * (sizeof (struct type *)));
2175 for (jj = 0; jj < nparms; jj++)
2176 parm_types[jj] = (method
2177 ? (TYPE_FN_FIELD_ARGS (fns_ptr, ix)[jj].type)
2178 : TYPE_FIELD_TYPE (SYMBOL_TYPE (oload_syms[ix]), jj));
2180 /* Compare parameter types to supplied argument types. Skip THIS for
2182 bv = rank_function (parm_types, nparms, arg_types + static_offset,
2183 nargs - static_offset);
2185 if (!*oload_champ_bv)
2187 *oload_champ_bv = bv;
2191 /* See whether current candidate is better or worse than previous best */
2192 switch (compare_badness (bv, *oload_champ_bv))
2195 oload_ambiguous = 1; /* top two contenders are equally good */
2198 oload_ambiguous = 2; /* incomparable top contenders */
2201 *oload_champ_bv = bv; /* new champion, record details */
2202 oload_ambiguous = 0;
2213 fprintf_filtered (gdb_stderr,"Overloaded method instance %s, # of parms %d\n", fns_ptr[ix].physname, nparms);
2215 fprintf_filtered (gdb_stderr,"Overloaded function instance %s # of parms %d\n", SYMBOL_DEMANGLED_NAME (oload_syms[ix]), nparms);
2216 for (jj = 0; jj < nargs - static_offset; jj++)
2217 fprintf_filtered (gdb_stderr,"...Badness @ %d : %d\n", jj, bv->rank[jj]);
2218 fprintf_filtered (gdb_stderr,"Overload resolution champion is %d, ambiguous? %d\n", oload_champ, oload_ambiguous);
2225 /* Return 1 if we're looking at a static method, 0 if we're looking at
2226 a non-static method or a function that isn't a method. */
2229 oload_method_static (int method, struct fn_field *fns_ptr, int index)
2231 if (method && TYPE_FN_FIELD_STATIC_P (fns_ptr, index))
2237 /* Check how good an overload match OLOAD_CHAMP_BV represents. */
2239 static enum oload_classification
2240 classify_oload_match (struct badness_vector *oload_champ_bv,
2246 for (ix = 1; ix <= nargs - static_offset; ix++)
2248 if (oload_champ_bv->rank[ix] >= 100)
2249 return INCOMPATIBLE; /* truly mismatched types */
2250 else if (oload_champ_bv->rank[ix] >= 10)
2251 return NON_STANDARD; /* non-standard type conversions needed */
2254 return STANDARD; /* Only standard conversions needed. */
2257 /* C++: return 1 is NAME is a legitimate name for the destructor
2258 of type TYPE. If TYPE does not have a destructor, or
2259 if NAME is inappropriate for TYPE, an error is signaled. */
2261 destructor_name_p (const char *name, const struct type *type)
2263 /* destructors are a special case. */
2267 char *dname = type_name_no_tag (type);
2268 char *cp = strchr (dname, '<');
2271 /* Do not compare the template part for template classes. */
2273 len = strlen (dname);
2276 if (strlen (name + 1) != len || strncmp (dname, name + 1, len) != 0)
2277 error ("name of destructor must equal name of class");
2284 /* Helper function for check_field: Given TYPE, a structure/union,
2285 return 1 if the component named NAME from the ultimate
2286 target structure/union is defined, otherwise, return 0. */
2289 check_field_in (struct type *type, const char *name)
2293 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
2295 char *t_field_name = TYPE_FIELD_NAME (type, i);
2296 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
2300 /* C++: If it was not found as a data field, then try to
2301 return it as a pointer to a method. */
2303 /* Destructors are a special case. */
2304 if (destructor_name_p (name, type))
2306 int m_index, f_index;
2308 return get_destructor_fn_field (type, &m_index, &f_index);
2311 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
2313 if (strcmp_iw (TYPE_FN_FIELDLIST_NAME (type, i), name) == 0)
2317 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
2318 if (check_field_in (TYPE_BASECLASS (type, i), name))
2325 /* C++: Given ARG1, a value of type (pointer to a)* structure/union,
2326 return 1 if the component named NAME from the ultimate
2327 target structure/union is defined, otherwise, return 0. */
2330 check_field (struct value *arg1, const char *name)
2334 COERCE_ARRAY (arg1);
2336 t = VALUE_TYPE (arg1);
2338 /* Follow pointers until we get to a non-pointer. */
2343 if (TYPE_CODE (t) != TYPE_CODE_PTR && TYPE_CODE (t) != TYPE_CODE_REF)
2345 t = TYPE_TARGET_TYPE (t);
2348 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
2349 error ("not implemented: member type in check_field");
2351 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
2352 && TYPE_CODE (t) != TYPE_CODE_UNION)
2353 error ("Internal error: `this' is not an aggregate");
2355 return check_field_in (t, name);
2358 /* C++: Given an aggregate type CURTYPE, and a member name NAME,
2359 return the appropriate member. This function is used to resolve
2360 user expressions of the form "DOMAIN::NAME". For more details on
2361 what happens, see the comment before
2362 value_struct_elt_for_reference. */
2365 value_aggregate_elt (struct type *curtype,
2369 switch (TYPE_CODE (curtype))
2371 case TYPE_CODE_STRUCT:
2372 case TYPE_CODE_UNION:
2373 return value_struct_elt_for_reference (curtype, 0, curtype, name, NULL,
2375 case TYPE_CODE_NAMESPACE:
2376 return value_namespace_elt (curtype, name, noside);
2378 internal_error (__FILE__, __LINE__,
2379 "non-aggregate type in value_aggregate_elt");
2383 /* C++: Given an aggregate type CURTYPE, and a member name NAME,
2384 return the address of this member as a "pointer to member"
2385 type. If INTYPE is non-null, then it will be the type
2386 of the member we are looking for. This will help us resolve
2387 "pointers to member functions". This function is used
2388 to resolve user expressions of the form "DOMAIN::NAME". */
2390 static struct value *
2391 value_struct_elt_for_reference (struct type *domain, int offset,
2392 struct type *curtype, char *name,
2393 struct type *intype,
2396 struct type *t = curtype;
2400 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
2401 && TYPE_CODE (t) != TYPE_CODE_UNION)
2402 error ("Internal error: non-aggregate type to value_struct_elt_for_reference");
2404 for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
2406 char *t_field_name = TYPE_FIELD_NAME (t, i);
2408 if (t_field_name && strcmp (t_field_name, name) == 0)
2410 if (TYPE_FIELD_STATIC (t, i))
2412 v = value_static_field (t, i);
2414 error ("static field %s has been optimized out",
2418 if (TYPE_FIELD_PACKED (t, i))
2419 error ("pointers to bitfield members not allowed");
2421 return value_from_longest
2422 (lookup_reference_type (lookup_member_type (TYPE_FIELD_TYPE (t, i),
2424 offset + (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3));
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_VIRTUAL_P (f, j))
2477 return value_from_longest
2478 (lookup_reference_type
2479 (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
2481 (LONGEST) METHOD_PTR_FROM_VOFFSET (TYPE_FN_FIELD_VOFFSET (f, j)));
2485 struct symbol *s = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
2486 0, VAR_DOMAIN, 0, NULL);
2493 v = read_var_value (s, 0);
2495 VALUE_TYPE (v) = lookup_reference_type
2496 (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
2504 for (i = TYPE_N_BASECLASSES (t) - 1; i >= 0; i--)
2509 if (BASETYPE_VIA_VIRTUAL (t, i))
2512 base_offset = TYPE_BASECLASS_BITPOS (t, i) / 8;
2513 v = value_struct_elt_for_reference (domain,
2514 offset + base_offset,
2515 TYPE_BASECLASS (t, i),
2523 /* As a last chance, pretend that CURTYPE is a namespace, and look
2524 it up that way; this (frequently) works for types nested inside
2527 return value_maybe_namespace_elt (curtype, name, noside);
2530 /* C++: Return the member NAME of the namespace given by the type
2533 static struct value *
2534 value_namespace_elt (const struct type *curtype,
2538 struct value *retval = value_maybe_namespace_elt (curtype, name,
2542 error ("No symbol \"%s\" in namespace \"%s\".", name,
2543 TYPE_TAG_NAME (curtype));
2548 /* A helper function used by value_namespace_elt and
2549 value_struct_elt_for_reference. It looks up NAME inside the
2550 context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
2551 is a class and NAME refers to a type in CURTYPE itself (as opposed
2552 to, say, some base class of CURTYPE). */
2554 static struct value *
2555 value_maybe_namespace_elt (const struct type *curtype,
2559 const char *namespace_name = TYPE_TAG_NAME (curtype);
2562 sym = cp_lookup_symbol_namespace (namespace_name, name, NULL,
2563 get_selected_block (0), VAR_DOMAIN,
2568 else if ((noside == EVAL_AVOID_SIDE_EFFECTS)
2569 && (SYMBOL_CLASS (sym) == LOC_TYPEDEF))
2570 return allocate_value (SYMBOL_TYPE (sym));
2572 return value_of_variable (sym, get_selected_block (0));
2575 /* Given a pointer value V, find the real (RTTI) type
2576 of the object it points to.
2577 Other parameters FULL, TOP, USING_ENC as with value_rtti_type()
2578 and refer to the values computed for the object pointed to. */
2581 value_rtti_target_type (struct value *v, int *full, int *top, int *using_enc)
2583 struct value *target;
2585 target = value_ind (v);
2587 return value_rtti_type (target, full, top, using_enc);
2590 /* Given a value pointed to by ARGP, check its real run-time type, and
2591 if that is different from the enclosing type, create a new value
2592 using the real run-time type as the enclosing type (and of the same
2593 type as ARGP) and return it, with the embedded offset adjusted to
2594 be the correct offset to the enclosed object
2595 RTYPE is the type, and XFULL, XTOP, and XUSING_ENC are the other
2596 parameters, computed by value_rtti_type(). If these are available,
2597 they can be supplied and a second call to value_rtti_type() is avoided.
2598 (Pass RTYPE == NULL if they're not available */
2601 value_full_object (struct value *argp, struct type *rtype, int xfull, int xtop,
2604 struct type *real_type;
2608 struct value *new_val;
2615 using_enc = xusing_enc;
2618 real_type = value_rtti_type (argp, &full, &top, &using_enc);
2620 /* If no RTTI data, or if object is already complete, do nothing */
2621 if (!real_type || real_type == VALUE_ENCLOSING_TYPE (argp))
2624 /* If we have the full object, but for some reason the enclosing
2625 type is wrong, set it *//* pai: FIXME -- sounds iffy */
2628 argp = value_change_enclosing_type (argp, real_type);
2632 /* Check if object is in memory */
2633 if (VALUE_LVAL (argp) != lval_memory)
2635 warning ("Couldn't retrieve complete object of RTTI type %s; object may be in register(s).", TYPE_NAME (real_type));
2640 /* All other cases -- retrieve the complete object */
2641 /* Go back by the computed top_offset from the beginning of the object,
2642 adjusting for the embedded offset of argp if that's what value_rtti_type
2643 used for its computation. */
2644 new_val = value_at_lazy (real_type, VALUE_ADDRESS (argp) - top +
2645 (using_enc ? 0 : VALUE_EMBEDDED_OFFSET (argp)),
2646 VALUE_BFD_SECTION (argp));
2647 VALUE_TYPE (new_val) = VALUE_TYPE (argp);
2648 VALUE_EMBEDDED_OFFSET (new_val) = using_enc ? top + VALUE_EMBEDDED_OFFSET (argp) : top;
2655 /* Return the value of the local variable, if one exists.
2656 Flag COMPLAIN signals an error if the request is made in an
2657 inappropriate context. */
2660 value_of_local (const char *name, int complain)
2662 struct symbol *func, *sym;
2666 if (deprecated_selected_frame == 0)
2669 error ("no frame selected");
2674 func = get_frame_function (deprecated_selected_frame);
2678 error ("no `%s' in nameless context", name);
2683 b = SYMBOL_BLOCK_VALUE (func);
2684 if (dict_empty (BLOCK_DICT (b)))
2687 error ("no args, no `%s'", name);
2692 /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
2693 symbol instead of the LOC_ARG one (if both exist). */
2694 sym = lookup_block_symbol (b, name, NULL, VAR_DOMAIN);
2698 error ("current stack frame does not contain a variable named `%s'", name);
2703 ret = read_var_value (sym, deprecated_selected_frame);
2704 if (ret == 0 && complain)
2705 error ("`%s' argument unreadable", name);
2709 /* C++/Objective-C: return the value of the class instance variable,
2710 if one exists. Flag COMPLAIN signals an error if the request is
2711 made in an inappropriate context. */
2714 value_of_this (int complain)
2716 if (current_language->la_language == language_objc)
2717 return value_of_local ("self", complain);
2719 return value_of_local ("this", complain);
2722 /* Create a slice (sub-string, sub-array) of ARRAY, that is LENGTH elements
2723 long, starting at LOWBOUND. The result has the same lower bound as
2724 the original ARRAY. */
2727 value_slice (struct value *array, int lowbound, int length)
2729 struct type *slice_range_type, *slice_type, *range_type;
2730 LONGEST lowerbound, upperbound;
2731 struct value *slice;
2732 struct type *array_type;
2733 array_type = check_typedef (VALUE_TYPE (array));
2734 COERCE_VARYING_ARRAY (array, array_type);
2735 if (TYPE_CODE (array_type) != TYPE_CODE_ARRAY
2736 && TYPE_CODE (array_type) != TYPE_CODE_STRING
2737 && TYPE_CODE (array_type) != TYPE_CODE_BITSTRING)
2738 error ("cannot take slice of non-array");
2739 range_type = TYPE_INDEX_TYPE (array_type);
2740 if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0)
2741 error ("slice from bad array or bitstring");
2742 if (lowbound < lowerbound || length < 0
2743 || lowbound + length - 1 > upperbound)
2744 error ("slice out of range");
2745 /* FIXME-type-allocation: need a way to free this type when we are
2747 slice_range_type = create_range_type ((struct type *) NULL,
2748 TYPE_TARGET_TYPE (range_type),
2749 lowbound, lowbound + length - 1);
2750 if (TYPE_CODE (array_type) == TYPE_CODE_BITSTRING)
2753 slice_type = create_set_type ((struct type *) NULL, slice_range_type);
2754 TYPE_CODE (slice_type) = TYPE_CODE_BITSTRING;
2755 slice = value_zero (slice_type, not_lval);
2756 for (i = 0; i < length; i++)
2758 int element = value_bit_index (array_type,
2759 VALUE_CONTENTS (array),
2762 error ("internal error accessing bitstring");
2763 else if (element > 0)
2765 int j = i % TARGET_CHAR_BIT;
2766 if (BITS_BIG_ENDIAN)
2767 j = TARGET_CHAR_BIT - 1 - j;
2768 VALUE_CONTENTS_RAW (slice)[i / TARGET_CHAR_BIT] |= (1 << j);
2771 /* We should set the address, bitssize, and bitspos, so the clice
2772 can be used on the LHS, but that may require extensions to
2773 value_assign. For now, just leave as a non_lval. FIXME. */
2777 struct type *element_type = TYPE_TARGET_TYPE (array_type);
2779 = (lowbound - lowerbound) * TYPE_LENGTH (check_typedef (element_type));
2780 slice_type = create_array_type ((struct type *) NULL, element_type,
2782 TYPE_CODE (slice_type) = TYPE_CODE (array_type);
2783 slice = allocate_value (slice_type);
2784 if (VALUE_LAZY (array))
2785 VALUE_LAZY (slice) = 1;
2787 memcpy (VALUE_CONTENTS (slice), VALUE_CONTENTS (array) + offset,
2788 TYPE_LENGTH (slice_type));
2789 if (VALUE_LVAL (array) == lval_internalvar)
2790 VALUE_LVAL (slice) = lval_internalvar_component;
2792 VALUE_LVAL (slice) = VALUE_LVAL (array);
2793 VALUE_ADDRESS (slice) = VALUE_ADDRESS (array);
2794 VALUE_OFFSET (slice) = VALUE_OFFSET (array) + offset;
2799 /* Create a value for a FORTRAN complex number. Currently most of
2800 the time values are coerced to COMPLEX*16 (i.e. a complex number
2801 composed of 2 doubles. This really should be a smarter routine
2802 that figures out precision inteligently as opposed to assuming
2803 doubles. FIXME: fmb */
2806 value_literal_complex (struct value *arg1, struct value *arg2, struct type *type)
2809 struct type *real_type = TYPE_TARGET_TYPE (type);
2811 val = allocate_value (type);
2812 arg1 = value_cast (real_type, arg1);
2813 arg2 = value_cast (real_type, arg2);
2815 memcpy (VALUE_CONTENTS_RAW (val),
2816 VALUE_CONTENTS (arg1), TYPE_LENGTH (real_type));
2817 memcpy (VALUE_CONTENTS_RAW (val) + TYPE_LENGTH (real_type),
2818 VALUE_CONTENTS (arg2), TYPE_LENGTH (real_type));
2822 /* Cast a value into the appropriate complex data type. */
2824 static struct value *
2825 cast_into_complex (struct type *type, struct value *val)
2827 struct type *real_type = TYPE_TARGET_TYPE (type);
2828 if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_COMPLEX)
2830 struct type *val_real_type = TYPE_TARGET_TYPE (VALUE_TYPE (val));
2831 struct value *re_val = allocate_value (val_real_type);
2832 struct value *im_val = allocate_value (val_real_type);
2834 memcpy (VALUE_CONTENTS_RAW (re_val),
2835 VALUE_CONTENTS (val), TYPE_LENGTH (val_real_type));
2836 memcpy (VALUE_CONTENTS_RAW (im_val),
2837 VALUE_CONTENTS (val) + TYPE_LENGTH (val_real_type),
2838 TYPE_LENGTH (val_real_type));
2840 return value_literal_complex (re_val, im_val, type);
2842 else if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FLT
2843 || TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_INT)
2844 return value_literal_complex (val, value_zero (real_type, not_lval), type);
2846 error ("cannot cast non-number to complex");
2850 _initialize_valops (void)
2853 deprecated_add_show_from_set
2854 (add_set_cmd ("abandon", class_support, var_boolean, (char *) &auto_abandon,
2855 "Set automatic abandonment of expressions upon failure.",
2860 deprecated_add_show_from_set
2861 (add_set_cmd ("overload-resolution", class_support, var_boolean, (char *) &overload_resolution,
2862 "Set overload resolution in evaluating C++ functions.",
2865 overload_resolution = 1;