* c-typeprint.c (c_type_print_modifier): Add note that this prints
[external/binutils.git] / gdb / c-typeprint.c
1 /* Support for printing C and C++ types for GDB, the GNU debugger.
2    Copyright (C) 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1998,
3    1999, 2000, 2001, 2002, 2003, 2006, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 #include "defs.h"
22 #include "gdb_obstack.h"
23 #include "bfd.h"                /* Binary File Description */
24 #include "symtab.h"
25 #include "gdbtypes.h"
26 #include "expression.h"
27 #include "value.h"
28 #include "gdbcore.h"
29 #include "target.h"
30 #include "language.h"
31 #include "demangle.h"
32 #include "c-lang.h"
33 #include "typeprint.h"
34 #include "cp-abi.h"
35 #include "jv-lang.h"
36
37 #include "gdb_string.h"
38 #include <errno.h>
39
40 static void cp_type_print_method_args (struct type *mtype, char *prefix,
41                                        char *varstring, int staticp,
42                                        struct ui_file *stream);
43
44 static void cp_type_print_derivation_info (struct ui_file *, struct type *);
45
46 static void c_type_print_varspec_prefix (struct type *, struct ui_file *, int,
47                                          int, int);
48
49 /* Print "const", "volatile", or address space modifiers. */
50 static void c_type_print_modifier (struct type *, struct ui_file *,
51                                    int, int);
52 \f
53
54
55
56 /* LEVEL is the depth to indent lines by.  */
57
58 void
59 c_print_type (struct type *type, const char *varstring, struct ui_file *stream,
60               int show, int level)
61 {
62   enum type_code code;
63   int demangled_args;
64   int need_post_space;
65
66   if (show > 0)
67     CHECK_TYPEDEF (type);
68
69   c_type_print_base (type, stream, show, level);
70   code = TYPE_CODE (type);
71   if ((varstring != NULL && *varstring != '\0')
72   /* Need a space if going to print stars or brackets;
73      but not if we will print just a type name.  */
74       || ((show > 0 || TYPE_NAME (type) == 0)
75           && (code == TYPE_CODE_PTR || code == TYPE_CODE_FUNC
76               || code == TYPE_CODE_METHOD
77               || code == TYPE_CODE_ARRAY
78               || code == TYPE_CODE_MEMBERPTR
79               || code == TYPE_CODE_METHODPTR
80               || code == TYPE_CODE_REF)))
81     fputs_filtered (" ", stream);
82   need_post_space = (varstring != NULL && strcmp (varstring, "") != 0);
83   c_type_print_varspec_prefix (type, stream, show, 0, need_post_space);
84
85   if (varstring != NULL)
86     {
87       fputs_filtered (varstring, stream);
88
89       /* For demangled function names, we have the arglist as part of the name,
90          so don't print an additional pair of ()'s */
91
92       demangled_args = strchr (varstring, '(') != NULL;
93       c_type_print_varspec_suffix (type, stream, show, 0, demangled_args);
94     }
95 }
96
97 /* Print a typedef using C syntax.  TYPE is the underlying type.
98    NEW_SYMBOL is the symbol naming the type.  STREAM is the stream on
99    which to print.  */
100
101 void
102 c_print_typedef (struct type *type, struct symbol *new_symbol,
103                  struct ui_file *stream)
104 {
105   CHECK_TYPEDEF (type);
106   fprintf_filtered (stream, "typedef ");
107   type_print (type, "", stream, 0);
108   if (TYPE_NAME ((SYMBOL_TYPE (new_symbol))) == 0
109       || strcmp (TYPE_NAME ((SYMBOL_TYPE (new_symbol))),
110                  SYMBOL_LINKAGE_NAME (new_symbol)) != 0
111       || TYPE_CODE (SYMBOL_TYPE (new_symbol)) == TYPE_CODE_TYPEDEF)
112     fprintf_filtered (stream, " %s", SYMBOL_PRINT_NAME (new_symbol));
113   fprintf_filtered (stream, ";\n");
114 }
115
116 /* If TYPE is a derived type, then print out derivation information.
117    Print only the actual base classes of this type, not the base classes
118    of the base classes.  I.E.  for the derivation hierarchy:
119
120    class A { int a; };
121    class B : public A {int b; };
122    class C : public B {int c; };
123
124    Print the type of class C as:
125
126    class C : public B {
127    int c;
128    }
129
130    Not as the following (like gdb used to), which is not legal C++ syntax for
131    derived types and may be confused with the multiple inheritance form:
132
133    class C : public B : public A {
134    int c;
135    }
136
137    In general, gdb should try to print the types as closely as possible to
138    the form that they appear in the source code. 
139    Note that in case of protected derivation gcc will not say 'protected' 
140    but 'private'. The HP's aCC compiler emits specific information for 
141    derivation via protected inheritance, so gdb can print it out */
142
143 static void
144 cp_type_print_derivation_info (struct ui_file *stream, struct type *type)
145 {
146   char *name;
147   int i;
148
149   for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
150     {
151       fputs_filtered (i == 0 ? ": " : ", ", stream);
152       fprintf_filtered (stream, "%s%s ",
153                         BASETYPE_VIA_PUBLIC (type, i) ? "public"
154                : (TYPE_FIELD_PROTECTED (type, i) ? "protected" : "private"),
155                         BASETYPE_VIA_VIRTUAL (type, i) ? " virtual" : "");
156       name = type_name_no_tag (TYPE_BASECLASS (type, i));
157       fprintf_filtered (stream, "%s", name ? name : "(null)");
158     }
159   if (i > 0)
160     {
161       fputs_filtered (" ", stream);
162     }
163 }
164
165 /* Print the C++ method arguments ARGS to the file STREAM.  */
166
167 static void
168 cp_type_print_method_args (struct type *mtype, char *prefix, char *varstring,
169                            int staticp, struct ui_file *stream)
170 {
171   struct field *args = TYPE_FIELDS (mtype);
172   int nargs = TYPE_NFIELDS (mtype);
173   int varargs = TYPE_VARARGS (mtype);
174   int i;
175
176   fprintf_symbol_filtered (stream, prefix, language_cplus, DMGL_ANSI);
177   fprintf_symbol_filtered (stream, varstring, language_cplus, DMGL_ANSI);
178   fputs_filtered ("(", stream);
179
180   /* Skip the class variable.  */
181   i = staticp ? 0 : 1;
182   if (nargs > i)
183     {
184       while (i < nargs)
185         {
186           type_print (args[i++].type, "", stream, 0);
187
188           if (i == nargs && varargs)
189             fprintf_filtered (stream, ", ...");
190           else if (i < nargs)
191             fprintf_filtered (stream, ", ");
192         }
193     }
194   else if (varargs)
195     fprintf_filtered (stream, "...");
196   else if (current_language->la_language == language_cplus)
197     fprintf_filtered (stream, "void");
198
199   fprintf_filtered (stream, ")");
200
201   /* For non-static methods, read qualifiers from the type of
202      THIS.  */
203   if (!staticp)
204     {
205       struct type *domain;
206
207       gdb_assert (nargs > 0);
208       gdb_assert (TYPE_CODE (args[0].type) == TYPE_CODE_PTR);
209       domain = TYPE_TARGET_TYPE (args[0].type);
210
211       if (TYPE_CONST (domain))
212         fprintf_filtered (stream, " const");
213
214       if (TYPE_VOLATILE (domain))
215         fprintf_filtered (stream, " volatile");
216     }
217 }
218
219
220 /* Print any asterisks or open-parentheses needed before the
221    variable name (to describe its type).
222
223    On outermost call, pass 0 for PASSED_A_PTR.
224    On outermost call, SHOW > 0 means should ignore
225    any typename for TYPE and show its details.
226    SHOW is always zero on recursive calls.
227    
228    NEED_POST_SPACE is non-zero when a space will be be needed
229    between a trailing qualifier and a field, variable, or function
230    name.  */
231
232 void
233 c_type_print_varspec_prefix (struct type *type, struct ui_file *stream,
234                              int show, int passed_a_ptr, int need_post_space)
235 {
236   char *name;
237
238   if (type == 0)
239     return;
240
241   if (TYPE_NAME (type) && show <= 0)
242     return;
243
244   QUIT;
245
246   switch (TYPE_CODE (type))
247     {
248     case TYPE_CODE_PTR:
249       c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, show, 1, 1);
250       fprintf_filtered (stream, "*");
251       c_type_print_modifier (type, stream, 1, need_post_space);
252       break;
253
254     case TYPE_CODE_MEMBERPTR:
255       c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, show, 0, 0);
256       name = type_name_no_tag (TYPE_DOMAIN_TYPE (type));
257       if (name)
258         fputs_filtered (name, stream);
259       else
260         c_type_print_base (TYPE_DOMAIN_TYPE (type), stream, 0, passed_a_ptr);
261       fprintf_filtered (stream, "::*");
262       break;
263
264     case TYPE_CODE_METHODPTR:
265       c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, show, 0, 0);
266       fprintf_filtered (stream, "(");
267       name = type_name_no_tag (TYPE_DOMAIN_TYPE (type));
268       if (name)
269         fputs_filtered (name, stream);
270       else
271         c_type_print_base (TYPE_DOMAIN_TYPE (type), stream, 0, passed_a_ptr);
272       fprintf_filtered (stream, "::*");
273       break;
274
275     case TYPE_CODE_REF:
276       c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, show, 1, 0);
277       fprintf_filtered (stream, "&");
278       c_type_print_modifier (type, stream, 1, need_post_space);
279       break;
280
281     case TYPE_CODE_METHOD:
282     case TYPE_CODE_FUNC:
283       c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, show, 0, 0);
284       if (passed_a_ptr)
285         fprintf_filtered (stream, "(");
286       break;
287
288     case TYPE_CODE_ARRAY:
289       c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, show, 0, 0);
290       if (passed_a_ptr)
291         fprintf_filtered (stream, "(");
292       break;
293
294     case TYPE_CODE_TYPEDEF:
295       c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, show, 0, 0);
296       break;
297
298     case TYPE_CODE_UNDEF:
299     case TYPE_CODE_STRUCT:
300     case TYPE_CODE_UNION:
301     case TYPE_CODE_ENUM:
302     case TYPE_CODE_INT:
303     case TYPE_CODE_FLT:
304     case TYPE_CODE_VOID:
305     case TYPE_CODE_ERROR:
306     case TYPE_CODE_CHAR:
307     case TYPE_CODE_BOOL:
308     case TYPE_CODE_SET:
309     case TYPE_CODE_RANGE:
310     case TYPE_CODE_STRING:
311     case TYPE_CODE_BITSTRING:
312     case TYPE_CODE_COMPLEX:
313     case TYPE_CODE_NAMESPACE:
314     case TYPE_CODE_DECFLOAT:
315       /* These types need no prefix.  They are listed here so that
316          gcc -Wall will reveal any types that haven't been handled.  */
317       break;
318     default:
319       error (_("type not handled in c_type_print_varspec_prefix()"));
320       break;
321     }
322 }
323
324 /* Print out "const" and "volatile" attributes,
325    and address space id if present.
326    TYPE is a pointer to the type being printed out.
327    STREAM is the output destination.
328    NEED_SPACE = 1 indicates an initial white space is needed */
329
330 static void
331 c_type_print_modifier (struct type *type, struct ui_file *stream,
332                        int need_pre_space, int need_post_space)
333 {
334   int did_print_modifier = 0;
335   const char *address_space_id;
336
337   /* We don't print `const' qualifiers for references --- since all
338      operators affect the thing referenced, not the reference itself,
339      every reference is `const'.  */
340   if (TYPE_CONST (type)
341       && TYPE_CODE (type) != TYPE_CODE_REF)
342     {
343       if (need_pre_space)
344         fprintf_filtered (stream, " ");
345       fprintf_filtered (stream, "const");
346       did_print_modifier = 1;
347     }
348
349   if (TYPE_VOLATILE (type))
350     {
351       if (did_print_modifier || need_pre_space)
352         fprintf_filtered (stream, " ");
353       fprintf_filtered (stream, "volatile");
354       did_print_modifier = 1;
355     }
356
357   address_space_id = address_space_int_to_name (get_type_arch (type),
358                                                 TYPE_INSTANCE_FLAGS (type));
359   if (address_space_id)
360     {
361       if (did_print_modifier || need_pre_space)
362         fprintf_filtered (stream, " ");
363       fprintf_filtered (stream, "@%s", address_space_id);
364       did_print_modifier = 1;
365     }
366
367   if (did_print_modifier && need_post_space)
368     fprintf_filtered (stream, " ");
369 }
370
371
372 /* Print out the arguments of TYPE, which should have TYPE_CODE_METHOD
373    or TYPE_CODE_FUNC, to STREAM.  Artificial arguments, such as "this"
374    in non-static methods, are displayed if SHOW_ARTIFICIAL is
375    non-zero. LANGUAGE is the language in which TYPE was defined.  This is
376    a necessary evil since this code is used by the C, C++, and Java
377    backends. */
378
379 void
380 c_type_print_args (struct type *type, struct ui_file *stream,
381                    int show_artificial, enum language language)
382 {
383   int i, len;
384   struct field *args;
385   int printed_any = 0;
386
387   fprintf_filtered (stream, "(");
388   args = TYPE_FIELDS (type);
389   len = TYPE_NFIELDS (type);
390
391   for (i = 0; i < TYPE_NFIELDS (type); i++)
392     {
393       if (TYPE_FIELD_ARTIFICIAL (type, i) && !show_artificial)
394         continue;
395
396       if (printed_any)
397         {
398           fprintf_filtered (stream, ", ");
399           wrap_here ("    ");
400         }
401
402       if (language == language_java)
403         java_print_type (TYPE_FIELD_TYPE (type, i), "", stream, -1, 0);
404       else
405         c_print_type (TYPE_FIELD_TYPE (type, i), "", stream, -1, 0);
406       printed_any = 1;
407     }
408
409   if (printed_any && TYPE_VARARGS (type))
410     {
411       /* Print out a trailing ellipsis for varargs functions.  Ignore
412          TYPE_VARARGS if the function has no named arguments; that
413          represents unprototyped (K&R style) C functions.  */
414       if (printed_any && TYPE_VARARGS (type))
415         {
416           fprintf_filtered (stream, ", ");
417           wrap_here ("    ");
418           fprintf_filtered (stream, "...");
419         }
420     }
421   else if (!printed_any
422            && ((TYPE_PROTOTYPED (type) && language != language_java)
423                || language == language_cplus))
424     fprintf_filtered (stream, "void");
425
426   fprintf_filtered (stream, ")");
427 }
428
429
430 /* Return true iff the j'th overloading of the i'th method of TYPE
431    is a type conversion operator, like `operator int () { ... }'.
432    When listing a class's methods, we don't print the return type of
433    such operators.  */
434 static int
435 is_type_conversion_operator (struct type *type, int i, int j)
436 {
437   /* I think the whole idea of recognizing type conversion operators
438      by their name is pretty terrible.  But I don't think our present
439      data structure gives us any other way to tell.  If you know of
440      some other way, feel free to rewrite this function.  */
441   char *name = TYPE_FN_FIELDLIST_NAME (type, i);
442
443   if (strncmp (name, "operator", 8) != 0)
444     return 0;
445
446   name += 8;
447   if (! strchr (" \t\f\n\r", *name))
448     return 0;
449
450   while (strchr (" \t\f\n\r", *name))
451     name++;
452
453   if (!('a' <= *name && *name <= 'z')
454       && !('A' <= *name && *name <= 'Z')
455       && *name != '_')
456     /* If this doesn't look like the start of an identifier, then it
457        isn't a type conversion operator.  */
458     return 0;
459   else if (strncmp (name, "new", 3) == 0)
460     name += 3;
461   else if (strncmp (name, "delete", 6) == 0)
462     name += 6;
463   else
464     /* If it doesn't look like new or delete, it's a type conversion
465        operator.  */
466     return 1;
467
468   /* Is that really the end of the name?  */
469   if (('a' <= *name && *name <= 'z')
470       || ('A' <= *name && *name <= 'Z')
471       || ('0' <= *name && *name <= '9')
472       || *name == '_')
473     /* No, so the identifier following "operator" must be a type name,
474        and this is a type conversion operator.  */
475     return 1;
476
477   /* That was indeed the end of the name, so it was `operator new' or
478      `operator delete', neither of which are type conversion operators.  */
479   return 0;
480 }
481
482
483 /* Given a C++ qualified identifier QID, strip off the qualifiers,
484    yielding the unqualified name.  The return value is a pointer into
485    the original string.
486
487    It's a pity we don't have this information in some more structured
488    form.  Even the author of this function feels that writing little
489    parsers like this everywhere is stupid.  */
490 static char *
491 remove_qualifiers (char *qid)
492 {
493   int quoted = 0;               /* zero if we're not in quotes;
494                                    '"' if we're in a double-quoted string;
495                                    '\'' if we're in a single-quoted string.  */
496   int depth = 0;                /* number of unclosed parens we've seen */
497   char *parenstack = (char *) alloca (strlen (qid));
498   char *scan;
499   char *last = 0;               /* The character after the rightmost
500                                    `::' token we've seen so far.  */
501
502   for (scan = qid; *scan; scan++)
503     {
504       if (quoted)
505         {
506           if (*scan == quoted)
507             quoted = 0;
508           else if (*scan == '\\' && *(scan + 1))
509             scan++;
510         }
511       else if (scan[0] == ':' && scan[1] == ':')
512         {
513           /* If we're inside parenthesis (i.e., an argument list) or
514              angle brackets (i.e., a list of template arguments), then
515              we don't record the position of this :: token, since it's
516              not relevant to the top-level structure we're trying
517              to operate on.  */
518           if (depth == 0)
519             {
520               last = scan + 2;
521               scan++;
522             }
523         }
524       else if (*scan == '"' || *scan == '\'')
525         quoted = *scan;
526       else if (*scan == '(')
527         parenstack[depth++] = ')';
528       else if (*scan == '[')
529         parenstack[depth++] = ']';
530       /* We're going to treat <> as a pair of matching characters,
531          since we're more likely to see those in template id's than
532          real less-than characters.  What a crock.  */
533       else if (*scan == '<')
534         parenstack[depth++] = '>';
535       else if (*scan == ')' || *scan == ']' || *scan == '>')
536         {
537           if (depth > 0 && parenstack[depth - 1] == *scan)
538             depth--;
539           else
540             {
541               /* We're going to do a little error recovery here.  If we
542                  don't find a match for *scan on the paren stack, but
543                  there is something lower on the stack that does match, we
544                  pop the stack to that point.  */
545               int i;
546
547               for (i = depth - 1; i >= 0; i--)
548                 if (parenstack[i] == *scan)
549                   {
550                     depth = i;
551                     break;
552                   }
553             }
554         }
555     }
556
557   if (last)
558     return last;
559   else
560     /* We didn't find any :: tokens at the top level, so declare the
561        whole thing an unqualified identifier.  */
562     return qid;
563 }
564
565
566 /* Print any array sizes, function arguments or close parentheses
567    needed after the variable name (to describe its type).
568    Args work like c_type_print_varspec_prefix.  */
569
570 void
571 c_type_print_varspec_suffix (struct type *type, struct ui_file *stream,
572                              int show, int passed_a_ptr, int demangled_args)
573 {
574   if (type == 0)
575     return;
576
577   if (TYPE_NAME (type) && show <= 0)
578     return;
579
580   QUIT;
581
582   switch (TYPE_CODE (type))
583     {
584     case TYPE_CODE_ARRAY:
585       if (passed_a_ptr)
586         fprintf_filtered (stream, ")");
587
588       fprintf_filtered (stream, "[");
589       if (TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0
590         && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
591         fprintf_filtered (stream, "%d",
592                           (TYPE_LENGTH (type)
593                            / TYPE_LENGTH (TYPE_TARGET_TYPE (type))));
594       fprintf_filtered (stream, "]");
595
596       c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, show,
597                                    0, 0);
598       break;
599
600     case TYPE_CODE_MEMBERPTR:
601       c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, show,
602                                    0, 0);
603       break;
604
605     case TYPE_CODE_METHODPTR:
606       fprintf_filtered (stream, ")");
607       c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, show,
608                                    0, 0);
609       break;
610
611     case TYPE_CODE_PTR:
612     case TYPE_CODE_REF:
613       c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, show,
614                                    1, 0);
615       break;
616
617     case TYPE_CODE_METHOD:
618     case TYPE_CODE_FUNC:
619       if (passed_a_ptr)
620         fprintf_filtered (stream, ")");
621       if (!demangled_args)
622         c_type_print_args (type, stream, 1, current_language->la_language);
623       c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, show,
624                                    passed_a_ptr, 0);
625       break;
626
627     case TYPE_CODE_TYPEDEF:
628       c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, show,
629                                    passed_a_ptr, 0);
630       break;
631
632     case TYPE_CODE_UNDEF:
633     case TYPE_CODE_STRUCT:
634     case TYPE_CODE_UNION:
635     case TYPE_CODE_ENUM:
636     case TYPE_CODE_INT:
637     case TYPE_CODE_FLT:
638     case TYPE_CODE_VOID:
639     case TYPE_CODE_ERROR:
640     case TYPE_CODE_CHAR:
641     case TYPE_CODE_BOOL:
642     case TYPE_CODE_SET:
643     case TYPE_CODE_RANGE:
644     case TYPE_CODE_STRING:
645     case TYPE_CODE_BITSTRING:
646     case TYPE_CODE_COMPLEX:
647     case TYPE_CODE_NAMESPACE:
648     case TYPE_CODE_DECFLOAT:
649       /* These types do not need a suffix.  They are listed so that
650          gcc -Wall will report types that may not have been considered.  */
651       break;
652     default:
653       error (_("type not handled in c_type_print_varspec_suffix()"));
654       break;
655     }
656 }
657
658 /* Print the name of the type (or the ultimate pointer target,
659    function value or array element), or the description of a
660    structure or union.
661
662    SHOW positive means print details about the type (e.g. enum values),
663    and print structure elements passing SHOW - 1 for show.
664    SHOW negative means just print the type name or struct tag if there is one.
665    If there is no name, print something sensible but concise like
666    "struct {...}".
667    SHOW zero means just print the type name or struct tag if there is one.
668    If there is no name, print something sensible but not as concise like
669    "struct {int x; int y;}".
670
671    LEVEL is the number of spaces to indent by.
672    We increase it for some recursive calls.  */
673
674 void
675 c_type_print_base (struct type *type, struct ui_file *stream, int show,
676                    int level)
677 {
678   int i;
679   int len, real_len;
680   int lastval;
681   char *mangled_name;
682   char *demangled_name;
683   char *demangled_no_static;
684   enum
685     {
686       s_none, s_public, s_private, s_protected
687     }
688   section_type;
689   int need_access_label = 0;
690   int j, len2;
691
692   QUIT;
693
694   wrap_here ("    ");
695   if (type == NULL)
696     {
697       fputs_filtered (_("<type unknown>"), stream);
698       return;
699     }
700
701   /* When SHOW is zero or less, and there is a valid type name, then always
702      just print the type name directly from the type.  */
703   /* If we have "typedef struct foo {. . .} bar;" do we want to print it
704      as "struct foo" or as "bar"?  Pick the latter, because C++ folk tend
705      to expect things like "class5 *foo" rather than "struct class5 *foo".  */
706
707   if (show <= 0
708       && TYPE_NAME (type) != NULL)
709     {
710       c_type_print_modifier (type, stream, 0, 1);
711       fputs_filtered (TYPE_NAME (type), stream);
712       return;
713     }
714
715   CHECK_TYPEDEF (type);
716
717   switch (TYPE_CODE (type))
718     {
719     case TYPE_CODE_TYPEDEF:
720     case TYPE_CODE_ARRAY:
721     case TYPE_CODE_PTR:
722     case TYPE_CODE_MEMBERPTR:
723     case TYPE_CODE_REF:
724     case TYPE_CODE_FUNC:
725     case TYPE_CODE_METHOD:
726     case TYPE_CODE_METHODPTR:
727       c_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
728       break;
729
730     case TYPE_CODE_STRUCT:
731       c_type_print_modifier (type, stream, 0, 1);
732       if (TYPE_DECLARED_CLASS (type))
733         fprintf_filtered (stream, "class ");
734       else
735         fprintf_filtered (stream, "struct ");
736       goto struct_union;
737
738     case TYPE_CODE_UNION:
739       c_type_print_modifier (type, stream, 0, 1);
740       fprintf_filtered (stream, "union ");
741
742     struct_union:
743
744       /* Print the tag if it exists. 
745        * The HP aCC compiler emits
746        * a spurious "{unnamed struct}"/"{unnamed union}"/"{unnamed enum}"
747        * tag  for unnamed struct/union/enum's, which we don't
748        * want to print.
749        */
750       if (TYPE_TAG_NAME (type) != NULL
751           && strncmp (TYPE_TAG_NAME (type), "{unnamed", 8))
752         {
753           fputs_filtered (TYPE_TAG_NAME (type), stream);
754           if (show > 0)
755             fputs_filtered (" ", stream);
756         }
757       wrap_here ("    ");
758       if (show < 0)
759         {
760           /* If we just printed a tag name, no need to print anything else.  */
761           if (TYPE_TAG_NAME (type) == NULL)
762             fprintf_filtered (stream, "{...}");
763         }
764       else if (show > 0 || TYPE_TAG_NAME (type) == NULL)
765         {
766           struct type *basetype;
767           int vptr_fieldno;
768
769           cp_type_print_derivation_info (stream, type);
770
771           fprintf_filtered (stream, "{\n");
772           if (TYPE_NFIELDS (type) == 0 && TYPE_NFN_FIELDS (type) == 0
773               && TYPE_TYPEDEF_FIELD_COUNT (type) == 0)
774             {
775               if (TYPE_STUB (type))
776                 fprintfi_filtered (level + 4, stream, _("<incomplete type>\n"));
777               else
778                 fprintfi_filtered (level + 4, stream, _("<no data fields>\n"));
779             }
780
781           /* Start off with no specific section type, so we can print
782              one for the first field we find, and use that section type
783              thereafter until we find another type. */
784
785           section_type = s_none;
786
787           /* For a class, if all members are private, there's no need
788              for a "private:" label; similarly, for a struct or union
789              masquerading as a class, if all members are public, there's
790              no need for a "public:" label. */
791
792           if (TYPE_DECLARED_CLASS (type))
793             {
794               QUIT;
795               len = TYPE_NFIELDS (type);
796               for (i = TYPE_N_BASECLASSES (type); i < len; i++)
797                 if (!TYPE_FIELD_PRIVATE (type, i))
798                   {
799                     need_access_label = 1;
800                     break;
801                   }
802               QUIT;
803               if (!need_access_label)
804                 {
805                   len2 = TYPE_NFN_FIELDS (type);
806                   for (j = 0; j < len2; j++)
807                     {
808                       len = TYPE_FN_FIELDLIST_LENGTH (type, j);
809                       for (i = 0; i < len; i++)
810                         if (!TYPE_FN_FIELD_PRIVATE (TYPE_FN_FIELDLIST1 (type, j), i))
811                           {
812                             need_access_label = 1;
813                             break;
814                           }
815                       if (need_access_label)
816                         break;
817                     }
818                 }
819             }
820           else
821             {
822               QUIT;
823               len = TYPE_NFIELDS (type);
824               for (i = TYPE_N_BASECLASSES (type); i < len; i++)
825                 if (TYPE_FIELD_PRIVATE (type, i) || TYPE_FIELD_PROTECTED (type, i))
826                   {
827                     need_access_label = 1;
828                     break;
829                   }
830               QUIT;
831               if (!need_access_label)
832                 {
833                   len2 = TYPE_NFN_FIELDS (type);
834                   for (j = 0; j < len2; j++)
835                     {
836                       QUIT;
837                       len = TYPE_FN_FIELDLIST_LENGTH (type, j);
838                       for (i = 0; i < len; i++)
839                         if (TYPE_FN_FIELD_PRIVATE (TYPE_FN_FIELDLIST1 (type, j), i)
840                             || TYPE_FN_FIELD_PROTECTED (TYPE_FN_FIELDLIST1 (type, j), i))
841                           {
842                             need_access_label = 1;
843                             break;
844                           }
845                       if (need_access_label)
846                         break;
847                     }
848                 }
849             }
850
851           /* If there is a base class for this type,
852              do not print the field that it occupies.  */
853
854           len = TYPE_NFIELDS (type);
855           vptr_fieldno = get_vptr_fieldno (type, &basetype);
856           for (i = TYPE_N_BASECLASSES (type); i < len; i++)
857             {
858               QUIT;
859
860               /* If we have a virtual table pointer, omit it.  Even if
861                  virtual table pointers are not specifically marked in
862                  the debug info, they should be artificial.  */
863               if ((i == vptr_fieldno && type == basetype)
864                   || TYPE_FIELD_ARTIFICIAL (type, i))
865                 continue;
866
867               if (need_access_label)
868                 {
869                   if (TYPE_FIELD_PROTECTED (type, i))
870                     {
871                       if (section_type != s_protected)
872                         {
873                           section_type = s_protected;
874                           fprintfi_filtered (level + 2, stream,
875                                              "protected:\n");
876                         }
877                     }
878                   else if (TYPE_FIELD_PRIVATE (type, i))
879                     {
880                       if (section_type != s_private)
881                         {
882                           section_type = s_private;
883                           fprintfi_filtered (level + 2, stream, "private:\n");
884                         }
885                     }
886                   else
887                     {
888                       if (section_type != s_public)
889                         {
890                           section_type = s_public;
891                           fprintfi_filtered (level + 2, stream, "public:\n");
892                         }
893                     }
894                 }
895
896               print_spaces_filtered (level + 4, stream);
897               if (field_is_static (&TYPE_FIELD (type, i)))
898                 fprintf_filtered (stream, "static ");
899               c_print_type (TYPE_FIELD_TYPE (type, i),
900                             TYPE_FIELD_NAME (type, i),
901                             stream, show - 1, level + 4);
902               if (!field_is_static (&TYPE_FIELD (type, i))
903                   && TYPE_FIELD_PACKED (type, i))
904                 {
905                   /* It is a bitfield.  This code does not attempt
906                      to look at the bitpos and reconstruct filler,
907                      unnamed fields.  This would lead to misleading
908                      results if the compiler does not put out fields
909                      for such things (I don't know what it does).  */
910                   fprintf_filtered (stream, " : %d",
911                                     TYPE_FIELD_BITSIZE (type, i));
912                 }
913               fprintf_filtered (stream, ";\n");
914             }
915
916           /* If there are both fields and methods, put a blank line
917               between them.  Make sure to count only method that we will
918               display; artificial methods will be hidden.  */
919           len = TYPE_NFN_FIELDS (type);
920           real_len = 0;
921           for (i = 0; i < len; i++)
922             {
923               struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
924               int len2 = TYPE_FN_FIELDLIST_LENGTH (type, i);
925               int j;
926
927               for (j = 0; j < len2; j++)
928                 if (!TYPE_FN_FIELD_ARTIFICIAL (f, j))
929                   real_len++;
930             }
931           if (real_len > 0 && section_type != s_none)
932             fprintf_filtered (stream, "\n");
933
934           /* C++: print out the methods */
935           for (i = 0; i < len; i++)
936             {
937               struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
938               int j, len2 = TYPE_FN_FIELDLIST_LENGTH (type, i);
939               char *method_name = TYPE_FN_FIELDLIST_NAME (type, i);
940               char *name = type_name_no_tag (type);
941               int is_constructor = name && strcmp (method_name, name) == 0;
942
943               for (j = 0; j < len2; j++)
944                 {
945                   char *physname = TYPE_FN_FIELD_PHYSNAME (f, j);
946                   int is_full_physname_constructor =
947                     is_constructor_name (physname) 
948                     || is_destructor_name (physname)
949                     || method_name[0] == '~';
950
951                   /* Do not print out artificial methods.  */
952                   if (TYPE_FN_FIELD_ARTIFICIAL (f, j))
953                     continue;
954
955                   QUIT;
956                   if (TYPE_FN_FIELD_PROTECTED (f, j))
957                     {
958                       if (section_type != s_protected)
959                         {
960                           section_type = s_protected;
961                           fprintfi_filtered (level + 2, stream,
962                                              "protected:\n");
963                         }
964                     }
965                   else if (TYPE_FN_FIELD_PRIVATE (f, j))
966                     {
967                       if (section_type != s_private)
968                         {
969                           section_type = s_private;
970                           fprintfi_filtered (level + 2, stream, "private:\n");
971                         }
972                     }
973                   else
974                     {
975                       if (section_type != s_public)
976                         {
977                           section_type = s_public;
978                           fprintfi_filtered (level + 2, stream, "public:\n");
979                         }
980                     }
981
982                   print_spaces_filtered (level + 4, stream);
983                   if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
984                     fprintf_filtered (stream, "virtual ");
985                   else if (TYPE_FN_FIELD_STATIC_P (f, j))
986                     fprintf_filtered (stream, "static ");
987                   if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)) == 0)
988                     {
989                       /* Keep GDB from crashing here.  */
990                       fprintf_filtered (stream, _("<undefined type> %s;\n"),
991                                         TYPE_FN_FIELD_PHYSNAME (f, j));
992                       break;
993                     }
994                   else if (!is_constructor      /* constructors don't have declared types */
995                            && !is_full_physname_constructor     /*    " "  */
996                            && !is_type_conversion_operator (type, i, j))
997                     {
998                       type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)),
999                                   "", stream, -1);
1000                       fputs_filtered (" ", stream);
1001                     }
1002                   if (TYPE_FN_FIELD_STUB (f, j))
1003                     /* Build something we can demangle.  */
1004                     mangled_name = gdb_mangle_name (type, i, j);
1005                   else
1006                     mangled_name = TYPE_FN_FIELD_PHYSNAME (f, j);
1007
1008                   demangled_name =
1009                     cplus_demangle (mangled_name,
1010                                     DMGL_ANSI | DMGL_PARAMS);
1011                   if (demangled_name == NULL)
1012                     {
1013                       /* in some cases (for instance with the HP demangling),
1014                          if a function has more than 10 arguments, 
1015                          the demangling will fail.
1016                          Let's try to reconstruct the function signature from 
1017                          the symbol information */
1018                       if (!TYPE_FN_FIELD_STUB (f, j))
1019                         {
1020                           int staticp = TYPE_FN_FIELD_STATIC_P (f, j);
1021                           struct type *mtype = TYPE_FN_FIELD_TYPE (f, j);
1022
1023                           cp_type_print_method_args (mtype,
1024                                                      "",
1025                                                      method_name,
1026                                                      staticp,
1027                                                      stream);
1028                         }
1029                       else
1030                         fprintf_filtered (stream, _("<badly mangled name '%s'>"),
1031                                           mangled_name);
1032                     }
1033                   else
1034                     {
1035                       char *p;
1036                       char *demangled_no_class
1037                         = remove_qualifiers (demangled_name);
1038
1039                       /* get rid of the `static' appended by the demangler */
1040                       p = strstr (demangled_no_class, " static");
1041                       if (p != NULL)
1042                         {
1043                           int length = p - demangled_no_class;
1044
1045                           demangled_no_static = (char *) xmalloc (length + 1);
1046                           strncpy (demangled_no_static, demangled_no_class, length);
1047                           *(demangled_no_static + length) = '\0';
1048                           fputs_filtered (demangled_no_static, stream);
1049                           xfree (demangled_no_static);
1050                         }
1051                       else
1052                         fputs_filtered (demangled_no_class, stream);
1053                       xfree (demangled_name);
1054                     }
1055
1056                   if (TYPE_FN_FIELD_STUB (f, j))
1057                     xfree (mangled_name);
1058
1059                   fprintf_filtered (stream, ";\n");
1060                 }
1061             }
1062
1063           /* Print typedefs defined in this class.  */
1064
1065           if (TYPE_TYPEDEF_FIELD_COUNT (type) != 0)
1066             {
1067               if (TYPE_NFIELDS (type) != 0 || TYPE_NFN_FIELDS (type) != 0)
1068                 fprintf_filtered (stream, "\n");
1069
1070               for (i = 0; i < TYPE_TYPEDEF_FIELD_COUNT (type); i++)
1071                 {
1072                   struct type *target = TYPE_TYPEDEF_FIELD_TYPE (type, i);
1073
1074                   /* Dereference the typedef declaration itself.  */
1075                   gdb_assert (TYPE_CODE (target) == TYPE_CODE_TYPEDEF);
1076                   target = TYPE_TARGET_TYPE (target);
1077
1078                   print_spaces_filtered (level + 4, stream);
1079                   fprintf_filtered (stream, "typedef ");
1080                   c_print_type (target, TYPE_TYPEDEF_FIELD_NAME (type, i),
1081                                 stream, show - 1, level + 4);
1082                   fprintf_filtered (stream, ";\n");
1083                 }
1084             }
1085
1086           fprintfi_filtered (level, stream, "}");
1087
1088           if (TYPE_LOCALTYPE_PTR (type) && show >= 0)
1089             fprintfi_filtered (level, stream, _(" (Local at %s:%d)\n"),
1090                                TYPE_LOCALTYPE_FILE (type),
1091                                TYPE_LOCALTYPE_LINE (type));
1092         }
1093       break;
1094
1095     case TYPE_CODE_ENUM:
1096       c_type_print_modifier (type, stream, 0, 1);
1097       fprintf_filtered (stream, "enum ");
1098       /* Print the tag name if it exists.
1099          The aCC compiler emits a spurious 
1100          "{unnamed struct}"/"{unnamed union}"/"{unnamed enum}"
1101          tag for unnamed struct/union/enum's, which we don't
1102          want to print. */
1103       if (TYPE_TAG_NAME (type) != NULL
1104           && strncmp (TYPE_TAG_NAME (type), "{unnamed", 8))
1105         {
1106           fputs_filtered (TYPE_TAG_NAME (type), stream);
1107           if (show > 0)
1108             fputs_filtered (" ", stream);
1109         }
1110
1111       wrap_here ("    ");
1112       if (show < 0)
1113         {
1114           /* If we just printed a tag name, no need to print anything else.  */
1115           if (TYPE_TAG_NAME (type) == NULL)
1116             fprintf_filtered (stream, "{...}");
1117         }
1118       else if (show > 0 || TYPE_TAG_NAME (type) == NULL)
1119         {
1120           fprintf_filtered (stream, "{");
1121           len = TYPE_NFIELDS (type);
1122           lastval = 0;
1123           for (i = 0; i < len; i++)
1124             {
1125               QUIT;
1126               if (i)
1127                 fprintf_filtered (stream, ", ");
1128               wrap_here ("    ");
1129               fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
1130               if (lastval != TYPE_FIELD_BITPOS (type, i))
1131                 {
1132                   fprintf_filtered (stream, " = %d", TYPE_FIELD_BITPOS (type, i));
1133                   lastval = TYPE_FIELD_BITPOS (type, i);
1134                 }
1135               lastval++;
1136             }
1137           fprintf_filtered (stream, "}");
1138         }
1139       break;
1140
1141     case TYPE_CODE_VOID:
1142       fprintf_filtered (stream, "void");
1143       break;
1144
1145     case TYPE_CODE_UNDEF:
1146       fprintf_filtered (stream, _("struct <unknown>"));
1147       break;
1148
1149     case TYPE_CODE_ERROR:
1150       fprintf_filtered (stream, "%s", TYPE_ERROR_NAME (type));
1151       break;
1152
1153     case TYPE_CODE_RANGE:
1154       /* This should not occur */
1155       fprintf_filtered (stream, _("<range type>"));
1156       break;
1157
1158     case TYPE_CODE_NAMESPACE:
1159       fputs_filtered ("namespace ", stream);
1160       fputs_filtered (TYPE_TAG_NAME (type), stream);
1161       break;
1162
1163     default:
1164       /* Handle types not explicitly handled by the other cases,
1165          such as fundamental types.  For these, just print whatever
1166          the type name is, as recorded in the type itself.  If there
1167          is no type name, then complain. */
1168       if (TYPE_NAME (type) != NULL)
1169         {
1170           c_type_print_modifier (type, stream, 0, 1);
1171           fputs_filtered (TYPE_NAME (type), stream);
1172         }
1173       else
1174         {
1175           /* At least for dump_symtab, it is important that this not be
1176              an error ().  */
1177           fprintf_filtered (stream, _("<invalid type code %d>"),
1178                             TYPE_CODE (type));
1179         }
1180       break;
1181     }
1182 }