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);
215 arg2 = coerce_ref (arg2);
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 arg2->type = 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 code2 = TYPE_CODE (type2);
258 if (code1 == TYPE_CODE_COMPLEX)
259 return cast_into_complex (type, arg2);
260 if (code1 == TYPE_CODE_BOOL)
262 code1 = TYPE_CODE_INT;
263 convert_to_boolean = 1;
265 if (code1 == TYPE_CODE_CHAR)
266 code1 = TYPE_CODE_INT;
267 if (code2 == TYPE_CODE_BOOL || code2 == TYPE_CODE_CHAR)
268 code2 = TYPE_CODE_INT;
270 scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
271 || code2 == TYPE_CODE_ENUM || code2 == TYPE_CODE_RANGE);
273 if (code1 == TYPE_CODE_STRUCT
274 && code2 == TYPE_CODE_STRUCT
275 && TYPE_NAME (type) != 0)
277 /* Look in the type of the source to see if it contains the
278 type of the target as a superclass. If so, we'll need to
279 offset the object in addition to changing its type. */
280 struct value *v = search_struct_field (type_name_no_tag (type),
288 if (code1 == TYPE_CODE_FLT && scalar)
289 return value_from_double (type, value_as_double (arg2));
290 else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM
291 || code1 == TYPE_CODE_RANGE)
292 && (scalar || code2 == TYPE_CODE_PTR))
296 if (deprecated_hp_som_som_object_present /* if target compiled by HP aCC */
297 && (code2 == TYPE_CODE_PTR))
300 struct value *retvalp;
302 switch (TYPE_CODE (TYPE_TARGET_TYPE (type2)))
304 /* With HP aCC, pointers to data members have a bias */
305 case TYPE_CODE_MEMBER:
306 retvalp = value_from_longest (type, value_as_long (arg2));
307 /* force evaluation */
308 ptr = (unsigned int *) VALUE_CONTENTS (retvalp);
309 *ptr &= ~0x20000000; /* zap 29th bit to remove bias */
312 /* While pointers to methods don't really point to a function */
313 case TYPE_CODE_METHOD:
314 error ("Pointers to methods not supported with HP aCC");
317 break; /* fall out and go to normal handling */
321 /* When we cast pointers to integers, we mustn't use
322 POINTER_TO_ADDRESS to find the address the pointer
323 represents, as value_as_long would. GDB should evaluate
324 expressions just as the compiler would --- and the compiler
325 sees a cast as a simple reinterpretation of the pointer's
327 if (code2 == TYPE_CODE_PTR)
328 longest = extract_unsigned_integer (VALUE_CONTENTS (arg2),
329 TYPE_LENGTH (type2));
331 longest = value_as_long (arg2);
332 return value_from_longest (type, convert_to_boolean ?
333 (LONGEST) (longest ? 1 : 0) : longest);
335 else if (code1 == TYPE_CODE_PTR && (code2 == TYPE_CODE_INT ||
336 code2 == TYPE_CODE_ENUM ||
337 code2 == TYPE_CODE_RANGE))
339 /* TYPE_LENGTH (type) is the length of a pointer, but we really
340 want the length of an address! -- we are really dealing with
341 addresses (i.e., gdb representations) not pointers (i.e.,
342 target representations) here.
344 This allows things like "print *(int *)0x01000234" to work
345 without printing a misleading message -- which would
346 otherwise occur when dealing with a target having two byte
347 pointers and four byte addresses. */
349 int addr_bit = TARGET_ADDR_BIT;
351 LONGEST longest = value_as_long (arg2);
352 if (addr_bit < sizeof (LONGEST) * HOST_CHAR_BIT)
354 if (longest >= ((LONGEST) 1 << addr_bit)
355 || longest <= -((LONGEST) 1 << addr_bit))
356 warning ("value truncated");
358 return value_from_longest (type, longest);
360 else if (TYPE_LENGTH (type) == TYPE_LENGTH (type2))
362 if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
364 struct type *t1 = check_typedef (TYPE_TARGET_TYPE (type));
365 struct type *t2 = check_typedef (TYPE_TARGET_TYPE (type2));
366 if (TYPE_CODE (t1) == TYPE_CODE_STRUCT
367 && TYPE_CODE (t2) == TYPE_CODE_STRUCT
368 && !value_logical_not (arg2))
372 /* Look in the type of the source to see if it contains the
373 type of the target as a superclass. If so, we'll need to
374 offset the pointer rather than just change its type. */
375 if (TYPE_NAME (t1) != NULL)
377 v = search_struct_field (type_name_no_tag (t1),
378 value_ind (arg2), 0, t2, 1);
387 /* Look in the type of the target to see if it contains the
388 type of the source as a superclass. If so, we'll need to
389 offset the pointer rather than just change its type.
390 FIXME: This fails silently with virtual inheritance. */
391 if (TYPE_NAME (t2) != NULL)
393 v = search_struct_field (type_name_no_tag (t2),
394 value_zero (t1, not_lval), 0, t1, 1);
397 CORE_ADDR addr2 = value_as_address (arg2);
398 addr2 -= (VALUE_ADDRESS (v)
400 + VALUE_EMBEDDED_OFFSET (v));
401 return value_from_pointer (type, addr2);
405 /* No superclass found, just fall through to change ptr type. */
408 arg2 = value_change_enclosing_type (arg2, type);
409 VALUE_POINTED_TO_OFFSET (arg2) = 0; /* pai: chk_val */
412 else if (VALUE_LVAL (arg2) == lval_memory)
413 return value_at_lazy (type, VALUE_ADDRESS (arg2) + value_offset (arg2));
414 else if (code1 == TYPE_CODE_VOID)
416 return value_zero (builtin_type_void, not_lval);
420 error ("Invalid cast.");
425 /* Create a value of type TYPE that is zero, and return it. */
428 value_zero (struct type *type, enum lval_type lv)
430 struct value *val = allocate_value (type);
432 memset (VALUE_CONTENTS (val), 0, TYPE_LENGTH (check_typedef (type)));
433 VALUE_LVAL (val) = lv;
438 /* Return a value with type TYPE located at ADDR.
440 Call value_at only if the data needs to be fetched immediately;
441 if we can be 'lazy' and defer the fetch, perhaps indefinately, call
442 value_at_lazy instead. value_at_lazy simply records the address of
443 the data and sets the lazy-evaluation-required flag. The lazy flag
444 is tested in the VALUE_CONTENTS macro, which is used if and when
445 the contents are actually required.
447 Note: value_at does *NOT* handle embedded offsets; perform such
448 adjustments before or after calling it. */
451 value_at (struct type *type, CORE_ADDR addr)
455 if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
456 error ("Attempt to dereference a generic pointer.");
458 val = allocate_value (type);
460 read_memory (addr, VALUE_CONTENTS_ALL_RAW (val), TYPE_LENGTH (type));
462 VALUE_LVAL (val) = lval_memory;
463 VALUE_ADDRESS (val) = addr;
468 /* Return a lazy value with type TYPE located at ADDR (cf. value_at). */
471 value_at_lazy (struct type *type, CORE_ADDR addr)
475 if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
476 error ("Attempt to dereference a generic pointer.");
478 val = allocate_value (type);
480 VALUE_LVAL (val) = lval_memory;
481 VALUE_ADDRESS (val) = addr;
482 VALUE_LAZY (val) = 1;
487 /* Called only from the VALUE_CONTENTS and VALUE_CONTENTS_ALL macros,
488 if the current data for a variable needs to be loaded into
489 VALUE_CONTENTS(VAL). Fetches the data from the user's process, and
490 clears the lazy flag to indicate that the data in the buffer is valid.
492 If the value is zero-length, we avoid calling read_memory, which would
493 abort. We mark the value as fetched anyway -- all 0 bytes of it.
495 This function returns a value because it is used in the VALUE_CONTENTS
496 macro as part of an expression, where a void would not work. The
500 value_fetch_lazy (struct value *val)
502 CORE_ADDR addr = VALUE_ADDRESS (val) + value_offset (val);
503 int length = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (val));
505 struct type *type = value_type (val);
507 read_memory (addr, VALUE_CONTENTS_ALL_RAW (val), length);
509 VALUE_LAZY (val) = 0;
514 /* Store the contents of FROMVAL into the location of TOVAL.
515 Return a new value with the location of TOVAL and contents of FROMVAL. */
518 value_assign (struct value *toval, struct value *fromval)
522 struct frame_id old_frame;
524 if (!toval->modifiable)
525 error ("Left operand of assignment is not a modifiable lvalue.");
527 toval = coerce_ref (toval);
529 type = value_type (toval);
530 if (VALUE_LVAL (toval) != lval_internalvar)
531 fromval = value_cast (type, fromval);
533 fromval = coerce_array (fromval);
534 CHECK_TYPEDEF (type);
536 /* Since modifying a register can trash the frame chain, and modifying memory
537 can trash the frame cache, we save the old frame and then restore the new
539 old_frame = get_frame_id (deprecated_selected_frame);
541 switch (VALUE_LVAL (toval))
543 case lval_internalvar:
544 set_internalvar (VALUE_INTERNALVAR (toval), fromval);
545 val = value_copy (VALUE_INTERNALVAR (toval)->value);
546 val = value_change_enclosing_type (val, VALUE_ENCLOSING_TYPE (fromval));
547 VALUE_EMBEDDED_OFFSET (val) = VALUE_EMBEDDED_OFFSET (fromval);
548 VALUE_POINTED_TO_OFFSET (val) = VALUE_POINTED_TO_OFFSET (fromval);
551 case lval_internalvar_component:
552 set_internalvar_component (VALUE_INTERNALVAR (toval),
553 value_offset (toval),
554 value_bitpos (toval),
555 value_bitsize (toval),
562 CORE_ADDR changed_addr;
564 char buffer[sizeof (LONGEST)];
566 if (value_bitsize (toval))
568 /* We assume that the argument to read_memory is in units of
569 host chars. FIXME: Is that correct? */
570 changed_len = (value_bitpos (toval)
571 + value_bitsize (toval)
575 if (changed_len > (int) sizeof (LONGEST))
576 error ("Can't handle bitfields which don't fit in a %d bit word.",
577 (int) sizeof (LONGEST) * HOST_CHAR_BIT);
579 read_memory (VALUE_ADDRESS (toval) + value_offset (toval),
580 buffer, changed_len);
581 modify_field (buffer, value_as_long (fromval),
582 value_bitpos (toval), value_bitsize (toval));
583 changed_addr = VALUE_ADDRESS (toval) + value_offset (toval);
584 dest_buffer = buffer;
588 changed_addr = VALUE_ADDRESS (toval) + value_offset (toval);
589 changed_len = TYPE_LENGTH (type);
590 dest_buffer = VALUE_CONTENTS (fromval);
593 write_memory (changed_addr, dest_buffer, changed_len);
594 if (deprecated_memory_changed_hook)
595 deprecated_memory_changed_hook (changed_addr, changed_len);
601 struct frame_info *frame;
604 /* Figure out which frame this is in currently. */
605 if (VALUE_LVAL (toval) == lval_register)
607 frame = get_current_frame ();
608 value_reg = VALUE_REGNUM (toval);
612 frame = frame_find_by_id (VALUE_FRAME_ID (toval));
613 value_reg = VALUE_REGNUM (toval);
617 error ("Value being assigned to is no longer active.");
619 if (VALUE_LVAL (toval) == lval_register
620 && CONVERT_REGISTER_P (VALUE_REGNUM (toval), type))
622 /* If TOVAL is a special machine register requiring
623 conversion of program values to a special raw format. */
624 VALUE_TO_REGISTER (frame, VALUE_REGNUM (toval),
625 type, VALUE_CONTENTS (fromval));
629 /* TOVAL is stored in a series of registers in the frame
630 specified by the structure. Copy that value out,
631 modify it, and copy it back in. */
639 /* Locate the first register that falls in the value that
640 needs to be transfered. Compute the offset of the
641 value in that register. */
644 for (reg_offset = value_reg, offset = 0;
645 offset + register_size (current_gdbarch, reg_offset) <= value_offset (toval);
647 byte_offset = value_offset (toval) - offset;
650 /* Compute the number of register aligned values that need
652 if (value_bitsize (toval))
653 amount_to_copy = byte_offset + 1;
655 amount_to_copy = byte_offset + TYPE_LENGTH (type);
657 /* And a bounce buffer. Be slightly over generous. */
658 buffer = (char *) alloca (amount_to_copy + MAX_REGISTER_SIZE);
661 for (regno = reg_offset, amount_copied = 0;
662 amount_copied < amount_to_copy;
663 amount_copied += register_size (current_gdbarch, regno), regno++)
664 frame_register_read (frame, regno, buffer + amount_copied);
666 /* Modify what needs to be modified. */
667 if (value_bitsize (toval))
668 modify_field (buffer + byte_offset,
669 value_as_long (fromval),
670 value_bitpos (toval), value_bitsize (toval));
672 memcpy (buffer + byte_offset, VALUE_CONTENTS (fromval),
676 for (regno = reg_offset, amount_copied = 0;
677 amount_copied < amount_to_copy;
678 amount_copied += register_size (current_gdbarch, regno), regno++)
679 put_frame_register (frame, regno, buffer + amount_copied);
682 if (deprecated_register_changed_hook)
683 deprecated_register_changed_hook (-1);
684 observer_notify_target_changed (¤t_target);
689 error ("Left operand of assignment is not an lvalue.");
692 /* Assigning to the stack pointer, frame pointer, and other
693 (architecture and calling convention specific) registers may
694 cause the frame cache to be out of date. Assigning to memory
695 also can. We just do this on all assignments to registers or
696 memory, for simplicity's sake; I doubt the slowdown matters. */
697 switch (VALUE_LVAL (toval))
702 reinit_frame_cache ();
704 /* Having destoroyed the frame cache, restore the selected frame. */
706 /* FIXME: cagney/2002-11-02: There has to be a better way of
707 doing this. Instead of constantly saving/restoring the
708 frame. Why not create a get_selected_frame() function that,
709 having saved the selected frame's ID can automatically
710 re-find the previously selected frame automatically. */
713 struct frame_info *fi = frame_find_by_id (old_frame);
723 /* If the field does not entirely fill a LONGEST, then zero the sign bits.
724 If the field is signed, and is negative, then sign extend. */
725 if ((value_bitsize (toval) > 0)
726 && (value_bitsize (toval) < 8 * (int) sizeof (LONGEST)))
728 LONGEST fieldval = value_as_long (fromval);
729 LONGEST valmask = (((ULONGEST) 1) << value_bitsize (toval)) - 1;
732 if (!TYPE_UNSIGNED (type) && (fieldval & (valmask ^ (valmask >> 1))))
733 fieldval |= ~valmask;
735 fromval = value_from_longest (type, fieldval);
738 val = value_copy (toval);
739 memcpy (VALUE_CONTENTS_RAW (val), VALUE_CONTENTS (fromval),
742 val = value_change_enclosing_type (val, VALUE_ENCLOSING_TYPE (fromval));
743 VALUE_EMBEDDED_OFFSET (val) = VALUE_EMBEDDED_OFFSET (fromval);
744 VALUE_POINTED_TO_OFFSET (val) = VALUE_POINTED_TO_OFFSET (fromval);
749 /* Extend a value VAL to COUNT repetitions of its type. */
752 value_repeat (struct value *arg1, int count)
756 if (VALUE_LVAL (arg1) != lval_memory)
757 error ("Only values in memory can be extended with '@'.");
759 error ("Invalid number %d of repetitions.", count);
761 val = allocate_repeat_value (VALUE_ENCLOSING_TYPE (arg1), count);
763 read_memory (VALUE_ADDRESS (arg1) + value_offset (arg1),
764 VALUE_CONTENTS_ALL_RAW (val),
765 TYPE_LENGTH (VALUE_ENCLOSING_TYPE (val)));
766 VALUE_LVAL (val) = lval_memory;
767 VALUE_ADDRESS (val) = VALUE_ADDRESS (arg1) + value_offset (arg1);
773 value_of_variable (struct symbol *var, struct block *b)
776 struct frame_info *frame = NULL;
779 frame = NULL; /* Use selected frame. */
780 else if (symbol_read_needs_frame (var))
782 frame = block_innermost_frame (b);
785 if (BLOCK_FUNCTION (b)
786 && SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)))
787 error ("No frame is currently executing in block %s.",
788 SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)));
790 error ("No frame is currently executing in specified block");
794 val = read_var_value (var, frame);
796 error ("Address of symbol \"%s\" is unknown.", SYMBOL_PRINT_NAME (var));
801 /* Given a value which is an array, return a value which is a pointer to its
802 first element, regardless of whether or not the array has a nonzero lower
805 FIXME: A previous comment here indicated that this routine should be
806 substracting the array's lower bound. It's not clear to me that this
807 is correct. Given an array subscripting operation, it would certainly
808 work to do the adjustment here, essentially computing:
810 (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0])
812 However I believe a more appropriate and logical place to account for
813 the lower bound is to do so in value_subscript, essentially computing:
815 (&array[0] + ((index - lowerbound) * sizeof array[0]))
817 As further evidence consider what would happen with operations other
818 than array subscripting, where the caller would get back a value that
819 had an address somewhere before the actual first element of the array,
820 and the information about the lower bound would be lost because of
821 the coercion to pointer type.
825 value_coerce_array (struct value *arg1)
827 struct type *type = check_typedef (value_type (arg1));
829 if (VALUE_LVAL (arg1) != lval_memory)
830 error ("Attempt to take address of value not located in memory.");
832 return value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
833 (VALUE_ADDRESS (arg1) + value_offset (arg1)));
836 /* Given a value which is a function, return a value which is a pointer
840 value_coerce_function (struct value *arg1)
842 struct value *retval;
844 if (VALUE_LVAL (arg1) != lval_memory)
845 error ("Attempt to take address of value not located in memory.");
847 retval = value_from_pointer (lookup_pointer_type (value_type (arg1)),
848 (VALUE_ADDRESS (arg1) + value_offset (arg1)));
852 /* Return a pointer value for the object for which ARG1 is the contents. */
855 value_addr (struct value *arg1)
859 struct type *type = check_typedef (value_type (arg1));
860 if (TYPE_CODE (type) == TYPE_CODE_REF)
862 /* Copy the value, but change the type from (T&) to (T*).
863 We keep the same location information, which is efficient,
864 and allows &(&X) to get the location containing the reference. */
865 arg2 = value_copy (arg1);
866 arg2->type = lookup_pointer_type (TYPE_TARGET_TYPE (type));
869 if (TYPE_CODE (type) == TYPE_CODE_FUNC)
870 return value_coerce_function (arg1);
872 if (VALUE_LVAL (arg1) != lval_memory)
873 error ("Attempt to take address of value not located in memory.");
875 /* Get target memory address */
876 arg2 = value_from_pointer (lookup_pointer_type (value_type (arg1)),
877 (VALUE_ADDRESS (arg1)
878 + value_offset (arg1)
879 + VALUE_EMBEDDED_OFFSET (arg1)));
881 /* This may be a pointer to a base subobject; so remember the
882 full derived object's type ... */
883 arg2 = value_change_enclosing_type (arg2, lookup_pointer_type (VALUE_ENCLOSING_TYPE (arg1)));
884 /* ... and also the relative position of the subobject in the full object */
885 VALUE_POINTED_TO_OFFSET (arg2) = VALUE_EMBEDDED_OFFSET (arg1);
889 /* Given a value of a pointer type, apply the C unary * operator to it. */
892 value_ind (struct value *arg1)
894 struct type *base_type;
897 arg1 = coerce_array (arg1);
899 base_type = check_typedef (value_type (arg1));
901 if (TYPE_CODE (base_type) == TYPE_CODE_MEMBER)
902 error ("not implemented: member types in value_ind");
904 /* Allow * on an integer so we can cast it to whatever we want.
905 This returns an int, which seems like the most C-like thing
906 to do. "long long" variables are rare enough that
907 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
908 if (TYPE_CODE (base_type) == TYPE_CODE_INT)
909 return value_at_lazy (builtin_type_int,
910 (CORE_ADDR) value_as_long (arg1));
911 else if (TYPE_CODE (base_type) == TYPE_CODE_PTR)
913 struct type *enc_type;
914 /* We may be pointing to something embedded in a larger object */
915 /* Get the real type of the enclosing object */
916 enc_type = check_typedef (VALUE_ENCLOSING_TYPE (arg1));
917 enc_type = TYPE_TARGET_TYPE (enc_type);
918 /* Retrieve the enclosing object pointed to */
919 arg2 = value_at_lazy (enc_type, (value_as_address (arg1)
920 - VALUE_POINTED_TO_OFFSET (arg1)));
922 arg2->type = TYPE_TARGET_TYPE (base_type);
923 /* Add embedding info */
924 arg2 = value_change_enclosing_type (arg2, enc_type);
925 VALUE_EMBEDDED_OFFSET (arg2) = VALUE_POINTED_TO_OFFSET (arg1);
927 /* We may be pointing to an object of some derived type */
928 arg2 = value_full_object (arg2, NULL, 0, 0, 0);
932 error ("Attempt to take contents of a non-pointer value.");
933 return 0; /* For lint -- never reached */
936 /* Pushing small parts of stack frames. */
938 /* Push one word (the size of object that a register holds). */
941 push_word (CORE_ADDR sp, ULONGEST word)
943 int len = DEPRECATED_REGISTER_SIZE;
944 char buffer[MAX_REGISTER_SIZE];
946 store_unsigned_integer (buffer, len, word);
947 if (INNER_THAN (1, 2))
949 /* stack grows downward */
951 write_memory (sp, buffer, len);
955 /* stack grows upward */
956 write_memory (sp, buffer, len);
963 /* Push LEN bytes with data at BUFFER. */
966 push_bytes (CORE_ADDR sp, char *buffer, int len)
968 if (INNER_THAN (1, 2))
970 /* stack grows downward */
972 write_memory (sp, buffer, len);
976 /* stack grows upward */
977 write_memory (sp, buffer, len);
984 /* Create a value for an array by allocating space in the inferior, copying
985 the data into that space, and then setting up an array value.
987 The array bounds are set from LOWBOUND and HIGHBOUND, and the array is
988 populated from the values passed in ELEMVEC.
990 The element type of the array is inherited from the type of the
991 first element, and all elements must have the same size (though we
992 don't currently enforce any restriction on their types). */
995 value_array (int lowbound, int highbound, struct value **elemvec)
999 unsigned int typelength;
1001 struct type *rangetype;
1002 struct type *arraytype;
1005 /* Validate that the bounds are reasonable and that each of the elements
1006 have the same size. */
1008 nelem = highbound - lowbound + 1;
1011 error ("bad array bounds (%d, %d)", lowbound, highbound);
1013 typelength = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (elemvec[0]));
1014 for (idx = 1; idx < nelem; idx++)
1016 if (TYPE_LENGTH (VALUE_ENCLOSING_TYPE (elemvec[idx])) != typelength)
1018 error ("array elements must all be the same size");
1022 rangetype = create_range_type ((struct type *) NULL, builtin_type_int,
1023 lowbound, highbound);
1024 arraytype = create_array_type ((struct type *) NULL,
1025 VALUE_ENCLOSING_TYPE (elemvec[0]), rangetype);
1027 if (!current_language->c_style_arrays)
1029 val = allocate_value (arraytype);
1030 for (idx = 0; idx < nelem; idx++)
1032 memcpy (VALUE_CONTENTS_ALL_RAW (val) + (idx * typelength),
1033 VALUE_CONTENTS_ALL (elemvec[idx]),
1039 /* Allocate space to store the array in the inferior, and then initialize
1040 it by copying in each element. FIXME: Is it worth it to create a
1041 local buffer in which to collect each value and then write all the
1042 bytes in one operation? */
1044 addr = allocate_space_in_inferior (nelem * typelength);
1045 for (idx = 0; idx < nelem; idx++)
1047 write_memory (addr + (idx * typelength), VALUE_CONTENTS_ALL (elemvec[idx]),
1051 /* Create the array type and set up an array value to be evaluated lazily. */
1053 val = value_at_lazy (arraytype, addr);
1057 /* Create a value for a string constant by allocating space in the inferior,
1058 copying the data into that space, and returning the address with type
1059 TYPE_CODE_STRING. PTR points to the string constant data; LEN is number
1061 Note that string types are like array of char types with a lower bound of
1062 zero and an upper bound of LEN - 1. Also note that the string may contain
1063 embedded null bytes. */
1066 value_string (char *ptr, int len)
1069 int lowbound = current_language->string_lower_bound;
1070 struct type *rangetype = create_range_type ((struct type *) NULL,
1072 lowbound, len + lowbound - 1);
1073 struct type *stringtype
1074 = create_string_type ((struct type *) NULL, rangetype);
1077 if (current_language->c_style_arrays == 0)
1079 val = allocate_value (stringtype);
1080 memcpy (VALUE_CONTENTS_RAW (val), ptr, len);
1085 /* Allocate space to store the string in the inferior, and then
1086 copy LEN bytes from PTR in gdb to that address in the inferior. */
1088 addr = allocate_space_in_inferior (len);
1089 write_memory (addr, ptr, len);
1091 val = value_at_lazy (stringtype, addr);
1096 value_bitstring (char *ptr, int len)
1099 struct type *domain_type = create_range_type (NULL, builtin_type_int,
1101 struct type *type = create_set_type ((struct type *) NULL, domain_type);
1102 TYPE_CODE (type) = TYPE_CODE_BITSTRING;
1103 val = allocate_value (type);
1104 memcpy (VALUE_CONTENTS_RAW (val), ptr, TYPE_LENGTH (type));
1108 /* See if we can pass arguments in T2 to a function which takes arguments
1109 of types T1. T1 is a list of NARGS arguments, and T2 is a NULL-terminated
1110 vector. If some arguments need coercion of some sort, then the coerced
1111 values are written into T2. Return value is 0 if the arguments could be
1112 matched, or the position at which they differ if not.
1114 STATICP is nonzero if the T1 argument list came from a
1115 static member function. T2 will still include the ``this'' pointer,
1116 but it will be skipped.
1118 For non-static member functions, we ignore the first argument,
1119 which is the type of the instance variable. This is because we want
1120 to handle calls with objects from derived classes. This is not
1121 entirely correct: we should actually check to make sure that a
1122 requested operation is type secure, shouldn't we? FIXME. */
1125 typecmp (int staticp, int varargs, int nargs,
1126 struct field t1[], struct value *t2[])
1131 internal_error (__FILE__, __LINE__, "typecmp: no argument list");
1133 /* Skip ``this'' argument if applicable. T2 will always include THIS. */
1138 (i < nargs) && TYPE_CODE (t1[i].type) != TYPE_CODE_VOID;
1141 struct type *tt1, *tt2;
1146 tt1 = check_typedef (t1[i].type);
1147 tt2 = check_typedef (value_type (t2[i]));
1149 if (TYPE_CODE (tt1) == TYPE_CODE_REF
1150 /* We should be doing hairy argument matching, as below. */
1151 && (TYPE_CODE (check_typedef (TYPE_TARGET_TYPE (tt1))) == TYPE_CODE (tt2)))
1153 if (TYPE_CODE (tt2) == TYPE_CODE_ARRAY)
1154 t2[i] = value_coerce_array (t2[i]);
1156 t2[i] = value_addr (t2[i]);
1160 /* djb - 20000715 - Until the new type structure is in the
1161 place, and we can attempt things like implicit conversions,
1162 we need to do this so you can take something like a map<const
1163 char *>, and properly access map["hello"], because the
1164 argument to [] will be a reference to a pointer to a char,
1165 and the argument will be a pointer to a char. */
1166 while ( TYPE_CODE(tt1) == TYPE_CODE_REF ||
1167 TYPE_CODE (tt1) == TYPE_CODE_PTR)
1169 tt1 = check_typedef( TYPE_TARGET_TYPE(tt1) );
1171 while ( TYPE_CODE(tt2) == TYPE_CODE_ARRAY ||
1172 TYPE_CODE(tt2) == TYPE_CODE_PTR ||
1173 TYPE_CODE(tt2) == TYPE_CODE_REF)
1175 tt2 = check_typedef( TYPE_TARGET_TYPE(tt2) );
1177 if (TYPE_CODE (tt1) == TYPE_CODE (tt2))
1179 /* Array to pointer is a `trivial conversion' according to the ARM. */
1181 /* We should be doing much hairier argument matching (see section 13.2
1182 of the ARM), but as a quick kludge, just check for the same type
1184 if (TYPE_CODE (t1[i].type) != TYPE_CODE (value_type (t2[i])))
1187 if (varargs || t2[i] == NULL)
1192 /* Helper function used by value_struct_elt to recurse through baseclasses.
1193 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1194 and search in it assuming it has (class) type TYPE.
1195 If found, return value, else return NULL.
1197 If LOOKING_FOR_BASECLASS, then instead of looking for struct fields,
1198 look for a baseclass named NAME. */
1200 static struct value *
1201 search_struct_field (char *name, struct value *arg1, int offset,
1202 struct type *type, int looking_for_baseclass)
1205 int nbases = TYPE_N_BASECLASSES (type);
1207 CHECK_TYPEDEF (type);
1209 if (!looking_for_baseclass)
1210 for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
1212 char *t_field_name = TYPE_FIELD_NAME (type, i);
1214 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
1217 if (TYPE_FIELD_STATIC (type, i))
1219 v = value_static_field (type, i);
1221 error ("field %s is nonexistent or has been optimised out",
1226 v = value_primitive_field (arg1, offset, i, type);
1228 error ("there is no field named %s", name);
1234 && (t_field_name[0] == '\0'
1235 || (TYPE_CODE (type) == TYPE_CODE_UNION
1236 && (strcmp_iw (t_field_name, "else") == 0))))
1238 struct type *field_type = TYPE_FIELD_TYPE (type, i);
1239 if (TYPE_CODE (field_type) == TYPE_CODE_UNION
1240 || TYPE_CODE (field_type) == TYPE_CODE_STRUCT)
1242 /* Look for a match through the fields of an anonymous union,
1243 or anonymous struct. C++ provides anonymous unions.
1245 In the GNU Chill (now deleted from GDB)
1246 implementation of variant record types, each
1247 <alternative field> has an (anonymous) union type,
1248 each member of the union represents a <variant
1249 alternative>. Each <variant alternative> is
1250 represented as a struct, with a member for each
1254 int new_offset = offset;
1256 /* This is pretty gross. In G++, the offset in an
1257 anonymous union is relative to the beginning of the
1258 enclosing struct. In the GNU Chill (now deleted
1259 from GDB) implementation of variant records, the
1260 bitpos is zero in an anonymous union field, so we
1261 have to add the offset of the union here. */
1262 if (TYPE_CODE (field_type) == TYPE_CODE_STRUCT
1263 || (TYPE_NFIELDS (field_type) > 0
1264 && TYPE_FIELD_BITPOS (field_type, 0) == 0))
1265 new_offset += TYPE_FIELD_BITPOS (type, i) / 8;
1267 v = search_struct_field (name, arg1, new_offset, field_type,
1268 looking_for_baseclass);
1275 for (i = 0; i < nbases; i++)
1278 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
1279 /* If we are looking for baseclasses, this is what we get when we
1280 hit them. But it could happen that the base part's member name
1281 is not yet filled in. */
1282 int found_baseclass = (looking_for_baseclass
1283 && TYPE_BASECLASS_NAME (type, i) != NULL
1284 && (strcmp_iw (name, TYPE_BASECLASS_NAME (type, i)) == 0));
1286 if (BASETYPE_VIA_VIRTUAL (type, i))
1289 struct value *v2 = allocate_value (basetype);
1291 boffset = baseclass_offset (type, i,
1292 VALUE_CONTENTS (arg1) + offset,
1293 VALUE_ADDRESS (arg1)
1294 + value_offset (arg1) + offset);
1296 error ("virtual baseclass botch");
1298 /* The virtual base class pointer might have been clobbered by the
1299 user program. Make sure that it still points to a valid memory
1303 if (boffset < 0 || boffset >= TYPE_LENGTH (type))
1305 CORE_ADDR base_addr;
1307 base_addr = VALUE_ADDRESS (arg1) + value_offset (arg1) + boffset;
1308 if (target_read_memory (base_addr, VALUE_CONTENTS_RAW (v2),
1309 TYPE_LENGTH (basetype)) != 0)
1310 error ("virtual baseclass botch");
1311 VALUE_LVAL (v2) = lval_memory;
1312 VALUE_ADDRESS (v2) = base_addr;
1316 VALUE_LVAL (v2) = VALUE_LVAL (arg1);
1317 VALUE_ADDRESS (v2) = VALUE_ADDRESS (arg1);
1318 v2->offset = value_offset (arg1) + boffset;
1319 if (VALUE_LAZY (arg1))
1320 VALUE_LAZY (v2) = 1;
1322 memcpy (VALUE_CONTENTS_RAW (v2),
1323 VALUE_CONTENTS_RAW (arg1) + boffset,
1324 TYPE_LENGTH (basetype));
1327 if (found_baseclass)
1329 v = search_struct_field (name, v2, 0, TYPE_BASECLASS (type, i),
1330 looking_for_baseclass);
1332 else if (found_baseclass)
1333 v = value_primitive_field (arg1, offset, i, type);
1335 v = search_struct_field (name, arg1,
1336 offset + TYPE_BASECLASS_BITPOS (type, i) / 8,
1337 basetype, looking_for_baseclass);
1345 /* Return the offset (in bytes) of the virtual base of type BASETYPE
1346 * in an object pointed to by VALADDR (on the host), assumed to be of
1347 * type TYPE. OFFSET is number of bytes beyond start of ARG to start
1348 * looking (in case VALADDR is the contents of an enclosing object).
1350 * This routine recurses on the primary base of the derived class because
1351 * the virtual base entries of the primary base appear before the other
1352 * virtual base entries.
1354 * If the virtual base is not found, a negative integer is returned.
1355 * The magnitude of the negative integer is the number of entries in
1356 * the virtual table to skip over (entries corresponding to various
1357 * ancestral classes in the chain of primary bases).
1359 * Important: This assumes the HP / Taligent C++ runtime
1360 * conventions. Use baseclass_offset() instead to deal with g++
1364 find_rt_vbase_offset (struct type *type, struct type *basetype, char *valaddr,
1365 int offset, int *boffset_p, int *skip_p)
1367 int boffset; /* offset of virtual base */
1368 int index; /* displacement to use in virtual table */
1372 CORE_ADDR vtbl; /* the virtual table pointer */
1373 struct type *pbc; /* the primary base class */
1375 /* Look for the virtual base recursively in the primary base, first.
1376 * This is because the derived class object and its primary base
1377 * subobject share the primary virtual table. */
1380 pbc = TYPE_PRIMARY_BASE (type);
1383 find_rt_vbase_offset (pbc, basetype, valaddr, offset, &boffset, &skip);
1386 *boffset_p = boffset;
1395 /* Find the index of the virtual base according to HP/Taligent
1396 runtime spec. (Depth-first, left-to-right.) */
1397 index = virtual_base_index_skip_primaries (basetype, type);
1401 *skip_p = skip + virtual_base_list_length_skip_primaries (type);
1406 /* pai: FIXME -- 32x64 possible problem */
1407 /* First word (4 bytes) in object layout is the vtable pointer */
1408 vtbl = *(CORE_ADDR *) (valaddr + offset);
1410 /* Before the constructor is invoked, things are usually zero'd out. */
1412 error ("Couldn't find virtual table -- object may not be constructed yet.");
1415 /* Find virtual base's offset -- jump over entries for primary base
1416 * ancestors, then use the index computed above. But also adjust by
1417 * HP_ACC_VBASE_START for the vtable slots before the start of the
1418 * virtual base entries. Offset is negative -- virtual base entries
1419 * appear _before_ the address point of the virtual table. */
1421 /* pai: FIXME -- 32x64 problem, if word = 8 bytes, change multiplier
1424 /* epstein : FIXME -- added param for overlay section. May not be correct */
1425 vp = value_at (builtin_type_int, vtbl + 4 * (-skip - index - HP_ACC_VBASE_START));
1426 boffset = value_as_long (vp);
1428 *boffset_p = boffset;
1433 /* Helper function used by value_struct_elt to recurse through baseclasses.
1434 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1435 and search in it assuming it has (class) type TYPE.
1436 If found, return value, else if name matched and args not return (value)-1,
1437 else return NULL. */
1439 static struct value *
1440 search_struct_method (char *name, struct value **arg1p,
1441 struct value **args, int offset,
1442 int *static_memfuncp, struct type *type)
1446 int name_matched = 0;
1447 char dem_opname[64];
1449 CHECK_TYPEDEF (type);
1450 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
1452 char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
1453 /* FIXME! May need to check for ARM demangling here */
1454 if (strncmp (t_field_name, "__", 2) == 0 ||
1455 strncmp (t_field_name, "op", 2) == 0 ||
1456 strncmp (t_field_name, "type", 4) == 0)
1458 if (cplus_demangle_opname (t_field_name, dem_opname, DMGL_ANSI))
1459 t_field_name = dem_opname;
1460 else if (cplus_demangle_opname (t_field_name, dem_opname, 0))
1461 t_field_name = dem_opname;
1463 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
1465 int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1;
1466 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1469 check_stub_method_group (type, i);
1470 if (j > 0 && args == 0)
1471 error ("cannot resolve overloaded method `%s': no arguments supplied", name);
1472 else if (j == 0 && args == 0)
1474 v = value_fn_field (arg1p, f, j, type, offset);
1481 if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j),
1482 TYPE_VARARGS (TYPE_FN_FIELD_TYPE (f, j)),
1483 TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f, j)),
1484 TYPE_FN_FIELD_ARGS (f, j), args))
1486 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1487 return value_virtual_fn_field (arg1p, f, j, type, offset);
1488 if (TYPE_FN_FIELD_STATIC_P (f, j) && static_memfuncp)
1489 *static_memfuncp = 1;
1490 v = value_fn_field (arg1p, f, j, type, offset);
1499 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1503 if (BASETYPE_VIA_VIRTUAL (type, i))
1505 if (TYPE_HAS_VTABLE (type))
1507 /* HP aCC compiled type, search for virtual base offset
1508 according to HP/Taligent runtime spec. */
1510 find_rt_vbase_offset (type, TYPE_BASECLASS (type, i),
1511 VALUE_CONTENTS_ALL (*arg1p),
1512 offset + VALUE_EMBEDDED_OFFSET (*arg1p),
1513 &base_offset, &skip);
1515 error ("Virtual base class offset not found in vtable");
1519 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
1522 /* The virtual base class pointer might have been clobbered by the
1523 user program. Make sure that it still points to a valid memory
1526 if (offset < 0 || offset >= TYPE_LENGTH (type))
1528 base_valaddr = (char *) alloca (TYPE_LENGTH (baseclass));
1529 if (target_read_memory (VALUE_ADDRESS (*arg1p)
1530 + value_offset (*arg1p) + offset,
1532 TYPE_LENGTH (baseclass)) != 0)
1533 error ("virtual baseclass botch");
1536 base_valaddr = VALUE_CONTENTS (*arg1p) + offset;
1539 baseclass_offset (type, i, base_valaddr,
1540 VALUE_ADDRESS (*arg1p)
1541 + value_offset (*arg1p) + offset);
1542 if (base_offset == -1)
1543 error ("virtual baseclass botch");
1548 base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
1550 v = search_struct_method (name, arg1p, args, base_offset + offset,
1551 static_memfuncp, TYPE_BASECLASS (type, i));
1552 if (v == (struct value *) - 1)
1558 /* FIXME-bothner: Why is this commented out? Why is it here? */
1559 /* *arg1p = arg1_tmp; */
1564 return (struct value *) - 1;
1569 /* Given *ARGP, a value of type (pointer to a)* structure/union,
1570 extract the component named NAME from the ultimate target structure/union
1571 and return it as a value with its appropriate type.
1572 ERR is used in the error message if *ARGP's type is wrong.
1574 C++: ARGS is a list of argument types to aid in the selection of
1575 an appropriate method. Also, handle derived types.
1577 STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
1578 where the truthvalue of whether the function that was resolved was
1579 a static member function or not is stored.
1581 ERR is an error message to be printed in case the field is not found. */
1584 value_struct_elt (struct value **argp, struct value **args,
1585 char *name, int *static_memfuncp, char *err)
1590 *argp = coerce_array (*argp);
1592 t = check_typedef (value_type (*argp));
1594 /* Follow pointers until we get to a non-pointer. */
1596 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1598 *argp = value_ind (*argp);
1599 /* Don't coerce fn pointer to fn and then back again! */
1600 if (TYPE_CODE (value_type (*argp)) != TYPE_CODE_FUNC)
1601 *argp = coerce_array (*argp);
1602 t = check_typedef (value_type (*argp));
1605 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1606 error ("not implemented: member type in value_struct_elt");
1608 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1609 && TYPE_CODE (t) != TYPE_CODE_UNION)
1610 error ("Attempt to extract a component of a value that is not a %s.", err);
1612 /* Assume it's not, unless we see that it is. */
1613 if (static_memfuncp)
1614 *static_memfuncp = 0;
1618 /* if there are no arguments ...do this... */
1620 /* Try as a field first, because if we succeed, there
1621 is less work to be done. */
1622 v = search_struct_field (name, *argp, 0, t, 0);
1626 /* C++: If it was not found as a data field, then try to
1627 return it as a pointer to a method. */
1629 if (destructor_name_p (name, t))
1630 error ("Cannot get value of destructor");
1632 v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
1634 if (v == (struct value *) - 1)
1635 error ("Cannot take address of a method");
1638 if (TYPE_NFN_FIELDS (t))
1639 error ("There is no member or method named %s.", name);
1641 error ("There is no member named %s.", name);
1646 if (destructor_name_p (name, t))
1650 /* Destructors are a special case. */
1651 int m_index, f_index;
1654 if (get_destructor_fn_field (t, &m_index, &f_index))
1656 v = value_fn_field (NULL, TYPE_FN_FIELDLIST1 (t, m_index),
1660 error ("could not find destructor function named %s.", name);
1666 error ("destructor should not have any argument");
1670 v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
1672 if (v == (struct value *) - 1)
1674 error ("One of the arguments you tried to pass to %s could not be converted to what the function wants.", name);
1678 /* See if user tried to invoke data as function. If so,
1679 hand it back. If it's not callable (i.e., a pointer to function),
1680 gdb should give an error. */
1681 v = search_struct_field (name, *argp, 0, t, 0);
1685 error ("Structure has no component named %s.", name);
1689 /* Search through the methods of an object (and its bases)
1690 * to find a specified method. Return the pointer to the
1691 * fn_field list of overloaded instances.
1692 * Helper function for value_find_oload_list.
1693 * ARGP is a pointer to a pointer to a value (the object)
1694 * METHOD is a string containing the method name
1695 * OFFSET is the offset within the value
1696 * TYPE is the assumed type of the object
1697 * NUM_FNS is the number of overloaded instances
1698 * BASETYPE is set to the actual type of the subobject where the method is found
1699 * BOFFSET is the offset of the base subobject where the method is found */
1701 static struct fn_field *
1702 find_method_list (struct value **argp, char *method, int offset,
1703 struct type *type, int *num_fns,
1704 struct type **basetype, int *boffset)
1708 CHECK_TYPEDEF (type);
1712 /* First check in object itself */
1713 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
1715 /* pai: FIXME What about operators and type conversions? */
1716 char *fn_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
1717 if (fn_field_name && (strcmp_iw (fn_field_name, method) == 0))
1719 int len = TYPE_FN_FIELDLIST_LENGTH (type, i);
1720 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1726 /* Resolve any stub methods. */
1727 check_stub_method_group (type, i);
1733 /* Not found in object, check in base subobjects */
1734 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1737 if (BASETYPE_VIA_VIRTUAL (type, i))
1739 if (TYPE_HAS_VTABLE (type))
1741 /* HP aCC compiled type, search for virtual base offset
1742 * according to HP/Taligent runtime spec. */
1744 find_rt_vbase_offset (type, TYPE_BASECLASS (type, i),
1745 VALUE_CONTENTS_ALL (*argp),
1746 offset + VALUE_EMBEDDED_OFFSET (*argp),
1747 &base_offset, &skip);
1749 error ("Virtual base class offset not found in vtable");
1753 /* probably g++ runtime model */
1754 base_offset = value_offset (*argp) + offset;
1756 baseclass_offset (type, i,
1757 VALUE_CONTENTS (*argp) + base_offset,
1758 VALUE_ADDRESS (*argp) + base_offset);
1759 if (base_offset == -1)
1760 error ("virtual baseclass botch");
1764 /* non-virtual base, simply use bit position from debug info */
1766 base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
1768 f = find_method_list (argp, method, base_offset + offset,
1769 TYPE_BASECLASS (type, i), num_fns, basetype,
1777 /* Return the list of overloaded methods of a specified name.
1778 * ARGP is a pointer to a pointer to a value (the object)
1779 * METHOD is the method name
1780 * OFFSET is the offset within the value contents
1781 * NUM_FNS is the number of overloaded instances
1782 * BASETYPE is set to the type of the base subobject that defines the method
1783 * BOFFSET is the offset of the base subobject which defines the method */
1786 value_find_oload_method_list (struct value **argp, char *method, int offset,
1787 int *num_fns, struct type **basetype,
1792 t = check_typedef (value_type (*argp));
1794 /* code snarfed from value_struct_elt */
1795 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1797 *argp = value_ind (*argp);
1798 /* Don't coerce fn pointer to fn and then back again! */
1799 if (TYPE_CODE (value_type (*argp)) != TYPE_CODE_FUNC)
1800 *argp = coerce_array (*argp);
1801 t = check_typedef (value_type (*argp));
1804 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1805 error ("Not implemented: member type in value_find_oload_lis");
1807 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1808 && TYPE_CODE (t) != TYPE_CODE_UNION)
1809 error ("Attempt to extract a component of a value that is not a struct or union");
1811 return find_method_list (argp, method, 0, t, num_fns, basetype, boffset);
1814 /* Given an array of argument types (ARGTYPES) (which includes an
1815 entry for "this" in the case of C++ methods), the number of
1816 arguments NARGS, the NAME of a function whether it's a method or
1817 not (METHOD), and the degree of laxness (LAX) in conforming to
1818 overload resolution rules in ANSI C++, find the best function that
1819 matches on the argument types according to the overload resolution
1822 In the case of class methods, the parameter OBJ is an object value
1823 in which to search for overloaded methods.
1825 In the case of non-method functions, the parameter FSYM is a symbol
1826 corresponding to one of the overloaded functions.
1828 Return value is an integer: 0 -> good match, 10 -> debugger applied
1829 non-standard coercions, 100 -> incompatible.
1831 If a method is being searched for, VALP will hold the value.
1832 If a non-method is being searched for, SYMP will hold the symbol for it.
1834 If a method is being searched for, and it is a static method,
1835 then STATICP will point to a non-zero value.
1837 Note: This function does *not* check the value of
1838 overload_resolution. Caller must check it to see whether overload
1839 resolution is permitted.
1843 find_overload_match (struct type **arg_types, int nargs, char *name, int method,
1844 int lax, struct value **objp, struct symbol *fsym,
1845 struct value **valp, struct symbol **symp, int *staticp)
1847 struct value *obj = (objp ? *objp : NULL);
1849 int oload_champ; /* Index of best overloaded function */
1851 struct badness_vector *oload_champ_bv = NULL; /* The measure for the current best match */
1853 struct value *temp = obj;
1854 struct fn_field *fns_ptr = NULL; /* For methods, the list of overloaded methods */
1855 struct symbol **oload_syms = NULL; /* For non-methods, the list of overloaded function symbols */
1856 int num_fns = 0; /* Number of overloaded instances being considered */
1857 struct type *basetype = NULL;
1861 struct cleanup *old_cleanups = NULL;
1863 const char *obj_type_name = NULL;
1864 char *func_name = NULL;
1865 enum oload_classification match_quality;
1867 /* Get the list of overloaded methods or functions */
1870 obj_type_name = TYPE_NAME (value_type (obj));
1871 /* Hack: evaluate_subexp_standard often passes in a pointer
1872 value rather than the object itself, so try again */
1873 if ((!obj_type_name || !*obj_type_name) &&
1874 (TYPE_CODE (value_type (obj)) == TYPE_CODE_PTR))
1875 obj_type_name = TYPE_NAME (TYPE_TARGET_TYPE (value_type (obj)));
1877 fns_ptr = value_find_oload_method_list (&temp, name, 0,
1879 &basetype, &boffset);
1880 if (!fns_ptr || !num_fns)
1881 error ("Couldn't find method %s%s%s",
1883 (obj_type_name && *obj_type_name) ? "::" : "",
1885 /* If we are dealing with stub method types, they should have
1886 been resolved by find_method_list via value_find_oload_method_list
1888 gdb_assert (TYPE_DOMAIN_TYPE (fns_ptr[0].type) != NULL);
1889 oload_champ = find_oload_champ (arg_types, nargs, method, num_fns,
1890 fns_ptr, oload_syms, &oload_champ_bv);
1894 const char *qualified_name = SYMBOL_CPLUS_DEMANGLED_NAME (fsym);
1895 func_name = cp_func_name (qualified_name);
1897 /* If the name is NULL this must be a C-style function.
1898 Just return the same symbol. */
1899 if (func_name == NULL)
1905 old_cleanups = make_cleanup (xfree, func_name);
1906 make_cleanup (xfree, oload_syms);
1907 make_cleanup (xfree, oload_champ_bv);
1909 oload_champ = find_oload_champ_namespace (arg_types, nargs,
1916 /* Check how bad the best match is. */
1919 = classify_oload_match (oload_champ_bv, nargs,
1920 oload_method_static (method, fns_ptr,
1923 if (match_quality == INCOMPATIBLE)
1926 error ("Cannot resolve method %s%s%s to any overloaded instance",
1928 (obj_type_name && *obj_type_name) ? "::" : "",
1931 error ("Cannot resolve function %s to any overloaded instance",
1934 else if (match_quality == NON_STANDARD)
1937 warning ("Using non-standard conversion to match method %s%s%s to supplied arguments",
1939 (obj_type_name && *obj_type_name) ? "::" : "",
1942 warning ("Using non-standard conversion to match function %s to supplied arguments",
1948 if (staticp != NULL)
1949 *staticp = oload_method_static (method, fns_ptr, oload_champ);
1950 if (TYPE_FN_FIELD_VIRTUAL_P (fns_ptr, oload_champ))
1951 *valp = value_virtual_fn_field (&temp, fns_ptr, oload_champ, basetype, boffset);
1953 *valp = value_fn_field (&temp, fns_ptr, oload_champ, basetype, boffset);
1957 *symp = oload_syms[oload_champ];
1962 if (TYPE_CODE (value_type (temp)) != TYPE_CODE_PTR
1963 && TYPE_CODE (value_type (*objp)) == TYPE_CODE_PTR)
1965 temp = value_addr (temp);
1969 if (old_cleanups != NULL)
1970 do_cleanups (old_cleanups);
1972 switch (match_quality)
1978 default: /* STANDARD */
1983 /* Find the best overload match, searching for FUNC_NAME in namespaces
1984 contained in QUALIFIED_NAME until it either finds a good match or
1985 runs out of namespaces. It stores the overloaded functions in
1986 *OLOAD_SYMS, and the badness vector in *OLOAD_CHAMP_BV. The
1987 calling function is responsible for freeing *OLOAD_SYMS and
1991 find_oload_champ_namespace (struct type **arg_types, int nargs,
1992 const char *func_name,
1993 const char *qualified_name,
1994 struct symbol ***oload_syms,
1995 struct badness_vector **oload_champ_bv)
1999 find_oload_champ_namespace_loop (arg_types, nargs,
2002 oload_syms, oload_champ_bv,
2008 /* Helper function for find_oload_champ_namespace; NAMESPACE_LEN is
2009 how deep we've looked for namespaces, and the champ is stored in
2010 OLOAD_CHAMP. The return value is 1 if the champ is a good one, 0
2013 It is the caller's responsibility to free *OLOAD_SYMS and
2017 find_oload_champ_namespace_loop (struct type **arg_types, int nargs,
2018 const char *func_name,
2019 const char *qualified_name,
2021 struct symbol ***oload_syms,
2022 struct badness_vector **oload_champ_bv,
2025 int next_namespace_len = namespace_len;
2026 int searched_deeper = 0;
2028 struct cleanup *old_cleanups;
2029 int new_oload_champ;
2030 struct symbol **new_oload_syms;
2031 struct badness_vector *new_oload_champ_bv;
2032 char *new_namespace;
2034 if (next_namespace_len != 0)
2036 gdb_assert (qualified_name[next_namespace_len] == ':');
2037 next_namespace_len += 2;
2040 += cp_find_first_component (qualified_name + next_namespace_len);
2042 /* Initialize these to values that can safely be xfree'd. */
2044 *oload_champ_bv = NULL;
2046 /* First, see if we have a deeper namespace we can search in. If we
2047 get a good match there, use it. */
2049 if (qualified_name[next_namespace_len] == ':')
2051 searched_deeper = 1;
2053 if (find_oload_champ_namespace_loop (arg_types, nargs,
2054 func_name, qualified_name,
2056 oload_syms, oload_champ_bv,
2063 /* If we reach here, either we're in the deepest namespace or we
2064 didn't find a good match in a deeper namespace. But, in the
2065 latter case, we still have a bad match in a deeper namespace;
2066 note that we might not find any match at all in the current
2067 namespace. (There's always a match in the deepest namespace,
2068 because this overload mechanism only gets called if there's a
2069 function symbol to start off with.) */
2071 old_cleanups = make_cleanup (xfree, *oload_syms);
2072 old_cleanups = make_cleanup (xfree, *oload_champ_bv);
2073 new_namespace = alloca (namespace_len + 1);
2074 strncpy (new_namespace, qualified_name, namespace_len);
2075 new_namespace[namespace_len] = '\0';
2076 new_oload_syms = make_symbol_overload_list (func_name,
2078 while (new_oload_syms[num_fns])
2081 new_oload_champ = find_oload_champ (arg_types, nargs, 0, num_fns,
2082 NULL, new_oload_syms,
2083 &new_oload_champ_bv);
2085 /* Case 1: We found a good match. Free earlier matches (if any),
2086 and return it. Case 2: We didn't find a good match, but we're
2087 not the deepest function. Then go with the bad match that the
2088 deeper function found. Case 3: We found a bad match, and we're
2089 the deepest function. Then return what we found, even though
2090 it's a bad match. */
2092 if (new_oload_champ != -1
2093 && classify_oload_match (new_oload_champ_bv, nargs, 0) == STANDARD)
2095 *oload_syms = new_oload_syms;
2096 *oload_champ = new_oload_champ;
2097 *oload_champ_bv = new_oload_champ_bv;
2098 do_cleanups (old_cleanups);
2101 else if (searched_deeper)
2103 xfree (new_oload_syms);
2104 xfree (new_oload_champ_bv);
2105 discard_cleanups (old_cleanups);
2110 gdb_assert (new_oload_champ != -1);
2111 *oload_syms = new_oload_syms;
2112 *oload_champ = new_oload_champ;
2113 *oload_champ_bv = new_oload_champ_bv;
2114 discard_cleanups (old_cleanups);
2119 /* Look for a function to take NARGS args of types ARG_TYPES. Find
2120 the best match from among the overloaded methods or functions
2121 (depending on METHOD) given by FNS_PTR or OLOAD_SYMS, respectively.
2122 The number of methods/functions in the list is given by NUM_FNS.
2123 Return the index of the best match; store an indication of the
2124 quality of the match in OLOAD_CHAMP_BV.
2126 It is the caller's responsibility to free *OLOAD_CHAMP_BV. */
2129 find_oload_champ (struct type **arg_types, int nargs, int method,
2130 int num_fns, struct fn_field *fns_ptr,
2131 struct symbol **oload_syms,
2132 struct badness_vector **oload_champ_bv)
2135 struct badness_vector *bv; /* A measure of how good an overloaded instance is */
2136 int oload_champ = -1; /* Index of best overloaded function */
2137 int oload_ambiguous = 0; /* Current ambiguity state for overload resolution */
2138 /* 0 => no ambiguity, 1 => two good funcs, 2 => incomparable funcs */
2140 *oload_champ_bv = NULL;
2142 /* Consider each candidate in turn */
2143 for (ix = 0; ix < num_fns; ix++)
2146 int static_offset = oload_method_static (method, fns_ptr, ix);
2148 struct type **parm_types;
2152 nparms = TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (fns_ptr, ix));
2156 /* If it's not a method, this is the proper place */
2157 nparms=TYPE_NFIELDS(SYMBOL_TYPE(oload_syms[ix]));
2160 /* Prepare array of parameter types */
2161 parm_types = (struct type **) xmalloc (nparms * (sizeof (struct type *)));
2162 for (jj = 0; jj < nparms; jj++)
2163 parm_types[jj] = (method
2164 ? (TYPE_FN_FIELD_ARGS (fns_ptr, ix)[jj].type)
2165 : TYPE_FIELD_TYPE (SYMBOL_TYPE (oload_syms[ix]), jj));
2167 /* Compare parameter types to supplied argument types. Skip THIS for
2169 bv = rank_function (parm_types, nparms, arg_types + static_offset,
2170 nargs - static_offset);
2172 if (!*oload_champ_bv)
2174 *oload_champ_bv = bv;
2178 /* See whether current candidate is better or worse than previous best */
2179 switch (compare_badness (bv, *oload_champ_bv))
2182 oload_ambiguous = 1; /* top two contenders are equally good */
2185 oload_ambiguous = 2; /* incomparable top contenders */
2188 *oload_champ_bv = bv; /* new champion, record details */
2189 oload_ambiguous = 0;
2200 fprintf_filtered (gdb_stderr,"Overloaded method instance %s, # of parms %d\n", fns_ptr[ix].physname, nparms);
2202 fprintf_filtered (gdb_stderr,"Overloaded function instance %s # of parms %d\n", SYMBOL_DEMANGLED_NAME (oload_syms[ix]), nparms);
2203 for (jj = 0; jj < nargs - static_offset; jj++)
2204 fprintf_filtered (gdb_stderr,"...Badness @ %d : %d\n", jj, bv->rank[jj]);
2205 fprintf_filtered (gdb_stderr,"Overload resolution champion is %d, ambiguous? %d\n", oload_champ, oload_ambiguous);
2212 /* Return 1 if we're looking at a static method, 0 if we're looking at
2213 a non-static method or a function that isn't a method. */
2216 oload_method_static (int method, struct fn_field *fns_ptr, int index)
2218 if (method && TYPE_FN_FIELD_STATIC_P (fns_ptr, index))
2224 /* Check how good an overload match OLOAD_CHAMP_BV represents. */
2226 static enum oload_classification
2227 classify_oload_match (struct badness_vector *oload_champ_bv,
2233 for (ix = 1; ix <= nargs - static_offset; ix++)
2235 if (oload_champ_bv->rank[ix] >= 100)
2236 return INCOMPATIBLE; /* truly mismatched types */
2237 else if (oload_champ_bv->rank[ix] >= 10)
2238 return NON_STANDARD; /* non-standard type conversions needed */
2241 return STANDARD; /* Only standard conversions needed. */
2244 /* C++: return 1 is NAME is a legitimate name for the destructor
2245 of type TYPE. If TYPE does not have a destructor, or
2246 if NAME is inappropriate for TYPE, an error is signaled. */
2248 destructor_name_p (const char *name, const struct type *type)
2250 /* destructors are a special case. */
2254 char *dname = type_name_no_tag (type);
2255 char *cp = strchr (dname, '<');
2258 /* Do not compare the template part for template classes. */
2260 len = strlen (dname);
2263 if (strlen (name + 1) != len || strncmp (dname, name + 1, len) != 0)
2264 error ("name of destructor must equal name of class");
2271 /* Helper function for check_field: Given TYPE, a structure/union,
2272 return 1 if the component named NAME from the ultimate
2273 target structure/union is defined, otherwise, return 0. */
2276 check_field_in (struct type *type, const char *name)
2280 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
2282 char *t_field_name = TYPE_FIELD_NAME (type, i);
2283 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
2287 /* C++: If it was not found as a data field, then try to
2288 return it as a pointer to a method. */
2290 /* Destructors are a special case. */
2291 if (destructor_name_p (name, type))
2293 int m_index, f_index;
2295 return get_destructor_fn_field (type, &m_index, &f_index);
2298 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
2300 if (strcmp_iw (TYPE_FN_FIELDLIST_NAME (type, i), name) == 0)
2304 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
2305 if (check_field_in (TYPE_BASECLASS (type, i), name))
2312 /* C++: Given ARG1, a value of type (pointer to a)* structure/union,
2313 return 1 if the component named NAME from the ultimate
2314 target structure/union is defined, otherwise, return 0. */
2317 check_field (struct value *arg1, const char *name)
2321 arg1 = coerce_array (arg1);
2323 t = value_type (arg1);
2325 /* Follow pointers until we get to a non-pointer. */
2330 if (TYPE_CODE (t) != TYPE_CODE_PTR && TYPE_CODE (t) != TYPE_CODE_REF)
2332 t = TYPE_TARGET_TYPE (t);
2335 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
2336 error ("not implemented: member type in check_field");
2338 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
2339 && TYPE_CODE (t) != TYPE_CODE_UNION)
2340 error ("Internal error: `this' is not an aggregate");
2342 return check_field_in (t, name);
2345 /* C++: Given an aggregate type CURTYPE, and a member name NAME,
2346 return the appropriate member. This function is used to resolve
2347 user expressions of the form "DOMAIN::NAME". For more details on
2348 what happens, see the comment before
2349 value_struct_elt_for_reference. */
2352 value_aggregate_elt (struct type *curtype,
2356 switch (TYPE_CODE (curtype))
2358 case TYPE_CODE_STRUCT:
2359 case TYPE_CODE_UNION:
2360 return value_struct_elt_for_reference (curtype, 0, curtype, name, NULL,
2362 case TYPE_CODE_NAMESPACE:
2363 return value_namespace_elt (curtype, name, noside);
2365 internal_error (__FILE__, __LINE__,
2366 "non-aggregate type in value_aggregate_elt");
2370 /* C++: Given an aggregate type CURTYPE, and a member name NAME,
2371 return the address of this member as a "pointer to member"
2372 type. If INTYPE is non-null, then it will be the type
2373 of the member we are looking for. This will help us resolve
2374 "pointers to member functions". This function is used
2375 to resolve user expressions of the form "DOMAIN::NAME". */
2377 static struct value *
2378 value_struct_elt_for_reference (struct type *domain, int offset,
2379 struct type *curtype, char *name,
2380 struct type *intype,
2383 struct type *t = curtype;
2387 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
2388 && TYPE_CODE (t) != TYPE_CODE_UNION)
2389 error ("Internal error: non-aggregate type to value_struct_elt_for_reference");
2391 for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
2393 char *t_field_name = TYPE_FIELD_NAME (t, i);
2395 if (t_field_name && strcmp (t_field_name, name) == 0)
2397 if (TYPE_FIELD_STATIC (t, i))
2399 v = value_static_field (t, i);
2401 error ("static field %s has been optimized out",
2405 if (TYPE_FIELD_PACKED (t, i))
2406 error ("pointers to bitfield members not allowed");
2408 return value_from_longest
2409 (lookup_reference_type (lookup_member_type (TYPE_FIELD_TYPE (t, i),
2411 offset + (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3));
2415 /* C++: If it was not found as a data field, then try to
2416 return it as a pointer to a method. */
2418 /* Destructors are a special case. */
2419 if (destructor_name_p (name, t))
2421 error ("member pointers to destructors not implemented yet");
2424 /* Perform all necessary dereferencing. */
2425 while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR)
2426 intype = TYPE_TARGET_TYPE (intype);
2428 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
2430 char *t_field_name = TYPE_FN_FIELDLIST_NAME (t, i);
2431 char dem_opname[64];
2433 if (strncmp (t_field_name, "__", 2) == 0 ||
2434 strncmp (t_field_name, "op", 2) == 0 ||
2435 strncmp (t_field_name, "type", 4) == 0)
2437 if (cplus_demangle_opname (t_field_name, dem_opname, DMGL_ANSI))
2438 t_field_name = dem_opname;
2439 else if (cplus_demangle_opname (t_field_name, dem_opname, 0))
2440 t_field_name = dem_opname;
2442 if (t_field_name && strcmp (t_field_name, name) == 0)
2444 int j = TYPE_FN_FIELDLIST_LENGTH (t, i);
2445 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
2447 check_stub_method_group (t, i);
2449 if (intype == 0 && j > 1)
2450 error ("non-unique member `%s' requires type instantiation", name);
2454 if (TYPE_FN_FIELD_TYPE (f, j) == intype)
2457 error ("no member function matches that type instantiation");
2462 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
2464 return value_from_longest
2465 (lookup_reference_type
2466 (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
2468 (LONGEST) METHOD_PTR_FROM_VOFFSET (TYPE_FN_FIELD_VOFFSET (f, j)));
2472 struct symbol *s = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
2473 0, VAR_DOMAIN, 0, NULL);
2480 v = read_var_value (s, 0);
2482 VALUE_TYPE (v) = lookup_reference_type
2483 (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
2491 for (i = TYPE_N_BASECLASSES (t) - 1; i >= 0; i--)
2496 if (BASETYPE_VIA_VIRTUAL (t, i))
2499 base_offset = TYPE_BASECLASS_BITPOS (t, i) / 8;
2500 v = value_struct_elt_for_reference (domain,
2501 offset + base_offset,
2502 TYPE_BASECLASS (t, i),
2510 /* As a last chance, pretend that CURTYPE is a namespace, and look
2511 it up that way; this (frequently) works for types nested inside
2514 return value_maybe_namespace_elt (curtype, name, noside);
2517 /* C++: Return the member NAME of the namespace given by the type
2520 static struct value *
2521 value_namespace_elt (const struct type *curtype,
2525 struct value *retval = value_maybe_namespace_elt (curtype, name,
2529 error ("No symbol \"%s\" in namespace \"%s\".", name,
2530 TYPE_TAG_NAME (curtype));
2535 /* A helper function used by value_namespace_elt and
2536 value_struct_elt_for_reference. It looks up NAME inside the
2537 context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
2538 is a class and NAME refers to a type in CURTYPE itself (as opposed
2539 to, say, some base class of CURTYPE). */
2541 static struct value *
2542 value_maybe_namespace_elt (const struct type *curtype,
2546 const char *namespace_name = TYPE_TAG_NAME (curtype);
2549 sym = cp_lookup_symbol_namespace (namespace_name, name, NULL,
2550 get_selected_block (0), VAR_DOMAIN,
2555 else if ((noside == EVAL_AVOID_SIDE_EFFECTS)
2556 && (SYMBOL_CLASS (sym) == LOC_TYPEDEF))
2557 return allocate_value (SYMBOL_TYPE (sym));
2559 return value_of_variable (sym, get_selected_block (0));
2562 /* Given a pointer value V, find the real (RTTI) type
2563 of the object it points to.
2564 Other parameters FULL, TOP, USING_ENC as with value_rtti_type()
2565 and refer to the values computed for the object pointed to. */
2568 value_rtti_target_type (struct value *v, int *full, int *top, int *using_enc)
2570 struct value *target;
2572 target = value_ind (v);
2574 return value_rtti_type (target, full, top, using_enc);
2577 /* Given a value pointed to by ARGP, check its real run-time type, and
2578 if that is different from the enclosing type, create a new value
2579 using the real run-time type as the enclosing type (and of the same
2580 type as ARGP) and return it, with the embedded offset adjusted to
2581 be the correct offset to the enclosed object
2582 RTYPE is the type, and XFULL, XTOP, and XUSING_ENC are the other
2583 parameters, computed by value_rtti_type(). If these are available,
2584 they can be supplied and a second call to value_rtti_type() is avoided.
2585 (Pass RTYPE == NULL if they're not available */
2588 value_full_object (struct value *argp, struct type *rtype, int xfull, int xtop,
2591 struct type *real_type;
2595 struct value *new_val;
2602 using_enc = xusing_enc;
2605 real_type = value_rtti_type (argp, &full, &top, &using_enc);
2607 /* If no RTTI data, or if object is already complete, do nothing */
2608 if (!real_type || real_type == VALUE_ENCLOSING_TYPE (argp))
2611 /* If we have the full object, but for some reason the enclosing
2612 type is wrong, set it *//* pai: FIXME -- sounds iffy */
2615 argp = value_change_enclosing_type (argp, real_type);
2619 /* Check if object is in memory */
2620 if (VALUE_LVAL (argp) != lval_memory)
2622 warning ("Couldn't retrieve complete object of RTTI type %s; object may be in register(s).", TYPE_NAME (real_type));
2627 /* All other cases -- retrieve the complete object */
2628 /* Go back by the computed top_offset from the beginning of the object,
2629 adjusting for the embedded offset of argp if that's what value_rtti_type
2630 used for its computation. */
2631 new_val = value_at_lazy (real_type, VALUE_ADDRESS (argp) - top +
2632 (using_enc ? 0 : VALUE_EMBEDDED_OFFSET (argp)));
2633 new_val->type = value_type (argp);
2634 VALUE_EMBEDDED_OFFSET (new_val) = using_enc ? top + VALUE_EMBEDDED_OFFSET (argp) : top;
2641 /* Return the value of the local variable, if one exists.
2642 Flag COMPLAIN signals an error if the request is made in an
2643 inappropriate context. */
2646 value_of_local (const char *name, int complain)
2648 struct symbol *func, *sym;
2652 if (deprecated_selected_frame == 0)
2655 error ("no frame selected");
2660 func = get_frame_function (deprecated_selected_frame);
2664 error ("no `%s' in nameless context", name);
2669 b = SYMBOL_BLOCK_VALUE (func);
2670 if (dict_empty (BLOCK_DICT (b)))
2673 error ("no args, no `%s'", name);
2678 /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
2679 symbol instead of the LOC_ARG one (if both exist). */
2680 sym = lookup_block_symbol (b, name, NULL, VAR_DOMAIN);
2684 error ("current stack frame does not contain a variable named `%s'", name);
2689 ret = read_var_value (sym, deprecated_selected_frame);
2690 if (ret == 0 && complain)
2691 error ("`%s' argument unreadable", name);
2695 /* C++/Objective-C: return the value of the class instance variable,
2696 if one exists. Flag COMPLAIN signals an error if the request is
2697 made in an inappropriate context. */
2700 value_of_this (int complain)
2702 if (current_language->la_language == language_objc)
2703 return value_of_local ("self", complain);
2705 return value_of_local ("this", complain);
2708 /* Create a slice (sub-string, sub-array) of ARRAY, that is LENGTH elements
2709 long, starting at LOWBOUND. The result has the same lower bound as
2710 the original ARRAY. */
2713 value_slice (struct value *array, int lowbound, int length)
2715 struct type *slice_range_type, *slice_type, *range_type;
2716 LONGEST lowerbound, upperbound;
2717 struct value *slice;
2718 struct type *array_type;
2719 array_type = check_typedef (value_type (array));
2720 if (TYPE_CODE (array_type) != TYPE_CODE_ARRAY
2721 && TYPE_CODE (array_type) != TYPE_CODE_STRING
2722 && TYPE_CODE (array_type) != TYPE_CODE_BITSTRING)
2723 error ("cannot take slice of non-array");
2724 range_type = TYPE_INDEX_TYPE (array_type);
2725 if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0)
2726 error ("slice from bad array or bitstring");
2727 if (lowbound < lowerbound || length < 0
2728 || lowbound + length - 1 > upperbound)
2729 error ("slice out of range");
2730 /* FIXME-type-allocation: need a way to free this type when we are
2732 slice_range_type = create_range_type ((struct type *) NULL,
2733 TYPE_TARGET_TYPE (range_type),
2734 lowbound, lowbound + length - 1);
2735 if (TYPE_CODE (array_type) == TYPE_CODE_BITSTRING)
2738 slice_type = create_set_type ((struct type *) NULL, slice_range_type);
2739 TYPE_CODE (slice_type) = TYPE_CODE_BITSTRING;
2740 slice = value_zero (slice_type, not_lval);
2741 for (i = 0; i < length; i++)
2743 int element = value_bit_index (array_type,
2744 VALUE_CONTENTS (array),
2747 error ("internal error accessing bitstring");
2748 else if (element > 0)
2750 int j = i % TARGET_CHAR_BIT;
2751 if (BITS_BIG_ENDIAN)
2752 j = TARGET_CHAR_BIT - 1 - j;
2753 VALUE_CONTENTS_RAW (slice)[i / TARGET_CHAR_BIT] |= (1 << j);
2756 /* We should set the address, bitssize, and bitspos, so the clice
2757 can be used on the LHS, but that may require extensions to
2758 value_assign. For now, just leave as a non_lval. FIXME. */
2762 struct type *element_type = TYPE_TARGET_TYPE (array_type);
2764 = (lowbound - lowerbound) * TYPE_LENGTH (check_typedef (element_type));
2765 slice_type = create_array_type ((struct type *) NULL, element_type,
2767 TYPE_CODE (slice_type) = TYPE_CODE (array_type);
2768 slice = allocate_value (slice_type);
2769 if (VALUE_LAZY (array))
2770 VALUE_LAZY (slice) = 1;
2772 memcpy (VALUE_CONTENTS (slice), VALUE_CONTENTS (array) + offset,
2773 TYPE_LENGTH (slice_type));
2774 if (VALUE_LVAL (array) == lval_internalvar)
2775 VALUE_LVAL (slice) = lval_internalvar_component;
2777 VALUE_LVAL (slice) = VALUE_LVAL (array);
2778 VALUE_ADDRESS (slice) = VALUE_ADDRESS (array);
2779 slice->offset = value_offset (array) + offset;
2784 /* Create a value for a FORTRAN complex number. Currently most of
2785 the time values are coerced to COMPLEX*16 (i.e. a complex number
2786 composed of 2 doubles. This really should be a smarter routine
2787 that figures out precision inteligently as opposed to assuming
2788 doubles. FIXME: fmb */
2791 value_literal_complex (struct value *arg1, struct value *arg2, struct type *type)
2794 struct type *real_type = TYPE_TARGET_TYPE (type);
2796 val = allocate_value (type);
2797 arg1 = value_cast (real_type, arg1);
2798 arg2 = value_cast (real_type, arg2);
2800 memcpy (VALUE_CONTENTS_RAW (val),
2801 VALUE_CONTENTS (arg1), TYPE_LENGTH (real_type));
2802 memcpy (VALUE_CONTENTS_RAW (val) + TYPE_LENGTH (real_type),
2803 VALUE_CONTENTS (arg2), TYPE_LENGTH (real_type));
2807 /* Cast a value into the appropriate complex data type. */
2809 static struct value *
2810 cast_into_complex (struct type *type, struct value *val)
2812 struct type *real_type = TYPE_TARGET_TYPE (type);
2813 if (TYPE_CODE (value_type (val)) == TYPE_CODE_COMPLEX)
2815 struct type *val_real_type = TYPE_TARGET_TYPE (value_type (val));
2816 struct value *re_val = allocate_value (val_real_type);
2817 struct value *im_val = allocate_value (val_real_type);
2819 memcpy (VALUE_CONTENTS_RAW (re_val),
2820 VALUE_CONTENTS (val), TYPE_LENGTH (val_real_type));
2821 memcpy (VALUE_CONTENTS_RAW (im_val),
2822 VALUE_CONTENTS (val) + TYPE_LENGTH (val_real_type),
2823 TYPE_LENGTH (val_real_type));
2825 return value_literal_complex (re_val, im_val, type);
2827 else if (TYPE_CODE (value_type (val)) == TYPE_CODE_FLT
2828 || TYPE_CODE (value_type (val)) == TYPE_CODE_INT)
2829 return value_literal_complex (val, value_zero (real_type, not_lval), type);
2831 error ("cannot cast non-number to complex");
2835 _initialize_valops (void)
2838 deprecated_add_show_from_set
2839 (add_set_cmd ("abandon", class_support, var_boolean, (char *) &auto_abandon,
2840 "Set automatic abandonment of expressions upon failure.",
2845 deprecated_add_show_from_set
2846 (add_set_cmd ("overload-resolution", class_support, var_boolean, (char *) &overload_resolution,
2847 "Set overload resolution in evaluating C++ functions.",
2850 overload_resolution = 1;