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