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