1 /* Support for printing C and C++ types for GDB, the GNU debugger.
2 Copyright (C) 1986-2024 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/>. */
19 #include "event-top.h"
20 #include "gdbsupport/gdb_obstack.h"
24 #include "expression.h"
31 #include "cli/cli-style.h"
32 #include "typeprint.h"
34 #include "cp-support.h"
36 static void c_type_print_varspec_suffix (struct type
*, struct ui_file
*, int,
39 const struct type_print_options
*);
41 static void c_type_print_varspec_prefix (struct type
*,
45 const struct type_print_options
*,
46 struct print_offset_data
*);
48 /* Print "const", "volatile", or address space modifiers. */
49 static void c_type_print_modifier (struct type
*,
51 int, int, enum language
);
53 static void c_type_print_base_1 (struct type
*type
, struct ui_file
*stream
,
54 int show
, int level
, enum language language
,
55 const struct type_print_options
*flags
,
56 struct print_offset_data
*podata
);
59 /* A callback function for cp_canonicalize_string_full that uses
60 typedef_hash_table::find_typedef. */
63 find_typedef_for_canonicalize (struct type
*t
, void *data
)
65 return typedef_hash_table::find_typedef
66 ((const struct type_print_options
*) data
, t
);
69 /* Print NAME on STREAM. If the 'raw' field of FLAGS is not set,
70 canonicalize NAME using the local typedefs first. */
73 print_name_maybe_canonical (const char *name
,
74 const struct type_print_options
*flags
,
75 struct ui_file
*stream
)
77 gdb::unique_xmalloc_ptr
<char> s
;
80 s
= cp_canonicalize_string_full (name
,
81 find_typedef_for_canonicalize
,
84 gdb_puts (s
!= nullptr ? s
.get () : name
, stream
);
89 /* Helper function for c_print_type. */
92 c_print_type_1 (struct type
*type
,
93 const char *varstring
,
94 struct ui_file
*stream
,
96 enum language language
,
97 const struct type_print_options
*flags
,
98 struct print_offset_data
*podata
)
103 const char *local_name
;
106 type
= check_typedef (type
);
108 local_name
= typedef_hash_table::find_typedef (flags
, type
);
109 code
= type
->code ();
110 if (local_name
!= NULL
)
112 c_type_print_modifier (type
, stream
, 0, 1, language
);
113 gdb_puts (local_name
, stream
);
114 if (varstring
!= NULL
&& *varstring
!= '\0')
115 gdb_puts (" ", stream
);
119 c_type_print_base_1 (type
, stream
, show
, level
, language
, flags
, podata
);
120 if ((varstring
!= NULL
&& *varstring
!= '\0')
121 /* Need a space if going to print stars or brackets;
122 but not if we will print just a type name. */
123 || ((show
> 0 || type
->name () == 0)
124 && (code
== TYPE_CODE_PTR
|| code
== TYPE_CODE_FUNC
125 || code
== TYPE_CODE_METHOD
126 || (code
== TYPE_CODE_ARRAY
127 && !type
->is_vector ())
128 || code
== TYPE_CODE_MEMBERPTR
129 || code
== TYPE_CODE_METHODPTR
130 || TYPE_IS_REFERENCE (type
))))
131 gdb_puts (" ", stream
);
132 need_post_space
= (varstring
!= NULL
&& strcmp (varstring
, "") != 0);
133 c_type_print_varspec_prefix (type
, stream
, show
, 0, need_post_space
,
134 language
, flags
, podata
);
137 if (varstring
!= NULL
)
139 if (code
== TYPE_CODE_FUNC
|| code
== TYPE_CODE_METHOD
)
140 fputs_styled (varstring
, function_name_style
.style (), stream
);
142 fputs_styled (varstring
, variable_name_style
.style (), stream
);
144 /* For demangled function names, we have the arglist as part of
145 the name, so don't print an additional pair of ()'s. */
146 if (local_name
== NULL
)
148 demangled_args
= strchr (varstring
, '(') != NULL
;
149 c_type_print_varspec_suffix (type
, stream
, show
,
159 c_print_type (struct type
*type
,
160 const char *varstring
,
161 struct ui_file
*stream
,
163 enum language language
,
164 const struct type_print_options
*flags
)
166 struct print_offset_data
podata (flags
);
168 c_print_type_1 (type
, varstring
, stream
, show
, level
, language
, flags
,
172 /* Print a typedef using C syntax. TYPE is the underlying type.
173 NEW_SYMBOL is the symbol naming the type. STREAM is the stream on
177 c_print_typedef (struct type
*type
,
178 struct symbol
*new_symbol
,
179 struct ui_file
*stream
)
181 type
= check_typedef (type
);
182 gdb_printf (stream
, "typedef ");
183 type_print (type
, "", stream
, -1);
184 if ((new_symbol
->type ())->name () == 0
185 || strcmp ((new_symbol
->type ())->name (),
186 new_symbol
->linkage_name ()) != 0
187 || new_symbol
->type ()->code () == TYPE_CODE_TYPEDEF
)
188 gdb_printf (stream
, " %s", new_symbol
->print_name ());
189 gdb_printf (stream
, ";");
192 /* If TYPE is a derived type, then print out derivation information.
193 Print only the actual base classes of this type, not the base
194 classes of the base classes. I.e. for the derivation hierarchy:
197 class B : public A {int b; };
198 class C : public B {int c; };
200 Print the type of class C as:
206 Not as the following (like gdb used to), which is not legal C++
207 syntax for derived types and may be confused with the multiple
210 class C : public B : public A {
214 In general, gdb should try to print the types as closely as
215 possible to the form that they appear in the source code. */
218 cp_type_print_derivation_info (struct ui_file
*stream
,
220 const struct type_print_options
*flags
)
225 for (i
= 0; i
< TYPE_N_BASECLASSES (type
); i
++)
227 stream
->wrap_here (8);
228 gdb_puts (i
== 0 ? ": " : ", ", stream
);
229 gdb_printf (stream
, "%s%s ",
230 BASETYPE_VIA_PUBLIC (type
, i
)
231 ? "public" : (type
->field (i
).is_protected ()
232 ? "protected" : "private"),
233 BASETYPE_VIA_VIRTUAL (type
, i
) ? " virtual" : "");
234 name
= TYPE_BASECLASS (type
, i
)->name ();
236 print_name_maybe_canonical (name
, flags
, stream
);
238 gdb_printf (stream
, "(null)");
242 gdb_puts (" ", stream
);
246 /* Print the C++ method arguments ARGS to the file STREAM. */
249 cp_type_print_method_args (struct type
*mtype
, const char *prefix
,
250 const char *varstring
, int staticp
,
251 struct ui_file
*stream
,
252 enum language language
,
253 const struct type_print_options
*flags
)
255 struct field
*args
= mtype
->fields ();
256 int nargs
= mtype
->num_fields ();
257 int varargs
= mtype
->has_varargs ();
260 fprintf_symbol (stream
, prefix
,
261 language_cplus
, DMGL_ANSI
);
262 fprintf_symbol (stream
, varstring
,
263 language_cplus
, DMGL_ANSI
);
264 gdb_puts ("(", stream
);
266 int printed_args
= 0;
267 for (i
= 0; i
< nargs
; ++i
)
269 if (i
== 0 && !staticp
)
271 /* Skip the class variable. We keep this here to accommodate older
272 compilers and debug formats which may not support artificial
277 struct field arg
= args
[i
];
278 /* Skip any artificial arguments. */
279 if (arg
.is_artificial ())
282 if (printed_args
> 0)
284 gdb_printf (stream
, ", ");
285 stream
->wrap_here (8);
288 c_print_type (arg
.type (), "", stream
, 0, 0, language
, flags
);
294 if (printed_args
== 0)
295 gdb_printf (stream
, "...");
297 gdb_printf (stream
, ", ...");
299 else if (printed_args
== 0)
301 if (language
== language_cplus
)
302 gdb_printf (stream
, "void");
305 gdb_printf (stream
, ")");
307 /* For non-static methods, read qualifiers from the type of
313 gdb_assert (nargs
> 0);
314 gdb_assert (args
[0].type ()->code () == TYPE_CODE_PTR
);
315 domain
= args
[0].type ()->target_type ();
317 if (TYPE_CONST (domain
))
318 gdb_printf (stream
, " const");
320 if (TYPE_VOLATILE (domain
))
321 gdb_printf (stream
, " volatile");
323 if (TYPE_RESTRICT (domain
))
324 gdb_printf (stream
, (language
== language_cplus
328 if (TYPE_ATOMIC (domain
))
329 gdb_printf (stream
, " _Atomic");
334 /* Print any asterisks or open-parentheses needed before the
335 variable name (to describe its type).
337 On outermost call, pass 0 for PASSED_A_PTR.
338 On outermost call, SHOW > 0 means should ignore
339 any typename for TYPE and show its details.
340 SHOW is always zero on recursive calls.
342 NEED_POST_SPACE is non-zero when a space will be be needed
343 between a trailing qualifier and a field, variable, or function
347 c_type_print_varspec_prefix (struct type
*type
,
348 struct ui_file
*stream
,
349 int show
, int passed_a_ptr
,
351 enum language language
,
352 const struct type_print_options
*flags
,
353 struct print_offset_data
*podata
)
360 if (type
->name () && show
<= 0)
365 switch (type
->code ())
368 c_type_print_varspec_prefix (type
->target_type (),
369 stream
, show
, 1, 1, language
, flags
,
371 gdb_printf (stream
, "*");
372 c_type_print_modifier (type
, stream
, 1, need_post_space
, language
);
375 case TYPE_CODE_MEMBERPTR
:
376 c_type_print_varspec_prefix (type
->target_type (),
377 stream
, show
, 0, 0, language
, flags
, podata
);
378 name
= TYPE_SELF_TYPE (type
)->name ();
380 print_name_maybe_canonical (name
, flags
, stream
);
382 c_type_print_base_1 (TYPE_SELF_TYPE (type
),
383 stream
, -1, passed_a_ptr
, language
, flags
,
385 gdb_printf (stream
, "::*");
388 case TYPE_CODE_METHODPTR
:
389 c_type_print_varspec_prefix (type
->target_type (),
390 stream
, show
, 0, 0, language
, flags
,
392 gdb_printf (stream
, "(");
393 name
= TYPE_SELF_TYPE (type
)->name ();
395 print_name_maybe_canonical (name
, flags
, stream
);
397 c_type_print_base_1 (TYPE_SELF_TYPE (type
),
398 stream
, -1, passed_a_ptr
, language
, flags
,
400 gdb_printf (stream
, "::*");
404 case TYPE_CODE_RVALUE_REF
:
405 c_type_print_varspec_prefix (type
->target_type (),
406 stream
, show
, 1, 0, language
, flags
,
408 gdb_printf (stream
, type
->code () == TYPE_CODE_REF
? "&" : "&&");
409 c_type_print_modifier (type
, stream
, 1, need_post_space
, language
);
412 case TYPE_CODE_METHOD
:
414 c_type_print_varspec_prefix (type
->target_type (),
415 stream
, show
, 0, 0, language
, flags
,
418 gdb_printf (stream
, "(");
421 case TYPE_CODE_ARRAY
:
422 c_type_print_varspec_prefix (type
->target_type (),
423 stream
, show
, 0, need_post_space
,
424 language
, flags
, podata
);
426 gdb_printf (stream
, "(");
429 case TYPE_CODE_TYPEDEF
:
430 c_type_print_varspec_prefix (type
->target_type (),
431 stream
, show
, passed_a_ptr
, 0,
432 language
, flags
, podata
);
437 /* Print out "const" and "volatile" attributes,
438 and address space id if present.
439 TYPE is a pointer to the type being printed out.
440 STREAM is the output destination.
441 NEED_PRE_SPACE = 1 indicates an initial white space is needed.
442 NEED_POST_SPACE = 1 indicates a final white space is needed. */
445 c_type_print_modifier (struct type
*type
, struct ui_file
*stream
,
446 int need_pre_space
, int need_post_space
,
447 enum language language
)
449 int did_print_modifier
= 0;
450 const char *address_space_id
;
452 /* We don't print `const' qualifiers for references --- since all
453 operators affect the thing referenced, not the reference itself,
454 every reference is `const'. */
455 if (TYPE_CONST (type
) && !TYPE_IS_REFERENCE (type
))
458 gdb_printf (stream
, " ");
459 gdb_printf (stream
, "const");
460 did_print_modifier
= 1;
463 if (TYPE_VOLATILE (type
))
465 if (did_print_modifier
|| need_pre_space
)
466 gdb_printf (stream
, " ");
467 gdb_printf (stream
, "volatile");
468 did_print_modifier
= 1;
471 if (TYPE_RESTRICT (type
))
473 if (did_print_modifier
|| need_pre_space
)
474 gdb_printf (stream
, " ");
475 gdb_printf (stream
, (language
== language_cplus
478 did_print_modifier
= 1;
481 if (TYPE_ATOMIC (type
))
483 if (did_print_modifier
|| need_pre_space
)
484 gdb_printf (stream
, " ");
485 gdb_printf (stream
, "_Atomic");
486 did_print_modifier
= 1;
490 = address_space_type_instance_flags_to_name (type
->arch (),
491 type
->instance_flags ());
492 if (address_space_id
)
494 if (did_print_modifier
|| need_pre_space
)
495 gdb_printf (stream
, " ");
496 gdb_printf (stream
, "@%s", address_space_id
);
497 did_print_modifier
= 1;
500 if (did_print_modifier
&& need_post_space
)
501 gdb_printf (stream
, " ");
505 /* Print out the arguments of TYPE, which should have TYPE_CODE_METHOD
506 or TYPE_CODE_FUNC, to STREAM. Artificial arguments, such as "this"
507 in non-static methods, are displayed if LINKAGE_NAME is zero. If
508 LINKAGE_NAME is non-zero and LANGUAGE is language_cplus the topmost
509 parameter types get removed their possible const and volatile qualifiers to
510 match demangled linkage name parameters part of such function type.
511 LANGUAGE is the language in which TYPE was defined. This is a necessary
512 evil since this code is used by the C and C++. */
515 c_type_print_args (struct type
*type
, struct ui_file
*stream
,
516 int linkage_name
, enum language language
,
517 const struct type_print_options
*flags
)
522 gdb_printf (stream
, "(");
524 for (i
= 0; i
< type
->num_fields (); i
++)
526 struct type
*param_type
;
528 if (type
->field (i
).is_artificial () && linkage_name
)
533 gdb_printf (stream
, ", ");
534 stream
->wrap_here (4);
537 param_type
= type
->field (i
).type ();
539 if (language
== language_cplus
&& linkage_name
)
541 /* C++ standard, 13.1 Overloadable declarations, point 3, item:
542 - Parameter declarations that differ only in the presence or
543 absence of const and/or volatile are equivalent.
545 And the const/volatile qualifiers are not present in the mangled
546 names as produced by GCC. */
548 param_type
= make_cv_type (0, 0, param_type
, NULL
);
551 c_print_type (param_type
, "", stream
, -1, 0, language
, flags
);
555 if (printed_any
&& type
->has_varargs ())
557 /* Print out a trailing ellipsis for varargs functions. Ignore
558 TYPE_VARARGS if the function has no named arguments; that
559 represents unprototyped (K&R style) C functions. */
560 if (printed_any
&& type
->has_varargs ())
562 gdb_printf (stream
, ", ");
563 stream
->wrap_here (4);
564 gdb_printf (stream
, "...");
567 else if (!printed_any
568 && (type
->is_prototyped () || language
== language_cplus
))
569 gdb_printf (stream
, "void");
571 gdb_printf (stream
, ")");
574 /* Return true iff the j'th overloading of the i'th method of TYPE
575 is a type conversion operator, like `operator int () { ... }'.
576 When listing a class's methods, we don't print the return type of
580 is_type_conversion_operator (struct type
*type
, int i
, int j
)
582 /* I think the whole idea of recognizing type conversion operators
583 by their name is pretty terrible. But I don't think our present
584 data structure gives us any other way to tell. If you know of
585 some other way, feel free to rewrite this function. */
586 const char *name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
588 if (!startswith (name
, CP_OPERATOR_STR
))
592 if (! strchr (" \t\f\n\r", *name
))
595 while (strchr (" \t\f\n\r", *name
))
598 if (!('a' <= *name
&& *name
<= 'z')
599 && !('A' <= *name
&& *name
<= 'Z')
601 /* If this doesn't look like the start of an identifier, then it
602 isn't a type conversion operator. */
604 else if (startswith (name
, "new"))
606 else if (startswith (name
, "delete"))
609 /* If it doesn't look like new or delete, it's a type conversion
613 /* Is that really the end of the name? */
614 if (('a' <= *name
&& *name
<= 'z')
615 || ('A' <= *name
&& *name
<= 'Z')
616 || ('0' <= *name
&& *name
<= '9')
618 /* No, so the identifier following "operator" must be a type name,
619 and this is a type conversion operator. */
622 /* That was indeed the end of the name, so it was `operator new' or
623 `operator delete', neither of which are type conversion
628 /* Given a C++ qualified identifier QID, strip off the qualifiers,
629 yielding the unqualified name. The return value is a pointer into
632 It's a pity we don't have this information in some more structured
633 form. Even the author of this function feels that writing little
634 parsers like this everywhere is stupid. */
637 remove_qualifiers (const char *qid
)
639 int quoted
= 0; /* Zero if we're not in quotes;
640 '"' if we're in a double-quoted string;
641 '\'' if we're in a single-quoted string. */
642 int depth
= 0; /* Number of unclosed parens we've seen. */
643 char *parenstack
= (char *) alloca (strlen (qid
));
645 const char *last
= 0; /* The character after the rightmost
646 `::' token we've seen so far. */
648 for (scan
= qid
; *scan
; scan
++)
654 else if (*scan
== '\\' && *(scan
+ 1))
657 else if (scan
[0] == ':' && scan
[1] == ':')
659 /* If we're inside parenthesis (i.e., an argument list) or
660 angle brackets (i.e., a list of template arguments), then
661 we don't record the position of this :: token, since it's
662 not relevant to the top-level structure we're trying to
670 else if (*scan
== '"' || *scan
== '\'')
672 else if (*scan
== '(')
673 parenstack
[depth
++] = ')';
674 else if (*scan
== '[')
675 parenstack
[depth
++] = ']';
676 /* We're going to treat <> as a pair of matching characters,
677 since we're more likely to see those in template id's than
678 real less-than characters. What a crock. */
679 else if (*scan
== '<')
680 parenstack
[depth
++] = '>';
681 else if (*scan
== ')' || *scan
== ']' || *scan
== '>')
683 if (depth
> 0 && parenstack
[depth
- 1] == *scan
)
687 /* We're going to do a little error recovery here. If
688 we don't find a match for *scan on the paren stack,
689 but there is something lower on the stack that does
690 match, we pop the stack to that point. */
693 for (i
= depth
- 1; i
>= 0; i
--)
694 if (parenstack
[i
] == *scan
)
706 /* We didn't find any :: tokens at the top level, so declare the
707 whole thing an unqualified identifier. */
711 /* Print any array sizes, function arguments or close parentheses
712 needed after the variable name (to describe its type).
713 Args work like c_type_print_varspec_prefix. */
716 c_type_print_varspec_suffix (struct type
*type
,
717 struct ui_file
*stream
,
718 int show
, int passed_a_ptr
,
720 enum language language
,
721 const struct type_print_options
*flags
)
726 if (type
->name () && show
<= 0)
731 switch (type
->code ())
733 case TYPE_CODE_ARRAY
:
735 LONGEST low_bound
, high_bound
;
736 int is_vector
= type
->is_vector ();
739 gdb_printf (stream
, ")");
741 gdb_printf (stream
, (is_vector
?
742 " __attribute__ ((vector_size(" : "["));
743 /* Bounds are not yet resolved, print a bounds placeholder instead. */
744 if (type
->bounds ()->high
.kind () == PROP_LOCEXPR
745 || type
->bounds ()->high
.kind () == PROP_LOCLIST
)
746 gdb_printf (stream
, "variable length");
747 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
748 gdb_printf (stream
, "%s",
749 plongest (high_bound
- low_bound
+ 1));
750 gdb_printf (stream
, (is_vector
? ")))" : "]"));
752 c_type_print_varspec_suffix (type
->target_type (), stream
,
753 show
, 0, 0, language
, flags
);
757 case TYPE_CODE_MEMBERPTR
:
758 c_type_print_varspec_suffix (type
->target_type (), stream
,
759 show
, 0, 0, language
, flags
);
762 case TYPE_CODE_METHODPTR
:
763 gdb_printf (stream
, ")");
764 c_type_print_varspec_suffix (type
->target_type (), stream
,
765 show
, 0, 0, language
, flags
);
770 case TYPE_CODE_RVALUE_REF
:
771 c_type_print_varspec_suffix (type
->target_type (), stream
,
772 show
, 1, 0, language
, flags
);
775 case TYPE_CODE_METHOD
:
778 gdb_printf (stream
, ")");
780 c_type_print_args (type
, stream
, 0, language
, flags
);
781 c_type_print_varspec_suffix (type
->target_type (), stream
,
782 show
, passed_a_ptr
, 0, language
, flags
);
785 case TYPE_CODE_TYPEDEF
:
786 c_type_print_varspec_suffix (type
->target_type (), stream
,
787 show
, passed_a_ptr
, 0, language
, flags
);
792 /* A helper for c_type_print_base that displays template
793 parameters and their bindings, if needed.
795 TABLE is the local bindings table to use. If NULL, no printing is
796 done. Note that, at this point, TABLE won't have any useful
797 information in it -- but it is also used as a flag to
798 print_name_maybe_canonical to activate searching the global typedef
801 TYPE is the type whose template arguments are being displayed.
803 STREAM is the stream on which to print. */
806 c_type_print_template_args (const struct type_print_options
*flags
,
807 struct type
*type
, struct ui_file
*stream
,
808 enum language language
)
815 for (i
= 0; i
< TYPE_N_TEMPLATE_ARGUMENTS (type
); ++i
)
817 struct symbol
*sym
= TYPE_TEMPLATE_ARGUMENT (type
, i
);
819 if (sym
->aclass () != LOC_TYPEDEF
)
824 stream
->wrap_here (4);
825 gdb_printf (stream
, _("[with %s = "), sym
->linkage_name ());
830 gdb_puts (", ", stream
);
831 stream
->wrap_here (9);
832 gdb_printf (stream
, "%s = ", sym
->linkage_name ());
835 c_print_type (sym
->type (), "", stream
, -1, 0, language
, flags
);
839 gdb_puts (_("] "), stream
);
842 /* Use 'print_spaces', but take into consideration the
843 type_print_options FLAGS in order to determine how many whitespaces
847 print_spaces_filtered_with_print_options
848 (int level
, struct ui_file
*stream
, const struct type_print_options
*flags
)
850 if (!flags
->print_offsets
)
851 print_spaces (level
, stream
);
853 print_spaces (level
+ print_offset_data::indentation
, stream
);
856 /* Output an access specifier to STREAM, if needed. LAST_ACCESS is the
857 last access specifier output (typically returned by this function). */
860 output_access_specifier (struct ui_file
*stream
,
861 accessibility last_access
,
862 int level
, accessibility new_access
,
863 const struct type_print_options
*flags
)
865 if (last_access
== new_access
)
868 if (new_access
== accessibility::PROTECTED
)
870 print_spaces_filtered_with_print_options (level
+ 2, stream
, flags
);
871 gdb_printf (stream
, "protected:\n");
873 else if (new_access
== accessibility::PRIVATE
)
875 print_spaces_filtered_with_print_options (level
+ 2, stream
, flags
);
876 gdb_printf (stream
, "private:\n");
880 print_spaces_filtered_with_print_options (level
+ 2, stream
, flags
);
881 gdb_printf (stream
, "public:\n");
887 /* Helper function that temporarily disables FLAGS->PRINT_OFFSETS,
888 calls 'c_print_type_1', and then reenables FLAGS->PRINT_OFFSETS if
892 c_print_type_no_offsets (struct type
*type
,
893 const char *varstring
,
894 struct ui_file
*stream
,
896 enum language language
,
897 struct type_print_options
*flags
,
898 struct print_offset_data
*podata
)
900 unsigned int old_po
= flags
->print_offsets
;
902 /* Temporarily disable print_offsets, because it would mess with
904 flags
->print_offsets
= 0;
905 c_print_type_1 (type
, varstring
, stream
, show
, level
, language
, flags
,
907 flags
->print_offsets
= old_po
;
910 /* Helper for 'c_type_print_base' that handles structs and unions.
911 For a description of the arguments, see 'c_type_print_base'. */
914 c_type_print_base_struct_union (struct type
*type
, struct ui_file
*stream
,
916 enum language language
,
917 const struct type_print_options
*flags
,
918 struct print_offset_data
*podata
)
920 struct type_print_options local_flags
= *flags
;
921 local_flags
.local_typedefs
= NULL
;
923 std::unique_ptr
<typedef_hash_table
> hash_holder
;
926 if (flags
->local_typedefs
)
927 local_flags
.local_typedefs
928 = new typedef_hash_table (*flags
->local_typedefs
);
930 local_flags
.local_typedefs
= new typedef_hash_table ();
932 hash_holder
.reset (local_flags
.local_typedefs
);
935 c_type_print_modifier (type
, stream
, 0, 1, language
);
936 if (type
->code () == TYPE_CODE_UNION
)
937 gdb_printf (stream
, "union ");
938 else if (type
->is_declared_class ())
939 gdb_printf (stream
, "class ");
941 gdb_printf (stream
, "struct ");
943 /* Print the tag if it exists. The HP aCC compiler emits a
944 spurious "{unnamed struct}"/"{unnamed union}"/"{unnamed
945 enum}" tag for unnamed struct/union/enum's, which we don't
947 if (type
->name () != NULL
948 && !startswith (type
->name (), "{unnamed"))
950 /* When printing the tag name, we are still effectively
951 printing in the outer context, hence the use of FLAGS
953 print_name_maybe_canonical (type
->name (), flags
, stream
);
955 gdb_puts (" ", stream
);
960 /* If we just printed a tag name, no need to print anything
962 if (type
->name () == NULL
)
963 gdb_printf (stream
, "{...}");
965 else if (show
> 0 || type
->name () == NULL
)
967 struct type
*basetype
;
970 c_type_print_template_args (&local_flags
, type
, stream
, language
);
972 /* Add in template parameters when printing derivation info. */
973 if (local_flags
.local_typedefs
!= NULL
)
974 local_flags
.local_typedefs
->add_template_parameters (type
);
975 cp_type_print_derivation_info (stream
, type
, &local_flags
);
977 /* This holds just the global typedefs and the template
979 struct type_print_options semi_local_flags
= *flags
;
980 semi_local_flags
.local_typedefs
= NULL
;
982 std::unique_ptr
<typedef_hash_table
> semi_holder
;
983 if (local_flags
.local_typedefs
!= nullptr)
985 semi_local_flags
.local_typedefs
986 = new typedef_hash_table (*local_flags
.local_typedefs
);
987 semi_holder
.reset (semi_local_flags
.local_typedefs
);
989 /* Now add in the local typedefs. */
990 local_flags
.local_typedefs
->recursively_update (type
);
993 gdb_printf (stream
, "{\n");
995 if (type
->num_fields () == 0 && TYPE_NFN_FIELDS (type
) == 0
996 && TYPE_TYPEDEF_FIELD_COUNT (type
) == 0)
998 print_spaces_filtered_with_print_options (level
+ 4, stream
, flags
);
999 if (type
->is_stub ())
1000 gdb_printf (stream
, _("%p[<incomplete type>%p]\n"),
1001 metadata_style
.style ().ptr (), nullptr);
1003 gdb_printf (stream
, _("%p[<no data fields>%p]\n"),
1004 metadata_style
.style ().ptr (), nullptr);
1007 accessibility section_type
= (type
->is_declared_class ()
1008 ? accessibility::PRIVATE
1009 : accessibility::PUBLIC
);
1011 /* If there is a base class for this type,
1012 do not print the field that it occupies. */
1014 int len
= type
->num_fields ();
1015 vptr_fieldno
= get_vptr_fieldno (type
, &basetype
);
1017 struct print_offset_data
local_podata (flags
);
1019 for (int i
= TYPE_N_BASECLASSES (type
); i
< len
; i
++)
1023 /* If we have a virtual table pointer, omit it. Even if
1024 virtual table pointers are not specifically marked in
1025 the debug info, they should be artificial. */
1026 if ((i
== vptr_fieldno
&& type
== basetype
)
1027 || type
->field (i
).is_artificial ())
1031 = output_access_specifier (stream
, section_type
, level
,
1032 type
->field (i
).accessibility (),
1035 bool is_static
= type
->field (i
).is_static ();
1037 if (flags
->print_offsets
)
1038 podata
->update (type
, i
, stream
);
1040 print_spaces (level
+ 4, stream
);
1042 gdb_printf (stream
, "static ");
1044 int newshow
= show
- 1;
1046 if (!is_static
&& flags
->print_offsets
1047 && (type
->field (i
).type ()->code () == TYPE_CODE_STRUCT
1048 || type
->field (i
).type ()->code () == TYPE_CODE_UNION
))
1050 /* If we're printing offsets and this field's type is
1051 either a struct or an union, then we're interested in
1055 /* Make sure we carry our offset when we expand the
1057 local_podata
.offset_bitpos
1058 = podata
->offset_bitpos
+ type
->field (i
).loc_bitpos ();
1059 /* We're entering a struct/union. Right now,
1060 PODATA->END_BITPOS points right *after* the
1061 struct/union. However, when printing the first field
1062 of this inner struct/union, the end_bitpos we're
1063 expecting is exactly at the beginning of the
1064 struct/union. Therefore, we subtract the length of
1065 the whole struct/union. */
1066 local_podata
.end_bitpos
1067 = podata
->end_bitpos
1068 - type
->field (i
).type ()->length () * TARGET_CHAR_BIT
;
1071 c_print_type_1 (type
->field (i
).type (),
1072 type
->field (i
).name (),
1073 stream
, newshow
, level
+ 4,
1074 language
, &local_flags
, &local_podata
);
1076 if (!is_static
&& type
->field (i
).is_packed ())
1078 /* It is a bitfield. This code does not attempt
1079 to look at the bitpos and reconstruct filler,
1080 unnamed fields. This would lead to misleading
1081 results if the compiler does not put out fields
1082 for such things (I don't know what it does). */
1083 gdb_printf (stream
, " : %d", type
->field (i
).bitsize ());
1085 gdb_printf (stream
, ";\n");
1088 /* If there are both fields and methods, put a blank line
1089 between them. Make sure to count only method that we
1090 will display; artificial methods will be hidden. */
1091 len
= TYPE_NFN_FIELDS (type
);
1092 if (!flags
->print_methods
)
1095 for (int i
= 0; i
< len
; i
++)
1097 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
1098 int len2
= TYPE_FN_FIELDLIST_LENGTH (type
, i
);
1101 for (j
= 0; j
< len2
; j
++)
1102 if (!TYPE_FN_FIELD_ARTIFICIAL (f
, j
))
1106 gdb_printf (stream
, "\n");
1108 /* C++: print out the methods. */
1109 for (int i
= 0; i
< len
; i
++)
1111 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
1112 int j
, len2
= TYPE_FN_FIELDLIST_LENGTH (type
, i
);
1113 const char *method_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
1114 const char *name
= type
->name ();
1115 int is_constructor
= name
&& strcmp (method_name
,
1118 for (j
= 0; j
< len2
; j
++)
1120 const char *mangled_name
;
1121 gdb::unique_xmalloc_ptr
<char> mangled_name_holder
;
1122 const char *physname
= TYPE_FN_FIELD_PHYSNAME (f
, j
);
1123 int is_full_physname_constructor
=
1124 TYPE_FN_FIELD_CONSTRUCTOR (f
, j
)
1125 || is_constructor_name (physname
)
1126 || is_destructor_name (physname
)
1127 || method_name
[0] == '~';
1129 /* Do not print out artificial methods. */
1130 if (TYPE_FN_FIELD_ARTIFICIAL (f
, j
))
1134 section_type
= output_access_specifier
1135 (stream
, section_type
, level
,
1136 TYPE_FN_FIELD (f
, j
).accessibility
,
1139 print_spaces_filtered_with_print_options (level
+ 4, stream
,
1141 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
1142 gdb_printf (stream
, "virtual ");
1143 else if (TYPE_FN_FIELD_STATIC_P (f
, j
))
1144 gdb_printf (stream
, "static ");
1145 if (TYPE_FN_FIELD_TYPE (f
, j
)->target_type () == 0)
1147 /* Keep GDB from crashing here. */
1149 _("%p[<undefined type>%p] %s;\n"),
1150 metadata_style
.style ().ptr (), nullptr,
1151 TYPE_FN_FIELD_PHYSNAME (f
, j
));
1154 else if (!is_constructor
/* Constructors don't
1157 && !is_full_physname_constructor
/* " " */
1158 && !is_type_conversion_operator (type
, i
, j
))
1160 c_print_type_no_offsets
1161 (TYPE_FN_FIELD_TYPE (f
, j
)->target_type (),
1162 "", stream
, -1, 0, language
, &local_flags
, podata
);
1164 gdb_puts (" ", stream
);
1166 if (TYPE_FN_FIELD_STUB (f
, j
))
1168 /* Build something we can demangle. */
1169 mangled_name_holder
.reset (gdb_mangle_name (type
, i
, j
));
1170 mangled_name
= mangled_name_holder
.get ();
1173 mangled_name
= TYPE_FN_FIELD_PHYSNAME (f
, j
);
1175 gdb::unique_xmalloc_ptr
<char> demangled_name
1176 = gdb_demangle (mangled_name
,
1177 DMGL_ANSI
| DMGL_PARAMS
);
1178 if (demangled_name
== NULL
)
1180 /* In some cases (for instance with the HP
1181 demangling), if a function has more than 10
1182 arguments, the demangling will fail.
1183 Let's try to reconstruct the function
1184 signature from the symbol information. */
1185 if (!TYPE_FN_FIELD_STUB (f
, j
))
1187 int staticp
= TYPE_FN_FIELD_STATIC_P (f
, j
);
1188 struct type
*mtype
= TYPE_FN_FIELD_TYPE (f
, j
);
1190 cp_type_print_method_args (mtype
,
1198 fprintf_styled (stream
, metadata_style
.style (),
1199 _("<badly mangled name '%s'>"),
1205 const char *demangled_no_class
1206 = remove_qualifiers (demangled_name
.get ());
1208 /* Get rid of the `static' appended by the
1210 p
= strstr (demangled_no_class
, " static");
1213 int length
= p
- demangled_no_class
;
1214 std::string
demangled_no_static (demangled_no_class
,
1216 gdb_puts (demangled_no_static
.c_str (), stream
);
1219 gdb_puts (demangled_no_class
, stream
);
1222 gdb_printf (stream
, ";\n");
1226 /* Print out nested types. */
1227 if (TYPE_NESTED_TYPES_COUNT (type
) != 0
1228 && semi_local_flags
.print_nested_type_limit
!= 0)
1230 if (semi_local_flags
.print_nested_type_limit
> 0)
1231 --semi_local_flags
.print_nested_type_limit
;
1233 if (type
->num_fields () != 0 || TYPE_NFN_FIELDS (type
) != 0)
1234 gdb_printf (stream
, "\n");
1236 for (int i
= 0; i
< TYPE_NESTED_TYPES_COUNT (type
); ++i
)
1238 print_spaces_filtered_with_print_options (level
+ 4, stream
,
1240 c_print_type_no_offsets (TYPE_NESTED_TYPES_FIELD_TYPE (type
, i
),
1241 "", stream
, show
, level
+ 4,
1242 language
, &semi_local_flags
, podata
);
1243 gdb_printf (stream
, ";\n");
1247 /* Print typedefs defined in this class. */
1249 if (TYPE_TYPEDEF_FIELD_COUNT (type
) != 0 && flags
->print_typedefs
)
1251 if (type
->num_fields () != 0 || TYPE_NFN_FIELDS (type
) != 0
1252 || TYPE_NESTED_TYPES_COUNT (type
) != 0)
1253 gdb_printf (stream
, "\n");
1255 for (int i
= 0; i
< TYPE_TYPEDEF_FIELD_COUNT (type
); i
++)
1257 struct type
*target
= TYPE_TYPEDEF_FIELD_TYPE (type
, i
);
1259 /* Dereference the typedef declaration itself. */
1260 gdb_assert (target
->code () == TYPE_CODE_TYPEDEF
);
1261 target
= target
->target_type ();
1263 section_type
= (output_access_specifier
1264 (stream
, section_type
, level
,
1265 TYPE_TYPEDEF_FIELD (type
, i
).accessibility
,
1268 print_spaces_filtered_with_print_options (level
+ 4, stream
,
1270 gdb_printf (stream
, "typedef ");
1272 /* We want to print typedefs with substitutions
1273 from the template parameters or globally-known
1274 typedefs but not local typedefs. */
1275 c_print_type_no_offsets (target
,
1276 TYPE_TYPEDEF_FIELD_NAME (type
, i
),
1277 stream
, show
- 1, level
+ 4,
1278 language
, &semi_local_flags
, podata
);
1279 gdb_printf (stream
, ";\n");
1283 if (flags
->print_offsets
)
1286 podata
->finish (type
, level
, stream
);
1288 print_spaces (print_offset_data::indentation
, stream
);
1290 print_spaces (2, stream
);
1293 gdb_printf (stream
, "%*s}", level
, "");
1297 /* Print the name of the type (or the ultimate pointer target,
1298 function value or array element), or the description of a structure
1301 SHOW positive means print details about the type (e.g. enum
1302 values), and print structure elements passing SHOW - 1 for show.
1304 SHOW negative means just print the type name or struct tag if there
1305 is one. If there is no name, print something sensible but concise
1306 like "struct {...}".
1308 SHOW zero means just print the type name or struct tag if there is
1309 one. If there is no name, print something sensible but not as
1310 concise like "struct {int x; int y;}".
1312 LEVEL is the number of spaces to indent by.
1313 We increase it for some recursive calls. */
1316 c_type_print_base_1 (struct type
*type
, struct ui_file
*stream
,
1317 int show
, int level
,
1318 enum language language
,
1319 const struct type_print_options
*flags
,
1320 struct print_offset_data
*podata
)
1329 fputs_styled (_("<type unknown>"), metadata_style
.style (), stream
);
1333 /* When SHOW is zero or less, and there is a valid type name, then
1334 always just print the type name directly from the type. */
1337 && type
->name () != NULL
)
1339 c_type_print_modifier (type
, stream
, 0, 1, language
);
1341 /* If we have "typedef struct foo {. . .} bar;" do we want to
1342 print it as "struct foo" or as "bar"? Pick the latter for
1343 C++, because C++ folk tend to expect things like "class5
1344 *foo" rather than "struct class5 *foo". We rather
1345 arbitrarily choose to make language_minimal work in a C-like
1347 if (language
== language_c
|| language
== language_minimal
)
1349 if (type
->code () == TYPE_CODE_UNION
)
1350 gdb_printf (stream
, "union ");
1351 else if (type
->code () == TYPE_CODE_STRUCT
)
1353 if (type
->is_declared_class ())
1354 gdb_printf (stream
, "class ");
1356 gdb_printf (stream
, "struct ");
1358 else if (type
->code () == TYPE_CODE_ENUM
)
1359 gdb_printf (stream
, "enum ");
1362 print_name_maybe_canonical (type
->name (), flags
, stream
);
1366 type
= check_typedef (type
);
1368 switch (type
->code ())
1370 case TYPE_CODE_TYPEDEF
:
1371 /* If we get here, the typedef doesn't have a name, and we
1372 couldn't resolve type::target_type. Not much we can do. */
1373 gdb_assert (type
->name () == NULL
);
1374 gdb_assert (type
->target_type () == NULL
);
1375 fprintf_styled (stream
, metadata_style
.style (),
1376 _("<unnamed typedef>"));
1379 case TYPE_CODE_FUNC
:
1380 case TYPE_CODE_METHOD
:
1381 if (type
->target_type () == NULL
)
1382 type_print_unknown_return_type (stream
);
1384 c_type_print_base_1 (type
->target_type (),
1385 stream
, show
, level
, language
, flags
, podata
);
1387 case TYPE_CODE_ARRAY
:
1389 case TYPE_CODE_MEMBERPTR
:
1391 case TYPE_CODE_RVALUE_REF
:
1392 case TYPE_CODE_METHODPTR
:
1393 c_type_print_base_1 (type
->target_type (),
1394 stream
, show
, level
, language
, flags
, podata
);
1397 case TYPE_CODE_STRUCT
:
1398 case TYPE_CODE_UNION
:
1399 c_type_print_base_struct_union (type
, stream
, show
, level
,
1400 language
, flags
, podata
);
1403 case TYPE_CODE_ENUM
:
1404 c_type_print_modifier (type
, stream
, 0, 1, language
);
1405 gdb_printf (stream
, "enum ");
1406 if (type
->is_declared_class ())
1407 gdb_printf (stream
, "class ");
1408 /* Print the tag name if it exists.
1409 The aCC compiler emits a spurious
1410 "{unnamed struct}"/"{unnamed union}"/"{unnamed enum}"
1411 tag for unnamed struct/union/enum's, which we don't
1413 if (type
->name () != NULL
1414 && !startswith (type
->name (), "{unnamed"))
1416 print_name_maybe_canonical (type
->name (), flags
, stream
);
1418 gdb_puts (" ", stream
);
1421 stream
->wrap_here (4);
1424 /* If we just printed a tag name, no need to print anything
1426 if (type
->name () == NULL
)
1427 gdb_printf (stream
, "{...}");
1429 else if (show
> 0 || type
->name () == NULL
)
1431 LONGEST lastval
= 0;
1433 /* We can't handle this case perfectly, as DWARF does not
1434 tell us whether or not the underlying type was specified
1435 in the source (and other debug formats don't provide this
1436 at all). We choose to print the underlying type, if it
1437 has a name, when in C++ on the theory that it's better to
1438 print too much than too little; but conversely not to
1439 print something egregiously outside the current
1440 language's syntax. */
1441 if (language
== language_cplus
&& type
->target_type () != NULL
)
1443 struct type
*underlying
= check_typedef (type
->target_type ());
1445 if (underlying
->name () != NULL
)
1446 gdb_printf (stream
, ": %s ", underlying
->name ());
1449 gdb_printf (stream
, "{");
1450 len
= type
->num_fields ();
1451 for (i
= 0; i
< len
; i
++)
1455 gdb_printf (stream
, ", ");
1456 stream
->wrap_here (4);
1457 fputs_styled (type
->field (i
).name (),
1458 variable_name_style
.style (), stream
);
1459 if (lastval
!= type
->field (i
).loc_enumval ())
1461 gdb_printf (stream
, " = %s",
1462 plongest (type
->field (i
).loc_enumval ()));
1463 lastval
= type
->field (i
).loc_enumval ();
1467 gdb_printf (stream
, "}");
1471 case TYPE_CODE_FLAGS
:
1473 struct type_print_options local_flags
= *flags
;
1475 local_flags
.local_typedefs
= NULL
;
1477 c_type_print_modifier (type
, stream
, 0, 1, language
);
1478 gdb_printf (stream
, "flag ");
1479 print_name_maybe_canonical (type
->name (), flags
, stream
);
1482 gdb_puts (" ", stream
);
1483 gdb_printf (stream
, "{\n");
1484 if (type
->num_fields () == 0)
1486 if (type
->is_stub ())
1488 _("%*s%p[<incomplete type>%p]\n"),
1490 metadata_style
.style ().ptr (), nullptr);
1493 _("%*s%p[<no data fields>%p]\n"),
1495 metadata_style
.style ().ptr (), nullptr);
1497 len
= type
->num_fields ();
1498 for (i
= 0; i
< len
; i
++)
1501 print_spaces (level
+ 4, stream
);
1502 /* We pass "show" here and not "show - 1" to get enum types
1503 printed. There's no other way to see them. */
1504 c_print_type_1 (type
->field (i
).type (),
1505 type
->field (i
).name (),
1506 stream
, show
, level
+ 4,
1507 language
, &local_flags
, podata
);
1508 gdb_printf (stream
, " @%s",
1509 plongest (type
->field (i
).loc_bitpos ()));
1510 if (type
->field (i
).bitsize () > 1)
1512 gdb_printf (stream
, "-%s",
1513 plongest (type
->field (i
).loc_bitpos ()
1514 + type
->field (i
).bitsize ()
1517 gdb_printf (stream
, ";\n");
1519 gdb_printf (stream
, "%*s}", level
, "");
1524 case TYPE_CODE_VOID
:
1525 gdb_printf (stream
, "void");
1528 case TYPE_CODE_UNDEF
:
1529 gdb_printf (stream
, _("struct <unknown>"));
1532 case TYPE_CODE_ERROR
:
1533 gdb_printf (stream
, "%s", TYPE_ERROR_NAME (type
));
1536 case TYPE_CODE_RANGE
:
1537 /* This should not occur. */
1538 fprintf_styled (stream
, metadata_style
.style (), _("<range type>"));
1541 case TYPE_CODE_FIXED_POINT
:
1542 print_type_fixed_point (type
, stream
);
1545 case TYPE_CODE_NAMESPACE
:
1546 gdb_puts ("namespace ", stream
);
1547 gdb_puts (type
->name (), stream
);
1551 /* Handle types not explicitly handled by the other cases, such
1552 as fundamental types. For these, just print whatever the
1553 type name is, as recorded in the type itself. If there is no
1554 type name, then complain. */
1555 if (type
->name () != NULL
)
1557 c_type_print_modifier (type
, stream
, 0, 1, language
);
1558 print_name_maybe_canonical (type
->name (), flags
, stream
);
1562 /* At least for dump_symtab, it is important that this not
1564 fprintf_styled (stream
, metadata_style
.style (),
1565 _("<invalid type code %d>"), type
->code ());
1571 /* See c_type_print_base_1. */
1574 c_type_print_base (struct type
*type
, struct ui_file
*stream
,
1575 int show
, int level
,
1576 const struct type_print_options
*flags
)
1578 struct print_offset_data
podata (flags
);
1580 c_type_print_base_1 (type
, stream
, show
, level
,
1581 current_language
->la_language
, flags
, &podata
);