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