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