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 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 /* If TYPE is a derived type, then print out derivation information.
149 Print only the actual base classes of this type, not the base classes
150 of the base classes. I.E. for the derivation hierarchy:
153 class B : public A {int b; };
154 class C : public B {int c; };
156 Print the type of class C as:
162 Not as the following (like gdb used to), which is not legal C++ syntax for
163 derived types and may be confused with the multiple inheritance form:
165 class C : public B : public A {
169 In general, gdb should try to print the types as closely as possible to
170 the form that they appear in the source code. */
173 cp_type_print_derivation_info (stream, type)
180 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
182 fputs_filtered (i == 0 ? ": " : ", ", stream);
183 fprintf_filtered (stream, "%s%s ",
184 BASETYPE_VIA_PUBLIC (type, i) ? "public" : "private",
185 BASETYPE_VIA_VIRTUAL(type, i) ? " virtual" : "");
186 name = type_name_no_tag (TYPE_BASECLASS (type, i));
187 fprintf_filtered (stream, "%s", name ? name : "(null)");
191 fputs_filtered (" ", stream);
195 /* Print any asterisks or open-parentheses needed before the
196 variable name (to describe its type).
198 On outermost call, pass 0 for PASSED_A_PTR.
199 On outermost call, SHOW > 0 means should ignore
200 any typename for TYPE and show its details.
201 SHOW is always zero on recursive calls. */
204 c_type_print_varspec_prefix (type, stream, show, passed_a_ptr)
214 if (TYPE_NAME (type) && show <= 0)
219 switch (TYPE_CODE (type))
222 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1);
223 fprintf_filtered (stream, "*");
226 case TYPE_CODE_MEMBER:
228 fprintf_filtered (stream, "(");
229 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
230 fprintf_filtered (stream, " ");
231 name = type_name_no_tag (TYPE_DOMAIN_TYPE (type));
233 fputs_filtered (name, stream);
235 c_type_print_base (TYPE_DOMAIN_TYPE (type), stream, 0, passed_a_ptr);
236 fprintf_filtered (stream, "::");
239 case TYPE_CODE_METHOD:
241 fprintf_filtered (stream, "(");
242 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
245 fprintf_filtered (stream, " ");
246 c_type_print_base (TYPE_DOMAIN_TYPE (type), stream, 0, passed_a_ptr);
247 fprintf_filtered (stream, "::");
252 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1);
253 fprintf_filtered (stream, "&");
257 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
259 fprintf_filtered (stream, "(");
262 case TYPE_CODE_ARRAY:
263 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
265 fprintf_filtered (stream, "(");
268 case TYPE_CODE_UNDEF:
269 case TYPE_CODE_STRUCT:
270 case TYPE_CODE_UNION:
275 case TYPE_CODE_ERROR:
279 case TYPE_CODE_RANGE:
280 case TYPE_CODE_STRING:
281 case TYPE_CODE_BITSTRING:
282 case TYPE_CODE_COMPLEX:
283 case TYPE_CODE_TYPEDEF:
284 /* These types need no prefix. They are listed here so that
285 gcc -Wall will reveal any types that haven't been handled. */
291 c_type_print_args (type, stream)
298 fprintf_filtered (stream, "(");
299 args = TYPE_ARG_TYPES (type);
304 fprintf_filtered (stream, "...");
309 args[i] != NULL && args[i]->code != TYPE_CODE_VOID;
312 c_print_type (args[i], "", stream, -1, 0);
313 if (args[i+1] == NULL)
315 fprintf_filtered (stream, "...");
317 else if (args[i+1]->code != TYPE_CODE_VOID)
319 fprintf_filtered (stream, ",");
325 fprintf_filtered (stream, ")");
328 /* Print any array sizes, function arguments or close parentheses
329 needed after the variable name (to describe its type).
330 Args work like c_type_print_varspec_prefix. */
333 c_type_print_varspec_suffix (type, stream, show, passed_a_ptr, demangled_args)
343 if (TYPE_NAME (type) && show <= 0)
348 switch (TYPE_CODE (type))
350 case TYPE_CODE_ARRAY:
352 fprintf_filtered (stream, ")");
354 fprintf_filtered (stream, "[");
355 if (TYPE_LENGTH (type) >= 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0
356 && TYPE_ARRAY_UPPER_BOUND_TYPE(type) != BOUND_CANNOT_BE_DETERMINED)
357 fprintf_filtered (stream, "%d",
359 / TYPE_LENGTH (TYPE_TARGET_TYPE (type))));
360 fprintf_filtered (stream, "]");
362 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0);
365 case TYPE_CODE_MEMBER:
367 fprintf_filtered (stream, ")");
368 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0);
371 case TYPE_CODE_METHOD:
373 fprintf_filtered (stream, ")");
374 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0);
377 c_type_print_args (type, stream);
383 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 1, 0);
388 fprintf_filtered (stream, ")");
390 { int i, len = TYPE_NFIELDS (type);
391 fprintf_filtered (stream, "(");
392 for (i = 0; i < len; i++)
396 fputs_filtered (", ", stream);
399 c_print_type (TYPE_FIELD_TYPE (type, i), "", stream, -1, 0);
401 fprintf_filtered (stream, ")");
403 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
407 case TYPE_CODE_UNDEF:
408 case TYPE_CODE_STRUCT:
409 case TYPE_CODE_UNION:
414 case TYPE_CODE_ERROR:
418 case TYPE_CODE_RANGE:
419 case TYPE_CODE_STRING:
420 case TYPE_CODE_BITSTRING:
421 case TYPE_CODE_COMPLEX:
422 case TYPE_CODE_TYPEDEF:
423 /* These types do not need a suffix. They are listed so that
424 gcc -Wall will report types that may not have been considered. */
429 /* Print the name of the type (or the ultimate pointer target,
430 function value or array element), or the description of a
433 SHOW positive means print details about the type (e.g. enum values),
434 and print structure elements passing SHOW - 1 for show.
435 SHOW negative means just print the type name or struct tag if there is one.
436 If there is no name, print something sensible but concise like
438 SHOW zero means just print the type name or struct tag if there is one.
439 If there is no name, print something sensible but not as concise like
440 "struct {int x; int y;}".
442 LEVEL is the number of spaces to indent by.
443 We increase it for some recursive calls. */
446 c_type_print_base (type, stream, show, level)
454 register int lastval;
456 char *demangled_name;
457 enum {s_none, s_public, s_private, s_protected} section_type;
463 fputs_filtered ("<type unknown>", stream);
467 /* When SHOW is zero or less, and there is a valid type name, then always
468 just print the type name directly from the type. */
469 /* If we have "typedef struct foo {. . .} bar;" do we want to print it
470 as "struct foo" or as "bar"? Pick the latter, because C++ folk tend
471 to expect things like "class5 *foo" rather than "struct class5 *foo". */
474 && TYPE_NAME (type) != NULL)
476 fputs_filtered (TYPE_NAME (type), stream);
480 CHECK_TYPEDEF (type);
482 switch (TYPE_CODE (type))
484 case TYPE_CODE_TYPEDEF:
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 if (HAVE_CPLUS_STRUCT (type))
497 fprintf_filtered (stream, "class ");
501 fprintf_filtered (stream, "struct ");
505 case TYPE_CODE_UNION:
506 fprintf_filtered (stream, "union ");
509 if (TYPE_TAG_NAME (type) != NULL)
511 fputs_filtered (TYPE_TAG_NAME (type), stream);
513 fputs_filtered (" ", stream);
518 /* If we just printed a tag name, no need to print anything else. */
519 if (TYPE_TAG_NAME (type) == NULL)
520 fprintf_filtered (stream, "{...}");
522 else if (show > 0 || TYPE_TAG_NAME (type) == NULL)
524 cp_type_print_derivation_info (stream, type);
526 fprintf_filtered (stream, "{\n");
527 if ((TYPE_NFIELDS (type) == 0) && (TYPE_NFN_FIELDS (type) == 0))
529 if (TYPE_FLAGS (type) & TYPE_FLAG_STUB)
530 fprintfi_filtered (level + 4, stream, "<incomplete type>\n");
532 fprintfi_filtered (level + 4, stream, "<no data fields>\n");
535 /* Start off with no specific section type, so we can print
536 one for the first field we find, and use that section type
537 thereafter until we find another type. */
539 section_type = s_none;
541 /* If there is a base class for this type,
542 do not print the field that it occupies. */
544 len = TYPE_NFIELDS (type);
545 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
548 /* Don't print out virtual function table. */
549 if (STREQN (TYPE_FIELD_NAME (type, i), "_vptr", 5)
550 && is_cplus_marker ((TYPE_FIELD_NAME (type, i))[5]))
553 /* If this is a C++ class we can print the various C++ section
556 if (HAVE_CPLUS_STRUCT (type))
558 if (TYPE_FIELD_PROTECTED (type, i))
560 if (section_type != s_protected)
562 section_type = s_protected;
563 fprintfi_filtered (level + 2, stream,
567 else if (TYPE_FIELD_PRIVATE (type, i))
569 if (section_type != s_private)
571 section_type = s_private;
572 fprintfi_filtered (level + 2, stream, "private:\n");
577 if (section_type != s_public)
579 section_type = s_public;
580 fprintfi_filtered (level + 2, stream, "public:\n");
585 print_spaces_filtered (level + 4, stream);
586 if (TYPE_FIELD_STATIC (type, i))
588 fprintf_filtered (stream, "static ");
590 c_print_type (TYPE_FIELD_TYPE (type, i),
591 TYPE_FIELD_NAME (type, i),
592 stream, show - 1, level + 4);
593 if (!TYPE_FIELD_STATIC (type, i)
594 && TYPE_FIELD_PACKED (type, i))
596 /* It is a bitfield. This code does not attempt
597 to look at the bitpos and reconstruct filler,
598 unnamed fields. This would lead to misleading
599 results if the compiler does not put out fields
600 for such things (I don't know what it does). */
601 fprintf_filtered (stream, " : %d",
602 TYPE_FIELD_BITSIZE (type, i));
604 fprintf_filtered (stream, ";\n");
607 /* If there are both fields and methods, put a space between. */
608 len = TYPE_NFN_FIELDS (type);
609 if (len && section_type != s_none)
610 fprintf_filtered (stream, "\n");
612 /* C++: print out the methods */
614 for (i = 0; i < len; i++)
616 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
617 int j, len2 = TYPE_FN_FIELDLIST_LENGTH (type, i);
618 char *method_name = TYPE_FN_FIELDLIST_NAME (type, i);
619 char *name = type_name_no_tag (type);
620 int is_constructor = name && STREQ(method_name, name);
621 for (j = 0; j < len2; j++)
623 char *physname = TYPE_FN_FIELD_PHYSNAME (f, j);
624 int is_full_physname_constructor =
625 ((physname[0] == '_' && physname[1] == '_'
626 && strchr ("0123456789Qt", physname[2]))
627 || STREQN (physname, "__ct__", 6)
628 || DESTRUCTOR_PREFIX_P (physname)
629 || STREQN (physname, "__dt__", 6));
632 if (TYPE_FN_FIELD_PROTECTED (f, j))
634 if (section_type != s_protected)
636 section_type = s_protected;
637 fprintfi_filtered (level + 2, stream,
641 else if (TYPE_FN_FIELD_PRIVATE (f, j))
643 if (section_type != s_private)
645 section_type = s_private;
646 fprintfi_filtered (level + 2, stream, "private:\n");
651 if (section_type != s_public)
653 section_type = s_public;
654 fprintfi_filtered (level + 2, stream, "public:\n");
658 print_spaces_filtered (level + 4, stream);
659 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
660 fprintf_filtered (stream, "virtual ");
661 else if (TYPE_FN_FIELD_STATIC_P (f, j))
662 fprintf_filtered (stream, "static ");
663 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)) == 0)
665 /* Keep GDB from crashing here. */
666 fprintf_filtered (stream, "<undefined type> %s;\n",
667 TYPE_FN_FIELD_PHYSNAME (f, j));
670 else if (!is_constructor && !is_full_physname_constructor)
672 type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)),
674 fputs_filtered (" ", stream);
676 if (TYPE_FN_FIELD_STUB (f, j))
677 /* Build something we can demangle. */
678 mangled_name = gdb_mangle_name (type, i, j);
680 mangled_name = TYPE_FN_FIELD_PHYSNAME (f, j);
683 cplus_demangle (mangled_name,
684 DMGL_ANSI | DMGL_PARAMS);
685 if (demangled_name == NULL)
686 fprintf_filtered (stream, "<badly mangled name '%s'>",
690 char *demangled_no_class =
691 strchr (demangled_name, ':');
693 if (demangled_no_class == NULL)
694 demangled_no_class = demangled_name;
697 if (*++demangled_no_class == ':')
698 ++demangled_no_class;
700 fputs_filtered (demangled_no_class, stream);
701 free (demangled_name);
704 if (TYPE_FN_FIELD_STUB (f, j))
707 fprintf_filtered (stream, ";\n");
711 fprintfi_filtered (level, stream, "}");
716 fprintf_filtered (stream, "enum ");
717 if (TYPE_TAG_NAME (type) != NULL)
719 fputs_filtered (TYPE_TAG_NAME (type), stream);
721 fputs_filtered (" ", stream);
727 /* If we just printed a tag name, no need to print anything else. */
728 if (TYPE_TAG_NAME (type) == NULL)
729 fprintf_filtered (stream, "{...}");
731 else if (show > 0 || TYPE_TAG_NAME (type) == NULL)
733 fprintf_filtered (stream, "{");
734 len = TYPE_NFIELDS (type);
736 for (i = 0; i < len; i++)
739 if (i) fprintf_filtered (stream, ", ");
741 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
742 if (lastval != TYPE_FIELD_BITPOS (type, i))
744 fprintf_filtered (stream, " = %d", TYPE_FIELD_BITPOS (type, i));
745 lastval = TYPE_FIELD_BITPOS (type, i);
749 fprintf_filtered (stream, "}");
754 fprintf_filtered (stream, "void");
757 case TYPE_CODE_UNDEF:
758 fprintf_filtered (stream, "struct <unknown>");
761 case TYPE_CODE_ERROR:
762 fprintf_filtered (stream, "<unknown type>");
765 case TYPE_CODE_RANGE:
766 /* This should not occur */
767 fprintf_filtered (stream, "<range type>");
771 /* Handle types not explicitly handled by the other cases,
772 such as fundamental types. For these, just print whatever
773 the type name is, as recorded in the type itself. If there
774 is no type name, then complain. */
775 if (TYPE_NAME (type) != NULL)
777 fputs_filtered (TYPE_NAME (type), stream);
781 /* At least for dump_symtab, it is important that this not be
783 fprintf_filtered (stream, "<invalid type code %d>",