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