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