* valops.c (value_coerce_array): Handle STRINGs as well as ARRAYs.
[platform/upstream/binutils.git] / gdb / valops.c
1 /* Perform non-arithmetic operations on values, for GDB.
2    Copyright 1986, 1987, 1989, 1991, 1992 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 "defs.h"
21 #include "symtab.h"
22 #include "gdbtypes.h"
23 #include "value.h"
24 #include "frame.h"
25 #include "inferior.h"
26 #include "gdbcore.h"
27 #include "target.h"
28 #include "demangle.h"
29 #include "language.h"
30
31 #include <errno.h>
32
33 /* Local functions.  */
34
35 static int
36 typecmp PARAMS ((int staticp, struct type *t1[], value t2[]));
37
38 static CORE_ADDR
39 find_function_addr PARAMS ((value, struct type **));
40
41 static CORE_ADDR
42 value_push PARAMS ((CORE_ADDR, value));
43
44 static CORE_ADDR
45 value_arg_push PARAMS ((CORE_ADDR, value));
46
47 static value
48 search_struct_field PARAMS ((char *, value, int, struct type *, int));
49
50 static value
51 search_struct_method PARAMS ((char *, value *, value *, int, int *,
52                               struct type *));
53
54 static int
55 check_field_in PARAMS ((struct type *, const char *));
56
57 static CORE_ADDR
58 allocate_space_in_inferior PARAMS ((int));
59
60 \f
61 /* Allocate NBYTES of space in the inferior using the inferior's malloc
62    and return a value that is a pointer to the allocated space. */
63
64 static CORE_ADDR
65 allocate_space_in_inferior (len)
66      int len;
67 {
68   register value val;
69   register struct symbol *sym;
70   struct minimal_symbol *msymbol;
71   struct type *type;
72   value blocklen;
73   LONGEST maddr;
74
75   /* Find the address of malloc in the inferior.  */
76
77   sym = lookup_symbol ("malloc", 0, VAR_NAMESPACE, 0, NULL);
78   if (sym != NULL)
79     {
80       if (SYMBOL_CLASS (sym) != LOC_BLOCK)
81         {
82           error ("\"malloc\" exists in this program but is not a function.");
83         }
84       val = value_of_variable (sym, NULL);
85     }
86   else
87     {
88       msymbol = lookup_minimal_symbol ("malloc", (struct objfile *) NULL);
89       if (msymbol != NULL)
90         {
91           type = lookup_pointer_type (builtin_type_char);
92           type = lookup_function_type (type);
93           type = lookup_pointer_type (type);
94           maddr = (LONGEST) SYMBOL_VALUE_ADDRESS (msymbol);
95           val = value_from_longest (type, maddr);
96         }
97       else
98         {
99           error ("evaluation of this expression requires the program to have a function \"malloc\".");
100         }
101     }
102
103   blocklen = value_from_longest (builtin_type_int, (LONGEST) len);
104   val = call_function_by_hand (val, 1, &blocklen);
105   if (value_logical_not (val))
106     {
107       error ("No memory available to program.");
108     }
109   return (value_as_long (val));
110 }
111
112 /* Cast value ARG2 to type TYPE and return as a value.
113    More general than a C cast: accepts any two types of the same length,
114    and if ARG2 is an lvalue it can be cast into anything at all.  */
115 /* In C++, casts may change pointer or object representations.  */
116
117 value
118 value_cast (type, arg2)
119      struct type *type;
120      register value arg2;
121 {
122   register enum type_code code1;
123   register enum type_code code2;
124   register int scalar;
125
126   /* Coerce arrays but not enums.  Enums will work as-is
127      and coercing them would cause an infinite recursion.  */
128   if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_ENUM)
129     COERCE_ARRAY (arg2);
130
131   code1 = TYPE_CODE (type);
132   code2 = TYPE_CODE (VALUE_TYPE (arg2));
133   scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
134             || code2 == TYPE_CODE_ENUM);
135
136   if (   code1 == TYPE_CODE_STRUCT
137       && code2 == TYPE_CODE_STRUCT
138       && TYPE_NAME (type) != 0)
139     {
140       /* Look in the type of the source to see if it contains the
141          type of the target as a superclass.  If so, we'll need to
142          offset the object in addition to changing its type.  */
143       value v = search_struct_field (type_name_no_tag (type),
144                                      arg2, 0, VALUE_TYPE (arg2), 1);
145       if (v)
146         {
147           VALUE_TYPE (v) = type;
148           return v;
149         }
150     }
151   if (code1 == TYPE_CODE_FLT && scalar)
152     return value_from_double (type, value_as_double (arg2));
153   else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM)
154            && (scalar || code2 == TYPE_CODE_PTR))
155     return value_from_longest (type, value_as_long (arg2));
156   else if (TYPE_LENGTH (type) == TYPE_LENGTH (VALUE_TYPE (arg2)))
157     {
158       if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
159         {
160           /* Look in the type of the source to see if it contains the
161              type of the target as a superclass.  If so, we'll need to
162              offset the pointer rather than just change its type.  */
163           struct type *t1 = TYPE_TARGET_TYPE (type);
164           struct type *t2 = TYPE_TARGET_TYPE (VALUE_TYPE (arg2));
165           if (   TYPE_CODE (t1) == TYPE_CODE_STRUCT
166               && TYPE_CODE (t2) == TYPE_CODE_STRUCT
167               && TYPE_NAME (t1) != 0) /* if name unknown, can't have supercl */
168             {
169               value v = search_struct_field (type_name_no_tag (t1),
170                                              value_ind (arg2), 0, t2, 1);
171               if (v)
172                 {
173                   v = value_addr (v);
174                   VALUE_TYPE (v) = type;
175                   return v;
176                 }
177             }
178           /* No superclass found, just fall through to change ptr type.  */
179         }
180       VALUE_TYPE (arg2) = type;
181       return arg2;
182     }
183   else if (VALUE_LVAL (arg2) == lval_memory)
184     {
185       return value_at_lazy (type, VALUE_ADDRESS (arg2) + VALUE_OFFSET (arg2));
186     }
187   else if (code1 == TYPE_CODE_VOID)
188     {
189       return value_zero (builtin_type_void, not_lval);
190     }
191   else
192     {
193       error ("Invalid cast.");
194       return 0;
195     }
196 }
197
198 /* Create a value of type TYPE that is zero, and return it.  */
199
200 value
201 value_zero (type, lv)
202      struct type *type;
203      enum lval_type lv;
204 {
205   register value val = allocate_value (type);
206
207   memset (VALUE_CONTENTS (val), 0, TYPE_LENGTH (type));
208   VALUE_LVAL (val) = lv;
209
210   return val;
211 }
212
213 /* Return a value with type TYPE located at ADDR.  
214
215    Call value_at only if the data needs to be fetched immediately;
216    if we can be 'lazy' and defer the fetch, perhaps indefinately, call
217    value_at_lazy instead.  value_at_lazy simply records the address of
218    the data and sets the lazy-evaluation-required flag.  The lazy flag 
219    is tested in the VALUE_CONTENTS macro, which is used if and when 
220    the contents are actually required.  */
221
222 value
223 value_at (type, addr)
224      struct type *type;
225      CORE_ADDR addr;
226 {
227   register value val = allocate_value (type);
228
229   read_memory (addr, VALUE_CONTENTS_RAW (val), TYPE_LENGTH (type));
230
231   VALUE_LVAL (val) = lval_memory;
232   VALUE_ADDRESS (val) = addr;
233
234   return val;
235 }
236
237 /* Return a lazy value with type TYPE located at ADDR (cf. value_at).  */
238
239 value
240 value_at_lazy (type, addr)
241      struct type *type;
242      CORE_ADDR addr;
243 {
244   register value val = allocate_value (type);
245
246   VALUE_LVAL (val) = lval_memory;
247   VALUE_ADDRESS (val) = addr;
248   VALUE_LAZY (val) = 1;
249
250   return val;
251 }
252
253 /* Called only from the VALUE_CONTENTS macro, if the current data for
254    a variable needs to be loaded into VALUE_CONTENTS(VAL).  Fetches the
255    data from the user's process, and clears the lazy flag to indicate
256    that the data in the buffer is valid.
257
258    If the value is zero-length, we avoid calling read_memory, which would
259    abort.  We mark the value as fetched anyway -- all 0 bytes of it.
260
261    This function returns a value because it is used in the VALUE_CONTENTS
262    macro as part of an expression, where a void would not work.  The
263    value is ignored.  */
264
265 int
266 value_fetch_lazy (val)
267      register value val;
268 {
269   CORE_ADDR addr = VALUE_ADDRESS (val) + VALUE_OFFSET (val);
270
271   if (TYPE_LENGTH (VALUE_TYPE (val)))
272     read_memory (addr, VALUE_CONTENTS_RAW (val), 
273                  TYPE_LENGTH (VALUE_TYPE (val)));
274   VALUE_LAZY (val) = 0;
275   return 0;
276 }
277
278
279 /* Store the contents of FROMVAL into the location of TOVAL.
280    Return a new value with the location of TOVAL and contents of FROMVAL.  */
281
282 value
283 value_assign (toval, fromval)
284      register value toval, fromval;
285 {
286   register struct type *type = VALUE_TYPE (toval);
287   register value val;
288   char raw_buffer[MAX_REGISTER_RAW_SIZE];
289   int use_buffer = 0;
290
291   COERCE_ARRAY (fromval);
292   COERCE_REF (toval);
293
294   if (VALUE_LVAL (toval) != lval_internalvar)
295     fromval = value_cast (type, fromval);
296
297   /* If TOVAL is a special machine register requiring conversion
298      of program values to a special raw format,
299      convert FROMVAL's contents now, with result in `raw_buffer',
300      and set USE_BUFFER to the number of bytes to write.  */
301
302 #ifdef REGISTER_CONVERTIBLE
303   if (VALUE_REGNO (toval) >= 0
304       && REGISTER_CONVERTIBLE (VALUE_REGNO (toval)))
305     {
306       int regno = VALUE_REGNO (toval);
307       if (REGISTER_CONVERTIBLE (regno))
308         {
309           REGISTER_CONVERT_TO_RAW (VALUE_TYPE (fromval), regno,
310                                    VALUE_CONTENTS (fromval), raw_buffer);
311           use_buffer = REGISTER_RAW_SIZE (regno);
312         }
313     }
314 #endif
315
316   switch (VALUE_LVAL (toval))
317     {
318     case lval_internalvar:
319       set_internalvar (VALUE_INTERNALVAR (toval), fromval);
320       break;
321
322     case lval_internalvar_component:
323       set_internalvar_component (VALUE_INTERNALVAR (toval),
324                                  VALUE_OFFSET (toval),
325                                  VALUE_BITPOS (toval),
326                                  VALUE_BITSIZE (toval),
327                                  fromval);
328       break;
329
330     case lval_memory:
331       if (VALUE_BITSIZE (toval))
332         {
333           char buffer[sizeof (LONGEST)];
334           /* We assume that the argument to read_memory is in units of
335              host chars.  FIXME:  Is that correct?  */
336           int len = (VALUE_BITPOS (toval)
337                      + VALUE_BITSIZE (toval)
338                      + HOST_CHAR_BIT - 1)
339                     / HOST_CHAR_BIT;
340
341           if (len > sizeof (LONGEST))
342             error ("Can't handle bitfields which don't fit in a %d bit word.",
343                    sizeof (LONGEST) * HOST_CHAR_BIT);
344
345           read_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
346                        buffer, len);
347           modify_field (buffer, value_as_long (fromval),
348                         VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
349           write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
350                         buffer, len);
351         }
352       else if (use_buffer)
353         write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
354                       raw_buffer, use_buffer);
355       else
356         write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
357                       VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
358       break;
359
360     case lval_register:
361       if (VALUE_BITSIZE (toval))
362         {
363           char buffer[sizeof (LONGEST)];
364           int len = REGISTER_RAW_SIZE (VALUE_REGNO (toval));
365
366           if (len > sizeof (LONGEST))
367             error ("Can't handle bitfields in registers larger than %d bits.",
368                    sizeof (LONGEST) * HOST_CHAR_BIT);
369
370           if (VALUE_BITPOS (toval) + VALUE_BITSIZE (toval)
371               > len * HOST_CHAR_BIT)
372             /* Getting this right would involve being very careful about
373                byte order.  */
374             error ("\
375 Can't handle bitfield which doesn't fit in a single register.");
376
377           read_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
378                                buffer, len);
379           modify_field (buffer, value_as_long (fromval),
380                         VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
381           write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
382                                 buffer, len);
383         }
384       else if (use_buffer)
385         write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
386                               raw_buffer, use_buffer);
387       else
388         {
389           /* Do any conversion necessary when storing this type to more
390              than one register.  */
391 #ifdef REGISTER_CONVERT_FROM_TYPE
392           memcpy (raw_buffer, VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
393           REGISTER_CONVERT_FROM_TYPE(VALUE_REGNO (toval), type, raw_buffer);
394           write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
395                                 raw_buffer, TYPE_LENGTH (type));
396 #else
397           write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
398                                 VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
399 #endif
400         }
401       /* Assigning to the stack pointer, frame pointer, and other
402          (architecture and calling convention specific) registers may
403          cause the frame cache to be out of date.  We just do this
404          on all assignments to registers for simplicity; I doubt the slowdown
405          matters.  */
406       reinit_frame_cache ();
407       break;
408
409     case lval_reg_frame_relative:
410       {
411         /* value is stored in a series of registers in the frame
412            specified by the structure.  Copy that value out, modify
413            it, and copy it back in.  */
414         int amount_to_copy = (VALUE_BITSIZE (toval) ? 1 : TYPE_LENGTH (type));
415         int reg_size = REGISTER_RAW_SIZE (VALUE_FRAME_REGNUM (toval));
416         int byte_offset = VALUE_OFFSET (toval) % reg_size;
417         int reg_offset = VALUE_OFFSET (toval) / reg_size;
418         int amount_copied;
419
420         /* Make the buffer large enough in all cases.  */
421         char *buffer = (char *) alloca (amount_to_copy
422                                         + sizeof (LONGEST)
423                                         + MAX_REGISTER_RAW_SIZE);
424
425         int regno;
426         FRAME frame;
427
428         /* Figure out which frame this is in currently.  */
429         for (frame = get_current_frame ();
430              frame && FRAME_FP (frame) != VALUE_FRAME (toval);
431              frame = get_prev_frame (frame))
432           ;
433
434         if (!frame)
435           error ("Value being assigned to is no longer active.");
436
437         amount_to_copy += (reg_size - amount_to_copy % reg_size);
438
439         /* Copy it out.  */
440         for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset,
441               amount_copied = 0);
442              amount_copied < amount_to_copy;
443              amount_copied += reg_size, regno++)
444           {
445             get_saved_register (buffer + amount_copied,
446                                 (int *)NULL, (CORE_ADDR *)NULL,
447                                 frame, regno, (enum lval_type *)NULL);
448           }
449
450         /* Modify what needs to be modified.  */
451         if (VALUE_BITSIZE (toval))
452           modify_field (buffer + byte_offset,
453                         value_as_long (fromval),
454                         VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
455         else if (use_buffer)
456           memcpy (buffer + byte_offset, raw_buffer, use_buffer);
457         else
458           memcpy (buffer + byte_offset, VALUE_CONTENTS (fromval),
459                   TYPE_LENGTH (type));
460
461         /* Copy it back.  */
462         for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset,
463               amount_copied = 0);
464              amount_copied < amount_to_copy;
465              amount_copied += reg_size, regno++)
466           {
467             enum lval_type lval;
468             CORE_ADDR addr;
469             int optim;
470
471             /* Just find out where to put it.  */
472             get_saved_register ((char *)NULL,
473                                 &optim, &addr, frame, regno, &lval);
474             
475             if (optim)
476               error ("Attempt to assign to a value that was optimized out.");
477             if (lval == lval_memory)
478               write_memory (addr, buffer + amount_copied, reg_size);
479             else if (lval == lval_register)
480               write_register_bytes (addr, buffer + amount_copied, reg_size);
481             else
482               error ("Attempt to assign to an unmodifiable value.");
483           }
484       }
485       break;
486         
487
488     default:
489       error ("Left side of = operation is not an lvalue.");
490     }
491
492   /* Return a value just like TOVAL except with the contents of FROMVAL
493      (except in the case of the type if TOVAL is an internalvar).  */
494
495   if (VALUE_LVAL (toval) == lval_internalvar
496       || VALUE_LVAL (toval) == lval_internalvar_component)
497     {
498       type = VALUE_TYPE (fromval);
499     }
500
501   val = allocate_value (type);
502   memcpy (val, toval, VALUE_CONTENTS_RAW (val) - (char *) val);
503   memcpy (VALUE_CONTENTS_RAW (val), VALUE_CONTENTS (fromval),
504           TYPE_LENGTH (type));
505   VALUE_TYPE (val) = type;
506   
507   return val;
508 }
509
510 /* Extend a value VAL to COUNT repetitions of its type.  */
511
512 value
513 value_repeat (arg1, count)
514      value arg1;
515      int count;
516 {
517   register value val;
518
519   if (VALUE_LVAL (arg1) != lval_memory)
520     error ("Only values in memory can be extended with '@'.");
521   if (count < 1)
522     error ("Invalid number %d of repetitions.", count);
523
524   val = allocate_repeat_value (VALUE_TYPE (arg1), count);
525
526   read_memory (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1),
527                VALUE_CONTENTS_RAW (val),
528                TYPE_LENGTH (VALUE_TYPE (val)) * count);
529   VALUE_LVAL (val) = lval_memory;
530   VALUE_ADDRESS (val) = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1);
531
532   return val;
533 }
534
535 value
536 value_of_variable (var, b)
537      struct symbol *var;
538      struct block *b;
539 {
540   value val;
541   FRAME fr;
542
543   if (b == NULL)
544     /* Use selected frame.  */
545     fr = NULL;
546   else
547     {
548       fr = block_innermost_frame (b);
549       if (fr == NULL && symbol_read_needs_frame (var))
550         {
551           if (BLOCK_FUNCTION (b) != NULL
552               && SYMBOL_NAME (BLOCK_FUNCTION (b)) != NULL)
553             error ("No frame is currently executing in block %s.",
554                    SYMBOL_NAME (BLOCK_FUNCTION (b)));
555           else
556             error ("No frame is currently executing in specified block");
557         }
558     }
559   val = read_var_value (var, fr);
560   if (val == 0)
561     error ("Address of symbol \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var));
562   return val;
563 }
564
565 /* Given a value which is an array, return a value which is a pointer to its
566    first element, regardless of whether or not the array has a nonzero lower
567    bound.
568
569    FIXME:  A previous comment here indicated that this routine should be
570    substracting the array's lower bound.  It's not clear to me that this
571    is correct.  Given an array subscripting operation, it would certainly
572    work to do the adjustment here, essentially computing:
573
574    (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0])
575
576    However I believe a more appropriate and logical place to account for
577    the lower bound is to do so in value_subscript, essentially computing:
578
579    (&array[0] + ((index - lowerbound) * sizeof array[0]))
580
581    As further evidence consider what would happen with operations other
582    than array subscripting, where the caller would get back a value that
583    had an address somewhere before the actual first element of the array,
584    and the information about the lower bound would be lost because of
585    the coercion to pointer type.
586    */
587
588 value
589 value_coerce_array (arg1)
590      value arg1;
591 {
592   register struct type *type;
593
594   if (VALUE_LVAL (arg1) != lval_memory)
595     error ("Attempt to take address of value not located in memory.");
596
597   /* Get type of elements.  */
598   if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_ARRAY
599       || TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRING)
600     type = TYPE_TARGET_TYPE (VALUE_TYPE (arg1));
601   else
602     /* A phony array made by value_repeat.
603        Its type is the type of the elements, not an array type.  */
604     type = VALUE_TYPE (arg1);
605
606   return value_from_longest (lookup_pointer_type (type),
607                        (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
608 }
609
610 /* Given a value which is a function, return a value which is a pointer
611    to it.  */
612
613 value
614 value_coerce_function (arg1)
615      value arg1;
616 {
617
618   if (VALUE_LVAL (arg1) != lval_memory)
619     error ("Attempt to take address of value not located in memory.");
620
621   return value_from_longest (lookup_pointer_type (VALUE_TYPE (arg1)),
622                 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
623 }  
624
625 /* Return a pointer value for the object for which ARG1 is the contents.  */
626
627 value
628 value_addr (arg1)
629      value arg1;
630 {
631   struct type *type = VALUE_TYPE (arg1);
632   if (TYPE_CODE (type) == TYPE_CODE_REF)
633     {
634       /* Copy the value, but change the type from (T&) to (T*).
635          We keep the same location information, which is efficient,
636          and allows &(&X) to get the location containing the reference. */
637       value arg2 = value_copy (arg1);
638       VALUE_TYPE (arg2) = lookup_pointer_type (TYPE_TARGET_TYPE (type));
639       return arg2;
640     }
641   if (VALUE_REPEATED (arg1)
642       || TYPE_CODE (type) == TYPE_CODE_ARRAY)
643     return value_coerce_array (arg1);
644   if (TYPE_CODE (type) == TYPE_CODE_FUNC)
645     return value_coerce_function (arg1);
646
647   if (VALUE_LVAL (arg1) != lval_memory)
648     error ("Attempt to take address of value not located in memory.");
649
650   return value_from_longest (lookup_pointer_type (type),
651                 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
652 }
653
654 /* Given a value of a pointer type, apply the C unary * operator to it.  */
655
656 value
657 value_ind (arg1)
658      value arg1;
659 {
660   COERCE_ARRAY (arg1);
661
662   if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_MEMBER)
663     error ("not implemented: member types in value_ind");
664
665   /* Allow * on an integer so we can cast it to whatever we want.
666      This returns an int, which seems like the most C-like thing
667      to do.  "long long" variables are rare enough that
668      BUILTIN_TYPE_LONGEST would seem to be a mistake.  */
669   if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT)
670     return value_at (builtin_type_int,
671                      (CORE_ADDR) value_as_long (arg1));
672   else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR)
673     return value_at_lazy (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
674                           value_as_pointer (arg1));
675   error ("Attempt to take contents of a non-pointer value.");
676   return 0;  /* For lint -- never reached */
677 }
678 \f
679 /* Pushing small parts of stack frames.  */
680
681 /* Push one word (the size of object that a register holds).  */
682
683 CORE_ADDR
684 push_word (sp, word)
685      CORE_ADDR sp;
686      REGISTER_TYPE word;
687 {
688   register int len = sizeof (REGISTER_TYPE);
689   char buffer[MAX_REGISTER_RAW_SIZE];
690
691   store_unsigned_integer (buffer, len, word);
692 #if 1 INNER_THAN 2
693   sp -= len;
694   write_memory (sp, buffer, len);
695 #else /* stack grows upward */
696   write_memory (sp, buffer, len);
697   sp += len;
698 #endif /* stack grows upward */
699
700   return sp;
701 }
702
703 /* Push LEN bytes with data at BUFFER.  */
704
705 CORE_ADDR
706 push_bytes (sp, buffer, len)
707      CORE_ADDR sp;
708      char *buffer;
709      int len;
710 {
711 #if 1 INNER_THAN 2
712   sp -= len;
713   write_memory (sp, buffer, len);
714 #else /* stack grows upward */
715   write_memory (sp, buffer, len);
716   sp += len;
717 #endif /* stack grows upward */
718
719   return sp;
720 }
721
722 /* Push onto the stack the specified value VALUE.  */
723
724 static CORE_ADDR
725 value_push (sp, arg)
726      register CORE_ADDR sp;
727      value arg;
728 {
729   register int len = TYPE_LENGTH (VALUE_TYPE (arg));
730
731 #if 1 INNER_THAN 2
732   sp -= len;
733   write_memory (sp, VALUE_CONTENTS (arg), len);
734 #else /* stack grows upward */
735   write_memory (sp, VALUE_CONTENTS (arg), len);
736   sp += len;
737 #endif /* stack grows upward */
738
739   return sp;
740 }
741
742 /* Perform the standard coercions that are specified
743    for arguments to be passed to C functions.  */
744
745 value
746 value_arg_coerce (arg)
747      value arg;
748 {
749   register struct type *type;
750
751   /* FIXME: We should coerce this according to the prototype (if we have
752      one).  Right now we do a little bit of this in typecmp(), but that
753      doesn't always get called.  For example, if passing a ref to a function
754      without a prototype, we probably should de-reference it.  Currently
755      we don't.  */
756
757   if (TYPE_CODE (VALUE_TYPE (arg)) == TYPE_CODE_ENUM)
758     arg = value_cast (builtin_type_unsigned_int, arg);
759
760 #if 1   /* FIXME:  This is only a temporary patch.  -fnf */
761   if (VALUE_REPEATED (arg)
762       || TYPE_CODE (VALUE_TYPE (arg)) == TYPE_CODE_ARRAY)
763     arg = value_coerce_array (arg);
764   if (TYPE_CODE (VALUE_TYPE (arg)) == TYPE_CODE_FUNC)
765     arg = value_coerce_function (arg);
766 #endif
767
768   type = VALUE_TYPE (arg);
769
770   if (TYPE_CODE (type) == TYPE_CODE_INT
771       && TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
772     return value_cast (builtin_type_int, arg);
773
774   if (TYPE_CODE (type) == TYPE_CODE_FLT
775       && TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_double))
776     return value_cast (builtin_type_double, arg);
777
778   return arg;
779 }
780
781 /* Push the value ARG, first coercing it as an argument
782    to a C function.  */
783
784 static CORE_ADDR
785 value_arg_push (sp, arg)
786      register CORE_ADDR sp;
787      value arg;
788 {
789   return value_push (sp, value_arg_coerce (arg));
790 }
791
792 /* Determine a function's address and its return type from its value. 
793    Calls error() if the function is not valid for calling.  */
794
795 static CORE_ADDR
796 find_function_addr (function, retval_type)
797      value function;
798      struct type **retval_type;
799 {
800   register struct type *ftype = VALUE_TYPE (function);
801   register enum type_code code = TYPE_CODE (ftype);
802   struct type *value_type;
803   CORE_ADDR funaddr;
804
805   /* If it's a member function, just look at the function
806      part of it.  */
807
808   /* Determine address to call.  */
809   if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD)
810     {
811       funaddr = VALUE_ADDRESS (function);
812       value_type = TYPE_TARGET_TYPE (ftype);
813     }
814   else if (code == TYPE_CODE_PTR)
815     {
816       funaddr = value_as_pointer (function);
817       if (TYPE_CODE (TYPE_TARGET_TYPE (ftype)) == TYPE_CODE_FUNC
818           || TYPE_CODE (TYPE_TARGET_TYPE (ftype)) == TYPE_CODE_METHOD)
819         value_type = TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (ftype));
820       else
821         value_type = builtin_type_int;
822     }
823   else if (code == TYPE_CODE_INT)
824     {
825       /* Handle the case of functions lacking debugging info.
826          Their values are characters since their addresses are char */
827       if (TYPE_LENGTH (ftype) == 1)
828         funaddr = value_as_pointer (value_addr (function));
829       else
830         /* Handle integer used as address of a function.  */
831         funaddr = (CORE_ADDR) value_as_long (function);
832
833       value_type = builtin_type_int;
834     }
835   else
836     error ("Invalid data type for function to be called.");
837
838   *retval_type = value_type;
839   return funaddr;
840 }
841
842 #if defined (CALL_DUMMY)
843 /* All this stuff with a dummy frame may seem unnecessarily complicated
844    (why not just save registers in GDB?).  The purpose of pushing a dummy
845    frame which looks just like a real frame is so that if you call a
846    function and then hit a breakpoint (get a signal, etc), "backtrace"
847    will look right.  Whether the backtrace needs to actually show the
848    stack at the time the inferior function was called is debatable, but
849    it certainly needs to not display garbage.  So if you are contemplating
850    making dummy frames be different from normal frames, consider that.  */
851
852 /* Perform a function call in the inferior.
853    ARGS is a vector of values of arguments (NARGS of them).
854    FUNCTION is a value, the function to be called.
855    Returns a value representing what the function returned.
856    May fail to return, if a breakpoint or signal is hit
857    during the execution of the function.  */
858
859 value
860 call_function_by_hand (function, nargs, args)
861      value function;
862      int nargs;
863      value *args;
864 {
865   register CORE_ADDR sp;
866   register int i;
867   CORE_ADDR start_sp;
868   /* CALL_DUMMY is an array of words (REGISTER_TYPE), but each word
869      is in host byte order.  It is switched to target byte order before calling
870      FIX_CALL_DUMMY.  */
871   static REGISTER_TYPE dummy[] = CALL_DUMMY;
872   REGISTER_TYPE dummy1[sizeof dummy / sizeof (REGISTER_TYPE)];
873   CORE_ADDR old_sp;
874   struct type *value_type;
875   unsigned char struct_return;
876   CORE_ADDR struct_addr;
877   struct inferior_status inf_status;
878   struct cleanup *old_chain;
879   CORE_ADDR funaddr;
880   int using_gcc;
881   CORE_ADDR real_pc;
882
883   if (!target_has_execution)
884     noprocess();
885
886   save_inferior_status (&inf_status, 1);
887   old_chain = make_cleanup (restore_inferior_status, &inf_status);
888
889   /* PUSH_DUMMY_FRAME is responsible for saving the inferior registers
890      (and POP_FRAME for restoring them).  (At least on most machines)
891      they are saved on the stack in the inferior.  */
892   PUSH_DUMMY_FRAME;
893
894   old_sp = sp = read_sp ();
895
896 #if 1 INNER_THAN 2              /* Stack grows down */
897   sp -= sizeof dummy;
898   start_sp = sp;
899 #else                           /* Stack grows up */
900   start_sp = sp;
901   sp += sizeof dummy;
902 #endif
903
904   funaddr = find_function_addr (function, &value_type);
905
906   {
907     struct block *b = block_for_pc (funaddr);
908     /* If compiled without -g, assume GCC.  */
909     using_gcc = b == NULL || BLOCK_GCC_COMPILED (b);
910   }
911
912   /* Are we returning a value using a structure return or a normal
913      value return? */
914
915   struct_return = using_struct_return (function, funaddr, value_type,
916                                        using_gcc);
917
918   /* Create a call sequence customized for this function
919      and the number of arguments for it.  */
920   for (i = 0; i < sizeof dummy / sizeof (REGISTER_TYPE); i++)
921     store_unsigned_integer (&dummy1[i], sizeof (REGISTER_TYPE),
922                             (unsigned LONGEST)dummy[i]);
923
924 #ifdef GDB_TARGET_IS_HPPA
925   real_pc = FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, args,
926                             value_type, using_gcc);
927 #else
928   FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, args,
929                   value_type, using_gcc);
930   real_pc = start_sp;
931 #endif
932
933 #if CALL_DUMMY_LOCATION == ON_STACK
934   write_memory (start_sp, (char *)dummy1, sizeof dummy);
935 #endif /* On stack.  */
936
937 #if CALL_DUMMY_LOCATION == BEFORE_TEXT_END
938   /* Convex Unix prohibits executing in the stack segment. */
939   /* Hope there is empty room at the top of the text segment. */
940   {
941     extern CORE_ADDR text_end;
942     static checked = 0;
943     if (!checked)
944       for (start_sp = text_end - sizeof dummy; start_sp < text_end; ++start_sp)
945         if (read_memory_integer (start_sp, 1) != 0)
946           error ("text segment full -- no place to put call");
947     checked = 1;
948     sp = old_sp;
949     real_pc = text_end - sizeof dummy;
950     write_memory (real_pc, (char *)dummy1, sizeof dummy);
951   }
952 #endif /* Before text_end.  */
953
954 #if CALL_DUMMY_LOCATION == AFTER_TEXT_END
955   {
956     extern CORE_ADDR text_end;
957     int errcode;
958     sp = old_sp;
959     real_pc = text_end;
960     errcode = target_write_memory (real_pc, (char *)dummy1, sizeof dummy);
961     if (errcode != 0)
962       error ("Cannot write text segment -- call_function failed");
963   }
964 #endif /* After text_end.  */
965
966 #if CALL_DUMMY_LOCATION == AT_ENTRY_POINT
967   real_pc = funaddr;
968 #endif /* At entry point.  */
969
970 #ifdef lint
971   sp = old_sp;          /* It really is used, for some ifdef's... */
972 #endif
973
974 #ifdef STACK_ALIGN
975   /* If stack grows down, we must leave a hole at the top. */
976   {
977     int len = 0;
978
979     /* Reserve space for the return structure to be written on the
980        stack, if necessary */
981
982     if (struct_return)
983       len += TYPE_LENGTH (value_type);
984     
985     for (i = nargs - 1; i >= 0; i--)
986       len += TYPE_LENGTH (VALUE_TYPE (value_arg_coerce (args[i])));
987 #ifdef CALL_DUMMY_STACK_ADJUST
988     len += CALL_DUMMY_STACK_ADJUST;
989 #endif
990 #if 1 INNER_THAN 2
991     sp -= STACK_ALIGN (len) - len;
992 #else
993     sp += STACK_ALIGN (len) - len;
994 #endif
995   }
996 #endif /* STACK_ALIGN */
997
998     /* Reserve space for the return structure to be written on the
999        stack, if necessary */
1000
1001     if (struct_return)
1002       {
1003 #if 1 INNER_THAN 2
1004         sp -= TYPE_LENGTH (value_type);
1005         struct_addr = sp;
1006 #else
1007         struct_addr = sp;
1008         sp += TYPE_LENGTH (value_type);
1009 #endif
1010       }
1011
1012 #if defined (REG_STRUCT_HAS_ADDR)
1013   {
1014     /* This is a machine like the sparc, where we need to pass a pointer
1015        to the structure, not the structure itself.  */
1016     if (REG_STRUCT_HAS_ADDR (using_gcc))
1017       for (i = nargs - 1; i >= 0; i--)
1018         if (TYPE_CODE (VALUE_TYPE (args[i])) == TYPE_CODE_STRUCT)
1019           {
1020             CORE_ADDR addr;
1021 #if !(1 INNER_THAN 2)
1022             /* The stack grows up, so the address of the thing we push
1023                is the stack pointer before we push it.  */
1024             addr = sp;
1025 #endif
1026             /* Push the structure.  */
1027             sp = value_push (sp, args[i]);
1028 #if 1 INNER_THAN 2
1029             /* The stack grows down, so the address of the thing we push
1030                is the stack pointer after we push it.  */
1031             addr = sp;
1032 #endif
1033             /* The value we're going to pass is the address of the thing
1034                we just pushed.  */
1035             args[i] = value_from_longest (lookup_pointer_type (value_type),
1036                                        (LONGEST) addr);
1037           }
1038   }
1039 #endif /* REG_STRUCT_HAS_ADDR.  */
1040
1041 #ifdef PUSH_ARGUMENTS
1042   PUSH_ARGUMENTS(nargs, args, sp, struct_return, struct_addr);
1043 #else /* !PUSH_ARGUMENTS */
1044   for (i = nargs - 1; i >= 0; i--)
1045     sp = value_arg_push (sp, args[i]);
1046 #endif /* !PUSH_ARGUMENTS */
1047
1048 #ifdef CALL_DUMMY_STACK_ADJUST
1049 #if 1 INNER_THAN 2
1050   sp -= CALL_DUMMY_STACK_ADJUST;
1051 #else
1052   sp += CALL_DUMMY_STACK_ADJUST;
1053 #endif
1054 #endif /* CALL_DUMMY_STACK_ADJUST */
1055
1056   /* Store the address at which the structure is supposed to be
1057      written.  Note that this (and the code which reserved the space
1058      above) assumes that gcc was used to compile this function.  Since
1059      it doesn't cost us anything but space and if the function is pcc
1060      it will ignore this value, we will make that assumption.
1061
1062      Also note that on some machines (like the sparc) pcc uses a 
1063      convention like gcc's.  */
1064
1065   if (struct_return)
1066     STORE_STRUCT_RETURN (struct_addr, sp);
1067
1068   /* Write the stack pointer.  This is here because the statements above
1069      might fool with it.  On SPARC, this write also stores the register
1070      window into the right place in the new stack frame, which otherwise
1071      wouldn't happen.  (See store_inferior_registers in sparc-nat.c.)  */
1072   write_sp (sp);
1073
1074   {
1075     char retbuf[REGISTER_BYTES];
1076     char *name;
1077     struct symbol *symbol;
1078
1079     name = NULL;
1080     symbol = find_pc_function (funaddr);
1081     if (symbol)
1082       {
1083         name = SYMBOL_SOURCE_NAME (symbol);
1084       }
1085     else
1086       {
1087         /* Try the minimal symbols.  */
1088         struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (funaddr);
1089
1090         if (msymbol)
1091           {
1092             name = SYMBOL_SOURCE_NAME (msymbol);
1093           }
1094       }
1095     if (name == NULL)
1096       {
1097         char format[80];
1098         sprintf (format, "at %s", local_hex_format ());
1099         name = alloca (80);
1100         sprintf (name, format, (unsigned long) funaddr);
1101       }
1102
1103     /* Execute the stack dummy routine, calling FUNCTION.
1104        When it is done, discard the empty frame
1105        after storing the contents of all regs into retbuf.  */
1106     if (run_stack_dummy (real_pc + CALL_DUMMY_START_OFFSET, retbuf))
1107       {
1108         /* We stopped somewhere besides the call dummy.  */
1109
1110         /* If we did the cleanups, we would print a spurious error message
1111            (Unable to restore previously selected frame), would write the
1112            registers from the inf_status (which is wrong), and would do other
1113            wrong things (like set stop_bpstat to the wrong thing).  */
1114         discard_cleanups (old_chain);
1115         /* Prevent memory leak.  */
1116         bpstat_clear (&inf_status.stop_bpstat);
1117
1118         /* The following error message used to say "The expression
1119            which contained the function call has been discarded."  It
1120            is a hard concept to explain in a few words.  Ideally, GDB
1121            would be able to resume evaluation of the expression when
1122            the function finally is done executing.  Perhaps someday
1123            this will be implemented (it would not be easy).  */
1124
1125         /* FIXME: Insert a bunch of wrap_here; name can be very long if it's
1126            a C++ name with arguments and stuff.  */
1127         error ("\
1128 The program being debugged stopped while in a function called from GDB.\n\
1129 When the function (%s) is done executing, GDB will silently\n\
1130 stop (instead of continuing to evaluate the expression containing\n\
1131 the function call).", name);
1132       }
1133
1134     do_cleanups (old_chain);
1135
1136     /* Figure out the value returned by the function.  */
1137     return value_being_returned (value_type, retbuf, struct_return);
1138   }
1139 }
1140 #else /* no CALL_DUMMY.  */
1141 value
1142 call_function_by_hand (function, nargs, args)
1143      value function;
1144      int nargs;
1145      value *args;
1146 {
1147   error ("Cannot invoke functions on this machine.");
1148 }
1149 #endif /* no CALL_DUMMY.  */
1150
1151 \f
1152 /* Create a value for an array by allocating space in the inferior, copying
1153    the data into that space, and then setting up an array value.
1154
1155    The array bounds are set from LOWBOUND and HIGHBOUND, and the array is
1156    populated from the values passed in ELEMVEC.
1157
1158    The element type of the array is inherited from the type of the
1159    first element, and all elements must have the same size (though we
1160    don't currently enforce any restriction on their types). */
1161
1162 value
1163 value_array (lowbound, highbound, elemvec)
1164      int lowbound;
1165      int highbound;
1166      value *elemvec;
1167 {
1168   int nelem;
1169   int idx;
1170   int typelength;
1171   value val;
1172   struct type *rangetype;
1173   struct type *arraytype;
1174   CORE_ADDR addr;
1175
1176   /* Validate that the bounds are reasonable and that each of the elements
1177      have the same size. */
1178
1179   nelem = highbound - lowbound + 1;
1180   if (nelem <= 0)
1181     {
1182       error ("bad array bounds (%d, %d)", lowbound, highbound);
1183     }
1184   typelength = TYPE_LENGTH (VALUE_TYPE (elemvec[0]));
1185   for (idx = 0; idx < nelem; idx++)
1186     {
1187       if (TYPE_LENGTH (VALUE_TYPE (elemvec[idx])) != typelength)
1188         {
1189           error ("array elements must all be the same size");
1190         }
1191     }
1192
1193   /* Allocate space to store the array in the inferior, and then initialize
1194      it by copying in each element.  FIXME:  Is it worth it to create a
1195      local buffer in which to collect each value and then write all the
1196      bytes in one operation? */
1197
1198   addr = allocate_space_in_inferior (nelem * typelength);
1199   for (idx = 0; idx < nelem; idx++)
1200     {
1201       write_memory (addr + (idx * typelength), VALUE_CONTENTS (elemvec[idx]),
1202                     typelength);
1203     }
1204
1205   /* Create the array type and set up an array value to be evaluated lazily. */
1206
1207   rangetype = create_range_type ((struct type *) NULL, builtin_type_int,
1208                                  lowbound, highbound);
1209   arraytype = create_array_type ((struct type *) NULL, 
1210                                  VALUE_TYPE (elemvec[0]), rangetype);
1211   val = value_at_lazy (arraytype, addr);
1212   return (val);
1213 }
1214
1215 /* Create a value for a string constant by allocating space in the inferior,
1216    copying the data into that space, and returning the address with type
1217    TYPE_CODE_STRING.  PTR points to the string constant data; LEN is number
1218    of characters.
1219    Note that string types are like array of char types with a lower bound of
1220    zero and an upper bound of LEN - 1.  Also note that the string may contain
1221    embedded null bytes. */
1222
1223 value
1224 value_string (ptr, len)
1225      char *ptr;
1226      int len;
1227 {
1228   value val;
1229   struct type *rangetype;
1230   struct type *stringtype;
1231   CORE_ADDR addr;
1232
1233   /* Allocate space to store the string in the inferior, and then
1234      copy LEN bytes from PTR in gdb to that address in the inferior. */
1235
1236   addr = allocate_space_in_inferior (len);
1237   write_memory (addr, ptr, len);
1238
1239   /* Create the string type and set up a string value to be evaluated
1240      lazily. */
1241
1242   rangetype = create_range_type ((struct type *) NULL, builtin_type_int,
1243                                  0, len - 1);
1244   stringtype = create_string_type ((struct type *) NULL, rangetype);
1245   val = value_at_lazy (stringtype, addr);
1246   return (val);
1247 }
1248 \f
1249 /* See if we can pass arguments in T2 to a function which takes arguments
1250    of types T1.  Both t1 and t2 are NULL-terminated vectors.  If some
1251    arguments need coercion of some sort, then the coerced values are written
1252    into T2.  Return value is 0 if the arguments could be matched, or the
1253    position at which they differ if not.
1254
1255    STATICP is nonzero if the T1 argument list came from a
1256    static member function.
1257
1258    For non-static member functions, we ignore the first argument,
1259    which is the type of the instance variable.  This is because we want
1260    to handle calls with objects from derived classes.  This is not
1261    entirely correct: we should actually check to make sure that a
1262    requested operation is type secure, shouldn't we?  FIXME.  */
1263
1264 static int
1265 typecmp (staticp, t1, t2)
1266      int staticp;
1267      struct type *t1[];
1268      value t2[];
1269 {
1270   int i;
1271
1272   if (t2 == 0)
1273     return 1;
1274   if (staticp && t1 == 0)
1275     return t2[1] != 0;
1276   if (t1 == 0)
1277     return 1;
1278   if (TYPE_CODE (t1[0]) == TYPE_CODE_VOID) return 0;
1279   if (t1[!staticp] == 0) return 0;
1280   for (i = !staticp; t1[i] && TYPE_CODE (t1[i]) != TYPE_CODE_VOID; i++)
1281     {
1282     struct type *tt1, *tt2;
1283       if (! t2[i])
1284         return i+1;
1285       tt1 = t1[i];
1286       tt2 = VALUE_TYPE(t2[i]);
1287       if (TYPE_CODE (tt1) == TYPE_CODE_REF
1288           /* We should be doing hairy argument matching, as below.  */
1289           && (TYPE_CODE (TYPE_TARGET_TYPE (tt1)) == TYPE_CODE (tt2)))
1290         {
1291           t2[i] = value_addr (t2[i]);
1292           continue;
1293         }
1294
1295       while (TYPE_CODE (tt1) == TYPE_CODE_PTR
1296           && (TYPE_CODE(tt2)==TYPE_CODE_ARRAY || TYPE_CODE(tt2)==TYPE_CODE_PTR))
1297         {
1298            tt1 = TYPE_TARGET_TYPE(tt1); 
1299            tt2 = TYPE_TARGET_TYPE(tt2);
1300         }
1301       if (TYPE_CODE(tt1) == TYPE_CODE(tt2)) continue;
1302       /* Array to pointer is a `trivial conversion' according to the ARM.  */
1303
1304       /* We should be doing much hairier argument matching (see section 13.2
1305          of the ARM), but as a quick kludge, just check for the same type
1306          code.  */
1307       if (TYPE_CODE (t1[i]) != TYPE_CODE (VALUE_TYPE (t2[i])))
1308         return i+1;
1309     }
1310   if (!t1[i]) return 0;
1311   return t2[i] ? i+1 : 0;
1312 }
1313
1314 /* Helper function used by value_struct_elt to recurse through baseclasses.
1315    Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1316    and search in it assuming it has (class) type TYPE.
1317    If found, return value, else return NULL.
1318
1319    If LOOKING_FOR_BASECLASS, then instead of looking for struct fields,
1320    look for a baseclass named NAME.  */
1321
1322 static value
1323 search_struct_field (name, arg1, offset, type, looking_for_baseclass)
1324      char *name;
1325      register value arg1;
1326      int offset;
1327      register struct type *type;
1328      int looking_for_baseclass;
1329 {
1330   int i;
1331
1332   check_stub_type (type);
1333
1334   if (! looking_for_baseclass)
1335     for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
1336       {
1337         char *t_field_name = TYPE_FIELD_NAME (type, i);
1338
1339         if (t_field_name && STREQ (t_field_name, name))
1340           {
1341             value v;
1342             if (TYPE_FIELD_STATIC (type, i))
1343               {
1344                 char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, i);
1345                 struct symbol *sym =
1346                     lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL);
1347                 if (sym == NULL)
1348                     error ("Internal error: could not find physical static variable named %s",
1349                            phys_name);
1350                 v = value_at (TYPE_FIELD_TYPE (type, i),
1351                               (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym));
1352               }
1353             else
1354               v = value_primitive_field (arg1, offset, i, type);
1355             if (v == 0)
1356               error("there is no field named %s", name);
1357             return v;
1358           }
1359       }
1360
1361   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1362     {
1363       value v;
1364       /* If we are looking for baseclasses, this is what we get when we
1365          hit them.  But it could happen that the base part's member name
1366          is not yet filled in.  */
1367       int found_baseclass = (looking_for_baseclass
1368                              && TYPE_BASECLASS_NAME (type, i) != NULL
1369                              && STREQ (name, TYPE_BASECLASS_NAME (type, i)));
1370
1371       if (BASETYPE_VIA_VIRTUAL (type, i))
1372         {
1373           value v2;
1374           /* Fix to use baseclass_offset instead. FIXME */
1375           baseclass_addr (type, i, VALUE_CONTENTS (arg1) + offset,
1376                           &v2, (int *)NULL);
1377           if (v2 == 0)
1378             error ("virtual baseclass botch");
1379           if (found_baseclass)
1380             return v2;
1381           v = search_struct_field (name, v2, 0, TYPE_BASECLASS (type, i),
1382                                    looking_for_baseclass);
1383         }
1384       else if (found_baseclass)
1385         v = value_primitive_field (arg1, offset, i, type);
1386       else
1387         v = search_struct_field (name, arg1,
1388                                  offset + TYPE_BASECLASS_BITPOS (type, i) / 8,
1389                                  TYPE_BASECLASS (type, i),
1390                                  looking_for_baseclass);
1391       if (v) return v;
1392     }
1393   return NULL;
1394 }
1395
1396 /* Helper function used by value_struct_elt to recurse through baseclasses.
1397    Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1398    and search in it assuming it has (class) type TYPE.
1399    If found, return value, else if name matched and args not return (value)-1,
1400    else return NULL. */
1401
1402 static value
1403 search_struct_method (name, arg1p, args, offset, static_memfuncp, type)
1404      char *name;
1405      register value *arg1p, *args;
1406      int offset, *static_memfuncp;
1407      register struct type *type;
1408 {
1409   int i;
1410   value v;
1411   static int name_matched = 0;
1412   char dem_opname[64];
1413
1414   check_stub_type (type);
1415   for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
1416     {
1417       char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
1418       if (strncmp(t_field_name, "__", 2)==0 ||
1419         strncmp(t_field_name, "op", 2)==0 ||
1420         strncmp(t_field_name, "type", 4)==0 )
1421         {
1422           if (cplus_demangle_opname(t_field_name, dem_opname, DMGL_ANSI))
1423             t_field_name = dem_opname;
1424           else if (cplus_demangle_opname(t_field_name, dem_opname, 0))
1425             t_field_name = dem_opname; 
1426         }
1427       if (t_field_name && STREQ (t_field_name, name))
1428         {
1429           int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1;
1430           struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1431           name_matched = 1; 
1432
1433           if (j > 0 && args == 0)
1434             error ("cannot resolve overloaded method `%s'", name);
1435           while (j >= 0)
1436             {
1437               if (TYPE_FN_FIELD_STUB (f, j))
1438                 check_stub_method (type, i, j);
1439               if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j),
1440                             TYPE_FN_FIELD_ARGS (f, j), args))
1441                 {
1442                   if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1443                     return (value)value_virtual_fn_field (arg1p, f, j, type, offset);
1444                   if (TYPE_FN_FIELD_STATIC_P (f, j) && static_memfuncp)
1445                     *static_memfuncp = 1;
1446                   v = (value)value_fn_field (arg1p, f, j, type, offset);
1447                   if (v != (value)NULL) return v;
1448                 }
1449               j--;
1450             }
1451         }
1452     }
1453
1454   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1455     {
1456       int base_offset;
1457
1458       if (BASETYPE_VIA_VIRTUAL (type, i))
1459         {
1460           base_offset = baseclass_offset (type, i, *arg1p, offset);
1461           if (base_offset == -1)
1462             error ("virtual baseclass botch");
1463         }
1464       else
1465         {
1466           base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
1467         }
1468       v = search_struct_method (name, arg1p, args, base_offset + offset,
1469                                 static_memfuncp, TYPE_BASECLASS (type, i));
1470       if (v == (value) -1)
1471         {
1472           name_matched = 1;
1473         }
1474       else if (v)
1475         {
1476 /* FIXME-bothner:  Why is this commented out?  Why is it here?  */
1477 /*        *arg1p = arg1_tmp;*/
1478           return v;
1479         }
1480     }
1481   if (name_matched) return (value) -1;
1482   else return NULL;
1483 }
1484
1485 /* Given *ARGP, a value of type (pointer to a)* structure/union,
1486    extract the component named NAME from the ultimate target structure/union
1487    and return it as a value with its appropriate type.
1488    ERR is used in the error message if *ARGP's type is wrong.
1489
1490    C++: ARGS is a list of argument types to aid in the selection of
1491    an appropriate method. Also, handle derived types.
1492
1493    STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
1494    where the truthvalue of whether the function that was resolved was
1495    a static member function or not is stored.
1496
1497    ERR is an error message to be printed in case the field is not found.  */
1498
1499 value
1500 value_struct_elt (argp, args, name, static_memfuncp, err)
1501      register value *argp, *args;
1502      char *name;
1503      int *static_memfuncp;
1504      char *err;
1505 {
1506   register struct type *t;
1507   value v;
1508
1509   COERCE_ARRAY (*argp);
1510
1511   t = VALUE_TYPE (*argp);
1512
1513   /* Follow pointers until we get to a non-pointer.  */
1514
1515   while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1516     {
1517       *argp = value_ind (*argp);
1518       /* Don't coerce fn pointer to fn and then back again!  */
1519       if (TYPE_CODE (VALUE_TYPE (*argp)) != TYPE_CODE_FUNC)
1520         COERCE_ARRAY (*argp);
1521       t = VALUE_TYPE (*argp);
1522     }
1523
1524   if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1525     error ("not implemented: member type in value_struct_elt");
1526
1527   if (   TYPE_CODE (t) != TYPE_CODE_STRUCT
1528       && TYPE_CODE (t) != TYPE_CODE_UNION)
1529     error ("Attempt to extract a component of a value that is not a %s.", err);
1530
1531   /* Assume it's not, unless we see that it is.  */
1532   if (static_memfuncp)
1533     *static_memfuncp =0;
1534
1535   if (!args)
1536     {
1537       /* if there are no arguments ...do this...  */
1538
1539       /* Try as a field first, because if we succeed, there
1540          is less work to be done.  */
1541       v = search_struct_field (name, *argp, 0, t, 0);
1542       if (v)
1543         return v;
1544
1545       /* C++: If it was not found as a data field, then try to
1546          return it as a pointer to a method.  */
1547
1548       if (destructor_name_p (name, t))
1549         error ("Cannot get value of destructor");
1550
1551       v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
1552
1553       if (v == 0)
1554         {
1555           if (TYPE_NFN_FIELDS (t))
1556             error ("There is no member or method named %s.", name);
1557           else
1558             error ("There is no member named %s.", name);
1559         }
1560       return v;
1561     }
1562
1563   if (destructor_name_p (name, t))
1564     {
1565       if (!args[1])
1566         {
1567           /* destructors are a special case.  */
1568           v = (value)value_fn_field (NULL, TYPE_FN_FIELDLIST1 (t, 0),
1569                                 TYPE_FN_FIELDLIST_LENGTH (t, 0), 0, 0);
1570           if (!v) error("could not find destructor function named %s.", name);
1571           else return v;
1572         }
1573       else
1574         {
1575           error ("destructor should not have any argument");
1576         }
1577     }
1578   else
1579     v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
1580
1581   if (v == (value) -1)
1582     {
1583         error("Argument list of %s mismatch with component in the structure.", name);
1584     }
1585   else if (v == 0)
1586     {
1587       /* See if user tried to invoke data as function.  If so,
1588          hand it back.  If it's not callable (i.e., a pointer to function),
1589          gdb should give an error.  */
1590       v = search_struct_field (name, *argp, 0, t, 0);
1591     }
1592
1593   if (!v)
1594     error ("Structure has no component named %s.", name);
1595   return v;
1596 }
1597
1598 /* C++: return 1 is NAME is a legitimate name for the destructor
1599    of type TYPE.  If TYPE does not have a destructor, or
1600    if NAME is inappropriate for TYPE, an error is signaled.  */
1601 int
1602 destructor_name_p (name, type)
1603      const char *name;
1604      const struct type *type;
1605 {
1606   /* destructors are a special case.  */
1607
1608   if (name[0] == '~')
1609     {
1610       char *dname = type_name_no_tag (type);
1611       if (!STREQ (dname, name+1))
1612         error ("name of destructor must equal name of class");
1613       else
1614         return 1;
1615     }
1616   return 0;
1617 }
1618
1619 /* Helper function for check_field: Given TYPE, a structure/union,
1620    return 1 if the component named NAME from the ultimate
1621    target structure/union is defined, otherwise, return 0. */
1622
1623 static int
1624 check_field_in (type, name)
1625      register struct type *type;
1626      const char *name;
1627 {
1628   register int i;
1629
1630   for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
1631     {
1632       char *t_field_name = TYPE_FIELD_NAME (type, i);
1633       if (t_field_name && STREQ (t_field_name, name))
1634         return 1;
1635     }
1636
1637   /* C++: If it was not found as a data field, then try to
1638      return it as a pointer to a method.  */
1639
1640   /* Destructors are a special case.  */
1641   if (destructor_name_p (name, type))
1642     return 1;
1643
1644   for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
1645     {
1646       if (STREQ (TYPE_FN_FIELDLIST_NAME (type, i), name))
1647         return 1;
1648     }
1649
1650   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1651     if (check_field_in (TYPE_BASECLASS (type, i), name))
1652       return 1;
1653       
1654   return 0;
1655 }
1656
1657
1658 /* C++: Given ARG1, a value of type (pointer to a)* structure/union,
1659    return 1 if the component named NAME from the ultimate
1660    target structure/union is defined, otherwise, return 0.  */
1661
1662 int
1663 check_field (arg1, name)
1664      register value arg1;
1665      const char *name;
1666 {
1667   register struct type *t;
1668
1669   COERCE_ARRAY (arg1);
1670
1671   t = VALUE_TYPE (arg1);
1672
1673   /* Follow pointers until we get to a non-pointer.  */
1674
1675   while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1676     t = TYPE_TARGET_TYPE (t);
1677
1678   if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1679     error ("not implemented: member type in check_field");
1680
1681   if (   TYPE_CODE (t) != TYPE_CODE_STRUCT
1682       && TYPE_CODE (t) != TYPE_CODE_UNION)
1683     error ("Internal error: `this' is not an aggregate");
1684
1685   return check_field_in (t, name);
1686 }
1687
1688 /* C++: Given an aggregate type CURTYPE, and a member name NAME,
1689    return the address of this member as a "pointer to member"
1690    type.  If INTYPE is non-null, then it will be the type
1691    of the member we are looking for.  This will help us resolve
1692    "pointers to member functions".  This function is used
1693    to resolve user expressions of the form "DOMAIN::NAME".  */
1694
1695 value
1696 value_struct_elt_for_reference (domain, offset, curtype, name, intype)
1697      struct type *domain, *curtype, *intype;
1698      int offset;
1699      char *name;
1700 {
1701   register struct type *t = curtype;
1702   register int i;
1703   value v;
1704
1705   if (   TYPE_CODE (t) != TYPE_CODE_STRUCT
1706       && TYPE_CODE (t) != TYPE_CODE_UNION)
1707     error ("Internal error: non-aggregate type to value_struct_elt_for_reference");
1708
1709   for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
1710     {
1711       char *t_field_name = TYPE_FIELD_NAME (t, i);
1712       
1713       if (t_field_name && STREQ (t_field_name, name))
1714         {
1715           if (TYPE_FIELD_STATIC (t, i))
1716             {
1717               char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (t, i);
1718               struct symbol *sym =
1719                 lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL);
1720               if (sym == NULL)
1721                 error ("Internal error: could not find physical static variable named %s",
1722                        phys_name);
1723               return value_at (SYMBOL_TYPE (sym),
1724                                (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym));
1725             }
1726           if (TYPE_FIELD_PACKED (t, i))
1727             error ("pointers to bitfield members not allowed");
1728           
1729           return value_from_longest
1730             (lookup_reference_type (lookup_member_type (TYPE_FIELD_TYPE (t, i),
1731                                                         domain)),
1732              offset + (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3));
1733         }
1734     }
1735
1736   /* C++: If it was not found as a data field, then try to
1737      return it as a pointer to a method.  */
1738
1739   /* Destructors are a special case.  */
1740   if (destructor_name_p (name, t))
1741     {
1742       error ("member pointers to destructors not implemented yet");
1743     }
1744
1745   /* Perform all necessary dereferencing.  */
1746   while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR)
1747     intype = TYPE_TARGET_TYPE (intype);
1748
1749   for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
1750     {
1751       char *t_field_name = TYPE_FN_FIELDLIST_NAME (t, i);
1752       char dem_opname[64];
1753
1754       if (strncmp(t_field_name, "__", 2)==0 ||
1755         strncmp(t_field_name, "op", 2)==0 ||
1756         strncmp(t_field_name, "type", 4)==0 )
1757         {
1758           if (cplus_demangle_opname(t_field_name, dem_opname, DMGL_ANSI))
1759             t_field_name = dem_opname;
1760           else if (cplus_demangle_opname(t_field_name, dem_opname, 0))
1761             t_field_name = dem_opname; 
1762         }
1763       if (t_field_name && STREQ (t_field_name, name))
1764         {
1765           int j = TYPE_FN_FIELDLIST_LENGTH (t, i);
1766           struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
1767           
1768           if (intype == 0 && j > 1)
1769             error ("non-unique member `%s' requires type instantiation", name);
1770           if (intype)
1771             {
1772               while (j--)
1773                 if (TYPE_FN_FIELD_TYPE (f, j) == intype)
1774                   break;
1775               if (j < 0)
1776                 error ("no member function matches that type instantiation");
1777             }
1778           else
1779             j = 0;
1780           
1781           if (TYPE_FN_FIELD_STUB (f, j))
1782             check_stub_method (t, i, j);
1783           if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1784             {
1785               return value_from_longest
1786                 (lookup_reference_type
1787                  (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
1788                                       domain)),
1789                  (LONGEST) METHOD_PTR_FROM_VOFFSET
1790                   (TYPE_FN_FIELD_VOFFSET (f, j)));
1791             }
1792           else
1793             {
1794               struct symbol *s = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
1795                                                 0, VAR_NAMESPACE, 0, NULL);
1796               if (s == NULL)
1797                 {
1798                   v = 0;
1799                 }
1800               else
1801                 {
1802                   v = read_var_value (s, 0);
1803 #if 0
1804                   VALUE_TYPE (v) = lookup_reference_type
1805                     (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
1806                                          domain));
1807 #endif
1808                 }
1809               return v;
1810             }
1811         }
1812     }
1813   for (i = TYPE_N_BASECLASSES (t) - 1; i >= 0; i--)
1814     {
1815       value v;
1816       int base_offset;
1817
1818       if (BASETYPE_VIA_VIRTUAL (t, i))
1819         base_offset = 0;
1820       else
1821         base_offset = TYPE_BASECLASS_BITPOS (t, i) / 8;
1822       v = value_struct_elt_for_reference (domain,
1823                                           offset + base_offset,
1824                                           TYPE_BASECLASS (t, i),
1825                                           name,
1826                                           intype);
1827       if (v)
1828         return v;
1829     }
1830   return 0;
1831 }
1832
1833 /* C++: return the value of the class instance variable, if one exists.
1834    Flag COMPLAIN signals an error if the request is made in an
1835    inappropriate context.  */
1836 value
1837 value_of_this (complain)
1838      int complain;
1839 {
1840   extern FRAME selected_frame;
1841   struct symbol *func, *sym;
1842   struct block *b;
1843   int i;
1844   static const char funny_this[] = "this";
1845   value this;
1846
1847   if (selected_frame == 0)
1848     if (complain)
1849       error ("no frame selected");
1850     else return 0;
1851
1852   func = get_frame_function (selected_frame);
1853   if (!func)
1854     {
1855       if (complain)
1856         error ("no `this' in nameless context");
1857       else return 0;
1858     }
1859
1860   b = SYMBOL_BLOCK_VALUE (func);
1861   i = BLOCK_NSYMS (b);
1862   if (i <= 0)
1863     if (complain)
1864       error ("no args, no `this'");
1865     else return 0;
1866
1867   /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
1868      symbol instead of the LOC_ARG one (if both exist).  */
1869   sym = lookup_block_symbol (b, funny_this, VAR_NAMESPACE);
1870   if (sym == NULL)
1871     {
1872       if (complain)
1873         error ("current stack frame not in method");
1874       else
1875         return NULL;
1876     }
1877
1878   this = read_var_value (sym, selected_frame);
1879   if (this == 0 && complain)
1880     error ("`this' argument at unknown address");
1881   return this;
1882 }