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