Remove path name from test case
[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"
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 (arg.is_artificial ())
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 }
446
447 /* Print out "const" and "volatile" attributes,
448 and address space id if present.
449 TYPE is a pointer to the type being printed out.
450 STREAM is the output destination.
451 NEED_PRE_SPACE = 1 indicates an initial white space is needed.
452 NEED_POST_SPACE = 1 indicates a final white space is needed. */
453
454 static void
455 c_type_print_modifier (struct type *type, struct ui_file *stream,
456 int need_pre_space, int need_post_space,
457 enum language language)
458 {
459 int did_print_modifier = 0;
460 const char *address_space_id;
461
462 /* We don't print `const' qualifiers for references --- since all
463 operators affect the thing referenced, not the reference itself,
464 every reference is `const'. */
465 if (TYPE_CONST (type) && !TYPE_IS_REFERENCE (type))
466 {
467 if (need_pre_space)
468 gdb_printf (stream, " ");
469 gdb_printf (stream, "const");
470 did_print_modifier = 1;
471 }
472
473 if (TYPE_VOLATILE (type))
474 {
475 if (did_print_modifier || need_pre_space)
476 gdb_printf (stream, " ");
477 gdb_printf (stream, "volatile");
478 did_print_modifier = 1;
479 }
480
481 if (TYPE_RESTRICT (type))
482 {
483 if (did_print_modifier || need_pre_space)
484 gdb_printf (stream, " ");
485 gdb_printf (stream, (language == language_cplus
486 ? "__restrict__"
487 : "restrict"));
488 did_print_modifier = 1;
489 }
490
491 if (TYPE_ATOMIC (type))
492 {
493 if (did_print_modifier || need_pre_space)
494 gdb_printf (stream, " ");
495 gdb_printf (stream, "_Atomic");
496 did_print_modifier = 1;
497 }
498
499 address_space_id
500 = address_space_type_instance_flags_to_name (type->arch (),
501 type->instance_flags ());
502 if (address_space_id)
503 {
504 if (did_print_modifier || need_pre_space)
505 gdb_printf (stream, " ");
506 gdb_printf (stream, "@%s", address_space_id);
507 did_print_modifier = 1;
508 }
509
510 if (did_print_modifier && need_post_space)
511 gdb_printf (stream, " ");
512 }
513
514
515 /* Print out the arguments of TYPE, which should have TYPE_CODE_METHOD
516 or TYPE_CODE_FUNC, to STREAM. Artificial arguments, such as "this"
517 in non-static methods, are displayed if LINKAGE_NAME is zero. If
518 LINKAGE_NAME is non-zero and LANGUAGE is language_cplus the topmost
519 parameter types get removed their possible const and volatile qualifiers to
520 match demangled linkage name parameters part of such function type.
521 LANGUAGE is the language in which TYPE was defined. This is a necessary
522 evil since this code is used by the C and C++. */
523
524 void
525 c_type_print_args (struct type *type, struct ui_file *stream,
526 int linkage_name, enum language language,
527 const struct type_print_options *flags)
528 {
529 int i;
530 int printed_any = 0;
531
532 gdb_printf (stream, "(");
533
534 for (i = 0; i < type->num_fields (); i++)
535 {
536 struct type *param_type;
537
538 if (type->field (i).is_artificial () && linkage_name)
539 continue;
540
541 if (printed_any)
542 {
543 gdb_printf (stream, ", ");
544 stream->wrap_here (4);
545 }
546
547 param_type = type->field (i).type ();
548
549 if (language == language_cplus && linkage_name)
550 {
551 /* C++ standard, 13.1 Overloadable declarations, point 3, item:
552 - Parameter declarations that differ only in the presence or
553 absence of const and/or volatile are equivalent.
554
555 And the const/volatile qualifiers are not present in the mangled
556 names as produced by GCC. */
557
558 param_type = make_cv_type (0, 0, param_type, NULL);
559 }
560
561 c_print_type (param_type, "", stream, -1, 0, language, flags);
562 printed_any = 1;
563 }
564
565 if (printed_any && type->has_varargs ())
566 {
567 /* Print out a trailing ellipsis for varargs functions. Ignore
568 TYPE_VARARGS if the function has no named arguments; that
569 represents unprototyped (K&R style) C functions. */
570 if (printed_any && type->has_varargs ())
571 {
572 gdb_printf (stream, ", ");
573 stream->wrap_here (4);
574 gdb_printf (stream, "...");
575 }
576 }
577 else if (!printed_any
578 && (type->is_prototyped () || language == language_cplus))
579 gdb_printf (stream, "void");
580
581 gdb_printf (stream, ")");
582 }
583
584 /* Return true iff the j'th overloading of the i'th method of TYPE
585 is a type conversion operator, like `operator int () { ... }'.
586 When listing a class's methods, we don't print the return type of
587 such operators. */
588
589 static int
590 is_type_conversion_operator (struct type *type, int i, int j)
591 {
592 /* I think the whole idea of recognizing type conversion operators
593 by their name is pretty terrible. But I don't think our present
594 data structure gives us any other way to tell. If you know of
595 some other way, feel free to rewrite this function. */
596 const char *name = TYPE_FN_FIELDLIST_NAME (type, i);
597
598 if (!startswith (name, CP_OPERATOR_STR))
599 return 0;
600
601 name += 8;
602 if (! strchr (" \t\f\n\r", *name))
603 return 0;
604
605 while (strchr (" \t\f\n\r", *name))
606 name++;
607
608 if (!('a' <= *name && *name <= 'z')
609 && !('A' <= *name && *name <= 'Z')
610 && *name != '_')
611 /* If this doesn't look like the start of an identifier, then it
612 isn't a type conversion operator. */
613 return 0;
614 else if (startswith (name, "new"))
615 name += 3;
616 else if (startswith (name, "delete"))
617 name += 6;
618 else
619 /* If it doesn't look like new or delete, it's a type conversion
620 operator. */
621 return 1;
622
623 /* Is that really the end of the name? */
624 if (('a' <= *name && *name <= 'z')
625 || ('A' <= *name && *name <= 'Z')
626 || ('0' <= *name && *name <= '9')
627 || *name == '_')
628 /* No, so the identifier following "operator" must be a type name,
629 and this is a type conversion operator. */
630 return 1;
631
632 /* That was indeed the end of the name, so it was `operator new' or
633 `operator delete', neither of which are type conversion
634 operators. */
635 return 0;
636 }
637
638 /* Given a C++ qualified identifier QID, strip off the qualifiers,
639 yielding the unqualified name. The return value is a pointer into
640 the original string.
641
642 It's a pity we don't have this information in some more structured
643 form. Even the author of this function feels that writing little
644 parsers like this everywhere is stupid. */
645
646 static const char *
647 remove_qualifiers (const char *qid)
648 {
649 int quoted = 0; /* Zero if we're not in quotes;
650 '"' if we're in a double-quoted string;
651 '\'' if we're in a single-quoted string. */
652 int depth = 0; /* Number of unclosed parens we've seen. */
653 char *parenstack = (char *) alloca (strlen (qid));
654 const char *scan;
655 const char *last = 0; /* The character after the rightmost
656 `::' token we've seen so far. */
657
658 for (scan = qid; *scan; scan++)
659 {
660 if (quoted)
661 {
662 if (*scan == quoted)
663 quoted = 0;
664 else if (*scan == '\\' && *(scan + 1))
665 scan++;
666 }
667 else if (scan[0] == ':' && scan[1] == ':')
668 {
669 /* If we're inside parenthesis (i.e., an argument list) or
670 angle brackets (i.e., a list of template arguments), then
671 we don't record the position of this :: token, since it's
672 not relevant to the top-level structure we're trying to
673 operate on. */
674 if (depth == 0)
675 {
676 last = scan + 2;
677 scan++;
678 }
679 }
680 else if (*scan == '"' || *scan == '\'')
681 quoted = *scan;
682 else if (*scan == '(')
683 parenstack[depth++] = ')';
684 else if (*scan == '[')
685 parenstack[depth++] = ']';
686 /* We're going to treat <> as a pair of matching characters,
687 since we're more likely to see those in template id's than
688 real less-than characters. What a crock. */
689 else if (*scan == '<')
690 parenstack[depth++] = '>';
691 else if (*scan == ')' || *scan == ']' || *scan == '>')
692 {
693 if (depth > 0 && parenstack[depth - 1] == *scan)
694 depth--;
695 else
696 {
697 /* We're going to do a little error recovery here. If
698 we don't find a match for *scan on the paren stack,
699 but there is something lower on the stack that does
700 match, we pop the stack to that point. */
701 int i;
702
703 for (i = depth - 1; i >= 0; i--)
704 if (parenstack[i] == *scan)
705 {
706 depth = i;
707 break;
708 }
709 }
710 }
711 }
712
713 if (last)
714 return last;
715 else
716 /* We didn't find any :: tokens at the top level, so declare the
717 whole thing an unqualified identifier. */
718 return qid;
719 }
720
721 /* Print any array sizes, function arguments or close parentheses
722 needed after the variable name (to describe its type).
723 Args work like c_type_print_varspec_prefix. */
724
725 static void
726 c_type_print_varspec_suffix (struct type *type,
727 struct ui_file *stream,
728 int show, int passed_a_ptr,
729 int demangled_args,
730 enum language language,
731 const struct type_print_options *flags)
732 {
733 if (type == 0)
734 return;
735
736 if (type->name () && show <= 0)
737 return;
738
739 QUIT;
740
741 switch (type->code ())
742 {
743 case TYPE_CODE_ARRAY:
744 {
745 LONGEST low_bound, high_bound;
746 int is_vector = type->is_vector ();
747
748 if (passed_a_ptr)
749 gdb_printf (stream, ")");
750
751 gdb_printf (stream, (is_vector ?
752 " __attribute__ ((vector_size(" : "["));
753 /* Bounds are not yet resolved, print a bounds placeholder instead. */
754 if (type->bounds ()->high.kind () == PROP_LOCEXPR
755 || type->bounds ()->high.kind () == PROP_LOCLIST)
756 gdb_printf (stream, "variable length");
757 else if (get_array_bounds (type, &low_bound, &high_bound))
758 gdb_printf (stream, "%s",
759 plongest (high_bound - low_bound + 1));
760 gdb_printf (stream, (is_vector ? ")))" : "]"));
761
762 c_type_print_varspec_suffix (type->target_type (), stream,
763 show, 0, 0, language, flags);
764 }
765 break;
766
767 case TYPE_CODE_MEMBERPTR:
768 c_type_print_varspec_suffix (type->target_type (), stream,
769 show, 0, 0, language, flags);
770 break;
771
772 case TYPE_CODE_METHODPTR:
773 gdb_printf (stream, ")");
774 c_type_print_varspec_suffix (type->target_type (), stream,
775 show, 0, 0, language, flags);
776 break;
777
778 case TYPE_CODE_PTR:
779 case TYPE_CODE_REF:
780 case TYPE_CODE_RVALUE_REF:
781 c_type_print_varspec_suffix (type->target_type (), stream,
782 show, 1, 0, language, flags);
783 break;
784
785 case TYPE_CODE_METHOD:
786 case TYPE_CODE_FUNC:
787 if (passed_a_ptr)
788 gdb_printf (stream, ")");
789 if (!demangled_args)
790 c_type_print_args (type, stream, 0, language, flags);
791 c_type_print_varspec_suffix (type->target_type (), stream,
792 show, passed_a_ptr, 0, language, flags);
793 break;
794
795 case TYPE_CODE_TYPEDEF:
796 c_type_print_varspec_suffix (type->target_type (), stream,
797 show, passed_a_ptr, 0, language, flags);
798 break;
799 }
800 }
801
802 /* A helper for c_type_print_base that displays template
803 parameters and their bindings, if needed.
804
805 TABLE is the local bindings table to use. If NULL, no printing is
806 done. Note that, at this point, TABLE won't have any useful
807 information in it -- but it is also used as a flag to
808 print_name_maybe_canonical to activate searching the global typedef
809 table.
810
811 TYPE is the type whose template arguments are being displayed.
812
813 STREAM is the stream on which to print. */
814
815 static void
816 c_type_print_template_args (const struct type_print_options *flags,
817 struct type *type, struct ui_file *stream,
818 enum language language)
819 {
820 int first = 1, i;
821
822 if (flags->raw)
823 return;
824
825 for (i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
826 {
827 struct symbol *sym = TYPE_TEMPLATE_ARGUMENT (type, i);
828
829 if (sym->aclass () != LOC_TYPEDEF)
830 continue;
831
832 if (first)
833 {
834 stream->wrap_here (4);
835 gdb_printf (stream, _("[with %s = "), sym->linkage_name ());
836 first = 0;
837 }
838 else
839 {
840 gdb_puts (", ", stream);
841 stream->wrap_here (9);
842 gdb_printf (stream, "%s = ", sym->linkage_name ());
843 }
844
845 c_print_type (sym->type (), "", stream, -1, 0, language, flags);
846 }
847
848 if (!first)
849 gdb_puts (_("] "), stream);
850 }
851
852 /* Use 'print_spaces', but take into consideration the
853 type_print_options FLAGS in order to determine how many whitespaces
854 will be printed. */
855
856 static void
857 print_spaces_filtered_with_print_options
858 (int level, struct ui_file *stream, const struct type_print_options *flags)
859 {
860 if (!flags->print_offsets)
861 print_spaces (level, stream);
862 else
863 print_spaces (level + print_offset_data::indentation, stream);
864 }
865
866 /* Output an access specifier to STREAM, if needed. LAST_ACCESS is the
867 last access specifier output (typically returned by this function). */
868
869 static enum access_specifier
870 output_access_specifier (struct ui_file *stream,
871 enum access_specifier last_access,
872 int level, bool is_protected, bool is_private,
873 const struct type_print_options *flags)
874 {
875 if (is_protected)
876 {
877 if (last_access != s_protected)
878 {
879 last_access = s_protected;
880 print_spaces_filtered_with_print_options (level + 2, stream, flags);
881 gdb_printf (stream, "protected:\n");
882 }
883 }
884 else if (is_private)
885 {
886 if (last_access != s_private)
887 {
888 last_access = s_private;
889 print_spaces_filtered_with_print_options (level + 2, stream, flags);
890 gdb_printf (stream, "private:\n");
891 }
892 }
893 else
894 {
895 if (last_access != s_public)
896 {
897 last_access = s_public;
898 print_spaces_filtered_with_print_options (level + 2, stream, flags);
899 gdb_printf (stream, "public:\n");
900 }
901 }
902
903 return last_access;
904 }
905
906 /* Return true if an access label (i.e., "public:", "private:",
907 "protected:") needs to be printed for TYPE. */
908
909 static bool
910 need_access_label_p (struct type *type)
911 {
912 if (type->is_declared_class ())
913 {
914 QUIT;
915 for (int i = TYPE_N_BASECLASSES (type); i < type->num_fields (); i++)
916 if (!TYPE_FIELD_PRIVATE (type, i))
917 return true;
918 QUIT;
919 for (int j = 0; j < TYPE_NFN_FIELDS (type); j++)
920 for (int i = 0; i < TYPE_FN_FIELDLIST_LENGTH (type, j); i++)
921 if (!TYPE_FN_FIELD_PRIVATE (TYPE_FN_FIELDLIST1 (type,
922 j), i))
923 return true;
924 QUIT;
925 for (int i = 0; i < TYPE_TYPEDEF_FIELD_COUNT (type); ++i)
926 if (!TYPE_TYPEDEF_FIELD_PRIVATE (type, i))
927 return true;
928 }
929 else
930 {
931 QUIT;
932 for (int i = TYPE_N_BASECLASSES (type); i < type->num_fields (); i++)
933 if (TYPE_FIELD_PRIVATE (type, i) || TYPE_FIELD_PROTECTED (type, i))
934 return true;
935 QUIT;
936 for (int j = 0; j < TYPE_NFN_FIELDS (type); j++)
937 {
938 QUIT;
939 for (int i = 0; i < TYPE_FN_FIELDLIST_LENGTH (type, j); i++)
940 if (TYPE_FN_FIELD_PROTECTED (TYPE_FN_FIELDLIST1 (type,
941 j), i)
942 || TYPE_FN_FIELD_PRIVATE (TYPE_FN_FIELDLIST1 (type,
943 j),
944 i))
945 return true;
946 }
947 QUIT;
948 for (int i = 0; i < TYPE_TYPEDEF_FIELD_COUNT (type); ++i)
949 if (TYPE_TYPEDEF_FIELD_PROTECTED (type, i)
950 || TYPE_TYPEDEF_FIELD_PRIVATE (type, i))
951 return true;
952 }
953
954 return false;
955 }
956
957 /* Helper function that temporarily disables FLAGS->PRINT_OFFSETS,
958 calls 'c_print_type_1', and then reenables FLAGS->PRINT_OFFSETS if
959 applicable. */
960
961 static void
962 c_print_type_no_offsets (struct type *type,
963 const char *varstring,
964 struct ui_file *stream,
965 int show, int level,
966 enum language language,
967 struct type_print_options *flags,
968 struct print_offset_data *podata)
969 {
970 unsigned int old_po = flags->print_offsets;
971
972 /* Temporarily disable print_offsets, because it would mess with
973 indentation. */
974 flags->print_offsets = 0;
975 c_print_type_1 (type, varstring, stream, show, level, language, flags,
976 podata);
977 flags->print_offsets = old_po;
978 }
979
980 /* Helper for 'c_type_print_base' that handles structs and unions.
981 For a description of the arguments, see 'c_type_print_base'. */
982
983 static void
984 c_type_print_base_struct_union (struct type *type, struct ui_file *stream,
985 int show, int level,
986 enum language language,
987 const struct type_print_options *flags,
988 struct print_offset_data *podata)
989 {
990 struct type_print_options local_flags = *flags;
991 local_flags.local_typedefs = NULL;
992
993 std::unique_ptr<typedef_hash_table> hash_holder;
994 if (!flags->raw)
995 {
996 if (flags->local_typedefs)
997 local_flags.local_typedefs
998 = new typedef_hash_table (*flags->local_typedefs);
999 else
1000 local_flags.local_typedefs = new typedef_hash_table ();
1001
1002 hash_holder.reset (local_flags.local_typedefs);
1003 }
1004
1005 c_type_print_modifier (type, stream, 0, 1, language);
1006 if (type->code () == TYPE_CODE_UNION)
1007 gdb_printf (stream, "union ");
1008 else if (type->is_declared_class ())
1009 gdb_printf (stream, "class ");
1010 else
1011 gdb_printf (stream, "struct ");
1012
1013 /* Print the tag if it exists. The HP aCC compiler emits a
1014 spurious "{unnamed struct}"/"{unnamed union}"/"{unnamed
1015 enum}" tag for unnamed struct/union/enum's, which we don't
1016 want to print. */
1017 if (type->name () != NULL
1018 && !startswith (type->name (), "{unnamed"))
1019 {
1020 /* When printing the tag name, we are still effectively
1021 printing in the outer context, hence the use of FLAGS
1022 here. */
1023 print_name_maybe_canonical (type->name (), flags, stream);
1024 if (show > 0)
1025 gdb_puts (" ", stream);
1026 }
1027
1028 if (show < 0)
1029 {
1030 /* If we just printed a tag name, no need to print anything
1031 else. */
1032 if (type->name () == NULL)
1033 gdb_printf (stream, "{...}");
1034 }
1035 else if (show > 0 || type->name () == NULL)
1036 {
1037 struct type *basetype;
1038 int vptr_fieldno;
1039
1040 c_type_print_template_args (&local_flags, type, stream, language);
1041
1042 /* Add in template parameters when printing derivation info. */
1043 if (local_flags.local_typedefs != NULL)
1044 local_flags.local_typedefs->add_template_parameters (type);
1045 cp_type_print_derivation_info (stream, type, &local_flags);
1046
1047 /* This holds just the global typedefs and the template
1048 parameters. */
1049 struct type_print_options semi_local_flags = *flags;
1050 semi_local_flags.local_typedefs = NULL;
1051
1052 std::unique_ptr<typedef_hash_table> semi_holder;
1053 if (local_flags.local_typedefs != nullptr)
1054 {
1055 semi_local_flags.local_typedefs
1056 = new typedef_hash_table (*local_flags.local_typedefs);
1057 semi_holder.reset (semi_local_flags.local_typedefs);
1058
1059 /* Now add in the local typedefs. */
1060 local_flags.local_typedefs->recursively_update (type);
1061 }
1062
1063 gdb_printf (stream, "{\n");
1064
1065 if (type->num_fields () == 0 && TYPE_NFN_FIELDS (type) == 0
1066 && TYPE_TYPEDEF_FIELD_COUNT (type) == 0)
1067 {
1068 print_spaces_filtered_with_print_options (level + 4, stream, flags);
1069 if (type->is_stub ())
1070 gdb_printf (stream, _("%p[<incomplete type>%p]\n"),
1071 metadata_style.style ().ptr (), nullptr);
1072 else
1073 gdb_printf (stream, _("%p[<no data fields>%p]\n"),
1074 metadata_style.style ().ptr (), nullptr);
1075 }
1076
1077 /* Start off with no specific section type, so we can print
1078 one for the first field we find, and use that section type
1079 thereafter until we find another type. */
1080
1081 enum access_specifier section_type = s_none;
1082
1083 /* For a class, if all members are private, there's no need
1084 for a "private:" label; similarly, for a struct or union
1085 masquerading as a class, if all members are public, there's
1086 no need for a "public:" label. */
1087 bool need_access_label = need_access_label_p (type);
1088
1089 /* If there is a base class for this type,
1090 do not print the field that it occupies. */
1091
1092 int len = type->num_fields ();
1093 vptr_fieldno = get_vptr_fieldno (type, &basetype);
1094
1095 struct print_offset_data local_podata (flags);
1096
1097 for (int i = TYPE_N_BASECLASSES (type); i < len; i++)
1098 {
1099 QUIT;
1100
1101 /* If we have a virtual table pointer, omit it. Even if
1102 virtual table pointers are not specifically marked in
1103 the debug info, they should be artificial. */
1104 if ((i == vptr_fieldno && type == basetype)
1105 || type->field (i).is_artificial ())
1106 continue;
1107
1108 if (need_access_label)
1109 {
1110 section_type = output_access_specifier
1111 (stream, section_type, level,
1112 TYPE_FIELD_PROTECTED (type, i),
1113 TYPE_FIELD_PRIVATE (type, i), flags);
1114 }
1115
1116 bool is_static = type->field (i).is_static ();
1117
1118 if (flags->print_offsets)
1119 podata->update (type, i, stream);
1120
1121 print_spaces (level + 4, stream);
1122 if (is_static)
1123 gdb_printf (stream, "static ");
1124
1125 int newshow = show - 1;
1126
1127 if (!is_static && flags->print_offsets
1128 && (type->field (i).type ()->code () == TYPE_CODE_STRUCT
1129 || type->field (i).type ()->code () == TYPE_CODE_UNION))
1130 {
1131 /* If we're printing offsets and this field's type is
1132 either a struct or an union, then we're interested in
1133 expanding it. */
1134 ++newshow;
1135
1136 /* Make sure we carry our offset when we expand the
1137 struct/union. */
1138 local_podata.offset_bitpos
1139 = podata->offset_bitpos + type->field (i).loc_bitpos ();
1140 /* We're entering a struct/union. Right now,
1141 PODATA->END_BITPOS points right *after* the
1142 struct/union. However, when printing the first field
1143 of this inner struct/union, the end_bitpos we're
1144 expecting is exactly at the beginning of the
1145 struct/union. Therefore, we subtract the length of
1146 the whole struct/union. */
1147 local_podata.end_bitpos
1148 = podata->end_bitpos
1149 - type->field (i).type ()->length () * TARGET_CHAR_BIT;
1150 }
1151
1152 c_print_type_1 (type->field (i).type (),
1153 type->field (i).name (),
1154 stream, newshow, level + 4,
1155 language, &local_flags, &local_podata);
1156
1157 if (!is_static && type->field (i).is_packed ())
1158 {
1159 /* It is a bitfield. This code does not attempt
1160 to look at the bitpos and reconstruct filler,
1161 unnamed fields. This would lead to misleading
1162 results if the compiler does not put out fields
1163 for such things (I don't know what it does). */
1164 gdb_printf (stream, " : %d", type->field (i).bitsize ());
1165 }
1166 gdb_printf (stream, ";\n");
1167 }
1168
1169 /* If there are both fields and methods, put a blank line
1170 between them. Make sure to count only method that we
1171 will display; artificial methods will be hidden. */
1172 len = TYPE_NFN_FIELDS (type);
1173 if (!flags->print_methods)
1174 len = 0;
1175 int real_len = 0;
1176 for (int i = 0; i < len; i++)
1177 {
1178 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1179 int len2 = TYPE_FN_FIELDLIST_LENGTH (type, i);
1180 int j;
1181
1182 for (j = 0; j < len2; j++)
1183 if (!TYPE_FN_FIELD_ARTIFICIAL (f, j))
1184 real_len++;
1185 }
1186 if (real_len > 0 && section_type != s_none)
1187 gdb_printf (stream, "\n");
1188
1189 /* C++: print out the methods. */
1190 for (int i = 0; i < len; i++)
1191 {
1192 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1193 int j, len2 = TYPE_FN_FIELDLIST_LENGTH (type, i);
1194 const char *method_name = TYPE_FN_FIELDLIST_NAME (type, i);
1195 const char *name = type->name ();
1196 int is_constructor = name && strcmp (method_name,
1197 name) == 0;
1198
1199 for (j = 0; j < len2; j++)
1200 {
1201 const char *mangled_name;
1202 gdb::unique_xmalloc_ptr<char> mangled_name_holder;
1203 const char *physname = TYPE_FN_FIELD_PHYSNAME (f, j);
1204 int is_full_physname_constructor =
1205 TYPE_FN_FIELD_CONSTRUCTOR (f, j)
1206 || is_constructor_name (physname)
1207 || is_destructor_name (physname)
1208 || method_name[0] == '~';
1209
1210 /* Do not print out artificial methods. */
1211 if (TYPE_FN_FIELD_ARTIFICIAL (f, j))
1212 continue;
1213
1214 QUIT;
1215 section_type = output_access_specifier
1216 (stream, section_type, level,
1217 TYPE_FN_FIELD_PROTECTED (f, j),
1218 TYPE_FN_FIELD_PRIVATE (f, j), flags);
1219
1220 print_spaces_filtered_with_print_options (level + 4, stream,
1221 flags);
1222 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1223 gdb_printf (stream, "virtual ");
1224 else if (TYPE_FN_FIELD_STATIC_P (f, j))
1225 gdb_printf (stream, "static ");
1226 if (TYPE_FN_FIELD_TYPE (f, j)->target_type () == 0)
1227 {
1228 /* Keep GDB from crashing here. */
1229 gdb_printf (stream,
1230 _("%p[<undefined type>%p] %s;\n"),
1231 metadata_style.style ().ptr (), nullptr,
1232 TYPE_FN_FIELD_PHYSNAME (f, j));
1233 break;
1234 }
1235 else if (!is_constructor /* Constructors don't
1236 have declared
1237 types. */
1238 && !is_full_physname_constructor /* " " */
1239 && !is_type_conversion_operator (type, i, j))
1240 {
1241 c_print_type_no_offsets
1242 (TYPE_FN_FIELD_TYPE (f, j)->target_type (),
1243 "", stream, -1, 0, language, &local_flags, podata);
1244
1245 gdb_puts (" ", stream);
1246 }
1247 if (TYPE_FN_FIELD_STUB (f, j))
1248 {
1249 /* Build something we can demangle. */
1250 mangled_name_holder.reset (gdb_mangle_name (type, i, j));
1251 mangled_name = mangled_name_holder.get ();
1252 }
1253 else
1254 mangled_name = TYPE_FN_FIELD_PHYSNAME (f, j);
1255
1256 gdb::unique_xmalloc_ptr<char> demangled_name
1257 = gdb_demangle (mangled_name,
1258 DMGL_ANSI | DMGL_PARAMS);
1259 if (demangled_name == NULL)
1260 {
1261 /* In some cases (for instance with the HP
1262 demangling), if a function has more than 10
1263 arguments, the demangling will fail.
1264 Let's try to reconstruct the function
1265 signature from the symbol information. */
1266 if (!TYPE_FN_FIELD_STUB (f, j))
1267 {
1268 int staticp = TYPE_FN_FIELD_STATIC_P (f, j);
1269 struct type *mtype = TYPE_FN_FIELD_TYPE (f, j);
1270
1271 cp_type_print_method_args (mtype,
1272 "",
1273 method_name,
1274 staticp,
1275 stream, language,
1276 &local_flags);
1277 }
1278 else
1279 fprintf_styled (stream, metadata_style.style (),
1280 _("<badly mangled name '%s'>"),
1281 mangled_name);
1282 }
1283 else
1284 {
1285 const char *p;
1286 const char *demangled_no_class
1287 = remove_qualifiers (demangled_name.get ());
1288
1289 /* Get rid of the `static' appended by the
1290 demangler. */
1291 p = strstr (demangled_no_class, " static");
1292 if (p != NULL)
1293 {
1294 int length = p - demangled_no_class;
1295 std::string demangled_no_static (demangled_no_class,
1296 length);
1297 gdb_puts (demangled_no_static.c_str (), stream);
1298 }
1299 else
1300 gdb_puts (demangled_no_class, stream);
1301 }
1302
1303 gdb_printf (stream, ";\n");
1304 }
1305 }
1306
1307 /* Print out nested types. */
1308 if (TYPE_NESTED_TYPES_COUNT (type) != 0
1309 && semi_local_flags.print_nested_type_limit != 0)
1310 {
1311 if (semi_local_flags.print_nested_type_limit > 0)
1312 --semi_local_flags.print_nested_type_limit;
1313
1314 if (type->num_fields () != 0 || TYPE_NFN_FIELDS (type) != 0)
1315 gdb_printf (stream, "\n");
1316
1317 for (int i = 0; i < TYPE_NESTED_TYPES_COUNT (type); ++i)
1318 {
1319 print_spaces_filtered_with_print_options (level + 4, stream,
1320 flags);
1321 c_print_type_no_offsets (TYPE_NESTED_TYPES_FIELD_TYPE (type, i),
1322 "", stream, show, level + 4,
1323 language, &semi_local_flags, podata);
1324 gdb_printf (stream, ";\n");
1325 }
1326 }
1327
1328 /* Print typedefs defined in this class. */
1329
1330 if (TYPE_TYPEDEF_FIELD_COUNT (type) != 0 && flags->print_typedefs)
1331 {
1332 if (type->num_fields () != 0 || TYPE_NFN_FIELDS (type) != 0
1333 || TYPE_NESTED_TYPES_COUNT (type) != 0)
1334 gdb_printf (stream, "\n");
1335
1336 for (int i = 0; i < TYPE_TYPEDEF_FIELD_COUNT (type); i++)
1337 {
1338 struct type *target = TYPE_TYPEDEF_FIELD_TYPE (type, i);
1339
1340 /* Dereference the typedef declaration itself. */
1341 gdb_assert (target->code () == TYPE_CODE_TYPEDEF);
1342 target = target->target_type ();
1343
1344 if (need_access_label)
1345 {
1346 section_type = output_access_specifier
1347 (stream, section_type, level,
1348 TYPE_TYPEDEF_FIELD_PROTECTED (type, i),
1349 TYPE_TYPEDEF_FIELD_PRIVATE (type, i), flags);
1350 }
1351 print_spaces_filtered_with_print_options (level + 4, stream,
1352 flags);
1353 gdb_printf (stream, "typedef ");
1354
1355 /* We want to print typedefs with substitutions
1356 from the template parameters or globally-known
1357 typedefs but not local typedefs. */
1358 c_print_type_no_offsets (target,
1359 TYPE_TYPEDEF_FIELD_NAME (type, i),
1360 stream, show - 1, level + 4,
1361 language, &semi_local_flags, podata);
1362 gdb_printf (stream, ";\n");
1363 }
1364 }
1365
1366 if (flags->print_offsets)
1367 {
1368 if (show > 0)
1369 podata->finish (type, level, stream);
1370
1371 print_spaces (print_offset_data::indentation, stream);
1372 if (level == 0)
1373 print_spaces (2, stream);
1374 }
1375
1376 gdb_printf (stream, "%*s}", level, "");
1377 }
1378 }
1379
1380 /* Print the name of the type (or the ultimate pointer target,
1381 function value or array element), or the description of a structure
1382 or union.
1383
1384 SHOW positive means print details about the type (e.g. enum
1385 values), and print structure elements passing SHOW - 1 for show.
1386
1387 SHOW negative means just print the type name or struct tag if there
1388 is one. If there is no name, print something sensible but concise
1389 like "struct {...}".
1390
1391 SHOW zero means just print the type name or struct tag if there is
1392 one. If there is no name, print something sensible but not as
1393 concise like "struct {int x; int y;}".
1394
1395 LEVEL is the number of spaces to indent by.
1396 We increase it for some recursive calls. */
1397
1398 static void
1399 c_type_print_base_1 (struct type *type, struct ui_file *stream,
1400 int show, int level,
1401 enum language language,
1402 const struct type_print_options *flags,
1403 struct print_offset_data *podata)
1404 {
1405 int i;
1406 int len;
1407
1408 QUIT;
1409
1410 if (type == NULL)
1411 {
1412 fputs_styled (_("<type unknown>"), metadata_style.style (), stream);
1413 return;
1414 }
1415
1416 /* When SHOW is zero or less, and there is a valid type name, then
1417 always just print the type name directly from the type. */
1418
1419 if (show <= 0
1420 && type->name () != NULL)
1421 {
1422 c_type_print_modifier (type, stream, 0, 1, language);
1423
1424 /* If we have "typedef struct foo {. . .} bar;" do we want to
1425 print it as "struct foo" or as "bar"? Pick the latter for
1426 C++, because C++ folk tend to expect things like "class5
1427 *foo" rather than "struct class5 *foo". We rather
1428 arbitrarily choose to make language_minimal work in a C-like
1429 way. */
1430 if (language == language_c || language == language_minimal)
1431 {
1432 if (type->code () == TYPE_CODE_UNION)
1433 gdb_printf (stream, "union ");
1434 else if (type->code () == TYPE_CODE_STRUCT)
1435 {
1436 if (type->is_declared_class ())
1437 gdb_printf (stream, "class ");
1438 else
1439 gdb_printf (stream, "struct ");
1440 }
1441 else if (type->code () == TYPE_CODE_ENUM)
1442 gdb_printf (stream, "enum ");
1443 }
1444
1445 print_name_maybe_canonical (type->name (), flags, stream);
1446 return;
1447 }
1448
1449 type = check_typedef (type);
1450
1451 switch (type->code ())
1452 {
1453 case TYPE_CODE_TYPEDEF:
1454 /* If we get here, the typedef doesn't have a name, and we
1455 couldn't resolve type::target_type. Not much we can do. */
1456 gdb_assert (type->name () == NULL);
1457 gdb_assert (type->target_type () == NULL);
1458 fprintf_styled (stream, metadata_style.style (),
1459 _("<unnamed typedef>"));
1460 break;
1461
1462 case TYPE_CODE_FUNC:
1463 case TYPE_CODE_METHOD:
1464 if (type->target_type () == NULL)
1465 type_print_unknown_return_type (stream);
1466 else
1467 c_type_print_base_1 (type->target_type (),
1468 stream, show, level, language, flags, podata);
1469 break;
1470 case TYPE_CODE_ARRAY:
1471 case TYPE_CODE_PTR:
1472 case TYPE_CODE_MEMBERPTR:
1473 case TYPE_CODE_REF:
1474 case TYPE_CODE_RVALUE_REF:
1475 case TYPE_CODE_METHODPTR:
1476 c_type_print_base_1 (type->target_type (),
1477 stream, show, level, language, flags, podata);
1478 break;
1479
1480 case TYPE_CODE_STRUCT:
1481 case TYPE_CODE_UNION:
1482 c_type_print_base_struct_union (type, stream, show, level,
1483 language, flags, podata);
1484 break;
1485
1486 case TYPE_CODE_ENUM:
1487 c_type_print_modifier (type, stream, 0, 1, language);
1488 gdb_printf (stream, "enum ");
1489 if (type->is_declared_class ())
1490 gdb_printf (stream, "class ");
1491 /* Print the tag name if it exists.
1492 The aCC compiler emits a spurious
1493 "{unnamed struct}"/"{unnamed union}"/"{unnamed enum}"
1494 tag for unnamed struct/union/enum's, which we don't
1495 want to print. */
1496 if (type->name () != NULL
1497 && !startswith (type->name (), "{unnamed"))
1498 {
1499 print_name_maybe_canonical (type->name (), flags, stream);
1500 if (show > 0)
1501 gdb_puts (" ", stream);
1502 }
1503
1504 stream->wrap_here (4);
1505 if (show < 0)
1506 {
1507 /* If we just printed a tag name, no need to print anything
1508 else. */
1509 if (type->name () == NULL)
1510 gdb_printf (stream, "{...}");
1511 }
1512 else if (show > 0 || type->name () == NULL)
1513 {
1514 LONGEST lastval = 0;
1515
1516 /* We can't handle this case perfectly, as DWARF does not
1517 tell us whether or not the underlying type was specified
1518 in the source (and other debug formats don't provide this
1519 at all). We choose to print the underlying type, if it
1520 has a name, when in C++ on the theory that it's better to
1521 print too much than too little; but conversely not to
1522 print something egregiously outside the current
1523 language's syntax. */
1524 if (language == language_cplus && type->target_type () != NULL)
1525 {
1526 struct type *underlying = check_typedef (type->target_type ());
1527
1528 if (underlying->name () != NULL)
1529 gdb_printf (stream, ": %s ", underlying->name ());
1530 }
1531
1532 gdb_printf (stream, "{");
1533 len = type->num_fields ();
1534 for (i = 0; i < len; i++)
1535 {
1536 QUIT;
1537 if (i)
1538 gdb_printf (stream, ", ");
1539 stream->wrap_here (4);
1540 fputs_styled (type->field (i).name (),
1541 variable_name_style.style (), stream);
1542 if (lastval != type->field (i).loc_enumval ())
1543 {
1544 gdb_printf (stream, " = %s",
1545 plongest (type->field (i).loc_enumval ()));
1546 lastval = type->field (i).loc_enumval ();
1547 }
1548 lastval++;
1549 }
1550 gdb_printf (stream, "}");
1551 }
1552 break;
1553
1554 case TYPE_CODE_FLAGS:
1555 {
1556 struct type_print_options local_flags = *flags;
1557
1558 local_flags.local_typedefs = NULL;
1559
1560 c_type_print_modifier (type, stream, 0, 1, language);
1561 gdb_printf (stream, "flag ");
1562 print_name_maybe_canonical (type->name (), flags, stream);
1563 if (show > 0)
1564 {
1565 gdb_puts (" ", stream);
1566 gdb_printf (stream, "{\n");
1567 if (type->num_fields () == 0)
1568 {
1569 if (type->is_stub ())
1570 gdb_printf (stream,
1571 _("%*s%p[<incomplete type>%p]\n"),
1572 level + 4, "",
1573 metadata_style.style ().ptr (), nullptr);
1574 else
1575 gdb_printf (stream,
1576 _("%*s%p[<no data fields>%p]\n"),
1577 level + 4, "",
1578 metadata_style.style ().ptr (), nullptr);
1579 }
1580 len = type->num_fields ();
1581 for (i = 0; i < len; i++)
1582 {
1583 QUIT;
1584 print_spaces (level + 4, stream);
1585 /* We pass "show" here and not "show - 1" to get enum types
1586 printed. There's no other way to see them. */
1587 c_print_type_1 (type->field (i).type (),
1588 type->field (i).name (),
1589 stream, show, level + 4,
1590 language, &local_flags, podata);
1591 gdb_printf (stream, " @%s",
1592 plongest (type->field (i).loc_bitpos ()));
1593 if (type->field (i).bitsize () > 1)
1594 {
1595 gdb_printf (stream, "-%s",
1596 plongest (type->field (i).loc_bitpos ()
1597 + type->field (i).bitsize ()
1598 - 1));
1599 }
1600 gdb_printf (stream, ";\n");
1601 }
1602 gdb_printf (stream, "%*s}", level, "");
1603 }
1604 }
1605 break;
1606
1607 case TYPE_CODE_VOID:
1608 gdb_printf (stream, "void");
1609 break;
1610
1611 case TYPE_CODE_UNDEF:
1612 gdb_printf (stream, _("struct <unknown>"));
1613 break;
1614
1615 case TYPE_CODE_ERROR:
1616 gdb_printf (stream, "%s", TYPE_ERROR_NAME (type));
1617 break;
1618
1619 case TYPE_CODE_RANGE:
1620 /* This should not occur. */
1621 fprintf_styled (stream, metadata_style.style (), _("<range type>"));
1622 break;
1623
1624 case TYPE_CODE_FIXED_POINT:
1625 print_type_fixed_point (type, stream);
1626 break;
1627
1628 case TYPE_CODE_NAMESPACE:
1629 gdb_puts ("namespace ", stream);
1630 gdb_puts (type->name (), stream);
1631 break;
1632
1633 default:
1634 /* Handle types not explicitly handled by the other cases, such
1635 as fundamental types. For these, just print whatever the
1636 type name is, as recorded in the type itself. If there is no
1637 type name, then complain. */
1638 if (type->name () != NULL)
1639 {
1640 c_type_print_modifier (type, stream, 0, 1, language);
1641 print_name_maybe_canonical (type->name (), flags, stream);
1642 }
1643 else
1644 {
1645 /* At least for dump_symtab, it is important that this not
1646 be an error (). */
1647 fprintf_styled (stream, metadata_style.style (),
1648 _("<invalid type code %d>"), type->code ());
1649 }
1650 break;
1651 }
1652 }
1653
1654 /* See c_type_print_base_1. */
1655
1656 void
1657 c_type_print_base (struct type *type, struct ui_file *stream,
1658 int show, int level,
1659 const struct type_print_options *flags)
1660 {
1661 struct print_offset_data podata (flags);
1662
1663 c_type_print_base_1 (type, stream, show, level,
1664 current_language->la_language, flags, &podata);
1665 }