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