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