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