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"
23 #include "expression.h"
30 #include "cli/cli-style.h"
31 #include "typeprint.h"
33 #include "cp-support.h"
35 static void c_type_print_varspec_suffix (struct type
*, struct ui_file
*, int,
38 const struct type_print_options
*);
40 static void c_type_print_varspec_prefix (struct type
*,
44 const struct type_print_options
*,
45 struct print_offset_data
*);
47 /* Print "const", "volatile", or address space modifiers. */
48 static void c_type_print_modifier (struct type
*,
50 int, int, enum language
);
52 static void c_type_print_base_1 (struct type
*type
, struct ui_file
*stream
,
53 int show
, int level
, enum language language
,
54 const struct type_print_options
*flags
,
55 struct print_offset_data
*podata
);
58 /* A callback function for cp_canonicalize_string_full that uses
59 typedef_hash_table::find_typedef. */
62 find_typedef_for_canonicalize (struct type
*t
, void *data
)
64 return typedef_hash_table::find_typedef
65 ((const struct type_print_options
*) data
, t
);
68 /* Print NAME on STREAM. If the 'raw' field of FLAGS is not set,
69 canonicalize NAME using the local typedefs first. */
72 print_name_maybe_canonical (const char *name
,
73 const struct type_print_options
*flags
,
74 struct ui_file
*stream
)
76 gdb::unique_xmalloc_ptr
<char> s
;
79 s
= cp_canonicalize_string_full (name
,
80 find_typedef_for_canonicalize
,
83 gdb_puts (s
!= nullptr ? s
.get () : name
, stream
);
88 /* Helper function for c_print_type. */
91 c_print_type_1 (struct type
*type
,
92 const char *varstring
,
93 struct ui_file
*stream
,
95 enum language language
,
96 const struct type_print_options
*flags
,
97 struct print_offset_data
*podata
)
102 const char *local_name
;
105 type
= check_typedef (type
);
107 local_name
= typedef_hash_table::find_typedef (flags
, type
);
108 code
= type
->code ();
109 if (local_name
!= NULL
)
111 c_type_print_modifier (type
, stream
, 0, 1, language
);
112 gdb_puts (local_name
, stream
);
113 if (varstring
!= NULL
&& *varstring
!= '\0')
114 gdb_puts (" ", stream
);
118 c_type_print_base_1 (type
, stream
, show
, level
, language
, flags
, podata
);
119 if ((varstring
!= NULL
&& *varstring
!= '\0')
120 /* Need a space if going to print stars or brackets;
121 but not if we will print just a type name. */
122 || ((show
> 0 || type
->name () == 0)
123 && (code
== TYPE_CODE_PTR
|| code
== TYPE_CODE_FUNC
124 || code
== TYPE_CODE_METHOD
125 || (code
== TYPE_CODE_ARRAY
126 && !type
->is_vector ())
127 || code
== TYPE_CODE_MEMBERPTR
128 || code
== TYPE_CODE_METHODPTR
129 || TYPE_IS_REFERENCE (type
))))
130 gdb_puts (" ", stream
);
131 need_post_space
= (varstring
!= NULL
&& strcmp (varstring
, "") != 0);
132 c_type_print_varspec_prefix (type
, stream
, show
, 0, need_post_space
,
133 language
, flags
, podata
);
136 if (varstring
!= NULL
)
138 if (code
== TYPE_CODE_FUNC
|| code
== TYPE_CODE_METHOD
)
139 fputs_styled (varstring
, function_name_style
.style (), stream
);
141 fputs_styled (varstring
, variable_name_style
.style (), stream
);
143 /* For demangled function names, we have the arglist as part of
144 the name, so don't print an additional pair of ()'s. */
145 if (local_name
== NULL
)
147 demangled_args
= strchr (varstring
, '(') != NULL
;
148 c_type_print_varspec_suffix (type
, stream
, show
,
158 c_print_type (struct type
*type
,
159 const char *varstring
,
160 struct ui_file
*stream
,
162 enum language language
,
163 const struct type_print_options
*flags
)
165 struct print_offset_data
podata (flags
);
167 c_print_type_1 (type
, varstring
, stream
, show
, level
, language
, flags
,
171 /* Print a typedef using C syntax. TYPE is the underlying type.
172 NEW_SYMBOL is the symbol naming the type. STREAM is the stream on
176 c_print_typedef (struct type
*type
,
177 struct symbol
*new_symbol
,
178 struct ui_file
*stream
)
180 type
= check_typedef (type
);
181 gdb_printf (stream
, "typedef ");
182 type_print (type
, "", stream
, -1);
183 if ((new_symbol
->type ())->name () == 0
184 || strcmp ((new_symbol
->type ())->name (),
185 new_symbol
->linkage_name ()) != 0
186 || new_symbol
->type ()->code () == TYPE_CODE_TYPEDEF
)
187 gdb_printf (stream
, " %s", new_symbol
->print_name ());
188 gdb_printf (stream
, ";");
191 /* If TYPE is a derived type, then print out derivation information.
192 Print only the actual base classes of this type, not the base
193 classes of the base classes. I.e. for the derivation hierarchy:
196 class B : public A {int b; };
197 class C : public B {int c; };
199 Print the type of class C as:
205 Not as the following (like gdb used to), which is not legal C++
206 syntax for derived types and may be confused with the multiple
209 class C : public B : public A {
213 In general, gdb should try to print the types as closely as
214 possible to the form that they appear in the source code. */
217 cp_type_print_derivation_info (struct ui_file
*stream
,
219 const struct type_print_options
*flags
)
224 for (i
= 0; i
< TYPE_N_BASECLASSES (type
); i
++)
226 stream
->wrap_here (8);
227 gdb_puts (i
== 0 ? ": " : ", ", stream
);
228 gdb_printf (stream
, "%s%s ",
229 BASETYPE_VIA_PUBLIC (type
, i
)
230 ? "public" : (type
->field (i
).is_protected ()
231 ? "protected" : "private"),
232 BASETYPE_VIA_VIRTUAL (type
, i
) ? " virtual" : "");
233 name
= TYPE_BASECLASS (type
, i
)->name ();
235 print_name_maybe_canonical (name
, flags
, stream
);
237 gdb_printf (stream
, "(null)");
241 gdb_puts (" ", stream
);
245 /* Print the C++ method arguments ARGS to the file STREAM. */
248 cp_type_print_method_args (struct type
*mtype
, const char *prefix
,
249 const char *varstring
, int staticp
,
250 struct ui_file
*stream
,
251 enum language language
,
252 const struct type_print_options
*flags
)
254 struct field
*args
= mtype
->fields ();
255 int nargs
= mtype
->num_fields ();
256 int varargs
= mtype
->has_varargs ();
259 fprintf_symbol (stream
, prefix
,
260 language_cplus
, DMGL_ANSI
);
261 fprintf_symbol (stream
, varstring
,
262 language_cplus
, DMGL_ANSI
);
263 gdb_puts ("(", stream
);
265 int printed_args
= 0;
266 for (i
= 0; i
< nargs
; ++i
)
268 if (i
== 0 && !staticp
)
270 /* Skip the class variable. We keep this here to accommodate older
271 compilers and debug formats which may not support artificial
276 struct field arg
= args
[i
];
277 /* Skip any artificial arguments. */
278 if (arg
.is_artificial ())
281 if (printed_args
> 0)
283 gdb_printf (stream
, ", ");
284 stream
->wrap_here (8);
287 c_print_type (arg
.type (), "", stream
, 0, 0, language
, flags
);
293 if (printed_args
== 0)
294 gdb_printf (stream
, "...");
296 gdb_printf (stream
, ", ...");
298 else if (printed_args
== 0)
300 if (language
== language_cplus
)
301 gdb_printf (stream
, "void");
304 gdb_printf (stream
, ")");
306 /* For non-static methods, read qualifiers from the type of
312 gdb_assert (nargs
> 0);
313 gdb_assert (args
[0].type ()->code () == TYPE_CODE_PTR
);
314 domain
= args
[0].type ()->target_type ();
316 if (TYPE_CONST (domain
))
317 gdb_printf (stream
, " const");
319 if (TYPE_VOLATILE (domain
))
320 gdb_printf (stream
, " volatile");
322 if (TYPE_RESTRICT (domain
))
323 gdb_printf (stream
, (language
== language_cplus
327 if (TYPE_ATOMIC (domain
))
328 gdb_printf (stream
, " _Atomic");
333 /* Print any asterisks or open-parentheses needed before the
334 variable name (to describe its type).
336 On outermost call, pass 0 for PASSED_A_PTR.
337 On outermost call, SHOW > 0 means should ignore
338 any typename for TYPE and show its details.
339 SHOW is always zero on recursive calls.
341 NEED_POST_SPACE is non-zero when a space will be be needed
342 between a trailing qualifier and a field, variable, or function
346 c_type_print_varspec_prefix (struct type
*type
,
347 struct ui_file
*stream
,
348 int show
, int passed_a_ptr
,
350 enum language language
,
351 const struct type_print_options
*flags
,
352 struct print_offset_data
*podata
)
359 if (type
->name () && show
<= 0)
364 switch (type
->code ())
367 c_type_print_varspec_prefix (type
->target_type (),
368 stream
, show
, 1, 1, language
, flags
,
370 gdb_printf (stream
, "*");
371 c_type_print_modifier (type
, stream
, 1, need_post_space
, language
);
374 case TYPE_CODE_MEMBERPTR
:
375 c_type_print_varspec_prefix (type
->target_type (),
376 stream
, show
, 0, 0, language
, flags
, podata
);
377 name
= TYPE_SELF_TYPE (type
)->name ();
379 print_name_maybe_canonical (name
, flags
, stream
);
381 c_type_print_base_1 (TYPE_SELF_TYPE (type
),
382 stream
, -1, passed_a_ptr
, language
, flags
,
384 gdb_printf (stream
, "::*");
387 case TYPE_CODE_METHODPTR
:
388 c_type_print_varspec_prefix (type
->target_type (),
389 stream
, show
, 0, 0, language
, flags
,
391 gdb_printf (stream
, "(");
392 name
= TYPE_SELF_TYPE (type
)->name ();
394 print_name_maybe_canonical (name
, flags
, stream
);
396 c_type_print_base_1 (TYPE_SELF_TYPE (type
),
397 stream
, -1, passed_a_ptr
, language
, flags
,
399 gdb_printf (stream
, "::*");
403 case TYPE_CODE_RVALUE_REF
:
404 c_type_print_varspec_prefix (type
->target_type (),
405 stream
, show
, 1, 0, language
, flags
,
407 gdb_printf (stream
, type
->code () == TYPE_CODE_REF
? "&" : "&&");
408 c_type_print_modifier (type
, stream
, 1, need_post_space
, language
);
411 case TYPE_CODE_METHOD
:
413 c_type_print_varspec_prefix (type
->target_type (),
414 stream
, show
, 0, 0, language
, flags
,
417 gdb_printf (stream
, "(");
420 case TYPE_CODE_ARRAY
:
421 c_type_print_varspec_prefix (type
->target_type (),
422 stream
, show
, 0, need_post_space
,
423 language
, flags
, podata
);
425 gdb_printf (stream
, "(");
428 case TYPE_CODE_TYPEDEF
:
429 c_type_print_varspec_prefix (type
->target_type (),
430 stream
, show
, passed_a_ptr
, 0,
431 language
, flags
, podata
);
436 /* Print out "const" and "volatile" attributes,
437 and address space id if present.
438 TYPE is a pointer to the type being printed out.
439 STREAM is the output destination.
440 NEED_PRE_SPACE = 1 indicates an initial white space is needed.
441 NEED_POST_SPACE = 1 indicates a final white space is needed. */
444 c_type_print_modifier (struct type
*type
, struct ui_file
*stream
,
445 int need_pre_space
, int need_post_space
,
446 enum language language
)
448 int did_print_modifier
= 0;
449 const char *address_space_id
;
451 /* We don't print `const' qualifiers for references --- since all
452 operators affect the thing referenced, not the reference itself,
453 every reference is `const'. */
454 if (TYPE_CONST (type
) && !TYPE_IS_REFERENCE (type
))
457 gdb_printf (stream
, " ");
458 gdb_printf (stream
, "const");
459 did_print_modifier
= 1;
462 if (TYPE_VOLATILE (type
))
464 if (did_print_modifier
|| need_pre_space
)
465 gdb_printf (stream
, " ");
466 gdb_printf (stream
, "volatile");
467 did_print_modifier
= 1;
470 if (TYPE_RESTRICT (type
))
472 if (did_print_modifier
|| need_pre_space
)
473 gdb_printf (stream
, " ");
474 gdb_printf (stream
, (language
== language_cplus
477 did_print_modifier
= 1;
480 if (TYPE_ATOMIC (type
))
482 if (did_print_modifier
|| need_pre_space
)
483 gdb_printf (stream
, " ");
484 gdb_printf (stream
, "_Atomic");
485 did_print_modifier
= 1;
489 = address_space_type_instance_flags_to_name (type
->arch (),
490 type
->instance_flags ());
491 if (address_space_id
)
493 if (did_print_modifier
|| need_pre_space
)
494 gdb_printf (stream
, " ");
495 gdb_printf (stream
, "@%s", address_space_id
);
496 did_print_modifier
= 1;
499 if (did_print_modifier
&& need_post_space
)
500 gdb_printf (stream
, " ");
504 /* Print out the arguments of TYPE, which should have TYPE_CODE_METHOD
505 or TYPE_CODE_FUNC, to STREAM. Artificial arguments, such as "this"
506 in non-static methods, are displayed if LINKAGE_NAME is zero. If
507 LINKAGE_NAME is non-zero and LANGUAGE is language_cplus the topmost
508 parameter types get removed their possible const and volatile qualifiers to
509 match demangled linkage name parameters part of such function type.
510 LANGUAGE is the language in which TYPE was defined. This is a necessary
511 evil since this code is used by the C and C++. */
514 c_type_print_args (struct type
*type
, struct ui_file
*stream
,
515 int linkage_name
, enum language language
,
516 const struct type_print_options
*flags
)
521 gdb_printf (stream
, "(");
523 for (i
= 0; i
< type
->num_fields (); i
++)
525 struct type
*param_type
;
527 if (type
->field (i
).is_artificial () && linkage_name
)
532 gdb_printf (stream
, ", ");
533 stream
->wrap_here (4);
536 param_type
= type
->field (i
).type ();
538 if (language
== language_cplus
&& linkage_name
)
540 /* C++ standard, 13.1 Overloadable declarations, point 3, item:
541 - Parameter declarations that differ only in the presence or
542 absence of const and/or volatile are equivalent.
544 And the const/volatile qualifiers are not present in the mangled
545 names as produced by GCC. */
547 param_type
= make_cv_type (0, 0, param_type
, NULL
);
550 c_print_type (param_type
, "", stream
, -1, 0, language
, flags
);
554 if (printed_any
&& type
->has_varargs ())
556 /* Print out a trailing ellipsis for varargs functions. Ignore
557 TYPE_VARARGS if the function has no named arguments; that
558 represents unprototyped (K&R style) C functions. */
559 if (printed_any
&& type
->has_varargs ())
561 gdb_printf (stream
, ", ");
562 stream
->wrap_here (4);
563 gdb_printf (stream
, "...");
566 else if (!printed_any
567 && (type
->is_prototyped () || language
== language_cplus
))
568 gdb_printf (stream
, "void");
570 gdb_printf (stream
, ")");
573 /* Return true iff the j'th overloading of the i'th method of TYPE
574 is a type conversion operator, like `operator int () { ... }'.
575 When listing a class's methods, we don't print the return type of
579 is_type_conversion_operator (struct type
*type
, int i
, int j
)
581 /* I think the whole idea of recognizing type conversion operators
582 by their name is pretty terrible. But I don't think our present
583 data structure gives us any other way to tell. If you know of
584 some other way, feel free to rewrite this function. */
585 const char *name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
587 if (!startswith (name
, CP_OPERATOR_STR
))
591 if (! strchr (" \t\f\n\r", *name
))
594 while (strchr (" \t\f\n\r", *name
))
597 if (!('a' <= *name
&& *name
<= 'z')
598 && !('A' <= *name
&& *name
<= 'Z')
600 /* If this doesn't look like the start of an identifier, then it
601 isn't a type conversion operator. */
603 else if (startswith (name
, "new"))
605 else if (startswith (name
, "delete"))
608 /* If it doesn't look like new or delete, it's a type conversion
612 /* Is that really the end of the name? */
613 if (('a' <= *name
&& *name
<= 'z')
614 || ('A' <= *name
&& *name
<= 'Z')
615 || ('0' <= *name
&& *name
<= '9')
617 /* No, so the identifier following "operator" must be a type name,
618 and this is a type conversion operator. */
621 /* That was indeed the end of the name, so it was `operator new' or
622 `operator delete', neither of which are type conversion
627 /* Given a C++ qualified identifier QID, strip off the qualifiers,
628 yielding the unqualified name. The return value is a pointer into
631 It's a pity we don't have this information in some more structured
632 form. Even the author of this function feels that writing little
633 parsers like this everywhere is stupid. */
636 remove_qualifiers (const char *qid
)
638 int quoted
= 0; /* Zero if we're not in quotes;
639 '"' if we're in a double-quoted string;
640 '\'' if we're in a single-quoted string. */
641 int depth
= 0; /* Number of unclosed parens we've seen. */
642 char *parenstack
= (char *) alloca (strlen (qid
));
644 const char *last
= 0; /* The character after the rightmost
645 `::' token we've seen so far. */
647 for (scan
= qid
; *scan
; scan
++)
653 else if (*scan
== '\\' && *(scan
+ 1))
656 else if (scan
[0] == ':' && scan
[1] == ':')
658 /* If we're inside parenthesis (i.e., an argument list) or
659 angle brackets (i.e., a list of template arguments), then
660 we don't record the position of this :: token, since it's
661 not relevant to the top-level structure we're trying to
669 else if (*scan
== '"' || *scan
== '\'')
671 else if (*scan
== '(')
672 parenstack
[depth
++] = ')';
673 else if (*scan
== '[')
674 parenstack
[depth
++] = ']';
675 /* We're going to treat <> as a pair of matching characters,
676 since we're more likely to see those in template id's than
677 real less-than characters. What a crock. */
678 else if (*scan
== '<')
679 parenstack
[depth
++] = '>';
680 else if (*scan
== ')' || *scan
== ']' || *scan
== '>')
682 if (depth
> 0 && parenstack
[depth
- 1] == *scan
)
686 /* We're going to do a little error recovery here. If
687 we don't find a match for *scan on the paren stack,
688 but there is something lower on the stack that does
689 match, we pop the stack to that point. */
692 for (i
= depth
- 1; i
>= 0; i
--)
693 if (parenstack
[i
] == *scan
)
705 /* We didn't find any :: tokens at the top level, so declare the
706 whole thing an unqualified identifier. */
710 /* Print any array sizes, function arguments or close parentheses
711 needed after the variable name (to describe its type).
712 Args work like c_type_print_varspec_prefix. */
715 c_type_print_varspec_suffix (struct type
*type
,
716 struct ui_file
*stream
,
717 int show
, int passed_a_ptr
,
719 enum language language
,
720 const struct type_print_options
*flags
)
725 if (type
->name () && show
<= 0)
730 switch (type
->code ())
732 case TYPE_CODE_ARRAY
:
734 LONGEST low_bound
, high_bound
;
735 int is_vector
= type
->is_vector ();
738 gdb_printf (stream
, ")");
740 gdb_printf (stream
, (is_vector
?
741 " __attribute__ ((vector_size(" : "["));
742 /* Bounds are not yet resolved, print a bounds placeholder instead. */
743 if (type
->bounds ()->high
.kind () == PROP_LOCEXPR
744 || type
->bounds ()->high
.kind () == PROP_LOCLIST
)
745 gdb_printf (stream
, "variable length");
746 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
747 gdb_printf (stream
, "%s",
748 plongest (high_bound
- low_bound
+ 1));
749 gdb_printf (stream
, (is_vector
? ")))" : "]"));
751 c_type_print_varspec_suffix (type
->target_type (), stream
,
752 show
, 0, 0, language
, flags
);
756 case TYPE_CODE_MEMBERPTR
:
757 c_type_print_varspec_suffix (type
->target_type (), stream
,
758 show
, 0, 0, language
, flags
);
761 case TYPE_CODE_METHODPTR
:
762 gdb_printf (stream
, ")");
763 c_type_print_varspec_suffix (type
->target_type (), stream
,
764 show
, 0, 0, language
, flags
);
769 case TYPE_CODE_RVALUE_REF
:
770 c_type_print_varspec_suffix (type
->target_type (), stream
,
771 show
, 1, 0, language
, flags
);
774 case TYPE_CODE_METHOD
:
777 gdb_printf (stream
, ")");
779 c_type_print_args (type
, stream
, 0, language
, flags
);
780 c_type_print_varspec_suffix (type
->target_type (), stream
,
781 show
, passed_a_ptr
, 0, language
, flags
);
784 case TYPE_CODE_TYPEDEF
:
785 c_type_print_varspec_suffix (type
->target_type (), stream
,
786 show
, passed_a_ptr
, 0, language
, flags
);
791 /* A helper for c_type_print_base that displays template
792 parameters and their bindings, if needed.
794 TABLE is the local bindings table to use. If NULL, no printing is
795 done. Note that, at this point, TABLE won't have any useful
796 information in it -- but it is also used as a flag to
797 print_name_maybe_canonical to activate searching the global typedef
800 TYPE is the type whose template arguments are being displayed.
802 STREAM is the stream on which to print. */
805 c_type_print_template_args (const struct type_print_options
*flags
,
806 struct type
*type
, struct ui_file
*stream
,
807 enum language language
)
814 for (i
= 0; i
< TYPE_N_TEMPLATE_ARGUMENTS (type
); ++i
)
816 struct symbol
*sym
= TYPE_TEMPLATE_ARGUMENT (type
, i
);
818 if (sym
->aclass () != LOC_TYPEDEF
)
823 stream
->wrap_here (4);
824 gdb_printf (stream
, _("[with %s = "), sym
->linkage_name ());
829 gdb_puts (", ", stream
);
830 stream
->wrap_here (9);
831 gdb_printf (stream
, "%s = ", sym
->linkage_name ());
834 c_print_type (sym
->type (), "", stream
, -1, 0, language
, flags
);
838 gdb_puts (_("] "), stream
);
841 /* Use 'print_spaces', but take into consideration the
842 type_print_options FLAGS in order to determine how many whitespaces
846 print_spaces_filtered_with_print_options
847 (int level
, struct ui_file
*stream
, const struct type_print_options
*flags
)
849 if (!flags
->print_offsets
)
850 print_spaces (level
, stream
);
852 print_spaces (level
+ print_offset_data::indentation
, stream
);
855 /* Output an access specifier to STREAM, if needed. LAST_ACCESS is the
856 last access specifier output (typically returned by this function). */
859 output_access_specifier (struct ui_file
*stream
,
860 accessibility last_access
,
861 int level
, accessibility new_access
,
862 const struct type_print_options
*flags
)
864 if (last_access
== new_access
)
867 if (new_access
== accessibility::PROTECTED
)
869 print_spaces_filtered_with_print_options (level
+ 2, stream
, flags
);
870 gdb_printf (stream
, "protected:\n");
872 else if (new_access
== accessibility::PRIVATE
)
874 print_spaces_filtered_with_print_options (level
+ 2, stream
, flags
);
875 gdb_printf (stream
, "private:\n");
879 print_spaces_filtered_with_print_options (level
+ 2, stream
, flags
);
880 gdb_printf (stream
, "public:\n");
886 /* Helper function that temporarily disables FLAGS->PRINT_OFFSETS,
887 calls 'c_print_type_1', and then reenables FLAGS->PRINT_OFFSETS if
891 c_print_type_no_offsets (struct type
*type
,
892 const char *varstring
,
893 struct ui_file
*stream
,
895 enum language language
,
896 struct type_print_options
*flags
,
897 struct print_offset_data
*podata
)
899 unsigned int old_po
= flags
->print_offsets
;
901 /* Temporarily disable print_offsets, because it would mess with
903 flags
->print_offsets
= 0;
904 c_print_type_1 (type
, varstring
, stream
, show
, level
, language
, flags
,
906 flags
->print_offsets
= old_po
;
909 /* Helper for 'c_type_print_base' that handles structs and unions.
910 For a description of the arguments, see 'c_type_print_base'. */
913 c_type_print_base_struct_union (struct type
*type
, struct ui_file
*stream
,
915 enum language language
,
916 const struct type_print_options
*flags
,
917 struct print_offset_data
*podata
)
919 struct type_print_options local_flags
= *flags
;
920 local_flags
.local_typedefs
= NULL
;
922 std::unique_ptr
<typedef_hash_table
> hash_holder
;
925 if (flags
->local_typedefs
)
926 local_flags
.local_typedefs
927 = new typedef_hash_table (*flags
->local_typedefs
);
929 local_flags
.local_typedefs
= new typedef_hash_table ();
931 hash_holder
.reset (local_flags
.local_typedefs
);
934 c_type_print_modifier (type
, stream
, 0, 1, language
);
935 if (type
->code () == TYPE_CODE_UNION
)
936 gdb_printf (stream
, "union ");
937 else if (type
->is_declared_class ())
938 gdb_printf (stream
, "class ");
940 gdb_printf (stream
, "struct ");
942 /* Print the tag if it exists. The HP aCC compiler emits a
943 spurious "{unnamed struct}"/"{unnamed union}"/"{unnamed
944 enum}" tag for unnamed struct/union/enum's, which we don't
946 if (type
->name () != NULL
947 && !startswith (type
->name (), "{unnamed"))
949 /* When printing the tag name, we are still effectively
950 printing in the outer context, hence the use of FLAGS
952 print_name_maybe_canonical (type
->name (), flags
, stream
);
954 gdb_puts (" ", stream
);
959 /* If we just printed a tag name, no need to print anything
961 if (type
->name () == NULL
)
962 gdb_printf (stream
, "{...}");
964 else if (show
> 0 || type
->name () == NULL
)
966 struct type
*basetype
;
969 c_type_print_template_args (&local_flags
, type
, stream
, language
);
971 /* Add in template parameters when printing derivation info. */
972 if (local_flags
.local_typedefs
!= NULL
)
973 local_flags
.local_typedefs
->add_template_parameters (type
);
974 cp_type_print_derivation_info (stream
, type
, &local_flags
);
976 /* This holds just the global typedefs and the template
978 struct type_print_options semi_local_flags
= *flags
;
979 semi_local_flags
.local_typedefs
= NULL
;
981 std::unique_ptr
<typedef_hash_table
> semi_holder
;
982 if (local_flags
.local_typedefs
!= nullptr)
984 semi_local_flags
.local_typedefs
985 = new typedef_hash_table (*local_flags
.local_typedefs
);
986 semi_holder
.reset (semi_local_flags
.local_typedefs
);
988 /* Now add in the local typedefs. */
989 local_flags
.local_typedefs
->recursively_update (type
);
992 gdb_printf (stream
, "{\n");
994 if (type
->num_fields () == 0 && TYPE_NFN_FIELDS (type
) == 0
995 && TYPE_TYPEDEF_FIELD_COUNT (type
) == 0)
997 print_spaces_filtered_with_print_options (level
+ 4, stream
, flags
);
998 if (type
->is_stub ())
999 gdb_printf (stream
, _("%p[<incomplete type>%p]\n"),
1000 metadata_style
.style ().ptr (), nullptr);
1002 gdb_printf (stream
, _("%p[<no data fields>%p]\n"),
1003 metadata_style
.style ().ptr (), nullptr);
1006 accessibility section_type
= (type
->is_declared_class ()
1007 ? accessibility::PRIVATE
1008 : accessibility::PUBLIC
);
1010 /* If there is a base class for this type,
1011 do not print the field that it occupies. */
1013 int len
= type
->num_fields ();
1014 vptr_fieldno
= get_vptr_fieldno (type
, &basetype
);
1016 struct print_offset_data
local_podata (flags
);
1018 for (int i
= TYPE_N_BASECLASSES (type
); i
< len
; i
++)
1022 /* If we have a virtual table pointer, omit it. Even if
1023 virtual table pointers are not specifically marked in
1024 the debug info, they should be artificial. */
1025 if ((i
== vptr_fieldno
&& type
== basetype
)
1026 || type
->field (i
).is_artificial ())
1030 = output_access_specifier (stream
, section_type
, level
,
1031 type
->field (i
).accessibility (),
1034 bool is_static
= type
->field (i
).is_static ();
1036 if (flags
->print_offsets
)
1037 podata
->update (type
, i
, stream
);
1039 print_spaces (level
+ 4, stream
);
1041 gdb_printf (stream
, "static ");
1043 int newshow
= show
- 1;
1045 if (!is_static
&& flags
->print_offsets
1046 && (type
->field (i
).type ()->code () == TYPE_CODE_STRUCT
1047 || type
->field (i
).type ()->code () == TYPE_CODE_UNION
))
1049 /* If we're printing offsets and this field's type is
1050 either a struct or an union, then we're interested in
1054 /* Make sure we carry our offset when we expand the
1056 local_podata
.offset_bitpos
1057 = podata
->offset_bitpos
+ type
->field (i
).loc_bitpos ();
1058 /* We're entering a struct/union. Right now,
1059 PODATA->END_BITPOS points right *after* the
1060 struct/union. However, when printing the first field
1061 of this inner struct/union, the end_bitpos we're
1062 expecting is exactly at the beginning of the
1063 struct/union. Therefore, we subtract the length of
1064 the whole struct/union. */
1065 local_podata
.end_bitpos
1066 = podata
->end_bitpos
1067 - type
->field (i
).type ()->length () * TARGET_CHAR_BIT
;
1070 c_print_type_1 (type
->field (i
).type (),
1071 type
->field (i
).name (),
1072 stream
, newshow
, level
+ 4,
1073 language
, &local_flags
, &local_podata
);
1075 if (!is_static
&& type
->field (i
).is_packed ())
1077 /* It is a bitfield. This code does not attempt
1078 to look at the bitpos and reconstruct filler,
1079 unnamed fields. This would lead to misleading
1080 results if the compiler does not put out fields
1081 for such things (I don't know what it does). */
1082 gdb_printf (stream
, " : %d", type
->field (i
).bitsize ());
1084 gdb_printf (stream
, ";\n");
1087 /* If there are both fields and methods, put a blank line
1088 between them. Make sure to count only method that we
1089 will display; artificial methods will be hidden. */
1090 len
= TYPE_NFN_FIELDS (type
);
1091 if (!flags
->print_methods
)
1094 for (int i
= 0; i
< len
; i
++)
1096 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
1097 int len2
= TYPE_FN_FIELDLIST_LENGTH (type
, i
);
1100 for (j
= 0; j
< len2
; j
++)
1101 if (!TYPE_FN_FIELD_ARTIFICIAL (f
, j
))
1105 gdb_printf (stream
, "\n");
1107 /* C++: print out the methods. */
1108 for (int i
= 0; i
< len
; i
++)
1110 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
1111 int j
, len2
= TYPE_FN_FIELDLIST_LENGTH (type
, i
);
1112 const char *method_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
1113 const char *name
= type
->name ();
1114 int is_constructor
= name
&& strcmp (method_name
,
1117 for (j
= 0; j
< len2
; j
++)
1119 const char *mangled_name
;
1120 gdb::unique_xmalloc_ptr
<char> mangled_name_holder
;
1121 const char *physname
= TYPE_FN_FIELD_PHYSNAME (f
, j
);
1122 int is_full_physname_constructor
=
1123 TYPE_FN_FIELD_CONSTRUCTOR (f
, j
)
1124 || is_constructor_name (physname
)
1125 || is_destructor_name (physname
)
1126 || method_name
[0] == '~';
1128 /* Do not print out artificial methods. */
1129 if (TYPE_FN_FIELD_ARTIFICIAL (f
, j
))
1133 section_type
= output_access_specifier
1134 (stream
, section_type
, level
,
1135 TYPE_FN_FIELD (f
, j
).accessibility
,
1138 print_spaces_filtered_with_print_options (level
+ 4, stream
,
1140 if (TYPE_FN_FIELD_VIRTUAL_P (f
, j
))
1141 gdb_printf (stream
, "virtual ");
1142 else if (TYPE_FN_FIELD_STATIC_P (f
, j
))
1143 gdb_printf (stream
, "static ");
1144 if (TYPE_FN_FIELD_TYPE (f
, j
)->target_type () == 0)
1146 /* Keep GDB from crashing here. */
1148 _("%p[<undefined type>%p] %s;\n"),
1149 metadata_style
.style ().ptr (), nullptr,
1150 TYPE_FN_FIELD_PHYSNAME (f
, j
));
1153 else if (!is_constructor
/* Constructors don't
1156 && !is_full_physname_constructor
/* " " */
1157 && !is_type_conversion_operator (type
, i
, j
))
1159 c_print_type_no_offsets
1160 (TYPE_FN_FIELD_TYPE (f
, j
)->target_type (),
1161 "", stream
, -1, 0, language
, &local_flags
, podata
);
1163 gdb_puts (" ", stream
);
1165 if (TYPE_FN_FIELD_STUB (f
, j
))
1167 /* Build something we can demangle. */
1168 mangled_name_holder
.reset (gdb_mangle_name (type
, i
, j
));
1169 mangled_name
= mangled_name_holder
.get ();
1172 mangled_name
= TYPE_FN_FIELD_PHYSNAME (f
, j
);
1174 gdb::unique_xmalloc_ptr
<char> demangled_name
1175 = gdb_demangle (mangled_name
,
1176 DMGL_ANSI
| DMGL_PARAMS
);
1177 if (demangled_name
== NULL
)
1179 /* In some cases (for instance with the HP
1180 demangling), if a function has more than 10
1181 arguments, the demangling will fail.
1182 Let's try to reconstruct the function
1183 signature from the symbol information. */
1184 if (!TYPE_FN_FIELD_STUB (f
, j
))
1186 int staticp
= TYPE_FN_FIELD_STATIC_P (f
, j
);
1187 struct type
*mtype
= TYPE_FN_FIELD_TYPE (f
, j
);
1189 cp_type_print_method_args (mtype
,
1197 fprintf_styled (stream
, metadata_style
.style (),
1198 _("<badly mangled name '%s'>"),
1204 const char *demangled_no_class
1205 = remove_qualifiers (demangled_name
.get ());
1207 /* Get rid of the `static' appended by the
1209 p
= strstr (demangled_no_class
, " static");
1212 int length
= p
- demangled_no_class
;
1213 std::string
demangled_no_static (demangled_no_class
,
1215 gdb_puts (demangled_no_static
.c_str (), stream
);
1218 gdb_puts (demangled_no_class
, stream
);
1221 gdb_printf (stream
, ";\n");
1225 /* Print out nested types. */
1226 if (TYPE_NESTED_TYPES_COUNT (type
) != 0
1227 && semi_local_flags
.print_nested_type_limit
!= 0)
1229 if (semi_local_flags
.print_nested_type_limit
> 0)
1230 --semi_local_flags
.print_nested_type_limit
;
1232 if (type
->num_fields () != 0 || TYPE_NFN_FIELDS (type
) != 0)
1233 gdb_printf (stream
, "\n");
1235 for (int i
= 0; i
< TYPE_NESTED_TYPES_COUNT (type
); ++i
)
1237 print_spaces_filtered_with_print_options (level
+ 4, stream
,
1239 c_print_type_no_offsets (TYPE_NESTED_TYPES_FIELD_TYPE (type
, i
),
1240 "", stream
, show
, level
+ 4,
1241 language
, &semi_local_flags
, podata
);
1242 gdb_printf (stream
, ";\n");
1246 /* Print typedefs defined in this class. */
1248 if (TYPE_TYPEDEF_FIELD_COUNT (type
) != 0 && flags
->print_typedefs
)
1250 if (type
->num_fields () != 0 || TYPE_NFN_FIELDS (type
) != 0
1251 || TYPE_NESTED_TYPES_COUNT (type
) != 0)
1252 gdb_printf (stream
, "\n");
1254 for (int i
= 0; i
< TYPE_TYPEDEF_FIELD_COUNT (type
); i
++)
1256 struct type
*target
= TYPE_TYPEDEF_FIELD_TYPE (type
, i
);
1258 /* Dereference the typedef declaration itself. */
1259 gdb_assert (target
->code () == TYPE_CODE_TYPEDEF
);
1260 target
= target
->target_type ();
1262 section_type
= (output_access_specifier
1263 (stream
, section_type
, level
,
1264 TYPE_TYPEDEF_FIELD (type
, i
).accessibility
,
1267 print_spaces_filtered_with_print_options (level
+ 4, stream
,
1269 gdb_printf (stream
, "typedef ");
1271 /* We want to print typedefs with substitutions
1272 from the template parameters or globally-known
1273 typedefs but not local typedefs. */
1274 c_print_type_no_offsets (target
,
1275 TYPE_TYPEDEF_FIELD_NAME (type
, i
),
1276 stream
, show
- 1, level
+ 4,
1277 language
, &semi_local_flags
, podata
);
1278 gdb_printf (stream
, ";\n");
1282 if (flags
->print_offsets
)
1285 podata
->finish (type
, level
, stream
);
1287 print_spaces (print_offset_data::indentation
, stream
);
1289 print_spaces (2, stream
);
1292 gdb_printf (stream
, "%*s}", level
, "");
1296 /* Print the name of the type (or the ultimate pointer target,
1297 function value or array element), or the description of a structure
1300 SHOW positive means print details about the type (e.g. enum
1301 values), and print structure elements passing SHOW - 1 for show.
1303 SHOW negative means just print the type name or struct tag if there
1304 is one. If there is no name, print something sensible but concise
1305 like "struct {...}".
1307 SHOW zero means just print the type name or struct tag if there is
1308 one. If there is no name, print something sensible but not as
1309 concise like "struct {int x; int y;}".
1311 LEVEL is the number of spaces to indent by.
1312 We increase it for some recursive calls. */
1315 c_type_print_base_1 (struct type
*type
, struct ui_file
*stream
,
1316 int show
, int level
,
1317 enum language language
,
1318 const struct type_print_options
*flags
,
1319 struct print_offset_data
*podata
)
1328 fputs_styled (_("<type unknown>"), metadata_style
.style (), stream
);
1332 /* When SHOW is zero or less, and there is a valid type name, then
1333 always just print the type name directly from the type. */
1336 && type
->name () != NULL
)
1338 c_type_print_modifier (type
, stream
, 0, 1, language
);
1340 /* If we have "typedef struct foo {. . .} bar;" do we want to
1341 print it as "struct foo" or as "bar"? Pick the latter for
1342 C++, because C++ folk tend to expect things like "class5
1343 *foo" rather than "struct class5 *foo". We rather
1344 arbitrarily choose to make language_minimal work in a C-like
1346 if (language
== language_c
|| language
== language_minimal
)
1348 if (type
->code () == TYPE_CODE_UNION
)
1349 gdb_printf (stream
, "union ");
1350 else if (type
->code () == TYPE_CODE_STRUCT
)
1352 if (type
->is_declared_class ())
1353 gdb_printf (stream
, "class ");
1355 gdb_printf (stream
, "struct ");
1357 else if (type
->code () == TYPE_CODE_ENUM
)
1358 gdb_printf (stream
, "enum ");
1361 print_name_maybe_canonical (type
->name (), flags
, stream
);
1365 type
= check_typedef (type
);
1367 switch (type
->code ())
1369 case TYPE_CODE_TYPEDEF
:
1370 /* If we get here, the typedef doesn't have a name, and we
1371 couldn't resolve type::target_type. Not much we can do. */
1372 gdb_assert (type
->name () == NULL
);
1373 gdb_assert (type
->target_type () == NULL
);
1374 fprintf_styled (stream
, metadata_style
.style (),
1375 _("<unnamed typedef>"));
1378 case TYPE_CODE_FUNC
:
1379 case TYPE_CODE_METHOD
:
1380 if (type
->target_type () == NULL
)
1381 type_print_unknown_return_type (stream
);
1383 c_type_print_base_1 (type
->target_type (),
1384 stream
, show
, level
, language
, flags
, podata
);
1386 case TYPE_CODE_ARRAY
:
1388 case TYPE_CODE_MEMBERPTR
:
1390 case TYPE_CODE_RVALUE_REF
:
1391 case TYPE_CODE_METHODPTR
:
1392 c_type_print_base_1 (type
->target_type (),
1393 stream
, show
, level
, language
, flags
, podata
);
1396 case TYPE_CODE_STRUCT
:
1397 case TYPE_CODE_UNION
:
1398 c_type_print_base_struct_union (type
, stream
, show
, level
,
1399 language
, flags
, podata
);
1402 case TYPE_CODE_ENUM
:
1403 c_type_print_modifier (type
, stream
, 0, 1, language
);
1404 gdb_printf (stream
, "enum ");
1405 if (type
->is_declared_class ())
1406 gdb_printf (stream
, "class ");
1407 /* Print the tag name if it exists.
1408 The aCC compiler emits a spurious
1409 "{unnamed struct}"/"{unnamed union}"/"{unnamed enum}"
1410 tag for unnamed struct/union/enum's, which we don't
1412 if (type
->name () != NULL
1413 && !startswith (type
->name (), "{unnamed"))
1415 print_name_maybe_canonical (type
->name (), flags
, stream
);
1417 gdb_puts (" ", stream
);
1420 stream
->wrap_here (4);
1423 /* If we just printed a tag name, no need to print anything
1425 if (type
->name () == NULL
)
1426 gdb_printf (stream
, "{...}");
1428 else if (show
> 0 || type
->name () == NULL
)
1430 LONGEST lastval
= 0;
1432 /* We can't handle this case perfectly, as DWARF does not
1433 tell us whether or not the underlying type was specified
1434 in the source (and other debug formats don't provide this
1435 at all). We choose to print the underlying type, if it
1436 has a name, when in C++ on the theory that it's better to
1437 print too much than too little; but conversely not to
1438 print something egregiously outside the current
1439 language's syntax. */
1440 if (language
== language_cplus
&& type
->target_type () != NULL
)
1442 struct type
*underlying
= check_typedef (type
->target_type ());
1444 if (underlying
->name () != NULL
)
1445 gdb_printf (stream
, ": %s ", underlying
->name ());
1448 gdb_printf (stream
, "{");
1449 len
= type
->num_fields ();
1450 for (i
= 0; i
< len
; i
++)
1454 gdb_printf (stream
, ", ");
1455 stream
->wrap_here (4);
1456 fputs_styled (type
->field (i
).name (),
1457 variable_name_style
.style (), stream
);
1458 if (lastval
!= type
->field (i
).loc_enumval ())
1460 gdb_printf (stream
, " = %s",
1461 plongest (type
->field (i
).loc_enumval ()));
1462 lastval
= type
->field (i
).loc_enumval ();
1466 gdb_printf (stream
, "}");
1470 case TYPE_CODE_FLAGS
:
1472 struct type_print_options local_flags
= *flags
;
1474 local_flags
.local_typedefs
= NULL
;
1476 c_type_print_modifier (type
, stream
, 0, 1, language
);
1477 gdb_printf (stream
, "flag ");
1478 print_name_maybe_canonical (type
->name (), flags
, stream
);
1481 gdb_puts (" ", stream
);
1482 gdb_printf (stream
, "{\n");
1483 if (type
->num_fields () == 0)
1485 if (type
->is_stub ())
1487 _("%*s%p[<incomplete type>%p]\n"),
1489 metadata_style
.style ().ptr (), nullptr);
1492 _("%*s%p[<no data fields>%p]\n"),
1494 metadata_style
.style ().ptr (), nullptr);
1496 len
= type
->num_fields ();
1497 for (i
= 0; i
< len
; i
++)
1500 print_spaces (level
+ 4, stream
);
1501 /* We pass "show" here and not "show - 1" to get enum types
1502 printed. There's no other way to see them. */
1503 c_print_type_1 (type
->field (i
).type (),
1504 type
->field (i
).name (),
1505 stream
, show
, level
+ 4,
1506 language
, &local_flags
, podata
);
1507 gdb_printf (stream
, " @%s",
1508 plongest (type
->field (i
).loc_bitpos ()));
1509 if (type
->field (i
).bitsize () > 1)
1511 gdb_printf (stream
, "-%s",
1512 plongest (type
->field (i
).loc_bitpos ()
1513 + type
->field (i
).bitsize ()
1516 gdb_printf (stream
, ";\n");
1518 gdb_printf (stream
, "%*s}", level
, "");
1523 case TYPE_CODE_VOID
:
1524 gdb_printf (stream
, "void");
1527 case TYPE_CODE_UNDEF
:
1528 gdb_printf (stream
, _("struct <unknown>"));
1531 case TYPE_CODE_ERROR
:
1532 gdb_printf (stream
, "%s", TYPE_ERROR_NAME (type
));
1535 case TYPE_CODE_RANGE
:
1536 /* This should not occur. */
1537 fprintf_styled (stream
, metadata_style
.style (), _("<range type>"));
1540 case TYPE_CODE_FIXED_POINT
:
1541 print_type_fixed_point (type
, stream
);
1544 case TYPE_CODE_NAMESPACE
:
1545 gdb_puts ("namespace ", stream
);
1546 gdb_puts (type
->name (), stream
);
1550 /* Handle types not explicitly handled by the other cases, such
1551 as fundamental types. For these, just print whatever the
1552 type name is, as recorded in the type itself. If there is no
1553 type name, then complain. */
1554 if (type
->name () != NULL
)
1556 c_type_print_modifier (type
, stream
, 0, 1, language
);
1557 print_name_maybe_canonical (type
->name (), flags
, stream
);
1561 /* At least for dump_symtab, it is important that this not
1563 fprintf_styled (stream
, metadata_style
.style (),
1564 _("<invalid type code %d>"), type
->code ());
1570 /* See c_type_print_base_1. */
1573 c_type_print_base (struct type
*type
, struct ui_file
*stream
,
1574 int show
, int level
,
1575 const struct type_print_options
*flags
)
1577 struct print_offset_data
podata (flags
);
1579 c_type_print_base_1 (type
, stream
, show
, level
,
1580 current_language
->la_language
, flags
, &podata
);