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