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