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