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