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