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