* valops.c (typecmp): If t2 == 0, return 1.
[platform/upstream/binutils.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, 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
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   SWAP_TARGET_AND_HOST (&buffer, len);
515 #if 1 INNER_THAN 2
516   sp -= len;
517   write_memory (sp, (char *)&buffer, len);
518 #else /* stack grows upward */
519   write_memory (sp, (char *)&buffer, len);
520   sp += len;
521 #endif /* stack grows upward */
522
523   return sp;
524 }
525
526 /* Push LEN bytes with data at BUFFER.  */
527
528 CORE_ADDR
529 push_bytes (sp, buffer, len)
530      CORE_ADDR sp;
531      char *buffer;
532      int len;
533 {
534 #if 1 INNER_THAN 2
535   sp -= len;
536   write_memory (sp, buffer, len);
537 #else /* stack grows upward */
538   write_memory (sp, buffer, len);
539   sp += len;
540 #endif /* stack grows upward */
541
542   return sp;
543 }
544
545 /* Push onto the stack the specified value VALUE.  */
546
547 CORE_ADDR
548 value_push (sp, arg)
549      register CORE_ADDR sp;
550      value arg;
551 {
552   register int len = TYPE_LENGTH (VALUE_TYPE (arg));
553
554 #if 1 INNER_THAN 2
555   sp -= len;
556   write_memory (sp, VALUE_CONTENTS (arg), len);
557 #else /* stack grows upward */
558   write_memory (sp, VALUE_CONTENTS (arg), len);
559   sp += len;
560 #endif /* stack grows upward */
561
562   return sp;
563 }
564
565 /* Perform the standard coercions that are specified
566    for arguments to be passed to C functions.  */
567
568 value
569 value_arg_coerce (arg)
570      value arg;
571 {
572   register struct type *type;
573
574   COERCE_ENUM (arg);
575
576   type = VALUE_TYPE (arg);
577
578   if (TYPE_CODE (type) == TYPE_CODE_INT
579       && TYPE_LENGTH (type) < sizeof (int))
580     return value_cast (builtin_type_int, arg);
581
582   if (type == builtin_type_float)
583     return value_cast (builtin_type_double, arg);
584
585   return arg;
586 }
587
588 /* Push the value ARG, first coercing it as an argument
589    to a C function.  */
590
591 CORE_ADDR
592 value_arg_push (sp, arg)
593      register CORE_ADDR sp;
594      value arg;
595 {
596   return value_push (sp, value_arg_coerce (arg));
597 }
598
599 /* Determine a function's address and its return type from its value. 
600    Calls error() if the function is not valid for calling.  */
601
602 CORE_ADDR
603 find_function_addr (function, retval_type)
604      value function;
605      struct type **retval_type;
606 {
607   register struct type *ftype = VALUE_TYPE (function);
608   register enum type_code code = TYPE_CODE (ftype);
609   struct type *value_type;
610   CORE_ADDR funaddr;
611
612   /* If it's a member function, just look at the function
613      part of it.  */
614
615   /* Determine address to call.  */
616   if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD)
617     {
618       funaddr = VALUE_ADDRESS (function);
619       value_type = TYPE_TARGET_TYPE (ftype);
620     }
621   else if (code == TYPE_CODE_PTR)
622     {
623       funaddr = value_as_long (function);
624       if (TYPE_CODE (TYPE_TARGET_TYPE (ftype)) == TYPE_CODE_FUNC
625           || TYPE_CODE (TYPE_TARGET_TYPE (ftype)) == TYPE_CODE_METHOD)
626         value_type = TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (ftype));
627       else
628         value_type = builtin_type_int;
629     }
630   else if (code == TYPE_CODE_INT)
631     {
632       /* Handle the case of functions lacking debugging info.
633          Their values are characters since their addresses are char */
634       if (TYPE_LENGTH (ftype) == 1)
635         funaddr = value_as_long (value_addr (function));
636       else
637         /* Handle integer used as address of a function.  */
638         funaddr = value_as_long (function);
639
640       value_type = builtin_type_int;
641     }
642   else
643     error ("Invalid data type for function to be called.");
644
645   *retval_type = value_type;
646   return funaddr;
647 }
648
649 #if defined (CALL_DUMMY)
650 /* All this stuff with a dummy frame may seem unnecessarily complicated
651    (why not just save registers in GDB?).  The purpose of pushing a dummy
652    frame which looks just like a real frame is so that if you call a
653    function and then hit a breakpoint (get a signal, etc), "backtrace"
654    will look right.  Whether the backtrace needs to actually show the
655    stack at the time the inferior function was called is debatable, but
656    it certainly needs to not display garbage.  So if you are contemplating
657    making dummy frames be different from normal frames, consider that.  */
658
659 /* Perform a function call in the inferior.
660    ARGS is a vector of values of arguments (NARGS of them).
661    FUNCTION is a value, the function to be called.
662    Returns a value representing what the function returned.
663    May fail to return, if a breakpoint or signal is hit
664    during the execution of the function.  */
665
666 value
667 call_function_by_hand (function, nargs, args)
668      value function;
669      int nargs;
670      value *args;
671 {
672   register CORE_ADDR sp;
673   register int i;
674   CORE_ADDR start_sp;
675   /* CALL_DUMMY is an array of words (REGISTER_TYPE), but each word
676      in in host byte order.  It is switched to target byte order before calling
677      FIX_CALL_DUMMY.  */
678   static REGISTER_TYPE dummy[] = CALL_DUMMY;
679   REGISTER_TYPE dummy1[sizeof dummy / sizeof (REGISTER_TYPE)];
680   CORE_ADDR old_sp;
681   struct type *value_type;
682   unsigned char struct_return;
683   CORE_ADDR struct_addr;
684   struct inferior_status inf_status;
685   struct cleanup *old_chain;
686   CORE_ADDR funaddr;
687   int using_gcc;
688
689   save_inferior_status (&inf_status, 1);
690   old_chain = make_cleanup (restore_inferior_status, &inf_status);
691
692   /* PUSH_DUMMY_FRAME is responsible for saving the inferior registers
693      (and POP_FRAME for restoring them).  (At least on most machines)
694      they are saved on the stack in the inferior.  */
695   PUSH_DUMMY_FRAME;
696
697   old_sp = sp = read_register (SP_REGNUM);
698
699 #if 1 INNER_THAN 2              /* Stack grows down */
700   sp -= sizeof dummy;
701   start_sp = sp;
702 #else                           /* Stack grows up */
703   start_sp = sp;
704   sp += sizeof dummy;
705 #endif
706
707   funaddr = find_function_addr (function, &value_type);
708
709   {
710     struct block *b = block_for_pc (funaddr);
711     /* If compiled without -g, assume GCC.  */
712     using_gcc = b == NULL || BLOCK_GCC_COMPILED (b);
713   }
714
715   /* Are we returning a value using a structure return or a normal
716      value return? */
717
718   struct_return = using_struct_return (function, funaddr, value_type,
719                                        using_gcc);
720
721   /* Create a call sequence customized for this function
722      and the number of arguments for it.  */
723   bcopy (dummy, dummy1, sizeof dummy);
724   for (i = 0; i < sizeof dummy / sizeof (REGISTER_TYPE); i++)
725     SWAP_TARGET_AND_HOST (&dummy1[i], sizeof (REGISTER_TYPE));
726   FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, args,
727                   value_type, using_gcc);
728
729 #if CALL_DUMMY_LOCATION == ON_STACK
730   write_memory (start_sp, (char *)dummy1, sizeof dummy);
731
732 #else /* Not on stack.  */
733 #if CALL_DUMMY_LOCATION == BEFORE_TEXT_END
734   /* Convex Unix prohibits executing in the stack segment. */
735   /* Hope there is empty room at the top of the text segment. */
736   {
737     static checked = 0;
738     if (!checked)
739       for (start_sp = text_end - sizeof dummy; start_sp < text_end; ++start_sp)
740         if (read_memory_integer (start_sp, 1) != 0)
741           error ("text segment full -- no place to put call");
742     checked = 1;
743     sp = old_sp;
744     start_sp = text_end - sizeof dummy;
745     write_memory (start_sp, (char *)dummy1, sizeof dummy);
746   }
747 #else /* After text_end.  */
748   {
749     int errcode;
750     sp = old_sp;
751     start_sp = text_end;
752     errcode = target_write_memory (start_sp, (char *)dummy1, sizeof dummy);
753     if (errcode != 0)
754       error ("Cannot write text segment -- call_function failed");
755   }
756 #endif /* After text_end.  */
757 #endif /* Not on stack.  */
758
759 #ifdef lint
760   sp = old_sp;          /* It really is used, for some ifdef's... */
761 #endif
762
763 #ifdef STACK_ALIGN
764   /* If stack grows down, we must leave a hole at the top. */
765   {
766     int len = 0;
767
768     /* Reserve space for the return structure to be written on the
769        stack, if necessary */
770
771     if (struct_return)
772       len += TYPE_LENGTH (value_type);
773     
774     for (i = nargs - 1; i >= 0; i--)
775       len += TYPE_LENGTH (VALUE_TYPE (value_arg_coerce (args[i])));
776 #ifdef CALL_DUMMY_STACK_ADJUST
777     len += CALL_DUMMY_STACK_ADJUST;
778 #endif
779 #if 1 INNER_THAN 2
780     sp -= STACK_ALIGN (len) - len;
781 #else
782     sp += STACK_ALIGN (len) - len;
783 #endif
784   }
785 #endif /* STACK_ALIGN */
786
787     /* Reserve space for the return structure to be written on the
788        stack, if necessary */
789
790     if (struct_return)
791       {
792 #if 1 INNER_THAN 2
793         sp -= TYPE_LENGTH (value_type);
794         struct_addr = sp;
795 #else
796         struct_addr = sp;
797         sp += TYPE_LENGTH (value_type);
798 #endif
799       }
800
801 #if defined (REG_STRUCT_HAS_ADDR)
802   {
803     /* This is a machine like the sparc, where we need to pass a pointer
804        to the structure, not the structure itself.  */
805     if (REG_STRUCT_HAS_ADDR (using_gcc))
806       for (i = nargs - 1; i >= 0; i--)
807         if (TYPE_CODE (VALUE_TYPE (args[i])) == TYPE_CODE_STRUCT)
808           {
809             CORE_ADDR addr;
810 #if !(1 INNER_THAN 2)
811             /* The stack grows up, so the address of the thing we push
812                is the stack pointer before we push it.  */
813             addr = sp;
814 #endif
815             /* Push the structure.  */
816             sp = value_push (sp, args[i]);
817 #if 1 INNER_THAN 2
818             /* The stack grows down, so the address of the thing we push
819                is the stack pointer after we push it.  */
820             addr = sp;
821 #endif
822             /* The value we're going to pass is the address of the thing
823                we just pushed.  */
824             args[i] = value_from_long (builtin_type_long, (LONGEST) addr);
825           }
826   }
827 #endif /* REG_STRUCT_HAS_ADDR.  */
828
829 #ifdef PUSH_ARGUMENTS
830   PUSH_ARGUMENTS(nargs, args, sp, struct_return, struct_addr);
831 #else /* !PUSH_ARGUMENTS */
832   for (i = nargs - 1; i >= 0; i--)
833     sp = value_arg_push (sp, args[i]);
834 #endif /* !PUSH_ARGUMENTS */
835
836 #ifdef CALL_DUMMY_STACK_ADJUST
837 #if 1 INNER_THAN 2
838   sp -= CALL_DUMMY_STACK_ADJUST;
839 #else
840   sp += CALL_DUMMY_STACK_ADJUST;
841 #endif
842 #endif /* CALL_DUMMY_STACK_ADJUST */
843
844   /* Store the address at which the structure is supposed to be
845      written.  Note that this (and the code which reserved the space
846      above) assumes that gcc was used to compile this function.  Since
847      it doesn't cost us anything but space and if the function is pcc
848      it will ignore this value, we will make that assumption.
849
850      Also note that on some machines (like the sparc) pcc uses a 
851      convention like gcc's.  */
852
853   if (struct_return)
854     STORE_STRUCT_RETURN (struct_addr, sp);
855
856   /* Write the stack pointer.  This is here because the statements above
857      might fool with it.  On SPARC, this write also stores the register
858      window into the right place in the new stack frame, which otherwise
859      wouldn't happen.  (See write_inferior_registers in sparc-xdep.c.)  */
860   write_register (SP_REGNUM, sp);
861
862   /* Figure out the value returned by the function.  */
863   {
864     char retbuf[REGISTER_BYTES];
865
866     /* Execute the stack dummy routine, calling FUNCTION.
867        When it is done, discard the empty frame
868        after storing the contents of all regs into retbuf.  */
869     run_stack_dummy (start_sp + CALL_DUMMY_START_OFFSET, retbuf);
870
871     do_cleanups (old_chain);
872
873     return value_being_returned (value_type, retbuf, struct_return);
874   }
875 }
876 #else /* no CALL_DUMMY.  */
877 value
878 call_function_by_hand (function, nargs, args)
879      value function;
880      int nargs;
881      value *args;
882 {
883   error ("Cannot invoke functions on this machine.");
884 }
885 #endif /* no CALL_DUMMY.  */
886 \f
887 /* Create a value for a string constant:
888    Call the function malloc in the inferior to get space for it,
889    then copy the data into that space
890    and then return the address with type char *.
891    PTR points to the string constant data; LEN is number of characters.  */
892
893 value
894 value_string (ptr, len)
895      char *ptr;
896      int len;
897 {
898   register value val;
899   register struct symbol *sym;
900   value blocklen;
901   register char *copy = (char *) alloca (len + 1);
902   char *i = ptr;
903   register char *o = copy, *ibeg = ptr;
904   register int c;
905
906   /* Copy the string into COPY, processing escapes.
907      We could not conveniently process them in expread
908      because the string there wants to be a substring of the input.  */
909
910   while (i - ibeg < len)
911     {
912       c = *i++;
913       if (c == '\\')
914         {
915           c = parse_escape (&i);
916           if (c == -1)
917             continue;
918         }
919       *o++ = c;
920     }
921   *o = 0;
922
923   /* Get the length of the string after escapes are processed.  */
924
925   len = o - copy;
926
927   /* Find the address of malloc in the inferior.  */
928
929   sym = lookup_symbol ("malloc", 0, VAR_NAMESPACE, 0, NULL);
930   if (sym != 0)
931     {
932       if (SYMBOL_CLASS (sym) != LOC_BLOCK)
933         error ("\"malloc\" exists in this program but is not a function.");
934       val = value_of_variable (sym);
935     }
936   else
937     {
938       register int j;
939       for (j = 0; j < misc_function_count; j++)
940         if (!strcmp (misc_function_vector[j].name, "malloc"))
941           break;
942       if (j < misc_function_count)
943         val = value_from_long (builtin_type_long,
944                              (LONGEST) misc_function_vector[j].address);
945       else
946         error ("String constants require the program to have a function \"malloc\".");
947     }
948
949   blocklen = value_from_long (builtin_type_int, (LONGEST) (len + 1));
950   val = target_call_function (val, 1, &blocklen);
951   if (value_zerop (val))
952     error ("No memory available for string constant.");
953   write_memory ((CORE_ADDR) value_as_long (val), copy, len + 1);
954   VALUE_TYPE (val) = lookup_pointer_type (builtin_type_char);
955   return val;
956 }
957 \f
958 /* Helper function used by value_struct_elt to recurse through baseclasses.
959    Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
960    and treat the result as having type TYPE.
961    If found, return value, else return NULL.
962
963    If LOOKING_FOR_BASECLASS, then instead of looking for struct fields,
964    look for a baseclass named NAME.  */
965
966 static value
967 search_struct_field (name, arg1, offset, type, looking_for_baseclass)
968      char *name;
969      register value arg1;
970      int offset;
971      register struct type *type;
972      int looking_for_baseclass;
973 {
974   int i;
975
976   check_stub_type (type);
977
978   if (! looking_for_baseclass)
979     for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
980       {
981         char *t_field_name = TYPE_FIELD_NAME (type, i);
982
983         if (t_field_name && !strcmp (t_field_name, name))
984           {
985             value v = (TYPE_FIELD_STATIC (type, i)
986                        ? value_static_field (type, name, i)
987                        : value_primitive_field (arg1, offset, i, type));
988             if (v == 0)
989               error("there is no field named %s", name);
990             return v;
991           }
992       }
993
994   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
995     {
996       value v;
997       /* If we are looking for baseclasses, this is what we get when we
998          hit them.  */
999       int found_baseclass = (looking_for_baseclass
1000                              && !strcmp (name, TYPE_BASECLASS_NAME (type, i)));
1001
1002       if (BASETYPE_VIA_VIRTUAL (type, i))
1003         {
1004           value v2;
1005           baseclass_addr (type, i, VALUE_CONTENTS (arg1) + offset, &v2);
1006           if (v2 == 0)
1007             error ("virtual baseclass botch");
1008           if (found_baseclass)
1009             return v2;
1010           v = search_struct_field (name, v2, 0, TYPE_BASECLASS (type, i),
1011                                    looking_for_baseclass);
1012           if (v) return v;
1013           else continue;
1014         }
1015       if (found_baseclass)
1016         v = value_primitive_field (arg1, offset, i, type);
1017       else
1018         v = search_struct_field (name, arg1,
1019                                  offset + TYPE_BASECLASS_BITPOS (type, i) / 8,
1020                                  TYPE_BASECLASS (type, i),
1021                                  looking_for_baseclass);
1022       if (v) return v;
1023     }
1024   return NULL;
1025 }
1026
1027 /* Helper function used by value_struct_elt to recurse through baseclasses.
1028    Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1029    and treat the result as having type TYPE.
1030    If found, return value, else return NULL. */
1031
1032 static value
1033 search_struct_method (name, arg1, args, offset, static_memfuncp, type)
1034      char *name;
1035      register value arg1, *args;
1036      int offset, *static_memfuncp;
1037      register struct type *type;
1038 {
1039   int i;
1040
1041   check_stub_type (type);
1042   for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
1043     {
1044       char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
1045       if (t_field_name && !strcmp (t_field_name, name))
1046         {
1047           int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1;
1048           struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1049
1050           if (j > 0 && args == 0)
1051             error ("cannot resolve overloaded method `%s'", name);
1052           while (j >= 0)
1053             {
1054               if (TYPE_FLAGS (TYPE_FN_FIELD_TYPE (f, j)) & TYPE_FLAG_STUB)
1055                 check_stub_method (type, i, j);
1056               if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j),
1057                             TYPE_FN_FIELD_ARGS (f, j), args))
1058                 {
1059                   if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1060                     return (value)value_virtual_fn_field (arg1, f, j, type);
1061                   if (TYPE_FN_FIELD_STATIC_P (f, j) && static_memfuncp)
1062                     *static_memfuncp = 1;
1063                   return (value)value_fn_field (arg1, i, j);
1064                 }
1065               j--;
1066             }
1067         }
1068     }
1069
1070   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1071     {
1072       value v;
1073
1074       if (BASETYPE_VIA_VIRTUAL (type, i))
1075         {
1076           value v2;
1077           baseclass_addr (type, i, VALUE_CONTENTS (arg1) + offset, &v2);
1078           if (v2 == 0)
1079             error ("virtual baseclass botch");
1080           v = search_struct_method (name, v2, args, 0,
1081                                     static_memfuncp, TYPE_BASECLASS (type, i));
1082           if (v) return v;
1083           else continue;
1084         }
1085
1086       v = search_struct_method (name, arg1, args,
1087                                 TYPE_BASECLASS_BITPOS (type, i) / 8,
1088                                 static_memfuncp, TYPE_BASECLASS (type, i));
1089       if (v) return v;
1090     }
1091   return NULL;
1092 }
1093
1094 /* Given *ARGP, a value of type (pointer to a)* structure/union,
1095    extract the component named NAME from the ultimate target structure/union
1096    and return it as a value with its appropriate type.
1097    ERR is used in the error message if *ARGP's type is wrong.
1098
1099    C++: ARGS is a list of argument types to aid in the selection of
1100    an appropriate method. Also, handle derived types.
1101
1102    STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
1103    where the truthvalue of whether the function that was resolved was
1104    a static member function or not is stored.
1105
1106    ERR is an error message to be printed in case the field is not found.  */
1107
1108 value
1109 value_struct_elt (argp, args, name, static_memfuncp, err)
1110      register value *argp, *args;
1111      char *name;
1112      int *static_memfuncp;
1113      char *err;
1114 {
1115   register struct type *t;
1116   value v;
1117
1118   COERCE_ARRAY (*argp);
1119
1120   t = VALUE_TYPE (*argp);
1121
1122   /* Follow pointers until we get to a non-pointer.  */
1123
1124   while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1125     {
1126       *argp = value_ind (*argp);
1127       /* Don't coerce fn pointer to fn and then back again!  */
1128       if (TYPE_CODE (VALUE_TYPE (*argp)) != TYPE_CODE_FUNC)
1129         COERCE_ARRAY (*argp);
1130       t = VALUE_TYPE (*argp);
1131     }
1132
1133   if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1134     error ("not implemented: member type in value_struct_elt");
1135
1136   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1137       && TYPE_CODE (t) != TYPE_CODE_UNION)
1138     error ("Attempt to extract a component of a value that is not a %s.", err);
1139
1140   /* Assume it's not, unless we see that it is.  */
1141   if (static_memfuncp)
1142     *static_memfuncp =0;
1143
1144   if (!args)
1145     {
1146       /* if there are no arguments ...do this...  */
1147
1148       /* Try as a field first, because if we succeed, there
1149          is less work to be done.  */
1150       v = search_struct_field (name, *argp, 0, t, 0);
1151       if (v)
1152         return v;
1153
1154       /* C++: If it was not found as a data field, then try to
1155          return it as a pointer to a method.  */
1156
1157       if (destructor_name_p (name, t))
1158         error ("Cannot get value of destructor");
1159
1160       v = search_struct_method (name, *argp, args, 0, static_memfuncp, t);
1161
1162       if (v == 0)
1163         {
1164           if (TYPE_NFN_FIELDS (t))
1165             error ("There is no member or method named %s.", name);
1166           else
1167             error ("There is no member named %s.", name);
1168         }
1169       return v;
1170     }
1171
1172   if (destructor_name_p (name, t))
1173     {
1174       if (!args[1])
1175         {
1176           /* destructors are a special case.  */
1177           return (value)value_fn_field (*argp, 0,
1178                                         TYPE_FN_FIELDLIST_LENGTH (t, 0));
1179         }
1180       else
1181         {
1182           error ("destructor should not have any argument");
1183         }
1184     }
1185   else
1186     v = search_struct_method (name, *argp, args, 0, static_memfuncp, t);
1187
1188   if (v == 0)
1189     {
1190       /* See if user tried to invoke data as function.  If so,
1191          hand it back.  If it's not callable (i.e., a pointer to function),
1192          gdb should give an error.  */
1193       v = search_struct_field (name, *argp, 0, t, 0);
1194     }
1195
1196   if (!v)
1197     error ("Structure has no component named %s.", name);
1198   return v;
1199 }
1200
1201 /* C++: return 1 is NAME is a legitimate name for the destructor
1202    of type TYPE.  If TYPE does not have a destructor, or
1203    if NAME is inappropriate for TYPE, an error is signaled.  */
1204 int
1205 destructor_name_p (name, type)
1206      char *name;
1207      struct type *type;
1208 {
1209   /* destructors are a special case.  */
1210
1211   if (name[0] == '~')
1212     {
1213       char *dname = type_name_no_tag (type);
1214
1215       if (! TYPE_HAS_DESTRUCTOR (type))
1216         error ("type `%s' does not have destructor defined", dname);
1217       if (strcmp (dname, name+1))
1218         error ("name of destructor must equal name of class");
1219       else
1220         return 1;
1221     }
1222   return 0;
1223 }
1224
1225 /* Helper function for check_field: Given TYPE, a structure/union,
1226    return 1 if the component named NAME from the ultimate
1227    target structure/union is defined, otherwise, return 0. */
1228
1229 static int
1230 check_field_in (type, name)
1231      register struct type *type;
1232      char *name;
1233 {
1234   register int i;
1235
1236   for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
1237     {
1238       char *t_field_name = TYPE_FIELD_NAME (type, i);
1239       if (t_field_name && !strcmp (t_field_name, name))
1240         return 1;
1241     }
1242
1243   /* C++: If it was not found as a data field, then try to
1244      return it as a pointer to a method.  */
1245
1246   /* Destructors are a special case.  */
1247   if (destructor_name_p (name, type))
1248     return 1;
1249
1250   for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
1251     {
1252       if (!strcmp (TYPE_FN_FIELDLIST_NAME (type, i), name))
1253         return 1;
1254     }
1255
1256   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1257     if (check_field_in (TYPE_BASECLASS (type, i), name))
1258       return 1;
1259       
1260   return 0;
1261 }
1262
1263
1264 /* C++: Given ARG1, a value of type (pointer to a)* structure/union,
1265    return 1 if the component named NAME from the ultimate
1266    target structure/union is defined, otherwise, return 0.  */
1267
1268 int
1269 check_field (arg1, name)
1270      register value arg1;
1271      char *name;
1272 {
1273   register struct type *t;
1274
1275   COERCE_ARRAY (arg1);
1276
1277   t = VALUE_TYPE (arg1);
1278
1279   /* Follow pointers until we get to a non-pointer.  */
1280
1281   while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1282     t = TYPE_TARGET_TYPE (t);
1283
1284   if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1285     error ("not implemented: member type in check_field");
1286
1287   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1288       && TYPE_CODE (t) != TYPE_CODE_UNION)
1289     error ("Internal error: `this' is not an aggregate");
1290
1291   return check_field_in (t, name);
1292 }
1293
1294 /* C++: Given an aggregate type DOMAIN, and a member name NAME,
1295    return the address of this member as a pointer to member
1296    type.  If INTYPE is non-null, then it will be the type
1297    of the member we are looking for.  This will help us resolve
1298    pointers to member functions.  */
1299
1300 value
1301 value_struct_elt_for_address (domain, intype, name)
1302      struct type *domain, *intype;
1303      char *name;
1304 {
1305   register struct type *t = domain;
1306   register int i;
1307   value v;
1308
1309   struct type *baseclass;
1310
1311   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1312       && TYPE_CODE (t) != TYPE_CODE_UNION)
1313     error ("Internal error: non-aggregate type to value_struct_elt_for_address");
1314
1315   baseclass = t;
1316
1317   while (t)
1318     {
1319       for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
1320         {
1321           char *t_field_name = TYPE_FIELD_NAME (t, i);
1322           if (t_field_name && !strcmp (t_field_name, name))
1323             {
1324               if (TYPE_FIELD_STATIC (t, i))
1325                 {
1326                   char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (t, i);
1327                   struct symbol *sym =
1328                       lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL);
1329                   if (! sym) error ("Internal error: could not find physical static variable named %s", phys_name);
1330                   v = value_from_long(builtin_type_long,
1331                                       (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym));
1332                   VALUE_TYPE(v) = lookup_pointer_type (TYPE_FIELD_TYPE (t, i));
1333                   return v;
1334                 }
1335               if (TYPE_FIELD_PACKED (t, i))
1336                 error ("pointers to bitfield members not allowed");
1337
1338               v = value_from_long (builtin_type_int,
1339                                    (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3));
1340               VALUE_TYPE (v)
1341                 = lookup_pointer_type (lookup_member_type (TYPE_FIELD_TYPE (t, i), baseclass));
1342               return v;
1343             }
1344         }
1345
1346       if (TYPE_N_BASECLASSES (t) == 0)
1347         break;
1348
1349       t = TYPE_BASECLASS (t, 0);
1350     }
1351
1352   /* C++: If it was not found as a data field, then try to
1353      return it as a pointer to a method.  */
1354   t = baseclass;
1355
1356   /* Destructors are a special case.  */
1357   if (destructor_name_p (name, t))
1358     {
1359       error ("pointers to destructors not implemented yet");
1360     }
1361
1362   /* Perform all necessary dereferencing.  */
1363   while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR)
1364     intype = TYPE_TARGET_TYPE (intype);
1365
1366   while (t)
1367     {
1368       for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
1369         {
1370           if (!strcmp (TYPE_FN_FIELDLIST_NAME (t, i), name))
1371             {
1372               int j = TYPE_FN_FIELDLIST_LENGTH (t, i);
1373               struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
1374
1375               if (intype == 0 && j > 1)
1376                 error ("non-unique member `%s' requires type instantiation", name);
1377               if (intype)
1378                 {
1379                   while (j--)
1380                     if (TYPE_FN_FIELD_TYPE (f, j) == intype)
1381                       break;
1382                   if (j < 0)
1383                     error ("no member function matches that type instantiation");
1384                 }
1385               else
1386                 j = 0;
1387
1388               check_stub_method (t, i, j);
1389               if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1390                 {
1391                   v = value_from_long (builtin_type_long,
1392                                        (LONGEST) TYPE_FN_FIELD_VOFFSET (f, j));
1393                 }
1394               else
1395                 {
1396                   struct symbol *s = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
1397                                                     0, VAR_NAMESPACE, 0, NULL);
1398                   v = locate_var_value (s, 0);
1399                 }
1400               VALUE_TYPE (v) = lookup_pointer_type (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j), baseclass));
1401               return v;
1402             }
1403         }
1404
1405       if (TYPE_N_BASECLASSES (t) == 0)
1406         break;
1407
1408       t = TYPE_BASECLASS (t, 0);
1409     }
1410   return 0;
1411 }
1412
1413 /* Compare two argument lists and return the position in which they differ,
1414    or zero if equal.
1415
1416    STATICP is nonzero if the T1 argument list came from a
1417    static member function.
1418
1419    For non-static member functions, we ignore the first argument,
1420    which is the type of the instance variable.  This is because we want
1421    to handle calls with objects from derived classes.  This is not
1422    entirely correct: we should actually check to make sure that a
1423    requested operation is type secure, shouldn't we?  FIXME.  */
1424
1425 int
1426 typecmp (staticp, t1, t2)
1427      int staticp;
1428      struct type *t1[];
1429      value t2[];
1430 {
1431   int i;
1432
1433   if (t2 == 0)
1434     return 1;
1435   if (staticp && t1 == 0)
1436     return t2[1] != 0;
1437   if (t1 == 0)
1438     return 1;
1439   if (t1[0]->code == TYPE_CODE_VOID) return 0;
1440   if (t1[!staticp] == 0) return 0;
1441   for (i = !staticp; t1[i] && t1[i]->code != TYPE_CODE_VOID; i++)
1442     {
1443       if (! t2[i]
1444           || t1[i]->code != t2[i]->type->code
1445 /* Too pessimistic:  || t1[i]->target_type != t2[i]->type->target_type */
1446  )
1447         return i+1;
1448     }
1449   if (!t1[i]) return 0;
1450   return t2[i] ? i+1 : 0;
1451 }
1452
1453 /* C++: return the value of the class instance variable, if one exists.
1454    Flag COMPLAIN signals an error if the request is made in an
1455    inappropriate context.  */
1456 value
1457 value_of_this (complain)
1458      int complain;
1459 {
1460   extern FRAME selected_frame;
1461   struct symbol *func, *sym;
1462   struct block *b;
1463   int i;
1464   static const char funny_this[] = "this";
1465   value this;
1466
1467   if (selected_frame == 0)
1468     if (complain)
1469       error ("no frame selected");
1470     else return 0;
1471
1472   func = get_frame_function (selected_frame);
1473   if (!func)
1474     {
1475       if (complain)
1476         error ("no `this' in nameless context");
1477       else return 0;
1478     }
1479
1480   b = SYMBOL_BLOCK_VALUE (func);
1481   i = BLOCK_NSYMS (b);
1482   if (i <= 0)
1483     if (complain)
1484       error ("no args, no `this'");
1485     else return 0;
1486
1487   /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
1488      symbol instead of the LOC_ARG one (if both exist).  */
1489   sym = lookup_block_symbol (b, funny_this, VAR_NAMESPACE);
1490   if (sym == NULL)
1491     {
1492       if (complain)
1493         error ("current stack frame not in method");
1494       else
1495         return NULL;
1496     }
1497
1498   this = read_var_value (sym, selected_frame);
1499   if (this == 0 && complain)
1500     error ("`this' argument at unknown address");
1501   return this;
1502 }