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