One more time...
[deliverable/binutils-gdb.git] / gdb / valops.c
CommitLineData
bd5635a1 1/* Perform non-arithmetic operations on values, for GDB.
e17960fb 2 Copyright 1986, 1987, 1989, 1991, 1992 Free Software Foundation, Inc.
bd5635a1
RP
3
4This file is part of GDB.
5
06b6c733 6This program is free software; you can redistribute it and/or modify
bd5635a1 7it under the terms of the GNU General Public License as published by
06b6c733
JG
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
bd5635a1 10
06b6c733 11This program is distributed in the hope that it will be useful,
bd5635a1
RP
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
06b6c733
JG
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
bd5635a1 19
bd5635a1 20#include "defs.h"
bd5635a1 21#include "symtab.h"
01be6913 22#include "gdbtypes.h"
bd5635a1
RP
23#include "value.h"
24#include "frame.h"
25#include "inferior.h"
26#include "gdbcore.h"
27#include "target.h"
28
29#include <errno.h>
30
31/* Local functions. */
01be6913
PB
32
33static CORE_ADDR
34find_function_addr PARAMS ((value, struct type **));
35
36static CORE_ADDR
37value_push PARAMS ((CORE_ADDR, value));
38
39static CORE_ADDR
40value_arg_push PARAMS ((CORE_ADDR, value));
41
42static value
43search_struct_field PARAMS ((char *, value, int, struct type *, int));
44
45static value
46search_struct_method PARAMS ((char *, value, value *, int, int *,
47 struct type *));
48
49static int
50check_field_in PARAMS ((struct type *, const char *));
51
bd5635a1
RP
52\f
53/* Cast value ARG2 to type TYPE and return as a value.
54 More general than a C cast: accepts any two types of the same length,
55 and if ARG2 is an lvalue it can be cast into anything at all. */
56/* In C++, casts may change pointer representations. */
57
58value
59value_cast (type, arg2)
60 struct type *type;
61 register value arg2;
62{
63 register enum type_code code1;
64 register enum type_code code2;
65 register int scalar;
66
67 /* Coerce arrays but not enums. Enums will work as-is
68 and coercing them would cause an infinite recursion. */
69 if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_ENUM)
70 COERCE_ARRAY (arg2);
71
72 code1 = TYPE_CODE (type);
73 code2 = TYPE_CODE (VALUE_TYPE (arg2));
74 scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
75 || code2 == TYPE_CODE_ENUM);
76
77 if (code1 == TYPE_CODE_FLT && scalar)
78 return value_from_double (type, value_as_double (arg2));
79 else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM)
80 && (scalar || code2 == TYPE_CODE_PTR))
06b6c733 81 return value_from_longest (type, value_as_long (arg2));
bd5635a1
RP
82 else if (TYPE_LENGTH (type) == TYPE_LENGTH (VALUE_TYPE (arg2)))
83 {
84 if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
85 {
86 /* Look in the type of the source to see if it contains the
87 type of the target as a superclass. If so, we'll need to
88 offset the pointer rather than just change its type. */
89 struct type *t1 = TYPE_TARGET_TYPE (type);
90 struct type *t2 = TYPE_TARGET_TYPE (VALUE_TYPE (arg2));
2a5ec41d 91 if ( TYPE_CODE (t1) == TYPE_CODE_STRUCT
bd5635a1
RP
92 && TYPE_CODE (t2) == TYPE_CODE_STRUCT
93 && TYPE_NAME (t1) != 0) /* if name unknown, can't have supercl */
94 {
95 value v = search_struct_field (type_name_no_tag (t1),
d3bab255 96 value_ind (arg2), 0, t2, 1);
bd5635a1
RP
97 if (v)
98 {
99 v = value_addr (v);
100 VALUE_TYPE (v) = type;
101 return v;
102 }
103 }
104 /* No superclass found, just fall through to change ptr type. */
105 }
106 VALUE_TYPE (arg2) = type;
107 return arg2;
108 }
109 else if (VALUE_LVAL (arg2) == lval_memory)
110 {
111 return value_at_lazy (type, VALUE_ADDRESS (arg2) + VALUE_OFFSET (arg2));
112 }
d11c44f1
JG
113 else if (code1 == TYPE_CODE_VOID)
114 {
115 return value_zero (builtin_type_void, not_lval);
116 }
bd5635a1
RP
117 else
118 {
119 error ("Invalid cast.");
120 return 0;
121 }
122}
123
124/* Create a value of type TYPE that is zero, and return it. */
125
126value
127value_zero (type, lv)
128 struct type *type;
129 enum lval_type lv;
130{
131 register value val = allocate_value (type);
132
8e9a3f3b 133 memset (VALUE_CONTENTS (val), 0, TYPE_LENGTH (type));
bd5635a1
RP
134 VALUE_LVAL (val) = lv;
135
136 return val;
137}
138
139/* Return a value with type TYPE located at ADDR.
140
141 Call value_at only if the data needs to be fetched immediately;
142 if we can be 'lazy' and defer the fetch, perhaps indefinately, call
143 value_at_lazy instead. value_at_lazy simply records the address of
144 the data and sets the lazy-evaluation-required flag. The lazy flag
145 is tested in the VALUE_CONTENTS macro, which is used if and when
146 the contents are actually required. */
147
148value
149value_at (type, addr)
150 struct type *type;
151 CORE_ADDR addr;
152{
153 register value val = allocate_value (type);
154
155 read_memory (addr, VALUE_CONTENTS_RAW (val), TYPE_LENGTH (type));
156
157 VALUE_LVAL (val) = lval_memory;
158 VALUE_ADDRESS (val) = addr;
159
160 return val;
161}
162
163/* Return a lazy value with type TYPE located at ADDR (cf. value_at). */
164
165value
166value_at_lazy (type, addr)
167 struct type *type;
168 CORE_ADDR addr;
169{
170 register value val = allocate_value (type);
171
172 VALUE_LVAL (val) = lval_memory;
173 VALUE_ADDRESS (val) = addr;
174 VALUE_LAZY (val) = 1;
175
176 return val;
177}
178
179/* Called only from the VALUE_CONTENTS macro, if the current data for
180 a variable needs to be loaded into VALUE_CONTENTS(VAL). Fetches the
181 data from the user's process, and clears the lazy flag to indicate
182 that the data in the buffer is valid.
183
9cb602e1
JG
184 If the value is zero-length, we avoid calling read_memory, which would
185 abort. We mark the value as fetched anyway -- all 0 bytes of it.
186
bd5635a1
RP
187 This function returns a value because it is used in the VALUE_CONTENTS
188 macro as part of an expression, where a void would not work. The
189 value is ignored. */
190
191int
192value_fetch_lazy (val)
193 register value val;
194{
195 CORE_ADDR addr = VALUE_ADDRESS (val) + VALUE_OFFSET (val);
196
9cb602e1
JG
197 if (TYPE_LENGTH (VALUE_TYPE (val)))
198 read_memory (addr, VALUE_CONTENTS_RAW (val),
199 TYPE_LENGTH (VALUE_TYPE (val)));
bd5635a1
RP
200 VALUE_LAZY (val) = 0;
201 return 0;
202}
203
204
205/* Store the contents of FROMVAL into the location of TOVAL.
206 Return a new value with the location of TOVAL and contents of FROMVAL. */
207
208value
209value_assign (toval, fromval)
210 register value toval, fromval;
211{
212 register struct type *type = VALUE_TYPE (toval);
213 register value val;
214 char raw_buffer[MAX_REGISTER_RAW_SIZE];
215 char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
216 int use_buffer = 0;
217
218 COERCE_ARRAY (fromval);
8e9a3f3b 219 COERCE_REF (toval);
bd5635a1
RP
220
221 if (VALUE_LVAL (toval) != lval_internalvar)
222 fromval = value_cast (type, fromval);
223
224 /* If TOVAL is a special machine register requiring conversion
225 of program values to a special raw format,
226 convert FROMVAL's contents now, with result in `raw_buffer',
227 and set USE_BUFFER to the number of bytes to write. */
228
229 if (VALUE_REGNO (toval) >= 0
230 && REGISTER_CONVERTIBLE (VALUE_REGNO (toval)))
231 {
232 int regno = VALUE_REGNO (toval);
233 if (VALUE_TYPE (fromval) != REGISTER_VIRTUAL_TYPE (regno))
234 fromval = value_cast (REGISTER_VIRTUAL_TYPE (regno), fromval);
8e9a3f3b 235 memcpy (virtual_buffer, VALUE_CONTENTS (fromval),
bd5635a1
RP
236 REGISTER_VIRTUAL_SIZE (regno));
237 target_convert_from_virtual (regno, virtual_buffer, raw_buffer);
238 use_buffer = REGISTER_RAW_SIZE (regno);
239 }
240
241 switch (VALUE_LVAL (toval))
242 {
243 case lval_internalvar:
244 set_internalvar (VALUE_INTERNALVAR (toval), fromval);
245 break;
246
247 case lval_internalvar_component:
248 set_internalvar_component (VALUE_INTERNALVAR (toval),
249 VALUE_OFFSET (toval),
250 VALUE_BITPOS (toval),
251 VALUE_BITSIZE (toval),
252 fromval);
253 break;
254
255 case lval_memory:
256 if (VALUE_BITSIZE (toval))
257 {
258 int v; /* FIXME, this won't work for large bitfields */
259 read_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
01be6913 260 (char *) &v, sizeof v);
e17960fb 261 modify_field ((char *) &v, (int) value_as_long (fromval),
bd5635a1
RP
262 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
263 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
264 (char *)&v, sizeof v);
265 }
266 else if (use_buffer)
267 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
268 raw_buffer, use_buffer);
269 else
270 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
271 VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
272 break;
273
274 case lval_register:
275 if (VALUE_BITSIZE (toval))
276 {
277 int v;
278
279 read_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
e17960fb
JG
280 (char *) &v, sizeof v);
281 modify_field ((char *) &v, (int) value_as_long (fromval),
bd5635a1
RP
282 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
283 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
e17960fb 284 (char *) &v, sizeof v);
bd5635a1
RP
285 }
286 else if (use_buffer)
287 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
288 raw_buffer, use_buffer);
289 else
290 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
291 VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
292 break;
293
294 case lval_reg_frame_relative:
295 {
296 /* value is stored in a series of registers in the frame
297 specified by the structure. Copy that value out, modify
298 it, and copy it back in. */
299 int amount_to_copy = (VALUE_BITSIZE (toval) ? 1 : TYPE_LENGTH (type));
300 int reg_size = REGISTER_RAW_SIZE (VALUE_FRAME_REGNUM (toval));
301 int byte_offset = VALUE_OFFSET (toval) % reg_size;
302 int reg_offset = VALUE_OFFSET (toval) / reg_size;
303 int amount_copied;
304 char *buffer = (char *) alloca (amount_to_copy);
305 int regno;
306 FRAME frame;
307
308 /* Figure out which frame this is in currently. */
309 for (frame = get_current_frame ();
310 frame && FRAME_FP (frame) != VALUE_FRAME (toval);
311 frame = get_prev_frame (frame))
312 ;
313
314 if (!frame)
315 error ("Value being assigned to is no longer active.");
316
317 amount_to_copy += (reg_size - amount_to_copy % reg_size);
318
319 /* Copy it out. */
320 for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset,
321 amount_copied = 0);
322 amount_copied < amount_to_copy;
323 amount_copied += reg_size, regno++)
324 {
325 get_saved_register (buffer + amount_copied,
326 (int *)NULL, (CORE_ADDR)NULL,
327 frame, regno, (enum lval_type *)NULL);
328 }
329
330 /* Modify what needs to be modified. */
331 if (VALUE_BITSIZE (toval))
332 modify_field (buffer + byte_offset,
333 (int) value_as_long (fromval),
334 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
335 else if (use_buffer)
336 bcopy (raw_buffer, buffer + byte_offset, use_buffer);
337 else
338 bcopy (VALUE_CONTENTS (fromval), buffer + byte_offset,
339 TYPE_LENGTH (type));
340
341 /* Copy it back. */
342 for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset,
343 amount_copied = 0);
344 amount_copied < amount_to_copy;
345 amount_copied += reg_size, regno++)
346 {
347 enum lval_type lval;
348 CORE_ADDR addr;
349 int optim;
350
351 /* Just find out where to put it. */
352 get_saved_register ((char *)NULL,
353 &optim, &addr, frame, regno, &lval);
354
355 if (optim)
356 error ("Attempt to assign to a value that was optimized out.");
357 if (lval == lval_memory)
358 write_memory (addr, buffer + amount_copied, reg_size);
359 else if (lval == lval_register)
360 write_register_bytes (addr, buffer + amount_copied, reg_size);
361 else
362 error ("Attempt to assign to an unmodifiable value.");
363 }
364 }
365 break;
366
367
368 default:
369 error ("Left side of = operation is not an lvalue.");
370 }
371
372 /* Return a value just like TOVAL except with the contents of FROMVAL
373 (except in the case of the type if TOVAL is an internalvar). */
374
375 if (VALUE_LVAL (toval) == lval_internalvar
376 || VALUE_LVAL (toval) == lval_internalvar_component)
377 {
378 type = VALUE_TYPE (fromval);
379 }
380
381 val = allocate_value (type);
382 bcopy (toval, val, VALUE_CONTENTS_RAW (val) - (char *) val);
383 bcopy (VALUE_CONTENTS (fromval), VALUE_CONTENTS_RAW (val), TYPE_LENGTH (type));
384 VALUE_TYPE (val) = type;
385
386 return val;
387}
388
389/* Extend a value VAL to COUNT repetitions of its type. */
390
391value
392value_repeat (arg1, count)
393 value arg1;
394 int count;
395{
396 register value val;
397
398 if (VALUE_LVAL (arg1) != lval_memory)
399 error ("Only values in memory can be extended with '@'.");
400 if (count < 1)
401 error ("Invalid number %d of repetitions.", count);
402
403 val = allocate_repeat_value (VALUE_TYPE (arg1), count);
404
405 read_memory (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1),
406 VALUE_CONTENTS_RAW (val),
407 TYPE_LENGTH (VALUE_TYPE (val)) * count);
408 VALUE_LVAL (val) = lval_memory;
409 VALUE_ADDRESS (val) = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1);
410
411 return val;
412}
413
414value
415value_of_variable (var)
416 struct symbol *var;
417{
418 value val;
419
420 val = read_var_value (var, (FRAME) 0);
421 if (val == 0)
422 error ("Address of symbol \"%s\" is unknown.", SYMBOL_NAME (var));
423 return val;
424}
425
426/* Given a value which is an array, return a value which is
06b6c733
JG
427 a pointer to its first (actually, zeroth) element.
428 FIXME, this should be subtracting the array's lower bound. */
bd5635a1
RP
429
430value
431value_coerce_array (arg1)
432 value arg1;
433{
434 register struct type *type;
bd5635a1
RP
435
436 if (VALUE_LVAL (arg1) != lval_memory)
437 error ("Attempt to take address of value not located in memory.");
438
439 /* Get type of elements. */
440 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_ARRAY)
441 type = TYPE_TARGET_TYPE (VALUE_TYPE (arg1));
442 else
443 /* A phony array made by value_repeat.
444 Its type is the type of the elements, not an array type. */
445 type = VALUE_TYPE (arg1);
446
06b6c733 447 return value_from_longest (lookup_pointer_type (type),
bd5635a1 448 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
bd5635a1
RP
449}
450
451/* Given a value which is a function, return a value which is a pointer
452 to it. */
453
454value
455value_coerce_function (arg1)
456 value arg1;
457{
bd5635a1
RP
458
459 if (VALUE_LVAL (arg1) != lval_memory)
460 error ("Attempt to take address of value not located in memory.");
461
06b6c733 462 return value_from_longest (lookup_pointer_type (VALUE_TYPE (arg1)),
bd5635a1 463 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
bd5635a1
RP
464}
465
466/* Return a pointer value for the object for which ARG1 is the contents. */
467
468value
469value_addr (arg1)
470 value arg1;
471{
8e9a3f3b
PB
472 struct type *type = VALUE_TYPE (arg1);
473 if (TYPE_CODE (type) == TYPE_CODE_REF)
474 {
475 /* Copy the value, but change the type from (T&) to (T*).
476 We keep the same location information, which is efficient,
477 and allows &(&X) to get the location containing the reference. */
478 value arg2 = value_copy (arg1);
479 VALUE_TYPE (arg2) = lookup_pointer_type (TYPE_TARGET_TYPE (type));
480 return arg2;
481 }
bd5635a1 482 if (VALUE_REPEATED (arg1)
8e9a3f3b 483 || TYPE_CODE (type) == TYPE_CODE_ARRAY)
bd5635a1 484 return value_coerce_array (arg1);
8e9a3f3b 485 if (TYPE_CODE (type) == TYPE_CODE_FUNC)
bd5635a1
RP
486 return value_coerce_function (arg1);
487
488 if (VALUE_LVAL (arg1) != lval_memory)
489 error ("Attempt to take address of value not located in memory.");
490
8e9a3f3b 491 return value_from_longest (lookup_pointer_type (type),
bd5635a1 492 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
bd5635a1
RP
493}
494
495/* Given a value of a pointer type, apply the C unary * operator to it. */
496
497value
498value_ind (arg1)
499 value arg1;
500{
501 COERCE_ARRAY (arg1);
502
503 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_MEMBER)
504 error ("not implemented: member types in value_ind");
505
506 /* Allow * on an integer so we can cast it to whatever we want.
507 This returns an int, which seems like the most C-like thing
508 to do. "long long" variables are rare enough that
509 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
510 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT)
511 return value_at (builtin_type_int,
512 (CORE_ADDR) value_as_long (arg1));
513 else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR)
514 return value_at_lazy (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
d11c44f1 515 value_as_pointer (arg1));
bd5635a1
RP
516 error ("Attempt to take contents of a non-pointer value.");
517 return 0; /* For lint -- never reached */
518}
519\f
520/* Pushing small parts of stack frames. */
521
522/* Push one word (the size of object that a register holds). */
523
524CORE_ADDR
525push_word (sp, buffer)
526 CORE_ADDR sp;
527 REGISTER_TYPE buffer;
528{
529 register int len = sizeof (REGISTER_TYPE);
530
f2ebc25f 531 SWAP_TARGET_AND_HOST (&buffer, len);
bd5635a1
RP
532#if 1 INNER_THAN 2
533 sp -= len;
534 write_memory (sp, (char *)&buffer, len);
535#else /* stack grows upward */
536 write_memory (sp, (char *)&buffer, len);
537 sp += len;
538#endif /* stack grows upward */
539
540 return sp;
541}
542
543/* Push LEN bytes with data at BUFFER. */
544
545CORE_ADDR
546push_bytes (sp, buffer, len)
547 CORE_ADDR sp;
548 char *buffer;
549 int len;
550{
551#if 1 INNER_THAN 2
552 sp -= len;
553 write_memory (sp, buffer, len);
554#else /* stack grows upward */
555 write_memory (sp, buffer, len);
556 sp += len;
557#endif /* stack grows upward */
558
559 return sp;
560}
561
562/* Push onto the stack the specified value VALUE. */
563
01be6913 564static CORE_ADDR
bd5635a1
RP
565value_push (sp, arg)
566 register CORE_ADDR sp;
567 value arg;
568{
569 register int len = TYPE_LENGTH (VALUE_TYPE (arg));
570
571#if 1 INNER_THAN 2
572 sp -= len;
573 write_memory (sp, VALUE_CONTENTS (arg), len);
574#else /* stack grows upward */
575 write_memory (sp, VALUE_CONTENTS (arg), len);
576 sp += len;
577#endif /* stack grows upward */
578
579 return sp;
580}
581
582/* Perform the standard coercions that are specified
583 for arguments to be passed to C functions. */
584
585value
586value_arg_coerce (arg)
587 value arg;
588{
589 register struct type *type;
590
591 COERCE_ENUM (arg);
592
593 type = VALUE_TYPE (arg);
594
595 if (TYPE_CODE (type) == TYPE_CODE_INT
2a5ec41d 596 && TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
bd5635a1
RP
597 return value_cast (builtin_type_int, arg);
598
2a5ec41d
JG
599 if (TYPE_CODE (type) == TYPE_CODE_FLT
600 && TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_double))
bd5635a1
RP
601 return value_cast (builtin_type_double, arg);
602
603 return arg;
604}
605
606/* Push the value ARG, first coercing it as an argument
607 to a C function. */
608
01be6913 609static CORE_ADDR
bd5635a1
RP
610value_arg_push (sp, arg)
611 register CORE_ADDR sp;
612 value arg;
613{
614 return value_push (sp, value_arg_coerce (arg));
615}
616
617/* Determine a function's address and its return type from its value.
618 Calls error() if the function is not valid for calling. */
619
01be6913 620static CORE_ADDR
bd5635a1
RP
621find_function_addr (function, retval_type)
622 value function;
623 struct type **retval_type;
624{
625 register struct type *ftype = VALUE_TYPE (function);
626 register enum type_code code = TYPE_CODE (ftype);
627 struct type *value_type;
628 CORE_ADDR funaddr;
629
630 /* If it's a member function, just look at the function
631 part of it. */
632
633 /* Determine address to call. */
634 if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD)
635 {
636 funaddr = VALUE_ADDRESS (function);
637 value_type = TYPE_TARGET_TYPE (ftype);
638 }
639 else if (code == TYPE_CODE_PTR)
640 {
d11c44f1 641 funaddr = value_as_pointer (function);
bd5635a1
RP
642 if (TYPE_CODE (TYPE_TARGET_TYPE (ftype)) == TYPE_CODE_FUNC
643 || TYPE_CODE (TYPE_TARGET_TYPE (ftype)) == TYPE_CODE_METHOD)
644 value_type = TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (ftype));
645 else
646 value_type = builtin_type_int;
647 }
648 else if (code == TYPE_CODE_INT)
649 {
650 /* Handle the case of functions lacking debugging info.
651 Their values are characters since their addresses are char */
652 if (TYPE_LENGTH (ftype) == 1)
d11c44f1 653 funaddr = value_as_pointer (value_addr (function));
bd5635a1
RP
654 else
655 /* Handle integer used as address of a function. */
d11c44f1 656 funaddr = (CORE_ADDR) value_as_long (function);
bd5635a1
RP
657
658 value_type = builtin_type_int;
659 }
660 else
661 error ("Invalid data type for function to be called.");
662
663 *retval_type = value_type;
664 return funaddr;
665}
666
667#if defined (CALL_DUMMY)
668/* All this stuff with a dummy frame may seem unnecessarily complicated
669 (why not just save registers in GDB?). The purpose of pushing a dummy
670 frame which looks just like a real frame is so that if you call a
671 function and then hit a breakpoint (get a signal, etc), "backtrace"
672 will look right. Whether the backtrace needs to actually show the
673 stack at the time the inferior function was called is debatable, but
674 it certainly needs to not display garbage. So if you are contemplating
675 making dummy frames be different from normal frames, consider that. */
676
677/* Perform a function call in the inferior.
678 ARGS is a vector of values of arguments (NARGS of them).
679 FUNCTION is a value, the function to be called.
680 Returns a value representing what the function returned.
681 May fail to return, if a breakpoint or signal is hit
682 during the execution of the function. */
683
684value
685call_function_by_hand (function, nargs, args)
686 value function;
687 int nargs;
688 value *args;
689{
690 register CORE_ADDR sp;
691 register int i;
692 CORE_ADDR start_sp;
f2ebc25f 693 /* CALL_DUMMY is an array of words (REGISTER_TYPE), but each word
84d82b1c 694 is in host byte order. It is switched to target byte order before calling
f2ebc25f 695 FIX_CALL_DUMMY. */
bd5635a1
RP
696 static REGISTER_TYPE dummy[] = CALL_DUMMY;
697 REGISTER_TYPE dummy1[sizeof dummy / sizeof (REGISTER_TYPE)];
698 CORE_ADDR old_sp;
699 struct type *value_type;
700 unsigned char struct_return;
701 CORE_ADDR struct_addr;
702 struct inferior_status inf_status;
703 struct cleanup *old_chain;
704 CORE_ADDR funaddr;
705 int using_gcc;
706
e17960fb
JG
707 if (!target_has_execution)
708 noprocess();
709
bd5635a1
RP
710 save_inferior_status (&inf_status, 1);
711 old_chain = make_cleanup (restore_inferior_status, &inf_status);
712
713 /* PUSH_DUMMY_FRAME is responsible for saving the inferior registers
714 (and POP_FRAME for restoring them). (At least on most machines)
715 they are saved on the stack in the inferior. */
716 PUSH_DUMMY_FRAME;
717
718 old_sp = sp = read_register (SP_REGNUM);
719
720#if 1 INNER_THAN 2 /* Stack grows down */
721 sp -= sizeof dummy;
722 start_sp = sp;
723#else /* Stack grows up */
724 start_sp = sp;
725 sp += sizeof dummy;
726#endif
727
728 funaddr = find_function_addr (function, &value_type);
729
730 {
731 struct block *b = block_for_pc (funaddr);
732 /* If compiled without -g, assume GCC. */
733 using_gcc = b == NULL || BLOCK_GCC_COMPILED (b);
734 }
735
736 /* Are we returning a value using a structure return or a normal
737 value return? */
738
739 struct_return = using_struct_return (function, funaddr, value_type,
740 using_gcc);
741
742 /* Create a call sequence customized for this function
743 and the number of arguments for it. */
744 bcopy (dummy, dummy1, sizeof dummy);
f2ebc25f
JK
745 for (i = 0; i < sizeof dummy / sizeof (REGISTER_TYPE); i++)
746 SWAP_TARGET_AND_HOST (&dummy1[i], sizeof (REGISTER_TYPE));
bd5635a1
RP
747 FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, args,
748 value_type, using_gcc);
749
750#if CALL_DUMMY_LOCATION == ON_STACK
751 write_memory (start_sp, (char *)dummy1, sizeof dummy);
752
753#else /* Not on stack. */
754#if CALL_DUMMY_LOCATION == BEFORE_TEXT_END
755 /* Convex Unix prohibits executing in the stack segment. */
756 /* Hope there is empty room at the top of the text segment. */
757 {
84d82b1c 758 extern CORE_ADDR text_end;
bd5635a1
RP
759 static checked = 0;
760 if (!checked)
761 for (start_sp = text_end - sizeof dummy; start_sp < text_end; ++start_sp)
762 if (read_memory_integer (start_sp, 1) != 0)
763 error ("text segment full -- no place to put call");
764 checked = 1;
765 sp = old_sp;
766 start_sp = text_end - sizeof dummy;
767 write_memory (start_sp, (char *)dummy1, sizeof dummy);
768 }
769#else /* After text_end. */
770 {
84d82b1c 771 extern CORE_ADDR text_end;
bd5635a1
RP
772 int errcode;
773 sp = old_sp;
774 start_sp = text_end;
775 errcode = target_write_memory (start_sp, (char *)dummy1, sizeof dummy);
776 if (errcode != 0)
777 error ("Cannot write text segment -- call_function failed");
778 }
779#endif /* After text_end. */
780#endif /* Not on stack. */
781
782#ifdef lint
783 sp = old_sp; /* It really is used, for some ifdef's... */
784#endif
785
786#ifdef STACK_ALIGN
787 /* If stack grows down, we must leave a hole at the top. */
788 {
789 int len = 0;
790
791 /* Reserve space for the return structure to be written on the
792 stack, if necessary */
793
794 if (struct_return)
795 len += TYPE_LENGTH (value_type);
796
797 for (i = nargs - 1; i >= 0; i--)
798 len += TYPE_LENGTH (VALUE_TYPE (value_arg_coerce (args[i])));
799#ifdef CALL_DUMMY_STACK_ADJUST
800 len += CALL_DUMMY_STACK_ADJUST;
801#endif
802#if 1 INNER_THAN 2
803 sp -= STACK_ALIGN (len) - len;
804#else
805 sp += STACK_ALIGN (len) - len;
806#endif
807 }
808#endif /* STACK_ALIGN */
809
810 /* Reserve space for the return structure to be written on the
811 stack, if necessary */
812
813 if (struct_return)
814 {
815#if 1 INNER_THAN 2
816 sp -= TYPE_LENGTH (value_type);
817 struct_addr = sp;
818#else
819 struct_addr = sp;
820 sp += TYPE_LENGTH (value_type);
821#endif
822 }
823
824#if defined (REG_STRUCT_HAS_ADDR)
825 {
826 /* This is a machine like the sparc, where we need to pass a pointer
827 to the structure, not the structure itself. */
828 if (REG_STRUCT_HAS_ADDR (using_gcc))
829 for (i = nargs - 1; i >= 0; i--)
830 if (TYPE_CODE (VALUE_TYPE (args[i])) == TYPE_CODE_STRUCT)
831 {
832 CORE_ADDR addr;
833#if !(1 INNER_THAN 2)
834 /* The stack grows up, so the address of the thing we push
835 is the stack pointer before we push it. */
836 addr = sp;
837#endif
838 /* Push the structure. */
839 sp = value_push (sp, args[i]);
840#if 1 INNER_THAN 2
841 /* The stack grows down, so the address of the thing we push
842 is the stack pointer after we push it. */
843 addr = sp;
844#endif
845 /* The value we're going to pass is the address of the thing
846 we just pushed. */
06b6c733
JG
847 args[i] = value_from_longest (lookup_pointer_type (value_type),
848 (LONGEST) addr);
bd5635a1
RP
849 }
850 }
851#endif /* REG_STRUCT_HAS_ADDR. */
852
853#ifdef PUSH_ARGUMENTS
854 PUSH_ARGUMENTS(nargs, args, sp, struct_return, struct_addr);
855#else /* !PUSH_ARGUMENTS */
856 for (i = nargs - 1; i >= 0; i--)
857 sp = value_arg_push (sp, args[i]);
858#endif /* !PUSH_ARGUMENTS */
859
860#ifdef CALL_DUMMY_STACK_ADJUST
861#if 1 INNER_THAN 2
862 sp -= CALL_DUMMY_STACK_ADJUST;
863#else
864 sp += CALL_DUMMY_STACK_ADJUST;
865#endif
866#endif /* CALL_DUMMY_STACK_ADJUST */
867
868 /* Store the address at which the structure is supposed to be
869 written. Note that this (and the code which reserved the space
870 above) assumes that gcc was used to compile this function. Since
871 it doesn't cost us anything but space and if the function is pcc
872 it will ignore this value, we will make that assumption.
873
874 Also note that on some machines (like the sparc) pcc uses a
875 convention like gcc's. */
876
877 if (struct_return)
878 STORE_STRUCT_RETURN (struct_addr, sp);
879
880 /* Write the stack pointer. This is here because the statements above
881 might fool with it. On SPARC, this write also stores the register
882 window into the right place in the new stack frame, which otherwise
883 wouldn't happen. (See write_inferior_registers in sparc-xdep.c.) */
884 write_register (SP_REGNUM, sp);
885
886 /* Figure out the value returned by the function. */
887 {
888 char retbuf[REGISTER_BYTES];
889
890 /* Execute the stack dummy routine, calling FUNCTION.
891 When it is done, discard the empty frame
892 after storing the contents of all regs into retbuf. */
893 run_stack_dummy (start_sp + CALL_DUMMY_START_OFFSET, retbuf);
894
895 do_cleanups (old_chain);
896
897 return value_being_returned (value_type, retbuf, struct_return);
898 }
899}
900#else /* no CALL_DUMMY. */
901value
902call_function_by_hand (function, nargs, args)
903 value function;
904 int nargs;
905 value *args;
906{
907 error ("Cannot invoke functions on this machine.");
908}
909#endif /* no CALL_DUMMY. */
910\f
911/* Create a value for a string constant:
912 Call the function malloc in the inferior to get space for it,
913 then copy the data into that space
914 and then return the address with type char *.
915 PTR points to the string constant data; LEN is number of characters. */
916
917value
918value_string (ptr, len)
919 char *ptr;
920 int len;
921{
922 register value val;
923 register struct symbol *sym;
924 value blocklen;
925 register char *copy = (char *) alloca (len + 1);
926 char *i = ptr;
927 register char *o = copy, *ibeg = ptr;
928 register int c;
929
930 /* Copy the string into COPY, processing escapes.
84d82b1c 931 We could not conveniently process them in the parser
bd5635a1
RP
932 because the string there wants to be a substring of the input. */
933
934 while (i - ibeg < len)
935 {
936 c = *i++;
937 if (c == '\\')
938 {
939 c = parse_escape (&i);
940 if (c == -1)
941 continue;
942 }
943 *o++ = c;
944 }
945 *o = 0;
946
947 /* Get the length of the string after escapes are processed. */
948
949 len = o - copy;
950
951 /* Find the address of malloc in the inferior. */
952
953 sym = lookup_symbol ("malloc", 0, VAR_NAMESPACE, 0, NULL);
954 if (sym != 0)
955 {
956 if (SYMBOL_CLASS (sym) != LOC_BLOCK)
957 error ("\"malloc\" exists in this program but is not a function.");
958 val = value_of_variable (sym);
959 }
960 else
961 {
01be6913
PB
962 struct minimal_symbol *msymbol;
963 msymbol = lookup_minimal_symbol ("malloc", (struct objfile *) NULL);
964 if (msymbol != NULL)
965 val =
966 value_from_longest (lookup_pointer_type (lookup_function_type (
967 lookup_pointer_type (builtin_type_char))),
968 (LONGEST) msymbol -> address);
bd5635a1
RP
969 else
970 error ("String constants require the program to have a function \"malloc\".");
971 }
972
06b6c733 973 blocklen = value_from_longest (builtin_type_int, (LONGEST) (len + 1));
e17960fb 974 val = call_function_by_hand (val, 1, &blocklen);
bd5635a1
RP
975 if (value_zerop (val))
976 error ("No memory available for string constant.");
d11c44f1 977 write_memory (value_as_pointer (val), copy, len + 1);
bd5635a1
RP
978 VALUE_TYPE (val) = lookup_pointer_type (builtin_type_char);
979 return val;
980}
981\f
982/* Helper function used by value_struct_elt to recurse through baseclasses.
983 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
2a5ec41d 984 and search in it assuming it has (class) type TYPE.
d3bab255
JK
985 If found, return value, else return NULL.
986
987 If LOOKING_FOR_BASECLASS, then instead of looking for struct fields,
988 look for a baseclass named NAME. */
bd5635a1
RP
989
990static value
d3bab255 991search_struct_field (name, arg1, offset, type, looking_for_baseclass)
bd5635a1
RP
992 char *name;
993 register value arg1;
994 int offset;
995 register struct type *type;
d3bab255 996 int looking_for_baseclass;
bd5635a1
RP
997{
998 int i;
999
1000 check_stub_type (type);
1001
d3bab255
JK
1002 if (! looking_for_baseclass)
1003 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
1004 {
1005 char *t_field_name = TYPE_FIELD_NAME (type, i);
1006
1007 if (t_field_name && !strcmp (t_field_name, name))
1008 {
01be6913
PB
1009 value v;
1010 if (TYPE_FIELD_STATIC (type, i))
1011 {
1012 char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, i);
1013 struct symbol *sym =
1014 lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL);
1015 if (! sym) error (
1016 "Internal error: could not find physical static variable named %s",
1017 phys_name);
1018 v = value_at (TYPE_FIELD_TYPE (type, i),
1019 (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym));
1020 }
1021 else
1022 v = value_primitive_field (arg1, offset, i, type);
d3bab255
JK
1023 if (v == 0)
1024 error("there is no field named %s", name);
1025 return v;
1026 }
1027 }
bd5635a1
RP
1028
1029 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1030 {
1031 value v;
1032 /* If we are looking for baseclasses, this is what we get when we
1033 hit them. */
d3bab255
JK
1034 int found_baseclass = (looking_for_baseclass
1035 && !strcmp (name, TYPE_BASECLASS_NAME (type, i)));
bd5635a1
RP
1036
1037 if (BASETYPE_VIA_VIRTUAL (type, i))
1038 {
1039 value v2;
d11c44f1
JG
1040 baseclass_addr (type, i, VALUE_CONTENTS (arg1) + offset,
1041 &v2, (int *)NULL);
bd5635a1
RP
1042 if (v2 == 0)
1043 error ("virtual baseclass botch");
1044 if (found_baseclass)
1045 return v2;
d3bab255
JK
1046 v = search_struct_field (name, v2, 0, TYPE_BASECLASS (type, i),
1047 looking_for_baseclass);
bd5635a1 1048 }
01be6913 1049 else if (found_baseclass)
bd5635a1
RP
1050 v = value_primitive_field (arg1, offset, i, type);
1051 else
1052 v = search_struct_field (name, arg1,
1053 offset + TYPE_BASECLASS_BITPOS (type, i) / 8,
d3bab255
JK
1054 TYPE_BASECLASS (type, i),
1055 looking_for_baseclass);
bd5635a1
RP
1056 if (v) return v;
1057 }
1058 return NULL;
1059}
1060
1061/* Helper function used by value_struct_elt to recurse through baseclasses.
1062 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
2a5ec41d 1063 and search in it assuming it has (class) type TYPE.
bd5635a1
RP
1064 If found, return value, else return NULL. */
1065
1066static value
1067search_struct_method (name, arg1, args, offset, static_memfuncp, type)
1068 char *name;
1069 register value arg1, *args;
1070 int offset, *static_memfuncp;
1071 register struct type *type;
1072{
1073 int i;
1074
1075 check_stub_type (type);
1076 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
1077 {
1078 char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
1079 if (t_field_name && !strcmp (t_field_name, name))
1080 {
d3bab255 1081 int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1;
bd5635a1
RP
1082 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1083
d3bab255
JK
1084 if (j > 0 && args == 0)
1085 error ("cannot resolve overloaded method `%s'", name);
1086 while (j >= 0)
bd5635a1 1087 {
8e9a3f3b 1088 if (TYPE_FN_FIELD_STUB (f, j))
bd5635a1
RP
1089 check_stub_method (type, i, j);
1090 if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j),
1091 TYPE_FN_FIELD_ARGS (f, j), args))
1092 {
1093 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
e532974c 1094 return (value)value_virtual_fn_field (arg1, f, j, type);
bd5635a1
RP
1095 if (TYPE_FN_FIELD_STATIC_P (f, j) && static_memfuncp)
1096 *static_memfuncp = 1;
7d9884b9 1097 return (value)value_fn_field (f, j);
bd5635a1 1098 }
d3bab255 1099 j--;
bd5635a1
RP
1100 }
1101 }
1102 }
1103
1104 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1105 {
01be6913
PB
1106 value v, v2;
1107 int base_offset;
bd5635a1
RP
1108
1109 if (BASETYPE_VIA_VIRTUAL (type, i))
1110 {
d11c44f1
JG
1111 baseclass_addr (type, i, VALUE_CONTENTS (arg1) + offset,
1112 &v2, (int *)NULL);
bd5635a1
RP
1113 if (v2 == 0)
1114 error ("virtual baseclass botch");
01be6913 1115 base_offset = 0;
bd5635a1 1116 }
01be6913
PB
1117 else
1118 {
1119 v2 = arg1;
1120 base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
1121 }
1122 v = search_struct_method (name, v2, args, base_offset,
bd5635a1
RP
1123 static_memfuncp, TYPE_BASECLASS (type, i));
1124 if (v) return v;
1125 }
1126 return NULL;
1127}
1128
1129/* Given *ARGP, a value of type (pointer to a)* structure/union,
1130 extract the component named NAME from the ultimate target structure/union
1131 and return it as a value with its appropriate type.
1132 ERR is used in the error message if *ARGP's type is wrong.
1133
1134 C++: ARGS is a list of argument types to aid in the selection of
1135 an appropriate method. Also, handle derived types.
1136
1137 STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
1138 where the truthvalue of whether the function that was resolved was
1139 a static member function or not is stored.
1140
1141 ERR is an error message to be printed in case the field is not found. */
1142
1143value
1144value_struct_elt (argp, args, name, static_memfuncp, err)
1145 register value *argp, *args;
1146 char *name;
1147 int *static_memfuncp;
1148 char *err;
1149{
1150 register struct type *t;
bd5635a1
RP
1151 value v;
1152
1153 COERCE_ARRAY (*argp);
1154
1155 t = VALUE_TYPE (*argp);
1156
1157 /* Follow pointers until we get to a non-pointer. */
1158
1159 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1160 {
bd5635a1 1161 *argp = value_ind (*argp);
f2ebc25f
JK
1162 /* Don't coerce fn pointer to fn and then back again! */
1163 if (TYPE_CODE (VALUE_TYPE (*argp)) != TYPE_CODE_FUNC)
1164 COERCE_ARRAY (*argp);
bd5635a1
RP
1165 t = VALUE_TYPE (*argp);
1166 }
1167
1168 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1169 error ("not implemented: member type in value_struct_elt");
1170
2a5ec41d 1171 if ( TYPE_CODE (t) != TYPE_CODE_STRUCT
bd5635a1
RP
1172 && TYPE_CODE (t) != TYPE_CODE_UNION)
1173 error ("Attempt to extract a component of a value that is not a %s.", err);
1174
1175 /* Assume it's not, unless we see that it is. */
1176 if (static_memfuncp)
1177 *static_memfuncp =0;
1178
1179 if (!args)
1180 {
1181 /* if there are no arguments ...do this... */
1182
d3bab255 1183 /* Try as a field first, because if we succeed, there
bd5635a1 1184 is less work to be done. */
d3bab255 1185 v = search_struct_field (name, *argp, 0, t, 0);
bd5635a1
RP
1186 if (v)
1187 return v;
1188
1189 /* C++: If it was not found as a data field, then try to
1190 return it as a pointer to a method. */
1191
1192 if (destructor_name_p (name, t))
1193 error ("Cannot get value of destructor");
1194
1195 v = search_struct_method (name, *argp, args, 0, static_memfuncp, t);
1196
1197 if (v == 0)
1198 {
1199 if (TYPE_NFN_FIELDS (t))
1200 error ("There is no member or method named %s.", name);
1201 else
1202 error ("There is no member named %s.", name);
1203 }
1204 return v;
1205 }
1206
1207 if (destructor_name_p (name, t))
1208 {
1209 if (!args[1])
1210 {
1211 /* destructors are a special case. */
7d9884b9 1212 return (value)value_fn_field (TYPE_FN_FIELDLIST1 (t, 0),
bd5635a1
RP
1213 TYPE_FN_FIELDLIST_LENGTH (t, 0));
1214 }
1215 else
1216 {
1217 error ("destructor should not have any argument");
1218 }
1219 }
1220 else
1221 v = search_struct_method (name, *argp, args, 0, static_memfuncp, t);
1222
1223 if (v == 0)
1224 {
1225 /* See if user tried to invoke data as function. If so,
1226 hand it back. If it's not callable (i.e., a pointer to function),
1227 gdb should give an error. */
d3bab255 1228 v = search_struct_field (name, *argp, 0, t, 0);
bd5635a1
RP
1229 }
1230
1231 if (!v)
1232 error ("Structure has no component named %s.", name);
1233 return v;
1234}
1235
1236/* C++: return 1 is NAME is a legitimate name for the destructor
1237 of type TYPE. If TYPE does not have a destructor, or
1238 if NAME is inappropriate for TYPE, an error is signaled. */
1239int
1240destructor_name_p (name, type)
7919c3ed
JG
1241 const char *name;
1242 const struct type *type;
bd5635a1
RP
1243{
1244 /* destructors are a special case. */
1245
1246 if (name[0] == '~')
1247 {
1248 char *dname = type_name_no_tag (type);
bd5635a1
RP
1249 if (strcmp (dname, name+1))
1250 error ("name of destructor must equal name of class");
1251 else
1252 return 1;
1253 }
1254 return 0;
1255}
1256
1257/* Helper function for check_field: Given TYPE, a structure/union,
1258 return 1 if the component named NAME from the ultimate
1259 target structure/union is defined, otherwise, return 0. */
1260
1261static int
1262check_field_in (type, name)
1263 register struct type *type;
01be6913 1264 const char *name;
bd5635a1
RP
1265{
1266 register int i;
1267
1268 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
1269 {
1270 char *t_field_name = TYPE_FIELD_NAME (type, i);
1271 if (t_field_name && !strcmp (t_field_name, name))
1272 return 1;
1273 }
1274
1275 /* C++: If it was not found as a data field, then try to
1276 return it as a pointer to a method. */
1277
1278 /* Destructors are a special case. */
1279 if (destructor_name_p (name, type))
1280 return 1;
1281
1282 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
1283 {
1284 if (!strcmp (TYPE_FN_FIELDLIST_NAME (type, i), name))
1285 return 1;
1286 }
1287
1288 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1289 if (check_field_in (TYPE_BASECLASS (type, i), name))
1290 return 1;
1291
1292 return 0;
1293}
1294
1295
1296/* C++: Given ARG1, a value of type (pointer to a)* structure/union,
1297 return 1 if the component named NAME from the ultimate
1298 target structure/union is defined, otherwise, return 0. */
1299
1300int
1301check_field (arg1, name)
01be6913 1302 register value arg1;
7919c3ed 1303 const char *name;
bd5635a1
RP
1304{
1305 register struct type *t;
1306
1307 COERCE_ARRAY (arg1);
1308
1309 t = VALUE_TYPE (arg1);
1310
1311 /* Follow pointers until we get to a non-pointer. */
1312
1313 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1314 t = TYPE_TARGET_TYPE (t);
1315
1316 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1317 error ("not implemented: member type in check_field");
1318
2a5ec41d 1319 if ( TYPE_CODE (t) != TYPE_CODE_STRUCT
bd5635a1
RP
1320 && TYPE_CODE (t) != TYPE_CODE_UNION)
1321 error ("Internal error: `this' is not an aggregate");
1322
1323 return check_field_in (t, name);
1324}
1325
01be6913 1326/* C++: Given an aggregate type CURTYPE, and a member name NAME,
2a5ec41d 1327 return the address of this member as a "pointer to member"
bd5635a1
RP
1328 type. If INTYPE is non-null, then it will be the type
1329 of the member we are looking for. This will help us resolve
01be6913
PB
1330 "pointers to member functions". This function is used
1331 to resolve user expressions of the form "DOMAIN::NAME". */
bd5635a1
RP
1332
1333value
01be6913
PB
1334value_struct_elt_for_reference (domain, curtype, name, intype)
1335 struct type *domain, *curtype, *intype;
bd5635a1
RP
1336 char *name;
1337{
01be6913 1338 register struct type *t = curtype;
bd5635a1
RP
1339 register int i;
1340 value v;
1341
2a5ec41d 1342 if ( TYPE_CODE (t) != TYPE_CODE_STRUCT
bd5635a1 1343 && TYPE_CODE (t) != TYPE_CODE_UNION)
01be6913 1344 error ("Internal error: non-aggregate type to value_struct_elt_for_reference");
bd5635a1 1345
01be6913 1346 for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
bd5635a1 1347 {
01be6913
PB
1348 char *t_field_name = TYPE_FIELD_NAME (t, i);
1349
1350 if (t_field_name && !strcmp (t_field_name, name))
bd5635a1 1351 {
01be6913 1352 if (TYPE_FIELD_STATIC (t, i))
bd5635a1 1353 {
01be6913
PB
1354 char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (t, i);
1355 struct symbol *sym =
1356 lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL);
1357 if (! sym)
1358 error (
1359 "Internal error: could not find physical static variable named %s",
1360 phys_name);
1361 return value_at (SYMBOL_TYPE (sym),
1362 (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym));
bd5635a1 1363 }
01be6913
PB
1364 if (TYPE_FIELD_PACKED (t, i))
1365 error ("pointers to bitfield members not allowed");
1366
1367 return value_from_longest
1368 (lookup_reference_type (lookup_member_type (TYPE_FIELD_TYPE (t, i),
1369 domain)),
1370 (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3));
bd5635a1 1371 }
bd5635a1
RP
1372 }
1373
1374 /* C++: If it was not found as a data field, then try to
1375 return it as a pointer to a method. */
bd5635a1
RP
1376
1377 /* Destructors are a special case. */
1378 if (destructor_name_p (name, t))
1379 {
2a5ec41d 1380 error ("member pointers to destructors not implemented yet");
bd5635a1
RP
1381 }
1382
1383 /* Perform all necessary dereferencing. */
1384 while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR)
1385 intype = TYPE_TARGET_TYPE (intype);
1386
01be6913 1387 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
bd5635a1 1388 {
01be6913 1389 if (!strcmp (TYPE_FN_FIELDLIST_NAME (t, i), name))
bd5635a1 1390 {
01be6913
PB
1391 int j = TYPE_FN_FIELDLIST_LENGTH (t, i);
1392 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
1393
1394 if (intype == 0 && j > 1)
1395 error ("non-unique member `%s' requires type instantiation", name);
1396 if (intype)
bd5635a1 1397 {
01be6913
PB
1398 while (j--)
1399 if (TYPE_FN_FIELD_TYPE (f, j) == intype)
1400 break;
1401 if (j < 0)
1402 error ("no member function matches that type instantiation");
1403 }
1404 else
1405 j = 0;
1406
1407 if (TYPE_FN_FIELD_STUB (f, j))
1408 check_stub_method (t, i, j);
1409 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1410 {
1411 return value_from_longest
1412 (lookup_reference_type
1413 (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
1414 domain)),
1415 (LONGEST) TYPE_FN_FIELD_VOFFSET (f, j));
1416 }
1417 else
1418 {
1419 struct symbol *s = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
1420 0, VAR_NAMESPACE, 0, NULL);
1421 v = read_var_value (s, 0);
1422#if 0
1423 VALUE_TYPE (v) = lookup_reference_type
1424 (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
1425 domain));
1426#endif
1427 return v;
bd5635a1 1428 }
bd5635a1
RP
1429 }
1430 }
1431
01be6913
PB
1432 for (i = TYPE_N_BASECLASSES (t) - 1; i >= 0; i--)
1433 {
1434 v = value_struct_elt_for_reference (domain,
1435 TYPE_BASECLASS (t, i),
1436 name,
1437 intype);
1438 if (v)
1439 return v;
bd5635a1
RP
1440 }
1441 return 0;
1442}
1443
1444/* Compare two argument lists and return the position in which they differ,
1445 or zero if equal.
1446
1447 STATICP is nonzero if the T1 argument list came from a
1448 static member function.
1449
1450 For non-static member functions, we ignore the first argument,
1451 which is the type of the instance variable. This is because we want
1452 to handle calls with objects from derived classes. This is not
1453 entirely correct: we should actually check to make sure that a
1454 requested operation is type secure, shouldn't we? FIXME. */
1455
1456int
1457typecmp (staticp, t1, t2)
1458 int staticp;
1459 struct type *t1[];
1460 value t2[];
1461{
1462 int i;
1463
d3bab255
JK
1464 if (t2 == 0)
1465 return 1;
bd5635a1
RP
1466 if (staticp && t1 == 0)
1467 return t2[1] != 0;
1468 if (t1 == 0)
1469 return 1;
1470 if (t1[0]->code == TYPE_CODE_VOID) return 0;
1471 if (t1[!staticp] == 0) return 0;
1472 for (i = !staticp; t1[i] && t1[i]->code != TYPE_CODE_VOID; i++)
1473 {
1474 if (! t2[i]
1475 || t1[i]->code != t2[i]->type->code
1476/* Too pessimistic: || t1[i]->target_type != t2[i]->type->target_type */
1477 )
1478 return i+1;
1479 }
1480 if (!t1[i]) return 0;
1481 return t2[i] ? i+1 : 0;
1482}
1483
1484/* C++: return the value of the class instance variable, if one exists.
1485 Flag COMPLAIN signals an error if the request is made in an
1486 inappropriate context. */
1487value
1488value_of_this (complain)
1489 int complain;
1490{
1491 extern FRAME selected_frame;
1492 struct symbol *func, *sym;
1493 struct block *b;
1494 int i;
1495 static const char funny_this[] = "this";
1496 value this;
bd5635a1
RP
1497
1498 if (selected_frame == 0)
1499 if (complain)
1500 error ("no frame selected");
1501 else return 0;
1502
1503 func = get_frame_function (selected_frame);
1504 if (!func)
1505 {
1506 if (complain)
1507 error ("no `this' in nameless context");
1508 else return 0;
1509 }
1510
1511 b = SYMBOL_BLOCK_VALUE (func);
1512 i = BLOCK_NSYMS (b);
1513 if (i <= 0)
1514 if (complain)
1515 error ("no args, no `this'");
1516 else return 0;
1517
1518 /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
1519 symbol instead of the LOC_ARG one (if both exist). */
1520 sym = lookup_block_symbol (b, funny_this, VAR_NAMESPACE);
1521 if (sym == NULL)
1522 {
1523 if (complain)
1524 error ("current stack frame not in method");
1525 else
1526 return NULL;
1527 }
1528
1529 this = read_var_value (sym, selected_frame);
1530 if (this == 0 && complain)
1531 error ("`this' argument at unknown address");
1532 return this;
1533}
This page took 0.121317 seconds and 4 git commands to generate.