xref: /dflybsd-src/contrib/gdb-7/gdb/valops.c (revision c50c785cb49e9377ca78104c5540c7b33f768771)
15796c8dcSSimon Schubert /* Perform non-arithmetic operations on values, for GDB.
25796c8dcSSimon Schubert 
35796c8dcSSimon Schubert    Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
45796c8dcSSimon Schubert    1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
5*c50c785cSJohn Marino    2008, 2009, 2010, 2011 Free Software Foundation, Inc.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This file is part of GDB.
85796c8dcSSimon Schubert 
95796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
105796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
115796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
125796c8dcSSimon Schubert    (at your option) any later version.
135796c8dcSSimon Schubert 
145796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
155796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
165796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
175796c8dcSSimon Schubert    GNU General Public License for more details.
185796c8dcSSimon Schubert 
195796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
205796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
215796c8dcSSimon Schubert 
225796c8dcSSimon Schubert #include "defs.h"
235796c8dcSSimon Schubert #include "symtab.h"
245796c8dcSSimon Schubert #include "gdbtypes.h"
255796c8dcSSimon Schubert #include "value.h"
265796c8dcSSimon Schubert #include "frame.h"
275796c8dcSSimon Schubert #include "inferior.h"
285796c8dcSSimon Schubert #include "gdbcore.h"
295796c8dcSSimon Schubert #include "target.h"
305796c8dcSSimon Schubert #include "demangle.h"
315796c8dcSSimon Schubert #include "language.h"
325796c8dcSSimon Schubert #include "gdbcmd.h"
335796c8dcSSimon Schubert #include "regcache.h"
345796c8dcSSimon Schubert #include "cp-abi.h"
355796c8dcSSimon Schubert #include "block.h"
365796c8dcSSimon Schubert #include "infcall.h"
375796c8dcSSimon Schubert #include "dictionary.h"
385796c8dcSSimon Schubert #include "cp-support.h"
395796c8dcSSimon Schubert #include "dfp.h"
405796c8dcSSimon Schubert #include "user-regs.h"
41*c50c785cSJohn Marino #include "tracepoint.h"
425796c8dcSSimon Schubert #include <errno.h>
435796c8dcSSimon Schubert #include "gdb_string.h"
445796c8dcSSimon Schubert #include "gdb_assert.h"
455796c8dcSSimon Schubert #include "cp-support.h"
465796c8dcSSimon Schubert #include "observer.h"
475796c8dcSSimon Schubert #include "objfiles.h"
485796c8dcSSimon Schubert #include "symtab.h"
49*c50c785cSJohn Marino #include "exceptions.h"
505796c8dcSSimon Schubert 
515796c8dcSSimon Schubert extern int overload_debug;
525796c8dcSSimon Schubert /* Local functions.  */
535796c8dcSSimon Schubert 
545796c8dcSSimon Schubert static int typecmp (int staticp, int varargs, int nargs,
555796c8dcSSimon Schubert 		    struct field t1[], struct value *t2[]);
565796c8dcSSimon Schubert 
57cf7f2e2dSJohn Marino static struct value *search_struct_field (const char *, struct value *,
585796c8dcSSimon Schubert 					  int, struct type *, int);
595796c8dcSSimon Schubert 
60cf7f2e2dSJohn Marino static struct value *search_struct_method (const char *, struct value **,
615796c8dcSSimon Schubert 					   struct value **,
625796c8dcSSimon Schubert 					   int, int *, struct type *);
635796c8dcSSimon Schubert 
645796c8dcSSimon Schubert static int find_oload_champ_namespace (struct type **, int,
655796c8dcSSimon Schubert 				       const char *, const char *,
665796c8dcSSimon Schubert 				       struct symbol ***,
67cf7f2e2dSJohn Marino 				       struct badness_vector **,
68cf7f2e2dSJohn Marino 				       const int no_adl);
695796c8dcSSimon Schubert 
705796c8dcSSimon Schubert static
715796c8dcSSimon Schubert int find_oload_champ_namespace_loop (struct type **, int,
725796c8dcSSimon Schubert 				     const char *, const char *,
735796c8dcSSimon Schubert 				     int, struct symbol ***,
74cf7f2e2dSJohn Marino 				     struct badness_vector **, int *,
75cf7f2e2dSJohn Marino 				     const int no_adl);
765796c8dcSSimon Schubert 
775796c8dcSSimon Schubert static int find_oload_champ (struct type **, int, int, int,
785796c8dcSSimon Schubert 			     struct fn_field *, struct symbol **,
795796c8dcSSimon Schubert 			     struct badness_vector **);
805796c8dcSSimon Schubert 
815796c8dcSSimon Schubert static int oload_method_static (int, struct fn_field *, int);
825796c8dcSSimon Schubert 
835796c8dcSSimon Schubert enum oload_classification { STANDARD, NON_STANDARD, INCOMPATIBLE };
845796c8dcSSimon Schubert 
855796c8dcSSimon Schubert static enum
865796c8dcSSimon Schubert oload_classification classify_oload_match (struct badness_vector *,
875796c8dcSSimon Schubert 					   int, int);
885796c8dcSSimon Schubert 
895796c8dcSSimon Schubert static struct value *value_struct_elt_for_reference (struct type *,
905796c8dcSSimon Schubert 						     int, struct type *,
915796c8dcSSimon Schubert 						     char *,
925796c8dcSSimon Schubert 						     struct type *,
935796c8dcSSimon Schubert 						     int, enum noside);
945796c8dcSSimon Schubert 
955796c8dcSSimon Schubert static struct value *value_namespace_elt (const struct type *,
965796c8dcSSimon Schubert 					  char *, int , enum noside);
975796c8dcSSimon Schubert 
985796c8dcSSimon Schubert static struct value *value_maybe_namespace_elt (const struct type *,
995796c8dcSSimon Schubert 						char *, int,
1005796c8dcSSimon Schubert 						enum noside);
1015796c8dcSSimon Schubert 
1025796c8dcSSimon Schubert static CORE_ADDR allocate_space_in_inferior (int);
1035796c8dcSSimon Schubert 
1045796c8dcSSimon Schubert static struct value *cast_into_complex (struct type *, struct value *);
1055796c8dcSSimon Schubert 
106cf7f2e2dSJohn Marino static struct fn_field *find_method_list (struct value **, const char *,
1075796c8dcSSimon Schubert 					  int, struct type *, int *,
1085796c8dcSSimon Schubert 					  struct type **, int *);
1095796c8dcSSimon Schubert 
1105796c8dcSSimon Schubert void _initialize_valops (void);
1115796c8dcSSimon Schubert 
1125796c8dcSSimon Schubert #if 0
1135796c8dcSSimon Schubert /* Flag for whether we want to abandon failed expression evals by
1145796c8dcSSimon Schubert    default.  */
1155796c8dcSSimon Schubert 
1165796c8dcSSimon Schubert static int auto_abandon = 0;
1175796c8dcSSimon Schubert #endif
1185796c8dcSSimon Schubert 
1195796c8dcSSimon Schubert int overload_resolution = 0;
1205796c8dcSSimon Schubert static void
1215796c8dcSSimon Schubert show_overload_resolution (struct ui_file *file, int from_tty,
1225796c8dcSSimon Schubert 			  struct cmd_list_element *c,
1235796c8dcSSimon Schubert 			  const char *value)
1245796c8dcSSimon Schubert {
125*c50c785cSJohn Marino   fprintf_filtered (file, _("Overload resolution in evaluating "
126*c50c785cSJohn Marino 			    "C++ functions is %s.\n"),
1275796c8dcSSimon Schubert 		    value);
1285796c8dcSSimon Schubert }
1295796c8dcSSimon Schubert 
1305796c8dcSSimon Schubert /* Find the address of function name NAME in the inferior.  If OBJF_P
1315796c8dcSSimon Schubert    is non-NULL, *OBJF_P will be set to the OBJFILE where the function
1325796c8dcSSimon Schubert    is defined.  */
1335796c8dcSSimon Schubert 
1345796c8dcSSimon Schubert struct value *
1355796c8dcSSimon Schubert find_function_in_inferior (const char *name, struct objfile **objf_p)
1365796c8dcSSimon Schubert {
1375796c8dcSSimon Schubert   struct symbol *sym;
138cf7f2e2dSJohn Marino 
1395796c8dcSSimon Schubert   sym = lookup_symbol (name, 0, VAR_DOMAIN, 0);
1405796c8dcSSimon Schubert   if (sym != NULL)
1415796c8dcSSimon Schubert     {
1425796c8dcSSimon Schubert       if (SYMBOL_CLASS (sym) != LOC_BLOCK)
1435796c8dcSSimon Schubert 	{
1445796c8dcSSimon Schubert 	  error (_("\"%s\" exists in this program but is not a function."),
1455796c8dcSSimon Schubert 		 name);
1465796c8dcSSimon Schubert 	}
1475796c8dcSSimon Schubert 
1485796c8dcSSimon Schubert       if (objf_p)
1495796c8dcSSimon Schubert 	*objf_p = SYMBOL_SYMTAB (sym)->objfile;
1505796c8dcSSimon Schubert 
1515796c8dcSSimon Schubert       return value_of_variable (sym, NULL);
1525796c8dcSSimon Schubert     }
1535796c8dcSSimon Schubert   else
1545796c8dcSSimon Schubert     {
1555796c8dcSSimon Schubert       struct minimal_symbol *msymbol =
1565796c8dcSSimon Schubert 	lookup_minimal_symbol (name, NULL, NULL);
157cf7f2e2dSJohn Marino 
1585796c8dcSSimon Schubert       if (msymbol != NULL)
1595796c8dcSSimon Schubert 	{
1605796c8dcSSimon Schubert 	  struct objfile *objfile = msymbol_objfile (msymbol);
1615796c8dcSSimon Schubert 	  struct gdbarch *gdbarch = get_objfile_arch (objfile);
1625796c8dcSSimon Schubert 
1635796c8dcSSimon Schubert 	  struct type *type;
1645796c8dcSSimon Schubert 	  CORE_ADDR maddr;
1655796c8dcSSimon Schubert 	  type = lookup_pointer_type (builtin_type (gdbarch)->builtin_char);
1665796c8dcSSimon Schubert 	  type = lookup_function_type (type);
1675796c8dcSSimon Schubert 	  type = lookup_pointer_type (type);
1685796c8dcSSimon Schubert 	  maddr = SYMBOL_VALUE_ADDRESS (msymbol);
1695796c8dcSSimon Schubert 
1705796c8dcSSimon Schubert 	  if (objf_p)
1715796c8dcSSimon Schubert 	    *objf_p = objfile;
1725796c8dcSSimon Schubert 
1735796c8dcSSimon Schubert 	  return value_from_pointer (type, maddr);
1745796c8dcSSimon Schubert 	}
1755796c8dcSSimon Schubert       else
1765796c8dcSSimon Schubert 	{
1775796c8dcSSimon Schubert 	  if (!target_has_execution)
178*c50c785cSJohn Marino 	    error (_("evaluation of this expression "
179*c50c785cSJohn Marino 		     "requires the target program to be active"));
1805796c8dcSSimon Schubert 	  else
181*c50c785cSJohn Marino 	    error (_("evaluation of this expression requires the "
182*c50c785cSJohn Marino 		     "program to have a function \"%s\"."),
183*c50c785cSJohn Marino 		   name);
1845796c8dcSSimon Schubert 	}
1855796c8dcSSimon Schubert     }
1865796c8dcSSimon Schubert }
1875796c8dcSSimon Schubert 
1885796c8dcSSimon Schubert /* Allocate NBYTES of space in the inferior using the inferior's
1895796c8dcSSimon Schubert    malloc and return a value that is a pointer to the allocated
1905796c8dcSSimon Schubert    space.  */
1915796c8dcSSimon Schubert 
1925796c8dcSSimon Schubert struct value *
1935796c8dcSSimon Schubert value_allocate_space_in_inferior (int len)
1945796c8dcSSimon Schubert {
1955796c8dcSSimon Schubert   struct objfile *objf;
1965796c8dcSSimon Schubert   struct value *val = find_function_in_inferior ("malloc", &objf);
1975796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_objfile_arch (objf);
1985796c8dcSSimon Schubert   struct value *blocklen;
1995796c8dcSSimon Schubert 
2005796c8dcSSimon Schubert   blocklen = value_from_longest (builtin_type (gdbarch)->builtin_int, len);
2015796c8dcSSimon Schubert   val = call_function_by_hand (val, 1, &blocklen);
2025796c8dcSSimon Schubert   if (value_logical_not (val))
2035796c8dcSSimon Schubert     {
2045796c8dcSSimon Schubert       if (!target_has_execution)
205*c50c785cSJohn Marino 	error (_("No memory available to program now: "
206*c50c785cSJohn Marino 		 "you need to start the target first"));
2075796c8dcSSimon Schubert       else
2085796c8dcSSimon Schubert 	error (_("No memory available to program: call to malloc failed"));
2095796c8dcSSimon Schubert     }
2105796c8dcSSimon Schubert   return val;
2115796c8dcSSimon Schubert }
2125796c8dcSSimon Schubert 
2135796c8dcSSimon Schubert static CORE_ADDR
2145796c8dcSSimon Schubert allocate_space_in_inferior (int len)
2155796c8dcSSimon Schubert {
2165796c8dcSSimon Schubert   return value_as_long (value_allocate_space_in_inferior (len));
2175796c8dcSSimon Schubert }
2185796c8dcSSimon Schubert 
2195796c8dcSSimon Schubert /* Cast struct value VAL to type TYPE and return as a value.
2205796c8dcSSimon Schubert    Both type and val must be of TYPE_CODE_STRUCT or TYPE_CODE_UNION
2215796c8dcSSimon Schubert    for this to work.  Typedef to one of the codes is permitted.
2225796c8dcSSimon Schubert    Returns NULL if the cast is neither an upcast nor a downcast.  */
2235796c8dcSSimon Schubert 
2245796c8dcSSimon Schubert static struct value *
2255796c8dcSSimon Schubert value_cast_structs (struct type *type, struct value *v2)
2265796c8dcSSimon Schubert {
2275796c8dcSSimon Schubert   struct type *t1;
2285796c8dcSSimon Schubert   struct type *t2;
2295796c8dcSSimon Schubert   struct value *v;
2305796c8dcSSimon Schubert 
2315796c8dcSSimon Schubert   gdb_assert (type != NULL && v2 != NULL);
2325796c8dcSSimon Schubert 
2335796c8dcSSimon Schubert   t1 = check_typedef (type);
2345796c8dcSSimon Schubert   t2 = check_typedef (value_type (v2));
2355796c8dcSSimon Schubert 
2365796c8dcSSimon Schubert   /* Check preconditions.  */
2375796c8dcSSimon Schubert   gdb_assert ((TYPE_CODE (t1) == TYPE_CODE_STRUCT
2385796c8dcSSimon Schubert 	       || TYPE_CODE (t1) == TYPE_CODE_UNION)
2395796c8dcSSimon Schubert 	      && !!"Precondition is that type is of STRUCT or UNION kind.");
2405796c8dcSSimon Schubert   gdb_assert ((TYPE_CODE (t2) == TYPE_CODE_STRUCT
2415796c8dcSSimon Schubert 	       || TYPE_CODE (t2) == TYPE_CODE_UNION)
2425796c8dcSSimon Schubert 	      && !!"Precondition is that value is of STRUCT or UNION kind");
2435796c8dcSSimon Schubert 
244cf7f2e2dSJohn Marino   if (TYPE_NAME (t1) != NULL
245cf7f2e2dSJohn Marino       && TYPE_NAME (t2) != NULL
246cf7f2e2dSJohn Marino       && !strcmp (TYPE_NAME (t1), TYPE_NAME (t2)))
247cf7f2e2dSJohn Marino     return NULL;
248cf7f2e2dSJohn Marino 
2495796c8dcSSimon Schubert   /* Upcasting: look in the type of the source to see if it contains the
2505796c8dcSSimon Schubert      type of the target as a superclass.  If so, we'll need to
2515796c8dcSSimon Schubert      offset the pointer rather than just change its type.  */
2525796c8dcSSimon Schubert   if (TYPE_NAME (t1) != NULL)
2535796c8dcSSimon Schubert     {
2545796c8dcSSimon Schubert       v = search_struct_field (type_name_no_tag (t1),
2555796c8dcSSimon Schubert 			       v2, 0, t2, 1);
2565796c8dcSSimon Schubert       if (v)
2575796c8dcSSimon Schubert 	return v;
2585796c8dcSSimon Schubert     }
2595796c8dcSSimon Schubert 
2605796c8dcSSimon Schubert   /* Downcasting: look in the type of the target to see if it contains the
2615796c8dcSSimon Schubert      type of the source as a superclass.  If so, we'll need to
262cf7f2e2dSJohn Marino      offset the pointer rather than just change its type.  */
2635796c8dcSSimon Schubert   if (TYPE_NAME (t2) != NULL)
2645796c8dcSSimon Schubert     {
265cf7f2e2dSJohn Marino       /* Try downcasting using the run-time type of the value.  */
266cf7f2e2dSJohn Marino       int full, top, using_enc;
267cf7f2e2dSJohn Marino       struct type *real_type;
268cf7f2e2dSJohn Marino 
269cf7f2e2dSJohn Marino       real_type = value_rtti_type (v2, &full, &top, &using_enc);
270cf7f2e2dSJohn Marino       if (real_type)
271cf7f2e2dSJohn Marino 	{
272cf7f2e2dSJohn Marino 	  v = value_full_object (v2, real_type, full, top, using_enc);
273cf7f2e2dSJohn Marino 	  v = value_at_lazy (real_type, value_address (v));
274cf7f2e2dSJohn Marino 
275cf7f2e2dSJohn Marino 	  /* We might be trying to cast to the outermost enclosing
276cf7f2e2dSJohn Marino 	     type, in which case search_struct_field won't work.  */
277cf7f2e2dSJohn Marino 	  if (TYPE_NAME (real_type) != NULL
278cf7f2e2dSJohn Marino 	      && !strcmp (TYPE_NAME (real_type), TYPE_NAME (t1)))
279cf7f2e2dSJohn Marino 	    return v;
280cf7f2e2dSJohn Marino 
281cf7f2e2dSJohn Marino 	  v = search_struct_field (type_name_no_tag (t2), v, 0, real_type, 1);
282cf7f2e2dSJohn Marino 	  if (v)
283cf7f2e2dSJohn Marino 	    return v;
284cf7f2e2dSJohn Marino 	}
285cf7f2e2dSJohn Marino 
286cf7f2e2dSJohn Marino       /* Try downcasting using information from the destination type
287cf7f2e2dSJohn Marino 	 T2.  This wouldn't work properly for classes with virtual
288cf7f2e2dSJohn Marino 	 bases, but those were handled above.  */
2895796c8dcSSimon Schubert       v = search_struct_field (type_name_no_tag (t2),
2905796c8dcSSimon Schubert 			       value_zero (t1, not_lval), 0, t1, 1);
2915796c8dcSSimon Schubert       if (v)
2925796c8dcSSimon Schubert 	{
2935796c8dcSSimon Schubert 	  /* Downcasting is possible (t1 is superclass of v2).  */
2945796c8dcSSimon Schubert 	  CORE_ADDR addr2 = value_address (v2);
295cf7f2e2dSJohn Marino 
2965796c8dcSSimon Schubert 	  addr2 -= value_address (v) + value_embedded_offset (v);
2975796c8dcSSimon Schubert 	  return value_at (type, addr2);
2985796c8dcSSimon Schubert 	}
2995796c8dcSSimon Schubert     }
3005796c8dcSSimon Schubert 
3015796c8dcSSimon Schubert   return NULL;
3025796c8dcSSimon Schubert }
3035796c8dcSSimon Schubert 
3045796c8dcSSimon Schubert /* Cast one pointer or reference type to another.  Both TYPE and
3055796c8dcSSimon Schubert    the type of ARG2 should be pointer types, or else both should be
3065796c8dcSSimon Schubert    reference types.  Returns the new pointer or reference.  */
3075796c8dcSSimon Schubert 
3085796c8dcSSimon Schubert struct value *
3095796c8dcSSimon Schubert value_cast_pointers (struct type *type, struct value *arg2)
3105796c8dcSSimon Schubert {
3115796c8dcSSimon Schubert   struct type *type1 = check_typedef (type);
3125796c8dcSSimon Schubert   struct type *type2 = check_typedef (value_type (arg2));
313cf7f2e2dSJohn Marino   struct type *t1 = check_typedef (TYPE_TARGET_TYPE (type1));
3145796c8dcSSimon Schubert   struct type *t2 = check_typedef (TYPE_TARGET_TYPE (type2));
3155796c8dcSSimon Schubert 
3165796c8dcSSimon Schubert   if (TYPE_CODE (t1) == TYPE_CODE_STRUCT
3175796c8dcSSimon Schubert       && TYPE_CODE (t2) == TYPE_CODE_STRUCT
3185796c8dcSSimon Schubert       && !value_logical_not (arg2))
3195796c8dcSSimon Schubert     {
3205796c8dcSSimon Schubert       struct value *v2;
3215796c8dcSSimon Schubert 
3225796c8dcSSimon Schubert       if (TYPE_CODE (type2) == TYPE_CODE_REF)
3235796c8dcSSimon Schubert 	v2 = coerce_ref (arg2);
3245796c8dcSSimon Schubert       else
3255796c8dcSSimon Schubert 	v2 = value_ind (arg2);
326*c50c785cSJohn Marino       gdb_assert (TYPE_CODE (check_typedef (value_type (v2)))
327*c50c785cSJohn Marino 		  == TYPE_CODE_STRUCT && !!"Why did coercion fail?");
3285796c8dcSSimon Schubert       v2 = value_cast_structs (t1, v2);
3295796c8dcSSimon Schubert       /* At this point we have what we can have, un-dereference if needed.  */
3305796c8dcSSimon Schubert       if (v2)
3315796c8dcSSimon Schubert 	{
3325796c8dcSSimon Schubert 	  struct value *v = value_addr (v2);
333cf7f2e2dSJohn Marino 
3345796c8dcSSimon Schubert 	  deprecated_set_value_type (v, type);
3355796c8dcSSimon Schubert 	  return v;
3365796c8dcSSimon Schubert 	}
3375796c8dcSSimon Schubert    }
3385796c8dcSSimon Schubert 
3395796c8dcSSimon Schubert   /* No superclass found, just change the pointer type.  */
3405796c8dcSSimon Schubert   arg2 = value_copy (arg2);
3415796c8dcSSimon Schubert   deprecated_set_value_type (arg2, type);
342*c50c785cSJohn Marino   set_value_enclosing_type (arg2, type);
3435796c8dcSSimon Schubert   set_value_pointed_to_offset (arg2, 0);	/* pai: chk_val */
3445796c8dcSSimon Schubert   return arg2;
3455796c8dcSSimon Schubert }
3465796c8dcSSimon Schubert 
3475796c8dcSSimon Schubert /* Cast value ARG2 to type TYPE and return as a value.
3485796c8dcSSimon Schubert    More general than a C cast: accepts any two types of the same length,
3495796c8dcSSimon Schubert    and if ARG2 is an lvalue it can be cast into anything at all.  */
3505796c8dcSSimon Schubert /* In C++, casts may change pointer or object representations.  */
3515796c8dcSSimon Schubert 
3525796c8dcSSimon Schubert struct value *
3535796c8dcSSimon Schubert value_cast (struct type *type, struct value *arg2)
3545796c8dcSSimon Schubert {
3555796c8dcSSimon Schubert   enum type_code code1;
3565796c8dcSSimon Schubert   enum type_code code2;
3575796c8dcSSimon Schubert   int scalar;
3585796c8dcSSimon Schubert   struct type *type2;
3595796c8dcSSimon Schubert 
3605796c8dcSSimon Schubert   int convert_to_boolean = 0;
3615796c8dcSSimon Schubert 
3625796c8dcSSimon Schubert   if (value_type (arg2) == type)
3635796c8dcSSimon Schubert     return arg2;
3645796c8dcSSimon Schubert 
3655796c8dcSSimon Schubert   code1 = TYPE_CODE (check_typedef (type));
3665796c8dcSSimon Schubert 
3675796c8dcSSimon Schubert   /* Check if we are casting struct reference to struct reference.  */
3685796c8dcSSimon Schubert   if (code1 == TYPE_CODE_REF)
3695796c8dcSSimon Schubert     {
3705796c8dcSSimon Schubert       /* We dereference type; then we recurse and finally
3715796c8dcSSimon Schubert          we generate value of the given reference.  Nothing wrong with
3725796c8dcSSimon Schubert 	 that.  */
3735796c8dcSSimon Schubert       struct type *t1 = check_typedef (type);
3745796c8dcSSimon Schubert       struct type *dereftype = check_typedef (TYPE_TARGET_TYPE (t1));
3755796c8dcSSimon Schubert       struct value *val =  value_cast (dereftype, arg2);
376cf7f2e2dSJohn Marino 
3775796c8dcSSimon Schubert       return value_ref (val);
3785796c8dcSSimon Schubert     }
3795796c8dcSSimon Schubert 
3805796c8dcSSimon Schubert   code2 = TYPE_CODE (check_typedef (value_type (arg2)));
3815796c8dcSSimon Schubert 
3825796c8dcSSimon Schubert   if (code2 == TYPE_CODE_REF)
3835796c8dcSSimon Schubert     /* We deref the value and then do the cast.  */
3845796c8dcSSimon Schubert     return value_cast (type, coerce_ref (arg2));
3855796c8dcSSimon Schubert 
3865796c8dcSSimon Schubert   CHECK_TYPEDEF (type);
3875796c8dcSSimon Schubert   code1 = TYPE_CODE (type);
3885796c8dcSSimon Schubert   arg2 = coerce_ref (arg2);
3895796c8dcSSimon Schubert   type2 = check_typedef (value_type (arg2));
3905796c8dcSSimon Schubert 
3915796c8dcSSimon Schubert   /* You can't cast to a reference type.  See value_cast_pointers
3925796c8dcSSimon Schubert      instead.  */
3935796c8dcSSimon Schubert   gdb_assert (code1 != TYPE_CODE_REF);
3945796c8dcSSimon Schubert 
3955796c8dcSSimon Schubert   /* A cast to an undetermined-length array_type, such as
3965796c8dcSSimon Schubert      (TYPE [])OBJECT, is treated like a cast to (TYPE [N])OBJECT,
3975796c8dcSSimon Schubert      where N is sizeof(OBJECT)/sizeof(TYPE).  */
3985796c8dcSSimon Schubert   if (code1 == TYPE_CODE_ARRAY)
3995796c8dcSSimon Schubert     {
4005796c8dcSSimon Schubert       struct type *element_type = TYPE_TARGET_TYPE (type);
4015796c8dcSSimon Schubert       unsigned element_length = TYPE_LENGTH (check_typedef (element_type));
402cf7f2e2dSJohn Marino 
4035796c8dcSSimon Schubert       if (element_length > 0 && TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
4045796c8dcSSimon Schubert 	{
4055796c8dcSSimon Schubert 	  struct type *range_type = TYPE_INDEX_TYPE (type);
4065796c8dcSSimon Schubert 	  int val_length = TYPE_LENGTH (type2);
4075796c8dcSSimon Schubert 	  LONGEST low_bound, high_bound, new_length;
408cf7f2e2dSJohn Marino 
4095796c8dcSSimon Schubert 	  if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
4105796c8dcSSimon Schubert 	    low_bound = 0, high_bound = 0;
4115796c8dcSSimon Schubert 	  new_length = val_length / element_length;
4125796c8dcSSimon Schubert 	  if (val_length % element_length != 0)
413*c50c785cSJohn Marino 	    warning (_("array element type size does not "
414*c50c785cSJohn Marino 		       "divide object size in cast"));
4155796c8dcSSimon Schubert 	  /* FIXME-type-allocation: need a way to free this type when
4165796c8dcSSimon Schubert 	     we are done with it.  */
4175796c8dcSSimon Schubert 	  range_type = create_range_type ((struct type *) NULL,
4185796c8dcSSimon Schubert 					  TYPE_TARGET_TYPE (range_type),
4195796c8dcSSimon Schubert 					  low_bound,
4205796c8dcSSimon Schubert 					  new_length + low_bound - 1);
4215796c8dcSSimon Schubert 	  deprecated_set_value_type (arg2,
4225796c8dcSSimon Schubert 				     create_array_type ((struct type *) NULL,
4235796c8dcSSimon Schubert 							element_type,
4245796c8dcSSimon Schubert 							range_type));
4255796c8dcSSimon Schubert 	  return arg2;
4265796c8dcSSimon Schubert 	}
4275796c8dcSSimon Schubert     }
4285796c8dcSSimon Schubert 
4295796c8dcSSimon Schubert   if (current_language->c_style_arrays
430*c50c785cSJohn Marino       && TYPE_CODE (type2) == TYPE_CODE_ARRAY
431*c50c785cSJohn Marino       && !TYPE_VECTOR (type2))
4325796c8dcSSimon Schubert     arg2 = value_coerce_array (arg2);
4335796c8dcSSimon Schubert 
4345796c8dcSSimon Schubert   if (TYPE_CODE (type2) == TYPE_CODE_FUNC)
4355796c8dcSSimon Schubert     arg2 = value_coerce_function (arg2);
4365796c8dcSSimon Schubert 
4375796c8dcSSimon Schubert   type2 = check_typedef (value_type (arg2));
4385796c8dcSSimon Schubert   code2 = TYPE_CODE (type2);
4395796c8dcSSimon Schubert 
4405796c8dcSSimon Schubert   if (code1 == TYPE_CODE_COMPLEX)
4415796c8dcSSimon Schubert     return cast_into_complex (type, arg2);
4425796c8dcSSimon Schubert   if (code1 == TYPE_CODE_BOOL)
4435796c8dcSSimon Schubert     {
4445796c8dcSSimon Schubert       code1 = TYPE_CODE_INT;
4455796c8dcSSimon Schubert       convert_to_boolean = 1;
4465796c8dcSSimon Schubert     }
4475796c8dcSSimon Schubert   if (code1 == TYPE_CODE_CHAR)
4485796c8dcSSimon Schubert     code1 = TYPE_CODE_INT;
4495796c8dcSSimon Schubert   if (code2 == TYPE_CODE_BOOL || code2 == TYPE_CODE_CHAR)
4505796c8dcSSimon Schubert     code2 = TYPE_CODE_INT;
4515796c8dcSSimon Schubert 
4525796c8dcSSimon Schubert   scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
4535796c8dcSSimon Schubert 	    || code2 == TYPE_CODE_DECFLOAT || code2 == TYPE_CODE_ENUM
4545796c8dcSSimon Schubert 	    || code2 == TYPE_CODE_RANGE);
4555796c8dcSSimon Schubert 
4565796c8dcSSimon Schubert   if ((code1 == TYPE_CODE_STRUCT || code1 == TYPE_CODE_UNION)
4575796c8dcSSimon Schubert       && (code2 == TYPE_CODE_STRUCT || code2 == TYPE_CODE_UNION)
4585796c8dcSSimon Schubert       && TYPE_NAME (type) != 0)
4595796c8dcSSimon Schubert     {
4605796c8dcSSimon Schubert       struct value *v = value_cast_structs (type, arg2);
461cf7f2e2dSJohn Marino 
4625796c8dcSSimon Schubert       if (v)
4635796c8dcSSimon Schubert 	return v;
4645796c8dcSSimon Schubert     }
4655796c8dcSSimon Schubert 
4665796c8dcSSimon Schubert   if (code1 == TYPE_CODE_FLT && scalar)
4675796c8dcSSimon Schubert     return value_from_double (type, value_as_double (arg2));
4685796c8dcSSimon Schubert   else if (code1 == TYPE_CODE_DECFLOAT && scalar)
4695796c8dcSSimon Schubert     {
4705796c8dcSSimon Schubert       enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
4715796c8dcSSimon Schubert       int dec_len = TYPE_LENGTH (type);
4725796c8dcSSimon Schubert       gdb_byte dec[16];
4735796c8dcSSimon Schubert 
4745796c8dcSSimon Schubert       if (code2 == TYPE_CODE_FLT)
4755796c8dcSSimon Schubert 	decimal_from_floating (arg2, dec, dec_len, byte_order);
4765796c8dcSSimon Schubert       else if (code2 == TYPE_CODE_DECFLOAT)
4775796c8dcSSimon Schubert 	decimal_convert (value_contents (arg2), TYPE_LENGTH (type2),
4785796c8dcSSimon Schubert 			 byte_order, dec, dec_len, byte_order);
4795796c8dcSSimon Schubert       else
4805796c8dcSSimon Schubert 	/* The only option left is an integral type.  */
4815796c8dcSSimon Schubert 	decimal_from_integral (arg2, dec, dec_len, byte_order);
4825796c8dcSSimon Schubert 
4835796c8dcSSimon Schubert       return value_from_decfloat (type, dec);
4845796c8dcSSimon Schubert     }
4855796c8dcSSimon Schubert   else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM
4865796c8dcSSimon Schubert 	    || code1 == TYPE_CODE_RANGE)
4875796c8dcSSimon Schubert 	   && (scalar || code2 == TYPE_CODE_PTR
4885796c8dcSSimon Schubert 	       || code2 == TYPE_CODE_MEMBERPTR))
4895796c8dcSSimon Schubert     {
4905796c8dcSSimon Schubert       LONGEST longest;
4915796c8dcSSimon Schubert 
4925796c8dcSSimon Schubert       /* When we cast pointers to integers, we mustn't use
4935796c8dcSSimon Schubert          gdbarch_pointer_to_address to find the address the pointer
4945796c8dcSSimon Schubert          represents, as value_as_long would.  GDB should evaluate
4955796c8dcSSimon Schubert          expressions just as the compiler would --- and the compiler
4965796c8dcSSimon Schubert          sees a cast as a simple reinterpretation of the pointer's
4975796c8dcSSimon Schubert          bits.  */
4985796c8dcSSimon Schubert       if (code2 == TYPE_CODE_PTR)
4995796c8dcSSimon Schubert         longest = extract_unsigned_integer
5005796c8dcSSimon Schubert 		    (value_contents (arg2), TYPE_LENGTH (type2),
5015796c8dcSSimon Schubert 		     gdbarch_byte_order (get_type_arch (type2)));
5025796c8dcSSimon Schubert       else
5035796c8dcSSimon Schubert         longest = value_as_long (arg2);
5045796c8dcSSimon Schubert       return value_from_longest (type, convert_to_boolean ?
5055796c8dcSSimon Schubert 				 (LONGEST) (longest ? 1 : 0) : longest);
5065796c8dcSSimon Schubert     }
5075796c8dcSSimon Schubert   else if (code1 == TYPE_CODE_PTR && (code2 == TYPE_CODE_INT
5085796c8dcSSimon Schubert 				      || code2 == TYPE_CODE_ENUM
5095796c8dcSSimon Schubert 				      || code2 == TYPE_CODE_RANGE))
5105796c8dcSSimon Schubert     {
5115796c8dcSSimon Schubert       /* TYPE_LENGTH (type) is the length of a pointer, but we really
5125796c8dcSSimon Schubert 	 want the length of an address! -- we are really dealing with
5135796c8dcSSimon Schubert 	 addresses (i.e., gdb representations) not pointers (i.e.,
5145796c8dcSSimon Schubert 	 target representations) here.
5155796c8dcSSimon Schubert 
5165796c8dcSSimon Schubert 	 This allows things like "print *(int *)0x01000234" to work
5175796c8dcSSimon Schubert 	 without printing a misleading message -- which would
5185796c8dcSSimon Schubert 	 otherwise occur when dealing with a target having two byte
5195796c8dcSSimon Schubert 	 pointers and four byte addresses.  */
5205796c8dcSSimon Schubert 
5215796c8dcSSimon Schubert       int addr_bit = gdbarch_addr_bit (get_type_arch (type2));
5225796c8dcSSimon Schubert       LONGEST longest = value_as_long (arg2);
523cf7f2e2dSJohn Marino 
5245796c8dcSSimon Schubert       if (addr_bit < sizeof (LONGEST) * HOST_CHAR_BIT)
5255796c8dcSSimon Schubert 	{
5265796c8dcSSimon Schubert 	  if (longest >= ((LONGEST) 1 << addr_bit)
5275796c8dcSSimon Schubert 	      || longest <= -((LONGEST) 1 << addr_bit))
5285796c8dcSSimon Schubert 	    warning (_("value truncated"));
5295796c8dcSSimon Schubert 	}
5305796c8dcSSimon Schubert       return value_from_longest (type, longest);
5315796c8dcSSimon Schubert     }
5325796c8dcSSimon Schubert   else if (code1 == TYPE_CODE_METHODPTR && code2 == TYPE_CODE_INT
5335796c8dcSSimon Schubert 	   && value_as_long (arg2) == 0)
5345796c8dcSSimon Schubert     {
5355796c8dcSSimon Schubert       struct value *result = allocate_value (type);
536cf7f2e2dSJohn Marino 
5375796c8dcSSimon Schubert       cplus_make_method_ptr (type, value_contents_writeable (result), 0, 0);
5385796c8dcSSimon Schubert       return result;
5395796c8dcSSimon Schubert     }
5405796c8dcSSimon Schubert   else if (code1 == TYPE_CODE_MEMBERPTR && code2 == TYPE_CODE_INT
5415796c8dcSSimon Schubert 	   && value_as_long (arg2) == 0)
5425796c8dcSSimon Schubert     {
5435796c8dcSSimon Schubert       /* The Itanium C++ ABI represents NULL pointers to members as
5445796c8dcSSimon Schubert 	 minus one, instead of biasing the normal case.  */
5455796c8dcSSimon Schubert       return value_from_longest (type, -1);
5465796c8dcSSimon Schubert     }
547*c50c785cSJohn Marino   else if (code1 == TYPE_CODE_ARRAY && TYPE_VECTOR (type) && scalar)
548*c50c785cSJohn Marino     {
549*c50c785cSJohn Marino       /* Widen the scalar to a vector.  */
550*c50c785cSJohn Marino       struct type *eltype;
551*c50c785cSJohn Marino       struct value *val;
552*c50c785cSJohn Marino       LONGEST low_bound, high_bound;
553*c50c785cSJohn Marino       int i;
554*c50c785cSJohn Marino 
555*c50c785cSJohn Marino       if (!get_array_bounds (type, &low_bound, &high_bound))
556*c50c785cSJohn Marino 	error (_("Could not determine the vector bounds"));
557*c50c785cSJohn Marino 
558*c50c785cSJohn Marino       eltype = check_typedef (TYPE_TARGET_TYPE (type));
559*c50c785cSJohn Marino       arg2 = value_cast (eltype, arg2);
560*c50c785cSJohn Marino       val = allocate_value (type);
561*c50c785cSJohn Marino 
562*c50c785cSJohn Marino       for (i = 0; i < high_bound - low_bound + 1; i++)
563*c50c785cSJohn Marino 	{
564*c50c785cSJohn Marino 	  /* Duplicate the contents of arg2 into the destination vector.  */
565*c50c785cSJohn Marino 	  memcpy (value_contents_writeable (val) + (i * TYPE_LENGTH (eltype)),
566*c50c785cSJohn Marino 		  value_contents_all (arg2), TYPE_LENGTH (eltype));
567*c50c785cSJohn Marino 	}
568*c50c785cSJohn Marino       return val;
569*c50c785cSJohn Marino     }
5705796c8dcSSimon Schubert   else if (TYPE_LENGTH (type) == TYPE_LENGTH (type2))
5715796c8dcSSimon Schubert     {
5725796c8dcSSimon Schubert       if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
5735796c8dcSSimon Schubert 	return value_cast_pointers (type, arg2);
5745796c8dcSSimon Schubert 
5755796c8dcSSimon Schubert       arg2 = value_copy (arg2);
5765796c8dcSSimon Schubert       deprecated_set_value_type (arg2, type);
577*c50c785cSJohn Marino       set_value_enclosing_type (arg2, type);
5785796c8dcSSimon Schubert       set_value_pointed_to_offset (arg2, 0);	/* pai: chk_val */
5795796c8dcSSimon Schubert       return arg2;
5805796c8dcSSimon Schubert     }
5815796c8dcSSimon Schubert   else if (VALUE_LVAL (arg2) == lval_memory)
5825796c8dcSSimon Schubert     return value_at_lazy (type, value_address (arg2));
5835796c8dcSSimon Schubert   else if (code1 == TYPE_CODE_VOID)
5845796c8dcSSimon Schubert     {
5855796c8dcSSimon Schubert       return value_zero (type, not_lval);
5865796c8dcSSimon Schubert     }
5875796c8dcSSimon Schubert   else
5885796c8dcSSimon Schubert     {
5895796c8dcSSimon Schubert       error (_("Invalid cast."));
5905796c8dcSSimon Schubert       return 0;
5915796c8dcSSimon Schubert     }
5925796c8dcSSimon Schubert }
5935796c8dcSSimon Schubert 
594cf7f2e2dSJohn Marino /* The C++ reinterpret_cast operator.  */
595cf7f2e2dSJohn Marino 
596cf7f2e2dSJohn Marino struct value *
597cf7f2e2dSJohn Marino value_reinterpret_cast (struct type *type, struct value *arg)
598cf7f2e2dSJohn Marino {
599cf7f2e2dSJohn Marino   struct value *result;
600cf7f2e2dSJohn Marino   struct type *real_type = check_typedef (type);
601cf7f2e2dSJohn Marino   struct type *arg_type, *dest_type;
602cf7f2e2dSJohn Marino   int is_ref = 0;
603cf7f2e2dSJohn Marino   enum type_code dest_code, arg_code;
604cf7f2e2dSJohn Marino 
605cf7f2e2dSJohn Marino   /* Do reference, function, and array conversion.  */
606cf7f2e2dSJohn Marino   arg = coerce_array (arg);
607cf7f2e2dSJohn Marino 
608cf7f2e2dSJohn Marino   /* Attempt to preserve the type the user asked for.  */
609cf7f2e2dSJohn Marino   dest_type = type;
610cf7f2e2dSJohn Marino 
611cf7f2e2dSJohn Marino   /* If we are casting to a reference type, transform
612cf7f2e2dSJohn Marino      reinterpret_cast<T&>(V) to *reinterpret_cast<T*>(&V).  */
613cf7f2e2dSJohn Marino   if (TYPE_CODE (real_type) == TYPE_CODE_REF)
614cf7f2e2dSJohn Marino     {
615cf7f2e2dSJohn Marino       is_ref = 1;
616cf7f2e2dSJohn Marino       arg = value_addr (arg);
617cf7f2e2dSJohn Marino       dest_type = lookup_pointer_type (TYPE_TARGET_TYPE (dest_type));
618cf7f2e2dSJohn Marino       real_type = lookup_pointer_type (real_type);
619cf7f2e2dSJohn Marino     }
620cf7f2e2dSJohn Marino 
621cf7f2e2dSJohn Marino   arg_type = value_type (arg);
622cf7f2e2dSJohn Marino 
623cf7f2e2dSJohn Marino   dest_code = TYPE_CODE (real_type);
624cf7f2e2dSJohn Marino   arg_code = TYPE_CODE (arg_type);
625cf7f2e2dSJohn Marino 
626cf7f2e2dSJohn Marino   /* We can convert pointer types, or any pointer type to int, or int
627cf7f2e2dSJohn Marino      type to pointer.  */
628cf7f2e2dSJohn Marino   if ((dest_code == TYPE_CODE_PTR && arg_code == TYPE_CODE_INT)
629cf7f2e2dSJohn Marino       || (dest_code == TYPE_CODE_INT && arg_code == TYPE_CODE_PTR)
630cf7f2e2dSJohn Marino       || (dest_code == TYPE_CODE_METHODPTR && arg_code == TYPE_CODE_INT)
631cf7f2e2dSJohn Marino       || (dest_code == TYPE_CODE_INT && arg_code == TYPE_CODE_METHODPTR)
632cf7f2e2dSJohn Marino       || (dest_code == TYPE_CODE_MEMBERPTR && arg_code == TYPE_CODE_INT)
633cf7f2e2dSJohn Marino       || (dest_code == TYPE_CODE_INT && arg_code == TYPE_CODE_MEMBERPTR)
634cf7f2e2dSJohn Marino       || (dest_code == arg_code
635cf7f2e2dSJohn Marino 	  && (dest_code == TYPE_CODE_PTR
636cf7f2e2dSJohn Marino 	      || dest_code == TYPE_CODE_METHODPTR
637cf7f2e2dSJohn Marino 	      || dest_code == TYPE_CODE_MEMBERPTR)))
638cf7f2e2dSJohn Marino     result = value_cast (dest_type, arg);
639cf7f2e2dSJohn Marino   else
640cf7f2e2dSJohn Marino     error (_("Invalid reinterpret_cast"));
641cf7f2e2dSJohn Marino 
642cf7f2e2dSJohn Marino   if (is_ref)
643cf7f2e2dSJohn Marino     result = value_cast (type, value_ref (value_ind (result)));
644cf7f2e2dSJohn Marino 
645cf7f2e2dSJohn Marino   return result;
646cf7f2e2dSJohn Marino }
647cf7f2e2dSJohn Marino 
648cf7f2e2dSJohn Marino /* A helper for value_dynamic_cast.  This implements the first of two
649cf7f2e2dSJohn Marino    runtime checks: we iterate over all the base classes of the value's
650cf7f2e2dSJohn Marino    class which are equal to the desired class; if only one of these
651cf7f2e2dSJohn Marino    holds the value, then it is the answer.  */
652cf7f2e2dSJohn Marino 
653cf7f2e2dSJohn Marino static int
654cf7f2e2dSJohn Marino dynamic_cast_check_1 (struct type *desired_type,
655*c50c785cSJohn Marino 		      const gdb_byte *valaddr,
656*c50c785cSJohn Marino 		      int embedded_offset,
657cf7f2e2dSJohn Marino 		      CORE_ADDR address,
658*c50c785cSJohn Marino 		      struct value *val,
659cf7f2e2dSJohn Marino 		      struct type *search_type,
660cf7f2e2dSJohn Marino 		      CORE_ADDR arg_addr,
661cf7f2e2dSJohn Marino 		      struct type *arg_type,
662cf7f2e2dSJohn Marino 		      struct value **result)
663cf7f2e2dSJohn Marino {
664cf7f2e2dSJohn Marino   int i, result_count = 0;
665cf7f2e2dSJohn Marino 
666cf7f2e2dSJohn Marino   for (i = 0; i < TYPE_N_BASECLASSES (search_type) && result_count < 2; ++i)
667cf7f2e2dSJohn Marino     {
668*c50c785cSJohn Marino       int offset = baseclass_offset (search_type, i, valaddr, embedded_offset,
669*c50c785cSJohn Marino 				     address, val);
670cf7f2e2dSJohn Marino 
671cf7f2e2dSJohn Marino       if (class_types_same_p (desired_type, TYPE_BASECLASS (search_type, i)))
672cf7f2e2dSJohn Marino 	{
673*c50c785cSJohn Marino 	  if (address + embedded_offset + offset >= arg_addr
674*c50c785cSJohn Marino 	      && address + embedded_offset + offset < arg_addr + TYPE_LENGTH (arg_type))
675cf7f2e2dSJohn Marino 	    {
676cf7f2e2dSJohn Marino 	      ++result_count;
677cf7f2e2dSJohn Marino 	      if (!*result)
678cf7f2e2dSJohn Marino 		*result = value_at_lazy (TYPE_BASECLASS (search_type, i),
679*c50c785cSJohn Marino 					 address + embedded_offset + offset);
680cf7f2e2dSJohn Marino 	    }
681cf7f2e2dSJohn Marino 	}
682cf7f2e2dSJohn Marino       else
683cf7f2e2dSJohn Marino 	result_count += dynamic_cast_check_1 (desired_type,
684*c50c785cSJohn Marino 					      valaddr,
685*c50c785cSJohn Marino 					      embedded_offset + offset,
686*c50c785cSJohn Marino 					      address, val,
687cf7f2e2dSJohn Marino 					      TYPE_BASECLASS (search_type, i),
688cf7f2e2dSJohn Marino 					      arg_addr,
689cf7f2e2dSJohn Marino 					      arg_type,
690cf7f2e2dSJohn Marino 					      result);
691cf7f2e2dSJohn Marino     }
692cf7f2e2dSJohn Marino 
693cf7f2e2dSJohn Marino   return result_count;
694cf7f2e2dSJohn Marino }
695cf7f2e2dSJohn Marino 
696cf7f2e2dSJohn Marino /* A helper for value_dynamic_cast.  This implements the second of two
697cf7f2e2dSJohn Marino    runtime checks: we look for a unique public sibling class of the
698cf7f2e2dSJohn Marino    argument's declared class.  */
699cf7f2e2dSJohn Marino 
700cf7f2e2dSJohn Marino static int
701cf7f2e2dSJohn Marino dynamic_cast_check_2 (struct type *desired_type,
702*c50c785cSJohn Marino 		      const gdb_byte *valaddr,
703*c50c785cSJohn Marino 		      int embedded_offset,
704cf7f2e2dSJohn Marino 		      CORE_ADDR address,
705*c50c785cSJohn Marino 		      struct value *val,
706cf7f2e2dSJohn Marino 		      struct type *search_type,
707cf7f2e2dSJohn Marino 		      struct value **result)
708cf7f2e2dSJohn Marino {
709cf7f2e2dSJohn Marino   int i, result_count = 0;
710cf7f2e2dSJohn Marino 
711cf7f2e2dSJohn Marino   for (i = 0; i < TYPE_N_BASECLASSES (search_type) && result_count < 2; ++i)
712cf7f2e2dSJohn Marino     {
713cf7f2e2dSJohn Marino       int offset;
714cf7f2e2dSJohn Marino 
715cf7f2e2dSJohn Marino       if (! BASETYPE_VIA_PUBLIC (search_type, i))
716cf7f2e2dSJohn Marino 	continue;
717cf7f2e2dSJohn Marino 
718*c50c785cSJohn Marino       offset = baseclass_offset (search_type, i, valaddr, embedded_offset,
719*c50c785cSJohn Marino 				 address, val);
720cf7f2e2dSJohn Marino       if (class_types_same_p (desired_type, TYPE_BASECLASS (search_type, i)))
721cf7f2e2dSJohn Marino 	{
722cf7f2e2dSJohn Marino 	  ++result_count;
723cf7f2e2dSJohn Marino 	  if (*result == NULL)
724cf7f2e2dSJohn Marino 	    *result = value_at_lazy (TYPE_BASECLASS (search_type, i),
725*c50c785cSJohn Marino 				     address + embedded_offset + offset);
726cf7f2e2dSJohn Marino 	}
727cf7f2e2dSJohn Marino       else
728cf7f2e2dSJohn Marino 	result_count += dynamic_cast_check_2 (desired_type,
729*c50c785cSJohn Marino 					      valaddr,
730*c50c785cSJohn Marino 					      embedded_offset + offset,
731*c50c785cSJohn Marino 					      address, val,
732cf7f2e2dSJohn Marino 					      TYPE_BASECLASS (search_type, i),
733cf7f2e2dSJohn Marino 					      result);
734cf7f2e2dSJohn Marino     }
735cf7f2e2dSJohn Marino 
736cf7f2e2dSJohn Marino   return result_count;
737cf7f2e2dSJohn Marino }
738cf7f2e2dSJohn Marino 
739cf7f2e2dSJohn Marino /* The C++ dynamic_cast operator.  */
740cf7f2e2dSJohn Marino 
741cf7f2e2dSJohn Marino struct value *
742cf7f2e2dSJohn Marino value_dynamic_cast (struct type *type, struct value *arg)
743cf7f2e2dSJohn Marino {
744cf7f2e2dSJohn Marino   int full, top, using_enc;
745cf7f2e2dSJohn Marino   struct type *resolved_type = check_typedef (type);
746cf7f2e2dSJohn Marino   struct type *arg_type = check_typedef (value_type (arg));
747cf7f2e2dSJohn Marino   struct type *class_type, *rtti_type;
748cf7f2e2dSJohn Marino   struct value *result, *tem, *original_arg = arg;
749cf7f2e2dSJohn Marino   CORE_ADDR addr;
750cf7f2e2dSJohn Marino   int is_ref = TYPE_CODE (resolved_type) == TYPE_CODE_REF;
751cf7f2e2dSJohn Marino 
752cf7f2e2dSJohn Marino   if (TYPE_CODE (resolved_type) != TYPE_CODE_PTR
753cf7f2e2dSJohn Marino       && TYPE_CODE (resolved_type) != TYPE_CODE_REF)
754cf7f2e2dSJohn Marino     error (_("Argument to dynamic_cast must be a pointer or reference type"));
755cf7f2e2dSJohn Marino   if (TYPE_CODE (TYPE_TARGET_TYPE (resolved_type)) != TYPE_CODE_VOID
756cf7f2e2dSJohn Marino       && TYPE_CODE (TYPE_TARGET_TYPE (resolved_type)) != TYPE_CODE_CLASS)
757cf7f2e2dSJohn Marino     error (_("Argument to dynamic_cast must be pointer to class or `void *'"));
758cf7f2e2dSJohn Marino 
759cf7f2e2dSJohn Marino   class_type = check_typedef (TYPE_TARGET_TYPE (resolved_type));
760cf7f2e2dSJohn Marino   if (TYPE_CODE (resolved_type) == TYPE_CODE_PTR)
761cf7f2e2dSJohn Marino     {
762cf7f2e2dSJohn Marino       if (TYPE_CODE (arg_type) != TYPE_CODE_PTR
763cf7f2e2dSJohn Marino 	  && ! (TYPE_CODE (arg_type) == TYPE_CODE_INT
764cf7f2e2dSJohn Marino 		&& value_as_long (arg) == 0))
765cf7f2e2dSJohn Marino 	error (_("Argument to dynamic_cast does not have pointer type"));
766cf7f2e2dSJohn Marino       if (TYPE_CODE (arg_type) == TYPE_CODE_PTR)
767cf7f2e2dSJohn Marino 	{
768cf7f2e2dSJohn Marino 	  arg_type = check_typedef (TYPE_TARGET_TYPE (arg_type));
769cf7f2e2dSJohn Marino 	  if (TYPE_CODE (arg_type) != TYPE_CODE_CLASS)
770*c50c785cSJohn Marino 	    error (_("Argument to dynamic_cast does "
771*c50c785cSJohn Marino 		     "not have pointer to class type"));
772cf7f2e2dSJohn Marino 	}
773cf7f2e2dSJohn Marino 
774cf7f2e2dSJohn Marino       /* Handle NULL pointers.  */
775cf7f2e2dSJohn Marino       if (value_as_long (arg) == 0)
776cf7f2e2dSJohn Marino 	return value_zero (type, not_lval);
777cf7f2e2dSJohn Marino 
778cf7f2e2dSJohn Marino       arg = value_ind (arg);
779cf7f2e2dSJohn Marino     }
780cf7f2e2dSJohn Marino   else
781cf7f2e2dSJohn Marino     {
782cf7f2e2dSJohn Marino       if (TYPE_CODE (arg_type) != TYPE_CODE_CLASS)
783cf7f2e2dSJohn Marino 	error (_("Argument to dynamic_cast does not have class type"));
784cf7f2e2dSJohn Marino     }
785cf7f2e2dSJohn Marino 
786cf7f2e2dSJohn Marino   /* If the classes are the same, just return the argument.  */
787cf7f2e2dSJohn Marino   if (class_types_same_p (class_type, arg_type))
788cf7f2e2dSJohn Marino     return value_cast (type, arg);
789cf7f2e2dSJohn Marino 
790cf7f2e2dSJohn Marino   /* If the target type is a unique base class of the argument's
791cf7f2e2dSJohn Marino      declared type, just cast it.  */
792cf7f2e2dSJohn Marino   if (is_ancestor (class_type, arg_type))
793cf7f2e2dSJohn Marino     {
794cf7f2e2dSJohn Marino       if (is_unique_ancestor (class_type, arg))
795cf7f2e2dSJohn Marino 	return value_cast (type, original_arg);
796cf7f2e2dSJohn Marino       error (_("Ambiguous dynamic_cast"));
797cf7f2e2dSJohn Marino     }
798cf7f2e2dSJohn Marino 
799cf7f2e2dSJohn Marino   rtti_type = value_rtti_type (arg, &full, &top, &using_enc);
800cf7f2e2dSJohn Marino   if (! rtti_type)
801cf7f2e2dSJohn Marino     error (_("Couldn't determine value's most derived type for dynamic_cast"));
802cf7f2e2dSJohn Marino 
803cf7f2e2dSJohn Marino   /* Compute the most derived object's address.  */
804cf7f2e2dSJohn Marino   addr = value_address (arg);
805cf7f2e2dSJohn Marino   if (full)
806cf7f2e2dSJohn Marino     {
807cf7f2e2dSJohn Marino       /* Done.  */
808cf7f2e2dSJohn Marino     }
809cf7f2e2dSJohn Marino   else if (using_enc)
810cf7f2e2dSJohn Marino     addr += top;
811cf7f2e2dSJohn Marino   else
812cf7f2e2dSJohn Marino     addr += top + value_embedded_offset (arg);
813cf7f2e2dSJohn Marino 
814cf7f2e2dSJohn Marino   /* dynamic_cast<void *> means to return a pointer to the
815cf7f2e2dSJohn Marino      most-derived object.  */
816cf7f2e2dSJohn Marino   if (TYPE_CODE (resolved_type) == TYPE_CODE_PTR
817cf7f2e2dSJohn Marino       && TYPE_CODE (TYPE_TARGET_TYPE (resolved_type)) == TYPE_CODE_VOID)
818cf7f2e2dSJohn Marino     return value_at_lazy (type, addr);
819cf7f2e2dSJohn Marino 
820cf7f2e2dSJohn Marino   tem = value_at (type, addr);
821cf7f2e2dSJohn Marino 
822cf7f2e2dSJohn Marino   /* The first dynamic check specified in 5.2.7.  */
823cf7f2e2dSJohn Marino   if (is_public_ancestor (arg_type, TYPE_TARGET_TYPE (resolved_type)))
824cf7f2e2dSJohn Marino     {
825cf7f2e2dSJohn Marino       if (class_types_same_p (rtti_type, TYPE_TARGET_TYPE (resolved_type)))
826cf7f2e2dSJohn Marino 	return tem;
827cf7f2e2dSJohn Marino       result = NULL;
828cf7f2e2dSJohn Marino       if (dynamic_cast_check_1 (TYPE_TARGET_TYPE (resolved_type),
829*c50c785cSJohn Marino 				value_contents_for_printing (tem),
830*c50c785cSJohn Marino 				value_embedded_offset (tem),
831*c50c785cSJohn Marino 				value_address (tem), tem,
832cf7f2e2dSJohn Marino 				rtti_type, addr,
833cf7f2e2dSJohn Marino 				arg_type,
834cf7f2e2dSJohn Marino 				&result) == 1)
835cf7f2e2dSJohn Marino 	return value_cast (type,
836cf7f2e2dSJohn Marino 			   is_ref ? value_ref (result) : value_addr (result));
837cf7f2e2dSJohn Marino     }
838cf7f2e2dSJohn Marino 
839cf7f2e2dSJohn Marino   /* The second dynamic check specified in 5.2.7.  */
840cf7f2e2dSJohn Marino   result = NULL;
841cf7f2e2dSJohn Marino   if (is_public_ancestor (arg_type, rtti_type)
842cf7f2e2dSJohn Marino       && dynamic_cast_check_2 (TYPE_TARGET_TYPE (resolved_type),
843*c50c785cSJohn Marino 			       value_contents_for_printing (tem),
844*c50c785cSJohn Marino 			       value_embedded_offset (tem),
845*c50c785cSJohn Marino 			       value_address (tem), tem,
846cf7f2e2dSJohn Marino 			       rtti_type, &result) == 1)
847cf7f2e2dSJohn Marino     return value_cast (type,
848cf7f2e2dSJohn Marino 		       is_ref ? value_ref (result) : value_addr (result));
849cf7f2e2dSJohn Marino 
850cf7f2e2dSJohn Marino   if (TYPE_CODE (resolved_type) == TYPE_CODE_PTR)
851cf7f2e2dSJohn Marino     return value_zero (type, not_lval);
852cf7f2e2dSJohn Marino 
853cf7f2e2dSJohn Marino   error (_("dynamic_cast failed"));
854cf7f2e2dSJohn Marino }
855cf7f2e2dSJohn Marino 
8565796c8dcSSimon Schubert /* Create a value of type TYPE that is zero, and return it.  */
8575796c8dcSSimon Schubert 
8585796c8dcSSimon Schubert struct value *
8595796c8dcSSimon Schubert value_zero (struct type *type, enum lval_type lv)
8605796c8dcSSimon Schubert {
8615796c8dcSSimon Schubert   struct value *val = allocate_value (type);
8625796c8dcSSimon Schubert 
863cf7f2e2dSJohn Marino   VALUE_LVAL (val) = lv;
8645796c8dcSSimon Schubert   return val;
8655796c8dcSSimon Schubert }
8665796c8dcSSimon Schubert 
8675796c8dcSSimon Schubert /* Create a value of numeric type TYPE that is one, and return it.  */
8685796c8dcSSimon Schubert 
8695796c8dcSSimon Schubert struct value *
8705796c8dcSSimon Schubert value_one (struct type *type, enum lval_type lv)
8715796c8dcSSimon Schubert {
8725796c8dcSSimon Schubert   struct type *type1 = check_typedef (type);
8735796c8dcSSimon Schubert   struct value *val;
8745796c8dcSSimon Schubert 
8755796c8dcSSimon Schubert   if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
8765796c8dcSSimon Schubert     {
8775796c8dcSSimon Schubert       enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
8785796c8dcSSimon Schubert       gdb_byte v[16];
879cf7f2e2dSJohn Marino 
8805796c8dcSSimon Schubert       decimal_from_string (v, TYPE_LENGTH (type), byte_order, "1");
8815796c8dcSSimon Schubert       val = value_from_decfloat (type, v);
8825796c8dcSSimon Schubert     }
8835796c8dcSSimon Schubert   else if (TYPE_CODE (type1) == TYPE_CODE_FLT)
8845796c8dcSSimon Schubert     {
8855796c8dcSSimon Schubert       val = value_from_double (type, (DOUBLEST) 1);
8865796c8dcSSimon Schubert     }
8875796c8dcSSimon Schubert   else if (is_integral_type (type1))
8885796c8dcSSimon Schubert     {
8895796c8dcSSimon Schubert       val = value_from_longest (type, (LONGEST) 1);
8905796c8dcSSimon Schubert     }
891*c50c785cSJohn Marino   else if (TYPE_CODE (type1) == TYPE_CODE_ARRAY && TYPE_VECTOR (type1))
892*c50c785cSJohn Marino     {
893*c50c785cSJohn Marino       struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type1));
894*c50c785cSJohn Marino       int i;
895*c50c785cSJohn Marino       LONGEST low_bound, high_bound;
896*c50c785cSJohn Marino       struct value *tmp;
897*c50c785cSJohn Marino 
898*c50c785cSJohn Marino       if (!get_array_bounds (type1, &low_bound, &high_bound))
899*c50c785cSJohn Marino 	error (_("Could not determine the vector bounds"));
900*c50c785cSJohn Marino 
901*c50c785cSJohn Marino       val = allocate_value (type);
902*c50c785cSJohn Marino       for (i = 0; i < high_bound - low_bound + 1; i++)
903*c50c785cSJohn Marino 	{
904*c50c785cSJohn Marino 	  tmp = value_one (eltype, lv);
905*c50c785cSJohn Marino 	  memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype),
906*c50c785cSJohn Marino 		  value_contents_all (tmp), TYPE_LENGTH (eltype));
907*c50c785cSJohn Marino 	}
908*c50c785cSJohn Marino     }
9095796c8dcSSimon Schubert   else
9105796c8dcSSimon Schubert     {
9115796c8dcSSimon Schubert       error (_("Not a numeric type."));
9125796c8dcSSimon Schubert     }
9135796c8dcSSimon Schubert 
9145796c8dcSSimon Schubert   VALUE_LVAL (val) = lv;
9155796c8dcSSimon Schubert   return val;
9165796c8dcSSimon Schubert }
9175796c8dcSSimon Schubert 
9185796c8dcSSimon Schubert /* Helper function for value_at, value_at_lazy, and value_at_lazy_stack.  */
9195796c8dcSSimon Schubert 
9205796c8dcSSimon Schubert static struct value *
9215796c8dcSSimon Schubert get_value_at (struct type *type, CORE_ADDR addr, int lazy)
9225796c8dcSSimon Schubert {
9235796c8dcSSimon Schubert   struct value *val;
9245796c8dcSSimon Schubert 
9255796c8dcSSimon Schubert   if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
9265796c8dcSSimon Schubert     error (_("Attempt to dereference a generic pointer."));
9275796c8dcSSimon Schubert 
928*c50c785cSJohn Marino   val = value_from_contents_and_address (type, NULL, addr);
9295796c8dcSSimon Schubert 
930*c50c785cSJohn Marino   if (!lazy)
931*c50c785cSJohn Marino     value_fetch_lazy (val);
9325796c8dcSSimon Schubert 
9335796c8dcSSimon Schubert   return val;
9345796c8dcSSimon Schubert }
9355796c8dcSSimon Schubert 
9365796c8dcSSimon Schubert /* Return a value with type TYPE located at ADDR.
9375796c8dcSSimon Schubert 
9385796c8dcSSimon Schubert    Call value_at only if the data needs to be fetched immediately;
9395796c8dcSSimon Schubert    if we can be 'lazy' and defer the fetch, perhaps indefinately, call
9405796c8dcSSimon Schubert    value_at_lazy instead.  value_at_lazy simply records the address of
9415796c8dcSSimon Schubert    the data and sets the lazy-evaluation-required flag.  The lazy flag
9425796c8dcSSimon Schubert    is tested in the value_contents macro, which is used if and when
9435796c8dcSSimon Schubert    the contents are actually required.
9445796c8dcSSimon Schubert 
9455796c8dcSSimon Schubert    Note: value_at does *NOT* handle embedded offsets; perform such
9465796c8dcSSimon Schubert    adjustments before or after calling it.  */
9475796c8dcSSimon Schubert 
9485796c8dcSSimon Schubert struct value *
9495796c8dcSSimon Schubert value_at (struct type *type, CORE_ADDR addr)
9505796c8dcSSimon Schubert {
9515796c8dcSSimon Schubert   return get_value_at (type, addr, 0);
9525796c8dcSSimon Schubert }
9535796c8dcSSimon Schubert 
9545796c8dcSSimon Schubert /* Return a lazy value with type TYPE located at ADDR (cf. value_at).  */
9555796c8dcSSimon Schubert 
9565796c8dcSSimon Schubert struct value *
9575796c8dcSSimon Schubert value_at_lazy (struct type *type, CORE_ADDR addr)
9585796c8dcSSimon Schubert {
9595796c8dcSSimon Schubert   return get_value_at (type, addr, 1);
9605796c8dcSSimon Schubert }
9615796c8dcSSimon Schubert 
9625796c8dcSSimon Schubert /* Called only from the value_contents and value_contents_all()
9635796c8dcSSimon Schubert    macros, if the current data for a variable needs to be loaded into
9645796c8dcSSimon Schubert    value_contents(VAL).  Fetches the data from the user's process, and
9655796c8dcSSimon Schubert    clears the lazy flag to indicate that the data in the buffer is
9665796c8dcSSimon Schubert    valid.
9675796c8dcSSimon Schubert 
9685796c8dcSSimon Schubert    If the value is zero-length, we avoid calling read_memory, which
9695796c8dcSSimon Schubert    would abort.  We mark the value as fetched anyway -- all 0 bytes of
9705796c8dcSSimon Schubert    it.
9715796c8dcSSimon Schubert 
9725796c8dcSSimon Schubert    This function returns a value because it is used in the
9735796c8dcSSimon Schubert    value_contents macro as part of an expression, where a void would
9745796c8dcSSimon Schubert    not work.  The value is ignored.  */
9755796c8dcSSimon Schubert 
9765796c8dcSSimon Schubert int
9775796c8dcSSimon Schubert value_fetch_lazy (struct value *val)
9785796c8dcSSimon Schubert {
9795796c8dcSSimon Schubert   gdb_assert (value_lazy (val));
9805796c8dcSSimon Schubert   allocate_value_contents (val);
9815796c8dcSSimon Schubert   if (value_bitsize (val))
9825796c8dcSSimon Schubert     {
9835796c8dcSSimon Schubert       /* To read a lazy bitfield, read the entire enclosing value.  This
9845796c8dcSSimon Schubert 	 prevents reading the same block of (possibly volatile) memory once
9855796c8dcSSimon Schubert          per bitfield.  It would be even better to read only the containing
9865796c8dcSSimon Schubert          word, but we have no way to record that just specific bits of a
9875796c8dcSSimon Schubert          value have been fetched.  */
9885796c8dcSSimon Schubert       struct type *type = check_typedef (value_type (val));
9895796c8dcSSimon Schubert       enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
9905796c8dcSSimon Schubert       struct value *parent = value_parent (val);
9915796c8dcSSimon Schubert       LONGEST offset = value_offset (val);
992*c50c785cSJohn Marino       LONGEST num;
9935796c8dcSSimon Schubert       int length = TYPE_LENGTH (type);
994cf7f2e2dSJohn Marino 
995cf7f2e2dSJohn Marino       if (!value_bits_valid (val,
996cf7f2e2dSJohn Marino 			     TARGET_CHAR_BIT * offset + value_bitpos (val),
997cf7f2e2dSJohn Marino 			     value_bitsize (val)))
998cf7f2e2dSJohn Marino 	error (_("value has been optimized out"));
999cf7f2e2dSJohn Marino 
1000*c50c785cSJohn Marino       if (!unpack_value_bits_as_long (value_type (val),
1001*c50c785cSJohn Marino 				      value_contents_for_printing (parent),
1002*c50c785cSJohn Marino 				      offset,
1003*c50c785cSJohn Marino 				      value_bitpos (val),
1004*c50c785cSJohn Marino 				      value_bitsize (val), parent, &num))
1005*c50c785cSJohn Marino 	mark_value_bytes_unavailable (val,
1006*c50c785cSJohn Marino 				      value_embedded_offset (val),
1007*c50c785cSJohn Marino 				      length);
1008*c50c785cSJohn Marino       else
1009*c50c785cSJohn Marino 	store_signed_integer (value_contents_raw (val), length,
1010*c50c785cSJohn Marino 			      byte_order, num);
10115796c8dcSSimon Schubert     }
10125796c8dcSSimon Schubert   else if (VALUE_LVAL (val) == lval_memory)
10135796c8dcSSimon Schubert     {
10145796c8dcSSimon Schubert       CORE_ADDR addr = value_address (val);
10155796c8dcSSimon Schubert       int length = TYPE_LENGTH (check_typedef (value_enclosing_type (val)));
10165796c8dcSSimon Schubert 
10175796c8dcSSimon Schubert       if (length)
1018*c50c785cSJohn Marino 	read_value_memory (val, 0, value_stack (val),
1019*c50c785cSJohn Marino 			   addr, value_contents_all_raw (val), length);
10205796c8dcSSimon Schubert     }
10215796c8dcSSimon Schubert   else if (VALUE_LVAL (val) == lval_register)
10225796c8dcSSimon Schubert     {
10235796c8dcSSimon Schubert       struct frame_info *frame;
10245796c8dcSSimon Schubert       int regnum;
10255796c8dcSSimon Schubert       struct type *type = check_typedef (value_type (val));
10265796c8dcSSimon Schubert       struct value *new_val = val, *mark = value_mark ();
10275796c8dcSSimon Schubert 
10285796c8dcSSimon Schubert       /* Offsets are not supported here; lazy register values must
10295796c8dcSSimon Schubert 	 refer to the entire register.  */
10305796c8dcSSimon Schubert       gdb_assert (value_offset (val) == 0);
10315796c8dcSSimon Schubert 
10325796c8dcSSimon Schubert       while (VALUE_LVAL (new_val) == lval_register && value_lazy (new_val))
10335796c8dcSSimon Schubert 	{
10345796c8dcSSimon Schubert 	  frame = frame_find_by_id (VALUE_FRAME_ID (new_val));
10355796c8dcSSimon Schubert 	  regnum = VALUE_REGNUM (new_val);
10365796c8dcSSimon Schubert 
10375796c8dcSSimon Schubert 	  gdb_assert (frame != NULL);
10385796c8dcSSimon Schubert 
10395796c8dcSSimon Schubert 	  /* Convertible register routines are used for multi-register
10405796c8dcSSimon Schubert 	     values and for interpretation in different types
10415796c8dcSSimon Schubert 	     (e.g. float or int from a double register).  Lazy
10425796c8dcSSimon Schubert 	     register values should have the register's natural type,
10435796c8dcSSimon Schubert 	     so they do not apply.  */
10445796c8dcSSimon Schubert 	  gdb_assert (!gdbarch_convert_register_p (get_frame_arch (frame),
10455796c8dcSSimon Schubert 						   regnum, type));
10465796c8dcSSimon Schubert 
10475796c8dcSSimon Schubert 	  new_val = get_frame_register_value (frame, regnum);
10485796c8dcSSimon Schubert 	}
10495796c8dcSSimon Schubert 
10505796c8dcSSimon Schubert       /* If it's still lazy (for instance, a saved register on the
10515796c8dcSSimon Schubert 	 stack), fetch it.  */
10525796c8dcSSimon Schubert       if (value_lazy (new_val))
10535796c8dcSSimon Schubert 	value_fetch_lazy (new_val);
10545796c8dcSSimon Schubert 
1055*c50c785cSJohn Marino       /* If the register was not saved, mark it optimized out.  */
10565796c8dcSSimon Schubert       if (value_optimized_out (new_val))
10575796c8dcSSimon Schubert 	set_value_optimized_out (val, 1);
10585796c8dcSSimon Schubert       else
1059*c50c785cSJohn Marino 	{
1060*c50c785cSJohn Marino 	  set_value_lazy (val, 0);
1061*c50c785cSJohn Marino 	  value_contents_copy (val, value_embedded_offset (val),
1062*c50c785cSJohn Marino 			       new_val, value_embedded_offset (new_val),
10635796c8dcSSimon Schubert 			       TYPE_LENGTH (type));
1064*c50c785cSJohn Marino 	}
10655796c8dcSSimon Schubert 
10665796c8dcSSimon Schubert       if (frame_debug)
10675796c8dcSSimon Schubert 	{
10685796c8dcSSimon Schubert 	  struct gdbarch *gdbarch;
10695796c8dcSSimon Schubert 	  frame = frame_find_by_id (VALUE_FRAME_ID (val));
10705796c8dcSSimon Schubert 	  regnum = VALUE_REGNUM (val);
10715796c8dcSSimon Schubert 	  gdbarch = get_frame_arch (frame);
10725796c8dcSSimon Schubert 
1073*c50c785cSJohn Marino 	  fprintf_unfiltered (gdb_stdlog,
1074*c50c785cSJohn Marino 			      "{ value_fetch_lazy "
1075*c50c785cSJohn Marino 			      "(frame=%d,regnum=%d(%s),...) ",
10765796c8dcSSimon Schubert 			      frame_relative_level (frame), regnum,
10775796c8dcSSimon Schubert 			      user_reg_map_regnum_to_name (gdbarch, regnum));
10785796c8dcSSimon Schubert 
10795796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stdlog, "->");
10805796c8dcSSimon Schubert 	  if (value_optimized_out (new_val))
10815796c8dcSSimon Schubert 	    fprintf_unfiltered (gdb_stdlog, " optimized out");
10825796c8dcSSimon Schubert 	  else
10835796c8dcSSimon Schubert 	    {
10845796c8dcSSimon Schubert 	      int i;
10855796c8dcSSimon Schubert 	      const gdb_byte *buf = value_contents (new_val);
10865796c8dcSSimon Schubert 
10875796c8dcSSimon Schubert 	      if (VALUE_LVAL (new_val) == lval_register)
10885796c8dcSSimon Schubert 		fprintf_unfiltered (gdb_stdlog, " register=%d",
10895796c8dcSSimon Schubert 				    VALUE_REGNUM (new_val));
10905796c8dcSSimon Schubert 	      else if (VALUE_LVAL (new_val) == lval_memory)
10915796c8dcSSimon Schubert 		fprintf_unfiltered (gdb_stdlog, " address=%s",
10925796c8dcSSimon Schubert 				    paddress (gdbarch,
10935796c8dcSSimon Schubert 					      value_address (new_val)));
10945796c8dcSSimon Schubert 	      else
10955796c8dcSSimon Schubert 		fprintf_unfiltered (gdb_stdlog, " computed");
10965796c8dcSSimon Schubert 
10975796c8dcSSimon Schubert 	      fprintf_unfiltered (gdb_stdlog, " bytes=");
10985796c8dcSSimon Schubert 	      fprintf_unfiltered (gdb_stdlog, "[");
10995796c8dcSSimon Schubert 	      for (i = 0; i < register_size (gdbarch, regnum); i++)
11005796c8dcSSimon Schubert 		fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
11015796c8dcSSimon Schubert 	      fprintf_unfiltered (gdb_stdlog, "]");
11025796c8dcSSimon Schubert 	    }
11035796c8dcSSimon Schubert 
11045796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stdlog, " }\n");
11055796c8dcSSimon Schubert 	}
11065796c8dcSSimon Schubert 
11075796c8dcSSimon Schubert       /* Dispose of the intermediate values.  This prevents
11085796c8dcSSimon Schubert 	 watchpoints from trying to watch the saved frame pointer.  */
11095796c8dcSSimon Schubert       value_free_to_mark (mark);
11105796c8dcSSimon Schubert     }
11115796c8dcSSimon Schubert   else if (VALUE_LVAL (val) == lval_computed)
11125796c8dcSSimon Schubert     value_computed_funcs (val)->read (val);
1113*c50c785cSJohn Marino   else if (value_optimized_out (val))
1114*c50c785cSJohn Marino     /* Keep it optimized out.  */;
11155796c8dcSSimon Schubert   else
1116*c50c785cSJohn Marino     internal_error (__FILE__, __LINE__, _("Unexpected lazy value type."));
11175796c8dcSSimon Schubert 
11185796c8dcSSimon Schubert   set_value_lazy (val, 0);
11195796c8dcSSimon Schubert   return 0;
11205796c8dcSSimon Schubert }
11215796c8dcSSimon Schubert 
1122*c50c785cSJohn Marino void
1123*c50c785cSJohn Marino read_value_memory (struct value *val, int embedded_offset,
1124*c50c785cSJohn Marino 		   int stack, CORE_ADDR memaddr,
1125*c50c785cSJohn Marino 		   gdb_byte *buffer, size_t length)
1126*c50c785cSJohn Marino {
1127*c50c785cSJohn Marino   if (length)
1128*c50c785cSJohn Marino     {
1129*c50c785cSJohn Marino       VEC(mem_range_s) *available_memory;
1130*c50c785cSJohn Marino 
1131*c50c785cSJohn Marino       if (get_traceframe_number () < 0
1132*c50c785cSJohn Marino 	  || !traceframe_available_memory (&available_memory, memaddr, length))
1133*c50c785cSJohn Marino 	{
1134*c50c785cSJohn Marino 	  if (stack)
1135*c50c785cSJohn Marino 	    read_stack (memaddr, buffer, length);
1136*c50c785cSJohn Marino 	  else
1137*c50c785cSJohn Marino 	    read_memory (memaddr, buffer, length);
1138*c50c785cSJohn Marino 	}
1139*c50c785cSJohn Marino       else
1140*c50c785cSJohn Marino 	{
1141*c50c785cSJohn Marino 	  struct target_section_table *table;
1142*c50c785cSJohn Marino 	  struct cleanup *old_chain;
1143*c50c785cSJohn Marino 	  CORE_ADDR unavail;
1144*c50c785cSJohn Marino 	  mem_range_s *r;
1145*c50c785cSJohn Marino 	  int i;
1146*c50c785cSJohn Marino 
1147*c50c785cSJohn Marino 	  /* Fallback to reading from read-only sections.  */
1148*c50c785cSJohn Marino 	  table = target_get_section_table (&exec_ops);
1149*c50c785cSJohn Marino 	  available_memory =
1150*c50c785cSJohn Marino 	    section_table_available_memory (available_memory,
1151*c50c785cSJohn Marino 					    memaddr, length,
1152*c50c785cSJohn Marino 					    table->sections,
1153*c50c785cSJohn Marino 					    table->sections_end);
1154*c50c785cSJohn Marino 
1155*c50c785cSJohn Marino 	  old_chain = make_cleanup (VEC_cleanup(mem_range_s),
1156*c50c785cSJohn Marino 				    &available_memory);
1157*c50c785cSJohn Marino 
1158*c50c785cSJohn Marino 	  normalize_mem_ranges (available_memory);
1159*c50c785cSJohn Marino 
1160*c50c785cSJohn Marino 	  /* Mark which bytes are unavailable, and read those which
1161*c50c785cSJohn Marino 	     are available.  */
1162*c50c785cSJohn Marino 
1163*c50c785cSJohn Marino 	  unavail = memaddr;
1164*c50c785cSJohn Marino 
1165*c50c785cSJohn Marino 	  for (i = 0;
1166*c50c785cSJohn Marino 	       VEC_iterate (mem_range_s, available_memory, i, r);
1167*c50c785cSJohn Marino 	       i++)
1168*c50c785cSJohn Marino 	    {
1169*c50c785cSJohn Marino 	      if (mem_ranges_overlap (r->start, r->length,
1170*c50c785cSJohn Marino 				      memaddr, length))
1171*c50c785cSJohn Marino 		{
1172*c50c785cSJohn Marino 		  CORE_ADDR lo1, hi1, lo2, hi2;
1173*c50c785cSJohn Marino 		  CORE_ADDR start, end;
1174*c50c785cSJohn Marino 
1175*c50c785cSJohn Marino 		  /* Get the intersection window.  */
1176*c50c785cSJohn Marino 		  lo1 = memaddr;
1177*c50c785cSJohn Marino 		  hi1 = memaddr + length;
1178*c50c785cSJohn Marino 		  lo2 = r->start;
1179*c50c785cSJohn Marino 		  hi2 = r->start + r->length;
1180*c50c785cSJohn Marino 		  start = max (lo1, lo2);
1181*c50c785cSJohn Marino 		  end = min (hi1, hi2);
1182*c50c785cSJohn Marino 
1183*c50c785cSJohn Marino 		  gdb_assert (end - memaddr <= length);
1184*c50c785cSJohn Marino 
1185*c50c785cSJohn Marino 		  if (start > unavail)
1186*c50c785cSJohn Marino 		    mark_value_bytes_unavailable (val,
1187*c50c785cSJohn Marino 						  (embedded_offset
1188*c50c785cSJohn Marino 						   + unavail - memaddr),
1189*c50c785cSJohn Marino 						  start - unavail);
1190*c50c785cSJohn Marino 		  unavail = end;
1191*c50c785cSJohn Marino 
1192*c50c785cSJohn Marino 		  read_memory (start, buffer + start - memaddr, end - start);
1193*c50c785cSJohn Marino 		}
1194*c50c785cSJohn Marino 	    }
1195*c50c785cSJohn Marino 
1196*c50c785cSJohn Marino 	  if (unavail != memaddr + length)
1197*c50c785cSJohn Marino 	    mark_value_bytes_unavailable (val,
1198*c50c785cSJohn Marino 					  embedded_offset + unavail - memaddr,
1199*c50c785cSJohn Marino 					  (memaddr + length) - unavail);
1200*c50c785cSJohn Marino 
1201*c50c785cSJohn Marino 	  do_cleanups (old_chain);
1202*c50c785cSJohn Marino 	}
1203*c50c785cSJohn Marino     }
1204*c50c785cSJohn Marino }
12055796c8dcSSimon Schubert 
12065796c8dcSSimon Schubert /* Store the contents of FROMVAL into the location of TOVAL.
12075796c8dcSSimon Schubert    Return a new value with the location of TOVAL and contents of FROMVAL.  */
12085796c8dcSSimon Schubert 
12095796c8dcSSimon Schubert struct value *
12105796c8dcSSimon Schubert value_assign (struct value *toval, struct value *fromval)
12115796c8dcSSimon Schubert {
12125796c8dcSSimon Schubert   struct type *type;
12135796c8dcSSimon Schubert   struct value *val;
12145796c8dcSSimon Schubert   struct frame_id old_frame;
12155796c8dcSSimon Schubert 
12165796c8dcSSimon Schubert   if (!deprecated_value_modifiable (toval))
12175796c8dcSSimon Schubert     error (_("Left operand of assignment is not a modifiable lvalue."));
12185796c8dcSSimon Schubert 
12195796c8dcSSimon Schubert   toval = coerce_ref (toval);
12205796c8dcSSimon Schubert 
12215796c8dcSSimon Schubert   type = value_type (toval);
12225796c8dcSSimon Schubert   if (VALUE_LVAL (toval) != lval_internalvar)
12235796c8dcSSimon Schubert     fromval = value_cast (type, fromval);
12245796c8dcSSimon Schubert   else
12255796c8dcSSimon Schubert     {
12265796c8dcSSimon Schubert       /* Coerce arrays and functions to pointers, except for arrays
12275796c8dcSSimon Schubert 	 which only live in GDB's storage.  */
12285796c8dcSSimon Schubert       if (!value_must_coerce_to_target (fromval))
12295796c8dcSSimon Schubert 	fromval = coerce_array (fromval);
12305796c8dcSSimon Schubert     }
12315796c8dcSSimon Schubert 
12325796c8dcSSimon Schubert   CHECK_TYPEDEF (type);
12335796c8dcSSimon Schubert 
12345796c8dcSSimon Schubert   /* Since modifying a register can trash the frame chain, and
12355796c8dcSSimon Schubert      modifying memory can trash the frame cache, we save the old frame
12365796c8dcSSimon Schubert      and then restore the new frame afterwards.  */
12375796c8dcSSimon Schubert   old_frame = get_frame_id (deprecated_safe_get_selected_frame ());
12385796c8dcSSimon Schubert 
12395796c8dcSSimon Schubert   switch (VALUE_LVAL (toval))
12405796c8dcSSimon Schubert     {
12415796c8dcSSimon Schubert     case lval_internalvar:
12425796c8dcSSimon Schubert       set_internalvar (VALUE_INTERNALVAR (toval), fromval);
1243*c50c785cSJohn Marino       return value_of_internalvar (get_type_arch (type),
1244*c50c785cSJohn Marino 				   VALUE_INTERNALVAR (toval));
12455796c8dcSSimon Schubert 
12465796c8dcSSimon Schubert     case lval_internalvar_component:
12475796c8dcSSimon Schubert       set_internalvar_component (VALUE_INTERNALVAR (toval),
12485796c8dcSSimon Schubert 				 value_offset (toval),
12495796c8dcSSimon Schubert 				 value_bitpos (toval),
12505796c8dcSSimon Schubert 				 value_bitsize (toval),
12515796c8dcSSimon Schubert 				 fromval);
12525796c8dcSSimon Schubert       break;
12535796c8dcSSimon Schubert 
12545796c8dcSSimon Schubert     case lval_memory:
12555796c8dcSSimon Schubert       {
12565796c8dcSSimon Schubert 	const gdb_byte *dest_buffer;
12575796c8dcSSimon Schubert 	CORE_ADDR changed_addr;
12585796c8dcSSimon Schubert 	int changed_len;
12595796c8dcSSimon Schubert         gdb_byte buffer[sizeof (LONGEST)];
12605796c8dcSSimon Schubert 
12615796c8dcSSimon Schubert 	if (value_bitsize (toval))
12625796c8dcSSimon Schubert 	  {
12635796c8dcSSimon Schubert 	    struct value *parent = value_parent (toval);
12645796c8dcSSimon Schubert 
1265cf7f2e2dSJohn Marino 	    changed_addr = value_address (parent) + value_offset (toval);
12665796c8dcSSimon Schubert 	    changed_len = (value_bitpos (toval)
12675796c8dcSSimon Schubert 			   + value_bitsize (toval)
12685796c8dcSSimon Schubert 			   + HOST_CHAR_BIT - 1)
12695796c8dcSSimon Schubert 	      / HOST_CHAR_BIT;
12705796c8dcSSimon Schubert 
12715796c8dcSSimon Schubert 	    /* If we can read-modify-write exactly the size of the
12725796c8dcSSimon Schubert 	       containing type (e.g. short or int) then do so.  This
12735796c8dcSSimon Schubert 	       is safer for volatile bitfields mapped to hardware
12745796c8dcSSimon Schubert 	       registers.  */
12755796c8dcSSimon Schubert 	    if (changed_len < TYPE_LENGTH (type)
12765796c8dcSSimon Schubert 		&& TYPE_LENGTH (type) <= (int) sizeof (LONGEST)
12775796c8dcSSimon Schubert 		&& ((LONGEST) changed_addr % TYPE_LENGTH (type)) == 0)
12785796c8dcSSimon Schubert 	      changed_len = TYPE_LENGTH (type);
12795796c8dcSSimon Schubert 
12805796c8dcSSimon Schubert 	    if (changed_len > (int) sizeof (LONGEST))
1281*c50c785cSJohn Marino 	      error (_("Can't handle bitfields which "
1282*c50c785cSJohn Marino 		       "don't fit in a %d bit word."),
12835796c8dcSSimon Schubert 		     (int) sizeof (LONGEST) * HOST_CHAR_BIT);
12845796c8dcSSimon Schubert 
12855796c8dcSSimon Schubert 	    read_memory (changed_addr, buffer, changed_len);
12865796c8dcSSimon Schubert 	    modify_field (type, buffer, value_as_long (fromval),
12875796c8dcSSimon Schubert 			  value_bitpos (toval), value_bitsize (toval));
12885796c8dcSSimon Schubert 	    dest_buffer = buffer;
12895796c8dcSSimon Schubert 	  }
12905796c8dcSSimon Schubert 	else
12915796c8dcSSimon Schubert 	  {
12925796c8dcSSimon Schubert 	    changed_addr = value_address (toval);
12935796c8dcSSimon Schubert 	    changed_len = TYPE_LENGTH (type);
12945796c8dcSSimon Schubert 	    dest_buffer = value_contents (fromval);
12955796c8dcSSimon Schubert 	  }
12965796c8dcSSimon Schubert 
12975796c8dcSSimon Schubert 	write_memory (changed_addr, dest_buffer, changed_len);
1298cf7f2e2dSJohn Marino 	observer_notify_memory_changed (changed_addr, changed_len,
1299cf7f2e2dSJohn Marino 					dest_buffer);
13005796c8dcSSimon Schubert       }
13015796c8dcSSimon Schubert       break;
13025796c8dcSSimon Schubert 
13035796c8dcSSimon Schubert     case lval_register:
13045796c8dcSSimon Schubert       {
13055796c8dcSSimon Schubert 	struct frame_info *frame;
13065796c8dcSSimon Schubert 	struct gdbarch *gdbarch;
13075796c8dcSSimon Schubert 	int value_reg;
13085796c8dcSSimon Schubert 
13095796c8dcSSimon Schubert 	/* Figure out which frame this is in currently.  */
13105796c8dcSSimon Schubert 	frame = frame_find_by_id (VALUE_FRAME_ID (toval));
13115796c8dcSSimon Schubert 	value_reg = VALUE_REGNUM (toval);
13125796c8dcSSimon Schubert 
13135796c8dcSSimon Schubert 	if (!frame)
13145796c8dcSSimon Schubert 	  error (_("Value being assigned to is no longer active."));
13155796c8dcSSimon Schubert 
13165796c8dcSSimon Schubert 	gdbarch = get_frame_arch (frame);
13175796c8dcSSimon Schubert 	if (gdbarch_convert_register_p (gdbarch, VALUE_REGNUM (toval), type))
13185796c8dcSSimon Schubert 	  {
13195796c8dcSSimon Schubert 	    /* If TOVAL is a special machine register requiring
13205796c8dcSSimon Schubert 	       conversion of program values to a special raw
13215796c8dcSSimon Schubert 	       format.  */
13225796c8dcSSimon Schubert 	    gdbarch_value_to_register (gdbarch, frame,
13235796c8dcSSimon Schubert 				       VALUE_REGNUM (toval), type,
13245796c8dcSSimon Schubert 				       value_contents (fromval));
13255796c8dcSSimon Schubert 	  }
13265796c8dcSSimon Schubert 	else
13275796c8dcSSimon Schubert 	  {
13285796c8dcSSimon Schubert 	    if (value_bitsize (toval))
13295796c8dcSSimon Schubert 	      {
13305796c8dcSSimon Schubert 		struct value *parent = value_parent (toval);
13315796c8dcSSimon Schubert 		int offset = value_offset (parent) + value_offset (toval);
13325796c8dcSSimon Schubert 		int changed_len;
13335796c8dcSSimon Schubert 		gdb_byte buffer[sizeof (LONGEST)];
1334*c50c785cSJohn Marino 		int optim, unavail;
13355796c8dcSSimon Schubert 
13365796c8dcSSimon Schubert 		changed_len = (value_bitpos (toval)
13375796c8dcSSimon Schubert 			       + value_bitsize (toval)
13385796c8dcSSimon Schubert 			       + HOST_CHAR_BIT - 1)
13395796c8dcSSimon Schubert 		  / HOST_CHAR_BIT;
13405796c8dcSSimon Schubert 
13415796c8dcSSimon Schubert 		if (changed_len > (int) sizeof (LONGEST))
1342*c50c785cSJohn Marino 		  error (_("Can't handle bitfields which "
1343*c50c785cSJohn Marino 			   "don't fit in a %d bit word."),
13445796c8dcSSimon Schubert 			 (int) sizeof (LONGEST) * HOST_CHAR_BIT);
13455796c8dcSSimon Schubert 
1346*c50c785cSJohn Marino 		if (!get_frame_register_bytes (frame, value_reg, offset,
1347*c50c785cSJohn Marino 					       changed_len, buffer,
1348*c50c785cSJohn Marino 					       &optim, &unavail))
1349*c50c785cSJohn Marino 		  {
1350*c50c785cSJohn Marino 		    if (optim)
1351*c50c785cSJohn Marino 		      error (_("value has been optimized out"));
1352*c50c785cSJohn Marino 		    if (unavail)
1353*c50c785cSJohn Marino 		      throw_error (NOT_AVAILABLE_ERROR,
1354*c50c785cSJohn Marino 				   _("value is not available"));
1355*c50c785cSJohn Marino 		  }
13565796c8dcSSimon Schubert 
13575796c8dcSSimon Schubert 		modify_field (type, buffer, value_as_long (fromval),
13585796c8dcSSimon Schubert 			      value_bitpos (toval), value_bitsize (toval));
13595796c8dcSSimon Schubert 
13605796c8dcSSimon Schubert 		put_frame_register_bytes (frame, value_reg, offset,
13615796c8dcSSimon Schubert 					  changed_len, buffer);
13625796c8dcSSimon Schubert 	      }
13635796c8dcSSimon Schubert 	    else
13645796c8dcSSimon Schubert 	      {
13655796c8dcSSimon Schubert 		put_frame_register_bytes (frame, value_reg,
13665796c8dcSSimon Schubert 					  value_offset (toval),
13675796c8dcSSimon Schubert 					  TYPE_LENGTH (type),
13685796c8dcSSimon Schubert 					  value_contents (fromval));
13695796c8dcSSimon Schubert 	      }
13705796c8dcSSimon Schubert 	  }
13715796c8dcSSimon Schubert 
13725796c8dcSSimon Schubert 	if (deprecated_register_changed_hook)
13735796c8dcSSimon Schubert 	  deprecated_register_changed_hook (-1);
13745796c8dcSSimon Schubert 	observer_notify_target_changed (&current_target);
13755796c8dcSSimon Schubert 	break;
13765796c8dcSSimon Schubert       }
13775796c8dcSSimon Schubert 
13785796c8dcSSimon Schubert     case lval_computed:
13795796c8dcSSimon Schubert       {
13805796c8dcSSimon Schubert 	struct lval_funcs *funcs = value_computed_funcs (toval);
13815796c8dcSSimon Schubert 
13825796c8dcSSimon Schubert 	funcs->write (toval, fromval);
13835796c8dcSSimon Schubert       }
13845796c8dcSSimon Schubert       break;
13855796c8dcSSimon Schubert 
13865796c8dcSSimon Schubert     default:
13875796c8dcSSimon Schubert       error (_("Left operand of assignment is not an lvalue."));
13885796c8dcSSimon Schubert     }
13895796c8dcSSimon Schubert 
13905796c8dcSSimon Schubert   /* Assigning to the stack pointer, frame pointer, and other
13915796c8dcSSimon Schubert      (architecture and calling convention specific) registers may
13925796c8dcSSimon Schubert      cause the frame cache to be out of date.  Assigning to memory
13935796c8dcSSimon Schubert      also can.  We just do this on all assignments to registers or
13945796c8dcSSimon Schubert      memory, for simplicity's sake; I doubt the slowdown matters.  */
13955796c8dcSSimon Schubert   switch (VALUE_LVAL (toval))
13965796c8dcSSimon Schubert     {
13975796c8dcSSimon Schubert     case lval_memory:
13985796c8dcSSimon Schubert     case lval_register:
1399cf7f2e2dSJohn Marino     case lval_computed:
14005796c8dcSSimon Schubert 
14015796c8dcSSimon Schubert       reinit_frame_cache ();
14025796c8dcSSimon Schubert 
14035796c8dcSSimon Schubert       /* Having destroyed the frame cache, restore the selected
14045796c8dcSSimon Schubert 	 frame.  */
14055796c8dcSSimon Schubert 
14065796c8dcSSimon Schubert       /* FIXME: cagney/2002-11-02: There has to be a better way of
14075796c8dcSSimon Schubert 	 doing this.  Instead of constantly saving/restoring the
14085796c8dcSSimon Schubert 	 frame.  Why not create a get_selected_frame() function that,
14095796c8dcSSimon Schubert 	 having saved the selected frame's ID can automatically
14105796c8dcSSimon Schubert 	 re-find the previously selected frame automatically.  */
14115796c8dcSSimon Schubert 
14125796c8dcSSimon Schubert       {
14135796c8dcSSimon Schubert 	struct frame_info *fi = frame_find_by_id (old_frame);
1414cf7f2e2dSJohn Marino 
14155796c8dcSSimon Schubert 	if (fi != NULL)
14165796c8dcSSimon Schubert 	  select_frame (fi);
14175796c8dcSSimon Schubert       }
14185796c8dcSSimon Schubert 
14195796c8dcSSimon Schubert       break;
14205796c8dcSSimon Schubert     default:
14215796c8dcSSimon Schubert       break;
14225796c8dcSSimon Schubert     }
14235796c8dcSSimon Schubert 
14245796c8dcSSimon Schubert   /* If the field does not entirely fill a LONGEST, then zero the sign
14255796c8dcSSimon Schubert      bits.  If the field is signed, and is negative, then sign
14265796c8dcSSimon Schubert      extend.  */
14275796c8dcSSimon Schubert   if ((value_bitsize (toval) > 0)
14285796c8dcSSimon Schubert       && (value_bitsize (toval) < 8 * (int) sizeof (LONGEST)))
14295796c8dcSSimon Schubert     {
14305796c8dcSSimon Schubert       LONGEST fieldval = value_as_long (fromval);
14315796c8dcSSimon Schubert       LONGEST valmask = (((ULONGEST) 1) << value_bitsize (toval)) - 1;
14325796c8dcSSimon Schubert 
14335796c8dcSSimon Schubert       fieldval &= valmask;
14345796c8dcSSimon Schubert       if (!TYPE_UNSIGNED (type)
14355796c8dcSSimon Schubert 	  && (fieldval & (valmask ^ (valmask >> 1))))
14365796c8dcSSimon Schubert 	fieldval |= ~valmask;
14375796c8dcSSimon Schubert 
14385796c8dcSSimon Schubert       fromval = value_from_longest (type, fieldval);
14395796c8dcSSimon Schubert     }
14405796c8dcSSimon Schubert 
1441*c50c785cSJohn Marino   /* The return value is a copy of TOVAL so it shares its location
1442*c50c785cSJohn Marino      information, but its contents are updated from FROMVAL.  This
1443*c50c785cSJohn Marino      implies the returned value is not lazy, even if TOVAL was.  */
14445796c8dcSSimon Schubert   val = value_copy (toval);
1445*c50c785cSJohn Marino   set_value_lazy (val, 0);
14465796c8dcSSimon Schubert   memcpy (value_contents_raw (val), value_contents (fromval),
14475796c8dcSSimon Schubert 	  TYPE_LENGTH (type));
1448*c50c785cSJohn Marino 
1449*c50c785cSJohn Marino   /* We copy over the enclosing type and pointed-to offset from FROMVAL
1450*c50c785cSJohn Marino      in the case of pointer types.  For object types, the enclosing type
1451*c50c785cSJohn Marino      and embedded offset must *not* be copied: the target object refered
1452*c50c785cSJohn Marino      to by TOVAL retains its original dynamic type after assignment.  */
1453*c50c785cSJohn Marino   if (TYPE_CODE (type) == TYPE_CODE_PTR)
1454*c50c785cSJohn Marino     {
1455*c50c785cSJohn Marino       set_value_enclosing_type (val, value_enclosing_type (fromval));
14565796c8dcSSimon Schubert       set_value_pointed_to_offset (val, value_pointed_to_offset (fromval));
1457*c50c785cSJohn Marino     }
14585796c8dcSSimon Schubert 
14595796c8dcSSimon Schubert   return val;
14605796c8dcSSimon Schubert }
14615796c8dcSSimon Schubert 
14625796c8dcSSimon Schubert /* Extend a value VAL to COUNT repetitions of its type.  */
14635796c8dcSSimon Schubert 
14645796c8dcSSimon Schubert struct value *
14655796c8dcSSimon Schubert value_repeat (struct value *arg1, int count)
14665796c8dcSSimon Schubert {
14675796c8dcSSimon Schubert   struct value *val;
14685796c8dcSSimon Schubert 
14695796c8dcSSimon Schubert   if (VALUE_LVAL (arg1) != lval_memory)
14705796c8dcSSimon Schubert     error (_("Only values in memory can be extended with '@'."));
14715796c8dcSSimon Schubert   if (count < 1)
14725796c8dcSSimon Schubert     error (_("Invalid number %d of repetitions."), count);
14735796c8dcSSimon Schubert 
14745796c8dcSSimon Schubert   val = allocate_repeat_value (value_enclosing_type (arg1), count);
14755796c8dcSSimon Schubert 
14765796c8dcSSimon Schubert   VALUE_LVAL (val) = lval_memory;
14775796c8dcSSimon Schubert   set_value_address (val, value_address (arg1));
14785796c8dcSSimon Schubert 
1479*c50c785cSJohn Marino   read_value_memory (val, 0, value_stack (val), value_address (val),
1480*c50c785cSJohn Marino 		     value_contents_all_raw (val),
1481*c50c785cSJohn Marino 		     TYPE_LENGTH (value_enclosing_type (val)));
1482*c50c785cSJohn Marino 
14835796c8dcSSimon Schubert   return val;
14845796c8dcSSimon Schubert }
14855796c8dcSSimon Schubert 
14865796c8dcSSimon Schubert struct value *
14875796c8dcSSimon Schubert value_of_variable (struct symbol *var, struct block *b)
14885796c8dcSSimon Schubert {
14895796c8dcSSimon Schubert   struct value *val;
14905796c8dcSSimon Schubert   struct frame_info *frame;
14915796c8dcSSimon Schubert 
14925796c8dcSSimon Schubert   if (!symbol_read_needs_frame (var))
14935796c8dcSSimon Schubert     frame = NULL;
14945796c8dcSSimon Schubert   else if (!b)
14955796c8dcSSimon Schubert     frame = get_selected_frame (_("No frame selected."));
14965796c8dcSSimon Schubert   else
14975796c8dcSSimon Schubert     {
14985796c8dcSSimon Schubert       frame = block_innermost_frame (b);
14995796c8dcSSimon Schubert       if (!frame)
15005796c8dcSSimon Schubert 	{
15015796c8dcSSimon Schubert 	  if (BLOCK_FUNCTION (b) && !block_inlined_p (b)
15025796c8dcSSimon Schubert 	      && SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)))
15035796c8dcSSimon Schubert 	    error (_("No frame is currently executing in block %s."),
15045796c8dcSSimon Schubert 		   SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)));
15055796c8dcSSimon Schubert 	  else
15065796c8dcSSimon Schubert 	    error (_("No frame is currently executing in specified block"));
15075796c8dcSSimon Schubert 	}
15085796c8dcSSimon Schubert     }
15095796c8dcSSimon Schubert 
15105796c8dcSSimon Schubert   val = read_var_value (var, frame);
15115796c8dcSSimon Schubert   if (!val)
15125796c8dcSSimon Schubert     error (_("Address of symbol \"%s\" is unknown."), SYMBOL_PRINT_NAME (var));
15135796c8dcSSimon Schubert 
15145796c8dcSSimon Schubert   return val;
15155796c8dcSSimon Schubert }
15165796c8dcSSimon Schubert 
15175796c8dcSSimon Schubert struct value *
15185796c8dcSSimon Schubert address_of_variable (struct symbol *var, struct block *b)
15195796c8dcSSimon Schubert {
15205796c8dcSSimon Schubert   struct type *type = SYMBOL_TYPE (var);
15215796c8dcSSimon Schubert   struct value *val;
15225796c8dcSSimon Schubert 
15235796c8dcSSimon Schubert   /* Evaluate it first; if the result is a memory address, we're fine.
15245796c8dcSSimon Schubert      Lazy evaluation pays off here.  */
15255796c8dcSSimon Schubert 
15265796c8dcSSimon Schubert   val = value_of_variable (var, b);
15275796c8dcSSimon Schubert 
15285796c8dcSSimon Schubert   if ((VALUE_LVAL (val) == lval_memory && value_lazy (val))
15295796c8dcSSimon Schubert       || TYPE_CODE (type) == TYPE_CODE_FUNC)
15305796c8dcSSimon Schubert     {
15315796c8dcSSimon Schubert       CORE_ADDR addr = value_address (val);
1532cf7f2e2dSJohn Marino 
15335796c8dcSSimon Schubert       return value_from_pointer (lookup_pointer_type (type), addr);
15345796c8dcSSimon Schubert     }
15355796c8dcSSimon Schubert 
15365796c8dcSSimon Schubert   /* Not a memory address; check what the problem was.  */
15375796c8dcSSimon Schubert   switch (VALUE_LVAL (val))
15385796c8dcSSimon Schubert     {
15395796c8dcSSimon Schubert     case lval_register:
15405796c8dcSSimon Schubert       {
15415796c8dcSSimon Schubert 	struct frame_info *frame;
15425796c8dcSSimon Schubert 	const char *regname;
15435796c8dcSSimon Schubert 
15445796c8dcSSimon Schubert 	frame = frame_find_by_id (VALUE_FRAME_ID (val));
15455796c8dcSSimon Schubert 	gdb_assert (frame);
15465796c8dcSSimon Schubert 
15475796c8dcSSimon Schubert 	regname = gdbarch_register_name (get_frame_arch (frame),
15485796c8dcSSimon Schubert 					 VALUE_REGNUM (val));
15495796c8dcSSimon Schubert 	gdb_assert (regname && *regname);
15505796c8dcSSimon Schubert 
15515796c8dcSSimon Schubert 	error (_("Address requested for identifier "
15525796c8dcSSimon Schubert 		 "\"%s\" which is in register $%s"),
15535796c8dcSSimon Schubert 	       SYMBOL_PRINT_NAME (var), regname);
15545796c8dcSSimon Schubert 	break;
15555796c8dcSSimon Schubert       }
15565796c8dcSSimon Schubert 
15575796c8dcSSimon Schubert     default:
15585796c8dcSSimon Schubert       error (_("Can't take address of \"%s\" which isn't an lvalue."),
15595796c8dcSSimon Schubert 	     SYMBOL_PRINT_NAME (var));
15605796c8dcSSimon Schubert       break;
15615796c8dcSSimon Schubert     }
15625796c8dcSSimon Schubert 
15635796c8dcSSimon Schubert   return val;
15645796c8dcSSimon Schubert }
15655796c8dcSSimon Schubert 
15665796c8dcSSimon Schubert /* Return one if VAL does not live in target memory, but should in order
15675796c8dcSSimon Schubert    to operate on it.  Otherwise return zero.  */
15685796c8dcSSimon Schubert 
15695796c8dcSSimon Schubert int
15705796c8dcSSimon Schubert value_must_coerce_to_target (struct value *val)
15715796c8dcSSimon Schubert {
15725796c8dcSSimon Schubert   struct type *valtype;
15735796c8dcSSimon Schubert 
15745796c8dcSSimon Schubert   /* The only lval kinds which do not live in target memory.  */
15755796c8dcSSimon Schubert   if (VALUE_LVAL (val) != not_lval
15765796c8dcSSimon Schubert       && VALUE_LVAL (val) != lval_internalvar)
15775796c8dcSSimon Schubert     return 0;
15785796c8dcSSimon Schubert 
15795796c8dcSSimon Schubert   valtype = check_typedef (value_type (val));
15805796c8dcSSimon Schubert 
15815796c8dcSSimon Schubert   switch (TYPE_CODE (valtype))
15825796c8dcSSimon Schubert     {
15835796c8dcSSimon Schubert     case TYPE_CODE_ARRAY:
1584*c50c785cSJohn Marino       return TYPE_VECTOR (valtype) ? 0 : 1;
15855796c8dcSSimon Schubert     case TYPE_CODE_STRING:
15865796c8dcSSimon Schubert       return 1;
15875796c8dcSSimon Schubert     default:
15885796c8dcSSimon Schubert       return 0;
15895796c8dcSSimon Schubert     }
15905796c8dcSSimon Schubert }
15915796c8dcSSimon Schubert 
1592*c50c785cSJohn Marino /* Make sure that VAL lives in target memory if it's supposed to.  For
1593*c50c785cSJohn Marino    instance, strings are constructed as character arrays in GDB's
1594*c50c785cSJohn Marino    storage, and this function copies them to the target.  */
15955796c8dcSSimon Schubert 
15965796c8dcSSimon Schubert struct value *
15975796c8dcSSimon Schubert value_coerce_to_target (struct value *val)
15985796c8dcSSimon Schubert {
15995796c8dcSSimon Schubert   LONGEST length;
16005796c8dcSSimon Schubert   CORE_ADDR addr;
16015796c8dcSSimon Schubert 
16025796c8dcSSimon Schubert   if (!value_must_coerce_to_target (val))
16035796c8dcSSimon Schubert     return val;
16045796c8dcSSimon Schubert 
16055796c8dcSSimon Schubert   length = TYPE_LENGTH (check_typedef (value_type (val)));
16065796c8dcSSimon Schubert   addr = allocate_space_in_inferior (length);
16075796c8dcSSimon Schubert   write_memory (addr, value_contents (val), length);
16085796c8dcSSimon Schubert   return value_at_lazy (value_type (val), addr);
16095796c8dcSSimon Schubert }
16105796c8dcSSimon Schubert 
16115796c8dcSSimon Schubert /* Given a value which is an array, return a value which is a pointer
16125796c8dcSSimon Schubert    to its first element, regardless of whether or not the array has a
16135796c8dcSSimon Schubert    nonzero lower bound.
16145796c8dcSSimon Schubert 
16155796c8dcSSimon Schubert    FIXME: A previous comment here indicated that this routine should
16165796c8dcSSimon Schubert    be substracting the array's lower bound.  It's not clear to me that
16175796c8dcSSimon Schubert    this is correct.  Given an array subscripting operation, it would
16185796c8dcSSimon Schubert    certainly work to do the adjustment here, essentially computing:
16195796c8dcSSimon Schubert 
16205796c8dcSSimon Schubert    (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0])
16215796c8dcSSimon Schubert 
16225796c8dcSSimon Schubert    However I believe a more appropriate and logical place to account
16235796c8dcSSimon Schubert    for the lower bound is to do so in value_subscript, essentially
16245796c8dcSSimon Schubert    computing:
16255796c8dcSSimon Schubert 
16265796c8dcSSimon Schubert    (&array[0] + ((index - lowerbound) * sizeof array[0]))
16275796c8dcSSimon Schubert 
16285796c8dcSSimon Schubert    As further evidence consider what would happen with operations
16295796c8dcSSimon Schubert    other than array subscripting, where the caller would get back a
16305796c8dcSSimon Schubert    value that had an address somewhere before the actual first element
16315796c8dcSSimon Schubert    of the array, and the information about the lower bound would be
1632*c50c785cSJohn Marino    lost because of the coercion to pointer type.  */
16335796c8dcSSimon Schubert 
16345796c8dcSSimon Schubert struct value *
16355796c8dcSSimon Schubert value_coerce_array (struct value *arg1)
16365796c8dcSSimon Schubert {
16375796c8dcSSimon Schubert   struct type *type = check_typedef (value_type (arg1));
16385796c8dcSSimon Schubert 
16395796c8dcSSimon Schubert   /* If the user tries to do something requiring a pointer with an
16405796c8dcSSimon Schubert      array that has not yet been pushed to the target, then this would
16415796c8dcSSimon Schubert      be a good time to do so.  */
16425796c8dcSSimon Schubert   arg1 = value_coerce_to_target (arg1);
16435796c8dcSSimon Schubert 
16445796c8dcSSimon Schubert   if (VALUE_LVAL (arg1) != lval_memory)
16455796c8dcSSimon Schubert     error (_("Attempt to take address of value not located in memory."));
16465796c8dcSSimon Schubert 
16475796c8dcSSimon Schubert   return value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
16485796c8dcSSimon Schubert 			     value_address (arg1));
16495796c8dcSSimon Schubert }
16505796c8dcSSimon Schubert 
16515796c8dcSSimon Schubert /* Given a value which is a function, return a value which is a pointer
16525796c8dcSSimon Schubert    to it.  */
16535796c8dcSSimon Schubert 
16545796c8dcSSimon Schubert struct value *
16555796c8dcSSimon Schubert value_coerce_function (struct value *arg1)
16565796c8dcSSimon Schubert {
16575796c8dcSSimon Schubert   struct value *retval;
16585796c8dcSSimon Schubert 
16595796c8dcSSimon Schubert   if (VALUE_LVAL (arg1) != lval_memory)
16605796c8dcSSimon Schubert     error (_("Attempt to take address of value not located in memory."));
16615796c8dcSSimon Schubert 
16625796c8dcSSimon Schubert   retval = value_from_pointer (lookup_pointer_type (value_type (arg1)),
16635796c8dcSSimon Schubert 			       value_address (arg1));
16645796c8dcSSimon Schubert   return retval;
16655796c8dcSSimon Schubert }
16665796c8dcSSimon Schubert 
16675796c8dcSSimon Schubert /* Return a pointer value for the object for which ARG1 is the
16685796c8dcSSimon Schubert    contents.  */
16695796c8dcSSimon Schubert 
16705796c8dcSSimon Schubert struct value *
16715796c8dcSSimon Schubert value_addr (struct value *arg1)
16725796c8dcSSimon Schubert {
16735796c8dcSSimon Schubert   struct value *arg2;
16745796c8dcSSimon Schubert   struct type *type = check_typedef (value_type (arg1));
1675cf7f2e2dSJohn Marino 
16765796c8dcSSimon Schubert   if (TYPE_CODE (type) == TYPE_CODE_REF)
16775796c8dcSSimon Schubert     {
16785796c8dcSSimon Schubert       /* Copy the value, but change the type from (T&) to (T*).  We
16795796c8dcSSimon Schubert          keep the same location information, which is efficient, and
16805796c8dcSSimon Schubert          allows &(&X) to get the location containing the reference.  */
16815796c8dcSSimon Schubert       arg2 = value_copy (arg1);
16825796c8dcSSimon Schubert       deprecated_set_value_type (arg2,
16835796c8dcSSimon Schubert 				 lookup_pointer_type (TYPE_TARGET_TYPE (type)));
16845796c8dcSSimon Schubert       return arg2;
16855796c8dcSSimon Schubert     }
16865796c8dcSSimon Schubert   if (TYPE_CODE (type) == TYPE_CODE_FUNC)
16875796c8dcSSimon Schubert     return value_coerce_function (arg1);
16885796c8dcSSimon Schubert 
16895796c8dcSSimon Schubert   /* If this is an array that has not yet been pushed to the target,
16905796c8dcSSimon Schubert      then this would be a good time to force it to memory.  */
16915796c8dcSSimon Schubert   arg1 = value_coerce_to_target (arg1);
16925796c8dcSSimon Schubert 
16935796c8dcSSimon Schubert   if (VALUE_LVAL (arg1) != lval_memory)
16945796c8dcSSimon Schubert     error (_("Attempt to take address of value not located in memory."));
16955796c8dcSSimon Schubert 
1696*c50c785cSJohn Marino   /* Get target memory address.  */
16975796c8dcSSimon Schubert   arg2 = value_from_pointer (lookup_pointer_type (value_type (arg1)),
16985796c8dcSSimon Schubert 			     (value_address (arg1)
16995796c8dcSSimon Schubert 			      + value_embedded_offset (arg1)));
17005796c8dcSSimon Schubert 
17015796c8dcSSimon Schubert   /* This may be a pointer to a base subobject; so remember the
17025796c8dcSSimon Schubert      full derived object's type ...  */
1703*c50c785cSJohn Marino   set_value_enclosing_type (arg2,
1704*c50c785cSJohn Marino 			    lookup_pointer_type (value_enclosing_type (arg1)));
17055796c8dcSSimon Schubert   /* ... and also the relative position of the subobject in the full
17065796c8dcSSimon Schubert      object.  */
17075796c8dcSSimon Schubert   set_value_pointed_to_offset (arg2, value_embedded_offset (arg1));
17085796c8dcSSimon Schubert   return arg2;
17095796c8dcSSimon Schubert }
17105796c8dcSSimon Schubert 
17115796c8dcSSimon Schubert /* Return a reference value for the object for which ARG1 is the
17125796c8dcSSimon Schubert    contents.  */
17135796c8dcSSimon Schubert 
17145796c8dcSSimon Schubert struct value *
17155796c8dcSSimon Schubert value_ref (struct value *arg1)
17165796c8dcSSimon Schubert {
17175796c8dcSSimon Schubert   struct value *arg2;
17185796c8dcSSimon Schubert   struct type *type = check_typedef (value_type (arg1));
1719cf7f2e2dSJohn Marino 
17205796c8dcSSimon Schubert   if (TYPE_CODE (type) == TYPE_CODE_REF)
17215796c8dcSSimon Schubert     return arg1;
17225796c8dcSSimon Schubert 
17235796c8dcSSimon Schubert   arg2 = value_addr (arg1);
17245796c8dcSSimon Schubert   deprecated_set_value_type (arg2, lookup_reference_type (type));
17255796c8dcSSimon Schubert   return arg2;
17265796c8dcSSimon Schubert }
17275796c8dcSSimon Schubert 
17285796c8dcSSimon Schubert /* Given a value of a pointer type, apply the C unary * operator to
17295796c8dcSSimon Schubert    it.  */
17305796c8dcSSimon Schubert 
17315796c8dcSSimon Schubert struct value *
17325796c8dcSSimon Schubert value_ind (struct value *arg1)
17335796c8dcSSimon Schubert {
17345796c8dcSSimon Schubert   struct type *base_type;
17355796c8dcSSimon Schubert   struct value *arg2;
17365796c8dcSSimon Schubert 
17375796c8dcSSimon Schubert   arg1 = coerce_array (arg1);
17385796c8dcSSimon Schubert 
17395796c8dcSSimon Schubert   base_type = check_typedef (value_type (arg1));
17405796c8dcSSimon Schubert 
1741*c50c785cSJohn Marino   if (VALUE_LVAL (arg1) == lval_computed)
1742*c50c785cSJohn Marino     {
1743*c50c785cSJohn Marino       struct lval_funcs *funcs = value_computed_funcs (arg1);
1744*c50c785cSJohn Marino 
1745*c50c785cSJohn Marino       if (funcs->indirect)
1746*c50c785cSJohn Marino 	{
1747*c50c785cSJohn Marino 	  struct value *result = funcs->indirect (arg1);
1748*c50c785cSJohn Marino 
1749*c50c785cSJohn Marino 	  if (result)
1750*c50c785cSJohn Marino 	    return result;
1751*c50c785cSJohn Marino 	}
1752*c50c785cSJohn Marino     }
1753*c50c785cSJohn Marino 
17545796c8dcSSimon Schubert   if (TYPE_CODE (base_type) == TYPE_CODE_PTR)
17555796c8dcSSimon Schubert     {
17565796c8dcSSimon Schubert       struct type *enc_type;
1757cf7f2e2dSJohn Marino 
17585796c8dcSSimon Schubert       /* We may be pointing to something embedded in a larger object.
17595796c8dcSSimon Schubert          Get the real type of the enclosing object.  */
17605796c8dcSSimon Schubert       enc_type = check_typedef (value_enclosing_type (arg1));
17615796c8dcSSimon Schubert       enc_type = TYPE_TARGET_TYPE (enc_type);
17625796c8dcSSimon Schubert 
17635796c8dcSSimon Schubert       if (TYPE_CODE (check_typedef (enc_type)) == TYPE_CODE_FUNC
17645796c8dcSSimon Schubert 	  || TYPE_CODE (check_typedef (enc_type)) == TYPE_CODE_METHOD)
17655796c8dcSSimon Schubert 	/* For functions, go through find_function_addr, which knows
17665796c8dcSSimon Schubert 	   how to handle function descriptors.  */
17675796c8dcSSimon Schubert 	arg2 = value_at_lazy (enc_type,
17685796c8dcSSimon Schubert 			      find_function_addr (arg1, NULL));
17695796c8dcSSimon Schubert       else
1770*c50c785cSJohn Marino 	/* Retrieve the enclosing object pointed to.  */
17715796c8dcSSimon Schubert 	arg2 = value_at_lazy (enc_type,
17725796c8dcSSimon Schubert 			      (value_as_address (arg1)
17735796c8dcSSimon Schubert 			       - value_pointed_to_offset (arg1)));
17745796c8dcSSimon Schubert 
17755796c8dcSSimon Schubert       /* Re-adjust type.  */
17765796c8dcSSimon Schubert       deprecated_set_value_type (arg2, TYPE_TARGET_TYPE (base_type));
17775796c8dcSSimon Schubert       /* Add embedding info.  */
1778*c50c785cSJohn Marino       set_value_enclosing_type (arg2, enc_type);
17795796c8dcSSimon Schubert       set_value_embedded_offset (arg2, value_pointed_to_offset (arg1));
17805796c8dcSSimon Schubert 
17815796c8dcSSimon Schubert       /* We may be pointing to an object of some derived type.  */
17825796c8dcSSimon Schubert       arg2 = value_full_object (arg2, NULL, 0, 0, 0);
17835796c8dcSSimon Schubert       return arg2;
17845796c8dcSSimon Schubert     }
17855796c8dcSSimon Schubert 
17865796c8dcSSimon Schubert   error (_("Attempt to take contents of a non-pointer value."));
17875796c8dcSSimon Schubert   return 0;			/* For lint -- never reached.  */
17885796c8dcSSimon Schubert }
17895796c8dcSSimon Schubert 
1790*c50c785cSJohn Marino /* Create a value for an array by allocating space in GDB, copying the
1791*c50c785cSJohn Marino    data into that space, and then setting up an array value.
17925796c8dcSSimon Schubert 
17935796c8dcSSimon Schubert    The array bounds are set from LOWBOUND and HIGHBOUND, and the array
17945796c8dcSSimon Schubert    is populated from the values passed in ELEMVEC.
17955796c8dcSSimon Schubert 
17965796c8dcSSimon Schubert    The element type of the array is inherited from the type of the
17975796c8dcSSimon Schubert    first element, and all elements must have the same size (though we
17985796c8dcSSimon Schubert    don't currently enforce any restriction on their types).  */
17995796c8dcSSimon Schubert 
18005796c8dcSSimon Schubert struct value *
18015796c8dcSSimon Schubert value_array (int lowbound, int highbound, struct value **elemvec)
18025796c8dcSSimon Schubert {
18035796c8dcSSimon Schubert   int nelem;
18045796c8dcSSimon Schubert   int idx;
18055796c8dcSSimon Schubert   unsigned int typelength;
18065796c8dcSSimon Schubert   struct value *val;
18075796c8dcSSimon Schubert   struct type *arraytype;
18085796c8dcSSimon Schubert 
18095796c8dcSSimon Schubert   /* Validate that the bounds are reasonable and that each of the
18105796c8dcSSimon Schubert      elements have the same size.  */
18115796c8dcSSimon Schubert 
18125796c8dcSSimon Schubert   nelem = highbound - lowbound + 1;
18135796c8dcSSimon Schubert   if (nelem <= 0)
18145796c8dcSSimon Schubert     {
18155796c8dcSSimon Schubert       error (_("bad array bounds (%d, %d)"), lowbound, highbound);
18165796c8dcSSimon Schubert     }
18175796c8dcSSimon Schubert   typelength = TYPE_LENGTH (value_enclosing_type (elemvec[0]));
18185796c8dcSSimon Schubert   for (idx = 1; idx < nelem; idx++)
18195796c8dcSSimon Schubert     {
18205796c8dcSSimon Schubert       if (TYPE_LENGTH (value_enclosing_type (elemvec[idx])) != typelength)
18215796c8dcSSimon Schubert 	{
18225796c8dcSSimon Schubert 	  error (_("array elements must all be the same size"));
18235796c8dcSSimon Schubert 	}
18245796c8dcSSimon Schubert     }
18255796c8dcSSimon Schubert 
18265796c8dcSSimon Schubert   arraytype = lookup_array_range_type (value_enclosing_type (elemvec[0]),
18275796c8dcSSimon Schubert 				       lowbound, highbound);
18285796c8dcSSimon Schubert 
18295796c8dcSSimon Schubert   if (!current_language->c_style_arrays)
18305796c8dcSSimon Schubert     {
18315796c8dcSSimon Schubert       val = allocate_value (arraytype);
18325796c8dcSSimon Schubert       for (idx = 0; idx < nelem; idx++)
1833*c50c785cSJohn Marino 	value_contents_copy (val, idx * typelength, elemvec[idx], 0,
18345796c8dcSSimon Schubert 			     typelength);
18355796c8dcSSimon Schubert       return val;
18365796c8dcSSimon Schubert     }
18375796c8dcSSimon Schubert 
18385796c8dcSSimon Schubert   /* Allocate space to store the array, and then initialize it by
18395796c8dcSSimon Schubert      copying in each element.  */
18405796c8dcSSimon Schubert 
18415796c8dcSSimon Schubert   val = allocate_value (arraytype);
18425796c8dcSSimon Schubert   for (idx = 0; idx < nelem; idx++)
1843*c50c785cSJohn Marino     value_contents_copy (val, idx * typelength, elemvec[idx], 0, typelength);
18445796c8dcSSimon Schubert   return val;
18455796c8dcSSimon Schubert }
18465796c8dcSSimon Schubert 
18475796c8dcSSimon Schubert struct value *
18485796c8dcSSimon Schubert value_cstring (char *ptr, int len, struct type *char_type)
18495796c8dcSSimon Schubert {
18505796c8dcSSimon Schubert   struct value *val;
18515796c8dcSSimon Schubert   int lowbound = current_language->string_lower_bound;
18525796c8dcSSimon Schubert   int highbound = len / TYPE_LENGTH (char_type);
18535796c8dcSSimon Schubert   struct type *stringtype
18545796c8dcSSimon Schubert     = lookup_array_range_type (char_type, lowbound, highbound + lowbound - 1);
18555796c8dcSSimon Schubert 
18565796c8dcSSimon Schubert   val = allocate_value (stringtype);
18575796c8dcSSimon Schubert   memcpy (value_contents_raw (val), ptr, len);
18585796c8dcSSimon Schubert   return val;
18595796c8dcSSimon Schubert }
18605796c8dcSSimon Schubert 
18615796c8dcSSimon Schubert /* Create a value for a string constant by allocating space in the
18625796c8dcSSimon Schubert    inferior, copying the data into that space, and returning the
18635796c8dcSSimon Schubert    address with type TYPE_CODE_STRING.  PTR points to the string
18645796c8dcSSimon Schubert    constant data; LEN is number of characters.
18655796c8dcSSimon Schubert 
18665796c8dcSSimon Schubert    Note that string types are like array of char types with a lower
18675796c8dcSSimon Schubert    bound of zero and an upper bound of LEN - 1.  Also note that the
18685796c8dcSSimon Schubert    string may contain embedded null bytes.  */
18695796c8dcSSimon Schubert 
18705796c8dcSSimon Schubert struct value *
18715796c8dcSSimon Schubert value_string (char *ptr, int len, struct type *char_type)
18725796c8dcSSimon Schubert {
18735796c8dcSSimon Schubert   struct value *val;
18745796c8dcSSimon Schubert   int lowbound = current_language->string_lower_bound;
18755796c8dcSSimon Schubert   int highbound = len / TYPE_LENGTH (char_type);
18765796c8dcSSimon Schubert   struct type *stringtype
18775796c8dcSSimon Schubert     = lookup_string_range_type (char_type, lowbound, highbound + lowbound - 1);
18785796c8dcSSimon Schubert 
18795796c8dcSSimon Schubert   val = allocate_value (stringtype);
18805796c8dcSSimon Schubert   memcpy (value_contents_raw (val), ptr, len);
18815796c8dcSSimon Schubert   return val;
18825796c8dcSSimon Schubert }
18835796c8dcSSimon Schubert 
18845796c8dcSSimon Schubert struct value *
18855796c8dcSSimon Schubert value_bitstring (char *ptr, int len, struct type *index_type)
18865796c8dcSSimon Schubert {
18875796c8dcSSimon Schubert   struct value *val;
18885796c8dcSSimon Schubert   struct type *domain_type
18895796c8dcSSimon Schubert     = create_range_type (NULL, index_type, 0, len - 1);
18905796c8dcSSimon Schubert   struct type *type = create_set_type (NULL, domain_type);
1891cf7f2e2dSJohn Marino 
18925796c8dcSSimon Schubert   TYPE_CODE (type) = TYPE_CODE_BITSTRING;
18935796c8dcSSimon Schubert   val = allocate_value (type);
18945796c8dcSSimon Schubert   memcpy (value_contents_raw (val), ptr, TYPE_LENGTH (type));
18955796c8dcSSimon Schubert   return val;
18965796c8dcSSimon Schubert }
18975796c8dcSSimon Schubert 
18985796c8dcSSimon Schubert /* See if we can pass arguments in T2 to a function which takes
18995796c8dcSSimon Schubert    arguments of types T1.  T1 is a list of NARGS arguments, and T2 is
19005796c8dcSSimon Schubert    a NULL-terminated vector.  If some arguments need coercion of some
19015796c8dcSSimon Schubert    sort, then the coerced values are written into T2.  Return value is
19025796c8dcSSimon Schubert    0 if the arguments could be matched, or the position at which they
19035796c8dcSSimon Schubert    differ if not.
19045796c8dcSSimon Schubert 
19055796c8dcSSimon Schubert    STATICP is nonzero if the T1 argument list came from a static
19065796c8dcSSimon Schubert    member function.  T2 will still include the ``this'' pointer, but
19075796c8dcSSimon Schubert    it will be skipped.
19085796c8dcSSimon Schubert 
19095796c8dcSSimon Schubert    For non-static member functions, we ignore the first argument,
19105796c8dcSSimon Schubert    which is the type of the instance variable.  This is because we
19115796c8dcSSimon Schubert    want to handle calls with objects from derived classes.  This is
19125796c8dcSSimon Schubert    not entirely correct: we should actually check to make sure that a
19135796c8dcSSimon Schubert    requested operation is type secure, shouldn't we?  FIXME.  */
19145796c8dcSSimon Schubert 
19155796c8dcSSimon Schubert static int
19165796c8dcSSimon Schubert typecmp (int staticp, int varargs, int nargs,
19175796c8dcSSimon Schubert 	 struct field t1[], struct value *t2[])
19185796c8dcSSimon Schubert {
19195796c8dcSSimon Schubert   int i;
19205796c8dcSSimon Schubert 
19215796c8dcSSimon Schubert   if (t2 == 0)
19225796c8dcSSimon Schubert     internal_error (__FILE__, __LINE__,
19235796c8dcSSimon Schubert 		    _("typecmp: no argument list"));
19245796c8dcSSimon Schubert 
19255796c8dcSSimon Schubert   /* Skip ``this'' argument if applicable.  T2 will always include
19265796c8dcSSimon Schubert      THIS.  */
19275796c8dcSSimon Schubert   if (staticp)
19285796c8dcSSimon Schubert     t2 ++;
19295796c8dcSSimon Schubert 
19305796c8dcSSimon Schubert   for (i = 0;
19315796c8dcSSimon Schubert        (i < nargs) && TYPE_CODE (t1[i].type) != TYPE_CODE_VOID;
19325796c8dcSSimon Schubert        i++)
19335796c8dcSSimon Schubert     {
19345796c8dcSSimon Schubert       struct type *tt1, *tt2;
19355796c8dcSSimon Schubert 
19365796c8dcSSimon Schubert       if (!t2[i])
19375796c8dcSSimon Schubert 	return i + 1;
19385796c8dcSSimon Schubert 
19395796c8dcSSimon Schubert       tt1 = check_typedef (t1[i].type);
19405796c8dcSSimon Schubert       tt2 = check_typedef (value_type (t2[i]));
19415796c8dcSSimon Schubert 
19425796c8dcSSimon Schubert       if (TYPE_CODE (tt1) == TYPE_CODE_REF
19435796c8dcSSimon Schubert       /* We should be doing hairy argument matching, as below.  */
1944*c50c785cSJohn Marino 	  && (TYPE_CODE (check_typedef (TYPE_TARGET_TYPE (tt1)))
1945*c50c785cSJohn Marino 	      == TYPE_CODE (tt2)))
19465796c8dcSSimon Schubert 	{
19475796c8dcSSimon Schubert 	  if (TYPE_CODE (tt2) == TYPE_CODE_ARRAY)
19485796c8dcSSimon Schubert 	    t2[i] = value_coerce_array (t2[i]);
19495796c8dcSSimon Schubert 	  else
19505796c8dcSSimon Schubert 	    t2[i] = value_ref (t2[i]);
19515796c8dcSSimon Schubert 	  continue;
19525796c8dcSSimon Schubert 	}
19535796c8dcSSimon Schubert 
19545796c8dcSSimon Schubert       /* djb - 20000715 - Until the new type structure is in the
19555796c8dcSSimon Schubert 	 place, and we can attempt things like implicit conversions,
19565796c8dcSSimon Schubert 	 we need to do this so you can take something like a map<const
19575796c8dcSSimon Schubert 	 char *>, and properly access map["hello"], because the
19585796c8dcSSimon Schubert 	 argument to [] will be a reference to a pointer to a char,
19595796c8dcSSimon Schubert 	 and the argument will be a pointer to a char.  */
19605796c8dcSSimon Schubert       while (TYPE_CODE(tt1) == TYPE_CODE_REF
19615796c8dcSSimon Schubert 	     || TYPE_CODE (tt1) == TYPE_CODE_PTR)
19625796c8dcSSimon Schubert 	{
19635796c8dcSSimon Schubert 	  tt1 = check_typedef( TYPE_TARGET_TYPE(tt1) );
19645796c8dcSSimon Schubert 	}
19655796c8dcSSimon Schubert       while (TYPE_CODE(tt2) == TYPE_CODE_ARRAY
19665796c8dcSSimon Schubert 	     || TYPE_CODE(tt2) == TYPE_CODE_PTR
19675796c8dcSSimon Schubert 	     || TYPE_CODE(tt2) == TYPE_CODE_REF)
19685796c8dcSSimon Schubert 	{
19695796c8dcSSimon Schubert 	  tt2 = check_typedef (TYPE_TARGET_TYPE(tt2));
19705796c8dcSSimon Schubert 	}
19715796c8dcSSimon Schubert       if (TYPE_CODE (tt1) == TYPE_CODE (tt2))
19725796c8dcSSimon Schubert 	continue;
19735796c8dcSSimon Schubert       /* Array to pointer is a `trivial conversion' according to the
19745796c8dcSSimon Schubert 	 ARM.  */
19755796c8dcSSimon Schubert 
19765796c8dcSSimon Schubert       /* We should be doing much hairier argument matching (see
19775796c8dcSSimon Schubert          section 13.2 of the ARM), but as a quick kludge, just check
19785796c8dcSSimon Schubert          for the same type code.  */
19795796c8dcSSimon Schubert       if (TYPE_CODE (t1[i].type) != TYPE_CODE (value_type (t2[i])))
19805796c8dcSSimon Schubert 	return i + 1;
19815796c8dcSSimon Schubert     }
19825796c8dcSSimon Schubert   if (varargs || t2[i] == NULL)
19835796c8dcSSimon Schubert     return 0;
19845796c8dcSSimon Schubert   return i + 1;
19855796c8dcSSimon Schubert }
19865796c8dcSSimon Schubert 
19875796c8dcSSimon Schubert /* Helper function used by value_struct_elt to recurse through
19885796c8dcSSimon Schubert    baseclasses.  Look for a field NAME in ARG1.  Adjust the address of
19895796c8dcSSimon Schubert    ARG1 by OFFSET bytes, and search in it assuming it has (class) type
19905796c8dcSSimon Schubert    TYPE.  If found, return value, else return NULL.
19915796c8dcSSimon Schubert 
19925796c8dcSSimon Schubert    If LOOKING_FOR_BASECLASS, then instead of looking for struct
19935796c8dcSSimon Schubert    fields, look for a baseclass named NAME.  */
19945796c8dcSSimon Schubert 
19955796c8dcSSimon Schubert static struct value *
1996cf7f2e2dSJohn Marino search_struct_field (const char *name, struct value *arg1, int offset,
19975796c8dcSSimon Schubert 		     struct type *type, int looking_for_baseclass)
19985796c8dcSSimon Schubert {
19995796c8dcSSimon Schubert   int i;
2000cf7f2e2dSJohn Marino   int nbases;
20015796c8dcSSimon Schubert 
20025796c8dcSSimon Schubert   CHECK_TYPEDEF (type);
2003cf7f2e2dSJohn Marino   nbases = TYPE_N_BASECLASSES (type);
20045796c8dcSSimon Schubert 
20055796c8dcSSimon Schubert   if (!looking_for_baseclass)
20065796c8dcSSimon Schubert     for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
20075796c8dcSSimon Schubert       {
20085796c8dcSSimon Schubert 	char *t_field_name = TYPE_FIELD_NAME (type, i);
20095796c8dcSSimon Schubert 
20105796c8dcSSimon Schubert 	if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
20115796c8dcSSimon Schubert 	  {
20125796c8dcSSimon Schubert 	    struct value *v;
2013cf7f2e2dSJohn Marino 
20145796c8dcSSimon Schubert 	    if (field_is_static (&TYPE_FIELD (type, i)))
20155796c8dcSSimon Schubert 	      {
20165796c8dcSSimon Schubert 		v = value_static_field (type, i);
20175796c8dcSSimon Schubert 		if (v == 0)
2018*c50c785cSJohn Marino 		  error (_("field %s is nonexistent or "
2019*c50c785cSJohn Marino 			   "has been optimized out"),
20205796c8dcSSimon Schubert 			 name);
20215796c8dcSSimon Schubert 	      }
20225796c8dcSSimon Schubert 	    else
20235796c8dcSSimon Schubert 	      {
20245796c8dcSSimon Schubert 		v = value_primitive_field (arg1, offset, i, type);
20255796c8dcSSimon Schubert 		if (v == 0)
20265796c8dcSSimon Schubert 		  error (_("there is no field named %s"), name);
20275796c8dcSSimon Schubert 	      }
20285796c8dcSSimon Schubert 	    return v;
20295796c8dcSSimon Schubert 	  }
20305796c8dcSSimon Schubert 
20315796c8dcSSimon Schubert 	if (t_field_name
20325796c8dcSSimon Schubert 	    && (t_field_name[0] == '\0'
20335796c8dcSSimon Schubert 		|| (TYPE_CODE (type) == TYPE_CODE_UNION
20345796c8dcSSimon Schubert 		    && (strcmp_iw (t_field_name, "else") == 0))))
20355796c8dcSSimon Schubert 	  {
20365796c8dcSSimon Schubert 	    struct type *field_type = TYPE_FIELD_TYPE (type, i);
2037cf7f2e2dSJohn Marino 
20385796c8dcSSimon Schubert 	    if (TYPE_CODE (field_type) == TYPE_CODE_UNION
20395796c8dcSSimon Schubert 		|| TYPE_CODE (field_type) == TYPE_CODE_STRUCT)
20405796c8dcSSimon Schubert 	      {
20415796c8dcSSimon Schubert 		/* Look for a match through the fields of an anonymous
20425796c8dcSSimon Schubert 		   union, or anonymous struct.  C++ provides anonymous
20435796c8dcSSimon Schubert 		   unions.
20445796c8dcSSimon Schubert 
20455796c8dcSSimon Schubert 		   In the GNU Chill (now deleted from GDB)
20465796c8dcSSimon Schubert 		   implementation of variant record types, each
20475796c8dcSSimon Schubert 		   <alternative field> has an (anonymous) union type,
20485796c8dcSSimon Schubert 		   each member of the union represents a <variant
20495796c8dcSSimon Schubert 		   alternative>.  Each <variant alternative> is
20505796c8dcSSimon Schubert 		   represented as a struct, with a member for each
20515796c8dcSSimon Schubert 		   <variant field>.  */
20525796c8dcSSimon Schubert 
20535796c8dcSSimon Schubert 		struct value *v;
20545796c8dcSSimon Schubert 		int new_offset = offset;
20555796c8dcSSimon Schubert 
20565796c8dcSSimon Schubert 		/* This is pretty gross.  In G++, the offset in an
20575796c8dcSSimon Schubert 		   anonymous union is relative to the beginning of the
20585796c8dcSSimon Schubert 		   enclosing struct.  In the GNU Chill (now deleted
20595796c8dcSSimon Schubert 		   from GDB) implementation of variant records, the
20605796c8dcSSimon Schubert 		   bitpos is zero in an anonymous union field, so we
20615796c8dcSSimon Schubert 		   have to add the offset of the union here.  */
20625796c8dcSSimon Schubert 		if (TYPE_CODE (field_type) == TYPE_CODE_STRUCT
20635796c8dcSSimon Schubert 		    || (TYPE_NFIELDS (field_type) > 0
20645796c8dcSSimon Schubert 			&& TYPE_FIELD_BITPOS (field_type, 0) == 0))
20655796c8dcSSimon Schubert 		  new_offset += TYPE_FIELD_BITPOS (type, i) / 8;
20665796c8dcSSimon Schubert 
20675796c8dcSSimon Schubert 		v = search_struct_field (name, arg1, new_offset,
20685796c8dcSSimon Schubert 					 field_type,
20695796c8dcSSimon Schubert 					 looking_for_baseclass);
20705796c8dcSSimon Schubert 		if (v)
20715796c8dcSSimon Schubert 		  return v;
20725796c8dcSSimon Schubert 	      }
20735796c8dcSSimon Schubert 	  }
20745796c8dcSSimon Schubert       }
20755796c8dcSSimon Schubert 
20765796c8dcSSimon Schubert   for (i = 0; i < nbases; i++)
20775796c8dcSSimon Schubert     {
20785796c8dcSSimon Schubert       struct value *v;
20795796c8dcSSimon Schubert       struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
20805796c8dcSSimon Schubert       /* If we are looking for baseclasses, this is what we get when
20815796c8dcSSimon Schubert          we hit them.  But it could happen that the base part's member
20825796c8dcSSimon Schubert          name is not yet filled in.  */
20835796c8dcSSimon Schubert       int found_baseclass = (looking_for_baseclass
20845796c8dcSSimon Schubert 			     && TYPE_BASECLASS_NAME (type, i) != NULL
20855796c8dcSSimon Schubert 			     && (strcmp_iw (name,
20865796c8dcSSimon Schubert 					    TYPE_BASECLASS_NAME (type,
20875796c8dcSSimon Schubert 								 i)) == 0));
20885796c8dcSSimon Schubert 
20895796c8dcSSimon Schubert       if (BASETYPE_VIA_VIRTUAL (type, i))
20905796c8dcSSimon Schubert 	{
20915796c8dcSSimon Schubert 	  int boffset;
20925796c8dcSSimon Schubert 	  struct value *v2;
20935796c8dcSSimon Schubert 
20945796c8dcSSimon Schubert 	  boffset = baseclass_offset (type, i,
2095*c50c785cSJohn Marino 				      value_contents_for_printing (arg1),
2096*c50c785cSJohn Marino 				      value_embedded_offset (arg1) + offset,
2097*c50c785cSJohn Marino 				      value_address (arg1),
2098*c50c785cSJohn Marino 				      arg1);
20995796c8dcSSimon Schubert 
21005796c8dcSSimon Schubert 	  /* The virtual base class pointer might have been clobbered
21015796c8dcSSimon Schubert 	     by the user program.  Make sure that it still points to a
21025796c8dcSSimon Schubert 	     valid memory location.  */
21035796c8dcSSimon Schubert 
2104cf7f2e2dSJohn Marino 	  boffset += value_embedded_offset (arg1) + offset;
2105cf7f2e2dSJohn Marino 	  if (boffset < 0
2106cf7f2e2dSJohn Marino 	      || boffset >= TYPE_LENGTH (value_enclosing_type (arg1)))
21075796c8dcSSimon Schubert 	    {
21085796c8dcSSimon Schubert 	      CORE_ADDR base_addr;
21095796c8dcSSimon Schubert 
21105796c8dcSSimon Schubert 	      v2  = allocate_value (basetype);
21115796c8dcSSimon Schubert 	      base_addr = value_address (arg1) + boffset;
21125796c8dcSSimon Schubert 	      if (target_read_memory (base_addr,
21135796c8dcSSimon Schubert 				      value_contents_raw (v2),
21145796c8dcSSimon Schubert 				      TYPE_LENGTH (basetype)) != 0)
21155796c8dcSSimon Schubert 		error (_("virtual baseclass botch"));
21165796c8dcSSimon Schubert 	      VALUE_LVAL (v2) = lval_memory;
21175796c8dcSSimon Schubert 	      set_value_address (v2, base_addr);
21185796c8dcSSimon Schubert 	    }
21195796c8dcSSimon Schubert 	  else
21205796c8dcSSimon Schubert 	    {
2121cf7f2e2dSJohn Marino 	      v2 = value_copy (arg1);
2122cf7f2e2dSJohn Marino 	      deprecated_set_value_type (v2, basetype);
2123cf7f2e2dSJohn Marino 	      set_value_embedded_offset (v2, boffset);
21245796c8dcSSimon Schubert 	    }
21255796c8dcSSimon Schubert 
21265796c8dcSSimon Schubert 	  if (found_baseclass)
21275796c8dcSSimon Schubert 	    return v2;
21285796c8dcSSimon Schubert 	  v = search_struct_field (name, v2, 0,
21295796c8dcSSimon Schubert 				   TYPE_BASECLASS (type, i),
21305796c8dcSSimon Schubert 				   looking_for_baseclass);
21315796c8dcSSimon Schubert 	}
21325796c8dcSSimon Schubert       else if (found_baseclass)
21335796c8dcSSimon Schubert 	v = value_primitive_field (arg1, offset, i, type);
21345796c8dcSSimon Schubert       else
21355796c8dcSSimon Schubert 	v = search_struct_field (name, arg1,
21365796c8dcSSimon Schubert 				 offset + TYPE_BASECLASS_BITPOS (type,
21375796c8dcSSimon Schubert 								 i) / 8,
21385796c8dcSSimon Schubert 				 basetype, looking_for_baseclass);
21395796c8dcSSimon Schubert       if (v)
21405796c8dcSSimon Schubert 	return v;
21415796c8dcSSimon Schubert     }
21425796c8dcSSimon Schubert   return NULL;
21435796c8dcSSimon Schubert }
21445796c8dcSSimon Schubert 
21455796c8dcSSimon Schubert /* Helper function used by value_struct_elt to recurse through
21465796c8dcSSimon Schubert    baseclasses.  Look for a field NAME in ARG1.  Adjust the address of
21475796c8dcSSimon Schubert    ARG1 by OFFSET bytes, and search in it assuming it has (class) type
21485796c8dcSSimon Schubert    TYPE.
21495796c8dcSSimon Schubert 
21505796c8dcSSimon Schubert    If found, return value, else if name matched and args not return
21515796c8dcSSimon Schubert    (value) -1, else return NULL.  */
21525796c8dcSSimon Schubert 
21535796c8dcSSimon Schubert static struct value *
2154cf7f2e2dSJohn Marino search_struct_method (const char *name, struct value **arg1p,
21555796c8dcSSimon Schubert 		      struct value **args, int offset,
21565796c8dcSSimon Schubert 		      int *static_memfuncp, struct type *type)
21575796c8dcSSimon Schubert {
21585796c8dcSSimon Schubert   int i;
21595796c8dcSSimon Schubert   struct value *v;
21605796c8dcSSimon Schubert   int name_matched = 0;
21615796c8dcSSimon Schubert   char dem_opname[64];
21625796c8dcSSimon Schubert 
21635796c8dcSSimon Schubert   CHECK_TYPEDEF (type);
21645796c8dcSSimon Schubert   for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
21655796c8dcSSimon Schubert     {
21665796c8dcSSimon Schubert       char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
2167cf7f2e2dSJohn Marino 
2168*c50c785cSJohn Marino       /* FIXME!  May need to check for ARM demangling here.  */
21695796c8dcSSimon Schubert       if (strncmp (t_field_name, "__", 2) == 0 ||
21705796c8dcSSimon Schubert 	  strncmp (t_field_name, "op", 2) == 0 ||
21715796c8dcSSimon Schubert 	  strncmp (t_field_name, "type", 4) == 0)
21725796c8dcSSimon Schubert 	{
21735796c8dcSSimon Schubert 	  if (cplus_demangle_opname (t_field_name, dem_opname, DMGL_ANSI))
21745796c8dcSSimon Schubert 	    t_field_name = dem_opname;
21755796c8dcSSimon Schubert 	  else if (cplus_demangle_opname (t_field_name, dem_opname, 0))
21765796c8dcSSimon Schubert 	    t_field_name = dem_opname;
21775796c8dcSSimon Schubert 	}
21785796c8dcSSimon Schubert       if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
21795796c8dcSSimon Schubert 	{
21805796c8dcSSimon Schubert 	  int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1;
21815796c8dcSSimon Schubert 	  struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
21825796c8dcSSimon Schubert 
2183cf7f2e2dSJohn Marino 	  name_matched = 1;
21845796c8dcSSimon Schubert 	  check_stub_method_group (type, i);
21855796c8dcSSimon Schubert 	  if (j > 0 && args == 0)
2186*c50c785cSJohn Marino 	    error (_("cannot resolve overloaded method "
2187*c50c785cSJohn Marino 		     "`%s': no arguments supplied"), name);
21885796c8dcSSimon Schubert 	  else if (j == 0 && args == 0)
21895796c8dcSSimon Schubert 	    {
21905796c8dcSSimon Schubert 	      v = value_fn_field (arg1p, f, j, type, offset);
21915796c8dcSSimon Schubert 	      if (v != NULL)
21925796c8dcSSimon Schubert 		return v;
21935796c8dcSSimon Schubert 	    }
21945796c8dcSSimon Schubert 	  else
21955796c8dcSSimon Schubert 	    while (j >= 0)
21965796c8dcSSimon Schubert 	      {
21975796c8dcSSimon Schubert 		if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j),
21985796c8dcSSimon Schubert 			      TYPE_VARARGS (TYPE_FN_FIELD_TYPE (f, j)),
21995796c8dcSSimon Schubert 			      TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f, j)),
22005796c8dcSSimon Schubert 			      TYPE_FN_FIELD_ARGS (f, j), args))
22015796c8dcSSimon Schubert 		  {
22025796c8dcSSimon Schubert 		    if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
22035796c8dcSSimon Schubert 		      return value_virtual_fn_field (arg1p, f, j,
22045796c8dcSSimon Schubert 						     type, offset);
22055796c8dcSSimon Schubert 		    if (TYPE_FN_FIELD_STATIC_P (f, j)
22065796c8dcSSimon Schubert 			&& static_memfuncp)
22075796c8dcSSimon Schubert 		      *static_memfuncp = 1;
22085796c8dcSSimon Schubert 		    v = value_fn_field (arg1p, f, j, type, offset);
22095796c8dcSSimon Schubert 		    if (v != NULL)
22105796c8dcSSimon Schubert 		      return v;
22115796c8dcSSimon Schubert 		  }
22125796c8dcSSimon Schubert 		j--;
22135796c8dcSSimon Schubert 	      }
22145796c8dcSSimon Schubert 	}
22155796c8dcSSimon Schubert     }
22165796c8dcSSimon Schubert 
22175796c8dcSSimon Schubert   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
22185796c8dcSSimon Schubert     {
22195796c8dcSSimon Schubert       int base_offset;
2220*c50c785cSJohn Marino       int skip = 0;
2221*c50c785cSJohn Marino       int this_offset;
22225796c8dcSSimon Schubert 
22235796c8dcSSimon Schubert       if (BASETYPE_VIA_VIRTUAL (type, i))
22245796c8dcSSimon Schubert 	{
22255796c8dcSSimon Schubert 	  struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
2226*c50c785cSJohn Marino 	  struct value *base_val;
22275796c8dcSSimon Schubert 	  const gdb_byte *base_valaddr;
22285796c8dcSSimon Schubert 
22295796c8dcSSimon Schubert 	  /* The virtual base class pointer might have been
22305796c8dcSSimon Schubert 	     clobbered by the user program.  Make sure that it
22315796c8dcSSimon Schubert 	    still points to a valid memory location.  */
22325796c8dcSSimon Schubert 
22335796c8dcSSimon Schubert 	  if (offset < 0 || offset >= TYPE_LENGTH (type))
22345796c8dcSSimon Schubert 	    {
22355796c8dcSSimon Schubert 	      gdb_byte *tmp = alloca (TYPE_LENGTH (baseclass));
2236*c50c785cSJohn Marino 	      CORE_ADDR address = value_address (*arg1p);
2237cf7f2e2dSJohn Marino 
2238*c50c785cSJohn Marino 	      if (target_read_memory (address + offset,
22395796c8dcSSimon Schubert 				      tmp, TYPE_LENGTH (baseclass)) != 0)
22405796c8dcSSimon Schubert 		error (_("virtual baseclass botch"));
2241*c50c785cSJohn Marino 
2242*c50c785cSJohn Marino 	      base_val = value_from_contents_and_address (baseclass,
2243*c50c785cSJohn Marino 							  tmp,
2244*c50c785cSJohn Marino 							  address + offset);
2245*c50c785cSJohn Marino 	      base_valaddr = value_contents_for_printing (base_val);
2246*c50c785cSJohn Marino 	      this_offset = 0;
22475796c8dcSSimon Schubert 	    }
22485796c8dcSSimon Schubert 	  else
2249*c50c785cSJohn Marino 	    {
2250*c50c785cSJohn Marino 	      base_val = *arg1p;
2251*c50c785cSJohn Marino 	      base_valaddr = value_contents_for_printing (*arg1p);
2252*c50c785cSJohn Marino 	      this_offset = offset;
2253*c50c785cSJohn Marino 	    }
22545796c8dcSSimon Schubert 
22555796c8dcSSimon Schubert 	  base_offset = baseclass_offset (type, i, base_valaddr,
2256*c50c785cSJohn Marino 					  this_offset, value_address (base_val),
2257*c50c785cSJohn Marino 					  base_val);
22585796c8dcSSimon Schubert 	}
22595796c8dcSSimon Schubert       else
22605796c8dcSSimon Schubert 	{
22615796c8dcSSimon Schubert 	  base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
22625796c8dcSSimon Schubert 	}
22635796c8dcSSimon Schubert       v = search_struct_method (name, arg1p, args, base_offset + offset,
22645796c8dcSSimon Schubert 				static_memfuncp, TYPE_BASECLASS (type, i));
22655796c8dcSSimon Schubert       if (v == (struct value *) - 1)
22665796c8dcSSimon Schubert 	{
22675796c8dcSSimon Schubert 	  name_matched = 1;
22685796c8dcSSimon Schubert 	}
22695796c8dcSSimon Schubert       else if (v)
22705796c8dcSSimon Schubert 	{
22715796c8dcSSimon Schubert 	  /* FIXME-bothner:  Why is this commented out?  Why is it here?  */
22725796c8dcSSimon Schubert 	  /* *arg1p = arg1_tmp; */
22735796c8dcSSimon Schubert 	  return v;
22745796c8dcSSimon Schubert 	}
22755796c8dcSSimon Schubert     }
22765796c8dcSSimon Schubert   if (name_matched)
22775796c8dcSSimon Schubert     return (struct value *) - 1;
22785796c8dcSSimon Schubert   else
22795796c8dcSSimon Schubert     return NULL;
22805796c8dcSSimon Schubert }
22815796c8dcSSimon Schubert 
22825796c8dcSSimon Schubert /* Given *ARGP, a value of type (pointer to a)* structure/union,
22835796c8dcSSimon Schubert    extract the component named NAME from the ultimate target
22845796c8dcSSimon Schubert    structure/union and return it as a value with its appropriate type.
22855796c8dcSSimon Schubert    ERR is used in the error message if *ARGP's type is wrong.
22865796c8dcSSimon Schubert 
22875796c8dcSSimon Schubert    C++: ARGS is a list of argument types to aid in the selection of
22885796c8dcSSimon Schubert    an appropriate method.  Also, handle derived types.
22895796c8dcSSimon Schubert 
22905796c8dcSSimon Schubert    STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
22915796c8dcSSimon Schubert    where the truthvalue of whether the function that was resolved was
22925796c8dcSSimon Schubert    a static member function or not is stored.
22935796c8dcSSimon Schubert 
22945796c8dcSSimon Schubert    ERR is an error message to be printed in case the field is not
22955796c8dcSSimon Schubert    found.  */
22965796c8dcSSimon Schubert 
22975796c8dcSSimon Schubert struct value *
22985796c8dcSSimon Schubert value_struct_elt (struct value **argp, struct value **args,
2299cf7f2e2dSJohn Marino 		  const char *name, int *static_memfuncp, const char *err)
23005796c8dcSSimon Schubert {
23015796c8dcSSimon Schubert   struct type *t;
23025796c8dcSSimon Schubert   struct value *v;
23035796c8dcSSimon Schubert 
23045796c8dcSSimon Schubert   *argp = coerce_array (*argp);
23055796c8dcSSimon Schubert 
23065796c8dcSSimon Schubert   t = check_typedef (value_type (*argp));
23075796c8dcSSimon Schubert 
23085796c8dcSSimon Schubert   /* Follow pointers until we get to a non-pointer.  */
23095796c8dcSSimon Schubert 
23105796c8dcSSimon Schubert   while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
23115796c8dcSSimon Schubert     {
23125796c8dcSSimon Schubert       *argp = value_ind (*argp);
23135796c8dcSSimon Schubert       /* Don't coerce fn pointer to fn and then back again!  */
23145796c8dcSSimon Schubert       if (TYPE_CODE (value_type (*argp)) != TYPE_CODE_FUNC)
23155796c8dcSSimon Schubert 	*argp = coerce_array (*argp);
23165796c8dcSSimon Schubert       t = check_typedef (value_type (*argp));
23175796c8dcSSimon Schubert     }
23185796c8dcSSimon Schubert 
23195796c8dcSSimon Schubert   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
23205796c8dcSSimon Schubert       && TYPE_CODE (t) != TYPE_CODE_UNION)
2321*c50c785cSJohn Marino     error (_("Attempt to extract a component of a value that is not a %s."),
2322*c50c785cSJohn Marino 	   err);
23235796c8dcSSimon Schubert 
23245796c8dcSSimon Schubert   /* Assume it's not, unless we see that it is.  */
23255796c8dcSSimon Schubert   if (static_memfuncp)
23265796c8dcSSimon Schubert     *static_memfuncp = 0;
23275796c8dcSSimon Schubert 
23285796c8dcSSimon Schubert   if (!args)
23295796c8dcSSimon Schubert     {
23305796c8dcSSimon Schubert       /* if there are no arguments ...do this...  */
23315796c8dcSSimon Schubert 
23325796c8dcSSimon Schubert       /* Try as a field first, because if we succeed, there is less
23335796c8dcSSimon Schubert          work to be done.  */
23345796c8dcSSimon Schubert       v = search_struct_field (name, *argp, 0, t, 0);
23355796c8dcSSimon Schubert       if (v)
23365796c8dcSSimon Schubert 	return v;
23375796c8dcSSimon Schubert 
23385796c8dcSSimon Schubert       /* C++: If it was not found as a data field, then try to
23395796c8dcSSimon Schubert          return it as a pointer to a method.  */
23405796c8dcSSimon Schubert       v = search_struct_method (name, argp, args, 0,
23415796c8dcSSimon Schubert 				static_memfuncp, t);
23425796c8dcSSimon Schubert 
23435796c8dcSSimon Schubert       if (v == (struct value *) - 1)
23445796c8dcSSimon Schubert 	error (_("Cannot take address of method %s."), name);
23455796c8dcSSimon Schubert       else if (v == 0)
23465796c8dcSSimon Schubert 	{
23475796c8dcSSimon Schubert 	  if (TYPE_NFN_FIELDS (t))
23485796c8dcSSimon Schubert 	    error (_("There is no member or method named %s."), name);
23495796c8dcSSimon Schubert 	  else
23505796c8dcSSimon Schubert 	    error (_("There is no member named %s."), name);
23515796c8dcSSimon Schubert 	}
23525796c8dcSSimon Schubert       return v;
23535796c8dcSSimon Schubert     }
23545796c8dcSSimon Schubert 
23555796c8dcSSimon Schubert     v = search_struct_method (name, argp, args, 0,
23565796c8dcSSimon Schubert 			      static_memfuncp, t);
23575796c8dcSSimon Schubert 
23585796c8dcSSimon Schubert   if (v == (struct value *) - 1)
23595796c8dcSSimon Schubert     {
2360*c50c785cSJohn Marino       error (_("One of the arguments you tried to pass to %s could not "
2361*c50c785cSJohn Marino 	       "be converted to what the function wants."), name);
23625796c8dcSSimon Schubert     }
23635796c8dcSSimon Schubert   else if (v == 0)
23645796c8dcSSimon Schubert     {
23655796c8dcSSimon Schubert       /* See if user tried to invoke data as function.  If so, hand it
23665796c8dcSSimon Schubert          back.  If it's not callable (i.e., a pointer to function),
23675796c8dcSSimon Schubert          gdb should give an error.  */
23685796c8dcSSimon Schubert       v = search_struct_field (name, *argp, 0, t, 0);
23695796c8dcSSimon Schubert       /* If we found an ordinary field, then it is not a method call.
23705796c8dcSSimon Schubert 	 So, treat it as if it were a static member function.  */
23715796c8dcSSimon Schubert       if (v && static_memfuncp)
23725796c8dcSSimon Schubert 	*static_memfuncp = 1;
23735796c8dcSSimon Schubert     }
23745796c8dcSSimon Schubert 
23755796c8dcSSimon Schubert   if (!v)
2376*c50c785cSJohn Marino     throw_error (NOT_FOUND_ERROR,
2377*c50c785cSJohn Marino                  _("Structure has no component named %s."), name);
23785796c8dcSSimon Schubert   return v;
23795796c8dcSSimon Schubert }
23805796c8dcSSimon Schubert 
23815796c8dcSSimon Schubert /* Search through the methods of an object (and its bases) to find a
23825796c8dcSSimon Schubert    specified method.  Return the pointer to the fn_field list of
23835796c8dcSSimon Schubert    overloaded instances.
23845796c8dcSSimon Schubert 
23855796c8dcSSimon Schubert    Helper function for value_find_oload_list.
23865796c8dcSSimon Schubert    ARGP is a pointer to a pointer to a value (the object).
23875796c8dcSSimon Schubert    METHOD is a string containing the method name.
23885796c8dcSSimon Schubert    OFFSET is the offset within the value.
23895796c8dcSSimon Schubert    TYPE is the assumed type of the object.
23905796c8dcSSimon Schubert    NUM_FNS is the number of overloaded instances.
23915796c8dcSSimon Schubert    BASETYPE is set to the actual type of the subobject where the
23925796c8dcSSimon Schubert       method is found.
2393*c50c785cSJohn Marino    BOFFSET is the offset of the base subobject where the method is found.  */
23945796c8dcSSimon Schubert 
23955796c8dcSSimon Schubert static struct fn_field *
2396cf7f2e2dSJohn Marino find_method_list (struct value **argp, const char *method,
23975796c8dcSSimon Schubert 		  int offset, struct type *type, int *num_fns,
23985796c8dcSSimon Schubert 		  struct type **basetype, int *boffset)
23995796c8dcSSimon Schubert {
24005796c8dcSSimon Schubert   int i;
24015796c8dcSSimon Schubert   struct fn_field *f;
24025796c8dcSSimon Schubert   CHECK_TYPEDEF (type);
24035796c8dcSSimon Schubert 
24045796c8dcSSimon Schubert   *num_fns = 0;
24055796c8dcSSimon Schubert 
24065796c8dcSSimon Schubert   /* First check in object itself.  */
24075796c8dcSSimon Schubert   for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
24085796c8dcSSimon Schubert     {
24095796c8dcSSimon Schubert       /* pai: FIXME What about operators and type conversions?  */
24105796c8dcSSimon Schubert       char *fn_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
2411cf7f2e2dSJohn Marino 
24125796c8dcSSimon Schubert       if (fn_field_name && (strcmp_iw (fn_field_name, method) == 0))
24135796c8dcSSimon Schubert 	{
24145796c8dcSSimon Schubert 	  int len = TYPE_FN_FIELDLIST_LENGTH (type, i);
24155796c8dcSSimon Schubert 	  struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
24165796c8dcSSimon Schubert 
24175796c8dcSSimon Schubert 	  *num_fns = len;
24185796c8dcSSimon Schubert 	  *basetype = type;
24195796c8dcSSimon Schubert 	  *boffset = offset;
24205796c8dcSSimon Schubert 
24215796c8dcSSimon Schubert 	  /* Resolve any stub methods.  */
24225796c8dcSSimon Schubert 	  check_stub_method_group (type, i);
24235796c8dcSSimon Schubert 
24245796c8dcSSimon Schubert 	  return f;
24255796c8dcSSimon Schubert 	}
24265796c8dcSSimon Schubert     }
24275796c8dcSSimon Schubert 
24285796c8dcSSimon Schubert   /* Not found in object, check in base subobjects.  */
24295796c8dcSSimon Schubert   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
24305796c8dcSSimon Schubert     {
24315796c8dcSSimon Schubert       int base_offset;
2432cf7f2e2dSJohn Marino 
24335796c8dcSSimon Schubert       if (BASETYPE_VIA_VIRTUAL (type, i))
24345796c8dcSSimon Schubert 	{
24355796c8dcSSimon Schubert 	  base_offset = baseclass_offset (type, i,
2436*c50c785cSJohn Marino 					  value_contents_for_printing (*argp),
2437*c50c785cSJohn Marino 					  value_offset (*argp) + offset,
2438*c50c785cSJohn Marino 					  value_address (*argp), *argp);
24395796c8dcSSimon Schubert 	}
24405796c8dcSSimon Schubert       else /* Non-virtual base, simply use bit position from debug
24415796c8dcSSimon Schubert 	      info.  */
24425796c8dcSSimon Schubert 	{
24435796c8dcSSimon Schubert 	  base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
24445796c8dcSSimon Schubert 	}
24455796c8dcSSimon Schubert       f = find_method_list (argp, method, base_offset + offset,
24465796c8dcSSimon Schubert 			    TYPE_BASECLASS (type, i), num_fns,
24475796c8dcSSimon Schubert 			    basetype, boffset);
24485796c8dcSSimon Schubert       if (f)
24495796c8dcSSimon Schubert 	return f;
24505796c8dcSSimon Schubert     }
24515796c8dcSSimon Schubert   return NULL;
24525796c8dcSSimon Schubert }
24535796c8dcSSimon Schubert 
24545796c8dcSSimon Schubert /* Return the list of overloaded methods of a specified name.
24555796c8dcSSimon Schubert 
24565796c8dcSSimon Schubert    ARGP is a pointer to a pointer to a value (the object).
24575796c8dcSSimon Schubert    METHOD is the method name.
24585796c8dcSSimon Schubert    OFFSET is the offset within the value contents.
24595796c8dcSSimon Schubert    NUM_FNS is the number of overloaded instances.
24605796c8dcSSimon Schubert    BASETYPE is set to the type of the base subobject that defines the
24615796c8dcSSimon Schubert       method.
2462*c50c785cSJohn Marino    BOFFSET is the offset of the base subobject which defines the method.  */
24635796c8dcSSimon Schubert 
24645796c8dcSSimon Schubert struct fn_field *
2465cf7f2e2dSJohn Marino value_find_oload_method_list (struct value **argp, const char *method,
24665796c8dcSSimon Schubert 			      int offset, int *num_fns,
24675796c8dcSSimon Schubert 			      struct type **basetype, int *boffset)
24685796c8dcSSimon Schubert {
24695796c8dcSSimon Schubert   struct type *t;
24705796c8dcSSimon Schubert 
24715796c8dcSSimon Schubert   t = check_typedef (value_type (*argp));
24725796c8dcSSimon Schubert 
24735796c8dcSSimon Schubert   /* Code snarfed from value_struct_elt.  */
24745796c8dcSSimon Schubert   while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
24755796c8dcSSimon Schubert     {
24765796c8dcSSimon Schubert       *argp = value_ind (*argp);
24775796c8dcSSimon Schubert       /* Don't coerce fn pointer to fn and then back again!  */
24785796c8dcSSimon Schubert       if (TYPE_CODE (value_type (*argp)) != TYPE_CODE_FUNC)
24795796c8dcSSimon Schubert 	*argp = coerce_array (*argp);
24805796c8dcSSimon Schubert       t = check_typedef (value_type (*argp));
24815796c8dcSSimon Schubert     }
24825796c8dcSSimon Schubert 
24835796c8dcSSimon Schubert   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
24845796c8dcSSimon Schubert       && TYPE_CODE (t) != TYPE_CODE_UNION)
2485*c50c785cSJohn Marino     error (_("Attempt to extract a component of a "
2486*c50c785cSJohn Marino 	     "value that is not a struct or union"));
24875796c8dcSSimon Schubert 
24885796c8dcSSimon Schubert   return find_method_list (argp, method, 0, t, num_fns,
24895796c8dcSSimon Schubert 			   basetype, boffset);
24905796c8dcSSimon Schubert }
24915796c8dcSSimon Schubert 
24925796c8dcSSimon Schubert /* Given an array of argument types (ARGTYPES) (which includes an
24935796c8dcSSimon Schubert    entry for "this" in the case of C++ methods), the number of
24945796c8dcSSimon Schubert    arguments NARGS, the NAME of a function whether it's a method or
24955796c8dcSSimon Schubert    not (METHOD), and the degree of laxness (LAX) in conforming to
24965796c8dcSSimon Schubert    overload resolution rules in ANSI C++, find the best function that
24975796c8dcSSimon Schubert    matches on the argument types according to the overload resolution
24985796c8dcSSimon Schubert    rules.
24995796c8dcSSimon Schubert 
2500cf7f2e2dSJohn Marino    METHOD can be one of three values:
2501cf7f2e2dSJohn Marino      NON_METHOD for non-member functions.
2502cf7f2e2dSJohn Marino      METHOD: for member functions.
2503cf7f2e2dSJohn Marino      BOTH: used for overload resolution of operators where the
2504cf7f2e2dSJohn Marino        candidates are expected to be either member or non member
2505cf7f2e2dSJohn Marino        functions.  In this case the first argument ARGTYPES
2506cf7f2e2dSJohn Marino        (representing 'this') is expected to be a reference to the
2507cf7f2e2dSJohn Marino        target object, and will be dereferenced when attempting the
2508cf7f2e2dSJohn Marino        non-member search.
2509cf7f2e2dSJohn Marino 
25105796c8dcSSimon Schubert    In the case of class methods, the parameter OBJ is an object value
25115796c8dcSSimon Schubert    in which to search for overloaded methods.
25125796c8dcSSimon Schubert 
25135796c8dcSSimon Schubert    In the case of non-method functions, the parameter FSYM is a symbol
25145796c8dcSSimon Schubert    corresponding to one of the overloaded functions.
25155796c8dcSSimon Schubert 
25165796c8dcSSimon Schubert    Return value is an integer: 0 -> good match, 10 -> debugger applied
25175796c8dcSSimon Schubert    non-standard coercions, 100 -> incompatible.
25185796c8dcSSimon Schubert 
25195796c8dcSSimon Schubert    If a method is being searched for, VALP will hold the value.
25205796c8dcSSimon Schubert    If a non-method is being searched for, SYMP will hold the symbol
25215796c8dcSSimon Schubert    for it.
25225796c8dcSSimon Schubert 
25235796c8dcSSimon Schubert    If a method is being searched for, and it is a static method,
25245796c8dcSSimon Schubert    then STATICP will point to a non-zero value.
25255796c8dcSSimon Schubert 
2526cf7f2e2dSJohn Marino    If NO_ADL argument dependent lookup is disabled.  This is used to prevent
2527cf7f2e2dSJohn Marino    ADL overload candidates when performing overload resolution for a fully
2528cf7f2e2dSJohn Marino    qualified name.
2529cf7f2e2dSJohn Marino 
25305796c8dcSSimon Schubert    Note: This function does *not* check the value of
25315796c8dcSSimon Schubert    overload_resolution.  Caller must check it to see whether overload
2532*c50c785cSJohn Marino    resolution is permitted.  */
25335796c8dcSSimon Schubert 
25345796c8dcSSimon Schubert int
25355796c8dcSSimon Schubert find_overload_match (struct type **arg_types, int nargs,
2536cf7f2e2dSJohn Marino 		     const char *name, enum oload_search_type method,
2537cf7f2e2dSJohn Marino 		     int lax, struct value **objp, struct symbol *fsym,
25385796c8dcSSimon Schubert 		     struct value **valp, struct symbol **symp,
2539cf7f2e2dSJohn Marino 		     int *staticp, const int no_adl)
25405796c8dcSSimon Schubert {
25415796c8dcSSimon Schubert   struct value *obj = (objp ? *objp : NULL);
25425796c8dcSSimon Schubert   /* Index of best overloaded function.  */
2543cf7f2e2dSJohn Marino   int func_oload_champ = -1;
2544cf7f2e2dSJohn Marino   int method_oload_champ = -1;
2545cf7f2e2dSJohn Marino 
25465796c8dcSSimon Schubert   /* The measure for the current best match.  */
2547cf7f2e2dSJohn Marino   struct badness_vector *method_badness = NULL;
2548cf7f2e2dSJohn Marino   struct badness_vector *func_badness = NULL;
2549cf7f2e2dSJohn Marino 
25505796c8dcSSimon Schubert   struct value *temp = obj;
25515796c8dcSSimon Schubert   /* For methods, the list of overloaded methods.  */
25525796c8dcSSimon Schubert   struct fn_field *fns_ptr = NULL;
25535796c8dcSSimon Schubert   /* For non-methods, the list of overloaded function symbols.  */
25545796c8dcSSimon Schubert   struct symbol **oload_syms = NULL;
25555796c8dcSSimon Schubert   /* Number of overloaded instances being considered.  */
25565796c8dcSSimon Schubert   int num_fns = 0;
25575796c8dcSSimon Schubert   struct type *basetype = NULL;
25585796c8dcSSimon Schubert   int boffset;
2559cf7f2e2dSJohn Marino 
2560cf7f2e2dSJohn Marino   struct cleanup *all_cleanups = make_cleanup (null_cleanup, NULL);
25615796c8dcSSimon Schubert 
25625796c8dcSSimon Schubert   const char *obj_type_name = NULL;
2563cf7f2e2dSJohn Marino   const char *func_name = NULL;
25645796c8dcSSimon Schubert   enum oload_classification match_quality;
2565cf7f2e2dSJohn Marino   enum oload_classification method_match_quality = INCOMPATIBLE;
2566cf7f2e2dSJohn Marino   enum oload_classification func_match_quality = INCOMPATIBLE;
25675796c8dcSSimon Schubert 
25685796c8dcSSimon Schubert   /* Get the list of overloaded methods or functions.  */
2569cf7f2e2dSJohn Marino   if (method == METHOD || method == BOTH)
25705796c8dcSSimon Schubert     {
25715796c8dcSSimon Schubert       gdb_assert (obj);
25725796c8dcSSimon Schubert 
2573cf7f2e2dSJohn Marino       /* OBJ may be a pointer value rather than the object itself.  */
2574cf7f2e2dSJohn Marino       obj = coerce_ref (obj);
2575cf7f2e2dSJohn Marino       while (TYPE_CODE (check_typedef (value_type (obj))) == TYPE_CODE_PTR)
2576cf7f2e2dSJohn Marino 	obj = coerce_ref (value_ind (obj));
2577cf7f2e2dSJohn Marino       obj_type_name = TYPE_NAME (value_type (obj));
2578cf7f2e2dSJohn Marino 
2579cf7f2e2dSJohn Marino       /* First check whether this is a data member, e.g. a pointer to
2580cf7f2e2dSJohn Marino 	 a function.  */
2581cf7f2e2dSJohn Marino       if (TYPE_CODE (check_typedef (value_type (obj))) == TYPE_CODE_STRUCT)
2582cf7f2e2dSJohn Marino 	{
2583cf7f2e2dSJohn Marino 	  *valp = search_struct_field (name, obj, 0,
2584cf7f2e2dSJohn Marino 				       check_typedef (value_type (obj)), 0);
2585cf7f2e2dSJohn Marino 	  if (*valp)
2586cf7f2e2dSJohn Marino 	    {
2587cf7f2e2dSJohn Marino 	      *staticp = 1;
2588cf7f2e2dSJohn Marino 	      return 0;
2589cf7f2e2dSJohn Marino 	    }
2590cf7f2e2dSJohn Marino 	}
2591cf7f2e2dSJohn Marino 
2592cf7f2e2dSJohn Marino       /* Retrieve the list of methods with the name NAME.  */
25935796c8dcSSimon Schubert       fns_ptr = value_find_oload_method_list (&temp, name,
25945796c8dcSSimon Schubert 					      0, &num_fns,
25955796c8dcSSimon Schubert 					      &basetype, &boffset);
2596cf7f2e2dSJohn Marino       /* If this is a method only search, and no methods were found
2597cf7f2e2dSJohn Marino          the search has faild.  */
2598cf7f2e2dSJohn Marino       if (method == METHOD && (!fns_ptr || !num_fns))
25995796c8dcSSimon Schubert 	error (_("Couldn't find method %s%s%s"),
26005796c8dcSSimon Schubert 	       obj_type_name,
26015796c8dcSSimon Schubert 	       (obj_type_name && *obj_type_name) ? "::" : "",
26025796c8dcSSimon Schubert 	       name);
26035796c8dcSSimon Schubert       /* If we are dealing with stub method types, they should have
26045796c8dcSSimon Schubert 	 been resolved by find_method_list via
26055796c8dcSSimon Schubert 	 value_find_oload_method_list above.  */
2606cf7f2e2dSJohn Marino       if (fns_ptr)
2607cf7f2e2dSJohn Marino 	{
26085796c8dcSSimon Schubert 	  gdb_assert (TYPE_DOMAIN_TYPE (fns_ptr[0].type) != NULL);
2609cf7f2e2dSJohn Marino 	  method_oload_champ = find_oload_champ (arg_types, nargs, method,
26105796c8dcSSimon Schubert 	                                         num_fns, fns_ptr,
2611cf7f2e2dSJohn Marino 	                                         oload_syms, &method_badness);
2612cf7f2e2dSJohn Marino 
2613cf7f2e2dSJohn Marino 	  method_match_quality =
2614cf7f2e2dSJohn Marino 	      classify_oload_match (method_badness, nargs,
2615cf7f2e2dSJohn Marino 	                            oload_method_static (method, fns_ptr,
2616cf7f2e2dSJohn Marino 	                                                 method_oload_champ));
2617cf7f2e2dSJohn Marino 
2618cf7f2e2dSJohn Marino 	  make_cleanup (xfree, method_badness);
2619cf7f2e2dSJohn Marino 	}
2620cf7f2e2dSJohn Marino 
2621cf7f2e2dSJohn Marino     }
2622cf7f2e2dSJohn Marino 
2623cf7f2e2dSJohn Marino   if (method == NON_METHOD || method == BOTH)
2624cf7f2e2dSJohn Marino     {
2625cf7f2e2dSJohn Marino       const char *qualified_name = NULL;
2626cf7f2e2dSJohn Marino 
2627*c50c785cSJohn Marino       /* If the overload match is being search for both as a method
2628*c50c785cSJohn Marino          and non member function, the first argument must now be
2629*c50c785cSJohn Marino          dereferenced.  */
2630cf7f2e2dSJohn Marino       if (method == BOTH)
2631cf7f2e2dSJohn Marino 	arg_types[0] = TYPE_TARGET_TYPE (arg_types[0]);
2632cf7f2e2dSJohn Marino 
2633cf7f2e2dSJohn Marino       if (fsym)
2634cf7f2e2dSJohn Marino         {
2635cf7f2e2dSJohn Marino           qualified_name = SYMBOL_NATURAL_NAME (fsym);
2636cf7f2e2dSJohn Marino 
2637cf7f2e2dSJohn Marino           /* If we have a function with a C++ name, try to extract just
2638cf7f2e2dSJohn Marino 	     the function part.  Do not try this for non-functions (e.g.
2639cf7f2e2dSJohn Marino 	     function pointers).  */
2640cf7f2e2dSJohn Marino           if (qualified_name
2641*c50c785cSJohn Marino               && TYPE_CODE (check_typedef (SYMBOL_TYPE (fsym)))
2642*c50c785cSJohn Marino 	      == TYPE_CODE_FUNC)
2643cf7f2e2dSJohn Marino             {
2644cf7f2e2dSJohn Marino 	      char *temp;
2645cf7f2e2dSJohn Marino 
2646cf7f2e2dSJohn Marino 	      temp = cp_func_name (qualified_name);
2647cf7f2e2dSJohn Marino 
2648cf7f2e2dSJohn Marino 	      /* If cp_func_name did not remove anything, the name of the
2649cf7f2e2dSJohn Marino 	         symbol did not include scope or argument types - it was
2650cf7f2e2dSJohn Marino 	         probably a C-style function.  */
2651cf7f2e2dSJohn Marino 	      if (temp)
2652cf7f2e2dSJohn Marino 		{
2653cf7f2e2dSJohn Marino 		  make_cleanup (xfree, temp);
2654cf7f2e2dSJohn Marino 		  if (strcmp (temp, qualified_name) == 0)
2655cf7f2e2dSJohn Marino 		    func_name = NULL;
2656cf7f2e2dSJohn Marino 		  else
2657cf7f2e2dSJohn Marino 		    func_name = temp;
2658cf7f2e2dSJohn Marino 		}
2659cf7f2e2dSJohn Marino             }
26605796c8dcSSimon Schubert         }
26615796c8dcSSimon Schubert       else
26625796c8dcSSimon Schubert 	{
2663cf7f2e2dSJohn Marino 	  func_name = name;
2664cf7f2e2dSJohn Marino 	  qualified_name = name;
2665cf7f2e2dSJohn Marino 	}
26665796c8dcSSimon Schubert 
2667cf7f2e2dSJohn Marino       /* If there was no C++ name, this must be a C-style function or
2668cf7f2e2dSJohn Marino 	 not a function at all.  Just return the same symbol.  Do the
2669cf7f2e2dSJohn Marino 	 same if cp_func_name fails for some reason.  */
26705796c8dcSSimon Schubert       if (func_name == NULL)
26715796c8dcSSimon Schubert         {
26725796c8dcSSimon Schubert 	  *symp = fsym;
26735796c8dcSSimon Schubert           return 0;
26745796c8dcSSimon Schubert         }
26755796c8dcSSimon Schubert 
2676cf7f2e2dSJohn Marino       func_oload_champ = find_oload_champ_namespace (arg_types, nargs,
26775796c8dcSSimon Schubert                                                      func_name,
26785796c8dcSSimon Schubert                                                      qualified_name,
26795796c8dcSSimon Schubert                                                      &oload_syms,
2680cf7f2e2dSJohn Marino                                                      &func_badness,
2681cf7f2e2dSJohn Marino                                                      no_adl);
2682cf7f2e2dSJohn Marino 
2683cf7f2e2dSJohn Marino       if (func_oload_champ >= 0)
2684cf7f2e2dSJohn Marino 	func_match_quality = classify_oload_match (func_badness, nargs, 0);
2685cf7f2e2dSJohn Marino 
2686cf7f2e2dSJohn Marino       make_cleanup (xfree, oload_syms);
2687cf7f2e2dSJohn Marino       make_cleanup (xfree, func_badness);
26885796c8dcSSimon Schubert     }
26895796c8dcSSimon Schubert 
2690cf7f2e2dSJohn Marino   /* Did we find a match ?  */
2691cf7f2e2dSJohn Marino   if (method_oload_champ == -1 && func_oload_champ == -1)
2692*c50c785cSJohn Marino     throw_error (NOT_FOUND_ERROR,
2693*c50c785cSJohn Marino                  _("No symbol \"%s\" in current context."),
2694*c50c785cSJohn Marino                  name);
26955796c8dcSSimon Schubert 
2696cf7f2e2dSJohn Marino   /* If we have found both a method match and a function
2697cf7f2e2dSJohn Marino      match, find out which one is better, and calculate match
2698cf7f2e2dSJohn Marino      quality.  */
2699cf7f2e2dSJohn Marino   if (method_oload_champ >= 0 && func_oload_champ >= 0)
2700cf7f2e2dSJohn Marino     {
2701cf7f2e2dSJohn Marino       switch (compare_badness (func_badness, method_badness))
2702cf7f2e2dSJohn Marino         {
2703cf7f2e2dSJohn Marino 	  case 0: /* Top two contenders are equally good.  */
2704*c50c785cSJohn Marino 	    /* FIXME: GDB does not support the general ambiguous case.
2705*c50c785cSJohn Marino 	     All candidates should be collected and presented the
2706*c50c785cSJohn Marino 	     user.  */
2707cf7f2e2dSJohn Marino 	    error (_("Ambiguous overload resolution"));
2708cf7f2e2dSJohn Marino 	    break;
2709cf7f2e2dSJohn Marino 	  case 1: /* Incomparable top contenders.  */
2710cf7f2e2dSJohn Marino 	    /* This is an error incompatible candidates
2711cf7f2e2dSJohn Marino 	       should not have been proposed.  */
2712*c50c785cSJohn Marino 	    error (_("Internal error: incompatible "
2713*c50c785cSJohn Marino 		     "overload candidates proposed"));
2714cf7f2e2dSJohn Marino 	    break;
2715cf7f2e2dSJohn Marino 	  case 2: /* Function champion.  */
2716cf7f2e2dSJohn Marino 	    method_oload_champ = -1;
2717cf7f2e2dSJohn Marino 	    match_quality = func_match_quality;
2718cf7f2e2dSJohn Marino 	    break;
2719cf7f2e2dSJohn Marino 	  case 3: /* Method champion.  */
2720cf7f2e2dSJohn Marino 	    func_oload_champ = -1;
2721cf7f2e2dSJohn Marino 	    match_quality = method_match_quality;
2722cf7f2e2dSJohn Marino 	    break;
2723cf7f2e2dSJohn Marino 	  default:
2724cf7f2e2dSJohn Marino 	    error (_("Internal error: unexpected overload comparison result"));
2725cf7f2e2dSJohn Marino 	    break;
2726cf7f2e2dSJohn Marino         }
2727cf7f2e2dSJohn Marino     }
2728cf7f2e2dSJohn Marino   else
2729cf7f2e2dSJohn Marino     {
2730cf7f2e2dSJohn Marino       /* We have either a method match or a function match.  */
2731cf7f2e2dSJohn Marino       if (method_oload_champ >= 0)
2732cf7f2e2dSJohn Marino 	match_quality = method_match_quality;
2733cf7f2e2dSJohn Marino       else
2734cf7f2e2dSJohn Marino 	match_quality = func_match_quality;
2735cf7f2e2dSJohn Marino     }
27365796c8dcSSimon Schubert 
27375796c8dcSSimon Schubert   if (match_quality == INCOMPATIBLE)
27385796c8dcSSimon Schubert     {
2739cf7f2e2dSJohn Marino       if (method == METHOD)
27405796c8dcSSimon Schubert 	error (_("Cannot resolve method %s%s%s to any overloaded instance"),
27415796c8dcSSimon Schubert 	       obj_type_name,
27425796c8dcSSimon Schubert 	       (obj_type_name && *obj_type_name) ? "::" : "",
27435796c8dcSSimon Schubert 	       name);
27445796c8dcSSimon Schubert       else
27455796c8dcSSimon Schubert 	error (_("Cannot resolve function %s to any overloaded instance"),
27465796c8dcSSimon Schubert 	       func_name);
27475796c8dcSSimon Schubert     }
27485796c8dcSSimon Schubert   else if (match_quality == NON_STANDARD)
27495796c8dcSSimon Schubert     {
2750cf7f2e2dSJohn Marino       if (method == METHOD)
2751*c50c785cSJohn Marino 	warning (_("Using non-standard conversion to match "
2752*c50c785cSJohn Marino 		   "method %s%s%s to supplied arguments"),
27535796c8dcSSimon Schubert 		 obj_type_name,
27545796c8dcSSimon Schubert 		 (obj_type_name && *obj_type_name) ? "::" : "",
27555796c8dcSSimon Schubert 		 name);
27565796c8dcSSimon Schubert       else
2757*c50c785cSJohn Marino 	warning (_("Using non-standard conversion to match "
2758*c50c785cSJohn Marino 		   "function %s to supplied arguments"),
27595796c8dcSSimon Schubert 		 func_name);
27605796c8dcSSimon Schubert     }
27615796c8dcSSimon Schubert 
27625796c8dcSSimon Schubert   if (staticp != NULL)
2763cf7f2e2dSJohn Marino     *staticp = oload_method_static (method, fns_ptr, method_oload_champ);
2764cf7f2e2dSJohn Marino 
2765cf7f2e2dSJohn Marino   if (method_oload_champ >= 0)
27665796c8dcSSimon Schubert     {
2767cf7f2e2dSJohn Marino       if (TYPE_FN_FIELD_VIRTUAL_P (fns_ptr, method_oload_champ))
2768cf7f2e2dSJohn Marino 	*valp = value_virtual_fn_field (&temp, fns_ptr, method_oload_champ,
2769cf7f2e2dSJohn Marino 					basetype, boffset);
2770cf7f2e2dSJohn Marino       else
2771cf7f2e2dSJohn Marino 	*valp = value_fn_field (&temp, fns_ptr, method_oload_champ,
2772cf7f2e2dSJohn Marino 				basetype, boffset);
27735796c8dcSSimon Schubert     }
2774cf7f2e2dSJohn Marino   else
2775cf7f2e2dSJohn Marino     *symp = oload_syms[func_oload_champ];
27765796c8dcSSimon Schubert 
27775796c8dcSSimon Schubert   if (objp)
27785796c8dcSSimon Schubert     {
27795796c8dcSSimon Schubert       struct type *temp_type = check_typedef (value_type (temp));
27805796c8dcSSimon Schubert       struct type *obj_type = check_typedef (value_type (*objp));
2781cf7f2e2dSJohn Marino 
27825796c8dcSSimon Schubert       if (TYPE_CODE (temp_type) != TYPE_CODE_PTR
27835796c8dcSSimon Schubert 	  && (TYPE_CODE (obj_type) == TYPE_CODE_PTR
27845796c8dcSSimon Schubert 	      || TYPE_CODE (obj_type) == TYPE_CODE_REF))
27855796c8dcSSimon Schubert 	{
27865796c8dcSSimon Schubert 	  temp = value_addr (temp);
27875796c8dcSSimon Schubert 	}
27885796c8dcSSimon Schubert       *objp = temp;
27895796c8dcSSimon Schubert     }
2790cf7f2e2dSJohn Marino 
2791cf7f2e2dSJohn Marino   do_cleanups (all_cleanups);
27925796c8dcSSimon Schubert 
27935796c8dcSSimon Schubert   switch (match_quality)
27945796c8dcSSimon Schubert     {
27955796c8dcSSimon Schubert     case INCOMPATIBLE:
27965796c8dcSSimon Schubert       return 100;
27975796c8dcSSimon Schubert     case NON_STANDARD:
27985796c8dcSSimon Schubert       return 10;
27995796c8dcSSimon Schubert     default:				/* STANDARD */
28005796c8dcSSimon Schubert       return 0;
28015796c8dcSSimon Schubert     }
28025796c8dcSSimon Schubert }
28035796c8dcSSimon Schubert 
28045796c8dcSSimon Schubert /* Find the best overload match, searching for FUNC_NAME in namespaces
28055796c8dcSSimon Schubert    contained in QUALIFIED_NAME until it either finds a good match or
28065796c8dcSSimon Schubert    runs out of namespaces.  It stores the overloaded functions in
28075796c8dcSSimon Schubert    *OLOAD_SYMS, and the badness vector in *OLOAD_CHAMP_BV.  The
28085796c8dcSSimon Schubert    calling function is responsible for freeing *OLOAD_SYMS and
2809cf7f2e2dSJohn Marino    *OLOAD_CHAMP_BV.  If NO_ADL, argument dependent lookup is not
2810cf7f2e2dSJohn Marino    performned.  */
28115796c8dcSSimon Schubert 
28125796c8dcSSimon Schubert static int
28135796c8dcSSimon Schubert find_oload_champ_namespace (struct type **arg_types, int nargs,
28145796c8dcSSimon Schubert 			    const char *func_name,
28155796c8dcSSimon Schubert 			    const char *qualified_name,
28165796c8dcSSimon Schubert 			    struct symbol ***oload_syms,
2817cf7f2e2dSJohn Marino 			    struct badness_vector **oload_champ_bv,
2818cf7f2e2dSJohn Marino 			    const int no_adl)
28195796c8dcSSimon Schubert {
28205796c8dcSSimon Schubert   int oload_champ;
28215796c8dcSSimon Schubert 
28225796c8dcSSimon Schubert   find_oload_champ_namespace_loop (arg_types, nargs,
28235796c8dcSSimon Schubert 				   func_name,
28245796c8dcSSimon Schubert 				   qualified_name, 0,
28255796c8dcSSimon Schubert 				   oload_syms, oload_champ_bv,
2826cf7f2e2dSJohn Marino 				   &oload_champ,
2827cf7f2e2dSJohn Marino 				   no_adl);
28285796c8dcSSimon Schubert 
28295796c8dcSSimon Schubert   return oload_champ;
28305796c8dcSSimon Schubert }
28315796c8dcSSimon Schubert 
28325796c8dcSSimon Schubert /* Helper function for find_oload_champ_namespace; NAMESPACE_LEN is
28335796c8dcSSimon Schubert    how deep we've looked for namespaces, and the champ is stored in
28345796c8dcSSimon Schubert    OLOAD_CHAMP.  The return value is 1 if the champ is a good one, 0
2835cf7f2e2dSJohn Marino    if it isn't.  Other arguments are the same as in
2836cf7f2e2dSJohn Marino    find_oload_champ_namespace
28375796c8dcSSimon Schubert 
28385796c8dcSSimon Schubert    It is the caller's responsibility to free *OLOAD_SYMS and
28395796c8dcSSimon Schubert    *OLOAD_CHAMP_BV.  */
28405796c8dcSSimon Schubert 
28415796c8dcSSimon Schubert static int
28425796c8dcSSimon Schubert find_oload_champ_namespace_loop (struct type **arg_types, int nargs,
28435796c8dcSSimon Schubert 				 const char *func_name,
28445796c8dcSSimon Schubert 				 const char *qualified_name,
28455796c8dcSSimon Schubert 				 int namespace_len,
28465796c8dcSSimon Schubert 				 struct symbol ***oload_syms,
28475796c8dcSSimon Schubert 				 struct badness_vector **oload_champ_bv,
2848cf7f2e2dSJohn Marino 				 int *oload_champ,
2849cf7f2e2dSJohn Marino 				 const int no_adl)
28505796c8dcSSimon Schubert {
28515796c8dcSSimon Schubert   int next_namespace_len = namespace_len;
28525796c8dcSSimon Schubert   int searched_deeper = 0;
28535796c8dcSSimon Schubert   int num_fns = 0;
28545796c8dcSSimon Schubert   struct cleanup *old_cleanups;
28555796c8dcSSimon Schubert   int new_oload_champ;
28565796c8dcSSimon Schubert   struct symbol **new_oload_syms;
28575796c8dcSSimon Schubert   struct badness_vector *new_oload_champ_bv;
28585796c8dcSSimon Schubert   char *new_namespace;
28595796c8dcSSimon Schubert 
28605796c8dcSSimon Schubert   if (next_namespace_len != 0)
28615796c8dcSSimon Schubert     {
28625796c8dcSSimon Schubert       gdb_assert (qualified_name[next_namespace_len] == ':');
28635796c8dcSSimon Schubert       next_namespace_len +=  2;
28645796c8dcSSimon Schubert     }
28655796c8dcSSimon Schubert   next_namespace_len +=
28665796c8dcSSimon Schubert     cp_find_first_component (qualified_name + next_namespace_len);
28675796c8dcSSimon Schubert 
28685796c8dcSSimon Schubert   /* Initialize these to values that can safely be xfree'd.  */
28695796c8dcSSimon Schubert   *oload_syms = NULL;
28705796c8dcSSimon Schubert   *oload_champ_bv = NULL;
28715796c8dcSSimon Schubert 
28725796c8dcSSimon Schubert   /* First, see if we have a deeper namespace we can search in.
28735796c8dcSSimon Schubert      If we get a good match there, use it.  */
28745796c8dcSSimon Schubert 
28755796c8dcSSimon Schubert   if (qualified_name[next_namespace_len] == ':')
28765796c8dcSSimon Schubert     {
28775796c8dcSSimon Schubert       searched_deeper = 1;
28785796c8dcSSimon Schubert 
28795796c8dcSSimon Schubert       if (find_oload_champ_namespace_loop (arg_types, nargs,
28805796c8dcSSimon Schubert 					   func_name, qualified_name,
28815796c8dcSSimon Schubert 					   next_namespace_len,
28825796c8dcSSimon Schubert 					   oload_syms, oload_champ_bv,
2883cf7f2e2dSJohn Marino 					   oload_champ, no_adl))
28845796c8dcSSimon Schubert 	{
28855796c8dcSSimon Schubert 	  return 1;
28865796c8dcSSimon Schubert 	}
28875796c8dcSSimon Schubert     };
28885796c8dcSSimon Schubert 
28895796c8dcSSimon Schubert   /* If we reach here, either we're in the deepest namespace or we
28905796c8dcSSimon Schubert      didn't find a good match in a deeper namespace.  But, in the
28915796c8dcSSimon Schubert      latter case, we still have a bad match in a deeper namespace;
28925796c8dcSSimon Schubert      note that we might not find any match at all in the current
28935796c8dcSSimon Schubert      namespace.  (There's always a match in the deepest namespace,
28945796c8dcSSimon Schubert      because this overload mechanism only gets called if there's a
28955796c8dcSSimon Schubert      function symbol to start off with.)  */
28965796c8dcSSimon Schubert 
28975796c8dcSSimon Schubert   old_cleanups = make_cleanup (xfree, *oload_syms);
2898*c50c785cSJohn Marino   make_cleanup (xfree, *oload_champ_bv);
28995796c8dcSSimon Schubert   new_namespace = alloca (namespace_len + 1);
29005796c8dcSSimon Schubert   strncpy (new_namespace, qualified_name, namespace_len);
29015796c8dcSSimon Schubert   new_namespace[namespace_len] = '\0';
29025796c8dcSSimon Schubert   new_oload_syms = make_symbol_overload_list (func_name,
29035796c8dcSSimon Schubert 					      new_namespace);
2904cf7f2e2dSJohn Marino 
2905cf7f2e2dSJohn Marino   /* If we have reached the deepest level perform argument
2906cf7f2e2dSJohn Marino      determined lookup.  */
2907cf7f2e2dSJohn Marino   if (!searched_deeper && !no_adl)
2908cf7f2e2dSJohn Marino     make_symbol_overload_list_adl (arg_types, nargs, func_name);
2909cf7f2e2dSJohn Marino 
29105796c8dcSSimon Schubert   while (new_oload_syms[num_fns])
29115796c8dcSSimon Schubert     ++num_fns;
29125796c8dcSSimon Schubert 
29135796c8dcSSimon Schubert   new_oload_champ = find_oload_champ (arg_types, nargs, 0, num_fns,
29145796c8dcSSimon Schubert 				      NULL, new_oload_syms,
29155796c8dcSSimon Schubert 				      &new_oload_champ_bv);
29165796c8dcSSimon Schubert 
29175796c8dcSSimon Schubert   /* Case 1: We found a good match.  Free earlier matches (if any),
29185796c8dcSSimon Schubert      and return it.  Case 2: We didn't find a good match, but we're
29195796c8dcSSimon Schubert      not the deepest function.  Then go with the bad match that the
29205796c8dcSSimon Schubert      deeper function found.  Case 3: We found a bad match, and we're
29215796c8dcSSimon Schubert      the deepest function.  Then return what we found, even though
29225796c8dcSSimon Schubert      it's a bad match.  */
29235796c8dcSSimon Schubert 
29245796c8dcSSimon Schubert   if (new_oload_champ != -1
29255796c8dcSSimon Schubert       && classify_oload_match (new_oload_champ_bv, nargs, 0) == STANDARD)
29265796c8dcSSimon Schubert     {
29275796c8dcSSimon Schubert       *oload_syms = new_oload_syms;
29285796c8dcSSimon Schubert       *oload_champ = new_oload_champ;
29295796c8dcSSimon Schubert       *oload_champ_bv = new_oload_champ_bv;
29305796c8dcSSimon Schubert       do_cleanups (old_cleanups);
29315796c8dcSSimon Schubert       return 1;
29325796c8dcSSimon Schubert     }
29335796c8dcSSimon Schubert   else if (searched_deeper)
29345796c8dcSSimon Schubert     {
29355796c8dcSSimon Schubert       xfree (new_oload_syms);
29365796c8dcSSimon Schubert       xfree (new_oload_champ_bv);
29375796c8dcSSimon Schubert       discard_cleanups (old_cleanups);
29385796c8dcSSimon Schubert       return 0;
29395796c8dcSSimon Schubert     }
29405796c8dcSSimon Schubert   else
29415796c8dcSSimon Schubert     {
29425796c8dcSSimon Schubert       *oload_syms = new_oload_syms;
29435796c8dcSSimon Schubert       *oload_champ = new_oload_champ;
29445796c8dcSSimon Schubert       *oload_champ_bv = new_oload_champ_bv;
2945*c50c785cSJohn Marino       do_cleanups (old_cleanups);
29465796c8dcSSimon Schubert       return 0;
29475796c8dcSSimon Schubert     }
29485796c8dcSSimon Schubert }
29495796c8dcSSimon Schubert 
29505796c8dcSSimon Schubert /* Look for a function to take NARGS args of types ARG_TYPES.  Find
29515796c8dcSSimon Schubert    the best match from among the overloaded methods or functions
29525796c8dcSSimon Schubert    (depending on METHOD) given by FNS_PTR or OLOAD_SYMS, respectively.
29535796c8dcSSimon Schubert    The number of methods/functions in the list is given by NUM_FNS.
29545796c8dcSSimon Schubert    Return the index of the best match; store an indication of the
29555796c8dcSSimon Schubert    quality of the match in OLOAD_CHAMP_BV.
29565796c8dcSSimon Schubert 
29575796c8dcSSimon Schubert    It is the caller's responsibility to free *OLOAD_CHAMP_BV.  */
29585796c8dcSSimon Schubert 
29595796c8dcSSimon Schubert static int
29605796c8dcSSimon Schubert find_oload_champ (struct type **arg_types, int nargs, int method,
29615796c8dcSSimon Schubert 		  int num_fns, struct fn_field *fns_ptr,
29625796c8dcSSimon Schubert 		  struct symbol **oload_syms,
29635796c8dcSSimon Schubert 		  struct badness_vector **oload_champ_bv)
29645796c8dcSSimon Schubert {
29655796c8dcSSimon Schubert   int ix;
29665796c8dcSSimon Schubert   /* A measure of how good an overloaded instance is.  */
29675796c8dcSSimon Schubert   struct badness_vector *bv;
29685796c8dcSSimon Schubert   /* Index of best overloaded function.  */
29695796c8dcSSimon Schubert   int oload_champ = -1;
29705796c8dcSSimon Schubert   /* Current ambiguity state for overload resolution.  */
29715796c8dcSSimon Schubert   int oload_ambiguous = 0;
29725796c8dcSSimon Schubert   /* 0 => no ambiguity, 1 => two good funcs, 2 => incomparable funcs.  */
29735796c8dcSSimon Schubert 
29745796c8dcSSimon Schubert   *oload_champ_bv = NULL;
29755796c8dcSSimon Schubert 
29765796c8dcSSimon Schubert   /* Consider each candidate in turn.  */
29775796c8dcSSimon Schubert   for (ix = 0; ix < num_fns; ix++)
29785796c8dcSSimon Schubert     {
29795796c8dcSSimon Schubert       int jj;
29805796c8dcSSimon Schubert       int static_offset = oload_method_static (method, fns_ptr, ix);
29815796c8dcSSimon Schubert       int nparms;
29825796c8dcSSimon Schubert       struct type **parm_types;
29835796c8dcSSimon Schubert 
29845796c8dcSSimon Schubert       if (method)
29855796c8dcSSimon Schubert 	{
29865796c8dcSSimon Schubert 	  nparms = TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (fns_ptr, ix));
29875796c8dcSSimon Schubert 	}
29885796c8dcSSimon Schubert       else
29895796c8dcSSimon Schubert 	{
29905796c8dcSSimon Schubert 	  /* If it's not a method, this is the proper place.  */
29915796c8dcSSimon Schubert 	  nparms = TYPE_NFIELDS (SYMBOL_TYPE (oload_syms[ix]));
29925796c8dcSSimon Schubert 	}
29935796c8dcSSimon Schubert 
29945796c8dcSSimon Schubert       /* Prepare array of parameter types.  */
29955796c8dcSSimon Schubert       parm_types = (struct type **)
29965796c8dcSSimon Schubert 	xmalloc (nparms * (sizeof (struct type *)));
29975796c8dcSSimon Schubert       for (jj = 0; jj < nparms; jj++)
29985796c8dcSSimon Schubert 	parm_types[jj] = (method
29995796c8dcSSimon Schubert 			  ? (TYPE_FN_FIELD_ARGS (fns_ptr, ix)[jj].type)
30005796c8dcSSimon Schubert 			  : TYPE_FIELD_TYPE (SYMBOL_TYPE (oload_syms[ix]),
30015796c8dcSSimon Schubert 					     jj));
30025796c8dcSSimon Schubert 
30035796c8dcSSimon Schubert       /* Compare parameter types to supplied argument types.  Skip
30045796c8dcSSimon Schubert          THIS for static methods.  */
30055796c8dcSSimon Schubert       bv = rank_function (parm_types, nparms,
30065796c8dcSSimon Schubert 			  arg_types + static_offset,
30075796c8dcSSimon Schubert 			  nargs - static_offset);
30085796c8dcSSimon Schubert 
30095796c8dcSSimon Schubert       if (!*oload_champ_bv)
30105796c8dcSSimon Schubert 	{
30115796c8dcSSimon Schubert 	  *oload_champ_bv = bv;
30125796c8dcSSimon Schubert 	  oload_champ = 0;
30135796c8dcSSimon Schubert 	}
30145796c8dcSSimon Schubert       else /* See whether current candidate is better or worse than
30155796c8dcSSimon Schubert 	      previous best.  */
30165796c8dcSSimon Schubert 	switch (compare_badness (bv, *oload_champ_bv))
30175796c8dcSSimon Schubert 	  {
30185796c8dcSSimon Schubert 	  case 0:		/* Top two contenders are equally good.  */
30195796c8dcSSimon Schubert 	    oload_ambiguous = 1;
30205796c8dcSSimon Schubert 	    break;
30215796c8dcSSimon Schubert 	  case 1:		/* Incomparable top contenders.  */
30225796c8dcSSimon Schubert 	    oload_ambiguous = 2;
30235796c8dcSSimon Schubert 	    break;
30245796c8dcSSimon Schubert 	  case 2:		/* New champion, record details.  */
30255796c8dcSSimon Schubert 	    *oload_champ_bv = bv;
30265796c8dcSSimon Schubert 	    oload_ambiguous = 0;
30275796c8dcSSimon Schubert 	    oload_champ = ix;
30285796c8dcSSimon Schubert 	    break;
30295796c8dcSSimon Schubert 	  case 3:
30305796c8dcSSimon Schubert 	  default:
30315796c8dcSSimon Schubert 	    break;
30325796c8dcSSimon Schubert 	  }
30335796c8dcSSimon Schubert       xfree (parm_types);
30345796c8dcSSimon Schubert       if (overload_debug)
30355796c8dcSSimon Schubert 	{
30365796c8dcSSimon Schubert 	  if (method)
30375796c8dcSSimon Schubert 	    fprintf_filtered (gdb_stderr,
30385796c8dcSSimon Schubert 			      "Overloaded method instance %s, # of parms %d\n",
30395796c8dcSSimon Schubert 			      fns_ptr[ix].physname, nparms);
30405796c8dcSSimon Schubert 	  else
30415796c8dcSSimon Schubert 	    fprintf_filtered (gdb_stderr,
3042*c50c785cSJohn Marino 			      "Overloaded function instance "
3043*c50c785cSJohn Marino 			      "%s # of parms %d\n",
30445796c8dcSSimon Schubert 			      SYMBOL_DEMANGLED_NAME (oload_syms[ix]),
30455796c8dcSSimon Schubert 			      nparms);
30465796c8dcSSimon Schubert 	  for (jj = 0; jj < nargs - static_offset; jj++)
30475796c8dcSSimon Schubert 	    fprintf_filtered (gdb_stderr,
30485796c8dcSSimon Schubert 			      "...Badness @ %d : %d\n",
3049*c50c785cSJohn Marino 			      jj, bv->rank[jj].rank);
3050*c50c785cSJohn Marino 	  fprintf_filtered (gdb_stderr, "Overload resolution "
3051*c50c785cSJohn Marino 			    "champion is %d, ambiguous? %d\n",
30525796c8dcSSimon Schubert 			    oload_champ, oload_ambiguous);
30535796c8dcSSimon Schubert 	}
30545796c8dcSSimon Schubert     }
30555796c8dcSSimon Schubert 
30565796c8dcSSimon Schubert   return oload_champ;
30575796c8dcSSimon Schubert }
30585796c8dcSSimon Schubert 
30595796c8dcSSimon Schubert /* Return 1 if we're looking at a static method, 0 if we're looking at
30605796c8dcSSimon Schubert    a non-static method or a function that isn't a method.  */
30615796c8dcSSimon Schubert 
30625796c8dcSSimon Schubert static int
30635796c8dcSSimon Schubert oload_method_static (int method, struct fn_field *fns_ptr, int index)
30645796c8dcSSimon Schubert {
3065cf7f2e2dSJohn Marino   if (method && fns_ptr && index >= 0
3066cf7f2e2dSJohn Marino       && TYPE_FN_FIELD_STATIC_P (fns_ptr, index))
30675796c8dcSSimon Schubert     return 1;
30685796c8dcSSimon Schubert   else
30695796c8dcSSimon Schubert     return 0;
30705796c8dcSSimon Schubert }
30715796c8dcSSimon Schubert 
30725796c8dcSSimon Schubert /* Check how good an overload match OLOAD_CHAMP_BV represents.  */
30735796c8dcSSimon Schubert 
30745796c8dcSSimon Schubert static enum oload_classification
30755796c8dcSSimon Schubert classify_oload_match (struct badness_vector *oload_champ_bv,
30765796c8dcSSimon Schubert 		      int nargs,
30775796c8dcSSimon Schubert 		      int static_offset)
30785796c8dcSSimon Schubert {
30795796c8dcSSimon Schubert   int ix;
30805796c8dcSSimon Schubert 
30815796c8dcSSimon Schubert   for (ix = 1; ix <= nargs - static_offset; ix++)
30825796c8dcSSimon Schubert     {
3083*c50c785cSJohn Marino       /* If this conversion is as bad as INCOMPATIBLE_TYPE_BADNESS
3084*c50c785cSJohn Marino          or worse return INCOMPATIBLE.  */
3085*c50c785cSJohn Marino       if (compare_ranks (oload_champ_bv->rank[ix],
3086*c50c785cSJohn Marino                          INCOMPATIBLE_TYPE_BADNESS) <= 0)
30875796c8dcSSimon Schubert 	return INCOMPATIBLE;	/* Truly mismatched types.  */
3088*c50c785cSJohn Marino       /* Otherwise If this conversion is as bad as
3089*c50c785cSJohn Marino          NS_POINTER_CONVERSION_BADNESS or worse return NON_STANDARD.  */
3090*c50c785cSJohn Marino       else if (compare_ranks (oload_champ_bv->rank[ix],
3091*c50c785cSJohn Marino                               NS_POINTER_CONVERSION_BADNESS) <= 0)
30925796c8dcSSimon Schubert 	return NON_STANDARD;	/* Non-standard type conversions
30935796c8dcSSimon Schubert 				   needed.  */
30945796c8dcSSimon Schubert     }
30955796c8dcSSimon Schubert 
30965796c8dcSSimon Schubert   return STANDARD;		/* Only standard conversions needed.  */
30975796c8dcSSimon Schubert }
30985796c8dcSSimon Schubert 
30995796c8dcSSimon Schubert /* C++: return 1 is NAME is a legitimate name for the destructor of
31005796c8dcSSimon Schubert    type TYPE.  If TYPE does not have a destructor, or if NAME is
31015796c8dcSSimon Schubert    inappropriate for TYPE, an error is signaled.  */
31025796c8dcSSimon Schubert int
31035796c8dcSSimon Schubert destructor_name_p (const char *name, const struct type *type)
31045796c8dcSSimon Schubert {
31055796c8dcSSimon Schubert   if (name[0] == '~')
31065796c8dcSSimon Schubert     {
31075796c8dcSSimon Schubert       char *dname = type_name_no_tag (type);
31085796c8dcSSimon Schubert       char *cp = strchr (dname, '<');
31095796c8dcSSimon Schubert       unsigned int len;
31105796c8dcSSimon Schubert 
31115796c8dcSSimon Schubert       /* Do not compare the template part for template classes.  */
31125796c8dcSSimon Schubert       if (cp == NULL)
31135796c8dcSSimon Schubert 	len = strlen (dname);
31145796c8dcSSimon Schubert       else
31155796c8dcSSimon Schubert 	len = cp - dname;
31165796c8dcSSimon Schubert       if (strlen (name + 1) != len || strncmp (dname, name + 1, len) != 0)
31175796c8dcSSimon Schubert 	error (_("name of destructor must equal name of class"));
31185796c8dcSSimon Schubert       else
31195796c8dcSSimon Schubert 	return 1;
31205796c8dcSSimon Schubert     }
31215796c8dcSSimon Schubert   return 0;
31225796c8dcSSimon Schubert }
31235796c8dcSSimon Schubert 
31245796c8dcSSimon Schubert /* Given TYPE, a structure/union,
31255796c8dcSSimon Schubert    return 1 if the component named NAME from the ultimate target
31265796c8dcSSimon Schubert    structure/union is defined, otherwise, return 0.  */
31275796c8dcSSimon Schubert 
31285796c8dcSSimon Schubert int
31295796c8dcSSimon Schubert check_field (struct type *type, const char *name)
31305796c8dcSSimon Schubert {
31315796c8dcSSimon Schubert   int i;
31325796c8dcSSimon Schubert 
3133cf7f2e2dSJohn Marino   /* The type may be a stub.  */
3134cf7f2e2dSJohn Marino   CHECK_TYPEDEF (type);
3135cf7f2e2dSJohn Marino 
31365796c8dcSSimon Schubert   for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
31375796c8dcSSimon Schubert     {
31385796c8dcSSimon Schubert       char *t_field_name = TYPE_FIELD_NAME (type, i);
3139cf7f2e2dSJohn Marino 
31405796c8dcSSimon Schubert       if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
31415796c8dcSSimon Schubert 	return 1;
31425796c8dcSSimon Schubert     }
31435796c8dcSSimon Schubert 
31445796c8dcSSimon Schubert   /* C++: If it was not found as a data field, then try to return it
31455796c8dcSSimon Schubert      as a pointer to a method.  */
31465796c8dcSSimon Schubert 
31475796c8dcSSimon Schubert   for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
31485796c8dcSSimon Schubert     {
31495796c8dcSSimon Schubert       if (strcmp_iw (TYPE_FN_FIELDLIST_NAME (type, i), name) == 0)
31505796c8dcSSimon Schubert 	return 1;
31515796c8dcSSimon Schubert     }
31525796c8dcSSimon Schubert 
31535796c8dcSSimon Schubert   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
31545796c8dcSSimon Schubert     if (check_field (TYPE_BASECLASS (type, i), name))
31555796c8dcSSimon Schubert       return 1;
31565796c8dcSSimon Schubert 
31575796c8dcSSimon Schubert   return 0;
31585796c8dcSSimon Schubert }
31595796c8dcSSimon Schubert 
31605796c8dcSSimon Schubert /* C++: Given an aggregate type CURTYPE, and a member name NAME,
31615796c8dcSSimon Schubert    return the appropriate member (or the address of the member, if
31625796c8dcSSimon Schubert    WANT_ADDRESS).  This function is used to resolve user expressions
31635796c8dcSSimon Schubert    of the form "DOMAIN::NAME".  For more details on what happens, see
31645796c8dcSSimon Schubert    the comment before value_struct_elt_for_reference.  */
31655796c8dcSSimon Schubert 
31665796c8dcSSimon Schubert struct value *
3167cf7f2e2dSJohn Marino value_aggregate_elt (struct type *curtype, char *name,
3168cf7f2e2dSJohn Marino 		     struct type *expect_type, int want_address,
31695796c8dcSSimon Schubert 		     enum noside noside)
31705796c8dcSSimon Schubert {
31715796c8dcSSimon Schubert   switch (TYPE_CODE (curtype))
31725796c8dcSSimon Schubert     {
31735796c8dcSSimon Schubert     case TYPE_CODE_STRUCT:
31745796c8dcSSimon Schubert     case TYPE_CODE_UNION:
31755796c8dcSSimon Schubert       return value_struct_elt_for_reference (curtype, 0, curtype,
3176cf7f2e2dSJohn Marino 					     name, expect_type,
31775796c8dcSSimon Schubert 					     want_address, noside);
31785796c8dcSSimon Schubert     case TYPE_CODE_NAMESPACE:
31795796c8dcSSimon Schubert       return value_namespace_elt (curtype, name,
31805796c8dcSSimon Schubert 				  want_address, noside);
31815796c8dcSSimon Schubert     default:
31825796c8dcSSimon Schubert       internal_error (__FILE__, __LINE__,
31835796c8dcSSimon Schubert 		      _("non-aggregate type in value_aggregate_elt"));
31845796c8dcSSimon Schubert     }
31855796c8dcSSimon Schubert }
31865796c8dcSSimon Schubert 
3187cf7f2e2dSJohn Marino /* Compares the two method/function types T1 and T2 for "equality"
3188*c50c785cSJohn Marino    with respect to the methods' parameters.  If the types of the
3189cf7f2e2dSJohn Marino    two parameter lists are the same, returns 1; 0 otherwise.  This
3190cf7f2e2dSJohn Marino    comparison may ignore any artificial parameters in T1 if
3191cf7f2e2dSJohn Marino    SKIP_ARTIFICIAL is non-zero.  This function will ALWAYS skip
3192cf7f2e2dSJohn Marino    the first artificial parameter in T1, assumed to be a 'this' pointer.
3193cf7f2e2dSJohn Marino 
3194cf7f2e2dSJohn Marino    The type T2 is expected to have come from make_params (in eval.c).  */
3195cf7f2e2dSJohn Marino 
3196cf7f2e2dSJohn Marino static int
3197cf7f2e2dSJohn Marino compare_parameters (struct type *t1, struct type *t2, int skip_artificial)
3198cf7f2e2dSJohn Marino {
3199cf7f2e2dSJohn Marino   int start = 0;
3200cf7f2e2dSJohn Marino 
3201*c50c785cSJohn Marino   if (TYPE_NFIELDS (t1) > 0 && TYPE_FIELD_ARTIFICIAL (t1, 0))
3202cf7f2e2dSJohn Marino     ++start;
3203cf7f2e2dSJohn Marino 
3204cf7f2e2dSJohn Marino   /* If skipping artificial fields, find the first real field
3205cf7f2e2dSJohn Marino      in T1.  */
3206cf7f2e2dSJohn Marino   if (skip_artificial)
3207cf7f2e2dSJohn Marino     {
3208cf7f2e2dSJohn Marino       while (start < TYPE_NFIELDS (t1)
3209cf7f2e2dSJohn Marino 	     && TYPE_FIELD_ARTIFICIAL (t1, start))
3210cf7f2e2dSJohn Marino 	++start;
3211cf7f2e2dSJohn Marino     }
3212cf7f2e2dSJohn Marino 
3213*c50c785cSJohn Marino   /* Now compare parameters.  */
3214cf7f2e2dSJohn Marino 
3215cf7f2e2dSJohn Marino   /* Special case: a method taking void.  T1 will contain no
3216cf7f2e2dSJohn Marino      non-artificial fields, and T2 will contain TYPE_CODE_VOID.  */
3217cf7f2e2dSJohn Marino   if ((TYPE_NFIELDS (t1) - start) == 0 && TYPE_NFIELDS (t2) == 1
3218cf7f2e2dSJohn Marino       && TYPE_CODE (TYPE_FIELD_TYPE (t2, 0)) == TYPE_CODE_VOID)
3219cf7f2e2dSJohn Marino     return 1;
3220cf7f2e2dSJohn Marino 
3221cf7f2e2dSJohn Marino   if ((TYPE_NFIELDS (t1) - start) == TYPE_NFIELDS (t2))
3222cf7f2e2dSJohn Marino     {
3223cf7f2e2dSJohn Marino       int i;
3224cf7f2e2dSJohn Marino 
3225cf7f2e2dSJohn Marino       for (i = 0; i < TYPE_NFIELDS (t2); ++i)
3226cf7f2e2dSJohn Marino 	{
3227*c50c785cSJohn Marino 	  if (compare_ranks (rank_one_type (TYPE_FIELD_TYPE (t1, start + i),
3228*c50c785cSJohn Marino 	                                   TYPE_FIELD_TYPE (t2, i)),
3229*c50c785cSJohn Marino 	                     EXACT_MATCH_BADNESS) != 0)
3230cf7f2e2dSJohn Marino 	    return 0;
3231cf7f2e2dSJohn Marino 	}
3232cf7f2e2dSJohn Marino 
3233cf7f2e2dSJohn Marino       return 1;
3234cf7f2e2dSJohn Marino     }
3235cf7f2e2dSJohn Marino 
3236cf7f2e2dSJohn Marino   return 0;
3237cf7f2e2dSJohn Marino }
3238cf7f2e2dSJohn Marino 
32395796c8dcSSimon Schubert /* C++: Given an aggregate type CURTYPE, and a member name NAME,
32405796c8dcSSimon Schubert    return the address of this member as a "pointer to member" type.
32415796c8dcSSimon Schubert    If INTYPE is non-null, then it will be the type of the member we
32425796c8dcSSimon Schubert    are looking for.  This will help us resolve "pointers to member
32435796c8dcSSimon Schubert    functions".  This function is used to resolve user expressions of
32445796c8dcSSimon Schubert    the form "DOMAIN::NAME".  */
32455796c8dcSSimon Schubert 
32465796c8dcSSimon Schubert static struct value *
32475796c8dcSSimon Schubert value_struct_elt_for_reference (struct type *domain, int offset,
32485796c8dcSSimon Schubert 				struct type *curtype, char *name,
32495796c8dcSSimon Schubert 				struct type *intype,
32505796c8dcSSimon Schubert 				int want_address,
32515796c8dcSSimon Schubert 				enum noside noside)
32525796c8dcSSimon Schubert {
32535796c8dcSSimon Schubert   struct type *t = curtype;
32545796c8dcSSimon Schubert   int i;
32555796c8dcSSimon Schubert   struct value *v, *result;
32565796c8dcSSimon Schubert 
32575796c8dcSSimon Schubert   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
32585796c8dcSSimon Schubert       && TYPE_CODE (t) != TYPE_CODE_UNION)
3259*c50c785cSJohn Marino     error (_("Internal error: non-aggregate type "
3260*c50c785cSJohn Marino 	     "to value_struct_elt_for_reference"));
32615796c8dcSSimon Schubert 
32625796c8dcSSimon Schubert   for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
32635796c8dcSSimon Schubert     {
32645796c8dcSSimon Schubert       char *t_field_name = TYPE_FIELD_NAME (t, i);
32655796c8dcSSimon Schubert 
32665796c8dcSSimon Schubert       if (t_field_name && strcmp (t_field_name, name) == 0)
32675796c8dcSSimon Schubert 	{
32685796c8dcSSimon Schubert 	  if (field_is_static (&TYPE_FIELD (t, i)))
32695796c8dcSSimon Schubert 	    {
32705796c8dcSSimon Schubert 	      v = value_static_field (t, i);
32715796c8dcSSimon Schubert 	      if (v == NULL)
32725796c8dcSSimon Schubert 		error (_("static field %s has been optimized out"),
32735796c8dcSSimon Schubert 		       name);
32745796c8dcSSimon Schubert 	      if (want_address)
32755796c8dcSSimon Schubert 		v = value_addr (v);
32765796c8dcSSimon Schubert 	      return v;
32775796c8dcSSimon Schubert 	    }
32785796c8dcSSimon Schubert 	  if (TYPE_FIELD_PACKED (t, i))
32795796c8dcSSimon Schubert 	    error (_("pointers to bitfield members not allowed"));
32805796c8dcSSimon Schubert 
32815796c8dcSSimon Schubert 	  if (want_address)
32825796c8dcSSimon Schubert 	    return value_from_longest
32835796c8dcSSimon Schubert 	      (lookup_memberptr_type (TYPE_FIELD_TYPE (t, i), domain),
32845796c8dcSSimon Schubert 	       offset + (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3));
32855796c8dcSSimon Schubert 	  else if (noside == EVAL_AVOID_SIDE_EFFECTS)
32865796c8dcSSimon Schubert 	    return allocate_value (TYPE_FIELD_TYPE (t, i));
32875796c8dcSSimon Schubert 	  else
32885796c8dcSSimon Schubert 	    error (_("Cannot reference non-static field \"%s\""), name);
32895796c8dcSSimon Schubert 	}
32905796c8dcSSimon Schubert     }
32915796c8dcSSimon Schubert 
32925796c8dcSSimon Schubert   /* C++: If it was not found as a data field, then try to return it
32935796c8dcSSimon Schubert      as a pointer to a method.  */
32945796c8dcSSimon Schubert 
32955796c8dcSSimon Schubert   /* Perform all necessary dereferencing.  */
32965796c8dcSSimon Schubert   while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR)
32975796c8dcSSimon Schubert     intype = TYPE_TARGET_TYPE (intype);
32985796c8dcSSimon Schubert 
32995796c8dcSSimon Schubert   for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
33005796c8dcSSimon Schubert     {
33015796c8dcSSimon Schubert       char *t_field_name = TYPE_FN_FIELDLIST_NAME (t, i);
33025796c8dcSSimon Schubert       char dem_opname[64];
33035796c8dcSSimon Schubert 
33045796c8dcSSimon Schubert       if (strncmp (t_field_name, "__", 2) == 0
33055796c8dcSSimon Schubert 	  || strncmp (t_field_name, "op", 2) == 0
33065796c8dcSSimon Schubert 	  || strncmp (t_field_name, "type", 4) == 0)
33075796c8dcSSimon Schubert 	{
33085796c8dcSSimon Schubert 	  if (cplus_demangle_opname (t_field_name,
33095796c8dcSSimon Schubert 				     dem_opname, DMGL_ANSI))
33105796c8dcSSimon Schubert 	    t_field_name = dem_opname;
33115796c8dcSSimon Schubert 	  else if (cplus_demangle_opname (t_field_name,
33125796c8dcSSimon Schubert 					  dem_opname, 0))
33135796c8dcSSimon Schubert 	    t_field_name = dem_opname;
33145796c8dcSSimon Schubert 	}
33155796c8dcSSimon Schubert       if (t_field_name && strcmp (t_field_name, name) == 0)
33165796c8dcSSimon Schubert 	{
3317cf7f2e2dSJohn Marino 	  int j;
3318cf7f2e2dSJohn Marino 	  int len = TYPE_FN_FIELDLIST_LENGTH (t, i);
33195796c8dcSSimon Schubert 	  struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
33205796c8dcSSimon Schubert 
33215796c8dcSSimon Schubert 	  check_stub_method_group (t, i);
33225796c8dcSSimon Schubert 
33235796c8dcSSimon Schubert 	  if (intype)
33245796c8dcSSimon Schubert 	    {
3325cf7f2e2dSJohn Marino 	      for (j = 0; j < len; ++j)
3326cf7f2e2dSJohn Marino 		{
3327cf7f2e2dSJohn Marino 		  if (compare_parameters (TYPE_FN_FIELD_TYPE (f, j), intype, 0)
3328*c50c785cSJohn Marino 		      || compare_parameters (TYPE_FN_FIELD_TYPE (f, j),
3329*c50c785cSJohn Marino 					     intype, 1))
33305796c8dcSSimon Schubert 		    break;
3331cf7f2e2dSJohn Marino 		}
3332cf7f2e2dSJohn Marino 
3333cf7f2e2dSJohn Marino 	      if (j == len)
3334*c50c785cSJohn Marino 		error (_("no member function matches "
3335*c50c785cSJohn Marino 			 "that type instantiation"));
33365796c8dcSSimon Schubert 	    }
33375796c8dcSSimon Schubert 	  else
3338cf7f2e2dSJohn Marino 	    {
3339cf7f2e2dSJohn Marino 	      int ii;
3340cf7f2e2dSJohn Marino 
3341cf7f2e2dSJohn Marino 	      j = -1;
3342*c50c785cSJohn Marino 	      for (ii = 0; ii < len; ++ii)
3343cf7f2e2dSJohn Marino 		{
3344cf7f2e2dSJohn Marino 		  /* Skip artificial methods.  This is necessary if,
3345cf7f2e2dSJohn Marino 		     for example, the user wants to "print
3346cf7f2e2dSJohn Marino 		     subclass::subclass" with only one user-defined
3347*c50c785cSJohn Marino 		     constructor.  There is no ambiguity in this case.
3348*c50c785cSJohn Marino 		     We are careful here to allow artificial methods
3349*c50c785cSJohn Marino 		     if they are the unique result.  */
3350cf7f2e2dSJohn Marino 		  if (TYPE_FN_FIELD_ARTIFICIAL (f, ii))
3351*c50c785cSJohn Marino 		    {
3352*c50c785cSJohn Marino 		      if (j == -1)
3353*c50c785cSJohn Marino 			j = ii;
3354cf7f2e2dSJohn Marino 		      continue;
3355*c50c785cSJohn Marino 		    }
3356cf7f2e2dSJohn Marino 
3357cf7f2e2dSJohn Marino 		  /* Desired method is ambiguous if more than one
3358cf7f2e2dSJohn Marino 		     method is defined.  */
3359*c50c785cSJohn Marino 		  if (j != -1 && !TYPE_FN_FIELD_ARTIFICIAL (f, j))
3360*c50c785cSJohn Marino 		    error (_("non-unique member `%s' requires "
3361*c50c785cSJohn Marino 			     "type instantiation"), name);
3362cf7f2e2dSJohn Marino 
3363cf7f2e2dSJohn Marino 		  j = ii;
3364cf7f2e2dSJohn Marino 		}
3365*c50c785cSJohn Marino 
3366*c50c785cSJohn Marino 	      if (j == -1)
3367*c50c785cSJohn Marino 		error (_("no matching member function"));
3368cf7f2e2dSJohn Marino 	    }
33695796c8dcSSimon Schubert 
33705796c8dcSSimon Schubert 	  if (TYPE_FN_FIELD_STATIC_P (f, j))
33715796c8dcSSimon Schubert 	    {
33725796c8dcSSimon Schubert 	      struct symbol *s =
33735796c8dcSSimon Schubert 		lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
33745796c8dcSSimon Schubert 			       0, VAR_DOMAIN, 0);
3375cf7f2e2dSJohn Marino 
33765796c8dcSSimon Schubert 	      if (s == NULL)
33775796c8dcSSimon Schubert 		return NULL;
33785796c8dcSSimon Schubert 
33795796c8dcSSimon Schubert 	      if (want_address)
33805796c8dcSSimon Schubert 		return value_addr (read_var_value (s, 0));
33815796c8dcSSimon Schubert 	      else
33825796c8dcSSimon Schubert 		return read_var_value (s, 0);
33835796c8dcSSimon Schubert 	    }
33845796c8dcSSimon Schubert 
33855796c8dcSSimon Schubert 	  if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
33865796c8dcSSimon Schubert 	    {
33875796c8dcSSimon Schubert 	      if (want_address)
33885796c8dcSSimon Schubert 		{
33895796c8dcSSimon Schubert 		  result = allocate_value
33905796c8dcSSimon Schubert 		    (lookup_methodptr_type (TYPE_FN_FIELD_TYPE (f, j)));
33915796c8dcSSimon Schubert 		  cplus_make_method_ptr (value_type (result),
33925796c8dcSSimon Schubert 					 value_contents_writeable (result),
33935796c8dcSSimon Schubert 					 TYPE_FN_FIELD_VOFFSET (f, j), 1);
33945796c8dcSSimon Schubert 		}
33955796c8dcSSimon Schubert 	      else if (noside == EVAL_AVOID_SIDE_EFFECTS)
33965796c8dcSSimon Schubert 		return allocate_value (TYPE_FN_FIELD_TYPE (f, j));
33975796c8dcSSimon Schubert 	      else
33985796c8dcSSimon Schubert 		error (_("Cannot reference virtual member function \"%s\""),
33995796c8dcSSimon Schubert 		       name);
34005796c8dcSSimon Schubert 	    }
34015796c8dcSSimon Schubert 	  else
34025796c8dcSSimon Schubert 	    {
34035796c8dcSSimon Schubert 	      struct symbol *s =
34045796c8dcSSimon Schubert 		lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
34055796c8dcSSimon Schubert 			       0, VAR_DOMAIN, 0);
3406cf7f2e2dSJohn Marino 
34075796c8dcSSimon Schubert 	      if (s == NULL)
34085796c8dcSSimon Schubert 		return NULL;
34095796c8dcSSimon Schubert 
34105796c8dcSSimon Schubert 	      v = read_var_value (s, 0);
34115796c8dcSSimon Schubert 	      if (!want_address)
34125796c8dcSSimon Schubert 		result = v;
34135796c8dcSSimon Schubert 	      else
34145796c8dcSSimon Schubert 		{
34155796c8dcSSimon Schubert 		  result = allocate_value (lookup_methodptr_type (TYPE_FN_FIELD_TYPE (f, j)));
34165796c8dcSSimon Schubert 		  cplus_make_method_ptr (value_type (result),
34175796c8dcSSimon Schubert 					 value_contents_writeable (result),
34185796c8dcSSimon Schubert 					 value_address (v), 0);
34195796c8dcSSimon Schubert 		}
34205796c8dcSSimon Schubert 	    }
34215796c8dcSSimon Schubert 	  return result;
34225796c8dcSSimon Schubert 	}
34235796c8dcSSimon Schubert     }
34245796c8dcSSimon Schubert   for (i = TYPE_N_BASECLASSES (t) - 1; i >= 0; i--)
34255796c8dcSSimon Schubert     {
34265796c8dcSSimon Schubert       struct value *v;
34275796c8dcSSimon Schubert       int base_offset;
34285796c8dcSSimon Schubert 
34295796c8dcSSimon Schubert       if (BASETYPE_VIA_VIRTUAL (t, i))
34305796c8dcSSimon Schubert 	base_offset = 0;
34315796c8dcSSimon Schubert       else
34325796c8dcSSimon Schubert 	base_offset = TYPE_BASECLASS_BITPOS (t, i) / 8;
34335796c8dcSSimon Schubert       v = value_struct_elt_for_reference (domain,
34345796c8dcSSimon Schubert 					  offset + base_offset,
34355796c8dcSSimon Schubert 					  TYPE_BASECLASS (t, i),
34365796c8dcSSimon Schubert 					  name, intype,
34375796c8dcSSimon Schubert 					  want_address, noside);
34385796c8dcSSimon Schubert       if (v)
34395796c8dcSSimon Schubert 	return v;
34405796c8dcSSimon Schubert     }
34415796c8dcSSimon Schubert 
34425796c8dcSSimon Schubert   /* As a last chance, pretend that CURTYPE is a namespace, and look
34435796c8dcSSimon Schubert      it up that way; this (frequently) works for types nested inside
34445796c8dcSSimon Schubert      classes.  */
34455796c8dcSSimon Schubert 
34465796c8dcSSimon Schubert   return value_maybe_namespace_elt (curtype, name,
34475796c8dcSSimon Schubert 				    want_address, noside);
34485796c8dcSSimon Schubert }
34495796c8dcSSimon Schubert 
34505796c8dcSSimon Schubert /* C++: Return the member NAME of the namespace given by the type
34515796c8dcSSimon Schubert    CURTYPE.  */
34525796c8dcSSimon Schubert 
34535796c8dcSSimon Schubert static struct value *
34545796c8dcSSimon Schubert value_namespace_elt (const struct type *curtype,
34555796c8dcSSimon Schubert 		     char *name, int want_address,
34565796c8dcSSimon Schubert 		     enum noside noside)
34575796c8dcSSimon Schubert {
34585796c8dcSSimon Schubert   struct value *retval = value_maybe_namespace_elt (curtype, name,
34595796c8dcSSimon Schubert 						    want_address,
34605796c8dcSSimon Schubert 						    noside);
34615796c8dcSSimon Schubert 
34625796c8dcSSimon Schubert   if (retval == NULL)
34635796c8dcSSimon Schubert     error (_("No symbol \"%s\" in namespace \"%s\"."),
34645796c8dcSSimon Schubert 	   name, TYPE_TAG_NAME (curtype));
34655796c8dcSSimon Schubert 
34665796c8dcSSimon Schubert   return retval;
34675796c8dcSSimon Schubert }
34685796c8dcSSimon Schubert 
34695796c8dcSSimon Schubert /* A helper function used by value_namespace_elt and
34705796c8dcSSimon Schubert    value_struct_elt_for_reference.  It looks up NAME inside the
34715796c8dcSSimon Schubert    context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
34725796c8dcSSimon Schubert    is a class and NAME refers to a type in CURTYPE itself (as opposed
34735796c8dcSSimon Schubert    to, say, some base class of CURTYPE).  */
34745796c8dcSSimon Schubert 
34755796c8dcSSimon Schubert static struct value *
34765796c8dcSSimon Schubert value_maybe_namespace_elt (const struct type *curtype,
34775796c8dcSSimon Schubert 			   char *name, int want_address,
34785796c8dcSSimon Schubert 			   enum noside noside)
34795796c8dcSSimon Schubert {
34805796c8dcSSimon Schubert   const char *namespace_name = TYPE_TAG_NAME (curtype);
34815796c8dcSSimon Schubert   struct symbol *sym;
34825796c8dcSSimon Schubert   struct value *result;
34835796c8dcSSimon Schubert 
3484cf7f2e2dSJohn Marino   sym = cp_lookup_symbol_namespace (namespace_name, name,
3485cf7f2e2dSJohn Marino 				    get_selected_block (0), VAR_DOMAIN);
3486cf7f2e2dSJohn Marino 
3487cf7f2e2dSJohn Marino   if (sym == NULL)
3488cf7f2e2dSJohn Marino     {
3489cf7f2e2dSJohn Marino       char *concatenated_name = alloca (strlen (namespace_name) + 2
3490cf7f2e2dSJohn Marino 					+ strlen (name) + 1);
3491cf7f2e2dSJohn Marino 
3492cf7f2e2dSJohn Marino       sprintf (concatenated_name, "%s::%s", namespace_name, name);
3493cf7f2e2dSJohn Marino       sym = lookup_static_symbol_aux (concatenated_name, VAR_DOMAIN);
3494cf7f2e2dSJohn Marino     }
34955796c8dcSSimon Schubert 
34965796c8dcSSimon Schubert   if (sym == NULL)
34975796c8dcSSimon Schubert     return NULL;
34985796c8dcSSimon Schubert   else if ((noside == EVAL_AVOID_SIDE_EFFECTS)
34995796c8dcSSimon Schubert 	   && (SYMBOL_CLASS (sym) == LOC_TYPEDEF))
35005796c8dcSSimon Schubert     result = allocate_value (SYMBOL_TYPE (sym));
35015796c8dcSSimon Schubert   else
35025796c8dcSSimon Schubert     result = value_of_variable (sym, get_selected_block (0));
35035796c8dcSSimon Schubert 
35045796c8dcSSimon Schubert   if (result && want_address)
35055796c8dcSSimon Schubert     result = value_addr (result);
35065796c8dcSSimon Schubert 
35075796c8dcSSimon Schubert   return result;
35085796c8dcSSimon Schubert }
35095796c8dcSSimon Schubert 
35105796c8dcSSimon Schubert /* Given a pointer value V, find the real (RTTI) type of the object it
35115796c8dcSSimon Schubert    points to.
35125796c8dcSSimon Schubert 
35135796c8dcSSimon Schubert    Other parameters FULL, TOP, USING_ENC as with value_rtti_type()
35145796c8dcSSimon Schubert    and refer to the values computed for the object pointed to.  */
35155796c8dcSSimon Schubert 
35165796c8dcSSimon Schubert struct type *
35175796c8dcSSimon Schubert value_rtti_target_type (struct value *v, int *full,
35185796c8dcSSimon Schubert 			int *top, int *using_enc)
35195796c8dcSSimon Schubert {
35205796c8dcSSimon Schubert   struct value *target;
35215796c8dcSSimon Schubert 
35225796c8dcSSimon Schubert   target = value_ind (v);
35235796c8dcSSimon Schubert 
35245796c8dcSSimon Schubert   return value_rtti_type (target, full, top, using_enc);
35255796c8dcSSimon Schubert }
35265796c8dcSSimon Schubert 
35275796c8dcSSimon Schubert /* Given a value pointed to by ARGP, check its real run-time type, and
35285796c8dcSSimon Schubert    if that is different from the enclosing type, create a new value
35295796c8dcSSimon Schubert    using the real run-time type as the enclosing type (and of the same
35305796c8dcSSimon Schubert    type as ARGP) and return it, with the embedded offset adjusted to
35315796c8dcSSimon Schubert    be the correct offset to the enclosed object.  RTYPE is the type,
35325796c8dcSSimon Schubert    and XFULL, XTOP, and XUSING_ENC are the other parameters, computed
35335796c8dcSSimon Schubert    by value_rtti_type().  If these are available, they can be supplied
35345796c8dcSSimon Schubert    and a second call to value_rtti_type() is avoided.  (Pass RTYPE ==
35355796c8dcSSimon Schubert    NULL if they're not available.  */
35365796c8dcSSimon Schubert 
35375796c8dcSSimon Schubert struct value *
35385796c8dcSSimon Schubert value_full_object (struct value *argp,
35395796c8dcSSimon Schubert 		   struct type *rtype,
35405796c8dcSSimon Schubert 		   int xfull, int xtop,
35415796c8dcSSimon Schubert 		   int xusing_enc)
35425796c8dcSSimon Schubert {
35435796c8dcSSimon Schubert   struct type *real_type;
35445796c8dcSSimon Schubert   int full = 0;
35455796c8dcSSimon Schubert   int top = -1;
35465796c8dcSSimon Schubert   int using_enc = 0;
35475796c8dcSSimon Schubert   struct value *new_val;
35485796c8dcSSimon Schubert 
35495796c8dcSSimon Schubert   if (rtype)
35505796c8dcSSimon Schubert     {
35515796c8dcSSimon Schubert       real_type = rtype;
35525796c8dcSSimon Schubert       full = xfull;
35535796c8dcSSimon Schubert       top = xtop;
35545796c8dcSSimon Schubert       using_enc = xusing_enc;
35555796c8dcSSimon Schubert     }
35565796c8dcSSimon Schubert   else
35575796c8dcSSimon Schubert     real_type = value_rtti_type (argp, &full, &top, &using_enc);
35585796c8dcSSimon Schubert 
35595796c8dcSSimon Schubert   /* If no RTTI data, or if object is already complete, do nothing.  */
35605796c8dcSSimon Schubert   if (!real_type || real_type == value_enclosing_type (argp))
35615796c8dcSSimon Schubert     return argp;
35625796c8dcSSimon Schubert 
35635796c8dcSSimon Schubert   /* If we have the full object, but for some reason the enclosing
35645796c8dcSSimon Schubert      type is wrong, set it.  */
35655796c8dcSSimon Schubert   /* pai: FIXME -- sounds iffy */
35665796c8dcSSimon Schubert   if (full)
35675796c8dcSSimon Schubert     {
3568*c50c785cSJohn Marino       argp = value_copy (argp);
3569*c50c785cSJohn Marino       set_value_enclosing_type (argp, real_type);
35705796c8dcSSimon Schubert       return argp;
35715796c8dcSSimon Schubert     }
35725796c8dcSSimon Schubert 
3573*c50c785cSJohn Marino   /* Check if object is in memory.  */
35745796c8dcSSimon Schubert   if (VALUE_LVAL (argp) != lval_memory)
35755796c8dcSSimon Schubert     {
3576*c50c785cSJohn Marino       warning (_("Couldn't retrieve complete object of RTTI "
3577*c50c785cSJohn Marino 		 "type %s; object may be in register(s)."),
35785796c8dcSSimon Schubert 	       TYPE_NAME (real_type));
35795796c8dcSSimon Schubert 
35805796c8dcSSimon Schubert       return argp;
35815796c8dcSSimon Schubert     }
35825796c8dcSSimon Schubert 
35835796c8dcSSimon Schubert   /* All other cases -- retrieve the complete object.  */
35845796c8dcSSimon Schubert   /* Go back by the computed top_offset from the beginning of the
35855796c8dcSSimon Schubert      object, adjusting for the embedded offset of argp if that's what
35865796c8dcSSimon Schubert      value_rtti_type used for its computation.  */
35875796c8dcSSimon Schubert   new_val = value_at_lazy (real_type, value_address (argp) - top +
35885796c8dcSSimon Schubert 			   (using_enc ? 0 : value_embedded_offset (argp)));
35895796c8dcSSimon Schubert   deprecated_set_value_type (new_val, value_type (argp));
35905796c8dcSSimon Schubert   set_value_embedded_offset (new_val, (using_enc
35915796c8dcSSimon Schubert 				       ? top + value_embedded_offset (argp)
35925796c8dcSSimon Schubert 				       : top));
35935796c8dcSSimon Schubert   return new_val;
35945796c8dcSSimon Schubert }
35955796c8dcSSimon Schubert 
35965796c8dcSSimon Schubert 
35975796c8dcSSimon Schubert /* Return the value of the local variable, if one exists.
35985796c8dcSSimon Schubert    Flag COMPLAIN signals an error if the request is made in an
35995796c8dcSSimon Schubert    inappropriate context.  */
36005796c8dcSSimon Schubert 
36015796c8dcSSimon Schubert struct value *
36025796c8dcSSimon Schubert value_of_local (const char *name, int complain)
36035796c8dcSSimon Schubert {
36045796c8dcSSimon Schubert   struct symbol *func, *sym;
36055796c8dcSSimon Schubert   struct block *b;
36065796c8dcSSimon Schubert   struct value * ret;
36075796c8dcSSimon Schubert   struct frame_info *frame;
36085796c8dcSSimon Schubert 
36095796c8dcSSimon Schubert   if (complain)
36105796c8dcSSimon Schubert     frame = get_selected_frame (_("no frame selected"));
36115796c8dcSSimon Schubert   else
36125796c8dcSSimon Schubert     {
36135796c8dcSSimon Schubert       frame = deprecated_safe_get_selected_frame ();
36145796c8dcSSimon Schubert       if (frame == 0)
36155796c8dcSSimon Schubert 	return 0;
36165796c8dcSSimon Schubert     }
36175796c8dcSSimon Schubert 
36185796c8dcSSimon Schubert   func = get_frame_function (frame);
36195796c8dcSSimon Schubert   if (!func)
36205796c8dcSSimon Schubert     {
36215796c8dcSSimon Schubert       if (complain)
36225796c8dcSSimon Schubert 	error (_("no `%s' in nameless context"), name);
36235796c8dcSSimon Schubert       else
36245796c8dcSSimon Schubert 	return 0;
36255796c8dcSSimon Schubert     }
36265796c8dcSSimon Schubert 
36275796c8dcSSimon Schubert   b = SYMBOL_BLOCK_VALUE (func);
36285796c8dcSSimon Schubert   if (dict_empty (BLOCK_DICT (b)))
36295796c8dcSSimon Schubert     {
36305796c8dcSSimon Schubert       if (complain)
36315796c8dcSSimon Schubert 	error (_("no args, no `%s'"), name);
36325796c8dcSSimon Schubert       else
36335796c8dcSSimon Schubert 	return 0;
36345796c8dcSSimon Schubert     }
36355796c8dcSSimon Schubert 
36365796c8dcSSimon Schubert   /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
36375796c8dcSSimon Schubert      symbol instead of the LOC_ARG one (if both exist).  */
3638cf7f2e2dSJohn Marino   sym = lookup_block_symbol (b, name, VAR_DOMAIN);
36395796c8dcSSimon Schubert   if (sym == NULL)
36405796c8dcSSimon Schubert     {
36415796c8dcSSimon Schubert       if (complain)
36425796c8dcSSimon Schubert 	error (_("current stack frame does not contain a variable named `%s'"),
36435796c8dcSSimon Schubert 	       name);
36445796c8dcSSimon Schubert       else
36455796c8dcSSimon Schubert 	return NULL;
36465796c8dcSSimon Schubert     }
36475796c8dcSSimon Schubert 
36485796c8dcSSimon Schubert   ret = read_var_value (sym, frame);
36495796c8dcSSimon Schubert   if (ret == 0 && complain)
36505796c8dcSSimon Schubert     error (_("`%s' argument unreadable"), name);
36515796c8dcSSimon Schubert   return ret;
36525796c8dcSSimon Schubert }
36535796c8dcSSimon Schubert 
36545796c8dcSSimon Schubert /* C++/Objective-C: return the value of the class instance variable,
36555796c8dcSSimon Schubert    if one exists.  Flag COMPLAIN signals an error if the request is
36565796c8dcSSimon Schubert    made in an inappropriate context.  */
36575796c8dcSSimon Schubert 
36585796c8dcSSimon Schubert struct value *
36595796c8dcSSimon Schubert value_of_this (int complain)
36605796c8dcSSimon Schubert {
36615796c8dcSSimon Schubert   if (!current_language->la_name_of_this)
36625796c8dcSSimon Schubert     return 0;
36635796c8dcSSimon Schubert   return value_of_local (current_language->la_name_of_this, complain);
36645796c8dcSSimon Schubert }
36655796c8dcSSimon Schubert 
36665796c8dcSSimon Schubert /* Create a slice (sub-string, sub-array) of ARRAY, that is LENGTH
36675796c8dcSSimon Schubert    elements long, starting at LOWBOUND.  The result has the same lower
36685796c8dcSSimon Schubert    bound as the original ARRAY.  */
36695796c8dcSSimon Schubert 
36705796c8dcSSimon Schubert struct value *
36715796c8dcSSimon Schubert value_slice (struct value *array, int lowbound, int length)
36725796c8dcSSimon Schubert {
36735796c8dcSSimon Schubert   struct type *slice_range_type, *slice_type, *range_type;
36745796c8dcSSimon Schubert   LONGEST lowerbound, upperbound;
36755796c8dcSSimon Schubert   struct value *slice;
36765796c8dcSSimon Schubert   struct type *array_type;
36775796c8dcSSimon Schubert 
36785796c8dcSSimon Schubert   array_type = check_typedef (value_type (array));
36795796c8dcSSimon Schubert   if (TYPE_CODE (array_type) != TYPE_CODE_ARRAY
36805796c8dcSSimon Schubert       && TYPE_CODE (array_type) != TYPE_CODE_STRING
36815796c8dcSSimon Schubert       && TYPE_CODE (array_type) != TYPE_CODE_BITSTRING)
36825796c8dcSSimon Schubert     error (_("cannot take slice of non-array"));
36835796c8dcSSimon Schubert 
36845796c8dcSSimon Schubert   range_type = TYPE_INDEX_TYPE (array_type);
36855796c8dcSSimon Schubert   if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0)
36865796c8dcSSimon Schubert     error (_("slice from bad array or bitstring"));
36875796c8dcSSimon Schubert 
36885796c8dcSSimon Schubert   if (lowbound < lowerbound || length < 0
36895796c8dcSSimon Schubert       || lowbound + length - 1 > upperbound)
36905796c8dcSSimon Schubert     error (_("slice out of range"));
36915796c8dcSSimon Schubert 
36925796c8dcSSimon Schubert   /* FIXME-type-allocation: need a way to free this type when we are
36935796c8dcSSimon Schubert      done with it.  */
36945796c8dcSSimon Schubert   slice_range_type = create_range_type ((struct type *) NULL,
36955796c8dcSSimon Schubert 					TYPE_TARGET_TYPE (range_type),
36965796c8dcSSimon Schubert 					lowbound,
36975796c8dcSSimon Schubert 					lowbound + length - 1);
36985796c8dcSSimon Schubert   if (TYPE_CODE (array_type) == TYPE_CODE_BITSTRING)
36995796c8dcSSimon Schubert     {
37005796c8dcSSimon Schubert       int i;
37015796c8dcSSimon Schubert 
37025796c8dcSSimon Schubert       slice_type = create_set_type ((struct type *) NULL,
37035796c8dcSSimon Schubert 				    slice_range_type);
37045796c8dcSSimon Schubert       TYPE_CODE (slice_type) = TYPE_CODE_BITSTRING;
37055796c8dcSSimon Schubert       slice = value_zero (slice_type, not_lval);
37065796c8dcSSimon Schubert 
37075796c8dcSSimon Schubert       for (i = 0; i < length; i++)
37085796c8dcSSimon Schubert 	{
37095796c8dcSSimon Schubert 	  int element = value_bit_index (array_type,
37105796c8dcSSimon Schubert 					 value_contents (array),
37115796c8dcSSimon Schubert 					 lowbound + i);
3712cf7f2e2dSJohn Marino 
37135796c8dcSSimon Schubert 	  if (element < 0)
37145796c8dcSSimon Schubert 	    error (_("internal error accessing bitstring"));
37155796c8dcSSimon Schubert 	  else if (element > 0)
37165796c8dcSSimon Schubert 	    {
37175796c8dcSSimon Schubert 	      int j = i % TARGET_CHAR_BIT;
3718cf7f2e2dSJohn Marino 
37195796c8dcSSimon Schubert 	      if (gdbarch_bits_big_endian (get_type_arch (array_type)))
37205796c8dcSSimon Schubert 		j = TARGET_CHAR_BIT - 1 - j;
37215796c8dcSSimon Schubert 	      value_contents_raw (slice)[i / TARGET_CHAR_BIT] |= (1 << j);
37225796c8dcSSimon Schubert 	    }
37235796c8dcSSimon Schubert 	}
37245796c8dcSSimon Schubert       /* We should set the address, bitssize, and bitspos, so the
37255796c8dcSSimon Schubert          slice can be used on the LHS, but that may require extensions
37265796c8dcSSimon Schubert          to value_assign.  For now, just leave as a non_lval.
37275796c8dcSSimon Schubert          FIXME.  */
37285796c8dcSSimon Schubert     }
37295796c8dcSSimon Schubert   else
37305796c8dcSSimon Schubert     {
37315796c8dcSSimon Schubert       struct type *element_type = TYPE_TARGET_TYPE (array_type);
37325796c8dcSSimon Schubert       LONGEST offset =
37335796c8dcSSimon Schubert 	(lowbound - lowerbound) * TYPE_LENGTH (check_typedef (element_type));
37345796c8dcSSimon Schubert 
37355796c8dcSSimon Schubert       slice_type = create_array_type ((struct type *) NULL,
37365796c8dcSSimon Schubert 				      element_type,
37375796c8dcSSimon Schubert 				      slice_range_type);
37385796c8dcSSimon Schubert       TYPE_CODE (slice_type) = TYPE_CODE (array_type);
37395796c8dcSSimon Schubert 
37405796c8dcSSimon Schubert       if (VALUE_LVAL (array) == lval_memory && value_lazy (array))
37415796c8dcSSimon Schubert 	slice = allocate_value_lazy (slice_type);
37425796c8dcSSimon Schubert       else
37435796c8dcSSimon Schubert 	{
37445796c8dcSSimon Schubert 	  slice = allocate_value (slice_type);
3745*c50c785cSJohn Marino 	  value_contents_copy (slice, 0, array, offset,
37465796c8dcSSimon Schubert 			       TYPE_LENGTH (slice_type));
37475796c8dcSSimon Schubert 	}
37485796c8dcSSimon Schubert 
37495796c8dcSSimon Schubert       set_value_component_location (slice, array);
37505796c8dcSSimon Schubert       VALUE_FRAME_ID (slice) = VALUE_FRAME_ID (array);
37515796c8dcSSimon Schubert       set_value_offset (slice, value_offset (array) + offset);
37525796c8dcSSimon Schubert     }
37535796c8dcSSimon Schubert   return slice;
37545796c8dcSSimon Schubert }
37555796c8dcSSimon Schubert 
37565796c8dcSSimon Schubert /* Create a value for a FORTRAN complex number.  Currently most of the
37575796c8dcSSimon Schubert    time values are coerced to COMPLEX*16 (i.e. a complex number
37585796c8dcSSimon Schubert    composed of 2 doubles.  This really should be a smarter routine
37595796c8dcSSimon Schubert    that figures out precision inteligently as opposed to assuming
37605796c8dcSSimon Schubert    doubles.  FIXME: fmb  */
37615796c8dcSSimon Schubert 
37625796c8dcSSimon Schubert struct value *
37635796c8dcSSimon Schubert value_literal_complex (struct value *arg1,
37645796c8dcSSimon Schubert 		       struct value *arg2,
37655796c8dcSSimon Schubert 		       struct type *type)
37665796c8dcSSimon Schubert {
37675796c8dcSSimon Schubert   struct value *val;
37685796c8dcSSimon Schubert   struct type *real_type = TYPE_TARGET_TYPE (type);
37695796c8dcSSimon Schubert 
37705796c8dcSSimon Schubert   val = allocate_value (type);
37715796c8dcSSimon Schubert   arg1 = value_cast (real_type, arg1);
37725796c8dcSSimon Schubert   arg2 = value_cast (real_type, arg2);
37735796c8dcSSimon Schubert 
37745796c8dcSSimon Schubert   memcpy (value_contents_raw (val),
37755796c8dcSSimon Schubert 	  value_contents (arg1), TYPE_LENGTH (real_type));
37765796c8dcSSimon Schubert   memcpy (value_contents_raw (val) + TYPE_LENGTH (real_type),
37775796c8dcSSimon Schubert 	  value_contents (arg2), TYPE_LENGTH (real_type));
37785796c8dcSSimon Schubert   return val;
37795796c8dcSSimon Schubert }
37805796c8dcSSimon Schubert 
37815796c8dcSSimon Schubert /* Cast a value into the appropriate complex data type.  */
37825796c8dcSSimon Schubert 
37835796c8dcSSimon Schubert static struct value *
37845796c8dcSSimon Schubert cast_into_complex (struct type *type, struct value *val)
37855796c8dcSSimon Schubert {
37865796c8dcSSimon Schubert   struct type *real_type = TYPE_TARGET_TYPE (type);
37875796c8dcSSimon Schubert 
37885796c8dcSSimon Schubert   if (TYPE_CODE (value_type (val)) == TYPE_CODE_COMPLEX)
37895796c8dcSSimon Schubert     {
37905796c8dcSSimon Schubert       struct type *val_real_type = TYPE_TARGET_TYPE (value_type (val));
37915796c8dcSSimon Schubert       struct value *re_val = allocate_value (val_real_type);
37925796c8dcSSimon Schubert       struct value *im_val = allocate_value (val_real_type);
37935796c8dcSSimon Schubert 
37945796c8dcSSimon Schubert       memcpy (value_contents_raw (re_val),
37955796c8dcSSimon Schubert 	      value_contents (val), TYPE_LENGTH (val_real_type));
37965796c8dcSSimon Schubert       memcpy (value_contents_raw (im_val),
37975796c8dcSSimon Schubert 	      value_contents (val) + TYPE_LENGTH (val_real_type),
37985796c8dcSSimon Schubert 	      TYPE_LENGTH (val_real_type));
37995796c8dcSSimon Schubert 
38005796c8dcSSimon Schubert       return value_literal_complex (re_val, im_val, type);
38015796c8dcSSimon Schubert     }
38025796c8dcSSimon Schubert   else if (TYPE_CODE (value_type (val)) == TYPE_CODE_FLT
38035796c8dcSSimon Schubert 	   || TYPE_CODE (value_type (val)) == TYPE_CODE_INT)
38045796c8dcSSimon Schubert     return value_literal_complex (val,
38055796c8dcSSimon Schubert 				  value_zero (real_type, not_lval),
38065796c8dcSSimon Schubert 				  type);
38075796c8dcSSimon Schubert   else
38085796c8dcSSimon Schubert     error (_("cannot cast non-number to complex"));
38095796c8dcSSimon Schubert }
38105796c8dcSSimon Schubert 
38115796c8dcSSimon Schubert void
38125796c8dcSSimon Schubert _initialize_valops (void)
38135796c8dcSSimon Schubert {
38145796c8dcSSimon Schubert   add_setshow_boolean_cmd ("overload-resolution", class_support,
38155796c8dcSSimon Schubert 			   &overload_resolution, _("\
38165796c8dcSSimon Schubert Set overload resolution in evaluating C++ functions."), _("\
38175796c8dcSSimon Schubert Show overload resolution in evaluating C++ functions."),
38185796c8dcSSimon Schubert 			   NULL, NULL,
38195796c8dcSSimon Schubert 			   show_overload_resolution,
38205796c8dcSSimon Schubert 			   &setlist, &showlist);
38215796c8dcSSimon Schubert   overload_resolution = 1;
38225796c8dcSSimon Schubert }
3823