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