1 /* Perform non-arithmetic operations on values, for GDB.
2 Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
33 #include "gdb_string.h"
35 /* Default to coercing float to double in function calls only when there is
36 no prototype. Otherwise on targets where the debug information is incorrect
37 for either the prototype or non-prototype case, we can force it by defining
38 COERCE_FLOAT_TO_DOUBLE in the target configuration file. */
40 #ifndef COERCE_FLOAT_TO_DOUBLE
41 #define COERCE_FLOAT_TO_DOUBLE (param_type == NULL)
44 /* Local functions. */
46 static int typecmp PARAMS ((int staticp, struct type *t1[], value_ptr t2[]));
48 static CORE_ADDR find_function_addr PARAMS ((value_ptr, struct type **));
50 static CORE_ADDR value_push PARAMS ((CORE_ADDR, value_ptr));
52 static value_ptr search_struct_field PARAMS ((char *, value_ptr, int,
55 static value_ptr search_struct_method PARAMS ((char *, value_ptr *,
57 int, int *, struct type *));
59 static int check_field_in PARAMS ((struct type *, const char *));
61 static CORE_ADDR allocate_space_in_inferior PARAMS ((int));
63 static value_ptr cast_into_complex PARAMS ((struct type *, value_ptr));
65 #define VALUE_SUBSTRING_START(VAL) VALUE_FRAME(VAL)
67 /* Flag for whether we want to abandon failed expression evals by default. */
70 static int auto_abandon = 0;
74 /* Find the address of function name NAME in the inferior. */
77 find_function_in_inferior (name)
80 register struct symbol *sym;
81 sym = lookup_symbol (name, 0, VAR_NAMESPACE, 0, NULL);
84 if (SYMBOL_CLASS (sym) != LOC_BLOCK)
86 error ("\"%s\" exists in this program but is not a function.",
89 return value_of_variable (sym, NULL);
93 struct minimal_symbol *msymbol = lookup_minimal_symbol(name, NULL, NULL);
98 type = lookup_pointer_type (builtin_type_char);
99 type = lookup_function_type (type);
100 type = lookup_pointer_type (type);
101 maddr = (LONGEST) SYMBOL_VALUE_ADDRESS (msymbol);
102 return value_from_longest (type, maddr);
106 error ("evaluation of this expression requires the program to have a function \"%s\".", name);
111 /* Allocate NBYTES of space in the inferior using the inferior's malloc
112 and return a value that is a pointer to the allocated space. */
115 value_allocate_space_in_inferior (len)
119 register value_ptr val = find_function_in_inferior ("malloc");
121 blocklen = value_from_longest (builtin_type_int, (LONGEST) len);
122 val = call_function_by_hand (val, 1, &blocklen);
123 if (value_logical_not (val))
125 error ("No memory available to program.");
131 allocate_space_in_inferior (len)
134 return value_as_long (value_allocate_space_in_inferior (len));
137 /* Cast value ARG2 to type TYPE and return as a value.
138 More general than a C cast: accepts any two types of the same length,
139 and if ARG2 is an lvalue it can be cast into anything at all. */
140 /* In C++, casts may change pointer or object representations. */
143 value_cast (type, arg2)
145 register value_ptr arg2;
147 register enum type_code code1;
148 register enum type_code code2;
152 if (VALUE_TYPE (arg2) == type)
155 CHECK_TYPEDEF (type);
156 code1 = TYPE_CODE (type);
158 type2 = check_typedef (VALUE_TYPE (arg2));
160 /* A cast to an undetermined-length array_type, such as (TYPE [])OBJECT,
161 is treated like a cast to (TYPE [N])OBJECT,
162 where N is sizeof(OBJECT)/sizeof(TYPE). */
163 if (code1 == TYPE_CODE_ARRAY)
165 struct type *element_type = TYPE_TARGET_TYPE (type);
166 unsigned element_length = TYPE_LENGTH (check_typedef (element_type));
167 if (element_length > 0
168 && TYPE_ARRAY_UPPER_BOUND_TYPE (type) == BOUND_CANNOT_BE_DETERMINED)
170 struct type *range_type = TYPE_INDEX_TYPE (type);
171 int val_length = TYPE_LENGTH (type2);
172 LONGEST low_bound, high_bound, new_length;
173 if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
174 low_bound = 0, high_bound = 0;
175 new_length = val_length / element_length;
176 if (val_length % element_length != 0)
177 warning("array element type size does not divide object size in cast");
178 /* FIXME-type-allocation: need a way to free this type when we are
180 range_type = create_range_type ((struct type *) NULL,
181 TYPE_TARGET_TYPE (range_type),
183 new_length + low_bound - 1);
184 VALUE_TYPE (arg2) = create_array_type ((struct type *) NULL,
185 element_type, range_type);
190 if (current_language->c_style_arrays
191 && TYPE_CODE (type2) == TYPE_CODE_ARRAY)
192 arg2 = value_coerce_array (arg2);
194 if (TYPE_CODE (type2) == TYPE_CODE_FUNC)
195 arg2 = value_coerce_function (arg2);
197 type2 = check_typedef (VALUE_TYPE (arg2));
198 COERCE_VARYING_ARRAY (arg2, type2);
199 code2 = TYPE_CODE (type2);
201 if (code1 == TYPE_CODE_COMPLEX)
202 return cast_into_complex (type, arg2);
203 if (code1 == TYPE_CODE_BOOL || code1 == TYPE_CODE_CHAR)
204 code1 = TYPE_CODE_INT;
205 if (code2 == TYPE_CODE_BOOL || code2 == TYPE_CODE_CHAR)
206 code2 = TYPE_CODE_INT;
208 scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
209 || code2 == TYPE_CODE_ENUM || code2 == TYPE_CODE_RANGE);
211 if ( code1 == TYPE_CODE_STRUCT
212 && code2 == TYPE_CODE_STRUCT
213 && TYPE_NAME (type) != 0)
215 /* Look in the type of the source to see if it contains the
216 type of the target as a superclass. If so, we'll need to
217 offset the object in addition to changing its type. */
218 value_ptr v = search_struct_field (type_name_no_tag (type),
222 VALUE_TYPE (v) = type;
226 if (code1 == TYPE_CODE_FLT && scalar)
227 return value_from_double (type, value_as_double (arg2));
228 else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM
229 || code1 == TYPE_CODE_RANGE)
230 && (scalar || code2 == TYPE_CODE_PTR))
231 return value_from_longest (type, value_as_long (arg2));
232 else if (TYPE_LENGTH (type) == TYPE_LENGTH (type2))
234 if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
236 /* Look in the type of the source to see if it contains the
237 type of the target as a superclass. If so, we'll need to
238 offset the pointer rather than just change its type. */
239 struct type *t1 = check_typedef (TYPE_TARGET_TYPE (type));
240 struct type *t2 = check_typedef (TYPE_TARGET_TYPE (type2));
241 if ( TYPE_CODE (t1) == TYPE_CODE_STRUCT
242 && TYPE_CODE (t2) == TYPE_CODE_STRUCT
243 && TYPE_NAME (t1) != 0) /* if name unknown, can't have supercl */
245 value_ptr v = search_struct_field (type_name_no_tag (t1),
246 value_ind (arg2), 0, t2, 1);
250 VALUE_TYPE (v) = type;
254 /* No superclass found, just fall through to change ptr type. */
256 VALUE_TYPE (arg2) = type;
259 else if (chill_varying_type (type))
261 struct type *range1, *range2, *eltype1, *eltype2;
264 LONGEST low_bound, high_bound;
265 char *valaddr, *valaddr_data;
266 if (code2 == TYPE_CODE_BITSTRING)
267 error ("not implemented: converting bitstring to varying type");
268 if ((code2 != TYPE_CODE_ARRAY && code2 != TYPE_CODE_STRING)
269 || (eltype1 = check_typedef (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 1))),
270 eltype2 = check_typedef (TYPE_TARGET_TYPE (type2)),
271 (TYPE_LENGTH (eltype1) != TYPE_LENGTH (eltype2)
272 /* || TYPE_CODE (eltype1) != TYPE_CODE (eltype2) */ )))
273 error ("Invalid conversion to varying type");
274 range1 = TYPE_FIELD_TYPE (TYPE_FIELD_TYPE (type, 1), 0);
275 range2 = TYPE_FIELD_TYPE (type2, 0);
276 if (get_discrete_bounds (range1, &low_bound, &high_bound) < 0)
279 count1 = high_bound - low_bound + 1;
280 if (get_discrete_bounds (range2, &low_bound, &high_bound) < 0)
281 count1 = -1, count2 = 0; /* To force error before */
283 count2 = high_bound - low_bound + 1;
285 error ("target varying type is too small");
286 val = allocate_value (type);
287 valaddr = VALUE_CONTENTS_RAW (val);
288 valaddr_data = valaddr + TYPE_FIELD_BITPOS (type, 1) / 8;
289 /* Set val's __var_length field to count2. */
290 store_signed_integer (valaddr, TYPE_LENGTH (TYPE_FIELD_TYPE (type, 0)),
292 /* Set the __var_data field to count2 elements copied from arg2. */
293 memcpy (valaddr_data, VALUE_CONTENTS (arg2),
294 count2 * TYPE_LENGTH (eltype2));
295 /* Zero the rest of the __var_data field of val. */
296 memset (valaddr_data + count2 * TYPE_LENGTH (eltype2), '\0',
297 (count1 - count2) * TYPE_LENGTH (eltype2));
300 else if (VALUE_LVAL (arg2) == lval_memory)
302 return value_at_lazy (type, VALUE_ADDRESS (arg2) + VALUE_OFFSET (arg2));
304 else if (code1 == TYPE_CODE_VOID)
306 return value_zero (builtin_type_void, not_lval);
310 error ("Invalid cast.");
315 /* Create a value of type TYPE that is zero, and return it. */
318 value_zero (type, lv)
322 register value_ptr val = allocate_value (type);
324 memset (VALUE_CONTENTS (val), 0, TYPE_LENGTH (check_typedef (type)));
325 VALUE_LVAL (val) = lv;
330 /* Return a value with type TYPE located at ADDR.
332 Call value_at only if the data needs to be fetched immediately;
333 if we can be 'lazy' and defer the fetch, perhaps indefinately, call
334 value_at_lazy instead. value_at_lazy simply records the address of
335 the data and sets the lazy-evaluation-required flag. The lazy flag
336 is tested in the VALUE_CONTENTS macro, which is used if and when
337 the contents are actually required. */
340 value_at (type, addr)
344 register value_ptr val;
346 if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
347 error ("Attempt to dereference a generic pointer.");
349 val = allocate_value (type);
351 read_memory (addr, VALUE_CONTENTS_RAW (val), TYPE_LENGTH (type));
353 VALUE_LVAL (val) = lval_memory;
354 VALUE_ADDRESS (val) = addr;
359 /* Return a lazy value with type TYPE located at ADDR (cf. value_at). */
362 value_at_lazy (type, addr)
366 register value_ptr val;
368 if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
369 error ("Attempt to dereference a generic pointer.");
371 val = allocate_value (type);
373 VALUE_LVAL (val) = lval_memory;
374 VALUE_ADDRESS (val) = addr;
375 VALUE_LAZY (val) = 1;
380 /* Called only from the VALUE_CONTENTS macro, if the current data for
381 a variable needs to be loaded into VALUE_CONTENTS(VAL). Fetches the
382 data from the user's process, and clears the lazy flag to indicate
383 that the data in the buffer is valid.
385 If the value is zero-length, we avoid calling read_memory, which would
386 abort. We mark the value as fetched anyway -- all 0 bytes of it.
388 This function returns a value because it is used in the VALUE_CONTENTS
389 macro as part of an expression, where a void would not work. The
393 value_fetch_lazy (val)
394 register value_ptr val;
396 CORE_ADDR addr = VALUE_ADDRESS (val) + VALUE_OFFSET (val);
397 int length = TYPE_LENGTH (VALUE_TYPE (val));
400 read_memory (addr, VALUE_CONTENTS_RAW (val), length);
401 VALUE_LAZY (val) = 0;
406 /* Store the contents of FROMVAL into the location of TOVAL.
407 Return a new value with the location of TOVAL and contents of FROMVAL. */
410 value_assign (toval, fromval)
411 register value_ptr toval, fromval;
413 register struct type *type;
414 register value_ptr val;
415 char raw_buffer[MAX_REGISTER_RAW_SIZE];
418 if (!toval->modifiable)
419 error ("Left operand of assignment is not a modifiable lvalue.");
423 type = VALUE_TYPE (toval);
424 if (VALUE_LVAL (toval) != lval_internalvar)
425 fromval = value_cast (type, fromval);
427 COERCE_ARRAY (fromval);
428 CHECK_TYPEDEF (type);
430 /* If TOVAL is a special machine register requiring conversion
431 of program values to a special raw format,
432 convert FROMVAL's contents now, with result in `raw_buffer',
433 and set USE_BUFFER to the number of bytes to write. */
435 #ifdef REGISTER_CONVERTIBLE
436 if (VALUE_REGNO (toval) >= 0
437 && REGISTER_CONVERTIBLE (VALUE_REGNO (toval)))
439 int regno = VALUE_REGNO (toval);
440 if (REGISTER_CONVERTIBLE (regno))
442 struct type *fromtype = check_typedef (VALUE_TYPE (fromval));
443 REGISTER_CONVERT_TO_RAW (fromtype, regno,
444 VALUE_CONTENTS (fromval), raw_buffer);
445 use_buffer = REGISTER_RAW_SIZE (regno);
450 switch (VALUE_LVAL (toval))
452 case lval_internalvar:
453 set_internalvar (VALUE_INTERNALVAR (toval), fromval);
454 return value_copy (VALUE_INTERNALVAR (toval)->value);
456 case lval_internalvar_component:
457 set_internalvar_component (VALUE_INTERNALVAR (toval),
458 VALUE_OFFSET (toval),
459 VALUE_BITPOS (toval),
460 VALUE_BITSIZE (toval),
465 if (VALUE_BITSIZE (toval))
467 char buffer[sizeof (LONGEST)];
468 /* We assume that the argument to read_memory is in units of
469 host chars. FIXME: Is that correct? */
470 int len = (VALUE_BITPOS (toval)
471 + VALUE_BITSIZE (toval)
475 if (len > (int) sizeof (LONGEST))
476 error ("Can't handle bitfields which don't fit in a %d bit word.",
477 sizeof (LONGEST) * HOST_CHAR_BIT);
479 read_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
481 modify_field (buffer, value_as_long (fromval),
482 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
483 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
487 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
488 raw_buffer, use_buffer);
490 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
491 VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
495 if (VALUE_BITSIZE (toval))
497 char buffer[sizeof (LONGEST)];
498 int len = REGISTER_RAW_SIZE (VALUE_REGNO (toval));
500 if (len > (int) sizeof (LONGEST))
501 error ("Can't handle bitfields in registers larger than %d bits.",
502 sizeof (LONGEST) * HOST_CHAR_BIT);
504 if (VALUE_BITPOS (toval) + VALUE_BITSIZE (toval)
505 > len * HOST_CHAR_BIT)
506 /* Getting this right would involve being very careful about
509 Can't handle bitfield which doesn't fit in a single register.");
511 read_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
513 modify_field (buffer, value_as_long (fromval),
514 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
515 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
519 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
520 raw_buffer, use_buffer);
523 /* Do any conversion necessary when storing this type to more
524 than one register. */
525 #ifdef REGISTER_CONVERT_FROM_TYPE
526 memcpy (raw_buffer, VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
527 REGISTER_CONVERT_FROM_TYPE(VALUE_REGNO (toval), type, raw_buffer);
528 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
529 raw_buffer, TYPE_LENGTH (type));
531 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
532 VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
535 /* Assigning to the stack pointer, frame pointer, and other
536 (architecture and calling convention specific) registers may
537 cause the frame cache to be out of date. We just do this
538 on all assignments to registers for simplicity; I doubt the slowdown
540 reinit_frame_cache ();
543 case lval_reg_frame_relative:
545 /* value is stored in a series of registers in the frame
546 specified by the structure. Copy that value out, modify
547 it, and copy it back in. */
548 int amount_to_copy = (VALUE_BITSIZE (toval) ? 1 : TYPE_LENGTH (type));
549 int reg_size = REGISTER_RAW_SIZE (VALUE_FRAME_REGNUM (toval));
550 int byte_offset = VALUE_OFFSET (toval) % reg_size;
551 int reg_offset = VALUE_OFFSET (toval) / reg_size;
554 /* Make the buffer large enough in all cases. */
555 char *buffer = (char *) alloca (amount_to_copy
557 + MAX_REGISTER_RAW_SIZE);
560 struct frame_info *frame;
562 /* Figure out which frame this is in currently. */
563 for (frame = get_current_frame ();
564 frame && FRAME_FP (frame) != VALUE_FRAME (toval);
565 frame = get_prev_frame (frame))
569 error ("Value being assigned to is no longer active.");
571 amount_to_copy += (reg_size - amount_to_copy % reg_size);
574 for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset,
576 amount_copied < amount_to_copy;
577 amount_copied += reg_size, regno++)
579 get_saved_register (buffer + amount_copied,
580 (int *)NULL, (CORE_ADDR *)NULL,
581 frame, regno, (enum lval_type *)NULL);
584 /* Modify what needs to be modified. */
585 if (VALUE_BITSIZE (toval))
586 modify_field (buffer + byte_offset,
587 value_as_long (fromval),
588 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
590 memcpy (buffer + byte_offset, raw_buffer, use_buffer);
592 memcpy (buffer + byte_offset, VALUE_CONTENTS (fromval),
596 for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset,
598 amount_copied < amount_to_copy;
599 amount_copied += reg_size, regno++)
605 /* Just find out where to put it. */
606 get_saved_register ((char *)NULL,
607 &optim, &addr, frame, regno, &lval);
610 error ("Attempt to assign to a value that was optimized out.");
611 if (lval == lval_memory)
612 write_memory (addr, buffer + amount_copied, reg_size);
613 else if (lval == lval_register)
614 write_register_bytes (addr, buffer + amount_copied, reg_size);
616 error ("Attempt to assign to an unmodifiable value.");
623 error ("Left operand of assignment is not an lvalue.");
626 /* If the field does not entirely fill a LONGEST, then zero the sign bits.
627 If the field is signed, and is negative, then sign extend. */
628 if ((VALUE_BITSIZE (toval) > 0)
629 && (VALUE_BITSIZE (toval) < 8 * (int) sizeof (LONGEST)))
631 LONGEST fieldval = value_as_long (fromval);
632 LONGEST valmask = (((unsigned LONGEST) 1) << VALUE_BITSIZE (toval)) - 1;
635 if (!TYPE_UNSIGNED (type) && (fieldval & (valmask ^ (valmask >> 1))))
636 fieldval |= ~valmask;
638 fromval = value_from_longest (type, fieldval);
641 val = value_copy (toval);
642 memcpy (VALUE_CONTENTS_RAW (val), VALUE_CONTENTS (fromval),
644 VALUE_TYPE (val) = type;
649 /* Extend a value VAL to COUNT repetitions of its type. */
652 value_repeat (arg1, count)
656 register value_ptr val;
658 if (VALUE_LVAL (arg1) != lval_memory)
659 error ("Only values in memory can be extended with '@'.");
661 error ("Invalid number %d of repetitions.", count);
663 val = allocate_repeat_value (VALUE_TYPE (arg1), count);
665 read_memory (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1),
666 VALUE_CONTENTS_RAW (val),
667 TYPE_LENGTH (VALUE_TYPE (val)));
668 VALUE_LVAL (val) = lval_memory;
669 VALUE_ADDRESS (val) = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1);
675 value_of_variable (var, b)
680 struct frame_info *frame;
683 /* Use selected frame. */
687 frame = block_innermost_frame (b);
688 if (frame == NULL && symbol_read_needs_frame (var))
690 if (BLOCK_FUNCTION (b) != NULL
691 && SYMBOL_NAME (BLOCK_FUNCTION (b)) != NULL)
692 error ("No frame is currently executing in block %s.",
693 SYMBOL_NAME (BLOCK_FUNCTION (b)));
695 error ("No frame is currently executing in specified block");
698 val = read_var_value (var, frame);
700 error ("Address of symbol \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var));
704 /* Given a value which is an array, return a value which is a pointer to its
705 first element, regardless of whether or not the array has a nonzero lower
708 FIXME: A previous comment here indicated that this routine should be
709 substracting the array's lower bound. It's not clear to me that this
710 is correct. Given an array subscripting operation, it would certainly
711 work to do the adjustment here, essentially computing:
713 (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0])
715 However I believe a more appropriate and logical place to account for
716 the lower bound is to do so in value_subscript, essentially computing:
718 (&array[0] + ((index - lowerbound) * sizeof array[0]))
720 As further evidence consider what would happen with operations other
721 than array subscripting, where the caller would get back a value that
722 had an address somewhere before the actual first element of the array,
723 and the information about the lower bound would be lost because of
724 the coercion to pointer type.
728 value_coerce_array (arg1)
731 register struct type *type = check_typedef (VALUE_TYPE (arg1));
733 if (VALUE_LVAL (arg1) != lval_memory)
734 error ("Attempt to take address of value not located in memory.");
736 return value_from_longest (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
737 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
740 /* Given a value which is a function, return a value which is a pointer
744 value_coerce_function (arg1)
748 if (VALUE_LVAL (arg1) != lval_memory)
749 error ("Attempt to take address of value not located in memory.");
751 return value_from_longest (lookup_pointer_type (VALUE_TYPE (arg1)),
752 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
755 /* Return a pointer value for the object for which ARG1 is the contents. */
761 struct type *type = check_typedef (VALUE_TYPE (arg1));
762 if (TYPE_CODE (type) == TYPE_CODE_REF)
764 /* Copy the value, but change the type from (T&) to (T*).
765 We keep the same location information, which is efficient,
766 and allows &(&X) to get the location containing the reference. */
767 value_ptr arg2 = value_copy (arg1);
768 VALUE_TYPE (arg2) = lookup_pointer_type (TYPE_TARGET_TYPE (type));
771 if (TYPE_CODE (type) == TYPE_CODE_FUNC)
772 return value_coerce_function (arg1);
774 if (VALUE_LVAL (arg1) != lval_memory)
775 error ("Attempt to take address of value not located in memory.");
777 return value_from_longest (lookup_pointer_type (VALUE_TYPE (arg1)),
778 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
781 /* Given a value of a pointer type, apply the C unary * operator to it. */
789 type1 = check_typedef (VALUE_TYPE (arg1));
791 if (TYPE_CODE (type1) == TYPE_CODE_MEMBER)
792 error ("not implemented: member types in value_ind");
794 /* Allow * on an integer so we can cast it to whatever we want.
795 This returns an int, which seems like the most C-like thing
796 to do. "long long" variables are rare enough that
797 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
798 if (TYPE_CODE (type1) == TYPE_CODE_INT)
799 return value_at (builtin_type_int,
800 (CORE_ADDR) value_as_long (arg1));
801 else if (TYPE_CODE (type1) == TYPE_CODE_PTR)
802 return value_at_lazy (TYPE_TARGET_TYPE (type1), value_as_pointer (arg1));
803 error ("Attempt to take contents of a non-pointer value.");
804 return 0; /* For lint -- never reached */
807 /* Pushing small parts of stack frames. */
809 /* Push one word (the size of object that a register holds). */
814 unsigned LONGEST word;
816 register int len = REGISTER_SIZE;
817 char buffer[MAX_REGISTER_RAW_SIZE];
819 store_unsigned_integer (buffer, len, word);
822 write_memory (sp, buffer, len);
823 #else /* stack grows upward */
824 write_memory (sp, buffer, len);
826 #endif /* stack grows upward */
831 /* Push LEN bytes with data at BUFFER. */
834 push_bytes (sp, buffer, len)
841 write_memory (sp, buffer, len);
842 #else /* stack grows upward */
843 write_memory (sp, buffer, len);
845 #endif /* stack grows upward */
850 /* Push onto the stack the specified value VALUE. */
854 register CORE_ADDR sp;
857 register int len = TYPE_LENGTH (VALUE_TYPE (arg));
861 write_memory (sp, VALUE_CONTENTS (arg), len);
862 #else /* stack grows upward */
863 write_memory (sp, VALUE_CONTENTS (arg), len);
865 #endif /* stack grows upward */
870 /* Perform the standard coercions that are specified
871 for arguments to be passed to C functions.
873 If PARAM_TYPE is non-NULL, it is the expected parameter type. */
876 value_arg_coerce (arg, param_type)
878 struct type *param_type;
880 register struct type *arg_type = check_typedef (VALUE_TYPE (arg));
881 register struct type *type
882 = param_type ? check_typedef (param_type) : arg_type;
884 switch (TYPE_CODE (type))
887 if (TYPE_CODE (arg_type) != TYPE_CODE_REF)
889 arg = value_addr (arg);
890 VALUE_TYPE (arg) = param_type;
898 if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
899 type = builtin_type_int;
902 /* coerce float to double, unless the function prototype specifies float */
903 if (COERCE_FLOAT_TO_DOUBLE)
905 if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_double))
906 type = builtin_type_double;
907 else if (TYPE_LENGTH (type) > TYPE_LENGTH (builtin_type_double))
908 type = builtin_type_long_double;
912 type = lookup_pointer_type (type);
914 case TYPE_CODE_ARRAY:
915 if (current_language->c_style_arrays)
916 type = lookup_pointer_type (TYPE_TARGET_TYPE (type));
918 case TYPE_CODE_UNDEF:
920 case TYPE_CODE_STRUCT:
921 case TYPE_CODE_UNION:
924 case TYPE_CODE_RANGE:
925 case TYPE_CODE_STRING:
926 case TYPE_CODE_BITSTRING:
927 case TYPE_CODE_ERROR:
928 case TYPE_CODE_MEMBER:
929 case TYPE_CODE_METHOD:
930 case TYPE_CODE_COMPLEX:
935 return value_cast (type, arg);
938 /* Determine a function's address and its return type from its value.
939 Calls error() if the function is not valid for calling. */
942 find_function_addr (function, retval_type)
944 struct type **retval_type;
946 register struct type *ftype = check_typedef (VALUE_TYPE (function));
947 register enum type_code code = TYPE_CODE (ftype);
948 struct type *value_type;
951 /* If it's a member function, just look at the function
954 /* Determine address to call. */
955 if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD)
957 funaddr = VALUE_ADDRESS (function);
958 value_type = TYPE_TARGET_TYPE (ftype);
960 else if (code == TYPE_CODE_PTR)
962 funaddr = value_as_pointer (function);
963 ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
964 if (TYPE_CODE (ftype) == TYPE_CODE_FUNC
965 || TYPE_CODE (ftype) == TYPE_CODE_METHOD)
967 #ifdef CONVERT_FROM_FUNC_PTR_ADDR
968 /* FIXME: This is a workaround for the unusual function
969 pointer representation on the RS/6000, see comment
970 in config/rs6000/tm-rs6000.h */
971 funaddr = CONVERT_FROM_FUNC_PTR_ADDR (funaddr);
973 value_type = TYPE_TARGET_TYPE (ftype);
976 value_type = builtin_type_int;
978 else if (code == TYPE_CODE_INT)
980 /* Handle the case of functions lacking debugging info.
981 Their values are characters since their addresses are char */
982 if (TYPE_LENGTH (ftype) == 1)
983 funaddr = value_as_pointer (value_addr (function));
985 /* Handle integer used as address of a function. */
986 funaddr = (CORE_ADDR) value_as_long (function);
988 value_type = builtin_type_int;
991 error ("Invalid data type for function to be called.");
993 *retval_type = value_type;
997 #if defined (CALL_DUMMY)
998 /* All this stuff with a dummy frame may seem unnecessarily complicated
999 (why not just save registers in GDB?). The purpose of pushing a dummy
1000 frame which looks just like a real frame is so that if you call a
1001 function and then hit a breakpoint (get a signal, etc), "backtrace"
1002 will look right. Whether the backtrace needs to actually show the
1003 stack at the time the inferior function was called is debatable, but
1004 it certainly needs to not display garbage. So if you are contemplating
1005 making dummy frames be different from normal frames, consider that. */
1007 /* Perform a function call in the inferior.
1008 ARGS is a vector of values of arguments (NARGS of them).
1009 FUNCTION is a value, the function to be called.
1010 Returns a value representing what the function returned.
1011 May fail to return, if a breakpoint or signal is hit
1012 during the execution of the function.
1014 ARGS is modified to contain coerced values. */
1017 call_function_by_hand (function, nargs, args)
1022 register CORE_ADDR sp;
1025 /* CALL_DUMMY is an array of words (REGISTER_SIZE), but each word
1026 is in host byte order. Before calling FIX_CALL_DUMMY, we byteswap it
1027 and remove any extra bytes which might exist because unsigned LONGEST is
1028 bigger than REGISTER_SIZE. */
1029 static unsigned LONGEST dummy[] = CALL_DUMMY;
1030 char dummy1[REGISTER_SIZE * sizeof dummy / sizeof (unsigned LONGEST)];
1032 struct type *value_type;
1033 unsigned char struct_return;
1034 CORE_ADDR struct_addr;
1035 struct inferior_status inf_status;
1036 struct cleanup *old_chain;
1040 struct type *ftype = check_typedef (SYMBOL_TYPE (function));
1042 if (!target_has_execution)
1045 save_inferior_status (&inf_status, 1);
1046 old_chain = make_cleanup (restore_inferior_status, &inf_status);
1048 /* PUSH_DUMMY_FRAME is responsible for saving the inferior registers
1049 (and POP_FRAME for restoring them). (At least on most machines)
1050 they are saved on the stack in the inferior. */
1053 old_sp = sp = read_sp ();
1055 #if 1 INNER_THAN 2 /* Stack grows down */
1056 sp -= sizeof dummy1;
1058 #else /* Stack grows up */
1060 sp += sizeof dummy1;
1063 funaddr = find_function_addr (function, &value_type);
1064 CHECK_TYPEDEF (value_type);
1067 struct block *b = block_for_pc (funaddr);
1068 /* If compiled without -g, assume GCC. */
1069 using_gcc = b == NULL ? 0 : BLOCK_GCC_COMPILED (b);
1072 /* Are we returning a value using a structure return or a normal
1075 struct_return = using_struct_return (function, funaddr, value_type,
1078 /* Create a call sequence customized for this function
1079 and the number of arguments for it. */
1080 for (i = 0; i < (int) (sizeof (dummy) / sizeof (dummy[0])); i++)
1081 store_unsigned_integer (&dummy1[i * REGISTER_SIZE],
1083 (unsigned LONGEST)dummy[i]);
1085 #ifdef GDB_TARGET_IS_HPPA
1086 real_pc = FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, args,
1087 value_type, using_gcc);
1089 FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, args,
1090 value_type, using_gcc);
1094 #if CALL_DUMMY_LOCATION == ON_STACK
1095 write_memory (start_sp, (char *)dummy1, sizeof dummy1);
1096 #endif /* On stack. */
1098 #if CALL_DUMMY_LOCATION == BEFORE_TEXT_END
1099 /* Convex Unix prohibits executing in the stack segment. */
1100 /* Hope there is empty room at the top of the text segment. */
1102 extern CORE_ADDR text_end;
1105 for (start_sp = text_end - sizeof dummy1; start_sp < text_end; ++start_sp)
1106 if (read_memory_integer (start_sp, 1) != 0)
1107 error ("text segment full -- no place to put call");
1110 real_pc = text_end - sizeof dummy1;
1111 write_memory (real_pc, (char *)dummy1, sizeof dummy1);
1113 #endif /* Before text_end. */
1115 #if CALL_DUMMY_LOCATION == AFTER_TEXT_END
1117 extern CORE_ADDR text_end;
1121 errcode = target_write_memory (real_pc, (char *)dummy1, sizeof dummy1);
1123 error ("Cannot write text segment -- call_function failed");
1125 #endif /* After text_end. */
1127 #if CALL_DUMMY_LOCATION == AT_ENTRY_POINT
1129 #endif /* At entry point. */
1132 sp = old_sp; /* It really is used, for some ifdef's... */
1135 if (nargs < TYPE_NFIELDS (ftype))
1136 error ("too few arguments in function call");
1138 for (i = nargs - 1; i >= 0; i--)
1140 struct type *param_type;
1141 if (TYPE_NFIELDS (ftype) > i)
1142 param_type = TYPE_FIELD_TYPE (ftype, i);
1145 args[i] = value_arg_coerce (args[i], param_type);
1148 #if defined (REG_STRUCT_HAS_ADDR)
1150 /* This is a machine like the sparc, where we may need to pass a pointer
1151 to the structure, not the structure itself. */
1152 for (i = nargs - 1; i >= 0; i--)
1154 struct type *arg_type = check_typedef (VALUE_TYPE (args[i]));
1155 if ((TYPE_CODE (arg_type) == TYPE_CODE_STRUCT
1156 || TYPE_CODE (arg_type) == TYPE_CODE_UNION
1157 || TYPE_CODE (arg_type) == TYPE_CODE_ARRAY
1158 || TYPE_CODE (arg_type) == TYPE_CODE_STRING
1159 || TYPE_CODE (arg_type) == TYPE_CODE_BITSTRING
1160 || TYPE_CODE (arg_type) == TYPE_CODE_SET
1161 || (TYPE_CODE (arg_type) == TYPE_CODE_FLT
1162 && TYPE_LENGTH (arg_type) > 8)
1164 && REG_STRUCT_HAS_ADDR (using_gcc, arg_type))
1167 int len = TYPE_LENGTH (arg_type);
1169 int aligned_len = STACK_ALIGN (len);
1171 int aligned_len = len;
1173 #if !(1 INNER_THAN 2)
1174 /* The stack grows up, so the address of the thing we push
1175 is the stack pointer before we push it. */
1180 /* Push the structure. */
1181 write_memory (sp, VALUE_CONTENTS (args[i]), len);
1183 /* The stack grows down, so the address of the thing we push
1184 is the stack pointer after we push it. */
1189 /* The value we're going to pass is the address of the thing
1191 args[i] = value_from_longest (lookup_pointer_type (value_type),
1196 #endif /* REG_STRUCT_HAS_ADDR. */
1198 /* Reserve space for the return structure to be written on the
1199 stack, if necessary */
1203 int len = TYPE_LENGTH (value_type);
1205 len = STACK_ALIGN (len);
1217 /* If stack grows down, we must leave a hole at the top. */
1221 for (i = nargs - 1; i >= 0; i--)
1222 len += TYPE_LENGTH (VALUE_TYPE (args[i]));
1223 #ifdef CALL_DUMMY_STACK_ADJUST
1224 len += CALL_DUMMY_STACK_ADJUST;
1227 sp -= STACK_ALIGN (len) - len;
1229 sp += STACK_ALIGN (len) - len;
1232 #endif /* STACK_ALIGN */
1234 #ifdef PUSH_ARGUMENTS
1235 PUSH_ARGUMENTS(nargs, args, sp, struct_return, struct_addr);
1236 #else /* !PUSH_ARGUMENTS */
1237 for (i = nargs - 1; i >= 0; i--)
1238 sp = value_push (sp, args[i]);
1239 #endif /* !PUSH_ARGUMENTS */
1241 #ifdef CALL_DUMMY_STACK_ADJUST
1243 sp -= CALL_DUMMY_STACK_ADJUST;
1245 sp += CALL_DUMMY_STACK_ADJUST;
1247 #endif /* CALL_DUMMY_STACK_ADJUST */
1249 /* Store the address at which the structure is supposed to be
1250 written. Note that this (and the code which reserved the space
1251 above) assumes that gcc was used to compile this function. Since
1252 it doesn't cost us anything but space and if the function is pcc
1253 it will ignore this value, we will make that assumption.
1255 Also note that on some machines (like the sparc) pcc uses a
1256 convention like gcc's. */
1259 STORE_STRUCT_RETURN (struct_addr, sp);
1261 /* Write the stack pointer. This is here because the statements above
1262 might fool with it. On SPARC, this write also stores the register
1263 window into the right place in the new stack frame, which otherwise
1264 wouldn't happen. (See store_inferior_registers in sparc-nat.c.) */
1268 char retbuf[REGISTER_BYTES];
1270 struct symbol *symbol;
1273 symbol = find_pc_function (funaddr);
1276 name = SYMBOL_SOURCE_NAME (symbol);
1280 /* Try the minimal symbols. */
1281 struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (funaddr);
1285 name = SYMBOL_SOURCE_NAME (msymbol);
1291 sprintf (format, "at %s", local_hex_format ());
1293 /* FIXME-32x64: assumes funaddr fits in a long. */
1294 sprintf (name, format, (unsigned long) funaddr);
1297 /* Execute the stack dummy routine, calling FUNCTION.
1298 When it is done, discard the empty frame
1299 after storing the contents of all regs into retbuf. */
1300 if (run_stack_dummy (real_pc + CALL_DUMMY_START_OFFSET, retbuf))
1302 /* We stopped somewhere besides the call dummy. */
1304 /* If we did the cleanups, we would print a spurious error message
1305 (Unable to restore previously selected frame), would write the
1306 registers from the inf_status (which is wrong), and would do other
1307 wrong things (like set stop_bpstat to the wrong thing). */
1308 discard_cleanups (old_chain);
1309 /* Prevent memory leak. */
1310 bpstat_clear (&inf_status.stop_bpstat);
1312 /* The following error message used to say "The expression
1313 which contained the function call has been discarded." It
1314 is a hard concept to explain in a few words. Ideally, GDB
1315 would be able to resume evaluation of the expression when
1316 the function finally is done executing. Perhaps someday
1317 this will be implemented (it would not be easy). */
1319 /* FIXME: Insert a bunch of wrap_here; name can be very long if it's
1320 a C++ name with arguments and stuff. */
1322 The program being debugged stopped while in a function called from GDB.\n\
1323 When the function (%s) is done executing, GDB will silently\n\
1324 stop (instead of continuing to evaluate the expression containing\n\
1325 the function call).", name);
1328 do_cleanups (old_chain);
1330 /* Figure out the value returned by the function. */
1331 return value_being_returned (value_type, retbuf, struct_return);
1334 #else /* no CALL_DUMMY. */
1336 call_function_by_hand (function, nargs, args)
1341 error ("Cannot invoke functions on this machine.");
1343 #endif /* no CALL_DUMMY. */
1346 /* Create a value for an array by allocating space in the inferior, copying
1347 the data into that space, and then setting up an array value.
1349 The array bounds are set from LOWBOUND and HIGHBOUND, and the array is
1350 populated from the values passed in ELEMVEC.
1352 The element type of the array is inherited from the type of the
1353 first element, and all elements must have the same size (though we
1354 don't currently enforce any restriction on their types). */
1357 value_array (lowbound, highbound, elemvec)
1364 unsigned int typelength;
1366 struct type *rangetype;
1367 struct type *arraytype;
1370 /* Validate that the bounds are reasonable and that each of the elements
1371 have the same size. */
1373 nelem = highbound - lowbound + 1;
1376 error ("bad array bounds (%d, %d)", lowbound, highbound);
1378 typelength = TYPE_LENGTH (VALUE_TYPE (elemvec[0]));
1379 for (idx = 1; idx < nelem; idx++)
1381 if (TYPE_LENGTH (VALUE_TYPE (elemvec[idx])) != typelength)
1383 error ("array elements must all be the same size");
1387 rangetype = create_range_type ((struct type *) NULL, builtin_type_int,
1388 lowbound, highbound);
1389 arraytype = create_array_type ((struct type *) NULL,
1390 VALUE_TYPE (elemvec[0]), rangetype);
1392 if (!current_language->c_style_arrays)
1394 val = allocate_value (arraytype);
1395 for (idx = 0; idx < nelem; idx++)
1397 memcpy (VALUE_CONTENTS_RAW (val) + (idx * typelength),
1398 VALUE_CONTENTS (elemvec[idx]),
1404 /* Allocate space to store the array in the inferior, and then initialize
1405 it by copying in each element. FIXME: Is it worth it to create a
1406 local buffer in which to collect each value and then write all the
1407 bytes in one operation? */
1409 addr = allocate_space_in_inferior (nelem * typelength);
1410 for (idx = 0; idx < nelem; idx++)
1412 write_memory (addr + (idx * typelength), VALUE_CONTENTS (elemvec[idx]),
1416 /* Create the array type and set up an array value to be evaluated lazily. */
1418 val = value_at_lazy (arraytype, addr);
1422 /* Create a value for a string constant by allocating space in the inferior,
1423 copying the data into that space, and returning the address with type
1424 TYPE_CODE_STRING. PTR points to the string constant data; LEN is number
1426 Note that string types are like array of char types with a lower bound of
1427 zero and an upper bound of LEN - 1. Also note that the string may contain
1428 embedded null bytes. */
1431 value_string (ptr, len)
1436 int lowbound = current_language->string_lower_bound;
1437 struct type *rangetype = create_range_type ((struct type *) NULL,
1439 lowbound, len + lowbound - 1);
1440 struct type *stringtype
1441 = create_string_type ((struct type *) NULL, rangetype);
1444 if (current_language->c_style_arrays == 0)
1446 val = allocate_value (stringtype);
1447 memcpy (VALUE_CONTENTS_RAW (val), ptr, len);
1452 /* Allocate space to store the string in the inferior, and then
1453 copy LEN bytes from PTR in gdb to that address in the inferior. */
1455 addr = allocate_space_in_inferior (len);
1456 write_memory (addr, ptr, len);
1458 val = value_at_lazy (stringtype, addr);
1463 value_bitstring (ptr, len)
1468 struct type *domain_type = create_range_type (NULL, builtin_type_int,
1470 struct type *type = create_set_type ((struct type*) NULL, domain_type);
1471 TYPE_CODE (type) = TYPE_CODE_BITSTRING;
1472 val = allocate_value (type);
1473 memcpy (VALUE_CONTENTS_RAW (val), ptr, TYPE_LENGTH (type));
1477 /* See if we can pass arguments in T2 to a function which takes arguments
1478 of types T1. Both t1 and t2 are NULL-terminated vectors. If some
1479 arguments need coercion of some sort, then the coerced values are written
1480 into T2. Return value is 0 if the arguments could be matched, or the
1481 position at which they differ if not.
1483 STATICP is nonzero if the T1 argument list came from a
1484 static member function.
1486 For non-static member functions, we ignore the first argument,
1487 which is the type of the instance variable. This is because we want
1488 to handle calls with objects from derived classes. This is not
1489 entirely correct: we should actually check to make sure that a
1490 requested operation is type secure, shouldn't we? FIXME. */
1493 typecmp (staticp, t1, t2)
1502 if (staticp && t1 == 0)
1506 if (TYPE_CODE (t1[0]) == TYPE_CODE_VOID) return 0;
1507 if (t1[!staticp] == 0) return 0;
1508 for (i = !staticp; t1[i] && TYPE_CODE (t1[i]) != TYPE_CODE_VOID; i++)
1510 struct type *tt1, *tt2;
1513 tt1 = check_typedef (t1[i]);
1514 tt2 = check_typedef (VALUE_TYPE(t2[i]));
1515 if (TYPE_CODE (tt1) == TYPE_CODE_REF
1516 /* We should be doing hairy argument matching, as below. */
1517 && (TYPE_CODE (check_typedef (TYPE_TARGET_TYPE (tt1))) == TYPE_CODE (tt2)))
1519 if (TYPE_CODE (tt2) == TYPE_CODE_ARRAY)
1520 t2[i] = value_coerce_array (t2[i]);
1522 t2[i] = value_addr (t2[i]);
1526 while (TYPE_CODE (tt1) == TYPE_CODE_PTR
1527 && ( TYPE_CODE (tt2) == TYPE_CODE_ARRAY
1528 || TYPE_CODE (tt2) == TYPE_CODE_PTR))
1530 tt1 = check_typedef (TYPE_TARGET_TYPE(tt1));
1531 tt2 = check_typedef (TYPE_TARGET_TYPE(tt2));
1533 if (TYPE_CODE(tt1) == TYPE_CODE(tt2)) continue;
1534 /* Array to pointer is a `trivial conversion' according to the ARM. */
1536 /* We should be doing much hairier argument matching (see section 13.2
1537 of the ARM), but as a quick kludge, just check for the same type
1539 if (TYPE_CODE (t1[i]) != TYPE_CODE (VALUE_TYPE (t2[i])))
1542 if (!t1[i]) return 0;
1543 return t2[i] ? i+1 : 0;
1546 /* Helper function used by value_struct_elt to recurse through baseclasses.
1547 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1548 and search in it assuming it has (class) type TYPE.
1549 If found, return value, else return NULL.
1551 If LOOKING_FOR_BASECLASS, then instead of looking for struct fields,
1552 look for a baseclass named NAME. */
1555 search_struct_field (name, arg1, offset, type, looking_for_baseclass)
1557 register value_ptr arg1;
1559 register struct type *type;
1560 int looking_for_baseclass;
1564 CHECK_TYPEDEF (type);
1566 if (! looking_for_baseclass)
1567 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
1569 char *t_field_name = TYPE_FIELD_NAME (type, i);
1571 if (t_field_name && STREQ (t_field_name, name))
1574 if (TYPE_FIELD_STATIC (type, i))
1576 char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, i);
1577 struct symbol *sym =
1578 lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL);
1580 error ("Internal error: could not find physical static variable named %s",
1582 v = value_at (TYPE_FIELD_TYPE (type, i),
1583 (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym));
1586 v = value_primitive_field (arg1, offset, i, type);
1588 error("there is no field named %s", name);
1593 && (t_field_name[0] == '\0'
1594 || (TYPE_CODE (type) == TYPE_CODE_UNION
1595 && STREQ (t_field_name, "else"))))
1597 struct type *field_type = TYPE_FIELD_TYPE (type, i);
1598 if (TYPE_CODE (field_type) == TYPE_CODE_UNION
1599 || TYPE_CODE (field_type) == TYPE_CODE_STRUCT)
1601 /* Look for a match through the fields of an anonymous union,
1602 or anonymous struct. C++ provides anonymous unions.
1604 In the GNU Chill implementation of variant record types,
1605 each <alternative field> has an (anonymous) union type,
1606 each member of the union represents a <variant alternative>.
1607 Each <variant alternative> is represented as a struct,
1608 with a member for each <variant field>. */
1611 int new_offset = offset;
1613 /* This is pretty gross. In G++, the offset in an anonymous
1614 union is relative to the beginning of the enclosing struct.
1615 In the GNU Chill implementation of variant records,
1616 the bitpos is zero in an anonymous union field, so we
1617 have to add the offset of the union here. */
1618 if (TYPE_CODE (field_type) == TYPE_CODE_STRUCT
1619 || (TYPE_NFIELDS (field_type) > 0
1620 && TYPE_FIELD_BITPOS (field_type, 0) == 0))
1621 new_offset += TYPE_FIELD_BITPOS (type, i) / 8;
1623 v = search_struct_field (name, arg1, new_offset, field_type,
1624 looking_for_baseclass);
1631 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1634 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
1635 /* If we are looking for baseclasses, this is what we get when we
1636 hit them. But it could happen that the base part's member name
1637 is not yet filled in. */
1638 int found_baseclass = (looking_for_baseclass
1639 && TYPE_BASECLASS_NAME (type, i) != NULL
1640 && STREQ (name, TYPE_BASECLASS_NAME (type, i)));
1642 if (BASETYPE_VIA_VIRTUAL (type, i))
1644 int boffset = VALUE_OFFSET (arg1) + offset;
1645 boffset = baseclass_offset (type, i,
1646 VALUE_CONTENTS (arg1) + boffset,
1647 VALUE_ADDRESS (arg1) + boffset);
1649 error ("virtual baseclass botch");
1650 if (found_baseclass)
1652 value_ptr v2 = allocate_value (basetype);
1653 VALUE_LVAL (v2) = VALUE_LVAL (arg1);
1654 VALUE_ADDRESS (v2) = VALUE_ADDRESS (arg1);
1655 VALUE_OFFSET (v2) = VALUE_OFFSET (arg1) + offset + boffset;
1656 if (VALUE_LAZY (arg1))
1657 VALUE_LAZY (v2) = 1;
1659 memcpy (VALUE_CONTENTS_RAW (v2),
1660 VALUE_CONTENTS_RAW (arg1) + offset + boffset,
1661 TYPE_LENGTH (basetype));
1664 v = search_struct_field (name, arg1, offset + boffset,
1665 TYPE_BASECLASS (type, i),
1666 looking_for_baseclass);
1668 else if (found_baseclass)
1669 v = value_primitive_field (arg1, offset, i, type);
1671 v = search_struct_field (name, arg1,
1672 offset + TYPE_BASECLASS_BITPOS (type, i) / 8,
1673 basetype, looking_for_baseclass);
1679 /* Helper function used by value_struct_elt to recurse through baseclasses.
1680 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1681 and search in it assuming it has (class) type TYPE.
1682 If found, return value, else if name matched and args not return (value)-1,
1683 else return NULL. */
1686 search_struct_method (name, arg1p, args, offset, static_memfuncp, type)
1688 register value_ptr *arg1p, *args;
1689 int offset, *static_memfuncp;
1690 register struct type *type;
1694 int name_matched = 0;
1695 char dem_opname[64];
1697 CHECK_TYPEDEF (type);
1698 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
1700 char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
1701 if (strncmp(t_field_name, "__", 2)==0 ||
1702 strncmp(t_field_name, "op", 2)==0 ||
1703 strncmp(t_field_name, "type", 4)==0 )
1705 if (cplus_demangle_opname(t_field_name, dem_opname, DMGL_ANSI))
1706 t_field_name = dem_opname;
1707 else if (cplus_demangle_opname(t_field_name, dem_opname, 0))
1708 t_field_name = dem_opname;
1710 if (t_field_name && STREQ (t_field_name, name))
1712 int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1;
1713 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1716 if (j > 0 && args == 0)
1717 error ("cannot resolve overloaded method `%s'", name);
1720 if (TYPE_FN_FIELD_STUB (f, j))
1721 check_stub_method (type, i, j);
1722 if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j),
1723 TYPE_FN_FIELD_ARGS (f, j), args))
1725 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1726 return value_virtual_fn_field (arg1p, f, j, type, offset);
1727 if (TYPE_FN_FIELD_STATIC_P (f, j) && static_memfuncp)
1728 *static_memfuncp = 1;
1729 v = value_fn_field (arg1p, f, j, type, offset);
1730 if (v != NULL) return v;
1737 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1741 if (BASETYPE_VIA_VIRTUAL (type, i))
1743 base_offset = VALUE_OFFSET (*arg1p) + offset;
1745 baseclass_offset (type, i,
1746 VALUE_CONTENTS (*arg1p) + base_offset,
1747 VALUE_ADDRESS (*arg1p) + base_offset);
1748 if (base_offset == -1)
1749 error ("virtual baseclass botch");
1753 base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
1755 v = search_struct_method (name, arg1p, args, base_offset + offset,
1756 static_memfuncp, TYPE_BASECLASS (type, i));
1757 if (v == (value_ptr) -1)
1763 /* FIXME-bothner: Why is this commented out? Why is it here? */
1764 /* *arg1p = arg1_tmp;*/
1768 if (name_matched) return (value_ptr) -1;
1772 /* Given *ARGP, a value of type (pointer to a)* structure/union,
1773 extract the component named NAME from the ultimate target structure/union
1774 and return it as a value with its appropriate type.
1775 ERR is used in the error message if *ARGP's type is wrong.
1777 C++: ARGS is a list of argument types to aid in the selection of
1778 an appropriate method. Also, handle derived types.
1780 STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
1781 where the truthvalue of whether the function that was resolved was
1782 a static member function or not is stored.
1784 ERR is an error message to be printed in case the field is not found. */
1787 value_struct_elt (argp, args, name, static_memfuncp, err)
1788 register value_ptr *argp, *args;
1790 int *static_memfuncp;
1793 register struct type *t;
1796 COERCE_ARRAY (*argp);
1798 t = check_typedef (VALUE_TYPE (*argp));
1800 /* Follow pointers until we get to a non-pointer. */
1802 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1804 *argp = value_ind (*argp);
1805 /* Don't coerce fn pointer to fn and then back again! */
1806 if (TYPE_CODE (VALUE_TYPE (*argp)) != TYPE_CODE_FUNC)
1807 COERCE_ARRAY (*argp);
1808 t = check_typedef (VALUE_TYPE (*argp));
1811 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1812 error ("not implemented: member type in value_struct_elt");
1814 if ( TYPE_CODE (t) != TYPE_CODE_STRUCT
1815 && TYPE_CODE (t) != TYPE_CODE_UNION)
1816 error ("Attempt to extract a component of a value that is not a %s.", err);
1818 /* Assume it's not, unless we see that it is. */
1819 if (static_memfuncp)
1820 *static_memfuncp =0;
1824 /* if there are no arguments ...do this... */
1826 /* Try as a field first, because if we succeed, there
1827 is less work to be done. */
1828 v = search_struct_field (name, *argp, 0, t, 0);
1832 /* C++: If it was not found as a data field, then try to
1833 return it as a pointer to a method. */
1835 if (destructor_name_p (name, t))
1836 error ("Cannot get value of destructor");
1838 v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
1840 if (v == (value_ptr) -1)
1841 error ("Cannot take address of a method");
1844 if (TYPE_NFN_FIELDS (t))
1845 error ("There is no member or method named %s.", name);
1847 error ("There is no member named %s.", name);
1852 if (destructor_name_p (name, t))
1856 /* destructors are a special case. */
1857 v = value_fn_field (NULL, TYPE_FN_FIELDLIST1 (t, 0),
1858 TYPE_FN_FIELDLIST_LENGTH (t, 0), 0, 0);
1859 if (!v) error("could not find destructor function named %s.", name);
1864 error ("destructor should not have any argument");
1868 v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
1870 if (v == (value_ptr) -1)
1872 error("Argument list of %s mismatch with component in the structure.", name);
1876 /* See if user tried to invoke data as function. If so,
1877 hand it back. If it's not callable (i.e., a pointer to function),
1878 gdb should give an error. */
1879 v = search_struct_field (name, *argp, 0, t, 0);
1883 error ("Structure has no component named %s.", name);
1887 /* C++: return 1 is NAME is a legitimate name for the destructor
1888 of type TYPE. If TYPE does not have a destructor, or
1889 if NAME is inappropriate for TYPE, an error is signaled. */
1891 destructor_name_p (name, type)
1893 const struct type *type;
1895 /* destructors are a special case. */
1899 char *dname = type_name_no_tag (type);
1900 char *cp = strchr (dname, '<');
1903 /* Do not compare the template part for template classes. */
1905 len = strlen (dname);
1908 if (strlen (name + 1) != len || !STREQN (dname, name + 1, len))
1909 error ("name of destructor must equal name of class");
1916 /* Helper function for check_field: Given TYPE, a structure/union,
1917 return 1 if the component named NAME from the ultimate
1918 target structure/union is defined, otherwise, return 0. */
1921 check_field_in (type, name)
1922 register struct type *type;
1927 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
1929 char *t_field_name = TYPE_FIELD_NAME (type, i);
1930 if (t_field_name && STREQ (t_field_name, name))
1934 /* C++: If it was not found as a data field, then try to
1935 return it as a pointer to a method. */
1937 /* Destructors are a special case. */
1938 if (destructor_name_p (name, type))
1941 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
1943 if (STREQ (TYPE_FN_FIELDLIST_NAME (type, i), name))
1947 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1948 if (check_field_in (TYPE_BASECLASS (type, i), name))
1955 /* C++: Given ARG1, a value of type (pointer to a)* structure/union,
1956 return 1 if the component named NAME from the ultimate
1957 target structure/union is defined, otherwise, return 0. */
1960 check_field (arg1, name)
1961 register value_ptr arg1;
1964 register struct type *t;
1966 COERCE_ARRAY (arg1);
1968 t = VALUE_TYPE (arg1);
1970 /* Follow pointers until we get to a non-pointer. */
1975 if (TYPE_CODE (t) != TYPE_CODE_PTR && TYPE_CODE (t) != TYPE_CODE_REF)
1977 t = TYPE_TARGET_TYPE (t);
1980 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1981 error ("not implemented: member type in check_field");
1983 if ( TYPE_CODE (t) != TYPE_CODE_STRUCT
1984 && TYPE_CODE (t) != TYPE_CODE_UNION)
1985 error ("Internal error: `this' is not an aggregate");
1987 return check_field_in (t, name);
1990 /* C++: Given an aggregate type CURTYPE, and a member name NAME,
1991 return the address of this member as a "pointer to member"
1992 type. If INTYPE is non-null, then it will be the type
1993 of the member we are looking for. This will help us resolve
1994 "pointers to member functions". This function is used
1995 to resolve user expressions of the form "DOMAIN::NAME". */
1998 value_struct_elt_for_reference (domain, offset, curtype, name, intype)
1999 struct type *domain, *curtype, *intype;
2003 register struct type *t = curtype;
2007 if ( TYPE_CODE (t) != TYPE_CODE_STRUCT
2008 && TYPE_CODE (t) != TYPE_CODE_UNION)
2009 error ("Internal error: non-aggregate type to value_struct_elt_for_reference");
2011 for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
2013 char *t_field_name = TYPE_FIELD_NAME (t, i);
2015 if (t_field_name && STREQ (t_field_name, name))
2017 if (TYPE_FIELD_STATIC (t, i))
2019 char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (t, i);
2020 struct symbol *sym =
2021 lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL);
2023 error ("Internal error: could not find physical static variable named %s",
2025 return value_at (SYMBOL_TYPE (sym),
2026 (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym));
2028 if (TYPE_FIELD_PACKED (t, i))
2029 error ("pointers to bitfield members not allowed");
2031 return value_from_longest
2032 (lookup_reference_type (lookup_member_type (TYPE_FIELD_TYPE (t, i),
2034 offset + (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3));
2038 /* C++: If it was not found as a data field, then try to
2039 return it as a pointer to a method. */
2041 /* Destructors are a special case. */
2042 if (destructor_name_p (name, t))
2044 error ("member pointers to destructors not implemented yet");
2047 /* Perform all necessary dereferencing. */
2048 while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR)
2049 intype = TYPE_TARGET_TYPE (intype);
2051 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
2053 char *t_field_name = TYPE_FN_FIELDLIST_NAME (t, i);
2054 char dem_opname[64];
2056 if (strncmp(t_field_name, "__", 2)==0 ||
2057 strncmp(t_field_name, "op", 2)==0 ||
2058 strncmp(t_field_name, "type", 4)==0 )
2060 if (cplus_demangle_opname(t_field_name, dem_opname, DMGL_ANSI))
2061 t_field_name = dem_opname;
2062 else if (cplus_demangle_opname(t_field_name, dem_opname, 0))
2063 t_field_name = dem_opname;
2065 if (t_field_name && STREQ (t_field_name, name))
2067 int j = TYPE_FN_FIELDLIST_LENGTH (t, i);
2068 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
2070 if (intype == 0 && j > 1)
2071 error ("non-unique member `%s' requires type instantiation", name);
2075 if (TYPE_FN_FIELD_TYPE (f, j) == intype)
2078 error ("no member function matches that type instantiation");
2083 if (TYPE_FN_FIELD_STUB (f, j))
2084 check_stub_method (t, i, j);
2085 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
2087 return value_from_longest
2088 (lookup_reference_type
2089 (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
2091 (LONGEST) METHOD_PTR_FROM_VOFFSET (TYPE_FN_FIELD_VOFFSET (f, j)));
2095 struct symbol *s = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
2096 0, VAR_NAMESPACE, 0, NULL);
2103 v = read_var_value (s, 0);
2105 VALUE_TYPE (v) = lookup_reference_type
2106 (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
2114 for (i = TYPE_N_BASECLASSES (t) - 1; i >= 0; i--)
2119 if (BASETYPE_VIA_VIRTUAL (t, i))
2122 base_offset = TYPE_BASECLASS_BITPOS (t, i) / 8;
2123 v = value_struct_elt_for_reference (domain,
2124 offset + base_offset,
2125 TYPE_BASECLASS (t, i),
2134 /* C++: return the value of the class instance variable, if one exists.
2135 Flag COMPLAIN signals an error if the request is made in an
2136 inappropriate context. */
2139 value_of_this (complain)
2142 struct symbol *func, *sym;
2145 static const char funny_this[] = "this";
2148 if (selected_frame == 0)
2150 error ("no frame selected");
2153 func = get_frame_function (selected_frame);
2157 error ("no `this' in nameless context");
2161 b = SYMBOL_BLOCK_VALUE (func);
2162 i = BLOCK_NSYMS (b);
2165 error ("no args, no `this'");
2168 /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
2169 symbol instead of the LOC_ARG one (if both exist). */
2170 sym = lookup_block_symbol (b, funny_this, VAR_NAMESPACE);
2174 error ("current stack frame not in method");
2179 this = read_var_value (sym, selected_frame);
2180 if (this == 0 && complain)
2181 error ("`this' argument at unknown address");
2185 /* Create a slice (sub-string, sub-array) of ARRAY, that is LENGTH elements
2186 long, starting at LOWBOUND. The result has the same lower bound as
2187 the original ARRAY. */
2190 value_slice (array, lowbound, length)
2192 int lowbound, length;
2194 struct type *slice_range_type, *slice_type, *range_type;
2195 LONGEST lowerbound, upperbound, offset;
2197 struct type *array_type;
2198 array_type = check_typedef (VALUE_TYPE (array));
2199 COERCE_VARYING_ARRAY (array, array_type);
2200 if (TYPE_CODE (array_type) != TYPE_CODE_ARRAY
2201 && TYPE_CODE (array_type) != TYPE_CODE_STRING
2202 && TYPE_CODE (array_type) != TYPE_CODE_BITSTRING)
2203 error ("cannot take slice of non-array");
2204 range_type = TYPE_INDEX_TYPE (array_type);
2205 if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0)
2206 error ("slice from bad array or bitstring");
2207 if (lowbound < lowerbound || length < 0
2208 || lowbound + length - 1 > upperbound
2209 /* Chill allows zero-length strings but not arrays. */
2210 || (current_language->la_language == language_chill
2211 && length == 0 && TYPE_CODE (array_type) == TYPE_CODE_ARRAY))
2212 error ("slice out of range");
2213 /* FIXME-type-allocation: need a way to free this type when we are
2215 slice_range_type = create_range_type ((struct type*) NULL,
2216 TYPE_TARGET_TYPE (range_type),
2217 lowerbound, lowerbound + length - 1);
2218 if (TYPE_CODE (array_type) == TYPE_CODE_BITSTRING)
2221 slice_type = create_set_type ((struct type*) NULL, slice_range_type);
2222 TYPE_CODE (slice_type) = TYPE_CODE_BITSTRING;
2223 slice = value_zero (slice_type, not_lval);
2224 for (i = 0; i < length; i++)
2226 int element = value_bit_index (array_type,
2227 VALUE_CONTENTS (array),
2230 error ("internal error accessing bitstring");
2231 else if (element > 0)
2233 int j = i % TARGET_CHAR_BIT;
2234 if (BITS_BIG_ENDIAN)
2235 j = TARGET_CHAR_BIT - 1 - j;
2236 VALUE_CONTENTS_RAW (slice)[i / TARGET_CHAR_BIT] |= (1 << j);
2239 /* We should set the address, bitssize, and bitspos, so the clice
2240 can be used on the LHS, but that may require extensions to
2241 value_assign. For now, just leave as a non_lval. FIXME. */
2245 struct type *element_type = TYPE_TARGET_TYPE (array_type);
2247 = (lowbound - lowerbound) * TYPE_LENGTH (check_typedef (element_type));
2248 slice_type = create_array_type ((struct type*) NULL, element_type,
2250 TYPE_CODE (slice_type) = TYPE_CODE (array_type);
2251 slice = allocate_value (slice_type);
2252 if (VALUE_LAZY (array))
2253 VALUE_LAZY (slice) = 1;
2255 memcpy (VALUE_CONTENTS (slice), VALUE_CONTENTS (array) + offset,
2256 TYPE_LENGTH (slice_type));
2257 if (VALUE_LVAL (array) == lval_internalvar)
2258 VALUE_LVAL (slice) = lval_internalvar_component;
2260 VALUE_LVAL (slice) = VALUE_LVAL (array);
2261 VALUE_ADDRESS (slice) = VALUE_ADDRESS (array);
2262 VALUE_OFFSET (slice) = VALUE_OFFSET (array) + offset;
2267 /* Assuming chill_varying_type (VARRAY) is true, return an equivalent
2268 value as a fixed-length array. */
2271 varying_to_slice (varray)
2274 struct type *vtype = check_typedef (VALUE_TYPE (varray));
2275 LONGEST length = unpack_long (TYPE_FIELD_TYPE (vtype, 0),
2276 VALUE_CONTENTS (varray)
2277 + TYPE_FIELD_BITPOS (vtype, 0) / 8);
2278 return value_slice (value_primitive_field (varray, 0, 1, vtype), 0, length);
2281 /* Create a value for a FORTRAN complex number. Currently most of
2282 the time values are coerced to COMPLEX*16 (i.e. a complex number
2283 composed of 2 doubles. This really should be a smarter routine
2284 that figures out precision inteligently as opposed to assuming
2285 doubles. FIXME: fmb */
2288 value_literal_complex (arg1, arg2, type)
2293 register value_ptr val;
2294 struct type *real_type = TYPE_TARGET_TYPE (type);
2296 val = allocate_value (type);
2297 arg1 = value_cast (real_type, arg1);
2298 arg2 = value_cast (real_type, arg2);
2300 memcpy (VALUE_CONTENTS_RAW (val),
2301 VALUE_CONTENTS (arg1), TYPE_LENGTH (real_type));
2302 memcpy (VALUE_CONTENTS_RAW (val) + TYPE_LENGTH (real_type),
2303 VALUE_CONTENTS (arg2), TYPE_LENGTH (real_type));
2307 /* Cast a value into the appropriate complex data type. */
2310 cast_into_complex (type, val)
2312 register value_ptr val;
2314 struct type *real_type = TYPE_TARGET_TYPE (type);
2315 if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_COMPLEX)
2317 struct type *val_real_type = TYPE_TARGET_TYPE (VALUE_TYPE (val));
2318 value_ptr re_val = allocate_value (val_real_type);
2319 value_ptr im_val = allocate_value (val_real_type);
2321 memcpy (VALUE_CONTENTS_RAW (re_val),
2322 VALUE_CONTENTS (val), TYPE_LENGTH (val_real_type));
2323 memcpy (VALUE_CONTENTS_RAW (im_val),
2324 VALUE_CONTENTS (val) + TYPE_LENGTH (val_real_type),
2325 TYPE_LENGTH (val_real_type));
2327 return value_literal_complex (re_val, im_val, type);
2329 else if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FLT
2330 || TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_INT)
2331 return value_literal_complex (val, value_zero (real_type, not_lval), type);
2333 error ("cannot cast non-number to complex");
2337 _initialize_valops ()
2341 (add_set_cmd ("abandon", class_support, var_boolean, (char *)&auto_abandon,
2342 "Set automatic abandonment of expressions upon failure.",