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 /* A list of access specifiers used for printing. */
45 static void c_type_print_varspec_prefix (struct type
*,
48 const struct type_print_options
*);
50 /* Print "const", "volatile", or address space modifiers. */
51 static void c_type_print_modifier (struct type
*,
56 /* A callback function for cp_canonicalize_string_full that uses
57 find_typedef_in_hash. */
60 find_typedef_for_canonicalize (struct type
*t
, void *data
)
62 return find_typedef_in_hash ((const struct type_print_options
*) data
, t
);
65 /* Print NAME on STREAM. If the 'raw' field of FLAGS is not set,
66 canonicalize NAME using the local typedefs first. */
69 print_name_maybe_canonical (const char *name
,
70 const struct type_print_options
*flags
,
71 struct ui_file
*stream
)
76 s
= cp_canonicalize_string_full (name
,
77 find_typedef_for_canonicalize
,
80 fputs_filtered (!s
.empty () ? s
.c_str () : name
, stream
);
85 /* LEVEL is the depth to indent lines by. */
88 c_print_type (struct type
*type
,
89 const char *varstring
,
90 struct ui_file
*stream
,
92 const struct type_print_options
*flags
)
97 const char *local_name
;
100 type
= check_typedef (type
);
102 local_name
= find_typedef_in_hash (flags
, type
);
103 if (local_name
!= NULL
)
105 fputs_filtered (local_name
, stream
);
106 if (varstring
!= NULL
&& *varstring
!= '\0')
107 fputs_filtered (" ", stream
);
111 c_type_print_base (type
, stream
, show
, level
, flags
);
112 code
= TYPE_CODE (type
);
113 if ((varstring
!= NULL
&& *varstring
!= '\0')
114 /* Need a space if going to print stars or brackets;
115 but not if we will print just a type name. */
116 || ((show
> 0 || TYPE_NAME (type
) == 0)
117 && (code
== TYPE_CODE_PTR
|| code
== TYPE_CODE_FUNC
118 || code
== TYPE_CODE_METHOD
119 || (code
== TYPE_CODE_ARRAY
120 && !TYPE_VECTOR (type
))
121 || code
== TYPE_CODE_MEMBERPTR
122 || code
== TYPE_CODE_METHODPTR
123 || TYPE_IS_REFERENCE (type
))))
124 fputs_filtered (" ", stream
);
125 need_post_space
= (varstring
!= NULL
&& strcmp (varstring
, "") != 0);
126 c_type_print_varspec_prefix (type
, stream
, show
, 0, need_post_space
,
130 if (varstring
!= NULL
)
132 fputs_filtered (varstring
, stream
);
134 /* For demangled function names, we have the arglist as part of
135 the name, so don't print an additional pair of ()'s. */
136 if (local_name
== NULL
)
138 demangled_args
= strchr (varstring
, '(') != NULL
;
139 c_type_print_varspec_suffix (type
, stream
, show
,
146 /* Print a typedef using C syntax. TYPE is the underlying type.
147 NEW_SYMBOL is the symbol naming the type. STREAM is the stream on
151 c_print_typedef (struct type
*type
,
152 struct symbol
*new_symbol
,
153 struct ui_file
*stream
)
155 type
= check_typedef (type
);
156 fprintf_filtered (stream
, "typedef ");
157 type_print (type
, "", stream
, 0);
158 if (TYPE_NAME ((SYMBOL_TYPE (new_symbol
))) == 0
159 || strcmp (TYPE_NAME ((SYMBOL_TYPE (new_symbol
))),
160 SYMBOL_LINKAGE_NAME (new_symbol
)) != 0
161 || TYPE_CODE (SYMBOL_TYPE (new_symbol
)) == TYPE_CODE_TYPEDEF
)
162 fprintf_filtered (stream
, " %s", SYMBOL_PRINT_NAME (new_symbol
));
163 fprintf_filtered (stream
, ";\n");
166 /* If TYPE is a derived type, then print out derivation information.
167 Print only the actual base classes of this type, not the base
168 classes of the base classes. I.e. for the derivation hierarchy:
171 class B : public A {int b; };
172 class C : public B {int c; };
174 Print the type of class C as:
180 Not as the following (like gdb used to), which is not legal C++
181 syntax for derived types and may be confused with the multiple
184 class C : public B : public A {
188 In general, gdb should try to print the types as closely as
189 possible to the form that they appear in the source code. */
192 cp_type_print_derivation_info (struct ui_file
*stream
,
194 const struct type_print_options
*flags
)
199 for (i
= 0; i
< TYPE_N_BASECLASSES (type
); i
++)
202 fputs_filtered (i
== 0 ? ": " : ", ", stream
);
203 fprintf_filtered (stream
, "%s%s ",
204 BASETYPE_VIA_PUBLIC (type
, i
)
205 ? "public" : (TYPE_FIELD_PROTECTED (type
, i
)
206 ? "protected" : "private"),
207 BASETYPE_VIA_VIRTUAL (type
, i
) ? " virtual" : "");
208 name
= type_name_no_tag (TYPE_BASECLASS (type
, i
));
210 print_name_maybe_canonical (name
, flags
, stream
);
212 fprintf_filtered (stream
, "(null)");
216 fputs_filtered (" ", stream
);
220 /* Print the C++ method arguments ARGS to the file STREAM. */
223 cp_type_print_method_args (struct type
*mtype
, const char *prefix
,
224 const char *varstring
, int staticp
,
225 struct ui_file
*stream
,
226 const struct type_print_options
*flags
)
228 struct field
*args
= TYPE_FIELDS (mtype
);
229 int nargs
= TYPE_NFIELDS (mtype
);
230 int varargs
= TYPE_VARARGS (mtype
);
233 fprintf_symbol_filtered (stream
, prefix
,
234 language_cplus
, DMGL_ANSI
);
235 fprintf_symbol_filtered (stream
, varstring
,
236 language_cplus
, DMGL_ANSI
);
237 fputs_filtered ("(", stream
);
239 /* Skip the class variable. We keep this here to accommodate older
240 compilers and debug formats which may not support artificial
247 struct field arg
= args
[i
++];
249 /* Skip any artificial arguments. */
250 if (FIELD_ARTIFICIAL (arg
))
253 c_print_type (arg
.type
, "", stream
, 0, 0, flags
);
255 if (i
== nargs
&& varargs
)
256 fprintf_filtered (stream
, ", ...");
259 fprintf_filtered (stream
, ", ");
265 fprintf_filtered (stream
, "...");
266 else if (current_language
->la_language
== language_cplus
)
267 fprintf_filtered (stream
, "void");
269 fprintf_filtered (stream
, ")");
271 /* For non-static methods, read qualifiers from the type of
277 gdb_assert (nargs
> 0);
278 gdb_assert (TYPE_CODE (args
[0].type
) == TYPE_CODE_PTR
);
279 domain
= TYPE_TARGET_TYPE (args
[0].type
);
281 if (TYPE_CONST (domain
))
282 fprintf_filtered (stream
, " const");
284 if (TYPE_VOLATILE (domain
))
285 fprintf_filtered (stream
, " volatile");
287 if (TYPE_RESTRICT (domain
))
288 fprintf_filtered (stream
, " restrict");
290 if (TYPE_ATOMIC (domain
))
291 fprintf_filtered (stream
, " _Atomic");
296 /* Print any asterisks or open-parentheses needed before the
297 variable name (to describe its type).
299 On outermost call, pass 0 for PASSED_A_PTR.
300 On outermost call, SHOW > 0 means should ignore
301 any typename for TYPE and show its details.
302 SHOW is always zero on recursive calls.
304 NEED_POST_SPACE is non-zero when a space will be be needed
305 between a trailing qualifier and a field, variable, or function
309 c_type_print_varspec_prefix (struct type
*type
,
310 struct ui_file
*stream
,
311 int show
, int passed_a_ptr
,
313 const struct type_print_options
*flags
)
320 if (TYPE_NAME (type
) && show
<= 0)
325 switch (TYPE_CODE (type
))
328 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
),
329 stream
, show
, 1, 1, flags
);
330 fprintf_filtered (stream
, "*");
331 c_type_print_modifier (type
, stream
, 1, need_post_space
);
334 case TYPE_CODE_MEMBERPTR
:
335 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
),
336 stream
, show
, 0, 0, flags
);
337 name
= type_name_no_tag (TYPE_SELF_TYPE (type
));
339 print_name_maybe_canonical (name
, flags
, stream
);
341 c_type_print_base (TYPE_SELF_TYPE (type
),
342 stream
, -1, passed_a_ptr
, flags
);
343 fprintf_filtered (stream
, "::*");
346 case TYPE_CODE_METHODPTR
:
347 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
),
348 stream
, show
, 0, 0, flags
);
349 fprintf_filtered (stream
, "(");
350 name
= type_name_no_tag (TYPE_SELF_TYPE (type
));
352 print_name_maybe_canonical (name
, flags
, stream
);
354 c_type_print_base (TYPE_SELF_TYPE (type
),
355 stream
, -1, passed_a_ptr
, flags
);
356 fprintf_filtered (stream
, "::*");
360 case TYPE_CODE_RVALUE_REF
:
361 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
),
362 stream
, show
, 1, 0, flags
);
363 fprintf_filtered (stream
, TYPE_CODE(type
) == TYPE_CODE_REF
? "&" : "&&");
364 c_type_print_modifier (type
, stream
, 1, need_post_space
);
367 case TYPE_CODE_METHOD
:
369 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
),
370 stream
, show
, 0, 0, flags
);
372 fprintf_filtered (stream
, "(");
375 case TYPE_CODE_ARRAY
:
376 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
),
377 stream
, show
, 0, 0, flags
);
379 fprintf_filtered (stream
, "(");
382 case TYPE_CODE_TYPEDEF
:
383 c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type
),
384 stream
, show
, passed_a_ptr
, 0, flags
);
387 case TYPE_CODE_UNDEF
:
388 case TYPE_CODE_STRUCT
:
389 case TYPE_CODE_UNION
:
391 case TYPE_CODE_FLAGS
:
395 case TYPE_CODE_ERROR
:
399 case TYPE_CODE_RANGE
:
400 case TYPE_CODE_STRING
:
401 case TYPE_CODE_COMPLEX
:
402 case TYPE_CODE_NAMESPACE
:
403 case TYPE_CODE_DECFLOAT
:
404 /* These types need no prefix. They are listed here so that
405 gcc -Wall will reveal any types that haven't been handled. */
408 error (_("type not handled in c_type_print_varspec_prefix()"));
413 /* Print out "const" and "volatile" attributes,
414 and address space id if present.
415 TYPE is a pointer to the type being printed out.
416 STREAM is the output destination.
417 NEED_PRE_SPACE = 1 indicates an initial white space is needed.
418 NEED_POST_SPACE = 1 indicates a final white space is needed. */
421 c_type_print_modifier (struct type
*type
, struct ui_file
*stream
,
422 int need_pre_space
, int need_post_space
)
424 int did_print_modifier
= 0;
425 const char *address_space_id
;
427 /* We don't print `const' qualifiers for references --- since all
428 operators affect the thing referenced, not the reference itself,
429 every reference is `const'. */
430 if (TYPE_CONST (type
) && !TYPE_IS_REFERENCE (type
))
433 fprintf_filtered (stream
, " ");
434 fprintf_filtered (stream
, "const");
435 did_print_modifier
= 1;
438 if (TYPE_VOLATILE (type
))
440 if (did_print_modifier
|| need_pre_space
)
441 fprintf_filtered (stream
, " ");
442 fprintf_filtered (stream
, "volatile");
443 did_print_modifier
= 1;
446 if (TYPE_RESTRICT (type
))
448 if (did_print_modifier
|| need_pre_space
)
449 fprintf_filtered (stream
, " ");
450 fprintf_filtered (stream
, "restrict");
451 did_print_modifier
= 1;
454 if (TYPE_ATOMIC (type
))
456 if (did_print_modifier
|| need_pre_space
)
457 fprintf_filtered (stream
, " ");
458 fprintf_filtered (stream
, "_Atomic");
459 did_print_modifier
= 1;
462 address_space_id
= address_space_int_to_name (get_type_arch (type
),
463 TYPE_INSTANCE_FLAGS (type
));
464 if (address_space_id
)
466 if (did_print_modifier
|| need_pre_space
)
467 fprintf_filtered (stream
, " ");
468 fprintf_filtered (stream
, "@%s", address_space_id
);
469 did_print_modifier
= 1;
472 if (did_print_modifier
&& need_post_space
)
473 fprintf_filtered (stream
, " ");
477 /* Print out the arguments of TYPE, which should have TYPE_CODE_METHOD
478 or TYPE_CODE_FUNC, to STREAM. Artificial arguments, such as "this"
479 in non-static methods, are displayed if LINKAGE_NAME is zero. If
480 LINKAGE_NAME is non-zero and LANGUAGE is language_cplus the topmost
481 parameter types get removed their possible const and volatile qualifiers to
482 match demangled linkage name parameters part of such function type.
483 LANGUAGE is the language in which TYPE was defined. This is a necessary
484 evil since this code is used by the C and C++. */
487 c_type_print_args (struct type
*type
, struct ui_file
*stream
,
488 int linkage_name
, enum language language
,
489 const struct type_print_options
*flags
)
494 fprintf_filtered (stream
, "(");
496 for (i
= 0; i
< TYPE_NFIELDS (type
); i
++)
498 struct type
*param_type
;
500 if (TYPE_FIELD_ARTIFICIAL (type
, i
) && linkage_name
)
505 fprintf_filtered (stream
, ", ");
509 param_type
= TYPE_FIELD_TYPE (type
, i
);
511 if (language
== language_cplus
&& linkage_name
)
513 /* C++ standard, 13.1 Overloadable declarations, point 3, item:
514 - Parameter declarations that differ only in the presence or
515 absence of const and/or volatile are equivalent.
517 And the const/volatile qualifiers are not present in the mangled
518 names as produced by GCC. */
520 param_type
= make_cv_type (0, 0, param_type
, NULL
);
523 c_print_type (param_type
, "", stream
, -1, 0, flags
);
527 if (printed_any
&& TYPE_VARARGS (type
))
529 /* Print out a trailing ellipsis for varargs functions. Ignore
530 TYPE_VARARGS if the function has no named arguments; that
531 represents unprototyped (K&R style) C functions. */
532 if (printed_any
&& TYPE_VARARGS (type
))
534 fprintf_filtered (stream
, ", ");
536 fprintf_filtered (stream
, "...");
539 else if (!printed_any
540 && (TYPE_PROTOTYPED (type
) || language
== language_cplus
))
541 fprintf_filtered (stream
, "void");
543 fprintf_filtered (stream
, ")");
546 /* Return true iff the j'th overloading of the i'th method of TYPE
547 is a type conversion operator, like `operator int () { ... }'.
548 When listing a class's methods, we don't print the return type of
552 is_type_conversion_operator (struct type
*type
, int i
, int j
)
554 /* I think the whole idea of recognizing type conversion operators
555 by their name is pretty terrible. But I don't think our present
556 data structure gives us any other way to tell. If you know of
557 some other way, feel free to rewrite this function. */
558 const char *name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
560 if (!startswith (name
, CP_OPERATOR_STR
))
564 if (! strchr (" \t\f\n\r", *name
))
567 while (strchr (" \t\f\n\r", *name
))
570 if (!('a' <= *name
&& *name
<= 'z')
571 && !('A' <= *name
&& *name
<= 'Z')
573 /* If this doesn't look like the start of an identifier, then it
574 isn't a type conversion operator. */
576 else if (startswith (name
, "new"))
578 else if (startswith (name
, "delete"))
581 /* If it doesn't look like new or delete, it's a type conversion
585 /* Is that really the end of the name? */
586 if (('a' <= *name
&& *name
<= 'z')
587 || ('A' <= *name
&& *name
<= 'Z')
588 || ('0' <= *name
&& *name
<= '9')
590 /* No, so the identifier following "operator" must be a type name,
591 and this is a type conversion operator. */
594 /* That was indeed the end of the name, so it was `operator new' or
595 `operator delete', neither of which are type conversion
600 /* Given a C++ qualified identifier QID, strip off the qualifiers,
601 yielding the unqualified name. The return value is a pointer into
604 It's a pity we don't have this information in some more structured
605 form. Even the author of this function feels that writing little
606 parsers like this everywhere is stupid. */
609 remove_qualifiers (char *qid
)
611 int quoted
= 0; /* Zero if we're not in quotes;
612 '"' if we're in a double-quoted string;
613 '\'' if we're in a single-quoted string. */
614 int depth
= 0; /* Number of unclosed parens we've seen. */
615 char *parenstack
= (char *) alloca (strlen (qid
));
617 char *last
= 0; /* The character after the rightmost
618 `::' token we've seen so far. */
620 for (scan
= qid
; *scan
; scan
++)
626 else if (*scan
== '\\' && *(scan
+ 1))
629 else if (scan
[0] == ':' && scan
[1] == ':')
631 /* If we're inside parenthesis (i.e., an argument list) or
632 angle brackets (i.e., a list of template arguments), then
633 we don't record the position of this :: token, since it's
634 not relevant to the top-level structure we're trying to
642 else if (*scan
== '"' || *scan
== '\'')
644 else if (*scan
== '(')
645 parenstack
[depth
++] = ')';
646 else if (*scan
== '[')
647 parenstack
[depth
++] = ']';
648 /* We're going to treat <> as a pair of matching characters,
649 since we're more likely to see those in template id's than
650 real less-than characters. What a crock. */
651 else if (*scan
== '<')
652 parenstack
[depth
++] = '>';
653 else if (*scan
== ')' || *scan
== ']' || *scan
== '>')
655 if (depth
> 0 && parenstack
[depth
- 1] == *scan
)
659 /* We're going to do a little error recovery here. If
660 we don't find a match for *scan on the paren stack,
661 but there is something lower on the stack that does
662 match, we pop the stack to that point. */
665 for (i
= depth
- 1; i
>= 0; i
--)
666 if (parenstack
[i
] == *scan
)
678 /* We didn't find any :: tokens at the top level, so declare the
679 whole thing an unqualified identifier. */
683 /* Print any array sizes, function arguments or close parentheses
684 needed after the variable name (to describe its type).
685 Args work like c_type_print_varspec_prefix. */
688 c_type_print_varspec_suffix (struct type
*type
,
689 struct ui_file
*stream
,
690 int show
, int passed_a_ptr
,
692 const struct type_print_options
*flags
)
697 if (TYPE_NAME (type
) && show
<= 0)
702 switch (TYPE_CODE (type
))
704 case TYPE_CODE_ARRAY
:
706 LONGEST low_bound
, high_bound
;
707 int is_vector
= TYPE_VECTOR (type
);
710 fprintf_filtered (stream
, ")");
712 fprintf_filtered (stream
, (is_vector
?
713 " __attribute__ ((vector_size(" : "["));
714 /* Bounds are not yet resolved, print a bounds placeholder instead. */
715 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCEXPR
716 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCLIST
)
717 fprintf_filtered (stream
, "variable length");
718 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
719 fprintf_filtered (stream
, "%s",
720 plongest (high_bound
- low_bound
+ 1));
721 fprintf_filtered (stream
, (is_vector
? ")))" : "]"));
723 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
,
728 case TYPE_CODE_MEMBERPTR
:
729 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
,
733 case TYPE_CODE_METHODPTR
:
734 fprintf_filtered (stream
, ")");
735 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
,
741 case TYPE_CODE_RVALUE_REF
:
742 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
,
746 case TYPE_CODE_METHOD
:
749 fprintf_filtered (stream
, ")");
751 c_type_print_args (type
, stream
, 0, current_language
->la_language
,
753 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
,
754 show
, passed_a_ptr
, 0, flags
);
757 case TYPE_CODE_TYPEDEF
:
758 c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type
), stream
,
759 show
, passed_a_ptr
, 0, flags
);
762 case TYPE_CODE_UNDEF
:
763 case TYPE_CODE_STRUCT
:
764 case TYPE_CODE_UNION
:
765 case TYPE_CODE_FLAGS
:
770 case TYPE_CODE_ERROR
:
774 case TYPE_CODE_RANGE
:
775 case TYPE_CODE_STRING
:
776 case TYPE_CODE_COMPLEX
:
777 case TYPE_CODE_NAMESPACE
:
778 case TYPE_CODE_DECFLOAT
:
779 /* These types do not need a suffix. They are listed so that
780 gcc -Wall will report types that may not have been
784 error (_("type not handled in c_type_print_varspec_suffix()"));
789 /* A helper for c_type_print_base that displays template
790 parameters and their bindings, if needed.
792 TABLE is the local bindings table to use. If NULL, no printing is
793 done. Note that, at this point, TABLE won't have any useful
794 information in it -- but it is also used as a flag to
795 print_name_maybe_canonical to activate searching the global typedef
798 TYPE is the type whose template arguments are being displayed.
800 STREAM is the stream on which to print. */
803 c_type_print_template_args (const struct type_print_options
*flags
,
804 struct type
*type
, struct ui_file
*stream
)
811 for (i
= 0; i
< TYPE_N_TEMPLATE_ARGUMENTS (type
); ++i
)
813 struct symbol
*sym
= TYPE_TEMPLATE_ARGUMENT (type
, i
);
815 if (SYMBOL_CLASS (sym
) != LOC_TYPEDEF
)
821 fprintf_filtered (stream
, _("[with %s = "),
822 SYMBOL_LINKAGE_NAME (sym
));
827 fputs_filtered (", ", stream
);
829 fprintf_filtered (stream
, "%s = ", SYMBOL_LINKAGE_NAME (sym
));
832 c_print_type (SYMBOL_TYPE (sym
), "", stream
, -1, 0, flags
);
836 fputs_filtered (_("] "), stream
);
839 /* Output an access specifier to STREAM, if needed. LAST_ACCESS is the
840 last access specifier output (typically returned by this function). */
842 static enum access_specifier
843 output_access_specifier (struct ui_file
*stream
,
844 enum access_specifier last_access
,
845 int level
, bool is_protected
, bool is_private
)
849 if (last_access
!= s_protected
)
851 last_access
= s_protected
;
852 fprintfi_filtered (level
+ 2, stream
,
858 if (last_access
!= s_private
)
860 last_access
= s_private
;
861 fprintfi_filtered (level
+ 2, stream
,
867 if (last_access
!= s_public
)
869 last_access
= s_public
;
870 fprintfi_filtered (level
+ 2, stream
,
878 /* Print the name of the type (or the ultimate pointer target,
879 function value or array element), or the description of a structure
882 SHOW positive means print details about the type (e.g. enum
883 values), and print structure elements passing SHOW - 1 for show.
885 SHOW negative means just print the type name or struct tag if there
886 is one. If there is no name, print something sensible but concise
889 SHOW zero means just print the type name or struct tag if there is
890 one. If there is no name, print something sensible but not as
891 concise like "struct {int x; int y;}".
893 LEVEL is the number of spaces to indent by.
894 We increase it for some recursive calls. */
897 c_type_print_base (struct type
*type
, struct ui_file
*stream
,
898 int show
, int level
, const struct type_print_options
*flags
)
902 enum access_specifier section_type
;
903 int need_access_label
= 0;
910 fputs_filtered (_("<type unknown>"), stream
);
914 /* When SHOW is zero or less, and there is a valid type name, then
915 always just print the type name directly from the type. */
916 /* If we have "typedef struct foo {. . .} bar;" do we want to print
917 it as "struct foo" or as "bar"? Pick the latter, because C++
918 folk tend to expect things like "class5 *foo" rather than "struct
922 && TYPE_NAME (type
) != NULL
)
924 c_type_print_modifier (type
, stream
, 0, 1);
925 print_name_maybe_canonical (TYPE_NAME (type
), flags
, stream
);
929 type
= check_typedef (type
);
931 switch (TYPE_CODE (type
))
933 case TYPE_CODE_TYPEDEF
:
934 /* If we get here, the typedef doesn't have a name, and we
935 couldn't resolve TYPE_TARGET_TYPE. Not much we can do. */
936 gdb_assert (TYPE_NAME (type
) == NULL
);
937 gdb_assert (TYPE_TARGET_TYPE (type
) == NULL
);
938 fprintf_filtered (stream
, _("<unnamed typedef>"));
942 case TYPE_CODE_METHOD
:
943 if (TYPE_TARGET_TYPE (type
) == NULL
)
944 type_print_unknown_return_type (stream
);
946 c_type_print_base (TYPE_TARGET_TYPE (type
),
947 stream
, show
, level
, flags
);
949 case TYPE_CODE_ARRAY
:
951 case TYPE_CODE_MEMBERPTR
:
953 case TYPE_CODE_RVALUE_REF
:
954 case TYPE_CODE_METHODPTR
:
955 c_type_print_base (TYPE_TARGET_TYPE (type
),
956 stream
, show
, level
, flags
);
959 case TYPE_CODE_STRUCT
:
960 case TYPE_CODE_UNION
:
962 struct type_print_options local_flags
= *flags
;
963 struct type_print_options semi_local_flags
= *flags
;
964 struct cleanup
*local_cleanups
= make_cleanup (null_cleanup
, NULL
);
966 local_flags
.local_typedefs
= NULL
;
967 semi_local_flags
.local_typedefs
= NULL
;
971 if (flags
->local_typedefs
)
972 local_flags
.local_typedefs
973 = copy_typedef_hash (flags
->local_typedefs
);
975 local_flags
.local_typedefs
= create_typedef_hash ();
977 make_cleanup_free_typedef_hash (local_flags
.local_typedefs
);
980 c_type_print_modifier (type
, stream
, 0, 1);
981 if (TYPE_CODE (type
) == TYPE_CODE_UNION
)
982 fprintf_filtered (stream
, "union ");
983 else if (TYPE_DECLARED_CLASS (type
))
984 fprintf_filtered (stream
, "class ");
986 fprintf_filtered (stream
, "struct ");
988 /* Print the tag if it exists. The HP aCC compiler emits a
989 spurious "{unnamed struct}"/"{unnamed union}"/"{unnamed
990 enum}" tag for unnamed struct/union/enum's, which we don't
992 if (TYPE_TAG_NAME (type
) != NULL
993 && !startswith (TYPE_TAG_NAME (type
), "{unnamed"))
995 /* When printing the tag name, we are still effectively
996 printing in the outer context, hence the use of FLAGS
998 print_name_maybe_canonical (TYPE_TAG_NAME (type
), flags
, stream
);
1000 fputs_filtered (" ", stream
);
1005 /* If we just printed a tag name, no need to print anything
1007 if (TYPE_TAG_NAME (type
) == NULL
)
1008 fprintf_filtered (stream
, "{...}");
1010 else if (show
> 0 || TYPE_TAG_NAME (type
) == NULL
)
1012 struct type
*basetype
;
1015 c_type_print_template_args (&local_flags
, type
, stream
);
1017 /* Add in template parameters when printing derivation info. */
1018 add_template_parameters (local_flags
.local_typedefs
, type
);
1019 cp_type_print_derivation_info (stream
, type
, &local_flags
);
1021 /* This holds just the global typedefs and the template
1023 semi_local_flags
.local_typedefs
1024 = copy_typedef_hash (local_flags
.local_typedefs
);
1025 if (semi_local_flags
.local_typedefs
)
1026 make_cleanup_free_typedef_hash (semi_local_flags
.local_typedefs
);
1028 /* Now add in the local typedefs. */
1029 recursively_update_typedef_hash (local_flags
.local_typedefs
, type
);
1031 fprintf_filtered (stream
, "{\n");
1032 if (TYPE_NFIELDS (type
) == 0 && TYPE_NFN_FIELDS (type
) == 0
1033 && TYPE_TYPEDEF_FIELD_COUNT (type
) == 0)
1035 if (TYPE_STUB (type
))
1036 fprintfi_filtered (level
+ 4, stream
,
1037 _("<incomplete type>\n"));
1039 fprintfi_filtered (level
+ 4, stream
,
1040 _("<no data fields>\n"));
1043 /* Start off with no specific section type, so we can print
1044 one for the first field we find, and use that section type
1045 thereafter until we find another type. */
1047 section_type
= s_none
;
1049 /* For a class, if all members are private, there's no need
1050 for a "private:" label; similarly, for a struct or union
1051 masquerading as a class, if all members are public, there's
1052 no need for a "public:" label. */
1054 if (TYPE_DECLARED_CLASS (type
))
1057 len
= TYPE_NFIELDS (type
);
1058 for (i
= TYPE_N_BASECLASSES (type
); i
< len
; i
++)
1059 if (!TYPE_FIELD_PRIVATE (type
, i
))
1061 need_access_label
= 1;
1065 if (!need_access_label
)
1067 len2
= TYPE_NFN_FIELDS (type
);
1068 for (j
= 0; j
< len2
; j
++)
1070 len
= TYPE_FN_FIELDLIST_LENGTH (type
, j
);
1071 for (i
= 0; i
< len
; i
++)
1072 if (!TYPE_FN_FIELD_PRIVATE (TYPE_FN_FIELDLIST1 (type
,
1075 need_access_label
= 1;
1078 if (need_access_label
)
1083 if (!need_access_label
)
1085 for (i
= 0; i
< TYPE_TYPEDEF_FIELD_COUNT (type
); ++i
)
1087 if (!TYPE_TYPEDEF_FIELD_PRIVATE (type
, i
))
1089 need_access_label
= 1;
1098 len
= TYPE_NFIELDS (type
);
1099 for (i
= TYPE_N_BASECLASSES (type
); i
< len
; i
++)
1100 if (TYPE_FIELD_PRIVATE (type
, i
)
1101 || TYPE_FIELD_PROTECTED (type
, i
))
1103 need_access_label
= 1;
1107 if (!need_access_label
)
1109 len2
= TYPE_NFN_FIELDS (type
);
1110 for (j
= 0; j
< len2
; j
++)
1113 len
= TYPE_FN_FIELDLIST_LENGTH (type
, j
);
1114 for (i
= 0; i
< len
; i
++)
1115 if (TYPE_FN_FIELD_PROTECTED (TYPE_FN_FIELDLIST1 (type
,
1117 || TYPE_FN_FIELD_PRIVATE (TYPE_FN_FIELDLIST1 (type
,
1121 need_access_label
= 1;
1124 if (need_access_label
)
1129 if (!need_access_label
)
1131 for (i
= 0; i
< TYPE_TYPEDEF_FIELD_COUNT (type
); ++i
)
1133 if (TYPE_TYPEDEF_FIELD_PROTECTED (type
, i
)
1134 || TYPE_TYPEDEF_FIELD_PRIVATE (type
, i
))
1136 need_access_label
= 1;
1143 /* If there is a base class for this type,
1144 do not print the field that it occupies. */
1146 len
= TYPE_NFIELDS (type
);
1147 vptr_fieldno
= get_vptr_fieldno (type
, &basetype
);
1148 for (i
= TYPE_N_BASECLASSES (type
); i
< len
; i
++)
1152 /* If we have a virtual table pointer, omit it. Even if
1153 virtual table pointers are not specifically marked in
1154 the debug info, they should be artificial. */
1155 if ((i
== vptr_fieldno
&& type
== basetype
)
1156 || TYPE_FIELD_ARTIFICIAL (type
, i
))
1159 if (need_access_label
)
1161 section_type
= output_access_specifier
1162 (stream
, section_type
, level
,
1163 TYPE_FIELD_PROTECTED (type
, i
),
1164 TYPE_FIELD_PRIVATE (type
, i
));
1167 print_spaces_filtered (level
+ 4, stream
);
1168 if (field_is_static (&TYPE_FIELD (type
, i
)))
1169 fprintf_filtered (stream
, "static ");
1170 c_print_type (TYPE_FIELD_TYPE (type
, i
),
1171 TYPE_FIELD_NAME (type
, i
),
1172 stream
, show
- 1, level
+ 4,
1174 if (!field_is_static (&TYPE_FIELD (type
, i
))
1175 && TYPE_FIELD_PACKED (type
, i
))
1177 /* It is a bitfield. This code does not attempt
1178 to look at the bitpos and reconstruct filler,
1179 unnamed fields. This would lead to misleading
1180 results if the compiler does not put out fields
1181 for such things (I don't know what it does). */
1182 fprintf_filtered (stream
, " : %d",
1183 TYPE_FIELD_BITSIZE (type
, i
));
1185 fprintf_filtered (stream
, ";\n");
1188 /* If there are both fields and methods, put a blank line
1189 between them. Make sure to count only method that we
1190 will display; artificial methods will be hidden. */
1191 len
= TYPE_NFN_FIELDS (type
);
1192 if (!flags
->print_methods
)
1195 for (i
= 0; i
< len
; i
++)
1197 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
1198 int len2
= TYPE_FN_FIELDLIST_LENGTH (type
, i
);
1201 for (j
= 0; j
< len2
; j
++)
1202 if (!TYPE_FN_FIELD_ARTIFICIAL (f
, j
))
1205 if (real_len
> 0 && section_type
!= s_none
)
1206 fprintf_filtered (stream
, "\n");
1208 /* C++: print out the methods. */
1209 for (i
= 0; i
< len
; i
++)
1211 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
1212 int j
, len2
= TYPE_FN_FIELDLIST_LENGTH (type
, i
);
1213 const char *method_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
1214 const char *name
= type_name_no_tag (type
);
1215 int is_constructor
= name
&& strcmp (method_name
,
1218 for (j
= 0; j
< len2
; j
++)
1220 const char *mangled_name
;
1221 gdb::unique_xmalloc_ptr
<char> mangled_name_holder
;
1222 char *demangled_name
;
1223 const char *physname
= TYPE_FN_FIELD_PHYSNAME (f
, j
);
1224 int is_full_physname_constructor
=
1225 TYPE_FN_FIELD_CONSTRUCTOR (f
, j
)
1226 || is_constructor_name (physname
)
1227 || is_destructor_name (physname
)
1228 || method_name
[0] == '~';
1230 /* Do not print out artificial methods. */
1231 if (TYPE_FN_FIELD_ARTIFICIAL (f
, j
))
1235 section_type
= output_access_specifier
1236 (stream
, section_type
, level
,
1237 TYPE_FN_FIELD_PROTECTED (f
, j
),
1238 TYPE_FN_FIELD_PRIVATE (f
, j
));
1240 print_spaces_filtered (level
+ 4, stream
);
1241 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
1242 fprintf_filtered (stream
, "virtual ");
1243 else if (TYPE_FN_FIELD_STATIC_P (f
, j
))
1244 fprintf_filtered (stream
, "static ");
1245 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)) == 0)
1247 /* Keep GDB from crashing here. */
1248 fprintf_filtered (stream
,
1249 _("<undefined type> %s;\n"),
1250 TYPE_FN_FIELD_PHYSNAME (f
, j
));
1253 else if (!is_constructor
/* Constructors don't
1256 && !is_full_physname_constructor
/* " " */
1257 && !is_type_conversion_operator (type
, i
, j
))
1259 c_print_type (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)),
1262 fputs_filtered (" ", stream
);
1264 if (TYPE_FN_FIELD_STUB (f
, j
))
1266 /* Build something we can demangle. */
1267 mangled_name_holder
.reset (gdb_mangle_name (type
, i
, j
));
1268 mangled_name
= mangled_name_holder
.get ();
1271 mangled_name
= TYPE_FN_FIELD_PHYSNAME (f
, j
);
1274 gdb_demangle (mangled_name
,
1275 DMGL_ANSI
| DMGL_PARAMS
);
1276 if (demangled_name
== NULL
)
1278 /* In some cases (for instance with the HP
1279 demangling), if a function has more than 10
1280 arguments, the demangling will fail.
1281 Let's try to reconstruct the function
1282 signature from the symbol information. */
1283 if (!TYPE_FN_FIELD_STUB (f
, j
))
1285 int staticp
= TYPE_FN_FIELD_STATIC_P (f
, j
);
1286 struct type
*mtype
= TYPE_FN_FIELD_TYPE (f
, j
);
1288 cp_type_print_method_args (mtype
,
1292 stream
, &local_flags
);
1295 fprintf_filtered (stream
,
1296 _("<badly mangled name '%s'>"),
1302 char *demangled_no_class
1303 = remove_qualifiers (demangled_name
);
1305 /* Get rid of the `static' appended by the
1307 p
= strstr (demangled_no_class
, " static");
1310 int length
= p
- demangled_no_class
;
1311 char *demangled_no_static
;
1314 = (char *) xmalloc (length
+ 1);
1315 strncpy (demangled_no_static
,
1316 demangled_no_class
, length
);
1317 *(demangled_no_static
+ length
) = '\0';
1318 fputs_filtered (demangled_no_static
, stream
);
1319 xfree (demangled_no_static
);
1322 fputs_filtered (demangled_no_class
, stream
);
1323 xfree (demangled_name
);
1326 fprintf_filtered (stream
, ";\n");
1330 /* Print typedefs defined in this class. */
1332 if (TYPE_TYPEDEF_FIELD_COUNT (type
) != 0 && flags
->print_typedefs
)
1334 if (TYPE_NFIELDS (type
) != 0 || TYPE_NFN_FIELDS (type
) != 0)
1335 fprintf_filtered (stream
, "\n");
1337 for (i
= 0; i
< TYPE_TYPEDEF_FIELD_COUNT (type
); i
++)
1339 struct type
*target
= TYPE_TYPEDEF_FIELD_TYPE (type
, i
);
1341 /* Dereference the typedef declaration itself. */
1342 gdb_assert (TYPE_CODE (target
) == TYPE_CODE_TYPEDEF
);
1343 target
= TYPE_TARGET_TYPE (target
);
1345 if (need_access_label
)
1347 section_type
= output_access_specifier
1348 (stream
, section_type
, level
,
1349 TYPE_TYPEDEF_FIELD_PROTECTED (type
, i
),
1350 TYPE_TYPEDEF_FIELD_PRIVATE (type
, i
));
1352 print_spaces_filtered (level
+ 4, stream
);
1353 fprintf_filtered (stream
, "typedef ");
1355 /* We want to print typedefs with substitutions
1356 from the template parameters or globally-known
1357 typedefs but not local typedefs. */
1358 c_print_type (target
,
1359 TYPE_TYPEDEF_FIELD_NAME (type
, i
),
1360 stream
, show
- 1, level
+ 4,
1362 fprintf_filtered (stream
, ";\n");
1366 fprintfi_filtered (level
, stream
, "}");
1369 do_cleanups (local_cleanups
);
1373 case TYPE_CODE_ENUM
:
1374 c_type_print_modifier (type
, stream
, 0, 1);
1375 fprintf_filtered (stream
, "enum ");
1376 if (TYPE_DECLARED_CLASS (type
))
1377 fprintf_filtered (stream
, "class ");
1378 /* Print the tag name if it exists.
1379 The aCC compiler emits a spurious
1380 "{unnamed struct}"/"{unnamed union}"/"{unnamed enum}"
1381 tag for unnamed struct/union/enum's, which we don't
1383 if (TYPE_TAG_NAME (type
) != NULL
1384 && !startswith (TYPE_TAG_NAME (type
), "{unnamed"))
1386 print_name_maybe_canonical (TYPE_TAG_NAME (type
), flags
, stream
);
1388 fputs_filtered (" ", stream
);
1394 /* If we just printed a tag name, no need to print anything
1396 if (TYPE_TAG_NAME (type
) == NULL
)
1397 fprintf_filtered (stream
, "{...}");
1399 else if (show
> 0 || TYPE_TAG_NAME (type
) == NULL
)
1401 LONGEST lastval
= 0;
1403 /* We can't handle this case perfectly, as DWARF does not
1404 tell us whether or not the underlying type was specified
1405 in the source (and other debug formats don't provide this
1406 at all). We choose to print the underlying type, if it
1407 has a name, when in C++ on the theory that it's better to
1408 print too much than too little; but conversely not to
1409 print something egregiously outside the current
1410 language's syntax. */
1411 if (current_language
->la_language
== language_cplus
1412 && TYPE_TARGET_TYPE (type
) != NULL
)
1414 struct type
*underlying
= check_typedef (TYPE_TARGET_TYPE (type
));
1416 if (TYPE_NAME (underlying
) != NULL
)
1417 fprintf_filtered (stream
, ": %s ", TYPE_NAME (underlying
));
1420 fprintf_filtered (stream
, "{");
1421 len
= TYPE_NFIELDS (type
);
1422 for (i
= 0; i
< len
; i
++)
1426 fprintf_filtered (stream
, ", ");
1428 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
1429 if (lastval
!= TYPE_FIELD_ENUMVAL (type
, i
))
1431 fprintf_filtered (stream
, " = %s",
1432 plongest (TYPE_FIELD_ENUMVAL (type
, i
)));
1433 lastval
= TYPE_FIELD_ENUMVAL (type
, i
);
1437 fprintf_filtered (stream
, "}");
1441 case TYPE_CODE_FLAGS
:
1443 struct type_print_options local_flags
= *flags
;
1445 local_flags
.local_typedefs
= NULL
;
1447 c_type_print_modifier (type
, stream
, 0, 1);
1448 fprintf_filtered (stream
, "flag ");
1449 print_name_maybe_canonical (TYPE_NAME (type
), flags
, stream
);
1452 fputs_filtered (" ", stream
);
1453 fprintf_filtered (stream
, "{\n");
1454 if (TYPE_NFIELDS (type
) == 0)
1456 if (TYPE_STUB (type
))
1457 fprintfi_filtered (level
+ 4, stream
,
1458 _("<incomplete type>\n"));
1460 fprintfi_filtered (level
+ 4, stream
,
1461 _("<no data fields>\n"));
1463 len
= TYPE_NFIELDS (type
);
1464 for (i
= 0; i
< len
; i
++)
1467 print_spaces_filtered (level
+ 4, stream
);
1468 /* We pass "show" here and not "show - 1" to get enum types
1469 printed. There's no other way to see them. */
1470 c_print_type (TYPE_FIELD_TYPE (type
, i
),
1471 TYPE_FIELD_NAME (type
, i
),
1472 stream
, show
, level
+ 4,
1474 fprintf_filtered (stream
, " @%s",
1475 plongest (TYPE_FIELD_BITPOS (type
, i
)));
1476 if (TYPE_FIELD_BITSIZE (type
, i
) > 1)
1478 fprintf_filtered (stream
, "-%s",
1479 plongest (TYPE_FIELD_BITPOS (type
, i
)
1480 + TYPE_FIELD_BITSIZE (type
, i
)
1483 fprintf_filtered (stream
, ";\n");
1485 fprintfi_filtered (level
, stream
, "}");
1490 case TYPE_CODE_VOID
:
1491 fprintf_filtered (stream
, "void");
1494 case TYPE_CODE_UNDEF
:
1495 fprintf_filtered (stream
, _("struct <unknown>"));
1498 case TYPE_CODE_ERROR
:
1499 fprintf_filtered (stream
, "%s", TYPE_ERROR_NAME (type
));
1502 case TYPE_CODE_RANGE
:
1503 /* This should not occur. */
1504 fprintf_filtered (stream
, _("<range type>"));
1507 case TYPE_CODE_NAMESPACE
:
1508 fputs_filtered ("namespace ", stream
);
1509 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
1513 /* Handle types not explicitly handled by the other cases, such
1514 as fundamental types. For these, just print whatever the
1515 type name is, as recorded in the type itself. If there is no
1516 type name, then complain. */
1517 if (TYPE_NAME (type
) != NULL
)
1519 c_type_print_modifier (type
, stream
, 0, 1);
1520 print_name_maybe_canonical (TYPE_NAME (type
), flags
, stream
);
1524 /* At least for dump_symtab, it is important that this not
1526 fprintf_filtered (stream
, _("<invalid type code %d>"),