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 || !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 error ("Missing Chill support in function c_typedef_print."); /*FIXME*/
95 error("Language not supported.");
97 fprintf_filtered(stream
, ";\n");
101 /* LEVEL is the depth to indent lines by. */
104 c_print_type (type
, varstring
, stream
, show
, level
)
111 register enum type_code code
;
114 c_type_print_base (type
, stream
, show
, level
);
115 code
= TYPE_CODE (type
);
116 if ((varstring
!= NULL
&& *varstring
!= '\0')
118 /* Need a space if going to print stars or brackets;
119 but not if we will print just a type name. */
120 ((show
> 0 || TYPE_NAME (type
) == 0)
122 (code
== TYPE_CODE_PTR
|| code
== TYPE_CODE_FUNC
123 || code
== TYPE_CODE_METHOD
124 || code
== TYPE_CODE_ARRAY
125 || code
== TYPE_CODE_MEMBER
126 || code
== TYPE_CODE_REF
)))
127 fputs_filtered (" ", stream
);
128 c_type_print_varspec_prefix (type
, stream
, show
, 0);
130 fputs_filtered (varstring
, stream
);
132 /* For demangled function names, we have the arglist as part of the name,
133 so don't print an additional pair of ()'s */
135 demangled_args
= varstring
[strlen(varstring
) - 1] == ')';
136 c_type_print_varspec_suffix (type
, stream
, show
, 0, demangled_args
);
140 /* Print the C++ method arguments ARGS to the file STREAM. */
143 cp_type_print_method_args (args
, prefix
, varstring
, staticp
, stream
)
152 fprintf_symbol_filtered (stream
, prefix
, language_cplus
, DMGL_ANSI
);
153 fprintf_symbol_filtered (stream
, varstring
, language_cplus
, DMGL_ANSI
);
154 fputs_filtered (" (", stream
);
155 if (args
&& args
[!staticp
] && args
[!staticp
]->code
!= TYPE_CODE_VOID
)
157 i
= !staticp
; /* skip the class variable */
160 type_print (args
[i
++], "", stream
, 0);
163 fprintf_filtered (stream
, " ...");
166 else if (args
[i
]->code
!= TYPE_CODE_VOID
)
168 fprintf_filtered (stream
, ", ");
173 fprintf_filtered (stream
, ")");
176 /* If TYPE is a derived type, then print out derivation information.
177 Print only the actual base classes of this type, not the base classes
178 of the base classes. I.E. for the derivation hierarchy:
181 class B : public A {int b; };
182 class C : public B {int c; };
184 Print the type of class C as:
190 Not as the following (like gdb used to), which is not legal C++ syntax for
191 derived types and may be confused with the multiple inheritance form:
193 class C : public B : public A {
197 In general, gdb should try to print the types as closely as possible to
198 the form that they appear in the source code. */
201 cp_type_print_derivation_info (stream
, type
)
208 for (i
= 0; i
< TYPE_N_BASECLASSES (type
); i
++)
210 fputs_filtered (i
== 0 ? ": " : ", ", stream
);
211 fprintf_filtered (stream
, "%s%s ",
212 BASETYPE_VIA_PUBLIC (type
, i
) ? "public" : "private",
213 BASETYPE_VIA_VIRTUAL(type
, i
) ? " virtual" : "");
214 name
= type_name_no_tag (TYPE_BASECLASS (type
, i
));
215 fprintf_filtered (stream
, "%s", name
? name
: "(null)");
219 fputs_filtered (" ", stream
);
223 /* Print any asterisks or open-parentheses needed before the
224 variable name (to describe its type).
226 On outermost call, pass 0 for PASSED_A_PTR.
227 On outermost call, SHOW > 0 means should ignore
228 any typename for TYPE and show its details.
229 SHOW is always zero on recursive calls. */
232 c_type_print_varspec_prefix (type
, stream
, show
, passed_a_ptr
)
242 if (TYPE_NAME (type
) && show
<= 0)
247 switch (TYPE_CODE (type
))
250 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 1);
251 fprintf_filtered (stream
, "*");
254 case TYPE_CODE_MEMBER
:
256 fprintf_filtered (stream
, "(");
257 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
258 fprintf_filtered (stream
, " ");
259 name
= type_name_no_tag (TYPE_DOMAIN_TYPE (type
));
261 fputs_filtered (name
, stream
);
263 c_type_print_base (TYPE_DOMAIN_TYPE (type
), stream
, 0, passed_a_ptr
);
264 fprintf_filtered (stream
, "::");
267 case TYPE_CODE_METHOD
:
269 fprintf (stream
, "(");
270 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
273 fprintf_filtered (stream
, " ");
274 c_type_print_base (TYPE_DOMAIN_TYPE (type
), stream
, 0, passed_a_ptr
);
275 fprintf_filtered (stream
, "::");
280 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 1);
281 fprintf_filtered (stream
, "&");
285 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
287 fprintf_filtered (stream
, "(");
290 case TYPE_CODE_ARRAY
:
291 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
), stream
, 0, 0);
293 fprintf_filtered (stream
, "(");
296 case TYPE_CODE_UNDEF
:
297 case TYPE_CODE_STRUCT
:
298 case TYPE_CODE_UNION
:
303 case TYPE_CODE_ERROR
:
307 case TYPE_CODE_RANGE
:
308 case TYPE_CODE_STRING
:
309 /* These types need no prefix. They are listed here so that
310 gcc -Wall will reveal any types that haven't been handled. */
316 c_type_print_args (type
, stream
)
323 fprintf_filtered (stream
, "(");
324 args
= TYPE_ARG_TYPES (type
);
329 fprintf_filtered (stream
, "...");
334 args
[i
] != NULL
&& args
[i
]->code
!= TYPE_CODE_VOID
;
337 c_print_type (args
[i
], "", stream
, -1, 0);
338 if (args
[i
+1] == NULL
)
340 fprintf_filtered (stream
, "...");
342 else if (args
[i
+1]->code
!= TYPE_CODE_VOID
)
344 fprintf_filtered (stream
, ",");
350 fprintf_filtered (stream
, ")");
353 /* Print any array sizes, function arguments or close parentheses
354 needed after the variable name (to describe its type).
355 Args work like c_type_print_varspec_prefix. */
358 c_type_print_varspec_suffix (type
, stream
, show
, passed_a_ptr
, demangled_args
)
368 if (TYPE_NAME (type
) && show
<= 0)
373 switch (TYPE_CODE (type
))
375 case TYPE_CODE_ARRAY
:
377 fprintf_filtered (stream
, ")");
379 fprintf_filtered (stream
, "[");
380 if (TYPE_LENGTH (type
) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type
)) > 0)
381 fprintf_filtered (stream
, "%d",
383 / TYPE_LENGTH (TYPE_TARGET_TYPE (type
))));
384 fprintf_filtered (stream
, "]");
386 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 0, 0);
389 case TYPE_CODE_MEMBER
:
391 fprintf_filtered (stream
, ")");
392 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 0, 0);
395 case TYPE_CODE_METHOD
:
397 fprintf_filtered (stream
, ")");
398 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 0, 0);
401 c_type_print_args (type
, stream
);
407 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0, 1, 0);
411 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
, 0,
414 fprintf_filtered (stream
, ")");
416 fprintf_filtered (stream
, "()");
419 case TYPE_CODE_UNDEF
:
420 case TYPE_CODE_STRUCT
:
421 case TYPE_CODE_UNION
:
426 case TYPE_CODE_ERROR
:
430 case TYPE_CODE_RANGE
:
431 case TYPE_CODE_STRING
:
432 /* These types do not need a suffix. They are listed so that
433 gcc -Wall will report types that may not have been considered. */
438 /* Print the name of the type (or the ultimate pointer target,
439 function value or array element), or the description of a
442 SHOW nonzero means don't print this type as just its name;
443 show its real definition even if it has a name.
444 SHOW zero means print just typename or struct tag if there is one
445 SHOW negative means abbreviate structure elements.
446 SHOW is decremented for printing of structure elements.
448 LEVEL is the depth to indent by.
449 We increase it for some recursive calls. */
452 c_type_print_base (type
, stream
, show
, level
)
461 register int lastval
;
463 char *demangled_name
;
464 enum {s_none
, s_public
, s_private
, s_protected
} section_type
;
470 fputs_filtered ("<type unknown>", stream
);
474 /* When SHOW is zero or less, and there is a valid type name, then always
475 just print the type name directly from the type. */
477 if ((show
<= 0) && (TYPE_NAME (type
) != NULL
))
479 fputs_filtered (TYPE_NAME (type
), stream
);
483 switch (TYPE_CODE (type
))
485 case TYPE_CODE_ARRAY
:
487 case TYPE_CODE_MEMBER
:
490 case TYPE_CODE_METHOD
:
491 c_type_print_base (TYPE_TARGET_TYPE (type
), stream
, show
, level
);
494 case TYPE_CODE_STRUCT
:
495 fprintf_filtered (stream
,
496 HAVE_CPLUS_STRUCT (type
) ? "class " : "struct ");
499 case TYPE_CODE_UNION
:
500 fprintf_filtered (stream
, "union ");
502 if ((name
= type_name_no_tag (type
)) != NULL
)
504 fputs_filtered (name
, stream
);
505 fputs_filtered (" ", stream
);
509 fprintf_filtered (stream
, "{...}");
512 check_stub_type (type
);
514 cp_type_print_derivation_info (stream
, type
);
516 fprintf_filtered (stream
, "{\n");
517 if ((TYPE_NFIELDS (type
) == 0) && (TYPE_NFN_FIELDS (type
) == 0))
519 if (TYPE_FLAGS (type
) & TYPE_FLAG_STUB
)
520 fprintfi_filtered (level
+ 4, stream
, "<incomplete type>\n");
522 fprintfi_filtered (level
+ 4, stream
, "<no data fields>\n");
525 /* Start off with no specific section type, so we can print
526 one for the first field we find, and use that section type
527 thereafter until we find another type. */
529 section_type
= s_none
;
531 /* If there is a base class for this type,
532 do not print the field that it occupies. */
534 len
= TYPE_NFIELDS (type
);
535 for (i
= TYPE_N_BASECLASSES (type
); i
< len
; i
++)
538 /* Don't print out virtual function table. */
539 if ((TYPE_FIELD_NAME (type
, i
))[5] == CPLUS_MARKER
&&
540 !strncmp (TYPE_FIELD_NAME (type
, i
), "_vptr", 5))
543 /* If this is a C++ class we can print the various C++ section
546 if (HAVE_CPLUS_STRUCT (type
))
548 if (TYPE_FIELD_PROTECTED (type
, i
))
550 if (section_type
!= s_protected
)
552 section_type
= s_protected
;
553 fprintfi_filtered (level
+ 2, stream
,
557 else if (TYPE_FIELD_PRIVATE (type
, i
))
559 if (section_type
!= s_private
)
561 section_type
= s_private
;
562 fprintfi_filtered (level
+ 2, stream
, "private:\n");
567 if (section_type
!= s_public
)
569 section_type
= s_public
;
570 fprintfi_filtered (level
+ 2, stream
, "public:\n");
575 print_spaces_filtered (level
+ 4, stream
);
576 if (TYPE_FIELD_STATIC (type
, i
))
578 fprintf_filtered (stream
, "static ");
580 c_print_type (TYPE_FIELD_TYPE (type
, i
),
581 TYPE_FIELD_NAME (type
, i
),
582 stream
, show
- 1, level
+ 4);
583 if (!TYPE_FIELD_STATIC (type
, i
)
584 && TYPE_FIELD_PACKED (type
, i
))
586 /* It is a bitfield. This code does not attempt
587 to look at the bitpos and reconstruct filler,
588 unnamed fields. This would lead to misleading
589 results if the compiler does not put out fields
590 for such things (I don't know what it does). */
591 fprintf_filtered (stream
, " : %d",
592 TYPE_FIELD_BITSIZE (type
, i
));
594 fprintf_filtered (stream
, ";\n");
597 /* If there are both fields and methods, put a space between. */
598 len
= TYPE_NFN_FIELDS (type
);
599 if (len
&& section_type
!= s_none
)
600 fprintf_filtered (stream
, "\n");
602 /* C++: print out the methods */
604 for (i
= 0; i
< len
; i
++)
606 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
607 int j
, len2
= TYPE_FN_FIELDLIST_LENGTH (type
, i
);
608 char *method_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
609 int is_constructor
= name
&& STREQ(method_name
, name
);
610 for (j
= 0; j
< len2
; j
++)
613 if (TYPE_FN_FIELD_PROTECTED (f
, j
))
615 if (section_type
!= s_protected
)
617 section_type
= s_protected
;
618 fprintfi_filtered (level
+ 2, stream
,
622 else if (TYPE_FN_FIELD_PRIVATE (f
, j
))
624 if (section_type
!= s_private
)
626 section_type
= s_private
;
627 fprintfi_filtered (level
+ 2, stream
, "private:\n");
632 if (section_type
!= s_public
)
634 section_type
= s_public
;
635 fprintfi_filtered (level
+ 2, stream
, "public:\n");
639 print_spaces_filtered (level
+ 4, stream
);
640 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
641 fprintf_filtered (stream
, "virtual ");
642 else if (TYPE_FN_FIELD_STATIC_P (f
, j
))
643 fprintf_filtered (stream
, "static ");
644 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)) == 0)
646 /* Keep GDB from crashing here. */
647 fprintf (stream
, "<undefined type> %s;\n",
648 TYPE_FN_FIELD_PHYSNAME (f
, j
));
651 else if (!is_constructor
)
653 type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)),
655 fputs_filtered (" ", stream
);
657 if (TYPE_FN_FIELD_STUB (f
, j
))
659 /* Build something we can demangle. */
660 mangled_name
= gdb_mangle_name (type
, i
, j
);
662 cplus_demangle (mangled_name
,
663 DMGL_ANSI
| DMGL_PARAMS
);
664 if (demangled_name
== NULL
)
665 fprintf_filtered (stream
, "<badly mangled name %s>",
669 fprintf_filtered (stream
, "%s",
670 strchr (demangled_name
, ':') + 2);
671 free (demangled_name
);
675 else if (TYPE_FN_FIELD_PHYSNAME (f
, j
)[0] == '_'
676 && TYPE_FN_FIELD_PHYSNAME (f
, j
)[1] == CPLUS_MARKER
)
677 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f
, j
) + 1,
678 "~", method_name
, 0, stream
);
680 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f
, j
), "",
682 TYPE_FN_FIELD_STATIC_P (f
, j
),
685 fprintf_filtered (stream
, ";\n");
689 fprintfi_filtered (level
, stream
, "}");
694 fprintf_filtered (stream
, "enum ");
695 if ((name
= type_name_no_tag (type
)) != NULL
)
697 fputs_filtered (name
, stream
);
698 fputs_filtered (" ", stream
);
702 fprintf_filtered (stream
, "{...}");
705 fprintf_filtered (stream
, "{");
706 len
= TYPE_NFIELDS (type
);
708 for (i
= 0; i
< len
; i
++)
711 if (i
) fprintf_filtered (stream
, ", ");
713 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
714 if (lastval
!= TYPE_FIELD_BITPOS (type
, i
))
716 fprintf_filtered (stream
, " = %d", TYPE_FIELD_BITPOS (type
, i
));
717 lastval
= TYPE_FIELD_BITPOS (type
, i
);
721 fprintf_filtered (stream
, "}");
726 fprintf_filtered (stream
, "void");
729 case TYPE_CODE_UNDEF
:
730 fprintf_filtered (stream
, "struct <unknown>");
733 case TYPE_CODE_ERROR
:
734 fprintf_filtered (stream
, "<unknown type>");
737 case TYPE_CODE_RANGE
:
738 /* This should not occur */
739 fprintf_filtered (stream
, "<range type>");
743 /* Handle types not explicitly handled by the other cases,
744 such as fundamental types. For these, just print whatever
745 the type name is, as recorded in the type itself. If there
746 is no type name, then complain. */
747 if (TYPE_NAME (type
) != NULL
)
749 fputs_filtered (TYPE_NAME (type
), stream
);
753 error ("Invalid type code (%d) in symbol table.", TYPE_CODE (type
));