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