1 /* Perform non-arithmetic operations on values, for GDB.
2 Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
35 /* Local functions. */
37 static int typecmp
PARAMS ((int staticp
, struct type
*t1
[], value_ptr t2
[]));
39 static CORE_ADDR find_function_addr
PARAMS ((value_ptr
, struct type
**));
41 static CORE_ADDR value_push
PARAMS ((CORE_ADDR
, value_ptr
));
43 static value_ptr search_struct_field
PARAMS ((char *, value_ptr
, int,
46 static value_ptr search_struct_method
PARAMS ((char *, value_ptr
*,
48 int, int *, struct type
*));
50 static int check_field_in
PARAMS ((struct type
*, const char *));
52 static CORE_ADDR allocate_space_in_inferior
PARAMS ((int));
54 static value_ptr cast_into_complex
PARAMS ((struct type
*, value_ptr
));
56 #define VALUE_SUBSTRING_START(VAL) VALUE_FRAME(VAL)
59 /* Allocate NBYTES of space in the inferior using the inferior's malloc
60 and return a value that is a pointer to the allocated space. */
63 allocate_space_in_inferior (len
)
66 register value_ptr val
;
67 register struct symbol
*sym
;
68 struct minimal_symbol
*msymbol
;
73 /* Find the address of malloc in the inferior. */
75 sym
= lookup_symbol ("malloc", 0, VAR_NAMESPACE
, 0, NULL
);
78 if (SYMBOL_CLASS (sym
) != LOC_BLOCK
)
80 error ("\"malloc\" exists in this program but is not a function.");
82 val
= value_of_variable (sym
, NULL
);
86 msymbol
= lookup_minimal_symbol ("malloc", NULL
, NULL
);
89 type
= lookup_pointer_type (builtin_type_char
);
90 type
= lookup_function_type (type
);
91 type
= lookup_pointer_type (type
);
92 maddr
= (LONGEST
) SYMBOL_VALUE_ADDRESS (msymbol
);
93 val
= value_from_longest (type
, maddr
);
97 error ("evaluation of this expression requires the program to have a function \"malloc\".");
101 blocklen
= value_from_longest (builtin_type_int
, (LONGEST
) len
);
102 val
= call_function_by_hand (val
, 1, &blocklen
);
103 if (value_logical_not (val
))
105 error ("No memory available to program.");
107 return (value_as_long (val
));
110 /* Cast value ARG2 to type TYPE and return as a value.
111 More general than a C cast: accepts any two types of the same length,
112 and if ARG2 is an lvalue it can be cast into anything at all. */
113 /* In C++, casts may change pointer or object representations. */
116 value_cast (type
, arg2
)
118 register value_ptr arg2
;
120 register enum type_code code1
= TYPE_CODE (type
);
121 register enum type_code code2
;
124 if (VALUE_TYPE (arg2
) == type
)
129 /* A cast to an undetermined-length array_type, such as (TYPE [])OBJECT,
130 is treated like a cast to (TYPE [N])OBJECT,
131 where N is sizeof(OBJECT)/sizeof(TYPE). */
132 if (code1
== TYPE_CODE_ARRAY
133 && TYPE_LENGTH (TYPE_TARGET_TYPE (type
)) > 0
134 && TYPE_ARRAY_UPPER_BOUND_TYPE (type
) == BOUND_CANNOT_BE_DETERMINED
)
136 struct type
*element_type
= TYPE_TARGET_TYPE (type
);
137 struct type
*range_type
= TYPE_INDEX_TYPE (type
);
138 int low_bound
= TYPE_LOW_BOUND (range_type
);
139 int val_length
= TYPE_LENGTH (VALUE_TYPE (arg2
));
140 int new_length
= val_length
/ TYPE_LENGTH (element_type
);
141 if (val_length
% TYPE_LENGTH (element_type
) != 0)
142 warning("array element type size does not divide object size in cast");
143 range_type
= create_range_type ((struct type
*) NULL
,
144 TYPE_TARGET_TYPE (range_type
),
145 low_bound
, new_length
+ low_bound
- 1);
146 VALUE_TYPE (arg2
) = create_array_type ((struct type
*) NULL
,
147 element_type
, range_type
);
151 if (current_language
->c_style_arrays
152 && (VALUE_REPEATED (arg2
)
153 || TYPE_CODE (VALUE_TYPE (arg2
)) == TYPE_CODE_ARRAY
))
154 arg2
= value_coerce_array (arg2
);
156 if (TYPE_CODE (VALUE_TYPE (arg2
)) == TYPE_CODE_FUNC
)
157 arg2
= value_coerce_function (arg2
);
159 COERCE_VARYING_ARRAY (arg2
);
161 code2
= TYPE_CODE (VALUE_TYPE (arg2
));
163 if (code1
== TYPE_CODE_COMPLEX
)
164 return cast_into_complex (type
, arg2
);
165 if (code1
== TYPE_CODE_BOOL
)
166 code1
= TYPE_CODE_INT
;
167 if (code2
== TYPE_CODE_BOOL
)
168 code2
= TYPE_CODE_INT
;
170 scalar
= (code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_FLT
171 || code2
== TYPE_CODE_ENUM
|| code2
== TYPE_CODE_RANGE
);
173 if ( code1
== TYPE_CODE_STRUCT
174 && code2
== TYPE_CODE_STRUCT
175 && TYPE_NAME (type
) != 0)
177 /* Look in the type of the source to see if it contains the
178 type of the target as a superclass. If so, we'll need to
179 offset the object in addition to changing its type. */
180 value_ptr v
= search_struct_field (type_name_no_tag (type
),
181 arg2
, 0, VALUE_TYPE (arg2
), 1);
184 VALUE_TYPE (v
) = type
;
188 if (code1
== TYPE_CODE_FLT
&& scalar
)
189 return value_from_double (type
, value_as_double (arg2
));
190 else if ((code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_ENUM
191 || code1
== TYPE_CODE_RANGE
)
192 && (scalar
|| code2
== TYPE_CODE_PTR
))
193 return value_from_longest (type
, value_as_long (arg2
));
194 else if (TYPE_LENGTH (type
) == TYPE_LENGTH (VALUE_TYPE (arg2
)))
196 if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_PTR
)
198 /* Look in the type of the source to see if it contains the
199 type of the target as a superclass. If so, we'll need to
200 offset the pointer rather than just change its type. */
201 struct type
*t1
= TYPE_TARGET_TYPE (type
);
202 struct type
*t2
= TYPE_TARGET_TYPE (VALUE_TYPE (arg2
));
203 if ( TYPE_CODE (t1
) == TYPE_CODE_STRUCT
204 && TYPE_CODE (t2
) == TYPE_CODE_STRUCT
205 && TYPE_NAME (t1
) != 0) /* if name unknown, can't have supercl */
207 value_ptr v
= search_struct_field (type_name_no_tag (t1
),
208 value_ind (arg2
), 0, t2
, 1);
212 VALUE_TYPE (v
) = type
;
216 /* No superclass found, just fall through to change ptr type. */
218 VALUE_TYPE (arg2
) = type
;
221 else if (chill_varying_type (type
))
223 struct type
*range1
, *range2
, *eltype1
, *eltype2
;
226 char *valaddr
, *valaddr_data
;
227 if (code2
== TYPE_CODE_BITSTRING
)
228 error ("not implemented: converting bitstring to varying type");
229 if ((code2
!= TYPE_CODE_ARRAY
&& code2
!= TYPE_CODE_STRING
)
230 || (eltype1
= TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type
, 1)),
231 eltype2
= TYPE_TARGET_TYPE (VALUE_TYPE (arg2
)),
232 (TYPE_LENGTH (eltype1
) != TYPE_LENGTH (eltype2
)
233 /* || TYPE_CODE (eltype1) != TYPE_CODE (eltype2) */ )))
234 error ("Invalid conversion to varying type");
235 range1
= TYPE_FIELD_TYPE (TYPE_FIELD_TYPE (type
, 1), 0);
236 range2
= TYPE_FIELD_TYPE (VALUE_TYPE (arg2
), 0);
237 count1
= TYPE_HIGH_BOUND (range1
) - TYPE_LOW_BOUND (range1
) + 1;
238 count2
= TYPE_HIGH_BOUND (range2
) - TYPE_LOW_BOUND (range2
) + 1;
240 error ("target varying type is too small");
241 val
= allocate_value (type
);
242 valaddr
= VALUE_CONTENTS_RAW (val
);
243 valaddr_data
= valaddr
+ TYPE_FIELD_BITPOS (type
, 1) / 8;
244 /* Set val's __var_length field to count2. */
245 store_signed_integer (valaddr
, TYPE_LENGTH (TYPE_FIELD_TYPE (type
, 0)),
247 /* Set the __var_data field to count2 elements copied from arg2. */
248 memcpy (valaddr_data
, VALUE_CONTENTS (arg2
),
249 count2
* TYPE_LENGTH (eltype2
));
250 /* Zero the rest of the __var_data field of val. */
251 memset (valaddr_data
+ count2
* TYPE_LENGTH (eltype2
), '\0',
252 (count1
- count2
) * TYPE_LENGTH (eltype2
));
255 else if (VALUE_LVAL (arg2
) == lval_memory
)
257 return value_at_lazy (type
, VALUE_ADDRESS (arg2
) + VALUE_OFFSET (arg2
));
259 else if (code1
== TYPE_CODE_VOID
)
261 return value_zero (builtin_type_void
, not_lval
);
265 error ("Invalid cast.");
270 /* Create a value of type TYPE that is zero, and return it. */
273 value_zero (type
, lv
)
277 register value_ptr val
= allocate_value (type
);
279 memset (VALUE_CONTENTS (val
), 0, TYPE_LENGTH (type
));
280 VALUE_LVAL (val
) = lv
;
285 /* Return a value with type TYPE located at ADDR.
287 Call value_at only if the data needs to be fetched immediately;
288 if we can be 'lazy' and defer the fetch, perhaps indefinately, call
289 value_at_lazy instead. value_at_lazy simply records the address of
290 the data and sets the lazy-evaluation-required flag. The lazy flag
291 is tested in the VALUE_CONTENTS macro, which is used if and when
292 the contents are actually required. */
295 value_at (type
, addr
)
299 register value_ptr val
;
301 if (TYPE_CODE (type
) == TYPE_CODE_VOID
)
302 error ("Attempt to dereference a generic pointer.");
304 val
= allocate_value (type
);
306 read_memory (addr
, VALUE_CONTENTS_RAW (val
), TYPE_LENGTH (type
));
308 VALUE_LVAL (val
) = lval_memory
;
309 VALUE_ADDRESS (val
) = addr
;
314 /* Return a lazy value with type TYPE located at ADDR (cf. value_at). */
317 value_at_lazy (type
, addr
)
321 register value_ptr val
;
323 if (TYPE_CODE (type
) == TYPE_CODE_VOID
)
324 error ("Attempt to dereference a generic pointer.");
326 val
= allocate_value (type
);
328 VALUE_LVAL (val
) = lval_memory
;
329 VALUE_ADDRESS (val
) = addr
;
330 VALUE_LAZY (val
) = 1;
335 /* Called only from the VALUE_CONTENTS macro, if the current data for
336 a variable needs to be loaded into VALUE_CONTENTS(VAL). Fetches the
337 data from the user's process, and clears the lazy flag to indicate
338 that the data in the buffer is valid.
340 If the value is zero-length, we avoid calling read_memory, which would
341 abort. We mark the value as fetched anyway -- all 0 bytes of it.
343 This function returns a value because it is used in the VALUE_CONTENTS
344 macro as part of an expression, where a void would not work. The
348 value_fetch_lazy (val
)
349 register value_ptr val
;
351 CORE_ADDR addr
= VALUE_ADDRESS (val
) + VALUE_OFFSET (val
);
353 if (TYPE_LENGTH (VALUE_TYPE (val
)))
354 read_memory (addr
, VALUE_CONTENTS_RAW (val
),
355 TYPE_LENGTH (VALUE_TYPE (val
)));
356 VALUE_LAZY (val
) = 0;
361 /* Store the contents of FROMVAL into the location of TOVAL.
362 Return a new value with the location of TOVAL and contents of FROMVAL. */
365 value_assign (toval
, fromval
)
366 register value_ptr toval
, fromval
;
368 register struct type
*type
;
369 register value_ptr val
;
370 char raw_buffer
[MAX_REGISTER_RAW_SIZE
];
373 if (!toval
->modifiable
)
374 error ("Left operand of assignment is not a modifiable lvalue.");
376 COERCE_ARRAY (fromval
);
379 type
= VALUE_TYPE (toval
);
380 if (VALUE_LVAL (toval
) != lval_internalvar
)
381 fromval
= value_cast (type
, fromval
);
383 /* If TOVAL is a special machine register requiring conversion
384 of program values to a special raw format,
385 convert FROMVAL's contents now, with result in `raw_buffer',
386 and set USE_BUFFER to the number of bytes to write. */
388 #ifdef REGISTER_CONVERTIBLE
389 if (VALUE_REGNO (toval
) >= 0
390 && REGISTER_CONVERTIBLE (VALUE_REGNO (toval
)))
392 int regno
= VALUE_REGNO (toval
);
393 if (REGISTER_CONVERTIBLE (regno
))
395 REGISTER_CONVERT_TO_RAW (VALUE_TYPE (fromval
), regno
,
396 VALUE_CONTENTS (fromval
), raw_buffer
);
397 use_buffer
= REGISTER_RAW_SIZE (regno
);
402 switch (VALUE_LVAL (toval
))
404 case lval_internalvar
:
405 set_internalvar (VALUE_INTERNALVAR (toval
), fromval
);
408 case lval_internalvar_component
:
409 set_internalvar_component (VALUE_INTERNALVAR (toval
),
410 VALUE_OFFSET (toval
),
411 VALUE_BITPOS (toval
),
412 VALUE_BITSIZE (toval
),
417 if (VALUE_BITSIZE (toval
))
419 char buffer
[sizeof (LONGEST
)];
420 /* We assume that the argument to read_memory is in units of
421 host chars. FIXME: Is that correct? */
422 int len
= (VALUE_BITPOS (toval
)
423 + VALUE_BITSIZE (toval
)
427 if (len
> sizeof (LONGEST
))
428 error ("Can't handle bitfields which don't fit in a %d bit word.",
429 sizeof (LONGEST
) * HOST_CHAR_BIT
);
431 read_memory (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
433 modify_field (buffer
, value_as_long (fromval
),
434 VALUE_BITPOS (toval
), VALUE_BITSIZE (toval
));
435 write_memory (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
439 write_memory (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
440 raw_buffer
, use_buffer
);
442 write_memory (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
443 VALUE_CONTENTS (fromval
), TYPE_LENGTH (type
));
447 if (VALUE_BITSIZE (toval
))
449 char buffer
[sizeof (LONGEST
)];
450 int len
= REGISTER_RAW_SIZE (VALUE_REGNO (toval
));
452 if (len
> sizeof (LONGEST
))
453 error ("Can't handle bitfields in registers larger than %d bits.",
454 sizeof (LONGEST
) * HOST_CHAR_BIT
);
456 if (VALUE_BITPOS (toval
) + VALUE_BITSIZE (toval
)
457 > len
* HOST_CHAR_BIT
)
458 /* Getting this right would involve being very careful about
461 Can't handle bitfield which doesn't fit in a single register.");
463 read_register_bytes (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
465 modify_field (buffer
, value_as_long (fromval
),
466 VALUE_BITPOS (toval
), VALUE_BITSIZE (toval
));
467 write_register_bytes (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
471 write_register_bytes (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
472 raw_buffer
, use_buffer
);
475 /* Do any conversion necessary when storing this type to more
476 than one register. */
477 #ifdef REGISTER_CONVERT_FROM_TYPE
478 memcpy (raw_buffer
, VALUE_CONTENTS (fromval
), TYPE_LENGTH (type
));
479 REGISTER_CONVERT_FROM_TYPE(VALUE_REGNO (toval
), type
, raw_buffer
);
480 write_register_bytes (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
481 raw_buffer
, TYPE_LENGTH (type
));
483 write_register_bytes (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
484 VALUE_CONTENTS (fromval
), TYPE_LENGTH (type
));
487 /* Assigning to the stack pointer, frame pointer, and other
488 (architecture and calling convention specific) registers may
489 cause the frame cache to be out of date. We just do this
490 on all assignments to registers for simplicity; I doubt the slowdown
492 reinit_frame_cache ();
495 case lval_reg_frame_relative
:
497 /* value is stored in a series of registers in the frame
498 specified by the structure. Copy that value out, modify
499 it, and copy it back in. */
500 int amount_to_copy
= (VALUE_BITSIZE (toval
) ? 1 : TYPE_LENGTH (type
));
501 int reg_size
= REGISTER_RAW_SIZE (VALUE_FRAME_REGNUM (toval
));
502 int byte_offset
= VALUE_OFFSET (toval
) % reg_size
;
503 int reg_offset
= VALUE_OFFSET (toval
) / reg_size
;
506 /* Make the buffer large enough in all cases. */
507 char *buffer
= (char *) alloca (amount_to_copy
509 + MAX_REGISTER_RAW_SIZE
);
512 struct frame_info
*frame
;
514 /* Figure out which frame this is in currently. */
515 for (frame
= get_current_frame ();
516 frame
&& FRAME_FP (frame
) != VALUE_FRAME (toval
);
517 frame
= get_prev_frame (frame
))
521 error ("Value being assigned to is no longer active.");
523 amount_to_copy
+= (reg_size
- amount_to_copy
% reg_size
);
526 for ((regno
= VALUE_FRAME_REGNUM (toval
) + reg_offset
,
528 amount_copied
< amount_to_copy
;
529 amount_copied
+= reg_size
, regno
++)
531 get_saved_register (buffer
+ amount_copied
,
532 (int *)NULL
, (CORE_ADDR
*)NULL
,
533 frame
, regno
, (enum lval_type
*)NULL
);
536 /* Modify what needs to be modified. */
537 if (VALUE_BITSIZE (toval
))
538 modify_field (buffer
+ byte_offset
,
539 value_as_long (fromval
),
540 VALUE_BITPOS (toval
), VALUE_BITSIZE (toval
));
542 memcpy (buffer
+ byte_offset
, raw_buffer
, use_buffer
);
544 memcpy (buffer
+ byte_offset
, VALUE_CONTENTS (fromval
),
548 for ((regno
= VALUE_FRAME_REGNUM (toval
) + reg_offset
,
550 amount_copied
< amount_to_copy
;
551 amount_copied
+= reg_size
, regno
++)
557 /* Just find out where to put it. */
558 get_saved_register ((char *)NULL
,
559 &optim
, &addr
, frame
, regno
, &lval
);
562 error ("Attempt to assign to a value that was optimized out.");
563 if (lval
== lval_memory
)
564 write_memory (addr
, buffer
+ amount_copied
, reg_size
);
565 else if (lval
== lval_register
)
566 write_register_bytes (addr
, buffer
+ amount_copied
, reg_size
);
568 error ("Attempt to assign to an unmodifiable value.");
575 error ("Left operand of assignment is not an lvalue.");
578 /* Return a value just like TOVAL except with the contents of FROMVAL
579 (except in the case of the type if TOVAL is an internalvar). */
581 if (VALUE_LVAL (toval
) == lval_internalvar
582 || VALUE_LVAL (toval
) == lval_internalvar_component
)
584 type
= VALUE_TYPE (fromval
);
587 val
= allocate_value (type
);
588 memcpy (val
, toval
, VALUE_CONTENTS_RAW (val
) - (char *) val
);
589 memcpy (VALUE_CONTENTS_RAW (val
), VALUE_CONTENTS (fromval
),
591 VALUE_TYPE (val
) = type
;
596 /* Extend a value VAL to COUNT repetitions of its type. */
599 value_repeat (arg1
, count
)
603 register value_ptr val
;
605 if (VALUE_LVAL (arg1
) != lval_memory
)
606 error ("Only values in memory can be extended with '@'.");
608 error ("Invalid number %d of repetitions.", count
);
610 val
= allocate_repeat_value (VALUE_TYPE (arg1
), count
);
612 read_memory (VALUE_ADDRESS (arg1
) + VALUE_OFFSET (arg1
),
613 VALUE_CONTENTS_RAW (val
),
614 TYPE_LENGTH (VALUE_TYPE (val
)) * count
);
615 VALUE_LVAL (val
) = lval_memory
;
616 VALUE_ADDRESS (val
) = VALUE_ADDRESS (arg1
) + VALUE_OFFSET (arg1
);
622 value_of_variable (var
, b
)
627 struct frame_info
*frame
;
630 /* Use selected frame. */
634 frame
= block_innermost_frame (b
);
635 if (frame
== NULL
&& symbol_read_needs_frame (var
))
637 if (BLOCK_FUNCTION (b
) != NULL
638 && SYMBOL_NAME (BLOCK_FUNCTION (b
)) != NULL
)
639 error ("No frame is currently executing in block %s.",
640 SYMBOL_NAME (BLOCK_FUNCTION (b
)));
642 error ("No frame is currently executing in specified block");
645 val
= read_var_value (var
, frame
);
647 error ("Address of symbol \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var
));
651 /* Given a value which is an array, return a value which is a pointer to its
652 first element, regardless of whether or not the array has a nonzero lower
655 FIXME: A previous comment here indicated that this routine should be
656 substracting the array's lower bound. It's not clear to me that this
657 is correct. Given an array subscripting operation, it would certainly
658 work to do the adjustment here, essentially computing:
660 (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0])
662 However I believe a more appropriate and logical place to account for
663 the lower bound is to do so in value_subscript, essentially computing:
665 (&array[0] + ((index - lowerbound) * sizeof array[0]))
667 As further evidence consider what would happen with operations other
668 than array subscripting, where the caller would get back a value that
669 had an address somewhere before the actual first element of the array,
670 and the information about the lower bound would be lost because of
671 the coercion to pointer type.
675 value_coerce_array (arg1
)
678 register struct type
*type
;
680 if (VALUE_LVAL (arg1
) != lval_memory
)
681 error ("Attempt to take address of value not located in memory.");
683 /* Get type of elements. */
684 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_ARRAY
685 || TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_STRING
)
686 type
= TYPE_TARGET_TYPE (VALUE_TYPE (arg1
));
688 /* A phony array made by value_repeat.
689 Its type is the type of the elements, not an array type. */
690 type
= VALUE_TYPE (arg1
);
692 return value_from_longest (lookup_pointer_type (type
),
693 (LONGEST
) (VALUE_ADDRESS (arg1
) + VALUE_OFFSET (arg1
)));
696 /* Given a value which is a function, return a value which is a pointer
700 value_coerce_function (arg1
)
704 if (VALUE_LVAL (arg1
) != lval_memory
)
705 error ("Attempt to take address of value not located in memory.");
707 return value_from_longest (lookup_pointer_type (VALUE_TYPE (arg1
)),
708 (LONGEST
) (VALUE_ADDRESS (arg1
) + VALUE_OFFSET (arg1
)));
711 /* Return a pointer value for the object for which ARG1 is the contents. */
717 struct type
*type
= VALUE_TYPE (arg1
);
718 if (TYPE_CODE (type
) == TYPE_CODE_REF
)
720 /* Copy the value, but change the type from (T&) to (T*).
721 We keep the same location information, which is efficient,
722 and allows &(&X) to get the location containing the reference. */
723 value_ptr arg2
= value_copy (arg1
);
724 VALUE_TYPE (arg2
) = lookup_pointer_type (TYPE_TARGET_TYPE (type
));
727 if (current_language
->c_style_arrays
728 && (VALUE_REPEATED (arg1
)
729 || TYPE_CODE (type
) == TYPE_CODE_ARRAY
))
730 return value_coerce_array (arg1
);
731 if (TYPE_CODE (type
) == TYPE_CODE_FUNC
)
732 return value_coerce_function (arg1
);
734 if (VALUE_LVAL (arg1
) != lval_memory
)
735 error ("Attempt to take address of value not located in memory.");
737 return value_from_longest (lookup_pointer_type (type
),
738 (LONGEST
) (VALUE_ADDRESS (arg1
) + VALUE_OFFSET (arg1
)));
741 /* Given a value of a pointer type, apply the C unary * operator to it. */
749 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_MEMBER
)
750 error ("not implemented: member types in value_ind");
752 /* Allow * on an integer so we can cast it to whatever we want.
753 This returns an int, which seems like the most C-like thing
754 to do. "long long" variables are rare enough that
755 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
756 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_INT
)
757 return value_at (builtin_type_int
,
758 (CORE_ADDR
) value_as_long (arg1
));
759 else if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_PTR
)
760 return value_at_lazy (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)),
761 value_as_pointer (arg1
));
762 error ("Attempt to take contents of a non-pointer value.");
763 return 0; /* For lint -- never reached */
766 /* Pushing small parts of stack frames. */
768 /* Push one word (the size of object that a register holds). */
773 unsigned LONGEST word
;
775 register int len
= REGISTER_SIZE
;
776 char buffer
[MAX_REGISTER_RAW_SIZE
];
778 store_unsigned_integer (buffer
, len
, word
);
781 write_memory (sp
, buffer
, len
);
782 #else /* stack grows upward */
783 write_memory (sp
, buffer
, len
);
785 #endif /* stack grows upward */
790 /* Push LEN bytes with data at BUFFER. */
793 push_bytes (sp
, buffer
, len
)
800 write_memory (sp
, buffer
, len
);
801 #else /* stack grows upward */
802 write_memory (sp
, buffer
, len
);
804 #endif /* stack grows upward */
809 /* Push onto the stack the specified value VALUE. */
813 register CORE_ADDR sp
;
816 register int len
= TYPE_LENGTH (VALUE_TYPE (arg
));
820 write_memory (sp
, VALUE_CONTENTS (arg
), len
);
821 #else /* stack grows upward */
822 write_memory (sp
, VALUE_CONTENTS (arg
), len
);
824 #endif /* stack grows upward */
829 /* Perform the standard coercions that are specified
830 for arguments to be passed to C functions.
832 If PARAM_TYPE is non-NULL, it is the expected parameter type. */
835 value_arg_coerce (arg
, param_type
)
837 struct type
*param_type
;
839 register struct type
*type
= param_type
? param_type
: VALUE_TYPE (arg
);
841 switch (TYPE_CODE (type
))
844 if (TYPE_CODE (VALUE_TYPE (arg
)) != TYPE_CODE_REF
)
846 arg
= value_addr (arg
);
847 VALUE_TYPE (arg
) = param_type
;
855 if (TYPE_LENGTH (type
) < TYPE_LENGTH (builtin_type_int
))
856 type
= builtin_type_int
;
859 if (TYPE_LENGTH (type
) < TYPE_LENGTH (builtin_type_double
))
860 type
= builtin_type_double
;
863 type
= lookup_pointer_type (type
);
867 #if 1 /* FIXME: This is only a temporary patch. -fnf */
868 if (current_language
->c_style_arrays
869 && (VALUE_REPEATED (arg
)
870 || TYPE_CODE (VALUE_TYPE (arg
)) == TYPE_CODE_ARRAY
))
871 arg
= value_coerce_array (arg
);
874 return value_cast (type
, arg
);
877 /* Determine a function's address and its return type from its value.
878 Calls error() if the function is not valid for calling. */
881 find_function_addr (function
, retval_type
)
883 struct type
**retval_type
;
885 register struct type
*ftype
= VALUE_TYPE (function
);
886 register enum type_code code
= TYPE_CODE (ftype
);
887 struct type
*value_type
;
890 /* If it's a member function, just look at the function
893 /* Determine address to call. */
894 if (code
== TYPE_CODE_FUNC
|| code
== TYPE_CODE_METHOD
)
896 funaddr
= VALUE_ADDRESS (function
);
897 value_type
= TYPE_TARGET_TYPE (ftype
);
899 else if (code
== TYPE_CODE_PTR
)
901 funaddr
= value_as_pointer (function
);
902 if (TYPE_CODE (TYPE_TARGET_TYPE (ftype
)) == TYPE_CODE_FUNC
903 || TYPE_CODE (TYPE_TARGET_TYPE (ftype
)) == TYPE_CODE_METHOD
)
905 #ifdef CONVERT_FROM_FUNC_PTR_ADDR
906 /* FIXME: This is a workaround for the unusual function
907 pointer representation on the RS/6000, see comment
908 in config/rs6000/tm-rs6000.h */
909 funaddr
= CONVERT_FROM_FUNC_PTR_ADDR (funaddr
);
911 value_type
= TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (ftype
));
914 value_type
= builtin_type_int
;
916 else if (code
== TYPE_CODE_INT
)
918 /* Handle the case of functions lacking debugging info.
919 Their values are characters since their addresses are char */
920 if (TYPE_LENGTH (ftype
) == 1)
921 funaddr
= value_as_pointer (value_addr (function
));
923 /* Handle integer used as address of a function. */
924 funaddr
= (CORE_ADDR
) value_as_long (function
);
926 value_type
= builtin_type_int
;
929 error ("Invalid data type for function to be called.");
931 *retval_type
= value_type
;
935 #if defined (CALL_DUMMY)
936 /* All this stuff with a dummy frame may seem unnecessarily complicated
937 (why not just save registers in GDB?). The purpose of pushing a dummy
938 frame which looks just like a real frame is so that if you call a
939 function and then hit a breakpoint (get a signal, etc), "backtrace"
940 will look right. Whether the backtrace needs to actually show the
941 stack at the time the inferior function was called is debatable, but
942 it certainly needs to not display garbage. So if you are contemplating
943 making dummy frames be different from normal frames, consider that. */
945 /* Perform a function call in the inferior.
946 ARGS is a vector of values of arguments (NARGS of them).
947 FUNCTION is a value, the function to be called.
948 Returns a value representing what the function returned.
949 May fail to return, if a breakpoint or signal is hit
950 during the execution of the function.
952 ARGS is modified to contain coerced values. */
955 call_function_by_hand (function
, nargs
, args
)
960 register CORE_ADDR sp
;
963 /* CALL_DUMMY is an array of words (REGISTER_SIZE), but each word
964 is in host byte order. Before calling FIX_CALL_DUMMY, we byteswap it
965 and remove any extra bytes which might exist because unsigned LONGEST is
966 bigger than REGISTER_SIZE. */
967 static unsigned LONGEST dummy
[] = CALL_DUMMY
;
968 char dummy1
[REGISTER_SIZE
* sizeof dummy
/ sizeof (unsigned LONGEST
)];
970 struct type
*value_type
;
971 unsigned char struct_return
;
972 CORE_ADDR struct_addr
;
973 struct inferior_status inf_status
;
974 struct cleanup
*old_chain
;
978 struct type
*ftype
= SYMBOL_TYPE (function
);
980 if (!target_has_execution
)
983 save_inferior_status (&inf_status
, 1);
984 old_chain
= make_cleanup (restore_inferior_status
, &inf_status
);
986 /* PUSH_DUMMY_FRAME is responsible for saving the inferior registers
987 (and POP_FRAME for restoring them). (At least on most machines)
988 they are saved on the stack in the inferior. */
991 old_sp
= sp
= read_sp ();
993 #if 1 INNER_THAN 2 /* Stack grows down */
996 #else /* Stack grows up */
1001 funaddr
= find_function_addr (function
, &value_type
);
1004 struct block
*b
= block_for_pc (funaddr
);
1005 /* If compiled without -g, assume GCC. */
1006 using_gcc
= b
== NULL
? 0 : BLOCK_GCC_COMPILED (b
);
1009 /* Are we returning a value using a structure return or a normal
1012 struct_return
= using_struct_return (function
, funaddr
, value_type
,
1015 /* Create a call sequence customized for this function
1016 and the number of arguments for it. */
1017 for (i
= 0; i
< sizeof dummy
/ sizeof (dummy
[0]); i
++)
1018 store_unsigned_integer (&dummy1
[i
* REGISTER_SIZE
],
1020 (unsigned LONGEST
)dummy
[i
]);
1022 #ifdef GDB_TARGET_IS_HPPA
1023 real_pc
= FIX_CALL_DUMMY (dummy1
, start_sp
, funaddr
, nargs
, args
,
1024 value_type
, using_gcc
);
1026 FIX_CALL_DUMMY (dummy1
, start_sp
, funaddr
, nargs
, args
,
1027 value_type
, using_gcc
);
1031 #if CALL_DUMMY_LOCATION == ON_STACK
1032 write_memory (start_sp
, (char *)dummy1
, sizeof dummy1
);
1033 #endif /* On stack. */
1035 #if CALL_DUMMY_LOCATION == BEFORE_TEXT_END
1036 /* Convex Unix prohibits executing in the stack segment. */
1037 /* Hope there is empty room at the top of the text segment. */
1039 extern CORE_ADDR text_end
;
1042 for (start_sp
= text_end
- sizeof dummy1
; start_sp
< text_end
; ++start_sp
)
1043 if (read_memory_integer (start_sp
, 1) != 0)
1044 error ("text segment full -- no place to put call");
1047 real_pc
= text_end
- sizeof dummy1
;
1048 write_memory (real_pc
, (char *)dummy1
, sizeof dummy1
);
1050 #endif /* Before text_end. */
1052 #if CALL_DUMMY_LOCATION == AFTER_TEXT_END
1054 extern CORE_ADDR text_end
;
1058 errcode
= target_write_memory (real_pc
, (char *)dummy1
, sizeof dummy1
);
1060 error ("Cannot write text segment -- call_function failed");
1062 #endif /* After text_end. */
1064 #if CALL_DUMMY_LOCATION == AT_ENTRY_POINT
1066 #endif /* At entry point. */
1069 sp
= old_sp
; /* It really is used, for some ifdef's... */
1072 if (nargs
< TYPE_NFIELDS (ftype
))
1073 error ("too few arguments in function call");
1075 for (i
= nargs
- 1; i
>= 0; i
--)
1077 struct type
*param_type
;
1078 if (TYPE_NFIELDS (ftype
) > i
)
1079 param_type
= TYPE_FIELD_TYPE (ftype
, i
);
1082 args
[i
] = value_arg_coerce (args
[i
], param_type
);
1085 #if defined (REG_STRUCT_HAS_ADDR)
1087 /* This is a machine like the sparc, where we may need to pass a pointer
1088 to the structure, not the structure itself. */
1089 for (i
= nargs
- 1; i
>= 0; i
--)
1090 if ((TYPE_CODE (VALUE_TYPE (args
[i
])) == TYPE_CODE_STRUCT
1091 || TYPE_CODE (VALUE_TYPE (args
[i
])) == TYPE_CODE_UNION
1092 || TYPE_CODE (VALUE_TYPE (args
[i
])) == TYPE_CODE_ARRAY
1093 || TYPE_CODE (VALUE_TYPE (args
[i
])) == TYPE_CODE_STRING
)
1094 && REG_STRUCT_HAS_ADDR (using_gcc
, VALUE_TYPE (args
[i
])))
1097 int len
= TYPE_LENGTH (VALUE_TYPE (args
[i
]));
1099 int aligned_len
= STACK_ALIGN (len
);
1101 int aligned_len
= len
;
1103 #if !(1 INNER_THAN 2)
1104 /* The stack grows up, so the address of the thing we push
1105 is the stack pointer before we push it. */
1110 /* Push the structure. */
1111 write_memory (sp
, VALUE_CONTENTS (args
[i
]), len
);
1113 /* The stack grows down, so the address of the thing we push
1114 is the stack pointer after we push it. */
1119 /* The value we're going to pass is the address of the thing
1121 args
[i
] = value_from_longest (lookup_pointer_type (value_type
),
1125 #endif /* REG_STRUCT_HAS_ADDR. */
1127 /* Reserve space for the return structure to be written on the
1128 stack, if necessary */
1132 int len
= TYPE_LENGTH (value_type
);
1134 len
= STACK_ALIGN (len
);
1146 /* If stack grows down, we must leave a hole at the top. */
1150 for (i
= nargs
- 1; i
>= 0; i
--)
1151 len
+= TYPE_LENGTH (VALUE_TYPE (args
[i
]));
1152 #ifdef CALL_DUMMY_STACK_ADJUST
1153 len
+= CALL_DUMMY_STACK_ADJUST
;
1156 sp
-= STACK_ALIGN (len
) - len
;
1158 sp
+= STACK_ALIGN (len
) - len
;
1161 #endif /* STACK_ALIGN */
1163 #ifdef PUSH_ARGUMENTS
1164 PUSH_ARGUMENTS(nargs
, args
, sp
, struct_return
, struct_addr
);
1165 #else /* !PUSH_ARGUMENTS */
1166 for (i
= nargs
- 1; i
>= 0; i
--)
1167 sp
= value_push (sp
, args
[i
]);
1168 #endif /* !PUSH_ARGUMENTS */
1170 #ifdef CALL_DUMMY_STACK_ADJUST
1172 sp
-= CALL_DUMMY_STACK_ADJUST
;
1174 sp
+= CALL_DUMMY_STACK_ADJUST
;
1176 #endif /* CALL_DUMMY_STACK_ADJUST */
1178 /* Store the address at which the structure is supposed to be
1179 written. Note that this (and the code which reserved the space
1180 above) assumes that gcc was used to compile this function. Since
1181 it doesn't cost us anything but space and if the function is pcc
1182 it will ignore this value, we will make that assumption.
1184 Also note that on some machines (like the sparc) pcc uses a
1185 convention like gcc's. */
1188 STORE_STRUCT_RETURN (struct_addr
, sp
);
1190 /* Write the stack pointer. This is here because the statements above
1191 might fool with it. On SPARC, this write also stores the register
1192 window into the right place in the new stack frame, which otherwise
1193 wouldn't happen. (See store_inferior_registers in sparc-nat.c.) */
1197 char retbuf
[REGISTER_BYTES
];
1199 struct symbol
*symbol
;
1202 symbol
= find_pc_function (funaddr
);
1205 name
= SYMBOL_SOURCE_NAME (symbol
);
1209 /* Try the minimal symbols. */
1210 struct minimal_symbol
*msymbol
= lookup_minimal_symbol_by_pc (funaddr
);
1214 name
= SYMBOL_SOURCE_NAME (msymbol
);
1220 sprintf (format
, "at %s", local_hex_format ());
1222 /* FIXME-32x64: assumes funaddr fits in a long. */
1223 sprintf (name
, format
, (unsigned long) funaddr
);
1226 /* Execute the stack dummy routine, calling FUNCTION.
1227 When it is done, discard the empty frame
1228 after storing the contents of all regs into retbuf. */
1229 if (run_stack_dummy (real_pc
+ CALL_DUMMY_START_OFFSET
, retbuf
))
1231 /* We stopped somewhere besides the call dummy. */
1233 /* If we did the cleanups, we would print a spurious error message
1234 (Unable to restore previously selected frame), would write the
1235 registers from the inf_status (which is wrong), and would do other
1236 wrong things (like set stop_bpstat to the wrong thing). */
1237 discard_cleanups (old_chain
);
1238 /* Prevent memory leak. */
1239 bpstat_clear (&inf_status
.stop_bpstat
);
1241 /* The following error message used to say "The expression
1242 which contained the function call has been discarded." It
1243 is a hard concept to explain in a few words. Ideally, GDB
1244 would be able to resume evaluation of the expression when
1245 the function finally is done executing. Perhaps someday
1246 this will be implemented (it would not be easy). */
1248 /* FIXME: Insert a bunch of wrap_here; name can be very long if it's
1249 a C++ name with arguments and stuff. */
1251 The program being debugged stopped while in a function called from GDB.\n\
1252 When the function (%s) is done executing, GDB will silently\n\
1253 stop (instead of continuing to evaluate the expression containing\n\
1254 the function call).", name
);
1257 do_cleanups (old_chain
);
1259 /* Figure out the value returned by the function. */
1260 return value_being_returned (value_type
, retbuf
, struct_return
);
1263 #else /* no CALL_DUMMY. */
1265 call_function_by_hand (function
, nargs
, args
)
1270 error ("Cannot invoke functions on this machine.");
1272 #endif /* no CALL_DUMMY. */
1275 /* Create a value for an array by allocating space in the inferior, copying
1276 the data into that space, and then setting up an array value.
1278 The array bounds are set from LOWBOUND and HIGHBOUND, and the array is
1279 populated from the values passed in ELEMVEC.
1281 The element type of the array is inherited from the type of the
1282 first element, and all elements must have the same size (though we
1283 don't currently enforce any restriction on their types). */
1286 value_array (lowbound
, highbound
, elemvec
)
1295 struct type
*rangetype
;
1296 struct type
*arraytype
;
1299 /* Validate that the bounds are reasonable and that each of the elements
1300 have the same size. */
1302 nelem
= highbound
- lowbound
+ 1;
1305 error ("bad array bounds (%d, %d)", lowbound
, highbound
);
1307 typelength
= TYPE_LENGTH (VALUE_TYPE (elemvec
[0]));
1308 for (idx
= 0; idx
< nelem
; idx
++)
1310 if (TYPE_LENGTH (VALUE_TYPE (elemvec
[idx
])) != typelength
)
1312 error ("array elements must all be the same size");
1316 /* Allocate space to store the array in the inferior, and then initialize
1317 it by copying in each element. FIXME: Is it worth it to create a
1318 local buffer in which to collect each value and then write all the
1319 bytes in one operation? */
1321 addr
= allocate_space_in_inferior (nelem
* typelength
);
1322 for (idx
= 0; idx
< nelem
; idx
++)
1324 write_memory (addr
+ (idx
* typelength
), VALUE_CONTENTS (elemvec
[idx
]),
1328 /* Create the array type and set up an array value to be evaluated lazily. */
1330 rangetype
= create_range_type ((struct type
*) NULL
, builtin_type_int
,
1331 lowbound
, highbound
);
1332 arraytype
= create_array_type ((struct type
*) NULL
,
1333 VALUE_TYPE (elemvec
[0]), rangetype
);
1334 val
= value_at_lazy (arraytype
, addr
);
1338 /* Create a value for a string constant by allocating space in the inferior,
1339 copying the data into that space, and returning the address with type
1340 TYPE_CODE_STRING. PTR points to the string constant data; LEN is number
1342 Note that string types are like array of char types with a lower bound of
1343 zero and an upper bound of LEN - 1. Also note that the string may contain
1344 embedded null bytes. */
1347 value_string (ptr
, len
)
1352 int lowbound
= current_language
->string_lower_bound
;
1353 struct type
*rangetype
= create_range_type ((struct type
*) NULL
,
1355 lowbound
, len
+ lowbound
- 1);
1356 struct type
*stringtype
1357 = create_string_type ((struct type
*) NULL
, rangetype
);
1360 if (current_language
->c_style_arrays
== 0)
1362 val
= allocate_value (stringtype
);
1363 memcpy (VALUE_CONTENTS_RAW (val
), ptr
, len
);
1368 /* Allocate space to store the string in the inferior, and then
1369 copy LEN bytes from PTR in gdb to that address in the inferior. */
1371 addr
= allocate_space_in_inferior (len
);
1372 write_memory (addr
, ptr
, len
);
1374 val
= value_at_lazy (stringtype
, addr
);
1379 value_bitstring (ptr
, len
)
1384 struct type
*domain_type
= create_range_type (NULL
, builtin_type_int
,
1386 struct type
*type
= create_set_type ((struct type
*) NULL
, domain_type
);
1387 TYPE_CODE (type
) = TYPE_CODE_BITSTRING
;
1388 val
= allocate_value (type
);
1389 memcpy (VALUE_CONTENTS_RAW (val
), ptr
, TYPE_LENGTH (type
) / TARGET_CHAR_BIT
);
1393 /* See if we can pass arguments in T2 to a function which takes arguments
1394 of types T1. Both t1 and t2 are NULL-terminated vectors. If some
1395 arguments need coercion of some sort, then the coerced values are written
1396 into T2. Return value is 0 if the arguments could be matched, or the
1397 position at which they differ if not.
1399 STATICP is nonzero if the T1 argument list came from a
1400 static member function.
1402 For non-static member functions, we ignore the first argument,
1403 which is the type of the instance variable. This is because we want
1404 to handle calls with objects from derived classes. This is not
1405 entirely correct: we should actually check to make sure that a
1406 requested operation is type secure, shouldn't we? FIXME. */
1409 typecmp (staticp
, t1
, t2
)
1418 if (staticp
&& t1
== 0)
1422 if (TYPE_CODE (t1
[0]) == TYPE_CODE_VOID
) return 0;
1423 if (t1
[!staticp
] == 0) return 0;
1424 for (i
= !staticp
; t1
[i
] && TYPE_CODE (t1
[i
]) != TYPE_CODE_VOID
; i
++)
1426 struct type
*tt1
, *tt2
;
1430 tt2
= VALUE_TYPE(t2
[i
]);
1431 if (TYPE_CODE (tt1
) == TYPE_CODE_REF
1432 /* We should be doing hairy argument matching, as below. */
1433 && (TYPE_CODE (TYPE_TARGET_TYPE (tt1
)) == TYPE_CODE (tt2
)))
1435 t2
[i
] = value_addr (t2
[i
]);
1439 while (TYPE_CODE (tt1
) == TYPE_CODE_PTR
1440 && (TYPE_CODE(tt2
)==TYPE_CODE_ARRAY
|| TYPE_CODE(tt2
)==TYPE_CODE_PTR
))
1442 tt1
= TYPE_TARGET_TYPE(tt1
);
1443 tt2
= TYPE_TARGET_TYPE(tt2
);
1445 if (TYPE_CODE(tt1
) == TYPE_CODE(tt2
)) continue;
1446 /* Array to pointer is a `trivial conversion' according to the ARM. */
1448 /* We should be doing much hairier argument matching (see section 13.2
1449 of the ARM), but as a quick kludge, just check for the same type
1451 if (TYPE_CODE (t1
[i
]) != TYPE_CODE (VALUE_TYPE (t2
[i
])))
1454 if (!t1
[i
]) return 0;
1455 return t2
[i
] ? i
+1 : 0;
1458 /* Helper function used by value_struct_elt to recurse through baseclasses.
1459 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1460 and search in it assuming it has (class) type TYPE.
1461 If found, return value, else return NULL.
1463 If LOOKING_FOR_BASECLASS, then instead of looking for struct fields,
1464 look for a baseclass named NAME. */
1467 search_struct_field (name
, arg1
, offset
, type
, looking_for_baseclass
)
1469 register value_ptr arg1
;
1471 register struct type
*type
;
1472 int looking_for_baseclass
;
1476 check_stub_type (type
);
1478 if (! looking_for_baseclass
)
1479 for (i
= TYPE_NFIELDS (type
) - 1; i
>= TYPE_N_BASECLASSES (type
); i
--)
1481 char *t_field_name
= TYPE_FIELD_NAME (type
, i
);
1483 if (t_field_name
&& STREQ (t_field_name
, name
))
1486 if (TYPE_FIELD_STATIC (type
, i
))
1488 char *phys_name
= TYPE_FIELD_STATIC_PHYSNAME (type
, i
);
1489 struct symbol
*sym
=
1490 lookup_symbol (phys_name
, 0, VAR_NAMESPACE
, 0, NULL
);
1492 error ("Internal error: could not find physical static variable named %s",
1494 v
= value_at (TYPE_FIELD_TYPE (type
, i
),
1495 (CORE_ADDR
)SYMBOL_BLOCK_VALUE (sym
));
1498 v
= value_primitive_field (arg1
, offset
, i
, type
);
1500 error("there is no field named %s", name
);
1503 if (t_field_name
&& t_field_name
[0] == '\0'
1504 && TYPE_CODE (TYPE_FIELD_TYPE (type
, i
)) == TYPE_CODE_UNION
)
1506 /* Look for a match through the fields of an anonymous union. */
1508 v
= search_struct_field (name
, arg1
, offset
,
1509 TYPE_FIELD_TYPE (type
, i
),
1510 looking_for_baseclass
);
1516 for (i
= TYPE_N_BASECLASSES (type
) - 1; i
>= 0; i
--)
1519 /* If we are looking for baseclasses, this is what we get when we
1520 hit them. But it could happen that the base part's member name
1521 is not yet filled in. */
1522 int found_baseclass
= (looking_for_baseclass
1523 && TYPE_BASECLASS_NAME (type
, i
) != NULL
1524 && STREQ (name
, TYPE_BASECLASS_NAME (type
, i
)));
1526 if (BASETYPE_VIA_VIRTUAL (type
, i
))
1529 /* Fix to use baseclass_offset instead. FIXME */
1530 baseclass_addr (type
, i
, VALUE_CONTENTS (arg1
) + offset
,
1533 error ("virtual baseclass botch");
1534 if (found_baseclass
)
1536 v
= search_struct_field (name
, v2
, 0, TYPE_BASECLASS (type
, i
),
1537 looking_for_baseclass
);
1539 else if (found_baseclass
)
1540 v
= value_primitive_field (arg1
, offset
, i
, type
);
1542 v
= search_struct_field (name
, arg1
,
1543 offset
+ TYPE_BASECLASS_BITPOS (type
, i
) / 8,
1544 TYPE_BASECLASS (type
, i
),
1545 looking_for_baseclass
);
1551 /* Helper function used by value_struct_elt to recurse through baseclasses.
1552 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1553 and search in it assuming it has (class) type TYPE.
1554 If found, return value, else if name matched and args not return (value)-1,
1555 else return NULL. */
1558 search_struct_method (name
, arg1p
, args
, offset
, static_memfuncp
, type
)
1560 register value_ptr
*arg1p
, *args
;
1561 int offset
, *static_memfuncp
;
1562 register struct type
*type
;
1566 int name_matched
= 0;
1567 char dem_opname
[64];
1569 check_stub_type (type
);
1570 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; i
--)
1572 char *t_field_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
1573 if (strncmp(t_field_name
, "__", 2)==0 ||
1574 strncmp(t_field_name
, "op", 2)==0 ||
1575 strncmp(t_field_name
, "type", 4)==0 )
1577 if (cplus_demangle_opname(t_field_name
, dem_opname
, DMGL_ANSI
))
1578 t_field_name
= dem_opname
;
1579 else if (cplus_demangle_opname(t_field_name
, dem_opname
, 0))
1580 t_field_name
= dem_opname
;
1582 if (t_field_name
&& STREQ (t_field_name
, name
))
1584 int j
= TYPE_FN_FIELDLIST_LENGTH (type
, i
) - 1;
1585 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
1588 if (j
> 0 && args
== 0)
1589 error ("cannot resolve overloaded method `%s'", name
);
1592 if (TYPE_FN_FIELD_STUB (f
, j
))
1593 check_stub_method (type
, i
, j
);
1594 if (!typecmp (TYPE_FN_FIELD_STATIC_P (f
, j
),
1595 TYPE_FN_FIELD_ARGS (f
, j
), args
))
1597 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
1598 return value_virtual_fn_field (arg1p
, f
, j
, type
, offset
);
1599 if (TYPE_FN_FIELD_STATIC_P (f
, j
) && static_memfuncp
)
1600 *static_memfuncp
= 1;
1601 v
= value_fn_field (arg1p
, f
, j
, type
, offset
);
1602 if (v
!= NULL
) return v
;
1609 for (i
= TYPE_N_BASECLASSES (type
) - 1; i
>= 0; i
--)
1613 if (BASETYPE_VIA_VIRTUAL (type
, i
))
1615 base_offset
= baseclass_offset (type
, i
, *arg1p
, offset
);
1616 if (base_offset
== -1)
1617 error ("virtual baseclass botch");
1621 base_offset
= TYPE_BASECLASS_BITPOS (type
, i
) / 8;
1623 v
= search_struct_method (name
, arg1p
, args
, base_offset
+ offset
,
1624 static_memfuncp
, TYPE_BASECLASS (type
, i
));
1625 if (v
== (value_ptr
) -1)
1631 /* FIXME-bothner: Why is this commented out? Why is it here? */
1632 /* *arg1p = arg1_tmp;*/
1636 if (name_matched
) return (value_ptr
) -1;
1640 /* Given *ARGP, a value of type (pointer to a)* structure/union,
1641 extract the component named NAME from the ultimate target structure/union
1642 and return it as a value with its appropriate type.
1643 ERR is used in the error message if *ARGP's type is wrong.
1645 C++: ARGS is a list of argument types to aid in the selection of
1646 an appropriate method. Also, handle derived types.
1648 STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
1649 where the truthvalue of whether the function that was resolved was
1650 a static member function or not is stored.
1652 ERR is an error message to be printed in case the field is not found. */
1655 value_struct_elt (argp
, args
, name
, static_memfuncp
, err
)
1656 register value_ptr
*argp
, *args
;
1658 int *static_memfuncp
;
1661 register struct type
*t
;
1664 COERCE_ARRAY (*argp
);
1666 t
= VALUE_TYPE (*argp
);
1668 /* Follow pointers until we get to a non-pointer. */
1670 while (TYPE_CODE (t
) == TYPE_CODE_PTR
|| TYPE_CODE (t
) == TYPE_CODE_REF
)
1672 *argp
= value_ind (*argp
);
1673 /* Don't coerce fn pointer to fn and then back again! */
1674 if (TYPE_CODE (VALUE_TYPE (*argp
)) != TYPE_CODE_FUNC
)
1675 COERCE_ARRAY (*argp
);
1676 t
= VALUE_TYPE (*argp
);
1679 if (TYPE_CODE (t
) == TYPE_CODE_MEMBER
)
1680 error ("not implemented: member type in value_struct_elt");
1682 if ( TYPE_CODE (t
) != TYPE_CODE_STRUCT
1683 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
1684 error ("Attempt to extract a component of a value that is not a %s.", err
);
1686 /* Assume it's not, unless we see that it is. */
1687 if (static_memfuncp
)
1688 *static_memfuncp
=0;
1692 /* if there are no arguments ...do this... */
1694 /* Try as a field first, because if we succeed, there
1695 is less work to be done. */
1696 v
= search_struct_field (name
, *argp
, 0, t
, 0);
1700 /* C++: If it was not found as a data field, then try to
1701 return it as a pointer to a method. */
1703 if (destructor_name_p (name
, t
))
1704 error ("Cannot get value of destructor");
1706 v
= search_struct_method (name
, argp
, args
, 0, static_memfuncp
, t
);
1708 if (v
== (value_ptr
) -1)
1709 error ("Cannot take address of a method");
1712 if (TYPE_NFN_FIELDS (t
))
1713 error ("There is no member or method named %s.", name
);
1715 error ("There is no member named %s.", name
);
1720 if (destructor_name_p (name
, t
))
1724 /* destructors are a special case. */
1725 v
= value_fn_field (NULL
, TYPE_FN_FIELDLIST1 (t
, 0),
1726 TYPE_FN_FIELDLIST_LENGTH (t
, 0), 0, 0);
1727 if (!v
) error("could not find destructor function named %s.", name
);
1732 error ("destructor should not have any argument");
1736 v
= search_struct_method (name
, argp
, args
, 0, static_memfuncp
, t
);
1738 if (v
== (value_ptr
) -1)
1740 error("Argument list of %s mismatch with component in the structure.", name
);
1744 /* See if user tried to invoke data as function. If so,
1745 hand it back. If it's not callable (i.e., a pointer to function),
1746 gdb should give an error. */
1747 v
= search_struct_field (name
, *argp
, 0, t
, 0);
1751 error ("Structure has no component named %s.", name
);
1755 /* C++: return 1 is NAME is a legitimate name for the destructor
1756 of type TYPE. If TYPE does not have a destructor, or
1757 if NAME is inappropriate for TYPE, an error is signaled. */
1759 destructor_name_p (name
, type
)
1761 const struct type
*type
;
1763 /* destructors are a special case. */
1767 char *dname
= type_name_no_tag (type
);
1768 char *cp
= strchr (dname
, '<');
1771 /* Do not compare the template part for template classes. */
1773 len
= strlen (dname
);
1776 if (strlen (name
+ 1) != len
|| !STREQN (dname
, name
+ 1, len
))
1777 error ("name of destructor must equal name of class");
1784 /* Helper function for check_field: Given TYPE, a structure/union,
1785 return 1 if the component named NAME from the ultimate
1786 target structure/union is defined, otherwise, return 0. */
1789 check_field_in (type
, name
)
1790 register struct type
*type
;
1795 for (i
= TYPE_NFIELDS (type
) - 1; i
>= TYPE_N_BASECLASSES (type
); i
--)
1797 char *t_field_name
= TYPE_FIELD_NAME (type
, i
);
1798 if (t_field_name
&& STREQ (t_field_name
, name
))
1802 /* C++: If it was not found as a data field, then try to
1803 return it as a pointer to a method. */
1805 /* Destructors are a special case. */
1806 if (destructor_name_p (name
, type
))
1809 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; --i
)
1811 if (STREQ (TYPE_FN_FIELDLIST_NAME (type
, i
), name
))
1815 for (i
= TYPE_N_BASECLASSES (type
) - 1; i
>= 0; i
--)
1816 if (check_field_in (TYPE_BASECLASS (type
, i
), name
))
1823 /* C++: Given ARG1, a value of type (pointer to a)* structure/union,
1824 return 1 if the component named NAME from the ultimate
1825 target structure/union is defined, otherwise, return 0. */
1828 check_field (arg1
, name
)
1829 register value_ptr arg1
;
1832 register struct type
*t
;
1834 COERCE_ARRAY (arg1
);
1836 t
= VALUE_TYPE (arg1
);
1838 /* Follow pointers until we get to a non-pointer. */
1840 while (TYPE_CODE (t
) == TYPE_CODE_PTR
|| TYPE_CODE (t
) == TYPE_CODE_REF
)
1841 t
= TYPE_TARGET_TYPE (t
);
1843 if (TYPE_CODE (t
) == TYPE_CODE_MEMBER
)
1844 error ("not implemented: member type in check_field");
1846 if ( TYPE_CODE (t
) != TYPE_CODE_STRUCT
1847 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
1848 error ("Internal error: `this' is not an aggregate");
1850 return check_field_in (t
, name
);
1853 /* C++: Given an aggregate type CURTYPE, and a member name NAME,
1854 return the address of this member as a "pointer to member"
1855 type. If INTYPE is non-null, then it will be the type
1856 of the member we are looking for. This will help us resolve
1857 "pointers to member functions". This function is used
1858 to resolve user expressions of the form "DOMAIN::NAME". */
1861 value_struct_elt_for_reference (domain
, offset
, curtype
, name
, intype
)
1862 struct type
*domain
, *curtype
, *intype
;
1866 register struct type
*t
= curtype
;
1870 if ( TYPE_CODE (t
) != TYPE_CODE_STRUCT
1871 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
1872 error ("Internal error: non-aggregate type to value_struct_elt_for_reference");
1874 for (i
= TYPE_NFIELDS (t
) - 1; i
>= TYPE_N_BASECLASSES (t
); i
--)
1876 char *t_field_name
= TYPE_FIELD_NAME (t
, i
);
1878 if (t_field_name
&& STREQ (t_field_name
, name
))
1880 if (TYPE_FIELD_STATIC (t
, i
))
1882 char *phys_name
= TYPE_FIELD_STATIC_PHYSNAME (t
, i
);
1883 struct symbol
*sym
=
1884 lookup_symbol (phys_name
, 0, VAR_NAMESPACE
, 0, NULL
);
1886 error ("Internal error: could not find physical static variable named %s",
1888 return value_at (SYMBOL_TYPE (sym
),
1889 (CORE_ADDR
)SYMBOL_BLOCK_VALUE (sym
));
1891 if (TYPE_FIELD_PACKED (t
, i
))
1892 error ("pointers to bitfield members not allowed");
1894 return value_from_longest
1895 (lookup_reference_type (lookup_member_type (TYPE_FIELD_TYPE (t
, i
),
1897 offset
+ (LONGEST
) (TYPE_FIELD_BITPOS (t
, i
) >> 3));
1901 /* C++: If it was not found as a data field, then try to
1902 return it as a pointer to a method. */
1904 /* Destructors are a special case. */
1905 if (destructor_name_p (name
, t
))
1907 error ("member pointers to destructors not implemented yet");
1910 /* Perform all necessary dereferencing. */
1911 while (intype
&& TYPE_CODE (intype
) == TYPE_CODE_PTR
)
1912 intype
= TYPE_TARGET_TYPE (intype
);
1914 for (i
= TYPE_NFN_FIELDS (t
) - 1; i
>= 0; --i
)
1916 char *t_field_name
= TYPE_FN_FIELDLIST_NAME (t
, i
);
1917 char dem_opname
[64];
1919 if (strncmp(t_field_name
, "__", 2)==0 ||
1920 strncmp(t_field_name
, "op", 2)==0 ||
1921 strncmp(t_field_name
, "type", 4)==0 )
1923 if (cplus_demangle_opname(t_field_name
, dem_opname
, DMGL_ANSI
))
1924 t_field_name
= dem_opname
;
1925 else if (cplus_demangle_opname(t_field_name
, dem_opname
, 0))
1926 t_field_name
= dem_opname
;
1928 if (t_field_name
&& STREQ (t_field_name
, name
))
1930 int j
= TYPE_FN_FIELDLIST_LENGTH (t
, i
);
1931 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (t
, i
);
1933 if (intype
== 0 && j
> 1)
1934 error ("non-unique member `%s' requires type instantiation", name
);
1938 if (TYPE_FN_FIELD_TYPE (f
, j
) == intype
)
1941 error ("no member function matches that type instantiation");
1946 if (TYPE_FN_FIELD_STUB (f
, j
))
1947 check_stub_method (t
, i
, j
);
1948 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
1950 return value_from_longest
1951 (lookup_reference_type
1952 (lookup_member_type (TYPE_FN_FIELD_TYPE (f
, j
),
1954 (LONGEST
) METHOD_PTR_FROM_VOFFSET (TYPE_FN_FIELD_VOFFSET (f
, j
)));
1958 struct symbol
*s
= lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f
, j
),
1959 0, VAR_NAMESPACE
, 0, NULL
);
1966 v
= read_var_value (s
, 0);
1968 VALUE_TYPE (v
) = lookup_reference_type
1969 (lookup_member_type (TYPE_FN_FIELD_TYPE (f
, j
),
1977 for (i
= TYPE_N_BASECLASSES (t
) - 1; i
>= 0; i
--)
1982 if (BASETYPE_VIA_VIRTUAL (t
, i
))
1985 base_offset
= TYPE_BASECLASS_BITPOS (t
, i
) / 8;
1986 v
= value_struct_elt_for_reference (domain
,
1987 offset
+ base_offset
,
1988 TYPE_BASECLASS (t
, i
),
1997 /* C++: return the value of the class instance variable, if one exists.
1998 Flag COMPLAIN signals an error if the request is made in an
1999 inappropriate context. */
2002 value_of_this (complain
)
2005 struct symbol
*func
, *sym
;
2008 static const char funny_this
[] = "this";
2011 if (selected_frame
== 0)
2013 error ("no frame selected");
2016 func
= get_frame_function (selected_frame
);
2020 error ("no `this' in nameless context");
2024 b
= SYMBOL_BLOCK_VALUE (func
);
2025 i
= BLOCK_NSYMS (b
);
2028 error ("no args, no `this'");
2031 /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
2032 symbol instead of the LOC_ARG one (if both exist). */
2033 sym
= lookup_block_symbol (b
, funny_this
, VAR_NAMESPACE
);
2037 error ("current stack frame not in method");
2042 this = read_var_value (sym
, selected_frame
);
2043 if (this == 0 && complain
)
2044 error ("`this' argument at unknown address");
2048 /* Create a slice (sub-string, sub-array) of ARRAY, that is LENGTH elements
2049 long, starting at LOWBOUND. The result has the same lower bound as
2050 the original ARRAY. */
2053 value_slice (array
, lowbound
, length
)
2055 int lowbound
, length
;
2057 if (TYPE_CODE (VALUE_TYPE (array
)) == TYPE_CODE_BITSTRING
)
2058 error ("not implemented - bitstring slice");
2059 if (TYPE_CODE (VALUE_TYPE (array
)) != TYPE_CODE_ARRAY
2060 && TYPE_CODE (VALUE_TYPE (array
)) != TYPE_CODE_STRING
)
2061 error ("cannot take slice of non-array");
2064 struct type
*slice_range_type
, *slice_type
;
2066 struct type
*range_type
= TYPE_FIELD_TYPE (VALUE_TYPE (array
), 0);
2067 struct type
*element_type
= TYPE_TARGET_TYPE (VALUE_TYPE (array
));
2068 int lowerbound
= TYPE_LOW_BOUND (range_type
);
2069 int upperbound
= TYPE_HIGH_BOUND (range_type
);
2070 int offset
= (lowbound
- lowerbound
) * TYPE_LENGTH (element_type
);
2071 if (lowbound
< lowerbound
|| length
< 0
2072 || lowbound
+ length
- 1 > upperbound
)
2073 error ("slice out of range");
2074 slice_range_type
= create_range_type ((struct type
*) NULL
,
2075 TYPE_TARGET_TYPE (range_type
),
2077 lowerbound
+ length
- 1);
2078 slice_type
= create_array_type ((struct type
*) NULL
, element_type
,
2080 TYPE_CODE (slice_type
) = TYPE_CODE (VALUE_TYPE (array
));
2081 slice
= allocate_value (slice_type
);
2082 if (VALUE_LAZY (array
))
2083 VALUE_LAZY (slice
) = 1;
2085 memcpy (VALUE_CONTENTS (slice
), VALUE_CONTENTS (array
) + offset
,
2086 TYPE_LENGTH (slice_type
));
2087 if (VALUE_LVAL (array
) == lval_internalvar
)
2088 VALUE_LVAL (slice
) = lval_internalvar_component
;
2090 VALUE_LVAL (slice
) = VALUE_LVAL (array
);
2091 VALUE_ADDRESS (slice
) = VALUE_ADDRESS (array
);
2092 VALUE_OFFSET (slice
) = VALUE_OFFSET (array
) + offset
;
2097 /* Assuming chill_varying_type (VARRAY) is true, return an equivalent
2098 value as a fixed-length array. */
2101 varying_to_slice (varray
)
2104 struct type
*vtype
= VALUE_TYPE (varray
);
2105 LONGEST length
= unpack_long (TYPE_FIELD_TYPE (vtype
, 0),
2106 VALUE_CONTENTS (varray
)
2107 + TYPE_FIELD_BITPOS (vtype
, 0) / 8);
2108 return value_slice (value_primitive_field (varray
, 0, 1, vtype
), 0, length
);
2111 /* Create a value for a FORTRAN complex number. Currently most of
2112 the time values are coerced to COMPLEX*16 (i.e. a complex number
2113 composed of 2 doubles. This really should be a smarter routine
2114 that figures out precision inteligently as opposed to assuming
2115 doubles. FIXME: fmb */
2118 value_literal_complex (arg1
, arg2
, type
)
2123 register value_ptr val
;
2124 struct type
*real_type
= TYPE_TARGET_TYPE (type
);
2126 val
= allocate_value (type
);
2127 arg1
= value_cast (real_type
, arg1
);
2128 arg2
= value_cast (real_type
, arg2
);
2130 memcpy (VALUE_CONTENTS_RAW (val
),
2131 VALUE_CONTENTS (arg1
), TYPE_LENGTH (real_type
));
2132 memcpy (VALUE_CONTENTS_RAW (val
) + TYPE_LENGTH (real_type
),
2133 VALUE_CONTENTS (arg2
), TYPE_LENGTH (real_type
));
2137 /* Cast a value into the appropriate complex data type. */
2140 cast_into_complex (type
, val
)
2142 register value_ptr val
;
2144 struct type
*real_type
= TYPE_TARGET_TYPE (type
);
2145 if (TYPE_CODE (VALUE_TYPE (val
)) == TYPE_CODE_COMPLEX
)
2147 struct type
*val_real_type
= TYPE_TARGET_TYPE (VALUE_TYPE (val
));
2148 value_ptr re_val
= allocate_value (val_real_type
);
2149 value_ptr im_val
= allocate_value (val_real_type
);
2151 memcpy (VALUE_CONTENTS_RAW (re_val
),
2152 VALUE_CONTENTS (val
), TYPE_LENGTH (val_real_type
));
2153 memcpy (VALUE_CONTENTS_RAW (im_val
),
2154 VALUE_CONTENTS (val
) + TYPE_LENGTH (val_real_type
),
2155 TYPE_LENGTH (val_real_type
));
2157 return value_literal_complex (re_val
, im_val
, type
);
2159 else if (TYPE_CODE (VALUE_TYPE (val
)) == TYPE_CODE_FLT
2160 || TYPE_CODE (VALUE_TYPE (val
)) == TYPE_CODE_INT
)
2161 return value_literal_complex (val
, value_zero (real_type
, not_lval
), type
);
2163 error ("cannot cast non-number to complex");