* values.c (check_stub_method): Do lookup_method_type and stuff
[binutils-gdb.git] / gdb / values.c
1 /* Low level packing and unpacking of values for GDB.
2 Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 GDB is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
9 any later version.
10
11 GDB is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GDB; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include "defs.h"
23 #include "param.h"
24 #include "symtab.h"
25 #include "value.h"
26 #include "gdbcore.h"
27 #include "frame.h"
28 #include "command.h"
29 #include "gdbcmd.h"
30
31 extern char *cplus_demangle ();
32 extern char *cplus_mangle_opname ();
33
34 /* The value-history records all the values printed
35 by print commands during this session. Each chunk
36 records 60 consecutive values. The first chunk on
37 the chain records the most recent values.
38 The total number of values is in value_history_count. */
39
40 #define VALUE_HISTORY_CHUNK 60
41
42 struct value_history_chunk
43 {
44 struct value_history_chunk *next;
45 value values[VALUE_HISTORY_CHUNK];
46 };
47
48 /* Chain of chunks now in use. */
49
50 static struct value_history_chunk *value_history_chain;
51
52 static int value_history_count; /* Abs number of last entry stored */
53
54 \f
55 /* List of all value objects currently allocated
56 (except for those released by calls to release_value)
57 This is so they can be freed after each command. */
58
59 static value all_values;
60
61 /* Allocate a value that has the correct length for type TYPE. */
62
63 value
64 allocate_value (type)
65 struct type *type;
66 {
67 register value val;
68
69 check_stub_type (type);
70
71 val = (value) xmalloc (sizeof (struct value) + TYPE_LENGTH (type));
72 VALUE_NEXT (val) = all_values;
73 all_values = val;
74 VALUE_TYPE (val) = type;
75 VALUE_LVAL (val) = not_lval;
76 VALUE_ADDRESS (val) = 0;
77 VALUE_FRAME (val) = 0;
78 VALUE_OFFSET (val) = 0;
79 VALUE_BITPOS (val) = 0;
80 VALUE_BITSIZE (val) = 0;
81 VALUE_REPEATED (val) = 0;
82 VALUE_REPETITIONS (val) = 0;
83 VALUE_REGNO (val) = -1;
84 VALUE_LAZY (val) = 0;
85 VALUE_OPTIMIZED_OUT (val) = 0;
86 return val;
87 }
88
89 /* Allocate a value that has the correct length
90 for COUNT repetitions type TYPE. */
91
92 value
93 allocate_repeat_value (type, count)
94 struct type *type;
95 int count;
96 {
97 register value val;
98
99 val = (value) xmalloc (sizeof (struct value) + TYPE_LENGTH (type) * count);
100 VALUE_NEXT (val) = all_values;
101 all_values = val;
102 VALUE_TYPE (val) = type;
103 VALUE_LVAL (val) = not_lval;
104 VALUE_ADDRESS (val) = 0;
105 VALUE_FRAME (val) = 0;
106 VALUE_OFFSET (val) = 0;
107 VALUE_BITPOS (val) = 0;
108 VALUE_BITSIZE (val) = 0;
109 VALUE_REPEATED (val) = 1;
110 VALUE_REPETITIONS (val) = count;
111 VALUE_REGNO (val) = -1;
112 VALUE_LAZY (val) = 0;
113 VALUE_OPTIMIZED_OUT (val) = 0;
114 return val;
115 }
116
117 /* Return a mark in the value chain. All values allocated after the
118 mark is obtained (except for those released) are subject to being freed
119 if a subsequent value_free_to_mark is passed the mark. */
120 value
121 value_mark ()
122 {
123 return all_values;
124 }
125
126 /* Free all values allocated since MARK was obtained by value_mark
127 (except for those released). */
128 void
129 value_free_to_mark (mark)
130 value mark;
131 {
132 value val, next;
133
134 for (val = all_values; val && val != mark; val = next)
135 {
136 next = VALUE_NEXT (val);
137 value_free (val);
138 }
139 all_values = val;
140 }
141
142 /* Free all the values that have been allocated (except for those released).
143 Called after each command, successful or not. */
144
145 void
146 free_all_values ()
147 {
148 register value val, next;
149
150 for (val = all_values; val; val = next)
151 {
152 next = VALUE_NEXT (val);
153 value_free (val);
154 }
155
156 all_values = 0;
157 }
158
159 /* Remove VAL from the chain all_values
160 so it will not be freed automatically. */
161
162 void
163 release_value (val)
164 register value val;
165 {
166 register value v;
167
168 if (all_values == val)
169 {
170 all_values = val->next;
171 return;
172 }
173
174 for (v = all_values; v; v = v->next)
175 {
176 if (v->next == val)
177 {
178 v->next = val->next;
179 break;
180 }
181 }
182 }
183
184 /* Return a copy of the value ARG.
185 It contains the same contents, for same memory address,
186 but it's a different block of storage. */
187
188 static value
189 value_copy (arg)
190 value arg;
191 {
192 register value val;
193 register struct type *type = VALUE_TYPE (arg);
194 if (VALUE_REPEATED (arg))
195 val = allocate_repeat_value (type, VALUE_REPETITIONS (arg));
196 else
197 val = allocate_value (type);
198 VALUE_LVAL (val) = VALUE_LVAL (arg);
199 VALUE_ADDRESS (val) = VALUE_ADDRESS (arg);
200 VALUE_OFFSET (val) = VALUE_OFFSET (arg);
201 VALUE_BITPOS (val) = VALUE_BITPOS (arg);
202 VALUE_BITSIZE (val) = VALUE_BITSIZE (arg);
203 VALUE_REGNO (val) = VALUE_REGNO (arg);
204 VALUE_LAZY (val) = VALUE_LAZY (arg);
205 if (!VALUE_LAZY (val))
206 {
207 bcopy (VALUE_CONTENTS_RAW (arg), VALUE_CONTENTS_RAW (val),
208 TYPE_LENGTH (VALUE_TYPE (arg))
209 * (VALUE_REPEATED (arg) ? VALUE_REPETITIONS (arg) : 1));
210 }
211 return val;
212 }
213 \f
214 /* Access to the value history. */
215
216 /* Record a new value in the value history.
217 Returns the absolute history index of the entry.
218 Result of -1 indicates the value was not saved; otherwise it is the
219 value history index of this new item. */
220
221 int
222 record_latest_value (val)
223 value val;
224 {
225 int i;
226
227 /* Check error now if about to store an invalid float. We return -1
228 to the caller, but allow them to continue, e.g. to print it as "Nan". */
229 if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FLT) {
230 (void) unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &i);
231 if (i) return -1; /* Indicate value not saved in history */
232 }
233
234 /* Here we treat value_history_count as origin-zero
235 and applying to the value being stored now. */
236
237 i = value_history_count % VALUE_HISTORY_CHUNK;
238 if (i == 0)
239 {
240 register struct value_history_chunk *new
241 = (struct value_history_chunk *)
242 xmalloc (sizeof (struct value_history_chunk));
243 bzero (new->values, sizeof new->values);
244 new->next = value_history_chain;
245 value_history_chain = new;
246 }
247
248 value_history_chain->values[i] = val;
249 release_value (val);
250
251 /* Now we regard value_history_count as origin-one
252 and applying to the value just stored. */
253
254 return ++value_history_count;
255 }
256
257 /* Return a copy of the value in the history with sequence number NUM. */
258
259 value
260 access_value_history (num)
261 int num;
262 {
263 register struct value_history_chunk *chunk;
264 register int i;
265 register int absnum = num;
266
267 if (absnum <= 0)
268 absnum += value_history_count;
269
270 if (absnum <= 0)
271 {
272 if (num == 0)
273 error ("The history is empty.");
274 else if (num == 1)
275 error ("There is only one value in the history.");
276 else
277 error ("History does not go back to $$%d.", -num);
278 }
279 if (absnum > value_history_count)
280 error ("History has not yet reached $%d.", absnum);
281
282 absnum--;
283
284 /* Now absnum is always absolute and origin zero. */
285
286 chunk = value_history_chain;
287 for (i = (value_history_count - 1) / VALUE_HISTORY_CHUNK - absnum / VALUE_HISTORY_CHUNK;
288 i > 0; i--)
289 chunk = chunk->next;
290
291 return value_copy (chunk->values[absnum % VALUE_HISTORY_CHUNK]);
292 }
293
294 /* Clear the value history entirely.
295 Must be done when new symbol tables are loaded,
296 because the type pointers become invalid. */
297
298 void
299 clear_value_history ()
300 {
301 register struct value_history_chunk *next;
302 register int i;
303 register value val;
304
305 while (value_history_chain)
306 {
307 for (i = 0; i < VALUE_HISTORY_CHUNK; i++)
308 if (val = value_history_chain->values[i])
309 free (val);
310 next = value_history_chain->next;
311 free (value_history_chain);
312 value_history_chain = next;
313 }
314 value_history_count = 0;
315 }
316
317 static void
318 show_values (num_exp, from_tty)
319 char *num_exp;
320 int from_tty;
321 {
322 register int i;
323 register value val;
324 static int num = 1;
325
326 if (num_exp)
327 {
328 if (num_exp[0] == '+' && num_exp[1] == '\0')
329 /* "info history +" should print from the stored position. */
330 ;
331 else
332 /* "info history <exp>" should print around value number <exp>. */
333 num = parse_and_eval_address (num_exp) - 5;
334 }
335 else
336 {
337 /* "info history" means print the last 10 values. */
338 num = value_history_count - 9;
339 }
340
341 if (num <= 0)
342 num = 1;
343
344 for (i = num; i < num + 10 && i <= value_history_count; i++)
345 {
346 val = access_value_history (i);
347 printf_filtered ("$%d = ", i);
348 value_print (val, stdout, 0, Val_pretty_default);
349 printf_filtered ("\n");
350 }
351
352 /* The next "info history +" should start after what we just printed. */
353 num += 10;
354
355 /* Hitting just return after this command should do the same thing as
356 "info history +". If num_exp is null, this is unnecessary, since
357 "info history +" is not useful after "info history". */
358 if (from_tty && num_exp)
359 {
360 num_exp[0] = '+';
361 num_exp[1] = '\0';
362 }
363 }
364 \f
365 /* Internal variables. These are variables within the debugger
366 that hold values assigned by debugger commands.
367 The user refers to them with a '$' prefix
368 that does not appear in the variable names stored internally. */
369
370 static struct internalvar *internalvars;
371
372 /* Look up an internal variable with name NAME. NAME should not
373 normally include a dollar sign.
374
375 If the specified internal variable does not exist,
376 one is created, with a void value. */
377
378 struct internalvar *
379 lookup_internalvar (name)
380 char *name;
381 {
382 register struct internalvar *var;
383
384 for (var = internalvars; var; var = var->next)
385 if (!strcmp (var->name, name))
386 return var;
387
388 var = (struct internalvar *) xmalloc (sizeof (struct internalvar));
389 var->name = concat (name, "", "");
390 var->value = allocate_value (builtin_type_void);
391 release_value (var->value);
392 var->next = internalvars;
393 internalvars = var;
394 return var;
395 }
396
397 value
398 value_of_internalvar (var)
399 struct internalvar *var;
400 {
401 register value val;
402
403 #ifdef IS_TRAPPED_INTERNALVAR
404 if (IS_TRAPPED_INTERNALVAR (var->name))
405 return VALUE_OF_TRAPPED_INTERNALVAR (var);
406 #endif
407
408 val = value_copy (var->value);
409 if (VALUE_LAZY (val))
410 value_fetch_lazy (val);
411 VALUE_LVAL (val) = lval_internalvar;
412 VALUE_INTERNALVAR (val) = var;
413 return val;
414 }
415
416 void
417 set_internalvar_component (var, offset, bitpos, bitsize, newval)
418 struct internalvar *var;
419 int offset, bitpos, bitsize;
420 value newval;
421 {
422 register char *addr = VALUE_CONTENTS (var->value) + offset;
423
424 #ifdef IS_TRAPPED_INTERNALVAR
425 if (IS_TRAPPED_INTERNALVAR (var->name))
426 SET_TRAPPED_INTERNALVAR (var, newval, bitpos, bitsize, offset);
427 #endif
428
429 if (bitsize)
430 modify_field (addr, (int) value_as_long (newval),
431 bitpos, bitsize);
432 else
433 bcopy (VALUE_CONTENTS (newval), addr,
434 TYPE_LENGTH (VALUE_TYPE (newval)));
435 }
436
437 void
438 set_internalvar (var, val)
439 struct internalvar *var;
440 value val;
441 {
442 #ifdef IS_TRAPPED_INTERNALVAR
443 if (IS_TRAPPED_INTERNALVAR (var->name))
444 SET_TRAPPED_INTERNALVAR (var, val, 0, 0, 0);
445 #endif
446
447 free (var->value);
448 var->value = value_copy (val);
449 release_value (var->value);
450 }
451
452 char *
453 internalvar_name (var)
454 struct internalvar *var;
455 {
456 return var->name;
457 }
458
459 /* Free all internalvars. Done when new symtabs are loaded,
460 because that makes the values invalid. */
461
462 void
463 clear_internalvars ()
464 {
465 register struct internalvar *var;
466
467 while (internalvars)
468 {
469 var = internalvars;
470 internalvars = var->next;
471 free (var->name);
472 free (var->value);
473 free (var);
474 }
475 }
476
477 static void
478 show_convenience ()
479 {
480 register struct internalvar *var;
481 int varseen = 0;
482
483 for (var = internalvars; var; var = var->next)
484 {
485 #ifdef IS_TRAPPED_INTERNALVAR
486 if (IS_TRAPPED_INTERNALVAR (var->name))
487 continue;
488 #endif
489 if (!varseen)
490 {
491 #if 0
492 /* Useless noise. */
493 printf ("Debugger convenience variables:\n\n");
494 #endif
495 varseen = 1;
496 }
497 printf ("$%s = ", var->name);
498 value_print (var->value, stdout, 0, Val_pretty_default);
499 printf ("\n");
500 }
501 if (!varseen)
502 printf ("No debugger convenience variables now defined.\n\
503 Convenience variables have names starting with \"$\";\n\
504 use \"set\" as in \"set $foo = 5\" to define them.\n");
505 }
506 \f
507 /* Extract a value as a C number (either long or double).
508 Knows how to convert fixed values to double, or
509 floating values to long.
510 Does not deallocate the value. */
511
512 LONGEST
513 value_as_long (val)
514 register value val;
515 {
516 /* This coerces arrays and functions, which is necessary (e.g.
517 in disassemble_command). It also dereferences references, which
518 I suspect is the most logical thing to do. */
519 if (TYPE_CODE (VALUE_TYPE (val)) != TYPE_CODE_ENUM)
520 COERCE_ARRAY (val);
521 return unpack_long (VALUE_TYPE (val), VALUE_CONTENTS (val));
522 }
523
524 double
525 value_as_double (val)
526 register value val;
527 {
528 double foo;
529 int inv;
530
531 foo = unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &inv);
532 if (inv)
533 error ("Invalid floating value found in program.");
534 return foo;
535 }
536 \f
537 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
538 as a long, or as a double, assuming the raw data is described
539 by type TYPE. Knows how to convert different sizes of values
540 and can convert between fixed and floating point. We don't assume
541 any alignment for the raw data. Return value is in host byte order.
542
543 If you want functions and arrays to be coerced to pointers, and
544 references to be dereferenced, call value_as_long() instead.
545
546 C++: It is assumed that the front-end has taken care of
547 all matters concerning pointers to members. A pointer
548 to member which reaches here is considered to be equivalent
549 to an INT (or some size). After all, it is only an offset. */
550
551 LONGEST
552 unpack_long (type, valaddr)
553 struct type *type;
554 char *valaddr;
555 {
556 register enum type_code code = TYPE_CODE (type);
557 register int len = TYPE_LENGTH (type);
558 register int nosign = TYPE_UNSIGNED (type);
559
560 if (code == TYPE_CODE_ENUM)
561 code = TYPE_CODE_INT;
562 if (code == TYPE_CODE_FLT)
563 {
564 if (len == sizeof (float))
565 {
566 float retval;
567 bcopy (valaddr, &retval, sizeof (retval));
568 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
569 return retval;
570 }
571
572 if (len == sizeof (double))
573 {
574 double retval;
575 bcopy (valaddr, &retval, sizeof (retval));
576 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
577 return retval;
578 }
579 else
580 {
581 error ("Unexpected type of floating point number.");
582 }
583 }
584 else if (code == TYPE_CODE_INT && nosign)
585 {
586 if (len == sizeof (char))
587 {
588 unsigned char retval = * (unsigned char *) valaddr;
589 /* SWAP_TARGET_AND_HOST (&retval, sizeof (unsigned char)); */
590 return retval;
591 }
592
593 if (len == sizeof (short))
594 {
595 unsigned short retval;
596 bcopy (valaddr, &retval, sizeof (retval));
597 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
598 return retval;
599 }
600
601 if (len == sizeof (int))
602 {
603 unsigned int retval;
604 bcopy (valaddr, &retval, sizeof (retval));
605 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
606 return retval;
607 }
608
609 if (len == sizeof (long))
610 {
611 unsigned long retval;
612 bcopy (valaddr, &retval, sizeof (retval));
613 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
614 return retval;
615 }
616 #ifdef LONG_LONG
617 if (len == sizeof (long long))
618 {
619 unsigned long long retval;
620 bcopy (valaddr, &retval, sizeof (retval));
621 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
622 return retval;
623 }
624 #endif
625 else
626 {
627 error ("That operation is not possible on an integer of that size.");
628 }
629 }
630 else if (code == TYPE_CODE_INT)
631 {
632 if (len == sizeof (char))
633 {
634 char retval;
635 bcopy (valaddr, &retval, sizeof (retval));
636 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
637 return retval;
638 }
639
640 if (len == sizeof (short))
641 {
642 short retval;
643 bcopy (valaddr, &retval, sizeof (retval));
644 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
645 return retval;
646 }
647
648 if (len == sizeof (int))
649 {
650 int retval;
651 bcopy (valaddr, &retval, sizeof (retval));
652 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
653 return retval;
654 }
655
656 if (len == sizeof (long))
657 {
658 long retval;
659 bcopy (valaddr, &retval, sizeof (retval));
660 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
661 return retval;
662 }
663
664 #ifdef LONG_LONG
665 if (len == sizeof (long long))
666 {
667 long long retval;
668 bcopy (valaddr, &retval, sizeof (retval));
669 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
670 return retval;
671 }
672 #endif
673 else
674 {
675 error ("That operation is not possible on an integer of that size.");
676 }
677 }
678 else if (code == TYPE_CODE_PTR
679 || code == TYPE_CODE_REF)
680 {
681 if (len == sizeof (char *))
682 {
683 CORE_ADDR retval;
684 bcopy (valaddr, &retval, sizeof (retval));
685 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
686 return retval;
687 }
688 }
689 else if (code == TYPE_CODE_MEMBER)
690 error ("not implemented: member types in unpack_long");
691
692 error ("Value not integer or pointer.");
693 return 0; /* For lint -- never reached */
694 }
695
696 /* Return a double value from the specified type and address.
697 INVP points to an int which is set to 0 for valid value,
698 1 for invalid value (bad float format). In either case,
699 the returned double is OK to use. Argument is in target
700 format, result is in host format. */
701
702 double
703 unpack_double (type, valaddr, invp)
704 struct type *type;
705 char *valaddr;
706 int *invp;
707 {
708 register enum type_code code = TYPE_CODE (type);
709 register int len = TYPE_LENGTH (type);
710 register int nosign = TYPE_UNSIGNED (type);
711
712 *invp = 0; /* Assume valid. */
713 if (code == TYPE_CODE_FLT)
714 {
715 if (INVALID_FLOAT (valaddr, len))
716 {
717 *invp = 1;
718 return 1.234567891011121314;
719 }
720
721 if (len == sizeof (float))
722 {
723 float retval;
724 bcopy (valaddr, &retval, sizeof (retval));
725 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
726 return retval;
727 }
728
729 if (len == sizeof (double))
730 {
731 double retval;
732 bcopy (valaddr, &retval, sizeof (retval));
733 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
734 return retval;
735 }
736 else
737 {
738 error ("Unexpected type of floating point number.");
739 }
740 }
741 else if (nosign) {
742 /* Unsigned -- be sure we compensate for signed LONGEST. */
743 #ifdef LONG_LONG
744 return (unsigned long long) unpack_long (type, valaddr);
745 #else
746 return (unsigned long ) unpack_long (type, valaddr);
747 #endif
748 } else {
749 /* Signed -- we are OK with unpack_long. */
750 return unpack_long (type, valaddr);
751 }
752 }
753 \f
754 /* Given a value ARG1 (offset by OFFSET bytes)
755 of a struct or union type ARG_TYPE,
756 extract and return the value of one of its fields.
757 FIELDNO says which field.
758
759 For C++, must also be able to return values from static fields */
760
761 value
762 value_primitive_field (arg1, offset, fieldno, arg_type)
763 register value arg1;
764 int offset;
765 register int fieldno;
766 register struct type *arg_type;
767 {
768 register value v;
769 register struct type *type;
770
771 check_stub_type (arg_type);
772 type = TYPE_FIELD_TYPE (arg_type, fieldno);
773
774 /* Handle packed fields */
775
776 offset += TYPE_FIELD_BITPOS (arg_type, fieldno) / 8;
777 if (TYPE_FIELD_BITSIZE (arg_type, fieldno))
778 {
779 v = value_from_long (type,
780 unpack_field_as_long (arg_type,
781 VALUE_CONTENTS (arg1),
782 fieldno));
783 VALUE_BITPOS (v) = TYPE_FIELD_BITPOS (arg_type, fieldno) % 8;
784 VALUE_BITSIZE (v) = TYPE_FIELD_BITSIZE (arg_type, fieldno);
785 }
786 else
787 {
788 v = allocate_value (type);
789 if (VALUE_LAZY (arg1))
790 VALUE_LAZY (v) = 1;
791 else
792 bcopy (VALUE_CONTENTS_RAW (arg1) + offset,
793 VALUE_CONTENTS_RAW (v),
794 TYPE_LENGTH (type));
795 }
796 VALUE_LVAL (v) = VALUE_LVAL (arg1);
797 if (VALUE_LVAL (arg1) == lval_internalvar)
798 VALUE_LVAL (v) = lval_internalvar_component;
799 VALUE_ADDRESS (v) = VALUE_ADDRESS (arg1);
800 VALUE_OFFSET (v) = offset + VALUE_OFFSET (arg1);
801 return v;
802 }
803
804 /* Given a value ARG1 of a struct or union type,
805 extract and return the value of one of its fields.
806 FIELDNO says which field.
807
808 For C++, must also be able to return values from static fields */
809
810 value
811 value_field (arg1, fieldno)
812 register value arg1;
813 register int fieldno;
814 {
815 return value_primitive_field (arg1, 0, fieldno, VALUE_TYPE (arg1));
816 }
817
818 value
819 value_fn_field (arg1, fieldno, subfieldno)
820 register value arg1;
821 register int fieldno;
822 int subfieldno;
823 {
824 register value v;
825 struct fn_field *f = TYPE_FN_FIELDLIST1 (VALUE_TYPE (arg1), fieldno);
826 register struct type *type = TYPE_FN_FIELD_TYPE (f, subfieldno);
827 struct symbol *sym;
828
829 sym = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, subfieldno),
830 0, VAR_NAMESPACE, 0, NULL);
831 if (! sym) error ("Internal error: could not find physical method named %s",
832 TYPE_FN_FIELD_PHYSNAME (f, subfieldno));
833
834 v = allocate_value (type);
835 VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
836 VALUE_TYPE (v) = type;
837 return v;
838 }
839
840 /* Return a virtual function as a value.
841 ARG1 is the object which provides the virtual function
842 table pointer. ARG1 is side-effected in calling this function.
843 F is the list of member functions which contains the desired virtual
844 function.
845 J is an index into F which provides the desired virtual function.
846
847 TYPE is the type in which F is located. */
848 value
849 value_virtual_fn_field (arg1, f, j, type)
850 value arg1;
851 struct fn_field *f;
852 int j;
853 struct type *type;
854 {
855 /* First, get the virtual function table pointer. That comes
856 with a strange type, so cast it to type `pointer to long' (which
857 should serve just fine as a function type). Then, index into
858 the table, and convert final value to appropriate function type. */
859 value entry, vfn, vtbl;
860 value vi = value_from_long (builtin_type_int,
861 (LONGEST) TYPE_FN_FIELD_VOFFSET (f, j));
862 struct type *fcontext = TYPE_FN_FIELD_FCONTEXT (f, j);
863 struct type *context;
864 if (fcontext == NULL)
865 /* We don't have an fcontext (e.g. the program was compiled with
866 g++ version 1). Try to get the vtbl from the TYPE_VPTR_BASETYPE.
867 This won't work right for multiple inheritance, but at least we
868 should do as well as GDB 3.x did. */
869 fcontext = TYPE_VPTR_BASETYPE (type);
870 context = lookup_pointer_type (fcontext);
871 /* Now context is a pointer to the basetype containing the vtbl. */
872 if (TYPE_TARGET_TYPE (context) != VALUE_TYPE (arg1))
873 arg1 = value_ind (value_cast (context, value_addr (arg1)));
874
875 context = VALUE_TYPE (arg1);
876 /* Now context is the basetype containing the vtbl. */
877
878 /* This type may have been defined before its virtual function table
879 was. If so, fill in the virtual function table entry for the
880 type now. */
881 if (TYPE_VPTR_FIELDNO (context) < 0)
882 fill_in_vptr_fieldno (context);
883
884 /* The virtual function table is now an array of structures
885 which have the form { int16 offset, delta; void *pfn; }. */
886 vtbl = value_ind (value_field (arg1, TYPE_VPTR_FIELDNO (context)));
887
888 /* Index into the virtual function table. This is hard-coded because
889 looking up a field is not cheap, and it may be important to save
890 time, e.g. if the user has set a conditional breakpoint calling
891 a virtual function. */
892 entry = value_subscript (vtbl, vi);
893
894 /* Move the `this' pointer according to the virtual function table. */
895 VALUE_OFFSET (arg1) += value_as_long (value_field (entry, 0));
896 if (! VALUE_LAZY (arg1))
897 {
898 VALUE_LAZY (arg1) = 1;
899 value_fetch_lazy (arg1);
900 }
901
902 vfn = value_field (entry, 2);
903 /* Reinstantiate the function pointer with the correct type. */
904 VALUE_TYPE (vfn) = lookup_pointer_type (TYPE_FN_FIELD_TYPE (f, j));
905
906 return vfn;
907 }
908
909 /* ARG is a pointer to an object we know to be at least
910 a DTYPE. BTYPE is the most derived basetype that has
911 already been searched (and need not be searched again).
912 After looking at the vtables between BTYPE and DTYPE,
913 return the most derived type we find. The caller must
914 be satisfied when the return value == DTYPE.
915
916 FIXME-tiemann: should work with dossier entries as well. */
917
918 static value
919 value_headof (arg, btype, dtype)
920 value arg;
921 struct type *btype, *dtype;
922 {
923 /* First collect the vtables we must look at for this object. */
924 /* FIXME-tiemann: right now, just look at top-most vtable. */
925 value vtbl, entry, best_entry = 0;
926 struct type *entry_type;
927 int i, nelems;
928 int offset, best_offset = 0;
929 struct symbol *sym;
930 CORE_ADDR pc_for_sym;
931 char *demangled_name;
932
933 vtbl = value_ind (value_field (value_ind (arg), TYPE_VPTR_FIELDNO (dtype)));
934
935 /* Check that VTBL looks like it points to a virtual function table. */
936 i = find_pc_misc_function (VALUE_ADDRESS (vtbl));
937 if (i < 0 || ! VTBL_PREFIX_P (misc_function_vector[i].name))
938 {
939 /* If we expected to find a vtable, but did not, let the user
940 know that we aren't happy, but don't throw an error.
941 FIXME: there has to be a better way to do this. */
942 struct type *error_type = (struct type *)xmalloc (sizeof (struct type));
943 bcopy (VALUE_TYPE (arg), error_type, sizeof (struct type));
944 TYPE_NAME (error_type) = savestring ("suspicious *", sizeof ("suspicious *"));
945 VALUE_TYPE (arg) = error_type;
946 return arg;
947 }
948
949 /* Now search through the virtual function table. */
950 entry = value_ind (vtbl);
951 entry_type = VALUE_TYPE (entry);
952 nelems = value_as_long (value_field (entry, 2));
953 for (i = 1; i <= nelems; i++)
954 {
955 entry = value_subscript (vtbl, value_from_long (builtin_type_int, i));
956 offset = value_as_long (value_field (entry, 0));
957 if (offset < best_offset)
958 {
959 best_offset = offset;
960 best_entry = entry;
961 }
962 }
963 if (best_entry == 0)
964 return arg;
965
966 /* Move the pointer according to BEST_ENTRY's offset, and figure
967 out what type we should return as the new pointer. */
968 pc_for_sym = value_as_long (value_field (best_entry, 2));
969 sym = find_pc_function (pc_for_sym);
970 demangled_name = cplus_demangle (SYMBOL_NAME (sym), -1);
971 *(strchr (demangled_name, ':')) = '\0';
972 sym = lookup_symbol (demangled_name, 0, VAR_NAMESPACE, 0, 0);
973 if (sym == 0)
974 error ("could not find type declaration for `%s'", SYMBOL_NAME (sym));
975 free (demangled_name);
976 arg = value_add (value_cast (builtin_type_int, arg),
977 value_field (best_entry, 0));
978 VALUE_TYPE (arg) = lookup_pointer_type (SYMBOL_TYPE (sym));
979 return arg;
980 }
981
982 /* ARG is a pointer object of type TYPE. If TYPE has virtual
983 function tables, probe ARG's tables (including the vtables
984 of its baseclasses) to figure out the most derived type that ARG
985 could actually be a pointer to. */
986
987 value
988 value_from_vtable_info (arg, type)
989 value arg;
990 struct type *type;
991 {
992 /* Take care of preliminaries. */
993 if (TYPE_VPTR_FIELDNO (type) < 0)
994 fill_in_vptr_fieldno (type);
995 if (TYPE_VPTR_FIELDNO (type) < 0 || VALUE_REPEATED (arg))
996 return 0;
997
998 return value_headof (arg, 0, type);
999 }
1000
1001 /* The value of a static class member does not depend
1002 on its instance, only on its type. If FIELDNO >= 0,
1003 then fieldno is a valid field number and is used directly.
1004 Otherwise, FIELDNAME is the name of the field we are
1005 searching for. If it is not a static field name, an
1006 error is signaled. TYPE is the type in which we look for the
1007 static field member.
1008
1009 Return zero if we couldn't find anything; the caller may signal
1010 an error in that case. */
1011
1012 value
1013 value_static_field (type, fieldname, fieldno)
1014 register struct type *type;
1015 char *fieldname;
1016 register int fieldno;
1017 {
1018 register value v;
1019 struct symbol *sym;
1020 char *phys_name;
1021
1022 if (fieldno < 0)
1023 {
1024 register struct type *t = type;
1025 /* Look for static field. */
1026 int i;
1027 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
1028 if (! strcmp (TYPE_FIELD_NAME (type, i), fieldname))
1029 {
1030 if (TYPE_FIELD_STATIC (type, i))
1031 {
1032 fieldno = i;
1033 goto found;
1034 }
1035 else
1036 error ("field `%s' is not static", fieldname);
1037 }
1038 for (; i > 0; i--)
1039 {
1040 v = value_static_field (TYPE_BASECLASS (type, i), fieldname, -1);
1041 if (v != 0)
1042 return v;
1043 }
1044
1045 if (destructor_name_p (fieldname, type))
1046 error ("Cannot get value of destructor");
1047
1048 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
1049 {
1050 if (! strcmp (TYPE_FN_FIELDLIST_NAME (type, i), fieldname))
1051 error ("Cannot get value of method \"%s\"", fieldname);
1052 }
1053 error("there is no field named %s", fieldname);
1054 }
1055
1056 found:
1057 phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, fieldno);
1058 sym = lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL);
1059 if (! sym) error ("Internal error: could not find physical static variable named %s", phys_name);
1060
1061 type = TYPE_FIELD_TYPE (type, fieldno);
1062 v = value_at (type, (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym));
1063 return v;
1064 }
1065
1066 /* Compute the address of the baseclass which is
1067 the INDEXth baseclass of TYPE. The TYPE base
1068 of the object is at VALADDR.
1069
1070 If ERRP is non-NULL, set *ERRP to be the errno code of any error,
1071 or 0 if no error. In that case the return value is not the address
1072 of the baseclasss, but the address which could not be read
1073 successfully. */
1074
1075 char *
1076 baseclass_addr (type, index, valaddr, valuep, errp)
1077 struct type *type;
1078 int index;
1079 char *valaddr;
1080 value *valuep;
1081 int *errp;
1082 {
1083 struct type *basetype = TYPE_BASECLASS (type, index);
1084
1085 if (errp)
1086 *errp = 0;
1087
1088 if (BASETYPE_VIA_VIRTUAL (type, index))
1089 {
1090 /* Must hunt for the pointer to this virtual baseclass. */
1091 register int i, len = TYPE_NFIELDS (type);
1092 register int n_baseclasses = TYPE_N_BASECLASSES (type);
1093 char *vbase_name, *type_name = type_name_no_tag (basetype);
1094
1095 if (TYPE_MAIN_VARIANT (basetype))
1096 basetype = TYPE_MAIN_VARIANT (basetype);
1097
1098 vbase_name = (char *)alloca (strlen (type_name) + 8);
1099 sprintf (vbase_name, "_vb$%s", type_name);
1100 /* First look for the virtual baseclass pointer
1101 in the fields. */
1102 for (i = n_baseclasses; i < len; i++)
1103 {
1104 if (! strcmp (vbase_name, TYPE_FIELD_NAME (type, i)))
1105 {
1106 value val = allocate_value (basetype);
1107 CORE_ADDR addr;
1108 int status;
1109
1110 addr = unpack_long (TYPE_FIELD_TYPE (type, i),
1111 valaddr + (TYPE_FIELD_BITPOS (type, i) / 8));
1112
1113 status = target_read_memory (addr,
1114 VALUE_CONTENTS_RAW (val),
1115 TYPE_LENGTH (type));
1116 VALUE_LVAL (val) = lval_memory;
1117 VALUE_ADDRESS (val) = addr;
1118
1119 if (status != 0)
1120 {
1121 if (valuep)
1122 *valuep = NULL;
1123 release_value (val);
1124 value_free (val);
1125 if (errp)
1126 *errp = status;
1127 return (char *)addr;
1128 }
1129 else
1130 {
1131 if (valuep)
1132 *valuep = val;
1133 return (char *) VALUE_CONTENTS (val);
1134 }
1135 }
1136 }
1137 /* Not in the fields, so try looking through the baseclasses. */
1138 for (i = index+1; i < n_baseclasses; i++)
1139 {
1140 char *baddr;
1141
1142 baddr = baseclass_addr (type, i, valaddr, valuep);
1143 if (baddr)
1144 return baddr;
1145 }
1146 /* Not found. */
1147 if (valuep)
1148 *valuep = 0;
1149 return 0;
1150 }
1151
1152 /* Baseclass is easily computed. */
1153 if (valuep)
1154 *valuep = 0;
1155 return valaddr + TYPE_BASECLASS_BITPOS (type, index) / 8;
1156 }
1157
1158 /* Ugly hack to convert method stubs into method types.
1159
1160 He ain't kiddin'. This demangles the name of the method into a string
1161 including argument types, parses out each argument type, generates
1162 a string casting a zero to that type, evaluates the string, and stuffs
1163 the resulting type into an argtype vector!!! Then it knows the type
1164 of the whole function (including argument types for overloading),
1165 which info used to be in the stab's but was removed to hack back
1166 the space required for them. */
1167 void
1168 check_stub_method (type, i, j)
1169 struct type *type;
1170 int i, j;
1171 {
1172 extern char *gdb_mangle_typename (), *strchr ();
1173 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1174 char *field_name = TYPE_FN_FIELDLIST_NAME (type, i);
1175 char *inner_name = gdb_mangle_typename (type);
1176 int mangled_name_len = (strlen (field_name)
1177 + strlen (inner_name)
1178 + strlen (TYPE_FN_FIELD_PHYSNAME (f, j))
1179 + 1);
1180 char *mangled_name;
1181 char *demangled_name;
1182 char *argtypetext, *p;
1183 int depth = 0, argcount = 1;
1184 struct type **argtypes;
1185
1186 if (OPNAME_PREFIX_P (field_name))
1187 {
1188 char *opname = cplus_mangle_opname (field_name + 3);
1189 mangled_name_len += strlen (opname);
1190 mangled_name = (char *)xmalloc (mangled_name_len);
1191
1192 strncpy (mangled_name, field_name, 3);
1193 mangled_name[3] = '\0';
1194 strcat (mangled_name, opname);
1195 }
1196 else
1197 {
1198 mangled_name = (char *)xmalloc (mangled_name_len);
1199 strcpy (mangled_name, TYPE_FN_FIELDLIST_NAME (type, i));
1200 }
1201 strcat (mangled_name, inner_name);
1202 strcat (mangled_name, TYPE_FN_FIELD_PHYSNAME (f, j));
1203 demangled_name = cplus_demangle (mangled_name, 0);
1204
1205 /* Now, read in the parameters that define this type. */
1206 argtypetext = strchr (demangled_name, '(') + 1;
1207 p = argtypetext;
1208 while (*p)
1209 {
1210 if (*p == '(')
1211 depth += 1;
1212 else if (*p == ')')
1213 depth -= 1;
1214 else if (*p == ',' && depth == 0)
1215 argcount += 1;
1216
1217 p += 1;
1218 }
1219 /* We need one more slot for the void [...] or NULL [end of arglist] */
1220 argtypes = (struct type **)xmalloc ((argcount+1) * sizeof (struct type *));
1221 p = argtypetext;
1222 argtypes[0] = lookup_pointer_type (type);
1223 argcount = 1;
1224
1225 if (*p != ')') /* () means no args, skip while */
1226 {
1227 while (*p)
1228 {
1229 if (*p == '(')
1230 depth += 1;
1231 else if (*p == ')')
1232 depth -= 1;
1233
1234 if (depth <= 0 && (*p == ',' || *p == ')'))
1235 {
1236 char *tmp = (char *)alloca (p - argtypetext + 4);
1237 value val;
1238 tmp[0] = '(';
1239 bcopy (argtypetext, tmp+1, p - argtypetext);
1240 tmp[p-argtypetext+1] = ')';
1241 tmp[p-argtypetext+2] = '0';
1242 tmp[p-argtypetext+3] = '\0';
1243 val = parse_and_eval (tmp);
1244 argtypes[argcount] = VALUE_TYPE (val);
1245 argcount += 1;
1246 argtypetext = p + 1;
1247 }
1248 p += 1;
1249 }
1250 }
1251
1252 if (p[-2] != '.') /* ... */
1253 argtypes[argcount] = builtin_type_void; /* Ellist terminator */
1254 else
1255 argtypes[argcount] = NULL; /* List terminator */
1256
1257 free (demangled_name);
1258
1259 type = lookup_method_type (type, TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)), argtypes);
1260 /* Free the stub type...it's no longer needed. */
1261 free (TYPE_FN_FIELD_TYPE (f, j));
1262 TYPE_FN_FIELD_PHYSNAME (f, j) = mangled_name;
1263 TYPE_FN_FIELD_TYPE (f, j) = type;
1264 }
1265 \f
1266 long
1267 unpack_field_as_long (type, valaddr, fieldno)
1268 struct type *type;
1269 char *valaddr;
1270 int fieldno;
1271 {
1272 long val;
1273 int bitpos = TYPE_FIELD_BITPOS (type, fieldno);
1274 int bitsize = TYPE_FIELD_BITSIZE (type, fieldno);
1275
1276 bcopy (valaddr + bitpos / 8, &val, sizeof val);
1277 SWAP_TARGET_AND_HOST (&val, sizeof val);
1278
1279 /* Extracting bits depends on endianness of the machine. */
1280 #ifdef BITS_BIG_ENDIAN
1281 val = val >> (sizeof val * 8 - bitpos % 8 - bitsize);
1282 #else
1283 val = val >> (bitpos % 8);
1284 #endif
1285
1286 val &= (1 << bitsize) - 1;
1287 return val;
1288 }
1289
1290 /* Modify the value of a bitfield. ADDR points to a block of memory in
1291 target byte order; the bitfield starts in the byte pointed to. FIELDVAL
1292 is the desired value of the field, in host byte order. BITPOS and BITSIZE
1293 indicate which bits (in target bit order) comprise the bitfield. */
1294
1295 void
1296 modify_field (addr, fieldval, bitpos, bitsize)
1297 char *addr;
1298 int fieldval;
1299 int bitpos, bitsize;
1300 {
1301 long oword;
1302
1303 /* Reject values too big to fit in the field in question.
1304 Otherwise adjoining fields may be corrupted. */
1305 if (fieldval & ~((1<<bitsize)-1))
1306 error ("Value %d does not fit in %d bits.", fieldval, bitsize);
1307
1308 bcopy (addr, &oword, sizeof oword);
1309 SWAP_TARGET_AND_HOST (&oword, sizeof oword); /* To host format */
1310
1311 /* Shifting for bit field depends on endianness of the target machine. */
1312 #ifdef BITS_BIG_ENDIAN
1313 bitpos = sizeof (oword) * 8 - bitpos - bitsize;
1314 #endif
1315
1316 oword &= ~(((1 << bitsize) - 1) << bitpos);
1317 oword |= fieldval << bitpos;
1318
1319 SWAP_TARGET_AND_HOST (&oword, sizeof oword); /* To target format */
1320 bcopy (&oword, addr, sizeof oword);
1321 }
1322 \f
1323 /* Convert C numbers into newly allocated values */
1324
1325 value
1326 value_from_long (type, num)
1327 struct type *type;
1328 register LONGEST num;
1329 {
1330 register value val = allocate_value (type);
1331 register enum type_code code = TYPE_CODE (type);
1332 register int len = TYPE_LENGTH (type);
1333
1334 if (code == TYPE_CODE_INT || code == TYPE_CODE_ENUM)
1335 {
1336 if (len == sizeof (char))
1337 * (char *) VALUE_CONTENTS_RAW (val) = num;
1338 else if (len == sizeof (short))
1339 * (short *) VALUE_CONTENTS_RAW (val) = num;
1340 else if (len == sizeof (int))
1341 * (int *) VALUE_CONTENTS_RAW (val) = num;
1342 else if (len == sizeof (long))
1343 * (long *) VALUE_CONTENTS_RAW (val) = num;
1344 #ifdef LONG_LONG
1345 else if (len == sizeof (long long))
1346 * (long long *) VALUE_CONTENTS_RAW (val) = num;
1347 #endif
1348 else
1349 error ("Integer type encountered with unexpected data length.");
1350 }
1351 else
1352 error ("Unexpected type encountered for integer constant.");
1353
1354 /* num was in host byte order. So now put the value's contents
1355 into target byte order. */
1356 SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (val), len);
1357
1358 return val;
1359 }
1360
1361 value
1362 value_from_double (type, num)
1363 struct type *type;
1364 double num;
1365 {
1366 register value val = allocate_value (type);
1367 register enum type_code code = TYPE_CODE (type);
1368 register int len = TYPE_LENGTH (type);
1369
1370 if (code == TYPE_CODE_FLT)
1371 {
1372 if (len == sizeof (float))
1373 * (float *) VALUE_CONTENTS_RAW (val) = num;
1374 else if (len == sizeof (double))
1375 * (double *) VALUE_CONTENTS_RAW (val) = num;
1376 else
1377 error ("Floating type encountered with unexpected data length.");
1378 }
1379 else
1380 error ("Unexpected type encountered for floating constant.");
1381
1382 /* num was in host byte order. So now put the value's contents
1383 into target byte order. */
1384 SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (val), len);
1385
1386 return val;
1387 }
1388 \f
1389 /* Deal with the value that is "about to be returned". */
1390
1391 /* Return the value that a function returning now
1392 would be returning to its caller, assuming its type is VALTYPE.
1393 RETBUF is where we look for what ought to be the contents
1394 of the registers (in raw form). This is because it is often
1395 desirable to restore old values to those registers
1396 after saving the contents of interest, and then call
1397 this function using the saved values.
1398 struct_return is non-zero when the function in question is
1399 using the structure return conventions on the machine in question;
1400 0 when it is using the value returning conventions (this often
1401 means returning pointer to where structure is vs. returning value). */
1402
1403 value
1404 value_being_returned (valtype, retbuf, struct_return)
1405 register struct type *valtype;
1406 char retbuf[REGISTER_BYTES];
1407 int struct_return;
1408 /*ARGSUSED*/
1409 {
1410 register value val;
1411 CORE_ADDR addr;
1412
1413 #if defined (EXTRACT_STRUCT_VALUE_ADDRESS)
1414 /* If this is not defined, just use EXTRACT_RETURN_VALUE instead. */
1415 if (struct_return) {
1416 addr = EXTRACT_STRUCT_VALUE_ADDRESS (retbuf);
1417 if (!addr)
1418 error ("Function return value unknown");
1419 return value_at (valtype, addr);
1420 }
1421 #endif
1422
1423 val = allocate_value (valtype);
1424 EXTRACT_RETURN_VALUE (valtype, retbuf, VALUE_CONTENTS_RAW (val));
1425
1426 return val;
1427 }
1428
1429 /* Should we use EXTRACT_STRUCT_VALUE_ADDRESS instead of
1430 EXTRACT_RETURN_VALUE? GCC_P is true if compiled with gcc
1431 and TYPE is the type (which is known to be struct, union or array).
1432
1433 On most machines, the struct convention is used unless we are
1434 using gcc and the type is of a special size. */
1435 #if !defined (USE_STRUCT_CONVENTION)
1436 #define USE_STRUCT_CONVENTION(gcc_p, type)\
1437 (!((gcc_p) && (TYPE_LENGTH (value_type) == 1 \
1438 || TYPE_LENGTH (value_type) == 2 \
1439 || TYPE_LENGTH (value_type) == 4 \
1440 || TYPE_LENGTH (value_type) == 8 \
1441 ) \
1442 ))
1443 #endif
1444
1445 /* Return true if the function specified is using the structure returning
1446 convention on this machine to return arguments, or 0 if it is using
1447 the value returning convention. FUNCTION is the value representing
1448 the function, FUNCADDR is the address of the function, and VALUE_TYPE
1449 is the type returned by the function. GCC_P is nonzero if compiled
1450 with GCC. */
1451
1452 int
1453 using_struct_return (function, funcaddr, value_type, gcc_p)
1454 value function;
1455 CORE_ADDR funcaddr;
1456 struct type *value_type;
1457 int gcc_p;
1458 /*ARGSUSED*/
1459 {
1460 register enum type_code code = TYPE_CODE (value_type);
1461
1462 if (code == TYPE_CODE_ERROR)
1463 error ("Function return type unknown.");
1464
1465 if (code == TYPE_CODE_STRUCT ||
1466 code == TYPE_CODE_UNION ||
1467 code == TYPE_CODE_ARRAY)
1468 return USE_STRUCT_CONVENTION (gcc_p, value_type);
1469
1470 return 0;
1471 }
1472
1473 /* Store VAL so it will be returned if a function returns now.
1474 Does not verify that VAL's type matches what the current
1475 function wants to return. */
1476
1477 void
1478 set_return_value (val)
1479 value val;
1480 {
1481 register enum type_code code = TYPE_CODE (VALUE_TYPE (val));
1482 double dbuf;
1483 LONGEST lbuf;
1484
1485 if (code == TYPE_CODE_ERROR)
1486 error ("Function return type unknown.");
1487
1488 if (code == TYPE_CODE_STRUCT
1489 || code == TYPE_CODE_UNION)
1490 error ("Specifying a struct or union return value is not supported.");
1491
1492 /* FIXME, this is bogus. We don't know what the return conventions
1493 are, or how values should be promoted.... */
1494 if (code == TYPE_CODE_FLT)
1495 {
1496 dbuf = value_as_double (val);
1497
1498 STORE_RETURN_VALUE (VALUE_TYPE (val), (char *)&dbuf);
1499 }
1500 else
1501 {
1502 lbuf = value_as_long (val);
1503 STORE_RETURN_VALUE (VALUE_TYPE (val), (char *)&lbuf);
1504 }
1505 }
1506 \f
1507 void
1508 _initialize_values ()
1509 {
1510 add_cmd ("convenience", no_class, show_convenience,
1511 "Debugger convenience (\"$foo\") variables.\n\
1512 These variables are created when you assign them values;\n\
1513 thus, \"print $foo=1\" gives \"$foo\" the value 1. Values may be any type.\n\n\
1514 A few convenience variables are given values automatically:\n\
1515 \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
1516 \"$__\" holds the contents of the last address examined with \"x\".",
1517 &showlist);
1518
1519 add_cmd ("values", no_class, show_values,
1520 "Elements of value history around item number IDX (or last ten).",
1521 &showlist);
1522 }