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