Commit | Line | Data |
---|---|---|
bd5635a1 | 1 | /* Perform non-arithmetic operations on values, for GDB. |
e17960fb | 2 | Copyright 1986, 1987, 1989, 1991, 1992 Free Software Foundation, Inc. |
bd5635a1 RP |
3 | |
4 | This file is part of GDB. | |
5 | ||
06b6c733 | 6 | This program is free software; you can redistribute it and/or modify |
bd5635a1 | 7 | it under the terms of the GNU General Public License as published by |
06b6c733 JG |
8 | the Free Software Foundation; either version 2 of the License, or |
9 | (at your option) any later version. | |
bd5635a1 | 10 | |
06b6c733 | 11 | This program is distributed in the hope that it will be useful, |
bd5635a1 RP |
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. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
06b6c733 JG |
17 | along with this program; if not, write to the Free Software |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
bd5635a1 | 19 | |
bd5635a1 | 20 | #include "defs.h" |
bd5635a1 | 21 | #include "symtab.h" |
01be6913 | 22 | #include "gdbtypes.h" |
bd5635a1 RP |
23 | #include "value.h" |
24 | #include "frame.h" | |
25 | #include "inferior.h" | |
26 | #include "gdbcore.h" | |
27 | #include "target.h" | |
28 | ||
29 | #include <errno.h> | |
30 | ||
31 | /* Local functions. */ | |
01be6913 PB |
32 | |
33 | static CORE_ADDR | |
34 | find_function_addr PARAMS ((value, struct type **)); | |
35 | ||
36 | static CORE_ADDR | |
37 | value_push PARAMS ((CORE_ADDR, value)); | |
38 | ||
39 | static CORE_ADDR | |
40 | value_arg_push PARAMS ((CORE_ADDR, value)); | |
41 | ||
42 | static value | |
43 | search_struct_field PARAMS ((char *, value, int, struct type *, int)); | |
44 | ||
45 | static value | |
bac89d6c | 46 | search_struct_method PARAMS ((char *, value *, value *, int, int *, |
01be6913 PB |
47 | struct type *)); |
48 | ||
49 | static int | |
50 | check_field_in PARAMS ((struct type *, const char *)); | |
51 | ||
bd5635a1 RP |
52 | \f |
53 | /* Cast value ARG2 to type TYPE and return as a value. | |
54 | More general than a C cast: accepts any two types of the same length, | |
55 | and if ARG2 is an lvalue it can be cast into anything at all. */ | |
56 | /* In C++, casts may change pointer representations. */ | |
57 | ||
58 | value | |
59 | value_cast (type, arg2) | |
60 | struct type *type; | |
61 | register value arg2; | |
62 | { | |
63 | register enum type_code code1; | |
64 | register enum type_code code2; | |
65 | register int scalar; | |
66 | ||
67 | /* Coerce arrays but not enums. Enums will work as-is | |
68 | and coercing them would cause an infinite recursion. */ | |
69 | if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_ENUM) | |
70 | COERCE_ARRAY (arg2); | |
71 | ||
72 | code1 = TYPE_CODE (type); | |
73 | code2 = TYPE_CODE (VALUE_TYPE (arg2)); | |
74 | scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT | |
75 | || code2 == TYPE_CODE_ENUM); | |
76 | ||
77 | if (code1 == TYPE_CODE_FLT && scalar) | |
78 | return value_from_double (type, value_as_double (arg2)); | |
79 | else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM) | |
80 | && (scalar || code2 == TYPE_CODE_PTR)) | |
06b6c733 | 81 | return value_from_longest (type, value_as_long (arg2)); |
bd5635a1 RP |
82 | else if (TYPE_LENGTH (type) == TYPE_LENGTH (VALUE_TYPE (arg2))) |
83 | { | |
84 | if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR) | |
85 | { | |
86 | /* Look in the type of the source to see if it contains the | |
87 | type of the target as a superclass. If so, we'll need to | |
88 | offset the pointer rather than just change its type. */ | |
89 | struct type *t1 = TYPE_TARGET_TYPE (type); | |
90 | struct type *t2 = TYPE_TARGET_TYPE (VALUE_TYPE (arg2)); | |
2a5ec41d | 91 | if ( TYPE_CODE (t1) == TYPE_CODE_STRUCT |
bd5635a1 RP |
92 | && TYPE_CODE (t2) == TYPE_CODE_STRUCT |
93 | && TYPE_NAME (t1) != 0) /* if name unknown, can't have supercl */ | |
94 | { | |
95 | value v = search_struct_field (type_name_no_tag (t1), | |
d3bab255 | 96 | value_ind (arg2), 0, t2, 1); |
bd5635a1 RP |
97 | if (v) |
98 | { | |
99 | v = value_addr (v); | |
100 | VALUE_TYPE (v) = type; | |
101 | return v; | |
102 | } | |
103 | } | |
104 | /* No superclass found, just fall through to change ptr type. */ | |
105 | } | |
106 | VALUE_TYPE (arg2) = type; | |
107 | return arg2; | |
108 | } | |
109 | else if (VALUE_LVAL (arg2) == lval_memory) | |
110 | { | |
111 | return value_at_lazy (type, VALUE_ADDRESS (arg2) + VALUE_OFFSET (arg2)); | |
112 | } | |
d11c44f1 JG |
113 | else if (code1 == TYPE_CODE_VOID) |
114 | { | |
115 | return value_zero (builtin_type_void, not_lval); | |
116 | } | |
bd5635a1 RP |
117 | else |
118 | { | |
119 | error ("Invalid cast."); | |
120 | return 0; | |
121 | } | |
122 | } | |
123 | ||
124 | /* Create a value of type TYPE that is zero, and return it. */ | |
125 | ||
126 | value | |
127 | value_zero (type, lv) | |
128 | struct type *type; | |
129 | enum lval_type lv; | |
130 | { | |
131 | register value val = allocate_value (type); | |
132 | ||
4ed3a9ea | 133 | memset (VALUE_CONTENTS (val), 0, TYPE_LENGTH (type)); |
bd5635a1 RP |
134 | VALUE_LVAL (val) = lv; |
135 | ||
136 | return val; | |
137 | } | |
138 | ||
139 | /* Return a value with type TYPE located at ADDR. | |
140 | ||
141 | Call value_at only if the data needs to be fetched immediately; | |
142 | if we can be 'lazy' and defer the fetch, perhaps indefinately, call | |
143 | value_at_lazy instead. value_at_lazy simply records the address of | |
144 | the data and sets the lazy-evaluation-required flag. The lazy flag | |
145 | is tested in the VALUE_CONTENTS macro, which is used if and when | |
146 | the contents are actually required. */ | |
147 | ||
148 | value | |
149 | value_at (type, addr) | |
150 | struct type *type; | |
151 | CORE_ADDR addr; | |
152 | { | |
153 | register value val = allocate_value (type); | |
154 | ||
155 | read_memory (addr, VALUE_CONTENTS_RAW (val), TYPE_LENGTH (type)); | |
156 | ||
157 | VALUE_LVAL (val) = lval_memory; | |
158 | VALUE_ADDRESS (val) = addr; | |
159 | ||
160 | return val; | |
161 | } | |
162 | ||
163 | /* Return a lazy value with type TYPE located at ADDR (cf. value_at). */ | |
164 | ||
165 | value | |
166 | value_at_lazy (type, addr) | |
167 | struct type *type; | |
168 | CORE_ADDR addr; | |
169 | { | |
170 | register value val = allocate_value (type); | |
171 | ||
172 | VALUE_LVAL (val) = lval_memory; | |
173 | VALUE_ADDRESS (val) = addr; | |
174 | VALUE_LAZY (val) = 1; | |
175 | ||
176 | return val; | |
177 | } | |
178 | ||
179 | /* Called only from the VALUE_CONTENTS macro, if the current data for | |
180 | a variable needs to be loaded into VALUE_CONTENTS(VAL). Fetches the | |
181 | data from the user's process, and clears the lazy flag to indicate | |
182 | that the data in the buffer is valid. | |
183 | ||
9cb602e1 JG |
184 | If the value is zero-length, we avoid calling read_memory, which would |
185 | abort. We mark the value as fetched anyway -- all 0 bytes of it. | |
186 | ||
bd5635a1 RP |
187 | This function returns a value because it is used in the VALUE_CONTENTS |
188 | macro as part of an expression, where a void would not work. The | |
189 | value is ignored. */ | |
190 | ||
191 | int | |
192 | value_fetch_lazy (val) | |
193 | register value val; | |
194 | { | |
195 | CORE_ADDR addr = VALUE_ADDRESS (val) + VALUE_OFFSET (val); | |
196 | ||
9cb602e1 JG |
197 | if (TYPE_LENGTH (VALUE_TYPE (val))) |
198 | read_memory (addr, VALUE_CONTENTS_RAW (val), | |
199 | TYPE_LENGTH (VALUE_TYPE (val))); | |
bd5635a1 RP |
200 | VALUE_LAZY (val) = 0; |
201 | return 0; | |
202 | } | |
203 | ||
204 | ||
205 | /* Store the contents of FROMVAL into the location of TOVAL. | |
206 | Return a new value with the location of TOVAL and contents of FROMVAL. */ | |
207 | ||
208 | value | |
209 | value_assign (toval, fromval) | |
210 | register value toval, fromval; | |
211 | { | |
212 | register struct type *type = VALUE_TYPE (toval); | |
213 | register value val; | |
214 | char raw_buffer[MAX_REGISTER_RAW_SIZE]; | |
215 | char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE]; | |
216 | int use_buffer = 0; | |
217 | ||
218 | COERCE_ARRAY (fromval); | |
8e9a3f3b | 219 | COERCE_REF (toval); |
bd5635a1 RP |
220 | |
221 | if (VALUE_LVAL (toval) != lval_internalvar) | |
222 | fromval = value_cast (type, fromval); | |
223 | ||
224 | /* If TOVAL is a special machine register requiring conversion | |
225 | of program values to a special raw format, | |
226 | convert FROMVAL's contents now, with result in `raw_buffer', | |
227 | and set USE_BUFFER to the number of bytes to write. */ | |
228 | ||
229 | if (VALUE_REGNO (toval) >= 0 | |
230 | && REGISTER_CONVERTIBLE (VALUE_REGNO (toval))) | |
231 | { | |
232 | int regno = VALUE_REGNO (toval); | |
233 | if (VALUE_TYPE (fromval) != REGISTER_VIRTUAL_TYPE (regno)) | |
234 | fromval = value_cast (REGISTER_VIRTUAL_TYPE (regno), fromval); | |
8e9a3f3b | 235 | memcpy (virtual_buffer, VALUE_CONTENTS (fromval), |
bd5635a1 | 236 | REGISTER_VIRTUAL_SIZE (regno)); |
bac89d6c | 237 | REGISTER_CONVERT_TO_RAW (regno, virtual_buffer, raw_buffer); |
bd5635a1 RP |
238 | use_buffer = REGISTER_RAW_SIZE (regno); |
239 | } | |
240 | ||
241 | switch (VALUE_LVAL (toval)) | |
242 | { | |
243 | case lval_internalvar: | |
244 | set_internalvar (VALUE_INTERNALVAR (toval), fromval); | |
245 | break; | |
246 | ||
247 | case lval_internalvar_component: | |
248 | set_internalvar_component (VALUE_INTERNALVAR (toval), | |
249 | VALUE_OFFSET (toval), | |
250 | VALUE_BITPOS (toval), | |
251 | VALUE_BITSIZE (toval), | |
252 | fromval); | |
253 | break; | |
254 | ||
255 | case lval_memory: | |
256 | if (VALUE_BITSIZE (toval)) | |
257 | { | |
258 | int v; /* FIXME, this won't work for large bitfields */ | |
259 | read_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), | |
01be6913 | 260 | (char *) &v, sizeof v); |
e17960fb | 261 | modify_field ((char *) &v, (int) value_as_long (fromval), |
bd5635a1 RP |
262 | VALUE_BITPOS (toval), VALUE_BITSIZE (toval)); |
263 | write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), | |
264 | (char *)&v, sizeof v); | |
265 | } | |
266 | else if (use_buffer) | |
267 | write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), | |
268 | raw_buffer, use_buffer); | |
269 | else | |
270 | write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), | |
271 | VALUE_CONTENTS (fromval), TYPE_LENGTH (type)); | |
272 | break; | |
273 | ||
274 | case lval_register: | |
275 | if (VALUE_BITSIZE (toval)) | |
276 | { | |
277 | int v; | |
278 | ||
279 | read_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), | |
e17960fb JG |
280 | (char *) &v, sizeof v); |
281 | modify_field ((char *) &v, (int) value_as_long (fromval), | |
bd5635a1 RP |
282 | VALUE_BITPOS (toval), VALUE_BITSIZE (toval)); |
283 | write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), | |
e17960fb | 284 | (char *) &v, sizeof v); |
bd5635a1 RP |
285 | } |
286 | else if (use_buffer) | |
287 | write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), | |
288 | raw_buffer, use_buffer); | |
289 | else | |
290 | write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), | |
291 | VALUE_CONTENTS (fromval), TYPE_LENGTH (type)); | |
292 | break; | |
293 | ||
294 | case lval_reg_frame_relative: | |
295 | { | |
296 | /* value is stored in a series of registers in the frame | |
297 | specified by the structure. Copy that value out, modify | |
298 | it, and copy it back in. */ | |
299 | int amount_to_copy = (VALUE_BITSIZE (toval) ? 1 : TYPE_LENGTH (type)); | |
300 | int reg_size = REGISTER_RAW_SIZE (VALUE_FRAME_REGNUM (toval)); | |
301 | int byte_offset = VALUE_OFFSET (toval) % reg_size; | |
302 | int reg_offset = VALUE_OFFSET (toval) / reg_size; | |
303 | int amount_copied; | |
304 | char *buffer = (char *) alloca (amount_to_copy); | |
305 | int regno; | |
306 | FRAME frame; | |
307 | ||
308 | /* Figure out which frame this is in currently. */ | |
309 | for (frame = get_current_frame (); | |
310 | frame && FRAME_FP (frame) != VALUE_FRAME (toval); | |
311 | frame = get_prev_frame (frame)) | |
312 | ; | |
313 | ||
314 | if (!frame) | |
315 | error ("Value being assigned to is no longer active."); | |
316 | ||
317 | amount_to_copy += (reg_size - amount_to_copy % reg_size); | |
318 | ||
319 | /* Copy it out. */ | |
320 | for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset, | |
321 | amount_copied = 0); | |
322 | amount_copied < amount_to_copy; | |
323 | amount_copied += reg_size, regno++) | |
324 | { | |
325 | get_saved_register (buffer + amount_copied, | |
51b57ded | 326 | (int *)NULL, (CORE_ADDR *)NULL, |
bd5635a1 RP |
327 | frame, regno, (enum lval_type *)NULL); |
328 | } | |
329 | ||
330 | /* Modify what needs to be modified. */ | |
331 | if (VALUE_BITSIZE (toval)) | |
332 | modify_field (buffer + byte_offset, | |
333 | (int) value_as_long (fromval), | |
334 | VALUE_BITPOS (toval), VALUE_BITSIZE (toval)); | |
335 | else if (use_buffer) | |
4ed3a9ea | 336 | memcpy (buffer + byte_offset, raw_buffer, use_buffer); |
bd5635a1 | 337 | else |
4ed3a9ea FF |
338 | memcpy (buffer + byte_offset, VALUE_CONTENTS (fromval), |
339 | TYPE_LENGTH (type)); | |
bd5635a1 RP |
340 | |
341 | /* Copy it back. */ | |
342 | for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset, | |
343 | amount_copied = 0); | |
344 | amount_copied < amount_to_copy; | |
345 | amount_copied += reg_size, regno++) | |
346 | { | |
347 | enum lval_type lval; | |
348 | CORE_ADDR addr; | |
349 | int optim; | |
350 | ||
351 | /* Just find out where to put it. */ | |
352 | get_saved_register ((char *)NULL, | |
353 | &optim, &addr, frame, regno, &lval); | |
354 | ||
355 | if (optim) | |
356 | error ("Attempt to assign to a value that was optimized out."); | |
357 | if (lval == lval_memory) | |
358 | write_memory (addr, buffer + amount_copied, reg_size); | |
359 | else if (lval == lval_register) | |
360 | write_register_bytes (addr, buffer + amount_copied, reg_size); | |
361 | else | |
362 | error ("Attempt to assign to an unmodifiable value."); | |
363 | } | |
364 | } | |
365 | break; | |
366 | ||
367 | ||
368 | default: | |
369 | error ("Left side of = operation is not an lvalue."); | |
370 | } | |
371 | ||
372 | /* Return a value just like TOVAL except with the contents of FROMVAL | |
373 | (except in the case of the type if TOVAL is an internalvar). */ | |
374 | ||
375 | if (VALUE_LVAL (toval) == lval_internalvar | |
376 | || VALUE_LVAL (toval) == lval_internalvar_component) | |
377 | { | |
378 | type = VALUE_TYPE (fromval); | |
379 | } | |
380 | ||
381 | val = allocate_value (type); | |
4ed3a9ea FF |
382 | memcpy (val, toval, VALUE_CONTENTS_RAW (val) - (char *) val); |
383 | memcpy (VALUE_CONTENTS_RAW (val), VALUE_CONTENTS (fromval), | |
384 | TYPE_LENGTH (type)); | |
bd5635a1 RP |
385 | VALUE_TYPE (val) = type; |
386 | ||
387 | return val; | |
388 | } | |
389 | ||
390 | /* Extend a value VAL to COUNT repetitions of its type. */ | |
391 | ||
392 | value | |
393 | value_repeat (arg1, count) | |
394 | value arg1; | |
395 | int count; | |
396 | { | |
397 | register value val; | |
398 | ||
399 | if (VALUE_LVAL (arg1) != lval_memory) | |
400 | error ("Only values in memory can be extended with '@'."); | |
401 | if (count < 1) | |
402 | error ("Invalid number %d of repetitions.", count); | |
403 | ||
404 | val = allocate_repeat_value (VALUE_TYPE (arg1), count); | |
405 | ||
406 | read_memory (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1), | |
407 | VALUE_CONTENTS_RAW (val), | |
408 | TYPE_LENGTH (VALUE_TYPE (val)) * count); | |
409 | VALUE_LVAL (val) = lval_memory; | |
410 | VALUE_ADDRESS (val) = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1); | |
411 | ||
412 | return val; | |
413 | } | |
414 | ||
415 | value | |
416 | value_of_variable (var) | |
417 | struct symbol *var; | |
418 | { | |
419 | value val; | |
420 | ||
421 | val = read_var_value (var, (FRAME) 0); | |
422 | if (val == 0) | |
423 | error ("Address of symbol \"%s\" is unknown.", SYMBOL_NAME (var)); | |
424 | return val; | |
425 | } | |
426 | ||
427 | /* Given a value which is an array, return a value which is | |
06b6c733 JG |
428 | a pointer to its first (actually, zeroth) element. |
429 | FIXME, this should be subtracting the array's lower bound. */ | |
bd5635a1 RP |
430 | |
431 | value | |
432 | value_coerce_array (arg1) | |
433 | value arg1; | |
434 | { | |
435 | register struct type *type; | |
bd5635a1 RP |
436 | |
437 | if (VALUE_LVAL (arg1) != lval_memory) | |
438 | error ("Attempt to take address of value not located in memory."); | |
439 | ||
440 | /* Get type of elements. */ | |
441 | if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_ARRAY) | |
442 | type = TYPE_TARGET_TYPE (VALUE_TYPE (arg1)); | |
443 | else | |
444 | /* A phony array made by value_repeat. | |
445 | Its type is the type of the elements, not an array type. */ | |
446 | type = VALUE_TYPE (arg1); | |
447 | ||
06b6c733 | 448 | return value_from_longest (lookup_pointer_type (type), |
bd5635a1 | 449 | (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1))); |
bd5635a1 RP |
450 | } |
451 | ||
452 | /* Given a value which is a function, return a value which is a pointer | |
453 | to it. */ | |
454 | ||
455 | value | |
456 | value_coerce_function (arg1) | |
457 | value arg1; | |
458 | { | |
bd5635a1 RP |
459 | |
460 | if (VALUE_LVAL (arg1) != lval_memory) | |
461 | error ("Attempt to take address of value not located in memory."); | |
462 | ||
06b6c733 | 463 | return value_from_longest (lookup_pointer_type (VALUE_TYPE (arg1)), |
bd5635a1 | 464 | (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1))); |
bd5635a1 RP |
465 | } |
466 | ||
467 | /* Return a pointer value for the object for which ARG1 is the contents. */ | |
468 | ||
469 | value | |
470 | value_addr (arg1) | |
471 | value arg1; | |
472 | { | |
8e9a3f3b PB |
473 | struct type *type = VALUE_TYPE (arg1); |
474 | if (TYPE_CODE (type) == TYPE_CODE_REF) | |
475 | { | |
476 | /* Copy the value, but change the type from (T&) to (T*). | |
477 | We keep the same location information, which is efficient, | |
478 | and allows &(&X) to get the location containing the reference. */ | |
479 | value arg2 = value_copy (arg1); | |
480 | VALUE_TYPE (arg2) = lookup_pointer_type (TYPE_TARGET_TYPE (type)); | |
481 | return arg2; | |
482 | } | |
bd5635a1 | 483 | if (VALUE_REPEATED (arg1) |
8e9a3f3b | 484 | || TYPE_CODE (type) == TYPE_CODE_ARRAY) |
bd5635a1 | 485 | return value_coerce_array (arg1); |
8e9a3f3b | 486 | if (TYPE_CODE (type) == TYPE_CODE_FUNC) |
bd5635a1 RP |
487 | return value_coerce_function (arg1); |
488 | ||
489 | if (VALUE_LVAL (arg1) != lval_memory) | |
490 | error ("Attempt to take address of value not located in memory."); | |
491 | ||
8e9a3f3b | 492 | return value_from_longest (lookup_pointer_type (type), |
bd5635a1 | 493 | (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1))); |
bd5635a1 RP |
494 | } |
495 | ||
496 | /* Given a value of a pointer type, apply the C unary * operator to it. */ | |
497 | ||
498 | value | |
499 | value_ind (arg1) | |
500 | value arg1; | |
501 | { | |
502 | COERCE_ARRAY (arg1); | |
503 | ||
504 | if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_MEMBER) | |
505 | error ("not implemented: member types in value_ind"); | |
506 | ||
507 | /* Allow * on an integer so we can cast it to whatever we want. | |
508 | This returns an int, which seems like the most C-like thing | |
509 | to do. "long long" variables are rare enough that | |
510 | BUILTIN_TYPE_LONGEST would seem to be a mistake. */ | |
511 | if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT) | |
512 | return value_at (builtin_type_int, | |
513 | (CORE_ADDR) value_as_long (arg1)); | |
514 | else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR) | |
515 | return value_at_lazy (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)), | |
d11c44f1 | 516 | value_as_pointer (arg1)); |
bd5635a1 RP |
517 | error ("Attempt to take contents of a non-pointer value."); |
518 | return 0; /* For lint -- never reached */ | |
519 | } | |
520 | \f | |
521 | /* Pushing small parts of stack frames. */ | |
522 | ||
523 | /* Push one word (the size of object that a register holds). */ | |
524 | ||
525 | CORE_ADDR | |
526 | push_word (sp, buffer) | |
527 | CORE_ADDR sp; | |
528 | REGISTER_TYPE buffer; | |
529 | { | |
530 | register int len = sizeof (REGISTER_TYPE); | |
531 | ||
f2ebc25f | 532 | SWAP_TARGET_AND_HOST (&buffer, len); |
bd5635a1 RP |
533 | #if 1 INNER_THAN 2 |
534 | sp -= len; | |
535 | write_memory (sp, (char *)&buffer, len); | |
536 | #else /* stack grows upward */ | |
537 | write_memory (sp, (char *)&buffer, len); | |
538 | sp += len; | |
539 | #endif /* stack grows upward */ | |
540 | ||
541 | return sp; | |
542 | } | |
543 | ||
544 | /* Push LEN bytes with data at BUFFER. */ | |
545 | ||
546 | CORE_ADDR | |
547 | push_bytes (sp, buffer, len) | |
548 | CORE_ADDR sp; | |
549 | char *buffer; | |
550 | int len; | |
551 | { | |
552 | #if 1 INNER_THAN 2 | |
553 | sp -= len; | |
554 | write_memory (sp, buffer, len); | |
555 | #else /* stack grows upward */ | |
556 | write_memory (sp, buffer, len); | |
557 | sp += len; | |
558 | #endif /* stack grows upward */ | |
559 | ||
560 | return sp; | |
561 | } | |
562 | ||
563 | /* Push onto the stack the specified value VALUE. */ | |
564 | ||
01be6913 | 565 | static CORE_ADDR |
bd5635a1 RP |
566 | value_push (sp, arg) |
567 | register CORE_ADDR sp; | |
568 | value arg; | |
569 | { | |
570 | register int len = TYPE_LENGTH (VALUE_TYPE (arg)); | |
571 | ||
572 | #if 1 INNER_THAN 2 | |
573 | sp -= len; | |
574 | write_memory (sp, VALUE_CONTENTS (arg), len); | |
575 | #else /* stack grows upward */ | |
576 | write_memory (sp, VALUE_CONTENTS (arg), len); | |
577 | sp += len; | |
578 | #endif /* stack grows upward */ | |
579 | ||
580 | return sp; | |
581 | } | |
582 | ||
583 | /* Perform the standard coercions that are specified | |
584 | for arguments to be passed to C functions. */ | |
585 | ||
586 | value | |
587 | value_arg_coerce (arg) | |
588 | value arg; | |
589 | { | |
590 | register struct type *type; | |
591 | ||
592 | COERCE_ENUM (arg); | |
593 | ||
594 | type = VALUE_TYPE (arg); | |
595 | ||
596 | if (TYPE_CODE (type) == TYPE_CODE_INT | |
2a5ec41d | 597 | && TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int)) |
bd5635a1 RP |
598 | return value_cast (builtin_type_int, arg); |
599 | ||
2a5ec41d JG |
600 | if (TYPE_CODE (type) == TYPE_CODE_FLT |
601 | && TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_double)) | |
bd5635a1 RP |
602 | return value_cast (builtin_type_double, arg); |
603 | ||
604 | return arg; | |
605 | } | |
606 | ||
607 | /* Push the value ARG, first coercing it as an argument | |
608 | to a C function. */ | |
609 | ||
01be6913 | 610 | static CORE_ADDR |
bd5635a1 RP |
611 | value_arg_push (sp, arg) |
612 | register CORE_ADDR sp; | |
613 | value arg; | |
614 | { | |
615 | return value_push (sp, value_arg_coerce (arg)); | |
616 | } | |
617 | ||
618 | /* Determine a function's address and its return type from its value. | |
619 | Calls error() if the function is not valid for calling. */ | |
620 | ||
01be6913 | 621 | static CORE_ADDR |
bd5635a1 RP |
622 | find_function_addr (function, retval_type) |
623 | value function; | |
624 | struct type **retval_type; | |
625 | { | |
626 | register struct type *ftype = VALUE_TYPE (function); | |
627 | register enum type_code code = TYPE_CODE (ftype); | |
628 | struct type *value_type; | |
629 | CORE_ADDR funaddr; | |
630 | ||
631 | /* If it's a member function, just look at the function | |
632 | part of it. */ | |
633 | ||
634 | /* Determine address to call. */ | |
635 | if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD) | |
636 | { | |
637 | funaddr = VALUE_ADDRESS (function); | |
638 | value_type = TYPE_TARGET_TYPE (ftype); | |
639 | } | |
640 | else if (code == TYPE_CODE_PTR) | |
641 | { | |
d11c44f1 | 642 | funaddr = value_as_pointer (function); |
bd5635a1 RP |
643 | if (TYPE_CODE (TYPE_TARGET_TYPE (ftype)) == TYPE_CODE_FUNC |
644 | || TYPE_CODE (TYPE_TARGET_TYPE (ftype)) == TYPE_CODE_METHOD) | |
645 | value_type = TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (ftype)); | |
646 | else | |
647 | value_type = builtin_type_int; | |
648 | } | |
649 | else if (code == TYPE_CODE_INT) | |
650 | { | |
651 | /* Handle the case of functions lacking debugging info. | |
652 | Their values are characters since their addresses are char */ | |
653 | if (TYPE_LENGTH (ftype) == 1) | |
d11c44f1 | 654 | funaddr = value_as_pointer (value_addr (function)); |
bd5635a1 RP |
655 | else |
656 | /* Handle integer used as address of a function. */ | |
d11c44f1 | 657 | funaddr = (CORE_ADDR) value_as_long (function); |
bd5635a1 RP |
658 | |
659 | value_type = builtin_type_int; | |
660 | } | |
661 | else | |
662 | error ("Invalid data type for function to be called."); | |
663 | ||
664 | *retval_type = value_type; | |
665 | return funaddr; | |
666 | } | |
667 | ||
668 | #if defined (CALL_DUMMY) | |
669 | /* All this stuff with a dummy frame may seem unnecessarily complicated | |
670 | (why not just save registers in GDB?). The purpose of pushing a dummy | |
671 | frame which looks just like a real frame is so that if you call a | |
672 | function and then hit a breakpoint (get a signal, etc), "backtrace" | |
673 | will look right. Whether the backtrace needs to actually show the | |
674 | stack at the time the inferior function was called is debatable, but | |
675 | it certainly needs to not display garbage. So if you are contemplating | |
676 | making dummy frames be different from normal frames, consider that. */ | |
677 | ||
678 | /* Perform a function call in the inferior. | |
679 | ARGS is a vector of values of arguments (NARGS of them). | |
680 | FUNCTION is a value, the function to be called. | |
681 | Returns a value representing what the function returned. | |
682 | May fail to return, if a breakpoint or signal is hit | |
683 | during the execution of the function. */ | |
684 | ||
685 | value | |
686 | call_function_by_hand (function, nargs, args) | |
687 | value function; | |
688 | int nargs; | |
689 | value *args; | |
690 | { | |
691 | register CORE_ADDR sp; | |
692 | register int i; | |
693 | CORE_ADDR start_sp; | |
f2ebc25f | 694 | /* CALL_DUMMY is an array of words (REGISTER_TYPE), but each word |
84d82b1c | 695 | is in host byte order. It is switched to target byte order before calling |
f2ebc25f | 696 | FIX_CALL_DUMMY. */ |
bd5635a1 RP |
697 | static REGISTER_TYPE dummy[] = CALL_DUMMY; |
698 | REGISTER_TYPE dummy1[sizeof dummy / sizeof (REGISTER_TYPE)]; | |
699 | CORE_ADDR old_sp; | |
700 | struct type *value_type; | |
701 | unsigned char struct_return; | |
702 | CORE_ADDR struct_addr; | |
703 | struct inferior_status inf_status; | |
704 | struct cleanup *old_chain; | |
705 | CORE_ADDR funaddr; | |
706 | int using_gcc; | |
9f739abd | 707 | CORE_ADDR real_pc; |
bd5635a1 | 708 | |
e17960fb JG |
709 | if (!target_has_execution) |
710 | noprocess(); | |
711 | ||
bd5635a1 RP |
712 | save_inferior_status (&inf_status, 1); |
713 | old_chain = make_cleanup (restore_inferior_status, &inf_status); | |
714 | ||
715 | /* PUSH_DUMMY_FRAME is responsible for saving the inferior registers | |
716 | (and POP_FRAME for restoring them). (At least on most machines) | |
717 | they are saved on the stack in the inferior. */ | |
718 | PUSH_DUMMY_FRAME; | |
719 | ||
720 | old_sp = sp = read_register (SP_REGNUM); | |
721 | ||
722 | #if 1 INNER_THAN 2 /* Stack grows down */ | |
723 | sp -= sizeof dummy; | |
724 | start_sp = sp; | |
725 | #else /* Stack grows up */ | |
726 | start_sp = sp; | |
727 | sp += sizeof dummy; | |
728 | #endif | |
729 | ||
730 | funaddr = find_function_addr (function, &value_type); | |
731 | ||
732 | { | |
733 | struct block *b = block_for_pc (funaddr); | |
734 | /* If compiled without -g, assume GCC. */ | |
735 | using_gcc = b == NULL || BLOCK_GCC_COMPILED (b); | |
736 | } | |
737 | ||
738 | /* Are we returning a value using a structure return or a normal | |
739 | value return? */ | |
740 | ||
741 | struct_return = using_struct_return (function, funaddr, value_type, | |
742 | using_gcc); | |
743 | ||
744 | /* Create a call sequence customized for this function | |
745 | and the number of arguments for it. */ | |
4ed3a9ea | 746 | memcpy (dummy1, dummy, sizeof dummy); |
f2ebc25f JK |
747 | for (i = 0; i < sizeof dummy / sizeof (REGISTER_TYPE); i++) |
748 | SWAP_TARGET_AND_HOST (&dummy1[i], sizeof (REGISTER_TYPE)); | |
9f739abd SG |
749 | |
750 | #ifdef GDB_TARGET_IS_HPPA | |
751 | FIX_CALL_DUMMY (dummy1, start_sp, real_pc, funaddr, nargs, args, | |
752 | value_type, using_gcc); | |
753 | #else | |
bd5635a1 RP |
754 | FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, args, |
755 | value_type, using_gcc); | |
9f739abd SG |
756 | real_pc = start_sp; |
757 | #endif | |
bd5635a1 RP |
758 | |
759 | #if CALL_DUMMY_LOCATION == ON_STACK | |
760 | write_memory (start_sp, (char *)dummy1, sizeof dummy); | |
761 | ||
762 | #else /* Not on stack. */ | |
763 | #if CALL_DUMMY_LOCATION == BEFORE_TEXT_END | |
764 | /* Convex Unix prohibits executing in the stack segment. */ | |
765 | /* Hope there is empty room at the top of the text segment. */ | |
766 | { | |
84d82b1c | 767 | extern CORE_ADDR text_end; |
bd5635a1 RP |
768 | static checked = 0; |
769 | if (!checked) | |
770 | for (start_sp = text_end - sizeof dummy; start_sp < text_end; ++start_sp) | |
771 | if (read_memory_integer (start_sp, 1) != 0) | |
772 | error ("text segment full -- no place to put call"); | |
773 | checked = 1; | |
774 | sp = old_sp; | |
775 | start_sp = text_end - sizeof dummy; | |
776 | write_memory (start_sp, (char *)dummy1, sizeof dummy); | |
777 | } | |
778 | #else /* After text_end. */ | |
779 | { | |
84d82b1c | 780 | extern CORE_ADDR text_end; |
bd5635a1 RP |
781 | int errcode; |
782 | sp = old_sp; | |
783 | start_sp = text_end; | |
784 | errcode = target_write_memory (start_sp, (char *)dummy1, sizeof dummy); | |
785 | if (errcode != 0) | |
786 | error ("Cannot write text segment -- call_function failed"); | |
787 | } | |
788 | #endif /* After text_end. */ | |
789 | #endif /* Not on stack. */ | |
790 | ||
791 | #ifdef lint | |
792 | sp = old_sp; /* It really is used, for some ifdef's... */ | |
793 | #endif | |
794 | ||
795 | #ifdef STACK_ALIGN | |
796 | /* If stack grows down, we must leave a hole at the top. */ | |
797 | { | |
798 | int len = 0; | |
799 | ||
800 | /* Reserve space for the return structure to be written on the | |
801 | stack, if necessary */ | |
802 | ||
803 | if (struct_return) | |
804 | len += TYPE_LENGTH (value_type); | |
805 | ||
806 | for (i = nargs - 1; i >= 0; i--) | |
807 | len += TYPE_LENGTH (VALUE_TYPE (value_arg_coerce (args[i]))); | |
808 | #ifdef CALL_DUMMY_STACK_ADJUST | |
809 | len += CALL_DUMMY_STACK_ADJUST; | |
810 | #endif | |
811 | #if 1 INNER_THAN 2 | |
812 | sp -= STACK_ALIGN (len) - len; | |
813 | #else | |
814 | sp += STACK_ALIGN (len) - len; | |
815 | #endif | |
816 | } | |
817 | #endif /* STACK_ALIGN */ | |
818 | ||
819 | /* Reserve space for the return structure to be written on the | |
820 | stack, if necessary */ | |
821 | ||
822 | if (struct_return) | |
823 | { | |
824 | #if 1 INNER_THAN 2 | |
825 | sp -= TYPE_LENGTH (value_type); | |
826 | struct_addr = sp; | |
827 | #else | |
828 | struct_addr = sp; | |
829 | sp += TYPE_LENGTH (value_type); | |
830 | #endif | |
831 | } | |
832 | ||
833 | #if defined (REG_STRUCT_HAS_ADDR) | |
834 | { | |
835 | /* This is a machine like the sparc, where we need to pass a pointer | |
836 | to the structure, not the structure itself. */ | |
837 | if (REG_STRUCT_HAS_ADDR (using_gcc)) | |
838 | for (i = nargs - 1; i >= 0; i--) | |
839 | if (TYPE_CODE (VALUE_TYPE (args[i])) == TYPE_CODE_STRUCT) | |
840 | { | |
841 | CORE_ADDR addr; | |
842 | #if !(1 INNER_THAN 2) | |
843 | /* The stack grows up, so the address of the thing we push | |
844 | is the stack pointer before we push it. */ | |
845 | addr = sp; | |
846 | #endif | |
847 | /* Push the structure. */ | |
848 | sp = value_push (sp, args[i]); | |
849 | #if 1 INNER_THAN 2 | |
850 | /* The stack grows down, so the address of the thing we push | |
851 | is the stack pointer after we push it. */ | |
852 | addr = sp; | |
853 | #endif | |
854 | /* The value we're going to pass is the address of the thing | |
855 | we just pushed. */ | |
06b6c733 JG |
856 | args[i] = value_from_longest (lookup_pointer_type (value_type), |
857 | (LONGEST) addr); | |
bd5635a1 RP |
858 | } |
859 | } | |
860 | #endif /* REG_STRUCT_HAS_ADDR. */ | |
861 | ||
862 | #ifdef PUSH_ARGUMENTS | |
863 | PUSH_ARGUMENTS(nargs, args, sp, struct_return, struct_addr); | |
864 | #else /* !PUSH_ARGUMENTS */ | |
865 | for (i = nargs - 1; i >= 0; i--) | |
866 | sp = value_arg_push (sp, args[i]); | |
867 | #endif /* !PUSH_ARGUMENTS */ | |
868 | ||
869 | #ifdef CALL_DUMMY_STACK_ADJUST | |
870 | #if 1 INNER_THAN 2 | |
871 | sp -= CALL_DUMMY_STACK_ADJUST; | |
872 | #else | |
873 | sp += CALL_DUMMY_STACK_ADJUST; | |
874 | #endif | |
875 | #endif /* CALL_DUMMY_STACK_ADJUST */ | |
876 | ||
877 | /* Store the address at which the structure is supposed to be | |
878 | written. Note that this (and the code which reserved the space | |
879 | above) assumes that gcc was used to compile this function. Since | |
880 | it doesn't cost us anything but space and if the function is pcc | |
881 | it will ignore this value, we will make that assumption. | |
882 | ||
883 | Also note that on some machines (like the sparc) pcc uses a | |
884 | convention like gcc's. */ | |
885 | ||
886 | if (struct_return) | |
887 | STORE_STRUCT_RETURN (struct_addr, sp); | |
888 | ||
889 | /* Write the stack pointer. This is here because the statements above | |
890 | might fool with it. On SPARC, this write also stores the register | |
891 | window into the right place in the new stack frame, which otherwise | |
892 | wouldn't happen. (See write_inferior_registers in sparc-xdep.c.) */ | |
893 | write_register (SP_REGNUM, sp); | |
894 | ||
895 | /* Figure out the value returned by the function. */ | |
896 | { | |
897 | char retbuf[REGISTER_BYTES]; | |
898 | ||
899 | /* Execute the stack dummy routine, calling FUNCTION. | |
900 | When it is done, discard the empty frame | |
901 | after storing the contents of all regs into retbuf. */ | |
9f739abd | 902 | run_stack_dummy (real_pc + CALL_DUMMY_START_OFFSET, retbuf); |
bd5635a1 RP |
903 | |
904 | do_cleanups (old_chain); | |
905 | ||
906 | return value_being_returned (value_type, retbuf, struct_return); | |
907 | } | |
908 | } | |
909 | #else /* no CALL_DUMMY. */ | |
910 | value | |
911 | call_function_by_hand (function, nargs, args) | |
912 | value function; | |
913 | int nargs; | |
914 | value *args; | |
915 | { | |
916 | error ("Cannot invoke functions on this machine."); | |
917 | } | |
918 | #endif /* no CALL_DUMMY. */ | |
919 | \f | |
920 | /* Create a value for a string constant: | |
921 | Call the function malloc in the inferior to get space for it, | |
922 | then copy the data into that space | |
923 | and then return the address with type char *. | |
bac89d6c FF |
924 | PTR points to the string constant data; LEN is number of characters. |
925 | Note that the string may contain embedded null bytes. */ | |
bd5635a1 RP |
926 | |
927 | value | |
928 | value_string (ptr, len) | |
929 | char *ptr; | |
930 | int len; | |
931 | { | |
932 | register value val; | |
933 | register struct symbol *sym; | |
934 | value blocklen; | |
bd5635a1 RP |
935 | |
936 | /* Find the address of malloc in the inferior. */ | |
937 | ||
938 | sym = lookup_symbol ("malloc", 0, VAR_NAMESPACE, 0, NULL); | |
bac89d6c | 939 | if (sym != NULL) |
bd5635a1 RP |
940 | { |
941 | if (SYMBOL_CLASS (sym) != LOC_BLOCK) | |
942 | error ("\"malloc\" exists in this program but is not a function."); | |
943 | val = value_of_variable (sym); | |
944 | } | |
945 | else | |
946 | { | |
01be6913 PB |
947 | struct minimal_symbol *msymbol; |
948 | msymbol = lookup_minimal_symbol ("malloc", (struct objfile *) NULL); | |
949 | if (msymbol != NULL) | |
950 | val = | |
951 | value_from_longest (lookup_pointer_type (lookup_function_type ( | |
952 | lookup_pointer_type (builtin_type_char))), | |
953 | (LONGEST) msymbol -> address); | |
bd5635a1 RP |
954 | else |
955 | error ("String constants require the program to have a function \"malloc\"."); | |
956 | } | |
957 | ||
06b6c733 | 958 | blocklen = value_from_longest (builtin_type_int, (LONGEST) (len + 1)); |
e17960fb | 959 | val = call_function_by_hand (val, 1, &blocklen); |
bac89d6c | 960 | if (value_logical_not (val)) |
bd5635a1 | 961 | error ("No memory available for string constant."); |
bac89d6c | 962 | write_memory (value_as_pointer (val), ptr, len + 1); |
bd5635a1 RP |
963 | VALUE_TYPE (val) = lookup_pointer_type (builtin_type_char); |
964 | return val; | |
965 | } | |
966 | \f | |
967 | /* Helper function used by value_struct_elt to recurse through baseclasses. | |
968 | Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes, | |
2a5ec41d | 969 | and search in it assuming it has (class) type TYPE. |
d3bab255 JK |
970 | If found, return value, else return NULL. |
971 | ||
972 | If LOOKING_FOR_BASECLASS, then instead of looking for struct fields, | |
973 | look for a baseclass named NAME. */ | |
bd5635a1 RP |
974 | |
975 | static value | |
d3bab255 | 976 | search_struct_field (name, arg1, offset, type, looking_for_baseclass) |
bd5635a1 RP |
977 | char *name; |
978 | register value arg1; | |
979 | int offset; | |
980 | register struct type *type; | |
d3bab255 | 981 | int looking_for_baseclass; |
bd5635a1 RP |
982 | { |
983 | int i; | |
984 | ||
985 | check_stub_type (type); | |
986 | ||
d3bab255 JK |
987 | if (! looking_for_baseclass) |
988 | for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--) | |
989 | { | |
990 | char *t_field_name = TYPE_FIELD_NAME (type, i); | |
991 | ||
992 | if (t_field_name && !strcmp (t_field_name, name)) | |
993 | { | |
01be6913 PB |
994 | value v; |
995 | if (TYPE_FIELD_STATIC (type, i)) | |
996 | { | |
997 | char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, i); | |
998 | struct symbol *sym = | |
999 | lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL); | |
1000 | if (! sym) error ( | |
1001 | "Internal error: could not find physical static variable named %s", | |
1002 | phys_name); | |
1003 | v = value_at (TYPE_FIELD_TYPE (type, i), | |
1004 | (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym)); | |
1005 | } | |
1006 | else | |
1007 | v = value_primitive_field (arg1, offset, i, type); | |
d3bab255 JK |
1008 | if (v == 0) |
1009 | error("there is no field named %s", name); | |
1010 | return v; | |
1011 | } | |
1012 | } | |
bd5635a1 RP |
1013 | |
1014 | for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--) | |
1015 | { | |
1016 | value v; | |
1017 | /* If we are looking for baseclasses, this is what we get when we | |
1018 | hit them. */ | |
d3bab255 JK |
1019 | int found_baseclass = (looking_for_baseclass |
1020 | && !strcmp (name, TYPE_BASECLASS_NAME (type, i))); | |
bd5635a1 RP |
1021 | |
1022 | if (BASETYPE_VIA_VIRTUAL (type, i)) | |
1023 | { | |
1024 | value v2; | |
bac89d6c | 1025 | /* Fix to use baseclass_offset instead. FIXME */ |
d11c44f1 JG |
1026 | baseclass_addr (type, i, VALUE_CONTENTS (arg1) + offset, |
1027 | &v2, (int *)NULL); | |
bd5635a1 RP |
1028 | if (v2 == 0) |
1029 | error ("virtual baseclass botch"); | |
1030 | if (found_baseclass) | |
1031 | return v2; | |
d3bab255 JK |
1032 | v = search_struct_field (name, v2, 0, TYPE_BASECLASS (type, i), |
1033 | looking_for_baseclass); | |
bd5635a1 | 1034 | } |
01be6913 | 1035 | else if (found_baseclass) |
bd5635a1 RP |
1036 | v = value_primitive_field (arg1, offset, i, type); |
1037 | else | |
1038 | v = search_struct_field (name, arg1, | |
1039 | offset + TYPE_BASECLASS_BITPOS (type, i) / 8, | |
d3bab255 JK |
1040 | TYPE_BASECLASS (type, i), |
1041 | looking_for_baseclass); | |
bd5635a1 RP |
1042 | if (v) return v; |
1043 | } | |
1044 | return NULL; | |
1045 | } | |
1046 | ||
1047 | /* Helper function used by value_struct_elt to recurse through baseclasses. | |
1048 | Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes, | |
2a5ec41d | 1049 | and search in it assuming it has (class) type TYPE. |
bd5635a1 RP |
1050 | If found, return value, else return NULL. */ |
1051 | ||
1052 | static value | |
bac89d6c | 1053 | search_struct_method (name, arg1p, args, offset, static_memfuncp, type) |
bd5635a1 | 1054 | char *name; |
bac89d6c | 1055 | register value *arg1p, *args; |
bd5635a1 RP |
1056 | int offset, *static_memfuncp; |
1057 | register struct type *type; | |
1058 | { | |
1059 | int i; | |
1060 | ||
1061 | check_stub_type (type); | |
1062 | for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--) | |
1063 | { | |
1064 | char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i); | |
1065 | if (t_field_name && !strcmp (t_field_name, name)) | |
1066 | { | |
d3bab255 | 1067 | int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1; |
bd5635a1 RP |
1068 | struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i); |
1069 | ||
d3bab255 JK |
1070 | if (j > 0 && args == 0) |
1071 | error ("cannot resolve overloaded method `%s'", name); | |
1072 | while (j >= 0) | |
bd5635a1 | 1073 | { |
8e9a3f3b | 1074 | if (TYPE_FN_FIELD_STUB (f, j)) |
bd5635a1 RP |
1075 | check_stub_method (type, i, j); |
1076 | if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j), | |
1077 | TYPE_FN_FIELD_ARGS (f, j), args)) | |
1078 | { | |
1079 | if (TYPE_FN_FIELD_VIRTUAL_P (f, j)) | |
bac89d6c | 1080 | return (value)value_virtual_fn_field (arg1p, f, j, type, offset); |
bd5635a1 RP |
1081 | if (TYPE_FN_FIELD_STATIC_P (f, j) && static_memfuncp) |
1082 | *static_memfuncp = 1; | |
bac89d6c | 1083 | return (value)value_fn_field (arg1p, f, j, type, offset); |
bd5635a1 | 1084 | } |
d3bab255 | 1085 | j--; |
bd5635a1 RP |
1086 | } |
1087 | } | |
1088 | } | |
1089 | ||
1090 | for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--) | |
1091 | { | |
bac89d6c | 1092 | value v; |
01be6913 | 1093 | int base_offset; |
bd5635a1 RP |
1094 | |
1095 | if (BASETYPE_VIA_VIRTUAL (type, i)) | |
1096 | { | |
9f739abd | 1097 | base_offset = baseclass_offset (type, i, *arg1p, offset); |
bac89d6c | 1098 | if (base_offset == -1) |
bd5635a1 | 1099 | error ("virtual baseclass botch"); |
bd5635a1 | 1100 | } |
01be6913 PB |
1101 | else |
1102 | { | |
01be6913 PB |
1103 | base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8; |
1104 | } | |
bac89d6c | 1105 | v = search_struct_method (name, arg1p, args, base_offset + offset, |
bd5635a1 | 1106 | static_memfuncp, TYPE_BASECLASS (type, i)); |
bac89d6c FF |
1107 | if (v) |
1108 | { | |
1109 | /* FIXME-bothner: Why is this commented out? Why is it here? */ | |
1110 | /* *arg1p = arg1_tmp;*/ | |
1111 | return v; | |
1112 | } | |
bd5635a1 RP |
1113 | } |
1114 | return NULL; | |
1115 | } | |
1116 | ||
1117 | /* Given *ARGP, a value of type (pointer to a)* structure/union, | |
1118 | extract the component named NAME from the ultimate target structure/union | |
1119 | and return it as a value with its appropriate type. | |
1120 | ERR is used in the error message if *ARGP's type is wrong. | |
1121 | ||
1122 | C++: ARGS is a list of argument types to aid in the selection of | |
1123 | an appropriate method. Also, handle derived types. | |
1124 | ||
1125 | STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location | |
1126 | where the truthvalue of whether the function that was resolved was | |
1127 | a static member function or not is stored. | |
1128 | ||
1129 | ERR is an error message to be printed in case the field is not found. */ | |
1130 | ||
1131 | value | |
1132 | value_struct_elt (argp, args, name, static_memfuncp, err) | |
1133 | register value *argp, *args; | |
1134 | char *name; | |
1135 | int *static_memfuncp; | |
1136 | char *err; | |
1137 | { | |
1138 | register struct type *t; | |
bd5635a1 RP |
1139 | value v; |
1140 | ||
1141 | COERCE_ARRAY (*argp); | |
1142 | ||
1143 | t = VALUE_TYPE (*argp); | |
1144 | ||
1145 | /* Follow pointers until we get to a non-pointer. */ | |
1146 | ||
1147 | while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF) | |
1148 | { | |
bd5635a1 | 1149 | *argp = value_ind (*argp); |
f2ebc25f JK |
1150 | /* Don't coerce fn pointer to fn and then back again! */ |
1151 | if (TYPE_CODE (VALUE_TYPE (*argp)) != TYPE_CODE_FUNC) | |
1152 | COERCE_ARRAY (*argp); | |
bd5635a1 RP |
1153 | t = VALUE_TYPE (*argp); |
1154 | } | |
1155 | ||
1156 | if (TYPE_CODE (t) == TYPE_CODE_MEMBER) | |
1157 | error ("not implemented: member type in value_struct_elt"); | |
1158 | ||
2a5ec41d | 1159 | if ( TYPE_CODE (t) != TYPE_CODE_STRUCT |
bd5635a1 RP |
1160 | && TYPE_CODE (t) != TYPE_CODE_UNION) |
1161 | error ("Attempt to extract a component of a value that is not a %s.", err); | |
1162 | ||
1163 | /* Assume it's not, unless we see that it is. */ | |
1164 | if (static_memfuncp) | |
1165 | *static_memfuncp =0; | |
1166 | ||
1167 | if (!args) | |
1168 | { | |
1169 | /* if there are no arguments ...do this... */ | |
1170 | ||
d3bab255 | 1171 | /* Try as a field first, because if we succeed, there |
bd5635a1 | 1172 | is less work to be done. */ |
d3bab255 | 1173 | v = search_struct_field (name, *argp, 0, t, 0); |
bd5635a1 RP |
1174 | if (v) |
1175 | return v; | |
1176 | ||
1177 | /* C++: If it was not found as a data field, then try to | |
1178 | return it as a pointer to a method. */ | |
1179 | ||
1180 | if (destructor_name_p (name, t)) | |
1181 | error ("Cannot get value of destructor"); | |
1182 | ||
bac89d6c | 1183 | v = search_struct_method (name, argp, args, 0, static_memfuncp, t); |
bd5635a1 RP |
1184 | |
1185 | if (v == 0) | |
1186 | { | |
1187 | if (TYPE_NFN_FIELDS (t)) | |
1188 | error ("There is no member or method named %s.", name); | |
1189 | else | |
1190 | error ("There is no member named %s.", name); | |
1191 | } | |
1192 | return v; | |
1193 | } | |
1194 | ||
1195 | if (destructor_name_p (name, t)) | |
1196 | { | |
1197 | if (!args[1]) | |
1198 | { | |
1199 | /* destructors are a special case. */ | |
bac89d6c FF |
1200 | return (value)value_fn_field (NULL, TYPE_FN_FIELDLIST1 (t, 0), |
1201 | TYPE_FN_FIELDLIST_LENGTH (t, 0), | |
1202 | 0, 0); | |
bd5635a1 RP |
1203 | } |
1204 | else | |
1205 | { | |
1206 | error ("destructor should not have any argument"); | |
1207 | } | |
1208 | } | |
1209 | else | |
bac89d6c | 1210 | v = search_struct_method (name, argp, args, 0, static_memfuncp, t); |
bd5635a1 RP |
1211 | |
1212 | if (v == 0) | |
1213 | { | |
1214 | /* See if user tried to invoke data as function. If so, | |
1215 | hand it back. If it's not callable (i.e., a pointer to function), | |
1216 | gdb should give an error. */ | |
d3bab255 | 1217 | v = search_struct_field (name, *argp, 0, t, 0); |
bd5635a1 RP |
1218 | } |
1219 | ||
1220 | if (!v) | |
1221 | error ("Structure has no component named %s.", name); | |
1222 | return v; | |
1223 | } | |
1224 | ||
1225 | /* C++: return 1 is NAME is a legitimate name for the destructor | |
1226 | of type TYPE. If TYPE does not have a destructor, or | |
1227 | if NAME is inappropriate for TYPE, an error is signaled. */ | |
1228 | int | |
1229 | destructor_name_p (name, type) | |
7919c3ed JG |
1230 | const char *name; |
1231 | const struct type *type; | |
bd5635a1 RP |
1232 | { |
1233 | /* destructors are a special case. */ | |
1234 | ||
1235 | if (name[0] == '~') | |
1236 | { | |
1237 | char *dname = type_name_no_tag (type); | |
bd5635a1 RP |
1238 | if (strcmp (dname, name+1)) |
1239 | error ("name of destructor must equal name of class"); | |
1240 | else | |
1241 | return 1; | |
1242 | } | |
1243 | return 0; | |
1244 | } | |
1245 | ||
1246 | /* Helper function for check_field: Given TYPE, a structure/union, | |
1247 | return 1 if the component named NAME from the ultimate | |
1248 | target structure/union is defined, otherwise, return 0. */ | |
1249 | ||
1250 | static int | |
1251 | check_field_in (type, name) | |
1252 | register struct type *type; | |
01be6913 | 1253 | const char *name; |
bd5635a1 RP |
1254 | { |
1255 | register int i; | |
1256 | ||
1257 | for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--) | |
1258 | { | |
1259 | char *t_field_name = TYPE_FIELD_NAME (type, i); | |
1260 | if (t_field_name && !strcmp (t_field_name, name)) | |
1261 | return 1; | |
1262 | } | |
1263 | ||
1264 | /* C++: If it was not found as a data field, then try to | |
1265 | return it as a pointer to a method. */ | |
1266 | ||
1267 | /* Destructors are a special case. */ | |
1268 | if (destructor_name_p (name, type)) | |
1269 | return 1; | |
1270 | ||
1271 | for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i) | |
1272 | { | |
1273 | if (!strcmp (TYPE_FN_FIELDLIST_NAME (type, i), name)) | |
1274 | return 1; | |
1275 | } | |
1276 | ||
1277 | for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--) | |
1278 | if (check_field_in (TYPE_BASECLASS (type, i), name)) | |
1279 | return 1; | |
1280 | ||
1281 | return 0; | |
1282 | } | |
1283 | ||
1284 | ||
1285 | /* C++: Given ARG1, a value of type (pointer to a)* structure/union, | |
1286 | return 1 if the component named NAME from the ultimate | |
1287 | target structure/union is defined, otherwise, return 0. */ | |
1288 | ||
1289 | int | |
1290 | check_field (arg1, name) | |
01be6913 | 1291 | register value arg1; |
7919c3ed | 1292 | const char *name; |
bd5635a1 RP |
1293 | { |
1294 | register struct type *t; | |
1295 | ||
1296 | COERCE_ARRAY (arg1); | |
1297 | ||
1298 | t = VALUE_TYPE (arg1); | |
1299 | ||
1300 | /* Follow pointers until we get to a non-pointer. */ | |
1301 | ||
1302 | while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF) | |
1303 | t = TYPE_TARGET_TYPE (t); | |
1304 | ||
1305 | if (TYPE_CODE (t) == TYPE_CODE_MEMBER) | |
1306 | error ("not implemented: member type in check_field"); | |
1307 | ||
2a5ec41d | 1308 | if ( TYPE_CODE (t) != TYPE_CODE_STRUCT |
bd5635a1 RP |
1309 | && TYPE_CODE (t) != TYPE_CODE_UNION) |
1310 | error ("Internal error: `this' is not an aggregate"); | |
1311 | ||
1312 | return check_field_in (t, name); | |
1313 | } | |
1314 | ||
01be6913 | 1315 | /* C++: Given an aggregate type CURTYPE, and a member name NAME, |
2a5ec41d | 1316 | return the address of this member as a "pointer to member" |
bd5635a1 RP |
1317 | type. If INTYPE is non-null, then it will be the type |
1318 | of the member we are looking for. This will help us resolve | |
01be6913 PB |
1319 | "pointers to member functions". This function is used |
1320 | to resolve user expressions of the form "DOMAIN::NAME". */ | |
bd5635a1 RP |
1321 | |
1322 | value | |
51b57ded | 1323 | value_struct_elt_for_reference (domain, offset, curtype, name, intype) |
01be6913 | 1324 | struct type *domain, *curtype, *intype; |
51b57ded | 1325 | int offset; |
bd5635a1 RP |
1326 | char *name; |
1327 | { | |
01be6913 | 1328 | register struct type *t = curtype; |
bd5635a1 RP |
1329 | register int i; |
1330 | value v; | |
1331 | ||
2a5ec41d | 1332 | if ( TYPE_CODE (t) != TYPE_CODE_STRUCT |
bd5635a1 | 1333 | && TYPE_CODE (t) != TYPE_CODE_UNION) |
01be6913 | 1334 | error ("Internal error: non-aggregate type to value_struct_elt_for_reference"); |
bd5635a1 | 1335 | |
01be6913 | 1336 | for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--) |
bd5635a1 | 1337 | { |
01be6913 PB |
1338 | char *t_field_name = TYPE_FIELD_NAME (t, i); |
1339 | ||
1340 | if (t_field_name && !strcmp (t_field_name, name)) | |
bd5635a1 | 1341 | { |
01be6913 | 1342 | if (TYPE_FIELD_STATIC (t, i)) |
bd5635a1 | 1343 | { |
01be6913 PB |
1344 | char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (t, i); |
1345 | struct symbol *sym = | |
1346 | lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL); | |
1347 | if (! sym) | |
1348 | error ( | |
1349 | "Internal error: could not find physical static variable named %s", | |
1350 | phys_name); | |
1351 | return value_at (SYMBOL_TYPE (sym), | |
1352 | (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym)); | |
bd5635a1 | 1353 | } |
01be6913 PB |
1354 | if (TYPE_FIELD_PACKED (t, i)) |
1355 | error ("pointers to bitfield members not allowed"); | |
1356 | ||
1357 | return value_from_longest | |
1358 | (lookup_reference_type (lookup_member_type (TYPE_FIELD_TYPE (t, i), | |
1359 | domain)), | |
51b57ded | 1360 | offset + (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3)); |
bd5635a1 | 1361 | } |
bd5635a1 RP |
1362 | } |
1363 | ||
1364 | /* C++: If it was not found as a data field, then try to | |
1365 | return it as a pointer to a method. */ | |
bd5635a1 RP |
1366 | |
1367 | /* Destructors are a special case. */ | |
1368 | if (destructor_name_p (name, t)) | |
1369 | { | |
2a5ec41d | 1370 | error ("member pointers to destructors not implemented yet"); |
bd5635a1 RP |
1371 | } |
1372 | ||
1373 | /* Perform all necessary dereferencing. */ | |
1374 | while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR) | |
1375 | intype = TYPE_TARGET_TYPE (intype); | |
1376 | ||
01be6913 | 1377 | for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i) |
bd5635a1 | 1378 | { |
01be6913 | 1379 | if (!strcmp (TYPE_FN_FIELDLIST_NAME (t, i), name)) |
bd5635a1 | 1380 | { |
01be6913 PB |
1381 | int j = TYPE_FN_FIELDLIST_LENGTH (t, i); |
1382 | struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i); | |
1383 | ||
1384 | if (intype == 0 && j > 1) | |
1385 | error ("non-unique member `%s' requires type instantiation", name); | |
1386 | if (intype) | |
bd5635a1 | 1387 | { |
01be6913 PB |
1388 | while (j--) |
1389 | if (TYPE_FN_FIELD_TYPE (f, j) == intype) | |
1390 | break; | |
1391 | if (j < 0) | |
1392 | error ("no member function matches that type instantiation"); | |
1393 | } | |
1394 | else | |
1395 | j = 0; | |
1396 | ||
1397 | if (TYPE_FN_FIELD_STUB (f, j)) | |
1398 | check_stub_method (t, i, j); | |
1399 | if (TYPE_FN_FIELD_VIRTUAL_P (f, j)) | |
1400 | { | |
1401 | return value_from_longest | |
1402 | (lookup_reference_type | |
1403 | (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j), | |
1404 | domain)), | |
bac89d6c FF |
1405 | (LONGEST) METHOD_PTR_FROM_VOFFSET |
1406 | (TYPE_FN_FIELD_VOFFSET (f, j))); | |
01be6913 PB |
1407 | } |
1408 | else | |
1409 | { | |
1410 | struct symbol *s = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j), | |
1411 | 0, VAR_NAMESPACE, 0, NULL); | |
35fcebce PB |
1412 | if (s == NULL) |
1413 | { | |
1414 | v = 0; | |
1415 | } | |
1416 | else | |
1417 | { | |
1418 | v = read_var_value (s, 0); | |
01be6913 | 1419 | #if 0 |
35fcebce PB |
1420 | VALUE_TYPE (v) = lookup_reference_type |
1421 | (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j), | |
1422 | domain)); | |
01be6913 | 1423 | #endif |
bd5635a1 | 1424 | } |
35fcebce | 1425 | return v; |
bd5635a1 RP |
1426 | } |
1427 | } | |
35fcebce | 1428 | } |
01be6913 PB |
1429 | for (i = TYPE_N_BASECLASSES (t) - 1; i >= 0; i--) |
1430 | { | |
51b57ded FF |
1431 | value v; |
1432 | int base_offset; | |
1433 | ||
1434 | if (BASETYPE_VIA_VIRTUAL (t, i)) | |
1435 | base_offset = 0; | |
1436 | else | |
1437 | base_offset = TYPE_BASECLASS_BITPOS (t, i) / 8; | |
01be6913 | 1438 | v = value_struct_elt_for_reference (domain, |
51b57ded | 1439 | offset + base_offset, |
01be6913 PB |
1440 | TYPE_BASECLASS (t, i), |
1441 | name, | |
1442 | intype); | |
1443 | if (v) | |
1444 | return v; | |
bd5635a1 RP |
1445 | } |
1446 | return 0; | |
1447 | } | |
1448 | ||
1449 | /* Compare two argument lists and return the position in which they differ, | |
1450 | or zero if equal. | |
1451 | ||
1452 | STATICP is nonzero if the T1 argument list came from a | |
1453 | static member function. | |
1454 | ||
1455 | For non-static member functions, we ignore the first argument, | |
1456 | which is the type of the instance variable. This is because we want | |
1457 | to handle calls with objects from derived classes. This is not | |
1458 | entirely correct: we should actually check to make sure that a | |
1459 | requested operation is type secure, shouldn't we? FIXME. */ | |
1460 | ||
1461 | int | |
1462 | typecmp (staticp, t1, t2) | |
1463 | int staticp; | |
1464 | struct type *t1[]; | |
1465 | value t2[]; | |
1466 | { | |
1467 | int i; | |
1468 | ||
d3bab255 JK |
1469 | if (t2 == 0) |
1470 | return 1; | |
bd5635a1 RP |
1471 | if (staticp && t1 == 0) |
1472 | return t2[1] != 0; | |
1473 | if (t1 == 0) | |
1474 | return 1; | |
1475 | if (t1[0]->code == TYPE_CODE_VOID) return 0; | |
1476 | if (t1[!staticp] == 0) return 0; | |
1477 | for (i = !staticp; t1[i] && t1[i]->code != TYPE_CODE_VOID; i++) | |
1478 | { | |
1479 | if (! t2[i] | |
1480 | || t1[i]->code != t2[i]->type->code | |
1481 | /* Too pessimistic: || t1[i]->target_type != t2[i]->type->target_type */ | |
1482 | ) | |
1483 | return i+1; | |
1484 | } | |
1485 | if (!t1[i]) return 0; | |
1486 | return t2[i] ? i+1 : 0; | |
1487 | } | |
1488 | ||
1489 | /* C++: return the value of the class instance variable, if one exists. | |
1490 | Flag COMPLAIN signals an error if the request is made in an | |
1491 | inappropriate context. */ | |
1492 | value | |
1493 | value_of_this (complain) | |
1494 | int complain; | |
1495 | { | |
1496 | extern FRAME selected_frame; | |
1497 | struct symbol *func, *sym; | |
1498 | struct block *b; | |
1499 | int i; | |
1500 | static const char funny_this[] = "this"; | |
1501 | value this; | |
bd5635a1 RP |
1502 | |
1503 | if (selected_frame == 0) | |
1504 | if (complain) | |
1505 | error ("no frame selected"); | |
1506 | else return 0; | |
1507 | ||
1508 | func = get_frame_function (selected_frame); | |
1509 | if (!func) | |
1510 | { | |
1511 | if (complain) | |
1512 | error ("no `this' in nameless context"); | |
1513 | else return 0; | |
1514 | } | |
1515 | ||
1516 | b = SYMBOL_BLOCK_VALUE (func); | |
1517 | i = BLOCK_NSYMS (b); | |
1518 | if (i <= 0) | |
1519 | if (complain) | |
1520 | error ("no args, no `this'"); | |
1521 | else return 0; | |
1522 | ||
1523 | /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER | |
1524 | symbol instead of the LOC_ARG one (if both exist). */ | |
1525 | sym = lookup_block_symbol (b, funny_this, VAR_NAMESPACE); | |
1526 | if (sym == NULL) | |
1527 | { | |
1528 | if (complain) | |
1529 | error ("current stack frame not in method"); | |
1530 | else | |
1531 | return NULL; | |
1532 | } | |
1533 | ||
1534 | this = read_var_value (sym, selected_frame); | |
1535 | if (this == 0 && complain) | |
1536 | error ("`this' argument at unknown address"); | |
1537 | return this; | |
1538 | } |