]>
Commit | Line | Data |
---|---|---|
7b4ac7e1 | 1 | /* Perform non-arithmetic operations on values, for GDB. |
2 | Copyright (C) 1986, 1987 Free Software Foundation, Inc. | |
3 | ||
4 | GDB is distributed in the hope that it will be useful, but WITHOUT ANY | |
5 | WARRANTY. No author or distributor accepts responsibility to anyone | |
6 | for the consequences of using it or for whether it serves any | |
7 | particular purpose or works at all, unless he says so in writing. | |
8 | Refer to the GDB General Public License for full details. | |
9 | ||
10 | Everyone is granted permission to copy, modify and redistribute GDB, | |
11 | but only under the conditions described in the GDB General Public | |
12 | License. A copy of this license is supposed to have been given to you | |
13 | along with GDB so you can know your rights and responsibilities. It | |
14 | should be in a file named COPYING. Among other things, the copyright | |
15 | notice and this notice must be preserved on all copies. | |
16 | ||
17 | In other words, go ahead and share GDB, but don't try to stop | |
18 | anyone else from sharing it farther. Help stamp out software hoarding! | |
19 | */ | |
632ea0cc | 20 | #include "stdio.h" |
7b4ac7e1 | 21 | #include "defs.h" |
22 | #include "initialize.h" | |
23 | #include "param.h" | |
24 | #include "symtab.h" | |
25 | #include "value.h" | |
26 | ||
27 | START_FILE | |
28 | \f | |
29 | /* Cast value ARG2 to type TYPE and return as a value. | |
30 | More general than a C cast: accepts any two types of the same length, | |
31 | and if ARG2 is an lvalue it can be cast into anything at all. */ | |
32 | ||
33 | value | |
34 | value_cast (type, arg2) | |
35 | struct type *type; | |
36 | register value arg2; | |
37 | { | |
38 | register enum type_code code1; | |
39 | register enum type_code code2; | |
40 | register int scalar; | |
41 | ||
42 | /* Coerce arrays but not enums. Enums will work as-is | |
43 | and coercing them would cause an infinite recursion. */ | |
44 | if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_ENUM) | |
45 | COERCE_ARRAY (arg2); | |
46 | ||
47 | code1 = TYPE_CODE (type); | |
48 | code2 = TYPE_CODE (VALUE_TYPE (arg2)); | |
49 | scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT | |
50 | || code2 == TYPE_CODE_ENUM); | |
51 | ||
52 | if (code1 == TYPE_CODE_FLT && scalar) | |
53 | return value_from_double (type, value_as_double (arg2)); | |
54 | else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM) | |
55 | && (scalar || code2 == TYPE_CODE_PTR)) | |
56 | return value_from_long (type, value_as_long (arg2)); | |
57 | else if (TYPE_LENGTH (type) == TYPE_LENGTH (VALUE_TYPE (arg2))) | |
58 | { | |
632ea0cc | 59 | if ((code1 == TYPE_CODE_MPTR) ^ (code2 == TYPE_CODE_MPTR)) |
60 | printf ("warning: assignment between pointer-to-member and non pointer-to-member types\n"); | |
61 | ||
7b4ac7e1 | 62 | VALUE_TYPE (arg2) = type; |
63 | return arg2; | |
64 | } | |
65 | else if (VALUE_LVAL (arg2) == lval_memory) | |
632ea0cc | 66 | { |
67 | if ((code1 == TYPE_CODE_MPTR) ^ (code2 == TYPE_CODE_MPTR)) | |
68 | printf ("warning: assignment between pointer-to-member and non pointer-to-member types\n"); | |
69 | ||
70 | return value_at (type, VALUE_ADDRESS (arg2) + VALUE_OFFSET (arg2)); | |
71 | } | |
7b4ac7e1 | 72 | else |
73 | error ("Invalid cast."); | |
74 | } | |
75 | ||
76 | /* Return the value with a specified type located at specified address. */ | |
77 | ||
78 | value | |
79 | value_at (type, addr) | |
80 | struct type *type; | |
81 | CORE_ADDR addr; | |
82 | { | |
83 | register value val = allocate_value (type); | |
84 | ||
85 | read_memory (addr, VALUE_CONTENTS (val), TYPE_LENGTH (type)); | |
86 | VALUE_LVAL (val) = lval_memory; | |
87 | VALUE_ADDRESS (val) = addr; | |
88 | ||
89 | return val; | |
90 | } | |
91 | ||
92 | /* Store the contents of FROMVAL into the location of TOVAL. | |
93 | Return a new value with the location of TOVAL and contents of FROMVAL. */ | |
94 | ||
95 | value | |
96 | value_assign (toval, fromval) | |
97 | register value toval, fromval; | |
98 | { | |
99 | register struct type *type = VALUE_TYPE (toval); | |
100 | register value val; | |
101 | char raw_buffer[MAX_REGISTER_RAW_SIZE]; | |
102 | char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE]; | |
103 | int use_buffer = 0; | |
104 | ||
105 | COERCE_ARRAY (fromval); | |
106 | ||
107 | if (VALUE_LVAL (toval) != lval_internalvar) | |
108 | fromval = value_cast (type, fromval); | |
109 | ||
110 | /* If TOVAL is a special machine register requiring conversion | |
111 | of program values to a special raw format, | |
112 | convert FROMVAL's contents now, with result in `raw_buffer', | |
113 | and set USE_BUFFER to the number of bytes to write. */ | |
114 | ||
115 | if (VALUE_REGNO (toval) >= 0 | |
116 | && REGISTER_CONVERTIBLE (VALUE_REGNO (toval))) | |
117 | { | |
118 | int regno = VALUE_REGNO (toval); | |
119 | if (VALUE_TYPE (fromval) != REGISTER_VIRTUAL_TYPE (regno)) | |
120 | fromval = value_cast (REGISTER_VIRTUAL_TYPE (regno), fromval); | |
121 | bcopy (VALUE_CONTENTS (fromval), virtual_buffer, | |
122 | REGISTER_VIRTUAL_SIZE (regno)); | |
123 | REGISTER_CONVERT_TO_RAW (regno, virtual_buffer, raw_buffer); | |
124 | use_buffer = REGISTER_RAW_SIZE (regno); | |
125 | } | |
126 | ||
127 | switch (VALUE_LVAL (toval)) | |
128 | { | |
129 | case lval_internalvar: | |
130 | set_internalvar (VALUE_INTERNALVAR (toval), fromval); | |
131 | break; | |
132 | ||
133 | case lval_internalvar_component: | |
134 | set_internalvar_component (VALUE_INTERNALVAR (toval), | |
135 | VALUE_OFFSET (toval), | |
136 | VALUE_BITPOS (toval), | |
137 | VALUE_BITSIZE (toval), | |
138 | fromval); | |
139 | break; | |
140 | ||
141 | case lval_memory: | |
142 | if (VALUE_BITSIZE (toval)) | |
143 | { | |
144 | int val; | |
145 | read_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), | |
146 | &val, sizeof val); | |
147 | modify_field (&val, value_as_long (fromval), | |
148 | VALUE_BITPOS (toval), VALUE_BITSIZE (toval)); | |
149 | write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), | |
150 | &val, sizeof val); | |
151 | } | |
152 | else if (use_buffer) | |
153 | write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), | |
154 | raw_buffer, use_buffer); | |
155 | else | |
156 | write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), | |
157 | VALUE_CONTENTS (fromval), TYPE_LENGTH (type)); | |
158 | break; | |
159 | ||
160 | case lval_register: | |
161 | if (VALUE_BITSIZE (toval)) | |
162 | { | |
163 | int val; | |
164 | ||
165 | read_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), | |
166 | &val, sizeof val); | |
167 | modify_field (&val, value_as_long (fromval), | |
168 | VALUE_BITPOS (toval), VALUE_BITSIZE (toval)); | |
169 | write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), | |
170 | &val, sizeof val); | |
171 | } | |
172 | else if (use_buffer) | |
173 | write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), | |
174 | raw_buffer, use_buffer); | |
175 | else | |
176 | write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), | |
177 | VALUE_CONTENTS (fromval), TYPE_LENGTH (type)); | |
178 | break; | |
179 | ||
180 | default: | |
181 | error ("Left side of = operation is not an lvalue."); | |
182 | } | |
183 | ||
184 | /* Return a value just like TOVAL except with the contents of FROMVAL. */ | |
185 | ||
186 | val = allocate_value (type); | |
187 | bcopy (toval, val, VALUE_CONTENTS (val) - (char *) val); | |
188 | bcopy (VALUE_CONTENTS (fromval), VALUE_CONTENTS (val), TYPE_LENGTH (type)); | |
189 | ||
190 | return val; | |
191 | } | |
192 | ||
193 | /* Extend a value VAL to COUNT repetitions of its type. */ | |
194 | ||
195 | value | |
196 | value_repeat (arg1, count) | |
197 | value arg1; | |
198 | int count; | |
199 | { | |
200 | register value val; | |
201 | ||
202 | if (VALUE_LVAL (arg1) != lval_memory) | |
203 | error ("Only values in memory can be extended with '@'."); | |
204 | if (count < 1) | |
205 | error ("Invalid number %d of repetitions.", count); | |
206 | ||
207 | val = allocate_repeat_value (VALUE_TYPE (arg1), count); | |
208 | ||
209 | read_memory (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1), | |
210 | VALUE_CONTENTS (val), | |
211 | TYPE_LENGTH (VALUE_TYPE (val)) * count); | |
212 | VALUE_LVAL (val) = lval_memory; | |
213 | VALUE_ADDRESS (val) = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1); | |
214 | ||
215 | return val; | |
216 | } | |
217 | ||
218 | value | |
219 | value_of_variable (var) | |
220 | struct symbol *var; | |
221 | { | |
222 | return read_var_value (var, (CORE_ADDR) 0); | |
223 | } | |
224 | ||
225 | /* Given a value which is an array, return a value which is | |
226 | a pointer to its first element. */ | |
227 | ||
228 | value | |
229 | value_coerce_array (arg1) | |
230 | value arg1; | |
231 | { | |
232 | register struct type *type; | |
233 | register value val; | |
234 | ||
235 | if (VALUE_LVAL (arg1) != lval_memory) | |
236 | error ("Attempt to take address of value not located in memory."); | |
237 | ||
238 | /* Get type of elements. */ | |
239 | if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_ARRAY) | |
240 | type = TYPE_TARGET_TYPE (VALUE_TYPE (arg1)); | |
241 | else | |
242 | /* A phony array made by value_repeat. | |
243 | Its type is the type of the elements, not an array type. */ | |
244 | type = VALUE_TYPE (arg1); | |
245 | ||
246 | /* Get the type of the result. */ | |
247 | type = lookup_pointer_type (type); | |
248 | val = value_from_long (builtin_type_long, | |
249 | VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)); | |
250 | VALUE_TYPE (val) = type; | |
251 | return val; | |
252 | } | |
253 | ||
254 | /* Return a pointer value for the object for which ARG1 is the contents. */ | |
255 | ||
256 | value | |
257 | value_addr (arg1) | |
258 | value arg1; | |
259 | { | |
260 | register struct type *type; | |
261 | register value val, arg1_coerced; | |
262 | ||
263 | /* Taking the address of an array is really a no-op | |
264 | once the array is coerced to a pointer to its first element. */ | |
265 | arg1_coerced = arg1; | |
266 | COERCE_ARRAY (arg1_coerced); | |
267 | if (arg1 != arg1_coerced) | |
268 | return arg1_coerced; | |
269 | ||
270 | if (VALUE_LVAL (arg1) != lval_memory) | |
271 | error ("Attempt to take address of value not located in memory."); | |
272 | ||
273 | /* Get the type of the result. */ | |
274 | type = lookup_pointer_type (VALUE_TYPE (arg1)); | |
275 | val = value_from_long (builtin_type_long, | |
276 | VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)); | |
277 | VALUE_TYPE (val) = type; | |
278 | return val; | |
279 | } | |
280 | ||
281 | /* Given a value of a pointer type, apply the C unary * operator to it. */ | |
282 | ||
283 | value | |
284 | value_ind (arg1) | |
285 | value arg1; | |
286 | { | |
287 | COERCE_ARRAY (arg1); | |
288 | ||
632ea0cc | 289 | if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_MPTR) |
290 | error ("not implemented: member pointers in value_ind"); | |
291 | ||
7b4ac7e1 | 292 | /* Allow * on an integer so we can cast it to whatever we want. */ |
293 | if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT) | |
294 | return value_at (builtin_type_long, | |
295 | (CORE_ADDR) value_as_long (arg1)); | |
296 | else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR) | |
297 | return value_at (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)), | |
298 | (CORE_ADDR) value_as_long (arg1)); | |
632ea0cc | 299 | else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF) |
300 | return value_at (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)), | |
301 | (CORE_ADDR) value_as_long (arg1)); | |
7b4ac7e1 | 302 | error ("Attempt to take contents of a non-pointer value."); |
303 | } | |
304 | \f | |
305 | /* Pushing small parts of stack frames. */ | |
306 | ||
307 | /* Push one word (the size of object that a register holds). */ | |
308 | ||
309 | CORE_ADDR | |
310 | push_word (sp, buffer) | |
311 | CORE_ADDR sp; | |
312 | REGISTER_TYPE buffer; | |
313 | { | |
314 | register int len = sizeof (REGISTER_TYPE); | |
315 | ||
316 | #if 1 INNER_THAN 2 | |
317 | sp -= len; | |
318 | write_memory (sp, &buffer, len); | |
319 | #else /* stack grows upward */ | |
320 | write_memory (sp, &buffer, len); | |
321 | sp += len; | |
322 | #endif /* stack grows upward */ | |
323 | ||
324 | return sp; | |
325 | } | |
326 | ||
327 | /* Push LEN bytes with data at BUFFER. */ | |
328 | ||
329 | CORE_ADDR | |
330 | push_bytes (sp, buffer, len) | |
331 | CORE_ADDR sp; | |
332 | char *buffer; | |
333 | int len; | |
334 | { | |
335 | #if 1 INNER_THAN 2 | |
336 | sp -= len; | |
337 | write_memory (sp, buffer, len); | |
338 | #else /* stack grows upward */ | |
339 | write_memory (sp, buffer, len); | |
340 | sp += len; | |
341 | #endif /* stack grows upward */ | |
342 | ||
343 | return sp; | |
344 | } | |
345 | ||
346 | /* Push onto the stack the specified value VALUE. */ | |
347 | ||
348 | CORE_ADDR | |
349 | value_push (sp, arg) | |
350 | register CORE_ADDR sp; | |
351 | value arg; | |
352 | { | |
353 | register int len = TYPE_LENGTH (VALUE_TYPE (arg)); | |
354 | ||
355 | #if 1 INNER_THAN 2 | |
356 | sp -= len; | |
357 | write_memory (sp, VALUE_CONTENTS (arg), len); | |
358 | #else /* stack grows upward */ | |
359 | write_memory (sp, VALUE_CONTENTS (arg), len); | |
360 | sp += len; | |
361 | #endif /* stack grows upward */ | |
362 | ||
363 | return sp; | |
364 | } | |
365 | ||
366 | /* Perform the standard coercions that are specified | |
367 | for arguments to be passed to C functions. */ | |
368 | ||
369 | value | |
370 | value_arg_coerce (arg) | |
371 | value arg; | |
372 | { | |
373 | register struct type *type; | |
374 | ||
375 | COERCE_ENUM (arg); | |
376 | ||
377 | type = VALUE_TYPE (arg); | |
378 | ||
379 | if (TYPE_CODE (type) == TYPE_CODE_INT | |
380 | && TYPE_LENGTH (type) < sizeof (int)) | |
381 | return value_cast (builtin_type_int, arg); | |
382 | ||
383 | if (type == builtin_type_float) | |
384 | return value_cast (builtin_type_double, arg); | |
385 | ||
386 | return arg; | |
387 | } | |
388 | ||
389 | /* Push the value ARG, first coercing it as an argument | |
390 | to a C function. */ | |
391 | ||
392 | CORE_ADDR | |
393 | value_arg_push (sp, arg) | |
394 | register CORE_ADDR sp; | |
395 | value arg; | |
396 | { | |
397 | return value_push (sp, value_arg_coerce (arg)); | |
398 | } | |
399 | ||
400 | /* Perform a function call in the inferior. | |
401 | ARGS is a vector of values of arguments (NARGS of them). | |
402 | FUNCTION is a value, the function to be called. | |
403 | Returns a value representing what the function returned. | |
404 | May fail to return, if a breakpoint or signal is hit | |
405 | during the execution of the function. */ | |
406 | ||
407 | value | |
408 | call_function (function, nargs, args) | |
409 | value function; | |
410 | int nargs; | |
411 | value *args; | |
412 | { | |
413 | register CORE_ADDR sp; | |
414 | register int i; | |
415 | CORE_ADDR start_sp; | |
416 | static REGISTER_TYPE dummy[] = CALL_DUMMY; | |
417 | REGISTER_TYPE dummy1[sizeof dummy / sizeof (REGISTER_TYPE)]; | |
418 | CORE_ADDR old_sp; | |
419 | struct type *value_type; | |
420 | ||
421 | PUSH_DUMMY_FRAME; | |
422 | ||
423 | { | |
424 | register CORE_ADDR funaddr; | |
425 | register struct type *ftype = VALUE_TYPE (function); | |
426 | register enum type_code code = TYPE_CODE (ftype); | |
427 | ||
632ea0cc | 428 | if (code == TYPE_CODE_MPTR) |
429 | error ("not implemented: member pointer to call_function"); | |
430 | ||
7b4ac7e1 | 431 | /* Determine address to call. */ |
432 | if (code == TYPE_CODE_FUNC) | |
433 | { | |
434 | funaddr = VALUE_ADDRESS (function); | |
435 | value_type = TYPE_TARGET_TYPE (ftype); | |
436 | } | |
437 | else if (code == TYPE_CODE_PTR) | |
438 | { | |
439 | funaddr = value_as_long (function); | |
440 | if (TYPE_CODE (TYPE_TARGET_TYPE (ftype)) | |
441 | == TYPE_CODE_FUNC) | |
442 | value_type = TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (ftype)); | |
443 | else | |
444 | value_type = builtin_type_int; | |
445 | } | |
446 | else if (code == TYPE_CODE_INT) | |
447 | { | |
448 | /* Handle the case of functions lacking debugging info. | |
449 | Their values are characters since their addresses are char */ | |
450 | if (TYPE_LENGTH (ftype) == 1) | |
451 | funaddr = value_as_long (value_addr (function)); | |
452 | else | |
453 | /* Handle integer used as address of a function. */ | |
454 | funaddr = value_as_long (function); | |
455 | ||
456 | value_type = builtin_type_int; | |
457 | } | |
458 | else | |
459 | error ("Invalid data type for function to be called."); | |
460 | ||
461 | /* Create a call sequence customized for this function | |
462 | and the number of arguments for it. */ | |
463 | bcopy (dummy, dummy1, sizeof dummy); | |
464 | FIX_CALL_DUMMY (dummy1, funaddr, nargs); | |
465 | } | |
466 | ||
467 | old_sp = sp = read_register (SP_REGNUM); | |
468 | ||
469 | #if 1 INNER_THAN 2 /* Stack grows down */ | |
470 | sp -= sizeof dummy; | |
471 | write_memory (sp, dummy1, sizeof dummy); | |
472 | start_sp = sp; | |
473 | for (i = nargs - 1; i >= 0; i--) | |
474 | sp = value_arg_push (sp, args[i]); | |
475 | #else /* Stack grows up */ | |
476 | start_sp = sp; | |
477 | write_memory (sp, dummy1, sizeof dummy); | |
478 | sp += sizeof dummy; | |
479 | for (i = 0; i < nargs; i++) | |
480 | sp = value_arg_push (sp, args[i]); | |
481 | #endif /* Stack grows up */ | |
482 | ||
483 | write_register (SP_REGNUM, sp); | |
484 | ||
485 | /* Figure out the value returned by the function. */ | |
486 | { | |
487 | char retbuf[REGISTER_BYTES]; | |
488 | ||
489 | /* Execute the stack dummy routine, calling FUNCTION. | |
490 | When it is done, discard the empty frame | |
491 | after storing the contents of all regs into retbuf. */ | |
492 | run_stack_dummy (start_sp + CALL_DUMMY_START_OFFSET, retbuf); | |
493 | ||
494 | return value_being_returned (value_type, retbuf); | |
495 | } | |
496 | } | |
497 | \f | |
498 | /* Create a value for a string constant: | |
499 | Call the function malloc in the inferior to get space for it, | |
500 | then copy the data into that space | |
501 | and then return the address with type char *. | |
502 | PTR points to the string constant data; LEN is number of characters. */ | |
503 | ||
504 | value | |
505 | value_string (ptr, len) | |
506 | char *ptr; | |
507 | int len; | |
508 | { | |
509 | register value val; | |
510 | register struct symbol *sym; | |
511 | value blocklen; | |
512 | register char *copy = (char *) alloca (len + 1); | |
513 | char *i = ptr; | |
514 | register char *o = copy, *ibeg = ptr; | |
515 | register int c; | |
516 | ||
517 | /* Copy the string into COPY, processing escapes. | |
518 | We could not conveniently process them in expread | |
519 | because the string there wants to be a substring of the input. */ | |
520 | ||
521 | while (i - ibeg < len) | |
522 | { | |
523 | c = *i++; | |
524 | if (c == '\\') | |
525 | { | |
526 | c = parse_escape (&i); | |
527 | if (c == -1) | |
528 | continue; | |
529 | } | |
530 | *o++ = c; | |
531 | } | |
532 | *o = 0; | |
533 | ||
534 | /* Get the length of the string after escapes are processed. */ | |
535 | ||
536 | len = o - copy; | |
537 | ||
538 | /* Find the address of malloc in the inferior. */ | |
539 | ||
540 | sym = lookup_symbol ("malloc", 0, VAR_NAMESPACE); | |
541 | if (sym != 0) | |
542 | { | |
543 | if (SYMBOL_CLASS (sym) != LOC_BLOCK) | |
544 | error ("\"malloc\" exists in this program but is not a function."); | |
545 | val = value_of_variable (sym); | |
546 | } | |
547 | else | |
548 | { | |
549 | register int i; | |
550 | for (i = 0; i < misc_function_count; i++) | |
551 | if (!strcmp (misc_function_vector[i].name, "malloc")) | |
552 | break; | |
553 | if (i < misc_function_count) | |
554 | val = value_from_long (builtin_type_long, | |
555 | misc_function_vector[i].address); | |
556 | else | |
557 | error ("String constants require the program to have a function \"malloc\"."); | |
558 | } | |
559 | ||
560 | blocklen = value_from_long (builtin_type_int, len + 1); | |
561 | val = call_function (val, 1, &blocklen); | |
562 | if (value_zerop (val)) | |
563 | error ("No memory available for string constant."); | |
564 | write_memory (value_as_long (val), copy, len + 1); | |
565 | VALUE_TYPE (val) = lookup_pointer_type (builtin_type_char); | |
566 | return val; | |
567 | } | |
568 | \f | |
569 | /* Given ARG1, a value of type (pointer to a)* structure/union, | |
570 | extract the component named NAME from the ultimate target structure/union | |
571 | and return it as a value with its appropriate type. | |
632ea0cc | 572 | ERR is used in the error message if ARG1's type is wrong. |
573 | ||
574 | C++: ARGS is a list of argument types to aid in the selection of | |
575 | an appropriate method. Also, handle derived types. | |
576 | ||
577 | ERR is an error message to be printed in case the field is not found. */ | |
7b4ac7e1 | 578 | |
579 | value | |
632ea0cc | 580 | value_struct_elt (arg1, args, name, err) |
581 | register value arg1, *args; | |
7b4ac7e1 | 582 | char *name; |
583 | char *err; | |
584 | { | |
585 | register struct type *t; | |
586 | register int i; | |
632ea0cc | 587 | int found = 0; |
588 | ||
589 | struct type *baseclass; | |
7b4ac7e1 | 590 | |
591 | COERCE_ARRAY (arg1); | |
592 | ||
593 | t = VALUE_TYPE (arg1); | |
594 | ||
595 | /* Follow pointers until we get to a non-pointer. */ | |
596 | ||
632ea0cc | 597 | while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF) |
7b4ac7e1 | 598 | { |
599 | arg1 = value_ind (arg1); | |
600 | COERCE_ARRAY (arg1); | |
601 | t = VALUE_TYPE (arg1); | |
602 | } | |
603 | ||
632ea0cc | 604 | if (TYPE_CODE (t) == TYPE_CODE_MPTR) |
605 | error ("not implemented: member pointers in value_struct_elt"); | |
606 | ||
7b4ac7e1 | 607 | if (TYPE_CODE (t) != TYPE_CODE_STRUCT |
632ea0cc | 608 | && TYPE_CODE (t) != TYPE_CODE_UNION) |
7b4ac7e1 | 609 | error ("Attempt to extract a component of a value that is not a %s.", err); |
610 | ||
632ea0cc | 611 | baseclass = t; |
612 | ||
613 | if (!args) | |
614 | { | |
615 | /* if there are no arguments ...do this... */ | |
616 | ||
617 | /* Try as a variable first, because if we succeed, there | |
618 | is less work to be done. */ | |
619 | while (t) | |
620 | { | |
621 | for (i = TYPE_NFIELDS (t) - 1; i >= 0; i--) | |
622 | { | |
623 | if (!strcmp (TYPE_FIELD_NAME (t, i), name)) | |
624 | { | |
625 | found = 1; | |
626 | break; | |
627 | } | |
628 | } | |
629 | ||
630 | if (i >= 0) | |
631 | return TYPE_FIELD_STATIC (t, i) | |
632 | ? value_static_field (t, name, i) : value_field (arg1, i); | |
633 | ||
634 | t = TYPE_BASECLASS (t); | |
635 | VALUE_TYPE (arg1) = t; /* side effect! */ | |
636 | } | |
637 | ||
638 | /* C++: If it was not found as a data field, then try to | |
639 | return it as a pointer to a method. */ | |
640 | t = baseclass; | |
641 | VALUE_TYPE (arg1) = t; /* side effect! */ | |
642 | ||
643 | if (destructor_name_p (name, t)) | |
644 | error ("use `info method' command to print out value of destructor"); | |
645 | ||
646 | while (t) | |
647 | { | |
648 | for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i) | |
649 | { | |
650 | if (! strcmp (TYPE_FN_FIELDLIST_NAME (t, i), name)) | |
651 | { | |
652 | error ("use `info method' command to print value of method \"%s\"", name); | |
653 | } | |
654 | } | |
655 | t = TYPE_BASECLASS (t); | |
656 | } | |
657 | ||
658 | if (found == 0) | |
659 | error("there is no field named %s", name); | |
660 | return 0; | |
661 | } | |
662 | ||
663 | if (destructor_name_p (name, t)) | |
664 | { | |
665 | if (!args[1]) | |
666 | { | |
667 | /* destructors are a special case. */ | |
668 | return (value)value_fn_field (arg1, 0, TYPE_FN_FIELDLIST_LENGTH (t, 0)); | |
669 | } | |
670 | else | |
671 | { | |
672 | error ("destructor should not have any argument"); | |
673 | } | |
674 | } | |
675 | ||
676 | /* This following loop is for methods with arguments. */ | |
677 | while (t) | |
678 | { | |
679 | /* Look up as method first, because that is where we | |
680 | expect to find it first. */ | |
681 | for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; i--) | |
682 | { | |
683 | struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i); | |
684 | ||
685 | if (!strcmp (TYPE_FN_FIELDLIST_NAME (t, i), name)) | |
686 | { | |
687 | int j; | |
688 | struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i); | |
689 | ||
690 | found = 1; | |
691 | for (j = TYPE_FN_FIELDLIST_LENGTH (t, i) - 1; j >= 0; --j) | |
692 | { | |
693 | if (!typecmp (TYPE_FN_FIELD_ARGS (f, j), args)) | |
694 | { | |
695 | if (TYPE_FN_FIELD_VIRTUAL_P (f, j)) | |
696 | { | |
697 | /* First, get the virtual function table pointer. | |
698 | That comes with a strange type, so cast | |
699 | it to type `pointer to long' (which | |
700 | should serve just fine as a function type). | |
701 | Then, index into the table, and convert | |
702 | final value to appropriate function type. */ | |
703 | value vfn, vtbl; | |
704 | value vi = value_from_long (builtin_type_int, | |
705 | TYPE_FN_FIELD_VOFFSET (f, j)); | |
706 | VALUE_TYPE (arg1) = TYPE_VPTR_BASETYPE (t); | |
707 | ||
708 | if (TYPE_VPTR_FIELDNO (t) < 0) | |
709 | TYPE_VPTR_FIELDNO (t) | |
710 | = fill_in_vptr_fieldno (t); | |
711 | ||
712 | vtbl = value_field (arg1, TYPE_VPTR_FIELDNO (t)); | |
713 | vfn = value_subscript (vtbl, vi); | |
714 | VALUE_TYPE (vfn) = lookup_pointer_type (TYPE_FN_FIELD_TYPE (f, j)); | |
715 | return vfn; | |
716 | } | |
717 | else | |
718 | return (value)value_fn_field (arg1, i, j); | |
719 | } | |
720 | } | |
721 | } | |
722 | } | |
723 | t = TYPE_BASECLASS (t); | |
724 | VALUE_TYPE (arg1) = t; /* side effect! */ | |
725 | } | |
726 | ||
727 | if (found) | |
728 | { | |
729 | error ("Structure method %s not defined for arglist.", name); | |
730 | return 0; | |
731 | } | |
732 | else | |
733 | { | |
734 | /* See if user tried to invoke data as function */ | |
735 | t = baseclass; | |
736 | while (t) | |
737 | { | |
738 | for (i = TYPE_NFIELDS (t) - 1; i >= 0; i--) | |
739 | { | |
740 | if (!strcmp (TYPE_FIELD_NAME (t, i), name)) | |
741 | { | |
742 | found = 1; | |
743 | break; | |
744 | } | |
745 | } | |
746 | ||
747 | if (i >= 0) | |
748 | return TYPE_FIELD_STATIC (t, i) | |
749 | ? value_static_field (t, name, i) : value_field (arg1, i); | |
750 | ||
751 | t = TYPE_BASECLASS (t); | |
752 | VALUE_TYPE (arg1) = t; /* side effect! */ | |
753 | } | |
754 | error ("Structure has no component named %s.", name); | |
755 | } | |
756 | } | |
757 | ||
758 | /* C++: return 1 is NAME is a legitimate name for the destructor | |
759 | of type TYPE. If TYPE does not have a destructor, or | |
760 | if NAME is inappropriate for TYPE, an error is signaled. */ | |
761 | int | |
762 | destructor_name_p (name, type) | |
763 | char *name; | |
764 | struct type *type; | |
765 | { | |
766 | /* destructors are a special case. */ | |
767 | char *dname = TYPE_NAME (type); | |
768 | ||
769 | if (name[0] == '~') | |
770 | { | |
771 | if (! TYPE_HAS_DESTRUCTOR (type)) | |
772 | error ("type `%s' does not have destructor defined", | |
773 | TYPE_NAME (type)); | |
774 | /* Skip past the "struct " at the front. */ | |
775 | while (*dname++ != ' ') ; | |
776 | if (strcmp (dname, name+1)) | |
777 | error ("destructor specification error"); | |
778 | else | |
779 | return 1; | |
780 | } | |
781 | return 0; | |
782 | } | |
783 | ||
784 | /* C++: Given ARG1, a value of type (pointer to a)* structure/union, | |
785 | return 1 if the component named NAME from the ultimate | |
786 | target structure/union is defined, otherwise, return 0. */ | |
787 | ||
788 | int | |
789 | check_field (arg1, name) | |
790 | register value arg1; | |
791 | char *name; | |
792 | { | |
793 | register struct type *t; | |
794 | register int i; | |
795 | int found = 0; | |
796 | ||
797 | struct type *baseclass; | |
798 | ||
799 | COERCE_ARRAY (arg1); | |
800 | ||
801 | t = VALUE_TYPE (arg1); | |
802 | ||
803 | /* Follow pointers until we get to a non-pointer. */ | |
804 | ||
805 | while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF) | |
806 | { | |
807 | arg1 = value_ind (arg1); | |
808 | COERCE_ARRAY (arg1); | |
809 | t = VALUE_TYPE (arg1); | |
810 | } | |
811 | ||
812 | if (TYPE_CODE (t) == TYPE_CODE_MPTR) | |
813 | error ("not implemented: member pointers in check_field"); | |
814 | ||
815 | if (TYPE_CODE (t) != TYPE_CODE_STRUCT | |
816 | && TYPE_CODE (t) != TYPE_CODE_UNION) | |
817 | error ("Internal error: `this' is not an aggregate"); | |
818 | ||
819 | baseclass = t; | |
820 | ||
821 | while (t) | |
7b4ac7e1 | 822 | { |
632ea0cc | 823 | for (i = TYPE_NFIELDS (t) - 1; i >= 0; i--) |
824 | { | |
825 | if (!strcmp (TYPE_FIELD_NAME (t, i), name)) | |
826 | { | |
827 | return 1; | |
828 | } | |
829 | } | |
830 | t = TYPE_BASECLASS (t); | |
831 | VALUE_TYPE (arg1) = t; /* side effect! */ | |
7b4ac7e1 | 832 | } |
833 | ||
632ea0cc | 834 | /* C++: If it was not found as a data field, then try to |
835 | return it as a pointer to a method. */ | |
836 | t = baseclass; | |
837 | VALUE_TYPE (arg1) = t; /* side effect! */ | |
838 | ||
839 | /* Destructors are a special case. */ | |
840 | if (destructor_name_p (name, t)) | |
841 | return 1; | |
842 | ||
843 | while (t) | |
844 | { | |
845 | for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i) | |
846 | { | |
847 | if (!strcmp (TYPE_FN_FIELDLIST_NAME (t, i), name)) | |
848 | return 1; | |
849 | } | |
850 | t = TYPE_BASECLASS (t); | |
851 | } | |
852 | return 0; | |
853 | } | |
7b4ac7e1 | 854 | |
632ea0cc | 855 | /* C++: Given an aggregate type DOMAIN, and a member name NAME, |
856 | return the address of this member as a pointer to member | |
857 | type. If INTYPE is non-null, then it will be the type | |
858 | of the member we are looking for. This will help us resolve | |
859 | pointers to member functions. */ | |
860 | ||
861 | value | |
862 | value_struct_elt_for_address (domain, intype, name) | |
863 | struct type *domain, *intype; | |
864 | char *name; | |
865 | { | |
866 | register struct type *t = domain; | |
867 | register int i; | |
868 | int found = 0; | |
869 | value v; | |
870 | ||
871 | struct type *baseclass; | |
872 | ||
873 | if (TYPE_CODE (t) != TYPE_CODE_STRUCT | |
874 | && TYPE_CODE (t) != TYPE_CODE_UNION) | |
875 | error ("Internal error: non-aggregate type to value_struct_elt_for_address"); | |
876 | ||
877 | baseclass = t; | |
878 | ||
879 | while (t) | |
880 | { | |
881 | for (i = TYPE_NFIELDS (t) - 1; i >= 0; i--) | |
882 | { | |
883 | if (!strcmp (TYPE_FIELD_NAME (t, i), name)) | |
884 | { | |
885 | if (TYPE_FIELD_PACKED (t, i)) | |
886 | error ("pointers to bitfield members not allowed"); | |
887 | ||
888 | v = value_from_long (builtin_type_int, TYPE_FIELD_BITPOS (t, i) >> 3); | |
889 | VALUE_TYPE (v) = lookup_member_pointer_type (TYPE_FIELD_TYPE (t, i), baseclass); | |
890 | return v; | |
891 | } | |
892 | } | |
893 | t = TYPE_BASECLASS (t); | |
894 | } | |
895 | ||
896 | /* C++: If it was not found as a data field, then try to | |
897 | return it as a pointer to a method. */ | |
898 | t = baseclass; | |
899 | ||
900 | /* Destructors are a special case. */ | |
901 | if (destructor_name_p (name, t)) | |
902 | { | |
903 | error ("pointers to destructors not implemented yet"); | |
904 | } | |
905 | ||
906 | while (t) | |
907 | { | |
908 | for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i) | |
909 | { | |
910 | if (!strcmp (TYPE_FN_FIELDLIST_NAME (t, i), name)) | |
911 | { | |
912 | int j = TYPE_FN_FIELDLIST_LENGTH (t, i); | |
913 | struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i); | |
914 | ||
915 | if (intype == 0 && j > 1) | |
916 | error ("non-unique member `%s' requires type instantiation", name); | |
917 | if (intype) | |
918 | { | |
919 | while (j--) | |
920 | if (TYPE_FN_FIELD_TYPE (f, j) == intype) | |
921 | break; | |
922 | if (j < 0) | |
923 | error ("no member function matches that type instantiation"); | |
924 | } | |
925 | else | |
926 | j = 0; | |
927 | ||
928 | if (TYPE_FN_FIELD_VIRTUAL_P (f, j)) | |
929 | { | |
930 | v = value_from_long (builtin_type_long, | |
931 | TYPE_FN_FIELD_VOFFSET (f, j)); | |
932 | } | |
933 | else | |
934 | { | |
935 | struct symbol *s = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j), | |
936 | 0, VAR_NAMESPACE); | |
937 | v = locate_var_value (s, 0); | |
938 | } | |
939 | VALUE_TYPE (v) = lookup_member_pointer_type (TYPE_FN_FIELD_TYPE (f, j), baseclass); | |
940 | return v; | |
941 | } | |
942 | } | |
943 | t = TYPE_BASECLASS (t); | |
944 | } | |
945 | return 0; | |
946 | } | |
947 | ||
948 | /* Compare two argument lists and return the position in which they differ, | |
949 | or zero if equal. Note that we ignore the first argument, which is | |
950 | the type of the instance variable. This is because we want to handle | |
951 | derived classes. This is not entirely correct: we should actually | |
952 | check to make sure that a requested operation is type secure, | |
953 | shouldn't we? */ | |
954 | int typecmp(t1, t2) | |
955 | struct type *t1[]; | |
956 | value t2[]; | |
957 | { | |
958 | int i; | |
959 | ||
960 | if (t1[0]->code == TYPE_CODE_VOID) return 0; | |
961 | if (!t1[1])return 0; | |
962 | for (i = 1; t1[i]->code != TYPE_CODE_VOID; i++) | |
963 | { | |
964 | if (! t2[i] | |
965 | || t1[i]->code != t2[i]->type->code | |
966 | || t1[i]->target_type != t2[i]->type->target_type) | |
967 | { | |
968 | return i+1; | |
969 | } | |
970 | } | |
971 | return t2[i] ? i+1 : 0; | |
972 | } | |
973 | ||
974 | #ifndef FRAME | |
975 | #include "frame.h" | |
976 | #endif | |
977 | ||
978 | /* C++: return the value of the class instance variable, if one exists. | |
979 | Flag COMPLAIN signals an error if the request is made in an | |
980 | inappropriate context. */ | |
981 | value | |
982 | value_of_this (complain) | |
983 | int complain; | |
984 | { | |
985 | extern FRAME selected_frame; | |
986 | struct symbol *func, *sym; | |
987 | char *funname = 0; | |
988 | struct block *b; | |
989 | int i; | |
990 | ||
991 | if (selected_frame == 0) | |
992 | if (complain) | |
993 | error ("no frame selected"); | |
994 | else return 0; | |
995 | ||
996 | func = get_frame_function (selected_frame); | |
997 | if (func) | |
998 | funname = SYMBOL_NAME (func); | |
999 | else | |
1000 | if (complain) | |
1001 | error ("no `this' in nameless context"); | |
1002 | else return 0; | |
1003 | ||
1004 | b = SYMBOL_BLOCK_VALUE (func); | |
1005 | i = BLOCK_NSYMS (b); | |
1006 | if (i <= 0) | |
1007 | if (complain) | |
1008 | error ("no args, no `this'"); | |
1009 | else return 0; | |
1010 | ||
1011 | sym = BLOCK_SYM (b, 0); | |
1012 | if (strncmp ("$this", SYMBOL_NAME (sym), 5)) | |
1013 | if (complain) | |
1014 | error ("current stack frame not in method"); | |
1015 | else return 0; | |
1016 | ||
1017 | return read_var_value (sym, selected_frame); | |
7b4ac7e1 | 1018 | } |
1019 | \f | |
1020 | static | |
1021 | initialize () | |
1022 | { } | |
1023 | ||
1024 | END_FILE |