* debug.c (debug_make_undefined_tagged_type): Make sure we are
[external/binutils.git] / binutils / prdbg.c
1 /* prdbg.c -- Print out generic debugging information.
2    Copyright (C) 1995, 1996 Free Software Foundation, Inc.
3    Written by Ian Lance Taylor <ian@cygnus.com>.
4
5    This file is part of GNU Binutils.
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
20    02111-1307, USA.  */
21
22 /* This file prints out the generic debugging information, by
23    supplying a set of routines to debug_write.  */
24
25 #include <stdio.h>
26 #include <assert.h>
27
28 #include "bfd.h"
29 #include "bucomm.h"
30 #include "libiberty.h"
31 #include "debug.h"
32 #include "budbg.h"
33
34 /* This is the structure we use as a handle for these routines.  */
35
36 struct pr_handle
37 {
38   /* File to print information to.  */
39   FILE *f;
40   /* Current indentation level.  */
41   unsigned int indent;
42   /* Type stack.  */
43   struct pr_stack *stack;
44   /* Parameter number we are about to output.  */
45   int parameter;
46 };
47
48 /* The type stack.  */
49
50 struct pr_stack
51 {
52   /* Next element on the stack.  */
53   struct pr_stack *next;
54   /* This element.  */
55   char *type;
56   /* Current visibility of fields if this is a class.  */
57   enum debug_visibility visibility;
58   /* Name of the current method we are handling.  */
59   const char *method;
60 };
61
62 static void indent PARAMS ((struct pr_handle *));
63 static boolean push_type PARAMS ((struct pr_handle *, const char *));
64 static boolean prepend_type PARAMS ((struct pr_handle *, const char *));
65 static boolean append_type PARAMS ((struct pr_handle *, const char *));
66 static boolean substitute_type PARAMS ((struct pr_handle *, const char *));
67 static boolean indent_type PARAMS ((struct pr_handle *));
68 static char *pop_type PARAMS ((struct pr_handle *));
69 static void print_vma PARAMS ((bfd_vma, char *, boolean, boolean));
70 static boolean pr_fix_visibility
71   PARAMS ((struct pr_handle *, enum debug_visibility));
72
73 static boolean pr_start_compilation_unit PARAMS ((PTR, const char *));
74 static boolean pr_start_source PARAMS ((PTR, const char *));
75 static boolean pr_empty_type PARAMS ((PTR));
76 static boolean pr_void_type PARAMS ((PTR));
77 static boolean pr_int_type PARAMS ((PTR, unsigned int, boolean));
78 static boolean pr_float_type PARAMS ((PTR, unsigned int));
79 static boolean pr_complex_type PARAMS ((PTR, unsigned int));
80 static boolean pr_bool_type PARAMS ((PTR, unsigned int));
81 static boolean pr_enum_type
82   PARAMS ((PTR, const char *, const char **, bfd_signed_vma *));
83 static boolean pr_pointer_type PARAMS ((PTR));
84 static boolean pr_function_type PARAMS ((PTR));
85 static boolean pr_reference_type PARAMS ((PTR));
86 static boolean pr_range_type PARAMS ((PTR, bfd_signed_vma, bfd_signed_vma));
87 static boolean pr_array_type
88   PARAMS ((PTR, bfd_signed_vma, bfd_signed_vma, boolean));
89 static boolean pr_set_type PARAMS ((PTR, boolean));
90 static boolean pr_offset_type PARAMS ((PTR));
91 static boolean pr_method_type PARAMS ((PTR, boolean, int));
92 static boolean pr_const_type PARAMS ((PTR));
93 static boolean pr_volatile_type PARAMS ((PTR));
94 static boolean pr_start_struct_type
95   PARAMS ((PTR, const char *, boolean, unsigned int));
96 static boolean pr_struct_field
97   PARAMS ((PTR, const char *, bfd_vma, bfd_vma, enum debug_visibility));
98 static boolean pr_end_struct_type PARAMS ((PTR));
99 static boolean pr_start_class_type
100   PARAMS ((PTR, const char *, boolean, unsigned int, boolean, boolean));
101 static boolean pr_class_static_member
102   PARAMS ((PTR, const char *, const char *, enum debug_visibility));
103 static boolean pr_class_baseclass
104   PARAMS ((PTR, bfd_vma, boolean, enum debug_visibility));
105 static boolean pr_class_start_method PARAMS ((PTR, const char *));
106 static boolean pr_class_method_variant
107   PARAMS ((PTR, const char *, enum debug_visibility, boolean, boolean,
108            bfd_vma, boolean));
109 static boolean pr_class_static_method_variant
110   PARAMS ((PTR, const char *, enum debug_visibility, boolean, boolean));
111 static boolean pr_class_end_method PARAMS ((PTR));
112 static boolean pr_end_class_type PARAMS ((PTR));
113 static boolean pr_typedef_type PARAMS ((PTR, const char *));
114 static boolean pr_tag_type PARAMS ((PTR, const char *, enum debug_type_kind));
115 static boolean pr_typdef PARAMS ((PTR, const char *));
116 static boolean pr_tag PARAMS ((PTR, const char *));
117 static boolean pr_int_constant PARAMS ((PTR, const char *, bfd_vma));
118 static boolean pr_float_constant PARAMS ((PTR, const char *, double));
119 static boolean pr_typed_constant PARAMS ((PTR, const char *, bfd_vma));
120 static boolean pr_variable
121   PARAMS ((PTR, const char *, enum debug_var_kind, bfd_vma));
122 static boolean pr_start_function PARAMS ((PTR, const char *, boolean));
123 static boolean pr_function_parameter
124   PARAMS ((PTR, const char *, enum debug_parm_kind, bfd_vma));
125 static boolean pr_start_block PARAMS ((PTR, bfd_vma));
126 static boolean pr_end_block PARAMS ((PTR, bfd_vma));
127 static boolean pr_end_function PARAMS ((PTR));
128 static boolean pr_lineno PARAMS ((PTR, const char *, unsigned long, bfd_vma));
129
130 static const struct debug_write_fns pr_fns =
131 {
132   pr_start_compilation_unit,
133   pr_start_source,
134   pr_empty_type,
135   pr_void_type,
136   pr_int_type,
137   pr_float_type,
138   pr_complex_type,
139   pr_bool_type,
140   pr_enum_type,
141   pr_pointer_type,
142   pr_function_type,
143   pr_reference_type,
144   pr_range_type,
145   pr_array_type,
146   pr_set_type,
147   pr_offset_type,
148   pr_method_type,
149   pr_const_type,
150   pr_volatile_type,
151   pr_start_struct_type,
152   pr_struct_field,
153   pr_end_struct_type,
154   pr_start_class_type,
155   pr_class_static_member,
156   pr_class_baseclass,
157   pr_class_start_method,
158   pr_class_method_variant,
159   pr_class_static_method_variant,
160   pr_class_end_method,
161   pr_end_class_type,
162   pr_typedef_type,
163   pr_tag_type,
164   pr_typdef,
165   pr_tag,
166   pr_int_constant,
167   pr_float_constant,
168   pr_typed_constant,
169   pr_variable,
170   pr_start_function,
171   pr_function_parameter,
172   pr_start_block,
173   pr_end_block,
174   pr_end_function,
175   pr_lineno
176 };
177 \f
178 /* Print out the generic debugging information recorded in dhandle.  */
179
180 boolean
181 print_debugging_info (f, dhandle)
182      FILE *f;
183      PTR dhandle;
184 {
185   struct pr_handle info;
186
187   info.f = f;
188   info.indent = 0;
189   info.stack = NULL;
190   info.parameter = 0;
191
192   return debug_write (dhandle, &pr_fns, (PTR) &info);
193 }
194 \f
195 /* Indent to the current indentation level.  */
196
197 static void
198 indent (info)
199      struct pr_handle *info;
200 {
201   unsigned int i;
202
203   for (i = 0; i < info->indent; i++)
204     putc (' ', info->f);
205 }
206
207 /* Push a type on the type stack.  */
208
209 static boolean
210 push_type (info, type)
211      struct pr_handle *info;
212      const char *type;
213 {
214   struct pr_stack *n;
215
216   if (type == NULL)
217     return false;
218
219   n = (struct pr_stack *) xmalloc (sizeof *n);
220   memset (n, 0, sizeof *n);
221
222   n->type = xstrdup (type);
223   n->visibility = DEBUG_VISIBILITY_IGNORE;
224   n->method = NULL;
225   n->next = info->stack;
226   info->stack = n;
227
228   return true;
229 }
230
231 /* Prepend a string onto the type on the top of the type stack.  */
232
233 static boolean
234 prepend_type (info, s)
235      struct pr_handle *info;
236      const char *s;
237 {
238   char *n;
239
240   assert (info->stack != NULL);
241
242   n = (char *) xmalloc (strlen (s) + strlen (info->stack->type) + 1);
243   sprintf (n, "%s%s", s, info->stack->type);
244   free (info->stack->type);
245   info->stack->type = n;
246
247   return true;
248 }
249
250 /* Append a string to the type on the top of the type stack.  */
251
252 static boolean
253 append_type (info, s)
254      struct pr_handle *info;
255      const char *s;
256 {
257   unsigned int len;
258
259   if (s == NULL)
260     return false;
261
262   assert (info->stack != NULL);
263
264   len = strlen (info->stack->type);
265   info->stack->type = (char *) xrealloc (info->stack->type,
266                                          len + strlen (s) + 1);
267   strcpy (info->stack->type + len, s);
268
269   return true;
270 }
271
272 /* We use an underscore to indicate where the name should go in a type
273    string.  This function substitutes a string for the underscore.  If
274    there is no underscore, the name follows the type.  */
275
276 static boolean
277 substitute_type (info, s)
278      struct pr_handle *info;
279      const char *s;
280 {
281   char *u;
282
283   assert (info->stack != NULL);
284
285   u = strchr (info->stack->type, '|');
286   if (u != NULL)
287     {
288       char *n;
289
290       n = (char *) xmalloc (strlen (info->stack->type) + strlen (s));
291
292       memcpy (n, info->stack->type, u - info->stack->type);
293       strcpy (n + (u - info->stack->type), s);
294       strcat (n, u + 1);
295
296       free (info->stack->type);
297       info->stack->type = n;
298
299       return true;
300     }
301
302   if (strchr (s, '|') != NULL
303       && (strchr (info->stack->type, '{') != NULL
304           || strchr (info->stack->type, '(') != NULL))
305     {
306       if (! prepend_type (info, "(")
307           || ! append_type (info, ")"))
308         return false;
309     }
310
311   if (*s == '\0')
312     return true;
313
314   return (append_type (info, " ")
315           && append_type (info, s));
316 }
317
318 /* Indent the type at the top of the stack by appending spaces.  */
319
320 static boolean
321 indent_type (info)
322      struct pr_handle *info;
323 {
324   unsigned int i;
325
326   for (i = 0; i < info->indent; i++)
327     {
328       if (! append_type (info, " "))
329         return false;
330     }
331
332   return true;
333 }
334
335 /* Pop a type from the type stack.  */
336
337 static char *
338 pop_type (info)
339      struct pr_handle *info;
340 {
341   struct pr_stack *o;
342   char *ret;
343
344   assert (info->stack != NULL);
345
346   o = info->stack;
347   info->stack = o->next;
348   ret = o->type;
349   free (o);
350
351   return ret;
352 }
353
354 /* Print a VMA value into a string.  */
355
356 static void
357 print_vma (vma, buf, unsignedp, hexp)
358      bfd_vma vma;
359      char *buf;
360      boolean unsignedp;
361      boolean hexp;
362 {
363   if (sizeof (vma) <= sizeof (unsigned long))
364     {
365       if (hexp)
366         sprintf (buf, "0x%lx", (unsigned long) vma);
367       else if (unsignedp)
368         sprintf (buf, "%lu", (unsigned long) vma);
369       else
370         sprintf (buf, "%ld", (long) vma);
371     }
372   else
373     {
374       buf[0] = '0';
375       buf[1] = 'x';
376       sprintf_vma (buf + 2, vma);
377     }
378 }
379 \f
380 /* Start a new compilation unit.  */
381
382 static boolean
383 pr_start_compilation_unit (p, filename)
384      PTR p;
385      const char *filename;
386 {
387   struct pr_handle *info = (struct pr_handle *) p;
388
389   assert (info->indent == 0);
390
391   fprintf (info->f, "%s:\n", filename);
392
393   return true;
394 }
395
396 /* Start a source file within a compilation unit.  */
397
398 static boolean
399 pr_start_source (p, filename)
400      PTR p;
401      const char *filename;
402 {
403   struct pr_handle *info = (struct pr_handle *) p;
404
405   assert (info->indent == 0);
406
407   fprintf (info->f, " %s:\n", filename);
408
409   return true;
410 }
411
412 /* Push an empty type onto the type stack.  */
413
414 static boolean
415 pr_empty_type (p)
416      PTR p;
417 {
418   struct pr_handle *info = (struct pr_handle *) p;
419
420   return push_type (info, "<undefined>");
421 }
422
423 /* Push a void type onto the type stack.  */
424
425 static boolean
426 pr_void_type (p)
427      PTR p;
428 {
429   struct pr_handle *info = (struct pr_handle *) p;
430
431   return push_type (info, "void");
432 }
433
434 /* Push an integer type onto the type stack.  */
435
436 static boolean
437 pr_int_type (p, size, unsignedp)
438      PTR p;
439      unsigned int size;
440      boolean unsignedp;
441 {
442   struct pr_handle *info = (struct pr_handle *) p;
443   char ab[10];
444
445   sprintf (ab, "%sint%d", unsignedp ? "u" : "", size * 8);
446   return push_type (info, ab);
447 }
448
449 /* Push a floating type onto the type stack.  */
450
451 static boolean
452 pr_float_type (p, size)
453      PTR p;
454      unsigned int size;
455 {
456   struct pr_handle *info = (struct pr_handle *) p;
457   char ab[10];
458
459   if (size == 4)
460     return push_type (info, "float");
461   else if (size == 8)
462     return push_type (info, "double");
463
464   sprintf (ab, "float%d", size * 8);
465   return push_type (info, ab);
466 }
467
468 /* Push a complex type onto the type stack.  */
469
470 static boolean
471 pr_complex_type (p, size)
472      PTR p;
473      unsigned int size;
474 {
475   struct pr_handle *info = (struct pr_handle *) p;
476
477   if (! pr_float_type (p, size))
478     return false;
479
480   return prepend_type (info, "complex ");
481 }
482
483 /* Push a boolean type onto the type stack.  */
484
485 static boolean
486 pr_bool_type (p, size)
487      PTR p;
488      unsigned int size;
489 {
490   struct pr_handle *info = (struct pr_handle *) p;
491   char ab[10];
492
493   sprintf (ab, "bool%d", size * 8);
494
495   return push_type (info, ab);
496 }
497
498 /* Push an enum type onto the type stack.  */
499
500 static boolean
501 pr_enum_type (p, tag, names, values)
502      PTR p;
503      const char *tag;
504      const char **names;
505      bfd_signed_vma *values;
506 {
507   struct pr_handle *info = (struct pr_handle *) p;
508   unsigned int i;
509   bfd_signed_vma val;
510
511   if (! push_type (info, "enum "))
512     return false;
513   if (tag != NULL)
514     {
515       if (! append_type (info, tag)
516           || ! append_type (info, " "))
517         return false;
518     }
519   if (! append_type (info, "{ "))
520     return false;
521
522   if (names == NULL)
523     {
524       if (! append_type (info, "/* undefined */"))
525         return false;
526     }
527   else
528     {
529       val = 0;
530       for (i = 0; names[i] != NULL; i++)
531         {
532           if (i > 0)
533             {
534               if (! append_type (info, ", "))
535                 return false;
536             }
537
538           if (! append_type (info, names[i]))
539             return false;
540
541           if (values[i] != val)
542             {
543               char ab[20];
544
545               print_vma (values[i], ab, false, false);
546               if (! append_type (info, " = ")
547                   || ! append_type (info, ab))
548                 return false;
549               val = values[i];
550             }
551
552           ++val;
553         }
554     }
555
556   return append_type (info, " }");
557 }
558
559 /* Turn the top type on the stack into a pointer.  */
560
561 static boolean
562 pr_pointer_type (p)
563      PTR p;
564 {
565   struct pr_handle *info = (struct pr_handle *) p;
566
567   assert (info->stack != NULL);
568
569   return substitute_type (info, "*|");
570 }
571
572 /* Turn the top type on the stack into a function returning that type.  */
573
574 static boolean
575 pr_function_type (p)
576      PTR p;
577 {
578   struct pr_handle *info = (struct pr_handle *) p;
579
580   assert (info->stack != NULL);
581
582   return substitute_type (info, "(|) ()");
583 }
584
585 /* Turn the top type on the stack into a reference to that type.  */
586
587 static boolean
588 pr_reference_type (p)
589      PTR p;
590 {
591   struct pr_handle *info = (struct pr_handle *) p;
592
593   assert (info->stack != NULL);
594
595   return substitute_type (info, "&|");
596 }
597
598 /* Make a range type.  */
599
600 static boolean
601 pr_range_type (p, lower, upper)
602      PTR p;
603      bfd_signed_vma lower;
604      bfd_signed_vma upper;
605 {
606   struct pr_handle *info = (struct pr_handle *) p;
607   char abl[20], abu[20];
608
609   assert (info->stack != NULL);
610
611   if (! substitute_type (info, ""))
612     return false;
613
614   print_vma (lower, abl, false, false);
615   print_vma (upper, abu, false, false);
616
617   return (prepend_type (info, "range (")
618           && append_type (info, "):")
619           && append_type (info, abl)
620           && append_type (info, ":")
621           && append_type (info, abu));
622 }
623
624 /* Make an array type.  */
625
626 /*ARGSUSED*/
627 static boolean
628 pr_array_type (p, lower, upper, stringp)
629      PTR p;
630      bfd_signed_vma lower;
631      bfd_signed_vma upper;
632      boolean stringp;
633 {
634   struct pr_handle *info = (struct pr_handle *) p;
635   char *range_type;
636   char abl[20], abu[20], ab[50];
637
638   range_type = pop_type (info);
639   if (range_type == NULL)
640     return false;
641
642   if (lower == 0)
643     {
644       if (upper == -1)
645         sprintf (ab, "|[]");
646       else
647         {
648           print_vma (upper + 1, abu, false, false);
649           sprintf (ab, "|[%s]", abu);
650         }
651     }
652   else
653     {
654       print_vma (lower, abl, false, false);
655       print_vma (upper, abu, false, false);
656       sprintf (ab, "|[%s:%s]", abl, abu);
657     }
658
659   if (! substitute_type (info, ab))
660     return false;
661
662   if (strcmp (range_type, "int") != 0)
663     {
664       if (! append_type (info, ":")
665           || ! append_type (info, range_type))
666         return false;
667     }
668
669   if (stringp)
670     {
671       if (! append_type (info, " /* string */"))
672         return false;
673     }
674
675   return true;
676 }
677
678 /* Make a set type.  */
679
680 /*ARGSUSED*/
681 static boolean
682 pr_set_type (p, bitstringp)
683      PTR p;
684      boolean bitstringp;
685 {
686   struct pr_handle *info = (struct pr_handle *) p;
687
688   if (! substitute_type (info, ""))
689     return false;
690
691   if (! prepend_type (info, "set { ")
692       || ! append_type (info, " }"))
693     return false;
694
695   if (bitstringp)
696     {
697       if (! append_type (info, "/* bitstring */"))
698         return false;
699     }
700
701   return true;
702 }
703
704 /* Make an offset type.  */
705
706 static boolean
707 pr_offset_type (p)
708      PTR p;
709 {
710   struct pr_handle *info = (struct pr_handle *) p;
711   char *t;
712
713   if (! substitute_type (info, ""))
714     return false;
715
716   t = pop_type (info);
717   if (t == NULL)
718     return false;
719
720   return (substitute_type (info, "")
721           && prepend_type (info, " ")
722           && prepend_type (info, t)
723           && append_type (info, "::|"));
724 }
725
726 /* Make a method type.  */
727
728 static boolean
729 pr_method_type (p, domain, argcount)
730      PTR p;
731      boolean domain;
732      int argcount;
733 {
734   struct pr_handle *info = (struct pr_handle *) p;
735   unsigned int len;
736   char *domain_type;
737   char **arg_types;
738   char *s;
739
740   len = 10;
741
742   if (! domain)
743     domain_type = NULL;
744   else
745     {
746       if (! substitute_type (info, ""))
747         return false;
748       domain_type = pop_type (info);
749       if (domain_type == NULL)
750         return false;
751       if (strncmp (domain_type, "class ", sizeof "class " - 1) == 0
752           && strchr (domain_type + sizeof "class " - 1, ' ') == NULL)
753         domain_type += sizeof "class " - 1;
754       else if (strncmp (domain_type, "union class ",
755                         sizeof "union class ") == 0
756                && (strchr (domain_type + sizeof "union class " - 1, ' ')
757                    == NULL))
758         domain_type += sizeof "union class " - 1;
759       len += strlen (domain_type);
760     }
761
762   if (argcount <= 0)
763     {
764       arg_types = NULL;
765       len += 15;
766     }
767   else
768     {
769       int i;
770
771       arg_types = (char **) xmalloc (argcount * sizeof *arg_types);
772       for (i = argcount - 1; i >= 0; i--)
773         {
774           if (! substitute_type (info, ""))
775             return false;
776           arg_types[i] = pop_type (info);
777           if (arg_types[i] == NULL)
778             return false;
779           len += strlen (arg_types[i]) + 2;
780         }
781     }
782
783   /* Now the return type is on the top of the stack.  */
784
785   s = (char *) xmalloc (len);
786   if (! domain)
787     *s = '\0';
788   else
789     strcpy (s, domain_type);
790   strcat (s, "::| (");
791
792   if (argcount < 0)
793     strcat (s, "/* unknown */");
794   else
795     {
796       int i;
797
798       for (i = 0; i < argcount; i++)
799         {
800           if (i > 0)
801             strcat (s, ", ");
802           strcat (s, arg_types[i]);
803         }
804     }
805
806   strcat (s, ")");
807
808   if (! substitute_type (info, s))
809     return false;
810
811   free (s);
812
813   return true;
814 }
815
816 /* Make a const qualified type.  */
817
818 static boolean
819 pr_const_type (p)
820      PTR p;
821 {
822   struct pr_handle *info = (struct pr_handle *) p;
823
824   return substitute_type (info, "const |");
825 }
826
827 /* Make a volatile qualified type.  */
828
829 static boolean
830 pr_volatile_type (p)
831      PTR p;
832 {
833   struct pr_handle *info = (struct pr_handle *) p;
834
835   return substitute_type (info, "volatile |");
836 }
837
838 /* Start accumulating a struct type.  */
839
840 static boolean
841 pr_start_struct_type (p, tag, structp, size)
842      PTR p;
843      const char *tag;
844      boolean structp;
845      unsigned int size;
846 {
847   struct pr_handle *info = (struct pr_handle *) p;
848   char ab[30];
849
850   info->indent += 2;
851
852   if (! push_type (info, structp ? "struct " : "union "))
853     return false;
854   if (tag != NULL)
855     {
856       if (! append_type (info, tag)
857           || ! append_type (info, " "))
858         return false;
859     }
860   if (size != 0)
861     sprintf (ab, "{ /* size %u */\n", size);
862   else
863     strcpy (ab, "{\n");
864   if (! append_type (info, ab))
865     return false;
866   info->stack->visibility = DEBUG_VISIBILITY_PUBLIC;
867   return indent_type (info);
868 }
869
870 /* Output the visibility of a field in a struct.  */
871
872 static boolean
873 pr_fix_visibility (info, visibility)
874      struct pr_handle *info;
875      enum debug_visibility visibility;
876 {
877   const char *s;
878   struct pr_stack *top;
879   char *t;
880   unsigned int len;
881
882   assert (info->stack != NULL && info->stack->next != NULL);
883
884   if (info->stack->next->visibility == visibility)
885     return true;
886
887   assert (info->stack->next->visibility != DEBUG_VISIBILITY_IGNORE);
888
889   switch (visibility)
890     {
891     case DEBUG_VISIBILITY_PUBLIC:
892       s = "public";
893       break;
894     case DEBUG_VISIBILITY_PRIVATE:
895       s = "private";
896       break;
897     case DEBUG_VISIBILITY_PROTECTED:
898       s = "protected";
899       break;
900     default:
901       abort ();
902       return false;
903     }
904
905   /* Trim off a trailing space in the struct string, to make the
906      output look a bit better, then stick on the visibility string.
907      Pop the stack temporarily to make the string manipulation
908      simpler.  */
909
910   top = info->stack;
911   info->stack = top->next;
912
913   t = info->stack->type;
914   len = strlen (t);
915   assert (t[len - 1] == ' ');
916   t[len - 1] = '\0';
917
918   if (! append_type (info, s)
919       || ! append_type (info, ":\n")
920       || ! indent_type (info))
921     return false;
922
923   info->stack->visibility = visibility;
924
925   info->stack = top;
926
927   return true;
928 }
929
930 /* Add a field to a struct type.  */
931
932 static boolean
933 pr_struct_field (p, name, bitpos, bitsize, visibility)
934      PTR p;
935      const char *name;
936      bfd_vma bitpos;
937      bfd_vma bitsize;
938      enum debug_visibility visibility;
939 {
940   struct pr_handle *info = (struct pr_handle *) p;
941   char ab[20];
942
943   if (! pr_fix_visibility (info, visibility))
944     return false;
945
946   if (! substitute_type (info, name))
947     return false;
948
949   if (! append_type (info, "; /* "))
950     return false;
951
952   if (bitsize != 0)
953     {
954       print_vma (bitsize, ab, true, false);
955       if (! append_type (info, "bitsize ")
956           || ! append_type (info, ab)
957           || ! append_type (info, ", "))
958         return false;
959     }
960
961   print_vma (bitpos, ab, true, false);
962   if (! append_type (info, "bitpos ")
963       || ! append_type (info, ab)
964       || ! append_type (info, " */\n")
965       || ! indent_type (info))
966     return false;
967
968   return append_type (info, pop_type (info));
969 }
970
971 /* Finish a struct type.  */
972
973 static boolean
974 pr_end_struct_type (p)
975      PTR p;
976 {
977   struct pr_handle *info = (struct pr_handle *) p;
978   char *s;
979
980   assert (info->stack != NULL);
981   assert (info->indent >= 2);
982
983   info->indent -= 2;
984
985   /* Change the trailing indentation to have a close brace.  */
986   s = info->stack->type + strlen (info->stack->type) - 2;
987   assert (strcmp (s, "  ") == 0);
988
989   *s++ = '}';
990   *s = '\0';
991
992   return true;
993 }
994
995 /* Start a class type.  */
996
997 static boolean
998 pr_start_class_type (p, tag, structp, size, vptr, ownvptr)
999      PTR p;
1000      const char *tag;
1001      boolean structp;
1002      unsigned int size;
1003      boolean vptr;
1004      boolean ownvptr;
1005 {
1006   struct pr_handle *info = (struct pr_handle *) p;
1007   char *tv = NULL;
1008
1009   info->indent += 2;
1010
1011   if (vptr && ! ownvptr)
1012     {
1013       tv = pop_type (info);
1014       if (tv == NULL)
1015         return false;
1016     }
1017
1018   if (! push_type (info, structp ? "class " : "union class "))
1019     return false;
1020   if (tag != NULL)
1021     {
1022       if (! append_type (info, tag)
1023           || ! append_type (info, " "))
1024         return false;
1025     }
1026   if (! append_type (info, "{"))
1027     return false;
1028   if (size != 0 || vptr || ownvptr)
1029     {
1030       if (! append_type (info, " /*"))
1031         return false;
1032
1033       if (size != 0)
1034         {
1035           char ab[20];
1036
1037           sprintf (ab, "%u", size);
1038           if (! append_type (info, " size ")
1039               || ! append_type (info, ab))
1040             return false;
1041         }
1042
1043       if (vptr)
1044         {
1045           if (! append_type (info, " vtable "))
1046             return false;
1047           if (ownvptr)
1048             {
1049               if (! append_type (info, "self "))
1050                 return false;
1051             }
1052           else
1053             {
1054               if (! append_type (info, tv)
1055                   || ! append_type (info, " "))
1056                 return false;
1057             }
1058         }
1059
1060       if (! append_type (info, " */"))
1061         return false;
1062     }
1063
1064   info->stack->visibility = DEBUG_VISIBILITY_PRIVATE;
1065
1066   return (append_type (info, "\n")
1067           && indent_type (info));
1068 }
1069
1070 /* Add a static member to a class.  */
1071
1072 static boolean
1073 pr_class_static_member (p, name, physname, visibility)
1074      PTR p;
1075      const char *name;
1076      const char *physname;
1077      enum debug_visibility visibility;
1078 {
1079   struct pr_handle *info = (struct pr_handle *) p;
1080
1081   if (! pr_fix_visibility (info, visibility))
1082     return false;
1083
1084   if (! substitute_type (info, name))
1085     return false;
1086
1087   return (prepend_type (info, "static ")
1088           && append_type (info, "; /* physname ")
1089           && append_type (info, physname)
1090           && append_type (info, " */\n")
1091           && indent_type (info)
1092           && append_type (info, pop_type (info)));
1093 }
1094
1095 /* Add a base class to a class.  */
1096
1097 static boolean
1098 pr_class_baseclass (p, bitpos, virtual, visibility)
1099      PTR p;
1100      bfd_vma bitpos;
1101      boolean virtual;
1102      enum debug_visibility visibility;
1103 {
1104   struct pr_handle *info = (struct pr_handle *) p;
1105   char *t;
1106   const char *prefix;
1107   char ab[20];
1108   char *s, *l, *n;
1109
1110   assert (info->stack != NULL && info->stack->next != NULL);
1111
1112   if (! substitute_type (info, ""))
1113     return false;
1114
1115   t = pop_type (info);
1116   if (t == NULL)
1117     return false;
1118
1119   if (strncmp (t, "class ", sizeof "class " - 1) == 0)
1120     t += sizeof "class " - 1;
1121
1122   /* Push it back on to take advantage of the prepend_type and
1123      append_type routines.  */
1124   if (! push_type (info, t))
1125     return false;
1126
1127   if (virtual)
1128     {
1129       if (! prepend_type (info, "virtual "))
1130         return false;
1131     }
1132
1133   switch (visibility)
1134     {
1135     case DEBUG_VISIBILITY_PUBLIC:
1136       prefix = "public ";
1137       break;
1138     case DEBUG_VISIBILITY_PROTECTED:
1139       prefix = "protected ";
1140       break;
1141     case DEBUG_VISIBILITY_PRIVATE:
1142       prefix = "private ";
1143       break;
1144     default:
1145       prefix = "/* unknown visibility */ ";
1146       break;
1147     }
1148
1149   if (! prepend_type (info, prefix))
1150     return false;
1151
1152   if (bitpos != 0)
1153     {
1154       print_vma (bitpos, ab, true, false);
1155       if (! append_type (info, " /* bitpos ")
1156           || ! append_type (info, ab)
1157           || ! append_type (info, " */"))
1158         return false;
1159     }
1160
1161   /* Now the top of the stack is something like "public A / * bitpos
1162      10 * /".  The next element on the stack is something like "class
1163      xx { / * size 8 * /\n...".  We want to substitute the top of the
1164      stack in before the {.  */
1165   s = strchr (info->stack->next->type, '{');
1166   assert (s != NULL);
1167   --s;
1168
1169   /* If there is already a ':', then we already have a baseclass, and
1170      we must append this one after a comma.  */
1171   for (l = info->stack->next->type; l != s; l++)
1172     if (*l == ':')
1173       break;
1174   if (! prepend_type (info, l == s ? " : " : ", "))
1175     return false;
1176
1177   t = pop_type (info);
1178   if (t == NULL)
1179     return false;
1180
1181   n = (char *) xmalloc (strlen (info->stack->type) + strlen (t) + 1);
1182   memcpy (n, info->stack->type, s - info->stack->type);
1183   strcpy (n + (s - info->stack->type), t);
1184   strcat (n, s);
1185
1186   free (info->stack->type);
1187   info->stack->type = n;
1188
1189   free (t);
1190
1191   return true;
1192 }
1193
1194 /* Start adding a method to a class.  */
1195
1196 static boolean
1197 pr_class_start_method (p, name)
1198      PTR p;
1199      const char *name;
1200 {
1201   struct pr_handle *info = (struct pr_handle *) p;
1202
1203   if (! push_type (info, ""))
1204     return false;
1205   info->stack->method = name;
1206   return true;
1207 }
1208
1209 /* Add a variant to a method.  */
1210
1211 static boolean
1212 pr_class_method_variant (p, argtypes, visibility, constp, volatilep, voffset,
1213                          context)
1214      PTR p;
1215      const char *argtypes;
1216      enum debug_visibility visibility;
1217      boolean constp;
1218      boolean volatilep;
1219      bfd_vma voffset;
1220      boolean context;
1221 {
1222   struct pr_handle *info = (struct pr_handle *) p;
1223   char *method_type;
1224   char *context_type;
1225
1226   assert (info->stack != NULL);
1227   assert (info->stack->next != NULL);
1228
1229   /* Put the const and volatile qualifiers on the type.  */
1230   if (volatilep)
1231     {
1232       if (! prepend_type (info, "volatile "))
1233         return false;
1234     }
1235   if (constp)
1236     {
1237       if (! prepend_type (info, "const "))
1238         return false;
1239     }
1240
1241   /* Stick the name of the method into its type.  */
1242   if (! substitute_type (info,
1243                          (context
1244                           ? info->stack->next->next->method
1245                           : info->stack->next->method)))
1246     return false;
1247
1248   /* Get the type.  */
1249   method_type = pop_type (info);
1250   if (method_type == NULL)
1251     return false;
1252
1253   /* Pull off the context type if there is one.  */
1254   if (! context)
1255     context_type = NULL;
1256   else
1257     {
1258       context_type = pop_type (info);
1259       if (context_type == NULL)
1260         return false;
1261     }
1262
1263   /* Now the top of the stack is the holder for the method, and the
1264      second element on the stack is the class.  */
1265
1266   if (! pr_fix_visibility (info, visibility))
1267     return false;
1268
1269   if (! append_type (info, method_type)
1270       || ! append_type (info, " /* ")
1271       || ! append_type (info, argtypes))
1272     return false;
1273   if (context || voffset != 0)
1274     {
1275       char ab[20];
1276
1277       if (context)
1278         {
1279           if (! append_type (info, "context ")
1280               || ! append_type (info, context_type)
1281               || ! append_type (info, " "))
1282             return false;
1283         }
1284       print_vma (voffset, ab, true, false);
1285       if (! append_type (info, "voffset ")
1286           || ! append_type (info, ab))
1287         return false;
1288     }
1289
1290   return (append_type (info, " */;\n")
1291           && indent_type (info));
1292 }
1293
1294 /* Add a static variant to a method.  */
1295
1296 static boolean
1297 pr_class_static_method_variant (p, argtypes, visibility, constp, volatilep)
1298      PTR p;
1299      const char *argtypes;
1300      enum debug_visibility visibility;
1301      boolean constp;
1302      boolean volatilep;
1303 {
1304   struct pr_handle *info = (struct pr_handle *) p;
1305   char *method_type;
1306
1307   assert (info->stack != NULL);
1308   assert (info->stack->next != NULL);
1309   assert (info->stack->next->method != NULL);
1310
1311   /* Put the const and volatile qualifiers on the type.  */
1312   if (volatilep)
1313     {
1314       if (! prepend_type (info, "volatile "))
1315         return false;
1316     }
1317   if (constp)
1318     {
1319       if (! prepend_type (info, "const "))
1320         return false;
1321     }
1322
1323   /* Mark it as static.  */
1324   if (! prepend_type (info, "static "))
1325     return false;
1326
1327   /* Stick the name of the method into its type.  */
1328   if (! substitute_type (info, info->stack->next->method))
1329     return false;
1330
1331   /* Get the type.  */
1332   method_type = pop_type (info);
1333   if (method_type == NULL)
1334     return false;
1335
1336   /* Now the top of the stack is the holder for the method, and the
1337      second element on the stack is the class.  */
1338
1339   if (! pr_fix_visibility (info, visibility))
1340     return false;
1341
1342   return (append_type (info, method_type)
1343           && append_type (info, " /* ")
1344           && append_type (info, argtypes)
1345           && append_type (info, " */;\n")
1346           && indent_type (info));
1347 }
1348
1349 /* Finish up a method.  */
1350
1351 static boolean
1352 pr_class_end_method (p)
1353      PTR p;
1354 {
1355   struct pr_handle *info = (struct pr_handle *) p;
1356
1357   /* The method variants have been appended to the string on top of
1358      the stack with the correct indentation.  We just need the append
1359      the string on top of the stack to the class string that is second
1360      on the stack.  */
1361   return append_type (info, pop_type (info));
1362 }
1363
1364 /* Finish up a class.  */
1365
1366 static boolean
1367 pr_end_class_type (p)
1368      PTR p;
1369 {
1370   return pr_end_struct_type (p);
1371 }
1372
1373 /* Push a type on the stack using a typedef name.  */
1374
1375 static boolean
1376 pr_typedef_type (p, name)
1377      PTR p;
1378      const char *name;
1379 {
1380   struct pr_handle *info = (struct pr_handle *) p;
1381
1382   return push_type (info, name);
1383 }
1384
1385 /* Push a type on the stack using a tag name.  */
1386
1387 static boolean
1388 pr_tag_type (p, name, kind)
1389      PTR p;
1390      const char *name;
1391      enum debug_type_kind kind;
1392 {
1393   struct pr_handle *info = (struct pr_handle *) p;
1394   const char *t;
1395
1396   switch (kind)
1397     {
1398     case DEBUG_KIND_STRUCT:
1399       t = "struct ";
1400       break;
1401     case DEBUG_KIND_UNION:
1402       t = "union ";
1403       break;
1404     case DEBUG_KIND_ENUM:
1405       t = "enum ";
1406       break;
1407     case DEBUG_KIND_CLASS:
1408       t = "class ";
1409       break;
1410     case DEBUG_KIND_UNION_CLASS:
1411       t = "union class ";
1412       break;
1413     default:
1414       abort ();
1415       return false;
1416     }
1417
1418   return (push_type (info, t)
1419           && append_type (info, name));
1420 }
1421
1422 /* Output a typedef.  */
1423
1424 static boolean
1425 pr_typdef (p, name)
1426      PTR p;
1427      const char *name;
1428 {
1429   struct pr_handle *info = (struct pr_handle *) p;
1430   char *s;
1431
1432   if (! substitute_type (info, name))
1433     return false;
1434
1435   s = pop_type (info);
1436   if (s == NULL)
1437     return false;
1438
1439   indent (info);
1440   fprintf (info->f, "typedef %s;\n", s);
1441
1442   free (s);
1443
1444   return true;
1445 }
1446
1447 /* Output a tag.  The tag should already be in the string on the
1448    stack, so all we have to do here is print it out.  */
1449
1450 /*ARGSUSED*/
1451 static boolean
1452 pr_tag (p, name)
1453      PTR p;
1454      const char *name;
1455 {
1456   struct pr_handle *info = (struct pr_handle *) p;
1457   char *t;
1458
1459   t = pop_type (info);
1460   if (t == NULL)
1461     return false;
1462
1463   indent (info);
1464   fprintf (info->f, "%s;\n", t);
1465
1466   free (t);
1467
1468   return true;
1469 }
1470
1471 /* Output an integer constant.  */
1472
1473 static boolean
1474 pr_int_constant (p, name, val)
1475      PTR p;
1476      const char *name;
1477      bfd_vma val;
1478 {
1479   struct pr_handle *info = (struct pr_handle *) info;
1480   char ab[20];
1481
1482   indent (info);
1483   print_vma (val, ab, false, false);
1484   fprintf (info->f, "const int %s = %s;\n", name, ab);
1485   return true;
1486 }
1487
1488 /* Output a floating point constant.  */
1489
1490 static boolean
1491 pr_float_constant (p, name, val)
1492      PTR p;
1493      const char *name;
1494      double val;
1495 {
1496   struct pr_handle *info = (struct pr_handle *) info;
1497
1498   indent (info);
1499   fprintf (info->f, "const double %s = %g;\n", name, val);
1500   return true;
1501 }
1502
1503 /* Output a typed constant.  */
1504
1505 static boolean
1506 pr_typed_constant (p, name, val)
1507      PTR p;
1508      const char *name;
1509      bfd_vma val;
1510 {
1511   struct pr_handle *info = (struct pr_handle *) p;
1512   char *t;
1513   char ab[20];
1514
1515   t = pop_type (info);
1516   if (t == NULL)
1517     return false;
1518
1519   indent (info);
1520   print_vma (val, ab, false, false);
1521   fprintf (info->f, "const %s %s = %s;\n", t, name, ab);
1522
1523   free (t);
1524
1525   return true;
1526 }
1527
1528 /* Output a variable.  */
1529
1530 static boolean
1531 pr_variable (p, name, kind, val)
1532      PTR p;
1533      const char *name;
1534      enum debug_var_kind kind;
1535      bfd_vma val;
1536 {
1537   struct pr_handle *info = (struct pr_handle *) p;
1538   char *t;
1539   char ab[20];
1540
1541   if (! substitute_type (info, name))
1542     return false;
1543
1544   t = pop_type (info);
1545   if (t == NULL)
1546     return false;
1547
1548   indent (info);
1549   switch (kind)
1550     {
1551     case DEBUG_STATIC:
1552     case DEBUG_LOCAL_STATIC:
1553       fprintf (info->f, "static ");
1554       break;
1555     case DEBUG_REGISTER:
1556       fprintf (info->f, "register ");
1557       break;
1558     default:
1559       break;
1560     }
1561   print_vma (val, ab, true, true);
1562   fprintf (info->f, "%s /* %s */;\n", t, ab);
1563
1564   free (t);
1565
1566   return true;
1567 }
1568
1569 /* Start outputting a function.  */
1570
1571 static boolean
1572 pr_start_function (p, name, global)
1573      PTR p;
1574      const char *name;
1575      boolean global;
1576 {
1577   struct pr_handle *info = (struct pr_handle *) p;
1578   char *t;
1579
1580   if (! substitute_type (info, name))
1581     return false;
1582
1583   t = pop_type (info);
1584   if (t == NULL)
1585     return false;
1586
1587   indent (info);
1588   if (! global)
1589     fprintf (info->f, "static ");
1590   fprintf (info->f, "%s (", t);
1591
1592   info->parameter = 1;
1593
1594   return true;
1595 }
1596
1597 /* Output a function parameter.  */
1598
1599 static boolean
1600 pr_function_parameter (p, name, kind, val)
1601      PTR p;
1602      const char *name;
1603      enum debug_parm_kind kind;
1604      bfd_vma val;
1605 {
1606   struct pr_handle *info = (struct pr_handle *) p;
1607   char *t;
1608   char ab[20];
1609
1610   if (kind == DEBUG_PARM_REFERENCE
1611       || kind == DEBUG_PARM_REF_REG)
1612     {
1613       if (! pr_reference_type (p))
1614         return false;
1615     }
1616
1617   if (! substitute_type (info, name))
1618     return false;
1619
1620   t = pop_type (info);
1621   if (t == NULL)
1622     return false;
1623
1624   if (info->parameter != 1)
1625     fprintf (info->f, ", ");
1626
1627   if (kind == DEBUG_PARM_REG || kind == DEBUG_PARM_REF_REG)
1628     fprintf (info->f, "register ");
1629
1630   print_vma (val, ab, true, true);
1631   fprintf (info->f, "%s /* %s */", t, ab);
1632
1633   free (t);
1634
1635   ++info->parameter;
1636
1637   return true;
1638 }
1639
1640 /* Start writing out a block.  */
1641
1642 static boolean
1643 pr_start_block (p, addr)
1644      PTR p;
1645      bfd_vma addr;
1646 {
1647   struct pr_handle *info = (struct pr_handle *) p;
1648   char ab[20];
1649
1650   if (info->parameter > 0)
1651     {
1652       fprintf (info->f, ")\n");
1653       info->parameter = 0;
1654     }
1655
1656   indent (info);
1657   print_vma (addr, ab, true, true);
1658   fprintf (info->f, "{ /* %s */\n", ab);
1659
1660   info->indent += 2;
1661
1662   return true;
1663 }
1664
1665 /* Write out line number information.  */
1666
1667 static boolean
1668 pr_lineno (p, filename, lineno, addr)
1669      PTR p;
1670      const char *filename;
1671      unsigned long lineno;
1672      bfd_vma addr;
1673 {
1674   struct pr_handle *info = (struct pr_handle *) p;
1675   char ab[20];
1676
1677   indent (info);
1678   print_vma (addr, ab, true, true);
1679   fprintf (info->f, "/* file %s line %lu addr %s */\n", filename, lineno, ab);
1680
1681   return true;
1682 }
1683
1684 /* Finish writing out a block.  */
1685
1686 static boolean
1687 pr_end_block (p, addr)
1688      PTR p;
1689      bfd_vma addr;
1690 {
1691   struct pr_handle *info = (struct pr_handle *) p;
1692   char ab[20];
1693
1694   info->indent -= 2;
1695
1696   indent (info);
1697   print_vma (addr, ab, true, true);
1698   fprintf (info->f, "} /* %s */\n", ab);
1699
1700   return true;
1701 }
1702
1703 /* Finish writing out a function.  */
1704
1705 /*ARGSUSED*/
1706 static boolean
1707 pr_end_function (p)
1708      PTR p;
1709 {
1710   return true;
1711 }