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