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