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., 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 fputs_filtered (varstring, stream);
140 /* For demangled function names, we have the arglist as part of the name,
141 so don't print an additional pair of ()'s */
143 demangled_args = strchr(varstring, '(') != NULL;
144 c_type_print_varspec_suffix (type, stream, show, 0, demangled_args);
148 /* Print the C++ method arguments ARGS to the file STREAM. */
151 cp_type_print_method_args (args, prefix, varstring, staticp, stream)
160 fprintf_symbol_filtered (stream, prefix, language_cplus, DMGL_ANSI);
161 fprintf_symbol_filtered (stream, varstring, language_cplus, DMGL_ANSI);
162 fputs_filtered (" (", stream);
163 if (args && args[!staticp] && args[!staticp]->code != TYPE_CODE_VOID)
165 i = !staticp; /* skip the class variable */
168 type_print (args[i++], "", stream, 0);
171 fprintf_filtered (stream, " ...");
174 else if (args[i]->code != TYPE_CODE_VOID)
176 fprintf_filtered (stream, ", ");
181 fprintf_filtered (stream, ")");
184 /* If TYPE is a derived type, then print out derivation information.
185 Print only the actual base classes of this type, not the base classes
186 of the base classes. I.E. for the derivation hierarchy:
189 class B : public A {int b; };
190 class C : public B {int c; };
192 Print the type of class C as:
198 Not as the following (like gdb used to), which is not legal C++ syntax for
199 derived types and may be confused with the multiple inheritance form:
201 class C : public B : public A {
205 In general, gdb should try to print the types as closely as possible to
206 the form that they appear in the source code. */
209 cp_type_print_derivation_info (stream, type)
216 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
218 fputs_filtered (i == 0 ? ": " : ", ", stream);
219 fprintf_filtered (stream, "%s%s ",
220 BASETYPE_VIA_PUBLIC (type, i) ? "public" : "private",
221 BASETYPE_VIA_VIRTUAL(type, i) ? " virtual" : "");
222 name = type_name_no_tag (TYPE_BASECLASS (type, i));
223 fprintf_filtered (stream, "%s", name ? name : "(null)");
227 fputs_filtered (" ", stream);
231 /* Print any asterisks or open-parentheses needed before the
232 variable name (to describe its type).
234 On outermost call, pass 0 for PASSED_A_PTR.
235 On outermost call, SHOW > 0 means should ignore
236 any typename for TYPE and show its details.
237 SHOW is always zero on recursive calls. */
240 c_type_print_varspec_prefix (type, stream, show, passed_a_ptr)
250 if (TYPE_NAME (type) && show <= 0)
255 switch (TYPE_CODE (type))
258 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1);
259 fprintf_filtered (stream, "*");
262 case TYPE_CODE_MEMBER:
264 fprintf_filtered (stream, "(");
265 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
266 fprintf_filtered (stream, " ");
267 name = type_name_no_tag (TYPE_DOMAIN_TYPE (type));
269 fputs_filtered (name, stream);
271 c_type_print_base (TYPE_DOMAIN_TYPE (type), stream, 0, passed_a_ptr);
272 fprintf_filtered (stream, "::");
275 case TYPE_CODE_METHOD:
277 fprintf_unfiltered (stream, "(");
278 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
281 fprintf_filtered (stream, " ");
282 c_type_print_base (TYPE_DOMAIN_TYPE (type), stream, 0, passed_a_ptr);
283 fprintf_filtered (stream, "::");
288 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1);
289 fprintf_filtered (stream, "&");
293 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
295 fprintf_filtered (stream, "(");
298 case TYPE_CODE_ARRAY:
299 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
301 fprintf_filtered (stream, "(");
304 case TYPE_CODE_UNDEF:
305 case TYPE_CODE_STRUCT:
306 case TYPE_CODE_UNION:
311 case TYPE_CODE_ERROR:
315 case TYPE_CODE_RANGE:
316 case TYPE_CODE_STRING:
317 case TYPE_CODE_BITSTRING:
318 case TYPE_CODE_COMPLEX:
319 case TYPE_CODE_TYPEDEF:
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 && TYPE_ARRAY_UPPER_BOUND_TYPE(type) != BOUND_CANNOT_BE_DETERMINED)
393 fprintf_filtered (stream, "%d",
395 / TYPE_LENGTH (TYPE_TARGET_TYPE (type))));
396 fprintf_filtered (stream, "]");
398 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0);
401 case TYPE_CODE_MEMBER:
403 fprintf_filtered (stream, ")");
404 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0);
407 case TYPE_CODE_METHOD:
409 fprintf_filtered (stream, ")");
410 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0);
413 c_type_print_args (type, stream);
419 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 1, 0);
424 fprintf_filtered (stream, ")");
426 { int i, len = TYPE_NFIELDS (type);
427 fprintf_filtered (stream, "(");
428 for (i = 0; i < len; i++)
432 fputs_filtered (", ", stream);
435 c_print_type (TYPE_FIELD_TYPE (type, i), "", stream, -1, 0);
437 fprintf_filtered (stream, ")");
439 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
443 case TYPE_CODE_UNDEF:
444 case TYPE_CODE_STRUCT:
445 case TYPE_CODE_UNION:
450 case TYPE_CODE_ERROR:
454 case TYPE_CODE_RANGE:
455 case TYPE_CODE_STRING:
456 case TYPE_CODE_BITSTRING:
457 case TYPE_CODE_COMPLEX:
458 case TYPE_CODE_TYPEDEF:
459 /* These types do not need a suffix. They are listed so that
460 gcc -Wall will report types that may not have been considered. */
465 /* Print the name of the type (or the ultimate pointer target,
466 function value or array element), or the description of a
469 SHOW positive means print details about the type (e.g. enum values),
470 and print structure elements passing SHOW - 1 for show.
471 SHOW negative means just print the type name or struct tag if there is one.
472 If there is no name, print something sensible but concise like
474 SHOW zero means just print the type name or struct tag if there is one.
475 If there is no name, print something sensible but not as concise like
476 "struct {int x; int y;}".
478 LEVEL is the number of spaces to indent by.
479 We increase it for some recursive calls. */
482 c_type_print_base (type, stream, show, level)
490 register int lastval;
492 char *demangled_name;
493 enum {s_none, s_public, s_private, s_protected} section_type;
499 fputs_filtered ("<type unknown>", stream);
503 /* When SHOW is zero or less, and there is a valid type name, then always
504 just print the type name directly from the type. */
505 /* If we have "typedef struct foo {. . .} bar;" do we want to print it
506 as "struct foo" or as "bar"? Pick the latter, because C++ folk tend
507 to expect things like "class5 *foo" rather than "struct class5 *foo". */
510 && TYPE_NAME (type) != NULL)
512 fputs_filtered (TYPE_NAME (type), stream);
516 CHECK_TYPEDEF (type);
518 switch (TYPE_CODE (type))
520 case TYPE_CODE_TYPEDEF:
521 case TYPE_CODE_ARRAY:
523 case TYPE_CODE_MEMBER:
526 case TYPE_CODE_METHOD:
527 c_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
530 case TYPE_CODE_STRUCT:
531 if (HAVE_CPLUS_STRUCT (type))
533 fprintf_filtered (stream, "class ");
537 fprintf_filtered (stream, "struct ");
541 case TYPE_CODE_UNION:
542 fprintf_filtered (stream, "union ");
545 if (TYPE_TAG_NAME (type) != NULL)
547 fputs_filtered (TYPE_TAG_NAME (type), stream);
549 fputs_filtered (" ", stream);
554 /* If we just printed a tag name, no need to print anything else. */
555 if (TYPE_TAG_NAME (type) == NULL)
556 fprintf_filtered (stream, "{...}");
558 else if (show > 0 || TYPE_TAG_NAME (type) == NULL)
560 cp_type_print_derivation_info (stream, type);
562 fprintf_filtered (stream, "{\n");
563 if ((TYPE_NFIELDS (type) == 0) && (TYPE_NFN_FIELDS (type) == 0))
565 if (TYPE_FLAGS (type) & TYPE_FLAG_STUB)
566 fprintfi_filtered (level + 4, stream, "<incomplete type>\n");
568 fprintfi_filtered (level + 4, stream, "<no data fields>\n");
571 /* Start off with no specific section type, so we can print
572 one for the first field we find, and use that section type
573 thereafter until we find another type. */
575 section_type = s_none;
577 /* If there is a base class for this type,
578 do not print the field that it occupies. */
580 len = TYPE_NFIELDS (type);
581 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
584 /* Don't print out virtual function table. */
585 if ((TYPE_FIELD_NAME (type, i))[5] == CPLUS_MARKER &&
586 !strncmp (TYPE_FIELD_NAME (type, i), "_vptr", 5))
589 /* If this is a C++ class we can print the various C++ section
592 if (HAVE_CPLUS_STRUCT (type))
594 if (TYPE_FIELD_PROTECTED (type, i))
596 if (section_type != s_protected)
598 section_type = s_protected;
599 fprintfi_filtered (level + 2, stream,
603 else if (TYPE_FIELD_PRIVATE (type, i))
605 if (section_type != s_private)
607 section_type = s_private;
608 fprintfi_filtered (level + 2, stream, "private:\n");
613 if (section_type != s_public)
615 section_type = s_public;
616 fprintfi_filtered (level + 2, stream, "public:\n");
621 print_spaces_filtered (level + 4, stream);
622 if (TYPE_FIELD_STATIC (type, i))
624 fprintf_filtered (stream, "static ");
626 c_print_type (TYPE_FIELD_TYPE (type, i),
627 TYPE_FIELD_NAME (type, i),
628 stream, show - 1, level + 4);
629 if (!TYPE_FIELD_STATIC (type, i)
630 && TYPE_FIELD_PACKED (type, i))
632 /* It is a bitfield. This code does not attempt
633 to look at the bitpos and reconstruct filler,
634 unnamed fields. This would lead to misleading
635 results if the compiler does not put out fields
636 for such things (I don't know what it does). */
637 fprintf_filtered (stream, " : %d",
638 TYPE_FIELD_BITSIZE (type, i));
640 fprintf_filtered (stream, ";\n");
643 /* If there are both fields and methods, put a space between. */
644 len = TYPE_NFN_FIELDS (type);
645 if (len && section_type != s_none)
646 fprintf_filtered (stream, "\n");
648 /* C++: print out the methods */
650 for (i = 0; i < len; i++)
652 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
653 int j, len2 = TYPE_FN_FIELDLIST_LENGTH (type, i);
654 char *method_name = TYPE_FN_FIELDLIST_NAME (type, i);
655 char *name = type_name_no_tag (type);
656 int is_constructor = name && STREQ(method_name, name);
657 for (j = 0; j < len2; j++)
659 char *physname = TYPE_FN_FIELD_PHYSNAME (f, j);
660 int is_full_physname_constructor =
661 ((physname[0]=='_' && physname[1]=='_' &&
662 (isdigit(physname[2])
664 || physname[2]=='t'))
665 || (strncmp(physname, "__ct__", 6) == 0));
668 if (TYPE_FN_FIELD_PROTECTED (f, j))
670 if (section_type != s_protected)
672 section_type = s_protected;
673 fprintfi_filtered (level + 2, stream,
677 else if (TYPE_FN_FIELD_PRIVATE (f, j))
679 if (section_type != s_private)
681 section_type = s_private;
682 fprintfi_filtered (level + 2, stream, "private:\n");
687 if (section_type != s_public)
689 section_type = s_public;
690 fprintfi_filtered (level + 2, stream, "public:\n");
694 print_spaces_filtered (level + 4, stream);
695 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
696 fprintf_filtered (stream, "virtual ");
697 else if (TYPE_FN_FIELD_STATIC_P (f, j))
698 fprintf_filtered (stream, "static ");
699 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)) == 0)
701 /* Keep GDB from crashing here. */
702 fprintf_unfiltered (stream, "<undefined type> %s;\n",
703 TYPE_FN_FIELD_PHYSNAME (f, j));
706 else if (!is_constructor && !is_full_physname_constructor)
708 type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)),
710 fputs_filtered (" ", stream);
712 if (TYPE_FN_FIELD_STUB (f, j))
714 /* Build something we can demangle. */
715 mangled_name = gdb_mangle_name (type, i, j);
717 cplus_demangle (mangled_name,
718 DMGL_ANSI | DMGL_PARAMS);
719 if (demangled_name == NULL)
720 fprintf_filtered (stream, "<badly mangled name %s>",
724 char *demangled_no_class =
725 strchr (demangled_name, ':');
727 if (demangled_no_class == NULL)
728 demangled_no_class = demangled_name;
731 if (*++demangled_no_class == ':')
732 ++demangled_no_class;
734 fputs_filtered (demangled_no_class, stream);
735 free (demangled_name);
739 else if (TYPE_FN_FIELD_PHYSNAME (f, j)[0] == '_'
740 && TYPE_FN_FIELD_PHYSNAME (f, j)[1] == CPLUS_MARKER)
741 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f, j) + 1,
742 "~", method_name, 0, stream);
744 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f, j), "",
746 TYPE_FN_FIELD_STATIC_P (f, j),
749 fprintf_filtered (stream, ";\n");
753 fprintfi_filtered (level, stream, "}");
758 fprintf_filtered (stream, "enum ");
759 if (TYPE_TAG_NAME (type) != NULL)
761 fputs_filtered (TYPE_TAG_NAME (type), stream);
763 fputs_filtered (" ", stream);
769 /* If we just printed a tag name, no need to print anything else. */
770 if (TYPE_TAG_NAME (type) == NULL)
771 fprintf_filtered (stream, "{...}");
773 else if (show > 0 || TYPE_TAG_NAME (type) == NULL)
775 fprintf_filtered (stream, "{");
776 len = TYPE_NFIELDS (type);
778 for (i = 0; i < len; i++)
781 if (i) fprintf_filtered (stream, ", ");
783 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
784 if (lastval != TYPE_FIELD_BITPOS (type, i))
786 fprintf_filtered (stream, " = %d", TYPE_FIELD_BITPOS (type, i));
787 lastval = TYPE_FIELD_BITPOS (type, i);
791 fprintf_filtered (stream, "}");
796 fprintf_filtered (stream, "void");
799 case TYPE_CODE_UNDEF:
800 fprintf_filtered (stream, "struct <unknown>");
803 case TYPE_CODE_ERROR:
804 fprintf_filtered (stream, "<unknown type>");
807 case TYPE_CODE_RANGE:
808 /* This should not occur */
809 fprintf_filtered (stream, "<range type>");
813 /* Handle types not explicitly handled by the other cases,
814 such as fundamental types. For these, just print whatever
815 the type name is, as recorded in the type itself. If there
816 is no type name, then complain. */
817 if (TYPE_NAME (type) != NULL)
819 fputs_filtered (TYPE_NAME (type), stream);
823 /* At least for dump_symtab, it is important that this not be
825 fprintf_filtered (stream, "<invalid type code %d>",