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