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