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