xref: /dflybsd-src/contrib/gdb-7/gdb/valops.c (revision cf7f2e2d389e8012d562650bd94d7e433f449d6e)
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*cf7f2e2dSJohn Marino    2008, 2009, 2010 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"
415796c8dcSSimon Schubert 
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"
495796c8dcSSimon Schubert 
505796c8dcSSimon Schubert extern int overload_debug;
515796c8dcSSimon Schubert /* Local functions.  */
525796c8dcSSimon Schubert 
535796c8dcSSimon Schubert static int typecmp (int staticp, int varargs, int nargs,
545796c8dcSSimon Schubert 		    struct field t1[], struct value *t2[]);
555796c8dcSSimon Schubert 
56*cf7f2e2dSJohn Marino static struct value *search_struct_field (const char *, struct value *,
575796c8dcSSimon Schubert 					  int, struct type *, int);
585796c8dcSSimon Schubert 
59*cf7f2e2dSJohn Marino static struct value *search_struct_method (const char *, struct value **,
605796c8dcSSimon Schubert 					   struct value **,
615796c8dcSSimon Schubert 					   int, int *, struct type *);
625796c8dcSSimon Schubert 
635796c8dcSSimon Schubert static int find_oload_champ_namespace (struct type **, int,
645796c8dcSSimon Schubert 				       const char *, const char *,
655796c8dcSSimon Schubert 				       struct symbol ***,
66*cf7f2e2dSJohn Marino 				       struct badness_vector **,
67*cf7f2e2dSJohn Marino 				       const int no_adl);
685796c8dcSSimon Schubert 
695796c8dcSSimon Schubert static
705796c8dcSSimon Schubert int find_oload_champ_namespace_loop (struct type **, int,
715796c8dcSSimon Schubert 				     const char *, const char *,
725796c8dcSSimon Schubert 				     int, struct symbol ***,
73*cf7f2e2dSJohn Marino 				     struct badness_vector **, int *,
74*cf7f2e2dSJohn Marino 				     const int no_adl);
755796c8dcSSimon Schubert 
765796c8dcSSimon Schubert static int find_oload_champ (struct type **, int, int, int,
775796c8dcSSimon Schubert 			     struct fn_field *, struct symbol **,
785796c8dcSSimon Schubert 			     struct badness_vector **);
795796c8dcSSimon Schubert 
805796c8dcSSimon Schubert static int oload_method_static (int, struct fn_field *, int);
815796c8dcSSimon Schubert 
825796c8dcSSimon Schubert enum oload_classification { STANDARD, NON_STANDARD, INCOMPATIBLE };
835796c8dcSSimon Schubert 
845796c8dcSSimon Schubert static enum
855796c8dcSSimon Schubert oload_classification classify_oload_match (struct badness_vector *,
865796c8dcSSimon Schubert 					   int, int);
875796c8dcSSimon Schubert 
885796c8dcSSimon Schubert static struct value *value_struct_elt_for_reference (struct type *,
895796c8dcSSimon Schubert 						     int, struct type *,
905796c8dcSSimon Schubert 						     char *,
915796c8dcSSimon Schubert 						     struct type *,
925796c8dcSSimon Schubert 						     int, enum noside);
935796c8dcSSimon Schubert 
945796c8dcSSimon Schubert static struct value *value_namespace_elt (const struct type *,
955796c8dcSSimon Schubert 					  char *, int , enum noside);
965796c8dcSSimon Schubert 
975796c8dcSSimon Schubert static struct value *value_maybe_namespace_elt (const struct type *,
985796c8dcSSimon Schubert 						char *, int,
995796c8dcSSimon Schubert 						enum noside);
1005796c8dcSSimon Schubert 
1015796c8dcSSimon Schubert static CORE_ADDR allocate_space_in_inferior (int);
1025796c8dcSSimon Schubert 
1035796c8dcSSimon Schubert static struct value *cast_into_complex (struct type *, struct value *);
1045796c8dcSSimon Schubert 
105*cf7f2e2dSJohn Marino static struct fn_field *find_method_list (struct value **, const char *,
1065796c8dcSSimon Schubert 					  int, struct type *, int *,
1075796c8dcSSimon Schubert 					  struct type **, int *);
1085796c8dcSSimon Schubert 
1095796c8dcSSimon Schubert void _initialize_valops (void);
1105796c8dcSSimon Schubert 
1115796c8dcSSimon Schubert #if 0
1125796c8dcSSimon Schubert /* Flag for whether we want to abandon failed expression evals by
1135796c8dcSSimon Schubert    default.  */
1145796c8dcSSimon Schubert 
1155796c8dcSSimon Schubert static int auto_abandon = 0;
1165796c8dcSSimon Schubert #endif
1175796c8dcSSimon Schubert 
1185796c8dcSSimon Schubert int overload_resolution = 0;
1195796c8dcSSimon Schubert static void
1205796c8dcSSimon Schubert show_overload_resolution (struct ui_file *file, int from_tty,
1215796c8dcSSimon Schubert 			  struct cmd_list_element *c,
1225796c8dcSSimon Schubert 			  const char *value)
1235796c8dcSSimon Schubert {
1245796c8dcSSimon Schubert   fprintf_filtered (file, _("\
1255796c8dcSSimon Schubert Overload resolution in evaluating C++ functions is %s.\n"),
1265796c8dcSSimon Schubert 		    value);
1275796c8dcSSimon Schubert }
1285796c8dcSSimon Schubert 
1295796c8dcSSimon Schubert /* Find the address of function name NAME in the inferior.  If OBJF_P
1305796c8dcSSimon Schubert    is non-NULL, *OBJF_P will be set to the OBJFILE where the function
1315796c8dcSSimon Schubert    is defined.  */
1325796c8dcSSimon Schubert 
1335796c8dcSSimon Schubert struct value *
1345796c8dcSSimon Schubert find_function_in_inferior (const char *name, struct objfile **objf_p)
1355796c8dcSSimon Schubert {
1365796c8dcSSimon Schubert   struct symbol *sym;
137*cf7f2e2dSJohn Marino 
1385796c8dcSSimon Schubert   sym = lookup_symbol (name, 0, VAR_DOMAIN, 0);
1395796c8dcSSimon Schubert   if (sym != NULL)
1405796c8dcSSimon Schubert     {
1415796c8dcSSimon Schubert       if (SYMBOL_CLASS (sym) != LOC_BLOCK)
1425796c8dcSSimon Schubert 	{
1435796c8dcSSimon Schubert 	  error (_("\"%s\" exists in this program but is not a function."),
1445796c8dcSSimon Schubert 		 name);
1455796c8dcSSimon Schubert 	}
1465796c8dcSSimon Schubert 
1475796c8dcSSimon Schubert       if (objf_p)
1485796c8dcSSimon Schubert 	*objf_p = SYMBOL_SYMTAB (sym)->objfile;
1495796c8dcSSimon Schubert 
1505796c8dcSSimon Schubert       return value_of_variable (sym, NULL);
1515796c8dcSSimon Schubert     }
1525796c8dcSSimon Schubert   else
1535796c8dcSSimon Schubert     {
1545796c8dcSSimon Schubert       struct minimal_symbol *msymbol =
1555796c8dcSSimon Schubert 	lookup_minimal_symbol (name, NULL, NULL);
156*cf7f2e2dSJohn Marino 
1575796c8dcSSimon Schubert       if (msymbol != NULL)
1585796c8dcSSimon Schubert 	{
1595796c8dcSSimon Schubert 	  struct objfile *objfile = msymbol_objfile (msymbol);
1605796c8dcSSimon Schubert 	  struct gdbarch *gdbarch = get_objfile_arch (objfile);
1615796c8dcSSimon Schubert 
1625796c8dcSSimon Schubert 	  struct type *type;
1635796c8dcSSimon Schubert 	  CORE_ADDR maddr;
1645796c8dcSSimon Schubert 	  type = lookup_pointer_type (builtin_type (gdbarch)->builtin_char);
1655796c8dcSSimon Schubert 	  type = lookup_function_type (type);
1665796c8dcSSimon Schubert 	  type = lookup_pointer_type (type);
1675796c8dcSSimon Schubert 	  maddr = SYMBOL_VALUE_ADDRESS (msymbol);
1685796c8dcSSimon Schubert 
1695796c8dcSSimon Schubert 	  if (objf_p)
1705796c8dcSSimon Schubert 	    *objf_p = objfile;
1715796c8dcSSimon Schubert 
1725796c8dcSSimon Schubert 	  return value_from_pointer (type, maddr);
1735796c8dcSSimon Schubert 	}
1745796c8dcSSimon Schubert       else
1755796c8dcSSimon Schubert 	{
1765796c8dcSSimon Schubert 	  if (!target_has_execution)
1775796c8dcSSimon Schubert 	    error (_("evaluation of this expression requires the target program to be active"));
1785796c8dcSSimon Schubert 	  else
1795796c8dcSSimon Schubert 	    error (_("evaluation of this expression requires the program to have a function \"%s\"."), name);
1805796c8dcSSimon Schubert 	}
1815796c8dcSSimon Schubert     }
1825796c8dcSSimon Schubert }
1835796c8dcSSimon Schubert 
1845796c8dcSSimon Schubert /* Allocate NBYTES of space in the inferior using the inferior's
1855796c8dcSSimon Schubert    malloc and return a value that is a pointer to the allocated
1865796c8dcSSimon Schubert    space.  */
1875796c8dcSSimon Schubert 
1885796c8dcSSimon Schubert struct value *
1895796c8dcSSimon Schubert value_allocate_space_in_inferior (int len)
1905796c8dcSSimon Schubert {
1915796c8dcSSimon Schubert   struct objfile *objf;
1925796c8dcSSimon Schubert   struct value *val = find_function_in_inferior ("malloc", &objf);
1935796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_objfile_arch (objf);
1945796c8dcSSimon Schubert   struct value *blocklen;
1955796c8dcSSimon Schubert 
1965796c8dcSSimon Schubert   blocklen = value_from_longest (builtin_type (gdbarch)->builtin_int, len);
1975796c8dcSSimon Schubert   val = call_function_by_hand (val, 1, &blocklen);
1985796c8dcSSimon Schubert   if (value_logical_not (val))
1995796c8dcSSimon Schubert     {
2005796c8dcSSimon Schubert       if (!target_has_execution)
2015796c8dcSSimon Schubert 	error (_("No memory available to program now: you need to start the target first"));
2025796c8dcSSimon Schubert       else
2035796c8dcSSimon Schubert 	error (_("No memory available to program: call to malloc failed"));
2045796c8dcSSimon Schubert     }
2055796c8dcSSimon Schubert   return val;
2065796c8dcSSimon Schubert }
2075796c8dcSSimon Schubert 
2085796c8dcSSimon Schubert static CORE_ADDR
2095796c8dcSSimon Schubert allocate_space_in_inferior (int len)
2105796c8dcSSimon Schubert {
2115796c8dcSSimon Schubert   return value_as_long (value_allocate_space_in_inferior (len));
2125796c8dcSSimon Schubert }
2135796c8dcSSimon Schubert 
2145796c8dcSSimon Schubert /* Cast struct value VAL to type TYPE and return as a value.
2155796c8dcSSimon Schubert    Both type and val must be of TYPE_CODE_STRUCT or TYPE_CODE_UNION
2165796c8dcSSimon Schubert    for this to work.  Typedef to one of the codes is permitted.
2175796c8dcSSimon Schubert    Returns NULL if the cast is neither an upcast nor a downcast.  */
2185796c8dcSSimon Schubert 
2195796c8dcSSimon Schubert static struct value *
2205796c8dcSSimon Schubert value_cast_structs (struct type *type, struct value *v2)
2215796c8dcSSimon Schubert {
2225796c8dcSSimon Schubert   struct type *t1;
2235796c8dcSSimon Schubert   struct type *t2;
2245796c8dcSSimon Schubert   struct value *v;
2255796c8dcSSimon Schubert 
2265796c8dcSSimon Schubert   gdb_assert (type != NULL && v2 != NULL);
2275796c8dcSSimon Schubert 
2285796c8dcSSimon Schubert   t1 = check_typedef (type);
2295796c8dcSSimon Schubert   t2 = check_typedef (value_type (v2));
2305796c8dcSSimon Schubert 
2315796c8dcSSimon Schubert   /* Check preconditions.  */
2325796c8dcSSimon Schubert   gdb_assert ((TYPE_CODE (t1) == TYPE_CODE_STRUCT
2335796c8dcSSimon Schubert 	       || TYPE_CODE (t1) == TYPE_CODE_UNION)
2345796c8dcSSimon Schubert 	      && !!"Precondition is that type is of STRUCT or UNION kind.");
2355796c8dcSSimon Schubert   gdb_assert ((TYPE_CODE (t2) == TYPE_CODE_STRUCT
2365796c8dcSSimon Schubert 	       || TYPE_CODE (t2) == TYPE_CODE_UNION)
2375796c8dcSSimon Schubert 	      && !!"Precondition is that value is of STRUCT or UNION kind");
2385796c8dcSSimon Schubert 
239*cf7f2e2dSJohn Marino   if (TYPE_NAME (t1) != NULL
240*cf7f2e2dSJohn Marino       && TYPE_NAME (t2) != NULL
241*cf7f2e2dSJohn Marino       && !strcmp (TYPE_NAME (t1), TYPE_NAME (t2)))
242*cf7f2e2dSJohn Marino     return NULL;
243*cf7f2e2dSJohn Marino 
2445796c8dcSSimon Schubert   /* Upcasting: look in the type of the source to see if it contains the
2455796c8dcSSimon Schubert      type of the target as a superclass.  If so, we'll need to
2465796c8dcSSimon Schubert      offset the pointer rather than just change its type.  */
2475796c8dcSSimon Schubert   if (TYPE_NAME (t1) != NULL)
2485796c8dcSSimon Schubert     {
2495796c8dcSSimon Schubert       v = search_struct_field (type_name_no_tag (t1),
2505796c8dcSSimon Schubert 			       v2, 0, t2, 1);
2515796c8dcSSimon Schubert       if (v)
2525796c8dcSSimon Schubert 	return v;
2535796c8dcSSimon Schubert     }
2545796c8dcSSimon Schubert 
2555796c8dcSSimon Schubert   /* Downcasting: look in the type of the target to see if it contains the
2565796c8dcSSimon Schubert      type of the source as a superclass.  If so, we'll need to
257*cf7f2e2dSJohn Marino      offset the pointer rather than just change its type.  */
2585796c8dcSSimon Schubert   if (TYPE_NAME (t2) != NULL)
2595796c8dcSSimon Schubert     {
260*cf7f2e2dSJohn Marino       /* Try downcasting using the run-time type of the value.  */
261*cf7f2e2dSJohn Marino       int full, top, using_enc;
262*cf7f2e2dSJohn Marino       struct type *real_type;
263*cf7f2e2dSJohn Marino 
264*cf7f2e2dSJohn Marino       real_type = value_rtti_type (v2, &full, &top, &using_enc);
265*cf7f2e2dSJohn Marino       if (real_type)
266*cf7f2e2dSJohn Marino 	{
267*cf7f2e2dSJohn Marino 	  v = value_full_object (v2, real_type, full, top, using_enc);
268*cf7f2e2dSJohn Marino 	  v = value_at_lazy (real_type, value_address (v));
269*cf7f2e2dSJohn Marino 
270*cf7f2e2dSJohn Marino 	  /* We might be trying to cast to the outermost enclosing
271*cf7f2e2dSJohn Marino 	     type, in which case search_struct_field won't work.  */
272*cf7f2e2dSJohn Marino 	  if (TYPE_NAME (real_type) != NULL
273*cf7f2e2dSJohn Marino 	      && !strcmp (TYPE_NAME (real_type), TYPE_NAME (t1)))
274*cf7f2e2dSJohn Marino 	    return v;
275*cf7f2e2dSJohn Marino 
276*cf7f2e2dSJohn Marino 	  v = search_struct_field (type_name_no_tag (t2), v, 0, real_type, 1);
277*cf7f2e2dSJohn Marino 	  if (v)
278*cf7f2e2dSJohn Marino 	    return v;
279*cf7f2e2dSJohn Marino 	}
280*cf7f2e2dSJohn Marino 
281*cf7f2e2dSJohn Marino       /* Try downcasting using information from the destination type
282*cf7f2e2dSJohn Marino 	 T2.  This wouldn't work properly for classes with virtual
283*cf7f2e2dSJohn Marino 	 bases, but those were handled above.  */
2845796c8dcSSimon Schubert       v = search_struct_field (type_name_no_tag (t2),
2855796c8dcSSimon Schubert 			       value_zero (t1, not_lval), 0, t1, 1);
2865796c8dcSSimon Schubert       if (v)
2875796c8dcSSimon Schubert 	{
2885796c8dcSSimon Schubert 	  /* Downcasting is possible (t1 is superclass of v2).  */
2895796c8dcSSimon Schubert 	  CORE_ADDR addr2 = value_address (v2);
290*cf7f2e2dSJohn Marino 
2915796c8dcSSimon Schubert 	  addr2 -= value_address (v) + value_embedded_offset (v);
2925796c8dcSSimon Schubert 	  return value_at (type, addr2);
2935796c8dcSSimon Schubert 	}
2945796c8dcSSimon Schubert     }
2955796c8dcSSimon Schubert 
2965796c8dcSSimon Schubert   return NULL;
2975796c8dcSSimon Schubert }
2985796c8dcSSimon Schubert 
2995796c8dcSSimon Schubert /* Cast one pointer or reference type to another.  Both TYPE and
3005796c8dcSSimon Schubert    the type of ARG2 should be pointer types, or else both should be
3015796c8dcSSimon Schubert    reference types.  Returns the new pointer or reference.  */
3025796c8dcSSimon Schubert 
3035796c8dcSSimon Schubert struct value *
3045796c8dcSSimon Schubert value_cast_pointers (struct type *type, struct value *arg2)
3055796c8dcSSimon Schubert {
3065796c8dcSSimon Schubert   struct type *type1 = check_typedef (type);
3075796c8dcSSimon Schubert   struct type *type2 = check_typedef (value_type (arg2));
308*cf7f2e2dSJohn Marino   struct type *t1 = check_typedef (TYPE_TARGET_TYPE (type1));
3095796c8dcSSimon Schubert   struct type *t2 = check_typedef (TYPE_TARGET_TYPE (type2));
3105796c8dcSSimon Schubert 
3115796c8dcSSimon Schubert   if (TYPE_CODE (t1) == TYPE_CODE_STRUCT
3125796c8dcSSimon Schubert       && TYPE_CODE (t2) == TYPE_CODE_STRUCT
3135796c8dcSSimon Schubert       && !value_logical_not (arg2))
3145796c8dcSSimon Schubert     {
3155796c8dcSSimon Schubert       struct value *v2;
3165796c8dcSSimon Schubert 
3175796c8dcSSimon Schubert       if (TYPE_CODE (type2) == TYPE_CODE_REF)
3185796c8dcSSimon Schubert 	v2 = coerce_ref (arg2);
3195796c8dcSSimon Schubert       else
3205796c8dcSSimon Schubert 	v2 = value_ind (arg2);
3215796c8dcSSimon Schubert       gdb_assert (TYPE_CODE (check_typedef (value_type (v2))) == TYPE_CODE_STRUCT
3225796c8dcSSimon Schubert 		  && !!"Why did coercion fail?");
3235796c8dcSSimon Schubert       v2 = value_cast_structs (t1, v2);
3245796c8dcSSimon Schubert       /* At this point we have what we can have, un-dereference if needed.  */
3255796c8dcSSimon Schubert       if (v2)
3265796c8dcSSimon Schubert 	{
3275796c8dcSSimon Schubert 	  struct value *v = value_addr (v2);
328*cf7f2e2dSJohn Marino 
3295796c8dcSSimon Schubert 	  deprecated_set_value_type (v, type);
3305796c8dcSSimon Schubert 	  return v;
3315796c8dcSSimon Schubert 	}
3325796c8dcSSimon Schubert    }
3335796c8dcSSimon Schubert 
3345796c8dcSSimon Schubert   /* No superclass found, just change the pointer type.  */
3355796c8dcSSimon Schubert   arg2 = value_copy (arg2);
3365796c8dcSSimon Schubert   deprecated_set_value_type (arg2, type);
3375796c8dcSSimon Schubert   arg2 = value_change_enclosing_type (arg2, type);
3385796c8dcSSimon Schubert   set_value_pointed_to_offset (arg2, 0);	/* pai: chk_val */
3395796c8dcSSimon Schubert   return arg2;
3405796c8dcSSimon Schubert }
3415796c8dcSSimon Schubert 
3425796c8dcSSimon Schubert /* Cast value ARG2 to type TYPE and return as a value.
3435796c8dcSSimon Schubert    More general than a C cast: accepts any two types of the same length,
3445796c8dcSSimon Schubert    and if ARG2 is an lvalue it can be cast into anything at all.  */
3455796c8dcSSimon Schubert /* In C++, casts may change pointer or object representations.  */
3465796c8dcSSimon Schubert 
3475796c8dcSSimon Schubert struct value *
3485796c8dcSSimon Schubert value_cast (struct type *type, struct value *arg2)
3495796c8dcSSimon Schubert {
3505796c8dcSSimon Schubert   enum type_code code1;
3515796c8dcSSimon Schubert   enum type_code code2;
3525796c8dcSSimon Schubert   int scalar;
3535796c8dcSSimon Schubert   struct type *type2;
3545796c8dcSSimon Schubert 
3555796c8dcSSimon Schubert   int convert_to_boolean = 0;
3565796c8dcSSimon Schubert 
3575796c8dcSSimon Schubert   if (value_type (arg2) == type)
3585796c8dcSSimon Schubert     return arg2;
3595796c8dcSSimon Schubert 
3605796c8dcSSimon Schubert   code1 = TYPE_CODE (check_typedef (type));
3615796c8dcSSimon Schubert 
3625796c8dcSSimon Schubert   /* Check if we are casting struct reference to struct reference.  */
3635796c8dcSSimon Schubert   if (code1 == TYPE_CODE_REF)
3645796c8dcSSimon Schubert     {
3655796c8dcSSimon Schubert       /* We dereference type; then we recurse and finally
3665796c8dcSSimon Schubert          we generate value of the given reference. Nothing wrong with
3675796c8dcSSimon Schubert 	 that.  */
3685796c8dcSSimon Schubert       struct type *t1 = check_typedef (type);
3695796c8dcSSimon Schubert       struct type *dereftype = check_typedef (TYPE_TARGET_TYPE (t1));
3705796c8dcSSimon Schubert       struct value *val =  value_cast (dereftype, arg2);
371*cf7f2e2dSJohn Marino 
3725796c8dcSSimon Schubert       return value_ref (val);
3735796c8dcSSimon Schubert     }
3745796c8dcSSimon Schubert 
3755796c8dcSSimon Schubert   code2 = TYPE_CODE (check_typedef (value_type (arg2)));
3765796c8dcSSimon Schubert 
3775796c8dcSSimon Schubert   if (code2 == TYPE_CODE_REF)
3785796c8dcSSimon Schubert     /* We deref the value and then do the cast.  */
3795796c8dcSSimon Schubert     return value_cast (type, coerce_ref (arg2));
3805796c8dcSSimon Schubert 
3815796c8dcSSimon Schubert   CHECK_TYPEDEF (type);
3825796c8dcSSimon Schubert   code1 = TYPE_CODE (type);
3835796c8dcSSimon Schubert   arg2 = coerce_ref (arg2);
3845796c8dcSSimon Schubert   type2 = check_typedef (value_type (arg2));
3855796c8dcSSimon Schubert 
3865796c8dcSSimon Schubert   /* You can't cast to a reference type.  See value_cast_pointers
3875796c8dcSSimon Schubert      instead.  */
3885796c8dcSSimon Schubert   gdb_assert (code1 != TYPE_CODE_REF);
3895796c8dcSSimon Schubert 
3905796c8dcSSimon Schubert   /* A cast to an undetermined-length array_type, such as
3915796c8dcSSimon Schubert      (TYPE [])OBJECT, is treated like a cast to (TYPE [N])OBJECT,
3925796c8dcSSimon Schubert      where N is sizeof(OBJECT)/sizeof(TYPE).  */
3935796c8dcSSimon Schubert   if (code1 == TYPE_CODE_ARRAY)
3945796c8dcSSimon Schubert     {
3955796c8dcSSimon Schubert       struct type *element_type = TYPE_TARGET_TYPE (type);
3965796c8dcSSimon Schubert       unsigned element_length = TYPE_LENGTH (check_typedef (element_type));
397*cf7f2e2dSJohn Marino 
3985796c8dcSSimon Schubert       if (element_length > 0 && TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
3995796c8dcSSimon Schubert 	{
4005796c8dcSSimon Schubert 	  struct type *range_type = TYPE_INDEX_TYPE (type);
4015796c8dcSSimon Schubert 	  int val_length = TYPE_LENGTH (type2);
4025796c8dcSSimon Schubert 	  LONGEST low_bound, high_bound, new_length;
403*cf7f2e2dSJohn Marino 
4045796c8dcSSimon Schubert 	  if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
4055796c8dcSSimon Schubert 	    low_bound = 0, high_bound = 0;
4065796c8dcSSimon Schubert 	  new_length = val_length / element_length;
4075796c8dcSSimon Schubert 	  if (val_length % element_length != 0)
4085796c8dcSSimon Schubert 	    warning (_("array element type size does not divide object size in cast"));
4095796c8dcSSimon Schubert 	  /* FIXME-type-allocation: need a way to free this type when
4105796c8dcSSimon Schubert 	     we are done with it.  */
4115796c8dcSSimon Schubert 	  range_type = create_range_type ((struct type *) NULL,
4125796c8dcSSimon Schubert 					  TYPE_TARGET_TYPE (range_type),
4135796c8dcSSimon Schubert 					  low_bound,
4145796c8dcSSimon Schubert 					  new_length + low_bound - 1);
4155796c8dcSSimon Schubert 	  deprecated_set_value_type (arg2,
4165796c8dcSSimon Schubert 				     create_array_type ((struct type *) NULL,
4175796c8dcSSimon Schubert 							element_type,
4185796c8dcSSimon Schubert 							range_type));
4195796c8dcSSimon Schubert 	  return arg2;
4205796c8dcSSimon Schubert 	}
4215796c8dcSSimon Schubert     }
4225796c8dcSSimon Schubert 
4235796c8dcSSimon Schubert   if (current_language->c_style_arrays
4245796c8dcSSimon Schubert       && TYPE_CODE (type2) == TYPE_CODE_ARRAY)
4255796c8dcSSimon Schubert     arg2 = value_coerce_array (arg2);
4265796c8dcSSimon Schubert 
4275796c8dcSSimon Schubert   if (TYPE_CODE (type2) == TYPE_CODE_FUNC)
4285796c8dcSSimon Schubert     arg2 = value_coerce_function (arg2);
4295796c8dcSSimon Schubert 
4305796c8dcSSimon Schubert   type2 = check_typedef (value_type (arg2));
4315796c8dcSSimon Schubert   code2 = TYPE_CODE (type2);
4325796c8dcSSimon Schubert 
4335796c8dcSSimon Schubert   if (code1 == TYPE_CODE_COMPLEX)
4345796c8dcSSimon Schubert     return cast_into_complex (type, arg2);
4355796c8dcSSimon Schubert   if (code1 == TYPE_CODE_BOOL)
4365796c8dcSSimon Schubert     {
4375796c8dcSSimon Schubert       code1 = TYPE_CODE_INT;
4385796c8dcSSimon Schubert       convert_to_boolean = 1;
4395796c8dcSSimon Schubert     }
4405796c8dcSSimon Schubert   if (code1 == TYPE_CODE_CHAR)
4415796c8dcSSimon Schubert     code1 = TYPE_CODE_INT;
4425796c8dcSSimon Schubert   if (code2 == TYPE_CODE_BOOL || code2 == TYPE_CODE_CHAR)
4435796c8dcSSimon Schubert     code2 = TYPE_CODE_INT;
4445796c8dcSSimon Schubert 
4455796c8dcSSimon Schubert   scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
4465796c8dcSSimon Schubert 	    || code2 == TYPE_CODE_DECFLOAT || code2 == TYPE_CODE_ENUM
4475796c8dcSSimon Schubert 	    || code2 == TYPE_CODE_RANGE);
4485796c8dcSSimon Schubert 
4495796c8dcSSimon Schubert   if ((code1 == TYPE_CODE_STRUCT || code1 == TYPE_CODE_UNION)
4505796c8dcSSimon Schubert       && (code2 == TYPE_CODE_STRUCT || code2 == TYPE_CODE_UNION)
4515796c8dcSSimon Schubert       && TYPE_NAME (type) != 0)
4525796c8dcSSimon Schubert     {
4535796c8dcSSimon Schubert       struct value *v = value_cast_structs (type, arg2);
454*cf7f2e2dSJohn Marino 
4555796c8dcSSimon Schubert       if (v)
4565796c8dcSSimon Schubert 	return v;
4575796c8dcSSimon Schubert     }
4585796c8dcSSimon Schubert 
4595796c8dcSSimon Schubert   if (code1 == TYPE_CODE_FLT && scalar)
4605796c8dcSSimon Schubert     return value_from_double (type, value_as_double (arg2));
4615796c8dcSSimon Schubert   else if (code1 == TYPE_CODE_DECFLOAT && scalar)
4625796c8dcSSimon Schubert     {
4635796c8dcSSimon Schubert       enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
4645796c8dcSSimon Schubert       int dec_len = TYPE_LENGTH (type);
4655796c8dcSSimon Schubert       gdb_byte dec[16];
4665796c8dcSSimon Schubert 
4675796c8dcSSimon Schubert       if (code2 == TYPE_CODE_FLT)
4685796c8dcSSimon Schubert 	decimal_from_floating (arg2, dec, dec_len, byte_order);
4695796c8dcSSimon Schubert       else if (code2 == TYPE_CODE_DECFLOAT)
4705796c8dcSSimon Schubert 	decimal_convert (value_contents (arg2), TYPE_LENGTH (type2),
4715796c8dcSSimon Schubert 			 byte_order, dec, dec_len, byte_order);
4725796c8dcSSimon Schubert       else
4735796c8dcSSimon Schubert 	/* The only option left is an integral type.  */
4745796c8dcSSimon Schubert 	decimal_from_integral (arg2, dec, dec_len, byte_order);
4755796c8dcSSimon Schubert 
4765796c8dcSSimon Schubert       return value_from_decfloat (type, dec);
4775796c8dcSSimon Schubert     }
4785796c8dcSSimon Schubert   else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM
4795796c8dcSSimon Schubert 	    || code1 == TYPE_CODE_RANGE)
4805796c8dcSSimon Schubert 	   && (scalar || code2 == TYPE_CODE_PTR
4815796c8dcSSimon Schubert 	       || code2 == TYPE_CODE_MEMBERPTR))
4825796c8dcSSimon Schubert     {
4835796c8dcSSimon Schubert       LONGEST longest;
4845796c8dcSSimon Schubert 
4855796c8dcSSimon Schubert       /* When we cast pointers to integers, we mustn't use
4865796c8dcSSimon Schubert          gdbarch_pointer_to_address to find the address the pointer
4875796c8dcSSimon Schubert          represents, as value_as_long would.  GDB should evaluate
4885796c8dcSSimon Schubert          expressions just as the compiler would --- and the compiler
4895796c8dcSSimon Schubert          sees a cast as a simple reinterpretation of the pointer's
4905796c8dcSSimon Schubert          bits.  */
4915796c8dcSSimon Schubert       if (code2 == TYPE_CODE_PTR)
4925796c8dcSSimon Schubert         longest = extract_unsigned_integer
4935796c8dcSSimon Schubert 		    (value_contents (arg2), TYPE_LENGTH (type2),
4945796c8dcSSimon Schubert 		     gdbarch_byte_order (get_type_arch (type2)));
4955796c8dcSSimon Schubert       else
4965796c8dcSSimon Schubert         longest = value_as_long (arg2);
4975796c8dcSSimon Schubert       return value_from_longest (type, convert_to_boolean ?
4985796c8dcSSimon Schubert 				 (LONGEST) (longest ? 1 : 0) : longest);
4995796c8dcSSimon Schubert     }
5005796c8dcSSimon Schubert   else if (code1 == TYPE_CODE_PTR && (code2 == TYPE_CODE_INT
5015796c8dcSSimon Schubert 				      || code2 == TYPE_CODE_ENUM
5025796c8dcSSimon Schubert 				      || code2 == TYPE_CODE_RANGE))
5035796c8dcSSimon Schubert     {
5045796c8dcSSimon Schubert       /* TYPE_LENGTH (type) is the length of a pointer, but we really
5055796c8dcSSimon Schubert 	 want the length of an address! -- we are really dealing with
5065796c8dcSSimon Schubert 	 addresses (i.e., gdb representations) not pointers (i.e.,
5075796c8dcSSimon Schubert 	 target representations) here.
5085796c8dcSSimon Schubert 
5095796c8dcSSimon Schubert 	 This allows things like "print *(int *)0x01000234" to work
5105796c8dcSSimon Schubert 	 without printing a misleading message -- which would
5115796c8dcSSimon Schubert 	 otherwise occur when dealing with a target having two byte
5125796c8dcSSimon Schubert 	 pointers and four byte addresses.  */
5135796c8dcSSimon Schubert 
5145796c8dcSSimon Schubert       int addr_bit = gdbarch_addr_bit (get_type_arch (type2));
5155796c8dcSSimon Schubert       LONGEST longest = value_as_long (arg2);
516*cf7f2e2dSJohn Marino 
5175796c8dcSSimon Schubert       if (addr_bit < sizeof (LONGEST) * HOST_CHAR_BIT)
5185796c8dcSSimon Schubert 	{
5195796c8dcSSimon Schubert 	  if (longest >= ((LONGEST) 1 << addr_bit)
5205796c8dcSSimon Schubert 	      || longest <= -((LONGEST) 1 << addr_bit))
5215796c8dcSSimon Schubert 	    warning (_("value truncated"));
5225796c8dcSSimon Schubert 	}
5235796c8dcSSimon Schubert       return value_from_longest (type, longest);
5245796c8dcSSimon Schubert     }
5255796c8dcSSimon Schubert   else if (code1 == TYPE_CODE_METHODPTR && code2 == TYPE_CODE_INT
5265796c8dcSSimon Schubert 	   && value_as_long (arg2) == 0)
5275796c8dcSSimon Schubert     {
5285796c8dcSSimon Schubert       struct value *result = allocate_value (type);
529*cf7f2e2dSJohn Marino 
5305796c8dcSSimon Schubert       cplus_make_method_ptr (type, value_contents_writeable (result), 0, 0);
5315796c8dcSSimon Schubert       return result;
5325796c8dcSSimon Schubert     }
5335796c8dcSSimon Schubert   else if (code1 == TYPE_CODE_MEMBERPTR && code2 == TYPE_CODE_INT
5345796c8dcSSimon Schubert 	   && value_as_long (arg2) == 0)
5355796c8dcSSimon Schubert     {
5365796c8dcSSimon Schubert       /* The Itanium C++ ABI represents NULL pointers to members as
5375796c8dcSSimon Schubert 	 minus one, instead of biasing the normal case.  */
5385796c8dcSSimon Schubert       return value_from_longest (type, -1);
5395796c8dcSSimon Schubert     }
5405796c8dcSSimon Schubert   else if (TYPE_LENGTH (type) == TYPE_LENGTH (type2))
5415796c8dcSSimon Schubert     {
5425796c8dcSSimon Schubert       if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
5435796c8dcSSimon Schubert 	return value_cast_pointers (type, arg2);
5445796c8dcSSimon Schubert 
5455796c8dcSSimon Schubert       arg2 = value_copy (arg2);
5465796c8dcSSimon Schubert       deprecated_set_value_type (arg2, type);
5475796c8dcSSimon Schubert       arg2 = value_change_enclosing_type (arg2, type);
5485796c8dcSSimon Schubert       set_value_pointed_to_offset (arg2, 0);	/* pai: chk_val */
5495796c8dcSSimon Schubert       return arg2;
5505796c8dcSSimon Schubert     }
5515796c8dcSSimon Schubert   else if (VALUE_LVAL (arg2) == lval_memory)
5525796c8dcSSimon Schubert     return value_at_lazy (type, value_address (arg2));
5535796c8dcSSimon Schubert   else if (code1 == TYPE_CODE_VOID)
5545796c8dcSSimon Schubert     {
5555796c8dcSSimon Schubert       return value_zero (type, not_lval);
5565796c8dcSSimon Schubert     }
5575796c8dcSSimon Schubert   else
5585796c8dcSSimon Schubert     {
5595796c8dcSSimon Schubert       error (_("Invalid cast."));
5605796c8dcSSimon Schubert       return 0;
5615796c8dcSSimon Schubert     }
5625796c8dcSSimon Schubert }
5635796c8dcSSimon Schubert 
564*cf7f2e2dSJohn Marino /* The C++ reinterpret_cast operator.  */
565*cf7f2e2dSJohn Marino 
566*cf7f2e2dSJohn Marino struct value *
567*cf7f2e2dSJohn Marino value_reinterpret_cast (struct type *type, struct value *arg)
568*cf7f2e2dSJohn Marino {
569*cf7f2e2dSJohn Marino   struct value *result;
570*cf7f2e2dSJohn Marino   struct type *real_type = check_typedef (type);
571*cf7f2e2dSJohn Marino   struct type *arg_type, *dest_type;
572*cf7f2e2dSJohn Marino   int is_ref = 0;
573*cf7f2e2dSJohn Marino   enum type_code dest_code, arg_code;
574*cf7f2e2dSJohn Marino 
575*cf7f2e2dSJohn Marino   /* Do reference, function, and array conversion.  */
576*cf7f2e2dSJohn Marino   arg = coerce_array (arg);
577*cf7f2e2dSJohn Marino 
578*cf7f2e2dSJohn Marino   /* Attempt to preserve the type the user asked for.  */
579*cf7f2e2dSJohn Marino   dest_type = type;
580*cf7f2e2dSJohn Marino 
581*cf7f2e2dSJohn Marino   /* If we are casting to a reference type, transform
582*cf7f2e2dSJohn Marino      reinterpret_cast<T&>(V) to *reinterpret_cast<T*>(&V).  */
583*cf7f2e2dSJohn Marino   if (TYPE_CODE (real_type) == TYPE_CODE_REF)
584*cf7f2e2dSJohn Marino     {
585*cf7f2e2dSJohn Marino       is_ref = 1;
586*cf7f2e2dSJohn Marino       arg = value_addr (arg);
587*cf7f2e2dSJohn Marino       dest_type = lookup_pointer_type (TYPE_TARGET_TYPE (dest_type));
588*cf7f2e2dSJohn Marino       real_type = lookup_pointer_type (real_type);
589*cf7f2e2dSJohn Marino     }
590*cf7f2e2dSJohn Marino 
591*cf7f2e2dSJohn Marino   arg_type = value_type (arg);
592*cf7f2e2dSJohn Marino 
593*cf7f2e2dSJohn Marino   dest_code = TYPE_CODE (real_type);
594*cf7f2e2dSJohn Marino   arg_code = TYPE_CODE (arg_type);
595*cf7f2e2dSJohn Marino 
596*cf7f2e2dSJohn Marino   /* We can convert pointer types, or any pointer type to int, or int
597*cf7f2e2dSJohn Marino      type to pointer.  */
598*cf7f2e2dSJohn Marino   if ((dest_code == TYPE_CODE_PTR && arg_code == TYPE_CODE_INT)
599*cf7f2e2dSJohn Marino       || (dest_code == TYPE_CODE_INT && arg_code == TYPE_CODE_PTR)
600*cf7f2e2dSJohn Marino       || (dest_code == TYPE_CODE_METHODPTR && arg_code == TYPE_CODE_INT)
601*cf7f2e2dSJohn Marino       || (dest_code == TYPE_CODE_INT && arg_code == TYPE_CODE_METHODPTR)
602*cf7f2e2dSJohn Marino       || (dest_code == TYPE_CODE_MEMBERPTR && arg_code == TYPE_CODE_INT)
603*cf7f2e2dSJohn Marino       || (dest_code == TYPE_CODE_INT && arg_code == TYPE_CODE_MEMBERPTR)
604*cf7f2e2dSJohn Marino       || (dest_code == arg_code
605*cf7f2e2dSJohn Marino 	  && (dest_code == TYPE_CODE_PTR
606*cf7f2e2dSJohn Marino 	      || dest_code == TYPE_CODE_METHODPTR
607*cf7f2e2dSJohn Marino 	      || dest_code == TYPE_CODE_MEMBERPTR)))
608*cf7f2e2dSJohn Marino     result = value_cast (dest_type, arg);
609*cf7f2e2dSJohn Marino   else
610*cf7f2e2dSJohn Marino     error (_("Invalid reinterpret_cast"));
611*cf7f2e2dSJohn Marino 
612*cf7f2e2dSJohn Marino   if (is_ref)
613*cf7f2e2dSJohn Marino     result = value_cast (type, value_ref (value_ind (result)));
614*cf7f2e2dSJohn Marino 
615*cf7f2e2dSJohn Marino   return result;
616*cf7f2e2dSJohn Marino }
617*cf7f2e2dSJohn Marino 
618*cf7f2e2dSJohn Marino /* A helper for value_dynamic_cast.  This implements the first of two
619*cf7f2e2dSJohn Marino    runtime checks: we iterate over all the base classes of the value's
620*cf7f2e2dSJohn Marino    class which are equal to the desired class; if only one of these
621*cf7f2e2dSJohn Marino    holds the value, then it is the answer.  */
622*cf7f2e2dSJohn Marino 
623*cf7f2e2dSJohn Marino static int
624*cf7f2e2dSJohn Marino dynamic_cast_check_1 (struct type *desired_type,
625*cf7f2e2dSJohn Marino 		      const bfd_byte *contents,
626*cf7f2e2dSJohn Marino 		      CORE_ADDR address,
627*cf7f2e2dSJohn Marino 		      struct type *search_type,
628*cf7f2e2dSJohn Marino 		      CORE_ADDR arg_addr,
629*cf7f2e2dSJohn Marino 		      struct type *arg_type,
630*cf7f2e2dSJohn Marino 		      struct value **result)
631*cf7f2e2dSJohn Marino {
632*cf7f2e2dSJohn Marino   int i, result_count = 0;
633*cf7f2e2dSJohn Marino 
634*cf7f2e2dSJohn Marino   for (i = 0; i < TYPE_N_BASECLASSES (search_type) && result_count < 2; ++i)
635*cf7f2e2dSJohn Marino     {
636*cf7f2e2dSJohn Marino       int offset = baseclass_offset (search_type, i, contents, address);
637*cf7f2e2dSJohn Marino 
638*cf7f2e2dSJohn Marino       if (offset == -1)
639*cf7f2e2dSJohn Marino 	error (_("virtual baseclass botch"));
640*cf7f2e2dSJohn Marino       if (class_types_same_p (desired_type, TYPE_BASECLASS (search_type, i)))
641*cf7f2e2dSJohn Marino 	{
642*cf7f2e2dSJohn Marino 	  if (address + offset >= arg_addr
643*cf7f2e2dSJohn Marino 	      && address + offset < arg_addr + TYPE_LENGTH (arg_type))
644*cf7f2e2dSJohn Marino 	    {
645*cf7f2e2dSJohn Marino 	      ++result_count;
646*cf7f2e2dSJohn Marino 	      if (!*result)
647*cf7f2e2dSJohn Marino 		*result = value_at_lazy (TYPE_BASECLASS (search_type, i),
648*cf7f2e2dSJohn Marino 					 address + offset);
649*cf7f2e2dSJohn Marino 	    }
650*cf7f2e2dSJohn Marino 	}
651*cf7f2e2dSJohn Marino       else
652*cf7f2e2dSJohn Marino 	result_count += dynamic_cast_check_1 (desired_type,
653*cf7f2e2dSJohn Marino 					      contents + offset,
654*cf7f2e2dSJohn Marino 					      address + offset,
655*cf7f2e2dSJohn Marino 					      TYPE_BASECLASS (search_type, i),
656*cf7f2e2dSJohn Marino 					      arg_addr,
657*cf7f2e2dSJohn Marino 					      arg_type,
658*cf7f2e2dSJohn Marino 					      result);
659*cf7f2e2dSJohn Marino     }
660*cf7f2e2dSJohn Marino 
661*cf7f2e2dSJohn Marino   return result_count;
662*cf7f2e2dSJohn Marino }
663*cf7f2e2dSJohn Marino 
664*cf7f2e2dSJohn Marino /* A helper for value_dynamic_cast.  This implements the second of two
665*cf7f2e2dSJohn Marino    runtime checks: we look for a unique public sibling class of the
666*cf7f2e2dSJohn Marino    argument's declared class.  */
667*cf7f2e2dSJohn Marino 
668*cf7f2e2dSJohn Marino static int
669*cf7f2e2dSJohn Marino dynamic_cast_check_2 (struct type *desired_type,
670*cf7f2e2dSJohn Marino 		      const bfd_byte *contents,
671*cf7f2e2dSJohn Marino 		      CORE_ADDR address,
672*cf7f2e2dSJohn Marino 		      struct type *search_type,
673*cf7f2e2dSJohn Marino 		      struct value **result)
674*cf7f2e2dSJohn Marino {
675*cf7f2e2dSJohn Marino   int i, result_count = 0;
676*cf7f2e2dSJohn Marino 
677*cf7f2e2dSJohn Marino   for (i = 0; i < TYPE_N_BASECLASSES (search_type) && result_count < 2; ++i)
678*cf7f2e2dSJohn Marino     {
679*cf7f2e2dSJohn Marino       int offset;
680*cf7f2e2dSJohn Marino 
681*cf7f2e2dSJohn Marino       if (! BASETYPE_VIA_PUBLIC (search_type, i))
682*cf7f2e2dSJohn Marino 	continue;
683*cf7f2e2dSJohn Marino 
684*cf7f2e2dSJohn Marino       offset = baseclass_offset (search_type, i, contents, address);
685*cf7f2e2dSJohn Marino       if (offset == -1)
686*cf7f2e2dSJohn Marino 	error (_("virtual baseclass botch"));
687*cf7f2e2dSJohn Marino       if (class_types_same_p (desired_type, TYPE_BASECLASS (search_type, i)))
688*cf7f2e2dSJohn Marino 	{
689*cf7f2e2dSJohn Marino 	  ++result_count;
690*cf7f2e2dSJohn Marino 	  if (*result == NULL)
691*cf7f2e2dSJohn Marino 	    *result = value_at_lazy (TYPE_BASECLASS (search_type, i),
692*cf7f2e2dSJohn Marino 				     address + offset);
693*cf7f2e2dSJohn Marino 	}
694*cf7f2e2dSJohn Marino       else
695*cf7f2e2dSJohn Marino 	result_count += dynamic_cast_check_2 (desired_type,
696*cf7f2e2dSJohn Marino 					      contents + offset,
697*cf7f2e2dSJohn Marino 					      address + offset,
698*cf7f2e2dSJohn Marino 					      TYPE_BASECLASS (search_type, i),
699*cf7f2e2dSJohn Marino 					      result);
700*cf7f2e2dSJohn Marino     }
701*cf7f2e2dSJohn Marino 
702*cf7f2e2dSJohn Marino   return result_count;
703*cf7f2e2dSJohn Marino }
704*cf7f2e2dSJohn Marino 
705*cf7f2e2dSJohn Marino /* The C++ dynamic_cast operator.  */
706*cf7f2e2dSJohn Marino 
707*cf7f2e2dSJohn Marino struct value *
708*cf7f2e2dSJohn Marino value_dynamic_cast (struct type *type, struct value *arg)
709*cf7f2e2dSJohn Marino {
710*cf7f2e2dSJohn Marino   int full, top, using_enc;
711*cf7f2e2dSJohn Marino   struct type *resolved_type = check_typedef (type);
712*cf7f2e2dSJohn Marino   struct type *arg_type = check_typedef (value_type (arg));
713*cf7f2e2dSJohn Marino   struct type *class_type, *rtti_type;
714*cf7f2e2dSJohn Marino   struct value *result, *tem, *original_arg = arg;
715*cf7f2e2dSJohn Marino   CORE_ADDR addr;
716*cf7f2e2dSJohn Marino   int is_ref = TYPE_CODE (resolved_type) == TYPE_CODE_REF;
717*cf7f2e2dSJohn Marino 
718*cf7f2e2dSJohn Marino   if (TYPE_CODE (resolved_type) != TYPE_CODE_PTR
719*cf7f2e2dSJohn Marino       && TYPE_CODE (resolved_type) != TYPE_CODE_REF)
720*cf7f2e2dSJohn Marino     error (_("Argument to dynamic_cast must be a pointer or reference type"));
721*cf7f2e2dSJohn Marino   if (TYPE_CODE (TYPE_TARGET_TYPE (resolved_type)) != TYPE_CODE_VOID
722*cf7f2e2dSJohn Marino       && TYPE_CODE (TYPE_TARGET_TYPE (resolved_type)) != TYPE_CODE_CLASS)
723*cf7f2e2dSJohn Marino     error (_("Argument to dynamic_cast must be pointer to class or `void *'"));
724*cf7f2e2dSJohn Marino 
725*cf7f2e2dSJohn Marino   class_type = check_typedef (TYPE_TARGET_TYPE (resolved_type));
726*cf7f2e2dSJohn Marino   if (TYPE_CODE (resolved_type) == TYPE_CODE_PTR)
727*cf7f2e2dSJohn Marino     {
728*cf7f2e2dSJohn Marino       if (TYPE_CODE (arg_type) != TYPE_CODE_PTR
729*cf7f2e2dSJohn Marino 	  && ! (TYPE_CODE (arg_type) == TYPE_CODE_INT
730*cf7f2e2dSJohn Marino 		&& value_as_long (arg) == 0))
731*cf7f2e2dSJohn Marino 	error (_("Argument to dynamic_cast does not have pointer type"));
732*cf7f2e2dSJohn Marino       if (TYPE_CODE (arg_type) == TYPE_CODE_PTR)
733*cf7f2e2dSJohn Marino 	{
734*cf7f2e2dSJohn Marino 	  arg_type = check_typedef (TYPE_TARGET_TYPE (arg_type));
735*cf7f2e2dSJohn Marino 	  if (TYPE_CODE (arg_type) != TYPE_CODE_CLASS)
736*cf7f2e2dSJohn Marino 	    error (_("Argument to dynamic_cast does not have pointer to class type"));
737*cf7f2e2dSJohn Marino 	}
738*cf7f2e2dSJohn Marino 
739*cf7f2e2dSJohn Marino       /* Handle NULL pointers.  */
740*cf7f2e2dSJohn Marino       if (value_as_long (arg) == 0)
741*cf7f2e2dSJohn Marino 	return value_zero (type, not_lval);
742*cf7f2e2dSJohn Marino 
743*cf7f2e2dSJohn Marino       arg = value_ind (arg);
744*cf7f2e2dSJohn Marino     }
745*cf7f2e2dSJohn Marino   else
746*cf7f2e2dSJohn Marino     {
747*cf7f2e2dSJohn Marino       if (TYPE_CODE (arg_type) != TYPE_CODE_CLASS)
748*cf7f2e2dSJohn Marino 	error (_("Argument to dynamic_cast does not have class type"));
749*cf7f2e2dSJohn Marino     }
750*cf7f2e2dSJohn Marino 
751*cf7f2e2dSJohn Marino   /* If the classes are the same, just return the argument.  */
752*cf7f2e2dSJohn Marino   if (class_types_same_p (class_type, arg_type))
753*cf7f2e2dSJohn Marino     return value_cast (type, arg);
754*cf7f2e2dSJohn Marino 
755*cf7f2e2dSJohn Marino   /* If the target type is a unique base class of the argument's
756*cf7f2e2dSJohn Marino      declared type, just cast it.  */
757*cf7f2e2dSJohn Marino   if (is_ancestor (class_type, arg_type))
758*cf7f2e2dSJohn Marino     {
759*cf7f2e2dSJohn Marino       if (is_unique_ancestor (class_type, arg))
760*cf7f2e2dSJohn Marino 	return value_cast (type, original_arg);
761*cf7f2e2dSJohn Marino       error (_("Ambiguous dynamic_cast"));
762*cf7f2e2dSJohn Marino     }
763*cf7f2e2dSJohn Marino 
764*cf7f2e2dSJohn Marino   rtti_type = value_rtti_type (arg, &full, &top, &using_enc);
765*cf7f2e2dSJohn Marino   if (! rtti_type)
766*cf7f2e2dSJohn Marino     error (_("Couldn't determine value's most derived type for dynamic_cast"));
767*cf7f2e2dSJohn Marino 
768*cf7f2e2dSJohn Marino   /* Compute the most derived object's address.  */
769*cf7f2e2dSJohn Marino   addr = value_address (arg);
770*cf7f2e2dSJohn Marino   if (full)
771*cf7f2e2dSJohn Marino     {
772*cf7f2e2dSJohn Marino       /* Done.  */
773*cf7f2e2dSJohn Marino     }
774*cf7f2e2dSJohn Marino   else if (using_enc)
775*cf7f2e2dSJohn Marino     addr += top;
776*cf7f2e2dSJohn Marino   else
777*cf7f2e2dSJohn Marino     addr += top + value_embedded_offset (arg);
778*cf7f2e2dSJohn Marino 
779*cf7f2e2dSJohn Marino   /* dynamic_cast<void *> means to return a pointer to the
780*cf7f2e2dSJohn Marino      most-derived object.  */
781*cf7f2e2dSJohn Marino   if (TYPE_CODE (resolved_type) == TYPE_CODE_PTR
782*cf7f2e2dSJohn Marino       && TYPE_CODE (TYPE_TARGET_TYPE (resolved_type)) == TYPE_CODE_VOID)
783*cf7f2e2dSJohn Marino     return value_at_lazy (type, addr);
784*cf7f2e2dSJohn Marino 
785*cf7f2e2dSJohn Marino   tem = value_at (type, addr);
786*cf7f2e2dSJohn Marino 
787*cf7f2e2dSJohn Marino   /* The first dynamic check specified in 5.2.7.  */
788*cf7f2e2dSJohn Marino   if (is_public_ancestor (arg_type, TYPE_TARGET_TYPE (resolved_type)))
789*cf7f2e2dSJohn Marino     {
790*cf7f2e2dSJohn Marino       if (class_types_same_p (rtti_type, TYPE_TARGET_TYPE (resolved_type)))
791*cf7f2e2dSJohn Marino 	return tem;
792*cf7f2e2dSJohn Marino       result = NULL;
793*cf7f2e2dSJohn Marino       if (dynamic_cast_check_1 (TYPE_TARGET_TYPE (resolved_type),
794*cf7f2e2dSJohn Marino 				value_contents (tem), value_address (tem),
795*cf7f2e2dSJohn Marino 				rtti_type, addr,
796*cf7f2e2dSJohn Marino 				arg_type,
797*cf7f2e2dSJohn Marino 				&result) == 1)
798*cf7f2e2dSJohn Marino 	return value_cast (type,
799*cf7f2e2dSJohn Marino 			   is_ref ? value_ref (result) : value_addr (result));
800*cf7f2e2dSJohn Marino     }
801*cf7f2e2dSJohn Marino 
802*cf7f2e2dSJohn Marino   /* The second dynamic check specified in 5.2.7.  */
803*cf7f2e2dSJohn Marino   result = NULL;
804*cf7f2e2dSJohn Marino   if (is_public_ancestor (arg_type, rtti_type)
805*cf7f2e2dSJohn Marino       && dynamic_cast_check_2 (TYPE_TARGET_TYPE (resolved_type),
806*cf7f2e2dSJohn Marino 			       value_contents (tem), value_address (tem),
807*cf7f2e2dSJohn Marino 			       rtti_type, &result) == 1)
808*cf7f2e2dSJohn Marino     return value_cast (type,
809*cf7f2e2dSJohn Marino 		       is_ref ? value_ref (result) : value_addr (result));
810*cf7f2e2dSJohn Marino 
811*cf7f2e2dSJohn Marino   if (TYPE_CODE (resolved_type) == TYPE_CODE_PTR)
812*cf7f2e2dSJohn Marino     return value_zero (type, not_lval);
813*cf7f2e2dSJohn Marino 
814*cf7f2e2dSJohn Marino   error (_("dynamic_cast failed"));
815*cf7f2e2dSJohn Marino }
816*cf7f2e2dSJohn Marino 
8175796c8dcSSimon Schubert /* Create a value of type TYPE that is zero, and return it.  */
8185796c8dcSSimon Schubert 
8195796c8dcSSimon Schubert struct value *
8205796c8dcSSimon Schubert value_zero (struct type *type, enum lval_type lv)
8215796c8dcSSimon Schubert {
8225796c8dcSSimon Schubert   struct value *val = allocate_value (type);
8235796c8dcSSimon Schubert 
824*cf7f2e2dSJohn Marino   VALUE_LVAL (val) = lv;
8255796c8dcSSimon Schubert   return val;
8265796c8dcSSimon Schubert }
8275796c8dcSSimon Schubert 
8285796c8dcSSimon Schubert /* Create a value of numeric type TYPE that is one, and return it.  */
8295796c8dcSSimon Schubert 
8305796c8dcSSimon Schubert struct value *
8315796c8dcSSimon Schubert value_one (struct type *type, enum lval_type lv)
8325796c8dcSSimon Schubert {
8335796c8dcSSimon Schubert   struct type *type1 = check_typedef (type);
8345796c8dcSSimon Schubert   struct value *val;
8355796c8dcSSimon Schubert 
8365796c8dcSSimon Schubert   if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
8375796c8dcSSimon Schubert     {
8385796c8dcSSimon Schubert       enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
8395796c8dcSSimon Schubert       gdb_byte v[16];
840*cf7f2e2dSJohn Marino 
8415796c8dcSSimon Schubert       decimal_from_string (v, TYPE_LENGTH (type), byte_order, "1");
8425796c8dcSSimon Schubert       val = value_from_decfloat (type, v);
8435796c8dcSSimon Schubert     }
8445796c8dcSSimon Schubert   else if (TYPE_CODE (type1) == TYPE_CODE_FLT)
8455796c8dcSSimon Schubert     {
8465796c8dcSSimon Schubert       val = value_from_double (type, (DOUBLEST) 1);
8475796c8dcSSimon Schubert     }
8485796c8dcSSimon Schubert   else if (is_integral_type (type1))
8495796c8dcSSimon Schubert     {
8505796c8dcSSimon Schubert       val = value_from_longest (type, (LONGEST) 1);
8515796c8dcSSimon Schubert     }
8525796c8dcSSimon Schubert   else
8535796c8dcSSimon Schubert     {
8545796c8dcSSimon Schubert       error (_("Not a numeric type."));
8555796c8dcSSimon Schubert     }
8565796c8dcSSimon Schubert 
8575796c8dcSSimon Schubert   VALUE_LVAL (val) = lv;
8585796c8dcSSimon Schubert   return val;
8595796c8dcSSimon Schubert }
8605796c8dcSSimon Schubert 
8615796c8dcSSimon Schubert /* Helper function for value_at, value_at_lazy, and value_at_lazy_stack.  */
8625796c8dcSSimon Schubert 
8635796c8dcSSimon Schubert static struct value *
8645796c8dcSSimon Schubert get_value_at (struct type *type, CORE_ADDR addr, int lazy)
8655796c8dcSSimon Schubert {
8665796c8dcSSimon Schubert   struct value *val;
8675796c8dcSSimon Schubert 
8685796c8dcSSimon Schubert   if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
8695796c8dcSSimon Schubert     error (_("Attempt to dereference a generic pointer."));
8705796c8dcSSimon Schubert 
8715796c8dcSSimon Schubert   if (lazy)
8725796c8dcSSimon Schubert     {
8735796c8dcSSimon Schubert       val = allocate_value_lazy (type);
8745796c8dcSSimon Schubert     }
8755796c8dcSSimon Schubert   else
8765796c8dcSSimon Schubert     {
8775796c8dcSSimon Schubert       val = allocate_value (type);
8785796c8dcSSimon Schubert       read_memory (addr, value_contents_all_raw (val), TYPE_LENGTH (type));
8795796c8dcSSimon Schubert     }
8805796c8dcSSimon Schubert 
8815796c8dcSSimon Schubert   VALUE_LVAL (val) = lval_memory;
8825796c8dcSSimon Schubert   set_value_address (val, addr);
8835796c8dcSSimon Schubert 
8845796c8dcSSimon Schubert   return val;
8855796c8dcSSimon Schubert }
8865796c8dcSSimon Schubert 
8875796c8dcSSimon Schubert /* Return a value with type TYPE located at ADDR.
8885796c8dcSSimon Schubert 
8895796c8dcSSimon Schubert    Call value_at only if the data needs to be fetched immediately;
8905796c8dcSSimon Schubert    if we can be 'lazy' and defer the fetch, perhaps indefinately, call
8915796c8dcSSimon Schubert    value_at_lazy instead.  value_at_lazy simply records the address of
8925796c8dcSSimon Schubert    the data and sets the lazy-evaluation-required flag.  The lazy flag
8935796c8dcSSimon Schubert    is tested in the value_contents macro, which is used if and when
8945796c8dcSSimon Schubert    the contents are actually required.
8955796c8dcSSimon Schubert 
8965796c8dcSSimon Schubert    Note: value_at does *NOT* handle embedded offsets; perform such
8975796c8dcSSimon Schubert    adjustments before or after calling it.  */
8985796c8dcSSimon Schubert 
8995796c8dcSSimon Schubert struct value *
9005796c8dcSSimon Schubert value_at (struct type *type, CORE_ADDR addr)
9015796c8dcSSimon Schubert {
9025796c8dcSSimon Schubert   return get_value_at (type, addr, 0);
9035796c8dcSSimon Schubert }
9045796c8dcSSimon Schubert 
9055796c8dcSSimon Schubert /* Return a lazy value with type TYPE located at ADDR (cf. value_at).  */
9065796c8dcSSimon Schubert 
9075796c8dcSSimon Schubert struct value *
9085796c8dcSSimon Schubert value_at_lazy (struct type *type, CORE_ADDR addr)
9095796c8dcSSimon Schubert {
9105796c8dcSSimon Schubert   return get_value_at (type, addr, 1);
9115796c8dcSSimon Schubert }
9125796c8dcSSimon Schubert 
9135796c8dcSSimon Schubert /* Called only from the value_contents and value_contents_all()
9145796c8dcSSimon Schubert    macros, if the current data for a variable needs to be loaded into
9155796c8dcSSimon Schubert    value_contents(VAL).  Fetches the data from the user's process, and
9165796c8dcSSimon Schubert    clears the lazy flag to indicate that the data in the buffer is
9175796c8dcSSimon Schubert    valid.
9185796c8dcSSimon Schubert 
9195796c8dcSSimon Schubert    If the value is zero-length, we avoid calling read_memory, which
9205796c8dcSSimon Schubert    would abort.  We mark the value as fetched anyway -- all 0 bytes of
9215796c8dcSSimon Schubert    it.
9225796c8dcSSimon Schubert 
9235796c8dcSSimon Schubert    This function returns a value because it is used in the
9245796c8dcSSimon Schubert    value_contents macro as part of an expression, where a void would
9255796c8dcSSimon Schubert    not work.  The value is ignored.  */
9265796c8dcSSimon Schubert 
9275796c8dcSSimon Schubert int
9285796c8dcSSimon Schubert value_fetch_lazy (struct value *val)
9295796c8dcSSimon Schubert {
9305796c8dcSSimon Schubert   gdb_assert (value_lazy (val));
9315796c8dcSSimon Schubert   allocate_value_contents (val);
9325796c8dcSSimon Schubert   if (value_bitsize (val))
9335796c8dcSSimon Schubert     {
9345796c8dcSSimon Schubert       /* To read a lazy bitfield, read the entire enclosing value.  This
9355796c8dcSSimon Schubert 	 prevents reading the same block of (possibly volatile) memory once
9365796c8dcSSimon Schubert          per bitfield.  It would be even better to read only the containing
9375796c8dcSSimon Schubert          word, but we have no way to record that just specific bits of a
9385796c8dcSSimon Schubert          value have been fetched.  */
9395796c8dcSSimon Schubert       struct type *type = check_typedef (value_type (val));
9405796c8dcSSimon Schubert       enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
9415796c8dcSSimon Schubert       struct value *parent = value_parent (val);
9425796c8dcSSimon Schubert       LONGEST offset = value_offset (val);
9435796c8dcSSimon Schubert       LONGEST num = unpack_bits_as_long (value_type (val),
944*cf7f2e2dSJohn Marino 					 (value_contents_for_printing (parent)
945*cf7f2e2dSJohn Marino 					  + offset),
9465796c8dcSSimon Schubert 					 value_bitpos (val),
9475796c8dcSSimon Schubert 					 value_bitsize (val));
9485796c8dcSSimon Schubert       int length = TYPE_LENGTH (type);
949*cf7f2e2dSJohn Marino 
950*cf7f2e2dSJohn Marino       if (!value_bits_valid (val,
951*cf7f2e2dSJohn Marino 			     TARGET_CHAR_BIT * offset + value_bitpos (val),
952*cf7f2e2dSJohn Marino 			     value_bitsize (val)))
953*cf7f2e2dSJohn Marino 	error (_("value has been optimized out"));
954*cf7f2e2dSJohn Marino 
9555796c8dcSSimon Schubert       store_signed_integer (value_contents_raw (val), length, byte_order, num);
9565796c8dcSSimon Schubert     }
9575796c8dcSSimon Schubert   else if (VALUE_LVAL (val) == lval_memory)
9585796c8dcSSimon Schubert     {
9595796c8dcSSimon Schubert       CORE_ADDR addr = value_address (val);
9605796c8dcSSimon Schubert       int length = TYPE_LENGTH (check_typedef (value_enclosing_type (val)));
9615796c8dcSSimon Schubert 
9625796c8dcSSimon Schubert       if (length)
9635796c8dcSSimon Schubert 	{
9645796c8dcSSimon Schubert 	  if (value_stack (val))
9655796c8dcSSimon Schubert 	    read_stack (addr, value_contents_all_raw (val), length);
9665796c8dcSSimon Schubert 	  else
9675796c8dcSSimon Schubert 	    read_memory (addr, value_contents_all_raw (val), length);
9685796c8dcSSimon Schubert 	}
9695796c8dcSSimon Schubert     }
9705796c8dcSSimon Schubert   else if (VALUE_LVAL (val) == lval_register)
9715796c8dcSSimon Schubert     {
9725796c8dcSSimon Schubert       struct frame_info *frame;
9735796c8dcSSimon Schubert       int regnum;
9745796c8dcSSimon Schubert       struct type *type = check_typedef (value_type (val));
9755796c8dcSSimon Schubert       struct value *new_val = val, *mark = value_mark ();
9765796c8dcSSimon Schubert 
9775796c8dcSSimon Schubert       /* Offsets are not supported here; lazy register values must
9785796c8dcSSimon Schubert 	 refer to the entire register.  */
9795796c8dcSSimon Schubert       gdb_assert (value_offset (val) == 0);
9805796c8dcSSimon Schubert 
9815796c8dcSSimon Schubert       while (VALUE_LVAL (new_val) == lval_register && value_lazy (new_val))
9825796c8dcSSimon Schubert 	{
9835796c8dcSSimon Schubert 	  frame = frame_find_by_id (VALUE_FRAME_ID (new_val));
9845796c8dcSSimon Schubert 	  regnum = VALUE_REGNUM (new_val);
9855796c8dcSSimon Schubert 
9865796c8dcSSimon Schubert 	  gdb_assert (frame != NULL);
9875796c8dcSSimon Schubert 
9885796c8dcSSimon Schubert 	  /* Convertible register routines are used for multi-register
9895796c8dcSSimon Schubert 	     values and for interpretation in different types
9905796c8dcSSimon Schubert 	     (e.g. float or int from a double register).  Lazy
9915796c8dcSSimon Schubert 	     register values should have the register's natural type,
9925796c8dcSSimon Schubert 	     so they do not apply.  */
9935796c8dcSSimon Schubert 	  gdb_assert (!gdbarch_convert_register_p (get_frame_arch (frame),
9945796c8dcSSimon Schubert 						   regnum, type));
9955796c8dcSSimon Schubert 
9965796c8dcSSimon Schubert 	  new_val = get_frame_register_value (frame, regnum);
9975796c8dcSSimon Schubert 	}
9985796c8dcSSimon Schubert 
9995796c8dcSSimon Schubert       /* If it's still lazy (for instance, a saved register on the
10005796c8dcSSimon Schubert 	 stack), fetch it.  */
10015796c8dcSSimon Schubert       if (value_lazy (new_val))
10025796c8dcSSimon Schubert 	value_fetch_lazy (new_val);
10035796c8dcSSimon Schubert 
10045796c8dcSSimon Schubert       /* If the register was not saved, mark it unavailable.  */
10055796c8dcSSimon Schubert       if (value_optimized_out (new_val))
10065796c8dcSSimon Schubert 	set_value_optimized_out (val, 1);
10075796c8dcSSimon Schubert       else
10085796c8dcSSimon Schubert 	memcpy (value_contents_raw (val), value_contents (new_val),
10095796c8dcSSimon Schubert 		TYPE_LENGTH (type));
10105796c8dcSSimon Schubert 
10115796c8dcSSimon Schubert       if (frame_debug)
10125796c8dcSSimon Schubert 	{
10135796c8dcSSimon Schubert 	  struct gdbarch *gdbarch;
10145796c8dcSSimon Schubert 	  frame = frame_find_by_id (VALUE_FRAME_ID (val));
10155796c8dcSSimon Schubert 	  regnum = VALUE_REGNUM (val);
10165796c8dcSSimon Schubert 	  gdbarch = get_frame_arch (frame);
10175796c8dcSSimon Schubert 
10185796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stdlog, "\
10195796c8dcSSimon Schubert { value_fetch_lazy (frame=%d,regnum=%d(%s),...) ",
10205796c8dcSSimon Schubert 			      frame_relative_level (frame), regnum,
10215796c8dcSSimon Schubert 			      user_reg_map_regnum_to_name (gdbarch, regnum));
10225796c8dcSSimon Schubert 
10235796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stdlog, "->");
10245796c8dcSSimon Schubert 	  if (value_optimized_out (new_val))
10255796c8dcSSimon Schubert 	    fprintf_unfiltered (gdb_stdlog, " optimized out");
10265796c8dcSSimon Schubert 	  else
10275796c8dcSSimon Schubert 	    {
10285796c8dcSSimon Schubert 	      int i;
10295796c8dcSSimon Schubert 	      const gdb_byte *buf = value_contents (new_val);
10305796c8dcSSimon Schubert 
10315796c8dcSSimon Schubert 	      if (VALUE_LVAL (new_val) == lval_register)
10325796c8dcSSimon Schubert 		fprintf_unfiltered (gdb_stdlog, " register=%d",
10335796c8dcSSimon Schubert 				    VALUE_REGNUM (new_val));
10345796c8dcSSimon Schubert 	      else if (VALUE_LVAL (new_val) == lval_memory)
10355796c8dcSSimon Schubert 		fprintf_unfiltered (gdb_stdlog, " address=%s",
10365796c8dcSSimon Schubert 				    paddress (gdbarch,
10375796c8dcSSimon Schubert 					      value_address (new_val)));
10385796c8dcSSimon Schubert 	      else
10395796c8dcSSimon Schubert 		fprintf_unfiltered (gdb_stdlog, " computed");
10405796c8dcSSimon Schubert 
10415796c8dcSSimon Schubert 	      fprintf_unfiltered (gdb_stdlog, " bytes=");
10425796c8dcSSimon Schubert 	      fprintf_unfiltered (gdb_stdlog, "[");
10435796c8dcSSimon Schubert 	      for (i = 0; i < register_size (gdbarch, regnum); i++)
10445796c8dcSSimon Schubert 		fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
10455796c8dcSSimon Schubert 	      fprintf_unfiltered (gdb_stdlog, "]");
10465796c8dcSSimon Schubert 	    }
10475796c8dcSSimon Schubert 
10485796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stdlog, " }\n");
10495796c8dcSSimon Schubert 	}
10505796c8dcSSimon Schubert 
10515796c8dcSSimon Schubert       /* Dispose of the intermediate values.  This prevents
10525796c8dcSSimon Schubert 	 watchpoints from trying to watch the saved frame pointer.  */
10535796c8dcSSimon Schubert       value_free_to_mark (mark);
10545796c8dcSSimon Schubert     }
10555796c8dcSSimon Schubert   else if (VALUE_LVAL (val) == lval_computed)
10565796c8dcSSimon Schubert     value_computed_funcs (val)->read (val);
10575796c8dcSSimon Schubert   else
10585796c8dcSSimon Schubert     internal_error (__FILE__, __LINE__, "Unexpected lazy value type.");
10595796c8dcSSimon Schubert 
10605796c8dcSSimon Schubert   set_value_lazy (val, 0);
10615796c8dcSSimon Schubert   return 0;
10625796c8dcSSimon Schubert }
10635796c8dcSSimon Schubert 
10645796c8dcSSimon Schubert 
10655796c8dcSSimon Schubert /* Store the contents of FROMVAL into the location of TOVAL.
10665796c8dcSSimon Schubert    Return a new value with the location of TOVAL and contents of FROMVAL.  */
10675796c8dcSSimon Schubert 
10685796c8dcSSimon Schubert struct value *
10695796c8dcSSimon Schubert value_assign (struct value *toval, struct value *fromval)
10705796c8dcSSimon Schubert {
10715796c8dcSSimon Schubert   struct type *type;
10725796c8dcSSimon Schubert   struct value *val;
10735796c8dcSSimon Schubert   struct frame_id old_frame;
10745796c8dcSSimon Schubert 
10755796c8dcSSimon Schubert   if (!deprecated_value_modifiable (toval))
10765796c8dcSSimon Schubert     error (_("Left operand of assignment is not a modifiable lvalue."));
10775796c8dcSSimon Schubert 
10785796c8dcSSimon Schubert   toval = coerce_ref (toval);
10795796c8dcSSimon Schubert 
10805796c8dcSSimon Schubert   type = value_type (toval);
10815796c8dcSSimon Schubert   if (VALUE_LVAL (toval) != lval_internalvar)
10825796c8dcSSimon Schubert     {
10835796c8dcSSimon Schubert       toval = value_coerce_to_target (toval);
10845796c8dcSSimon Schubert       fromval = value_cast (type, fromval);
10855796c8dcSSimon Schubert     }
10865796c8dcSSimon Schubert   else
10875796c8dcSSimon Schubert     {
10885796c8dcSSimon Schubert       /* Coerce arrays and functions to pointers, except for arrays
10895796c8dcSSimon Schubert 	 which only live in GDB's storage.  */
10905796c8dcSSimon Schubert       if (!value_must_coerce_to_target (fromval))
10915796c8dcSSimon Schubert 	fromval = coerce_array (fromval);
10925796c8dcSSimon Schubert     }
10935796c8dcSSimon Schubert 
10945796c8dcSSimon Schubert   CHECK_TYPEDEF (type);
10955796c8dcSSimon Schubert 
10965796c8dcSSimon Schubert   /* Since modifying a register can trash the frame chain, and
10975796c8dcSSimon Schubert      modifying memory can trash the frame cache, we save the old frame
10985796c8dcSSimon Schubert      and then restore the new frame afterwards.  */
10995796c8dcSSimon Schubert   old_frame = get_frame_id (deprecated_safe_get_selected_frame ());
11005796c8dcSSimon Schubert 
11015796c8dcSSimon Schubert   switch (VALUE_LVAL (toval))
11025796c8dcSSimon Schubert     {
11035796c8dcSSimon Schubert     case lval_internalvar:
11045796c8dcSSimon Schubert       set_internalvar (VALUE_INTERNALVAR (toval), fromval);
11055796c8dcSSimon Schubert       val = value_copy (fromval);
11065796c8dcSSimon Schubert       val = value_change_enclosing_type (val,
11075796c8dcSSimon Schubert 					 value_enclosing_type (fromval));
11085796c8dcSSimon Schubert       set_value_embedded_offset (val, value_embedded_offset (fromval));
11095796c8dcSSimon Schubert       set_value_pointed_to_offset (val,
11105796c8dcSSimon Schubert 				   value_pointed_to_offset (fromval));
11115796c8dcSSimon Schubert       return val;
11125796c8dcSSimon Schubert 
11135796c8dcSSimon Schubert     case lval_internalvar_component:
11145796c8dcSSimon Schubert       set_internalvar_component (VALUE_INTERNALVAR (toval),
11155796c8dcSSimon Schubert 				 value_offset (toval),
11165796c8dcSSimon Schubert 				 value_bitpos (toval),
11175796c8dcSSimon Schubert 				 value_bitsize (toval),
11185796c8dcSSimon Schubert 				 fromval);
11195796c8dcSSimon Schubert       break;
11205796c8dcSSimon Schubert 
11215796c8dcSSimon Schubert     case lval_memory:
11225796c8dcSSimon Schubert       {
11235796c8dcSSimon Schubert 	const gdb_byte *dest_buffer;
11245796c8dcSSimon Schubert 	CORE_ADDR changed_addr;
11255796c8dcSSimon Schubert 	int changed_len;
11265796c8dcSSimon Schubert         gdb_byte buffer[sizeof (LONGEST)];
11275796c8dcSSimon Schubert 
11285796c8dcSSimon Schubert 	if (value_bitsize (toval))
11295796c8dcSSimon Schubert 	  {
11305796c8dcSSimon Schubert 	    struct value *parent = value_parent (toval);
11315796c8dcSSimon Schubert 
1132*cf7f2e2dSJohn Marino 	    changed_addr = value_address (parent) + value_offset (toval);
11335796c8dcSSimon Schubert 	    changed_len = (value_bitpos (toval)
11345796c8dcSSimon Schubert 			   + value_bitsize (toval)
11355796c8dcSSimon Schubert 			   + HOST_CHAR_BIT - 1)
11365796c8dcSSimon Schubert 	      / HOST_CHAR_BIT;
11375796c8dcSSimon Schubert 
11385796c8dcSSimon Schubert 	    /* If we can read-modify-write exactly the size of the
11395796c8dcSSimon Schubert 	       containing type (e.g. short or int) then do so.  This
11405796c8dcSSimon Schubert 	       is safer for volatile bitfields mapped to hardware
11415796c8dcSSimon Schubert 	       registers.  */
11425796c8dcSSimon Schubert 	    if (changed_len < TYPE_LENGTH (type)
11435796c8dcSSimon Schubert 		&& TYPE_LENGTH (type) <= (int) sizeof (LONGEST)
11445796c8dcSSimon Schubert 		&& ((LONGEST) changed_addr % TYPE_LENGTH (type)) == 0)
11455796c8dcSSimon Schubert 	      changed_len = TYPE_LENGTH (type);
11465796c8dcSSimon Schubert 
11475796c8dcSSimon Schubert 	    if (changed_len > (int) sizeof (LONGEST))
11485796c8dcSSimon Schubert 	      error (_("Can't handle bitfields which don't fit in a %d bit word."),
11495796c8dcSSimon Schubert 		     (int) sizeof (LONGEST) * HOST_CHAR_BIT);
11505796c8dcSSimon Schubert 
11515796c8dcSSimon Schubert 	    read_memory (changed_addr, buffer, changed_len);
11525796c8dcSSimon Schubert 	    modify_field (type, buffer, value_as_long (fromval),
11535796c8dcSSimon Schubert 			  value_bitpos (toval), value_bitsize (toval));
11545796c8dcSSimon Schubert 	    dest_buffer = buffer;
11555796c8dcSSimon Schubert 	  }
11565796c8dcSSimon Schubert 	else
11575796c8dcSSimon Schubert 	  {
11585796c8dcSSimon Schubert 	    changed_addr = value_address (toval);
11595796c8dcSSimon Schubert 	    changed_len = TYPE_LENGTH (type);
11605796c8dcSSimon Schubert 	    dest_buffer = value_contents (fromval);
11615796c8dcSSimon Schubert 	  }
11625796c8dcSSimon Schubert 
11635796c8dcSSimon Schubert 	write_memory (changed_addr, dest_buffer, changed_len);
1164*cf7f2e2dSJohn Marino 	observer_notify_memory_changed (changed_addr, changed_len,
1165*cf7f2e2dSJohn Marino 					dest_buffer);
11665796c8dcSSimon Schubert       }
11675796c8dcSSimon Schubert       break;
11685796c8dcSSimon Schubert 
11695796c8dcSSimon Schubert     case lval_register:
11705796c8dcSSimon Schubert       {
11715796c8dcSSimon Schubert 	struct frame_info *frame;
11725796c8dcSSimon Schubert 	struct gdbarch *gdbarch;
11735796c8dcSSimon Schubert 	int value_reg;
11745796c8dcSSimon Schubert 
11755796c8dcSSimon Schubert 	/* Figure out which frame this is in currently.  */
11765796c8dcSSimon Schubert 	frame = frame_find_by_id (VALUE_FRAME_ID (toval));
11775796c8dcSSimon Schubert 	value_reg = VALUE_REGNUM (toval);
11785796c8dcSSimon Schubert 
11795796c8dcSSimon Schubert 	if (!frame)
11805796c8dcSSimon Schubert 	  error (_("Value being assigned to is no longer active."));
11815796c8dcSSimon Schubert 
11825796c8dcSSimon Schubert 	gdbarch = get_frame_arch (frame);
11835796c8dcSSimon Schubert 	if (gdbarch_convert_register_p (gdbarch, VALUE_REGNUM (toval), type))
11845796c8dcSSimon Schubert 	  {
11855796c8dcSSimon Schubert 	    /* If TOVAL is a special machine register requiring
11865796c8dcSSimon Schubert 	       conversion of program values to a special raw
11875796c8dcSSimon Schubert 	       format.  */
11885796c8dcSSimon Schubert 	    gdbarch_value_to_register (gdbarch, frame,
11895796c8dcSSimon Schubert 				       VALUE_REGNUM (toval), type,
11905796c8dcSSimon Schubert 				       value_contents (fromval));
11915796c8dcSSimon Schubert 	  }
11925796c8dcSSimon Schubert 	else
11935796c8dcSSimon Schubert 	  {
11945796c8dcSSimon Schubert 	    if (value_bitsize (toval))
11955796c8dcSSimon Schubert 	      {
11965796c8dcSSimon Schubert 		struct value *parent = value_parent (toval);
11975796c8dcSSimon Schubert 		int offset = value_offset (parent) + value_offset (toval);
11985796c8dcSSimon Schubert 		int changed_len;
11995796c8dcSSimon Schubert 		gdb_byte buffer[sizeof (LONGEST)];
12005796c8dcSSimon Schubert 
12015796c8dcSSimon Schubert 		changed_len = (value_bitpos (toval)
12025796c8dcSSimon Schubert 			       + value_bitsize (toval)
12035796c8dcSSimon Schubert 			       + HOST_CHAR_BIT - 1)
12045796c8dcSSimon Schubert 		  / HOST_CHAR_BIT;
12055796c8dcSSimon Schubert 
12065796c8dcSSimon Schubert 		if (changed_len > (int) sizeof (LONGEST))
12075796c8dcSSimon Schubert 		  error (_("Can't handle bitfields which don't fit in a %d bit word."),
12085796c8dcSSimon Schubert 			 (int) sizeof (LONGEST) * HOST_CHAR_BIT);
12095796c8dcSSimon Schubert 
12105796c8dcSSimon Schubert 		get_frame_register_bytes (frame, value_reg, offset,
12115796c8dcSSimon Schubert 					  changed_len, buffer);
12125796c8dcSSimon Schubert 
12135796c8dcSSimon Schubert 		modify_field (type, buffer, value_as_long (fromval),
12145796c8dcSSimon Schubert 			      value_bitpos (toval), value_bitsize (toval));
12155796c8dcSSimon Schubert 
12165796c8dcSSimon Schubert 		put_frame_register_bytes (frame, value_reg, offset,
12175796c8dcSSimon Schubert 					  changed_len, buffer);
12185796c8dcSSimon Schubert 	      }
12195796c8dcSSimon Schubert 	    else
12205796c8dcSSimon Schubert 	      {
12215796c8dcSSimon Schubert 		put_frame_register_bytes (frame, value_reg,
12225796c8dcSSimon Schubert 					  value_offset (toval),
12235796c8dcSSimon Schubert 					  TYPE_LENGTH (type),
12245796c8dcSSimon Schubert 					  value_contents (fromval));
12255796c8dcSSimon Schubert 	      }
12265796c8dcSSimon Schubert 	  }
12275796c8dcSSimon Schubert 
12285796c8dcSSimon Schubert 	if (deprecated_register_changed_hook)
12295796c8dcSSimon Schubert 	  deprecated_register_changed_hook (-1);
12305796c8dcSSimon Schubert 	observer_notify_target_changed (&current_target);
12315796c8dcSSimon Schubert 	break;
12325796c8dcSSimon Schubert       }
12335796c8dcSSimon Schubert 
12345796c8dcSSimon Schubert     case lval_computed:
12355796c8dcSSimon Schubert       {
12365796c8dcSSimon Schubert 	struct lval_funcs *funcs = value_computed_funcs (toval);
12375796c8dcSSimon Schubert 
12385796c8dcSSimon Schubert 	funcs->write (toval, fromval);
12395796c8dcSSimon Schubert       }
12405796c8dcSSimon Schubert       break;
12415796c8dcSSimon Schubert 
12425796c8dcSSimon Schubert     default:
12435796c8dcSSimon Schubert       error (_("Left operand of assignment is not an lvalue."));
12445796c8dcSSimon Schubert     }
12455796c8dcSSimon Schubert 
12465796c8dcSSimon Schubert   /* Assigning to the stack pointer, frame pointer, and other
12475796c8dcSSimon Schubert      (architecture and calling convention specific) registers may
12485796c8dcSSimon Schubert      cause the frame cache to be out of date.  Assigning to memory
12495796c8dcSSimon Schubert      also can.  We just do this on all assignments to registers or
12505796c8dcSSimon Schubert      memory, for simplicity's sake; I doubt the slowdown matters.  */
12515796c8dcSSimon Schubert   switch (VALUE_LVAL (toval))
12525796c8dcSSimon Schubert     {
12535796c8dcSSimon Schubert     case lval_memory:
12545796c8dcSSimon Schubert     case lval_register:
1255*cf7f2e2dSJohn Marino     case lval_computed:
12565796c8dcSSimon Schubert 
12575796c8dcSSimon Schubert       reinit_frame_cache ();
12585796c8dcSSimon Schubert 
12595796c8dcSSimon Schubert       /* Having destroyed the frame cache, restore the selected
12605796c8dcSSimon Schubert 	 frame.  */
12615796c8dcSSimon Schubert 
12625796c8dcSSimon Schubert       /* FIXME: cagney/2002-11-02: There has to be a better way of
12635796c8dcSSimon Schubert 	 doing this.  Instead of constantly saving/restoring the
12645796c8dcSSimon Schubert 	 frame.  Why not create a get_selected_frame() function that,
12655796c8dcSSimon Schubert 	 having saved the selected frame's ID can automatically
12665796c8dcSSimon Schubert 	 re-find the previously selected frame automatically.  */
12675796c8dcSSimon Schubert 
12685796c8dcSSimon Schubert       {
12695796c8dcSSimon Schubert 	struct frame_info *fi = frame_find_by_id (old_frame);
1270*cf7f2e2dSJohn Marino 
12715796c8dcSSimon Schubert 	if (fi != NULL)
12725796c8dcSSimon Schubert 	  select_frame (fi);
12735796c8dcSSimon Schubert       }
12745796c8dcSSimon Schubert 
12755796c8dcSSimon Schubert       break;
12765796c8dcSSimon Schubert     default:
12775796c8dcSSimon Schubert       break;
12785796c8dcSSimon Schubert     }
12795796c8dcSSimon Schubert 
12805796c8dcSSimon Schubert   /* If the field does not entirely fill a LONGEST, then zero the sign
12815796c8dcSSimon Schubert      bits.  If the field is signed, and is negative, then sign
12825796c8dcSSimon Schubert      extend.  */
12835796c8dcSSimon Schubert   if ((value_bitsize (toval) > 0)
12845796c8dcSSimon Schubert       && (value_bitsize (toval) < 8 * (int) sizeof (LONGEST)))
12855796c8dcSSimon Schubert     {
12865796c8dcSSimon Schubert       LONGEST fieldval = value_as_long (fromval);
12875796c8dcSSimon Schubert       LONGEST valmask = (((ULONGEST) 1) << value_bitsize (toval)) - 1;
12885796c8dcSSimon Schubert 
12895796c8dcSSimon Schubert       fieldval &= valmask;
12905796c8dcSSimon Schubert       if (!TYPE_UNSIGNED (type)
12915796c8dcSSimon Schubert 	  && (fieldval & (valmask ^ (valmask >> 1))))
12925796c8dcSSimon Schubert 	fieldval |= ~valmask;
12935796c8dcSSimon Schubert 
12945796c8dcSSimon Schubert       fromval = value_from_longest (type, fieldval);
12955796c8dcSSimon Schubert     }
12965796c8dcSSimon Schubert 
12975796c8dcSSimon Schubert   val = value_copy (toval);
12985796c8dcSSimon Schubert   memcpy (value_contents_raw (val), value_contents (fromval),
12995796c8dcSSimon Schubert 	  TYPE_LENGTH (type));
13005796c8dcSSimon Schubert   deprecated_set_value_type (val, type);
13015796c8dcSSimon Schubert   val = value_change_enclosing_type (val,
13025796c8dcSSimon Schubert 				     value_enclosing_type (fromval));
13035796c8dcSSimon Schubert   set_value_embedded_offset (val, value_embedded_offset (fromval));
13045796c8dcSSimon Schubert   set_value_pointed_to_offset (val, value_pointed_to_offset (fromval));
13055796c8dcSSimon Schubert 
13065796c8dcSSimon Schubert   return val;
13075796c8dcSSimon Schubert }
13085796c8dcSSimon Schubert 
13095796c8dcSSimon Schubert /* Extend a value VAL to COUNT repetitions of its type.  */
13105796c8dcSSimon Schubert 
13115796c8dcSSimon Schubert struct value *
13125796c8dcSSimon Schubert value_repeat (struct value *arg1, int count)
13135796c8dcSSimon Schubert {
13145796c8dcSSimon Schubert   struct value *val;
13155796c8dcSSimon Schubert 
13165796c8dcSSimon Schubert   if (VALUE_LVAL (arg1) != lval_memory)
13175796c8dcSSimon Schubert     error (_("Only values in memory can be extended with '@'."));
13185796c8dcSSimon Schubert   if (count < 1)
13195796c8dcSSimon Schubert     error (_("Invalid number %d of repetitions."), count);
13205796c8dcSSimon Schubert 
13215796c8dcSSimon Schubert   val = allocate_repeat_value (value_enclosing_type (arg1), count);
13225796c8dcSSimon Schubert 
13235796c8dcSSimon Schubert   read_memory (value_address (arg1),
13245796c8dcSSimon Schubert 	       value_contents_all_raw (val),
13255796c8dcSSimon Schubert 	       TYPE_LENGTH (value_enclosing_type (val)));
13265796c8dcSSimon Schubert   VALUE_LVAL (val) = lval_memory;
13275796c8dcSSimon Schubert   set_value_address (val, value_address (arg1));
13285796c8dcSSimon Schubert 
13295796c8dcSSimon Schubert   return val;
13305796c8dcSSimon Schubert }
13315796c8dcSSimon Schubert 
13325796c8dcSSimon Schubert struct value *
13335796c8dcSSimon Schubert value_of_variable (struct symbol *var, struct block *b)
13345796c8dcSSimon Schubert {
13355796c8dcSSimon Schubert   struct value *val;
13365796c8dcSSimon Schubert   struct frame_info *frame;
13375796c8dcSSimon Schubert 
13385796c8dcSSimon Schubert   if (!symbol_read_needs_frame (var))
13395796c8dcSSimon Schubert     frame = NULL;
13405796c8dcSSimon Schubert   else if (!b)
13415796c8dcSSimon Schubert     frame = get_selected_frame (_("No frame selected."));
13425796c8dcSSimon Schubert   else
13435796c8dcSSimon Schubert     {
13445796c8dcSSimon Schubert       frame = block_innermost_frame (b);
13455796c8dcSSimon Schubert       if (!frame)
13465796c8dcSSimon Schubert 	{
13475796c8dcSSimon Schubert 	  if (BLOCK_FUNCTION (b) && !block_inlined_p (b)
13485796c8dcSSimon Schubert 	      && SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)))
13495796c8dcSSimon Schubert 	    error (_("No frame is currently executing in block %s."),
13505796c8dcSSimon Schubert 		   SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)));
13515796c8dcSSimon Schubert 	  else
13525796c8dcSSimon Schubert 	    error (_("No frame is currently executing in specified block"));
13535796c8dcSSimon Schubert 	}
13545796c8dcSSimon Schubert     }
13555796c8dcSSimon Schubert 
13565796c8dcSSimon Schubert   val = read_var_value (var, frame);
13575796c8dcSSimon Schubert   if (!val)
13585796c8dcSSimon Schubert     error (_("Address of symbol \"%s\" is unknown."), SYMBOL_PRINT_NAME (var));
13595796c8dcSSimon Schubert 
13605796c8dcSSimon Schubert   return val;
13615796c8dcSSimon Schubert }
13625796c8dcSSimon Schubert 
13635796c8dcSSimon Schubert struct value *
13645796c8dcSSimon Schubert address_of_variable (struct symbol *var, struct block *b)
13655796c8dcSSimon Schubert {
13665796c8dcSSimon Schubert   struct type *type = SYMBOL_TYPE (var);
13675796c8dcSSimon Schubert   struct value *val;
13685796c8dcSSimon Schubert 
13695796c8dcSSimon Schubert   /* Evaluate it first; if the result is a memory address, we're fine.
13705796c8dcSSimon Schubert      Lazy evaluation pays off here. */
13715796c8dcSSimon Schubert 
13725796c8dcSSimon Schubert   val = value_of_variable (var, b);
13735796c8dcSSimon Schubert 
13745796c8dcSSimon Schubert   if ((VALUE_LVAL (val) == lval_memory && value_lazy (val))
13755796c8dcSSimon Schubert       || TYPE_CODE (type) == TYPE_CODE_FUNC)
13765796c8dcSSimon Schubert     {
13775796c8dcSSimon Schubert       CORE_ADDR addr = value_address (val);
1378*cf7f2e2dSJohn Marino 
13795796c8dcSSimon Schubert       return value_from_pointer (lookup_pointer_type (type), addr);
13805796c8dcSSimon Schubert     }
13815796c8dcSSimon Schubert 
13825796c8dcSSimon Schubert   /* Not a memory address; check what the problem was.  */
13835796c8dcSSimon Schubert   switch (VALUE_LVAL (val))
13845796c8dcSSimon Schubert     {
13855796c8dcSSimon Schubert     case lval_register:
13865796c8dcSSimon Schubert       {
13875796c8dcSSimon Schubert 	struct frame_info *frame;
13885796c8dcSSimon Schubert 	const char *regname;
13895796c8dcSSimon Schubert 
13905796c8dcSSimon Schubert 	frame = frame_find_by_id (VALUE_FRAME_ID (val));
13915796c8dcSSimon Schubert 	gdb_assert (frame);
13925796c8dcSSimon Schubert 
13935796c8dcSSimon Schubert 	regname = gdbarch_register_name (get_frame_arch (frame),
13945796c8dcSSimon Schubert 					 VALUE_REGNUM (val));
13955796c8dcSSimon Schubert 	gdb_assert (regname && *regname);
13965796c8dcSSimon Schubert 
13975796c8dcSSimon Schubert 	error (_("Address requested for identifier "
13985796c8dcSSimon Schubert 		 "\"%s\" which is in register $%s"),
13995796c8dcSSimon Schubert 	       SYMBOL_PRINT_NAME (var), regname);
14005796c8dcSSimon Schubert 	break;
14015796c8dcSSimon Schubert       }
14025796c8dcSSimon Schubert 
14035796c8dcSSimon Schubert     default:
14045796c8dcSSimon Schubert       error (_("Can't take address of \"%s\" which isn't an lvalue."),
14055796c8dcSSimon Schubert 	     SYMBOL_PRINT_NAME (var));
14065796c8dcSSimon Schubert       break;
14075796c8dcSSimon Schubert     }
14085796c8dcSSimon Schubert 
14095796c8dcSSimon Schubert   return val;
14105796c8dcSSimon Schubert }
14115796c8dcSSimon Schubert 
14125796c8dcSSimon Schubert /* Return one if VAL does not live in target memory, but should in order
14135796c8dcSSimon Schubert    to operate on it.  Otherwise return zero.  */
14145796c8dcSSimon Schubert 
14155796c8dcSSimon Schubert int
14165796c8dcSSimon Schubert value_must_coerce_to_target (struct value *val)
14175796c8dcSSimon Schubert {
14185796c8dcSSimon Schubert   struct type *valtype;
14195796c8dcSSimon Schubert 
14205796c8dcSSimon Schubert   /* The only lval kinds which do not live in target memory.  */
14215796c8dcSSimon Schubert   if (VALUE_LVAL (val) != not_lval
14225796c8dcSSimon Schubert       && VALUE_LVAL (val) != lval_internalvar)
14235796c8dcSSimon Schubert     return 0;
14245796c8dcSSimon Schubert 
14255796c8dcSSimon Schubert   valtype = check_typedef (value_type (val));
14265796c8dcSSimon Schubert 
14275796c8dcSSimon Schubert   switch (TYPE_CODE (valtype))
14285796c8dcSSimon Schubert     {
14295796c8dcSSimon Schubert     case TYPE_CODE_ARRAY:
14305796c8dcSSimon Schubert     case TYPE_CODE_STRING:
14315796c8dcSSimon Schubert       return 1;
14325796c8dcSSimon Schubert     default:
14335796c8dcSSimon Schubert       return 0;
14345796c8dcSSimon Schubert     }
14355796c8dcSSimon Schubert }
14365796c8dcSSimon Schubert 
14375796c8dcSSimon Schubert /* Make sure that VAL lives in target memory if it's supposed to.  For instance,
14385796c8dcSSimon Schubert    strings are constructed as character arrays in GDB's storage, and this
14395796c8dcSSimon Schubert    function copies them to the target.  */
14405796c8dcSSimon Schubert 
14415796c8dcSSimon Schubert struct value *
14425796c8dcSSimon Schubert value_coerce_to_target (struct value *val)
14435796c8dcSSimon Schubert {
14445796c8dcSSimon Schubert   LONGEST length;
14455796c8dcSSimon Schubert   CORE_ADDR addr;
14465796c8dcSSimon Schubert 
14475796c8dcSSimon Schubert   if (!value_must_coerce_to_target (val))
14485796c8dcSSimon Schubert     return val;
14495796c8dcSSimon Schubert 
14505796c8dcSSimon Schubert   length = TYPE_LENGTH (check_typedef (value_type (val)));
14515796c8dcSSimon Schubert   addr = allocate_space_in_inferior (length);
14525796c8dcSSimon Schubert   write_memory (addr, value_contents (val), length);
14535796c8dcSSimon Schubert   return value_at_lazy (value_type (val), addr);
14545796c8dcSSimon Schubert }
14555796c8dcSSimon Schubert 
14565796c8dcSSimon Schubert /* Given a value which is an array, return a value which is a pointer
14575796c8dcSSimon Schubert    to its first element, regardless of whether or not the array has a
14585796c8dcSSimon Schubert    nonzero lower bound.
14595796c8dcSSimon Schubert 
14605796c8dcSSimon Schubert    FIXME: A previous comment here indicated that this routine should
14615796c8dcSSimon Schubert    be substracting the array's lower bound.  It's not clear to me that
14625796c8dcSSimon Schubert    this is correct.  Given an array subscripting operation, it would
14635796c8dcSSimon Schubert    certainly work to do the adjustment here, essentially computing:
14645796c8dcSSimon Schubert 
14655796c8dcSSimon Schubert    (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0])
14665796c8dcSSimon Schubert 
14675796c8dcSSimon Schubert    However I believe a more appropriate and logical place to account
14685796c8dcSSimon Schubert    for the lower bound is to do so in value_subscript, essentially
14695796c8dcSSimon Schubert    computing:
14705796c8dcSSimon Schubert 
14715796c8dcSSimon Schubert    (&array[0] + ((index - lowerbound) * sizeof array[0]))
14725796c8dcSSimon Schubert 
14735796c8dcSSimon Schubert    As further evidence consider what would happen with operations
14745796c8dcSSimon Schubert    other than array subscripting, where the caller would get back a
14755796c8dcSSimon Schubert    value that had an address somewhere before the actual first element
14765796c8dcSSimon Schubert    of the array, and the information about the lower bound would be
14775796c8dcSSimon Schubert    lost because of the coercion to pointer type.
14785796c8dcSSimon Schubert  */
14795796c8dcSSimon Schubert 
14805796c8dcSSimon Schubert struct value *
14815796c8dcSSimon Schubert value_coerce_array (struct value *arg1)
14825796c8dcSSimon Schubert {
14835796c8dcSSimon Schubert   struct type *type = check_typedef (value_type (arg1));
14845796c8dcSSimon Schubert 
14855796c8dcSSimon Schubert   /* If the user tries to do something requiring a pointer with an
14865796c8dcSSimon Schubert      array that has not yet been pushed to the target, then this would
14875796c8dcSSimon Schubert      be a good time to do so.  */
14885796c8dcSSimon Schubert   arg1 = value_coerce_to_target (arg1);
14895796c8dcSSimon Schubert 
14905796c8dcSSimon Schubert   if (VALUE_LVAL (arg1) != lval_memory)
14915796c8dcSSimon Schubert     error (_("Attempt to take address of value not located in memory."));
14925796c8dcSSimon Schubert 
14935796c8dcSSimon Schubert   return value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
14945796c8dcSSimon Schubert 			     value_address (arg1));
14955796c8dcSSimon Schubert }
14965796c8dcSSimon Schubert 
14975796c8dcSSimon Schubert /* Given a value which is a function, return a value which is a pointer
14985796c8dcSSimon Schubert    to it.  */
14995796c8dcSSimon Schubert 
15005796c8dcSSimon Schubert struct value *
15015796c8dcSSimon Schubert value_coerce_function (struct value *arg1)
15025796c8dcSSimon Schubert {
15035796c8dcSSimon Schubert   struct value *retval;
15045796c8dcSSimon Schubert 
15055796c8dcSSimon Schubert   if (VALUE_LVAL (arg1) != lval_memory)
15065796c8dcSSimon Schubert     error (_("Attempt to take address of value not located in memory."));
15075796c8dcSSimon Schubert 
15085796c8dcSSimon Schubert   retval = value_from_pointer (lookup_pointer_type (value_type (arg1)),
15095796c8dcSSimon Schubert 			       value_address (arg1));
15105796c8dcSSimon Schubert   return retval;
15115796c8dcSSimon Schubert }
15125796c8dcSSimon Schubert 
15135796c8dcSSimon Schubert /* Return a pointer value for the object for which ARG1 is the
15145796c8dcSSimon Schubert    contents.  */
15155796c8dcSSimon Schubert 
15165796c8dcSSimon Schubert struct value *
15175796c8dcSSimon Schubert value_addr (struct value *arg1)
15185796c8dcSSimon Schubert {
15195796c8dcSSimon Schubert   struct value *arg2;
15205796c8dcSSimon Schubert   struct type *type = check_typedef (value_type (arg1));
1521*cf7f2e2dSJohn Marino 
15225796c8dcSSimon Schubert   if (TYPE_CODE (type) == TYPE_CODE_REF)
15235796c8dcSSimon Schubert     {
15245796c8dcSSimon Schubert       /* Copy the value, but change the type from (T&) to (T*).  We
15255796c8dcSSimon Schubert          keep the same location information, which is efficient, and
15265796c8dcSSimon Schubert          allows &(&X) to get the location containing the reference.  */
15275796c8dcSSimon Schubert       arg2 = value_copy (arg1);
15285796c8dcSSimon Schubert       deprecated_set_value_type (arg2,
15295796c8dcSSimon Schubert 				 lookup_pointer_type (TYPE_TARGET_TYPE (type)));
15305796c8dcSSimon Schubert       return arg2;
15315796c8dcSSimon Schubert     }
15325796c8dcSSimon Schubert   if (TYPE_CODE (type) == TYPE_CODE_FUNC)
15335796c8dcSSimon Schubert     return value_coerce_function (arg1);
15345796c8dcSSimon Schubert 
15355796c8dcSSimon Schubert   /* If this is an array that has not yet been pushed to the target,
15365796c8dcSSimon Schubert      then this would be a good time to force it to memory.  */
15375796c8dcSSimon Schubert   arg1 = value_coerce_to_target (arg1);
15385796c8dcSSimon Schubert 
15395796c8dcSSimon Schubert   if (VALUE_LVAL (arg1) != lval_memory)
15405796c8dcSSimon Schubert     error (_("Attempt to take address of value not located in memory."));
15415796c8dcSSimon Schubert 
15425796c8dcSSimon Schubert   /* Get target memory address */
15435796c8dcSSimon Schubert   arg2 = value_from_pointer (lookup_pointer_type (value_type (arg1)),
15445796c8dcSSimon Schubert 			     (value_address (arg1)
15455796c8dcSSimon Schubert 			      + value_embedded_offset (arg1)));
15465796c8dcSSimon Schubert 
15475796c8dcSSimon Schubert   /* This may be a pointer to a base subobject; so remember the
15485796c8dcSSimon Schubert      full derived object's type ...  */
15495796c8dcSSimon Schubert   arg2 = value_change_enclosing_type (arg2, lookup_pointer_type (value_enclosing_type (arg1)));
15505796c8dcSSimon Schubert   /* ... and also the relative position of the subobject in the full
15515796c8dcSSimon Schubert      object.  */
15525796c8dcSSimon Schubert   set_value_pointed_to_offset (arg2, value_embedded_offset (arg1));
15535796c8dcSSimon Schubert   return arg2;
15545796c8dcSSimon Schubert }
15555796c8dcSSimon Schubert 
15565796c8dcSSimon Schubert /* Return a reference value for the object for which ARG1 is the
15575796c8dcSSimon Schubert    contents.  */
15585796c8dcSSimon Schubert 
15595796c8dcSSimon Schubert struct value *
15605796c8dcSSimon Schubert value_ref (struct value *arg1)
15615796c8dcSSimon Schubert {
15625796c8dcSSimon Schubert   struct value *arg2;
15635796c8dcSSimon Schubert   struct type *type = check_typedef (value_type (arg1));
1564*cf7f2e2dSJohn Marino 
15655796c8dcSSimon Schubert   if (TYPE_CODE (type) == TYPE_CODE_REF)
15665796c8dcSSimon Schubert     return arg1;
15675796c8dcSSimon Schubert 
15685796c8dcSSimon Schubert   arg2 = value_addr (arg1);
15695796c8dcSSimon Schubert   deprecated_set_value_type (arg2, lookup_reference_type (type));
15705796c8dcSSimon Schubert   return arg2;
15715796c8dcSSimon Schubert }
15725796c8dcSSimon Schubert 
15735796c8dcSSimon Schubert /* Given a value of a pointer type, apply the C unary * operator to
15745796c8dcSSimon Schubert    it.  */
15755796c8dcSSimon Schubert 
15765796c8dcSSimon Schubert struct value *
15775796c8dcSSimon Schubert value_ind (struct value *arg1)
15785796c8dcSSimon Schubert {
15795796c8dcSSimon Schubert   struct type *base_type;
15805796c8dcSSimon Schubert   struct value *arg2;
15815796c8dcSSimon Schubert 
15825796c8dcSSimon Schubert   arg1 = coerce_array (arg1);
15835796c8dcSSimon Schubert 
15845796c8dcSSimon Schubert   base_type = check_typedef (value_type (arg1));
15855796c8dcSSimon Schubert 
15865796c8dcSSimon Schubert   if (TYPE_CODE (base_type) == TYPE_CODE_PTR)
15875796c8dcSSimon Schubert     {
15885796c8dcSSimon Schubert       struct type *enc_type;
1589*cf7f2e2dSJohn Marino 
15905796c8dcSSimon Schubert       /* We may be pointing to something embedded in a larger object.
15915796c8dcSSimon Schubert          Get the real type of the enclosing object.  */
15925796c8dcSSimon Schubert       enc_type = check_typedef (value_enclosing_type (arg1));
15935796c8dcSSimon Schubert       enc_type = TYPE_TARGET_TYPE (enc_type);
15945796c8dcSSimon Schubert 
15955796c8dcSSimon Schubert       if (TYPE_CODE (check_typedef (enc_type)) == TYPE_CODE_FUNC
15965796c8dcSSimon Schubert 	  || TYPE_CODE (check_typedef (enc_type)) == TYPE_CODE_METHOD)
15975796c8dcSSimon Schubert 	/* For functions, go through find_function_addr, which knows
15985796c8dcSSimon Schubert 	   how to handle function descriptors.  */
15995796c8dcSSimon Schubert 	arg2 = value_at_lazy (enc_type,
16005796c8dcSSimon Schubert 			      find_function_addr (arg1, NULL));
16015796c8dcSSimon Schubert       else
16025796c8dcSSimon Schubert 	/* Retrieve the enclosing object pointed to */
16035796c8dcSSimon Schubert 	arg2 = value_at_lazy (enc_type,
16045796c8dcSSimon Schubert 			      (value_as_address (arg1)
16055796c8dcSSimon Schubert 			       - value_pointed_to_offset (arg1)));
16065796c8dcSSimon Schubert 
16075796c8dcSSimon Schubert       /* Re-adjust type.  */
16085796c8dcSSimon Schubert       deprecated_set_value_type (arg2, TYPE_TARGET_TYPE (base_type));
16095796c8dcSSimon Schubert       /* Add embedding info.  */
16105796c8dcSSimon Schubert       arg2 = value_change_enclosing_type (arg2, enc_type);
16115796c8dcSSimon Schubert       set_value_embedded_offset (arg2, value_pointed_to_offset (arg1));
16125796c8dcSSimon Schubert 
16135796c8dcSSimon Schubert       /* We may be pointing to an object of some derived type.  */
16145796c8dcSSimon Schubert       arg2 = value_full_object (arg2, NULL, 0, 0, 0);
16155796c8dcSSimon Schubert       return arg2;
16165796c8dcSSimon Schubert     }
16175796c8dcSSimon Schubert 
16185796c8dcSSimon Schubert   error (_("Attempt to take contents of a non-pointer value."));
16195796c8dcSSimon Schubert   return 0;			/* For lint -- never reached.  */
16205796c8dcSSimon Schubert }
16215796c8dcSSimon Schubert 
16225796c8dcSSimon Schubert /* Create a value for an array by allocating space in GDB, copying
16235796c8dcSSimon Schubert    copying the data into that space, and then setting up an array
16245796c8dcSSimon Schubert    value.
16255796c8dcSSimon Schubert 
16265796c8dcSSimon Schubert    The array bounds are set from LOWBOUND and HIGHBOUND, and the array
16275796c8dcSSimon Schubert    is populated from the values passed in ELEMVEC.
16285796c8dcSSimon Schubert 
16295796c8dcSSimon Schubert    The element type of the array is inherited from the type of the
16305796c8dcSSimon Schubert    first element, and all elements must have the same size (though we
16315796c8dcSSimon Schubert    don't currently enforce any restriction on their types).  */
16325796c8dcSSimon Schubert 
16335796c8dcSSimon Schubert struct value *
16345796c8dcSSimon Schubert value_array (int lowbound, int highbound, struct value **elemvec)
16355796c8dcSSimon Schubert {
16365796c8dcSSimon Schubert   int nelem;
16375796c8dcSSimon Schubert   int idx;
16385796c8dcSSimon Schubert   unsigned int typelength;
16395796c8dcSSimon Schubert   struct value *val;
16405796c8dcSSimon Schubert   struct type *arraytype;
16415796c8dcSSimon Schubert 
16425796c8dcSSimon Schubert   /* Validate that the bounds are reasonable and that each of the
16435796c8dcSSimon Schubert      elements have the same size.  */
16445796c8dcSSimon Schubert 
16455796c8dcSSimon Schubert   nelem = highbound - lowbound + 1;
16465796c8dcSSimon Schubert   if (nelem <= 0)
16475796c8dcSSimon Schubert     {
16485796c8dcSSimon Schubert       error (_("bad array bounds (%d, %d)"), lowbound, highbound);
16495796c8dcSSimon Schubert     }
16505796c8dcSSimon Schubert   typelength = TYPE_LENGTH (value_enclosing_type (elemvec[0]));
16515796c8dcSSimon Schubert   for (idx = 1; idx < nelem; idx++)
16525796c8dcSSimon Schubert     {
16535796c8dcSSimon Schubert       if (TYPE_LENGTH (value_enclosing_type (elemvec[idx])) != typelength)
16545796c8dcSSimon Schubert 	{
16555796c8dcSSimon Schubert 	  error (_("array elements must all be the same size"));
16565796c8dcSSimon Schubert 	}
16575796c8dcSSimon Schubert     }
16585796c8dcSSimon Schubert 
16595796c8dcSSimon Schubert   arraytype = lookup_array_range_type (value_enclosing_type (elemvec[0]),
16605796c8dcSSimon Schubert 				       lowbound, highbound);
16615796c8dcSSimon Schubert 
16625796c8dcSSimon Schubert   if (!current_language->c_style_arrays)
16635796c8dcSSimon Schubert     {
16645796c8dcSSimon Schubert       val = allocate_value (arraytype);
16655796c8dcSSimon Schubert       for (idx = 0; idx < nelem; idx++)
16665796c8dcSSimon Schubert 	{
16675796c8dcSSimon Schubert 	  memcpy (value_contents_all_raw (val) + (idx * typelength),
16685796c8dcSSimon Schubert 		  value_contents_all (elemvec[idx]),
16695796c8dcSSimon Schubert 		  typelength);
16705796c8dcSSimon Schubert 	}
16715796c8dcSSimon Schubert       return val;
16725796c8dcSSimon Schubert     }
16735796c8dcSSimon Schubert 
16745796c8dcSSimon Schubert   /* Allocate space to store the array, and then initialize it by
16755796c8dcSSimon Schubert      copying in each element.  */
16765796c8dcSSimon Schubert 
16775796c8dcSSimon Schubert   val = allocate_value (arraytype);
16785796c8dcSSimon Schubert   for (idx = 0; idx < nelem; idx++)
16795796c8dcSSimon Schubert     memcpy (value_contents_writeable (val) + (idx * typelength),
16805796c8dcSSimon Schubert 	    value_contents_all (elemvec[idx]),
16815796c8dcSSimon Schubert 	    typelength);
16825796c8dcSSimon Schubert   return val;
16835796c8dcSSimon Schubert }
16845796c8dcSSimon Schubert 
16855796c8dcSSimon Schubert struct value *
16865796c8dcSSimon Schubert value_cstring (char *ptr, int len, struct type *char_type)
16875796c8dcSSimon Schubert {
16885796c8dcSSimon Schubert   struct value *val;
16895796c8dcSSimon Schubert   int lowbound = current_language->string_lower_bound;
16905796c8dcSSimon Schubert   int highbound = len / TYPE_LENGTH (char_type);
16915796c8dcSSimon Schubert   struct type *stringtype
16925796c8dcSSimon Schubert     = lookup_array_range_type (char_type, lowbound, highbound + lowbound - 1);
16935796c8dcSSimon Schubert 
16945796c8dcSSimon Schubert   val = allocate_value (stringtype);
16955796c8dcSSimon Schubert   memcpy (value_contents_raw (val), ptr, len);
16965796c8dcSSimon Schubert   return val;
16975796c8dcSSimon Schubert }
16985796c8dcSSimon Schubert 
16995796c8dcSSimon Schubert /* Create a value for a string constant by allocating space in the
17005796c8dcSSimon Schubert    inferior, copying the data into that space, and returning the
17015796c8dcSSimon Schubert    address with type TYPE_CODE_STRING.  PTR points to the string
17025796c8dcSSimon Schubert    constant data; LEN is number of characters.
17035796c8dcSSimon Schubert 
17045796c8dcSSimon Schubert    Note that string types are like array of char types with a lower
17055796c8dcSSimon Schubert    bound of zero and an upper bound of LEN - 1.  Also note that the
17065796c8dcSSimon Schubert    string may contain embedded null bytes.  */
17075796c8dcSSimon Schubert 
17085796c8dcSSimon Schubert struct value *
17095796c8dcSSimon Schubert value_string (char *ptr, int len, struct type *char_type)
17105796c8dcSSimon Schubert {
17115796c8dcSSimon Schubert   struct value *val;
17125796c8dcSSimon Schubert   int lowbound = current_language->string_lower_bound;
17135796c8dcSSimon Schubert   int highbound = len / TYPE_LENGTH (char_type);
17145796c8dcSSimon Schubert   struct type *stringtype
17155796c8dcSSimon Schubert     = lookup_string_range_type (char_type, lowbound, highbound + lowbound - 1);
17165796c8dcSSimon Schubert 
17175796c8dcSSimon Schubert   val = allocate_value (stringtype);
17185796c8dcSSimon Schubert   memcpy (value_contents_raw (val), ptr, len);
17195796c8dcSSimon Schubert   return val;
17205796c8dcSSimon Schubert }
17215796c8dcSSimon Schubert 
17225796c8dcSSimon Schubert struct value *
17235796c8dcSSimon Schubert value_bitstring (char *ptr, int len, struct type *index_type)
17245796c8dcSSimon Schubert {
17255796c8dcSSimon Schubert   struct value *val;
17265796c8dcSSimon Schubert   struct type *domain_type
17275796c8dcSSimon Schubert     = create_range_type (NULL, index_type, 0, len - 1);
17285796c8dcSSimon Schubert   struct type *type = create_set_type (NULL, domain_type);
1729*cf7f2e2dSJohn Marino 
17305796c8dcSSimon Schubert   TYPE_CODE (type) = TYPE_CODE_BITSTRING;
17315796c8dcSSimon Schubert   val = allocate_value (type);
17325796c8dcSSimon Schubert   memcpy (value_contents_raw (val), ptr, TYPE_LENGTH (type));
17335796c8dcSSimon Schubert   return val;
17345796c8dcSSimon Schubert }
17355796c8dcSSimon Schubert 
17365796c8dcSSimon Schubert /* See if we can pass arguments in T2 to a function which takes
17375796c8dcSSimon Schubert    arguments of types T1.  T1 is a list of NARGS arguments, and T2 is
17385796c8dcSSimon Schubert    a NULL-terminated vector.  If some arguments need coercion of some
17395796c8dcSSimon Schubert    sort, then the coerced values are written into T2.  Return value is
17405796c8dcSSimon Schubert    0 if the arguments could be matched, or the position at which they
17415796c8dcSSimon Schubert    differ if not.
17425796c8dcSSimon Schubert 
17435796c8dcSSimon Schubert    STATICP is nonzero if the T1 argument list came from a static
17445796c8dcSSimon Schubert    member function.  T2 will still include the ``this'' pointer, but
17455796c8dcSSimon Schubert    it will be skipped.
17465796c8dcSSimon Schubert 
17475796c8dcSSimon Schubert    For non-static member functions, we ignore the first argument,
17485796c8dcSSimon Schubert    which is the type of the instance variable.  This is because we
17495796c8dcSSimon Schubert    want to handle calls with objects from derived classes.  This is
17505796c8dcSSimon Schubert    not entirely correct: we should actually check to make sure that a
17515796c8dcSSimon Schubert    requested operation is type secure, shouldn't we?  FIXME.  */
17525796c8dcSSimon Schubert 
17535796c8dcSSimon Schubert static int
17545796c8dcSSimon Schubert typecmp (int staticp, int varargs, int nargs,
17555796c8dcSSimon Schubert 	 struct field t1[], struct value *t2[])
17565796c8dcSSimon Schubert {
17575796c8dcSSimon Schubert   int i;
17585796c8dcSSimon Schubert 
17595796c8dcSSimon Schubert   if (t2 == 0)
17605796c8dcSSimon Schubert     internal_error (__FILE__, __LINE__,
17615796c8dcSSimon Schubert 		    _("typecmp: no argument list"));
17625796c8dcSSimon Schubert 
17635796c8dcSSimon Schubert   /* Skip ``this'' argument if applicable.  T2 will always include
17645796c8dcSSimon Schubert      THIS.  */
17655796c8dcSSimon Schubert   if (staticp)
17665796c8dcSSimon Schubert     t2 ++;
17675796c8dcSSimon Schubert 
17685796c8dcSSimon Schubert   for (i = 0;
17695796c8dcSSimon Schubert        (i < nargs) && TYPE_CODE (t1[i].type) != TYPE_CODE_VOID;
17705796c8dcSSimon Schubert        i++)
17715796c8dcSSimon Schubert     {
17725796c8dcSSimon Schubert       struct type *tt1, *tt2;
17735796c8dcSSimon Schubert 
17745796c8dcSSimon Schubert       if (!t2[i])
17755796c8dcSSimon Schubert 	return i + 1;
17765796c8dcSSimon Schubert 
17775796c8dcSSimon Schubert       tt1 = check_typedef (t1[i].type);
17785796c8dcSSimon Schubert       tt2 = check_typedef (value_type (t2[i]));
17795796c8dcSSimon Schubert 
17805796c8dcSSimon Schubert       if (TYPE_CODE (tt1) == TYPE_CODE_REF
17815796c8dcSSimon Schubert       /* We should be doing hairy argument matching, as below.  */
17825796c8dcSSimon Schubert 	  && (TYPE_CODE (check_typedef (TYPE_TARGET_TYPE (tt1))) == TYPE_CODE (tt2)))
17835796c8dcSSimon Schubert 	{
17845796c8dcSSimon Schubert 	  if (TYPE_CODE (tt2) == TYPE_CODE_ARRAY)
17855796c8dcSSimon Schubert 	    t2[i] = value_coerce_array (t2[i]);
17865796c8dcSSimon Schubert 	  else
17875796c8dcSSimon Schubert 	    t2[i] = value_ref (t2[i]);
17885796c8dcSSimon Schubert 	  continue;
17895796c8dcSSimon Schubert 	}
17905796c8dcSSimon Schubert 
17915796c8dcSSimon Schubert       /* djb - 20000715 - Until the new type structure is in the
17925796c8dcSSimon Schubert 	 place, and we can attempt things like implicit conversions,
17935796c8dcSSimon Schubert 	 we need to do this so you can take something like a map<const
17945796c8dcSSimon Schubert 	 char *>, and properly access map["hello"], because the
17955796c8dcSSimon Schubert 	 argument to [] will be a reference to a pointer to a char,
17965796c8dcSSimon Schubert 	 and the argument will be a pointer to a char.  */
17975796c8dcSSimon Schubert       while (TYPE_CODE(tt1) == TYPE_CODE_REF
17985796c8dcSSimon Schubert 	     || TYPE_CODE (tt1) == TYPE_CODE_PTR)
17995796c8dcSSimon Schubert 	{
18005796c8dcSSimon Schubert 	  tt1 = check_typedef( TYPE_TARGET_TYPE(tt1) );
18015796c8dcSSimon Schubert 	}
18025796c8dcSSimon Schubert       while (TYPE_CODE(tt2) == TYPE_CODE_ARRAY
18035796c8dcSSimon Schubert 	     || TYPE_CODE(tt2) == TYPE_CODE_PTR
18045796c8dcSSimon Schubert 	     || TYPE_CODE(tt2) == TYPE_CODE_REF)
18055796c8dcSSimon Schubert 	{
18065796c8dcSSimon Schubert 	  tt2 = check_typedef (TYPE_TARGET_TYPE(tt2));
18075796c8dcSSimon Schubert 	}
18085796c8dcSSimon Schubert       if (TYPE_CODE (tt1) == TYPE_CODE (tt2))
18095796c8dcSSimon Schubert 	continue;
18105796c8dcSSimon Schubert       /* Array to pointer is a `trivial conversion' according to the
18115796c8dcSSimon Schubert 	 ARM.  */
18125796c8dcSSimon Schubert 
18135796c8dcSSimon Schubert       /* We should be doing much hairier argument matching (see
18145796c8dcSSimon Schubert          section 13.2 of the ARM), but as a quick kludge, just check
18155796c8dcSSimon Schubert          for the same type code.  */
18165796c8dcSSimon Schubert       if (TYPE_CODE (t1[i].type) != TYPE_CODE (value_type (t2[i])))
18175796c8dcSSimon Schubert 	return i + 1;
18185796c8dcSSimon Schubert     }
18195796c8dcSSimon Schubert   if (varargs || t2[i] == NULL)
18205796c8dcSSimon Schubert     return 0;
18215796c8dcSSimon Schubert   return i + 1;
18225796c8dcSSimon Schubert }
18235796c8dcSSimon Schubert 
18245796c8dcSSimon Schubert /* Helper function used by value_struct_elt to recurse through
18255796c8dcSSimon Schubert    baseclasses.  Look for a field NAME in ARG1. Adjust the address of
18265796c8dcSSimon Schubert    ARG1 by OFFSET bytes, and search in it assuming it has (class) type
18275796c8dcSSimon Schubert    TYPE.  If found, return value, else return NULL.
18285796c8dcSSimon Schubert 
18295796c8dcSSimon Schubert    If LOOKING_FOR_BASECLASS, then instead of looking for struct
18305796c8dcSSimon Schubert    fields, look for a baseclass named NAME.  */
18315796c8dcSSimon Schubert 
18325796c8dcSSimon Schubert static struct value *
1833*cf7f2e2dSJohn Marino search_struct_field (const char *name, struct value *arg1, int offset,
18345796c8dcSSimon Schubert 		     struct type *type, int looking_for_baseclass)
18355796c8dcSSimon Schubert {
18365796c8dcSSimon Schubert   int i;
1837*cf7f2e2dSJohn Marino   int nbases;
18385796c8dcSSimon Schubert 
18395796c8dcSSimon Schubert   CHECK_TYPEDEF (type);
1840*cf7f2e2dSJohn Marino   nbases = TYPE_N_BASECLASSES (type);
18415796c8dcSSimon Schubert 
18425796c8dcSSimon Schubert   if (!looking_for_baseclass)
18435796c8dcSSimon Schubert     for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
18445796c8dcSSimon Schubert       {
18455796c8dcSSimon Schubert 	char *t_field_name = TYPE_FIELD_NAME (type, i);
18465796c8dcSSimon Schubert 
18475796c8dcSSimon Schubert 	if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
18485796c8dcSSimon Schubert 	  {
18495796c8dcSSimon Schubert 	    struct value *v;
1850*cf7f2e2dSJohn Marino 
18515796c8dcSSimon Schubert 	    if (field_is_static (&TYPE_FIELD (type, i)))
18525796c8dcSSimon Schubert 	      {
18535796c8dcSSimon Schubert 		v = value_static_field (type, i);
18545796c8dcSSimon Schubert 		if (v == 0)
1855*cf7f2e2dSJohn Marino 		  error (_("field %s is nonexistent or has been optimized out"),
18565796c8dcSSimon Schubert 			 name);
18575796c8dcSSimon Schubert 	      }
18585796c8dcSSimon Schubert 	    else
18595796c8dcSSimon Schubert 	      {
18605796c8dcSSimon Schubert 		v = value_primitive_field (arg1, offset, i, type);
18615796c8dcSSimon Schubert 		if (v == 0)
18625796c8dcSSimon Schubert 		  error (_("there is no field named %s"), name);
18635796c8dcSSimon Schubert 	      }
18645796c8dcSSimon Schubert 	    return v;
18655796c8dcSSimon Schubert 	  }
18665796c8dcSSimon Schubert 
18675796c8dcSSimon Schubert 	if (t_field_name
18685796c8dcSSimon Schubert 	    && (t_field_name[0] == '\0'
18695796c8dcSSimon Schubert 		|| (TYPE_CODE (type) == TYPE_CODE_UNION
18705796c8dcSSimon Schubert 		    && (strcmp_iw (t_field_name, "else") == 0))))
18715796c8dcSSimon Schubert 	  {
18725796c8dcSSimon Schubert 	    struct type *field_type = TYPE_FIELD_TYPE (type, i);
1873*cf7f2e2dSJohn Marino 
18745796c8dcSSimon Schubert 	    if (TYPE_CODE (field_type) == TYPE_CODE_UNION
18755796c8dcSSimon Schubert 		|| TYPE_CODE (field_type) == TYPE_CODE_STRUCT)
18765796c8dcSSimon Schubert 	      {
18775796c8dcSSimon Schubert 		/* Look for a match through the fields of an anonymous
18785796c8dcSSimon Schubert 		   union, or anonymous struct.  C++ provides anonymous
18795796c8dcSSimon Schubert 		   unions.
18805796c8dcSSimon Schubert 
18815796c8dcSSimon Schubert 		   In the GNU Chill (now deleted from GDB)
18825796c8dcSSimon Schubert 		   implementation of variant record types, each
18835796c8dcSSimon Schubert 		   <alternative field> has an (anonymous) union type,
18845796c8dcSSimon Schubert 		   each member of the union represents a <variant
18855796c8dcSSimon Schubert 		   alternative>.  Each <variant alternative> is
18865796c8dcSSimon Schubert 		   represented as a struct, with a member for each
18875796c8dcSSimon Schubert 		   <variant field>.  */
18885796c8dcSSimon Schubert 
18895796c8dcSSimon Schubert 		struct value *v;
18905796c8dcSSimon Schubert 		int new_offset = offset;
18915796c8dcSSimon Schubert 
18925796c8dcSSimon Schubert 		/* This is pretty gross.  In G++, the offset in an
18935796c8dcSSimon Schubert 		   anonymous union is relative to the beginning of the
18945796c8dcSSimon Schubert 		   enclosing struct.  In the GNU Chill (now deleted
18955796c8dcSSimon Schubert 		   from GDB) implementation of variant records, the
18965796c8dcSSimon Schubert 		   bitpos is zero in an anonymous union field, so we
18975796c8dcSSimon Schubert 		   have to add the offset of the union here.  */
18985796c8dcSSimon Schubert 		if (TYPE_CODE (field_type) == TYPE_CODE_STRUCT
18995796c8dcSSimon Schubert 		    || (TYPE_NFIELDS (field_type) > 0
19005796c8dcSSimon Schubert 			&& TYPE_FIELD_BITPOS (field_type, 0) == 0))
19015796c8dcSSimon Schubert 		  new_offset += TYPE_FIELD_BITPOS (type, i) / 8;
19025796c8dcSSimon Schubert 
19035796c8dcSSimon Schubert 		v = search_struct_field (name, arg1, new_offset,
19045796c8dcSSimon Schubert 					 field_type,
19055796c8dcSSimon Schubert 					 looking_for_baseclass);
19065796c8dcSSimon Schubert 		if (v)
19075796c8dcSSimon Schubert 		  return v;
19085796c8dcSSimon Schubert 	      }
19095796c8dcSSimon Schubert 	  }
19105796c8dcSSimon Schubert       }
19115796c8dcSSimon Schubert 
19125796c8dcSSimon Schubert   for (i = 0; i < nbases; i++)
19135796c8dcSSimon Schubert     {
19145796c8dcSSimon Schubert       struct value *v;
19155796c8dcSSimon Schubert       struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
19165796c8dcSSimon Schubert       /* If we are looking for baseclasses, this is what we get when
19175796c8dcSSimon Schubert          we hit them.  But it could happen that the base part's member
19185796c8dcSSimon Schubert          name is not yet filled in.  */
19195796c8dcSSimon Schubert       int found_baseclass = (looking_for_baseclass
19205796c8dcSSimon Schubert 			     && TYPE_BASECLASS_NAME (type, i) != NULL
19215796c8dcSSimon Schubert 			     && (strcmp_iw (name,
19225796c8dcSSimon Schubert 					    TYPE_BASECLASS_NAME (type,
19235796c8dcSSimon Schubert 								 i)) == 0));
19245796c8dcSSimon Schubert 
19255796c8dcSSimon Schubert       if (BASETYPE_VIA_VIRTUAL (type, i))
19265796c8dcSSimon Schubert 	{
19275796c8dcSSimon Schubert 	  int boffset;
19285796c8dcSSimon Schubert 	  struct value *v2;
19295796c8dcSSimon Schubert 
19305796c8dcSSimon Schubert 	  boffset = baseclass_offset (type, i,
19315796c8dcSSimon Schubert 				      value_contents (arg1) + offset,
1932*cf7f2e2dSJohn Marino 				      value_address (arg1)
1933*cf7f2e2dSJohn Marino 				      + value_embedded_offset (arg1)
1934*cf7f2e2dSJohn Marino 				      + offset);
19355796c8dcSSimon Schubert 	  if (boffset == -1)
19365796c8dcSSimon Schubert 	    error (_("virtual baseclass botch"));
19375796c8dcSSimon Schubert 
19385796c8dcSSimon Schubert 	  /* The virtual base class pointer might have been clobbered
19395796c8dcSSimon Schubert 	     by the user program. Make sure that it still points to a
19405796c8dcSSimon Schubert 	     valid memory location.  */
19415796c8dcSSimon Schubert 
1942*cf7f2e2dSJohn Marino 	  boffset += value_embedded_offset (arg1) + offset;
1943*cf7f2e2dSJohn Marino 	  if (boffset < 0
1944*cf7f2e2dSJohn Marino 	      || boffset >= TYPE_LENGTH (value_enclosing_type (arg1)))
19455796c8dcSSimon Schubert 	    {
19465796c8dcSSimon Schubert 	      CORE_ADDR base_addr;
19475796c8dcSSimon Schubert 
19485796c8dcSSimon Schubert 	      v2  = allocate_value (basetype);
19495796c8dcSSimon Schubert 	      base_addr = value_address (arg1) + boffset;
19505796c8dcSSimon Schubert 	      if (target_read_memory (base_addr,
19515796c8dcSSimon Schubert 				      value_contents_raw (v2),
19525796c8dcSSimon Schubert 				      TYPE_LENGTH (basetype)) != 0)
19535796c8dcSSimon Schubert 		error (_("virtual baseclass botch"));
19545796c8dcSSimon Schubert 	      VALUE_LVAL (v2) = lval_memory;
19555796c8dcSSimon Schubert 	      set_value_address (v2, base_addr);
19565796c8dcSSimon Schubert 	    }
19575796c8dcSSimon Schubert 	  else
19585796c8dcSSimon Schubert 	    {
1959*cf7f2e2dSJohn Marino 	      v2 = value_copy (arg1);
1960*cf7f2e2dSJohn Marino 	      deprecated_set_value_type (v2, basetype);
1961*cf7f2e2dSJohn Marino 	      set_value_embedded_offset (v2, boffset);
19625796c8dcSSimon Schubert 	    }
19635796c8dcSSimon Schubert 
19645796c8dcSSimon Schubert 	  if (found_baseclass)
19655796c8dcSSimon Schubert 	    return v2;
19665796c8dcSSimon Schubert 	  v = search_struct_field (name, v2, 0,
19675796c8dcSSimon Schubert 				   TYPE_BASECLASS (type, i),
19685796c8dcSSimon Schubert 				   looking_for_baseclass);
19695796c8dcSSimon Schubert 	}
19705796c8dcSSimon Schubert       else if (found_baseclass)
19715796c8dcSSimon Schubert 	v = value_primitive_field (arg1, offset, i, type);
19725796c8dcSSimon Schubert       else
19735796c8dcSSimon Schubert 	v = search_struct_field (name, arg1,
19745796c8dcSSimon Schubert 				 offset + TYPE_BASECLASS_BITPOS (type,
19755796c8dcSSimon Schubert 								 i) / 8,
19765796c8dcSSimon Schubert 				 basetype, looking_for_baseclass);
19775796c8dcSSimon Schubert       if (v)
19785796c8dcSSimon Schubert 	return v;
19795796c8dcSSimon Schubert     }
19805796c8dcSSimon Schubert   return NULL;
19815796c8dcSSimon Schubert }
19825796c8dcSSimon Schubert 
19835796c8dcSSimon Schubert /* Helper function used by value_struct_elt to recurse through
19845796c8dcSSimon Schubert    baseclasses.  Look for a field NAME in ARG1. Adjust the address of
19855796c8dcSSimon Schubert    ARG1 by OFFSET bytes, and search in it assuming it has (class) type
19865796c8dcSSimon Schubert    TYPE.
19875796c8dcSSimon Schubert 
19885796c8dcSSimon Schubert    If found, return value, else if name matched and args not return
19895796c8dcSSimon Schubert    (value) -1, else return NULL.  */
19905796c8dcSSimon Schubert 
19915796c8dcSSimon Schubert static struct value *
1992*cf7f2e2dSJohn Marino search_struct_method (const char *name, struct value **arg1p,
19935796c8dcSSimon Schubert 		      struct value **args, int offset,
19945796c8dcSSimon Schubert 		      int *static_memfuncp, struct type *type)
19955796c8dcSSimon Schubert {
19965796c8dcSSimon Schubert   int i;
19975796c8dcSSimon Schubert   struct value *v;
19985796c8dcSSimon Schubert   int name_matched = 0;
19995796c8dcSSimon Schubert   char dem_opname[64];
20005796c8dcSSimon Schubert 
20015796c8dcSSimon Schubert   CHECK_TYPEDEF (type);
20025796c8dcSSimon Schubert   for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
20035796c8dcSSimon Schubert     {
20045796c8dcSSimon Schubert       char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
2005*cf7f2e2dSJohn Marino 
20065796c8dcSSimon Schubert       /* FIXME!  May need to check for ARM demangling here */
20075796c8dcSSimon Schubert       if (strncmp (t_field_name, "__", 2) == 0 ||
20085796c8dcSSimon Schubert 	  strncmp (t_field_name, "op", 2) == 0 ||
20095796c8dcSSimon Schubert 	  strncmp (t_field_name, "type", 4) == 0)
20105796c8dcSSimon Schubert 	{
20115796c8dcSSimon Schubert 	  if (cplus_demangle_opname (t_field_name, dem_opname, DMGL_ANSI))
20125796c8dcSSimon Schubert 	    t_field_name = dem_opname;
20135796c8dcSSimon Schubert 	  else if (cplus_demangle_opname (t_field_name, dem_opname, 0))
20145796c8dcSSimon Schubert 	    t_field_name = dem_opname;
20155796c8dcSSimon Schubert 	}
20165796c8dcSSimon Schubert       if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
20175796c8dcSSimon Schubert 	{
20185796c8dcSSimon Schubert 	  int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1;
20195796c8dcSSimon Schubert 	  struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
20205796c8dcSSimon Schubert 
2021*cf7f2e2dSJohn Marino 	  name_matched = 1;
20225796c8dcSSimon Schubert 	  check_stub_method_group (type, i);
20235796c8dcSSimon Schubert 	  if (j > 0 && args == 0)
20245796c8dcSSimon Schubert 	    error (_("cannot resolve overloaded method `%s': no arguments supplied"), name);
20255796c8dcSSimon Schubert 	  else if (j == 0 && args == 0)
20265796c8dcSSimon Schubert 	    {
20275796c8dcSSimon Schubert 	      v = value_fn_field (arg1p, f, j, type, offset);
20285796c8dcSSimon Schubert 	      if (v != NULL)
20295796c8dcSSimon Schubert 		return v;
20305796c8dcSSimon Schubert 	    }
20315796c8dcSSimon Schubert 	  else
20325796c8dcSSimon Schubert 	    while (j >= 0)
20335796c8dcSSimon Schubert 	      {
20345796c8dcSSimon Schubert 		if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j),
20355796c8dcSSimon Schubert 			      TYPE_VARARGS (TYPE_FN_FIELD_TYPE (f, j)),
20365796c8dcSSimon Schubert 			      TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f, j)),
20375796c8dcSSimon Schubert 			      TYPE_FN_FIELD_ARGS (f, j), args))
20385796c8dcSSimon Schubert 		  {
20395796c8dcSSimon Schubert 		    if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
20405796c8dcSSimon Schubert 		      return value_virtual_fn_field (arg1p, f, j,
20415796c8dcSSimon Schubert 						     type, offset);
20425796c8dcSSimon Schubert 		    if (TYPE_FN_FIELD_STATIC_P (f, j)
20435796c8dcSSimon Schubert 			&& static_memfuncp)
20445796c8dcSSimon Schubert 		      *static_memfuncp = 1;
20455796c8dcSSimon Schubert 		    v = value_fn_field (arg1p, f, j, type, offset);
20465796c8dcSSimon Schubert 		    if (v != NULL)
20475796c8dcSSimon Schubert 		      return v;
20485796c8dcSSimon Schubert 		  }
20495796c8dcSSimon Schubert 		j--;
20505796c8dcSSimon Schubert 	      }
20515796c8dcSSimon Schubert 	}
20525796c8dcSSimon Schubert     }
20535796c8dcSSimon Schubert 
20545796c8dcSSimon Schubert   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
20555796c8dcSSimon Schubert     {
20565796c8dcSSimon Schubert       int base_offset;
20575796c8dcSSimon Schubert 
20585796c8dcSSimon Schubert       if (BASETYPE_VIA_VIRTUAL (type, i))
20595796c8dcSSimon Schubert 	{
20605796c8dcSSimon Schubert 	  struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
20615796c8dcSSimon Schubert 	  const gdb_byte *base_valaddr;
20625796c8dcSSimon Schubert 
20635796c8dcSSimon Schubert 	  /* The virtual base class pointer might have been
20645796c8dcSSimon Schubert 	     clobbered by the user program. Make sure that it
20655796c8dcSSimon Schubert 	    still points to a valid memory location.  */
20665796c8dcSSimon Schubert 
20675796c8dcSSimon Schubert 	  if (offset < 0 || offset >= TYPE_LENGTH (type))
20685796c8dcSSimon Schubert 	    {
20695796c8dcSSimon Schubert 	      gdb_byte *tmp = alloca (TYPE_LENGTH (baseclass));
2070*cf7f2e2dSJohn Marino 
20715796c8dcSSimon Schubert 	      if (target_read_memory (value_address (*arg1p) + offset,
20725796c8dcSSimon Schubert 				      tmp, TYPE_LENGTH (baseclass)) != 0)
20735796c8dcSSimon Schubert 		error (_("virtual baseclass botch"));
20745796c8dcSSimon Schubert 	      base_valaddr = tmp;
20755796c8dcSSimon Schubert 	    }
20765796c8dcSSimon Schubert 	  else
20775796c8dcSSimon Schubert 	    base_valaddr = value_contents (*arg1p) + offset;
20785796c8dcSSimon Schubert 
20795796c8dcSSimon Schubert 	  base_offset = baseclass_offset (type, i, base_valaddr,
20805796c8dcSSimon Schubert 					  value_address (*arg1p) + offset);
20815796c8dcSSimon Schubert 	  if (base_offset == -1)
20825796c8dcSSimon Schubert 	    error (_("virtual baseclass botch"));
20835796c8dcSSimon Schubert 	}
20845796c8dcSSimon Schubert       else
20855796c8dcSSimon Schubert 	{
20865796c8dcSSimon Schubert 	  base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
20875796c8dcSSimon Schubert 	}
20885796c8dcSSimon Schubert       v = search_struct_method (name, arg1p, args, base_offset + offset,
20895796c8dcSSimon Schubert 				static_memfuncp, TYPE_BASECLASS (type, i));
20905796c8dcSSimon Schubert       if (v == (struct value *) - 1)
20915796c8dcSSimon Schubert 	{
20925796c8dcSSimon Schubert 	  name_matched = 1;
20935796c8dcSSimon Schubert 	}
20945796c8dcSSimon Schubert       else if (v)
20955796c8dcSSimon Schubert 	{
20965796c8dcSSimon Schubert 	  /* FIXME-bothner:  Why is this commented out?  Why is it here?  */
20975796c8dcSSimon Schubert 	  /* *arg1p = arg1_tmp; */
20985796c8dcSSimon Schubert 	  return v;
20995796c8dcSSimon Schubert 	}
21005796c8dcSSimon Schubert     }
21015796c8dcSSimon Schubert   if (name_matched)
21025796c8dcSSimon Schubert     return (struct value *) - 1;
21035796c8dcSSimon Schubert   else
21045796c8dcSSimon Schubert     return NULL;
21055796c8dcSSimon Schubert }
21065796c8dcSSimon Schubert 
21075796c8dcSSimon Schubert /* Given *ARGP, a value of type (pointer to a)* structure/union,
21085796c8dcSSimon Schubert    extract the component named NAME from the ultimate target
21095796c8dcSSimon Schubert    structure/union and return it as a value with its appropriate type.
21105796c8dcSSimon Schubert    ERR is used in the error message if *ARGP's type is wrong.
21115796c8dcSSimon Schubert 
21125796c8dcSSimon Schubert    C++: ARGS is a list of argument types to aid in the selection of
21135796c8dcSSimon Schubert    an appropriate method. Also, handle derived types.
21145796c8dcSSimon Schubert 
21155796c8dcSSimon Schubert    STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
21165796c8dcSSimon Schubert    where the truthvalue of whether the function that was resolved was
21175796c8dcSSimon Schubert    a static member function or not is stored.
21185796c8dcSSimon Schubert 
21195796c8dcSSimon Schubert    ERR is an error message to be printed in case the field is not
21205796c8dcSSimon Schubert    found.  */
21215796c8dcSSimon Schubert 
21225796c8dcSSimon Schubert struct value *
21235796c8dcSSimon Schubert value_struct_elt (struct value **argp, struct value **args,
2124*cf7f2e2dSJohn Marino 		  const char *name, int *static_memfuncp, const char *err)
21255796c8dcSSimon Schubert {
21265796c8dcSSimon Schubert   struct type *t;
21275796c8dcSSimon Schubert   struct value *v;
21285796c8dcSSimon Schubert 
21295796c8dcSSimon Schubert   *argp = coerce_array (*argp);
21305796c8dcSSimon Schubert 
21315796c8dcSSimon Schubert   t = check_typedef (value_type (*argp));
21325796c8dcSSimon Schubert 
21335796c8dcSSimon Schubert   /* Follow pointers until we get to a non-pointer.  */
21345796c8dcSSimon Schubert 
21355796c8dcSSimon Schubert   while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
21365796c8dcSSimon Schubert     {
21375796c8dcSSimon Schubert       *argp = value_ind (*argp);
21385796c8dcSSimon Schubert       /* Don't coerce fn pointer to fn and then back again!  */
21395796c8dcSSimon Schubert       if (TYPE_CODE (value_type (*argp)) != TYPE_CODE_FUNC)
21405796c8dcSSimon Schubert 	*argp = coerce_array (*argp);
21415796c8dcSSimon Schubert       t = check_typedef (value_type (*argp));
21425796c8dcSSimon Schubert     }
21435796c8dcSSimon Schubert 
21445796c8dcSSimon Schubert   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
21455796c8dcSSimon Schubert       && TYPE_CODE (t) != TYPE_CODE_UNION)
21465796c8dcSSimon Schubert     error (_("Attempt to extract a component of a value that is not a %s."), err);
21475796c8dcSSimon Schubert 
21485796c8dcSSimon Schubert   /* Assume it's not, unless we see that it is.  */
21495796c8dcSSimon Schubert   if (static_memfuncp)
21505796c8dcSSimon Schubert     *static_memfuncp = 0;
21515796c8dcSSimon Schubert 
21525796c8dcSSimon Schubert   if (!args)
21535796c8dcSSimon Schubert     {
21545796c8dcSSimon Schubert       /* if there are no arguments ...do this...  */
21555796c8dcSSimon Schubert 
21565796c8dcSSimon Schubert       /* Try as a field first, because if we succeed, there is less
21575796c8dcSSimon Schubert          work to be done.  */
21585796c8dcSSimon Schubert       v = search_struct_field (name, *argp, 0, t, 0);
21595796c8dcSSimon Schubert       if (v)
21605796c8dcSSimon Schubert 	return v;
21615796c8dcSSimon Schubert 
21625796c8dcSSimon Schubert       /* C++: If it was not found as a data field, then try to
21635796c8dcSSimon Schubert          return it as a pointer to a method.  */
21645796c8dcSSimon Schubert       v = search_struct_method (name, argp, args, 0,
21655796c8dcSSimon Schubert 				static_memfuncp, t);
21665796c8dcSSimon Schubert 
21675796c8dcSSimon Schubert       if (v == (struct value *) - 1)
21685796c8dcSSimon Schubert 	error (_("Cannot take address of method %s."), name);
21695796c8dcSSimon Schubert       else if (v == 0)
21705796c8dcSSimon Schubert 	{
21715796c8dcSSimon Schubert 	  if (TYPE_NFN_FIELDS (t))
21725796c8dcSSimon Schubert 	    error (_("There is no member or method named %s."), name);
21735796c8dcSSimon Schubert 	  else
21745796c8dcSSimon Schubert 	    error (_("There is no member named %s."), name);
21755796c8dcSSimon Schubert 	}
21765796c8dcSSimon Schubert       return v;
21775796c8dcSSimon Schubert     }
21785796c8dcSSimon Schubert 
21795796c8dcSSimon Schubert     v = search_struct_method (name, argp, args, 0,
21805796c8dcSSimon Schubert 			      static_memfuncp, t);
21815796c8dcSSimon Schubert 
21825796c8dcSSimon Schubert   if (v == (struct value *) - 1)
21835796c8dcSSimon Schubert     {
21845796c8dcSSimon Schubert       error (_("One of the arguments you tried to pass to %s could not be converted to what the function wants."), name);
21855796c8dcSSimon Schubert     }
21865796c8dcSSimon Schubert   else if (v == 0)
21875796c8dcSSimon Schubert     {
21885796c8dcSSimon Schubert       /* See if user tried to invoke data as function.  If so, hand it
21895796c8dcSSimon Schubert          back.  If it's not callable (i.e., a pointer to function),
21905796c8dcSSimon Schubert          gdb should give an error.  */
21915796c8dcSSimon Schubert       v = search_struct_field (name, *argp, 0, t, 0);
21925796c8dcSSimon Schubert       /* If we found an ordinary field, then it is not a method call.
21935796c8dcSSimon Schubert 	 So, treat it as if it were a static member function.  */
21945796c8dcSSimon Schubert       if (v && static_memfuncp)
21955796c8dcSSimon Schubert 	*static_memfuncp = 1;
21965796c8dcSSimon Schubert     }
21975796c8dcSSimon Schubert 
21985796c8dcSSimon Schubert   if (!v)
21995796c8dcSSimon Schubert     error (_("Structure has no component named %s."), name);
22005796c8dcSSimon Schubert   return v;
22015796c8dcSSimon Schubert }
22025796c8dcSSimon Schubert 
22035796c8dcSSimon Schubert /* Search through the methods of an object (and its bases) to find a
22045796c8dcSSimon Schubert    specified method.  Return the pointer to the fn_field list of
22055796c8dcSSimon Schubert    overloaded instances.
22065796c8dcSSimon Schubert 
22075796c8dcSSimon Schubert    Helper function for value_find_oload_list.
22085796c8dcSSimon Schubert    ARGP is a pointer to a pointer to a value (the object).
22095796c8dcSSimon Schubert    METHOD is a string containing the method name.
22105796c8dcSSimon Schubert    OFFSET is the offset within the value.
22115796c8dcSSimon Schubert    TYPE is the assumed type of the object.
22125796c8dcSSimon Schubert    NUM_FNS is the number of overloaded instances.
22135796c8dcSSimon Schubert    BASETYPE is set to the actual type of the subobject where the
22145796c8dcSSimon Schubert       method is found.
22155796c8dcSSimon Schubert    BOFFSET is the offset of the base subobject where the method is found.
22165796c8dcSSimon Schubert */
22175796c8dcSSimon Schubert 
22185796c8dcSSimon Schubert static struct fn_field *
2219*cf7f2e2dSJohn Marino find_method_list (struct value **argp, const char *method,
22205796c8dcSSimon Schubert 		  int offset, struct type *type, int *num_fns,
22215796c8dcSSimon Schubert 		  struct type **basetype, int *boffset)
22225796c8dcSSimon Schubert {
22235796c8dcSSimon Schubert   int i;
22245796c8dcSSimon Schubert   struct fn_field *f;
22255796c8dcSSimon Schubert   CHECK_TYPEDEF (type);
22265796c8dcSSimon Schubert 
22275796c8dcSSimon Schubert   *num_fns = 0;
22285796c8dcSSimon Schubert 
22295796c8dcSSimon Schubert   /* First check in object itself.  */
22305796c8dcSSimon Schubert   for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
22315796c8dcSSimon Schubert     {
22325796c8dcSSimon Schubert       /* pai: FIXME What about operators and type conversions?  */
22335796c8dcSSimon Schubert       char *fn_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
2234*cf7f2e2dSJohn Marino 
22355796c8dcSSimon Schubert       if (fn_field_name && (strcmp_iw (fn_field_name, method) == 0))
22365796c8dcSSimon Schubert 	{
22375796c8dcSSimon Schubert 	  int len = TYPE_FN_FIELDLIST_LENGTH (type, i);
22385796c8dcSSimon Schubert 	  struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
22395796c8dcSSimon Schubert 
22405796c8dcSSimon Schubert 	  *num_fns = len;
22415796c8dcSSimon Schubert 	  *basetype = type;
22425796c8dcSSimon Schubert 	  *boffset = offset;
22435796c8dcSSimon Schubert 
22445796c8dcSSimon Schubert 	  /* Resolve any stub methods.  */
22455796c8dcSSimon Schubert 	  check_stub_method_group (type, i);
22465796c8dcSSimon Schubert 
22475796c8dcSSimon Schubert 	  return f;
22485796c8dcSSimon Schubert 	}
22495796c8dcSSimon Schubert     }
22505796c8dcSSimon Schubert 
22515796c8dcSSimon Schubert   /* Not found in object, check in base subobjects.  */
22525796c8dcSSimon Schubert   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
22535796c8dcSSimon Schubert     {
22545796c8dcSSimon Schubert       int base_offset;
2255*cf7f2e2dSJohn Marino 
22565796c8dcSSimon Schubert       if (BASETYPE_VIA_VIRTUAL (type, i))
22575796c8dcSSimon Schubert 	{
22585796c8dcSSimon Schubert 	  base_offset = value_offset (*argp) + offset;
22595796c8dcSSimon Schubert 	  base_offset = baseclass_offset (type, i,
22605796c8dcSSimon Schubert 					  value_contents (*argp) + base_offset,
22615796c8dcSSimon Schubert 					  value_address (*argp) + base_offset);
22625796c8dcSSimon Schubert 	  if (base_offset == -1)
22635796c8dcSSimon Schubert 	    error (_("virtual baseclass botch"));
22645796c8dcSSimon Schubert 	}
22655796c8dcSSimon Schubert       else /* Non-virtual base, simply use bit position from debug
22665796c8dcSSimon Schubert 	      info.  */
22675796c8dcSSimon Schubert 	{
22685796c8dcSSimon Schubert 	  base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
22695796c8dcSSimon Schubert 	}
22705796c8dcSSimon Schubert       f = find_method_list (argp, method, base_offset + offset,
22715796c8dcSSimon Schubert 			    TYPE_BASECLASS (type, i), num_fns,
22725796c8dcSSimon Schubert 			    basetype, boffset);
22735796c8dcSSimon Schubert       if (f)
22745796c8dcSSimon Schubert 	return f;
22755796c8dcSSimon Schubert     }
22765796c8dcSSimon Schubert   return NULL;
22775796c8dcSSimon Schubert }
22785796c8dcSSimon Schubert 
22795796c8dcSSimon Schubert /* Return the list of overloaded methods of a specified name.
22805796c8dcSSimon Schubert 
22815796c8dcSSimon Schubert    ARGP is a pointer to a pointer to a value (the object).
22825796c8dcSSimon Schubert    METHOD is the method name.
22835796c8dcSSimon Schubert    OFFSET is the offset within the value contents.
22845796c8dcSSimon Schubert    NUM_FNS is the number of overloaded instances.
22855796c8dcSSimon Schubert    BASETYPE is set to the type of the base subobject that defines the
22865796c8dcSSimon Schubert       method.
22875796c8dcSSimon Schubert    BOFFSET is the offset of the base subobject which defines the method.
22885796c8dcSSimon Schubert */
22895796c8dcSSimon Schubert 
22905796c8dcSSimon Schubert struct fn_field *
2291*cf7f2e2dSJohn Marino value_find_oload_method_list (struct value **argp, const char *method,
22925796c8dcSSimon Schubert 			      int offset, int *num_fns,
22935796c8dcSSimon Schubert 			      struct type **basetype, int *boffset)
22945796c8dcSSimon Schubert {
22955796c8dcSSimon Schubert   struct type *t;
22965796c8dcSSimon Schubert 
22975796c8dcSSimon Schubert   t = check_typedef (value_type (*argp));
22985796c8dcSSimon Schubert 
22995796c8dcSSimon Schubert   /* Code snarfed from value_struct_elt.  */
23005796c8dcSSimon Schubert   while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
23015796c8dcSSimon Schubert     {
23025796c8dcSSimon Schubert       *argp = value_ind (*argp);
23035796c8dcSSimon Schubert       /* Don't coerce fn pointer to fn and then back again!  */
23045796c8dcSSimon Schubert       if (TYPE_CODE (value_type (*argp)) != TYPE_CODE_FUNC)
23055796c8dcSSimon Schubert 	*argp = coerce_array (*argp);
23065796c8dcSSimon Schubert       t = check_typedef (value_type (*argp));
23075796c8dcSSimon Schubert     }
23085796c8dcSSimon Schubert 
23095796c8dcSSimon Schubert   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
23105796c8dcSSimon Schubert       && TYPE_CODE (t) != TYPE_CODE_UNION)
23115796c8dcSSimon Schubert     error (_("Attempt to extract a component of a value that is not a struct or union"));
23125796c8dcSSimon Schubert 
23135796c8dcSSimon Schubert   return find_method_list (argp, method, 0, t, num_fns,
23145796c8dcSSimon Schubert 			   basetype, boffset);
23155796c8dcSSimon Schubert }
23165796c8dcSSimon Schubert 
23175796c8dcSSimon Schubert /* Given an array of argument types (ARGTYPES) (which includes an
23185796c8dcSSimon Schubert    entry for "this" in the case of C++ methods), the number of
23195796c8dcSSimon Schubert    arguments NARGS, the NAME of a function whether it's a method or
23205796c8dcSSimon Schubert    not (METHOD), and the degree of laxness (LAX) in conforming to
23215796c8dcSSimon Schubert    overload resolution rules in ANSI C++, find the best function that
23225796c8dcSSimon Schubert    matches on the argument types according to the overload resolution
23235796c8dcSSimon Schubert    rules.
23245796c8dcSSimon Schubert 
2325*cf7f2e2dSJohn Marino    METHOD can be one of three values:
2326*cf7f2e2dSJohn Marino      NON_METHOD for non-member functions.
2327*cf7f2e2dSJohn Marino      METHOD: for member functions.
2328*cf7f2e2dSJohn Marino      BOTH: used for overload resolution of operators where the
2329*cf7f2e2dSJohn Marino        candidates are expected to be either member or non member
2330*cf7f2e2dSJohn Marino        functions. In this case the first argument ARGTYPES
2331*cf7f2e2dSJohn Marino        (representing 'this') is expected to be a reference to the
2332*cf7f2e2dSJohn Marino        target object, and will be dereferenced when attempting the
2333*cf7f2e2dSJohn Marino        non-member search.
2334*cf7f2e2dSJohn Marino 
23355796c8dcSSimon Schubert    In the case of class methods, the parameter OBJ is an object value
23365796c8dcSSimon Schubert    in which to search for overloaded methods.
23375796c8dcSSimon Schubert 
23385796c8dcSSimon Schubert    In the case of non-method functions, the parameter FSYM is a symbol
23395796c8dcSSimon Schubert    corresponding to one of the overloaded functions.
23405796c8dcSSimon Schubert 
23415796c8dcSSimon Schubert    Return value is an integer: 0 -> good match, 10 -> debugger applied
23425796c8dcSSimon Schubert    non-standard coercions, 100 -> incompatible.
23435796c8dcSSimon Schubert 
23445796c8dcSSimon Schubert    If a method is being searched for, VALP will hold the value.
23455796c8dcSSimon Schubert    If a non-method is being searched for, SYMP will hold the symbol
23465796c8dcSSimon Schubert    for it.
23475796c8dcSSimon Schubert 
23485796c8dcSSimon Schubert    If a method is being searched for, and it is a static method,
23495796c8dcSSimon Schubert    then STATICP will point to a non-zero value.
23505796c8dcSSimon Schubert 
2351*cf7f2e2dSJohn Marino    If NO_ADL argument dependent lookup is disabled.  This is used to prevent
2352*cf7f2e2dSJohn Marino    ADL overload candidates when performing overload resolution for a fully
2353*cf7f2e2dSJohn Marino    qualified name.
2354*cf7f2e2dSJohn Marino 
23555796c8dcSSimon Schubert    Note: This function does *not* check the value of
23565796c8dcSSimon Schubert    overload_resolution.  Caller must check it to see whether overload
23575796c8dcSSimon Schubert    resolution is permitted.
23585796c8dcSSimon Schubert */
23595796c8dcSSimon Schubert 
23605796c8dcSSimon Schubert int
23615796c8dcSSimon Schubert find_overload_match (struct type **arg_types, int nargs,
2362*cf7f2e2dSJohn Marino 		     const char *name, enum oload_search_type method,
2363*cf7f2e2dSJohn Marino 		     int lax, struct value **objp, struct symbol *fsym,
23645796c8dcSSimon Schubert 		     struct value **valp, struct symbol **symp,
2365*cf7f2e2dSJohn Marino 		     int *staticp, const int no_adl)
23665796c8dcSSimon Schubert {
23675796c8dcSSimon Schubert   struct value *obj = (objp ? *objp : NULL);
23685796c8dcSSimon Schubert   /* Index of best overloaded function.  */
2369*cf7f2e2dSJohn Marino   int func_oload_champ = -1;
2370*cf7f2e2dSJohn Marino   int method_oload_champ = -1;
2371*cf7f2e2dSJohn Marino 
23725796c8dcSSimon Schubert   /* The measure for the current best match.  */
2373*cf7f2e2dSJohn Marino   struct badness_vector *method_badness = NULL;
2374*cf7f2e2dSJohn Marino   struct badness_vector *func_badness = NULL;
2375*cf7f2e2dSJohn Marino 
23765796c8dcSSimon Schubert   struct value *temp = obj;
23775796c8dcSSimon Schubert   /* For methods, the list of overloaded methods.  */
23785796c8dcSSimon Schubert   struct fn_field *fns_ptr = NULL;
23795796c8dcSSimon Schubert   /* For non-methods, the list of overloaded function symbols.  */
23805796c8dcSSimon Schubert   struct symbol **oload_syms = NULL;
23815796c8dcSSimon Schubert   /* Number of overloaded instances being considered.  */
23825796c8dcSSimon Schubert   int num_fns = 0;
23835796c8dcSSimon Schubert   struct type *basetype = NULL;
23845796c8dcSSimon Schubert   int boffset;
2385*cf7f2e2dSJohn Marino 
2386*cf7f2e2dSJohn Marino   struct cleanup *all_cleanups = make_cleanup (null_cleanup, NULL);
23875796c8dcSSimon Schubert 
23885796c8dcSSimon Schubert   const char *obj_type_name = NULL;
2389*cf7f2e2dSJohn Marino   const char *func_name = NULL;
23905796c8dcSSimon Schubert   enum oload_classification match_quality;
2391*cf7f2e2dSJohn Marino   enum oload_classification method_match_quality = INCOMPATIBLE;
2392*cf7f2e2dSJohn Marino   enum oload_classification func_match_quality = INCOMPATIBLE;
23935796c8dcSSimon Schubert 
23945796c8dcSSimon Schubert   /* Get the list of overloaded methods or functions.  */
2395*cf7f2e2dSJohn Marino   if (method == METHOD || method == BOTH)
23965796c8dcSSimon Schubert     {
23975796c8dcSSimon Schubert       gdb_assert (obj);
23985796c8dcSSimon Schubert 
2399*cf7f2e2dSJohn Marino       /* OBJ may be a pointer value rather than the object itself.  */
2400*cf7f2e2dSJohn Marino       obj = coerce_ref (obj);
2401*cf7f2e2dSJohn Marino       while (TYPE_CODE (check_typedef (value_type (obj))) == TYPE_CODE_PTR)
2402*cf7f2e2dSJohn Marino 	obj = coerce_ref (value_ind (obj));
2403*cf7f2e2dSJohn Marino       obj_type_name = TYPE_NAME (value_type (obj));
2404*cf7f2e2dSJohn Marino 
2405*cf7f2e2dSJohn Marino       /* First check whether this is a data member, e.g. a pointer to
2406*cf7f2e2dSJohn Marino 	 a function.  */
2407*cf7f2e2dSJohn Marino       if (TYPE_CODE (check_typedef (value_type (obj))) == TYPE_CODE_STRUCT)
2408*cf7f2e2dSJohn Marino 	{
2409*cf7f2e2dSJohn Marino 	  *valp = search_struct_field (name, obj, 0,
2410*cf7f2e2dSJohn Marino 				       check_typedef (value_type (obj)), 0);
2411*cf7f2e2dSJohn Marino 	  if (*valp)
2412*cf7f2e2dSJohn Marino 	    {
2413*cf7f2e2dSJohn Marino 	      *staticp = 1;
2414*cf7f2e2dSJohn Marino 	      return 0;
2415*cf7f2e2dSJohn Marino 	    }
2416*cf7f2e2dSJohn Marino 	}
2417*cf7f2e2dSJohn Marino 
2418*cf7f2e2dSJohn Marino       /* Retrieve the list of methods with the name NAME.  */
24195796c8dcSSimon Schubert       fns_ptr = value_find_oload_method_list (&temp, name,
24205796c8dcSSimon Schubert 					      0, &num_fns,
24215796c8dcSSimon Schubert 					      &basetype, &boffset);
2422*cf7f2e2dSJohn Marino       /* If this is a method only search, and no methods were found
2423*cf7f2e2dSJohn Marino          the search has faild.  */
2424*cf7f2e2dSJohn Marino       if (method == METHOD && (!fns_ptr || !num_fns))
24255796c8dcSSimon Schubert 	error (_("Couldn't find method %s%s%s"),
24265796c8dcSSimon Schubert 	       obj_type_name,
24275796c8dcSSimon Schubert 	       (obj_type_name && *obj_type_name) ? "::" : "",
24285796c8dcSSimon Schubert 	       name);
24295796c8dcSSimon Schubert       /* If we are dealing with stub method types, they should have
24305796c8dcSSimon Schubert 	 been resolved by find_method_list via
24315796c8dcSSimon Schubert 	 value_find_oload_method_list above.  */
2432*cf7f2e2dSJohn Marino       if (fns_ptr)
2433*cf7f2e2dSJohn Marino 	{
24345796c8dcSSimon Schubert 	  gdb_assert (TYPE_DOMAIN_TYPE (fns_ptr[0].type) != NULL);
2435*cf7f2e2dSJohn Marino 	  method_oload_champ = find_oload_champ (arg_types, nargs, method,
24365796c8dcSSimon Schubert 	                                         num_fns, fns_ptr,
2437*cf7f2e2dSJohn Marino 	                                         oload_syms, &method_badness);
2438*cf7f2e2dSJohn Marino 
2439*cf7f2e2dSJohn Marino 	  method_match_quality =
2440*cf7f2e2dSJohn Marino 	      classify_oload_match (method_badness, nargs,
2441*cf7f2e2dSJohn Marino 	                            oload_method_static (method, fns_ptr,
2442*cf7f2e2dSJohn Marino 	                                                 method_oload_champ));
2443*cf7f2e2dSJohn Marino 
2444*cf7f2e2dSJohn Marino 	  make_cleanup (xfree, method_badness);
2445*cf7f2e2dSJohn Marino 	}
2446*cf7f2e2dSJohn Marino 
2447*cf7f2e2dSJohn Marino     }
2448*cf7f2e2dSJohn Marino 
2449*cf7f2e2dSJohn Marino   if (method == NON_METHOD || method == BOTH)
2450*cf7f2e2dSJohn Marino     {
2451*cf7f2e2dSJohn Marino       const char *qualified_name = NULL;
2452*cf7f2e2dSJohn Marino 
2453*cf7f2e2dSJohn Marino       /* If the the overload match is being search for both
2454*cf7f2e2dSJohn Marino          as a method and non member function, the first argument
2455*cf7f2e2dSJohn Marino          must now be dereferenced.  */
2456*cf7f2e2dSJohn Marino       if (method == BOTH)
2457*cf7f2e2dSJohn Marino 	arg_types[0] = TYPE_TARGET_TYPE (arg_types[0]);
2458*cf7f2e2dSJohn Marino 
2459*cf7f2e2dSJohn Marino       if (fsym)
2460*cf7f2e2dSJohn Marino         {
2461*cf7f2e2dSJohn Marino           qualified_name = SYMBOL_NATURAL_NAME (fsym);
2462*cf7f2e2dSJohn Marino 
2463*cf7f2e2dSJohn Marino           /* If we have a function with a C++ name, try to extract just
2464*cf7f2e2dSJohn Marino 	     the function part.  Do not try this for non-functions (e.g.
2465*cf7f2e2dSJohn Marino 	     function pointers).  */
2466*cf7f2e2dSJohn Marino           if (qualified_name
2467*cf7f2e2dSJohn Marino               && TYPE_CODE (check_typedef (SYMBOL_TYPE (fsym))) == TYPE_CODE_FUNC)
2468*cf7f2e2dSJohn Marino             {
2469*cf7f2e2dSJohn Marino 	      char *temp;
2470*cf7f2e2dSJohn Marino 
2471*cf7f2e2dSJohn Marino 	      temp = cp_func_name (qualified_name);
2472*cf7f2e2dSJohn Marino 
2473*cf7f2e2dSJohn Marino 	      /* If cp_func_name did not remove anything, the name of the
2474*cf7f2e2dSJohn Marino 	         symbol did not include scope or argument types - it was
2475*cf7f2e2dSJohn Marino 	         probably a C-style function.  */
2476*cf7f2e2dSJohn Marino 	      if (temp)
2477*cf7f2e2dSJohn Marino 		{
2478*cf7f2e2dSJohn Marino 		  make_cleanup (xfree, temp);
2479*cf7f2e2dSJohn Marino 		  if (strcmp (temp, qualified_name) == 0)
2480*cf7f2e2dSJohn Marino 		    func_name = NULL;
2481*cf7f2e2dSJohn Marino 		  else
2482*cf7f2e2dSJohn Marino 		    func_name = temp;
2483*cf7f2e2dSJohn Marino 		}
2484*cf7f2e2dSJohn Marino             }
24855796c8dcSSimon Schubert         }
24865796c8dcSSimon Schubert       else
24875796c8dcSSimon Schubert 	{
2488*cf7f2e2dSJohn Marino 	  func_name = name;
2489*cf7f2e2dSJohn Marino 	  qualified_name = name;
2490*cf7f2e2dSJohn Marino 	}
24915796c8dcSSimon Schubert 
2492*cf7f2e2dSJohn Marino       /* If there was no C++ name, this must be a C-style function or
2493*cf7f2e2dSJohn Marino 	 not a function at all.  Just return the same symbol.  Do the
2494*cf7f2e2dSJohn Marino 	 same if cp_func_name fails for some reason.  */
24955796c8dcSSimon Schubert       if (func_name == NULL)
24965796c8dcSSimon Schubert         {
24975796c8dcSSimon Schubert 	  *symp = fsym;
24985796c8dcSSimon Schubert           return 0;
24995796c8dcSSimon Schubert         }
25005796c8dcSSimon Schubert 
2501*cf7f2e2dSJohn Marino       func_oload_champ = find_oload_champ_namespace (arg_types, nargs,
25025796c8dcSSimon Schubert                                                      func_name,
25035796c8dcSSimon Schubert                                                      qualified_name,
25045796c8dcSSimon Schubert                                                      &oload_syms,
2505*cf7f2e2dSJohn Marino                                                      &func_badness,
2506*cf7f2e2dSJohn Marino                                                      no_adl);
2507*cf7f2e2dSJohn Marino 
2508*cf7f2e2dSJohn Marino       if (func_oload_champ >= 0)
2509*cf7f2e2dSJohn Marino 	func_match_quality = classify_oload_match (func_badness, nargs, 0);
2510*cf7f2e2dSJohn Marino 
2511*cf7f2e2dSJohn Marino       make_cleanup (xfree, oload_syms);
2512*cf7f2e2dSJohn Marino       make_cleanup (xfree, func_badness);
25135796c8dcSSimon Schubert     }
25145796c8dcSSimon Schubert 
2515*cf7f2e2dSJohn Marino   /* Did we find a match ?  */
2516*cf7f2e2dSJohn Marino   if (method_oload_champ == -1 && func_oload_champ == -1)
2517*cf7f2e2dSJohn Marino     error (_("No symbol \"%s\" in current context."), name);
25185796c8dcSSimon Schubert 
2519*cf7f2e2dSJohn Marino   /* If we have found both a method match and a function
2520*cf7f2e2dSJohn Marino      match, find out which one is better, and calculate match
2521*cf7f2e2dSJohn Marino      quality.  */
2522*cf7f2e2dSJohn Marino   if (method_oload_champ >= 0 && func_oload_champ >= 0)
2523*cf7f2e2dSJohn Marino     {
2524*cf7f2e2dSJohn Marino       switch (compare_badness (func_badness, method_badness))
2525*cf7f2e2dSJohn Marino         {
2526*cf7f2e2dSJohn Marino 	  case 0: /* Top two contenders are equally good.  */
2527*cf7f2e2dSJohn Marino 	    /* FIXME: GDB does not support the general ambiguous
2528*cf7f2e2dSJohn Marino 	     case.  All candidates should be collected and presented
2529*cf7f2e2dSJohn Marino 	     the the user.  */
2530*cf7f2e2dSJohn Marino 	    error (_("Ambiguous overload resolution"));
2531*cf7f2e2dSJohn Marino 	    break;
2532*cf7f2e2dSJohn Marino 	  case 1: /* Incomparable top contenders.  */
2533*cf7f2e2dSJohn Marino 	    /* This is an error incompatible candidates
2534*cf7f2e2dSJohn Marino 	       should not have been proposed.  */
2535*cf7f2e2dSJohn Marino 	    error (_("Internal error: incompatible overload candidates proposed"));
2536*cf7f2e2dSJohn Marino 	    break;
2537*cf7f2e2dSJohn Marino 	  case 2: /* Function champion.  */
2538*cf7f2e2dSJohn Marino 	    method_oload_champ = -1;
2539*cf7f2e2dSJohn Marino 	    match_quality = func_match_quality;
2540*cf7f2e2dSJohn Marino 	    break;
2541*cf7f2e2dSJohn Marino 	  case 3: /* Method champion.  */
2542*cf7f2e2dSJohn Marino 	    func_oload_champ = -1;
2543*cf7f2e2dSJohn Marino 	    match_quality = method_match_quality;
2544*cf7f2e2dSJohn Marino 	    break;
2545*cf7f2e2dSJohn Marino 	  default:
2546*cf7f2e2dSJohn Marino 	    error (_("Internal error: unexpected overload comparison result"));
2547*cf7f2e2dSJohn Marino 	    break;
2548*cf7f2e2dSJohn Marino         }
2549*cf7f2e2dSJohn Marino     }
2550*cf7f2e2dSJohn Marino   else
2551*cf7f2e2dSJohn Marino     {
2552*cf7f2e2dSJohn Marino       /* We have either a method match or a function match.  */
2553*cf7f2e2dSJohn Marino       if (method_oload_champ >= 0)
2554*cf7f2e2dSJohn Marino 	match_quality = method_match_quality;
2555*cf7f2e2dSJohn Marino       else
2556*cf7f2e2dSJohn Marino 	match_quality = func_match_quality;
2557*cf7f2e2dSJohn Marino     }
25585796c8dcSSimon Schubert 
25595796c8dcSSimon Schubert   if (match_quality == INCOMPATIBLE)
25605796c8dcSSimon Schubert     {
2561*cf7f2e2dSJohn Marino       if (method == METHOD)
25625796c8dcSSimon Schubert 	error (_("Cannot resolve method %s%s%s to any overloaded instance"),
25635796c8dcSSimon Schubert 	       obj_type_name,
25645796c8dcSSimon Schubert 	       (obj_type_name && *obj_type_name) ? "::" : "",
25655796c8dcSSimon Schubert 	       name);
25665796c8dcSSimon Schubert       else
25675796c8dcSSimon Schubert 	error (_("Cannot resolve function %s to any overloaded instance"),
25685796c8dcSSimon Schubert 	       func_name);
25695796c8dcSSimon Schubert     }
25705796c8dcSSimon Schubert   else if (match_quality == NON_STANDARD)
25715796c8dcSSimon Schubert     {
2572*cf7f2e2dSJohn Marino       if (method == METHOD)
25735796c8dcSSimon Schubert 	warning (_("Using non-standard conversion to match method %s%s%s to supplied arguments"),
25745796c8dcSSimon Schubert 		 obj_type_name,
25755796c8dcSSimon Schubert 		 (obj_type_name && *obj_type_name) ? "::" : "",
25765796c8dcSSimon Schubert 		 name);
25775796c8dcSSimon Schubert       else
25785796c8dcSSimon Schubert 	warning (_("Using non-standard conversion to match function %s to supplied arguments"),
25795796c8dcSSimon Schubert 		 func_name);
25805796c8dcSSimon Schubert     }
25815796c8dcSSimon Schubert 
25825796c8dcSSimon Schubert   if (staticp != NULL)
2583*cf7f2e2dSJohn Marino     *staticp = oload_method_static (method, fns_ptr, method_oload_champ);
2584*cf7f2e2dSJohn Marino 
2585*cf7f2e2dSJohn Marino   if (method_oload_champ >= 0)
25865796c8dcSSimon Schubert     {
2587*cf7f2e2dSJohn Marino       if (TYPE_FN_FIELD_VIRTUAL_P (fns_ptr, method_oload_champ))
2588*cf7f2e2dSJohn Marino 	*valp = value_virtual_fn_field (&temp, fns_ptr, method_oload_champ,
2589*cf7f2e2dSJohn Marino 					basetype, boffset);
2590*cf7f2e2dSJohn Marino       else
2591*cf7f2e2dSJohn Marino 	*valp = value_fn_field (&temp, fns_ptr, method_oload_champ,
2592*cf7f2e2dSJohn Marino 				basetype, boffset);
25935796c8dcSSimon Schubert     }
2594*cf7f2e2dSJohn Marino   else
2595*cf7f2e2dSJohn Marino     *symp = oload_syms[func_oload_champ];
25965796c8dcSSimon Schubert 
25975796c8dcSSimon Schubert   if (objp)
25985796c8dcSSimon Schubert     {
25995796c8dcSSimon Schubert       struct type *temp_type = check_typedef (value_type (temp));
26005796c8dcSSimon Schubert       struct type *obj_type = check_typedef (value_type (*objp));
2601*cf7f2e2dSJohn Marino 
26025796c8dcSSimon Schubert       if (TYPE_CODE (temp_type) != TYPE_CODE_PTR
26035796c8dcSSimon Schubert 	  && (TYPE_CODE (obj_type) == TYPE_CODE_PTR
26045796c8dcSSimon Schubert 	      || TYPE_CODE (obj_type) == TYPE_CODE_REF))
26055796c8dcSSimon Schubert 	{
26065796c8dcSSimon Schubert 	  temp = value_addr (temp);
26075796c8dcSSimon Schubert 	}
26085796c8dcSSimon Schubert       *objp = temp;
26095796c8dcSSimon Schubert     }
2610*cf7f2e2dSJohn Marino 
2611*cf7f2e2dSJohn Marino   do_cleanups (all_cleanups);
26125796c8dcSSimon Schubert 
26135796c8dcSSimon Schubert   switch (match_quality)
26145796c8dcSSimon Schubert     {
26155796c8dcSSimon Schubert     case INCOMPATIBLE:
26165796c8dcSSimon Schubert       return 100;
26175796c8dcSSimon Schubert     case NON_STANDARD:
26185796c8dcSSimon Schubert       return 10;
26195796c8dcSSimon Schubert     default:				/* STANDARD */
26205796c8dcSSimon Schubert       return 0;
26215796c8dcSSimon Schubert     }
26225796c8dcSSimon Schubert }
26235796c8dcSSimon Schubert 
26245796c8dcSSimon Schubert /* Find the best overload match, searching for FUNC_NAME in namespaces
26255796c8dcSSimon Schubert    contained in QUALIFIED_NAME until it either finds a good match or
26265796c8dcSSimon Schubert    runs out of namespaces.  It stores the overloaded functions in
26275796c8dcSSimon Schubert    *OLOAD_SYMS, and the badness vector in *OLOAD_CHAMP_BV.  The
26285796c8dcSSimon Schubert    calling function is responsible for freeing *OLOAD_SYMS and
2629*cf7f2e2dSJohn Marino    *OLOAD_CHAMP_BV.  If NO_ADL, argument dependent lookup is not
2630*cf7f2e2dSJohn Marino    performned.  */
26315796c8dcSSimon Schubert 
26325796c8dcSSimon Schubert static int
26335796c8dcSSimon Schubert find_oload_champ_namespace (struct type **arg_types, int nargs,
26345796c8dcSSimon Schubert 			    const char *func_name,
26355796c8dcSSimon Schubert 			    const char *qualified_name,
26365796c8dcSSimon Schubert 			    struct symbol ***oload_syms,
2637*cf7f2e2dSJohn Marino 			    struct badness_vector **oload_champ_bv,
2638*cf7f2e2dSJohn Marino 			    const int no_adl)
26395796c8dcSSimon Schubert {
26405796c8dcSSimon Schubert   int oload_champ;
26415796c8dcSSimon Schubert 
26425796c8dcSSimon Schubert   find_oload_champ_namespace_loop (arg_types, nargs,
26435796c8dcSSimon Schubert 				   func_name,
26445796c8dcSSimon Schubert 				   qualified_name, 0,
26455796c8dcSSimon Schubert 				   oload_syms, oload_champ_bv,
2646*cf7f2e2dSJohn Marino 				   &oload_champ,
2647*cf7f2e2dSJohn Marino 				   no_adl);
26485796c8dcSSimon Schubert 
26495796c8dcSSimon Schubert   return oload_champ;
26505796c8dcSSimon Schubert }
26515796c8dcSSimon Schubert 
26525796c8dcSSimon Schubert /* Helper function for find_oload_champ_namespace; NAMESPACE_LEN is
26535796c8dcSSimon Schubert    how deep we've looked for namespaces, and the champ is stored in
26545796c8dcSSimon Schubert    OLOAD_CHAMP.  The return value is 1 if the champ is a good one, 0
2655*cf7f2e2dSJohn Marino    if it isn't.  Other arguments are the same as in
2656*cf7f2e2dSJohn Marino    find_oload_champ_namespace
26575796c8dcSSimon Schubert 
26585796c8dcSSimon Schubert    It is the caller's responsibility to free *OLOAD_SYMS and
26595796c8dcSSimon Schubert    *OLOAD_CHAMP_BV.  */
26605796c8dcSSimon Schubert 
26615796c8dcSSimon Schubert static int
26625796c8dcSSimon Schubert find_oload_champ_namespace_loop (struct type **arg_types, int nargs,
26635796c8dcSSimon Schubert 				 const char *func_name,
26645796c8dcSSimon Schubert 				 const char *qualified_name,
26655796c8dcSSimon Schubert 				 int namespace_len,
26665796c8dcSSimon Schubert 				 struct symbol ***oload_syms,
26675796c8dcSSimon Schubert 				 struct badness_vector **oload_champ_bv,
2668*cf7f2e2dSJohn Marino 				 int *oload_champ,
2669*cf7f2e2dSJohn Marino 				 const int no_adl)
26705796c8dcSSimon Schubert {
26715796c8dcSSimon Schubert   int next_namespace_len = namespace_len;
26725796c8dcSSimon Schubert   int searched_deeper = 0;
26735796c8dcSSimon Schubert   int num_fns = 0;
26745796c8dcSSimon Schubert   struct cleanup *old_cleanups;
26755796c8dcSSimon Schubert   int new_oload_champ;
26765796c8dcSSimon Schubert   struct symbol **new_oload_syms;
26775796c8dcSSimon Schubert   struct badness_vector *new_oload_champ_bv;
26785796c8dcSSimon Schubert   char *new_namespace;
26795796c8dcSSimon Schubert 
26805796c8dcSSimon Schubert   if (next_namespace_len != 0)
26815796c8dcSSimon Schubert     {
26825796c8dcSSimon Schubert       gdb_assert (qualified_name[next_namespace_len] == ':');
26835796c8dcSSimon Schubert       next_namespace_len +=  2;
26845796c8dcSSimon Schubert     }
26855796c8dcSSimon Schubert   next_namespace_len +=
26865796c8dcSSimon Schubert     cp_find_first_component (qualified_name + next_namespace_len);
26875796c8dcSSimon Schubert 
26885796c8dcSSimon Schubert   /* Initialize these to values that can safely be xfree'd.  */
26895796c8dcSSimon Schubert   *oload_syms = NULL;
26905796c8dcSSimon Schubert   *oload_champ_bv = NULL;
26915796c8dcSSimon Schubert 
26925796c8dcSSimon Schubert   /* First, see if we have a deeper namespace we can search in.
26935796c8dcSSimon Schubert      If we get a good match there, use it.  */
26945796c8dcSSimon Schubert 
26955796c8dcSSimon Schubert   if (qualified_name[next_namespace_len] == ':')
26965796c8dcSSimon Schubert     {
26975796c8dcSSimon Schubert       searched_deeper = 1;
26985796c8dcSSimon Schubert 
26995796c8dcSSimon Schubert       if (find_oload_champ_namespace_loop (arg_types, nargs,
27005796c8dcSSimon Schubert 					   func_name, qualified_name,
27015796c8dcSSimon Schubert 					   next_namespace_len,
27025796c8dcSSimon Schubert 					   oload_syms, oload_champ_bv,
2703*cf7f2e2dSJohn Marino 					   oload_champ, no_adl))
27045796c8dcSSimon Schubert 	{
27055796c8dcSSimon Schubert 	  return 1;
27065796c8dcSSimon Schubert 	}
27075796c8dcSSimon Schubert     };
27085796c8dcSSimon Schubert 
27095796c8dcSSimon Schubert   /* If we reach here, either we're in the deepest namespace or we
27105796c8dcSSimon Schubert      didn't find a good match in a deeper namespace.  But, in the
27115796c8dcSSimon Schubert      latter case, we still have a bad match in a deeper namespace;
27125796c8dcSSimon Schubert      note that we might not find any match at all in the current
27135796c8dcSSimon Schubert      namespace.  (There's always a match in the deepest namespace,
27145796c8dcSSimon Schubert      because this overload mechanism only gets called if there's a
27155796c8dcSSimon Schubert      function symbol to start off with.)  */
27165796c8dcSSimon Schubert 
27175796c8dcSSimon Schubert   old_cleanups = make_cleanup (xfree, *oload_syms);
27185796c8dcSSimon Schubert   old_cleanups = make_cleanup (xfree, *oload_champ_bv);
27195796c8dcSSimon Schubert   new_namespace = alloca (namespace_len + 1);
27205796c8dcSSimon Schubert   strncpy (new_namespace, qualified_name, namespace_len);
27215796c8dcSSimon Schubert   new_namespace[namespace_len] = '\0';
27225796c8dcSSimon Schubert   new_oload_syms = make_symbol_overload_list (func_name,
27235796c8dcSSimon Schubert 					      new_namespace);
2724*cf7f2e2dSJohn Marino 
2725*cf7f2e2dSJohn Marino   /* If we have reached the deepest level perform argument
2726*cf7f2e2dSJohn Marino      determined lookup.  */
2727*cf7f2e2dSJohn Marino   if (!searched_deeper && !no_adl)
2728*cf7f2e2dSJohn Marino     make_symbol_overload_list_adl (arg_types, nargs, func_name);
2729*cf7f2e2dSJohn Marino 
27305796c8dcSSimon Schubert   while (new_oload_syms[num_fns])
27315796c8dcSSimon Schubert     ++num_fns;
27325796c8dcSSimon Schubert 
27335796c8dcSSimon Schubert   new_oload_champ = find_oload_champ (arg_types, nargs, 0, num_fns,
27345796c8dcSSimon Schubert 				      NULL, new_oload_syms,
27355796c8dcSSimon Schubert 				      &new_oload_champ_bv);
27365796c8dcSSimon Schubert 
27375796c8dcSSimon Schubert   /* Case 1: We found a good match.  Free earlier matches (if any),
27385796c8dcSSimon Schubert      and return it.  Case 2: We didn't find a good match, but we're
27395796c8dcSSimon Schubert      not the deepest function.  Then go with the bad match that the
27405796c8dcSSimon Schubert      deeper function found.  Case 3: We found a bad match, and we're
27415796c8dcSSimon Schubert      the deepest function.  Then return what we found, even though
27425796c8dcSSimon Schubert      it's a bad match.  */
27435796c8dcSSimon Schubert 
27445796c8dcSSimon Schubert   if (new_oload_champ != -1
27455796c8dcSSimon Schubert       && classify_oload_match (new_oload_champ_bv, nargs, 0) == STANDARD)
27465796c8dcSSimon Schubert     {
27475796c8dcSSimon Schubert       *oload_syms = new_oload_syms;
27485796c8dcSSimon Schubert       *oload_champ = new_oload_champ;
27495796c8dcSSimon Schubert       *oload_champ_bv = new_oload_champ_bv;
27505796c8dcSSimon Schubert       do_cleanups (old_cleanups);
27515796c8dcSSimon Schubert       return 1;
27525796c8dcSSimon Schubert     }
27535796c8dcSSimon Schubert   else if (searched_deeper)
27545796c8dcSSimon Schubert     {
27555796c8dcSSimon Schubert       xfree (new_oload_syms);
27565796c8dcSSimon Schubert       xfree (new_oload_champ_bv);
27575796c8dcSSimon Schubert       discard_cleanups (old_cleanups);
27585796c8dcSSimon Schubert       return 0;
27595796c8dcSSimon Schubert     }
27605796c8dcSSimon Schubert   else
27615796c8dcSSimon Schubert     {
27625796c8dcSSimon Schubert       *oload_syms = new_oload_syms;
27635796c8dcSSimon Schubert       *oload_champ = new_oload_champ;
27645796c8dcSSimon Schubert       *oload_champ_bv = new_oload_champ_bv;
27655796c8dcSSimon Schubert       discard_cleanups (old_cleanups);
27665796c8dcSSimon Schubert       return 0;
27675796c8dcSSimon Schubert     }
27685796c8dcSSimon Schubert }
27695796c8dcSSimon Schubert 
27705796c8dcSSimon Schubert /* Look for a function to take NARGS args of types ARG_TYPES.  Find
27715796c8dcSSimon Schubert    the best match from among the overloaded methods or functions
27725796c8dcSSimon Schubert    (depending on METHOD) given by FNS_PTR or OLOAD_SYMS, respectively.
27735796c8dcSSimon Schubert    The number of methods/functions in the list is given by NUM_FNS.
27745796c8dcSSimon Schubert    Return the index of the best match; store an indication of the
27755796c8dcSSimon Schubert    quality of the match in OLOAD_CHAMP_BV.
27765796c8dcSSimon Schubert 
27775796c8dcSSimon Schubert    It is the caller's responsibility to free *OLOAD_CHAMP_BV.  */
27785796c8dcSSimon Schubert 
27795796c8dcSSimon Schubert static int
27805796c8dcSSimon Schubert find_oload_champ (struct type **arg_types, int nargs, int method,
27815796c8dcSSimon Schubert 		  int num_fns, struct fn_field *fns_ptr,
27825796c8dcSSimon Schubert 		  struct symbol **oload_syms,
27835796c8dcSSimon Schubert 		  struct badness_vector **oload_champ_bv)
27845796c8dcSSimon Schubert {
27855796c8dcSSimon Schubert   int ix;
27865796c8dcSSimon Schubert   /* A measure of how good an overloaded instance is.  */
27875796c8dcSSimon Schubert   struct badness_vector *bv;
27885796c8dcSSimon Schubert   /* Index of best overloaded function.  */
27895796c8dcSSimon Schubert   int oload_champ = -1;
27905796c8dcSSimon Schubert   /* Current ambiguity state for overload resolution.  */
27915796c8dcSSimon Schubert   int oload_ambiguous = 0;
27925796c8dcSSimon Schubert   /* 0 => no ambiguity, 1 => two good funcs, 2 => incomparable funcs.  */
27935796c8dcSSimon Schubert 
27945796c8dcSSimon Schubert   *oload_champ_bv = NULL;
27955796c8dcSSimon Schubert 
27965796c8dcSSimon Schubert   /* Consider each candidate in turn.  */
27975796c8dcSSimon Schubert   for (ix = 0; ix < num_fns; ix++)
27985796c8dcSSimon Schubert     {
27995796c8dcSSimon Schubert       int jj;
28005796c8dcSSimon Schubert       int static_offset = oload_method_static (method, fns_ptr, ix);
28015796c8dcSSimon Schubert       int nparms;
28025796c8dcSSimon Schubert       struct type **parm_types;
28035796c8dcSSimon Schubert 
28045796c8dcSSimon Schubert       if (method)
28055796c8dcSSimon Schubert 	{
28065796c8dcSSimon Schubert 	  nparms = TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (fns_ptr, ix));
28075796c8dcSSimon Schubert 	}
28085796c8dcSSimon Schubert       else
28095796c8dcSSimon Schubert 	{
28105796c8dcSSimon Schubert 	  /* If it's not a method, this is the proper place.  */
28115796c8dcSSimon Schubert 	  nparms = TYPE_NFIELDS (SYMBOL_TYPE (oload_syms[ix]));
28125796c8dcSSimon Schubert 	}
28135796c8dcSSimon Schubert 
28145796c8dcSSimon Schubert       /* Prepare array of parameter types.  */
28155796c8dcSSimon Schubert       parm_types = (struct type **)
28165796c8dcSSimon Schubert 	xmalloc (nparms * (sizeof (struct type *)));
28175796c8dcSSimon Schubert       for (jj = 0; jj < nparms; jj++)
28185796c8dcSSimon Schubert 	parm_types[jj] = (method
28195796c8dcSSimon Schubert 			  ? (TYPE_FN_FIELD_ARGS (fns_ptr, ix)[jj].type)
28205796c8dcSSimon Schubert 			  : TYPE_FIELD_TYPE (SYMBOL_TYPE (oload_syms[ix]),
28215796c8dcSSimon Schubert 					     jj));
28225796c8dcSSimon Schubert 
28235796c8dcSSimon Schubert       /* Compare parameter types to supplied argument types.  Skip
28245796c8dcSSimon Schubert          THIS for static methods.  */
28255796c8dcSSimon Schubert       bv = rank_function (parm_types, nparms,
28265796c8dcSSimon Schubert 			  arg_types + static_offset,
28275796c8dcSSimon Schubert 			  nargs - static_offset);
28285796c8dcSSimon Schubert 
28295796c8dcSSimon Schubert       if (!*oload_champ_bv)
28305796c8dcSSimon Schubert 	{
28315796c8dcSSimon Schubert 	  *oload_champ_bv = bv;
28325796c8dcSSimon Schubert 	  oload_champ = 0;
28335796c8dcSSimon Schubert 	}
28345796c8dcSSimon Schubert       else /* See whether current candidate is better or worse than
28355796c8dcSSimon Schubert 	      previous best.  */
28365796c8dcSSimon Schubert 	switch (compare_badness (bv, *oload_champ_bv))
28375796c8dcSSimon Schubert 	  {
28385796c8dcSSimon Schubert 	  case 0:		/* Top two contenders are equally good.  */
28395796c8dcSSimon Schubert 	    oload_ambiguous = 1;
28405796c8dcSSimon Schubert 	    break;
28415796c8dcSSimon Schubert 	  case 1:		/* Incomparable top contenders.  */
28425796c8dcSSimon Schubert 	    oload_ambiguous = 2;
28435796c8dcSSimon Schubert 	    break;
28445796c8dcSSimon Schubert 	  case 2:		/* New champion, record details.  */
28455796c8dcSSimon Schubert 	    *oload_champ_bv = bv;
28465796c8dcSSimon Schubert 	    oload_ambiguous = 0;
28475796c8dcSSimon Schubert 	    oload_champ = ix;
28485796c8dcSSimon Schubert 	    break;
28495796c8dcSSimon Schubert 	  case 3:
28505796c8dcSSimon Schubert 	  default:
28515796c8dcSSimon Schubert 	    break;
28525796c8dcSSimon Schubert 	  }
28535796c8dcSSimon Schubert       xfree (parm_types);
28545796c8dcSSimon Schubert       if (overload_debug)
28555796c8dcSSimon Schubert 	{
28565796c8dcSSimon Schubert 	  if (method)
28575796c8dcSSimon Schubert 	    fprintf_filtered (gdb_stderr,
28585796c8dcSSimon Schubert 			      "Overloaded method instance %s, # of parms %d\n",
28595796c8dcSSimon Schubert 			      fns_ptr[ix].physname, nparms);
28605796c8dcSSimon Schubert 	  else
28615796c8dcSSimon Schubert 	    fprintf_filtered (gdb_stderr,
28625796c8dcSSimon Schubert 			      "Overloaded function instance %s # of parms %d\n",
28635796c8dcSSimon Schubert 			      SYMBOL_DEMANGLED_NAME (oload_syms[ix]),
28645796c8dcSSimon Schubert 			      nparms);
28655796c8dcSSimon Schubert 	  for (jj = 0; jj < nargs - static_offset; jj++)
28665796c8dcSSimon Schubert 	    fprintf_filtered (gdb_stderr,
28675796c8dcSSimon Schubert 			      "...Badness @ %d : %d\n",
28685796c8dcSSimon Schubert 			      jj, bv->rank[jj]);
28695796c8dcSSimon Schubert 	  fprintf_filtered (gdb_stderr,
28705796c8dcSSimon Schubert 			    "Overload resolution champion is %d, ambiguous? %d\n",
28715796c8dcSSimon Schubert 			    oload_champ, oload_ambiguous);
28725796c8dcSSimon Schubert 	}
28735796c8dcSSimon Schubert     }
28745796c8dcSSimon Schubert 
28755796c8dcSSimon Schubert   return oload_champ;
28765796c8dcSSimon Schubert }
28775796c8dcSSimon Schubert 
28785796c8dcSSimon Schubert /* Return 1 if we're looking at a static method, 0 if we're looking at
28795796c8dcSSimon Schubert    a non-static method or a function that isn't a method.  */
28805796c8dcSSimon Schubert 
28815796c8dcSSimon Schubert static int
28825796c8dcSSimon Schubert oload_method_static (int method, struct fn_field *fns_ptr, int index)
28835796c8dcSSimon Schubert {
2884*cf7f2e2dSJohn Marino   if (method && fns_ptr && index >= 0
2885*cf7f2e2dSJohn Marino       && TYPE_FN_FIELD_STATIC_P (fns_ptr, index))
28865796c8dcSSimon Schubert     return 1;
28875796c8dcSSimon Schubert   else
28885796c8dcSSimon Schubert     return 0;
28895796c8dcSSimon Schubert }
28905796c8dcSSimon Schubert 
28915796c8dcSSimon Schubert /* Check how good an overload match OLOAD_CHAMP_BV represents.  */
28925796c8dcSSimon Schubert 
28935796c8dcSSimon Schubert static enum oload_classification
28945796c8dcSSimon Schubert classify_oload_match (struct badness_vector *oload_champ_bv,
28955796c8dcSSimon Schubert 		      int nargs,
28965796c8dcSSimon Schubert 		      int static_offset)
28975796c8dcSSimon Schubert {
28985796c8dcSSimon Schubert   int ix;
28995796c8dcSSimon Schubert 
29005796c8dcSSimon Schubert   for (ix = 1; ix <= nargs - static_offset; ix++)
29015796c8dcSSimon Schubert     {
29025796c8dcSSimon Schubert       if (oload_champ_bv->rank[ix] >= 100)
29035796c8dcSSimon Schubert 	return INCOMPATIBLE;	/* Truly mismatched types.  */
29045796c8dcSSimon Schubert       else if (oload_champ_bv->rank[ix] >= 10)
29055796c8dcSSimon Schubert 	return NON_STANDARD;	/* Non-standard type conversions
29065796c8dcSSimon Schubert 				   needed.  */
29075796c8dcSSimon Schubert     }
29085796c8dcSSimon Schubert 
29095796c8dcSSimon Schubert   return STANDARD;		/* Only standard conversions needed.  */
29105796c8dcSSimon Schubert }
29115796c8dcSSimon Schubert 
29125796c8dcSSimon Schubert /* C++: return 1 is NAME is a legitimate name for the destructor of
29135796c8dcSSimon Schubert    type TYPE.  If TYPE does not have a destructor, or if NAME is
29145796c8dcSSimon Schubert    inappropriate for TYPE, an error is signaled.  */
29155796c8dcSSimon Schubert int
29165796c8dcSSimon Schubert destructor_name_p (const char *name, const struct type *type)
29175796c8dcSSimon Schubert {
29185796c8dcSSimon Schubert   if (name[0] == '~')
29195796c8dcSSimon Schubert     {
29205796c8dcSSimon Schubert       char *dname = type_name_no_tag (type);
29215796c8dcSSimon Schubert       char *cp = strchr (dname, '<');
29225796c8dcSSimon Schubert       unsigned int len;
29235796c8dcSSimon Schubert 
29245796c8dcSSimon Schubert       /* Do not compare the template part for template classes.  */
29255796c8dcSSimon Schubert       if (cp == NULL)
29265796c8dcSSimon Schubert 	len = strlen (dname);
29275796c8dcSSimon Schubert       else
29285796c8dcSSimon Schubert 	len = cp - dname;
29295796c8dcSSimon Schubert       if (strlen (name + 1) != len || strncmp (dname, name + 1, len) != 0)
29305796c8dcSSimon Schubert 	error (_("name of destructor must equal name of class"));
29315796c8dcSSimon Schubert       else
29325796c8dcSSimon Schubert 	return 1;
29335796c8dcSSimon Schubert     }
29345796c8dcSSimon Schubert   return 0;
29355796c8dcSSimon Schubert }
29365796c8dcSSimon Schubert 
29375796c8dcSSimon Schubert /* Given TYPE, a structure/union,
29385796c8dcSSimon Schubert    return 1 if the component named NAME from the ultimate target
29395796c8dcSSimon Schubert    structure/union is defined, otherwise, return 0.  */
29405796c8dcSSimon Schubert 
29415796c8dcSSimon Schubert int
29425796c8dcSSimon Schubert check_field (struct type *type, const char *name)
29435796c8dcSSimon Schubert {
29445796c8dcSSimon Schubert   int i;
29455796c8dcSSimon Schubert 
2946*cf7f2e2dSJohn Marino   /* The type may be a stub.  */
2947*cf7f2e2dSJohn Marino   CHECK_TYPEDEF (type);
2948*cf7f2e2dSJohn Marino 
29495796c8dcSSimon Schubert   for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
29505796c8dcSSimon Schubert     {
29515796c8dcSSimon Schubert       char *t_field_name = TYPE_FIELD_NAME (type, i);
2952*cf7f2e2dSJohn Marino 
29535796c8dcSSimon Schubert       if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
29545796c8dcSSimon Schubert 	return 1;
29555796c8dcSSimon Schubert     }
29565796c8dcSSimon Schubert 
29575796c8dcSSimon Schubert   /* C++: If it was not found as a data field, then try to return it
29585796c8dcSSimon Schubert      as a pointer to a method.  */
29595796c8dcSSimon Schubert 
29605796c8dcSSimon Schubert   for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
29615796c8dcSSimon Schubert     {
29625796c8dcSSimon Schubert       if (strcmp_iw (TYPE_FN_FIELDLIST_NAME (type, i), name) == 0)
29635796c8dcSSimon Schubert 	return 1;
29645796c8dcSSimon Schubert     }
29655796c8dcSSimon Schubert 
29665796c8dcSSimon Schubert   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
29675796c8dcSSimon Schubert     if (check_field (TYPE_BASECLASS (type, i), name))
29685796c8dcSSimon Schubert       return 1;
29695796c8dcSSimon Schubert 
29705796c8dcSSimon Schubert   return 0;
29715796c8dcSSimon Schubert }
29725796c8dcSSimon Schubert 
29735796c8dcSSimon Schubert /* C++: Given an aggregate type CURTYPE, and a member name NAME,
29745796c8dcSSimon Schubert    return the appropriate member (or the address of the member, if
29755796c8dcSSimon Schubert    WANT_ADDRESS).  This function is used to resolve user expressions
29765796c8dcSSimon Schubert    of the form "DOMAIN::NAME".  For more details on what happens, see
29775796c8dcSSimon Schubert    the comment before value_struct_elt_for_reference.  */
29785796c8dcSSimon Schubert 
29795796c8dcSSimon Schubert struct value *
2980*cf7f2e2dSJohn Marino value_aggregate_elt (struct type *curtype, char *name,
2981*cf7f2e2dSJohn Marino 		     struct type *expect_type, int want_address,
29825796c8dcSSimon Schubert 		     enum noside noside)
29835796c8dcSSimon Schubert {
29845796c8dcSSimon Schubert   switch (TYPE_CODE (curtype))
29855796c8dcSSimon Schubert     {
29865796c8dcSSimon Schubert     case TYPE_CODE_STRUCT:
29875796c8dcSSimon Schubert     case TYPE_CODE_UNION:
29885796c8dcSSimon Schubert       return value_struct_elt_for_reference (curtype, 0, curtype,
2989*cf7f2e2dSJohn Marino 					     name, expect_type,
29905796c8dcSSimon Schubert 					     want_address, noside);
29915796c8dcSSimon Schubert     case TYPE_CODE_NAMESPACE:
29925796c8dcSSimon Schubert       return value_namespace_elt (curtype, name,
29935796c8dcSSimon Schubert 				  want_address, noside);
29945796c8dcSSimon Schubert     default:
29955796c8dcSSimon Schubert       internal_error (__FILE__, __LINE__,
29965796c8dcSSimon Schubert 		      _("non-aggregate type in value_aggregate_elt"));
29975796c8dcSSimon Schubert     }
29985796c8dcSSimon Schubert }
29995796c8dcSSimon Schubert 
3000*cf7f2e2dSJohn Marino /* Compares the two method/function types T1 and T2 for "equality"
3001*cf7f2e2dSJohn Marino    with respect to the the methods' parameters.  If the types of the
3002*cf7f2e2dSJohn Marino    two parameter lists are the same, returns 1; 0 otherwise.  This
3003*cf7f2e2dSJohn Marino    comparison may ignore any artificial parameters in T1 if
3004*cf7f2e2dSJohn Marino    SKIP_ARTIFICIAL is non-zero.  This function will ALWAYS skip
3005*cf7f2e2dSJohn Marino    the first artificial parameter in T1, assumed to be a 'this' pointer.
3006*cf7f2e2dSJohn Marino 
3007*cf7f2e2dSJohn Marino    The type T2 is expected to have come from make_params (in eval.c).  */
3008*cf7f2e2dSJohn Marino 
3009*cf7f2e2dSJohn Marino static int
3010*cf7f2e2dSJohn Marino compare_parameters (struct type *t1, struct type *t2, int skip_artificial)
3011*cf7f2e2dSJohn Marino {
3012*cf7f2e2dSJohn Marino   int start = 0;
3013*cf7f2e2dSJohn Marino 
3014*cf7f2e2dSJohn Marino   if (TYPE_FIELD_ARTIFICIAL (t1, 0))
3015*cf7f2e2dSJohn Marino     ++start;
3016*cf7f2e2dSJohn Marino 
3017*cf7f2e2dSJohn Marino   /* If skipping artificial fields, find the first real field
3018*cf7f2e2dSJohn Marino      in T1. */
3019*cf7f2e2dSJohn Marino   if (skip_artificial)
3020*cf7f2e2dSJohn Marino     {
3021*cf7f2e2dSJohn Marino       while (start < TYPE_NFIELDS (t1)
3022*cf7f2e2dSJohn Marino 	     && TYPE_FIELD_ARTIFICIAL (t1, start))
3023*cf7f2e2dSJohn Marino 	++start;
3024*cf7f2e2dSJohn Marino     }
3025*cf7f2e2dSJohn Marino 
3026*cf7f2e2dSJohn Marino   /* Now compare parameters */
3027*cf7f2e2dSJohn Marino 
3028*cf7f2e2dSJohn Marino   /* Special case: a method taking void.  T1 will contain no
3029*cf7f2e2dSJohn Marino      non-artificial fields, and T2 will contain TYPE_CODE_VOID.  */
3030*cf7f2e2dSJohn Marino   if ((TYPE_NFIELDS (t1) - start) == 0 && TYPE_NFIELDS (t2) == 1
3031*cf7f2e2dSJohn Marino       && TYPE_CODE (TYPE_FIELD_TYPE (t2, 0)) == TYPE_CODE_VOID)
3032*cf7f2e2dSJohn Marino     return 1;
3033*cf7f2e2dSJohn Marino 
3034*cf7f2e2dSJohn Marino   if ((TYPE_NFIELDS (t1) - start) == TYPE_NFIELDS (t2))
3035*cf7f2e2dSJohn Marino     {
3036*cf7f2e2dSJohn Marino       int i;
3037*cf7f2e2dSJohn Marino 
3038*cf7f2e2dSJohn Marino       for (i = 0; i < TYPE_NFIELDS (t2); ++i)
3039*cf7f2e2dSJohn Marino 	{
3040*cf7f2e2dSJohn Marino 	  if (rank_one_type (TYPE_FIELD_TYPE (t1, start + i),
3041*cf7f2e2dSJohn Marino 			      TYPE_FIELD_TYPE (t2, i))
3042*cf7f2e2dSJohn Marino 	      != 0)
3043*cf7f2e2dSJohn Marino 	    return 0;
3044*cf7f2e2dSJohn Marino 	}
3045*cf7f2e2dSJohn Marino 
3046*cf7f2e2dSJohn Marino       return 1;
3047*cf7f2e2dSJohn Marino     }
3048*cf7f2e2dSJohn Marino 
3049*cf7f2e2dSJohn Marino   return 0;
3050*cf7f2e2dSJohn Marino }
3051*cf7f2e2dSJohn Marino 
30525796c8dcSSimon Schubert /* C++: Given an aggregate type CURTYPE, and a member name NAME,
30535796c8dcSSimon Schubert    return the address of this member as a "pointer to member" type.
30545796c8dcSSimon Schubert    If INTYPE is non-null, then it will be the type of the member we
30555796c8dcSSimon Schubert    are looking for.  This will help us resolve "pointers to member
30565796c8dcSSimon Schubert    functions".  This function is used to resolve user expressions of
30575796c8dcSSimon Schubert    the form "DOMAIN::NAME".  */
30585796c8dcSSimon Schubert 
30595796c8dcSSimon Schubert static struct value *
30605796c8dcSSimon Schubert value_struct_elt_for_reference (struct type *domain, int offset,
30615796c8dcSSimon Schubert 				struct type *curtype, char *name,
30625796c8dcSSimon Schubert 				struct type *intype,
30635796c8dcSSimon Schubert 				int want_address,
30645796c8dcSSimon Schubert 				enum noside noside)
30655796c8dcSSimon Schubert {
30665796c8dcSSimon Schubert   struct type *t = curtype;
30675796c8dcSSimon Schubert   int i;
30685796c8dcSSimon Schubert   struct value *v, *result;
30695796c8dcSSimon Schubert 
30705796c8dcSSimon Schubert   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
30715796c8dcSSimon Schubert       && TYPE_CODE (t) != TYPE_CODE_UNION)
30725796c8dcSSimon Schubert     error (_("Internal error: non-aggregate type to value_struct_elt_for_reference"));
30735796c8dcSSimon Schubert 
30745796c8dcSSimon Schubert   for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
30755796c8dcSSimon Schubert     {
30765796c8dcSSimon Schubert       char *t_field_name = TYPE_FIELD_NAME (t, i);
30775796c8dcSSimon Schubert 
30785796c8dcSSimon Schubert       if (t_field_name && strcmp (t_field_name, name) == 0)
30795796c8dcSSimon Schubert 	{
30805796c8dcSSimon Schubert 	  if (field_is_static (&TYPE_FIELD (t, i)))
30815796c8dcSSimon Schubert 	    {
30825796c8dcSSimon Schubert 	      v = value_static_field (t, i);
30835796c8dcSSimon Schubert 	      if (v == NULL)
30845796c8dcSSimon Schubert 		error (_("static field %s has been optimized out"),
30855796c8dcSSimon Schubert 		       name);
30865796c8dcSSimon Schubert 	      if (want_address)
30875796c8dcSSimon Schubert 		v = value_addr (v);
30885796c8dcSSimon Schubert 	      return v;
30895796c8dcSSimon Schubert 	    }
30905796c8dcSSimon Schubert 	  if (TYPE_FIELD_PACKED (t, i))
30915796c8dcSSimon Schubert 	    error (_("pointers to bitfield members not allowed"));
30925796c8dcSSimon Schubert 
30935796c8dcSSimon Schubert 	  if (want_address)
30945796c8dcSSimon Schubert 	    return value_from_longest
30955796c8dcSSimon Schubert 	      (lookup_memberptr_type (TYPE_FIELD_TYPE (t, i), domain),
30965796c8dcSSimon Schubert 	       offset + (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3));
30975796c8dcSSimon Schubert 	  else if (noside == EVAL_AVOID_SIDE_EFFECTS)
30985796c8dcSSimon Schubert 	    return allocate_value (TYPE_FIELD_TYPE (t, i));
30995796c8dcSSimon Schubert 	  else
31005796c8dcSSimon Schubert 	    error (_("Cannot reference non-static field \"%s\""), name);
31015796c8dcSSimon Schubert 	}
31025796c8dcSSimon Schubert     }
31035796c8dcSSimon Schubert 
31045796c8dcSSimon Schubert   /* C++: If it was not found as a data field, then try to return it
31055796c8dcSSimon Schubert      as a pointer to a method.  */
31065796c8dcSSimon Schubert 
31075796c8dcSSimon Schubert   /* Perform all necessary dereferencing.  */
31085796c8dcSSimon Schubert   while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR)
31095796c8dcSSimon Schubert     intype = TYPE_TARGET_TYPE (intype);
31105796c8dcSSimon Schubert 
31115796c8dcSSimon Schubert   for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
31125796c8dcSSimon Schubert     {
31135796c8dcSSimon Schubert       char *t_field_name = TYPE_FN_FIELDLIST_NAME (t, i);
31145796c8dcSSimon Schubert       char dem_opname[64];
31155796c8dcSSimon Schubert 
31165796c8dcSSimon Schubert       if (strncmp (t_field_name, "__", 2) == 0
31175796c8dcSSimon Schubert 	  || strncmp (t_field_name, "op", 2) == 0
31185796c8dcSSimon Schubert 	  || strncmp (t_field_name, "type", 4) == 0)
31195796c8dcSSimon Schubert 	{
31205796c8dcSSimon Schubert 	  if (cplus_demangle_opname (t_field_name,
31215796c8dcSSimon Schubert 				     dem_opname, DMGL_ANSI))
31225796c8dcSSimon Schubert 	    t_field_name = dem_opname;
31235796c8dcSSimon Schubert 	  else if (cplus_demangle_opname (t_field_name,
31245796c8dcSSimon Schubert 					  dem_opname, 0))
31255796c8dcSSimon Schubert 	    t_field_name = dem_opname;
31265796c8dcSSimon Schubert 	}
31275796c8dcSSimon Schubert       if (t_field_name && strcmp (t_field_name, name) == 0)
31285796c8dcSSimon Schubert 	{
3129*cf7f2e2dSJohn Marino 	  int j;
3130*cf7f2e2dSJohn Marino 	  int len = TYPE_FN_FIELDLIST_LENGTH (t, i);
31315796c8dcSSimon Schubert 	  struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
31325796c8dcSSimon Schubert 
31335796c8dcSSimon Schubert 	  check_stub_method_group (t, i);
31345796c8dcSSimon Schubert 
31355796c8dcSSimon Schubert 	  if (intype)
31365796c8dcSSimon Schubert 	    {
3137*cf7f2e2dSJohn Marino 	      for (j = 0; j < len; ++j)
3138*cf7f2e2dSJohn Marino 		{
3139*cf7f2e2dSJohn Marino 		  if (compare_parameters (TYPE_FN_FIELD_TYPE (f, j), intype, 0)
3140*cf7f2e2dSJohn Marino 		      || compare_parameters (TYPE_FN_FIELD_TYPE (f, j), intype, 1))
31415796c8dcSSimon Schubert 		    break;
3142*cf7f2e2dSJohn Marino 		}
3143*cf7f2e2dSJohn Marino 
3144*cf7f2e2dSJohn Marino 	      if (j == len)
31455796c8dcSSimon Schubert 		error (_("no member function matches that type instantiation"));
31465796c8dcSSimon Schubert 	    }
31475796c8dcSSimon Schubert 	  else
3148*cf7f2e2dSJohn Marino 	    {
3149*cf7f2e2dSJohn Marino 	      int ii;
3150*cf7f2e2dSJohn Marino 
3151*cf7f2e2dSJohn Marino 	      j = -1;
3152*cf7f2e2dSJohn Marino 	      for (ii = 0; ii < TYPE_FN_FIELDLIST_LENGTH (t, i);
3153*cf7f2e2dSJohn Marino 		   ++ii)
3154*cf7f2e2dSJohn Marino 		{
3155*cf7f2e2dSJohn Marino 		  /* Skip artificial methods.  This is necessary if,
3156*cf7f2e2dSJohn Marino 		     for example, the user wants to "print
3157*cf7f2e2dSJohn Marino 		     subclass::subclass" with only one user-defined
3158*cf7f2e2dSJohn Marino 		     constructor.  There is no ambiguity in this
3159*cf7f2e2dSJohn Marino 		     case.  */
3160*cf7f2e2dSJohn Marino 		  if (TYPE_FN_FIELD_ARTIFICIAL (f, ii))
3161*cf7f2e2dSJohn Marino 		    continue;
3162*cf7f2e2dSJohn Marino 
3163*cf7f2e2dSJohn Marino 		  /* Desired method is ambiguous if more than one
3164*cf7f2e2dSJohn Marino 		     method is defined.  */
3165*cf7f2e2dSJohn Marino 		  if (j != -1)
3166*cf7f2e2dSJohn Marino 		    error (_("non-unique member `%s' requires type instantiation"), name);
3167*cf7f2e2dSJohn Marino 
3168*cf7f2e2dSJohn Marino 		  j = ii;
3169*cf7f2e2dSJohn Marino 		}
3170*cf7f2e2dSJohn Marino 	    }
31715796c8dcSSimon Schubert 
31725796c8dcSSimon Schubert 	  if (TYPE_FN_FIELD_STATIC_P (f, j))
31735796c8dcSSimon Schubert 	    {
31745796c8dcSSimon Schubert 	      struct symbol *s =
31755796c8dcSSimon Schubert 		lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
31765796c8dcSSimon Schubert 			       0, VAR_DOMAIN, 0);
3177*cf7f2e2dSJohn Marino 
31785796c8dcSSimon Schubert 	      if (s == NULL)
31795796c8dcSSimon Schubert 		return NULL;
31805796c8dcSSimon Schubert 
31815796c8dcSSimon Schubert 	      if (want_address)
31825796c8dcSSimon Schubert 		return value_addr (read_var_value (s, 0));
31835796c8dcSSimon Schubert 	      else
31845796c8dcSSimon Schubert 		return read_var_value (s, 0);
31855796c8dcSSimon Schubert 	    }
31865796c8dcSSimon Schubert 
31875796c8dcSSimon Schubert 	  if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
31885796c8dcSSimon Schubert 	    {
31895796c8dcSSimon Schubert 	      if (want_address)
31905796c8dcSSimon Schubert 		{
31915796c8dcSSimon Schubert 		  result = allocate_value
31925796c8dcSSimon Schubert 		    (lookup_methodptr_type (TYPE_FN_FIELD_TYPE (f, j)));
31935796c8dcSSimon Schubert 		  cplus_make_method_ptr (value_type (result),
31945796c8dcSSimon Schubert 					 value_contents_writeable (result),
31955796c8dcSSimon Schubert 					 TYPE_FN_FIELD_VOFFSET (f, j), 1);
31965796c8dcSSimon Schubert 		}
31975796c8dcSSimon Schubert 	      else if (noside == EVAL_AVOID_SIDE_EFFECTS)
31985796c8dcSSimon Schubert 		return allocate_value (TYPE_FN_FIELD_TYPE (f, j));
31995796c8dcSSimon Schubert 	      else
32005796c8dcSSimon Schubert 		error (_("Cannot reference virtual member function \"%s\""),
32015796c8dcSSimon Schubert 		       name);
32025796c8dcSSimon Schubert 	    }
32035796c8dcSSimon Schubert 	  else
32045796c8dcSSimon Schubert 	    {
32055796c8dcSSimon Schubert 	      struct symbol *s =
32065796c8dcSSimon Schubert 		lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
32075796c8dcSSimon Schubert 			       0, VAR_DOMAIN, 0);
3208*cf7f2e2dSJohn Marino 
32095796c8dcSSimon Schubert 	      if (s == NULL)
32105796c8dcSSimon Schubert 		return NULL;
32115796c8dcSSimon Schubert 
32125796c8dcSSimon Schubert 	      v = read_var_value (s, 0);
32135796c8dcSSimon Schubert 	      if (!want_address)
32145796c8dcSSimon Schubert 		result = v;
32155796c8dcSSimon Schubert 	      else
32165796c8dcSSimon Schubert 		{
32175796c8dcSSimon Schubert 		  result = allocate_value (lookup_methodptr_type (TYPE_FN_FIELD_TYPE (f, j)));
32185796c8dcSSimon Schubert 		  cplus_make_method_ptr (value_type (result),
32195796c8dcSSimon Schubert 					 value_contents_writeable (result),
32205796c8dcSSimon Schubert 					 value_address (v), 0);
32215796c8dcSSimon Schubert 		}
32225796c8dcSSimon Schubert 	    }
32235796c8dcSSimon Schubert 	  return result;
32245796c8dcSSimon Schubert 	}
32255796c8dcSSimon Schubert     }
32265796c8dcSSimon Schubert   for (i = TYPE_N_BASECLASSES (t) - 1; i >= 0; i--)
32275796c8dcSSimon Schubert     {
32285796c8dcSSimon Schubert       struct value *v;
32295796c8dcSSimon Schubert       int base_offset;
32305796c8dcSSimon Schubert 
32315796c8dcSSimon Schubert       if (BASETYPE_VIA_VIRTUAL (t, i))
32325796c8dcSSimon Schubert 	base_offset = 0;
32335796c8dcSSimon Schubert       else
32345796c8dcSSimon Schubert 	base_offset = TYPE_BASECLASS_BITPOS (t, i) / 8;
32355796c8dcSSimon Schubert       v = value_struct_elt_for_reference (domain,
32365796c8dcSSimon Schubert 					  offset + base_offset,
32375796c8dcSSimon Schubert 					  TYPE_BASECLASS (t, i),
32385796c8dcSSimon Schubert 					  name, intype,
32395796c8dcSSimon Schubert 					  want_address, noside);
32405796c8dcSSimon Schubert       if (v)
32415796c8dcSSimon Schubert 	return v;
32425796c8dcSSimon Schubert     }
32435796c8dcSSimon Schubert 
32445796c8dcSSimon Schubert   /* As a last chance, pretend that CURTYPE is a namespace, and look
32455796c8dcSSimon Schubert      it up that way; this (frequently) works for types nested inside
32465796c8dcSSimon Schubert      classes.  */
32475796c8dcSSimon Schubert 
32485796c8dcSSimon Schubert   return value_maybe_namespace_elt (curtype, name,
32495796c8dcSSimon Schubert 				    want_address, noside);
32505796c8dcSSimon Schubert }
32515796c8dcSSimon Schubert 
32525796c8dcSSimon Schubert /* C++: Return the member NAME of the namespace given by the type
32535796c8dcSSimon Schubert    CURTYPE.  */
32545796c8dcSSimon Schubert 
32555796c8dcSSimon Schubert static struct value *
32565796c8dcSSimon Schubert value_namespace_elt (const struct type *curtype,
32575796c8dcSSimon Schubert 		     char *name, int want_address,
32585796c8dcSSimon Schubert 		     enum noside noside)
32595796c8dcSSimon Schubert {
32605796c8dcSSimon Schubert   struct value *retval = value_maybe_namespace_elt (curtype, name,
32615796c8dcSSimon Schubert 						    want_address,
32625796c8dcSSimon Schubert 						    noside);
32635796c8dcSSimon Schubert 
32645796c8dcSSimon Schubert   if (retval == NULL)
32655796c8dcSSimon Schubert     error (_("No symbol \"%s\" in namespace \"%s\"."),
32665796c8dcSSimon Schubert 	   name, TYPE_TAG_NAME (curtype));
32675796c8dcSSimon Schubert 
32685796c8dcSSimon Schubert   return retval;
32695796c8dcSSimon Schubert }
32705796c8dcSSimon Schubert 
32715796c8dcSSimon Schubert /* A helper function used by value_namespace_elt and
32725796c8dcSSimon Schubert    value_struct_elt_for_reference.  It looks up NAME inside the
32735796c8dcSSimon Schubert    context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
32745796c8dcSSimon Schubert    is a class and NAME refers to a type in CURTYPE itself (as opposed
32755796c8dcSSimon Schubert    to, say, some base class of CURTYPE).  */
32765796c8dcSSimon Schubert 
32775796c8dcSSimon Schubert static struct value *
32785796c8dcSSimon Schubert value_maybe_namespace_elt (const struct type *curtype,
32795796c8dcSSimon Schubert 			   char *name, int want_address,
32805796c8dcSSimon Schubert 			   enum noside noside)
32815796c8dcSSimon Schubert {
32825796c8dcSSimon Schubert   const char *namespace_name = TYPE_TAG_NAME (curtype);
32835796c8dcSSimon Schubert   struct symbol *sym;
32845796c8dcSSimon Schubert   struct value *result;
32855796c8dcSSimon Schubert 
3286*cf7f2e2dSJohn Marino   sym = cp_lookup_symbol_namespace (namespace_name, name,
3287*cf7f2e2dSJohn Marino 				    get_selected_block (0), VAR_DOMAIN);
3288*cf7f2e2dSJohn Marino 
3289*cf7f2e2dSJohn Marino   if (sym == NULL)
3290*cf7f2e2dSJohn Marino     {
3291*cf7f2e2dSJohn Marino       char *concatenated_name = alloca (strlen (namespace_name) + 2
3292*cf7f2e2dSJohn Marino 					+ strlen (name) + 1);
3293*cf7f2e2dSJohn Marino 
3294*cf7f2e2dSJohn Marino       sprintf (concatenated_name, "%s::%s", namespace_name, name);
3295*cf7f2e2dSJohn Marino       sym = lookup_static_symbol_aux (concatenated_name, VAR_DOMAIN);
3296*cf7f2e2dSJohn Marino     }
32975796c8dcSSimon Schubert 
32985796c8dcSSimon Schubert   if (sym == NULL)
32995796c8dcSSimon Schubert     return NULL;
33005796c8dcSSimon Schubert   else if ((noside == EVAL_AVOID_SIDE_EFFECTS)
33015796c8dcSSimon Schubert 	   && (SYMBOL_CLASS (sym) == LOC_TYPEDEF))
33025796c8dcSSimon Schubert     result = allocate_value (SYMBOL_TYPE (sym));
33035796c8dcSSimon Schubert   else
33045796c8dcSSimon Schubert     result = value_of_variable (sym, get_selected_block (0));
33055796c8dcSSimon Schubert 
33065796c8dcSSimon Schubert   if (result && want_address)
33075796c8dcSSimon Schubert     result = value_addr (result);
33085796c8dcSSimon Schubert 
33095796c8dcSSimon Schubert   return result;
33105796c8dcSSimon Schubert }
33115796c8dcSSimon Schubert 
33125796c8dcSSimon Schubert /* Given a pointer value V, find the real (RTTI) type of the object it
33135796c8dcSSimon Schubert    points to.
33145796c8dcSSimon Schubert 
33155796c8dcSSimon Schubert    Other parameters FULL, TOP, USING_ENC as with value_rtti_type()
33165796c8dcSSimon Schubert    and refer to the values computed for the object pointed to.  */
33175796c8dcSSimon Schubert 
33185796c8dcSSimon Schubert struct type *
33195796c8dcSSimon Schubert value_rtti_target_type (struct value *v, int *full,
33205796c8dcSSimon Schubert 			int *top, int *using_enc)
33215796c8dcSSimon Schubert {
33225796c8dcSSimon Schubert   struct value *target;
33235796c8dcSSimon Schubert 
33245796c8dcSSimon Schubert   target = value_ind (v);
33255796c8dcSSimon Schubert 
33265796c8dcSSimon Schubert   return value_rtti_type (target, full, top, using_enc);
33275796c8dcSSimon Schubert }
33285796c8dcSSimon Schubert 
33295796c8dcSSimon Schubert /* Given a value pointed to by ARGP, check its real run-time type, and
33305796c8dcSSimon Schubert    if that is different from the enclosing type, create a new value
33315796c8dcSSimon Schubert    using the real run-time type as the enclosing type (and of the same
33325796c8dcSSimon Schubert    type as ARGP) and return it, with the embedded offset adjusted to
33335796c8dcSSimon Schubert    be the correct offset to the enclosed object.  RTYPE is the type,
33345796c8dcSSimon Schubert    and XFULL, XTOP, and XUSING_ENC are the other parameters, computed
33355796c8dcSSimon Schubert    by value_rtti_type().  If these are available, they can be supplied
33365796c8dcSSimon Schubert    and a second call to value_rtti_type() is avoided.  (Pass RTYPE ==
33375796c8dcSSimon Schubert    NULL if they're not available.  */
33385796c8dcSSimon Schubert 
33395796c8dcSSimon Schubert struct value *
33405796c8dcSSimon Schubert value_full_object (struct value *argp,
33415796c8dcSSimon Schubert 		   struct type *rtype,
33425796c8dcSSimon Schubert 		   int xfull, int xtop,
33435796c8dcSSimon Schubert 		   int xusing_enc)
33445796c8dcSSimon Schubert {
33455796c8dcSSimon Schubert   struct type *real_type;
33465796c8dcSSimon Schubert   int full = 0;
33475796c8dcSSimon Schubert   int top = -1;
33485796c8dcSSimon Schubert   int using_enc = 0;
33495796c8dcSSimon Schubert   struct value *new_val;
33505796c8dcSSimon Schubert 
33515796c8dcSSimon Schubert   if (rtype)
33525796c8dcSSimon Schubert     {
33535796c8dcSSimon Schubert       real_type = rtype;
33545796c8dcSSimon Schubert       full = xfull;
33555796c8dcSSimon Schubert       top = xtop;
33565796c8dcSSimon Schubert       using_enc = xusing_enc;
33575796c8dcSSimon Schubert     }
33585796c8dcSSimon Schubert   else
33595796c8dcSSimon Schubert     real_type = value_rtti_type (argp, &full, &top, &using_enc);
33605796c8dcSSimon Schubert 
33615796c8dcSSimon Schubert   /* If no RTTI data, or if object is already complete, do nothing.  */
33625796c8dcSSimon Schubert   if (!real_type || real_type == value_enclosing_type (argp))
33635796c8dcSSimon Schubert     return argp;
33645796c8dcSSimon Schubert 
33655796c8dcSSimon Schubert   /* If we have the full object, but for some reason the enclosing
33665796c8dcSSimon Schubert      type is wrong, set it.  */
33675796c8dcSSimon Schubert   /* pai: FIXME -- sounds iffy */
33685796c8dcSSimon Schubert   if (full)
33695796c8dcSSimon Schubert     {
33705796c8dcSSimon Schubert       argp = value_change_enclosing_type (argp, real_type);
33715796c8dcSSimon Schubert       return argp;
33725796c8dcSSimon Schubert     }
33735796c8dcSSimon Schubert 
33745796c8dcSSimon Schubert   /* Check if object is in memory */
33755796c8dcSSimon Schubert   if (VALUE_LVAL (argp) != lval_memory)
33765796c8dcSSimon Schubert     {
33775796c8dcSSimon Schubert       warning (_("Couldn't retrieve complete object of RTTI type %s; object may be in register(s)."),
33785796c8dcSSimon Schubert 	       TYPE_NAME (real_type));
33795796c8dcSSimon Schubert 
33805796c8dcSSimon Schubert       return argp;
33815796c8dcSSimon Schubert     }
33825796c8dcSSimon Schubert 
33835796c8dcSSimon Schubert   /* All other cases -- retrieve the complete object.  */
33845796c8dcSSimon Schubert   /* Go back by the computed top_offset from the beginning of the
33855796c8dcSSimon Schubert      object, adjusting for the embedded offset of argp if that's what
33865796c8dcSSimon Schubert      value_rtti_type used for its computation.  */
33875796c8dcSSimon Schubert   new_val = value_at_lazy (real_type, value_address (argp) - top +
33885796c8dcSSimon Schubert 			   (using_enc ? 0 : value_embedded_offset (argp)));
33895796c8dcSSimon Schubert   deprecated_set_value_type (new_val, value_type (argp));
33905796c8dcSSimon Schubert   set_value_embedded_offset (new_val, (using_enc
33915796c8dcSSimon Schubert 				       ? top + value_embedded_offset (argp)
33925796c8dcSSimon Schubert 				       : top));
33935796c8dcSSimon Schubert   return new_val;
33945796c8dcSSimon Schubert }
33955796c8dcSSimon Schubert 
33965796c8dcSSimon Schubert 
33975796c8dcSSimon Schubert /* Return the value of the local variable, if one exists.
33985796c8dcSSimon Schubert    Flag COMPLAIN signals an error if the request is made in an
33995796c8dcSSimon Schubert    inappropriate context.  */
34005796c8dcSSimon Schubert 
34015796c8dcSSimon Schubert struct value *
34025796c8dcSSimon Schubert value_of_local (const char *name, int complain)
34035796c8dcSSimon Schubert {
34045796c8dcSSimon Schubert   struct symbol *func, *sym;
34055796c8dcSSimon Schubert   struct block *b;
34065796c8dcSSimon Schubert   struct value * ret;
34075796c8dcSSimon Schubert   struct frame_info *frame;
34085796c8dcSSimon Schubert 
34095796c8dcSSimon Schubert   if (complain)
34105796c8dcSSimon Schubert     frame = get_selected_frame (_("no frame selected"));
34115796c8dcSSimon Schubert   else
34125796c8dcSSimon Schubert     {
34135796c8dcSSimon Schubert       frame = deprecated_safe_get_selected_frame ();
34145796c8dcSSimon Schubert       if (frame == 0)
34155796c8dcSSimon Schubert 	return 0;
34165796c8dcSSimon Schubert     }
34175796c8dcSSimon Schubert 
34185796c8dcSSimon Schubert   func = get_frame_function (frame);
34195796c8dcSSimon Schubert   if (!func)
34205796c8dcSSimon Schubert     {
34215796c8dcSSimon Schubert       if (complain)
34225796c8dcSSimon Schubert 	error (_("no `%s' in nameless context"), name);
34235796c8dcSSimon Schubert       else
34245796c8dcSSimon Schubert 	return 0;
34255796c8dcSSimon Schubert     }
34265796c8dcSSimon Schubert 
34275796c8dcSSimon Schubert   b = SYMBOL_BLOCK_VALUE (func);
34285796c8dcSSimon Schubert   if (dict_empty (BLOCK_DICT (b)))
34295796c8dcSSimon Schubert     {
34305796c8dcSSimon Schubert       if (complain)
34315796c8dcSSimon Schubert 	error (_("no args, no `%s'"), name);
34325796c8dcSSimon Schubert       else
34335796c8dcSSimon Schubert 	return 0;
34345796c8dcSSimon Schubert     }
34355796c8dcSSimon Schubert 
34365796c8dcSSimon Schubert   /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
34375796c8dcSSimon Schubert      symbol instead of the LOC_ARG one (if both exist).  */
3438*cf7f2e2dSJohn Marino   sym = lookup_block_symbol (b, name, VAR_DOMAIN);
34395796c8dcSSimon Schubert   if (sym == NULL)
34405796c8dcSSimon Schubert     {
34415796c8dcSSimon Schubert       if (complain)
34425796c8dcSSimon Schubert 	error (_("current stack frame does not contain a variable named `%s'"),
34435796c8dcSSimon Schubert 	       name);
34445796c8dcSSimon Schubert       else
34455796c8dcSSimon Schubert 	return NULL;
34465796c8dcSSimon Schubert     }
34475796c8dcSSimon Schubert 
34485796c8dcSSimon Schubert   ret = read_var_value (sym, frame);
34495796c8dcSSimon Schubert   if (ret == 0 && complain)
34505796c8dcSSimon Schubert     error (_("`%s' argument unreadable"), name);
34515796c8dcSSimon Schubert   return ret;
34525796c8dcSSimon Schubert }
34535796c8dcSSimon Schubert 
34545796c8dcSSimon Schubert /* C++/Objective-C: return the value of the class instance variable,
34555796c8dcSSimon Schubert    if one exists.  Flag COMPLAIN signals an error if the request is
34565796c8dcSSimon Schubert    made in an inappropriate context.  */
34575796c8dcSSimon Schubert 
34585796c8dcSSimon Schubert struct value *
34595796c8dcSSimon Schubert value_of_this (int complain)
34605796c8dcSSimon Schubert {
34615796c8dcSSimon Schubert   if (!current_language->la_name_of_this)
34625796c8dcSSimon Schubert     return 0;
34635796c8dcSSimon Schubert   return value_of_local (current_language->la_name_of_this, complain);
34645796c8dcSSimon Schubert }
34655796c8dcSSimon Schubert 
34665796c8dcSSimon Schubert /* Create a slice (sub-string, sub-array) of ARRAY, that is LENGTH
34675796c8dcSSimon Schubert    elements long, starting at LOWBOUND.  The result has the same lower
34685796c8dcSSimon Schubert    bound as the original ARRAY.  */
34695796c8dcSSimon Schubert 
34705796c8dcSSimon Schubert struct value *
34715796c8dcSSimon Schubert value_slice (struct value *array, int lowbound, int length)
34725796c8dcSSimon Schubert {
34735796c8dcSSimon Schubert   struct type *slice_range_type, *slice_type, *range_type;
34745796c8dcSSimon Schubert   LONGEST lowerbound, upperbound;
34755796c8dcSSimon Schubert   struct value *slice;
34765796c8dcSSimon Schubert   struct type *array_type;
34775796c8dcSSimon Schubert 
34785796c8dcSSimon Schubert   array_type = check_typedef (value_type (array));
34795796c8dcSSimon Schubert   if (TYPE_CODE (array_type) != TYPE_CODE_ARRAY
34805796c8dcSSimon Schubert       && TYPE_CODE (array_type) != TYPE_CODE_STRING
34815796c8dcSSimon Schubert       && TYPE_CODE (array_type) != TYPE_CODE_BITSTRING)
34825796c8dcSSimon Schubert     error (_("cannot take slice of non-array"));
34835796c8dcSSimon Schubert 
34845796c8dcSSimon Schubert   range_type = TYPE_INDEX_TYPE (array_type);
34855796c8dcSSimon Schubert   if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0)
34865796c8dcSSimon Schubert     error (_("slice from bad array or bitstring"));
34875796c8dcSSimon Schubert 
34885796c8dcSSimon Schubert   if (lowbound < lowerbound || length < 0
34895796c8dcSSimon Schubert       || lowbound + length - 1 > upperbound)
34905796c8dcSSimon Schubert     error (_("slice out of range"));
34915796c8dcSSimon Schubert 
34925796c8dcSSimon Schubert   /* FIXME-type-allocation: need a way to free this type when we are
34935796c8dcSSimon Schubert      done with it.  */
34945796c8dcSSimon Schubert   slice_range_type = create_range_type ((struct type *) NULL,
34955796c8dcSSimon Schubert 					TYPE_TARGET_TYPE (range_type),
34965796c8dcSSimon Schubert 					lowbound,
34975796c8dcSSimon Schubert 					lowbound + length - 1);
34985796c8dcSSimon Schubert   if (TYPE_CODE (array_type) == TYPE_CODE_BITSTRING)
34995796c8dcSSimon Schubert     {
35005796c8dcSSimon Schubert       int i;
35015796c8dcSSimon Schubert 
35025796c8dcSSimon Schubert       slice_type = create_set_type ((struct type *) NULL,
35035796c8dcSSimon Schubert 				    slice_range_type);
35045796c8dcSSimon Schubert       TYPE_CODE (slice_type) = TYPE_CODE_BITSTRING;
35055796c8dcSSimon Schubert       slice = value_zero (slice_type, not_lval);
35065796c8dcSSimon Schubert 
35075796c8dcSSimon Schubert       for (i = 0; i < length; i++)
35085796c8dcSSimon Schubert 	{
35095796c8dcSSimon Schubert 	  int element = value_bit_index (array_type,
35105796c8dcSSimon Schubert 					 value_contents (array),
35115796c8dcSSimon Schubert 					 lowbound + i);
3512*cf7f2e2dSJohn Marino 
35135796c8dcSSimon Schubert 	  if (element < 0)
35145796c8dcSSimon Schubert 	    error (_("internal error accessing bitstring"));
35155796c8dcSSimon Schubert 	  else if (element > 0)
35165796c8dcSSimon Schubert 	    {
35175796c8dcSSimon Schubert 	      int j = i % TARGET_CHAR_BIT;
3518*cf7f2e2dSJohn Marino 
35195796c8dcSSimon Schubert 	      if (gdbarch_bits_big_endian (get_type_arch (array_type)))
35205796c8dcSSimon Schubert 		j = TARGET_CHAR_BIT - 1 - j;
35215796c8dcSSimon Schubert 	      value_contents_raw (slice)[i / TARGET_CHAR_BIT] |= (1 << j);
35225796c8dcSSimon Schubert 	    }
35235796c8dcSSimon Schubert 	}
35245796c8dcSSimon Schubert       /* We should set the address, bitssize, and bitspos, so the
35255796c8dcSSimon Schubert          slice can be used on the LHS, but that may require extensions
35265796c8dcSSimon Schubert          to value_assign.  For now, just leave as a non_lval.
35275796c8dcSSimon Schubert          FIXME.  */
35285796c8dcSSimon Schubert     }
35295796c8dcSSimon Schubert   else
35305796c8dcSSimon Schubert     {
35315796c8dcSSimon Schubert       struct type *element_type = TYPE_TARGET_TYPE (array_type);
35325796c8dcSSimon Schubert       LONGEST offset =
35335796c8dcSSimon Schubert 	(lowbound - lowerbound) * TYPE_LENGTH (check_typedef (element_type));
35345796c8dcSSimon Schubert 
35355796c8dcSSimon Schubert       slice_type = create_array_type ((struct type *) NULL,
35365796c8dcSSimon Schubert 				      element_type,
35375796c8dcSSimon Schubert 				      slice_range_type);
35385796c8dcSSimon Schubert       TYPE_CODE (slice_type) = TYPE_CODE (array_type);
35395796c8dcSSimon Schubert 
35405796c8dcSSimon Schubert       if (VALUE_LVAL (array) == lval_memory && value_lazy (array))
35415796c8dcSSimon Schubert 	slice = allocate_value_lazy (slice_type);
35425796c8dcSSimon Schubert       else
35435796c8dcSSimon Schubert 	{
35445796c8dcSSimon Schubert 	  slice = allocate_value (slice_type);
35455796c8dcSSimon Schubert 	  memcpy (value_contents_writeable (slice),
35465796c8dcSSimon Schubert 		  value_contents (array) + offset,
35475796c8dcSSimon Schubert 		  TYPE_LENGTH (slice_type));
35485796c8dcSSimon Schubert 	}
35495796c8dcSSimon Schubert 
35505796c8dcSSimon Schubert       set_value_component_location (slice, array);
35515796c8dcSSimon Schubert       VALUE_FRAME_ID (slice) = VALUE_FRAME_ID (array);
35525796c8dcSSimon Schubert       set_value_offset (slice, value_offset (array) + offset);
35535796c8dcSSimon Schubert     }
35545796c8dcSSimon Schubert   return slice;
35555796c8dcSSimon Schubert }
35565796c8dcSSimon Schubert 
35575796c8dcSSimon Schubert /* Create a value for a FORTRAN complex number.  Currently most of the
35585796c8dcSSimon Schubert    time values are coerced to COMPLEX*16 (i.e. a complex number
35595796c8dcSSimon Schubert    composed of 2 doubles.  This really should be a smarter routine
35605796c8dcSSimon Schubert    that figures out precision inteligently as opposed to assuming
35615796c8dcSSimon Schubert    doubles.  FIXME: fmb  */
35625796c8dcSSimon Schubert 
35635796c8dcSSimon Schubert struct value *
35645796c8dcSSimon Schubert value_literal_complex (struct value *arg1,
35655796c8dcSSimon Schubert 		       struct value *arg2,
35665796c8dcSSimon Schubert 		       struct type *type)
35675796c8dcSSimon Schubert {
35685796c8dcSSimon Schubert   struct value *val;
35695796c8dcSSimon Schubert   struct type *real_type = TYPE_TARGET_TYPE (type);
35705796c8dcSSimon Schubert 
35715796c8dcSSimon Schubert   val = allocate_value (type);
35725796c8dcSSimon Schubert   arg1 = value_cast (real_type, arg1);
35735796c8dcSSimon Schubert   arg2 = value_cast (real_type, arg2);
35745796c8dcSSimon Schubert 
35755796c8dcSSimon Schubert   memcpy (value_contents_raw (val),
35765796c8dcSSimon Schubert 	  value_contents (arg1), TYPE_LENGTH (real_type));
35775796c8dcSSimon Schubert   memcpy (value_contents_raw (val) + TYPE_LENGTH (real_type),
35785796c8dcSSimon Schubert 	  value_contents (arg2), TYPE_LENGTH (real_type));
35795796c8dcSSimon Schubert   return val;
35805796c8dcSSimon Schubert }
35815796c8dcSSimon Schubert 
35825796c8dcSSimon Schubert /* Cast a value into the appropriate complex data type.  */
35835796c8dcSSimon Schubert 
35845796c8dcSSimon Schubert static struct value *
35855796c8dcSSimon Schubert cast_into_complex (struct type *type, struct value *val)
35865796c8dcSSimon Schubert {
35875796c8dcSSimon Schubert   struct type *real_type = TYPE_TARGET_TYPE (type);
35885796c8dcSSimon Schubert 
35895796c8dcSSimon Schubert   if (TYPE_CODE (value_type (val)) == TYPE_CODE_COMPLEX)
35905796c8dcSSimon Schubert     {
35915796c8dcSSimon Schubert       struct type *val_real_type = TYPE_TARGET_TYPE (value_type (val));
35925796c8dcSSimon Schubert       struct value *re_val = allocate_value (val_real_type);
35935796c8dcSSimon Schubert       struct value *im_val = allocate_value (val_real_type);
35945796c8dcSSimon Schubert 
35955796c8dcSSimon Schubert       memcpy (value_contents_raw (re_val),
35965796c8dcSSimon Schubert 	      value_contents (val), TYPE_LENGTH (val_real_type));
35975796c8dcSSimon Schubert       memcpy (value_contents_raw (im_val),
35985796c8dcSSimon Schubert 	      value_contents (val) + TYPE_LENGTH (val_real_type),
35995796c8dcSSimon Schubert 	      TYPE_LENGTH (val_real_type));
36005796c8dcSSimon Schubert 
36015796c8dcSSimon Schubert       return value_literal_complex (re_val, im_val, type);
36025796c8dcSSimon Schubert     }
36035796c8dcSSimon Schubert   else if (TYPE_CODE (value_type (val)) == TYPE_CODE_FLT
36045796c8dcSSimon Schubert 	   || TYPE_CODE (value_type (val)) == TYPE_CODE_INT)
36055796c8dcSSimon Schubert     return value_literal_complex (val,
36065796c8dcSSimon Schubert 				  value_zero (real_type, not_lval),
36075796c8dcSSimon Schubert 				  type);
36085796c8dcSSimon Schubert   else
36095796c8dcSSimon Schubert     error (_("cannot cast non-number to complex"));
36105796c8dcSSimon Schubert }
36115796c8dcSSimon Schubert 
36125796c8dcSSimon Schubert void
36135796c8dcSSimon Schubert _initialize_valops (void)
36145796c8dcSSimon Schubert {
36155796c8dcSSimon Schubert   add_setshow_boolean_cmd ("overload-resolution", class_support,
36165796c8dcSSimon Schubert 			   &overload_resolution, _("\
36175796c8dcSSimon Schubert Set overload resolution in evaluating C++ functions."), _("\
36185796c8dcSSimon Schubert Show overload resolution in evaluating C++ functions."),
36195796c8dcSSimon Schubert 			   NULL, NULL,
36205796c8dcSSimon Schubert 			   show_overload_resolution,
36215796c8dcSSimon Schubert 			   &setlist, &showlist);
36225796c8dcSSimon Schubert   overload_resolution = 1;
36235796c8dcSSimon Schubert }
3624