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