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