* gdb.c++/templates.exp: Source templ-info.exp only if
[deliverable/binutils-gdb.git] / gdb / valops.c
1 /* Perform non-arithmetic operations on values, for GDB.
2 Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994
3 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 #include "defs.h"
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "value.h"
25 #include "frame.h"
26 #include "inferior.h"
27 #include "gdbcore.h"
28 #include "target.h"
29 #include "demangle.h"
30 #include "language.h"
31
32 #include <errno.h>
33 #include <string.h>
34
35 /* Local functions. */
36
37 static int typecmp PARAMS ((int staticp, struct type *t1[], value_ptr t2[]));
38
39 static CORE_ADDR find_function_addr PARAMS ((value_ptr, struct type **));
40
41 static CORE_ADDR value_push PARAMS ((CORE_ADDR, value_ptr));
42
43 static value_ptr search_struct_field PARAMS ((char *, value_ptr, int,
44 struct type *, int));
45
46 static value_ptr search_struct_method PARAMS ((char *, value_ptr *,
47 value_ptr *,
48 int, int *, struct type *));
49
50 static int check_field_in PARAMS ((struct type *, const char *));
51
52 static CORE_ADDR allocate_space_in_inferior PARAMS ((int));
53
54 static value_ptr cast_into_complex PARAMS ((struct type *, value_ptr));
55
56 #define VALUE_SUBSTRING_START(VAL) VALUE_FRAME(VAL)
57
58 \f
59 /* Allocate NBYTES of space in the inferior using the inferior's malloc
60 and return a value that is a pointer to the allocated space. */
61
62 static CORE_ADDR
63 allocate_space_in_inferior (len)
64 int len;
65 {
66 register value_ptr val;
67 register struct symbol *sym;
68 struct minimal_symbol *msymbol;
69 struct type *type;
70 value_ptr blocklen;
71 LONGEST maddr;
72
73 /* Find the address of malloc in the inferior. */
74
75 sym = lookup_symbol ("malloc", 0, VAR_NAMESPACE, 0, NULL);
76 if (sym != NULL)
77 {
78 if (SYMBOL_CLASS (sym) != LOC_BLOCK)
79 {
80 error ("\"malloc\" exists in this program but is not a function.");
81 }
82 val = value_of_variable (sym, NULL);
83 }
84 else
85 {
86 msymbol = lookup_minimal_symbol ("malloc", NULL, NULL);
87 if (msymbol != NULL)
88 {
89 type = lookup_pointer_type (builtin_type_char);
90 type = lookup_function_type (type);
91 type = lookup_pointer_type (type);
92 maddr = (LONGEST) SYMBOL_VALUE_ADDRESS (msymbol);
93 val = value_from_longest (type, maddr);
94 }
95 else
96 {
97 error ("evaluation of this expression requires the program to have a function \"malloc\".");
98 }
99 }
100
101 blocklen = value_from_longest (builtin_type_int, (LONGEST) len);
102 val = call_function_by_hand (val, 1, &blocklen);
103 if (value_logical_not (val))
104 {
105 error ("No memory available to program.");
106 }
107 return (value_as_long (val));
108 }
109
110 /* Cast value ARG2 to type TYPE and return as a value.
111 More general than a C cast: accepts any two types of the same length,
112 and if ARG2 is an lvalue it can be cast into anything at all. */
113 /* In C++, casts may change pointer or object representations. */
114
115 value_ptr
116 value_cast (type, arg2)
117 struct type *type;
118 register value_ptr arg2;
119 {
120 register enum type_code code1 = TYPE_CODE (type);
121 register enum type_code code2;
122 register int scalar;
123
124 if (VALUE_TYPE (arg2) == type)
125 return arg2;
126
127 COERCE_REF(arg2);
128
129 /* A cast to an undetermined-length array_type, such as (TYPE [])OBJECT,
130 is treated like a cast to (TYPE [N])OBJECT,
131 where N is sizeof(OBJECT)/sizeof(TYPE). */
132 if (code1 == TYPE_CODE_ARRAY
133 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0
134 && TYPE_ARRAY_UPPER_BOUND_TYPE (type) == BOUND_CANNOT_BE_DETERMINED)
135 {
136 struct type *element_type = TYPE_TARGET_TYPE (type);
137 struct type *range_type = TYPE_INDEX_TYPE (type);
138 int low_bound = TYPE_LOW_BOUND (range_type);
139 int val_length = TYPE_LENGTH (VALUE_TYPE (arg2));
140 int new_length = val_length / TYPE_LENGTH (element_type);
141 if (val_length % TYPE_LENGTH (element_type) != 0)
142 warning("array element type size does not divide object size in cast");
143 range_type = create_range_type ((struct type *) NULL,
144 TYPE_TARGET_TYPE (range_type),
145 low_bound, new_length + low_bound - 1);
146 VALUE_TYPE (arg2) = create_array_type ((struct type *) NULL,
147 element_type, range_type);
148 return arg2;
149 }
150
151 if (current_language->c_style_arrays
152 && (VALUE_REPEATED (arg2)
153 || TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_ARRAY))
154 arg2 = value_coerce_array (arg2);
155
156 if (TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_FUNC)
157 arg2 = value_coerce_function (arg2);
158
159 COERCE_VARYING_ARRAY (arg2);
160
161 code2 = TYPE_CODE (VALUE_TYPE (arg2));
162
163 if (code1 == TYPE_CODE_COMPLEX)
164 return cast_into_complex (type, arg2);
165 if (code1 == TYPE_CODE_BOOL)
166 code1 = TYPE_CODE_INT;
167 if (code2 == TYPE_CODE_BOOL)
168 code2 = TYPE_CODE_INT;
169
170 scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
171 || code2 == TYPE_CODE_ENUM || code2 == TYPE_CODE_RANGE);
172
173 if ( code1 == TYPE_CODE_STRUCT
174 && code2 == TYPE_CODE_STRUCT
175 && TYPE_NAME (type) != 0)
176 {
177 /* Look in the type of the source to see if it contains the
178 type of the target as a superclass. If so, we'll need to
179 offset the object in addition to changing its type. */
180 value_ptr v = search_struct_field (type_name_no_tag (type),
181 arg2, 0, VALUE_TYPE (arg2), 1);
182 if (v)
183 {
184 VALUE_TYPE (v) = type;
185 return v;
186 }
187 }
188 if (code1 == TYPE_CODE_FLT && scalar)
189 return value_from_double (type, value_as_double (arg2));
190 else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM
191 || code1 == TYPE_CODE_RANGE)
192 && (scalar || code2 == TYPE_CODE_PTR))
193 return value_from_longest (type, value_as_long (arg2));
194 else if (TYPE_LENGTH (type) == TYPE_LENGTH (VALUE_TYPE (arg2)))
195 {
196 if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
197 {
198 /* Look in the type of the source to see if it contains the
199 type of the target as a superclass. If so, we'll need to
200 offset the pointer rather than just change its type. */
201 struct type *t1 = TYPE_TARGET_TYPE (type);
202 struct type *t2 = TYPE_TARGET_TYPE (VALUE_TYPE (arg2));
203 if ( TYPE_CODE (t1) == TYPE_CODE_STRUCT
204 && TYPE_CODE (t2) == TYPE_CODE_STRUCT
205 && TYPE_NAME (t1) != 0) /* if name unknown, can't have supercl */
206 {
207 value_ptr v = search_struct_field (type_name_no_tag (t1),
208 value_ind (arg2), 0, t2, 1);
209 if (v)
210 {
211 v = value_addr (v);
212 VALUE_TYPE (v) = type;
213 return v;
214 }
215 }
216 /* No superclass found, just fall through to change ptr type. */
217 }
218 VALUE_TYPE (arg2) = type;
219 return arg2;
220 }
221 else if (chill_varying_type (type))
222 {
223 struct type *range1, *range2, *eltype1, *eltype2;
224 value_ptr val;
225 int count1, count2;
226 char *valaddr, *valaddr_data;
227 if (code2 == TYPE_CODE_BITSTRING)
228 error ("not implemented: converting bitstring to varying type");
229 if ((code2 != TYPE_CODE_ARRAY && code2 != TYPE_CODE_STRING)
230 || (eltype1 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 1)),
231 eltype2 = TYPE_TARGET_TYPE (VALUE_TYPE (arg2)),
232 (TYPE_LENGTH (eltype1) != TYPE_LENGTH (eltype2)
233 /* || TYPE_CODE (eltype1) != TYPE_CODE (eltype2) */ )))
234 error ("Invalid conversion to varying type");
235 range1 = TYPE_FIELD_TYPE (TYPE_FIELD_TYPE (type, 1), 0);
236 range2 = TYPE_FIELD_TYPE (VALUE_TYPE (arg2), 0);
237 count1 = TYPE_HIGH_BOUND (range1) - TYPE_LOW_BOUND (range1) + 1;
238 count2 = TYPE_HIGH_BOUND (range2) - TYPE_LOW_BOUND (range2) + 1;
239 if (count2 > count1)
240 error ("target varying type is too small");
241 val = allocate_value (type);
242 valaddr = VALUE_CONTENTS_RAW (val);
243 valaddr_data = valaddr + TYPE_FIELD_BITPOS (type, 1) / 8;
244 /* Set val's __var_length field to count2. */
245 store_signed_integer (valaddr, TYPE_LENGTH (TYPE_FIELD_TYPE (type, 0)),
246 count2);
247 /* Set the __var_data field to count2 elements copied from arg2. */
248 memcpy (valaddr_data, VALUE_CONTENTS (arg2),
249 count2 * TYPE_LENGTH (eltype2));
250 /* Zero the rest of the __var_data field of val. */
251 memset (valaddr_data + count2 * TYPE_LENGTH (eltype2), '\0',
252 (count1 - count2) * TYPE_LENGTH (eltype2));
253 return val;
254 }
255 else if (VALUE_LVAL (arg2) == lval_memory)
256 {
257 return value_at_lazy (type, VALUE_ADDRESS (arg2) + VALUE_OFFSET (arg2));
258 }
259 else if (code1 == TYPE_CODE_VOID)
260 {
261 return value_zero (builtin_type_void, not_lval);
262 }
263 else
264 {
265 error ("Invalid cast.");
266 return 0;
267 }
268 }
269
270 /* Create a value of type TYPE that is zero, and return it. */
271
272 value_ptr
273 value_zero (type, lv)
274 struct type *type;
275 enum lval_type lv;
276 {
277 register value_ptr val = allocate_value (type);
278
279 memset (VALUE_CONTENTS (val), 0, TYPE_LENGTH (type));
280 VALUE_LVAL (val) = lv;
281
282 return val;
283 }
284
285 /* Return a value with type TYPE located at ADDR.
286
287 Call value_at only if the data needs to be fetched immediately;
288 if we can be 'lazy' and defer the fetch, perhaps indefinately, call
289 value_at_lazy instead. value_at_lazy simply records the address of
290 the data and sets the lazy-evaluation-required flag. The lazy flag
291 is tested in the VALUE_CONTENTS macro, which is used if and when
292 the contents are actually required. */
293
294 value_ptr
295 value_at (type, addr)
296 struct type *type;
297 CORE_ADDR addr;
298 {
299 register value_ptr val;
300
301 if (TYPE_CODE (type) == TYPE_CODE_VOID)
302 error ("Attempt to dereference a generic pointer.");
303
304 val = allocate_value (type);
305
306 read_memory (addr, VALUE_CONTENTS_RAW (val), TYPE_LENGTH (type));
307
308 VALUE_LVAL (val) = lval_memory;
309 VALUE_ADDRESS (val) = addr;
310
311 return val;
312 }
313
314 /* Return a lazy value with type TYPE located at ADDR (cf. value_at). */
315
316 value_ptr
317 value_at_lazy (type, addr)
318 struct type *type;
319 CORE_ADDR addr;
320 {
321 register value_ptr val;
322
323 if (TYPE_CODE (type) == TYPE_CODE_VOID)
324 error ("Attempt to dereference a generic pointer.");
325
326 val = allocate_value (type);
327
328 VALUE_LVAL (val) = lval_memory;
329 VALUE_ADDRESS (val) = addr;
330 VALUE_LAZY (val) = 1;
331
332 return val;
333 }
334
335 /* Called only from the VALUE_CONTENTS macro, if the current data for
336 a variable needs to be loaded into VALUE_CONTENTS(VAL). Fetches the
337 data from the user's process, and clears the lazy flag to indicate
338 that the data in the buffer is valid.
339
340 If the value is zero-length, we avoid calling read_memory, which would
341 abort. We mark the value as fetched anyway -- all 0 bytes of it.
342
343 This function returns a value because it is used in the VALUE_CONTENTS
344 macro as part of an expression, where a void would not work. The
345 value is ignored. */
346
347 int
348 value_fetch_lazy (val)
349 register value_ptr val;
350 {
351 CORE_ADDR addr = VALUE_ADDRESS (val) + VALUE_OFFSET (val);
352
353 if (TYPE_LENGTH (VALUE_TYPE (val)))
354 read_memory (addr, VALUE_CONTENTS_RAW (val),
355 TYPE_LENGTH (VALUE_TYPE (val)));
356 VALUE_LAZY (val) = 0;
357 return 0;
358 }
359
360
361 /* Store the contents of FROMVAL into the location of TOVAL.
362 Return a new value with the location of TOVAL and contents of FROMVAL. */
363
364 value_ptr
365 value_assign (toval, fromval)
366 register value_ptr toval, fromval;
367 {
368 register struct type *type;
369 register value_ptr val;
370 char raw_buffer[MAX_REGISTER_RAW_SIZE];
371 int use_buffer = 0;
372
373 if (!toval->modifiable)
374 error ("Left operand of assignment is not a modifiable lvalue.");
375
376 COERCE_ARRAY (fromval);
377 COERCE_REF (toval);
378
379 type = VALUE_TYPE (toval);
380 if (VALUE_LVAL (toval) != lval_internalvar)
381 fromval = value_cast (type, fromval);
382
383 /* If TOVAL is a special machine register requiring conversion
384 of program values to a special raw format,
385 convert FROMVAL's contents now, with result in `raw_buffer',
386 and set USE_BUFFER to the number of bytes to write. */
387
388 #ifdef REGISTER_CONVERTIBLE
389 if (VALUE_REGNO (toval) >= 0
390 && REGISTER_CONVERTIBLE (VALUE_REGNO (toval)))
391 {
392 int regno = VALUE_REGNO (toval);
393 if (REGISTER_CONVERTIBLE (regno))
394 {
395 REGISTER_CONVERT_TO_RAW (VALUE_TYPE (fromval), regno,
396 VALUE_CONTENTS (fromval), raw_buffer);
397 use_buffer = REGISTER_RAW_SIZE (regno);
398 }
399 }
400 #endif
401
402 switch (VALUE_LVAL (toval))
403 {
404 case lval_internalvar:
405 set_internalvar (VALUE_INTERNALVAR (toval), fromval);
406 break;
407
408 case lval_internalvar_component:
409 set_internalvar_component (VALUE_INTERNALVAR (toval),
410 VALUE_OFFSET (toval),
411 VALUE_BITPOS (toval),
412 VALUE_BITSIZE (toval),
413 fromval);
414 break;
415
416 case lval_memory:
417 if (VALUE_BITSIZE (toval))
418 {
419 char buffer[sizeof (LONGEST)];
420 /* We assume that the argument to read_memory is in units of
421 host chars. FIXME: Is that correct? */
422 int len = (VALUE_BITPOS (toval)
423 + VALUE_BITSIZE (toval)
424 + HOST_CHAR_BIT - 1)
425 / HOST_CHAR_BIT;
426
427 if (len > sizeof (LONGEST))
428 error ("Can't handle bitfields which don't fit in a %d bit word.",
429 sizeof (LONGEST) * HOST_CHAR_BIT);
430
431 read_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
432 buffer, len);
433 modify_field (buffer, value_as_long (fromval),
434 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
435 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
436 buffer, len);
437 }
438 else if (use_buffer)
439 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
440 raw_buffer, use_buffer);
441 else
442 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
443 VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
444 break;
445
446 case lval_register:
447 if (VALUE_BITSIZE (toval))
448 {
449 char buffer[sizeof (LONGEST)];
450 int len = REGISTER_RAW_SIZE (VALUE_REGNO (toval));
451
452 if (len > sizeof (LONGEST))
453 error ("Can't handle bitfields in registers larger than %d bits.",
454 sizeof (LONGEST) * HOST_CHAR_BIT);
455
456 if (VALUE_BITPOS (toval) + VALUE_BITSIZE (toval)
457 > len * HOST_CHAR_BIT)
458 /* Getting this right would involve being very careful about
459 byte order. */
460 error ("\
461 Can't handle bitfield which doesn't fit in a single register.");
462
463 read_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
464 buffer, len);
465 modify_field (buffer, value_as_long (fromval),
466 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
467 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
468 buffer, len);
469 }
470 else if (use_buffer)
471 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
472 raw_buffer, use_buffer);
473 else
474 {
475 /* Do any conversion necessary when storing this type to more
476 than one register. */
477 #ifdef REGISTER_CONVERT_FROM_TYPE
478 memcpy (raw_buffer, VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
479 REGISTER_CONVERT_FROM_TYPE(VALUE_REGNO (toval), type, raw_buffer);
480 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
481 raw_buffer, TYPE_LENGTH (type));
482 #else
483 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
484 VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
485 #endif
486 }
487 /* Assigning to the stack pointer, frame pointer, and other
488 (architecture and calling convention specific) registers may
489 cause the frame cache to be out of date. We just do this
490 on all assignments to registers for simplicity; I doubt the slowdown
491 matters. */
492 reinit_frame_cache ();
493 break;
494
495 case lval_reg_frame_relative:
496 {
497 /* value is stored in a series of registers in the frame
498 specified by the structure. Copy that value out, modify
499 it, and copy it back in. */
500 int amount_to_copy = (VALUE_BITSIZE (toval) ? 1 : TYPE_LENGTH (type));
501 int reg_size = REGISTER_RAW_SIZE (VALUE_FRAME_REGNUM (toval));
502 int byte_offset = VALUE_OFFSET (toval) % reg_size;
503 int reg_offset = VALUE_OFFSET (toval) / reg_size;
504 int amount_copied;
505
506 /* Make the buffer large enough in all cases. */
507 char *buffer = (char *) alloca (amount_to_copy
508 + sizeof (LONGEST)
509 + MAX_REGISTER_RAW_SIZE);
510
511 int regno;
512 struct frame_info *frame;
513
514 /* Figure out which frame this is in currently. */
515 for (frame = get_current_frame ();
516 frame && FRAME_FP (frame) != VALUE_FRAME (toval);
517 frame = get_prev_frame (frame))
518 ;
519
520 if (!frame)
521 error ("Value being assigned to is no longer active.");
522
523 amount_to_copy += (reg_size - amount_to_copy % reg_size);
524
525 /* Copy it out. */
526 for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset,
527 amount_copied = 0);
528 amount_copied < amount_to_copy;
529 amount_copied += reg_size, regno++)
530 {
531 get_saved_register (buffer + amount_copied,
532 (int *)NULL, (CORE_ADDR *)NULL,
533 frame, regno, (enum lval_type *)NULL);
534 }
535
536 /* Modify what needs to be modified. */
537 if (VALUE_BITSIZE (toval))
538 modify_field (buffer + byte_offset,
539 value_as_long (fromval),
540 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
541 else if (use_buffer)
542 memcpy (buffer + byte_offset, raw_buffer, use_buffer);
543 else
544 memcpy (buffer + byte_offset, VALUE_CONTENTS (fromval),
545 TYPE_LENGTH (type));
546
547 /* Copy it back. */
548 for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset,
549 amount_copied = 0);
550 amount_copied < amount_to_copy;
551 amount_copied += reg_size, regno++)
552 {
553 enum lval_type lval;
554 CORE_ADDR addr;
555 int optim;
556
557 /* Just find out where to put it. */
558 get_saved_register ((char *)NULL,
559 &optim, &addr, frame, regno, &lval);
560
561 if (optim)
562 error ("Attempt to assign to a value that was optimized out.");
563 if (lval == lval_memory)
564 write_memory (addr, buffer + amount_copied, reg_size);
565 else if (lval == lval_register)
566 write_register_bytes (addr, buffer + amount_copied, reg_size);
567 else
568 error ("Attempt to assign to an unmodifiable value.");
569 }
570 }
571 break;
572
573
574 default:
575 error ("Left operand of assignment is not an lvalue.");
576 }
577
578 /* Return a value just like TOVAL except with the contents of FROMVAL
579 (except in the case of the type if TOVAL is an internalvar). */
580
581 if (VALUE_LVAL (toval) == lval_internalvar
582 || VALUE_LVAL (toval) == lval_internalvar_component)
583 {
584 type = VALUE_TYPE (fromval);
585 }
586
587 val = allocate_value (type);
588 memcpy (val, toval, VALUE_CONTENTS_RAW (val) - (char *) val);
589 memcpy (VALUE_CONTENTS_RAW (val), VALUE_CONTENTS (fromval),
590 TYPE_LENGTH (type));
591 VALUE_TYPE (val) = type;
592
593 return val;
594 }
595
596 /* Extend a value VAL to COUNT repetitions of its type. */
597
598 value_ptr
599 value_repeat (arg1, count)
600 value_ptr arg1;
601 int count;
602 {
603 register value_ptr val;
604
605 if (VALUE_LVAL (arg1) != lval_memory)
606 error ("Only values in memory can be extended with '@'.");
607 if (count < 1)
608 error ("Invalid number %d of repetitions.", count);
609
610 val = allocate_repeat_value (VALUE_TYPE (arg1), count);
611
612 read_memory (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1),
613 VALUE_CONTENTS_RAW (val),
614 TYPE_LENGTH (VALUE_TYPE (val)) * count);
615 VALUE_LVAL (val) = lval_memory;
616 VALUE_ADDRESS (val) = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1);
617
618 return val;
619 }
620
621 value_ptr
622 value_of_variable (var, b)
623 struct symbol *var;
624 struct block *b;
625 {
626 value_ptr val;
627 struct frame_info *frame;
628
629 if (b == NULL)
630 /* Use selected frame. */
631 frame = NULL;
632 else
633 {
634 frame = block_innermost_frame (b);
635 if (frame == NULL && symbol_read_needs_frame (var))
636 {
637 if (BLOCK_FUNCTION (b) != NULL
638 && SYMBOL_NAME (BLOCK_FUNCTION (b)) != NULL)
639 error ("No frame is currently executing in block %s.",
640 SYMBOL_NAME (BLOCK_FUNCTION (b)));
641 else
642 error ("No frame is currently executing in specified block");
643 }
644 }
645 val = read_var_value (var, frame);
646 if (val == 0)
647 error ("Address of symbol \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var));
648 return val;
649 }
650
651 /* Given a value which is an array, return a value which is a pointer to its
652 first element, regardless of whether or not the array has a nonzero lower
653 bound.
654
655 FIXME: A previous comment here indicated that this routine should be
656 substracting the array's lower bound. It's not clear to me that this
657 is correct. Given an array subscripting operation, it would certainly
658 work to do the adjustment here, essentially computing:
659
660 (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0])
661
662 However I believe a more appropriate and logical place to account for
663 the lower bound is to do so in value_subscript, essentially computing:
664
665 (&array[0] + ((index - lowerbound) * sizeof array[0]))
666
667 As further evidence consider what would happen with operations other
668 than array subscripting, where the caller would get back a value that
669 had an address somewhere before the actual first element of the array,
670 and the information about the lower bound would be lost because of
671 the coercion to pointer type.
672 */
673
674 value_ptr
675 value_coerce_array (arg1)
676 value_ptr arg1;
677 {
678 register struct type *type;
679
680 if (VALUE_LVAL (arg1) != lval_memory)
681 error ("Attempt to take address of value not located in memory.");
682
683 /* Get type of elements. */
684 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_ARRAY
685 || TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRING)
686 type = TYPE_TARGET_TYPE (VALUE_TYPE (arg1));
687 else
688 /* A phony array made by value_repeat.
689 Its type is the type of the elements, not an array type. */
690 type = VALUE_TYPE (arg1);
691
692 return value_from_longest (lookup_pointer_type (type),
693 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
694 }
695
696 /* Given a value which is a function, return a value which is a pointer
697 to it. */
698
699 value_ptr
700 value_coerce_function (arg1)
701 value_ptr arg1;
702 {
703
704 if (VALUE_LVAL (arg1) != lval_memory)
705 error ("Attempt to take address of value not located in memory.");
706
707 return value_from_longest (lookup_pointer_type (VALUE_TYPE (arg1)),
708 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
709 }
710
711 /* Return a pointer value for the object for which ARG1 is the contents. */
712
713 value_ptr
714 value_addr (arg1)
715 value_ptr arg1;
716 {
717 struct type *type = VALUE_TYPE (arg1);
718 if (TYPE_CODE (type) == TYPE_CODE_REF)
719 {
720 /* Copy the value, but change the type from (T&) to (T*).
721 We keep the same location information, which is efficient,
722 and allows &(&X) to get the location containing the reference. */
723 value_ptr arg2 = value_copy (arg1);
724 VALUE_TYPE (arg2) = lookup_pointer_type (TYPE_TARGET_TYPE (type));
725 return arg2;
726 }
727 if (current_language->c_style_arrays
728 && (VALUE_REPEATED (arg1)
729 || TYPE_CODE (type) == TYPE_CODE_ARRAY))
730 return value_coerce_array (arg1);
731 if (TYPE_CODE (type) == TYPE_CODE_FUNC)
732 return value_coerce_function (arg1);
733
734 if (VALUE_LVAL (arg1) != lval_memory)
735 error ("Attempt to take address of value not located in memory.");
736
737 return value_from_longest (lookup_pointer_type (type),
738 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
739 }
740
741 /* Given a value of a pointer type, apply the C unary * operator to it. */
742
743 value_ptr
744 value_ind (arg1)
745 value_ptr arg1;
746 {
747 COERCE_ARRAY (arg1);
748
749 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_MEMBER)
750 error ("not implemented: member types in value_ind");
751
752 /* Allow * on an integer so we can cast it to whatever we want.
753 This returns an int, which seems like the most C-like thing
754 to do. "long long" variables are rare enough that
755 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
756 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT)
757 return value_at (builtin_type_int,
758 (CORE_ADDR) value_as_long (arg1));
759 else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR)
760 return value_at_lazy (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
761 value_as_pointer (arg1));
762 error ("Attempt to take contents of a non-pointer value.");
763 return 0; /* For lint -- never reached */
764 }
765 \f
766 /* Pushing small parts of stack frames. */
767
768 /* Push one word (the size of object that a register holds). */
769
770 CORE_ADDR
771 push_word (sp, word)
772 CORE_ADDR sp;
773 unsigned LONGEST word;
774 {
775 register int len = REGISTER_SIZE;
776 char buffer[MAX_REGISTER_RAW_SIZE];
777
778 store_unsigned_integer (buffer, len, word);
779 #if 1 INNER_THAN 2
780 sp -= len;
781 write_memory (sp, buffer, len);
782 #else /* stack grows upward */
783 write_memory (sp, buffer, len);
784 sp += len;
785 #endif /* stack grows upward */
786
787 return sp;
788 }
789
790 /* Push LEN bytes with data at BUFFER. */
791
792 CORE_ADDR
793 push_bytes (sp, buffer, len)
794 CORE_ADDR sp;
795 char *buffer;
796 int len;
797 {
798 #if 1 INNER_THAN 2
799 sp -= len;
800 write_memory (sp, buffer, len);
801 #else /* stack grows upward */
802 write_memory (sp, buffer, len);
803 sp += len;
804 #endif /* stack grows upward */
805
806 return sp;
807 }
808
809 /* Push onto the stack the specified value VALUE. */
810
811 static CORE_ADDR
812 value_push (sp, arg)
813 register CORE_ADDR sp;
814 value_ptr arg;
815 {
816 register int len = TYPE_LENGTH (VALUE_TYPE (arg));
817
818 #if 1 INNER_THAN 2
819 sp -= len;
820 write_memory (sp, VALUE_CONTENTS (arg), len);
821 #else /* stack grows upward */
822 write_memory (sp, VALUE_CONTENTS (arg), len);
823 sp += len;
824 #endif /* stack grows upward */
825
826 return sp;
827 }
828
829 /* Perform the standard coercions that are specified
830 for arguments to be passed to C functions.
831
832 If PARAM_TYPE is non-NULL, it is the expected parameter type. */
833
834 static value_ptr
835 value_arg_coerce (arg, param_type)
836 value_ptr arg;
837 struct type *param_type;
838 {
839 register struct type *type = param_type ? param_type : VALUE_TYPE (arg);
840
841 switch (TYPE_CODE (type))
842 {
843 case TYPE_CODE_REF:
844 if (TYPE_CODE (VALUE_TYPE (arg)) != TYPE_CODE_REF)
845 {
846 arg = value_addr (arg);
847 VALUE_TYPE (arg) = param_type;
848 return arg;
849 }
850 break;
851 case TYPE_CODE_INT:
852 case TYPE_CODE_CHAR:
853 case TYPE_CODE_BOOL:
854 case TYPE_CODE_ENUM:
855 if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
856 type = builtin_type_int;
857 break;
858 case TYPE_CODE_FLT:
859 if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_double))
860 type = builtin_type_double;
861 break;
862 case TYPE_CODE_FUNC:
863 type = lookup_pointer_type (type);
864 break;
865 }
866
867 #if 1 /* FIXME: This is only a temporary patch. -fnf */
868 if (current_language->c_style_arrays
869 && (VALUE_REPEATED (arg)
870 || TYPE_CODE (VALUE_TYPE (arg)) == TYPE_CODE_ARRAY))
871 arg = value_coerce_array (arg);
872 #endif
873
874 return value_cast (type, arg);
875 }
876
877 /* Determine a function's address and its return type from its value.
878 Calls error() if the function is not valid for calling. */
879
880 static CORE_ADDR
881 find_function_addr (function, retval_type)
882 value_ptr function;
883 struct type **retval_type;
884 {
885 register struct type *ftype = VALUE_TYPE (function);
886 register enum type_code code = TYPE_CODE (ftype);
887 struct type *value_type;
888 CORE_ADDR funaddr;
889
890 /* If it's a member function, just look at the function
891 part of it. */
892
893 /* Determine address to call. */
894 if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD)
895 {
896 funaddr = VALUE_ADDRESS (function);
897 value_type = TYPE_TARGET_TYPE (ftype);
898 }
899 else if (code == TYPE_CODE_PTR)
900 {
901 funaddr = value_as_pointer (function);
902 if (TYPE_CODE (TYPE_TARGET_TYPE (ftype)) == TYPE_CODE_FUNC
903 || TYPE_CODE (TYPE_TARGET_TYPE (ftype)) == TYPE_CODE_METHOD)
904 {
905 #ifdef CONVERT_FROM_FUNC_PTR_ADDR
906 /* FIXME: This is a workaround for the unusual function
907 pointer representation on the RS/6000, see comment
908 in config/rs6000/tm-rs6000.h */
909 funaddr = CONVERT_FROM_FUNC_PTR_ADDR (funaddr);
910 #endif
911 value_type = TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (ftype));
912 }
913 else
914 value_type = builtin_type_int;
915 }
916 else if (code == TYPE_CODE_INT)
917 {
918 /* Handle the case of functions lacking debugging info.
919 Their values are characters since their addresses are char */
920 if (TYPE_LENGTH (ftype) == 1)
921 funaddr = value_as_pointer (value_addr (function));
922 else
923 /* Handle integer used as address of a function. */
924 funaddr = (CORE_ADDR) value_as_long (function);
925
926 value_type = builtin_type_int;
927 }
928 else
929 error ("Invalid data type for function to be called.");
930
931 *retval_type = value_type;
932 return funaddr;
933 }
934
935 #if defined (CALL_DUMMY)
936 /* All this stuff with a dummy frame may seem unnecessarily complicated
937 (why not just save registers in GDB?). The purpose of pushing a dummy
938 frame which looks just like a real frame is so that if you call a
939 function and then hit a breakpoint (get a signal, etc), "backtrace"
940 will look right. Whether the backtrace needs to actually show the
941 stack at the time the inferior function was called is debatable, but
942 it certainly needs to not display garbage. So if you are contemplating
943 making dummy frames be different from normal frames, consider that. */
944
945 /* Perform a function call in the inferior.
946 ARGS is a vector of values of arguments (NARGS of them).
947 FUNCTION is a value, the function to be called.
948 Returns a value representing what the function returned.
949 May fail to return, if a breakpoint or signal is hit
950 during the execution of the function.
951
952 ARGS is modified to contain coerced values. */
953
954 value_ptr
955 call_function_by_hand (function, nargs, args)
956 value_ptr function;
957 int nargs;
958 value_ptr *args;
959 {
960 register CORE_ADDR sp;
961 register int i;
962 CORE_ADDR start_sp;
963 /* CALL_DUMMY is an array of words (REGISTER_SIZE), but each word
964 is in host byte order. Before calling FIX_CALL_DUMMY, we byteswap it
965 and remove any extra bytes which might exist because unsigned LONGEST is
966 bigger than REGISTER_SIZE. */
967 static unsigned LONGEST dummy[] = CALL_DUMMY;
968 char dummy1[REGISTER_SIZE * sizeof dummy / sizeof (unsigned LONGEST)];
969 CORE_ADDR old_sp;
970 struct type *value_type;
971 unsigned char struct_return;
972 CORE_ADDR struct_addr;
973 struct inferior_status inf_status;
974 struct cleanup *old_chain;
975 CORE_ADDR funaddr;
976 int using_gcc;
977 CORE_ADDR real_pc;
978 struct type *ftype = SYMBOL_TYPE (function);
979
980 if (!target_has_execution)
981 noprocess();
982
983 save_inferior_status (&inf_status, 1);
984 old_chain = make_cleanup (restore_inferior_status, &inf_status);
985
986 /* PUSH_DUMMY_FRAME is responsible for saving the inferior registers
987 (and POP_FRAME for restoring them). (At least on most machines)
988 they are saved on the stack in the inferior. */
989 PUSH_DUMMY_FRAME;
990
991 old_sp = sp = read_sp ();
992
993 #if 1 INNER_THAN 2 /* Stack grows down */
994 sp -= sizeof dummy1;
995 start_sp = sp;
996 #else /* Stack grows up */
997 start_sp = sp;
998 sp += sizeof dummy1;
999 #endif
1000
1001 funaddr = find_function_addr (function, &value_type);
1002
1003 {
1004 struct block *b = block_for_pc (funaddr);
1005 /* If compiled without -g, assume GCC. */
1006 using_gcc = b == NULL ? 0 : BLOCK_GCC_COMPILED (b);
1007 }
1008
1009 /* Are we returning a value using a structure return or a normal
1010 value return? */
1011
1012 struct_return = using_struct_return (function, funaddr, value_type,
1013 using_gcc);
1014
1015 /* Create a call sequence customized for this function
1016 and the number of arguments for it. */
1017 for (i = 0; i < sizeof dummy / sizeof (dummy[0]); i++)
1018 store_unsigned_integer (&dummy1[i * REGISTER_SIZE],
1019 REGISTER_SIZE,
1020 (unsigned LONGEST)dummy[i]);
1021
1022 #ifdef GDB_TARGET_IS_HPPA
1023 real_pc = FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, args,
1024 value_type, using_gcc);
1025 #else
1026 FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, args,
1027 value_type, using_gcc);
1028 real_pc = start_sp;
1029 #endif
1030
1031 #if CALL_DUMMY_LOCATION == ON_STACK
1032 write_memory (start_sp, (char *)dummy1, sizeof dummy1);
1033 #endif /* On stack. */
1034
1035 #if CALL_DUMMY_LOCATION == BEFORE_TEXT_END
1036 /* Convex Unix prohibits executing in the stack segment. */
1037 /* Hope there is empty room at the top of the text segment. */
1038 {
1039 extern CORE_ADDR text_end;
1040 static checked = 0;
1041 if (!checked)
1042 for (start_sp = text_end - sizeof dummy1; start_sp < text_end; ++start_sp)
1043 if (read_memory_integer (start_sp, 1) != 0)
1044 error ("text segment full -- no place to put call");
1045 checked = 1;
1046 sp = old_sp;
1047 real_pc = text_end - sizeof dummy1;
1048 write_memory (real_pc, (char *)dummy1, sizeof dummy1);
1049 }
1050 #endif /* Before text_end. */
1051
1052 #if CALL_DUMMY_LOCATION == AFTER_TEXT_END
1053 {
1054 extern CORE_ADDR text_end;
1055 int errcode;
1056 sp = old_sp;
1057 real_pc = text_end;
1058 errcode = target_write_memory (real_pc, (char *)dummy1, sizeof dummy1);
1059 if (errcode != 0)
1060 error ("Cannot write text segment -- call_function failed");
1061 }
1062 #endif /* After text_end. */
1063
1064 #if CALL_DUMMY_LOCATION == AT_ENTRY_POINT
1065 real_pc = funaddr;
1066 #endif /* At entry point. */
1067
1068 #ifdef lint
1069 sp = old_sp; /* It really is used, for some ifdef's... */
1070 #endif
1071
1072 if (nargs < TYPE_NFIELDS (ftype))
1073 error ("too few arguments in function call");
1074
1075 for (i = nargs - 1; i >= 0; i--)
1076 {
1077 struct type *param_type;
1078 if (TYPE_NFIELDS (ftype) > i)
1079 param_type = TYPE_FIELD_TYPE (ftype, i);
1080 else
1081 param_type = 0;
1082 args[i] = value_arg_coerce (args[i], param_type);
1083 }
1084
1085 #if defined (REG_STRUCT_HAS_ADDR)
1086 {
1087 /* This is a machine like the sparc, where we may need to pass a pointer
1088 to the structure, not the structure itself. */
1089 for (i = nargs - 1; i >= 0; i--)
1090 if ((TYPE_CODE (VALUE_TYPE (args[i])) == TYPE_CODE_STRUCT
1091 || TYPE_CODE (VALUE_TYPE (args[i])) == TYPE_CODE_UNION
1092 || TYPE_CODE (VALUE_TYPE (args[i])) == TYPE_CODE_ARRAY
1093 || TYPE_CODE (VALUE_TYPE (args[i])) == TYPE_CODE_STRING)
1094 && REG_STRUCT_HAS_ADDR (using_gcc, VALUE_TYPE (args[i])))
1095 {
1096 CORE_ADDR addr;
1097 int len = TYPE_LENGTH (VALUE_TYPE (args[i]));
1098 #ifdef STACK_ALIGN
1099 int aligned_len = STACK_ALIGN (len);
1100 #else
1101 int aligned_len = len;
1102 #endif
1103 #if !(1 INNER_THAN 2)
1104 /* The stack grows up, so the address of the thing we push
1105 is the stack pointer before we push it. */
1106 addr = sp;
1107 #else
1108 sp -= aligned_len;
1109 #endif
1110 /* Push the structure. */
1111 write_memory (sp, VALUE_CONTENTS (args[i]), len);
1112 #if 1 INNER_THAN 2
1113 /* The stack grows down, so the address of the thing we push
1114 is the stack pointer after we push it. */
1115 addr = sp;
1116 #else
1117 sp += aligned_len;
1118 #endif
1119 /* The value we're going to pass is the address of the thing
1120 we just pushed. */
1121 args[i] = value_from_longest (lookup_pointer_type (value_type),
1122 (LONGEST) addr);
1123 }
1124 }
1125 #endif /* REG_STRUCT_HAS_ADDR. */
1126
1127 /* Reserve space for the return structure to be written on the
1128 stack, if necessary */
1129
1130 if (struct_return)
1131 {
1132 int len = TYPE_LENGTH (value_type);
1133 #ifdef STACK_ALIGN
1134 len = STACK_ALIGN (len);
1135 #endif
1136 #if 1 INNER_THAN 2
1137 sp -= len;
1138 struct_addr = sp;
1139 #else
1140 struct_addr = sp;
1141 sp += len;
1142 #endif
1143 }
1144
1145 #ifdef STACK_ALIGN
1146 /* If stack grows down, we must leave a hole at the top. */
1147 {
1148 int len = 0;
1149
1150 for (i = nargs - 1; i >= 0; i--)
1151 len += TYPE_LENGTH (VALUE_TYPE (args[i]));
1152 #ifdef CALL_DUMMY_STACK_ADJUST
1153 len += CALL_DUMMY_STACK_ADJUST;
1154 #endif
1155 #if 1 INNER_THAN 2
1156 sp -= STACK_ALIGN (len) - len;
1157 #else
1158 sp += STACK_ALIGN (len) - len;
1159 #endif
1160 }
1161 #endif /* STACK_ALIGN */
1162
1163 #ifdef PUSH_ARGUMENTS
1164 PUSH_ARGUMENTS(nargs, args, sp, struct_return, struct_addr);
1165 #else /* !PUSH_ARGUMENTS */
1166 for (i = nargs - 1; i >= 0; i--)
1167 sp = value_push (sp, args[i]);
1168 #endif /* !PUSH_ARGUMENTS */
1169
1170 #ifdef CALL_DUMMY_STACK_ADJUST
1171 #if 1 INNER_THAN 2
1172 sp -= CALL_DUMMY_STACK_ADJUST;
1173 #else
1174 sp += CALL_DUMMY_STACK_ADJUST;
1175 #endif
1176 #endif /* CALL_DUMMY_STACK_ADJUST */
1177
1178 /* Store the address at which the structure is supposed to be
1179 written. Note that this (and the code which reserved the space
1180 above) assumes that gcc was used to compile this function. Since
1181 it doesn't cost us anything but space and if the function is pcc
1182 it will ignore this value, we will make that assumption.
1183
1184 Also note that on some machines (like the sparc) pcc uses a
1185 convention like gcc's. */
1186
1187 if (struct_return)
1188 STORE_STRUCT_RETURN (struct_addr, sp);
1189
1190 /* Write the stack pointer. This is here because the statements above
1191 might fool with it. On SPARC, this write also stores the register
1192 window into the right place in the new stack frame, which otherwise
1193 wouldn't happen. (See store_inferior_registers in sparc-nat.c.) */
1194 write_sp (sp);
1195
1196 {
1197 char retbuf[REGISTER_BYTES];
1198 char *name;
1199 struct symbol *symbol;
1200
1201 name = NULL;
1202 symbol = find_pc_function (funaddr);
1203 if (symbol)
1204 {
1205 name = SYMBOL_SOURCE_NAME (symbol);
1206 }
1207 else
1208 {
1209 /* Try the minimal symbols. */
1210 struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (funaddr);
1211
1212 if (msymbol)
1213 {
1214 name = SYMBOL_SOURCE_NAME (msymbol);
1215 }
1216 }
1217 if (name == NULL)
1218 {
1219 char format[80];
1220 sprintf (format, "at %s", local_hex_format ());
1221 name = alloca (80);
1222 /* FIXME-32x64: assumes funaddr fits in a long. */
1223 sprintf (name, format, (unsigned long) funaddr);
1224 }
1225
1226 /* Execute the stack dummy routine, calling FUNCTION.
1227 When it is done, discard the empty frame
1228 after storing the contents of all regs into retbuf. */
1229 if (run_stack_dummy (real_pc + CALL_DUMMY_START_OFFSET, retbuf))
1230 {
1231 /* We stopped somewhere besides the call dummy. */
1232
1233 /* If we did the cleanups, we would print a spurious error message
1234 (Unable to restore previously selected frame), would write the
1235 registers from the inf_status (which is wrong), and would do other
1236 wrong things (like set stop_bpstat to the wrong thing). */
1237 discard_cleanups (old_chain);
1238 /* Prevent memory leak. */
1239 bpstat_clear (&inf_status.stop_bpstat);
1240
1241 /* The following error message used to say "The expression
1242 which contained the function call has been discarded." It
1243 is a hard concept to explain in a few words. Ideally, GDB
1244 would be able to resume evaluation of the expression when
1245 the function finally is done executing. Perhaps someday
1246 this will be implemented (it would not be easy). */
1247
1248 /* FIXME: Insert a bunch of wrap_here; name can be very long if it's
1249 a C++ name with arguments and stuff. */
1250 error ("\
1251 The program being debugged stopped while in a function called from GDB.\n\
1252 When the function (%s) is done executing, GDB will silently\n\
1253 stop (instead of continuing to evaluate the expression containing\n\
1254 the function call).", name);
1255 }
1256
1257 do_cleanups (old_chain);
1258
1259 /* Figure out the value returned by the function. */
1260 return value_being_returned (value_type, retbuf, struct_return);
1261 }
1262 }
1263 #else /* no CALL_DUMMY. */
1264 value_ptr
1265 call_function_by_hand (function, nargs, args)
1266 value_ptr function;
1267 int nargs;
1268 value_ptr *args;
1269 {
1270 error ("Cannot invoke functions on this machine.");
1271 }
1272 #endif /* no CALL_DUMMY. */
1273
1274 \f
1275 /* Create a value for an array by allocating space in the inferior, copying
1276 the data into that space, and then setting up an array value.
1277
1278 The array bounds are set from LOWBOUND and HIGHBOUND, and the array is
1279 populated from the values passed in ELEMVEC.
1280
1281 The element type of the array is inherited from the type of the
1282 first element, and all elements must have the same size (though we
1283 don't currently enforce any restriction on their types). */
1284
1285 value_ptr
1286 value_array (lowbound, highbound, elemvec)
1287 int lowbound;
1288 int highbound;
1289 value_ptr *elemvec;
1290 {
1291 int nelem;
1292 int idx;
1293 int typelength;
1294 value_ptr val;
1295 struct type *rangetype;
1296 struct type *arraytype;
1297 CORE_ADDR addr;
1298
1299 /* Validate that the bounds are reasonable and that each of the elements
1300 have the same size. */
1301
1302 nelem = highbound - lowbound + 1;
1303 if (nelem <= 0)
1304 {
1305 error ("bad array bounds (%d, %d)", lowbound, highbound);
1306 }
1307 typelength = TYPE_LENGTH (VALUE_TYPE (elemvec[0]));
1308 for (idx = 0; idx < nelem; idx++)
1309 {
1310 if (TYPE_LENGTH (VALUE_TYPE (elemvec[idx])) != typelength)
1311 {
1312 error ("array elements must all be the same size");
1313 }
1314 }
1315
1316 /* Allocate space to store the array in the inferior, and then initialize
1317 it by copying in each element. FIXME: Is it worth it to create a
1318 local buffer in which to collect each value and then write all the
1319 bytes in one operation? */
1320
1321 addr = allocate_space_in_inferior (nelem * typelength);
1322 for (idx = 0; idx < nelem; idx++)
1323 {
1324 write_memory (addr + (idx * typelength), VALUE_CONTENTS (elemvec[idx]),
1325 typelength);
1326 }
1327
1328 /* Create the array type and set up an array value to be evaluated lazily. */
1329
1330 rangetype = create_range_type ((struct type *) NULL, builtin_type_int,
1331 lowbound, highbound);
1332 arraytype = create_array_type ((struct type *) NULL,
1333 VALUE_TYPE (elemvec[0]), rangetype);
1334 val = value_at_lazy (arraytype, addr);
1335 return (val);
1336 }
1337
1338 /* Create a value for a string constant by allocating space in the inferior,
1339 copying the data into that space, and returning the address with type
1340 TYPE_CODE_STRING. PTR points to the string constant data; LEN is number
1341 of characters.
1342 Note that string types are like array of char types with a lower bound of
1343 zero and an upper bound of LEN - 1. Also note that the string may contain
1344 embedded null bytes. */
1345
1346 value_ptr
1347 value_string (ptr, len)
1348 char *ptr;
1349 int len;
1350 {
1351 value_ptr val;
1352 int lowbound = current_language->string_lower_bound;
1353 struct type *rangetype = create_range_type ((struct type *) NULL,
1354 builtin_type_int,
1355 lowbound, len + lowbound - 1);
1356 struct type *stringtype
1357 = create_string_type ((struct type *) NULL, rangetype);
1358 CORE_ADDR addr;
1359
1360 if (current_language->c_style_arrays == 0)
1361 {
1362 val = allocate_value (stringtype);
1363 memcpy (VALUE_CONTENTS_RAW (val), ptr, len);
1364 return val;
1365 }
1366
1367
1368 /* Allocate space to store the string in the inferior, and then
1369 copy LEN bytes from PTR in gdb to that address in the inferior. */
1370
1371 addr = allocate_space_in_inferior (len);
1372 write_memory (addr, ptr, len);
1373
1374 val = value_at_lazy (stringtype, addr);
1375 return (val);
1376 }
1377
1378 value_ptr
1379 value_bitstring (ptr, len)
1380 char *ptr;
1381 int len;
1382 {
1383 value_ptr val;
1384 struct type *domain_type = create_range_type (NULL, builtin_type_int,
1385 0, len - 1);
1386 struct type *type = create_set_type ((struct type*) NULL, domain_type);
1387 TYPE_CODE (type) = TYPE_CODE_BITSTRING;
1388 val = allocate_value (type);
1389 memcpy (VALUE_CONTENTS_RAW (val), ptr, TYPE_LENGTH (type) / TARGET_CHAR_BIT);
1390 return val;
1391 }
1392 \f
1393 /* See if we can pass arguments in T2 to a function which takes arguments
1394 of types T1. Both t1 and t2 are NULL-terminated vectors. If some
1395 arguments need coercion of some sort, then the coerced values are written
1396 into T2. Return value is 0 if the arguments could be matched, or the
1397 position at which they differ if not.
1398
1399 STATICP is nonzero if the T1 argument list came from a
1400 static member function.
1401
1402 For non-static member functions, we ignore the first argument,
1403 which is the type of the instance variable. This is because we want
1404 to handle calls with objects from derived classes. This is not
1405 entirely correct: we should actually check to make sure that a
1406 requested operation is type secure, shouldn't we? FIXME. */
1407
1408 static int
1409 typecmp (staticp, t1, t2)
1410 int staticp;
1411 struct type *t1[];
1412 value_ptr t2[];
1413 {
1414 int i;
1415
1416 if (t2 == 0)
1417 return 1;
1418 if (staticp && t1 == 0)
1419 return t2[1] != 0;
1420 if (t1 == 0)
1421 return 1;
1422 if (TYPE_CODE (t1[0]) == TYPE_CODE_VOID) return 0;
1423 if (t1[!staticp] == 0) return 0;
1424 for (i = !staticp; t1[i] && TYPE_CODE (t1[i]) != TYPE_CODE_VOID; i++)
1425 {
1426 struct type *tt1, *tt2;
1427 if (! t2[i])
1428 return i+1;
1429 tt1 = t1[i];
1430 tt2 = VALUE_TYPE(t2[i]);
1431 if (TYPE_CODE (tt1) == TYPE_CODE_REF
1432 /* We should be doing hairy argument matching, as below. */
1433 && (TYPE_CODE (TYPE_TARGET_TYPE (tt1)) == TYPE_CODE (tt2)))
1434 {
1435 t2[i] = value_addr (t2[i]);
1436 continue;
1437 }
1438
1439 while (TYPE_CODE (tt1) == TYPE_CODE_PTR
1440 && (TYPE_CODE(tt2)==TYPE_CODE_ARRAY || TYPE_CODE(tt2)==TYPE_CODE_PTR))
1441 {
1442 tt1 = TYPE_TARGET_TYPE(tt1);
1443 tt2 = TYPE_TARGET_TYPE(tt2);
1444 }
1445 if (TYPE_CODE(tt1) == TYPE_CODE(tt2)) continue;
1446 /* Array to pointer is a `trivial conversion' according to the ARM. */
1447
1448 /* We should be doing much hairier argument matching (see section 13.2
1449 of the ARM), but as a quick kludge, just check for the same type
1450 code. */
1451 if (TYPE_CODE (t1[i]) != TYPE_CODE (VALUE_TYPE (t2[i])))
1452 return i+1;
1453 }
1454 if (!t1[i]) return 0;
1455 return t2[i] ? i+1 : 0;
1456 }
1457
1458 /* Helper function used by value_struct_elt to recurse through baseclasses.
1459 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1460 and search in it assuming it has (class) type TYPE.
1461 If found, return value, else return NULL.
1462
1463 If LOOKING_FOR_BASECLASS, then instead of looking for struct fields,
1464 look for a baseclass named NAME. */
1465
1466 static value_ptr
1467 search_struct_field (name, arg1, offset, type, looking_for_baseclass)
1468 char *name;
1469 register value_ptr arg1;
1470 int offset;
1471 register struct type *type;
1472 int looking_for_baseclass;
1473 {
1474 int i;
1475
1476 check_stub_type (type);
1477
1478 if (! looking_for_baseclass)
1479 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
1480 {
1481 char *t_field_name = TYPE_FIELD_NAME (type, i);
1482
1483 if (t_field_name && STREQ (t_field_name, name))
1484 {
1485 value_ptr v;
1486 if (TYPE_FIELD_STATIC (type, i))
1487 {
1488 char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, i);
1489 struct symbol *sym =
1490 lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL);
1491 if (sym == NULL)
1492 error ("Internal error: could not find physical static variable named %s",
1493 phys_name);
1494 v = value_at (TYPE_FIELD_TYPE (type, i),
1495 (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym));
1496 }
1497 else
1498 v = value_primitive_field (arg1, offset, i, type);
1499 if (v == 0)
1500 error("there is no field named %s", name);
1501 return v;
1502 }
1503 if (t_field_name && t_field_name[0] == '\0'
1504 && TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_UNION)
1505 {
1506 /* Look for a match through the fields of an anonymous union. */
1507 value_ptr v;
1508 v = search_struct_field (name, arg1, offset,
1509 TYPE_FIELD_TYPE (type, i),
1510 looking_for_baseclass);
1511 if (v)
1512 return v;
1513 }
1514 }
1515
1516 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1517 {
1518 value_ptr v;
1519 /* If we are looking for baseclasses, this is what we get when we
1520 hit them. But it could happen that the base part's member name
1521 is not yet filled in. */
1522 int found_baseclass = (looking_for_baseclass
1523 && TYPE_BASECLASS_NAME (type, i) != NULL
1524 && STREQ (name, TYPE_BASECLASS_NAME (type, i)));
1525
1526 if (BASETYPE_VIA_VIRTUAL (type, i))
1527 {
1528 value_ptr v2;
1529 /* Fix to use baseclass_offset instead. FIXME */
1530 baseclass_addr (type, i, VALUE_CONTENTS (arg1) + offset,
1531 &v2, (int *)NULL);
1532 if (v2 == 0)
1533 error ("virtual baseclass botch");
1534 if (found_baseclass)
1535 return v2;
1536 v = search_struct_field (name, v2, 0, TYPE_BASECLASS (type, i),
1537 looking_for_baseclass);
1538 }
1539 else if (found_baseclass)
1540 v = value_primitive_field (arg1, offset, i, type);
1541 else
1542 v = search_struct_field (name, arg1,
1543 offset + TYPE_BASECLASS_BITPOS (type, i) / 8,
1544 TYPE_BASECLASS (type, i),
1545 looking_for_baseclass);
1546 if (v) return v;
1547 }
1548 return NULL;
1549 }
1550
1551 /* Helper function used by value_struct_elt to recurse through baseclasses.
1552 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1553 and search in it assuming it has (class) type TYPE.
1554 If found, return value, else if name matched and args not return (value)-1,
1555 else return NULL. */
1556
1557 static value_ptr
1558 search_struct_method (name, arg1p, args, offset, static_memfuncp, type)
1559 char *name;
1560 register value_ptr *arg1p, *args;
1561 int offset, *static_memfuncp;
1562 register struct type *type;
1563 {
1564 int i;
1565 value_ptr v;
1566 int name_matched = 0;
1567 char dem_opname[64];
1568
1569 check_stub_type (type);
1570 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
1571 {
1572 char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
1573 if (strncmp(t_field_name, "__", 2)==0 ||
1574 strncmp(t_field_name, "op", 2)==0 ||
1575 strncmp(t_field_name, "type", 4)==0 )
1576 {
1577 if (cplus_demangle_opname(t_field_name, dem_opname, DMGL_ANSI))
1578 t_field_name = dem_opname;
1579 else if (cplus_demangle_opname(t_field_name, dem_opname, 0))
1580 t_field_name = dem_opname;
1581 }
1582 if (t_field_name && STREQ (t_field_name, name))
1583 {
1584 int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1;
1585 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1586 name_matched = 1;
1587
1588 if (j > 0 && args == 0)
1589 error ("cannot resolve overloaded method `%s'", name);
1590 while (j >= 0)
1591 {
1592 if (TYPE_FN_FIELD_STUB (f, j))
1593 check_stub_method (type, i, j);
1594 if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j),
1595 TYPE_FN_FIELD_ARGS (f, j), args))
1596 {
1597 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1598 return value_virtual_fn_field (arg1p, f, j, type, offset);
1599 if (TYPE_FN_FIELD_STATIC_P (f, j) && static_memfuncp)
1600 *static_memfuncp = 1;
1601 v = value_fn_field (arg1p, f, j, type, offset);
1602 if (v != NULL) return v;
1603 }
1604 j--;
1605 }
1606 }
1607 }
1608
1609 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1610 {
1611 int base_offset;
1612
1613 if (BASETYPE_VIA_VIRTUAL (type, i))
1614 {
1615 base_offset = baseclass_offset (type, i, *arg1p, offset);
1616 if (base_offset == -1)
1617 error ("virtual baseclass botch");
1618 }
1619 else
1620 {
1621 base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
1622 }
1623 v = search_struct_method (name, arg1p, args, base_offset + offset,
1624 static_memfuncp, TYPE_BASECLASS (type, i));
1625 if (v == (value_ptr) -1)
1626 {
1627 name_matched = 1;
1628 }
1629 else if (v)
1630 {
1631 /* FIXME-bothner: Why is this commented out? Why is it here? */
1632 /* *arg1p = arg1_tmp;*/
1633 return v;
1634 }
1635 }
1636 if (name_matched) return (value_ptr) -1;
1637 else return NULL;
1638 }
1639
1640 /* Given *ARGP, a value of type (pointer to a)* structure/union,
1641 extract the component named NAME from the ultimate target structure/union
1642 and return it as a value with its appropriate type.
1643 ERR is used in the error message if *ARGP's type is wrong.
1644
1645 C++: ARGS is a list of argument types to aid in the selection of
1646 an appropriate method. Also, handle derived types.
1647
1648 STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
1649 where the truthvalue of whether the function that was resolved was
1650 a static member function or not is stored.
1651
1652 ERR is an error message to be printed in case the field is not found. */
1653
1654 value_ptr
1655 value_struct_elt (argp, args, name, static_memfuncp, err)
1656 register value_ptr *argp, *args;
1657 char *name;
1658 int *static_memfuncp;
1659 char *err;
1660 {
1661 register struct type *t;
1662 value_ptr v;
1663
1664 COERCE_ARRAY (*argp);
1665
1666 t = VALUE_TYPE (*argp);
1667
1668 /* Follow pointers until we get to a non-pointer. */
1669
1670 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1671 {
1672 *argp = value_ind (*argp);
1673 /* Don't coerce fn pointer to fn and then back again! */
1674 if (TYPE_CODE (VALUE_TYPE (*argp)) != TYPE_CODE_FUNC)
1675 COERCE_ARRAY (*argp);
1676 t = VALUE_TYPE (*argp);
1677 }
1678
1679 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1680 error ("not implemented: member type in value_struct_elt");
1681
1682 if ( TYPE_CODE (t) != TYPE_CODE_STRUCT
1683 && TYPE_CODE (t) != TYPE_CODE_UNION)
1684 error ("Attempt to extract a component of a value that is not a %s.", err);
1685
1686 /* Assume it's not, unless we see that it is. */
1687 if (static_memfuncp)
1688 *static_memfuncp =0;
1689
1690 if (!args)
1691 {
1692 /* if there are no arguments ...do this... */
1693
1694 /* Try as a field first, because if we succeed, there
1695 is less work to be done. */
1696 v = search_struct_field (name, *argp, 0, t, 0);
1697 if (v)
1698 return v;
1699
1700 /* C++: If it was not found as a data field, then try to
1701 return it as a pointer to a method. */
1702
1703 if (destructor_name_p (name, t))
1704 error ("Cannot get value of destructor");
1705
1706 v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
1707
1708 if (v == (value_ptr) -1)
1709 error ("Cannot take address of a method");
1710 else if (v == 0)
1711 {
1712 if (TYPE_NFN_FIELDS (t))
1713 error ("There is no member or method named %s.", name);
1714 else
1715 error ("There is no member named %s.", name);
1716 }
1717 return v;
1718 }
1719
1720 if (destructor_name_p (name, t))
1721 {
1722 if (!args[1])
1723 {
1724 /* destructors are a special case. */
1725 v = value_fn_field (NULL, TYPE_FN_FIELDLIST1 (t, 0),
1726 TYPE_FN_FIELDLIST_LENGTH (t, 0), 0, 0);
1727 if (!v) error("could not find destructor function named %s.", name);
1728 else return v;
1729 }
1730 else
1731 {
1732 error ("destructor should not have any argument");
1733 }
1734 }
1735 else
1736 v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
1737
1738 if (v == (value_ptr) -1)
1739 {
1740 error("Argument list of %s mismatch with component in the structure.", name);
1741 }
1742 else if (v == 0)
1743 {
1744 /* See if user tried to invoke data as function. If so,
1745 hand it back. If it's not callable (i.e., a pointer to function),
1746 gdb should give an error. */
1747 v = search_struct_field (name, *argp, 0, t, 0);
1748 }
1749
1750 if (!v)
1751 error ("Structure has no component named %s.", name);
1752 return v;
1753 }
1754
1755 /* C++: return 1 is NAME is a legitimate name for the destructor
1756 of type TYPE. If TYPE does not have a destructor, or
1757 if NAME is inappropriate for TYPE, an error is signaled. */
1758 int
1759 destructor_name_p (name, type)
1760 const char *name;
1761 const struct type *type;
1762 {
1763 /* destructors are a special case. */
1764
1765 if (name[0] == '~')
1766 {
1767 char *dname = type_name_no_tag (type);
1768 char *cp = strchr (dname, '<');
1769 int len;
1770
1771 /* Do not compare the template part for template classes. */
1772 if (cp == NULL)
1773 len = strlen (dname);
1774 else
1775 len = cp - dname;
1776 if (strlen (name + 1) != len || !STREQN (dname, name + 1, len))
1777 error ("name of destructor must equal name of class");
1778 else
1779 return 1;
1780 }
1781 return 0;
1782 }
1783
1784 /* Helper function for check_field: Given TYPE, a structure/union,
1785 return 1 if the component named NAME from the ultimate
1786 target structure/union is defined, otherwise, return 0. */
1787
1788 static int
1789 check_field_in (type, name)
1790 register struct type *type;
1791 const char *name;
1792 {
1793 register int i;
1794
1795 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
1796 {
1797 char *t_field_name = TYPE_FIELD_NAME (type, i);
1798 if (t_field_name && STREQ (t_field_name, name))
1799 return 1;
1800 }
1801
1802 /* C++: If it was not found as a data field, then try to
1803 return it as a pointer to a method. */
1804
1805 /* Destructors are a special case. */
1806 if (destructor_name_p (name, type))
1807 return 1;
1808
1809 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
1810 {
1811 if (STREQ (TYPE_FN_FIELDLIST_NAME (type, i), name))
1812 return 1;
1813 }
1814
1815 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1816 if (check_field_in (TYPE_BASECLASS (type, i), name))
1817 return 1;
1818
1819 return 0;
1820 }
1821
1822
1823 /* C++: Given ARG1, a value of type (pointer to a)* structure/union,
1824 return 1 if the component named NAME from the ultimate
1825 target structure/union is defined, otherwise, return 0. */
1826
1827 int
1828 check_field (arg1, name)
1829 register value_ptr arg1;
1830 const char *name;
1831 {
1832 register struct type *t;
1833
1834 COERCE_ARRAY (arg1);
1835
1836 t = VALUE_TYPE (arg1);
1837
1838 /* Follow pointers until we get to a non-pointer. */
1839
1840 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1841 t = TYPE_TARGET_TYPE (t);
1842
1843 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1844 error ("not implemented: member type in check_field");
1845
1846 if ( TYPE_CODE (t) != TYPE_CODE_STRUCT
1847 && TYPE_CODE (t) != TYPE_CODE_UNION)
1848 error ("Internal error: `this' is not an aggregate");
1849
1850 return check_field_in (t, name);
1851 }
1852
1853 /* C++: Given an aggregate type CURTYPE, and a member name NAME,
1854 return the address of this member as a "pointer to member"
1855 type. If INTYPE is non-null, then it will be the type
1856 of the member we are looking for. This will help us resolve
1857 "pointers to member functions". This function is used
1858 to resolve user expressions of the form "DOMAIN::NAME". */
1859
1860 value_ptr
1861 value_struct_elt_for_reference (domain, offset, curtype, name, intype)
1862 struct type *domain, *curtype, *intype;
1863 int offset;
1864 char *name;
1865 {
1866 register struct type *t = curtype;
1867 register int i;
1868 value_ptr v;
1869
1870 if ( TYPE_CODE (t) != TYPE_CODE_STRUCT
1871 && TYPE_CODE (t) != TYPE_CODE_UNION)
1872 error ("Internal error: non-aggregate type to value_struct_elt_for_reference");
1873
1874 for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
1875 {
1876 char *t_field_name = TYPE_FIELD_NAME (t, i);
1877
1878 if (t_field_name && STREQ (t_field_name, name))
1879 {
1880 if (TYPE_FIELD_STATIC (t, i))
1881 {
1882 char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (t, i);
1883 struct symbol *sym =
1884 lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL);
1885 if (sym == NULL)
1886 error ("Internal error: could not find physical static variable named %s",
1887 phys_name);
1888 return value_at (SYMBOL_TYPE (sym),
1889 (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym));
1890 }
1891 if (TYPE_FIELD_PACKED (t, i))
1892 error ("pointers to bitfield members not allowed");
1893
1894 return value_from_longest
1895 (lookup_reference_type (lookup_member_type (TYPE_FIELD_TYPE (t, i),
1896 domain)),
1897 offset + (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3));
1898 }
1899 }
1900
1901 /* C++: If it was not found as a data field, then try to
1902 return it as a pointer to a method. */
1903
1904 /* Destructors are a special case. */
1905 if (destructor_name_p (name, t))
1906 {
1907 error ("member pointers to destructors not implemented yet");
1908 }
1909
1910 /* Perform all necessary dereferencing. */
1911 while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR)
1912 intype = TYPE_TARGET_TYPE (intype);
1913
1914 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
1915 {
1916 char *t_field_name = TYPE_FN_FIELDLIST_NAME (t, i);
1917 char dem_opname[64];
1918
1919 if (strncmp(t_field_name, "__", 2)==0 ||
1920 strncmp(t_field_name, "op", 2)==0 ||
1921 strncmp(t_field_name, "type", 4)==0 )
1922 {
1923 if (cplus_demangle_opname(t_field_name, dem_opname, DMGL_ANSI))
1924 t_field_name = dem_opname;
1925 else if (cplus_demangle_opname(t_field_name, dem_opname, 0))
1926 t_field_name = dem_opname;
1927 }
1928 if (t_field_name && STREQ (t_field_name, name))
1929 {
1930 int j = TYPE_FN_FIELDLIST_LENGTH (t, i);
1931 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
1932
1933 if (intype == 0 && j > 1)
1934 error ("non-unique member `%s' requires type instantiation", name);
1935 if (intype)
1936 {
1937 while (j--)
1938 if (TYPE_FN_FIELD_TYPE (f, j) == intype)
1939 break;
1940 if (j < 0)
1941 error ("no member function matches that type instantiation");
1942 }
1943 else
1944 j = 0;
1945
1946 if (TYPE_FN_FIELD_STUB (f, j))
1947 check_stub_method (t, i, j);
1948 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1949 {
1950 return value_from_longest
1951 (lookup_reference_type
1952 (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
1953 domain)),
1954 (LONGEST) METHOD_PTR_FROM_VOFFSET (TYPE_FN_FIELD_VOFFSET (f, j)));
1955 }
1956 else
1957 {
1958 struct symbol *s = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
1959 0, VAR_NAMESPACE, 0, NULL);
1960 if (s == NULL)
1961 {
1962 v = 0;
1963 }
1964 else
1965 {
1966 v = read_var_value (s, 0);
1967 #if 0
1968 VALUE_TYPE (v) = lookup_reference_type
1969 (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
1970 domain));
1971 #endif
1972 }
1973 return v;
1974 }
1975 }
1976 }
1977 for (i = TYPE_N_BASECLASSES (t) - 1; i >= 0; i--)
1978 {
1979 value_ptr v;
1980 int base_offset;
1981
1982 if (BASETYPE_VIA_VIRTUAL (t, i))
1983 base_offset = 0;
1984 else
1985 base_offset = TYPE_BASECLASS_BITPOS (t, i) / 8;
1986 v = value_struct_elt_for_reference (domain,
1987 offset + base_offset,
1988 TYPE_BASECLASS (t, i),
1989 name,
1990 intype);
1991 if (v)
1992 return v;
1993 }
1994 return 0;
1995 }
1996
1997 /* C++: return the value of the class instance variable, if one exists.
1998 Flag COMPLAIN signals an error if the request is made in an
1999 inappropriate context. */
2000
2001 value_ptr
2002 value_of_this (complain)
2003 int complain;
2004 {
2005 struct symbol *func, *sym;
2006 struct block *b;
2007 int i;
2008 static const char funny_this[] = "this";
2009 value_ptr this;
2010
2011 if (selected_frame == 0)
2012 if (complain)
2013 error ("no frame selected");
2014 else return 0;
2015
2016 func = get_frame_function (selected_frame);
2017 if (!func)
2018 {
2019 if (complain)
2020 error ("no `this' in nameless context");
2021 else return 0;
2022 }
2023
2024 b = SYMBOL_BLOCK_VALUE (func);
2025 i = BLOCK_NSYMS (b);
2026 if (i <= 0)
2027 if (complain)
2028 error ("no args, no `this'");
2029 else return 0;
2030
2031 /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
2032 symbol instead of the LOC_ARG one (if both exist). */
2033 sym = lookup_block_symbol (b, funny_this, VAR_NAMESPACE);
2034 if (sym == NULL)
2035 {
2036 if (complain)
2037 error ("current stack frame not in method");
2038 else
2039 return NULL;
2040 }
2041
2042 this = read_var_value (sym, selected_frame);
2043 if (this == 0 && complain)
2044 error ("`this' argument at unknown address");
2045 return this;
2046 }
2047
2048 /* Create a slice (sub-string, sub-array) of ARRAY, that is LENGTH elements
2049 long, starting at LOWBOUND. The result has the same lower bound as
2050 the original ARRAY. */
2051
2052 value_ptr
2053 value_slice (array, lowbound, length)
2054 value_ptr array;
2055 int lowbound, length;
2056 {
2057 if (TYPE_CODE (VALUE_TYPE (array)) == TYPE_CODE_BITSTRING)
2058 error ("not implemented - bitstring slice");
2059 if (TYPE_CODE (VALUE_TYPE (array)) != TYPE_CODE_ARRAY
2060 && TYPE_CODE (VALUE_TYPE (array)) != TYPE_CODE_STRING)
2061 error ("cannot take slice of non-array");
2062 else
2063 {
2064 struct type *slice_range_type, *slice_type;
2065 value_ptr slice;
2066 struct type *range_type = TYPE_FIELD_TYPE (VALUE_TYPE (array), 0);
2067 struct type *element_type = TYPE_TARGET_TYPE (VALUE_TYPE (array));
2068 int lowerbound = TYPE_LOW_BOUND (range_type);
2069 int upperbound = TYPE_HIGH_BOUND (range_type);
2070 int offset = (lowbound - lowerbound) * TYPE_LENGTH (element_type);
2071 if (lowbound < lowerbound || length < 0
2072 || lowbound + length - 1 > upperbound)
2073 error ("slice out of range");
2074 slice_range_type = create_range_type ((struct type*) NULL,
2075 TYPE_TARGET_TYPE (range_type),
2076 lowerbound,
2077 lowerbound + length - 1);
2078 slice_type = create_array_type ((struct type*) NULL, element_type,
2079 slice_range_type);
2080 TYPE_CODE (slice_type) = TYPE_CODE (VALUE_TYPE (array));
2081 slice = allocate_value (slice_type);
2082 if (VALUE_LAZY (array))
2083 VALUE_LAZY (slice) = 1;
2084 else
2085 memcpy (VALUE_CONTENTS (slice), VALUE_CONTENTS (array) + offset,
2086 TYPE_LENGTH (slice_type));
2087 if (VALUE_LVAL (array) == lval_internalvar)
2088 VALUE_LVAL (slice) = lval_internalvar_component;
2089 else
2090 VALUE_LVAL (slice) = VALUE_LVAL (array);
2091 VALUE_ADDRESS (slice) = VALUE_ADDRESS (array);
2092 VALUE_OFFSET (slice) = VALUE_OFFSET (array) + offset;
2093 return slice;
2094 }
2095 }
2096
2097 /* Assuming chill_varying_type (VARRAY) is true, return an equivalent
2098 value as a fixed-length array. */
2099
2100 value_ptr
2101 varying_to_slice (varray)
2102 value_ptr varray;
2103 {
2104 struct type *vtype = VALUE_TYPE (varray);
2105 LONGEST length = unpack_long (TYPE_FIELD_TYPE (vtype, 0),
2106 VALUE_CONTENTS (varray)
2107 + TYPE_FIELD_BITPOS (vtype, 0) / 8);
2108 return value_slice (value_primitive_field (varray, 0, 1, vtype), 0, length);
2109 }
2110
2111 /* Create a value for a FORTRAN complex number. Currently most of
2112 the time values are coerced to COMPLEX*16 (i.e. a complex number
2113 composed of 2 doubles. This really should be a smarter routine
2114 that figures out precision inteligently as opposed to assuming
2115 doubles. FIXME: fmb */
2116
2117 value_ptr
2118 value_literal_complex (arg1, arg2, type)
2119 value_ptr arg1;
2120 value_ptr arg2;
2121 struct type *type;
2122 {
2123 register value_ptr val;
2124 struct type *real_type = TYPE_TARGET_TYPE (type);
2125
2126 val = allocate_value (type);
2127 arg1 = value_cast (real_type, arg1);
2128 arg2 = value_cast (real_type, arg2);
2129
2130 memcpy (VALUE_CONTENTS_RAW (val),
2131 VALUE_CONTENTS (arg1), TYPE_LENGTH (real_type));
2132 memcpy (VALUE_CONTENTS_RAW (val) + TYPE_LENGTH (real_type),
2133 VALUE_CONTENTS (arg2), TYPE_LENGTH (real_type));
2134 return val;
2135 }
2136
2137 /* Cast a value into the appropriate complex data type. */
2138
2139 static value_ptr
2140 cast_into_complex (type, val)
2141 struct type *type;
2142 register value_ptr val;
2143 {
2144 struct type *real_type = TYPE_TARGET_TYPE (type);
2145 if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_COMPLEX)
2146 {
2147 struct type *val_real_type = TYPE_TARGET_TYPE (VALUE_TYPE (val));
2148 value_ptr re_val = allocate_value (val_real_type);
2149 value_ptr im_val = allocate_value (val_real_type);
2150
2151 memcpy (VALUE_CONTENTS_RAW (re_val),
2152 VALUE_CONTENTS (val), TYPE_LENGTH (val_real_type));
2153 memcpy (VALUE_CONTENTS_RAW (im_val),
2154 VALUE_CONTENTS (val) + TYPE_LENGTH (val_real_type),
2155 TYPE_LENGTH (val_real_type));
2156
2157 return value_literal_complex (re_val, im_val, type);
2158 }
2159 else if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FLT
2160 || TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_INT)
2161 return value_literal_complex (val, value_zero (real_type, not_lval), type);
2162 else
2163 error ("cannot cast non-number to complex");
2164 }
This page took 0.093618 seconds and 4 git commands to generate.