1 /* prdbg.c -- Print out generic debugging information.
2    Copyright (C) 1995-2020 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 
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 
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 
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 
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 
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 
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_section_flags (section) & SEC_ALLOC) == 0)
1934     return;
1935 
1936   vma = bfd_section_vma (section);
1937   if (pc < vma)
1938     return;
1939 
1940   size = bfd_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   char idbuf[20];
2121 
2122   info->indent += 2;
2123 
2124   if (vptr && ! ownvptr)
2125     {
2126       tv = pop_type (info);
2127       if (tv == NULL)
2128 	return FALSE;
2129     }
2130 
2131   if (tag != NULL)
2132     name = tag;
2133   else
2134     {
2135       sprintf (idbuf, "%%anon%u", id);
2136       name = idbuf;
2137     }
2138 
2139   if (! push_type (info, name))
2140     return FALSE;
2141 
2142   info->stack->flavor = structp ? "class" : "union class";
2143   info->stack->parents = NULL;
2144   info->stack->num_parents = 0;
2145 
2146   if (size != 0 || vptr || ownvptr || tag != NULL)
2147     {
2148       if (vptr)
2149 	{
2150 	  if (! append_type (info, " vtable "))
2151 	    return FALSE;
2152 	  if (ownvptr)
2153 	    {
2154 	      if (! append_type (info, "self "))
2155 		return FALSE;
2156 	    }
2157 	  else
2158 	    {
2159 	      if (! append_type (info, tv)
2160 		  || ! append_type (info, " "))
2161 		return FALSE;
2162 	    }
2163 	}
2164     }
2165 
2166   info->stack->visibility = DEBUG_VISIBILITY_PRIVATE;
2167 
2168   return TRUE;
2169 }
2170 
2171 /* Add a static member to a class.  */
2172 
2173 static bfd_boolean
2174 tg_class_static_member (void *p, const char *name,
2175 			const char *physname ATTRIBUTE_UNUSED,
2176 			enum debug_visibility visibility)
2177 {
2178   struct pr_handle *info = (struct pr_handle *) p;
2179   char *t;
2180   int len_var, len_class;
2181   char *full_name;
2182 
2183   len_var = strlen (name);
2184   len_class = strlen (info->stack->next->type);
2185   full_name = (char *) xmalloc (len_var + len_class + 3);
2186   if (! full_name)
2187     return FALSE;
2188   sprintf (full_name, "%s::%s", info->stack->next->type, name);
2189 
2190   if (! substitute_type (info, full_name))
2191     {
2192       free (full_name);
2193       return FALSE;
2194     }
2195 
2196   if (! prepend_type (info, "static "))
2197     {
2198       free (full_name);
2199       return FALSE;
2200     }
2201 
2202   t = pop_type (info);
2203   if (t == NULL)
2204     {
2205       free (full_name);
2206       return FALSE;
2207     }
2208 
2209   if (! tg_fix_visibility (info, visibility))
2210     {
2211       free (t);
2212       free (full_name);
2213       return FALSE;
2214     }
2215 
2216   fprintf (info->f, "%s\t%s\t0;\"\tkind:x\ttype:%s\tclass:%s\taccess:%s\n",
2217 	   name, info->filename, t, info->stack->type,
2218 	   visibility_name (visibility));
2219   free (t);
2220   free (full_name);
2221 
2222   return TRUE;
2223 }
2224 
2225 /* Add a base class to a class.  */
2226 
2227 static bfd_boolean
2228 tg_class_baseclass (void *p, bfd_vma bitpos ATTRIBUTE_UNUSED,
2229 		    bfd_boolean is_virtual, enum debug_visibility visibility)
2230 {
2231   struct pr_handle *info = (struct pr_handle *) p;
2232   char *t;
2233   const char *prefix;
2234 
2235   assert (info->stack != NULL && info->stack->next != NULL);
2236 
2237   t = pop_type (info);
2238   if (t == NULL)
2239     return FALSE;
2240 
2241   if (CONST_STRNEQ (t, "class "))
2242     t += sizeof "class " - 1;
2243 
2244   /* Push it back on to take advantage of the prepend_type and
2245      append_type routines.  */
2246   if (! push_type (info, t))
2247     return FALSE;
2248 
2249   if (is_virtual)
2250     {
2251       if (! prepend_type (info, "virtual "))
2252 	return FALSE;
2253     }
2254 
2255   switch (visibility)
2256     {
2257     case DEBUG_VISIBILITY_PUBLIC:
2258       prefix = "public ";
2259       break;
2260     case DEBUG_VISIBILITY_PROTECTED:
2261       prefix = "protected ";
2262       break;
2263     case DEBUG_VISIBILITY_PRIVATE:
2264       prefix = "private ";
2265       break;
2266     default:
2267       prefix = "/* unknown visibility */ ";
2268       break;
2269     }
2270 
2271   if (! prepend_type (info, prefix))
2272     return FALSE;
2273 
2274   t = pop_type (info);
2275   if (t == NULL)
2276     return FALSE;
2277 
2278   if (info->stack->num_parents && ! append_parent (info, ", "))
2279     return FALSE;
2280 
2281   if (! append_parent (info, t))
2282     return FALSE;
2283   info->stack->num_parents++;
2284 
2285   free (t);
2286 
2287   return TRUE;
2288 }
2289 
2290 /* Add a variant to a method.  */
2291 
2292 static bfd_boolean
2293 tg_class_method_variant (void *p, const char *physname ATTRIBUTE_UNUSED,
2294 			 enum debug_visibility visibility,
2295 			 bfd_boolean constp, bfd_boolean volatilep,
2296 			 bfd_vma voffset ATTRIBUTE_UNUSED,
2297 			 bfd_boolean context)
2298 {
2299   struct pr_handle *info = (struct pr_handle *) p;
2300   char *method_type;
2301   char *context_type;
2302   char *method_name;
2303 
2304   assert (info->stack != NULL);
2305   assert (info->stack->next != NULL);
2306 
2307   /* Put the const and volatile qualifiers on the type.  */
2308   if (volatilep)
2309     {
2310       if (! append_type (info, " volatile"))
2311 	return FALSE;
2312     }
2313   if (constp)
2314     {
2315       if (! append_type (info, " const"))
2316 	return FALSE;
2317     }
2318 
2319   method_name = strdup (context ? info->stack->next->next->method
2320 			: info->stack->next->method);
2321 
2322   /* Stick the name of the method into its type.  */
2323   if (! substitute_type (info, method_name))
2324     {
2325       free (method_name);
2326       return FALSE;
2327     }
2328 
2329   /* Get the type.  */
2330   method_type = pop_type (info);
2331   if (method_type == NULL)
2332     {
2333       free (method_name);
2334       return FALSE;
2335     }
2336 
2337   /* Pull off the context type if there is one.  */
2338   if (! context)
2339     context_type = NULL;
2340   else
2341     {
2342       context_type = pop_type (info);
2343       if (context_type == NULL)
2344 	{
2345 	  free (method_type);
2346 	  free (method_name);
2347 	  return FALSE;
2348 	}
2349     }
2350 
2351   /* Now the top of the stack is the class.  */
2352   if (! tg_fix_visibility (info, visibility))
2353     {
2354       free (method_type);
2355       free (method_name);
2356       free (context_type);
2357       return FALSE;
2358     }
2359 
2360   fprintf (info->f, "%s\t%s\t0;\"\tkind:p\ttype:%s\tclass:%s\n",
2361 	   method_name, info->filename, method_type, info->stack->type);
2362   free (method_type);
2363   free (method_name);
2364   free (context_type);
2365 
2366   return TRUE;
2367 }
2368 
2369 /* Add a static variant to a method.  */
2370 
2371 static bfd_boolean
2372 tg_class_static_method_variant (void *p,
2373 				const char *physname ATTRIBUTE_UNUSED,
2374 				enum debug_visibility visibility,
2375 				bfd_boolean constp, bfd_boolean volatilep)
2376 {
2377   struct pr_handle *info = (struct pr_handle *) p;
2378   char *method_type;
2379   char *method_name;
2380 
2381   assert (info->stack != NULL);
2382   assert (info->stack->next != NULL);
2383   assert (info->stack->next->method != NULL);
2384 
2385   /* Put the const and volatile qualifiers on the type.  */
2386   if (volatilep)
2387     {
2388       if (! append_type (info, " volatile"))
2389 	return FALSE;
2390     }
2391   if (constp)
2392     {
2393       if (! append_type (info, " const"))
2394 	return FALSE;
2395     }
2396 
2397   /* Mark it as static.  */
2398   if (! prepend_type (info, "static "))
2399     return FALSE;
2400 
2401   method_name = strdup (info->stack->next->method);
2402   /* Stick the name of the method into its type.  */
2403   if (! substitute_type (info, info->stack->next->method))
2404     {
2405       free (method_name);
2406       return FALSE;
2407     }
2408 
2409   /* Get the type.  */
2410   method_type = pop_type (info);
2411   if (method_type == NULL)
2412     {
2413       free (method_name);
2414       return FALSE;
2415     }
2416 
2417   /* Now the top of the stack is the class.  */
2418   if (! tg_fix_visibility (info, visibility))
2419     {
2420       free (method_type);
2421       free (method_name);
2422       return FALSE;
2423     }
2424 
2425   fprintf (info->f, "%s\t%s\t0;\"\tkind:p\ttype:%s\tclass:%s\taccess:%s\n",
2426 	   method_name, info->filename, method_type, info->stack->type,
2427 	   visibility_name (visibility));
2428   free (method_type);
2429   free (method_name);
2430 
2431   return TRUE;
2432 }
2433 
2434 /* Finish up a class.  */
2435 
2436 static bfd_boolean
2437 tg_end_class_type (void *p)
2438 {
2439   struct pr_handle *info = (struct pr_handle *) p;
2440 
2441   fprintf (info->f, "%s\t%s\t0;\"\tkind:c\ttype:%s", info->stack->type,
2442 	   info->filename, info->stack->flavor);
2443   if (info->stack->num_parents)
2444     {
2445       fprintf  (info->f, "\tinherits:%s", info->stack->parents);
2446       free (info->stack->parents);
2447     }
2448   fputc ('\n', info->f);
2449 
2450   return tg_end_struct_type (p);
2451 }
2452 
2453 /* Push a type on the stack using a tag name.  */
2454 
2455 static bfd_boolean
2456 tg_tag_type (void *p, const char *name, unsigned int id,
2457 	     enum debug_type_kind kind)
2458 {
2459   struct pr_handle *info = (struct pr_handle *) p;
2460   const char *t, *tag;
2461   char idbuf[20];
2462 
2463   switch (kind)
2464     {
2465     case DEBUG_KIND_STRUCT:
2466       t = "struct ";
2467       break;
2468     case DEBUG_KIND_UNION:
2469       t = "union ";
2470       break;
2471     case DEBUG_KIND_ENUM:
2472       t = "enum ";
2473       break;
2474     case DEBUG_KIND_CLASS:
2475       t = "class ";
2476       break;
2477     case DEBUG_KIND_UNION_CLASS:
2478       t = "union class ";
2479       break;
2480     default:
2481       abort ();
2482       return FALSE;
2483     }
2484 
2485   if (! push_type (info, t))
2486     return FALSE;
2487   if (name != NULL)
2488     tag = name;
2489   else
2490     {
2491       sprintf (idbuf, "%%anon%u", id);
2492       tag = idbuf;
2493     }
2494 
2495   if (! append_type (info, tag))
2496     return FALSE;
2497 
2498   return TRUE;
2499 }
2500 
2501 /* Output a typedef.  */
2502 
2503 static bfd_boolean
2504 tg_typdef (void *p, const char *name)
2505 {
2506   struct pr_handle *info = (struct pr_handle *) p;
2507   char *s;
2508 
2509   s = pop_type (info);
2510   if (s == NULL)
2511     return FALSE;
2512 
2513   fprintf (info->f, "%s\t%s\t0;\"\tkind:t\ttype:%s\n", name,
2514 	   info->filename, s);
2515 
2516   free (s);
2517 
2518   return TRUE;
2519 }
2520 
2521 /* Output a tag.  The tag should already be in the string on the
2522    stack, so all we have to do here is print it out.  */
2523 
2524 static bfd_boolean
2525 tg_tag (void *p ATTRIBUTE_UNUSED, const char *name ATTRIBUTE_UNUSED)
2526 {
2527   struct pr_handle *info = (struct pr_handle *) p;
2528   char *t;
2529 
2530   t = pop_type (info);
2531   if (t == NULL)
2532     return FALSE;
2533   free (t);
2534 
2535   return TRUE;
2536 }
2537 
2538 /* Output an integer constant.  */
2539 
2540 static bfd_boolean
2541 tg_int_constant (void *p, const char *name, bfd_vma val)
2542 {
2543   struct pr_handle *info = (struct pr_handle *) p;
2544   char ab[22];
2545 
2546   indent (info);
2547   print_vma (val, ab, FALSE, FALSE);
2548   fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:const int\tvalue:%s\n",
2549 	   name, info->filename, ab);
2550   return TRUE;
2551 }
2552 
2553 /* Output a floating point constant.  */
2554 
2555 static bfd_boolean
2556 tg_float_constant (void *p, const char *name, double val)
2557 {
2558   struct pr_handle *info = (struct pr_handle *) p;
2559 
2560   indent (info);
2561   fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:const double\tvalue:%g\n",
2562 	   name, info->filename, val);
2563   return TRUE;
2564 }
2565 
2566 /* Output a typed constant.  */
2567 
2568 static bfd_boolean
2569 tg_typed_constant (void *p, const char *name, bfd_vma val)
2570 {
2571   struct pr_handle *info = (struct pr_handle *) p;
2572   char *t;
2573   char ab[22];
2574 
2575   t = pop_type (info);
2576   if (t == NULL)
2577     return FALSE;
2578 
2579   indent (info);
2580   print_vma (val, ab, FALSE, FALSE);
2581   fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:const %s\tvalue:%s\n",
2582 	   name, info->filename, t, ab);
2583 
2584   free (t);
2585 
2586   return TRUE;
2587 }
2588 
2589 /* Output a variable.  */
2590 
2591 static bfd_boolean
2592 tg_variable (void *p, const char *name, enum debug_var_kind kind,
2593 	     bfd_vma val ATTRIBUTE_UNUSED)
2594 {
2595   struct pr_handle *info = (struct pr_handle *) p;
2596   char *t, *dname, *from_class;
2597 
2598   t = pop_type (info);
2599   if (t == NULL)
2600     return FALSE;
2601 
2602   dname = NULL;
2603   if (info->demangler)
2604     dname = info->demangler (info->abfd, name, demangle_flags);
2605 
2606   from_class = NULL;
2607   if (dname != NULL)
2608     {
2609       char *sep;
2610       sep = strstr (dname, "::");
2611       if (sep)
2612 	{
2613 	  *sep = 0;
2614 	  name = sep + 2;
2615 	  from_class = dname;
2616 	}
2617       else
2618 	/* Obscure types as vts and type_info nodes.  */
2619 	name = dname;
2620     }
2621 
2622   fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:%s", name, info->filename, t);
2623 
2624   switch (kind)
2625     {
2626     case DEBUG_STATIC:
2627     case DEBUG_LOCAL_STATIC:
2628       fprintf (info->f, "\tfile:");
2629       break;
2630     case DEBUG_REGISTER:
2631       fprintf (info->f, "\tregister:");
2632       break;
2633     default:
2634       break;
2635     }
2636 
2637   if (from_class)
2638     fprintf (info->f, "\tclass:%s", from_class);
2639 
2640   if (dname)
2641     free (dname);
2642 
2643   fprintf (info->f, "\n");
2644 
2645   free (t);
2646 
2647   return TRUE;
2648 }
2649 
2650 /* Start outputting a function.  */
2651 
2652 static bfd_boolean
2653 tg_start_function (void *p, const char *name, bfd_boolean global)
2654 {
2655   struct pr_handle *info = (struct pr_handle *) p;
2656   char *dname;
2657 
2658   if (! global)
2659     info->stack->flavor = "static";
2660   else
2661     info->stack->flavor = NULL;
2662 
2663   dname = NULL;
2664   if (info->demangler)
2665     dname = info->demangler (info->abfd, name, demangle_flags);
2666 
2667   if (! substitute_type (info, dname ? dname : name))
2668     return FALSE;
2669 
2670   info->stack->method = NULL;
2671   if (dname != NULL)
2672     {
2673       char *sep;
2674       sep = strstr (dname, "::");
2675       if (sep)
2676 	{
2677 	  info->stack->method = dname;
2678 	  *sep = 0;
2679 	  name = sep + 2;
2680 	}
2681       else
2682 	{
2683 	  info->stack->method = "";
2684 	  name = dname;
2685 	}
2686       sep = strchr (name, '(');
2687       if (sep)
2688 	*sep = 0;
2689       /* Obscure functions as type_info function.  */
2690     }
2691 
2692   info->stack->parents = strdup (name);
2693 
2694   if (! info->stack->method && ! append_type (info, "("))
2695     return FALSE;
2696 
2697   info->parameter = 1;
2698 
2699   return TRUE;
2700 }
2701 
2702 /* Output a function parameter.  */
2703 
2704 static bfd_boolean
2705 tg_function_parameter (void *p, const char *name, enum debug_parm_kind kind,
2706 		       bfd_vma val ATTRIBUTE_UNUSED)
2707 {
2708   struct pr_handle *info = (struct pr_handle *) p;
2709   char *t;
2710 
2711   if (kind == DEBUG_PARM_REFERENCE
2712       || kind == DEBUG_PARM_REF_REG)
2713     {
2714       if (! pr_reference_type (p))
2715 	return FALSE;
2716     }
2717 
2718   if (! substitute_type (info, name))
2719     return FALSE;
2720 
2721   t = pop_type (info);
2722   if (t == NULL)
2723     return FALSE;
2724 
2725   if (! info->stack->method)
2726     {
2727       if (info->parameter != 1 && ! append_type (info, ", "))
2728 	return FALSE;
2729 
2730       if (kind == DEBUG_PARM_REG || kind == DEBUG_PARM_REF_REG)
2731 	if (! append_type (info, "register "))
2732 	  return FALSE;
2733 
2734       if (! append_type (info, t))
2735 	return FALSE;
2736     }
2737 
2738   free (t);
2739 
2740   ++info->parameter;
2741 
2742   return TRUE;
2743 }
2744 
2745 /* Start writing out a block.  */
2746 
2747 static bfd_boolean
2748 tg_start_block (void *p, bfd_vma addr)
2749 {
2750   struct pr_handle *info = (struct pr_handle *) p;
2751   char ab[22], kind, *partof;
2752   char *t;
2753   bfd_boolean local;
2754 
2755   if (info->parameter > 0)
2756     {
2757       info->parameter = 0;
2758 
2759       /* Delayed name.  */
2760       fprintf (info->f, "%s\t%s\t", info->stack->parents, info->filename);
2761       free (info->stack->parents);
2762 
2763       print_vma (addr, ab, TRUE, TRUE);
2764       translate_addresses (info->abfd, ab, info->f, info->syms);
2765       local = info->stack->flavor != NULL;
2766       if (info->stack->method && *info->stack->method)
2767 	{
2768 	  kind = 'm';
2769 	  partof = (char *) info->stack->method;
2770 	}
2771       else
2772 	{
2773 	  kind = 'f';
2774 	  partof = NULL;
2775 	  if (! info->stack->method && ! append_type (info, ")"))
2776 	    return FALSE;
2777 	}
2778       t = pop_type (info);
2779       if (t == NULL)
2780 	return FALSE;
2781       fprintf (info->f, ";\"\tkind:%c\ttype:%s", kind, t);
2782       if (local)
2783 	fputs ("\tfile:", info->f);
2784       if (partof)
2785 	{
2786 	  fprintf (info->f, "\tclass:%s", partof);
2787 	  free (partof);
2788 	}
2789       fputc ('\n', info->f);
2790     }
2791 
2792   return TRUE;
2793 }
2794 
2795 /* Write out line number information.  */
2796 
2797 static bfd_boolean
2798 tg_lineno (void *p ATTRIBUTE_UNUSED, const char *fname ATTRIBUTE_UNUSED,
2799 	   unsigned long lineno ATTRIBUTE_UNUSED,
2800 	   bfd_vma addr ATTRIBUTE_UNUSED)
2801 {
2802   return TRUE;
2803 }
2804 
2805 /* Finish writing out a block.  */
2806 
2807 static bfd_boolean
2808 tg_end_block (void *p ATTRIBUTE_UNUSED, bfd_vma addr ATTRIBUTE_UNUSED)
2809 {
2810   return TRUE;
2811 }
2812 
2813 /* Convert the visibility value into a human readable name.  */
2814 
2815 static const char *
2816 visibility_name (enum debug_visibility visibility)
2817 {
2818   const char *s;
2819 
2820   switch (visibility)
2821     {
2822     case DEBUG_VISIBILITY_PUBLIC:
2823       s = "public";
2824       break;
2825     case DEBUG_VISIBILITY_PRIVATE:
2826       s = "private";
2827       break;
2828     case DEBUG_VISIBILITY_PROTECTED:
2829       s = "protected";
2830       break;
2831     case DEBUG_VISIBILITY_IGNORE:
2832       s = "/* ignore */";
2833       break;
2834     default:
2835       abort ();
2836       return FALSE;
2837     }
2838   return s;
2839 }
2840