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