1 /* Perform non-arithmetic operations on values, for GDB.
2 Copyright (C) 1986, 1987 Free Software Foundation, Inc.
4 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
5 WARRANTY. No author or distributor accepts responsibility to anyone
6 for the consequences of using it or for whether it serves any
7 particular purpose or works at all, unless he says so in writing.
8 Refer to the GDB General Public License for full details.
10 Everyone is granted permission to copy, modify and redistribute GDB,
11 but only under the conditions described in the GDB General Public
12 License. A copy of this license is supposed to have been given to you
13 along with GDB so you can know your rights and responsibilities. It
14 should be in a file named COPYING. Among other things, the copyright
15 notice and this notice must be preserved on all copies.
17 In other words, go ahead and share GDB, but don't try to stop
18 anyone else from sharing it farther. Help stamp out software hoarding!
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 /* Return the value with a specified type located at specified address. */
76 register value val
= allocate_value (type
);
79 temp
= read_memory (addr
, VALUE_CONTENTS (val
), TYPE_LENGTH (type
));
82 if (have_inferior_p ())
83 print_sys_errmsg ("ptrace", temp
);
84 /* Actually, address between addr and addr + len was out of bounds. */
85 error ("Cannot read memory: address 0x%x out of bounds.", addr
);
88 VALUE_LVAL (val
) = lval_memory
;
89 VALUE_ADDRESS (val
) = addr
;
94 /* Store the contents of FROMVAL into the location of TOVAL.
95 Return a new value with the location of TOVAL and contents of FROMVAL. */
98 value_assign (toval
, fromval
)
99 register value toval
, fromval
;
101 register struct type
*type
= VALUE_TYPE (toval
);
103 char raw_buffer
[MAX_REGISTER_RAW_SIZE
];
104 char virtual_buffer
[MAX_REGISTER_VIRTUAL_SIZE
];
107 extern CORE_ADDR
find_saved_register ();
109 COERCE_ARRAY (fromval
);
111 if (VALUE_LVAL (toval
) != lval_internalvar
)
112 fromval
= value_cast (type
, fromval
);
114 /* If TOVAL is a special machine register requiring conversion
115 of program values to a special raw format,
116 convert FROMVAL's contents now, with result in `raw_buffer',
117 and set USE_BUFFER to the number of bytes to write. */
119 if (VALUE_REGNO (toval
) >= 0
120 && REGISTER_CONVERTIBLE (VALUE_REGNO (toval
)))
122 int regno
= VALUE_REGNO (toval
);
123 if (VALUE_TYPE (fromval
) != REGISTER_VIRTUAL_TYPE (regno
))
124 fromval
= value_cast (REGISTER_VIRTUAL_TYPE (regno
), fromval
);
125 bcopy (VALUE_CONTENTS (fromval
), virtual_buffer
,
126 REGISTER_VIRTUAL_SIZE (regno
));
127 REGISTER_CONVERT_TO_RAW (regno
, virtual_buffer
, raw_buffer
);
128 use_buffer
= REGISTER_RAW_SIZE (regno
);
131 switch (VALUE_LVAL (toval
))
133 case lval_internalvar
:
134 set_internalvar (VALUE_INTERNALVAR (toval
), fromval
);
137 case lval_internalvar_component
:
138 set_internalvar_component (VALUE_INTERNALVAR (toval
),
139 VALUE_OFFSET (toval
),
140 VALUE_BITPOS (toval
),
141 VALUE_BITSIZE (toval
),
146 if (VALUE_BITSIZE (toval
))
149 read_memory (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
151 modify_field (&val
, (int) value_as_long (fromval
),
152 VALUE_BITPOS (toval
), VALUE_BITSIZE (toval
));
153 write_memory (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
157 write_memory (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
158 raw_buffer
, use_buffer
);
160 write_memory (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
161 VALUE_CONTENTS (fromval
), TYPE_LENGTH (type
));
165 if (VALUE_BITSIZE (toval
))
169 read_register_bytes (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
171 modify_field (&val
, (int) value_as_long (fromval
),
172 VALUE_BITPOS (toval
), VALUE_BITSIZE (toval
));
173 write_register_bytes (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
177 write_register_bytes (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
178 raw_buffer
, use_buffer
);
180 write_register_bytes (VALUE_ADDRESS (toval
) + VALUE_OFFSET (toval
),
181 VALUE_CONTENTS (fromval
), TYPE_LENGTH (type
));
184 case lval_reg_frame_relative
:
186 /* value is stored in a series of registers in the frame
187 specified by the structure. Copy that value out, modify
188 it, and copy it back in. */
189 int amount_to_copy
= (VALUE_BITSIZE (toval
) ? 1 : TYPE_LENGTH (type
));
190 int reg_size
= REGISTER_RAW_SIZE (VALUE_FRAME_REGNUM (toval
));
191 int byte_offset
= VALUE_OFFSET (toval
) % reg_size
;
192 int reg_offset
= VALUE_OFFSET (toval
) / reg_size
;
194 char *buffer
= (char *) alloca (amount_to_copy
);
199 /* Figure out which frame this is in currently. */
200 for (frame
= get_current_frame ();
201 frame
&& FRAME_FP (frame
) != VALUE_FRAME (toval
);
202 frame
= get_prev_frame (frame
))
206 error ("Value being assigned to is no longer active.");
208 amount_to_copy
+= (reg_size
- amount_to_copy
% reg_size
);
211 for ((regno
= VALUE_FRAME_REGNUM (toval
) + reg_offset
,
213 amount_copied
< amount_to_copy
;
214 amount_copied
+= reg_size
, regno
++)
216 addr
= find_saved_register (frame
, regno
);
218 read_register_bytes (REGISTER_BYTE (regno
),
219 buffer
+ amount_copied
,
222 read_memory (addr
, buffer
+ amount_copied
, reg_size
);
225 /* Modify what needs to be modified. */
226 if (VALUE_BITSIZE (toval
))
227 modify_field (buffer
+ byte_offset
,
228 (int) value_as_long (fromval
),
229 VALUE_BITPOS (toval
), VALUE_BITSIZE (toval
));
231 bcopy (raw_buffer
, buffer
+ byte_offset
, use_buffer
);
233 bcopy (VALUE_CONTENTS (fromval
), buffer
+ byte_offset
,
237 for ((regno
= VALUE_FRAME_REGNUM (toval
) + reg_offset
,
239 amount_copied
< amount_to_copy
;
240 amount_copied
+= reg_size
, regno
++)
242 addr
= find_saved_register (frame
, regno
);
244 write_register_bytes (REGISTER_BYTE (regno
),
245 buffer
+ amount_copied
,
248 write_memory (addr
, buffer
+ amount_copied
, reg_size
);
255 error ("Left side of = operation is not an lvalue.");
258 /* Return a value just like TOVAL except with the contents of FROMVAL. */
260 val
= allocate_value (type
);
261 bcopy (toval
, val
, VALUE_CONTENTS (val
) - (char *) val
);
262 bcopy (VALUE_CONTENTS (fromval
), VALUE_CONTENTS (val
), TYPE_LENGTH (type
));
267 /* Extend a value VAL to COUNT repetitions of its type. */
270 value_repeat (arg1
, count
)
276 if (VALUE_LVAL (arg1
) != lval_memory
)
277 error ("Only values in memory can be extended with '@'.");
279 error ("Invalid number %d of repetitions.", count
);
281 val
= allocate_repeat_value (VALUE_TYPE (arg1
), count
);
283 read_memory (VALUE_ADDRESS (arg1
) + VALUE_OFFSET (arg1
),
284 VALUE_CONTENTS (val
),
285 TYPE_LENGTH (VALUE_TYPE (val
)) * count
);
286 VALUE_LVAL (val
) = lval_memory
;
287 VALUE_ADDRESS (val
) = VALUE_ADDRESS (arg1
) + VALUE_OFFSET (arg1
);
293 value_of_variable (var
)
296 return read_var_value (var
, (CORE_ADDR
) 0);
299 /* Given a value which is an array, return a value which is
300 a pointer to its first element. */
303 value_coerce_array (arg1
)
306 register struct type
*type
;
309 if (VALUE_LVAL (arg1
) != lval_memory
)
310 error ("Attempt to take address of value not located in memory.");
312 /* Get type of elements. */
313 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_ARRAY
)
314 type
= TYPE_TARGET_TYPE (VALUE_TYPE (arg1
));
316 /* A phony array made by value_repeat.
317 Its type is the type of the elements, not an array type. */
318 type
= VALUE_TYPE (arg1
);
320 /* Get the type of the result. */
321 type
= lookup_pointer_type (type
);
322 val
= value_from_long (builtin_type_long
,
323 (LONGEST
) (VALUE_ADDRESS (arg1
) + VALUE_OFFSET (arg1
)));
324 VALUE_TYPE (val
) = type
;
328 /* Return a pointer value for the object for which ARG1 is the contents. */
334 register struct type
*type
;
335 register value val
, arg1_coerced
;
337 /* Taking the address of an array is really a no-op
338 once the array is coerced to a pointer to its first element. */
340 COERCE_ARRAY (arg1_coerced
);
341 if (arg1
!= arg1_coerced
)
344 if (VALUE_LVAL (arg1
) != lval_memory
)
345 error ("Attempt to take address of value not located in memory.");
347 /* Get the type of the result. */
348 type
= lookup_pointer_type (VALUE_TYPE (arg1
));
349 val
= value_from_long (builtin_type_long
,
350 (LONGEST
) (VALUE_ADDRESS (arg1
) + VALUE_OFFSET (arg1
)));
351 VALUE_TYPE (val
) = type
;
355 /* Given a value of a pointer type, apply the C unary * operator to it. */
361 /* Must do this before COERCE_ARRAY, otherwise an infinite loop
363 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_REF
)
364 return value_at (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)),
365 (CORE_ADDR
) value_as_long (arg1
));
369 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_MEMBER
)
370 error ("not implemented: member types in value_ind");
372 /* Allow * on an integer so we can cast it to whatever we want. */
373 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_INT
)
374 return value_at (builtin_type_long
,
375 (CORE_ADDR
) value_as_long (arg1
));
376 else if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_PTR
)
377 return value_at (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)),
378 (CORE_ADDR
) value_as_long (arg1
));
379 else if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_REF
)
380 return value_at (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)),
381 (CORE_ADDR
) value_as_long (arg1
));
382 error ("Attempt to take contents of a non-pointer value.");
385 /* Pushing small parts of stack frames. */
387 /* Push one word (the size of object that a register holds). */
390 push_word (sp
, buffer
)
392 REGISTER_TYPE buffer
;
394 register int len
= sizeof (REGISTER_TYPE
);
398 write_memory (sp
, &buffer
, len
);
399 #else /* stack grows upward */
400 write_memory (sp
, &buffer
, len
);
402 #endif /* stack grows upward */
407 /* Push LEN bytes with data at BUFFER. */
410 push_bytes (sp
, buffer
, len
)
417 write_memory (sp
, buffer
, len
);
418 #else /* stack grows upward */
419 write_memory (sp
, buffer
, len
);
421 #endif /* stack grows upward */
426 /* Push onto the stack the specified value VALUE. */
430 register CORE_ADDR sp
;
433 register int len
= TYPE_LENGTH (VALUE_TYPE (arg
));
437 write_memory (sp
, VALUE_CONTENTS (arg
), len
);
438 #else /* stack grows upward */
439 write_memory (sp
, VALUE_CONTENTS (arg
), len
);
441 #endif /* stack grows upward */
446 /* Perform the standard coercions that are specified
447 for arguments to be passed to C functions. */
450 value_arg_coerce (arg
)
453 register struct type
*type
;
457 type
= VALUE_TYPE (arg
);
459 if (TYPE_CODE (type
) == TYPE_CODE_INT
460 && TYPE_LENGTH (type
) < sizeof (int))
461 return value_cast (builtin_type_int
, arg
);
463 if (type
== builtin_type_float
)
464 return value_cast (builtin_type_double
, arg
);
469 /* Push the value ARG, first coercing it as an argument
473 value_arg_push (sp
, arg
)
474 register CORE_ADDR sp
;
477 return value_push (sp
, value_arg_coerce (arg
));
480 /* Perform a function call in the inferior.
481 ARGS is a vector of values of arguments (NARGS of them).
482 FUNCTION is a value, the function to be called.
483 Returns a value representing what the function returned.
484 May fail to return, if a breakpoint or signal is hit
485 during the execution of the function. */
488 call_function (function
, nargs
, args
)
493 register CORE_ADDR sp
;
496 static REGISTER_TYPE dummy
[] = CALL_DUMMY
;
497 REGISTER_TYPE dummy1
[sizeof dummy
/ sizeof (REGISTER_TYPE
)];
499 struct type
*value_type
;
500 unsigned char struct_return
;
501 CORE_ADDR struct_addr
;
502 struct inferior_status inf_status
;
503 struct cleanup
*old_chain
;
505 save_inferior_status (&inf_status
, 1);
506 old_chain
= make_cleanup (restore_inferior_status
, &inf_status
);
510 old_sp
= sp
= read_register (SP_REGNUM
);
512 #if 1 INNER_THAN 2 /* Stack grows down */
515 #else /* Stack grows up */
521 register CORE_ADDR funaddr
;
522 register struct type
*ftype
= VALUE_TYPE (function
);
523 register enum type_code code
= TYPE_CODE (ftype
);
525 /* If it's a member function, just look at the function
527 if (code
== TYPE_CODE_MEMBER
)
529 ftype
= TYPE_TARGET_TYPE (ftype
);
530 code
= TYPE_CODE (ftype
);
533 /* Determine address to call. */
534 if (code
== TYPE_CODE_FUNC
)
536 funaddr
= VALUE_ADDRESS (function
);
537 value_type
= TYPE_TARGET_TYPE (ftype
);
539 else if (code
== TYPE_CODE_PTR
)
541 funaddr
= value_as_long (function
);
542 if (TYPE_CODE (TYPE_TARGET_TYPE (ftype
))
544 value_type
= TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (ftype
));
546 value_type
= builtin_type_int
;
548 else if (code
== TYPE_CODE_INT
)
550 /* Handle the case of functions lacking debugging info.
551 Their values are characters since their addresses are char */
552 if (TYPE_LENGTH (ftype
) == 1)
553 funaddr
= value_as_long (value_addr (function
));
555 /* Handle integer used as address of a function. */
556 funaddr
= value_as_long (function
);
558 value_type
= builtin_type_int
;
561 error ("Invalid data type for function to be called.");
563 /* Are we returning a value using a structure return or a normal
566 struct_return
= using_struct_return (function
, funaddr
, value_type
);
568 /* Create a call sequence customized for this function
569 and the number of arguments for it. */
570 bcopy (dummy
, dummy1
, sizeof dummy
);
571 FIX_CALL_DUMMY (dummy1
, start_sp
, funaddr
, nargs
, value_type
);
574 write_memory (start_sp
, dummy1
, sizeof dummy
);
577 /* Convex Unix prohibits executing in the stack segment. */
578 /* Hope there is empty room at the top of the text segment. */
580 extern CORE_ADDR text_end
;
583 for (start_sp
= text_end
- sizeof dummy
; start_sp
< text_end
; ++start_sp
)
584 if (read_memory_integer (start_sp
, 1) != 0)
585 error ("text segment full -- no place to put call");
587 start_sp
= text_end
- sizeof dummy
;
588 write_memory (start_sp
, dummy1
, sizeof dummy
);
592 /* If stack grows down, we must leave a hole at the top. */
596 /* Reserve space for the return structure to be written on the
597 stack, if necessary */
600 len
+= TYPE_LENGTH (value_type
);
602 for (i
= nargs
- 1; i
>= 0; i
--)
603 len
+= TYPE_LENGTH (VALUE_TYPE (args
[i
]));
604 #ifdef CALL_DUMMY_STACK_ADJUST
605 len
+= CALL_DUMMY_STACK_ADJUST
;
608 sp
-= STACK_ALIGN (len
) - len
;
610 sp
+= STACK_ALIGN (len
) - len
;
613 #endif /* STACK_ALIGN */
615 /* Reserve space for the return structure to be written on the
616 stack, if necessary */
621 sp
-= TYPE_LENGTH (value_type
);
625 sp
+= TYPE_LENGTH (value_type
);
629 for (i
= nargs
- 1; i
>= 0; i
--)
630 sp
= value_arg_push (sp
, args
[i
]);
632 #ifdef CALL_DUMMY_STACK_ADJUST
634 sp
-= CALL_DUMMY_STACK_ADJUST
;
636 sp
+= CALL_DUMMY_STACK_ADJUST
;
638 #endif /* CALL_DUMMY_STACK_ADJUST */
641 /* Store the address at which the structure is supposed to be
642 written. Note that this (and the code which reserved the space
643 above) assumes that gcc was used to compile this function. Since
644 it doesn't cost us anything but space and if the function is pcc
645 it will ignore this value, we will make that assumption.
647 Also note that on some machines (like the sparc) pcc uses this
648 convention in a slightly twisted way also. */
651 STORE_STRUCT_RETURN (struct_addr
, sp
);
653 /* Write the stack pointer. This is here because the statement above
654 might fool with it */
655 write_register (SP_REGNUM
, sp
);
657 /* Figure out the value returned by the function. */
659 char retbuf
[REGISTER_BYTES
];
661 /* Execute the stack dummy routine, calling FUNCTION.
662 When it is done, discard the empty frame
663 after storing the contents of all regs into retbuf. */
664 run_stack_dummy (start_sp
+ CALL_DUMMY_START_OFFSET
, retbuf
);
666 do_cleanups (old_chain
);
668 return value_being_returned (value_type
, retbuf
, struct_return
);
672 /* Create a value for a string constant:
673 Call the function malloc in the inferior to get space for it,
674 then copy the data into that space
675 and then return the address with type char *.
676 PTR points to the string constant data; LEN is number of characters. */
679 value_string (ptr
, len
)
684 register struct symbol
*sym
;
686 register char *copy
= (char *) alloca (len
+ 1);
688 register char *o
= copy
, *ibeg
= ptr
;
691 /* Copy the string into COPY, processing escapes.
692 We could not conveniently process them in expread
693 because the string there wants to be a substring of the input. */
695 while (i
- ibeg
< len
)
700 c
= parse_escape (&i
);
708 /* Get the length of the string after escapes are processed. */
712 /* Find the address of malloc in the inferior. */
714 sym
= lookup_symbol ("malloc", 0, VAR_NAMESPACE
, 0);
717 if (SYMBOL_CLASS (sym
) != LOC_BLOCK
)
718 error ("\"malloc\" exists in this program but is not a function.");
719 val
= value_of_variable (sym
);
724 for (i
= 0; i
< misc_function_count
; i
++)
725 if (!strcmp (misc_function_vector
[i
].name
, "malloc"))
727 if (i
< misc_function_count
)
728 val
= value_from_long (builtin_type_long
,
729 (LONGEST
) misc_function_vector
[i
].address
);
731 error ("String constants require the program to have a function \"malloc\".");
734 blocklen
= value_from_long (builtin_type_int
, (LONGEST
) (len
+ 1));
735 val
= call_function (val
, 1, &blocklen
);
736 if (value_zerop (val
))
737 error ("No memory available for string constant.");
738 write_memory ((CORE_ADDR
) value_as_long (val
), copy
, len
+ 1);
739 VALUE_TYPE (val
) = lookup_pointer_type (builtin_type_char
);
743 /* Given ARG1, a value of type (pointer to a)* structure/union,
744 extract the component named NAME from the ultimate target structure/union
745 and return it as a value with its appropriate type.
746 ERR is used in the error message if ARG1's type is wrong.
748 C++: ARGS is a list of argument types to aid in the selection of
749 an appropriate method. Also, handle derived types.
751 ERR is an error message to be printed in case the field is not found. */
754 value_struct_elt (arg1
, args
, name
, err
)
755 register value arg1
, *args
;
759 register struct type
*t
;
763 struct type
*baseclass
;
767 t
= VALUE_TYPE (arg1
);
769 /* Follow pointers until we get to a non-pointer. */
771 while (TYPE_CODE (t
) == TYPE_CODE_PTR
|| TYPE_CODE (t
) == TYPE_CODE_REF
)
773 arg1
= value_ind (arg1
);
775 t
= VALUE_TYPE (arg1
);
778 if (TYPE_CODE (t
) == TYPE_CODE_MEMBER
)
779 error ("not implemented: member type in value_struct_elt");
781 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
782 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
783 error ("Attempt to extract a component of a value that is not a %s.", err
);
789 /* if there are no arguments ...do this... */
791 /* Try as a variable first, because if we succeed, there
792 is less work to be done. */
795 for (i
= TYPE_NFIELDS (t
) - 1; i
>= 0; i
--)
797 if (!strcmp (TYPE_FIELD_NAME (t
, i
), name
))
805 return TYPE_FIELD_STATIC (t
, i
)
806 ? value_static_field (t
, name
, i
) : value_field (arg1
, i
);
808 if (TYPE_N_BASECLASSES (t
) == 0)
811 t
= TYPE_BASECLASS (t
, 1);
812 VALUE_TYPE (arg1
) = t
; /* side effect! */
815 /* C++: If it was not found as a data field, then try to
816 return it as a pointer to a method. */
818 VALUE_TYPE (arg1
) = t
; /* side effect! */
820 if (destructor_name_p (name
, t
))
821 error ("use `info method' command to print out value of destructor");
825 for (i
= TYPE_NFN_FIELDS (t
) - 1; i
>= 0; --i
)
827 if (! strcmp (TYPE_FN_FIELDLIST_NAME (t
, i
), name
))
829 error ("use `info method' command to print value of method \"%s\"", name
);
833 if (TYPE_N_BASECLASSES (t
) == 0)
836 t
= TYPE_BASECLASS (t
, 1);
840 error("there is no field named %s", name
);
844 if (destructor_name_p (name
, t
))
848 /* destructors are a special case. */
849 return (value
)value_fn_field (arg1
, 0,
850 TYPE_FN_FIELDLIST_LENGTH (t
, 0));
854 error ("destructor should not have any argument");
858 /* This following loop is for methods with arguments. */
861 /* Look up as method first, because that is where we
862 expect to find it first. */
863 for (i
= TYPE_NFN_FIELDS (t
) - 1; i
>= 0; i
--)
865 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (t
, i
);
867 if (!strcmp (TYPE_FN_FIELDLIST_NAME (t
, i
), name
))
870 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (t
, i
);
873 for (j
= TYPE_FN_FIELDLIST_LENGTH (t
, i
) - 1; j
>= 0; --j
)
875 if (!typecmp (TYPE_FN_FIELD_ARGS (f
, j
), args
))
877 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
878 return (value
)value_virtual_fn_field (arg1
, f
, j
, t
);
880 return (value
)value_fn_field (arg1
, i
, j
);
886 if (TYPE_N_BASECLASSES (t
) == 0)
889 t
= TYPE_BASECLASS (t
, 1);
890 VALUE_TYPE (arg1
) = t
; /* side effect! */
895 error ("Structure method %s not defined for arglist.", name
);
900 /* See if user tried to invoke data as function */
904 for (i
= TYPE_NFIELDS (t
) - 1; i
>= 0; i
--)
906 if (!strcmp (TYPE_FIELD_NAME (t
, i
), name
))
914 return TYPE_FIELD_STATIC (t
, i
)
915 ? value_static_field (t
, name
, i
) : value_field (arg1
, i
);
917 if (TYPE_N_BASECLASSES (t
) == 0)
920 t
= TYPE_BASECLASS (t
, 1);
921 VALUE_TYPE (arg1
) = t
; /* side effect! */
923 error ("Structure has no component named %s.", name
);
927 /* C++: return 1 is NAME is a legitimate name for the destructor
928 of type TYPE. If TYPE does not have a destructor, or
929 if NAME is inappropriate for TYPE, an error is signaled. */
931 destructor_name_p (name
, type
)
935 /* destructors are a special case. */
936 char *dname
= TYPE_NAME (type
);
940 if (! TYPE_HAS_DESTRUCTOR (type
))
941 error ("type `%s' does not have destructor defined",
943 /* Skip past the "struct " at the front. */
944 while (*dname
++ != ' ') ;
945 if (strcmp (dname
, name
+1))
946 error ("destructor specification error");
953 /* C++: Given ARG1, a value of type (pointer to a)* structure/union,
954 return 1 if the component named NAME from the ultimate
955 target structure/union is defined, otherwise, return 0. */
958 check_field (arg1
, name
)
962 register struct type
*t
;
966 struct type
*baseclass
;
970 t
= VALUE_TYPE (arg1
);
972 /* Follow pointers until we get to a non-pointer. */
974 while (TYPE_CODE (t
) == TYPE_CODE_PTR
|| TYPE_CODE (t
) == TYPE_CODE_REF
)
976 arg1
= value_ind (arg1
);
978 t
= VALUE_TYPE (arg1
);
981 if (TYPE_CODE (t
) == TYPE_CODE_MEMBER
)
982 error ("not implemented: member type in check_field");
984 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
985 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
986 error ("Internal error: `this' is not an aggregate");
992 for (i
= TYPE_NFIELDS (t
) - 1; i
>= 0; i
--)
994 if (!strcmp (TYPE_FIELD_NAME (t
, i
), name
))
999 if (TYPE_N_BASECLASSES (t
) == 0)
1002 t
= TYPE_BASECLASS (t
, 1);
1003 VALUE_TYPE (arg1
) = t
; /* side effect! */
1006 /* C++: If it was not found as a data field, then try to
1007 return it as a pointer to a method. */
1009 VALUE_TYPE (arg1
) = t
; /* side effect! */
1011 /* Destructors are a special case. */
1012 if (destructor_name_p (name
, t
))
1017 for (i
= TYPE_NFN_FIELDS (t
) - 1; i
>= 0; --i
)
1019 if (!strcmp (TYPE_FN_FIELDLIST_NAME (t
, i
), name
))
1023 if (TYPE_N_BASECLASSES (t
) == 0)
1026 t
= TYPE_BASECLASS (t
, 1);
1031 /* C++: Given an aggregate type DOMAIN, and a member name NAME,
1032 return the address of this member as a pointer to member
1033 type. If INTYPE is non-null, then it will be the type
1034 of the member we are looking for. This will help us resolve
1035 pointers to member functions. */
1038 value_struct_elt_for_address (domain
, intype
, name
)
1039 struct type
*domain
, *intype
;
1042 register struct type
*t
= domain
;
1047 struct type
*baseclass
;
1049 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
1050 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
1051 error ("Internal error: non-aggregate type to value_struct_elt_for_address");
1057 for (i
= TYPE_NFIELDS (t
) - 1; i
>= 0; i
--)
1059 if (!strcmp (TYPE_FIELD_NAME (t
, i
), name
))
1061 if (TYPE_FIELD_PACKED (t
, i
))
1062 error ("pointers to bitfield members not allowed");
1064 v
= value_from_long (builtin_type_int
,
1065 (LONGEST
) (TYPE_FIELD_BITPOS (t
, i
) >> 3));
1066 VALUE_TYPE (v
) = lookup_pointer_type (
1067 lookup_member_type (TYPE_FIELD_TYPE (t
, i
), baseclass
));
1072 if (TYPE_N_BASECLASSES (t
) == 0)
1075 t
= TYPE_BASECLASS (t
, 1);
1078 /* C++: If it was not found as a data field, then try to
1079 return it as a pointer to a method. */
1082 /* Destructors are a special case. */
1083 if (destructor_name_p (name
, t
))
1085 error ("pointers to destructors not implemented yet");
1088 /* Perform all necessary dereferencing. */
1089 while (intype
&& TYPE_CODE (intype
) == TYPE_CODE_PTR
)
1090 intype
= TYPE_TARGET_TYPE (intype
);
1094 for (i
= TYPE_NFN_FIELDS (t
) - 1; i
>= 0; --i
)
1096 if (!strcmp (TYPE_FN_FIELDLIST_NAME (t
, i
), name
))
1098 int j
= TYPE_FN_FIELDLIST_LENGTH (t
, i
);
1099 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (t
, i
);
1101 if (intype
== 0 && j
> 1)
1102 error ("non-unique member `%s' requires type instantiation", name
);
1106 if (TYPE_FN_FIELD_TYPE (f
, j
) == intype
)
1109 error ("no member function matches that type instantiation");
1114 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
1116 v
= value_from_long (builtin_type_long
,
1117 (LONGEST
) TYPE_FN_FIELD_VOFFSET (f
, j
));
1121 struct symbol
*s
= lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f
, j
),
1122 0, VAR_NAMESPACE
, 0);
1123 v
= locate_var_value (s
, 0);
1125 VALUE_TYPE (v
) = lookup_pointer_type (lookup_member_type (TYPE_FN_FIELD_TYPE (f
, j
), baseclass
));
1130 if (TYPE_N_BASECLASSES (t
) == 0)
1133 t
= TYPE_BASECLASS (t
, 1);
1138 /* Compare two argument lists and return the position in which they differ,
1139 or zero if equal. Note that we ignore the first argument, which is
1140 the type of the instance variable. This is because we want to handle
1141 derived classes. This is not entirely correct: we should actually
1142 check to make sure that a requested operation is type secure,
1150 if (t1
[0]->code
== TYPE_CODE_VOID
) return 0;
1151 if (!t1
[1]) return 0;
1152 for (i
= 1; t1
[i
] && t1
[i
]->code
!= TYPE_CODE_VOID
; i
++)
1155 || t1
[i
]->code
!= t2
[i
]->type
->code
1156 || t1
[i
]->target_type
!= t2
[i
]->type
->target_type
)
1161 if (!t1
[i
]) return 0;
1162 return t2
[i
] ? i
+1 : 0;
1165 /* C++: return the value of the class instance variable, if one exists.
1166 Flag COMPLAIN signals an error if the request is made in an
1167 inappropriate context. */
1169 value_of_this (complain
)
1172 extern FRAME selected_frame
;
1173 struct symbol
*func
, *sym
;
1178 if (selected_frame
== 0)
1180 error ("no frame selected");
1183 func
= get_frame_function (selected_frame
);
1185 funname
= SYMBOL_NAME (func
);
1188 error ("no `this' in nameless context");
1191 b
= SYMBOL_BLOCK_VALUE (func
);
1192 i
= BLOCK_NSYMS (b
);
1195 error ("no args, no `this'");
1198 sym
= BLOCK_SYM (b
, 0);
1199 if (strncmp ("$this", SYMBOL_NAME (sym
), 5))
1201 error ("current stack frame not in method");
1204 return read_var_value (sym
, selected_frame
);