Vax Ultrix changes from David Taylor <taylor@think.com>.
[deliverable/binutils-gdb.git] / gdb / valprint.c
CommitLineData
bd5635a1
RP
1/* Print values for GNU debugger gdb.
2 Copyright (C) 1986, 1988, 1989 Free Software Foundation, Inc.
3
4This file is part of GDB.
5
36b9d39c 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
36b9d39c
JG
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
bd5635a1 10
36b9d39c 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
36b9d39c
JG
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
bd5635a1
RP
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 "gdbcmd.h"
28#include "target.h"
29#include "obstack.h"
be3bc7ad 30#include "language.h"
bd5635a1
RP
31
32#include <errno.h>
33extern int sys_nerr;
34extern char *sys_errlist[];
35
36extern void print_scalar_formatted(); /* printcmd.c */
37extern void print_address_demangle(); /* printcmd.c */
38extern int demangle; /* whether to print C++ syms raw or source-form */
39
40/* Maximum number of chars to print for a string pointer value
41 or vector contents, or UINT_MAX for no limit. */
42
43static unsigned int print_max;
44
45static void type_print_varspec_suffix ();
46static void type_print_varspec_prefix ();
47static void type_print_base ();
48static void type_print_method_args ();
49
50/* Default input and output radixes, and output format letter. */
51
52unsigned input_radix = 10;
53unsigned output_radix = 10;
54int output_format = 0;
55
56
57char **unsigned_type_table;
58char **signed_type_table;
59char **float_type_table;
60
61
62/* Print repeat counts if there are more than this
63 many repetitions of an element in an array. */
64#define REPEAT_COUNT_THRESHOLD 10
0dce3774
JK
65
66/* Define a mess of print controls. */
67
68int prettyprint; /* Controls pretty printing of structures */
69int vtblprint; /* Controls printing of vtbl's */
70int unionprint; /* Controls printing of nested unions. */
71int arrayprint; /* Controls pretty printing of arrays. */
72int addressprint; /* Controls pretty printing of addresses. */
73int objectprint; /* Controls looking up an object's derived type
74 using what we find in its vtables. */
75
76struct obstack dont_print_obstack;
77
78static void cplus_val_print ();
bd5635a1
RP
79\f
80/* Print the character string STRING, printing at most LENGTH characters.
81 Printing stops early if the number hits print_max; repeat counts
82 are printed as appropriate. Print ellipses at the end if we
83 had to stop before printing LENGTH characters, or if FORCE_ELLIPSES. */
84
85void
86print_string (stream, string, length, force_ellipses)
87 FILE *stream;
88 char *string;
89 unsigned int length;
90 int force_ellipses;
91{
92 register unsigned int i;
93 unsigned int things_printed = 0;
94 int in_quotes = 0;
95 int need_comma = 0;
96 extern int inspect_it;
97
98 if (length == 0)
99 {
100 fputs_filtered ("\"\"", stdout);
101 return;
102 }
103
104 for (i = 0; i < length && things_printed < print_max; ++i)
105 {
106 /* Position of the character we are examining
107 to see whether it is repeated. */
108 unsigned int rep1;
109 /* Number of repetitions we have detected so far. */
110 unsigned int reps;
111
112 QUIT;
113
114 if (need_comma)
115 {
116 fputs_filtered (", ", stream);
117 need_comma = 0;
118 }
119
120 rep1 = i + 1;
121 reps = 1;
122 while (rep1 < length && string[rep1] == string[i])
123 {
124 ++rep1;
125 ++reps;
126 }
127
128 if (reps > REPEAT_COUNT_THRESHOLD)
129 {
130 if (in_quotes)
131 {
132 if (inspect_it)
133 fputs_filtered ("\\\", ", stream);
134 else
135 fputs_filtered ("\", ", stream);
136 in_quotes = 0;
137 }
138 fputs_filtered ("'", stream);
139 printchar (string[i], stream, '\'');
140 fprintf_filtered (stream, "' <repeats %u times>", reps);
141 i = rep1 - 1;
142 things_printed += REPEAT_COUNT_THRESHOLD;
143 need_comma = 1;
144 }
145 else
146 {
147 if (!in_quotes)
148 {
149 if (inspect_it)
150 fputs_filtered ("\\\"", stream);
151 else
152 fputs_filtered ("\"", stream);
153 in_quotes = 1;
154 }
155 printchar (string[i], stream, '"');
156 ++things_printed;
157 }
158 }
159
160 /* Terminate the quotes if necessary. */
161 if (in_quotes)
162 {
163 if (inspect_it)
164 fputs_filtered ("\\\"", stream);
165 else
166 fputs_filtered ("\"", stream);
167 }
168
169 if (force_ellipses || i < length)
170 fputs_filtered ("...", stream);
171}
172
173/* Print a floating point value of type TYPE, pointed to in GDB by VALADDR,
174 on STREAM. */
175
176void
177print_floating (valaddr, type, stream)
178 char *valaddr;
179 struct type *type;
180 FILE *stream;
181{
182 double doub;
183 int inv;
184 unsigned len = TYPE_LENGTH (type);
185
186#if defined (IEEE_FLOAT)
187
188 /* Check for NaN's. Note that this code does not depend on us being
189 on an IEEE conforming system. It only depends on the target
190 machine using IEEE representation. This means (a)
191 cross-debugging works right, and (2) IEEE_FLOAT can (and should)
192 be defined for systems like the 68881, which uses IEEE
193 representation, but is not IEEE conforming. */
194
195 {
196 long low, high;
197 /* Is the sign bit 0? */
198 int nonnegative;
199 /* Is it is a NaN (i.e. the exponent is all ones and
200 the fraction is nonzero)? */
201 int is_nan;
202
203 if (len == sizeof (float))
204 {
205 /* It's single precision. */
206 bcopy (valaddr, &low, sizeof (low));
207 /* target -> host. */
208 SWAP_TARGET_AND_HOST (&low, sizeof (float));
209 nonnegative = low >= 0;
210 is_nan = ((((low >> 23) & 0xFF) == 0xFF)
211 && 0 != (low & 0x7FFFFF));
212 low &= 0x7fffff;
213 high = 0;
214 }
215 else
216 {
217 /* It's double precision. Get the high and low words. */
218
219#if TARGET_BYTE_ORDER == BIG_ENDIAN
220 bcopy (valaddr+4, &low, sizeof (low));
221 bcopy (valaddr+0, &high, sizeof (high));
222#else
223 bcopy (valaddr+0, &low, sizeof (low));
224 bcopy (valaddr+4, &high, sizeof (high));
225#endif
226 SWAP_TARGET_AND_HOST (&low, sizeof (low));
227 SWAP_TARGET_AND_HOST (&high, sizeof (high));
228 nonnegative = high >= 0;
229 is_nan = (((high >> 20) & 0x7ff) == 0x7ff
230 && ! ((((high & 0xfffff) == 0)) && (low == 0)));
231 high &= 0xfffff;
232 }
233
234 if (is_nan)
235 {
236 /* The meaning of the sign and fraction is not defined by IEEE.
237 But the user might know what they mean. For example, they
238 (in an implementation-defined manner) distinguish between
239 signaling and quiet NaN's. */
240 if (high)
241 fprintf_filtered (stream, "-NaN(0x%lx%.8lx)" + nonnegative,
242 high, low);
243 else
244 fprintf_filtered (stream, "-NaN(0x%lx)" + nonnegative, low);
245 return;
246 }
247 }
248#endif /* IEEE_FLOAT. */
249
250 doub = unpack_double (type, valaddr, &inv);
251 if (inv)
252 fprintf_filtered (stream, "<invalid float value>");
253 else
be3bc7ad 254 fprintf_filtered (stream, len <= sizeof(float) ? "%.9g" : "%.17g", doub);
bd5635a1
RP
255}
256
257/* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */
258static void
259print_hex_chars (stream, valaddr, len)
260 FILE *stream;
261 unsigned char *valaddr;
262 unsigned len;
263{
264 unsigned char *p;
265
266 fprintf_filtered (stream, "0x");
267#if TARGET_BYTE_ORDER == BIG_ENDIAN
268 for (p = valaddr;
269 p < valaddr + len;
270 p++)
271#else /* Little endian. */
272 for (p = valaddr + len - 1;
273 p >= valaddr;
274 p--)
275#endif
276 {
277 fprintf_filtered (stream, "%02x", *p);
278 }
279}
280\f
281/* Print the value VAL in C-ish syntax on stream STREAM.
282 FORMAT is a format-letter, or 0 for print in natural format of data type.
283 If the object printed is a string pointer, returns
284 the number of string bytes printed. */
285
286int
287value_print (val, stream, format, pretty)
288 value val;
289 FILE *stream;
290 char format;
291 enum val_prettyprint pretty;
292{
293 register unsigned int i, n, typelen;
294
295 if (val == 0)
296 {
297 printf_filtered ("<address of value unknown>");
298 return 0;
299 }
300 if (VALUE_OPTIMIZED_OUT (val))
301 {
302 printf_filtered ("<value optimized out>");
303 return 0;
304 }
aec4cb91 305
bd5635a1
RP
306 /* A "repeated" value really contains several values in a row.
307 They are made by the @ operator.
308 Print such values as if they were arrays. */
309
310 else if (VALUE_REPEATED (val))
311 {
312 n = VALUE_REPETITIONS (val);
313 typelen = TYPE_LENGTH (VALUE_TYPE (val));
314 fprintf_filtered (stream, "{");
315 /* Print arrays of characters using string syntax. */
316 if (typelen == 1 && TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_INT
317 && format == 0)
318 print_string (stream, VALUE_CONTENTS (val), n, 0);
319 else
320 {
321 unsigned int things_printed = 0;
322
323 for (i = 0; i < n && things_printed < print_max; i++)
324 {
325 /* Position of the array element we are examining to see
326 whether it is repeated. */
327 unsigned int rep1;
328 /* Number of repetitions we have detected so far. */
329 unsigned int reps;
330
331 if (i != 0)
332 fprintf_filtered (stream, ", ");
333 wrap_here ("");
334
335 rep1 = i + 1;
336 reps = 1;
337 while (rep1 < n
338 && !bcmp (VALUE_CONTENTS (val) + typelen * i,
339 VALUE_CONTENTS (val) + typelen * rep1, typelen))
340 {
341 ++reps;
342 ++rep1;
343 }
344
345 if (reps > REPEAT_COUNT_THRESHOLD)
346 {
347 val_print (VALUE_TYPE (val),
348 VALUE_CONTENTS (val) + typelen * i,
349 VALUE_ADDRESS (val) + typelen * i,
350 stream, format, 1, 0, pretty);
351 fprintf (stream, " <repeats %u times>", reps);
352 i = rep1 - 1;
353 things_printed += REPEAT_COUNT_THRESHOLD;
354 }
355 else
356 {
357 val_print (VALUE_TYPE (val),
358 VALUE_CONTENTS (val) + typelen * i,
359 VALUE_ADDRESS (val) + typelen * i,
360 stream, format, 1, 0, pretty);
361 things_printed++;
362 }
363 }
364 if (i < n)
365 fprintf_filtered (stream, "...");
366 }
367 fprintf_filtered (stream, "}");
368 return n * typelen;
369 }
370 else
371 {
0dce3774
JK
372 struct type *type = VALUE_TYPE (val);
373
bd5635a1
RP
374 /* If it is a pointer, indicate what it points to.
375
376 Print type also if it is a reference.
377
378 C++: if it is a member pointer, we will take care
379 of that when we print it. */
0dce3774
JK
380 if (TYPE_CODE (type) == TYPE_CODE_PTR
381 || TYPE_CODE (type) == TYPE_CODE_REF)
bd5635a1
RP
382 {
383 /* Hack: remove (char *) for char strings. Their
384 type is indicated by the quoted string anyway. */
0dce3774
JK
385 if (TYPE_CODE (type) == TYPE_CODE_PTR
386 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) == sizeof(char)
387 && TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_INT
388 && !TYPE_UNSIGNED (TYPE_TARGET_TYPE (type)))
bd5635a1
RP
389 {
390 /* Print nothing */
391 }
392 else
393 {
394 fprintf_filtered (stream, "(");
0dce3774 395 type_print (type, "", stream, -1);
bd5635a1
RP
396 fprintf_filtered (stream, ") ");
397 }
398 }
0dce3774 399 return val_print (type, VALUE_CONTENTS (val),
bd5635a1
RP
400 VALUE_ADDRESS (val), stream, format, 1, 0, pretty);
401 }
402}
403
404/* Return truth value for assertion that TYPE is of the type
405 "pointer to virtual function". */
406static int
407is_vtbl_ptr_type(type)
408 struct type *type;
409{
410 char *typename = TYPE_NAME(type);
411 static const char vtbl_ptr_name[] =
412 { CPLUS_MARKER,'v','t','b','l','_','p','t','r','_','t','y','p','e' };
413
414 return (typename != NULL && !strcmp(typename, vtbl_ptr_name));
415}
416
417/* Return truth value for the assertion that TYPE is of the type
418 "pointer to virtual function table". */
419static int
420is_vtbl_member(type)
421 struct type *type;
422{
0dce3774
JK
423 if (TYPE_CODE (type) == TYPE_CODE_PTR)
424 type = TYPE_TARGET_TYPE (type);
425 else
426 return 0;
427
428 if (TYPE_CODE (type) == TYPE_CODE_ARRAY
429 && TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRUCT)
bd5635a1 430 /* Virtual functions tables are full of pointers to virtual functions. */
0dce3774 431 return is_vtbl_ptr_type (TYPE_TARGET_TYPE (type));
bd5635a1
RP
432 return 0;
433}
434\f
bd5635a1
RP
435/* Mutually recursive subroutines of cplus_val_print and val_print to print out
436 a structure's fields: val_print_fields and cplus_val_print.
437
438 TYPE, VALADDR, STREAM, RECURSE, and PRETTY have the
439 same meanings as in cplus_val_print and val_print.
440
441 DONT_PRINT is an array of baseclass types that we
442 should not print, or zero if called from top level. */
443static void
444val_print_fields (type, valaddr, stream, format, recurse, pretty, dont_print)
445 struct type *type;
446 char *valaddr;
447 FILE *stream;
448 char format;
449 int recurse;
450 enum val_prettyprint pretty;
451 struct type **dont_print;
452{
453 int i, len, n_baseclasses;
454
be3bc7ad
JG
455 check_stub_type (type);
456
bd5635a1
RP
457 fprintf_filtered (stream, "{");
458 len = TYPE_NFIELDS (type);
459 n_baseclasses = TYPE_N_BASECLASSES (type);
460
461 /* Print out baseclasses such that we don't print
462 duplicates of virtual baseclasses. */
463 if (n_baseclasses > 0)
464 cplus_val_print (type, valaddr, stream, format, recurse+1, pretty, dont_print);
465
466 if (!len && n_baseclasses == 1)
467 fprintf_filtered (stream, "<No data fields>");
468 else
469 {
470 extern int inspect_it;
471 int fields_seen = 0;
472
473 for (i = n_baseclasses; i < len; i++)
474 {
475 /* Check if static field */
476 if (TYPE_FIELD_STATIC (type, i))
477 continue;
478 if (fields_seen)
479 fprintf_filtered (stream, ", ");
480 else if (n_baseclasses > 0)
481 {
482 fprintf_filtered (stream, "\n");
483 print_spaces_filtered (2 + 2 * recurse, stream);
484 fputs_filtered ("members of ", stream);
485 fputs_filtered (type_name_no_tag (type), stream);
486 fputs_filtered (": ", stream);
487 }
488 fields_seen = 1;
489
490 if (pretty)
491 {
492 fprintf_filtered (stream, "\n");
493 print_spaces_filtered (2 + 2 * recurse, stream);
494 }
495 else
496 {
497 wrap_here (n_spaces (2 + 2 * recurse));
498 }
499 if (inspect_it)
500 {
501 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
502 fputs_filtered ("\"( ptr \"", stream);
503 else
504 fputs_filtered ("\"( nodef \"", stream);
505 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
506 fputs_filtered ("\" \"", stream);
507 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
508 fputs_filtered ("\") \"", stream);
509 }
510 else
511 {
512 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
513 fputs_filtered (" = ", stream);
514 }
515 if (TYPE_FIELD_PACKED (type, i))
516 {
f266e564 517 value v;
bd5635a1 518
f266e564
JK
519 /* Bitfields require special handling, especially due to byte
520 order problems. */
bee3c1a1 521 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
f266e564 522 unpack_field_as_long (type, valaddr, i));
bd5635a1 523
f266e564 524 val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0,
bd5635a1
RP
525 stream, format, 0, recurse + 1, pretty);
526 }
527 else
528 {
529 val_print (TYPE_FIELD_TYPE (type, i),
530 valaddr + TYPE_FIELD_BITPOS (type, i) / 8,
531 0, stream, format, 0, recurse + 1, pretty);
532 }
533 }
534 if (pretty)
535 {
536 fprintf_filtered (stream, "\n");
537 print_spaces_filtered (2 * recurse, stream);
538 }
539 }
540 fprintf_filtered (stream, "}");
541}
542
543/* Special val_print routine to avoid printing multiple copies of virtual
544 baseclasses. */
545
546static void
547cplus_val_print (type, valaddr, stream, format, recurse, pretty, dont_print)
548 struct type *type;
549 char *valaddr;
550 FILE *stream;
551 char format;
552 int recurse;
553 enum val_prettyprint pretty;
554 struct type **dont_print;
555{
556 struct obstack tmp_obstack;
557 struct type **last_dont_print
558 = (struct type **)obstack_next_free (&dont_print_obstack);
559 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
560
561 if (dont_print == 0)
562 {
563 /* If we're at top level, carve out a completely fresh
564 chunk of the obstack and use that until this particular
565 invocation returns. */
566 tmp_obstack = dont_print_obstack;
567 /* Bump up the high-water mark. Now alpha is omega. */
568 obstack_finish (&dont_print_obstack);
569 }
570
571 for (i = 0; i < n_baseclasses; i++)
572 {
573 char *baddr;
0dce3774 574 int err;
bd5635a1
RP
575
576 if (BASETYPE_VIA_VIRTUAL (type, i))
577 {
578 struct type **first_dont_print
579 = (struct type **)obstack_base (&dont_print_obstack);
580
581 int j = (struct type **)obstack_next_free (&dont_print_obstack)
582 - first_dont_print;
583
584 while (--j >= 0)
585 if (TYPE_BASECLASS (type, i) == first_dont_print[j])
586 goto flush_it;
587
588 obstack_ptr_grow (&dont_print_obstack, TYPE_BASECLASS (type, i));
589 }
590
0dce3774
JK
591 baddr = baseclass_addr (type, i, valaddr, 0, &err);
592 if (err == 0 && baddr == 0)
bd5635a1
RP
593 error ("could not find virtual baseclass `%s'\n",
594 type_name_no_tag (TYPE_BASECLASS (type, i)));
595
596 fprintf_filtered (stream, "\n");
597 if (pretty)
598 print_spaces_filtered (2 + 2 * recurse, stream);
599 fputs_filtered ("<", stream);
600 fputs_filtered (type_name_no_tag (TYPE_BASECLASS (type, i)), stream);
601 fputs_filtered ("> = ", stream);
0dce3774
JK
602 if (err != 0)
603 fprintf_filtered (stream, "<invalid address 0x%x>", baddr);
604 else
605 val_print_fields (TYPE_BASECLASS (type, i), baddr, stream, format,
606 recurse, pretty,
607 (struct type **)obstack_base (&dont_print_obstack));
bd5635a1
RP
608 flush_it:
609 ;
610 }
611
612 if (dont_print == 0)
613 {
614 /* Free the space used to deal with the printing
615 of this type from top level. */
616 obstack_free (&dont_print_obstack, last_dont_print);
617 /* Reset watermark so that we can continue protecting
618 ourselves from whatever we were protecting ourselves. */
619 dont_print_obstack = tmp_obstack;
620 }
621}
622
623/* Print data of type TYPE located at VALADDR (within GDB),
624 which came from the inferior at address ADDRESS,
625 onto stdio stream STREAM according to FORMAT
626 (a letter or 0 for natural format). The data at VALADDR
627 is in target byte order.
628
629 If the data are a string pointer, returns the number of
630 sting characters printed.
631
632 if DEREF_REF is nonzero, then dereference references,
633 otherwise just print them like pointers.
634
635 The PRETTY parameter controls prettyprinting. */
636
637int
638val_print (type, valaddr, address, stream, format,
639 deref_ref, recurse, pretty)
640 struct type *type;
641 char *valaddr;
642 CORE_ADDR address;
643 FILE *stream;
644 char format;
645 int deref_ref;
646 int recurse;
647 enum val_prettyprint pretty;
648{
649 register unsigned int i;
650 unsigned len;
651 struct type *elttype;
652 unsigned eltlen;
653 LONGEST val;
654 unsigned char c;
655
656 if (pretty == Val_pretty_default)
657 {
658 pretty = prettyprint ? Val_prettyprint : Val_no_prettyprint;
659 }
660
661 QUIT;
662
663 check_stub_type (type);
664
665 if (TYPE_FLAGS (type) & TYPE_FLAG_STUB)
666 {
667 fprintf_filtered (stream, "<unknown struct>");
668 fflush (stream);
669 return 0;
670 }
671
672 switch (TYPE_CODE (type))
673 {
674 case TYPE_CODE_ARRAY:
e1ce8aa5 675 /* FIXME: TYPE_LENGTH (type) is unsigned and therefore always
bee3c1a1 676 >= 0. Is "> 0" meant? I'm not sure what an "array of
e1ce8aa5
JK
677 unspecified length" (mentioned in the comment for the else-part
678 of this if) is. */
bd5635a1
RP
679 if (TYPE_LENGTH (type) >= 0
680 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
681 {
682 elttype = TYPE_TARGET_TYPE (type);
683 eltlen = TYPE_LENGTH (elttype);
684 len = TYPE_LENGTH (type) / eltlen;
685 if (arrayprint)
686 print_spaces_filtered (2 + 2 * recurse, stream);
687 fprintf_filtered (stream, "{");
688 /* For an array of chars, print with string syntax. */
689 if (eltlen == 1 && TYPE_CODE (elttype) == TYPE_CODE_INT
690 && (format == 0 || format == 's') )
691 print_string (stream, valaddr, len, 0);
692 else
693 {
694 unsigned int things_printed = 0;
695
0dce3774
JK
696 /* If this is a virtual function table, print the 0th
697 entry specially, and the rest of the members normally. */
698 if (is_vtbl_ptr_type (elttype))
699 {
700 fprintf_filtered (stream, "%d vtable entries", len-1);
701 i = 1;
702 }
703 else
704 i = 0;
705
706 for (; i < len && things_printed < print_max; i++)
bd5635a1
RP
707 {
708 /* Position of the array element we are examining to see
709 whether it is repeated. */
710 unsigned int rep1;
711 /* Number of repetitions we have detected so far. */
712 unsigned int reps;
713
714 if (i != 0)
715 if (arrayprint)
716 {
717 fprintf_filtered (stream, ",\n");
718 print_spaces_filtered (2 + 2 * recurse, stream);
719 }
720 else
721 fprintf_filtered (stream, ", ");
722 wrap_here (n_spaces (2 + 2 * recurse));
723
724 rep1 = i + 1;
725 reps = 1;
726 while (rep1 < len
727 && !bcmp (valaddr + i * eltlen,
728 valaddr + rep1 * eltlen, eltlen))
729 {
730 ++reps;
731 ++rep1;
732 }
733
734 if (reps > REPEAT_COUNT_THRESHOLD)
735 {
736 val_print (elttype, valaddr + i * eltlen,
737 0, stream, format, deref_ref,
738 recurse + 1, pretty);
739 fprintf_filtered (stream, " <repeats %u times>", reps);
740 i = rep1 - 1;
741 things_printed += REPEAT_COUNT_THRESHOLD;
742 }
743 else
744 {
745 val_print (elttype, valaddr + i * eltlen,
746 0, stream, format, deref_ref,
747 recurse + 1, pretty);
748 things_printed++;
749 }
750 }
751 if (i < len)
752 fprintf_filtered (stream, "...");
753 }
754 fprintf_filtered (stream, "}");
755 break;
756 }
757 /* Array of unspecified length: treat like pointer to first elt. */
758 valaddr = (char *) &address;
759
760 case TYPE_CODE_PTR:
761 if (format && format != 's')
762 {
763 print_scalar_formatted (valaddr, type, format, 0, stream);
764 break;
765 }
766 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_METHOD)
767 {
768 struct type *domain = TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type));
769 struct fn_field *f;
770 int j, len2;
771 char *kind = "";
e1ce8aa5 772 CORE_ADDR addr;
bd5635a1 773
e1ce8aa5
JK
774 addr = unpack_pointer (lookup_pointer_type (builtin_type_void),
775 valaddr);
776 if (addr < 128)
bd5635a1
RP
777 {
778 len = TYPE_NFN_FIELDS (domain);
779 for (i = 0; i < len; i++)
780 {
781 f = TYPE_FN_FIELDLIST1 (domain, i);
782 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
783
784 for (j = 0; j < len2; j++)
785 {
786 QUIT;
e1ce8aa5 787 if (TYPE_FN_FIELD_VOFFSET (f, j) == addr)
bd5635a1
RP
788 {
789 kind = "virtual";
790 goto common;
791 }
792 }
793 }
794 }
795 else
796 {
e1ce8aa5 797 struct symbol *sym = find_pc_function (addr);
bd5635a1
RP
798 if (sym == 0)
799 error ("invalid pointer to member function");
800 len = TYPE_NFN_FIELDS (domain);
801 for (i = 0; i < len; i++)
802 {
803 f = TYPE_FN_FIELDLIST1 (domain, i);
804 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
805
806 for (j = 0; j < len2; j++)
807 {
808 QUIT;
809 if (!strcmp (SYMBOL_NAME (sym), TYPE_FN_FIELD_PHYSNAME (f, j)))
810 goto common;
811 }
812 }
813 }
814 common:
815 if (i < len)
816 {
817 fprintf_filtered (stream, "&");
818 type_print_varspec_prefix (TYPE_FN_FIELD_TYPE (f, j), stream, 0, 0);
819 fprintf (stream, kind);
820 if (TYPE_FN_FIELD_PHYSNAME (f, j)[0] == '_'
821 && TYPE_FN_FIELD_PHYSNAME (f, j)[1] == CPLUS_MARKER)
822 type_print_method_args
823 (TYPE_FN_FIELD_ARGS (f, j) + 1, "~",
824 TYPE_FN_FIELDLIST_NAME (domain, i), 0, stream);
825 else
826 type_print_method_args
827 (TYPE_FN_FIELD_ARGS (f, j), "",
828 TYPE_FN_FIELDLIST_NAME (domain, i), 0, stream);
829 break;
830 }
831 fprintf_filtered (stream, "(");
832 type_print (type, "", stream, -1);
e1ce8aa5 833 fprintf_filtered (stream, ") %d", (int) addr >> 3);
bd5635a1
RP
834 }
835 else if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_MEMBER)
836 {
837 struct type *domain = TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type));
838
839 /* VAL is a byte offset into the structure type DOMAIN.
840 Find the name of the field for that offset and
841 print it. */
842 int extra = 0;
843 int bits = 0;
844 len = TYPE_NFIELDS (domain);
845 /* @@ Make VAL into bit offset */
846 val = unpack_long (builtin_type_int, valaddr) << 3;
847 for (i = TYPE_N_BASECLASSES (domain); i < len; i++)
848 {
849 int bitpos = TYPE_FIELD_BITPOS (domain, i);
850 QUIT;
851 if (val == bitpos)
852 break;
853 if (val < bitpos && i != 0)
854 {
855 /* Somehow pointing into a field. */
856 i -= 1;
857 extra = (val - TYPE_FIELD_BITPOS (domain, i));
858 if (extra & 0x3)
859 bits = 1;
860 else
861 extra >>= 3;
862 break;
863 }
864 }
865 if (i < len)
866 {
867 fprintf_filtered (stream, "&");
868 type_print_base (domain, stream, 0, 0);
869 fprintf_filtered (stream, "::");
870 fputs_filtered (TYPE_FIELD_NAME (domain, i), stream);
871 if (extra)
872 fprintf_filtered (stream, " + %d bytes", extra);
873 if (bits)
874 fprintf_filtered (stream, " (offset in bits)");
875 break;
876 }
877 fprintf_filtered (stream, "%d", val >> 3);
878 }
879 else
880 {
e1ce8aa5 881 CORE_ADDR addr = unpack_pointer (type, valaddr);
bd5635a1
RP
882 elttype = TYPE_TARGET_TYPE (type);
883
884 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
885 {
886 /* Try to print what function it points to. */
887 print_address_demangle (addr, stream, demangle);
888 /* Return value is irrelevant except for string pointers. */
889 return 0;
890 }
891
892 if (addressprint && format != 's')
893 fprintf_filtered (stream, "0x%x", addr);
894
895 /* For a pointer to char or unsigned char,
896 also print the string pointed to, unless pointer is null. */
897 i = 0; /* Number of characters printed. */
898 if (TYPE_LENGTH (elttype) == 1
899 && TYPE_CODE (elttype) == TYPE_CODE_INT
900 && (format == 0 || format == 's')
901 && addr != 0
902 /* If print_max is UINT_MAX, the alloca below will fail.
903 In that case don't try to print the string. */
904 && print_max < UINT_MAX)
905 {
906 int first_addr_err = 0;
907 int errcode = 0;
908
909 /* Get first character. */
910 errcode = target_read_memory (addr, (char *)&c, 1);
911 if (errcode != 0)
912 {
913 /* First address out of bounds. */
914 first_addr_err = 1;
915 }
916 else
917 {
918 /* A real string. */
919 char *string = (char *) alloca (print_max);
920
921 /* If the loop ends by us hitting print_max characters,
922 we need to have elipses at the end. */
923 int force_ellipses = 1;
924
925 /* This loop always fetches print_max characters, even
926 though print_string might want to print more or fewer
927 (with repeated characters). This is so that
928 we don't spend forever fetching if we print
929 a long string consisting of the same character
930 repeated. Also so we can do it all in one memory
931 operation, which is faster. However, this will be
932 slower if print_max is set high, e.g. if you set
933 print_max to 1000, not only will it take a long
934 time to fetch short strings, but if you are near
935 the end of the address space, it might not work.
936 FIXME. */
937 QUIT;
938 errcode = target_read_memory (addr, string, print_max);
939 if (errcode != 0)
940 force_ellipses = 0;
941 else
942 for (i = 0; i < print_max; i++)
943 if (string[i] == '\0')
944 {
945 force_ellipses = 0;
946 break;
947 }
948 QUIT;
949
950 if (addressprint)
951 fputs_filtered (" ", stream);
952 print_string (stream, string, i, force_ellipses);
953 }
954
955 if (errcode != 0)
956 {
957 if (errcode == EIO)
958 {
959 fprintf_filtered (stream,
960 (" <Address 0x%x out of bounds>"
961 + first_addr_err),
962 addr + i);
963 }
964 else
965 {
966 if (errcode >= sys_nerr || errcode < 0)
967 error ("Error reading memory address 0x%x: unknown error (%d).",
968 addr + i, errcode);
969 else
970 error ("Error reading memory address 0x%x: %s.",
971 addr + i, sys_errlist[errcode]);
972 }
973 }
974
975 fflush (stream);
976 }
977 else /* print vtbl's nicely */
978 if (is_vtbl_member(type))
979 {
e1ce8aa5 980 CORE_ADDR vt_address = unpack_pointer (type, valaddr);
bd5635a1
RP
981
982 int vt_index = find_pc_misc_function (vt_address);
983 if (vt_index >= 0
984 && vt_address == misc_function_vector[vt_index].address)
985 {
986 fputs_filtered (" <", stream);
987 fputs_demangled (misc_function_vector[vt_index].name,
988 stream, 1);
989 fputs_filtered (">", stream);
990 }
991 if (vtblprint)
992 {
e1ce8aa5 993 value vt_val;
bd5635a1 994
e1ce8aa5
JK
995 vt_val = value_at (TYPE_TARGET_TYPE (type), vt_address);
996 val_print (VALUE_TYPE (vt_val), VALUE_CONTENTS (vt_val),
997 VALUE_ADDRESS (vt_val), stream, format,
bd5635a1
RP
998 deref_ref, recurse + 1, pretty);
999 if (pretty)
1000 {
1001 fprintf_filtered (stream, "\n");
1002 print_spaces_filtered (2 + 2 * recurse, stream);
1003 }
1004 }
1005 }
1006
1007 /* Return number of characters printed, plus one for the
1008 terminating null if we have "reached the end". */
1009 return i + (print_max && i != print_max);
1010 }
1011 break;
1012
1013 case TYPE_CODE_MEMBER:
1014 error ("not implemented: member type in val_print");
1015 break;
1016
1017 case TYPE_CODE_REF:
1018 if (addressprint)
1019 {
1020 fprintf_filtered (stream, "@0x%lx",
1021 unpack_long (builtin_type_int, valaddr));
1022 if (deref_ref)
1023 fputs_filtered (": ", stream);
1024 }
1025 /* De-reference the reference. */
1026 if (deref_ref)
1027 {
1028 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_UNDEF)
1029 {
e1ce8aa5
JK
1030 value deref_val =
1031 value_at
1032 (TYPE_TARGET_TYPE (type),
1033 unpack_pointer (lookup_pointer_type (builtin_type_void),
1034 valaddr));
1035 val_print (VALUE_TYPE (deref_val), VALUE_CONTENTS (deref_val),
1036 VALUE_ADDRESS (deref_val), stream, format,
bd5635a1
RP
1037 deref_ref, recurse + 1, pretty);
1038 }
1039 else
1040 fputs_filtered ("???", stream);
1041 }
1042 break;
1043
1044 case TYPE_CODE_UNION:
1045 if (recurse && !unionprint)
1046 {
1047 fprintf_filtered (stream, "{...}");
1048 break;
1049 }
1050 /* Fall through. */
1051 case TYPE_CODE_STRUCT:
1052 if (vtblprint && is_vtbl_ptr_type(type))
1053 {
1054 /* Print the unmangled name if desired. */
1055 print_address_demangle(*((int *) (valaddr + /* FIXME bytesex */
1056 TYPE_FIELD_BITPOS (type, VTBL_FNADDR_OFFSET) / 8)),
1057 stream, demangle);
1058 break;
1059 }
1060 val_print_fields (type, valaddr, stream, format, recurse, pretty, 0);
1061 break;
1062
1063 case TYPE_CODE_ENUM:
1064 if (format)
1065 {
1066 print_scalar_formatted (valaddr, type, format, 0, stream);
1067 break;
1068 }
1069 len = TYPE_NFIELDS (type);
1070 val = unpack_long (builtin_type_int, valaddr);
1071 for (i = 0; i < len; i++)
1072 {
1073 QUIT;
1074 if (val == TYPE_FIELD_BITPOS (type, i))
1075 break;
1076 }
1077 if (i < len)
1078 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
1079 else
e1ce8aa5
JK
1080#ifdef LONG_LONG
1081 fprintf_filtered (stream, "%lld", val);
1082#else
1083 fprintf_filtered (stream, "%ld", val);
1084#endif
bd5635a1
RP
1085 break;
1086
1087 case TYPE_CODE_FUNC:
1088 if (format)
1089 {
1090 print_scalar_formatted (valaddr, type, format, 0, stream);
1091 break;
1092 }
36b9d39c
JG
1093 /* FIXME, we should consider, at least for ANSI C language, eliminating
1094 the distinction made between FUNCs and POINTERs to FUNCs. */
bd5635a1
RP
1095 fprintf_filtered (stream, "{");
1096 type_print (type, "", stream, -1);
1097 fprintf_filtered (stream, "} ");
36b9d39c
JG
1098 /* Try to print what function it points to, and its address. */
1099 print_address_demangle (address, stream, demangle);
bd5635a1
RP
1100 break;
1101
1102 case TYPE_CODE_INT:
1103 if (format || output_format)
1104 {
1105 print_scalar_formatted (valaddr, type,
1106 format? format: output_format,
1107 0, stream);
1108 break;
1109 }
1110 if (TYPE_LENGTH (type) > sizeof (LONGEST))
1111 {
1112 if (TYPE_UNSIGNED (type))
1113 {
1114 /* First figure out whether the number in fact has zeros
1115 in all its bytes more significant than least significant
1116 sizeof (LONGEST) ones. */
1117 char *p;
1118 /* Pointer to first (i.e. lowest address) nonzero character. */
1119 char *first_addr;
e1ce8aa5 1120 len = TYPE_LENGTH (type);
bd5635a1
RP
1121
1122#if TARGET_BYTE_ORDER == BIG_ENDIAN
1123 for (p = valaddr;
1124 len > sizeof (LONGEST)
1125 && p < valaddr + TYPE_LENGTH (type);
1126 p++)
1127#else /* Little endian. */
1128 first_addr = valaddr;
1129 for (p = valaddr + TYPE_LENGTH (type);
1130 len > sizeof (LONGEST) && p >= valaddr;
1131 p--)
1132#endif /* Little endian. */
1133 {
1134 if (*p == 0)
1135 len--;
1136 else
1137 break;
1138 }
1139#if TARGET_BYTE_ORDER == BIG_ENDIAN
1140 first_addr = p;
1141#endif
1142
1143 if (len <= sizeof (LONGEST))
1144 {
1145 /* We can print it in decimal. */
1146 fprintf_filtered
1147 (stream,
1148#if defined (LONG_LONG)
1149 "%llu",
1150#else
1151 "%lu",
1152#endif
1153 unpack_long (BUILTIN_TYPE_LONGEST, first_addr));
1154 }
1155 else
1156 {
1157 /* It is big, so print it in hex. */
1158 print_hex_chars (stream, (unsigned char *)first_addr, len);
1159 }
1160 }
1161 else
1162 {
1163 /* Signed. One could assume two's complement (a reasonable
1164 assumption, I think) and do better than this. */
1165 print_hex_chars (stream, (unsigned char *)valaddr,
1166 TYPE_LENGTH (type));
1167 }
1168 break;
1169 }
1170#ifdef PRINT_TYPELESS_INTEGER
1171 PRINT_TYPELESS_INTEGER (stream, type, unpack_long (type, valaddr));
1172#else
1173#ifndef LONG_LONG
1174 fprintf_filtered (stream,
1175 TYPE_UNSIGNED (type) ? "%u" : "%d",
1176 unpack_long (type, valaddr));
1177#else
1178 fprintf_filtered (stream,
1179 TYPE_UNSIGNED (type) ? "%llu" : "%lld",
1180 unpack_long (type, valaddr));
1181#endif
1182#endif
1183
1184 if (TYPE_LENGTH (type) == 1)
1185 {
1186 fprintf_filtered (stream, " '");
1187 printchar ((unsigned char) unpack_long (type, valaddr),
1188 stream, '\'');
1189 fprintf_filtered (stream, "'");
1190 }
1191 break;
1192
1193 case TYPE_CODE_FLT:
1194 if (format)
1195 print_scalar_formatted (valaddr, type, format, 0, stream);
1196 else
1197 print_floating (valaddr, type, stream);
1198 break;
1199
1200 case TYPE_CODE_VOID:
1201 fprintf_filtered (stream, "void");
1202 break;
1203
1204 case TYPE_CODE_UNDEF:
1205 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
1206 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
1207 and no complete type for struct foo in that file. */
1208 fprintf_filtered (stream, "<unknown struct>");
1209 break;
1210
1211 case TYPE_CODE_ERROR:
1212 fprintf_filtered (stream, "?");
1213 break;
1214
1215 default:
1216 error ("Invalid type code in symbol table.");
1217 }
1218 fflush (stream);
1219 return 0;
1220}
1221\f
be3bc7ad
JG
1222/* Print a description of a type in the format of a
1223 typedef for the current language.
1224 NEW is the new name for a type TYPE. */
1225void
1226typedef_print (type, new, stream)
1227 struct type *type;
1228 struct symbol *new;
1229 FILE *stream;
1230{
1231 switch (current_language->la_language)
1232 {
1233#ifdef _LANG_c
1234 case language_c:
1235 fprintf_filtered(stream, "typedef ");
1236 type_print(type,"",stream,0);
1237 if(TYPE_NAME ((SYMBOL_TYPE (new))) == 0
1238 || 0 != strcmp (TYPE_NAME ((SYMBOL_TYPE (new))),
1239 SYMBOL_NAME (new)))
1240 fprintf_filtered(stream, " %s", SYMBOL_NAME(new));
1241 break;
1242#endif
1243#ifdef _LANG_m2
1244 case language_m2:
1245 fprintf_filtered(stream, "TYPE ");
1246 if(!TYPE_NAME(SYMBOL_TYPE(new)) ||
1247 strcmp (TYPE_NAME(SYMBOL_TYPE(new)),
1248 SYMBOL_NAME(new)))
1249 fprintf_filtered(stream, "%s = ", SYMBOL_NAME(new));
1250 else
1251 fprintf_filtered(stream, "<builtin> = ");
1252 type_print(type,"",stream,0);
1253 break;
1254#endif
1255 default:
1256 error("Language not supported.");
1257 }
1258 fprintf_filtered(stream, ";\n");
1259}
1260
1261
bd5635a1
RP
1262/* Print a description of a type TYPE
1263 in the form of a declaration of a variable named VARSTRING.
1264 (VARSTRING is demangled if necessary.)
1265 Output goes to STREAM (via stdio).
1266 If SHOW is positive, we show the contents of the outermost level
1267 of structure even if there is a type name that could be used instead.
1268 If SHOW is negative, we never show the details of elements' types. */
1269
1270void
1271type_print (type, varstring, stream, show)
1272 struct type *type;
1273 char *varstring;
1274 FILE *stream;
1275 int show;
1276{
1277 type_print_1 (type, varstring, stream, show, 0);
1278}
1279
1280/* LEVEL is the depth to indent lines by. */
1281
1282void
1283type_print_1 (type, varstring, stream, show, level)
1284 struct type *type;
1285 char *varstring;
1286 FILE *stream;
1287 int show;
1288 int level;
1289{
1290 register enum type_code code;
1291 type_print_base (type, stream, show, level);
1292 code = TYPE_CODE (type);
1293 if ((varstring && *varstring)
1294 ||
1295 /* Need a space if going to print stars or brackets;
1296 but not if we will print just a type name. */
1297 ((show > 0 || TYPE_NAME (type) == 0)
1298 &&
1299 (code == TYPE_CODE_PTR || code == TYPE_CODE_FUNC
1300 || code == TYPE_CODE_METHOD
1301 || code == TYPE_CODE_ARRAY
1302 || code == TYPE_CODE_MEMBER
1303 || code == TYPE_CODE_REF)))
1304 fprintf_filtered (stream, " ");
1305 type_print_varspec_prefix (type, stream, show, 0);
1306 fputs_demangled (varstring, stream, -1); /* Print demangled name
1307 without arguments */
1308 type_print_varspec_suffix (type, stream, show, 0);
1309}
1310
1311/* Print the method arguments ARGS to the file STREAM. */
1312static void
1313type_print_method_args (args, prefix, varstring, staticp, stream)
1314 struct type **args;
1315 char *prefix, *varstring;
1316 int staticp;
1317 FILE *stream;
1318{
1319 int i;
1320
1321 fputs_filtered (" ", stream);
1322 fputs_demangled (prefix, stream, 1);
1323 fputs_demangled (varstring, stream, 1);
1324 fputs_filtered (" (", stream);
1325 if (args && args[!staticp] && args[!staticp]->code != TYPE_CODE_VOID)
1326 {
1327 i = !staticp; /* skip the class variable */
1328 while (1)
1329 {
1330 type_print (args[i++], "", stream, 0);
1331 if (!args[i])
1332 {
1333 fprintf_filtered (stream, " ...");
1334 break;
1335 }
1336 else if (args[i]->code != TYPE_CODE_VOID)
1337 {
1338 fprintf_filtered (stream, ", ");
1339 }
1340 else break;
1341 }
1342 }
1343 fprintf_filtered (stream, ")");
1344}
1345
1346/* If TYPE is a derived type, then print out derivation
1347 information. Print out all layers of the type heirarchy
1348 until we encounter one with multiple inheritance.
1349 At that point, print out that ply, and return. */
1350static void
1351type_print_derivation_info (stream, type)
1352 FILE *stream;
1353 struct type *type;
1354{
1355 char *name;
1356 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
1357 struct type *basetype = 0;
1358
1359 while (type && n_baseclasses > 0)
1360 {
1361 /* Not actually sure about this one -- Bryan. */
1362 check_stub_type (type);
1363
1364 fprintf_filtered (stream, ": ");
1365 for (i = 0; ;)
1366 {
1367 basetype = TYPE_BASECLASS (type, i);
1368 if (name = type_name_no_tag (basetype))
1369 {
1370 fprintf_filtered (stream, "%s%s ",
1371 BASETYPE_VIA_PUBLIC(type, i) ? "public" : "private",
1372 BASETYPE_VIA_VIRTUAL(type, i) ? " virtual" : "");
1373 fputs_filtered (name, stream);
1374 }
1375 i++;
1376 if (i >= n_baseclasses)
1377 break;
1378 fprintf_filtered (stream, ", ");
1379 }
1380
1381 fprintf_filtered (stream, " ");
1382 if (n_baseclasses != 1)
1383 break;
1384 n_baseclasses = TYPE_N_BASECLASSES (basetype);
1385 type = basetype;
1386 }
1387}
1388
1389/* Print any asterisks or open-parentheses needed before the
1390 variable name (to describe its type).
1391
1392 On outermost call, pass 0 for PASSED_A_PTR.
1393 On outermost call, SHOW > 0 means should ignore
1394 any typename for TYPE and show its details.
1395 SHOW is always zero on recursive calls. */
1396
1397static void
1398type_print_varspec_prefix (type, stream, show, passed_a_ptr)
1399 struct type *type;
1400 FILE *stream;
1401 int show;
1402 int passed_a_ptr;
1403{
1404 if (type == 0)
1405 return;
1406
1407 if (TYPE_NAME (type) && show <= 0)
1408 return;
1409
1410 QUIT;
1411
1412 switch (TYPE_CODE (type))
1413 {
1414 case TYPE_CODE_PTR:
1415 type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1);
1416 fprintf_filtered (stream, "*");
1417 break;
1418
1419 case TYPE_CODE_MEMBER:
1420 if (passed_a_ptr)
1421 fprintf_filtered (stream, "(");
1422 type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0,
1423 0);
1424 fprintf_filtered (stream, " ");
1425 type_print_base (TYPE_DOMAIN_TYPE (type), stream, 0,
1426 passed_a_ptr);
1427 fprintf_filtered (stream, "::");
1428 break;
1429
1430 case TYPE_CODE_METHOD:
1431 if (passed_a_ptr)
1432 fprintf (stream, "(");
1433 type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0,
1434 0);
0dce3774
JK
1435 if (passed_a_ptr)
1436 {
1437 fprintf_filtered (stream, " ");
1438 type_print_base (TYPE_DOMAIN_TYPE (type), stream, 0,
1439 passed_a_ptr);
1440 fprintf_filtered (stream, "::");
1441 }
bd5635a1
RP
1442 break;
1443
1444 case TYPE_CODE_REF:
1445 type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1);
1446 fprintf_filtered (stream, "&");
1447 break;
1448
1449 case TYPE_CODE_FUNC:
1450 type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0,
1451 0);
1452 if (passed_a_ptr)
1453 fprintf_filtered (stream, "(");
1454 break;
1455
1456 case TYPE_CODE_ARRAY:
1457 type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0,
1458 0);
1459 if (passed_a_ptr)
1460 fprintf_filtered (stream, "(");
1461
1462 case TYPE_CODE_UNDEF:
1463 case TYPE_CODE_STRUCT:
1464 case TYPE_CODE_UNION:
1465 case TYPE_CODE_ENUM:
1466 case TYPE_CODE_INT:
1467 case TYPE_CODE_FLT:
1468 case TYPE_CODE_VOID:
1469 case TYPE_CODE_ERROR:
1470 /* These types need no prefix. They are listed here so that
1471 gcc -Wall will reveal any types that haven't been handled. */
1472 break;
1473 }
1474}
1475
1476/* Print any array sizes, function arguments or close parentheses
1477 needed after the variable name (to describe its type).
1478 Args work like type_print_varspec_prefix. */
1479
1480static void
1481type_print_varspec_suffix (type, stream, show, passed_a_ptr)
1482 struct type *type;
1483 FILE *stream;
1484 int show;
1485 int passed_a_ptr;
1486{
1487 if (type == 0)
1488 return;
1489
1490 if (TYPE_NAME (type) && show <= 0)
1491 return;
1492
1493 QUIT;
1494
1495 switch (TYPE_CODE (type))
1496 {
1497 case TYPE_CODE_ARRAY:
1498 if (passed_a_ptr)
1499 fprintf_filtered (stream, ")");
1500
1501 fprintf_filtered (stream, "[");
bee3c1a1
JG
1502 if (TYPE_LENGTH (type) > 0
1503 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
bd5635a1
RP
1504 fprintf_filtered (stream, "%d",
1505 (TYPE_LENGTH (type)
1506 / TYPE_LENGTH (TYPE_TARGET_TYPE (type))));
1507 fprintf_filtered (stream, "]");
1508
1509 type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
1510 0);
1511 break;
1512
1513 case TYPE_CODE_MEMBER:
1514 if (passed_a_ptr)
1515 fprintf_filtered (stream, ")");
1516 type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0);
1517 break;
1518
1519 case TYPE_CODE_METHOD:
1520 if (passed_a_ptr)
1521 fprintf_filtered (stream, ")");
1522 type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0);
1523 if (passed_a_ptr)
1524 {
1525 int i;
1526 struct type **args = TYPE_ARG_TYPES (type);
1527
1528 fprintf_filtered (stream, "(");
1529 if (args[1] == 0)
1530 fprintf_filtered (stream, "...");
1531 else for (i = 1; args[i] != 0 && args[i]->code != TYPE_CODE_VOID; i++)
1532 {
1533 type_print_1 (args[i], "", stream, -1, 0);
1534 if (args[i+1] == 0)
1535 fprintf_filtered (stream, "...");
8ffd75c8 1536 else if (args[i+1]->code != TYPE_CODE_VOID) {
bd5635a1 1537 fprintf_filtered (stream, ",");
8ffd75c8
JG
1538 wrap_here (" ");
1539 }
bd5635a1
RP
1540 }
1541 fprintf_filtered (stream, ")");
1542 }
1543 break;
1544
1545 case TYPE_CODE_PTR:
1546 case TYPE_CODE_REF:
1547 type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 1);
1548 break;
1549
1550 case TYPE_CODE_FUNC:
1551 type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
1552 passed_a_ptr);
1553 if (passed_a_ptr)
1554 fprintf_filtered (stream, ")");
1555 fprintf_filtered (stream, "()");
1556 break;
1557
1558 case TYPE_CODE_UNDEF:
1559 case TYPE_CODE_STRUCT:
1560 case TYPE_CODE_UNION:
1561 case TYPE_CODE_ENUM:
1562 case TYPE_CODE_INT:
1563 case TYPE_CODE_FLT:
1564 case TYPE_CODE_VOID:
1565 case TYPE_CODE_ERROR:
1566 /* These types do not need a suffix. They are listed so that
1567 gcc -Wall will report types that may not have been considered. */
1568 break;
1569 }
1570}
1571
1572/* Print the name of the type (or the ultimate pointer target,
1573 function value or array element), or the description of a
1574 structure or union.
1575
1576 SHOW nonzero means don't print this type as just its name;
1577 show its real definition even if it has a name.
1578 SHOW zero means print just typename or struct tag if there is one
1579 SHOW negative means abbreviate structure elements.
1580 SHOW is decremented for printing of structure elements.
1581
1582 LEVEL is the depth to indent by.
1583 We increase it for some recursive calls. */
1584
1585static void
1586type_print_base (type, stream, show, level)
1587 struct type *type;
1588 FILE *stream;
1589 int show;
1590 int level;
1591{
1592 char *name;
1593 register int i;
1594 register int len;
1595 register int lastval;
1596
1597 QUIT;
1598
8ffd75c8 1599 wrap_here (" ");
bd5635a1
RP
1600 if (type == 0)
1601 {
1602 fprintf_filtered (stream, "type unknown");
1603 return;
1604 }
1605
1606 if (TYPE_NAME (type) && show <= 0)
1607 {
1608 fputs_filtered (TYPE_NAME (type), stream);
1609 return;
1610 }
1611
1612 switch (TYPE_CODE (type))
1613 {
1614 case TYPE_CODE_ARRAY:
1615 case TYPE_CODE_PTR:
1616 case TYPE_CODE_MEMBER:
1617 case TYPE_CODE_REF:
1618 case TYPE_CODE_FUNC:
1619 case TYPE_CODE_METHOD:
1620 type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
1621 break;
1622
1623 case TYPE_CODE_STRUCT:
1624 fprintf_filtered (stream, "struct ");
1625 goto struct_union;
1626
1627 case TYPE_CODE_UNION:
1628 fprintf_filtered (stream, "union ");
1629 struct_union:
1630 if (name = type_name_no_tag (type))
1631 {
1632 fputs_filtered (name, stream);
1633 fputs_filtered (" ", stream);
8ffd75c8 1634 wrap_here (" ");
bd5635a1
RP
1635 }
1636 if (show < 0)
1637 fprintf_filtered (stream, "{...}");
1638 else
1639 {
1640 check_stub_type (type);
1641
1642 type_print_derivation_info (stream, type);
1643
1644 fprintf_filtered (stream, "{");
1645 len = TYPE_NFIELDS (type);
1646 if (len)
1647 fprintf_filtered (stream, "\n");
1648 else
1649 {
1650 if (TYPE_FLAGS (type) & TYPE_FLAG_STUB)
1651 fprintf_filtered (stream, "<incomplete type>\n");
1652 else
1653 fprintf_filtered (stream, "<no data fields>\n");
1654 }
1655
1656 /* If there is a base class for this type,
1657 do not print the field that it occupies. */
1658 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
1659 {
1660 QUIT;
1661 /* Don't print out virtual function table. */
1662 if ((TYPE_FIELD_NAME (type, i))[5] == CPLUS_MARKER &&
1663 !strncmp (TYPE_FIELD_NAME (type, i), "_vptr", 5))
1664 continue;
1665
1666 print_spaces_filtered (level + 4, stream);
1667 if (TYPE_FIELD_STATIC (type, i))
1668 {
1669 fprintf_filtered (stream, "static ");
1670 }
1671 type_print_1 (TYPE_FIELD_TYPE (type, i),
1672 TYPE_FIELD_NAME (type, i),
1673 stream, show - 1, level + 4);
1674 if (!TYPE_FIELD_STATIC (type, i)
1675 && TYPE_FIELD_PACKED (type, i))
1676 {
1677 /* It is a bitfield. This code does not attempt
1678 to look at the bitpos and reconstruct filler,
1679 unnamed fields. This would lead to misleading
1680 results if the compiler does not put out fields
1681 for such things (I don't know what it does). */
1682 fprintf_filtered (stream, " : %d",
1683 TYPE_FIELD_BITSIZE (type, i));
1684 }
1685 fprintf_filtered (stream, ";\n");
1686 }
1687
1688 /* C++: print out the methods */
1689 len = TYPE_NFN_FIELDS (type);
1690 if (len) fprintf_filtered (stream, "\n");
1691 for (i = 0; i < len; i++)
1692 {
1693 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1694 int j, len2 = TYPE_FN_FIELDLIST_LENGTH (type, i);
1695
1696 for (j = 0; j < len2; j++)
1697 {
1698 QUIT;
1699 print_spaces_filtered (level + 4, stream);
1700 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1701 fprintf_filtered (stream, "virtual ");
1702 else if (TYPE_FN_FIELD_STATIC_P (f, j))
1703 fprintf_filtered (stream, "static ");
aec4cb91
MT
1704 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)) == 0)
1705 {
1706 /* Keep GDB from crashing here. */
1707 fprintf (stream, "<undefined type> %s;\n",
1708 TYPE_FN_FIELD_PHYSNAME (f, j));
1709 break;
1710 }
1711 else
1712 type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)), "", stream, 0);
bd5635a1
RP
1713 if (TYPE_FLAGS (TYPE_FN_FIELD_TYPE (f, j)) & TYPE_FLAG_STUB)
1714 {
1715 /* Build something we can demangle. */
1716 char *strchr (), *gdb_mangle_typename ();
1717 char *inner_name = gdb_mangle_typename (type);
1718 char *mangled_name
1719 = (char *)xmalloc (strlen (TYPE_FN_FIELDLIST_NAME (type, i))
1720 + strlen (inner_name)
1721 + strlen (TYPE_FN_FIELD_PHYSNAME (f, j))
1722 + 1);
1723 char *demangled_name, *cplus_demangle ();
1724 strcpy (mangled_name, TYPE_FN_FIELDLIST_NAME (type, i));
1725 strcat (mangled_name, inner_name);
1726 strcat (mangled_name, TYPE_FN_FIELD_PHYSNAME (f, j));
1727 demangled_name = cplus_demangle (mangled_name, 1);
1728 if (demangled_name == 0)
1729 fprintf_filtered (stream, " <badly mangled name %s>",
1730 mangled_name);
1731 else
1732 {
1733 fprintf_filtered (stream, " %s",
1734 strchr (demangled_name, ':') + 2);
1735 free (demangled_name);
1736 }
1737 free (mangled_name);
1738 }
1739 else if (TYPE_FN_FIELD_PHYSNAME (f, j)[0] == '_'
1740 && TYPE_FN_FIELD_PHYSNAME (f, j)[1] == CPLUS_MARKER)
1741 type_print_method_args
1742 (TYPE_FN_FIELD_ARGS (f, j) + 1, "~",
1743 TYPE_FN_FIELDLIST_NAME (type, i), 0, stream);
1744 else
1745 type_print_method_args
1746 (TYPE_FN_FIELD_ARGS (f, j), "",
1747 TYPE_FN_FIELDLIST_NAME (type, i),
1748 TYPE_FN_FIELD_STATIC_P (f, j), stream);
1749
1750 fprintf_filtered (stream, ";\n");
1751 }
1752 }
1753
1754 print_spaces_filtered (level, stream);
1755 fprintf_filtered (stream, "}");
1756 }
1757 break;
1758
1759 case TYPE_CODE_ENUM:
1760 fprintf_filtered (stream, "enum ");
1761 if (name = type_name_no_tag (type))
1762 {
1763 fputs_filtered (name, stream);
1764 fputs_filtered (" ", stream);
1765 }
8ffd75c8 1766 wrap_here (" ");
bd5635a1
RP
1767 if (show < 0)
1768 fprintf_filtered (stream, "{...}");
1769 else
1770 {
1771 fprintf_filtered (stream, "{");
1772 len = TYPE_NFIELDS (type);
1773 lastval = 0;
1774 for (i = 0; i < len; i++)
1775 {
1776 QUIT;
1777 if (i) fprintf_filtered (stream, ", ");
8ffd75c8 1778 wrap_here (" ");
bd5635a1
RP
1779 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
1780 if (lastval != TYPE_FIELD_BITPOS (type, i))
1781 {
1782 fprintf_filtered (stream, " = %d", TYPE_FIELD_BITPOS (type, i));
1783 lastval = TYPE_FIELD_BITPOS (type, i);
1784 }
1785 lastval++;
1786 }
1787 fprintf_filtered (stream, "}");
1788 }
1789 break;
1790
1791 case TYPE_CODE_INT:
bee3c1a1
JG
1792 name = 0;
1793 if (TYPE_LENGTH (type) <= sizeof (LONGEST))
bd5635a1
RP
1794 {
1795 if (TYPE_UNSIGNED (type))
1796 name = unsigned_type_table[TYPE_LENGTH (type)];
1797 else
1798 name = signed_type_table[TYPE_LENGTH (type)];
1799 }
bee3c1a1
JG
1800 if (name)
1801 fputs_filtered (name, stream);
1802 else
1803 fprintf_filtered (stream, "<%d bit integer>",
1804 TYPE_LENGTH (type) * TARGET_CHAR_BIT);
bd5635a1
RP
1805 break;
1806
1807 case TYPE_CODE_FLT:
1808 name = float_type_table[TYPE_LENGTH (type)];
1809 fputs_filtered (name, stream);
1810 break;
1811
1812 case TYPE_CODE_VOID:
1813 fprintf_filtered (stream, "void");
1814 break;
1815
1816 case 0:
1817 fprintf_filtered (stream, "struct unknown");
1818 break;
1819
1820 case TYPE_CODE_ERROR:
1821 fprintf_filtered (stream, "<unknown type>");
1822 break;
1823
1824 default:
1825 error ("Invalid type code in symbol table.");
1826 }
1827}
1828\f
e1ce8aa5 1829#if 0
bd5635a1
RP
1830/* Validate an input or output radix setting, and make sure the user
1831 knows what they really did here. Radix setting is confusing, e.g.
1832 setting the input radix to "10" never changes it! */
1833
e1ce8aa5 1834/* ARGSUSED */
bd5635a1
RP
1835static void
1836set_input_radix (args, from_tty, c)
1837 char *args;
1838 int from_tty;
1839 struct cmd_list_element *c;
1840{
1841 unsigned radix = *(unsigned *)c->var;
1842
1843 if (from_tty)
1844 printf_filtered ("Input radix set to decimal %d, hex %x, octal %o\n",
1845 radix, radix, radix);
1846}
e1ce8aa5 1847#endif
bd5635a1 1848
e1ce8aa5 1849/* ARGSUSED */
bd5635a1
RP
1850static void
1851set_output_radix (args, from_tty, c)
1852 char *args;
1853 int from_tty;
1854 struct cmd_list_element *c;
1855{
1856 unsigned radix = *(unsigned *)c->var;
1857
1858 if (from_tty)
1859 printf_filtered ("Output radix set to decimal %d, hex %x, octal %o\n",
1860 radix, radix, radix);
1861
1862 /* FIXME, we really should be able to validate the setting BEFORE
1863 it takes effect. */
1864 switch (radix)
1865 {
1866 case 16:
1867 output_format = 'x';
1868 break;
1869 case 10:
1870 output_format = 0;
1871 break;
1872 case 8:
1873 output_format = 'o'; /* octal */
1874 break;
1875 default:
1876 output_format = 0;
1877 error ("Unsupported radix ``decimal %d''; using decimal output",
1878 radix);
1879 }
1880}
1881
1882/* Both at once */
1883static void
1884set_radix (arg, from_tty, c)
1885 char *arg;
1886 int from_tty;
1887 struct cmd_list_element *c;
1888{
1889 unsigned radix = *(unsigned *)c->var;
1890
1891 if (from_tty)
1892 printf_filtered ("Radix set to decimal %d, hex %x, octal %o\n",
1893 radix, radix, radix);
1894
1895 input_radix = radix;
1896 output_radix = radix;
1897
1898 set_output_radix (arg, 0, c);
1899}
1900\f
f266e564
JK
1901struct cmd_list_element *setprintlist = NULL;
1902struct cmd_list_element *showprintlist = NULL;
1903
1904/*ARGSUSED*/
1905static void
1906set_print (arg, from_tty)
1907 char *arg;
1908 int from_tty;
1909{
1910 printf (
1911"\"set print\" must be followed by the name of a print subcommand.\n");
1912 help_list (setprintlist, "set print ", -1, stdout);
1913}
1914
1915/*ARGSUSED*/
1916static void
1917show_print (args, from_tty)
1918 char *args;
1919 int from_tty;
1920{
1921 cmd_show_list (showprintlist, from_tty, "");
1922}
1923\f
bd5635a1
RP
1924void
1925_initialize_valprint ()
1926{
1927 struct cmd_list_element *c;
1928
f266e564
JK
1929 add_prefix_cmd ("print", no_class, set_print,
1930 "Generic command for setting how things print.",
1931 &setprintlist, "set print ", 0, &setlist);
36b9d39c
JG
1932 add_alias_cmd ("p", "print", no_class, 1, &setlist);
1933 add_alias_cmd ("pr", "print", no_class, 1, &setlist); /* prefer set print
1934 to set prompt */
f266e564
JK
1935 add_prefix_cmd ("print", no_class, show_print,
1936 "Generic command for showing print settings.",
1937 &showprintlist, "show print ", 0, &showlist);
36b9d39c
JG
1938 add_alias_cmd ("p", "print", no_class, 1, &showlist);
1939 add_alias_cmd ("pr", "print", no_class, 1, &showlist);
f266e564 1940
bd5635a1 1941 add_show_from_set
f266e564 1942 (add_set_cmd ("elements", no_class, var_uinteger, (char *)&print_max,
bd5635a1 1943 "Set limit on string chars or array elements to print.\n\
f266e564
JK
1944\"set print elements 0\" causes there to be no limit.",
1945 &setprintlist),
1946 &showprintlist);
bd5635a1
RP
1947
1948 add_show_from_set
f266e564 1949 (add_set_cmd ("pretty", class_support, var_boolean, (char *)&prettyprint,
bd5635a1 1950 "Set prettyprinting of structures.",
f266e564
JK
1951 &setprintlist),
1952 &showprintlist);
bd5635a1
RP
1953
1954 add_show_from_set
f266e564 1955 (add_set_cmd ("union", class_support, var_boolean, (char *)&unionprint,
bd5635a1 1956 "Set printing of unions interior to structures.",
f266e564
JK
1957 &setprintlist),
1958 &showprintlist);
bd5635a1
RP
1959
1960 add_show_from_set
f266e564 1961 (add_set_cmd ("vtbl", class_support, var_boolean, (char *)&vtblprint,
bd5635a1 1962 "Set printing of C++ virtual function tables.",
f266e564
JK
1963 &setprintlist),
1964 &showprintlist);
bd5635a1
RP
1965
1966 add_show_from_set
f266e564 1967 (add_set_cmd ("array", class_support, var_boolean, (char *)&arrayprint,
bd5635a1 1968 "Set prettyprinting of arrays.",
f266e564
JK
1969 &setprintlist),
1970 &showprintlist);
bd5635a1 1971
0dce3774
JK
1972 add_show_from_set
1973 (add_set_cmd ("object", class_support, var_boolean, (char *)&objectprint,
1974 "Set printing of object's derived type based on vtable info.",
1975 &setprintlist),
1976 &showprintlist);
1977
bd5635a1 1978 add_show_from_set
f266e564 1979 (add_set_cmd ("address", class_support, var_boolean, (char *)&addressprint,
bd5635a1 1980 "Set printing of addresses.",
f266e564
JK
1981 &setprintlist),
1982 &showprintlist);
bd5635a1
RP
1983
1984#if 0
1985 /* The "show radix" cmd isn't good enough to show two separate values.
1986 The rest of the code works, but the show part is confusing, so don't
1987 let them be set separately 'til we work out "show". */
1988 c = add_set_cmd ("input-radix", class_support, var_uinteger,
1989 (char *)&input_radix,
1990 "Set default input radix for entering numbers.",
1991 &setlist);
1992 add_show_from_set (c, &showlist);
1993 c->function = set_input_radix;
1994
1995 c = add_set_cmd ("output-radix", class_support, var_uinteger,
1996 (char *)&output_radix,
1997 "Set default output radix for printing of values.",
1998 &setlist);
1999 add_show_from_set (c, &showlist);
2000 c->function = set_output_radix;
2001#endif
2002
2003 c = add_set_cmd ("radix", class_support, var_uinteger,
2004 (char *)&output_radix,
2005 "Set default input and output number radix.",
2006 &setlist);
2007 add_show_from_set (c, &showlist);
2008 c->function = set_radix;
2009
2010 /* Give people the defaults which they are used to. */
2011 prettyprint = 0;
2012 unionprint = 1;
2013 vtblprint = 0;
2014 arrayprint = 0;
2015 addressprint = 1;
0dce3774 2016 objectprint = 0;
bd5635a1
RP
2017
2018 print_max = 200;
2019
bee3c1a1
JG
2020 /* FIXME! This assumes that these sizes and types are the same on the
2021 host and target machines! */
bd5635a1
RP
2022 unsigned_type_table
2023 = (char **) xmalloc ((1 + sizeof (unsigned LONGEST)) * sizeof (char *));
2024 bzero (unsigned_type_table, (1 + sizeof (unsigned LONGEST)));
2025 unsigned_type_table[sizeof (unsigned char)] = "unsigned char";
2026 unsigned_type_table[sizeof (unsigned short)] = "unsigned short";
2027 unsigned_type_table[sizeof (unsigned long)] = "unsigned long";
2028 unsigned_type_table[sizeof (unsigned int)] = "unsigned int";
2029#ifdef LONG_LONG
bee3c1a1
JG
2030 unsigned_type_table[TARGET_LONG_LONG_BIT/TARGET_CHAR_BIT] =
2031 "unsigned long long";
bd5635a1
RP
2032#endif
2033
2034 signed_type_table
2035 = (char **) xmalloc ((1 + sizeof (LONGEST)) * sizeof (char *));
2036 bzero (signed_type_table, (1 + sizeof (LONGEST)));
2037 signed_type_table[sizeof (char)] = "char";
2038 signed_type_table[sizeof (short)] = "short";
2039 signed_type_table[sizeof (long)] = "long";
2040 signed_type_table[sizeof (int)] = "int";
2041#ifdef LONG_LONG
bee3c1a1 2042 signed_type_table[TARGET_LONG_LONG_BIT/TARGET_CHAR_BIT] = "long long";
bd5635a1
RP
2043#endif
2044
2045 float_type_table
2046 = (char **) xmalloc ((1 + sizeof (double)) * sizeof (char *));
2047 bzero (float_type_table, (1 + sizeof (double)));
2048 float_type_table[sizeof (float)] = "float";
2049 float_type_table[sizeof (double)] = "double";
2050 obstack_begin (&dont_print_obstack, 32 * sizeof (struct type *));
2051}
This page took 0.114889 seconds and 4 git commands to generate.