Update copyright year range in header of all files managed by GDB
[binutils-gdb.git] / gdb / c-typeprint.c
1 /* Support for printing C and C++ types for GDB, the GNU debugger.
2 Copyright (C) 1986-2023 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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.
15
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/>. */
18
19 #include "defs.h"
20 #include "gdbsupport/gdb_obstack.h"
21 #include "bfd.h" /* Binary File Description. */
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "expression.h"
25 #include "value.h"
26 #include "gdbcore.h"
27 #include "target.h"
28 #include "language.h"
29 #include "demangle.h"
30 #include "c-lang.h"
31 #include "cli/cli-style.h"
32 #include "typeprint.h"
33 #include "cp-abi.h"
34 #include "cp-support.h"
35
36 /* A list of access specifiers used for printing. */
37
38 enum access_specifier
39 {
40 s_none,
41 s_public,
42 s_private,
43 s_protected
44 };
45
46 static void c_type_print_varspec_suffix (struct type *, struct ui_file *, int,
47 int, int,
48 enum language,
49 const struct type_print_options *);
50
51 static void c_type_print_varspec_prefix (struct type *,
52 struct ui_file *,
53 int, int, int,
54 enum language,
55 const struct type_print_options *,
56 struct print_offset_data *);
57
58 /* Print "const", "volatile", or address space modifiers. */
59 static void c_type_print_modifier (struct type *,
60 struct ui_file *,
61 int, int, enum language);
62
63 static void c_type_print_base_1 (struct type *type, struct ui_file *stream,
64 int show, int level, enum language language,
65 const struct type_print_options *flags,
66 struct print_offset_data *podata);
67 \f
68
69 /* A callback function for cp_canonicalize_string_full that uses
70 typedef_hash_table::find_typedef. */
71
72 static const char *
73 find_typedef_for_canonicalize (struct type *t, void *data)
74 {
75 return typedef_hash_table::find_typedef
76 ((const struct type_print_options *) data, t);
77 }
78
79 /* Print NAME on STREAM. If the 'raw' field of FLAGS is not set,
80 canonicalize NAME using the local typedefs first. */
81
82 static void
83 print_name_maybe_canonical (const char *name,
84 const struct type_print_options *flags,
85 struct ui_file *stream)
86 {
87 gdb::unique_xmalloc_ptr<char> s;
88
89 if (!flags->raw)
90 s = cp_canonicalize_string_full (name,
91 find_typedef_for_canonicalize,
92 (void *) flags);
93
94 gdb_puts (s != nullptr ? s.get () : name, stream);
95 }
96
97 \f
98
99 /* Helper function for c_print_type. */
100
101 static void
102 c_print_type_1 (struct type *type,
103 const char *varstring,
104 struct ui_file *stream,
105 int show, int level,
106 enum language language,
107 const struct type_print_options *flags,
108 struct print_offset_data *podata)
109 {
110 enum type_code code;
111 int demangled_args;
112 int need_post_space;
113 const char *local_name;
114
115 if (show > 0)
116 type = check_typedef (type);
117
118 local_name = typedef_hash_table::find_typedef (flags, type);
119 code = type->code ();
120 if (local_name != NULL)
121 {
122 c_type_print_modifier (type, stream, 0, 1, language);
123 gdb_puts (local_name, stream);
124 if (varstring != NULL && *varstring != '\0')
125 gdb_puts (" ", stream);
126 }
127 else
128 {
129 c_type_print_base_1 (type, stream, show, level, language, flags, podata);
130 if ((varstring != NULL && *varstring != '\0')
131 /* Need a space if going to print stars or brackets;
132 but not if we will print just a type name. */
133 || ((show > 0 || type->name () == 0)
134 && (code == TYPE_CODE_PTR || code == TYPE_CODE_FUNC
135 || code == TYPE_CODE_METHOD
136 || (code == TYPE_CODE_ARRAY
137 && !type->is_vector ())
138 || code == TYPE_CODE_MEMBERPTR
139 || code == TYPE_CODE_METHODPTR
140 || TYPE_IS_REFERENCE (type))))
141 gdb_puts (" ", stream);
142 need_post_space = (varstring != NULL && strcmp (varstring, "") != 0);
143 c_type_print_varspec_prefix (type, stream, show, 0, need_post_space,
144 language, flags, podata);
145 }
146
147 if (varstring != NULL)
148 {
149 if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD)
150 fputs_styled (varstring, function_name_style.style (), stream);
151 else
152 fputs_styled (varstring, variable_name_style.style (), stream);
153
154 /* For demangled function names, we have the arglist as part of
155 the name, so don't print an additional pair of ()'s. */
156 if (local_name == NULL)
157 {
158 demangled_args = strchr (varstring, '(') != NULL;
159 c_type_print_varspec_suffix (type, stream, show,
160 0, demangled_args,
161 language, flags);
162 }
163 }
164 }
165
166 /* See c-lang.h. */
167
168 void
169 c_print_type (struct type *type,
170 const char *varstring,
171 struct ui_file *stream,
172 int show, int level,
173 enum language language,
174 const struct type_print_options *flags)
175 {
176 struct print_offset_data podata (flags);
177
178 c_print_type_1 (type, varstring, stream, show, level, language, flags,
179 &podata);
180 }
181
182 /* Print a typedef using C syntax. TYPE is the underlying type.
183 NEW_SYMBOL is the symbol naming the type. STREAM is the stream on
184 which to print. */
185
186 void
187 c_print_typedef (struct type *type,
188 struct symbol *new_symbol,
189 struct ui_file *stream)
190 {
191 type = check_typedef (type);
192 gdb_printf (stream, "typedef ");
193 type_print (type, "", stream, -1);
194 if ((new_symbol->type ())->name () == 0
195 || strcmp ((new_symbol->type ())->name (),
196 new_symbol->linkage_name ()) != 0
197 || new_symbol->type ()->code () == TYPE_CODE_TYPEDEF)
198 gdb_printf (stream, " %s", new_symbol->print_name ());
199 gdb_printf (stream, ";");
200 }
201
202 /* If TYPE is a derived type, then print out derivation information.
203 Print only the actual base classes of this type, not the base
204 classes of the base classes. I.e. for the derivation hierarchy:
205
206 class A { int a; };
207 class B : public A {int b; };
208 class C : public B {int c; };
209
210 Print the type of class C as:
211
212 class C : public B {
213 int c;
214 }
215
216 Not as the following (like gdb used to), which is not legal C++
217 syntax for derived types and may be confused with the multiple
218 inheritance form:
219
220 class C : public B : public A {
221 int c;
222 }
223
224 In general, gdb should try to print the types as closely as
225 possible to the form that they appear in the source code. */
226
227 static void
228 cp_type_print_derivation_info (struct ui_file *stream,
229 struct type *type,
230 const struct type_print_options *flags)
231 {
232 const char *name;
233 int i;
234
235 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
236 {
237 stream->wrap_here (8);
238 gdb_puts (i == 0 ? ": " : ", ", stream);
239 gdb_printf (stream, "%s%s ",
240 BASETYPE_VIA_PUBLIC (type, i)
241 ? "public" : (TYPE_FIELD_PROTECTED (type, i)
242 ? "protected" : "private"),
243 BASETYPE_VIA_VIRTUAL (type, i) ? " virtual" : "");
244 name = TYPE_BASECLASS (type, i)->name ();
245 if (name)
246 print_name_maybe_canonical (name, flags, stream);
247 else
248 gdb_printf (stream, "(null)");
249 }
250 if (i > 0)
251 {
252 gdb_puts (" ", stream);
253 }
254 }
255
256 /* Print the C++ method arguments ARGS to the file STREAM. */
257
258 static void
259 cp_type_print_method_args (struct type *mtype, const char *prefix,
260 const char *varstring, int staticp,
261 struct ui_file *stream,
262 enum language language,
263 const struct type_print_options *flags)
264 {
265 struct field *args = mtype->fields ();
266 int nargs = mtype->num_fields ();
267 int varargs = mtype->has_varargs ();
268 int i;
269
270 fprintf_symbol (stream, prefix,
271 language_cplus, DMGL_ANSI);
272 fprintf_symbol (stream, varstring,
273 language_cplus, DMGL_ANSI);
274 gdb_puts ("(", stream);
275
276 int printed_args = 0;
277 for (i = 0; i < nargs; ++i)
278 {
279 if (i == 0 && !staticp)
280 {
281 /* Skip the class variable. We keep this here to accommodate older
282 compilers and debug formats which may not support artificial
283 parameters. */
284 continue;
285 }
286
287 struct field arg = args[i];
288 /* Skip any artificial arguments. */
289 if (FIELD_ARTIFICIAL (arg))
290 continue;
291
292 if (printed_args > 0)
293 {
294 gdb_printf (stream, ", ");
295 stream->wrap_here (8);
296 }
297
298 c_print_type (arg.type (), "", stream, 0, 0, language, flags);
299 printed_args++;
300 }
301
302 if (varargs)
303 {
304 if (printed_args == 0)
305 gdb_printf (stream, "...");
306 else
307 gdb_printf (stream, ", ...");
308 }
309 else if (printed_args == 0)
310 {
311 if (language == language_cplus)
312 gdb_printf (stream, "void");
313 }
314
315 gdb_printf (stream, ")");
316
317 /* For non-static methods, read qualifiers from the type of
318 THIS. */
319 if (!staticp)
320 {
321 struct type *domain;
322
323 gdb_assert (nargs > 0);
324 gdb_assert (args[0].type ()->code () == TYPE_CODE_PTR);
325 domain = args[0].type ()->target_type ();
326
327 if (TYPE_CONST (domain))
328 gdb_printf (stream, " const");
329
330 if (TYPE_VOLATILE (domain))
331 gdb_printf (stream, " volatile");
332
333 if (TYPE_RESTRICT (domain))
334 gdb_printf (stream, (language == language_cplus
335 ? " __restrict__"
336 : " restrict"));
337
338 if (TYPE_ATOMIC (domain))
339 gdb_printf (stream, " _Atomic");
340 }
341 }
342
343
344 /* Print any asterisks or open-parentheses needed before the
345 variable name (to describe its type).
346
347 On outermost call, pass 0 for PASSED_A_PTR.
348 On outermost call, SHOW > 0 means should ignore
349 any typename for TYPE and show its details.
350 SHOW is always zero on recursive calls.
351
352 NEED_POST_SPACE is non-zero when a space will be be needed
353 between a trailing qualifier and a field, variable, or function
354 name. */
355
356 static void
357 c_type_print_varspec_prefix (struct type *type,
358 struct ui_file *stream,
359 int show, int passed_a_ptr,
360 int need_post_space,
361 enum language language,
362 const struct type_print_options *flags,
363 struct print_offset_data *podata)
364 {
365 const char *name;
366
367 if (type == 0)
368 return;
369
370 if (type->name () && show <= 0)
371 return;
372
373 QUIT;
374
375 switch (type->code ())
376 {
377 case TYPE_CODE_PTR:
378 c_type_print_varspec_prefix (type->target_type (),
379 stream, show, 1, 1, language, flags,
380 podata);
381 gdb_printf (stream, "*");
382 c_type_print_modifier (type, stream, 1, need_post_space, language);
383 break;
384
385 case TYPE_CODE_MEMBERPTR:
386 c_type_print_varspec_prefix (type->target_type (),
387 stream, show, 0, 0, language, flags, podata);
388 name = TYPE_SELF_TYPE (type)->name ();
389 if (name)
390 print_name_maybe_canonical (name, flags, stream);
391 else
392 c_type_print_base_1 (TYPE_SELF_TYPE (type),
393 stream, -1, passed_a_ptr, language, flags,
394 podata);
395 gdb_printf (stream, "::*");
396 break;
397
398 case TYPE_CODE_METHODPTR:
399 c_type_print_varspec_prefix (type->target_type (),
400 stream, show, 0, 0, language, flags,
401 podata);
402 gdb_printf (stream, "(");
403 name = TYPE_SELF_TYPE (type)->name ();
404 if (name)
405 print_name_maybe_canonical (name, flags, stream);
406 else
407 c_type_print_base_1 (TYPE_SELF_TYPE (type),
408 stream, -1, passed_a_ptr, language, flags,
409 podata);
410 gdb_printf (stream, "::*");
411 break;
412
413 case TYPE_CODE_REF:
414 case TYPE_CODE_RVALUE_REF:
415 c_type_print_varspec_prefix (type->target_type (),
416 stream, show, 1, 0, language, flags,
417 podata);
418 gdb_printf (stream, type->code () == TYPE_CODE_REF ? "&" : "&&");
419 c_type_print_modifier (type, stream, 1, need_post_space, language);
420 break;
421
422 case TYPE_CODE_METHOD:
423 case TYPE_CODE_FUNC:
424 c_type_print_varspec_prefix (type->target_type (),
425 stream, show, 0, 0, language, flags,
426 podata);
427 if (passed_a_ptr)
428 gdb_printf (stream, "(");
429 break;
430
431 case TYPE_CODE_ARRAY:
432 c_type_print_varspec_prefix (type->target_type (),
433 stream, show, 0, need_post_space,
434 language, flags, podata);
435 if (passed_a_ptr)
436 gdb_printf (stream, "(");
437 break;
438
439 case TYPE_CODE_TYPEDEF:
440 c_type_print_varspec_prefix (type->target_type (),
441 stream, show, passed_a_ptr, 0,
442 language, flags, podata);
443 break;
444
445 case TYPE_CODE_UNDEF:
446 case TYPE_CODE_STRUCT:
447 case TYPE_CODE_UNION:
448 case TYPE_CODE_ENUM:
449 case TYPE_CODE_FLAGS:
450 case TYPE_CODE_INT:
451 case TYPE_CODE_FLT:
452 case TYPE_CODE_VOID:
453 case TYPE_CODE_ERROR:
454 case TYPE_CODE_CHAR:
455 case TYPE_CODE_BOOL:
456 case TYPE_CODE_SET:
457 case TYPE_CODE_RANGE:
458 case TYPE_CODE_STRING:
459 case TYPE_CODE_COMPLEX:
460 case TYPE_CODE_NAMESPACE:
461 case TYPE_CODE_DECFLOAT:
462 case TYPE_CODE_FIXED_POINT:
463 /* These types need no prefix. They are listed here so that
464 gcc -Wall will reveal any types that haven't been handled. */
465 break;
466 default:
467 error (_("type not handled in c_type_print_varspec_prefix()"));
468 break;
469 }
470 }
471
472 /* Print out "const" and "volatile" attributes,
473 and address space id if present.
474 TYPE is a pointer to the type being printed out.
475 STREAM is the output destination.
476 NEED_PRE_SPACE = 1 indicates an initial white space is needed.
477 NEED_POST_SPACE = 1 indicates a final white space is needed. */
478
479 static void
480 c_type_print_modifier (struct type *type, struct ui_file *stream,
481 int need_pre_space, int need_post_space,
482 enum language language)
483 {
484 int did_print_modifier = 0;
485 const char *address_space_id;
486
487 /* We don't print `const' qualifiers for references --- since all
488 operators affect the thing referenced, not the reference itself,
489 every reference is `const'. */
490 if (TYPE_CONST (type) && !TYPE_IS_REFERENCE (type))
491 {
492 if (need_pre_space)
493 gdb_printf (stream, " ");
494 gdb_printf (stream, "const");
495 did_print_modifier = 1;
496 }
497
498 if (TYPE_VOLATILE (type))
499 {
500 if (did_print_modifier || need_pre_space)
501 gdb_printf (stream, " ");
502 gdb_printf (stream, "volatile");
503 did_print_modifier = 1;
504 }
505
506 if (TYPE_RESTRICT (type))
507 {
508 if (did_print_modifier || need_pre_space)
509 gdb_printf (stream, " ");
510 gdb_printf (stream, (language == language_cplus
511 ? "__restrict__"
512 : "restrict"));
513 did_print_modifier = 1;
514 }
515
516 if (TYPE_ATOMIC (type))
517 {
518 if (did_print_modifier || need_pre_space)
519 gdb_printf (stream, " ");
520 gdb_printf (stream, "_Atomic");
521 did_print_modifier = 1;
522 }
523
524 address_space_id
525 = address_space_type_instance_flags_to_name (type->arch (),
526 type->instance_flags ());
527 if (address_space_id)
528 {
529 if (did_print_modifier || need_pre_space)
530 gdb_printf (stream, " ");
531 gdb_printf (stream, "@%s", address_space_id);
532 did_print_modifier = 1;
533 }
534
535 if (did_print_modifier && need_post_space)
536 gdb_printf (stream, " ");
537 }
538
539
540 /* Print out the arguments of TYPE, which should have TYPE_CODE_METHOD
541 or TYPE_CODE_FUNC, to STREAM. Artificial arguments, such as "this"
542 in non-static methods, are displayed if LINKAGE_NAME is zero. If
543 LINKAGE_NAME is non-zero and LANGUAGE is language_cplus the topmost
544 parameter types get removed their possible const and volatile qualifiers to
545 match demangled linkage name parameters part of such function type.
546 LANGUAGE is the language in which TYPE was defined. This is a necessary
547 evil since this code is used by the C and C++. */
548
549 void
550 c_type_print_args (struct type *type, struct ui_file *stream,
551 int linkage_name, enum language language,
552 const struct type_print_options *flags)
553 {
554 int i;
555 int printed_any = 0;
556
557 gdb_printf (stream, "(");
558
559 for (i = 0; i < type->num_fields (); i++)
560 {
561 struct type *param_type;
562
563 if (TYPE_FIELD_ARTIFICIAL (type, i) && linkage_name)
564 continue;
565
566 if (printed_any)
567 {
568 gdb_printf (stream, ", ");
569 stream->wrap_here (4);
570 }
571
572 param_type = type->field (i).type ();
573
574 if (language == language_cplus && linkage_name)
575 {
576 /* C++ standard, 13.1 Overloadable declarations, point 3, item:
577 - Parameter declarations that differ only in the presence or
578 absence of const and/or volatile are equivalent.
579
580 And the const/volatile qualifiers are not present in the mangled
581 names as produced by GCC. */
582
583 param_type = make_cv_type (0, 0, param_type, NULL);
584 }
585
586 c_print_type (param_type, "", stream, -1, 0, language, flags);
587 printed_any = 1;
588 }
589
590 if (printed_any && type->has_varargs ())
591 {
592 /* Print out a trailing ellipsis for varargs functions. Ignore
593 TYPE_VARARGS if the function has no named arguments; that
594 represents unprototyped (K&R style) C functions. */
595 if (printed_any && type->has_varargs ())
596 {
597 gdb_printf (stream, ", ");
598 stream->wrap_here (4);
599 gdb_printf (stream, "...");
600 }
601 }
602 else if (!printed_any
603 && (type->is_prototyped () || language == language_cplus))
604 gdb_printf (stream, "void");
605
606 gdb_printf (stream, ")");
607 }
608
609 /* Return true iff the j'th overloading of the i'th method of TYPE
610 is a type conversion operator, like `operator int () { ... }'.
611 When listing a class's methods, we don't print the return type of
612 such operators. */
613
614 static int
615 is_type_conversion_operator (struct type *type, int i, int j)
616 {
617 /* I think the whole idea of recognizing type conversion operators
618 by their name is pretty terrible. But I don't think our present
619 data structure gives us any other way to tell. If you know of
620 some other way, feel free to rewrite this function. */
621 const char *name = TYPE_FN_FIELDLIST_NAME (type, i);
622
623 if (!startswith (name, CP_OPERATOR_STR))
624 return 0;
625
626 name += 8;
627 if (! strchr (" \t\f\n\r", *name))
628 return 0;
629
630 while (strchr (" \t\f\n\r", *name))
631 name++;
632
633 if (!('a' <= *name && *name <= 'z')
634 && !('A' <= *name && *name <= 'Z')
635 && *name != '_')
636 /* If this doesn't look like the start of an identifier, then it
637 isn't a type conversion operator. */
638 return 0;
639 else if (startswith (name, "new"))
640 name += 3;
641 else if (startswith (name, "delete"))
642 name += 6;
643 else
644 /* If it doesn't look like new or delete, it's a type conversion
645 operator. */
646 return 1;
647
648 /* Is that really the end of the name? */
649 if (('a' <= *name && *name <= 'z')
650 || ('A' <= *name && *name <= 'Z')
651 || ('0' <= *name && *name <= '9')
652 || *name == '_')
653 /* No, so the identifier following "operator" must be a type name,
654 and this is a type conversion operator. */
655 return 1;
656
657 /* That was indeed the end of the name, so it was `operator new' or
658 `operator delete', neither of which are type conversion
659 operators. */
660 return 0;
661 }
662
663 /* Given a C++ qualified identifier QID, strip off the qualifiers,
664 yielding the unqualified name. The return value is a pointer into
665 the original string.
666
667 It's a pity we don't have this information in some more structured
668 form. Even the author of this function feels that writing little
669 parsers like this everywhere is stupid. */
670
671 static const char *
672 remove_qualifiers (const char *qid)
673 {
674 int quoted = 0; /* Zero if we're not in quotes;
675 '"' if we're in a double-quoted string;
676 '\'' if we're in a single-quoted string. */
677 int depth = 0; /* Number of unclosed parens we've seen. */
678 char *parenstack = (char *) alloca (strlen (qid));
679 const char *scan;
680 const char *last = 0; /* The character after the rightmost
681 `::' token we've seen so far. */
682
683 for (scan = qid; *scan; scan++)
684 {
685 if (quoted)
686 {
687 if (*scan == quoted)
688 quoted = 0;
689 else if (*scan == '\\' && *(scan + 1))
690 scan++;
691 }
692 else if (scan[0] == ':' && scan[1] == ':')
693 {
694 /* If we're inside parenthesis (i.e., an argument list) or
695 angle brackets (i.e., a list of template arguments), then
696 we don't record the position of this :: token, since it's
697 not relevant to the top-level structure we're trying to
698 operate on. */
699 if (depth == 0)
700 {
701 last = scan + 2;
702 scan++;
703 }
704 }
705 else if (*scan == '"' || *scan == '\'')
706 quoted = *scan;
707 else if (*scan == '(')
708 parenstack[depth++] = ')';
709 else if (*scan == '[')
710 parenstack[depth++] = ']';
711 /* We're going to treat <> as a pair of matching characters,
712 since we're more likely to see those in template id's than
713 real less-than characters. What a crock. */
714 else if (*scan == '<')
715 parenstack[depth++] = '>';
716 else if (*scan == ')' || *scan == ']' || *scan == '>')
717 {
718 if (depth > 0 && parenstack[depth - 1] == *scan)
719 depth--;
720 else
721 {
722 /* We're going to do a little error recovery here. If
723 we don't find a match for *scan on the paren stack,
724 but there is something lower on the stack that does
725 match, we pop the stack to that point. */
726 int i;
727
728 for (i = depth - 1; i >= 0; i--)
729 if (parenstack[i] == *scan)
730 {
731 depth = i;
732 break;
733 }
734 }
735 }
736 }
737
738 if (last)
739 return last;
740 else
741 /* We didn't find any :: tokens at the top level, so declare the
742 whole thing an unqualified identifier. */
743 return qid;
744 }
745
746 /* Print any array sizes, function arguments or close parentheses
747 needed after the variable name (to describe its type).
748 Args work like c_type_print_varspec_prefix. */
749
750 static void
751 c_type_print_varspec_suffix (struct type *type,
752 struct ui_file *stream,
753 int show, int passed_a_ptr,
754 int demangled_args,
755 enum language language,
756 const struct type_print_options *flags)
757 {
758 if (type == 0)
759 return;
760
761 if (type->name () && show <= 0)
762 return;
763
764 QUIT;
765
766 switch (type->code ())
767 {
768 case TYPE_CODE_ARRAY:
769 {
770 LONGEST low_bound, high_bound;
771 int is_vector = type->is_vector ();
772
773 if (passed_a_ptr)
774 gdb_printf (stream, ")");
775
776 gdb_printf (stream, (is_vector ?
777 " __attribute__ ((vector_size(" : "["));
778 /* Bounds are not yet resolved, print a bounds placeholder instead. */
779 if (type->bounds ()->high.kind () == PROP_LOCEXPR
780 || type->bounds ()->high.kind () == PROP_LOCLIST)
781 gdb_printf (stream, "variable length");
782 else if (get_array_bounds (type, &low_bound, &high_bound))
783 gdb_printf (stream, "%s",
784 plongest (high_bound - low_bound + 1));
785 gdb_printf (stream, (is_vector ? ")))" : "]"));
786
787 c_type_print_varspec_suffix (type->target_type (), stream,
788 show, 0, 0, language, flags);
789 }
790 break;
791
792 case TYPE_CODE_MEMBERPTR:
793 c_type_print_varspec_suffix (type->target_type (), stream,
794 show, 0, 0, language, flags);
795 break;
796
797 case TYPE_CODE_METHODPTR:
798 gdb_printf (stream, ")");
799 c_type_print_varspec_suffix (type->target_type (), stream,
800 show, 0, 0, language, flags);
801 break;
802
803 case TYPE_CODE_PTR:
804 case TYPE_CODE_REF:
805 case TYPE_CODE_RVALUE_REF:
806 c_type_print_varspec_suffix (type->target_type (), stream,
807 show, 1, 0, language, flags);
808 break;
809
810 case TYPE_CODE_METHOD:
811 case TYPE_CODE_FUNC:
812 if (passed_a_ptr)
813 gdb_printf (stream, ")");
814 if (!demangled_args)
815 c_type_print_args (type, stream, 0, language, flags);
816 c_type_print_varspec_suffix (type->target_type (), stream,
817 show, passed_a_ptr, 0, language, flags);
818 break;
819
820 case TYPE_CODE_TYPEDEF:
821 c_type_print_varspec_suffix (type->target_type (), stream,
822 show, passed_a_ptr, 0, language, flags);
823 break;
824
825 case TYPE_CODE_UNDEF:
826 case TYPE_CODE_STRUCT:
827 case TYPE_CODE_UNION:
828 case TYPE_CODE_FLAGS:
829 case TYPE_CODE_ENUM:
830 case TYPE_CODE_INT:
831 case TYPE_CODE_FLT:
832 case TYPE_CODE_VOID:
833 case TYPE_CODE_ERROR:
834 case TYPE_CODE_CHAR:
835 case TYPE_CODE_BOOL:
836 case TYPE_CODE_SET:
837 case TYPE_CODE_RANGE:
838 case TYPE_CODE_STRING:
839 case TYPE_CODE_COMPLEX:
840 case TYPE_CODE_NAMESPACE:
841 case TYPE_CODE_DECFLOAT:
842 case TYPE_CODE_FIXED_POINT:
843 /* These types do not need a suffix. They are listed so that
844 gcc -Wall will report types that may not have been
845 considered. */
846 break;
847 default:
848 error (_("type not handled in c_type_print_varspec_suffix()"));
849 break;
850 }
851 }
852
853 /* A helper for c_type_print_base that displays template
854 parameters and their bindings, if needed.
855
856 TABLE is the local bindings table to use. If NULL, no printing is
857 done. Note that, at this point, TABLE won't have any useful
858 information in it -- but it is also used as a flag to
859 print_name_maybe_canonical to activate searching the global typedef
860 table.
861
862 TYPE is the type whose template arguments are being displayed.
863
864 STREAM is the stream on which to print. */
865
866 static void
867 c_type_print_template_args (const struct type_print_options *flags,
868 struct type *type, struct ui_file *stream,
869 enum language language)
870 {
871 int first = 1, i;
872
873 if (flags->raw)
874 return;
875
876 for (i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
877 {
878 struct symbol *sym = TYPE_TEMPLATE_ARGUMENT (type, i);
879
880 if (sym->aclass () != LOC_TYPEDEF)
881 continue;
882
883 if (first)
884 {
885 stream->wrap_here (4);
886 gdb_printf (stream, _("[with %s = "), sym->linkage_name ());
887 first = 0;
888 }
889 else
890 {
891 gdb_puts (", ", stream);
892 stream->wrap_here (9);
893 gdb_printf (stream, "%s = ", sym->linkage_name ());
894 }
895
896 c_print_type (sym->type (), "", stream, -1, 0, language, flags);
897 }
898
899 if (!first)
900 gdb_puts (_("] "), stream);
901 }
902
903 /* Use 'print_spaces', but take into consideration the
904 type_print_options FLAGS in order to determine how many whitespaces
905 will be printed. */
906
907 static void
908 print_spaces_filtered_with_print_options
909 (int level, struct ui_file *stream, const struct type_print_options *flags)
910 {
911 if (!flags->print_offsets)
912 print_spaces (level, stream);
913 else
914 print_spaces (level + print_offset_data::indentation, stream);
915 }
916
917 /* Output an access specifier to STREAM, if needed. LAST_ACCESS is the
918 last access specifier output (typically returned by this function). */
919
920 static enum access_specifier
921 output_access_specifier (struct ui_file *stream,
922 enum access_specifier last_access,
923 int level, bool is_protected, bool is_private,
924 const struct type_print_options *flags)
925 {
926 if (is_protected)
927 {
928 if (last_access != s_protected)
929 {
930 last_access = s_protected;
931 print_spaces_filtered_with_print_options (level + 2, stream, flags);
932 gdb_printf (stream, "protected:\n");
933 }
934 }
935 else if (is_private)
936 {
937 if (last_access != s_private)
938 {
939 last_access = s_private;
940 print_spaces_filtered_with_print_options (level + 2, stream, flags);
941 gdb_printf (stream, "private:\n");
942 }
943 }
944 else
945 {
946 if (last_access != s_public)
947 {
948 last_access = s_public;
949 print_spaces_filtered_with_print_options (level + 2, stream, flags);
950 gdb_printf (stream, "public:\n");
951 }
952 }
953
954 return last_access;
955 }
956
957 /* Return true if an access label (i.e., "public:", "private:",
958 "protected:") needs to be printed for TYPE. */
959
960 static bool
961 need_access_label_p (struct type *type)
962 {
963 if (type->is_declared_class ())
964 {
965 QUIT;
966 for (int i = TYPE_N_BASECLASSES (type); i < type->num_fields (); i++)
967 if (!TYPE_FIELD_PRIVATE (type, i))
968 return true;
969 QUIT;
970 for (int j = 0; j < TYPE_NFN_FIELDS (type); j++)
971 for (int i = 0; i < TYPE_FN_FIELDLIST_LENGTH (type, j); i++)
972 if (!TYPE_FN_FIELD_PRIVATE (TYPE_FN_FIELDLIST1 (type,
973 j), i))
974 return true;
975 QUIT;
976 for (int i = 0; i < TYPE_TYPEDEF_FIELD_COUNT (type); ++i)
977 if (!TYPE_TYPEDEF_FIELD_PRIVATE (type, i))
978 return true;
979 }
980 else
981 {
982 QUIT;
983 for (int i = TYPE_N_BASECLASSES (type); i < type->num_fields (); i++)
984 if (TYPE_FIELD_PRIVATE (type, i) || TYPE_FIELD_PROTECTED (type, i))
985 return true;
986 QUIT;
987 for (int j = 0; j < TYPE_NFN_FIELDS (type); j++)
988 {
989 QUIT;
990 for (int i = 0; i < TYPE_FN_FIELDLIST_LENGTH (type, j); i++)
991 if (TYPE_FN_FIELD_PROTECTED (TYPE_FN_FIELDLIST1 (type,
992 j), i)
993 || TYPE_FN_FIELD_PRIVATE (TYPE_FN_FIELDLIST1 (type,
994 j),
995 i))
996 return true;
997 }
998 QUIT;
999 for (int i = 0; i < TYPE_TYPEDEF_FIELD_COUNT (type); ++i)
1000 if (TYPE_TYPEDEF_FIELD_PROTECTED (type, i)
1001 || TYPE_TYPEDEF_FIELD_PRIVATE (type, i))
1002 return true;
1003 }
1004
1005 return false;
1006 }
1007
1008 /* Helper function that temporarily disables FLAGS->PRINT_OFFSETS,
1009 calls 'c_print_type_1', and then reenables FLAGS->PRINT_OFFSETS if
1010 applicable. */
1011
1012 static void
1013 c_print_type_no_offsets (struct type *type,
1014 const char *varstring,
1015 struct ui_file *stream,
1016 int show, int level,
1017 enum language language,
1018 struct type_print_options *flags,
1019 struct print_offset_data *podata)
1020 {
1021 unsigned int old_po = flags->print_offsets;
1022
1023 /* Temporarily disable print_offsets, because it would mess with
1024 indentation. */
1025 flags->print_offsets = 0;
1026 c_print_type_1 (type, varstring, stream, show, level, language, flags,
1027 podata);
1028 flags->print_offsets = old_po;
1029 }
1030
1031 /* Helper for 'c_type_print_base' that handles structs and unions.
1032 For a description of the arguments, see 'c_type_print_base'. */
1033
1034 static void
1035 c_type_print_base_struct_union (struct type *type, struct ui_file *stream,
1036 int show, int level,
1037 enum language language,
1038 const struct type_print_options *flags,
1039 struct print_offset_data *podata)
1040 {
1041 struct type_print_options local_flags = *flags;
1042 local_flags.local_typedefs = NULL;
1043
1044 std::unique_ptr<typedef_hash_table> hash_holder;
1045 if (!flags->raw)
1046 {
1047 if (flags->local_typedefs)
1048 local_flags.local_typedefs
1049 = new typedef_hash_table (*flags->local_typedefs);
1050 else
1051 local_flags.local_typedefs = new typedef_hash_table ();
1052
1053 hash_holder.reset (local_flags.local_typedefs);
1054 }
1055
1056 c_type_print_modifier (type, stream, 0, 1, language);
1057 if (type->code () == TYPE_CODE_UNION)
1058 gdb_printf (stream, "union ");
1059 else if (type->is_declared_class ())
1060 gdb_printf (stream, "class ");
1061 else
1062 gdb_printf (stream, "struct ");
1063
1064 /* Print the tag if it exists. The HP aCC compiler emits a
1065 spurious "{unnamed struct}"/"{unnamed union}"/"{unnamed
1066 enum}" tag for unnamed struct/union/enum's, which we don't
1067 want to print. */
1068 if (type->name () != NULL
1069 && !startswith (type->name (), "{unnamed"))
1070 {
1071 /* When printing the tag name, we are still effectively
1072 printing in the outer context, hence the use of FLAGS
1073 here. */
1074 print_name_maybe_canonical (type->name (), flags, stream);
1075 if (show > 0)
1076 gdb_puts (" ", stream);
1077 }
1078
1079 if (show < 0)
1080 {
1081 /* If we just printed a tag name, no need to print anything
1082 else. */
1083 if (type->name () == NULL)
1084 gdb_printf (stream, "{...}");
1085 }
1086 else if (show > 0 || type->name () == NULL)
1087 {
1088 struct type *basetype;
1089 int vptr_fieldno;
1090
1091 c_type_print_template_args (&local_flags, type, stream, language);
1092
1093 /* Add in template parameters when printing derivation info. */
1094 if (local_flags.local_typedefs != NULL)
1095 local_flags.local_typedefs->add_template_parameters (type);
1096 cp_type_print_derivation_info (stream, type, &local_flags);
1097
1098 /* This holds just the global typedefs and the template
1099 parameters. */
1100 struct type_print_options semi_local_flags = *flags;
1101 semi_local_flags.local_typedefs = NULL;
1102
1103 std::unique_ptr<typedef_hash_table> semi_holder;
1104 if (local_flags.local_typedefs != nullptr)
1105 {
1106 semi_local_flags.local_typedefs
1107 = new typedef_hash_table (*local_flags.local_typedefs);
1108 semi_holder.reset (semi_local_flags.local_typedefs);
1109
1110 /* Now add in the local typedefs. */
1111 local_flags.local_typedefs->recursively_update (type);
1112 }
1113
1114 gdb_printf (stream, "{\n");
1115
1116 if (type->num_fields () == 0 && TYPE_NFN_FIELDS (type) == 0
1117 && TYPE_TYPEDEF_FIELD_COUNT (type) == 0)
1118 {
1119 print_spaces_filtered_with_print_options (level + 4, stream, flags);
1120 if (type->is_stub ())
1121 gdb_printf (stream, _("%p[<incomplete type>%p]\n"),
1122 metadata_style.style ().ptr (), nullptr);
1123 else
1124 gdb_printf (stream, _("%p[<no data fields>%p]\n"),
1125 metadata_style.style ().ptr (), nullptr);
1126 }
1127
1128 /* Start off with no specific section type, so we can print
1129 one for the first field we find, and use that section type
1130 thereafter until we find another type. */
1131
1132 enum access_specifier section_type = s_none;
1133
1134 /* For a class, if all members are private, there's no need
1135 for a "private:" label; similarly, for a struct or union
1136 masquerading as a class, if all members are public, there's
1137 no need for a "public:" label. */
1138 bool need_access_label = need_access_label_p (type);
1139
1140 /* If there is a base class for this type,
1141 do not print the field that it occupies. */
1142
1143 int len = type->num_fields ();
1144 vptr_fieldno = get_vptr_fieldno (type, &basetype);
1145
1146 struct print_offset_data local_podata (flags);
1147
1148 for (int i = TYPE_N_BASECLASSES (type); i < len; i++)
1149 {
1150 QUIT;
1151
1152 /* If we have a virtual table pointer, omit it. Even if
1153 virtual table pointers are not specifically marked in
1154 the debug info, they should be artificial. */
1155 if ((i == vptr_fieldno && type == basetype)
1156 || TYPE_FIELD_ARTIFICIAL (type, i))
1157 continue;
1158
1159 if (need_access_label)
1160 {
1161 section_type = output_access_specifier
1162 (stream, section_type, level,
1163 TYPE_FIELD_PROTECTED (type, i),
1164 TYPE_FIELD_PRIVATE (type, i), flags);
1165 }
1166
1167 bool is_static = field_is_static (&type->field (i));
1168
1169 if (flags->print_offsets)
1170 podata->update (type, i, stream);
1171
1172 print_spaces (level + 4, stream);
1173 if (is_static)
1174 gdb_printf (stream, "static ");
1175
1176 int newshow = show - 1;
1177
1178 if (!is_static && flags->print_offsets
1179 && (type->field (i).type ()->code () == TYPE_CODE_STRUCT
1180 || type->field (i).type ()->code () == TYPE_CODE_UNION))
1181 {
1182 /* If we're printing offsets and this field's type is
1183 either a struct or an union, then we're interested in
1184 expanding it. */
1185 ++newshow;
1186
1187 /* Make sure we carry our offset when we expand the
1188 struct/union. */
1189 local_podata.offset_bitpos
1190 = podata->offset_bitpos + type->field (i).loc_bitpos ();
1191 /* We're entering a struct/union. Right now,
1192 PODATA->END_BITPOS points right *after* the
1193 struct/union. However, when printing the first field
1194 of this inner struct/union, the end_bitpos we're
1195 expecting is exactly at the beginning of the
1196 struct/union. Therefore, we subtract the length of
1197 the whole struct/union. */
1198 local_podata.end_bitpos
1199 = podata->end_bitpos
1200 - type->field (i).type ()->length () * TARGET_CHAR_BIT;
1201 }
1202
1203 c_print_type_1 (type->field (i).type (),
1204 type->field (i).name (),
1205 stream, newshow, level + 4,
1206 language, &local_flags, &local_podata);
1207
1208 if (!is_static && TYPE_FIELD_PACKED (type, i))
1209 {
1210 /* It is a bitfield. This code does not attempt
1211 to look at the bitpos and reconstruct filler,
1212 unnamed fields. This would lead to misleading
1213 results if the compiler does not put out fields
1214 for such things (I don't know what it does). */
1215 gdb_printf (stream, " : %d",
1216 TYPE_FIELD_BITSIZE (type, i));
1217 }
1218 gdb_printf (stream, ";\n");
1219 }
1220
1221 /* If there are both fields and methods, put a blank line
1222 between them. Make sure to count only method that we
1223 will display; artificial methods will be hidden. */
1224 len = TYPE_NFN_FIELDS (type);
1225 if (!flags->print_methods)
1226 len = 0;
1227 int real_len = 0;
1228 for (int i = 0; i < len; i++)
1229 {
1230 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1231 int len2 = TYPE_FN_FIELDLIST_LENGTH (type, i);
1232 int j;
1233
1234 for (j = 0; j < len2; j++)
1235 if (!TYPE_FN_FIELD_ARTIFICIAL (f, j))
1236 real_len++;
1237 }
1238 if (real_len > 0 && section_type != s_none)
1239 gdb_printf (stream, "\n");
1240
1241 /* C++: print out the methods. */
1242 for (int i = 0; i < len; i++)
1243 {
1244 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1245 int j, len2 = TYPE_FN_FIELDLIST_LENGTH (type, i);
1246 const char *method_name = TYPE_FN_FIELDLIST_NAME (type, i);
1247 const char *name = type->name ();
1248 int is_constructor = name && strcmp (method_name,
1249 name) == 0;
1250
1251 for (j = 0; j < len2; j++)
1252 {
1253 const char *mangled_name;
1254 gdb::unique_xmalloc_ptr<char> mangled_name_holder;
1255 const char *physname = TYPE_FN_FIELD_PHYSNAME (f, j);
1256 int is_full_physname_constructor =
1257 TYPE_FN_FIELD_CONSTRUCTOR (f, j)
1258 || is_constructor_name (physname)
1259 || is_destructor_name (physname)
1260 || method_name[0] == '~';
1261
1262 /* Do not print out artificial methods. */
1263 if (TYPE_FN_FIELD_ARTIFICIAL (f, j))
1264 continue;
1265
1266 QUIT;
1267 section_type = output_access_specifier
1268 (stream, section_type, level,
1269 TYPE_FN_FIELD_PROTECTED (f, j),
1270 TYPE_FN_FIELD_PRIVATE (f, j), flags);
1271
1272 print_spaces_filtered_with_print_options (level + 4, stream,
1273 flags);
1274 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1275 gdb_printf (stream, "virtual ");
1276 else if (TYPE_FN_FIELD_STATIC_P (f, j))
1277 gdb_printf (stream, "static ");
1278 if (TYPE_FN_FIELD_TYPE (f, j)->target_type () == 0)
1279 {
1280 /* Keep GDB from crashing here. */
1281 gdb_printf (stream,
1282 _("%p[<undefined type>%p] %s;\n"),
1283 metadata_style.style ().ptr (), nullptr,
1284 TYPE_FN_FIELD_PHYSNAME (f, j));
1285 break;
1286 }
1287 else if (!is_constructor /* Constructors don't
1288 have declared
1289 types. */
1290 && !is_full_physname_constructor /* " " */
1291 && !is_type_conversion_operator (type, i, j))
1292 {
1293 c_print_type_no_offsets
1294 (TYPE_FN_FIELD_TYPE (f, j)->target_type (),
1295 "", stream, -1, 0, language, &local_flags, podata);
1296
1297 gdb_puts (" ", stream);
1298 }
1299 if (TYPE_FN_FIELD_STUB (f, j))
1300 {
1301 /* Build something we can demangle. */
1302 mangled_name_holder.reset (gdb_mangle_name (type, i, j));
1303 mangled_name = mangled_name_holder.get ();
1304 }
1305 else
1306 mangled_name = TYPE_FN_FIELD_PHYSNAME (f, j);
1307
1308 gdb::unique_xmalloc_ptr<char> demangled_name
1309 = gdb_demangle (mangled_name,
1310 DMGL_ANSI | DMGL_PARAMS);
1311 if (demangled_name == NULL)
1312 {
1313 /* In some cases (for instance with the HP
1314 demangling), if a function has more than 10
1315 arguments, the demangling will fail.
1316 Let's try to reconstruct the function
1317 signature from the symbol information. */
1318 if (!TYPE_FN_FIELD_STUB (f, j))
1319 {
1320 int staticp = TYPE_FN_FIELD_STATIC_P (f, j);
1321 struct type *mtype = TYPE_FN_FIELD_TYPE (f, j);
1322
1323 cp_type_print_method_args (mtype,
1324 "",
1325 method_name,
1326 staticp,
1327 stream, language,
1328 &local_flags);
1329 }
1330 else
1331 fprintf_styled (stream, metadata_style.style (),
1332 _("<badly mangled name '%s'>"),
1333 mangled_name);
1334 }
1335 else
1336 {
1337 const char *p;
1338 const char *demangled_no_class
1339 = remove_qualifiers (demangled_name.get ());
1340
1341 /* Get rid of the `static' appended by the
1342 demangler. */
1343 p = strstr (demangled_no_class, " static");
1344 if (p != NULL)
1345 {
1346 int length = p - demangled_no_class;
1347 std::string demangled_no_static (demangled_no_class,
1348 length);
1349 gdb_puts (demangled_no_static.c_str (), stream);
1350 }
1351 else
1352 gdb_puts (demangled_no_class, stream);
1353 }
1354
1355 gdb_printf (stream, ";\n");
1356 }
1357 }
1358
1359 /* Print out nested types. */
1360 if (TYPE_NESTED_TYPES_COUNT (type) != 0
1361 && semi_local_flags.print_nested_type_limit != 0)
1362 {
1363 if (semi_local_flags.print_nested_type_limit > 0)
1364 --semi_local_flags.print_nested_type_limit;
1365
1366 if (type->num_fields () != 0 || TYPE_NFN_FIELDS (type) != 0)
1367 gdb_printf (stream, "\n");
1368
1369 for (int i = 0; i < TYPE_NESTED_TYPES_COUNT (type); ++i)
1370 {
1371 print_spaces_filtered_with_print_options (level + 4, stream,
1372 flags);
1373 c_print_type_no_offsets (TYPE_NESTED_TYPES_FIELD_TYPE (type, i),
1374 "", stream, show, level + 4,
1375 language, &semi_local_flags, podata);
1376 gdb_printf (stream, ";\n");
1377 }
1378 }
1379
1380 /* Print typedefs defined in this class. */
1381
1382 if (TYPE_TYPEDEF_FIELD_COUNT (type) != 0 && flags->print_typedefs)
1383 {
1384 if (type->num_fields () != 0 || TYPE_NFN_FIELDS (type) != 0
1385 || TYPE_NESTED_TYPES_COUNT (type) != 0)
1386 gdb_printf (stream, "\n");
1387
1388 for (int i = 0; i < TYPE_TYPEDEF_FIELD_COUNT (type); i++)
1389 {
1390 struct type *target = TYPE_TYPEDEF_FIELD_TYPE (type, i);
1391
1392 /* Dereference the typedef declaration itself. */
1393 gdb_assert (target->code () == TYPE_CODE_TYPEDEF);
1394 target = target->target_type ();
1395
1396 if (need_access_label)
1397 {
1398 section_type = output_access_specifier
1399 (stream, section_type, level,
1400 TYPE_TYPEDEF_FIELD_PROTECTED (type, i),
1401 TYPE_TYPEDEF_FIELD_PRIVATE (type, i), flags);
1402 }
1403 print_spaces_filtered_with_print_options (level + 4, stream,
1404 flags);
1405 gdb_printf (stream, "typedef ");
1406
1407 /* We want to print typedefs with substitutions
1408 from the template parameters or globally-known
1409 typedefs but not local typedefs. */
1410 c_print_type_no_offsets (target,
1411 TYPE_TYPEDEF_FIELD_NAME (type, i),
1412 stream, show - 1, level + 4,
1413 language, &semi_local_flags, podata);
1414 gdb_printf (stream, ";\n");
1415 }
1416 }
1417
1418 if (flags->print_offsets)
1419 {
1420 if (show > 0)
1421 podata->finish (type, level, stream);
1422
1423 print_spaces (print_offset_data::indentation, stream);
1424 if (level == 0)
1425 print_spaces (2, stream);
1426 }
1427
1428 gdb_printf (stream, "%*s}", level, "");
1429 }
1430 }
1431
1432 /* Print the name of the type (or the ultimate pointer target,
1433 function value or array element), or the description of a structure
1434 or union.
1435
1436 SHOW positive means print details about the type (e.g. enum
1437 values), and print structure elements passing SHOW - 1 for show.
1438
1439 SHOW negative means just print the type name or struct tag if there
1440 is one. If there is no name, print something sensible but concise
1441 like "struct {...}".
1442
1443 SHOW zero means just print the type name or struct tag if there is
1444 one. If there is no name, print something sensible but not as
1445 concise like "struct {int x; int y;}".
1446
1447 LEVEL is the number of spaces to indent by.
1448 We increase it for some recursive calls. */
1449
1450 static void
1451 c_type_print_base_1 (struct type *type, struct ui_file *stream,
1452 int show, int level,
1453 enum language language,
1454 const struct type_print_options *flags,
1455 struct print_offset_data *podata)
1456 {
1457 int i;
1458 int len;
1459
1460 QUIT;
1461
1462 if (type == NULL)
1463 {
1464 fputs_styled (_("<type unknown>"), metadata_style.style (), stream);
1465 return;
1466 }
1467
1468 /* When SHOW is zero or less, and there is a valid type name, then
1469 always just print the type name directly from the type. */
1470
1471 if (show <= 0
1472 && type->name () != NULL)
1473 {
1474 c_type_print_modifier (type, stream, 0, 1, language);
1475
1476 /* If we have "typedef struct foo {. . .} bar;" do we want to
1477 print it as "struct foo" or as "bar"? Pick the latter for
1478 C++, because C++ folk tend to expect things like "class5
1479 *foo" rather than "struct class5 *foo". We rather
1480 arbitrarily choose to make language_minimal work in a C-like
1481 way. */
1482 if (language == language_c || language == language_minimal)
1483 {
1484 if (type->code () == TYPE_CODE_UNION)
1485 gdb_printf (stream, "union ");
1486 else if (type->code () == TYPE_CODE_STRUCT)
1487 {
1488 if (type->is_declared_class ())
1489 gdb_printf (stream, "class ");
1490 else
1491 gdb_printf (stream, "struct ");
1492 }
1493 else if (type->code () == TYPE_CODE_ENUM)
1494 gdb_printf (stream, "enum ");
1495 }
1496
1497 print_name_maybe_canonical (type->name (), flags, stream);
1498 return;
1499 }
1500
1501 type = check_typedef (type);
1502
1503 switch (type->code ())
1504 {
1505 case TYPE_CODE_TYPEDEF:
1506 /* If we get here, the typedef doesn't have a name, and we
1507 couldn't resolve type::target_type. Not much we can do. */
1508 gdb_assert (type->name () == NULL);
1509 gdb_assert (type->target_type () == NULL);
1510 fprintf_styled (stream, metadata_style.style (),
1511 _("<unnamed typedef>"));
1512 break;
1513
1514 case TYPE_CODE_FUNC:
1515 case TYPE_CODE_METHOD:
1516 if (type->target_type () == NULL)
1517 type_print_unknown_return_type (stream);
1518 else
1519 c_type_print_base_1 (type->target_type (),
1520 stream, show, level, language, flags, podata);
1521 break;
1522 case TYPE_CODE_ARRAY:
1523 case TYPE_CODE_PTR:
1524 case TYPE_CODE_MEMBERPTR:
1525 case TYPE_CODE_REF:
1526 case TYPE_CODE_RVALUE_REF:
1527 case TYPE_CODE_METHODPTR:
1528 c_type_print_base_1 (type->target_type (),
1529 stream, show, level, language, flags, podata);
1530 break;
1531
1532 case TYPE_CODE_STRUCT:
1533 case TYPE_CODE_UNION:
1534 c_type_print_base_struct_union (type, stream, show, level,
1535 language, flags, podata);
1536 break;
1537
1538 case TYPE_CODE_ENUM:
1539 c_type_print_modifier (type, stream, 0, 1, language);
1540 gdb_printf (stream, "enum ");
1541 if (type->is_declared_class ())
1542 gdb_printf (stream, "class ");
1543 /* Print the tag name if it exists.
1544 The aCC compiler emits a spurious
1545 "{unnamed struct}"/"{unnamed union}"/"{unnamed enum}"
1546 tag for unnamed struct/union/enum's, which we don't
1547 want to print. */
1548 if (type->name () != NULL
1549 && !startswith (type->name (), "{unnamed"))
1550 {
1551 print_name_maybe_canonical (type->name (), flags, stream);
1552 if (show > 0)
1553 gdb_puts (" ", stream);
1554 }
1555
1556 stream->wrap_here (4);
1557 if (show < 0)
1558 {
1559 /* If we just printed a tag name, no need to print anything
1560 else. */
1561 if (type->name () == NULL)
1562 gdb_printf (stream, "{...}");
1563 }
1564 else if (show > 0 || type->name () == NULL)
1565 {
1566 LONGEST lastval = 0;
1567
1568 /* We can't handle this case perfectly, as DWARF does not
1569 tell us whether or not the underlying type was specified
1570 in the source (and other debug formats don't provide this
1571 at all). We choose to print the underlying type, if it
1572 has a name, when in C++ on the theory that it's better to
1573 print too much than too little; but conversely not to
1574 print something egregiously outside the current
1575 language's syntax. */
1576 if (language == language_cplus && type->target_type () != NULL)
1577 {
1578 struct type *underlying = check_typedef (type->target_type ());
1579
1580 if (underlying->name () != NULL)
1581 gdb_printf (stream, ": %s ", underlying->name ());
1582 }
1583
1584 gdb_printf (stream, "{");
1585 len = type->num_fields ();
1586 for (i = 0; i < len; i++)
1587 {
1588 QUIT;
1589 if (i)
1590 gdb_printf (stream, ", ");
1591 stream->wrap_here (4);
1592 fputs_styled (type->field (i).name (),
1593 variable_name_style.style (), stream);
1594 if (lastval != type->field (i).loc_enumval ())
1595 {
1596 gdb_printf (stream, " = %s",
1597 plongest (type->field (i).loc_enumval ()));
1598 lastval = type->field (i).loc_enumval ();
1599 }
1600 lastval++;
1601 }
1602 gdb_printf (stream, "}");
1603 }
1604 break;
1605
1606 case TYPE_CODE_FLAGS:
1607 {
1608 struct type_print_options local_flags = *flags;
1609
1610 local_flags.local_typedefs = NULL;
1611
1612 c_type_print_modifier (type, stream, 0, 1, language);
1613 gdb_printf (stream, "flag ");
1614 print_name_maybe_canonical (type->name (), flags, stream);
1615 if (show > 0)
1616 {
1617 gdb_puts (" ", stream);
1618 gdb_printf (stream, "{\n");
1619 if (type->num_fields () == 0)
1620 {
1621 if (type->is_stub ())
1622 gdb_printf (stream,
1623 _("%*s%p[<incomplete type>%p]\n"),
1624 level + 4, "",
1625 metadata_style.style ().ptr (), nullptr);
1626 else
1627 gdb_printf (stream,
1628 _("%*s%p[<no data fields>%p]\n"),
1629 level + 4, "",
1630 metadata_style.style ().ptr (), nullptr);
1631 }
1632 len = type->num_fields ();
1633 for (i = 0; i < len; i++)
1634 {
1635 QUIT;
1636 print_spaces (level + 4, stream);
1637 /* We pass "show" here and not "show - 1" to get enum types
1638 printed. There's no other way to see them. */
1639 c_print_type_1 (type->field (i).type (),
1640 type->field (i).name (),
1641 stream, show, level + 4,
1642 language, &local_flags, podata);
1643 gdb_printf (stream, " @%s",
1644 plongest (type->field (i).loc_bitpos ()));
1645 if (TYPE_FIELD_BITSIZE (type, i) > 1)
1646 {
1647 gdb_printf (stream, "-%s",
1648 plongest (type->field (i).loc_bitpos ()
1649 + TYPE_FIELD_BITSIZE (type, i)
1650 - 1));
1651 }
1652 gdb_printf (stream, ";\n");
1653 }
1654 gdb_printf (stream, "%*s}", level, "");
1655 }
1656 }
1657 break;
1658
1659 case TYPE_CODE_VOID:
1660 gdb_printf (stream, "void");
1661 break;
1662
1663 case TYPE_CODE_UNDEF:
1664 gdb_printf (stream, _("struct <unknown>"));
1665 break;
1666
1667 case TYPE_CODE_ERROR:
1668 gdb_printf (stream, "%s", TYPE_ERROR_NAME (type));
1669 break;
1670
1671 case TYPE_CODE_RANGE:
1672 /* This should not occur. */
1673 fprintf_styled (stream, metadata_style.style (), _("<range type>"));
1674 break;
1675
1676 case TYPE_CODE_FIXED_POINT:
1677 print_type_fixed_point (type, stream);
1678 break;
1679
1680 case TYPE_CODE_NAMESPACE:
1681 gdb_puts ("namespace ", stream);
1682 gdb_puts (type->name (), stream);
1683 break;
1684
1685 default:
1686 /* Handle types not explicitly handled by the other cases, such
1687 as fundamental types. For these, just print whatever the
1688 type name is, as recorded in the type itself. If there is no
1689 type name, then complain. */
1690 if (type->name () != NULL)
1691 {
1692 c_type_print_modifier (type, stream, 0, 1, language);
1693 print_name_maybe_canonical (type->name (), flags, stream);
1694 }
1695 else
1696 {
1697 /* At least for dump_symtab, it is important that this not
1698 be an error (). */
1699 fprintf_styled (stream, metadata_style.style (),
1700 _("<invalid type code %d>"), type->code ());
1701 }
1702 break;
1703 }
1704 }
1705
1706 /* See c_type_print_base_1. */
1707
1708 void
1709 c_type_print_base (struct type *type, struct ui_file *stream,
1710 int show, int level,
1711 const struct type_print_options *flags)
1712 {
1713 struct print_offset_data podata (flags);
1714
1715 c_type_print_base_1 (type, stream, show, level,
1716 current_language->la_language, flags, &podata);
1717 }