OSDN Git Service

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