]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/binutils/binutils/prdbg.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / binutils / binutils / prdbg.c
1 /* prdbg.c -- Print out generic debugging information.
2    Copyright 1995, 1996, 1999, 2002, 2003, 2004, 2006, 2007
3    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 2 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 = 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   else
503     {
504       buf[0] = '0';
505       buf[1] = 'x';
506       sprintf_vma (buf + 2, vma);
507     }
508 }
509 \f
510 /* Start a new compilation unit.  */
511
512 static bfd_boolean
513 pr_start_compilation_unit (void *p, const char *filename)
514 {
515   struct pr_handle *info = (struct pr_handle *) p;
516
517   assert (info->indent == 0);
518
519   fprintf (info->f, "%s:\n", filename);
520
521   return TRUE;
522 }
523
524 /* Start a source file within a compilation unit.  */
525
526 static bfd_boolean
527 pr_start_source (void *p, const char *filename)
528 {
529   struct pr_handle *info = (struct pr_handle *) p;
530
531   assert (info->indent == 0);
532
533   fprintf (info->f, " %s:\n", filename);
534
535   return TRUE;
536 }
537
538 /* Push an empty type onto the type stack.  */
539
540 static bfd_boolean
541 pr_empty_type (void *p)
542 {
543   struct pr_handle *info = (struct pr_handle *) p;
544
545   return push_type (info, "<undefined>");
546 }
547
548 /* Push a void type onto the type stack.  */
549
550 static bfd_boolean
551 pr_void_type (void *p)
552 {
553   struct pr_handle *info = (struct pr_handle *) p;
554
555   return push_type (info, "void");
556 }
557
558 /* Push an integer type onto the type stack.  */
559
560 static bfd_boolean
561 pr_int_type (void *p, unsigned int size, bfd_boolean unsignedp)
562 {
563   struct pr_handle *info = (struct pr_handle *) p;
564   char ab[10];
565
566   sprintf (ab, "%sint%d", unsignedp ? "u" : "", size * 8);
567   return push_type (info, ab);
568 }
569
570 /* Push a floating type onto the type stack.  */
571
572 static bfd_boolean
573 pr_float_type (void *p, unsigned int size)
574 {
575   struct pr_handle *info = (struct pr_handle *) p;
576   char ab[10];
577
578   if (size == 4)
579     return push_type (info, "float");
580   else if (size == 8)
581     return push_type (info, "double");
582
583   sprintf (ab, "float%d", size * 8);
584   return push_type (info, ab);
585 }
586
587 /* Push a complex type onto the type stack.  */
588
589 static bfd_boolean
590 pr_complex_type (void *p, unsigned int size)
591 {
592   struct pr_handle *info = (struct pr_handle *) p;
593
594   if (! pr_float_type (p, size))
595     return FALSE;
596
597   return prepend_type (info, "complex ");
598 }
599
600 /* Push a bfd_boolean type onto the type stack.  */
601
602 static bfd_boolean
603 pr_bool_type (void *p, unsigned int size)
604 {
605   struct pr_handle *info = (struct pr_handle *) p;
606   char ab[10];
607
608   sprintf (ab, "bool%d", size * 8);
609
610   return push_type (info, ab);
611 }
612
613 /* Push an enum type onto the type stack.  */
614
615 static bfd_boolean
616 pr_enum_type (void *p, const char *tag, const char **names,
617               bfd_signed_vma *values)
618 {
619   struct pr_handle *info = (struct pr_handle *) p;
620   unsigned int i;
621   bfd_signed_vma val;
622
623   if (! push_type (info, "enum "))
624     return FALSE;
625   if (tag != NULL)
626     {
627       if (! append_type (info, tag)
628           || ! append_type (info, " "))
629         return FALSE;
630     }
631   if (! append_type (info, "{ "))
632     return FALSE;
633
634   if (names == NULL)
635     {
636       if (! append_type (info, "/* undefined */"))
637         return FALSE;
638     }
639   else
640     {
641       val = 0;
642       for (i = 0; names[i] != NULL; i++)
643         {
644           if (i > 0)
645             {
646               if (! append_type (info, ", "))
647                 return FALSE;
648             }
649
650           if (! append_type (info, names[i]))
651             return FALSE;
652
653           if (values[i] != val)
654             {
655               char ab[20];
656
657               print_vma (values[i], ab, FALSE, FALSE);
658               if (! append_type (info, " = ")
659                   || ! append_type (info, ab))
660                 return FALSE;
661               val = values[i];
662             }
663
664           ++val;
665         }
666     }
667
668   return append_type (info, " }");
669 }
670
671 /* Turn the top type on the stack into a pointer.  */
672
673 static bfd_boolean
674 pr_pointer_type (void *p)
675 {
676   struct pr_handle *info = (struct pr_handle *) p;
677   char *s;
678
679   assert (info->stack != NULL);
680
681   s = strchr (info->stack->type, '|');
682   if (s != NULL && s[1] == '[')
683     return substitute_type (info, "(*|)");
684   return substitute_type (info, "*|");
685 }
686
687 /* Turn the top type on the stack into a function returning that type.  */
688
689 static bfd_boolean
690 pr_function_type (void *p, int argcount, bfd_boolean varargs)
691 {
692   struct pr_handle *info = (struct pr_handle *) p;
693   char **arg_types;
694   unsigned int len;
695   char *s;
696
697   assert (info->stack != NULL);
698
699   len = 10;
700
701   if (argcount <= 0)
702     {
703       arg_types = NULL;
704       len += 15;
705     }
706   else
707     {
708       int i;
709
710       arg_types = (char **) xmalloc (argcount * sizeof *arg_types);
711       for (i = argcount - 1; i >= 0; i--)
712         {
713           if (! substitute_type (info, ""))
714             return FALSE;
715           arg_types[i] = pop_type (info);
716           if (arg_types[i] == NULL)
717             return FALSE;
718           len += strlen (arg_types[i]) + 2;
719         }
720       if (varargs)
721         len += 5;
722     }
723
724   /* Now the return type is on the top of the stack.  */
725
726   s = xmalloc (len);
727   LITSTRCPY (s, "(|) (");
728
729   if (argcount < 0)
730     strcat (s, "/* unknown */");
731   else
732     {
733       int i;
734
735       for (i = 0; i < argcount; i++)
736         {
737           if (i > 0)
738             strcat (s, ", ");
739           strcat (s, arg_types[i]);
740         }
741       if (varargs)
742         {
743           if (i > 0)
744             strcat (s, ", ");
745           strcat (s, "...");
746         }
747       if (argcount > 0)
748         free (arg_types);
749     }
750
751   strcat (s, ")");
752
753   if (! substitute_type (info, s))
754     return FALSE;
755
756   free (s);
757
758   return TRUE;
759 }
760
761 /* Turn the top type on the stack into a reference to that type.  */
762
763 static bfd_boolean
764 pr_reference_type (void *p)
765 {
766   struct pr_handle *info = (struct pr_handle *) p;
767
768   assert (info->stack != NULL);
769
770   return substitute_type (info, "&|");
771 }
772
773 /* Make a range type.  */
774
775 static bfd_boolean
776 pr_range_type (void *p, bfd_signed_vma lower, bfd_signed_vma upper)
777 {
778   struct pr_handle *info = (struct pr_handle *) p;
779   char abl[20], abu[20];
780
781   assert (info->stack != NULL);
782
783   if (! substitute_type (info, ""))
784     return FALSE;
785
786   print_vma (lower, abl, FALSE, FALSE);
787   print_vma (upper, abu, FALSE, FALSE);
788
789   return (prepend_type (info, "range (")
790           && append_type (info, "):")
791           && append_type (info, abl)
792           && append_type (info, ":")
793           && append_type (info, abu));
794 }
795
796 /* Make an array type.  */
797
798 static bfd_boolean
799 pr_array_type (void *p, bfd_signed_vma lower, bfd_signed_vma upper,
800                bfd_boolean stringp)
801 {
802   struct pr_handle *info = (struct pr_handle *) p;
803   char *range_type;
804   char abl[20], abu[20], ab[50];
805
806   range_type = pop_type (info);
807   if (range_type == NULL)
808     return FALSE;
809
810   if (lower == 0)
811     {
812       if (upper == -1)
813         sprintf (ab, "|[]");
814       else
815         {
816           print_vma (upper + 1, abu, FALSE, FALSE);
817           sprintf (ab, "|[%s]", abu);
818         }
819     }
820   else
821     {
822       print_vma (lower, abl, FALSE, FALSE);
823       print_vma (upper, abu, FALSE, FALSE);
824       sprintf (ab, "|[%s:%s]", abl, abu);
825     }
826
827   if (! substitute_type (info, ab))
828     return FALSE;
829
830   if (strcmp (range_type, "int") != 0)
831     {
832       if (! append_type (info, ":")
833           || ! append_type (info, range_type))
834         return FALSE;
835     }
836
837   if (stringp)
838     {
839       if (! append_type (info, " /* string */"))
840         return FALSE;
841     }
842
843   return TRUE;
844 }
845
846 /* Make a set type.  */
847
848 static bfd_boolean
849 pr_set_type (void *p, bfd_boolean bitstringp)
850 {
851   struct pr_handle *info = (struct pr_handle *) p;
852
853   if (! substitute_type (info, ""))
854     return FALSE;
855
856   if (! prepend_type (info, "set { ")
857       || ! append_type (info, " }"))
858     return FALSE;
859
860   if (bitstringp)
861     {
862       if (! append_type (info, "/* bitstring */"))
863         return FALSE;
864     }
865
866   return TRUE;
867 }
868
869 /* Make an offset type.  */
870
871 static bfd_boolean
872 pr_offset_type (void *p)
873 {
874   struct pr_handle *info = (struct pr_handle *) p;
875   char *t;
876
877   if (! substitute_type (info, ""))
878     return FALSE;
879
880   t = pop_type (info);
881   if (t == NULL)
882     return FALSE;
883
884   return (substitute_type (info, "")
885           && prepend_type (info, " ")
886           && prepend_type (info, t)
887           && append_type (info, "::|"));
888 }
889
890 /* Make a method type.  */
891
892 static bfd_boolean
893 pr_method_type (void *p, bfd_boolean domain, int argcount, bfd_boolean varargs)
894 {
895   struct pr_handle *info = (struct pr_handle *) p;
896   unsigned int len;
897   char *domain_type;
898   char **arg_types;
899   char *s;
900
901   len = 10;
902
903   if (! domain)
904     domain_type = NULL;
905   else
906     {
907       if (! substitute_type (info, ""))
908         return FALSE;
909       domain_type = pop_type (info);
910       if (domain_type == NULL)
911         return FALSE;
912       if (CONST_STRNEQ (domain_type, "class ")
913           && strchr (domain_type + sizeof "class " - 1, ' ') == NULL)
914         domain_type += sizeof "class " - 1;
915       else if (CONST_STRNEQ (domain_type, "union class ")
916                && (strchr (domain_type + sizeof "union class " - 1, ' ')
917                    == NULL))
918         domain_type += sizeof "union class " - 1;
919       len += strlen (domain_type);
920     }
921
922   if (argcount <= 0)
923     {
924       arg_types = NULL;
925       len += 15;
926     }
927   else
928     {
929       int i;
930
931       arg_types = (char **) xmalloc (argcount * sizeof *arg_types);
932       for (i = argcount - 1; i >= 0; i--)
933         {
934           if (! substitute_type (info, ""))
935             return FALSE;
936           arg_types[i] = pop_type (info);
937           if (arg_types[i] == NULL)
938             return FALSE;
939           len += strlen (arg_types[i]) + 2;
940         }
941       if (varargs)
942         len += 5;
943     }
944
945   /* Now the return type is on the top of the stack.  */
946
947   s = (char *) xmalloc (len);
948   if (! domain)
949     *s = '\0';
950   else
951     strcpy (s, domain_type);
952   strcat (s, "::| (");
953
954   if (argcount < 0)
955     strcat (s, "/* unknown */");
956   else
957     {
958       int i;
959
960       for (i = 0; i < argcount; i++)
961         {
962           if (i > 0)
963             strcat (s, ", ");
964           strcat (s, arg_types[i]);
965         }
966       if (varargs)
967         {
968           if (i > 0)
969             strcat (s, ", ");
970           strcat (s, "...");
971         }
972       if (argcount > 0)
973         free (arg_types);
974     }
975
976   strcat (s, ")");
977
978   if (! substitute_type (info, s))
979     return FALSE;
980
981   free (s);
982
983   return TRUE;
984 }
985
986 /* Make a const qualified type.  */
987
988 static bfd_boolean
989 pr_const_type (void *p)
990 {
991   struct pr_handle *info = (struct pr_handle *) p;
992
993   return substitute_type (info, "const |");
994 }
995
996 /* Make a volatile qualified type.  */
997
998 static bfd_boolean
999 pr_volatile_type (void *p)
1000 {
1001   struct pr_handle *info = (struct pr_handle *) p;
1002
1003   return substitute_type (info, "volatile |");
1004 }
1005
1006 /* Start accumulating a struct type.  */
1007
1008 static bfd_boolean
1009 pr_start_struct_type (void *p, const char *tag, unsigned int id,
1010                       bfd_boolean structp, unsigned int size)
1011 {
1012   struct pr_handle *info = (struct pr_handle *) p;
1013
1014   info->indent += 2;
1015
1016   if (! push_type (info, structp ? "struct " : "union "))
1017     return FALSE;
1018   if (tag != NULL)
1019     {
1020       if (! append_type (info, tag))
1021         return FALSE;
1022     }
1023   else
1024     {
1025       char idbuf[20];
1026
1027       sprintf (idbuf, "%%anon%u", id);
1028       if (! append_type (info, idbuf))
1029         return FALSE;
1030     }
1031
1032   if (! append_type (info, " {"))
1033     return FALSE;
1034   if (size != 0 || tag != NULL)
1035     {
1036       char ab[30];
1037
1038       if (! append_type (info, " /*"))
1039         return FALSE;
1040
1041       if (size != 0)
1042         {
1043           sprintf (ab, " size %u", size);
1044           if (! append_type (info, ab))
1045             return FALSE;
1046         }
1047       if (tag != NULL)
1048         {
1049           sprintf (ab, " id %u", id);
1050           if (! append_type (info, ab))
1051             return FALSE;
1052         }
1053       if (! append_type (info, " */"))
1054         return FALSE;
1055     }
1056   if (! append_type (info, "\n"))
1057     return FALSE;
1058
1059   info->stack->visibility = DEBUG_VISIBILITY_PUBLIC;
1060
1061   return indent_type (info);
1062 }
1063
1064 /* Output the visibility of a field in a struct.  */
1065
1066 static bfd_boolean
1067 pr_fix_visibility (struct pr_handle *info, enum debug_visibility visibility)
1068 {
1069   const char *s = NULL;
1070   char *t;
1071   unsigned int len;
1072
1073   assert (info->stack != NULL);
1074
1075   if (info->stack->visibility == visibility)
1076     return TRUE;
1077
1078   switch (visibility)
1079     {
1080     case DEBUG_VISIBILITY_PUBLIC:
1081       s = "public";
1082       break;
1083     case DEBUG_VISIBILITY_PRIVATE:
1084       s = "private";
1085       break;
1086     case DEBUG_VISIBILITY_PROTECTED:
1087       s = "protected";
1088       break;
1089     case DEBUG_VISIBILITY_IGNORE:
1090       s = "/* ignore */";
1091       break;
1092     default:
1093       abort ();
1094       return FALSE;
1095     }
1096
1097   /* Trim off a trailing space in the struct string, to make the
1098      output look a bit better, then stick on the visibility string.  */
1099
1100   t = info->stack->type;
1101   len = strlen (t);
1102   assert (t[len - 1] == ' ');
1103   t[len - 1] = '\0';
1104
1105   if (! append_type (info, s)
1106       || ! append_type (info, ":\n")
1107       || ! indent_type (info))
1108     return FALSE;
1109
1110   info->stack->visibility = visibility;
1111
1112   return TRUE;
1113 }
1114
1115 /* Add a field to a struct type.  */
1116
1117 static bfd_boolean
1118 pr_struct_field (void *p, const char *name, bfd_vma bitpos, bfd_vma bitsize,
1119                  enum debug_visibility visibility)
1120 {
1121   struct pr_handle *info = (struct pr_handle *) p;
1122   char ab[20];
1123   char *t;
1124
1125   if (! substitute_type (info, name))
1126     return FALSE;
1127
1128   if (! append_type (info, "; /* "))
1129     return FALSE;
1130
1131   if (bitsize != 0)
1132     {
1133       print_vma (bitsize, ab, TRUE, FALSE);
1134       if (! append_type (info, "bitsize ")
1135           || ! append_type (info, ab)
1136           || ! append_type (info, ", "))
1137         return FALSE;
1138     }
1139
1140   print_vma (bitpos, ab, TRUE, FALSE);
1141   if (! append_type (info, "bitpos ")
1142       || ! append_type (info, ab)
1143       || ! append_type (info, " */\n")
1144       || ! indent_type (info))
1145     return FALSE;
1146
1147   t = pop_type (info);
1148   if (t == NULL)
1149     return FALSE;
1150
1151   if (! pr_fix_visibility (info, visibility))
1152     return FALSE;
1153
1154   return append_type (info, t);
1155 }
1156
1157 /* Finish a struct type.  */
1158
1159 static bfd_boolean
1160 pr_end_struct_type (void *p)
1161 {
1162   struct pr_handle *info = (struct pr_handle *) p;
1163   char *s;
1164
1165   assert (info->stack != NULL);
1166   assert (info->indent >= 2);
1167
1168   info->indent -= 2;
1169
1170   /* Change the trailing indentation to have a close brace.  */
1171   s = info->stack->type + strlen (info->stack->type) - 2;
1172   assert (s[0] == ' ' && s[1] == ' ' && s[2] == '\0');
1173
1174   *s++ = '}';
1175   *s = '\0';
1176
1177   return TRUE;
1178 }
1179
1180 /* Start a class type.  */
1181
1182 static bfd_boolean
1183 pr_start_class_type (void *p, const char *tag, unsigned int id,
1184                      bfd_boolean structp, unsigned int size,
1185                      bfd_boolean vptr, bfd_boolean ownvptr)
1186 {
1187   struct pr_handle *info = (struct pr_handle *) p;
1188   char *tv = NULL;
1189
1190   info->indent += 2;
1191
1192   if (vptr && ! ownvptr)
1193     {
1194       tv = pop_type (info);
1195       if (tv == NULL)
1196         return FALSE;
1197     }
1198
1199   if (! push_type (info, structp ? "class " : "union class "))
1200     return FALSE;
1201   if (tag != NULL)
1202     {
1203       if (! append_type (info, tag))
1204         return FALSE;
1205     }
1206   else
1207     {
1208       char idbuf[20];
1209
1210       sprintf (idbuf, "%%anon%u", id);
1211       if (! append_type (info, idbuf))
1212         return FALSE;
1213     }
1214
1215   if (! append_type (info, " {"))
1216     return FALSE;
1217   if (size != 0 || vptr || ownvptr || tag != NULL)
1218     {
1219       if (! append_type (info, " /*"))
1220         return FALSE;
1221
1222       if (size != 0)
1223         {
1224           char ab[20];
1225
1226           sprintf (ab, "%u", size);
1227           if (! append_type (info, " size ")
1228               || ! append_type (info, ab))
1229             return FALSE;
1230         }
1231
1232       if (vptr)
1233         {
1234           if (! append_type (info, " vtable "))
1235             return FALSE;
1236           if (ownvptr)
1237             {
1238               if (! append_type (info, "self "))
1239                 return FALSE;
1240             }
1241           else
1242             {
1243               if (! append_type (info, tv)
1244                   || ! append_type (info, " "))
1245                 return FALSE;
1246             }
1247         }
1248
1249       if (tag != NULL)
1250         {
1251           char ab[30];
1252
1253           sprintf (ab, " id %u", id);
1254           if (! append_type (info, ab))
1255             return FALSE;
1256         }
1257
1258       if (! append_type (info, " */"))
1259         return FALSE;
1260     }
1261
1262   info->stack->visibility = DEBUG_VISIBILITY_PRIVATE;
1263
1264   return (append_type (info, "\n")
1265           && indent_type (info));
1266 }
1267
1268 /* Add a static member to a class.  */
1269
1270 static bfd_boolean
1271 pr_class_static_member (void *p, const char *name, const char *physname,
1272                         enum debug_visibility visibility)
1273 {
1274   struct pr_handle *info = (struct pr_handle *) p;
1275   char *t;
1276
1277   if (! substitute_type (info, name))
1278     return FALSE;
1279
1280   if (! prepend_type (info, "static ")
1281       || ! append_type (info, "; /* ")
1282       || ! append_type (info, physname)
1283       || ! append_type (info, " */\n")
1284       || ! indent_type (info))
1285     return FALSE;
1286
1287   t = pop_type (info);
1288   if (t == NULL)
1289     return FALSE;
1290
1291   if (! pr_fix_visibility (info, visibility))
1292     return FALSE;
1293
1294   return append_type (info, t);
1295 }
1296
1297 /* Add a base class to a class.  */
1298
1299 static bfd_boolean
1300 pr_class_baseclass (void *p, bfd_vma bitpos, bfd_boolean virtual,
1301                     enum debug_visibility visibility)
1302 {
1303   struct pr_handle *info = (struct pr_handle *) p;
1304   char *t;
1305   const char *prefix;
1306   char ab[20];
1307   char *s, *l, *n;
1308
1309   assert (info->stack != NULL && info->stack->next != NULL);
1310
1311   if (! substitute_type (info, ""))
1312     return FALSE;
1313
1314   t = pop_type (info);
1315   if (t == NULL)
1316     return FALSE;
1317
1318   if (CONST_STRNEQ (t, "class "))
1319     t += sizeof "class " - 1;
1320
1321   /* Push it back on to take advantage of the prepend_type and
1322      append_type routines.  */
1323   if (! push_type (info, t))
1324     return FALSE;
1325
1326   if (virtual)
1327     {
1328       if (! prepend_type (info, "virtual "))
1329         return FALSE;
1330     }
1331
1332   switch (visibility)
1333     {
1334     case DEBUG_VISIBILITY_PUBLIC:
1335       prefix = "public ";
1336       break;
1337     case DEBUG_VISIBILITY_PROTECTED:
1338       prefix = "protected ";
1339       break;
1340     case DEBUG_VISIBILITY_PRIVATE:
1341       prefix = "private ";
1342       break;
1343     default:
1344       prefix = "/* unknown visibility */ ";
1345       break;
1346     }
1347
1348   if (! prepend_type (info, prefix))
1349     return FALSE;
1350
1351   if (bitpos != 0)
1352     {
1353       print_vma (bitpos, ab, TRUE, FALSE);
1354       if (! append_type (info, " /* bitpos ")
1355           || ! append_type (info, ab)
1356           || ! append_type (info, " */"))
1357         return FALSE;
1358     }
1359
1360   /* Now the top of the stack is something like "public A / * bitpos
1361      10 * /".  The next element on the stack is something like "class
1362      xx { / * size 8 * /\n...".  We want to substitute the top of the
1363      stack in before the {.  */
1364   s = strchr (info->stack->next->type, '{');
1365   assert (s != NULL);
1366   --s;
1367
1368   /* If there is already a ':', then we already have a baseclass, and
1369      we must append this one after a comma.  */
1370   for (l = info->stack->next->type; l != s; l++)
1371     if (*l == ':')
1372       break;
1373   if (! prepend_type (info, l == s ? " : " : ", "))
1374     return FALSE;
1375
1376   t = pop_type (info);
1377   if (t == NULL)
1378     return FALSE;
1379
1380   n = (char *) xmalloc (strlen (info->stack->type) + strlen (t) + 1);
1381   memcpy (n, info->stack->type, s - info->stack->type);
1382   strcpy (n + (s - info->stack->type), t);
1383   strcat (n, s);
1384
1385   free (info->stack->type);
1386   info->stack->type = n;
1387
1388   free (t);
1389
1390   return TRUE;
1391 }
1392
1393 /* Start adding a method to a class.  */
1394
1395 static bfd_boolean
1396 pr_class_start_method (void *p, const char *name)
1397 {
1398   struct pr_handle *info = (struct pr_handle *) p;
1399
1400   assert (info->stack != NULL);
1401   info->stack->method = name;
1402   return TRUE;
1403 }
1404
1405 /* Add a variant to a method.  */
1406
1407 static bfd_boolean
1408 pr_class_method_variant (void *p, const char *physname,
1409                          enum debug_visibility visibility,
1410                          bfd_boolean constp, bfd_boolean volatilep,
1411                          bfd_vma voffset, bfd_boolean context)
1412 {
1413   struct pr_handle *info = (struct pr_handle *) p;
1414   char *method_type;
1415   char *context_type;
1416
1417   assert (info->stack != NULL);
1418   assert (info->stack->next != NULL);
1419
1420   /* Put the const and volatile qualifiers on the type.  */
1421   if (volatilep)
1422     {
1423       if (! append_type (info, " volatile"))
1424         return FALSE;
1425     }
1426   if (constp)
1427     {
1428       if (! append_type (info, " const"))
1429         return FALSE;
1430     }
1431
1432   /* Stick the name of the method into its type.  */
1433   if (! substitute_type (info,
1434                          (context
1435                           ? info->stack->next->next->method
1436                           : info->stack->next->method)))
1437     return FALSE;
1438
1439   /* Get the type.  */
1440   method_type = pop_type (info);
1441   if (method_type == NULL)
1442     return FALSE;
1443
1444   /* Pull off the context type if there is one.  */
1445   if (! context)
1446     context_type = NULL;
1447   else
1448     {
1449       context_type = pop_type (info);
1450       if (context_type == NULL)
1451         return FALSE;
1452     }
1453
1454   /* Now the top of the stack is the class.  */
1455
1456   if (! pr_fix_visibility (info, visibility))
1457     return FALSE;
1458
1459   if (! append_type (info, method_type)
1460       || ! append_type (info, " /* ")
1461       || ! append_type (info, physname)
1462       || ! append_type (info, " "))
1463     return FALSE;
1464   if (context || voffset != 0)
1465     {
1466       char ab[20];
1467
1468       if (context)
1469         {
1470           if (! append_type (info, "context ")
1471               || ! append_type (info, context_type)
1472               || ! append_type (info, " "))
1473             return FALSE;
1474         }
1475       print_vma (voffset, ab, TRUE, FALSE);
1476       if (! append_type (info, "voffset ")
1477           || ! append_type (info, ab))
1478         return FALSE;
1479     }
1480
1481   return (append_type (info, " */;\n")
1482           && indent_type (info));
1483 }
1484
1485 /* Add a static variant to a method.  */
1486
1487 static bfd_boolean
1488 pr_class_static_method_variant (void *p, const char *physname,
1489                                 enum debug_visibility visibility,
1490                                 bfd_boolean constp, bfd_boolean volatilep)
1491 {
1492   struct pr_handle *info = (struct pr_handle *) p;
1493   char *method_type;
1494
1495   assert (info->stack != NULL);
1496   assert (info->stack->next != NULL);
1497   assert (info->stack->next->method != NULL);
1498
1499   /* Put the const and volatile qualifiers on the type.  */
1500   if (volatilep)
1501     {
1502       if (! append_type (info, " volatile"))
1503         return FALSE;
1504     }
1505   if (constp)
1506     {
1507       if (! append_type (info, " const"))
1508         return FALSE;
1509     }
1510
1511   /* Mark it as static.  */
1512   if (! prepend_type (info, "static "))
1513     return FALSE;
1514
1515   /* Stick the name of the method into its type.  */
1516   if (! substitute_type (info, info->stack->next->method))
1517     return FALSE;
1518
1519   /* Get the type.  */
1520   method_type = pop_type (info);
1521   if (method_type == NULL)
1522     return FALSE;
1523
1524   /* Now the top of the stack is the class.  */
1525
1526   if (! pr_fix_visibility (info, visibility))
1527     return FALSE;
1528
1529   return (append_type (info, method_type)
1530           && append_type (info, " /* ")
1531           && append_type (info, physname)
1532           && append_type (info, " */;\n")
1533           && indent_type (info));
1534 }
1535
1536 /* Finish up a method.  */
1537
1538 static bfd_boolean
1539 pr_class_end_method (void *p)
1540 {
1541   struct pr_handle *info = (struct pr_handle *) p;
1542
1543   info->stack->method = NULL;
1544   return TRUE;
1545 }
1546
1547 /* Finish up a class.  */
1548
1549 static bfd_boolean
1550 pr_end_class_type (void *p)
1551 {
1552   return pr_end_struct_type (p);
1553 }
1554
1555 /* Push a type on the stack using a typedef name.  */
1556
1557 static bfd_boolean
1558 pr_typedef_type (void *p, const char *name)
1559 {
1560   struct pr_handle *info = (struct pr_handle *) p;
1561
1562   return push_type (info, name);
1563 }
1564
1565 /* Push a type on the stack using a tag name.  */
1566
1567 static bfd_boolean
1568 pr_tag_type (void *p, const char *name, unsigned int id,
1569              enum debug_type_kind kind)
1570 {
1571   struct pr_handle *info = (struct pr_handle *) p;
1572   const char *t, *tag;
1573   char idbuf[20];
1574
1575   switch (kind)
1576     {
1577     case DEBUG_KIND_STRUCT:
1578       t = "struct ";
1579       break;
1580     case DEBUG_KIND_UNION:
1581       t = "union ";
1582       break;
1583     case DEBUG_KIND_ENUM:
1584       t = "enum ";
1585       break;
1586     case DEBUG_KIND_CLASS:
1587       t = "class ";
1588       break;
1589     case DEBUG_KIND_UNION_CLASS:
1590       t = "union class ";
1591       break;
1592     default:
1593       abort ();
1594       return FALSE;
1595     }
1596
1597   if (! push_type (info, t))
1598     return FALSE;
1599   if (name != NULL)
1600     tag = name;
1601   else
1602     {
1603       sprintf (idbuf, "%%anon%u", id);
1604       tag = idbuf;
1605     }
1606
1607   if (! append_type (info, tag))
1608     return FALSE;
1609   if (name != NULL && kind != DEBUG_KIND_ENUM)
1610     {
1611       sprintf (idbuf, " /* id %u */", id);
1612       if (! append_type (info, idbuf))
1613         return FALSE;
1614     }
1615
1616   return TRUE;
1617 }
1618
1619 /* Output a typedef.  */
1620
1621 static bfd_boolean
1622 pr_typdef (void *p, const char *name)
1623 {
1624   struct pr_handle *info = (struct pr_handle *) p;
1625   char *s;
1626
1627   if (! substitute_type (info, name))
1628     return FALSE;
1629
1630   s = pop_type (info);
1631   if (s == NULL)
1632     return FALSE;
1633
1634   indent (info);
1635   fprintf (info->f, "typedef %s;\n", s);
1636
1637   free (s);
1638
1639   return TRUE;
1640 }
1641
1642 /* Output a tag.  The tag should already be in the string on the
1643    stack, so all we have to do here is print it out.  */
1644
1645 static bfd_boolean
1646 pr_tag (void *p, const char *name ATTRIBUTE_UNUSED)
1647 {
1648   struct pr_handle *info = (struct pr_handle *) p;
1649   char *t;
1650
1651   t = pop_type (info);
1652   if (t == NULL)
1653     return FALSE;
1654
1655   indent (info);
1656   fprintf (info->f, "%s;\n", t);
1657
1658   free (t);
1659
1660   return TRUE;
1661 }
1662
1663 /* Output an integer constant.  */
1664
1665 static bfd_boolean
1666 pr_int_constant (void *p, const char *name, bfd_vma val)
1667 {
1668   struct pr_handle *info = (struct pr_handle *) p;
1669   char ab[20];
1670
1671   indent (info);
1672   print_vma (val, ab, FALSE, FALSE);
1673   fprintf (info->f, "const int %s = %s;\n", name, ab);
1674   return TRUE;
1675 }
1676
1677 /* Output a floating point constant.  */
1678
1679 static bfd_boolean
1680 pr_float_constant (void *p, const char *name, double val)
1681 {
1682   struct pr_handle *info = (struct pr_handle *) p;
1683
1684   indent (info);
1685   fprintf (info->f, "const double %s = %g;\n", name, val);
1686   return TRUE;
1687 }
1688
1689 /* Output a typed constant.  */
1690
1691 static bfd_boolean
1692 pr_typed_constant (void *p, const char *name, bfd_vma val)
1693 {
1694   struct pr_handle *info = (struct pr_handle *) p;
1695   char *t;
1696   char ab[20];
1697
1698   t = pop_type (info);
1699   if (t == NULL)
1700     return FALSE;
1701
1702   indent (info);
1703   print_vma (val, ab, FALSE, FALSE);
1704   fprintf (info->f, "const %s %s = %s;\n", t, name, ab);
1705
1706   free (t);
1707
1708   return TRUE;
1709 }
1710
1711 /* Output a variable.  */
1712
1713 static bfd_boolean
1714 pr_variable (void *p, const char *name, enum debug_var_kind kind,
1715              bfd_vma val)
1716 {
1717   struct pr_handle *info = (struct pr_handle *) p;
1718   char *t;
1719   char ab[20];
1720
1721   if (! substitute_type (info, name))
1722     return FALSE;
1723
1724   t = pop_type (info);
1725   if (t == NULL)
1726     return FALSE;
1727
1728   indent (info);
1729   switch (kind)
1730     {
1731     case DEBUG_STATIC:
1732     case DEBUG_LOCAL_STATIC:
1733       fprintf (info->f, "static ");
1734       break;
1735     case DEBUG_REGISTER:
1736       fprintf (info->f, "register ");
1737       break;
1738     default:
1739       break;
1740     }
1741   print_vma (val, ab, TRUE, TRUE);
1742   fprintf (info->f, "%s /* %s */;\n", t, ab);
1743
1744   free (t);
1745
1746   return TRUE;
1747 }
1748
1749 /* Start outputting a function.  */
1750
1751 static bfd_boolean
1752 pr_start_function (void *p, const char *name, bfd_boolean global)
1753 {
1754   struct pr_handle *info = (struct pr_handle *) p;
1755   char *t;
1756
1757   if (! substitute_type (info, name))
1758     return FALSE;
1759
1760   t = pop_type (info);
1761   if (t == NULL)
1762     return FALSE;
1763
1764   indent (info);
1765   if (! global)
1766     fprintf (info->f, "static ");
1767   fprintf (info->f, "%s (", t);
1768
1769   info->parameter = 1;
1770
1771   return TRUE;
1772 }
1773
1774 /* Output a function parameter.  */
1775
1776 static bfd_boolean
1777 pr_function_parameter (void *p, const char *name,
1778                        enum debug_parm_kind kind, bfd_vma val)
1779 {
1780   struct pr_handle *info = (struct pr_handle *) p;
1781   char *t;
1782   char ab[20];
1783
1784   if (kind == DEBUG_PARM_REFERENCE
1785       || kind == DEBUG_PARM_REF_REG)
1786     {
1787       if (! pr_reference_type (p))
1788         return FALSE;
1789     }
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   if (info->parameter != 1)
1799     fprintf (info->f, ", ");
1800
1801   if (kind == DEBUG_PARM_REG || kind == DEBUG_PARM_REF_REG)
1802     fprintf (info->f, "register ");
1803
1804   print_vma (val, ab, TRUE, TRUE);
1805   fprintf (info->f, "%s /* %s */", t, ab);
1806
1807   free (t);
1808
1809   ++info->parameter;
1810
1811   return TRUE;
1812 }
1813
1814 /* Start writing out a block.  */
1815
1816 static bfd_boolean
1817 pr_start_block (void *p, bfd_vma addr)
1818 {
1819   struct pr_handle *info = (struct pr_handle *) p;
1820   char ab[20];
1821
1822   if (info->parameter > 0)
1823     {
1824       fprintf (info->f, ")\n");
1825       info->parameter = 0;
1826     }
1827
1828   indent (info);
1829   print_vma (addr, ab, TRUE, TRUE);
1830   fprintf (info->f, "{ /* %s */\n", ab);
1831
1832   info->indent += 2;
1833
1834   return TRUE;
1835 }
1836
1837 /* Write out line number information.  */
1838
1839 static bfd_boolean
1840 pr_lineno (void *p, const char *filename, unsigned long lineno, bfd_vma addr)
1841 {
1842   struct pr_handle *info = (struct pr_handle *) p;
1843   char ab[20];
1844
1845   indent (info);
1846   print_vma (addr, ab, TRUE, TRUE);
1847   fprintf (info->f, "/* file %s line %lu addr %s */\n", filename, lineno, ab);
1848
1849   return TRUE;
1850 }
1851
1852 /* Finish writing out a block.  */
1853
1854 static bfd_boolean
1855 pr_end_block (void *p, bfd_vma addr)
1856 {
1857   struct pr_handle *info = (struct pr_handle *) p;
1858   char ab[20];
1859
1860   info->indent -= 2;
1861
1862   indent (info);
1863   print_vma (addr, ab, TRUE, TRUE);
1864   fprintf (info->f, "} /* %s */\n", ab);
1865
1866   return TRUE;
1867 }
1868
1869 /* Finish writing out a function.  */
1870
1871 static bfd_boolean
1872 pr_end_function (void *p ATTRIBUTE_UNUSED)
1873 {
1874   return TRUE;
1875 }
1876 \f
1877 /* Tags style generation functions start here.  */
1878
1879 /* Variables for address to line translation.  */
1880 static bfd_vma pc;
1881 static const char *filename;
1882 static const char *functionname;
1883 static unsigned int line;
1884 static bfd_boolean found;
1885
1886 /* Look for an address in a section.  This is called via
1887    bfd_map_over_sections.  */
1888
1889 static void
1890 find_address_in_section (bfd *abfd, asection *section, void *data)
1891 {
1892   bfd_vma vma;
1893   bfd_size_type size;
1894   asymbol **syms = (asymbol **) data;
1895
1896   if (found)
1897     return;
1898
1899   if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0)
1900     return;
1901
1902   vma = bfd_get_section_vma (abfd, section);
1903   if (pc < vma)
1904     return;
1905
1906   size = bfd_get_section_size (section);
1907   if (pc >= vma + size)
1908     return;
1909
1910   found = bfd_find_nearest_line (abfd, section, syms, pc - vma,
1911                                  &filename, &functionname, &line);
1912 }
1913
1914 static void
1915 translate_addresses (bfd *abfd, char *addr_hex, FILE *f, asymbol **syms)
1916 {
1917   pc = bfd_scan_vma (addr_hex, NULL, 16);
1918   found = FALSE;
1919   bfd_map_over_sections (abfd, find_address_in_section, syms);
1920
1921   if (! found)
1922     fprintf (f, "??");
1923   else
1924     fprintf (f, "%u", line);
1925 }
1926
1927 /* Start a new compilation unit.  */
1928
1929 static bfd_boolean
1930 tg_start_compilation_unit (void * p, const char *filename ATTRIBUTE_UNUSED)
1931 {
1932   struct pr_handle *info = (struct pr_handle *) p;
1933
1934   fprintf (stderr, "New compilation unit: %s\n", filename);
1935
1936   free (info->filename);
1937   /* Should it be relative? best way to do it here?.  */
1938   info->filename = strdup (filename);
1939
1940   return TRUE;
1941 }
1942
1943 /* Start a source file within a compilation unit.  */
1944
1945 static bfd_boolean
1946 tg_start_source (void *p, const char *filename)
1947 {
1948   struct pr_handle *info = (struct pr_handle *) p;
1949
1950   free (info->filename);
1951   /* Should it be relative? best way to do it here?.  */
1952   info->filename = strdup (filename);
1953
1954   return TRUE;
1955 }
1956
1957 /* Push an enum type onto the type stack.  */
1958
1959 static bfd_boolean
1960 tg_enum_type (void *p, const char *tag, const char **names,
1961               bfd_signed_vma *values)
1962 {
1963   struct pr_handle *info = (struct pr_handle *) p;
1964   unsigned int i;
1965   const char *name;
1966   char ab[20];
1967
1968   if (! pr_enum_type (p, tag, names, values))
1969     return FALSE;
1970
1971   name = tag ? tag : "unknown";
1972   /* Generate an entry for the enum.  */
1973   if (tag)
1974     fprintf (info->f, "%s\t%s\t0;\"\tkind:e\ttype:%s\n", tag,
1975              info->filename, info->stack->type);
1976
1977   /* Generate entries for the values.  */
1978   if (names != NULL)
1979     {
1980       for (i = 0; names[i] != NULL; i++)
1981         {
1982           print_vma (values[i], ab, FALSE, FALSE);
1983           fprintf (info->f, "%s\t%s\t0;\"\tkind:g\tenum:%s\tvalue:%s\n",
1984                    names[i], info->filename, name, ab);
1985         }
1986     }
1987
1988   return TRUE;
1989 }
1990
1991 /* Start accumulating a struct type.  */
1992
1993 static bfd_boolean
1994 tg_start_struct_type (void *p, const char *tag, unsigned int id,
1995                       bfd_boolean structp,
1996                       unsigned int size ATTRIBUTE_UNUSED)
1997 {
1998   struct pr_handle *info = (struct pr_handle *) p;
1999   const char *name;
2000   char idbuf[20];
2001
2002   if (tag != NULL)
2003     name = tag;
2004   else
2005     {
2006       name = idbuf;
2007       sprintf (idbuf, "%%anon%u", id);
2008     }
2009
2010   if (! push_type (info, name))
2011     return FALSE;
2012
2013   info->stack->flavor = structp ? "struct" : "union";
2014
2015   fprintf (info->f, "%s\t%s\t0;\"\tkind:%c\n", name, info->filename,
2016            info->stack->flavor[0]);
2017
2018   info->stack->visibility = DEBUG_VISIBILITY_PUBLIC;
2019
2020   return indent_type (info);
2021 }
2022
2023 /* Output the visibility of a field in a struct.  */
2024
2025 static bfd_boolean
2026 tg_fix_visibility (struct pr_handle *info, enum debug_visibility visibility)
2027 {
2028   assert (info->stack != NULL);
2029
2030   if (info->stack->visibility == visibility)
2031     return TRUE;
2032
2033   assert (info->stack->visibility != DEBUG_VISIBILITY_IGNORE);
2034
2035   info->stack->visibility = visibility;
2036
2037   return TRUE;
2038 }
2039
2040 /* Add a field to a struct type.  */
2041
2042 static bfd_boolean
2043 tg_struct_field (void *p, const char *name, bfd_vma bitpos ATTRIBUTE_UNUSED,
2044                  bfd_vma bitsize ATTRIBUTE_UNUSED,
2045                  enum debug_visibility visibility)
2046 {
2047   struct pr_handle *info = (struct pr_handle *) p;
2048   char *t;
2049
2050   t = pop_type (info);
2051   if (t == NULL)
2052     return FALSE;
2053
2054   if (! tg_fix_visibility (info, visibility))
2055     return FALSE;
2056
2057   /* It happens, a bug? */
2058   if (! name[0])
2059     return TRUE;
2060
2061   fprintf (info->f, "%s\t%s\t0;\"\tkind:m\ttype:%s\t%s:%s\taccess:%s\n",
2062            name, info->filename, t, info->stack->flavor, info->stack->type,
2063            visibility_name (visibility));
2064
2065   return TRUE;
2066 }
2067
2068 /* Finish a struct type.  */
2069
2070 static bfd_boolean
2071 tg_end_struct_type (void *p ATTRIBUTE_UNUSED)
2072 {
2073   struct pr_handle *info = (struct pr_handle *) p;
2074   assert (info->stack != NULL);
2075
2076   return TRUE;
2077 }
2078
2079 /* Start a class type.  */
2080
2081 static bfd_boolean
2082 tg_start_class_type (void *p, const char *tag, unsigned int id,
2083                      bfd_boolean structp, unsigned int size,
2084                      bfd_boolean vptr, bfd_boolean ownvptr)
2085 {
2086   struct pr_handle *info = (struct pr_handle *) p;
2087   char *tv = NULL;
2088   const char *name;
2089
2090   info->indent += 2;
2091
2092   if (vptr && ! ownvptr)
2093     {
2094       tv = pop_type (info);
2095       if (tv == NULL)
2096         return FALSE;
2097     }
2098
2099   if (tag != NULL)
2100     name = tag;
2101   else
2102     {
2103       char idbuf[20];
2104
2105       sprintf (idbuf, "%%anon%u", id);
2106       name = idbuf;
2107     }
2108
2109   if (! push_type (info, name))
2110     return FALSE;
2111
2112   info->stack->flavor = structp ? "class" : "union class";
2113   info->stack->parents = NULL;
2114   info->stack->num_parents = 0;
2115
2116   if (size != 0 || vptr || ownvptr || tag != NULL)
2117     {
2118       if (vptr)
2119         {
2120           if (! append_type (info, " vtable "))
2121             return FALSE;
2122           if (ownvptr)
2123             {
2124               if (! append_type (info, "self "))
2125                 return FALSE;
2126             }
2127           else
2128             {
2129               if (! append_type (info, tv)
2130                   || ! append_type (info, " "))
2131                 return FALSE;
2132             }
2133         }
2134     }
2135
2136   info->stack->visibility = DEBUG_VISIBILITY_PRIVATE;
2137
2138   return TRUE;
2139 }
2140
2141 /* Add a static member to a class.  */
2142
2143 static bfd_boolean
2144 tg_class_static_member (void *p, const char *name,
2145                         const char *physname ATTRIBUTE_UNUSED,
2146                         enum debug_visibility visibility)
2147 {
2148   struct pr_handle *info = (struct pr_handle *) p;
2149   char *t;
2150   int len_var, len_class;
2151   char *full_name;
2152
2153   len_var = strlen (name);
2154   len_class = strlen (info->stack->next->type);
2155   full_name = xmalloc (len_var + len_class + 3);
2156   if (! full_name)
2157     return FALSE;
2158   sprintf (full_name, "%s::%s", info->stack->next->type, name);
2159
2160   if (! substitute_type (info, full_name))
2161     return FALSE;
2162
2163   if (! prepend_type (info, "static "))
2164     return FALSE;
2165
2166   t = pop_type (info);
2167   if (t == NULL)
2168     return FALSE;
2169
2170   if (! tg_fix_visibility (info, visibility))
2171     return FALSE;
2172
2173   fprintf (info->f, "%s\t%s\t0;\"\tkind:x\ttype:%s\tclass:%s\taccess:%s\n",
2174            name, info->filename, t, info->stack->type,
2175            visibility_name (visibility));
2176   free (t);
2177   free (full_name);
2178
2179   return TRUE;
2180 }
2181
2182 /* Add a base class to a class.  */
2183
2184 static bfd_boolean
2185 tg_class_baseclass (void *p, bfd_vma bitpos ATTRIBUTE_UNUSED,
2186                     bfd_boolean virtual, enum debug_visibility visibility)
2187 {
2188   struct pr_handle *info = (struct pr_handle *) p;
2189   char *t;
2190   const char *prefix;
2191
2192   assert (info->stack != NULL && info->stack->next != NULL);
2193
2194   t = pop_type (info);
2195   if (t == NULL)
2196     return FALSE;
2197
2198   if (CONST_STRNEQ (t, "class "))
2199     t += sizeof "class " - 1;
2200
2201   /* Push it back on to take advantage of the prepend_type and
2202      append_type routines.  */
2203   if (! push_type (info, t))
2204     return FALSE;
2205
2206   if (virtual)
2207     {
2208       if (! prepend_type (info, "virtual "))
2209         return FALSE;
2210     }
2211
2212   switch (visibility)
2213     {
2214     case DEBUG_VISIBILITY_PUBLIC:
2215       prefix = "public ";
2216       break;
2217     case DEBUG_VISIBILITY_PROTECTED:
2218       prefix = "protected ";
2219       break;
2220     case DEBUG_VISIBILITY_PRIVATE:
2221       prefix = "private ";
2222       break;
2223     default:
2224       prefix = "/* unknown visibility */ ";
2225       break;
2226     }
2227
2228   if (! prepend_type (info, prefix))
2229     return FALSE;
2230
2231   t = pop_type (info);
2232   if (t == NULL)
2233     return FALSE;
2234
2235   if (info->stack->num_parents && ! append_parent (info, ", "))
2236     return FALSE;
2237
2238   if (! append_parent (info, t))
2239     return FALSE;
2240   info->stack->num_parents++;
2241
2242   free (t);
2243
2244   return TRUE;
2245 }
2246
2247 /* Add a variant to a method.  */
2248
2249 static bfd_boolean
2250 tg_class_method_variant (void *p, const char *physname ATTRIBUTE_UNUSED,
2251                          enum debug_visibility visibility,
2252                          bfd_boolean constp, bfd_boolean volatilep,
2253                          bfd_vma voffset ATTRIBUTE_UNUSED,
2254                          bfd_boolean context)
2255 {
2256   struct pr_handle *info = (struct pr_handle *) p;
2257   char *method_type;
2258   char *context_type;
2259   char *method_name;
2260
2261   assert (info->stack != NULL);
2262   assert (info->stack->next != NULL);
2263
2264   /* Put the const and volatile qualifiers on the type.  */
2265   if (volatilep)
2266     {
2267       if (! append_type (info, " volatile"))
2268         return FALSE;
2269     }
2270   if (constp)
2271     {
2272       if (! append_type (info, " const"))
2273         return FALSE;
2274     }
2275
2276   method_name = strdup (context ? info->stack->next->next->method
2277                         : info->stack->next->method);
2278
2279   /* Stick the name of the method into its type.  */
2280   if (! substitute_type (info, method_name))
2281     return FALSE;
2282
2283   /* Get the type.  */
2284   method_type = pop_type (info);
2285   if (method_type == NULL)
2286     return FALSE;
2287
2288   /* Pull off the context type if there is one.  */
2289   if (! context)
2290     context_type = NULL;
2291   else
2292     {
2293       context_type = pop_type (info);
2294       if (context_type == NULL)
2295         return FALSE;
2296     }
2297
2298   /* Now the top of the stack is the class.  */
2299   if (! tg_fix_visibility (info, visibility))
2300     return FALSE;
2301
2302   fprintf (info->f, "%s\t%s\t0;\"\tkind:p\ttype:%s\tclass:%s\n",
2303            method_name, info->filename, method_type, info->stack->type);
2304   free (method_type);
2305   free (method_name);
2306   free (context_type);
2307
2308   return TRUE;
2309 }
2310
2311 /* Add a static variant to a method.  */
2312
2313 static bfd_boolean
2314 tg_class_static_method_variant (void *p,
2315                                 const char *physname ATTRIBUTE_UNUSED,
2316                                 enum debug_visibility visibility,
2317                                 bfd_boolean constp, bfd_boolean volatilep)
2318 {
2319   struct pr_handle *info = (struct pr_handle *) p;
2320   char *method_type;
2321   char *method_name;
2322
2323   assert (info->stack != NULL);
2324   assert (info->stack->next != NULL);
2325   assert (info->stack->next->method != NULL);
2326
2327   /* Put the const and volatile qualifiers on the type.  */
2328   if (volatilep)
2329     {
2330       if (! append_type (info, " volatile"))
2331         return FALSE;
2332     }
2333   if (constp)
2334     {
2335       if (! append_type (info, " const"))
2336         return FALSE;
2337     }
2338
2339   /* Mark it as static.  */
2340   if (! prepend_type (info, "static "))
2341     return FALSE;
2342
2343   method_name = strdup (info->stack->next->method);
2344   /* Stick the name of the method into its type.  */
2345   if (! substitute_type (info, info->stack->next->method))
2346     return FALSE;
2347
2348   /* Get the type.  */
2349   method_type = pop_type (info);
2350   if (method_type == NULL)
2351     return FALSE;
2352
2353   /* Now the top of the stack is the class.  */
2354   if (! tg_fix_visibility (info, visibility))
2355     return FALSE;
2356
2357   fprintf (info->f, "%s\t%s\t0;\"\tkind:p\ttype:%s\tclass:%s\taccess:%s\n",
2358            method_name, info->filename, method_type, info->stack->type,
2359            visibility_name (visibility));
2360   free (method_type);
2361   free (method_name);
2362
2363   return TRUE;
2364 }
2365
2366 /* Finish up a class.  */
2367
2368 static bfd_boolean
2369 tg_end_class_type (void *p)
2370 {
2371   struct pr_handle *info = (struct pr_handle *) p;
2372
2373   fprintf (info->f, "%s\t%s\t0;\"\tkind:c\ttype:%s", info->stack->type,
2374            info->filename, info->stack->flavor);
2375   if (info->stack->num_parents)
2376     {
2377       fprintf  (info->f, "\tinherits:%s", info->stack->parents);
2378       free (info->stack->parents);
2379     }
2380   fputc ('\n', info->f);
2381
2382   return tg_end_struct_type (p);
2383 }
2384
2385 /* Push a type on the stack using a tag name.  */
2386
2387 static bfd_boolean
2388 tg_tag_type (void *p, const char *name, unsigned int id,
2389              enum debug_type_kind kind)
2390 {
2391   struct pr_handle *info = (struct pr_handle *) p;
2392   const char *t, *tag;
2393   char idbuf[20];
2394
2395   switch (kind)
2396     {
2397     case DEBUG_KIND_STRUCT:
2398       t = "struct ";
2399       break;
2400     case DEBUG_KIND_UNION:
2401       t = "union ";
2402       break;
2403     case DEBUG_KIND_ENUM:
2404       t = "enum ";
2405       break;
2406     case DEBUG_KIND_CLASS:
2407       t = "class ";
2408       break;
2409     case DEBUG_KIND_UNION_CLASS:
2410       t = "union class ";
2411       break;
2412     default:
2413       abort ();
2414       return FALSE;
2415     }
2416
2417   if (! push_type (info, t))
2418     return FALSE;
2419   if (name != NULL)
2420     tag = name;
2421   else
2422     {
2423       sprintf (idbuf, "%%anon%u", id);
2424       tag = idbuf;
2425     }
2426
2427   if (! append_type (info, tag))
2428     return FALSE;
2429
2430   return TRUE;
2431 }
2432
2433 /* Output a typedef.  */
2434
2435 static bfd_boolean
2436 tg_typdef (void *p, const char *name)
2437 {
2438   struct pr_handle *info = (struct pr_handle *) p;
2439   char *s;
2440
2441   s = pop_type (info);
2442   if (s == NULL)
2443     return FALSE;
2444
2445   fprintf (info->f, "%s\t%s\t0;\"\tkind:t\ttype:%s\n", name,
2446            info->filename, s);
2447
2448   free (s);
2449
2450   return TRUE;
2451 }
2452
2453 /* Output a tag.  The tag should already be in the string on the
2454    stack, so all we have to do here is print it out.  */
2455
2456 static bfd_boolean
2457 tg_tag (void *p ATTRIBUTE_UNUSED, const char *name ATTRIBUTE_UNUSED)
2458 {
2459   struct pr_handle *info = (struct pr_handle *) p;
2460   char *t;
2461
2462   t = pop_type (info);
2463   if (t == NULL)
2464     return FALSE;
2465   free (t);
2466
2467   return TRUE;
2468 }
2469
2470 /* Output an integer constant.  */
2471
2472 static bfd_boolean
2473 tg_int_constant (void *p, const char *name, bfd_vma val)
2474 {
2475   struct pr_handle *info = (struct pr_handle *) p;
2476   char ab[20];
2477
2478   indent (info);
2479   print_vma (val, ab, FALSE, FALSE);
2480   fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:const int\tvalue:%s\n",
2481            name, info->filename, ab);
2482   return TRUE;
2483 }
2484
2485 /* Output a floating point constant.  */
2486
2487 static bfd_boolean
2488 tg_float_constant (void *p, const char *name, double val)
2489 {
2490   struct pr_handle *info = (struct pr_handle *) p;
2491
2492   indent (info);
2493   fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:const double\tvalue:%g\n",
2494            name, info->filename, val);
2495   return TRUE;
2496 }
2497
2498 /* Output a typed constant.  */
2499
2500 static bfd_boolean
2501 tg_typed_constant (void *p, const char *name, bfd_vma val)
2502 {
2503   struct pr_handle *info = (struct pr_handle *) p;
2504   char *t;
2505   char ab[20];
2506
2507   t = pop_type (info);
2508   if (t == NULL)
2509     return FALSE;
2510
2511   indent (info);
2512   print_vma (val, ab, FALSE, FALSE);
2513   fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:const %s\tvalue:%s\n",
2514            name, info->filename, t, ab);
2515
2516   free (t);
2517
2518   return TRUE;
2519 }
2520
2521 /* Output a variable.  */
2522
2523 static bfd_boolean
2524 tg_variable (void *p, const char *name, enum debug_var_kind kind,
2525              bfd_vma val ATTRIBUTE_UNUSED)
2526 {
2527   struct pr_handle *info = (struct pr_handle *) p;
2528   char *t, *dname, *from_class;
2529
2530   t = pop_type (info);
2531   if (t == NULL)
2532     return FALSE;
2533
2534   dname = NULL;
2535   if (info->demangler)
2536     dname = info->demangler (info->abfd, name, DMGL_ANSI | DMGL_PARAMS);
2537
2538   from_class = NULL;
2539   if (dname != NULL)
2540     {
2541       char *sep;
2542       sep = strstr (dname, "::");
2543       if (sep)
2544         {
2545           *sep = 0;
2546           name = sep + 2;
2547           from_class = dname;
2548         }
2549       else
2550         /* Obscure types as vts and type_info nodes.  */
2551         name = dname;
2552     }
2553
2554   fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:%s", name, info->filename, t);
2555
2556   switch (kind)
2557     {
2558     case DEBUG_STATIC:
2559     case DEBUG_LOCAL_STATIC:
2560       fprintf (info->f, "\tfile:");
2561       break;
2562     case DEBUG_REGISTER:
2563       fprintf (info->f, "\tregister:");
2564       break;
2565     default:
2566       break;
2567     }
2568
2569   if (from_class)
2570     fprintf (info->f, "\tclass:%s", from_class);
2571
2572   if (dname)
2573     free (dname);
2574
2575   fprintf (info->f, "\n");
2576
2577   free (t);
2578
2579   return TRUE;
2580 }
2581
2582 /* Start outputting a function.  */
2583
2584 static bfd_boolean
2585 tg_start_function (void *p, const char *name, bfd_boolean global)
2586 {
2587   struct pr_handle *info = (struct pr_handle *) p;
2588   char *dname;
2589
2590   if (! global)
2591     info->stack->flavor = "static";
2592   else
2593     info->stack->flavor = NULL;
2594
2595   dname = NULL;
2596   if (info->demangler)
2597     dname = info->demangler (info->abfd, name, DMGL_ANSI | DMGL_PARAMS);
2598
2599   if (! substitute_type (info, dname ? dname : name))
2600     return FALSE;
2601
2602   info->stack->method = NULL;
2603   if (dname != NULL)
2604     {
2605       char *sep;
2606       sep = strstr (dname, "::");
2607       if (sep)
2608         {
2609           info->stack->method = dname;
2610           *sep = 0;
2611           name = sep + 2;
2612         }
2613       else
2614         {
2615           info->stack->method = "";
2616           name = dname;
2617         }
2618       sep = strchr (name, '(');
2619       if (sep)
2620         *sep = 0;
2621       /* Obscure functions as type_info function.  */
2622     }
2623
2624   info->stack->parents = strdup (name);
2625
2626   if (! info->stack->method && ! append_type (info, "("))
2627     return FALSE;
2628
2629   info->parameter = 1;
2630
2631   return TRUE;
2632 }
2633
2634 /* Output a function parameter.  */
2635
2636 static bfd_boolean
2637 tg_function_parameter (void *p, const char *name, enum debug_parm_kind kind,
2638                        bfd_vma val ATTRIBUTE_UNUSED)
2639 {
2640   struct pr_handle *info = (struct pr_handle *) p;
2641   char *t;
2642
2643   if (kind == DEBUG_PARM_REFERENCE
2644       || kind == DEBUG_PARM_REF_REG)
2645     {
2646       if (! pr_reference_type (p))
2647         return FALSE;
2648     }
2649
2650   if (! substitute_type (info, name))
2651     return FALSE;
2652
2653   t = pop_type (info);
2654   if (t == NULL)
2655     return FALSE;
2656
2657   if (! info->stack->method)
2658     {
2659       if (info->parameter != 1 && ! append_type (info, ", "))
2660         return FALSE;
2661
2662       if (kind == DEBUG_PARM_REG || kind == DEBUG_PARM_REF_REG)
2663         if (! append_type (info, "register "))
2664           return FALSE;
2665
2666       if (! append_type (info, t))
2667         return FALSE;
2668     }
2669
2670   free (t);
2671
2672   ++info->parameter;
2673
2674   return TRUE;
2675 }
2676
2677 /* Start writing out a block.  */
2678
2679 static bfd_boolean
2680 tg_start_block (void *p, bfd_vma addr)
2681 {
2682   struct pr_handle *info = (struct pr_handle *) p;
2683   char ab[20], kind, *partof;
2684   char *t;
2685   bfd_boolean local;
2686
2687   if (info->parameter > 0)
2688     {
2689       info->parameter = 0;
2690
2691       /* Delayed name.  */
2692       fprintf (info->f, "%s\t%s\t", info->stack->parents, info->filename);
2693       free (info->stack->parents);
2694
2695       print_vma (addr, ab, TRUE, TRUE);
2696       translate_addresses (info->abfd, ab, info->f, info->syms);
2697       local = info->stack->flavor != NULL;
2698       if (info->stack->method && *info->stack->method)
2699         {
2700           kind = 'm';
2701           partof = (char *) info->stack->method;
2702         }
2703       else
2704         {
2705           kind = 'f';
2706           partof = NULL;
2707           if (! info->stack->method && ! append_type (info, ")"))
2708             return FALSE;
2709         }
2710       t = pop_type (info);
2711       if (t == NULL)
2712         return FALSE;
2713       fprintf (info->f, ";\"\tkind:%c\ttype:%s", kind, t);
2714       if (local)
2715         fputs ("\tfile:", info->f);
2716       if (partof)
2717         {
2718           fprintf (info->f, "\tclass:%s", partof);
2719           free (partof);
2720         }
2721       fputc ('\n', info->f);
2722     }
2723
2724   return TRUE;
2725 }
2726
2727 /* Write out line number information.  */
2728
2729 static bfd_boolean
2730 tg_lineno (void *p ATTRIBUTE_UNUSED, const char *filename ATTRIBUTE_UNUSED,
2731            unsigned long lineno ATTRIBUTE_UNUSED,
2732            bfd_vma addr ATTRIBUTE_UNUSED)
2733 {
2734   return TRUE;
2735 }
2736
2737 /* Finish writing out a block.  */
2738
2739 static bfd_boolean
2740 tg_end_block (void *p ATTRIBUTE_UNUSED, bfd_vma addr ATTRIBUTE_UNUSED)
2741 {
2742   return TRUE;
2743 }
2744
2745 /* Convert the visibility value into a human readable name.  */
2746
2747 static const char *
2748 visibility_name (enum debug_visibility visibility)
2749 {
2750   const char *s;
2751
2752   switch (visibility)
2753     {
2754     case DEBUG_VISIBILITY_PUBLIC:
2755       s = "public";
2756       break;
2757     case DEBUG_VISIBILITY_PRIVATE:
2758       s = "private";
2759       break;
2760     case DEBUG_VISIBILITY_PROTECTED:
2761       s = "protected";
2762       break;
2763     case DEBUG_VISIBILITY_IGNORE:
2764       s = "/* ignore */";
2765       break;
2766     default:
2767       abort ();
2768       return FALSE;
2769     }
2770   return s;
2771 }