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