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