xref: /dragonfly/contrib/gdb-7/gdb/valops.c (revision ef5ccd6c)
15796c8dcSSimon Schubert /* Perform non-arithmetic operations on values, for GDB.
25796c8dcSSimon Schubert 
3*ef5ccd6cSJohn Marino    Copyright (C) 1986-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    This file is part of GDB.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert    (at your option) any later version.
115796c8dcSSimon Schubert 
125796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
155796c8dcSSimon Schubert    GNU General Public License for more details.
165796c8dcSSimon Schubert 
175796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert #include "defs.h"
215796c8dcSSimon Schubert #include "symtab.h"
225796c8dcSSimon Schubert #include "gdbtypes.h"
235796c8dcSSimon Schubert #include "value.h"
245796c8dcSSimon Schubert #include "frame.h"
255796c8dcSSimon Schubert #include "inferior.h"
265796c8dcSSimon Schubert #include "gdbcore.h"
275796c8dcSSimon Schubert #include "target.h"
285796c8dcSSimon Schubert #include "demangle.h"
295796c8dcSSimon Schubert #include "language.h"
305796c8dcSSimon Schubert #include "gdbcmd.h"
315796c8dcSSimon Schubert #include "regcache.h"
325796c8dcSSimon Schubert #include "cp-abi.h"
335796c8dcSSimon Schubert #include "block.h"
345796c8dcSSimon Schubert #include "infcall.h"
355796c8dcSSimon Schubert #include "dictionary.h"
365796c8dcSSimon Schubert #include "cp-support.h"
375796c8dcSSimon Schubert #include "dfp.h"
385796c8dcSSimon Schubert #include "user-regs.h"
39c50c785cSJohn Marino #include "tracepoint.h"
405796c8dcSSimon Schubert #include <errno.h>
415796c8dcSSimon Schubert #include "gdb_string.h"
425796c8dcSSimon Schubert #include "gdb_assert.h"
435796c8dcSSimon Schubert #include "cp-support.h"
445796c8dcSSimon Schubert #include "observer.h"
455796c8dcSSimon Schubert #include "objfiles.h"
465796c8dcSSimon Schubert #include "symtab.h"
47c50c785cSJohn Marino #include "exceptions.h"
485796c8dcSSimon Schubert 
49*ef5ccd6cSJohn Marino extern unsigned int overload_debug;
505796c8dcSSimon Schubert /* Local functions.  */
515796c8dcSSimon Schubert 
525796c8dcSSimon Schubert static int typecmp (int staticp, int varargs, int nargs,
535796c8dcSSimon Schubert 		    struct field t1[], struct value *t2[]);
545796c8dcSSimon Schubert 
55cf7f2e2dSJohn Marino static struct value *search_struct_field (const char *, struct value *,
565796c8dcSSimon Schubert 					  int, struct type *, int);
575796c8dcSSimon Schubert 
58cf7f2e2dSJohn Marino static struct value *search_struct_method (const char *, struct value **,
595796c8dcSSimon Schubert 					   struct value **,
605796c8dcSSimon Schubert 					   int, int *, struct type *);
615796c8dcSSimon Schubert 
62a45ae5f8SJohn Marino static int find_oload_champ_namespace (struct value **, int,
635796c8dcSSimon Schubert 				       const char *, const char *,
645796c8dcSSimon Schubert 				       struct symbol ***,
65cf7f2e2dSJohn Marino 				       struct badness_vector **,
66cf7f2e2dSJohn Marino 				       const int no_adl);
675796c8dcSSimon Schubert 
685796c8dcSSimon Schubert static
69a45ae5f8SJohn Marino int find_oload_champ_namespace_loop (struct value **, int,
705796c8dcSSimon Schubert 				     const char *, const char *,
715796c8dcSSimon Schubert 				     int, struct symbol ***,
72cf7f2e2dSJohn Marino 				     struct badness_vector **, int *,
73cf7f2e2dSJohn Marino 				     const int no_adl);
745796c8dcSSimon Schubert 
75a45ae5f8SJohn Marino static int find_oload_champ (struct value **, int, int, int,
765796c8dcSSimon Schubert 			     struct fn_field *, struct symbol **,
775796c8dcSSimon Schubert 			     struct badness_vector **);
785796c8dcSSimon Schubert 
795796c8dcSSimon Schubert static int oload_method_static (int, struct fn_field *, int);
805796c8dcSSimon Schubert 
815796c8dcSSimon Schubert enum oload_classification { STANDARD, NON_STANDARD, INCOMPATIBLE };
825796c8dcSSimon Schubert 
835796c8dcSSimon Schubert static enum
845796c8dcSSimon Schubert oload_classification classify_oload_match (struct badness_vector *,
855796c8dcSSimon Schubert 					   int, int);
865796c8dcSSimon Schubert 
875796c8dcSSimon Schubert static struct value *value_struct_elt_for_reference (struct type *,
885796c8dcSSimon Schubert 						     int, struct type *,
895796c8dcSSimon Schubert 						     char *,
905796c8dcSSimon Schubert 						     struct type *,
915796c8dcSSimon Schubert 						     int, enum noside);
925796c8dcSSimon Schubert 
935796c8dcSSimon Schubert static struct value *value_namespace_elt (const struct type *,
945796c8dcSSimon Schubert 					  char *, int , enum noside);
955796c8dcSSimon Schubert 
965796c8dcSSimon Schubert static struct value *value_maybe_namespace_elt (const struct type *,
975796c8dcSSimon Schubert 						char *, int,
985796c8dcSSimon Schubert 						enum noside);
995796c8dcSSimon Schubert 
1005796c8dcSSimon Schubert static CORE_ADDR allocate_space_in_inferior (int);
1015796c8dcSSimon Schubert 
1025796c8dcSSimon Schubert static struct value *cast_into_complex (struct type *, struct value *);
1035796c8dcSSimon Schubert 
104cf7f2e2dSJohn Marino static struct fn_field *find_method_list (struct value **, const char *,
1055796c8dcSSimon Schubert 					  int, struct type *, int *,
1065796c8dcSSimon Schubert 					  struct type **, int *);
1075796c8dcSSimon Schubert 
1085796c8dcSSimon Schubert void _initialize_valops (void);
1095796c8dcSSimon Schubert 
1105796c8dcSSimon Schubert #if 0
1115796c8dcSSimon Schubert /* Flag for whether we want to abandon failed expression evals by
1125796c8dcSSimon Schubert    default.  */
1135796c8dcSSimon Schubert 
1145796c8dcSSimon Schubert static int auto_abandon = 0;
1155796c8dcSSimon Schubert #endif
1165796c8dcSSimon Schubert 
1175796c8dcSSimon Schubert int overload_resolution = 0;
1185796c8dcSSimon Schubert static void
show_overload_resolution(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)1195796c8dcSSimon Schubert show_overload_resolution (struct ui_file *file, int from_tty,
1205796c8dcSSimon Schubert 			  struct cmd_list_element *c,
1215796c8dcSSimon Schubert 			  const char *value)
1225796c8dcSSimon Schubert {
123c50c785cSJohn Marino   fprintf_filtered (file, _("Overload resolution in evaluating "
124c50c785cSJohn Marino 			    "C++ functions is %s.\n"),
1255796c8dcSSimon Schubert 		    value);
1265796c8dcSSimon Schubert }
1275796c8dcSSimon Schubert 
1285796c8dcSSimon Schubert /* Find the address of function name NAME in the inferior.  If OBJF_P
1295796c8dcSSimon Schubert    is non-NULL, *OBJF_P will be set to the OBJFILE where the function
1305796c8dcSSimon Schubert    is defined.  */
1315796c8dcSSimon Schubert 
1325796c8dcSSimon Schubert struct value *
find_function_in_inferior(const char * name,struct objfile ** objf_p)1335796c8dcSSimon Schubert find_function_in_inferior (const char *name, struct objfile **objf_p)
1345796c8dcSSimon Schubert {
1355796c8dcSSimon Schubert   struct symbol *sym;
136cf7f2e2dSJohn Marino 
1375796c8dcSSimon Schubert   sym = lookup_symbol (name, 0, VAR_DOMAIN, 0);
1385796c8dcSSimon Schubert   if (sym != NULL)
1395796c8dcSSimon Schubert     {
1405796c8dcSSimon Schubert       if (SYMBOL_CLASS (sym) != LOC_BLOCK)
1415796c8dcSSimon Schubert 	{
1425796c8dcSSimon Schubert 	  error (_("\"%s\" exists in this program but is not a function."),
1435796c8dcSSimon Schubert 		 name);
1445796c8dcSSimon Schubert 	}
1455796c8dcSSimon Schubert 
1465796c8dcSSimon Schubert       if (objf_p)
1475796c8dcSSimon Schubert 	*objf_p = SYMBOL_SYMTAB (sym)->objfile;
1485796c8dcSSimon Schubert 
1495796c8dcSSimon Schubert       return value_of_variable (sym, NULL);
1505796c8dcSSimon Schubert     }
1515796c8dcSSimon Schubert   else
1525796c8dcSSimon Schubert     {
1535796c8dcSSimon Schubert       struct minimal_symbol *msymbol =
1545796c8dcSSimon Schubert 	lookup_minimal_symbol (name, NULL, NULL);
155cf7f2e2dSJohn Marino 
1565796c8dcSSimon Schubert       if (msymbol != NULL)
1575796c8dcSSimon Schubert 	{
1585796c8dcSSimon Schubert 	  struct objfile *objfile = msymbol_objfile (msymbol);
1595796c8dcSSimon Schubert 	  struct gdbarch *gdbarch = get_objfile_arch (objfile);
1605796c8dcSSimon Schubert 
1615796c8dcSSimon Schubert 	  struct type *type;
1625796c8dcSSimon Schubert 	  CORE_ADDR maddr;
1635796c8dcSSimon Schubert 	  type = lookup_pointer_type (builtin_type (gdbarch)->builtin_char);
1645796c8dcSSimon Schubert 	  type = lookup_function_type (type);
1655796c8dcSSimon Schubert 	  type = lookup_pointer_type (type);
1665796c8dcSSimon Schubert 	  maddr = SYMBOL_VALUE_ADDRESS (msymbol);
1675796c8dcSSimon Schubert 
1685796c8dcSSimon Schubert 	  if (objf_p)
1695796c8dcSSimon Schubert 	    *objf_p = objfile;
1705796c8dcSSimon Schubert 
1715796c8dcSSimon Schubert 	  return value_from_pointer (type, maddr);
1725796c8dcSSimon Schubert 	}
1735796c8dcSSimon Schubert       else
1745796c8dcSSimon Schubert 	{
1755796c8dcSSimon Schubert 	  if (!target_has_execution)
176c50c785cSJohn Marino 	    error (_("evaluation of this expression "
177c50c785cSJohn Marino 		     "requires the target program to be active"));
1785796c8dcSSimon Schubert 	  else
179c50c785cSJohn Marino 	    error (_("evaluation of this expression requires the "
180c50c785cSJohn Marino 		     "program to have a function \"%s\"."),
181c50c785cSJohn Marino 		   name);
1825796c8dcSSimon Schubert 	}
1835796c8dcSSimon Schubert     }
1845796c8dcSSimon Schubert }
1855796c8dcSSimon Schubert 
1865796c8dcSSimon Schubert /* Allocate NBYTES of space in the inferior using the inferior's
1875796c8dcSSimon Schubert    malloc and return a value that is a pointer to the allocated
1885796c8dcSSimon Schubert    space.  */
1895796c8dcSSimon Schubert 
1905796c8dcSSimon Schubert struct value *
value_allocate_space_in_inferior(int len)1915796c8dcSSimon Schubert value_allocate_space_in_inferior (int len)
1925796c8dcSSimon Schubert {
1935796c8dcSSimon Schubert   struct objfile *objf;
1945796c8dcSSimon Schubert   struct value *val = find_function_in_inferior ("malloc", &objf);
1955796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_objfile_arch (objf);
1965796c8dcSSimon Schubert   struct value *blocklen;
1975796c8dcSSimon Schubert 
1985796c8dcSSimon Schubert   blocklen = value_from_longest (builtin_type (gdbarch)->builtin_int, len);
1995796c8dcSSimon Schubert   val = call_function_by_hand (val, 1, &blocklen);
2005796c8dcSSimon Schubert   if (value_logical_not (val))
2015796c8dcSSimon Schubert     {
2025796c8dcSSimon Schubert       if (!target_has_execution)
203c50c785cSJohn Marino 	error (_("No memory available to program now: "
204c50c785cSJohn Marino 		 "you need to start the target first"));
2055796c8dcSSimon Schubert       else
2065796c8dcSSimon Schubert 	error (_("No memory available to program: call to malloc failed"));
2075796c8dcSSimon Schubert     }
2085796c8dcSSimon Schubert   return val;
2095796c8dcSSimon Schubert }
2105796c8dcSSimon Schubert 
2115796c8dcSSimon Schubert static CORE_ADDR
allocate_space_in_inferior(int len)2125796c8dcSSimon Schubert allocate_space_in_inferior (int len)
2135796c8dcSSimon Schubert {
2145796c8dcSSimon Schubert   return value_as_long (value_allocate_space_in_inferior (len));
2155796c8dcSSimon Schubert }
2165796c8dcSSimon Schubert 
2175796c8dcSSimon Schubert /* Cast struct value VAL to type TYPE and return as a value.
2185796c8dcSSimon Schubert    Both type and val must be of TYPE_CODE_STRUCT or TYPE_CODE_UNION
2195796c8dcSSimon Schubert    for this to work.  Typedef to one of the codes is permitted.
2205796c8dcSSimon Schubert    Returns NULL if the cast is neither an upcast nor a downcast.  */
2215796c8dcSSimon Schubert 
2225796c8dcSSimon Schubert static struct value *
value_cast_structs(struct type * type,struct value * v2)2235796c8dcSSimon Schubert value_cast_structs (struct type *type, struct value *v2)
2245796c8dcSSimon Schubert {
2255796c8dcSSimon Schubert   struct type *t1;
2265796c8dcSSimon Schubert   struct type *t2;
2275796c8dcSSimon Schubert   struct value *v;
2285796c8dcSSimon Schubert 
2295796c8dcSSimon Schubert   gdb_assert (type != NULL && v2 != NULL);
2305796c8dcSSimon Schubert 
2315796c8dcSSimon Schubert   t1 = check_typedef (type);
2325796c8dcSSimon Schubert   t2 = check_typedef (value_type (v2));
2335796c8dcSSimon Schubert 
2345796c8dcSSimon Schubert   /* Check preconditions.  */
2355796c8dcSSimon Schubert   gdb_assert ((TYPE_CODE (t1) == TYPE_CODE_STRUCT
2365796c8dcSSimon Schubert 	       || TYPE_CODE (t1) == TYPE_CODE_UNION)
2375796c8dcSSimon Schubert 	      && !!"Precondition is that type is of STRUCT or UNION kind.");
2385796c8dcSSimon Schubert   gdb_assert ((TYPE_CODE (t2) == TYPE_CODE_STRUCT
2395796c8dcSSimon Schubert 	       || TYPE_CODE (t2) == TYPE_CODE_UNION)
2405796c8dcSSimon Schubert 	      && !!"Precondition is that value is of STRUCT or UNION kind");
2415796c8dcSSimon Schubert 
242cf7f2e2dSJohn Marino   if (TYPE_NAME (t1) != NULL
243cf7f2e2dSJohn Marino       && TYPE_NAME (t2) != NULL
244cf7f2e2dSJohn Marino       && !strcmp (TYPE_NAME (t1), TYPE_NAME (t2)))
245cf7f2e2dSJohn Marino     return NULL;
246cf7f2e2dSJohn Marino 
2475796c8dcSSimon Schubert   /* Upcasting: look in the type of the source to see if it contains the
2485796c8dcSSimon Schubert      type of the target as a superclass.  If so, we'll need to
2495796c8dcSSimon Schubert      offset the pointer rather than just change its type.  */
2505796c8dcSSimon Schubert   if (TYPE_NAME (t1) != NULL)
2515796c8dcSSimon Schubert     {
2525796c8dcSSimon Schubert       v = search_struct_field (type_name_no_tag (t1),
2535796c8dcSSimon Schubert 			       v2, 0, t2, 1);
2545796c8dcSSimon Schubert       if (v)
2555796c8dcSSimon Schubert 	return v;
2565796c8dcSSimon Schubert     }
2575796c8dcSSimon Schubert 
2585796c8dcSSimon Schubert   /* Downcasting: look in the type of the target to see if it contains the
2595796c8dcSSimon Schubert      type of the source as a superclass.  If so, we'll need to
260cf7f2e2dSJohn Marino      offset the pointer rather than just change its type.  */
2615796c8dcSSimon Schubert   if (TYPE_NAME (t2) != NULL)
2625796c8dcSSimon Schubert     {
263cf7f2e2dSJohn Marino       /* Try downcasting using the run-time type of the value.  */
264cf7f2e2dSJohn Marino       int full, top, using_enc;
265cf7f2e2dSJohn Marino       struct type *real_type;
266cf7f2e2dSJohn Marino 
267cf7f2e2dSJohn Marino       real_type = value_rtti_type (v2, &full, &top, &using_enc);
268cf7f2e2dSJohn Marino       if (real_type)
269cf7f2e2dSJohn Marino 	{
270cf7f2e2dSJohn Marino 	  v = value_full_object (v2, real_type, full, top, using_enc);
271cf7f2e2dSJohn Marino 	  v = value_at_lazy (real_type, value_address (v));
272cf7f2e2dSJohn Marino 
273cf7f2e2dSJohn Marino 	  /* We might be trying to cast to the outermost enclosing
274cf7f2e2dSJohn Marino 	     type, in which case search_struct_field won't work.  */
275cf7f2e2dSJohn Marino 	  if (TYPE_NAME (real_type) != NULL
276cf7f2e2dSJohn Marino 	      && !strcmp (TYPE_NAME (real_type), TYPE_NAME (t1)))
277cf7f2e2dSJohn Marino 	    return v;
278cf7f2e2dSJohn Marino 
279cf7f2e2dSJohn Marino 	  v = search_struct_field (type_name_no_tag (t2), v, 0, real_type, 1);
280cf7f2e2dSJohn Marino 	  if (v)
281cf7f2e2dSJohn Marino 	    return v;
282cf7f2e2dSJohn Marino 	}
283cf7f2e2dSJohn Marino 
284cf7f2e2dSJohn Marino       /* Try downcasting using information from the destination type
285cf7f2e2dSJohn Marino 	 T2.  This wouldn't work properly for classes with virtual
286cf7f2e2dSJohn Marino 	 bases, but those were handled above.  */
2875796c8dcSSimon Schubert       v = search_struct_field (type_name_no_tag (t2),
2885796c8dcSSimon Schubert 			       value_zero (t1, not_lval), 0, t1, 1);
2895796c8dcSSimon Schubert       if (v)
2905796c8dcSSimon Schubert 	{
2915796c8dcSSimon Schubert 	  /* Downcasting is possible (t1 is superclass of v2).  */
2925796c8dcSSimon Schubert 	  CORE_ADDR addr2 = value_address (v2);
293cf7f2e2dSJohn Marino 
2945796c8dcSSimon Schubert 	  addr2 -= value_address (v) + value_embedded_offset (v);
2955796c8dcSSimon Schubert 	  return value_at (type, addr2);
2965796c8dcSSimon Schubert 	}
2975796c8dcSSimon Schubert     }
2985796c8dcSSimon Schubert 
2995796c8dcSSimon Schubert   return NULL;
3005796c8dcSSimon Schubert }
3015796c8dcSSimon Schubert 
3025796c8dcSSimon Schubert /* Cast one pointer or reference type to another.  Both TYPE and
3035796c8dcSSimon Schubert    the type of ARG2 should be pointer types, or else both should be
304*ef5ccd6cSJohn Marino    reference types.  If SUBCLASS_CHECK is non-zero, this will force a
305*ef5ccd6cSJohn Marino    check to see whether TYPE is a superclass of ARG2's type.  If
306*ef5ccd6cSJohn Marino    SUBCLASS_CHECK is zero, then the subclass check is done only when
307*ef5ccd6cSJohn Marino    ARG2 is itself non-zero.  Returns the new pointer or reference.  */
3085796c8dcSSimon Schubert 
3095796c8dcSSimon Schubert struct value *
value_cast_pointers(struct type * type,struct value * arg2,int subclass_check)310*ef5ccd6cSJohn Marino value_cast_pointers (struct type *type, struct value *arg2,
311*ef5ccd6cSJohn Marino 		     int subclass_check)
3125796c8dcSSimon Schubert {
3135796c8dcSSimon Schubert   struct type *type1 = check_typedef (type);
3145796c8dcSSimon Schubert   struct type *type2 = check_typedef (value_type (arg2));
315cf7f2e2dSJohn Marino   struct type *t1 = check_typedef (TYPE_TARGET_TYPE (type1));
3165796c8dcSSimon Schubert   struct type *t2 = check_typedef (TYPE_TARGET_TYPE (type2));
3175796c8dcSSimon Schubert 
3185796c8dcSSimon Schubert   if (TYPE_CODE (t1) == TYPE_CODE_STRUCT
3195796c8dcSSimon Schubert       && TYPE_CODE (t2) == TYPE_CODE_STRUCT
320*ef5ccd6cSJohn Marino       && (subclass_check || !value_logical_not (arg2)))
3215796c8dcSSimon Schubert     {
3225796c8dcSSimon Schubert       struct value *v2;
3235796c8dcSSimon Schubert 
3245796c8dcSSimon Schubert       if (TYPE_CODE (type2) == TYPE_CODE_REF)
3255796c8dcSSimon Schubert 	v2 = coerce_ref (arg2);
3265796c8dcSSimon Schubert       else
3275796c8dcSSimon Schubert 	v2 = value_ind (arg2);
328c50c785cSJohn Marino       gdb_assert (TYPE_CODE (check_typedef (value_type (v2)))
329c50c785cSJohn Marino 		  == TYPE_CODE_STRUCT && !!"Why did coercion fail?");
3305796c8dcSSimon Schubert       v2 = value_cast_structs (t1, v2);
3315796c8dcSSimon Schubert       /* At this point we have what we can have, un-dereference if needed.  */
3325796c8dcSSimon Schubert       if (v2)
3335796c8dcSSimon Schubert 	{
3345796c8dcSSimon Schubert 	  struct value *v = value_addr (v2);
335cf7f2e2dSJohn Marino 
3365796c8dcSSimon Schubert 	  deprecated_set_value_type (v, type);
3375796c8dcSSimon Schubert 	  return v;
3385796c8dcSSimon Schubert 	}
3395796c8dcSSimon Schubert    }
3405796c8dcSSimon Schubert 
3415796c8dcSSimon Schubert   /* No superclass found, just change the pointer type.  */
3425796c8dcSSimon Schubert   arg2 = value_copy (arg2);
3435796c8dcSSimon Schubert   deprecated_set_value_type (arg2, type);
344c50c785cSJohn Marino   set_value_enclosing_type (arg2, type);
3455796c8dcSSimon Schubert   set_value_pointed_to_offset (arg2, 0);	/* pai: chk_val */
3465796c8dcSSimon Schubert   return arg2;
3475796c8dcSSimon Schubert }
3485796c8dcSSimon Schubert 
3495796c8dcSSimon Schubert /* Cast value ARG2 to type TYPE and return as a value.
3505796c8dcSSimon Schubert    More general than a C cast: accepts any two types of the same length,
3515796c8dcSSimon Schubert    and if ARG2 is an lvalue it can be cast into anything at all.  */
3525796c8dcSSimon Schubert /* In C++, casts may change pointer or object representations.  */
3535796c8dcSSimon Schubert 
3545796c8dcSSimon Schubert struct value *
value_cast(struct type * type,struct value * arg2)3555796c8dcSSimon Schubert value_cast (struct type *type, struct value *arg2)
3565796c8dcSSimon Schubert {
3575796c8dcSSimon Schubert   enum type_code code1;
3585796c8dcSSimon Schubert   enum type_code code2;
3595796c8dcSSimon Schubert   int scalar;
3605796c8dcSSimon Schubert   struct type *type2;
3615796c8dcSSimon Schubert 
3625796c8dcSSimon Schubert   int convert_to_boolean = 0;
3635796c8dcSSimon Schubert 
3645796c8dcSSimon Schubert   if (value_type (arg2) == type)
3655796c8dcSSimon Schubert     return arg2;
3665796c8dcSSimon Schubert 
3675796c8dcSSimon Schubert   code1 = TYPE_CODE (check_typedef (type));
3685796c8dcSSimon Schubert 
3695796c8dcSSimon Schubert   /* Check if we are casting struct reference to struct reference.  */
3705796c8dcSSimon Schubert   if (code1 == TYPE_CODE_REF)
3715796c8dcSSimon Schubert     {
3725796c8dcSSimon Schubert       /* We dereference type; then we recurse and finally
3735796c8dcSSimon Schubert          we generate value of the given reference.  Nothing wrong with
3745796c8dcSSimon Schubert 	 that.  */
3755796c8dcSSimon Schubert       struct type *t1 = check_typedef (type);
3765796c8dcSSimon Schubert       struct type *dereftype = check_typedef (TYPE_TARGET_TYPE (t1));
3775796c8dcSSimon Schubert       struct value *val =  value_cast (dereftype, arg2);
378cf7f2e2dSJohn Marino 
3795796c8dcSSimon Schubert       return value_ref (val);
3805796c8dcSSimon Schubert     }
3815796c8dcSSimon Schubert 
3825796c8dcSSimon Schubert   code2 = TYPE_CODE (check_typedef (value_type (arg2)));
3835796c8dcSSimon Schubert 
3845796c8dcSSimon Schubert   if (code2 == TYPE_CODE_REF)
3855796c8dcSSimon Schubert     /* We deref the value and then do the cast.  */
3865796c8dcSSimon Schubert     return value_cast (type, coerce_ref (arg2));
3875796c8dcSSimon Schubert 
3885796c8dcSSimon Schubert   CHECK_TYPEDEF (type);
3895796c8dcSSimon Schubert   code1 = TYPE_CODE (type);
3905796c8dcSSimon Schubert   arg2 = coerce_ref (arg2);
3915796c8dcSSimon Schubert   type2 = check_typedef (value_type (arg2));
3925796c8dcSSimon Schubert 
3935796c8dcSSimon Schubert   /* You can't cast to a reference type.  See value_cast_pointers
3945796c8dcSSimon Schubert      instead.  */
3955796c8dcSSimon Schubert   gdb_assert (code1 != TYPE_CODE_REF);
3965796c8dcSSimon Schubert 
3975796c8dcSSimon Schubert   /* A cast to an undetermined-length array_type, such as
3985796c8dcSSimon Schubert      (TYPE [])OBJECT, is treated like a cast to (TYPE [N])OBJECT,
3995796c8dcSSimon Schubert      where N is sizeof(OBJECT)/sizeof(TYPE).  */
4005796c8dcSSimon Schubert   if (code1 == TYPE_CODE_ARRAY)
4015796c8dcSSimon Schubert     {
4025796c8dcSSimon Schubert       struct type *element_type = TYPE_TARGET_TYPE (type);
4035796c8dcSSimon Schubert       unsigned element_length = TYPE_LENGTH (check_typedef (element_type));
404cf7f2e2dSJohn Marino 
4055796c8dcSSimon Schubert       if (element_length > 0 && TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
4065796c8dcSSimon Schubert 	{
4075796c8dcSSimon Schubert 	  struct type *range_type = TYPE_INDEX_TYPE (type);
4085796c8dcSSimon Schubert 	  int val_length = TYPE_LENGTH (type2);
4095796c8dcSSimon Schubert 	  LONGEST low_bound, high_bound, new_length;
410cf7f2e2dSJohn Marino 
4115796c8dcSSimon Schubert 	  if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
4125796c8dcSSimon Schubert 	    low_bound = 0, high_bound = 0;
4135796c8dcSSimon Schubert 	  new_length = val_length / element_length;
4145796c8dcSSimon Schubert 	  if (val_length % element_length != 0)
415c50c785cSJohn Marino 	    warning (_("array element type size does not "
416c50c785cSJohn Marino 		       "divide object size in cast"));
4175796c8dcSSimon Schubert 	  /* FIXME-type-allocation: need a way to free this type when
4185796c8dcSSimon Schubert 	     we are done with it.  */
4195796c8dcSSimon Schubert 	  range_type = create_range_type ((struct type *) NULL,
4205796c8dcSSimon Schubert 					  TYPE_TARGET_TYPE (range_type),
4215796c8dcSSimon Schubert 					  low_bound,
4225796c8dcSSimon Schubert 					  new_length + low_bound - 1);
4235796c8dcSSimon Schubert 	  deprecated_set_value_type (arg2,
4245796c8dcSSimon Schubert 				     create_array_type ((struct type *) NULL,
4255796c8dcSSimon Schubert 							element_type,
4265796c8dcSSimon Schubert 							range_type));
4275796c8dcSSimon Schubert 	  return arg2;
4285796c8dcSSimon Schubert 	}
4295796c8dcSSimon Schubert     }
4305796c8dcSSimon Schubert 
4315796c8dcSSimon Schubert   if (current_language->c_style_arrays
432c50c785cSJohn Marino       && TYPE_CODE (type2) == TYPE_CODE_ARRAY
433c50c785cSJohn Marino       && !TYPE_VECTOR (type2))
4345796c8dcSSimon Schubert     arg2 = value_coerce_array (arg2);
4355796c8dcSSimon Schubert 
4365796c8dcSSimon Schubert   if (TYPE_CODE (type2) == TYPE_CODE_FUNC)
4375796c8dcSSimon Schubert     arg2 = value_coerce_function (arg2);
4385796c8dcSSimon Schubert 
4395796c8dcSSimon Schubert   type2 = check_typedef (value_type (arg2));
4405796c8dcSSimon Schubert   code2 = TYPE_CODE (type2);
4415796c8dcSSimon Schubert 
4425796c8dcSSimon Schubert   if (code1 == TYPE_CODE_COMPLEX)
4435796c8dcSSimon Schubert     return cast_into_complex (type, arg2);
4445796c8dcSSimon Schubert   if (code1 == TYPE_CODE_BOOL)
4455796c8dcSSimon Schubert     {
4465796c8dcSSimon Schubert       code1 = TYPE_CODE_INT;
4475796c8dcSSimon Schubert       convert_to_boolean = 1;
4485796c8dcSSimon Schubert     }
4495796c8dcSSimon Schubert   if (code1 == TYPE_CODE_CHAR)
4505796c8dcSSimon Schubert     code1 = TYPE_CODE_INT;
4515796c8dcSSimon Schubert   if (code2 == TYPE_CODE_BOOL || code2 == TYPE_CODE_CHAR)
4525796c8dcSSimon Schubert     code2 = TYPE_CODE_INT;
4535796c8dcSSimon Schubert 
4545796c8dcSSimon Schubert   scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
4555796c8dcSSimon Schubert 	    || code2 == TYPE_CODE_DECFLOAT || code2 == TYPE_CODE_ENUM
4565796c8dcSSimon Schubert 	    || code2 == TYPE_CODE_RANGE);
4575796c8dcSSimon Schubert 
4585796c8dcSSimon Schubert   if ((code1 == TYPE_CODE_STRUCT || code1 == TYPE_CODE_UNION)
4595796c8dcSSimon Schubert       && (code2 == TYPE_CODE_STRUCT || code2 == TYPE_CODE_UNION)
4605796c8dcSSimon Schubert       && TYPE_NAME (type) != 0)
4615796c8dcSSimon Schubert     {
4625796c8dcSSimon Schubert       struct value *v = value_cast_structs (type, arg2);
463cf7f2e2dSJohn Marino 
4645796c8dcSSimon Schubert       if (v)
4655796c8dcSSimon Schubert 	return v;
4665796c8dcSSimon Schubert     }
4675796c8dcSSimon Schubert 
4685796c8dcSSimon Schubert   if (code1 == TYPE_CODE_FLT && scalar)
4695796c8dcSSimon Schubert     return value_from_double (type, value_as_double (arg2));
4705796c8dcSSimon Schubert   else if (code1 == TYPE_CODE_DECFLOAT && scalar)
4715796c8dcSSimon Schubert     {
4725796c8dcSSimon Schubert       enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
4735796c8dcSSimon Schubert       int dec_len = TYPE_LENGTH (type);
4745796c8dcSSimon Schubert       gdb_byte dec[16];
4755796c8dcSSimon Schubert 
4765796c8dcSSimon Schubert       if (code2 == TYPE_CODE_FLT)
4775796c8dcSSimon Schubert 	decimal_from_floating (arg2, dec, dec_len, byte_order);
4785796c8dcSSimon Schubert       else if (code2 == TYPE_CODE_DECFLOAT)
4795796c8dcSSimon Schubert 	decimal_convert (value_contents (arg2), TYPE_LENGTH (type2),
4805796c8dcSSimon Schubert 			 byte_order, dec, dec_len, byte_order);
4815796c8dcSSimon Schubert       else
4825796c8dcSSimon Schubert 	/* The only option left is an integral type.  */
4835796c8dcSSimon Schubert 	decimal_from_integral (arg2, dec, dec_len, byte_order);
4845796c8dcSSimon Schubert 
4855796c8dcSSimon Schubert       return value_from_decfloat (type, dec);
4865796c8dcSSimon Schubert     }
4875796c8dcSSimon Schubert   else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM
4885796c8dcSSimon Schubert 	    || code1 == TYPE_CODE_RANGE)
4895796c8dcSSimon Schubert 	   && (scalar || code2 == TYPE_CODE_PTR
4905796c8dcSSimon Schubert 	       || code2 == TYPE_CODE_MEMBERPTR))
4915796c8dcSSimon Schubert     {
4925796c8dcSSimon Schubert       LONGEST longest;
4935796c8dcSSimon Schubert 
4945796c8dcSSimon Schubert       /* When we cast pointers to integers, we mustn't use
4955796c8dcSSimon Schubert          gdbarch_pointer_to_address to find the address the pointer
4965796c8dcSSimon Schubert          represents, as value_as_long would.  GDB should evaluate
4975796c8dcSSimon Schubert          expressions just as the compiler would --- and the compiler
4985796c8dcSSimon Schubert          sees a cast as a simple reinterpretation of the pointer's
4995796c8dcSSimon Schubert          bits.  */
5005796c8dcSSimon Schubert       if (code2 == TYPE_CODE_PTR)
5015796c8dcSSimon Schubert         longest = extract_unsigned_integer
5025796c8dcSSimon Schubert 		    (value_contents (arg2), TYPE_LENGTH (type2),
5035796c8dcSSimon Schubert 		     gdbarch_byte_order (get_type_arch (type2)));
5045796c8dcSSimon Schubert       else
5055796c8dcSSimon Schubert         longest = value_as_long (arg2);
5065796c8dcSSimon Schubert       return value_from_longest (type, convert_to_boolean ?
5075796c8dcSSimon Schubert 				 (LONGEST) (longest ? 1 : 0) : longest);
5085796c8dcSSimon Schubert     }
5095796c8dcSSimon Schubert   else if (code1 == TYPE_CODE_PTR && (code2 == TYPE_CODE_INT
5105796c8dcSSimon Schubert 				      || code2 == TYPE_CODE_ENUM
5115796c8dcSSimon Schubert 				      || code2 == TYPE_CODE_RANGE))
5125796c8dcSSimon Schubert     {
5135796c8dcSSimon Schubert       /* TYPE_LENGTH (type) is the length of a pointer, but we really
5145796c8dcSSimon Schubert 	 want the length of an address! -- we are really dealing with
5155796c8dcSSimon Schubert 	 addresses (i.e., gdb representations) not pointers (i.e.,
5165796c8dcSSimon Schubert 	 target representations) here.
5175796c8dcSSimon Schubert 
5185796c8dcSSimon Schubert 	 This allows things like "print *(int *)0x01000234" to work
5195796c8dcSSimon Schubert 	 without printing a misleading message -- which would
5205796c8dcSSimon Schubert 	 otherwise occur when dealing with a target having two byte
5215796c8dcSSimon Schubert 	 pointers and four byte addresses.  */
5225796c8dcSSimon Schubert 
5235796c8dcSSimon Schubert       int addr_bit = gdbarch_addr_bit (get_type_arch (type2));
5245796c8dcSSimon Schubert       LONGEST longest = value_as_long (arg2);
525cf7f2e2dSJohn Marino 
5265796c8dcSSimon Schubert       if (addr_bit < sizeof (LONGEST) * HOST_CHAR_BIT)
5275796c8dcSSimon Schubert 	{
5285796c8dcSSimon Schubert 	  if (longest >= ((LONGEST) 1 << addr_bit)
5295796c8dcSSimon Schubert 	      || longest <= -((LONGEST) 1 << addr_bit))
5305796c8dcSSimon Schubert 	    warning (_("value truncated"));
5315796c8dcSSimon Schubert 	}
5325796c8dcSSimon Schubert       return value_from_longest (type, longest);
5335796c8dcSSimon Schubert     }
5345796c8dcSSimon Schubert   else if (code1 == TYPE_CODE_METHODPTR && code2 == TYPE_CODE_INT
5355796c8dcSSimon Schubert 	   && value_as_long (arg2) == 0)
5365796c8dcSSimon Schubert     {
5375796c8dcSSimon Schubert       struct value *result = allocate_value (type);
538cf7f2e2dSJohn Marino 
5395796c8dcSSimon Schubert       cplus_make_method_ptr (type, value_contents_writeable (result), 0, 0);
5405796c8dcSSimon Schubert       return result;
5415796c8dcSSimon Schubert     }
5425796c8dcSSimon Schubert   else if (code1 == TYPE_CODE_MEMBERPTR && code2 == TYPE_CODE_INT
5435796c8dcSSimon Schubert 	   && value_as_long (arg2) == 0)
5445796c8dcSSimon Schubert     {
5455796c8dcSSimon Schubert       /* The Itanium C++ ABI represents NULL pointers to members as
5465796c8dcSSimon Schubert 	 minus one, instead of biasing the normal case.  */
5475796c8dcSSimon Schubert       return value_from_longest (type, -1);
5485796c8dcSSimon Schubert     }
549*ef5ccd6cSJohn Marino   else if (code1 == TYPE_CODE_ARRAY && TYPE_VECTOR (type)
550*ef5ccd6cSJohn Marino 	   && code2 == TYPE_CODE_ARRAY && TYPE_VECTOR (type2)
551*ef5ccd6cSJohn Marino 	   && TYPE_LENGTH (type) != TYPE_LENGTH (type2))
552*ef5ccd6cSJohn Marino     error (_("Cannot convert between vector values of different sizes"));
553*ef5ccd6cSJohn Marino   else if (code1 == TYPE_CODE_ARRAY && TYPE_VECTOR (type) && scalar
554*ef5ccd6cSJohn Marino 	   && TYPE_LENGTH (type) != TYPE_LENGTH (type2))
555*ef5ccd6cSJohn Marino     error (_("can only cast scalar to vector of same size"));
556*ef5ccd6cSJohn Marino   else if (code1 == TYPE_CODE_VOID)
557c50c785cSJohn Marino     {
558*ef5ccd6cSJohn Marino       return value_zero (type, not_lval);
559c50c785cSJohn Marino     }
5605796c8dcSSimon Schubert   else if (TYPE_LENGTH (type) == TYPE_LENGTH (type2))
5615796c8dcSSimon Schubert     {
5625796c8dcSSimon Schubert       if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
563*ef5ccd6cSJohn Marino 	return value_cast_pointers (type, arg2, 0);
5645796c8dcSSimon Schubert 
5655796c8dcSSimon Schubert       arg2 = value_copy (arg2);
5665796c8dcSSimon Schubert       deprecated_set_value_type (arg2, type);
567c50c785cSJohn Marino       set_value_enclosing_type (arg2, type);
5685796c8dcSSimon Schubert       set_value_pointed_to_offset (arg2, 0);	/* pai: chk_val */
5695796c8dcSSimon Schubert       return arg2;
5705796c8dcSSimon Schubert     }
5715796c8dcSSimon Schubert   else if (VALUE_LVAL (arg2) == lval_memory)
5725796c8dcSSimon Schubert     return value_at_lazy (type, value_address (arg2));
5735796c8dcSSimon Schubert   else
5745796c8dcSSimon Schubert     {
5755796c8dcSSimon Schubert       error (_("Invalid cast."));
5765796c8dcSSimon Schubert       return 0;
5775796c8dcSSimon Schubert     }
5785796c8dcSSimon Schubert }
5795796c8dcSSimon Schubert 
580cf7f2e2dSJohn Marino /* The C++ reinterpret_cast operator.  */
581cf7f2e2dSJohn Marino 
582cf7f2e2dSJohn Marino struct value *
value_reinterpret_cast(struct type * type,struct value * arg)583cf7f2e2dSJohn Marino value_reinterpret_cast (struct type *type, struct value *arg)
584cf7f2e2dSJohn Marino {
585cf7f2e2dSJohn Marino   struct value *result;
586cf7f2e2dSJohn Marino   struct type *real_type = check_typedef (type);
587cf7f2e2dSJohn Marino   struct type *arg_type, *dest_type;
588cf7f2e2dSJohn Marino   int is_ref = 0;
589cf7f2e2dSJohn Marino   enum type_code dest_code, arg_code;
590cf7f2e2dSJohn Marino 
591cf7f2e2dSJohn Marino   /* Do reference, function, and array conversion.  */
592cf7f2e2dSJohn Marino   arg = coerce_array (arg);
593cf7f2e2dSJohn Marino 
594cf7f2e2dSJohn Marino   /* Attempt to preserve the type the user asked for.  */
595cf7f2e2dSJohn Marino   dest_type = type;
596cf7f2e2dSJohn Marino 
597cf7f2e2dSJohn Marino   /* If we are casting to a reference type, transform
598cf7f2e2dSJohn Marino      reinterpret_cast<T&>(V) to *reinterpret_cast<T*>(&V).  */
599cf7f2e2dSJohn Marino   if (TYPE_CODE (real_type) == TYPE_CODE_REF)
600cf7f2e2dSJohn Marino     {
601cf7f2e2dSJohn Marino       is_ref = 1;
602cf7f2e2dSJohn Marino       arg = value_addr (arg);
603cf7f2e2dSJohn Marino       dest_type = lookup_pointer_type (TYPE_TARGET_TYPE (dest_type));
604cf7f2e2dSJohn Marino       real_type = lookup_pointer_type (real_type);
605cf7f2e2dSJohn Marino     }
606cf7f2e2dSJohn Marino 
607cf7f2e2dSJohn Marino   arg_type = value_type (arg);
608cf7f2e2dSJohn Marino 
609cf7f2e2dSJohn Marino   dest_code = TYPE_CODE (real_type);
610cf7f2e2dSJohn Marino   arg_code = TYPE_CODE (arg_type);
611cf7f2e2dSJohn Marino 
612cf7f2e2dSJohn Marino   /* We can convert pointer types, or any pointer type to int, or int
613cf7f2e2dSJohn Marino      type to pointer.  */
614cf7f2e2dSJohn Marino   if ((dest_code == TYPE_CODE_PTR && arg_code == TYPE_CODE_INT)
615cf7f2e2dSJohn Marino       || (dest_code == TYPE_CODE_INT && arg_code == TYPE_CODE_PTR)
616cf7f2e2dSJohn Marino       || (dest_code == TYPE_CODE_METHODPTR && arg_code == TYPE_CODE_INT)
617cf7f2e2dSJohn Marino       || (dest_code == TYPE_CODE_INT && arg_code == TYPE_CODE_METHODPTR)
618cf7f2e2dSJohn Marino       || (dest_code == TYPE_CODE_MEMBERPTR && arg_code == TYPE_CODE_INT)
619cf7f2e2dSJohn Marino       || (dest_code == TYPE_CODE_INT && arg_code == TYPE_CODE_MEMBERPTR)
620cf7f2e2dSJohn Marino       || (dest_code == arg_code
621cf7f2e2dSJohn Marino 	  && (dest_code == TYPE_CODE_PTR
622cf7f2e2dSJohn Marino 	      || dest_code == TYPE_CODE_METHODPTR
623cf7f2e2dSJohn Marino 	      || dest_code == TYPE_CODE_MEMBERPTR)))
624cf7f2e2dSJohn Marino     result = value_cast (dest_type, arg);
625cf7f2e2dSJohn Marino   else
626cf7f2e2dSJohn Marino     error (_("Invalid reinterpret_cast"));
627cf7f2e2dSJohn Marino 
628cf7f2e2dSJohn Marino   if (is_ref)
629cf7f2e2dSJohn Marino     result = value_cast (type, value_ref (value_ind (result)));
630cf7f2e2dSJohn Marino 
631cf7f2e2dSJohn Marino   return result;
632cf7f2e2dSJohn Marino }
633cf7f2e2dSJohn Marino 
634cf7f2e2dSJohn Marino /* A helper for value_dynamic_cast.  This implements the first of two
635cf7f2e2dSJohn Marino    runtime checks: we iterate over all the base classes of the value's
636cf7f2e2dSJohn Marino    class which are equal to the desired class; if only one of these
637cf7f2e2dSJohn Marino    holds the value, then it is the answer.  */
638cf7f2e2dSJohn Marino 
639cf7f2e2dSJohn Marino static int
dynamic_cast_check_1(struct type * desired_type,const gdb_byte * valaddr,int embedded_offset,CORE_ADDR address,struct value * val,struct type * search_type,CORE_ADDR arg_addr,struct type * arg_type,struct value ** result)640cf7f2e2dSJohn Marino dynamic_cast_check_1 (struct type *desired_type,
641c50c785cSJohn Marino 		      const gdb_byte *valaddr,
642c50c785cSJohn Marino 		      int embedded_offset,
643cf7f2e2dSJohn Marino 		      CORE_ADDR address,
644c50c785cSJohn Marino 		      struct value *val,
645cf7f2e2dSJohn Marino 		      struct type *search_type,
646cf7f2e2dSJohn Marino 		      CORE_ADDR arg_addr,
647cf7f2e2dSJohn Marino 		      struct type *arg_type,
648cf7f2e2dSJohn Marino 		      struct value **result)
649cf7f2e2dSJohn Marino {
650cf7f2e2dSJohn Marino   int i, result_count = 0;
651cf7f2e2dSJohn Marino 
652cf7f2e2dSJohn Marino   for (i = 0; i < TYPE_N_BASECLASSES (search_type) && result_count < 2; ++i)
653cf7f2e2dSJohn Marino     {
654c50c785cSJohn Marino       int offset = baseclass_offset (search_type, i, valaddr, embedded_offset,
655c50c785cSJohn Marino 				     address, val);
656cf7f2e2dSJohn Marino 
657cf7f2e2dSJohn Marino       if (class_types_same_p (desired_type, TYPE_BASECLASS (search_type, i)))
658cf7f2e2dSJohn Marino 	{
659c50c785cSJohn Marino 	  if (address + embedded_offset + offset >= arg_addr
660c50c785cSJohn Marino 	      && address + embedded_offset + offset < arg_addr + TYPE_LENGTH (arg_type))
661cf7f2e2dSJohn Marino 	    {
662cf7f2e2dSJohn Marino 	      ++result_count;
663cf7f2e2dSJohn Marino 	      if (!*result)
664cf7f2e2dSJohn Marino 		*result = value_at_lazy (TYPE_BASECLASS (search_type, i),
665c50c785cSJohn Marino 					 address + embedded_offset + offset);
666cf7f2e2dSJohn Marino 	    }
667cf7f2e2dSJohn Marino 	}
668cf7f2e2dSJohn Marino       else
669cf7f2e2dSJohn Marino 	result_count += dynamic_cast_check_1 (desired_type,
670c50c785cSJohn Marino 					      valaddr,
671c50c785cSJohn Marino 					      embedded_offset + offset,
672c50c785cSJohn Marino 					      address, val,
673cf7f2e2dSJohn Marino 					      TYPE_BASECLASS (search_type, i),
674cf7f2e2dSJohn Marino 					      arg_addr,
675cf7f2e2dSJohn Marino 					      arg_type,
676cf7f2e2dSJohn Marino 					      result);
677cf7f2e2dSJohn Marino     }
678cf7f2e2dSJohn Marino 
679cf7f2e2dSJohn Marino   return result_count;
680cf7f2e2dSJohn Marino }
681cf7f2e2dSJohn Marino 
682cf7f2e2dSJohn Marino /* A helper for value_dynamic_cast.  This implements the second of two
683cf7f2e2dSJohn Marino    runtime checks: we look for a unique public sibling class of the
684cf7f2e2dSJohn Marino    argument's declared class.  */
685cf7f2e2dSJohn Marino 
686cf7f2e2dSJohn Marino static int
dynamic_cast_check_2(struct type * desired_type,const gdb_byte * valaddr,int embedded_offset,CORE_ADDR address,struct value * val,struct type * search_type,struct value ** result)687cf7f2e2dSJohn Marino dynamic_cast_check_2 (struct type *desired_type,
688c50c785cSJohn Marino 		      const gdb_byte *valaddr,
689c50c785cSJohn Marino 		      int embedded_offset,
690cf7f2e2dSJohn Marino 		      CORE_ADDR address,
691c50c785cSJohn Marino 		      struct value *val,
692cf7f2e2dSJohn Marino 		      struct type *search_type,
693cf7f2e2dSJohn Marino 		      struct value **result)
694cf7f2e2dSJohn Marino {
695cf7f2e2dSJohn Marino   int i, result_count = 0;
696cf7f2e2dSJohn Marino 
697cf7f2e2dSJohn Marino   for (i = 0; i < TYPE_N_BASECLASSES (search_type) && result_count < 2; ++i)
698cf7f2e2dSJohn Marino     {
699cf7f2e2dSJohn Marino       int offset;
700cf7f2e2dSJohn Marino 
701cf7f2e2dSJohn Marino       if (! BASETYPE_VIA_PUBLIC (search_type, i))
702cf7f2e2dSJohn Marino 	continue;
703cf7f2e2dSJohn Marino 
704c50c785cSJohn Marino       offset = baseclass_offset (search_type, i, valaddr, embedded_offset,
705c50c785cSJohn Marino 				 address, val);
706cf7f2e2dSJohn Marino       if (class_types_same_p (desired_type, TYPE_BASECLASS (search_type, i)))
707cf7f2e2dSJohn Marino 	{
708cf7f2e2dSJohn Marino 	  ++result_count;
709cf7f2e2dSJohn Marino 	  if (*result == NULL)
710cf7f2e2dSJohn Marino 	    *result = value_at_lazy (TYPE_BASECLASS (search_type, i),
711c50c785cSJohn Marino 				     address + embedded_offset + offset);
712cf7f2e2dSJohn Marino 	}
713cf7f2e2dSJohn Marino       else
714cf7f2e2dSJohn Marino 	result_count += dynamic_cast_check_2 (desired_type,
715c50c785cSJohn Marino 					      valaddr,
716c50c785cSJohn Marino 					      embedded_offset + offset,
717c50c785cSJohn Marino 					      address, val,
718cf7f2e2dSJohn Marino 					      TYPE_BASECLASS (search_type, i),
719cf7f2e2dSJohn Marino 					      result);
720cf7f2e2dSJohn Marino     }
721cf7f2e2dSJohn Marino 
722cf7f2e2dSJohn Marino   return result_count;
723cf7f2e2dSJohn Marino }
724cf7f2e2dSJohn Marino 
725cf7f2e2dSJohn Marino /* The C++ dynamic_cast operator.  */
726cf7f2e2dSJohn Marino 
727cf7f2e2dSJohn Marino struct value *
value_dynamic_cast(struct type * type,struct value * arg)728cf7f2e2dSJohn Marino value_dynamic_cast (struct type *type, struct value *arg)
729cf7f2e2dSJohn Marino {
730cf7f2e2dSJohn Marino   int full, top, using_enc;
731cf7f2e2dSJohn Marino   struct type *resolved_type = check_typedef (type);
732cf7f2e2dSJohn Marino   struct type *arg_type = check_typedef (value_type (arg));
733cf7f2e2dSJohn Marino   struct type *class_type, *rtti_type;
734cf7f2e2dSJohn Marino   struct value *result, *tem, *original_arg = arg;
735cf7f2e2dSJohn Marino   CORE_ADDR addr;
736cf7f2e2dSJohn Marino   int is_ref = TYPE_CODE (resolved_type) == TYPE_CODE_REF;
737cf7f2e2dSJohn Marino 
738cf7f2e2dSJohn Marino   if (TYPE_CODE (resolved_type) != TYPE_CODE_PTR
739cf7f2e2dSJohn Marino       && TYPE_CODE (resolved_type) != TYPE_CODE_REF)
740cf7f2e2dSJohn Marino     error (_("Argument to dynamic_cast must be a pointer or reference type"));
741cf7f2e2dSJohn Marino   if (TYPE_CODE (TYPE_TARGET_TYPE (resolved_type)) != TYPE_CODE_VOID
742cf7f2e2dSJohn Marino       && TYPE_CODE (TYPE_TARGET_TYPE (resolved_type)) != TYPE_CODE_CLASS)
743cf7f2e2dSJohn Marino     error (_("Argument to dynamic_cast must be pointer to class or `void *'"));
744cf7f2e2dSJohn Marino 
745cf7f2e2dSJohn Marino   class_type = check_typedef (TYPE_TARGET_TYPE (resolved_type));
746cf7f2e2dSJohn Marino   if (TYPE_CODE (resolved_type) == TYPE_CODE_PTR)
747cf7f2e2dSJohn Marino     {
748cf7f2e2dSJohn Marino       if (TYPE_CODE (arg_type) != TYPE_CODE_PTR
749cf7f2e2dSJohn Marino 	  && ! (TYPE_CODE (arg_type) == TYPE_CODE_INT
750cf7f2e2dSJohn Marino 		&& value_as_long (arg) == 0))
751cf7f2e2dSJohn Marino 	error (_("Argument to dynamic_cast does not have pointer type"));
752cf7f2e2dSJohn Marino       if (TYPE_CODE (arg_type) == TYPE_CODE_PTR)
753cf7f2e2dSJohn Marino 	{
754cf7f2e2dSJohn Marino 	  arg_type = check_typedef (TYPE_TARGET_TYPE (arg_type));
755cf7f2e2dSJohn Marino 	  if (TYPE_CODE (arg_type) != TYPE_CODE_CLASS)
756c50c785cSJohn Marino 	    error (_("Argument to dynamic_cast does "
757c50c785cSJohn Marino 		     "not have pointer to class type"));
758cf7f2e2dSJohn Marino 	}
759cf7f2e2dSJohn Marino 
760cf7f2e2dSJohn Marino       /* Handle NULL pointers.  */
761cf7f2e2dSJohn Marino       if (value_as_long (arg) == 0)
762cf7f2e2dSJohn Marino 	return value_zero (type, not_lval);
763cf7f2e2dSJohn Marino 
764cf7f2e2dSJohn Marino       arg = value_ind (arg);
765cf7f2e2dSJohn Marino     }
766cf7f2e2dSJohn Marino   else
767cf7f2e2dSJohn Marino     {
768cf7f2e2dSJohn Marino       if (TYPE_CODE (arg_type) != TYPE_CODE_CLASS)
769cf7f2e2dSJohn Marino 	error (_("Argument to dynamic_cast does not have class type"));
770cf7f2e2dSJohn Marino     }
771cf7f2e2dSJohn Marino 
772cf7f2e2dSJohn Marino   /* If the classes are the same, just return the argument.  */
773cf7f2e2dSJohn Marino   if (class_types_same_p (class_type, arg_type))
774cf7f2e2dSJohn Marino     return value_cast (type, arg);
775cf7f2e2dSJohn Marino 
776cf7f2e2dSJohn Marino   /* If the target type is a unique base class of the argument's
777cf7f2e2dSJohn Marino      declared type, just cast it.  */
778cf7f2e2dSJohn Marino   if (is_ancestor (class_type, arg_type))
779cf7f2e2dSJohn Marino     {
780cf7f2e2dSJohn Marino       if (is_unique_ancestor (class_type, arg))
781cf7f2e2dSJohn Marino 	return value_cast (type, original_arg);
782cf7f2e2dSJohn Marino       error (_("Ambiguous dynamic_cast"));
783cf7f2e2dSJohn Marino     }
784cf7f2e2dSJohn Marino 
785cf7f2e2dSJohn Marino   rtti_type = value_rtti_type (arg, &full, &top, &using_enc);
786cf7f2e2dSJohn Marino   if (! rtti_type)
787cf7f2e2dSJohn Marino     error (_("Couldn't determine value's most derived type for dynamic_cast"));
788cf7f2e2dSJohn Marino 
789cf7f2e2dSJohn Marino   /* Compute the most derived object's address.  */
790cf7f2e2dSJohn Marino   addr = value_address (arg);
791cf7f2e2dSJohn Marino   if (full)
792cf7f2e2dSJohn Marino     {
793cf7f2e2dSJohn Marino       /* Done.  */
794cf7f2e2dSJohn Marino     }
795cf7f2e2dSJohn Marino   else if (using_enc)
796cf7f2e2dSJohn Marino     addr += top;
797cf7f2e2dSJohn Marino   else
798cf7f2e2dSJohn Marino     addr += top + value_embedded_offset (arg);
799cf7f2e2dSJohn Marino 
800cf7f2e2dSJohn Marino   /* dynamic_cast<void *> means to return a pointer to the
801cf7f2e2dSJohn Marino      most-derived object.  */
802cf7f2e2dSJohn Marino   if (TYPE_CODE (resolved_type) == TYPE_CODE_PTR
803cf7f2e2dSJohn Marino       && TYPE_CODE (TYPE_TARGET_TYPE (resolved_type)) == TYPE_CODE_VOID)
804cf7f2e2dSJohn Marino     return value_at_lazy (type, addr);
805cf7f2e2dSJohn Marino 
806cf7f2e2dSJohn Marino   tem = value_at (type, addr);
807cf7f2e2dSJohn Marino 
808cf7f2e2dSJohn Marino   /* The first dynamic check specified in 5.2.7.  */
809cf7f2e2dSJohn Marino   if (is_public_ancestor (arg_type, TYPE_TARGET_TYPE (resolved_type)))
810cf7f2e2dSJohn Marino     {
811cf7f2e2dSJohn Marino       if (class_types_same_p (rtti_type, TYPE_TARGET_TYPE (resolved_type)))
812cf7f2e2dSJohn Marino 	return tem;
813cf7f2e2dSJohn Marino       result = NULL;
814cf7f2e2dSJohn Marino       if (dynamic_cast_check_1 (TYPE_TARGET_TYPE (resolved_type),
815c50c785cSJohn Marino 				value_contents_for_printing (tem),
816c50c785cSJohn Marino 				value_embedded_offset (tem),
817c50c785cSJohn Marino 				value_address (tem), tem,
818cf7f2e2dSJohn Marino 				rtti_type, addr,
819cf7f2e2dSJohn Marino 				arg_type,
820cf7f2e2dSJohn Marino 				&result) == 1)
821cf7f2e2dSJohn Marino 	return value_cast (type,
822cf7f2e2dSJohn Marino 			   is_ref ? value_ref (result) : value_addr (result));
823cf7f2e2dSJohn Marino     }
824cf7f2e2dSJohn Marino 
825cf7f2e2dSJohn Marino   /* The second dynamic check specified in 5.2.7.  */
826cf7f2e2dSJohn Marino   result = NULL;
827cf7f2e2dSJohn Marino   if (is_public_ancestor (arg_type, rtti_type)
828cf7f2e2dSJohn Marino       && dynamic_cast_check_2 (TYPE_TARGET_TYPE (resolved_type),
829c50c785cSJohn Marino 			       value_contents_for_printing (tem),
830c50c785cSJohn Marino 			       value_embedded_offset (tem),
831c50c785cSJohn Marino 			       value_address (tem), tem,
832cf7f2e2dSJohn Marino 			       rtti_type, &result) == 1)
833cf7f2e2dSJohn Marino     return value_cast (type,
834cf7f2e2dSJohn Marino 		       is_ref ? value_ref (result) : value_addr (result));
835cf7f2e2dSJohn Marino 
836cf7f2e2dSJohn Marino   if (TYPE_CODE (resolved_type) == TYPE_CODE_PTR)
837cf7f2e2dSJohn Marino     return value_zero (type, not_lval);
838cf7f2e2dSJohn Marino 
839cf7f2e2dSJohn Marino   error (_("dynamic_cast failed"));
840cf7f2e2dSJohn Marino }
841cf7f2e2dSJohn Marino 
8425796c8dcSSimon Schubert /* Create a value of type TYPE that is zero, and return it.  */
8435796c8dcSSimon Schubert 
8445796c8dcSSimon Schubert struct value *
value_zero(struct type * type,enum lval_type lv)8455796c8dcSSimon Schubert value_zero (struct type *type, enum lval_type lv)
8465796c8dcSSimon Schubert {
8475796c8dcSSimon Schubert   struct value *val = allocate_value (type);
8485796c8dcSSimon Schubert 
849a45ae5f8SJohn Marino   VALUE_LVAL (val) = (lv == lval_computed ? not_lval : lv);
8505796c8dcSSimon Schubert   return val;
8515796c8dcSSimon Schubert }
8525796c8dcSSimon Schubert 
853a45ae5f8SJohn Marino /* Create a not_lval value of numeric type TYPE that is one, and return it.  */
8545796c8dcSSimon Schubert 
8555796c8dcSSimon Schubert struct value *
value_one(struct type * type)856a45ae5f8SJohn Marino value_one (struct type *type)
8575796c8dcSSimon Schubert {
8585796c8dcSSimon Schubert   struct type *type1 = check_typedef (type);
8595796c8dcSSimon Schubert   struct value *val;
8605796c8dcSSimon Schubert 
8615796c8dcSSimon Schubert   if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
8625796c8dcSSimon Schubert     {
8635796c8dcSSimon Schubert       enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
8645796c8dcSSimon Schubert       gdb_byte v[16];
865cf7f2e2dSJohn Marino 
8665796c8dcSSimon Schubert       decimal_from_string (v, TYPE_LENGTH (type), byte_order, "1");
8675796c8dcSSimon Schubert       val = value_from_decfloat (type, v);
8685796c8dcSSimon Schubert     }
8695796c8dcSSimon Schubert   else if (TYPE_CODE (type1) == TYPE_CODE_FLT)
8705796c8dcSSimon Schubert     {
8715796c8dcSSimon Schubert       val = value_from_double (type, (DOUBLEST) 1);
8725796c8dcSSimon Schubert     }
8735796c8dcSSimon Schubert   else if (is_integral_type (type1))
8745796c8dcSSimon Schubert     {
8755796c8dcSSimon Schubert       val = value_from_longest (type, (LONGEST) 1);
8765796c8dcSSimon Schubert     }
877c50c785cSJohn Marino   else if (TYPE_CODE (type1) == TYPE_CODE_ARRAY && TYPE_VECTOR (type1))
878c50c785cSJohn Marino     {
879c50c785cSJohn Marino       struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type1));
880c50c785cSJohn Marino       int i;
881c50c785cSJohn Marino       LONGEST low_bound, high_bound;
882c50c785cSJohn Marino       struct value *tmp;
883c50c785cSJohn Marino 
884c50c785cSJohn Marino       if (!get_array_bounds (type1, &low_bound, &high_bound))
885c50c785cSJohn Marino 	error (_("Could not determine the vector bounds"));
886c50c785cSJohn Marino 
887c50c785cSJohn Marino       val = allocate_value (type);
888c50c785cSJohn Marino       for (i = 0; i < high_bound - low_bound + 1; i++)
889c50c785cSJohn Marino 	{
890a45ae5f8SJohn Marino 	  tmp = value_one (eltype);
891c50c785cSJohn Marino 	  memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype),
892c50c785cSJohn Marino 		  value_contents_all (tmp), TYPE_LENGTH (eltype));
893c50c785cSJohn Marino 	}
894c50c785cSJohn Marino     }
8955796c8dcSSimon Schubert   else
8965796c8dcSSimon Schubert     {
8975796c8dcSSimon Schubert       error (_("Not a numeric type."));
8985796c8dcSSimon Schubert     }
8995796c8dcSSimon Schubert 
900a45ae5f8SJohn Marino   /* value_one result is never used for assignments to.  */
901a45ae5f8SJohn Marino   gdb_assert (VALUE_LVAL (val) == not_lval);
902a45ae5f8SJohn Marino 
9035796c8dcSSimon Schubert   return val;
9045796c8dcSSimon Schubert }
9055796c8dcSSimon Schubert 
9065796c8dcSSimon Schubert /* Helper function for value_at, value_at_lazy, and value_at_lazy_stack.  */
9075796c8dcSSimon Schubert 
9085796c8dcSSimon Schubert static struct value *
get_value_at(struct type * type,CORE_ADDR addr,int lazy)9095796c8dcSSimon Schubert get_value_at (struct type *type, CORE_ADDR addr, int lazy)
9105796c8dcSSimon Schubert {
9115796c8dcSSimon Schubert   struct value *val;
9125796c8dcSSimon Schubert 
9135796c8dcSSimon Schubert   if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
9145796c8dcSSimon Schubert     error (_("Attempt to dereference a generic pointer."));
9155796c8dcSSimon Schubert 
916c50c785cSJohn Marino   val = value_from_contents_and_address (type, NULL, addr);
9175796c8dcSSimon Schubert 
918c50c785cSJohn Marino   if (!lazy)
919c50c785cSJohn Marino     value_fetch_lazy (val);
9205796c8dcSSimon Schubert 
9215796c8dcSSimon Schubert   return val;
9225796c8dcSSimon Schubert }
9235796c8dcSSimon Schubert 
9245796c8dcSSimon Schubert /* Return a value with type TYPE located at ADDR.
9255796c8dcSSimon Schubert 
9265796c8dcSSimon Schubert    Call value_at only if the data needs to be fetched immediately;
9275796c8dcSSimon Schubert    if we can be 'lazy' and defer the fetch, perhaps indefinately, call
9285796c8dcSSimon Schubert    value_at_lazy instead.  value_at_lazy simply records the address of
9295796c8dcSSimon Schubert    the data and sets the lazy-evaluation-required flag.  The lazy flag
9305796c8dcSSimon Schubert    is tested in the value_contents macro, which is used if and when
9315796c8dcSSimon Schubert    the contents are actually required.
9325796c8dcSSimon Schubert 
9335796c8dcSSimon Schubert    Note: value_at does *NOT* handle embedded offsets; perform such
9345796c8dcSSimon Schubert    adjustments before or after calling it.  */
9355796c8dcSSimon Schubert 
9365796c8dcSSimon Schubert struct value *
value_at(struct type * type,CORE_ADDR addr)9375796c8dcSSimon Schubert value_at (struct type *type, CORE_ADDR addr)
9385796c8dcSSimon Schubert {
9395796c8dcSSimon Schubert   return get_value_at (type, addr, 0);
9405796c8dcSSimon Schubert }
9415796c8dcSSimon Schubert 
9425796c8dcSSimon Schubert /* Return a lazy value with type TYPE located at ADDR (cf. value_at).  */
9435796c8dcSSimon Schubert 
9445796c8dcSSimon Schubert struct value *
value_at_lazy(struct type * type,CORE_ADDR addr)9455796c8dcSSimon Schubert value_at_lazy (struct type *type, CORE_ADDR addr)
9465796c8dcSSimon Schubert {
9475796c8dcSSimon Schubert   return get_value_at (type, addr, 1);
9485796c8dcSSimon Schubert }
9495796c8dcSSimon Schubert 
9505796c8dcSSimon Schubert /* Called only from the value_contents and value_contents_all()
9515796c8dcSSimon Schubert    macros, if the current data for a variable needs to be loaded into
9525796c8dcSSimon Schubert    value_contents(VAL).  Fetches the data from the user's process, and
9535796c8dcSSimon Schubert    clears the lazy flag to indicate that the data in the buffer is
9545796c8dcSSimon Schubert    valid.
9555796c8dcSSimon Schubert 
9565796c8dcSSimon Schubert    If the value is zero-length, we avoid calling read_memory, which
9575796c8dcSSimon Schubert    would abort.  We mark the value as fetched anyway -- all 0 bytes of
9585796c8dcSSimon Schubert    it.
9595796c8dcSSimon Schubert 
9605796c8dcSSimon Schubert    This function returns a value because it is used in the
9615796c8dcSSimon Schubert    value_contents macro as part of an expression, where a void would
9625796c8dcSSimon Schubert    not work.  The value is ignored.  */
9635796c8dcSSimon Schubert 
9645796c8dcSSimon Schubert int
value_fetch_lazy(struct value * val)9655796c8dcSSimon Schubert value_fetch_lazy (struct value *val)
9665796c8dcSSimon Schubert {
9675796c8dcSSimon Schubert   gdb_assert (value_lazy (val));
9685796c8dcSSimon Schubert   allocate_value_contents (val);
9695796c8dcSSimon Schubert   if (value_bitsize (val))
9705796c8dcSSimon Schubert     {
9715796c8dcSSimon Schubert       /* To read a lazy bitfield, read the entire enclosing value.  This
9725796c8dcSSimon Schubert 	 prevents reading the same block of (possibly volatile) memory once
9735796c8dcSSimon Schubert          per bitfield.  It would be even better to read only the containing
9745796c8dcSSimon Schubert          word, but we have no way to record that just specific bits of a
9755796c8dcSSimon Schubert          value have been fetched.  */
9765796c8dcSSimon Schubert       struct type *type = check_typedef (value_type (val));
9775796c8dcSSimon Schubert       enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
9785796c8dcSSimon Schubert       struct value *parent = value_parent (val);
9795796c8dcSSimon Schubert       LONGEST offset = value_offset (val);
980c50c785cSJohn Marino       LONGEST num;
981cf7f2e2dSJohn Marino 
982cf7f2e2dSJohn Marino       if (!value_bits_valid (val,
983cf7f2e2dSJohn Marino 			     TARGET_CHAR_BIT * offset + value_bitpos (val),
984cf7f2e2dSJohn Marino 			     value_bitsize (val)))
985cf7f2e2dSJohn Marino 	error (_("value has been optimized out"));
986cf7f2e2dSJohn Marino 
987c50c785cSJohn Marino       if (!unpack_value_bits_as_long (value_type (val),
988c50c785cSJohn Marino 				      value_contents_for_printing (parent),
989c50c785cSJohn Marino 				      offset,
990c50c785cSJohn Marino 				      value_bitpos (val),
991c50c785cSJohn Marino 				      value_bitsize (val), parent, &num))
992c50c785cSJohn Marino 	mark_value_bytes_unavailable (val,
993c50c785cSJohn Marino 				      value_embedded_offset (val),
994*ef5ccd6cSJohn Marino 				      TYPE_LENGTH (type));
995c50c785cSJohn Marino       else
996*ef5ccd6cSJohn Marino 	store_signed_integer (value_contents_raw (val), TYPE_LENGTH (type),
997c50c785cSJohn Marino 			      byte_order, num);
9985796c8dcSSimon Schubert     }
9995796c8dcSSimon Schubert   else if (VALUE_LVAL (val) == lval_memory)
10005796c8dcSSimon Schubert     {
10015796c8dcSSimon Schubert       CORE_ADDR addr = value_address (val);
1002*ef5ccd6cSJohn Marino       struct type *type = check_typedef (value_enclosing_type (val));
10035796c8dcSSimon Schubert 
1004*ef5ccd6cSJohn Marino       if (TYPE_LENGTH (type))
1005c50c785cSJohn Marino 	read_value_memory (val, 0, value_stack (val),
1006*ef5ccd6cSJohn Marino 			   addr, value_contents_all_raw (val),
1007*ef5ccd6cSJohn Marino 			   TYPE_LENGTH (type));
10085796c8dcSSimon Schubert     }
10095796c8dcSSimon Schubert   else if (VALUE_LVAL (val) == lval_register)
10105796c8dcSSimon Schubert     {
10115796c8dcSSimon Schubert       struct frame_info *frame;
10125796c8dcSSimon Schubert       int regnum;
10135796c8dcSSimon Schubert       struct type *type = check_typedef (value_type (val));
10145796c8dcSSimon Schubert       struct value *new_val = val, *mark = value_mark ();
10155796c8dcSSimon Schubert 
10165796c8dcSSimon Schubert       /* Offsets are not supported here; lazy register values must
10175796c8dcSSimon Schubert 	 refer to the entire register.  */
10185796c8dcSSimon Schubert       gdb_assert (value_offset (val) == 0);
10195796c8dcSSimon Schubert 
10205796c8dcSSimon Schubert       while (VALUE_LVAL (new_val) == lval_register && value_lazy (new_val))
10215796c8dcSSimon Schubert 	{
10225796c8dcSSimon Schubert 	  frame = frame_find_by_id (VALUE_FRAME_ID (new_val));
10235796c8dcSSimon Schubert 	  regnum = VALUE_REGNUM (new_val);
10245796c8dcSSimon Schubert 
10255796c8dcSSimon Schubert 	  gdb_assert (frame != NULL);
10265796c8dcSSimon Schubert 
10275796c8dcSSimon Schubert 	  /* Convertible register routines are used for multi-register
10285796c8dcSSimon Schubert 	     values and for interpretation in different types
10295796c8dcSSimon Schubert 	     (e.g. float or int from a double register).  Lazy
10305796c8dcSSimon Schubert 	     register values should have the register's natural type,
10315796c8dcSSimon Schubert 	     so they do not apply.  */
10325796c8dcSSimon Schubert 	  gdb_assert (!gdbarch_convert_register_p (get_frame_arch (frame),
10335796c8dcSSimon Schubert 						   regnum, type));
10345796c8dcSSimon Schubert 
10355796c8dcSSimon Schubert 	  new_val = get_frame_register_value (frame, regnum);
10365796c8dcSSimon Schubert 	}
10375796c8dcSSimon Schubert 
10385796c8dcSSimon Schubert       /* If it's still lazy (for instance, a saved register on the
10395796c8dcSSimon Schubert 	 stack), fetch it.  */
10405796c8dcSSimon Schubert       if (value_lazy (new_val))
10415796c8dcSSimon Schubert 	value_fetch_lazy (new_val);
10425796c8dcSSimon Schubert 
1043c50c785cSJohn Marino       /* If the register was not saved, mark it optimized out.  */
10445796c8dcSSimon Schubert       if (value_optimized_out (new_val))
10455796c8dcSSimon Schubert 	set_value_optimized_out (val, 1);
10465796c8dcSSimon Schubert       else
1047c50c785cSJohn Marino 	{
1048c50c785cSJohn Marino 	  set_value_lazy (val, 0);
1049c50c785cSJohn Marino 	  value_contents_copy (val, value_embedded_offset (val),
1050c50c785cSJohn Marino 			       new_val, value_embedded_offset (new_val),
10515796c8dcSSimon Schubert 			       TYPE_LENGTH (type));
1052c50c785cSJohn Marino 	}
10535796c8dcSSimon Schubert 
10545796c8dcSSimon Schubert       if (frame_debug)
10555796c8dcSSimon Schubert 	{
10565796c8dcSSimon Schubert 	  struct gdbarch *gdbarch;
10575796c8dcSSimon Schubert 	  frame = frame_find_by_id (VALUE_FRAME_ID (val));
10585796c8dcSSimon Schubert 	  regnum = VALUE_REGNUM (val);
10595796c8dcSSimon Schubert 	  gdbarch = get_frame_arch (frame);
10605796c8dcSSimon Schubert 
1061c50c785cSJohn Marino 	  fprintf_unfiltered (gdb_stdlog,
1062c50c785cSJohn Marino 			      "{ value_fetch_lazy "
1063c50c785cSJohn Marino 			      "(frame=%d,regnum=%d(%s),...) ",
10645796c8dcSSimon Schubert 			      frame_relative_level (frame), regnum,
10655796c8dcSSimon Schubert 			      user_reg_map_regnum_to_name (gdbarch, regnum));
10665796c8dcSSimon Schubert 
10675796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stdlog, "->");
10685796c8dcSSimon Schubert 	  if (value_optimized_out (new_val))
10695796c8dcSSimon Schubert 	    fprintf_unfiltered (gdb_stdlog, " optimized out");
10705796c8dcSSimon Schubert 	  else
10715796c8dcSSimon Schubert 	    {
10725796c8dcSSimon Schubert 	      int i;
10735796c8dcSSimon Schubert 	      const gdb_byte *buf = value_contents (new_val);
10745796c8dcSSimon Schubert 
10755796c8dcSSimon Schubert 	      if (VALUE_LVAL (new_val) == lval_register)
10765796c8dcSSimon Schubert 		fprintf_unfiltered (gdb_stdlog, " register=%d",
10775796c8dcSSimon Schubert 				    VALUE_REGNUM (new_val));
10785796c8dcSSimon Schubert 	      else if (VALUE_LVAL (new_val) == lval_memory)
10795796c8dcSSimon Schubert 		fprintf_unfiltered (gdb_stdlog, " address=%s",
10805796c8dcSSimon Schubert 				    paddress (gdbarch,
10815796c8dcSSimon Schubert 					      value_address (new_val)));
10825796c8dcSSimon Schubert 	      else
10835796c8dcSSimon Schubert 		fprintf_unfiltered (gdb_stdlog, " computed");
10845796c8dcSSimon Schubert 
10855796c8dcSSimon Schubert 	      fprintf_unfiltered (gdb_stdlog, " bytes=");
10865796c8dcSSimon Schubert 	      fprintf_unfiltered (gdb_stdlog, "[");
10875796c8dcSSimon Schubert 	      for (i = 0; i < register_size (gdbarch, regnum); i++)
10885796c8dcSSimon Schubert 		fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
10895796c8dcSSimon Schubert 	      fprintf_unfiltered (gdb_stdlog, "]");
10905796c8dcSSimon Schubert 	    }
10915796c8dcSSimon Schubert 
10925796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stdlog, " }\n");
10935796c8dcSSimon Schubert 	}
10945796c8dcSSimon Schubert 
10955796c8dcSSimon Schubert       /* Dispose of the intermediate values.  This prevents
10965796c8dcSSimon Schubert 	 watchpoints from trying to watch the saved frame pointer.  */
10975796c8dcSSimon Schubert       value_free_to_mark (mark);
10985796c8dcSSimon Schubert     }
1099a45ae5f8SJohn Marino   else if (VALUE_LVAL (val) == lval_computed
1100a45ae5f8SJohn Marino 	   && value_computed_funcs (val)->read != NULL)
11015796c8dcSSimon Schubert     value_computed_funcs (val)->read (val);
1102c50c785cSJohn Marino   else if (value_optimized_out (val))
1103c50c785cSJohn Marino     /* Keep it optimized out.  */;
11045796c8dcSSimon Schubert   else
1105c50c785cSJohn Marino     internal_error (__FILE__, __LINE__, _("Unexpected lazy value type."));
11065796c8dcSSimon Schubert 
11075796c8dcSSimon Schubert   set_value_lazy (val, 0);
11085796c8dcSSimon Schubert   return 0;
11095796c8dcSSimon Schubert }
11105796c8dcSSimon Schubert 
1111c50c785cSJohn Marino void
read_value_memory(struct value * val,int embedded_offset,int stack,CORE_ADDR memaddr,gdb_byte * buffer,size_t length)1112c50c785cSJohn Marino read_value_memory (struct value *val, int embedded_offset,
1113c50c785cSJohn Marino 		   int stack, CORE_ADDR memaddr,
1114c50c785cSJohn Marino 		   gdb_byte *buffer, size_t length)
1115c50c785cSJohn Marino {
1116c50c785cSJohn Marino   if (length)
1117c50c785cSJohn Marino     {
1118c50c785cSJohn Marino       VEC(mem_range_s) *available_memory;
1119c50c785cSJohn Marino 
1120c50c785cSJohn Marino       if (get_traceframe_number () < 0
1121c50c785cSJohn Marino 	  || !traceframe_available_memory (&available_memory, memaddr, length))
1122c50c785cSJohn Marino 	{
1123c50c785cSJohn Marino 	  if (stack)
1124c50c785cSJohn Marino 	    read_stack (memaddr, buffer, length);
1125c50c785cSJohn Marino 	  else
1126c50c785cSJohn Marino 	    read_memory (memaddr, buffer, length);
1127c50c785cSJohn Marino 	}
1128c50c785cSJohn Marino       else
1129c50c785cSJohn Marino 	{
1130c50c785cSJohn Marino 	  struct target_section_table *table;
1131c50c785cSJohn Marino 	  struct cleanup *old_chain;
1132c50c785cSJohn Marino 	  CORE_ADDR unavail;
1133c50c785cSJohn Marino 	  mem_range_s *r;
1134c50c785cSJohn Marino 	  int i;
1135c50c785cSJohn Marino 
1136c50c785cSJohn Marino 	  /* Fallback to reading from read-only sections.  */
1137c50c785cSJohn Marino 	  table = target_get_section_table (&exec_ops);
1138c50c785cSJohn Marino 	  available_memory =
1139c50c785cSJohn Marino 	    section_table_available_memory (available_memory,
1140c50c785cSJohn Marino 					    memaddr, length,
1141c50c785cSJohn Marino 					    table->sections,
1142c50c785cSJohn Marino 					    table->sections_end);
1143c50c785cSJohn Marino 
1144c50c785cSJohn Marino 	  old_chain = make_cleanup (VEC_cleanup(mem_range_s),
1145c50c785cSJohn Marino 				    &available_memory);
1146c50c785cSJohn Marino 
1147c50c785cSJohn Marino 	  normalize_mem_ranges (available_memory);
1148c50c785cSJohn Marino 
1149c50c785cSJohn Marino 	  /* Mark which bytes are unavailable, and read those which
1150c50c785cSJohn Marino 	     are available.  */
1151c50c785cSJohn Marino 
1152c50c785cSJohn Marino 	  unavail = memaddr;
1153c50c785cSJohn Marino 
1154c50c785cSJohn Marino 	  for (i = 0;
1155c50c785cSJohn Marino 	       VEC_iterate (mem_range_s, available_memory, i, r);
1156c50c785cSJohn Marino 	       i++)
1157c50c785cSJohn Marino 	    {
1158c50c785cSJohn Marino 	      if (mem_ranges_overlap (r->start, r->length,
1159c50c785cSJohn Marino 				      memaddr, length))
1160c50c785cSJohn Marino 		{
1161c50c785cSJohn Marino 		  CORE_ADDR lo1, hi1, lo2, hi2;
1162c50c785cSJohn Marino 		  CORE_ADDR start, end;
1163c50c785cSJohn Marino 
1164c50c785cSJohn Marino 		  /* Get the intersection window.  */
1165c50c785cSJohn Marino 		  lo1 = memaddr;
1166c50c785cSJohn Marino 		  hi1 = memaddr + length;
1167c50c785cSJohn Marino 		  lo2 = r->start;
1168c50c785cSJohn Marino 		  hi2 = r->start + r->length;
1169c50c785cSJohn Marino 		  start = max (lo1, lo2);
1170c50c785cSJohn Marino 		  end = min (hi1, hi2);
1171c50c785cSJohn Marino 
1172c50c785cSJohn Marino 		  gdb_assert (end - memaddr <= length);
1173c50c785cSJohn Marino 
1174c50c785cSJohn Marino 		  if (start > unavail)
1175c50c785cSJohn Marino 		    mark_value_bytes_unavailable (val,
1176c50c785cSJohn Marino 						  (embedded_offset
1177c50c785cSJohn Marino 						   + unavail - memaddr),
1178c50c785cSJohn Marino 						  start - unavail);
1179c50c785cSJohn Marino 		  unavail = end;
1180c50c785cSJohn Marino 
1181c50c785cSJohn Marino 		  read_memory (start, buffer + start - memaddr, end - start);
1182c50c785cSJohn Marino 		}
1183c50c785cSJohn Marino 	    }
1184c50c785cSJohn Marino 
1185c50c785cSJohn Marino 	  if (unavail != memaddr + length)
1186c50c785cSJohn Marino 	    mark_value_bytes_unavailable (val,
1187c50c785cSJohn Marino 					  embedded_offset + unavail - memaddr,
1188c50c785cSJohn Marino 					  (memaddr + length) - unavail);
1189c50c785cSJohn Marino 
1190c50c785cSJohn Marino 	  do_cleanups (old_chain);
1191c50c785cSJohn Marino 	}
1192c50c785cSJohn Marino     }
1193c50c785cSJohn Marino }
11945796c8dcSSimon Schubert 
11955796c8dcSSimon Schubert /* Store the contents of FROMVAL into the location of TOVAL.
11965796c8dcSSimon Schubert    Return a new value with the location of TOVAL and contents of FROMVAL.  */
11975796c8dcSSimon Schubert 
11985796c8dcSSimon Schubert struct value *
value_assign(struct value * toval,struct value * fromval)11995796c8dcSSimon Schubert value_assign (struct value *toval, struct value *fromval)
12005796c8dcSSimon Schubert {
12015796c8dcSSimon Schubert   struct type *type;
12025796c8dcSSimon Schubert   struct value *val;
12035796c8dcSSimon Schubert   struct frame_id old_frame;
12045796c8dcSSimon Schubert 
12055796c8dcSSimon Schubert   if (!deprecated_value_modifiable (toval))
12065796c8dcSSimon Schubert     error (_("Left operand of assignment is not a modifiable lvalue."));
12075796c8dcSSimon Schubert 
12085796c8dcSSimon Schubert   toval = coerce_ref (toval);
12095796c8dcSSimon Schubert 
12105796c8dcSSimon Schubert   type = value_type (toval);
12115796c8dcSSimon Schubert   if (VALUE_LVAL (toval) != lval_internalvar)
12125796c8dcSSimon Schubert     fromval = value_cast (type, fromval);
12135796c8dcSSimon Schubert   else
12145796c8dcSSimon Schubert     {
12155796c8dcSSimon Schubert       /* Coerce arrays and functions to pointers, except for arrays
12165796c8dcSSimon Schubert 	 which only live in GDB's storage.  */
12175796c8dcSSimon Schubert       if (!value_must_coerce_to_target (fromval))
12185796c8dcSSimon Schubert 	fromval = coerce_array (fromval);
12195796c8dcSSimon Schubert     }
12205796c8dcSSimon Schubert 
12215796c8dcSSimon Schubert   CHECK_TYPEDEF (type);
12225796c8dcSSimon Schubert 
12235796c8dcSSimon Schubert   /* Since modifying a register can trash the frame chain, and
12245796c8dcSSimon Schubert      modifying memory can trash the frame cache, we save the old frame
12255796c8dcSSimon Schubert      and then restore the new frame afterwards.  */
12265796c8dcSSimon Schubert   old_frame = get_frame_id (deprecated_safe_get_selected_frame ());
12275796c8dcSSimon Schubert 
12285796c8dcSSimon Schubert   switch (VALUE_LVAL (toval))
12295796c8dcSSimon Schubert     {
12305796c8dcSSimon Schubert     case lval_internalvar:
12315796c8dcSSimon Schubert       set_internalvar (VALUE_INTERNALVAR (toval), fromval);
1232c50c785cSJohn Marino       return value_of_internalvar (get_type_arch (type),
1233c50c785cSJohn Marino 				   VALUE_INTERNALVAR (toval));
12345796c8dcSSimon Schubert 
12355796c8dcSSimon Schubert     case lval_internalvar_component:
1236*ef5ccd6cSJohn Marino       {
1237*ef5ccd6cSJohn Marino 	int offset = value_offset (toval);
1238*ef5ccd6cSJohn Marino 
1239*ef5ccd6cSJohn Marino 	/* Are we dealing with a bitfield?
1240*ef5ccd6cSJohn Marino 
1241*ef5ccd6cSJohn Marino 	   It is important to mention that `value_parent (toval)' is
1242*ef5ccd6cSJohn Marino 	   non-NULL iff `value_bitsize (toval)' is non-zero.  */
1243*ef5ccd6cSJohn Marino 	if (value_bitsize (toval))
1244*ef5ccd6cSJohn Marino 	  {
1245*ef5ccd6cSJohn Marino 	    /* VALUE_INTERNALVAR below refers to the parent value, while
1246*ef5ccd6cSJohn Marino 	       the offset is relative to this parent value.  */
1247*ef5ccd6cSJohn Marino 	    gdb_assert (value_parent (value_parent (toval)) == NULL);
1248*ef5ccd6cSJohn Marino 	    offset += value_offset (value_parent (toval));
1249*ef5ccd6cSJohn Marino 	  }
1250*ef5ccd6cSJohn Marino 
12515796c8dcSSimon Schubert 	set_internalvar_component (VALUE_INTERNALVAR (toval),
1252*ef5ccd6cSJohn Marino 				   offset,
12535796c8dcSSimon Schubert 				   value_bitpos (toval),
12545796c8dcSSimon Schubert 				   value_bitsize (toval),
12555796c8dcSSimon Schubert 				   fromval);
1256*ef5ccd6cSJohn Marino       }
12575796c8dcSSimon Schubert       break;
12585796c8dcSSimon Schubert 
12595796c8dcSSimon Schubert     case lval_memory:
12605796c8dcSSimon Schubert       {
12615796c8dcSSimon Schubert 	const gdb_byte *dest_buffer;
12625796c8dcSSimon Schubert 	CORE_ADDR changed_addr;
12635796c8dcSSimon Schubert 	int changed_len;
12645796c8dcSSimon Schubert         gdb_byte buffer[sizeof (LONGEST)];
12655796c8dcSSimon Schubert 
12665796c8dcSSimon Schubert 	if (value_bitsize (toval))
12675796c8dcSSimon Schubert 	  {
12685796c8dcSSimon Schubert 	    struct value *parent = value_parent (toval);
12695796c8dcSSimon Schubert 
1270cf7f2e2dSJohn Marino 	    changed_addr = value_address (parent) + value_offset (toval);
12715796c8dcSSimon Schubert 	    changed_len = (value_bitpos (toval)
12725796c8dcSSimon Schubert 			   + value_bitsize (toval)
12735796c8dcSSimon Schubert 			   + HOST_CHAR_BIT - 1)
12745796c8dcSSimon Schubert 	      / HOST_CHAR_BIT;
12755796c8dcSSimon Schubert 
12765796c8dcSSimon Schubert 	    /* If we can read-modify-write exactly the size of the
12775796c8dcSSimon Schubert 	       containing type (e.g. short or int) then do so.  This
12785796c8dcSSimon Schubert 	       is safer for volatile bitfields mapped to hardware
12795796c8dcSSimon Schubert 	       registers.  */
12805796c8dcSSimon Schubert 	    if (changed_len < TYPE_LENGTH (type)
12815796c8dcSSimon Schubert 		&& TYPE_LENGTH (type) <= (int) sizeof (LONGEST)
12825796c8dcSSimon Schubert 		&& ((LONGEST) changed_addr % TYPE_LENGTH (type)) == 0)
12835796c8dcSSimon Schubert 	      changed_len = TYPE_LENGTH (type);
12845796c8dcSSimon Schubert 
12855796c8dcSSimon Schubert 	    if (changed_len > (int) sizeof (LONGEST))
1286c50c785cSJohn Marino 	      error (_("Can't handle bitfields which "
1287c50c785cSJohn Marino 		       "don't fit in a %d bit word."),
12885796c8dcSSimon Schubert 		     (int) sizeof (LONGEST) * HOST_CHAR_BIT);
12895796c8dcSSimon Schubert 
12905796c8dcSSimon Schubert 	    read_memory (changed_addr, buffer, changed_len);
12915796c8dcSSimon Schubert 	    modify_field (type, buffer, value_as_long (fromval),
12925796c8dcSSimon Schubert 			  value_bitpos (toval), value_bitsize (toval));
12935796c8dcSSimon Schubert 	    dest_buffer = buffer;
12945796c8dcSSimon Schubert 	  }
12955796c8dcSSimon Schubert 	else
12965796c8dcSSimon Schubert 	  {
12975796c8dcSSimon Schubert 	    changed_addr = value_address (toval);
12985796c8dcSSimon Schubert 	    changed_len = TYPE_LENGTH (type);
12995796c8dcSSimon Schubert 	    dest_buffer = value_contents (fromval);
13005796c8dcSSimon Schubert 	  }
13015796c8dcSSimon Schubert 
1302*ef5ccd6cSJohn Marino 	write_memory_with_notification (changed_addr, dest_buffer, changed_len);
13035796c8dcSSimon Schubert       }
13045796c8dcSSimon Schubert       break;
13055796c8dcSSimon Schubert 
13065796c8dcSSimon Schubert     case lval_register:
13075796c8dcSSimon Schubert       {
13085796c8dcSSimon Schubert 	struct frame_info *frame;
13095796c8dcSSimon Schubert 	struct gdbarch *gdbarch;
13105796c8dcSSimon Schubert 	int value_reg;
13115796c8dcSSimon Schubert 
13125796c8dcSSimon Schubert 	/* Figure out which frame this is in currently.  */
13135796c8dcSSimon Schubert 	frame = frame_find_by_id (VALUE_FRAME_ID (toval));
13145796c8dcSSimon Schubert 	value_reg = VALUE_REGNUM (toval);
13155796c8dcSSimon Schubert 
13165796c8dcSSimon Schubert 	if (!frame)
13175796c8dcSSimon Schubert 	  error (_("Value being assigned to is no longer active."));
13185796c8dcSSimon Schubert 
13195796c8dcSSimon Schubert 	gdbarch = get_frame_arch (frame);
13205796c8dcSSimon Schubert 	if (gdbarch_convert_register_p (gdbarch, VALUE_REGNUM (toval), type))
13215796c8dcSSimon Schubert 	  {
13225796c8dcSSimon Schubert 	    /* If TOVAL is a special machine register requiring
13235796c8dcSSimon Schubert 	       conversion of program values to a special raw
13245796c8dcSSimon Schubert 	       format.  */
13255796c8dcSSimon Schubert 	    gdbarch_value_to_register (gdbarch, frame,
13265796c8dcSSimon Schubert 				       VALUE_REGNUM (toval), type,
13275796c8dcSSimon Schubert 				       value_contents (fromval));
13285796c8dcSSimon Schubert 	  }
13295796c8dcSSimon Schubert 	else
13305796c8dcSSimon Schubert 	  {
13315796c8dcSSimon Schubert 	    if (value_bitsize (toval))
13325796c8dcSSimon Schubert 	      {
13335796c8dcSSimon Schubert 		struct value *parent = value_parent (toval);
13345796c8dcSSimon Schubert 		int offset = value_offset (parent) + value_offset (toval);
13355796c8dcSSimon Schubert 		int changed_len;
13365796c8dcSSimon Schubert 		gdb_byte buffer[sizeof (LONGEST)];
1337c50c785cSJohn Marino 		int optim, unavail;
13385796c8dcSSimon Schubert 
13395796c8dcSSimon Schubert 		changed_len = (value_bitpos (toval)
13405796c8dcSSimon Schubert 			       + value_bitsize (toval)
13415796c8dcSSimon Schubert 			       + HOST_CHAR_BIT - 1)
13425796c8dcSSimon Schubert 		  / HOST_CHAR_BIT;
13435796c8dcSSimon Schubert 
13445796c8dcSSimon Schubert 		if (changed_len > (int) sizeof (LONGEST))
1345c50c785cSJohn Marino 		  error (_("Can't handle bitfields which "
1346c50c785cSJohn Marino 			   "don't fit in a %d bit word."),
13475796c8dcSSimon Schubert 			 (int) sizeof (LONGEST) * HOST_CHAR_BIT);
13485796c8dcSSimon Schubert 
1349c50c785cSJohn Marino 		if (!get_frame_register_bytes (frame, value_reg, offset,
1350c50c785cSJohn Marino 					       changed_len, buffer,
1351c50c785cSJohn Marino 					       &optim, &unavail))
1352c50c785cSJohn Marino 		  {
1353c50c785cSJohn Marino 		    if (optim)
1354c50c785cSJohn Marino 		      error (_("value has been optimized out"));
1355c50c785cSJohn Marino 		    if (unavail)
1356c50c785cSJohn Marino 		      throw_error (NOT_AVAILABLE_ERROR,
1357c50c785cSJohn Marino 				   _("value is not available"));
1358c50c785cSJohn Marino 		  }
13595796c8dcSSimon Schubert 
13605796c8dcSSimon Schubert 		modify_field (type, buffer, value_as_long (fromval),
13615796c8dcSSimon Schubert 			      value_bitpos (toval), value_bitsize (toval));
13625796c8dcSSimon Schubert 
13635796c8dcSSimon Schubert 		put_frame_register_bytes (frame, value_reg, offset,
13645796c8dcSSimon Schubert 					  changed_len, buffer);
13655796c8dcSSimon Schubert 	      }
13665796c8dcSSimon Schubert 	    else
13675796c8dcSSimon Schubert 	      {
13685796c8dcSSimon Schubert 		put_frame_register_bytes (frame, value_reg,
13695796c8dcSSimon Schubert 					  value_offset (toval),
13705796c8dcSSimon Schubert 					  TYPE_LENGTH (type),
13715796c8dcSSimon Schubert 					  value_contents (fromval));
13725796c8dcSSimon Schubert 	      }
13735796c8dcSSimon Schubert 	  }
13745796c8dcSSimon Schubert 
13755796c8dcSSimon Schubert 	if (deprecated_register_changed_hook)
13765796c8dcSSimon Schubert 	  deprecated_register_changed_hook (-1);
13775796c8dcSSimon Schubert 	break;
13785796c8dcSSimon Schubert       }
13795796c8dcSSimon Schubert 
13805796c8dcSSimon Schubert     case lval_computed:
13815796c8dcSSimon Schubert       {
1382a45ae5f8SJohn Marino 	const struct lval_funcs *funcs = value_computed_funcs (toval);
13835796c8dcSSimon Schubert 
1384a45ae5f8SJohn Marino 	if (funcs->write != NULL)
1385a45ae5f8SJohn Marino 	  {
13865796c8dcSSimon Schubert 	    funcs->write (toval, fromval);
13875796c8dcSSimon Schubert 	    break;
1388a45ae5f8SJohn Marino 	  }
1389a45ae5f8SJohn Marino       }
1390a45ae5f8SJohn Marino       /* Fall through.  */
13915796c8dcSSimon Schubert 
13925796c8dcSSimon Schubert     default:
13935796c8dcSSimon Schubert       error (_("Left operand of assignment is not an lvalue."));
13945796c8dcSSimon Schubert     }
13955796c8dcSSimon Schubert 
13965796c8dcSSimon Schubert   /* Assigning to the stack pointer, frame pointer, and other
13975796c8dcSSimon Schubert      (architecture and calling convention specific) registers may
1398*ef5ccd6cSJohn Marino      cause the frame cache and regcache to be out of date.  Assigning to memory
13995796c8dcSSimon Schubert      also can.  We just do this on all assignments to registers or
14005796c8dcSSimon Schubert      memory, for simplicity's sake; I doubt the slowdown matters.  */
14015796c8dcSSimon Schubert   switch (VALUE_LVAL (toval))
14025796c8dcSSimon Schubert     {
14035796c8dcSSimon Schubert     case lval_memory:
14045796c8dcSSimon Schubert     case lval_register:
1405cf7f2e2dSJohn Marino     case lval_computed:
14065796c8dcSSimon Schubert 
1407*ef5ccd6cSJohn Marino       observer_notify_target_changed (&current_target);
14085796c8dcSSimon Schubert 
14095796c8dcSSimon Schubert       /* Having destroyed the frame cache, restore the selected
14105796c8dcSSimon Schubert 	 frame.  */
14115796c8dcSSimon Schubert 
14125796c8dcSSimon Schubert       /* FIXME: cagney/2002-11-02: There has to be a better way of
14135796c8dcSSimon Schubert 	 doing this.  Instead of constantly saving/restoring the
14145796c8dcSSimon Schubert 	 frame.  Why not create a get_selected_frame() function that,
14155796c8dcSSimon Schubert 	 having saved the selected frame's ID can automatically
14165796c8dcSSimon Schubert 	 re-find the previously selected frame automatically.  */
14175796c8dcSSimon Schubert 
14185796c8dcSSimon Schubert       {
14195796c8dcSSimon Schubert 	struct frame_info *fi = frame_find_by_id (old_frame);
1420cf7f2e2dSJohn Marino 
14215796c8dcSSimon Schubert 	if (fi != NULL)
14225796c8dcSSimon Schubert 	  select_frame (fi);
14235796c8dcSSimon Schubert       }
14245796c8dcSSimon Schubert 
14255796c8dcSSimon Schubert       break;
14265796c8dcSSimon Schubert     default:
14275796c8dcSSimon Schubert       break;
14285796c8dcSSimon Schubert     }
14295796c8dcSSimon Schubert 
14305796c8dcSSimon Schubert   /* If the field does not entirely fill a LONGEST, then zero the sign
14315796c8dcSSimon Schubert      bits.  If the field is signed, and is negative, then sign
14325796c8dcSSimon Schubert      extend.  */
14335796c8dcSSimon Schubert   if ((value_bitsize (toval) > 0)
14345796c8dcSSimon Schubert       && (value_bitsize (toval) < 8 * (int) sizeof (LONGEST)))
14355796c8dcSSimon Schubert     {
14365796c8dcSSimon Schubert       LONGEST fieldval = value_as_long (fromval);
14375796c8dcSSimon Schubert       LONGEST valmask = (((ULONGEST) 1) << value_bitsize (toval)) - 1;
14385796c8dcSSimon Schubert 
14395796c8dcSSimon Schubert       fieldval &= valmask;
14405796c8dcSSimon Schubert       if (!TYPE_UNSIGNED (type)
14415796c8dcSSimon Schubert 	  && (fieldval & (valmask ^ (valmask >> 1))))
14425796c8dcSSimon Schubert 	fieldval |= ~valmask;
14435796c8dcSSimon Schubert 
14445796c8dcSSimon Schubert       fromval = value_from_longest (type, fieldval);
14455796c8dcSSimon Schubert     }
14465796c8dcSSimon Schubert 
1447c50c785cSJohn Marino   /* The return value is a copy of TOVAL so it shares its location
1448c50c785cSJohn Marino      information, but its contents are updated from FROMVAL.  This
1449c50c785cSJohn Marino      implies the returned value is not lazy, even if TOVAL was.  */
14505796c8dcSSimon Schubert   val = value_copy (toval);
1451c50c785cSJohn Marino   set_value_lazy (val, 0);
14525796c8dcSSimon Schubert   memcpy (value_contents_raw (val), value_contents (fromval),
14535796c8dcSSimon Schubert 	  TYPE_LENGTH (type));
1454c50c785cSJohn Marino 
1455c50c785cSJohn Marino   /* We copy over the enclosing type and pointed-to offset from FROMVAL
1456c50c785cSJohn Marino      in the case of pointer types.  For object types, the enclosing type
1457c50c785cSJohn Marino      and embedded offset must *not* be copied: the target object refered
1458c50c785cSJohn Marino      to by TOVAL retains its original dynamic type after assignment.  */
1459c50c785cSJohn Marino   if (TYPE_CODE (type) == TYPE_CODE_PTR)
1460c50c785cSJohn Marino     {
1461c50c785cSJohn Marino       set_value_enclosing_type (val, value_enclosing_type (fromval));
14625796c8dcSSimon Schubert       set_value_pointed_to_offset (val, value_pointed_to_offset (fromval));
1463c50c785cSJohn Marino     }
14645796c8dcSSimon Schubert 
14655796c8dcSSimon Schubert   return val;
14665796c8dcSSimon Schubert }
14675796c8dcSSimon Schubert 
14685796c8dcSSimon Schubert /* Extend a value VAL to COUNT repetitions of its type.  */
14695796c8dcSSimon Schubert 
14705796c8dcSSimon Schubert struct value *
value_repeat(struct value * arg1,int count)14715796c8dcSSimon Schubert value_repeat (struct value *arg1, int count)
14725796c8dcSSimon Schubert {
14735796c8dcSSimon Schubert   struct value *val;
14745796c8dcSSimon Schubert 
14755796c8dcSSimon Schubert   if (VALUE_LVAL (arg1) != lval_memory)
14765796c8dcSSimon Schubert     error (_("Only values in memory can be extended with '@'."));
14775796c8dcSSimon Schubert   if (count < 1)
14785796c8dcSSimon Schubert     error (_("Invalid number %d of repetitions."), count);
14795796c8dcSSimon Schubert 
14805796c8dcSSimon Schubert   val = allocate_repeat_value (value_enclosing_type (arg1), count);
14815796c8dcSSimon Schubert 
14825796c8dcSSimon Schubert   VALUE_LVAL (val) = lval_memory;
14835796c8dcSSimon Schubert   set_value_address (val, value_address (arg1));
14845796c8dcSSimon Schubert 
1485c50c785cSJohn Marino   read_value_memory (val, 0, value_stack (val), value_address (val),
1486c50c785cSJohn Marino 		     value_contents_all_raw (val),
1487c50c785cSJohn Marino 		     TYPE_LENGTH (value_enclosing_type (val)));
1488c50c785cSJohn Marino 
14895796c8dcSSimon Schubert   return val;
14905796c8dcSSimon Schubert }
14915796c8dcSSimon Schubert 
14925796c8dcSSimon Schubert struct value *
value_of_variable(struct symbol * var,const struct block * b)1493a45ae5f8SJohn Marino value_of_variable (struct symbol *var, const struct block *b)
14945796c8dcSSimon Schubert {
14955796c8dcSSimon Schubert   struct frame_info *frame;
14965796c8dcSSimon Schubert 
14975796c8dcSSimon Schubert   if (!symbol_read_needs_frame (var))
14985796c8dcSSimon Schubert     frame = NULL;
14995796c8dcSSimon Schubert   else if (!b)
15005796c8dcSSimon Schubert     frame = get_selected_frame (_("No frame selected."));
15015796c8dcSSimon Schubert   else
15025796c8dcSSimon Schubert     {
15035796c8dcSSimon Schubert       frame = block_innermost_frame (b);
15045796c8dcSSimon Schubert       if (!frame)
15055796c8dcSSimon Schubert 	{
15065796c8dcSSimon Schubert 	  if (BLOCK_FUNCTION (b) && !block_inlined_p (b)
15075796c8dcSSimon Schubert 	      && SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)))
15085796c8dcSSimon Schubert 	    error (_("No frame is currently executing in block %s."),
15095796c8dcSSimon Schubert 		   SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)));
15105796c8dcSSimon Schubert 	  else
15115796c8dcSSimon Schubert 	    error (_("No frame is currently executing in specified block"));
15125796c8dcSSimon Schubert 	}
15135796c8dcSSimon Schubert     }
15145796c8dcSSimon Schubert 
1515a45ae5f8SJohn Marino   return read_var_value (var, frame);
15165796c8dcSSimon Schubert }
15175796c8dcSSimon Schubert 
15185796c8dcSSimon Schubert struct value *
address_of_variable(struct symbol * var,const struct block * b)1519*ef5ccd6cSJohn Marino address_of_variable (struct symbol *var, const struct block *b)
15205796c8dcSSimon Schubert {
15215796c8dcSSimon Schubert   struct type *type = SYMBOL_TYPE (var);
15225796c8dcSSimon Schubert   struct value *val;
15235796c8dcSSimon Schubert 
15245796c8dcSSimon Schubert   /* Evaluate it first; if the result is a memory address, we're fine.
15255796c8dcSSimon Schubert      Lazy evaluation pays off here.  */
15265796c8dcSSimon Schubert 
15275796c8dcSSimon Schubert   val = value_of_variable (var, b);
15285796c8dcSSimon Schubert 
15295796c8dcSSimon Schubert   if ((VALUE_LVAL (val) == lval_memory && value_lazy (val))
15305796c8dcSSimon Schubert       || TYPE_CODE (type) == TYPE_CODE_FUNC)
15315796c8dcSSimon Schubert     {
15325796c8dcSSimon Schubert       CORE_ADDR addr = value_address (val);
1533cf7f2e2dSJohn Marino 
15345796c8dcSSimon Schubert       return value_from_pointer (lookup_pointer_type (type), addr);
15355796c8dcSSimon Schubert     }
15365796c8dcSSimon Schubert 
15375796c8dcSSimon Schubert   /* Not a memory address; check what the problem was.  */
15385796c8dcSSimon Schubert   switch (VALUE_LVAL (val))
15395796c8dcSSimon Schubert     {
15405796c8dcSSimon Schubert     case lval_register:
15415796c8dcSSimon Schubert       {
15425796c8dcSSimon Schubert 	struct frame_info *frame;
15435796c8dcSSimon Schubert 	const char *regname;
15445796c8dcSSimon Schubert 
15455796c8dcSSimon Schubert 	frame = frame_find_by_id (VALUE_FRAME_ID (val));
15465796c8dcSSimon Schubert 	gdb_assert (frame);
15475796c8dcSSimon Schubert 
15485796c8dcSSimon Schubert 	regname = gdbarch_register_name (get_frame_arch (frame),
15495796c8dcSSimon Schubert 					 VALUE_REGNUM (val));
15505796c8dcSSimon Schubert 	gdb_assert (regname && *regname);
15515796c8dcSSimon Schubert 
15525796c8dcSSimon Schubert 	error (_("Address requested for identifier "
15535796c8dcSSimon Schubert 		 "\"%s\" which is in register $%s"),
15545796c8dcSSimon Schubert 	       SYMBOL_PRINT_NAME (var), regname);
15555796c8dcSSimon Schubert 	break;
15565796c8dcSSimon Schubert       }
15575796c8dcSSimon Schubert 
15585796c8dcSSimon Schubert     default:
15595796c8dcSSimon Schubert       error (_("Can't take address of \"%s\" which isn't an lvalue."),
15605796c8dcSSimon Schubert 	     SYMBOL_PRINT_NAME (var));
15615796c8dcSSimon Schubert       break;
15625796c8dcSSimon Schubert     }
15635796c8dcSSimon Schubert 
15645796c8dcSSimon Schubert   return val;
15655796c8dcSSimon Schubert }
15665796c8dcSSimon Schubert 
15675796c8dcSSimon Schubert /* Return one if VAL does not live in target memory, but should in order
15685796c8dcSSimon Schubert    to operate on it.  Otherwise return zero.  */
15695796c8dcSSimon Schubert 
15705796c8dcSSimon Schubert int
value_must_coerce_to_target(struct value * val)15715796c8dcSSimon Schubert value_must_coerce_to_target (struct value *val)
15725796c8dcSSimon Schubert {
15735796c8dcSSimon Schubert   struct type *valtype;
15745796c8dcSSimon Schubert 
15755796c8dcSSimon Schubert   /* The only lval kinds which do not live in target memory.  */
15765796c8dcSSimon Schubert   if (VALUE_LVAL (val) != not_lval
15775796c8dcSSimon Schubert       && VALUE_LVAL (val) != lval_internalvar)
15785796c8dcSSimon Schubert     return 0;
15795796c8dcSSimon Schubert 
15805796c8dcSSimon Schubert   valtype = check_typedef (value_type (val));
15815796c8dcSSimon Schubert 
15825796c8dcSSimon Schubert   switch (TYPE_CODE (valtype))
15835796c8dcSSimon Schubert     {
15845796c8dcSSimon Schubert     case TYPE_CODE_ARRAY:
1585c50c785cSJohn Marino       return TYPE_VECTOR (valtype) ? 0 : 1;
15865796c8dcSSimon Schubert     case TYPE_CODE_STRING:
15875796c8dcSSimon Schubert       return 1;
15885796c8dcSSimon Schubert     default:
15895796c8dcSSimon Schubert       return 0;
15905796c8dcSSimon Schubert     }
15915796c8dcSSimon Schubert }
15925796c8dcSSimon Schubert 
1593c50c785cSJohn Marino /* Make sure that VAL lives in target memory if it's supposed to.  For
1594c50c785cSJohn Marino    instance, strings are constructed as character arrays in GDB's
1595c50c785cSJohn Marino    storage, and this function copies them to the target.  */
15965796c8dcSSimon Schubert 
15975796c8dcSSimon Schubert struct value *
value_coerce_to_target(struct value * val)15985796c8dcSSimon Schubert value_coerce_to_target (struct value *val)
15995796c8dcSSimon Schubert {
16005796c8dcSSimon Schubert   LONGEST length;
16015796c8dcSSimon Schubert   CORE_ADDR addr;
16025796c8dcSSimon Schubert 
16035796c8dcSSimon Schubert   if (!value_must_coerce_to_target (val))
16045796c8dcSSimon Schubert     return val;
16055796c8dcSSimon Schubert 
16065796c8dcSSimon Schubert   length = TYPE_LENGTH (check_typedef (value_type (val)));
16075796c8dcSSimon Schubert   addr = allocate_space_in_inferior (length);
16085796c8dcSSimon Schubert   write_memory (addr, value_contents (val), length);
16095796c8dcSSimon Schubert   return value_at_lazy (value_type (val), addr);
16105796c8dcSSimon Schubert }
16115796c8dcSSimon Schubert 
16125796c8dcSSimon Schubert /* Given a value which is an array, return a value which is a pointer
16135796c8dcSSimon Schubert    to its first element, regardless of whether or not the array has a
16145796c8dcSSimon Schubert    nonzero lower bound.
16155796c8dcSSimon Schubert 
16165796c8dcSSimon Schubert    FIXME: A previous comment here indicated that this routine should
16175796c8dcSSimon Schubert    be substracting the array's lower bound.  It's not clear to me that
16185796c8dcSSimon Schubert    this is correct.  Given an array subscripting operation, it would
16195796c8dcSSimon Schubert    certainly work to do the adjustment here, essentially computing:
16205796c8dcSSimon Schubert 
16215796c8dcSSimon Schubert    (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0])
16225796c8dcSSimon Schubert 
16235796c8dcSSimon Schubert    However I believe a more appropriate and logical place to account
16245796c8dcSSimon Schubert    for the lower bound is to do so in value_subscript, essentially
16255796c8dcSSimon Schubert    computing:
16265796c8dcSSimon Schubert 
16275796c8dcSSimon Schubert    (&array[0] + ((index - lowerbound) * sizeof array[0]))
16285796c8dcSSimon Schubert 
16295796c8dcSSimon Schubert    As further evidence consider what would happen with operations
16305796c8dcSSimon Schubert    other than array subscripting, where the caller would get back a
16315796c8dcSSimon Schubert    value that had an address somewhere before the actual first element
16325796c8dcSSimon Schubert    of the array, and the information about the lower bound would be
1633c50c785cSJohn Marino    lost because of the coercion to pointer type.  */
16345796c8dcSSimon Schubert 
16355796c8dcSSimon Schubert struct value *
value_coerce_array(struct value * arg1)16365796c8dcSSimon Schubert value_coerce_array (struct value *arg1)
16375796c8dcSSimon Schubert {
16385796c8dcSSimon Schubert   struct type *type = check_typedef (value_type (arg1));
16395796c8dcSSimon Schubert 
16405796c8dcSSimon Schubert   /* If the user tries to do something requiring a pointer with an
16415796c8dcSSimon Schubert      array that has not yet been pushed to the target, then this would
16425796c8dcSSimon Schubert      be a good time to do so.  */
16435796c8dcSSimon Schubert   arg1 = value_coerce_to_target (arg1);
16445796c8dcSSimon Schubert 
16455796c8dcSSimon Schubert   if (VALUE_LVAL (arg1) != lval_memory)
16465796c8dcSSimon Schubert     error (_("Attempt to take address of value not located in memory."));
16475796c8dcSSimon Schubert 
16485796c8dcSSimon Schubert   return value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
16495796c8dcSSimon Schubert 			     value_address (arg1));
16505796c8dcSSimon Schubert }
16515796c8dcSSimon Schubert 
16525796c8dcSSimon Schubert /* Given a value which is a function, return a value which is a pointer
16535796c8dcSSimon Schubert    to it.  */
16545796c8dcSSimon Schubert 
16555796c8dcSSimon Schubert struct value *
value_coerce_function(struct value * arg1)16565796c8dcSSimon Schubert value_coerce_function (struct value *arg1)
16575796c8dcSSimon Schubert {
16585796c8dcSSimon Schubert   struct value *retval;
16595796c8dcSSimon Schubert 
16605796c8dcSSimon Schubert   if (VALUE_LVAL (arg1) != lval_memory)
16615796c8dcSSimon Schubert     error (_("Attempt to take address of value not located in memory."));
16625796c8dcSSimon Schubert 
16635796c8dcSSimon Schubert   retval = value_from_pointer (lookup_pointer_type (value_type (arg1)),
16645796c8dcSSimon Schubert 			       value_address (arg1));
16655796c8dcSSimon Schubert   return retval;
16665796c8dcSSimon Schubert }
16675796c8dcSSimon Schubert 
16685796c8dcSSimon Schubert /* Return a pointer value for the object for which ARG1 is the
16695796c8dcSSimon Schubert    contents.  */
16705796c8dcSSimon Schubert 
16715796c8dcSSimon Schubert struct value *
value_addr(struct value * arg1)16725796c8dcSSimon Schubert value_addr (struct value *arg1)
16735796c8dcSSimon Schubert {
16745796c8dcSSimon Schubert   struct value *arg2;
16755796c8dcSSimon Schubert   struct type *type = check_typedef (value_type (arg1));
1676cf7f2e2dSJohn Marino 
16775796c8dcSSimon Schubert   if (TYPE_CODE (type) == TYPE_CODE_REF)
16785796c8dcSSimon Schubert     {
16795796c8dcSSimon Schubert       /* Copy the value, but change the type from (T&) to (T*).  We
16805796c8dcSSimon Schubert          keep the same location information, which is efficient, and
16815796c8dcSSimon Schubert          allows &(&X) to get the location containing the reference.  */
16825796c8dcSSimon Schubert       arg2 = value_copy (arg1);
16835796c8dcSSimon Schubert       deprecated_set_value_type (arg2,
16845796c8dcSSimon Schubert 				 lookup_pointer_type (TYPE_TARGET_TYPE (type)));
16855796c8dcSSimon Schubert       return arg2;
16865796c8dcSSimon Schubert     }
16875796c8dcSSimon Schubert   if (TYPE_CODE (type) == TYPE_CODE_FUNC)
16885796c8dcSSimon Schubert     return value_coerce_function (arg1);
16895796c8dcSSimon Schubert 
16905796c8dcSSimon Schubert   /* If this is an array that has not yet been pushed to the target,
16915796c8dcSSimon Schubert      then this would be a good time to force it to memory.  */
16925796c8dcSSimon Schubert   arg1 = value_coerce_to_target (arg1);
16935796c8dcSSimon Schubert 
16945796c8dcSSimon Schubert   if (VALUE_LVAL (arg1) != lval_memory)
16955796c8dcSSimon Schubert     error (_("Attempt to take address of value not located in memory."));
16965796c8dcSSimon Schubert 
1697c50c785cSJohn Marino   /* Get target memory address.  */
16985796c8dcSSimon Schubert   arg2 = value_from_pointer (lookup_pointer_type (value_type (arg1)),
16995796c8dcSSimon Schubert 			     (value_address (arg1)
17005796c8dcSSimon Schubert 			      + value_embedded_offset (arg1)));
17015796c8dcSSimon Schubert 
17025796c8dcSSimon Schubert   /* This may be a pointer to a base subobject; so remember the
17035796c8dcSSimon Schubert      full derived object's type ...  */
1704c50c785cSJohn Marino   set_value_enclosing_type (arg2,
1705c50c785cSJohn Marino 			    lookup_pointer_type (value_enclosing_type (arg1)));
17065796c8dcSSimon Schubert   /* ... and also the relative position of the subobject in the full
17075796c8dcSSimon Schubert      object.  */
17085796c8dcSSimon Schubert   set_value_pointed_to_offset (arg2, value_embedded_offset (arg1));
17095796c8dcSSimon Schubert   return arg2;
17105796c8dcSSimon Schubert }
17115796c8dcSSimon Schubert 
17125796c8dcSSimon Schubert /* Return a reference value for the object for which ARG1 is the
17135796c8dcSSimon Schubert    contents.  */
17145796c8dcSSimon Schubert 
17155796c8dcSSimon Schubert struct value *
value_ref(struct value * arg1)17165796c8dcSSimon Schubert value_ref (struct value *arg1)
17175796c8dcSSimon Schubert {
17185796c8dcSSimon Schubert   struct value *arg2;
17195796c8dcSSimon Schubert   struct type *type = check_typedef (value_type (arg1));
1720cf7f2e2dSJohn Marino 
17215796c8dcSSimon Schubert   if (TYPE_CODE (type) == TYPE_CODE_REF)
17225796c8dcSSimon Schubert     return arg1;
17235796c8dcSSimon Schubert 
17245796c8dcSSimon Schubert   arg2 = value_addr (arg1);
17255796c8dcSSimon Schubert   deprecated_set_value_type (arg2, lookup_reference_type (type));
17265796c8dcSSimon Schubert   return arg2;
17275796c8dcSSimon Schubert }
17285796c8dcSSimon Schubert 
17295796c8dcSSimon Schubert /* Given a value of a pointer type, apply the C unary * operator to
17305796c8dcSSimon Schubert    it.  */
17315796c8dcSSimon Schubert 
17325796c8dcSSimon Schubert struct value *
value_ind(struct value * arg1)17335796c8dcSSimon Schubert value_ind (struct value *arg1)
17345796c8dcSSimon Schubert {
17355796c8dcSSimon Schubert   struct type *base_type;
17365796c8dcSSimon Schubert   struct value *arg2;
17375796c8dcSSimon Schubert 
17385796c8dcSSimon Schubert   arg1 = coerce_array (arg1);
17395796c8dcSSimon Schubert 
17405796c8dcSSimon Schubert   base_type = check_typedef (value_type (arg1));
17415796c8dcSSimon Schubert 
1742c50c785cSJohn Marino   if (VALUE_LVAL (arg1) == lval_computed)
1743c50c785cSJohn Marino     {
1744a45ae5f8SJohn Marino       const struct lval_funcs *funcs = value_computed_funcs (arg1);
1745c50c785cSJohn Marino 
1746c50c785cSJohn Marino       if (funcs->indirect)
1747c50c785cSJohn Marino 	{
1748c50c785cSJohn Marino 	  struct value *result = funcs->indirect (arg1);
1749c50c785cSJohn Marino 
1750c50c785cSJohn Marino 	  if (result)
1751c50c785cSJohn Marino 	    return result;
1752c50c785cSJohn Marino 	}
1753c50c785cSJohn Marino     }
1754c50c785cSJohn Marino 
17555796c8dcSSimon Schubert   if (TYPE_CODE (base_type) == TYPE_CODE_PTR)
17565796c8dcSSimon Schubert     {
17575796c8dcSSimon Schubert       struct type *enc_type;
1758cf7f2e2dSJohn Marino 
17595796c8dcSSimon Schubert       /* We may be pointing to something embedded in a larger object.
17605796c8dcSSimon Schubert          Get the real type of the enclosing object.  */
17615796c8dcSSimon Schubert       enc_type = check_typedef (value_enclosing_type (arg1));
17625796c8dcSSimon Schubert       enc_type = TYPE_TARGET_TYPE (enc_type);
17635796c8dcSSimon Schubert 
17645796c8dcSSimon Schubert       if (TYPE_CODE (check_typedef (enc_type)) == TYPE_CODE_FUNC
17655796c8dcSSimon Schubert 	  || TYPE_CODE (check_typedef (enc_type)) == TYPE_CODE_METHOD)
17665796c8dcSSimon Schubert 	/* For functions, go through find_function_addr, which knows
17675796c8dcSSimon Schubert 	   how to handle function descriptors.  */
17685796c8dcSSimon Schubert 	arg2 = value_at_lazy (enc_type,
17695796c8dcSSimon Schubert 			      find_function_addr (arg1, NULL));
17705796c8dcSSimon Schubert       else
1771c50c785cSJohn Marino 	/* Retrieve the enclosing object pointed to.  */
17725796c8dcSSimon Schubert 	arg2 = value_at_lazy (enc_type,
17735796c8dcSSimon Schubert 			      (value_as_address (arg1)
17745796c8dcSSimon Schubert 			       - value_pointed_to_offset (arg1)));
17755796c8dcSSimon Schubert 
1776*ef5ccd6cSJohn Marino       return readjust_indirect_value_type (arg2, enc_type, base_type, arg1);
17775796c8dcSSimon Schubert     }
17785796c8dcSSimon Schubert 
17795796c8dcSSimon Schubert   error (_("Attempt to take contents of a non-pointer value."));
17805796c8dcSSimon Schubert   return 0;			/* For lint -- never reached.  */
17815796c8dcSSimon Schubert }
17825796c8dcSSimon Schubert 
1783c50c785cSJohn Marino /* Create a value for an array by allocating space in GDB, copying the
1784c50c785cSJohn Marino    data into that space, and then setting up an array value.
17855796c8dcSSimon Schubert 
17865796c8dcSSimon Schubert    The array bounds are set from LOWBOUND and HIGHBOUND, and the array
17875796c8dcSSimon Schubert    is populated from the values passed in ELEMVEC.
17885796c8dcSSimon Schubert 
17895796c8dcSSimon Schubert    The element type of the array is inherited from the type of the
17905796c8dcSSimon Schubert    first element, and all elements must have the same size (though we
17915796c8dcSSimon Schubert    don't currently enforce any restriction on their types).  */
17925796c8dcSSimon Schubert 
17935796c8dcSSimon Schubert struct value *
value_array(int lowbound,int highbound,struct value ** elemvec)17945796c8dcSSimon Schubert value_array (int lowbound, int highbound, struct value **elemvec)
17955796c8dcSSimon Schubert {
17965796c8dcSSimon Schubert   int nelem;
17975796c8dcSSimon Schubert   int idx;
17985796c8dcSSimon Schubert   unsigned int typelength;
17995796c8dcSSimon Schubert   struct value *val;
18005796c8dcSSimon Schubert   struct type *arraytype;
18015796c8dcSSimon Schubert 
18025796c8dcSSimon Schubert   /* Validate that the bounds are reasonable and that each of the
18035796c8dcSSimon Schubert      elements have the same size.  */
18045796c8dcSSimon Schubert 
18055796c8dcSSimon Schubert   nelem = highbound - lowbound + 1;
18065796c8dcSSimon Schubert   if (nelem <= 0)
18075796c8dcSSimon Schubert     {
18085796c8dcSSimon Schubert       error (_("bad array bounds (%d, %d)"), lowbound, highbound);
18095796c8dcSSimon Schubert     }
18105796c8dcSSimon Schubert   typelength = TYPE_LENGTH (value_enclosing_type (elemvec[0]));
18115796c8dcSSimon Schubert   for (idx = 1; idx < nelem; idx++)
18125796c8dcSSimon Schubert     {
18135796c8dcSSimon Schubert       if (TYPE_LENGTH (value_enclosing_type (elemvec[idx])) != typelength)
18145796c8dcSSimon Schubert 	{
18155796c8dcSSimon Schubert 	  error (_("array elements must all be the same size"));
18165796c8dcSSimon Schubert 	}
18175796c8dcSSimon Schubert     }
18185796c8dcSSimon Schubert 
18195796c8dcSSimon Schubert   arraytype = lookup_array_range_type (value_enclosing_type (elemvec[0]),
18205796c8dcSSimon Schubert 				       lowbound, highbound);
18215796c8dcSSimon Schubert 
18225796c8dcSSimon Schubert   if (!current_language->c_style_arrays)
18235796c8dcSSimon Schubert     {
18245796c8dcSSimon Schubert       val = allocate_value (arraytype);
18255796c8dcSSimon Schubert       for (idx = 0; idx < nelem; idx++)
1826c50c785cSJohn Marino 	value_contents_copy (val, idx * typelength, elemvec[idx], 0,
18275796c8dcSSimon Schubert 			     typelength);
18285796c8dcSSimon Schubert       return val;
18295796c8dcSSimon Schubert     }
18305796c8dcSSimon Schubert 
18315796c8dcSSimon Schubert   /* Allocate space to store the array, and then initialize it by
18325796c8dcSSimon Schubert      copying in each element.  */
18335796c8dcSSimon Schubert 
18345796c8dcSSimon Schubert   val = allocate_value (arraytype);
18355796c8dcSSimon Schubert   for (idx = 0; idx < nelem; idx++)
1836c50c785cSJohn Marino     value_contents_copy (val, idx * typelength, elemvec[idx], 0, typelength);
18375796c8dcSSimon Schubert   return val;
18385796c8dcSSimon Schubert }
18395796c8dcSSimon Schubert 
18405796c8dcSSimon Schubert struct value *
value_cstring(char * ptr,ssize_t len,struct type * char_type)1841*ef5ccd6cSJohn Marino value_cstring (char *ptr, ssize_t len, struct type *char_type)
18425796c8dcSSimon Schubert {
18435796c8dcSSimon Schubert   struct value *val;
18445796c8dcSSimon Schubert   int lowbound = current_language->string_lower_bound;
1845*ef5ccd6cSJohn Marino   ssize_t highbound = len / TYPE_LENGTH (char_type);
18465796c8dcSSimon Schubert   struct type *stringtype
18475796c8dcSSimon Schubert     = lookup_array_range_type (char_type, lowbound, highbound + lowbound - 1);
18485796c8dcSSimon Schubert 
18495796c8dcSSimon Schubert   val = allocate_value (stringtype);
18505796c8dcSSimon Schubert   memcpy (value_contents_raw (val), ptr, len);
18515796c8dcSSimon Schubert   return val;
18525796c8dcSSimon Schubert }
18535796c8dcSSimon Schubert 
18545796c8dcSSimon Schubert /* Create a value for a string constant by allocating space in the
18555796c8dcSSimon Schubert    inferior, copying the data into that space, and returning the
18565796c8dcSSimon Schubert    address with type TYPE_CODE_STRING.  PTR points to the string
18575796c8dcSSimon Schubert    constant data; LEN is number of characters.
18585796c8dcSSimon Schubert 
18595796c8dcSSimon Schubert    Note that string types are like array of char types with a lower
18605796c8dcSSimon Schubert    bound of zero and an upper bound of LEN - 1.  Also note that the
18615796c8dcSSimon Schubert    string may contain embedded null bytes.  */
18625796c8dcSSimon Schubert 
18635796c8dcSSimon Schubert struct value *
value_string(char * ptr,ssize_t len,struct type * char_type)1864*ef5ccd6cSJohn Marino value_string (char *ptr, ssize_t len, struct type *char_type)
18655796c8dcSSimon Schubert {
18665796c8dcSSimon Schubert   struct value *val;
18675796c8dcSSimon Schubert   int lowbound = current_language->string_lower_bound;
1868*ef5ccd6cSJohn Marino   ssize_t highbound = len / TYPE_LENGTH (char_type);
18695796c8dcSSimon Schubert   struct type *stringtype
18705796c8dcSSimon Schubert     = lookup_string_range_type (char_type, lowbound, highbound + lowbound - 1);
18715796c8dcSSimon Schubert 
18725796c8dcSSimon Schubert   val = allocate_value (stringtype);
18735796c8dcSSimon Schubert   memcpy (value_contents_raw (val), ptr, len);
18745796c8dcSSimon Schubert   return val;
18755796c8dcSSimon Schubert }
18765796c8dcSSimon Schubert 
18775796c8dcSSimon Schubert 
18785796c8dcSSimon Schubert /* See if we can pass arguments in T2 to a function which takes
18795796c8dcSSimon Schubert    arguments of types T1.  T1 is a list of NARGS arguments, and T2 is
18805796c8dcSSimon Schubert    a NULL-terminated vector.  If some arguments need coercion of some
18815796c8dcSSimon Schubert    sort, then the coerced values are written into T2.  Return value is
18825796c8dcSSimon Schubert    0 if the arguments could be matched, or the position at which they
18835796c8dcSSimon Schubert    differ if not.
18845796c8dcSSimon Schubert 
18855796c8dcSSimon Schubert    STATICP is nonzero if the T1 argument list came from a static
18865796c8dcSSimon Schubert    member function.  T2 will still include the ``this'' pointer, but
18875796c8dcSSimon Schubert    it will be skipped.
18885796c8dcSSimon Schubert 
18895796c8dcSSimon Schubert    For non-static member functions, we ignore the first argument,
18905796c8dcSSimon Schubert    which is the type of the instance variable.  This is because we
18915796c8dcSSimon Schubert    want to handle calls with objects from derived classes.  This is
18925796c8dcSSimon Schubert    not entirely correct: we should actually check to make sure that a
18935796c8dcSSimon Schubert    requested operation is type secure, shouldn't we?  FIXME.  */
18945796c8dcSSimon Schubert 
18955796c8dcSSimon Schubert static int
typecmp(int staticp,int varargs,int nargs,struct field t1[],struct value * t2[])18965796c8dcSSimon Schubert typecmp (int staticp, int varargs, int nargs,
18975796c8dcSSimon Schubert 	 struct field t1[], struct value *t2[])
18985796c8dcSSimon Schubert {
18995796c8dcSSimon Schubert   int i;
19005796c8dcSSimon Schubert 
19015796c8dcSSimon Schubert   if (t2 == 0)
19025796c8dcSSimon Schubert     internal_error (__FILE__, __LINE__,
19035796c8dcSSimon Schubert 		    _("typecmp: no argument list"));
19045796c8dcSSimon Schubert 
19055796c8dcSSimon Schubert   /* Skip ``this'' argument if applicable.  T2 will always include
19065796c8dcSSimon Schubert      THIS.  */
19075796c8dcSSimon Schubert   if (staticp)
19085796c8dcSSimon Schubert     t2 ++;
19095796c8dcSSimon Schubert 
19105796c8dcSSimon Schubert   for (i = 0;
19115796c8dcSSimon Schubert        (i < nargs) && TYPE_CODE (t1[i].type) != TYPE_CODE_VOID;
19125796c8dcSSimon Schubert        i++)
19135796c8dcSSimon Schubert     {
19145796c8dcSSimon Schubert       struct type *tt1, *tt2;
19155796c8dcSSimon Schubert 
19165796c8dcSSimon Schubert       if (!t2[i])
19175796c8dcSSimon Schubert 	return i + 1;
19185796c8dcSSimon Schubert 
19195796c8dcSSimon Schubert       tt1 = check_typedef (t1[i].type);
19205796c8dcSSimon Schubert       tt2 = check_typedef (value_type (t2[i]));
19215796c8dcSSimon Schubert 
19225796c8dcSSimon Schubert       if (TYPE_CODE (tt1) == TYPE_CODE_REF
19235796c8dcSSimon Schubert       /* We should be doing hairy argument matching, as below.  */
1924c50c785cSJohn Marino 	  && (TYPE_CODE (check_typedef (TYPE_TARGET_TYPE (tt1)))
1925c50c785cSJohn Marino 	      == TYPE_CODE (tt2)))
19265796c8dcSSimon Schubert 	{
19275796c8dcSSimon Schubert 	  if (TYPE_CODE (tt2) == TYPE_CODE_ARRAY)
19285796c8dcSSimon Schubert 	    t2[i] = value_coerce_array (t2[i]);
19295796c8dcSSimon Schubert 	  else
19305796c8dcSSimon Schubert 	    t2[i] = value_ref (t2[i]);
19315796c8dcSSimon Schubert 	  continue;
19325796c8dcSSimon Schubert 	}
19335796c8dcSSimon Schubert 
19345796c8dcSSimon Schubert       /* djb - 20000715 - Until the new type structure is in the
19355796c8dcSSimon Schubert 	 place, and we can attempt things like implicit conversions,
19365796c8dcSSimon Schubert 	 we need to do this so you can take something like a map<const
19375796c8dcSSimon Schubert 	 char *>, and properly access map["hello"], because the
19385796c8dcSSimon Schubert 	 argument to [] will be a reference to a pointer to a char,
19395796c8dcSSimon Schubert 	 and the argument will be a pointer to a char.  */
19405796c8dcSSimon Schubert       while (TYPE_CODE(tt1) == TYPE_CODE_REF
19415796c8dcSSimon Schubert 	     || TYPE_CODE (tt1) == TYPE_CODE_PTR)
19425796c8dcSSimon Schubert 	{
19435796c8dcSSimon Schubert 	  tt1 = check_typedef( TYPE_TARGET_TYPE(tt1) );
19445796c8dcSSimon Schubert 	}
19455796c8dcSSimon Schubert       while (TYPE_CODE(tt2) == TYPE_CODE_ARRAY
19465796c8dcSSimon Schubert 	     || TYPE_CODE(tt2) == TYPE_CODE_PTR
19475796c8dcSSimon Schubert 	     || TYPE_CODE(tt2) == TYPE_CODE_REF)
19485796c8dcSSimon Schubert 	{
19495796c8dcSSimon Schubert 	  tt2 = check_typedef (TYPE_TARGET_TYPE(tt2));
19505796c8dcSSimon Schubert 	}
19515796c8dcSSimon Schubert       if (TYPE_CODE (tt1) == TYPE_CODE (tt2))
19525796c8dcSSimon Schubert 	continue;
19535796c8dcSSimon Schubert       /* Array to pointer is a `trivial conversion' according to the
19545796c8dcSSimon Schubert 	 ARM.  */
19555796c8dcSSimon Schubert 
19565796c8dcSSimon Schubert       /* We should be doing much hairier argument matching (see
19575796c8dcSSimon Schubert          section 13.2 of the ARM), but as a quick kludge, just check
19585796c8dcSSimon Schubert          for the same type code.  */
19595796c8dcSSimon Schubert       if (TYPE_CODE (t1[i].type) != TYPE_CODE (value_type (t2[i])))
19605796c8dcSSimon Schubert 	return i + 1;
19615796c8dcSSimon Schubert     }
19625796c8dcSSimon Schubert   if (varargs || t2[i] == NULL)
19635796c8dcSSimon Schubert     return 0;
19645796c8dcSSimon Schubert   return i + 1;
19655796c8dcSSimon Schubert }
19665796c8dcSSimon Schubert 
1967*ef5ccd6cSJohn Marino /* Helper class for do_search_struct_field that updates *RESULT_PTR
1968*ef5ccd6cSJohn Marino    and *LAST_BOFFSET, and possibly throws an exception if the field
1969*ef5ccd6cSJohn Marino    search has yielded ambiguous results.  */
19705796c8dcSSimon Schubert 
1971*ef5ccd6cSJohn Marino static void
update_search_result(struct value ** result_ptr,struct value * v,int * last_boffset,int boffset,const char * name,struct type * type)1972*ef5ccd6cSJohn Marino update_search_result (struct value **result_ptr, struct value *v,
1973*ef5ccd6cSJohn Marino 		      int *last_boffset, int boffset,
1974*ef5ccd6cSJohn Marino 		      const char *name, struct type *type)
1975*ef5ccd6cSJohn Marino {
1976*ef5ccd6cSJohn Marino   if (v != NULL)
1977*ef5ccd6cSJohn Marino     {
1978*ef5ccd6cSJohn Marino       if (*result_ptr != NULL
1979*ef5ccd6cSJohn Marino 	  /* The result is not ambiguous if all the classes that are
1980*ef5ccd6cSJohn Marino 	     found occupy the same space.  */
1981*ef5ccd6cSJohn Marino 	  && *last_boffset != boffset)
1982*ef5ccd6cSJohn Marino 	error (_("base class '%s' is ambiguous in type '%s'"),
1983*ef5ccd6cSJohn Marino 	       name, TYPE_SAFE_NAME (type));
1984*ef5ccd6cSJohn Marino       *result_ptr = v;
1985*ef5ccd6cSJohn Marino       *last_boffset = boffset;
1986*ef5ccd6cSJohn Marino     }
1987*ef5ccd6cSJohn Marino }
19885796c8dcSSimon Schubert 
1989*ef5ccd6cSJohn Marino /* A helper for search_struct_field.  This does all the work; most
1990*ef5ccd6cSJohn Marino    arguments are as passed to search_struct_field.  The result is
1991*ef5ccd6cSJohn Marino    stored in *RESULT_PTR, which must be initialized to NULL.
1992*ef5ccd6cSJohn Marino    OUTERMOST_TYPE is the type of the initial type passed to
1993*ef5ccd6cSJohn Marino    search_struct_field; this is used for error reporting when the
1994*ef5ccd6cSJohn Marino    lookup is ambiguous.  */
1995*ef5ccd6cSJohn Marino 
1996*ef5ccd6cSJohn Marino static void
do_search_struct_field(const char * name,struct value * arg1,int offset,struct type * type,int looking_for_baseclass,struct value ** result_ptr,int * last_boffset,struct type * outermost_type)1997*ef5ccd6cSJohn Marino do_search_struct_field (const char *name, struct value *arg1, int offset,
1998*ef5ccd6cSJohn Marino 			struct type *type, int looking_for_baseclass,
1999*ef5ccd6cSJohn Marino 			struct value **result_ptr,
2000*ef5ccd6cSJohn Marino 			int *last_boffset,
2001*ef5ccd6cSJohn Marino 			struct type *outermost_type)
20025796c8dcSSimon Schubert {
20035796c8dcSSimon Schubert   int i;
2004cf7f2e2dSJohn Marino   int nbases;
20055796c8dcSSimon Schubert 
20065796c8dcSSimon Schubert   CHECK_TYPEDEF (type);
2007cf7f2e2dSJohn Marino   nbases = TYPE_N_BASECLASSES (type);
20085796c8dcSSimon Schubert 
20095796c8dcSSimon Schubert   if (!looking_for_baseclass)
20105796c8dcSSimon Schubert     for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
20115796c8dcSSimon Schubert       {
2012*ef5ccd6cSJohn Marino 	const char *t_field_name = TYPE_FIELD_NAME (type, i);
20135796c8dcSSimon Schubert 
20145796c8dcSSimon Schubert 	if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
20155796c8dcSSimon Schubert 	  {
20165796c8dcSSimon Schubert 	    struct value *v;
2017cf7f2e2dSJohn Marino 
20185796c8dcSSimon Schubert 	    if (field_is_static (&TYPE_FIELD (type, i)))
20195796c8dcSSimon Schubert 	      {
20205796c8dcSSimon Schubert 		v = value_static_field (type, i);
20215796c8dcSSimon Schubert 		if (v == 0)
2022c50c785cSJohn Marino 		  error (_("field %s is nonexistent or "
2023c50c785cSJohn Marino 			   "has been optimized out"),
20245796c8dcSSimon Schubert 			 name);
20255796c8dcSSimon Schubert 	      }
20265796c8dcSSimon Schubert 	    else
20275796c8dcSSimon Schubert 	      v = value_primitive_field (arg1, offset, i, type);
2028*ef5ccd6cSJohn Marino 	    *result_ptr = v;
2029*ef5ccd6cSJohn Marino 	    return;
20305796c8dcSSimon Schubert 	  }
20315796c8dcSSimon Schubert 
20325796c8dcSSimon Schubert 	if (t_field_name
20335796c8dcSSimon Schubert 	    && (t_field_name[0] == '\0'
20345796c8dcSSimon Schubert 		|| (TYPE_CODE (type) == TYPE_CODE_UNION
20355796c8dcSSimon Schubert 		    && (strcmp_iw (t_field_name, "else") == 0))))
20365796c8dcSSimon Schubert 	  {
20375796c8dcSSimon Schubert 	    struct type *field_type = TYPE_FIELD_TYPE (type, i);
2038cf7f2e2dSJohn Marino 
20395796c8dcSSimon Schubert 	    if (TYPE_CODE (field_type) == TYPE_CODE_UNION
20405796c8dcSSimon Schubert 		|| TYPE_CODE (field_type) == TYPE_CODE_STRUCT)
20415796c8dcSSimon Schubert 	      {
20425796c8dcSSimon Schubert 		/* Look for a match through the fields of an anonymous
20435796c8dcSSimon Schubert 		   union, or anonymous struct.  C++ provides anonymous
20445796c8dcSSimon Schubert 		   unions.
20455796c8dcSSimon Schubert 
20465796c8dcSSimon Schubert 		   In the GNU Chill (now deleted from GDB)
20475796c8dcSSimon Schubert 		   implementation of variant record types, each
20485796c8dcSSimon Schubert 		   <alternative field> has an (anonymous) union type,
20495796c8dcSSimon Schubert 		   each member of the union represents a <variant
20505796c8dcSSimon Schubert 		   alternative>.  Each <variant alternative> is
20515796c8dcSSimon Schubert 		   represented as a struct, with a member for each
20525796c8dcSSimon Schubert 		   <variant field>.  */
20535796c8dcSSimon Schubert 
2054*ef5ccd6cSJohn Marino 		struct value *v = NULL;
20555796c8dcSSimon Schubert 		int new_offset = offset;
20565796c8dcSSimon Schubert 
20575796c8dcSSimon Schubert 		/* This is pretty gross.  In G++, the offset in an
20585796c8dcSSimon Schubert 		   anonymous union is relative to the beginning of the
20595796c8dcSSimon Schubert 		   enclosing struct.  In the GNU Chill (now deleted
20605796c8dcSSimon Schubert 		   from GDB) implementation of variant records, the
20615796c8dcSSimon Schubert 		   bitpos is zero in an anonymous union field, so we
20625796c8dcSSimon Schubert 		   have to add the offset of the union here.  */
20635796c8dcSSimon Schubert 		if (TYPE_CODE (field_type) == TYPE_CODE_STRUCT
20645796c8dcSSimon Schubert 		    || (TYPE_NFIELDS (field_type) > 0
20655796c8dcSSimon Schubert 			&& TYPE_FIELD_BITPOS (field_type, 0) == 0))
20665796c8dcSSimon Schubert 		  new_offset += TYPE_FIELD_BITPOS (type, i) / 8;
20675796c8dcSSimon Schubert 
2068*ef5ccd6cSJohn Marino 		do_search_struct_field (name, arg1, new_offset,
20695796c8dcSSimon Schubert 					field_type,
2070*ef5ccd6cSJohn Marino 					looking_for_baseclass, &v,
2071*ef5ccd6cSJohn Marino 					last_boffset,
2072*ef5ccd6cSJohn Marino 					outermost_type);
20735796c8dcSSimon Schubert 		if (v)
2074*ef5ccd6cSJohn Marino 		  {
2075*ef5ccd6cSJohn Marino 		    *result_ptr = v;
2076*ef5ccd6cSJohn Marino 		    return;
2077*ef5ccd6cSJohn Marino 		  }
20785796c8dcSSimon Schubert 	      }
20795796c8dcSSimon Schubert 	  }
20805796c8dcSSimon Schubert       }
20815796c8dcSSimon Schubert 
20825796c8dcSSimon Schubert   for (i = 0; i < nbases; i++)
20835796c8dcSSimon Schubert     {
2084*ef5ccd6cSJohn Marino       struct value *v = NULL;
20855796c8dcSSimon Schubert       struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
20865796c8dcSSimon Schubert       /* If we are looking for baseclasses, this is what we get when
20875796c8dcSSimon Schubert          we hit them.  But it could happen that the base part's member
20885796c8dcSSimon Schubert          name is not yet filled in.  */
20895796c8dcSSimon Schubert       int found_baseclass = (looking_for_baseclass
20905796c8dcSSimon Schubert 			     && TYPE_BASECLASS_NAME (type, i) != NULL
20915796c8dcSSimon Schubert 			     && (strcmp_iw (name,
20925796c8dcSSimon Schubert 					    TYPE_BASECLASS_NAME (type,
20935796c8dcSSimon Schubert 								 i)) == 0));
2094*ef5ccd6cSJohn Marino       int boffset = value_embedded_offset (arg1) + offset;
20955796c8dcSSimon Schubert 
20965796c8dcSSimon Schubert       if (BASETYPE_VIA_VIRTUAL (type, i))
20975796c8dcSSimon Schubert 	{
20985796c8dcSSimon Schubert 	  struct value *v2;
20995796c8dcSSimon Schubert 
21005796c8dcSSimon Schubert 	  boffset = baseclass_offset (type, i,
2101c50c785cSJohn Marino 				      value_contents_for_printing (arg1),
2102c50c785cSJohn Marino 				      value_embedded_offset (arg1) + offset,
2103c50c785cSJohn Marino 				      value_address (arg1),
2104c50c785cSJohn Marino 				      arg1);
21055796c8dcSSimon Schubert 
21065796c8dcSSimon Schubert 	  /* The virtual base class pointer might have been clobbered
21075796c8dcSSimon Schubert 	     by the user program.  Make sure that it still points to a
21085796c8dcSSimon Schubert 	     valid memory location.  */
21095796c8dcSSimon Schubert 
2110cf7f2e2dSJohn Marino 	  boffset += value_embedded_offset (arg1) + offset;
2111cf7f2e2dSJohn Marino 	  if (boffset < 0
2112cf7f2e2dSJohn Marino 	      || boffset >= TYPE_LENGTH (value_enclosing_type (arg1)))
21135796c8dcSSimon Schubert 	    {
21145796c8dcSSimon Schubert 	      CORE_ADDR base_addr;
21155796c8dcSSimon Schubert 
21165796c8dcSSimon Schubert 	      v2  = allocate_value (basetype);
21175796c8dcSSimon Schubert 	      base_addr = value_address (arg1) + boffset;
21185796c8dcSSimon Schubert 	      if (target_read_memory (base_addr,
21195796c8dcSSimon Schubert 				      value_contents_raw (v2),
21205796c8dcSSimon Schubert 				      TYPE_LENGTH (basetype)) != 0)
21215796c8dcSSimon Schubert 		error (_("virtual baseclass botch"));
21225796c8dcSSimon Schubert 	      VALUE_LVAL (v2) = lval_memory;
21235796c8dcSSimon Schubert 	      set_value_address (v2, base_addr);
21245796c8dcSSimon Schubert 	    }
21255796c8dcSSimon Schubert 	  else
21265796c8dcSSimon Schubert 	    {
2127cf7f2e2dSJohn Marino 	      v2 = value_copy (arg1);
2128cf7f2e2dSJohn Marino 	      deprecated_set_value_type (v2, basetype);
2129cf7f2e2dSJohn Marino 	      set_value_embedded_offset (v2, boffset);
21305796c8dcSSimon Schubert 	    }
21315796c8dcSSimon Schubert 
21325796c8dcSSimon Schubert 	  if (found_baseclass)
2133*ef5ccd6cSJohn Marino 	    v = v2;
2134*ef5ccd6cSJohn Marino 	  else
2135*ef5ccd6cSJohn Marino 	    {
2136*ef5ccd6cSJohn Marino 	      do_search_struct_field (name, v2, 0,
21375796c8dcSSimon Schubert 				      TYPE_BASECLASS (type, i),
2138*ef5ccd6cSJohn Marino 				      looking_for_baseclass,
2139*ef5ccd6cSJohn Marino 				      result_ptr, last_boffset,
2140*ef5ccd6cSJohn Marino 				      outermost_type);
2141*ef5ccd6cSJohn Marino 	    }
21425796c8dcSSimon Schubert 	}
21435796c8dcSSimon Schubert       else if (found_baseclass)
21445796c8dcSSimon Schubert 	v = value_primitive_field (arg1, offset, i, type);
21455796c8dcSSimon Schubert       else
2146*ef5ccd6cSJohn Marino 	{
2147*ef5ccd6cSJohn Marino 	  do_search_struct_field (name, arg1,
21485796c8dcSSimon Schubert 				  offset + TYPE_BASECLASS_BITPOS (type,
21495796c8dcSSimon Schubert 								  i) / 8,
2150*ef5ccd6cSJohn Marino 				  basetype, looking_for_baseclass,
2151*ef5ccd6cSJohn Marino 				  result_ptr, last_boffset,
2152*ef5ccd6cSJohn Marino 				  outermost_type);
21535796c8dcSSimon Schubert 	}
2154*ef5ccd6cSJohn Marino 
2155*ef5ccd6cSJohn Marino       update_search_result (result_ptr, v, last_boffset,
2156*ef5ccd6cSJohn Marino 			    boffset, name, outermost_type);
2157*ef5ccd6cSJohn Marino     }
2158*ef5ccd6cSJohn Marino }
2159*ef5ccd6cSJohn Marino 
2160*ef5ccd6cSJohn Marino /* Helper function used by value_struct_elt to recurse through
2161*ef5ccd6cSJohn Marino    baseclasses.  Look for a field NAME in ARG1.  Adjust the address of
2162*ef5ccd6cSJohn Marino    ARG1 by OFFSET bytes, and search in it assuming it has (class) type
2163*ef5ccd6cSJohn Marino    TYPE.  If found, return value, else return NULL.
2164*ef5ccd6cSJohn Marino 
2165*ef5ccd6cSJohn Marino    If LOOKING_FOR_BASECLASS, then instead of looking for struct
2166*ef5ccd6cSJohn Marino    fields, look for a baseclass named NAME.  */
2167*ef5ccd6cSJohn Marino 
2168*ef5ccd6cSJohn Marino static struct value *
search_struct_field(const char * name,struct value * arg1,int offset,struct type * type,int looking_for_baseclass)2169*ef5ccd6cSJohn Marino search_struct_field (const char *name, struct value *arg1, int offset,
2170*ef5ccd6cSJohn Marino 		     struct type *type, int looking_for_baseclass)
2171*ef5ccd6cSJohn Marino {
2172*ef5ccd6cSJohn Marino   struct value *result = NULL;
2173*ef5ccd6cSJohn Marino   int boffset = 0;
2174*ef5ccd6cSJohn Marino 
2175*ef5ccd6cSJohn Marino   do_search_struct_field (name, arg1, offset, type, looking_for_baseclass,
2176*ef5ccd6cSJohn Marino 			  &result, &boffset, type);
2177*ef5ccd6cSJohn Marino   return result;
21785796c8dcSSimon Schubert }
21795796c8dcSSimon Schubert 
21805796c8dcSSimon Schubert /* Helper function used by value_struct_elt to recurse through
21815796c8dcSSimon Schubert    baseclasses.  Look for a field NAME in ARG1.  Adjust the address of
21825796c8dcSSimon Schubert    ARG1 by OFFSET bytes, and search in it assuming it has (class) type
21835796c8dcSSimon Schubert    TYPE.
21845796c8dcSSimon Schubert 
21855796c8dcSSimon Schubert    If found, return value, else if name matched and args not return
21865796c8dcSSimon Schubert    (value) -1, else return NULL.  */
21875796c8dcSSimon Schubert 
21885796c8dcSSimon Schubert static struct value *
search_struct_method(const char * name,struct value ** arg1p,struct value ** args,int offset,int * static_memfuncp,struct type * type)2189cf7f2e2dSJohn Marino search_struct_method (const char *name, struct value **arg1p,
21905796c8dcSSimon Schubert 		      struct value **args, int offset,
21915796c8dcSSimon Schubert 		      int *static_memfuncp, struct type *type)
21925796c8dcSSimon Schubert {
21935796c8dcSSimon Schubert   int i;
21945796c8dcSSimon Schubert   struct value *v;
21955796c8dcSSimon Schubert   int name_matched = 0;
21965796c8dcSSimon Schubert   char dem_opname[64];
21975796c8dcSSimon Schubert 
21985796c8dcSSimon Schubert   CHECK_TYPEDEF (type);
21995796c8dcSSimon Schubert   for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
22005796c8dcSSimon Schubert     {
2201*ef5ccd6cSJohn Marino       const char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
2202cf7f2e2dSJohn Marino 
2203c50c785cSJohn Marino       /* FIXME!  May need to check for ARM demangling here.  */
22045796c8dcSSimon Schubert       if (strncmp (t_field_name, "__", 2) == 0 ||
22055796c8dcSSimon Schubert 	  strncmp (t_field_name, "op", 2) == 0 ||
22065796c8dcSSimon Schubert 	  strncmp (t_field_name, "type", 4) == 0)
22075796c8dcSSimon Schubert 	{
22085796c8dcSSimon Schubert 	  if (cplus_demangle_opname (t_field_name, dem_opname, DMGL_ANSI))
22095796c8dcSSimon Schubert 	    t_field_name = dem_opname;
22105796c8dcSSimon Schubert 	  else if (cplus_demangle_opname (t_field_name, dem_opname, 0))
22115796c8dcSSimon Schubert 	    t_field_name = dem_opname;
22125796c8dcSSimon Schubert 	}
22135796c8dcSSimon Schubert       if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
22145796c8dcSSimon Schubert 	{
22155796c8dcSSimon Schubert 	  int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1;
22165796c8dcSSimon Schubert 	  struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
22175796c8dcSSimon Schubert 
2218cf7f2e2dSJohn Marino 	  name_matched = 1;
22195796c8dcSSimon Schubert 	  check_stub_method_group (type, i);
22205796c8dcSSimon Schubert 	  if (j > 0 && args == 0)
2221c50c785cSJohn Marino 	    error (_("cannot resolve overloaded method "
2222c50c785cSJohn Marino 		     "`%s': no arguments supplied"), name);
22235796c8dcSSimon Schubert 	  else if (j == 0 && args == 0)
22245796c8dcSSimon Schubert 	    {
22255796c8dcSSimon Schubert 	      v = value_fn_field (arg1p, f, j, type, offset);
22265796c8dcSSimon Schubert 	      if (v != NULL)
22275796c8dcSSimon Schubert 		return v;
22285796c8dcSSimon Schubert 	    }
22295796c8dcSSimon Schubert 	  else
22305796c8dcSSimon Schubert 	    while (j >= 0)
22315796c8dcSSimon Schubert 	      {
22325796c8dcSSimon Schubert 		if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j),
22335796c8dcSSimon Schubert 			      TYPE_VARARGS (TYPE_FN_FIELD_TYPE (f, j)),
22345796c8dcSSimon Schubert 			      TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f, j)),
22355796c8dcSSimon Schubert 			      TYPE_FN_FIELD_ARGS (f, j), args))
22365796c8dcSSimon Schubert 		  {
22375796c8dcSSimon Schubert 		    if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
22385796c8dcSSimon Schubert 		      return value_virtual_fn_field (arg1p, f, j,
22395796c8dcSSimon Schubert 						     type, offset);
22405796c8dcSSimon Schubert 		    if (TYPE_FN_FIELD_STATIC_P (f, j)
22415796c8dcSSimon Schubert 			&& static_memfuncp)
22425796c8dcSSimon Schubert 		      *static_memfuncp = 1;
22435796c8dcSSimon Schubert 		    v = value_fn_field (arg1p, f, j, type, offset);
22445796c8dcSSimon Schubert 		    if (v != NULL)
22455796c8dcSSimon Schubert 		      return v;
22465796c8dcSSimon Schubert 		  }
22475796c8dcSSimon Schubert 		j--;
22485796c8dcSSimon Schubert 	      }
22495796c8dcSSimon Schubert 	}
22505796c8dcSSimon Schubert     }
22515796c8dcSSimon Schubert 
22525796c8dcSSimon Schubert   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
22535796c8dcSSimon Schubert     {
22545796c8dcSSimon Schubert       int base_offset;
2255c50c785cSJohn Marino       int this_offset;
22565796c8dcSSimon Schubert 
22575796c8dcSSimon Schubert       if (BASETYPE_VIA_VIRTUAL (type, i))
22585796c8dcSSimon Schubert 	{
22595796c8dcSSimon Schubert 	  struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
2260c50c785cSJohn Marino 	  struct value *base_val;
22615796c8dcSSimon Schubert 	  const gdb_byte *base_valaddr;
22625796c8dcSSimon Schubert 
22635796c8dcSSimon Schubert 	  /* The virtual base class pointer might have been
22645796c8dcSSimon Schubert 	     clobbered by the user program.  Make sure that it
22655796c8dcSSimon Schubert 	    still points to a valid memory location.  */
22665796c8dcSSimon Schubert 
22675796c8dcSSimon Schubert 	  if (offset < 0 || offset >= TYPE_LENGTH (type))
22685796c8dcSSimon Schubert 	    {
2269*ef5ccd6cSJohn Marino 	      gdb_byte *tmp;
2270*ef5ccd6cSJohn Marino 	      struct cleanup *back_to;
2271*ef5ccd6cSJohn Marino 	      CORE_ADDR address;
2272*ef5ccd6cSJohn Marino 
2273*ef5ccd6cSJohn Marino 	      tmp = xmalloc (TYPE_LENGTH (baseclass));
2274*ef5ccd6cSJohn Marino 	      back_to = make_cleanup (xfree, tmp);
2275*ef5ccd6cSJohn Marino 	      address = value_address (*arg1p);
2276cf7f2e2dSJohn Marino 
2277c50c785cSJohn Marino 	      if (target_read_memory (address + offset,
22785796c8dcSSimon Schubert 				      tmp, TYPE_LENGTH (baseclass)) != 0)
22795796c8dcSSimon Schubert 		error (_("virtual baseclass botch"));
2280c50c785cSJohn Marino 
2281c50c785cSJohn Marino 	      base_val = value_from_contents_and_address (baseclass,
2282c50c785cSJohn Marino 							  tmp,
2283c50c785cSJohn Marino 							  address + offset);
2284c50c785cSJohn Marino 	      base_valaddr = value_contents_for_printing (base_val);
2285c50c785cSJohn Marino 	      this_offset = 0;
2286*ef5ccd6cSJohn Marino 	      do_cleanups (back_to);
22875796c8dcSSimon Schubert 	    }
22885796c8dcSSimon Schubert 	  else
2289c50c785cSJohn Marino 	    {
2290c50c785cSJohn Marino 	      base_val = *arg1p;
2291c50c785cSJohn Marino 	      base_valaddr = value_contents_for_printing (*arg1p);
2292c50c785cSJohn Marino 	      this_offset = offset;
2293c50c785cSJohn Marino 	    }
22945796c8dcSSimon Schubert 
22955796c8dcSSimon Schubert 	  base_offset = baseclass_offset (type, i, base_valaddr,
2296c50c785cSJohn Marino 					  this_offset, value_address (base_val),
2297c50c785cSJohn Marino 					  base_val);
22985796c8dcSSimon Schubert 	}
22995796c8dcSSimon Schubert       else
23005796c8dcSSimon Schubert 	{
23015796c8dcSSimon Schubert 	  base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
23025796c8dcSSimon Schubert 	}
23035796c8dcSSimon Schubert       v = search_struct_method (name, arg1p, args, base_offset + offset,
23045796c8dcSSimon Schubert 				static_memfuncp, TYPE_BASECLASS (type, i));
23055796c8dcSSimon Schubert       if (v == (struct value *) - 1)
23065796c8dcSSimon Schubert 	{
23075796c8dcSSimon Schubert 	  name_matched = 1;
23085796c8dcSSimon Schubert 	}
23095796c8dcSSimon Schubert       else if (v)
23105796c8dcSSimon Schubert 	{
23115796c8dcSSimon Schubert 	  /* FIXME-bothner:  Why is this commented out?  Why is it here?  */
23125796c8dcSSimon Schubert 	  /* *arg1p = arg1_tmp; */
23135796c8dcSSimon Schubert 	  return v;
23145796c8dcSSimon Schubert 	}
23155796c8dcSSimon Schubert     }
23165796c8dcSSimon Schubert   if (name_matched)
23175796c8dcSSimon Schubert     return (struct value *) - 1;
23185796c8dcSSimon Schubert   else
23195796c8dcSSimon Schubert     return NULL;
23205796c8dcSSimon Schubert }
23215796c8dcSSimon Schubert 
23225796c8dcSSimon Schubert /* Given *ARGP, a value of type (pointer to a)* structure/union,
23235796c8dcSSimon Schubert    extract the component named NAME from the ultimate target
23245796c8dcSSimon Schubert    structure/union and return it as a value with its appropriate type.
23255796c8dcSSimon Schubert    ERR is used in the error message if *ARGP's type is wrong.
23265796c8dcSSimon Schubert 
23275796c8dcSSimon Schubert    C++: ARGS is a list of argument types to aid in the selection of
23285796c8dcSSimon Schubert    an appropriate method.  Also, handle derived types.
23295796c8dcSSimon Schubert 
23305796c8dcSSimon Schubert    STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
23315796c8dcSSimon Schubert    where the truthvalue of whether the function that was resolved was
23325796c8dcSSimon Schubert    a static member function or not is stored.
23335796c8dcSSimon Schubert 
23345796c8dcSSimon Schubert    ERR is an error message to be printed in case the field is not
23355796c8dcSSimon Schubert    found.  */
23365796c8dcSSimon Schubert 
23375796c8dcSSimon Schubert struct value *
value_struct_elt(struct value ** argp,struct value ** args,const char * name,int * static_memfuncp,const char * err)23385796c8dcSSimon Schubert value_struct_elt (struct value **argp, struct value **args,
2339cf7f2e2dSJohn Marino 		  const char *name, int *static_memfuncp, const char *err)
23405796c8dcSSimon Schubert {
23415796c8dcSSimon Schubert   struct type *t;
23425796c8dcSSimon Schubert   struct value *v;
23435796c8dcSSimon Schubert 
23445796c8dcSSimon Schubert   *argp = coerce_array (*argp);
23455796c8dcSSimon Schubert 
23465796c8dcSSimon Schubert   t = check_typedef (value_type (*argp));
23475796c8dcSSimon Schubert 
23485796c8dcSSimon Schubert   /* Follow pointers until we get to a non-pointer.  */
23495796c8dcSSimon Schubert 
23505796c8dcSSimon Schubert   while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
23515796c8dcSSimon Schubert     {
23525796c8dcSSimon Schubert       *argp = value_ind (*argp);
23535796c8dcSSimon Schubert       /* Don't coerce fn pointer to fn and then back again!  */
23545796c8dcSSimon Schubert       if (TYPE_CODE (value_type (*argp)) != TYPE_CODE_FUNC)
23555796c8dcSSimon Schubert 	*argp = coerce_array (*argp);
23565796c8dcSSimon Schubert       t = check_typedef (value_type (*argp));
23575796c8dcSSimon Schubert     }
23585796c8dcSSimon Schubert 
23595796c8dcSSimon Schubert   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
23605796c8dcSSimon Schubert       && TYPE_CODE (t) != TYPE_CODE_UNION)
2361c50c785cSJohn Marino     error (_("Attempt to extract a component of a value that is not a %s."),
2362c50c785cSJohn Marino 	   err);
23635796c8dcSSimon Schubert 
23645796c8dcSSimon Schubert   /* Assume it's not, unless we see that it is.  */
23655796c8dcSSimon Schubert   if (static_memfuncp)
23665796c8dcSSimon Schubert     *static_memfuncp = 0;
23675796c8dcSSimon Schubert 
23685796c8dcSSimon Schubert   if (!args)
23695796c8dcSSimon Schubert     {
23705796c8dcSSimon Schubert       /* if there are no arguments ...do this...  */
23715796c8dcSSimon Schubert 
23725796c8dcSSimon Schubert       /* Try as a field first, because if we succeed, there is less
23735796c8dcSSimon Schubert          work to be done.  */
23745796c8dcSSimon Schubert       v = search_struct_field (name, *argp, 0, t, 0);
23755796c8dcSSimon Schubert       if (v)
23765796c8dcSSimon Schubert 	return v;
23775796c8dcSSimon Schubert 
23785796c8dcSSimon Schubert       /* C++: If it was not found as a data field, then try to
23795796c8dcSSimon Schubert          return it as a pointer to a method.  */
23805796c8dcSSimon Schubert       v = search_struct_method (name, argp, args, 0,
23815796c8dcSSimon Schubert 				static_memfuncp, t);
23825796c8dcSSimon Schubert 
23835796c8dcSSimon Schubert       if (v == (struct value *) - 1)
23845796c8dcSSimon Schubert 	error (_("Cannot take address of method %s."), name);
23855796c8dcSSimon Schubert       else if (v == 0)
23865796c8dcSSimon Schubert 	{
23875796c8dcSSimon Schubert 	  if (TYPE_NFN_FIELDS (t))
23885796c8dcSSimon Schubert 	    error (_("There is no member or method named %s."), name);
23895796c8dcSSimon Schubert 	  else
23905796c8dcSSimon Schubert 	    error (_("There is no member named %s."), name);
23915796c8dcSSimon Schubert 	}
23925796c8dcSSimon Schubert       return v;
23935796c8dcSSimon Schubert     }
23945796c8dcSSimon Schubert 
23955796c8dcSSimon Schubert     v = search_struct_method (name, argp, args, 0,
23965796c8dcSSimon Schubert 			      static_memfuncp, t);
23975796c8dcSSimon Schubert 
23985796c8dcSSimon Schubert   if (v == (struct value *) - 1)
23995796c8dcSSimon Schubert     {
2400c50c785cSJohn Marino       error (_("One of the arguments you tried to pass to %s could not "
2401c50c785cSJohn Marino 	       "be converted to what the function wants."), name);
24025796c8dcSSimon Schubert     }
24035796c8dcSSimon Schubert   else if (v == 0)
24045796c8dcSSimon Schubert     {
24055796c8dcSSimon Schubert       /* See if user tried to invoke data as function.  If so, hand it
24065796c8dcSSimon Schubert          back.  If it's not callable (i.e., a pointer to function),
24075796c8dcSSimon Schubert          gdb should give an error.  */
24085796c8dcSSimon Schubert       v = search_struct_field (name, *argp, 0, t, 0);
24095796c8dcSSimon Schubert       /* If we found an ordinary field, then it is not a method call.
24105796c8dcSSimon Schubert 	 So, treat it as if it were a static member function.  */
24115796c8dcSSimon Schubert       if (v && static_memfuncp)
24125796c8dcSSimon Schubert 	*static_memfuncp = 1;
24135796c8dcSSimon Schubert     }
24145796c8dcSSimon Schubert 
24155796c8dcSSimon Schubert   if (!v)
2416c50c785cSJohn Marino     throw_error (NOT_FOUND_ERROR,
2417c50c785cSJohn Marino                  _("Structure has no component named %s."), name);
24185796c8dcSSimon Schubert   return v;
24195796c8dcSSimon Schubert }
24205796c8dcSSimon Schubert 
24215796c8dcSSimon Schubert /* Search through the methods of an object (and its bases) to find a
24225796c8dcSSimon Schubert    specified method.  Return the pointer to the fn_field list of
24235796c8dcSSimon Schubert    overloaded instances.
24245796c8dcSSimon Schubert 
24255796c8dcSSimon Schubert    Helper function for value_find_oload_list.
24265796c8dcSSimon Schubert    ARGP is a pointer to a pointer to a value (the object).
24275796c8dcSSimon Schubert    METHOD is a string containing the method name.
24285796c8dcSSimon Schubert    OFFSET is the offset within the value.
24295796c8dcSSimon Schubert    TYPE is the assumed type of the object.
24305796c8dcSSimon Schubert    NUM_FNS is the number of overloaded instances.
24315796c8dcSSimon Schubert    BASETYPE is set to the actual type of the subobject where the
24325796c8dcSSimon Schubert       method is found.
2433c50c785cSJohn Marino    BOFFSET is the offset of the base subobject where the method is found.  */
24345796c8dcSSimon Schubert 
24355796c8dcSSimon Schubert static struct fn_field *
find_method_list(struct value ** argp,const char * method,int offset,struct type * type,int * num_fns,struct type ** basetype,int * boffset)2436cf7f2e2dSJohn Marino find_method_list (struct value **argp, const char *method,
24375796c8dcSSimon Schubert 		  int offset, struct type *type, int *num_fns,
24385796c8dcSSimon Schubert 		  struct type **basetype, int *boffset)
24395796c8dcSSimon Schubert {
24405796c8dcSSimon Schubert   int i;
24415796c8dcSSimon Schubert   struct fn_field *f;
24425796c8dcSSimon Schubert   CHECK_TYPEDEF (type);
24435796c8dcSSimon Schubert 
24445796c8dcSSimon Schubert   *num_fns = 0;
24455796c8dcSSimon Schubert 
24465796c8dcSSimon Schubert   /* First check in object itself.  */
24475796c8dcSSimon Schubert   for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
24485796c8dcSSimon Schubert     {
24495796c8dcSSimon Schubert       /* pai: FIXME What about operators and type conversions?  */
2450*ef5ccd6cSJohn Marino       const char *fn_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
2451cf7f2e2dSJohn Marino 
24525796c8dcSSimon Schubert       if (fn_field_name && (strcmp_iw (fn_field_name, method) == 0))
24535796c8dcSSimon Schubert 	{
24545796c8dcSSimon Schubert 	  int len = TYPE_FN_FIELDLIST_LENGTH (type, i);
24555796c8dcSSimon Schubert 	  struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
24565796c8dcSSimon Schubert 
24575796c8dcSSimon Schubert 	  *num_fns = len;
24585796c8dcSSimon Schubert 	  *basetype = type;
24595796c8dcSSimon Schubert 	  *boffset = offset;
24605796c8dcSSimon Schubert 
24615796c8dcSSimon Schubert 	  /* Resolve any stub methods.  */
24625796c8dcSSimon Schubert 	  check_stub_method_group (type, i);
24635796c8dcSSimon Schubert 
24645796c8dcSSimon Schubert 	  return f;
24655796c8dcSSimon Schubert 	}
24665796c8dcSSimon Schubert     }
24675796c8dcSSimon Schubert 
24685796c8dcSSimon Schubert   /* Not found in object, check in base subobjects.  */
24695796c8dcSSimon Schubert   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
24705796c8dcSSimon Schubert     {
24715796c8dcSSimon Schubert       int base_offset;
2472cf7f2e2dSJohn Marino 
24735796c8dcSSimon Schubert       if (BASETYPE_VIA_VIRTUAL (type, i))
24745796c8dcSSimon Schubert 	{
24755796c8dcSSimon Schubert 	  base_offset = baseclass_offset (type, i,
2476c50c785cSJohn Marino 					  value_contents_for_printing (*argp),
2477c50c785cSJohn Marino 					  value_offset (*argp) + offset,
2478c50c785cSJohn Marino 					  value_address (*argp), *argp);
24795796c8dcSSimon Schubert 	}
24805796c8dcSSimon Schubert       else /* Non-virtual base, simply use bit position from debug
24815796c8dcSSimon Schubert 	      info.  */
24825796c8dcSSimon Schubert 	{
24835796c8dcSSimon Schubert 	  base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
24845796c8dcSSimon Schubert 	}
24855796c8dcSSimon Schubert       f = find_method_list (argp, method, base_offset + offset,
24865796c8dcSSimon Schubert 			    TYPE_BASECLASS (type, i), num_fns,
24875796c8dcSSimon Schubert 			    basetype, boffset);
24885796c8dcSSimon Schubert       if (f)
24895796c8dcSSimon Schubert 	return f;
24905796c8dcSSimon Schubert     }
24915796c8dcSSimon Schubert   return NULL;
24925796c8dcSSimon Schubert }
24935796c8dcSSimon Schubert 
24945796c8dcSSimon Schubert /* Return the list of overloaded methods of a specified name.
24955796c8dcSSimon Schubert 
24965796c8dcSSimon Schubert    ARGP is a pointer to a pointer to a value (the object).
24975796c8dcSSimon Schubert    METHOD is the method name.
24985796c8dcSSimon Schubert    OFFSET is the offset within the value contents.
24995796c8dcSSimon Schubert    NUM_FNS is the number of overloaded instances.
25005796c8dcSSimon Schubert    BASETYPE is set to the type of the base subobject that defines the
25015796c8dcSSimon Schubert       method.
2502c50c785cSJohn Marino    BOFFSET is the offset of the base subobject which defines the method.  */
25035796c8dcSSimon Schubert 
2504*ef5ccd6cSJohn Marino static struct fn_field *
value_find_oload_method_list(struct value ** argp,const char * method,int offset,int * num_fns,struct type ** basetype,int * boffset)2505cf7f2e2dSJohn Marino value_find_oload_method_list (struct value **argp, const char *method,
25065796c8dcSSimon Schubert 			      int offset, int *num_fns,
25075796c8dcSSimon Schubert 			      struct type **basetype, int *boffset)
25085796c8dcSSimon Schubert {
25095796c8dcSSimon Schubert   struct type *t;
25105796c8dcSSimon Schubert 
25115796c8dcSSimon Schubert   t = check_typedef (value_type (*argp));
25125796c8dcSSimon Schubert 
25135796c8dcSSimon Schubert   /* Code snarfed from value_struct_elt.  */
25145796c8dcSSimon Schubert   while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
25155796c8dcSSimon Schubert     {
25165796c8dcSSimon Schubert       *argp = value_ind (*argp);
25175796c8dcSSimon Schubert       /* Don't coerce fn pointer to fn and then back again!  */
25185796c8dcSSimon Schubert       if (TYPE_CODE (value_type (*argp)) != TYPE_CODE_FUNC)
25195796c8dcSSimon Schubert 	*argp = coerce_array (*argp);
25205796c8dcSSimon Schubert       t = check_typedef (value_type (*argp));
25215796c8dcSSimon Schubert     }
25225796c8dcSSimon Schubert 
25235796c8dcSSimon Schubert   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
25245796c8dcSSimon Schubert       && TYPE_CODE (t) != TYPE_CODE_UNION)
2525c50c785cSJohn Marino     error (_("Attempt to extract a component of a "
2526c50c785cSJohn Marino 	     "value that is not a struct or union"));
25275796c8dcSSimon Schubert 
25285796c8dcSSimon Schubert   return find_method_list (argp, method, 0, t, num_fns,
25295796c8dcSSimon Schubert 			   basetype, boffset);
25305796c8dcSSimon Schubert }
25315796c8dcSSimon Schubert 
2532a45ae5f8SJohn Marino /* Given an array of arguments (ARGS) (which includes an
25335796c8dcSSimon Schubert    entry for "this" in the case of C++ methods), the number of
2534*ef5ccd6cSJohn Marino    arguments NARGS, the NAME of a function, and whether it's a method or
2535*ef5ccd6cSJohn Marino    not (METHOD), find the best function that matches on the argument types
2536*ef5ccd6cSJohn Marino    according to the overload resolution rules.
25375796c8dcSSimon Schubert 
2538cf7f2e2dSJohn Marino    METHOD can be one of three values:
2539cf7f2e2dSJohn Marino      NON_METHOD for non-member functions.
2540cf7f2e2dSJohn Marino      METHOD: for member functions.
2541cf7f2e2dSJohn Marino      BOTH: used for overload resolution of operators where the
2542cf7f2e2dSJohn Marino        candidates are expected to be either member or non member
2543cf7f2e2dSJohn Marino        functions.  In this case the first argument ARGTYPES
2544cf7f2e2dSJohn Marino        (representing 'this') is expected to be a reference to the
2545cf7f2e2dSJohn Marino        target object, and will be dereferenced when attempting the
2546cf7f2e2dSJohn Marino        non-member search.
2547cf7f2e2dSJohn Marino 
25485796c8dcSSimon Schubert    In the case of class methods, the parameter OBJ is an object value
25495796c8dcSSimon Schubert    in which to search for overloaded methods.
25505796c8dcSSimon Schubert 
25515796c8dcSSimon Schubert    In the case of non-method functions, the parameter FSYM is a symbol
25525796c8dcSSimon Schubert    corresponding to one of the overloaded functions.
25535796c8dcSSimon Schubert 
25545796c8dcSSimon Schubert    Return value is an integer: 0 -> good match, 10 -> debugger applied
25555796c8dcSSimon Schubert    non-standard coercions, 100 -> incompatible.
25565796c8dcSSimon Schubert 
25575796c8dcSSimon Schubert    If a method is being searched for, VALP will hold the value.
25585796c8dcSSimon Schubert    If a non-method is being searched for, SYMP will hold the symbol
25595796c8dcSSimon Schubert    for it.
25605796c8dcSSimon Schubert 
25615796c8dcSSimon Schubert    If a method is being searched for, and it is a static method,
25625796c8dcSSimon Schubert    then STATICP will point to a non-zero value.
25635796c8dcSSimon Schubert 
2564cf7f2e2dSJohn Marino    If NO_ADL argument dependent lookup is disabled.  This is used to prevent
2565cf7f2e2dSJohn Marino    ADL overload candidates when performing overload resolution for a fully
2566cf7f2e2dSJohn Marino    qualified name.
2567cf7f2e2dSJohn Marino 
25685796c8dcSSimon Schubert    Note: This function does *not* check the value of
25695796c8dcSSimon Schubert    overload_resolution.  Caller must check it to see whether overload
2570c50c785cSJohn Marino    resolution is permitted.  */
25715796c8dcSSimon Schubert 
25725796c8dcSSimon Schubert int
find_overload_match(struct value ** args,int nargs,const char * name,enum oload_search_type method,struct value ** objp,struct symbol * fsym,struct value ** valp,struct symbol ** symp,int * staticp,const int no_adl)2573a45ae5f8SJohn Marino find_overload_match (struct value **args, int nargs,
2574cf7f2e2dSJohn Marino 		     const char *name, enum oload_search_type method,
2575*ef5ccd6cSJohn Marino 		     struct value **objp, struct symbol *fsym,
25765796c8dcSSimon Schubert 		     struct value **valp, struct symbol **symp,
2577cf7f2e2dSJohn Marino 		     int *staticp, const int no_adl)
25785796c8dcSSimon Schubert {
25795796c8dcSSimon Schubert   struct value *obj = (objp ? *objp : NULL);
2580a45ae5f8SJohn Marino   struct type *obj_type = obj ? value_type (obj) : NULL;
25815796c8dcSSimon Schubert   /* Index of best overloaded function.  */
2582cf7f2e2dSJohn Marino   int func_oload_champ = -1;
2583cf7f2e2dSJohn Marino   int method_oload_champ = -1;
2584cf7f2e2dSJohn Marino 
25855796c8dcSSimon Schubert   /* The measure for the current best match.  */
2586cf7f2e2dSJohn Marino   struct badness_vector *method_badness = NULL;
2587cf7f2e2dSJohn Marino   struct badness_vector *func_badness = NULL;
2588cf7f2e2dSJohn Marino 
25895796c8dcSSimon Schubert   struct value *temp = obj;
25905796c8dcSSimon Schubert   /* For methods, the list of overloaded methods.  */
25915796c8dcSSimon Schubert   struct fn_field *fns_ptr = NULL;
25925796c8dcSSimon Schubert   /* For non-methods, the list of overloaded function symbols.  */
25935796c8dcSSimon Schubert   struct symbol **oload_syms = NULL;
25945796c8dcSSimon Schubert   /* Number of overloaded instances being considered.  */
25955796c8dcSSimon Schubert   int num_fns = 0;
25965796c8dcSSimon Schubert   struct type *basetype = NULL;
25975796c8dcSSimon Schubert   int boffset;
2598cf7f2e2dSJohn Marino 
2599cf7f2e2dSJohn Marino   struct cleanup *all_cleanups = make_cleanup (null_cleanup, NULL);
26005796c8dcSSimon Schubert 
26015796c8dcSSimon Schubert   const char *obj_type_name = NULL;
2602cf7f2e2dSJohn Marino   const char *func_name = NULL;
26035796c8dcSSimon Schubert   enum oload_classification match_quality;
2604cf7f2e2dSJohn Marino   enum oload_classification method_match_quality = INCOMPATIBLE;
2605cf7f2e2dSJohn Marino   enum oload_classification func_match_quality = INCOMPATIBLE;
26065796c8dcSSimon Schubert 
26075796c8dcSSimon Schubert   /* Get the list of overloaded methods or functions.  */
2608cf7f2e2dSJohn Marino   if (method == METHOD || method == BOTH)
26095796c8dcSSimon Schubert     {
26105796c8dcSSimon Schubert       gdb_assert (obj);
26115796c8dcSSimon Schubert 
2612cf7f2e2dSJohn Marino       /* OBJ may be a pointer value rather than the object itself.  */
2613cf7f2e2dSJohn Marino       obj = coerce_ref (obj);
2614cf7f2e2dSJohn Marino       while (TYPE_CODE (check_typedef (value_type (obj))) == TYPE_CODE_PTR)
2615cf7f2e2dSJohn Marino 	obj = coerce_ref (value_ind (obj));
2616cf7f2e2dSJohn Marino       obj_type_name = TYPE_NAME (value_type (obj));
2617cf7f2e2dSJohn Marino 
2618cf7f2e2dSJohn Marino       /* First check whether this is a data member, e.g. a pointer to
2619cf7f2e2dSJohn Marino 	 a function.  */
2620cf7f2e2dSJohn Marino       if (TYPE_CODE (check_typedef (value_type (obj))) == TYPE_CODE_STRUCT)
2621cf7f2e2dSJohn Marino 	{
2622cf7f2e2dSJohn Marino 	  *valp = search_struct_field (name, obj, 0,
2623cf7f2e2dSJohn Marino 				       check_typedef (value_type (obj)), 0);
2624cf7f2e2dSJohn Marino 	  if (*valp)
2625cf7f2e2dSJohn Marino 	    {
2626cf7f2e2dSJohn Marino 	      *staticp = 1;
2627a45ae5f8SJohn Marino 	      do_cleanups (all_cleanups);
2628cf7f2e2dSJohn Marino 	      return 0;
2629cf7f2e2dSJohn Marino 	    }
2630cf7f2e2dSJohn Marino 	}
2631cf7f2e2dSJohn Marino 
2632cf7f2e2dSJohn Marino       /* Retrieve the list of methods with the name NAME.  */
26335796c8dcSSimon Schubert       fns_ptr = value_find_oload_method_list (&temp, name,
26345796c8dcSSimon Schubert 					      0, &num_fns,
26355796c8dcSSimon Schubert 					      &basetype, &boffset);
2636cf7f2e2dSJohn Marino       /* If this is a method only search, and no methods were found
2637cf7f2e2dSJohn Marino          the search has faild.  */
2638cf7f2e2dSJohn Marino       if (method == METHOD && (!fns_ptr || !num_fns))
26395796c8dcSSimon Schubert 	error (_("Couldn't find method %s%s%s"),
26405796c8dcSSimon Schubert 	       obj_type_name,
26415796c8dcSSimon Schubert 	       (obj_type_name && *obj_type_name) ? "::" : "",
26425796c8dcSSimon Schubert 	       name);
26435796c8dcSSimon Schubert       /* If we are dealing with stub method types, they should have
26445796c8dcSSimon Schubert 	 been resolved by find_method_list via
26455796c8dcSSimon Schubert 	 value_find_oload_method_list above.  */
2646cf7f2e2dSJohn Marino       if (fns_ptr)
2647cf7f2e2dSJohn Marino 	{
26485796c8dcSSimon Schubert 	  gdb_assert (TYPE_DOMAIN_TYPE (fns_ptr[0].type) != NULL);
2649a45ae5f8SJohn Marino 	  method_oload_champ = find_oload_champ (args, nargs, method,
26505796c8dcSSimon Schubert 	                                         num_fns, fns_ptr,
2651cf7f2e2dSJohn Marino 	                                         oload_syms, &method_badness);
2652cf7f2e2dSJohn Marino 
2653cf7f2e2dSJohn Marino 	  method_match_quality =
2654cf7f2e2dSJohn Marino 	      classify_oload_match (method_badness, nargs,
2655cf7f2e2dSJohn Marino 	                            oload_method_static (method, fns_ptr,
2656cf7f2e2dSJohn Marino 	                                                 method_oload_champ));
2657cf7f2e2dSJohn Marino 
2658cf7f2e2dSJohn Marino 	  make_cleanup (xfree, method_badness);
2659cf7f2e2dSJohn Marino 	}
2660cf7f2e2dSJohn Marino 
2661cf7f2e2dSJohn Marino     }
2662cf7f2e2dSJohn Marino 
2663cf7f2e2dSJohn Marino   if (method == NON_METHOD || method == BOTH)
2664cf7f2e2dSJohn Marino     {
2665cf7f2e2dSJohn Marino       const char *qualified_name = NULL;
2666cf7f2e2dSJohn Marino 
2667c50c785cSJohn Marino       /* If the overload match is being search for both as a method
2668c50c785cSJohn Marino          and non member function, the first argument must now be
2669c50c785cSJohn Marino          dereferenced.  */
2670cf7f2e2dSJohn Marino       if (method == BOTH)
2671*ef5ccd6cSJohn Marino 	args[0] = value_ind (args[0]);
2672cf7f2e2dSJohn Marino 
2673cf7f2e2dSJohn Marino       if (fsym)
2674cf7f2e2dSJohn Marino         {
2675cf7f2e2dSJohn Marino           qualified_name = SYMBOL_NATURAL_NAME (fsym);
2676cf7f2e2dSJohn Marino 
2677cf7f2e2dSJohn Marino           /* If we have a function with a C++ name, try to extract just
2678cf7f2e2dSJohn Marino 	     the function part.  Do not try this for non-functions (e.g.
2679cf7f2e2dSJohn Marino 	     function pointers).  */
2680cf7f2e2dSJohn Marino           if (qualified_name
2681c50c785cSJohn Marino               && TYPE_CODE (check_typedef (SYMBOL_TYPE (fsym)))
2682c50c785cSJohn Marino 	      == TYPE_CODE_FUNC)
2683cf7f2e2dSJohn Marino             {
2684cf7f2e2dSJohn Marino 	      char *temp;
2685cf7f2e2dSJohn Marino 
2686cf7f2e2dSJohn Marino 	      temp = cp_func_name (qualified_name);
2687cf7f2e2dSJohn Marino 
2688cf7f2e2dSJohn Marino 	      /* If cp_func_name did not remove anything, the name of the
2689cf7f2e2dSJohn Marino 	         symbol did not include scope or argument types - it was
2690cf7f2e2dSJohn Marino 	         probably a C-style function.  */
2691cf7f2e2dSJohn Marino 	      if (temp)
2692cf7f2e2dSJohn Marino 		{
2693cf7f2e2dSJohn Marino 		  make_cleanup (xfree, temp);
2694cf7f2e2dSJohn Marino 		  if (strcmp (temp, qualified_name) == 0)
2695cf7f2e2dSJohn Marino 		    func_name = NULL;
2696cf7f2e2dSJohn Marino 		  else
2697cf7f2e2dSJohn Marino 		    func_name = temp;
2698cf7f2e2dSJohn Marino 		}
2699cf7f2e2dSJohn Marino             }
27005796c8dcSSimon Schubert         }
27015796c8dcSSimon Schubert       else
27025796c8dcSSimon Schubert 	{
2703cf7f2e2dSJohn Marino 	  func_name = name;
2704cf7f2e2dSJohn Marino 	  qualified_name = name;
2705cf7f2e2dSJohn Marino 	}
27065796c8dcSSimon Schubert 
2707cf7f2e2dSJohn Marino       /* If there was no C++ name, this must be a C-style function or
2708cf7f2e2dSJohn Marino 	 not a function at all.  Just return the same symbol.  Do the
2709cf7f2e2dSJohn Marino 	 same if cp_func_name fails for some reason.  */
27105796c8dcSSimon Schubert       if (func_name == NULL)
27115796c8dcSSimon Schubert         {
27125796c8dcSSimon Schubert 	  *symp = fsym;
2713a45ae5f8SJohn Marino 	  do_cleanups (all_cleanups);
27145796c8dcSSimon Schubert           return 0;
27155796c8dcSSimon Schubert         }
27165796c8dcSSimon Schubert 
2717a45ae5f8SJohn Marino       func_oload_champ = find_oload_champ_namespace (args, nargs,
27185796c8dcSSimon Schubert                                                      func_name,
27195796c8dcSSimon Schubert                                                      qualified_name,
27205796c8dcSSimon Schubert                                                      &oload_syms,
2721cf7f2e2dSJohn Marino                                                      &func_badness,
2722cf7f2e2dSJohn Marino                                                      no_adl);
2723cf7f2e2dSJohn Marino 
2724cf7f2e2dSJohn Marino       if (func_oload_champ >= 0)
2725cf7f2e2dSJohn Marino 	func_match_quality = classify_oload_match (func_badness, nargs, 0);
2726cf7f2e2dSJohn Marino 
2727cf7f2e2dSJohn Marino       make_cleanup (xfree, oload_syms);
2728cf7f2e2dSJohn Marino       make_cleanup (xfree, func_badness);
27295796c8dcSSimon Schubert     }
27305796c8dcSSimon Schubert 
2731cf7f2e2dSJohn Marino   /* Did we find a match ?  */
2732cf7f2e2dSJohn Marino   if (method_oload_champ == -1 && func_oload_champ == -1)
2733c50c785cSJohn Marino     throw_error (NOT_FOUND_ERROR,
2734c50c785cSJohn Marino                  _("No symbol \"%s\" in current context."),
2735c50c785cSJohn Marino                  name);
27365796c8dcSSimon Schubert 
2737cf7f2e2dSJohn Marino   /* If we have found both a method match and a function
2738cf7f2e2dSJohn Marino      match, find out which one is better, and calculate match
2739cf7f2e2dSJohn Marino      quality.  */
2740cf7f2e2dSJohn Marino   if (method_oload_champ >= 0 && func_oload_champ >= 0)
2741cf7f2e2dSJohn Marino     {
2742cf7f2e2dSJohn Marino       switch (compare_badness (func_badness, method_badness))
2743cf7f2e2dSJohn Marino         {
2744cf7f2e2dSJohn Marino 	  case 0: /* Top two contenders are equally good.  */
2745c50c785cSJohn Marino 	    /* FIXME: GDB does not support the general ambiguous case.
2746c50c785cSJohn Marino 	     All candidates should be collected and presented the
2747c50c785cSJohn Marino 	     user.  */
2748cf7f2e2dSJohn Marino 	    error (_("Ambiguous overload resolution"));
2749cf7f2e2dSJohn Marino 	    break;
2750cf7f2e2dSJohn Marino 	  case 1: /* Incomparable top contenders.  */
2751cf7f2e2dSJohn Marino 	    /* This is an error incompatible candidates
2752cf7f2e2dSJohn Marino 	       should not have been proposed.  */
2753c50c785cSJohn Marino 	    error (_("Internal error: incompatible "
2754c50c785cSJohn Marino 		     "overload candidates proposed"));
2755cf7f2e2dSJohn Marino 	    break;
2756cf7f2e2dSJohn Marino 	  case 2: /* Function champion.  */
2757cf7f2e2dSJohn Marino 	    method_oload_champ = -1;
2758cf7f2e2dSJohn Marino 	    match_quality = func_match_quality;
2759cf7f2e2dSJohn Marino 	    break;
2760cf7f2e2dSJohn Marino 	  case 3: /* Method champion.  */
2761cf7f2e2dSJohn Marino 	    func_oload_champ = -1;
2762cf7f2e2dSJohn Marino 	    match_quality = method_match_quality;
2763cf7f2e2dSJohn Marino 	    break;
2764cf7f2e2dSJohn Marino 	  default:
2765cf7f2e2dSJohn Marino 	    error (_("Internal error: unexpected overload comparison result"));
2766cf7f2e2dSJohn Marino 	    break;
2767cf7f2e2dSJohn Marino         }
2768cf7f2e2dSJohn Marino     }
2769cf7f2e2dSJohn Marino   else
2770cf7f2e2dSJohn Marino     {
2771cf7f2e2dSJohn Marino       /* We have either a method match or a function match.  */
2772cf7f2e2dSJohn Marino       if (method_oload_champ >= 0)
2773cf7f2e2dSJohn Marino 	match_quality = method_match_quality;
2774cf7f2e2dSJohn Marino       else
2775cf7f2e2dSJohn Marino 	match_quality = func_match_quality;
2776cf7f2e2dSJohn Marino     }
27775796c8dcSSimon Schubert 
27785796c8dcSSimon Schubert   if (match_quality == INCOMPATIBLE)
27795796c8dcSSimon Schubert     {
2780cf7f2e2dSJohn Marino       if (method == METHOD)
27815796c8dcSSimon Schubert 	error (_("Cannot resolve method %s%s%s to any overloaded instance"),
27825796c8dcSSimon Schubert 	       obj_type_name,
27835796c8dcSSimon Schubert 	       (obj_type_name && *obj_type_name) ? "::" : "",
27845796c8dcSSimon Schubert 	       name);
27855796c8dcSSimon Schubert       else
27865796c8dcSSimon Schubert 	error (_("Cannot resolve function %s to any overloaded instance"),
27875796c8dcSSimon Schubert 	       func_name);
27885796c8dcSSimon Schubert     }
27895796c8dcSSimon Schubert   else if (match_quality == NON_STANDARD)
27905796c8dcSSimon Schubert     {
2791cf7f2e2dSJohn Marino       if (method == METHOD)
2792c50c785cSJohn Marino 	warning (_("Using non-standard conversion to match "
2793c50c785cSJohn Marino 		   "method %s%s%s to supplied arguments"),
27945796c8dcSSimon Schubert 		 obj_type_name,
27955796c8dcSSimon Schubert 		 (obj_type_name && *obj_type_name) ? "::" : "",
27965796c8dcSSimon Schubert 		 name);
27975796c8dcSSimon Schubert       else
2798c50c785cSJohn Marino 	warning (_("Using non-standard conversion to match "
2799c50c785cSJohn Marino 		   "function %s to supplied arguments"),
28005796c8dcSSimon Schubert 		 func_name);
28015796c8dcSSimon Schubert     }
28025796c8dcSSimon Schubert 
28035796c8dcSSimon Schubert   if (staticp != NULL)
2804cf7f2e2dSJohn Marino     *staticp = oload_method_static (method, fns_ptr, method_oload_champ);
2805cf7f2e2dSJohn Marino 
2806cf7f2e2dSJohn Marino   if (method_oload_champ >= 0)
28075796c8dcSSimon Schubert     {
2808cf7f2e2dSJohn Marino       if (TYPE_FN_FIELD_VIRTUAL_P (fns_ptr, method_oload_champ))
2809cf7f2e2dSJohn Marino 	*valp = value_virtual_fn_field (&temp, fns_ptr, method_oload_champ,
2810cf7f2e2dSJohn Marino 					basetype, boffset);
2811cf7f2e2dSJohn Marino       else
2812cf7f2e2dSJohn Marino 	*valp = value_fn_field (&temp, fns_ptr, method_oload_champ,
2813cf7f2e2dSJohn Marino 				basetype, boffset);
28145796c8dcSSimon Schubert     }
2815cf7f2e2dSJohn Marino   else
2816cf7f2e2dSJohn Marino     *symp = oload_syms[func_oload_champ];
28175796c8dcSSimon Schubert 
28185796c8dcSSimon Schubert   if (objp)
28195796c8dcSSimon Schubert     {
28205796c8dcSSimon Schubert       struct type *temp_type = check_typedef (value_type (temp));
2821a45ae5f8SJohn Marino       struct type *objtype = check_typedef (obj_type);
2822cf7f2e2dSJohn Marino 
28235796c8dcSSimon Schubert       if (TYPE_CODE (temp_type) != TYPE_CODE_PTR
2824a45ae5f8SJohn Marino 	  && (TYPE_CODE (objtype) == TYPE_CODE_PTR
2825a45ae5f8SJohn Marino 	      || TYPE_CODE (objtype) == TYPE_CODE_REF))
28265796c8dcSSimon Schubert 	{
28275796c8dcSSimon Schubert 	  temp = value_addr (temp);
28285796c8dcSSimon Schubert 	}
28295796c8dcSSimon Schubert       *objp = temp;
28305796c8dcSSimon Schubert     }
2831cf7f2e2dSJohn Marino 
2832cf7f2e2dSJohn Marino   do_cleanups (all_cleanups);
28335796c8dcSSimon Schubert 
28345796c8dcSSimon Schubert   switch (match_quality)
28355796c8dcSSimon Schubert     {
28365796c8dcSSimon Schubert     case INCOMPATIBLE:
28375796c8dcSSimon Schubert       return 100;
28385796c8dcSSimon Schubert     case NON_STANDARD:
28395796c8dcSSimon Schubert       return 10;
28405796c8dcSSimon Schubert     default:				/* STANDARD */
28415796c8dcSSimon Schubert       return 0;
28425796c8dcSSimon Schubert     }
28435796c8dcSSimon Schubert }
28445796c8dcSSimon Schubert 
28455796c8dcSSimon Schubert /* Find the best overload match, searching for FUNC_NAME in namespaces
28465796c8dcSSimon Schubert    contained in QUALIFIED_NAME until it either finds a good match or
28475796c8dcSSimon Schubert    runs out of namespaces.  It stores the overloaded functions in
28485796c8dcSSimon Schubert    *OLOAD_SYMS, and the badness vector in *OLOAD_CHAMP_BV.  The
28495796c8dcSSimon Schubert    calling function is responsible for freeing *OLOAD_SYMS and
2850cf7f2e2dSJohn Marino    *OLOAD_CHAMP_BV.  If NO_ADL, argument dependent lookup is not
2851cf7f2e2dSJohn Marino    performned.  */
28525796c8dcSSimon Schubert 
28535796c8dcSSimon Schubert static int
find_oload_champ_namespace(struct value ** args,int nargs,const char * func_name,const char * qualified_name,struct symbol *** oload_syms,struct badness_vector ** oload_champ_bv,const int no_adl)2854a45ae5f8SJohn Marino find_oload_champ_namespace (struct value **args, int nargs,
28555796c8dcSSimon Schubert 			    const char *func_name,
28565796c8dcSSimon Schubert 			    const char *qualified_name,
28575796c8dcSSimon Schubert 			    struct symbol ***oload_syms,
2858cf7f2e2dSJohn Marino 			    struct badness_vector **oload_champ_bv,
2859cf7f2e2dSJohn Marino 			    const int no_adl)
28605796c8dcSSimon Schubert {
28615796c8dcSSimon Schubert   int oload_champ;
28625796c8dcSSimon Schubert 
2863a45ae5f8SJohn Marino   find_oload_champ_namespace_loop (args, nargs,
28645796c8dcSSimon Schubert 				   func_name,
28655796c8dcSSimon Schubert 				   qualified_name, 0,
28665796c8dcSSimon Schubert 				   oload_syms, oload_champ_bv,
2867cf7f2e2dSJohn Marino 				   &oload_champ,
2868cf7f2e2dSJohn Marino 				   no_adl);
28695796c8dcSSimon Schubert 
28705796c8dcSSimon Schubert   return oload_champ;
28715796c8dcSSimon Schubert }
28725796c8dcSSimon Schubert 
28735796c8dcSSimon Schubert /* Helper function for find_oload_champ_namespace; NAMESPACE_LEN is
28745796c8dcSSimon Schubert    how deep we've looked for namespaces, and the champ is stored in
28755796c8dcSSimon Schubert    OLOAD_CHAMP.  The return value is 1 if the champ is a good one, 0
2876cf7f2e2dSJohn Marino    if it isn't.  Other arguments are the same as in
2877cf7f2e2dSJohn Marino    find_oload_champ_namespace
28785796c8dcSSimon Schubert 
28795796c8dcSSimon Schubert    It is the caller's responsibility to free *OLOAD_SYMS and
28805796c8dcSSimon Schubert    *OLOAD_CHAMP_BV.  */
28815796c8dcSSimon Schubert 
28825796c8dcSSimon Schubert static int
find_oload_champ_namespace_loop(struct value ** args,int nargs,const char * func_name,const char * qualified_name,int namespace_len,struct symbol *** oload_syms,struct badness_vector ** oload_champ_bv,int * oload_champ,const int no_adl)2883a45ae5f8SJohn Marino find_oload_champ_namespace_loop (struct value **args, int nargs,
28845796c8dcSSimon Schubert 				 const char *func_name,
28855796c8dcSSimon Schubert 				 const char *qualified_name,
28865796c8dcSSimon Schubert 				 int namespace_len,
28875796c8dcSSimon Schubert 				 struct symbol ***oload_syms,
28885796c8dcSSimon Schubert 				 struct badness_vector **oload_champ_bv,
2889cf7f2e2dSJohn Marino 				 int *oload_champ,
2890cf7f2e2dSJohn Marino 				 const int no_adl)
28915796c8dcSSimon Schubert {
28925796c8dcSSimon Schubert   int next_namespace_len = namespace_len;
28935796c8dcSSimon Schubert   int searched_deeper = 0;
28945796c8dcSSimon Schubert   int num_fns = 0;
28955796c8dcSSimon Schubert   struct cleanup *old_cleanups;
28965796c8dcSSimon Schubert   int new_oload_champ;
28975796c8dcSSimon Schubert   struct symbol **new_oload_syms;
28985796c8dcSSimon Schubert   struct badness_vector *new_oload_champ_bv;
28995796c8dcSSimon Schubert   char *new_namespace;
29005796c8dcSSimon Schubert 
29015796c8dcSSimon Schubert   if (next_namespace_len != 0)
29025796c8dcSSimon Schubert     {
29035796c8dcSSimon Schubert       gdb_assert (qualified_name[next_namespace_len] == ':');
29045796c8dcSSimon Schubert       next_namespace_len +=  2;
29055796c8dcSSimon Schubert     }
29065796c8dcSSimon Schubert   next_namespace_len +=
29075796c8dcSSimon Schubert     cp_find_first_component (qualified_name + next_namespace_len);
29085796c8dcSSimon Schubert 
29095796c8dcSSimon Schubert   /* Initialize these to values that can safely be xfree'd.  */
29105796c8dcSSimon Schubert   *oload_syms = NULL;
29115796c8dcSSimon Schubert   *oload_champ_bv = NULL;
29125796c8dcSSimon Schubert 
29135796c8dcSSimon Schubert   /* First, see if we have a deeper namespace we can search in.
29145796c8dcSSimon Schubert      If we get a good match there, use it.  */
29155796c8dcSSimon Schubert 
29165796c8dcSSimon Schubert   if (qualified_name[next_namespace_len] == ':')
29175796c8dcSSimon Schubert     {
29185796c8dcSSimon Schubert       searched_deeper = 1;
29195796c8dcSSimon Schubert 
2920a45ae5f8SJohn Marino       if (find_oload_champ_namespace_loop (args, nargs,
29215796c8dcSSimon Schubert 					   func_name, qualified_name,
29225796c8dcSSimon Schubert 					   next_namespace_len,
29235796c8dcSSimon Schubert 					   oload_syms, oload_champ_bv,
2924cf7f2e2dSJohn Marino 					   oload_champ, no_adl))
29255796c8dcSSimon Schubert 	{
29265796c8dcSSimon Schubert 	  return 1;
29275796c8dcSSimon Schubert 	}
29285796c8dcSSimon Schubert     };
29295796c8dcSSimon Schubert 
29305796c8dcSSimon Schubert   /* If we reach here, either we're in the deepest namespace or we
29315796c8dcSSimon Schubert      didn't find a good match in a deeper namespace.  But, in the
29325796c8dcSSimon Schubert      latter case, we still have a bad match in a deeper namespace;
29335796c8dcSSimon Schubert      note that we might not find any match at all in the current
29345796c8dcSSimon Schubert      namespace.  (There's always a match in the deepest namespace,
29355796c8dcSSimon Schubert      because this overload mechanism only gets called if there's a
29365796c8dcSSimon Schubert      function symbol to start off with.)  */
29375796c8dcSSimon Schubert 
29385796c8dcSSimon Schubert   old_cleanups = make_cleanup (xfree, *oload_syms);
2939c50c785cSJohn Marino   make_cleanup (xfree, *oload_champ_bv);
29405796c8dcSSimon Schubert   new_namespace = alloca (namespace_len + 1);
29415796c8dcSSimon Schubert   strncpy (new_namespace, qualified_name, namespace_len);
29425796c8dcSSimon Schubert   new_namespace[namespace_len] = '\0';
29435796c8dcSSimon Schubert   new_oload_syms = make_symbol_overload_list (func_name,
29445796c8dcSSimon Schubert 					      new_namespace);
2945cf7f2e2dSJohn Marino 
2946cf7f2e2dSJohn Marino   /* If we have reached the deepest level perform argument
2947cf7f2e2dSJohn Marino      determined lookup.  */
2948cf7f2e2dSJohn Marino   if (!searched_deeper && !no_adl)
2949a45ae5f8SJohn Marino     {
2950a45ae5f8SJohn Marino       int ix;
2951a45ae5f8SJohn Marino       struct type **arg_types;
2952a45ae5f8SJohn Marino 
2953a45ae5f8SJohn Marino       /* Prepare list of argument types for overload resolution.  */
2954a45ae5f8SJohn Marino       arg_types = (struct type **)
2955a45ae5f8SJohn Marino 	alloca (nargs * (sizeof (struct type *)));
2956a45ae5f8SJohn Marino       for (ix = 0; ix < nargs; ix++)
2957a45ae5f8SJohn Marino 	arg_types[ix] = value_type (args[ix]);
2958cf7f2e2dSJohn Marino       make_symbol_overload_list_adl (arg_types, nargs, func_name);
2959a45ae5f8SJohn Marino     }
2960cf7f2e2dSJohn Marino 
29615796c8dcSSimon Schubert   while (new_oload_syms[num_fns])
29625796c8dcSSimon Schubert     ++num_fns;
29635796c8dcSSimon Schubert 
2964a45ae5f8SJohn Marino   new_oload_champ = find_oload_champ (args, nargs, 0, num_fns,
29655796c8dcSSimon Schubert 				      NULL, new_oload_syms,
29665796c8dcSSimon Schubert 				      &new_oload_champ_bv);
29675796c8dcSSimon Schubert 
29685796c8dcSSimon Schubert   /* Case 1: We found a good match.  Free earlier matches (if any),
29695796c8dcSSimon Schubert      and return it.  Case 2: We didn't find a good match, but we're
29705796c8dcSSimon Schubert      not the deepest function.  Then go with the bad match that the
29715796c8dcSSimon Schubert      deeper function found.  Case 3: We found a bad match, and we're
29725796c8dcSSimon Schubert      the deepest function.  Then return what we found, even though
29735796c8dcSSimon Schubert      it's a bad match.  */
29745796c8dcSSimon Schubert 
29755796c8dcSSimon Schubert   if (new_oload_champ != -1
29765796c8dcSSimon Schubert       && classify_oload_match (new_oload_champ_bv, nargs, 0) == STANDARD)
29775796c8dcSSimon Schubert     {
29785796c8dcSSimon Schubert       *oload_syms = new_oload_syms;
29795796c8dcSSimon Schubert       *oload_champ = new_oload_champ;
29805796c8dcSSimon Schubert       *oload_champ_bv = new_oload_champ_bv;
29815796c8dcSSimon Schubert       do_cleanups (old_cleanups);
29825796c8dcSSimon Schubert       return 1;
29835796c8dcSSimon Schubert     }
29845796c8dcSSimon Schubert   else if (searched_deeper)
29855796c8dcSSimon Schubert     {
29865796c8dcSSimon Schubert       xfree (new_oload_syms);
29875796c8dcSSimon Schubert       xfree (new_oload_champ_bv);
29885796c8dcSSimon Schubert       discard_cleanups (old_cleanups);
29895796c8dcSSimon Schubert       return 0;
29905796c8dcSSimon Schubert     }
29915796c8dcSSimon Schubert   else
29925796c8dcSSimon Schubert     {
29935796c8dcSSimon Schubert       *oload_syms = new_oload_syms;
29945796c8dcSSimon Schubert       *oload_champ = new_oload_champ;
29955796c8dcSSimon Schubert       *oload_champ_bv = new_oload_champ_bv;
2996c50c785cSJohn Marino       do_cleanups (old_cleanups);
29975796c8dcSSimon Schubert       return 0;
29985796c8dcSSimon Schubert     }
29995796c8dcSSimon Schubert }
30005796c8dcSSimon Schubert 
3001a45ae5f8SJohn Marino /* Look for a function to take NARGS args of ARGS.  Find
30025796c8dcSSimon Schubert    the best match from among the overloaded methods or functions
30035796c8dcSSimon Schubert    (depending on METHOD) given by FNS_PTR or OLOAD_SYMS, respectively.
30045796c8dcSSimon Schubert    The number of methods/functions in the list is given by NUM_FNS.
30055796c8dcSSimon Schubert    Return the index of the best match; store an indication of the
30065796c8dcSSimon Schubert    quality of the match in OLOAD_CHAMP_BV.
30075796c8dcSSimon Schubert 
30085796c8dcSSimon Schubert    It is the caller's responsibility to free *OLOAD_CHAMP_BV.  */
30095796c8dcSSimon Schubert 
30105796c8dcSSimon Schubert static int
find_oload_champ(struct value ** args,int nargs,int method,int num_fns,struct fn_field * fns_ptr,struct symbol ** oload_syms,struct badness_vector ** oload_champ_bv)3011a45ae5f8SJohn Marino find_oload_champ (struct value **args, int nargs, int method,
30125796c8dcSSimon Schubert 		  int num_fns, struct fn_field *fns_ptr,
30135796c8dcSSimon Schubert 		  struct symbol **oload_syms,
30145796c8dcSSimon Schubert 		  struct badness_vector **oload_champ_bv)
30155796c8dcSSimon Schubert {
30165796c8dcSSimon Schubert   int ix;
30175796c8dcSSimon Schubert   /* A measure of how good an overloaded instance is.  */
30185796c8dcSSimon Schubert   struct badness_vector *bv;
30195796c8dcSSimon Schubert   /* Index of best overloaded function.  */
30205796c8dcSSimon Schubert   int oload_champ = -1;
30215796c8dcSSimon Schubert   /* Current ambiguity state for overload resolution.  */
30225796c8dcSSimon Schubert   int oload_ambiguous = 0;
30235796c8dcSSimon Schubert   /* 0 => no ambiguity, 1 => two good funcs, 2 => incomparable funcs.  */
30245796c8dcSSimon Schubert 
30255796c8dcSSimon Schubert   *oload_champ_bv = NULL;
30265796c8dcSSimon Schubert 
30275796c8dcSSimon Schubert   /* Consider each candidate in turn.  */
30285796c8dcSSimon Schubert   for (ix = 0; ix < num_fns; ix++)
30295796c8dcSSimon Schubert     {
30305796c8dcSSimon Schubert       int jj;
30315796c8dcSSimon Schubert       int static_offset = oload_method_static (method, fns_ptr, ix);
30325796c8dcSSimon Schubert       int nparms;
30335796c8dcSSimon Schubert       struct type **parm_types;
30345796c8dcSSimon Schubert 
30355796c8dcSSimon Schubert       if (method)
30365796c8dcSSimon Schubert 	{
30375796c8dcSSimon Schubert 	  nparms = TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (fns_ptr, ix));
30385796c8dcSSimon Schubert 	}
30395796c8dcSSimon Schubert       else
30405796c8dcSSimon Schubert 	{
30415796c8dcSSimon Schubert 	  /* If it's not a method, this is the proper place.  */
30425796c8dcSSimon Schubert 	  nparms = TYPE_NFIELDS (SYMBOL_TYPE (oload_syms[ix]));
30435796c8dcSSimon Schubert 	}
30445796c8dcSSimon Schubert 
30455796c8dcSSimon Schubert       /* Prepare array of parameter types.  */
30465796c8dcSSimon Schubert       parm_types = (struct type **)
30475796c8dcSSimon Schubert 	xmalloc (nparms * (sizeof (struct type *)));
30485796c8dcSSimon Schubert       for (jj = 0; jj < nparms; jj++)
30495796c8dcSSimon Schubert 	parm_types[jj] = (method
30505796c8dcSSimon Schubert 			  ? (TYPE_FN_FIELD_ARGS (fns_ptr, ix)[jj].type)
30515796c8dcSSimon Schubert 			  : TYPE_FIELD_TYPE (SYMBOL_TYPE (oload_syms[ix]),
30525796c8dcSSimon Schubert 					     jj));
30535796c8dcSSimon Schubert 
30545796c8dcSSimon Schubert       /* Compare parameter types to supplied argument types.  Skip
30555796c8dcSSimon Schubert          THIS for static methods.  */
30565796c8dcSSimon Schubert       bv = rank_function (parm_types, nparms,
3057a45ae5f8SJohn Marino 			  args + static_offset,
30585796c8dcSSimon Schubert 			  nargs - static_offset);
30595796c8dcSSimon Schubert 
30605796c8dcSSimon Schubert       if (!*oload_champ_bv)
30615796c8dcSSimon Schubert 	{
30625796c8dcSSimon Schubert 	  *oload_champ_bv = bv;
30635796c8dcSSimon Schubert 	  oload_champ = 0;
30645796c8dcSSimon Schubert 	}
30655796c8dcSSimon Schubert       else /* See whether current candidate is better or worse than
30665796c8dcSSimon Schubert 	      previous best.  */
30675796c8dcSSimon Schubert 	switch (compare_badness (bv, *oload_champ_bv))
30685796c8dcSSimon Schubert 	  {
30695796c8dcSSimon Schubert 	  case 0:		/* Top two contenders are equally good.  */
30705796c8dcSSimon Schubert 	    oload_ambiguous = 1;
30715796c8dcSSimon Schubert 	    break;
30725796c8dcSSimon Schubert 	  case 1:		/* Incomparable top contenders.  */
30735796c8dcSSimon Schubert 	    oload_ambiguous = 2;
30745796c8dcSSimon Schubert 	    break;
30755796c8dcSSimon Schubert 	  case 2:		/* New champion, record details.  */
30765796c8dcSSimon Schubert 	    *oload_champ_bv = bv;
30775796c8dcSSimon Schubert 	    oload_ambiguous = 0;
30785796c8dcSSimon Schubert 	    oload_champ = ix;
30795796c8dcSSimon Schubert 	    break;
30805796c8dcSSimon Schubert 	  case 3:
30815796c8dcSSimon Schubert 	  default:
30825796c8dcSSimon Schubert 	    break;
30835796c8dcSSimon Schubert 	  }
30845796c8dcSSimon Schubert       xfree (parm_types);
30855796c8dcSSimon Schubert       if (overload_debug)
30865796c8dcSSimon Schubert 	{
30875796c8dcSSimon Schubert 	  if (method)
30885796c8dcSSimon Schubert 	    fprintf_filtered (gdb_stderr,
30895796c8dcSSimon Schubert 			      "Overloaded method instance %s, # of parms %d\n",
30905796c8dcSSimon Schubert 			      fns_ptr[ix].physname, nparms);
30915796c8dcSSimon Schubert 	  else
30925796c8dcSSimon Schubert 	    fprintf_filtered (gdb_stderr,
3093c50c785cSJohn Marino 			      "Overloaded function instance "
3094c50c785cSJohn Marino 			      "%s # of parms %d\n",
30955796c8dcSSimon Schubert 			      SYMBOL_DEMANGLED_NAME (oload_syms[ix]),
30965796c8dcSSimon Schubert 			      nparms);
30975796c8dcSSimon Schubert 	  for (jj = 0; jj < nargs - static_offset; jj++)
30985796c8dcSSimon Schubert 	    fprintf_filtered (gdb_stderr,
30995796c8dcSSimon Schubert 			      "...Badness @ %d : %d\n",
3100c50c785cSJohn Marino 			      jj, bv->rank[jj].rank);
3101c50c785cSJohn Marino 	  fprintf_filtered (gdb_stderr, "Overload resolution "
3102c50c785cSJohn Marino 			    "champion is %d, ambiguous? %d\n",
31035796c8dcSSimon Schubert 			    oload_champ, oload_ambiguous);
31045796c8dcSSimon Schubert 	}
31055796c8dcSSimon Schubert     }
31065796c8dcSSimon Schubert 
31075796c8dcSSimon Schubert   return oload_champ;
31085796c8dcSSimon Schubert }
31095796c8dcSSimon Schubert 
31105796c8dcSSimon Schubert /* Return 1 if we're looking at a static method, 0 if we're looking at
31115796c8dcSSimon Schubert    a non-static method or a function that isn't a method.  */
31125796c8dcSSimon Schubert 
31135796c8dcSSimon Schubert static int
oload_method_static(int method,struct fn_field * fns_ptr,int index)31145796c8dcSSimon Schubert oload_method_static (int method, struct fn_field *fns_ptr, int index)
31155796c8dcSSimon Schubert {
3116cf7f2e2dSJohn Marino   if (method && fns_ptr && index >= 0
3117cf7f2e2dSJohn Marino       && TYPE_FN_FIELD_STATIC_P (fns_ptr, index))
31185796c8dcSSimon Schubert     return 1;
31195796c8dcSSimon Schubert   else
31205796c8dcSSimon Schubert     return 0;
31215796c8dcSSimon Schubert }
31225796c8dcSSimon Schubert 
31235796c8dcSSimon Schubert /* Check how good an overload match OLOAD_CHAMP_BV represents.  */
31245796c8dcSSimon Schubert 
31255796c8dcSSimon Schubert static enum oload_classification
classify_oload_match(struct badness_vector * oload_champ_bv,int nargs,int static_offset)31265796c8dcSSimon Schubert classify_oload_match (struct badness_vector *oload_champ_bv,
31275796c8dcSSimon Schubert 		      int nargs,
31285796c8dcSSimon Schubert 		      int static_offset)
31295796c8dcSSimon Schubert {
31305796c8dcSSimon Schubert   int ix;
3131a45ae5f8SJohn Marino   enum oload_classification worst = STANDARD;
31325796c8dcSSimon Schubert 
31335796c8dcSSimon Schubert   for (ix = 1; ix <= nargs - static_offset; ix++)
31345796c8dcSSimon Schubert     {
3135c50c785cSJohn Marino       /* If this conversion is as bad as INCOMPATIBLE_TYPE_BADNESS
3136c50c785cSJohn Marino          or worse return INCOMPATIBLE.  */
3137c50c785cSJohn Marino       if (compare_ranks (oload_champ_bv->rank[ix],
3138c50c785cSJohn Marino                          INCOMPATIBLE_TYPE_BADNESS) <= 0)
31395796c8dcSSimon Schubert 	return INCOMPATIBLE;	/* Truly mismatched types.  */
3140c50c785cSJohn Marino       /* Otherwise If this conversion is as bad as
3141c50c785cSJohn Marino          NS_POINTER_CONVERSION_BADNESS or worse return NON_STANDARD.  */
3142c50c785cSJohn Marino       else if (compare_ranks (oload_champ_bv->rank[ix],
3143c50c785cSJohn Marino                               NS_POINTER_CONVERSION_BADNESS) <= 0)
3144a45ae5f8SJohn Marino 	worst = NON_STANDARD;	/* Non-standard type conversions
31455796c8dcSSimon Schubert 				   needed.  */
31465796c8dcSSimon Schubert     }
31475796c8dcSSimon Schubert 
3148a45ae5f8SJohn Marino   /* If no INCOMPATIBLE classification was found, return the worst one
3149a45ae5f8SJohn Marino      that was found (if any).  */
3150a45ae5f8SJohn Marino   return worst;
31515796c8dcSSimon Schubert }
31525796c8dcSSimon Schubert 
31535796c8dcSSimon Schubert /* C++: return 1 is NAME is a legitimate name for the destructor of
31545796c8dcSSimon Schubert    type TYPE.  If TYPE does not have a destructor, or if NAME is
3155a45ae5f8SJohn Marino    inappropriate for TYPE, an error is signaled.  Parameter TYPE should not yet
3156a45ae5f8SJohn Marino    have CHECK_TYPEDEF applied, this function will apply it itself.  */
3157a45ae5f8SJohn Marino 
31585796c8dcSSimon Schubert int
destructor_name_p(const char * name,struct type * type)3159a45ae5f8SJohn Marino destructor_name_p (const char *name, struct type *type)
31605796c8dcSSimon Schubert {
31615796c8dcSSimon Schubert   if (name[0] == '~')
31625796c8dcSSimon Schubert     {
3163a45ae5f8SJohn Marino       const char *dname = type_name_no_tag_or_error (type);
3164a45ae5f8SJohn Marino       const char *cp = strchr (dname, '<');
31655796c8dcSSimon Schubert       unsigned int len;
31665796c8dcSSimon Schubert 
31675796c8dcSSimon Schubert       /* Do not compare the template part for template classes.  */
31685796c8dcSSimon Schubert       if (cp == NULL)
31695796c8dcSSimon Schubert 	len = strlen (dname);
31705796c8dcSSimon Schubert       else
31715796c8dcSSimon Schubert 	len = cp - dname;
31725796c8dcSSimon Schubert       if (strlen (name + 1) != len || strncmp (dname, name + 1, len) != 0)
31735796c8dcSSimon Schubert 	error (_("name of destructor must equal name of class"));
31745796c8dcSSimon Schubert       else
31755796c8dcSSimon Schubert 	return 1;
31765796c8dcSSimon Schubert     }
31775796c8dcSSimon Schubert   return 0;
31785796c8dcSSimon Schubert }
31795796c8dcSSimon Schubert 
31805796c8dcSSimon Schubert /* C++: Given an aggregate type CURTYPE, and a member name NAME,
31815796c8dcSSimon Schubert    return the appropriate member (or the address of the member, if
31825796c8dcSSimon Schubert    WANT_ADDRESS).  This function is used to resolve user expressions
31835796c8dcSSimon Schubert    of the form "DOMAIN::NAME".  For more details on what happens, see
31845796c8dcSSimon Schubert    the comment before value_struct_elt_for_reference.  */
31855796c8dcSSimon Schubert 
31865796c8dcSSimon Schubert struct value *
value_aggregate_elt(struct type * curtype,char * name,struct type * expect_type,int want_address,enum noside noside)3187cf7f2e2dSJohn Marino value_aggregate_elt (struct type *curtype, char *name,
3188cf7f2e2dSJohn Marino 		     struct type *expect_type, int want_address,
31895796c8dcSSimon Schubert 		     enum noside noside)
31905796c8dcSSimon Schubert {
31915796c8dcSSimon Schubert   switch (TYPE_CODE (curtype))
31925796c8dcSSimon Schubert     {
31935796c8dcSSimon Schubert     case TYPE_CODE_STRUCT:
31945796c8dcSSimon Schubert     case TYPE_CODE_UNION:
31955796c8dcSSimon Schubert       return value_struct_elt_for_reference (curtype, 0, curtype,
3196cf7f2e2dSJohn Marino 					     name, expect_type,
31975796c8dcSSimon Schubert 					     want_address, noside);
31985796c8dcSSimon Schubert     case TYPE_CODE_NAMESPACE:
31995796c8dcSSimon Schubert       return value_namespace_elt (curtype, name,
32005796c8dcSSimon Schubert 				  want_address, noside);
32015796c8dcSSimon Schubert     default:
32025796c8dcSSimon Schubert       internal_error (__FILE__, __LINE__,
32035796c8dcSSimon Schubert 		      _("non-aggregate type in value_aggregate_elt"));
32045796c8dcSSimon Schubert     }
32055796c8dcSSimon Schubert }
32065796c8dcSSimon Schubert 
3207cf7f2e2dSJohn Marino /* Compares the two method/function types T1 and T2 for "equality"
3208c50c785cSJohn Marino    with respect to the methods' parameters.  If the types of the
3209cf7f2e2dSJohn Marino    two parameter lists are the same, returns 1; 0 otherwise.  This
3210cf7f2e2dSJohn Marino    comparison may ignore any artificial parameters in T1 if
3211cf7f2e2dSJohn Marino    SKIP_ARTIFICIAL is non-zero.  This function will ALWAYS skip
3212cf7f2e2dSJohn Marino    the first artificial parameter in T1, assumed to be a 'this' pointer.
3213cf7f2e2dSJohn Marino 
3214cf7f2e2dSJohn Marino    The type T2 is expected to have come from make_params (in eval.c).  */
3215cf7f2e2dSJohn Marino 
3216cf7f2e2dSJohn Marino static int
compare_parameters(struct type * t1,struct type * t2,int skip_artificial)3217cf7f2e2dSJohn Marino compare_parameters (struct type *t1, struct type *t2, int skip_artificial)
3218cf7f2e2dSJohn Marino {
3219cf7f2e2dSJohn Marino   int start = 0;
3220cf7f2e2dSJohn Marino 
3221c50c785cSJohn Marino   if (TYPE_NFIELDS (t1) > 0 && TYPE_FIELD_ARTIFICIAL (t1, 0))
3222cf7f2e2dSJohn Marino     ++start;
3223cf7f2e2dSJohn Marino 
3224cf7f2e2dSJohn Marino   /* If skipping artificial fields, find the first real field
3225cf7f2e2dSJohn Marino      in T1.  */
3226cf7f2e2dSJohn Marino   if (skip_artificial)
3227cf7f2e2dSJohn Marino     {
3228cf7f2e2dSJohn Marino       while (start < TYPE_NFIELDS (t1)
3229cf7f2e2dSJohn Marino 	     && TYPE_FIELD_ARTIFICIAL (t1, start))
3230cf7f2e2dSJohn Marino 	++start;
3231cf7f2e2dSJohn Marino     }
3232cf7f2e2dSJohn Marino 
3233c50c785cSJohn Marino   /* Now compare parameters.  */
3234cf7f2e2dSJohn Marino 
3235cf7f2e2dSJohn Marino   /* Special case: a method taking void.  T1 will contain no
3236cf7f2e2dSJohn Marino      non-artificial fields, and T2 will contain TYPE_CODE_VOID.  */
3237cf7f2e2dSJohn Marino   if ((TYPE_NFIELDS (t1) - start) == 0 && TYPE_NFIELDS (t2) == 1
3238cf7f2e2dSJohn Marino       && TYPE_CODE (TYPE_FIELD_TYPE (t2, 0)) == TYPE_CODE_VOID)
3239cf7f2e2dSJohn Marino     return 1;
3240cf7f2e2dSJohn Marino 
3241cf7f2e2dSJohn Marino   if ((TYPE_NFIELDS (t1) - start) == TYPE_NFIELDS (t2))
3242cf7f2e2dSJohn Marino     {
3243cf7f2e2dSJohn Marino       int i;
3244cf7f2e2dSJohn Marino 
3245cf7f2e2dSJohn Marino       for (i = 0; i < TYPE_NFIELDS (t2); ++i)
3246cf7f2e2dSJohn Marino 	{
3247c50c785cSJohn Marino 	  if (compare_ranks (rank_one_type (TYPE_FIELD_TYPE (t1, start + i),
3248a45ae5f8SJohn Marino 					    TYPE_FIELD_TYPE (t2, i), NULL),
3249c50c785cSJohn Marino 	                     EXACT_MATCH_BADNESS) != 0)
3250cf7f2e2dSJohn Marino 	    return 0;
3251cf7f2e2dSJohn Marino 	}
3252cf7f2e2dSJohn Marino 
3253cf7f2e2dSJohn Marino       return 1;
3254cf7f2e2dSJohn Marino     }
3255cf7f2e2dSJohn Marino 
3256cf7f2e2dSJohn Marino   return 0;
3257cf7f2e2dSJohn Marino }
3258cf7f2e2dSJohn Marino 
32595796c8dcSSimon Schubert /* C++: Given an aggregate type CURTYPE, and a member name NAME,
32605796c8dcSSimon Schubert    return the address of this member as a "pointer to member" type.
32615796c8dcSSimon Schubert    If INTYPE is non-null, then it will be the type of the member we
32625796c8dcSSimon Schubert    are looking for.  This will help us resolve "pointers to member
32635796c8dcSSimon Schubert    functions".  This function is used to resolve user expressions of
32645796c8dcSSimon Schubert    the form "DOMAIN::NAME".  */
32655796c8dcSSimon Schubert 
32665796c8dcSSimon Schubert static struct value *
value_struct_elt_for_reference(struct type * domain,int offset,struct type * curtype,char * name,struct type * intype,int want_address,enum noside noside)32675796c8dcSSimon Schubert value_struct_elt_for_reference (struct type *domain, int offset,
32685796c8dcSSimon Schubert 				struct type *curtype, char *name,
32695796c8dcSSimon Schubert 				struct type *intype,
32705796c8dcSSimon Schubert 				int want_address,
32715796c8dcSSimon Schubert 				enum noside noside)
32725796c8dcSSimon Schubert {
32735796c8dcSSimon Schubert   struct type *t = curtype;
32745796c8dcSSimon Schubert   int i;
32755796c8dcSSimon Schubert   struct value *v, *result;
32765796c8dcSSimon Schubert 
32775796c8dcSSimon Schubert   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
32785796c8dcSSimon Schubert       && TYPE_CODE (t) != TYPE_CODE_UNION)
3279c50c785cSJohn Marino     error (_("Internal error: non-aggregate type "
3280c50c785cSJohn Marino 	     "to value_struct_elt_for_reference"));
32815796c8dcSSimon Schubert 
32825796c8dcSSimon Schubert   for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
32835796c8dcSSimon Schubert     {
3284*ef5ccd6cSJohn Marino       const char *t_field_name = TYPE_FIELD_NAME (t, i);
32855796c8dcSSimon Schubert 
32865796c8dcSSimon Schubert       if (t_field_name && strcmp (t_field_name, name) == 0)
32875796c8dcSSimon Schubert 	{
32885796c8dcSSimon Schubert 	  if (field_is_static (&TYPE_FIELD (t, i)))
32895796c8dcSSimon Schubert 	    {
32905796c8dcSSimon Schubert 	      v = value_static_field (t, i);
32915796c8dcSSimon Schubert 	      if (v == NULL)
32925796c8dcSSimon Schubert 		error (_("static field %s has been optimized out"),
32935796c8dcSSimon Schubert 		       name);
32945796c8dcSSimon Schubert 	      if (want_address)
32955796c8dcSSimon Schubert 		v = value_addr (v);
32965796c8dcSSimon Schubert 	      return v;
32975796c8dcSSimon Schubert 	    }
32985796c8dcSSimon Schubert 	  if (TYPE_FIELD_PACKED (t, i))
32995796c8dcSSimon Schubert 	    error (_("pointers to bitfield members not allowed"));
33005796c8dcSSimon Schubert 
33015796c8dcSSimon Schubert 	  if (want_address)
33025796c8dcSSimon Schubert 	    return value_from_longest
33035796c8dcSSimon Schubert 	      (lookup_memberptr_type (TYPE_FIELD_TYPE (t, i), domain),
33045796c8dcSSimon Schubert 	       offset + (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3));
33055796c8dcSSimon Schubert 	  else if (noside == EVAL_AVOID_SIDE_EFFECTS)
33065796c8dcSSimon Schubert 	    return allocate_value (TYPE_FIELD_TYPE (t, i));
33075796c8dcSSimon Schubert 	  else
33085796c8dcSSimon Schubert 	    error (_("Cannot reference non-static field \"%s\""), name);
33095796c8dcSSimon Schubert 	}
33105796c8dcSSimon Schubert     }
33115796c8dcSSimon Schubert 
33125796c8dcSSimon Schubert   /* C++: If it was not found as a data field, then try to return it
33135796c8dcSSimon Schubert      as a pointer to a method.  */
33145796c8dcSSimon Schubert 
33155796c8dcSSimon Schubert   /* Perform all necessary dereferencing.  */
33165796c8dcSSimon Schubert   while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR)
33175796c8dcSSimon Schubert     intype = TYPE_TARGET_TYPE (intype);
33185796c8dcSSimon Schubert 
33195796c8dcSSimon Schubert   for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
33205796c8dcSSimon Schubert     {
3321*ef5ccd6cSJohn Marino       const char *t_field_name = TYPE_FN_FIELDLIST_NAME (t, i);
33225796c8dcSSimon Schubert       char dem_opname[64];
33235796c8dcSSimon Schubert 
33245796c8dcSSimon Schubert       if (strncmp (t_field_name, "__", 2) == 0
33255796c8dcSSimon Schubert 	  || strncmp (t_field_name, "op", 2) == 0
33265796c8dcSSimon Schubert 	  || strncmp (t_field_name, "type", 4) == 0)
33275796c8dcSSimon Schubert 	{
33285796c8dcSSimon Schubert 	  if (cplus_demangle_opname (t_field_name,
33295796c8dcSSimon Schubert 				     dem_opname, DMGL_ANSI))
33305796c8dcSSimon Schubert 	    t_field_name = dem_opname;
33315796c8dcSSimon Schubert 	  else if (cplus_demangle_opname (t_field_name,
33325796c8dcSSimon Schubert 					  dem_opname, 0))
33335796c8dcSSimon Schubert 	    t_field_name = dem_opname;
33345796c8dcSSimon Schubert 	}
33355796c8dcSSimon Schubert       if (t_field_name && strcmp (t_field_name, name) == 0)
33365796c8dcSSimon Schubert 	{
3337cf7f2e2dSJohn Marino 	  int j;
3338cf7f2e2dSJohn Marino 	  int len = TYPE_FN_FIELDLIST_LENGTH (t, i);
33395796c8dcSSimon Schubert 	  struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
33405796c8dcSSimon Schubert 
33415796c8dcSSimon Schubert 	  check_stub_method_group (t, i);
33425796c8dcSSimon Schubert 
33435796c8dcSSimon Schubert 	  if (intype)
33445796c8dcSSimon Schubert 	    {
3345cf7f2e2dSJohn Marino 	      for (j = 0; j < len; ++j)
3346cf7f2e2dSJohn Marino 		{
3347cf7f2e2dSJohn Marino 		  if (compare_parameters (TYPE_FN_FIELD_TYPE (f, j), intype, 0)
3348c50c785cSJohn Marino 		      || compare_parameters (TYPE_FN_FIELD_TYPE (f, j),
3349c50c785cSJohn Marino 					     intype, 1))
33505796c8dcSSimon Schubert 		    break;
3351cf7f2e2dSJohn Marino 		}
3352cf7f2e2dSJohn Marino 
3353cf7f2e2dSJohn Marino 	      if (j == len)
3354c50c785cSJohn Marino 		error (_("no member function matches "
3355c50c785cSJohn Marino 			 "that type instantiation"));
33565796c8dcSSimon Schubert 	    }
33575796c8dcSSimon Schubert 	  else
3358cf7f2e2dSJohn Marino 	    {
3359cf7f2e2dSJohn Marino 	      int ii;
3360cf7f2e2dSJohn Marino 
3361cf7f2e2dSJohn Marino 	      j = -1;
3362c50c785cSJohn Marino 	      for (ii = 0; ii < len; ++ii)
3363cf7f2e2dSJohn Marino 		{
3364cf7f2e2dSJohn Marino 		  /* Skip artificial methods.  This is necessary if,
3365cf7f2e2dSJohn Marino 		     for example, the user wants to "print
3366cf7f2e2dSJohn Marino 		     subclass::subclass" with only one user-defined
3367c50c785cSJohn Marino 		     constructor.  There is no ambiguity in this case.
3368c50c785cSJohn Marino 		     We are careful here to allow artificial methods
3369c50c785cSJohn Marino 		     if they are the unique result.  */
3370cf7f2e2dSJohn Marino 		  if (TYPE_FN_FIELD_ARTIFICIAL (f, ii))
3371c50c785cSJohn Marino 		    {
3372c50c785cSJohn Marino 		      if (j == -1)
3373c50c785cSJohn Marino 			j = ii;
3374cf7f2e2dSJohn Marino 		      continue;
3375c50c785cSJohn Marino 		    }
3376cf7f2e2dSJohn Marino 
3377cf7f2e2dSJohn Marino 		  /* Desired method is ambiguous if more than one
3378cf7f2e2dSJohn Marino 		     method is defined.  */
3379c50c785cSJohn Marino 		  if (j != -1 && !TYPE_FN_FIELD_ARTIFICIAL (f, j))
3380c50c785cSJohn Marino 		    error (_("non-unique member `%s' requires "
3381c50c785cSJohn Marino 			     "type instantiation"), name);
3382cf7f2e2dSJohn Marino 
3383cf7f2e2dSJohn Marino 		  j = ii;
3384cf7f2e2dSJohn Marino 		}
3385c50c785cSJohn Marino 
3386c50c785cSJohn Marino 	      if (j == -1)
3387c50c785cSJohn Marino 		error (_("no matching member function"));
3388cf7f2e2dSJohn Marino 	    }
33895796c8dcSSimon Schubert 
33905796c8dcSSimon Schubert 	  if (TYPE_FN_FIELD_STATIC_P (f, j))
33915796c8dcSSimon Schubert 	    {
33925796c8dcSSimon Schubert 	      struct symbol *s =
33935796c8dcSSimon Schubert 		lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
33945796c8dcSSimon Schubert 			       0, VAR_DOMAIN, 0);
3395cf7f2e2dSJohn Marino 
33965796c8dcSSimon Schubert 	      if (s == NULL)
33975796c8dcSSimon Schubert 		return NULL;
33985796c8dcSSimon Schubert 
33995796c8dcSSimon Schubert 	      if (want_address)
34005796c8dcSSimon Schubert 		return value_addr (read_var_value (s, 0));
34015796c8dcSSimon Schubert 	      else
34025796c8dcSSimon Schubert 		return read_var_value (s, 0);
34035796c8dcSSimon Schubert 	    }
34045796c8dcSSimon Schubert 
34055796c8dcSSimon Schubert 	  if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
34065796c8dcSSimon Schubert 	    {
34075796c8dcSSimon Schubert 	      if (want_address)
34085796c8dcSSimon Schubert 		{
34095796c8dcSSimon Schubert 		  result = allocate_value
34105796c8dcSSimon Schubert 		    (lookup_methodptr_type (TYPE_FN_FIELD_TYPE (f, j)));
34115796c8dcSSimon Schubert 		  cplus_make_method_ptr (value_type (result),
34125796c8dcSSimon Schubert 					 value_contents_writeable (result),
34135796c8dcSSimon Schubert 					 TYPE_FN_FIELD_VOFFSET (f, j), 1);
34145796c8dcSSimon Schubert 		}
34155796c8dcSSimon Schubert 	      else if (noside == EVAL_AVOID_SIDE_EFFECTS)
34165796c8dcSSimon Schubert 		return allocate_value (TYPE_FN_FIELD_TYPE (f, j));
34175796c8dcSSimon Schubert 	      else
34185796c8dcSSimon Schubert 		error (_("Cannot reference virtual member function \"%s\""),
34195796c8dcSSimon Schubert 		       name);
34205796c8dcSSimon Schubert 	    }
34215796c8dcSSimon Schubert 	  else
34225796c8dcSSimon Schubert 	    {
34235796c8dcSSimon Schubert 	      struct symbol *s =
34245796c8dcSSimon Schubert 		lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
34255796c8dcSSimon Schubert 			       0, VAR_DOMAIN, 0);
3426cf7f2e2dSJohn Marino 
34275796c8dcSSimon Schubert 	      if (s == NULL)
34285796c8dcSSimon Schubert 		return NULL;
34295796c8dcSSimon Schubert 
34305796c8dcSSimon Schubert 	      v = read_var_value (s, 0);
34315796c8dcSSimon Schubert 	      if (!want_address)
34325796c8dcSSimon Schubert 		result = v;
34335796c8dcSSimon Schubert 	      else
34345796c8dcSSimon Schubert 		{
34355796c8dcSSimon Schubert 		  result = allocate_value (lookup_methodptr_type (TYPE_FN_FIELD_TYPE (f, j)));
34365796c8dcSSimon Schubert 		  cplus_make_method_ptr (value_type (result),
34375796c8dcSSimon Schubert 					 value_contents_writeable (result),
34385796c8dcSSimon Schubert 					 value_address (v), 0);
34395796c8dcSSimon Schubert 		}
34405796c8dcSSimon Schubert 	    }
34415796c8dcSSimon Schubert 	  return result;
34425796c8dcSSimon Schubert 	}
34435796c8dcSSimon Schubert     }
34445796c8dcSSimon Schubert   for (i = TYPE_N_BASECLASSES (t) - 1; i >= 0; i--)
34455796c8dcSSimon Schubert     {
34465796c8dcSSimon Schubert       struct value *v;
34475796c8dcSSimon Schubert       int base_offset;
34485796c8dcSSimon Schubert 
34495796c8dcSSimon Schubert       if (BASETYPE_VIA_VIRTUAL (t, i))
34505796c8dcSSimon Schubert 	base_offset = 0;
34515796c8dcSSimon Schubert       else
34525796c8dcSSimon Schubert 	base_offset = TYPE_BASECLASS_BITPOS (t, i) / 8;
34535796c8dcSSimon Schubert       v = value_struct_elt_for_reference (domain,
34545796c8dcSSimon Schubert 					  offset + base_offset,
34555796c8dcSSimon Schubert 					  TYPE_BASECLASS (t, i),
34565796c8dcSSimon Schubert 					  name, intype,
34575796c8dcSSimon Schubert 					  want_address, noside);
34585796c8dcSSimon Schubert       if (v)
34595796c8dcSSimon Schubert 	return v;
34605796c8dcSSimon Schubert     }
34615796c8dcSSimon Schubert 
34625796c8dcSSimon Schubert   /* As a last chance, pretend that CURTYPE is a namespace, and look
34635796c8dcSSimon Schubert      it up that way; this (frequently) works for types nested inside
34645796c8dcSSimon Schubert      classes.  */
34655796c8dcSSimon Schubert 
34665796c8dcSSimon Schubert   return value_maybe_namespace_elt (curtype, name,
34675796c8dcSSimon Schubert 				    want_address, noside);
34685796c8dcSSimon Schubert }
34695796c8dcSSimon Schubert 
34705796c8dcSSimon Schubert /* C++: Return the member NAME of the namespace given by the type
34715796c8dcSSimon Schubert    CURTYPE.  */
34725796c8dcSSimon Schubert 
34735796c8dcSSimon Schubert static struct value *
value_namespace_elt(const struct type * curtype,char * name,int want_address,enum noside noside)34745796c8dcSSimon Schubert value_namespace_elt (const struct type *curtype,
34755796c8dcSSimon Schubert 		     char *name, int want_address,
34765796c8dcSSimon Schubert 		     enum noside noside)
34775796c8dcSSimon Schubert {
34785796c8dcSSimon Schubert   struct value *retval = value_maybe_namespace_elt (curtype, name,
34795796c8dcSSimon Schubert 						    want_address,
34805796c8dcSSimon Schubert 						    noside);
34815796c8dcSSimon Schubert 
34825796c8dcSSimon Schubert   if (retval == NULL)
34835796c8dcSSimon Schubert     error (_("No symbol \"%s\" in namespace \"%s\"."),
34845796c8dcSSimon Schubert 	   name, TYPE_TAG_NAME (curtype));
34855796c8dcSSimon Schubert 
34865796c8dcSSimon Schubert   return retval;
34875796c8dcSSimon Schubert }
34885796c8dcSSimon Schubert 
34895796c8dcSSimon Schubert /* A helper function used by value_namespace_elt and
34905796c8dcSSimon Schubert    value_struct_elt_for_reference.  It looks up NAME inside the
34915796c8dcSSimon Schubert    context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
34925796c8dcSSimon Schubert    is a class and NAME refers to a type in CURTYPE itself (as opposed
34935796c8dcSSimon Schubert    to, say, some base class of CURTYPE).  */
34945796c8dcSSimon Schubert 
34955796c8dcSSimon Schubert static struct value *
value_maybe_namespace_elt(const struct type * curtype,char * name,int want_address,enum noside noside)34965796c8dcSSimon Schubert value_maybe_namespace_elt (const struct type *curtype,
34975796c8dcSSimon Schubert 			   char *name, int want_address,
34985796c8dcSSimon Schubert 			   enum noside noside)
34995796c8dcSSimon Schubert {
35005796c8dcSSimon Schubert   const char *namespace_name = TYPE_TAG_NAME (curtype);
35015796c8dcSSimon Schubert   struct symbol *sym;
35025796c8dcSSimon Schubert   struct value *result;
35035796c8dcSSimon Schubert 
3504cf7f2e2dSJohn Marino   sym = cp_lookup_symbol_namespace (namespace_name, name,
3505cf7f2e2dSJohn Marino 				    get_selected_block (0), VAR_DOMAIN);
3506cf7f2e2dSJohn Marino 
3507cf7f2e2dSJohn Marino   if (sym == NULL)
3508cf7f2e2dSJohn Marino     {
3509cf7f2e2dSJohn Marino       char *concatenated_name = alloca (strlen (namespace_name) + 2
3510cf7f2e2dSJohn Marino 					+ strlen (name) + 1);
3511cf7f2e2dSJohn Marino 
3512cf7f2e2dSJohn Marino       sprintf (concatenated_name, "%s::%s", namespace_name, name);
3513cf7f2e2dSJohn Marino       sym = lookup_static_symbol_aux (concatenated_name, VAR_DOMAIN);
3514cf7f2e2dSJohn Marino     }
35155796c8dcSSimon Schubert 
35165796c8dcSSimon Schubert   if (sym == NULL)
35175796c8dcSSimon Schubert     return NULL;
35185796c8dcSSimon Schubert   else if ((noside == EVAL_AVOID_SIDE_EFFECTS)
35195796c8dcSSimon Schubert 	   && (SYMBOL_CLASS (sym) == LOC_TYPEDEF))
35205796c8dcSSimon Schubert     result = allocate_value (SYMBOL_TYPE (sym));
35215796c8dcSSimon Schubert   else
35225796c8dcSSimon Schubert     result = value_of_variable (sym, get_selected_block (0));
35235796c8dcSSimon Schubert 
35245796c8dcSSimon Schubert   if (result && want_address)
35255796c8dcSSimon Schubert     result = value_addr (result);
35265796c8dcSSimon Schubert 
35275796c8dcSSimon Schubert   return result;
35285796c8dcSSimon Schubert }
35295796c8dcSSimon Schubert 
3530*ef5ccd6cSJohn Marino /* Given a pointer or a reference value V, find its real (RTTI) type.
35315796c8dcSSimon Schubert 
35325796c8dcSSimon Schubert    Other parameters FULL, TOP, USING_ENC as with value_rtti_type()
35335796c8dcSSimon Schubert    and refer to the values computed for the object pointed to.  */
35345796c8dcSSimon Schubert 
35355796c8dcSSimon Schubert struct type *
value_rtti_indirect_type(struct value * v,int * full,int * top,int * using_enc)3536*ef5ccd6cSJohn Marino value_rtti_indirect_type (struct value *v, int *full,
35375796c8dcSSimon Schubert 			  int *top, int *using_enc)
35385796c8dcSSimon Schubert {
35395796c8dcSSimon Schubert   struct value *target;
3540*ef5ccd6cSJohn Marino   struct type *type, *real_type, *target_type;
35415796c8dcSSimon Schubert 
3542*ef5ccd6cSJohn Marino   type = value_type (v);
3543*ef5ccd6cSJohn Marino   type = check_typedef (type);
3544*ef5ccd6cSJohn Marino   if (TYPE_CODE (type) == TYPE_CODE_REF)
3545*ef5ccd6cSJohn Marino     target = coerce_ref (v);
3546*ef5ccd6cSJohn Marino   else if (TYPE_CODE (type) == TYPE_CODE_PTR)
35475796c8dcSSimon Schubert     target = value_ind (v);
3548*ef5ccd6cSJohn Marino   else
3549*ef5ccd6cSJohn Marino     return NULL;
35505796c8dcSSimon Schubert 
3551*ef5ccd6cSJohn Marino   real_type = value_rtti_type (target, full, top, using_enc);
3552*ef5ccd6cSJohn Marino 
3553*ef5ccd6cSJohn Marino   if (real_type)
3554*ef5ccd6cSJohn Marino     {
3555*ef5ccd6cSJohn Marino       /* Copy qualifiers to the referenced object.  */
3556*ef5ccd6cSJohn Marino       target_type = value_type (target);
3557*ef5ccd6cSJohn Marino       real_type = make_cv_type (TYPE_CONST (target_type),
3558*ef5ccd6cSJohn Marino 				TYPE_VOLATILE (target_type), real_type, NULL);
3559*ef5ccd6cSJohn Marino       if (TYPE_CODE (type) == TYPE_CODE_REF)
3560*ef5ccd6cSJohn Marino         real_type = lookup_reference_type (real_type);
3561*ef5ccd6cSJohn Marino       else if (TYPE_CODE (type) == TYPE_CODE_PTR)
3562*ef5ccd6cSJohn Marino         real_type = lookup_pointer_type (real_type);
3563*ef5ccd6cSJohn Marino       else
3564*ef5ccd6cSJohn Marino         internal_error (__FILE__, __LINE__, _("Unexpected value type."));
3565*ef5ccd6cSJohn Marino 
3566*ef5ccd6cSJohn Marino       /* Copy qualifiers to the pointer/reference.  */
3567*ef5ccd6cSJohn Marino       real_type = make_cv_type (TYPE_CONST (type), TYPE_VOLATILE (type),
3568*ef5ccd6cSJohn Marino 				real_type, NULL);
3569*ef5ccd6cSJohn Marino     }
3570*ef5ccd6cSJohn Marino 
3571*ef5ccd6cSJohn Marino   return real_type;
35725796c8dcSSimon Schubert }
35735796c8dcSSimon Schubert 
35745796c8dcSSimon Schubert /* Given a value pointed to by ARGP, check its real run-time type, and
35755796c8dcSSimon Schubert    if that is different from the enclosing type, create a new value
35765796c8dcSSimon Schubert    using the real run-time type as the enclosing type (and of the same
35775796c8dcSSimon Schubert    type as ARGP) and return it, with the embedded offset adjusted to
35785796c8dcSSimon Schubert    be the correct offset to the enclosed object.  RTYPE is the type,
35795796c8dcSSimon Schubert    and XFULL, XTOP, and XUSING_ENC are the other parameters, computed
35805796c8dcSSimon Schubert    by value_rtti_type().  If these are available, they can be supplied
35815796c8dcSSimon Schubert    and a second call to value_rtti_type() is avoided.  (Pass RTYPE ==
35825796c8dcSSimon Schubert    NULL if they're not available.  */
35835796c8dcSSimon Schubert 
35845796c8dcSSimon Schubert struct value *
value_full_object(struct value * argp,struct type * rtype,int xfull,int xtop,int xusing_enc)35855796c8dcSSimon Schubert value_full_object (struct value *argp,
35865796c8dcSSimon Schubert 		   struct type *rtype,
35875796c8dcSSimon Schubert 		   int xfull, int xtop,
35885796c8dcSSimon Schubert 		   int xusing_enc)
35895796c8dcSSimon Schubert {
35905796c8dcSSimon Schubert   struct type *real_type;
35915796c8dcSSimon Schubert   int full = 0;
35925796c8dcSSimon Schubert   int top = -1;
35935796c8dcSSimon Schubert   int using_enc = 0;
35945796c8dcSSimon Schubert   struct value *new_val;
35955796c8dcSSimon Schubert 
35965796c8dcSSimon Schubert   if (rtype)
35975796c8dcSSimon Schubert     {
35985796c8dcSSimon Schubert       real_type = rtype;
35995796c8dcSSimon Schubert       full = xfull;
36005796c8dcSSimon Schubert       top = xtop;
36015796c8dcSSimon Schubert       using_enc = xusing_enc;
36025796c8dcSSimon Schubert     }
36035796c8dcSSimon Schubert   else
36045796c8dcSSimon Schubert     real_type = value_rtti_type (argp, &full, &top, &using_enc);
36055796c8dcSSimon Schubert 
36065796c8dcSSimon Schubert   /* If no RTTI data, or if object is already complete, do nothing.  */
36075796c8dcSSimon Schubert   if (!real_type || real_type == value_enclosing_type (argp))
36085796c8dcSSimon Schubert     return argp;
36095796c8dcSSimon Schubert 
3610a45ae5f8SJohn Marino   /* In a destructor we might see a real type that is a superclass of
3611a45ae5f8SJohn Marino      the object's type.  In this case it is better to leave the object
3612a45ae5f8SJohn Marino      as-is.  */
3613a45ae5f8SJohn Marino   if (full
3614a45ae5f8SJohn Marino       && TYPE_LENGTH (real_type) < TYPE_LENGTH (value_enclosing_type (argp)))
3615a45ae5f8SJohn Marino     return argp;
3616a45ae5f8SJohn Marino 
36175796c8dcSSimon Schubert   /* If we have the full object, but for some reason the enclosing
36185796c8dcSSimon Schubert      type is wrong, set it.  */
36195796c8dcSSimon Schubert   /* pai: FIXME -- sounds iffy */
36205796c8dcSSimon Schubert   if (full)
36215796c8dcSSimon Schubert     {
3622c50c785cSJohn Marino       argp = value_copy (argp);
3623c50c785cSJohn Marino       set_value_enclosing_type (argp, real_type);
36245796c8dcSSimon Schubert       return argp;
36255796c8dcSSimon Schubert     }
36265796c8dcSSimon Schubert 
3627c50c785cSJohn Marino   /* Check if object is in memory.  */
36285796c8dcSSimon Schubert   if (VALUE_LVAL (argp) != lval_memory)
36295796c8dcSSimon Schubert     {
3630c50c785cSJohn Marino       warning (_("Couldn't retrieve complete object of RTTI "
3631c50c785cSJohn Marino 		 "type %s; object may be in register(s)."),
36325796c8dcSSimon Schubert 	       TYPE_NAME (real_type));
36335796c8dcSSimon Schubert 
36345796c8dcSSimon Schubert       return argp;
36355796c8dcSSimon Schubert     }
36365796c8dcSSimon Schubert 
36375796c8dcSSimon Schubert   /* All other cases -- retrieve the complete object.  */
36385796c8dcSSimon Schubert   /* Go back by the computed top_offset from the beginning of the
36395796c8dcSSimon Schubert      object, adjusting for the embedded offset of argp if that's what
36405796c8dcSSimon Schubert      value_rtti_type used for its computation.  */
36415796c8dcSSimon Schubert   new_val = value_at_lazy (real_type, value_address (argp) - top +
36425796c8dcSSimon Schubert 			   (using_enc ? 0 : value_embedded_offset (argp)));
36435796c8dcSSimon Schubert   deprecated_set_value_type (new_val, value_type (argp));
36445796c8dcSSimon Schubert   set_value_embedded_offset (new_val, (using_enc
36455796c8dcSSimon Schubert 				       ? top + value_embedded_offset (argp)
36465796c8dcSSimon Schubert 				       : top));
36475796c8dcSSimon Schubert   return new_val;
36485796c8dcSSimon Schubert }
36495796c8dcSSimon Schubert 
36505796c8dcSSimon Schubert 
3651a45ae5f8SJohn Marino /* Return the value of the local variable, if one exists.  Throw error
3652a45ae5f8SJohn Marino    otherwise, such as if the request is made in an inappropriate context.  */
36535796c8dcSSimon Schubert 
36545796c8dcSSimon Schubert struct value *
value_of_this(const struct language_defn * lang)3655a45ae5f8SJohn Marino value_of_this (const struct language_defn *lang)
36565796c8dcSSimon Schubert {
3657a45ae5f8SJohn Marino   struct symbol *sym;
36585796c8dcSSimon Schubert   struct block *b;
36595796c8dcSSimon Schubert   struct frame_info *frame;
36605796c8dcSSimon Schubert 
3661a45ae5f8SJohn Marino   if (!lang->la_name_of_this)
3662a45ae5f8SJohn Marino     error (_("no `this' in current language"));
3663a45ae5f8SJohn Marino 
36645796c8dcSSimon Schubert   frame = get_selected_frame (_("no frame selected"));
36655796c8dcSSimon Schubert 
3666a45ae5f8SJohn Marino   b = get_frame_block (frame, NULL);
36675796c8dcSSimon Schubert 
3668a45ae5f8SJohn Marino   sym = lookup_language_this (lang, b);
36695796c8dcSSimon Schubert   if (sym == NULL)
36705796c8dcSSimon Schubert     error (_("current stack frame does not contain a variable named `%s'"),
3671a45ae5f8SJohn Marino 	   lang->la_name_of_this);
3672a45ae5f8SJohn Marino 
3673a45ae5f8SJohn Marino   return read_var_value (sym, frame);
36745796c8dcSSimon Schubert }
36755796c8dcSSimon Schubert 
3676a45ae5f8SJohn Marino /* Return the value of the local variable, if one exists.  Return NULL
3677a45ae5f8SJohn Marino    otherwise.  Never throw error.  */
36785796c8dcSSimon Schubert 
36795796c8dcSSimon Schubert struct value *
value_of_this_silent(const struct language_defn * lang)3680a45ae5f8SJohn Marino value_of_this_silent (const struct language_defn *lang)
36815796c8dcSSimon Schubert {
3682a45ae5f8SJohn Marino   struct value *ret = NULL;
3683a45ae5f8SJohn Marino   volatile struct gdb_exception except;
3684a45ae5f8SJohn Marino 
3685a45ae5f8SJohn Marino   TRY_CATCH (except, RETURN_MASK_ERROR)
3686a45ae5f8SJohn Marino     {
3687a45ae5f8SJohn Marino       ret = value_of_this (lang);
3688a45ae5f8SJohn Marino     }
3689a45ae5f8SJohn Marino 
3690a45ae5f8SJohn Marino   return ret;
36915796c8dcSSimon Schubert }
36925796c8dcSSimon Schubert 
36935796c8dcSSimon Schubert /* Create a slice (sub-string, sub-array) of ARRAY, that is LENGTH
36945796c8dcSSimon Schubert    elements long, starting at LOWBOUND.  The result has the same lower
36955796c8dcSSimon Schubert    bound as the original ARRAY.  */
36965796c8dcSSimon Schubert 
36975796c8dcSSimon Schubert struct value *
value_slice(struct value * array,int lowbound,int length)36985796c8dcSSimon Schubert value_slice (struct value *array, int lowbound, int length)
36995796c8dcSSimon Schubert {
37005796c8dcSSimon Schubert   struct type *slice_range_type, *slice_type, *range_type;
37015796c8dcSSimon Schubert   LONGEST lowerbound, upperbound;
37025796c8dcSSimon Schubert   struct value *slice;
37035796c8dcSSimon Schubert   struct type *array_type;
37045796c8dcSSimon Schubert 
37055796c8dcSSimon Schubert   array_type = check_typedef (value_type (array));
37065796c8dcSSimon Schubert   if (TYPE_CODE (array_type) != TYPE_CODE_ARRAY
3707*ef5ccd6cSJohn Marino       && TYPE_CODE (array_type) != TYPE_CODE_STRING)
37085796c8dcSSimon Schubert     error (_("cannot take slice of non-array"));
37095796c8dcSSimon Schubert 
37105796c8dcSSimon Schubert   range_type = TYPE_INDEX_TYPE (array_type);
37115796c8dcSSimon Schubert   if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0)
37125796c8dcSSimon Schubert     error (_("slice from bad array or bitstring"));
37135796c8dcSSimon Schubert 
37145796c8dcSSimon Schubert   if (lowbound < lowerbound || length < 0
37155796c8dcSSimon Schubert       || lowbound + length - 1 > upperbound)
37165796c8dcSSimon Schubert     error (_("slice out of range"));
37175796c8dcSSimon Schubert 
37185796c8dcSSimon Schubert   /* FIXME-type-allocation: need a way to free this type when we are
37195796c8dcSSimon Schubert      done with it.  */
37205796c8dcSSimon Schubert   slice_range_type = create_range_type ((struct type *) NULL,
37215796c8dcSSimon Schubert 					TYPE_TARGET_TYPE (range_type),
37225796c8dcSSimon Schubert 					lowbound,
37235796c8dcSSimon Schubert 					lowbound + length - 1);
37245796c8dcSSimon Schubert 
37255796c8dcSSimon Schubert     {
37265796c8dcSSimon Schubert       struct type *element_type = TYPE_TARGET_TYPE (array_type);
37275796c8dcSSimon Schubert       LONGEST offset =
37285796c8dcSSimon Schubert 	(lowbound - lowerbound) * TYPE_LENGTH (check_typedef (element_type));
37295796c8dcSSimon Schubert 
37305796c8dcSSimon Schubert       slice_type = create_array_type ((struct type *) NULL,
37315796c8dcSSimon Schubert 				      element_type,
37325796c8dcSSimon Schubert 				      slice_range_type);
37335796c8dcSSimon Schubert       TYPE_CODE (slice_type) = TYPE_CODE (array_type);
37345796c8dcSSimon Schubert 
37355796c8dcSSimon Schubert       if (VALUE_LVAL (array) == lval_memory && value_lazy (array))
37365796c8dcSSimon Schubert 	slice = allocate_value_lazy (slice_type);
37375796c8dcSSimon Schubert       else
37385796c8dcSSimon Schubert 	{
37395796c8dcSSimon Schubert 	  slice = allocate_value (slice_type);
3740c50c785cSJohn Marino 	  value_contents_copy (slice, 0, array, offset,
37415796c8dcSSimon Schubert 			       TYPE_LENGTH (slice_type));
37425796c8dcSSimon Schubert 	}
37435796c8dcSSimon Schubert 
37445796c8dcSSimon Schubert       set_value_component_location (slice, array);
37455796c8dcSSimon Schubert       VALUE_FRAME_ID (slice) = VALUE_FRAME_ID (array);
37465796c8dcSSimon Schubert       set_value_offset (slice, value_offset (array) + offset);
37475796c8dcSSimon Schubert     }
37485796c8dcSSimon Schubert   return slice;
37495796c8dcSSimon Schubert }
37505796c8dcSSimon Schubert 
37515796c8dcSSimon Schubert /* Create a value for a FORTRAN complex number.  Currently most of the
37525796c8dcSSimon Schubert    time values are coerced to COMPLEX*16 (i.e. a complex number
37535796c8dcSSimon Schubert    composed of 2 doubles.  This really should be a smarter routine
37545796c8dcSSimon Schubert    that figures out precision inteligently as opposed to assuming
37555796c8dcSSimon Schubert    doubles.  FIXME: fmb  */
37565796c8dcSSimon Schubert 
37575796c8dcSSimon Schubert struct value *
value_literal_complex(struct value * arg1,struct value * arg2,struct type * type)37585796c8dcSSimon Schubert value_literal_complex (struct value *arg1,
37595796c8dcSSimon Schubert 		       struct value *arg2,
37605796c8dcSSimon Schubert 		       struct type *type)
37615796c8dcSSimon Schubert {
37625796c8dcSSimon Schubert   struct value *val;
37635796c8dcSSimon Schubert   struct type *real_type = TYPE_TARGET_TYPE (type);
37645796c8dcSSimon Schubert 
37655796c8dcSSimon Schubert   val = allocate_value (type);
37665796c8dcSSimon Schubert   arg1 = value_cast (real_type, arg1);
37675796c8dcSSimon Schubert   arg2 = value_cast (real_type, arg2);
37685796c8dcSSimon Schubert 
37695796c8dcSSimon Schubert   memcpy (value_contents_raw (val),
37705796c8dcSSimon Schubert 	  value_contents (arg1), TYPE_LENGTH (real_type));
37715796c8dcSSimon Schubert   memcpy (value_contents_raw (val) + TYPE_LENGTH (real_type),
37725796c8dcSSimon Schubert 	  value_contents (arg2), TYPE_LENGTH (real_type));
37735796c8dcSSimon Schubert   return val;
37745796c8dcSSimon Schubert }
37755796c8dcSSimon Schubert 
37765796c8dcSSimon Schubert /* Cast a value into the appropriate complex data type.  */
37775796c8dcSSimon Schubert 
37785796c8dcSSimon Schubert static struct value *
cast_into_complex(struct type * type,struct value * val)37795796c8dcSSimon Schubert cast_into_complex (struct type *type, struct value *val)
37805796c8dcSSimon Schubert {
37815796c8dcSSimon Schubert   struct type *real_type = TYPE_TARGET_TYPE (type);
37825796c8dcSSimon Schubert 
37835796c8dcSSimon Schubert   if (TYPE_CODE (value_type (val)) == TYPE_CODE_COMPLEX)
37845796c8dcSSimon Schubert     {
37855796c8dcSSimon Schubert       struct type *val_real_type = TYPE_TARGET_TYPE (value_type (val));
37865796c8dcSSimon Schubert       struct value *re_val = allocate_value (val_real_type);
37875796c8dcSSimon Schubert       struct value *im_val = allocate_value (val_real_type);
37885796c8dcSSimon Schubert 
37895796c8dcSSimon Schubert       memcpy (value_contents_raw (re_val),
37905796c8dcSSimon Schubert 	      value_contents (val), TYPE_LENGTH (val_real_type));
37915796c8dcSSimon Schubert       memcpy (value_contents_raw (im_val),
37925796c8dcSSimon Schubert 	      value_contents (val) + TYPE_LENGTH (val_real_type),
37935796c8dcSSimon Schubert 	      TYPE_LENGTH (val_real_type));
37945796c8dcSSimon Schubert 
37955796c8dcSSimon Schubert       return value_literal_complex (re_val, im_val, type);
37965796c8dcSSimon Schubert     }
37975796c8dcSSimon Schubert   else if (TYPE_CODE (value_type (val)) == TYPE_CODE_FLT
37985796c8dcSSimon Schubert 	   || TYPE_CODE (value_type (val)) == TYPE_CODE_INT)
37995796c8dcSSimon Schubert     return value_literal_complex (val,
38005796c8dcSSimon Schubert 				  value_zero (real_type, not_lval),
38015796c8dcSSimon Schubert 				  type);
38025796c8dcSSimon Schubert   else
38035796c8dcSSimon Schubert     error (_("cannot cast non-number to complex"));
38045796c8dcSSimon Schubert }
38055796c8dcSSimon Schubert 
38065796c8dcSSimon Schubert void
_initialize_valops(void)38075796c8dcSSimon Schubert _initialize_valops (void)
38085796c8dcSSimon Schubert {
38095796c8dcSSimon Schubert   add_setshow_boolean_cmd ("overload-resolution", class_support,
38105796c8dcSSimon Schubert 			   &overload_resolution, _("\
38115796c8dcSSimon Schubert Set overload resolution in evaluating C++ functions."), _("\
38125796c8dcSSimon Schubert Show overload resolution in evaluating C++ functions."),
38135796c8dcSSimon Schubert 			   NULL, NULL,
38145796c8dcSSimon Schubert 			   show_overload_resolution,
38155796c8dcSSimon Schubert 			   &setlist, &showlist);
38165796c8dcSSimon Schubert   overload_resolution = 1;
38175796c8dcSSimon Schubert }
3818