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