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);
90 /* start-sanitize-chill */
93 error ("Missing Chill support in function c_typedef_print."); /*FIXME*/
95 /* end-sanitize-chill */
97 error("Language not supported.");
99 fprintf_filtered(stream, ";\n");
103 /* LEVEL is the depth to indent lines by. */
106 c_print_type (type, varstring, stream, show, level)
113 register enum type_code code;
116 c_type_print_base (type, stream, show, level);
117 code = TYPE_CODE (type);
118 if ((varstring != NULL && *varstring != '\0')
120 /* Need a space if going to print stars or brackets;
121 but not if we will print just a type name. */
122 ((show > 0 || TYPE_NAME (type) == 0)
124 (code == TYPE_CODE_PTR || code == TYPE_CODE_FUNC
125 || code == TYPE_CODE_METHOD
126 || code == TYPE_CODE_ARRAY
127 || code == TYPE_CODE_MEMBER
128 || code == TYPE_CODE_REF)))
129 fputs_filtered (" ", stream);
130 c_type_print_varspec_prefix (type, stream, show, 0);
132 fputs_filtered (varstring, stream);
134 /* For demangled function names, we have the arglist as part of the name,
135 so don't print an additional pair of ()'s */
137 demangled_args = varstring[strlen(varstring) - 1] == ')';
138 c_type_print_varspec_suffix (type, stream, show, 0, demangled_args);
142 /* Print the C++ method arguments ARGS to the file STREAM. */
145 cp_type_print_method_args (args, prefix, varstring, staticp, stream)
154 fputs_demangled (prefix, stream, DMGL_ANSI | DMGL_PARAMS);
155 fputs_demangled (varstring, stream, DMGL_ANSI | DMGL_PARAMS);
156 fputs_filtered (" (", stream);
157 if (args && args[!staticp] && args[!staticp]->code != TYPE_CODE_VOID)
159 i = !staticp; /* skip the class variable */
162 type_print (args[i++], "", stream, 0);
165 fprintf_filtered (stream, " ...");
168 else if (args[i]->code != TYPE_CODE_VOID)
170 fprintf_filtered (stream, ", ");
175 fprintf_filtered (stream, ")");
178 /* If TYPE is a derived type, then print out derivation information.
179 Print only the actual base classes of this type, not the base classes
180 of the base classes. I.E. for the derivation hierarchy:
183 class B : public A {int b; };
184 class C : public B {int c; };
186 Print the type of class C as:
192 Not as the following (like gdb used to), which is not legal C++ syntax for
193 derived types and may be confused with the multiple inheritance form:
195 class C : public B : public A {
199 In general, gdb should try to print the types as closely as possible to
200 the form that they appear in the source code. */
203 cp_type_print_derivation_info (stream, type)
210 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
212 fputs_filtered (i == 0 ? ": " : ", ", stream);
213 fprintf_filtered (stream, "%s%s ",
214 BASETYPE_VIA_PUBLIC (type, i) ? "public" : "private",
215 BASETYPE_VIA_VIRTUAL(type, i) ? " virtual" : "");
216 name = type_name_no_tag (TYPE_BASECLASS (type, i));
217 fprintf_filtered (stream, "%s", name ? name : "(null)");
221 fputs_filtered (" ", stream);
225 /* Print any asterisks or open-parentheses needed before the
226 variable name (to describe its type).
228 On outermost call, pass 0 for PASSED_A_PTR.
229 On outermost call, SHOW > 0 means should ignore
230 any typename for TYPE and show its details.
231 SHOW is always zero on recursive calls. */
234 c_type_print_varspec_prefix (type, stream, show, passed_a_ptr)
244 if (TYPE_NAME (type) && show <= 0)
249 switch (TYPE_CODE (type))
252 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1);
253 fprintf_filtered (stream, "*");
256 case TYPE_CODE_MEMBER:
258 fprintf_filtered (stream, "(");
259 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
260 fprintf_filtered (stream, " ");
261 name = type_name_no_tag (TYPE_DOMAIN_TYPE (type));
263 fputs_filtered (name, stream);
265 c_type_print_base (TYPE_DOMAIN_TYPE (type), stream, 0, passed_a_ptr);
266 fprintf_filtered (stream, "::");
269 case TYPE_CODE_METHOD:
271 fprintf (stream, "(");
272 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
275 fprintf_filtered (stream, " ");
276 c_type_print_base (TYPE_DOMAIN_TYPE (type), stream, 0, passed_a_ptr);
277 fprintf_filtered (stream, "::");
282 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1);
283 fprintf_filtered (stream, "&");
287 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
289 fprintf_filtered (stream, "(");
292 case TYPE_CODE_ARRAY:
293 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
295 fprintf_filtered (stream, "(");
298 case TYPE_CODE_UNDEF:
299 case TYPE_CODE_STRUCT:
300 case TYPE_CODE_UNION:
305 case TYPE_CODE_ERROR:
309 case TYPE_CODE_RANGE:
310 case TYPE_CODE_STRING:
311 /* These types need no prefix. They are listed here so that
312 gcc -Wall will reveal any types that haven't been handled. */
318 c_type_print_args (type, stream)
325 fprintf_filtered (stream, "(");
326 args = TYPE_ARG_TYPES (type);
331 fprintf_filtered (stream, "...");
336 args[i] != NULL && args[i]->code != TYPE_CODE_VOID;
339 c_print_type (args[i], "", stream, -1, 0);
340 if (args[i+1] == NULL)
342 fprintf_filtered (stream, "...");
344 else if (args[i+1]->code != TYPE_CODE_VOID)
346 fprintf_filtered (stream, ",");
352 fprintf_filtered (stream, ")");
355 /* Print any array sizes, function arguments or close parentheses
356 needed after the variable name (to describe its type).
357 Args work like c_type_print_varspec_prefix. */
360 c_type_print_varspec_suffix (type, stream, show, passed_a_ptr, demangled_args)
370 if (TYPE_NAME (type) && show <= 0)
375 switch (TYPE_CODE (type))
377 case TYPE_CODE_ARRAY:
379 fprintf_filtered (stream, ")");
381 fprintf_filtered (stream, "[");
382 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
383 fprintf_filtered (stream, "%d",
385 / TYPE_LENGTH (TYPE_TARGET_TYPE (type))));
386 fprintf_filtered (stream, "]");
388 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0);
391 case TYPE_CODE_MEMBER:
393 fprintf_filtered (stream, ")");
394 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0);
397 case TYPE_CODE_METHOD:
399 fprintf_filtered (stream, ")");
400 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0);
403 c_type_print_args (type, stream);
409 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 1, 0);
413 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
416 fprintf_filtered (stream, ")");
418 fprintf_filtered (stream, "()");
421 case TYPE_CODE_UNDEF:
422 case TYPE_CODE_STRUCT:
423 case TYPE_CODE_UNION:
428 case TYPE_CODE_ERROR:
432 case TYPE_CODE_RANGE:
433 case TYPE_CODE_STRING:
434 /* These types do not need a suffix. They are listed so that
435 gcc -Wall will report types that may not have been considered. */
440 /* Print the name of the type (or the ultimate pointer target,
441 function value or array element), or the description of a
444 SHOW nonzero means don't print this type as just its name;
445 show its real definition even if it has a name.
446 SHOW zero means print just typename or struct tag if there is one
447 SHOW negative means abbreviate structure elements.
448 SHOW is decremented for printing of structure elements.
450 LEVEL is the depth to indent by.
451 We increase it for some recursive calls. */
454 c_type_print_base (type, stream, show, level)
463 register int lastval;
465 char *demangled_name;
466 enum {s_none, s_public, s_private, s_protected} section_type;
472 fputs_filtered ("<type unknown>", stream);
476 /* When SHOW is zero or less, and there is a valid type name, then always
477 just print the type name directly from the type. */
479 if ((show <= 0) && (TYPE_NAME (type) != NULL))
481 fputs_filtered (TYPE_NAME (type), stream);
485 switch (TYPE_CODE (type))
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 fprintf_filtered (stream,
498 HAVE_CPLUS_STRUCT (type) ? "class " : "struct ");
501 case TYPE_CODE_UNION:
502 fprintf_filtered (stream, "union ");
504 if ((name = type_name_no_tag (type)) != NULL)
506 fputs_filtered (name, stream);
507 fputs_filtered (" ", stream);
511 fprintf_filtered (stream, "{...}");
514 check_stub_type (type);
516 cp_type_print_derivation_info (stream, type);
518 fprintf_filtered (stream, "{\n");
519 if ((TYPE_NFIELDS (type) == 0) && (TYPE_NFN_FIELDS (type) == 0))
521 if (TYPE_FLAGS (type) & TYPE_FLAG_STUB)
522 fprintfi_filtered (level + 4, stream, "<incomplete type>\n");
524 fprintfi_filtered (level + 4, stream, "<no data fields>\n");
527 /* Start off with no specific section type, so we can print
528 one for the first field we find, and use that section type
529 thereafter until we find another type. */
531 section_type = s_none;
533 /* If there is a base class for this type,
534 do not print the field that it occupies. */
536 len = TYPE_NFIELDS (type);
537 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
540 /* Don't print out virtual function table. */
541 if ((TYPE_FIELD_NAME (type, i))[5] == CPLUS_MARKER &&
542 !strncmp (TYPE_FIELD_NAME (type, i), "_vptr", 5))
545 /* If this is a C++ class we can print the various C++ section
548 if (HAVE_CPLUS_STRUCT (type))
550 if (TYPE_FIELD_PROTECTED (type, i))
552 if (section_type != s_protected)
554 section_type = s_protected;
555 fprintfi_filtered (level + 2, stream,
559 else if (TYPE_FIELD_PRIVATE (type, i))
561 if (section_type != s_private)
563 section_type = s_private;
564 fprintfi_filtered (level + 2, stream, "private:\n");
569 if (section_type != s_public)
571 section_type = s_public;
572 fprintfi_filtered (level + 2, stream, "public:\n");
577 print_spaces_filtered (level + 4, stream);
578 if (TYPE_FIELD_STATIC (type, i))
580 fprintf_filtered (stream, "static ");
582 c_print_type (TYPE_FIELD_TYPE (type, i),
583 TYPE_FIELD_NAME (type, i),
584 stream, show - 1, level + 4);
585 if (!TYPE_FIELD_STATIC (type, i)
586 && TYPE_FIELD_PACKED (type, i))
588 /* It is a bitfield. This code does not attempt
589 to look at the bitpos and reconstruct filler,
590 unnamed fields. This would lead to misleading
591 results if the compiler does not put out fields
592 for such things (I don't know what it does). */
593 fprintf_filtered (stream, " : %d",
594 TYPE_FIELD_BITSIZE (type, i));
596 fprintf_filtered (stream, ";\n");
599 /* If there are both fields and methods, put a space between. */
600 len = TYPE_NFN_FIELDS (type);
601 if (len && section_type != s_none)
602 fprintf_filtered (stream, "\n");
604 /* C++: print out the methods */
606 for (i = 0; i < len; i++)
608 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
609 int j, len2 = TYPE_FN_FIELDLIST_LENGTH (type, i);
610 char *method_name = TYPE_FN_FIELDLIST_NAME (type, i);
611 int is_constructor = name && STREQ(method_name, name);
612 for (j = 0; j < len2; j++)
615 if (TYPE_FN_FIELD_PROTECTED (f, j))
617 if (section_type != s_protected)
619 section_type = s_protected;
620 fprintfi_filtered (level + 2, stream,
624 else if (TYPE_FN_FIELD_PRIVATE (f, j))
626 if (section_type != s_private)
628 section_type = s_private;
629 fprintfi_filtered (level + 2, stream, "private:\n");
634 if (section_type != s_public)
636 section_type = s_public;
637 fprintfi_filtered (level + 2, stream, "public:\n");
641 print_spaces_filtered (level + 4, stream);
642 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
643 fprintf_filtered (stream, "virtual ");
644 else if (TYPE_FN_FIELD_STATIC_P (f, j))
645 fprintf_filtered (stream, "static ");
646 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)) == 0)
648 /* Keep GDB from crashing here. */
649 fprintf (stream, "<undefined type> %s;\n",
650 TYPE_FN_FIELD_PHYSNAME (f, j));
653 else if (!is_constructor)
655 type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)),
657 fputs_filtered (" ", stream);
659 if (TYPE_FN_FIELD_STUB (f, j))
661 /* Build something we can demangle. */
662 mangled_name = gdb_mangle_name (type, i, j);
664 cplus_demangle (mangled_name,
665 DMGL_ANSI | DMGL_PARAMS);
666 if (demangled_name == NULL)
667 fprintf_filtered (stream, "<badly mangled name %s>",
671 fprintf_filtered (stream, "%s",
672 strchr (demangled_name, ':') + 2);
673 free (demangled_name);
677 else if (TYPE_FN_FIELD_PHYSNAME (f, j)[0] == '_'
678 && TYPE_FN_FIELD_PHYSNAME (f, j)[1] == CPLUS_MARKER)
679 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f, j) + 1,
680 "~", method_name, 0, stream);
682 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f, j), "",
684 TYPE_FN_FIELD_STATIC_P (f, j),
687 fprintf_filtered (stream, ";\n");
691 fprintfi_filtered (level, stream, "}");
696 fprintf_filtered (stream, "enum ");
697 if ((name = type_name_no_tag (type)) != NULL)
699 fputs_filtered (name, stream);
700 fputs_filtered (" ", stream);
704 fprintf_filtered (stream, "{...}");
707 fprintf_filtered (stream, "{");
708 len = TYPE_NFIELDS (type);
710 for (i = 0; i < len; i++)
713 if (i) fprintf_filtered (stream, ", ");
715 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
716 if (lastval != TYPE_FIELD_BITPOS (type, i))
718 fprintf_filtered (stream, " = %d", TYPE_FIELD_BITPOS (type, i));
719 lastval = TYPE_FIELD_BITPOS (type, i);
723 fprintf_filtered (stream, "}");
728 fprintf_filtered (stream, "void");
731 case TYPE_CODE_UNDEF:
732 fprintf_filtered (stream, "struct <unknown>");
735 case TYPE_CODE_ERROR:
736 fprintf_filtered (stream, "<unknown type>");
739 case TYPE_CODE_RANGE:
740 /* This should not occur */
741 fprintf_filtered (stream, "<range type>");
745 /* Handle types not explicitly handled by the other cases,
746 such as fundamental types. For these, just print whatever
747 the type name is, as recorded in the type itself. If there
748 is no type name, then complain. */
749 if (TYPE_NAME (type) != NULL)
751 fputs_filtered (TYPE_NAME (type), stream);
755 error ("Invalid type code (%d) in symbol table.", TYPE_CODE (type));