1 /* Support for printing C and C++ types for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1991, 1993, 1994, 1995, 1996
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 #include "bfd.h" /* Binary File Description */
26 #include "expression.h"
35 #include "typeprint.h"
37 #include "gdb_string.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 /* Print a description of a type in the format of a
55 typedef for the current language.
56 NEW is the new name for a type TYPE. */
59 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
;
120 CHECK_TYPEDEF (type
);
122 c_type_print_base (type
, stream
, show
, level
);
123 code
= TYPE_CODE (type
);
124 if ((varstring
!= NULL
&& *varstring
!= '\0')
126 /* Need a space if going to print stars or brackets;
127 but not if we will print just a type name. */
128 ((show
> 0 || TYPE_NAME (type
) == 0)
130 (code
== TYPE_CODE_PTR
|| code
== TYPE_CODE_FUNC
131 || code
== TYPE_CODE_METHOD
132 || code
== TYPE_CODE_ARRAY
133 || code
== TYPE_CODE_MEMBER
134 || code
== TYPE_CODE_REF
)))
135 fputs_filtered (" ", stream
);
136 c_type_print_varspec_prefix (type
, stream
, show
, 0);
138 if (varstring
!= NULL
)
140 fputs_filtered (varstring
, stream
);
142 /* For demangled function names, we have the arglist as part of the name,
143 so don't print an additional pair of ()'s */
145 demangled_args
= strchr(varstring
, '(') != NULL
;
146 c_type_print_varspec_suffix (type
, stream
, show
, 0, demangled_args
);
150 /* If TYPE is a derived type, then print out derivation information.
151 Print only the actual base classes of this type, not the base classes
152 of the base classes. I.E. for the derivation hierarchy:
155 class B : public A {int b; };
156 class C : public B {int c; };
158 Print the type of class C as:
164 Not as the following (like gdb used to), which is not legal C++ syntax for
165 derived types and may be confused with the multiple inheritance form:
167 class C : public B : public A {
171 In general, gdb should try to print the types as closely as possible to
172 the form that they appear in the source code. */
175 cp_type_print_derivation_info (stream
, type
)
182 for (i
= 0; i
< TYPE_N_BASECLASSES (type
); i
++)
184 fputs_filtered (i
== 0 ? ": " : ", ", stream
);
185 fprintf_filtered (stream
, "%s%s ",
186 BASETYPE_VIA_PUBLIC (type
, i
) ? "public" : "private",
187 BASETYPE_VIA_VIRTUAL(type
, i
) ? " virtual" : "");
188 name
= type_name_no_tag (TYPE_BASECLASS (type
, i
));
189 fprintf_filtered (stream
, "%s", name
? name
: "(null)");
193 fputs_filtered (" ", stream
);
197 /* Print any asterisks or open-parentheses needed before the
198 variable name (to describe its type).
200 On outermost call, pass 0 for PASSED_A_PTR.
201 On outermost call, SHOW > 0 means should ignore
202 any typename for TYPE and show its details.
203 SHOW is always zero on recursive calls. */
206 c_type_print_varspec_prefix (type
, stream
, show
, passed_a_ptr
)
216 if (TYPE_NAME (type
) && show
<= 0)
221 switch (TYPE_CODE (type
))
224 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 1);
225 fprintf_filtered (stream
, "*");
228 case TYPE_CODE_MEMBER
:
230 fprintf_filtered (stream
, "(");
231 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
232 fprintf_filtered (stream
, " ");
233 name
= type_name_no_tag (TYPE_DOMAIN_TYPE (type
));
235 fputs_filtered (name
, stream
);
237 c_type_print_base (TYPE_DOMAIN_TYPE (type
), stream
, 0, passed_a_ptr
);
238 fprintf_filtered (stream
, "::");
241 case TYPE_CODE_METHOD
:
243 fprintf_filtered (stream
, "(");
244 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
247 fprintf_filtered (stream
, " ");
248 c_type_print_base (TYPE_DOMAIN_TYPE (type
), stream
, 0, passed_a_ptr
);
249 fprintf_filtered (stream
, "::");
254 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 1);
255 fprintf_filtered (stream
, "&");
259 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
261 fprintf_filtered (stream
, "(");
264 case TYPE_CODE_ARRAY
:
265 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
267 fprintf_filtered (stream
, "(");
270 case TYPE_CODE_UNDEF
:
271 case TYPE_CODE_STRUCT
:
272 case TYPE_CODE_UNION
:
277 case TYPE_CODE_ERROR
:
281 case TYPE_CODE_RANGE
:
282 case TYPE_CODE_STRING
:
283 case TYPE_CODE_BITSTRING
:
284 case TYPE_CODE_COMPLEX
:
285 case TYPE_CODE_TYPEDEF
:
286 /* These types need no prefix. They are listed here so that
287 gcc -Wall will reveal any types that haven't been handled. */
293 c_type_print_args (type
, stream
)
300 fprintf_filtered (stream
, "(");
301 args
= TYPE_ARG_TYPES (type
);
306 fprintf_filtered (stream
, "...");
311 args
[i
] != NULL
&& args
[i
]->code
!= TYPE_CODE_VOID
;
314 c_print_type (args
[i
], "", stream
, -1, 0);
315 if (args
[i
+1] == NULL
)
317 fprintf_filtered (stream
, "...");
319 else if (args
[i
+1]->code
!= TYPE_CODE_VOID
)
321 fprintf_filtered (stream
, ",");
327 fprintf_filtered (stream
, ")");
330 /* Print any array sizes, function arguments or close parentheses
331 needed after the variable name (to describe its type).
332 Args work like c_type_print_varspec_prefix. */
335 c_type_print_varspec_suffix (type
, stream
, show
, passed_a_ptr
, demangled_args
)
345 if (TYPE_NAME (type
) && show
<= 0)
350 switch (TYPE_CODE (type
))
352 case TYPE_CODE_ARRAY
:
354 fprintf_filtered (stream
, ")");
356 fprintf_filtered (stream
, "[");
357 if (TYPE_LENGTH (type
) >= 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type
)) > 0
358 && TYPE_ARRAY_UPPER_BOUND_TYPE(type
) != BOUND_CANNOT_BE_DETERMINED
)
359 fprintf_filtered (stream
, "%d",
361 / TYPE_LENGTH (TYPE_TARGET_TYPE (type
))));
362 fprintf_filtered (stream
, "]");
364 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 0, 0);
367 case TYPE_CODE_MEMBER
:
369 fprintf_filtered (stream
, ")");
370 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 0, 0);
373 case TYPE_CODE_METHOD
:
375 fprintf_filtered (stream
, ")");
376 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 0, 0);
379 c_type_print_args (type
, stream
);
385 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 1, 0);
390 fprintf_filtered (stream
, ")");
392 { int i
, len
= TYPE_NFIELDS (type
);
393 fprintf_filtered (stream
, "(");
394 for (i
= 0; i
< len
; i
++)
398 fputs_filtered (", ", stream
);
401 c_print_type (TYPE_FIELD_TYPE (type
, i
), "", stream
, -1, 0);
403 fprintf_filtered (stream
, ")");
405 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0,
409 case TYPE_CODE_UNDEF
:
410 case TYPE_CODE_STRUCT
:
411 case TYPE_CODE_UNION
:
416 case TYPE_CODE_ERROR
:
420 case TYPE_CODE_RANGE
:
421 case TYPE_CODE_STRING
:
422 case TYPE_CODE_BITSTRING
:
423 case TYPE_CODE_COMPLEX
:
424 case TYPE_CODE_TYPEDEF
:
425 /* These types do not need a suffix. They are listed so that
426 gcc -Wall will report types that may not have been considered. */
431 /* Print the name of the type (or the ultimate pointer target,
432 function value or array element), or the description of a
435 SHOW positive means print details about the type (e.g. enum values),
436 and print structure elements passing SHOW - 1 for show.
437 SHOW negative means just print the type name or struct tag if there is one.
438 If there is no name, print something sensible but concise like
440 SHOW zero means just print the type name or struct tag if there is one.
441 If there is no name, print something sensible but not as concise like
442 "struct {int x; int y;}".
444 LEVEL is the number of spaces to indent by.
445 We increase it for some recursive calls. */
448 c_type_print_base (type
, stream
, show
, level
)
456 register int lastval
;
458 char *demangled_name
;
459 enum {s_none
, s_public
, s_private
, s_protected
} section_type
;
465 fputs_filtered ("<type unknown>", stream
);
469 /* When SHOW is zero or less, and there is a valid type name, then always
470 just print the type name directly from the type. */
471 /* If we have "typedef struct foo {. . .} bar;" do we want to print it
472 as "struct foo" or as "bar"? Pick the latter, because C++ folk tend
473 to expect things like "class5 *foo" rather than "struct class5 *foo". */
476 && TYPE_NAME (type
) != NULL
)
478 fputs_filtered (TYPE_NAME (type
), stream
);
482 CHECK_TYPEDEF (type
);
484 switch (TYPE_CODE (type
))
486 case TYPE_CODE_TYPEDEF
:
487 case TYPE_CODE_ARRAY
:
489 case TYPE_CODE_MEMBER
:
492 case TYPE_CODE_METHOD
:
493 c_type_print_base (TYPE_TARGET_TYPE (type
), stream
, show
, level
);
496 case TYPE_CODE_STRUCT
:
497 if (HAVE_CPLUS_STRUCT (type
))
499 fprintf_filtered (stream
, "class ");
503 fprintf_filtered (stream
, "struct ");
507 case TYPE_CODE_UNION
:
508 fprintf_filtered (stream
, "union ");
511 if (TYPE_TAG_NAME (type
) != NULL
)
513 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
515 fputs_filtered (" ", stream
);
520 /* If we just printed a tag name, no need to print anything else. */
521 if (TYPE_TAG_NAME (type
) == NULL
)
522 fprintf_filtered (stream
, "{...}");
524 else if (show
> 0 || TYPE_TAG_NAME (type
) == NULL
)
526 cp_type_print_derivation_info (stream
, type
);
528 fprintf_filtered (stream
, "{\n");
529 if ((TYPE_NFIELDS (type
) == 0) && (TYPE_NFN_FIELDS (type
) == 0))
531 if (TYPE_FLAGS (type
) & TYPE_FLAG_STUB
)
532 fprintfi_filtered (level
+ 4, stream
, "<incomplete type>\n");
534 fprintfi_filtered (level
+ 4, stream
, "<no data fields>\n");
537 /* Start off with no specific section type, so we can print
538 one for the first field we find, and use that section type
539 thereafter until we find another type. */
541 section_type
= s_none
;
543 /* If there is a base class for this type,
544 do not print the field that it occupies. */
546 len
= TYPE_NFIELDS (type
);
547 for (i
= TYPE_N_BASECLASSES (type
); i
< len
; i
++)
550 /* Don't print out virtual function table. */
551 if (STREQN (TYPE_FIELD_NAME (type
, i
), "_vptr", 5)
552 && is_cplus_marker ((TYPE_FIELD_NAME (type
, i
))[5]))
555 /* If this is a C++ class we can print the various C++ section
558 if (HAVE_CPLUS_STRUCT (type
))
560 if (TYPE_FIELD_PROTECTED (type
, i
))
562 if (section_type
!= s_protected
)
564 section_type
= s_protected
;
565 fprintfi_filtered (level
+ 2, stream
,
569 else if (TYPE_FIELD_PRIVATE (type
, i
))
571 if (section_type
!= s_private
)
573 section_type
= s_private
;
574 fprintfi_filtered (level
+ 2, stream
, "private:\n");
579 if (section_type
!= s_public
)
581 section_type
= s_public
;
582 fprintfi_filtered (level
+ 2, stream
, "public:\n");
587 print_spaces_filtered (level
+ 4, stream
);
588 if (TYPE_FIELD_STATIC (type
, i
))
590 fprintf_filtered (stream
, "static ");
592 c_print_type (TYPE_FIELD_TYPE (type
, i
),
593 TYPE_FIELD_NAME (type
, i
),
594 stream
, show
- 1, level
+ 4);
595 if (!TYPE_FIELD_STATIC (type
, i
)
596 && TYPE_FIELD_PACKED (type
, i
))
598 /* It is a bitfield. This code does not attempt
599 to look at the bitpos and reconstruct filler,
600 unnamed fields. This would lead to misleading
601 results if the compiler does not put out fields
602 for such things (I don't know what it does). */
603 fprintf_filtered (stream
, " : %d",
604 TYPE_FIELD_BITSIZE (type
, i
));
606 fprintf_filtered (stream
, ";\n");
609 /* If there are both fields and methods, put a space between. */
610 len
= TYPE_NFN_FIELDS (type
);
611 if (len
&& section_type
!= s_none
)
612 fprintf_filtered (stream
, "\n");
614 /* C++: print out the methods */
616 for (i
= 0; i
< len
; i
++)
618 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
619 int j
, len2
= TYPE_FN_FIELDLIST_LENGTH (type
, i
);
620 char *method_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
621 char *name
= type_name_no_tag (type
);
622 int is_constructor
= name
&& STREQ(method_name
, name
);
623 for (j
= 0; j
< len2
; j
++)
625 char *physname
= TYPE_FN_FIELD_PHYSNAME (f
, j
);
626 int is_full_physname_constructor
=
627 ((physname
[0] == '_' && physname
[1] == '_'
628 && strchr ("0123456789Qt", physname
[2]))
629 || STREQN (physname
, "__ct__", 6)
630 || DESTRUCTOR_PREFIX_P (physname
)
631 || STREQN (physname
, "__dt__", 6));
634 if (TYPE_FN_FIELD_PROTECTED (f
, j
))
636 if (section_type
!= s_protected
)
638 section_type
= s_protected
;
639 fprintfi_filtered (level
+ 2, stream
,
643 else if (TYPE_FN_FIELD_PRIVATE (f
, j
))
645 if (section_type
!= s_private
)
647 section_type
= s_private
;
648 fprintfi_filtered (level
+ 2, stream
, "private:\n");
653 if (section_type
!= s_public
)
655 section_type
= s_public
;
656 fprintfi_filtered (level
+ 2, stream
, "public:\n");
660 print_spaces_filtered (level
+ 4, stream
);
661 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
662 fprintf_filtered (stream
, "virtual ");
663 else if (TYPE_FN_FIELD_STATIC_P (f
, j
))
664 fprintf_filtered (stream
, "static ");
665 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)) == 0)
667 /* Keep GDB from crashing here. */
668 fprintf_filtered (stream
, "<undefined type> %s;\n",
669 TYPE_FN_FIELD_PHYSNAME (f
, j
));
672 else if (!is_constructor
&& !is_full_physname_constructor
)
674 type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)),
676 fputs_filtered (" ", stream
);
678 if (TYPE_FN_FIELD_STUB (f
, j
))
679 /* Build something we can demangle. */
680 mangled_name
= gdb_mangle_name (type
, i
, j
);
682 mangled_name
= TYPE_FN_FIELD_PHYSNAME (f
, j
);
685 cplus_demangle (mangled_name
,
686 DMGL_ANSI
| DMGL_PARAMS
);
687 if (demangled_name
== NULL
)
688 fprintf_filtered (stream
, "<badly mangled name '%s'>",
692 char *demangled_no_class
=
693 strchr (demangled_name
, ':');
695 if (demangled_no_class
== NULL
)
696 demangled_no_class
= demangled_name
;
699 if (*++demangled_no_class
== ':')
700 ++demangled_no_class
;
702 fputs_filtered (demangled_no_class
, stream
);
703 free (demangled_name
);
706 if (TYPE_FN_FIELD_STUB (f
, j
))
709 fprintf_filtered (stream
, ";\n");
713 fprintfi_filtered (level
, stream
, "}");
718 fprintf_filtered (stream
, "enum ");
719 if (TYPE_TAG_NAME (type
) != NULL
)
721 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
723 fputs_filtered (" ", stream
);
729 /* If we just printed a tag name, no need to print anything else. */
730 if (TYPE_TAG_NAME (type
) == NULL
)
731 fprintf_filtered (stream
, "{...}");
733 else if (show
> 0 || TYPE_TAG_NAME (type
) == NULL
)
735 fprintf_filtered (stream
, "{");
736 len
= TYPE_NFIELDS (type
);
738 for (i
= 0; i
< len
; i
++)
741 if (i
) fprintf_filtered (stream
, ", ");
743 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
744 if (lastval
!= TYPE_FIELD_BITPOS (type
, i
))
746 fprintf_filtered (stream
, " = %d", TYPE_FIELD_BITPOS (type
, i
));
747 lastval
= TYPE_FIELD_BITPOS (type
, i
);
751 fprintf_filtered (stream
, "}");
756 fprintf_filtered (stream
, "void");
759 case TYPE_CODE_UNDEF
:
760 fprintf_filtered (stream
, "struct <unknown>");
763 case TYPE_CODE_ERROR
:
764 fprintf_filtered (stream
, "<unknown type>");
767 case TYPE_CODE_RANGE
:
768 /* This should not occur */
769 fprintf_filtered (stream
, "<range type>");
773 /* Handle types not explicitly handled by the other cases,
774 such as fundamental types. For these, just print whatever
775 the type name is, as recorded in the type itself. If there
776 is no type name, then complain. */
777 if (TYPE_NAME (type
) != NULL
)
779 fputs_filtered (TYPE_NAME (type
), stream
);
783 /* At least for dump_symtab, it is important that this not be
785 fprintf_filtered (stream
, "<invalid type code %d>",