1 /* Support for printing C and C++ types for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1991 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program 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 2 of the License, or
9 (at your option) any later version.
11 This program 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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #include "bfd.h" /* Binary File Description */
25 #include "expression.h"
34 #include "typeprint.h"
41 c_type_print_args
PARAMS ((struct type
*, GDB_FILE
*));
44 c_type_print_varspec_suffix
PARAMS ((struct type
*, GDB_FILE
*, int, int, int));
47 cp_type_print_derivation_info
PARAMS ((GDB_FILE
*, struct type
*));
50 c_type_print_varspec_prefix
PARAMS ((struct type
*, GDB_FILE
*, int, int));
53 c_type_print_base
PARAMS ((struct type
*, GDB_FILE
*, int, int));
56 /* Print a description of a type in the format of a
57 typedef for the current language.
58 NEW is the new name for a type TYPE. */
61 c_typedef_print (type
, new, stream
)
66 switch (current_language
->la_language
)
71 fprintf_filtered(stream
, "typedef ");
72 type_print(type
,"",stream
,0);
73 if(TYPE_NAME ((SYMBOL_TYPE (new))) == 0
74 || !STREQ (TYPE_NAME ((SYMBOL_TYPE (new))), SYMBOL_NAME (new)))
75 fprintf_filtered(stream
, " %s", SYMBOL_SOURCE_NAME(new));
80 fprintf_filtered(stream
, "TYPE ");
81 if(!TYPE_NAME(SYMBOL_TYPE(new)) ||
82 !STREQ (TYPE_NAME(SYMBOL_TYPE(new)), SYMBOL_NAME(new)))
83 fprintf_filtered(stream
, "%s = ", SYMBOL_SOURCE_NAME(new));
85 fprintf_filtered(stream
, "<builtin> = ");
86 type_print(type
,"",stream
,0);
91 fprintf_filtered(stream
, "SYNMODE ");
92 if(!TYPE_NAME(SYMBOL_TYPE(new)) ||
93 !STREQ (TYPE_NAME(SYMBOL_TYPE(new)), SYMBOL_NAME(new)))
94 fprintf_filtered(stream
, "%s = ", SYMBOL_SOURCE_NAME(new));
96 fprintf_filtered(stream
, "<builtin> = ");
97 type_print(type
,"",stream
,0);
101 error("Language not supported.");
103 fprintf_filtered(stream
, ";\n");
107 /* LEVEL is the depth to indent lines by. */
110 c_print_type (type
, varstring
, stream
, show
, level
)
117 register enum type_code code
;
120 c_type_print_base (type
, stream
, show
, level
);
121 code
= TYPE_CODE (type
);
122 if ((varstring
!= NULL
&& *varstring
!= '\0')
124 /* Need a space if going to print stars or brackets;
125 but not if we will print just a type name. */
126 ((show
> 0 || TYPE_NAME (type
) == 0)
128 (code
== TYPE_CODE_PTR
|| code
== TYPE_CODE_FUNC
129 || code
== TYPE_CODE_METHOD
130 || code
== TYPE_CODE_ARRAY
131 || code
== TYPE_CODE_MEMBER
132 || code
== TYPE_CODE_REF
)))
133 fputs_filtered (" ", stream
);
134 c_type_print_varspec_prefix (type
, stream
, show
, 0);
136 fputs_filtered (varstring
, stream
);
138 /* For demangled function names, we have the arglist as part of the name,
139 so don't print an additional pair of ()'s */
141 demangled_args
= strchr(varstring
, '(') != NULL
;
142 c_type_print_varspec_suffix (type
, stream
, show
, 0, demangled_args
);
146 /* Print the C++ method arguments ARGS to the file STREAM. */
149 cp_type_print_method_args (args
, prefix
, varstring
, staticp
, stream
)
158 fprintf_symbol_filtered (stream
, prefix
, language_cplus
, DMGL_ANSI
);
159 fprintf_symbol_filtered (stream
, varstring
, language_cplus
, DMGL_ANSI
);
160 fputs_filtered (" (", stream
);
161 if (args
&& args
[!staticp
] && args
[!staticp
]->code
!= TYPE_CODE_VOID
)
163 i
= !staticp
; /* skip the class variable */
166 type_print (args
[i
++], "", stream
, 0);
169 fprintf_filtered (stream
, " ...");
172 else if (args
[i
]->code
!= TYPE_CODE_VOID
)
174 fprintf_filtered (stream
, ", ");
179 fprintf_filtered (stream
, ")");
182 /* If TYPE is a derived type, then print out derivation information.
183 Print only the actual base classes of this type, not the base classes
184 of the base classes. I.E. for the derivation hierarchy:
187 class B : public A {int b; };
188 class C : public B {int c; };
190 Print the type of class C as:
196 Not as the following (like gdb used to), which is not legal C++ syntax for
197 derived types and may be confused with the multiple inheritance form:
199 class C : public B : public A {
203 In general, gdb should try to print the types as closely as possible to
204 the form that they appear in the source code. */
207 cp_type_print_derivation_info (stream
, type
)
214 for (i
= 0; i
< TYPE_N_BASECLASSES (type
); i
++)
216 fputs_filtered (i
== 0 ? ": " : ", ", stream
);
217 fprintf_filtered (stream
, "%s%s ",
218 BASETYPE_VIA_PUBLIC (type
, i
) ? "public" : "private",
219 BASETYPE_VIA_VIRTUAL(type
, i
) ? " virtual" : "");
220 name
= type_name_no_tag (TYPE_BASECLASS (type
, i
));
221 fprintf_filtered (stream
, "%s", name
? name
: "(null)");
225 fputs_filtered (" ", stream
);
229 /* Print any asterisks or open-parentheses needed before the
230 variable name (to describe its type).
232 On outermost call, pass 0 for PASSED_A_PTR.
233 On outermost call, SHOW > 0 means should ignore
234 any typename for TYPE and show its details.
235 SHOW is always zero on recursive calls. */
238 c_type_print_varspec_prefix (type
, stream
, show
, passed_a_ptr
)
248 if (TYPE_NAME (type
) && show
<= 0)
253 switch (TYPE_CODE (type
))
256 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 1);
257 fprintf_filtered (stream
, "*");
260 case TYPE_CODE_MEMBER
:
262 fprintf_filtered (stream
, "(");
263 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
264 fprintf_filtered (stream
, " ");
265 name
= type_name_no_tag (TYPE_DOMAIN_TYPE (type
));
267 fputs_filtered (name
, stream
);
269 c_type_print_base (TYPE_DOMAIN_TYPE (type
), stream
, 0, passed_a_ptr
);
270 fprintf_filtered (stream
, "::");
273 case TYPE_CODE_METHOD
:
275 fprintf_unfiltered (stream
, "(");
276 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
279 fprintf_filtered (stream
, " ");
280 c_type_print_base (TYPE_DOMAIN_TYPE (type
), stream
, 0, passed_a_ptr
);
281 fprintf_filtered (stream
, "::");
286 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 1);
287 fprintf_filtered (stream
, "&");
291 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
293 fprintf_filtered (stream
, "(");
296 case TYPE_CODE_ARRAY
:
297 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
299 fprintf_filtered (stream
, "(");
302 case TYPE_CODE_UNDEF
:
303 case TYPE_CODE_STRUCT
:
304 case TYPE_CODE_UNION
:
309 case TYPE_CODE_ERROR
:
313 case TYPE_CODE_RANGE
:
314 case TYPE_CODE_STRING
:
315 case TYPE_CODE_BITSTRING
:
316 case TYPE_CODE_COMPLEX
:
317 case TYPE_CODE_LITERAL_COMPLEX
:
318 case TYPE_CODE_LITERAL_STRING
:
319 /* These types need no prefix. They are listed here so that
320 gcc -Wall will reveal any types that haven't been handled. */
326 c_type_print_args (type
, stream
)
333 fprintf_filtered (stream
, "(");
334 args
= TYPE_ARG_TYPES (type
);
339 fprintf_filtered (stream
, "...");
344 args
[i
] != NULL
&& args
[i
]->code
!= TYPE_CODE_VOID
;
347 c_print_type (args
[i
], "", stream
, -1, 0);
348 if (args
[i
+1] == NULL
)
350 fprintf_filtered (stream
, "...");
352 else if (args
[i
+1]->code
!= TYPE_CODE_VOID
)
354 fprintf_filtered (stream
, ",");
360 fprintf_filtered (stream
, ")");
363 /* Print any array sizes, function arguments or close parentheses
364 needed after the variable name (to describe its type).
365 Args work like c_type_print_varspec_prefix. */
368 c_type_print_varspec_suffix (type
, stream
, show
, passed_a_ptr
, demangled_args
)
378 if (TYPE_NAME (type
) && show
<= 0)
383 switch (TYPE_CODE (type
))
385 case TYPE_CODE_ARRAY
:
387 fprintf_filtered (stream
, ")");
389 fprintf_filtered (stream
, "[");
390 if (TYPE_LENGTH (type
) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type
)) > 0)
391 fprintf_filtered (stream
, "%d",
393 / TYPE_LENGTH (TYPE_TARGET_TYPE (type
))));
394 fprintf_filtered (stream
, "]");
396 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 0, 0);
399 case TYPE_CODE_MEMBER
:
401 fprintf_filtered (stream
, ")");
402 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 0, 0);
405 case TYPE_CODE_METHOD
:
407 fprintf_filtered (stream
, ")");
408 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 0, 0);
411 c_type_print_args (type
, stream
);
417 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 1, 0);
422 fprintf_filtered (stream
, ")");
424 fprintf_filtered (stream
, "()");
425 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0,
429 case TYPE_CODE_UNDEF
:
430 case TYPE_CODE_STRUCT
:
431 case TYPE_CODE_UNION
:
436 case TYPE_CODE_ERROR
:
440 case TYPE_CODE_RANGE
:
441 case TYPE_CODE_STRING
:
442 case TYPE_CODE_BITSTRING
:
443 case TYPE_CODE_COMPLEX
:
444 case TYPE_CODE_LITERAL_COMPLEX
:
445 case TYPE_CODE_LITERAL_STRING
:
446 /* These types do not need a suffix. They are listed so that
447 gcc -Wall will report types that may not have been considered. */
452 /* Print the name of the type (or the ultimate pointer target,
453 function value or array element), or the description of a
456 SHOW positive means print details about the type (e.g. enum values),
457 and print structure elements passing SHOW - 1 for show.
458 SHOW negative means just print the type name or struct tag if there is one.
459 If there is no name, print something sensible but concise like
461 SHOW zero means just print the type name or struct tag if there is one.
462 If there is no name, print something sensible but not as concise like
463 "struct {int x; int y;}".
465 LEVEL is the number of spaces to indent by.
466 We increase it for some recursive calls. */
469 c_type_print_base (type
, stream
, show
, level
)
477 register int lastval
;
479 char *demangled_name
;
480 enum {s_none
, s_public
, s_private
, s_protected
} section_type
;
486 fputs_filtered ("<type unknown>", stream
);
490 /* When SHOW is zero or less, and there is a valid type name, then always
491 just print the type name directly from the type. */
492 /* If we have "typedef struct foo {. . .} bar;" do we want to print it
493 as "struct foo" or as "bar"? Pick the latter, because C++ folk tend
494 to expect things like "class5 *foo" rather than "struct class5 *foo". */
497 && TYPE_NAME (type
) != NULL
)
499 fputs_filtered (TYPE_NAME (type
), stream
);
503 check_stub_type (type
);
505 switch (TYPE_CODE (type
))
507 case TYPE_CODE_ARRAY
:
509 case TYPE_CODE_MEMBER
:
512 case TYPE_CODE_METHOD
:
513 c_type_print_base (TYPE_TARGET_TYPE (type
), stream
, show
, level
);
516 case TYPE_CODE_STRUCT
:
517 if (HAVE_CPLUS_STRUCT (type
))
519 fprintf_filtered (stream
, "class ");
523 fprintf_filtered (stream
, "struct ");
527 case TYPE_CODE_UNION
:
528 fprintf_filtered (stream
, "union ");
531 if (TYPE_TAG_NAME (type
) != NULL
)
533 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
535 fputs_filtered (" ", stream
);
540 /* If we just printed a tag name, no need to print anything else. */
541 if (TYPE_TAG_NAME (type
) == NULL
)
542 fprintf_filtered (stream
, "{...}");
544 else if (show
> 0 || TYPE_TAG_NAME (type
) == NULL
)
546 cp_type_print_derivation_info (stream
, type
);
548 fprintf_filtered (stream
, "{\n");
549 if ((TYPE_NFIELDS (type
) == 0) && (TYPE_NFN_FIELDS (type
) == 0))
551 if (TYPE_FLAGS (type
) & TYPE_FLAG_STUB
)
552 fprintfi_filtered (level
+ 4, stream
, "<incomplete type>\n");
554 fprintfi_filtered (level
+ 4, stream
, "<no data fields>\n");
557 /* Start off with no specific section type, so we can print
558 one for the first field we find, and use that section type
559 thereafter until we find another type. */
561 section_type
= s_none
;
563 /* If there is a base class for this type,
564 do not print the field that it occupies. */
566 len
= TYPE_NFIELDS (type
);
567 for (i
= TYPE_N_BASECLASSES (type
); i
< len
; i
++)
570 /* Don't print out virtual function table. */
571 if ((TYPE_FIELD_NAME (type
, i
))[5] == CPLUS_MARKER
&&
572 !strncmp (TYPE_FIELD_NAME (type
, i
), "_vptr", 5))
575 /* If this is a C++ class we can print the various C++ section
578 if (HAVE_CPLUS_STRUCT (type
))
580 if (TYPE_FIELD_PROTECTED (type
, i
))
582 if (section_type
!= s_protected
)
584 section_type
= s_protected
;
585 fprintfi_filtered (level
+ 2, stream
,
589 else if (TYPE_FIELD_PRIVATE (type
, i
))
591 if (section_type
!= s_private
)
593 section_type
= s_private
;
594 fprintfi_filtered (level
+ 2, stream
, "private:\n");
599 if (section_type
!= s_public
)
601 section_type
= s_public
;
602 fprintfi_filtered (level
+ 2, stream
, "public:\n");
607 print_spaces_filtered (level
+ 4, stream
);
608 if (TYPE_FIELD_STATIC (type
, i
))
610 fprintf_filtered (stream
, "static ");
612 c_print_type (TYPE_FIELD_TYPE (type
, i
),
613 TYPE_FIELD_NAME (type
, i
),
614 stream
, show
- 1, level
+ 4);
615 if (!TYPE_FIELD_STATIC (type
, i
)
616 && TYPE_FIELD_PACKED (type
, i
))
618 /* It is a bitfield. This code does not attempt
619 to look at the bitpos and reconstruct filler,
620 unnamed fields. This would lead to misleading
621 results if the compiler does not put out fields
622 for such things (I don't know what it does). */
623 fprintf_filtered (stream
, " : %d",
624 TYPE_FIELD_BITSIZE (type
, i
));
626 fprintf_filtered (stream
, ";\n");
629 /* If there are both fields and methods, put a space between. */
630 len
= TYPE_NFN_FIELDS (type
);
631 if (len
&& section_type
!= s_none
)
632 fprintf_filtered (stream
, "\n");
634 /* C++: print out the methods */
636 for (i
= 0; i
< len
; i
++)
638 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
639 int j
, len2
= TYPE_FN_FIELDLIST_LENGTH (type
, i
);
640 char *method_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
641 char *name
= type_name_no_tag (type
);
642 int is_constructor
= name
&& STREQ(method_name
, name
);
643 for (j
= 0; j
< len2
; j
++)
645 char *physname
= TYPE_FN_FIELD_PHYSNAME (f
, j
);
646 int is_full_physname_constructor
=
647 ((physname
[0]=='_' && physname
[1]=='_' &&
648 (isdigit(physname
[2])
650 || physname
[2]=='t'))
651 || (strncmp(physname
, "__ct__", 6) == 0));
654 if (TYPE_FN_FIELD_PROTECTED (f
, j
))
656 if (section_type
!= s_protected
)
658 section_type
= s_protected
;
659 fprintfi_filtered (level
+ 2, stream
,
663 else if (TYPE_FN_FIELD_PRIVATE (f
, j
))
665 if (section_type
!= s_private
)
667 section_type
= s_private
;
668 fprintfi_filtered (level
+ 2, stream
, "private:\n");
673 if (section_type
!= s_public
)
675 section_type
= s_public
;
676 fprintfi_filtered (level
+ 2, stream
, "public:\n");
680 print_spaces_filtered (level
+ 4, stream
);
681 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
682 fprintf_filtered (stream
, "virtual ");
683 else if (TYPE_FN_FIELD_STATIC_P (f
, j
))
684 fprintf_filtered (stream
, "static ");
685 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)) == 0)
687 /* Keep GDB from crashing here. */
688 fprintf_unfiltered (stream
, "<undefined type> %s;\n",
689 TYPE_FN_FIELD_PHYSNAME (f
, j
));
692 else if (!is_constructor
&& !is_full_physname_constructor
)
694 type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)),
696 fputs_filtered (" ", stream
);
698 if (TYPE_FN_FIELD_STUB (f
, j
))
700 /* Build something we can demangle. */
701 mangled_name
= gdb_mangle_name (type
, i
, j
);
703 cplus_demangle (mangled_name
,
704 DMGL_ANSI
| DMGL_PARAMS
);
705 if (demangled_name
== NULL
)
706 fprintf_filtered (stream
, "<badly mangled name %s>",
710 char *demangled_no_class
=
711 strchr (demangled_name
, ':');
713 if (demangled_no_class
== NULL
)
714 demangled_no_class
= demangled_name
;
717 if (*++demangled_no_class
== ':')
718 ++demangled_no_class
;
720 fputs_filtered (demangled_no_class
, stream
);
721 free (demangled_name
);
725 else if (TYPE_FN_FIELD_PHYSNAME (f
, j
)[0] == '_'
726 && TYPE_FN_FIELD_PHYSNAME (f
, j
)[1] == CPLUS_MARKER
)
727 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f
, j
) + 1,
728 "~", method_name
, 0, stream
);
730 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f
, j
), "",
732 TYPE_FN_FIELD_STATIC_P (f
, j
),
735 fprintf_filtered (stream
, ";\n");
739 fprintfi_filtered (level
, stream
, "}");
744 fprintf_filtered (stream
, "enum ");
745 if (TYPE_TAG_NAME (type
) != NULL
)
747 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
749 fputs_filtered (" ", stream
);
755 /* If we just printed a tag name, no need to print anything else. */
756 if (TYPE_TAG_NAME (type
) == NULL
)
757 fprintf_filtered (stream
, "{...}");
759 else if (show
> 0 || TYPE_TAG_NAME (type
) == NULL
)
761 fprintf_filtered (stream
, "{");
762 len
= TYPE_NFIELDS (type
);
764 for (i
= 0; i
< len
; i
++)
767 if (i
) fprintf_filtered (stream
, ", ");
769 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
770 if (lastval
!= TYPE_FIELD_BITPOS (type
, i
))
772 fprintf_filtered (stream
, " = %d", TYPE_FIELD_BITPOS (type
, i
));
773 lastval
= TYPE_FIELD_BITPOS (type
, i
);
777 fprintf_filtered (stream
, "}");
782 fprintf_filtered (stream
, "void");
785 case TYPE_CODE_UNDEF
:
786 fprintf_filtered (stream
, "struct <unknown>");
789 case TYPE_CODE_ERROR
:
790 fprintf_filtered (stream
, "<unknown type>");
793 case TYPE_CODE_RANGE
:
794 /* This should not occur */
795 fprintf_filtered (stream
, "<range type>");
799 /* Handle types not explicitly handled by the other cases,
800 such as fundamental types. For these, just print whatever
801 the type name is, as recorded in the type itself. If there
802 is no type name, then complain. */
803 if (TYPE_NAME (type
) != NULL
)
805 fputs_filtered (TYPE_NAME (type
), stream
);
809 /* At least for dump_symtab, it is important that this not be
811 fprintf_filtered (stream
, "<invalid type code %d>",