Home | History | Annotate | Line # | Download | only in gdb
valops.c revision 1.1.1.8
      1      1.1  christos /* Perform non-arithmetic operations on values, for GDB.
      2      1.1  christos 
      3  1.1.1.8  christos    Copyright (C) 1986-2023 Free Software Foundation, Inc.
      4      1.1  christos 
      5      1.1  christos    This file is part of GDB.
      6      1.1  christos 
      7      1.1  christos    This program is free software; you can redistribute it and/or modify
      8      1.1  christos    it under the terms of the GNU General Public License as published by
      9      1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10      1.1  christos    (at your option) any later version.
     11      1.1  christos 
     12      1.1  christos    This program is distributed in the hope that it will be useful,
     13      1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14      1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15      1.1  christos    GNU General Public License for more details.
     16      1.1  christos 
     17      1.1  christos    You should have received a copy of the GNU General Public License
     18      1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19      1.1  christos 
     20      1.1  christos #include "defs.h"
     21      1.1  christos #include "symtab.h"
     22      1.1  christos #include "gdbtypes.h"
     23      1.1  christos #include "value.h"
     24      1.1  christos #include "frame.h"
     25      1.1  christos #include "inferior.h"
     26      1.1  christos #include "gdbcore.h"
     27      1.1  christos #include "target.h"
     28      1.1  christos #include "demangle.h"
     29      1.1  christos #include "language.h"
     30      1.1  christos #include "gdbcmd.h"
     31      1.1  christos #include "regcache.h"
     32      1.1  christos #include "cp-abi.h"
     33      1.1  christos #include "block.h"
     34      1.1  christos #include "infcall.h"
     35      1.1  christos #include "dictionary.h"
     36      1.1  christos #include "cp-support.h"
     37  1.1.1.6  christos #include "target-float.h"
     38      1.1  christos #include "tracepoint.h"
     39  1.1.1.6  christos #include "observable.h"
     40      1.1  christos #include "objfiles.h"
     41  1.1.1.2  christos #include "extension.h"
     42  1.1.1.7  christos #include "gdbtypes.h"
     43  1.1.1.7  christos #include "gdbsupport/byte-vector.h"
     44  1.1.1.8  christos #include "typeprint.h"
     45      1.1  christos 
     46      1.1  christos /* Local functions.  */
     47      1.1  christos 
     48  1.1.1.8  christos static int typecmp (bool staticp, bool varargs, int nargs,
     49  1.1.1.8  christos 		    struct field t1[], const gdb::array_view<value *> t2);
     50      1.1  christos 
     51      1.1  christos static struct value *search_struct_field (const char *, struct value *,
     52  1.1.1.3  christos 					  struct type *, int);
     53      1.1  christos 
     54      1.1  christos static struct value *search_struct_method (const char *, struct value **,
     55  1.1.1.8  christos 					   gdb::optional<gdb::array_view<value *>>,
     56  1.1.1.4  christos 					   LONGEST, int *, struct type *);
     57      1.1  christos 
     58  1.1.1.6  christos static int find_oload_champ_namespace (gdb::array_view<value *> args,
     59      1.1  christos 				       const char *, const char *,
     60  1.1.1.6  christos 				       std::vector<symbol *> *oload_syms,
     61  1.1.1.6  christos 				       badness_vector *,
     62      1.1  christos 				       const int no_adl);
     63      1.1  christos 
     64  1.1.1.6  christos static int find_oload_champ_namespace_loop (gdb::array_view<value *> args,
     65  1.1.1.6  christos 					    const char *, const char *,
     66  1.1.1.6  christos 					    int, std::vector<symbol *> *oload_syms,
     67  1.1.1.6  christos 					    badness_vector *, int *,
     68  1.1.1.6  christos 					    const int no_adl);
     69  1.1.1.6  christos 
     70  1.1.1.6  christos static int find_oload_champ (gdb::array_view<value *> args,
     71  1.1.1.6  christos 			     size_t num_fns,
     72  1.1.1.6  christos 			     fn_field *methods,
     73  1.1.1.6  christos 			     xmethod_worker_up *xmethods,
     74  1.1.1.6  christos 			     symbol **functions,
     75  1.1.1.6  christos 			     badness_vector *oload_champ_bv);
     76      1.1  christos 
     77  1.1.1.2  christos static int oload_method_static_p (struct fn_field *, int);
     78      1.1  christos 
     79      1.1  christos enum oload_classification { STANDARD, NON_STANDARD, INCOMPATIBLE };
     80      1.1  christos 
     81  1.1.1.6  christos static enum oload_classification classify_oload_match
     82  1.1.1.6  christos   (const badness_vector &, int, int);
     83      1.1  christos 
     84      1.1  christos static struct value *value_struct_elt_for_reference (struct type *,
     85      1.1  christos 						     int, struct type *,
     86  1.1.1.2  christos 						     const char *,
     87      1.1  christos 						     struct type *,
     88      1.1  christos 						     int, enum noside);
     89      1.1  christos 
     90      1.1  christos static struct value *value_namespace_elt (const struct type *,
     91  1.1.1.2  christos 					  const char *, int , enum noside);
     92      1.1  christos 
     93      1.1  christos static struct value *value_maybe_namespace_elt (const struct type *,
     94  1.1.1.2  christos 						const char *, int,
     95      1.1  christos 						enum noside);
     96      1.1  christos 
     97      1.1  christos static CORE_ADDR allocate_space_in_inferior (int);
     98      1.1  christos 
     99      1.1  christos static struct value *cast_into_complex (struct type *, struct value *);
    100      1.1  christos 
    101  1.1.1.7  christos bool overload_resolution = false;
    102      1.1  christos static void
    103      1.1  christos show_overload_resolution (struct ui_file *file, int from_tty,
    104      1.1  christos 			  struct cmd_list_element *c,
    105      1.1  christos 			  const char *value)
    106      1.1  christos {
    107  1.1.1.8  christos   gdb_printf (file, _("Overload resolution in evaluating "
    108  1.1.1.8  christos 		      "C++ functions is %s.\n"),
    109  1.1.1.8  christos 	      value);
    110      1.1  christos }
    111      1.1  christos 
    112      1.1  christos /* Find the address of function name NAME in the inferior.  If OBJF_P
    113      1.1  christos    is non-NULL, *OBJF_P will be set to the OBJFILE where the function
    114      1.1  christos    is defined.  */
    115      1.1  christos 
    116      1.1  christos struct value *
    117      1.1  christos find_function_in_inferior (const char *name, struct objfile **objf_p)
    118      1.1  christos {
    119  1.1.1.4  christos   struct block_symbol sym;
    120      1.1  christos 
    121      1.1  christos   sym = lookup_symbol (name, 0, VAR_DOMAIN, 0);
    122  1.1.1.4  christos   if (sym.symbol != NULL)
    123      1.1  christos     {
    124  1.1.1.8  christos       if (sym.symbol->aclass () != LOC_BLOCK)
    125      1.1  christos 	{
    126      1.1  christos 	  error (_("\"%s\" exists in this program but is not a function."),
    127      1.1  christos 		 name);
    128      1.1  christos 	}
    129      1.1  christos 
    130      1.1  christos       if (objf_p)
    131  1.1.1.8  christos 	*objf_p = sym.symbol->objfile ();
    132      1.1  christos 
    133  1.1.1.4  christos       return value_of_variable (sym.symbol, sym.block);
    134      1.1  christos     }
    135      1.1  christos   else
    136      1.1  christos     {
    137      1.1  christos       struct bound_minimal_symbol msymbol =
    138      1.1  christos 	lookup_bound_minimal_symbol (name);
    139      1.1  christos 
    140      1.1  christos       if (msymbol.minsym != NULL)
    141      1.1  christos 	{
    142      1.1  christos 	  struct objfile *objfile = msymbol.objfile;
    143  1.1.1.7  christos 	  struct gdbarch *gdbarch = objfile->arch ();
    144      1.1  christos 
    145      1.1  christos 	  struct type *type;
    146      1.1  christos 	  CORE_ADDR maddr;
    147      1.1  christos 	  type = lookup_pointer_type (builtin_type (gdbarch)->builtin_char);
    148      1.1  christos 	  type = lookup_function_type (type);
    149      1.1  christos 	  type = lookup_pointer_type (type);
    150  1.1.1.8  christos 	  maddr = msymbol.value_address ();
    151      1.1  christos 
    152      1.1  christos 	  if (objf_p)
    153      1.1  christos 	    *objf_p = objfile;
    154      1.1  christos 
    155      1.1  christos 	  return value_from_pointer (type, maddr);
    156      1.1  christos 	}
    157      1.1  christos       else
    158      1.1  christos 	{
    159  1.1.1.8  christos 	  if (!target_has_execution ())
    160      1.1  christos 	    error (_("evaluation of this expression "
    161      1.1  christos 		     "requires the target program to be active"));
    162      1.1  christos 	  else
    163      1.1  christos 	    error (_("evaluation of this expression requires the "
    164      1.1  christos 		     "program to have a function \"%s\"."),
    165      1.1  christos 		   name);
    166      1.1  christos 	}
    167      1.1  christos     }
    168      1.1  christos }
    169      1.1  christos 
    170      1.1  christos /* Allocate NBYTES of space in the inferior using the inferior's
    171      1.1  christos    malloc and return a value that is a pointer to the allocated
    172      1.1  christos    space.  */
    173      1.1  christos 
    174      1.1  christos struct value *
    175      1.1  christos value_allocate_space_in_inferior (int len)
    176      1.1  christos {
    177      1.1  christos   struct objfile *objf;
    178      1.1  christos   struct value *val = find_function_in_inferior ("malloc", &objf);
    179  1.1.1.7  christos   struct gdbarch *gdbarch = objf->arch ();
    180      1.1  christos   struct value *blocklen;
    181      1.1  christos 
    182      1.1  christos   blocklen = value_from_longest (builtin_type (gdbarch)->builtin_int, len);
    183  1.1.1.6  christos   val = call_function_by_hand (val, NULL, blocklen);
    184      1.1  christos   if (value_logical_not (val))
    185      1.1  christos     {
    186  1.1.1.8  christos       if (!target_has_execution ())
    187      1.1  christos 	error (_("No memory available to program now: "
    188      1.1  christos 		 "you need to start the target first"));
    189      1.1  christos       else
    190      1.1  christos 	error (_("No memory available to program: call to malloc failed"));
    191      1.1  christos     }
    192      1.1  christos   return val;
    193      1.1  christos }
    194      1.1  christos 
    195      1.1  christos static CORE_ADDR
    196      1.1  christos allocate_space_in_inferior (int len)
    197      1.1  christos {
    198      1.1  christos   return value_as_long (value_allocate_space_in_inferior (len));
    199      1.1  christos }
    200      1.1  christos 
    201      1.1  christos /* Cast struct value VAL to type TYPE and return as a value.
    202      1.1  christos    Both type and val must be of TYPE_CODE_STRUCT or TYPE_CODE_UNION
    203      1.1  christos    for this to work.  Typedef to one of the codes is permitted.
    204      1.1  christos    Returns NULL if the cast is neither an upcast nor a downcast.  */
    205      1.1  christos 
    206      1.1  christos static struct value *
    207      1.1  christos value_cast_structs (struct type *type, struct value *v2)
    208      1.1  christos {
    209      1.1  christos   struct type *t1;
    210      1.1  christos   struct type *t2;
    211      1.1  christos   struct value *v;
    212      1.1  christos 
    213      1.1  christos   gdb_assert (type != NULL && v2 != NULL);
    214      1.1  christos 
    215      1.1  christos   t1 = check_typedef (type);
    216      1.1  christos   t2 = check_typedef (value_type (v2));
    217      1.1  christos 
    218      1.1  christos   /* Check preconditions.  */
    219  1.1.1.7  christos   gdb_assert ((t1->code () == TYPE_CODE_STRUCT
    220  1.1.1.7  christos 	       || t1->code () == TYPE_CODE_UNION)
    221      1.1  christos 	      && !!"Precondition is that type is of STRUCT or UNION kind.");
    222  1.1.1.7  christos   gdb_assert ((t2->code () == TYPE_CODE_STRUCT
    223  1.1.1.7  christos 	       || t2->code () == TYPE_CODE_UNION)
    224      1.1  christos 	      && !!"Precondition is that value is of STRUCT or UNION kind");
    225      1.1  christos 
    226  1.1.1.7  christos   if (t1->name () != NULL
    227  1.1.1.7  christos       && t2->name () != NULL
    228  1.1.1.7  christos       && !strcmp (t1->name (), t2->name ()))
    229      1.1  christos     return NULL;
    230      1.1  christos 
    231      1.1  christos   /* Upcasting: look in the type of the source to see if it contains the
    232      1.1  christos      type of the target as a superclass.  If so, we'll need to
    233      1.1  christos      offset the pointer rather than just change its type.  */
    234  1.1.1.7  christos   if (t1->name () != NULL)
    235      1.1  christos     {
    236  1.1.1.7  christos       v = search_struct_field (t1->name (),
    237  1.1.1.3  christos 			       v2, t2, 1);
    238      1.1  christos       if (v)
    239      1.1  christos 	return v;
    240      1.1  christos     }
    241      1.1  christos 
    242      1.1  christos   /* Downcasting: look in the type of the target to see if it contains the
    243      1.1  christos      type of the source as a superclass.  If so, we'll need to
    244      1.1  christos      offset the pointer rather than just change its type.  */
    245  1.1.1.7  christos   if (t2->name () != NULL)
    246      1.1  christos     {
    247      1.1  christos       /* Try downcasting using the run-time type of the value.  */
    248  1.1.1.4  christos       int full, using_enc;
    249  1.1.1.4  christos       LONGEST top;
    250      1.1  christos       struct type *real_type;
    251      1.1  christos 
    252      1.1  christos       real_type = value_rtti_type (v2, &full, &top, &using_enc);
    253      1.1  christos       if (real_type)
    254      1.1  christos 	{
    255      1.1  christos 	  v = value_full_object (v2, real_type, full, top, using_enc);
    256      1.1  christos 	  v = value_at_lazy (real_type, value_address (v));
    257  1.1.1.2  christos 	  real_type = value_type (v);
    258      1.1  christos 
    259      1.1  christos 	  /* We might be trying to cast to the outermost enclosing
    260      1.1  christos 	     type, in which case search_struct_field won't work.  */
    261  1.1.1.7  christos 	  if (real_type->name () != NULL
    262  1.1.1.7  christos 	      && !strcmp (real_type->name (), t1->name ()))
    263      1.1  christos 	    return v;
    264      1.1  christos 
    265  1.1.1.7  christos 	  v = search_struct_field (t2->name (), v, real_type, 1);
    266      1.1  christos 	  if (v)
    267      1.1  christos 	    return v;
    268      1.1  christos 	}
    269      1.1  christos 
    270      1.1  christos       /* Try downcasting using information from the destination type
    271      1.1  christos 	 T2.  This wouldn't work properly for classes with virtual
    272      1.1  christos 	 bases, but those were handled above.  */
    273  1.1.1.7  christos       v = search_struct_field (t2->name (),
    274  1.1.1.3  christos 			       value_zero (t1, not_lval), t1, 1);
    275      1.1  christos       if (v)
    276      1.1  christos 	{
    277      1.1  christos 	  /* Downcasting is possible (t1 is superclass of v2).  */
    278  1.1.1.8  christos 	  CORE_ADDR addr2 = value_address (v2) + value_embedded_offset (v2);
    279      1.1  christos 
    280      1.1  christos 	  addr2 -= value_address (v) + value_embedded_offset (v);
    281      1.1  christos 	  return value_at (type, addr2);
    282      1.1  christos 	}
    283      1.1  christos     }
    284      1.1  christos 
    285      1.1  christos   return NULL;
    286      1.1  christos }
    287      1.1  christos 
    288      1.1  christos /* Cast one pointer or reference type to another.  Both TYPE and
    289      1.1  christos    the type of ARG2 should be pointer types, or else both should be
    290      1.1  christos    reference types.  If SUBCLASS_CHECK is non-zero, this will force a
    291      1.1  christos    check to see whether TYPE is a superclass of ARG2's type.  If
    292      1.1  christos    SUBCLASS_CHECK is zero, then the subclass check is done only when
    293      1.1  christos    ARG2 is itself non-zero.  Returns the new pointer or reference.  */
    294      1.1  christos 
    295      1.1  christos struct value *
    296      1.1  christos value_cast_pointers (struct type *type, struct value *arg2,
    297      1.1  christos 		     int subclass_check)
    298      1.1  christos {
    299      1.1  christos   struct type *type1 = check_typedef (type);
    300      1.1  christos   struct type *type2 = check_typedef (value_type (arg2));
    301  1.1.1.8  christos   struct type *t1 = check_typedef (type1->target_type ());
    302  1.1.1.8  christos   struct type *t2 = check_typedef (type2->target_type ());
    303      1.1  christos 
    304  1.1.1.7  christos   if (t1->code () == TYPE_CODE_STRUCT
    305  1.1.1.7  christos       && t2->code () == TYPE_CODE_STRUCT
    306      1.1  christos       && (subclass_check || !value_logical_not (arg2)))
    307      1.1  christos     {
    308      1.1  christos       struct value *v2;
    309      1.1  christos 
    310  1.1.1.5  christos       if (TYPE_IS_REFERENCE (type2))
    311      1.1  christos 	v2 = coerce_ref (arg2);
    312      1.1  christos       else
    313      1.1  christos 	v2 = value_ind (arg2);
    314  1.1.1.7  christos       gdb_assert (check_typedef (value_type (v2))->code ()
    315      1.1  christos 		  == TYPE_CODE_STRUCT && !!"Why did coercion fail?");
    316      1.1  christos       v2 = value_cast_structs (t1, v2);
    317      1.1  christos       /* At this point we have what we can have, un-dereference if needed.  */
    318      1.1  christos       if (v2)
    319      1.1  christos 	{
    320      1.1  christos 	  struct value *v = value_addr (v2);
    321      1.1  christos 
    322      1.1  christos 	  deprecated_set_value_type (v, type);
    323      1.1  christos 	  return v;
    324      1.1  christos 	}
    325  1.1.1.2  christos     }
    326      1.1  christos 
    327      1.1  christos   /* No superclass found, just change the pointer type.  */
    328      1.1  christos   arg2 = value_copy (arg2);
    329      1.1  christos   deprecated_set_value_type (arg2, type);
    330      1.1  christos   set_value_enclosing_type (arg2, type);
    331      1.1  christos   set_value_pointed_to_offset (arg2, 0);	/* pai: chk_val */
    332      1.1  christos   return arg2;
    333      1.1  christos }
    334      1.1  christos 
    335  1.1.1.8  christos /* See value.h.  */
    336  1.1.1.8  christos 
    337  1.1.1.8  christos gdb_mpq
    338  1.1.1.8  christos value_to_gdb_mpq (struct value *value)
    339  1.1.1.8  christos {
    340  1.1.1.8  christos   struct type *type = check_typedef (value_type (value));
    341  1.1.1.8  christos 
    342  1.1.1.8  christos   gdb_mpq result;
    343  1.1.1.8  christos   if (is_floating_type (type))
    344  1.1.1.8  christos     {
    345  1.1.1.8  christos       double d = target_float_to_host_double (value_contents (value).data (),
    346  1.1.1.8  christos 					      type);
    347  1.1.1.8  christos       mpq_set_d (result.val, d);
    348  1.1.1.8  christos     }
    349  1.1.1.8  christos   else
    350  1.1.1.8  christos     {
    351  1.1.1.8  christos       gdb_assert (is_integral_type (type)
    352  1.1.1.8  christos 		  || is_fixed_point_type (type));
    353  1.1.1.8  christos 
    354  1.1.1.8  christos       gdb_mpz vz;
    355  1.1.1.8  christos       vz.read (value_contents (value), type_byte_order (type),
    356  1.1.1.8  christos 	       type->is_unsigned ());
    357  1.1.1.8  christos       mpq_set_z (result.val, vz.val);
    358  1.1.1.8  christos 
    359  1.1.1.8  christos       if (is_fixed_point_type (type))
    360  1.1.1.8  christos 	mpq_mul (result.val, result.val,
    361  1.1.1.8  christos 		 type->fixed_point_scaling_factor ().val);
    362  1.1.1.8  christos     }
    363  1.1.1.8  christos 
    364  1.1.1.8  christos   return result;
    365  1.1.1.8  christos }
    366  1.1.1.8  christos 
    367  1.1.1.8  christos /* Assuming that TO_TYPE is a fixed point type, return a value
    368  1.1.1.8  christos    corresponding to the cast of FROM_VAL to that type.  */
    369  1.1.1.8  christos 
    370  1.1.1.8  christos static struct value *
    371  1.1.1.8  christos value_cast_to_fixed_point (struct type *to_type, struct value *from_val)
    372  1.1.1.8  christos {
    373  1.1.1.8  christos   struct type *from_type = value_type (from_val);
    374  1.1.1.8  christos 
    375  1.1.1.8  christos   if (from_type == to_type)
    376  1.1.1.8  christos     return from_val;
    377  1.1.1.8  christos 
    378  1.1.1.8  christos   if (!is_floating_type (from_type)
    379  1.1.1.8  christos       && !is_integral_type (from_type)
    380  1.1.1.8  christos       && !is_fixed_point_type (from_type))
    381  1.1.1.8  christos     error (_("Invalid conversion from type %s to fixed point type %s"),
    382  1.1.1.8  christos 	   from_type->name (), to_type->name ());
    383  1.1.1.8  christos 
    384  1.1.1.8  christos   gdb_mpq vq = value_to_gdb_mpq (from_val);
    385  1.1.1.8  christos 
    386  1.1.1.8  christos   /* Divide that value by the scaling factor to obtain the unscaled
    387  1.1.1.8  christos      value, first in rational form, and then in integer form.  */
    388  1.1.1.8  christos 
    389  1.1.1.8  christos   mpq_div (vq.val, vq.val, to_type->fixed_point_scaling_factor ().val);
    390  1.1.1.8  christos   gdb_mpz unscaled = vq.get_rounded ();
    391  1.1.1.8  christos 
    392  1.1.1.8  christos   /* Finally, create the result value, and pack the unscaled value
    393  1.1.1.8  christos      in it.  */
    394  1.1.1.8  christos   struct value *result = allocate_value (to_type);
    395  1.1.1.8  christos   unscaled.write (value_contents_raw (result),
    396  1.1.1.8  christos 		  type_byte_order (to_type),
    397  1.1.1.8  christos 		  to_type->is_unsigned ());
    398  1.1.1.8  christos 
    399  1.1.1.8  christos   return result;
    400  1.1.1.8  christos }
    401  1.1.1.8  christos 
    402      1.1  christos /* Cast value ARG2 to type TYPE and return as a value.
    403      1.1  christos    More general than a C cast: accepts any two types of the same length,
    404      1.1  christos    and if ARG2 is an lvalue it can be cast into anything at all.  */
    405      1.1  christos /* In C++, casts may change pointer or object representations.  */
    406      1.1  christos 
    407      1.1  christos struct value *
    408      1.1  christos value_cast (struct type *type, struct value *arg2)
    409      1.1  christos {
    410      1.1  christos   enum type_code code1;
    411      1.1  christos   enum type_code code2;
    412      1.1  christos   int scalar;
    413      1.1  christos   struct type *type2;
    414      1.1  christos 
    415      1.1  christos   int convert_to_boolean = 0;
    416      1.1  christos 
    417  1.1.1.8  christos   /* TYPE might be equal in meaning to the existing type of ARG2, but for
    418  1.1.1.8  christos      many reasons, might be a different type object (e.g. TYPE might be a
    419  1.1.1.8  christos      gdbarch owned type, while VALUE_TYPE (ARG2) could be an objfile owned
    420  1.1.1.8  christos      type).
    421  1.1.1.8  christos 
    422  1.1.1.8  christos      In this case we want to preserve the LVAL of ARG2 as this allows the
    423  1.1.1.8  christos      resulting value to be used in more places.  We do this by calling
    424  1.1.1.8  christos      VALUE_COPY if appropriate.  */
    425  1.1.1.8  christos   if (types_deeply_equal (value_type (arg2), type))
    426  1.1.1.8  christos     {
    427  1.1.1.8  christos       /* If the types are exactly equal then we can avoid creating a new
    428  1.1.1.8  christos 	 value completely.  */
    429  1.1.1.8  christos       if (value_type (arg2) != type)
    430  1.1.1.8  christos 	{
    431  1.1.1.8  christos 	  arg2 = value_copy (arg2);
    432  1.1.1.8  christos 	  deprecated_set_value_type (arg2, type);
    433  1.1.1.8  christos 	}
    434  1.1.1.8  christos       return arg2;
    435  1.1.1.8  christos     }
    436  1.1.1.8  christos 
    437  1.1.1.8  christos   if (is_fixed_point_type (type))
    438  1.1.1.8  christos     return value_cast_to_fixed_point (type, arg2);
    439      1.1  christos 
    440      1.1  christos   /* Check if we are casting struct reference to struct reference.  */
    441  1.1.1.5  christos   if (TYPE_IS_REFERENCE (check_typedef (type)))
    442      1.1  christos     {
    443      1.1  christos       /* We dereference type; then we recurse and finally
    444  1.1.1.8  christos 	 we generate value of the given reference.  Nothing wrong with
    445      1.1  christos 	 that.  */
    446      1.1  christos       struct type *t1 = check_typedef (type);
    447  1.1.1.8  christos       struct type *dereftype = check_typedef (t1->target_type ());
    448  1.1.1.5  christos       struct value *val = value_cast (dereftype, arg2);
    449      1.1  christos 
    450  1.1.1.7  christos       return value_ref (val, t1->code ());
    451      1.1  christos     }
    452      1.1  christos 
    453  1.1.1.5  christos   if (TYPE_IS_REFERENCE (check_typedef (value_type (arg2))))
    454      1.1  christos     /* We deref the value and then do the cast.  */
    455      1.1  christos     return value_cast (type, coerce_ref (arg2));
    456      1.1  christos 
    457  1.1.1.6  christos   /* Strip typedefs / resolve stubs in order to get at the type's
    458  1.1.1.6  christos      code/length, but remember the original type, to use as the
    459  1.1.1.6  christos      resulting type of the cast, in case it was a typedef.  */
    460  1.1.1.6  christos   struct type *to_type = type;
    461  1.1.1.6  christos 
    462  1.1.1.4  christos   type = check_typedef (type);
    463  1.1.1.7  christos   code1 = type->code ();
    464      1.1  christos   arg2 = coerce_ref (arg2);
    465      1.1  christos   type2 = check_typedef (value_type (arg2));
    466      1.1  christos 
    467      1.1  christos   /* You can't cast to a reference type.  See value_cast_pointers
    468      1.1  christos      instead.  */
    469  1.1.1.5  christos   gdb_assert (!TYPE_IS_REFERENCE (type));
    470      1.1  christos 
    471      1.1  christos   /* A cast to an undetermined-length array_type, such as
    472      1.1  christos      (TYPE [])OBJECT, is treated like a cast to (TYPE [N])OBJECT,
    473      1.1  christos      where N is sizeof(OBJECT)/sizeof(TYPE).  */
    474      1.1  christos   if (code1 == TYPE_CODE_ARRAY)
    475      1.1  christos     {
    476  1.1.1.8  christos       struct type *element_type = type->target_type ();
    477  1.1.1.8  christos       unsigned element_length = check_typedef (element_type)->length ();
    478      1.1  christos 
    479  1.1.1.7  christos       if (element_length > 0 && type->bounds ()->high.kind () == PROP_UNDEFINED)
    480      1.1  christos 	{
    481  1.1.1.7  christos 	  struct type *range_type = type->index_type ();
    482  1.1.1.8  christos 	  int val_length = type2->length ();
    483      1.1  christos 	  LONGEST low_bound, high_bound, new_length;
    484      1.1  christos 
    485  1.1.1.8  christos 	  if (!get_discrete_bounds (range_type, &low_bound, &high_bound))
    486      1.1  christos 	    low_bound = 0, high_bound = 0;
    487      1.1  christos 	  new_length = val_length / element_length;
    488      1.1  christos 	  if (val_length % element_length != 0)
    489      1.1  christos 	    warning (_("array element type size does not "
    490      1.1  christos 		       "divide object size in cast"));
    491      1.1  christos 	  /* FIXME-type-allocation: need a way to free this type when
    492      1.1  christos 	     we are done with it.  */
    493  1.1.1.7  christos 	  range_type = create_static_range_type (NULL,
    494  1.1.1.8  christos 						 range_type->target_type (),
    495  1.1.1.2  christos 						 low_bound,
    496  1.1.1.2  christos 						 new_length + low_bound - 1);
    497      1.1  christos 	  deprecated_set_value_type (arg2,
    498  1.1.1.7  christos 				     create_array_type (NULL,
    499      1.1  christos 							element_type,
    500      1.1  christos 							range_type));
    501      1.1  christos 	  return arg2;
    502      1.1  christos 	}
    503      1.1  christos     }
    504      1.1  christos 
    505  1.1.1.8  christos   if (current_language->c_style_arrays_p ()
    506  1.1.1.7  christos       && type2->code () == TYPE_CODE_ARRAY
    507  1.1.1.8  christos       && !type2->is_vector ())
    508      1.1  christos     arg2 = value_coerce_array (arg2);
    509      1.1  christos 
    510  1.1.1.7  christos   if (type2->code () == TYPE_CODE_FUNC)
    511      1.1  christos     arg2 = value_coerce_function (arg2);
    512      1.1  christos 
    513      1.1  christos   type2 = check_typedef (value_type (arg2));
    514  1.1.1.7  christos   code2 = type2->code ();
    515      1.1  christos 
    516      1.1  christos   if (code1 == TYPE_CODE_COMPLEX)
    517  1.1.1.6  christos     return cast_into_complex (to_type, arg2);
    518      1.1  christos   if (code1 == TYPE_CODE_BOOL)
    519      1.1  christos     {
    520      1.1  christos       code1 = TYPE_CODE_INT;
    521      1.1  christos       convert_to_boolean = 1;
    522      1.1  christos     }
    523      1.1  christos   if (code1 == TYPE_CODE_CHAR)
    524      1.1  christos     code1 = TYPE_CODE_INT;
    525      1.1  christos   if (code2 == TYPE_CODE_BOOL || code2 == TYPE_CODE_CHAR)
    526      1.1  christos     code2 = TYPE_CODE_INT;
    527      1.1  christos 
    528      1.1  christos   scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
    529      1.1  christos 	    || code2 == TYPE_CODE_DECFLOAT || code2 == TYPE_CODE_ENUM
    530  1.1.1.8  christos 	    || code2 == TYPE_CODE_RANGE
    531  1.1.1.8  christos 	    || is_fixed_point_type (type2));
    532      1.1  christos 
    533      1.1  christos   if ((code1 == TYPE_CODE_STRUCT || code1 == TYPE_CODE_UNION)
    534      1.1  christos       && (code2 == TYPE_CODE_STRUCT || code2 == TYPE_CODE_UNION)
    535  1.1.1.7  christos       && type->name () != 0)
    536      1.1  christos     {
    537  1.1.1.6  christos       struct value *v = value_cast_structs (to_type, arg2);
    538      1.1  christos 
    539      1.1  christos       if (v)
    540      1.1  christos 	return v;
    541      1.1  christos     }
    542      1.1  christos 
    543  1.1.1.6  christos   if (is_floating_type (type) && scalar)
    544  1.1.1.6  christos     {
    545  1.1.1.6  christos       if (is_floating_value (arg2))
    546  1.1.1.6  christos 	{
    547  1.1.1.6  christos 	  struct value *v = allocate_value (to_type);
    548  1.1.1.8  christos 	  target_float_convert (value_contents (arg2).data (), type2,
    549  1.1.1.8  christos 				value_contents_raw (v).data (), type);
    550  1.1.1.8  christos 	  return v;
    551  1.1.1.8  christos 	}
    552  1.1.1.8  christos       else if (is_fixed_point_type (type2))
    553  1.1.1.8  christos 	{
    554  1.1.1.8  christos 	  gdb_mpq fp_val;
    555  1.1.1.8  christos 
    556  1.1.1.8  christos 	  fp_val.read_fixed_point (value_contents (arg2),
    557  1.1.1.8  christos 				   type_byte_order (type2),
    558  1.1.1.8  christos 				   type2->is_unsigned (),
    559  1.1.1.8  christos 				   type2->fixed_point_scaling_factor ());
    560  1.1.1.8  christos 
    561  1.1.1.8  christos 	  struct value *v = allocate_value (to_type);
    562  1.1.1.8  christos 	  target_float_from_host_double (value_contents_raw (v).data (),
    563  1.1.1.8  christos 					 to_type, mpq_get_d (fp_val.val));
    564  1.1.1.6  christos 	  return v;
    565  1.1.1.6  christos 	}
    566      1.1  christos 
    567  1.1.1.6  christos       /* The only option left is an integral type.  */
    568  1.1.1.8  christos       if (type2->is_unsigned ())
    569  1.1.1.6  christos 	return value_from_ulongest (to_type, value_as_long (arg2));
    570  1.1.1.6  christos       else
    571  1.1.1.6  christos 	return value_from_longest (to_type, value_as_long (arg2));
    572      1.1  christos     }
    573      1.1  christos   else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM
    574      1.1  christos 	    || code1 == TYPE_CODE_RANGE)
    575      1.1  christos 	   && (scalar || code2 == TYPE_CODE_PTR
    576      1.1  christos 	       || code2 == TYPE_CODE_MEMBERPTR))
    577      1.1  christos     {
    578      1.1  christos       LONGEST longest;
    579      1.1  christos 
    580      1.1  christos       /* When we cast pointers to integers, we mustn't use
    581  1.1.1.8  christos 	 gdbarch_pointer_to_address to find the address the pointer
    582  1.1.1.8  christos 	 represents, as value_as_long would.  GDB should evaluate
    583  1.1.1.8  christos 	 expressions just as the compiler would --- and the compiler
    584  1.1.1.8  christos 	 sees a cast as a simple reinterpretation of the pointer's
    585  1.1.1.8  christos 	 bits.  */
    586      1.1  christos       if (code2 == TYPE_CODE_PTR)
    587  1.1.1.8  christos 	longest = extract_unsigned_integer
    588  1.1.1.8  christos 		    (value_contents (arg2), type_byte_order (type2));
    589      1.1  christos       else
    590  1.1.1.8  christos 	longest = value_as_long (arg2);
    591  1.1.1.6  christos       return value_from_longest (to_type, convert_to_boolean ?
    592      1.1  christos 				 (LONGEST) (longest ? 1 : 0) : longest);
    593      1.1  christos     }
    594      1.1  christos   else if (code1 == TYPE_CODE_PTR && (code2 == TYPE_CODE_INT
    595      1.1  christos 				      || code2 == TYPE_CODE_ENUM
    596      1.1  christos 				      || code2 == TYPE_CODE_RANGE))
    597      1.1  christos     {
    598  1.1.1.8  christos       /* type->length () is the length of a pointer, but we really
    599      1.1  christos 	 want the length of an address! -- we are really dealing with
    600      1.1  christos 	 addresses (i.e., gdb representations) not pointers (i.e.,
    601      1.1  christos 	 target representations) here.
    602      1.1  christos 
    603      1.1  christos 	 This allows things like "print *(int *)0x01000234" to work
    604      1.1  christos 	 without printing a misleading message -- which would
    605      1.1  christos 	 otherwise occur when dealing with a target having two byte
    606      1.1  christos 	 pointers and four byte addresses.  */
    607      1.1  christos 
    608  1.1.1.8  christos       int addr_bit = gdbarch_addr_bit (type2->arch ());
    609      1.1  christos       LONGEST longest = value_as_long (arg2);
    610      1.1  christos 
    611      1.1  christos       if (addr_bit < sizeof (LONGEST) * HOST_CHAR_BIT)
    612      1.1  christos 	{
    613      1.1  christos 	  if (longest >= ((LONGEST) 1 << addr_bit)
    614      1.1  christos 	      || longest <= -((LONGEST) 1 << addr_bit))
    615      1.1  christos 	    warning (_("value truncated"));
    616      1.1  christos 	}
    617  1.1.1.6  christos       return value_from_longest (to_type, longest);
    618      1.1  christos     }
    619      1.1  christos   else if (code1 == TYPE_CODE_METHODPTR && code2 == TYPE_CODE_INT
    620      1.1  christos 	   && value_as_long (arg2) == 0)
    621      1.1  christos     {
    622  1.1.1.6  christos       struct value *result = allocate_value (to_type);
    623      1.1  christos 
    624  1.1.1.8  christos       cplus_make_method_ptr (to_type,
    625  1.1.1.8  christos 			     value_contents_writeable (result).data (), 0, 0);
    626      1.1  christos       return result;
    627      1.1  christos     }
    628      1.1  christos   else if (code1 == TYPE_CODE_MEMBERPTR && code2 == TYPE_CODE_INT
    629      1.1  christos 	   && value_as_long (arg2) == 0)
    630      1.1  christos     {
    631      1.1  christos       /* The Itanium C++ ABI represents NULL pointers to members as
    632      1.1  christos 	 minus one, instead of biasing the normal case.  */
    633  1.1.1.6  christos       return value_from_longest (to_type, -1);
    634      1.1  christos     }
    635  1.1.1.8  christos   else if (code1 == TYPE_CODE_ARRAY && type->is_vector ()
    636  1.1.1.8  christos 	   && code2 == TYPE_CODE_ARRAY && type2->is_vector ()
    637  1.1.1.8  christos 	   && type->length () != type2->length ())
    638      1.1  christos     error (_("Cannot convert between vector values of different sizes"));
    639  1.1.1.8  christos   else if (code1 == TYPE_CODE_ARRAY && type->is_vector () && scalar
    640  1.1.1.8  christos 	   && type->length () != type2->length ())
    641      1.1  christos     error (_("can only cast scalar to vector of same size"));
    642      1.1  christos   else if (code1 == TYPE_CODE_VOID)
    643      1.1  christos     {
    644  1.1.1.6  christos       return value_zero (to_type, not_lval);
    645      1.1  christos     }
    646  1.1.1.8  christos   else if (type->length () == type2->length ())
    647      1.1  christos     {
    648      1.1  christos       if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
    649  1.1.1.6  christos 	return value_cast_pointers (to_type, arg2, 0);
    650      1.1  christos 
    651      1.1  christos       arg2 = value_copy (arg2);
    652  1.1.1.6  christos       deprecated_set_value_type (arg2, to_type);
    653  1.1.1.6  christos       set_value_enclosing_type (arg2, to_type);
    654      1.1  christos       set_value_pointed_to_offset (arg2, 0);	/* pai: chk_val */
    655      1.1  christos       return arg2;
    656      1.1  christos     }
    657      1.1  christos   else if (VALUE_LVAL (arg2) == lval_memory)
    658  1.1.1.6  christos     return value_at_lazy (to_type, value_address (arg2));
    659      1.1  christos   else
    660      1.1  christos     {
    661  1.1.1.7  christos       if (current_language->la_language == language_ada)
    662  1.1.1.7  christos 	error (_("Invalid type conversion."));
    663      1.1  christos       error (_("Invalid cast."));
    664      1.1  christos     }
    665      1.1  christos }
    666      1.1  christos 
    667      1.1  christos /* The C++ reinterpret_cast operator.  */
    668      1.1  christos 
    669      1.1  christos struct value *
    670      1.1  christos value_reinterpret_cast (struct type *type, struct value *arg)
    671      1.1  christos {
    672      1.1  christos   struct value *result;
    673      1.1  christos   struct type *real_type = check_typedef (type);
    674      1.1  christos   struct type *arg_type, *dest_type;
    675      1.1  christos   int is_ref = 0;
    676      1.1  christos   enum type_code dest_code, arg_code;
    677      1.1  christos 
    678      1.1  christos   /* Do reference, function, and array conversion.  */
    679      1.1  christos   arg = coerce_array (arg);
    680      1.1  christos 
    681      1.1  christos   /* Attempt to preserve the type the user asked for.  */
    682      1.1  christos   dest_type = type;
    683      1.1  christos 
    684      1.1  christos   /* If we are casting to a reference type, transform
    685  1.1.1.5  christos      reinterpret_cast<T&[&]>(V) to *reinterpret_cast<T*>(&V).  */
    686  1.1.1.5  christos   if (TYPE_IS_REFERENCE (real_type))
    687      1.1  christos     {
    688      1.1  christos       is_ref = 1;
    689      1.1  christos       arg = value_addr (arg);
    690  1.1.1.8  christos       dest_type = lookup_pointer_type (dest_type->target_type ());
    691      1.1  christos       real_type = lookup_pointer_type (real_type);
    692      1.1  christos     }
    693      1.1  christos 
    694      1.1  christos   arg_type = value_type (arg);
    695      1.1  christos 
    696  1.1.1.7  christos   dest_code = real_type->code ();
    697  1.1.1.7  christos   arg_code = arg_type->code ();
    698      1.1  christos 
    699      1.1  christos   /* We can convert pointer types, or any pointer type to int, or int
    700      1.1  christos      type to pointer.  */
    701      1.1  christos   if ((dest_code == TYPE_CODE_PTR && arg_code == TYPE_CODE_INT)
    702      1.1  christos       || (dest_code == TYPE_CODE_INT && arg_code == TYPE_CODE_PTR)
    703      1.1  christos       || (dest_code == TYPE_CODE_METHODPTR && arg_code == TYPE_CODE_INT)
    704      1.1  christos       || (dest_code == TYPE_CODE_INT && arg_code == TYPE_CODE_METHODPTR)
    705      1.1  christos       || (dest_code == TYPE_CODE_MEMBERPTR && arg_code == TYPE_CODE_INT)
    706      1.1  christos       || (dest_code == TYPE_CODE_INT && arg_code == TYPE_CODE_MEMBERPTR)
    707      1.1  christos       || (dest_code == arg_code
    708      1.1  christos 	  && (dest_code == TYPE_CODE_PTR
    709      1.1  christos 	      || dest_code == TYPE_CODE_METHODPTR
    710      1.1  christos 	      || dest_code == TYPE_CODE_MEMBERPTR)))
    711      1.1  christos     result = value_cast (dest_type, arg);
    712      1.1  christos   else
    713      1.1  christos     error (_("Invalid reinterpret_cast"));
    714      1.1  christos 
    715      1.1  christos   if (is_ref)
    716  1.1.1.5  christos     result = value_cast (type, value_ref (value_ind (result),
    717  1.1.1.8  christos 					  type->code ()));
    718      1.1  christos 
    719      1.1  christos   return result;
    720      1.1  christos }
    721      1.1  christos 
    722      1.1  christos /* A helper for value_dynamic_cast.  This implements the first of two
    723      1.1  christos    runtime checks: we iterate over all the base classes of the value's
    724      1.1  christos    class which are equal to the desired class; if only one of these
    725      1.1  christos    holds the value, then it is the answer.  */
    726      1.1  christos 
    727      1.1  christos static int
    728      1.1  christos dynamic_cast_check_1 (struct type *desired_type,
    729      1.1  christos 		      const gdb_byte *valaddr,
    730  1.1.1.4  christos 		      LONGEST embedded_offset,
    731      1.1  christos 		      CORE_ADDR address,
    732      1.1  christos 		      struct value *val,
    733      1.1  christos 		      struct type *search_type,
    734      1.1  christos 		      CORE_ADDR arg_addr,
    735      1.1  christos 		      struct type *arg_type,
    736      1.1  christos 		      struct value **result)
    737      1.1  christos {
    738      1.1  christos   int i, result_count = 0;
    739      1.1  christos 
    740      1.1  christos   for (i = 0; i < TYPE_N_BASECLASSES (search_type) && result_count < 2; ++i)
    741      1.1  christos     {
    742  1.1.1.4  christos       LONGEST offset = baseclass_offset (search_type, i, valaddr,
    743  1.1.1.4  christos 					 embedded_offset,
    744  1.1.1.4  christos 					 address, val);
    745      1.1  christos 
    746      1.1  christos       if (class_types_same_p (desired_type, TYPE_BASECLASS (search_type, i)))
    747      1.1  christos 	{
    748      1.1  christos 	  if (address + embedded_offset + offset >= arg_addr
    749  1.1.1.8  christos 	      && address + embedded_offset + offset < arg_addr + arg_type->length ())
    750      1.1  christos 	    {
    751      1.1  christos 	      ++result_count;
    752      1.1  christos 	      if (!*result)
    753      1.1  christos 		*result = value_at_lazy (TYPE_BASECLASS (search_type, i),
    754      1.1  christos 					 address + embedded_offset + offset);
    755      1.1  christos 	    }
    756      1.1  christos 	}
    757      1.1  christos       else
    758      1.1  christos 	result_count += dynamic_cast_check_1 (desired_type,
    759      1.1  christos 					      valaddr,
    760      1.1  christos 					      embedded_offset + offset,
    761      1.1  christos 					      address, val,
    762      1.1  christos 					      TYPE_BASECLASS (search_type, i),
    763      1.1  christos 					      arg_addr,
    764      1.1  christos 					      arg_type,
    765      1.1  christos 					      result);
    766      1.1  christos     }
    767      1.1  christos 
    768      1.1  christos   return result_count;
    769      1.1  christos }
    770      1.1  christos 
    771      1.1  christos /* A helper for value_dynamic_cast.  This implements the second of two
    772      1.1  christos    runtime checks: we look for a unique public sibling class of the
    773      1.1  christos    argument's declared class.  */
    774      1.1  christos 
    775      1.1  christos static int
    776      1.1  christos dynamic_cast_check_2 (struct type *desired_type,
    777      1.1  christos 		      const gdb_byte *valaddr,
    778  1.1.1.4  christos 		      LONGEST embedded_offset,
    779      1.1  christos 		      CORE_ADDR address,
    780      1.1  christos 		      struct value *val,
    781      1.1  christos 		      struct type *search_type,
    782      1.1  christos 		      struct value **result)
    783      1.1  christos {
    784      1.1  christos   int i, result_count = 0;
    785      1.1  christos 
    786      1.1  christos   for (i = 0; i < TYPE_N_BASECLASSES (search_type) && result_count < 2; ++i)
    787      1.1  christos     {
    788  1.1.1.4  christos       LONGEST offset;
    789      1.1  christos 
    790      1.1  christos       if (! BASETYPE_VIA_PUBLIC (search_type, i))
    791      1.1  christos 	continue;
    792      1.1  christos 
    793      1.1  christos       offset = baseclass_offset (search_type, i, valaddr, embedded_offset,
    794      1.1  christos 				 address, val);
    795      1.1  christos       if (class_types_same_p (desired_type, TYPE_BASECLASS (search_type, i)))
    796      1.1  christos 	{
    797      1.1  christos 	  ++result_count;
    798      1.1  christos 	  if (*result == NULL)
    799      1.1  christos 	    *result = value_at_lazy (TYPE_BASECLASS (search_type, i),
    800      1.1  christos 				     address + embedded_offset + offset);
    801      1.1  christos 	}
    802      1.1  christos       else
    803      1.1  christos 	result_count += dynamic_cast_check_2 (desired_type,
    804      1.1  christos 					      valaddr,
    805      1.1  christos 					      embedded_offset + offset,
    806      1.1  christos 					      address, val,
    807      1.1  christos 					      TYPE_BASECLASS (search_type, i),
    808      1.1  christos 					      result);
    809      1.1  christos     }
    810      1.1  christos 
    811      1.1  christos   return result_count;
    812      1.1  christos }
    813      1.1  christos 
    814      1.1  christos /* The C++ dynamic_cast operator.  */
    815      1.1  christos 
    816      1.1  christos struct value *
    817      1.1  christos value_dynamic_cast (struct type *type, struct value *arg)
    818      1.1  christos {
    819  1.1.1.4  christos   int full, using_enc;
    820  1.1.1.4  christos   LONGEST top;
    821      1.1  christos   struct type *resolved_type = check_typedef (type);
    822      1.1  christos   struct type *arg_type = check_typedef (value_type (arg));
    823      1.1  christos   struct type *class_type, *rtti_type;
    824      1.1  christos   struct value *result, *tem, *original_arg = arg;
    825      1.1  christos   CORE_ADDR addr;
    826  1.1.1.5  christos   int is_ref = TYPE_IS_REFERENCE (resolved_type);
    827      1.1  christos 
    828  1.1.1.7  christos   if (resolved_type->code () != TYPE_CODE_PTR
    829  1.1.1.5  christos       && !TYPE_IS_REFERENCE (resolved_type))
    830      1.1  christos     error (_("Argument to dynamic_cast must be a pointer or reference type"));
    831  1.1.1.8  christos   if (resolved_type->target_type ()->code () != TYPE_CODE_VOID
    832  1.1.1.8  christos       && resolved_type->target_type ()->code () != TYPE_CODE_STRUCT)
    833      1.1  christos     error (_("Argument to dynamic_cast must be pointer to class or `void *'"));
    834      1.1  christos 
    835  1.1.1.8  christos   class_type = check_typedef (resolved_type->target_type ());
    836  1.1.1.7  christos   if (resolved_type->code () == TYPE_CODE_PTR)
    837      1.1  christos     {
    838  1.1.1.7  christos       if (arg_type->code () != TYPE_CODE_PTR
    839  1.1.1.7  christos 	  && ! (arg_type->code () == TYPE_CODE_INT
    840      1.1  christos 		&& value_as_long (arg) == 0))
    841      1.1  christos 	error (_("Argument to dynamic_cast does not have pointer type"));
    842  1.1.1.7  christos       if (arg_type->code () == TYPE_CODE_PTR)
    843      1.1  christos 	{
    844  1.1.1.8  christos 	  arg_type = check_typedef (arg_type->target_type ());
    845  1.1.1.7  christos 	  if (arg_type->code () != TYPE_CODE_STRUCT)
    846      1.1  christos 	    error (_("Argument to dynamic_cast does "
    847      1.1  christos 		     "not have pointer to class type"));
    848      1.1  christos 	}
    849      1.1  christos 
    850      1.1  christos       /* Handle NULL pointers.  */
    851      1.1  christos       if (value_as_long (arg) == 0)
    852      1.1  christos 	return value_zero (type, not_lval);
    853      1.1  christos 
    854      1.1  christos       arg = value_ind (arg);
    855      1.1  christos     }
    856      1.1  christos   else
    857      1.1  christos     {
    858  1.1.1.7  christos       if (arg_type->code () != TYPE_CODE_STRUCT)
    859      1.1  christos 	error (_("Argument to dynamic_cast does not have class type"));
    860      1.1  christos     }
    861      1.1  christos 
    862      1.1  christos   /* If the classes are the same, just return the argument.  */
    863      1.1  christos   if (class_types_same_p (class_type, arg_type))
    864      1.1  christos     return value_cast (type, arg);
    865      1.1  christos 
    866      1.1  christos   /* If the target type is a unique base class of the argument's
    867      1.1  christos      declared type, just cast it.  */
    868      1.1  christos   if (is_ancestor (class_type, arg_type))
    869      1.1  christos     {
    870      1.1  christos       if (is_unique_ancestor (class_type, arg))
    871      1.1  christos 	return value_cast (type, original_arg);
    872      1.1  christos       error (_("Ambiguous dynamic_cast"));
    873      1.1  christos     }
    874      1.1  christos 
    875      1.1  christos   rtti_type = value_rtti_type (arg, &full, &top, &using_enc);
    876      1.1  christos   if (! rtti_type)
    877      1.1  christos     error (_("Couldn't determine value's most derived type for dynamic_cast"));
    878      1.1  christos 
    879      1.1  christos   /* Compute the most derived object's address.  */
    880      1.1  christos   addr = value_address (arg);
    881      1.1  christos   if (full)
    882      1.1  christos     {
    883      1.1  christos       /* Done.  */
    884      1.1  christos     }
    885      1.1  christos   else if (using_enc)
    886      1.1  christos     addr += top;
    887      1.1  christos   else
    888      1.1  christos     addr += top + value_embedded_offset (arg);
    889      1.1  christos 
    890      1.1  christos   /* dynamic_cast<void *> means to return a pointer to the
    891      1.1  christos      most-derived object.  */
    892  1.1.1.7  christos   if (resolved_type->code () == TYPE_CODE_PTR
    893  1.1.1.8  christos       && resolved_type->target_type ()->code () == TYPE_CODE_VOID)
    894      1.1  christos     return value_at_lazy (type, addr);
    895      1.1  christos 
    896      1.1  christos   tem = value_at (type, addr);
    897  1.1.1.2  christos   type = value_type (tem);
    898      1.1  christos 
    899      1.1  christos   /* The first dynamic check specified in 5.2.7.  */
    900  1.1.1.8  christos   if (is_public_ancestor (arg_type, resolved_type->target_type ()))
    901      1.1  christos     {
    902  1.1.1.8  christos       if (class_types_same_p (rtti_type, resolved_type->target_type ()))
    903      1.1  christos 	return tem;
    904      1.1  christos       result = NULL;
    905  1.1.1.8  christos       if (dynamic_cast_check_1 (resolved_type->target_type (),
    906  1.1.1.8  christos 				value_contents_for_printing (tem).data (),
    907      1.1  christos 				value_embedded_offset (tem),
    908      1.1  christos 				value_address (tem), tem,
    909      1.1  christos 				rtti_type, addr,
    910      1.1  christos 				arg_type,
    911      1.1  christos 				&result) == 1)
    912      1.1  christos 	return value_cast (type,
    913  1.1.1.5  christos 			   is_ref
    914  1.1.1.7  christos 			   ? value_ref (result, resolved_type->code ())
    915  1.1.1.5  christos 			   : value_addr (result));
    916      1.1  christos     }
    917      1.1  christos 
    918      1.1  christos   /* The second dynamic check specified in 5.2.7.  */
    919      1.1  christos   result = NULL;
    920      1.1  christos   if (is_public_ancestor (arg_type, rtti_type)
    921  1.1.1.8  christos       && dynamic_cast_check_2 (resolved_type->target_type (),
    922  1.1.1.8  christos 			       value_contents_for_printing (tem).data (),
    923      1.1  christos 			       value_embedded_offset (tem),
    924      1.1  christos 			       value_address (tem), tem,
    925      1.1  christos 			       rtti_type, &result) == 1)
    926      1.1  christos     return value_cast (type,
    927  1.1.1.5  christos 		       is_ref
    928  1.1.1.7  christos 		       ? value_ref (result, resolved_type->code ())
    929  1.1.1.5  christos 		       : value_addr (result));
    930      1.1  christos 
    931  1.1.1.7  christos   if (resolved_type->code () == TYPE_CODE_PTR)
    932      1.1  christos     return value_zero (type, not_lval);
    933      1.1  christos 
    934      1.1  christos   error (_("dynamic_cast failed"));
    935      1.1  christos }
    936      1.1  christos 
    937      1.1  christos /* Create a not_lval value of numeric type TYPE that is one, and return it.  */
    938      1.1  christos 
    939      1.1  christos struct value *
    940      1.1  christos value_one (struct type *type)
    941      1.1  christos {
    942      1.1  christos   struct type *type1 = check_typedef (type);
    943      1.1  christos   struct value *val;
    944      1.1  christos 
    945  1.1.1.6  christos   if (is_integral_type (type1) || is_floating_type (type1))
    946      1.1  christos     {
    947      1.1  christos       val = value_from_longest (type, (LONGEST) 1);
    948      1.1  christos     }
    949  1.1.1.8  christos   else if (type1->code () == TYPE_CODE_ARRAY && type1->is_vector ())
    950      1.1  christos     {
    951  1.1.1.8  christos       struct type *eltype = check_typedef (type1->target_type ());
    952      1.1  christos       int i;
    953      1.1  christos       LONGEST low_bound, high_bound;
    954      1.1  christos 
    955      1.1  christos       if (!get_array_bounds (type1, &low_bound, &high_bound))
    956      1.1  christos 	error (_("Could not determine the vector bounds"));
    957      1.1  christos 
    958      1.1  christos       val = allocate_value (type);
    959  1.1.1.8  christos       gdb::array_view<gdb_byte> val_contents = value_contents_writeable (val);
    960  1.1.1.8  christos       int elt_len = eltype->length ();
    961  1.1.1.8  christos 
    962      1.1  christos       for (i = 0; i < high_bound - low_bound + 1; i++)
    963      1.1  christos 	{
    964  1.1.1.8  christos 	  value *tmp = value_one (eltype);
    965  1.1.1.8  christos 	  copy (value_contents_all (tmp),
    966  1.1.1.8  christos 		val_contents.slice (i * elt_len, elt_len));
    967      1.1  christos 	}
    968      1.1  christos     }
    969      1.1  christos   else
    970      1.1  christos     {
    971      1.1  christos       error (_("Not a numeric type."));
    972      1.1  christos     }
    973      1.1  christos 
    974      1.1  christos   /* value_one result is never used for assignments to.  */
    975      1.1  christos   gdb_assert (VALUE_LVAL (val) == not_lval);
    976      1.1  christos 
    977      1.1  christos   return val;
    978      1.1  christos }
    979      1.1  christos 
    980  1.1.1.2  christos /* Helper function for value_at, value_at_lazy, and value_at_lazy_stack.
    981  1.1.1.2  christos    The type of the created value may differ from the passed type TYPE.
    982  1.1.1.2  christos    Make sure to retrieve the returned values's new type after this call
    983  1.1.1.2  christos    e.g. in case the type is a variable length array.  */
    984      1.1  christos 
    985      1.1  christos static struct value *
    986      1.1  christos get_value_at (struct type *type, CORE_ADDR addr, int lazy)
    987      1.1  christos {
    988      1.1  christos   struct value *val;
    989      1.1  christos 
    990  1.1.1.7  christos   if (check_typedef (type)->code () == TYPE_CODE_VOID)
    991      1.1  christos     error (_("Attempt to dereference a generic pointer."));
    992      1.1  christos 
    993      1.1  christos   val = value_from_contents_and_address (type, NULL, addr);
    994      1.1  christos 
    995      1.1  christos   if (!lazy)
    996      1.1  christos     value_fetch_lazy (val);
    997      1.1  christos 
    998      1.1  christos   return val;
    999      1.1  christos }
   1000      1.1  christos 
   1001      1.1  christos /* Return a value with type TYPE located at ADDR.
   1002      1.1  christos 
   1003      1.1  christos    Call value_at only if the data needs to be fetched immediately;
   1004  1.1.1.7  christos    if we can be 'lazy' and defer the fetch, perhaps indefinitely, call
   1005      1.1  christos    value_at_lazy instead.  value_at_lazy simply records the address of
   1006      1.1  christos    the data and sets the lazy-evaluation-required flag.  The lazy flag
   1007      1.1  christos    is tested in the value_contents macro, which is used if and when
   1008  1.1.1.2  christos    the contents are actually required.  The type of the created value
   1009  1.1.1.2  christos    may differ from the passed type TYPE.  Make sure to retrieve the
   1010  1.1.1.2  christos    returned values's new type after this call e.g. in case the type
   1011  1.1.1.2  christos    is a variable length array.
   1012      1.1  christos 
   1013      1.1  christos    Note: value_at does *NOT* handle embedded offsets; perform such
   1014      1.1  christos    adjustments before or after calling it.  */
   1015      1.1  christos 
   1016      1.1  christos struct value *
   1017      1.1  christos value_at (struct type *type, CORE_ADDR addr)
   1018      1.1  christos {
   1019      1.1  christos   return get_value_at (type, addr, 0);
   1020      1.1  christos }
   1021      1.1  christos 
   1022  1.1.1.2  christos /* Return a lazy value with type TYPE located at ADDR (cf. value_at).
   1023  1.1.1.2  christos    The type of the created value may differ from the passed type TYPE.
   1024  1.1.1.2  christos    Make sure to retrieve the returned values's new type after this call
   1025  1.1.1.2  christos    e.g. in case the type is a variable length array.  */
   1026      1.1  christos 
   1027      1.1  christos struct value *
   1028      1.1  christos value_at_lazy (struct type *type, CORE_ADDR addr)
   1029      1.1  christos {
   1030      1.1  christos   return get_value_at (type, addr, 1);
   1031      1.1  christos }
   1032      1.1  christos 
   1033      1.1  christos void
   1034  1.1.1.6  christos read_value_memory (struct value *val, LONGEST bit_offset,
   1035      1.1  christos 		   int stack, CORE_ADDR memaddr,
   1036      1.1  christos 		   gdb_byte *buffer, size_t length)
   1037      1.1  christos {
   1038  1.1.1.4  christos   ULONGEST xfered_total = 0;
   1039  1.1.1.4  christos   struct gdbarch *arch = get_value_arch (val);
   1040  1.1.1.4  christos   int unit_size = gdbarch_addressable_memory_unit_size (arch);
   1041  1.1.1.4  christos   enum target_object object;
   1042  1.1.1.4  christos 
   1043  1.1.1.4  christos   object = stack ? TARGET_OBJECT_STACK_MEMORY : TARGET_OBJECT_MEMORY;
   1044  1.1.1.2  christos 
   1045  1.1.1.4  christos   while (xfered_total < length)
   1046      1.1  christos     {
   1047  1.1.1.2  christos       enum target_xfer_status status;
   1048  1.1.1.4  christos       ULONGEST xfered_partial;
   1049      1.1  christos 
   1050  1.1.1.8  christos       status = target_xfer_partial (current_inferior ()->top_target (),
   1051  1.1.1.4  christos 				    object, NULL,
   1052  1.1.1.4  christos 				    buffer + xfered_total * unit_size, NULL,
   1053  1.1.1.4  christos 				    memaddr + xfered_total,
   1054  1.1.1.4  christos 				    length - xfered_total,
   1055  1.1.1.4  christos 				    &xfered_partial);
   1056  1.1.1.2  christos 
   1057  1.1.1.2  christos       if (status == TARGET_XFER_OK)
   1058  1.1.1.2  christos 	/* nothing */;
   1059  1.1.1.2  christos       else if (status == TARGET_XFER_UNAVAILABLE)
   1060  1.1.1.6  christos 	mark_value_bits_unavailable (val, (xfered_total * HOST_CHAR_BIT
   1061  1.1.1.6  christos 					   + bit_offset),
   1062  1.1.1.6  christos 				     xfered_partial * HOST_CHAR_BIT);
   1063  1.1.1.2  christos       else if (status == TARGET_XFER_EOF)
   1064  1.1.1.4  christos 	memory_error (TARGET_XFER_E_IO, memaddr + xfered_total);
   1065      1.1  christos       else
   1066  1.1.1.4  christos 	memory_error (status, memaddr + xfered_total);
   1067      1.1  christos 
   1068  1.1.1.4  christos       xfered_total += xfered_partial;
   1069  1.1.1.2  christos       QUIT;
   1070      1.1  christos     }
   1071      1.1  christos }
   1072      1.1  christos 
   1073      1.1  christos /* Store the contents of FROMVAL into the location of TOVAL.
   1074      1.1  christos    Return a new value with the location of TOVAL and contents of FROMVAL.  */
   1075      1.1  christos 
   1076      1.1  christos struct value *
   1077      1.1  christos value_assign (struct value *toval, struct value *fromval)
   1078      1.1  christos {
   1079      1.1  christos   struct type *type;
   1080      1.1  christos   struct value *val;
   1081      1.1  christos   struct frame_id old_frame;
   1082      1.1  christos 
   1083      1.1  christos   if (!deprecated_value_modifiable (toval))
   1084      1.1  christos     error (_("Left operand of assignment is not a modifiable lvalue."));
   1085      1.1  christos 
   1086      1.1  christos   toval = coerce_ref (toval);
   1087      1.1  christos 
   1088      1.1  christos   type = value_type (toval);
   1089      1.1  christos   if (VALUE_LVAL (toval) != lval_internalvar)
   1090      1.1  christos     fromval = value_cast (type, fromval);
   1091      1.1  christos   else
   1092      1.1  christos     {
   1093      1.1  christos       /* Coerce arrays and functions to pointers, except for arrays
   1094      1.1  christos 	 which only live in GDB's storage.  */
   1095      1.1  christos       if (!value_must_coerce_to_target (fromval))
   1096      1.1  christos 	fromval = coerce_array (fromval);
   1097      1.1  christos     }
   1098      1.1  christos 
   1099  1.1.1.4  christos   type = check_typedef (type);
   1100      1.1  christos 
   1101      1.1  christos   /* Since modifying a register can trash the frame chain, and
   1102      1.1  christos      modifying memory can trash the frame cache, we save the old frame
   1103      1.1  christos      and then restore the new frame afterwards.  */
   1104      1.1  christos   old_frame = get_frame_id (deprecated_safe_get_selected_frame ());
   1105      1.1  christos 
   1106      1.1  christos   switch (VALUE_LVAL (toval))
   1107      1.1  christos     {
   1108      1.1  christos     case lval_internalvar:
   1109      1.1  christos       set_internalvar (VALUE_INTERNALVAR (toval), fromval);
   1110  1.1.1.8  christos       return value_of_internalvar (type->arch (),
   1111      1.1  christos 				   VALUE_INTERNALVAR (toval));
   1112      1.1  christos 
   1113      1.1  christos     case lval_internalvar_component:
   1114      1.1  christos       {
   1115  1.1.1.4  christos 	LONGEST offset = value_offset (toval);
   1116      1.1  christos 
   1117      1.1  christos 	/* Are we dealing with a bitfield?
   1118      1.1  christos 
   1119      1.1  christos 	   It is important to mention that `value_parent (toval)' is
   1120      1.1  christos 	   non-NULL iff `value_bitsize (toval)' is non-zero.  */
   1121      1.1  christos 	if (value_bitsize (toval))
   1122      1.1  christos 	  {
   1123      1.1  christos 	    /* VALUE_INTERNALVAR below refers to the parent value, while
   1124      1.1  christos 	       the offset is relative to this parent value.  */
   1125      1.1  christos 	    gdb_assert (value_parent (value_parent (toval)) == NULL);
   1126      1.1  christos 	    offset += value_offset (value_parent (toval));
   1127      1.1  christos 	  }
   1128      1.1  christos 
   1129      1.1  christos 	set_internalvar_component (VALUE_INTERNALVAR (toval),
   1130      1.1  christos 				   offset,
   1131      1.1  christos 				   value_bitpos (toval),
   1132      1.1  christos 				   value_bitsize (toval),
   1133      1.1  christos 				   fromval);
   1134      1.1  christos       }
   1135      1.1  christos       break;
   1136      1.1  christos 
   1137      1.1  christos     case lval_memory:
   1138      1.1  christos       {
   1139      1.1  christos 	const gdb_byte *dest_buffer;
   1140      1.1  christos 	CORE_ADDR changed_addr;
   1141      1.1  christos 	int changed_len;
   1142  1.1.1.8  christos 	gdb_byte buffer[sizeof (LONGEST)];
   1143      1.1  christos 
   1144      1.1  christos 	if (value_bitsize (toval))
   1145      1.1  christos 	  {
   1146      1.1  christos 	    struct value *parent = value_parent (toval);
   1147      1.1  christos 
   1148      1.1  christos 	    changed_addr = value_address (parent) + value_offset (toval);
   1149      1.1  christos 	    changed_len = (value_bitpos (toval)
   1150      1.1  christos 			   + value_bitsize (toval)
   1151      1.1  christos 			   + HOST_CHAR_BIT - 1)
   1152      1.1  christos 	      / HOST_CHAR_BIT;
   1153      1.1  christos 
   1154      1.1  christos 	    /* If we can read-modify-write exactly the size of the
   1155      1.1  christos 	       containing type (e.g. short or int) then do so.  This
   1156      1.1  christos 	       is safer for volatile bitfields mapped to hardware
   1157      1.1  christos 	       registers.  */
   1158  1.1.1.8  christos 	    if (changed_len < type->length ()
   1159  1.1.1.8  christos 		&& type->length () <= (int) sizeof (LONGEST)
   1160  1.1.1.8  christos 		&& ((LONGEST) changed_addr % type->length ()) == 0)
   1161  1.1.1.8  christos 	      changed_len = type->length ();
   1162      1.1  christos 
   1163      1.1  christos 	    if (changed_len > (int) sizeof (LONGEST))
   1164      1.1  christos 	      error (_("Can't handle bitfields which "
   1165      1.1  christos 		       "don't fit in a %d bit word."),
   1166      1.1  christos 		     (int) sizeof (LONGEST) * HOST_CHAR_BIT);
   1167      1.1  christos 
   1168      1.1  christos 	    read_memory (changed_addr, buffer, changed_len);
   1169      1.1  christos 	    modify_field (type, buffer, value_as_long (fromval),
   1170      1.1  christos 			  value_bitpos (toval), value_bitsize (toval));
   1171      1.1  christos 	    dest_buffer = buffer;
   1172      1.1  christos 	  }
   1173      1.1  christos 	else
   1174      1.1  christos 	  {
   1175      1.1  christos 	    changed_addr = value_address (toval);
   1176  1.1.1.4  christos 	    changed_len = type_length_units (type);
   1177  1.1.1.8  christos 	    dest_buffer = value_contents (fromval).data ();
   1178      1.1  christos 	  }
   1179      1.1  christos 
   1180      1.1  christos 	write_memory_with_notification (changed_addr, dest_buffer, changed_len);
   1181      1.1  christos       }
   1182      1.1  christos       break;
   1183      1.1  christos 
   1184      1.1  christos     case lval_register:
   1185      1.1  christos       {
   1186  1.1.1.8  christos 	frame_info_ptr frame;
   1187      1.1  christos 	struct gdbarch *gdbarch;
   1188      1.1  christos 	int value_reg;
   1189      1.1  christos 
   1190  1.1.1.8  christos 	/* Figure out which frame this register value is in.  The value
   1191  1.1.1.8  christos 	   holds the frame_id for the next frame, that is the frame this
   1192  1.1.1.8  christos 	   register value was unwound from.
   1193  1.1.1.8  christos 
   1194  1.1.1.8  christos 	   Below we will call put_frame_register_bytes which requires that
   1195  1.1.1.8  christos 	   we pass it the actual frame in which the register value is
   1196  1.1.1.8  christos 	   valid, i.e. not the next frame.  */
   1197  1.1.1.8  christos 	frame = frame_find_by_id (VALUE_NEXT_FRAME_ID (toval));
   1198  1.1.1.8  christos 	frame = get_prev_frame_always (frame);
   1199  1.1.1.5  christos 
   1200      1.1  christos 	value_reg = VALUE_REGNUM (toval);
   1201      1.1  christos 
   1202      1.1  christos 	if (!frame)
   1203      1.1  christos 	  error (_("Value being assigned to is no longer active."));
   1204      1.1  christos 
   1205      1.1  christos 	gdbarch = get_frame_arch (frame);
   1206  1.1.1.2  christos 
   1207  1.1.1.2  christos 	if (value_bitsize (toval))
   1208      1.1  christos 	  {
   1209  1.1.1.2  christos 	    struct value *parent = value_parent (toval);
   1210  1.1.1.4  christos 	    LONGEST offset = value_offset (parent) + value_offset (toval);
   1211  1.1.1.8  christos 	    size_t changed_len;
   1212  1.1.1.2  christos 	    gdb_byte buffer[sizeof (LONGEST)];
   1213  1.1.1.2  christos 	    int optim, unavail;
   1214  1.1.1.2  christos 
   1215  1.1.1.2  christos 	    changed_len = (value_bitpos (toval)
   1216  1.1.1.2  christos 			   + value_bitsize (toval)
   1217  1.1.1.2  christos 			   + HOST_CHAR_BIT - 1)
   1218  1.1.1.2  christos 			  / HOST_CHAR_BIT;
   1219  1.1.1.2  christos 
   1220  1.1.1.8  christos 	    if (changed_len > sizeof (LONGEST))
   1221  1.1.1.2  christos 	      error (_("Can't handle bitfields which "
   1222  1.1.1.2  christos 		       "don't fit in a %d bit word."),
   1223  1.1.1.2  christos 		     (int) sizeof (LONGEST) * HOST_CHAR_BIT);
   1224  1.1.1.2  christos 
   1225  1.1.1.2  christos 	    if (!get_frame_register_bytes (frame, value_reg, offset,
   1226  1.1.1.8  christos 					   {buffer, changed_len},
   1227  1.1.1.2  christos 					   &optim, &unavail))
   1228  1.1.1.2  christos 	      {
   1229  1.1.1.2  christos 		if (optim)
   1230  1.1.1.2  christos 		  throw_error (OPTIMIZED_OUT_ERROR,
   1231  1.1.1.2  christos 			       _("value has been optimized out"));
   1232  1.1.1.2  christos 		if (unavail)
   1233  1.1.1.2  christos 		  throw_error (NOT_AVAILABLE_ERROR,
   1234  1.1.1.2  christos 			       _("value is not available"));
   1235  1.1.1.2  christos 	      }
   1236  1.1.1.2  christos 
   1237  1.1.1.2  christos 	    modify_field (type, buffer, value_as_long (fromval),
   1238  1.1.1.2  christos 			  value_bitpos (toval), value_bitsize (toval));
   1239  1.1.1.2  christos 
   1240  1.1.1.2  christos 	    put_frame_register_bytes (frame, value_reg, offset,
   1241  1.1.1.8  christos 				      {buffer, changed_len});
   1242      1.1  christos 	  }
   1243      1.1  christos 	else
   1244      1.1  christos 	  {
   1245  1.1.1.2  christos 	    if (gdbarch_convert_register_p (gdbarch, VALUE_REGNUM (toval),
   1246  1.1.1.2  christos 					    type))
   1247      1.1  christos 	      {
   1248  1.1.1.2  christos 		/* If TOVAL is a special machine register requiring
   1249  1.1.1.2  christos 		   conversion of program values to a special raw
   1250  1.1.1.2  christos 		   format.  */
   1251  1.1.1.2  christos 		gdbarch_value_to_register (gdbarch, frame,
   1252  1.1.1.2  christos 					   VALUE_REGNUM (toval), type,
   1253  1.1.1.8  christos 					   value_contents (fromval).data ());
   1254      1.1  christos 	      }
   1255      1.1  christos 	    else
   1256  1.1.1.8  christos 	      put_frame_register_bytes (frame, value_reg,
   1257  1.1.1.8  christos 					value_offset (toval),
   1258  1.1.1.8  christos 					value_contents (fromval));
   1259      1.1  christos 	  }
   1260      1.1  christos 
   1261  1.1.1.6  christos 	gdb::observers::register_changed.notify (frame, value_reg);
   1262      1.1  christos 	break;
   1263      1.1  christos       }
   1264      1.1  christos 
   1265      1.1  christos     case lval_computed:
   1266      1.1  christos       {
   1267      1.1  christos 	const struct lval_funcs *funcs = value_computed_funcs (toval);
   1268      1.1  christos 
   1269      1.1  christos 	if (funcs->write != NULL)
   1270      1.1  christos 	  {
   1271      1.1  christos 	    funcs->write (toval, fromval);
   1272      1.1  christos 	    break;
   1273      1.1  christos 	  }
   1274      1.1  christos       }
   1275      1.1  christos       /* Fall through.  */
   1276      1.1  christos 
   1277      1.1  christos     default:
   1278      1.1  christos       error (_("Left operand of assignment is not an lvalue."));
   1279      1.1  christos     }
   1280      1.1  christos 
   1281      1.1  christos   /* Assigning to the stack pointer, frame pointer, and other
   1282      1.1  christos      (architecture and calling convention specific) registers may
   1283      1.1  christos      cause the frame cache and regcache to be out of date.  Assigning to memory
   1284      1.1  christos      also can.  We just do this on all assignments to registers or
   1285      1.1  christos      memory, for simplicity's sake; I doubt the slowdown matters.  */
   1286      1.1  christos   switch (VALUE_LVAL (toval))
   1287      1.1  christos     {
   1288      1.1  christos     case lval_memory:
   1289      1.1  christos     case lval_register:
   1290      1.1  christos     case lval_computed:
   1291      1.1  christos 
   1292  1.1.1.8  christos       gdb::observers::target_changed.notify
   1293  1.1.1.8  christos 	(current_inferior ()->top_target ());
   1294      1.1  christos 
   1295      1.1  christos       /* Having destroyed the frame cache, restore the selected
   1296      1.1  christos 	 frame.  */
   1297      1.1  christos 
   1298      1.1  christos       /* FIXME: cagney/2002-11-02: There has to be a better way of
   1299      1.1  christos 	 doing this.  Instead of constantly saving/restoring the
   1300      1.1  christos 	 frame.  Why not create a get_selected_frame() function that,
   1301      1.1  christos 	 having saved the selected frame's ID can automatically
   1302      1.1  christos 	 re-find the previously selected frame automatically.  */
   1303      1.1  christos 
   1304      1.1  christos       {
   1305  1.1.1.8  christos 	frame_info_ptr fi = frame_find_by_id (old_frame);
   1306      1.1  christos 
   1307      1.1  christos 	if (fi != NULL)
   1308      1.1  christos 	  select_frame (fi);
   1309      1.1  christos       }
   1310      1.1  christos 
   1311      1.1  christos       break;
   1312      1.1  christos     default:
   1313      1.1  christos       break;
   1314      1.1  christos     }
   1315      1.1  christos 
   1316      1.1  christos   /* If the field does not entirely fill a LONGEST, then zero the sign
   1317      1.1  christos      bits.  If the field is signed, and is negative, then sign
   1318      1.1  christos      extend.  */
   1319      1.1  christos   if ((value_bitsize (toval) > 0)
   1320      1.1  christos       && (value_bitsize (toval) < 8 * (int) sizeof (LONGEST)))
   1321      1.1  christos     {
   1322      1.1  christos       LONGEST fieldval = value_as_long (fromval);
   1323      1.1  christos       LONGEST valmask = (((ULONGEST) 1) << value_bitsize (toval)) - 1;
   1324      1.1  christos 
   1325      1.1  christos       fieldval &= valmask;
   1326  1.1.1.8  christos       if (!type->is_unsigned ()
   1327      1.1  christos 	  && (fieldval & (valmask ^ (valmask >> 1))))
   1328      1.1  christos 	fieldval |= ~valmask;
   1329      1.1  christos 
   1330      1.1  christos       fromval = value_from_longest (type, fieldval);
   1331      1.1  christos     }
   1332      1.1  christos 
   1333      1.1  christos   /* The return value is a copy of TOVAL so it shares its location
   1334      1.1  christos      information, but its contents are updated from FROMVAL.  This
   1335      1.1  christos      implies the returned value is not lazy, even if TOVAL was.  */
   1336      1.1  christos   val = value_copy (toval);
   1337      1.1  christos   set_value_lazy (val, 0);
   1338  1.1.1.8  christos   copy (value_contents (fromval), value_contents_raw (val));
   1339      1.1  christos 
   1340      1.1  christos   /* We copy over the enclosing type and pointed-to offset from FROMVAL
   1341      1.1  christos      in the case of pointer types.  For object types, the enclosing type
   1342      1.1  christos      and embedded offset must *not* be copied: the target object refered
   1343      1.1  christos      to by TOVAL retains its original dynamic type after assignment.  */
   1344  1.1.1.7  christos   if (type->code () == TYPE_CODE_PTR)
   1345      1.1  christos     {
   1346      1.1  christos       set_value_enclosing_type (val, value_enclosing_type (fromval));
   1347      1.1  christos       set_value_pointed_to_offset (val, value_pointed_to_offset (fromval));
   1348      1.1  christos     }
   1349      1.1  christos 
   1350      1.1  christos   return val;
   1351      1.1  christos }
   1352      1.1  christos 
   1353  1.1.1.8  christos /* Extend a value ARG1 to COUNT repetitions of its type.  */
   1354      1.1  christos 
   1355      1.1  christos struct value *
   1356      1.1  christos value_repeat (struct value *arg1, int count)
   1357      1.1  christos {
   1358      1.1  christos   struct value *val;
   1359      1.1  christos 
   1360      1.1  christos   if (VALUE_LVAL (arg1) != lval_memory)
   1361      1.1  christos     error (_("Only values in memory can be extended with '@'."));
   1362      1.1  christos   if (count < 1)
   1363      1.1  christos     error (_("Invalid number %d of repetitions."), count);
   1364      1.1  christos 
   1365      1.1  christos   val = allocate_repeat_value (value_enclosing_type (arg1), count);
   1366      1.1  christos 
   1367      1.1  christos   VALUE_LVAL (val) = lval_memory;
   1368      1.1  christos   set_value_address (val, value_address (arg1));
   1369      1.1  christos 
   1370      1.1  christos   read_value_memory (val, 0, value_stack (val), value_address (val),
   1371  1.1.1.8  christos 		     value_contents_all_raw (val).data (),
   1372  1.1.1.4  christos 		     type_length_units (value_enclosing_type (val)));
   1373      1.1  christos 
   1374      1.1  christos   return val;
   1375      1.1  christos }
   1376      1.1  christos 
   1377      1.1  christos struct value *
   1378      1.1  christos value_of_variable (struct symbol *var, const struct block *b)
   1379      1.1  christos {
   1380  1.1.1.8  christos   frame_info_ptr frame = NULL;
   1381      1.1  christos 
   1382  1.1.1.4  christos   if (symbol_read_needs_frame (var))
   1383      1.1  christos     frame = get_selected_frame (_("No frame selected."));
   1384      1.1  christos 
   1385  1.1.1.4  christos   return read_var_value (var, b, frame);
   1386      1.1  christos }
   1387      1.1  christos 
   1388      1.1  christos struct value *
   1389      1.1  christos address_of_variable (struct symbol *var, const struct block *b)
   1390      1.1  christos {
   1391  1.1.1.8  christos   struct type *type = var->type ();
   1392      1.1  christos   struct value *val;
   1393      1.1  christos 
   1394      1.1  christos   /* Evaluate it first; if the result is a memory address, we're fine.
   1395      1.1  christos      Lazy evaluation pays off here.  */
   1396      1.1  christos 
   1397      1.1  christos   val = value_of_variable (var, b);
   1398  1.1.1.2  christos   type = value_type (val);
   1399      1.1  christos 
   1400      1.1  christos   if ((VALUE_LVAL (val) == lval_memory && value_lazy (val))
   1401  1.1.1.7  christos       || type->code () == TYPE_CODE_FUNC)
   1402      1.1  christos     {
   1403      1.1  christos       CORE_ADDR addr = value_address (val);
   1404      1.1  christos 
   1405      1.1  christos       return value_from_pointer (lookup_pointer_type (type), addr);
   1406      1.1  christos     }
   1407      1.1  christos 
   1408      1.1  christos   /* Not a memory address; check what the problem was.  */
   1409      1.1  christos   switch (VALUE_LVAL (val))
   1410      1.1  christos     {
   1411      1.1  christos     case lval_register:
   1412      1.1  christos       {
   1413  1.1.1.8  christos 	frame_info_ptr frame;
   1414      1.1  christos 	const char *regname;
   1415      1.1  christos 
   1416  1.1.1.5  christos 	frame = frame_find_by_id (VALUE_NEXT_FRAME_ID (val));
   1417      1.1  christos 	gdb_assert (frame);
   1418      1.1  christos 
   1419      1.1  christos 	regname = gdbarch_register_name (get_frame_arch (frame),
   1420      1.1  christos 					 VALUE_REGNUM (val));
   1421  1.1.1.8  christos 	gdb_assert (regname != nullptr && *regname != '\0');
   1422      1.1  christos 
   1423      1.1  christos 	error (_("Address requested for identifier "
   1424      1.1  christos 		 "\"%s\" which is in register $%s"),
   1425  1.1.1.7  christos 	       var->print_name (), regname);
   1426      1.1  christos 	break;
   1427      1.1  christos       }
   1428      1.1  christos 
   1429      1.1  christos     default:
   1430      1.1  christos       error (_("Can't take address of \"%s\" which isn't an lvalue."),
   1431  1.1.1.7  christos 	     var->print_name ());
   1432      1.1  christos       break;
   1433      1.1  christos     }
   1434      1.1  christos 
   1435      1.1  christos   return val;
   1436      1.1  christos }
   1437      1.1  christos 
   1438  1.1.1.7  christos /* See value.h.  */
   1439      1.1  christos 
   1440  1.1.1.7  christos bool
   1441      1.1  christos value_must_coerce_to_target (struct value *val)
   1442      1.1  christos {
   1443      1.1  christos   struct type *valtype;
   1444      1.1  christos 
   1445      1.1  christos   /* The only lval kinds which do not live in target memory.  */
   1446      1.1  christos   if (VALUE_LVAL (val) != not_lval
   1447  1.1.1.2  christos       && VALUE_LVAL (val) != lval_internalvar
   1448  1.1.1.2  christos       && VALUE_LVAL (val) != lval_xcallable)
   1449  1.1.1.7  christos     return false;
   1450      1.1  christos 
   1451      1.1  christos   valtype = check_typedef (value_type (val));
   1452      1.1  christos 
   1453  1.1.1.7  christos   switch (valtype->code ())
   1454      1.1  christos     {
   1455      1.1  christos     case TYPE_CODE_ARRAY:
   1456  1.1.1.8  christos       return valtype->is_vector () ? 0 : 1;
   1457      1.1  christos     case TYPE_CODE_STRING:
   1458  1.1.1.7  christos       return true;
   1459      1.1  christos     default:
   1460  1.1.1.7  christos       return false;
   1461      1.1  christos     }
   1462      1.1  christos }
   1463      1.1  christos 
   1464      1.1  christos /* Make sure that VAL lives in target memory if it's supposed to.  For
   1465      1.1  christos    instance, strings are constructed as character arrays in GDB's
   1466      1.1  christos    storage, and this function copies them to the target.  */
   1467      1.1  christos 
   1468      1.1  christos struct value *
   1469      1.1  christos value_coerce_to_target (struct value *val)
   1470      1.1  christos {
   1471      1.1  christos   LONGEST length;
   1472      1.1  christos   CORE_ADDR addr;
   1473      1.1  christos 
   1474      1.1  christos   if (!value_must_coerce_to_target (val))
   1475      1.1  christos     return val;
   1476      1.1  christos 
   1477  1.1.1.8  christos   length = check_typedef (value_type (val))->length ();
   1478      1.1  christos   addr = allocate_space_in_inferior (length);
   1479  1.1.1.8  christos   write_memory (addr, value_contents (val).data (), length);
   1480      1.1  christos   return value_at_lazy (value_type (val), addr);
   1481      1.1  christos }
   1482      1.1  christos 
   1483      1.1  christos /* Given a value which is an array, return a value which is a pointer
   1484      1.1  christos    to its first element, regardless of whether or not the array has a
   1485      1.1  christos    nonzero lower bound.
   1486      1.1  christos 
   1487      1.1  christos    FIXME: A previous comment here indicated that this routine should
   1488      1.1  christos    be substracting the array's lower bound.  It's not clear to me that
   1489      1.1  christos    this is correct.  Given an array subscripting operation, it would
   1490      1.1  christos    certainly work to do the adjustment here, essentially computing:
   1491      1.1  christos 
   1492      1.1  christos    (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0])
   1493      1.1  christos 
   1494      1.1  christos    However I believe a more appropriate and logical place to account
   1495      1.1  christos    for the lower bound is to do so in value_subscript, essentially
   1496      1.1  christos    computing:
   1497      1.1  christos 
   1498      1.1  christos    (&array[0] + ((index - lowerbound) * sizeof array[0]))
   1499      1.1  christos 
   1500      1.1  christos    As further evidence consider what would happen with operations
   1501      1.1  christos    other than array subscripting, where the caller would get back a
   1502      1.1  christos    value that had an address somewhere before the actual first element
   1503      1.1  christos    of the array, and the information about the lower bound would be
   1504      1.1  christos    lost because of the coercion to pointer type.  */
   1505      1.1  christos 
   1506      1.1  christos struct value *
   1507      1.1  christos value_coerce_array (struct value *arg1)
   1508      1.1  christos {
   1509      1.1  christos   struct type *type = check_typedef (value_type (arg1));
   1510      1.1  christos 
   1511      1.1  christos   /* If the user tries to do something requiring a pointer with an
   1512      1.1  christos      array that has not yet been pushed to the target, then this would
   1513      1.1  christos      be a good time to do so.  */
   1514      1.1  christos   arg1 = value_coerce_to_target (arg1);
   1515      1.1  christos 
   1516      1.1  christos   if (VALUE_LVAL (arg1) != lval_memory)
   1517      1.1  christos     error (_("Attempt to take address of value not located in memory."));
   1518      1.1  christos 
   1519  1.1.1.8  christos   return value_from_pointer (lookup_pointer_type (type->target_type ()),
   1520      1.1  christos 			     value_address (arg1));
   1521      1.1  christos }
   1522      1.1  christos 
   1523      1.1  christos /* Given a value which is a function, return a value which is a pointer
   1524      1.1  christos    to it.  */
   1525      1.1  christos 
   1526      1.1  christos struct value *
   1527      1.1  christos value_coerce_function (struct value *arg1)
   1528      1.1  christos {
   1529      1.1  christos   struct value *retval;
   1530      1.1  christos 
   1531      1.1  christos   if (VALUE_LVAL (arg1) != lval_memory)
   1532      1.1  christos     error (_("Attempt to take address of value not located in memory."));
   1533      1.1  christos 
   1534      1.1  christos   retval = value_from_pointer (lookup_pointer_type (value_type (arg1)),
   1535      1.1  christos 			       value_address (arg1));
   1536      1.1  christos   return retval;
   1537      1.1  christos }
   1538      1.1  christos 
   1539      1.1  christos /* Return a pointer value for the object for which ARG1 is the
   1540      1.1  christos    contents.  */
   1541      1.1  christos 
   1542      1.1  christos struct value *
   1543      1.1  christos value_addr (struct value *arg1)
   1544      1.1  christos {
   1545      1.1  christos   struct value *arg2;
   1546      1.1  christos   struct type *type = check_typedef (value_type (arg1));
   1547      1.1  christos 
   1548  1.1.1.5  christos   if (TYPE_IS_REFERENCE (type))
   1549      1.1  christos     {
   1550  1.1.1.4  christos       if (value_bits_synthetic_pointer (arg1, value_embedded_offset (arg1),
   1551  1.1.1.8  christos 	  TARGET_CHAR_BIT * type->length ()))
   1552  1.1.1.4  christos 	arg1 = coerce_ref (arg1);
   1553  1.1.1.4  christos       else
   1554  1.1.1.4  christos 	{
   1555  1.1.1.4  christos 	  /* Copy the value, but change the type from (T&) to (T*).  We
   1556  1.1.1.4  christos 	     keep the same location information, which is efficient, and
   1557  1.1.1.4  christos 	     allows &(&X) to get the location containing the reference.
   1558  1.1.1.4  christos 	     Do the same to its enclosing type for consistency.  */
   1559  1.1.1.4  christos 	  struct type *type_ptr
   1560  1.1.1.8  christos 	    = lookup_pointer_type (type->target_type ());
   1561  1.1.1.4  christos 	  struct type *enclosing_type
   1562  1.1.1.4  christos 	    = check_typedef (value_enclosing_type (arg1));
   1563  1.1.1.4  christos 	  struct type *enclosing_type_ptr
   1564  1.1.1.8  christos 	    = lookup_pointer_type (enclosing_type->target_type ());
   1565  1.1.1.4  christos 
   1566  1.1.1.4  christos 	  arg2 = value_copy (arg1);
   1567  1.1.1.4  christos 	  deprecated_set_value_type (arg2, type_ptr);
   1568  1.1.1.4  christos 	  set_value_enclosing_type (arg2, enclosing_type_ptr);
   1569  1.1.1.4  christos 
   1570  1.1.1.4  christos 	  return arg2;
   1571  1.1.1.4  christos 	}
   1572      1.1  christos     }
   1573  1.1.1.7  christos   if (type->code () == TYPE_CODE_FUNC)
   1574      1.1  christos     return value_coerce_function (arg1);
   1575      1.1  christos 
   1576      1.1  christos   /* If this is an array that has not yet been pushed to the target,
   1577      1.1  christos      then this would be a good time to force it to memory.  */
   1578      1.1  christos   arg1 = value_coerce_to_target (arg1);
   1579      1.1  christos 
   1580      1.1  christos   if (VALUE_LVAL (arg1) != lval_memory)
   1581      1.1  christos     error (_("Attempt to take address of value not located in memory."));
   1582      1.1  christos 
   1583      1.1  christos   /* Get target memory address.  */
   1584      1.1  christos   arg2 = value_from_pointer (lookup_pointer_type (value_type (arg1)),
   1585      1.1  christos 			     (value_address (arg1)
   1586      1.1  christos 			      + value_embedded_offset (arg1)));
   1587      1.1  christos 
   1588      1.1  christos   /* This may be a pointer to a base subobject; so remember the
   1589      1.1  christos      full derived object's type ...  */
   1590      1.1  christos   set_value_enclosing_type (arg2,
   1591      1.1  christos 			    lookup_pointer_type (value_enclosing_type (arg1)));
   1592      1.1  christos   /* ... and also the relative position of the subobject in the full
   1593      1.1  christos      object.  */
   1594      1.1  christos   set_value_pointed_to_offset (arg2, value_embedded_offset (arg1));
   1595      1.1  christos   return arg2;
   1596      1.1  christos }
   1597      1.1  christos 
   1598      1.1  christos /* Return a reference value for the object for which ARG1 is the
   1599      1.1  christos    contents.  */
   1600      1.1  christos 
   1601      1.1  christos struct value *
   1602  1.1.1.5  christos value_ref (struct value *arg1, enum type_code refcode)
   1603      1.1  christos {
   1604      1.1  christos   struct value *arg2;
   1605      1.1  christos   struct type *type = check_typedef (value_type (arg1));
   1606      1.1  christos 
   1607  1.1.1.5  christos   gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
   1608  1.1.1.5  christos 
   1609  1.1.1.7  christos   if ((type->code () == TYPE_CODE_REF
   1610  1.1.1.7  christos        || type->code () == TYPE_CODE_RVALUE_REF)
   1611  1.1.1.7  christos       && type->code () == refcode)
   1612      1.1  christos     return arg1;
   1613      1.1  christos 
   1614      1.1  christos   arg2 = value_addr (arg1);
   1615  1.1.1.5  christos   deprecated_set_value_type (arg2, lookup_reference_type (type, refcode));
   1616      1.1  christos   return arg2;
   1617      1.1  christos }
   1618      1.1  christos 
   1619      1.1  christos /* Given a value of a pointer type, apply the C unary * operator to
   1620      1.1  christos    it.  */
   1621      1.1  christos 
   1622      1.1  christos struct value *
   1623      1.1  christos value_ind (struct value *arg1)
   1624      1.1  christos {
   1625      1.1  christos   struct type *base_type;
   1626      1.1  christos   struct value *arg2;
   1627      1.1  christos 
   1628      1.1  christos   arg1 = coerce_array (arg1);
   1629      1.1  christos 
   1630      1.1  christos   base_type = check_typedef (value_type (arg1));
   1631      1.1  christos 
   1632      1.1  christos   if (VALUE_LVAL (arg1) == lval_computed)
   1633      1.1  christos     {
   1634      1.1  christos       const struct lval_funcs *funcs = value_computed_funcs (arg1);
   1635      1.1  christos 
   1636      1.1  christos       if (funcs->indirect)
   1637      1.1  christos 	{
   1638      1.1  christos 	  struct value *result = funcs->indirect (arg1);
   1639      1.1  christos 
   1640      1.1  christos 	  if (result)
   1641      1.1  christos 	    return result;
   1642      1.1  christos 	}
   1643      1.1  christos     }
   1644      1.1  christos 
   1645  1.1.1.7  christos   if (base_type->code () == TYPE_CODE_PTR)
   1646      1.1  christos     {
   1647      1.1  christos       struct type *enc_type;
   1648      1.1  christos 
   1649      1.1  christos       /* We may be pointing to something embedded in a larger object.
   1650  1.1.1.8  christos 	 Get the real type of the enclosing object.  */
   1651      1.1  christos       enc_type = check_typedef (value_enclosing_type (arg1));
   1652  1.1.1.8  christos       enc_type = enc_type->target_type ();
   1653      1.1  christos 
   1654  1.1.1.7  christos       CORE_ADDR base_addr;
   1655  1.1.1.7  christos       if (check_typedef (enc_type)->code () == TYPE_CODE_FUNC
   1656  1.1.1.7  christos 	  || check_typedef (enc_type)->code () == TYPE_CODE_METHOD)
   1657  1.1.1.7  christos 	{
   1658  1.1.1.7  christos 	  /* For functions, go through find_function_addr, which knows
   1659  1.1.1.7  christos 	     how to handle function descriptors.  */
   1660  1.1.1.7  christos 	  base_addr = find_function_addr (arg1, NULL);
   1661  1.1.1.7  christos 	}
   1662  1.1.1.7  christos       else
   1663  1.1.1.7  christos 	{
   1664  1.1.1.7  christos 	  /* Retrieve the enclosing object pointed to.  */
   1665  1.1.1.7  christos 	  base_addr = (value_as_address (arg1)
   1666  1.1.1.7  christos 		       - value_pointed_to_offset (arg1));
   1667  1.1.1.7  christos 	}
   1668  1.1.1.7  christos       arg2 = value_at_lazy (enc_type, base_addr);
   1669  1.1.1.2  christos       enc_type = value_type (arg2);
   1670  1.1.1.7  christos       return readjust_indirect_value_type (arg2, enc_type, base_type,
   1671  1.1.1.7  christos 					   arg1, base_addr);
   1672      1.1  christos     }
   1673      1.1  christos 
   1674      1.1  christos   error (_("Attempt to take contents of a non-pointer value."));
   1675      1.1  christos }
   1676      1.1  christos 
   1677      1.1  christos /* Create a value for an array by allocating space in GDB, copying the
   1679      1.1  christos    data into that space, and then setting up an array value.
   1680      1.1  christos 
   1681      1.1  christos    The array bounds are set from LOWBOUND and HIGHBOUND, and the array
   1682      1.1  christos    is populated from the values passed in ELEMVEC.
   1683      1.1  christos 
   1684      1.1  christos    The element type of the array is inherited from the type of the
   1685      1.1  christos    first element, and all elements must have the same size (though we
   1686      1.1  christos    don't currently enforce any restriction on their types).  */
   1687      1.1  christos 
   1688      1.1  christos struct value *
   1689      1.1  christos value_array (int lowbound, int highbound, struct value **elemvec)
   1690      1.1  christos {
   1691      1.1  christos   int nelem;
   1692  1.1.1.4  christos   int idx;
   1693      1.1  christos   ULONGEST typelength;
   1694      1.1  christos   struct value *val;
   1695      1.1  christos   struct type *arraytype;
   1696      1.1  christos 
   1697      1.1  christos   /* Validate that the bounds are reasonable and that each of the
   1698      1.1  christos      elements have the same size.  */
   1699      1.1  christos 
   1700      1.1  christos   nelem = highbound - lowbound + 1;
   1701      1.1  christos   if (nelem <= 0)
   1702      1.1  christos     {
   1703      1.1  christos       error (_("bad array bounds (%d, %d)"), lowbound, highbound);
   1704  1.1.1.4  christos     }
   1705      1.1  christos   typelength = type_length_units (value_enclosing_type (elemvec[0]));
   1706      1.1  christos   for (idx = 1; idx < nelem; idx++)
   1707  1.1.1.4  christos     {
   1708  1.1.1.4  christos       if (type_length_units (value_enclosing_type (elemvec[idx]))
   1709      1.1  christos 	  != typelength)
   1710      1.1  christos 	{
   1711      1.1  christos 	  error (_("array elements must all be the same size"));
   1712      1.1  christos 	}
   1713      1.1  christos     }
   1714      1.1  christos 
   1715      1.1  christos   arraytype = lookup_array_range_type (value_enclosing_type (elemvec[0]),
   1716      1.1  christos 				       lowbound, highbound);
   1717  1.1.1.8  christos 
   1718      1.1  christos   if (!current_language->c_style_arrays_p ())
   1719      1.1  christos     {
   1720      1.1  christos       val = allocate_value (arraytype);
   1721      1.1  christos       for (idx = 0; idx < nelem; idx++)
   1722      1.1  christos 	value_contents_copy (val, idx * typelength, elemvec[idx], 0,
   1723      1.1  christos 			     typelength);
   1724      1.1  christos       return val;
   1725      1.1  christos     }
   1726      1.1  christos 
   1727      1.1  christos   /* Allocate space to store the array, and then initialize it by
   1728      1.1  christos      copying in each element.  */
   1729      1.1  christos 
   1730      1.1  christos   val = allocate_value (arraytype);
   1731      1.1  christos   for (idx = 0; idx < nelem; idx++)
   1732      1.1  christos     value_contents_copy (val, idx * typelength, elemvec[idx], 0, typelength);
   1733      1.1  christos   return val;
   1734      1.1  christos }
   1735      1.1  christos 
   1736  1.1.1.4  christos struct value *
   1737      1.1  christos value_cstring (const char *ptr, ssize_t len, struct type *char_type)
   1738      1.1  christos {
   1739  1.1.1.8  christos   struct value *val;
   1740  1.1.1.8  christos   int lowbound = current_language->string_lower_bound ();
   1741      1.1  christos   ssize_t highbound = len / char_type->length ();
   1742      1.1  christos   struct type *stringtype
   1743      1.1  christos     = lookup_array_range_type (char_type, lowbound, highbound + lowbound - 1);
   1744      1.1  christos 
   1745  1.1.1.8  christos   val = allocate_value (stringtype);
   1746      1.1  christos   memcpy (value_contents_raw (val).data (), ptr, len);
   1747      1.1  christos   return val;
   1748      1.1  christos }
   1749      1.1  christos 
   1750      1.1  christos /* Create a value for a string constant by allocating space in the
   1751      1.1  christos    inferior, copying the data into that space, and returning the
   1752      1.1  christos    address with type TYPE_CODE_STRING.  PTR points to the string
   1753      1.1  christos    constant data; LEN is number of characters.
   1754      1.1  christos 
   1755      1.1  christos    Note that string types are like array of char types with a lower
   1756      1.1  christos    bound of zero and an upper bound of LEN - 1.  Also note that the
   1757      1.1  christos    string may contain embedded null bytes.  */
   1758      1.1  christos 
   1759  1.1.1.4  christos struct value *
   1760      1.1  christos value_string (const char *ptr, ssize_t len, struct type *char_type)
   1761      1.1  christos {
   1762  1.1.1.8  christos   struct value *val;
   1763  1.1.1.8  christos   int lowbound = current_language->string_lower_bound ();
   1764      1.1  christos   ssize_t highbound = len / char_type->length ();
   1765      1.1  christos   struct type *stringtype
   1766      1.1  christos     = lookup_string_range_type (char_type, lowbound, highbound + lowbound - 1);
   1767      1.1  christos 
   1768  1.1.1.8  christos   val = allocate_value (stringtype);
   1769      1.1  christos   memcpy (value_contents_raw (val).data (), ptr, len);
   1770      1.1  christos   return val;
   1771      1.1  christos }
   1772      1.1  christos 
   1773  1.1.1.8  christos 
   1774  1.1.1.8  christos /* See if we can pass arguments in T2 to a function which takes arguments
   1776  1.1.1.8  christos    of types T1.  T1 is a list of NARGS arguments, and T2 is an array_view
   1777      1.1  christos    of the values we're trying to pass.  If some arguments need coercion of
   1778      1.1  christos    some sort, then the coerced values are written into T2.  Return value is
   1779      1.1  christos    0 if the arguments could be matched, or the position at which they
   1780      1.1  christos    differ if not.
   1781  1.1.1.8  christos 
   1782      1.1  christos    STATICP is nonzero if the T1 argument list came from a static
   1783      1.1  christos    member function.  T2 must still include the ``this'' pointer, but
   1784      1.1  christos    it will be skipped.
   1785      1.1  christos 
   1786      1.1  christos    For non-static member functions, we ignore the first argument,
   1787      1.1  christos    which is the type of the instance variable.  This is because we
   1788      1.1  christos    want to handle calls with objects from derived classes.  This is
   1789      1.1  christos    not entirely correct: we should actually check to make sure that a
   1790      1.1  christos    requested operation is type secure, shouldn't we?  FIXME.  */
   1791  1.1.1.8  christos 
   1792  1.1.1.8  christos static int
   1793      1.1  christos typecmp (bool staticp, bool varargs, int nargs,
   1794      1.1  christos 	 struct field t1[], gdb::array_view<value *> t2)
   1795      1.1  christos {
   1796      1.1  christos   int i;
   1797      1.1  christos 
   1798      1.1  christos   /* Skip ``this'' argument if applicable.  T2 will always include
   1799  1.1.1.8  christos      THIS.  */
   1800      1.1  christos   if (staticp)
   1801      1.1  christos     t2 = t2.slice (1);
   1802  1.1.1.7  christos 
   1803      1.1  christos   for (i = 0;
   1804      1.1  christos        (i < nargs) && t1[i].type ()->code () != TYPE_CODE_VOID;
   1805      1.1  christos        i++)
   1806      1.1  christos     {
   1807  1.1.1.8  christos       struct type *tt1, *tt2;
   1808      1.1  christos 
   1809      1.1  christos       if (i == t2.size ())
   1810  1.1.1.7  christos 	return i + 1;
   1811      1.1  christos 
   1812      1.1  christos       tt1 = check_typedef (t1[i].type ());
   1813  1.1.1.5  christos       tt2 = check_typedef (value_type (t2[i]));
   1814  1.1.1.2  christos 
   1815  1.1.1.8  christos       if (TYPE_IS_REFERENCE (tt1)
   1816  1.1.1.7  christos 	  /* We should be doing hairy argument matching, as below.  */
   1817      1.1  christos 	  && (check_typedef (tt1->target_type ())->code ()
   1818  1.1.1.7  christos 	      == tt2->code ()))
   1819      1.1  christos 	{
   1820      1.1  christos 	  if (tt2->code () == TYPE_CODE_ARRAY)
   1821  1.1.1.7  christos 	    t2[i] = value_coerce_array (t2[i]);
   1822      1.1  christos 	  else
   1823      1.1  christos 	    t2[i] = value_ref (t2[i], tt1->code ());
   1824      1.1  christos 	  continue;
   1825      1.1  christos 	}
   1826      1.1  christos 
   1827      1.1  christos       /* djb - 20000715 - Until the new type structure is in the
   1828      1.1  christos 	 place, and we can attempt things like implicit conversions,
   1829      1.1  christos 	 we need to do this so you can take something like a map<const
   1830      1.1  christos 	 char *>, and properly access map["hello"], because the
   1831  1.1.1.7  christos 	 argument to [] will be a reference to a pointer to a char,
   1832      1.1  christos 	 and the argument will be a pointer to a char.  */
   1833  1.1.1.8  christos       while (TYPE_IS_REFERENCE (tt1) || tt1->code () == TYPE_CODE_PTR)
   1834      1.1  christos 	{
   1835  1.1.1.7  christos 	  tt1 = check_typedef ( tt1->target_type () );
   1836  1.1.1.7  christos 	}
   1837  1.1.1.5  christos       while (tt2->code () == TYPE_CODE_ARRAY
   1838      1.1  christos 	     || tt2->code () == TYPE_CODE_PTR
   1839  1.1.1.8  christos 	     || TYPE_IS_REFERENCE (tt2))
   1840      1.1  christos 	{
   1841  1.1.1.7  christos 	  tt2 = check_typedef (tt2->target_type ());
   1842      1.1  christos 	}
   1843      1.1  christos       if (tt1->code () == tt2->code ())
   1844      1.1  christos 	continue;
   1845      1.1  christos       /* Array to pointer is a `trivial conversion' according to the
   1846      1.1  christos 	 ARM.  */
   1847  1.1.1.8  christos 
   1848  1.1.1.8  christos       /* We should be doing much hairier argument matching (see
   1849  1.1.1.7  christos 	 section 13.2 of the ARM), but as a quick kludge, just check
   1850      1.1  christos 	 for the same type code.  */
   1851      1.1  christos       if (t1[i].type ()->code () != value_type (t2[i])->code ())
   1852  1.1.1.8  christos 	return i + 1;
   1853      1.1  christos     }
   1854      1.1  christos   if (varargs || i == t2.size ())
   1855      1.1  christos     return 0;
   1856      1.1  christos   return i + 1;
   1857  1.1.1.8  christos }
   1858  1.1.1.8  christos 
   1859  1.1.1.8  christos /* Helper class for search_struct_field that keeps track of found
   1860  1.1.1.8  christos    results and possibly throws an exception if the search yields
   1861      1.1  christos    ambiguous results.  See search_struct_field for description of
   1862  1.1.1.8  christos    LOOKING_FOR_BASECLASS.  */
   1863  1.1.1.8  christos 
   1864  1.1.1.8  christos struct struct_field_searcher
   1865  1.1.1.8  christos {
   1866  1.1.1.8  christos   /* A found field.  */
   1867  1.1.1.8  christos   struct found_field
   1868  1.1.1.8  christos   {
   1869  1.1.1.8  christos     /* Path to the structure where the field was found.  */
   1870  1.1.1.8  christos     std::vector<struct type *> path;
   1871  1.1.1.8  christos 
   1872  1.1.1.8  christos     /* The field found.  */
   1873  1.1.1.8  christos     struct value *field_value;
   1874  1.1.1.8  christos   };
   1875  1.1.1.8  christos 
   1876  1.1.1.8  christos   /* See corresponding fields for description of parameters.  */
   1877  1.1.1.8  christos   struct_field_searcher (const char *name,
   1878  1.1.1.8  christos 			 struct type *outermost_type,
   1879  1.1.1.8  christos 			 bool looking_for_baseclass)
   1880  1.1.1.8  christos     : m_name (name),
   1881  1.1.1.8  christos       m_looking_for_baseclass (looking_for_baseclass),
   1882  1.1.1.8  christos       m_outermost_type (outermost_type)
   1883  1.1.1.8  christos   {
   1884  1.1.1.8  christos   }
   1885  1.1.1.8  christos 
   1886  1.1.1.8  christos   /* The search entry point.  If LOOKING_FOR_BASECLASS is true and the
   1887  1.1.1.8  christos      base class search yields ambiguous results, this throws an
   1888  1.1.1.8  christos      exception.  If LOOKING_FOR_BASECLASS is false, the found fields
   1889  1.1.1.8  christos      are accumulated and the caller (search_struct_field) takes care
   1890  1.1.1.8  christos      of throwing an error if the field search yields ambiguous
   1891  1.1.1.8  christos      results.  The latter is done that way so that the error message
   1892  1.1.1.8  christos      can include a list of all the found candidates.  */
   1893  1.1.1.8  christos   void search (struct value *arg, LONGEST offset, struct type *type);
   1894  1.1.1.8  christos 
   1895  1.1.1.8  christos   const std::vector<found_field> &fields ()
   1896  1.1.1.8  christos   {
   1897  1.1.1.8  christos     return m_fields;
   1898  1.1.1.8  christos   }
   1899  1.1.1.8  christos 
   1900  1.1.1.8  christos   struct value *baseclass ()
   1901  1.1.1.8  christos   {
   1902  1.1.1.8  christos     return m_baseclass;
   1903  1.1.1.8  christos   }
   1904  1.1.1.8  christos 
   1905  1.1.1.8  christos private:
   1906  1.1.1.8  christos   /* Update results to include V, a found field/baseclass.  */
   1907  1.1.1.8  christos   void update_result (struct value *v, LONGEST boffset);
   1908  1.1.1.8  christos 
   1909  1.1.1.8  christos   /* The name of the field/baseclass we're searching for.  */
   1910  1.1.1.8  christos   const char *m_name;
   1911  1.1.1.8  christos 
   1912  1.1.1.8  christos   /* Whether we're looking for a baseclass, or a field.  */
   1913  1.1.1.8  christos   const bool m_looking_for_baseclass;
   1914  1.1.1.8  christos 
   1915  1.1.1.8  christos   /* The offset of the baseclass containing the field/baseclass we
   1916  1.1.1.8  christos      last recorded.  */
   1917  1.1.1.8  christos   LONGEST m_last_boffset = 0;
   1918  1.1.1.8  christos 
   1919  1.1.1.8  christos   /* If looking for a baseclass, then the result is stored here.  */
   1920  1.1.1.8  christos   struct value *m_baseclass = nullptr;
   1921  1.1.1.8  christos 
   1922  1.1.1.8  christos   /* When looking for fields, the found candidates are stored
   1923  1.1.1.8  christos      here.  */
   1924  1.1.1.8  christos   std::vector<found_field> m_fields;
   1925  1.1.1.8  christos 
   1926  1.1.1.8  christos   /* The type of the initial type passed to search_struct_field; this
   1927  1.1.1.8  christos      is used for error reporting when the lookup is ambiguous.  */
   1928  1.1.1.8  christos   struct type *m_outermost_type;
   1929  1.1.1.8  christos 
   1930  1.1.1.8  christos   /* The full path to the struct being inspected.  E.g. for field 'x'
   1931  1.1.1.8  christos      defined in class B inherited by class A, we have A and B pushed
   1932  1.1.1.8  christos      on the path.  */
   1933  1.1.1.8  christos   std::vector <struct type *> m_struct_path;
   1934  1.1.1.8  christos };
   1935  1.1.1.8  christos 
   1936      1.1  christos void
   1937      1.1  christos struct_field_searcher::update_result (struct value *v, LONGEST boffset)
   1938      1.1  christos {
   1939  1.1.1.8  christos   if (v != NULL)
   1940  1.1.1.8  christos     {
   1941  1.1.1.8  christos       if (m_looking_for_baseclass)
   1942  1.1.1.8  christos 	{
   1943  1.1.1.8  christos 	  if (m_baseclass != nullptr
   1944  1.1.1.8  christos 	      /* The result is not ambiguous if all the classes that are
   1945  1.1.1.8  christos 		 found occupy the same space.  */
   1946  1.1.1.8  christos 	      && m_last_boffset != boffset)
   1947  1.1.1.8  christos 	    error (_("base class '%s' is ambiguous in type '%s'"),
   1948  1.1.1.8  christos 		   m_name, TYPE_SAFE_NAME (m_outermost_type));
   1949  1.1.1.8  christos 
   1950  1.1.1.8  christos 	  m_baseclass = v;
   1951  1.1.1.8  christos 	  m_last_boffset = boffset;
   1952  1.1.1.8  christos 	}
   1953  1.1.1.8  christos       else
   1954  1.1.1.8  christos 	{
   1955  1.1.1.8  christos 	  /* The field is not ambiguous if it occupies the same
   1956  1.1.1.8  christos 	     space.  */
   1957  1.1.1.8  christos 	  if (m_fields.empty () || m_last_boffset != boffset)
   1958  1.1.1.8  christos 	    m_fields.push_back ({m_struct_path, v});
   1959  1.1.1.8  christos 	  else
   1960  1.1.1.8  christos 	    {
   1961  1.1.1.8  christos 	    /*Fields can occupy the same space and have the same name (be
   1962  1.1.1.8  christos 	      ambiguous).  This can happen when fields in two different base
   1963  1.1.1.8  christos 	      classes are marked [[no_unique_address]] and have the same name.
   1964  1.1.1.8  christos 	      The C++ standard says that such fields can only occupy the same
   1965  1.1.1.8  christos 	      space if they are of different type, but we don't rely on that in
   1966  1.1.1.8  christos 	      the following code. */
   1967  1.1.1.8  christos 	      bool ambiguous = false, insert = true;
   1968  1.1.1.8  christos 	      for (const found_field &field: m_fields)
   1969  1.1.1.8  christos 		{
   1970  1.1.1.8  christos 		  if(field.path.back () != m_struct_path.back ())
   1971  1.1.1.8  christos 		    {
   1972  1.1.1.8  christos 		    /* Same boffset points to members of different classes.
   1973  1.1.1.8  christos 		       We have found an ambiguity and should record it.  */
   1974  1.1.1.8  christos 		      ambiguous = true;
   1975  1.1.1.8  christos 		    }
   1976  1.1.1.8  christos 		  else
   1977  1.1.1.8  christos 		    {
   1978  1.1.1.8  christos 		    /* We don't need to insert this value again, because a
   1979  1.1.1.8  christos 		       non-ambiguous path already leads to it.  */
   1980  1.1.1.8  christos 		      insert = false;
   1981  1.1.1.8  christos 		      break;
   1982  1.1.1.8  christos 		    }
   1983  1.1.1.8  christos 		}
   1984  1.1.1.8  christos 	      if (ambiguous && insert)
   1985  1.1.1.8  christos 		m_fields.push_back ({m_struct_path, v});
   1986      1.1  christos 	    }
   1987      1.1  christos 	}
   1988      1.1  christos     }
   1989      1.1  christos }
   1990  1.1.1.8  christos 
   1991      1.1  christos /* A helper for search_struct_field.  This does all the work; most
   1992  1.1.1.8  christos    arguments are as passed to search_struct_field.  */
   1993  1.1.1.8  christos 
   1994  1.1.1.8  christos void
   1995      1.1  christos struct_field_searcher::search (struct value *arg1, LONGEST offset,
   1996      1.1  christos 			       struct type *type)
   1997      1.1  christos {
   1998      1.1  christos   int i;
   1999  1.1.1.8  christos   int nbases;
   2000  1.1.1.8  christos 
   2001  1.1.1.8  christos   m_struct_path.push_back (type);
   2002  1.1.1.4  christos   SCOPE_EXIT { m_struct_path.pop_back (); };
   2003      1.1  christos 
   2004      1.1  christos   type = check_typedef (type);
   2005  1.1.1.8  christos   nbases = TYPE_N_BASECLASSES (type);
   2006  1.1.1.7  christos 
   2007      1.1  christos   if (!m_looking_for_baseclass)
   2008  1.1.1.8  christos     for (i = type->num_fields () - 1; i >= nbases; i--)
   2009      1.1  christos       {
   2010  1.1.1.8  christos 	const char *t_field_name = type->field (i).name ();
   2011      1.1  christos 
   2012      1.1  christos 	if (t_field_name && (strcmp_iw (t_field_name, m_name) == 0))
   2013      1.1  christos 	  {
   2014  1.1.1.7  christos 	    struct value *v;
   2015      1.1  christos 
   2016      1.1  christos 	    if (field_is_static (&type->field (i)))
   2017      1.1  christos 	      v = value_static_field (type, i);
   2018  1.1.1.8  christos 	    else
   2019  1.1.1.8  christos 	      v = value_primitive_field (arg1, offset, i, type);
   2020      1.1  christos 
   2021      1.1  christos 	    update_result (v, offset);
   2022      1.1  christos 	    return;
   2023      1.1  christos 	  }
   2024  1.1.1.2  christos 
   2025      1.1  christos 	if (t_field_name
   2026  1.1.1.7  christos 	    && t_field_name[0] == '\0')
   2027      1.1  christos 	  {
   2028  1.1.1.7  christos 	    struct type *field_type = type->field (i).type ();
   2029  1.1.1.7  christos 
   2030      1.1  christos 	    if (field_type->code () == TYPE_CODE_UNION
   2031      1.1  christos 		|| field_type->code () == TYPE_CODE_STRUCT)
   2032      1.1  christos 	      {
   2033      1.1  christos 		/* Look for a match through the fields of an anonymous
   2034      1.1  christos 		   union, or anonymous struct.  C++ provides anonymous
   2035      1.1  christos 		   unions.
   2036      1.1  christos 
   2037      1.1  christos 		   In the GNU Chill (now deleted from GDB)
   2038      1.1  christos 		   implementation of variant record types, each
   2039      1.1  christos 		   <alternative field> has an (anonymous) union type,
   2040      1.1  christos 		   each member of the union represents a <variant
   2041      1.1  christos 		   alternative>.  Each <variant alternative> is
   2042      1.1  christos 		   represented as a struct, with a member for each
   2043  1.1.1.4  christos 		   <variant field>.  */
   2044      1.1  christos 
   2045      1.1  christos 		LONGEST new_offset = offset;
   2046      1.1  christos 
   2047      1.1  christos 		/* This is pretty gross.  In G++, the offset in an
   2048      1.1  christos 		   anonymous union is relative to the beginning of the
   2049      1.1  christos 		   enclosing struct.  In the GNU Chill (now deleted
   2050      1.1  christos 		   from GDB) implementation of variant records, the
   2051  1.1.1.7  christos 		   bitpos is zero in an anonymous union field, so we
   2052  1.1.1.7  christos 		   have to add the offset of the union here.  */
   2053  1.1.1.8  christos 		if (field_type->code () == TYPE_CODE_STRUCT
   2054  1.1.1.8  christos 		    || (field_type->num_fields () > 0
   2055      1.1  christos 			&& field_type->field (0).loc_bitpos () == 0))
   2056  1.1.1.8  christos 		  new_offset += type->field (i).loc_bitpos () / 8;
   2057      1.1  christos 
   2058      1.1  christos 		search (arg1, new_offset, field_type);
   2059      1.1  christos 	      }
   2060      1.1  christos 	  }
   2061      1.1  christos       }
   2062      1.1  christos 
   2063      1.1  christos   for (i = 0; i < nbases; i++)
   2064      1.1  christos     {
   2065      1.1  christos       struct value *v = NULL;
   2066  1.1.1.8  christos       struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
   2067  1.1.1.8  christos       /* If we are looking for baseclasses, this is what we get when
   2068  1.1.1.8  christos 	 we hit them.  But it could happen that the base part's member
   2069      1.1  christos 	 name is not yet filled in.  */
   2070  1.1.1.8  christos       int found_baseclass = (m_looking_for_baseclass
   2071  1.1.1.4  christos 			     && TYPE_BASECLASS_NAME (type, i) != NULL
   2072      1.1  christos 			     && (strcmp_iw (m_name, basetype->name ()) == 0));
   2073      1.1  christos       LONGEST boffset = value_embedded_offset (arg1) + offset;
   2074      1.1  christos 
   2075      1.1  christos       if (BASETYPE_VIA_VIRTUAL (type, i))
   2076      1.1  christos 	{
   2077      1.1  christos 	  struct value *v2;
   2078  1.1.1.8  christos 
   2079      1.1  christos 	  boffset = baseclass_offset (type, i,
   2080      1.1  christos 				      value_contents_for_printing (arg1).data (),
   2081      1.1  christos 				      value_embedded_offset (arg1) + offset,
   2082      1.1  christos 				      value_address (arg1),
   2083      1.1  christos 				      arg1);
   2084      1.1  christos 
   2085      1.1  christos 	  /* The virtual base class pointer might have been clobbered
   2086      1.1  christos 	     by the user program.  Make sure that it still points to a
   2087      1.1  christos 	     valid memory location.  */
   2088      1.1  christos 
   2089  1.1.1.8  christos 	  boffset += value_embedded_offset (arg1) + offset;
   2090      1.1  christos 	  if (boffset < 0
   2091      1.1  christos 	      || boffset >= value_enclosing_type (arg1)->length ())
   2092      1.1  christos 	    {
   2093      1.1  christos 	      CORE_ADDR base_addr;
   2094      1.1  christos 
   2095      1.1  christos 	      base_addr = value_address (arg1) + boffset;
   2096  1.1.1.8  christos 	      v2 = value_at_lazy (basetype, base_addr);
   2097  1.1.1.8  christos 	      if (target_read_memory (base_addr,
   2098      1.1  christos 				      value_contents_raw (v2).data (),
   2099      1.1  christos 				      value_type (v2)->length ()) != 0)
   2100      1.1  christos 		error (_("virtual baseclass botch"));
   2101      1.1  christos 	    }
   2102      1.1  christos 	  else
   2103      1.1  christos 	    {
   2104      1.1  christos 	      v2 = value_copy (arg1);
   2105      1.1  christos 	      deprecated_set_value_type (v2, basetype);
   2106      1.1  christos 	      set_value_embedded_offset (v2, boffset);
   2107      1.1  christos 	    }
   2108      1.1  christos 
   2109      1.1  christos 	  if (found_baseclass)
   2110  1.1.1.8  christos 	    v = v2;
   2111      1.1  christos 	  else
   2112      1.1  christos 	    search (v2, 0, TYPE_BASECLASS (type, i));
   2113      1.1  christos 	}
   2114      1.1  christos       else if (found_baseclass)
   2115      1.1  christos 	v = value_primitive_field (arg1, offset, i, type);
   2116  1.1.1.8  christos       else
   2117  1.1.1.8  christos 	{
   2118      1.1  christos 	  search (arg1, offset + TYPE_BASECLASS_BITPOS (type, i) / 8,
   2119      1.1  christos 		  basetype);
   2120  1.1.1.8  christos 	}
   2121      1.1  christos 
   2122      1.1  christos       update_result (v, boffset);
   2123      1.1  christos     }
   2124      1.1  christos }
   2125  1.1.1.3  christos 
   2126  1.1.1.3  christos /* Helper function used by value_struct_elt to recurse through
   2127      1.1  christos    baseclasses.  Look for a field NAME in ARG1.  Search in it assuming
   2128      1.1  christos    it has (class) type TYPE.  If found, return value, else return NULL.
   2129      1.1  christos 
   2130      1.1  christos    If LOOKING_FOR_BASECLASS, then instead of looking for struct
   2131      1.1  christos    fields, look for a baseclass named NAME.  */
   2132  1.1.1.3  christos 
   2133      1.1  christos static struct value *
   2134      1.1  christos search_struct_field (const char *name, struct value *arg1,
   2135  1.1.1.8  christos 		     struct type *type, int looking_for_baseclass)
   2136      1.1  christos {
   2137  1.1.1.8  christos   struct_field_searcher searcher (name, type, looking_for_baseclass);
   2138  1.1.1.8  christos 
   2139  1.1.1.8  christos   searcher.search (arg1, 0, type);
   2140  1.1.1.8  christos 
   2141  1.1.1.8  christos   if (!looking_for_baseclass)
   2142  1.1.1.8  christos     {
   2143  1.1.1.8  christos       const auto &fields = searcher.fields ();
   2144  1.1.1.8  christos 
   2145  1.1.1.8  christos       if (fields.empty ())
   2146  1.1.1.8  christos 	return nullptr;
   2147  1.1.1.8  christos       else if (fields.size () == 1)
   2148  1.1.1.8  christos 	return fields[0].field_value;
   2149  1.1.1.8  christos       else
   2150  1.1.1.8  christos 	{
   2151  1.1.1.8  christos 	  std::string candidates;
   2152  1.1.1.8  christos 
   2153  1.1.1.8  christos 	  for (auto &&candidate : fields)
   2154  1.1.1.8  christos 	    {
   2155  1.1.1.8  christos 	      gdb_assert (!candidate.path.empty ());
   2156  1.1.1.8  christos 
   2157  1.1.1.8  christos 	      struct type *field_type = value_type (candidate.field_value);
   2158  1.1.1.8  christos 	      struct type *struct_type = candidate.path.back ();
   2159  1.1.1.8  christos 
   2160  1.1.1.8  christos 	      std::string path;
   2161  1.1.1.8  christos 	      bool first = true;
   2162  1.1.1.8  christos 	      for (struct type *t : candidate.path)
   2163  1.1.1.8  christos 		{
   2164  1.1.1.8  christos 		  if (first)
   2165  1.1.1.8  christos 		    first = false;
   2166  1.1.1.8  christos 		  else
   2167  1.1.1.8  christos 		    path += " -> ";
   2168  1.1.1.8  christos 		  path += t->name ();
   2169  1.1.1.8  christos 		}
   2170  1.1.1.8  christos 
   2171  1.1.1.8  christos 	      candidates += string_printf ("\n  '%s %s::%s' (%s)",
   2172  1.1.1.8  christos 					   TYPE_SAFE_NAME (field_type),
   2173  1.1.1.8  christos 					   TYPE_SAFE_NAME (struct_type),
   2174  1.1.1.8  christos 					   name,
   2175  1.1.1.8  christos 					   path.c_str ());
   2176  1.1.1.8  christos 	    }
   2177  1.1.1.8  christos 
   2178  1.1.1.8  christos 	  error (_("Request for member '%s' is ambiguous in type '%s'."
   2179  1.1.1.8  christos 		   " Candidates are:%s"),
   2180  1.1.1.8  christos 		 name, TYPE_SAFE_NAME (type),
   2181  1.1.1.8  christos 		 candidates.c_str ());
   2182  1.1.1.8  christos 	}
   2183  1.1.1.8  christos     }
   2184      1.1  christos   else
   2185      1.1  christos     return searcher.baseclass ();
   2186      1.1  christos }
   2187      1.1  christos 
   2188      1.1  christos /* Helper function used by value_struct_elt to recurse through
   2189      1.1  christos    baseclasses.  Look for a field NAME in ARG1.  Adjust the address of
   2190      1.1  christos    ARG1 by OFFSET bytes, and search in it assuming it has (class) type
   2191  1.1.1.8  christos    TYPE.
   2192  1.1.1.8  christos 
   2193  1.1.1.8  christos    ARGS is an optional array of argument values used to help finding NAME.
   2194  1.1.1.8  christos    The contents of ARGS can be adjusted if type coercion is required in
   2195      1.1  christos    order to find a matching NAME.
   2196      1.1  christos 
   2197      1.1  christos    If found, return value, else if name matched and args not return
   2198      1.1  christos    (value) -1, else return NULL.  */
   2199      1.1  christos 
   2200  1.1.1.8  christos static struct value *
   2201  1.1.1.8  christos search_struct_method (const char *name, struct value **arg1p,
   2202  1.1.1.8  christos 		      gdb::optional<gdb::array_view<value *>> args,
   2203      1.1  christos 		      LONGEST offset, int *static_memfuncp,
   2204      1.1  christos 		      struct type *type)
   2205      1.1  christos {
   2206      1.1  christos   int i;
   2207      1.1  christos   struct value *v;
   2208  1.1.1.4  christos   int name_matched = 0;
   2209      1.1  christos 
   2210      1.1  christos   type = check_typedef (type);
   2211      1.1  christos   for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
   2212      1.1  christos     {
   2213      1.1  christos       const char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
   2214      1.1  christos 
   2215      1.1  christos       if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
   2216      1.1  christos 	{
   2217      1.1  christos 	  int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1;
   2218      1.1  christos 	  struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
   2219      1.1  christos 
   2220  1.1.1.8  christos 	  name_matched = 1;
   2221      1.1  christos 	  check_stub_method_group (type, i);
   2222      1.1  christos 	  if (j > 0 && !args.has_value ())
   2223  1.1.1.8  christos 	    error (_("cannot resolve overloaded method "
   2224      1.1  christos 		     "`%s': no arguments supplied"), name);
   2225      1.1  christos 	  else if (j == 0 && !args.has_value ())
   2226      1.1  christos 	    {
   2227      1.1  christos 	      v = value_fn_field (arg1p, f, j, type, offset);
   2228      1.1  christos 	      if (v != NULL)
   2229      1.1  christos 		return v;
   2230      1.1  christos 	    }
   2231      1.1  christos 	  else
   2232  1.1.1.8  christos 	    while (j >= 0)
   2233      1.1  christos 	      {
   2234  1.1.1.8  christos 		gdb_assert (args.has_value ());
   2235  1.1.1.7  christos 		if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j),
   2236  1.1.1.8  christos 			      TYPE_FN_FIELD_TYPE (f, j)->has_varargs (),
   2237      1.1  christos 			      TYPE_FN_FIELD_TYPE (f, j)->num_fields (),
   2238      1.1  christos 			      TYPE_FN_FIELD_ARGS (f, j), *args))
   2239      1.1  christos 		  {
   2240      1.1  christos 		    if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
   2241      1.1  christos 		      return value_virtual_fn_field (arg1p, f, j,
   2242      1.1  christos 						     type, offset);
   2243      1.1  christos 		    if (TYPE_FN_FIELD_STATIC_P (f, j)
   2244      1.1  christos 			&& static_memfuncp)
   2245      1.1  christos 		      *static_memfuncp = 1;
   2246      1.1  christos 		    v = value_fn_field (arg1p, f, j, type, offset);
   2247      1.1  christos 		    if (v != NULL)
   2248      1.1  christos 		      return v;
   2249      1.1  christos 		  }
   2250      1.1  christos 		j--;
   2251      1.1  christos 	      }
   2252      1.1  christos 	}
   2253      1.1  christos     }
   2254      1.1  christos 
   2255  1.1.1.4  christos   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
   2256  1.1.1.4  christos     {
   2257      1.1  christos       LONGEST base_offset;
   2258      1.1  christos       LONGEST this_offset;
   2259      1.1  christos 
   2260      1.1  christos       if (BASETYPE_VIA_VIRTUAL (type, i))
   2261      1.1  christos 	{
   2262      1.1  christos 	  struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
   2263      1.1  christos 	  struct value *base_val;
   2264      1.1  christos 	  const gdb_byte *base_valaddr;
   2265      1.1  christos 
   2266  1.1.1.2  christos 	  /* The virtual base class pointer might have been
   2267      1.1  christos 	     clobbered by the user program.  Make sure that it
   2268  1.1.1.8  christos 	     still points to a valid memory location.  */
   2269      1.1  christos 
   2270      1.1  christos 	  if (offset < 0 || offset >= type->length ())
   2271      1.1  christos 	    {
   2272  1.1.1.8  christos 	      CORE_ADDR address;
   2273      1.1  christos 
   2274      1.1  christos 	      gdb::byte_vector tmp (baseclass->length ());
   2275      1.1  christos 	      address = value_address (*arg1p);
   2276  1.1.1.8  christos 
   2277      1.1  christos 	      if (target_read_memory (address + offset,
   2278      1.1  christos 				      tmp.data (), baseclass->length ()) != 0)
   2279      1.1  christos 		error (_("virtual baseclass botch"));
   2280  1.1.1.6  christos 
   2281      1.1  christos 	      base_val = value_from_contents_and_address (baseclass,
   2282  1.1.1.8  christos 							  tmp.data (),
   2283      1.1  christos 							  address + offset);
   2284      1.1  christos 	      base_valaddr = value_contents_for_printing (base_val).data ();
   2285      1.1  christos 	      this_offset = 0;
   2286      1.1  christos 	    }
   2287      1.1  christos 	  else
   2288  1.1.1.8  christos 	    {
   2289      1.1  christos 	      base_val = *arg1p;
   2290      1.1  christos 	      base_valaddr = value_contents_for_printing (*arg1p).data ();
   2291      1.1  christos 	      this_offset = offset;
   2292      1.1  christos 	    }
   2293      1.1  christos 
   2294      1.1  christos 	  base_offset = baseclass_offset (type, i, base_valaddr,
   2295      1.1  christos 					  this_offset, value_address (base_val),
   2296      1.1  christos 					  base_val);
   2297      1.1  christos 	}
   2298      1.1  christos       else
   2299      1.1  christos 	{
   2300      1.1  christos 	  base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
   2301      1.1  christos 	}
   2302      1.1  christos       v = search_struct_method (name, arg1p, args, base_offset + offset,
   2303      1.1  christos 				static_memfuncp, TYPE_BASECLASS (type, i));
   2304      1.1  christos       if (v == (struct value *) - 1)
   2305      1.1  christos 	{
   2306      1.1  christos 	  name_matched = 1;
   2307      1.1  christos 	}
   2308      1.1  christos       else if (v)
   2309      1.1  christos 	{
   2310      1.1  christos 	  /* FIXME-bothner:  Why is this commented out?  Why is it here?  */
   2311      1.1  christos 	  /* *arg1p = arg1_tmp; */
   2312      1.1  christos 	  return v;
   2313      1.1  christos 	}
   2314      1.1  christos     }
   2315      1.1  christos   if (name_matched)
   2316      1.1  christos     return (struct value *) - 1;
   2317      1.1  christos   else
   2318      1.1  christos     return NULL;
   2319      1.1  christos }
   2320      1.1  christos 
   2321      1.1  christos /* Given *ARGP, a value of type (pointer to a)* structure/union,
   2322      1.1  christos    extract the component named NAME from the ultimate target
   2323      1.1  christos    structure/union and return it as a value with its appropriate type.
   2324      1.1  christos    ERR is used in the error message if *ARGP's type is wrong.
   2325      1.1  christos 
   2326      1.1  christos    C++: ARGS is a list of argument types to aid in the selection of
   2327      1.1  christos    an appropriate method.  Also, handle derived types.
   2328      1.1  christos 
   2329      1.1  christos    STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
   2330      1.1  christos    where the truthvalue of whether the function that was resolved was
   2331      1.1  christos    a static member function or not is stored.
   2332      1.1  christos 
   2333      1.1  christos    ERR is an error message to be printed in case the field is not
   2334      1.1  christos    found.  */
   2335  1.1.1.8  christos 
   2336  1.1.1.8  christos struct value *
   2337      1.1  christos value_struct_elt (struct value **argp,
   2338      1.1  christos 		  gdb::optional<gdb::array_view<value *>> args,
   2339      1.1  christos 		  const char *name, int *static_memfuncp, const char *err)
   2340      1.1  christos {
   2341      1.1  christos   struct type *t;
   2342      1.1  christos   struct value *v;
   2343      1.1  christos 
   2344      1.1  christos   *argp = coerce_array (*argp);
   2345      1.1  christos 
   2346      1.1  christos   t = check_typedef (value_type (*argp));
   2347      1.1  christos 
   2348  1.1.1.8  christos   /* Follow pointers until we get to a non-pointer.  */
   2349      1.1  christos 
   2350      1.1  christos   while (t->is_pointer_or_reference ())
   2351      1.1  christos     {
   2352  1.1.1.7  christos       *argp = value_ind (*argp);
   2353      1.1  christos       /* Don't coerce fn pointer to fn and then back again!  */
   2354      1.1  christos       if (check_typedef (value_type (*argp))->code () != TYPE_CODE_FUNC)
   2355      1.1  christos 	*argp = coerce_array (*argp);
   2356      1.1  christos       t = check_typedef (value_type (*argp));
   2357  1.1.1.7  christos     }
   2358  1.1.1.7  christos 
   2359      1.1  christos   if (t->code () != TYPE_CODE_STRUCT
   2360      1.1  christos       && t->code () != TYPE_CODE_UNION)
   2361      1.1  christos     error (_("Attempt to extract a component of a value that is not a %s."),
   2362      1.1  christos 	   err);
   2363      1.1  christos 
   2364      1.1  christos   /* Assume it's not, unless we see that it is.  */
   2365      1.1  christos   if (static_memfuncp)
   2366  1.1.1.8  christos     *static_memfuncp = 0;
   2367      1.1  christos 
   2368      1.1  christos   if (!args.has_value ())
   2369      1.1  christos     {
   2370      1.1  christos       /* if there are no arguments ...do this...  */
   2371  1.1.1.8  christos 
   2372  1.1.1.3  christos       /* Try as a field first, because if we succeed, there is less
   2373      1.1  christos 	 work to be done.  */
   2374      1.1  christos       v = search_struct_field (name, *argp, t, 0);
   2375      1.1  christos       if (v)
   2376  1.1.1.8  christos 	return v;
   2377  1.1.1.8  christos 
   2378  1.1.1.8  christos       if (current_language->la_language == language_fortran)
   2379  1.1.1.8  christos 	{
   2380  1.1.1.8  christos 	  /* If it is not a field it is the type name of an inherited
   2381  1.1.1.8  christos 	  structure.  */
   2382  1.1.1.8  christos 	  v = search_struct_field (name, *argp, t, 1);
   2383  1.1.1.8  christos 	  if (v)
   2384  1.1.1.8  christos 	    return v;
   2385      1.1  christos 	}
   2386  1.1.1.8  christos 
   2387  1.1.1.8  christos       /* C++: If it was not found as a data field, then try to
   2388      1.1  christos 	 return it as a pointer to a method.  */
   2389      1.1  christos       v = search_struct_method (name, argp, args, 0,
   2390      1.1  christos 				static_memfuncp, t);
   2391      1.1  christos 
   2392      1.1  christos       if (v == (struct value *) - 1)
   2393      1.1  christos 	error (_("Cannot take address of method %s."), name);
   2394      1.1  christos       else if (v == 0)
   2395      1.1  christos 	{
   2396      1.1  christos 	  if (TYPE_NFN_FIELDS (t))
   2397      1.1  christos 	    error (_("There is no member or method named %s."), name);
   2398      1.1  christos 	  else
   2399      1.1  christos 	    error (_("There is no member named %s."), name);
   2400      1.1  christos 	}
   2401      1.1  christos       return v;
   2402  1.1.1.8  christos     }
   2403  1.1.1.2  christos 
   2404  1.1.1.8  christos   v = search_struct_method (name, argp, args, 0,
   2405      1.1  christos 			    static_memfuncp, t);
   2406      1.1  christos 
   2407      1.1  christos   if (v == (struct value *) - 1)
   2408      1.1  christos     {
   2409      1.1  christos       error (_("One of the arguments you tried to pass to %s could not "
   2410      1.1  christos 	       "be converted to what the function wants."), name);
   2411      1.1  christos     }
   2412      1.1  christos   else if (v == 0)
   2413  1.1.1.8  christos     {
   2414  1.1.1.8  christos       /* See if user tried to invoke data as function.  If so, hand it
   2415  1.1.1.3  christos 	 back.  If it's not callable (i.e., a pointer to function),
   2416      1.1  christos 	 gdb should give an error.  */
   2417      1.1  christos       v = search_struct_field (name, *argp, t, 0);
   2418      1.1  christos       /* If we found an ordinary field, then it is not a method call.
   2419      1.1  christos 	 So, treat it as if it were a static member function.  */
   2420      1.1  christos       if (v && static_memfuncp)
   2421      1.1  christos 	*static_memfuncp = 1;
   2422      1.1  christos     }
   2423      1.1  christos 
   2424  1.1.1.8  christos   if (!v)
   2425      1.1  christos     throw_error (NOT_FOUND_ERROR,
   2426      1.1  christos 		 _("Structure has no component named %s."), name);
   2427      1.1  christos   return v;
   2428      1.1  christos }
   2429      1.1  christos 
   2430      1.1  christos /* Given *ARGP, a value of type structure or union, or a pointer/reference
   2431      1.1  christos    to a structure or union, extract and return its component (field) of
   2432      1.1  christos    type FTYPE at the specified BITPOS.
   2433      1.1  christos    Throw an exception on error.  */
   2434      1.1  christos 
   2435      1.1  christos struct value *
   2436      1.1  christos value_struct_elt_bitpos (struct value **argp, int bitpos, struct type *ftype,
   2437      1.1  christos 			 const char *err)
   2438      1.1  christos {
   2439      1.1  christos   struct type *t;
   2440      1.1  christos   int i;
   2441      1.1  christos 
   2442      1.1  christos   *argp = coerce_array (*argp);
   2443      1.1  christos 
   2444  1.1.1.8  christos   t = check_typedef (value_type (*argp));
   2445      1.1  christos 
   2446      1.1  christos   while (t->is_pointer_or_reference ())
   2447  1.1.1.7  christos     {
   2448      1.1  christos       *argp = value_ind (*argp);
   2449      1.1  christos       if (check_typedef (value_type (*argp))->code () != TYPE_CODE_FUNC)
   2450      1.1  christos 	*argp = coerce_array (*argp);
   2451      1.1  christos       t = check_typedef (value_type (*argp));
   2452  1.1.1.7  christos     }
   2453  1.1.1.7  christos 
   2454      1.1  christos   if (t->code () != TYPE_CODE_STRUCT
   2455      1.1  christos       && t->code () != TYPE_CODE_UNION)
   2456      1.1  christos     error (_("Attempt to extract a component of a value that is not a %s."),
   2457  1.1.1.7  christos 	   err);
   2458      1.1  christos 
   2459  1.1.1.7  christos   for (i = TYPE_N_BASECLASSES (t); i < t->num_fields (); i++)
   2460  1.1.1.8  christos     {
   2461  1.1.1.7  christos       if (!field_is_static (&t->field (i))
   2462      1.1  christos 	  && bitpos == t->field (i).loc_bitpos ()
   2463      1.1  christos 	  && types_equal (ftype, t->field (i).type ()))
   2464      1.1  christos 	return value_primitive_field (*argp, 0, i, t);
   2465      1.1  christos     }
   2466      1.1  christos 
   2467      1.1  christos   error (_("No field with matching bitpos and type."));
   2468      1.1  christos 
   2469      1.1  christos   /* Never hit.  */
   2470      1.1  christos   return NULL;
   2471      1.1  christos }
   2472  1.1.1.6  christos 
   2473  1.1.1.2  christos /* Search through the methods of an object (and its bases) to find a
   2474  1.1.1.2  christos    specified method.  Return a reference to the fn_field list METHODS of
   2475  1.1.1.6  christos    overloaded instances defined in the source language.  If available
   2476      1.1  christos    and matching, a vector of matching xmethods defined in extension
   2477      1.1  christos    languages are also returned in XMETHODS.
   2478      1.1  christos 
   2479      1.1  christos    Helper function for value_find_oload_list.
   2480      1.1  christos    ARGP is a pointer to a pointer to a value (the object).
   2481      1.1  christos    METHOD is a string containing the method name.
   2482  1.1.1.6  christos    OFFSET is the offset within the value.
   2483  1.1.1.6  christos    TYPE is the assumed type of the object.
   2484  1.1.1.6  christos    METHODS is a pointer to the matching overloaded instances defined
   2485  1.1.1.2  christos       in the source language.  Since this is a recursive function,
   2486  1.1.1.2  christos       *METHODS should be set to NULL when calling this function.
   2487  1.1.1.6  christos    NUM_FNS is the number of overloaded instances.  *NUM_FNS should be set to
   2488  1.1.1.2  christos       0 when calling this function.
   2489      1.1  christos    XMETHODS is the vector of matching xmethod workers.  *XMETHODS
   2490      1.1  christos       should also be set to NULL when calling this function.
   2491      1.1  christos    BASETYPE is set to the actual type of the subobject where the
   2492      1.1  christos       method is found.
   2493  1.1.1.2  christos    BOFFSET is the offset of the base subobject where the method is found.  */
   2494      1.1  christos 
   2495  1.1.1.4  christos static void
   2496  1.1.1.6  christos find_method_list (struct value **argp, const char *method,
   2497  1.1.1.6  christos 		  LONGEST offset, struct type *type,
   2498  1.1.1.4  christos 		  gdb::array_view<fn_field> *methods,
   2499      1.1  christos 		  std::vector<xmethod_worker_up> *xmethods,
   2500      1.1  christos 		  struct type **basetype, LONGEST *boffset)
   2501  1.1.1.2  christos {
   2502      1.1  christos   int i;
   2503  1.1.1.6  christos   struct fn_field *f = NULL;
   2504  1.1.1.4  christos 
   2505      1.1  christos   gdb_assert (methods != NULL && xmethods != NULL);
   2506  1.1.1.2  christos   type = check_typedef (type);
   2507  1.1.1.2  christos 
   2508  1.1.1.2  christos   /* First check in object itself.
   2509  1.1.1.2  christos      This function is called recursively to search through base classes.
   2510  1.1.1.6  christos      If there is a source method match found at some stage, then we need not
   2511      1.1  christos      look for source methods in consequent recursive calls.  */
   2512  1.1.1.2  christos   if (methods->empty ())
   2513      1.1  christos     {
   2514  1.1.1.2  christos       for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
   2515  1.1.1.2  christos 	{
   2516      1.1  christos 	  /* pai: FIXME What about operators and type conversions?  */
   2517  1.1.1.2  christos 	  const char *fn_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
   2518  1.1.1.2  christos 
   2519  1.1.1.2  christos 	  if (fn_field_name && (strcmp_iw (fn_field_name, method) == 0))
   2520  1.1.1.2  christos 	    {
   2521  1.1.1.6  christos 	      int len = TYPE_FN_FIELDLIST_LENGTH (type, i);
   2522  1.1.1.2  christos 	      f = TYPE_FN_FIELDLIST1 (type, i);
   2523  1.1.1.2  christos 	      *methods = gdb::make_array_view (f, len);
   2524  1.1.1.2  christos 
   2525      1.1  christos 	      *basetype = type;
   2526  1.1.1.2  christos 	      *boffset = offset;
   2527  1.1.1.2  christos 
   2528      1.1  christos 	      /* Resolve any stub methods.  */
   2529  1.1.1.2  christos 	      check_stub_method_group (type, i);
   2530  1.1.1.2  christos 
   2531      1.1  christos 	      break;
   2532      1.1  christos 	    }
   2533      1.1  christos 	}
   2534  1.1.1.2  christos     }
   2535  1.1.1.2  christos 
   2536  1.1.1.2  christos   /* Unlike source methods, xmethods can be accumulated over successive
   2537  1.1.1.2  christos      recursive calls.  In other words, an xmethod named 'm' in a class
   2538  1.1.1.2  christos      will not hide an xmethod named 'm' in its base class(es).  We want
   2539  1.1.1.2  christos      it to be this way because xmethods are after all convenience functions
   2540  1.1.1.2  christos      and hence there is no point restricting them with something like method
   2541  1.1.1.6  christos      hiding.  Moreover, if hiding is done for xmethods as well, then we will
   2542  1.1.1.2  christos      have to provide a mechanism to un-hide (like the 'using' construct).  */
   2543  1.1.1.2  christos   get_matching_xmethod_workers (type, method, xmethods);
   2544  1.1.1.2  christos 
   2545  1.1.1.2  christos   /* If source methods are not found in current class, look for them in the
   2546      1.1  christos      base classes.  We also have to go through the base classes to gather
   2547      1.1  christos      extension methods.  */
   2548  1.1.1.4  christos   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
   2549      1.1  christos     {
   2550      1.1  christos       LONGEST base_offset;
   2551      1.1  christos 
   2552      1.1  christos       if (BASETYPE_VIA_VIRTUAL (type, i))
   2553  1.1.1.8  christos 	{
   2554      1.1  christos 	  base_offset = baseclass_offset (type, i,
   2555      1.1  christos 					  value_contents_for_printing (*argp).data (),
   2556      1.1  christos 					  value_offset (*argp) + offset,
   2557      1.1  christos 					  value_address (*argp), *argp);
   2558      1.1  christos 	}
   2559      1.1  christos       else /* Non-virtual base, simply use bit position from debug
   2560      1.1  christos 	      info.  */
   2561      1.1  christos 	{
   2562  1.1.1.2  christos 	  base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
   2563  1.1.1.2  christos 	}
   2564  1.1.1.6  christos 
   2565  1.1.1.6  christos       find_method_list (argp, method, base_offset + offset,
   2566      1.1  christos 			TYPE_BASECLASS (type, i), methods,
   2567      1.1  christos 			xmethods, basetype, boffset);
   2568      1.1  christos     }
   2569  1.1.1.2  christos }
   2570  1.1.1.2  christos 
   2571  1.1.1.6  christos /* Return the list of overloaded methods of a specified name.  The methods
   2572  1.1.1.6  christos    could be those GDB finds in the binary, or xmethod.  Methods found in
   2573      1.1  christos    the binary are returned in METHODS, and xmethods are returned in
   2574      1.1  christos    XMETHODS.
   2575      1.1  christos 
   2576      1.1  christos    ARGP is a pointer to a pointer to a value (the object).
   2577  1.1.1.6  christos    METHOD is the method name.
   2578  1.1.1.6  christos    OFFSET is the offset within the value contents.
   2579  1.1.1.6  christos    METHODS is the list of matching overloaded instances defined in
   2580  1.1.1.2  christos       the source language.
   2581      1.1  christos    XMETHODS is the vector of matching xmethod workers defined in
   2582      1.1  christos       extension languages.
   2583      1.1  christos    BASETYPE is set to the type of the base subobject that defines the
   2584      1.1  christos       method.
   2585  1.1.1.2  christos    BOFFSET is the offset of the base subobject which defines the method.  */
   2586      1.1  christos 
   2587  1.1.1.6  christos static void
   2588  1.1.1.6  christos value_find_oload_method_list (struct value **argp, const char *method,
   2589  1.1.1.6  christos 			      LONGEST offset,
   2590  1.1.1.4  christos 			      gdb::array_view<fn_field> *methods,
   2591      1.1  christos 			      std::vector<xmethod_worker_up> *xmethods,
   2592      1.1  christos 			      struct type **basetype, LONGEST *boffset)
   2593      1.1  christos {
   2594      1.1  christos   struct type *t;
   2595      1.1  christos 
   2596      1.1  christos   t = check_typedef (value_type (*argp));
   2597  1.1.1.8  christos 
   2598      1.1  christos   /* Code snarfed from value_struct_elt.  */
   2599      1.1  christos   while (t->is_pointer_or_reference ())
   2600      1.1  christos     {
   2601  1.1.1.7  christos       *argp = value_ind (*argp);
   2602      1.1  christos       /* Don't coerce fn pointer to fn and then back again!  */
   2603      1.1  christos       if (check_typedef (value_type (*argp))->code () != TYPE_CODE_FUNC)
   2604      1.1  christos 	*argp = coerce_array (*argp);
   2605      1.1  christos       t = check_typedef (value_type (*argp));
   2606  1.1.1.7  christos     }
   2607  1.1.1.7  christos 
   2608      1.1  christos   if (t->code () != TYPE_CODE_STRUCT
   2609      1.1  christos       && t->code () != TYPE_CODE_UNION)
   2610      1.1  christos     error (_("Attempt to extract a component of a "
   2611  1.1.1.6  christos 	     "value that is not a struct or union"));
   2612  1.1.1.2  christos 
   2613  1.1.1.2  christos   gdb_assert (methods != NULL && xmethods != NULL);
   2614  1.1.1.6  christos 
   2615  1.1.1.6  christos   /* Clear the lists.  */
   2616  1.1.1.2  christos   *methods = {};
   2617  1.1.1.6  christos   xmethods->clear ();
   2618  1.1.1.2  christos 
   2619      1.1  christos   find_method_list (argp, method, 0, t, methods, xmethods,
   2620      1.1  christos 		    basetype, boffset);
   2621  1.1.1.8  christos }
   2622  1.1.1.8  christos 
   2623  1.1.1.8  christos /* Helper function for find_overload_match.  If no matches were
   2624  1.1.1.8  christos    found, this function may generate a hint for the user that some
   2625  1.1.1.8  christos    of the relevant types are incomplete, so GDB can't evaluate
   2626  1.1.1.8  christos    type relationships to properly evaluate overloads.
   2627  1.1.1.8  christos 
   2628  1.1.1.8  christos    If no incomplete types are present, an empty string is returned.  */
   2629  1.1.1.8  christos static std::string
   2630  1.1.1.8  christos incomplete_type_hint (gdb::array_view<value *> args)
   2631  1.1.1.8  christos {
   2632  1.1.1.8  christos   int incomplete_types = 0;
   2633  1.1.1.8  christos   std::string incomplete_arg_names;
   2634  1.1.1.8  christos   for (const struct value *arg : args)
   2635  1.1.1.8  christos     {
   2636  1.1.1.8  christos       struct type *t = value_type (arg);
   2637  1.1.1.8  christos       while (t->code () == TYPE_CODE_PTR)
   2638  1.1.1.8  christos 	t = t->target_type ();
   2639  1.1.1.8  christos       if (t->is_stub ())
   2640  1.1.1.8  christos 	{
   2641  1.1.1.8  christos 	  string_file buffer;
   2642  1.1.1.8  christos 	  if (incomplete_types > 0)
   2643  1.1.1.8  christos 	    incomplete_arg_names += ", ";
   2644  1.1.1.8  christos 
   2645  1.1.1.8  christos 	  current_language->print_type (value_type (arg), "", &buffer,
   2646  1.1.1.8  christos 				       -1, 0, &type_print_raw_options);
   2647  1.1.1.8  christos 
   2648  1.1.1.8  christos 	  incomplete_types++;
   2649  1.1.1.8  christos 	  incomplete_arg_names += buffer.string ();
   2650  1.1.1.8  christos 	}
   2651  1.1.1.8  christos     }
   2652  1.1.1.8  christos   std::string hint;
   2653  1.1.1.8  christos   if (incomplete_types > 1)
   2654  1.1.1.8  christos     hint = string_printf (_("\nThe types: '%s' aren't fully known to GDB."
   2655  1.1.1.8  christos 			    " Please cast them directly to the desired"
   2656  1.1.1.8  christos 			    " typed in the function call."),
   2657  1.1.1.8  christos 			    incomplete_arg_names.c_str ());
   2658  1.1.1.8  christos   else if (incomplete_types == 1)
   2659  1.1.1.8  christos     hint = string_printf (_("\nThe type: '%s' isn't fully known to GDB."
   2660  1.1.1.8  christos 			    " Please cast it directly to the desired"
   2661  1.1.1.8  christos 			    " typed in the function call."),
   2662  1.1.1.8  christos 			    incomplete_arg_names.c_str ());
   2663  1.1.1.8  christos   return hint;
   2664  1.1.1.6  christos }
   2665  1.1.1.6  christos 
   2666  1.1.1.6  christos /* Given an array of arguments (ARGS) (which includes an entry for
   2667  1.1.1.6  christos    "this" in the case of C++ methods), the NAME of a function, and
   2668  1.1.1.6  christos    whether it's a method or not (METHOD), find the best function that
   2669      1.1  christos    matches on the argument types according to the overload resolution
   2670      1.1  christos    rules.
   2671      1.1  christos 
   2672      1.1  christos    METHOD can be one of three values:
   2673      1.1  christos      NON_METHOD for non-member functions.
   2674      1.1  christos      METHOD: for member functions.
   2675      1.1  christos      BOTH: used for overload resolution of operators where the
   2676      1.1  christos        candidates are expected to be either member or non member
   2677      1.1  christos        functions.  In this case the first argument ARGTYPES
   2678      1.1  christos        (representing 'this') is expected to be a reference to the
   2679      1.1  christos        target object, and will be dereferenced when attempting the
   2680      1.1  christos        non-member search.
   2681      1.1  christos 
   2682      1.1  christos    In the case of class methods, the parameter OBJ is an object value
   2683      1.1  christos    in which to search for overloaded methods.
   2684      1.1  christos 
   2685      1.1  christos    In the case of non-method functions, the parameter FSYM is a symbol
   2686      1.1  christos    corresponding to one of the overloaded functions.
   2687      1.1  christos 
   2688      1.1  christos    Return value is an integer: 0 -> good match, 10 -> debugger applied
   2689      1.1  christos    non-standard coercions, 100 -> incompatible.
   2690      1.1  christos 
   2691      1.1  christos    If a method is being searched for, VALP will hold the value.
   2692      1.1  christos    If a non-method is being searched for, SYMP will hold the symbol
   2693      1.1  christos    for it.
   2694      1.1  christos 
   2695      1.1  christos    If a method is being searched for, and it is a static method,
   2696      1.1  christos    then STATICP will point to a non-zero value.
   2697      1.1  christos 
   2698      1.1  christos    If NO_ADL argument dependent lookup is disabled.  This is used to prevent
   2699      1.1  christos    ADL overload candidates when performing overload resolution for a fully
   2700  1.1.1.2  christos    qualified name.
   2701  1.1.1.2  christos 
   2702  1.1.1.2  christos    If NOSIDE is EVAL_AVOID_SIDE_EFFECTS, then OBJP's memory cannot be
   2703  1.1.1.2  christos    read while picking the best overload match (it may be all zeroes and thus
   2704  1.1.1.2  christos    not have a vtable pointer), in which case skip virtual function lookup.
   2705  1.1.1.2  christos    This is ok as typically EVAL_AVOID_SIDE_EFFECTS is only used to determine
   2706      1.1  christos    the result type.
   2707      1.1  christos 
   2708      1.1  christos    Note: This function does *not* check the value of
   2709      1.1  christos    overload_resolution.  Caller must check it to see whether overload
   2710      1.1  christos    resolution is permitted.  */
   2711  1.1.1.6  christos 
   2712      1.1  christos int
   2713      1.1  christos find_overload_match (gdb::array_view<value *> args,
   2714      1.1  christos 		     const char *name, enum oload_search_type method,
   2715  1.1.1.2  christos 		     struct value **objp, struct symbol *fsym,
   2716  1.1.1.2  christos 		     struct value **valp, struct symbol **symp,
   2717      1.1  christos 		     int *staticp, const int no_adl,
   2718      1.1  christos 		     const enum noside noside)
   2719      1.1  christos {
   2720      1.1  christos   struct value *obj = (objp ? *objp : NULL);
   2721      1.1  christos   struct type *obj_type = obj ? value_type (obj) : NULL;
   2722      1.1  christos   /* Index of best overloaded function.  */
   2723  1.1.1.2  christos   int func_oload_champ = -1;
   2724  1.1.1.2  christos   int method_oload_champ = -1;
   2725      1.1  christos   int src_method_oload_champ = -1;
   2726      1.1  christos   int ext_method_oload_champ = -1;
   2727  1.1.1.6  christos 
   2728  1.1.1.6  christos   /* The measure for the current best match.  */
   2729  1.1.1.6  christos   badness_vector method_badness;
   2730  1.1.1.6  christos   badness_vector func_badness;
   2731      1.1  christos   badness_vector ext_method_badness;
   2732      1.1  christos   badness_vector src_method_badness;
   2733      1.1  christos 
   2734  1.1.1.6  christos   struct value *temp = obj;
   2735      1.1  christos   /* For methods, the list of overloaded methods.  */
   2736  1.1.1.6  christos   gdb::array_view<fn_field> methods;
   2737  1.1.1.6  christos   /* For non-methods, the list of overloaded function symbols.  */
   2738  1.1.1.6  christos   std::vector<symbol *> functions;
   2739      1.1  christos   /* For xmethods, the vector of xmethod workers.  */
   2740  1.1.1.4  christos   std::vector<xmethod_worker_up> xmethods;
   2741      1.1  christos   struct type *basetype = NULL;
   2742      1.1  christos   LONGEST boffset;
   2743      1.1  christos 
   2744  1.1.1.6  christos   const char *obj_type_name = NULL;
   2745      1.1  christos   const char *func_name = NULL;
   2746      1.1  christos   gdb::unique_xmalloc_ptr<char> temp_func;
   2747  1.1.1.2  christos   enum oload_classification match_quality;
   2748  1.1.1.2  christos   enum oload_classification method_match_quality = INCOMPATIBLE;
   2749      1.1  christos   enum oload_classification src_method_match_quality = INCOMPATIBLE;
   2750      1.1  christos   enum oload_classification ext_method_match_quality = INCOMPATIBLE;
   2751      1.1  christos   enum oload_classification func_match_quality = INCOMPATIBLE;
   2752      1.1  christos 
   2753      1.1  christos   /* Get the list of overloaded methods or functions.  */
   2754      1.1  christos   if (method == METHOD || method == BOTH)
   2755      1.1  christos     {
   2756      1.1  christos       gdb_assert (obj);
   2757      1.1  christos 
   2758  1.1.1.7  christos       /* OBJ may be a pointer value rather than the object itself.  */
   2759      1.1  christos       obj = coerce_ref (obj);
   2760  1.1.1.7  christos       while (check_typedef (value_type (obj))->code () == TYPE_CODE_PTR)
   2761      1.1  christos 	obj = coerce_ref (value_ind (obj));
   2762      1.1  christos       obj_type_name = value_type (obj)->name ();
   2763      1.1  christos 
   2764  1.1.1.7  christos       /* First check whether this is a data member, e.g. a pointer to
   2765      1.1  christos 	 a function.  */
   2766  1.1.1.3  christos       if (check_typedef (value_type (obj))->code () == TYPE_CODE_STRUCT)
   2767      1.1  christos 	{
   2768      1.1  christos 	  *valp = search_struct_field (name, obj,
   2769      1.1  christos 				       check_typedef (value_type (obj)), 0);
   2770      1.1  christos 	  if (*valp)
   2771      1.1  christos 	    {
   2772      1.1  christos 	      *staticp = 1;
   2773      1.1  christos 	      return 0;
   2774      1.1  christos 	    }
   2775      1.1  christos 	}
   2776  1.1.1.6  christos 
   2777  1.1.1.6  christos       /* Retrieve the list of methods with the name NAME.  */
   2778      1.1  christos       value_find_oload_method_list (&temp, name, 0, &methods,
   2779  1.1.1.8  christos 				    &xmethods, &basetype, &boffset);
   2780  1.1.1.6  christos       /* If this is a method only search, and no methods were found
   2781      1.1  christos 	 the search has failed.  */
   2782      1.1  christos       if (method == METHOD && methods.empty () && xmethods.empty ())
   2783      1.1  christos 	error (_("Couldn't find method %s%s%s"),
   2784      1.1  christos 	       obj_type_name,
   2785      1.1  christos 	       (obj_type_name && *obj_type_name) ? "::" : "",
   2786      1.1  christos 	       name);
   2787      1.1  christos       /* If we are dealing with stub method types, they should have
   2788  1.1.1.6  christos 	 been resolved by find_method_list via
   2789      1.1  christos 	 value_find_oload_method_list above.  */
   2790  1.1.1.6  christos       if (!methods.empty ())
   2791      1.1  christos 	{
   2792  1.1.1.6  christos 	  gdb_assert (TYPE_SELF_TYPE (methods[0].type) != NULL);
   2793  1.1.1.6  christos 
   2794  1.1.1.6  christos 	  src_method_oload_champ
   2795  1.1.1.6  christos 	    = find_oload_champ (args,
   2796  1.1.1.6  christos 				methods.size (),
   2797  1.1.1.2  christos 				methods.data (), NULL, NULL,
   2798  1.1.1.2  christos 				&src_method_badness);
   2799  1.1.1.6  christos 
   2800  1.1.1.6  christos 	  src_method_match_quality = classify_oload_match
   2801  1.1.1.2  christos 	    (src_method_badness, args.size (),
   2802  1.1.1.2  christos 	     oload_method_static_p (methods.data (), src_method_oload_champ));
   2803  1.1.1.6  christos 	}
   2804  1.1.1.2  christos 
   2805  1.1.1.6  christos       if (!xmethods.empty ())
   2806  1.1.1.6  christos 	{
   2807  1.1.1.6  christos 	  ext_method_oload_champ
   2808  1.1.1.6  christos 	    = find_oload_champ (args,
   2809  1.1.1.6  christos 				xmethods.size (),
   2810  1.1.1.2  christos 				NULL, xmethods.data (), NULL,
   2811  1.1.1.6  christos 				&ext_method_badness);
   2812      1.1  christos 	  ext_method_match_quality = classify_oload_match (ext_method_badness,
   2813      1.1  christos 							   args.size (), 0);
   2814  1.1.1.2  christos 	}
   2815  1.1.1.2  christos 
   2816  1.1.1.2  christos       if (src_method_oload_champ >= 0 && ext_method_oload_champ >= 0)
   2817  1.1.1.2  christos 	{
   2818  1.1.1.2  christos 	  switch (compare_badness (ext_method_badness, src_method_badness))
   2819  1.1.1.2  christos 	    {
   2820  1.1.1.2  christos 	      case 0: /* Src method and xmethod are equally good.  */
   2821  1.1.1.2  christos 		/* If src method and xmethod are equally good, then
   2822  1.1.1.2  christos 		   xmethod should be the winner.  Hence, fall through to the
   2823  1.1.1.2  christos 		   case where a xmethod is better than the source
   2824  1.1.1.2  christos 		   method, except when the xmethod match quality is
   2825  1.1.1.2  christos 		   non-standard.  */
   2826  1.1.1.2  christos 		/* FALLTHROUGH */
   2827  1.1.1.2  christos 	      case 1: /* Src method and ext method are incompatible.  */
   2828  1.1.1.2  christos 		/* If ext method match is not standard, then let source method
   2829  1.1.1.2  christos 		   win.  Otherwise, fallthrough to let xmethod win.  */
   2830  1.1.1.2  christos 		if (ext_method_match_quality != STANDARD)
   2831  1.1.1.2  christos 		  {
   2832  1.1.1.2  christos 		    method_oload_champ = src_method_oload_champ;
   2833  1.1.1.2  christos 		    method_badness = src_method_badness;
   2834  1.1.1.2  christos 		    ext_method_oload_champ = -1;
   2835  1.1.1.2  christos 		    method_match_quality = src_method_match_quality;
   2836  1.1.1.2  christos 		    break;
   2837  1.1.1.2  christos 		  }
   2838  1.1.1.2  christos 		/* FALLTHROUGH */
   2839  1.1.1.2  christos 	      case 2: /* Ext method is champion.  */
   2840  1.1.1.2  christos 		method_oload_champ = ext_method_oload_champ;
   2841  1.1.1.2  christos 		method_badness = ext_method_badness;
   2842  1.1.1.2  christos 		src_method_oload_champ = -1;
   2843  1.1.1.2  christos 		method_match_quality = ext_method_match_quality;
   2844  1.1.1.2  christos 		break;
   2845  1.1.1.2  christos 	      case 3: /* Src method is champion.  */
   2846  1.1.1.2  christos 		method_oload_champ = src_method_oload_champ;
   2847  1.1.1.2  christos 		method_badness = src_method_badness;
   2848  1.1.1.2  christos 		ext_method_oload_champ = -1;
   2849  1.1.1.2  christos 		method_match_quality = src_method_match_quality;
   2850  1.1.1.2  christos 		break;
   2851  1.1.1.2  christos 	      default:
   2852  1.1.1.2  christos 		gdb_assert_not_reached ("Unexpected overload comparison "
   2853  1.1.1.2  christos 					"result");
   2854  1.1.1.2  christos 		break;
   2855  1.1.1.2  christos 	    }
   2856  1.1.1.2  christos 	}
   2857  1.1.1.2  christos       else if (src_method_oload_champ >= 0)
   2858  1.1.1.2  christos 	{
   2859  1.1.1.2  christos 	  method_oload_champ = src_method_oload_champ;
   2860  1.1.1.2  christos 	  method_badness = src_method_badness;
   2861  1.1.1.2  christos 	  method_match_quality = src_method_match_quality;
   2862  1.1.1.2  christos 	}
   2863  1.1.1.2  christos       else if (ext_method_oload_champ >= 0)
   2864  1.1.1.2  christos 	{
   2865  1.1.1.2  christos 	  method_oload_champ = ext_method_oload_champ;
   2866  1.1.1.2  christos 	  method_badness = ext_method_badness;
   2867      1.1  christos 	  method_match_quality = ext_method_match_quality;
   2868      1.1  christos 	}
   2869      1.1  christos     }
   2870      1.1  christos 
   2871      1.1  christos   if (method == NON_METHOD || method == BOTH)
   2872      1.1  christos     {
   2873      1.1  christos       const char *qualified_name = NULL;
   2874  1.1.1.8  christos 
   2875  1.1.1.8  christos       /* If the overload match is being search for both as a method
   2876      1.1  christos 	 and non member function, the first argument must now be
   2877      1.1  christos 	 dereferenced.  */
   2878      1.1  christos       if (method == BOTH)
   2879      1.1  christos 	args[0] = value_ind (args[0]);
   2880  1.1.1.8  christos 
   2881  1.1.1.8  christos       if (fsym)
   2882      1.1  christos 	{
   2883  1.1.1.8  christos 	  qualified_name = fsym->natural_name ();
   2884      1.1  christos 
   2885      1.1  christos 	  /* If we have a function with a C++ name, try to extract just
   2886  1.1.1.8  christos 	     the function part.  Do not try this for non-functions (e.g.
   2887  1.1.1.8  christos 	     function pointers).  */
   2888  1.1.1.7  christos 	  if (qualified_name
   2889  1.1.1.8  christos 	      && (check_typedef (fsym->type ())->code ()
   2890  1.1.1.6  christos 		  == TYPE_CODE_FUNC))
   2891      1.1  christos 	    {
   2892      1.1  christos 	      temp_func = cp_func_name (qualified_name);
   2893  1.1.1.8  christos 
   2894  1.1.1.8  christos 	      /* If cp_func_name did not remove anything, the name of the
   2895  1.1.1.6  christos 		 symbol did not include scope or argument types - it was
   2896      1.1  christos 		 probably a C-style function.  */
   2897  1.1.1.6  christos 	      if (temp_func != nullptr)
   2898      1.1  christos 		{
   2899      1.1  christos 		  if (strcmp (temp_func.get (), qualified_name) == 0)
   2900  1.1.1.6  christos 		    func_name = NULL;
   2901      1.1  christos 		  else
   2902  1.1.1.8  christos 		    func_name = temp_func.get ();
   2903  1.1.1.8  christos 		}
   2904      1.1  christos 	    }
   2905      1.1  christos 	}
   2906      1.1  christos       else
   2907      1.1  christos 	{
   2908      1.1  christos 	  func_name = name;
   2909      1.1  christos 	  qualified_name = name;
   2910      1.1  christos 	}
   2911      1.1  christos 
   2912      1.1  christos       /* If there was no C++ name, this must be a C-style function or
   2913      1.1  christos 	 not a function at all.  Just return the same symbol.  Do the
   2914  1.1.1.8  christos 	 same if cp_func_name fails for some reason.  */
   2915      1.1  christos       if (func_name == NULL)
   2916  1.1.1.8  christos 	{
   2917  1.1.1.8  christos 	  *symp = fsym;
   2918      1.1  christos 	  return 0;
   2919  1.1.1.6  christos 	}
   2920  1.1.1.8  christos 
   2921  1.1.1.8  christos       func_oload_champ = find_oload_champ_namespace (args,
   2922  1.1.1.8  christos 						     func_name,
   2923  1.1.1.8  christos 						     qualified_name,
   2924  1.1.1.8  christos 						     &functions,
   2925      1.1  christos 						     &func_badness,
   2926      1.1  christos 						     no_adl);
   2927  1.1.1.6  christos 
   2928  1.1.1.6  christos       if (func_oload_champ >= 0)
   2929      1.1  christos 	func_match_quality = classify_oload_match (func_badness,
   2930      1.1  christos 						   args.size (), 0);
   2931      1.1  christos     }
   2932      1.1  christos 
   2933      1.1  christos   /* Did we find a match ?  */
   2934  1.1.1.8  christos   if (method_oload_champ == -1 && func_oload_champ == -1)
   2935  1.1.1.8  christos     throw_error (NOT_FOUND_ERROR,
   2936      1.1  christos 		 _("No symbol \"%s\" in current context."),
   2937      1.1  christos 		 name);
   2938      1.1  christos 
   2939      1.1  christos   /* If we have found both a method match and a function
   2940      1.1  christos      match, find out which one is better, and calculate match
   2941      1.1  christos      quality.  */
   2942      1.1  christos   if (method_oload_champ >= 0 && func_oload_champ >= 0)
   2943  1.1.1.8  christos     {
   2944      1.1  christos       switch (compare_badness (func_badness, method_badness))
   2945      1.1  christos 	{
   2946      1.1  christos 	  case 0: /* Top two contenders are equally good.  */
   2947      1.1  christos 	    /* FIXME: GDB does not support the general ambiguous case.
   2948      1.1  christos 	     All candidates should be collected and presented the
   2949      1.1  christos 	     user.  */
   2950      1.1  christos 	    error (_("Ambiguous overload resolution"));
   2951      1.1  christos 	    break;
   2952      1.1  christos 	  case 1: /* Incomparable top contenders.  */
   2953      1.1  christos 	    /* This is an error incompatible candidates
   2954      1.1  christos 	       should not have been proposed.  */
   2955      1.1  christos 	    error (_("Internal error: incompatible "
   2956      1.1  christos 		     "overload candidates proposed"));
   2957      1.1  christos 	    break;
   2958      1.1  christos 	  case 2: /* Function champion.  */
   2959      1.1  christos 	    method_oload_champ = -1;
   2960      1.1  christos 	    match_quality = func_match_quality;
   2961      1.1  christos 	    break;
   2962      1.1  christos 	  case 3: /* Method champion.  */
   2963      1.1  christos 	    func_oload_champ = -1;
   2964      1.1  christos 	    match_quality = method_match_quality;
   2965      1.1  christos 	    break;
   2966      1.1  christos 	  default:
   2967  1.1.1.8  christos 	    error (_("Internal error: unexpected overload comparison result"));
   2968      1.1  christos 	    break;
   2969      1.1  christos 	}
   2970      1.1  christos     }
   2971      1.1  christos   else
   2972      1.1  christos     {
   2973      1.1  christos       /* We have either a method match or a function match.  */
   2974      1.1  christos       if (method_oload_champ >= 0)
   2975      1.1  christos 	match_quality = method_match_quality;
   2976      1.1  christos       else
   2977      1.1  christos 	match_quality = func_match_quality;
   2978      1.1  christos     }
   2979      1.1  christos 
   2980  1.1.1.8  christos   if (match_quality == INCOMPATIBLE)
   2981      1.1  christos     {
   2982  1.1.1.8  christos       std::string hint = incomplete_type_hint (args);
   2983      1.1  christos       if (method == METHOD)
   2984      1.1  christos 	error (_("Cannot resolve method %s%s%s to any overloaded instance%s"),
   2985  1.1.1.8  christos 	       obj_type_name,
   2986      1.1  christos 	       (obj_type_name && *obj_type_name) ? "::" : "",
   2987  1.1.1.8  christos 	       name, hint.c_str ());
   2988  1.1.1.8  christos       else
   2989      1.1  christos 	error (_("Cannot resolve function %s to any overloaded instance%s"),
   2990      1.1  christos 	       func_name, hint.c_str ());
   2991      1.1  christos     }
   2992      1.1  christos   else if (match_quality == NON_STANDARD)
   2993      1.1  christos     {
   2994      1.1  christos       if (method == METHOD)
   2995      1.1  christos 	warning (_("Using non-standard conversion to match "
   2996      1.1  christos 		   "method %s%s%s to supplied arguments"),
   2997      1.1  christos 		 obj_type_name,
   2998      1.1  christos 		 (obj_type_name && *obj_type_name) ? "::" : "",
   2999      1.1  christos 		 name);
   3000      1.1  christos       else
   3001      1.1  christos 	warning (_("Using non-standard conversion to match "
   3002      1.1  christos 		   "function %s to supplied arguments"),
   3003      1.1  christos 		 func_name);
   3004      1.1  christos     }
   3005  1.1.1.6  christos 
   3006      1.1  christos   if (staticp != NULL)
   3007      1.1  christos     *staticp = oload_method_static_p (methods.data (), method_oload_champ);
   3008      1.1  christos 
   3009  1.1.1.2  christos   if (method_oload_champ >= 0)
   3010  1.1.1.2  christos     {
   3011  1.1.1.6  christos       if (src_method_oload_champ >= 0)
   3012  1.1.1.2  christos 	{
   3013  1.1.1.2  christos 	  if (TYPE_FN_FIELD_VIRTUAL_P (methods, method_oload_champ)
   3014  1.1.1.6  christos 	      && noside != EVAL_AVOID_SIDE_EFFECTS)
   3015  1.1.1.2  christos 	    {
   3016  1.1.1.2  christos 	      *valp = value_virtual_fn_field (&temp, methods.data (),
   3017  1.1.1.2  christos 					      method_oload_champ, basetype,
   3018  1.1.1.2  christos 					      boffset);
   3019  1.1.1.6  christos 	    }
   3020  1.1.1.6  christos 	  else
   3021  1.1.1.2  christos 	    *valp = value_fn_field (&temp, methods.data (),
   3022      1.1  christos 				    method_oload_champ, basetype, boffset);
   3023  1.1.1.6  christos 	}
   3024  1.1.1.6  christos       else
   3025      1.1  christos 	*valp = value_from_xmethod
   3026      1.1  christos 	  (std::move (xmethods[ext_method_oload_champ]));
   3027  1.1.1.6  christos     }
   3028      1.1  christos   else
   3029      1.1  christos     *symp = functions[func_oload_champ];
   3030      1.1  christos 
   3031      1.1  christos   if (objp)
   3032      1.1  christos     {
   3033      1.1  christos       struct type *temp_type = check_typedef (value_type (temp));
   3034  1.1.1.7  christos       struct type *objtype = check_typedef (obj_type);
   3035  1.1.1.8  christos 
   3036      1.1  christos       if (temp_type->code () != TYPE_CODE_PTR
   3037      1.1  christos 	  && objtype->is_pointer_or_reference ())
   3038      1.1  christos 	{
   3039      1.1  christos 	  temp = value_addr (temp);
   3040      1.1  christos 	}
   3041      1.1  christos       *objp = temp;
   3042      1.1  christos     }
   3043      1.1  christos 
   3044      1.1  christos   switch (match_quality)
   3045      1.1  christos     {
   3046      1.1  christos     case INCOMPATIBLE:
   3047      1.1  christos       return 100;
   3048      1.1  christos     case NON_STANDARD:
   3049      1.1  christos       return 10;
   3050      1.1  christos     default:				/* STANDARD */
   3051      1.1  christos       return 0;
   3052      1.1  christos     }
   3053      1.1  christos }
   3054      1.1  christos 
   3055      1.1  christos /* Find the best overload match, searching for FUNC_NAME in namespaces
   3056  1.1.1.6  christos    contained in QUALIFIED_NAME until it either finds a good match or
   3057  1.1.1.7  christos    runs out of namespaces.  It stores the overloaded functions in
   3058      1.1  christos    *OLOAD_SYMS, and the badness vector in *OLOAD_CHAMP_BV.  If NO_ADL,
   3059      1.1  christos    argument dependent lookup is not performed.  */
   3060  1.1.1.6  christos 
   3061      1.1  christos static int
   3062      1.1  christos find_oload_champ_namespace (gdb::array_view<value *> args,
   3063  1.1.1.6  christos 			    const char *func_name,
   3064  1.1.1.6  christos 			    const char *qualified_name,
   3065      1.1  christos 			    std::vector<symbol *> *oload_syms,
   3066      1.1  christos 			    badness_vector *oload_champ_bv,
   3067      1.1  christos 			    const int no_adl)
   3068      1.1  christos {
   3069  1.1.1.6  christos   int oload_champ;
   3070      1.1  christos 
   3071      1.1  christos   find_oload_champ_namespace_loop (args,
   3072      1.1  christos 				   func_name,
   3073      1.1  christos 				   qualified_name, 0,
   3074      1.1  christos 				   oload_syms, oload_champ_bv,
   3075      1.1  christos 				   &oload_champ,
   3076      1.1  christos 				   no_adl);
   3077      1.1  christos 
   3078      1.1  christos   return oload_champ;
   3079      1.1  christos }
   3080      1.1  christos 
   3081      1.1  christos /* Helper function for find_oload_champ_namespace; NAMESPACE_LEN is
   3082      1.1  christos    how deep we've looked for namespaces, and the champ is stored in
   3083  1.1.1.6  christos    OLOAD_CHAMP.  The return value is 1 if the champ is a good one, 0
   3084      1.1  christos    if it isn't.  Other arguments are the same as in
   3085      1.1  christos    find_oload_champ_namespace.  */
   3086  1.1.1.6  christos 
   3087      1.1  christos static int
   3088      1.1  christos find_oload_champ_namespace_loop (gdb::array_view<value *> args,
   3089      1.1  christos 				 const char *func_name,
   3090  1.1.1.6  christos 				 const char *qualified_name,
   3091  1.1.1.6  christos 				 int namespace_len,
   3092      1.1  christos 				 std::vector<symbol *> *oload_syms,
   3093      1.1  christos 				 badness_vector *oload_champ_bv,
   3094      1.1  christos 				 int *oload_champ,
   3095      1.1  christos 				 const int no_adl)
   3096      1.1  christos {
   3097      1.1  christos   int next_namespace_len = namespace_len;
   3098      1.1  christos   int searched_deeper = 0;
   3099      1.1  christos   int new_oload_champ;
   3100      1.1  christos   char *new_namespace;
   3101      1.1  christos 
   3102      1.1  christos   if (next_namespace_len != 0)
   3103      1.1  christos     {
   3104      1.1  christos       gdb_assert (qualified_name[next_namespace_len] == ':');
   3105      1.1  christos       next_namespace_len +=  2;
   3106      1.1  christos     }
   3107      1.1  christos   next_namespace_len +=
   3108      1.1  christos     cp_find_first_component (qualified_name + next_namespace_len);
   3109      1.1  christos 
   3110      1.1  christos   /* First, see if we have a deeper namespace we can search in.
   3111      1.1  christos      If we get a good match there, use it.  */
   3112      1.1  christos 
   3113      1.1  christos   if (qualified_name[next_namespace_len] == ':')
   3114      1.1  christos     {
   3115  1.1.1.6  christos       searched_deeper = 1;
   3116      1.1  christos 
   3117      1.1  christos       if (find_oload_champ_namespace_loop (args,
   3118      1.1  christos 					   func_name, qualified_name,
   3119      1.1  christos 					   next_namespace_len,
   3120      1.1  christos 					   oload_syms, oload_champ_bv,
   3121      1.1  christos 					   oload_champ, no_adl))
   3122      1.1  christos 	{
   3123      1.1  christos 	  return 1;
   3124      1.1  christos 	}
   3125      1.1  christos     };
   3126      1.1  christos 
   3127      1.1  christos   /* If we reach here, either we're in the deepest namespace or we
   3128      1.1  christos      didn't find a good match in a deeper namespace.  But, in the
   3129      1.1  christos      latter case, we still have a bad match in a deeper namespace;
   3130      1.1  christos      note that we might not find any match at all in the current
   3131      1.1  christos      namespace.  (There's always a match in the deepest namespace,
   3132      1.1  christos      because this overload mechanism only gets called if there's a
   3133  1.1.1.4  christos      function symbol to start off with.)  */
   3134      1.1  christos 
   3135      1.1  christos   new_namespace = (char *) alloca (namespace_len + 1);
   3136  1.1.1.6  christos   strncpy (new_namespace, qualified_name, namespace_len);
   3137  1.1.1.6  christos   new_namespace[namespace_len] = '\0';
   3138  1.1.1.6  christos 
   3139      1.1  christos   std::vector<symbol *> new_oload_syms
   3140      1.1  christos     = make_symbol_overload_list (func_name, new_namespace);
   3141      1.1  christos 
   3142      1.1  christos   /* If we have reached the deepest level perform argument
   3143      1.1  christos      determined lookup.  */
   3144      1.1  christos   if (!searched_deeper && !no_adl)
   3145      1.1  christos     {
   3146      1.1  christos       int ix;
   3147      1.1  christos       struct type **arg_types;
   3148      1.1  christos 
   3149  1.1.1.6  christos       /* Prepare list of argument types for overload resolution.  */
   3150  1.1.1.6  christos       arg_types = (struct type **)
   3151      1.1  christos 	alloca (args.size () * (sizeof (struct type *)));
   3152  1.1.1.6  christos       for (ix = 0; ix < args.size (); ix++)
   3153  1.1.1.6  christos 	arg_types[ix] = value_type (args[ix]);
   3154      1.1  christos       add_symbol_overload_list_adl ({arg_types, args.size ()}, func_name,
   3155      1.1  christos 				    &new_oload_syms);
   3156  1.1.1.6  christos     }
   3157  1.1.1.6  christos 
   3158  1.1.1.6  christos   badness_vector new_oload_champ_bv;
   3159  1.1.1.6  christos   new_oload_champ = find_oload_champ (args,
   3160      1.1  christos 				      new_oload_syms.size (),
   3161      1.1  christos 				      NULL, NULL, new_oload_syms.data (),
   3162      1.1  christos 				      &new_oload_champ_bv);
   3163      1.1  christos 
   3164      1.1  christos   /* Case 1: We found a good match.  Free earlier matches (if any),
   3165      1.1  christos      and return it.  Case 2: We didn't find a good match, but we're
   3166      1.1  christos      not the deepest function.  Then go with the bad match that the
   3167      1.1  christos      deeper function found.  Case 3: We found a bad match, and we're
   3168      1.1  christos      the deepest function.  Then return what we found, even though
   3169      1.1  christos      it's a bad match.  */
   3170  1.1.1.6  christos 
   3171      1.1  christos   if (new_oload_champ != -1
   3172  1.1.1.6  christos       && classify_oload_match (new_oload_champ_bv, args.size (), 0) == STANDARD)
   3173      1.1  christos     {
   3174  1.1.1.6  christos       *oload_syms = std::move (new_oload_syms);
   3175      1.1  christos       *oload_champ = new_oload_champ;
   3176      1.1  christos       *oload_champ_bv = std::move (new_oload_champ_bv);
   3177      1.1  christos       return 1;
   3178      1.1  christos     }
   3179      1.1  christos   else if (searched_deeper)
   3180      1.1  christos     {
   3181      1.1  christos       return 0;
   3182      1.1  christos     }
   3183  1.1.1.6  christos   else
   3184      1.1  christos     {
   3185  1.1.1.6  christos       *oload_syms = std::move (new_oload_syms);
   3186      1.1  christos       *oload_champ = new_oload_champ;
   3187      1.1  christos       *oload_champ_bv = std::move (new_oload_champ_bv);
   3188      1.1  christos       return 0;
   3189      1.1  christos     }
   3190  1.1.1.6  christos }
   3191  1.1.1.6  christos 
   3192  1.1.1.6  christos /* Look for a function to take ARGS.  Find the best match from among
   3193  1.1.1.6  christos    the overloaded methods or functions given by METHODS or FUNCTIONS
   3194  1.1.1.2  christos    or XMETHODS, respectively.  One, and only one of METHODS, FUNCTIONS
   3195  1.1.1.6  christos    and XMETHODS can be non-NULL.
   3196  1.1.1.6  christos 
   3197  1.1.1.2  christos    NUM_FNS is the length of the array pointed at by METHODS, FUNCTIONS
   3198      1.1  christos    or XMETHODS, whichever is non-NULL.
   3199  1.1.1.6  christos 
   3200      1.1  christos    Return the index of the best match; store an indication of the
   3201      1.1  christos    quality of the match in OLOAD_CHAMP_BV.  */
   3202  1.1.1.6  christos 
   3203  1.1.1.6  christos static int
   3204  1.1.1.6  christos find_oload_champ (gdb::array_view<value *> args,
   3205  1.1.1.6  christos 		  size_t num_fns,
   3206  1.1.1.6  christos 		  fn_field *methods,
   3207  1.1.1.6  christos 		  xmethod_worker_up *xmethods,
   3208      1.1  christos 		  symbol **functions,
   3209      1.1  christos 		  badness_vector *oload_champ_bv)
   3210  1.1.1.6  christos {
   3211      1.1  christos   /* A measure of how good an overloaded instance is.  */
   3212      1.1  christos   badness_vector bv;
   3213      1.1  christos   /* Index of best overloaded function.  */
   3214      1.1  christos   int oload_champ = -1;
   3215      1.1  christos   /* Current ambiguity state for overload resolution.  */
   3216      1.1  christos   int oload_ambiguous = 0;
   3217  1.1.1.2  christos   /* 0 => no ambiguity, 1 => two good funcs, 2 => incomparable funcs.  */
   3218  1.1.1.2  christos 
   3219  1.1.1.2  christos   /* A champion can be found among methods alone, or among functions
   3220  1.1.1.6  christos      alone, or in xmethods alone, but not in more than one of these
   3221  1.1.1.2  christos      groups.  */
   3222  1.1.1.2  christos   gdb_assert ((methods != NULL) + (functions != NULL) + (xmethods != NULL)
   3223      1.1  christos 	      == 1);
   3224  1.1.1.6  christos 
   3225      1.1  christos   /* Consider each candidate in turn.  */
   3226      1.1  christos   for (size_t ix = 0; ix < num_fns; ix++)
   3227  1.1.1.2  christos     {
   3228  1.1.1.6  christos       int jj;
   3229      1.1  christos       int static_offset = 0;
   3230  1.1.1.6  christos       std::vector<type *> parm_types;
   3231  1.1.1.6  christos 
   3232      1.1  christos       if (xmethods != NULL)
   3233      1.1  christos 	parm_types = xmethods[ix]->get_arg_types ();
   3234  1.1.1.6  christos       else
   3235  1.1.1.6  christos 	{
   3236  1.1.1.6  christos 	  size_t nparms;
   3237  1.1.1.2  christos 
   3238  1.1.1.7  christos 	  if (methods != NULL)
   3239  1.1.1.6  christos 	    {
   3240  1.1.1.2  christos 	      nparms = TYPE_FN_FIELD_TYPE (methods, ix)->num_fields ();
   3241  1.1.1.2  christos 	      static_offset = oload_method_static_p (methods, ix);
   3242  1.1.1.8  christos 	    }
   3243      1.1  christos 	  else
   3244  1.1.1.6  christos 	    nparms = functions[ix]->type ()->num_fields ();
   3245  1.1.1.2  christos 
   3246  1.1.1.6  christos 	  parm_types.reserve (nparms);
   3247  1.1.1.6  christos 	  for (jj = 0; jj < nparms; jj++)
   3248  1.1.1.7  christos 	    {
   3249  1.1.1.8  christos 	      type *t = (methods != NULL
   3250  1.1.1.6  christos 			 ? (TYPE_FN_FIELD_ARGS (methods, ix)[jj].type ())
   3251  1.1.1.6  christos 			 : functions[ix]->type ()->field (jj).type ());
   3252  1.1.1.2  christos 	      parm_types.push_back (t);
   3253      1.1  christos 	    }
   3254      1.1  christos 	}
   3255  1.1.1.8  christos 
   3256  1.1.1.6  christos       /* Compare parameter types to supplied argument types.  Skip
   3257  1.1.1.6  christos 	 THIS for static methods.  */
   3258      1.1  christos       bv = rank_function (parm_types,
   3259  1.1.1.7  christos 			  args.slice (static_offset));
   3260  1.1.1.7  christos 
   3261  1.1.1.7  christos       if (overload_debug)
   3262  1.1.1.8  christos 	{
   3263  1.1.1.8  christos 	  if (methods != NULL)
   3264  1.1.1.8  christos 	    gdb_printf (gdb_stderr,
   3265  1.1.1.7  christos 			"Overloaded method instance %s, # of parms %d\n",
   3266  1.1.1.8  christos 			methods[ix].physname, (int) parm_types.size ());
   3267  1.1.1.8  christos 	  else if (xmethods != NULL)
   3268  1.1.1.8  christos 	    gdb_printf (gdb_stderr,
   3269  1.1.1.7  christos 			"Xmethod worker, # of parms %d\n",
   3270  1.1.1.8  christos 			(int) parm_types.size ());
   3271  1.1.1.8  christos 	  else
   3272  1.1.1.8  christos 	    gdb_printf (gdb_stderr,
   3273  1.1.1.8  christos 			"Overloaded function instance "
   3274  1.1.1.8  christos 			"%s # of parms %d\n",
   3275  1.1.1.8  christos 			functions[ix]->demangled_name (),
   3276  1.1.1.8  christos 			(int) parm_types.size ());
   3277  1.1.1.8  christos 
   3278  1.1.1.8  christos 	  gdb_printf (gdb_stderr,
   3279  1.1.1.7  christos 		      "...Badness of length : {%d, %d}\n",
   3280  1.1.1.7  christos 		      bv[0].rank, bv[0].subrank);
   3281  1.1.1.8  christos 
   3282  1.1.1.8  christos 	  for (jj = 1; jj < bv.size (); jj++)
   3283  1.1.1.8  christos 	    gdb_printf (gdb_stderr,
   3284  1.1.1.7  christos 			"...Badness of arg %d : {%d, %d}\n",
   3285  1.1.1.7  christos 			jj, bv[jj].rank, bv[jj].subrank);
   3286  1.1.1.6  christos 	}
   3287      1.1  christos 
   3288  1.1.1.6  christos       if (oload_champ_bv->empty ())
   3289      1.1  christos 	{
   3290      1.1  christos 	  *oload_champ_bv = std::move (bv);
   3291      1.1  christos 	  oload_champ = 0;
   3292      1.1  christos 	}
   3293      1.1  christos       else /* See whether current candidate is better or worse than
   3294      1.1  christos 	      previous best.  */
   3295      1.1  christos 	switch (compare_badness (bv, *oload_champ_bv))
   3296      1.1  christos 	  {
   3297      1.1  christos 	  case 0:		/* Top two contenders are equally good.  */
   3298      1.1  christos 	    oload_ambiguous = 1;
   3299      1.1  christos 	    break;
   3300      1.1  christos 	  case 1:		/* Incomparable top contenders.  */
   3301      1.1  christos 	    oload_ambiguous = 2;
   3302  1.1.1.6  christos 	    break;
   3303      1.1  christos 	  case 2:		/* New champion, record details.  */
   3304      1.1  christos 	    *oload_champ_bv = std::move (bv);
   3305      1.1  christos 	    oload_ambiguous = 0;
   3306      1.1  christos 	    oload_champ = ix;
   3307      1.1  christos 	    break;
   3308      1.1  christos 	  case 3:
   3309      1.1  christos 	  default:
   3310      1.1  christos 	    break;
   3311  1.1.1.8  christos 	  }
   3312  1.1.1.8  christos       if (overload_debug)
   3313  1.1.1.8  christos 	gdb_printf (gdb_stderr, "Overload resolution "
   3314      1.1  christos 		    "champion is %d, ambiguous? %d\n",
   3315      1.1  christos 		    oload_champ, oload_ambiguous);
   3316      1.1  christos     }
   3317      1.1  christos 
   3318      1.1  christos   return oload_champ;
   3319      1.1  christos }
   3320      1.1  christos 
   3321      1.1  christos /* Return 1 if we're looking at a static method, 0 if we're looking at
   3322      1.1  christos    a non-static method or a function that isn't a method.  */
   3323  1.1.1.2  christos 
   3324      1.1  christos static int
   3325  1.1.1.2  christos oload_method_static_p (struct fn_field *fns_ptr, int index)
   3326      1.1  christos {
   3327      1.1  christos   if (fns_ptr && index >= 0 && TYPE_FN_FIELD_STATIC_P (fns_ptr, index))
   3328      1.1  christos     return 1;
   3329      1.1  christos   else
   3330      1.1  christos     return 0;
   3331      1.1  christos }
   3332      1.1  christos 
   3333      1.1  christos /* Check how good an overload match OLOAD_CHAMP_BV represents.  */
   3334  1.1.1.6  christos 
   3335      1.1  christos static enum oload_classification
   3336      1.1  christos classify_oload_match (const badness_vector &oload_champ_bv,
   3337      1.1  christos 		      int nargs,
   3338      1.1  christos 		      int static_offset)
   3339      1.1  christos {
   3340      1.1  christos   int ix;
   3341      1.1  christos   enum oload_classification worst = STANDARD;
   3342      1.1  christos 
   3343      1.1  christos   for (ix = 1; ix <= nargs - static_offset; ix++)
   3344  1.1.1.8  christos     {
   3345  1.1.1.6  christos       /* If this conversion is as bad as INCOMPATIBLE_TYPE_BADNESS
   3346  1.1.1.8  christos 	 or worse return INCOMPATIBLE.  */
   3347      1.1  christos       if (compare_ranks (oload_champ_bv[ix],
   3348      1.1  christos 			 INCOMPATIBLE_TYPE_BADNESS) <= 0)
   3349  1.1.1.8  christos 	return INCOMPATIBLE;	/* Truly mismatched types.  */
   3350  1.1.1.6  christos       /* Otherwise If this conversion is as bad as
   3351  1.1.1.8  christos 	 NS_POINTER_CONVERSION_BADNESS or worse return NON_STANDARD.  */
   3352      1.1  christos       else if (compare_ranks (oload_champ_bv[ix],
   3353      1.1  christos 			      NS_POINTER_CONVERSION_BADNESS) <= 0)
   3354      1.1  christos 	worst = NON_STANDARD;	/* Non-standard type conversions
   3355      1.1  christos 				   needed.  */
   3356      1.1  christos     }
   3357      1.1  christos 
   3358      1.1  christos   /* If no INCOMPATIBLE classification was found, return the worst one
   3359      1.1  christos      that was found (if any).  */
   3360      1.1  christos   return worst;
   3361      1.1  christos }
   3362      1.1  christos 
   3363      1.1  christos /* C++: return 1 is NAME is a legitimate name for the destructor of
   3364      1.1  christos    type TYPE.  If TYPE does not have a destructor, or if NAME is
   3365      1.1  christos    inappropriate for TYPE, an error is signaled.  Parameter TYPE should not yet
   3366      1.1  christos    have CHECK_TYPEDEF applied, this function will apply it itself.  */
   3367      1.1  christos 
   3368      1.1  christos int
   3369      1.1  christos destructor_name_p (const char *name, struct type *type)
   3370      1.1  christos {
   3371  1.1.1.6  christos   if (name[0] == '~')
   3372      1.1  christos     {
   3373      1.1  christos       const char *dname = type_name_or_error (type);
   3374      1.1  christos       const char *cp = strchr (dname, '<');
   3375      1.1  christos       unsigned int len;
   3376      1.1  christos 
   3377      1.1  christos       /* Do not compare the template part for template classes.  */
   3378      1.1  christos       if (cp == NULL)
   3379      1.1  christos 	len = strlen (dname);
   3380      1.1  christos       else
   3381      1.1  christos 	len = cp - dname;
   3382      1.1  christos       if (strlen (name + 1) != len || strncmp (dname, name + 1, len) != 0)
   3383      1.1  christos 	error (_("name of destructor must equal name of class"));
   3384      1.1  christos       else
   3385      1.1  christos 	return 1;
   3386      1.1  christos     }
   3387      1.1  christos   return 0;
   3388  1.1.1.2  christos }
   3389  1.1.1.2  christos 
   3390  1.1.1.2  christos /* Find an enum constant named NAME in TYPE.  TYPE must be an "enum
   3391  1.1.1.2  christos    class".  If the name is found, return a value representing it;
   3392  1.1.1.2  christos    otherwise throw an exception.  */
   3393  1.1.1.2  christos 
   3394  1.1.1.2  christos static struct value *
   3395  1.1.1.2  christos enum_constant_from_type (struct type *type, const char *name)
   3396  1.1.1.2  christos {
   3397  1.1.1.2  christos   int i;
   3398  1.1.1.7  christos   int name_len = strlen (name);
   3399  1.1.1.8  christos 
   3400  1.1.1.2  christos   gdb_assert (type->code () == TYPE_CODE_ENUM
   3401  1.1.1.7  christos 	      && type->is_declared_class ());
   3402  1.1.1.2  christos 
   3403  1.1.1.8  christos   for (i = TYPE_N_BASECLASSES (type); i < type->num_fields (); ++i)
   3404  1.1.1.2  christos     {
   3405  1.1.1.2  christos       const char *fname = type->field (i).name ();
   3406  1.1.1.8  christos       int len;
   3407  1.1.1.2  christos 
   3408  1.1.1.2  christos       if (type->field (i).loc_kind () != FIELD_LOC_KIND_ENUMVAL
   3409  1.1.1.2  christos 	  || fname == NULL)
   3410  1.1.1.2  christos 	continue;
   3411  1.1.1.2  christos 
   3412  1.1.1.2  christos       /* Look for the trailing "::NAME", since enum class constant
   3413  1.1.1.2  christos 	 names are qualified here.  */
   3414  1.1.1.2  christos       len = strlen (fname);
   3415  1.1.1.2  christos       if (len + 2 >= name_len
   3416  1.1.1.2  christos 	  && fname[len - name_len - 2] == ':'
   3417  1.1.1.8  christos 	  && fname[len - name_len - 1] == ':'
   3418  1.1.1.2  christos 	  && strcmp (&fname[len - name_len], name) == 0)
   3419  1.1.1.2  christos 	return value_from_longest (type, type->field (i).loc_enumval ());
   3420  1.1.1.2  christos     }
   3421  1.1.1.7  christos 
   3422  1.1.1.2  christos   error (_("no constant named \"%s\" in enum \"%s\""),
   3423  1.1.1.2  christos 	 name, type->name ());
   3424      1.1  christos }
   3425      1.1  christos 
   3426      1.1  christos /* C++: Given an aggregate type CURTYPE, and a member name NAME,
   3427      1.1  christos    return the appropriate member (or the address of the member, if
   3428      1.1  christos    WANT_ADDRESS).  This function is used to resolve user expressions
   3429      1.1  christos    of the form "DOMAIN::NAME".  For more details on what happens, see
   3430      1.1  christos    the comment before value_struct_elt_for_reference.  */
   3431  1.1.1.2  christos 
   3432      1.1  christos struct value *
   3433      1.1  christos value_aggregate_elt (struct type *curtype, const char *name,
   3434      1.1  christos 		     struct type *expect_type, int want_address,
   3435  1.1.1.7  christos 		     enum noside noside)
   3436      1.1  christos {
   3437      1.1  christos   switch (curtype->code ())
   3438      1.1  christos     {
   3439      1.1  christos     case TYPE_CODE_STRUCT:
   3440      1.1  christos     case TYPE_CODE_UNION:
   3441      1.1  christos       return value_struct_elt_for_reference (curtype, 0, curtype,
   3442      1.1  christos 					     name, expect_type,
   3443      1.1  christos 					     want_address, noside);
   3444      1.1  christos     case TYPE_CODE_NAMESPACE:
   3445  1.1.1.2  christos       return value_namespace_elt (curtype, name,
   3446  1.1.1.2  christos 				  want_address, noside);
   3447  1.1.1.2  christos 
   3448  1.1.1.2  christos     case TYPE_CODE_ENUM:
   3449      1.1  christos       return enum_constant_from_type (curtype, name);
   3450  1.1.1.8  christos 
   3451      1.1  christos     default:
   3452      1.1  christos       internal_error (_("non-aggregate type in value_aggregate_elt"));
   3453      1.1  christos     }
   3454      1.1  christos }
   3455      1.1  christos 
   3456      1.1  christos /* Compares the two method/function types T1 and T2 for "equality"
   3457      1.1  christos    with respect to the methods' parameters.  If the types of the
   3458      1.1  christos    two parameter lists are the same, returns 1; 0 otherwise.  This
   3459      1.1  christos    comparison may ignore any artificial parameters in T1 if
   3460      1.1  christos    SKIP_ARTIFICIAL is non-zero.  This function will ALWAYS skip
   3461      1.1  christos    the first artificial parameter in T1, assumed to be a 'this' pointer.
   3462      1.1  christos 
   3463      1.1  christos    The type T2 is expected to have come from make_params (in eval.c).  */
   3464      1.1  christos 
   3465      1.1  christos static int
   3466      1.1  christos compare_parameters (struct type *t1, struct type *t2, int skip_artificial)
   3467      1.1  christos {
   3468  1.1.1.7  christos   int start = 0;
   3469      1.1  christos 
   3470      1.1  christos   if (t1->num_fields () > 0 && TYPE_FIELD_ARTIFICIAL (t1, 0))
   3471      1.1  christos     ++start;
   3472      1.1  christos 
   3473      1.1  christos   /* If skipping artificial fields, find the first real field
   3474      1.1  christos      in T1.  */
   3475  1.1.1.7  christos   if (skip_artificial)
   3476      1.1  christos     {
   3477      1.1  christos       while (start < t1->num_fields ()
   3478      1.1  christos 	     && TYPE_FIELD_ARTIFICIAL (t1, start))
   3479      1.1  christos 	++start;
   3480      1.1  christos     }
   3481      1.1  christos 
   3482      1.1  christos   /* Now compare parameters.  */
   3483      1.1  christos 
   3484  1.1.1.7  christos   /* Special case: a method taking void.  T1 will contain no
   3485  1.1.1.7  christos      non-artificial fields, and T2 will contain TYPE_CODE_VOID.  */
   3486      1.1  christos   if ((t1->num_fields () - start) == 0 && t2->num_fields () == 1
   3487      1.1  christos       && t2->field (0).type ()->code () == TYPE_CODE_VOID)
   3488  1.1.1.7  christos     return 1;
   3489      1.1  christos 
   3490      1.1  christos   if ((t1->num_fields () - start) == t2->num_fields ())
   3491      1.1  christos     {
   3492  1.1.1.7  christos       int i;
   3493      1.1  christos 
   3494  1.1.1.7  christos       for (i = 0; i < t2->num_fields (); ++i)
   3495  1.1.1.7  christos 	{
   3496  1.1.1.8  christos 	  if (compare_ranks (rank_one_type (t1->field (start + i).type (),
   3497      1.1  christos 					    t2->field (i).type (), NULL),
   3498      1.1  christos 			     EXACT_MATCH_BADNESS) != 0)
   3499      1.1  christos 	    return 0;
   3500      1.1  christos 	}
   3501      1.1  christos 
   3502      1.1  christos       return 1;
   3503      1.1  christos     }
   3504      1.1  christos 
   3505      1.1  christos   return 0;
   3506  1.1.1.6  christos }
   3507  1.1.1.6  christos 
   3508  1.1.1.6  christos /* C++: Given an aggregate type VT, and a class type CLS, search
   3509  1.1.1.6  christos    recursively for CLS using value V; If found, store the offset
   3510  1.1.1.6  christos    which is either fetched from the virtual base pointer if CLS
   3511  1.1.1.6  christos    is virtual or accumulated offset of its parent classes if
   3512  1.1.1.6  christos    CLS is non-virtual in *BOFFS, set ISVIRT to indicate if CLS
   3513  1.1.1.6  christos    is virtual, and return true.  If not found, return false.  */
   3514  1.1.1.6  christos 
   3515  1.1.1.6  christos static bool
   3516  1.1.1.6  christos get_baseclass_offset (struct type *vt, struct type *cls,
   3517  1.1.1.6  christos 		      struct value *v, int *boffs, bool *isvirt)
   3518  1.1.1.6  christos {
   3519  1.1.1.7  christos   for (int i = 0; i < TYPE_N_BASECLASSES (vt); i++)
   3520  1.1.1.6  christos     {
   3521  1.1.1.8  christos       struct type *t = vt->field (i).type ();
   3522  1.1.1.8  christos       if (types_equal (t, cls))
   3523  1.1.1.8  christos 	{
   3524  1.1.1.8  christos 	  if (BASETYPE_VIA_VIRTUAL (vt, i))
   3525  1.1.1.6  christos 	    {
   3526  1.1.1.6  christos 	      const gdb_byte *adr = value_contents_for_printing (v).data ();
   3527  1.1.1.6  christos 	      *boffs = baseclass_offset (vt, i, adr, value_offset (v),
   3528  1.1.1.8  christos 					 value_as_long (v), v);
   3529  1.1.1.8  christos 	      *isvirt = true;
   3530  1.1.1.6  christos 	    }
   3531  1.1.1.8  christos 	  else
   3532  1.1.1.8  christos 	    *isvirt = false;
   3533  1.1.1.6  christos 	  return true;
   3534  1.1.1.6  christos 	}
   3535  1.1.1.8  christos 
   3536  1.1.1.6  christos       if (get_baseclass_offset (check_typedef (t), cls, v, boffs, isvirt))
   3537  1.1.1.6  christos 	{
   3538  1.1.1.8  christos 	  if (*isvirt == false)	/* Add non-virtual base offset.  */
   3539  1.1.1.6  christos 	    {
   3540  1.1.1.6  christos 	      const gdb_byte *adr = value_contents_for_printing (v).data ();
   3541  1.1.1.6  christos 	      *boffs += baseclass_offset (vt, i, adr, value_offset (v),
   3542  1.1.1.6  christos 					  value_as_long (v), v);
   3543  1.1.1.6  christos 	    }
   3544  1.1.1.6  christos 	  return true;
   3545  1.1.1.6  christos 	}
   3546  1.1.1.6  christos     }
   3547  1.1.1.6  christos 
   3548  1.1.1.6  christos   return false;
   3549      1.1  christos }
   3550      1.1  christos 
   3551      1.1  christos /* C++: Given an aggregate type CURTYPE, and a member name NAME,
   3552      1.1  christos    return the address of this member as a "pointer to member" type.
   3553      1.1  christos    If INTYPE is non-null, then it will be the type of the member we
   3554      1.1  christos    are looking for.  This will help us resolve "pointers to member
   3555      1.1  christos    functions".  This function is used to resolve user expressions of
   3556      1.1  christos    the form "DOMAIN::NAME".  */
   3557      1.1  christos 
   3558  1.1.1.2  christos static struct value *
   3559      1.1  christos value_struct_elt_for_reference (struct type *domain, int offset,
   3560      1.1  christos 				struct type *curtype, const char *name,
   3561      1.1  christos 				struct type *intype,
   3562      1.1  christos 				int want_address,
   3563  1.1.1.6  christos 				enum noside noside)
   3564      1.1  christos {
   3565  1.1.1.6  christos   struct type *t = check_typedef (curtype);
   3566      1.1  christos   int i;
   3567  1.1.1.7  christos   struct value *result;
   3568  1.1.1.7  christos 
   3569      1.1  christos   if (t->code () != TYPE_CODE_STRUCT
   3570      1.1  christos       && t->code () != TYPE_CODE_UNION)
   3571      1.1  christos     error (_("Internal error: non-aggregate type "
   3572  1.1.1.7  christos 	     "to value_struct_elt_for_reference"));
   3573      1.1  christos 
   3574  1.1.1.8  christos   for (i = t->num_fields () - 1; i >= TYPE_N_BASECLASSES (t); i--)
   3575      1.1  christos     {
   3576      1.1  christos       const char *t_field_name = t->field (i).name ();
   3577      1.1  christos 
   3578  1.1.1.7  christos       if (t_field_name && strcmp (t_field_name, name) == 0)
   3579      1.1  christos 	{
   3580  1.1.1.6  christos 	  if (field_is_static (&t->field (i)))
   3581      1.1  christos 	    {
   3582      1.1  christos 	      struct value *v = value_static_field (t, i);
   3583      1.1  christos 	      if (want_address)
   3584      1.1  christos 		v = value_addr (v);
   3585      1.1  christos 	      return v;
   3586      1.1  christos 	    }
   3587      1.1  christos 	  if (TYPE_FIELD_PACKED (t, i))
   3588      1.1  christos 	    error (_("pointers to bitfield members not allowed"));
   3589      1.1  christos 
   3590  1.1.1.7  christos 	  if (want_address)
   3591  1.1.1.8  christos 	    return value_from_longest
   3592      1.1  christos 	      (lookup_memberptr_type (t->field (i).type (), domain),
   3593  1.1.1.7  christos 	       offset + (LONGEST) (t->field (i).loc_bitpos () >> 3));
   3594      1.1  christos 	  else if (noside != EVAL_NORMAL)
   3595      1.1  christos 	    return allocate_value (t->field (i).type ());
   3596      1.1  christos 	  else
   3597      1.1  christos 	    {
   3598      1.1  christos 	      /* Try to evaluate NAME as a qualified name with implicit
   3599  1.1.1.6  christos 		 this pointer.  In this case, attempt to return the
   3600      1.1  christos 		 equivalent to `this->*(&TYPE::NAME)'.  */
   3601      1.1  christos 	      struct value *v = value_of_this_silent (current_language);
   3602  1.1.1.6  christos 	      if (v != NULL)
   3603      1.1  christos 		{
   3604      1.1  christos 		  struct value *ptr, *this_v = v;
   3605      1.1  christos 		  long mem_offset;
   3606      1.1  christos 		  struct type *type, *tmp;
   3607      1.1  christos 
   3608      1.1  christos 		  ptr = value_aggregate_elt (domain, name, NULL, 1, noside);
   3609  1.1.1.7  christos 		  type = check_typedef (value_type (ptr));
   3610  1.1.1.3  christos 		  gdb_assert (type != NULL
   3611      1.1  christos 			      && type->code () == TYPE_CODE_MEMBERPTR);
   3612      1.1  christos 		  tmp = lookup_pointer_type (TYPE_SELF_TYPE (type));
   3613  1.1.1.6  christos 		  v = value_cast_pointers (tmp, v, 1);
   3614  1.1.1.6  christos 		  mem_offset = value_as_long (ptr);
   3615  1.1.1.6  christos 		  if (domain != curtype)
   3616  1.1.1.6  christos 		    {
   3617  1.1.1.6  christos 		      /* Find class offset of type CURTYPE from either its
   3618  1.1.1.6  christos 			 parent type DOMAIN or the type of implied this.  */
   3619  1.1.1.6  christos 		      int boff = 0;
   3620  1.1.1.6  christos 		      bool isvirt = false;
   3621  1.1.1.8  christos 		      if (get_baseclass_offset (domain, curtype, v, &boff,
   3622  1.1.1.6  christos 						&isvirt))
   3623  1.1.1.8  christos 			mem_offset += boff;
   3624  1.1.1.8  christos 		      else
   3625  1.1.1.8  christos 			{
   3626  1.1.1.8  christos 			  struct type *p = check_typedef (value_type (this_v));
   3627  1.1.1.6  christos 			  p = check_typedef (p->target_type ());
   3628  1.1.1.8  christos 			  if (get_baseclass_offset (p, curtype, this_v,
   3629  1.1.1.8  christos 						    &boff, &isvirt))
   3630  1.1.1.6  christos 			    mem_offset += boff;
   3631  1.1.1.8  christos 			}
   3632      1.1  christos 		    }
   3633      1.1  christos 		  tmp = lookup_pointer_type (type->target_type ());
   3634      1.1  christos 		  result = value_from_pointer (tmp,
   3635      1.1  christos 					       value_as_long (v) + mem_offset);
   3636      1.1  christos 		  return value_ind (result);
   3637      1.1  christos 		}
   3638      1.1  christos 
   3639      1.1  christos 	      error (_("Cannot reference non-static field \"%s\""), name);
   3640      1.1  christos 	    }
   3641      1.1  christos 	}
   3642      1.1  christos     }
   3643      1.1  christos 
   3644      1.1  christos   /* C++: If it was not found as a data field, then try to return it
   3645      1.1  christos      as a pointer to a method.  */
   3646  1.1.1.7  christos 
   3647  1.1.1.8  christos   /* Perform all necessary dereferencing.  */
   3648      1.1  christos   while (intype && intype->code () == TYPE_CODE_PTR)
   3649      1.1  christos     intype = intype->target_type ();
   3650      1.1  christos 
   3651      1.1  christos   for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
   3652      1.1  christos     {
   3653      1.1  christos       const char *t_field_name = TYPE_FN_FIELDLIST_NAME (t, i);
   3654      1.1  christos 
   3655      1.1  christos       if (t_field_name && strcmp (t_field_name, name) == 0)
   3656      1.1  christos 	{
   3657      1.1  christos 	  int j;
   3658      1.1  christos 	  int len = TYPE_FN_FIELDLIST_LENGTH (t, i);
   3659      1.1  christos 	  struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
   3660      1.1  christos 
   3661      1.1  christos 	  check_stub_method_group (t, i);
   3662      1.1  christos 
   3663      1.1  christos 	  if (intype)
   3664      1.1  christos 	    {
   3665  1.1.1.6  christos 	      for (j = 0; j < len; ++j)
   3666  1.1.1.6  christos 		{
   3667  1.1.1.6  christos 		  if (TYPE_CONST (intype) != TYPE_FN_FIELD_CONST (f, j))
   3668  1.1.1.6  christos 		    continue;
   3669  1.1.1.6  christos 		  if (TYPE_VOLATILE (intype) != TYPE_FN_FIELD_VOLATILE (f, j))
   3670      1.1  christos 		    continue;
   3671      1.1  christos 
   3672      1.1  christos 		  if (compare_parameters (TYPE_FN_FIELD_TYPE (f, j), intype, 0)
   3673      1.1  christos 		      || compare_parameters (TYPE_FN_FIELD_TYPE (f, j),
   3674      1.1  christos 					     intype, 1))
   3675      1.1  christos 		    break;
   3676      1.1  christos 		}
   3677      1.1  christos 
   3678      1.1  christos 	      if (j == len)
   3679      1.1  christos 		error (_("no member function matches "
   3680      1.1  christos 			 "that type instantiation"));
   3681      1.1  christos 	    }
   3682      1.1  christos 	  else
   3683      1.1  christos 	    {
   3684      1.1  christos 	      int ii;
   3685      1.1  christos 
   3686      1.1  christos 	      j = -1;
   3687      1.1  christos 	      for (ii = 0; ii < len; ++ii)
   3688      1.1  christos 		{
   3689      1.1  christos 		  /* Skip artificial methods.  This is necessary if,
   3690      1.1  christos 		     for example, the user wants to "print
   3691      1.1  christos 		     subclass::subclass" with only one user-defined
   3692      1.1  christos 		     constructor.  There is no ambiguity in this case.
   3693      1.1  christos 		     We are careful here to allow artificial methods
   3694      1.1  christos 		     if they are the unique result.  */
   3695      1.1  christos 		  if (TYPE_FN_FIELD_ARTIFICIAL (f, ii))
   3696      1.1  christos 		    {
   3697      1.1  christos 		      if (j == -1)
   3698      1.1  christos 			j = ii;
   3699      1.1  christos 		      continue;
   3700      1.1  christos 		    }
   3701      1.1  christos 
   3702      1.1  christos 		  /* Desired method is ambiguous if more than one
   3703      1.1  christos 		     method is defined.  */
   3704      1.1  christos 		  if (j != -1 && !TYPE_FN_FIELD_ARTIFICIAL (f, j))
   3705      1.1  christos 		    error (_("non-unique member `%s' requires "
   3706      1.1  christos 			     "type instantiation"), name);
   3707      1.1  christos 
   3708      1.1  christos 		  j = ii;
   3709      1.1  christos 		}
   3710      1.1  christos 
   3711      1.1  christos 	      if (j == -1)
   3712      1.1  christos 		error (_("no matching member function"));
   3713      1.1  christos 	    }
   3714      1.1  christos 
   3715      1.1  christos 	  if (TYPE_FN_FIELD_STATIC_P (f, j))
   3716      1.1  christos 	    {
   3717  1.1.1.4  christos 	      struct symbol *s =
   3718      1.1  christos 		lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
   3719      1.1  christos 			       0, VAR_DOMAIN, 0).symbol;
   3720      1.1  christos 
   3721      1.1  christos 	      if (s == NULL)
   3722      1.1  christos 		return NULL;
   3723  1.1.1.4  christos 
   3724      1.1  christos 	      if (want_address)
   3725  1.1.1.4  christos 		return value_addr (read_var_value (s, 0, 0));
   3726      1.1  christos 	      else
   3727      1.1  christos 		return read_var_value (s, 0, 0);
   3728      1.1  christos 	    }
   3729      1.1  christos 
   3730      1.1  christos 	  if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
   3731      1.1  christos 	    {
   3732      1.1  christos 	      if (want_address)
   3733      1.1  christos 		{
   3734      1.1  christos 		  result = allocate_value
   3735  1.1.1.8  christos 		    (lookup_methodptr_type (TYPE_FN_FIELD_TYPE (f, j)));
   3736      1.1  christos 		  cplus_make_method_ptr (value_type (result),
   3737      1.1  christos 					 value_contents_writeable (result).data (),
   3738      1.1  christos 					 TYPE_FN_FIELD_VOFFSET (f, j), 1);
   3739      1.1  christos 		}
   3740      1.1  christos 	      else if (noside == EVAL_AVOID_SIDE_EFFECTS)
   3741      1.1  christos 		return allocate_value (TYPE_FN_FIELD_TYPE (f, j));
   3742      1.1  christos 	      else
   3743      1.1  christos 		error (_("Cannot reference virtual member function \"%s\""),
   3744      1.1  christos 		       name);
   3745      1.1  christos 	    }
   3746      1.1  christos 	  else
   3747      1.1  christos 	    {
   3748  1.1.1.4  christos 	      struct symbol *s =
   3749      1.1  christos 		lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
   3750      1.1  christos 			       0, VAR_DOMAIN, 0).symbol;
   3751      1.1  christos 
   3752      1.1  christos 	      if (s == NULL)
   3753  1.1.1.6  christos 		return NULL;
   3754      1.1  christos 
   3755      1.1  christos 	      struct value *v = read_var_value (s, 0, 0);
   3756      1.1  christos 	      if (!want_address)
   3757      1.1  christos 		result = v;
   3758      1.1  christos 	      else
   3759      1.1  christos 		{
   3760  1.1.1.8  christos 		  result = allocate_value (lookup_methodptr_type (TYPE_FN_FIELD_TYPE (f, j)));
   3761      1.1  christos 		  cplus_make_method_ptr (value_type (result),
   3762      1.1  christos 					 value_contents_writeable (result).data (),
   3763      1.1  christos 					 value_address (v), 0);
   3764      1.1  christos 		}
   3765      1.1  christos 	    }
   3766      1.1  christos 	  return result;
   3767      1.1  christos 	}
   3768      1.1  christos     }
   3769      1.1  christos   for (i = TYPE_N_BASECLASSES (t) - 1; i >= 0; i--)
   3770      1.1  christos     {
   3771      1.1  christos       struct value *v;
   3772      1.1  christos       int base_offset;
   3773      1.1  christos 
   3774      1.1  christos       if (BASETYPE_VIA_VIRTUAL (t, i))
   3775      1.1  christos 	base_offset = 0;
   3776      1.1  christos       else
   3777      1.1  christos 	base_offset = TYPE_BASECLASS_BITPOS (t, i) / 8;
   3778      1.1  christos       v = value_struct_elt_for_reference (domain,
   3779      1.1  christos 					  offset + base_offset,
   3780      1.1  christos 					  TYPE_BASECLASS (t, i),
   3781      1.1  christos 					  name, intype,
   3782      1.1  christos 					  want_address, noside);
   3783      1.1  christos       if (v)
   3784      1.1  christos 	return v;
   3785      1.1  christos     }
   3786      1.1  christos 
   3787      1.1  christos   /* As a last chance, pretend that CURTYPE is a namespace, and look
   3788      1.1  christos      it up that way; this (frequently) works for types nested inside
   3789      1.1  christos      classes.  */
   3790      1.1  christos 
   3791      1.1  christos   return value_maybe_namespace_elt (curtype, name,
   3792      1.1  christos 				    want_address, noside);
   3793      1.1  christos }
   3794      1.1  christos 
   3795      1.1  christos /* C++: Return the member NAME of the namespace given by the type
   3796      1.1  christos    CURTYPE.  */
   3797      1.1  christos 
   3798  1.1.1.2  christos static struct value *
   3799      1.1  christos value_namespace_elt (const struct type *curtype,
   3800      1.1  christos 		     const char *name, int want_address,
   3801      1.1  christos 		     enum noside noside)
   3802      1.1  christos {
   3803      1.1  christos   struct value *retval = value_maybe_namespace_elt (curtype, name,
   3804      1.1  christos 						    want_address,
   3805      1.1  christos 						    noside);
   3806      1.1  christos 
   3807  1.1.1.7  christos   if (retval == NULL)
   3808      1.1  christos     error (_("No symbol \"%s\" in namespace \"%s\"."),
   3809      1.1  christos 	   name, curtype->name ());
   3810      1.1  christos 
   3811      1.1  christos   return retval;
   3812      1.1  christos }
   3813      1.1  christos 
   3814      1.1  christos /* A helper function used by value_namespace_elt and
   3815      1.1  christos    value_struct_elt_for_reference.  It looks up NAME inside the
   3816      1.1  christos    context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
   3817      1.1  christos    is a class and NAME refers to a type in CURTYPE itself (as opposed
   3818      1.1  christos    to, say, some base class of CURTYPE).  */
   3819      1.1  christos 
   3820  1.1.1.2  christos static struct value *
   3821      1.1  christos value_maybe_namespace_elt (const struct type *curtype,
   3822      1.1  christos 			   const char *name, int want_address,
   3823  1.1.1.7  christos 			   enum noside noside)
   3824  1.1.1.4  christos {
   3825      1.1  christos   const char *namespace_name = curtype->name ();
   3826      1.1  christos   struct block_symbol sym;
   3827      1.1  christos   struct value *result;
   3828      1.1  christos 
   3829      1.1  christos   sym = cp_lookup_symbol_namespace (namespace_name, name,
   3830  1.1.1.4  christos 				    get_selected_block (0), VAR_DOMAIN);
   3831      1.1  christos 
   3832      1.1  christos   if (sym.symbol == NULL)
   3833  1.1.1.8  christos     return NULL;
   3834  1.1.1.8  christos   else if ((noside == EVAL_AVOID_SIDE_EFFECTS)
   3835      1.1  christos 	   && (sym.symbol->aclass () == LOC_TYPEDEF))
   3836  1.1.1.4  christos     result = allocate_value (sym.symbol->type ());
   3837      1.1  christos   else
   3838  1.1.1.2  christos     result = value_of_variable (sym.symbol, sym.block);
   3839      1.1  christos 
   3840      1.1  christos   if (want_address)
   3841      1.1  christos     result = value_addr (result);
   3842      1.1  christos 
   3843      1.1  christos   return result;
   3844      1.1  christos }
   3845      1.1  christos 
   3846      1.1  christos /* Given a pointer or a reference value V, find its real (RTTI) type.
   3847      1.1  christos 
   3848      1.1  christos    Other parameters FULL, TOP, USING_ENC as with value_rtti_type()
   3849      1.1  christos    and refer to the values computed for the object pointed to.  */
   3850      1.1  christos 
   3851  1.1.1.4  christos struct type *
   3852      1.1  christos value_rtti_indirect_type (struct value *v, int *full,
   3853  1.1.1.3  christos 			  LONGEST *top, int *using_enc)
   3854      1.1  christos {
   3855      1.1  christos   struct value *target = NULL;
   3856      1.1  christos   struct type *type, *real_type, *target_type;
   3857      1.1  christos 
   3858  1.1.1.5  christos   type = value_type (v);
   3859      1.1  christos   type = check_typedef (type);
   3860  1.1.1.7  christos   if (TYPE_IS_REFERENCE (type))
   3861  1.1.1.3  christos     target = coerce_ref (v);
   3862  1.1.1.3  christos   else if (type->code () == TYPE_CODE_PTR)
   3863  1.1.1.7  christos     {
   3864  1.1.1.8  christos 
   3865  1.1.1.3  christos       try
   3866  1.1.1.8  christos 	{
   3867  1.1.1.7  christos 	  target = value_ind (v);
   3868  1.1.1.3  christos 	}
   3869  1.1.1.3  christos       catch (const gdb_exception_error &except)
   3870  1.1.1.3  christos 	{
   3871  1.1.1.3  christos 	  if (except.error == MEMORY_ERROR)
   3872  1.1.1.8  christos 	    {
   3873  1.1.1.8  christos 	      /* value_ind threw a memory error. The pointer is NULL or
   3874  1.1.1.3  christos 		 contains an uninitialized value: we can't determine any
   3875  1.1.1.3  christos 		 type.  */
   3876  1.1.1.7  christos 	      return NULL;
   3877  1.1.1.3  christos 	    }
   3878  1.1.1.3  christos 	  throw;
   3879      1.1  christos 	}
   3880      1.1  christos     }
   3881      1.1  christos   else
   3882      1.1  christos     return NULL;
   3883      1.1  christos 
   3884      1.1  christos   real_type = value_rtti_type (target, full, top, using_enc);
   3885      1.1  christos 
   3886      1.1  christos   if (real_type)
   3887      1.1  christos     {
   3888      1.1  christos       /* Copy qualifiers to the referenced object.  */
   3889      1.1  christos       target_type = value_type (target);
   3890  1.1.1.5  christos       real_type = make_cv_type (TYPE_CONST (target_type),
   3891  1.1.1.8  christos 				TYPE_VOLATILE (target_type), real_type, NULL);
   3892  1.1.1.7  christos       if (TYPE_IS_REFERENCE (type))
   3893  1.1.1.8  christos 	real_type = lookup_reference_type (real_type, type->code ());
   3894      1.1  christos       else if (type->code () == TYPE_CODE_PTR)
   3895  1.1.1.8  christos 	real_type = lookup_pointer_type (real_type);
   3896      1.1  christos       else
   3897      1.1  christos 	internal_error (_("Unexpected value type."));
   3898      1.1  christos 
   3899      1.1  christos       /* Copy qualifiers to the pointer/reference.  */
   3900      1.1  christos       real_type = make_cv_type (TYPE_CONST (type), TYPE_VOLATILE (type),
   3901      1.1  christos 				real_type, NULL);
   3902      1.1  christos     }
   3903      1.1  christos 
   3904      1.1  christos   return real_type;
   3905      1.1  christos }
   3906      1.1  christos 
   3907      1.1  christos /* Given a value pointed to by ARGP, check its real run-time type, and
   3908      1.1  christos    if that is different from the enclosing type, create a new value
   3909      1.1  christos    using the real run-time type as the enclosing type (and of the same
   3910      1.1  christos    type as ARGP) and return it, with the embedded offset adjusted to
   3911      1.1  christos    be the correct offset to the enclosed object.  RTYPE is the type,
   3912      1.1  christos    and XFULL, XTOP, and XUSING_ENC are the other parameters, computed
   3913      1.1  christos    by value_rtti_type().  If these are available, they can be supplied
   3914      1.1  christos    and a second call to value_rtti_type() is avoided.  (Pass RTYPE ==
   3915      1.1  christos    NULL if they're not available.  */
   3916      1.1  christos 
   3917      1.1  christos struct value *
   3918      1.1  christos value_full_object (struct value *argp,
   3919      1.1  christos 		   struct type *rtype,
   3920      1.1  christos 		   int xfull, int xtop,
   3921      1.1  christos 		   int xusing_enc)
   3922      1.1  christos {
   3923  1.1.1.4  christos   struct type *real_type;
   3924      1.1  christos   int full = 0;
   3925      1.1  christos   LONGEST top = -1;
   3926      1.1  christos   int using_enc = 0;
   3927      1.1  christos   struct value *new_val;
   3928      1.1  christos 
   3929      1.1  christos   if (rtype)
   3930      1.1  christos     {
   3931      1.1  christos       real_type = rtype;
   3932      1.1  christos       full = xfull;
   3933      1.1  christos       top = xtop;
   3934      1.1  christos       using_enc = xusing_enc;
   3935      1.1  christos     }
   3936      1.1  christos   else
   3937      1.1  christos     real_type = value_rtti_type (argp, &full, &top, &using_enc);
   3938      1.1  christos 
   3939      1.1  christos   /* If no RTTI data, or if object is already complete, do nothing.  */
   3940      1.1  christos   if (!real_type || real_type == value_enclosing_type (argp))
   3941      1.1  christos     return argp;
   3942      1.1  christos 
   3943      1.1  christos   /* In a destructor we might see a real type that is a superclass of
   3944      1.1  christos      the object's type.  In this case it is better to leave the object
   3945  1.1.1.8  christos      as-is.  */
   3946      1.1  christos   if (full
   3947      1.1  christos       && real_type->length () < value_enclosing_type (argp)->length ())
   3948      1.1  christos     return argp;
   3949      1.1  christos 
   3950      1.1  christos   /* If we have the full object, but for some reason the enclosing
   3951      1.1  christos      type is wrong, set it.  */
   3952      1.1  christos   /* pai: FIXME -- sounds iffy */
   3953      1.1  christos   if (full)
   3954      1.1  christos     {
   3955      1.1  christos       argp = value_copy (argp);
   3956      1.1  christos       set_value_enclosing_type (argp, real_type);
   3957      1.1  christos       return argp;
   3958      1.1  christos     }
   3959      1.1  christos 
   3960      1.1  christos   /* Check if object is in memory.  */
   3961      1.1  christos   if (VALUE_LVAL (argp) != lval_memory)
   3962      1.1  christos     {
   3963  1.1.1.7  christos       warning (_("Couldn't retrieve complete object of RTTI "
   3964      1.1  christos 		 "type %s; object may be in register(s)."),
   3965      1.1  christos 	       real_type->name ());
   3966      1.1  christos 
   3967      1.1  christos       return argp;
   3968      1.1  christos     }
   3969      1.1  christos 
   3970      1.1  christos   /* All other cases -- retrieve the complete object.  */
   3971      1.1  christos   /* Go back by the computed top_offset from the beginning of the
   3972      1.1  christos      object, adjusting for the embedded offset of argp if that's what
   3973      1.1  christos      value_rtti_type used for its computation.  */
   3974      1.1  christos   new_val = value_at_lazy (real_type, value_address (argp) - top +
   3975      1.1  christos 			   (using_enc ? 0 : value_embedded_offset (argp)));
   3976      1.1  christos   deprecated_set_value_type (new_val, value_type (argp));
   3977      1.1  christos   set_value_embedded_offset (new_val, (using_enc
   3978      1.1  christos 				       ? top + value_embedded_offset (argp)
   3979      1.1  christos 				       : top));
   3980      1.1  christos   return new_val;
   3981      1.1  christos }
   3982      1.1  christos 
   3983      1.1  christos 
   3984      1.1  christos /* Return the value of the local variable, if one exists.  Throw error
   3985      1.1  christos    otherwise, such as if the request is made in an inappropriate context.  */
   3986      1.1  christos 
   3987      1.1  christos struct value *
   3988  1.1.1.4  christos value_of_this (const struct language_defn *lang)
   3989  1.1.1.2  christos {
   3990  1.1.1.8  christos   struct block_symbol sym;
   3991      1.1  christos   const struct block *b;
   3992  1.1.1.8  christos   frame_info_ptr frame;
   3993      1.1  christos 
   3994      1.1  christos   if (lang->name_of_this () == NULL)
   3995      1.1  christos     error (_("no `this' in current language"));
   3996      1.1  christos 
   3997      1.1  christos   frame = get_selected_frame (_("no frame selected"));
   3998      1.1  christos 
   3999      1.1  christos   b = get_frame_block (frame, NULL);
   4000  1.1.1.4  christos 
   4001      1.1  christos   sym = lookup_language_this (lang, b);
   4002  1.1.1.8  christos   if (sym.symbol == NULL)
   4003      1.1  christos     error (_("current stack frame does not contain a variable named `%s'"),
   4004  1.1.1.4  christos 	   lang->name_of_this ());
   4005      1.1  christos 
   4006      1.1  christos   return read_var_value (sym.symbol, sym.block, frame);
   4007      1.1  christos }
   4008      1.1  christos 
   4009      1.1  christos /* Return the value of the local variable, if one exists.  Return NULL
   4010      1.1  christos    otherwise.  Never throw error.  */
   4011      1.1  christos 
   4012      1.1  christos struct value *
   4013      1.1  christos value_of_this_silent (const struct language_defn *lang)
   4014      1.1  christos {
   4015  1.1.1.7  christos   struct value *ret = NULL;
   4016      1.1  christos 
   4017      1.1  christos   try
   4018      1.1  christos     {
   4019  1.1.1.7  christos       ret = value_of_this (lang);
   4020  1.1.1.3  christos     }
   4021  1.1.1.3  christos   catch (const gdb_exception_error &except)
   4022      1.1  christos     {
   4023      1.1  christos     }
   4024      1.1  christos 
   4025      1.1  christos   return ret;
   4026      1.1  christos }
   4027      1.1  christos 
   4028      1.1  christos /* Create a slice (sub-string, sub-array) of ARRAY, that is LENGTH
   4029      1.1  christos    elements long, starting at LOWBOUND.  The result has the same lower
   4030      1.1  christos    bound as the original ARRAY.  */
   4031      1.1  christos 
   4032      1.1  christos struct value *
   4033      1.1  christos value_slice (struct value *array, int lowbound, int length)
   4034      1.1  christos {
   4035      1.1  christos   struct type *slice_range_type, *slice_type, *range_type;
   4036      1.1  christos   LONGEST lowerbound, upperbound;
   4037      1.1  christos   struct value *slice;
   4038      1.1  christos   struct type *array_type;
   4039  1.1.1.7  christos 
   4040  1.1.1.7  christos   array_type = check_typedef (value_type (array));
   4041      1.1  christos   if (array_type->code () != TYPE_CODE_ARRAY
   4042      1.1  christos       && array_type->code () != TYPE_CODE_STRING)
   4043  1.1.1.7  christos     error (_("cannot take slice of non-array"));
   4044  1.1.1.7  christos 
   4045  1.1.1.7  christos   if (type_not_allocated (array_type))
   4046  1.1.1.7  christos     error (_("array not allocated"));
   4047  1.1.1.7  christos   if (type_not_associated (array_type))
   4048  1.1.1.7  christos     error (_("array not associated"));
   4049  1.1.1.8  christos 
   4050      1.1  christos   range_type = array_type->index_type ();
   4051      1.1  christos   if (!get_discrete_bounds (range_type, &lowerbound, &upperbound))
   4052      1.1  christos     error (_("slice from bad array or bitstring"));
   4053      1.1  christos 
   4054      1.1  christos   if (lowbound < lowerbound || length < 0
   4055      1.1  christos       || lowbound + length - 1 > upperbound)
   4056      1.1  christos     error (_("slice out of range"));
   4057      1.1  christos 
   4058  1.1.1.7  christos   /* FIXME-type-allocation: need a way to free this type when we are
   4059  1.1.1.8  christos      done with it.  */
   4060  1.1.1.2  christos   slice_range_type = create_static_range_type (NULL,
   4061  1.1.1.2  christos 					       range_type->target_type (),
   4062  1.1.1.2  christos 					       lowbound,
   4063  1.1.1.2  christos 					       lowbound + length - 1);
   4064  1.1.1.8  christos 
   4065  1.1.1.2  christos   {
   4066  1.1.1.8  christos     struct type *element_type = array_type->target_type ();
   4067  1.1.1.2  christos     LONGEST offset
   4068  1.1.1.7  christos       = (lowbound - lowerbound) * check_typedef (element_type)->length ();
   4069  1.1.1.2  christos 
   4070  1.1.1.2  christos     slice_type = create_array_type (NULL,
   4071  1.1.1.7  christos 				    element_type,
   4072  1.1.1.2  christos 				    slice_range_type);
   4073  1.1.1.2  christos     slice_type->set_code (array_type->code ());
   4074  1.1.1.2  christos 
   4075  1.1.1.2  christos     if (VALUE_LVAL (array) == lval_memory && value_lazy (array))
   4076  1.1.1.2  christos       slice = allocate_value_lazy (slice_type);
   4077  1.1.1.2  christos     else
   4078  1.1.1.2  christos       {
   4079  1.1.1.4  christos 	slice = allocate_value (slice_type);
   4080  1.1.1.2  christos 	value_contents_copy (slice, 0, array, offset,
   4081      1.1  christos 			     type_length_units (slice_type));
   4082  1.1.1.2  christos       }
   4083  1.1.1.2  christos 
   4084  1.1.1.2  christos     set_value_component_location (slice, array);
   4085      1.1  christos     set_value_offset (slice, value_offset (array) + offset);
   4086      1.1  christos   }
   4087      1.1  christos 
   4088      1.1  christos   return slice;
   4089  1.1.1.7  christos }
   4090      1.1  christos 
   4091      1.1  christos /* See value.h.  */
   4092  1.1.1.7  christos 
   4093      1.1  christos struct value *
   4094      1.1  christos value_literal_complex (struct value *arg1,
   4095      1.1  christos 		       struct value *arg2,
   4096      1.1  christos 		       struct type *type)
   4097  1.1.1.8  christos {
   4098      1.1  christos   struct value *val;
   4099      1.1  christos   struct type *real_type = type->target_type ();
   4100      1.1  christos 
   4101      1.1  christos   val = allocate_value (type);
   4102      1.1  christos   arg1 = value_cast (real_type, arg1);
   4103  1.1.1.8  christos   arg2 = value_cast (real_type, arg2);
   4104  1.1.1.8  christos 
   4105  1.1.1.8  christos   int len = real_type->length ();
   4106  1.1.1.8  christos 
   4107  1.1.1.8  christos   copy (value_contents (arg1),
   4108  1.1.1.8  christos 	value_contents_raw (val).slice (0, len));
   4109  1.1.1.8  christos   copy (value_contents (arg2),
   4110      1.1  christos 	value_contents_raw (val).slice (len, len));
   4111      1.1  christos 
   4112      1.1  christos   return val;
   4113  1.1.1.7  christos }
   4114  1.1.1.7  christos 
   4115  1.1.1.7  christos /* See value.h.  */
   4116  1.1.1.7  christos 
   4117  1.1.1.7  christos struct value *
   4118  1.1.1.7  christos value_real_part (struct value *value)
   4119  1.1.1.8  christos {
   4120  1.1.1.7  christos   struct type *type = check_typedef (value_type (value));
   4121  1.1.1.7  christos   struct type *ttype = type->target_type ();
   4122  1.1.1.7  christos 
   4123  1.1.1.7  christos   gdb_assert (type->code () == TYPE_CODE_COMPLEX);
   4124  1.1.1.7  christos   return value_from_component (value, ttype, 0);
   4125  1.1.1.7  christos }
   4126  1.1.1.7  christos 
   4127  1.1.1.7  christos /* See value.h.  */
   4128  1.1.1.7  christos 
   4129  1.1.1.7  christos struct value *
   4130  1.1.1.7  christos value_imaginary_part (struct value *value)
   4131  1.1.1.8  christos {
   4132  1.1.1.7  christos   struct type *type = check_typedef (value_type (value));
   4133  1.1.1.7  christos   struct type *ttype = type->target_type ();
   4134  1.1.1.7  christos 
   4135  1.1.1.8  christos   gdb_assert (type->code () == TYPE_CODE_COMPLEX);
   4136  1.1.1.7  christos   return value_from_component (value, ttype,
   4137  1.1.1.7  christos 			       check_typedef (ttype)->length ());
   4138      1.1  christos }
   4139      1.1  christos 
   4140      1.1  christos /* Cast a value into the appropriate complex data type.  */
   4141      1.1  christos 
   4142      1.1  christos static struct value *
   4143  1.1.1.8  christos cast_into_complex (struct type *type, struct value *val)
   4144      1.1  christos {
   4145  1.1.1.7  christos   struct type *real_type = type->target_type ();
   4146      1.1  christos 
   4147  1.1.1.8  christos   if (value_type (val)->code () == TYPE_CODE_COMPLEX)
   4148      1.1  christos     {
   4149      1.1  christos       struct type *val_real_type = value_type (val)->target_type ();
   4150  1.1.1.8  christos       struct value *re_val = allocate_value (val_real_type);
   4151      1.1  christos       struct value *im_val = allocate_value (val_real_type);
   4152  1.1.1.8  christos       int len = val_real_type->length ();
   4153  1.1.1.8  christos 
   4154  1.1.1.8  christos       copy (value_contents (val).slice (0, len),
   4155  1.1.1.8  christos 	    value_contents_raw (re_val));
   4156      1.1  christos       copy (value_contents (val).slice (len, len),
   4157      1.1  christos 	    value_contents_raw (im_val));
   4158      1.1  christos 
   4159  1.1.1.7  christos       return value_literal_complex (re_val, im_val, type);
   4160  1.1.1.7  christos     }
   4161      1.1  christos   else if (value_type (val)->code () == TYPE_CODE_FLT
   4162      1.1  christos 	   || value_type (val)->code () == TYPE_CODE_INT)
   4163      1.1  christos     return value_literal_complex (val,
   4164      1.1  christos 				  value_zero (real_type, not_lval),
   4165      1.1  christos 				  type);
   4166      1.1  christos   else
   4167      1.1  christos     error (_("cannot cast non-number to complex"));
   4168  1.1.1.7  christos }
   4169      1.1  christos 
   4170  1.1.1.7  christos void _initialize_valops ();
   4171      1.1  christos void
   4172      1.1  christos _initialize_valops ()
   4173      1.1  christos {
   4174      1.1  christos   add_setshow_boolean_cmd ("overload-resolution", class_support,
   4175      1.1  christos 			   &overload_resolution, _("\
   4176      1.1  christos Set overload resolution in evaluating C++ functions."), _("\
   4177      1.1  christos Show overload resolution in evaluating C++ functions."),
   4178      1.1  christos 			   NULL, NULL,
   4179      1.1  christos 			   show_overload_resolution,
   4180      1.1  christos 			   &setlist, &showlist);
   4181                      overload_resolution = 1;
   4182                    }
   4183