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