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