* objdump.c: New command line option --debugging-tags.
[external/binutils.git] / binutils / prdbg.c
1 /* prdbg.c -- Print out generic debugging information.
2    Copyright 1995, 1996, 2002, 2003 Free Software Foundation, Inc.
3    Written by Ian Lance Taylor <ian@cygnus.com>.
4    Tags style generation written by Salvador E. Tropea <set@computer.org>.
5
6    This file is part of GNU Binutils.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21    02111-1307, USA.  */
22
23 /* This file prints out the generic debugging information, by
24    supplying a set of routines to debug_write.  */
25
26 #include <stdio.h>
27 #include <assert.h>
28
29 #include "bfd.h"
30 #include "bucomm.h"
31 #include "libiberty.h"
32 #include "debug.h"
33 #include "budbg.h"
34
35 /* This is the structure we use as a handle for these routines.  */
36
37 struct pr_handle
38 {
39   /* File to print information to.  */
40   FILE *f;
41   /* Current indentation level.  */
42   unsigned int indent;
43   /* Type stack.  */
44   struct pr_stack *stack;
45   /* Parameter number we are about to output.  */
46   int parameter;
47   /* The following are used only by the tags code (tg_).  */
48   /* Name of the file we are using.  */
49   char *filename;
50   /* The BFD.  */
51   bfd *abfd;
52   /* The symbols table for this BFD.  */
53   asymbol **syms;
54   /* Pointer to a function to demangle symbols.  */
55   char *(*demangler) (bfd *, const char *);
56 };
57
58 /* The type stack.  */
59
60 struct pr_stack
61 {
62   /* Next element on the stack.  */
63   struct pr_stack *next;
64   /* This element.  */
65   char *type;
66   /* Current visibility of fields if this is a class.  */
67   enum debug_visibility visibility;
68   /* Name of the current method we are handling.  */
69   const char *method;
70   /* The following are used only by the tags code (tg_).  */
71   /* Type for the container (struct, union, class, union class).  */
72   const char *flavor;
73   /* A comma separated list of parent classes.  */
74   char *parents;
75   /* How many parents contains parents.  */
76   int num_parents;
77 };
78
79 static void indent
80   PARAMS ((struct pr_handle *));
81 static bfd_boolean push_type
82   PARAMS ((struct pr_handle *, const char *));
83 static bfd_boolean prepend_type
84   PARAMS ((struct pr_handle *, const char *));
85 static bfd_boolean append_type
86   PARAMS ((struct pr_handle *, const char *));
87 static bfd_boolean substitute_type
88   PARAMS ((struct pr_handle *, const char *));
89 static bfd_boolean indent_type
90   PARAMS ((struct pr_handle *));
91 static char *pop_type
92   PARAMS ((struct pr_handle *));
93 static void print_vma
94   PARAMS ((bfd_vma, char *, bfd_boolean, bfd_boolean));
95 static bfd_boolean pr_fix_visibility
96   PARAMS ((struct pr_handle *, enum debug_visibility));
97 static bfd_boolean pr_start_compilation_unit
98   PARAMS ((PTR, const char *));
99 static bfd_boolean pr_start_source
100   PARAMS ((PTR, const char *));
101 static bfd_boolean pr_empty_type
102   PARAMS ((PTR));
103 static bfd_boolean pr_void_type
104   PARAMS ((PTR));
105 static bfd_boolean pr_int_type
106   PARAMS ((PTR, unsigned int, bfd_boolean));
107 static bfd_boolean pr_float_type
108   PARAMS ((PTR, unsigned int));
109 static bfd_boolean pr_complex_type
110   PARAMS ((PTR, unsigned int));
111 static bfd_boolean pr_bool_type
112   PARAMS ((PTR, unsigned int));
113 static bfd_boolean pr_enum_type
114   PARAMS ((PTR, const char *, const char **, bfd_signed_vma *));
115 static bfd_boolean pr_pointer_type
116   PARAMS ((PTR));
117 static bfd_boolean pr_function_type
118   PARAMS ((PTR, int, bfd_boolean));
119 static bfd_boolean pr_reference_type
120   PARAMS ((PTR));
121 static bfd_boolean pr_range_type
122   PARAMS ((PTR, bfd_signed_vma, bfd_signed_vma));
123 static bfd_boolean pr_array_type
124   PARAMS ((PTR, bfd_signed_vma, bfd_signed_vma, bfd_boolean));
125 static bfd_boolean pr_set_type
126   PARAMS ((PTR, bfd_boolean));
127 static bfd_boolean pr_offset_type
128   PARAMS ((PTR));
129 static bfd_boolean pr_method_type
130   PARAMS ((PTR, bfd_boolean, int, bfd_boolean));
131 static bfd_boolean pr_const_type
132   PARAMS ((PTR));
133 static bfd_boolean pr_volatile_type
134   PARAMS ((PTR));
135 static bfd_boolean pr_start_struct_type
136   PARAMS ((PTR, const char *, unsigned int, bfd_boolean, unsigned int));
137 static bfd_boolean pr_struct_field
138   PARAMS ((PTR, const char *, bfd_vma, bfd_vma, enum debug_visibility));
139 static bfd_boolean pr_end_struct_type
140   PARAMS ((PTR));
141 static bfd_boolean pr_start_class_type
142   PARAMS ((PTR, const char *, unsigned int, bfd_boolean, unsigned int,
143            bfd_boolean, bfd_boolean));
144 static bfd_boolean pr_class_static_member
145   PARAMS ((PTR, const char *, const char *, enum debug_visibility));
146 static bfd_boolean pr_class_baseclass
147   PARAMS ((PTR, bfd_vma, bfd_boolean, enum debug_visibility));
148 static bfd_boolean pr_class_start_method
149   PARAMS ((PTR, const char *));
150 static bfd_boolean pr_class_method_variant
151   PARAMS ((PTR, const char *, enum debug_visibility, bfd_boolean, bfd_boolean,
152            bfd_vma, bfd_boolean));
153 static bfd_boolean pr_class_static_method_variant
154   PARAMS ((PTR, const char *, enum debug_visibility, bfd_boolean,
155            bfd_boolean));
156 static bfd_boolean pr_class_end_method
157   PARAMS ((PTR));
158 static bfd_boolean pr_end_class_type
159   PARAMS ((PTR));
160 static bfd_boolean pr_typedef_type
161   PARAMS ((PTR, const char *));
162 static bfd_boolean pr_tag_type
163   PARAMS ((PTR, const char *, unsigned int, enum debug_type_kind));
164 static bfd_boolean pr_typdef
165   PARAMS ((PTR, const char *));
166 static bfd_boolean pr_tag
167   PARAMS ((PTR, const char *));
168 static bfd_boolean pr_int_constant
169   PARAMS ((PTR, const char *, bfd_vma));
170 static bfd_boolean pr_float_constant
171   PARAMS ((PTR, const char *, double));
172 static bfd_boolean pr_typed_constant
173   PARAMS ((PTR, const char *, bfd_vma));
174 static bfd_boolean pr_variable
175   PARAMS ((PTR, const char *, enum debug_var_kind, bfd_vma));
176 static bfd_boolean pr_start_function
177   PARAMS ((PTR, const char *, bfd_boolean));
178 static bfd_boolean pr_function_parameter
179   PARAMS ((PTR, const char *, enum debug_parm_kind, bfd_vma));
180 static bfd_boolean pr_start_block
181   PARAMS ((PTR, bfd_vma));
182 static bfd_boolean pr_end_block
183   PARAMS ((PTR, bfd_vma));
184 static bfd_boolean pr_end_function
185   PARAMS ((PTR));
186 static bfd_boolean pr_lineno
187   PARAMS ((PTR, const char *, unsigned long, bfd_vma));
188 static bfd_boolean append_parent (struct pr_handle *, const char *);
189 /* Only used by tg_ code.  */
190 static bfd_boolean tg_fix_visibility (struct pr_handle *, enum debug_visibility);
191 static void find_address_in_section (bfd *, asection *, void *);
192 static void translate_addresses (bfd *, char *, FILE *, asymbol **);
193 static const char *visibility_name (enum debug_visibility);
194 /* Tags style replacements.  */
195 static bfd_boolean tg_start_compilation_unit (void *, const char *);
196 static bfd_boolean tg_start_source (void *, const char *);
197 static bfd_boolean tg_enum_type (void *, const char *, const char **, bfd_signed_vma *);
198 static bfd_boolean tg_start_struct_type (void *, const char *, unsigned int, bfd_boolean, unsigned int);
199 static bfd_boolean pr_struct_field (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
200 static bfd_boolean tg_struct_field (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
201 static bfd_boolean tg_struct_field  (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
202 static bfd_boolean tg_end_struct_type (void *);
203 static bfd_boolean tg_start_class_type (void *, const char *, unsigned int, bfd_boolean, unsigned int, bfd_boolean, bfd_boolean);
204 static bfd_boolean tg_class_static_member (void *, const char *, const char *, enum debug_visibility);
205 static bfd_boolean tg_class_baseclass (void *, bfd_vma, bfd_boolean, enum debug_visibility);
206 static bfd_boolean tg_class_method_variant (void *, const char *, enum debug_visibility, bfd_boolean, bfd_boolean, bfd_vma, bfd_boolean);
207 static bfd_boolean tg_class_static_method_variant (void *, const char *, enum debug_visibility, bfd_boolean, bfd_boolean);
208 static bfd_boolean tg_end_class_type (void *);
209 static bfd_boolean tg_tag_type (void *, const char *, unsigned int, enum debug_type_kind);
210 static bfd_boolean tg_typdef (void *, const char *);
211 static bfd_boolean tg_tag (void *, const char *);
212 static bfd_boolean tg_int_constant (void *, const char *, bfd_vma);
213 static bfd_boolean tg_float_constant (void *, const char *, double);
214 static bfd_boolean tg_typed_constant (void *, const char *, bfd_vma);
215 static bfd_boolean tg_variable (void *, const char *, enum debug_var_kind, bfd_vma);
216 static bfd_boolean tg_start_function (void *, const char *, bfd_boolean);
217 static bfd_boolean tg_function_parameter (void *, const char *, enum debug_parm_kind, bfd_vma);
218 static bfd_boolean tg_start_block (void *, bfd_vma);
219 static bfd_boolean tg_end_block (void *, bfd_vma);
220 static bfd_boolean tg_lineno (void *, const char *, unsigned long, bfd_vma);
221 \f
222 static const struct debug_write_fns pr_fns =
223 {
224   pr_start_compilation_unit,
225   pr_start_source,
226   pr_empty_type,
227   pr_void_type,
228   pr_int_type,
229   pr_float_type,
230   pr_complex_type,
231   pr_bool_type,
232   pr_enum_type,
233   pr_pointer_type,
234   pr_function_type,
235   pr_reference_type,
236   pr_range_type,
237   pr_array_type,
238   pr_set_type,
239   pr_offset_type,
240   pr_method_type,
241   pr_const_type,
242   pr_volatile_type,
243   pr_start_struct_type,
244   pr_struct_field,
245   pr_end_struct_type,
246   pr_start_class_type,
247   pr_class_static_member,
248   pr_class_baseclass,
249   pr_class_start_method,
250   pr_class_method_variant,
251   pr_class_static_method_variant,
252   pr_class_end_method,
253   pr_end_class_type,
254   pr_typedef_type,
255   pr_tag_type,
256   pr_typdef,
257   pr_tag,
258   pr_int_constant,
259   pr_float_constant,
260   pr_typed_constant,
261   pr_variable,
262   pr_start_function,
263   pr_function_parameter,
264   pr_start_block,
265   pr_end_block,
266   pr_end_function,
267   pr_lineno
268 };
269 \f
270 static const struct debug_write_fns tg_fns =
271 {
272   tg_start_compilation_unit,
273   tg_start_source,
274   pr_empty_type,                /* Same, push_type.  */
275   pr_void_type,                 /* Same, push_type.  */
276   pr_int_type,                  /* Same, push_type.  */
277   pr_float_type,                /* Same, push_type.  */
278   pr_complex_type,              /* Same, push_type.  */
279   pr_bool_type,                 /* Same, push_type.  */
280   tg_enum_type,
281   pr_pointer_type,              /* Same, changes to pointer.  */
282   pr_function_type,             /* Same, push_type.  */
283   pr_reference_type,            /* Same, changes to reference.  */
284   pr_range_type,                /* FIXME: What's that?.  */
285   pr_array_type,                /* Same, push_type.  */
286   pr_set_type,                  /* FIXME: What's that?.  */
287   pr_offset_type,               /* FIXME: What's that?.  */
288   pr_method_type,               /* Same.  */
289   pr_const_type,                /* Same, changes to const.  */
290   pr_volatile_type,             /* Same, changes to volatile.  */
291   tg_start_struct_type,
292   tg_struct_field,
293   tg_end_struct_type,
294   tg_start_class_type,
295   tg_class_static_member,
296   tg_class_baseclass,
297   pr_class_start_method,        /* Same, remmembers that's a method.  */
298   tg_class_method_variant,
299   tg_class_static_method_variant,
300   pr_class_end_method,          /* Same, forgets that's a method.  */
301   tg_end_class_type,
302   pr_typedef_type,              /* Same, just push type.  */
303   tg_tag_type,
304   tg_typdef,
305   tg_tag,
306   tg_int_constant,              /* Untested.  */
307   tg_float_constant,            /* Untested.  */
308   tg_typed_constant,            /* Untested.  */
309   tg_variable,
310   tg_start_function,
311   tg_function_parameter,
312   tg_start_block,
313   tg_end_block,
314   pr_end_function,              /* Same, does nothing.  */
315   tg_lineno
316 };
317 \f
318 /* Print out the generic debugging information recorded in dhandle.  */
319
320 bfd_boolean
321 print_debugging_info (f, dhandle, abfd, syms, demangler, as_tags)
322      FILE *f;
323      PTR dhandle;
324      bfd *abfd;
325      asymbol **syms;
326      PTR demangler;
327      bfd_boolean as_tags;
328 {
329   struct pr_handle info;
330
331   info.f = f;
332   info.indent = 0;
333   info.stack = NULL;
334   info.parameter = 0;
335   info.filename = NULL;
336   info.abfd = abfd;
337   info.syms = syms;
338   info.demangler = demangler;
339
340   if (as_tags)
341     {
342       fputs ("!_TAG_FILE_FORMAT\t2\t/extended format/\n", f);
343       fputs ("!_TAG_FILE_SORTED\t0\t/0=unsorted, 1=sorted/\n", f);
344       fputs ("!_TAG_PROGRAM_AUTHOR\tIan Lance Taylor, Salvador E. Tropea and others\t//\n", f);
345       fputs ("!_TAG_PROGRAM_NAME\tobjdump\t/From GNU binutils/\n", f);
346     }
347
348   return as_tags ? debug_write (dhandle, &tg_fns, (void *) & info)
349     : debug_write (dhandle, &pr_fns, (void *) & info);
350 }
351 \f
352 /* Indent to the current indentation level.  */
353
354 static void
355 indent (info)
356      struct pr_handle *info;
357 {
358   unsigned int i;
359
360   for (i = 0; i < info->indent; i++)
361     putc (' ', info->f);
362 }
363
364 /* Push a type on the type stack.  */
365
366 static bfd_boolean
367 push_type (info, type)
368      struct pr_handle *info;
369      const char *type;
370 {
371   struct pr_stack *n;
372
373   if (type == NULL)
374     return FALSE;
375
376   n = (struct pr_stack *) xmalloc (sizeof *n);
377   memset (n, 0, sizeof *n);
378
379   n->type = xstrdup (type);
380   n->visibility = DEBUG_VISIBILITY_IGNORE;
381   n->method = NULL;
382   n->next = info->stack;
383   info->stack = n;
384
385   return TRUE;
386 }
387
388 /* Prepend a string onto the type on the top of the type stack.  */
389
390 static bfd_boolean
391 prepend_type (info, s)
392      struct pr_handle *info;
393      const char *s;
394 {
395   char *n;
396
397   assert (info->stack != NULL);
398
399   n = (char *) xmalloc (strlen (s) + strlen (info->stack->type) + 1);
400   sprintf (n, "%s%s", s, info->stack->type);
401   free (info->stack->type);
402   info->stack->type = n;
403
404   return TRUE;
405 }
406
407 /* Append a string to the type on the top of the type stack.  */
408
409 static bfd_boolean
410 append_type (info, s)
411      struct pr_handle *info;
412      const char *s;
413 {
414   unsigned int len;
415
416   if (s == NULL)
417     return FALSE;
418
419   assert (info->stack != NULL);
420
421   len = strlen (info->stack->type);
422   info->stack->type = (char *) xrealloc (info->stack->type,
423                                          len + strlen (s) + 1);
424   strcpy (info->stack->type + len, s);
425
426   return TRUE;
427 }
428
429 /* Append a string to the parents on the top of the type stack.  */
430
431 static bfd_boolean
432 append_parent (struct pr_handle *info, const char *s)
433 {
434   unsigned int len;
435
436   if (s == NULL)
437     return FALSE;
438
439   assert (info->stack != NULL);
440
441   len = info->stack->parents ? strlen (info->stack->parents) : 0;
442   info->stack->parents = (char *) xrealloc (info->stack->parents,
443                                             len + strlen (s) + 1);
444   strcpy (info->stack->parents + len, s);
445
446   return TRUE;
447 }
448
449 /* We use an underscore to indicate where the name should go in a type
450    string.  This function substitutes a string for the underscore.  If
451    there is no underscore, the name follows the type.  */
452
453 static bfd_boolean
454 substitute_type (info, s)
455      struct pr_handle *info;
456      const char *s;
457 {
458   char *u;
459
460   assert (info->stack != NULL);
461
462   u = strchr (info->stack->type, '|');
463   if (u != NULL)
464     {
465       char *n;
466
467       n = (char *) xmalloc (strlen (info->stack->type) + strlen (s));
468
469       memcpy (n, info->stack->type, u - info->stack->type);
470       strcpy (n + (u - info->stack->type), s);
471       strcat (n, u + 1);
472
473       free (info->stack->type);
474       info->stack->type = n;
475
476       return TRUE;
477     }
478
479   if (strchr (s, '|') != NULL
480       && (strchr (info->stack->type, '{') != NULL
481           || strchr (info->stack->type, '(') != NULL))
482     {
483       if (! prepend_type (info, "(")
484           || ! append_type (info, ")"))
485         return FALSE;
486     }
487
488   if (*s == '\0')
489     return TRUE;
490
491   return (append_type (info, " ")
492           && append_type (info, s));
493 }
494
495 /* Indent the type at the top of the stack by appending spaces.  */
496
497 static bfd_boolean
498 indent_type (info)
499      struct pr_handle *info;
500 {
501   unsigned int i;
502
503   for (i = 0; i < info->indent; i++)
504     {
505       if (! append_type (info, " "))
506         return FALSE;
507     }
508
509   return TRUE;
510 }
511
512 /* Pop a type from the type stack.  */
513
514 static char *
515 pop_type (info)
516      struct pr_handle *info;
517 {
518   struct pr_stack *o;
519   char *ret;
520
521   assert (info->stack != NULL);
522
523   o = info->stack;
524   info->stack = o->next;
525   ret = o->type;
526   free (o);
527
528   return ret;
529 }
530
531 /* Print a VMA value into a string.  */
532
533 static void
534 print_vma (vma, buf, unsignedp, hexp)
535      bfd_vma vma;
536      char *buf;
537      bfd_boolean unsignedp;
538      bfd_boolean hexp;
539 {
540   if (sizeof (vma) <= sizeof (unsigned long))
541     {
542       if (hexp)
543         sprintf (buf, "0x%lx", (unsigned long) vma);
544       else if (unsignedp)
545         sprintf (buf, "%lu", (unsigned long) vma);
546       else
547         sprintf (buf, "%ld", (long) vma);
548     }
549   else
550     {
551       buf[0] = '0';
552       buf[1] = 'x';
553       sprintf_vma (buf + 2, vma);
554     }
555 }
556 \f
557 /* Start a new compilation unit.  */
558
559 static bfd_boolean
560 pr_start_compilation_unit (p, filename)
561      PTR p;
562      const char *filename;
563 {
564   struct pr_handle *info = (struct pr_handle *) p;
565
566   assert (info->indent == 0);
567
568   fprintf (info->f, "%s:\n", filename);
569
570   return TRUE;
571 }
572
573 /* Start a source file within a compilation unit.  */
574
575 static bfd_boolean
576 pr_start_source (p, filename)
577      PTR p;
578      const char *filename;
579 {
580   struct pr_handle *info = (struct pr_handle *) p;
581
582   assert (info->indent == 0);
583
584   fprintf (info->f, " %s:\n", filename);
585
586   return TRUE;
587 }
588
589 /* Push an empty type onto the type stack.  */
590
591 static bfd_boolean
592 pr_empty_type (p)
593      PTR p;
594 {
595   struct pr_handle *info = (struct pr_handle *) p;
596
597   return push_type (info, "<undefined>");
598 }
599
600 /* Push a void type onto the type stack.  */
601
602 static bfd_boolean
603 pr_void_type (p)
604      PTR p;
605 {
606   struct pr_handle *info = (struct pr_handle *) p;
607
608   return push_type (info, "void");
609 }
610
611 /* Push an integer type onto the type stack.  */
612
613 static bfd_boolean
614 pr_int_type (p, size, unsignedp)
615      PTR p;
616      unsigned int size;
617      bfd_boolean unsignedp;
618 {
619   struct pr_handle *info = (struct pr_handle *) p;
620   char ab[10];
621
622   sprintf (ab, "%sint%d", unsignedp ? "u" : "", size * 8);
623   return push_type (info, ab);
624 }
625
626 /* Push a floating type onto the type stack.  */
627
628 static bfd_boolean
629 pr_float_type (p, size)
630      PTR p;
631      unsigned int size;
632 {
633   struct pr_handle *info = (struct pr_handle *) p;
634   char ab[10];
635
636   if (size == 4)
637     return push_type (info, "float");
638   else if (size == 8)
639     return push_type (info, "double");
640
641   sprintf (ab, "float%d", size * 8);
642   return push_type (info, ab);
643 }
644
645 /* Push a complex type onto the type stack.  */
646
647 static bfd_boolean
648 pr_complex_type (p, size)
649      PTR p;
650      unsigned int size;
651 {
652   struct pr_handle *info = (struct pr_handle *) p;
653
654   if (! pr_float_type (p, size))
655     return FALSE;
656
657   return prepend_type (info, "complex ");
658 }
659
660 /* Push a bfd_boolean type onto the type stack.  */
661
662 static bfd_boolean
663 pr_bool_type (p, size)
664      PTR p;
665      unsigned int size;
666 {
667   struct pr_handle *info = (struct pr_handle *) p;
668   char ab[10];
669
670   sprintf (ab, "bool%d", size * 8);
671
672   return push_type (info, ab);
673 }
674
675 /* Push an enum type onto the type stack.  */
676
677 static bfd_boolean
678 pr_enum_type (p, tag, names, values)
679      PTR p;
680      const char *tag;
681      const char **names;
682      bfd_signed_vma *values;
683 {
684   struct pr_handle *info = (struct pr_handle *) p;
685   unsigned int i;
686   bfd_signed_vma val;
687
688   if (! push_type (info, "enum "))
689     return FALSE;
690   if (tag != NULL)
691     {
692       if (! append_type (info, tag)
693           || ! append_type (info, " "))
694         return FALSE;
695     }
696   if (! append_type (info, "{ "))
697     return FALSE;
698
699   if (names == NULL)
700     {
701       if (! append_type (info, "/* undefined */"))
702         return FALSE;
703     }
704   else
705     {
706       val = 0;
707       for (i = 0; names[i] != NULL; i++)
708         {
709           if (i > 0)
710             {
711               if (! append_type (info, ", "))
712                 return FALSE;
713             }
714
715           if (! append_type (info, names[i]))
716             return FALSE;
717
718           if (values[i] != val)
719             {
720               char ab[20];
721
722               print_vma (values[i], ab, FALSE, FALSE);
723               if (! append_type (info, " = ")
724                   || ! append_type (info, ab))
725                 return FALSE;
726               val = values[i];
727             }
728
729           ++val;
730         }
731     }
732
733   return append_type (info, " }");
734 }
735
736 /* Turn the top type on the stack into a pointer.  */
737
738 static bfd_boolean
739 pr_pointer_type (p)
740      PTR p;
741 {
742   struct pr_handle *info = (struct pr_handle *) p;
743   char *s;
744
745   assert (info->stack != NULL);
746
747   s = strchr (info->stack->type, '|');
748   if (s != NULL && s[1] == '[')
749     return substitute_type (info, "(*|)");
750   return substitute_type (info, "*|");
751 }
752
753 /* Turn the top type on the stack into a function returning that type.  */
754
755 static bfd_boolean
756 pr_function_type (p, argcount, varargs)
757      PTR p;
758      int argcount;
759      bfd_boolean varargs;
760 {
761   struct pr_handle *info = (struct pr_handle *) p;
762   char **arg_types;
763   unsigned int len;
764   char *s;
765
766   assert (info->stack != NULL);
767
768   len = 10;
769
770   if (argcount <= 0)
771     {
772       arg_types = NULL;
773       len += 15;
774     }
775   else
776     {
777       int i;
778
779       arg_types = (char **) xmalloc (argcount * sizeof *arg_types);
780       for (i = argcount - 1; i >= 0; i--)
781         {
782           if (! substitute_type (info, ""))
783             return FALSE;
784           arg_types[i] = pop_type (info);
785           if (arg_types[i] == NULL)
786             return FALSE;
787           len += strlen (arg_types[i]) + 2;
788         }
789       if (varargs)
790         len += 5;
791     }
792
793   /* Now the return type is on the top of the stack.  */
794
795   s = (char *) xmalloc (len);
796   strcpy (s, "(|) (");
797
798   if (argcount < 0)
799     strcat (s, "/* unknown */");
800   else
801     {
802       int i;
803
804       for (i = 0; i < argcount; i++)
805         {
806           if (i > 0)
807             strcat (s, ", ");
808           strcat (s, arg_types[i]);
809         }
810       if (varargs)
811         {
812           if (i > 0)
813             strcat (s, ", ");
814           strcat (s, "...");
815         }
816       if (argcount > 0)
817         free (arg_types);
818     }
819
820   strcat (s, ")");
821
822   if (! substitute_type (info, s))
823     return FALSE;
824
825   free (s);
826
827   return TRUE;
828 }
829
830 /* Turn the top type on the stack into a reference to that type.  */
831
832 static bfd_boolean
833 pr_reference_type (p)
834      PTR p;
835 {
836   struct pr_handle *info = (struct pr_handle *) p;
837
838   assert (info->stack != NULL);
839
840   return substitute_type (info, "&|");
841 }
842
843 /* Make a range type.  */
844
845 static bfd_boolean
846 pr_range_type (p, lower, upper)
847      PTR p;
848      bfd_signed_vma lower;
849      bfd_signed_vma upper;
850 {
851   struct pr_handle *info = (struct pr_handle *) p;
852   char abl[20], abu[20];
853
854   assert (info->stack != NULL);
855
856   if (! substitute_type (info, ""))
857     return FALSE;
858
859   print_vma (lower, abl, FALSE, FALSE);
860   print_vma (upper, abu, FALSE, FALSE);
861
862   return (prepend_type (info, "range (")
863           && append_type (info, "):")
864           && append_type (info, abl)
865           && append_type (info, ":")
866           && append_type (info, abu));
867 }
868
869 /* Make an array type.  */
870
871 static bfd_boolean
872 pr_array_type (p, lower, upper, stringp)
873      PTR p;
874      bfd_signed_vma lower;
875      bfd_signed_vma upper;
876      bfd_boolean stringp;
877 {
878   struct pr_handle *info = (struct pr_handle *) p;
879   char *range_type;
880   char abl[20], abu[20], ab[50];
881
882   range_type = pop_type (info);
883   if (range_type == NULL)
884     return FALSE;
885
886   if (lower == 0)
887     {
888       if (upper == -1)
889         sprintf (ab, "|[]");
890       else
891         {
892           print_vma (upper + 1, abu, FALSE, FALSE);
893           sprintf (ab, "|[%s]", abu);
894         }
895     }
896   else
897     {
898       print_vma (lower, abl, FALSE, FALSE);
899       print_vma (upper, abu, FALSE, FALSE);
900       sprintf (ab, "|[%s:%s]", abl, abu);
901     }
902
903   if (! substitute_type (info, ab))
904     return FALSE;
905
906   if (strcmp (range_type, "int") != 0)
907     {
908       if (! append_type (info, ":")
909           || ! append_type (info, range_type))
910         return FALSE;
911     }
912
913   if (stringp)
914     {
915       if (! append_type (info, " /* string */"))
916         return FALSE;
917     }
918
919   return TRUE;
920 }
921
922 /* Make a set type.  */
923
924 static bfd_boolean
925 pr_set_type (p, bitstringp)
926      PTR p;
927      bfd_boolean bitstringp;
928 {
929   struct pr_handle *info = (struct pr_handle *) p;
930
931   if (! substitute_type (info, ""))
932     return FALSE;
933
934   if (! prepend_type (info, "set { ")
935       || ! append_type (info, " }"))
936     return FALSE;
937
938   if (bitstringp)
939     {
940       if (! append_type (info, "/* bitstring */"))
941         return FALSE;
942     }
943
944   return TRUE;
945 }
946
947 /* Make an offset type.  */
948
949 static bfd_boolean
950 pr_offset_type (p)
951      PTR p;
952 {
953   struct pr_handle *info = (struct pr_handle *) p;
954   char *t;
955
956   if (! substitute_type (info, ""))
957     return FALSE;
958
959   t = pop_type (info);
960   if (t == NULL)
961     return FALSE;
962
963   return (substitute_type (info, "")
964           && prepend_type (info, " ")
965           && prepend_type (info, t)
966           && append_type (info, "::|"));
967 }
968
969 /* Make a method type.  */
970
971 static bfd_boolean
972 pr_method_type (p, domain, argcount, varargs)
973      PTR p;
974      bfd_boolean domain;
975      int argcount;
976      bfd_boolean varargs;
977 {
978   struct pr_handle *info = (struct pr_handle *) p;
979   unsigned int len;
980   char *domain_type;
981   char **arg_types;
982   char *s;
983
984   len = 10;
985
986   if (! domain)
987     domain_type = NULL;
988   else
989     {
990       if (! substitute_type (info, ""))
991         return FALSE;
992       domain_type = pop_type (info);
993       if (domain_type == NULL)
994         return FALSE;
995       if (strncmp (domain_type, "class ", sizeof "class " - 1) == 0
996           && strchr (domain_type + sizeof "class " - 1, ' ') == NULL)
997         domain_type += sizeof "class " - 1;
998       else if (strncmp (domain_type, "union class ",
999                         sizeof "union class ") == 0
1000                && (strchr (domain_type + sizeof "union class " - 1, ' ')
1001                    == NULL))
1002         domain_type += sizeof "union class " - 1;
1003       len += strlen (domain_type);
1004     }
1005
1006   if (argcount <= 0)
1007     {
1008       arg_types = NULL;
1009       len += 15;
1010     }
1011   else
1012     {
1013       int i;
1014
1015       arg_types = (char **) xmalloc (argcount * sizeof *arg_types);
1016       for (i = argcount - 1; i >= 0; i--)
1017         {
1018           if (! substitute_type (info, ""))
1019             return FALSE;
1020           arg_types[i] = pop_type (info);
1021           if (arg_types[i] == NULL)
1022             return FALSE;
1023           len += strlen (arg_types[i]) + 2;
1024         }
1025       if (varargs)
1026         len += 5;
1027     }
1028
1029   /* Now the return type is on the top of the stack.  */
1030
1031   s = (char *) xmalloc (len);
1032   if (! domain)
1033     *s = '\0';
1034   else
1035     strcpy (s, domain_type);
1036   strcat (s, "::| (");
1037
1038   if (argcount < 0)
1039     strcat (s, "/* unknown */");
1040   else
1041     {
1042       int i;
1043
1044       for (i = 0; i < argcount; i++)
1045         {
1046           if (i > 0)
1047             strcat (s, ", ");
1048           strcat (s, arg_types[i]);
1049         }
1050       if (varargs)
1051         {
1052           if (i > 0)
1053             strcat (s, ", ");
1054           strcat (s, "...");
1055         }
1056       if (argcount > 0)
1057         free (arg_types);
1058     }
1059
1060   strcat (s, ")");
1061
1062   if (! substitute_type (info, s))
1063     return FALSE;
1064
1065   free (s);
1066
1067   return TRUE;
1068 }
1069
1070 /* Make a const qualified type.  */
1071
1072 static bfd_boolean
1073 pr_const_type (p)
1074      PTR p;
1075 {
1076   struct pr_handle *info = (struct pr_handle *) p;
1077
1078   return substitute_type (info, "const |");
1079 }
1080
1081 /* Make a volatile qualified type.  */
1082
1083 static bfd_boolean
1084 pr_volatile_type (p)
1085      PTR p;
1086 {
1087   struct pr_handle *info = (struct pr_handle *) p;
1088
1089   return substitute_type (info, "volatile |");
1090 }
1091
1092 /* Start accumulating a struct type.  */
1093
1094 static bfd_boolean
1095 pr_start_struct_type (p, tag, id, structp, size)
1096      PTR p;
1097      const char *tag;
1098      unsigned int id;
1099      bfd_boolean structp;
1100      unsigned int size;
1101 {
1102   struct pr_handle *info = (struct pr_handle *) p;
1103
1104   info->indent += 2;
1105
1106   if (! push_type (info, structp ? "struct " : "union "))
1107     return FALSE;
1108   if (tag != NULL)
1109     {
1110       if (! append_type (info, tag))
1111         return FALSE;
1112     }
1113   else
1114     {
1115       char idbuf[20];
1116
1117       sprintf (idbuf, "%%anon%u", id);
1118       if (! append_type (info, idbuf))
1119         return FALSE;
1120     }
1121
1122   if (! append_type (info, " {"))
1123     return FALSE;
1124   if (size != 0 || tag != NULL)
1125     {
1126       char ab[30];
1127
1128       if (! append_type (info, " /*"))
1129         return FALSE;
1130
1131       if (size != 0)
1132         {
1133           sprintf (ab, " size %u", size);
1134           if (! append_type (info, ab))
1135             return FALSE;
1136         }
1137       if (tag != NULL)
1138         {
1139           sprintf (ab, " id %u", id);
1140           if (! append_type (info, ab))
1141             return FALSE;
1142         }
1143       if (! append_type (info, " */"))
1144         return FALSE;
1145     }
1146   if (! append_type (info, "\n"))
1147     return FALSE;
1148
1149   info->stack->visibility = DEBUG_VISIBILITY_PUBLIC;
1150
1151   return indent_type (info);
1152 }
1153
1154 /* Output the visibility of a field in a struct.  */
1155
1156 static bfd_boolean
1157 pr_fix_visibility (info, visibility)
1158      struct pr_handle *info;
1159      enum debug_visibility visibility;
1160 {
1161   const char *s = NULL;
1162   char *t;
1163   unsigned int len;
1164
1165   assert (info->stack != NULL);
1166
1167   if (info->stack->visibility == visibility)
1168     return TRUE;
1169
1170   switch (visibility)
1171     {
1172     case DEBUG_VISIBILITY_PUBLIC:
1173       s = "public";
1174       break;
1175     case DEBUG_VISIBILITY_PRIVATE:
1176       s = "private";
1177       break;
1178     case DEBUG_VISIBILITY_PROTECTED:
1179       s = "protected";
1180       break;
1181     case DEBUG_VISIBILITY_IGNORE:
1182       s = "/* ignore */";
1183       break;
1184     default:
1185       abort ();
1186       return FALSE;
1187     }
1188
1189   /* Trim off a trailing space in the struct string, to make the
1190      output look a bit better, then stick on the visibility string.  */
1191
1192   t = info->stack->type;
1193   len = strlen (t);
1194   assert (t[len - 1] == ' ');
1195   t[len - 1] = '\0';
1196
1197   if (! append_type (info, s)
1198       || ! append_type (info, ":\n")
1199       || ! indent_type (info))
1200     return FALSE;
1201
1202   info->stack->visibility = visibility;
1203
1204   return TRUE;
1205 }
1206
1207 /* Add a field to a struct type.  */
1208
1209 static bfd_boolean
1210 pr_struct_field (p, name, bitpos, bitsize, visibility)
1211      PTR p;
1212      const char *name;
1213      bfd_vma bitpos;
1214      bfd_vma bitsize;
1215      enum debug_visibility visibility;
1216 {
1217   struct pr_handle *info = (struct pr_handle *) p;
1218   char ab[20];
1219   char *t;
1220
1221   if (! substitute_type (info, name))
1222     return FALSE;
1223
1224   if (! append_type (info, "; /* "))
1225     return FALSE;
1226
1227   if (bitsize != 0)
1228     {
1229       print_vma (bitsize, ab, TRUE, FALSE);
1230       if (! append_type (info, "bitsize ")
1231           || ! append_type (info, ab)
1232           || ! append_type (info, ", "))
1233         return FALSE;
1234     }
1235
1236   print_vma (bitpos, ab, TRUE, FALSE);
1237   if (! append_type (info, "bitpos ")
1238       || ! append_type (info, ab)
1239       || ! append_type (info, " */\n")
1240       || ! indent_type (info))
1241     return FALSE;
1242
1243   t = pop_type (info);
1244   if (t == NULL)
1245     return FALSE;
1246
1247   if (! pr_fix_visibility (info, visibility))
1248     return FALSE;
1249
1250   return append_type (info, t);
1251 }
1252
1253 /* Finish a struct type.  */
1254
1255 static bfd_boolean
1256 pr_end_struct_type (p)
1257      PTR p;
1258 {
1259   struct pr_handle *info = (struct pr_handle *) p;
1260   char *s;
1261
1262   assert (info->stack != NULL);
1263   assert (info->indent >= 2);
1264
1265   info->indent -= 2;
1266
1267   /* Change the trailing indentation to have a close brace.  */
1268   s = info->stack->type + strlen (info->stack->type) - 2;
1269   assert (s[0] == ' ' && s[1] == ' ' && s[2] == '\0');
1270
1271   *s++ = '}';
1272   *s = '\0';
1273
1274   return TRUE;
1275 }
1276
1277 /* Start a class type.  */
1278
1279 static bfd_boolean
1280 pr_start_class_type (p, tag, id, structp, size, vptr, ownvptr)
1281      PTR p;
1282      const char *tag;
1283      unsigned int id;
1284      bfd_boolean structp;
1285      unsigned int size;
1286      bfd_boolean vptr;
1287      bfd_boolean ownvptr;
1288 {
1289   struct pr_handle *info = (struct pr_handle *) p;
1290   char *tv = NULL;
1291
1292   info->indent += 2;
1293
1294   if (vptr && ! ownvptr)
1295     {
1296       tv = pop_type (info);
1297       if (tv == NULL)
1298         return FALSE;
1299     }
1300
1301   if (! push_type (info, structp ? "class " : "union class "))
1302     return FALSE;
1303   if (tag != NULL)
1304     {
1305       if (! append_type (info, tag))
1306         return FALSE;
1307     }
1308   else
1309     {
1310       char idbuf[20];
1311
1312       sprintf (idbuf, "%%anon%u", id);
1313       if (! append_type (info, idbuf))
1314         return FALSE;
1315     }
1316
1317   if (! append_type (info, " {"))
1318     return FALSE;
1319   if (size != 0 || vptr || ownvptr || tag != NULL)
1320     {
1321       if (! append_type (info, " /*"))
1322         return FALSE;
1323
1324       if (size != 0)
1325         {
1326           char ab[20];
1327
1328           sprintf (ab, "%u", size);
1329           if (! append_type (info, " size ")
1330               || ! append_type (info, ab))
1331             return FALSE;
1332         }
1333
1334       if (vptr)
1335         {
1336           if (! append_type (info, " vtable "))
1337             return FALSE;
1338           if (ownvptr)
1339             {
1340               if (! append_type (info, "self "))
1341                 return FALSE;
1342             }
1343           else
1344             {
1345               if (! append_type (info, tv)
1346                   || ! append_type (info, " "))
1347                 return FALSE;
1348             }
1349         }
1350
1351       if (tag != NULL)
1352         {
1353           char ab[30];
1354
1355           sprintf (ab, " id %u", id);
1356           if (! append_type (info, ab))
1357             return FALSE;
1358         }
1359
1360       if (! append_type (info, " */"))
1361         return FALSE;
1362     }
1363
1364   info->stack->visibility = DEBUG_VISIBILITY_PRIVATE;
1365
1366   return (append_type (info, "\n")
1367           && indent_type (info));
1368 }
1369
1370 /* Add a static member to a class.  */
1371
1372 static bfd_boolean
1373 pr_class_static_member (p, name, physname, visibility)
1374      PTR p;
1375      const char *name;
1376      const char *physname;
1377      enum debug_visibility visibility;
1378 {
1379   struct pr_handle *info = (struct pr_handle *) p;
1380   char *t;
1381
1382   if (! substitute_type (info, name))
1383     return FALSE;
1384
1385   if (! prepend_type (info, "static ")
1386       || ! append_type (info, "; /* ")
1387       || ! append_type (info, physname)
1388       || ! append_type (info, " */\n")
1389       || ! indent_type (info))
1390     return FALSE;
1391
1392   t = pop_type (info);
1393   if (t == NULL)
1394     return FALSE;
1395
1396   if (! pr_fix_visibility (info, visibility))
1397     return FALSE;
1398
1399   return append_type (info, t);
1400 }
1401
1402 /* Add a base class to a class.  */
1403
1404 static bfd_boolean
1405 pr_class_baseclass (p, bitpos, virtual, visibility)
1406      PTR p;
1407      bfd_vma bitpos;
1408      bfd_boolean virtual;
1409      enum debug_visibility visibility;
1410 {
1411   struct pr_handle *info = (struct pr_handle *) p;
1412   char *t;
1413   const char *prefix;
1414   char ab[20];
1415   char *s, *l, *n;
1416
1417   assert (info->stack != NULL && info->stack->next != NULL);
1418
1419   if (! substitute_type (info, ""))
1420     return FALSE;
1421
1422   t = pop_type (info);
1423   if (t == NULL)
1424     return FALSE;
1425
1426   if (strncmp (t, "class ", sizeof "class " - 1) == 0)
1427     t += sizeof "class " - 1;
1428
1429   /* Push it back on to take advantage of the prepend_type and
1430      append_type routines.  */
1431   if (! push_type (info, t))
1432     return FALSE;
1433
1434   if (virtual)
1435     {
1436       if (! prepend_type (info, "virtual "))
1437         return FALSE;
1438     }
1439
1440   switch (visibility)
1441     {
1442     case DEBUG_VISIBILITY_PUBLIC:
1443       prefix = "public ";
1444       break;
1445     case DEBUG_VISIBILITY_PROTECTED:
1446       prefix = "protected ";
1447       break;
1448     case DEBUG_VISIBILITY_PRIVATE:
1449       prefix = "private ";
1450       break;
1451     default:
1452       prefix = "/* unknown visibility */ ";
1453       break;
1454     }
1455
1456   if (! prepend_type (info, prefix))
1457     return FALSE;
1458
1459   if (bitpos != 0)
1460     {
1461       print_vma (bitpos, ab, TRUE, FALSE);
1462       if (! append_type (info, " /* bitpos ")
1463           || ! append_type (info, ab)
1464           || ! append_type (info, " */"))
1465         return FALSE;
1466     }
1467
1468   /* Now the top of the stack is something like "public A / * bitpos
1469      10 * /".  The next element on the stack is something like "class
1470      xx { / * size 8 * /\n...".  We want to substitute the top of the
1471      stack in before the {.  */
1472   s = strchr (info->stack->next->type, '{');
1473   assert (s != NULL);
1474   --s;
1475
1476   /* If there is already a ':', then we already have a baseclass, and
1477      we must append this one after a comma.  */
1478   for (l = info->stack->next->type; l != s; l++)
1479     if (*l == ':')
1480       break;
1481   if (! prepend_type (info, l == s ? " : " : ", "))
1482     return FALSE;
1483
1484   t = pop_type (info);
1485   if (t == NULL)
1486     return FALSE;
1487
1488   n = (char *) xmalloc (strlen (info->stack->type) + strlen (t) + 1);
1489   memcpy (n, info->stack->type, s - info->stack->type);
1490   strcpy (n + (s - info->stack->type), t);
1491   strcat (n, s);
1492
1493   free (info->stack->type);
1494   info->stack->type = n;
1495
1496   free (t);
1497
1498   return TRUE;
1499 }
1500
1501 /* Start adding a method to a class.  */
1502
1503 static bfd_boolean
1504 pr_class_start_method (p, name)
1505      PTR p;
1506      const char *name;
1507 {
1508   struct pr_handle *info = (struct pr_handle *) p;
1509
1510   assert (info->stack != NULL);
1511   info->stack->method = name;
1512   return TRUE;
1513 }
1514
1515 /* Add a variant to a method.  */
1516
1517 static bfd_boolean
1518 pr_class_method_variant (p, physname, visibility, constp, volatilep, voffset,
1519                          context)
1520      PTR p;
1521      const char *physname;
1522      enum debug_visibility visibility;
1523      bfd_boolean constp;
1524      bfd_boolean volatilep;
1525      bfd_vma voffset;
1526      bfd_boolean context;
1527 {
1528   struct pr_handle *info = (struct pr_handle *) p;
1529   char *method_type;
1530   char *context_type;
1531
1532   assert (info->stack != NULL);
1533   assert (info->stack->next != NULL);
1534
1535   /* Put the const and volatile qualifiers on the type.  */
1536   if (volatilep)
1537     {
1538       if (! append_type (info, " volatile"))
1539         return FALSE;
1540     }
1541   if (constp)
1542     {
1543       if (! append_type (info, " const"))
1544         return FALSE;
1545     }
1546
1547   /* Stick the name of the method into its type.  */
1548   if (! substitute_type (info,
1549                          (context
1550                           ? info->stack->next->next->method
1551                           : info->stack->next->method)))
1552     return FALSE;
1553
1554   /* Get the type.  */
1555   method_type = pop_type (info);
1556   if (method_type == NULL)
1557     return FALSE;
1558
1559   /* Pull off the context type if there is one.  */
1560   if (! context)
1561     context_type = NULL;
1562   else
1563     {
1564       context_type = pop_type (info);
1565       if (context_type == NULL)
1566         return FALSE;
1567     }
1568
1569   /* Now the top of the stack is the class.  */
1570
1571   if (! pr_fix_visibility (info, visibility))
1572     return FALSE;
1573
1574   if (! append_type (info, method_type)
1575       || ! append_type (info, " /* ")
1576       || ! append_type (info, physname)
1577       || ! append_type (info, " "))
1578     return FALSE;
1579   if (context || voffset != 0)
1580     {
1581       char ab[20];
1582
1583       if (context)
1584         {
1585           if (! append_type (info, "context ")
1586               || ! append_type (info, context_type)
1587               || ! append_type (info, " "))
1588             return FALSE;
1589         }
1590       print_vma (voffset, ab, TRUE, FALSE);
1591       if (! append_type (info, "voffset ")
1592           || ! append_type (info, ab))
1593         return FALSE;
1594     }
1595
1596   return (append_type (info, " */;\n")
1597           && indent_type (info));
1598 }
1599
1600 /* Add a static variant to a method.  */
1601
1602 static bfd_boolean
1603 pr_class_static_method_variant (p, physname, visibility, constp, volatilep)
1604      PTR p;
1605      const char *physname;
1606      enum debug_visibility visibility;
1607      bfd_boolean constp;
1608      bfd_boolean volatilep;
1609 {
1610   struct pr_handle *info = (struct pr_handle *) p;
1611   char *method_type;
1612
1613   assert (info->stack != NULL);
1614   assert (info->stack->next != NULL);
1615   assert (info->stack->next->method != NULL);
1616
1617   /* Put the const and volatile qualifiers on the type.  */
1618   if (volatilep)
1619     {
1620       if (! append_type (info, " volatile"))
1621         return FALSE;
1622     }
1623   if (constp)
1624     {
1625       if (! append_type (info, " const"))
1626         return FALSE;
1627     }
1628
1629   /* Mark it as static.  */
1630   if (! prepend_type (info, "static "))
1631     return FALSE;
1632
1633   /* Stick the name of the method into its type.  */
1634   if (! substitute_type (info, info->stack->next->method))
1635     return FALSE;
1636
1637   /* Get the type.  */
1638   method_type = pop_type (info);
1639   if (method_type == NULL)
1640     return FALSE;
1641
1642   /* Now the top of the stack is the class.  */
1643
1644   if (! pr_fix_visibility (info, visibility))
1645     return FALSE;
1646
1647   return (append_type (info, method_type)
1648           && append_type (info, " /* ")
1649           && append_type (info, physname)
1650           && append_type (info, " */;\n")
1651           && indent_type (info));
1652 }
1653
1654 /* Finish up a method.  */
1655
1656 static bfd_boolean
1657 pr_class_end_method (p)
1658      PTR p;
1659 {
1660   struct pr_handle *info = (struct pr_handle *) p;
1661
1662   info->stack->method = NULL;
1663   return TRUE;
1664 }
1665
1666 /* Finish up a class.  */
1667
1668 static bfd_boolean
1669 pr_end_class_type (p)
1670      PTR p;
1671 {
1672   return pr_end_struct_type (p);
1673 }
1674
1675 /* Push a type on the stack using a typedef name.  */
1676
1677 static bfd_boolean
1678 pr_typedef_type (p, name)
1679      PTR p;
1680      const char *name;
1681 {
1682   struct pr_handle *info = (struct pr_handle *) p;
1683
1684   return push_type (info, name);
1685 }
1686
1687 /* Push a type on the stack using a tag name.  */
1688
1689 static bfd_boolean
1690 pr_tag_type (p, name, id, kind)
1691      PTR p;
1692      const char *name;
1693      unsigned int id;
1694      enum debug_type_kind kind;
1695 {
1696   struct pr_handle *info = (struct pr_handle *) p;
1697   const char *t, *tag;
1698   char idbuf[20];
1699
1700   switch (kind)
1701     {
1702     case DEBUG_KIND_STRUCT:
1703       t = "struct ";
1704       break;
1705     case DEBUG_KIND_UNION:
1706       t = "union ";
1707       break;
1708     case DEBUG_KIND_ENUM:
1709       t = "enum ";
1710       break;
1711     case DEBUG_KIND_CLASS:
1712       t = "class ";
1713       break;
1714     case DEBUG_KIND_UNION_CLASS:
1715       t = "union class ";
1716       break;
1717     default:
1718       abort ();
1719       return FALSE;
1720     }
1721
1722   if (! push_type (info, t))
1723     return FALSE;
1724   if (name != NULL)
1725     tag = name;
1726   else
1727     {
1728       sprintf (idbuf, "%%anon%u", id);
1729       tag = idbuf;
1730     }
1731
1732   if (! append_type (info, tag))
1733     return FALSE;
1734   if (name != NULL && kind != DEBUG_KIND_ENUM)
1735     {
1736       sprintf (idbuf, " /* id %u */", id);
1737       if (! append_type (info, idbuf))
1738         return FALSE;
1739     }
1740
1741   return TRUE;
1742 }
1743
1744 /* Output a typedef.  */
1745
1746 static bfd_boolean
1747 pr_typdef (p, name)
1748      PTR p;
1749      const char *name;
1750 {
1751   struct pr_handle *info = (struct pr_handle *) p;
1752   char *s;
1753
1754   if (! substitute_type (info, name))
1755     return FALSE;
1756
1757   s = pop_type (info);
1758   if (s == NULL)
1759     return FALSE;
1760
1761   indent (info);
1762   fprintf (info->f, "typedef %s;\n", s);
1763
1764   free (s);
1765
1766   return TRUE;
1767 }
1768
1769 /* Output a tag.  The tag should already be in the string on the
1770    stack, so all we have to do here is print it out.  */
1771
1772 static bfd_boolean
1773 pr_tag (p, name)
1774      PTR p;
1775      const char *name ATTRIBUTE_UNUSED;
1776 {
1777   struct pr_handle *info = (struct pr_handle *) p;
1778   char *t;
1779
1780   t = pop_type (info);
1781   if (t == NULL)
1782     return FALSE;
1783
1784   indent (info);
1785   fprintf (info->f, "%s;\n", t);
1786
1787   free (t);
1788
1789   return TRUE;
1790 }
1791
1792 /* Output an integer constant.  */
1793
1794 static bfd_boolean
1795 pr_int_constant (p, name, val)
1796      PTR p;
1797      const char *name;
1798      bfd_vma val;
1799 {
1800   struct pr_handle *info = (struct pr_handle *) p;
1801   char ab[20];
1802
1803   indent (info);
1804   print_vma (val, ab, FALSE, FALSE);
1805   fprintf (info->f, "const int %s = %s;\n", name, ab);
1806   return TRUE;
1807 }
1808
1809 /* Output a floating point constant.  */
1810
1811 static bfd_boolean
1812 pr_float_constant (p, name, val)
1813      PTR p;
1814      const char *name;
1815      double val;
1816 {
1817   struct pr_handle *info = (struct pr_handle *) p;
1818
1819   indent (info);
1820   fprintf (info->f, "const double %s = %g;\n", name, val);
1821   return TRUE;
1822 }
1823
1824 /* Output a typed constant.  */
1825
1826 static bfd_boolean
1827 pr_typed_constant (p, name, val)
1828      PTR p;
1829      const char *name;
1830      bfd_vma val;
1831 {
1832   struct pr_handle *info = (struct pr_handle *) p;
1833   char *t;
1834   char ab[20];
1835
1836   t = pop_type (info);
1837   if (t == NULL)
1838     return FALSE;
1839
1840   indent (info);
1841   print_vma (val, ab, FALSE, FALSE);
1842   fprintf (info->f, "const %s %s = %s;\n", t, name, ab);
1843
1844   free (t);
1845
1846   return TRUE;
1847 }
1848
1849 /* Output a variable.  */
1850
1851 static bfd_boolean
1852 pr_variable (p, name, kind, val)
1853      PTR p;
1854      const char *name;
1855      enum debug_var_kind kind;
1856      bfd_vma val;
1857 {
1858   struct pr_handle *info = (struct pr_handle *) p;
1859   char *t;
1860   char ab[20];
1861
1862   if (! substitute_type (info, name))
1863     return FALSE;
1864
1865   t = pop_type (info);
1866   if (t == NULL)
1867     return FALSE;
1868
1869   indent (info);
1870   switch (kind)
1871     {
1872     case DEBUG_STATIC:
1873     case DEBUG_LOCAL_STATIC:
1874       fprintf (info->f, "static ");
1875       break;
1876     case DEBUG_REGISTER:
1877       fprintf (info->f, "register ");
1878       break;
1879     default:
1880       break;
1881     }
1882   print_vma (val, ab, TRUE, TRUE);
1883   fprintf (info->f, "%s /* %s */;\n", t, ab);
1884
1885   free (t);
1886
1887   return TRUE;
1888 }
1889
1890 /* Start outputting a function.  */
1891
1892 static bfd_boolean
1893 pr_start_function (p, name, global)
1894      PTR p;
1895      const char *name;
1896      bfd_boolean global;
1897 {
1898   struct pr_handle *info = (struct pr_handle *) p;
1899   char *t;
1900
1901   if (! substitute_type (info, name))
1902     return FALSE;
1903
1904   t = pop_type (info);
1905   if (t == NULL)
1906     return FALSE;
1907
1908   indent (info);
1909   if (! global)
1910     fprintf (info->f, "static ");
1911   fprintf (info->f, "%s (", t);
1912
1913   info->parameter = 1;
1914
1915   return TRUE;
1916 }
1917
1918 /* Output a function parameter.  */
1919
1920 static bfd_boolean
1921 pr_function_parameter (p, name, kind, val)
1922      PTR p;
1923      const char *name;
1924      enum debug_parm_kind kind;
1925      bfd_vma val;
1926 {
1927   struct pr_handle *info = (struct pr_handle *) p;
1928   char *t;
1929   char ab[20];
1930
1931   if (kind == DEBUG_PARM_REFERENCE
1932       || kind == DEBUG_PARM_REF_REG)
1933     {
1934       if (! pr_reference_type (p))
1935         return FALSE;
1936     }
1937
1938   if (! substitute_type (info, name))
1939     return FALSE;
1940
1941   t = pop_type (info);
1942   if (t == NULL)
1943     return FALSE;
1944
1945   if (info->parameter != 1)
1946     fprintf (info->f, ", ");
1947
1948   if (kind == DEBUG_PARM_REG || kind == DEBUG_PARM_REF_REG)
1949     fprintf (info->f, "register ");
1950
1951   print_vma (val, ab, TRUE, TRUE);
1952   fprintf (info->f, "%s /* %s */", t, ab);
1953
1954   free (t);
1955
1956   ++info->parameter;
1957
1958   return TRUE;
1959 }
1960
1961 /* Start writing out a block.  */
1962
1963 static bfd_boolean
1964 pr_start_block (p, addr)
1965      PTR p;
1966      bfd_vma addr;
1967 {
1968   struct pr_handle *info = (struct pr_handle *) p;
1969   char ab[20];
1970
1971   if (info->parameter > 0)
1972     {
1973       fprintf (info->f, ")\n");
1974       info->parameter = 0;
1975     }
1976
1977   indent (info);
1978   print_vma (addr, ab, TRUE, TRUE);
1979   fprintf (info->f, "{ /* %s */\n", ab);
1980
1981   info->indent += 2;
1982
1983   return TRUE;
1984 }
1985
1986 /* Write out line number information.  */
1987
1988 static bfd_boolean
1989 pr_lineno (p, filename, lineno, addr)
1990      PTR p;
1991      const char *filename;
1992      unsigned long lineno;
1993      bfd_vma addr;
1994 {
1995   struct pr_handle *info = (struct pr_handle *) p;
1996   char ab[20];
1997
1998   indent (info);
1999   print_vma (addr, ab, TRUE, TRUE);
2000   fprintf (info->f, "/* file %s line %lu addr %s */\n", filename, lineno, ab);
2001
2002   return TRUE;
2003 }
2004
2005 /* Finish writing out a block.  */
2006
2007 static bfd_boolean
2008 pr_end_block (p, addr)
2009      PTR p;
2010      bfd_vma addr;
2011 {
2012   struct pr_handle *info = (struct pr_handle *) p;
2013   char ab[20];
2014
2015   info->indent -= 2;
2016
2017   indent (info);
2018   print_vma (addr, ab, TRUE, TRUE);
2019   fprintf (info->f, "} /* %s */\n", ab);
2020
2021   return TRUE;
2022 }
2023
2024 /* Finish writing out a function.  */
2025
2026 static bfd_boolean
2027 pr_end_function (p)
2028      PTR p ATTRIBUTE_UNUSED;
2029 {
2030   return TRUE;
2031 }
2032 \f
2033 /* Tags style generation functions start here.  */
2034
2035 /* Variables for address to line translation.  */
2036 static bfd_vma pc;
2037 static const char *filename;
2038 static const char *functionname;
2039 static unsigned int line;
2040 static bfd_boolean found;
2041
2042 /* Look for an address in a section.  This is called via
2043    bfd_map_over_sections.  */
2044
2045 static void
2046 find_address_in_section (bfd *abfd, asection *section, void *data)
2047 {
2048   bfd_vma vma;
2049   bfd_size_type size;
2050   asymbol **syms = (asymbol **) data;
2051
2052   if (found)
2053     return;
2054
2055   if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0)
2056     return;
2057
2058   vma = bfd_get_section_vma (abfd, section);
2059   if (pc < vma)
2060     return;
2061
2062   size = bfd_get_section_size_before_reloc (section);
2063   if (pc >= vma + size)
2064     return;
2065
2066   found = bfd_find_nearest_line (abfd, section, syms, pc - vma,
2067                                  &filename, &functionname, &line);
2068 }
2069
2070 static void
2071 translate_addresses (bfd *abfd, char *addr_hex, FILE *f, asymbol **syms)
2072 {
2073   pc = bfd_scan_vma (addr_hex, NULL, 16);
2074   found = FALSE;
2075   bfd_map_over_sections (abfd, find_address_in_section, syms);
2076
2077   if (! found)
2078     fprintf (f, "??");
2079   else
2080     fprintf (f, "%u", line);
2081 }
2082
2083 /* Start a new compilation unit.  */
2084
2085 static bfd_boolean
2086 tg_start_compilation_unit (void * p, const char *filename ATTRIBUTE_UNUSED)
2087 {
2088   struct pr_handle *info = (struct pr_handle *) p;
2089
2090   fprintf (stderr, "New compilation unit: %s\n", filename);
2091
2092   free (info->filename);
2093   /* Should it be relative? best way to do it here?.  */
2094   info->filename = strdup (filename);
2095
2096   return TRUE;
2097 }
2098
2099 /* Start a source file within a compilation unit.  */
2100
2101 static bfd_boolean
2102 tg_start_source (void *p, const char *filename)
2103 {
2104   struct pr_handle *info = (struct pr_handle *) p;
2105
2106   free (info->filename);
2107   /* Should it be relative? best way to do it here?.  */
2108   info->filename = strdup (filename);
2109
2110   return TRUE;
2111 }
2112
2113 /* Push an enum type onto the type stack.  */
2114
2115 static bfd_boolean
2116 tg_enum_type (void *p, const char *tag, const char **names,
2117               bfd_signed_vma *values)
2118 {
2119   struct pr_handle *info = (struct pr_handle *) p;
2120   unsigned int i;
2121   const char *name;
2122   char ab[20];
2123
2124   if (! pr_enum_type (p, tag, names, values))
2125     return FALSE;
2126
2127   name = tag ? tag : "unknown";
2128   /* Generate an entry for the enum.  */
2129   if (tag)
2130     fprintf (info->f, "%s\t%s\t0;\"\tkind:e\ttype:%s\n", tag,
2131              info->filename, info->stack->type);
2132
2133   /* Generate entries for the values.  */
2134   if (names != NULL)
2135     {
2136       for (i = 0; names[i] != NULL; i++)
2137         {
2138           print_vma (values[i], ab, FALSE, FALSE);
2139           fprintf (info->f, "%s\t%s\t0;\"\tkind:g\tenum:%s\tvalue:%s\n",
2140                    names[i], info->filename, name, ab);
2141         }
2142     }
2143
2144   return TRUE;
2145 }
2146
2147 /* Start accumulating a struct type.  */
2148
2149 static bfd_boolean
2150 tg_start_struct_type (void *p, const char *tag, unsigned int id,
2151                       bfd_boolean structp, unsigned int size ATTRIBUTE_UNUSED)
2152 {
2153   struct pr_handle *info = (struct pr_handle *) p;
2154   const char *name;
2155   char idbuf[20];
2156
2157   if (tag != NULL)
2158     name = tag;
2159   else
2160     {
2161       name = idbuf;
2162       sprintf (idbuf, "%%anon%u", id);
2163     }
2164
2165   if (! push_type (info, name))
2166     return FALSE;
2167
2168   info->stack->flavor = structp ? "struct" : "union";
2169
2170   fprintf (info->f, "%s\t%s\t0;\"\tkind:%c\n", name, info->filename,
2171            info->stack->flavor[0]);
2172
2173   info->stack->visibility = DEBUG_VISIBILITY_PUBLIC;
2174
2175   return indent_type (info);
2176 }
2177
2178 /* Output the visibility of a field in a struct.  */
2179
2180 static bfd_boolean
2181 tg_fix_visibility (struct pr_handle *info, enum debug_visibility visibility)
2182 {
2183   assert (info->stack != NULL);
2184
2185   if (info->stack->visibility == visibility)
2186     return TRUE;
2187
2188   assert (info->stack->visibility != DEBUG_VISIBILITY_IGNORE);
2189
2190   info->stack->visibility = visibility;
2191
2192   return TRUE;
2193 }
2194
2195 /* Add a field to a struct type.  */
2196
2197 static bfd_boolean
2198 tg_struct_field (void *p, const char *name, bfd_vma bitpos ATTRIBUTE_UNUSED,
2199                  bfd_vma bitsize ATTRIBUTE_UNUSED,
2200                  enum debug_visibility visibility)
2201 {
2202   struct pr_handle *info = (struct pr_handle *) p;
2203   char *t;
2204
2205   t = pop_type (info);
2206   if (t == NULL)
2207     return FALSE;
2208
2209   if (! tg_fix_visibility (info, visibility))
2210     return FALSE;
2211
2212   /* It happends, a bug? */
2213   if (! name[0])
2214     return TRUE;
2215
2216   fprintf (info->f, "%s\t%s\t0;\"\tkind:m\ttype:%s\t%s:%s\taccess:%s\n",
2217            name, info->filename, t, info->stack->flavor, info->stack->type,
2218            visibility_name (visibility));
2219
2220   return TRUE;
2221 }
2222
2223 /* Finish a struct type.  */
2224
2225 static bfd_boolean
2226 tg_end_struct_type (void *p ATTRIBUTE_UNUSED)
2227 {
2228   struct pr_handle *info = (struct pr_handle *) p;
2229   assert (info->stack != NULL);
2230
2231   return TRUE;
2232 }
2233
2234 /* Start a class type.  */
2235
2236 static bfd_boolean
2237 tg_start_class_type (void *p, const char *tag, unsigned int id,
2238                      bfd_boolean structp, unsigned int size,
2239                      bfd_boolean vptr, bfd_boolean ownvptr)
2240 {
2241   struct pr_handle *info = (struct pr_handle *) p;
2242   char *tv = NULL;
2243   const char *name;
2244
2245   info->indent += 2;
2246
2247   if (vptr && ! ownvptr)
2248     {
2249       tv = pop_type (info);
2250       if (tv == NULL)
2251         return FALSE;
2252     }
2253
2254   if (tag != NULL)
2255     name = tag;
2256   else
2257     {
2258       char idbuf[20];
2259
2260       sprintf (idbuf, "%%anon%u", id);
2261       name = idbuf;
2262     }
2263
2264   if (! push_type (info, name))
2265     return FALSE;
2266
2267   info->stack->flavor = structp ? "class" : "union class";
2268   info->stack->parents = NULL;
2269   info->stack->num_parents = 0;
2270
2271   if (size != 0 || vptr || ownvptr || tag != NULL)
2272     {
2273       if (vptr)
2274         {
2275           if (! append_type (info, " vtable "))
2276             return FALSE;
2277           if (ownvptr)
2278             {
2279               if (! append_type (info, "self "))
2280                 return FALSE;
2281             }
2282           else
2283             {
2284               if (! append_type (info, tv)
2285                   || ! append_type (info, " "))
2286                 return FALSE;
2287             }
2288         }
2289     }
2290
2291   info->stack->visibility = DEBUG_VISIBILITY_PRIVATE;
2292
2293   return TRUE;
2294 }
2295
2296 /* Add a static member to a class.  */
2297
2298 static bfd_boolean
2299 tg_class_static_member (void *p, const char *name,
2300                         const char *physname ATTRIBUTE_UNUSED,
2301                         enum debug_visibility visibility)
2302 {
2303   struct pr_handle *info = (struct pr_handle *) p;
2304   char *t;
2305   int len_var, len_class;
2306   char *full_name;
2307
2308   len_var = strlen (name);
2309   len_class = strlen (info->stack->next->type);
2310   full_name = (char *) xmalloc (len_var + len_class + 3);
2311   if (! full_name)
2312     return FALSE;
2313   memcpy (full_name, info->stack->next->type, len_class);
2314   memcpy (full_name + len_class, "::", 2);
2315   memcpy (full_name + len_class + 2, name, len_var + 1);
2316
2317   if (! substitute_type (info, full_name))
2318     return FALSE;
2319
2320   if (! prepend_type (info, "static "))
2321     return FALSE;
2322
2323   t = pop_type (info);
2324   if (t == NULL)
2325     return FALSE;
2326
2327   if (! tg_fix_visibility (info, visibility))
2328     return FALSE;
2329
2330   fprintf (info->f, "%s\t%s\t0;\"\tkind:x\ttype:%s\tclass:%s\taccess:%s\n",
2331            name, info->filename, t, info->stack->type,
2332            visibility_name (visibility));
2333   free (t);
2334   free (full_name);
2335
2336   return TRUE;
2337 }
2338
2339 /* Add a base class to a class.  */
2340
2341 static bfd_boolean
2342 tg_class_baseclass (void *p, bfd_vma bitpos ATTRIBUTE_UNUSED,
2343                     bfd_boolean virtual, enum debug_visibility visibility)
2344 {
2345   struct pr_handle *info = (struct pr_handle *) p;
2346   char *t;
2347   const char *prefix;
2348
2349   assert (info->stack != NULL && info->stack->next != NULL);
2350
2351   t = pop_type (info);
2352   if (t == NULL)
2353     return FALSE;
2354
2355   if (strncmp (t, "class ", sizeof "class " - 1) == 0)
2356     t += sizeof "class " - 1;
2357
2358   /* Push it back on to take advantage of the prepend_type and
2359      append_type routines.  */
2360   if (! push_type (info, t))
2361     return FALSE;
2362
2363   if (virtual)
2364     {
2365       if (! prepend_type (info, "virtual "))
2366         return FALSE;
2367     }
2368
2369   switch (visibility)
2370     {
2371     case DEBUG_VISIBILITY_PUBLIC:
2372       prefix = "public ";
2373       break;
2374     case DEBUG_VISIBILITY_PROTECTED:
2375       prefix = "protected ";
2376       break;
2377     case DEBUG_VISIBILITY_PRIVATE:
2378       prefix = "private ";
2379       break;
2380     default:
2381       prefix = "/* unknown visibility */ ";
2382       break;
2383     }
2384
2385   if (! prepend_type (info, prefix))
2386     return FALSE;
2387
2388   t = pop_type (info);
2389   if (t == NULL)
2390     return FALSE;
2391
2392   if (info->stack->num_parents && ! append_parent (info, ", "))
2393     return FALSE;
2394
2395   if (! append_parent (info, t))
2396     return FALSE;
2397   info->stack->num_parents++;
2398
2399   free (t);
2400
2401   return TRUE;
2402 }
2403
2404 /* Add a variant to a method.  */
2405
2406 static bfd_boolean
2407 tg_class_method_variant (void *p, const char *physname ATTRIBUTE_UNUSED,
2408                          enum debug_visibility visibility,
2409                          bfd_boolean constp, bfd_boolean volatilep,
2410                          bfd_vma voffset ATTRIBUTE_UNUSED,
2411                          bfd_boolean context)
2412 {
2413   struct pr_handle *info = (struct pr_handle *) p;
2414   char *method_type;
2415   char *context_type;
2416   char *method_name;
2417
2418   assert (info->stack != NULL);
2419   assert (info->stack->next != NULL);
2420
2421   /* Put the const and volatile qualifiers on the type.  */
2422   if (volatilep)
2423     {
2424       if (! append_type (info, " volatile"))
2425         return FALSE;
2426     }
2427   if (constp)
2428     {
2429       if (! append_type (info, " const"))
2430         return FALSE;
2431     }
2432
2433   method_name = strdup (context ? info->stack->next->next->method
2434                         : info->stack->next->method);
2435   
2436   /* Stick the name of the method into its type.  */
2437   if (! substitute_type (info, method_name))
2438     return FALSE;
2439
2440   /* Get the type.  */
2441   method_type = pop_type (info);
2442   if (method_type == NULL)
2443     return FALSE;
2444
2445   /* Pull off the context type if there is one.  */
2446   if (! context)
2447     context_type = NULL;
2448   else
2449     {
2450       context_type = pop_type (info);
2451       if (context_type == NULL)
2452         return FALSE;
2453     }
2454
2455   /* Now the top of the stack is the class.  */
2456   if (! tg_fix_visibility (info, visibility))
2457     return FALSE;
2458
2459   fprintf (info->f, "%s\t%s\t0;\"\tkind:p\ttype:%s\tclass:%s\n",
2460            method_name, info->filename, method_type, info->stack->type);
2461   free (method_type);
2462   free (method_name);
2463   free (context_type);
2464   
2465   return TRUE;
2466 }
2467
2468 /* Add a static variant to a method.  */
2469
2470 static bfd_boolean
2471 tg_class_static_method_variant (void *p,
2472                                 const char *physname ATTRIBUTE_UNUSED,
2473                                 enum debug_visibility visibility,
2474                                 bfd_boolean constp,
2475                                 bfd_boolean volatilep)
2476 {
2477   struct pr_handle *info = (struct pr_handle *) p;
2478   char *method_type;
2479   char *method_name;
2480
2481   assert (info->stack != NULL);
2482   assert (info->stack->next != NULL);
2483   assert (info->stack->next->method != NULL);
2484
2485   /* Put the const and volatile qualifiers on the type.  */
2486   if (volatilep)
2487     {
2488       if (! append_type (info, " volatile"))
2489         return FALSE;
2490     }
2491   if (constp)
2492     {
2493       if (! append_type (info, " const"))
2494         return FALSE;
2495     }
2496
2497   /* Mark it as static.  */
2498   if (! prepend_type (info, "static "))
2499     return FALSE;
2500
2501   method_name = strdup (info->stack->next->method);
2502   /* Stick the name of the method into its type.  */
2503   if (! substitute_type (info, info->stack->next->method))
2504     return FALSE;
2505
2506   /* Get the type.  */
2507   method_type = pop_type (info);
2508   if (method_type == NULL)
2509     return FALSE;
2510
2511   /* Now the top of the stack is the class.  */
2512   if (! tg_fix_visibility (info, visibility))
2513     return FALSE;
2514
2515   fprintf (info->f, "%s\t%s\t0;\"\tkind:p\ttype:%s\tclass:%s\taccess:%s\n",
2516            method_name, info->filename, method_type, info->stack->type,
2517            visibility_name (visibility));
2518   free (method_type);
2519   free (method_name);
2520
2521   return TRUE;
2522 }
2523
2524 /* Finish up a class.  */
2525
2526 static bfd_boolean
2527 tg_end_class_type (void *p)
2528 {
2529   struct pr_handle *info = (struct pr_handle *) p;
2530
2531   fprintf (info->f, "%s\t%s\t0;\"\tkind:c\ttype:%s", info->stack->type,
2532            info->filename, info->stack->flavor);
2533   if (info->stack->num_parents)
2534     {
2535       fprintf  (info->f, "\tinherits:%s", info->stack->parents);
2536       free (info->stack->parents);
2537     }
2538   fputc ('\n', info->f);
2539
2540   return tg_end_struct_type (p);
2541 }
2542
2543 /* Push a type on the stack using a tag name.  */
2544
2545 static bfd_boolean
2546 tg_tag_type (void *p, const char *name, unsigned int id,
2547              enum debug_type_kind kind)
2548 {
2549   struct pr_handle *info = (struct pr_handle *) p;
2550   const char *t, *tag;
2551   char idbuf[20];
2552
2553   switch (kind)
2554     {
2555     case DEBUG_KIND_STRUCT:
2556       t = "struct ";
2557       break;
2558     case DEBUG_KIND_UNION:
2559       t = "union ";
2560       break;
2561     case DEBUG_KIND_ENUM:
2562       t = "enum ";
2563       break;
2564     case DEBUG_KIND_CLASS:
2565       t = "class ";
2566       break;
2567     case DEBUG_KIND_UNION_CLASS:
2568       t = "union class ";
2569       break;
2570     default:
2571       abort ();
2572       return FALSE;
2573     }
2574
2575   if (! push_type (info, t))
2576     return FALSE;
2577   if (name != NULL)
2578     tag = name;
2579   else
2580     {
2581       sprintf (idbuf, "%%anon%u", id);
2582       tag = idbuf;
2583     }
2584
2585   if (! append_type (info, tag))
2586     return FALSE;
2587
2588   return TRUE;
2589 }
2590
2591 /* Output a typedef.  */
2592
2593 static bfd_boolean
2594 tg_typdef (void *p, const char *name)
2595 {
2596   struct pr_handle *info = (struct pr_handle *) p;
2597   char *s;
2598
2599   s = pop_type (info);
2600   if (s == NULL)
2601     return FALSE;
2602
2603   fprintf (info->f, "%s\t%s\t0;\"\tkind:t\ttype:%s\n", name,
2604            info->filename, s);
2605
2606   free (s);
2607
2608   return TRUE;
2609 }
2610
2611 /* Output a tag.  The tag should already be in the string on the
2612    stack, so all we have to do here is print it out.  */
2613
2614 static bfd_boolean
2615 tg_tag (void *p ATTRIBUTE_UNUSED, const char *name ATTRIBUTE_UNUSED)
2616 {
2617   struct pr_handle *info = (struct pr_handle *) p;
2618   char *t;
2619
2620   t = pop_type (info);
2621   if (t == NULL)
2622     return FALSE;
2623   free (t);
2624
2625   return TRUE;
2626 }
2627
2628 /* Output an integer constant.  */
2629
2630 static bfd_boolean
2631 tg_int_constant (void *p, const char *name, bfd_vma val)
2632 {
2633   struct pr_handle *info = (struct pr_handle *) p;
2634   char ab[20];
2635
2636   indent (info);
2637   print_vma (val, ab, FALSE, FALSE);
2638   fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:const int\tvalue:%s\n",
2639            name, info->filename, ab);
2640   return TRUE;
2641 }
2642
2643 /* Output a floating point constant.  */
2644
2645 static bfd_boolean
2646 tg_float_constant (void *p, const char *name, double val)
2647 {
2648   struct pr_handle *info = (struct pr_handle *) p;
2649
2650   indent (info);
2651   fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:const double\tvalue:%g\n",
2652            name, info->filename, val);
2653   return TRUE;
2654 }
2655
2656 /* Output a typed constant.  */
2657
2658 static bfd_boolean
2659 tg_typed_constant (void *p, const char *name, bfd_vma val)
2660 {
2661   struct pr_handle *info = (struct pr_handle *) p;
2662   char *t;
2663   char ab[20];
2664
2665   t = pop_type (info);
2666   if (t == NULL)
2667     return FALSE;
2668
2669   indent (info);
2670   print_vma (val, ab, FALSE, FALSE);
2671   fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:const %s\tvalue:%s\n",
2672            name, info->filename, t, ab);
2673
2674   free (t);
2675
2676   return TRUE;
2677 }
2678
2679 /* Output a variable.  */
2680
2681 static bfd_boolean
2682 tg_variable (void *p, const char *name, enum debug_var_kind kind,
2683              bfd_vma val ATTRIBUTE_UNUSED)
2684 {
2685   struct pr_handle *info = (struct pr_handle *) p;
2686   char *t;
2687   const char *dname, *from_class;
2688
2689   t = pop_type (info);
2690   if (t == NULL)
2691     return FALSE;
2692
2693   dname = name;
2694   if (info->demangler)
2695     {
2696       dname = info->demangler (info->abfd, name);
2697       if (strcmp (name, dname) == 0)
2698         {
2699           free ((char *) dname);
2700           dname = name;
2701         }
2702     }
2703
2704   if (dname != name)
2705     {
2706       char *sep;
2707       sep = strstr (dname, "::");
2708       if (sep)
2709         {
2710           *sep = 0;
2711           name = sep + 2;
2712           from_class = dname;
2713         }
2714       else
2715         {
2716           /* Obscure types as vts and type_info nodes.  */
2717           name = dname;
2718           from_class = NULL;
2719         }
2720     }
2721   else
2722     from_class = NULL;
2723
2724   fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:%s", name, info->filename, t);
2725
2726   switch (kind)
2727     {
2728     case DEBUG_STATIC:
2729     case DEBUG_LOCAL_STATIC:
2730       fprintf (info->f, "\tfile:");
2731       break;
2732     case DEBUG_REGISTER:
2733       fprintf (info->f, "\tregister:");
2734       break;
2735     default:
2736       break;
2737     }
2738
2739   if (from_class)
2740     {
2741       fprintf (info->f, "\tclass:%s",from_class);
2742       free ((char *) dname);
2743     }
2744
2745   fprintf (info->f, "\n");
2746
2747   free (t);
2748
2749   return TRUE;
2750 }
2751
2752 /* Start outputting a function.  */
2753
2754 static bfd_boolean
2755 tg_start_function (void *p, const char *name, bfd_boolean global)
2756 {
2757   struct pr_handle *info = (struct pr_handle *) p;
2758   const char *dname;
2759
2760   if (! global)
2761     info->stack->flavor = "static";
2762   else
2763     info->stack->flavor = NULL;
2764
2765   dname = name;
2766   if (info->demangler)
2767     {
2768       dname = info->demangler (info->abfd, name);
2769       if (strcmp (name, dname) == 0)
2770         {
2771           free ((char *) dname);
2772           dname = name;
2773         }
2774     }
2775
2776   if (! substitute_type (info, dname))
2777     return FALSE;
2778     
2779   if (dname != name)
2780     {
2781       char *sep;
2782       sep = strstr (dname, "::");
2783       if (sep)
2784         {
2785           info->stack->method = dname;
2786           *sep = 0;
2787           name = sep + 2;
2788         }
2789       else
2790         {
2791           info->stack->method = "";
2792           name = dname;
2793         }
2794       sep = strchr (name, '(');
2795       if (sep)
2796         *sep = 0;
2797       /* Obscure functions as type_info function.  */
2798     }
2799   else
2800     info->stack->method = NULL;
2801
2802   info->stack->parents = strdup (name);
2803
2804   if (! info->stack->method && ! append_type (info, "("))
2805     return FALSE;
2806
2807   info->parameter = 1;
2808
2809   return TRUE;
2810 }
2811
2812 /* Output a function parameter.  */
2813
2814 static bfd_boolean
2815 tg_function_parameter (void *p, const char *name, enum debug_parm_kind kind,
2816                        bfd_vma val ATTRIBUTE_UNUSED)
2817 {
2818   struct pr_handle *info = (struct pr_handle *) p;
2819   char *t;
2820
2821   if (kind == DEBUG_PARM_REFERENCE
2822       || kind == DEBUG_PARM_REF_REG)
2823     {
2824       if (! pr_reference_type (p))
2825         return FALSE;
2826     }
2827
2828   if (! substitute_type (info, name))
2829     return FALSE;
2830
2831   t = pop_type (info);
2832   if (t == NULL)
2833     return FALSE;
2834
2835   if (! info->stack->method)
2836     {
2837       if (info->parameter != 1 && ! append_type (info, ", "))
2838         return FALSE;
2839     
2840       if (kind == DEBUG_PARM_REG || kind == DEBUG_PARM_REF_REG)
2841         if (! append_type (info, "register "))
2842           return FALSE;
2843           
2844       if (! append_type (info, t))
2845         return FALSE;
2846     }
2847
2848   free (t);
2849
2850   ++info->parameter;
2851
2852   return TRUE;
2853 }
2854
2855 /* Start writing out a block.  */
2856
2857 static bfd_boolean
2858 tg_start_block (void *p, bfd_vma addr)
2859 {
2860   struct pr_handle *info = (struct pr_handle *) p;
2861   char ab[20], kind, *partof;
2862   char *t;
2863   bfd_boolean local;
2864
2865   if (info->parameter > 0)
2866     {
2867       info->parameter = 0;
2868
2869       /* Delayed name.  */
2870       fprintf (info->f, "%s\t%s\t", info->stack->parents, info->filename);
2871       free (info->stack->parents);
2872
2873       print_vma (addr, ab, TRUE, TRUE);
2874       translate_addresses (info->abfd, ab, info->f, info->syms);
2875       local = info->stack->flavor != NULL;
2876       if (info->stack->method && *info->stack->method)
2877         {
2878           kind = 'm';
2879           partof = (char *) info->stack->method;
2880         }
2881       else
2882         {
2883           kind = 'f';
2884           partof = NULL;
2885           if (! info->stack->method && ! append_type (info, ")"))
2886             return FALSE;
2887         }
2888       t = pop_type (info);
2889       if (t == NULL)
2890         return FALSE;
2891       fprintf (info->f, ";\"\tkind:%c\ttype:%s", kind, t);
2892       if (local)
2893         fputs ("\tfile:", info->f);
2894       if (partof)
2895         {
2896           fprintf (info->f, "\tclass:%s", partof);
2897           free (partof);
2898         }
2899       fputc ('\n', info->f);
2900     }
2901
2902   return TRUE;
2903 }
2904
2905 /* Write out line number information.  */
2906
2907 static bfd_boolean
2908 tg_lineno (void *p ATTRIBUTE_UNUSED,
2909            const char *filename ATTRIBUTE_UNUSED,
2910            unsigned long lineno ATTRIBUTE_UNUSED,
2911            bfd_vma addr ATTRIBUTE_UNUSED)
2912 {
2913   return TRUE;
2914 }
2915
2916 /* Finish writing out a block.  */
2917
2918 static bfd_boolean
2919 tg_end_block (void *p ATTRIBUTE_UNUSED, bfd_vma addr ATTRIBUTE_UNUSED)
2920 {
2921   return TRUE;
2922 }
2923
2924 /* Convert the visibility value into a human readable name.  */
2925
2926 static const char *
2927 visibility_name (enum debug_visibility visibility)
2928 {
2929   const char *s;
2930
2931   switch (visibility)
2932     {
2933     case DEBUG_VISIBILITY_PUBLIC:
2934       s = "public";
2935       break;
2936     case DEBUG_VISIBILITY_PRIVATE:
2937       s = "private";
2938       break;
2939     case DEBUG_VISIBILITY_PROTECTED:
2940       s = "protected";
2941       break;
2942     case DEBUG_VISIBILITY_IGNORE:
2943       s = "/* ignore */";
2944       break;
2945     default:
2946       abort ();
2947       return FALSE;
2948     }
2949   return s;
2950 }