]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Perform non-arithmetic operations on values, for GDB. |
990a07ab | 2 | |
f23631e4 | 3 | Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, |
990a07ab | 4 | 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 |
f23631e4 | 5 | Free Software Foundation, Inc. |
c906108c | 6 | |
c5aa993b | 7 | This file is part of GDB. |
c906108c | 8 | |
c5aa993b JM |
9 | This program is free software; you can redistribute it and/or modify |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 2 of the License, or | |
12 | (at your option) any later version. | |
c906108c | 13 | |
c5aa993b JM |
14 | This program is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
c906108c | 18 | |
c5aa993b JM |
19 | You should have received a copy of the GNU General Public License |
20 | along with this program; if not, write to the Free Software | |
21 | Foundation, Inc., 59 Temple Place - Suite 330, | |
22 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
23 | |
24 | #include "defs.h" | |
25 | #include "symtab.h" | |
26 | #include "gdbtypes.h" | |
27 | #include "value.h" | |
28 | #include "frame.h" | |
29 | #include "inferior.h" | |
30 | #include "gdbcore.h" | |
31 | #include "target.h" | |
32 | #include "demangle.h" | |
33 | #include "language.h" | |
34 | #include "gdbcmd.h" | |
4e052eda | 35 | #include "regcache.h" |
015a42b4 | 36 | #include "cp-abi.h" |
fe898f56 | 37 | #include "block.h" |
04714b91 | 38 | #include "infcall.h" |
de4f826b | 39 | #include "dictionary.h" |
b6429628 | 40 | #include "cp-support.h" |
c906108c SS |
41 | |
42 | #include <errno.h> | |
43 | #include "gdb_string.h" | |
4a1970e4 | 44 | #include "gdb_assert.h" |
79c2c32d | 45 | #include "cp-support.h" |
f4c5303c | 46 | #include "observer.h" |
c906108c | 47 | |
070ad9f0 | 48 | extern int overload_debug; |
c906108c SS |
49 | /* Local functions. */ |
50 | ||
ad2f7632 DJ |
51 | static int typecmp (int staticp, int varargs, int nargs, |
52 | struct field t1[], struct value *t2[]); | |
c906108c | 53 | |
f23631e4 | 54 | static struct value *search_struct_field (char *, struct value *, int, |
a14ed312 | 55 | struct type *, int); |
c906108c | 56 | |
f23631e4 AC |
57 | static struct value *search_struct_method (char *, struct value **, |
58 | struct value **, | |
a14ed312 | 59 | int, int *, struct type *); |
c906108c | 60 | |
8d577d32 DC |
61 | static int find_oload_champ_namespace (struct type **arg_types, int nargs, |
62 | const char *func_name, | |
63 | const char *qualified_name, | |
64 | struct symbol ***oload_syms, | |
65 | struct badness_vector **oload_champ_bv); | |
66 | ||
67 | static | |
68 | int find_oload_champ_namespace_loop (struct type **arg_types, int nargs, | |
69 | const char *func_name, | |
70 | const char *qualified_name, | |
71 | int namespace_len, | |
72 | struct symbol ***oload_syms, | |
73 | struct badness_vector **oload_champ_bv, | |
74 | int *oload_champ); | |
75 | ||
76 | static int find_oload_champ (struct type **arg_types, int nargs, int method, | |
77 | int num_fns, | |
78 | struct fn_field *fns_ptr, | |
79 | struct symbol **oload_syms, | |
80 | struct badness_vector **oload_champ_bv); | |
81 | ||
82 | static int oload_method_static (int method, struct fn_field *fns_ptr, | |
83 | int index); | |
84 | ||
85 | enum oload_classification { STANDARD, NON_STANDARD, INCOMPATIBLE }; | |
86 | ||
87 | static enum | |
88 | oload_classification classify_oload_match (struct badness_vector | |
89 | * oload_champ_bv, | |
90 | int nargs, | |
91 | int static_offset); | |
92 | ||
a14ed312 | 93 | static int check_field_in (struct type *, const char *); |
c906108c | 94 | |
79c2c32d DC |
95 | static struct value *value_struct_elt_for_reference (struct type *domain, |
96 | int offset, | |
97 | struct type *curtype, | |
98 | char *name, | |
63d06c5c DC |
99 | struct type *intype, |
100 | enum noside noside); | |
79c2c32d DC |
101 | |
102 | static struct value *value_namespace_elt (const struct type *curtype, | |
63d06c5c | 103 | char *name, |
79c2c32d DC |
104 | enum noside noside); |
105 | ||
63d06c5c DC |
106 | static struct value *value_maybe_namespace_elt (const struct type *curtype, |
107 | char *name, | |
108 | enum noside noside); | |
109 | ||
a14ed312 | 110 | static CORE_ADDR allocate_space_in_inferior (int); |
c906108c | 111 | |
f23631e4 | 112 | static struct value *cast_into_complex (struct type *, struct value *); |
c906108c | 113 | |
f23631e4 | 114 | static struct fn_field *find_method_list (struct value ** argp, char *method, |
4a1970e4 | 115 | int offset, |
a14ed312 KB |
116 | struct type *type, int *num_fns, |
117 | struct type **basetype, | |
118 | int *boffset); | |
7a292a7a | 119 | |
a14ed312 | 120 | void _initialize_valops (void); |
c906108c | 121 | |
c906108c SS |
122 | /* Flag for whether we want to abandon failed expression evals by default. */ |
123 | ||
124 | #if 0 | |
125 | static int auto_abandon = 0; | |
126 | #endif | |
127 | ||
128 | int overload_resolution = 0; | |
242bfc55 | 129 | |
c906108c SS |
130 | /* Find the address of function name NAME in the inferior. */ |
131 | ||
f23631e4 | 132 | struct value * |
3bada2a2 | 133 | find_function_in_inferior (const char *name) |
c906108c | 134 | { |
52f0bd74 | 135 | struct symbol *sym; |
176620f1 | 136 | sym = lookup_symbol (name, 0, VAR_DOMAIN, 0, NULL); |
c906108c SS |
137 | if (sym != NULL) |
138 | { | |
139 | if (SYMBOL_CLASS (sym) != LOC_BLOCK) | |
140 | { | |
141 | error ("\"%s\" exists in this program but is not a function.", | |
142 | name); | |
143 | } | |
144 | return value_of_variable (sym, NULL); | |
145 | } | |
146 | else | |
147 | { | |
c5aa993b | 148 | struct minimal_symbol *msymbol = lookup_minimal_symbol (name, NULL, NULL); |
c906108c SS |
149 | if (msymbol != NULL) |
150 | { | |
151 | struct type *type; | |
4478b372 | 152 | CORE_ADDR maddr; |
c906108c SS |
153 | type = lookup_pointer_type (builtin_type_char); |
154 | type = lookup_function_type (type); | |
155 | type = lookup_pointer_type (type); | |
4478b372 JB |
156 | maddr = SYMBOL_VALUE_ADDRESS (msymbol); |
157 | return value_from_pointer (type, maddr); | |
c906108c SS |
158 | } |
159 | else | |
160 | { | |
c5aa993b | 161 | if (!target_has_execution) |
c906108c | 162 | error ("evaluation of this expression requires the target program to be active"); |
c5aa993b | 163 | else |
c906108c SS |
164 | error ("evaluation of this expression requires the program to have a function \"%s\".", name); |
165 | } | |
166 | } | |
167 | } | |
168 | ||
169 | /* Allocate NBYTES of space in the inferior using the inferior's malloc | |
170 | and return a value that is a pointer to the allocated space. */ | |
171 | ||
f23631e4 | 172 | struct value * |
fba45db2 | 173 | value_allocate_space_in_inferior (int len) |
c906108c | 174 | { |
f23631e4 | 175 | struct value *blocklen; |
5720643c | 176 | struct value *val = find_function_in_inferior (NAME_OF_MALLOC); |
c906108c SS |
177 | |
178 | blocklen = value_from_longest (builtin_type_int, (LONGEST) len); | |
179 | val = call_function_by_hand (val, 1, &blocklen); | |
180 | if (value_logical_not (val)) | |
181 | { | |
182 | if (!target_has_execution) | |
c5aa993b JM |
183 | error ("No memory available to program now: you need to start the target first"); |
184 | else | |
185 | error ("No memory available to program: call to malloc failed"); | |
c906108c SS |
186 | } |
187 | return val; | |
188 | } | |
189 | ||
190 | static CORE_ADDR | |
fba45db2 | 191 | allocate_space_in_inferior (int len) |
c906108c SS |
192 | { |
193 | return value_as_long (value_allocate_space_in_inferior (len)); | |
194 | } | |
195 | ||
196 | /* Cast value ARG2 to type TYPE and return as a value. | |
197 | More general than a C cast: accepts any two types of the same length, | |
198 | and if ARG2 is an lvalue it can be cast into anything at all. */ | |
199 | /* In C++, casts may change pointer or object representations. */ | |
200 | ||
f23631e4 AC |
201 | struct value * |
202 | value_cast (struct type *type, struct value *arg2) | |
c906108c | 203 | { |
52f0bd74 AC |
204 | enum type_code code1; |
205 | enum type_code code2; | |
206 | int scalar; | |
c906108c SS |
207 | struct type *type2; |
208 | ||
209 | int convert_to_boolean = 0; | |
c5aa993b | 210 | |
df407dfe | 211 | if (value_type (arg2) == type) |
c906108c SS |
212 | return arg2; |
213 | ||
214 | CHECK_TYPEDEF (type); | |
215 | code1 = TYPE_CODE (type); | |
994b9211 | 216 | arg2 = coerce_ref (arg2); |
df407dfe | 217 | type2 = check_typedef (value_type (arg2)); |
c906108c SS |
218 | |
219 | /* A cast to an undetermined-length array_type, such as (TYPE [])OBJECT, | |
220 | is treated like a cast to (TYPE [N])OBJECT, | |
221 | where N is sizeof(OBJECT)/sizeof(TYPE). */ | |
222 | if (code1 == TYPE_CODE_ARRAY) | |
223 | { | |
224 | struct type *element_type = TYPE_TARGET_TYPE (type); | |
225 | unsigned element_length = TYPE_LENGTH (check_typedef (element_type)); | |
226 | if (element_length > 0 | |
c5aa993b | 227 | && TYPE_ARRAY_UPPER_BOUND_TYPE (type) == BOUND_CANNOT_BE_DETERMINED) |
c906108c SS |
228 | { |
229 | struct type *range_type = TYPE_INDEX_TYPE (type); | |
230 | int val_length = TYPE_LENGTH (type2); | |
231 | LONGEST low_bound, high_bound, new_length; | |
232 | if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0) | |
233 | low_bound = 0, high_bound = 0; | |
234 | new_length = val_length / element_length; | |
235 | if (val_length % element_length != 0) | |
c5aa993b | 236 | warning ("array element type size does not divide object size in cast"); |
c906108c SS |
237 | /* FIXME-type-allocation: need a way to free this type when we are |
238 | done with it. */ | |
239 | range_type = create_range_type ((struct type *) NULL, | |
240 | TYPE_TARGET_TYPE (range_type), | |
241 | low_bound, | |
242 | new_length + low_bound - 1); | |
df407dfe AC |
243 | arg2->type = create_array_type ((struct type *) NULL, |
244 | element_type, range_type); | |
c906108c SS |
245 | return arg2; |
246 | } | |
247 | } | |
248 | ||
249 | if (current_language->c_style_arrays | |
250 | && TYPE_CODE (type2) == TYPE_CODE_ARRAY) | |
251 | arg2 = value_coerce_array (arg2); | |
252 | ||
253 | if (TYPE_CODE (type2) == TYPE_CODE_FUNC) | |
254 | arg2 = value_coerce_function (arg2); | |
255 | ||
df407dfe | 256 | type2 = check_typedef (value_type (arg2)); |
c906108c SS |
257 | code2 = TYPE_CODE (type2); |
258 | ||
259 | if (code1 == TYPE_CODE_COMPLEX) | |
260 | return cast_into_complex (type, arg2); | |
261 | if (code1 == TYPE_CODE_BOOL) | |
262 | { | |
263 | code1 = TYPE_CODE_INT; | |
264 | convert_to_boolean = 1; | |
265 | } | |
266 | if (code1 == TYPE_CODE_CHAR) | |
267 | code1 = TYPE_CODE_INT; | |
268 | if (code2 == TYPE_CODE_BOOL || code2 == TYPE_CODE_CHAR) | |
269 | code2 = TYPE_CODE_INT; | |
270 | ||
271 | scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT | |
272 | || code2 == TYPE_CODE_ENUM || code2 == TYPE_CODE_RANGE); | |
273 | ||
c5aa993b | 274 | if (code1 == TYPE_CODE_STRUCT |
c906108c SS |
275 | && code2 == TYPE_CODE_STRUCT |
276 | && TYPE_NAME (type) != 0) | |
277 | { | |
278 | /* Look in the type of the source to see if it contains the | |
7b83ea04 AC |
279 | type of the target as a superclass. If so, we'll need to |
280 | offset the object in addition to changing its type. */ | |
f23631e4 | 281 | struct value *v = search_struct_field (type_name_no_tag (type), |
c906108c SS |
282 | arg2, 0, type2, 1); |
283 | if (v) | |
284 | { | |
df407dfe | 285 | v->type = type; |
c906108c SS |
286 | return v; |
287 | } | |
288 | } | |
289 | if (code1 == TYPE_CODE_FLT && scalar) | |
290 | return value_from_double (type, value_as_double (arg2)); | |
291 | else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM | |
292 | || code1 == TYPE_CODE_RANGE) | |
293 | && (scalar || code2 == TYPE_CODE_PTR)) | |
294 | { | |
295 | LONGEST longest; | |
c5aa993b | 296 | |
f83f82bc AC |
297 | if (deprecated_hp_som_som_object_present /* if target compiled by HP aCC */ |
298 | && (code2 == TYPE_CODE_PTR)) | |
c5aa993b JM |
299 | { |
300 | unsigned int *ptr; | |
f23631e4 | 301 | struct value *retvalp; |
c5aa993b JM |
302 | |
303 | switch (TYPE_CODE (TYPE_TARGET_TYPE (type2))) | |
304 | { | |
305 | /* With HP aCC, pointers to data members have a bias */ | |
306 | case TYPE_CODE_MEMBER: | |
307 | retvalp = value_from_longest (type, value_as_long (arg2)); | |
716c501e | 308 | /* force evaluation */ |
0fd88904 | 309 | ptr = (unsigned int *) value_contents (retvalp); |
c5aa993b JM |
310 | *ptr &= ~0x20000000; /* zap 29th bit to remove bias */ |
311 | return retvalp; | |
312 | ||
313 | /* While pointers to methods don't really point to a function */ | |
314 | case TYPE_CODE_METHOD: | |
315 | error ("Pointers to methods not supported with HP aCC"); | |
316 | ||
317 | default: | |
318 | break; /* fall out and go to normal handling */ | |
319 | } | |
320 | } | |
2bf1f4a1 JB |
321 | |
322 | /* When we cast pointers to integers, we mustn't use | |
323 | POINTER_TO_ADDRESS to find the address the pointer | |
324 | represents, as value_as_long would. GDB should evaluate | |
325 | expressions just as the compiler would --- and the compiler | |
326 | sees a cast as a simple reinterpretation of the pointer's | |
327 | bits. */ | |
328 | if (code2 == TYPE_CODE_PTR) | |
0fd88904 | 329 | longest = extract_unsigned_integer (value_contents (arg2), |
2bf1f4a1 JB |
330 | TYPE_LENGTH (type2)); |
331 | else | |
332 | longest = value_as_long (arg2); | |
802db21b | 333 | return value_from_longest (type, convert_to_boolean ? |
716c501e | 334 | (LONGEST) (longest ? 1 : 0) : longest); |
c906108c | 335 | } |
802db21b | 336 | else if (code1 == TYPE_CODE_PTR && (code2 == TYPE_CODE_INT || |
23e04971 MS |
337 | code2 == TYPE_CODE_ENUM || |
338 | code2 == TYPE_CODE_RANGE)) | |
634acd5f | 339 | { |
4603e466 DT |
340 | /* TYPE_LENGTH (type) is the length of a pointer, but we really |
341 | want the length of an address! -- we are really dealing with | |
342 | addresses (i.e., gdb representations) not pointers (i.e., | |
343 | target representations) here. | |
344 | ||
345 | This allows things like "print *(int *)0x01000234" to work | |
346 | without printing a misleading message -- which would | |
347 | otherwise occur when dealing with a target having two byte | |
348 | pointers and four byte addresses. */ | |
349 | ||
350 | int addr_bit = TARGET_ADDR_BIT; | |
351 | ||
634acd5f | 352 | LONGEST longest = value_as_long (arg2); |
4603e466 | 353 | if (addr_bit < sizeof (LONGEST) * HOST_CHAR_BIT) |
634acd5f | 354 | { |
4603e466 DT |
355 | if (longest >= ((LONGEST) 1 << addr_bit) |
356 | || longest <= -((LONGEST) 1 << addr_bit)) | |
634acd5f AC |
357 | warning ("value truncated"); |
358 | } | |
359 | return value_from_longest (type, longest); | |
360 | } | |
c906108c SS |
361 | else if (TYPE_LENGTH (type) == TYPE_LENGTH (type2)) |
362 | { | |
363 | if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR) | |
364 | { | |
365 | struct type *t1 = check_typedef (TYPE_TARGET_TYPE (type)); | |
366 | struct type *t2 = check_typedef (TYPE_TARGET_TYPE (type2)); | |
c5aa993b | 367 | if (TYPE_CODE (t1) == TYPE_CODE_STRUCT |
c906108c SS |
368 | && TYPE_CODE (t2) == TYPE_CODE_STRUCT |
369 | && !value_logical_not (arg2)) | |
370 | { | |
f23631e4 | 371 | struct value *v; |
c906108c SS |
372 | |
373 | /* Look in the type of the source to see if it contains the | |
7b83ea04 AC |
374 | type of the target as a superclass. If so, we'll need to |
375 | offset the pointer rather than just change its type. */ | |
c906108c SS |
376 | if (TYPE_NAME (t1) != NULL) |
377 | { | |
378 | v = search_struct_field (type_name_no_tag (t1), | |
379 | value_ind (arg2), 0, t2, 1); | |
380 | if (v) | |
381 | { | |
382 | v = value_addr (v); | |
df407dfe | 383 | v->type = type; |
c906108c SS |
384 | return v; |
385 | } | |
386 | } | |
387 | ||
388 | /* Look in the type of the target to see if it contains the | |
7b83ea04 AC |
389 | type of the source as a superclass. If so, we'll need to |
390 | offset the pointer rather than just change its type. | |
391 | FIXME: This fails silently with virtual inheritance. */ | |
c906108c SS |
392 | if (TYPE_NAME (t2) != NULL) |
393 | { | |
394 | v = search_struct_field (type_name_no_tag (t2), | |
c5aa993b | 395 | value_zero (t1, not_lval), 0, t1, 1); |
c906108c SS |
396 | if (v) |
397 | { | |
d174216d JB |
398 | CORE_ADDR addr2 = value_as_address (arg2); |
399 | addr2 -= (VALUE_ADDRESS (v) | |
df407dfe | 400 | + value_offset (v) |
13c3b5f5 | 401 | + value_embedded_offset (v)); |
d174216d | 402 | return value_from_pointer (type, addr2); |
c906108c SS |
403 | } |
404 | } | |
405 | } | |
406 | /* No superclass found, just fall through to change ptr type. */ | |
407 | } | |
df407dfe | 408 | arg2->type = type; |
2b127877 | 409 | arg2 = value_change_enclosing_type (arg2, type); |
c5aa993b | 410 | VALUE_POINTED_TO_OFFSET (arg2) = 0; /* pai: chk_val */ |
c906108c SS |
411 | return arg2; |
412 | } | |
c906108c | 413 | else if (VALUE_LVAL (arg2) == lval_memory) |
df407dfe | 414 | return value_at_lazy (type, VALUE_ADDRESS (arg2) + value_offset (arg2)); |
c906108c SS |
415 | else if (code1 == TYPE_CODE_VOID) |
416 | { | |
417 | return value_zero (builtin_type_void, not_lval); | |
418 | } | |
419 | else | |
420 | { | |
421 | error ("Invalid cast."); | |
422 | return 0; | |
423 | } | |
424 | } | |
425 | ||
426 | /* Create a value of type TYPE that is zero, and return it. */ | |
427 | ||
f23631e4 | 428 | struct value * |
fba45db2 | 429 | value_zero (struct type *type, enum lval_type lv) |
c906108c | 430 | { |
f23631e4 | 431 | struct value *val = allocate_value (type); |
c906108c SS |
432 | VALUE_LVAL (val) = lv; |
433 | ||
434 | return val; | |
435 | } | |
436 | ||
070ad9f0 | 437 | /* Return a value with type TYPE located at ADDR. |
c906108c SS |
438 | |
439 | Call value_at only if the data needs to be fetched immediately; | |
440 | if we can be 'lazy' and defer the fetch, perhaps indefinately, call | |
441 | value_at_lazy instead. value_at_lazy simply records the address of | |
070ad9f0 | 442 | the data and sets the lazy-evaluation-required flag. The lazy flag |
0fd88904 | 443 | is tested in the value_contents macro, which is used if and when |
070ad9f0 | 444 | the contents are actually required. |
c906108c SS |
445 | |
446 | Note: value_at does *NOT* handle embedded offsets; perform such | |
447 | adjustments before or after calling it. */ | |
448 | ||
f23631e4 | 449 | struct value * |
00a4c844 | 450 | value_at (struct type *type, CORE_ADDR addr) |
c906108c | 451 | { |
f23631e4 | 452 | struct value *val; |
c906108c SS |
453 | |
454 | if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID) | |
455 | error ("Attempt to dereference a generic pointer."); | |
456 | ||
457 | val = allocate_value (type); | |
458 | ||
990a07ab | 459 | read_memory (addr, value_contents_all_raw (val), TYPE_LENGTH (type)); |
c906108c SS |
460 | |
461 | VALUE_LVAL (val) = lval_memory; | |
462 | VALUE_ADDRESS (val) = addr; | |
c906108c SS |
463 | |
464 | return val; | |
465 | } | |
466 | ||
467 | /* Return a lazy value with type TYPE located at ADDR (cf. value_at). */ | |
468 | ||
f23631e4 | 469 | struct value * |
00a4c844 | 470 | value_at_lazy (struct type *type, CORE_ADDR addr) |
c906108c | 471 | { |
f23631e4 | 472 | struct value *val; |
c906108c SS |
473 | |
474 | if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID) | |
475 | error ("Attempt to dereference a generic pointer."); | |
476 | ||
477 | val = allocate_value (type); | |
478 | ||
479 | VALUE_LVAL (val) = lval_memory; | |
480 | VALUE_ADDRESS (val) = addr; | |
dfa52d88 | 481 | set_value_lazy (val, 1); |
c906108c SS |
482 | |
483 | return val; | |
484 | } | |
485 | ||
0fd88904 | 486 | /* Called only from the value_contents and value_contents_all() |
46615f07 | 487 | macros, if the current data for a variable needs to be loaded into |
0fd88904 | 488 | value_contents(VAL). Fetches the data from the user's process, and |
46615f07 AC |
489 | clears the lazy flag to indicate that the data in the buffer is |
490 | valid. | |
c906108c SS |
491 | |
492 | If the value is zero-length, we avoid calling read_memory, which would | |
493 | abort. We mark the value as fetched anyway -- all 0 bytes of it. | |
494 | ||
0fd88904 | 495 | This function returns a value because it is used in the value_contents |
c906108c SS |
496 | macro as part of an expression, where a void would not work. The |
497 | value is ignored. */ | |
498 | ||
499 | int | |
f23631e4 | 500 | value_fetch_lazy (struct value *val) |
c906108c | 501 | { |
df407dfe | 502 | CORE_ADDR addr = VALUE_ADDRESS (val) + value_offset (val); |
4754a64e | 503 | int length = TYPE_LENGTH (value_enclosing_type (val)); |
c906108c | 504 | |
df407dfe | 505 | struct type *type = value_type (val); |
75af7f68 | 506 | if (length) |
990a07ab | 507 | read_memory (addr, value_contents_all_raw (val), length); |
802db21b | 508 | |
dfa52d88 | 509 | set_value_lazy (val, 0); |
c906108c SS |
510 | return 0; |
511 | } | |
512 | ||
513 | ||
514 | /* Store the contents of FROMVAL into the location of TOVAL. | |
515 | Return a new value with the location of TOVAL and contents of FROMVAL. */ | |
516 | ||
f23631e4 AC |
517 | struct value * |
518 | value_assign (struct value *toval, struct value *fromval) | |
c906108c | 519 | { |
52f0bd74 | 520 | struct type *type; |
f23631e4 | 521 | struct value *val; |
cb741690 | 522 | struct frame_id old_frame; |
c906108c SS |
523 | |
524 | if (!toval->modifiable) | |
525 | error ("Left operand of assignment is not a modifiable lvalue."); | |
526 | ||
994b9211 | 527 | toval = coerce_ref (toval); |
c906108c | 528 | |
df407dfe | 529 | type = value_type (toval); |
c906108c SS |
530 | if (VALUE_LVAL (toval) != lval_internalvar) |
531 | fromval = value_cast (type, fromval); | |
532 | else | |
994b9211 | 533 | fromval = coerce_array (fromval); |
c906108c SS |
534 | CHECK_TYPEDEF (type); |
535 | ||
cb741690 DJ |
536 | /* Since modifying a register can trash the frame chain, and modifying memory |
537 | can trash the frame cache, we save the old frame and then restore the new | |
538 | frame afterwards. */ | |
539 | old_frame = get_frame_id (deprecated_selected_frame); | |
540 | ||
c906108c SS |
541 | switch (VALUE_LVAL (toval)) |
542 | { | |
543 | case lval_internalvar: | |
544 | set_internalvar (VALUE_INTERNALVAR (toval), fromval); | |
545 | val = value_copy (VALUE_INTERNALVAR (toval)->value); | |
4754a64e | 546 | val = value_change_enclosing_type (val, value_enclosing_type (fromval)); |
13c3b5f5 | 547 | set_value_embedded_offset (val, value_embedded_offset (fromval)); |
c906108c SS |
548 | VALUE_POINTED_TO_OFFSET (val) = VALUE_POINTED_TO_OFFSET (fromval); |
549 | return val; | |
550 | ||
551 | case lval_internalvar_component: | |
552 | set_internalvar_component (VALUE_INTERNALVAR (toval), | |
df407dfe AC |
553 | value_offset (toval), |
554 | value_bitpos (toval), | |
555 | value_bitsize (toval), | |
c906108c SS |
556 | fromval); |
557 | break; | |
558 | ||
559 | case lval_memory: | |
560 | { | |
0fd88904 | 561 | const bfd_byte *dest_buffer; |
c5aa993b JM |
562 | CORE_ADDR changed_addr; |
563 | int changed_len; | |
69657671 | 564 | char buffer[sizeof (LONGEST)]; |
c906108c | 565 | |
df407dfe | 566 | if (value_bitsize (toval)) |
c5aa993b | 567 | { |
c906108c SS |
568 | /* We assume that the argument to read_memory is in units of |
569 | host chars. FIXME: Is that correct? */ | |
df407dfe AC |
570 | changed_len = (value_bitpos (toval) |
571 | + value_bitsize (toval) | |
c5aa993b JM |
572 | + HOST_CHAR_BIT - 1) |
573 | / HOST_CHAR_BIT; | |
c906108c SS |
574 | |
575 | if (changed_len > (int) sizeof (LONGEST)) | |
576 | error ("Can't handle bitfields which don't fit in a %d bit word.", | |
baa6f10b | 577 | (int) sizeof (LONGEST) * HOST_CHAR_BIT); |
c906108c | 578 | |
df407dfe | 579 | read_memory (VALUE_ADDRESS (toval) + value_offset (toval), |
c906108c SS |
580 | buffer, changed_len); |
581 | modify_field (buffer, value_as_long (fromval), | |
df407dfe AC |
582 | value_bitpos (toval), value_bitsize (toval)); |
583 | changed_addr = VALUE_ADDRESS (toval) + value_offset (toval); | |
c906108c SS |
584 | dest_buffer = buffer; |
585 | } | |
c906108c SS |
586 | else |
587 | { | |
df407dfe | 588 | changed_addr = VALUE_ADDRESS (toval) + value_offset (toval); |
c906108c | 589 | changed_len = TYPE_LENGTH (type); |
0fd88904 | 590 | dest_buffer = value_contents (fromval); |
c906108c SS |
591 | } |
592 | ||
593 | write_memory (changed_addr, dest_buffer, changed_len); | |
9a4105ab AC |
594 | if (deprecated_memory_changed_hook) |
595 | deprecated_memory_changed_hook (changed_addr, changed_len); | |
c906108c SS |
596 | } |
597 | break; | |
598 | ||
492254e9 | 599 | case lval_register: |
c906108c | 600 | { |
c906108c | 601 | struct frame_info *frame; |
ff2e87ac | 602 | int value_reg; |
c906108c SS |
603 | |
604 | /* Figure out which frame this is in currently. */ | |
0c16dd26 AC |
605 | frame = frame_find_by_id (VALUE_FRAME_ID (toval)); |
606 | value_reg = VALUE_REGNUM (toval); | |
c906108c SS |
607 | |
608 | if (!frame) | |
609 | error ("Value being assigned to is no longer active."); | |
492254e9 | 610 | |
25ae5d16 | 611 | if (VALUE_LVAL (toval) == lval_register |
9ee8fc9d | 612 | && CONVERT_REGISTER_P (VALUE_REGNUM (toval), type)) |
492254e9 | 613 | { |
ff2e87ac AC |
614 | /* If TOVAL is a special machine register requiring |
615 | conversion of program values to a special raw format. */ | |
9ee8fc9d | 616 | VALUE_TO_REGISTER (frame, VALUE_REGNUM (toval), |
0fd88904 | 617 | type, value_contents (fromval)); |
492254e9 | 618 | } |
c906108c | 619 | else |
492254e9 | 620 | { |
ff2e87ac AC |
621 | /* TOVAL is stored in a series of registers in the frame |
622 | specified by the structure. Copy that value out, | |
623 | modify it, and copy it back in. */ | |
624 | int amount_copied; | |
625 | int amount_to_copy; | |
626 | char *buffer; | |
627 | int reg_offset; | |
628 | int byte_offset; | |
629 | int regno; | |
630 | ||
631 | /* Locate the first register that falls in the value that | |
632 | needs to be transfered. Compute the offset of the | |
633 | value in that register. */ | |
634 | { | |
635 | int offset; | |
636 | for (reg_offset = value_reg, offset = 0; | |
df407dfe | 637 | offset + register_size (current_gdbarch, reg_offset) <= value_offset (toval); |
ff2e87ac | 638 | reg_offset++); |
df407dfe | 639 | byte_offset = value_offset (toval) - offset; |
ff2e87ac | 640 | } |
c906108c | 641 | |
ff2e87ac AC |
642 | /* Compute the number of register aligned values that need |
643 | to be copied. */ | |
df407dfe | 644 | if (value_bitsize (toval)) |
ff2e87ac AC |
645 | amount_to_copy = byte_offset + 1; |
646 | else | |
647 | amount_to_copy = byte_offset + TYPE_LENGTH (type); | |
492254e9 | 648 | |
ff2e87ac AC |
649 | /* And a bounce buffer. Be slightly over generous. */ |
650 | buffer = (char *) alloca (amount_to_copy + MAX_REGISTER_SIZE); | |
651 | ||
652 | /* Copy it in. */ | |
653 | for (regno = reg_offset, amount_copied = 0; | |
654 | amount_copied < amount_to_copy; | |
3acba339 | 655 | amount_copied += register_size (current_gdbarch, regno), regno++) |
ff2e87ac | 656 | frame_register_read (frame, regno, buffer + amount_copied); |
492254e9 | 657 | |
ff2e87ac | 658 | /* Modify what needs to be modified. */ |
df407dfe | 659 | if (value_bitsize (toval)) |
ff2e87ac AC |
660 | modify_field (buffer + byte_offset, |
661 | value_as_long (fromval), | |
df407dfe | 662 | value_bitpos (toval), value_bitsize (toval)); |
c906108c | 663 | else |
0fd88904 | 664 | memcpy (buffer + byte_offset, value_contents (fromval), |
ff2e87ac AC |
665 | TYPE_LENGTH (type)); |
666 | ||
667 | /* Copy it out. */ | |
668 | for (regno = reg_offset, amount_copied = 0; | |
669 | amount_copied < amount_to_copy; | |
3acba339 | 670 | amount_copied += register_size (current_gdbarch, regno), regno++) |
ff2e87ac | 671 | put_frame_register (frame, regno, buffer + amount_copied); |
c906108c | 672 | |
ff2e87ac | 673 | } |
9a4105ab AC |
674 | if (deprecated_register_changed_hook) |
675 | deprecated_register_changed_hook (-1); | |
f4c5303c | 676 | observer_notify_target_changed (¤t_target); |
ff2e87ac | 677 | break; |
c906108c | 678 | } |
492254e9 | 679 | |
c906108c SS |
680 | default: |
681 | error ("Left operand of assignment is not an lvalue."); | |
682 | } | |
683 | ||
cb741690 DJ |
684 | /* Assigning to the stack pointer, frame pointer, and other |
685 | (architecture and calling convention specific) registers may | |
686 | cause the frame cache to be out of date. Assigning to memory | |
687 | also can. We just do this on all assignments to registers or | |
688 | memory, for simplicity's sake; I doubt the slowdown matters. */ | |
689 | switch (VALUE_LVAL (toval)) | |
690 | { | |
691 | case lval_memory: | |
692 | case lval_register: | |
cb741690 DJ |
693 | |
694 | reinit_frame_cache (); | |
695 | ||
696 | /* Having destoroyed the frame cache, restore the selected frame. */ | |
697 | ||
698 | /* FIXME: cagney/2002-11-02: There has to be a better way of | |
699 | doing this. Instead of constantly saving/restoring the | |
700 | frame. Why not create a get_selected_frame() function that, | |
701 | having saved the selected frame's ID can automatically | |
702 | re-find the previously selected frame automatically. */ | |
703 | ||
704 | { | |
705 | struct frame_info *fi = frame_find_by_id (old_frame); | |
706 | if (fi != NULL) | |
707 | select_frame (fi); | |
708 | } | |
709 | ||
710 | break; | |
711 | default: | |
712 | break; | |
713 | } | |
714 | ||
c906108c SS |
715 | /* If the field does not entirely fill a LONGEST, then zero the sign bits. |
716 | If the field is signed, and is negative, then sign extend. */ | |
df407dfe AC |
717 | if ((value_bitsize (toval) > 0) |
718 | && (value_bitsize (toval) < 8 * (int) sizeof (LONGEST))) | |
c906108c SS |
719 | { |
720 | LONGEST fieldval = value_as_long (fromval); | |
df407dfe | 721 | LONGEST valmask = (((ULONGEST) 1) << value_bitsize (toval)) - 1; |
c906108c SS |
722 | |
723 | fieldval &= valmask; | |
724 | if (!TYPE_UNSIGNED (type) && (fieldval & (valmask ^ (valmask >> 1)))) | |
725 | fieldval |= ~valmask; | |
726 | ||
727 | fromval = value_from_longest (type, fieldval); | |
728 | } | |
729 | ||
730 | val = value_copy (toval); | |
0fd88904 | 731 | memcpy (value_contents_raw (val), value_contents (fromval), |
c906108c | 732 | TYPE_LENGTH (type)); |
df407dfe | 733 | val->type = type; |
4754a64e | 734 | val = value_change_enclosing_type (val, value_enclosing_type (fromval)); |
13c3b5f5 | 735 | set_value_embedded_offset (val, value_embedded_offset (fromval)); |
c906108c | 736 | VALUE_POINTED_TO_OFFSET (val) = VALUE_POINTED_TO_OFFSET (fromval); |
c5aa993b | 737 | |
c906108c SS |
738 | return val; |
739 | } | |
740 | ||
741 | /* Extend a value VAL to COUNT repetitions of its type. */ | |
742 | ||
f23631e4 AC |
743 | struct value * |
744 | value_repeat (struct value *arg1, int count) | |
c906108c | 745 | { |
f23631e4 | 746 | struct value *val; |
c906108c SS |
747 | |
748 | if (VALUE_LVAL (arg1) != lval_memory) | |
749 | error ("Only values in memory can be extended with '@'."); | |
750 | if (count < 1) | |
751 | error ("Invalid number %d of repetitions.", count); | |
752 | ||
4754a64e | 753 | val = allocate_repeat_value (value_enclosing_type (arg1), count); |
c906108c | 754 | |
df407dfe | 755 | read_memory (VALUE_ADDRESS (arg1) + value_offset (arg1), |
990a07ab | 756 | value_contents_all_raw (val), |
4754a64e | 757 | TYPE_LENGTH (value_enclosing_type (val))); |
c906108c | 758 | VALUE_LVAL (val) = lval_memory; |
df407dfe | 759 | VALUE_ADDRESS (val) = VALUE_ADDRESS (arg1) + value_offset (arg1); |
c906108c SS |
760 | |
761 | return val; | |
762 | } | |
763 | ||
f23631e4 | 764 | struct value * |
fba45db2 | 765 | value_of_variable (struct symbol *var, struct block *b) |
c906108c | 766 | { |
f23631e4 | 767 | struct value *val; |
c906108c SS |
768 | struct frame_info *frame = NULL; |
769 | ||
770 | if (!b) | |
771 | frame = NULL; /* Use selected frame. */ | |
772 | else if (symbol_read_needs_frame (var)) | |
773 | { | |
774 | frame = block_innermost_frame (b); | |
775 | if (!frame) | |
c5aa993b | 776 | { |
c906108c | 777 | if (BLOCK_FUNCTION (b) |
de5ad195 | 778 | && SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b))) |
c906108c | 779 | error ("No frame is currently executing in block %s.", |
de5ad195 | 780 | SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b))); |
c906108c SS |
781 | else |
782 | error ("No frame is currently executing in specified block"); | |
c5aa993b | 783 | } |
c906108c SS |
784 | } |
785 | ||
786 | val = read_var_value (var, frame); | |
787 | if (!val) | |
de5ad195 | 788 | error ("Address of symbol \"%s\" is unknown.", SYMBOL_PRINT_NAME (var)); |
c906108c SS |
789 | |
790 | return val; | |
791 | } | |
792 | ||
793 | /* Given a value which is an array, return a value which is a pointer to its | |
794 | first element, regardless of whether or not the array has a nonzero lower | |
795 | bound. | |
796 | ||
797 | FIXME: A previous comment here indicated that this routine should be | |
798 | substracting the array's lower bound. It's not clear to me that this | |
799 | is correct. Given an array subscripting operation, it would certainly | |
800 | work to do the adjustment here, essentially computing: | |
801 | ||
802 | (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0]) | |
803 | ||
804 | However I believe a more appropriate and logical place to account for | |
805 | the lower bound is to do so in value_subscript, essentially computing: | |
806 | ||
807 | (&array[0] + ((index - lowerbound) * sizeof array[0])) | |
808 | ||
809 | As further evidence consider what would happen with operations other | |
810 | than array subscripting, where the caller would get back a value that | |
811 | had an address somewhere before the actual first element of the array, | |
812 | and the information about the lower bound would be lost because of | |
813 | the coercion to pointer type. | |
c5aa993b | 814 | */ |
c906108c | 815 | |
f23631e4 AC |
816 | struct value * |
817 | value_coerce_array (struct value *arg1) | |
c906108c | 818 | { |
df407dfe | 819 | struct type *type = check_typedef (value_type (arg1)); |
c906108c SS |
820 | |
821 | if (VALUE_LVAL (arg1) != lval_memory) | |
822 | error ("Attempt to take address of value not located in memory."); | |
823 | ||
4478b372 | 824 | return value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type)), |
df407dfe | 825 | (VALUE_ADDRESS (arg1) + value_offset (arg1))); |
c906108c SS |
826 | } |
827 | ||
828 | /* Given a value which is a function, return a value which is a pointer | |
829 | to it. */ | |
830 | ||
f23631e4 AC |
831 | struct value * |
832 | value_coerce_function (struct value *arg1) | |
c906108c | 833 | { |
f23631e4 | 834 | struct value *retval; |
c906108c SS |
835 | |
836 | if (VALUE_LVAL (arg1) != lval_memory) | |
837 | error ("Attempt to take address of value not located in memory."); | |
838 | ||
df407dfe AC |
839 | retval = value_from_pointer (lookup_pointer_type (value_type (arg1)), |
840 | (VALUE_ADDRESS (arg1) + value_offset (arg1))); | |
c906108c | 841 | return retval; |
c5aa993b | 842 | } |
c906108c SS |
843 | |
844 | /* Return a pointer value for the object for which ARG1 is the contents. */ | |
845 | ||
f23631e4 AC |
846 | struct value * |
847 | value_addr (struct value *arg1) | |
c906108c | 848 | { |
f23631e4 | 849 | struct value *arg2; |
c906108c | 850 | |
df407dfe | 851 | struct type *type = check_typedef (value_type (arg1)); |
c906108c SS |
852 | if (TYPE_CODE (type) == TYPE_CODE_REF) |
853 | { | |
854 | /* Copy the value, but change the type from (T&) to (T*). | |
7b83ea04 AC |
855 | We keep the same location information, which is efficient, |
856 | and allows &(&X) to get the location containing the reference. */ | |
c906108c | 857 | arg2 = value_copy (arg1); |
df407dfe | 858 | arg2->type = lookup_pointer_type (TYPE_TARGET_TYPE (type)); |
c906108c SS |
859 | return arg2; |
860 | } | |
861 | if (TYPE_CODE (type) == TYPE_CODE_FUNC) | |
862 | return value_coerce_function (arg1); | |
863 | ||
864 | if (VALUE_LVAL (arg1) != lval_memory) | |
865 | error ("Attempt to take address of value not located in memory."); | |
866 | ||
c5aa993b | 867 | /* Get target memory address */ |
df407dfe | 868 | arg2 = value_from_pointer (lookup_pointer_type (value_type (arg1)), |
4478b372 | 869 | (VALUE_ADDRESS (arg1) |
df407dfe | 870 | + value_offset (arg1) |
13c3b5f5 | 871 | + value_embedded_offset (arg1))); |
c906108c SS |
872 | |
873 | /* This may be a pointer to a base subobject; so remember the | |
c5aa993b | 874 | full derived object's type ... */ |
4754a64e | 875 | arg2 = value_change_enclosing_type (arg2, lookup_pointer_type (value_enclosing_type (arg1))); |
c5aa993b | 876 | /* ... and also the relative position of the subobject in the full object */ |
13c3b5f5 | 877 | VALUE_POINTED_TO_OFFSET (arg2) = value_embedded_offset (arg1); |
c906108c SS |
878 | return arg2; |
879 | } | |
880 | ||
881 | /* Given a value of a pointer type, apply the C unary * operator to it. */ | |
882 | ||
f23631e4 AC |
883 | struct value * |
884 | value_ind (struct value *arg1) | |
c906108c SS |
885 | { |
886 | struct type *base_type; | |
f23631e4 | 887 | struct value *arg2; |
c906108c | 888 | |
994b9211 | 889 | arg1 = coerce_array (arg1); |
c906108c | 890 | |
df407dfe | 891 | base_type = check_typedef (value_type (arg1)); |
c906108c SS |
892 | |
893 | if (TYPE_CODE (base_type) == TYPE_CODE_MEMBER) | |
894 | error ("not implemented: member types in value_ind"); | |
895 | ||
896 | /* Allow * on an integer so we can cast it to whatever we want. | |
897 | This returns an int, which seems like the most C-like thing | |
898 | to do. "long long" variables are rare enough that | |
899 | BUILTIN_TYPE_LONGEST would seem to be a mistake. */ | |
900 | if (TYPE_CODE (base_type) == TYPE_CODE_INT) | |
56468235 | 901 | return value_at_lazy (builtin_type_int, |
00a4c844 | 902 | (CORE_ADDR) value_as_long (arg1)); |
c906108c SS |
903 | else if (TYPE_CODE (base_type) == TYPE_CODE_PTR) |
904 | { | |
905 | struct type *enc_type; | |
906 | /* We may be pointing to something embedded in a larger object */ | |
c5aa993b | 907 | /* Get the real type of the enclosing object */ |
4754a64e | 908 | enc_type = check_typedef (value_enclosing_type (arg1)); |
c906108c | 909 | enc_type = TYPE_TARGET_TYPE (enc_type); |
c5aa993b | 910 | /* Retrieve the enclosing object pointed to */ |
00a4c844 AC |
911 | arg2 = value_at_lazy (enc_type, (value_as_address (arg1) |
912 | - VALUE_POINTED_TO_OFFSET (arg1))); | |
c5aa993b | 913 | /* Re-adjust type */ |
df407dfe | 914 | arg2->type = TYPE_TARGET_TYPE (base_type); |
c906108c | 915 | /* Add embedding info */ |
2b127877 | 916 | arg2 = value_change_enclosing_type (arg2, enc_type); |
13c3b5f5 | 917 | set_value_embedded_offset (arg2, VALUE_POINTED_TO_OFFSET (arg1)); |
c906108c SS |
918 | |
919 | /* We may be pointing to an object of some derived type */ | |
920 | arg2 = value_full_object (arg2, NULL, 0, 0, 0); | |
921 | return arg2; | |
922 | } | |
923 | ||
924 | error ("Attempt to take contents of a non-pointer value."); | |
c5aa993b | 925 | return 0; /* For lint -- never reached */ |
c906108c SS |
926 | } |
927 | \f | |
928 | /* Pushing small parts of stack frames. */ | |
929 | ||
930 | /* Push one word (the size of object that a register holds). */ | |
931 | ||
932 | CORE_ADDR | |
fba45db2 | 933 | push_word (CORE_ADDR sp, ULONGEST word) |
c906108c | 934 | { |
52f0bd74 | 935 | int len = DEPRECATED_REGISTER_SIZE; |
eb294659 | 936 | char buffer[MAX_REGISTER_SIZE]; |
c906108c SS |
937 | |
938 | store_unsigned_integer (buffer, len, word); | |
939 | if (INNER_THAN (1, 2)) | |
940 | { | |
941 | /* stack grows downward */ | |
942 | sp -= len; | |
943 | write_memory (sp, buffer, len); | |
944 | } | |
945 | else | |
946 | { | |
947 | /* stack grows upward */ | |
948 | write_memory (sp, buffer, len); | |
949 | sp += len; | |
950 | } | |
951 | ||
952 | return sp; | |
953 | } | |
954 | ||
955 | /* Push LEN bytes with data at BUFFER. */ | |
956 | ||
957 | CORE_ADDR | |
fba45db2 | 958 | push_bytes (CORE_ADDR sp, char *buffer, int len) |
c906108c SS |
959 | { |
960 | if (INNER_THAN (1, 2)) | |
961 | { | |
962 | /* stack grows downward */ | |
963 | sp -= len; | |
964 | write_memory (sp, buffer, len); | |
965 | } | |
966 | else | |
967 | { | |
968 | /* stack grows upward */ | |
969 | write_memory (sp, buffer, len); | |
970 | sp += len; | |
971 | } | |
972 | ||
973 | return sp; | |
974 | } | |
975 | ||
c906108c SS |
976 | /* Create a value for an array by allocating space in the inferior, copying |
977 | the data into that space, and then setting up an array value. | |
978 | ||
979 | The array bounds are set from LOWBOUND and HIGHBOUND, and the array is | |
980 | populated from the values passed in ELEMVEC. | |
981 | ||
982 | The element type of the array is inherited from the type of the | |
983 | first element, and all elements must have the same size (though we | |
984 | don't currently enforce any restriction on their types). */ | |
985 | ||
f23631e4 AC |
986 | struct value * |
987 | value_array (int lowbound, int highbound, struct value **elemvec) | |
c906108c SS |
988 | { |
989 | int nelem; | |
990 | int idx; | |
991 | unsigned int typelength; | |
f23631e4 | 992 | struct value *val; |
c906108c SS |
993 | struct type *rangetype; |
994 | struct type *arraytype; | |
995 | CORE_ADDR addr; | |
996 | ||
997 | /* Validate that the bounds are reasonable and that each of the elements | |
998 | have the same size. */ | |
999 | ||
1000 | nelem = highbound - lowbound + 1; | |
1001 | if (nelem <= 0) | |
1002 | { | |
1003 | error ("bad array bounds (%d, %d)", lowbound, highbound); | |
1004 | } | |
4754a64e | 1005 | typelength = TYPE_LENGTH (value_enclosing_type (elemvec[0])); |
c906108c SS |
1006 | for (idx = 1; idx < nelem; idx++) |
1007 | { | |
4754a64e | 1008 | if (TYPE_LENGTH (value_enclosing_type (elemvec[idx])) != typelength) |
c906108c SS |
1009 | { |
1010 | error ("array elements must all be the same size"); | |
1011 | } | |
1012 | } | |
1013 | ||
1014 | rangetype = create_range_type ((struct type *) NULL, builtin_type_int, | |
1015 | lowbound, highbound); | |
c5aa993b | 1016 | arraytype = create_array_type ((struct type *) NULL, |
4754a64e | 1017 | value_enclosing_type (elemvec[0]), rangetype); |
c906108c SS |
1018 | |
1019 | if (!current_language->c_style_arrays) | |
1020 | { | |
1021 | val = allocate_value (arraytype); | |
1022 | for (idx = 0; idx < nelem; idx++) | |
1023 | { | |
990a07ab | 1024 | memcpy (value_contents_all_raw (val) + (idx * typelength), |
46615f07 | 1025 | value_contents_all (elemvec[idx]), |
c906108c SS |
1026 | typelength); |
1027 | } | |
c906108c SS |
1028 | return val; |
1029 | } | |
1030 | ||
1031 | /* Allocate space to store the array in the inferior, and then initialize | |
1032 | it by copying in each element. FIXME: Is it worth it to create a | |
1033 | local buffer in which to collect each value and then write all the | |
1034 | bytes in one operation? */ | |
1035 | ||
1036 | addr = allocate_space_in_inferior (nelem * typelength); | |
1037 | for (idx = 0; idx < nelem; idx++) | |
1038 | { | |
46615f07 AC |
1039 | write_memory (addr + (idx * typelength), |
1040 | value_contents_all (elemvec[idx]), | |
c906108c SS |
1041 | typelength); |
1042 | } | |
1043 | ||
1044 | /* Create the array type and set up an array value to be evaluated lazily. */ | |
1045 | ||
00a4c844 | 1046 | val = value_at_lazy (arraytype, addr); |
c906108c SS |
1047 | return (val); |
1048 | } | |
1049 | ||
1050 | /* Create a value for a string constant by allocating space in the inferior, | |
1051 | copying the data into that space, and returning the address with type | |
1052 | TYPE_CODE_STRING. PTR points to the string constant data; LEN is number | |
1053 | of characters. | |
1054 | Note that string types are like array of char types with a lower bound of | |
1055 | zero and an upper bound of LEN - 1. Also note that the string may contain | |
1056 | embedded null bytes. */ | |
1057 | ||
f23631e4 | 1058 | struct value * |
fba45db2 | 1059 | value_string (char *ptr, int len) |
c906108c | 1060 | { |
f23631e4 | 1061 | struct value *val; |
c906108c SS |
1062 | int lowbound = current_language->string_lower_bound; |
1063 | struct type *rangetype = create_range_type ((struct type *) NULL, | |
1064 | builtin_type_int, | |
1065 | lowbound, len + lowbound - 1); | |
1066 | struct type *stringtype | |
c5aa993b | 1067 | = create_string_type ((struct type *) NULL, rangetype); |
c906108c SS |
1068 | CORE_ADDR addr; |
1069 | ||
1070 | if (current_language->c_style_arrays == 0) | |
1071 | { | |
1072 | val = allocate_value (stringtype); | |
990a07ab | 1073 | memcpy (value_contents_raw (val), ptr, len); |
c906108c SS |
1074 | return val; |
1075 | } | |
1076 | ||
1077 | ||
1078 | /* Allocate space to store the string in the inferior, and then | |
1079 | copy LEN bytes from PTR in gdb to that address in the inferior. */ | |
1080 | ||
1081 | addr = allocate_space_in_inferior (len); | |
1082 | write_memory (addr, ptr, len); | |
1083 | ||
00a4c844 | 1084 | val = value_at_lazy (stringtype, addr); |
c906108c SS |
1085 | return (val); |
1086 | } | |
1087 | ||
f23631e4 | 1088 | struct value * |
fba45db2 | 1089 | value_bitstring (char *ptr, int len) |
c906108c | 1090 | { |
f23631e4 | 1091 | struct value *val; |
c906108c SS |
1092 | struct type *domain_type = create_range_type (NULL, builtin_type_int, |
1093 | 0, len - 1); | |
c5aa993b | 1094 | struct type *type = create_set_type ((struct type *) NULL, domain_type); |
c906108c SS |
1095 | TYPE_CODE (type) = TYPE_CODE_BITSTRING; |
1096 | val = allocate_value (type); | |
990a07ab | 1097 | memcpy (value_contents_raw (val), ptr, TYPE_LENGTH (type)); |
c906108c SS |
1098 | return val; |
1099 | } | |
1100 | \f | |
1101 | /* See if we can pass arguments in T2 to a function which takes arguments | |
ad2f7632 DJ |
1102 | of types T1. T1 is a list of NARGS arguments, and T2 is a NULL-terminated |
1103 | vector. If some arguments need coercion of some sort, then the coerced | |
1104 | values are written into T2. Return value is 0 if the arguments could be | |
1105 | matched, or the position at which they differ if not. | |
c906108c SS |
1106 | |
1107 | STATICP is nonzero if the T1 argument list came from a | |
ad2f7632 DJ |
1108 | static member function. T2 will still include the ``this'' pointer, |
1109 | but it will be skipped. | |
c906108c SS |
1110 | |
1111 | For non-static member functions, we ignore the first argument, | |
1112 | which is the type of the instance variable. This is because we want | |
1113 | to handle calls with objects from derived classes. This is not | |
1114 | entirely correct: we should actually check to make sure that a | |
1115 | requested operation is type secure, shouldn't we? FIXME. */ | |
1116 | ||
1117 | static int | |
ad2f7632 DJ |
1118 | typecmp (int staticp, int varargs, int nargs, |
1119 | struct field t1[], struct value *t2[]) | |
c906108c SS |
1120 | { |
1121 | int i; | |
1122 | ||
1123 | if (t2 == 0) | |
ad2f7632 DJ |
1124 | internal_error (__FILE__, __LINE__, "typecmp: no argument list"); |
1125 | ||
4a1970e4 DJ |
1126 | /* Skip ``this'' argument if applicable. T2 will always include THIS. */ |
1127 | if (staticp) | |
ad2f7632 DJ |
1128 | t2 ++; |
1129 | ||
1130 | for (i = 0; | |
1131 | (i < nargs) && TYPE_CODE (t1[i].type) != TYPE_CODE_VOID; | |
1132 | i++) | |
c906108c | 1133 | { |
c5aa993b | 1134 | struct type *tt1, *tt2; |
ad2f7632 | 1135 | |
c5aa993b JM |
1136 | if (!t2[i]) |
1137 | return i + 1; | |
ad2f7632 DJ |
1138 | |
1139 | tt1 = check_typedef (t1[i].type); | |
df407dfe | 1140 | tt2 = check_typedef (value_type (t2[i])); |
ad2f7632 | 1141 | |
c906108c | 1142 | if (TYPE_CODE (tt1) == TYPE_CODE_REF |
c5aa993b | 1143 | /* We should be doing hairy argument matching, as below. */ |
c906108c SS |
1144 | && (TYPE_CODE (check_typedef (TYPE_TARGET_TYPE (tt1))) == TYPE_CODE (tt2))) |
1145 | { | |
1146 | if (TYPE_CODE (tt2) == TYPE_CODE_ARRAY) | |
1147 | t2[i] = value_coerce_array (t2[i]); | |
1148 | else | |
1149 | t2[i] = value_addr (t2[i]); | |
1150 | continue; | |
1151 | } | |
1152 | ||
802db21b DB |
1153 | /* djb - 20000715 - Until the new type structure is in the |
1154 | place, and we can attempt things like implicit conversions, | |
1155 | we need to do this so you can take something like a map<const | |
1156 | char *>, and properly access map["hello"], because the | |
1157 | argument to [] will be a reference to a pointer to a char, | |
7168a814 | 1158 | and the argument will be a pointer to a char. */ |
802db21b DB |
1159 | while ( TYPE_CODE(tt1) == TYPE_CODE_REF || |
1160 | TYPE_CODE (tt1) == TYPE_CODE_PTR) | |
1161 | { | |
1162 | tt1 = check_typedef( TYPE_TARGET_TYPE(tt1) ); | |
1163 | } | |
1164 | while ( TYPE_CODE(tt2) == TYPE_CODE_ARRAY || | |
1165 | TYPE_CODE(tt2) == TYPE_CODE_PTR || | |
1166 | TYPE_CODE(tt2) == TYPE_CODE_REF) | |
c906108c | 1167 | { |
802db21b | 1168 | tt2 = check_typedef( TYPE_TARGET_TYPE(tt2) ); |
c906108c | 1169 | } |
c5aa993b JM |
1170 | if (TYPE_CODE (tt1) == TYPE_CODE (tt2)) |
1171 | continue; | |
c906108c SS |
1172 | /* Array to pointer is a `trivial conversion' according to the ARM. */ |
1173 | ||
1174 | /* We should be doing much hairier argument matching (see section 13.2 | |
7b83ea04 AC |
1175 | of the ARM), but as a quick kludge, just check for the same type |
1176 | code. */ | |
df407dfe | 1177 | if (TYPE_CODE (t1[i].type) != TYPE_CODE (value_type (t2[i]))) |
c5aa993b | 1178 | return i + 1; |
c906108c | 1179 | } |
ad2f7632 | 1180 | if (varargs || t2[i] == NULL) |
c5aa993b | 1181 | return 0; |
ad2f7632 | 1182 | return i + 1; |
c906108c SS |
1183 | } |
1184 | ||
1185 | /* Helper function used by value_struct_elt to recurse through baseclasses. | |
1186 | Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes, | |
1187 | and search in it assuming it has (class) type TYPE. | |
1188 | If found, return value, else return NULL. | |
1189 | ||
1190 | If LOOKING_FOR_BASECLASS, then instead of looking for struct fields, | |
1191 | look for a baseclass named NAME. */ | |
1192 | ||
f23631e4 AC |
1193 | static struct value * |
1194 | search_struct_field (char *name, struct value *arg1, int offset, | |
aa1ee363 | 1195 | struct type *type, int looking_for_baseclass) |
c906108c SS |
1196 | { |
1197 | int i; | |
1198 | int nbases = TYPE_N_BASECLASSES (type); | |
1199 | ||
1200 | CHECK_TYPEDEF (type); | |
1201 | ||
c5aa993b | 1202 | if (!looking_for_baseclass) |
c906108c SS |
1203 | for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--) |
1204 | { | |
1205 | char *t_field_name = TYPE_FIELD_NAME (type, i); | |
1206 | ||
db577aea | 1207 | if (t_field_name && (strcmp_iw (t_field_name, name) == 0)) |
c906108c | 1208 | { |
f23631e4 | 1209 | struct value *v; |
c906108c | 1210 | if (TYPE_FIELD_STATIC (type, i)) |
2c2738a0 DC |
1211 | { |
1212 | v = value_static_field (type, i); | |
1213 | if (v == 0) | |
1214 | error ("field %s is nonexistent or has been optimised out", | |
1215 | name); | |
1216 | } | |
c906108c | 1217 | else |
2c2738a0 DC |
1218 | { |
1219 | v = value_primitive_field (arg1, offset, i, type); | |
1220 | if (v == 0) | |
1221 | error ("there is no field named %s", name); | |
1222 | } | |
c906108c SS |
1223 | return v; |
1224 | } | |
1225 | ||
1226 | if (t_field_name | |
1227 | && (t_field_name[0] == '\0' | |
1228 | || (TYPE_CODE (type) == TYPE_CODE_UNION | |
db577aea | 1229 | && (strcmp_iw (t_field_name, "else") == 0)))) |
c906108c SS |
1230 | { |
1231 | struct type *field_type = TYPE_FIELD_TYPE (type, i); | |
1232 | if (TYPE_CODE (field_type) == TYPE_CODE_UNION | |
1233 | || TYPE_CODE (field_type) == TYPE_CODE_STRUCT) | |
1234 | { | |
1235 | /* Look for a match through the fields of an anonymous union, | |
1236 | or anonymous struct. C++ provides anonymous unions. | |
1237 | ||
1b831c93 AC |
1238 | In the GNU Chill (now deleted from GDB) |
1239 | implementation of variant record types, each | |
1240 | <alternative field> has an (anonymous) union type, | |
1241 | each member of the union represents a <variant | |
1242 | alternative>. Each <variant alternative> is | |
1243 | represented as a struct, with a member for each | |
1244 | <variant field>. */ | |
c5aa993b | 1245 | |
f23631e4 | 1246 | struct value *v; |
c906108c SS |
1247 | int new_offset = offset; |
1248 | ||
db034ac5 AC |
1249 | /* This is pretty gross. In G++, the offset in an |
1250 | anonymous union is relative to the beginning of the | |
1b831c93 AC |
1251 | enclosing struct. In the GNU Chill (now deleted |
1252 | from GDB) implementation of variant records, the | |
1253 | bitpos is zero in an anonymous union field, so we | |
1254 | have to add the offset of the union here. */ | |
c906108c SS |
1255 | if (TYPE_CODE (field_type) == TYPE_CODE_STRUCT |
1256 | || (TYPE_NFIELDS (field_type) > 0 | |
1257 | && TYPE_FIELD_BITPOS (field_type, 0) == 0)) | |
1258 | new_offset += TYPE_FIELD_BITPOS (type, i) / 8; | |
1259 | ||
1260 | v = search_struct_field (name, arg1, new_offset, field_type, | |
1261 | looking_for_baseclass); | |
1262 | if (v) | |
1263 | return v; | |
1264 | } | |
1265 | } | |
1266 | } | |
1267 | ||
c5aa993b | 1268 | for (i = 0; i < nbases; i++) |
c906108c | 1269 | { |
f23631e4 | 1270 | struct value *v; |
c906108c SS |
1271 | struct type *basetype = check_typedef (TYPE_BASECLASS (type, i)); |
1272 | /* If we are looking for baseclasses, this is what we get when we | |
7b83ea04 AC |
1273 | hit them. But it could happen that the base part's member name |
1274 | is not yet filled in. */ | |
c906108c SS |
1275 | int found_baseclass = (looking_for_baseclass |
1276 | && TYPE_BASECLASS_NAME (type, i) != NULL | |
db577aea | 1277 | && (strcmp_iw (name, TYPE_BASECLASS_NAME (type, i)) == 0)); |
c906108c SS |
1278 | |
1279 | if (BASETYPE_VIA_VIRTUAL (type, i)) | |
1280 | { | |
1281 | int boffset; | |
f23631e4 | 1282 | struct value *v2 = allocate_value (basetype); |
c906108c SS |
1283 | |
1284 | boffset = baseclass_offset (type, i, | |
0fd88904 | 1285 | value_contents (arg1) + offset, |
c906108c | 1286 | VALUE_ADDRESS (arg1) |
df407dfe | 1287 | + value_offset (arg1) + offset); |
c906108c SS |
1288 | if (boffset == -1) |
1289 | error ("virtual baseclass botch"); | |
1290 | ||
1291 | /* The virtual base class pointer might have been clobbered by the | |
1292 | user program. Make sure that it still points to a valid memory | |
1293 | location. */ | |
1294 | ||
1295 | boffset += offset; | |
1296 | if (boffset < 0 || boffset >= TYPE_LENGTH (type)) | |
1297 | { | |
1298 | CORE_ADDR base_addr; | |
c5aa993b | 1299 | |
df407dfe | 1300 | base_addr = VALUE_ADDRESS (arg1) + value_offset (arg1) + boffset; |
990a07ab | 1301 | if (target_read_memory (base_addr, value_contents_raw (v2), |
c906108c SS |
1302 | TYPE_LENGTH (basetype)) != 0) |
1303 | error ("virtual baseclass botch"); | |
1304 | VALUE_LVAL (v2) = lval_memory; | |
1305 | VALUE_ADDRESS (v2) = base_addr; | |
1306 | } | |
1307 | else | |
1308 | { | |
1309 | VALUE_LVAL (v2) = VALUE_LVAL (arg1); | |
1310 | VALUE_ADDRESS (v2) = VALUE_ADDRESS (arg1); | |
65d3800a | 1311 | VALUE_FRAME_ID (v2) = VALUE_FRAME_ID (arg1); |
df407dfe | 1312 | v2->offset = value_offset (arg1) + boffset; |
d69fe07e | 1313 | if (value_lazy (arg1)) |
dfa52d88 | 1314 | set_value_lazy (v2, 1); |
c906108c | 1315 | else |
990a07ab AC |
1316 | memcpy (value_contents_raw (v2), |
1317 | value_contents_raw (arg1) + boffset, | |
c906108c SS |
1318 | TYPE_LENGTH (basetype)); |
1319 | } | |
1320 | ||
1321 | if (found_baseclass) | |
1322 | return v2; | |
1323 | v = search_struct_field (name, v2, 0, TYPE_BASECLASS (type, i), | |
1324 | looking_for_baseclass); | |
1325 | } | |
1326 | else if (found_baseclass) | |
1327 | v = value_primitive_field (arg1, offset, i, type); | |
1328 | else | |
1329 | v = search_struct_field (name, arg1, | |
c5aa993b | 1330 | offset + TYPE_BASECLASS_BITPOS (type, i) / 8, |
c906108c | 1331 | basetype, looking_for_baseclass); |
c5aa993b JM |
1332 | if (v) |
1333 | return v; | |
c906108c SS |
1334 | } |
1335 | return NULL; | |
1336 | } | |
1337 | ||
1338 | ||
1339 | /* Return the offset (in bytes) of the virtual base of type BASETYPE | |
1340 | * in an object pointed to by VALADDR (on the host), assumed to be of | |
1341 | * type TYPE. OFFSET is number of bytes beyond start of ARG to start | |
1342 | * looking (in case VALADDR is the contents of an enclosing object). | |
1343 | * | |
1344 | * This routine recurses on the primary base of the derived class because | |
1345 | * the virtual base entries of the primary base appear before the other | |
1346 | * virtual base entries. | |
1347 | * | |
1348 | * If the virtual base is not found, a negative integer is returned. | |
1349 | * The magnitude of the negative integer is the number of entries in | |
1350 | * the virtual table to skip over (entries corresponding to various | |
1351 | * ancestral classes in the chain of primary bases). | |
1352 | * | |
1353 | * Important: This assumes the HP / Taligent C++ runtime | |
1354 | * conventions. Use baseclass_offset() instead to deal with g++ | |
1355 | * conventions. */ | |
1356 | ||
1357 | void | |
c84141d6 AC |
1358 | find_rt_vbase_offset (struct type *type, struct type *basetype, |
1359 | const bfd_byte *valaddr, int offset, int *boffset_p, | |
1360 | int *skip_p) | |
c906108c | 1361 | { |
c5aa993b JM |
1362 | int boffset; /* offset of virtual base */ |
1363 | int index; /* displacement to use in virtual table */ | |
c906108c | 1364 | int skip; |
c5aa993b | 1365 | |
f23631e4 | 1366 | struct value *vp; |
c5aa993b JM |
1367 | CORE_ADDR vtbl; /* the virtual table pointer */ |
1368 | struct type *pbc; /* the primary base class */ | |
c906108c SS |
1369 | |
1370 | /* Look for the virtual base recursively in the primary base, first. | |
1371 | * This is because the derived class object and its primary base | |
1372 | * subobject share the primary virtual table. */ | |
c5aa993b | 1373 | |
c906108c | 1374 | boffset = 0; |
c5aa993b | 1375 | pbc = TYPE_PRIMARY_BASE (type); |
c906108c SS |
1376 | if (pbc) |
1377 | { | |
1378 | find_rt_vbase_offset (pbc, basetype, valaddr, offset, &boffset, &skip); | |
1379 | if (skip < 0) | |
c5aa993b JM |
1380 | { |
1381 | *boffset_p = boffset; | |
1382 | *skip_p = -1; | |
1383 | return; | |
1384 | } | |
c906108c SS |
1385 | } |
1386 | else | |
1387 | skip = 0; | |
1388 | ||
1389 | ||
1390 | /* Find the index of the virtual base according to HP/Taligent | |
1391 | runtime spec. (Depth-first, left-to-right.) */ | |
1392 | index = virtual_base_index_skip_primaries (basetype, type); | |
1393 | ||
c5aa993b JM |
1394 | if (index < 0) |
1395 | { | |
1396 | *skip_p = skip + virtual_base_list_length_skip_primaries (type); | |
1397 | *boffset_p = 0; | |
1398 | return; | |
1399 | } | |
c906108c | 1400 | |
c5aa993b | 1401 | /* pai: FIXME -- 32x64 possible problem */ |
c906108c | 1402 | /* First word (4 bytes) in object layout is the vtable pointer */ |
c5aa993b | 1403 | vtbl = *(CORE_ADDR *) (valaddr + offset); |
c906108c | 1404 | |
c5aa993b | 1405 | /* Before the constructor is invoked, things are usually zero'd out. */ |
c906108c SS |
1406 | if (vtbl == 0) |
1407 | error ("Couldn't find virtual table -- object may not be constructed yet."); | |
1408 | ||
1409 | ||
1410 | /* Find virtual base's offset -- jump over entries for primary base | |
1411 | * ancestors, then use the index computed above. But also adjust by | |
1412 | * HP_ACC_VBASE_START for the vtable slots before the start of the | |
1413 | * virtual base entries. Offset is negative -- virtual base entries | |
1414 | * appear _before_ the address point of the virtual table. */ | |
c5aa993b | 1415 | |
070ad9f0 | 1416 | /* pai: FIXME -- 32x64 problem, if word = 8 bytes, change multiplier |
c5aa993b | 1417 | & use long type */ |
c906108c SS |
1418 | |
1419 | /* epstein : FIXME -- added param for overlay section. May not be correct */ | |
00a4c844 | 1420 | vp = value_at (builtin_type_int, vtbl + 4 * (-skip - index - HP_ACC_VBASE_START)); |
c906108c SS |
1421 | boffset = value_as_long (vp); |
1422 | *skip_p = -1; | |
1423 | *boffset_p = boffset; | |
1424 | return; | |
1425 | } | |
1426 | ||
1427 | ||
1428 | /* Helper function used by value_struct_elt to recurse through baseclasses. | |
1429 | Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes, | |
1430 | and search in it assuming it has (class) type TYPE. | |
1431 | If found, return value, else if name matched and args not return (value)-1, | |
1432 | else return NULL. */ | |
1433 | ||
f23631e4 AC |
1434 | static struct value * |
1435 | search_struct_method (char *name, struct value **arg1p, | |
1436 | struct value **args, int offset, | |
aa1ee363 | 1437 | int *static_memfuncp, struct type *type) |
c906108c SS |
1438 | { |
1439 | int i; | |
f23631e4 | 1440 | struct value *v; |
c906108c SS |
1441 | int name_matched = 0; |
1442 | char dem_opname[64]; | |
1443 | ||
1444 | CHECK_TYPEDEF (type); | |
1445 | for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--) | |
1446 | { | |
1447 | char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i); | |
1448 | /* FIXME! May need to check for ARM demangling here */ | |
c5aa993b JM |
1449 | if (strncmp (t_field_name, "__", 2) == 0 || |
1450 | strncmp (t_field_name, "op", 2) == 0 || | |
1451 | strncmp (t_field_name, "type", 4) == 0) | |
c906108c | 1452 | { |
c5aa993b JM |
1453 | if (cplus_demangle_opname (t_field_name, dem_opname, DMGL_ANSI)) |
1454 | t_field_name = dem_opname; | |
1455 | else if (cplus_demangle_opname (t_field_name, dem_opname, 0)) | |
c906108c | 1456 | t_field_name = dem_opname; |
c906108c | 1457 | } |
db577aea | 1458 | if (t_field_name && (strcmp_iw (t_field_name, name) == 0)) |
c906108c SS |
1459 | { |
1460 | int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1; | |
1461 | struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i); | |
c5aa993b | 1462 | name_matched = 1; |
c906108c | 1463 | |
de17c821 | 1464 | check_stub_method_group (type, i); |
c906108c SS |
1465 | if (j > 0 && args == 0) |
1466 | error ("cannot resolve overloaded method `%s': no arguments supplied", name); | |
acf5ed49 | 1467 | else if (j == 0 && args == 0) |
c906108c | 1468 | { |
acf5ed49 DJ |
1469 | v = value_fn_field (arg1p, f, j, type, offset); |
1470 | if (v != NULL) | |
1471 | return v; | |
c906108c | 1472 | } |
acf5ed49 DJ |
1473 | else |
1474 | while (j >= 0) | |
1475 | { | |
acf5ed49 | 1476 | if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j), |
ad2f7632 DJ |
1477 | TYPE_VARARGS (TYPE_FN_FIELD_TYPE (f, j)), |
1478 | TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f, j)), | |
acf5ed49 DJ |
1479 | TYPE_FN_FIELD_ARGS (f, j), args)) |
1480 | { | |
1481 | if (TYPE_FN_FIELD_VIRTUAL_P (f, j)) | |
1482 | return value_virtual_fn_field (arg1p, f, j, type, offset); | |
1483 | if (TYPE_FN_FIELD_STATIC_P (f, j) && static_memfuncp) | |
1484 | *static_memfuncp = 1; | |
1485 | v = value_fn_field (arg1p, f, j, type, offset); | |
1486 | if (v != NULL) | |
1487 | return v; | |
1488 | } | |
1489 | j--; | |
1490 | } | |
c906108c SS |
1491 | } |
1492 | } | |
1493 | ||
1494 | for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--) | |
1495 | { | |
1496 | int base_offset; | |
1497 | ||
1498 | if (BASETYPE_VIA_VIRTUAL (type, i)) | |
1499 | { | |
c5aa993b JM |
1500 | if (TYPE_HAS_VTABLE (type)) |
1501 | { | |
1502 | /* HP aCC compiled type, search for virtual base offset | |
7b83ea04 | 1503 | according to HP/Taligent runtime spec. */ |
c5aa993b JM |
1504 | int skip; |
1505 | find_rt_vbase_offset (type, TYPE_BASECLASS (type, i), | |
46615f07 | 1506 | value_contents_all (*arg1p), |
13c3b5f5 | 1507 | offset + value_embedded_offset (*arg1p), |
c5aa993b JM |
1508 | &base_offset, &skip); |
1509 | if (skip >= 0) | |
1510 | error ("Virtual base class offset not found in vtable"); | |
1511 | } | |
1512 | else | |
1513 | { | |
1514 | struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i)); | |
0fd88904 | 1515 | const bfd_byte *base_valaddr; |
c5aa993b JM |
1516 | |
1517 | /* The virtual base class pointer might have been clobbered by the | |
7b83ea04 AC |
1518 | user program. Make sure that it still points to a valid memory |
1519 | location. */ | |
c5aa993b JM |
1520 | |
1521 | if (offset < 0 || offset >= TYPE_LENGTH (type)) | |
1522 | { | |
0fd88904 | 1523 | bfd_byte *tmp = alloca (TYPE_LENGTH (baseclass)); |
c5aa993b | 1524 | if (target_read_memory (VALUE_ADDRESS (*arg1p) |
df407dfe | 1525 | + value_offset (*arg1p) + offset, |
0fd88904 | 1526 | tmp, TYPE_LENGTH (baseclass)) != 0) |
c5aa993b | 1527 | error ("virtual baseclass botch"); |
0fd88904 | 1528 | base_valaddr = tmp; |
c5aa993b JM |
1529 | } |
1530 | else | |
0fd88904 | 1531 | base_valaddr = value_contents (*arg1p) + offset; |
c5aa993b JM |
1532 | |
1533 | base_offset = | |
1534 | baseclass_offset (type, i, base_valaddr, | |
1535 | VALUE_ADDRESS (*arg1p) | |
df407dfe | 1536 | + value_offset (*arg1p) + offset); |
c5aa993b JM |
1537 | if (base_offset == -1) |
1538 | error ("virtual baseclass botch"); | |
1539 | } | |
1540 | } | |
c906108c SS |
1541 | else |
1542 | { | |
1543 | base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8; | |
c5aa993b | 1544 | } |
c906108c SS |
1545 | v = search_struct_method (name, arg1p, args, base_offset + offset, |
1546 | static_memfuncp, TYPE_BASECLASS (type, i)); | |
f23631e4 | 1547 | if (v == (struct value *) - 1) |
c906108c SS |
1548 | { |
1549 | name_matched = 1; | |
1550 | } | |
1551 | else if (v) | |
1552 | { | |
1553 | /* FIXME-bothner: Why is this commented out? Why is it here? */ | |
c5aa993b | 1554 | /* *arg1p = arg1_tmp; */ |
c906108c | 1555 | return v; |
c5aa993b | 1556 | } |
c906108c | 1557 | } |
c5aa993b | 1558 | if (name_matched) |
f23631e4 | 1559 | return (struct value *) - 1; |
c5aa993b JM |
1560 | else |
1561 | return NULL; | |
c906108c SS |
1562 | } |
1563 | ||
1564 | /* Given *ARGP, a value of type (pointer to a)* structure/union, | |
1565 | extract the component named NAME from the ultimate target structure/union | |
1566 | and return it as a value with its appropriate type. | |
1567 | ERR is used in the error message if *ARGP's type is wrong. | |
1568 | ||
1569 | C++: ARGS is a list of argument types to aid in the selection of | |
1570 | an appropriate method. Also, handle derived types. | |
1571 | ||
1572 | STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location | |
1573 | where the truthvalue of whether the function that was resolved was | |
1574 | a static member function or not is stored. | |
1575 | ||
1576 | ERR is an error message to be printed in case the field is not found. */ | |
1577 | ||
f23631e4 AC |
1578 | struct value * |
1579 | value_struct_elt (struct value **argp, struct value **args, | |
fba45db2 | 1580 | char *name, int *static_memfuncp, char *err) |
c906108c | 1581 | { |
52f0bd74 | 1582 | struct type *t; |
f23631e4 | 1583 | struct value *v; |
c906108c | 1584 | |
994b9211 | 1585 | *argp = coerce_array (*argp); |
c906108c | 1586 | |
df407dfe | 1587 | t = check_typedef (value_type (*argp)); |
c906108c SS |
1588 | |
1589 | /* Follow pointers until we get to a non-pointer. */ | |
1590 | ||
1591 | while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF) | |
1592 | { | |
1593 | *argp = value_ind (*argp); | |
1594 | /* Don't coerce fn pointer to fn and then back again! */ | |
df407dfe | 1595 | if (TYPE_CODE (value_type (*argp)) != TYPE_CODE_FUNC) |
994b9211 | 1596 | *argp = coerce_array (*argp); |
df407dfe | 1597 | t = check_typedef (value_type (*argp)); |
c906108c SS |
1598 | } |
1599 | ||
1600 | if (TYPE_CODE (t) == TYPE_CODE_MEMBER) | |
1601 | error ("not implemented: member type in value_struct_elt"); | |
1602 | ||
c5aa993b | 1603 | if (TYPE_CODE (t) != TYPE_CODE_STRUCT |
c906108c SS |
1604 | && TYPE_CODE (t) != TYPE_CODE_UNION) |
1605 | error ("Attempt to extract a component of a value that is not a %s.", err); | |
1606 | ||
1607 | /* Assume it's not, unless we see that it is. */ | |
1608 | if (static_memfuncp) | |
c5aa993b | 1609 | *static_memfuncp = 0; |
c906108c SS |
1610 | |
1611 | if (!args) | |
1612 | { | |
1613 | /* if there are no arguments ...do this... */ | |
1614 | ||
1615 | /* Try as a field first, because if we succeed, there | |
7b83ea04 | 1616 | is less work to be done. */ |
c906108c SS |
1617 | v = search_struct_field (name, *argp, 0, t, 0); |
1618 | if (v) | |
1619 | return v; | |
1620 | ||
1621 | /* C++: If it was not found as a data field, then try to | |
7b83ea04 | 1622 | return it as a pointer to a method. */ |
c906108c SS |
1623 | |
1624 | if (destructor_name_p (name, t)) | |
1625 | error ("Cannot get value of destructor"); | |
1626 | ||
1627 | v = search_struct_method (name, argp, args, 0, static_memfuncp, t); | |
1628 | ||
f23631e4 | 1629 | if (v == (struct value *) - 1) |
c906108c SS |
1630 | error ("Cannot take address of a method"); |
1631 | else if (v == 0) | |
1632 | { | |
1633 | if (TYPE_NFN_FIELDS (t)) | |
1634 | error ("There is no member or method named %s.", name); | |
1635 | else | |
1636 | error ("There is no member named %s.", name); | |
1637 | } | |
1638 | return v; | |
1639 | } | |
1640 | ||
1641 | if (destructor_name_p (name, t)) | |
1642 | { | |
1643 | if (!args[1]) | |
1644 | { | |
1645 | /* Destructors are a special case. */ | |
1646 | int m_index, f_index; | |
1647 | ||
1648 | v = NULL; | |
1649 | if (get_destructor_fn_field (t, &m_index, &f_index)) | |
1650 | { | |
1651 | v = value_fn_field (NULL, TYPE_FN_FIELDLIST1 (t, m_index), | |
1652 | f_index, NULL, 0); | |
1653 | } | |
1654 | if (v == NULL) | |
1655 | error ("could not find destructor function named %s.", name); | |
1656 | else | |
1657 | return v; | |
1658 | } | |
1659 | else | |
1660 | { | |
1661 | error ("destructor should not have any argument"); | |
1662 | } | |
1663 | } | |
1664 | else | |
1665 | v = search_struct_method (name, argp, args, 0, static_memfuncp, t); | |
7168a814 | 1666 | |
f23631e4 | 1667 | if (v == (struct value *) - 1) |
c906108c | 1668 | { |
7168a814 | 1669 | error ("One of the arguments you tried to pass to %s could not be converted to what the function wants.", name); |
c906108c SS |
1670 | } |
1671 | else if (v == 0) | |
1672 | { | |
1673 | /* See if user tried to invoke data as function. If so, | |
7b83ea04 AC |
1674 | hand it back. If it's not callable (i.e., a pointer to function), |
1675 | gdb should give an error. */ | |
c906108c SS |
1676 | v = search_struct_field (name, *argp, 0, t, 0); |
1677 | } | |
1678 | ||
1679 | if (!v) | |
1680 | error ("Structure has no component named %s.", name); | |
1681 | return v; | |
1682 | } | |
1683 | ||
1684 | /* Search through the methods of an object (and its bases) | |
1685 | * to find a specified method. Return the pointer to the | |
1686 | * fn_field list of overloaded instances. | |
1687 | * Helper function for value_find_oload_list. | |
1688 | * ARGP is a pointer to a pointer to a value (the object) | |
1689 | * METHOD is a string containing the method name | |
1690 | * OFFSET is the offset within the value | |
c906108c SS |
1691 | * TYPE is the assumed type of the object |
1692 | * NUM_FNS is the number of overloaded instances | |
1693 | * BASETYPE is set to the actual type of the subobject where the method is found | |
1694 | * BOFFSET is the offset of the base subobject where the method is found */ | |
1695 | ||
7a292a7a | 1696 | static struct fn_field * |
f23631e4 | 1697 | find_method_list (struct value **argp, char *method, int offset, |
4a1970e4 | 1698 | struct type *type, int *num_fns, |
fba45db2 | 1699 | struct type **basetype, int *boffset) |
c906108c SS |
1700 | { |
1701 | int i; | |
c5aa993b | 1702 | struct fn_field *f; |
c906108c SS |
1703 | CHECK_TYPEDEF (type); |
1704 | ||
1705 | *num_fns = 0; | |
1706 | ||
c5aa993b JM |
1707 | /* First check in object itself */ |
1708 | for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--) | |
c906108c SS |
1709 | { |
1710 | /* pai: FIXME What about operators and type conversions? */ | |
c5aa993b | 1711 | char *fn_field_name = TYPE_FN_FIELDLIST_NAME (type, i); |
db577aea | 1712 | if (fn_field_name && (strcmp_iw (fn_field_name, method) == 0)) |
c5aa993b | 1713 | { |
4a1970e4 DJ |
1714 | int len = TYPE_FN_FIELDLIST_LENGTH (type, i); |
1715 | struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i); | |
4a1970e4 DJ |
1716 | |
1717 | *num_fns = len; | |
c5aa993b JM |
1718 | *basetype = type; |
1719 | *boffset = offset; | |
4a1970e4 | 1720 | |
de17c821 DJ |
1721 | /* Resolve any stub methods. */ |
1722 | check_stub_method_group (type, i); | |
4a1970e4 DJ |
1723 | |
1724 | return f; | |
c5aa993b JM |
1725 | } |
1726 | } | |
1727 | ||
c906108c SS |
1728 | /* Not found in object, check in base subobjects */ |
1729 | for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--) | |
1730 | { | |
1731 | int base_offset; | |
1732 | if (BASETYPE_VIA_VIRTUAL (type, i)) | |
1733 | { | |
c5aa993b JM |
1734 | if (TYPE_HAS_VTABLE (type)) |
1735 | { | |
1736 | /* HP aCC compiled type, search for virtual base offset | |
1737 | * according to HP/Taligent runtime spec. */ | |
1738 | int skip; | |
1739 | find_rt_vbase_offset (type, TYPE_BASECLASS (type, i), | |
46615f07 | 1740 | value_contents_all (*argp), |
13c3b5f5 | 1741 | offset + value_embedded_offset (*argp), |
c5aa993b JM |
1742 | &base_offset, &skip); |
1743 | if (skip >= 0) | |
1744 | error ("Virtual base class offset not found in vtable"); | |
1745 | } | |
1746 | else | |
1747 | { | |
1748 | /* probably g++ runtime model */ | |
df407dfe | 1749 | base_offset = value_offset (*argp) + offset; |
c5aa993b JM |
1750 | base_offset = |
1751 | baseclass_offset (type, i, | |
0fd88904 | 1752 | value_contents (*argp) + base_offset, |
c5aa993b JM |
1753 | VALUE_ADDRESS (*argp) + base_offset); |
1754 | if (base_offset == -1) | |
1755 | error ("virtual baseclass botch"); | |
1756 | } | |
1757 | } | |
1758 | else | |
1759 | /* non-virtual base, simply use bit position from debug info */ | |
c906108c SS |
1760 | { |
1761 | base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8; | |
c5aa993b | 1762 | } |
c906108c | 1763 | f = find_method_list (argp, method, base_offset + offset, |
4a1970e4 DJ |
1764 | TYPE_BASECLASS (type, i), num_fns, basetype, |
1765 | boffset); | |
c906108c | 1766 | if (f) |
c5aa993b | 1767 | return f; |
c906108c | 1768 | } |
c5aa993b | 1769 | return NULL; |
c906108c SS |
1770 | } |
1771 | ||
1772 | /* Return the list of overloaded methods of a specified name. | |
1773 | * ARGP is a pointer to a pointer to a value (the object) | |
1774 | * METHOD is the method name | |
1775 | * OFFSET is the offset within the value contents | |
c906108c SS |
1776 | * NUM_FNS is the number of overloaded instances |
1777 | * BASETYPE is set to the type of the base subobject that defines the method | |
1778 | * BOFFSET is the offset of the base subobject which defines the method */ | |
1779 | ||
1780 | struct fn_field * | |
f23631e4 | 1781 | value_find_oload_method_list (struct value **argp, char *method, int offset, |
4a1970e4 DJ |
1782 | int *num_fns, struct type **basetype, |
1783 | int *boffset) | |
c906108c | 1784 | { |
c5aa993b | 1785 | struct type *t; |
c906108c | 1786 | |
df407dfe | 1787 | t = check_typedef (value_type (*argp)); |
c906108c | 1788 | |
c5aa993b | 1789 | /* code snarfed from value_struct_elt */ |
c906108c SS |
1790 | while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF) |
1791 | { | |
1792 | *argp = value_ind (*argp); | |
1793 | /* Don't coerce fn pointer to fn and then back again! */ | |
df407dfe | 1794 | if (TYPE_CODE (value_type (*argp)) != TYPE_CODE_FUNC) |
994b9211 | 1795 | *argp = coerce_array (*argp); |
df407dfe | 1796 | t = check_typedef (value_type (*argp)); |
c906108c | 1797 | } |
c5aa993b | 1798 | |
c906108c SS |
1799 | if (TYPE_CODE (t) == TYPE_CODE_MEMBER) |
1800 | error ("Not implemented: member type in value_find_oload_lis"); | |
c5aa993b JM |
1801 | |
1802 | if (TYPE_CODE (t) != TYPE_CODE_STRUCT | |
1803 | && TYPE_CODE (t) != TYPE_CODE_UNION) | |
c906108c | 1804 | error ("Attempt to extract a component of a value that is not a struct or union"); |
c5aa993b | 1805 | |
4a1970e4 | 1806 | return find_method_list (argp, method, 0, t, num_fns, basetype, boffset); |
c906108c SS |
1807 | } |
1808 | ||
1809 | /* Given an array of argument types (ARGTYPES) (which includes an | |
1810 | entry for "this" in the case of C++ methods), the number of | |
1811 | arguments NARGS, the NAME of a function whether it's a method or | |
1812 | not (METHOD), and the degree of laxness (LAX) in conforming to | |
1813 | overload resolution rules in ANSI C++, find the best function that | |
1814 | matches on the argument types according to the overload resolution | |
1815 | rules. | |
1816 | ||
1817 | In the case of class methods, the parameter OBJ is an object value | |
1818 | in which to search for overloaded methods. | |
1819 | ||
1820 | In the case of non-method functions, the parameter FSYM is a symbol | |
1821 | corresponding to one of the overloaded functions. | |
1822 | ||
1823 | Return value is an integer: 0 -> good match, 10 -> debugger applied | |
1824 | non-standard coercions, 100 -> incompatible. | |
1825 | ||
1826 | If a method is being searched for, VALP will hold the value. | |
1827 | If a non-method is being searched for, SYMP will hold the symbol for it. | |
1828 | ||
1829 | If a method is being searched for, and it is a static method, | |
1830 | then STATICP will point to a non-zero value. | |
1831 | ||
1832 | Note: This function does *not* check the value of | |
1833 | overload_resolution. Caller must check it to see whether overload | |
1834 | resolution is permitted. | |
c5aa993b | 1835 | */ |
c906108c SS |
1836 | |
1837 | int | |
fba45db2 | 1838 | find_overload_match (struct type **arg_types, int nargs, char *name, int method, |
7f8c9282 | 1839 | int lax, struct value **objp, struct symbol *fsym, |
f23631e4 | 1840 | struct value **valp, struct symbol **symp, int *staticp) |
c906108c | 1841 | { |
7f8c9282 | 1842 | struct value *obj = (objp ? *objp : NULL); |
c5aa993b | 1843 | |
8d577d32 | 1844 | int oload_champ; /* Index of best overloaded function */ |
c5aa993b | 1845 | |
c5aa993b JM |
1846 | struct badness_vector *oload_champ_bv = NULL; /* The measure for the current best match */ |
1847 | ||
f23631e4 | 1848 | struct value *temp = obj; |
c5aa993b JM |
1849 | struct fn_field *fns_ptr = NULL; /* For methods, the list of overloaded methods */ |
1850 | struct symbol **oload_syms = NULL; /* For non-methods, the list of overloaded function symbols */ | |
1851 | int num_fns = 0; /* Number of overloaded instances being considered */ | |
1852 | struct type *basetype = NULL; | |
c906108c | 1853 | int boffset; |
52f0bd74 | 1854 | int ix; |
4a1970e4 | 1855 | int static_offset; |
8d577d32 | 1856 | struct cleanup *old_cleanups = NULL; |
c906108c | 1857 | |
8d577d32 | 1858 | const char *obj_type_name = NULL; |
c5aa993b | 1859 | char *func_name = NULL; |
8d577d32 | 1860 | enum oload_classification match_quality; |
c906108c SS |
1861 | |
1862 | /* Get the list of overloaded methods or functions */ | |
1863 | if (method) | |
1864 | { | |
df407dfe | 1865 | obj_type_name = TYPE_NAME (value_type (obj)); |
c906108c | 1866 | /* Hack: evaluate_subexp_standard often passes in a pointer |
7b83ea04 | 1867 | value rather than the object itself, so try again */ |
c906108c | 1868 | if ((!obj_type_name || !*obj_type_name) && |
df407dfe AC |
1869 | (TYPE_CODE (value_type (obj)) == TYPE_CODE_PTR)) |
1870 | obj_type_name = TYPE_NAME (TYPE_TARGET_TYPE (value_type (obj))); | |
c906108c SS |
1871 | |
1872 | fns_ptr = value_find_oload_method_list (&temp, name, 0, | |
c5aa993b JM |
1873 | &num_fns, |
1874 | &basetype, &boffset); | |
c906108c | 1875 | if (!fns_ptr || !num_fns) |
c5aa993b JM |
1876 | error ("Couldn't find method %s%s%s", |
1877 | obj_type_name, | |
1878 | (obj_type_name && *obj_type_name) ? "::" : "", | |
1879 | name); | |
4a1970e4 DJ |
1880 | /* If we are dealing with stub method types, they should have |
1881 | been resolved by find_method_list via value_find_oload_method_list | |
1882 | above. */ | |
1883 | gdb_assert (TYPE_DOMAIN_TYPE (fns_ptr[0].type) != NULL); | |
8d577d32 DC |
1884 | oload_champ = find_oload_champ (arg_types, nargs, method, num_fns, |
1885 | fns_ptr, oload_syms, &oload_champ_bv); | |
c906108c SS |
1886 | } |
1887 | else | |
1888 | { | |
8d577d32 DC |
1889 | const char *qualified_name = SYMBOL_CPLUS_DEMANGLED_NAME (fsym); |
1890 | func_name = cp_func_name (qualified_name); | |
c906108c | 1891 | |
917317f4 | 1892 | /* If the name is NULL this must be a C-style function. |
7b83ea04 | 1893 | Just return the same symbol. */ |
8d577d32 | 1894 | if (func_name == NULL) |
7b83ea04 | 1895 | { |
917317f4 | 1896 | *symp = fsym; |
7b83ea04 AC |
1897 | return 0; |
1898 | } | |
917317f4 | 1899 | |
8d577d32 DC |
1900 | old_cleanups = make_cleanup (xfree, func_name); |
1901 | make_cleanup (xfree, oload_syms); | |
1902 | make_cleanup (xfree, oload_champ_bv); | |
1903 | ||
1904 | oload_champ = find_oload_champ_namespace (arg_types, nargs, | |
1905 | func_name, | |
1906 | qualified_name, | |
1907 | &oload_syms, | |
1908 | &oload_champ_bv); | |
1909 | } | |
1910 | ||
1911 | /* Check how bad the best match is. */ | |
1912 | ||
1913 | match_quality | |
1914 | = classify_oload_match (oload_champ_bv, nargs, | |
1915 | oload_method_static (method, fns_ptr, | |
1916 | oload_champ)); | |
1917 | ||
1918 | if (match_quality == INCOMPATIBLE) | |
1919 | { | |
1920 | if (method) | |
1921 | error ("Cannot resolve method %s%s%s to any overloaded instance", | |
1922 | obj_type_name, | |
1923 | (obj_type_name && *obj_type_name) ? "::" : "", | |
1924 | name); | |
1925 | else | |
1926 | error ("Cannot resolve function %s to any overloaded instance", | |
1927 | func_name); | |
1928 | } | |
1929 | else if (match_quality == NON_STANDARD) | |
1930 | { | |
1931 | if (method) | |
1932 | warning ("Using non-standard conversion to match method %s%s%s to supplied arguments", | |
1933 | obj_type_name, | |
1934 | (obj_type_name && *obj_type_name) ? "::" : "", | |
1935 | name); | |
1936 | else | |
1937 | warning ("Using non-standard conversion to match function %s to supplied arguments", | |
1938 | func_name); | |
1939 | } | |
1940 | ||
1941 | if (method) | |
1942 | { | |
1943 | if (staticp != NULL) | |
1944 | *staticp = oload_method_static (method, fns_ptr, oload_champ); | |
1945 | if (TYPE_FN_FIELD_VIRTUAL_P (fns_ptr, oload_champ)) | |
1946 | *valp = value_virtual_fn_field (&temp, fns_ptr, oload_champ, basetype, boffset); | |
1947 | else | |
1948 | *valp = value_fn_field (&temp, fns_ptr, oload_champ, basetype, boffset); | |
1949 | } | |
1950 | else | |
1951 | { | |
1952 | *symp = oload_syms[oload_champ]; | |
1953 | } | |
1954 | ||
1955 | if (objp) | |
1956 | { | |
df407dfe AC |
1957 | if (TYPE_CODE (value_type (temp)) != TYPE_CODE_PTR |
1958 | && TYPE_CODE (value_type (*objp)) == TYPE_CODE_PTR) | |
8d577d32 DC |
1959 | { |
1960 | temp = value_addr (temp); | |
1961 | } | |
1962 | *objp = temp; | |
1963 | } | |
1964 | if (old_cleanups != NULL) | |
1965 | do_cleanups (old_cleanups); | |
1966 | ||
1967 | switch (match_quality) | |
1968 | { | |
1969 | case INCOMPATIBLE: | |
1970 | return 100; | |
1971 | case NON_STANDARD: | |
1972 | return 10; | |
1973 | default: /* STANDARD */ | |
1974 | return 0; | |
1975 | } | |
1976 | } | |
1977 | ||
1978 | /* Find the best overload match, searching for FUNC_NAME in namespaces | |
1979 | contained in QUALIFIED_NAME until it either finds a good match or | |
1980 | runs out of namespaces. It stores the overloaded functions in | |
1981 | *OLOAD_SYMS, and the badness vector in *OLOAD_CHAMP_BV. The | |
1982 | calling function is responsible for freeing *OLOAD_SYMS and | |
1983 | *OLOAD_CHAMP_BV. */ | |
1984 | ||
1985 | static int | |
1986 | find_oload_champ_namespace (struct type **arg_types, int nargs, | |
1987 | const char *func_name, | |
1988 | const char *qualified_name, | |
1989 | struct symbol ***oload_syms, | |
1990 | struct badness_vector **oload_champ_bv) | |
1991 | { | |
1992 | int oload_champ; | |
1993 | ||
1994 | find_oload_champ_namespace_loop (arg_types, nargs, | |
1995 | func_name, | |
1996 | qualified_name, 0, | |
1997 | oload_syms, oload_champ_bv, | |
1998 | &oload_champ); | |
1999 | ||
2000 | return oload_champ; | |
2001 | } | |
2002 | ||
2003 | /* Helper function for find_oload_champ_namespace; NAMESPACE_LEN is | |
2004 | how deep we've looked for namespaces, and the champ is stored in | |
2005 | OLOAD_CHAMP. The return value is 1 if the champ is a good one, 0 | |
2006 | if it isn't. | |
2007 | ||
2008 | It is the caller's responsibility to free *OLOAD_SYMS and | |
2009 | *OLOAD_CHAMP_BV. */ | |
2010 | ||
2011 | static int | |
2012 | find_oload_champ_namespace_loop (struct type **arg_types, int nargs, | |
2013 | const char *func_name, | |
2014 | const char *qualified_name, | |
2015 | int namespace_len, | |
2016 | struct symbol ***oload_syms, | |
2017 | struct badness_vector **oload_champ_bv, | |
2018 | int *oload_champ) | |
2019 | { | |
2020 | int next_namespace_len = namespace_len; | |
2021 | int searched_deeper = 0; | |
2022 | int num_fns = 0; | |
2023 | struct cleanup *old_cleanups; | |
2024 | int new_oload_champ; | |
2025 | struct symbol **new_oload_syms; | |
2026 | struct badness_vector *new_oload_champ_bv; | |
2027 | char *new_namespace; | |
2028 | ||
2029 | if (next_namespace_len != 0) | |
2030 | { | |
2031 | gdb_assert (qualified_name[next_namespace_len] == ':'); | |
2032 | next_namespace_len += 2; | |
c906108c | 2033 | } |
8d577d32 DC |
2034 | next_namespace_len |
2035 | += cp_find_first_component (qualified_name + next_namespace_len); | |
2036 | ||
2037 | /* Initialize these to values that can safely be xfree'd. */ | |
2038 | *oload_syms = NULL; | |
2039 | *oload_champ_bv = NULL; | |
c5aa993b | 2040 | |
8d577d32 DC |
2041 | /* First, see if we have a deeper namespace we can search in. If we |
2042 | get a good match there, use it. */ | |
2043 | ||
2044 | if (qualified_name[next_namespace_len] == ':') | |
2045 | { | |
2046 | searched_deeper = 1; | |
2047 | ||
2048 | if (find_oload_champ_namespace_loop (arg_types, nargs, | |
2049 | func_name, qualified_name, | |
2050 | next_namespace_len, | |
2051 | oload_syms, oload_champ_bv, | |
2052 | oload_champ)) | |
2053 | { | |
2054 | return 1; | |
2055 | } | |
2056 | }; | |
2057 | ||
2058 | /* If we reach here, either we're in the deepest namespace or we | |
2059 | didn't find a good match in a deeper namespace. But, in the | |
2060 | latter case, we still have a bad match in a deeper namespace; | |
2061 | note that we might not find any match at all in the current | |
2062 | namespace. (There's always a match in the deepest namespace, | |
2063 | because this overload mechanism only gets called if there's a | |
2064 | function symbol to start off with.) */ | |
2065 | ||
2066 | old_cleanups = make_cleanup (xfree, *oload_syms); | |
2067 | old_cleanups = make_cleanup (xfree, *oload_champ_bv); | |
2068 | new_namespace = alloca (namespace_len + 1); | |
2069 | strncpy (new_namespace, qualified_name, namespace_len); | |
2070 | new_namespace[namespace_len] = '\0'; | |
2071 | new_oload_syms = make_symbol_overload_list (func_name, | |
2072 | new_namespace); | |
2073 | while (new_oload_syms[num_fns]) | |
2074 | ++num_fns; | |
2075 | ||
2076 | new_oload_champ = find_oload_champ (arg_types, nargs, 0, num_fns, | |
2077 | NULL, new_oload_syms, | |
2078 | &new_oload_champ_bv); | |
2079 | ||
2080 | /* Case 1: We found a good match. Free earlier matches (if any), | |
2081 | and return it. Case 2: We didn't find a good match, but we're | |
2082 | not the deepest function. Then go with the bad match that the | |
2083 | deeper function found. Case 3: We found a bad match, and we're | |
2084 | the deepest function. Then return what we found, even though | |
2085 | it's a bad match. */ | |
2086 | ||
2087 | if (new_oload_champ != -1 | |
2088 | && classify_oload_match (new_oload_champ_bv, nargs, 0) == STANDARD) | |
2089 | { | |
2090 | *oload_syms = new_oload_syms; | |
2091 | *oload_champ = new_oload_champ; | |
2092 | *oload_champ_bv = new_oload_champ_bv; | |
2093 | do_cleanups (old_cleanups); | |
2094 | return 1; | |
2095 | } | |
2096 | else if (searched_deeper) | |
2097 | { | |
2098 | xfree (new_oload_syms); | |
2099 | xfree (new_oload_champ_bv); | |
2100 | discard_cleanups (old_cleanups); | |
2101 | return 0; | |
2102 | } | |
2103 | else | |
2104 | { | |
2105 | gdb_assert (new_oload_champ != -1); | |
2106 | *oload_syms = new_oload_syms; | |
2107 | *oload_champ = new_oload_champ; | |
2108 | *oload_champ_bv = new_oload_champ_bv; | |
2109 | discard_cleanups (old_cleanups); | |
2110 | return 0; | |
2111 | } | |
2112 | } | |
2113 | ||
2114 | /* Look for a function to take NARGS args of types ARG_TYPES. Find | |
2115 | the best match from among the overloaded methods or functions | |
2116 | (depending on METHOD) given by FNS_PTR or OLOAD_SYMS, respectively. | |
2117 | The number of methods/functions in the list is given by NUM_FNS. | |
2118 | Return the index of the best match; store an indication of the | |
2119 | quality of the match in OLOAD_CHAMP_BV. | |
2120 | ||
2121 | It is the caller's responsibility to free *OLOAD_CHAMP_BV. */ | |
2122 | ||
2123 | static int | |
2124 | find_oload_champ (struct type **arg_types, int nargs, int method, | |
2125 | int num_fns, struct fn_field *fns_ptr, | |
2126 | struct symbol **oload_syms, | |
2127 | struct badness_vector **oload_champ_bv) | |
2128 | { | |
2129 | int ix; | |
2130 | struct badness_vector *bv; /* A measure of how good an overloaded instance is */ | |
2131 | int oload_champ = -1; /* Index of best overloaded function */ | |
2132 | int oload_ambiguous = 0; /* Current ambiguity state for overload resolution */ | |
2133 | /* 0 => no ambiguity, 1 => two good funcs, 2 => incomparable funcs */ | |
2134 | ||
2135 | *oload_champ_bv = NULL; | |
c906108c | 2136 | |
c5aa993b | 2137 | /* Consider each candidate in turn */ |
c906108c SS |
2138 | for (ix = 0; ix < num_fns; ix++) |
2139 | { | |
8d577d32 DC |
2140 | int jj; |
2141 | int static_offset = oload_method_static (method, fns_ptr, ix); | |
2142 | int nparms; | |
2143 | struct type **parm_types; | |
2144 | ||
db577aea AC |
2145 | if (method) |
2146 | { | |
ad2f7632 | 2147 | nparms = TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (fns_ptr, ix)); |
db577aea AC |
2148 | } |
2149 | else | |
2150 | { | |
2151 | /* If it's not a method, this is the proper place */ | |
2152 | nparms=TYPE_NFIELDS(SYMBOL_TYPE(oload_syms[ix])); | |
2153 | } | |
c906108c | 2154 | |
c5aa993b | 2155 | /* Prepare array of parameter types */ |
c906108c SS |
2156 | parm_types = (struct type **) xmalloc (nparms * (sizeof (struct type *))); |
2157 | for (jj = 0; jj < nparms; jj++) | |
db577aea | 2158 | parm_types[jj] = (method |
ad2f7632 | 2159 | ? (TYPE_FN_FIELD_ARGS (fns_ptr, ix)[jj].type) |
db577aea | 2160 | : TYPE_FIELD_TYPE (SYMBOL_TYPE (oload_syms[ix]), jj)); |
c906108c | 2161 | |
4a1970e4 DJ |
2162 | /* Compare parameter types to supplied argument types. Skip THIS for |
2163 | static methods. */ | |
2164 | bv = rank_function (parm_types, nparms, arg_types + static_offset, | |
2165 | nargs - static_offset); | |
c5aa993b | 2166 | |
8d577d32 | 2167 | if (!*oload_champ_bv) |
c5aa993b | 2168 | { |
8d577d32 | 2169 | *oload_champ_bv = bv; |
c5aa993b | 2170 | oload_champ = 0; |
c5aa993b | 2171 | } |
c906108c | 2172 | else |
c5aa993b | 2173 | /* See whether current candidate is better or worse than previous best */ |
8d577d32 | 2174 | switch (compare_badness (bv, *oload_champ_bv)) |
c5aa993b JM |
2175 | { |
2176 | case 0: | |
2177 | oload_ambiguous = 1; /* top two contenders are equally good */ | |
c5aa993b JM |
2178 | break; |
2179 | case 1: | |
2180 | oload_ambiguous = 2; /* incomparable top contenders */ | |
c5aa993b JM |
2181 | break; |
2182 | case 2: | |
8d577d32 | 2183 | *oload_champ_bv = bv; /* new champion, record details */ |
c5aa993b JM |
2184 | oload_ambiguous = 0; |
2185 | oload_champ = ix; | |
c5aa993b JM |
2186 | break; |
2187 | case 3: | |
2188 | default: | |
2189 | break; | |
2190 | } | |
b8c9b27d | 2191 | xfree (parm_types); |
6b1ba9a0 ND |
2192 | if (overload_debug) |
2193 | { | |
2194 | if (method) | |
2195 | fprintf_filtered (gdb_stderr,"Overloaded method instance %s, # of parms %d\n", fns_ptr[ix].physname, nparms); | |
2196 | else | |
2197 | fprintf_filtered (gdb_stderr,"Overloaded function instance %s # of parms %d\n", SYMBOL_DEMANGLED_NAME (oload_syms[ix]), nparms); | |
4a1970e4 | 2198 | for (jj = 0; jj < nargs - static_offset; jj++) |
6b1ba9a0 ND |
2199 | fprintf_filtered (gdb_stderr,"...Badness @ %d : %d\n", jj, bv->rank[jj]); |
2200 | fprintf_filtered (gdb_stderr,"Overload resolution champion is %d, ambiguous? %d\n", oload_champ, oload_ambiguous); | |
2201 | } | |
c906108c SS |
2202 | } |
2203 | ||
8d577d32 DC |
2204 | return oload_champ; |
2205 | } | |
6b1ba9a0 | 2206 | |
8d577d32 DC |
2207 | /* Return 1 if we're looking at a static method, 0 if we're looking at |
2208 | a non-static method or a function that isn't a method. */ | |
c906108c | 2209 | |
8d577d32 DC |
2210 | static int |
2211 | oload_method_static (int method, struct fn_field *fns_ptr, int index) | |
2212 | { | |
2213 | if (method && TYPE_FN_FIELD_STATIC_P (fns_ptr, index)) | |
2214 | return 1; | |
c906108c | 2215 | else |
8d577d32 DC |
2216 | return 0; |
2217 | } | |
c906108c | 2218 | |
8d577d32 DC |
2219 | /* Check how good an overload match OLOAD_CHAMP_BV represents. */ |
2220 | ||
2221 | static enum oload_classification | |
2222 | classify_oload_match (struct badness_vector *oload_champ_bv, | |
2223 | int nargs, | |
2224 | int static_offset) | |
2225 | { | |
2226 | int ix; | |
2227 | ||
2228 | for (ix = 1; ix <= nargs - static_offset; ix++) | |
7f8c9282 | 2229 | { |
8d577d32 DC |
2230 | if (oload_champ_bv->rank[ix] >= 100) |
2231 | return INCOMPATIBLE; /* truly mismatched types */ | |
2232 | else if (oload_champ_bv->rank[ix] >= 10) | |
2233 | return NON_STANDARD; /* non-standard type conversions needed */ | |
7f8c9282 | 2234 | } |
02f0d45d | 2235 | |
8d577d32 | 2236 | return STANDARD; /* Only standard conversions needed. */ |
c906108c SS |
2237 | } |
2238 | ||
2239 | /* C++: return 1 is NAME is a legitimate name for the destructor | |
2240 | of type TYPE. If TYPE does not have a destructor, or | |
2241 | if NAME is inappropriate for TYPE, an error is signaled. */ | |
2242 | int | |
fba45db2 | 2243 | destructor_name_p (const char *name, const struct type *type) |
c906108c SS |
2244 | { |
2245 | /* destructors are a special case. */ | |
2246 | ||
2247 | if (name[0] == '~') | |
2248 | { | |
2249 | char *dname = type_name_no_tag (type); | |
2250 | char *cp = strchr (dname, '<'); | |
2251 | unsigned int len; | |
2252 | ||
2253 | /* Do not compare the template part for template classes. */ | |
2254 | if (cp == NULL) | |
2255 | len = strlen (dname); | |
2256 | else | |
2257 | len = cp - dname; | |
bf896cb0 | 2258 | if (strlen (name + 1) != len || strncmp (dname, name + 1, len) != 0) |
c906108c SS |
2259 | error ("name of destructor must equal name of class"); |
2260 | else | |
2261 | return 1; | |
2262 | } | |
2263 | return 0; | |
2264 | } | |
2265 | ||
2266 | /* Helper function for check_field: Given TYPE, a structure/union, | |
2267 | return 1 if the component named NAME from the ultimate | |
2268 | target structure/union is defined, otherwise, return 0. */ | |
2269 | ||
2270 | static int | |
aa1ee363 | 2271 | check_field_in (struct type *type, const char *name) |
c906108c | 2272 | { |
52f0bd74 | 2273 | int i; |
c906108c SS |
2274 | |
2275 | for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--) | |
2276 | { | |
2277 | char *t_field_name = TYPE_FIELD_NAME (type, i); | |
db577aea | 2278 | if (t_field_name && (strcmp_iw (t_field_name, name) == 0)) |
c906108c SS |
2279 | return 1; |
2280 | } | |
2281 | ||
2282 | /* C++: If it was not found as a data field, then try to | |
2283 | return it as a pointer to a method. */ | |
2284 | ||
2285 | /* Destructors are a special case. */ | |
2286 | if (destructor_name_p (name, type)) | |
2287 | { | |
2288 | int m_index, f_index; | |
2289 | ||
2290 | return get_destructor_fn_field (type, &m_index, &f_index); | |
2291 | } | |
2292 | ||
2293 | for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i) | |
2294 | { | |
db577aea | 2295 | if (strcmp_iw (TYPE_FN_FIELDLIST_NAME (type, i), name) == 0) |
c906108c SS |
2296 | return 1; |
2297 | } | |
2298 | ||
2299 | for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--) | |
2300 | if (check_field_in (TYPE_BASECLASS (type, i), name)) | |
2301 | return 1; | |
c5aa993b | 2302 | |
c906108c SS |
2303 | return 0; |
2304 | } | |
2305 | ||
2306 | ||
2307 | /* C++: Given ARG1, a value of type (pointer to a)* structure/union, | |
2308 | return 1 if the component named NAME from the ultimate | |
2309 | target structure/union is defined, otherwise, return 0. */ | |
2310 | ||
2311 | int | |
f23631e4 | 2312 | check_field (struct value *arg1, const char *name) |
c906108c | 2313 | { |
52f0bd74 | 2314 | struct type *t; |
c906108c | 2315 | |
994b9211 | 2316 | arg1 = coerce_array (arg1); |
c906108c | 2317 | |
df407dfe | 2318 | t = value_type (arg1); |
c906108c SS |
2319 | |
2320 | /* Follow pointers until we get to a non-pointer. */ | |
2321 | ||
2322 | for (;;) | |
2323 | { | |
2324 | CHECK_TYPEDEF (t); | |
2325 | if (TYPE_CODE (t) != TYPE_CODE_PTR && TYPE_CODE (t) != TYPE_CODE_REF) | |
2326 | break; | |
2327 | t = TYPE_TARGET_TYPE (t); | |
2328 | } | |
2329 | ||
2330 | if (TYPE_CODE (t) == TYPE_CODE_MEMBER) | |
2331 | error ("not implemented: member type in check_field"); | |
2332 | ||
c5aa993b | 2333 | if (TYPE_CODE (t) != TYPE_CODE_STRUCT |
c906108c SS |
2334 | && TYPE_CODE (t) != TYPE_CODE_UNION) |
2335 | error ("Internal error: `this' is not an aggregate"); | |
2336 | ||
2337 | return check_field_in (t, name); | |
2338 | } | |
2339 | ||
79c2c32d DC |
2340 | /* C++: Given an aggregate type CURTYPE, and a member name NAME, |
2341 | return the appropriate member. This function is used to resolve | |
2342 | user expressions of the form "DOMAIN::NAME". For more details on | |
2343 | what happens, see the comment before | |
2344 | value_struct_elt_for_reference. */ | |
2345 | ||
2346 | struct value * | |
2347 | value_aggregate_elt (struct type *curtype, | |
2348 | char *name, | |
2349 | enum noside noside) | |
2350 | { | |
2351 | switch (TYPE_CODE (curtype)) | |
2352 | { | |
2353 | case TYPE_CODE_STRUCT: | |
2354 | case TYPE_CODE_UNION: | |
63d06c5c DC |
2355 | return value_struct_elt_for_reference (curtype, 0, curtype, name, NULL, |
2356 | noside); | |
79c2c32d DC |
2357 | case TYPE_CODE_NAMESPACE: |
2358 | return value_namespace_elt (curtype, name, noside); | |
2359 | default: | |
2360 | internal_error (__FILE__, __LINE__, | |
2361 | "non-aggregate type in value_aggregate_elt"); | |
2362 | } | |
2363 | } | |
2364 | ||
c906108c SS |
2365 | /* C++: Given an aggregate type CURTYPE, and a member name NAME, |
2366 | return the address of this member as a "pointer to member" | |
2367 | type. If INTYPE is non-null, then it will be the type | |
2368 | of the member we are looking for. This will help us resolve | |
2369 | "pointers to member functions". This function is used | |
2370 | to resolve user expressions of the form "DOMAIN::NAME". */ | |
2371 | ||
63d06c5c | 2372 | static struct value * |
fba45db2 KB |
2373 | value_struct_elt_for_reference (struct type *domain, int offset, |
2374 | struct type *curtype, char *name, | |
63d06c5c DC |
2375 | struct type *intype, |
2376 | enum noside noside) | |
c906108c | 2377 | { |
52f0bd74 AC |
2378 | struct type *t = curtype; |
2379 | int i; | |
f23631e4 | 2380 | struct value *v; |
c906108c | 2381 | |
c5aa993b | 2382 | if (TYPE_CODE (t) != TYPE_CODE_STRUCT |
c906108c SS |
2383 | && TYPE_CODE (t) != TYPE_CODE_UNION) |
2384 | error ("Internal error: non-aggregate type to value_struct_elt_for_reference"); | |
2385 | ||
2386 | for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--) | |
2387 | { | |
2388 | char *t_field_name = TYPE_FIELD_NAME (t, i); | |
c5aa993b | 2389 | |
6314a349 | 2390 | if (t_field_name && strcmp (t_field_name, name) == 0) |
c906108c SS |
2391 | { |
2392 | if (TYPE_FIELD_STATIC (t, i)) | |
2393 | { | |
2394 | v = value_static_field (t, i); | |
2395 | if (v == NULL) | |
2c2738a0 | 2396 | error ("static field %s has been optimized out", |
c906108c SS |
2397 | name); |
2398 | return v; | |
2399 | } | |
2400 | if (TYPE_FIELD_PACKED (t, i)) | |
2401 | error ("pointers to bitfield members not allowed"); | |
c5aa993b | 2402 | |
c906108c SS |
2403 | return value_from_longest |
2404 | (lookup_reference_type (lookup_member_type (TYPE_FIELD_TYPE (t, i), | |
2405 | domain)), | |
2406 | offset + (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3)); | |
2407 | } | |
2408 | } | |
2409 | ||
2410 | /* C++: If it was not found as a data field, then try to | |
2411 | return it as a pointer to a method. */ | |
2412 | ||
2413 | /* Destructors are a special case. */ | |
2414 | if (destructor_name_p (name, t)) | |
2415 | { | |
2416 | error ("member pointers to destructors not implemented yet"); | |
2417 | } | |
2418 | ||
2419 | /* Perform all necessary dereferencing. */ | |
2420 | while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR) | |
2421 | intype = TYPE_TARGET_TYPE (intype); | |
2422 | ||
2423 | for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i) | |
2424 | { | |
2425 | char *t_field_name = TYPE_FN_FIELDLIST_NAME (t, i); | |
2426 | char dem_opname[64]; | |
2427 | ||
c5aa993b JM |
2428 | if (strncmp (t_field_name, "__", 2) == 0 || |
2429 | strncmp (t_field_name, "op", 2) == 0 || | |
2430 | strncmp (t_field_name, "type", 4) == 0) | |
c906108c | 2431 | { |
c5aa993b JM |
2432 | if (cplus_demangle_opname (t_field_name, dem_opname, DMGL_ANSI)) |
2433 | t_field_name = dem_opname; | |
2434 | else if (cplus_demangle_opname (t_field_name, dem_opname, 0)) | |
c906108c | 2435 | t_field_name = dem_opname; |
c906108c | 2436 | } |
6314a349 | 2437 | if (t_field_name && strcmp (t_field_name, name) == 0) |
c906108c SS |
2438 | { |
2439 | int j = TYPE_FN_FIELDLIST_LENGTH (t, i); | |
2440 | struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i); | |
c5aa993b | 2441 | |
de17c821 DJ |
2442 | check_stub_method_group (t, i); |
2443 | ||
c906108c SS |
2444 | if (intype == 0 && j > 1) |
2445 | error ("non-unique member `%s' requires type instantiation", name); | |
2446 | if (intype) | |
2447 | { | |
2448 | while (j--) | |
2449 | if (TYPE_FN_FIELD_TYPE (f, j) == intype) | |
2450 | break; | |
2451 | if (j < 0) | |
2452 | error ("no member function matches that type instantiation"); | |
2453 | } | |
2454 | else | |
2455 | j = 0; | |
c5aa993b | 2456 | |
c906108c SS |
2457 | if (TYPE_FN_FIELD_VIRTUAL_P (f, j)) |
2458 | { | |
2459 | return value_from_longest | |
2460 | (lookup_reference_type | |
2461 | (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j), | |
2462 | domain)), | |
2463 | (LONGEST) METHOD_PTR_FROM_VOFFSET (TYPE_FN_FIELD_VOFFSET (f, j))); | |
2464 | } | |
2465 | else | |
2466 | { | |
2467 | struct symbol *s = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j), | |
176620f1 | 2468 | 0, VAR_DOMAIN, 0, NULL); |
c906108c SS |
2469 | if (s == NULL) |
2470 | { | |
2471 | v = 0; | |
2472 | } | |
2473 | else | |
2474 | { | |
2475 | v = read_var_value (s, 0); | |
2476 | #if 0 | |
2477 | VALUE_TYPE (v) = lookup_reference_type | |
2478 | (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j), | |
2479 | domain)); | |
2480 | #endif | |
2481 | } | |
2482 | return v; | |
2483 | } | |
2484 | } | |
2485 | } | |
2486 | for (i = TYPE_N_BASECLASSES (t) - 1; i >= 0; i--) | |
2487 | { | |
f23631e4 | 2488 | struct value *v; |
c906108c SS |
2489 | int base_offset; |
2490 | ||
2491 | if (BASETYPE_VIA_VIRTUAL (t, i)) | |
2492 | base_offset = 0; | |
2493 | else | |
2494 | base_offset = TYPE_BASECLASS_BITPOS (t, i) / 8; | |
2495 | v = value_struct_elt_for_reference (domain, | |
2496 | offset + base_offset, | |
2497 | TYPE_BASECLASS (t, i), | |
2498 | name, | |
63d06c5c DC |
2499 | intype, |
2500 | noside); | |
c906108c SS |
2501 | if (v) |
2502 | return v; | |
2503 | } | |
63d06c5c DC |
2504 | |
2505 | /* As a last chance, pretend that CURTYPE is a namespace, and look | |
2506 | it up that way; this (frequently) works for types nested inside | |
2507 | classes. */ | |
2508 | ||
2509 | return value_maybe_namespace_elt (curtype, name, noside); | |
c906108c SS |
2510 | } |
2511 | ||
79c2c32d DC |
2512 | /* C++: Return the member NAME of the namespace given by the type |
2513 | CURTYPE. */ | |
2514 | ||
2515 | static struct value * | |
2516 | value_namespace_elt (const struct type *curtype, | |
63d06c5c | 2517 | char *name, |
79c2c32d | 2518 | enum noside noside) |
63d06c5c DC |
2519 | { |
2520 | struct value *retval = value_maybe_namespace_elt (curtype, name, | |
2521 | noside); | |
2522 | ||
2523 | if (retval == NULL) | |
2524 | error ("No symbol \"%s\" in namespace \"%s\".", name, | |
2525 | TYPE_TAG_NAME (curtype)); | |
2526 | ||
2527 | return retval; | |
2528 | } | |
2529 | ||
2530 | /* A helper function used by value_namespace_elt and | |
2531 | value_struct_elt_for_reference. It looks up NAME inside the | |
2532 | context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE | |
2533 | is a class and NAME refers to a type in CURTYPE itself (as opposed | |
2534 | to, say, some base class of CURTYPE). */ | |
2535 | ||
2536 | static struct value * | |
2537 | value_maybe_namespace_elt (const struct type *curtype, | |
2538 | char *name, | |
2539 | enum noside noside) | |
79c2c32d DC |
2540 | { |
2541 | const char *namespace_name = TYPE_TAG_NAME (curtype); | |
2542 | struct symbol *sym; | |
79c2c32d DC |
2543 | |
2544 | sym = cp_lookup_symbol_namespace (namespace_name, name, NULL, | |
2545 | get_selected_block (0), VAR_DOMAIN, | |
2546 | NULL); | |
2547 | ||
2548 | if (sym == NULL) | |
63d06c5c | 2549 | return NULL; |
79c2c32d DC |
2550 | else if ((noside == EVAL_AVOID_SIDE_EFFECTS) |
2551 | && (SYMBOL_CLASS (sym) == LOC_TYPEDEF)) | |
63d06c5c | 2552 | return allocate_value (SYMBOL_TYPE (sym)); |
79c2c32d | 2553 | else |
63d06c5c | 2554 | return value_of_variable (sym, get_selected_block (0)); |
79c2c32d DC |
2555 | } |
2556 | ||
c906108c SS |
2557 | /* Given a pointer value V, find the real (RTTI) type |
2558 | of the object it points to. | |
2559 | Other parameters FULL, TOP, USING_ENC as with value_rtti_type() | |
2560 | and refer to the values computed for the object pointed to. */ | |
2561 | ||
2562 | struct type * | |
f23631e4 | 2563 | value_rtti_target_type (struct value *v, int *full, int *top, int *using_enc) |
c906108c | 2564 | { |
f23631e4 | 2565 | struct value *target; |
c906108c SS |
2566 | |
2567 | target = value_ind (v); | |
2568 | ||
2569 | return value_rtti_type (target, full, top, using_enc); | |
2570 | } | |
2571 | ||
2572 | /* Given a value pointed to by ARGP, check its real run-time type, and | |
2573 | if that is different from the enclosing type, create a new value | |
2574 | using the real run-time type as the enclosing type (and of the same | |
2575 | type as ARGP) and return it, with the embedded offset adjusted to | |
2576 | be the correct offset to the enclosed object | |
2577 | RTYPE is the type, and XFULL, XTOP, and XUSING_ENC are the other | |
2578 | parameters, computed by value_rtti_type(). If these are available, | |
2579 | they can be supplied and a second call to value_rtti_type() is avoided. | |
2580 | (Pass RTYPE == NULL if they're not available */ | |
2581 | ||
f23631e4 AC |
2582 | struct value * |
2583 | value_full_object (struct value *argp, struct type *rtype, int xfull, int xtop, | |
fba45db2 | 2584 | int xusing_enc) |
c906108c | 2585 | { |
c5aa993b | 2586 | struct type *real_type; |
c906108c SS |
2587 | int full = 0; |
2588 | int top = -1; | |
2589 | int using_enc = 0; | |
f23631e4 | 2590 | struct value *new_val; |
c906108c SS |
2591 | |
2592 | if (rtype) | |
2593 | { | |
2594 | real_type = rtype; | |
2595 | full = xfull; | |
2596 | top = xtop; | |
2597 | using_enc = xusing_enc; | |
2598 | } | |
2599 | else | |
2600 | real_type = value_rtti_type (argp, &full, &top, &using_enc); | |
2601 | ||
2602 | /* If no RTTI data, or if object is already complete, do nothing */ | |
4754a64e | 2603 | if (!real_type || real_type == value_enclosing_type (argp)) |
c906108c SS |
2604 | return argp; |
2605 | ||
2606 | /* If we have the full object, but for some reason the enclosing | |
c5aa993b | 2607 | type is wrong, set it *//* pai: FIXME -- sounds iffy */ |
c906108c SS |
2608 | if (full) |
2609 | { | |
2b127877 | 2610 | argp = value_change_enclosing_type (argp, real_type); |
c906108c SS |
2611 | return argp; |
2612 | } | |
2613 | ||
2614 | /* Check if object is in memory */ | |
2615 | if (VALUE_LVAL (argp) != lval_memory) | |
2616 | { | |
2617 | warning ("Couldn't retrieve complete object of RTTI type %s; object may be in register(s).", TYPE_NAME (real_type)); | |
c5aa993b | 2618 | |
c906108c SS |
2619 | return argp; |
2620 | } | |
c5aa993b | 2621 | |
c906108c SS |
2622 | /* All other cases -- retrieve the complete object */ |
2623 | /* Go back by the computed top_offset from the beginning of the object, | |
2624 | adjusting for the embedded offset of argp if that's what value_rtti_type | |
2625 | used for its computation. */ | |
2626 | new_val = value_at_lazy (real_type, VALUE_ADDRESS (argp) - top + | |
13c3b5f5 | 2627 | (using_enc ? 0 : value_embedded_offset (argp))); |
df407dfe | 2628 | new_val->type = value_type (argp); |
13c3b5f5 AC |
2629 | set_value_embedded_offset (new_val, (using_enc |
2630 | ? top + value_embedded_offset (argp) | |
2631 | : top)); | |
c906108c SS |
2632 | return new_val; |
2633 | } | |
2634 | ||
389e51db AC |
2635 | |
2636 | ||
2637 | ||
d069f99d | 2638 | /* Return the value of the local variable, if one exists. |
c906108c SS |
2639 | Flag COMPLAIN signals an error if the request is made in an |
2640 | inappropriate context. */ | |
2641 | ||
f23631e4 | 2642 | struct value * |
d069f99d | 2643 | value_of_local (const char *name, int complain) |
c906108c SS |
2644 | { |
2645 | struct symbol *func, *sym; | |
2646 | struct block *b; | |
d069f99d | 2647 | struct value * ret; |
c906108c | 2648 | |
6e7f8b9c | 2649 | if (deprecated_selected_frame == 0) |
c906108c SS |
2650 | { |
2651 | if (complain) | |
c5aa993b JM |
2652 | error ("no frame selected"); |
2653 | else | |
2654 | return 0; | |
c906108c SS |
2655 | } |
2656 | ||
6e7f8b9c | 2657 | func = get_frame_function (deprecated_selected_frame); |
c906108c SS |
2658 | if (!func) |
2659 | { | |
2660 | if (complain) | |
2625d86c | 2661 | error ("no `%s' in nameless context", name); |
c5aa993b JM |
2662 | else |
2663 | return 0; | |
c906108c SS |
2664 | } |
2665 | ||
2666 | b = SYMBOL_BLOCK_VALUE (func); | |
de4f826b | 2667 | if (dict_empty (BLOCK_DICT (b))) |
c906108c SS |
2668 | { |
2669 | if (complain) | |
2625d86c | 2670 | error ("no args, no `%s'", name); |
c5aa993b JM |
2671 | else |
2672 | return 0; | |
c906108c SS |
2673 | } |
2674 | ||
2675 | /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER | |
2676 | symbol instead of the LOC_ARG one (if both exist). */ | |
176620f1 | 2677 | sym = lookup_block_symbol (b, name, NULL, VAR_DOMAIN); |
c906108c SS |
2678 | if (sym == NULL) |
2679 | { | |
2680 | if (complain) | |
2625d86c | 2681 | error ("current stack frame does not contain a variable named `%s'", name); |
c906108c SS |
2682 | else |
2683 | return NULL; | |
2684 | } | |
2685 | ||
6e7f8b9c | 2686 | ret = read_var_value (sym, deprecated_selected_frame); |
d069f99d | 2687 | if (ret == 0 && complain) |
2625d86c | 2688 | error ("`%s' argument unreadable", name); |
d069f99d AF |
2689 | return ret; |
2690 | } | |
2691 | ||
2692 | /* C++/Objective-C: return the value of the class instance variable, | |
2693 | if one exists. Flag COMPLAIN signals an error if the request is | |
2694 | made in an inappropriate context. */ | |
2695 | ||
2696 | struct value * | |
2697 | value_of_this (int complain) | |
2698 | { | |
2699 | if (current_language->la_language == language_objc) | |
2700 | return value_of_local ("self", complain); | |
2701 | else | |
2702 | return value_of_local ("this", complain); | |
c906108c SS |
2703 | } |
2704 | ||
2705 | /* Create a slice (sub-string, sub-array) of ARRAY, that is LENGTH elements | |
2706 | long, starting at LOWBOUND. The result has the same lower bound as | |
2707 | the original ARRAY. */ | |
2708 | ||
f23631e4 AC |
2709 | struct value * |
2710 | value_slice (struct value *array, int lowbound, int length) | |
c906108c SS |
2711 | { |
2712 | struct type *slice_range_type, *slice_type, *range_type; | |
7a67d0fe | 2713 | LONGEST lowerbound, upperbound; |
f23631e4 | 2714 | struct value *slice; |
c906108c | 2715 | struct type *array_type; |
df407dfe | 2716 | array_type = check_typedef (value_type (array)); |
c906108c SS |
2717 | if (TYPE_CODE (array_type) != TYPE_CODE_ARRAY |
2718 | && TYPE_CODE (array_type) != TYPE_CODE_STRING | |
2719 | && TYPE_CODE (array_type) != TYPE_CODE_BITSTRING) | |
2720 | error ("cannot take slice of non-array"); | |
2721 | range_type = TYPE_INDEX_TYPE (array_type); | |
2722 | if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0) | |
2723 | error ("slice from bad array or bitstring"); | |
2724 | if (lowbound < lowerbound || length < 0 | |
db034ac5 | 2725 | || lowbound + length - 1 > upperbound) |
c906108c SS |
2726 | error ("slice out of range"); |
2727 | /* FIXME-type-allocation: need a way to free this type when we are | |
2728 | done with it. */ | |
c5aa993b | 2729 | slice_range_type = create_range_type ((struct type *) NULL, |
c906108c SS |
2730 | TYPE_TARGET_TYPE (range_type), |
2731 | lowbound, lowbound + length - 1); | |
2732 | if (TYPE_CODE (array_type) == TYPE_CODE_BITSTRING) | |
2733 | { | |
2734 | int i; | |
c5aa993b | 2735 | slice_type = create_set_type ((struct type *) NULL, slice_range_type); |
c906108c SS |
2736 | TYPE_CODE (slice_type) = TYPE_CODE_BITSTRING; |
2737 | slice = value_zero (slice_type, not_lval); | |
2738 | for (i = 0; i < length; i++) | |
2739 | { | |
2740 | int element = value_bit_index (array_type, | |
0fd88904 | 2741 | value_contents (array), |
c906108c SS |
2742 | lowbound + i); |
2743 | if (element < 0) | |
2744 | error ("internal error accessing bitstring"); | |
2745 | else if (element > 0) | |
2746 | { | |
2747 | int j = i % TARGET_CHAR_BIT; | |
2748 | if (BITS_BIG_ENDIAN) | |
2749 | j = TARGET_CHAR_BIT - 1 - j; | |
990a07ab | 2750 | value_contents_raw (slice)[i / TARGET_CHAR_BIT] |= (1 << j); |
c906108c SS |
2751 | } |
2752 | } | |
2753 | /* We should set the address, bitssize, and bitspos, so the clice | |
7b83ea04 AC |
2754 | can be used on the LHS, but that may require extensions to |
2755 | value_assign. For now, just leave as a non_lval. FIXME. */ | |
c906108c SS |
2756 | } |
2757 | else | |
2758 | { | |
2759 | struct type *element_type = TYPE_TARGET_TYPE (array_type); | |
7a67d0fe | 2760 | LONGEST offset |
c906108c | 2761 | = (lowbound - lowerbound) * TYPE_LENGTH (check_typedef (element_type)); |
c5aa993b | 2762 | slice_type = create_array_type ((struct type *) NULL, element_type, |
c906108c SS |
2763 | slice_range_type); |
2764 | TYPE_CODE (slice_type) = TYPE_CODE (array_type); | |
2765 | slice = allocate_value (slice_type); | |
d69fe07e | 2766 | if (value_lazy (array)) |
dfa52d88 | 2767 | set_value_lazy (slice, 1); |
c906108c | 2768 | else |
0fd88904 AC |
2769 | memcpy (value_contents_writeable (slice), |
2770 | value_contents (array) + offset, | |
c906108c SS |
2771 | TYPE_LENGTH (slice_type)); |
2772 | if (VALUE_LVAL (array) == lval_internalvar) | |
2773 | VALUE_LVAL (slice) = lval_internalvar_component; | |
2774 | else | |
2775 | VALUE_LVAL (slice) = VALUE_LVAL (array); | |
2776 | VALUE_ADDRESS (slice) = VALUE_ADDRESS (array); | |
65d3800a | 2777 | VALUE_FRAME_ID (slice) = VALUE_FRAME_ID (array); |
df407dfe | 2778 | slice->offset = value_offset (array) + offset; |
c906108c SS |
2779 | } |
2780 | return slice; | |
2781 | } | |
2782 | ||
070ad9f0 DB |
2783 | /* Create a value for a FORTRAN complex number. Currently most of |
2784 | the time values are coerced to COMPLEX*16 (i.e. a complex number | |
2785 | composed of 2 doubles. This really should be a smarter routine | |
2786 | that figures out precision inteligently as opposed to assuming | |
c5aa993b | 2787 | doubles. FIXME: fmb */ |
c906108c | 2788 | |
f23631e4 AC |
2789 | struct value * |
2790 | value_literal_complex (struct value *arg1, struct value *arg2, struct type *type) | |
c906108c | 2791 | { |
f23631e4 | 2792 | struct value *val; |
c906108c SS |
2793 | struct type *real_type = TYPE_TARGET_TYPE (type); |
2794 | ||
2795 | val = allocate_value (type); | |
2796 | arg1 = value_cast (real_type, arg1); | |
2797 | arg2 = value_cast (real_type, arg2); | |
2798 | ||
990a07ab | 2799 | memcpy (value_contents_raw (val), |
0fd88904 | 2800 | value_contents (arg1), TYPE_LENGTH (real_type)); |
990a07ab | 2801 | memcpy (value_contents_raw (val) + TYPE_LENGTH (real_type), |
0fd88904 | 2802 | value_contents (arg2), TYPE_LENGTH (real_type)); |
c906108c SS |
2803 | return val; |
2804 | } | |
2805 | ||
2806 | /* Cast a value into the appropriate complex data type. */ | |
2807 | ||
f23631e4 AC |
2808 | static struct value * |
2809 | cast_into_complex (struct type *type, struct value *val) | |
c906108c SS |
2810 | { |
2811 | struct type *real_type = TYPE_TARGET_TYPE (type); | |
df407dfe | 2812 | if (TYPE_CODE (value_type (val)) == TYPE_CODE_COMPLEX) |
c906108c | 2813 | { |
df407dfe | 2814 | struct type *val_real_type = TYPE_TARGET_TYPE (value_type (val)); |
f23631e4 AC |
2815 | struct value *re_val = allocate_value (val_real_type); |
2816 | struct value *im_val = allocate_value (val_real_type); | |
c906108c | 2817 | |
990a07ab | 2818 | memcpy (value_contents_raw (re_val), |
0fd88904 | 2819 | value_contents (val), TYPE_LENGTH (val_real_type)); |
990a07ab | 2820 | memcpy (value_contents_raw (im_val), |
0fd88904 | 2821 | value_contents (val) + TYPE_LENGTH (val_real_type), |
c5aa993b | 2822 | TYPE_LENGTH (val_real_type)); |
c906108c SS |
2823 | |
2824 | return value_literal_complex (re_val, im_val, type); | |
2825 | } | |
df407dfe AC |
2826 | else if (TYPE_CODE (value_type (val)) == TYPE_CODE_FLT |
2827 | || TYPE_CODE (value_type (val)) == TYPE_CODE_INT) | |
c906108c SS |
2828 | return value_literal_complex (val, value_zero (real_type, not_lval), type); |
2829 | else | |
2830 | error ("cannot cast non-number to complex"); | |
2831 | } | |
2832 | ||
2833 | void | |
fba45db2 | 2834 | _initialize_valops (void) |
c906108c SS |
2835 | { |
2836 | #if 0 | |
cb1a6d5f | 2837 | deprecated_add_show_from_set |
c5aa993b | 2838 | (add_set_cmd ("abandon", class_support, var_boolean, (char *) &auto_abandon, |
c906108c SS |
2839 | "Set automatic abandonment of expressions upon failure.", |
2840 | &setlist), | |
2841 | &showlist); | |
2842 | #endif | |
2843 | ||
cb1a6d5f | 2844 | deprecated_add_show_from_set |
c5aa993b | 2845 | (add_set_cmd ("overload-resolution", class_support, var_boolean, (char *) &overload_resolution, |
c906108c SS |
2846 | "Set overload resolution in evaluating C++ functions.", |
2847 | &setlist), | |
2848 | &showlist); | |
2849 | overload_resolution = 1; | |
c906108c | 2850 | } |