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