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