1 /* Perform non-arithmetic operations on values, for GDB.
2 Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994
3 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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. */
34 /* Local functions. */
36 static int typecmp PARAMS ((int staticp, struct type *t1[], value_ptr t2[]));
38 static CORE_ADDR find_function_addr PARAMS ((value_ptr, struct type **));
40 static CORE_ADDR value_push PARAMS ((CORE_ADDR, value_ptr));
42 static CORE_ADDR value_arg_push PARAMS ((CORE_ADDR, value_ptr));
44 static value_ptr search_struct_field PARAMS ((char *, value_ptr, int,
47 static value_ptr search_struct_method PARAMS ((char *, value_ptr *,
49 int, int *, struct type *));
51 static int check_field_in PARAMS ((struct type *, const char *));
53 static CORE_ADDR allocate_space_in_inferior PARAMS ((int));
55 /* Allocate NBYTES of space in the inferior using the inferior's malloc
56 and return a value that is a pointer to the allocated space. */
59 allocate_space_in_inferior (len)
62 register value_ptr val;
63 register struct symbol *sym;
64 struct minimal_symbol *msymbol;
69 /* Find the address of malloc in the inferior. */
71 sym = lookup_symbol ("malloc", 0, VAR_NAMESPACE, 0, NULL);
74 if (SYMBOL_CLASS (sym) != LOC_BLOCK)
76 error ("\"malloc\" exists in this program but is not a function.");
78 val = value_of_variable (sym, NULL);
82 msymbol = lookup_minimal_symbol ("malloc", (struct objfile *) NULL);
85 type = lookup_pointer_type (builtin_type_char);
86 type = lookup_function_type (type);
87 type = lookup_pointer_type (type);
88 maddr = (LONGEST) SYMBOL_VALUE_ADDRESS (msymbol);
89 val = value_from_longest (type, maddr);
93 error ("evaluation of this expression requires the program to have a function \"malloc\".");
97 blocklen = value_from_longest (builtin_type_int, (LONGEST) len);
98 val = call_function_by_hand (val, 1, &blocklen);
99 if (value_logical_not (val))
101 error ("No memory available to program.");
103 return (value_as_long (val));
106 /* Cast value ARG2 to type TYPE and return as a value.
107 More general than a C cast: accepts any two types of the same length,
108 and if ARG2 is an lvalue it can be cast into anything at all. */
109 /* In C++, casts may change pointer or object representations. */
112 value_cast (type, arg2)
114 register value_ptr arg2;
116 register enum type_code code1;
117 register enum type_code code2;
120 /* Coerce arrays but not enums. Enums will work as-is
121 and coercing them would cause an infinite recursion. */
122 if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_ENUM)
125 code1 = TYPE_CODE (type);
126 code2 = TYPE_CODE (VALUE_TYPE (arg2));
127 scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
128 || code2 == TYPE_CODE_ENUM);
130 if ( code1 == TYPE_CODE_STRUCT
131 && code2 == TYPE_CODE_STRUCT
132 && TYPE_NAME (type) != 0)
134 /* Look in the type of the source to see if it contains the
135 type of the target as a superclass. If so, we'll need to
136 offset the object in addition to changing its type. */
137 value_ptr v = search_struct_field (type_name_no_tag (type),
138 arg2, 0, VALUE_TYPE (arg2), 1);
141 VALUE_TYPE (v) = type;
145 if (code1 == TYPE_CODE_FLT && scalar)
146 return value_from_double (type, value_as_double (arg2));
147 else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM)
148 && (scalar || code2 == TYPE_CODE_PTR))
149 return value_from_longest (type, value_as_long (arg2));
150 else if (TYPE_LENGTH (type) == TYPE_LENGTH (VALUE_TYPE (arg2)))
152 if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
154 /* Look in the type of the source to see if it contains the
155 type of the target as a superclass. If so, we'll need to
156 offset the pointer rather than just change its type. */
157 struct type *t1 = TYPE_TARGET_TYPE (type);
158 struct type *t2 = TYPE_TARGET_TYPE (VALUE_TYPE (arg2));
159 if ( TYPE_CODE (t1) == TYPE_CODE_STRUCT
160 && TYPE_CODE (t2) == TYPE_CODE_STRUCT
161 && TYPE_NAME (t1) != 0) /* if name unknown, can't have supercl */
163 value_ptr v = search_struct_field (type_name_no_tag (t1),
164 value_ind (arg2), 0, t2, 1);
168 VALUE_TYPE (v) = type;
172 /* No superclass found, just fall through to change ptr type. */
174 VALUE_TYPE (arg2) = type;
177 else if (VALUE_LVAL (arg2) == lval_memory)
179 return value_at_lazy (type, VALUE_ADDRESS (arg2) + VALUE_OFFSET (arg2));
181 else if (code1 == TYPE_CODE_VOID)
183 return value_zero (builtin_type_void, not_lval);
187 error ("Invalid cast.");
192 /* Create a value of type TYPE that is zero, and return it. */
195 value_zero (type, lv)
199 register value_ptr val = allocate_value (type);
201 memset (VALUE_CONTENTS (val), 0, TYPE_LENGTH (type));
202 VALUE_LVAL (val) = lv;
207 /* Return a value with type TYPE located at ADDR.
209 Call value_at only if the data needs to be fetched immediately;
210 if we can be 'lazy' and defer the fetch, perhaps indefinately, call
211 value_at_lazy instead. value_at_lazy simply records the address of
212 the data and sets the lazy-evaluation-required flag. The lazy flag
213 is tested in the VALUE_CONTENTS macro, which is used if and when
214 the contents are actually required. */
217 value_at (type, addr)
221 register value_ptr val;
223 if (TYPE_CODE (type) == TYPE_CODE_VOID)
224 error ("Attempt to dereference a generic pointer.");
226 val = allocate_value (type);
228 read_memory (addr, VALUE_CONTENTS_RAW (val), TYPE_LENGTH (type));
230 VALUE_LVAL (val) = lval_memory;
231 VALUE_ADDRESS (val) = addr;
236 /* Return a lazy value with type TYPE located at ADDR (cf. value_at). */
239 value_at_lazy (type, addr)
243 register value_ptr val;
245 if (TYPE_CODE (type) == TYPE_CODE_VOID)
246 error ("Attempt to dereference a generic pointer.");
248 val = allocate_value (type);
250 VALUE_LVAL (val) = lval_memory;
251 VALUE_ADDRESS (val) = addr;
252 VALUE_LAZY (val) = 1;
257 /* Called only from the VALUE_CONTENTS macro, if the current data for
258 a variable needs to be loaded into VALUE_CONTENTS(VAL). Fetches the
259 data from the user's process, and clears the lazy flag to indicate
260 that the data in the buffer is valid.
262 If the value is zero-length, we avoid calling read_memory, which would
263 abort. We mark the value as fetched anyway -- all 0 bytes of it.
265 This function returns a value because it is used in the VALUE_CONTENTS
266 macro as part of an expression, where a void would not work. The
270 value_fetch_lazy (val)
271 register value_ptr val;
273 CORE_ADDR addr = VALUE_ADDRESS (val) + VALUE_OFFSET (val);
275 if (TYPE_LENGTH (VALUE_TYPE (val)))
276 read_memory (addr, VALUE_CONTENTS_RAW (val),
277 TYPE_LENGTH (VALUE_TYPE (val)));
278 VALUE_LAZY (val) = 0;
283 /* Store the contents of FROMVAL into the location of TOVAL.
284 Return a new value with the location of TOVAL and contents of FROMVAL. */
287 value_assign (toval, fromval)
288 register value_ptr toval, fromval;
290 register struct type *type;
291 register value_ptr val;
292 char raw_buffer[MAX_REGISTER_RAW_SIZE];
295 if (!toval->modifiable)
296 error ("Left operand of assignment is not a modifiable lvalue.");
298 COERCE_ARRAY (fromval);
301 type = VALUE_TYPE (toval);
302 if (VALUE_LVAL (toval) != lval_internalvar)
303 fromval = value_cast (type, fromval);
305 /* If TOVAL is a special machine register requiring conversion
306 of program values to a special raw format,
307 convert FROMVAL's contents now, with result in `raw_buffer',
308 and set USE_BUFFER to the number of bytes to write. */
310 #ifdef REGISTER_CONVERTIBLE
311 if (VALUE_REGNO (toval) >= 0
312 && REGISTER_CONVERTIBLE (VALUE_REGNO (toval)))
314 int regno = VALUE_REGNO (toval);
315 if (REGISTER_CONVERTIBLE (regno))
317 REGISTER_CONVERT_TO_RAW (VALUE_TYPE (fromval), regno,
318 VALUE_CONTENTS (fromval), raw_buffer);
319 use_buffer = REGISTER_RAW_SIZE (regno);
324 switch (VALUE_LVAL (toval))
326 case lval_internalvar:
327 set_internalvar (VALUE_INTERNALVAR (toval), fromval);
330 case lval_internalvar_component:
331 set_internalvar_component (VALUE_INTERNALVAR (toval),
332 VALUE_OFFSET (toval),
333 VALUE_BITPOS (toval),
334 VALUE_BITSIZE (toval),
339 if (VALUE_BITSIZE (toval))
341 char buffer[sizeof (LONGEST)];
342 /* We assume that the argument to read_memory is in units of
343 host chars. FIXME: Is that correct? */
344 int len = (VALUE_BITPOS (toval)
345 + VALUE_BITSIZE (toval)
349 if (len > sizeof (LONGEST))
350 error ("Can't handle bitfields which don't fit in a %d bit word.",
351 sizeof (LONGEST) * HOST_CHAR_BIT);
353 read_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
355 modify_field (buffer, value_as_long (fromval),
356 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
357 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
361 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
362 raw_buffer, use_buffer);
364 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
365 VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
369 if (VALUE_BITSIZE (toval))
371 char buffer[sizeof (LONGEST)];
372 int len = REGISTER_RAW_SIZE (VALUE_REGNO (toval));
374 if (len > sizeof (LONGEST))
375 error ("Can't handle bitfields in registers larger than %d bits.",
376 sizeof (LONGEST) * HOST_CHAR_BIT);
378 if (VALUE_BITPOS (toval) + VALUE_BITSIZE (toval)
379 > len * HOST_CHAR_BIT)
380 /* Getting this right would involve being very careful about
383 Can't handle bitfield which doesn't fit in a single register.");
385 read_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
387 modify_field (buffer, value_as_long (fromval),
388 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
389 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
393 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
394 raw_buffer, use_buffer);
397 /* Do any conversion necessary when storing this type to more
398 than one register. */
399 #ifdef REGISTER_CONVERT_FROM_TYPE
400 memcpy (raw_buffer, VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
401 REGISTER_CONVERT_FROM_TYPE(VALUE_REGNO (toval), type, raw_buffer);
402 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
403 raw_buffer, TYPE_LENGTH (type));
405 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
406 VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
409 /* Assigning to the stack pointer, frame pointer, and other
410 (architecture and calling convention specific) registers may
411 cause the frame cache to be out of date. We just do this
412 on all assignments to registers for simplicity; I doubt the slowdown
414 reinit_frame_cache ();
417 case lval_reg_frame_relative:
419 /* value is stored in a series of registers in the frame
420 specified by the structure. Copy that value out, modify
421 it, and copy it back in. */
422 int amount_to_copy = (VALUE_BITSIZE (toval) ? 1 : TYPE_LENGTH (type));
423 int reg_size = REGISTER_RAW_SIZE (VALUE_FRAME_REGNUM (toval));
424 int byte_offset = VALUE_OFFSET (toval) % reg_size;
425 int reg_offset = VALUE_OFFSET (toval) / reg_size;
428 /* Make the buffer large enough in all cases. */
429 char *buffer = (char *) alloca (amount_to_copy
431 + MAX_REGISTER_RAW_SIZE);
436 /* Figure out which frame this is in currently. */
437 for (frame = get_current_frame ();
438 frame && FRAME_FP (frame) != VALUE_FRAME (toval);
439 frame = get_prev_frame (frame))
443 error ("Value being assigned to is no longer active.");
445 amount_to_copy += (reg_size - amount_to_copy % reg_size);
448 for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset,
450 amount_copied < amount_to_copy;
451 amount_copied += reg_size, regno++)
453 get_saved_register (buffer + amount_copied,
454 (int *)NULL, (CORE_ADDR *)NULL,
455 frame, regno, (enum lval_type *)NULL);
458 /* Modify what needs to be modified. */
459 if (VALUE_BITSIZE (toval))
460 modify_field (buffer + byte_offset,
461 value_as_long (fromval),
462 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
464 memcpy (buffer + byte_offset, raw_buffer, use_buffer);
466 memcpy (buffer + byte_offset, VALUE_CONTENTS (fromval),
470 for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset,
472 amount_copied < amount_to_copy;
473 amount_copied += reg_size, regno++)
479 /* Just find out where to put it. */
480 get_saved_register ((char *)NULL,
481 &optim, &addr, frame, regno, &lval);
484 error ("Attempt to assign to a value that was optimized out.");
485 if (lval == lval_memory)
486 write_memory (addr, buffer + amount_copied, reg_size);
487 else if (lval == lval_register)
488 write_register_bytes (addr, buffer + amount_copied, reg_size);
490 error ("Attempt to assign to an unmodifiable value.");
497 error ("Left operand of assignment is not an lvalue.");
500 /* Return a value just like TOVAL except with the contents of FROMVAL
501 (except in the case of the type if TOVAL is an internalvar). */
503 if (VALUE_LVAL (toval) == lval_internalvar
504 || VALUE_LVAL (toval) == lval_internalvar_component)
506 type = VALUE_TYPE (fromval);
509 val = allocate_value (type);
510 memcpy (val, toval, VALUE_CONTENTS_RAW (val) - (char *) val);
511 memcpy (VALUE_CONTENTS_RAW (val), VALUE_CONTENTS (fromval),
513 VALUE_TYPE (val) = type;
518 /* Extend a value VAL to COUNT repetitions of its type. */
521 value_repeat (arg1, count)
525 register value_ptr val;
527 if (VALUE_LVAL (arg1) != lval_memory)
528 error ("Only values in memory can be extended with '@'.");
530 error ("Invalid number %d of repetitions.", count);
532 val = allocate_repeat_value (VALUE_TYPE (arg1), count);
534 read_memory (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1),
535 VALUE_CONTENTS_RAW (val),
536 TYPE_LENGTH (VALUE_TYPE (val)) * count);
537 VALUE_LVAL (val) = lval_memory;
538 VALUE_ADDRESS (val) = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1);
544 value_of_variable (var, b)
552 /* Use selected frame. */
556 fr = block_innermost_frame (b);
557 if (fr == NULL && symbol_read_needs_frame (var))
559 if (BLOCK_FUNCTION (b) != NULL
560 && SYMBOL_NAME (BLOCK_FUNCTION (b)) != NULL)
561 error ("No frame is currently executing in block %s.",
562 SYMBOL_NAME (BLOCK_FUNCTION (b)));
564 error ("No frame is currently executing in specified block");
567 val = read_var_value (var, fr);
569 error ("Address of symbol \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var));
573 /* Given a value which is an array, return a value which is a pointer to its
574 first element, regardless of whether or not the array has a nonzero lower
577 FIXME: A previous comment here indicated that this routine should be
578 substracting the array's lower bound. It's not clear to me that this
579 is correct. Given an array subscripting operation, it would certainly
580 work to do the adjustment here, essentially computing:
582 (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0])
584 However I believe a more appropriate and logical place to account for
585 the lower bound is to do so in value_subscript, essentially computing:
587 (&array[0] + ((index - lowerbound) * sizeof array[0]))
589 As further evidence consider what would happen with operations other
590 than array subscripting, where the caller would get back a value that
591 had an address somewhere before the actual first element of the array,
592 and the information about the lower bound would be lost because of
593 the coercion to pointer type.
597 value_coerce_array (arg1)
600 register struct type *type;
602 if (VALUE_LVAL (arg1) != lval_memory)
603 error ("Attempt to take address of value not located in memory.");
605 /* Get type of elements. */
606 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_ARRAY
607 || TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRING)
608 type = TYPE_TARGET_TYPE (VALUE_TYPE (arg1));
610 /* A phony array made by value_repeat.
611 Its type is the type of the elements, not an array type. */
612 type = VALUE_TYPE (arg1);
614 return value_from_longest (lookup_pointer_type (type),
615 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
618 /* Given a value which is a function, return a value which is a pointer
622 value_coerce_function (arg1)
626 if (VALUE_LVAL (arg1) != lval_memory)
627 error ("Attempt to take address of value not located in memory.");
629 return value_from_longest (lookup_pointer_type (VALUE_TYPE (arg1)),
630 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
633 /* Return a pointer value for the object for which ARG1 is the contents. */
639 struct type *type = VALUE_TYPE (arg1);
640 if (TYPE_CODE (type) == TYPE_CODE_REF)
642 /* Copy the value, but change the type from (T&) to (T*).
643 We keep the same location information, which is efficient,
644 and allows &(&X) to get the location containing the reference. */
645 value_ptr arg2 = value_copy (arg1);
646 VALUE_TYPE (arg2) = lookup_pointer_type (TYPE_TARGET_TYPE (type));
649 if (VALUE_REPEATED (arg1)
650 || TYPE_CODE (type) == TYPE_CODE_ARRAY)
651 return value_coerce_array (arg1);
652 if (TYPE_CODE (type) == TYPE_CODE_FUNC)
653 return value_coerce_function (arg1);
655 if (VALUE_LVAL (arg1) != lval_memory)
656 error ("Attempt to take address of value not located in memory.");
658 return value_from_longest (lookup_pointer_type (type),
659 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
662 /* Given a value of a pointer type, apply the C unary * operator to it. */
670 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_MEMBER)
671 error ("not implemented: member types in value_ind");
673 /* Allow * on an integer so we can cast it to whatever we want.
674 This returns an int, which seems like the most C-like thing
675 to do. "long long" variables are rare enough that
676 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
677 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT)
678 return value_at (builtin_type_int,
679 (CORE_ADDR) value_as_long (arg1));
680 else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR)
681 return value_at_lazy (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
682 value_as_pointer (arg1));
683 error ("Attempt to take contents of a non-pointer value.");
684 return 0; /* For lint -- never reached */
687 /* Pushing small parts of stack frames. */
689 /* Push one word (the size of object that a register holds). */
694 unsigned LONGEST word;
696 register int len = REGISTER_SIZE;
697 char buffer[MAX_REGISTER_RAW_SIZE];
699 store_unsigned_integer (buffer, len, word);
702 write_memory (sp, buffer, len);
703 #else /* stack grows upward */
704 write_memory (sp, buffer, len);
706 #endif /* stack grows upward */
711 /* Push LEN bytes with data at BUFFER. */
714 push_bytes (sp, buffer, len)
721 write_memory (sp, buffer, len);
722 #else /* stack grows upward */
723 write_memory (sp, buffer, len);
725 #endif /* stack grows upward */
730 /* Push onto the stack the specified value VALUE. */
734 register CORE_ADDR sp;
737 register int len = TYPE_LENGTH (VALUE_TYPE (arg));
741 write_memory (sp, VALUE_CONTENTS (arg), len);
742 #else /* stack grows upward */
743 write_memory (sp, VALUE_CONTENTS (arg), len);
745 #endif /* stack grows upward */
750 /* Perform the standard coercions that are specified
751 for arguments to be passed to C functions. */
754 value_arg_coerce (arg)
757 register struct type *type;
759 /* FIXME: We should coerce this according to the prototype (if we have
760 one). Right now we do a little bit of this in typecmp(), but that
761 doesn't always get called. For example, if passing a ref to a function
762 without a prototype, we probably should de-reference it. Currently
765 if (TYPE_CODE (VALUE_TYPE (arg)) == TYPE_CODE_ENUM)
766 arg = value_cast (builtin_type_unsigned_int, arg);
768 #if 1 /* FIXME: This is only a temporary patch. -fnf */
769 if (VALUE_REPEATED (arg)
770 || TYPE_CODE (VALUE_TYPE (arg)) == TYPE_CODE_ARRAY)
771 arg = value_coerce_array (arg);
772 if (TYPE_CODE (VALUE_TYPE (arg)) == TYPE_CODE_FUNC)
773 arg = value_coerce_function (arg);
776 type = VALUE_TYPE (arg);
778 if (TYPE_CODE (type) == TYPE_CODE_INT
779 && TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
780 return value_cast (builtin_type_int, arg);
782 if (TYPE_CODE (type) == TYPE_CODE_FLT
783 && TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_double))
784 return value_cast (builtin_type_double, arg);
789 /* Push the value ARG, first coercing it as an argument
793 value_arg_push (sp, arg)
794 register CORE_ADDR sp;
797 return value_push (sp, value_arg_coerce (arg));
800 /* Determine a function's address and its return type from its value.
801 Calls error() if the function is not valid for calling. */
804 find_function_addr (function, retval_type)
806 struct type **retval_type;
808 register struct type *ftype = VALUE_TYPE (function);
809 register enum type_code code = TYPE_CODE (ftype);
810 struct type *value_type;
813 /* If it's a member function, just look at the function
816 /* Determine address to call. */
817 if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD)
819 funaddr = VALUE_ADDRESS (function);
820 value_type = TYPE_TARGET_TYPE (ftype);
822 else if (code == TYPE_CODE_PTR)
824 funaddr = value_as_pointer (function);
825 if (TYPE_CODE (TYPE_TARGET_TYPE (ftype)) == TYPE_CODE_FUNC
826 || TYPE_CODE (TYPE_TARGET_TYPE (ftype)) == TYPE_CODE_METHOD)
827 value_type = TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (ftype));
829 value_type = builtin_type_int;
831 else if (code == TYPE_CODE_INT)
833 /* Handle the case of functions lacking debugging info.
834 Their values are characters since their addresses are char */
835 if (TYPE_LENGTH (ftype) == 1)
836 funaddr = value_as_pointer (value_addr (function));
838 /* Handle integer used as address of a function. */
839 funaddr = (CORE_ADDR) value_as_long (function);
841 value_type = builtin_type_int;
844 error ("Invalid data type for function to be called.");
846 *retval_type = value_type;
850 #if defined (CALL_DUMMY)
851 /* All this stuff with a dummy frame may seem unnecessarily complicated
852 (why not just save registers in GDB?). The purpose of pushing a dummy
853 frame which looks just like a real frame is so that if you call a
854 function and then hit a breakpoint (get a signal, etc), "backtrace"
855 will look right. Whether the backtrace needs to actually show the
856 stack at the time the inferior function was called is debatable, but
857 it certainly needs to not display garbage. So if you are contemplating
858 making dummy frames be different from normal frames, consider that. */
860 /* Perform a function call in the inferior.
861 ARGS is a vector of values of arguments (NARGS of them).
862 FUNCTION is a value, the function to be called.
863 Returns a value representing what the function returned.
864 May fail to return, if a breakpoint or signal is hit
865 during the execution of the function. */
868 call_function_by_hand (function, nargs, args)
873 register CORE_ADDR sp;
876 /* CALL_DUMMY is an array of words (REGISTER_SIZE), but each word
877 is in host byte order. Before calling FIX_CALL_DUMMY, we byteswap it
878 and remove any extra bytes which might exist because unsigned LONGEST is
879 bigger than REGISTER_SIZE. */
880 static unsigned LONGEST dummy[] = CALL_DUMMY;
881 char dummy1[REGISTER_SIZE * sizeof dummy / sizeof (unsigned LONGEST)];
883 struct type *value_type;
884 unsigned char struct_return;
885 CORE_ADDR struct_addr;
886 struct inferior_status inf_status;
887 struct cleanup *old_chain;
892 if (!target_has_execution)
895 save_inferior_status (&inf_status, 1);
896 old_chain = make_cleanup (restore_inferior_status, &inf_status);
898 /* PUSH_DUMMY_FRAME is responsible for saving the inferior registers
899 (and POP_FRAME for restoring them). (At least on most machines)
900 they are saved on the stack in the inferior. */
903 old_sp = sp = read_sp ();
905 #if 1 INNER_THAN 2 /* Stack grows down */
908 #else /* Stack grows up */
913 funaddr = find_function_addr (function, &value_type);
916 struct block *b = block_for_pc (funaddr);
917 /* If compiled without -g, assume GCC. */
918 using_gcc = b == NULL || BLOCK_GCC_COMPILED (b);
921 /* Are we returning a value using a structure return or a normal
924 struct_return = using_struct_return (function, funaddr, value_type,
927 /* Create a call sequence customized for this function
928 and the number of arguments for it. */
929 for (i = 0; i < sizeof dummy / sizeof (dummy[0]); i++)
930 store_unsigned_integer (&dummy1[i * REGISTER_SIZE],
932 (unsigned LONGEST)dummy[i]);
934 #ifdef GDB_TARGET_IS_HPPA
935 real_pc = FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, args,
936 value_type, using_gcc);
938 FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, args,
939 value_type, using_gcc);
943 #if CALL_DUMMY_LOCATION == ON_STACK
944 write_memory (start_sp, (char *)dummy1, sizeof dummy);
945 #endif /* On stack. */
947 #if CALL_DUMMY_LOCATION == BEFORE_TEXT_END
948 /* Convex Unix prohibits executing in the stack segment. */
949 /* Hope there is empty room at the top of the text segment. */
951 extern CORE_ADDR text_end;
954 for (start_sp = text_end - sizeof dummy; start_sp < text_end; ++start_sp)
955 if (read_memory_integer (start_sp, 1) != 0)
956 error ("text segment full -- no place to put call");
959 real_pc = text_end - sizeof dummy;
960 write_memory (real_pc, (char *)dummy1, sizeof dummy);
962 #endif /* Before text_end. */
964 #if CALL_DUMMY_LOCATION == AFTER_TEXT_END
966 extern CORE_ADDR text_end;
970 errcode = target_write_memory (real_pc, (char *)dummy1, sizeof dummy);
972 error ("Cannot write text segment -- call_function failed");
974 #endif /* After text_end. */
976 #if CALL_DUMMY_LOCATION == AT_ENTRY_POINT
978 #endif /* At entry point. */
981 sp = old_sp; /* It really is used, for some ifdef's... */
985 /* If stack grows down, we must leave a hole at the top. */
989 /* Reserve space for the return structure to be written on the
990 stack, if necessary */
993 len += TYPE_LENGTH (value_type);
995 for (i = nargs - 1; i >= 0; i--)
996 len += TYPE_LENGTH (VALUE_TYPE (value_arg_coerce (args[i])));
997 #ifdef CALL_DUMMY_STACK_ADJUST
998 len += CALL_DUMMY_STACK_ADJUST;
1001 sp -= STACK_ALIGN (len) - len;
1003 sp += STACK_ALIGN (len) - len;
1006 #endif /* STACK_ALIGN */
1008 /* Reserve space for the return structure to be written on the
1009 stack, if necessary */
1014 sp -= TYPE_LENGTH (value_type);
1018 sp += TYPE_LENGTH (value_type);
1022 #if defined (REG_STRUCT_HAS_ADDR)
1024 /* This is a machine like the sparc, where we may need to pass a pointer
1025 to the structure, not the structure itself. */
1026 for (i = nargs - 1; i >= 0; i--)
1027 if (TYPE_CODE (VALUE_TYPE (args[i])) == TYPE_CODE_STRUCT
1028 && REG_STRUCT_HAS_ADDR (using_gcc, VALUE_TYPE (args[i])))
1031 #if !(1 INNER_THAN 2)
1032 /* The stack grows up, so the address of the thing we push
1033 is the stack pointer before we push it. */
1036 /* Push the structure. */
1037 sp = value_push (sp, args[i]);
1039 /* The stack grows down, so the address of the thing we push
1040 is the stack pointer after we push it. */
1043 /* The value we're going to pass is the address of the thing
1045 args[i] = value_from_longest (lookup_pointer_type (value_type),
1049 #endif /* REG_STRUCT_HAS_ADDR. */
1051 #ifdef PUSH_ARGUMENTS
1052 PUSH_ARGUMENTS(nargs, args, sp, struct_return, struct_addr);
1053 #else /* !PUSH_ARGUMENTS */
1054 for (i = nargs - 1; i >= 0; i--)
1055 sp = value_arg_push (sp, args[i]);
1056 #endif /* !PUSH_ARGUMENTS */
1058 #ifdef CALL_DUMMY_STACK_ADJUST
1060 sp -= CALL_DUMMY_STACK_ADJUST;
1062 sp += CALL_DUMMY_STACK_ADJUST;
1064 #endif /* CALL_DUMMY_STACK_ADJUST */
1066 /* Store the address at which the structure is supposed to be
1067 written. Note that this (and the code which reserved the space
1068 above) assumes that gcc was used to compile this function. Since
1069 it doesn't cost us anything but space and if the function is pcc
1070 it will ignore this value, we will make that assumption.
1072 Also note that on some machines (like the sparc) pcc uses a
1073 convention like gcc's. */
1076 STORE_STRUCT_RETURN (struct_addr, sp);
1078 /* Write the stack pointer. This is here because the statements above
1079 might fool with it. On SPARC, this write also stores the register
1080 window into the right place in the new stack frame, which otherwise
1081 wouldn't happen. (See store_inferior_registers in sparc-nat.c.) */
1085 char retbuf[REGISTER_BYTES];
1087 struct symbol *symbol;
1090 symbol = find_pc_function (funaddr);
1093 name = SYMBOL_SOURCE_NAME (symbol);
1097 /* Try the minimal symbols. */
1098 struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (funaddr);
1102 name = SYMBOL_SOURCE_NAME (msymbol);
1108 sprintf (format, "at %s", local_hex_format ());
1110 /* FIXME-32x64: assumes funaddr fits in a long. */
1111 sprintf (name, format, (unsigned long) funaddr);
1114 /* Execute the stack dummy routine, calling FUNCTION.
1115 When it is done, discard the empty frame
1116 after storing the contents of all regs into retbuf. */
1117 if (run_stack_dummy (real_pc + CALL_DUMMY_START_OFFSET, retbuf))
1119 /* We stopped somewhere besides the call dummy. */
1121 /* If we did the cleanups, we would print a spurious error message
1122 (Unable to restore previously selected frame), would write the
1123 registers from the inf_status (which is wrong), and would do other
1124 wrong things (like set stop_bpstat to the wrong thing). */
1125 discard_cleanups (old_chain);
1126 /* Prevent memory leak. */
1127 bpstat_clear (&inf_status.stop_bpstat);
1129 /* The following error message used to say "The expression
1130 which contained the function call has been discarded." It
1131 is a hard concept to explain in a few words. Ideally, GDB
1132 would be able to resume evaluation of the expression when
1133 the function finally is done executing. Perhaps someday
1134 this will be implemented (it would not be easy). */
1136 /* FIXME: Insert a bunch of wrap_here; name can be very long if it's
1137 a C++ name with arguments and stuff. */
1139 The program being debugged stopped while in a function called from GDB.\n\
1140 When the function (%s) is done executing, GDB will silently\n\
1141 stop (instead of continuing to evaluate the expression containing\n\
1142 the function call).", name);
1145 do_cleanups (old_chain);
1147 /* Figure out the value returned by the function. */
1148 return value_being_returned (value_type, retbuf, struct_return);
1151 #else /* no CALL_DUMMY. */
1153 call_function_by_hand (function, nargs, args)
1158 error ("Cannot invoke functions on this machine.");
1160 #endif /* no CALL_DUMMY. */
1163 /* Create a value for an array by allocating space in the inferior, copying
1164 the data into that space, and then setting up an array value.
1166 The array bounds are set from LOWBOUND and HIGHBOUND, and the array is
1167 populated from the values passed in ELEMVEC.
1169 The element type of the array is inherited from the type of the
1170 first element, and all elements must have the same size (though we
1171 don't currently enforce any restriction on their types). */
1174 value_array (lowbound, highbound, elemvec)
1183 struct type *rangetype;
1184 struct type *arraytype;
1187 /* Validate that the bounds are reasonable and that each of the elements
1188 have the same size. */
1190 nelem = highbound - lowbound + 1;
1193 error ("bad array bounds (%d, %d)", lowbound, highbound);
1195 typelength = TYPE_LENGTH (VALUE_TYPE (elemvec[0]));
1196 for (idx = 0; idx < nelem; idx++)
1198 if (TYPE_LENGTH (VALUE_TYPE (elemvec[idx])) != typelength)
1200 error ("array elements must all be the same size");
1204 /* Allocate space to store the array in the inferior, and then initialize
1205 it by copying in each element. FIXME: Is it worth it to create a
1206 local buffer in which to collect each value and then write all the
1207 bytes in one operation? */
1209 addr = allocate_space_in_inferior (nelem * typelength);
1210 for (idx = 0; idx < nelem; idx++)
1212 write_memory (addr + (idx * typelength), VALUE_CONTENTS (elemvec[idx]),
1216 /* Create the array type and set up an array value to be evaluated lazily. */
1218 rangetype = create_range_type ((struct type *) NULL, builtin_type_int,
1219 lowbound, highbound);
1220 arraytype = create_array_type ((struct type *) NULL,
1221 VALUE_TYPE (elemvec[0]), rangetype);
1222 val = value_at_lazy (arraytype, addr);
1226 /* Create a value for a string constant by allocating space in the inferior,
1227 copying the data into that space, and returning the address with type
1228 TYPE_CODE_STRING. PTR points to the string constant data; LEN is number
1230 Note that string types are like array of char types with a lower bound of
1231 zero and an upper bound of LEN - 1. Also note that the string may contain
1232 embedded null bytes. */
1235 value_string (ptr, len)
1240 struct type *rangetype;
1241 struct type *stringtype;
1244 /* Allocate space to store the string in the inferior, and then
1245 copy LEN bytes from PTR in gdb to that address in the inferior. */
1247 addr = allocate_space_in_inferior (len);
1248 write_memory (addr, ptr, len);
1250 /* Create the string type and set up a string value to be evaluated
1253 rangetype = create_range_type ((struct type *) NULL, builtin_type_int,
1255 stringtype = create_string_type ((struct type *) NULL, rangetype);
1256 val = value_at_lazy (stringtype, addr);
1260 /* See if we can pass arguments in T2 to a function which takes arguments
1261 of types T1. Both t1 and t2 are NULL-terminated vectors. If some
1262 arguments need coercion of some sort, then the coerced values are written
1263 into T2. Return value is 0 if the arguments could be matched, or the
1264 position at which they differ if not.
1266 STATICP is nonzero if the T1 argument list came from a
1267 static member function.
1269 For non-static member functions, we ignore the first argument,
1270 which is the type of the instance variable. This is because we want
1271 to handle calls with objects from derived classes. This is not
1272 entirely correct: we should actually check to make sure that a
1273 requested operation is type secure, shouldn't we? FIXME. */
1276 typecmp (staticp, t1, t2)
1285 if (staticp && t1 == 0)
1289 if (TYPE_CODE (t1[0]) == TYPE_CODE_VOID) return 0;
1290 if (t1[!staticp] == 0) return 0;
1291 for (i = !staticp; t1[i] && TYPE_CODE (t1[i]) != TYPE_CODE_VOID; i++)
1293 struct type *tt1, *tt2;
1297 tt2 = VALUE_TYPE(t2[i]);
1298 if (TYPE_CODE (tt1) == TYPE_CODE_REF
1299 /* We should be doing hairy argument matching, as below. */
1300 && (TYPE_CODE (TYPE_TARGET_TYPE (tt1)) == TYPE_CODE (tt2)))
1302 t2[i] = value_addr (t2[i]);
1306 while (TYPE_CODE (tt1) == TYPE_CODE_PTR
1307 && (TYPE_CODE(tt2)==TYPE_CODE_ARRAY || TYPE_CODE(tt2)==TYPE_CODE_PTR))
1309 tt1 = TYPE_TARGET_TYPE(tt1);
1310 tt2 = TYPE_TARGET_TYPE(tt2);
1312 if (TYPE_CODE(tt1) == TYPE_CODE(tt2)) continue;
1313 /* Array to pointer is a `trivial conversion' according to the ARM. */
1315 /* We should be doing much hairier argument matching (see section 13.2
1316 of the ARM), but as a quick kludge, just check for the same type
1318 if (TYPE_CODE (t1[i]) != TYPE_CODE (VALUE_TYPE (t2[i])))
1321 if (!t1[i]) return 0;
1322 return t2[i] ? i+1 : 0;
1325 /* Helper function used by value_struct_elt to recurse through baseclasses.
1326 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1327 and search in it assuming it has (class) type TYPE.
1328 If found, return value, else return NULL.
1330 If LOOKING_FOR_BASECLASS, then instead of looking for struct fields,
1331 look for a baseclass named NAME. */
1334 search_struct_field (name, arg1, offset, type, looking_for_baseclass)
1336 register value_ptr arg1;
1338 register struct type *type;
1339 int looking_for_baseclass;
1343 check_stub_type (type);
1345 if (! looking_for_baseclass)
1346 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
1348 char *t_field_name = TYPE_FIELD_NAME (type, i);
1350 if (t_field_name && STREQ (t_field_name, name))
1353 if (TYPE_FIELD_STATIC (type, i))
1355 char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, i);
1356 struct symbol *sym =
1357 lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL);
1359 error ("Internal error: could not find physical static variable named %s",
1361 v = value_at (TYPE_FIELD_TYPE (type, i),
1362 (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym));
1365 v = value_primitive_field (arg1, offset, i, type);
1367 error("there is no field named %s", name);
1372 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1375 /* If we are looking for baseclasses, this is what we get when we
1376 hit them. But it could happen that the base part's member name
1377 is not yet filled in. */
1378 int found_baseclass = (looking_for_baseclass
1379 && TYPE_BASECLASS_NAME (type, i) != NULL
1380 && STREQ (name, TYPE_BASECLASS_NAME (type, i)));
1382 if (BASETYPE_VIA_VIRTUAL (type, i))
1385 /* Fix to use baseclass_offset instead. FIXME */
1386 baseclass_addr (type, i, VALUE_CONTENTS (arg1) + offset,
1389 error ("virtual baseclass botch");
1390 if (found_baseclass)
1392 v = search_struct_field (name, v2, 0, TYPE_BASECLASS (type, i),
1393 looking_for_baseclass);
1395 else if (found_baseclass)
1396 v = value_primitive_field (arg1, offset, i, type);
1398 v = search_struct_field (name, arg1,
1399 offset + TYPE_BASECLASS_BITPOS (type, i) / 8,
1400 TYPE_BASECLASS (type, i),
1401 looking_for_baseclass);
1407 /* Helper function used by value_struct_elt to recurse through baseclasses.
1408 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1409 and search in it assuming it has (class) type TYPE.
1410 If found, return value, else if name matched and args not return (value)-1,
1411 else return NULL. */
1414 search_struct_method (name, arg1p, args, offset, static_memfuncp, type)
1416 register value_ptr *arg1p, *args;
1417 int offset, *static_memfuncp;
1418 register struct type *type;
1422 int name_matched = 0;
1423 char dem_opname[64];
1425 check_stub_type (type);
1426 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
1428 char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
1429 if (strncmp(t_field_name, "__", 2)==0 ||
1430 strncmp(t_field_name, "op", 2)==0 ||
1431 strncmp(t_field_name, "type", 4)==0 )
1433 if (cplus_demangle_opname(t_field_name, dem_opname, DMGL_ANSI))
1434 t_field_name = dem_opname;
1435 else if (cplus_demangle_opname(t_field_name, dem_opname, 0))
1436 t_field_name = dem_opname;
1438 if (t_field_name && STREQ (t_field_name, name))
1440 int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1;
1441 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1444 if (j > 0 && args == 0)
1445 error ("cannot resolve overloaded method `%s'", name);
1448 if (TYPE_FN_FIELD_STUB (f, j))
1449 check_stub_method (type, i, j);
1450 if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j),
1451 TYPE_FN_FIELD_ARGS (f, j), args))
1453 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1454 return value_virtual_fn_field (arg1p, f, j, type, offset);
1455 if (TYPE_FN_FIELD_STATIC_P (f, j) && static_memfuncp)
1456 *static_memfuncp = 1;
1457 v = value_fn_field (arg1p, f, j, type, offset);
1458 if (v != NULL) return v;
1465 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1469 if (BASETYPE_VIA_VIRTUAL (type, i))
1471 base_offset = baseclass_offset (type, i, *arg1p, offset);
1472 if (base_offset == -1)
1473 error ("virtual baseclass botch");
1477 base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
1479 v = search_struct_method (name, arg1p, args, base_offset + offset,
1480 static_memfuncp, TYPE_BASECLASS (type, i));
1481 if (v == (value_ptr) -1)
1487 /* FIXME-bothner: Why is this commented out? Why is it here? */
1488 /* *arg1p = arg1_tmp;*/
1492 if (name_matched) return (value_ptr) -1;
1496 /* Given *ARGP, a value of type (pointer to a)* structure/union,
1497 extract the component named NAME from the ultimate target structure/union
1498 and return it as a value with its appropriate type.
1499 ERR is used in the error message if *ARGP's type is wrong.
1501 C++: ARGS is a list of argument types to aid in the selection of
1502 an appropriate method. Also, handle derived types.
1504 STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
1505 where the truthvalue of whether the function that was resolved was
1506 a static member function or not is stored.
1508 ERR is an error message to be printed in case the field is not found. */
1511 value_struct_elt (argp, args, name, static_memfuncp, err)
1512 register value_ptr *argp, *args;
1514 int *static_memfuncp;
1517 register struct type *t;
1520 COERCE_ARRAY (*argp);
1522 t = VALUE_TYPE (*argp);
1524 /* Follow pointers until we get to a non-pointer. */
1526 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1528 *argp = value_ind (*argp);
1529 /* Don't coerce fn pointer to fn and then back again! */
1530 if (TYPE_CODE (VALUE_TYPE (*argp)) != TYPE_CODE_FUNC)
1531 COERCE_ARRAY (*argp);
1532 t = VALUE_TYPE (*argp);
1535 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1536 error ("not implemented: member type in value_struct_elt");
1538 if ( TYPE_CODE (t) != TYPE_CODE_STRUCT
1539 && TYPE_CODE (t) != TYPE_CODE_UNION)
1540 error ("Attempt to extract a component of a value that is not a %s.", err);
1542 /* Assume it's not, unless we see that it is. */
1543 if (static_memfuncp)
1544 *static_memfuncp =0;
1548 /* if there are no arguments ...do this... */
1550 /* Try as a field first, because if we succeed, there
1551 is less work to be done. */
1552 v = search_struct_field (name, *argp, 0, t, 0);
1556 /* C++: If it was not found as a data field, then try to
1557 return it as a pointer to a method. */
1559 if (destructor_name_p (name, t))
1560 error ("Cannot get value of destructor");
1562 v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
1564 if (v == (value_ptr) -1)
1565 error ("Cannot take address of a method");
1568 if (TYPE_NFN_FIELDS (t))
1569 error ("There is no member or method named %s.", name);
1571 error ("There is no member named %s.", name);
1576 if (destructor_name_p (name, t))
1580 /* destructors are a special case. */
1581 v = value_fn_field (NULL, TYPE_FN_FIELDLIST1 (t, 0),
1582 TYPE_FN_FIELDLIST_LENGTH (t, 0), 0, 0);
1583 if (!v) error("could not find destructor function named %s.", name);
1588 error ("destructor should not have any argument");
1592 v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
1594 if (v == (value_ptr) -1)
1596 error("Argument list of %s mismatch with component in the structure.", name);
1600 /* See if user tried to invoke data as function. If so,
1601 hand it back. If it's not callable (i.e., a pointer to function),
1602 gdb should give an error. */
1603 v = search_struct_field (name, *argp, 0, t, 0);
1607 error ("Structure has no component named %s.", name);
1611 /* C++: return 1 is NAME is a legitimate name for the destructor
1612 of type TYPE. If TYPE does not have a destructor, or
1613 if NAME is inappropriate for TYPE, an error is signaled. */
1615 destructor_name_p (name, type)
1617 const struct type *type;
1619 /* destructors are a special case. */
1623 char *dname = type_name_no_tag (type);
1624 if (!STREQ (dname, name+1))
1625 error ("name of destructor must equal name of class");
1632 /* Helper function for check_field: Given TYPE, a structure/union,
1633 return 1 if the component named NAME from the ultimate
1634 target structure/union is defined, otherwise, return 0. */
1637 check_field_in (type, name)
1638 register struct type *type;
1643 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
1645 char *t_field_name = TYPE_FIELD_NAME (type, i);
1646 if (t_field_name && STREQ (t_field_name, name))
1650 /* C++: If it was not found as a data field, then try to
1651 return it as a pointer to a method. */
1653 /* Destructors are a special case. */
1654 if (destructor_name_p (name, type))
1657 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
1659 if (STREQ (TYPE_FN_FIELDLIST_NAME (type, i), name))
1663 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1664 if (check_field_in (TYPE_BASECLASS (type, i), name))
1671 /* C++: Given ARG1, a value of type (pointer to a)* structure/union,
1672 return 1 if the component named NAME from the ultimate
1673 target structure/union is defined, otherwise, return 0. */
1676 check_field (arg1, name)
1677 register value_ptr arg1;
1680 register struct type *t;
1682 COERCE_ARRAY (arg1);
1684 t = VALUE_TYPE (arg1);
1686 /* Follow pointers until we get to a non-pointer. */
1688 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1689 t = TYPE_TARGET_TYPE (t);
1691 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1692 error ("not implemented: member type in check_field");
1694 if ( TYPE_CODE (t) != TYPE_CODE_STRUCT
1695 && TYPE_CODE (t) != TYPE_CODE_UNION)
1696 error ("Internal error: `this' is not an aggregate");
1698 return check_field_in (t, name);
1701 /* C++: Given an aggregate type CURTYPE, and a member name NAME,
1702 return the address of this member as a "pointer to member"
1703 type. If INTYPE is non-null, then it will be the type
1704 of the member we are looking for. This will help us resolve
1705 "pointers to member functions". This function is used
1706 to resolve user expressions of the form "DOMAIN::NAME". */
1709 value_struct_elt_for_reference (domain, offset, curtype, name, intype)
1710 struct type *domain, *curtype, *intype;
1714 register struct type *t = curtype;
1718 if ( TYPE_CODE (t) != TYPE_CODE_STRUCT
1719 && TYPE_CODE (t) != TYPE_CODE_UNION)
1720 error ("Internal error: non-aggregate type to value_struct_elt_for_reference");
1722 for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
1724 char *t_field_name = TYPE_FIELD_NAME (t, i);
1726 if (t_field_name && STREQ (t_field_name, name))
1728 if (TYPE_FIELD_STATIC (t, i))
1730 char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (t, i);
1731 struct symbol *sym =
1732 lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL);
1734 error ("Internal error: could not find physical static variable named %s",
1736 return value_at (SYMBOL_TYPE (sym),
1737 (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym));
1739 if (TYPE_FIELD_PACKED (t, i))
1740 error ("pointers to bitfield members not allowed");
1742 return value_from_longest
1743 (lookup_reference_type (lookup_member_type (TYPE_FIELD_TYPE (t, i),
1745 offset + (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3));
1749 /* C++: If it was not found as a data field, then try to
1750 return it as a pointer to a method. */
1752 /* Destructors are a special case. */
1753 if (destructor_name_p (name, t))
1755 error ("member pointers to destructors not implemented yet");
1758 /* Perform all necessary dereferencing. */
1759 while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR)
1760 intype = TYPE_TARGET_TYPE (intype);
1762 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
1764 char *t_field_name = TYPE_FN_FIELDLIST_NAME (t, i);
1765 char dem_opname[64];
1767 if (strncmp(t_field_name, "__", 2)==0 ||
1768 strncmp(t_field_name, "op", 2)==0 ||
1769 strncmp(t_field_name, "type", 4)==0 )
1771 if (cplus_demangle_opname(t_field_name, dem_opname, DMGL_ANSI))
1772 t_field_name = dem_opname;
1773 else if (cplus_demangle_opname(t_field_name, dem_opname, 0))
1774 t_field_name = dem_opname;
1776 if (t_field_name && STREQ (t_field_name, name))
1778 int j = TYPE_FN_FIELDLIST_LENGTH (t, i);
1779 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
1781 if (intype == 0 && j > 1)
1782 error ("non-unique member `%s' requires type instantiation", name);
1786 if (TYPE_FN_FIELD_TYPE (f, j) == intype)
1789 error ("no member function matches that type instantiation");
1794 if (TYPE_FN_FIELD_STUB (f, j))
1795 check_stub_method (t, i, j);
1796 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1798 return value_from_longest
1799 (lookup_reference_type
1800 (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
1802 (LONGEST) METHOD_PTR_FROM_VOFFSET
1803 (TYPE_FN_FIELD_VOFFSET (f, j)));
1807 struct symbol *s = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
1808 0, VAR_NAMESPACE, 0, NULL);
1815 v = read_var_value (s, 0);
1817 VALUE_TYPE (v) = lookup_reference_type
1818 (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
1826 for (i = TYPE_N_BASECLASSES (t) - 1; i >= 0; i--)
1831 if (BASETYPE_VIA_VIRTUAL (t, i))
1834 base_offset = TYPE_BASECLASS_BITPOS (t, i) / 8;
1835 v = value_struct_elt_for_reference (domain,
1836 offset + base_offset,
1837 TYPE_BASECLASS (t, i),
1846 /* C++: return the value of the class instance variable, if one exists.
1847 Flag COMPLAIN signals an error if the request is made in an
1848 inappropriate context. */
1850 value_of_this (complain)
1853 extern FRAME selected_frame;
1854 struct symbol *func, *sym;
1857 static const char funny_this[] = "this";
1860 if (selected_frame == 0)
1862 error ("no frame selected");
1865 func = get_frame_function (selected_frame);
1869 error ("no `this' in nameless context");
1873 b = SYMBOL_BLOCK_VALUE (func);
1874 i = BLOCK_NSYMS (b);
1877 error ("no args, no `this'");
1880 /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
1881 symbol instead of the LOC_ARG one (if both exist). */
1882 sym = lookup_block_symbol (b, funny_this, VAR_NAMESPACE);
1886 error ("current stack frame not in method");
1891 this = read_var_value (sym, selected_frame);
1892 if (this == 0 && complain)
1893 error ("`this' argument at unknown address");
1897 /* Create a value for a literal string. We copy data into a local
1898 (NOT inferior's memory) buffer, and then set up an array value.
1900 The array bounds are set from LOWBOUND and HIGHBOUND, and the array is
1901 populated from the values passed in ELEMVEC.
1903 The element type of the array is inherited from the type of the
1904 first element, and all elements must have the same size (though we
1905 don't currently enforce any restriction on their types). */
1908 f77_value_literal_string (lowbound, highbound, elemvec)
1916 register value_ptr val;
1917 struct type *rangetype;
1918 struct type *arraytype;
1921 /* Validate that the bounds are reasonable and that each of the elements
1922 have the same size. */
1924 nelem = highbound - lowbound + 1;
1926 error ("bad array bounds (%d, %d)", lowbound, highbound);
1927 typelength = TYPE_LENGTH (VALUE_TYPE (elemvec[0]));
1928 for (idx = 0; idx < nelem; idx++)
1930 if (TYPE_LENGTH (VALUE_TYPE (elemvec[idx])) != typelength)
1931 error ("array elements must all be the same size");
1934 /* Make sure we are dealing with characters */
1936 if (typelength != 1)
1937 error ("Found a non character type in a literal string ");
1939 /* Allocate space to store the array */
1941 addr = malloc (nelem);
1942 for (idx = 0; idx < nelem; idx++)
1944 memcpy (addr + (idx), VALUE_CONTENTS (elemvec[idx]), 1);
1947 rangetype = create_range_type ((struct type *) NULL, builtin_type_int,
1948 lowbound, highbound);
1950 arraytype = f77_create_literal_string_type ((struct type *) NULL,
1953 val = allocate_value (arraytype);
1955 /* Make sure that this the rest of the world knows that this is
1956 a standard literal string, not one that is a substring of
1959 VALUE_SUBSTRING_START (val) = NULL;
1961 VALUE_LAZY (val) = 0;
1962 VALUE_LITERAL_DATA (val) = addr;
1964 /* Since this is a standard literal string with no real lval,
1965 make sure that value_lval indicates this fact */
1967 VALUE_LVAL (val) = not_lval;
1971 /* Create a value for a substring. We copy data into a local
1972 (NOT inferior's memory) buffer, and then set up an array value.
1974 The array bounds for the string are (1:(to-from +1))
1975 The elements of the string are all characters. */
1978 f77_value_substring (str, from, to)
1984 register value_ptr val;
1985 struct type *rangetype;
1986 struct type *arraytype;
1987 struct internalvar *var;
1990 /* Validate that the bounds are reasonable. */
1992 nelem = to - from + 1;
1994 error ("bad substring bounds (%d, %d)", from, to);
1996 rangetype = create_range_type ((struct type *) NULL, builtin_type_int,
1999 arraytype = f77_create_literal_string_type ((struct type *) NULL,
2002 val = allocate_value (arraytype);
2004 /* Allocate space to store the substring array */
2006 addr = malloc (nelem);
2008 /* Copy over the data */
2010 /* In case we ever try to use this substring on the LHS of an assignment
2011 remember where the SOURCE substring begins, for lval_memory
2012 types this ptr is to a location in legal inferior memory,
2013 for lval_internalvars it is a ptr. to superior memory. This
2014 helps us out later when we do assigments like:
2016 set var ARR(2:3) = 'ab'
2021 if (VALUE_LVAL (str) == lval_memory)
2023 if (VALUE_SUBSTRING_START (str) == NULL)
2025 /* This is a regular lval_memory string located in the
2028 VALUE_SUBSTRING_START (val) = VALUE_ADDRESS (str) + (from - 1);
2029 target_read_memory (VALUE_SUBSTRING_START (val), addr, nelem);
2035 /* str is a substring allocated in the superior. Just
2038 VALUE_SUBSTRING_START(val) = VALUE_LITERAL_DATA(str)+(from - 1);
2039 memcpy(addr,VALUE_SUBSTRING_START(val),nelem);
2041 error ("Cannot get substrings of substrings");
2046 if (VALUE_LVAL(str) == lval_internalvar)
2048 /* Internal variables of type TYPE_CODE_LITERAL_STRING
2049 have their data located in the superior
2050 process not the inferior */
2052 var = VALUE_INTERNALVAR (str);
2054 if (VALUE_SUBSTRING_START (str) == NULL)
2055 VALUE_SUBSTRING_START (val) =
2056 VALUE_LITERAL_DATA (var->value) + (from - 1);
2059 VALUE_SUBSTRING_START(val)=VALUE_LITERAL_DATA(str)+(from -1);
2061 error ("Cannot get substrings of substrings");
2063 memcpy (addr, VALUE_SUBSTRING_START (val), nelem);
2066 error ("Substrings can not be applied to this data item");
2068 VALUE_LAZY (val) = 0;
2069 VALUE_LITERAL_DATA (val) = addr;
2071 /* This literal string's *data* is located in the superior BUT
2072 we do need to know where it came from (i.e. was the source
2073 string an internalvar or a regular lval_memory variable), so
2074 we set the lval field to indicate this. This will be useful
2075 when we use this value on the LHS of an expr. */
2077 VALUE_LVAL (val) = VALUE_LVAL (str);
2081 /* Create a value for a FORTRAN complex number. Currently most of
2082 the time values are coerced to COMPLEX*16 (i.e. a complex number
2083 composed of 2 doubles. This really should be a smarter routine
2084 that figures out precision inteligently as opposed to assuming
2085 doubles. FIXME: fmb */
2088 f77_value_literal_complex (arg1, arg2, size)
2093 struct type *complex_type;
2094 register value_ptr val;
2097 if (size != 8 && size != 16 && size != 32)
2098 error ("Cannot create number of type 'complex*%d'", size);
2100 /* If either value comprising a complex number is a non-floating
2101 type, cast to double. */
2103 if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_FLT)
2104 arg1 = value_cast (builtin_type_f_real_s8, arg1);
2106 if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_FLT)
2107 arg2 = value_cast (builtin_type_f_real_s8, arg2);
2109 complex_type = f77_create_literal_complex_type (VALUE_TYPE (arg1),
2113 val = allocate_value (complex_type);
2115 /* Now create a pointer to enough memory to hold the the two args */
2117 addr = malloc (TYPE_LENGTH (complex_type));
2119 /* Copy over the two components */
2121 memcpy (addr, VALUE_CONTENTS_RAW (arg1), TYPE_LENGTH (VALUE_TYPE (arg1)));
2123 memcpy (addr + TYPE_LENGTH (VALUE_TYPE (arg1)), VALUE_CONTENTS_RAW (arg2),
2124 TYPE_LENGTH (VALUE_TYPE (arg2)));
2126 VALUE_ADDRESS (val) = 0; /* Not located in the inferior */
2127 VALUE_LAZY (val) = 0;
2128 VALUE_LITERAL_DATA (val) = addr;
2130 /* Since this is a literal value, make sure that value_lval indicates
2133 VALUE_LVAL (val) = not_lval;