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