1 /* Perform non-arithmetic operations on values, for GDB.
2 Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
4 This file is part of GDB.
6 GDB is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
11 GDB is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GDB; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
28 /* Cast value ARG2 to type TYPE and return as a value.
29 More general than a C cast: accepts any two types of the same length,
30 and if ARG2 is an lvalue it can be cast into anything at all. */
33 value_cast (type
, arg2
)
37 register enum type_code code1
;
38 register enum type_code code2
;
41 /* Coerce arrays but not enums. Enums will work as-is
42 and coercing them would cause an infinite recursion. */
43 if (TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_ENUM
)
46 code1
= TYPE_CODE (type
);
47 code2
= TYPE_CODE (VALUE_TYPE (arg2
));
48 scalar
= (code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_FLT
49 || code2
== TYPE_CODE_ENUM
);
51 if (code1
== TYPE_CODE_FLT
&& scalar
)
52 return value_from_double (type
, value_as_double (arg2
));
53 else if ((code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_ENUM
)
54 && (scalar
|| code2
== TYPE_CODE_PTR
))
55 return value_from_long (type
, value_as_long (arg2
));
56 else if (TYPE_LENGTH (type
) == TYPE_LENGTH (VALUE_TYPE (arg2
)))
58 VALUE_TYPE (arg2
) = type
;
61 else if (VALUE_LVAL (arg2
) == lval_memory
)
63 return value_at (type
, VALUE_ADDRESS (arg2
) + VALUE_OFFSET (arg2
));
66 error ("Invalid cast.");
69 /* Create a value of type TYPE that is zero, and return it. */
76 register value val
= allocate_value (type
);
78 bzero (VALUE_CONTENTS (val
), TYPE_LENGTH (type
));
79 VALUE_LVAL (val
) = lv
;
84 /* Return the value with a specified type located at specified address. */
91 register value val
= allocate_value (type
);
94 temp
= read_memory (addr
, VALUE_CONTENTS (val
), TYPE_LENGTH (type
));
97 if (have_inferior_p ())
98 print_sys_errmsg ("ptrace", temp
);
99 /* Actually, address between addr and addr + len was out of bounds. */
100 error ("Cannot read memory: address 0x%x out of bounds.", addr
);
103 VALUE_LVAL (val
) = lval_memory
;
104 VALUE_ADDRESS (val
) = addr
;
109 /* Store the contents of FROMVAL into the location of TOVAL.
110 Return a new value with the location of TOVAL and contents of FROMVAL. */
113 value_assign (toval
, fromval
)
114 register value toval
, fromval
;
116 register struct type
*type
= VALUE_TYPE (toval
);
118 char raw_buffer
[MAX_REGISTER_RAW_SIZE
];
119 char virtual_buffer
[MAX_REGISTER_VIRTUAL_SIZE
];
122 extern CORE_ADDR
find_saved_register ();
124 COERCE_ARRAY (fromval
);
126 if (VALUE_LVAL (toval
) != lval_internalvar
)
127 fromval
= value_cast (type
, fromval
);
129 /* If TOVAL is a special machine register requiring conversion
130 of program values to a special raw format,
131 convert FROMVAL's contents now, with result in `raw_buffer',
132 and set USE_BUFFER to the number of bytes to write. */
134 if (VALUE_REGNO (toval
) >= 0
135 && REGISTER_CONVERTIBLE (VALUE_REGNO (toval
)))
137 int regno
= VALUE_REGNO (toval
);
138 if (VALUE_TYPE (fromval
) != REGISTER_VIRTUAL_TYPE (regno
))
139 fromval
= value_cast (REGISTER_VIRTUAL_TYPE (regno
), fromval
);
140 bcopy (VALUE_CONTENTS (fromval
), virtual_buffer
,
141 REGISTER_VIRTUAL_SIZE (regno
));
142 REGISTER_CONVERT_TO_RAW (regno
, virtual_buffer
, raw_buffer
);
143 use_buffer
= REGISTER_RAW_SIZE (regno
);
146 switch (VALUE_LVAL (toval
))
148 case lval_internalvar
:
149 set_internalvar (VALUE_INTERNALVAR (toval
), fromval
);
152 case lval_internalvar_component
:
153 set_internalvar_component (VALUE_INTERNALVAR (toval
),
154 VALUE_OFFSET (toval
),
155 VALUE_BITPOS (toval
),
156 VALUE_BITSIZE (toval
),
161 if (VALUE_BITSIZE (toval
))
164 read_memory (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
166 modify_field (&val
, (int) value_as_long (fromval
),
167 VALUE_BITPOS (toval
), VALUE_BITSIZE (toval
));
168 write_memory (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
172 write_memory (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
173 raw_buffer
, use_buffer
);
175 write_memory (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
176 VALUE_CONTENTS (fromval
), TYPE_LENGTH (type
));
180 if (VALUE_BITSIZE (toval
))
184 read_register_bytes (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
186 modify_field (&val
, (int) value_as_long (fromval
),
187 VALUE_BITPOS (toval
), VALUE_BITSIZE (toval
));
188 write_register_bytes (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
192 write_register_bytes (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
193 raw_buffer
, use_buffer
);
195 write_register_bytes (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
196 VALUE_CONTENTS (fromval
), TYPE_LENGTH (type
));
199 case lval_reg_frame_relative
:
201 /* value is stored in a series of registers in the frame
202 specified by the structure. Copy that value out, modify
203 it, and copy it back in. */
204 int amount_to_copy
= (VALUE_BITSIZE (toval
) ? 1 : TYPE_LENGTH (type
));
205 int reg_size
= REGISTER_RAW_SIZE (VALUE_FRAME_REGNUM (toval
));
206 int byte_offset
= VALUE_OFFSET (toval
) % reg_size
;
207 int reg_offset
= VALUE_OFFSET (toval
) / reg_size
;
209 char *buffer
= (char *) alloca (amount_to_copy
);
214 /* Figure out which frame this is in currently. */
215 for (frame
= get_current_frame ();
216 frame
&& FRAME_FP (frame
) != VALUE_FRAME (toval
);
217 frame
= get_prev_frame (frame
))
221 error ("Value being assigned to is no longer active.");
223 amount_to_copy
+= (reg_size
- amount_to_copy
% reg_size
);
226 for ((regno
= VALUE_FRAME_REGNUM (toval
) + reg_offset
,
228 amount_copied
< amount_to_copy
;
229 amount_copied
+= reg_size
, regno
++)
231 addr
= find_saved_register (frame
, regno
);
233 read_register_bytes (REGISTER_BYTE (regno
),
234 buffer
+ amount_copied
,
237 read_memory (addr
, buffer
+ amount_copied
, reg_size
);
240 /* Modify what needs to be modified. */
241 if (VALUE_BITSIZE (toval
))
242 modify_field (buffer
+ byte_offset
,
243 (int) value_as_long (fromval
),
244 VALUE_BITPOS (toval
), VALUE_BITSIZE (toval
));
246 bcopy (raw_buffer
, buffer
+ byte_offset
, use_buffer
);
248 bcopy (VALUE_CONTENTS (fromval
), buffer
+ byte_offset
,
252 for ((regno
= VALUE_FRAME_REGNUM (toval
) + reg_offset
,
254 amount_copied
< amount_to_copy
;
255 amount_copied
+= reg_size
, regno
++)
257 addr
= find_saved_register (frame
, regno
);
259 write_register_bytes (REGISTER_BYTE (regno
),
260 buffer
+ amount_copied
,
263 write_memory (addr
, buffer
+ amount_copied
, reg_size
);
270 error ("Left side of = operation is not an lvalue.");
273 /* Return a value just like TOVAL except with the contents of FROMVAL
274 (except in the case of the type if TOVAL is an internalvar). */
276 if (VALUE_LVAL (toval
) == lval_internalvar
277 || VALUE_LVAL (toval
) == lval_internalvar_component
)
279 type
= VALUE_TYPE (fromval
);
282 val
= allocate_value (type
);
283 bcopy (toval
, val
, VALUE_CONTENTS (val
) - (char *) val
);
284 bcopy (VALUE_CONTENTS (fromval
), VALUE_CONTENTS (val
), TYPE_LENGTH (type
));
285 VALUE_TYPE (val
) = type
;
290 /* Extend a value VAL to COUNT repetitions of its type. */
293 value_repeat (arg1
, count
)
299 if (VALUE_LVAL (arg1
) != lval_memory
)
300 error ("Only values in memory can be extended with '@'.");
302 error ("Invalid number %d of repetitions.", count
);
304 val
= allocate_repeat_value (VALUE_TYPE (arg1
), count
);
306 read_memory (VALUE_ADDRESS (arg1
) + VALUE_OFFSET (arg1
),
307 VALUE_CONTENTS (val
),
308 TYPE_LENGTH (VALUE_TYPE (val
)) * count
);
309 VALUE_LVAL (val
) = lval_memory
;
310 VALUE_ADDRESS (val
) = VALUE_ADDRESS (arg1
) + VALUE_OFFSET (arg1
);
316 value_of_variable (var
)
319 return read_var_value (var
, (FRAME
) 0);
322 /* Given a value which is an array, return a value which is
323 a pointer to its first element. */
326 value_coerce_array (arg1
)
329 register struct type
*type
;
332 if (VALUE_LVAL (arg1
) != lval_memory
)
333 error ("Attempt to take address of value not located in memory.");
335 /* Get type of elements. */
336 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_ARRAY
)
337 type
= TYPE_TARGET_TYPE (VALUE_TYPE (arg1
));
339 /* A phony array made by value_repeat.
340 Its type is the type of the elements, not an array type. */
341 type
= VALUE_TYPE (arg1
);
343 /* Get the type of the result. */
344 type
= lookup_pointer_type (type
);
345 val
= value_from_long (builtin_type_long
,
346 (LONGEST
) (VALUE_ADDRESS (arg1
) + VALUE_OFFSET (arg1
)));
347 VALUE_TYPE (val
) = type
;
351 /* Return a pointer value for the object for which ARG1 is the contents. */
357 register struct type
*type
;
358 register value val
, arg1_coerced
;
360 /* Taking the address of an array is really a no-op
361 once the array is coerced to a pointer to its first element. */
363 COERCE_ARRAY (arg1_coerced
);
364 if (arg1
!= arg1_coerced
)
367 if (VALUE_LVAL (arg1
) != lval_memory
)
368 error ("Attempt to take address of value not located in memory.");
370 /* Get the type of the result. */
371 type
= lookup_pointer_type (VALUE_TYPE (arg1
));
372 val
= value_from_long (builtin_type_long
,
373 (LONGEST
) (VALUE_ADDRESS (arg1
) + VALUE_OFFSET (arg1
)));
374 VALUE_TYPE (val
) = type
;
378 /* Given a value of a pointer type, apply the C unary * operator to it. */
384 /* Must do this before COERCE_ARRAY, otherwise an infinite loop
386 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_REF
)
387 return value_at (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)),
388 (CORE_ADDR
) value_as_long (arg1
));
392 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_MEMBER
)
393 error ("not implemented: member types in value_ind");
395 /* Allow * on an integer so we can cast it to whatever we want. */
396 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_INT
)
397 return value_at (BUILTIN_TYPE_LONGEST
,
398 (CORE_ADDR
) value_as_long (arg1
));
399 else if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_PTR
)
400 return value_at (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)),
401 (CORE_ADDR
) value_as_long (arg1
));
402 else if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_REF
)
403 return value_at (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)),
404 (CORE_ADDR
) value_as_long (arg1
));
405 error ("Attempt to take contents of a non-pointer value.");
408 /* Pushing small parts of stack frames. */
410 /* Push one word (the size of object that a register holds). */
413 push_word (sp
, buffer
)
415 REGISTER_TYPE buffer
;
417 register int len
= sizeof (REGISTER_TYPE
);
421 write_memory (sp
, &buffer
, len
);
422 #else /* stack grows upward */
423 write_memory (sp
, &buffer
, len
);
425 #endif /* stack grows upward */
430 /* Push LEN bytes with data at BUFFER. */
433 push_bytes (sp
, buffer
, len
)
440 write_memory (sp
, buffer
, len
);
441 #else /* stack grows upward */
442 write_memory (sp
, buffer
, len
);
444 #endif /* stack grows upward */
449 /* Push onto the stack the specified value VALUE. */
453 register CORE_ADDR sp
;
456 register int len
= TYPE_LENGTH (VALUE_TYPE (arg
));
460 write_memory (sp
, VALUE_CONTENTS (arg
), len
);
461 #else /* stack grows upward */
462 write_memory (sp
, VALUE_CONTENTS (arg
), len
);
464 #endif /* stack grows upward */
469 /* Perform the standard coercions that are specified
470 for arguments to be passed to C functions. */
473 value_arg_coerce (arg
)
476 register struct type
*type
;
480 type
= VALUE_TYPE (arg
);
482 if (TYPE_CODE (type
) == TYPE_CODE_INT
483 && TYPE_LENGTH (type
) < sizeof (int))
484 return value_cast (builtin_type_int
, arg
);
486 if (type
== builtin_type_float
)
487 return value_cast (builtin_type_double
, arg
);
492 /* Push the value ARG, first coercing it as an argument
496 value_arg_push (sp
, arg
)
497 register CORE_ADDR sp
;
500 return value_push (sp
, value_arg_coerce (arg
));
503 /* Perform a function call in the inferior.
504 ARGS is a vector of values of arguments (NARGS of them).
505 FUNCTION is a value, the function to be called.
506 Returns a value representing what the function returned.
507 May fail to return, if a breakpoint or signal is hit
508 during the execution of the function. */
511 call_function (function
, nargs
, args
)
516 register CORE_ADDR sp
;
519 static REGISTER_TYPE dummy
[] = CALL_DUMMY
;
520 REGISTER_TYPE dummy1
[sizeof dummy
/ sizeof (REGISTER_TYPE
)];
522 struct type
*value_type
;
523 unsigned char struct_return
;
524 CORE_ADDR struct_addr
;
525 struct inferior_status inf_status
;
526 struct cleanup
*old_chain
;
528 if (!have_inferior_p ())
529 error ("Cannot invoke functions if the inferior is not running.");
531 save_inferior_status (&inf_status
, 1);
532 old_chain
= make_cleanup (restore_inferior_status
, &inf_status
);
536 old_sp
= sp
= read_register (SP_REGNUM
);
538 #if 1 INNER_THAN 2 /* Stack grows down */
541 #else /* Stack grows up */
547 register CORE_ADDR funaddr
;
548 register struct type
*ftype
= VALUE_TYPE (function
);
549 register enum type_code code
= TYPE_CODE (ftype
);
551 /* If it's a member function, just look at the function
554 /* Determine address to call. */
555 if (code
== TYPE_CODE_FUNC
|| code
== TYPE_CODE_METHOD
)
557 funaddr
= VALUE_ADDRESS (function
);
558 value_type
= TYPE_TARGET_TYPE (ftype
);
560 else if (code
== TYPE_CODE_PTR
)
562 funaddr
= value_as_long (function
);
563 if (TYPE_CODE (TYPE_TARGET_TYPE (ftype
)) == TYPE_CODE_FUNC
564 || TYPE_CODE (TYPE_TARGET_TYPE (ftype
)) == TYPE_CODE_METHOD
)
565 value_type
= TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (ftype
));
567 value_type
= builtin_type_int
;
569 else if (code
== TYPE_CODE_INT
)
571 /* Handle the case of functions lacking debugging info.
572 Their values are characters since their addresses are char */
573 if (TYPE_LENGTH (ftype
) == 1)
574 funaddr
= value_as_long (value_addr (function
));
576 /* Handle integer used as address of a function. */
577 funaddr
= value_as_long (function
);
579 value_type
= builtin_type_int
;
582 error ("Invalid data type for function to be called.");
584 /* Are we returning a value using a structure return or a normal
587 struct_return
= using_struct_return (function
, funaddr
, value_type
);
589 /* Create a call sequence customized for this function
590 and the number of arguments for it. */
591 bcopy (dummy
, dummy1
, sizeof dummy
);
592 FIX_CALL_DUMMY (dummy1
, start_sp
, funaddr
, nargs
, value_type
);
595 #ifndef CANNOT_EXECUTE_STACK
596 write_memory (start_sp
, dummy1
, sizeof dummy
);
599 /* Convex Unix prohibits executing in the stack segment. */
600 /* Hope there is empty room at the top of the text segment. */
602 extern CORE_ADDR text_end
;
605 for (start_sp
= text_end
- sizeof dummy
; start_sp
< text_end
; ++start_sp
)
606 if (read_memory_integer (start_sp
, 1) != 0)
607 error ("text segment full -- no place to put call");
610 start_sp
= text_end
- sizeof dummy
;
611 write_memory (start_sp
, dummy1
, sizeof dummy
);
613 #endif /* CANNOT_EXECUTE_STACK */
615 /* If stack grows down, we must leave a hole at the top. */
619 /* Reserve space for the return structure to be written on the
620 stack, if necessary */
623 len
+= TYPE_LENGTH (value_type
);
625 for (i
= nargs
- 1; i
>= 0; i
--)
626 len
+= TYPE_LENGTH (VALUE_TYPE (value_arg_coerce (args
[i
])));
627 #ifdef CALL_DUMMY_STACK_ADJUST
628 len
+= CALL_DUMMY_STACK_ADJUST
;
631 sp
-= STACK_ALIGN (len
) - len
;
633 sp
+= STACK_ALIGN (len
) - len
;
636 #endif /* STACK_ALIGN */
638 /* Reserve space for the return structure to be written on the
639 stack, if necessary */
644 sp
-= TYPE_LENGTH (value_type
);
648 sp
+= TYPE_LENGTH (value_type
);
652 for (i
= nargs
- 1; i
>= 0; i
--)
653 sp
= value_arg_push (sp
, args
[i
]);
655 #ifdef CALL_DUMMY_STACK_ADJUST
657 sp
-= CALL_DUMMY_STACK_ADJUST
;
659 sp
+= CALL_DUMMY_STACK_ADJUST
;
661 #endif /* CALL_DUMMY_STACK_ADJUST */
663 /* Store the address at which the structure is supposed to be
664 written. Note that this (and the code which reserved the space
665 above) assumes that gcc was used to compile this function. Since
666 it doesn't cost us anything but space and if the function is pcc
667 it will ignore this value, we will make that assumption.
669 Also note that on some machines (like the sparc) pcc uses this
670 convention in a slightly twisted way also. */
673 STORE_STRUCT_RETURN (struct_addr
, sp
);
675 /* Write the stack pointer. This is here because the statement above
676 might fool with it */
677 write_register (SP_REGNUM
, sp
);
679 /* Figure out the value returned by the function. */
681 char retbuf
[REGISTER_BYTES
];
683 /* Execute the stack dummy routine, calling FUNCTION.
684 When it is done, discard the empty frame
685 after storing the contents of all regs into retbuf. */
686 run_stack_dummy (start_sp
+ CALL_DUMMY_START_OFFSET
, retbuf
);
688 do_cleanups (old_chain
);
690 return value_being_returned (value_type
, retbuf
, struct_return
);
694 /* Create a value for a string constant:
695 Call the function malloc in the inferior to get space for it,
696 then copy the data into that space
697 and then return the address with type char *.
698 PTR points to the string constant data; LEN is number of characters. */
701 value_string (ptr
, len
)
706 register struct symbol
*sym
;
708 register char *copy
= (char *) alloca (len
+ 1);
710 register char *o
= copy
, *ibeg
= ptr
;
713 /* Copy the string into COPY, processing escapes.
714 We could not conveniently process them in expread
715 because the string there wants to be a substring of the input. */
717 while (i
- ibeg
< len
)
722 c
= parse_escape (&i
);
730 /* Get the length of the string after escapes are processed. */
734 /* Find the address of malloc in the inferior. */
736 sym
= lookup_symbol ("malloc", 0, VAR_NAMESPACE
, 0);
739 if (SYMBOL_CLASS (sym
) != LOC_BLOCK
)
740 error ("\"malloc\" exists in this program but is not a function.");
741 val
= value_of_variable (sym
);
746 for (i
= 0; i
< misc_function_count
; i
++)
747 if (!strcmp (misc_function_vector
[i
].name
, "malloc"))
749 if (i
< misc_function_count
)
750 val
= value_from_long (builtin_type_long
,
751 (LONGEST
) misc_function_vector
[i
].address
);
753 error ("String constants require the program to have a function \"malloc\".");
756 blocklen
= value_from_long (builtin_type_int
, (LONGEST
) (len
+ 1));
757 val
= call_function (val
, 1, &blocklen
);
758 if (value_zerop (val
))
759 error ("No memory available for string constant.");
760 write_memory ((CORE_ADDR
) value_as_long (val
), copy
, len
+ 1);
761 VALUE_TYPE (val
) = lookup_pointer_type (builtin_type_char
);
765 /* Given ARG1, a value of type (pointer to a)* structure/union,
766 extract the component named NAME from the ultimate target structure/union
767 and return it as a value with its appropriate type.
768 ERR is used in the error message if ARG1's type is wrong.
770 C++: ARGS is a list of argument types to aid in the selection of
771 an appropriate method. Also, handle derived types.
773 STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
774 where the truthvalue of whether the function that was resolved was
775 a static member function or not.
777 ERR is an error message to be printed in case the field is not found. */
780 value_struct_elt (arg1
, args
, name
, static_memfuncp
, err
)
781 register value arg1
, *args
;
783 int *static_memfuncp
;
786 register struct type
*t
;
790 struct type
*baseclass
;
794 t
= VALUE_TYPE (arg1
);
796 /* Follow pointers until we get to a non-pointer. */
798 while (TYPE_CODE (t
) == TYPE_CODE_PTR
|| TYPE_CODE (t
) == TYPE_CODE_REF
)
800 arg1
= value_ind (arg1
);
802 t
= VALUE_TYPE (arg1
);
805 if (TYPE_CODE (t
) == TYPE_CODE_MEMBER
)
806 error ("not implemented: member type in value_struct_elt");
808 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
809 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
810 error ("Attempt to extract a component of a value that is not a %s.", err
);
814 /* Assume it's not, unless we see that it is. */
820 /* if there are no arguments ...do this... */
822 /* Try as a variable first, because if we succeed, there
823 is less work to be done. */
826 for (i
= TYPE_NFIELDS (t
) - 1; i
>= 0; i
--)
828 char *t_field_name
= TYPE_FIELD_NAME (t
, i
);
829 if (t_field_name
&& !strcmp (t_field_name
, name
))
837 return TYPE_FIELD_STATIC (t
, i
)
838 ? value_static_field (t
, name
, i
) : value_field (arg1
, i
);
840 if (TYPE_N_BASECLASSES (t
) == 0)
843 t
= TYPE_BASECLASS (t
, 1);
844 VALUE_TYPE (arg1
) = t
; /* side effect! */
847 /* C++: If it was not found as a data field, then try to
848 return it as a pointer to a method. */
850 VALUE_TYPE (arg1
) = t
; /* side effect! */
852 if (destructor_name_p (name
, t
))
853 error ("use `info method' command to print out value of destructor");
857 for (i
= TYPE_NFN_FIELDS (t
) - 1; i
>= 0; --i
)
859 if (! strcmp (TYPE_FN_FIELDLIST_NAME (t
, i
), name
))
861 error ("use `info method' command to print value of method \"%s\"", name
);
865 if (TYPE_N_BASECLASSES (t
) == 0)
868 t
= TYPE_BASECLASS (t
, 1);
872 error ("there is no field named %s", name
);
876 if (destructor_name_p (name
, t
))
880 /* destructors are a special case. */
881 return (value
)value_fn_field (arg1
, 0,
882 TYPE_FN_FIELDLIST_LENGTH (t
, 0));
886 error ("destructor should not have any argument");
890 /* This following loop is for methods with arguments. */
893 /* Look up as method first, because that is where we
894 expect to find it first. */
895 for (i
= TYPE_NFN_FIELDS (t
) - 1; i
>= 0; i
--)
897 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (t
, i
);
899 if (!strcmp (TYPE_FN_FIELDLIST_NAME (t
, i
), name
))
902 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (t
, i
);
905 for (j
= TYPE_FN_FIELDLIST_LENGTH (t
, i
) - 1; j
>= 0; --j
)
906 if (!typecmp (TYPE_FN_FIELD_STATIC_P (f
, j
),
907 TYPE_FN_FIELD_ARGS (f
, j
), args
))
909 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
910 return (value
)value_virtual_fn_field (arg1
, f
, j
, t
);
911 if (TYPE_FN_FIELD_STATIC_P (f
, j
) && static_memfuncp
)
912 *static_memfuncp
= 1;
913 return (value
)value_fn_field (arg1
, i
, j
);
918 if (TYPE_N_BASECLASSES (t
) == 0)
921 t
= TYPE_BASECLASS (t
, 1);
922 VALUE_TYPE (arg1
) = t
; /* side effect! */
927 error ("Structure method %s not defined for arglist.", name
);
932 /* See if user tried to invoke data as function */
936 for (i
= TYPE_NFIELDS (t
) - 1; i
>= 0; i
--)
938 char *t_field_name
= TYPE_FIELD_NAME (t
, i
);
939 if (t_field_name
&& !strcmp (t_field_name
, name
))
947 return TYPE_FIELD_STATIC (t
, i
)
948 ? value_static_field (t
, name
, i
) : value_field (arg1
, i
);
950 if (TYPE_N_BASECLASSES (t
) == 0)
953 t
= TYPE_BASECLASS (t
, 1);
954 VALUE_TYPE (arg1
) = t
; /* side effect! */
956 error ("Structure has no component named %s.", name
);
960 /* C++: return 1 is NAME is a legitimate name for the destructor
961 of type TYPE. If TYPE does not have a destructor, or
962 if NAME is inappropriate for TYPE, an error is signaled. */
964 destructor_name_p (name
, type
)
968 /* destructors are a special case. */
969 char *dname
= TYPE_NAME (type
);
973 if (! TYPE_HAS_DESTRUCTOR (type
))
974 error ("type `%s' does not have destructor defined",
976 /* Skip past the "struct " at the front. */
977 while (*dname
++ != ' ') ;
978 if (strcmp (dname
, name
+1))
979 error ("destructor specification error");
986 /* C++: Given ARG1, a value of type (pointer to a)* structure/union,
987 return 1 if the component named NAME from the ultimate
988 target structure/union is defined, otherwise, return 0. */
991 check_field (arg1
, name
)
995 register struct type
*t
;
999 struct type
*baseclass
;
1001 COERCE_ARRAY (arg1
);
1003 t
= VALUE_TYPE (arg1
);
1005 /* Follow pointers until we get to a non-pointer. */
1007 while (TYPE_CODE (t
) == TYPE_CODE_PTR
|| TYPE_CODE (t
) == TYPE_CODE_REF
)
1009 arg1
= value_ind (arg1
);
1010 COERCE_ARRAY (arg1
);
1011 t
= VALUE_TYPE (arg1
);
1014 if (TYPE_CODE (t
) == TYPE_CODE_MEMBER
)
1015 error ("not implemented: member type in check_field");
1017 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
1018 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
1019 error ("Internal error: `this' is not an aggregate");
1025 for (i
= TYPE_NFIELDS (t
) - 1; i
>= 0; i
--)
1027 char *t_field_name
= TYPE_FIELD_NAME (t
, i
);
1028 if (t_field_name
&& !strcmp (t_field_name
, name
))
1033 if (TYPE_N_BASECLASSES (t
) == 0)
1036 t
= TYPE_BASECLASS (t
, 1);
1037 VALUE_TYPE (arg1
) = t
; /* side effect! */
1040 /* C++: If it was not found as a data field, then try to
1041 return it as a pointer to a method. */
1043 VALUE_TYPE (arg1
) = t
; /* side effect! */
1045 /* Destructors are a special case. */
1046 if (destructor_name_p (name
, t
))
1051 for (i
= TYPE_NFN_FIELDS (t
) - 1; i
>= 0; --i
)
1053 if (!strcmp (TYPE_FN_FIELDLIST_NAME (t
, i
), name
))
1057 if (TYPE_N_BASECLASSES (t
) == 0)
1060 t
= TYPE_BASECLASS (t
, 1);
1065 /* C++: Given an aggregate type DOMAIN, and a member name NAME,
1066 return the address of this member as a pointer to member
1067 type. If INTYPE is non-null, then it will be the type
1068 of the member we are looking for. This will help us resolve
1069 pointers to member functions. */
1072 value_struct_elt_for_address (domain
, intype
, name
)
1073 struct type
*domain
, *intype
;
1076 register struct type
*t
= domain
;
1081 struct type
*baseclass
;
1083 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
1084 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
1085 error ("Internal error: non-aggregate type to value_struct_elt_for_address");
1091 for (i
= TYPE_NFIELDS (t
) - 1; i
>= 0; i
--)
1093 char *t_field_name
= TYPE_FIELD_NAME (t
, i
);
1094 if (t_field_name
&& !strcmp (t_field_name
, name
))
1096 if (TYPE_FIELD_PACKED (t
, i
))
1097 error ("pointers to bitfield members not allowed");
1099 v
= value_from_long (builtin_type_int
,
1100 (LONGEST
) (TYPE_FIELD_BITPOS (t
, i
) >> 3));
1101 VALUE_TYPE (v
) = lookup_pointer_type (
1102 lookup_member_type (TYPE_FIELD_TYPE (t
, i
), baseclass
));
1107 if (TYPE_N_BASECLASSES (t
) == 0)
1110 t
= TYPE_BASECLASS (t
, 1);
1113 /* C++: If it was not found as a data field, then try to
1114 return it as a pointer to a method. */
1117 /* Destructors are a special case. */
1118 if (destructor_name_p (name
, t
))
1120 error ("pointers to destructors not implemented yet");
1123 /* Perform all necessary dereferencing. */
1124 while (intype
&& TYPE_CODE (intype
) == TYPE_CODE_PTR
)
1125 intype
= TYPE_TARGET_TYPE (intype
);
1129 for (i
= TYPE_NFN_FIELDS (t
) - 1; i
>= 0; --i
)
1131 if (!strcmp (TYPE_FN_FIELDLIST_NAME (t
, i
), name
))
1133 int j
= TYPE_FN_FIELDLIST_LENGTH (t
, i
);
1134 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (t
, i
);
1136 if (intype
== 0 && j
> 1)
1137 error ("non-unique member `%s' requires type instantiation", name
);
1141 if (TYPE_FN_FIELD_TYPE (f
, j
) == intype
)
1144 error ("no member function matches that type instantiation");
1149 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
1151 v
= value_from_long (builtin_type_long
,
1152 (LONGEST
) TYPE_FN_FIELD_VOFFSET (f
, j
));
1156 struct symbol
*s
= lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f
, j
),
1157 0, VAR_NAMESPACE
, 0);
1158 v
= locate_var_value (s
, 0);
1160 VALUE_TYPE (v
) = lookup_pointer_type (lookup_member_type (TYPE_FN_FIELD_TYPE (f
, j
), baseclass
));
1165 if (TYPE_N_BASECLASSES (t
) == 0)
1168 t
= TYPE_BASECLASS (t
, 1);
1173 /* Compare two argument lists and return the position in which they differ,
1176 STATICP is nonzero if the T1 argument list came from a
1177 static member function.
1179 For non-static member functions, we ignore the first argument,
1180 which is the type of the instance variable. This is because we want
1181 to handle calls with objects from derived classes. This is not
1182 entirely correct: we should actually check to make sure that a
1183 requested operation is type secure, shouldn't we? */
1186 typecmp (staticp
, t1
, t2
)
1193 if (staticp
&& t1
== 0)
1197 if (t1
[0]->code
== TYPE_CODE_VOID
) return 0;
1198 if (t1
[!staticp
] == 0) return 0;
1199 for (i
= !staticp
; t1
[i
] && t1
[i
]->code
!= TYPE_CODE_VOID
; i
++)
1202 || t1
[i
]->code
!= t2
[i
]->type
->code
1203 || t1
[i
]->target_type
!= t2
[i
]->type
->target_type
)
1206 if (!t1
[i
]) return 0;
1207 return t2
[i
] ? i
+1 : 0;
1210 /* C++: return the value of the class instance variable, if one exists.
1211 Flag COMPLAIN signals an error if the request is made in an
1212 inappropriate context. */
1214 value_of_this (complain
)
1217 extern FRAME selected_frame
;
1218 struct symbol
*func
, *sym
;
1223 if (selected_frame
== 0)
1225 error ("no frame selected");
1228 func
= get_frame_function (selected_frame
);
1230 funname
= SYMBOL_NAME (func
);
1233 error ("no `this' in nameless context");
1236 b
= SYMBOL_BLOCK_VALUE (func
);
1237 i
= BLOCK_NSYMS (b
);
1240 error ("no args, no `this'");
1243 sym
= BLOCK_SYM (b
, 0);
1244 if (strncmp ("$this", SYMBOL_NAME (sym
), 5))
1246 error ("current stack frame not in method");
1249 return read_var_value (sym
, selected_frame
);