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