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