Implement CLOCK_MONOTONIC using getnanouptime(), which in DragonFly is
[dragonfly.git] / contrib / gdb / gdb / c-typeprint.c
1 /* Support for printing C and C++ types for GDB, the GNU debugger.
2    Copyright 1986, 1988, 1989, 1991, 1993, 1994, 1995, 1996, 1998, 1999
3    Free 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 2 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, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #include "defs.h"
22 #include "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 "command.h"
31 #include "gdbcmd.h"
32 #include "language.h"
33 #include "demangle.h"
34 #include "c-lang.h"
35 #include "typeprint.h"
36
37 #include "gdb_string.h"
38 #include <errno.h>
39 #include <ctype.h>
40
41 /* Flag indicating target was compiled by HP compiler */
42 extern int hp_som_som_object_present;
43
44 static void
45 c_type_print_args PARAMS ((struct type *, GDB_FILE *));
46
47 static void
48 cp_type_print_derivation_info PARAMS ((GDB_FILE *, struct type *));
49
50 void
51 c_type_print_varspec_prefix PARAMS ((struct type *, GDB_FILE *, int, int));
52
53 static void
54 c_type_print_cv_qualifier PARAMS ((struct type *, GDB_FILE *, int, int));
55
56
57 \f
58 /* Print a description of a type in the format of a 
59    typedef for the current language.
60    NEW is the new name for a type TYPE. */
61
62 void
63 c_typedef_print (type, new, stream)
64    struct type *type;
65    struct symbol *new;
66    GDB_FILE *stream;
67 {
68   CHECK_TYPEDEF (type);
69    switch (current_language->la_language)
70    {
71 #ifdef _LANG_c
72    case language_c:
73    case language_cplus:
74       fprintf_filtered(stream, "typedef ");
75       type_print(type,"",stream,0);
76       if(TYPE_NAME ((SYMBOL_TYPE (new))) == 0
77          || !STREQ (TYPE_NAME ((SYMBOL_TYPE (new))), SYMBOL_NAME (new)))
78         fprintf_filtered(stream,  " %s", SYMBOL_SOURCE_NAME(new));
79       break;
80 #endif
81 #ifdef _LANG_m2
82    case language_m2:
83       fprintf_filtered(stream, "TYPE ");
84       if(!TYPE_NAME(SYMBOL_TYPE(new)) ||
85          !STREQ (TYPE_NAME(SYMBOL_TYPE(new)), SYMBOL_NAME(new)))
86         fprintf_filtered(stream, "%s = ", SYMBOL_SOURCE_NAME(new));
87       else
88          fprintf_filtered(stream, "<builtin> = ");
89       type_print(type,"",stream,0);
90       break;
91 #endif
92 #ifdef _LANG_chill
93    case language_chill:
94       fprintf_filtered(stream, "SYNMODE ");
95       if(!TYPE_NAME(SYMBOL_TYPE(new)) ||
96          !STREQ (TYPE_NAME(SYMBOL_TYPE(new)), SYMBOL_NAME(new)))
97         fprintf_filtered(stream, "%s = ", SYMBOL_SOURCE_NAME(new));
98       else
99          fprintf_filtered(stream, "<builtin> = ");
100       type_print(type,"",stream,0);
101       break;
102 #endif
103    default:
104       error("Language not supported.");
105    }
106    fprintf_filtered(stream, ";\n");
107 }
108
109
110 /* LEVEL is the depth to indent lines by.  */
111
112 void
113 c_print_type (type, varstring, stream, show, level)
114      struct type *type;
115      char *varstring;
116      GDB_FILE *stream;
117      int show;
118      int level;
119 {
120   register enum type_code code;
121   int demangled_args;
122
123   if (show > 0)
124     CHECK_TYPEDEF (type);
125
126   c_type_print_base (type, stream, show, level);
127   code = TYPE_CODE (type);
128   if ((varstring != NULL && *varstring != '\0')
129       ||
130       /* Need a space if going to print stars or brackets;
131          but not if we will print just a type name.  */
132       ((show > 0 || TYPE_NAME (type) == 0)
133        &&
134        (code == TYPE_CODE_PTR || code == TYPE_CODE_FUNC
135         || code == TYPE_CODE_METHOD
136         || code == TYPE_CODE_ARRAY
137         || code == TYPE_CODE_MEMBER
138         || code == TYPE_CODE_REF)))
139     fputs_filtered (" ", stream);
140   c_type_print_varspec_prefix (type, stream, show, 0);
141
142   if (varstring != NULL)
143     {
144       fputs_filtered (varstring, stream);
145
146       /* For demangled function names, we have the arglist as part of the name,
147          so don't print an additional pair of ()'s */
148
149       demangled_args = strchr(varstring, '(') != NULL;
150       c_type_print_varspec_suffix (type, stream, show, 0, demangled_args);
151     }
152 }
153   
154 /* If TYPE is a derived type, then print out derivation information.
155    Print only the actual base classes of this type, not the base classes
156    of the base classes.  I.E.  for the derivation hierarchy:
157
158         class A { int a; };
159         class B : public A {int b; };
160         class C : public B {int c; };
161
162    Print the type of class C as:
163
164         class C : public B {
165                 int c;
166         }
167
168    Not as the following (like gdb used to), which is not legal C++ syntax for
169    derived types and may be confused with the multiple inheritance form:
170
171         class C : public B : public A {
172                 int c;
173         }
174
175    In general, gdb should try to print the types as closely as possible to
176    the form that they appear in the source code. 
177    Note that in case of protected derivation gcc will not say 'protected' 
178    but 'private'. The HP's aCC compiler emits specific information for 
179    derivation via protected inheritance, so gdb can print it out */
180
181 static void
182 cp_type_print_derivation_info (stream, type)
183      GDB_FILE *stream;
184      struct type *type;
185 {
186   char *name;
187   int i;
188
189   for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
190     {
191       fputs_filtered (i == 0 ? ": " : ", ", stream);
192       fprintf_filtered (stream, "%s%s ",
193                         BASETYPE_VIA_PUBLIC (type, i) ? "public" 
194                         : (TYPE_FIELD_PROTECTED (type, i) ? "protected" : "private"),
195                         BASETYPE_VIA_VIRTUAL(type, i) ? " virtual" : "");
196       name = type_name_no_tag (TYPE_BASECLASS (type, i));
197       fprintf_filtered (stream, "%s", name ? name : "(null)");
198     }
199   if (i > 0)
200     {
201       fputs_filtered (" ", stream);
202     }
203 }
204 /* Print the C++ method arguments ARGS to the file STREAM.  */
205  
206 void
207 cp_type_print_method_args (args, prefix, varstring, staticp, stream)
208      struct type **args;
209      char *prefix;
210      char *varstring;
211      int staticp;
212      GDB_FILE *stream;
213 {
214   int i;
215  
216   fprintf_symbol_filtered (stream, prefix, language_cplus, DMGL_ANSI);
217   fprintf_symbol_filtered (stream, varstring, language_cplus, DMGL_ANSI);
218   fputs_filtered ("(", stream);
219   if (args && args[!staticp] && args[!staticp]->code != TYPE_CODE_VOID)
220     {
221       i = !staticp;             /* skip the class variable */
222       while (1)
223         {
224           type_print (args[i++], "", stream, 0);
225           if (!args[i])
226             {
227               fprintf_filtered (stream, " ...");
228               break;
229             }
230           else if (args[i]->code != TYPE_CODE_VOID)
231             {
232               fprintf_filtered (stream, ", ");
233             }
234           else break;
235         }
236     }
237   else if (current_language->la_language == language_cplus)
238     {
239       fprintf_filtered (stream, "void");
240     }
241  
242   fprintf_filtered (stream, ")");
243 }
244
245
246 /* Print any asterisks or open-parentheses needed before the
247    variable name (to describe its type).
248
249    On outermost call, pass 0 for PASSED_A_PTR.
250    On outermost call, SHOW > 0 means should ignore
251    any typename for TYPE and show its details.
252    SHOW is always zero on recursive calls.  */
253
254 void
255 c_type_print_varspec_prefix (type, stream, show, passed_a_ptr)
256      struct type *type;
257      GDB_FILE *stream;
258      int show;
259      int passed_a_ptr;
260 {
261   char *name;
262   if (type == 0)
263     return;
264
265   if (TYPE_NAME (type) && show <= 0)
266     return;
267
268   QUIT;
269
270   switch (TYPE_CODE (type))
271     {
272     case TYPE_CODE_PTR:
273       c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1);
274       fprintf_filtered (stream, "*");
275       c_type_print_cv_qualifier (type, stream, 1, 0);
276       break;
277
278     case TYPE_CODE_MEMBER:
279       if (passed_a_ptr)
280         fprintf_filtered (stream, "(");
281       c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
282       fprintf_filtered (stream, " ");
283       name = type_name_no_tag (TYPE_DOMAIN_TYPE (type));
284       if (name)
285         fputs_filtered (name, stream);
286       else
287         c_type_print_base (TYPE_DOMAIN_TYPE (type), stream, 0, passed_a_ptr);
288       fprintf_filtered (stream, "::");
289       break;
290
291     case TYPE_CODE_METHOD:
292       if (passed_a_ptr)
293         fprintf_filtered (stream, "(");
294       c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
295       if (passed_a_ptr)
296         {
297           fprintf_filtered (stream, " ");
298           c_type_print_base (TYPE_DOMAIN_TYPE (type), stream, 0, passed_a_ptr);
299           fprintf_filtered (stream, "::");
300         }
301       break;
302
303     case TYPE_CODE_REF:
304       c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1);
305       fprintf_filtered (stream, "&");
306       c_type_print_cv_qualifier (type, stream, 1, 0);
307       break;
308
309     case TYPE_CODE_FUNC:
310       c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
311       if (passed_a_ptr)
312         fprintf_filtered (stream, "(");
313       break;
314
315     case TYPE_CODE_ARRAY:
316       c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
317       if (passed_a_ptr)
318         fprintf_filtered (stream, "(");
319       break;
320
321     case TYPE_CODE_UNDEF:
322     case TYPE_CODE_STRUCT:
323     case TYPE_CODE_UNION:
324     case TYPE_CODE_ENUM:
325     case TYPE_CODE_INT:
326     case TYPE_CODE_FLT:
327     case TYPE_CODE_VOID:
328     case TYPE_CODE_ERROR:
329     case TYPE_CODE_CHAR:
330     case TYPE_CODE_BOOL:
331     case TYPE_CODE_SET:
332     case TYPE_CODE_RANGE:
333     case TYPE_CODE_STRING:
334     case TYPE_CODE_BITSTRING:
335     case TYPE_CODE_COMPLEX:
336     case TYPE_CODE_TYPEDEF:
337       /* These types need no prefix.  They are listed here so that
338          gcc -Wall will reveal any types that haven't been handled.  */
339       break;
340     }
341 }
342
343 /* Print out "const" and "volatile" attributes.
344    TYPE is a pointer to the type being printed out.
345    STREAM is the output destination.
346    NEED_SPACE = 1 indicates an initial white space is needed */
347
348 static void
349 c_type_print_cv_qualifier (type, stream, need_pre_space, need_post_space)
350   struct type *type;
351   GDB_FILE *stream;
352   int need_pre_space;
353   int need_post_space;
354 {
355   int flag = 0;
356   
357   if (TYPE_CONST (type))
358     {
359       if (need_pre_space)
360         fprintf_filtered (stream, " ");
361       fprintf_filtered (stream, "const");
362       flag = 1;
363     }
364   
365   if (TYPE_VOLATILE (type))
366     {
367       if (flag || need_pre_space)
368         fprintf_filtered (stream, " ");
369       fprintf_filtered (stream, "volatile");
370       flag = 1;
371     }
372
373   if (flag && need_post_space)
374     fprintf_filtered (stream, " ");
375 }
376
377
378
379
380 static void
381 c_type_print_args (type, stream)
382      struct type *type;
383      GDB_FILE *stream;
384 {
385   int i;
386   struct type **args;
387
388   fprintf_filtered (stream, "(");
389   args = TYPE_ARG_TYPES (type);
390   if (args != NULL)
391     {
392       if (args[1] == NULL)
393         {
394           fprintf_filtered (stream, "...");
395         }
396       else if ((args[1]->code == TYPE_CODE_VOID) &&
397                (current_language->la_language == language_cplus))
398         {
399           fprintf_filtered (stream, "void");
400         }
401       else
402         {
403           for (i = 1;
404                args[i] != NULL && args[i]->code != TYPE_CODE_VOID;
405                i++)
406             {
407               c_print_type (args[i], "", stream, -1, 0);
408               if (args[i+1] == NULL)
409                 {
410                   fprintf_filtered (stream, "...");
411                 }
412               else if (args[i+1]->code != TYPE_CODE_VOID)
413                 {
414                   fprintf_filtered (stream, ",");
415                   wrap_here ("    ");
416                 }
417             }
418         }
419     }
420   else if (current_language->la_language == language_cplus)
421     {
422       fprintf_filtered (stream, "void");
423     }
424   
425   fprintf_filtered (stream, ")");
426 }
427
428 /* Print any array sizes, function arguments or close parentheses
429    needed after the variable name (to describe its type).
430    Args work like c_type_print_varspec_prefix.  */
431
432 void
433 c_type_print_varspec_suffix (type, stream, show, passed_a_ptr, demangled_args)
434      struct type *type;
435      GDB_FILE *stream;
436      int show;
437      int passed_a_ptr;
438      int demangled_args;
439 {
440   if (type == 0)
441     return;
442
443   if (TYPE_NAME (type) && show <= 0)
444     return;
445
446   QUIT;
447
448   switch (TYPE_CODE (type))
449     {
450     case TYPE_CODE_ARRAY:
451       if (passed_a_ptr)
452         fprintf_filtered (stream, ")");
453       
454       fprintf_filtered (stream, "[");
455       if (TYPE_LENGTH (type) >= 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0
456           && TYPE_ARRAY_UPPER_BOUND_TYPE(type) != BOUND_CANNOT_BE_DETERMINED)
457         fprintf_filtered (stream, "%d",
458                           (TYPE_LENGTH (type)
459                            / TYPE_LENGTH (TYPE_TARGET_TYPE (type))));
460       fprintf_filtered (stream, "]");
461       
462       c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0);
463       break;
464
465     case TYPE_CODE_MEMBER:
466       if (passed_a_ptr)
467         fprintf_filtered (stream, ")");
468       c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0);
469       break;
470
471     case TYPE_CODE_METHOD:
472       if (passed_a_ptr)
473         fprintf_filtered (stream, ")");
474       c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0);
475       if (passed_a_ptr)
476         {
477           c_type_print_args (type, stream);
478         }
479       break;
480
481     case TYPE_CODE_PTR:
482     case TYPE_CODE_REF:
483       c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 1, 0);
484       break;
485
486     case TYPE_CODE_FUNC:
487       if (passed_a_ptr)
488         fprintf_filtered (stream, ")");
489       if (!demangled_args)
490         { int i, len = TYPE_NFIELDS (type);
491           fprintf_filtered (stream, "(");
492           if ((len == 0) && (current_language->la_language == language_cplus))
493             {
494               fprintf_filtered (stream, "void");
495             }
496           else
497             for (i = 0; i < len; i++)
498               {
499                 if (i > 0)
500                   {
501                     fputs_filtered (", ", stream);
502                     wrap_here ("    ");
503                   }
504                 c_print_type (TYPE_FIELD_TYPE (type, i), "", stream, -1, 0);
505               }
506           fprintf_filtered (stream, ")");
507         }
508       c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
509                                    passed_a_ptr, 0);
510       break;
511
512     case TYPE_CODE_UNDEF:
513     case TYPE_CODE_STRUCT:
514     case TYPE_CODE_UNION:
515     case TYPE_CODE_ENUM:
516     case TYPE_CODE_INT:
517     case TYPE_CODE_FLT:
518     case TYPE_CODE_VOID:
519     case TYPE_CODE_ERROR:
520     case TYPE_CODE_CHAR:
521     case TYPE_CODE_BOOL:
522     case TYPE_CODE_SET:
523     case TYPE_CODE_RANGE:
524     case TYPE_CODE_STRING:
525     case TYPE_CODE_BITSTRING:
526     case TYPE_CODE_COMPLEX:
527     case TYPE_CODE_TYPEDEF:
528       /* These types do not need a suffix.  They are listed so that
529          gcc -Wall will report types that may not have been considered.  */
530       break;
531     }
532 }
533
534 /* Print the name of the type (or the ultimate pointer target,
535    function value or array element), or the description of a
536    structure or union.
537
538    SHOW positive means print details about the type (e.g. enum values),
539    and print structure elements passing SHOW - 1 for show.
540    SHOW negative means just print the type name or struct tag if there is one.
541    If there is no name, print something sensible but concise like
542    "struct {...}".
543    SHOW zero means just print the type name or struct tag if there is one.
544    If there is no name, print something sensible but not as concise like
545    "struct {int x; int y;}".
546
547    LEVEL is the number of spaces to indent by.
548    We increase it for some recursive calls.  */
549
550 void
551 c_type_print_base (type, stream, show, level)
552      struct type *type;
553      GDB_FILE *stream;
554      int show;
555      int level;
556 {
557   register int i;
558   register int len;
559   register int lastval;
560   char *mangled_name;
561   char *demangled_name;
562   char *demangled_no_static;
563   enum {s_none, s_public, s_private, s_protected} section_type;
564   int need_access_label = 0;
565   int j, len2;
566
567   QUIT;
568
569   wrap_here ("    ");
570   if (type == NULL)
571     {
572       fputs_filtered ("<type unknown>", stream);
573       return;
574     }
575
576   /* When SHOW is zero or less, and there is a valid type name, then always
577      just print the type name directly from the type.  */
578   /* If we have "typedef struct foo {. . .} bar;" do we want to print it
579      as "struct foo" or as "bar"?  Pick the latter, because C++ folk tend
580      to expect things like "class5 *foo" rather than "struct class5 *foo".  */
581
582   if (show <= 0
583       && TYPE_NAME (type) != NULL)
584     {
585       c_type_print_cv_qualifier (type, stream, 0, 1);
586       fputs_filtered (TYPE_NAME (type), stream);
587       return;
588     }
589
590   CHECK_TYPEDEF (type);
591           
592   switch (TYPE_CODE (type))
593     {
594     case TYPE_CODE_TYPEDEF:
595     case TYPE_CODE_ARRAY:
596     case TYPE_CODE_PTR:
597     case TYPE_CODE_MEMBER:
598     case TYPE_CODE_REF:
599     case TYPE_CODE_FUNC:
600     case TYPE_CODE_METHOD:
601       c_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
602       break;
603
604     case TYPE_CODE_STRUCT:
605       c_type_print_cv_qualifier (type, stream, 0, 1);
606       /* Note TYPE_CODE_STRUCT and TYPE_CODE_CLASS have the same value,
607        * so we use another means for distinguishing them.
608        */
609       if (HAVE_CPLUS_STRUCT (type)) {
610         switch (TYPE_DECLARED_TYPE(type)) {
611           case DECLARED_TYPE_CLASS:
612             fprintf_filtered (stream, "class ");
613             break;
614           case DECLARED_TYPE_UNION:
615             fprintf_filtered (stream, "union ");
616             break;
617           case DECLARED_TYPE_STRUCT:
618             fprintf_filtered (stream, "struct ");
619             break;
620           default:
621             /* If there is a CPLUS_STRUCT, assume class if not
622              * otherwise specified in the declared_type field.
623              */
624             fprintf_filtered (stream, "class ");
625             break;
626         } /* switch */
627       } else {
628         /* If not CPLUS_STRUCT, then assume it's a C struct */ 
629         fprintf_filtered (stream, "struct ");
630       }
631       goto struct_union;
632
633     case TYPE_CODE_UNION:
634       c_type_print_cv_qualifier (type, stream, 0, 1);
635       fprintf_filtered (stream, "union ");
636
637     struct_union:
638
639       /* Print the tag if it exists. 
640        * The HP aCC compiler emits
641        * a spurious "{unnamed struct}"/"{unnamed union}"/"{unnamed enum}"
642        * tag  for unnamed struct/union/enum's, which we don't
643        * want to print.
644        */
645       if (TYPE_TAG_NAME (type) != NULL &&
646           strncmp(TYPE_TAG_NAME(type), "{unnamed", 8))
647         {
648           fputs_filtered (TYPE_TAG_NAME (type), stream);
649           if (show > 0)
650             fputs_filtered (" ", stream);
651         }
652       wrap_here ("    ");
653       if (show < 0)
654         {
655           /* If we just printed a tag name, no need to print anything else.  */
656           if (TYPE_TAG_NAME (type) == NULL)
657             fprintf_filtered (stream, "{...}");
658         }
659       else if (show > 0 || TYPE_TAG_NAME (type) == NULL)
660         {
661           cp_type_print_derivation_info (stream, type);
662           
663           fprintf_filtered (stream, "{\n");
664           if ((TYPE_NFIELDS (type) == 0) && (TYPE_NFN_FIELDS (type) == 0))
665             {
666               if (TYPE_FLAGS (type) & TYPE_FLAG_STUB)
667                 fprintfi_filtered (level + 4, stream, "<incomplete type>\n");
668               else
669                 fprintfi_filtered (level + 4, stream, "<no data fields>\n");
670             }
671
672           /* Start off with no specific section type, so we can print
673              one for the first field we find, and use that section type
674              thereafter until we find another type. */
675
676           section_type = s_none;
677
678           /* For a class, if all members are private, there's no need
679              for a "private:" label; similarly, for a struct or union
680              masquerading as a class, if all members are public, there's
681              no need for a "public:" label. */ 
682
683           if ((TYPE_DECLARED_TYPE (type) == DECLARED_TYPE_CLASS) ||
684                (TYPE_DECLARED_TYPE (type) == DECLARED_TYPE_TEMPLATE))
685             {
686               QUIT;
687               len = TYPE_NFIELDS (type);
688               for (i = TYPE_N_BASECLASSES (type); i < len; i++)
689                 if (!TYPE_FIELD_PRIVATE (type, i))
690                   {
691                     need_access_label = 1;
692                     break;
693                   }
694               QUIT;
695               if (!need_access_label)
696                 {
697                   len2 = TYPE_NFN_FIELDS (type);
698                   for (j = 0; j < len2; j++)
699                     {
700                       len = TYPE_FN_FIELDLIST_LENGTH (type, j);
701                       for (i = 0; i < len; i++)
702                         if (!TYPE_FN_FIELD_PRIVATE (TYPE_FN_FIELDLIST1 (type, j), i))
703                           {
704                             need_access_label = 1;
705                             break;
706                           }
707                       if (need_access_label)
708                         break;
709                     }
710                 }
711             }
712           else if ((TYPE_DECLARED_TYPE (type) == DECLARED_TYPE_STRUCT) ||
713                    (TYPE_DECLARED_TYPE (type) == DECLARED_TYPE_UNION))
714             {
715               QUIT;
716               len = TYPE_NFIELDS (type);
717               for (i = TYPE_N_BASECLASSES (type); i < len; i++)
718                 if (TYPE_FIELD_PRIVATE (type, i) || TYPE_FIELD_PROTECTED (type, i))
719                   {
720                     need_access_label = 1;
721                     break;
722                   }
723               QUIT;
724               if (!need_access_label)
725                 {
726                   len2 = TYPE_NFN_FIELDS (type);
727                   for (j = 0; j < len2; j++)
728                     {
729                       QUIT;
730                       len = TYPE_FN_FIELDLIST_LENGTH (type, j);
731                       for (i = 0; i < len; i++)
732                         if (TYPE_FN_FIELD_PRIVATE (TYPE_FN_FIELDLIST1 (type, j), i) ||
733                             TYPE_FN_FIELD_PROTECTED (TYPE_FN_FIELDLIST1 (type, j), i))
734                           {
735                             need_access_label = 1;
736                             break;
737                           }
738                       if (need_access_label)
739                         break;
740                     }
741                 }
742             }
743
744           /* If there is a base class for this type,
745              do not print the field that it occupies.  */
746
747           len = TYPE_NFIELDS (type);
748           for (i = TYPE_N_BASECLASSES (type); i < len; i++)
749             {
750               QUIT;
751               /* Don't print out virtual function table.  */
752               /* HP ANSI C++ case */
753               if (TYPE_HAS_VTABLE(type) && (STREQN (TYPE_FIELD_NAME (type, i), "__vfp", 5)))
754                  continue;
755               /* Other compilers */
756               /* pai:: FIXME : check for has_vtable < 0 */
757               if (STREQN (TYPE_FIELD_NAME (type, i), "_vptr", 5)
758                   && is_cplus_marker ((TYPE_FIELD_NAME (type, i))[5]))
759                 continue;
760
761               /* If this is a C++ class we can print the various C++ section
762                  labels. */
763
764               if (HAVE_CPLUS_STRUCT (type) && need_access_label)
765                 {
766                   if (TYPE_FIELD_PROTECTED (type, i))
767                     {
768                       if (section_type != s_protected)
769                         {
770                           section_type = s_protected;
771                           fprintfi_filtered (level + 2, stream,
772                                              "protected:\n");
773                         }
774                     }
775                   else if (TYPE_FIELD_PRIVATE (type, i))
776                     {
777                       if (section_type != s_private)
778                         {
779                           section_type = s_private;
780                           fprintfi_filtered (level + 2, stream, "private:\n");
781                         }
782                     }
783                   else
784                     {
785                       if (section_type != s_public)
786                         {
787                           section_type = s_public;
788                           fprintfi_filtered (level + 2, stream, "public:\n");
789                         }
790                     }
791                 }
792
793               print_spaces_filtered (level + 4, stream);
794               if (TYPE_FIELD_STATIC (type, i))
795                 {
796                   fprintf_filtered (stream, "static ");
797                 }
798               c_print_type (TYPE_FIELD_TYPE (type, i),
799                             TYPE_FIELD_NAME (type, i),
800                             stream, show - 1, level + 4);
801               if (!TYPE_FIELD_STATIC (type, i)
802                   && TYPE_FIELD_PACKED (type, i))
803                 {
804                   /* It is a bitfield.  This code does not attempt
805                      to look at the bitpos and reconstruct filler,
806                      unnamed fields.  This would lead to misleading
807                      results if the compiler does not put out fields
808                      for such things (I don't know what it does).  */
809                   fprintf_filtered (stream, " : %d",
810                                     TYPE_FIELD_BITSIZE (type, i));
811                 }
812               fprintf_filtered (stream, ";\n");
813             }
814
815           /* If there are both fields and methods, put a space between. */
816           len = TYPE_NFN_FIELDS (type);
817           if (len && section_type != s_none)
818              fprintf_filtered (stream, "\n");
819
820           /* C++: print out the methods */
821           for (i = 0; i < len; i++)
822             {
823               struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
824               int j, len2 = TYPE_FN_FIELDLIST_LENGTH (type, i);
825               char *method_name = TYPE_FN_FIELDLIST_NAME (type, i);
826               char *name = type_name_no_tag (type);
827               int is_constructor = name && STREQ(method_name, name);
828               for (j = 0; j < len2; j++)
829                 {
830                   char *physname = TYPE_FN_FIELD_PHYSNAME (f, j);
831                   int is_full_physname_constructor = 
832                     ((physname[0] == '_' && physname[1] == '_'
833                       && strchr ("0123456789Qt", physname[2]))
834                      || STREQN (physname, "__ct__", 6)
835                      || DESTRUCTOR_PREFIX_P (physname)
836                      || STREQN (physname, "__dt__", 6));
837
838                   QUIT;
839                   if (TYPE_FN_FIELD_PROTECTED (f, j))
840                     {
841                       if (section_type != s_protected)
842                         {
843                           section_type = s_protected;
844                           fprintfi_filtered (level + 2, stream,
845                                              "protected:\n");
846                         }
847                     }
848                   else if (TYPE_FN_FIELD_PRIVATE (f, j))
849                     {
850                       if (section_type != s_private)
851                         {
852                           section_type = s_private;
853                           fprintfi_filtered (level + 2, stream, "private:\n");
854                         }
855                     }
856                   else
857                     {
858                       if (section_type != s_public)
859                         {
860                           section_type = s_public;
861                           fprintfi_filtered (level + 2, stream, "public:\n");
862                         }
863                     }
864
865                   print_spaces_filtered (level + 4, stream);
866                   if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
867                     fprintf_filtered (stream, "virtual ");
868                   else if (TYPE_FN_FIELD_STATIC_P (f, j))
869                     fprintf_filtered (stream, "static ");
870                   if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)) == 0)
871                     {
872                       /* Keep GDB from crashing here.  */
873                       fprintf_filtered (stream, "<undefined type> %s;\n",
874                                TYPE_FN_FIELD_PHYSNAME (f, j));
875                       break;
876                     }
877                   else if (!is_constructor &&                  /* constructors don't have declared types */
878                            !is_full_physname_constructor &&    /*    " "  */ 
879                            !strstr (method_name, "operator ")) /* Not a type conversion operator */
880                                                                /* (note space -- other operators don't have it) */ 
881                     {
882                       type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)),
883                                   "", stream, -1);
884                       fputs_filtered (" ", stream);
885                     }
886                   if (TYPE_FN_FIELD_STUB (f, j))
887                     /* Build something we can demangle.  */
888                     mangled_name = gdb_mangle_name (type, i, j);
889                   else
890                     mangled_name = TYPE_FN_FIELD_PHYSNAME (f, j);
891
892                   demangled_name =
893                     cplus_demangle (mangled_name,
894                                     DMGL_ANSI | DMGL_PARAMS);
895                   if (demangled_name == NULL)
896                     {
897                       /* in some cases (for instance with the HP demangling),
898                          if a function has more than 10 arguments, 
899                          the demangling will fail.
900                          Let's try to reconstruct the function signature from 
901                          the symbol information */
902                       if (!TYPE_FN_FIELD_STUB (f, j))
903                         cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f, j), "",
904                                                    method_name,
905                                                    TYPE_FN_FIELD_STATIC_P (f, j),
906                                                    stream);
907                       else
908                         fprintf_filtered (stream, "<badly mangled name '%s'>",
909                                           mangled_name);
910                     }
911                   else
912                     {
913                       char *p;
914                       char *demangled_no_class = demangled_name;
915                       
916                       while (p = strchr (demangled_no_class, ':'))
917                         {
918                           demangled_no_class = p;
919                           if (*++demangled_no_class == ':')
920                             ++demangled_no_class;
921                         }
922                       /* get rid of the static word appended by the demangler */
923                       p = strstr (demangled_no_class, " static");
924                       if (p != NULL)
925                         {
926                           int length = p - demangled_no_class;
927                           demangled_no_static = (char *) xmalloc (length + 1);
928                           strncpy (demangled_no_static, demangled_no_class, length);
929                           *(demangled_no_static + length) = '\0';
930                           fputs_filtered (demangled_no_static, stream);
931                           free (demangled_no_static);
932                         }
933                       else
934                         fputs_filtered (demangled_no_class, stream);
935                       free (demangled_name);
936                     }
937
938                   if (TYPE_FN_FIELD_STUB (f, j))
939                     free (mangled_name);
940
941                   fprintf_filtered (stream, ";\n");
942                 }
943             }
944
945       if (TYPE_LOCALTYPE_PTR (type) && show >= 0)
946         fprintfi_filtered (level, stream, " (Local at %s:%d)\n",
947                            TYPE_LOCALTYPE_FILE (type),
948                            TYPE_LOCALTYPE_LINE (type));
949
950           fprintfi_filtered (level, stream, "}");
951         }
952       if (TYPE_CODE(type) == TYPE_CODE_TEMPLATE)
953         goto go_back;
954       break;
955
956     case TYPE_CODE_ENUM:
957       c_type_print_cv_qualifier (type, stream, 0, 1);
958       /* HP C supports sized enums */
959       if (hp_som_som_object_present)
960         switch (TYPE_LENGTH (type))
961           {
962             case 1:
963               fputs_filtered ("char ", stream);
964               break;
965             case 2:
966               fputs_filtered ("short ", stream);
967               break;
968             default:
969               break;
970           }
971        fprintf_filtered (stream, "enum ");
972       /* Print the tag name if it exists.
973          The aCC compiler emits a spurious 
974          "{unnamed struct}"/"{unnamed union}"/"{unnamed enum}"
975          tag for unnamed struct/union/enum's, which we don't
976          want to print. */
977       if (TYPE_TAG_NAME (type) != NULL &&
978           strncmp(TYPE_TAG_NAME(type), "{unnamed", 8))
979         {
980           fputs_filtered (TYPE_TAG_NAME (type), stream);
981           if (show > 0)
982             fputs_filtered (" ", stream);
983         }
984
985       wrap_here ("    ");
986       if (show < 0)
987         {
988           /* If we just printed a tag name, no need to print anything else.  */
989           if (TYPE_TAG_NAME (type) == NULL)
990             fprintf_filtered (stream, "{...}");
991         }
992       else if (show > 0 || TYPE_TAG_NAME (type) == NULL)
993         {
994           fprintf_filtered (stream, "{");
995           len = TYPE_NFIELDS (type);
996           lastval = 0;
997           for (i = 0; i < len; i++)
998             {
999               QUIT;
1000               if (i) fprintf_filtered (stream, ", ");
1001               wrap_here ("    ");
1002               fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
1003               if (lastval != TYPE_FIELD_BITPOS (type, i))
1004                 {
1005                   fprintf_filtered (stream, " = %d", TYPE_FIELD_BITPOS (type, i));
1006                   lastval = TYPE_FIELD_BITPOS (type, i);
1007                 }
1008               lastval++;
1009             }
1010           fprintf_filtered (stream, "}");
1011         }
1012       break;
1013
1014     case TYPE_CODE_VOID:
1015       fprintf_filtered (stream, "void");
1016       break;
1017
1018     case TYPE_CODE_UNDEF:
1019       fprintf_filtered (stream, "struct <unknown>");
1020       break;
1021
1022     case TYPE_CODE_ERROR:
1023       fprintf_filtered (stream, "<unknown type>");
1024       break;
1025
1026     case TYPE_CODE_RANGE:
1027       /* This should not occur */
1028       fprintf_filtered (stream, "<range type>");
1029       break;
1030
1031     case TYPE_CODE_TEMPLATE:
1032       /* Called on "ptype t" where "t" is a template.
1033          Prints the template header (with args), e.g.:
1034            template <class T1, class T2> class "
1035          and then merges with the struct/union/class code to
1036          print the rest of the definition. */
1037       c_type_print_cv_qualifier (type, stream, 0, 1);
1038       fprintf_filtered (stream, "template <");
1039       for (i = 0; i < TYPE_NTEMPLATE_ARGS(type); i++) {
1040         struct template_arg templ_arg;
1041         templ_arg = TYPE_TEMPLATE_ARG(type, i);
1042         fprintf_filtered (stream, "class %s", templ_arg.name);
1043         if (i < TYPE_NTEMPLATE_ARGS(type)-1)
1044           fprintf_filtered (stream, ", ");
1045       }
1046       fprintf_filtered (stream, "> class ");
1047       /* Yuck, factor this out to a subroutine so we can call
1048          it and return to the point marked with the "goback:" label... - RT */
1049       goto struct_union; 
1050 go_back:
1051       if (TYPE_NINSTANTIATIONS(type) > 0) {
1052         fprintf_filtered (stream, "\ntemplate instantiations:\n");
1053         for (i = 0; i < TYPE_NINSTANTIATIONS(type); i++) {
1054           fprintf_filtered(stream, "  ");
1055           c_type_print_base (TYPE_INSTANTIATION(type, i), stream, 0, level);
1056           if (i < TYPE_NINSTANTIATIONS(type)-1) fprintf_filtered(stream, "\n");
1057         }
1058       }
1059       break;
1060        
1061     default:
1062       /* Handle types not explicitly handled by the other cases,
1063          such as fundamental types.  For these, just print whatever
1064          the type name is, as recorded in the type itself.  If there
1065          is no type name, then complain. */
1066       if (TYPE_NAME (type) != NULL)
1067         {
1068           c_type_print_cv_qualifier (type, stream, 0, 1);
1069           fputs_filtered (TYPE_NAME (type), stream);
1070         }
1071       else
1072         {
1073           /* At least for dump_symtab, it is important that this not be
1074              an error ().  */
1075           fprintf_filtered (stream, "<invalid type code %d>",
1076                             TYPE_CODE (type));
1077         }
1078       break;
1079     }
1080 }
1081
1082
1083
1084
1085
1086
1087
1088
1089