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"
40 c_type_print_args
PARAMS ((struct type
*, GDB_FILE
*));
43 c_type_print_varspec_suffix
PARAMS ((struct type
*, GDB_FILE
*, int, int, int));
46 cp_type_print_derivation_info
PARAMS ((GDB_FILE
*, struct type
*));
49 c_type_print_varspec_prefix
PARAMS ((struct type
*, GDB_FILE
*, int, int));
52 c_type_print_base
PARAMS ((struct type
*, GDB_FILE
*, int, int));
55 /* Print a description of a type in the format of a
56 typedef for the current language.
57 NEW is the new name for a type TYPE. */
60 c_typedef_print (type
, new, stream
)
65 switch (current_language
->la_language
)
70 fprintf_filtered(stream
, "typedef ");
71 type_print(type
,"",stream
,0);
72 if(TYPE_NAME ((SYMBOL_TYPE (new))) == 0
73 || !STREQ (TYPE_NAME ((SYMBOL_TYPE (new))), SYMBOL_NAME (new)))
74 fprintf_filtered(stream
, " %s", SYMBOL_SOURCE_NAME(new));
79 fprintf_filtered(stream
, "TYPE ");
80 if(!TYPE_NAME(SYMBOL_TYPE(new)) ||
81 !STREQ (TYPE_NAME(SYMBOL_TYPE(new)), SYMBOL_NAME(new)))
82 fprintf_filtered(stream
, "%s = ", SYMBOL_SOURCE_NAME(new));
84 fprintf_filtered(stream
, "<builtin> = ");
85 type_print(type
,"",stream
,0);
90 fprintf_filtered(stream
, "SYNMODE ");
91 if(!TYPE_NAME(SYMBOL_TYPE(new)) ||
92 !STREQ (TYPE_NAME(SYMBOL_TYPE(new)), SYMBOL_NAME(new)))
93 fprintf_filtered(stream
, "%s = ", SYMBOL_SOURCE_NAME(new));
95 fprintf_filtered(stream
, "<builtin> = ");
96 type_print(type
,"",stream
,0);
100 error("Language not supported.");
102 fprintf_filtered(stream
, ";\n");
106 /* LEVEL is the depth to indent lines by. */
109 c_print_type (type
, varstring
, stream
, show
, level
)
116 register enum type_code code
;
119 c_type_print_base (type
, stream
, show
, level
);
120 code
= TYPE_CODE (type
);
121 if ((varstring
!= NULL
&& *varstring
!= '\0')
123 /* Need a space if going to print stars or brackets;
124 but not if we will print just a type name. */
125 ((show
> 0 || TYPE_NAME (type
) == 0)
127 (code
== TYPE_CODE_PTR
|| code
== TYPE_CODE_FUNC
128 || code
== TYPE_CODE_METHOD
129 || code
== TYPE_CODE_ARRAY
130 || code
== TYPE_CODE_MEMBER
131 || code
== TYPE_CODE_REF
)))
132 fputs_filtered (" ", stream
);
133 c_type_print_varspec_prefix (type
, stream
, show
, 0);
135 fputs_filtered (varstring
, stream
);
137 /* For demangled function names, we have the arglist as part of the name,
138 so don't print an additional pair of ()'s */
140 demangled_args
= strchr(varstring
, '(') != NULL
;
141 c_type_print_varspec_suffix (type
, stream
, show
, 0, demangled_args
);
145 /* Print the C++ method arguments ARGS to the file STREAM. */
148 cp_type_print_method_args (args
, prefix
, varstring
, staticp
, stream
)
157 fprintf_symbol_filtered (stream
, prefix
, language_cplus
, DMGL_ANSI
);
158 fprintf_symbol_filtered (stream
, varstring
, language_cplus
, DMGL_ANSI
);
159 fputs_filtered (" (", stream
);
160 if (args
&& args
[!staticp
] && args
[!staticp
]->code
!= TYPE_CODE_VOID
)
162 i
= !staticp
; /* skip the class variable */
165 type_print (args
[i
++], "", stream
, 0);
168 fprintf_filtered (stream
, " ...");
171 else if (args
[i
]->code
!= TYPE_CODE_VOID
)
173 fprintf_filtered (stream
, ", ");
178 fprintf_filtered (stream
, ")");
181 /* If TYPE is a derived type, then print out derivation information.
182 Print only the actual base classes of this type, not the base classes
183 of the base classes. I.E. for the derivation hierarchy:
186 class B : public A {int b; };
187 class C : public B {int c; };
189 Print the type of class C as:
195 Not as the following (like gdb used to), which is not legal C++ syntax for
196 derived types and may be confused with the multiple inheritance form:
198 class C : public B : public A {
202 In general, gdb should try to print the types as closely as possible to
203 the form that they appear in the source code. */
206 cp_type_print_derivation_info (stream
, type
)
213 for (i
= 0; i
< TYPE_N_BASECLASSES (type
); i
++)
215 fputs_filtered (i
== 0 ? ": " : ", ", stream
);
216 fprintf_filtered (stream
, "%s%s ",
217 BASETYPE_VIA_PUBLIC (type
, i
) ? "public" : "private",
218 BASETYPE_VIA_VIRTUAL(type
, i
) ? " virtual" : "");
219 name
= type_name_no_tag (TYPE_BASECLASS (type
, i
));
220 fprintf_filtered (stream
, "%s", name
? name
: "(null)");
224 fputs_filtered (" ", stream
);
228 /* Print any asterisks or open-parentheses needed before the
229 variable name (to describe its type).
231 On outermost call, pass 0 for PASSED_A_PTR.
232 On outermost call, SHOW > 0 means should ignore
233 any typename for TYPE and show its details.
234 SHOW is always zero on recursive calls. */
237 c_type_print_varspec_prefix (type
, stream
, show
, passed_a_ptr
)
247 if (TYPE_NAME (type
) && show
<= 0)
252 switch (TYPE_CODE (type
))
255 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 1);
256 fprintf_filtered (stream
, "*");
259 case TYPE_CODE_MEMBER
:
261 fprintf_filtered (stream
, "(");
262 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
263 fprintf_filtered (stream
, " ");
264 name
= type_name_no_tag (TYPE_DOMAIN_TYPE (type
));
266 fputs_filtered (name
, stream
);
268 c_type_print_base (TYPE_DOMAIN_TYPE (type
), stream
, 0, passed_a_ptr
);
269 fprintf_filtered (stream
, "::");
272 case TYPE_CODE_METHOD
:
274 fprintf_unfiltered (stream
, "(");
275 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
278 fprintf_filtered (stream
, " ");
279 c_type_print_base (TYPE_DOMAIN_TYPE (type
), stream
, 0, passed_a_ptr
);
280 fprintf_filtered (stream
, "::");
285 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 1);
286 fprintf_filtered (stream
, "&");
290 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
292 fprintf_filtered (stream
, "(");
295 case TYPE_CODE_ARRAY
:
296 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
298 fprintf_filtered (stream
, "(");
301 case TYPE_CODE_UNDEF
:
302 case TYPE_CODE_STRUCT
:
303 case TYPE_CODE_UNION
:
308 case TYPE_CODE_ERROR
:
312 case TYPE_CODE_RANGE
:
313 case TYPE_CODE_STRING
:
314 case TYPE_CODE_BITSTRING
:
315 /* These types need no prefix. They are listed here so that
316 gcc -Wall will reveal any types that haven't been handled. */
322 c_type_print_args (type
, stream
)
329 fprintf_filtered (stream
, "(");
330 args
= TYPE_ARG_TYPES (type
);
335 fprintf_filtered (stream
, "...");
340 args
[i
] != NULL
&& args
[i
]->code
!= TYPE_CODE_VOID
;
343 c_print_type (args
[i
], "", stream
, -1, 0);
344 if (args
[i
+1] == NULL
)
346 fprintf_filtered (stream
, "...");
348 else if (args
[i
+1]->code
!= TYPE_CODE_VOID
)
350 fprintf_filtered (stream
, ",");
356 fprintf_filtered (stream
, ")");
359 /* Print any array sizes, function arguments or close parentheses
360 needed after the variable name (to describe its type).
361 Args work like c_type_print_varspec_prefix. */
364 c_type_print_varspec_suffix (type
, stream
, show
, passed_a_ptr
, demangled_args
)
374 if (TYPE_NAME (type
) && show
<= 0)
379 switch (TYPE_CODE (type
))
381 case TYPE_CODE_ARRAY
:
383 fprintf_filtered (stream
, ")");
385 fprintf_filtered (stream
, "[");
386 if (TYPE_LENGTH (type
) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type
)) > 0)
387 fprintf_filtered (stream
, "%d",
389 / TYPE_LENGTH (TYPE_TARGET_TYPE (type
))));
390 fprintf_filtered (stream
, "]");
392 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 0, 0);
395 case TYPE_CODE_MEMBER
:
397 fprintf_filtered (stream
, ")");
398 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 0, 0);
401 case TYPE_CODE_METHOD
:
403 fprintf_filtered (stream
, ")");
404 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 0, 0);
407 c_type_print_args (type
, stream
);
413 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 1, 0);
418 fprintf_filtered (stream
, ")");
420 fprintf_filtered (stream
, "()");
421 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0,
425 case TYPE_CODE_UNDEF
:
426 case TYPE_CODE_STRUCT
:
427 case TYPE_CODE_UNION
:
432 case TYPE_CODE_ERROR
:
436 case TYPE_CODE_RANGE
:
437 case TYPE_CODE_STRING
:
438 case TYPE_CODE_BITSTRING
:
439 /* These types do not need a suffix. They are listed so that
440 gcc -Wall will report types that may not have been considered. */
445 /* Print the name of the type (or the ultimate pointer target,
446 function value or array element), or the description of a
449 SHOW positive means print details about the type (e.g. enum values),
450 and print structure elements passing SHOW - 1 for show.
451 SHOW negative means just print the type name or struct tag if there is one.
452 If there is no name, print something sensible but concise like
454 SHOW zero means just print the type name or struct tag if there is one.
455 If there is no name, print something sensible but not as concise like
456 "struct {int x; int y;}".
458 LEVEL is the number of spaces to indent by.
459 We increase it for some recursive calls. */
462 c_type_print_base (type
, stream
, show
, level
)
470 register int lastval
;
472 char *demangled_name
;
473 enum {s_none
, s_public
, s_private
, s_protected
} section_type
;
479 fputs_filtered ("<type unknown>", stream
);
483 /* When SHOW is zero or less, and there is a valid type name, then always
484 just print the type name directly from the type. */
485 /* If we have "typedef struct foo {. . .} bar;" do we want to print it
486 as "struct foo" or as "bar"? Pick the latter, because C++ folk tend
487 to expect things like "class5 *foo" rather than "struct class5 *foo". */
490 && TYPE_NAME (type
) != NULL
)
492 fputs_filtered (TYPE_NAME (type
), stream
);
496 check_stub_type (type
);
498 switch (TYPE_CODE (type
))
500 case TYPE_CODE_ARRAY
:
502 case TYPE_CODE_MEMBER
:
505 case TYPE_CODE_METHOD
:
506 c_type_print_base (TYPE_TARGET_TYPE (type
), stream
, show
, level
);
509 case TYPE_CODE_STRUCT
:
510 if (HAVE_CPLUS_STRUCT (type
))
512 fprintf_filtered (stream
, "class ");
516 fprintf_filtered (stream
, "struct ");
520 case TYPE_CODE_UNION
:
521 fprintf_filtered (stream
, "union ");
524 if (TYPE_TAG_NAME (type
) != NULL
)
526 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
528 fputs_filtered (" ", stream
);
533 /* If we just printed a tag name, no need to print anything else. */
534 if (TYPE_TAG_NAME (type
) == NULL
)
535 fprintf_filtered (stream
, "{...}");
537 else if (show
> 0 || TYPE_TAG_NAME (type
) == NULL
)
539 cp_type_print_derivation_info (stream
, type
);
541 fprintf_filtered (stream
, "{\n");
542 if ((TYPE_NFIELDS (type
) == 0) && (TYPE_NFN_FIELDS (type
) == 0))
544 if (TYPE_FLAGS (type
) & TYPE_FLAG_STUB
)
545 fprintfi_filtered (level
+ 4, stream
, "<incomplete type>\n");
547 fprintfi_filtered (level
+ 4, stream
, "<no data fields>\n");
550 /* Start off with no specific section type, so we can print
551 one for the first field we find, and use that section type
552 thereafter until we find another type. */
554 section_type
= s_none
;
556 /* If there is a base class for this type,
557 do not print the field that it occupies. */
559 len
= TYPE_NFIELDS (type
);
560 for (i
= TYPE_N_BASECLASSES (type
); i
< len
; i
++)
563 /* Don't print out virtual function table. */
564 if ((TYPE_FIELD_NAME (type
, i
))[5] == CPLUS_MARKER
&&
565 !strncmp (TYPE_FIELD_NAME (type
, i
), "_vptr", 5))
568 /* If this is a C++ class we can print the various C++ section
571 if (HAVE_CPLUS_STRUCT (type
))
573 if (TYPE_FIELD_PROTECTED (type
, i
))
575 if (section_type
!= s_protected
)
577 section_type
= s_protected
;
578 fprintfi_filtered (level
+ 2, stream
,
582 else if (TYPE_FIELD_PRIVATE (type
, i
))
584 if (section_type
!= s_private
)
586 section_type
= s_private
;
587 fprintfi_filtered (level
+ 2, stream
, "private:\n");
592 if (section_type
!= s_public
)
594 section_type
= s_public
;
595 fprintfi_filtered (level
+ 2, stream
, "public:\n");
600 print_spaces_filtered (level
+ 4, stream
);
601 if (TYPE_FIELD_STATIC (type
, i
))
603 fprintf_filtered (stream
, "static ");
605 c_print_type (TYPE_FIELD_TYPE (type
, i
),
606 TYPE_FIELD_NAME (type
, i
),
607 stream
, show
- 1, level
+ 4);
608 if (!TYPE_FIELD_STATIC (type
, i
)
609 && TYPE_FIELD_PACKED (type
, i
))
611 /* It is a bitfield. This code does not attempt
612 to look at the bitpos and reconstruct filler,
613 unnamed fields. This would lead to misleading
614 results if the compiler does not put out fields
615 for such things (I don't know what it does). */
616 fprintf_filtered (stream
, " : %d",
617 TYPE_FIELD_BITSIZE (type
, i
));
619 fprintf_filtered (stream
, ";\n");
622 /* If there are both fields and methods, put a space between. */
623 len
= TYPE_NFN_FIELDS (type
);
624 if (len
&& section_type
!= s_none
)
625 fprintf_filtered (stream
, "\n");
627 /* C++: print out the methods */
629 for (i
= 0; i
< len
; i
++)
631 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
632 int j
, len2
= TYPE_FN_FIELDLIST_LENGTH (type
, i
);
633 char *method_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
634 char *name
= type_name_no_tag (type
);
635 int is_constructor
= name
&& STREQ(method_name
, name
);
636 for (j
= 0; j
< len2
; j
++)
639 if (TYPE_FN_FIELD_PROTECTED (f
, j
))
641 if (section_type
!= s_protected
)
643 section_type
= s_protected
;
644 fprintfi_filtered (level
+ 2, stream
,
648 else if (TYPE_FN_FIELD_PRIVATE (f
, j
))
650 if (section_type
!= s_private
)
652 section_type
= s_private
;
653 fprintfi_filtered (level
+ 2, stream
, "private:\n");
658 if (section_type
!= s_public
)
660 section_type
= s_public
;
661 fprintfi_filtered (level
+ 2, stream
, "public:\n");
665 print_spaces_filtered (level
+ 4, stream
);
666 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
667 fprintf_filtered (stream
, "virtual ");
668 else if (TYPE_FN_FIELD_STATIC_P (f
, j
))
669 fprintf_filtered (stream
, "static ");
670 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)) == 0)
672 /* Keep GDB from crashing here. */
673 fprintf_unfiltered (stream
, "<undefined type> %s;\n",
674 TYPE_FN_FIELD_PHYSNAME (f
, j
));
677 else if (!is_constructor
)
679 type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)),
681 fputs_filtered (" ", stream
);
683 if (TYPE_FN_FIELD_STUB (f
, j
))
685 /* Build something we can demangle. */
686 mangled_name
= gdb_mangle_name (type
, i
, j
);
688 cplus_demangle (mangled_name
,
689 DMGL_ANSI
| DMGL_PARAMS
);
690 if (demangled_name
== NULL
)
691 fprintf_filtered (stream
, "<badly mangled name %s>",
695 char *demangled_no_class
=
696 strchr (demangled_name
, ':');
698 if (demangled_no_class
== NULL
)
699 demangled_no_class
= demangled_name
;
702 if (*++demangled_no_class
== ':')
703 ++demangled_no_class
;
705 fputs_filtered (demangled_no_class
, stream
);
706 free (demangled_name
);
710 else if (TYPE_FN_FIELD_PHYSNAME (f
, j
)[0] == '_'
711 && TYPE_FN_FIELD_PHYSNAME (f
, j
)[1] == CPLUS_MARKER
)
712 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f
, j
) + 1,
713 "~", method_name
, 0, stream
);
715 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f
, j
), "",
717 TYPE_FN_FIELD_STATIC_P (f
, j
),
720 fprintf_filtered (stream
, ";\n");
724 fprintfi_filtered (level
, stream
, "}");
729 fprintf_filtered (stream
, "enum ");
730 if (TYPE_TAG_NAME (type
) != NULL
)
732 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
734 fputs_filtered (" ", stream
);
740 /* If we just printed a tag name, no need to print anything else. */
741 if (TYPE_TAG_NAME (type
) == NULL
)
742 fprintf_filtered (stream
, "{...}");
744 else if (show
> 0 || TYPE_TAG_NAME (type
) == NULL
)
746 fprintf_filtered (stream
, "{");
747 len
= TYPE_NFIELDS (type
);
749 for (i
= 0; i
< len
; i
++)
752 if (i
) fprintf_filtered (stream
, ", ");
754 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
755 if (lastval
!= TYPE_FIELD_BITPOS (type
, i
))
757 fprintf_filtered (stream
, " = %d", TYPE_FIELD_BITPOS (type
, i
));
758 lastval
= TYPE_FIELD_BITPOS (type
, i
);
762 fprintf_filtered (stream
, "}");
767 fprintf_filtered (stream
, "void");
770 case TYPE_CODE_UNDEF
:
771 fprintf_filtered (stream
, "struct <unknown>");
774 case TYPE_CODE_ERROR
:
775 fprintf_filtered (stream
, "<unknown type>");
778 case TYPE_CODE_RANGE
:
779 /* This should not occur */
780 fprintf_filtered (stream
, "<range type>");
784 /* Handle types not explicitly handled by the other cases,
785 such as fundamental types. For these, just print whatever
786 the type name is, as recorded in the type itself. If there
787 is no type name, then complain. */
788 if (TYPE_NAME (type
) != NULL
)
790 fputs_filtered (TYPE_NAME (type
), stream
);
794 /* At least for dump_symtab, it is important that this not be
796 fprintf_filtered (stream
, "<invalid type code %d>",