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