1 /* Support for printing C and C++ types for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1991, 1993, 1994
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #include "bfd.h" /* Binary File Description */
26 #include "expression.h"
35 #include "typeprint.h"
42 c_type_print_args
PARAMS ((struct type
*, GDB_FILE
*));
45 c_type_print_varspec_suffix
PARAMS ((struct type
*, GDB_FILE
*, int, int, int));
48 cp_type_print_derivation_info
PARAMS ((GDB_FILE
*, struct type
*));
51 c_type_print_varspec_prefix
PARAMS ((struct type
*, GDB_FILE
*, int, int));
54 c_type_print_base
PARAMS ((struct type
*, GDB_FILE
*, int, int));
57 /* Print a description of a type in the format of a
58 typedef for the current language.
59 NEW is the new name for a type TYPE. */
62 c_typedef_print (type
, new, stream
)
67 switch (current_language
->la_language
)
72 fprintf_filtered(stream
, "typedef ");
73 type_print(type
,"",stream
,0);
74 if(TYPE_NAME ((SYMBOL_TYPE (new))) == 0
75 || !STREQ (TYPE_NAME ((SYMBOL_TYPE (new))), SYMBOL_NAME (new)))
76 fprintf_filtered(stream
, " %s", SYMBOL_SOURCE_NAME(new));
81 fprintf_filtered(stream
, "TYPE ");
82 if(!TYPE_NAME(SYMBOL_TYPE(new)) ||
83 !STREQ (TYPE_NAME(SYMBOL_TYPE(new)), SYMBOL_NAME(new)))
84 fprintf_filtered(stream
, "%s = ", SYMBOL_SOURCE_NAME(new));
86 fprintf_filtered(stream
, "<builtin> = ");
87 type_print(type
,"",stream
,0);
92 fprintf_filtered(stream
, "SYNMODE ");
93 if(!TYPE_NAME(SYMBOL_TYPE(new)) ||
94 !STREQ (TYPE_NAME(SYMBOL_TYPE(new)), SYMBOL_NAME(new)))
95 fprintf_filtered(stream
, "%s = ", SYMBOL_SOURCE_NAME(new));
97 fprintf_filtered(stream
, "<builtin> = ");
98 type_print(type
,"",stream
,0);
102 error("Language not supported.");
104 fprintf_filtered(stream
, ";\n");
108 /* LEVEL is the depth to indent lines by. */
111 c_print_type (type
, varstring
, stream
, show
, level
)
118 register enum type_code code
;
121 c_type_print_base (type
, stream
, show
, level
);
122 code
= TYPE_CODE (type
);
123 if ((varstring
!= NULL
&& *varstring
!= '\0')
125 /* Need a space if going to print stars or brackets;
126 but not if we will print just a type name. */
127 ((show
> 0 || TYPE_NAME (type
) == 0)
129 (code
== TYPE_CODE_PTR
|| code
== TYPE_CODE_FUNC
130 || code
== TYPE_CODE_METHOD
131 || code
== TYPE_CODE_ARRAY
132 || code
== TYPE_CODE_MEMBER
133 || code
== TYPE_CODE_REF
)))
134 fputs_filtered (" ", stream
);
135 c_type_print_varspec_prefix (type
, stream
, show
, 0);
137 fputs_filtered (varstring
, stream
);
139 /* For demangled function names, we have the arglist as part of the name,
140 so don't print an additional pair of ()'s */
142 demangled_args
= strchr(varstring
, '(') != NULL
;
143 c_type_print_varspec_suffix (type
, stream
, show
, 0, demangled_args
);
147 /* Print the C++ method arguments ARGS to the file STREAM. */
150 cp_type_print_method_args (args
, prefix
, varstring
, staticp
, stream
)
159 fprintf_symbol_filtered (stream
, prefix
, language_cplus
, DMGL_ANSI
);
160 fprintf_symbol_filtered (stream
, varstring
, language_cplus
, DMGL_ANSI
);
161 fputs_filtered (" (", stream
);
162 if (args
&& args
[!staticp
] && args
[!staticp
]->code
!= TYPE_CODE_VOID
)
164 i
= !staticp
; /* skip the class variable */
167 type_print (args
[i
++], "", stream
, 0);
170 fprintf_filtered (stream
, " ...");
173 else if (args
[i
]->code
!= TYPE_CODE_VOID
)
175 fprintf_filtered (stream
, ", ");
180 fprintf_filtered (stream
, ")");
183 /* If TYPE is a derived type, then print out derivation information.
184 Print only the actual base classes of this type, not the base classes
185 of the base classes. I.E. for the derivation hierarchy:
188 class B : public A {int b; };
189 class C : public B {int c; };
191 Print the type of class C as:
197 Not as the following (like gdb used to), which is not legal C++ syntax for
198 derived types and may be confused with the multiple inheritance form:
200 class C : public B : public A {
204 In general, gdb should try to print the types as closely as possible to
205 the form that they appear in the source code. */
208 cp_type_print_derivation_info (stream
, type
)
215 for (i
= 0; i
< TYPE_N_BASECLASSES (type
); i
++)
217 fputs_filtered (i
== 0 ? ": " : ", ", stream
);
218 fprintf_filtered (stream
, "%s%s ",
219 BASETYPE_VIA_PUBLIC (type
, i
) ? "public" : "private",
220 BASETYPE_VIA_VIRTUAL(type
, i
) ? " virtual" : "");
221 name
= type_name_no_tag (TYPE_BASECLASS (type
, i
));
222 fprintf_filtered (stream
, "%s", name
? name
: "(null)");
226 fputs_filtered (" ", stream
);
230 /* Print any asterisks or open-parentheses needed before the
231 variable name (to describe its type).
233 On outermost call, pass 0 for PASSED_A_PTR.
234 On outermost call, SHOW > 0 means should ignore
235 any typename for TYPE and show its details.
236 SHOW is always zero on recursive calls. */
239 c_type_print_varspec_prefix (type
, stream
, show
, passed_a_ptr
)
249 if (TYPE_NAME (type
) && show
<= 0)
254 switch (TYPE_CODE (type
))
257 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 1);
258 fprintf_filtered (stream
, "*");
261 case TYPE_CODE_MEMBER
:
263 fprintf_filtered (stream
, "(");
264 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
265 fprintf_filtered (stream
, " ");
266 name
= type_name_no_tag (TYPE_DOMAIN_TYPE (type
));
268 fputs_filtered (name
, stream
);
270 c_type_print_base (TYPE_DOMAIN_TYPE (type
), stream
, 0, passed_a_ptr
);
271 fprintf_filtered (stream
, "::");
274 case TYPE_CODE_METHOD
:
276 fprintf_unfiltered (stream
, "(");
277 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
280 fprintf_filtered (stream
, " ");
281 c_type_print_base (TYPE_DOMAIN_TYPE (type
), stream
, 0, passed_a_ptr
);
282 fprintf_filtered (stream
, "::");
287 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 1);
288 fprintf_filtered (stream
, "&");
292 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
294 fprintf_filtered (stream
, "(");
297 case TYPE_CODE_ARRAY
:
298 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
300 fprintf_filtered (stream
, "(");
303 case TYPE_CODE_UNDEF
:
304 case TYPE_CODE_STRUCT
:
305 case TYPE_CODE_UNION
:
310 case TYPE_CODE_ERROR
:
314 case TYPE_CODE_RANGE
:
315 case TYPE_CODE_STRING
:
316 case TYPE_CODE_BITSTRING
:
317 case TYPE_CODE_COMPLEX
:
318 case TYPE_CODE_LITERAL_COMPLEX
:
319 case TYPE_CODE_LITERAL_STRING
:
320 /* These types need no prefix. They are listed here so that
321 gcc -Wall will reveal any types that haven't been handled. */
327 c_type_print_args (type
, stream
)
334 fprintf_filtered (stream
, "(");
335 args
= TYPE_ARG_TYPES (type
);
340 fprintf_filtered (stream
, "...");
345 args
[i
] != NULL
&& args
[i
]->code
!= TYPE_CODE_VOID
;
348 c_print_type (args
[i
], "", stream
, -1, 0);
349 if (args
[i
+1] == NULL
)
351 fprintf_filtered (stream
, "...");
353 else if (args
[i
+1]->code
!= TYPE_CODE_VOID
)
355 fprintf_filtered (stream
, ",");
361 fprintf_filtered (stream
, ")");
364 /* Print any array sizes, function arguments or close parentheses
365 needed after the variable name (to describe its type).
366 Args work like c_type_print_varspec_prefix. */
369 c_type_print_varspec_suffix (type
, stream
, show
, passed_a_ptr
, demangled_args
)
379 if (TYPE_NAME (type
) && show
<= 0)
384 switch (TYPE_CODE (type
))
386 case TYPE_CODE_ARRAY
:
388 fprintf_filtered (stream
, ")");
390 fprintf_filtered (stream
, "[");
391 if (TYPE_LENGTH (type
) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type
)) > 0)
392 fprintf_filtered (stream
, "%d",
394 / TYPE_LENGTH (TYPE_TARGET_TYPE (type
))));
395 fprintf_filtered (stream
, "]");
397 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 0, 0);
400 case TYPE_CODE_MEMBER
:
402 fprintf_filtered (stream
, ")");
403 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 0, 0);
406 case TYPE_CODE_METHOD
:
408 fprintf_filtered (stream
, ")");
409 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 0, 0);
412 c_type_print_args (type
, stream
);
418 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 1, 0);
423 fprintf_filtered (stream
, ")");
425 fprintf_filtered (stream
, "()");
426 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0,
430 case TYPE_CODE_UNDEF
:
431 case TYPE_CODE_STRUCT
:
432 case TYPE_CODE_UNION
:
437 case TYPE_CODE_ERROR
:
441 case TYPE_CODE_RANGE
:
442 case TYPE_CODE_STRING
:
443 case TYPE_CODE_BITSTRING
:
444 case TYPE_CODE_COMPLEX
:
445 case TYPE_CODE_LITERAL_COMPLEX
:
446 case TYPE_CODE_LITERAL_STRING
:
447 /* These types do not need a suffix. They are listed so that
448 gcc -Wall will report types that may not have been considered. */
453 /* Print the name of the type (or the ultimate pointer target,
454 function value or array element), or the description of a
457 SHOW positive means print details about the type (e.g. enum values),
458 and print structure elements passing SHOW - 1 for show.
459 SHOW negative means just print the type name or struct tag if there is one.
460 If there is no name, print something sensible but concise like
462 SHOW zero means just print the type name or struct tag if there is one.
463 If there is no name, print something sensible but not as concise like
464 "struct {int x; int y;}".
466 LEVEL is the number of spaces to indent by.
467 We increase it for some recursive calls. */
470 c_type_print_base (type
, stream
, show
, level
)
478 register int lastval
;
480 char *demangled_name
;
481 enum {s_none
, s_public
, s_private
, s_protected
} section_type
;
487 fputs_filtered ("<type unknown>", stream
);
491 /* When SHOW is zero or less, and there is a valid type name, then always
492 just print the type name directly from the type. */
493 /* If we have "typedef struct foo {. . .} bar;" do we want to print it
494 as "struct foo" or as "bar"? Pick the latter, because C++ folk tend
495 to expect things like "class5 *foo" rather than "struct class5 *foo". */
498 && TYPE_NAME (type
) != NULL
)
500 fputs_filtered (TYPE_NAME (type
), stream
);
504 check_stub_type (type
);
506 switch (TYPE_CODE (type
))
508 case TYPE_CODE_ARRAY
:
510 case TYPE_CODE_MEMBER
:
513 case TYPE_CODE_METHOD
:
514 c_type_print_base (TYPE_TARGET_TYPE (type
), stream
, show
, level
);
517 case TYPE_CODE_STRUCT
:
518 if (HAVE_CPLUS_STRUCT (type
))
520 fprintf_filtered (stream
, "class ");
524 fprintf_filtered (stream
, "struct ");
528 case TYPE_CODE_UNION
:
529 fprintf_filtered (stream
, "union ");
532 if (TYPE_TAG_NAME (type
) != NULL
)
534 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
536 fputs_filtered (" ", stream
);
541 /* If we just printed a tag name, no need to print anything else. */
542 if (TYPE_TAG_NAME (type
) == NULL
)
543 fprintf_filtered (stream
, "{...}");
545 else if (show
> 0 || TYPE_TAG_NAME (type
) == NULL
)
547 cp_type_print_derivation_info (stream
, type
);
549 fprintf_filtered (stream
, "{\n");
550 if ((TYPE_NFIELDS (type
) == 0) && (TYPE_NFN_FIELDS (type
) == 0))
552 if (TYPE_FLAGS (type
) & TYPE_FLAG_STUB
)
553 fprintfi_filtered (level
+ 4, stream
, "<incomplete type>\n");
555 fprintfi_filtered (level
+ 4, stream
, "<no data fields>\n");
558 /* Start off with no specific section type, so we can print
559 one for the first field we find, and use that section type
560 thereafter until we find another type. */
562 section_type
= s_none
;
564 /* If there is a base class for this type,
565 do not print the field that it occupies. */
567 len
= TYPE_NFIELDS (type
);
568 for (i
= TYPE_N_BASECLASSES (type
); i
< len
; i
++)
571 /* Don't print out virtual function table. */
572 if ((TYPE_FIELD_NAME (type
, i
))[5] == CPLUS_MARKER
&&
573 !strncmp (TYPE_FIELD_NAME (type
, i
), "_vptr", 5))
576 /* If this is a C++ class we can print the various C++ section
579 if (HAVE_CPLUS_STRUCT (type
))
581 if (TYPE_FIELD_PROTECTED (type
, i
))
583 if (section_type
!= s_protected
)
585 section_type
= s_protected
;
586 fprintfi_filtered (level
+ 2, stream
,
590 else if (TYPE_FIELD_PRIVATE (type
, i
))
592 if (section_type
!= s_private
)
594 section_type
= s_private
;
595 fprintfi_filtered (level
+ 2, stream
, "private:\n");
600 if (section_type
!= s_public
)
602 section_type
= s_public
;
603 fprintfi_filtered (level
+ 2, stream
, "public:\n");
608 print_spaces_filtered (level
+ 4, stream
);
609 if (TYPE_FIELD_STATIC (type
, i
))
611 fprintf_filtered (stream
, "static ");
613 c_print_type (TYPE_FIELD_TYPE (type
, i
),
614 TYPE_FIELD_NAME (type
, i
),
615 stream
, show
- 1, level
+ 4);
616 if (!TYPE_FIELD_STATIC (type
, i
)
617 && TYPE_FIELD_PACKED (type
, i
))
619 /* It is a bitfield. This code does not attempt
620 to look at the bitpos and reconstruct filler,
621 unnamed fields. This would lead to misleading
622 results if the compiler does not put out fields
623 for such things (I don't know what it does). */
624 fprintf_filtered (stream
, " : %d",
625 TYPE_FIELD_BITSIZE (type
, i
));
627 fprintf_filtered (stream
, ";\n");
630 /* If there are both fields and methods, put a space between. */
631 len
= TYPE_NFN_FIELDS (type
);
632 if (len
&& section_type
!= s_none
)
633 fprintf_filtered (stream
, "\n");
635 /* C++: print out the methods */
637 for (i
= 0; i
< len
; i
++)
639 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
640 int j
, len2
= TYPE_FN_FIELDLIST_LENGTH (type
, i
);
641 char *method_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
642 char *name
= type_name_no_tag (type
);
643 int is_constructor
= name
&& STREQ(method_name
, name
);
644 for (j
= 0; j
< len2
; j
++)
646 char *physname
= TYPE_FN_FIELD_PHYSNAME (f
, j
);
647 int is_full_physname_constructor
=
648 ((physname
[0]=='_' && physname
[1]=='_' &&
649 (isdigit(physname
[2])
651 || physname
[2]=='t'))
652 || (strncmp(physname
, "__ct__", 6) == 0));
655 if (TYPE_FN_FIELD_PROTECTED (f
, j
))
657 if (section_type
!= s_protected
)
659 section_type
= s_protected
;
660 fprintfi_filtered (level
+ 2, stream
,
664 else if (TYPE_FN_FIELD_PRIVATE (f
, j
))
666 if (section_type
!= s_private
)
668 section_type
= s_private
;
669 fprintfi_filtered (level
+ 2, stream
, "private:\n");
674 if (section_type
!= s_public
)
676 section_type
= s_public
;
677 fprintfi_filtered (level
+ 2, stream
, "public:\n");
681 print_spaces_filtered (level
+ 4, stream
);
682 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
683 fprintf_filtered (stream
, "virtual ");
684 else if (TYPE_FN_FIELD_STATIC_P (f
, j
))
685 fprintf_filtered (stream
, "static ");
686 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)) == 0)
688 /* Keep GDB from crashing here. */
689 fprintf_unfiltered (stream
, "<undefined type> %s;\n",
690 TYPE_FN_FIELD_PHYSNAME (f
, j
));
693 else if (!is_constructor
&& !is_full_physname_constructor
)
695 type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)),
697 fputs_filtered (" ", stream
);
699 if (TYPE_FN_FIELD_STUB (f
, j
))
701 /* Build something we can demangle. */
702 mangled_name
= gdb_mangle_name (type
, i
, j
);
704 cplus_demangle (mangled_name
,
705 DMGL_ANSI
| DMGL_PARAMS
);
706 if (demangled_name
== NULL
)
707 fprintf_filtered (stream
, "<badly mangled name %s>",
711 char *demangled_no_class
=
712 strchr (demangled_name
, ':');
714 if (demangled_no_class
== NULL
)
715 demangled_no_class
= demangled_name
;
718 if (*++demangled_no_class
== ':')
719 ++demangled_no_class
;
721 fputs_filtered (demangled_no_class
, stream
);
722 free (demangled_name
);
726 else if (TYPE_FN_FIELD_PHYSNAME (f
, j
)[0] == '_'
727 && TYPE_FN_FIELD_PHYSNAME (f
, j
)[1] == CPLUS_MARKER
)
728 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f
, j
) + 1,
729 "~", method_name
, 0, stream
);
731 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f
, j
), "",
733 TYPE_FN_FIELD_STATIC_P (f
, j
),
736 fprintf_filtered (stream
, ";\n");
740 fprintfi_filtered (level
, stream
, "}");
745 fprintf_filtered (stream
, "enum ");
746 if (TYPE_TAG_NAME (type
) != NULL
)
748 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
750 fputs_filtered (" ", stream
);
756 /* If we just printed a tag name, no need to print anything else. */
757 if (TYPE_TAG_NAME (type
) == NULL
)
758 fprintf_filtered (stream
, "{...}");
760 else if (show
> 0 || TYPE_TAG_NAME (type
) == NULL
)
762 fprintf_filtered (stream
, "{");
763 len
= TYPE_NFIELDS (type
);
765 for (i
= 0; i
< len
; i
++)
768 if (i
) fprintf_filtered (stream
, ", ");
770 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
771 if (lastval
!= TYPE_FIELD_BITPOS (type
, i
))
773 fprintf_filtered (stream
, " = %d", TYPE_FIELD_BITPOS (type
, i
));
774 lastval
= TYPE_FIELD_BITPOS (type
, i
);
778 fprintf_filtered (stream
, "}");
783 fprintf_filtered (stream
, "void");
786 case TYPE_CODE_UNDEF
:
787 fprintf_filtered (stream
, "struct <unknown>");
790 case TYPE_CODE_ERROR
:
791 fprintf_filtered (stream
, "<unknown type>");
794 case TYPE_CODE_RANGE
:
795 /* This should not occur */
796 fprintf_filtered (stream
, "<range type>");
800 /* Handle types not explicitly handled by the other cases,
801 such as fundamental types. For these, just print whatever
802 the type name is, as recorded in the type itself. If there
803 is no type name, then complain. */
804 if (TYPE_NAME (type
) != NULL
)
806 fputs_filtered (TYPE_NAME (type
), stream
);
810 /* At least for dump_symtab, it is important that this not be
812 fprintf_filtered (stream
, "<invalid type code %d>",