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