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 /* Return true is an access label (i.e., "public:", "private:",
879 "protected:") needs to be printed for TYPE. */
882 need_access_label_p (struct type
*type
)
884 if (TYPE_DECLARED_CLASS (type
))
887 for (int i
= TYPE_N_BASECLASSES (type
); i
< TYPE_NFIELDS (type
); i
++)
888 if (!TYPE_FIELD_PRIVATE (type
, i
))
891 for (int j
= 0; j
< TYPE_NFN_FIELDS (type
); j
++)
892 for (int i
= 0; i
< TYPE_FN_FIELDLIST_LENGTH (type
, j
); i
++)
893 if (!TYPE_FN_FIELD_PRIVATE (TYPE_FN_FIELDLIST1 (type
,
897 for (int i
= 0; i
< TYPE_TYPEDEF_FIELD_COUNT (type
); ++i
)
898 if (!TYPE_TYPEDEF_FIELD_PRIVATE (type
, i
))
904 for (int i
= TYPE_N_BASECLASSES (type
); i
< TYPE_NFIELDS (type
); i
++)
905 if (TYPE_FIELD_PRIVATE (type
, i
) || TYPE_FIELD_PROTECTED (type
, i
))
908 for (int j
= 0; j
< TYPE_NFN_FIELDS (type
); j
++)
911 for (int i
= 0; i
< TYPE_FN_FIELDLIST_LENGTH (type
, j
); i
++)
912 if (TYPE_FN_FIELD_PROTECTED (TYPE_FN_FIELDLIST1 (type
,
914 || TYPE_FN_FIELD_PRIVATE (TYPE_FN_FIELDLIST1 (type
,
920 for (int i
= 0; i
< TYPE_TYPEDEF_FIELD_COUNT (type
); ++i
)
921 if (TYPE_TYPEDEF_FIELD_PROTECTED (type
, i
)
922 || TYPE_TYPEDEF_FIELD_PRIVATE (type
, i
))
929 /* Helper for 'c_type_print_base' that handles structs and unions.
930 For a description of the arguments, see 'c_type_print_base'. */
933 c_type_print_base_struct_union (struct type
*type
, struct ui_file
*stream
,
935 const struct type_print_options
*flags
)
937 struct type_print_options local_flags
= *flags
;
938 struct type_print_options semi_local_flags
= *flags
;
939 struct cleanup
*local_cleanups
= make_cleanup (null_cleanup
, NULL
);
941 local_flags
.local_typedefs
= NULL
;
942 semi_local_flags
.local_typedefs
= NULL
;
946 if (flags
->local_typedefs
)
947 local_flags
.local_typedefs
948 = copy_typedef_hash (flags
->local_typedefs
);
950 local_flags
.local_typedefs
= create_typedef_hash ();
952 make_cleanup_free_typedef_hash (local_flags
.local_typedefs
);
955 c_type_print_modifier (type
, stream
, 0, 1);
956 if (TYPE_CODE (type
) == TYPE_CODE_UNION
)
957 fprintf_filtered (stream
, "union ");
958 else if (TYPE_DECLARED_CLASS (type
))
959 fprintf_filtered (stream
, "class ");
961 fprintf_filtered (stream
, "struct ");
963 /* Print the tag if it exists. The HP aCC compiler emits a
964 spurious "{unnamed struct}"/"{unnamed union}"/"{unnamed
965 enum}" tag for unnamed struct/union/enum's, which we don't
967 if (TYPE_TAG_NAME (type
) != NULL
968 && !startswith (TYPE_TAG_NAME (type
), "{unnamed"))
970 /* When printing the tag name, we are still effectively
971 printing in the outer context, hence the use of FLAGS
973 print_name_maybe_canonical (TYPE_TAG_NAME (type
), flags
, stream
);
975 fputs_filtered (" ", stream
);
980 /* If we just printed a tag name, no need to print anything
982 if (TYPE_TAG_NAME (type
) == NULL
)
983 fprintf_filtered (stream
, "{...}");
985 else if (show
> 0 || TYPE_TAG_NAME (type
) == NULL
)
987 struct type
*basetype
;
990 c_type_print_template_args (&local_flags
, type
, stream
);
992 /* Add in template parameters when printing derivation info. */
993 add_template_parameters (local_flags
.local_typedefs
, type
);
994 cp_type_print_derivation_info (stream
, type
, &local_flags
);
996 /* This holds just the global typedefs and the template
998 semi_local_flags
.local_typedefs
999 = copy_typedef_hash (local_flags
.local_typedefs
);
1000 if (semi_local_flags
.local_typedefs
)
1001 make_cleanup_free_typedef_hash (semi_local_flags
.local_typedefs
);
1003 /* Now add in the local typedefs. */
1004 recursively_update_typedef_hash (local_flags
.local_typedefs
, type
);
1006 fprintf_filtered (stream
, "{\n");
1007 if (TYPE_NFIELDS (type
) == 0 && TYPE_NFN_FIELDS (type
) == 0
1008 && TYPE_TYPEDEF_FIELD_COUNT (type
) == 0)
1010 if (TYPE_STUB (type
))
1011 fprintfi_filtered (level
+ 4, stream
,
1012 _("<incomplete type>\n"));
1014 fprintfi_filtered (level
+ 4, stream
,
1015 _("<no data fields>\n"));
1018 /* Start off with no specific section type, so we can print
1019 one for the first field we find, and use that section type
1020 thereafter until we find another type. */
1022 enum access_specifier section_type
= s_none
;
1024 /* For a class, if all members are private, there's no need
1025 for a "private:" label; similarly, for a struct or union
1026 masquerading as a class, if all members are public, there's
1027 no need for a "public:" label. */
1028 bool need_access_label
= need_access_label_p (type
);
1030 /* If there is a base class for this type,
1031 do not print the field that it occupies. */
1033 int len
= TYPE_NFIELDS (type
);
1034 vptr_fieldno
= get_vptr_fieldno (type
, &basetype
);
1035 for (int i
= TYPE_N_BASECLASSES (type
); i
< len
; i
++)
1039 /* If we have a virtual table pointer, omit it. Even if
1040 virtual table pointers are not specifically marked in
1041 the debug info, they should be artificial. */
1042 if ((i
== vptr_fieldno
&& type
== basetype
)
1043 || TYPE_FIELD_ARTIFICIAL (type
, i
))
1046 if (need_access_label
)
1048 section_type
= output_access_specifier
1049 (stream
, section_type
, level
,
1050 TYPE_FIELD_PROTECTED (type
, i
),
1051 TYPE_FIELD_PRIVATE (type
, i
));
1054 print_spaces_filtered (level
+ 4, stream
);
1055 if (field_is_static (&TYPE_FIELD (type
, i
)))
1056 fprintf_filtered (stream
, "static ");
1057 c_print_type (TYPE_FIELD_TYPE (type
, i
),
1058 TYPE_FIELD_NAME (type
, i
),
1059 stream
, show
- 1, level
+ 4,
1061 if (!field_is_static (&TYPE_FIELD (type
, i
))
1062 && TYPE_FIELD_PACKED (type
, i
))
1064 /* It is a bitfield. This code does not attempt
1065 to look at the bitpos and reconstruct filler,
1066 unnamed fields. This would lead to misleading
1067 results if the compiler does not put out fields
1068 for such things (I don't know what it does). */
1069 fprintf_filtered (stream
, " : %d",
1070 TYPE_FIELD_BITSIZE (type
, i
));
1072 fprintf_filtered (stream
, ";\n");
1075 /* If there are both fields and methods, put a blank line
1076 between them. Make sure to count only method that we
1077 will display; artificial methods will be hidden. */
1078 len
= TYPE_NFN_FIELDS (type
);
1079 if (!flags
->print_methods
)
1082 for (int i
= 0; i
< len
; i
++)
1084 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
1085 int len2
= TYPE_FN_FIELDLIST_LENGTH (type
, i
);
1088 for (j
= 0; j
< len2
; j
++)
1089 if (!TYPE_FN_FIELD_ARTIFICIAL (f
, j
))
1092 if (real_len
> 0 && section_type
!= s_none
)
1093 fprintf_filtered (stream
, "\n");
1095 /* C++: print out the methods. */
1096 for (int i
= 0; i
< len
; i
++)
1098 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
1099 int j
, len2
= TYPE_FN_FIELDLIST_LENGTH (type
, i
);
1100 const char *method_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
1101 const char *name
= type_name_no_tag (type
);
1102 int is_constructor
= name
&& strcmp (method_name
,
1105 for (j
= 0; j
< len2
; j
++)
1107 const char *mangled_name
;
1108 gdb::unique_xmalloc_ptr
<char> mangled_name_holder
;
1109 char *demangled_name
;
1110 const char *physname
= TYPE_FN_FIELD_PHYSNAME (f
, j
);
1111 int is_full_physname_constructor
=
1112 TYPE_FN_FIELD_CONSTRUCTOR (f
, j
)
1113 || is_constructor_name (physname
)
1114 || is_destructor_name (physname
)
1115 || method_name
[0] == '~';
1117 /* Do not print out artificial methods. */
1118 if (TYPE_FN_FIELD_ARTIFICIAL (f
, j
))
1122 section_type
= output_access_specifier
1123 (stream
, section_type
, level
,
1124 TYPE_FN_FIELD_PROTECTED (f
, j
),
1125 TYPE_FN_FIELD_PRIVATE (f
, j
));
1127 print_spaces_filtered (level
+ 4, stream
);
1128 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
1129 fprintf_filtered (stream
, "virtual ");
1130 else if (TYPE_FN_FIELD_STATIC_P (f
, j
))
1131 fprintf_filtered (stream
, "static ");
1132 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)) == 0)
1134 /* Keep GDB from crashing here. */
1135 fprintf_filtered (stream
,
1136 _("<undefined type> %s;\n"),
1137 TYPE_FN_FIELD_PHYSNAME (f
, j
));
1140 else if (!is_constructor
/* Constructors don't
1143 && !is_full_physname_constructor
/* " " */
1144 && !is_type_conversion_operator (type
, i
, j
))
1146 c_print_type (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f
, j
)),
1149 fputs_filtered (" ", stream
);
1151 if (TYPE_FN_FIELD_STUB (f
, j
))
1153 /* Build something we can demangle. */
1154 mangled_name_holder
.reset (gdb_mangle_name (type
, i
, j
));
1155 mangled_name
= mangled_name_holder
.get ();
1158 mangled_name
= TYPE_FN_FIELD_PHYSNAME (f
, j
);
1161 gdb_demangle (mangled_name
,
1162 DMGL_ANSI
| DMGL_PARAMS
);
1163 if (demangled_name
== NULL
)
1165 /* In some cases (for instance with the HP
1166 demangling), if a function has more than 10
1167 arguments, the demangling will fail.
1168 Let's try to reconstruct the function
1169 signature from the symbol information. */
1170 if (!TYPE_FN_FIELD_STUB (f
, j
))
1172 int staticp
= TYPE_FN_FIELD_STATIC_P (f
, j
);
1173 struct type
*mtype
= TYPE_FN_FIELD_TYPE (f
, j
);
1175 cp_type_print_method_args (mtype
,
1179 stream
, &local_flags
);
1182 fprintf_filtered (stream
,
1183 _("<badly mangled name '%s'>"),
1189 char *demangled_no_class
1190 = remove_qualifiers (demangled_name
);
1192 /* Get rid of the `static' appended by the
1194 p
= strstr (demangled_no_class
, " static");
1197 int length
= p
- demangled_no_class
;
1198 char *demangled_no_static
;
1201 = (char *) xmalloc (length
+ 1);
1202 strncpy (demangled_no_static
,
1203 demangled_no_class
, length
);
1204 *(demangled_no_static
+ length
) = '\0';
1205 fputs_filtered (demangled_no_static
, stream
);
1206 xfree (demangled_no_static
);
1209 fputs_filtered (demangled_no_class
, stream
);
1210 xfree (demangled_name
);
1213 fprintf_filtered (stream
, ";\n");
1217 /* Print out nested types. */
1218 if (TYPE_NESTED_TYPES_COUNT (type
) != 0
1219 && semi_local_flags
.print_nested_type_limit
!= 0)
1221 if (semi_local_flags
.print_nested_type_limit
> 0)
1222 --semi_local_flags
.print_nested_type_limit
;
1224 if (TYPE_NFIELDS (type
) != 0 || TYPE_NFN_FIELDS (type
) != 0)
1225 fprintf_filtered (stream
, "\n");
1227 for (int i
= 0; i
< TYPE_NESTED_TYPES_COUNT (type
); ++i
)
1229 print_spaces_filtered (level
+ 4, stream
);
1230 c_print_type (TYPE_NESTED_TYPES_FIELD_TYPE (type
, i
),
1231 "", stream
, show
, level
+ 4, &semi_local_flags
);
1232 fprintf_filtered (stream
, ";\n");
1236 /* Print typedefs defined in this class. */
1238 if (TYPE_TYPEDEF_FIELD_COUNT (type
) != 0 && flags
->print_typedefs
)
1240 if (TYPE_NFIELDS (type
) != 0 || TYPE_NFN_FIELDS (type
) != 0
1241 || TYPE_NESTED_TYPES_COUNT (type
) != 0)
1242 fprintf_filtered (stream
, "\n");
1244 for (int i
= 0; i
< TYPE_TYPEDEF_FIELD_COUNT (type
); i
++)
1246 struct type
*target
= TYPE_TYPEDEF_FIELD_TYPE (type
, i
);
1248 /* Dereference the typedef declaration itself. */
1249 gdb_assert (TYPE_CODE (target
) == TYPE_CODE_TYPEDEF
);
1250 target
= TYPE_TARGET_TYPE (target
);
1252 if (need_access_label
)
1254 section_type
= output_access_specifier
1255 (stream
, section_type
, level
,
1256 TYPE_TYPEDEF_FIELD_PROTECTED (type
, i
),
1257 TYPE_TYPEDEF_FIELD_PRIVATE (type
, i
));
1259 print_spaces_filtered (level
+ 4, stream
);
1260 fprintf_filtered (stream
, "typedef ");
1262 /* We want to print typedefs with substitutions
1263 from the template parameters or globally-known
1264 typedefs but not local typedefs. */
1265 c_print_type (target
,
1266 TYPE_TYPEDEF_FIELD_NAME (type
, i
),
1267 stream
, show
- 1, level
+ 4,
1269 fprintf_filtered (stream
, ";\n");
1273 fprintfi_filtered (level
, stream
, "}");
1276 do_cleanups (local_cleanups
);
1279 /* Print the name of the type (or the ultimate pointer target,
1280 function value or array element), or the description of a structure
1283 SHOW positive means print details about the type (e.g. enum
1284 values), and print structure elements passing SHOW - 1 for show.
1286 SHOW negative means just print the type name or struct tag if there
1287 is one. If there is no name, print something sensible but concise
1288 like "struct {...}".
1290 SHOW zero means just print the type name or struct tag if there is
1291 one. If there is no name, print something sensible but not as
1292 concise like "struct {int x; int y;}".
1294 LEVEL is the number of spaces to indent by.
1295 We increase it for some recursive calls. */
1298 c_type_print_base (struct type
*type
, struct ui_file
*stream
,
1299 int show
, int level
, const struct type_print_options
*flags
)
1308 fputs_filtered (_("<type unknown>"), stream
);
1312 /* When SHOW is zero or less, and there is a valid type name, then
1313 always just print the type name directly from the type. */
1314 /* If we have "typedef struct foo {. . .} bar;" do we want to print
1315 it as "struct foo" or as "bar"? Pick the latter, because C++
1316 folk tend to expect things like "class5 *foo" rather than "struct
1320 && TYPE_NAME (type
) != NULL
)
1322 c_type_print_modifier (type
, stream
, 0, 1);
1323 print_name_maybe_canonical (TYPE_NAME (type
), flags
, stream
);
1327 type
= check_typedef (type
);
1329 switch (TYPE_CODE (type
))
1331 case TYPE_CODE_TYPEDEF
:
1332 /* If we get here, the typedef doesn't have a name, and we
1333 couldn't resolve TYPE_TARGET_TYPE. Not much we can do. */
1334 gdb_assert (TYPE_NAME (type
) == NULL
);
1335 gdb_assert (TYPE_TARGET_TYPE (type
) == NULL
);
1336 fprintf_filtered (stream
, _("<unnamed typedef>"));
1339 case TYPE_CODE_FUNC
:
1340 case TYPE_CODE_METHOD
:
1341 if (TYPE_TARGET_TYPE (type
) == NULL
)
1342 type_print_unknown_return_type (stream
);
1344 c_type_print_base (TYPE_TARGET_TYPE (type
),
1345 stream
, show
, level
, flags
);
1347 case TYPE_CODE_ARRAY
:
1349 case TYPE_CODE_MEMBERPTR
:
1351 case TYPE_CODE_RVALUE_REF
:
1352 case TYPE_CODE_METHODPTR
:
1353 c_type_print_base (TYPE_TARGET_TYPE (type
),
1354 stream
, show
, level
, flags
);
1357 case TYPE_CODE_STRUCT
:
1358 case TYPE_CODE_UNION
:
1359 c_type_print_base_struct_union (type
, stream
, show
, level
, flags
);
1362 case TYPE_CODE_ENUM
:
1363 c_type_print_modifier (type
, stream
, 0, 1);
1364 fprintf_filtered (stream
, "enum ");
1365 if (TYPE_DECLARED_CLASS (type
))
1366 fprintf_filtered (stream
, "class ");
1367 /* Print the tag name if it exists.
1368 The aCC compiler emits a spurious
1369 "{unnamed struct}"/"{unnamed union}"/"{unnamed enum}"
1370 tag for unnamed struct/union/enum's, which we don't
1372 if (TYPE_TAG_NAME (type
) != NULL
1373 && !startswith (TYPE_TAG_NAME (type
), "{unnamed"))
1375 print_name_maybe_canonical (TYPE_TAG_NAME (type
), flags
, stream
);
1377 fputs_filtered (" ", stream
);
1383 /* If we just printed a tag name, no need to print anything
1385 if (TYPE_TAG_NAME (type
) == NULL
)
1386 fprintf_filtered (stream
, "{...}");
1388 else if (show
> 0 || TYPE_TAG_NAME (type
) == NULL
)
1390 LONGEST lastval
= 0;
1392 /* We can't handle this case perfectly, as DWARF does not
1393 tell us whether or not the underlying type was specified
1394 in the source (and other debug formats don't provide this
1395 at all). We choose to print the underlying type, if it
1396 has a name, when in C++ on the theory that it's better to
1397 print too much than too little; but conversely not to
1398 print something egregiously outside the current
1399 language's syntax. */
1400 if (current_language
->la_language
== language_cplus
1401 && TYPE_TARGET_TYPE (type
) != NULL
)
1403 struct type
*underlying
= check_typedef (TYPE_TARGET_TYPE (type
));
1405 if (TYPE_NAME (underlying
) != NULL
)
1406 fprintf_filtered (stream
, ": %s ", TYPE_NAME (underlying
));
1409 fprintf_filtered (stream
, "{");
1410 len
= TYPE_NFIELDS (type
);
1411 for (i
= 0; i
< len
; i
++)
1415 fprintf_filtered (stream
, ", ");
1417 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
1418 if (lastval
!= TYPE_FIELD_ENUMVAL (type
, i
))
1420 fprintf_filtered (stream
, " = %s",
1421 plongest (TYPE_FIELD_ENUMVAL (type
, i
)));
1422 lastval
= TYPE_FIELD_ENUMVAL (type
, i
);
1426 fprintf_filtered (stream
, "}");
1430 case TYPE_CODE_FLAGS
:
1432 struct type_print_options local_flags
= *flags
;
1434 local_flags
.local_typedefs
= NULL
;
1436 c_type_print_modifier (type
, stream
, 0, 1);
1437 fprintf_filtered (stream
, "flag ");
1438 print_name_maybe_canonical (TYPE_NAME (type
), flags
, stream
);
1441 fputs_filtered (" ", stream
);
1442 fprintf_filtered (stream
, "{\n");
1443 if (TYPE_NFIELDS (type
) == 0)
1445 if (TYPE_STUB (type
))
1446 fprintfi_filtered (level
+ 4, stream
,
1447 _("<incomplete type>\n"));
1449 fprintfi_filtered (level
+ 4, stream
,
1450 _("<no data fields>\n"));
1452 len
= TYPE_NFIELDS (type
);
1453 for (i
= 0; i
< len
; i
++)
1456 print_spaces_filtered (level
+ 4, stream
);
1457 /* We pass "show" here and not "show - 1" to get enum types
1458 printed. There's no other way to see them. */
1459 c_print_type (TYPE_FIELD_TYPE (type
, i
),
1460 TYPE_FIELD_NAME (type
, i
),
1461 stream
, show
, level
+ 4,
1463 fprintf_filtered (stream
, " @%s",
1464 plongest (TYPE_FIELD_BITPOS (type
, i
)));
1465 if (TYPE_FIELD_BITSIZE (type
, i
) > 1)
1467 fprintf_filtered (stream
, "-%s",
1468 plongest (TYPE_FIELD_BITPOS (type
, i
)
1469 + TYPE_FIELD_BITSIZE (type
, i
)
1472 fprintf_filtered (stream
, ";\n");
1474 fprintfi_filtered (level
, stream
, "}");
1479 case TYPE_CODE_VOID
:
1480 fprintf_filtered (stream
, "void");
1483 case TYPE_CODE_UNDEF
:
1484 fprintf_filtered (stream
, _("struct <unknown>"));
1487 case TYPE_CODE_ERROR
:
1488 fprintf_filtered (stream
, "%s", TYPE_ERROR_NAME (type
));
1491 case TYPE_CODE_RANGE
:
1492 /* This should not occur. */
1493 fprintf_filtered (stream
, _("<range type>"));
1496 case TYPE_CODE_NAMESPACE
:
1497 fputs_filtered ("namespace ", stream
);
1498 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
1502 /* Handle types not explicitly handled by the other cases, such
1503 as fundamental types. For these, just print whatever the
1504 type name is, as recorded in the type itself. If there is no
1505 type name, then complain. */
1506 if (TYPE_NAME (type
) != NULL
)
1508 c_type_print_modifier (type
, stream
, 0, 1);
1509 print_name_maybe_canonical (TYPE_NAME (type
), flags
, stream
);
1513 /* At least for dump_symtab, it is important that this not
1515 fprintf_filtered (stream
, _("<invalid type code %d>"),