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