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