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