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