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