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"
39 extern int demangle
; /* whether to print C++ syms raw or source-form */
42 c_type_print_args
PARAMS ((struct type
*, FILE *));
45 c_type_print_varspec_suffix
PARAMS ((struct type
*, FILE *, int, int, int));
48 cp_type_print_derivation_info
PARAMS ((FILE *, struct type
*));
51 c_type_print_varspec_prefix
PARAMS ((struct type
*, FILE *, int, int));
54 c_type_print_base
PARAMS ((struct type
*, 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 || 0 != strcmp (TYPE_NAME ((SYMBOL_TYPE (new))),
77 fprintf_filtered(stream
, " %s", SYMBOL_NAME(new));
82 fprintf_filtered(stream
, "TYPE ");
83 if(!TYPE_NAME(SYMBOL_TYPE(new)) ||
84 strcmp (TYPE_NAME(SYMBOL_TYPE(new)),
86 fprintf_filtered(stream
, "%s = ", SYMBOL_NAME(new));
88 fprintf_filtered(stream
, "<builtin> = ");
89 type_print(type
,"",stream
,0);
92 /* start-sanitize-chill */
95 error ("Missing Chill support in function c_typedef_print."); /*FIXME*/
97 /* end-sanitize-chill */
99 error("Language not supported.");
101 fprintf_filtered(stream
, ";\n");
105 /* LEVEL is the depth to indent lines by. */
108 c_print_type (type
, varstring
, stream
, show
, level
)
115 register enum type_code code
;
116 char *demangled
= NULL
;
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 /* See if the name has a C++ demangled equivalent, and if so, print that
140 demangled
= cplus_demangle (varstring
, DMGL_ANSI
| DMGL_PARAMS
);
142 fputs_filtered ((demangled
!= NULL
) ? demangled
: varstring
, stream
);
144 /* For demangled function names, we have the arglist as part of the name,
145 so don't print an additional pair of ()'s */
147 demangled_args
= (demangled
!= NULL
) && (code
== TYPE_CODE_FUNC
);
148 c_type_print_varspec_suffix (type
, stream
, show
, 0, demangled_args
);
150 if (demangled
!= NULL
)
156 /* Print the C++ method arguments ARGS to the file STREAM. */
159 cp_type_print_method_args (args
, prefix
, varstring
, staticp
, stream
)
168 fputs_demangled (prefix
, stream
, DMGL_ANSI
| DMGL_PARAMS
);
169 fputs_demangled (varstring
, stream
, DMGL_ANSI
| DMGL_PARAMS
);
170 fputs_filtered (" (", stream
);
171 if (args
&& args
[!staticp
] && args
[!staticp
]->code
!= TYPE_CODE_VOID
)
173 i
= !staticp
; /* skip the class variable */
176 type_print (args
[i
++], "", stream
, 0);
179 fprintf_filtered (stream
, " ...");
182 else if (args
[i
]->code
!= TYPE_CODE_VOID
)
184 fprintf_filtered (stream
, ", ");
189 fprintf_filtered (stream
, ")");
192 /* If TYPE is a derived type, then print out derivation information.
193 Print only the actual base classes of this type, not the base classes
194 of the base classes. I.E. for the derivation hierarchy:
197 class B : public A {int b; };
198 class C : public B {int c; };
200 Print the type of class C as:
206 Not as the following (like gdb used to), which is not legal C++ syntax for
207 derived types and may be confused with the multiple inheritance form:
209 class C : public B : public A {
213 In general, gdb should try to print the types as closely as possible to
214 the form that they appear in the source code. */
217 cp_type_print_derivation_info (stream
, type
)
224 for (i
= 0; i
< TYPE_N_BASECLASSES (type
); i
++)
226 fputs_filtered (i
== 0 ? ": " : ", ", stream
);
227 fprintf_filtered (stream
, "%s%s ",
228 BASETYPE_VIA_PUBLIC (type
, i
) ? "public" : "private",
229 BASETYPE_VIA_VIRTUAL(type
, i
) ? " virtual" : "");
230 name
= type_name_no_tag (TYPE_BASECLASS (type
, i
));
231 fprintf_filtered (stream
, "%s", name
? name
: "(null)");
235 fputs_filtered (" ", stream
);
239 /* Print any asterisks or open-parentheses needed before the
240 variable name (to describe its type).
242 On outermost call, pass 0 for PASSED_A_PTR.
243 On outermost call, SHOW > 0 means should ignore
244 any typename for TYPE and show its details.
245 SHOW is always zero on recursive calls. */
248 c_type_print_varspec_prefix (type
, stream
, show
, passed_a_ptr
)
258 if (TYPE_NAME (type
) && show
<= 0)
263 switch (TYPE_CODE (type
))
266 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 1);
267 fprintf_filtered (stream
, "*");
270 case TYPE_CODE_MEMBER
:
272 fprintf_filtered (stream
, "(");
273 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
274 fprintf_filtered (stream
, " ");
275 name
= type_name_no_tag (TYPE_DOMAIN_TYPE (type
));
277 fputs_filtered (name
, stream
);
279 c_type_print_base (TYPE_DOMAIN_TYPE (type
), stream
, 0, passed_a_ptr
);
280 fprintf_filtered (stream
, "::");
283 case TYPE_CODE_METHOD
:
285 fprintf (stream
, "(");
286 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
289 fprintf_filtered (stream
, " ");
290 c_type_print_base (TYPE_DOMAIN_TYPE (type
), stream
, 0, passed_a_ptr
);
291 fprintf_filtered (stream
, "::");
296 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 1);
297 fprintf_filtered (stream
, "&");
301 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
303 fprintf_filtered (stream
, "(");
306 case TYPE_CODE_ARRAY
:
307 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
309 fprintf_filtered (stream
, "(");
312 case TYPE_CODE_UNDEF
:
313 case TYPE_CODE_STRUCT
:
314 case TYPE_CODE_UNION
:
319 case TYPE_CODE_ERROR
:
323 case TYPE_CODE_RANGE
:
324 case TYPE_CODE_PASCAL_ARRAY
:
325 /* These types need no prefix. They are listed here so that
326 gcc -Wall will reveal any types that haven't been handled. */
332 c_type_print_args (type
, stream
)
339 fprintf_filtered (stream
, "(");
340 args
= TYPE_ARG_TYPES (type
);
345 fprintf_filtered (stream
, "...");
350 args
[i
] != NULL
&& args
[i
]->code
!= TYPE_CODE_VOID
;
353 c_print_type (args
[i
], "", stream
, -1, 0);
354 if (args
[i
+1] == NULL
)
356 fprintf_filtered (stream
, "...");
358 else if (args
[i
+1]->code
!= TYPE_CODE_VOID
)
360 fprintf_filtered (stream
, ",");
366 fprintf_filtered (stream
, ")");
369 /* Print any array sizes, function arguments or close parentheses
370 needed after the variable name (to describe its type).
371 Args work like c_type_print_varspec_prefix. */
374 c_type_print_varspec_suffix (type
, stream
, show
, passed_a_ptr
, demangled_args
)
384 if (TYPE_NAME (type
) && show
<= 0)
389 switch (TYPE_CODE (type
))
391 case TYPE_CODE_ARRAY
:
393 fprintf_filtered (stream
, ")");
395 fprintf_filtered (stream
, "[");
396 if (TYPE_LENGTH (type
) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type
)) > 0)
397 fprintf_filtered (stream
, "%d",
399 / TYPE_LENGTH (TYPE_TARGET_TYPE (type
))));
400 fprintf_filtered (stream
, "]");
402 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 0, 0);
405 case TYPE_CODE_MEMBER
:
407 fprintf_filtered (stream
, ")");
408 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 0, 0);
411 case TYPE_CODE_METHOD
:
413 fprintf_filtered (stream
, ")");
414 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 0, 0);
417 c_type_print_args (type
, stream
);
423 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 1, 0);
427 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0,
430 fprintf_filtered (stream
, ")");
432 fprintf_filtered (stream
, "()");
435 case TYPE_CODE_UNDEF
:
436 case TYPE_CODE_STRUCT
:
437 case TYPE_CODE_UNION
:
442 case TYPE_CODE_ERROR
:
446 case TYPE_CODE_RANGE
:
447 case TYPE_CODE_PASCAL_ARRAY
:
448 /* These types do not need a suffix. They are listed so that
449 gcc -Wall will report types that may not have been considered. */
454 /* Print the name of the type (or the ultimate pointer target,
455 function value or array element), or the description of a
458 SHOW nonzero means don't print this type as just its name;
459 show its real definition even if it has a name.
460 SHOW zero means print just typename or struct tag if there is one
461 SHOW negative means abbreviate structure elements.
462 SHOW is decremented for printing of structure elements.
464 LEVEL is the depth to indent by.
465 We increase it for some recursive calls. */
468 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. */
493 if ((show
<= 0) && (TYPE_NAME (type
) != NULL
))
495 fputs_filtered (TYPE_NAME (type
), stream
);
499 switch (TYPE_CODE (type
))
501 case TYPE_CODE_ARRAY
:
503 case TYPE_CODE_MEMBER
:
506 case TYPE_CODE_METHOD
:
507 c_type_print_base (TYPE_TARGET_TYPE (type
), stream
, show
, level
);
510 case TYPE_CODE_STRUCT
:
511 fprintf_filtered (stream
,
512 HAVE_CPLUS_STRUCT (type
) ? "class " : "struct ");
515 case TYPE_CODE_UNION
:
516 fprintf_filtered (stream
, "union ");
518 if ((name
= type_name_no_tag (type
)) != NULL
)
520 fputs_filtered (name
, stream
);
521 fputs_filtered (" ", stream
);
525 fprintf_filtered (stream
, "{...}");
528 check_stub_type (type
);
530 cp_type_print_derivation_info (stream
, type
);
532 fprintf_filtered (stream
, "{\n");
533 if ((TYPE_NFIELDS (type
) == 0) && (TYPE_NFN_FIELDS (type
) == 0))
535 if (TYPE_FLAGS (type
) & TYPE_FLAG_STUB
)
536 fprintfi_filtered (level
+ 4, stream
, "<incomplete type>\n");
538 fprintfi_filtered (level
+ 4, stream
, "<no data fields>\n");
541 /* Start off with no specific section type, so we can print
542 one for the first field we find, and use that section type
543 thereafter until we find another type. */
545 section_type
= s_none
;
547 /* If there is a base class for this type,
548 do not print the field that it occupies. */
550 len
= TYPE_NFIELDS (type
);
551 for (i
= TYPE_N_BASECLASSES (type
); i
< len
; i
++)
554 /* Don't print out virtual function table. */
555 if ((TYPE_FIELD_NAME (type
, i
))[5] == CPLUS_MARKER
&&
556 !strncmp (TYPE_FIELD_NAME (type
, i
), "_vptr", 5))
559 /* If this is a C++ class we can print the various C++ section
562 if (HAVE_CPLUS_STRUCT (type
))
564 if (TYPE_FIELD_PROTECTED (type
, i
))
566 if (section_type
!= s_protected
)
568 section_type
= s_protected
;
569 fprintfi_filtered (level
+ 2, stream
,
573 else if (TYPE_FIELD_PRIVATE (type
, i
))
575 if (section_type
!= s_private
)
577 section_type
= s_private
;
578 fprintfi_filtered (level
+ 2, stream
, "private:\n");
583 if (section_type
!= s_public
)
585 section_type
= s_public
;
586 fprintfi_filtered (level
+ 2, stream
, "public:\n");
591 print_spaces_filtered (level
+ 4, stream
);
592 if (TYPE_FIELD_STATIC (type
, i
))
594 fprintf_filtered (stream
, "static ");
596 c_print_type (TYPE_FIELD_TYPE (type
, i
),
597 TYPE_FIELD_NAME (type
, i
),
598 stream
, show
- 1, level
+ 4);
599 if (!TYPE_FIELD_STATIC (type
, i
)
600 && TYPE_FIELD_PACKED (type
, i
))
602 /* It is a bitfield. This code does not attempt
603 to look at the bitpos and reconstruct filler,
604 unnamed fields. This would lead to misleading
605 results if the compiler does not put out fields
606 for such things (I don't know what it does). */
607 fprintf_filtered (stream
, " : %d",
608 TYPE_FIELD_BITSIZE (type
, i
));
610 fprintf_filtered (stream
, ";\n");
613 /* If there are both fields and methods, put a space between. */
614 len
= TYPE_NFN_FIELDS (type
);
615 if (len
&& section_type
!= s_none
)
616 fprintf_filtered (stream
, "\n");
618 /* C++: print out the methods */
620 for (i
= 0; i
< len
; i
++)
622 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
623 int j
, len2
= TYPE_FN_FIELDLIST_LENGTH (type
, i
);
624 char *method_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
625 int is_constructor
= name
&& strcmp(method_name
, name
) == 0;
626 for (j
= 0; j
< len2
; j
++)
629 if (TYPE_FN_FIELD_PROTECTED (f
, j
))
631 if (section_type
!= s_protected
)
633 section_type
= s_protected
;
634 fprintfi_filtered (level
+ 2, stream
,
638 else if (TYPE_FN_FIELD_PRIVATE (f
, j
))
640 if (section_type
!= s_private
)
642 section_type
= s_private
;
643 fprintfi_filtered (level
+ 2, stream
, "private:\n");
648 if (section_type
!= s_public
)
650 section_type
= s_public
;
651 fprintfi_filtered (level
+ 2, stream
, "public:\n");
655 print_spaces_filtered (level
+ 4, stream
);
656 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
657 fprintf_filtered (stream
, "virtual ");
658 else if (TYPE_FN_FIELD_STATIC_P (f
, j
))
659 fprintf_filtered (stream
, "static ");
660 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)) == 0)
662 /* Keep GDB from crashing here. */
663 fprintf (stream
, "<undefined type> %s;\n",
664 TYPE_FN_FIELD_PHYSNAME (f
, j
));
667 else if (!is_constructor
)
669 type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)),
671 fputs_filtered (" ", stream
);
673 if (TYPE_FN_FIELD_STUB (f
, j
))
675 /* Build something we can demangle. */
676 mangled_name
= gdb_mangle_name (type
, i
, j
);
678 cplus_demangle (mangled_name
,
679 DMGL_ANSI
| DMGL_PARAMS
);
680 if (demangled_name
== NULL
)
681 fprintf_filtered (stream
, "<badly mangled name %s>",
685 fprintf_filtered (stream
, "%s",
686 strchr (demangled_name
, ':') + 2);
687 free (demangled_name
);
691 else if (TYPE_FN_FIELD_PHYSNAME (f
, j
)[0] == '_'
692 && TYPE_FN_FIELD_PHYSNAME (f
, j
)[1] == CPLUS_MARKER
)
693 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f
, j
) + 1,
694 "~", method_name
, 0, stream
);
696 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f
, j
), "",
698 TYPE_FN_FIELD_STATIC_P (f
, j
),
701 fprintf_filtered (stream
, ";\n");
705 fprintfi_filtered (level
, stream
, "}");
710 fprintf_filtered (stream
, "enum ");
711 if ((name
= type_name_no_tag (type
)) != NULL
)
713 fputs_filtered (name
, stream
);
714 fputs_filtered (" ", stream
);
718 fprintf_filtered (stream
, "{...}");
721 fprintf_filtered (stream
, "{");
722 len
= TYPE_NFIELDS (type
);
724 for (i
= 0; i
< len
; i
++)
727 if (i
) fprintf_filtered (stream
, ", ");
729 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
730 if (lastval
!= TYPE_FIELD_BITPOS (type
, i
))
732 fprintf_filtered (stream
, " = %d", TYPE_FIELD_BITPOS (type
, i
));
733 lastval
= TYPE_FIELD_BITPOS (type
, i
);
737 fprintf_filtered (stream
, "}");
742 fprintf_filtered (stream
, "void");
745 case TYPE_CODE_UNDEF
:
746 fprintf_filtered (stream
, "struct <unknown>");
749 case TYPE_CODE_ERROR
:
750 fprintf_filtered (stream
, "<unknown type>");
753 case TYPE_CODE_RANGE
:
754 /* This should not occur */
755 fprintf_filtered (stream
, "<range type>");
759 /* Handle types not explicitly handled by the other cases,
760 such as fundamental types. For these, just print whatever
761 the type name is, as recorded in the type itself. If there
762 is no type name, then complain. */
763 if (TYPE_NAME (type
) != NULL
)
765 fputs_filtered (TYPE_NAME (type
), stream
);
769 error ("Invalid type code (%d) in symbol table.", TYPE_CODE (type
));