]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/valops.c
Turn value_offset into method
[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);
256 v = value_at_lazy (real_type, value_address (v));
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 (),
8a13d42d 274 value_zero (t1, not_lval), t1, 1);
6af87b03
AR
275 if (v)
276 {
277 /* Downcasting is possible (t1 is superclass of v2). */
2390419d 278 CORE_ADDR addr2 = value_address (v2) + value_embedded_offset (v2);
a109c7c1 279
42ae5230 280 addr2 -= value_address (v) + value_embedded_offset (v);
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. */
0d5de010 328 arg2 = value_copy (arg2);
81ae560c 329 arg2->deprecated_set_type (type);
4dfea560 330 set_value_enclosing_type (arg2, type);
fb933624
DJ
331 set_value_pointed_to_offset (arg2, 0); /* pai: chk_val */
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 {
50888e42 345 double d = target_float_to_host_double (value_contents (value).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;
46680d22
SM
355 vz.read (value_contents (value), type_byte_order (type),
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. */
394 struct value *result = allocate_value (to_type);
46680d22 395 unscaled.write (value_contents_raw (result),
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
AB
430 {
431 arg2 = value_copy (arg2);
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 {
546 struct value *v = allocate_value (to_type);
50888e42
SM
547 target_float_convert (value_contents (arg2).data (), type2,
548 value_contents_raw (v).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
46680d22
SM
555 fp_val.read_fixed_point (value_contents (arg2),
556 type_byte_order (type2),
557 type2->is_unsigned (),
558 type2->fixed_point_scaling_factor ());
0a12719e
JB
559
560 struct value *v = allocate_value (to_type);
50888e42 561 target_float_from_host_double (value_contents_raw (v).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
2a50938a 587 (value_contents (arg2), 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 {
c973d0aa 621 struct value *result = allocate_value (to_type);
a109c7c1 622
50888e42
SM
623 cplus_make_method_ptr (to_type,
624 value_contents_writeable (result).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 {
c973d0aa 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
0d5de010 650 arg2 = value_copy (arg2);
81ae560c 651 arg2->deprecated_set_type (to_type);
c973d0aa 652 set_value_enclosing_type (arg2, to_type);
b44d461b 653 set_value_pointed_to_offset (arg2, 0); /* pai: chk_val */
c906108c
SS
654 return arg2;
655 }
c906108c 656 else if (VALUE_LVAL (arg2) == lval_memory)
c973d0aa 657 return value_at_lazy (to_type, value_address (arg2));
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)
851 return value_zero (type, not_lval);
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. */
879 addr = value_address (arg);
880 if (full)
881 {
882 /* Done. */
883 }
884 else if (using_enc)
885 addr += top;
886 else
887 addr += top + value_embedded_offset (arg);
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 (),
50888e42 905 value_contents_for_printing (tem).data (),
8af8e3bc
PA
906 value_embedded_offset (tem),
907 value_address (tem), 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 (),
50888e42 921 value_contents_for_printing (tem).data (),
8af8e3bc
PA
922 value_embedded_offset (tem),
923 value_address (tem), 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)
4e8f195d
TT
931 return value_zero (type, not_lval);
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
120bd360 957 val = allocate_value (type);
4bce7cda 958 gdb::array_view<gdb_byte> val_contents = value_contents_writeable (val);
df86565b 959 int elt_len = eltype->length ();
4bce7cda 960
cfa6f054 961 for (i = 0; i < high_bound - low_bound + 1; i++)
120bd360 962 {
4bce7cda
SM
963 value *tmp = value_one (eltype);
964 copy (value_contents_all (tmp),
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
PA
994 if (!lazy)
995 value_fetch_lazy (val);
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)
23f945bf
AA
1069 mark_value_bits_unavailable (val, (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
88e3b34b 1092 if (!deprecated_value_modifiable (toval))
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
76675c4d 1157 changed_addr = value_address (parent) + 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 {
42ae5230 1184 changed_addr = value_address (toval);
3ae385af 1185 changed_len = type_length_units (type);
50888e42 1186 dest_buffer = value_contents (fromval).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,
50888e42 1262 value_contents (fromval).data ());
00fa51f6 1263 }
c906108c 1264 else
46680d22 1265 put_frame_register_bytes (frame, value_reg,
76675c4d 1266 toval->offset (),
46680d22 1267 value_contents (fromval));
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 {
c8f2448a 1276 const struct lval_funcs *funcs = value_computed_funcs (toval);
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. */
c906108c 1345 val = value_copy (toval);
4aac0db7 1346 set_value_lazy (val, 0);
4bce7cda 1347 copy (value_contents (fromval), value_contents_raw (val));
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
UW
1354 {
1355 set_value_enclosing_type (val, value_enclosing_type (fromval));
1356 set_value_pointed_to_offset (val, value_pointed_to_offset (fromval));
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
4754a64e 1374 val = allocate_repeat_value (value_enclosing_type (arg1), count);
c906108c 1375
c906108c 1376 VALUE_LVAL (val) = lval_memory;
42ae5230 1377 set_value_address (val, value_address (arg1));
c906108c 1378
24e6bcee 1379 read_value_memory (val, 0, value_stack (val), value_address (val),
50888e42 1380 value_contents_all_raw (val).data (),
3ae385af 1381 type_length_units (value_enclosing_type (val)));
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
UW
1408
1409 if ((VALUE_LVAL (val) == lval_memory && value_lazy (val))
78134374 1410 || type->code () == TYPE_CODE_FUNC)
61212c0f 1411 {
42ae5230 1412 CORE_ADDR addr = value_address (val);
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);
50888e42 1488 write_memory (addr, value_contents (val).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 ()),
42ae5230 1529 value_address (arg1));
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 ()),
42ae5230 1544 value_address (arg1));
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 {
3326303b 1559 if (value_bits_synthetic_pointer (arg1, value_embedded_offset (arg1),
df86565b 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
MG
1570 struct type *enclosing_type
1571 = check_typedef (value_enclosing_type (arg1));
1572 struct type *enclosing_type_ptr
27710edb 1573 = lookup_pointer_type (enclosing_type->target_type ());
3326303b
MG
1574
1575 arg2 = value_copy (arg1);
81ae560c 1576 arg2->deprecated_set_type (type_ptr);
3326303b 1577 set_value_enclosing_type (arg2, 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 ()),
42ae5230 1594 (value_address (arg1)
13c3b5f5 1595 + value_embedded_offset (arg1)));
c906108c
SS
1596
1597 /* This may be a pointer to a base subobject; so remember the
ac3eeb49 1598 full derived object's type ... */
4dfea560
DE
1599 set_value_enclosing_type (arg2,
1600 lookup_pointer_type (value_enclosing_type (arg1)));
ac3eeb49
MS
1601 /* ... and also the relative position of the subobject in the full
1602 object. */
b44d461b 1603 set_value_pointed_to_offset (arg2, value_embedded_offset (arg1));
c906108c
SS
1604 return arg2;
1605}
1606
ac3eeb49
MS
1607/* Return a reference value for the object for which ARG1 is the
1608 contents. */
fb933624
DJ
1609
1610struct value *
a65cfae5 1611value_ref (struct value *arg1, enum type_code refcode)
fb933624
DJ
1612{
1613 struct value *arg2;
d0c97917 1614 struct type *type = check_typedef (arg1->type ());
a109c7c1 1615
a65cfae5
AV
1616 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
1617
78134374
SM
1618 if ((type->code () == TYPE_CODE_REF
1619 || type->code () == TYPE_CODE_RVALUE_REF)
1620 && type->code () == refcode)
fb933624
DJ
1621 return arg1;
1622
1623 arg2 = value_addr (arg1);
81ae560c 1624 arg2->deprecated_set_type (lookup_reference_type (type, refcode));
fb933624
DJ
1625 return arg2;
1626}
1627
ac3eeb49
MS
1628/* Given a value of a pointer type, apply the C unary * operator to
1629 it. */
c906108c 1630
f23631e4
AC
1631struct value *
1632value_ind (struct value *arg1)
c906108c
SS
1633{
1634 struct type *base_type;
f23631e4 1635 struct value *arg2;
c906108c 1636
994b9211 1637 arg1 = coerce_array (arg1);
c906108c 1638
d0c97917 1639 base_type = check_typedef (arg1->type ());
c906108c 1640
8cf6f0b1
TT
1641 if (VALUE_LVAL (arg1) == lval_computed)
1642 {
c8f2448a 1643 const struct lval_funcs *funcs = value_computed_funcs (arg1);
8cf6f0b1
TT
1644
1645 if (funcs->indirect)
1646 {
1647 struct value *result = funcs->indirect (arg1);
1648
1649 if (result)
1650 return result;
1651 }
1652 }
1653
78134374 1654 if (base_type->code () == TYPE_CODE_PTR)
c906108c
SS
1655 {
1656 struct type *enc_type;
a109c7c1 1657
ac3eeb49 1658 /* We may be pointing to something embedded in a larger object.
dda83cd7 1659 Get the real type of the enclosing object. */
4754a64e 1660 enc_type = check_typedef (value_enclosing_type (arg1));
27710edb 1661 enc_type = enc_type->target_type ();
0d5de010 1662
e79eb02f 1663 CORE_ADDR base_addr;
78134374
SM
1664 if (check_typedef (enc_type)->code () == TYPE_CODE_FUNC
1665 || check_typedef (enc_type)->code () == TYPE_CODE_METHOD)
e79eb02f
AB
1666 {
1667 /* For functions, go through find_function_addr, which knows
1668 how to handle function descriptors. */
1669 base_addr = find_function_addr (arg1, NULL);
1670 }
0d5de010 1671 else
e79eb02f
AB
1672 {
1673 /* Retrieve the enclosing object pointed to. */
1674 base_addr = (value_as_address (arg1)
1675 - value_pointed_to_offset (arg1));
1676 }
1677 arg2 = value_at_lazy (enc_type, base_addr);
d0c97917 1678 enc_type = arg2->type ();
e79eb02f
AB
1679 return readjust_indirect_value_type (arg2, enc_type, base_type,
1680 arg1, base_addr);
c906108c
SS
1681 }
1682
8a3fe4f8 1683 error (_("Attempt to take contents of a non-pointer value."));
c906108c
SS
1684}
1685\f
39d37385
PA
1686/* Create a value for an array by allocating space in GDB, copying the
1687 data into that space, and then setting up an array value.
c906108c 1688
ac3eeb49
MS
1689 The array bounds are set from LOWBOUND and HIGHBOUND, and the array
1690 is populated from the values passed in ELEMVEC.
c906108c
SS
1691
1692 The element type of the array is inherited from the type of the
1693 first element, and all elements must have the same size (though we
ac3eeb49 1694 don't currently enforce any restriction on their types). */
c906108c 1695
f23631e4
AC
1696struct value *
1697value_array (int lowbound, int highbound, struct value **elemvec)
c906108c
SS
1698{
1699 int nelem;
1700 int idx;
6b850546 1701 ULONGEST typelength;
f23631e4 1702 struct value *val;
c906108c 1703 struct type *arraytype;
c906108c 1704
ac3eeb49
MS
1705 /* Validate that the bounds are reasonable and that each of the
1706 elements have the same size. */
c906108c
SS
1707
1708 nelem = highbound - lowbound + 1;
1709 if (nelem <= 0)
1710 {
8a3fe4f8 1711 error (_("bad array bounds (%d, %d)"), lowbound, highbound);
c906108c 1712 }
3ae385af 1713 typelength = type_length_units (value_enclosing_type (elemvec[0]));
c906108c
SS
1714 for (idx = 1; idx < nelem; idx++)
1715 {
3ae385af
SM
1716 if (type_length_units (value_enclosing_type (elemvec[idx]))
1717 != typelength)
c906108c 1718 {
8a3fe4f8 1719 error (_("array elements must all be the same size"));
c906108c
SS
1720 }
1721 }
1722
e3506a9f
UW
1723 arraytype = lookup_array_range_type (value_enclosing_type (elemvec[0]),
1724 lowbound, highbound);
c906108c 1725
67bd3fd5 1726 if (!current_language->c_style_arrays_p ())
c906108c
SS
1727 {
1728 val = allocate_value (arraytype);
1729 for (idx = 0; idx < nelem; idx++)
39d37385
PA
1730 value_contents_copy (val, idx * typelength, elemvec[idx], 0,
1731 typelength);
c906108c
SS
1732 return val;
1733 }
1734
63092375
DJ
1735 /* Allocate space to store the array, and then initialize it by
1736 copying in each element. */
c906108c 1737
63092375 1738 val = allocate_value (arraytype);
c906108c 1739 for (idx = 0; idx < nelem; idx++)
39d37385 1740 value_contents_copy (val, idx * typelength, elemvec[idx], 0, typelength);
63092375 1741 return val;
c906108c
SS
1742}
1743
6c7a06a3 1744struct value *
e3a3797e 1745value_cstring (const char *ptr, ssize_t len, struct type *char_type)
6c7a06a3
TT
1746{
1747 struct value *val;
22c12a6c 1748 int lowbound = current_language->string_lower_bound ();
df86565b 1749 ssize_t highbound = len / char_type->length ();
6c7a06a3 1750 struct type *stringtype
e3506a9f 1751 = lookup_array_range_type (char_type, lowbound, highbound + lowbound - 1);
6c7a06a3
TT
1752
1753 val = allocate_value (stringtype);
50888e42 1754 memcpy (value_contents_raw (val).data (), ptr, len);
6c7a06a3
TT
1755 return val;
1756}
1757
ac3eeb49
MS
1758/* Create a value for a string constant by allocating space in the
1759 inferior, copying the data into that space, and returning the
1760 address with type TYPE_CODE_STRING. PTR points to the string
1761 constant data; LEN is number of characters.
1762
1763 Note that string types are like array of char types with a lower
1764 bound of zero and an upper bound of LEN - 1. Also note that the
1765 string may contain embedded null bytes. */
c906108c 1766
f23631e4 1767struct value *
7cc3f8e2 1768value_string (const char *ptr, ssize_t len, struct type *char_type)
c906108c 1769{
f23631e4 1770 struct value *val;
22c12a6c 1771 int lowbound = current_language->string_lower_bound ();
df86565b 1772 ssize_t highbound = len / char_type->length ();
c906108c 1773 struct type *stringtype
e3506a9f 1774 = lookup_string_range_type (char_type, lowbound, highbound + lowbound - 1);
c906108c 1775
3b7538c0 1776 val = allocate_value (stringtype);
50888e42 1777 memcpy (value_contents_raw (val).data (), ptr, len);
3b7538c0 1778 return val;
c906108c
SS
1779}
1780
c906108c 1781\f
13221aec
AB
1782/* See if we can pass arguments in T2 to a function which takes arguments
1783 of types T1. T1 is a list of NARGS arguments, and T2 is an array_view
1784 of the values we're trying to pass. If some arguments need coercion of
1785 some sort, then the coerced values are written into T2. Return value is
ac3eeb49
MS
1786 0 if the arguments could be matched, or the position at which they
1787 differ if not.
c906108c 1788
ac3eeb49 1789 STATICP is nonzero if the T1 argument list came from a static
13221aec 1790 member function. T2 must still include the ``this'' pointer, but
ac3eeb49 1791 it will be skipped.
c906108c
SS
1792
1793 For non-static member functions, we ignore the first argument,
ac3eeb49
MS
1794 which is the type of the instance variable. This is because we
1795 want to handle calls with objects from derived classes. This is
1796 not entirely correct: we should actually check to make sure that a
c906108c
SS
1797 requested operation is type secure, shouldn't we? FIXME. */
1798
1799static int
13221aec
AB
1800typecmp (bool staticp, bool varargs, int nargs,
1801 struct field t1[], gdb::array_view<value *> t2)
c906108c
SS
1802{
1803 int i;
1804
ac3eeb49
MS
1805 /* Skip ``this'' argument if applicable. T2 will always include
1806 THIS. */
4a1970e4 1807 if (staticp)
13221aec 1808 t2 = t2.slice (1);
ad2f7632
DJ
1809
1810 for (i = 0;
5d14b6e5 1811 (i < nargs) && t1[i].type ()->code () != TYPE_CODE_VOID;
ad2f7632 1812 i++)
c906108c 1813 {
c5aa993b 1814 struct type *tt1, *tt2;
ad2f7632 1815
13221aec 1816 if (i == t2.size ())
c5aa993b 1817 return i + 1;
ad2f7632 1818
5d14b6e5 1819 tt1 = check_typedef (t1[i].type ());
d0c97917 1820 tt2 = check_typedef (t2[i]->type ());
ad2f7632 1821
aa006118 1822 if (TYPE_IS_REFERENCE (tt1)
8301c89e 1823 /* We should be doing hairy argument matching, as below. */
27710edb 1824 && (check_typedef (tt1->target_type ())->code ()
78134374 1825 == tt2->code ()))
c906108c 1826 {
78134374 1827 if (tt2->code () == TYPE_CODE_ARRAY)
c906108c
SS
1828 t2[i] = value_coerce_array (t2[i]);
1829 else
78134374 1830 t2[i] = value_ref (t2[i], tt1->code ());
c906108c
SS
1831 continue;
1832 }
1833
802db21b
DB
1834 /* djb - 20000715 - Until the new type structure is in the
1835 place, and we can attempt things like implicit conversions,
1836 we need to do this so you can take something like a map<const
1837 char *>, and properly access map["hello"], because the
1838 argument to [] will be a reference to a pointer to a char,
ac3eeb49 1839 and the argument will be a pointer to a char. */
78134374 1840 while (TYPE_IS_REFERENCE (tt1) || tt1->code () == TYPE_CODE_PTR)
802db21b 1841 {
27710edb 1842 tt1 = check_typedef ( tt1->target_type () );
802db21b 1843 }
78134374
SM
1844 while (tt2->code () == TYPE_CODE_ARRAY
1845 || tt2->code () == TYPE_CODE_PTR
aa006118 1846 || TYPE_IS_REFERENCE (tt2))
c906108c 1847 {
27710edb 1848 tt2 = check_typedef (tt2->target_type ());
c906108c 1849 }
78134374 1850 if (tt1->code () == tt2->code ())
c5aa993b 1851 continue;
ac3eeb49
MS
1852 /* Array to pointer is a `trivial conversion' according to the
1853 ARM. */
c906108c 1854
ac3eeb49 1855 /* We should be doing much hairier argument matching (see
dda83cd7
SM
1856 section 13.2 of the ARM), but as a quick kludge, just check
1857 for the same type code. */
d0c97917 1858 if (t1[i].type ()->code () != t2[i]->type ()->code ())
c5aa993b 1859 return i + 1;
c906108c 1860 }
13221aec 1861 if (varargs || i == t2.size ())
c5aa993b 1862 return 0;
ad2f7632 1863 return i + 1;
c906108c
SS
1864}
1865
87a37e5e
PA
1866/* Helper class for search_struct_field that keeps track of found
1867 results and possibly throws an exception if the search yields
1868 ambiguous results. See search_struct_field for description of
1869 LOOKING_FOR_BASECLASS. */
c906108c 1870
87a37e5e
PA
1871struct struct_field_searcher
1872{
1873 /* A found field. */
1874 struct found_field
1875 {
1876 /* Path to the structure where the field was found. */
1877 std::vector<struct type *> path;
1878
1879 /* The field found. */
1880 struct value *field_value;
1881 };
1882
1883 /* See corresponding fields for description of parameters. */
1884 struct_field_searcher (const char *name,
1885 struct type *outermost_type,
1886 bool looking_for_baseclass)
1887 : m_name (name),
1888 m_looking_for_baseclass (looking_for_baseclass),
1889 m_outermost_type (outermost_type)
1890 {
1891 }
1892
1893 /* The search entry point. If LOOKING_FOR_BASECLASS is true and the
1894 base class search yields ambiguous results, this throws an
1895 exception. If LOOKING_FOR_BASECLASS is false, the found fields
1896 are accumulated and the caller (search_struct_field) takes care
1897 of throwing an error if the field search yields ambiguous
1898 results. The latter is done that way so that the error message
1899 can include a list of all the found candidates. */
1900 void search (struct value *arg, LONGEST offset, struct type *type);
1901
1902 const std::vector<found_field> &fields ()
1903 {
1904 return m_fields;
1905 }
1906
1907 struct value *baseclass ()
1908 {
1909 return m_baseclass;
1910 }
1911
1912private:
1913 /* Update results to include V, a found field/baseclass. */
1914 void update_result (struct value *v, LONGEST boffset);
1915
1916 /* The name of the field/baseclass we're searching for. */
1917 const char *m_name;
1918
1919 /* Whether we're looking for a baseclass, or a field. */
1920 const bool m_looking_for_baseclass;
1921
1922 /* The offset of the baseclass containing the field/baseclass we
1923 last recorded. */
1924 LONGEST m_last_boffset = 0;
1925
1926 /* If looking for a baseclass, then the result is stored here. */
1927 struct value *m_baseclass = nullptr;
1928
1929 /* When looking for fields, the found candidates are stored
1930 here. */
1931 std::vector<found_field> m_fields;
1932
1933 /* The type of the initial type passed to search_struct_field; this
1934 is used for error reporting when the lookup is ambiguous. */
1935 struct type *m_outermost_type;
1936
1937 /* The full path to the struct being inspected. E.g. for field 'x'
1938 defined in class B inherited by class A, we have A and B pushed
1939 on the path. */
1940 std::vector <struct type *> m_struct_path;
1941};
1942
1943void
1944struct_field_searcher::update_result (struct value *v, LONGEST boffset)
b1af9e97
TT
1945{
1946 if (v != NULL)
1947 {
87a37e5e
PA
1948 if (m_looking_for_baseclass)
1949 {
1950 if (m_baseclass != nullptr
1951 /* The result is not ambiguous if all the classes that are
1952 found occupy the same space. */
1953 && m_last_boffset != boffset)
1954 error (_("base class '%s' is ambiguous in type '%s'"),
1955 m_name, TYPE_SAFE_NAME (m_outermost_type));
1956
1957 m_baseclass = v;
1958 m_last_boffset = boffset;
1959 }
1960 else
1961 {
1962 /* The field is not ambiguous if it occupies the same
1963 space. */
1964 if (m_fields.empty () || m_last_boffset != boffset)
1965 m_fields.push_back ({m_struct_path, v});
a41ad347
BL
1966 else
1967 {
1968 /*Fields can occupy the same space and have the same name (be
1969 ambiguous). This can happen when fields in two different base
1970 classes are marked [[no_unique_address]] and have the same name.
1971 The C++ standard says that such fields can only occupy the same
1972 space if they are of different type, but we don't rely on that in
1973 the following code. */
1974 bool ambiguous = false, insert = true;
1975 for (const found_field &field: m_fields)
1976 {
1977 if(field.path.back () != m_struct_path.back ())
1978 {
1979 /* Same boffset points to members of different classes.
1980 We have found an ambiguity and should record it. */
1981 ambiguous = true;
1982 }
1983 else
1984 {
1985 /* We don't need to insert this value again, because a
1986 non-ambiguous path already leads to it. */
1987 insert = false;
1988 break;
1989 }
1990 }
1991 if (ambiguous && insert)
1992 m_fields.push_back ({m_struct_path, v});
1993 }
87a37e5e 1994 }
b1af9e97
TT
1995 }
1996}
c906108c 1997
b1af9e97 1998/* A helper for search_struct_field. This does all the work; most
87a37e5e 1999 arguments are as passed to search_struct_field. */
b1af9e97 2000
87a37e5e
PA
2001void
2002struct_field_searcher::search (struct value *arg1, LONGEST offset,
2003 struct type *type)
c906108c
SS
2004{
2005 int i;
edf3d5f3 2006 int nbases;
c906108c 2007
87a37e5e
PA
2008 m_struct_path.push_back (type);
2009 SCOPE_EXIT { m_struct_path.pop_back (); };
2010
f168693b 2011 type = check_typedef (type);
edf3d5f3 2012 nbases = TYPE_N_BASECLASSES (type);
c906108c 2013
87a37e5e 2014 if (!m_looking_for_baseclass)
1f704f76 2015 for (i = type->num_fields () - 1; i >= nbases; i--)
c906108c 2016 {
33d16dd9 2017 const char *t_field_name = type->field (i).name ();
c906108c 2018
87a37e5e 2019 if (t_field_name && (strcmp_iw (t_field_name, m_name) == 0))
c906108c 2020 {
f23631e4 2021 struct value *v;
a109c7c1 2022
ceacbf6e 2023 if (field_is_static (&type->field (i)))
686d4def 2024 v = value_static_field (type, i);
c906108c 2025 else
b1af9e97 2026 v = value_primitive_field (arg1, offset, i, type);
87a37e5e
PA
2027
2028 update_result (v, offset);
b1af9e97 2029 return;
c906108c
SS
2030 }
2031
2032 if (t_field_name
47c6ee49 2033 && t_field_name[0] == '\0')
c906108c 2034 {
940da03e 2035 struct type *field_type = type->field (i).type ();
a109c7c1 2036
78134374
SM
2037 if (field_type->code () == TYPE_CODE_UNION
2038 || field_type->code () == TYPE_CODE_STRUCT)
c906108c 2039 {
ac3eeb49
MS
2040 /* Look for a match through the fields of an anonymous
2041 union, or anonymous struct. C++ provides anonymous
2042 unions.
c906108c 2043
1b831c93
AC
2044 In the GNU Chill (now deleted from GDB)
2045 implementation of variant record types, each
2046 <alternative field> has an (anonymous) union type,
2047 each member of the union represents a <variant
2048 alternative>. Each <variant alternative> is
2049 represented as a struct, with a member for each
2050 <variant field>. */
c5aa993b 2051
6b850546 2052 LONGEST new_offset = offset;
c906108c 2053
db034ac5
AC
2054 /* This is pretty gross. In G++, the offset in an
2055 anonymous union is relative to the beginning of the
1b831c93
AC
2056 enclosing struct. In the GNU Chill (now deleted
2057 from GDB) implementation of variant records, the
2058 bitpos is zero in an anonymous union field, so we
ac3eeb49 2059 have to add the offset of the union here. */
78134374 2060 if (field_type->code () == TYPE_CODE_STRUCT
1f704f76 2061 || (field_type->num_fields () > 0
b610c045
SM
2062 && field_type->field (0).loc_bitpos () == 0))
2063 new_offset += type->field (i).loc_bitpos () / 8;
c906108c 2064
87a37e5e 2065 search (arg1, new_offset, field_type);
c906108c
SS
2066 }
2067 }
2068 }
2069
c5aa993b 2070 for (i = 0; i < nbases; i++)
c906108c 2071 {
b1af9e97 2072 struct value *v = NULL;
c906108c 2073 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
ac3eeb49 2074 /* If we are looking for baseclasses, this is what we get when
dda83cd7
SM
2075 we hit them. But it could happen that the base part's member
2076 name is not yet filled in. */
87a37e5e 2077 int found_baseclass = (m_looking_for_baseclass
c906108c 2078 && TYPE_BASECLASS_NAME (type, i) != NULL
2820f08f 2079 && (strcmp_iw (m_name, basetype->name ()) == 0));
6b850546 2080 LONGEST boffset = value_embedded_offset (arg1) + offset;
c906108c
SS
2081
2082 if (BASETYPE_VIA_VIRTUAL (type, i))
2083 {
3e3d7139 2084 struct value *v2;
c906108c
SS
2085
2086 boffset = baseclass_offset (type, i,
50888e42 2087 value_contents_for_printing (arg1).data (),
8af8e3bc
PA
2088 value_embedded_offset (arg1) + offset,
2089 value_address (arg1),
2090 arg1);
c906108c 2091
ac3eeb49 2092 /* The virtual base class pointer might have been clobbered
581e13c1 2093 by the user program. Make sure that it still points to a
ac3eeb49 2094 valid memory location. */
c906108c 2095
1a334831
TT
2096 boffset += value_embedded_offset (arg1) + offset;
2097 if (boffset < 0
df86565b 2098 || boffset >= value_enclosing_type (arg1)->length ())
c906108c
SS
2099 {
2100 CORE_ADDR base_addr;
c5aa993b 2101
42ae5230 2102 base_addr = value_address (arg1) + boffset;
08039c9e 2103 v2 = value_at_lazy (basetype, base_addr);
ac3eeb49 2104 if (target_read_memory (base_addr,
50888e42 2105 value_contents_raw (v2).data (),
d0c97917 2106 v2->type ()->length ()) != 0)
8a3fe4f8 2107 error (_("virtual baseclass botch"));
c906108c
SS
2108 }
2109 else
2110 {
1a334831 2111 v2 = value_copy (arg1);
81ae560c 2112 v2->deprecated_set_type (basetype);
1a334831 2113 set_value_embedded_offset (v2, boffset);
c906108c
SS
2114 }
2115
2116 if (found_baseclass)
b1af9e97
TT
2117 v = v2;
2118 else
87a37e5e 2119 search (v2, 0, TYPE_BASECLASS (type, i));
c906108c
SS
2120 }
2121 else if (found_baseclass)
2122 v = value_primitive_field (arg1, offset, i, type);
2123 else
b1af9e97 2124 {
87a37e5e
PA
2125 search (arg1, offset + TYPE_BASECLASS_BITPOS (type, i) / 8,
2126 basetype);
b1af9e97
TT
2127 }
2128
87a37e5e 2129 update_result (v, boffset);
c906108c 2130 }
b1af9e97
TT
2131}
2132
2133/* Helper function used by value_struct_elt to recurse through
8a13d42d
SM
2134 baseclasses. Look for a field NAME in ARG1. Search in it assuming
2135 it has (class) type TYPE. If found, return value, else return NULL.
b1af9e97
TT
2136
2137 If LOOKING_FOR_BASECLASS, then instead of looking for struct
2138 fields, look for a baseclass named NAME. */
2139
2140static struct value *
8a13d42d 2141search_struct_field (const char *name, struct value *arg1,
b1af9e97
TT
2142 struct type *type, int looking_for_baseclass)
2143{
87a37e5e 2144 struct_field_searcher searcher (name, type, looking_for_baseclass);
b1af9e97 2145
87a37e5e
PA
2146 searcher.search (arg1, 0, type);
2147
2148 if (!looking_for_baseclass)
2149 {
2150 const auto &fields = searcher.fields ();
2151
2152 if (fields.empty ())
2153 return nullptr;
2154 else if (fields.size () == 1)
2155 return fields[0].field_value;
2156 else
2157 {
2158 std::string candidates;
2159
2160 for (auto &&candidate : fields)
2161 {
2162 gdb_assert (!candidate.path.empty ());
2163
d0c97917 2164 struct type *field_type = candidate.field_value->type ();
87a37e5e
PA
2165 struct type *struct_type = candidate.path.back ();
2166
2167 std::string path;
2168 bool first = true;
2169 for (struct type *t : candidate.path)
2170 {
2171 if (first)
2172 first = false;
2173 else
2174 path += " -> ";
2175 path += t->name ();
2176 }
2177
2178 candidates += string_printf ("\n '%s %s::%s' (%s)",
2179 TYPE_SAFE_NAME (field_type),
2180 TYPE_SAFE_NAME (struct_type),
2181 name,
2182 path.c_str ());
2183 }
2184
2185 error (_("Request for member '%s' is ambiguous in type '%s'."
2186 " Candidates are:%s"),
2187 name, TYPE_SAFE_NAME (type),
2188 candidates.c_str ());
2189 }
2190 }
2191 else
2192 return searcher.baseclass ();
c906108c
SS
2193}
2194
ac3eeb49 2195/* Helper function used by value_struct_elt to recurse through
581e13c1 2196 baseclasses. Look for a field NAME in ARG1. Adjust the address of
ac3eeb49
MS
2197 ARG1 by OFFSET bytes, and search in it assuming it has (class) type
2198 TYPE.
2199
158cc4fe
AB
2200 ARGS is an optional array of argument values used to help finding NAME.
2201 The contents of ARGS can be adjusted if type coercion is required in
2202 order to find a matching NAME.
79bd4d34 2203
ac3eeb49
MS
2204 If found, return value, else if name matched and args not return
2205 (value) -1, else return NULL. */
c906108c 2206
f23631e4 2207static struct value *
714f19d5 2208search_struct_method (const char *name, struct value **arg1p,
158cc4fe
AB
2209 gdb::optional<gdb::array_view<value *>> args,
2210 LONGEST offset, int *static_memfuncp,
2211 struct type *type)
c906108c
SS
2212{
2213 int i;
f23631e4 2214 struct value *v;
c906108c 2215 int name_matched = 0;
c906108c 2216
f168693b 2217 type = check_typedef (type);
c906108c
SS
2218 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
2219 {
0d5cff50 2220 const char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
a109c7c1 2221
db577aea 2222 if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
c906108c
SS
2223 {
2224 int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1;
2225 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
c906108c 2226
a109c7c1 2227 name_matched = 1;
de17c821 2228 check_stub_method_group (type, i);
158cc4fe 2229 if (j > 0 && !args.has_value ())
3e43a32a
MS
2230 error (_("cannot resolve overloaded method "
2231 "`%s': no arguments supplied"), name);
158cc4fe 2232 else if (j == 0 && !args.has_value ())
c906108c 2233 {
acf5ed49
DJ
2234 v = value_fn_field (arg1p, f, j, type, offset);
2235 if (v != NULL)
2236 return v;
c906108c 2237 }
acf5ed49
DJ
2238 else
2239 while (j >= 0)
2240 {
158cc4fe 2241 gdb_assert (args.has_value ());
acf5ed49 2242 if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j),
a409645d 2243 TYPE_FN_FIELD_TYPE (f, j)->has_varargs (),
1f704f76 2244 TYPE_FN_FIELD_TYPE (f, j)->num_fields (),
13221aec 2245 TYPE_FN_FIELD_ARGS (f, j), *args))
acf5ed49
DJ
2246 {
2247 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
ac3eeb49
MS
2248 return value_virtual_fn_field (arg1p, f, j,
2249 type, offset);
2250 if (TYPE_FN_FIELD_STATIC_P (f, j)
2251 && static_memfuncp)
acf5ed49
DJ
2252 *static_memfuncp = 1;
2253 v = value_fn_field (arg1p, f, j, type, offset);
2254 if (v != NULL)
2255 return v;
2256 }
2257 j--;
2258 }
c906108c
SS
2259 }
2260 }
2261
2262 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
2263 {
6b850546
DT
2264 LONGEST base_offset;
2265 LONGEST this_offset;
c906108c
SS
2266
2267 if (BASETYPE_VIA_VIRTUAL (type, i))
2268 {
086280be 2269 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
8af8e3bc 2270 struct value *base_val;
086280be
UW
2271 const gdb_byte *base_valaddr;
2272
2273 /* The virtual base class pointer might have been
581e13c1 2274 clobbered by the user program. Make sure that it
8301c89e 2275 still points to a valid memory location. */
086280be 2276
df86565b 2277 if (offset < 0 || offset >= type->length ())
c5aa993b 2278 {
6c18f3e0
SP
2279 CORE_ADDR address;
2280
df86565b 2281 gdb::byte_vector tmp (baseclass->length ());
6c18f3e0 2282 address = value_address (*arg1p);
a109c7c1 2283
8af8e3bc 2284 if (target_read_memory (address + offset,
df86565b 2285 tmp.data (), baseclass->length ()) != 0)
086280be 2286 error (_("virtual baseclass botch"));
8af8e3bc
PA
2287
2288 base_val = value_from_contents_and_address (baseclass,
26fcd5d7 2289 tmp.data (),
8af8e3bc 2290 address + offset);
50888e42 2291 base_valaddr = value_contents_for_printing (base_val).data ();
8af8e3bc 2292 this_offset = 0;
c5aa993b
JM
2293 }
2294 else
8af8e3bc
PA
2295 {
2296 base_val = *arg1p;
50888e42 2297 base_valaddr = value_contents_for_printing (*arg1p).data ();
8af8e3bc
PA
2298 this_offset = offset;
2299 }
c5aa993b 2300
086280be 2301 base_offset = baseclass_offset (type, i, base_valaddr,
8af8e3bc
PA
2302 this_offset, value_address (base_val),
2303 base_val);
c5aa993b 2304 }
c906108c
SS
2305 else
2306 {
2307 base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
c5aa993b 2308 }
c906108c
SS
2309 v = search_struct_method (name, arg1p, args, base_offset + offset,
2310 static_memfuncp, TYPE_BASECLASS (type, i));
f23631e4 2311 if (v == (struct value *) - 1)
c906108c
SS
2312 {
2313 name_matched = 1;
2314 }
2315 else if (v)
2316 {
ac3eeb49
MS
2317 /* FIXME-bothner: Why is this commented out? Why is it here? */
2318 /* *arg1p = arg1_tmp; */
c906108c 2319 return v;
c5aa993b 2320 }
c906108c 2321 }
c5aa993b 2322 if (name_matched)
f23631e4 2323 return (struct value *) - 1;
c5aa993b
JM
2324 else
2325 return NULL;
c906108c
SS
2326}
2327
2328/* Given *ARGP, a value of type (pointer to a)* structure/union,
ac3eeb49
MS
2329 extract the component named NAME from the ultimate target
2330 structure/union and return it as a value with its appropriate type.
c906108c
SS
2331 ERR is used in the error message if *ARGP's type is wrong.
2332
2333 C++: ARGS is a list of argument types to aid in the selection of
13221aec 2334 an appropriate method. Also, handle derived types.
c906108c
SS
2335
2336 STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
2337 where the truthvalue of whether the function that was resolved was
2338 a static member function or not is stored.
2339
ac3eeb49
MS
2340 ERR is an error message to be printed in case the field is not
2341 found. */
c906108c 2342
f23631e4 2343struct value *
158cc4fe
AB
2344value_struct_elt (struct value **argp,
2345 gdb::optional<gdb::array_view<value *>> args,
714f19d5 2346 const char *name, int *static_memfuncp, const char *err)
c906108c 2347{
52f0bd74 2348 struct type *t;
f23631e4 2349 struct value *v;
c906108c 2350
994b9211 2351 *argp = coerce_array (*argp);
c906108c 2352
d0c97917 2353 t = check_typedef ((*argp)->type ());
c906108c
SS
2354
2355 /* Follow pointers until we get to a non-pointer. */
2356
809f3be1 2357 while (t->is_pointer_or_reference ())
c906108c
SS
2358 {
2359 *argp = value_ind (*argp);
2360 /* Don't coerce fn pointer to fn and then back again! */
d0c97917 2361 if (check_typedef ((*argp)->type ())->code () != TYPE_CODE_FUNC)
994b9211 2362 *argp = coerce_array (*argp);
d0c97917 2363 t = check_typedef ((*argp)->type ());
c906108c
SS
2364 }
2365
78134374
SM
2366 if (t->code () != TYPE_CODE_STRUCT
2367 && t->code () != TYPE_CODE_UNION)
3e43a32a
MS
2368 error (_("Attempt to extract a component of a value that is not a %s."),
2369 err);
c906108c
SS
2370
2371 /* Assume it's not, unless we see that it is. */
2372 if (static_memfuncp)
c5aa993b 2373 *static_memfuncp = 0;
c906108c 2374
158cc4fe 2375 if (!args.has_value ())
c906108c
SS
2376 {
2377 /* if there are no arguments ...do this... */
2378
ac3eeb49 2379 /* Try as a field first, because if we succeed, there is less
dda83cd7 2380 work to be done. */
8a13d42d 2381 v = search_struct_field (name, *argp, t, 0);
c906108c
SS
2382 if (v)
2383 return v;
2384
87e10e9c
BH
2385 if (current_language->la_language == language_fortran)
2386 {
2387 /* If it is not a field it is the type name of an inherited
2388 structure. */
2389 v = search_struct_field (name, *argp, t, 1);
2390 if (v)
2391 return v;
2392 }
2393
c906108c 2394 /* C++: If it was not found as a data field, then try to
dda83cd7 2395 return it as a pointer to a method. */
13221aec 2396 v = search_struct_method (name, argp, args, 0,
ac3eeb49 2397 static_memfuncp, t);
c906108c 2398
f23631e4 2399 if (v == (struct value *) - 1)
55b39184 2400 error (_("Cannot take address of method %s."), name);
c906108c
SS
2401 else if (v == 0)
2402 {
2403 if (TYPE_NFN_FIELDS (t))
8a3fe4f8 2404 error (_("There is no member or method named %s."), name);
c906108c 2405 else
8a3fe4f8 2406 error (_("There is no member named %s."), name);
c906108c
SS
2407 }
2408 return v;
2409 }
2410
13221aec 2411 v = search_struct_method (name, argp, args, 0,
8301c89e 2412 static_memfuncp, t);
13221aec 2413
f23631e4 2414 if (v == (struct value *) - 1)
c906108c 2415 {
3e43a32a
MS
2416 error (_("One of the arguments you tried to pass to %s could not "
2417 "be converted to what the function wants."), name);
c906108c
SS
2418 }
2419 else if (v == 0)
2420 {
ac3eeb49 2421 /* See if user tried to invoke data as function. If so, hand it
dda83cd7
SM
2422 back. If it's not callable (i.e., a pointer to function),
2423 gdb should give an error. */
8a13d42d 2424 v = search_struct_field (name, *argp, t, 0);
fa8de41e
TT
2425 /* If we found an ordinary field, then it is not a method call.
2426 So, treat it as if it were a static member function. */
2427 if (v && static_memfuncp)
2428 *static_memfuncp = 1;
c906108c
SS
2429 }
2430
2431 if (!v)
79afc5ef 2432 throw_error (NOT_FOUND_ERROR,
dda83cd7 2433 _("Structure has no component named %s."), name);
c906108c
SS
2434 return v;
2435}
2436
b5b08fb4
SC
2437/* Given *ARGP, a value of type structure or union, or a pointer/reference
2438 to a structure or union, extract and return its component (field) of
2439 type FTYPE at the specified BITPOS.
2440 Throw an exception on error. */
2441
2442struct value *
2443value_struct_elt_bitpos (struct value **argp, int bitpos, struct type *ftype,
2444 const char *err)
2445{
2446 struct type *t;
b5b08fb4 2447 int i;
b5b08fb4
SC
2448
2449 *argp = coerce_array (*argp);
2450
d0c97917 2451 t = check_typedef ((*argp)->type ());
b5b08fb4 2452
809f3be1 2453 while (t->is_pointer_or_reference ())
b5b08fb4
SC
2454 {
2455 *argp = value_ind (*argp);
d0c97917 2456 if (check_typedef ((*argp)->type ())->code () != TYPE_CODE_FUNC)
b5b08fb4 2457 *argp = coerce_array (*argp);
d0c97917 2458 t = check_typedef ((*argp)->type ());
b5b08fb4
SC
2459 }
2460
78134374
SM
2461 if (t->code () != TYPE_CODE_STRUCT
2462 && t->code () != TYPE_CODE_UNION)
b5b08fb4
SC
2463 error (_("Attempt to extract a component of a value that is not a %s."),
2464 err);
2465
1f704f76 2466 for (i = TYPE_N_BASECLASSES (t); i < t->num_fields (); i++)
b5b08fb4 2467 {
ceacbf6e 2468 if (!field_is_static (&t->field (i))
b610c045 2469 && bitpos == t->field (i).loc_bitpos ()
940da03e 2470 && types_equal (ftype, t->field (i).type ()))
b5b08fb4
SC
2471 return value_primitive_field (*argp, 0, i, t);
2472 }
2473
2474 error (_("No field with matching bitpos and type."));
2475
2476 /* Never hit. */
2477 return NULL;
2478}
2479
ac3eeb49 2480/* Search through the methods of an object (and its bases) to find a
38139a96 2481 specified method. Return a reference to the fn_field list METHODS of
233e8b28
SC
2482 overloaded instances defined in the source language. If available
2483 and matching, a vector of matching xmethods defined in extension
38139a96 2484 languages are also returned in XMETHODS.
ac3eeb49
MS
2485
2486 Helper function for value_find_oload_list.
2487 ARGP is a pointer to a pointer to a value (the object).
2488 METHOD is a string containing the method name.
2489 OFFSET is the offset within the value.
2490 TYPE is the assumed type of the object.
38139a96
PA
2491 METHODS is a pointer to the matching overloaded instances defined
2492 in the source language. Since this is a recursive function,
2493 *METHODS should be set to NULL when calling this function.
233e8b28
SC
2494 NUM_FNS is the number of overloaded instances. *NUM_FNS should be set to
2495 0 when calling this function.
38139a96 2496 XMETHODS is the vector of matching xmethod workers. *XMETHODS
233e8b28 2497 should also be set to NULL when calling this function.
ac3eeb49
MS
2498 BASETYPE is set to the actual type of the subobject where the
2499 method is found.
581e13c1 2500 BOFFSET is the offset of the base subobject where the method is found. */
c906108c 2501
233e8b28 2502static void
714f19d5 2503find_method_list (struct value **argp, const char *method,
6b850546 2504 LONGEST offset, struct type *type,
38139a96
PA
2505 gdb::array_view<fn_field> *methods,
2506 std::vector<xmethod_worker_up> *xmethods,
6b850546 2507 struct type **basetype, LONGEST *boffset)
c906108c
SS
2508{
2509 int i;
233e8b28 2510 struct fn_field *f = NULL;
c906108c 2511
38139a96 2512 gdb_assert (methods != NULL && xmethods != NULL);
f168693b 2513 type = check_typedef (type);
c906108c 2514
233e8b28
SC
2515 /* First check in object itself.
2516 This function is called recursively to search through base classes.
2517 If there is a source method match found at some stage, then we need not
2518 look for source methods in consequent recursive calls. */
38139a96 2519 if (methods->empty ())
c906108c 2520 {
233e8b28 2521 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
c5aa993b 2522 {
233e8b28
SC
2523 /* pai: FIXME What about operators and type conversions? */
2524 const char *fn_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
2525
2526 if (fn_field_name && (strcmp_iw (fn_field_name, method) == 0))
2527 {
2528 int len = TYPE_FN_FIELDLIST_LENGTH (type, i);
2529 f = TYPE_FN_FIELDLIST1 (type, i);
38139a96 2530 *methods = gdb::make_array_view (f, len);
4a1970e4 2531
233e8b28
SC
2532 *basetype = type;
2533 *boffset = offset;
4a1970e4 2534
233e8b28
SC
2535 /* Resolve any stub methods. */
2536 check_stub_method_group (type, i);
4a1970e4 2537
233e8b28
SC
2538 break;
2539 }
c5aa993b
JM
2540 }
2541 }
2542
233e8b28
SC
2543 /* Unlike source methods, xmethods can be accumulated over successive
2544 recursive calls. In other words, an xmethod named 'm' in a class
2545 will not hide an xmethod named 'm' in its base class(es). We want
2546 it to be this way because xmethods are after all convenience functions
2547 and hence there is no point restricting them with something like method
2548 hiding. Moreover, if hiding is done for xmethods as well, then we will
2549 have to provide a mechanism to un-hide (like the 'using' construct). */
38139a96 2550 get_matching_xmethod_workers (type, method, xmethods);
233e8b28
SC
2551
2552 /* If source methods are not found in current class, look for them in the
2553 base classes. We also have to go through the base classes to gather
2554 extension methods. */
c906108c
SS
2555 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
2556 {
6b850546 2557 LONGEST base_offset;
a109c7c1 2558
c906108c
SS
2559 if (BASETYPE_VIA_VIRTUAL (type, i))
2560 {
086280be 2561 base_offset = baseclass_offset (type, i,
50888e42 2562 value_contents_for_printing (*argp).data (),
76675c4d 2563 (*argp)->offset () + offset,
8af8e3bc 2564 value_address (*argp), *argp);
c5aa993b 2565 }
ac3eeb49
MS
2566 else /* Non-virtual base, simply use bit position from debug
2567 info. */
c906108c
SS
2568 {
2569 base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
c5aa993b 2570 }
233e8b28
SC
2571
2572 find_method_list (argp, method, base_offset + offset,
38139a96
PA
2573 TYPE_BASECLASS (type, i), methods,
2574 xmethods, basetype, boffset);
c906108c 2575 }
c906108c
SS
2576}
2577
233e8b28
SC
2578/* Return the list of overloaded methods of a specified name. The methods
2579 could be those GDB finds in the binary, or xmethod. Methods found in
38139a96
PA
2580 the binary are returned in METHODS, and xmethods are returned in
2581 XMETHODS.
ac3eeb49
MS
2582
2583 ARGP is a pointer to a pointer to a value (the object).
2584 METHOD is the method name.
2585 OFFSET is the offset within the value contents.
38139a96
PA
2586 METHODS is the list of matching overloaded instances defined in
2587 the source language.
2588 XMETHODS is the vector of matching xmethod workers defined in
233e8b28 2589 extension languages.
ac3eeb49
MS
2590 BASETYPE is set to the type of the base subobject that defines the
2591 method.
581e13c1 2592 BOFFSET is the offset of the base subobject which defines the method. */
c906108c 2593
233e8b28 2594static void
714f19d5 2595value_find_oload_method_list (struct value **argp, const char *method,
85cca2bc 2596 LONGEST offset,
38139a96
PA
2597 gdb::array_view<fn_field> *methods,
2598 std::vector<xmethod_worker_up> *xmethods,
6b850546 2599 struct type **basetype, LONGEST *boffset)
c906108c 2600{
c5aa993b 2601 struct type *t;
c906108c 2602
d0c97917 2603 t = check_typedef ((*argp)->type ());
c906108c 2604
ac3eeb49 2605 /* Code snarfed from value_struct_elt. */
809f3be1 2606 while (t->is_pointer_or_reference ())
c906108c
SS
2607 {
2608 *argp = value_ind (*argp);
2609 /* Don't coerce fn pointer to fn and then back again! */
d0c97917 2610 if (check_typedef ((*argp)->type ())->code () != TYPE_CODE_FUNC)
994b9211 2611 *argp = coerce_array (*argp);
d0c97917 2612 t = check_typedef ((*argp)->type ());
c906108c 2613 }
c5aa993b 2614
78134374
SM
2615 if (t->code () != TYPE_CODE_STRUCT
2616 && t->code () != TYPE_CODE_UNION)
3e43a32a
MS
2617 error (_("Attempt to extract a component of a "
2618 "value that is not a struct or union"));
c5aa993b 2619
38139a96 2620 gdb_assert (methods != NULL && xmethods != NULL);
233e8b28
SC
2621
2622 /* Clear the lists. */
38139a96
PA
2623 *methods = {};
2624 xmethods->clear ();
233e8b28 2625
38139a96 2626 find_method_list (argp, method, 0, t, methods, xmethods,
233e8b28 2627 basetype, boffset);
c906108c
SS
2628}
2629
041de3d7
BL
2630/* Helper function for find_overload_match. If no matches were
2631 found, this function may generate a hint for the user that some
2632 of the relevant types are incomplete, so GDB can't evaluate
2633 type relationships to properly evaluate overloads.
2634
2635 If no incomplete types are present, an empty string is returned. */
2636static std::string
2637incomplete_type_hint (gdb::array_view<value *> args)
2638{
2639 int incomplete_types = 0;
2640 std::string incomplete_arg_names;
2641 for (const struct value *arg : args)
2642 {
d0c97917 2643 struct type *t = arg->type ();
041de3d7
BL
2644 while (t->code () == TYPE_CODE_PTR)
2645 t = t->target_type ();
2646 if (t->is_stub ())
2647 {
2648 string_file buffer;
2649 if (incomplete_types > 0)
2650 incomplete_arg_names += ", ";
2651
d0c97917 2652 current_language->print_type (arg->type (), "", &buffer,
041de3d7
BL
2653 -1, 0, &type_print_raw_options);
2654
2655 incomplete_types++;
2656 incomplete_arg_names += buffer.string ();
2657 }
2658 }
2659 std::string hint;
2660 if (incomplete_types > 1)
2661 hint = string_printf (_("\nThe types: '%s' aren't fully known to GDB."
2662 " Please cast them directly to the desired"
2663 " typed in the function call."),
2664 incomplete_arg_names.c_str ());
2665 else if (incomplete_types == 1)
2666 hint = string_printf (_("\nThe type: '%s' isn't fully known to GDB."
2667 " Please cast it directly to the desired"
2668 " typed in the function call."),
2669 incomplete_arg_names.c_str ());
2670 return hint;
2671}
2672
6b1747cd
PA
2673/* Given an array of arguments (ARGS) (which includes an entry for
2674 "this" in the case of C++ methods), the NAME of a function, and
2675 whether it's a method or not (METHOD), find the best function that
2676 matches on the argument types according to the overload resolution
2677 rules.
c906108c 2678
4c3376c8
SW
2679 METHOD can be one of three values:
2680 NON_METHOD for non-member functions.
2681 METHOD: for member functions.
2682 BOTH: used for overload resolution of operators where the
2683 candidates are expected to be either member or non member
581e13c1 2684 functions. In this case the first argument ARGTYPES
4c3376c8
SW
2685 (representing 'this') is expected to be a reference to the
2686 target object, and will be dereferenced when attempting the
2687 non-member search.
2688
c906108c
SS
2689 In the case of class methods, the parameter OBJ is an object value
2690 in which to search for overloaded methods.
2691
2692 In the case of non-method functions, the parameter FSYM is a symbol
2693 corresponding to one of the overloaded functions.
2694
2695 Return value is an integer: 0 -> good match, 10 -> debugger applied
2696 non-standard coercions, 100 -> incompatible.
2697
2698 If a method is being searched for, VALP will hold the value.
ac3eeb49
MS
2699 If a non-method is being searched for, SYMP will hold the symbol
2700 for it.
c906108c
SS
2701
2702 If a method is being searched for, and it is a static method,
2703 then STATICP will point to a non-zero value.
2704
7322dca9
SW
2705 If NO_ADL argument dependent lookup is disabled. This is used to prevent
2706 ADL overload candidates when performing overload resolution for a fully
2707 qualified name.
2708
e66d4446
SC
2709 If NOSIDE is EVAL_AVOID_SIDE_EFFECTS, then OBJP's memory cannot be
2710 read while picking the best overload match (it may be all zeroes and thus
2711 not have a vtable pointer), in which case skip virtual function lookup.
2712 This is ok as typically EVAL_AVOID_SIDE_EFFECTS is only used to determine
2713 the result type.
2714
c906108c
SS
2715 Note: This function does *not* check the value of
2716 overload_resolution. Caller must check it to see whether overload
581e13c1 2717 resolution is permitted. */
c906108c
SS
2718
2719int
6b1747cd 2720find_overload_match (gdb::array_view<value *> args,
4c3376c8 2721 const char *name, enum oload_search_type method,
28c64fc2 2722 struct value **objp, struct symbol *fsym,
ac3eeb49 2723 struct value **valp, struct symbol **symp,
e66d4446
SC
2724 int *staticp, const int no_adl,
2725 const enum noside noside)
c906108c 2726{
7f8c9282 2727 struct value *obj = (objp ? *objp : NULL);
d0c97917 2728 struct type *obj_type = obj ? obj->type () : NULL;
ac3eeb49 2729 /* Index of best overloaded function. */
4c3376c8
SW
2730 int func_oload_champ = -1;
2731 int method_oload_champ = -1;
233e8b28
SC
2732 int src_method_oload_champ = -1;
2733 int ext_method_oload_champ = -1;
4c3376c8 2734
ac3eeb49 2735 /* The measure for the current best match. */
82ceee50
PA
2736 badness_vector method_badness;
2737 badness_vector func_badness;
2738 badness_vector ext_method_badness;
2739 badness_vector src_method_badness;
4c3376c8 2740
f23631e4 2741 struct value *temp = obj;
ac3eeb49 2742 /* For methods, the list of overloaded methods. */
38139a96 2743 gdb::array_view<fn_field> methods;
ac3eeb49 2744 /* For non-methods, the list of overloaded function symbols. */
38139a96 2745 std::vector<symbol *> functions;
ba18742c 2746 /* For xmethods, the vector of xmethod workers. */
38139a96 2747 std::vector<xmethod_worker_up> xmethods;
c5aa993b 2748 struct type *basetype = NULL;
6b850546 2749 LONGEST boffset;
7322dca9 2750
8d577d32 2751 const char *obj_type_name = NULL;
7322dca9 2752 const char *func_name = NULL;
06d3e5b0 2753 gdb::unique_xmalloc_ptr<char> temp_func;
8d577d32 2754 enum oload_classification match_quality;
4c3376c8 2755 enum oload_classification method_match_quality = INCOMPATIBLE;
233e8b28
SC
2756 enum oload_classification src_method_match_quality = INCOMPATIBLE;
2757 enum oload_classification ext_method_match_quality = INCOMPATIBLE;
4c3376c8 2758 enum oload_classification func_match_quality = INCOMPATIBLE;
c906108c 2759
ac3eeb49 2760 /* Get the list of overloaded methods or functions. */
4c3376c8 2761 if (method == METHOD || method == BOTH)
c906108c 2762 {
a2ca50ae 2763 gdb_assert (obj);
94af9270
KS
2764
2765 /* OBJ may be a pointer value rather than the object itself. */
2766 obj = coerce_ref (obj);
d0c97917 2767 while (check_typedef (obj->type ())->code () == TYPE_CODE_PTR)
94af9270 2768 obj = coerce_ref (value_ind (obj));
d0c97917 2769 obj_type_name = obj->type ()->name ();
94af9270
KS
2770
2771 /* First check whether this is a data member, e.g. a pointer to
2772 a function. */
d0c97917 2773 if (check_typedef (obj->type ())->code () == TYPE_CODE_STRUCT)
94af9270 2774 {
8a13d42d 2775 *valp = search_struct_field (name, obj,
d0c97917 2776 check_typedef (obj->type ()), 0);
94af9270
KS
2777 if (*valp)
2778 {
2779 *staticp = 1;
2780 return 0;
2781 }
2782 }
c906108c 2783
4c3376c8 2784 /* Retrieve the list of methods with the name NAME. */
38139a96
PA
2785 value_find_oload_method_list (&temp, name, 0, &methods,
2786 &xmethods, &basetype, &boffset);
4c3376c8 2787 /* If this is a method only search, and no methods were found
dda83cd7 2788 the search has failed. */
38139a96 2789 if (method == METHOD && methods.empty () && xmethods.empty ())
8a3fe4f8 2790 error (_("Couldn't find method %s%s%s"),
c5aa993b
JM
2791 obj_type_name,
2792 (obj_type_name && *obj_type_name) ? "::" : "",
2793 name);
4a1970e4 2794 /* If we are dealing with stub method types, they should have
ac3eeb49
MS
2795 been resolved by find_method_list via
2796 value_find_oload_method_list above. */
38139a96 2797 if (!methods.empty ())
4c3376c8 2798 {
38139a96 2799 gdb_assert (TYPE_SELF_TYPE (methods[0].type) != NULL);
4c3376c8 2800
85cca2bc
PA
2801 src_method_oload_champ
2802 = find_oload_champ (args,
38139a96
PA
2803 methods.size (),
2804 methods.data (), NULL, NULL,
85cca2bc 2805 &src_method_badness);
233e8b28
SC
2806
2807 src_method_match_quality = classify_oload_match
6b1747cd 2808 (src_method_badness, args.size (),
38139a96 2809 oload_method_static_p (methods.data (), src_method_oload_champ));
233e8b28 2810 }
4c3376c8 2811
38139a96 2812 if (!xmethods.empty ())
233e8b28 2813 {
85cca2bc
PA
2814 ext_method_oload_champ
2815 = find_oload_champ (args,
38139a96
PA
2816 xmethods.size (),
2817 NULL, xmethods.data (), NULL,
85cca2bc 2818 &ext_method_badness);
233e8b28 2819 ext_method_match_quality = classify_oload_match (ext_method_badness,
6b1747cd 2820 args.size (), 0);
4c3376c8
SW
2821 }
2822
233e8b28
SC
2823 if (src_method_oload_champ >= 0 && ext_method_oload_champ >= 0)
2824 {
2825 switch (compare_badness (ext_method_badness, src_method_badness))
2826 {
2827 case 0: /* Src method and xmethod are equally good. */
233e8b28
SC
2828 /* If src method and xmethod are equally good, then
2829 xmethod should be the winner. Hence, fall through to the
2830 case where a xmethod is better than the source
2831 method, except when the xmethod match quality is
2832 non-standard. */
2833 /* FALLTHROUGH */
2834 case 1: /* Src method and ext method are incompatible. */
2835 /* If ext method match is not standard, then let source method
2836 win. Otherwise, fallthrough to let xmethod win. */
2837 if (ext_method_match_quality != STANDARD)
2838 {
2839 method_oload_champ = src_method_oload_champ;
2840 method_badness = src_method_badness;
2841 ext_method_oload_champ = -1;
2842 method_match_quality = src_method_match_quality;
2843 break;
2844 }
2845 /* FALLTHROUGH */
2846 case 2: /* Ext method is champion. */
2847 method_oload_champ = ext_method_oload_champ;
2848 method_badness = ext_method_badness;
2849 src_method_oload_champ = -1;
2850 method_match_quality = ext_method_match_quality;
2851 break;
2852 case 3: /* Src method is champion. */
2853 method_oload_champ = src_method_oload_champ;
2854 method_badness = src_method_badness;
2855 ext_method_oload_champ = -1;
2856 method_match_quality = src_method_match_quality;
2857 break;
2858 default:
2859 gdb_assert_not_reached ("Unexpected overload comparison "
2860 "result");
2861 break;
2862 }
2863 }
2864 else if (src_method_oload_champ >= 0)
2865 {
2866 method_oload_champ = src_method_oload_champ;
2867 method_badness = src_method_badness;
2868 method_match_quality = src_method_match_quality;
2869 }
2870 else if (ext_method_oload_champ >= 0)
2871 {
2872 method_oload_champ = ext_method_oload_champ;
2873 method_badness = ext_method_badness;
2874 method_match_quality = ext_method_match_quality;
2875 }
c906108c 2876 }
4c3376c8
SW
2877
2878 if (method == NON_METHOD || method == BOTH)
c906108c 2879 {
7322dca9 2880 const char *qualified_name = NULL;
c906108c 2881
b021a221 2882 /* If the overload match is being search for both as a method
dda83cd7
SM
2883 and non member function, the first argument must now be
2884 dereferenced. */
4c3376c8 2885 if (method == BOTH)
2b214ea6 2886 args[0] = value_ind (args[0]);
4c3376c8 2887
7322dca9 2888 if (fsym)
dda83cd7
SM
2889 {
2890 qualified_name = fsym->natural_name ();
7322dca9 2891
dda83cd7 2892 /* If we have a function with a C++ name, try to extract just
7322dca9
SW
2893 the function part. Do not try this for non-functions (e.g.
2894 function pointers). */
dda83cd7 2895 if (qualified_name
5f9c5a63 2896 && (check_typedef (fsym->type ())->code ()
78134374 2897 == TYPE_CODE_FUNC))
dda83cd7 2898 {
b926417a 2899 temp_func = cp_func_name (qualified_name);
7322dca9
SW
2900
2901 /* If cp_func_name did not remove anything, the name of the
dda83cd7
SM
2902 symbol did not include scope or argument types - it was
2903 probably a C-style function. */
06d3e5b0 2904 if (temp_func != nullptr)
7322dca9 2905 {
06d3e5b0 2906 if (strcmp (temp_func.get (), qualified_name) == 0)
7322dca9
SW
2907 func_name = NULL;
2908 else
06d3e5b0 2909 func_name = temp_func.get ();
7322dca9 2910 }
dda83cd7
SM
2911 }
2912 }
7322dca9 2913 else
94af9270 2914 {
7322dca9
SW
2915 func_name = name;
2916 qualified_name = name;
94af9270 2917 }
d9639e13 2918
94af9270
KS
2919 /* If there was no C++ name, this must be a C-style function or
2920 not a function at all. Just return the same symbol. Do the
2921 same if cp_func_name fails for some reason. */
8d577d32 2922 if (func_name == NULL)
dda83cd7 2923 {
917317f4 2924 *symp = fsym;
dda83cd7
SM
2925 return 0;
2926 }
917317f4 2927
6b1747cd 2928 func_oload_champ = find_oload_champ_namespace (args,
dda83cd7
SM
2929 func_name,
2930 qualified_name,
2931 &functions,
2932 &func_badness,
2933 no_adl);
8d577d32 2934
4c3376c8 2935 if (func_oload_champ >= 0)
6b1747cd
PA
2936 func_match_quality = classify_oload_match (func_badness,
2937 args.size (), 0);
8d577d32
DC
2938 }
2939
7322dca9 2940 /* Did we find a match ? */
4c3376c8 2941 if (method_oload_champ == -1 && func_oload_champ == -1)
79afc5ef 2942 throw_error (NOT_FOUND_ERROR,
dda83cd7
SM
2943 _("No symbol \"%s\" in current context."),
2944 name);
8d577d32 2945
4c3376c8
SW
2946 /* If we have found both a method match and a function
2947 match, find out which one is better, and calculate match
2948 quality. */
2949 if (method_oload_champ >= 0 && func_oload_champ >= 0)
2950 {
2951 switch (compare_badness (func_badness, method_badness))
dda83cd7 2952 {
4c3376c8 2953 case 0: /* Top two contenders are equally good. */
b021a221
MS
2954 /* FIXME: GDB does not support the general ambiguous case.
2955 All candidates should be collected and presented the
2956 user. */
4c3376c8
SW
2957 error (_("Ambiguous overload resolution"));
2958 break;
2959 case 1: /* Incomparable top contenders. */
2960 /* This is an error incompatible candidates
2961 should not have been proposed. */
3e43a32a
MS
2962 error (_("Internal error: incompatible "
2963 "overload candidates proposed"));
4c3376c8
SW
2964 break;
2965 case 2: /* Function champion. */
2966 method_oload_champ = -1;
2967 match_quality = func_match_quality;
2968 break;
2969 case 3: /* Method champion. */
2970 func_oload_champ = -1;
2971 match_quality = method_match_quality;
2972 break;
2973 default:
2974 error (_("Internal error: unexpected overload comparison result"));
2975 break;
dda83cd7 2976 }
4c3376c8
SW
2977 }
2978 else
2979 {
2980 /* We have either a method match or a function match. */
2981 if (method_oload_champ >= 0)
2982 match_quality = method_match_quality;
2983 else
2984 match_quality = func_match_quality;
2985 }
8d577d32
DC
2986
2987 if (match_quality == INCOMPATIBLE)
2988 {
041de3d7 2989 std::string hint = incomplete_type_hint (args);
4c3376c8 2990 if (method == METHOD)
05e8d17b 2991 error (_("Cannot resolve method %s%s%s to any overloaded instance%s"),
8d577d32
DC
2992 obj_type_name,
2993 (obj_type_name && *obj_type_name) ? "::" : "",
041de3d7 2994 name, hint.c_str ());
8d577d32 2995 else
05e8d17b 2996 error (_("Cannot resolve function %s to any overloaded instance%s"),
041de3d7 2997 func_name, hint.c_str ());
8d577d32
DC
2998 }
2999 else if (match_quality == NON_STANDARD)
3000 {
4c3376c8 3001 if (method == METHOD)
3e43a32a
MS
3002 warning (_("Using non-standard conversion to match "
3003 "method %s%s%s to supplied arguments"),
8d577d32
DC
3004 obj_type_name,
3005 (obj_type_name && *obj_type_name) ? "::" : "",
3006 name);
3007 else
3e43a32a
MS
3008 warning (_("Using non-standard conversion to match "
3009 "function %s to supplied arguments"),
8d577d32
DC
3010 func_name);
3011 }
3012
4c3376c8 3013 if (staticp != NULL)
38139a96 3014 *staticp = oload_method_static_p (methods.data (), method_oload_champ);
4c3376c8
SW
3015
3016 if (method_oload_champ >= 0)
8d577d32 3017 {
233e8b28
SC
3018 if (src_method_oload_champ >= 0)
3019 {
38139a96 3020 if (TYPE_FN_FIELD_VIRTUAL_P (methods, method_oload_champ)
e66d4446
SC
3021 && noside != EVAL_AVOID_SIDE_EFFECTS)
3022 {
38139a96 3023 *valp = value_virtual_fn_field (&temp, methods.data (),
e66d4446
SC
3024 method_oload_champ, basetype,
3025 boffset);
3026 }
233e8b28 3027 else
38139a96 3028 *valp = value_fn_field (&temp, methods.data (),
85cca2bc 3029 method_oload_champ, basetype, boffset);
233e8b28 3030 }
8d577d32 3031 else
ba18742c 3032 *valp = value_from_xmethod
38139a96 3033 (std::move (xmethods[ext_method_oload_champ]));
8d577d32
DC
3034 }
3035 else
38139a96 3036 *symp = functions[func_oload_champ];
8d577d32
DC
3037
3038 if (objp)
3039 {
d0c97917 3040 struct type *temp_type = check_typedef (temp->type ());
da096638 3041 struct type *objtype = check_typedef (obj_type);
a109c7c1 3042
78134374 3043 if (temp_type->code () != TYPE_CODE_PTR
809f3be1 3044 && objtype->is_pointer_or_reference ())
8d577d32
DC
3045 {
3046 temp = value_addr (temp);
3047 }
3048 *objp = temp;
3049 }
7322dca9 3050
8d577d32
DC
3051 switch (match_quality)
3052 {
3053 case INCOMPATIBLE:
3054 return 100;
3055 case NON_STANDARD:
3056 return 10;
3057 default: /* STANDARD */
3058 return 0;
3059 }
3060}
3061
3062/* Find the best overload match, searching for FUNC_NAME in namespaces
3063 contained in QUALIFIED_NAME until it either finds a good match or
3064 runs out of namespaces. It stores the overloaded functions in
82ceee50 3065 *OLOAD_SYMS, and the badness vector in *OLOAD_CHAMP_BV. If NO_ADL,
30baf67b 3066 argument dependent lookup is not performed. */
8d577d32
DC
3067
3068static int
6b1747cd 3069find_oload_champ_namespace (gdb::array_view<value *> args,
8d577d32
DC
3070 const char *func_name,
3071 const char *qualified_name,
0891c3cc 3072 std::vector<symbol *> *oload_syms,
82ceee50 3073 badness_vector *oload_champ_bv,
7322dca9 3074 const int no_adl)
8d577d32
DC
3075{
3076 int oload_champ;
3077
6b1747cd 3078 find_oload_champ_namespace_loop (args,
8d577d32
DC
3079 func_name,
3080 qualified_name, 0,
3081 oload_syms, oload_champ_bv,
7322dca9
SW
3082 &oload_champ,
3083 no_adl);
8d577d32
DC
3084
3085 return oload_champ;
3086}
3087
3088/* Helper function for find_oload_champ_namespace; NAMESPACE_LEN is
3089 how deep we've looked for namespaces, and the champ is stored in
3090 OLOAD_CHAMP. The return value is 1 if the champ is a good one, 0
7322dca9 3091 if it isn't. Other arguments are the same as in
82ceee50 3092 find_oload_champ_namespace. */
8d577d32
DC
3093
3094static int
6b1747cd 3095find_oload_champ_namespace_loop (gdb::array_view<value *> args,
8d577d32
DC
3096 const char *func_name,
3097 const char *qualified_name,
3098 int namespace_len,
0891c3cc 3099 std::vector<symbol *> *oload_syms,
82ceee50 3100 badness_vector *oload_champ_bv,
7322dca9
SW
3101 int *oload_champ,
3102 const int no_adl)
8d577d32
DC
3103{
3104 int next_namespace_len = namespace_len;
3105 int searched_deeper = 0;
8d577d32 3106 int new_oload_champ;
8d577d32
DC
3107 char *new_namespace;
3108
3109 if (next_namespace_len != 0)
3110 {
3111 gdb_assert (qualified_name[next_namespace_len] == ':');
3112 next_namespace_len += 2;
c906108c 3113 }
ac3eeb49
MS
3114 next_namespace_len +=
3115 cp_find_first_component (qualified_name + next_namespace_len);
8d577d32 3116
581e13c1 3117 /* First, see if we have a deeper namespace we can search in.
ac3eeb49 3118 If we get a good match there, use it. */
8d577d32
DC
3119
3120 if (qualified_name[next_namespace_len] == ':')
3121 {
3122 searched_deeper = 1;
3123
6b1747cd 3124 if (find_oload_champ_namespace_loop (args,
8d577d32
DC
3125 func_name, qualified_name,
3126 next_namespace_len,
3127 oload_syms, oload_champ_bv,
7322dca9 3128 oload_champ, no_adl))
8d577d32
DC
3129 {
3130 return 1;
3131 }
3132 };
3133
3134 /* If we reach here, either we're in the deepest namespace or we
3135 didn't find a good match in a deeper namespace. But, in the
3136 latter case, we still have a bad match in a deeper namespace;
3137 note that we might not find any match at all in the current
3138 namespace. (There's always a match in the deepest namespace,
3139 because this overload mechanism only gets called if there's a
3140 function symbol to start off with.) */
3141
224c3ddb 3142 new_namespace = (char *) alloca (namespace_len + 1);
8d577d32
DC
3143 strncpy (new_namespace, qualified_name, namespace_len);
3144 new_namespace[namespace_len] = '\0';
0891c3cc
PA
3145
3146 std::vector<symbol *> new_oload_syms
3147 = make_symbol_overload_list (func_name, new_namespace);
7322dca9
SW
3148
3149 /* If we have reached the deepest level perform argument
3150 determined lookup. */
3151 if (!searched_deeper && !no_adl)
da096638
KS
3152 {
3153 int ix;
3154 struct type **arg_types;
3155
3156 /* Prepare list of argument types for overload resolution. */
3157 arg_types = (struct type **)
6b1747cd
PA
3158 alloca (args.size () * (sizeof (struct type *)));
3159 for (ix = 0; ix < args.size (); ix++)
d0c97917 3160 arg_types[ix] = args[ix]->type ();
0891c3cc
PA
3161 add_symbol_overload_list_adl ({arg_types, args.size ()}, func_name,
3162 &new_oload_syms);
da096638 3163 }
7322dca9 3164
82ceee50 3165 badness_vector new_oload_champ_bv;
85cca2bc
PA
3166 new_oload_champ = find_oload_champ (args,
3167 new_oload_syms.size (),
0891c3cc 3168 NULL, NULL, new_oload_syms.data (),
8d577d32
DC
3169 &new_oload_champ_bv);
3170
3171 /* Case 1: We found a good match. Free earlier matches (if any),
3172 and return it. Case 2: We didn't find a good match, but we're
3173 not the deepest function. Then go with the bad match that the
3174 deeper function found. Case 3: We found a bad match, and we're
3175 the deepest function. Then return what we found, even though
3176 it's a bad match. */
3177
3178 if (new_oload_champ != -1
6b1747cd 3179 && classify_oload_match (new_oload_champ_bv, args.size (), 0) == STANDARD)
8d577d32 3180 {
0891c3cc 3181 *oload_syms = std::move (new_oload_syms);
8d577d32 3182 *oload_champ = new_oload_champ;
82ceee50 3183 *oload_champ_bv = std::move (new_oload_champ_bv);
8d577d32
DC
3184 return 1;
3185 }
3186 else if (searched_deeper)
3187 {
8d577d32
DC
3188 return 0;
3189 }
3190 else
3191 {
0891c3cc 3192 *oload_syms = std::move (new_oload_syms);
8d577d32 3193 *oload_champ = new_oload_champ;
82ceee50 3194 *oload_champ_bv = std::move (new_oload_champ_bv);
8d577d32
DC
3195 return 0;
3196 }
3197}
3198
6b1747cd 3199/* Look for a function to take ARGS. Find the best match from among
38139a96
PA
3200 the overloaded methods or functions given by METHODS or FUNCTIONS
3201 or XMETHODS, respectively. One, and only one of METHODS, FUNCTIONS
3202 and XMETHODS can be non-NULL.
233e8b28 3203
38139a96
PA
3204 NUM_FNS is the length of the array pointed at by METHODS, FUNCTIONS
3205 or XMETHODS, whichever is non-NULL.
233e8b28 3206
8d577d32 3207 Return the index of the best match; store an indication of the
82ceee50 3208 quality of the match in OLOAD_CHAMP_BV. */
8d577d32
DC
3209
3210static int
6b1747cd 3211find_oload_champ (gdb::array_view<value *> args,
85cca2bc 3212 size_t num_fns,
38139a96
PA
3213 fn_field *methods,
3214 xmethod_worker_up *xmethods,
3215 symbol **functions,
82ceee50 3216 badness_vector *oload_champ_bv)
8d577d32 3217{
ac3eeb49 3218 /* A measure of how good an overloaded instance is. */
82ceee50 3219 badness_vector bv;
ac3eeb49
MS
3220 /* Index of best overloaded function. */
3221 int oload_champ = -1;
3222 /* Current ambiguity state for overload resolution. */
3223 int oload_ambiguous = 0;
3224 /* 0 => no ambiguity, 1 => two good funcs, 2 => incomparable funcs. */
8d577d32 3225
9cf95373 3226 /* A champion can be found among methods alone, or among functions
233e8b28
SC
3227 alone, or in xmethods alone, but not in more than one of these
3228 groups. */
38139a96 3229 gdb_assert ((methods != NULL) + (functions != NULL) + (xmethods != NULL)
233e8b28 3230 == 1);
9cf95373 3231
ac3eeb49 3232 /* Consider each candidate in turn. */
85cca2bc 3233 for (size_t ix = 0; ix < num_fns; ix++)
c906108c 3234 {
8d577d32 3235 int jj;
233e8b28 3236 int static_offset = 0;
6b1747cd 3237 std::vector<type *> parm_types;
8d577d32 3238
38139a96
PA
3239 if (xmethods != NULL)
3240 parm_types = xmethods[ix]->get_arg_types ();
db577aea
AC
3241 else
3242 {
6b1747cd
PA
3243 size_t nparms;
3244
38139a96 3245 if (methods != NULL)
233e8b28 3246 {
1f704f76 3247 nparms = TYPE_FN_FIELD_TYPE (methods, ix)->num_fields ();
38139a96 3248 static_offset = oload_method_static_p (methods, ix);
233e8b28
SC
3249 }
3250 else
5f9c5a63 3251 nparms = functions[ix]->type ()->num_fields ();
233e8b28 3252
6b1747cd 3253 parm_types.reserve (nparms);
233e8b28 3254 for (jj = 0; jj < nparms; jj++)
6b1747cd 3255 {
38139a96 3256 type *t = (methods != NULL
5d14b6e5 3257 ? (TYPE_FN_FIELD_ARGS (methods, ix)[jj].type ())
5f9c5a63 3258 : functions[ix]->type ()->field (jj).type ());
6b1747cd
PA
3259 parm_types.push_back (t);
3260 }
db577aea 3261 }
c906108c 3262
ac3eeb49 3263 /* Compare parameter types to supplied argument types. Skip
dda83cd7 3264 THIS for static methods. */
6b1747cd
PA
3265 bv = rank_function (parm_types,
3266 args.slice (static_offset));
c5aa993b 3267
e9194a1a
TBA
3268 if (overload_debug)
3269 {
3270 if (methods != NULL)
6cb06a8c
TT
3271 gdb_printf (gdb_stderr,
3272 "Overloaded method instance %s, # of parms %d\n",
3273 methods[ix].physname, (int) parm_types.size ());
e9194a1a 3274 else if (xmethods != NULL)
6cb06a8c
TT
3275 gdb_printf (gdb_stderr,
3276 "Xmethod worker, # of parms %d\n",
3277 (int) parm_types.size ());
e9194a1a 3278 else
6cb06a8c
TT
3279 gdb_printf (gdb_stderr,
3280 "Overloaded function instance "
3281 "%s # of parms %d\n",
3282 functions[ix]->demangled_name (),
3283 (int) parm_types.size ());
a992a3b0 3284
6cb06a8c
TT
3285 gdb_printf (gdb_stderr,
3286 "...Badness of length : {%d, %d}\n",
3287 bv[0].rank, bv[0].subrank);
a992a3b0
TBA
3288
3289 for (jj = 1; jj < bv.size (); jj++)
6cb06a8c
TT
3290 gdb_printf (gdb_stderr,
3291 "...Badness of arg %d : {%d, %d}\n",
3292 jj, bv[jj].rank, bv[jj].subrank);
e9194a1a
TBA
3293 }
3294
82ceee50 3295 if (oload_champ_bv->empty ())
c5aa993b 3296 {
82ceee50 3297 *oload_champ_bv = std::move (bv);
c5aa993b 3298 oload_champ = 0;
c5aa993b 3299 }
ac3eeb49
MS
3300 else /* See whether current candidate is better or worse than
3301 previous best. */
8d577d32 3302 switch (compare_badness (bv, *oload_champ_bv))
c5aa993b 3303 {
ac3eeb49
MS
3304 case 0: /* Top two contenders are equally good. */
3305 oload_ambiguous = 1;
c5aa993b 3306 break;
ac3eeb49
MS
3307 case 1: /* Incomparable top contenders. */
3308 oload_ambiguous = 2;
c5aa993b 3309 break;
ac3eeb49 3310 case 2: /* New champion, record details. */
82ceee50 3311 *oload_champ_bv = std::move (bv);
c5aa993b
JM
3312 oload_ambiguous = 0;
3313 oload_champ = ix;
c5aa993b
JM
3314 break;
3315 case 3:
3316 default:
3317 break;
3318 }
6b1ba9a0 3319 if (overload_debug)
6cb06a8c
TT
3320 gdb_printf (gdb_stderr, "Overload resolution "
3321 "champion is %d, ambiguous? %d\n",
3322 oload_champ, oload_ambiguous);
c906108c
SS
3323 }
3324
8d577d32
DC
3325 return oload_champ;
3326}
6b1ba9a0 3327
8d577d32
DC
3328/* Return 1 if we're looking at a static method, 0 if we're looking at
3329 a non-static method or a function that isn't a method. */
c906108c 3330
8d577d32 3331static int
2bca57ba 3332oload_method_static_p (struct fn_field *fns_ptr, int index)
8d577d32 3333{
2bca57ba 3334 if (fns_ptr && index >= 0 && TYPE_FN_FIELD_STATIC_P (fns_ptr, index))
8d577d32 3335 return 1;
c906108c 3336 else
8d577d32
DC
3337 return 0;
3338}
c906108c 3339
8d577d32
DC
3340/* Check how good an overload match OLOAD_CHAMP_BV represents. */
3341
3342static enum oload_classification
82ceee50 3343classify_oload_match (const badness_vector &oload_champ_bv,
8d577d32
DC
3344 int nargs,
3345 int static_offset)
3346{
3347 int ix;
da096638 3348 enum oload_classification worst = STANDARD;
8d577d32
DC
3349
3350 for (ix = 1; ix <= nargs - static_offset; ix++)
7f8c9282 3351 {
6403aeea 3352 /* If this conversion is as bad as INCOMPATIBLE_TYPE_BADNESS
dda83cd7 3353 or worse return INCOMPATIBLE. */
82ceee50 3354 if (compare_ranks (oload_champ_bv[ix],
dda83cd7 3355 INCOMPATIBLE_TYPE_BADNESS) <= 0)
ac3eeb49 3356 return INCOMPATIBLE; /* Truly mismatched types. */
6403aeea 3357 /* Otherwise If this conversion is as bad as
dda83cd7 3358 NS_POINTER_CONVERSION_BADNESS or worse return NON_STANDARD. */
82ceee50 3359 else if (compare_ranks (oload_champ_bv[ix],
dda83cd7 3360 NS_POINTER_CONVERSION_BADNESS) <= 0)
da096638 3361 worst = NON_STANDARD; /* Non-standard type conversions
ac3eeb49 3362 needed. */
7f8c9282 3363 }
02f0d45d 3364
da096638
KS
3365 /* If no INCOMPATIBLE classification was found, return the worst one
3366 that was found (if any). */
3367 return worst;
c906108c
SS
3368}
3369
ac3eeb49
MS
3370/* C++: return 1 is NAME is a legitimate name for the destructor of
3371 type TYPE. If TYPE does not have a destructor, or if NAME is
d8228535
JK
3372 inappropriate for TYPE, an error is signaled. Parameter TYPE should not yet
3373 have CHECK_TYPEDEF applied, this function will apply it itself. */
3374
c906108c 3375int
d8228535 3376destructor_name_p (const char *name, struct type *type)
c906108c 3377{
c906108c
SS
3378 if (name[0] == '~')
3379 {
a737d952 3380 const char *dname = type_name_or_error (type);
d8228535 3381 const char *cp = strchr (dname, '<');
c906108c
SS
3382 unsigned int len;
3383
3384 /* Do not compare the template part for template classes. */
3385 if (cp == NULL)
3386 len = strlen (dname);
3387 else
3388 len = cp - dname;
bf896cb0 3389 if (strlen (name + 1) != len || strncmp (dname, name + 1, len) != 0)
8a3fe4f8 3390 error (_("name of destructor must equal name of class"));
c906108c
SS
3391 else
3392 return 1;
3393 }
3394 return 0;
3395}
3396
3d567982
TT
3397/* Find an enum constant named NAME in TYPE. TYPE must be an "enum
3398 class". If the name is found, return a value representing it;
3399 otherwise throw an exception. */
3400
3401static struct value *
3402enum_constant_from_type (struct type *type, const char *name)
3403{
3404 int i;
3405 int name_len = strlen (name);
3406
78134374 3407 gdb_assert (type->code () == TYPE_CODE_ENUM
3bc440a2 3408 && type->is_declared_class ());
3d567982 3409
1f704f76 3410 for (i = TYPE_N_BASECLASSES (type); i < type->num_fields (); ++i)
3d567982 3411 {
33d16dd9 3412 const char *fname = type->field (i).name ();
3d567982
TT
3413 int len;
3414
2ad53ea1 3415 if (type->field (i).loc_kind () != FIELD_LOC_KIND_ENUMVAL
3d567982
TT
3416 || fname == NULL)
3417 continue;
3418
3419 /* Look for the trailing "::NAME", since enum class constant
3420 names are qualified here. */
3421 len = strlen (fname);
3422 if (len + 2 >= name_len
3423 && fname[len - name_len - 2] == ':'
3424 && fname[len - name_len - 1] == ':'
3425 && strcmp (&fname[len - name_len], name) == 0)
970db518 3426 return value_from_longest (type, type->field (i).loc_enumval ());
3d567982
TT
3427 }
3428
3429 error (_("no constant named \"%s\" in enum \"%s\""),
7d93a1e0 3430 name, type->name ());
3d567982
TT
3431}
3432
79c2c32d 3433/* C++: Given an aggregate type CURTYPE, and a member name NAME,
0d5de010
DJ
3434 return the appropriate member (or the address of the member, if
3435 WANT_ADDRESS). This function is used to resolve user expressions
3436 of the form "DOMAIN::NAME". For more details on what happens, see
3437 the comment before value_struct_elt_for_reference. */
79c2c32d
DC
3438
3439struct value *
c848d642 3440value_aggregate_elt (struct type *curtype, const char *name,
072bba3b 3441 struct type *expect_type, int want_address,
79c2c32d
DC
3442 enum noside noside)
3443{
78134374 3444 switch (curtype->code ())
79c2c32d
DC
3445 {
3446 case TYPE_CODE_STRUCT:
3447 case TYPE_CODE_UNION:
ac3eeb49 3448 return value_struct_elt_for_reference (curtype, 0, curtype,
072bba3b 3449 name, expect_type,
0d5de010 3450 want_address, noside);
79c2c32d 3451 case TYPE_CODE_NAMESPACE:
ac3eeb49
MS
3452 return value_namespace_elt (curtype, name,
3453 want_address, noside);
3d567982
TT
3454
3455 case TYPE_CODE_ENUM:
3456 return enum_constant_from_type (curtype, name);
3457
79c2c32d 3458 default:
f34652de 3459 internal_error (_("non-aggregate type in value_aggregate_elt"));
79c2c32d
DC
3460 }
3461}
3462
072bba3b 3463/* Compares the two method/function types T1 and T2 for "equality"
b021a221 3464 with respect to the methods' parameters. If the types of the
072bba3b
KS
3465 two parameter lists are the same, returns 1; 0 otherwise. This
3466 comparison may ignore any artificial parameters in T1 if
3467 SKIP_ARTIFICIAL is non-zero. This function will ALWAYS skip
3468 the first artificial parameter in T1, assumed to be a 'this' pointer.
3469
3470 The type T2 is expected to have come from make_params (in eval.c). */
3471
3472static int
3473compare_parameters (struct type *t1, struct type *t2, int skip_artificial)
3474{
3475 int start = 0;
3476
1f704f76 3477 if (t1->num_fields () > 0 && TYPE_FIELD_ARTIFICIAL (t1, 0))
072bba3b
KS
3478 ++start;
3479
3480 /* If skipping artificial fields, find the first real field
581e13c1 3481 in T1. */
072bba3b
KS
3482 if (skip_artificial)
3483 {
1f704f76 3484 while (start < t1->num_fields ()
072bba3b
KS
3485 && TYPE_FIELD_ARTIFICIAL (t1, start))
3486 ++start;
3487 }
3488
581e13c1 3489 /* Now compare parameters. */
072bba3b
KS
3490
3491 /* Special case: a method taking void. T1 will contain no
3492 non-artificial fields, and T2 will contain TYPE_CODE_VOID. */
1f704f76 3493 if ((t1->num_fields () - start) == 0 && t2->num_fields () == 1
940da03e 3494 && t2->field (0).type ()->code () == TYPE_CODE_VOID)
072bba3b
KS
3495 return 1;
3496
1f704f76 3497 if ((t1->num_fields () - start) == t2->num_fields ())
072bba3b
KS
3498 {
3499 int i;
a109c7c1 3500
1f704f76 3501 for (i = 0; i < t2->num_fields (); ++i)
072bba3b 3502 {
940da03e
SM
3503 if (compare_ranks (rank_one_type (t1->field (start + i).type (),
3504 t2->field (i).type (), NULL),
dda83cd7 3505 EXACT_MATCH_BADNESS) != 0)
072bba3b
KS
3506 return 0;
3507 }
3508
3509 return 1;
3510 }
3511
3512 return 0;
3513}
3514
9f6b697b
WP
3515/* C++: Given an aggregate type VT, and a class type CLS, search
3516 recursively for CLS using value V; If found, store the offset
3517 which is either fetched from the virtual base pointer if CLS
3518 is virtual or accumulated offset of its parent classes if
3519 CLS is non-virtual in *BOFFS, set ISVIRT to indicate if CLS
3520 is virtual, and return true. If not found, return false. */
3521
3522static bool
3523get_baseclass_offset (struct type *vt, struct type *cls,
3524 struct value *v, int *boffs, bool *isvirt)
3525{
3526 for (int i = 0; i < TYPE_N_BASECLASSES (vt); i++)
3527 {
940da03e 3528 struct type *t = vt->field (i).type ();
9f6b697b 3529 if (types_equal (t, cls))
dda83cd7
SM
3530 {
3531 if (BASETYPE_VIA_VIRTUAL (vt, i))
3532 {
50888e42 3533 const gdb_byte *adr = value_contents_for_printing (v).data ();
76675c4d 3534 *boffs = baseclass_offset (vt, i, adr, v->offset (),
9f6b697b
WP
3535 value_as_long (v), v);
3536 *isvirt = true;
dda83cd7
SM
3537 }
3538 else
9f6b697b 3539 *isvirt = false;
dda83cd7
SM
3540 return true;
3541 }
9f6b697b
WP
3542
3543 if (get_baseclass_offset (check_typedef (t), cls, v, boffs, isvirt))
dda83cd7 3544 {
9f6b697b
WP
3545 if (*isvirt == false) /* Add non-virtual base offset. */
3546 {
50888e42 3547 const gdb_byte *adr = value_contents_for_printing (v).data ();
76675c4d 3548 *boffs += baseclass_offset (vt, i, adr, v->offset (),
9f6b697b
WP
3549 value_as_long (v), v);
3550 }
3551 return true;
3552 }
3553 }
3554
3555 return false;
3556}
3557
c906108c 3558/* C++: Given an aggregate type CURTYPE, and a member name NAME,
ac3eeb49
MS
3559 return the address of this member as a "pointer to member" type.
3560 If INTYPE is non-null, then it will be the type of the member we
3561 are looking for. This will help us resolve "pointers to member
3562 functions". This function is used to resolve user expressions of
3563 the form "DOMAIN::NAME". */
c906108c 3564
63d06c5c 3565static struct value *
fba45db2 3566value_struct_elt_for_reference (struct type *domain, int offset,
c848d642 3567 struct type *curtype, const char *name,
ac3eeb49
MS
3568 struct type *intype,
3569 int want_address,
63d06c5c 3570 enum noside noside)
c906108c 3571{
bf2977b5 3572 struct type *t = check_typedef (curtype);
52f0bd74 3573 int i;
b926417a 3574 struct value *result;
c906108c 3575
78134374
SM
3576 if (t->code () != TYPE_CODE_STRUCT
3577 && t->code () != TYPE_CODE_UNION)
3e43a32a
MS
3578 error (_("Internal error: non-aggregate type "
3579 "to value_struct_elt_for_reference"));
c906108c 3580
1f704f76 3581 for (i = t->num_fields () - 1; i >= TYPE_N_BASECLASSES (t); i--)
c906108c 3582 {
33d16dd9 3583 const char *t_field_name = t->field (i).name ();
c5aa993b 3584
6314a349 3585 if (t_field_name && strcmp (t_field_name, name) == 0)
c906108c 3586 {
ceacbf6e 3587 if (field_is_static (&t->field (i)))
c906108c 3588 {
b926417a 3589 struct value *v = value_static_field (t, i);
0d5de010
DJ
3590 if (want_address)
3591 v = value_addr (v);
c906108c
SS
3592 return v;
3593 }
3594 if (TYPE_FIELD_PACKED (t, i))
8a3fe4f8 3595 error (_("pointers to bitfield members not allowed"));
c5aa993b 3596
0d5de010
DJ
3597 if (want_address)
3598 return value_from_longest
940da03e 3599 (lookup_memberptr_type (t->field (i).type (), domain),
b610c045 3600 offset + (LONGEST) (t->field (i).loc_bitpos () >> 3));
f7e3ecae 3601 else if (noside != EVAL_NORMAL)
940da03e 3602 return allocate_value (t->field (i).type ());
0d5de010 3603 else
f7e3ecae
KS
3604 {
3605 /* Try to evaluate NAME as a qualified name with implicit
3606 this pointer. In this case, attempt to return the
3607 equivalent to `this->*(&TYPE::NAME)'. */
b926417a 3608 struct value *v = value_of_this_silent (current_language);
f7e3ecae
KS
3609 if (v != NULL)
3610 {
9f6b697b 3611 struct value *ptr, *this_v = v;
f7e3ecae
KS
3612 long mem_offset;
3613 struct type *type, *tmp;
3614
3615 ptr = value_aggregate_elt (domain, name, NULL, 1, noside);
d0c97917 3616 type = check_typedef (ptr->type ());
f7e3ecae 3617 gdb_assert (type != NULL
78134374 3618 && type->code () == TYPE_CODE_MEMBERPTR);
4bfb94b8 3619 tmp = lookup_pointer_type (TYPE_SELF_TYPE (type));
f7e3ecae
KS
3620 v = value_cast_pointers (tmp, v, 1);
3621 mem_offset = value_as_long (ptr);
9f6b697b
WP
3622 if (domain != curtype)
3623 {
3624 /* Find class offset of type CURTYPE from either its
3625 parent type DOMAIN or the type of implied this. */
3626 int boff = 0;
3627 bool isvirt = false;
3628 if (get_baseclass_offset (domain, curtype, v, &boff,
3629 &isvirt))
dda83cd7 3630 mem_offset += boff;
9f6b697b 3631 else
dda83cd7 3632 {
d0c97917 3633 struct type *p = check_typedef (this_v->type ());
27710edb 3634 p = check_typedef (p->target_type ());
dda83cd7 3635 if (get_baseclass_offset (p, curtype, this_v,
9f6b697b 3636 &boff, &isvirt))
dda83cd7
SM
3637 mem_offset += boff;
3638 }
9f6b697b 3639 }
27710edb 3640 tmp = lookup_pointer_type (type->target_type ());
f7e3ecae
KS
3641 result = value_from_pointer (tmp,
3642 value_as_long (v) + mem_offset);
3643 return value_ind (result);
3644 }
3645
3646 error (_("Cannot reference non-static field \"%s\""), name);
3647 }
c906108c
SS
3648 }
3649 }
3650
ac3eeb49
MS
3651 /* C++: If it was not found as a data field, then try to return it
3652 as a pointer to a method. */
c906108c 3653
c906108c 3654 /* Perform all necessary dereferencing. */
78134374 3655 while (intype && intype->code () == TYPE_CODE_PTR)
27710edb 3656 intype = intype->target_type ();
c906108c
SS
3657
3658 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
3659 {
0d5cff50 3660 const char *t_field_name = TYPE_FN_FIELDLIST_NAME (t, i);
c906108c 3661
6314a349 3662 if (t_field_name && strcmp (t_field_name, name) == 0)
c906108c 3663 {
072bba3b
KS
3664 int j;
3665 int len = TYPE_FN_FIELDLIST_LENGTH (t, i);
c906108c 3666 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
c5aa993b 3667
de17c821
DJ
3668 check_stub_method_group (t, i);
3669
c906108c
SS
3670 if (intype)
3671 {
072bba3b
KS
3672 for (j = 0; j < len; ++j)
3673 {
3693fdb3
PA
3674 if (TYPE_CONST (intype) != TYPE_FN_FIELD_CONST (f, j))
3675 continue;
3676 if (TYPE_VOLATILE (intype) != TYPE_FN_FIELD_VOLATILE (f, j))
3677 continue;
3678
072bba3b 3679 if (compare_parameters (TYPE_FN_FIELD_TYPE (f, j), intype, 0)
3e43a32a
MS
3680 || compare_parameters (TYPE_FN_FIELD_TYPE (f, j),
3681 intype, 1))
072bba3b
KS
3682 break;
3683 }
3684
3685 if (j == len)
3e43a32a
MS
3686 error (_("no member function matches "
3687 "that type instantiation"));
7f79b1c5 3688 }
c906108c 3689 else
072bba3b
KS
3690 {
3691 int ii;
7f79b1c5
DJ
3692
3693 j = -1;
53832f31 3694 for (ii = 0; ii < len; ++ii)
072bba3b 3695 {
7f79b1c5
DJ
3696 /* Skip artificial methods. This is necessary if,
3697 for example, the user wants to "print
3698 subclass::subclass" with only one user-defined
53832f31
TT
3699 constructor. There is no ambiguity in this case.
3700 We are careful here to allow artificial methods
3701 if they are the unique result. */
072bba3b 3702 if (TYPE_FN_FIELD_ARTIFICIAL (f, ii))
53832f31
TT
3703 {
3704 if (j == -1)
3705 j = ii;
3706 continue;
3707 }
072bba3b 3708
7f79b1c5
DJ
3709 /* Desired method is ambiguous if more than one
3710 method is defined. */
53832f31 3711 if (j != -1 && !TYPE_FN_FIELD_ARTIFICIAL (f, j))
3e43a32a
MS
3712 error (_("non-unique member `%s' requires "
3713 "type instantiation"), name);
072bba3b 3714
7f79b1c5
DJ
3715 j = ii;
3716 }
53832f31
TT
3717
3718 if (j == -1)
3719 error (_("no matching member function"));
072bba3b 3720 }
c5aa993b 3721
0d5de010
DJ
3722 if (TYPE_FN_FIELD_STATIC_P (f, j))
3723 {
ac3eeb49
MS
3724 struct symbol *s =
3725 lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
d12307c1 3726 0, VAR_DOMAIN, 0).symbol;
a109c7c1 3727
0d5de010
DJ
3728 if (s == NULL)
3729 return NULL;
3730
3731 if (want_address)
63e43d3a 3732 return value_addr (read_var_value (s, 0, 0));
0d5de010 3733 else
63e43d3a 3734 return read_var_value (s, 0, 0);
0d5de010
DJ
3735 }
3736
c906108c
SS
3737 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
3738 {
0d5de010
DJ
3739 if (want_address)
3740 {
3741 result = allocate_value
3742 (lookup_methodptr_type (TYPE_FN_FIELD_TYPE (f, j)));
d0c97917 3743 cplus_make_method_ptr (result->type (),
50888e42 3744 value_contents_writeable (result).data (),
0d5de010
DJ
3745 TYPE_FN_FIELD_VOFFSET (f, j), 1);
3746 }
3747 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
3748 return allocate_value (TYPE_FN_FIELD_TYPE (f, j));
3749 else
3750 error (_("Cannot reference virtual member function \"%s\""),
3751 name);
c906108c
SS
3752 }
3753 else
3754 {
ac3eeb49
MS
3755 struct symbol *s =
3756 lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
d12307c1 3757 0, VAR_DOMAIN, 0).symbol;
a109c7c1 3758
c906108c 3759 if (s == NULL)
0d5de010
DJ
3760 return NULL;
3761
b926417a 3762 struct value *v = read_var_value (s, 0, 0);
0d5de010
DJ
3763 if (!want_address)
3764 result = v;
c906108c
SS
3765 else
3766 {
0d5de010 3767 result = allocate_value (lookup_methodptr_type (TYPE_FN_FIELD_TYPE (f, j)));
d0c97917 3768 cplus_make_method_ptr (result->type (),
50888e42 3769 value_contents_writeable (result).data (),
42ae5230 3770 value_address (v), 0);
c906108c 3771 }
c906108c 3772 }
0d5de010 3773 return result;
c906108c
SS
3774 }
3775 }
3776 for (i = TYPE_N_BASECLASSES (t) - 1; i >= 0; i--)
3777 {
f23631e4 3778 struct value *v;
c906108c
SS
3779 int base_offset;
3780
3781 if (BASETYPE_VIA_VIRTUAL (t, i))
3782 base_offset = 0;
3783 else
3784 base_offset = TYPE_BASECLASS_BITPOS (t, i) / 8;
3785 v = value_struct_elt_for_reference (domain,
3786 offset + base_offset,
3787 TYPE_BASECLASS (t, i),
ac3eeb49
MS
3788 name, intype,
3789 want_address, noside);
c906108c
SS
3790 if (v)
3791 return v;
3792 }
63d06c5c
DC
3793
3794 /* As a last chance, pretend that CURTYPE is a namespace, and look
3795 it up that way; this (frequently) works for types nested inside
3796 classes. */
3797
ac3eeb49
MS
3798 return value_maybe_namespace_elt (curtype, name,
3799 want_address, noside);
c906108c
SS
3800}
3801
79c2c32d
DC
3802/* C++: Return the member NAME of the namespace given by the type
3803 CURTYPE. */
3804
3805static struct value *
3806value_namespace_elt (const struct type *curtype,
c848d642 3807 const char *name, int want_address,
79c2c32d 3808 enum noside noside)
63d06c5c
DC
3809{
3810 struct value *retval = value_maybe_namespace_elt (curtype, name,
ac3eeb49
MS
3811 want_address,
3812 noside);
63d06c5c
DC
3813
3814 if (retval == NULL)
ac3eeb49 3815 error (_("No symbol \"%s\" in namespace \"%s\"."),
7d93a1e0 3816 name, curtype->name ());
63d06c5c
DC
3817
3818 return retval;
3819}
3820
3821/* A helper function used by value_namespace_elt and
3822 value_struct_elt_for_reference. It looks up NAME inside the
3823 context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
3824 is a class and NAME refers to a type in CURTYPE itself (as opposed
3825 to, say, some base class of CURTYPE). */
3826
3827static struct value *
3828value_maybe_namespace_elt (const struct type *curtype,
c848d642 3829 const char *name, int want_address,
63d06c5c 3830 enum noside noside)
79c2c32d 3831{
7d93a1e0 3832 const char *namespace_name = curtype->name ();
d12307c1 3833 struct block_symbol sym;
0d5de010 3834 struct value *result;
79c2c32d 3835
13387711 3836 sym = cp_lookup_symbol_namespace (namespace_name, name,
41f62f39
JK
3837 get_selected_block (0), VAR_DOMAIN);
3838
d12307c1 3839 if (sym.symbol == NULL)
63d06c5c 3840 return NULL;
79c2c32d 3841 else if ((noside == EVAL_AVOID_SIDE_EFFECTS)
66d7f48f 3842 && (sym.symbol->aclass () == LOC_TYPEDEF))
5f9c5a63 3843 result = allocate_value (sym.symbol->type ());
79c2c32d 3844 else
d12307c1 3845 result = value_of_variable (sym.symbol, sym.block);
0d5de010 3846
ae6a105d 3847 if (want_address)
0d5de010
DJ
3848 result = value_addr (result);
3849
3850 return result;
79c2c32d
DC
3851}
3852
dfcee124 3853/* Given a pointer or a reference value V, find its real (RTTI) type.
ac3eeb49 3854
c906108c 3855 Other parameters FULL, TOP, USING_ENC as with value_rtti_type()
ac3eeb49 3856 and refer to the values computed for the object pointed to. */
c906108c
SS
3857
3858struct type *
dfcee124 3859value_rtti_indirect_type (struct value *v, int *full,
6b850546 3860 LONGEST *top, int *using_enc)
c906108c 3861{
f7e5394d 3862 struct value *target = NULL;
dfcee124
AG
3863 struct type *type, *real_type, *target_type;
3864
d0c97917 3865 type = v->type ();
dfcee124 3866 type = check_typedef (type);
aa006118 3867 if (TYPE_IS_REFERENCE (type))
dfcee124 3868 target = coerce_ref (v);
78134374 3869 else if (type->code () == TYPE_CODE_PTR)
f7e5394d 3870 {
f7e5394d 3871
a70b8144 3872 try
dda83cd7 3873 {
f7e5394d 3874 target = value_ind (v);
dda83cd7 3875 }
230d2906 3876 catch (const gdb_exception_error &except)
f7e5394d
SM
3877 {
3878 if (except.error == MEMORY_ERROR)
3879 {
3880 /* value_ind threw a memory error. The pointer is NULL or
dda83cd7
SM
3881 contains an uninitialized value: we can't determine any
3882 type. */
f7e5394d
SM
3883 return NULL;
3884 }
eedc3f4f 3885 throw;
f7e5394d
SM
3886 }
3887 }
dfcee124
AG
3888 else
3889 return NULL;
c906108c 3890
dfcee124
AG
3891 real_type = value_rtti_type (target, full, top, using_enc);
3892
3893 if (real_type)
3894 {
3895 /* Copy qualifiers to the referenced object. */
d0c97917 3896 target_type = target->type ();
dfcee124
AG
3897 real_type = make_cv_type (TYPE_CONST (target_type),
3898 TYPE_VOLATILE (target_type), real_type, NULL);
aa006118 3899 if (TYPE_IS_REFERENCE (type))
dda83cd7 3900 real_type = lookup_reference_type (real_type, type->code ());
78134374 3901 else if (type->code () == TYPE_CODE_PTR)
dda83cd7 3902 real_type = lookup_pointer_type (real_type);
dfcee124 3903 else
f34652de 3904 internal_error (_("Unexpected value type."));
dfcee124
AG
3905
3906 /* Copy qualifiers to the pointer/reference. */
3907 real_type = make_cv_type (TYPE_CONST (type), TYPE_VOLATILE (type),
3908 real_type, NULL);
3909 }
c906108c 3910
dfcee124 3911 return real_type;
c906108c
SS
3912}
3913
3914/* Given a value pointed to by ARGP, check its real run-time type, and
3915 if that is different from the enclosing type, create a new value
3916 using the real run-time type as the enclosing type (and of the same
3917 type as ARGP) and return it, with the embedded offset adjusted to
ac3eeb49
MS
3918 be the correct offset to the enclosed object. RTYPE is the type,
3919 and XFULL, XTOP, and XUSING_ENC are the other parameters, computed
3920 by value_rtti_type(). If these are available, they can be supplied
3921 and a second call to value_rtti_type() is avoided. (Pass RTYPE ==
3922 NULL if they're not available. */
c906108c 3923
f23631e4 3924struct value *
ac3eeb49
MS
3925value_full_object (struct value *argp,
3926 struct type *rtype,
3927 int xfull, int xtop,
fba45db2 3928 int xusing_enc)
c906108c 3929{
c5aa993b 3930 struct type *real_type;
c906108c 3931 int full = 0;
6b850546 3932 LONGEST top = -1;
c906108c 3933 int using_enc = 0;
f23631e4 3934 struct value *new_val;
c906108c
SS
3935
3936 if (rtype)
3937 {
3938 real_type = rtype;
3939 full = xfull;
3940 top = xtop;
3941 using_enc = xusing_enc;
3942 }
3943 else
3944 real_type = value_rtti_type (argp, &full, &top, &using_enc);
3945
ac3eeb49 3946 /* If no RTTI data, or if object is already complete, do nothing. */
4754a64e 3947 if (!real_type || real_type == value_enclosing_type (argp))
c906108c
SS
3948 return argp;
3949
a7860e76
TT
3950 /* In a destructor we might see a real type that is a superclass of
3951 the object's type. In this case it is better to leave the object
3952 as-is. */
3953 if (full
df86565b 3954 && real_type->length () < value_enclosing_type (argp)->length ())
a7860e76
TT
3955 return argp;
3956
c906108c 3957 /* If we have the full object, but for some reason the enclosing
ac3eeb49
MS
3958 type is wrong, set it. */
3959 /* pai: FIXME -- sounds iffy */
c906108c
SS
3960 if (full)
3961 {
4dfea560
DE
3962 argp = value_copy (argp);
3963 set_value_enclosing_type (argp, real_type);
c906108c
SS
3964 return argp;
3965 }
3966
581e13c1 3967 /* Check if object is in memory. */
c906108c
SS
3968 if (VALUE_LVAL (argp) != lval_memory)
3969 {
3e43a32a
MS
3970 warning (_("Couldn't retrieve complete object of RTTI "
3971 "type %s; object may be in register(s)."),
7d93a1e0 3972 real_type->name ());
c5aa993b 3973
c906108c
SS
3974 return argp;
3975 }
c5aa993b 3976
ac3eeb49
MS
3977 /* All other cases -- retrieve the complete object. */
3978 /* Go back by the computed top_offset from the beginning of the
3979 object, adjusting for the embedded offset of argp if that's what
3980 value_rtti_type used for its computation. */
42ae5230 3981 new_val = value_at_lazy (real_type, value_address (argp) - top +
13c3b5f5 3982 (using_enc ? 0 : value_embedded_offset (argp)));
81ae560c 3983 new_val->deprecated_set_type (argp->type ());
13c3b5f5
AC
3984 set_value_embedded_offset (new_val, (using_enc
3985 ? top + value_embedded_offset (argp)
3986 : top));
c906108c
SS
3987 return new_val;
3988}
3989
389e51db 3990
85bc8cb7
JK
3991/* Return the value of the local variable, if one exists. Throw error
3992 otherwise, such as if the request is made in an inappropriate context. */
c906108c 3993
f23631e4 3994struct value *
85bc8cb7 3995value_of_this (const struct language_defn *lang)
c906108c 3996{
63e43d3a 3997 struct block_symbol sym;
3977b71f 3998 const struct block *b;
bd2b40ac 3999 frame_info_ptr frame;
c906108c 4000
5bae7c4e 4001 if (lang->name_of_this () == NULL)
85bc8cb7 4002 error (_("no `this' in current language"));
aee28ec6 4003
85bc8cb7 4004 frame = get_selected_frame (_("no frame selected"));
c906108c 4005
66a17cb6 4006 b = get_frame_block (frame, NULL);
c906108c 4007
63e43d3a
PMR
4008 sym = lookup_language_this (lang, b);
4009 if (sym.symbol == NULL)
85bc8cb7 4010 error (_("current stack frame does not contain a variable named `%s'"),
5bae7c4e 4011 lang->name_of_this ());
85bc8cb7 4012
63e43d3a 4013 return read_var_value (sym.symbol, sym.block, frame);
85bc8cb7
JK
4014}
4015
4016/* Return the value of the local variable, if one exists. Return NULL
4017 otherwise. Never throw error. */
4018
4019struct value *
4020value_of_this_silent (const struct language_defn *lang)
4021{
4022 struct value *ret = NULL;
85bc8cb7 4023
a70b8144 4024 try
c906108c 4025 {
85bc8cb7 4026 ret = value_of_this (lang);
c906108c 4027 }
230d2906 4028 catch (const gdb_exception_error &except)
492d29ea
PA
4029 {
4030 }
c906108c 4031
d069f99d
AF
4032 return ret;
4033}
4034
ac3eeb49
MS
4035/* Create a slice (sub-string, sub-array) of ARRAY, that is LENGTH
4036 elements long, starting at LOWBOUND. The result has the same lower
4037 bound as the original ARRAY. */
c906108c 4038
f23631e4
AC
4039struct value *
4040value_slice (struct value *array, int lowbound, int length)
c906108c
SS
4041{
4042 struct type *slice_range_type, *slice_type, *range_type;
7a67d0fe 4043 LONGEST lowerbound, upperbound;
f23631e4 4044 struct value *slice;
c906108c 4045 struct type *array_type;
ac3eeb49 4046
d0c97917 4047 array_type = check_typedef (array->type ());
78134374
SM
4048 if (array_type->code () != TYPE_CODE_ARRAY
4049 && array_type->code () != TYPE_CODE_STRING)
8a3fe4f8 4050 error (_("cannot take slice of non-array"));
ac3eeb49 4051
a7067863
AB
4052 if (type_not_allocated (array_type))
4053 error (_("array not allocated"));
4054 if (type_not_associated (array_type))
4055 error (_("array not associated"));
4056
3d967001 4057 range_type = array_type->index_type ();
1f8d2881 4058 if (!get_discrete_bounds (range_type, &lowerbound, &upperbound))
8a3fe4f8 4059 error (_("slice from bad array or bitstring"));
ac3eeb49 4060
c906108c 4061 if (lowbound < lowerbound || length < 0
db034ac5 4062 || lowbound + length - 1 > upperbound)
8a3fe4f8 4063 error (_("slice out of range"));
ac3eeb49 4064
c906108c
SS
4065 /* FIXME-type-allocation: need a way to free this type when we are
4066 done with it. */
cafb3438 4067 slice_range_type = create_static_range_type (NULL,
27710edb 4068 range_type->target_type (),
0c9c3474
SA
4069 lowbound,
4070 lowbound + length - 1);
ac3eeb49 4071
a7c88acd 4072 {
27710edb 4073 struct type *element_type = array_type->target_type ();
a7c88acd 4074 LONGEST offset
df86565b 4075 = (lowbound - lowerbound) * check_typedef (element_type)->length ();
ac3eeb49 4076
cafb3438 4077 slice_type = create_array_type (NULL,
a7c88acd
JB
4078 element_type,
4079 slice_range_type);
78134374 4080 slice_type->set_code (array_type->code ());
ac3eeb49 4081
a7c88acd
JB
4082 if (VALUE_LVAL (array) == lval_memory && value_lazy (array))
4083 slice = allocate_value_lazy (slice_type);
4084 else
4085 {
4086 slice = allocate_value (slice_type);
4087 value_contents_copy (slice, 0, array, offset,
3ae385af 4088 type_length_units (slice_type));
a7c88acd
JB
4089 }
4090
4091 set_value_component_location (slice, array);
76675c4d 4092 slice->set_offset (array->offset () + offset);
a7c88acd 4093 }
ac3eeb49 4094
c906108c
SS
4095 return slice;
4096}
4097
6b4a335b 4098/* See value.h. */
c906108c 4099
f23631e4 4100struct value *
6b4a335b 4101value_literal_complex (struct value *arg1,
ac3eeb49
MS
4102 struct value *arg2,
4103 struct type *type)
c906108c 4104{
f23631e4 4105 struct value *val;
27710edb 4106 struct type *real_type = type->target_type ();
c906108c
SS
4107
4108 val = allocate_value (type);
4109 arg1 = value_cast (real_type, arg1);
4110 arg2 = value_cast (real_type, arg2);
4111
df86565b 4112 int len = real_type->length ();
4bce7cda
SM
4113
4114 copy (value_contents (arg1),
4115 value_contents_raw (val).slice (0, len));
4116 copy (value_contents (arg2),
4117 value_contents_raw (val).slice (len, len));
4118
c906108c
SS
4119 return val;
4120}
4121
4c99290d
TT
4122/* See value.h. */
4123
4124struct value *
4125value_real_part (struct value *value)
4126{
d0c97917 4127 struct type *type = check_typedef (value->type ());
27710edb 4128 struct type *ttype = type->target_type ();
4c99290d 4129
78134374 4130 gdb_assert (type->code () == TYPE_CODE_COMPLEX);
4c99290d
TT
4131 return value_from_component (value, ttype, 0);
4132}
4133
4134/* See value.h. */
4135
4136struct value *
4137value_imaginary_part (struct value *value)
4138{
d0c97917 4139 struct type *type = check_typedef (value->type ());
27710edb 4140 struct type *ttype = type->target_type ();
4c99290d 4141
78134374 4142 gdb_assert (type->code () == TYPE_CODE_COMPLEX);
4c99290d 4143 return value_from_component (value, ttype,
df86565b 4144 check_typedef (ttype)->length ());
4c99290d
TT
4145}
4146
ac3eeb49 4147/* Cast a value into the appropriate complex data type. */
c906108c 4148
f23631e4
AC
4149static struct value *
4150cast_into_complex (struct type *type, struct value *val)
c906108c 4151{
27710edb 4152 struct type *real_type = type->target_type ();
ac3eeb49 4153
d0c97917 4154 if (val->type ()->code () == TYPE_CODE_COMPLEX)
c906108c 4155 {
d0c97917 4156 struct type *val_real_type = val->type ()->target_type ();
f23631e4
AC
4157 struct value *re_val = allocate_value (val_real_type);
4158 struct value *im_val = allocate_value (val_real_type);
df86565b 4159 int len = val_real_type->length ();
c906108c 4160
4bce7cda
SM
4161 copy (value_contents (val).slice (0, len),
4162 value_contents_raw (re_val));
4163 copy (value_contents (val).slice (len, len),
4164 value_contents_raw (im_val));
c906108c
SS
4165
4166 return value_literal_complex (re_val, im_val, type);
4167 }
d0c97917
TT
4168 else if (val->type ()->code () == TYPE_CODE_FLT
4169 || val->type ()->code () == TYPE_CODE_INT)
ac3eeb49
MS
4170 return value_literal_complex (val,
4171 value_zero (real_type, not_lval),
4172 type);
c906108c 4173 else
8a3fe4f8 4174 error (_("cannot cast non-number to complex"));
c906108c
SS
4175}
4176
6c265988 4177void _initialize_valops ();
c906108c 4178void
6c265988 4179_initialize_valops ()
c906108c 4180{
5bf193a2
AC
4181 add_setshow_boolean_cmd ("overload-resolution", class_support,
4182 &overload_resolution, _("\
4183Set overload resolution in evaluating C++ functions."), _("\
ac3eeb49
MS
4184Show overload resolution in evaluating C++ functions."),
4185 NULL, NULL,
920d2a44 4186 show_overload_resolution,
5bf193a2 4187 &setlist, &showlist);
c906108c 4188 overload_resolution = 1;
c906108c 4189}