1 /* Support for printing C and C++ types for GDB, the GNU debugger.
2 Copyright (C) 1986-2017 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 3 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, see <http://www.gnu.org/licenses/>. */
20 #include "gdb_obstack.h"
21 #include "bfd.h" /* Binary File Description. */
24 #include "expression.h"
31 #include "typeprint.h"
33 #include "cp-support.h"
35 static void c_type_print_varspec_prefix (struct type *,
38 const struct type_print_options *);
40 /* Print "const", "volatile", or address space modifiers. */
41 static void c_type_print_modifier (struct type *,
46 /* A callback function for cp_canonicalize_string_full that uses
47 find_typedef_in_hash. */
50 find_typedef_for_canonicalize (struct type *t, void *data)
52 return find_typedef_in_hash ((const struct type_print_options *) data, t);
55 /* Print NAME on STREAM. If the 'raw' field of FLAGS is not set,
56 canonicalize NAME using the local typedefs first. */
59 print_name_maybe_canonical (const char *name,
60 const struct type_print_options *flags,
61 struct ui_file *stream)
66 s = cp_canonicalize_string_full (name,
67 find_typedef_for_canonicalize,
70 fputs_filtered (!s.empty () ? s.c_str () : name, stream);
75 /* LEVEL is the depth to indent lines by. */
78 c_print_type (struct type *type,
79 const char *varstring,
80 struct ui_file *stream,
82 const struct type_print_options *flags)
87 const char *local_name;
90 type = check_typedef (type);
92 local_name = find_typedef_in_hash (flags, type);
93 if (local_name != NULL)
95 fputs_filtered (local_name, stream);
96 if (varstring != NULL && *varstring != '\0')
97 fputs_filtered (" ", stream);
101 c_type_print_base (type, stream, show, level, flags);
102 code = TYPE_CODE (type);
103 if ((varstring != NULL && *varstring != '\0')
104 /* Need a space if going to print stars or brackets;
105 but not if we will print just a type name. */
106 || ((show > 0 || TYPE_NAME (type) == 0)
107 && (code == TYPE_CODE_PTR || code == TYPE_CODE_FUNC
108 || code == TYPE_CODE_METHOD
109 || (code == TYPE_CODE_ARRAY
110 && !TYPE_VECTOR (type))
111 || code == TYPE_CODE_MEMBERPTR
112 || code == TYPE_CODE_METHODPTR
113 || TYPE_IS_REFERENCE (type))))
114 fputs_filtered (" ", stream);
115 need_post_space = (varstring != NULL && strcmp (varstring, "") != 0);
116 c_type_print_varspec_prefix (type, stream, show, 0, need_post_space,
120 if (varstring != NULL)
122 fputs_filtered (varstring, stream);
124 /* For demangled function names, we have the arglist as part of
125 the name, so don't print an additional pair of ()'s. */
126 if (local_name == NULL)
128 demangled_args = strchr (varstring, '(') != NULL;
129 c_type_print_varspec_suffix (type, stream, show,
136 /* Print a typedef using C syntax. TYPE is the underlying type.
137 NEW_SYMBOL is the symbol naming the type. STREAM is the stream on
141 c_print_typedef (struct type *type,
142 struct symbol *new_symbol,
143 struct ui_file *stream)
145 type = check_typedef (type);
146 fprintf_filtered (stream, "typedef ");
147 type_print (type, "", stream, 0);
148 if (TYPE_NAME ((SYMBOL_TYPE (new_symbol))) == 0
149 || strcmp (TYPE_NAME ((SYMBOL_TYPE (new_symbol))),
150 SYMBOL_LINKAGE_NAME (new_symbol)) != 0
151 || TYPE_CODE (SYMBOL_TYPE (new_symbol)) == TYPE_CODE_TYPEDEF)
152 fprintf_filtered (stream, " %s", SYMBOL_PRINT_NAME (new_symbol));
153 fprintf_filtered (stream, ";\n");
156 /* If TYPE is a derived type, then print out derivation information.
157 Print only the actual base classes of this type, not the base
158 classes of the base classes. I.e. for the derivation hierarchy:
161 class B : public A {int b; };
162 class C : public B {int c; };
164 Print the type of class C as:
170 Not as the following (like gdb used to), which is not legal C++
171 syntax for derived types and may be confused with the multiple
174 class C : public B : public A {
178 In general, gdb should try to print the types as closely as
179 possible to the form that they appear in the source code. */
182 cp_type_print_derivation_info (struct ui_file *stream,
184 const struct type_print_options *flags)
189 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
192 fputs_filtered (i == 0 ? ": " : ", ", stream);
193 fprintf_filtered (stream, "%s%s ",
194 BASETYPE_VIA_PUBLIC (type, i)
195 ? "public" : (TYPE_FIELD_PROTECTED (type, i)
196 ? "protected" : "private"),
197 BASETYPE_VIA_VIRTUAL (type, i) ? " virtual" : "");
198 name = type_name_no_tag (TYPE_BASECLASS (type, i));
200 print_name_maybe_canonical (name, flags, stream);
202 fprintf_filtered (stream, "(null)");
206 fputs_filtered (" ", stream);
210 /* Print the C++ method arguments ARGS to the file STREAM. */
213 cp_type_print_method_args (struct type *mtype, const char *prefix,
214 const char *varstring, int staticp,
215 struct ui_file *stream,
216 const struct type_print_options *flags)
218 struct field *args = TYPE_FIELDS (mtype);
219 int nargs = TYPE_NFIELDS (mtype);
220 int varargs = TYPE_VARARGS (mtype);
223 fprintf_symbol_filtered (stream, prefix,
224 language_cplus, DMGL_ANSI);
225 fprintf_symbol_filtered (stream, varstring,
226 language_cplus, DMGL_ANSI);
227 fputs_filtered ("(", stream);
229 /* Skip the class variable. We keep this here to accommodate older
230 compilers and debug formats which may not support artificial
237 struct field arg = args[i++];
239 /* Skip any artificial arguments. */
240 if (FIELD_ARTIFICIAL (arg))
243 c_print_type (arg.type, "", stream, 0, 0, flags);
245 if (i == nargs && varargs)
246 fprintf_filtered (stream, ", ...");
249 fprintf_filtered (stream, ", ");
255 fprintf_filtered (stream, "...");
256 else if (current_language->la_language == language_cplus)
257 fprintf_filtered (stream, "void");
259 fprintf_filtered (stream, ")");
261 /* For non-static methods, read qualifiers from the type of
267 gdb_assert (nargs > 0);
268 gdb_assert (TYPE_CODE (args[0].type) == TYPE_CODE_PTR);
269 domain = TYPE_TARGET_TYPE (args[0].type);
271 if (TYPE_CONST (domain))
272 fprintf_filtered (stream, " const");
274 if (TYPE_VOLATILE (domain))
275 fprintf_filtered (stream, " volatile");
277 if (TYPE_RESTRICT (domain))
278 fprintf_filtered (stream, " restrict");
280 if (TYPE_ATOMIC (domain))
281 fprintf_filtered (stream, " _Atomic");
286 /* Print any asterisks or open-parentheses needed before the
287 variable name (to describe its type).
289 On outermost call, pass 0 for PASSED_A_PTR.
290 On outermost call, SHOW > 0 means should ignore
291 any typename for TYPE and show its details.
292 SHOW is always zero on recursive calls.
294 NEED_POST_SPACE is non-zero when a space will be be needed
295 between a trailing qualifier and a field, variable, or function
299 c_type_print_varspec_prefix (struct type *type,
300 struct ui_file *stream,
301 int show, int passed_a_ptr,
303 const struct type_print_options *flags)
310 if (TYPE_NAME (type) && show <= 0)
315 switch (TYPE_CODE (type))
318 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
319 stream, show, 1, 1, flags);
320 fprintf_filtered (stream, "*");
321 c_type_print_modifier (type, stream, 1, need_post_space);
324 case TYPE_CODE_MEMBERPTR:
325 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
326 stream, show, 0, 0, flags);
327 name = type_name_no_tag (TYPE_SELF_TYPE (type));
329 print_name_maybe_canonical (name, flags, stream);
331 c_type_print_base (TYPE_SELF_TYPE (type),
332 stream, -1, passed_a_ptr, flags);
333 fprintf_filtered (stream, "::*");
336 case TYPE_CODE_METHODPTR:
337 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
338 stream, show, 0, 0, flags);
339 fprintf_filtered (stream, "(");
340 name = type_name_no_tag (TYPE_SELF_TYPE (type));
342 print_name_maybe_canonical (name, flags, stream);
344 c_type_print_base (TYPE_SELF_TYPE (type),
345 stream, -1, passed_a_ptr, flags);
346 fprintf_filtered (stream, "::*");
350 case TYPE_CODE_RVALUE_REF:
351 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
352 stream, show, 1, 0, flags);
353 fprintf_filtered (stream, TYPE_CODE(type) == TYPE_CODE_REF ? "&" : "&&");
354 c_type_print_modifier (type, stream, 1, need_post_space);
357 case TYPE_CODE_METHOD:
359 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
360 stream, show, 0, 0, flags);
362 fprintf_filtered (stream, "(");
365 case TYPE_CODE_ARRAY:
366 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
367 stream, show, 0, 0, flags);
369 fprintf_filtered (stream, "(");
372 case TYPE_CODE_TYPEDEF:
373 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
374 stream, show, passed_a_ptr, 0, flags);
377 case TYPE_CODE_UNDEF:
378 case TYPE_CODE_STRUCT:
379 case TYPE_CODE_UNION:
381 case TYPE_CODE_FLAGS:
385 case TYPE_CODE_ERROR:
389 case TYPE_CODE_RANGE:
390 case TYPE_CODE_STRING:
391 case TYPE_CODE_COMPLEX:
392 case TYPE_CODE_NAMESPACE:
393 case TYPE_CODE_DECFLOAT:
394 /* These types need no prefix. They are listed here so that
395 gcc -Wall will reveal any types that haven't been handled. */
398 error (_("type not handled in c_type_print_varspec_prefix()"));
403 /* Print out "const" and "volatile" attributes,
404 and address space id if present.
405 TYPE is a pointer to the type being printed out.
406 STREAM is the output destination.
407 NEED_PRE_SPACE = 1 indicates an initial white space is needed.
408 NEED_POST_SPACE = 1 indicates a final white space is needed. */
411 c_type_print_modifier (struct type *type, struct ui_file *stream,
412 int need_pre_space, int need_post_space)
414 int did_print_modifier = 0;
415 const char *address_space_id;
417 /* We don't print `const' qualifiers for references --- since all
418 operators affect the thing referenced, not the reference itself,
419 every reference is `const'. */
420 if (TYPE_CONST (type) && !TYPE_IS_REFERENCE (type))
423 fprintf_filtered (stream, " ");
424 fprintf_filtered (stream, "const");
425 did_print_modifier = 1;
428 if (TYPE_VOLATILE (type))
430 if (did_print_modifier || need_pre_space)
431 fprintf_filtered (stream, " ");
432 fprintf_filtered (stream, "volatile");
433 did_print_modifier = 1;
436 if (TYPE_RESTRICT (type))
438 if (did_print_modifier || need_pre_space)
439 fprintf_filtered (stream, " ");
440 fprintf_filtered (stream, "restrict");
441 did_print_modifier = 1;
444 if (TYPE_ATOMIC (type))
446 if (did_print_modifier || need_pre_space)
447 fprintf_filtered (stream, " ");
448 fprintf_filtered (stream, "_Atomic");
449 did_print_modifier = 1;
452 address_space_id = address_space_int_to_name (get_type_arch (type),
453 TYPE_INSTANCE_FLAGS (type));
454 if (address_space_id)
456 if (did_print_modifier || need_pre_space)
457 fprintf_filtered (stream, " ");
458 fprintf_filtered (stream, "@%s", address_space_id);
459 did_print_modifier = 1;
462 if (did_print_modifier && need_post_space)
463 fprintf_filtered (stream, " ");
467 /* Print out the arguments of TYPE, which should have TYPE_CODE_METHOD
468 or TYPE_CODE_FUNC, to STREAM. Artificial arguments, such as "this"
469 in non-static methods, are displayed if LINKAGE_NAME is zero. If
470 LINKAGE_NAME is non-zero and LANGUAGE is language_cplus the topmost
471 parameter types get removed their possible const and volatile qualifiers to
472 match demangled linkage name parameters part of such function type.
473 LANGUAGE is the language in which TYPE was defined. This is a necessary
474 evil since this code is used by the C and C++. */
477 c_type_print_args (struct type *type, struct ui_file *stream,
478 int linkage_name, enum language language,
479 const struct type_print_options *flags)
484 fprintf_filtered (stream, "(");
486 for (i = 0; i < TYPE_NFIELDS (type); i++)
488 struct type *param_type;
490 if (TYPE_FIELD_ARTIFICIAL (type, i) && linkage_name)
495 fprintf_filtered (stream, ", ");
499 param_type = TYPE_FIELD_TYPE (type, i);
501 if (language == language_cplus && linkage_name)
503 /* C++ standard, 13.1 Overloadable declarations, point 3, item:
504 - Parameter declarations that differ only in the presence or
505 absence of const and/or volatile are equivalent.
507 And the const/volatile qualifiers are not present in the mangled
508 names as produced by GCC. */
510 param_type = make_cv_type (0, 0, param_type, NULL);
513 c_print_type (param_type, "", stream, -1, 0, flags);
517 if (printed_any && TYPE_VARARGS (type))
519 /* Print out a trailing ellipsis for varargs functions. Ignore
520 TYPE_VARARGS if the function has no named arguments; that
521 represents unprototyped (K&R style) C functions. */
522 if (printed_any && TYPE_VARARGS (type))
524 fprintf_filtered (stream, ", ");
526 fprintf_filtered (stream, "...");
529 else if (!printed_any
530 && (TYPE_PROTOTYPED (type) || language == language_cplus))
531 fprintf_filtered (stream, "void");
533 fprintf_filtered (stream, ")");
536 /* Return true iff the j'th overloading of the i'th method of TYPE
537 is a type conversion operator, like `operator int () { ... }'.
538 When listing a class's methods, we don't print the return type of
542 is_type_conversion_operator (struct type *type, int i, int j)
544 /* I think the whole idea of recognizing type conversion operators
545 by their name is pretty terrible. But I don't think our present
546 data structure gives us any other way to tell. If you know of
547 some other way, feel free to rewrite this function. */
548 const char *name = TYPE_FN_FIELDLIST_NAME (type, i);
550 if (!startswith (name, CP_OPERATOR_STR))
554 if (! strchr (" \t\f\n\r", *name))
557 while (strchr (" \t\f\n\r", *name))
560 if (!('a' <= *name && *name <= 'z')
561 && !('A' <= *name && *name <= 'Z')
563 /* If this doesn't look like the start of an identifier, then it
564 isn't a type conversion operator. */
566 else if (startswith (name, "new"))
568 else if (startswith (name, "delete"))
571 /* If it doesn't look like new or delete, it's a type conversion
575 /* Is that really the end of the name? */
576 if (('a' <= *name && *name <= 'z')
577 || ('A' <= *name && *name <= 'Z')
578 || ('0' <= *name && *name <= '9')
580 /* No, so the identifier following "operator" must be a type name,
581 and this is a type conversion operator. */
584 /* That was indeed the end of the name, so it was `operator new' or
585 `operator delete', neither of which are type conversion
590 /* Given a C++ qualified identifier QID, strip off the qualifiers,
591 yielding the unqualified name. The return value is a pointer into
594 It's a pity we don't have this information in some more structured
595 form. Even the author of this function feels that writing little
596 parsers like this everywhere is stupid. */
599 remove_qualifiers (char *qid)
601 int quoted = 0; /* Zero if we're not in quotes;
602 '"' if we're in a double-quoted string;
603 '\'' if we're in a single-quoted string. */
604 int depth = 0; /* Number of unclosed parens we've seen. */
605 char *parenstack = (char *) alloca (strlen (qid));
607 char *last = 0; /* The character after the rightmost
608 `::' token we've seen so far. */
610 for (scan = qid; *scan; scan++)
616 else if (*scan == '\\' && *(scan + 1))
619 else if (scan[0] == ':' && scan[1] == ':')
621 /* If we're inside parenthesis (i.e., an argument list) or
622 angle brackets (i.e., a list of template arguments), then
623 we don't record the position of this :: token, since it's
624 not relevant to the top-level structure we're trying to
632 else if (*scan == '"' || *scan == '\'')
634 else if (*scan == '(')
635 parenstack[depth++] = ')';
636 else if (*scan == '[')
637 parenstack[depth++] = ']';
638 /* We're going to treat <> as a pair of matching characters,
639 since we're more likely to see those in template id's than
640 real less-than characters. What a crock. */
641 else if (*scan == '<')
642 parenstack[depth++] = '>';
643 else if (*scan == ')' || *scan == ']' || *scan == '>')
645 if (depth > 0 && parenstack[depth - 1] == *scan)
649 /* We're going to do a little error recovery here. If
650 we don't find a match for *scan on the paren stack,
651 but there is something lower on the stack that does
652 match, we pop the stack to that point. */
655 for (i = depth - 1; i >= 0; i--)
656 if (parenstack[i] == *scan)
668 /* We didn't find any :: tokens at the top level, so declare the
669 whole thing an unqualified identifier. */
673 /* Print any array sizes, function arguments or close parentheses
674 needed after the variable name (to describe its type).
675 Args work like c_type_print_varspec_prefix. */
678 c_type_print_varspec_suffix (struct type *type,
679 struct ui_file *stream,
680 int show, int passed_a_ptr,
682 const struct type_print_options *flags)
687 if (TYPE_NAME (type) && show <= 0)
692 switch (TYPE_CODE (type))
694 case TYPE_CODE_ARRAY:
696 LONGEST low_bound, high_bound;
697 int is_vector = TYPE_VECTOR (type);
700 fprintf_filtered (stream, ")");
702 fprintf_filtered (stream, (is_vector ?
703 " __attribute__ ((vector_size(" : "["));
704 /* Bounds are not yet resolved, print a bounds placeholder instead. */
705 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type)) == PROP_LOCEXPR
706 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type)) == PROP_LOCLIST)
707 fprintf_filtered (stream, "variable length");
708 else if (get_array_bounds (type, &low_bound, &high_bound))
709 fprintf_filtered (stream, "%s",
710 plongest (high_bound - low_bound + 1));
711 fprintf_filtered (stream, (is_vector ? ")))" : "]"));
713 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream,
718 case TYPE_CODE_MEMBERPTR:
719 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream,
723 case TYPE_CODE_METHODPTR:
724 fprintf_filtered (stream, ")");
725 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream,
731 case TYPE_CODE_RVALUE_REF:
732 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream,
736 case TYPE_CODE_METHOD:
739 fprintf_filtered (stream, ")");
741 c_type_print_args (type, stream, 0, current_language->la_language,
743 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream,
744 show, passed_a_ptr, 0, flags);
747 case TYPE_CODE_TYPEDEF:
748 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream,
749 show, passed_a_ptr, 0, flags);
752 case TYPE_CODE_UNDEF:
753 case TYPE_CODE_STRUCT:
754 case TYPE_CODE_UNION:
755 case TYPE_CODE_FLAGS:
760 case TYPE_CODE_ERROR:
764 case TYPE_CODE_RANGE:
765 case TYPE_CODE_STRING:
766 case TYPE_CODE_COMPLEX:
767 case TYPE_CODE_NAMESPACE:
768 case TYPE_CODE_DECFLOAT:
769 /* These types do not need a suffix. They are listed so that
770 gcc -Wall will report types that may not have been
774 error (_("type not handled in c_type_print_varspec_suffix()"));
779 /* A helper for c_type_print_base that displays template
780 parameters and their bindings, if needed.
782 TABLE is the local bindings table to use. If NULL, no printing is
783 done. Note that, at this point, TABLE won't have any useful
784 information in it -- but it is also used as a flag to
785 print_name_maybe_canonical to activate searching the global typedef
788 TYPE is the type whose template arguments are being displayed.
790 STREAM is the stream on which to print. */
793 c_type_print_template_args (const struct type_print_options *flags,
794 struct type *type, struct ui_file *stream)
801 for (i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
803 struct symbol *sym = TYPE_TEMPLATE_ARGUMENT (type, i);
805 if (SYMBOL_CLASS (sym) != LOC_TYPEDEF)
811 fprintf_filtered (stream, _("[with %s = "),
812 SYMBOL_LINKAGE_NAME (sym));
817 fputs_filtered (", ", stream);
819 fprintf_filtered (stream, "%s = ", SYMBOL_LINKAGE_NAME (sym));
822 c_print_type (SYMBOL_TYPE (sym), "", stream, -1, 0, flags);
826 fputs_filtered (_("] "), stream);
829 /* Print the name of the type (or the ultimate pointer target,
830 function value or array element), or the description of a structure
833 SHOW positive means print details about the type (e.g. enum
834 values), and print structure elements passing SHOW - 1 for show.
836 SHOW negative means just print the type name or struct tag if there
837 is one. If there is no name, print something sensible but concise
840 SHOW zero means just print the type name or struct tag if there is
841 one. If there is no name, print something sensible but not as
842 concise like "struct {int x; int y;}".
844 LEVEL is the number of spaces to indent by.
845 We increase it for some recursive calls. */
848 c_type_print_base (struct type *type, struct ui_file *stream,
849 int show, int level, const struct type_print_options *flags)
855 s_none, s_public, s_private, s_protected
858 int need_access_label = 0;
865 fputs_filtered (_("<type unknown>"), stream);
869 /* When SHOW is zero or less, and there is a valid type name, then
870 always just print the type name directly from the type. */
871 /* If we have "typedef struct foo {. . .} bar;" do we want to print
872 it as "struct foo" or as "bar"? Pick the latter, because C++
873 folk tend to expect things like "class5 *foo" rather than "struct
877 && TYPE_NAME (type) != NULL)
879 c_type_print_modifier (type, stream, 0, 1);
880 print_name_maybe_canonical (TYPE_NAME (type), flags, stream);
884 type = check_typedef (type);
886 switch (TYPE_CODE (type))
888 case TYPE_CODE_TYPEDEF:
889 /* If we get here, the typedef doesn't have a name, and we
890 couldn't resolve TYPE_TARGET_TYPE. Not much we can do. */
891 gdb_assert (TYPE_NAME (type) == NULL);
892 gdb_assert (TYPE_TARGET_TYPE (type) == NULL);
893 fprintf_filtered (stream, _("<unnamed typedef>"));
896 case TYPE_CODE_ARRAY:
898 case TYPE_CODE_MEMBERPTR:
900 case TYPE_CODE_RVALUE_REF:
902 case TYPE_CODE_METHOD:
903 case TYPE_CODE_METHODPTR:
904 c_type_print_base (TYPE_TARGET_TYPE (type),
905 stream, show, level, flags);
908 case TYPE_CODE_STRUCT:
909 case TYPE_CODE_UNION:
911 struct type_print_options local_flags = *flags;
912 struct type_print_options semi_local_flags = *flags;
913 struct cleanup *local_cleanups = make_cleanup (null_cleanup, NULL);
915 local_flags.local_typedefs = NULL;
916 semi_local_flags.local_typedefs = NULL;
920 if (flags->local_typedefs)
921 local_flags.local_typedefs
922 = copy_typedef_hash (flags->local_typedefs);
924 local_flags.local_typedefs = create_typedef_hash ();
926 make_cleanup_free_typedef_hash (local_flags.local_typedefs);
929 c_type_print_modifier (type, stream, 0, 1);
930 if (TYPE_CODE (type) == TYPE_CODE_UNION)
931 fprintf_filtered (stream, "union ");
932 else if (TYPE_DECLARED_CLASS (type))
933 fprintf_filtered (stream, "class ");
935 fprintf_filtered (stream, "struct ");
937 /* Print the tag if it exists. The HP aCC compiler emits a
938 spurious "{unnamed struct}"/"{unnamed union}"/"{unnamed
939 enum}" tag for unnamed struct/union/enum's, which we don't
941 if (TYPE_TAG_NAME (type) != NULL
942 && !startswith (TYPE_TAG_NAME (type), "{unnamed"))
944 /* When printing the tag name, we are still effectively
945 printing in the outer context, hence the use of FLAGS
947 print_name_maybe_canonical (TYPE_TAG_NAME (type), flags, stream);
949 fputs_filtered (" ", stream);
954 /* If we just printed a tag name, no need to print anything
956 if (TYPE_TAG_NAME (type) == NULL)
957 fprintf_filtered (stream, "{...}");
959 else if (show > 0 || TYPE_TAG_NAME (type) == NULL)
961 struct type *basetype;
964 c_type_print_template_args (&local_flags, type, stream);
966 /* Add in template parameters when printing derivation info. */
967 add_template_parameters (local_flags.local_typedefs, type);
968 cp_type_print_derivation_info (stream, type, &local_flags);
970 /* This holds just the global typedefs and the template
972 semi_local_flags.local_typedefs
973 = copy_typedef_hash (local_flags.local_typedefs);
974 if (semi_local_flags.local_typedefs)
975 make_cleanup_free_typedef_hash (semi_local_flags.local_typedefs);
977 /* Now add in the local typedefs. */
978 recursively_update_typedef_hash (local_flags.local_typedefs, type);
980 fprintf_filtered (stream, "{\n");
981 if (TYPE_NFIELDS (type) == 0 && TYPE_NFN_FIELDS (type) == 0
982 && TYPE_TYPEDEF_FIELD_COUNT (type) == 0)
984 if (TYPE_STUB (type))
985 fprintfi_filtered (level + 4, stream,
986 _("<incomplete type>\n"));
988 fprintfi_filtered (level + 4, stream,
989 _("<no data fields>\n"));
992 /* Start off with no specific section type, so we can print
993 one for the first field we find, and use that section type
994 thereafter until we find another type. */
996 section_type = s_none;
998 /* For a class, if all members are private, there's no need
999 for a "private:" label; similarly, for a struct or union
1000 masquerading as a class, if all members are public, there's
1001 no need for a "public:" label. */
1003 if (TYPE_DECLARED_CLASS (type))
1006 len = TYPE_NFIELDS (type);
1007 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
1008 if (!TYPE_FIELD_PRIVATE (type, i))
1010 need_access_label = 1;
1014 if (!need_access_label)
1016 len2 = TYPE_NFN_FIELDS (type);
1017 for (j = 0; j < len2; j++)
1019 len = TYPE_FN_FIELDLIST_LENGTH (type, j);
1020 for (i = 0; i < len; i++)
1021 if (!TYPE_FN_FIELD_PRIVATE (TYPE_FN_FIELDLIST1 (type,
1024 need_access_label = 1;
1027 if (need_access_label)
1035 len = TYPE_NFIELDS (type);
1036 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
1037 if (TYPE_FIELD_PRIVATE (type, i)
1038 || TYPE_FIELD_PROTECTED (type, i))
1040 need_access_label = 1;
1044 if (!need_access_label)
1046 len2 = TYPE_NFN_FIELDS (type);
1047 for (j = 0; j < len2; j++)
1050 len = TYPE_FN_FIELDLIST_LENGTH (type, j);
1051 for (i = 0; i < len; i++)
1052 if (TYPE_FN_FIELD_PROTECTED (TYPE_FN_FIELDLIST1 (type,
1054 || TYPE_FN_FIELD_PRIVATE (TYPE_FN_FIELDLIST1 (type,
1058 need_access_label = 1;
1061 if (need_access_label)
1067 /* If there is a base class for this type,
1068 do not print the field that it occupies. */
1070 len = TYPE_NFIELDS (type);
1071 vptr_fieldno = get_vptr_fieldno (type, &basetype);
1072 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
1076 /* If we have a virtual table pointer, omit it. Even if
1077 virtual table pointers are not specifically marked in
1078 the debug info, they should be artificial. */
1079 if ((i == vptr_fieldno && type == basetype)
1080 || TYPE_FIELD_ARTIFICIAL (type, i))
1083 if (need_access_label)
1085 if (TYPE_FIELD_PROTECTED (type, i))
1087 if (section_type != s_protected)
1089 section_type = s_protected;
1090 fprintfi_filtered (level + 2, stream,
1094 else if (TYPE_FIELD_PRIVATE (type, i))
1096 if (section_type != s_private)
1098 section_type = s_private;
1099 fprintfi_filtered (level + 2, stream,
1105 if (section_type != s_public)
1107 section_type = s_public;
1108 fprintfi_filtered (level + 2, stream,
1114 print_spaces_filtered (level + 4, stream);
1115 if (field_is_static (&TYPE_FIELD (type, i)))
1116 fprintf_filtered (stream, "static ");
1117 c_print_type (TYPE_FIELD_TYPE (type, i),
1118 TYPE_FIELD_NAME (type, i),
1119 stream, show - 1, level + 4,
1121 if (!field_is_static (&TYPE_FIELD (type, i))
1122 && TYPE_FIELD_PACKED (type, i))
1124 /* It is a bitfield. This code does not attempt
1125 to look at the bitpos and reconstruct filler,
1126 unnamed fields. This would lead to misleading
1127 results if the compiler does not put out fields
1128 for such things (I don't know what it does). */
1129 fprintf_filtered (stream, " : %d",
1130 TYPE_FIELD_BITSIZE (type, i));
1132 fprintf_filtered (stream, ";\n");
1135 /* If there are both fields and methods, put a blank line
1136 between them. Make sure to count only method that we
1137 will display; artificial methods will be hidden. */
1138 len = TYPE_NFN_FIELDS (type);
1139 if (!flags->print_methods)
1142 for (i = 0; i < len; i++)
1144 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1145 int len2 = TYPE_FN_FIELDLIST_LENGTH (type, i);
1148 for (j = 0; j < len2; j++)
1149 if (!TYPE_FN_FIELD_ARTIFICIAL (f, j))
1152 if (real_len > 0 && section_type != s_none)
1153 fprintf_filtered (stream, "\n");
1155 /* C++: print out the methods. */
1156 for (i = 0; i < len; i++)
1158 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1159 int j, len2 = TYPE_FN_FIELDLIST_LENGTH (type, i);
1160 const char *method_name = TYPE_FN_FIELDLIST_NAME (type, i);
1161 const char *name = type_name_no_tag (type);
1162 int is_constructor = name && strcmp (method_name,
1165 for (j = 0; j < len2; j++)
1167 const char *mangled_name;
1168 char *demangled_name;
1169 struct cleanup *inner_cleanup;
1170 const char *physname = TYPE_FN_FIELD_PHYSNAME (f, j);
1171 int is_full_physname_constructor =
1172 TYPE_FN_FIELD_CONSTRUCTOR (f, j)
1173 || is_constructor_name (physname)
1174 || is_destructor_name (physname)
1175 || method_name[0] == '~';
1177 /* Do not print out artificial methods. */
1178 if (TYPE_FN_FIELD_ARTIFICIAL (f, j))
1181 inner_cleanup = make_cleanup (null_cleanup, NULL);
1184 if (TYPE_FN_FIELD_PROTECTED (f, j))
1186 if (section_type != s_protected)
1188 section_type = s_protected;
1189 fprintfi_filtered (level + 2, stream,
1193 else if (TYPE_FN_FIELD_PRIVATE (f, j))
1195 if (section_type != s_private)
1197 section_type = s_private;
1198 fprintfi_filtered (level + 2, stream,
1204 if (section_type != s_public)
1206 section_type = s_public;
1207 fprintfi_filtered (level + 2, stream,
1212 print_spaces_filtered (level + 4, stream);
1213 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1214 fprintf_filtered (stream, "virtual ");
1215 else if (TYPE_FN_FIELD_STATIC_P (f, j))
1216 fprintf_filtered (stream, "static ");
1217 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)) == 0)
1219 /* Keep GDB from crashing here. */
1220 fprintf_filtered (stream,
1221 _("<undefined type> %s;\n"),
1222 TYPE_FN_FIELD_PHYSNAME (f, j));
1225 else if (!is_constructor /* Constructors don't
1228 && !is_full_physname_constructor /* " " */
1229 && !is_type_conversion_operator (type, i, j))
1231 c_print_type (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)),
1234 fputs_filtered (" ", stream);
1236 if (TYPE_FN_FIELD_STUB (f, j))
1240 /* Build something we can demangle. */
1241 tem = gdb_mangle_name (type, i, j);
1242 make_cleanup (xfree, tem);
1246 mangled_name = TYPE_FN_FIELD_PHYSNAME (f, j);
1249 gdb_demangle (mangled_name,
1250 DMGL_ANSI | DMGL_PARAMS);
1251 if (demangled_name == NULL)
1253 /* In some cases (for instance with the HP
1254 demangling), if a function has more than 10
1255 arguments, the demangling will fail.
1256 Let's try to reconstruct the function
1257 signature from the symbol information. */
1258 if (!TYPE_FN_FIELD_STUB (f, j))
1260 int staticp = TYPE_FN_FIELD_STATIC_P (f, j);
1261 struct type *mtype = TYPE_FN_FIELD_TYPE (f, j);
1263 cp_type_print_method_args (mtype,
1267 stream, &local_flags);
1270 fprintf_filtered (stream,
1271 _("<badly mangled name '%s'>"),
1277 char *demangled_no_class
1278 = remove_qualifiers (demangled_name);
1280 /* Get rid of the `static' appended by the
1282 p = strstr (demangled_no_class, " static");
1285 int length = p - demangled_no_class;
1286 char *demangled_no_static;
1289 = (char *) xmalloc (length + 1);
1290 strncpy (demangled_no_static,
1291 demangled_no_class, length);
1292 *(demangled_no_static + length) = '\0';
1293 fputs_filtered (demangled_no_static, stream);
1294 xfree (demangled_no_static);
1297 fputs_filtered (demangled_no_class, stream);
1298 xfree (demangled_name);
1301 do_cleanups (inner_cleanup);
1303 fprintf_filtered (stream, ";\n");
1307 /* Print typedefs defined in this class. */
1309 if (TYPE_TYPEDEF_FIELD_COUNT (type) != 0 && flags->print_typedefs)
1311 if (TYPE_NFIELDS (type) != 0 || TYPE_NFN_FIELDS (type) != 0)
1312 fprintf_filtered (stream, "\n");
1314 for (i = 0; i < TYPE_TYPEDEF_FIELD_COUNT (type); i++)
1316 struct type *target = TYPE_TYPEDEF_FIELD_TYPE (type, i);
1318 /* Dereference the typedef declaration itself. */
1319 gdb_assert (TYPE_CODE (target) == TYPE_CODE_TYPEDEF);
1320 target = TYPE_TARGET_TYPE (target);
1322 print_spaces_filtered (level + 4, stream);
1323 fprintf_filtered (stream, "typedef ");
1325 /* We want to print typedefs with substitutions
1326 from the template parameters or globally-known
1327 typedefs but not local typedefs. */
1328 c_print_type (target,
1329 TYPE_TYPEDEF_FIELD_NAME (type, i),
1330 stream, show - 1, level + 4,
1332 fprintf_filtered (stream, ";\n");
1336 fprintfi_filtered (level, stream, "}");
1339 do_cleanups (local_cleanups);
1343 case TYPE_CODE_ENUM:
1344 c_type_print_modifier (type, stream, 0, 1);
1345 fprintf_filtered (stream, "enum ");
1346 if (TYPE_DECLARED_CLASS (type))
1347 fprintf_filtered (stream, "class ");
1348 /* Print the tag name if it exists.
1349 The aCC compiler emits a spurious
1350 "{unnamed struct}"/"{unnamed union}"/"{unnamed enum}"
1351 tag for unnamed struct/union/enum's, which we don't
1353 if (TYPE_TAG_NAME (type) != NULL
1354 && !startswith (TYPE_TAG_NAME (type), "{unnamed"))
1356 print_name_maybe_canonical (TYPE_TAG_NAME (type), flags, stream);
1358 fputs_filtered (" ", stream);
1364 /* If we just printed a tag name, no need to print anything
1366 if (TYPE_TAG_NAME (type) == NULL)
1367 fprintf_filtered (stream, "{...}");
1369 else if (show > 0 || TYPE_TAG_NAME (type) == NULL)
1371 LONGEST lastval = 0;
1373 /* We can't handle this case perfectly, as DWARF does not
1374 tell us whether or not the underlying type was specified
1375 in the source (and other debug formats don't provide this
1376 at all). We choose to print the underlying type, if it
1377 has a name, when in C++ on the theory that it's better to
1378 print too much than too little; but conversely not to
1379 print something egregiously outside the current
1380 language's syntax. */
1381 if (current_language->la_language == language_cplus
1382 && TYPE_TARGET_TYPE (type) != NULL)
1384 struct type *underlying = check_typedef (TYPE_TARGET_TYPE (type));
1386 if (TYPE_NAME (underlying) != NULL)
1387 fprintf_filtered (stream, ": %s ", TYPE_NAME (underlying));
1390 fprintf_filtered (stream, "{");
1391 len = TYPE_NFIELDS (type);
1392 for (i = 0; i < len; i++)
1396 fprintf_filtered (stream, ", ");
1398 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
1399 if (lastval != TYPE_FIELD_ENUMVAL (type, i))
1401 fprintf_filtered (stream, " = %s",
1402 plongest (TYPE_FIELD_ENUMVAL (type, i)));
1403 lastval = TYPE_FIELD_ENUMVAL (type, i);
1407 fprintf_filtered (stream, "}");
1411 case TYPE_CODE_FLAGS:
1413 struct type_print_options local_flags = *flags;
1415 local_flags.local_typedefs = NULL;
1417 c_type_print_modifier (type, stream, 0, 1);
1418 fprintf_filtered (stream, "flag ");
1419 print_name_maybe_canonical (TYPE_NAME (type), flags, stream);
1422 fputs_filtered (" ", stream);
1423 fprintf_filtered (stream, "{\n");
1424 if (TYPE_NFIELDS (type) == 0)
1426 if (TYPE_STUB (type))
1427 fprintfi_filtered (level + 4, stream,
1428 _("<incomplete type>\n"));
1430 fprintfi_filtered (level + 4, stream,
1431 _("<no data fields>\n"));
1433 len = TYPE_NFIELDS (type);
1434 for (i = 0; i < len; i++)
1437 print_spaces_filtered (level + 4, stream);
1438 /* We pass "show" here and not "show - 1" to get enum types
1439 printed. There's no other way to see them. */
1440 c_print_type (TYPE_FIELD_TYPE (type, i),
1441 TYPE_FIELD_NAME (type, i),
1442 stream, show, level + 4,
1444 fprintf_filtered (stream, " @%s",
1445 plongest (TYPE_FIELD_BITPOS (type, i)));
1446 if (TYPE_FIELD_BITSIZE (type, i) > 1)
1448 fprintf_filtered (stream, "-%s",
1449 plongest (TYPE_FIELD_BITPOS (type, i)
1450 + TYPE_FIELD_BITSIZE (type, i)
1453 fprintf_filtered (stream, ";\n");
1455 fprintfi_filtered (level, stream, "}");
1460 case TYPE_CODE_VOID:
1461 fprintf_filtered (stream, "void");
1464 case TYPE_CODE_UNDEF:
1465 fprintf_filtered (stream, _("struct <unknown>"));
1468 case TYPE_CODE_ERROR:
1469 fprintf_filtered (stream, "%s", TYPE_ERROR_NAME (type));
1472 case TYPE_CODE_RANGE:
1473 /* This should not occur. */
1474 fprintf_filtered (stream, _("<range type>"));
1477 case TYPE_CODE_NAMESPACE:
1478 fputs_filtered ("namespace ", stream);
1479 fputs_filtered (TYPE_TAG_NAME (type), stream);
1483 /* Handle types not explicitly handled by the other cases, such
1484 as fundamental types. For these, just print whatever the
1485 type name is, as recorded in the type itself. If there is no
1486 type name, then complain. */
1487 if (TYPE_NAME (type) != NULL)
1489 c_type_print_modifier (type, stream, 0, 1);
1490 print_name_maybe_canonical (TYPE_NAME (type), flags, stream);
1494 /* At least for dump_symtab, it is important that this not
1496 fprintf_filtered (stream, _("<invalid type code %d>"),