gdb-3.1
[binutils-gdb.git] / gdb / valops.c
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 */
20 #include "stdio.h"
21 #include "defs.h"
22 #include "param.h"
23 #include "symtab.h"
24 #include "value.h"
25 #include "frame.h"
26 #include "inferior.h"
27 \f
28 /* Cast value ARG2 to type TYPE and return as a value.
29 More general than a C cast: accepts any two types of the same length,
30 and if ARG2 is an lvalue it can be cast into anything at all. */
31
32 value
33 value_cast (type, arg2)
34 struct type *type;
35 register value arg2;
36 {
37 register enum type_code code1;
38 register enum type_code code2;
39 register int scalar;
40
41 /* Coerce arrays but not enums. Enums will work as-is
42 and coercing them would cause an infinite recursion. */
43 if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_ENUM)
44 COERCE_ARRAY (arg2);
45
46 code1 = TYPE_CODE (type);
47 code2 = TYPE_CODE (VALUE_TYPE (arg2));
48 scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
49 || code2 == TYPE_CODE_ENUM);
50
51 if (code1 == TYPE_CODE_FLT && scalar)
52 return value_from_double (type, value_as_double (arg2));
53 else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM)
54 && (scalar || code2 == TYPE_CODE_PTR))
55 return value_from_long (type, value_as_long (arg2));
56 else if (TYPE_LENGTH (type) == TYPE_LENGTH (VALUE_TYPE (arg2)))
57 {
58 VALUE_TYPE (arg2) = type;
59 return arg2;
60 }
61 else if (VALUE_LVAL (arg2) == lval_memory)
62 {
63 return value_at (type, VALUE_ADDRESS (arg2) + VALUE_OFFSET (arg2));
64 }
65 else
66 error ("Invalid cast.");
67 }
68
69 /* Return the value with a specified type located at specified address. */
70
71 value
72 value_at (type, addr)
73 struct type *type;
74 CORE_ADDR addr;
75 {
76 register value val = allocate_value (type);
77 int temp;
78
79 temp = read_memory (addr, VALUE_CONTENTS (val), TYPE_LENGTH (type));
80 if (temp)
81 {
82 if (have_inferior_p ())
83 print_sys_errmsg ("ptrace", temp);
84 /* Actually, address between addr and addr + len was out of bounds. */
85 error ("Cannot read memory: address 0x%x out of bounds.", addr);
86 }
87
88 VALUE_LVAL (val) = lval_memory;
89 VALUE_ADDRESS (val) = addr;
90
91 return val;
92 }
93
94 /* Store the contents of FROMVAL into the location of TOVAL.
95 Return a new value with the location of TOVAL and contents of FROMVAL. */
96
97 value
98 value_assign (toval, fromval)
99 register value toval, fromval;
100 {
101 register struct type *type = VALUE_TYPE (toval);
102 register value val;
103 char raw_buffer[MAX_REGISTER_RAW_SIZE];
104 char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
105 int use_buffer = 0;
106
107 extern CORE_ADDR find_saved_register ();
108
109 COERCE_ARRAY (fromval);
110
111 if (VALUE_LVAL (toval) != lval_internalvar)
112 fromval = value_cast (type, fromval);
113
114 /* If TOVAL is a special machine register requiring conversion
115 of program values to a special raw format,
116 convert FROMVAL's contents now, with result in `raw_buffer',
117 and set USE_BUFFER to the number of bytes to write. */
118
119 if (VALUE_REGNO (toval) >= 0
120 && REGISTER_CONVERTIBLE (VALUE_REGNO (toval)))
121 {
122 int regno = VALUE_REGNO (toval);
123 if (VALUE_TYPE (fromval) != REGISTER_VIRTUAL_TYPE (regno))
124 fromval = value_cast (REGISTER_VIRTUAL_TYPE (regno), fromval);
125 bcopy (VALUE_CONTENTS (fromval), virtual_buffer,
126 REGISTER_VIRTUAL_SIZE (regno));
127 REGISTER_CONVERT_TO_RAW (regno, virtual_buffer, raw_buffer);
128 use_buffer = REGISTER_RAW_SIZE (regno);
129 }
130
131 switch (VALUE_LVAL (toval))
132 {
133 case lval_internalvar:
134 set_internalvar (VALUE_INTERNALVAR (toval), fromval);
135 break;
136
137 case lval_internalvar_component:
138 set_internalvar_component (VALUE_INTERNALVAR (toval),
139 VALUE_OFFSET (toval),
140 VALUE_BITPOS (toval),
141 VALUE_BITSIZE (toval),
142 fromval);
143 break;
144
145 case lval_memory:
146 if (VALUE_BITSIZE (toval))
147 {
148 int val;
149 read_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
150 &val, sizeof val);
151 modify_field (&val, (int) value_as_long (fromval),
152 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
153 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
154 &val, sizeof val);
155 }
156 else if (use_buffer)
157 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
158 raw_buffer, use_buffer);
159 else
160 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
161 VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
162 break;
163
164 case lval_register:
165 if (VALUE_BITSIZE (toval))
166 {
167 int val;
168
169 read_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
170 &val, sizeof val);
171 modify_field (&val, (int) value_as_long (fromval),
172 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
173 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
174 &val, sizeof val);
175 }
176 else if (use_buffer)
177 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
178 raw_buffer, use_buffer);
179 else
180 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
181 VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
182 break;
183
184 case lval_reg_frame_relative:
185 {
186 /* value is stored in a series of registers in the frame
187 specified by the structure. Copy that value out, modify
188 it, and copy it back in. */
189 int amount_to_copy = (VALUE_BITSIZE (toval) ? 1 : TYPE_LENGTH (type));
190 int reg_size = REGISTER_RAW_SIZE (VALUE_FRAME_REGNUM (toval));
191 int byte_offset = VALUE_OFFSET (toval) % reg_size;
192 int reg_offset = VALUE_OFFSET (toval) / reg_size;
193 int amount_copied;
194 char *buffer = (char *) alloca (amount_to_copy);
195 int regno;
196 FRAME frame;
197 CORE_ADDR addr;
198
199 /* Figure out which frame this is in currently. */
200 for (frame = get_current_frame ();
201 frame && FRAME_FP (frame) != VALUE_FRAME (toval);
202 frame = get_prev_frame (frame))
203 ;
204
205 if (!frame)
206 error ("Value being assigned to is no longer active.");
207
208 amount_to_copy += (reg_size - amount_to_copy % reg_size);
209
210 /* Copy it out. */
211 for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset,
212 amount_copied = 0);
213 amount_copied < amount_to_copy;
214 amount_copied += reg_size, regno++)
215 {
216 addr = find_saved_register (frame, regno);
217 if (addr == 0)
218 read_register_bytes (REGISTER_BYTE (regno),
219 buffer + amount_copied,
220 reg_size);
221 else
222 read_memory (addr, buffer + amount_copied, reg_size);
223 }
224
225 /* Modify what needs to be modified. */
226 if (VALUE_BITSIZE (toval))
227 modify_field (buffer + byte_offset,
228 (int) value_as_long (fromval),
229 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
230 else if (use_buffer)
231 bcopy (raw_buffer, buffer + byte_offset, use_buffer);
232 else
233 bcopy (VALUE_CONTENTS (fromval), buffer + byte_offset,
234 TYPE_LENGTH (type));
235
236 /* Copy it back. */
237 for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset,
238 amount_copied = 0);
239 amount_copied < amount_to_copy;
240 amount_copied += reg_size, regno++)
241 {
242 addr = find_saved_register (frame, regno);
243 if (addr == 0)
244 write_register_bytes (REGISTER_BYTE (regno),
245 buffer + amount_copied,
246 reg_size);
247 else
248 write_memory (addr, buffer + amount_copied, reg_size);
249 }
250 }
251 break;
252
253
254 default:
255 error ("Left side of = operation is not an lvalue.");
256 }
257
258 /* Return a value just like TOVAL except with the contents of FROMVAL. */
259
260 val = allocate_value (type);
261 bcopy (toval, val, VALUE_CONTENTS (val) - (char *) val);
262 bcopy (VALUE_CONTENTS (fromval), VALUE_CONTENTS (val), TYPE_LENGTH (type));
263
264 return val;
265 }
266
267 /* Extend a value VAL to COUNT repetitions of its type. */
268
269 value
270 value_repeat (arg1, count)
271 value arg1;
272 int count;
273 {
274 register value val;
275
276 if (VALUE_LVAL (arg1) != lval_memory)
277 error ("Only values in memory can be extended with '@'.");
278 if (count < 1)
279 error ("Invalid number %d of repetitions.", count);
280
281 val = allocate_repeat_value (VALUE_TYPE (arg1), count);
282
283 read_memory (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1),
284 VALUE_CONTENTS (val),
285 TYPE_LENGTH (VALUE_TYPE (val)) * count);
286 VALUE_LVAL (val) = lval_memory;
287 VALUE_ADDRESS (val) = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1);
288
289 return val;
290 }
291
292 value
293 value_of_variable (var)
294 struct symbol *var;
295 {
296 return read_var_value (var, (CORE_ADDR) 0);
297 }
298
299 /* Given a value which is an array, return a value which is
300 a pointer to its first element. */
301
302 value
303 value_coerce_array (arg1)
304 value arg1;
305 {
306 register struct type *type;
307 register value val;
308
309 if (VALUE_LVAL (arg1) != lval_memory)
310 error ("Attempt to take address of value not located in memory.");
311
312 /* Get type of elements. */
313 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_ARRAY)
314 type = TYPE_TARGET_TYPE (VALUE_TYPE (arg1));
315 else
316 /* A phony array made by value_repeat.
317 Its type is the type of the elements, not an array type. */
318 type = VALUE_TYPE (arg1);
319
320 /* Get the type of the result. */
321 type = lookup_pointer_type (type);
322 val = value_from_long (builtin_type_long,
323 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
324 VALUE_TYPE (val) = type;
325 return val;
326 }
327
328 /* Return a pointer value for the object for which ARG1 is the contents. */
329
330 value
331 value_addr (arg1)
332 value arg1;
333 {
334 register struct type *type;
335 register value val, arg1_coerced;
336
337 /* Taking the address of an array is really a no-op
338 once the array is coerced to a pointer to its first element. */
339 arg1_coerced = arg1;
340 COERCE_ARRAY (arg1_coerced);
341 if (arg1 != arg1_coerced)
342 return arg1_coerced;
343
344 if (VALUE_LVAL (arg1) != lval_memory)
345 error ("Attempt to take address of value not located in memory.");
346
347 /* Get the type of the result. */
348 type = lookup_pointer_type (VALUE_TYPE (arg1));
349 val = value_from_long (builtin_type_long,
350 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
351 VALUE_TYPE (val) = type;
352 return val;
353 }
354
355 /* Given a value of a pointer type, apply the C unary * operator to it. */
356
357 value
358 value_ind (arg1)
359 value arg1;
360 {
361 /* Must do this before COERCE_ARRAY, otherwise an infinite loop
362 will result */
363 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF)
364 return value_at (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
365 (CORE_ADDR) value_as_long (arg1));
366
367 COERCE_ARRAY (arg1);
368
369 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_MEMBER)
370 error ("not implemented: member types in value_ind");
371
372 /* Allow * on an integer so we can cast it to whatever we want. */
373 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT)
374 return value_at (builtin_type_long,
375 (CORE_ADDR) value_as_long (arg1));
376 else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR)
377 return value_at (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
378 (CORE_ADDR) value_as_long (arg1));
379 else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF)
380 return value_at (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
381 (CORE_ADDR) value_as_long (arg1));
382 error ("Attempt to take contents of a non-pointer value.");
383 }
384 \f
385 /* Pushing small parts of stack frames. */
386
387 /* Push one word (the size of object that a register holds). */
388
389 CORE_ADDR
390 push_word (sp, buffer)
391 CORE_ADDR sp;
392 REGISTER_TYPE buffer;
393 {
394 register int len = sizeof (REGISTER_TYPE);
395
396 #if 1 INNER_THAN 2
397 sp -= len;
398 write_memory (sp, &buffer, len);
399 #else /* stack grows upward */
400 write_memory (sp, &buffer, len);
401 sp += len;
402 #endif /* stack grows upward */
403
404 return sp;
405 }
406
407 /* Push LEN bytes with data at BUFFER. */
408
409 CORE_ADDR
410 push_bytes (sp, buffer, len)
411 CORE_ADDR sp;
412 char *buffer;
413 int len;
414 {
415 #if 1 INNER_THAN 2
416 sp -= len;
417 write_memory (sp, buffer, len);
418 #else /* stack grows upward */
419 write_memory (sp, buffer, len);
420 sp += len;
421 #endif /* stack grows upward */
422
423 return sp;
424 }
425
426 /* Push onto the stack the specified value VALUE. */
427
428 CORE_ADDR
429 value_push (sp, arg)
430 register CORE_ADDR sp;
431 value arg;
432 {
433 register int len = TYPE_LENGTH (VALUE_TYPE (arg));
434
435 #if 1 INNER_THAN 2
436 sp -= len;
437 write_memory (sp, VALUE_CONTENTS (arg), len);
438 #else /* stack grows upward */
439 write_memory (sp, VALUE_CONTENTS (arg), len);
440 sp += len;
441 #endif /* stack grows upward */
442
443 return sp;
444 }
445
446 /* Perform the standard coercions that are specified
447 for arguments to be passed to C functions. */
448
449 value
450 value_arg_coerce (arg)
451 value arg;
452 {
453 register struct type *type;
454
455 COERCE_ENUM (arg);
456
457 type = VALUE_TYPE (arg);
458
459 if (TYPE_CODE (type) == TYPE_CODE_INT
460 && TYPE_LENGTH (type) < sizeof (int))
461 return value_cast (builtin_type_int, arg);
462
463 if (type == builtin_type_float)
464 return value_cast (builtin_type_double, arg);
465
466 return arg;
467 }
468
469 /* Push the value ARG, first coercing it as an argument
470 to a C function. */
471
472 CORE_ADDR
473 value_arg_push (sp, arg)
474 register CORE_ADDR sp;
475 value arg;
476 {
477 return value_push (sp, value_arg_coerce (arg));
478 }
479
480 /* Perform a function call in the inferior.
481 ARGS is a vector of values of arguments (NARGS of them).
482 FUNCTION is a value, the function to be called.
483 Returns a value representing what the function returned.
484 May fail to return, if a breakpoint or signal is hit
485 during the execution of the function. */
486
487 value
488 call_function (function, nargs, args)
489 value function;
490 int nargs;
491 value *args;
492 {
493 register CORE_ADDR sp;
494 register int i;
495 CORE_ADDR start_sp;
496 static REGISTER_TYPE dummy[] = CALL_DUMMY;
497 REGISTER_TYPE dummy1[sizeof dummy / sizeof (REGISTER_TYPE)];
498 CORE_ADDR old_sp;
499 struct type *value_type;
500 unsigned char struct_return;
501 CORE_ADDR struct_addr;
502 struct inferior_status inf_status;
503 struct cleanup *old_chain;
504
505 save_inferior_status (&inf_status, 1);
506 old_chain = make_cleanup (restore_inferior_status, &inf_status);
507
508 PUSH_DUMMY_FRAME;
509
510 old_sp = sp = read_register (SP_REGNUM);
511
512 #if 1 INNER_THAN 2 /* Stack grows down */
513 sp -= sizeof dummy;
514 start_sp = sp;
515 #else /* Stack grows up */
516 start_sp = sp;
517 sp += sizeof dummy;
518 #endif
519
520 {
521 register CORE_ADDR funaddr;
522 register struct type *ftype = VALUE_TYPE (function);
523 register enum type_code code = TYPE_CODE (ftype);
524
525 /* If it's a member function, just look at the function
526 part of it. */
527 if (code == TYPE_CODE_MEMBER)
528 {
529 ftype = TYPE_TARGET_TYPE (ftype);
530 code = TYPE_CODE (ftype);
531 }
532
533 /* Determine address to call. */
534 if (code == TYPE_CODE_FUNC)
535 {
536 funaddr = VALUE_ADDRESS (function);
537 value_type = TYPE_TARGET_TYPE (ftype);
538 }
539 else if (code == TYPE_CODE_PTR)
540 {
541 funaddr = value_as_long (function);
542 if (TYPE_CODE (TYPE_TARGET_TYPE (ftype))
543 == TYPE_CODE_FUNC)
544 value_type = TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (ftype));
545 else
546 value_type = builtin_type_int;
547 }
548 else if (code == TYPE_CODE_INT)
549 {
550 /* Handle the case of functions lacking debugging info.
551 Their values are characters since their addresses are char */
552 if (TYPE_LENGTH (ftype) == 1)
553 funaddr = value_as_long (value_addr (function));
554 else
555 /* Handle integer used as address of a function. */
556 funaddr = value_as_long (function);
557
558 value_type = builtin_type_int;
559 }
560 else
561 error ("Invalid data type for function to be called.");
562
563 /* Are we returning a value using a structure return or a normal
564 value return? */
565
566 struct_return = using_struct_return (function, funaddr, value_type);
567
568 /* Create a call sequence customized for this function
569 and the number of arguments for it. */
570 bcopy (dummy, dummy1, sizeof dummy);
571 FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, value_type);
572 }
573
574 write_memory (start_sp, dummy1, sizeof dummy);
575
576 #ifdef convex
577 /* Convex Unix prohibits executing in the stack segment. */
578 /* Hope there is empty room at the top of the text segment. */
579 {
580 extern CORE_ADDR text_end;
581 static checked = 0;
582 if (!checked)
583 for (start_sp = text_end - sizeof dummy; start_sp < text_end; ++start_sp)
584 if (read_memory_integer (start_sp, 1) != 0)
585 error ("text segment full -- no place to put call");
586 checked = 1;
587 start_sp = text_end - sizeof dummy;
588 write_memory (start_sp, dummy1, sizeof dummy);
589 }
590 #else /* !convex */
591 #ifdef STACK_ALIGN
592 /* If stack grows down, we must leave a hole at the top. */
593 {
594 int len = 0;
595
596 /* Reserve space for the return structure to be written on the
597 stack, if necessary */
598
599 if (struct_return)
600 len += TYPE_LENGTH (value_type);
601
602 for (i = nargs - 1; i >= 0; i--)
603 len += TYPE_LENGTH (VALUE_TYPE (args[i]));
604 #ifdef CALL_DUMMY_STACK_ADJUST
605 len += CALL_DUMMY_STACK_ADJUST;
606 #endif
607 #if 1 INNER_THAN 2
608 sp -= STACK_ALIGN (len) - len;
609 #else
610 sp += STACK_ALIGN (len) - len;
611 #endif
612 }
613 #endif /* STACK_ALIGN */
614
615 /* Reserve space for the return structure to be written on the
616 stack, if necessary */
617
618 if (struct_return)
619 {
620 #if 1 INNER_THAN 2
621 sp -= TYPE_LENGTH (value_type);
622 struct_addr = sp;
623 #else
624 struct_addr = sp;
625 sp += TYPE_LENGTH (value_type);
626 #endif
627 }
628
629 for (i = nargs - 1; i >= 0; i--)
630 sp = value_arg_push (sp, args[i]);
631
632 #ifdef CALL_DUMMY_STACK_ADJUST
633 #if 1 INNER_THAN 2
634 sp -= CALL_DUMMY_STACK_ADJUST;
635 #else
636 sp += CALL_DUMMY_STACK_ADJUST;
637 #endif
638 #endif /* CALL_DUMMY_STACK_ADJUST */
639 #endif /* !convex */
640
641 /* Store the address at which the structure is supposed to be
642 written. Note that this (and the code which reserved the space
643 above) assumes that gcc was used to compile this function. Since
644 it doesn't cost us anything but space and if the function is pcc
645 it will ignore this value, we will make that assumption.
646
647 Also note that on some machines (like the sparc) pcc uses this
648 convention in a slightly twisted way also. */
649
650 if (struct_return)
651 STORE_STRUCT_RETURN (struct_addr, sp);
652
653 /* Write the stack pointer. This is here because the statement above
654 might fool with it */
655 write_register (SP_REGNUM, sp);
656
657 /* Figure out the value returned by the function. */
658 {
659 char retbuf[REGISTER_BYTES];
660
661 /* Execute the stack dummy routine, calling FUNCTION.
662 When it is done, discard the empty frame
663 after storing the contents of all regs into retbuf. */
664 run_stack_dummy (start_sp + CALL_DUMMY_START_OFFSET, retbuf);
665
666 do_cleanups (old_chain);
667
668 return value_being_returned (value_type, retbuf, struct_return);
669 }
670 }
671 \f
672 /* Create a value for a string constant:
673 Call the function malloc in the inferior to get space for it,
674 then copy the data into that space
675 and then return the address with type char *.
676 PTR points to the string constant data; LEN is number of characters. */
677
678 value
679 value_string (ptr, len)
680 char *ptr;
681 int len;
682 {
683 register value val;
684 register struct symbol *sym;
685 value blocklen;
686 register char *copy = (char *) alloca (len + 1);
687 char *i = ptr;
688 register char *o = copy, *ibeg = ptr;
689 register int c;
690
691 /* Copy the string into COPY, processing escapes.
692 We could not conveniently process them in expread
693 because the string there wants to be a substring of the input. */
694
695 while (i - ibeg < len)
696 {
697 c = *i++;
698 if (c == '\\')
699 {
700 c = parse_escape (&i);
701 if (c == -1)
702 continue;
703 }
704 *o++ = c;
705 }
706 *o = 0;
707
708 /* Get the length of the string after escapes are processed. */
709
710 len = o - copy;
711
712 /* Find the address of malloc in the inferior. */
713
714 sym = lookup_symbol ("malloc", 0, VAR_NAMESPACE, 0);
715 if (sym != 0)
716 {
717 if (SYMBOL_CLASS (sym) != LOC_BLOCK)
718 error ("\"malloc\" exists in this program but is not a function.");
719 val = value_of_variable (sym);
720 }
721 else
722 {
723 register int i;
724 for (i = 0; i < misc_function_count; i++)
725 if (!strcmp (misc_function_vector[i].name, "malloc"))
726 break;
727 if (i < misc_function_count)
728 val = value_from_long (builtin_type_long,
729 (LONGEST) misc_function_vector[i].address);
730 else
731 error ("String constants require the program to have a function \"malloc\".");
732 }
733
734 blocklen = value_from_long (builtin_type_int, (LONGEST) (len + 1));
735 val = call_function (val, 1, &blocklen);
736 if (value_zerop (val))
737 error ("No memory available for string constant.");
738 write_memory ((CORE_ADDR) value_as_long (val), copy, len + 1);
739 VALUE_TYPE (val) = lookup_pointer_type (builtin_type_char);
740 return val;
741 }
742 \f
743 /* Given ARG1, a value of type (pointer to a)* structure/union,
744 extract the component named NAME from the ultimate target structure/union
745 and return it as a value with its appropriate type.
746 ERR is used in the error message if ARG1's type is wrong.
747
748 C++: ARGS is a list of argument types to aid in the selection of
749 an appropriate method. Also, handle derived types.
750
751 ERR is an error message to be printed in case the field is not found. */
752
753 value
754 value_struct_elt (arg1, args, name, err)
755 register value arg1, *args;
756 char *name;
757 char *err;
758 {
759 register struct type *t;
760 register int i;
761 int found = 0;
762
763 struct type *baseclass;
764
765 COERCE_ARRAY (arg1);
766
767 t = VALUE_TYPE (arg1);
768
769 /* Follow pointers until we get to a non-pointer. */
770
771 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
772 {
773 arg1 = value_ind (arg1);
774 COERCE_ARRAY (arg1);
775 t = VALUE_TYPE (arg1);
776 }
777
778 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
779 error ("not implemented: member type in value_struct_elt");
780
781 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
782 && TYPE_CODE (t) != TYPE_CODE_UNION)
783 error ("Attempt to extract a component of a value that is not a %s.", err);
784
785 baseclass = t;
786
787 if (!args)
788 {
789 /* if there are no arguments ...do this... */
790
791 /* Try as a variable first, because if we succeed, there
792 is less work to be done. */
793 while (t)
794 {
795 for (i = TYPE_NFIELDS (t) - 1; i >= 0; i--)
796 {
797 if (!strcmp (TYPE_FIELD_NAME (t, i), name))
798 {
799 found = 1;
800 break;
801 }
802 }
803
804 if (i >= 0)
805 return TYPE_FIELD_STATIC (t, i)
806 ? value_static_field (t, name, i) : value_field (arg1, i);
807
808 if (TYPE_N_BASECLASSES (t) == 0)
809 break;
810
811 t = TYPE_BASECLASS (t, 1);
812 VALUE_TYPE (arg1) = t; /* side effect! */
813 }
814
815 /* C++: If it was not found as a data field, then try to
816 return it as a pointer to a method. */
817 t = baseclass;
818 VALUE_TYPE (arg1) = t; /* side effect! */
819
820 if (destructor_name_p (name, t))
821 error ("use `info method' command to print out value of destructor");
822
823 while (t)
824 {
825 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
826 {
827 if (! strcmp (TYPE_FN_FIELDLIST_NAME (t, i), name))
828 {
829 error ("use `info method' command to print value of method \"%s\"", name);
830 }
831 }
832
833 if (TYPE_N_BASECLASSES (t) == 0)
834 break;
835
836 t = TYPE_BASECLASS (t, 1);
837 }
838
839 if (found == 0)
840 error("there is no field named %s", name);
841 return 0;
842 }
843
844 if (destructor_name_p (name, t))
845 {
846 if (!args[1])
847 {
848 /* destructors are a special case. */
849 return (value)value_fn_field (arg1, 0,
850 TYPE_FN_FIELDLIST_LENGTH (t, 0));
851 }
852 else
853 {
854 error ("destructor should not have any argument");
855 }
856 }
857
858 /* This following loop is for methods with arguments. */
859 while (t)
860 {
861 /* Look up as method first, because that is where we
862 expect to find it first. */
863 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; i--)
864 {
865 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
866
867 if (!strcmp (TYPE_FN_FIELDLIST_NAME (t, i), name))
868 {
869 int j;
870 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
871
872 found = 1;
873 for (j = TYPE_FN_FIELDLIST_LENGTH (t, i) - 1; j >= 0; --j)
874 {
875 if (!typecmp (TYPE_FN_FIELD_ARGS (f, j), args))
876 {
877 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
878 return (value)value_virtual_fn_field (arg1, f, j, t);
879 else
880 return (value)value_fn_field (arg1, i, j);
881 }
882 }
883 }
884 }
885
886 if (TYPE_N_BASECLASSES (t) == 0)
887 break;
888
889 t = TYPE_BASECLASS (t, 1);
890 VALUE_TYPE (arg1) = t; /* side effect! */
891 }
892
893 if (found)
894 {
895 error ("Structure method %s not defined for arglist.", name);
896 return 0;
897 }
898 else
899 {
900 /* See if user tried to invoke data as function */
901 t = baseclass;
902 while (t)
903 {
904 for (i = TYPE_NFIELDS (t) - 1; i >= 0; i--)
905 {
906 if (!strcmp (TYPE_FIELD_NAME (t, i), name))
907 {
908 found = 1;
909 break;
910 }
911 }
912
913 if (i >= 0)
914 return TYPE_FIELD_STATIC (t, i)
915 ? value_static_field (t, name, i) : value_field (arg1, i);
916
917 if (TYPE_N_BASECLASSES (t) == 0)
918 break;
919
920 t = TYPE_BASECLASS (t, 1);
921 VALUE_TYPE (arg1) = t; /* side effect! */
922 }
923 error ("Structure has no component named %s.", name);
924 }
925 }
926
927 /* C++: return 1 is NAME is a legitimate name for the destructor
928 of type TYPE. If TYPE does not have a destructor, or
929 if NAME is inappropriate for TYPE, an error is signaled. */
930 int
931 destructor_name_p (name, type)
932 char *name;
933 struct type *type;
934 {
935 /* destructors are a special case. */
936 char *dname = TYPE_NAME (type);
937
938 if (name[0] == '~')
939 {
940 if (! TYPE_HAS_DESTRUCTOR (type))
941 error ("type `%s' does not have destructor defined",
942 TYPE_NAME (type));
943 /* Skip past the "struct " at the front. */
944 while (*dname++ != ' ') ;
945 if (strcmp (dname, name+1))
946 error ("destructor specification error");
947 else
948 return 1;
949 }
950 return 0;
951 }
952
953 /* C++: Given ARG1, a value of type (pointer to a)* structure/union,
954 return 1 if the component named NAME from the ultimate
955 target structure/union is defined, otherwise, return 0. */
956
957 int
958 check_field (arg1, name)
959 register value arg1;
960 char *name;
961 {
962 register struct type *t;
963 register int i;
964 int found = 0;
965
966 struct type *baseclass;
967
968 COERCE_ARRAY (arg1);
969
970 t = VALUE_TYPE (arg1);
971
972 /* Follow pointers until we get to a non-pointer. */
973
974 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
975 {
976 arg1 = value_ind (arg1);
977 COERCE_ARRAY (arg1);
978 t = VALUE_TYPE (arg1);
979 }
980
981 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
982 error ("not implemented: member type in check_field");
983
984 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
985 && TYPE_CODE (t) != TYPE_CODE_UNION)
986 error ("Internal error: `this' is not an aggregate");
987
988 baseclass = t;
989
990 while (t)
991 {
992 for (i = TYPE_NFIELDS (t) - 1; i >= 0; i--)
993 {
994 if (!strcmp (TYPE_FIELD_NAME (t, i), name))
995 {
996 return 1;
997 }
998 }
999 if (TYPE_N_BASECLASSES (t) == 0)
1000 break;
1001
1002 t = TYPE_BASECLASS (t, 1);
1003 VALUE_TYPE (arg1) = t; /* side effect! */
1004 }
1005
1006 /* C++: If it was not found as a data field, then try to
1007 return it as a pointer to a method. */
1008 t = baseclass;
1009 VALUE_TYPE (arg1) = t; /* side effect! */
1010
1011 /* Destructors are a special case. */
1012 if (destructor_name_p (name, t))
1013 return 1;
1014
1015 while (t)
1016 {
1017 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
1018 {
1019 if (!strcmp (TYPE_FN_FIELDLIST_NAME (t, i), name))
1020 return 1;
1021 }
1022
1023 if (TYPE_N_BASECLASSES (t) == 0)
1024 break;
1025
1026 t = TYPE_BASECLASS (t, 1);
1027 }
1028 return 0;
1029 }
1030
1031 /* C++: Given an aggregate type DOMAIN, and a member name NAME,
1032 return the address of this member as a pointer to member
1033 type. If INTYPE is non-null, then it will be the type
1034 of the member we are looking for. This will help us resolve
1035 pointers to member functions. */
1036
1037 value
1038 value_struct_elt_for_address (domain, intype, name)
1039 struct type *domain, *intype;
1040 char *name;
1041 {
1042 register struct type *t = domain;
1043 register int i;
1044 int found = 0;
1045 value v;
1046
1047 struct type *baseclass;
1048
1049 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1050 && TYPE_CODE (t) != TYPE_CODE_UNION)
1051 error ("Internal error: non-aggregate type to value_struct_elt_for_address");
1052
1053 baseclass = t;
1054
1055 while (t)
1056 {
1057 for (i = TYPE_NFIELDS (t) - 1; i >= 0; i--)
1058 {
1059 if (!strcmp (TYPE_FIELD_NAME (t, i), name))
1060 {
1061 if (TYPE_FIELD_PACKED (t, i))
1062 error ("pointers to bitfield members not allowed");
1063
1064 v = value_from_long (builtin_type_int,
1065 (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3));
1066 VALUE_TYPE (v) = lookup_pointer_type (
1067 lookup_member_type (TYPE_FIELD_TYPE (t, i), baseclass));
1068 return v;
1069 }
1070 }
1071
1072 if (TYPE_N_BASECLASSES (t) == 0)
1073 break;
1074
1075 t = TYPE_BASECLASS (t, 1);
1076 }
1077
1078 /* C++: If it was not found as a data field, then try to
1079 return it as a pointer to a method. */
1080 t = baseclass;
1081
1082 /* Destructors are a special case. */
1083 if (destructor_name_p (name, t))
1084 {
1085 error ("pointers to destructors not implemented yet");
1086 }
1087
1088 /* Perform all necessary dereferencing. */
1089 while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR)
1090 intype = TYPE_TARGET_TYPE (intype);
1091
1092 while (t)
1093 {
1094 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
1095 {
1096 if (!strcmp (TYPE_FN_FIELDLIST_NAME (t, i), name))
1097 {
1098 int j = TYPE_FN_FIELDLIST_LENGTH (t, i);
1099 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
1100
1101 if (intype == 0 && j > 1)
1102 error ("non-unique member `%s' requires type instantiation", name);
1103 if (intype)
1104 {
1105 while (j--)
1106 if (TYPE_FN_FIELD_TYPE (f, j) == intype)
1107 break;
1108 if (j < 0)
1109 error ("no member function matches that type instantiation");
1110 }
1111 else
1112 j = 0;
1113
1114 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1115 {
1116 v = value_from_long (builtin_type_long,
1117 (LONGEST) TYPE_FN_FIELD_VOFFSET (f, j));
1118 }
1119 else
1120 {
1121 struct symbol *s = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
1122 0, VAR_NAMESPACE, 0);
1123 v = locate_var_value (s, 0);
1124 }
1125 VALUE_TYPE (v) = lookup_pointer_type (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j), baseclass));
1126 return v;
1127 }
1128 }
1129
1130 if (TYPE_N_BASECLASSES (t) == 0)
1131 break;
1132
1133 t = TYPE_BASECLASS (t, 1);
1134 }
1135 return 0;
1136 }
1137
1138 /* Compare two argument lists and return the position in which they differ,
1139 or zero if equal. Note that we ignore the first argument, which is
1140 the type of the instance variable. This is because we want to handle
1141 derived classes. This is not entirely correct: we should actually
1142 check to make sure that a requested operation is type secure,
1143 shouldn't we? */
1144 int typecmp(t1, t2)
1145 struct type *t1[];
1146 value t2[];
1147 {
1148 int i;
1149
1150 if (t1[0]->code == TYPE_CODE_VOID) return 0;
1151 if (!t1[1]) return 0;
1152 for (i = 1; t1[i] && t1[i]->code != TYPE_CODE_VOID; i++)
1153 {
1154 if (! t2[i]
1155 || t1[i]->code != t2[i]->type->code
1156 || t1[i]->target_type != t2[i]->type->target_type)
1157 {
1158 return i+1;
1159 }
1160 }
1161 if (!t1[i]) return 0;
1162 return t2[i] ? i+1 : 0;
1163 }
1164
1165 /* C++: return the value of the class instance variable, if one exists.
1166 Flag COMPLAIN signals an error if the request is made in an
1167 inappropriate context. */
1168 value
1169 value_of_this (complain)
1170 int complain;
1171 {
1172 extern FRAME selected_frame;
1173 struct symbol *func, *sym;
1174 char *funname = 0;
1175 struct block *b;
1176 int i;
1177
1178 if (selected_frame == 0)
1179 if (complain)
1180 error ("no frame selected");
1181 else return 0;
1182
1183 func = get_frame_function (selected_frame);
1184 if (func)
1185 funname = SYMBOL_NAME (func);
1186 else
1187 if (complain)
1188 error ("no `this' in nameless context");
1189 else return 0;
1190
1191 b = SYMBOL_BLOCK_VALUE (func);
1192 i = BLOCK_NSYMS (b);
1193 if (i <= 0)
1194 if (complain)
1195 error ("no args, no `this'");
1196 else return 0;
1197
1198 sym = BLOCK_SYM (b, 0);
1199 if (strncmp ("$this", SYMBOL_NAME (sym), 5))
1200 if (complain)
1201 error ("current stack frame not in method");
1202 else return 0;
1203
1204 return read_var_value (sym, selected_frame);
1205 }