2004-04-26 Orjan Friberg <orjanf@axis.com>
[platform/upstream/binutils.git] / gdb / valops.c
1 /* Perform non-arithmetic operations on values, for GDB.
2    Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
3    1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
4    Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place - Suite 330,
21    Boston, MA 02111-1307, USA.  */
22
23 #include "defs.h"
24 #include "symtab.h"
25 #include "gdbtypes.h"
26 #include "value.h"
27 #include "frame.h"
28 #include "inferior.h"
29 #include "gdbcore.h"
30 #include "target.h"
31 #include "demangle.h"
32 #include "language.h"
33 #include "gdbcmd.h"
34 #include "regcache.h"
35 #include "cp-abi.h"
36 #include "block.h"
37 #include "infcall.h"
38 #include "dictionary.h"
39 #include "cp-support.h"
40
41 #include <errno.h>
42 #include "gdb_string.h"
43 #include "gdb_assert.h"
44 #include "cp-support.h"
45 #include "observer.h"
46
47 extern int overload_debug;
48 /* Local functions.  */
49
50 static int typecmp (int staticp, int varargs, int nargs,
51                     struct field t1[], struct value *t2[]);
52
53 static CORE_ADDR value_push (CORE_ADDR, struct value *);
54
55 static struct value *search_struct_field (char *, struct value *, int,
56                                       struct type *, int);
57
58 static struct value *search_struct_method (char *, struct value **,
59                                        struct value **,
60                                        int, int *, struct type *);
61
62 static int find_oload_champ_namespace (struct type **arg_types, int nargs,
63                                        const char *func_name,
64                                        const char *qualified_name,
65                                        struct symbol ***oload_syms,
66                                        struct badness_vector **oload_champ_bv);
67
68 static
69 int find_oload_champ_namespace_loop (struct type **arg_types, int nargs,
70                                      const char *func_name,
71                                      const char *qualified_name,
72                                      int namespace_len,
73                                      struct symbol ***oload_syms,
74                                      struct badness_vector **oload_champ_bv,
75                                      int *oload_champ);
76
77 static int find_oload_champ (struct type **arg_types, int nargs, int method,
78                              int num_fns,
79                              struct fn_field *fns_ptr,
80                              struct symbol **oload_syms,
81                              struct badness_vector **oload_champ_bv);
82
83 static int oload_method_static (int method, struct fn_field *fns_ptr,
84                                 int index);
85
86 enum oload_classification { STANDARD, NON_STANDARD, INCOMPATIBLE };
87
88 static enum
89 oload_classification classify_oload_match (struct badness_vector
90                                            * oload_champ_bv,
91                                            int nargs,
92                                            int static_offset);
93
94 static int check_field_in (struct type *, const char *);
95
96 static struct value *value_struct_elt_for_reference (struct type *domain,
97                                                      int offset,
98                                                      struct type *curtype,
99                                                      char *name,
100                                                      struct type *intype,
101                                                      enum noside noside);
102
103 static struct value *value_namespace_elt (const struct type *curtype,
104                                           char *name,
105                                           enum noside noside);
106
107 static struct value *value_maybe_namespace_elt (const struct type *curtype,
108                                                 char *name,
109                                                 enum noside noside);
110
111 static CORE_ADDR allocate_space_in_inferior (int);
112
113 static struct value *cast_into_complex (struct type *, struct value *);
114
115 static struct fn_field *find_method_list (struct value ** argp, char *method,
116                                           int offset,
117                                           struct type *type, int *num_fns,
118                                           struct type **basetype,
119                                           int *boffset);
120
121 void _initialize_valops (void);
122
123 /* Flag for whether we want to abandon failed expression evals by default.  */
124
125 #if 0
126 static int auto_abandon = 0;
127 #endif
128
129 int overload_resolution = 0;
130
131 /* Find the address of function name NAME in the inferior.  */
132
133 struct value *
134 find_function_in_inferior (const char *name)
135 {
136   struct symbol *sym;
137   sym = lookup_symbol (name, 0, VAR_DOMAIN, 0, NULL);
138   if (sym != NULL)
139     {
140       if (SYMBOL_CLASS (sym) != LOC_BLOCK)
141         {
142           error ("\"%s\" exists in this program but is not a function.",
143                  name);
144         }
145       return value_of_variable (sym, NULL);
146     }
147   else
148     {
149       struct minimal_symbol *msymbol = lookup_minimal_symbol (name, NULL, NULL);
150       if (msymbol != NULL)
151         {
152           struct type *type;
153           CORE_ADDR maddr;
154           type = lookup_pointer_type (builtin_type_char);
155           type = lookup_function_type (type);
156           type = lookup_pointer_type (type);
157           maddr = SYMBOL_VALUE_ADDRESS (msymbol);
158           return value_from_pointer (type, maddr);
159         }
160       else
161         {
162           if (!target_has_execution)
163             error ("evaluation of this expression requires the target program to be active");
164           else
165             error ("evaluation of this expression requires the program to have a function \"%s\".", name);
166         }
167     }
168 }
169
170 /* Allocate NBYTES of space in the inferior using the inferior's malloc
171    and return a value that is a pointer to the allocated space. */
172
173 struct value *
174 value_allocate_space_in_inferior (int len)
175 {
176   struct value *blocklen;
177   struct value *val = find_function_in_inferior (NAME_OF_MALLOC);
178
179   blocklen = value_from_longest (builtin_type_int, (LONGEST) len);
180   val = call_function_by_hand (val, 1, &blocklen);
181   if (value_logical_not (val))
182     {
183       if (!target_has_execution)
184         error ("No memory available to program now: you need to start the target first");
185       else
186         error ("No memory available to program: call to malloc failed");
187     }
188   return val;
189 }
190
191 static CORE_ADDR
192 allocate_space_in_inferior (int len)
193 {
194   return value_as_long (value_allocate_space_in_inferior (len));
195 }
196
197 /* Cast value ARG2 to type TYPE and return as a value.
198    More general than a C cast: accepts any two types of the same length,
199    and if ARG2 is an lvalue it can be cast into anything at all.  */
200 /* In C++, casts may change pointer or object representations.  */
201
202 struct value *
203 value_cast (struct type *type, struct value *arg2)
204 {
205   enum type_code code1;
206   enum type_code code2;
207   int scalar;
208   struct type *type2;
209
210   int convert_to_boolean = 0;
211
212   if (VALUE_TYPE (arg2) == type)
213     return arg2;
214
215   CHECK_TYPEDEF (type);
216   code1 = TYPE_CODE (type);
217   COERCE_REF (arg2);
218   type2 = check_typedef (VALUE_TYPE (arg2));
219
220   /* A cast to an undetermined-length array_type, such as (TYPE [])OBJECT,
221      is treated like a cast to (TYPE [N])OBJECT,
222      where N is sizeof(OBJECT)/sizeof(TYPE). */
223   if (code1 == TYPE_CODE_ARRAY)
224     {
225       struct type *element_type = TYPE_TARGET_TYPE (type);
226       unsigned element_length = TYPE_LENGTH (check_typedef (element_type));
227       if (element_length > 0
228         && TYPE_ARRAY_UPPER_BOUND_TYPE (type) == BOUND_CANNOT_BE_DETERMINED)
229         {
230           struct type *range_type = TYPE_INDEX_TYPE (type);
231           int val_length = TYPE_LENGTH (type2);
232           LONGEST low_bound, high_bound, new_length;
233           if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
234             low_bound = 0, high_bound = 0;
235           new_length = val_length / element_length;
236           if (val_length % element_length != 0)
237             warning ("array element type size does not divide object size in cast");
238           /* FIXME-type-allocation: need a way to free this type when we are
239              done with it.  */
240           range_type = create_range_type ((struct type *) NULL,
241                                           TYPE_TARGET_TYPE (range_type),
242                                           low_bound,
243                                           new_length + low_bound - 1);
244           VALUE_TYPE (arg2) = create_array_type ((struct type *) NULL,
245                                                  element_type, range_type);
246           return arg2;
247         }
248     }
249
250   if (current_language->c_style_arrays
251       && TYPE_CODE (type2) == TYPE_CODE_ARRAY)
252     arg2 = value_coerce_array (arg2);
253
254   if (TYPE_CODE (type2) == TYPE_CODE_FUNC)
255     arg2 = value_coerce_function (arg2);
256
257   type2 = check_typedef (VALUE_TYPE (arg2));
258   COERCE_VARYING_ARRAY (arg2, type2);
259   code2 = TYPE_CODE (type2);
260
261   if (code1 == TYPE_CODE_COMPLEX)
262     return cast_into_complex (type, arg2);
263   if (code1 == TYPE_CODE_BOOL)
264     {
265       code1 = TYPE_CODE_INT;
266       convert_to_boolean = 1;
267     }
268   if (code1 == TYPE_CODE_CHAR)
269     code1 = TYPE_CODE_INT;
270   if (code2 == TYPE_CODE_BOOL || code2 == TYPE_CODE_CHAR)
271     code2 = TYPE_CODE_INT;
272
273   scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
274             || code2 == TYPE_CODE_ENUM || code2 == TYPE_CODE_RANGE);
275
276   if (code1 == TYPE_CODE_STRUCT
277       && code2 == TYPE_CODE_STRUCT
278       && TYPE_NAME (type) != 0)
279     {
280       /* Look in the type of the source to see if it contains the
281          type of the target as a superclass.  If so, we'll need to
282          offset the object in addition to changing its type.  */
283       struct value *v = search_struct_field (type_name_no_tag (type),
284                                          arg2, 0, type2, 1);
285       if (v)
286         {
287           VALUE_TYPE (v) = type;
288           return v;
289         }
290     }
291   if (code1 == TYPE_CODE_FLT && scalar)
292     return value_from_double (type, value_as_double (arg2));
293   else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM
294             || code1 == TYPE_CODE_RANGE)
295            && (scalar || code2 == TYPE_CODE_PTR))
296     {
297       LONGEST longest;
298
299       if (deprecated_hp_som_som_object_present  /* if target compiled by HP aCC */
300           && (code2 == TYPE_CODE_PTR))
301         {
302           unsigned int *ptr;
303           struct value *retvalp;
304
305           switch (TYPE_CODE (TYPE_TARGET_TYPE (type2)))
306             {
307               /* With HP aCC, pointers to data members have a bias */
308             case TYPE_CODE_MEMBER:
309               retvalp = value_from_longest (type, value_as_long (arg2));
310               /* force evaluation */
311               ptr = (unsigned int *) VALUE_CONTENTS (retvalp);
312               *ptr &= ~0x20000000;      /* zap 29th bit to remove bias */
313               return retvalp;
314
315               /* While pointers to methods don't really point to a function */
316             case TYPE_CODE_METHOD:
317               error ("Pointers to methods not supported with HP aCC");
318
319             default:
320               break;            /* fall out and go to normal handling */
321             }
322         }
323
324       /* When we cast pointers to integers, we mustn't use
325          POINTER_TO_ADDRESS to find the address the pointer
326          represents, as value_as_long would.  GDB should evaluate
327          expressions just as the compiler would --- and the compiler
328          sees a cast as a simple reinterpretation of the pointer's
329          bits.  */
330       if (code2 == TYPE_CODE_PTR)
331         longest = extract_unsigned_integer (VALUE_CONTENTS (arg2),
332                                             TYPE_LENGTH (type2));
333       else
334         longest = value_as_long (arg2);
335       return value_from_longest (type, convert_to_boolean ?
336                                  (LONGEST) (longest ? 1 : 0) : longest);
337     }
338   else if (code1 == TYPE_CODE_PTR && (code2 == TYPE_CODE_INT  ||
339                                       code2 == TYPE_CODE_ENUM ||
340                                       code2 == TYPE_CODE_RANGE))
341     {
342       /* TYPE_LENGTH (type) is the length of a pointer, but we really
343          want the length of an address! -- we are really dealing with
344          addresses (i.e., gdb representations) not pointers (i.e.,
345          target representations) here.
346
347          This allows things like "print *(int *)0x01000234" to work
348          without printing a misleading message -- which would
349          otherwise occur when dealing with a target having two byte
350          pointers and four byte addresses.  */
351
352       int addr_bit = TARGET_ADDR_BIT;
353
354       LONGEST longest = value_as_long (arg2);
355       if (addr_bit < sizeof (LONGEST) * HOST_CHAR_BIT)
356         {
357           if (longest >= ((LONGEST) 1 << addr_bit)
358               || longest <= -((LONGEST) 1 << addr_bit))
359             warning ("value truncated");
360         }
361       return value_from_longest (type, longest);
362     }
363   else if (TYPE_LENGTH (type) == TYPE_LENGTH (type2))
364     {
365       if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
366         {
367           struct type *t1 = check_typedef (TYPE_TARGET_TYPE (type));
368           struct type *t2 = check_typedef (TYPE_TARGET_TYPE (type2));
369           if (TYPE_CODE (t1) == TYPE_CODE_STRUCT
370               && TYPE_CODE (t2) == TYPE_CODE_STRUCT
371               && !value_logical_not (arg2))
372             {
373               struct value *v;
374
375               /* Look in the type of the source to see if it contains the
376                  type of the target as a superclass.  If so, we'll need to
377                  offset the pointer rather than just change its type.  */
378               if (TYPE_NAME (t1) != NULL)
379                 {
380                   v = search_struct_field (type_name_no_tag (t1),
381                                            value_ind (arg2), 0, t2, 1);
382                   if (v)
383                     {
384                       v = value_addr (v);
385                       VALUE_TYPE (v) = type;
386                       return v;
387                     }
388                 }
389
390               /* Look in the type of the target to see if it contains the
391                  type of the source as a superclass.  If so, we'll need to
392                  offset the pointer rather than just change its type.
393                  FIXME: This fails silently with virtual inheritance.  */
394               if (TYPE_NAME (t2) != NULL)
395                 {
396                   v = search_struct_field (type_name_no_tag (t2),
397                                        value_zero (t1, not_lval), 0, t1, 1);
398                   if (v)
399                     {
400                       CORE_ADDR addr2 = value_as_address (arg2);
401                       addr2 -= (VALUE_ADDRESS (v)
402                                 + VALUE_OFFSET (v)
403                                 + VALUE_EMBEDDED_OFFSET (v));
404                       return value_from_pointer (type, addr2);
405                     }
406                 }
407             }
408           /* No superclass found, just fall through to change ptr type.  */
409         }
410       VALUE_TYPE (arg2) = type;
411       arg2 = value_change_enclosing_type (arg2, type);
412       VALUE_POINTED_TO_OFFSET (arg2) = 0;       /* pai: chk_val */
413       return arg2;
414     }
415   else if (VALUE_LVAL (arg2) == lval_memory)
416     {
417       return value_at_lazy (type, VALUE_ADDRESS (arg2) + VALUE_OFFSET (arg2),
418                             VALUE_BFD_SECTION (arg2));
419     }
420   else if (code1 == TYPE_CODE_VOID)
421     {
422       return value_zero (builtin_type_void, not_lval);
423     }
424   else
425     {
426       error ("Invalid cast.");
427       return 0;
428     }
429 }
430
431 /* Create a value of type TYPE that is zero, and return it.  */
432
433 struct value *
434 value_zero (struct type *type, enum lval_type lv)
435 {
436   struct value *val = allocate_value (type);
437
438   memset (VALUE_CONTENTS (val), 0, TYPE_LENGTH (check_typedef (type)));
439   VALUE_LVAL (val) = lv;
440
441   return val;
442 }
443
444 /* Return a value with type TYPE located at ADDR.
445
446    Call value_at only if the data needs to be fetched immediately;
447    if we can be 'lazy' and defer the fetch, perhaps indefinately, call
448    value_at_lazy instead.  value_at_lazy simply records the address of
449    the data and sets the lazy-evaluation-required flag.  The lazy flag
450    is tested in the VALUE_CONTENTS macro, which is used if and when
451    the contents are actually required.
452
453    Note: value_at does *NOT* handle embedded offsets; perform such
454    adjustments before or after calling it. */
455
456 struct value *
457 value_at (struct type *type, CORE_ADDR addr, asection *sect)
458 {
459   struct value *val;
460
461   if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
462     error ("Attempt to dereference a generic pointer.");
463
464   val = allocate_value (type);
465
466   read_memory (addr, VALUE_CONTENTS_ALL_RAW (val), TYPE_LENGTH (type));
467
468   VALUE_LVAL (val) = lval_memory;
469   VALUE_ADDRESS (val) = addr;
470   VALUE_BFD_SECTION (val) = sect;
471
472   return val;
473 }
474
475 /* Return a lazy value with type TYPE located at ADDR (cf. value_at).  */
476
477 struct value *
478 value_at_lazy (struct type *type, CORE_ADDR addr, asection *sect)
479 {
480   struct value *val;
481
482   if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
483     error ("Attempt to dereference a generic pointer.");
484
485   val = allocate_value (type);
486
487   VALUE_LVAL (val) = lval_memory;
488   VALUE_ADDRESS (val) = addr;
489   VALUE_LAZY (val) = 1;
490   VALUE_BFD_SECTION (val) = sect;
491
492   return val;
493 }
494
495 /* Called only from the VALUE_CONTENTS and VALUE_CONTENTS_ALL macros,
496    if the current data for a variable needs to be loaded into
497    VALUE_CONTENTS(VAL).  Fetches the data from the user's process, and
498    clears the lazy flag to indicate that the data in the buffer is valid.
499
500    If the value is zero-length, we avoid calling read_memory, which would
501    abort.  We mark the value as fetched anyway -- all 0 bytes of it.
502
503    This function returns a value because it is used in the VALUE_CONTENTS
504    macro as part of an expression, where a void would not work.  The
505    value is ignored.  */
506
507 int
508 value_fetch_lazy (struct value *val)
509 {
510   CORE_ADDR addr = VALUE_ADDRESS (val) + VALUE_OFFSET (val);
511   int length = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (val));
512
513   struct type *type = VALUE_TYPE (val);
514   if (length)
515     read_memory (addr, VALUE_CONTENTS_ALL_RAW (val), length);
516
517   VALUE_LAZY (val) = 0;
518   return 0;
519 }
520
521
522 /* Store the contents of FROMVAL into the location of TOVAL.
523    Return a new value with the location of TOVAL and contents of FROMVAL.  */
524
525 struct value *
526 value_assign (struct value *toval, struct value *fromval)
527 {
528   struct type *type;
529   struct value *val;
530   char raw_buffer[MAX_REGISTER_SIZE];
531   int use_buffer = 0;
532   struct frame_id old_frame;
533
534   if (!toval->modifiable)
535     error ("Left operand of assignment is not a modifiable lvalue.");
536
537   COERCE_REF (toval);
538
539   type = VALUE_TYPE (toval);
540   if (VALUE_LVAL (toval) != lval_internalvar)
541     fromval = value_cast (type, fromval);
542   else
543     COERCE_ARRAY (fromval);
544   CHECK_TYPEDEF (type);
545
546   /* Since modifying a register can trash the frame chain, and modifying memory
547      can trash the frame cache, we save the old frame and then restore the new
548      frame afterwards.  */
549   old_frame = get_frame_id (deprecated_selected_frame);
550
551   switch (VALUE_LVAL (toval))
552     {
553     case lval_internalvar:
554       set_internalvar (VALUE_INTERNALVAR (toval), fromval);
555       val = value_copy (VALUE_INTERNALVAR (toval)->value);
556       val = value_change_enclosing_type (val, VALUE_ENCLOSING_TYPE (fromval));
557       VALUE_EMBEDDED_OFFSET (val) = VALUE_EMBEDDED_OFFSET (fromval);
558       VALUE_POINTED_TO_OFFSET (val) = VALUE_POINTED_TO_OFFSET (fromval);
559       return val;
560
561     case lval_internalvar_component:
562       set_internalvar_component (VALUE_INTERNALVAR (toval),
563                                  VALUE_OFFSET (toval),
564                                  VALUE_BITPOS (toval),
565                                  VALUE_BITSIZE (toval),
566                                  fromval);
567       break;
568
569     case lval_memory:
570       {
571         char *dest_buffer;
572         CORE_ADDR changed_addr;
573         int changed_len;
574
575         if (VALUE_BITSIZE (toval))
576           {
577             char buffer[sizeof (LONGEST)];
578             /* We assume that the argument to read_memory is in units of
579                host chars.  FIXME:  Is that correct?  */
580             changed_len = (VALUE_BITPOS (toval)
581                            + VALUE_BITSIZE (toval)
582                            + HOST_CHAR_BIT - 1)
583               / HOST_CHAR_BIT;
584
585             if (changed_len > (int) sizeof (LONGEST))
586               error ("Can't handle bitfields which don't fit in a %d bit word.",
587                      (int) sizeof (LONGEST) * HOST_CHAR_BIT);
588
589             read_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
590                          buffer, changed_len);
591             modify_field (buffer, value_as_long (fromval),
592                           VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
593             changed_addr = VALUE_ADDRESS (toval) + VALUE_OFFSET (toval);
594             dest_buffer = buffer;
595           }
596         else if (use_buffer)
597           {
598             changed_addr = VALUE_ADDRESS (toval) + VALUE_OFFSET (toval);
599             changed_len = use_buffer;
600             dest_buffer = raw_buffer;
601           }
602         else
603           {
604             changed_addr = VALUE_ADDRESS (toval) + VALUE_OFFSET (toval);
605             changed_len = TYPE_LENGTH (type);
606             dest_buffer = VALUE_CONTENTS (fromval);
607           }
608
609         write_memory (changed_addr, dest_buffer, changed_len);
610         if (deprecated_memory_changed_hook)
611           deprecated_memory_changed_hook (changed_addr, changed_len);
612         target_changed_event ();
613       }
614       break;
615
616     case lval_reg_frame_relative:
617     case lval_register:
618       {
619         struct frame_info *frame;
620         int value_reg;
621
622         /* Figure out which frame this is in currently.  */
623         if (VALUE_LVAL (toval) == lval_register)
624           {
625             frame = get_current_frame ();
626             value_reg = VALUE_REGNO (toval);
627           }
628         else
629           {
630             frame = frame_find_by_id (VALUE_FRAME_ID (toval));
631             value_reg = VALUE_FRAME_REGNUM (toval);
632           }
633
634         if (!frame)
635           error ("Value being assigned to is no longer active.");
636         
637         if (VALUE_LVAL (toval) == lval_reg_frame_relative
638             && CONVERT_REGISTER_P (VALUE_FRAME_REGNUM (toval), type))
639           {
640             /* If TOVAL is a special machine register requiring
641                conversion of program values to a special raw format.  */
642             VALUE_TO_REGISTER (frame, VALUE_FRAME_REGNUM (toval),
643                                type, VALUE_CONTENTS (fromval));
644           }
645         else
646           {
647             /* TOVAL is stored in a series of registers in the frame
648                specified by the structure.  Copy that value out,
649                modify it, and copy it back in.  */
650             int amount_copied;
651             int amount_to_copy;
652             char *buffer;
653             int reg_offset;
654             int byte_offset;
655             int regno;
656
657             /* Locate the first register that falls in the value that
658                needs to be transfered.  Compute the offset of the
659                value in that register.  */
660             {
661               int offset;
662               for (reg_offset = value_reg, offset = 0;
663                    offset + DEPRECATED_REGISTER_RAW_SIZE (reg_offset) <= VALUE_OFFSET (toval);
664                    reg_offset++);
665               byte_offset = VALUE_OFFSET (toval) - offset;
666             }
667
668             /* Compute the number of register aligned values that need
669                to be copied.  */
670             if (VALUE_BITSIZE (toval))
671               amount_to_copy = byte_offset + 1;
672             else
673               amount_to_copy = byte_offset + TYPE_LENGTH (type);
674             
675             /* And a bounce buffer.  Be slightly over generous.  */
676             buffer = (char *) alloca (amount_to_copy + MAX_REGISTER_SIZE);
677
678             /* Copy it in.  */
679             for (regno = reg_offset, amount_copied = 0;
680                  amount_copied < amount_to_copy;
681                  amount_copied += DEPRECATED_REGISTER_RAW_SIZE (regno), regno++)
682               frame_register_read (frame, regno, buffer + amount_copied);
683             
684             /* Modify what needs to be modified.  */
685             if (VALUE_BITSIZE (toval))
686               modify_field (buffer + byte_offset,
687                             value_as_long (fromval),
688                             VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
689             else if (use_buffer)
690               memcpy (buffer + VALUE_OFFSET (toval), raw_buffer, use_buffer);
691             else
692               memcpy (buffer + byte_offset, VALUE_CONTENTS (fromval),
693                       TYPE_LENGTH (type));
694
695             /* Copy it out.  */
696             for (regno = reg_offset, amount_copied = 0;
697                  amount_copied < amount_to_copy;
698                  amount_copied += DEPRECATED_REGISTER_RAW_SIZE (regno), regno++)
699               put_frame_register (frame, regno, buffer + amount_copied);
700
701           }
702         if (deprecated_register_changed_hook)
703           deprecated_register_changed_hook (-1);
704         target_changed_event ();
705         observer_notify_target_changed (&current_target);
706         break;
707       }
708       
709     default:
710       error ("Left operand of assignment is not an lvalue.");
711     }
712
713   /* Assigning to the stack pointer, frame pointer, and other
714      (architecture and calling convention specific) registers may
715      cause the frame cache to be out of date.  Assigning to memory
716      also can.  We just do this on all assignments to registers or
717      memory, for simplicity's sake; I doubt the slowdown matters.  */
718   switch (VALUE_LVAL (toval))
719     {
720     case lval_memory:
721     case lval_register:
722     case lval_reg_frame_relative:
723
724       reinit_frame_cache ();
725
726       /* Having destoroyed the frame cache, restore the selected frame.  */
727
728       /* FIXME: cagney/2002-11-02: There has to be a better way of
729          doing this.  Instead of constantly saving/restoring the
730          frame.  Why not create a get_selected_frame() function that,
731          having saved the selected frame's ID can automatically
732          re-find the previously selected frame automatically.  */
733
734       {
735         struct frame_info *fi = frame_find_by_id (old_frame);
736         if (fi != NULL)
737           select_frame (fi);
738       }
739
740       break;
741     default:
742       break;
743     }
744   
745   /* If the field does not entirely fill a LONGEST, then zero the sign bits.
746      If the field is signed, and is negative, then sign extend. */
747   if ((VALUE_BITSIZE (toval) > 0)
748       && (VALUE_BITSIZE (toval) < 8 * (int) sizeof (LONGEST)))
749     {
750       LONGEST fieldval = value_as_long (fromval);
751       LONGEST valmask = (((ULONGEST) 1) << VALUE_BITSIZE (toval)) - 1;
752
753       fieldval &= valmask;
754       if (!TYPE_UNSIGNED (type) && (fieldval & (valmask ^ (valmask >> 1))))
755         fieldval |= ~valmask;
756
757       fromval = value_from_longest (type, fieldval);
758     }
759
760   val = value_copy (toval);
761   memcpy (VALUE_CONTENTS_RAW (val), VALUE_CONTENTS (fromval),
762           TYPE_LENGTH (type));
763   VALUE_TYPE (val) = type;
764   val = value_change_enclosing_type (val, VALUE_ENCLOSING_TYPE (fromval));
765   VALUE_EMBEDDED_OFFSET (val) = VALUE_EMBEDDED_OFFSET (fromval);
766   VALUE_POINTED_TO_OFFSET (val) = VALUE_POINTED_TO_OFFSET (fromval);
767
768   return val;
769 }
770
771 /* Extend a value VAL to COUNT repetitions of its type.  */
772
773 struct value *
774 value_repeat (struct value *arg1, int count)
775 {
776   struct value *val;
777
778   if (VALUE_LVAL (arg1) != lval_memory)
779     error ("Only values in memory can be extended with '@'.");
780   if (count < 1)
781     error ("Invalid number %d of repetitions.", count);
782
783   val = allocate_repeat_value (VALUE_ENCLOSING_TYPE (arg1), count);
784
785   read_memory (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1),
786                VALUE_CONTENTS_ALL_RAW (val),
787                TYPE_LENGTH (VALUE_ENCLOSING_TYPE (val)));
788   VALUE_LVAL (val) = lval_memory;
789   VALUE_ADDRESS (val) = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1);
790
791   return val;
792 }
793
794 struct value *
795 value_of_variable (struct symbol *var, struct block *b)
796 {
797   struct value *val;
798   struct frame_info *frame = NULL;
799
800   if (!b)
801     frame = NULL;               /* Use selected frame.  */
802   else if (symbol_read_needs_frame (var))
803     {
804       frame = block_innermost_frame (b);
805       if (!frame)
806         {
807           if (BLOCK_FUNCTION (b)
808               && SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)))
809             error ("No frame is currently executing in block %s.",
810                    SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)));
811           else
812             error ("No frame is currently executing in specified block");
813         }
814     }
815
816   val = read_var_value (var, frame);
817   if (!val)
818     error ("Address of symbol \"%s\" is unknown.", SYMBOL_PRINT_NAME (var));
819
820   return val;
821 }
822
823 /* Given a value which is an array, return a value which is a pointer to its
824    first element, regardless of whether or not the array has a nonzero lower
825    bound.
826
827    FIXME:  A previous comment here indicated that this routine should be
828    substracting the array's lower bound.  It's not clear to me that this
829    is correct.  Given an array subscripting operation, it would certainly
830    work to do the adjustment here, essentially computing:
831
832    (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0])
833
834    However I believe a more appropriate and logical place to account for
835    the lower bound is to do so in value_subscript, essentially computing:
836
837    (&array[0] + ((index - lowerbound) * sizeof array[0]))
838
839    As further evidence consider what would happen with operations other
840    than array subscripting, where the caller would get back a value that
841    had an address somewhere before the actual first element of the array,
842    and the information about the lower bound would be lost because of
843    the coercion to pointer type.
844  */
845
846 struct value *
847 value_coerce_array (struct value *arg1)
848 {
849   struct type *type = check_typedef (VALUE_TYPE (arg1));
850
851   if (VALUE_LVAL (arg1) != lval_memory)
852     error ("Attempt to take address of value not located in memory.");
853
854   return value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
855                              (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
856 }
857
858 /* Given a value which is a function, return a value which is a pointer
859    to it.  */
860
861 struct value *
862 value_coerce_function (struct value *arg1)
863 {
864   struct value *retval;
865
866   if (VALUE_LVAL (arg1) != lval_memory)
867     error ("Attempt to take address of value not located in memory.");
868
869   retval = value_from_pointer (lookup_pointer_type (VALUE_TYPE (arg1)),
870                                (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
871   VALUE_BFD_SECTION (retval) = VALUE_BFD_SECTION (arg1);
872   return retval;
873 }
874
875 /* Return a pointer value for the object for which ARG1 is the contents.  */
876
877 struct value *
878 value_addr (struct value *arg1)
879 {
880   struct value *arg2;
881
882   struct type *type = check_typedef (VALUE_TYPE (arg1));
883   if (TYPE_CODE (type) == TYPE_CODE_REF)
884     {
885       /* Copy the value, but change the type from (T&) to (T*).
886          We keep the same location information, which is efficient,
887          and allows &(&X) to get the location containing the reference. */
888       arg2 = value_copy (arg1);
889       VALUE_TYPE (arg2) = lookup_pointer_type (TYPE_TARGET_TYPE (type));
890       return arg2;
891     }
892   if (TYPE_CODE (type) == TYPE_CODE_FUNC)
893     return value_coerce_function (arg1);
894
895   if (VALUE_LVAL (arg1) != lval_memory)
896     error ("Attempt to take address of value not located in memory.");
897
898   /* Get target memory address */
899   arg2 = value_from_pointer (lookup_pointer_type (VALUE_TYPE (arg1)),
900                              (VALUE_ADDRESS (arg1)
901                               + VALUE_OFFSET (arg1)
902                               + VALUE_EMBEDDED_OFFSET (arg1)));
903
904   /* This may be a pointer to a base subobject; so remember the
905      full derived object's type ... */
906   arg2 = value_change_enclosing_type (arg2, lookup_pointer_type (VALUE_ENCLOSING_TYPE (arg1)));
907   /* ... and also the relative position of the subobject in the full object */
908   VALUE_POINTED_TO_OFFSET (arg2) = VALUE_EMBEDDED_OFFSET (arg1);
909   VALUE_BFD_SECTION (arg2) = VALUE_BFD_SECTION (arg1);
910   return arg2;
911 }
912
913 /* Given a value of a pointer type, apply the C unary * operator to it.  */
914
915 struct value *
916 value_ind (struct value *arg1)
917 {
918   struct type *base_type;
919   struct value *arg2;
920
921   COERCE_ARRAY (arg1);
922
923   base_type = check_typedef (VALUE_TYPE (arg1));
924
925   if (TYPE_CODE (base_type) == TYPE_CODE_MEMBER)
926     error ("not implemented: member types in value_ind");
927
928   /* Allow * on an integer so we can cast it to whatever we want.
929      This returns an int, which seems like the most C-like thing
930      to do.  "long long" variables are rare enough that
931      BUILTIN_TYPE_LONGEST would seem to be a mistake.  */
932   if (TYPE_CODE (base_type) == TYPE_CODE_INT)
933     return value_at_lazy (builtin_type_int,
934                           (CORE_ADDR) value_as_long (arg1),
935                           VALUE_BFD_SECTION (arg1));
936   else if (TYPE_CODE (base_type) == TYPE_CODE_PTR)
937     {
938       struct type *enc_type;
939       /* We may be pointing to something embedded in a larger object */
940       /* Get the real type of the enclosing object */
941       enc_type = check_typedef (VALUE_ENCLOSING_TYPE (arg1));
942       enc_type = TYPE_TARGET_TYPE (enc_type);
943       /* Retrieve the enclosing object pointed to */
944       arg2 = value_at_lazy (enc_type,
945                    value_as_address (arg1) - VALUE_POINTED_TO_OFFSET (arg1),
946                             VALUE_BFD_SECTION (arg1));
947       /* Re-adjust type */
948       VALUE_TYPE (arg2) = TYPE_TARGET_TYPE (base_type);
949       /* Add embedding info */
950       arg2 = value_change_enclosing_type (arg2, enc_type);
951       VALUE_EMBEDDED_OFFSET (arg2) = VALUE_POINTED_TO_OFFSET (arg1);
952
953       /* We may be pointing to an object of some derived type */
954       arg2 = value_full_object (arg2, NULL, 0, 0, 0);
955       return arg2;
956     }
957
958   error ("Attempt to take contents of a non-pointer value.");
959   return 0;                     /* For lint -- never reached */
960 }
961 \f
962 /* Pushing small parts of stack frames.  */
963
964 /* Push one word (the size of object that a register holds).  */
965
966 CORE_ADDR
967 push_word (CORE_ADDR sp, ULONGEST word)
968 {
969   int len = DEPRECATED_REGISTER_SIZE;
970   char buffer[MAX_REGISTER_SIZE];
971
972   store_unsigned_integer (buffer, len, word);
973   if (INNER_THAN (1, 2))
974     {
975       /* stack grows downward */
976       sp -= len;
977       write_memory (sp, buffer, len);
978     }
979   else
980     {
981       /* stack grows upward */
982       write_memory (sp, buffer, len);
983       sp += len;
984     }
985
986   return sp;
987 }
988
989 /* Push LEN bytes with data at BUFFER.  */
990
991 CORE_ADDR
992 push_bytes (CORE_ADDR sp, char *buffer, int len)
993 {
994   if (INNER_THAN (1, 2))
995     {
996       /* stack grows downward */
997       sp -= len;
998       write_memory (sp, buffer, len);
999     }
1000   else
1001     {
1002       /* stack grows upward */
1003       write_memory (sp, buffer, len);
1004       sp += len;
1005     }
1006
1007   return sp;
1008 }
1009
1010 #ifndef PARM_BOUNDARY
1011 #define PARM_BOUNDARY (0)
1012 #endif
1013
1014 /* Push onto the stack the specified value VALUE.  Pad it correctly for
1015    it to be an argument to a function.  */
1016
1017 static CORE_ADDR
1018 value_push (CORE_ADDR sp, struct value *arg)
1019 {
1020   int len = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (arg));
1021   int container_len = len;
1022   int offset;
1023
1024   /* How big is the container we're going to put this value in?  */
1025   if (PARM_BOUNDARY)
1026     container_len = ((len + PARM_BOUNDARY / TARGET_CHAR_BIT - 1)
1027                      & ~(PARM_BOUNDARY / TARGET_CHAR_BIT - 1));
1028
1029   /* Are we going to put it at the high or low end of the container?  */
1030   if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
1031     offset = container_len - len;
1032   else
1033     offset = 0;
1034
1035   if (INNER_THAN (1, 2))
1036     {
1037       /* stack grows downward */
1038       sp -= container_len;
1039       write_memory (sp + offset, VALUE_CONTENTS_ALL (arg), len);
1040     }
1041   else
1042     {
1043       /* stack grows upward */
1044       write_memory (sp + offset, VALUE_CONTENTS_ALL (arg), len);
1045       sp += container_len;
1046     }
1047
1048   return sp;
1049 }
1050
1051 CORE_ADDR
1052 legacy_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
1053                        int struct_return, CORE_ADDR struct_addr)
1054 {
1055   /* ASSERT ( !struct_return); */
1056   int i;
1057   for (i = nargs - 1; i >= 0; i--)
1058     sp = value_push (sp, args[i]);
1059   return sp;
1060 }
1061
1062 /* Create a value for an array by allocating space in the inferior, copying
1063    the data into that space, and then setting up an array value.
1064
1065    The array bounds are set from LOWBOUND and HIGHBOUND, and the array is
1066    populated from the values passed in ELEMVEC.
1067
1068    The element type of the array is inherited from the type of the
1069    first element, and all elements must have the same size (though we
1070    don't currently enforce any restriction on their types). */
1071
1072 struct value *
1073 value_array (int lowbound, int highbound, struct value **elemvec)
1074 {
1075   int nelem;
1076   int idx;
1077   unsigned int typelength;
1078   struct value *val;
1079   struct type *rangetype;
1080   struct type *arraytype;
1081   CORE_ADDR addr;
1082
1083   /* Validate that the bounds are reasonable and that each of the elements
1084      have the same size. */
1085
1086   nelem = highbound - lowbound + 1;
1087   if (nelem <= 0)
1088     {
1089       error ("bad array bounds (%d, %d)", lowbound, highbound);
1090     }
1091   typelength = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (elemvec[0]));
1092   for (idx = 1; idx < nelem; idx++)
1093     {
1094       if (TYPE_LENGTH (VALUE_ENCLOSING_TYPE (elemvec[idx])) != typelength)
1095         {
1096           error ("array elements must all be the same size");
1097         }
1098     }
1099
1100   rangetype = create_range_type ((struct type *) NULL, builtin_type_int,
1101                                  lowbound, highbound);
1102   arraytype = create_array_type ((struct type *) NULL,
1103                               VALUE_ENCLOSING_TYPE (elemvec[0]), rangetype);
1104
1105   if (!current_language->c_style_arrays)
1106     {
1107       val = allocate_value (arraytype);
1108       for (idx = 0; idx < nelem; idx++)
1109         {
1110           memcpy (VALUE_CONTENTS_ALL_RAW (val) + (idx * typelength),
1111                   VALUE_CONTENTS_ALL (elemvec[idx]),
1112                   typelength);
1113         }
1114       VALUE_BFD_SECTION (val) = VALUE_BFD_SECTION (elemvec[0]);
1115       return val;
1116     }
1117
1118   /* Allocate space to store the array in the inferior, and then initialize
1119      it by copying in each element.  FIXME:  Is it worth it to create a
1120      local buffer in which to collect each value and then write all the
1121      bytes in one operation? */
1122
1123   addr = allocate_space_in_inferior (nelem * typelength);
1124   for (idx = 0; idx < nelem; idx++)
1125     {
1126       write_memory (addr + (idx * typelength), VALUE_CONTENTS_ALL (elemvec[idx]),
1127                     typelength);
1128     }
1129
1130   /* Create the array type and set up an array value to be evaluated lazily. */
1131
1132   val = value_at_lazy (arraytype, addr, VALUE_BFD_SECTION (elemvec[0]));
1133   return (val);
1134 }
1135
1136 /* Create a value for a string constant by allocating space in the inferior,
1137    copying the data into that space, and returning the address with type
1138    TYPE_CODE_STRING.  PTR points to the string constant data; LEN is number
1139    of characters.
1140    Note that string types are like array of char types with a lower bound of
1141    zero and an upper bound of LEN - 1.  Also note that the string may contain
1142    embedded null bytes. */
1143
1144 struct value *
1145 value_string (char *ptr, int len)
1146 {
1147   struct value *val;
1148   int lowbound = current_language->string_lower_bound;
1149   struct type *rangetype = create_range_type ((struct type *) NULL,
1150                                               builtin_type_int,
1151                                               lowbound, len + lowbound - 1);
1152   struct type *stringtype
1153   = create_string_type ((struct type *) NULL, rangetype);
1154   CORE_ADDR addr;
1155
1156   if (current_language->c_style_arrays == 0)
1157     {
1158       val = allocate_value (stringtype);
1159       memcpy (VALUE_CONTENTS_RAW (val), ptr, len);
1160       return val;
1161     }
1162
1163
1164   /* Allocate space to store the string in the inferior, and then
1165      copy LEN bytes from PTR in gdb to that address in the inferior. */
1166
1167   addr = allocate_space_in_inferior (len);
1168   write_memory (addr, ptr, len);
1169
1170   val = value_at_lazy (stringtype, addr, NULL);
1171   return (val);
1172 }
1173
1174 struct value *
1175 value_bitstring (char *ptr, int len)
1176 {
1177   struct value *val;
1178   struct type *domain_type = create_range_type (NULL, builtin_type_int,
1179                                                 0, len - 1);
1180   struct type *type = create_set_type ((struct type *) NULL, domain_type);
1181   TYPE_CODE (type) = TYPE_CODE_BITSTRING;
1182   val = allocate_value (type);
1183   memcpy (VALUE_CONTENTS_RAW (val), ptr, TYPE_LENGTH (type));
1184   return val;
1185 }
1186 \f
1187 /* See if we can pass arguments in T2 to a function which takes arguments
1188    of types T1.  T1 is a list of NARGS arguments, and T2 is a NULL-terminated
1189    vector.  If some arguments need coercion of some sort, then the coerced
1190    values are written into T2.  Return value is 0 if the arguments could be
1191    matched, or the position at which they differ if not.
1192
1193    STATICP is nonzero if the T1 argument list came from a
1194    static member function.  T2 will still include the ``this'' pointer,
1195    but it will be skipped.
1196
1197    For non-static member functions, we ignore the first argument,
1198    which is the type of the instance variable.  This is because we want
1199    to handle calls with objects from derived classes.  This is not
1200    entirely correct: we should actually check to make sure that a
1201    requested operation is type secure, shouldn't we?  FIXME.  */
1202
1203 static int
1204 typecmp (int staticp, int varargs, int nargs,
1205          struct field t1[], struct value *t2[])
1206 {
1207   int i;
1208
1209   if (t2 == 0)
1210     internal_error (__FILE__, __LINE__, "typecmp: no argument list");
1211
1212   /* Skip ``this'' argument if applicable.  T2 will always include THIS.  */
1213   if (staticp)
1214     t2 ++;
1215
1216   for (i = 0;
1217        (i < nargs) && TYPE_CODE (t1[i].type) != TYPE_CODE_VOID;
1218        i++)
1219     {
1220       struct type *tt1, *tt2;
1221
1222       if (!t2[i])
1223         return i + 1;
1224
1225       tt1 = check_typedef (t1[i].type);
1226       tt2 = check_typedef (VALUE_TYPE (t2[i]));
1227
1228       if (TYPE_CODE (tt1) == TYPE_CODE_REF
1229       /* We should be doing hairy argument matching, as below.  */
1230           && (TYPE_CODE (check_typedef (TYPE_TARGET_TYPE (tt1))) == TYPE_CODE (tt2)))
1231         {
1232           if (TYPE_CODE (tt2) == TYPE_CODE_ARRAY)
1233             t2[i] = value_coerce_array (t2[i]);
1234           else
1235             t2[i] = value_addr (t2[i]);
1236           continue;
1237         }
1238
1239       /* djb - 20000715 - Until the new type structure is in the
1240          place, and we can attempt things like implicit conversions,
1241          we need to do this so you can take something like a map<const
1242          char *>, and properly access map["hello"], because the
1243          argument to [] will be a reference to a pointer to a char,
1244          and the argument will be a pointer to a char. */
1245       while ( TYPE_CODE(tt1) == TYPE_CODE_REF ||
1246               TYPE_CODE (tt1) == TYPE_CODE_PTR)
1247         {
1248           tt1 = check_typedef( TYPE_TARGET_TYPE(tt1) );
1249         }
1250       while ( TYPE_CODE(tt2) == TYPE_CODE_ARRAY ||
1251               TYPE_CODE(tt2) == TYPE_CODE_PTR ||
1252               TYPE_CODE(tt2) == TYPE_CODE_REF)
1253         {
1254           tt2 = check_typedef( TYPE_TARGET_TYPE(tt2) );
1255         }
1256       if (TYPE_CODE (tt1) == TYPE_CODE (tt2))
1257         continue;
1258       /* Array to pointer is a `trivial conversion' according to the ARM.  */
1259
1260       /* We should be doing much hairier argument matching (see section 13.2
1261          of the ARM), but as a quick kludge, just check for the same type
1262          code.  */
1263       if (TYPE_CODE (t1[i].type) != TYPE_CODE (VALUE_TYPE (t2[i])))
1264         return i + 1;
1265     }
1266   if (varargs || t2[i] == NULL)
1267     return 0;
1268   return i + 1;
1269 }
1270
1271 /* Helper function used by value_struct_elt to recurse through baseclasses.
1272    Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1273    and search in it assuming it has (class) type TYPE.
1274    If found, return value, else return NULL.
1275
1276    If LOOKING_FOR_BASECLASS, then instead of looking for struct fields,
1277    look for a baseclass named NAME.  */
1278
1279 static struct value *
1280 search_struct_field (char *name, struct value *arg1, int offset,
1281                      struct type *type, int looking_for_baseclass)
1282 {
1283   int i;
1284   int nbases = TYPE_N_BASECLASSES (type);
1285
1286   CHECK_TYPEDEF (type);
1287
1288   if (!looking_for_baseclass)
1289     for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
1290       {
1291         char *t_field_name = TYPE_FIELD_NAME (type, i);
1292
1293         if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
1294           {
1295             struct value *v;
1296             if (TYPE_FIELD_STATIC (type, i))
1297               {
1298                 v = value_static_field (type, i);
1299                 if (v == 0)
1300                   error ("field %s is nonexistent or has been optimised out",
1301                          name);
1302               }
1303             else
1304               {
1305                 v = value_primitive_field (arg1, offset, i, type);
1306                 if (v == 0)
1307                   error ("there is no field named %s", name);
1308               }
1309             return v;
1310           }
1311
1312         if (t_field_name
1313             && (t_field_name[0] == '\0'
1314                 || (TYPE_CODE (type) == TYPE_CODE_UNION
1315                     && (strcmp_iw (t_field_name, "else") == 0))))
1316           {
1317             struct type *field_type = TYPE_FIELD_TYPE (type, i);
1318             if (TYPE_CODE (field_type) == TYPE_CODE_UNION
1319                 || TYPE_CODE (field_type) == TYPE_CODE_STRUCT)
1320               {
1321                 /* Look for a match through the fields of an anonymous union,
1322                    or anonymous struct.  C++ provides anonymous unions.
1323
1324                    In the GNU Chill (now deleted from GDB)
1325                    implementation of variant record types, each
1326                    <alternative field> has an (anonymous) union type,
1327                    each member of the union represents a <variant
1328                    alternative>.  Each <variant alternative> is
1329                    represented as a struct, with a member for each
1330                    <variant field>.  */
1331
1332                 struct value *v;
1333                 int new_offset = offset;
1334
1335                 /* This is pretty gross.  In G++, the offset in an
1336                    anonymous union is relative to the beginning of the
1337                    enclosing struct.  In the GNU Chill (now deleted
1338                    from GDB) implementation of variant records, the
1339                    bitpos is zero in an anonymous union field, so we
1340                    have to add the offset of the union here. */
1341                 if (TYPE_CODE (field_type) == TYPE_CODE_STRUCT
1342                     || (TYPE_NFIELDS (field_type) > 0
1343                         && TYPE_FIELD_BITPOS (field_type, 0) == 0))
1344                   new_offset += TYPE_FIELD_BITPOS (type, i) / 8;
1345
1346                 v = search_struct_field (name, arg1, new_offset, field_type,
1347                                          looking_for_baseclass);
1348                 if (v)
1349                   return v;
1350               }
1351           }
1352       }
1353
1354   for (i = 0; i < nbases; i++)
1355     {
1356       struct value *v;
1357       struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
1358       /* If we are looking for baseclasses, this is what we get when we
1359          hit them.  But it could happen that the base part's member name
1360          is not yet filled in.  */
1361       int found_baseclass = (looking_for_baseclass
1362                              && TYPE_BASECLASS_NAME (type, i) != NULL
1363                              && (strcmp_iw (name, TYPE_BASECLASS_NAME (type, i)) == 0));
1364
1365       if (BASETYPE_VIA_VIRTUAL (type, i))
1366         {
1367           int boffset;
1368           struct value *v2 = allocate_value (basetype);
1369
1370           boffset = baseclass_offset (type, i,
1371                                       VALUE_CONTENTS (arg1) + offset,
1372                                       VALUE_ADDRESS (arg1)
1373                                       + VALUE_OFFSET (arg1) + offset);
1374           if (boffset == -1)
1375             error ("virtual baseclass botch");
1376
1377           /* The virtual base class pointer might have been clobbered by the
1378              user program. Make sure that it still points to a valid memory
1379              location.  */
1380
1381           boffset += offset;
1382           if (boffset < 0 || boffset >= TYPE_LENGTH (type))
1383             {
1384               CORE_ADDR base_addr;
1385
1386               base_addr = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1) + boffset;
1387               if (target_read_memory (base_addr, VALUE_CONTENTS_RAW (v2),
1388                                       TYPE_LENGTH (basetype)) != 0)
1389                 error ("virtual baseclass botch");
1390               VALUE_LVAL (v2) = lval_memory;
1391               VALUE_ADDRESS (v2) = base_addr;
1392             }
1393           else
1394             {
1395               VALUE_LVAL (v2) = VALUE_LVAL (arg1);
1396               VALUE_ADDRESS (v2) = VALUE_ADDRESS (arg1);
1397               VALUE_OFFSET (v2) = VALUE_OFFSET (arg1) + boffset;
1398               if (VALUE_LAZY (arg1))
1399                 VALUE_LAZY (v2) = 1;
1400               else
1401                 memcpy (VALUE_CONTENTS_RAW (v2),
1402                         VALUE_CONTENTS_RAW (arg1) + boffset,
1403                         TYPE_LENGTH (basetype));
1404             }
1405
1406           if (found_baseclass)
1407             return v2;
1408           v = search_struct_field (name, v2, 0, TYPE_BASECLASS (type, i),
1409                                    looking_for_baseclass);
1410         }
1411       else if (found_baseclass)
1412         v = value_primitive_field (arg1, offset, i, type);
1413       else
1414         v = search_struct_field (name, arg1,
1415                                offset + TYPE_BASECLASS_BITPOS (type, i) / 8,
1416                                  basetype, looking_for_baseclass);
1417       if (v)
1418         return v;
1419     }
1420   return NULL;
1421 }
1422
1423
1424 /* Return the offset (in bytes) of the virtual base of type BASETYPE
1425  * in an object pointed to by VALADDR (on the host), assumed to be of
1426  * type TYPE.  OFFSET is number of bytes beyond start of ARG to start
1427  * looking (in case VALADDR is the contents of an enclosing object).
1428  *
1429  * This routine recurses on the primary base of the derived class because
1430  * the virtual base entries of the primary base appear before the other
1431  * virtual base entries.
1432  *
1433  * If the virtual base is not found, a negative integer is returned.
1434  * The magnitude of the negative integer is the number of entries in
1435  * the virtual table to skip over (entries corresponding to various
1436  * ancestral classes in the chain of primary bases).
1437  *
1438  * Important: This assumes the HP / Taligent C++ runtime
1439  * conventions. Use baseclass_offset() instead to deal with g++
1440  * conventions.  */
1441
1442 void
1443 find_rt_vbase_offset (struct type *type, struct type *basetype, char *valaddr,
1444                       int offset, int *boffset_p, int *skip_p)
1445 {
1446   int boffset;                  /* offset of virtual base */
1447   int index;                    /* displacement to use in virtual table */
1448   int skip;
1449
1450   struct value *vp;
1451   CORE_ADDR vtbl;               /* the virtual table pointer */
1452   struct type *pbc;             /* the primary base class */
1453
1454   /* Look for the virtual base recursively in the primary base, first.
1455    * This is because the derived class object and its primary base
1456    * subobject share the primary virtual table.  */
1457
1458   boffset = 0;
1459   pbc = TYPE_PRIMARY_BASE (type);
1460   if (pbc)
1461     {
1462       find_rt_vbase_offset (pbc, basetype, valaddr, offset, &boffset, &skip);
1463       if (skip < 0)
1464         {
1465           *boffset_p = boffset;
1466           *skip_p = -1;
1467           return;
1468         }
1469     }
1470   else
1471     skip = 0;
1472
1473
1474   /* Find the index of the virtual base according to HP/Taligent
1475      runtime spec. (Depth-first, left-to-right.)  */
1476   index = virtual_base_index_skip_primaries (basetype, type);
1477
1478   if (index < 0)
1479     {
1480       *skip_p = skip + virtual_base_list_length_skip_primaries (type);
1481       *boffset_p = 0;
1482       return;
1483     }
1484
1485   /* pai: FIXME -- 32x64 possible problem */
1486   /* First word (4 bytes) in object layout is the vtable pointer */
1487   vtbl = *(CORE_ADDR *) (valaddr + offset);
1488
1489   /* Before the constructor is invoked, things are usually zero'd out. */
1490   if (vtbl == 0)
1491     error ("Couldn't find virtual table -- object may not be constructed yet.");
1492
1493
1494   /* Find virtual base's offset -- jump over entries for primary base
1495    * ancestors, then use the index computed above.  But also adjust by
1496    * HP_ACC_VBASE_START for the vtable slots before the start of the
1497    * virtual base entries.  Offset is negative -- virtual base entries
1498    * appear _before_ the address point of the virtual table. */
1499
1500   /* pai: FIXME -- 32x64 problem, if word = 8 bytes, change multiplier
1501      & use long type */
1502
1503   /* epstein : FIXME -- added param for overlay section. May not be correct */
1504   vp = value_at (builtin_type_int, vtbl + 4 * (-skip - index - HP_ACC_VBASE_START), NULL);
1505   boffset = value_as_long (vp);
1506   *skip_p = -1;
1507   *boffset_p = boffset;
1508   return;
1509 }
1510
1511
1512 /* Helper function used by value_struct_elt to recurse through baseclasses.
1513    Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1514    and search in it assuming it has (class) type TYPE.
1515    If found, return value, else if name matched and args not return (value)-1,
1516    else return NULL. */
1517
1518 static struct value *
1519 search_struct_method (char *name, struct value **arg1p,
1520                       struct value **args, int offset,
1521                       int *static_memfuncp, struct type *type)
1522 {
1523   int i;
1524   struct value *v;
1525   int name_matched = 0;
1526   char dem_opname[64];
1527
1528   CHECK_TYPEDEF (type);
1529   for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
1530     {
1531       char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
1532       /* FIXME!  May need to check for ARM demangling here */
1533       if (strncmp (t_field_name, "__", 2) == 0 ||
1534           strncmp (t_field_name, "op", 2) == 0 ||
1535           strncmp (t_field_name, "type", 4) == 0)
1536         {
1537           if (cplus_demangle_opname (t_field_name, dem_opname, DMGL_ANSI))
1538             t_field_name = dem_opname;
1539           else if (cplus_demangle_opname (t_field_name, dem_opname, 0))
1540             t_field_name = dem_opname;
1541         }
1542       if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
1543         {
1544           int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1;
1545           struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1546           name_matched = 1;
1547
1548           check_stub_method_group (type, i);
1549           if (j > 0 && args == 0)
1550             error ("cannot resolve overloaded method `%s': no arguments supplied", name);
1551           else if (j == 0 && args == 0)
1552             {
1553               v = value_fn_field (arg1p, f, j, type, offset);
1554               if (v != NULL)
1555                 return v;
1556             }
1557           else
1558             while (j >= 0)
1559               {
1560                 if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j),
1561                               TYPE_VARARGS (TYPE_FN_FIELD_TYPE (f, j)),
1562                               TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f, j)),
1563                               TYPE_FN_FIELD_ARGS (f, j), args))
1564                   {
1565                     if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1566                       return value_virtual_fn_field (arg1p, f, j, type, offset);
1567                     if (TYPE_FN_FIELD_STATIC_P (f, j) && static_memfuncp)
1568                       *static_memfuncp = 1;
1569                     v = value_fn_field (arg1p, f, j, type, offset);
1570                     if (v != NULL)
1571                       return v;       
1572                   }
1573                 j--;
1574               }
1575         }
1576     }
1577
1578   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1579     {
1580       int base_offset;
1581
1582       if (BASETYPE_VIA_VIRTUAL (type, i))
1583         {
1584           if (TYPE_HAS_VTABLE (type))
1585             {
1586               /* HP aCC compiled type, search for virtual base offset
1587                  according to HP/Taligent runtime spec.  */
1588               int skip;
1589               find_rt_vbase_offset (type, TYPE_BASECLASS (type, i),
1590                                     VALUE_CONTENTS_ALL (*arg1p),
1591                                     offset + VALUE_EMBEDDED_OFFSET (*arg1p),
1592                                     &base_offset, &skip);
1593               if (skip >= 0)
1594                 error ("Virtual base class offset not found in vtable");
1595             }
1596           else
1597             {
1598               struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
1599               char *base_valaddr;
1600
1601               /* The virtual base class pointer might have been clobbered by the
1602                  user program. Make sure that it still points to a valid memory
1603                  location.  */
1604
1605               if (offset < 0 || offset >= TYPE_LENGTH (type))
1606                 {
1607                   base_valaddr = (char *) alloca (TYPE_LENGTH (baseclass));
1608                   if (target_read_memory (VALUE_ADDRESS (*arg1p)
1609                                           + VALUE_OFFSET (*arg1p) + offset,
1610                                           base_valaddr,
1611                                           TYPE_LENGTH (baseclass)) != 0)
1612                     error ("virtual baseclass botch");
1613                 }
1614               else
1615                 base_valaddr = VALUE_CONTENTS (*arg1p) + offset;
1616
1617               base_offset =
1618                 baseclass_offset (type, i, base_valaddr,
1619                                   VALUE_ADDRESS (*arg1p)
1620                                   + VALUE_OFFSET (*arg1p) + offset);
1621               if (base_offset == -1)
1622                 error ("virtual baseclass botch");
1623             }
1624         }
1625       else
1626         {
1627           base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
1628         }
1629       v = search_struct_method (name, arg1p, args, base_offset + offset,
1630                                 static_memfuncp, TYPE_BASECLASS (type, i));
1631       if (v == (struct value *) - 1)
1632         {
1633           name_matched = 1;
1634         }
1635       else if (v)
1636         {
1637 /* FIXME-bothner:  Why is this commented out?  Why is it here?  */
1638 /*        *arg1p = arg1_tmp; */
1639           return v;
1640         }
1641     }
1642   if (name_matched)
1643     return (struct value *) - 1;
1644   else
1645     return NULL;
1646 }
1647
1648 /* Given *ARGP, a value of type (pointer to a)* structure/union,
1649    extract the component named NAME from the ultimate target structure/union
1650    and return it as a value with its appropriate type.
1651    ERR is used in the error message if *ARGP's type is wrong.
1652
1653    C++: ARGS is a list of argument types to aid in the selection of
1654    an appropriate method. Also, handle derived types.
1655
1656    STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
1657    where the truthvalue of whether the function that was resolved was
1658    a static member function or not is stored.
1659
1660    ERR is an error message to be printed in case the field is not found.  */
1661
1662 struct value *
1663 value_struct_elt (struct value **argp, struct value **args,
1664                   char *name, int *static_memfuncp, char *err)
1665 {
1666   struct type *t;
1667   struct value *v;
1668
1669   COERCE_ARRAY (*argp);
1670
1671   t = check_typedef (VALUE_TYPE (*argp));
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     {
1677       *argp = value_ind (*argp);
1678       /* Don't coerce fn pointer to fn and then back again!  */
1679       if (TYPE_CODE (VALUE_TYPE (*argp)) != TYPE_CODE_FUNC)
1680         COERCE_ARRAY (*argp);
1681       t = check_typedef (VALUE_TYPE (*argp));
1682     }
1683
1684   if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1685     error ("not implemented: member type in value_struct_elt");
1686
1687   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1688       && TYPE_CODE (t) != TYPE_CODE_UNION)
1689     error ("Attempt to extract a component of a value that is not a %s.", err);
1690
1691   /* Assume it's not, unless we see that it is.  */
1692   if (static_memfuncp)
1693     *static_memfuncp = 0;
1694
1695   if (!args)
1696     {
1697       /* if there are no arguments ...do this...  */
1698
1699       /* Try as a field first, because if we succeed, there
1700          is less work to be done.  */
1701       v = search_struct_field (name, *argp, 0, t, 0);
1702       if (v)
1703         return v;
1704
1705       /* C++: If it was not found as a data field, then try to
1706          return it as a pointer to a method.  */
1707
1708       if (destructor_name_p (name, t))
1709         error ("Cannot get value of destructor");
1710
1711       v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
1712
1713       if (v == (struct value *) - 1)
1714         error ("Cannot take address of a method");
1715       else if (v == 0)
1716         {
1717           if (TYPE_NFN_FIELDS (t))
1718             error ("There is no member or method named %s.", name);
1719           else
1720             error ("There is no member named %s.", name);
1721         }
1722       return v;
1723     }
1724
1725   if (destructor_name_p (name, t))
1726     {
1727       if (!args[1])
1728         {
1729           /* Destructors are a special case.  */
1730           int m_index, f_index;
1731
1732           v = NULL;
1733           if (get_destructor_fn_field (t, &m_index, &f_index))
1734             {
1735               v = value_fn_field (NULL, TYPE_FN_FIELDLIST1 (t, m_index),
1736                                   f_index, NULL, 0);
1737             }
1738           if (v == NULL)
1739             error ("could not find destructor function named %s.", name);
1740           else
1741             return v;
1742         }
1743       else
1744         {
1745           error ("destructor should not have any argument");
1746         }
1747     }
1748   else
1749     v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
1750   
1751   if (v == (struct value *) - 1)
1752     {
1753       error ("One of the arguments you tried to pass to %s could not be converted to what the function wants.", name);
1754     }
1755   else if (v == 0)
1756     {
1757       /* See if user tried to invoke data as function.  If so,
1758          hand it back.  If it's not callable (i.e., a pointer to function),
1759          gdb should give an error.  */
1760       v = search_struct_field (name, *argp, 0, t, 0);
1761     }
1762
1763   if (!v)
1764     error ("Structure has no component named %s.", name);
1765   return v;
1766 }
1767
1768 /* Search through the methods of an object (and its bases)
1769  * to find a specified method. Return the pointer to the
1770  * fn_field list of overloaded instances.
1771  * Helper function for value_find_oload_list.
1772  * ARGP is a pointer to a pointer to a value (the object)
1773  * METHOD is a string containing the method name
1774  * OFFSET is the offset within the value
1775  * TYPE is the assumed type of the object
1776  * NUM_FNS is the number of overloaded instances
1777  * BASETYPE is set to the actual type of the subobject where the method is found
1778  * BOFFSET is the offset of the base subobject where the method is found */
1779
1780 static struct fn_field *
1781 find_method_list (struct value **argp, char *method, int offset,
1782                   struct type *type, int *num_fns,
1783                   struct type **basetype, int *boffset)
1784 {
1785   int i;
1786   struct fn_field *f;
1787   CHECK_TYPEDEF (type);
1788
1789   *num_fns = 0;
1790
1791   /* First check in object itself */
1792   for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
1793     {
1794       /* pai: FIXME What about operators and type conversions? */
1795       char *fn_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
1796       if (fn_field_name && (strcmp_iw (fn_field_name, method) == 0))
1797         {
1798           int len = TYPE_FN_FIELDLIST_LENGTH (type, i);
1799           struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1800
1801           *num_fns = len;
1802           *basetype = type;
1803           *boffset = offset;
1804
1805           /* Resolve any stub methods.  */
1806           check_stub_method_group (type, i);
1807
1808           return f;
1809         }
1810     }
1811
1812   /* Not found in object, check in base subobjects */
1813   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1814     {
1815       int base_offset;
1816       if (BASETYPE_VIA_VIRTUAL (type, i))
1817         {
1818           if (TYPE_HAS_VTABLE (type))
1819             {
1820               /* HP aCC compiled type, search for virtual base offset
1821                * according to HP/Taligent runtime spec.  */
1822               int skip;
1823               find_rt_vbase_offset (type, TYPE_BASECLASS (type, i),
1824                                     VALUE_CONTENTS_ALL (*argp),
1825                                     offset + VALUE_EMBEDDED_OFFSET (*argp),
1826                                     &base_offset, &skip);
1827               if (skip >= 0)
1828                 error ("Virtual base class offset not found in vtable");
1829             }
1830           else
1831             {
1832               /* probably g++ runtime model */
1833               base_offset = VALUE_OFFSET (*argp) + offset;
1834               base_offset =
1835                 baseclass_offset (type, i,
1836                                   VALUE_CONTENTS (*argp) + base_offset,
1837                                   VALUE_ADDRESS (*argp) + base_offset);
1838               if (base_offset == -1)
1839                 error ("virtual baseclass botch");
1840             }
1841         }
1842       else
1843         /* non-virtual base, simply use bit position from debug info */
1844         {
1845           base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
1846         }
1847       f = find_method_list (argp, method, base_offset + offset,
1848                             TYPE_BASECLASS (type, i), num_fns, basetype,
1849                             boffset);
1850       if (f)
1851         return f;
1852     }
1853   return NULL;
1854 }
1855
1856 /* Return the list of overloaded methods of a specified name.
1857  * ARGP is a pointer to a pointer to a value (the object)
1858  * METHOD is the method name
1859  * OFFSET is the offset within the value contents
1860  * NUM_FNS is the number of overloaded instances
1861  * BASETYPE is set to the type of the base subobject that defines the method
1862  * BOFFSET is the offset of the base subobject which defines the method */
1863
1864 struct fn_field *
1865 value_find_oload_method_list (struct value **argp, char *method, int offset,
1866                               int *num_fns, struct type **basetype,
1867                               int *boffset)
1868 {
1869   struct type *t;
1870
1871   t = check_typedef (VALUE_TYPE (*argp));
1872
1873   /* code snarfed from value_struct_elt */
1874   while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1875     {
1876       *argp = value_ind (*argp);
1877       /* Don't coerce fn pointer to fn and then back again!  */
1878       if (TYPE_CODE (VALUE_TYPE (*argp)) != TYPE_CODE_FUNC)
1879         COERCE_ARRAY (*argp);
1880       t = check_typedef (VALUE_TYPE (*argp));
1881     }
1882
1883   if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1884     error ("Not implemented: member type in value_find_oload_lis");
1885
1886   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1887       && TYPE_CODE (t) != TYPE_CODE_UNION)
1888     error ("Attempt to extract a component of a value that is not a struct or union");
1889
1890   return find_method_list (argp, method, 0, t, num_fns, basetype, boffset);
1891 }
1892
1893 /* Given an array of argument types (ARGTYPES) (which includes an
1894    entry for "this" in the case of C++ methods), the number of
1895    arguments NARGS, the NAME of a function whether it's a method or
1896    not (METHOD), and the degree of laxness (LAX) in conforming to
1897    overload resolution rules in ANSI C++, find the best function that
1898    matches on the argument types according to the overload resolution
1899    rules.
1900
1901    In the case of class methods, the parameter OBJ is an object value
1902    in which to search for overloaded methods.
1903
1904    In the case of non-method functions, the parameter FSYM is a symbol
1905    corresponding to one of the overloaded functions.
1906
1907    Return value is an integer: 0 -> good match, 10 -> debugger applied
1908    non-standard coercions, 100 -> incompatible.
1909
1910    If a method is being searched for, VALP will hold the value.
1911    If a non-method is being searched for, SYMP will hold the symbol for it.
1912
1913    If a method is being searched for, and it is a static method,
1914    then STATICP will point to a non-zero value.
1915
1916    Note: This function does *not* check the value of
1917    overload_resolution.  Caller must check it to see whether overload
1918    resolution is permitted.
1919  */
1920
1921 int
1922 find_overload_match (struct type **arg_types, int nargs, char *name, int method,
1923                      int lax, struct value **objp, struct symbol *fsym,
1924                      struct value **valp, struct symbol **symp, int *staticp)
1925 {
1926   struct value *obj = (objp ? *objp : NULL);
1927
1928   int oload_champ;              /* Index of best overloaded function */
1929
1930   struct badness_vector *oload_champ_bv = NULL;         /* The measure for the current best match */
1931
1932   struct value *temp = obj;
1933   struct fn_field *fns_ptr = NULL;      /* For methods, the list of overloaded methods */
1934   struct symbol **oload_syms = NULL;    /* For non-methods, the list of overloaded function symbols */
1935   int num_fns = 0;              /* Number of overloaded instances being considered */
1936   struct type *basetype = NULL;
1937   int boffset;
1938   int ix;
1939   int static_offset;
1940   struct cleanup *old_cleanups = NULL;
1941
1942   const char *obj_type_name = NULL;
1943   char *func_name = NULL;
1944   enum oload_classification match_quality;
1945
1946   /* Get the list of overloaded methods or functions */
1947   if (method)
1948     {
1949       obj_type_name = TYPE_NAME (VALUE_TYPE (obj));
1950       /* Hack: evaluate_subexp_standard often passes in a pointer
1951          value rather than the object itself, so try again */
1952       if ((!obj_type_name || !*obj_type_name) &&
1953           (TYPE_CODE (VALUE_TYPE (obj)) == TYPE_CODE_PTR))
1954         obj_type_name = TYPE_NAME (TYPE_TARGET_TYPE (VALUE_TYPE (obj)));
1955
1956       fns_ptr = value_find_oload_method_list (&temp, name, 0,
1957                                               &num_fns,
1958                                               &basetype, &boffset);
1959       if (!fns_ptr || !num_fns)
1960         error ("Couldn't find method %s%s%s",
1961                obj_type_name,
1962                (obj_type_name && *obj_type_name) ? "::" : "",
1963                name);
1964       /* If we are dealing with stub method types, they should have
1965          been resolved by find_method_list via value_find_oload_method_list
1966          above.  */
1967       gdb_assert (TYPE_DOMAIN_TYPE (fns_ptr[0].type) != NULL);
1968       oload_champ = find_oload_champ (arg_types, nargs, method, num_fns,
1969                                       fns_ptr, oload_syms, &oload_champ_bv);
1970     }
1971   else
1972     {
1973       const char *qualified_name = SYMBOL_CPLUS_DEMANGLED_NAME (fsym);
1974       func_name = cp_func_name (qualified_name);
1975
1976       /* If the name is NULL this must be a C-style function.
1977          Just return the same symbol. */
1978       if (func_name == NULL)
1979         {
1980           *symp = fsym;
1981           return 0;
1982         }
1983
1984       old_cleanups = make_cleanup (xfree, func_name);
1985       make_cleanup (xfree, oload_syms);
1986       make_cleanup (xfree, oload_champ_bv);
1987
1988       oload_champ = find_oload_champ_namespace (arg_types, nargs,
1989                                                 func_name,
1990                                                 qualified_name,
1991                                                 &oload_syms,
1992                                                 &oload_champ_bv);
1993     }
1994
1995   /* Check how bad the best match is.  */
1996
1997   match_quality
1998     = classify_oload_match (oload_champ_bv, nargs,
1999                             oload_method_static (method, fns_ptr,
2000                                                  oload_champ));
2001
2002   if (match_quality == INCOMPATIBLE)
2003     {
2004       if (method)
2005         error ("Cannot resolve method %s%s%s to any overloaded instance",
2006                obj_type_name,
2007                (obj_type_name && *obj_type_name) ? "::" : "",
2008                name);
2009       else
2010         error ("Cannot resolve function %s to any overloaded instance",
2011                func_name);
2012     }
2013   else if (match_quality == NON_STANDARD)
2014     {
2015       if (method)
2016         warning ("Using non-standard conversion to match method %s%s%s to supplied arguments",
2017                  obj_type_name,
2018                  (obj_type_name && *obj_type_name) ? "::" : "",
2019                  name);
2020       else
2021         warning ("Using non-standard conversion to match function %s to supplied arguments",
2022                  func_name);
2023     }
2024
2025   if (method)
2026     {
2027       if (staticp != NULL)
2028         *staticp = oload_method_static (method, fns_ptr, oload_champ);
2029       if (TYPE_FN_FIELD_VIRTUAL_P (fns_ptr, oload_champ))
2030         *valp = value_virtual_fn_field (&temp, fns_ptr, oload_champ, basetype, boffset);
2031       else
2032         *valp = value_fn_field (&temp, fns_ptr, oload_champ, basetype, boffset);
2033     }
2034   else
2035     {
2036       *symp = oload_syms[oload_champ];
2037     }
2038
2039   if (objp)
2040     {
2041       if (TYPE_CODE (VALUE_TYPE (temp)) != TYPE_CODE_PTR
2042           && TYPE_CODE (VALUE_TYPE (*objp)) == TYPE_CODE_PTR)
2043         {
2044           temp = value_addr (temp);
2045         }
2046       *objp = temp;
2047     }
2048   if (old_cleanups != NULL)
2049     do_cleanups (old_cleanups);
2050
2051   switch (match_quality)
2052     {
2053     case INCOMPATIBLE:
2054       return 100;
2055     case NON_STANDARD:
2056       return 10;
2057     default:                            /* STANDARD */
2058       return 0;
2059     }
2060 }
2061
2062 /* Find the best overload match, searching for FUNC_NAME in namespaces
2063    contained in QUALIFIED_NAME until it either finds a good match or
2064    runs out of namespaces.  It stores the overloaded functions in
2065    *OLOAD_SYMS, and the badness vector in *OLOAD_CHAMP_BV.  The
2066    calling function is responsible for freeing *OLOAD_SYMS and
2067    *OLOAD_CHAMP_BV.  */
2068
2069 static int
2070 find_oload_champ_namespace (struct type **arg_types, int nargs,
2071                             const char *func_name,
2072                             const char *qualified_name,
2073                             struct symbol ***oload_syms,
2074                             struct badness_vector **oload_champ_bv)
2075 {
2076   int oload_champ;
2077
2078   find_oload_champ_namespace_loop (arg_types, nargs,
2079                                    func_name,
2080                                    qualified_name, 0,
2081                                    oload_syms, oload_champ_bv,
2082                                    &oload_champ);
2083
2084   return oload_champ;
2085 }
2086
2087 /* Helper function for find_oload_champ_namespace; NAMESPACE_LEN is
2088    how deep we've looked for namespaces, and the champ is stored in
2089    OLOAD_CHAMP.  The return value is 1 if the champ is a good one, 0
2090    if it isn't.
2091
2092    It is the caller's responsibility to free *OLOAD_SYMS and
2093    *OLOAD_CHAMP_BV.  */
2094
2095 static int
2096 find_oload_champ_namespace_loop (struct type **arg_types, int nargs,
2097                                  const char *func_name,
2098                                  const char *qualified_name,
2099                                  int namespace_len,
2100                                  struct symbol ***oload_syms,
2101                                  struct badness_vector **oload_champ_bv,
2102                                  int *oload_champ)
2103 {
2104   int next_namespace_len = namespace_len;
2105   int searched_deeper = 0;
2106   int num_fns = 0;
2107   struct cleanup *old_cleanups;
2108   int new_oload_champ;
2109   struct symbol **new_oload_syms;
2110   struct badness_vector *new_oload_champ_bv;
2111   char *new_namespace;
2112
2113   if (next_namespace_len != 0)
2114     {
2115       gdb_assert (qualified_name[next_namespace_len] == ':');
2116       next_namespace_len +=  2;
2117     }
2118   next_namespace_len
2119     += cp_find_first_component (qualified_name + next_namespace_len);
2120
2121   /* Initialize these to values that can safely be xfree'd.  */
2122   *oload_syms = NULL;
2123   *oload_champ_bv = NULL;
2124
2125   /* First, see if we have a deeper namespace we can search in.  If we
2126      get a good match there, use it.  */
2127
2128   if (qualified_name[next_namespace_len] == ':')
2129     {
2130       searched_deeper = 1;
2131
2132       if (find_oload_champ_namespace_loop (arg_types, nargs,
2133                                            func_name, qualified_name,
2134                                            next_namespace_len,
2135                                            oload_syms, oload_champ_bv,
2136                                            oload_champ))
2137         {
2138           return 1;
2139         }
2140     };
2141
2142   /* If we reach here, either we're in the deepest namespace or we
2143      didn't find a good match in a deeper namespace.  But, in the
2144      latter case, we still have a bad match in a deeper namespace;
2145      note that we might not find any match at all in the current
2146      namespace.  (There's always a match in the deepest namespace,
2147      because this overload mechanism only gets called if there's a
2148      function symbol to start off with.)  */
2149
2150   old_cleanups = make_cleanup (xfree, *oload_syms);
2151   old_cleanups = make_cleanup (xfree, *oload_champ_bv);
2152   new_namespace = alloca (namespace_len + 1);
2153   strncpy (new_namespace, qualified_name, namespace_len);
2154   new_namespace[namespace_len] = '\0';
2155   new_oload_syms = make_symbol_overload_list (func_name,
2156                                               new_namespace);
2157   while (new_oload_syms[num_fns])
2158     ++num_fns;
2159
2160   new_oload_champ = find_oload_champ (arg_types, nargs, 0, num_fns,
2161                                       NULL, new_oload_syms,
2162                                       &new_oload_champ_bv);
2163
2164   /* Case 1: We found a good match.  Free earlier matches (if any),
2165      and return it.  Case 2: We didn't find a good match, but we're
2166      not the deepest function.  Then go with the bad match that the
2167      deeper function found.  Case 3: We found a bad match, and we're
2168      the deepest function.  Then return what we found, even though
2169      it's a bad match.  */
2170
2171   if (new_oload_champ != -1
2172       && classify_oload_match (new_oload_champ_bv, nargs, 0) == STANDARD)
2173     {
2174       *oload_syms = new_oload_syms;
2175       *oload_champ = new_oload_champ;
2176       *oload_champ_bv = new_oload_champ_bv;
2177       do_cleanups (old_cleanups);
2178       return 1;
2179     }
2180   else if (searched_deeper)
2181     {
2182       xfree (new_oload_syms);
2183       xfree (new_oload_champ_bv);
2184       discard_cleanups (old_cleanups);
2185       return 0;
2186     }
2187   else
2188     {
2189       gdb_assert (new_oload_champ != -1);
2190       *oload_syms = new_oload_syms;
2191       *oload_champ = new_oload_champ;
2192       *oload_champ_bv = new_oload_champ_bv;
2193       discard_cleanups (old_cleanups);
2194       return 0;
2195     }
2196 }
2197
2198 /* Look for a function to take NARGS args of types ARG_TYPES.  Find
2199    the best match from among the overloaded methods or functions
2200    (depending on METHOD) given by FNS_PTR or OLOAD_SYMS, respectively.
2201    The number of methods/functions in the list is given by NUM_FNS.
2202    Return the index of the best match; store an indication of the
2203    quality of the match in OLOAD_CHAMP_BV.
2204
2205    It is the caller's responsibility to free *OLOAD_CHAMP_BV.  */
2206
2207 static int
2208 find_oload_champ (struct type **arg_types, int nargs, int method,
2209                   int num_fns, struct fn_field *fns_ptr,
2210                   struct symbol **oload_syms,
2211                   struct badness_vector **oload_champ_bv)
2212 {
2213   int ix;
2214   struct badness_vector *bv;    /* A measure of how good an overloaded instance is */
2215   int oload_champ = -1;         /* Index of best overloaded function */
2216   int oload_ambiguous = 0;      /* Current ambiguity state for overload resolution */
2217   /* 0 => no ambiguity, 1 => two good funcs, 2 => incomparable funcs */
2218
2219   *oload_champ_bv = NULL;
2220
2221   /* Consider each candidate in turn */
2222   for (ix = 0; ix < num_fns; ix++)
2223     {
2224       int jj;
2225       int static_offset = oload_method_static (method, fns_ptr, ix);
2226       int nparms;
2227       struct type **parm_types;
2228
2229       if (method)
2230         {
2231           nparms = TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (fns_ptr, ix));
2232         }
2233       else
2234         {
2235           /* If it's not a method, this is the proper place */
2236           nparms=TYPE_NFIELDS(SYMBOL_TYPE(oload_syms[ix]));
2237         }
2238
2239       /* Prepare array of parameter types */
2240       parm_types = (struct type **) xmalloc (nparms * (sizeof (struct type *)));
2241       for (jj = 0; jj < nparms; jj++)
2242         parm_types[jj] = (method
2243                           ? (TYPE_FN_FIELD_ARGS (fns_ptr, ix)[jj].type)
2244                           : TYPE_FIELD_TYPE (SYMBOL_TYPE (oload_syms[ix]), jj));
2245
2246       /* Compare parameter types to supplied argument types.  Skip THIS for
2247          static methods.  */
2248       bv = rank_function (parm_types, nparms, arg_types + static_offset,
2249                           nargs - static_offset);
2250
2251       if (!*oload_champ_bv)
2252         {
2253           *oload_champ_bv = bv;
2254           oload_champ = 0;
2255         }
2256       else
2257         /* See whether current candidate is better or worse than previous best */
2258         switch (compare_badness (bv, *oload_champ_bv))
2259           {
2260           case 0:
2261             oload_ambiguous = 1;        /* top two contenders are equally good */
2262             break;
2263           case 1:
2264             oload_ambiguous = 2;        /* incomparable top contenders */
2265             break;
2266           case 2:
2267             *oload_champ_bv = bv;       /* new champion, record details */
2268             oload_ambiguous = 0;
2269             oload_champ = ix;
2270             break;
2271           case 3:
2272           default:
2273             break;
2274           }
2275       xfree (parm_types);
2276       if (overload_debug)
2277         {
2278           if (method)
2279             fprintf_filtered (gdb_stderr,"Overloaded method instance %s, # of parms %d\n", fns_ptr[ix].physname, nparms);
2280           else
2281             fprintf_filtered (gdb_stderr,"Overloaded function instance %s # of parms %d\n", SYMBOL_DEMANGLED_NAME (oload_syms[ix]), nparms);
2282           for (jj = 0; jj < nargs - static_offset; jj++)
2283             fprintf_filtered (gdb_stderr,"...Badness @ %d : %d\n", jj, bv->rank[jj]);
2284           fprintf_filtered (gdb_stderr,"Overload resolution champion is %d, ambiguous? %d\n", oload_champ, oload_ambiguous);
2285         }
2286     }
2287
2288   return oload_champ;
2289 }
2290
2291 /* Return 1 if we're looking at a static method, 0 if we're looking at
2292    a non-static method or a function that isn't a method.  */
2293
2294 static int
2295 oload_method_static (int method, struct fn_field *fns_ptr, int index)
2296 {
2297   if (method && TYPE_FN_FIELD_STATIC_P (fns_ptr, index))
2298     return 1;
2299   else
2300     return 0;
2301 }
2302
2303 /* Check how good an overload match OLOAD_CHAMP_BV represents.  */
2304
2305 static enum oload_classification
2306 classify_oload_match (struct badness_vector *oload_champ_bv,
2307                       int nargs,
2308                       int static_offset)
2309 {
2310   int ix;
2311
2312   for (ix = 1; ix <= nargs - static_offset; ix++)
2313     {
2314       if (oload_champ_bv->rank[ix] >= 100)
2315         return INCOMPATIBLE;    /* truly mismatched types */
2316       else if (oload_champ_bv->rank[ix] >= 10)
2317         return NON_STANDARD;    /* non-standard type conversions needed */
2318     }
2319
2320   return STANDARD;              /* Only standard conversions needed.  */
2321 }
2322
2323 /* C++: return 1 is NAME is a legitimate name for the destructor
2324    of type TYPE.  If TYPE does not have a destructor, or
2325    if NAME is inappropriate for TYPE, an error is signaled.  */
2326 int
2327 destructor_name_p (const char *name, const struct type *type)
2328 {
2329   /* destructors are a special case.  */
2330
2331   if (name[0] == '~')
2332     {
2333       char *dname = type_name_no_tag (type);
2334       char *cp = strchr (dname, '<');
2335       unsigned int len;
2336
2337       /* Do not compare the template part for template classes.  */
2338       if (cp == NULL)
2339         len = strlen (dname);
2340       else
2341         len = cp - dname;
2342       if (strlen (name + 1) != len || strncmp (dname, name + 1, len) != 0)
2343         error ("name of destructor must equal name of class");
2344       else
2345         return 1;
2346     }
2347   return 0;
2348 }
2349
2350 /* Helper function for check_field: Given TYPE, a structure/union,
2351    return 1 if the component named NAME from the ultimate
2352    target structure/union is defined, otherwise, return 0. */
2353
2354 static int
2355 check_field_in (struct type *type, const char *name)
2356 {
2357   int i;
2358
2359   for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
2360     {
2361       char *t_field_name = TYPE_FIELD_NAME (type, i);
2362       if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
2363         return 1;
2364     }
2365
2366   /* C++: If it was not found as a data field, then try to
2367      return it as a pointer to a method.  */
2368
2369   /* Destructors are a special case.  */
2370   if (destructor_name_p (name, type))
2371     {
2372       int m_index, f_index;
2373
2374       return get_destructor_fn_field (type, &m_index, &f_index);
2375     }
2376
2377   for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
2378     {
2379       if (strcmp_iw (TYPE_FN_FIELDLIST_NAME (type, i), name) == 0)
2380         return 1;
2381     }
2382
2383   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
2384     if (check_field_in (TYPE_BASECLASS (type, i), name))
2385       return 1;
2386
2387   return 0;
2388 }
2389
2390
2391 /* C++: Given ARG1, a value of type (pointer to a)* structure/union,
2392    return 1 if the component named NAME from the ultimate
2393    target structure/union is defined, otherwise, return 0.  */
2394
2395 int
2396 check_field (struct value *arg1, const char *name)
2397 {
2398   struct type *t;
2399
2400   COERCE_ARRAY (arg1);
2401
2402   t = VALUE_TYPE (arg1);
2403
2404   /* Follow pointers until we get to a non-pointer.  */
2405
2406   for (;;)
2407     {
2408       CHECK_TYPEDEF (t);
2409       if (TYPE_CODE (t) != TYPE_CODE_PTR && TYPE_CODE (t) != TYPE_CODE_REF)
2410         break;
2411       t = TYPE_TARGET_TYPE (t);
2412     }
2413
2414   if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
2415     error ("not implemented: member type in check_field");
2416
2417   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
2418       && TYPE_CODE (t) != TYPE_CODE_UNION)
2419     error ("Internal error: `this' is not an aggregate");
2420
2421   return check_field_in (t, name);
2422 }
2423
2424 /* C++: Given an aggregate type CURTYPE, and a member name NAME,
2425    return the appropriate member.  This function is used to resolve
2426    user expressions of the form "DOMAIN::NAME".  For more details on
2427    what happens, see the comment before
2428    value_struct_elt_for_reference.  */
2429
2430 struct value *
2431 value_aggregate_elt (struct type *curtype,
2432                      char *name,
2433                      enum noside noside)
2434 {
2435   switch (TYPE_CODE (curtype))
2436     {
2437     case TYPE_CODE_STRUCT:
2438     case TYPE_CODE_UNION:
2439       return value_struct_elt_for_reference (curtype, 0, curtype, name, NULL,
2440                                              noside);
2441     case TYPE_CODE_NAMESPACE:
2442       return value_namespace_elt (curtype, name, noside);
2443     default:
2444       internal_error (__FILE__, __LINE__,
2445                       "non-aggregate type in value_aggregate_elt");
2446     }
2447 }
2448
2449 /* C++: Given an aggregate type CURTYPE, and a member name NAME,
2450    return the address of this member as a "pointer to member"
2451    type.  If INTYPE is non-null, then it will be the type
2452    of the member we are looking for.  This will help us resolve
2453    "pointers to member functions".  This function is used
2454    to resolve user expressions of the form "DOMAIN::NAME".  */
2455
2456 static struct value *
2457 value_struct_elt_for_reference (struct type *domain, int offset,
2458                                 struct type *curtype, char *name,
2459                                 struct type *intype,
2460                                 enum noside noside)
2461 {
2462   struct type *t = curtype;
2463   int i;
2464   struct value *v;
2465
2466   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
2467       && TYPE_CODE (t) != TYPE_CODE_UNION)
2468     error ("Internal error: non-aggregate type to value_struct_elt_for_reference");
2469
2470   for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
2471     {
2472       char *t_field_name = TYPE_FIELD_NAME (t, i);
2473
2474       if (t_field_name && strcmp (t_field_name, name) == 0)
2475         {
2476           if (TYPE_FIELD_STATIC (t, i))
2477             {
2478               v = value_static_field (t, i);
2479               if (v == NULL)
2480                 error ("static field %s has been optimized out",
2481                        name);
2482               return v;
2483             }
2484           if (TYPE_FIELD_PACKED (t, i))
2485             error ("pointers to bitfield members not allowed");
2486
2487           return value_from_longest
2488             (lookup_reference_type (lookup_member_type (TYPE_FIELD_TYPE (t, i),
2489                                                         domain)),
2490              offset + (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3));
2491         }
2492     }
2493
2494   /* C++: If it was not found as a data field, then try to
2495      return it as a pointer to a method.  */
2496
2497   /* Destructors are a special case.  */
2498   if (destructor_name_p (name, t))
2499     {
2500       error ("member pointers to destructors not implemented yet");
2501     }
2502
2503   /* Perform all necessary dereferencing.  */
2504   while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR)
2505     intype = TYPE_TARGET_TYPE (intype);
2506
2507   for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
2508     {
2509       char *t_field_name = TYPE_FN_FIELDLIST_NAME (t, i);
2510       char dem_opname[64];
2511
2512       if (strncmp (t_field_name, "__", 2) == 0 ||
2513           strncmp (t_field_name, "op", 2) == 0 ||
2514           strncmp (t_field_name, "type", 4) == 0)
2515         {
2516           if (cplus_demangle_opname (t_field_name, dem_opname, DMGL_ANSI))
2517             t_field_name = dem_opname;
2518           else if (cplus_demangle_opname (t_field_name, dem_opname, 0))
2519             t_field_name = dem_opname;
2520         }
2521       if (t_field_name && strcmp (t_field_name, name) == 0)
2522         {
2523           int j = TYPE_FN_FIELDLIST_LENGTH (t, i);
2524           struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
2525
2526           check_stub_method_group (t, i);
2527
2528           if (intype == 0 && j > 1)
2529             error ("non-unique member `%s' requires type instantiation", name);
2530           if (intype)
2531             {
2532               while (j--)
2533                 if (TYPE_FN_FIELD_TYPE (f, j) == intype)
2534                   break;
2535               if (j < 0)
2536                 error ("no member function matches that type instantiation");
2537             }
2538           else
2539             j = 0;
2540
2541           if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
2542             {
2543               return value_from_longest
2544                 (lookup_reference_type
2545                  (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
2546                                       domain)),
2547                  (LONGEST) METHOD_PTR_FROM_VOFFSET (TYPE_FN_FIELD_VOFFSET (f, j)));
2548             }
2549           else
2550             {
2551               struct symbol *s = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
2552                                                 0, VAR_DOMAIN, 0, NULL);
2553               if (s == NULL)
2554                 {
2555                   v = 0;
2556                 }
2557               else
2558                 {
2559                   v = read_var_value (s, 0);
2560 #if 0
2561                   VALUE_TYPE (v) = lookup_reference_type
2562                     (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
2563                                          domain));
2564 #endif
2565                 }
2566               return v;
2567             }
2568         }
2569     }
2570   for (i = TYPE_N_BASECLASSES (t) - 1; i >= 0; i--)
2571     {
2572       struct value *v;
2573       int base_offset;
2574
2575       if (BASETYPE_VIA_VIRTUAL (t, i))
2576         base_offset = 0;
2577       else
2578         base_offset = TYPE_BASECLASS_BITPOS (t, i) / 8;
2579       v = value_struct_elt_for_reference (domain,
2580                                           offset + base_offset,
2581                                           TYPE_BASECLASS (t, i),
2582                                           name,
2583                                           intype,
2584                                           noside);
2585       if (v)
2586         return v;
2587     }
2588
2589   /* As a last chance, pretend that CURTYPE is a namespace, and look
2590      it up that way; this (frequently) works for types nested inside
2591      classes.  */
2592
2593   return value_maybe_namespace_elt (curtype, name, noside);
2594 }
2595
2596 /* C++: Return the member NAME of the namespace given by the type
2597    CURTYPE.  */
2598
2599 static struct value *
2600 value_namespace_elt (const struct type *curtype,
2601                      char *name,
2602                      enum noside noside)
2603 {
2604   struct value *retval = value_maybe_namespace_elt (curtype, name,
2605                                                     noside);
2606
2607   if (retval == NULL)
2608     error ("No symbol \"%s\" in namespace \"%s\".", name,
2609            TYPE_TAG_NAME (curtype));
2610
2611   return retval;
2612 }
2613
2614 /* A helper function used by value_namespace_elt and
2615    value_struct_elt_for_reference.  It looks up NAME inside the
2616    context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
2617    is a class and NAME refers to a type in CURTYPE itself (as opposed
2618    to, say, some base class of CURTYPE).  */
2619
2620 static struct value *
2621 value_maybe_namespace_elt (const struct type *curtype,
2622                            char *name,
2623                            enum noside noside)
2624 {
2625   const char *namespace_name = TYPE_TAG_NAME (curtype);
2626   struct symbol *sym;
2627
2628   sym = cp_lookup_symbol_namespace (namespace_name, name, NULL,
2629                                     get_selected_block (0), VAR_DOMAIN,
2630                                     NULL);
2631
2632   if (sym == NULL)
2633     return NULL;
2634   else if ((noside == EVAL_AVOID_SIDE_EFFECTS)
2635            && (SYMBOL_CLASS (sym) == LOC_TYPEDEF))
2636     return allocate_value (SYMBOL_TYPE (sym));
2637   else
2638     return value_of_variable (sym, get_selected_block (0));
2639 }
2640
2641 /* Given a pointer value V, find the real (RTTI) type
2642    of the object it points to.
2643    Other parameters FULL, TOP, USING_ENC as with value_rtti_type()
2644    and refer to the values computed for the object pointed to. */
2645
2646 struct type *
2647 value_rtti_target_type (struct value *v, int *full, int *top, int *using_enc)
2648 {
2649   struct value *target;
2650
2651   target = value_ind (v);
2652
2653   return value_rtti_type (target, full, top, using_enc);
2654 }
2655
2656 /* Given a value pointed to by ARGP, check its real run-time type, and
2657    if that is different from the enclosing type, create a new value
2658    using the real run-time type as the enclosing type (and of the same
2659    type as ARGP) and return it, with the embedded offset adjusted to
2660    be the correct offset to the enclosed object
2661    RTYPE is the type, and XFULL, XTOP, and XUSING_ENC are the other
2662    parameters, computed by value_rtti_type(). If these are available,
2663    they can be supplied and a second call to value_rtti_type() is avoided.
2664    (Pass RTYPE == NULL if they're not available */
2665
2666 struct value *
2667 value_full_object (struct value *argp, struct type *rtype, int xfull, int xtop,
2668                    int xusing_enc)
2669 {
2670   struct type *real_type;
2671   int full = 0;
2672   int top = -1;
2673   int using_enc = 0;
2674   struct value *new_val;
2675
2676   if (rtype)
2677     {
2678       real_type = rtype;
2679       full = xfull;
2680       top = xtop;
2681       using_enc = xusing_enc;
2682     }
2683   else
2684     real_type = value_rtti_type (argp, &full, &top, &using_enc);
2685
2686   /* If no RTTI data, or if object is already complete, do nothing */
2687   if (!real_type || real_type == VALUE_ENCLOSING_TYPE (argp))
2688     return argp;
2689
2690   /* If we have the full object, but for some reason the enclosing
2691      type is wrong, set it *//* pai: FIXME -- sounds iffy */
2692   if (full)
2693     {
2694       argp = value_change_enclosing_type (argp, real_type);
2695       return argp;
2696     }
2697
2698   /* Check if object is in memory */
2699   if (VALUE_LVAL (argp) != lval_memory)
2700     {
2701       warning ("Couldn't retrieve complete object of RTTI type %s; object may be in register(s).", TYPE_NAME (real_type));
2702
2703       return argp;
2704     }
2705
2706   /* All other cases -- retrieve the complete object */
2707   /* Go back by the computed top_offset from the beginning of the object,
2708      adjusting for the embedded offset of argp if that's what value_rtti_type
2709      used for its computation. */
2710   new_val = value_at_lazy (real_type, VALUE_ADDRESS (argp) - top +
2711                            (using_enc ? 0 : VALUE_EMBEDDED_OFFSET (argp)),
2712                            VALUE_BFD_SECTION (argp));
2713   VALUE_TYPE (new_val) = VALUE_TYPE (argp);
2714   VALUE_EMBEDDED_OFFSET (new_val) = using_enc ? top + VALUE_EMBEDDED_OFFSET (argp) : top;
2715   return new_val;
2716 }
2717
2718
2719
2720
2721 /* Return the value of the local variable, if one exists.
2722    Flag COMPLAIN signals an error if the request is made in an
2723    inappropriate context.  */
2724
2725 struct value *
2726 value_of_local (const char *name, int complain)
2727 {
2728   struct symbol *func, *sym;
2729   struct block *b;
2730   struct value * ret;
2731
2732   if (deprecated_selected_frame == 0)
2733     {
2734       if (complain)
2735         error ("no frame selected");
2736       else
2737         return 0;
2738     }
2739
2740   func = get_frame_function (deprecated_selected_frame);
2741   if (!func)
2742     {
2743       if (complain)
2744         error ("no `%s' in nameless context", name);
2745       else
2746         return 0;
2747     }
2748
2749   b = SYMBOL_BLOCK_VALUE (func);
2750   if (dict_empty (BLOCK_DICT (b)))
2751     {
2752       if (complain)
2753         error ("no args, no `%s'", name);
2754       else
2755         return 0;
2756     }
2757
2758   /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
2759      symbol instead of the LOC_ARG one (if both exist).  */
2760   sym = lookup_block_symbol (b, name, NULL, VAR_DOMAIN);
2761   if (sym == NULL)
2762     {
2763       if (complain)
2764         error ("current stack frame does not contain a variable named `%s'", name);
2765       else
2766         return NULL;
2767     }
2768
2769   ret = read_var_value (sym, deprecated_selected_frame);
2770   if (ret == 0 && complain)
2771     error ("`%s' argument unreadable", name);
2772   return ret;
2773 }
2774
2775 /* C++/Objective-C: return the value of the class instance variable,
2776    if one exists.  Flag COMPLAIN signals an error if the request is
2777    made in an inappropriate context.  */
2778
2779 struct value *
2780 value_of_this (int complain)
2781 {
2782   if (current_language->la_language == language_objc)
2783     return value_of_local ("self", complain);
2784   else
2785     return value_of_local ("this", complain);
2786 }
2787
2788 /* Create a slice (sub-string, sub-array) of ARRAY, that is LENGTH elements
2789    long, starting at LOWBOUND.  The result has the same lower bound as
2790    the original ARRAY.  */
2791
2792 struct value *
2793 value_slice (struct value *array, int lowbound, int length)
2794 {
2795   struct type *slice_range_type, *slice_type, *range_type;
2796   LONGEST lowerbound, upperbound;
2797   struct value *slice;
2798   struct type *array_type;
2799   array_type = check_typedef (VALUE_TYPE (array));
2800   COERCE_VARYING_ARRAY (array, array_type);
2801   if (TYPE_CODE (array_type) != TYPE_CODE_ARRAY
2802       && TYPE_CODE (array_type) != TYPE_CODE_STRING
2803       && TYPE_CODE (array_type) != TYPE_CODE_BITSTRING)
2804     error ("cannot take slice of non-array");
2805   range_type = TYPE_INDEX_TYPE (array_type);
2806   if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0)
2807     error ("slice from bad array or bitstring");
2808   if (lowbound < lowerbound || length < 0
2809       || lowbound + length - 1 > upperbound)
2810     error ("slice out of range");
2811   /* FIXME-type-allocation: need a way to free this type when we are
2812      done with it.  */
2813   slice_range_type = create_range_type ((struct type *) NULL,
2814                                         TYPE_TARGET_TYPE (range_type),
2815                                         lowbound, lowbound + length - 1);
2816   if (TYPE_CODE (array_type) == TYPE_CODE_BITSTRING)
2817     {
2818       int i;
2819       slice_type = create_set_type ((struct type *) NULL, slice_range_type);
2820       TYPE_CODE (slice_type) = TYPE_CODE_BITSTRING;
2821       slice = value_zero (slice_type, not_lval);
2822       for (i = 0; i < length; i++)
2823         {
2824           int element = value_bit_index (array_type,
2825                                          VALUE_CONTENTS (array),
2826                                          lowbound + i);
2827           if (element < 0)
2828             error ("internal error accessing bitstring");
2829           else if (element > 0)
2830             {
2831               int j = i % TARGET_CHAR_BIT;
2832               if (BITS_BIG_ENDIAN)
2833                 j = TARGET_CHAR_BIT - 1 - j;
2834               VALUE_CONTENTS_RAW (slice)[i / TARGET_CHAR_BIT] |= (1 << j);
2835             }
2836         }
2837       /* We should set the address, bitssize, and bitspos, so the clice
2838          can be used on the LHS, but that may require extensions to
2839          value_assign.  For now, just leave as a non_lval.  FIXME.  */
2840     }
2841   else
2842     {
2843       struct type *element_type = TYPE_TARGET_TYPE (array_type);
2844       LONGEST offset
2845         = (lowbound - lowerbound) * TYPE_LENGTH (check_typedef (element_type));
2846       slice_type = create_array_type ((struct type *) NULL, element_type,
2847                                       slice_range_type);
2848       TYPE_CODE (slice_type) = TYPE_CODE (array_type);
2849       slice = allocate_value (slice_type);
2850       if (VALUE_LAZY (array))
2851         VALUE_LAZY (slice) = 1;
2852       else
2853         memcpy (VALUE_CONTENTS (slice), VALUE_CONTENTS (array) + offset,
2854                 TYPE_LENGTH (slice_type));
2855       if (VALUE_LVAL (array) == lval_internalvar)
2856         VALUE_LVAL (slice) = lval_internalvar_component;
2857       else
2858         VALUE_LVAL (slice) = VALUE_LVAL (array);
2859       VALUE_ADDRESS (slice) = VALUE_ADDRESS (array);
2860       VALUE_OFFSET (slice) = VALUE_OFFSET (array) + offset;
2861     }
2862   return slice;
2863 }
2864
2865 /* Create a value for a FORTRAN complex number.  Currently most of
2866    the time values are coerced to COMPLEX*16 (i.e. a complex number
2867    composed of 2 doubles.  This really should be a smarter routine
2868    that figures out precision inteligently as opposed to assuming
2869    doubles. FIXME: fmb */
2870
2871 struct value *
2872 value_literal_complex (struct value *arg1, struct value *arg2, struct type *type)
2873 {
2874   struct value *val;
2875   struct type *real_type = TYPE_TARGET_TYPE (type);
2876
2877   val = allocate_value (type);
2878   arg1 = value_cast (real_type, arg1);
2879   arg2 = value_cast (real_type, arg2);
2880
2881   memcpy (VALUE_CONTENTS_RAW (val),
2882           VALUE_CONTENTS (arg1), TYPE_LENGTH (real_type));
2883   memcpy (VALUE_CONTENTS_RAW (val) + TYPE_LENGTH (real_type),
2884           VALUE_CONTENTS (arg2), TYPE_LENGTH (real_type));
2885   return val;
2886 }
2887
2888 /* Cast a value into the appropriate complex data type. */
2889
2890 static struct value *
2891 cast_into_complex (struct type *type, struct value *val)
2892 {
2893   struct type *real_type = TYPE_TARGET_TYPE (type);
2894   if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_COMPLEX)
2895     {
2896       struct type *val_real_type = TYPE_TARGET_TYPE (VALUE_TYPE (val));
2897       struct value *re_val = allocate_value (val_real_type);
2898       struct value *im_val = allocate_value (val_real_type);
2899
2900       memcpy (VALUE_CONTENTS_RAW (re_val),
2901               VALUE_CONTENTS (val), TYPE_LENGTH (val_real_type));
2902       memcpy (VALUE_CONTENTS_RAW (im_val),
2903               VALUE_CONTENTS (val) + TYPE_LENGTH (val_real_type),
2904               TYPE_LENGTH (val_real_type));
2905
2906       return value_literal_complex (re_val, im_val, type);
2907     }
2908   else if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FLT
2909            || TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_INT)
2910     return value_literal_complex (val, value_zero (real_type, not_lval), type);
2911   else
2912     error ("cannot cast non-number to complex");
2913 }
2914
2915 void
2916 _initialize_valops (void)
2917 {
2918 #if 0
2919   add_show_from_set
2920     (add_set_cmd ("abandon", class_support, var_boolean, (char *) &auto_abandon,
2921                   "Set automatic abandonment of expressions upon failure.",
2922                   &setlist),
2923      &showlist);
2924 #endif
2925
2926   add_show_from_set
2927     (add_set_cmd ("overload-resolution", class_support, var_boolean, (char *) &overload_resolution,
2928                   "Set overload resolution in evaluating C++ functions.",
2929                   &setlist),
2930      &showlist);
2931   overload_resolution = 1;
2932 }