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 CORE_ADDR value_push (CORE_ADDR, struct value *);
55 static struct value *search_struct_field (char *, struct value *, int,
58 static struct value *search_struct_method (char *, struct value **,
60 int, int *, struct type *);
62 static int find_oload_champ_namespace (struct type **arg_types, int nargs,
63 const char *func_name,
64 const char *qualified_name,
65 struct symbol ***oload_syms,
66 struct badness_vector **oload_champ_bv);
69 int find_oload_champ_namespace_loop (struct type **arg_types, int nargs,
70 const char *func_name,
71 const char *qualified_name,
73 struct symbol ***oload_syms,
74 struct badness_vector **oload_champ_bv,
77 static int find_oload_champ (struct type **arg_types, int nargs, int method,
79 struct fn_field *fns_ptr,
80 struct symbol **oload_syms,
81 struct badness_vector **oload_champ_bv);
83 static int oload_method_static (int method, struct fn_field *fns_ptr,
86 enum oload_classification { STANDARD, NON_STANDARD, INCOMPATIBLE };
89 oload_classification classify_oload_match (struct badness_vector
94 static int check_field_in (struct type *, const char *);
96 static struct value *value_struct_elt_for_reference (struct type *domain,
103 static struct value *value_namespace_elt (const struct type *curtype,
107 static struct value *value_maybe_namespace_elt (const struct type *curtype,
111 static CORE_ADDR allocate_space_in_inferior (int);
113 static struct value *cast_into_complex (struct type *, struct value *);
115 static struct fn_field *find_method_list (struct value ** argp, char *method,
117 struct type *type, int *num_fns,
118 struct type **basetype,
121 void _initialize_valops (void);
123 /* Flag for whether we want to abandon failed expression evals by default. */
126 static int auto_abandon = 0;
129 int overload_resolution = 0;
131 /* Find the address of function name NAME in the inferior. */
134 find_function_in_inferior (const char *name)
137 sym = lookup_symbol (name, 0, VAR_DOMAIN, 0, NULL);
140 if (SYMBOL_CLASS (sym) != LOC_BLOCK)
142 error ("\"%s\" exists in this program but is not a function.",
145 return value_of_variable (sym, NULL);
149 struct minimal_symbol *msymbol = lookup_minimal_symbol (name, NULL, NULL);
154 type = lookup_pointer_type (builtin_type_char);
155 type = lookup_function_type (type);
156 type = lookup_pointer_type (type);
157 maddr = SYMBOL_VALUE_ADDRESS (msymbol);
158 return value_from_pointer (type, maddr);
162 if (!target_has_execution)
163 error ("evaluation of this expression requires the target program to be active");
165 error ("evaluation of this expression requires the program to have a function \"%s\".", name);
170 /* Allocate NBYTES of space in the inferior using the inferior's malloc
171 and return a value that is a pointer to the allocated space. */
174 value_allocate_space_in_inferior (int len)
176 struct value *blocklen;
177 struct value *val = find_function_in_inferior (NAME_OF_MALLOC);
179 blocklen = value_from_longest (builtin_type_int, (LONGEST) len);
180 val = call_function_by_hand (val, 1, &blocklen);
181 if (value_logical_not (val))
183 if (!target_has_execution)
184 error ("No memory available to program now: you need to start the target first");
186 error ("No memory available to program: call to malloc failed");
192 allocate_space_in_inferior (int len)
194 return value_as_long (value_allocate_space_in_inferior (len));
197 /* Cast value ARG2 to type TYPE and return as a value.
198 More general than a C cast: accepts any two types of the same length,
199 and if ARG2 is an lvalue it can be cast into anything at all. */
200 /* In C++, casts may change pointer or object representations. */
203 value_cast (struct type *type, struct value *arg2)
205 enum type_code code1;
206 enum type_code code2;
210 int convert_to_boolean = 0;
212 if (VALUE_TYPE (arg2) == type)
215 CHECK_TYPEDEF (type);
216 code1 = TYPE_CODE (type);
218 type2 = check_typedef (VALUE_TYPE (arg2));
220 /* A cast to an undetermined-length array_type, such as (TYPE [])OBJECT,
221 is treated like a cast to (TYPE [N])OBJECT,
222 where N is sizeof(OBJECT)/sizeof(TYPE). */
223 if (code1 == TYPE_CODE_ARRAY)
225 struct type *element_type = TYPE_TARGET_TYPE (type);
226 unsigned element_length = TYPE_LENGTH (check_typedef (element_type));
227 if (element_length > 0
228 && TYPE_ARRAY_UPPER_BOUND_TYPE (type) == BOUND_CANNOT_BE_DETERMINED)
230 struct type *range_type = TYPE_INDEX_TYPE (type);
231 int val_length = TYPE_LENGTH (type2);
232 LONGEST low_bound, high_bound, new_length;
233 if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
234 low_bound = 0, high_bound = 0;
235 new_length = val_length / element_length;
236 if (val_length % element_length != 0)
237 warning ("array element type size does not divide object size in cast");
238 /* FIXME-type-allocation: need a way to free this type when we are
240 range_type = create_range_type ((struct type *) NULL,
241 TYPE_TARGET_TYPE (range_type),
243 new_length + low_bound - 1);
244 VALUE_TYPE (arg2) = create_array_type ((struct type *) NULL,
245 element_type, range_type);
250 if (current_language->c_style_arrays
251 && TYPE_CODE (type2) == TYPE_CODE_ARRAY)
252 arg2 = value_coerce_array (arg2);
254 if (TYPE_CODE (type2) == TYPE_CODE_FUNC)
255 arg2 = value_coerce_function (arg2);
257 type2 = check_typedef (VALUE_TYPE (arg2));
258 COERCE_VARYING_ARRAY (arg2, type2);
259 code2 = TYPE_CODE (type2);
261 if (code1 == TYPE_CODE_COMPLEX)
262 return cast_into_complex (type, arg2);
263 if (code1 == TYPE_CODE_BOOL)
265 code1 = TYPE_CODE_INT;
266 convert_to_boolean = 1;
268 if (code1 == TYPE_CODE_CHAR)
269 code1 = TYPE_CODE_INT;
270 if (code2 == TYPE_CODE_BOOL || code2 == TYPE_CODE_CHAR)
271 code2 = TYPE_CODE_INT;
273 scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
274 || code2 == TYPE_CODE_ENUM || code2 == TYPE_CODE_RANGE);
276 if (code1 == TYPE_CODE_STRUCT
277 && code2 == TYPE_CODE_STRUCT
278 && TYPE_NAME (type) != 0)
280 /* Look in the type of the source to see if it contains the
281 type of the target as a superclass. If so, we'll need to
282 offset the object in addition to changing its type. */
283 struct value *v = search_struct_field (type_name_no_tag (type),
287 VALUE_TYPE (v) = type;
291 if (code1 == TYPE_CODE_FLT && scalar)
292 return value_from_double (type, value_as_double (arg2));
293 else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM
294 || code1 == TYPE_CODE_RANGE)
295 && (scalar || code2 == TYPE_CODE_PTR))
299 if (deprecated_hp_som_som_object_present /* if target compiled by HP aCC */
300 && (code2 == TYPE_CODE_PTR))
303 struct value *retvalp;
305 switch (TYPE_CODE (TYPE_TARGET_TYPE (type2)))
307 /* With HP aCC, pointers to data members have a bias */
308 case TYPE_CODE_MEMBER:
309 retvalp = value_from_longest (type, value_as_long (arg2));
310 /* force evaluation */
311 ptr = (unsigned int *) VALUE_CONTENTS (retvalp);
312 *ptr &= ~0x20000000; /* zap 29th bit to remove bias */
315 /* While pointers to methods don't really point to a function */
316 case TYPE_CODE_METHOD:
317 error ("Pointers to methods not supported with HP aCC");
320 break; /* fall out and go to normal handling */
324 /* When we cast pointers to integers, we mustn't use
325 POINTER_TO_ADDRESS to find the address the pointer
326 represents, as value_as_long would. GDB should evaluate
327 expressions just as the compiler would --- and the compiler
328 sees a cast as a simple reinterpretation of the pointer's
330 if (code2 == TYPE_CODE_PTR)
331 longest = extract_unsigned_integer (VALUE_CONTENTS (arg2),
332 TYPE_LENGTH (type2));
334 longest = value_as_long (arg2);
335 return value_from_longest (type, convert_to_boolean ?
336 (LONGEST) (longest ? 1 : 0) : longest);
338 else if (code1 == TYPE_CODE_PTR && (code2 == TYPE_CODE_INT ||
339 code2 == TYPE_CODE_ENUM ||
340 code2 == TYPE_CODE_RANGE))
342 /* TYPE_LENGTH (type) is the length of a pointer, but we really
343 want the length of an address! -- we are really dealing with
344 addresses (i.e., gdb representations) not pointers (i.e.,
345 target representations) here.
347 This allows things like "print *(int *)0x01000234" to work
348 without printing a misleading message -- which would
349 otherwise occur when dealing with a target having two byte
350 pointers and four byte addresses. */
352 int addr_bit = TARGET_ADDR_BIT;
354 LONGEST longest = value_as_long (arg2);
355 if (addr_bit < sizeof (LONGEST) * HOST_CHAR_BIT)
357 if (longest >= ((LONGEST) 1 << addr_bit)
358 || longest <= -((LONGEST) 1 << addr_bit))
359 warning ("value truncated");
361 return value_from_longest (type, longest);
363 else if (TYPE_LENGTH (type) == TYPE_LENGTH (type2))
365 if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
367 struct type *t1 = check_typedef (TYPE_TARGET_TYPE (type));
368 struct type *t2 = check_typedef (TYPE_TARGET_TYPE (type2));
369 if (TYPE_CODE (t1) == TYPE_CODE_STRUCT
370 && TYPE_CODE (t2) == TYPE_CODE_STRUCT
371 && !value_logical_not (arg2))
375 /* Look in the type of the source to see if it contains the
376 type of the target as a superclass. If so, we'll need to
377 offset the pointer rather than just change its type. */
378 if (TYPE_NAME (t1) != NULL)
380 v = search_struct_field (type_name_no_tag (t1),
381 value_ind (arg2), 0, t2, 1);
385 VALUE_TYPE (v) = type;
390 /* Look in the type of the target to see if it contains the
391 type of the source as a superclass. If so, we'll need to
392 offset the pointer rather than just change its type.
393 FIXME: This fails silently with virtual inheritance. */
394 if (TYPE_NAME (t2) != NULL)
396 v = search_struct_field (type_name_no_tag (t2),
397 value_zero (t1, not_lval), 0, t1, 1);
400 CORE_ADDR addr2 = value_as_address (arg2);
401 addr2 -= (VALUE_ADDRESS (v)
403 + VALUE_EMBEDDED_OFFSET (v));
404 return value_from_pointer (type, addr2);
408 /* No superclass found, just fall through to change ptr type. */
410 VALUE_TYPE (arg2) = type;
411 arg2 = value_change_enclosing_type (arg2, type);
412 VALUE_POINTED_TO_OFFSET (arg2) = 0; /* pai: chk_val */
415 else if (VALUE_LVAL (arg2) == lval_memory)
417 return value_at_lazy (type, VALUE_ADDRESS (arg2) + VALUE_OFFSET (arg2),
418 VALUE_BFD_SECTION (arg2));
420 else if (code1 == TYPE_CODE_VOID)
422 return value_zero (builtin_type_void, not_lval);
426 error ("Invalid cast.");
431 /* Create a value of type TYPE that is zero, and return it. */
434 value_zero (struct type *type, enum lval_type lv)
436 struct value *val = allocate_value (type);
438 memset (VALUE_CONTENTS (val), 0, TYPE_LENGTH (check_typedef (type)));
439 VALUE_LVAL (val) = lv;
444 /* Return a value with type TYPE located at ADDR.
446 Call value_at only if the data needs to be fetched immediately;
447 if we can be 'lazy' and defer the fetch, perhaps indefinately, call
448 value_at_lazy instead. value_at_lazy simply records the address of
449 the data and sets the lazy-evaluation-required flag. The lazy flag
450 is tested in the VALUE_CONTENTS macro, which is used if and when
451 the contents are actually required.
453 Note: value_at does *NOT* handle embedded offsets; perform such
454 adjustments before or after calling it. */
457 value_at (struct type *type, CORE_ADDR addr, asection *sect)
461 if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
462 error ("Attempt to dereference a generic pointer.");
464 val = allocate_value (type);
466 read_memory (addr, VALUE_CONTENTS_ALL_RAW (val), TYPE_LENGTH (type));
468 VALUE_LVAL (val) = lval_memory;
469 VALUE_ADDRESS (val) = addr;
470 VALUE_BFD_SECTION (val) = sect;
475 /* Return a lazy value with type TYPE located at ADDR (cf. value_at). */
478 value_at_lazy (struct type *type, CORE_ADDR addr, asection *sect)
482 if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
483 error ("Attempt to dereference a generic pointer.");
485 val = allocate_value (type);
487 VALUE_LVAL (val) = lval_memory;
488 VALUE_ADDRESS (val) = addr;
489 VALUE_LAZY (val) = 1;
490 VALUE_BFD_SECTION (val) = sect;
495 /* Called only from the VALUE_CONTENTS and VALUE_CONTENTS_ALL macros,
496 if the current data for a variable needs to be loaded into
497 VALUE_CONTENTS(VAL). Fetches the data from the user's process, and
498 clears the lazy flag to indicate that the data in the buffer is valid.
500 If the value is zero-length, we avoid calling read_memory, which would
501 abort. We mark the value as fetched anyway -- all 0 bytes of it.
503 This function returns a value because it is used in the VALUE_CONTENTS
504 macro as part of an expression, where a void would not work. The
508 value_fetch_lazy (struct value *val)
510 CORE_ADDR addr = VALUE_ADDRESS (val) + VALUE_OFFSET (val);
511 int length = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (val));
513 struct type *type = VALUE_TYPE (val);
515 read_memory (addr, VALUE_CONTENTS_ALL_RAW (val), length);
517 VALUE_LAZY (val) = 0;
522 /* Store the contents of FROMVAL into the location of TOVAL.
523 Return a new value with the location of TOVAL and contents of FROMVAL. */
526 value_assign (struct value *toval, struct value *fromval)
530 char raw_buffer[MAX_REGISTER_SIZE];
532 struct frame_id old_frame;
534 if (!toval->modifiable)
535 error ("Left operand of assignment is not a modifiable lvalue.");
539 type = VALUE_TYPE (toval);
540 if (VALUE_LVAL (toval) != lval_internalvar)
541 fromval = value_cast (type, fromval);
543 COERCE_ARRAY (fromval);
544 CHECK_TYPEDEF (type);
546 /* Since modifying a register can trash the frame chain, and modifying memory
547 can trash the frame cache, we save the old frame and then restore the new
549 old_frame = get_frame_id (deprecated_selected_frame);
551 switch (VALUE_LVAL (toval))
553 case lval_internalvar:
554 set_internalvar (VALUE_INTERNALVAR (toval), fromval);
555 val = value_copy (VALUE_INTERNALVAR (toval)->value);
556 val = value_change_enclosing_type (val, VALUE_ENCLOSING_TYPE (fromval));
557 VALUE_EMBEDDED_OFFSET (val) = VALUE_EMBEDDED_OFFSET (fromval);
558 VALUE_POINTED_TO_OFFSET (val) = VALUE_POINTED_TO_OFFSET (fromval);
561 case lval_internalvar_component:
562 set_internalvar_component (VALUE_INTERNALVAR (toval),
563 VALUE_OFFSET (toval),
564 VALUE_BITPOS (toval),
565 VALUE_BITSIZE (toval),
572 CORE_ADDR changed_addr;
575 if (VALUE_BITSIZE (toval))
577 char buffer[sizeof (LONGEST)];
578 /* We assume that the argument to read_memory is in units of
579 host chars. FIXME: Is that correct? */
580 changed_len = (VALUE_BITPOS (toval)
581 + VALUE_BITSIZE (toval)
585 if (changed_len > (int) sizeof (LONGEST))
586 error ("Can't handle bitfields which don't fit in a %d bit word.",
587 (int) sizeof (LONGEST) * HOST_CHAR_BIT);
589 read_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
590 buffer, changed_len);
591 modify_field (buffer, value_as_long (fromval),
592 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
593 changed_addr = VALUE_ADDRESS (toval) + VALUE_OFFSET (toval);
594 dest_buffer = buffer;
598 changed_addr = VALUE_ADDRESS (toval) + VALUE_OFFSET (toval);
599 changed_len = use_buffer;
600 dest_buffer = raw_buffer;
604 changed_addr = VALUE_ADDRESS (toval) + VALUE_OFFSET (toval);
605 changed_len = TYPE_LENGTH (type);
606 dest_buffer = VALUE_CONTENTS (fromval);
609 write_memory (changed_addr, dest_buffer, changed_len);
610 if (deprecated_memory_changed_hook)
611 deprecated_memory_changed_hook (changed_addr, changed_len);
615 case lval_reg_frame_relative:
618 struct frame_info *frame;
621 /* Figure out which frame this is in currently. */
622 if (VALUE_LVAL (toval) == lval_register)
624 frame = get_current_frame ();
625 value_reg = VALUE_REGNO (toval);
629 frame = frame_find_by_id (VALUE_FRAME_ID (toval));
630 value_reg = VALUE_FRAME_REGNUM (toval);
634 error ("Value being assigned to is no longer active.");
636 if (VALUE_LVAL (toval) == lval_reg_frame_relative
637 && CONVERT_REGISTER_P (VALUE_FRAME_REGNUM (toval), type))
639 /* If TOVAL is a special machine register requiring
640 conversion of program values to a special raw format. */
641 VALUE_TO_REGISTER (frame, VALUE_FRAME_REGNUM (toval),
642 type, VALUE_CONTENTS (fromval));
646 /* TOVAL is stored in a series of registers in the frame
647 specified by the structure. Copy that value out,
648 modify it, and copy it back in. */
656 /* Locate the first register that falls in the value that
657 needs to be transfered. Compute the offset of the
658 value in that register. */
661 for (reg_offset = value_reg, offset = 0;
662 offset + DEPRECATED_REGISTER_RAW_SIZE (reg_offset) <= VALUE_OFFSET (toval);
664 byte_offset = VALUE_OFFSET (toval) - offset;
667 /* Compute the number of register aligned values that need
669 if (VALUE_BITSIZE (toval))
670 amount_to_copy = byte_offset + 1;
672 amount_to_copy = byte_offset + TYPE_LENGTH (type);
674 /* And a bounce buffer. Be slightly over generous. */
675 buffer = (char *) alloca (amount_to_copy + MAX_REGISTER_SIZE);
678 for (regno = reg_offset, amount_copied = 0;
679 amount_copied < amount_to_copy;
680 amount_copied += DEPRECATED_REGISTER_RAW_SIZE (regno), regno++)
681 frame_register_read (frame, regno, buffer + amount_copied);
683 /* Modify what needs to be modified. */
684 if (VALUE_BITSIZE (toval))
685 modify_field (buffer + byte_offset,
686 value_as_long (fromval),
687 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
689 memcpy (buffer + VALUE_OFFSET (toval), raw_buffer, use_buffer);
691 memcpy (buffer + byte_offset, VALUE_CONTENTS (fromval),
695 for (regno = reg_offset, amount_copied = 0;
696 amount_copied < amount_to_copy;
697 amount_copied += DEPRECATED_REGISTER_RAW_SIZE (regno), regno++)
698 put_frame_register (frame, regno, buffer + amount_copied);
701 if (deprecated_register_changed_hook)
702 deprecated_register_changed_hook (-1);
703 observer_notify_target_changed (¤t_target);
708 error ("Left operand of assignment is not an lvalue.");
711 /* Assigning to the stack pointer, frame pointer, and other
712 (architecture and calling convention specific) registers may
713 cause the frame cache to be out of date. Assigning to memory
714 also can. We just do this on all assignments to registers or
715 memory, for simplicity's sake; I doubt the slowdown matters. */
716 switch (VALUE_LVAL (toval))
720 case lval_reg_frame_relative:
722 reinit_frame_cache ();
724 /* Having destoroyed the frame cache, restore the selected frame. */
726 /* FIXME: cagney/2002-11-02: There has to be a better way of
727 doing this. Instead of constantly saving/restoring the
728 frame. Why not create a get_selected_frame() function that,
729 having saved the selected frame's ID can automatically
730 re-find the previously selected frame automatically. */
733 struct frame_info *fi = frame_find_by_id (old_frame);
743 /* If the field does not entirely fill a LONGEST, then zero the sign bits.
744 If the field is signed, and is negative, then sign extend. */
745 if ((VALUE_BITSIZE (toval) > 0)
746 && (VALUE_BITSIZE (toval) < 8 * (int) sizeof (LONGEST)))
748 LONGEST fieldval = value_as_long (fromval);
749 LONGEST valmask = (((ULONGEST) 1) << VALUE_BITSIZE (toval)) - 1;
752 if (!TYPE_UNSIGNED (type) && (fieldval & (valmask ^ (valmask >> 1))))
753 fieldval |= ~valmask;
755 fromval = value_from_longest (type, fieldval);
758 val = value_copy (toval);
759 memcpy (VALUE_CONTENTS_RAW (val), VALUE_CONTENTS (fromval),
761 VALUE_TYPE (val) = type;
762 val = value_change_enclosing_type (val, VALUE_ENCLOSING_TYPE (fromval));
763 VALUE_EMBEDDED_OFFSET (val) = VALUE_EMBEDDED_OFFSET (fromval);
764 VALUE_POINTED_TO_OFFSET (val) = VALUE_POINTED_TO_OFFSET (fromval);
769 /* Extend a value VAL to COUNT repetitions of its type. */
772 value_repeat (struct value *arg1, int count)
776 if (VALUE_LVAL (arg1) != lval_memory)
777 error ("Only values in memory can be extended with '@'.");
779 error ("Invalid number %d of repetitions.", count);
781 val = allocate_repeat_value (VALUE_ENCLOSING_TYPE (arg1), count);
783 read_memory (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1),
784 VALUE_CONTENTS_ALL_RAW (val),
785 TYPE_LENGTH (VALUE_ENCLOSING_TYPE (val)));
786 VALUE_LVAL (val) = lval_memory;
787 VALUE_ADDRESS (val) = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1);
793 value_of_variable (struct symbol *var, struct block *b)
796 struct frame_info *frame = NULL;
799 frame = NULL; /* Use selected frame. */
800 else if (symbol_read_needs_frame (var))
802 frame = block_innermost_frame (b);
805 if (BLOCK_FUNCTION (b)
806 && SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)))
807 error ("No frame is currently executing in block %s.",
808 SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)));
810 error ("No frame is currently executing in specified block");
814 val = read_var_value (var, frame);
816 error ("Address of symbol \"%s\" is unknown.", SYMBOL_PRINT_NAME (var));
821 /* Given a value which is an array, return a value which is a pointer to its
822 first element, regardless of whether or not the array has a nonzero lower
825 FIXME: A previous comment here indicated that this routine should be
826 substracting the array's lower bound. It's not clear to me that this
827 is correct. Given an array subscripting operation, it would certainly
828 work to do the adjustment here, essentially computing:
830 (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0])
832 However I believe a more appropriate and logical place to account for
833 the lower bound is to do so in value_subscript, essentially computing:
835 (&array[0] + ((index - lowerbound) * sizeof array[0]))
837 As further evidence consider what would happen with operations other
838 than array subscripting, where the caller would get back a value that
839 had an address somewhere before the actual first element of the array,
840 and the information about the lower bound would be lost because of
841 the coercion to pointer type.
845 value_coerce_array (struct value *arg1)
847 struct type *type = check_typedef (VALUE_TYPE (arg1));
849 if (VALUE_LVAL (arg1) != lval_memory)
850 error ("Attempt to take address of value not located in memory.");
852 return value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
853 (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
856 /* Given a value which is a function, return a value which is a pointer
860 value_coerce_function (struct value *arg1)
862 struct value *retval;
864 if (VALUE_LVAL (arg1) != lval_memory)
865 error ("Attempt to take address of value not located in memory.");
867 retval = value_from_pointer (lookup_pointer_type (VALUE_TYPE (arg1)),
868 (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
869 VALUE_BFD_SECTION (retval) = VALUE_BFD_SECTION (arg1);
873 /* Return a pointer value for the object for which ARG1 is the contents. */
876 value_addr (struct value *arg1)
880 struct type *type = check_typedef (VALUE_TYPE (arg1));
881 if (TYPE_CODE (type) == TYPE_CODE_REF)
883 /* Copy the value, but change the type from (T&) to (T*).
884 We keep the same location information, which is efficient,
885 and allows &(&X) to get the location containing the reference. */
886 arg2 = value_copy (arg1);
887 VALUE_TYPE (arg2) = lookup_pointer_type (TYPE_TARGET_TYPE (type));
890 if (TYPE_CODE (type) == TYPE_CODE_FUNC)
891 return value_coerce_function (arg1);
893 if (VALUE_LVAL (arg1) != lval_memory)
894 error ("Attempt to take address of value not located in memory.");
896 /* Get target memory address */
897 arg2 = value_from_pointer (lookup_pointer_type (VALUE_TYPE (arg1)),
898 (VALUE_ADDRESS (arg1)
899 + VALUE_OFFSET (arg1)
900 + VALUE_EMBEDDED_OFFSET (arg1)));
902 /* This may be a pointer to a base subobject; so remember the
903 full derived object's type ... */
904 arg2 = value_change_enclosing_type (arg2, lookup_pointer_type (VALUE_ENCLOSING_TYPE (arg1)));
905 /* ... and also the relative position of the subobject in the full object */
906 VALUE_POINTED_TO_OFFSET (arg2) = VALUE_EMBEDDED_OFFSET (arg1);
907 VALUE_BFD_SECTION (arg2) = VALUE_BFD_SECTION (arg1);
911 /* Given a value of a pointer type, apply the C unary * operator to it. */
914 value_ind (struct value *arg1)
916 struct type *base_type;
921 base_type = check_typedef (VALUE_TYPE (arg1));
923 if (TYPE_CODE (base_type) == TYPE_CODE_MEMBER)
924 error ("not implemented: member types in value_ind");
926 /* Allow * on an integer so we can cast it to whatever we want.
927 This returns an int, which seems like the most C-like thing
928 to do. "long long" variables are rare enough that
929 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
930 if (TYPE_CODE (base_type) == TYPE_CODE_INT)
931 return value_at_lazy (builtin_type_int,
932 (CORE_ADDR) value_as_long (arg1),
933 VALUE_BFD_SECTION (arg1));
934 else if (TYPE_CODE (base_type) == TYPE_CODE_PTR)
936 struct type *enc_type;
937 /* We may be pointing to something embedded in a larger object */
938 /* Get the real type of the enclosing object */
939 enc_type = check_typedef (VALUE_ENCLOSING_TYPE (arg1));
940 enc_type = TYPE_TARGET_TYPE (enc_type);
941 /* Retrieve the enclosing object pointed to */
942 arg2 = value_at_lazy (enc_type,
943 value_as_address (arg1) - VALUE_POINTED_TO_OFFSET (arg1),
944 VALUE_BFD_SECTION (arg1));
946 VALUE_TYPE (arg2) = TYPE_TARGET_TYPE (base_type);
947 /* Add embedding info */
948 arg2 = value_change_enclosing_type (arg2, enc_type);
949 VALUE_EMBEDDED_OFFSET (arg2) = VALUE_POINTED_TO_OFFSET (arg1);
951 /* We may be pointing to an object of some derived type */
952 arg2 = value_full_object (arg2, NULL, 0, 0, 0);
956 error ("Attempt to take contents of a non-pointer value.");
957 return 0; /* For lint -- never reached */
960 /* Pushing small parts of stack frames. */
962 /* Push one word (the size of object that a register holds). */
965 push_word (CORE_ADDR sp, ULONGEST word)
967 int len = DEPRECATED_REGISTER_SIZE;
968 char buffer[MAX_REGISTER_SIZE];
970 store_unsigned_integer (buffer, len, word);
971 if (INNER_THAN (1, 2))
973 /* stack grows downward */
975 write_memory (sp, buffer, len);
979 /* stack grows upward */
980 write_memory (sp, buffer, len);
987 /* Push LEN bytes with data at BUFFER. */
990 push_bytes (CORE_ADDR sp, char *buffer, int len)
992 if (INNER_THAN (1, 2))
994 /* stack grows downward */
996 write_memory (sp, buffer, len);
1000 /* stack grows upward */
1001 write_memory (sp, buffer, len);
1008 /* Push onto the stack the specified value VALUE. Pad it correctly for
1009 it to be an argument to a function. */
1012 value_push (CORE_ADDR sp, struct value *arg)
1014 int len = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (arg));
1015 int container_len = len;
1018 /* Are we going to put it at the high or low end of the container? */
1019 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
1020 offset = container_len - len;
1024 if (INNER_THAN (1, 2))
1026 /* stack grows downward */
1027 sp -= container_len;
1028 write_memory (sp + offset, VALUE_CONTENTS_ALL (arg), len);
1032 /* stack grows upward */
1033 write_memory (sp + offset, VALUE_CONTENTS_ALL (arg), len);
1034 sp += container_len;
1041 legacy_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
1042 int struct_return, CORE_ADDR struct_addr)
1044 /* ASSERT ( !struct_return); */
1046 for (i = nargs - 1; i >= 0; i--)
1047 sp = value_push (sp, args[i]);
1051 /* Create a value for an array by allocating space in the inferior, copying
1052 the data into that space, and then setting up an array value.
1054 The array bounds are set from LOWBOUND and HIGHBOUND, and the array is
1055 populated from the values passed in ELEMVEC.
1057 The element type of the array is inherited from the type of the
1058 first element, and all elements must have the same size (though we
1059 don't currently enforce any restriction on their types). */
1062 value_array (int lowbound, int highbound, struct value **elemvec)
1066 unsigned int typelength;
1068 struct type *rangetype;
1069 struct type *arraytype;
1072 /* Validate that the bounds are reasonable and that each of the elements
1073 have the same size. */
1075 nelem = highbound - lowbound + 1;
1078 error ("bad array bounds (%d, %d)", lowbound, highbound);
1080 typelength = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (elemvec[0]));
1081 for (idx = 1; idx < nelem; idx++)
1083 if (TYPE_LENGTH (VALUE_ENCLOSING_TYPE (elemvec[idx])) != typelength)
1085 error ("array elements must all be the same size");
1089 rangetype = create_range_type ((struct type *) NULL, builtin_type_int,
1090 lowbound, highbound);
1091 arraytype = create_array_type ((struct type *) NULL,
1092 VALUE_ENCLOSING_TYPE (elemvec[0]), rangetype);
1094 if (!current_language->c_style_arrays)
1096 val = allocate_value (arraytype);
1097 for (idx = 0; idx < nelem; idx++)
1099 memcpy (VALUE_CONTENTS_ALL_RAW (val) + (idx * typelength),
1100 VALUE_CONTENTS_ALL (elemvec[idx]),
1103 VALUE_BFD_SECTION (val) = VALUE_BFD_SECTION (elemvec[0]);
1107 /* Allocate space to store the array in the inferior, and then initialize
1108 it by copying in each element. FIXME: Is it worth it to create a
1109 local buffer in which to collect each value and then write all the
1110 bytes in one operation? */
1112 addr = allocate_space_in_inferior (nelem * typelength);
1113 for (idx = 0; idx < nelem; idx++)
1115 write_memory (addr + (idx * typelength), VALUE_CONTENTS_ALL (elemvec[idx]),
1119 /* Create the array type and set up an array value to be evaluated lazily. */
1121 val = value_at_lazy (arraytype, addr, VALUE_BFD_SECTION (elemvec[0]));
1125 /* Create a value for a string constant by allocating space in the inferior,
1126 copying the data into that space, and returning the address with type
1127 TYPE_CODE_STRING. PTR points to the string constant data; LEN is number
1129 Note that string types are like array of char types with a lower bound of
1130 zero and an upper bound of LEN - 1. Also note that the string may contain
1131 embedded null bytes. */
1134 value_string (char *ptr, int len)
1137 int lowbound = current_language->string_lower_bound;
1138 struct type *rangetype = create_range_type ((struct type *) NULL,
1140 lowbound, len + lowbound - 1);
1141 struct type *stringtype
1142 = create_string_type ((struct type *) NULL, rangetype);
1145 if (current_language->c_style_arrays == 0)
1147 val = allocate_value (stringtype);
1148 memcpy (VALUE_CONTENTS_RAW (val), ptr, len);
1153 /* Allocate space to store the string in the inferior, and then
1154 copy LEN bytes from PTR in gdb to that address in the inferior. */
1156 addr = allocate_space_in_inferior (len);
1157 write_memory (addr, ptr, len);
1159 val = value_at_lazy (stringtype, addr, NULL);
1164 value_bitstring (char *ptr, int len)
1167 struct type *domain_type = create_range_type (NULL, builtin_type_int,
1169 struct type *type = create_set_type ((struct type *) NULL, domain_type);
1170 TYPE_CODE (type) = TYPE_CODE_BITSTRING;
1171 val = allocate_value (type);
1172 memcpy (VALUE_CONTENTS_RAW (val), ptr, TYPE_LENGTH (type));
1176 /* See if we can pass arguments in T2 to a function which takes arguments
1177 of types T1. T1 is a list of NARGS arguments, and T2 is a NULL-terminated
1178 vector. If some arguments need coercion of some sort, then the coerced
1179 values are written into T2. Return value is 0 if the arguments could be
1180 matched, or the position at which they differ if not.
1182 STATICP is nonzero if the T1 argument list came from a
1183 static member function. T2 will still include the ``this'' pointer,
1184 but it will be skipped.
1186 For non-static member functions, we ignore the first argument,
1187 which is the type of the instance variable. This is because we want
1188 to handle calls with objects from derived classes. This is not
1189 entirely correct: we should actually check to make sure that a
1190 requested operation is type secure, shouldn't we? FIXME. */
1193 typecmp (int staticp, int varargs, int nargs,
1194 struct field t1[], struct value *t2[])
1199 internal_error (__FILE__, __LINE__, "typecmp: no argument list");
1201 /* Skip ``this'' argument if applicable. T2 will always include THIS. */
1206 (i < nargs) && TYPE_CODE (t1[i].type) != TYPE_CODE_VOID;
1209 struct type *tt1, *tt2;
1214 tt1 = check_typedef (t1[i].type);
1215 tt2 = check_typedef (VALUE_TYPE (t2[i]));
1217 if (TYPE_CODE (tt1) == TYPE_CODE_REF
1218 /* We should be doing hairy argument matching, as below. */
1219 && (TYPE_CODE (check_typedef (TYPE_TARGET_TYPE (tt1))) == TYPE_CODE (tt2)))
1221 if (TYPE_CODE (tt2) == TYPE_CODE_ARRAY)
1222 t2[i] = value_coerce_array (t2[i]);
1224 t2[i] = value_addr (t2[i]);
1228 /* djb - 20000715 - Until the new type structure is in the
1229 place, and we can attempt things like implicit conversions,
1230 we need to do this so you can take something like a map<const
1231 char *>, and properly access map["hello"], because the
1232 argument to [] will be a reference to a pointer to a char,
1233 and the argument will be a pointer to a char. */
1234 while ( TYPE_CODE(tt1) == TYPE_CODE_REF ||
1235 TYPE_CODE (tt1) == TYPE_CODE_PTR)
1237 tt1 = check_typedef( TYPE_TARGET_TYPE(tt1) );
1239 while ( TYPE_CODE(tt2) == TYPE_CODE_ARRAY ||
1240 TYPE_CODE(tt2) == TYPE_CODE_PTR ||
1241 TYPE_CODE(tt2) == TYPE_CODE_REF)
1243 tt2 = check_typedef( TYPE_TARGET_TYPE(tt2) );
1245 if (TYPE_CODE (tt1) == TYPE_CODE (tt2))
1247 /* Array to pointer is a `trivial conversion' according to the ARM. */
1249 /* We should be doing much hairier argument matching (see section 13.2
1250 of the ARM), but as a quick kludge, just check for the same type
1252 if (TYPE_CODE (t1[i].type) != TYPE_CODE (VALUE_TYPE (t2[i])))
1255 if (varargs || t2[i] == NULL)
1260 /* Helper function used by value_struct_elt to recurse through baseclasses.
1261 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1262 and search in it assuming it has (class) type TYPE.
1263 If found, return value, else return NULL.
1265 If LOOKING_FOR_BASECLASS, then instead of looking for struct fields,
1266 look for a baseclass named NAME. */
1268 static struct value *
1269 search_struct_field (char *name, struct value *arg1, int offset,
1270 struct type *type, int looking_for_baseclass)
1273 int nbases = TYPE_N_BASECLASSES (type);
1275 CHECK_TYPEDEF (type);
1277 if (!looking_for_baseclass)
1278 for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
1280 char *t_field_name = TYPE_FIELD_NAME (type, i);
1282 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
1285 if (TYPE_FIELD_STATIC (type, i))
1287 v = value_static_field (type, i);
1289 error ("field %s is nonexistent or has been optimised out",
1294 v = value_primitive_field (arg1, offset, i, type);
1296 error ("there is no field named %s", name);
1302 && (t_field_name[0] == '\0'
1303 || (TYPE_CODE (type) == TYPE_CODE_UNION
1304 && (strcmp_iw (t_field_name, "else") == 0))))
1306 struct type *field_type = TYPE_FIELD_TYPE (type, i);
1307 if (TYPE_CODE (field_type) == TYPE_CODE_UNION
1308 || TYPE_CODE (field_type) == TYPE_CODE_STRUCT)
1310 /* Look for a match through the fields of an anonymous union,
1311 or anonymous struct. C++ provides anonymous unions.
1313 In the GNU Chill (now deleted from GDB)
1314 implementation of variant record types, each
1315 <alternative field> has an (anonymous) union type,
1316 each member of the union represents a <variant
1317 alternative>. Each <variant alternative> is
1318 represented as a struct, with a member for each
1322 int new_offset = offset;
1324 /* This is pretty gross. In G++, the offset in an
1325 anonymous union is relative to the beginning of the
1326 enclosing struct. In the GNU Chill (now deleted
1327 from GDB) implementation of variant records, the
1328 bitpos is zero in an anonymous union field, so we
1329 have to add the offset of the union here. */
1330 if (TYPE_CODE (field_type) == TYPE_CODE_STRUCT
1331 || (TYPE_NFIELDS (field_type) > 0
1332 && TYPE_FIELD_BITPOS (field_type, 0) == 0))
1333 new_offset += TYPE_FIELD_BITPOS (type, i) / 8;
1335 v = search_struct_field (name, arg1, new_offset, field_type,
1336 looking_for_baseclass);
1343 for (i = 0; i < nbases; i++)
1346 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
1347 /* If we are looking for baseclasses, this is what we get when we
1348 hit them. But it could happen that the base part's member name
1349 is not yet filled in. */
1350 int found_baseclass = (looking_for_baseclass
1351 && TYPE_BASECLASS_NAME (type, i) != NULL
1352 && (strcmp_iw (name, TYPE_BASECLASS_NAME (type, i)) == 0));
1354 if (BASETYPE_VIA_VIRTUAL (type, i))
1357 struct value *v2 = allocate_value (basetype);
1359 boffset = baseclass_offset (type, i,
1360 VALUE_CONTENTS (arg1) + offset,
1361 VALUE_ADDRESS (arg1)
1362 + VALUE_OFFSET (arg1) + offset);
1364 error ("virtual baseclass botch");
1366 /* The virtual base class pointer might have been clobbered by the
1367 user program. Make sure that it still points to a valid memory
1371 if (boffset < 0 || boffset >= TYPE_LENGTH (type))
1373 CORE_ADDR base_addr;
1375 base_addr = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1) + boffset;
1376 if (target_read_memory (base_addr, VALUE_CONTENTS_RAW (v2),
1377 TYPE_LENGTH (basetype)) != 0)
1378 error ("virtual baseclass botch");
1379 VALUE_LVAL (v2) = lval_memory;
1380 VALUE_ADDRESS (v2) = base_addr;
1384 VALUE_LVAL (v2) = VALUE_LVAL (arg1);
1385 VALUE_ADDRESS (v2) = VALUE_ADDRESS (arg1);
1386 VALUE_OFFSET (v2) = VALUE_OFFSET (arg1) + boffset;
1387 if (VALUE_LAZY (arg1))
1388 VALUE_LAZY (v2) = 1;
1390 memcpy (VALUE_CONTENTS_RAW (v2),
1391 VALUE_CONTENTS_RAW (arg1) + boffset,
1392 TYPE_LENGTH (basetype));
1395 if (found_baseclass)
1397 v = search_struct_field (name, v2, 0, TYPE_BASECLASS (type, i),
1398 looking_for_baseclass);
1400 else if (found_baseclass)
1401 v = value_primitive_field (arg1, offset, i, type);
1403 v = search_struct_field (name, arg1,
1404 offset + TYPE_BASECLASS_BITPOS (type, i) / 8,
1405 basetype, looking_for_baseclass);
1413 /* Return the offset (in bytes) of the virtual base of type BASETYPE
1414 * in an object pointed to by VALADDR (on the host), assumed to be of
1415 * type TYPE. OFFSET is number of bytes beyond start of ARG to start
1416 * looking (in case VALADDR is the contents of an enclosing object).
1418 * This routine recurses on the primary base of the derived class because
1419 * the virtual base entries of the primary base appear before the other
1420 * virtual base entries.
1422 * If the virtual base is not found, a negative integer is returned.
1423 * The magnitude of the negative integer is the number of entries in
1424 * the virtual table to skip over (entries corresponding to various
1425 * ancestral classes in the chain of primary bases).
1427 * Important: This assumes the HP / Taligent C++ runtime
1428 * conventions. Use baseclass_offset() instead to deal with g++
1432 find_rt_vbase_offset (struct type *type, struct type *basetype, char *valaddr,
1433 int offset, int *boffset_p, int *skip_p)
1435 int boffset; /* offset of virtual base */
1436 int index; /* displacement to use in virtual table */
1440 CORE_ADDR vtbl; /* the virtual table pointer */
1441 struct type *pbc; /* the primary base class */
1443 /* Look for the virtual base recursively in the primary base, first.
1444 * This is because the derived class object and its primary base
1445 * subobject share the primary virtual table. */
1448 pbc = TYPE_PRIMARY_BASE (type);
1451 find_rt_vbase_offset (pbc, basetype, valaddr, offset, &boffset, &skip);
1454 *boffset_p = boffset;
1463 /* Find the index of the virtual base according to HP/Taligent
1464 runtime spec. (Depth-first, left-to-right.) */
1465 index = virtual_base_index_skip_primaries (basetype, type);
1469 *skip_p = skip + virtual_base_list_length_skip_primaries (type);
1474 /* pai: FIXME -- 32x64 possible problem */
1475 /* First word (4 bytes) in object layout is the vtable pointer */
1476 vtbl = *(CORE_ADDR *) (valaddr + offset);
1478 /* Before the constructor is invoked, things are usually zero'd out. */
1480 error ("Couldn't find virtual table -- object may not be constructed yet.");
1483 /* Find virtual base's offset -- jump over entries for primary base
1484 * ancestors, then use the index computed above. But also adjust by
1485 * HP_ACC_VBASE_START for the vtable slots before the start of the
1486 * virtual base entries. Offset is negative -- virtual base entries
1487 * appear _before_ the address point of the virtual table. */
1489 /* pai: FIXME -- 32x64 problem, if word = 8 bytes, change multiplier
1492 /* epstein : FIXME -- added param for overlay section. May not be correct */
1493 vp = value_at (builtin_type_int, vtbl + 4 * (-skip - index - HP_ACC_VBASE_START), NULL);
1494 boffset = value_as_long (vp);
1496 *boffset_p = boffset;
1501 /* Helper function used by value_struct_elt to recurse through baseclasses.
1502 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1503 and search in it assuming it has (class) type TYPE.
1504 If found, return value, else if name matched and args not return (value)-1,
1505 else return NULL. */
1507 static struct value *
1508 search_struct_method (char *name, struct value **arg1p,
1509 struct value **args, int offset,
1510 int *static_memfuncp, struct type *type)
1514 int name_matched = 0;
1515 char dem_opname[64];
1517 CHECK_TYPEDEF (type);
1518 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
1520 char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
1521 /* FIXME! May need to check for ARM demangling here */
1522 if (strncmp (t_field_name, "__", 2) == 0 ||
1523 strncmp (t_field_name, "op", 2) == 0 ||
1524 strncmp (t_field_name, "type", 4) == 0)
1526 if (cplus_demangle_opname (t_field_name, dem_opname, DMGL_ANSI))
1527 t_field_name = dem_opname;
1528 else if (cplus_demangle_opname (t_field_name, dem_opname, 0))
1529 t_field_name = dem_opname;
1531 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
1533 int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1;
1534 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1537 check_stub_method_group (type, i);
1538 if (j > 0 && args == 0)
1539 error ("cannot resolve overloaded method `%s': no arguments supplied", name);
1540 else if (j == 0 && args == 0)
1542 v = value_fn_field (arg1p, f, j, type, offset);
1549 if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j),
1550 TYPE_VARARGS (TYPE_FN_FIELD_TYPE (f, j)),
1551 TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f, j)),
1552 TYPE_FN_FIELD_ARGS (f, j), args))
1554 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1555 return value_virtual_fn_field (arg1p, f, j, type, offset);
1556 if (TYPE_FN_FIELD_STATIC_P (f, j) && static_memfuncp)
1557 *static_memfuncp = 1;
1558 v = value_fn_field (arg1p, f, j, type, offset);
1567 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1571 if (BASETYPE_VIA_VIRTUAL (type, i))
1573 if (TYPE_HAS_VTABLE (type))
1575 /* HP aCC compiled type, search for virtual base offset
1576 according to HP/Taligent runtime spec. */
1578 find_rt_vbase_offset (type, TYPE_BASECLASS (type, i),
1579 VALUE_CONTENTS_ALL (*arg1p),
1580 offset + VALUE_EMBEDDED_OFFSET (*arg1p),
1581 &base_offset, &skip);
1583 error ("Virtual base class offset not found in vtable");
1587 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
1590 /* The virtual base class pointer might have been clobbered by the
1591 user program. Make sure that it still points to a valid memory
1594 if (offset < 0 || offset >= TYPE_LENGTH (type))
1596 base_valaddr = (char *) alloca (TYPE_LENGTH (baseclass));
1597 if (target_read_memory (VALUE_ADDRESS (*arg1p)
1598 + VALUE_OFFSET (*arg1p) + offset,
1600 TYPE_LENGTH (baseclass)) != 0)
1601 error ("virtual baseclass botch");
1604 base_valaddr = VALUE_CONTENTS (*arg1p) + offset;
1607 baseclass_offset (type, i, base_valaddr,
1608 VALUE_ADDRESS (*arg1p)
1609 + VALUE_OFFSET (*arg1p) + offset);
1610 if (base_offset == -1)
1611 error ("virtual baseclass botch");
1616 base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
1618 v = search_struct_method (name, arg1p, args, base_offset + offset,
1619 static_memfuncp, TYPE_BASECLASS (type, i));
1620 if (v == (struct value *) - 1)
1626 /* FIXME-bothner: Why is this commented out? Why is it here? */
1627 /* *arg1p = arg1_tmp; */
1632 return (struct value *) - 1;
1637 /* Given *ARGP, a value of type (pointer to a)* structure/union,
1638 extract the component named NAME from the ultimate target structure/union
1639 and return it as a value with its appropriate type.
1640 ERR is used in the error message if *ARGP's type is wrong.
1642 C++: ARGS is a list of argument types to aid in the selection of
1643 an appropriate method. Also, handle derived types.
1645 STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
1646 where the truthvalue of whether the function that was resolved was
1647 a static member function or not is stored.
1649 ERR is an error message to be printed in case the field is not found. */
1652 value_struct_elt (struct value **argp, struct value **args,
1653 char *name, int *static_memfuncp, char *err)
1658 COERCE_ARRAY (*argp);
1660 t = check_typedef (VALUE_TYPE (*argp));
1662 /* Follow pointers until we get to a non-pointer. */
1664 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1666 *argp = value_ind (*argp);
1667 /* Don't coerce fn pointer to fn and then back again! */
1668 if (TYPE_CODE (VALUE_TYPE (*argp)) != TYPE_CODE_FUNC)
1669 COERCE_ARRAY (*argp);
1670 t = check_typedef (VALUE_TYPE (*argp));
1673 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1674 error ("not implemented: member type in value_struct_elt");
1676 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1677 && TYPE_CODE (t) != TYPE_CODE_UNION)
1678 error ("Attempt to extract a component of a value that is not a %s.", err);
1680 /* Assume it's not, unless we see that it is. */
1681 if (static_memfuncp)
1682 *static_memfuncp = 0;
1686 /* if there are no arguments ...do this... */
1688 /* Try as a field first, because if we succeed, there
1689 is less work to be done. */
1690 v = search_struct_field (name, *argp, 0, t, 0);
1694 /* C++: If it was not found as a data field, then try to
1695 return it as a pointer to a method. */
1697 if (destructor_name_p (name, t))
1698 error ("Cannot get value of destructor");
1700 v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
1702 if (v == (struct value *) - 1)
1703 error ("Cannot take address of a method");
1706 if (TYPE_NFN_FIELDS (t))
1707 error ("There is no member or method named %s.", name);
1709 error ("There is no member named %s.", name);
1714 if (destructor_name_p (name, t))
1718 /* Destructors are a special case. */
1719 int m_index, f_index;
1722 if (get_destructor_fn_field (t, &m_index, &f_index))
1724 v = value_fn_field (NULL, TYPE_FN_FIELDLIST1 (t, m_index),
1728 error ("could not find destructor function named %s.", name);
1734 error ("destructor should not have any argument");
1738 v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
1740 if (v == (struct value *) - 1)
1742 error ("One of the arguments you tried to pass to %s could not be converted to what the function wants.", name);
1746 /* See if user tried to invoke data as function. If so,
1747 hand it back. If it's not callable (i.e., a pointer to function),
1748 gdb should give an error. */
1749 v = search_struct_field (name, *argp, 0, t, 0);
1753 error ("Structure has no component named %s.", name);
1757 /* Search through the methods of an object (and its bases)
1758 * to find a specified method. Return the pointer to the
1759 * fn_field list of overloaded instances.
1760 * Helper function for value_find_oload_list.
1761 * ARGP is a pointer to a pointer to a value (the object)
1762 * METHOD is a string containing the method name
1763 * OFFSET is the offset within the value
1764 * TYPE is the assumed type of the object
1765 * NUM_FNS is the number of overloaded instances
1766 * BASETYPE is set to the actual type of the subobject where the method is found
1767 * BOFFSET is the offset of the base subobject where the method is found */
1769 static struct fn_field *
1770 find_method_list (struct value **argp, char *method, int offset,
1771 struct type *type, int *num_fns,
1772 struct type **basetype, int *boffset)
1776 CHECK_TYPEDEF (type);
1780 /* First check in object itself */
1781 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
1783 /* pai: FIXME What about operators and type conversions? */
1784 char *fn_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
1785 if (fn_field_name && (strcmp_iw (fn_field_name, method) == 0))
1787 int len = TYPE_FN_FIELDLIST_LENGTH (type, i);
1788 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1794 /* Resolve any stub methods. */
1795 check_stub_method_group (type, i);
1801 /* Not found in object, check in base subobjects */
1802 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1805 if (BASETYPE_VIA_VIRTUAL (type, i))
1807 if (TYPE_HAS_VTABLE (type))
1809 /* HP aCC compiled type, search for virtual base offset
1810 * according to HP/Taligent runtime spec. */
1812 find_rt_vbase_offset (type, TYPE_BASECLASS (type, i),
1813 VALUE_CONTENTS_ALL (*argp),
1814 offset + VALUE_EMBEDDED_OFFSET (*argp),
1815 &base_offset, &skip);
1817 error ("Virtual base class offset not found in vtable");
1821 /* probably g++ runtime model */
1822 base_offset = VALUE_OFFSET (*argp) + offset;
1824 baseclass_offset (type, i,
1825 VALUE_CONTENTS (*argp) + base_offset,
1826 VALUE_ADDRESS (*argp) + base_offset);
1827 if (base_offset == -1)
1828 error ("virtual baseclass botch");
1832 /* non-virtual base, simply use bit position from debug info */
1834 base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
1836 f = find_method_list (argp, method, base_offset + offset,
1837 TYPE_BASECLASS (type, i), num_fns, basetype,
1845 /* Return the list of overloaded methods of a specified name.
1846 * ARGP is a pointer to a pointer to a value (the object)
1847 * METHOD is the method name
1848 * OFFSET is the offset within the value contents
1849 * NUM_FNS is the number of overloaded instances
1850 * BASETYPE is set to the type of the base subobject that defines the method
1851 * BOFFSET is the offset of the base subobject which defines the method */
1854 value_find_oload_method_list (struct value **argp, char *method, int offset,
1855 int *num_fns, struct type **basetype,
1860 t = check_typedef (VALUE_TYPE (*argp));
1862 /* code snarfed from value_struct_elt */
1863 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1865 *argp = value_ind (*argp);
1866 /* Don't coerce fn pointer to fn and then back again! */
1867 if (TYPE_CODE (VALUE_TYPE (*argp)) != TYPE_CODE_FUNC)
1868 COERCE_ARRAY (*argp);
1869 t = check_typedef (VALUE_TYPE (*argp));
1872 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1873 error ("Not implemented: member type in value_find_oload_lis");
1875 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1876 && TYPE_CODE (t) != TYPE_CODE_UNION)
1877 error ("Attempt to extract a component of a value that is not a struct or union");
1879 return find_method_list (argp, method, 0, t, num_fns, basetype, boffset);
1882 /* Given an array of argument types (ARGTYPES) (which includes an
1883 entry for "this" in the case of C++ methods), the number of
1884 arguments NARGS, the NAME of a function whether it's a method or
1885 not (METHOD), and the degree of laxness (LAX) in conforming to
1886 overload resolution rules in ANSI C++, find the best function that
1887 matches on the argument types according to the overload resolution
1890 In the case of class methods, the parameter OBJ is an object value
1891 in which to search for overloaded methods.
1893 In the case of non-method functions, the parameter FSYM is a symbol
1894 corresponding to one of the overloaded functions.
1896 Return value is an integer: 0 -> good match, 10 -> debugger applied
1897 non-standard coercions, 100 -> incompatible.
1899 If a method is being searched for, VALP will hold the value.
1900 If a non-method is being searched for, SYMP will hold the symbol for it.
1902 If a method is being searched for, and it is a static method,
1903 then STATICP will point to a non-zero value.
1905 Note: This function does *not* check the value of
1906 overload_resolution. Caller must check it to see whether overload
1907 resolution is permitted.
1911 find_overload_match (struct type **arg_types, int nargs, char *name, int method,
1912 int lax, struct value **objp, struct symbol *fsym,
1913 struct value **valp, struct symbol **symp, int *staticp)
1915 struct value *obj = (objp ? *objp : NULL);
1917 int oload_champ; /* Index of best overloaded function */
1919 struct badness_vector *oload_champ_bv = NULL; /* The measure for the current best match */
1921 struct value *temp = obj;
1922 struct fn_field *fns_ptr = NULL; /* For methods, the list of overloaded methods */
1923 struct symbol **oload_syms = NULL; /* For non-methods, the list of overloaded function symbols */
1924 int num_fns = 0; /* Number of overloaded instances being considered */
1925 struct type *basetype = NULL;
1929 struct cleanup *old_cleanups = NULL;
1931 const char *obj_type_name = NULL;
1932 char *func_name = NULL;
1933 enum oload_classification match_quality;
1935 /* Get the list of overloaded methods or functions */
1938 obj_type_name = TYPE_NAME (VALUE_TYPE (obj));
1939 /* Hack: evaluate_subexp_standard often passes in a pointer
1940 value rather than the object itself, so try again */
1941 if ((!obj_type_name || !*obj_type_name) &&
1942 (TYPE_CODE (VALUE_TYPE (obj)) == TYPE_CODE_PTR))
1943 obj_type_name = TYPE_NAME (TYPE_TARGET_TYPE (VALUE_TYPE (obj)));
1945 fns_ptr = value_find_oload_method_list (&temp, name, 0,
1947 &basetype, &boffset);
1948 if (!fns_ptr || !num_fns)
1949 error ("Couldn't find method %s%s%s",
1951 (obj_type_name && *obj_type_name) ? "::" : "",
1953 /* If we are dealing with stub method types, they should have
1954 been resolved by find_method_list via value_find_oload_method_list
1956 gdb_assert (TYPE_DOMAIN_TYPE (fns_ptr[0].type) != NULL);
1957 oload_champ = find_oload_champ (arg_types, nargs, method, num_fns,
1958 fns_ptr, oload_syms, &oload_champ_bv);
1962 const char *qualified_name = SYMBOL_CPLUS_DEMANGLED_NAME (fsym);
1963 func_name = cp_func_name (qualified_name);
1965 /* If the name is NULL this must be a C-style function.
1966 Just return the same symbol. */
1967 if (func_name == NULL)
1973 old_cleanups = make_cleanup (xfree, func_name);
1974 make_cleanup (xfree, oload_syms);
1975 make_cleanup (xfree, oload_champ_bv);
1977 oload_champ = find_oload_champ_namespace (arg_types, nargs,
1984 /* Check how bad the best match is. */
1987 = classify_oload_match (oload_champ_bv, nargs,
1988 oload_method_static (method, fns_ptr,
1991 if (match_quality == INCOMPATIBLE)
1994 error ("Cannot resolve method %s%s%s to any overloaded instance",
1996 (obj_type_name && *obj_type_name) ? "::" : "",
1999 error ("Cannot resolve function %s to any overloaded instance",
2002 else if (match_quality == NON_STANDARD)
2005 warning ("Using non-standard conversion to match method %s%s%s to supplied arguments",
2007 (obj_type_name && *obj_type_name) ? "::" : "",
2010 warning ("Using non-standard conversion to match function %s to supplied arguments",
2016 if (staticp != NULL)
2017 *staticp = oload_method_static (method, fns_ptr, oload_champ);
2018 if (TYPE_FN_FIELD_VIRTUAL_P (fns_ptr, oload_champ))
2019 *valp = value_virtual_fn_field (&temp, fns_ptr, oload_champ, basetype, boffset);
2021 *valp = value_fn_field (&temp, fns_ptr, oload_champ, basetype, boffset);
2025 *symp = oload_syms[oload_champ];
2030 if (TYPE_CODE (VALUE_TYPE (temp)) != TYPE_CODE_PTR
2031 && TYPE_CODE (VALUE_TYPE (*objp)) == TYPE_CODE_PTR)
2033 temp = value_addr (temp);
2037 if (old_cleanups != NULL)
2038 do_cleanups (old_cleanups);
2040 switch (match_quality)
2046 default: /* STANDARD */
2051 /* Find the best overload match, searching for FUNC_NAME in namespaces
2052 contained in QUALIFIED_NAME until it either finds a good match or
2053 runs out of namespaces. It stores the overloaded functions in
2054 *OLOAD_SYMS, and the badness vector in *OLOAD_CHAMP_BV. The
2055 calling function is responsible for freeing *OLOAD_SYMS and
2059 find_oload_champ_namespace (struct type **arg_types, int nargs,
2060 const char *func_name,
2061 const char *qualified_name,
2062 struct symbol ***oload_syms,
2063 struct badness_vector **oload_champ_bv)
2067 find_oload_champ_namespace_loop (arg_types, nargs,
2070 oload_syms, oload_champ_bv,
2076 /* Helper function for find_oload_champ_namespace; NAMESPACE_LEN is
2077 how deep we've looked for namespaces, and the champ is stored in
2078 OLOAD_CHAMP. The return value is 1 if the champ is a good one, 0
2081 It is the caller's responsibility to free *OLOAD_SYMS and
2085 find_oload_champ_namespace_loop (struct type **arg_types, int nargs,
2086 const char *func_name,
2087 const char *qualified_name,
2089 struct symbol ***oload_syms,
2090 struct badness_vector **oload_champ_bv,
2093 int next_namespace_len = namespace_len;
2094 int searched_deeper = 0;
2096 struct cleanup *old_cleanups;
2097 int new_oload_champ;
2098 struct symbol **new_oload_syms;
2099 struct badness_vector *new_oload_champ_bv;
2100 char *new_namespace;
2102 if (next_namespace_len != 0)
2104 gdb_assert (qualified_name[next_namespace_len] == ':');
2105 next_namespace_len += 2;
2108 += cp_find_first_component (qualified_name + next_namespace_len);
2110 /* Initialize these to values that can safely be xfree'd. */
2112 *oload_champ_bv = NULL;
2114 /* First, see if we have a deeper namespace we can search in. If we
2115 get a good match there, use it. */
2117 if (qualified_name[next_namespace_len] == ':')
2119 searched_deeper = 1;
2121 if (find_oload_champ_namespace_loop (arg_types, nargs,
2122 func_name, qualified_name,
2124 oload_syms, oload_champ_bv,
2131 /* If we reach here, either we're in the deepest namespace or we
2132 didn't find a good match in a deeper namespace. But, in the
2133 latter case, we still have a bad match in a deeper namespace;
2134 note that we might not find any match at all in the current
2135 namespace. (There's always a match in the deepest namespace,
2136 because this overload mechanism only gets called if there's a
2137 function symbol to start off with.) */
2139 old_cleanups = make_cleanup (xfree, *oload_syms);
2140 old_cleanups = make_cleanup (xfree, *oload_champ_bv);
2141 new_namespace = alloca (namespace_len + 1);
2142 strncpy (new_namespace, qualified_name, namespace_len);
2143 new_namespace[namespace_len] = '\0';
2144 new_oload_syms = make_symbol_overload_list (func_name,
2146 while (new_oload_syms[num_fns])
2149 new_oload_champ = find_oload_champ (arg_types, nargs, 0, num_fns,
2150 NULL, new_oload_syms,
2151 &new_oload_champ_bv);
2153 /* Case 1: We found a good match. Free earlier matches (if any),
2154 and return it. Case 2: We didn't find a good match, but we're
2155 not the deepest function. Then go with the bad match that the
2156 deeper function found. Case 3: We found a bad match, and we're
2157 the deepest function. Then return what we found, even though
2158 it's a bad match. */
2160 if (new_oload_champ != -1
2161 && classify_oload_match (new_oload_champ_bv, nargs, 0) == STANDARD)
2163 *oload_syms = new_oload_syms;
2164 *oload_champ = new_oload_champ;
2165 *oload_champ_bv = new_oload_champ_bv;
2166 do_cleanups (old_cleanups);
2169 else if (searched_deeper)
2171 xfree (new_oload_syms);
2172 xfree (new_oload_champ_bv);
2173 discard_cleanups (old_cleanups);
2178 gdb_assert (new_oload_champ != -1);
2179 *oload_syms = new_oload_syms;
2180 *oload_champ = new_oload_champ;
2181 *oload_champ_bv = new_oload_champ_bv;
2182 discard_cleanups (old_cleanups);
2187 /* Look for a function to take NARGS args of types ARG_TYPES. Find
2188 the best match from among the overloaded methods or functions
2189 (depending on METHOD) given by FNS_PTR or OLOAD_SYMS, respectively.
2190 The number of methods/functions in the list is given by NUM_FNS.
2191 Return the index of the best match; store an indication of the
2192 quality of the match in OLOAD_CHAMP_BV.
2194 It is the caller's responsibility to free *OLOAD_CHAMP_BV. */
2197 find_oload_champ (struct type **arg_types, int nargs, int method,
2198 int num_fns, struct fn_field *fns_ptr,
2199 struct symbol **oload_syms,
2200 struct badness_vector **oload_champ_bv)
2203 struct badness_vector *bv; /* A measure of how good an overloaded instance is */
2204 int oload_champ = -1; /* Index of best overloaded function */
2205 int oload_ambiguous = 0; /* Current ambiguity state for overload resolution */
2206 /* 0 => no ambiguity, 1 => two good funcs, 2 => incomparable funcs */
2208 *oload_champ_bv = NULL;
2210 /* Consider each candidate in turn */
2211 for (ix = 0; ix < num_fns; ix++)
2214 int static_offset = oload_method_static (method, fns_ptr, ix);
2216 struct type **parm_types;
2220 nparms = TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (fns_ptr, ix));
2224 /* If it's not a method, this is the proper place */
2225 nparms=TYPE_NFIELDS(SYMBOL_TYPE(oload_syms[ix]));
2228 /* Prepare array of parameter types */
2229 parm_types = (struct type **) xmalloc (nparms * (sizeof (struct type *)));
2230 for (jj = 0; jj < nparms; jj++)
2231 parm_types[jj] = (method
2232 ? (TYPE_FN_FIELD_ARGS (fns_ptr, ix)[jj].type)
2233 : TYPE_FIELD_TYPE (SYMBOL_TYPE (oload_syms[ix]), jj));
2235 /* Compare parameter types to supplied argument types. Skip THIS for
2237 bv = rank_function (parm_types, nparms, arg_types + static_offset,
2238 nargs - static_offset);
2240 if (!*oload_champ_bv)
2242 *oload_champ_bv = bv;
2246 /* See whether current candidate is better or worse than previous best */
2247 switch (compare_badness (bv, *oload_champ_bv))
2250 oload_ambiguous = 1; /* top two contenders are equally good */
2253 oload_ambiguous = 2; /* incomparable top contenders */
2256 *oload_champ_bv = bv; /* new champion, record details */
2257 oload_ambiguous = 0;
2268 fprintf_filtered (gdb_stderr,"Overloaded method instance %s, # of parms %d\n", fns_ptr[ix].physname, nparms);
2270 fprintf_filtered (gdb_stderr,"Overloaded function instance %s # of parms %d\n", SYMBOL_DEMANGLED_NAME (oload_syms[ix]), nparms);
2271 for (jj = 0; jj < nargs - static_offset; jj++)
2272 fprintf_filtered (gdb_stderr,"...Badness @ %d : %d\n", jj, bv->rank[jj]);
2273 fprintf_filtered (gdb_stderr,"Overload resolution champion is %d, ambiguous? %d\n", oload_champ, oload_ambiguous);
2280 /* Return 1 if we're looking at a static method, 0 if we're looking at
2281 a non-static method or a function that isn't a method. */
2284 oload_method_static (int method, struct fn_field *fns_ptr, int index)
2286 if (method && TYPE_FN_FIELD_STATIC_P (fns_ptr, index))
2292 /* Check how good an overload match OLOAD_CHAMP_BV represents. */
2294 static enum oload_classification
2295 classify_oload_match (struct badness_vector *oload_champ_bv,
2301 for (ix = 1; ix <= nargs - static_offset; ix++)
2303 if (oload_champ_bv->rank[ix] >= 100)
2304 return INCOMPATIBLE; /* truly mismatched types */
2305 else if (oload_champ_bv->rank[ix] >= 10)
2306 return NON_STANDARD; /* non-standard type conversions needed */
2309 return STANDARD; /* Only standard conversions needed. */
2312 /* C++: return 1 is NAME is a legitimate name for the destructor
2313 of type TYPE. If TYPE does not have a destructor, or
2314 if NAME is inappropriate for TYPE, an error is signaled. */
2316 destructor_name_p (const char *name, const struct type *type)
2318 /* destructors are a special case. */
2322 char *dname = type_name_no_tag (type);
2323 char *cp = strchr (dname, '<');
2326 /* Do not compare the template part for template classes. */
2328 len = strlen (dname);
2331 if (strlen (name + 1) != len || strncmp (dname, name + 1, len) != 0)
2332 error ("name of destructor must equal name of class");
2339 /* Helper function for check_field: Given TYPE, a structure/union,
2340 return 1 if the component named NAME from the ultimate
2341 target structure/union is defined, otherwise, return 0. */
2344 check_field_in (struct type *type, const char *name)
2348 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
2350 char *t_field_name = TYPE_FIELD_NAME (type, i);
2351 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
2355 /* C++: If it was not found as a data field, then try to
2356 return it as a pointer to a method. */
2358 /* Destructors are a special case. */
2359 if (destructor_name_p (name, type))
2361 int m_index, f_index;
2363 return get_destructor_fn_field (type, &m_index, &f_index);
2366 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
2368 if (strcmp_iw (TYPE_FN_FIELDLIST_NAME (type, i), name) == 0)
2372 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
2373 if (check_field_in (TYPE_BASECLASS (type, i), name))
2380 /* C++: Given ARG1, a value of type (pointer to a)* structure/union,
2381 return 1 if the component named NAME from the ultimate
2382 target structure/union is defined, otherwise, return 0. */
2385 check_field (struct value *arg1, const char *name)
2389 COERCE_ARRAY (arg1);
2391 t = VALUE_TYPE (arg1);
2393 /* Follow pointers until we get to a non-pointer. */
2398 if (TYPE_CODE (t) != TYPE_CODE_PTR && TYPE_CODE (t) != TYPE_CODE_REF)
2400 t = TYPE_TARGET_TYPE (t);
2403 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
2404 error ("not implemented: member type in check_field");
2406 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
2407 && TYPE_CODE (t) != TYPE_CODE_UNION)
2408 error ("Internal error: `this' is not an aggregate");
2410 return check_field_in (t, name);
2413 /* C++: Given an aggregate type CURTYPE, and a member name NAME,
2414 return the appropriate member. This function is used to resolve
2415 user expressions of the form "DOMAIN::NAME". For more details on
2416 what happens, see the comment before
2417 value_struct_elt_for_reference. */
2420 value_aggregate_elt (struct type *curtype,
2424 switch (TYPE_CODE (curtype))
2426 case TYPE_CODE_STRUCT:
2427 case TYPE_CODE_UNION:
2428 return value_struct_elt_for_reference (curtype, 0, curtype, name, NULL,
2430 case TYPE_CODE_NAMESPACE:
2431 return value_namespace_elt (curtype, name, noside);
2433 internal_error (__FILE__, __LINE__,
2434 "non-aggregate type in value_aggregate_elt");
2438 /* C++: Given an aggregate type CURTYPE, and a member name NAME,
2439 return the address of this member as a "pointer to member"
2440 type. If INTYPE is non-null, then it will be the type
2441 of the member we are looking for. This will help us resolve
2442 "pointers to member functions". This function is used
2443 to resolve user expressions of the form "DOMAIN::NAME". */
2445 static struct value *
2446 value_struct_elt_for_reference (struct type *domain, int offset,
2447 struct type *curtype, char *name,
2448 struct type *intype,
2451 struct type *t = curtype;
2455 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
2456 && TYPE_CODE (t) != TYPE_CODE_UNION)
2457 error ("Internal error: non-aggregate type to value_struct_elt_for_reference");
2459 for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
2461 char *t_field_name = TYPE_FIELD_NAME (t, i);
2463 if (t_field_name && strcmp (t_field_name, name) == 0)
2465 if (TYPE_FIELD_STATIC (t, i))
2467 v = value_static_field (t, i);
2469 error ("static field %s has been optimized out",
2473 if (TYPE_FIELD_PACKED (t, i))
2474 error ("pointers to bitfield members not allowed");
2476 return value_from_longest
2477 (lookup_reference_type (lookup_member_type (TYPE_FIELD_TYPE (t, i),
2479 offset + (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3));
2483 /* C++: If it was not found as a data field, then try to
2484 return it as a pointer to a method. */
2486 /* Destructors are a special case. */
2487 if (destructor_name_p (name, t))
2489 error ("member pointers to destructors not implemented yet");
2492 /* Perform all necessary dereferencing. */
2493 while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR)
2494 intype = TYPE_TARGET_TYPE (intype);
2496 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
2498 char *t_field_name = TYPE_FN_FIELDLIST_NAME (t, i);
2499 char dem_opname[64];
2501 if (strncmp (t_field_name, "__", 2) == 0 ||
2502 strncmp (t_field_name, "op", 2) == 0 ||
2503 strncmp (t_field_name, "type", 4) == 0)
2505 if (cplus_demangle_opname (t_field_name, dem_opname, DMGL_ANSI))
2506 t_field_name = dem_opname;
2507 else if (cplus_demangle_opname (t_field_name, dem_opname, 0))
2508 t_field_name = dem_opname;
2510 if (t_field_name && strcmp (t_field_name, name) == 0)
2512 int j = TYPE_FN_FIELDLIST_LENGTH (t, i);
2513 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
2515 check_stub_method_group (t, i);
2517 if (intype == 0 && j > 1)
2518 error ("non-unique member `%s' requires type instantiation", name);
2522 if (TYPE_FN_FIELD_TYPE (f, j) == intype)
2525 error ("no member function matches that type instantiation");
2530 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
2532 return value_from_longest
2533 (lookup_reference_type
2534 (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
2536 (LONGEST) METHOD_PTR_FROM_VOFFSET (TYPE_FN_FIELD_VOFFSET (f, j)));
2540 struct symbol *s = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
2541 0, VAR_DOMAIN, 0, NULL);
2548 v = read_var_value (s, 0);
2550 VALUE_TYPE (v) = lookup_reference_type
2551 (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
2559 for (i = TYPE_N_BASECLASSES (t) - 1; i >= 0; i--)
2564 if (BASETYPE_VIA_VIRTUAL (t, i))
2567 base_offset = TYPE_BASECLASS_BITPOS (t, i) / 8;
2568 v = value_struct_elt_for_reference (domain,
2569 offset + base_offset,
2570 TYPE_BASECLASS (t, i),
2578 /* As a last chance, pretend that CURTYPE is a namespace, and look
2579 it up that way; this (frequently) works for types nested inside
2582 return value_maybe_namespace_elt (curtype, name, noside);
2585 /* C++: Return the member NAME of the namespace given by the type
2588 static struct value *
2589 value_namespace_elt (const struct type *curtype,
2593 struct value *retval = value_maybe_namespace_elt (curtype, name,
2597 error ("No symbol \"%s\" in namespace \"%s\".", name,
2598 TYPE_TAG_NAME (curtype));
2603 /* A helper function used by value_namespace_elt and
2604 value_struct_elt_for_reference. It looks up NAME inside the
2605 context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
2606 is a class and NAME refers to a type in CURTYPE itself (as opposed
2607 to, say, some base class of CURTYPE). */
2609 static struct value *
2610 value_maybe_namespace_elt (const struct type *curtype,
2614 const char *namespace_name = TYPE_TAG_NAME (curtype);
2617 sym = cp_lookup_symbol_namespace (namespace_name, name, NULL,
2618 get_selected_block (0), VAR_DOMAIN,
2623 else if ((noside == EVAL_AVOID_SIDE_EFFECTS)
2624 && (SYMBOL_CLASS (sym) == LOC_TYPEDEF))
2625 return allocate_value (SYMBOL_TYPE (sym));
2627 return value_of_variable (sym, get_selected_block (0));
2630 /* Given a pointer value V, find the real (RTTI) type
2631 of the object it points to.
2632 Other parameters FULL, TOP, USING_ENC as with value_rtti_type()
2633 and refer to the values computed for the object pointed to. */
2636 value_rtti_target_type (struct value *v, int *full, int *top, int *using_enc)
2638 struct value *target;
2640 target = value_ind (v);
2642 return value_rtti_type (target, full, top, using_enc);
2645 /* Given a value pointed to by ARGP, check its real run-time type, and
2646 if that is different from the enclosing type, create a new value
2647 using the real run-time type as the enclosing type (and of the same
2648 type as ARGP) and return it, with the embedded offset adjusted to
2649 be the correct offset to the enclosed object
2650 RTYPE is the type, and XFULL, XTOP, and XUSING_ENC are the other
2651 parameters, computed by value_rtti_type(). If these are available,
2652 they can be supplied and a second call to value_rtti_type() is avoided.
2653 (Pass RTYPE == NULL if they're not available */
2656 value_full_object (struct value *argp, struct type *rtype, int xfull, int xtop,
2659 struct type *real_type;
2663 struct value *new_val;
2670 using_enc = xusing_enc;
2673 real_type = value_rtti_type (argp, &full, &top, &using_enc);
2675 /* If no RTTI data, or if object is already complete, do nothing */
2676 if (!real_type || real_type == VALUE_ENCLOSING_TYPE (argp))
2679 /* If we have the full object, but for some reason the enclosing
2680 type is wrong, set it *//* pai: FIXME -- sounds iffy */
2683 argp = value_change_enclosing_type (argp, real_type);
2687 /* Check if object is in memory */
2688 if (VALUE_LVAL (argp) != lval_memory)
2690 warning ("Couldn't retrieve complete object of RTTI type %s; object may be in register(s).", TYPE_NAME (real_type));
2695 /* All other cases -- retrieve the complete object */
2696 /* Go back by the computed top_offset from the beginning of the object,
2697 adjusting for the embedded offset of argp if that's what value_rtti_type
2698 used for its computation. */
2699 new_val = value_at_lazy (real_type, VALUE_ADDRESS (argp) - top +
2700 (using_enc ? 0 : VALUE_EMBEDDED_OFFSET (argp)),
2701 VALUE_BFD_SECTION (argp));
2702 VALUE_TYPE (new_val) = VALUE_TYPE (argp);
2703 VALUE_EMBEDDED_OFFSET (new_val) = using_enc ? top + VALUE_EMBEDDED_OFFSET (argp) : top;
2710 /* Return the value of the local variable, if one exists.
2711 Flag COMPLAIN signals an error if the request is made in an
2712 inappropriate context. */
2715 value_of_local (const char *name, int complain)
2717 struct symbol *func, *sym;
2721 if (deprecated_selected_frame == 0)
2724 error ("no frame selected");
2729 func = get_frame_function (deprecated_selected_frame);
2733 error ("no `%s' in nameless context", name);
2738 b = SYMBOL_BLOCK_VALUE (func);
2739 if (dict_empty (BLOCK_DICT (b)))
2742 error ("no args, no `%s'", name);
2747 /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
2748 symbol instead of the LOC_ARG one (if both exist). */
2749 sym = lookup_block_symbol (b, name, NULL, VAR_DOMAIN);
2753 error ("current stack frame does not contain a variable named `%s'", name);
2758 ret = read_var_value (sym, deprecated_selected_frame);
2759 if (ret == 0 && complain)
2760 error ("`%s' argument unreadable", name);
2764 /* C++/Objective-C: return the value of the class instance variable,
2765 if one exists. Flag COMPLAIN signals an error if the request is
2766 made in an inappropriate context. */
2769 value_of_this (int complain)
2771 if (current_language->la_language == language_objc)
2772 return value_of_local ("self", complain);
2774 return value_of_local ("this", complain);
2777 /* Create a slice (sub-string, sub-array) of ARRAY, that is LENGTH elements
2778 long, starting at LOWBOUND. The result has the same lower bound as
2779 the original ARRAY. */
2782 value_slice (struct value *array, int lowbound, int length)
2784 struct type *slice_range_type, *slice_type, *range_type;
2785 LONGEST lowerbound, upperbound;
2786 struct value *slice;
2787 struct type *array_type;
2788 array_type = check_typedef (VALUE_TYPE (array));
2789 COERCE_VARYING_ARRAY (array, array_type);
2790 if (TYPE_CODE (array_type) != TYPE_CODE_ARRAY
2791 && TYPE_CODE (array_type) != TYPE_CODE_STRING
2792 && TYPE_CODE (array_type) != TYPE_CODE_BITSTRING)
2793 error ("cannot take slice of non-array");
2794 range_type = TYPE_INDEX_TYPE (array_type);
2795 if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0)
2796 error ("slice from bad array or bitstring");
2797 if (lowbound < lowerbound || length < 0
2798 || lowbound + length - 1 > upperbound)
2799 error ("slice out of range");
2800 /* FIXME-type-allocation: need a way to free this type when we are
2802 slice_range_type = create_range_type ((struct type *) NULL,
2803 TYPE_TARGET_TYPE (range_type),
2804 lowbound, lowbound + length - 1);
2805 if (TYPE_CODE (array_type) == TYPE_CODE_BITSTRING)
2808 slice_type = create_set_type ((struct type *) NULL, slice_range_type);
2809 TYPE_CODE (slice_type) = TYPE_CODE_BITSTRING;
2810 slice = value_zero (slice_type, not_lval);
2811 for (i = 0; i < length; i++)
2813 int element = value_bit_index (array_type,
2814 VALUE_CONTENTS (array),
2817 error ("internal error accessing bitstring");
2818 else if (element > 0)
2820 int j = i % TARGET_CHAR_BIT;
2821 if (BITS_BIG_ENDIAN)
2822 j = TARGET_CHAR_BIT - 1 - j;
2823 VALUE_CONTENTS_RAW (slice)[i / TARGET_CHAR_BIT] |= (1 << j);
2826 /* We should set the address, bitssize, and bitspos, so the clice
2827 can be used on the LHS, but that may require extensions to
2828 value_assign. For now, just leave as a non_lval. FIXME. */
2832 struct type *element_type = TYPE_TARGET_TYPE (array_type);
2834 = (lowbound - lowerbound) * TYPE_LENGTH (check_typedef (element_type));
2835 slice_type = create_array_type ((struct type *) NULL, element_type,
2837 TYPE_CODE (slice_type) = TYPE_CODE (array_type);
2838 slice = allocate_value (slice_type);
2839 if (VALUE_LAZY (array))
2840 VALUE_LAZY (slice) = 1;
2842 memcpy (VALUE_CONTENTS (slice), VALUE_CONTENTS (array) + offset,
2843 TYPE_LENGTH (slice_type));
2844 if (VALUE_LVAL (array) == lval_internalvar)
2845 VALUE_LVAL (slice) = lval_internalvar_component;
2847 VALUE_LVAL (slice) = VALUE_LVAL (array);
2848 VALUE_ADDRESS (slice) = VALUE_ADDRESS (array);
2849 VALUE_OFFSET (slice) = VALUE_OFFSET (array) + offset;
2854 /* Create a value for a FORTRAN complex number. Currently most of
2855 the time values are coerced to COMPLEX*16 (i.e. a complex number
2856 composed of 2 doubles. This really should be a smarter routine
2857 that figures out precision inteligently as opposed to assuming
2858 doubles. FIXME: fmb */
2861 value_literal_complex (struct value *arg1, struct value *arg2, struct type *type)
2864 struct type *real_type = TYPE_TARGET_TYPE (type);
2866 val = allocate_value (type);
2867 arg1 = value_cast (real_type, arg1);
2868 arg2 = value_cast (real_type, arg2);
2870 memcpy (VALUE_CONTENTS_RAW (val),
2871 VALUE_CONTENTS (arg1), TYPE_LENGTH (real_type));
2872 memcpy (VALUE_CONTENTS_RAW (val) + TYPE_LENGTH (real_type),
2873 VALUE_CONTENTS (arg2), TYPE_LENGTH (real_type));
2877 /* Cast a value into the appropriate complex data type. */
2879 static struct value *
2880 cast_into_complex (struct type *type, struct value *val)
2882 struct type *real_type = TYPE_TARGET_TYPE (type);
2883 if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_COMPLEX)
2885 struct type *val_real_type = TYPE_TARGET_TYPE (VALUE_TYPE (val));
2886 struct value *re_val = allocate_value (val_real_type);
2887 struct value *im_val = allocate_value (val_real_type);
2889 memcpy (VALUE_CONTENTS_RAW (re_val),
2890 VALUE_CONTENTS (val), TYPE_LENGTH (val_real_type));
2891 memcpy (VALUE_CONTENTS_RAW (im_val),
2892 VALUE_CONTENTS (val) + TYPE_LENGTH (val_real_type),
2893 TYPE_LENGTH (val_real_type));
2895 return value_literal_complex (re_val, im_val, type);
2897 else if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FLT
2898 || TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_INT)
2899 return value_literal_complex (val, value_zero (real_type, not_lval), type);
2901 error ("cannot cast non-number to complex");
2905 _initialize_valops (void)
2909 (add_set_cmd ("abandon", class_support, var_boolean, (char *) &auto_abandon,
2910 "Set automatic abandonment of expressions upon failure.",
2916 (add_set_cmd ("overload-resolution", class_support, var_boolean, (char *) &overload_resolution,
2917 "Set overload resolution in evaluating C++ functions.",
2920 overload_resolution = 1;