Home | History | Annotate | Download | only in m_demangle
      1 /* Demangler for g++ V3 ABI.
      2    Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
      3    Free Software Foundation, Inc.
      4    Written by Ian Lance Taylor <ian (at) wasabisystems.com>.
      5 
      6    This file is part of the libiberty library, which is part of GCC.
      7 
      8    This file 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 2 of the License, or
     11    (at your option) any later version.
     12 
     13    In addition to the permissions in the GNU General Public License, the
     14    Free Software Foundation gives you unlimited permission to link the
     15    compiled version of this file into combinations with other programs,
     16    and to distribute those combinations without any restriction coming
     17    from the use of this file.  (The General Public License restrictions
     18    do apply in other respects; for example, they cover modification of
     19    the file, and distribution when not linked into a combined
     20    executable.)
     21 
     22    This program is distributed in the hope that it will be useful,
     23    but WITHOUT ANY WARRANTY; without even the implied warranty of
     24    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     25    GNU General Public License for more details.
     26 
     27    You should have received a copy of the GNU General Public License
     28    along with this program; if not, write to the Free Software
     29    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
     30 */
     31 
     32 /* This code implements a demangler for the g++ V3 ABI.  The ABI is
     33    described on this web page:
     34        http://www.codesourcery.com/cxx-abi/abi.html#mangling
     35 
     36    This code was written while looking at the demangler written by
     37    Alex Samuel <samuel (at) codesourcery.com>.
     38 
     39    This code first pulls the mangled name apart into a list of
     40    components, and then walks the list generating the demangled
     41    name.
     42 
     43    This file will normally define the following functions, q.v.:
     44       char *cplus_demangle_v3(const char *mangled, int options)
     45       char *java_demangle_v3(const char *mangled)
     46       int cplus_demangle_v3_callback(const char *mangled, int options,
     47                                      demangle_callbackref callback)
     48       int java_demangle_v3_callback(const char *mangled,
     49                                     demangle_callbackref callback)
     50       enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
     51       enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
     52 
     53    Also, the interface to the component list is public, and defined in
     54    demangle.h.  The interface consists of these types, which are
     55    defined in demangle.h:
     56       enum demangle_component_type
     57       struct demangle_component
     58       demangle_callbackref
     59    and these functions defined in this file:
     60       cplus_demangle_fill_name
     61       cplus_demangle_fill_extended_operator
     62       cplus_demangle_fill_ctor
     63       cplus_demangle_fill_dtor
     64       cplus_demangle_print
     65       cplus_demangle_print_callback
     66    and other functions defined in the file cp-demint.c.
     67 
     68    This file also defines some other functions and variables which are
     69    only to be used by the file cp-demint.c.
     70 
     71    Preprocessor macros you can define while compiling this file:
     72 
     73    IN_LIBGCC2
     74       If defined, this file defines the following functions, q.v.:
     75          char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
     76                                int *status)
     77          int __gcclibcxx_demangle_callback (const char *,
     78                                             void (*)
     79                                               (const char *, size_t, void *),
     80                                             void *)
     81       instead of cplus_demangle_v3[_callback]() and
     82       java_demangle_v3[_callback]().
     83 
     84    IN_GLIBCPP_V3
     85       If defined, this file defines only __cxa_demangle() and
     86       __gcclibcxx_demangle_callback(), and no other publically visible
     87       functions or variables.
     88 
     89    STANDALONE_DEMANGLER
     90       If defined, this file defines a main() function which demangles
     91       any arguments, or, if none, demangles stdin.
     92 
     93    CP_DEMANGLE_DEBUG
     94       If defined, turns on debugging mode, which prints information on
     95       stdout about the mangled string.  This is not generally useful.
     96 */
     97 
     98 #if 0 /* in valgrind */
     99 #if defined (_AIX) && !defined (__GNUC__)
    100  #pragma alloca
    101 #endif
    102 #endif /* ! in valgrind */
    103 
    104 #if 0 /* in valgrind */
    105 #ifdef HAVE_CONFIG_H
    106 #include "config.h"
    107 #endif
    108 #endif /* ! in valgrind */
    109 
    110 #if 0 /* in valgrind */
    111 #include <stdio.h>
    112 #endif /* ! in valgrind */
    113 
    114 #if 0 /* in valgrind */
    115 #ifdef HAVE_STDLIB_H
    116 #include <stdlib.h>
    117 #endif
    118 #ifdef HAVE_STRING_H
    119 #include <string.h>
    120 #endif
    121 #endif /* ! in valgrind */
    122 
    123 #if 0 /* in valgrind */
    124 #ifdef HAVE_ALLOCA_H
    125 # include <alloca.h>
    126 #else
    127 # ifndef alloca
    128 #  ifdef __GNUC__
    129 #   define alloca __builtin_alloca
    130 #  else
    131 extern char *alloca ();
    132 #  endif /* __GNUC__ */
    133 # endif /* alloca */
    134 #endif /* HAVE_ALLOCA_H */
    135 #endif /* ! in valgrind */
    136 
    137 #if 0 /* in valgrind */
    138 #include "ansidecl.h"
    139 #include "libiberty.h"
    140 #endif /* ! in valgrind */
    141 
    142 #include "vg_libciface.h"
    143 
    144 #include "demangle.h"
    145 #include "cp-demangle.h"
    146 
    147 /* If IN_GLIBCPP_V3 is defined, some functions are made static.  We
    148    also rename them via #define to avoid compiler errors when the
    149    static definition conflicts with the extern declaration in a header
    150    file.  */
    151 #ifdef IN_GLIBCPP_V3
    152 
    153 #define CP_STATIC_IF_GLIBCPP_V3 static
    154 
    155 #define cplus_demangle_fill_name d_fill_name
    156 static int d_fill_name (struct demangle_component *, const char *, int);
    157 
    158 #define cplus_demangle_fill_extended_operator d_fill_extended_operator
    159 static int
    160 d_fill_extended_operator (struct demangle_component *, int,
    161                           struct demangle_component *);
    162 
    163 #define cplus_demangle_fill_ctor d_fill_ctor
    164 static int
    165 d_fill_ctor (struct demangle_component *, enum gnu_v3_ctor_kinds,
    166              struct demangle_component *);
    167 
    168 #define cplus_demangle_fill_dtor d_fill_dtor
    169 static int
    170 d_fill_dtor (struct demangle_component *, enum gnu_v3_dtor_kinds,
    171              struct demangle_component *);
    172 
    173 #define cplus_demangle_mangled_name d_mangled_name
    174 static struct demangle_component *d_mangled_name (struct d_info *, int);
    175 
    176 #define cplus_demangle_type d_type
    177 static struct demangle_component *d_type (struct d_info *);
    178 
    179 #define cplus_demangle_print d_print
    180 static char *d_print (int, const struct demangle_component *, int, size_t *);
    181 
    182 #define cplus_demangle_print_callback d_print_callback
    183 static int d_print_callback (int, const struct demangle_component *,
    184                              demangle_callbackref, void *);
    185 
    186 #define cplus_demangle_init_info d_init_info
    187 static void d_init_info (const char *, int, size_t, struct d_info *);
    188 
    189 #else /* ! defined(IN_GLIBCPP_V3) */
    190 #define CP_STATIC_IF_GLIBCPP_V3
    191 #endif /* ! defined(IN_GLIBCPP_V3) */
    192 
    193 /* See if the compiler supports dynamic arrays.  */
    194 
    195 #ifdef __GNUC__
    196 #define CP_DYNAMIC_ARRAYS
    197 #else
    198 #ifdef __STDC__
    199 #ifdef __STDC_VERSION__
    200 #if __STDC_VERSION__ >= 199901L
    201 #define CP_DYNAMIC_ARRAYS
    202 #endif /* __STDC__VERSION >= 199901L */
    203 #endif /* defined (__STDC_VERSION__) */
    204 #endif /* defined (__STDC__) */
    205 #endif /* ! defined (__GNUC__) */
    206 
    207 /* We avoid pulling in the ctype tables, to prevent pulling in
    208    additional unresolved symbols when this code is used in a library.
    209    FIXME: Is this really a valid reason?  This comes from the original
    210    V3 demangler code.
    211 
    212    As of this writing this file has the following undefined references
    213    when compiled with -DIN_GLIBCPP_V3: realloc, free, memcpy, strcpy,
    214    strcat, strlen.  */
    215 
    216 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
    217 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
    218 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
    219 
    220 /* The prefix prepended by GCC to an identifier represnting the
    221    anonymous namespace.  */
    222 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
    223 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
    224   (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
    225 
    226 /* Information we keep for the standard substitutions.  */
    227 
    228 struct d_standard_sub_info
    229 {
    230   /* The code for this substitution.  */
    231   char code;
    232   /* The simple string it expands to.  */
    233   const char *simple_expansion;
    234   /* The length of the simple expansion.  */
    235   int simple_len;
    236   /* The results of a full, verbose, expansion.  This is used when
    237      qualifying a constructor/destructor, or when in verbose mode.  */
    238   const char *full_expansion;
    239   /* The length of the full expansion.  */
    240   int full_len;
    241   /* What to set the last_name field of d_info to; NULL if we should
    242      not set it.  This is only relevant when qualifying a
    243      constructor/destructor.  */
    244   const char *set_last_name;
    245   /* The length of set_last_name.  */
    246   int set_last_name_len;
    247 };
    248 
    249 /* Accessors for subtrees of struct demangle_component.  */
    250 
    251 #define d_left(dc) ((dc)->u.s_binary.left)
    252 #define d_right(dc) ((dc)->u.s_binary.right)
    253 
    254 /* A list of templates.  This is used while printing.  */
    255 
    256 struct d_print_template
    257 {
    258   /* Next template on the list.  */
    259   struct d_print_template *next;
    260   /* This template.  */
    261   const struct demangle_component *template_decl;
    262 };
    263 
    264 /* A list of type modifiers.  This is used while printing.  */
    265 
    266 struct d_print_mod
    267 {
    268   /* Next modifier on the list.  These are in the reverse of the order
    269      in which they appeared in the mangled string.  */
    270   struct d_print_mod *next;
    271   /* The modifier.  */
    272   const struct demangle_component *mod;
    273   /* Whether this modifier was printed.  */
    274   int printed;
    275   /* The list of templates which applies to this modifier.  */
    276   struct d_print_template *templates;
    277 };
    278 
    279 /* We use these structures to hold information during printing.  */
    280 
    281 struct d_growable_string
    282 {
    283   /* Buffer holding the result.  */
    284   char *buf;
    285   /* Current length of data in buffer.  */
    286   size_t len;
    287   /* Allocated size of buffer.  */
    288   size_t alc;
    289   /* Set to 1 if we had a memory allocation failure.  */
    290   int allocation_failure;
    291 };
    292 
    293 enum { D_PRINT_BUFFER_LENGTH = 256 };
    294 struct d_print_info
    295 {
    296   /* The options passed to the demangler.  */
    297   int options;
    298   /* Fixed-length allocated buffer for demangled data, flushed to the
    299      callback with a NUL termination once full.  */
    300   char buf[D_PRINT_BUFFER_LENGTH];
    301   /* Current length of data in buffer.  */
    302   size_t len;
    303   /* The last character printed, saved individually so that it survives
    304      any buffer flush.  */
    305   char last_char;
    306   /* Callback function to handle demangled buffer flush.  */
    307   demangle_callbackref callback;
    308   /* Opaque callback argument.  */
    309   void *opaque;
    310   /* The current list of templates, if any.  */
    311   struct d_print_template *templates;
    312   /* The current list of modifiers (e.g., pointer, reference, etc.),
    313      if any.  */
    314   struct d_print_mod *modifiers;
    315   /* Set to 1 if we saw a demangling error.  */
    316   int demangle_failure;
    317   /* The current index into any template argument packs we are using
    318      for printing.  */
    319   int pack_index;
    320 };
    321 
    322 #ifdef CP_DEMANGLE_DEBUG
    323 static void d_dump (struct demangle_component *, int);
    324 #endif
    325 
    326 static struct demangle_component *
    327 d_make_empty (struct d_info *);
    328 
    329 static struct demangle_component *
    330 d_make_comp (struct d_info *, enum demangle_component_type,
    331              struct demangle_component *,
    332              struct demangle_component *);
    333 
    334 static struct demangle_component *
    335 d_make_name (struct d_info *, const char *, int);
    336 
    337 static struct demangle_component *
    338 d_make_builtin_type (struct d_info *,
    339                      const struct demangle_builtin_type_info *);
    340 
    341 static struct demangle_component *
    342 d_make_operator (struct d_info *,
    343                  const struct demangle_operator_info *);
    344 
    345 static struct demangle_component *
    346 d_make_extended_operator (struct d_info *, int,
    347                           struct demangle_component *);
    348 
    349 static struct demangle_component *
    350 d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
    351              struct demangle_component *);
    352 
    353 static struct demangle_component *
    354 d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
    355              struct demangle_component *);
    356 
    357 static struct demangle_component *
    358 d_make_template_param (struct d_info *, long);
    359 
    360 static struct demangle_component *
    361 d_make_sub (struct d_info *, const char *, int);
    362 
    363 static int
    364 has_return_type (struct demangle_component *);
    365 
    366 static int
    367 is_ctor_dtor_or_conversion (struct demangle_component *);
    368 
    369 static struct demangle_component *d_encoding (struct d_info *, int);
    370 
    371 static struct demangle_component *d_name (struct d_info *);
    372 
    373 static struct demangle_component *d_nested_name (struct d_info *);
    374 
    375 static struct demangle_component *d_prefix (struct d_info *);
    376 
    377 static struct demangle_component *d_unqualified_name (struct d_info *);
    378 
    379 static struct demangle_component *d_source_name (struct d_info *);
    380 
    381 static long d_number (struct d_info *);
    382 
    383 static struct demangle_component *d_identifier (struct d_info *, int);
    384 
    385 static struct demangle_component *d_operator_name (struct d_info *);
    386 
    387 static struct demangle_component *d_special_name (struct d_info *);
    388 
    389 static int d_call_offset (struct d_info *, int);
    390 
    391 static struct demangle_component *d_ctor_dtor_name (struct d_info *);
    392 
    393 static struct demangle_component **
    394 d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
    395 
    396 static struct demangle_component *
    397 d_function_type (struct d_info *);
    398 
    399 static struct demangle_component *
    400 d_bare_function_type (struct d_info *, int);
    401 
    402 static struct demangle_component *
    403 d_class_enum_type (struct d_info *);
    404 
    405 static struct demangle_component *d_array_type (struct d_info *);
    406 
    407 static struct demangle_component *
    408 d_pointer_to_member_type (struct d_info *);
    409 
    410 static struct demangle_component *
    411 d_template_param (struct d_info *);
    412 
    413 static struct demangle_component *d_template_args (struct d_info *);
    414 
    415 static struct demangle_component *
    416 d_template_arg (struct d_info *);
    417 
    418 static struct demangle_component *d_expression (struct d_info *);
    419 
    420 static struct demangle_component *d_expr_primary (struct d_info *);
    421 
    422 static struct demangle_component *d_local_name (struct d_info *);
    423 
    424 static int d_discriminator (struct d_info *);
    425 
    426 static int
    427 d_add_substitution (struct d_info *, struct demangle_component *);
    428 
    429 static struct demangle_component *d_substitution (struct d_info *, int);
    430 
    431 static void d_growable_string_init (struct d_growable_string *, size_t);
    432 
    433 static inline void
    434 d_growable_string_resize (struct d_growable_string *, size_t);
    435 
    436 static inline void
    437 d_growable_string_append_buffer (struct d_growable_string *,
    438                                  const char *, size_t);
    439 static void
    440 d_growable_string_callback_adapter (const char *, size_t, void *);
    441 
    442 static void
    443 d_print_init (struct d_print_info *, int, demangle_callbackref, void *);
    444 
    445 static inline void d_print_error (struct d_print_info *);
    446 
    447 static inline int d_print_saw_error (struct d_print_info *);
    448 
    449 static inline void d_print_flush (struct d_print_info *);
    450 
    451 static inline void d_append_char (struct d_print_info *, char);
    452 
    453 static inline void d_append_buffer (struct d_print_info *,
    454                                     const char *, size_t);
    455 
    456 static inline void d_append_string (struct d_print_info *, const char *);
    457 
    458 static inline char d_last_char (struct d_print_info *);
    459 
    460 static void
    461 d_print_comp (struct d_print_info *, const struct demangle_component *);
    462 
    463 static void
    464 d_print_java_identifier (struct d_print_info *, const char *, int);
    465 
    466 static void
    467 d_print_mod_list (struct d_print_info *, struct d_print_mod *, int);
    468 
    469 static void
    470 d_print_mod (struct d_print_info *, const struct demangle_component *);
    471 
    472 static void
    473 d_print_function_type (struct d_print_info *,
    474                        const struct demangle_component *,
    475                        struct d_print_mod *);
    476 
    477 static void
    478 d_print_array_type (struct d_print_info *,
    479                     const struct demangle_component *,
    480                     struct d_print_mod *);
    481 
    482 static void
    483 d_print_expr_op (struct d_print_info *, const struct demangle_component *);
    484 
    485 static void
    486 d_print_cast (struct d_print_info *, const struct demangle_component *);
    487 
    488 static int d_demangle_callback (const char *, int,
    489                                 demangle_callbackref, void *);
    490 static char *d_demangle (const char *, int, size_t *);
    491 
    492 #ifdef CP_DEMANGLE_DEBUG
    493 
    494 static void
    495 d_dump (struct demangle_component *dc, int indent)
    496 {
    497   int i;
    498 
    499   if (dc == NULL)
    500     {
    501       if (indent == 0)
    502         printf ("failed demangling\n");
    503       return;
    504     }
    505 
    506   for (i = 0; i < indent; ++i)
    507     putchar (' ');
    508 
    509   switch (dc->type)
    510     {
    511     case DEMANGLE_COMPONENT_NAME:
    512       printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
    513       return;
    514     case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
    515       printf ("template parameter %ld\n", dc->u.s_number.number);
    516       return;
    517     case DEMANGLE_COMPONENT_CTOR:
    518       printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
    519       d_dump (dc->u.s_ctor.name, indent + 2);
    520       return;
    521     case DEMANGLE_COMPONENT_DTOR:
    522       printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
    523       d_dump (dc->u.s_dtor.name, indent + 2);
    524       return;
    525     case DEMANGLE_COMPONENT_SUB_STD:
    526       printf ("standard substitution %s\n", dc->u.s_string.string);
    527       return;
    528     case DEMANGLE_COMPONENT_BUILTIN_TYPE:
    529       printf ("builtin type %s\n", dc->u.s_builtin.type->name);
    530       return;
    531     case DEMANGLE_COMPONENT_OPERATOR:
    532       printf ("operator %s\n", dc->u.s_operator.op->name);
    533       return;
    534     case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
    535       printf ("extended operator with %d args\n",
    536 	      dc->u.s_extended_operator.args);
    537       d_dump (dc->u.s_extended_operator.name, indent + 2);
    538       return;
    539 
    540     case DEMANGLE_COMPONENT_QUAL_NAME:
    541       printf ("qualified name\n");
    542       break;
    543     case DEMANGLE_COMPONENT_LOCAL_NAME:
    544       printf ("local name\n");
    545       break;
    546     case DEMANGLE_COMPONENT_TYPED_NAME:
    547       printf ("typed name\n");
    548       break;
    549     case DEMANGLE_COMPONENT_TEMPLATE:
    550       printf ("template\n");
    551       break;
    552     case DEMANGLE_COMPONENT_VTABLE:
    553       printf ("vtable\n");
    554       break;
    555     case DEMANGLE_COMPONENT_VTT:
    556       printf ("VTT\n");
    557       break;
    558     case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
    559       printf ("construction vtable\n");
    560       break;
    561     case DEMANGLE_COMPONENT_TYPEINFO:
    562       printf ("typeinfo\n");
    563       break;
    564     case DEMANGLE_COMPONENT_TYPEINFO_NAME:
    565       printf ("typeinfo name\n");
    566       break;
    567     case DEMANGLE_COMPONENT_TYPEINFO_FN:
    568       printf ("typeinfo function\n");
    569       break;
    570     case DEMANGLE_COMPONENT_THUNK:
    571       printf ("thunk\n");
    572       break;
    573     case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
    574       printf ("virtual thunk\n");
    575       break;
    576     case DEMANGLE_COMPONENT_COVARIANT_THUNK:
    577       printf ("covariant thunk\n");
    578       break;
    579     case DEMANGLE_COMPONENT_JAVA_CLASS:
    580       printf ("java class\n");
    581       break;
    582     case DEMANGLE_COMPONENT_GUARD:
    583       printf ("guard\n");
    584       break;
    585     case DEMANGLE_COMPONENT_REFTEMP:
    586       printf ("reference temporary\n");
    587       break;
    588     case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
    589       printf ("hidden alias\n");
    590       break;
    591     case DEMANGLE_COMPONENT_RESTRICT:
    592       printf ("restrict\n");
    593       break;
    594     case DEMANGLE_COMPONENT_VOLATILE:
    595       printf ("volatile\n");
    596       break;
    597     case DEMANGLE_COMPONENT_CONST:
    598       printf ("const\n");
    599       break;
    600     case DEMANGLE_COMPONENT_RESTRICT_THIS:
    601       printf ("restrict this\n");
    602       break;
    603     case DEMANGLE_COMPONENT_VOLATILE_THIS:
    604       printf ("volatile this\n");
    605       break;
    606     case DEMANGLE_COMPONENT_CONST_THIS:
    607       printf ("const this\n");
    608       break;
    609     case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
    610       printf ("vendor type qualifier\n");
    611       break;
    612     case DEMANGLE_COMPONENT_POINTER:
    613       printf ("pointer\n");
    614       break;
    615     case DEMANGLE_COMPONENT_REFERENCE:
    616       printf ("reference\n");
    617       break;
    618     case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
    619       printf ("rvalue reference\n");
    620       break;
    621     case DEMANGLE_COMPONENT_COMPLEX:
    622       printf ("complex\n");
    623       break;
    624     case DEMANGLE_COMPONENT_IMAGINARY:
    625       printf ("imaginary\n");
    626       break;
    627     case DEMANGLE_COMPONENT_VENDOR_TYPE:
    628       printf ("vendor type\n");
    629       break;
    630     case DEMANGLE_COMPONENT_FUNCTION_TYPE:
    631       printf ("function type\n");
    632       break;
    633     case DEMANGLE_COMPONENT_ARRAY_TYPE:
    634       printf ("array type\n");
    635       break;
    636     case DEMANGLE_COMPONENT_PTRMEM_TYPE:
    637       printf ("pointer to member type\n");
    638       break;
    639     case DEMANGLE_COMPONENT_ARGLIST:
    640       printf ("argument list\n");
    641       break;
    642     case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
    643       printf ("template argument list\n");
    644       break;
    645     case DEMANGLE_COMPONENT_CAST:
    646       printf ("cast\n");
    647       break;
    648     case DEMANGLE_COMPONENT_UNARY:
    649       printf ("unary operator\n");
    650       break;
    651     case DEMANGLE_COMPONENT_BINARY:
    652       printf ("binary operator\n");
    653       break;
    654     case DEMANGLE_COMPONENT_BINARY_ARGS:
    655       printf ("binary operator arguments\n");
    656       break;
    657     case DEMANGLE_COMPONENT_TRINARY:
    658       printf ("trinary operator\n");
    659       break;
    660     case DEMANGLE_COMPONENT_TRINARY_ARG1:
    661       printf ("trinary operator arguments 1\n");
    662       break;
    663     case DEMANGLE_COMPONENT_TRINARY_ARG2:
    664       printf ("trinary operator arguments 1\n");
    665       break;
    666     case DEMANGLE_COMPONENT_LITERAL:
    667       printf ("literal\n");
    668       break;
    669     case DEMANGLE_COMPONENT_LITERAL_NEG:
    670       printf ("negative literal\n");
    671       break;
    672     case DEMANGLE_COMPONENT_JAVA_RESOURCE:
    673       printf ("java resource\n");
    674       break;
    675     case DEMANGLE_COMPONENT_COMPOUND_NAME:
    676       printf ("compound name\n");
    677       break;
    678     case DEMANGLE_COMPONENT_CHARACTER:
    679       printf ("character '%c'\n",  dc->u.s_character.character);
    680       return;
    681     case DEMANGLE_COMPONENT_DECLTYPE:
    682       printf ("decltype\n");
    683       break;
    684     case DEMANGLE_COMPONENT_PACK_EXPANSION:
    685       printf ("pack expansion\n");
    686       break;
    687     }
    688 
    689   d_dump (d_left (dc), indent + 2);
    690   d_dump (d_right (dc), indent + 2);
    691 }
    692 
    693 #endif /* CP_DEMANGLE_DEBUG */
    694 
    695 /* Fill in a DEMANGLE_COMPONENT_NAME.  */
    696 
    697 CP_STATIC_IF_GLIBCPP_V3
    698 int
    699 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
    700 {
    701   if (p == NULL || s == NULL || len == 0)
    702     return 0;
    703   p->type = DEMANGLE_COMPONENT_NAME;
    704   p->u.s_name.s = s;
    705   p->u.s_name.len = len;
    706   return 1;
    707 }
    708 
    709 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR.  */
    710 
    711 CP_STATIC_IF_GLIBCPP_V3
    712 int
    713 cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
    714                                        struct demangle_component *name)
    715 {
    716   if (p == NULL || args < 0 || name == NULL)
    717     return 0;
    718   p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
    719   p->u.s_extended_operator.args = args;
    720   p->u.s_extended_operator.name = name;
    721   return 1;
    722 }
    723 
    724 /* Fill in a DEMANGLE_COMPONENT_CTOR.  */
    725 
    726 CP_STATIC_IF_GLIBCPP_V3
    727 int
    728 cplus_demangle_fill_ctor (struct demangle_component *p,
    729                           enum gnu_v3_ctor_kinds kind,
    730                           struct demangle_component *name)
    731 {
    732   if (p == NULL
    733       || name == NULL
    734       || (kind < gnu_v3_complete_object_ctor
    735 	  && kind > gnu_v3_complete_object_allocating_ctor))
    736     return 0;
    737   p->type = DEMANGLE_COMPONENT_CTOR;
    738   p->u.s_ctor.kind = kind;
    739   p->u.s_ctor.name = name;
    740   return 1;
    741 }
    742 
    743 /* Fill in a DEMANGLE_COMPONENT_DTOR.  */
    744 
    745 CP_STATIC_IF_GLIBCPP_V3
    746 int
    747 cplus_demangle_fill_dtor (struct demangle_component *p,
    748                           enum gnu_v3_dtor_kinds kind,
    749                           struct demangle_component *name)
    750 {
    751   if (p == NULL
    752       || name == NULL
    753       || (kind < gnu_v3_deleting_dtor
    754 	  && kind > gnu_v3_base_object_dtor))
    755     return 0;
    756   p->type = DEMANGLE_COMPONENT_DTOR;
    757   p->u.s_dtor.kind = kind;
    758   p->u.s_dtor.name = name;
    759   return 1;
    760 }
    761 
    762 /* Add a new component.  */
    763 
    764 static struct demangle_component *
    765 d_make_empty (struct d_info *di)
    766 {
    767   struct demangle_component *p;
    768 
    769   if (di->next_comp >= di->num_comps)
    770     return NULL;
    771   p = &di->comps[di->next_comp];
    772   ++di->next_comp;
    773   return p;
    774 }
    775 
    776 /* Add a new generic component.  */
    777 
    778 static struct demangle_component *
    779 d_make_comp (struct d_info *di, enum demangle_component_type type,
    780              struct demangle_component *left,
    781              struct demangle_component *right)
    782 {
    783   struct demangle_component *p;
    784 
    785   /* We check for errors here.  A typical error would be a NULL return
    786      from a subroutine.  We catch those here, and return NULL
    787      upward.  */
    788   switch (type)
    789     {
    790       /* These types require two parameters.  */
    791     case DEMANGLE_COMPONENT_QUAL_NAME:
    792     case DEMANGLE_COMPONENT_LOCAL_NAME:
    793     case DEMANGLE_COMPONENT_TYPED_NAME:
    794     case DEMANGLE_COMPONENT_TEMPLATE:
    795     case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
    796     case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
    797     case DEMANGLE_COMPONENT_PTRMEM_TYPE:
    798     case DEMANGLE_COMPONENT_UNARY:
    799     case DEMANGLE_COMPONENT_BINARY:
    800     case DEMANGLE_COMPONENT_BINARY_ARGS:
    801     case DEMANGLE_COMPONENT_TRINARY:
    802     case DEMANGLE_COMPONENT_TRINARY_ARG1:
    803     case DEMANGLE_COMPONENT_TRINARY_ARG2:
    804     case DEMANGLE_COMPONENT_LITERAL:
    805     case DEMANGLE_COMPONENT_LITERAL_NEG:
    806     case DEMANGLE_COMPONENT_COMPOUND_NAME:
    807       if (left == NULL || right == NULL)
    808 	return NULL;
    809       break;
    810 
    811       /* These types only require one parameter.  */
    812     case DEMANGLE_COMPONENT_VTABLE:
    813     case DEMANGLE_COMPONENT_VTT:
    814     case DEMANGLE_COMPONENT_TYPEINFO:
    815     case DEMANGLE_COMPONENT_TYPEINFO_NAME:
    816     case DEMANGLE_COMPONENT_TYPEINFO_FN:
    817     case DEMANGLE_COMPONENT_THUNK:
    818     case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
    819     case DEMANGLE_COMPONENT_COVARIANT_THUNK:
    820     case DEMANGLE_COMPONENT_JAVA_CLASS:
    821     case DEMANGLE_COMPONENT_GUARD:
    822     case DEMANGLE_COMPONENT_REFTEMP:
    823     case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
    824     case DEMANGLE_COMPONENT_POINTER:
    825     case DEMANGLE_COMPONENT_REFERENCE:
    826     case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
    827     case DEMANGLE_COMPONENT_COMPLEX:
    828     case DEMANGLE_COMPONENT_IMAGINARY:
    829     case DEMANGLE_COMPONENT_VENDOR_TYPE:
    830     case DEMANGLE_COMPONENT_CAST:
    831     case DEMANGLE_COMPONENT_JAVA_RESOURCE:
    832     case DEMANGLE_COMPONENT_DECLTYPE:
    833     case DEMANGLE_COMPONENT_PACK_EXPANSION:
    834       if (left == NULL)
    835 	return NULL;
    836       break;
    837 
    838       /* This needs a right parameter, but the left parameter can be
    839 	 empty.  */
    840     case DEMANGLE_COMPONENT_ARRAY_TYPE:
    841       if (right == NULL)
    842 	return NULL;
    843       break;
    844 
    845       /* These are allowed to have no parameters--in some cases they
    846 	 will be filled in later.  */
    847     case DEMANGLE_COMPONENT_FUNCTION_TYPE:
    848     case DEMANGLE_COMPONENT_RESTRICT:
    849     case DEMANGLE_COMPONENT_VOLATILE:
    850     case DEMANGLE_COMPONENT_CONST:
    851     case DEMANGLE_COMPONENT_RESTRICT_THIS:
    852     case DEMANGLE_COMPONENT_VOLATILE_THIS:
    853     case DEMANGLE_COMPONENT_CONST_THIS:
    854     case DEMANGLE_COMPONENT_ARGLIST:
    855     case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
    856       break;
    857 
    858       /* Other types should not be seen here.  */
    859     default:
    860       return NULL;
    861     }
    862 
    863   p = d_make_empty (di);
    864   if (p != NULL)
    865     {
    866       p->type = type;
    867       p->u.s_binary.left = left;
    868       p->u.s_binary.right = right;
    869     }
    870   return p;
    871 }
    872 
    873 /* Add a new name component.  */
    874 
    875 static struct demangle_component *
    876 d_make_name (struct d_info *di, const char *s, int len)
    877 {
    878   struct demangle_component *p;
    879 
    880   p = d_make_empty (di);
    881   if (! cplus_demangle_fill_name (p, s, len))
    882     return NULL;
    883   return p;
    884 }
    885 
    886 /* Add a new builtin type component.  */
    887 
    888 static struct demangle_component *
    889 d_make_builtin_type (struct d_info *di,
    890                      const struct demangle_builtin_type_info *type)
    891 {
    892   struct demangle_component *p;
    893 
    894   if (type == NULL)
    895     return NULL;
    896   p = d_make_empty (di);
    897   if (p != NULL)
    898     {
    899       p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
    900       p->u.s_builtin.type = type;
    901     }
    902   return p;
    903 }
    904 
    905 /* Add a new operator component.  */
    906 
    907 static struct demangle_component *
    908 d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
    909 {
    910   struct demangle_component *p;
    911 
    912   p = d_make_empty (di);
    913   if (p != NULL)
    914     {
    915       p->type = DEMANGLE_COMPONENT_OPERATOR;
    916       p->u.s_operator.op = op;
    917     }
    918   return p;
    919 }
    920 
    921 /* Add a new extended operator component.  */
    922 
    923 static struct demangle_component *
    924 d_make_extended_operator (struct d_info *di, int args,
    925                           struct demangle_component *name)
    926 {
    927   struct demangle_component *p;
    928 
    929   p = d_make_empty (di);
    930   if (! cplus_demangle_fill_extended_operator (p, args, name))
    931     return NULL;
    932   return p;
    933 }
    934 
    935 /* Add a new constructor component.  */
    936 
    937 static struct demangle_component *
    938 d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
    939              struct demangle_component *name)
    940 {
    941   struct demangle_component *p;
    942 
    943   p = d_make_empty (di);
    944   if (! cplus_demangle_fill_ctor (p, kind, name))
    945     return NULL;
    946   return p;
    947 }
    948 
    949 /* Add a new destructor component.  */
    950 
    951 static struct demangle_component *
    952 d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
    953              struct demangle_component *name)
    954 {
    955   struct demangle_component *p;
    956 
    957   p = d_make_empty (di);
    958   if (! cplus_demangle_fill_dtor (p, kind, name))
    959     return NULL;
    960   return p;
    961 }
    962 
    963 /* Add a new template parameter.  */
    964 
    965 static struct demangle_component *
    966 d_make_template_param (struct d_info *di, long i)
    967 {
    968   struct demangle_component *p;
    969 
    970   p = d_make_empty (di);
    971   if (p != NULL)
    972     {
    973       p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
    974       p->u.s_number.number = i;
    975     }
    976   return p;
    977 }
    978 
    979 /* Add a new standard substitution component.  */
    980 
    981 static struct demangle_component *
    982 d_make_sub (struct d_info *di, const char *name, int len)
    983 {
    984   struct demangle_component *p;
    985 
    986   p = d_make_empty (di);
    987   if (p != NULL)
    988     {
    989       p->type = DEMANGLE_COMPONENT_SUB_STD;
    990       p->u.s_string.string = name;
    991       p->u.s_string.len = len;
    992     }
    993   return p;
    994 }
    995 
    996 /* <mangled-name> ::= _Z <encoding>
    997 
    998    TOP_LEVEL is non-zero when called at the top level.  */
    999 
   1000 CP_STATIC_IF_GLIBCPP_V3
   1001 struct demangle_component *
   1002 cplus_demangle_mangled_name (struct d_info *di, int top_level)
   1003 {
   1004   if (! d_check_char (di, '_'))
   1005     return NULL;
   1006   if (! d_check_char (di, 'Z'))
   1007     return NULL;
   1008   return d_encoding (di, top_level);
   1009 }
   1010 
   1011 /* Return whether a function should have a return type.  The argument
   1012    is the function name, which may be qualified in various ways.  The
   1013    rules are that template functions have return types with some
   1014    exceptions, function types which are not part of a function name
   1015    mangling have return types with some exceptions, and non-template
   1016    function names do not have return types.  The exceptions are that
   1017    constructors, destructors, and conversion operators do not have
   1018    return types.  */
   1019 
   1020 static int
   1021 has_return_type (struct demangle_component *dc)
   1022 {
   1023   if (dc == NULL)
   1024     return 0;
   1025   switch (dc->type)
   1026     {
   1027     default:
   1028       return 0;
   1029     case DEMANGLE_COMPONENT_TEMPLATE:
   1030       return ! is_ctor_dtor_or_conversion (d_left (dc));
   1031     case DEMANGLE_COMPONENT_RESTRICT_THIS:
   1032     case DEMANGLE_COMPONENT_VOLATILE_THIS:
   1033     case DEMANGLE_COMPONENT_CONST_THIS:
   1034       return has_return_type (d_left (dc));
   1035     }
   1036 }
   1037 
   1038 /* Return whether a name is a constructor, a destructor, or a
   1039    conversion operator.  */
   1040 
   1041 static int
   1042 is_ctor_dtor_or_conversion (struct demangle_component *dc)
   1043 {
   1044   if (dc == NULL)
   1045     return 0;
   1046   switch (dc->type)
   1047     {
   1048     default:
   1049       return 0;
   1050     case DEMANGLE_COMPONENT_QUAL_NAME:
   1051     case DEMANGLE_COMPONENT_LOCAL_NAME:
   1052       return is_ctor_dtor_or_conversion (d_right (dc));
   1053     case DEMANGLE_COMPONENT_CTOR:
   1054     case DEMANGLE_COMPONENT_DTOR:
   1055     case DEMANGLE_COMPONENT_CAST:
   1056       return 1;
   1057     }
   1058 }
   1059 
   1060 /* <encoding> ::= <(function) name> <bare-function-type>
   1061               ::= <(data) name>
   1062               ::= <special-name>
   1063 
   1064    TOP_LEVEL is non-zero when called at the top level, in which case
   1065    if DMGL_PARAMS is not set we do not demangle the function
   1066    parameters.  We only set this at the top level, because otherwise
   1067    we would not correctly demangle names in local scopes.  */
   1068 
   1069 static struct demangle_component *
   1070 d_encoding (struct d_info *di, int top_level)
   1071 {
   1072   char peek = d_peek_char (di);
   1073 
   1074   if (peek == 'G' || peek == 'T')
   1075     return d_special_name (di);
   1076   else
   1077     {
   1078       struct demangle_component *dc;
   1079 
   1080       dc = d_name (di);
   1081 
   1082       if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
   1083 	{
   1084 	  /* Strip off any initial CV-qualifiers, as they really apply
   1085 	     to the `this' parameter, and they were not output by the
   1086 	     v2 demangler without DMGL_PARAMS.  */
   1087 	  while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
   1088 		 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
   1089 		 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
   1090 	    dc = d_left (dc);
   1091 
   1092 	  /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
   1093 	     there may be CV-qualifiers on its right argument which
   1094 	     really apply here; this happens when parsing a class
   1095 	     which is local to a function.  */
   1096 	  if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
   1097 	    {
   1098 	      struct demangle_component *dcr;
   1099 
   1100 	      dcr = d_right (dc);
   1101 	      while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS
   1102 		     || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS
   1103 		     || dcr->type == DEMANGLE_COMPONENT_CONST_THIS)
   1104 		dcr = d_left (dcr);
   1105 	      dc->u.s_binary.right = dcr;
   1106 	    }
   1107 
   1108 	  return dc;
   1109 	}
   1110 
   1111       peek = d_peek_char (di);
   1112       if (dc == NULL || peek == '\0' || peek == 'E')
   1113 	return dc;
   1114       return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
   1115 			  d_bare_function_type (di, has_return_type (dc)));
   1116     }
   1117 }
   1118 
   1119 /* <name> ::= <nested-name>
   1120           ::= <unscoped-name>
   1121           ::= <unscoped-template-name> <template-args>
   1122           ::= <local-name>
   1123 
   1124    <unscoped-name> ::= <unqualified-name>
   1125                    ::= St <unqualified-name>
   1126 
   1127    <unscoped-template-name> ::= <unscoped-name>
   1128                             ::= <substitution>
   1129 */
   1130 
   1131 static struct demangle_component *
   1132 d_name (struct d_info *di)
   1133 {
   1134   char peek = d_peek_char (di);
   1135   struct demangle_component *dc;
   1136 
   1137   switch (peek)
   1138     {
   1139     case 'N':
   1140       return d_nested_name (di);
   1141 
   1142     case 'Z':
   1143       return d_local_name (di);
   1144 
   1145     case 'L':
   1146       return d_unqualified_name (di);
   1147 
   1148     case 'S':
   1149       {
   1150 	int subst;
   1151 
   1152 	if (d_peek_next_char (di) != 't')
   1153 	  {
   1154 	    dc = d_substitution (di, 0);
   1155 	    subst = 1;
   1156 	  }
   1157 	else
   1158 	  {
   1159 	    d_advance (di, 2);
   1160 	    dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
   1161 			      d_make_name (di, "std", 3),
   1162 			      d_unqualified_name (di));
   1163 	    di->expansion += 3;
   1164 	    subst = 0;
   1165 	  }
   1166 
   1167 	if (d_peek_char (di) != 'I')
   1168 	  {
   1169 	    /* The grammar does not permit this case to occur if we
   1170 	       called d_substitution() above (i.e., subst == 1).  We
   1171 	       don't bother to check.  */
   1172 	  }
   1173 	else
   1174 	  {
   1175 	    /* This is <template-args>, which means that we just saw
   1176 	       <unscoped-template-name>, which is a substitution
   1177 	       candidate if we didn't just get it from a
   1178 	       substitution.  */
   1179 	    if (! subst)
   1180 	      {
   1181 		if (! d_add_substitution (di, dc))
   1182 		  return NULL;
   1183 	      }
   1184 	    dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
   1185 			      d_template_args (di));
   1186 	  }
   1187 
   1188 	return dc;
   1189       }
   1190 
   1191     default:
   1192       dc = d_unqualified_name (di);
   1193       if (d_peek_char (di) == 'I')
   1194 	{
   1195 	  /* This is <template-args>, which means that we just saw
   1196 	     <unscoped-template-name>, which is a substitution
   1197 	     candidate.  */
   1198 	  if (! d_add_substitution (di, dc))
   1199 	    return NULL;
   1200 	  dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
   1201 			    d_template_args (di));
   1202 	}
   1203       return dc;
   1204     }
   1205 }
   1206 
   1207 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
   1208                  ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
   1209 */
   1210 
   1211 static struct demangle_component *
   1212 d_nested_name (struct d_info *di)
   1213 {
   1214   struct demangle_component *ret;
   1215   struct demangle_component **pret;
   1216 
   1217   if (! d_check_char (di, 'N'))
   1218     return NULL;
   1219 
   1220   pret = d_cv_qualifiers (di, &ret, 1);
   1221   if (pret == NULL)
   1222     return NULL;
   1223 
   1224   *pret = d_prefix (di);
   1225   if (*pret == NULL)
   1226     return NULL;
   1227 
   1228   if (! d_check_char (di, 'E'))
   1229     return NULL;
   1230 
   1231   return ret;
   1232 }
   1233 
   1234 /* <prefix> ::= <prefix> <unqualified-name>
   1235             ::= <template-prefix> <template-args>
   1236             ::= <template-param>
   1237             ::=
   1238             ::= <substitution>
   1239 
   1240    <template-prefix> ::= <prefix> <(template) unqualified-name>
   1241                      ::= <template-param>
   1242                      ::= <substitution>
   1243 */
   1244 
   1245 static struct demangle_component *
   1246 d_prefix (struct d_info *di)
   1247 {
   1248   struct demangle_component *ret = NULL;
   1249 
   1250   while (1)
   1251     {
   1252       char peek;
   1253       enum demangle_component_type comb_type;
   1254       struct demangle_component *dc;
   1255 
   1256       peek = d_peek_char (di);
   1257       if (peek == '\0')
   1258 	return NULL;
   1259 
   1260       /* The older code accepts a <local-name> here, but I don't see
   1261 	 that in the grammar.  The older code does not accept a
   1262 	 <template-param> here.  */
   1263 
   1264       comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
   1265       if (IS_DIGIT (peek)
   1266 	  || IS_LOWER (peek)
   1267 	  || peek == 'C'
   1268 	  || peek == 'D'
   1269 	  || peek == 'L')
   1270 	dc = d_unqualified_name (di);
   1271       else if (peek == 'S')
   1272 	dc = d_substitution (di, 1);
   1273       else if (peek == 'I')
   1274 	{
   1275 	  if (ret == NULL)
   1276 	    return NULL;
   1277 	  comb_type = DEMANGLE_COMPONENT_TEMPLATE;
   1278 	  dc = d_template_args (di);
   1279 	}
   1280       else if (peek == 'T')
   1281 	dc = d_template_param (di);
   1282       else if (peek == 'E')
   1283 	return ret;
   1284       else
   1285 	return NULL;
   1286 
   1287       if (ret == NULL)
   1288 	ret = dc;
   1289       else
   1290 	ret = d_make_comp (di, comb_type, ret, dc);
   1291 
   1292       if (peek != 'S' && d_peek_char (di) != 'E')
   1293 	{
   1294 	  if (! d_add_substitution (di, ret))
   1295 	    return NULL;
   1296 	}
   1297     }
   1298 }
   1299 
   1300 /* <unqualified-name> ::= <operator-name>
   1301                       ::= <ctor-dtor-name>
   1302                       ::= <source-name>
   1303 		      ::= <local-source-name>
   1304 
   1305     <local-source-name>	::= L <source-name> <discriminator>
   1306 */
   1307 
   1308 static struct demangle_component *
   1309 d_unqualified_name (struct d_info *di)
   1310 {
   1311   char peek;
   1312 
   1313   peek = d_peek_char (di);
   1314   if (IS_DIGIT (peek))
   1315     return d_source_name (di);
   1316   else if (IS_LOWER (peek))
   1317     {
   1318       struct demangle_component *ret;
   1319 
   1320       ret = d_operator_name (di);
   1321       if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
   1322 	di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
   1323       return ret;
   1324     }
   1325   else if (peek == 'C' || peek == 'D')
   1326     return d_ctor_dtor_name (di);
   1327   else if (peek == 'L')
   1328     {
   1329       struct demangle_component * ret;
   1330 
   1331       d_advance (di, 1);
   1332 
   1333       ret = d_source_name (di);
   1334       if (ret == NULL)
   1335 	return NULL;
   1336       if (! d_discriminator (di))
   1337 	return NULL;
   1338       return ret;
   1339     }
   1340   else
   1341     return NULL;
   1342 }
   1343 
   1344 /* <source-name> ::= <(positive length) number> <identifier>  */
   1345 
   1346 static struct demangle_component *
   1347 d_source_name (struct d_info *di)
   1348 {
   1349   long len;
   1350   struct demangle_component *ret;
   1351 
   1352   len = d_number (di);
   1353   if (len <= 0)
   1354     return NULL;
   1355   ret = d_identifier (di, len);
   1356   di->last_name = ret;
   1357   return ret;
   1358 }
   1359 
   1360 /* number ::= [n] <(non-negative decimal integer)>  */
   1361 
   1362 static long
   1363 d_number (struct d_info *di)
   1364 {
   1365   int negative;
   1366   char peek;
   1367   long ret;
   1368 
   1369   negative = 0;
   1370   peek = d_peek_char (di);
   1371   if (peek == 'n')
   1372     {
   1373       negative = 1;
   1374       d_advance (di, 1);
   1375       peek = d_peek_char (di);
   1376     }
   1377 
   1378   ret = 0;
   1379   while (1)
   1380     {
   1381       if (! IS_DIGIT (peek))
   1382 	{
   1383 	  if (negative)
   1384 	    ret = - ret;
   1385 	  return ret;
   1386 	}
   1387       ret = ret * 10 + peek - '0';
   1388       d_advance (di, 1);
   1389       peek = d_peek_char (di);
   1390     }
   1391 }
   1392 
   1393 /* identifier ::= <(unqualified source code identifier)>  */
   1394 
   1395 static struct demangle_component *
   1396 d_identifier (struct d_info *di, int len)
   1397 {
   1398   const char *name;
   1399 
   1400   name = d_str (di);
   1401 
   1402   if (di->send - name < len)
   1403     return NULL;
   1404 
   1405   d_advance (di, len);
   1406 
   1407   /* A Java mangled name may have a trailing '$' if it is a C++
   1408      keyword.  This '$' is not included in the length count.  We just
   1409      ignore the '$'.  */
   1410   if ((di->options & DMGL_JAVA) != 0
   1411       && d_peek_char (di) == '$')
   1412     d_advance (di, 1);
   1413 
   1414   /* Look for something which looks like a gcc encoding of an
   1415      anonymous namespace, and replace it with a more user friendly
   1416      name.  */
   1417   if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
   1418       && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
   1419 		 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
   1420     {
   1421       const char *s;
   1422 
   1423       s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
   1424       if ((*s == '.' || *s == '_' || *s == '$')
   1425 	  && s[1] == 'N')
   1426 	{
   1427 	  di->expansion -= len - sizeof "(anonymous namespace)";
   1428 	  return d_make_name (di, "(anonymous namespace)",
   1429 			      sizeof "(anonymous namespace)" - 1);
   1430 	}
   1431     }
   1432 
   1433   return d_make_name (di, name, len);
   1434 }
   1435 
   1436 /* operator_name ::= many different two character encodings.
   1437                  ::= cv <type>
   1438                  ::= v <digit> <source-name>
   1439 */
   1440 
   1441 #define NL(s) s, (sizeof s) - 1
   1442 
   1443 CP_STATIC_IF_GLIBCPP_V3
   1444 const struct demangle_operator_info cplus_demangle_operators[] =
   1445 {
   1446   { "aN", NL ("&="),        2 },
   1447   { "aS", NL ("="),         2 },
   1448   { "aa", NL ("&&"),        2 },
   1449   { "ad", NL ("&"),         1 },
   1450   { "an", NL ("&"),         2 },
   1451   { "cl", NL ("()"),        2 },
   1452   { "cm", NL (","),         2 },
   1453   { "co", NL ("~"),         1 },
   1454   { "dV", NL ("/="),        2 },
   1455   { "da", NL ("delete[]"),  1 },
   1456   { "de", NL ("*"),         1 },
   1457   { "dl", NL ("delete"),    1 },
   1458   { "dt", NL ("."),         2 },
   1459   { "dv", NL ("/"),         2 },
   1460   { "eO", NL ("^="),        2 },
   1461   { "eo", NL ("^"),         2 },
   1462   { "eq", NL ("=="),        2 },
   1463   { "ge", NL (">="),        2 },
   1464   { "gt", NL (">"),         2 },
   1465   { "ix", NL ("[]"),        2 },
   1466   { "lS", NL ("<<="),       2 },
   1467   { "le", NL ("<="),        2 },
   1468   { "ls", NL ("<<"),        2 },
   1469   { "lt", NL ("<"),         2 },
   1470   { "mI", NL ("-="),        2 },
   1471   { "mL", NL ("*="),        2 },
   1472   { "mi", NL ("-"),         2 },
   1473   { "ml", NL ("*"),         2 },
   1474   { "mm", NL ("--"),        1 },
   1475   { "na", NL ("new[]"),     1 },
   1476   { "ne", NL ("!="),        2 },
   1477   { "ng", NL ("-"),         1 },
   1478   { "nt", NL ("!"),         1 },
   1479   { "nw", NL ("new"),       1 },
   1480   { "oR", NL ("|="),        2 },
   1481   { "oo", NL ("||"),        2 },
   1482   { "or", NL ("|"),         2 },
   1483   { "pL", NL ("+="),        2 },
   1484   { "pl", NL ("+"),         2 },
   1485   { "pm", NL ("->*"),       2 },
   1486   { "pp", NL ("++"),        1 },
   1487   { "ps", NL ("+"),         1 },
   1488   { "pt", NL ("->"),        2 },
   1489   { "qu", NL ("?"),         3 },
   1490   { "rM", NL ("%="),        2 },
   1491   { "rS", NL (">>="),       2 },
   1492   { "rm", NL ("%"),         2 },
   1493   { "rs", NL (">>"),        2 },
   1494   { "st", NL ("sizeof "),   1 },
   1495   { "sz", NL ("sizeof "),   1 },
   1496   { NULL, NULL, 0,          0 }
   1497 };
   1498 
   1499 static struct demangle_component *
   1500 d_operator_name (struct d_info *di)
   1501 {
   1502   char c1;
   1503   char c2;
   1504 
   1505   c1 = d_next_char (di);
   1506   c2 = d_next_char (di);
   1507   if (c1 == 'v' && IS_DIGIT (c2))
   1508     return d_make_extended_operator (di, c2 - '0', d_source_name (di));
   1509   else if (c1 == 'c' && c2 == 'v')
   1510     return d_make_comp (di, DEMANGLE_COMPONENT_CAST,
   1511 			cplus_demangle_type (di), NULL);
   1512   else
   1513     {
   1514       /* LOW is the inclusive lower bound.  */
   1515       int low = 0;
   1516       /* HIGH is the exclusive upper bound.  We subtract one to ignore
   1517 	 the sentinel at the end of the array.  */
   1518       int high = ((sizeof (cplus_demangle_operators)
   1519 		   / sizeof (cplus_demangle_operators[0]))
   1520 		  - 1);
   1521 
   1522       while (1)
   1523 	{
   1524 	  int i;
   1525 	  const struct demangle_operator_info *p;
   1526 
   1527 	  i = low + (high - low) / 2;
   1528 	  p = cplus_demangle_operators + i;
   1529 
   1530 	  if (c1 == p->code[0] && c2 == p->code[1])
   1531 	    return d_make_operator (di, p);
   1532 
   1533 	  if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
   1534 	    high = i;
   1535 	  else
   1536 	    low = i + 1;
   1537 	  if (low == high)
   1538 	    return NULL;
   1539 	}
   1540     }
   1541 }
   1542 
   1543 static struct demangle_component *
   1544 d_make_character (struct d_info *di, int c)
   1545 {
   1546   struct demangle_component *p;
   1547   p = d_make_empty (di);
   1548   if (p != NULL)
   1549     {
   1550       p->type = DEMANGLE_COMPONENT_CHARACTER;
   1551       p->u.s_character.character = c;
   1552     }
   1553   return p;
   1554 }
   1555 
   1556 static struct demangle_component *
   1557 d_java_resource (struct d_info *di)
   1558 {
   1559   struct demangle_component *p = NULL;
   1560   struct demangle_component *next = NULL;
   1561   long len, i;
   1562   char c;
   1563   const char *str;
   1564 
   1565   len = d_number (di);
   1566   if (len <= 1)
   1567     return NULL;
   1568 
   1569   /* Eat the leading '_'.  */
   1570   if (d_next_char (di) != '_')
   1571     return NULL;
   1572   len--;
   1573 
   1574   str = d_str (di);
   1575   i = 0;
   1576 
   1577   while (len > 0)
   1578     {
   1579       c = str[i];
   1580       if (!c)
   1581 	return NULL;
   1582 
   1583       /* Each chunk is either a '$' escape...  */
   1584       if (c == '$')
   1585 	{
   1586 	  i++;
   1587 	  switch (str[i++])
   1588 	    {
   1589 	    case 'S':
   1590 	      c = '/';
   1591 	      break;
   1592 	    case '_':
   1593 	      c = '.';
   1594 	      break;
   1595 	    case '$':
   1596 	      c = '$';
   1597 	      break;
   1598 	    default:
   1599 	      return NULL;
   1600 	    }
   1601 	  next = d_make_character (di, c);
   1602 	  d_advance (di, i);
   1603 	  str = d_str (di);
   1604 	  len -= i;
   1605 	  i = 0;
   1606 	  if (next == NULL)
   1607 	    return NULL;
   1608 	}
   1609       /* ... or a sequence of characters.  */
   1610       else
   1611 	{
   1612 	  while (i < len && str[i] && str[i] != '$')
   1613 	    i++;
   1614 
   1615 	  next = d_make_name (di, str, i);
   1616 	  d_advance (di, i);
   1617 	  str = d_str (di);
   1618 	  len -= i;
   1619 	  i = 0;
   1620 	  if (next == NULL)
   1621 	    return NULL;
   1622 	}
   1623 
   1624       if (p == NULL)
   1625 	p = next;
   1626       else
   1627 	{
   1628 	  p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next);
   1629 	  if (p == NULL)
   1630 	    return NULL;
   1631 	}
   1632     }
   1633 
   1634   p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL);
   1635 
   1636   return p;
   1637 }
   1638 
   1639 /* <special-name> ::= TV <type>
   1640                   ::= TT <type>
   1641                   ::= TI <type>
   1642                   ::= TS <type>
   1643                   ::= GV <(object) name>
   1644                   ::= T <call-offset> <(base) encoding>
   1645                   ::= Tc <call-offset> <call-offset> <(base) encoding>
   1646    Also g++ extensions:
   1647                   ::= TC <type> <(offset) number> _ <(base) type>
   1648                   ::= TF <type>
   1649                   ::= TJ <type>
   1650                   ::= GR <name>
   1651 		  ::= GA <encoding>
   1652 		  ::= Gr <resource name>
   1653 */
   1654 
   1655 static struct demangle_component *
   1656 d_special_name (struct d_info *di)
   1657 {
   1658   di->expansion += 20;
   1659   if (d_check_char (di, 'T'))
   1660     {
   1661       switch (d_next_char (di))
   1662 	{
   1663 	case 'V':
   1664 	  di->expansion -= 5;
   1665 	  return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
   1666 			      cplus_demangle_type (di), NULL);
   1667 	case 'T':
   1668 	  di->expansion -= 10;
   1669 	  return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
   1670 			      cplus_demangle_type (di), NULL);
   1671 	case 'I':
   1672 	  return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
   1673 			      cplus_demangle_type (di), NULL);
   1674 	case 'S':
   1675 	  return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
   1676 			      cplus_demangle_type (di), NULL);
   1677 
   1678 	case 'h':
   1679 	  if (! d_call_offset (di, 'h'))
   1680 	    return NULL;
   1681 	  return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
   1682 			      d_encoding (di, 0), NULL);
   1683 
   1684 	case 'v':
   1685 	  if (! d_call_offset (di, 'v'))
   1686 	    return NULL;
   1687 	  return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
   1688 			      d_encoding (di, 0), NULL);
   1689 
   1690 	case 'c':
   1691 	  if (! d_call_offset (di, '\0'))
   1692 	    return NULL;
   1693 	  if (! d_call_offset (di, '\0'))
   1694 	    return NULL;
   1695 	  return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
   1696 			      d_encoding (di, 0), NULL);
   1697 
   1698 	case 'C':
   1699 	  {
   1700 	    struct demangle_component *derived_type;
   1701 	    long offset;
   1702 	    struct demangle_component *base_type;
   1703 
   1704 	    derived_type = cplus_demangle_type (di);
   1705 	    offset = d_number (di);
   1706 	    if (offset < 0)
   1707 	      return NULL;
   1708 	    if (! d_check_char (di, '_'))
   1709 	      return NULL;
   1710 	    base_type = cplus_demangle_type (di);
   1711 	    /* We don't display the offset.  FIXME: We should display
   1712 	       it in verbose mode.  */
   1713 	    di->expansion += 5;
   1714 	    return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
   1715 				base_type, derived_type);
   1716 	  }
   1717 
   1718 	case 'F':
   1719 	  return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
   1720 			      cplus_demangle_type (di), NULL);
   1721 	case 'J':
   1722 	  return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
   1723 			      cplus_demangle_type (di), NULL);
   1724 
   1725 	default:
   1726 	  return NULL;
   1727 	}
   1728     }
   1729   else if (d_check_char (di, 'G'))
   1730     {
   1731       switch (d_next_char (di))
   1732 	{
   1733 	case 'V':
   1734 	  return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
   1735 
   1736 	case 'R':
   1737 	  return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, d_name (di),
   1738 			      NULL);
   1739 
   1740 	case 'A':
   1741 	  return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
   1742 			      d_encoding (di, 0), NULL);
   1743 
   1744 	case 'r':
   1745 	  return d_java_resource (di);
   1746 
   1747 	default:
   1748 	  return NULL;
   1749 	}
   1750     }
   1751   else
   1752     return NULL;
   1753 }
   1754 
   1755 /* <call-offset> ::= h <nv-offset> _
   1756                  ::= v <v-offset> _
   1757 
   1758    <nv-offset> ::= <(offset) number>
   1759 
   1760    <v-offset> ::= <(offset) number> _ <(virtual offset) number>
   1761 
   1762    The C parameter, if not '\0', is a character we just read which is
   1763    the start of the <call-offset>.
   1764 
   1765    We don't display the offset information anywhere.  FIXME: We should
   1766    display it in verbose mode.  */
   1767 
   1768 static int
   1769 d_call_offset (struct d_info *di, int c)
   1770 {
   1771   if (c == '\0')
   1772     c = d_next_char (di);
   1773 
   1774   if (c == 'h')
   1775     d_number (di);
   1776   else if (c == 'v')
   1777     {
   1778       d_number (di);
   1779       if (! d_check_char (di, '_'))
   1780 	return 0;
   1781       d_number (di);
   1782     }
   1783   else
   1784     return 0;
   1785 
   1786   if (! d_check_char (di, '_'))
   1787     return 0;
   1788 
   1789   return 1;
   1790 }
   1791 
   1792 /* <ctor-dtor-name> ::= C1
   1793                     ::= C2
   1794                     ::= C3
   1795                     ::= D0
   1796                     ::= D1
   1797                     ::= D2
   1798 */
   1799 
   1800 static struct demangle_component *
   1801 d_ctor_dtor_name (struct d_info *di)
   1802 {
   1803   if (di->last_name != NULL)
   1804     {
   1805       if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
   1806 	di->expansion += di->last_name->u.s_name.len;
   1807       else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
   1808 	di->expansion += di->last_name->u.s_string.len;
   1809     }
   1810   switch (d_peek_char (di))
   1811     {
   1812     case 'C':
   1813       {
   1814 	enum gnu_v3_ctor_kinds kind;
   1815 
   1816 	switch (d_peek_next_char (di))
   1817 	  {
   1818 	  case '1':
   1819 	    kind = gnu_v3_complete_object_ctor;
   1820 	    break;
   1821 	  case '2':
   1822 	    kind = gnu_v3_base_object_ctor;
   1823 	    break;
   1824 	  case '3':
   1825 	    kind = gnu_v3_complete_object_allocating_ctor;
   1826 	    break;
   1827 	  default:
   1828 	    return NULL;
   1829 	  }
   1830 	d_advance (di, 2);
   1831 	return d_make_ctor (di, kind, di->last_name);
   1832       }
   1833 
   1834     case 'D':
   1835       {
   1836 	enum gnu_v3_dtor_kinds kind;
   1837 
   1838 	switch (d_peek_next_char (di))
   1839 	  {
   1840 	  case '0':
   1841 	    kind = gnu_v3_deleting_dtor;
   1842 	    break;
   1843 	  case '1':
   1844 	    kind = gnu_v3_complete_object_dtor;
   1845 	    break;
   1846 	  case '2':
   1847 	    kind = gnu_v3_base_object_dtor;
   1848 	    break;
   1849 	  default:
   1850 	    return NULL;
   1851 	  }
   1852 	d_advance (di, 2);
   1853 	return d_make_dtor (di, kind, di->last_name);
   1854       }
   1855 
   1856     default:
   1857       return NULL;
   1858     }
   1859 }
   1860 
   1861 /* <type> ::= <builtin-type>
   1862           ::= <function-type>
   1863           ::= <class-enum-type>
   1864           ::= <array-type>
   1865           ::= <pointer-to-member-type>
   1866           ::= <template-param>
   1867           ::= <template-template-param> <template-args>
   1868           ::= <substitution>
   1869           ::= <CV-qualifiers> <type>
   1870           ::= P <type>
   1871           ::= R <type>
   1872           ::= O <type> (C++0x)
   1873           ::= C <type>
   1874           ::= G <type>
   1875           ::= U <source-name> <type>
   1876 
   1877    <builtin-type> ::= various one letter codes
   1878                   ::= u <source-name>
   1879 */
   1880 
   1881 CP_STATIC_IF_GLIBCPP_V3
   1882 const struct demangle_builtin_type_info
   1883 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
   1884 {
   1885   /* a */ { NL ("signed char"),	NL ("signed char"),	D_PRINT_DEFAULT },
   1886   /* b */ { NL ("bool"),	NL ("boolean"),		D_PRINT_BOOL },
   1887   /* c */ { NL ("char"),	NL ("byte"),		D_PRINT_DEFAULT },
   1888   /* d */ { NL ("double"),	NL ("double"),		D_PRINT_FLOAT },
   1889   /* e */ { NL ("long double"),	NL ("long double"),	D_PRINT_FLOAT },
   1890   /* f */ { NL ("float"),	NL ("float"),		D_PRINT_FLOAT },
   1891   /* g */ { NL ("__float128"),	NL ("__float128"),	D_PRINT_FLOAT },
   1892   /* h */ { NL ("unsigned char"), NL ("unsigned char"),	D_PRINT_DEFAULT },
   1893   /* i */ { NL ("int"),		NL ("int"),		D_PRINT_INT },
   1894   /* j */ { NL ("unsigned int"), NL ("unsigned"),	D_PRINT_UNSIGNED },
   1895   /* k */ { NULL, 0,		NULL, 0,		D_PRINT_DEFAULT },
   1896   /* l */ { NL ("long"),	NL ("long"),		D_PRINT_LONG },
   1897   /* m */ { NL ("unsigned long"), NL ("unsigned long"),	D_PRINT_UNSIGNED_LONG },
   1898   /* n */ { NL ("__int128"),	NL ("__int128"),	D_PRINT_DEFAULT },
   1899   /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
   1900 	    D_PRINT_DEFAULT },
   1901   /* p */ { NULL, 0,		NULL, 0,		D_PRINT_DEFAULT },
   1902   /* q */ { NULL, 0,		NULL, 0,		D_PRINT_DEFAULT },
   1903   /* r */ { NULL, 0,		NULL, 0,		D_PRINT_DEFAULT },
   1904   /* s */ { NL ("short"),	NL ("short"),		D_PRINT_DEFAULT },
   1905   /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
   1906   /* u */ { NULL, 0,		NULL, 0,		D_PRINT_DEFAULT },
   1907   /* v */ { NL ("void"),	NL ("void"),		D_PRINT_VOID },
   1908   /* w */ { NL ("wchar_t"),	NL ("char"),		D_PRINT_DEFAULT },
   1909   /* x */ { NL ("long long"),	NL ("long"),		D_PRINT_LONG_LONG },
   1910   /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
   1911 	    D_PRINT_UNSIGNED_LONG_LONG },
   1912   /* z */ { NL ("..."),		NL ("..."),		D_PRINT_DEFAULT },
   1913   /* 26 */ { NL ("decimal32"),	NL ("decimal32"),	D_PRINT_DEFAULT },
   1914   /* 27 */ { NL ("decimal64"),	NL ("decimal64"),	D_PRINT_DEFAULT },
   1915   /* 28 */ { NL ("decimal128"),	NL ("decimal128"),	D_PRINT_DEFAULT },
   1916   /* 29 */ { NL ("half"),	NL ("half"),		D_PRINT_FLOAT },
   1917   /* 30 */ { NL ("char16_t"),	NL ("char16_t"),	D_PRINT_DEFAULT },
   1918   /* 31 */ { NL ("char32_t"),	NL ("char32_t"),	D_PRINT_DEFAULT },
   1919 };
   1920 
   1921 CP_STATIC_IF_GLIBCPP_V3
   1922 struct demangle_component *
   1923 cplus_demangle_type (struct d_info *di)
   1924 {
   1925   char peek;
   1926   struct demangle_component *ret = NULL;
   1927   int can_subst;
   1928 
   1929   /* The ABI specifies that when CV-qualifiers are used, the base type
   1930      is substitutable, and the fully qualified type is substitutable,
   1931      but the base type with a strict subset of the CV-qualifiers is
   1932      not substitutable.  The natural recursive implementation of the
   1933      CV-qualifiers would cause subsets to be substitutable, so instead
   1934      we pull them all off now.
   1935 
   1936      FIXME: The ABI says that order-insensitive vendor qualifiers
   1937      should be handled in the same way, but we have no way to tell
   1938      which vendor qualifiers are order-insensitive and which are
   1939      order-sensitive.  So we just assume that they are all
   1940      order-sensitive.  g++ 3.4 supports only one vendor qualifier,
   1941      __vector, and it treats it as order-sensitive when mangling
   1942      names.  */
   1943 
   1944   peek = d_peek_char (di);
   1945   if (peek == 'r' || peek == 'V' || peek == 'K')
   1946     {
   1947       struct demangle_component **pret;
   1948 
   1949       pret = d_cv_qualifiers (di, &ret, 0);
   1950       if (pret == NULL)
   1951 	return NULL;
   1952       *pret = cplus_demangle_type (di);
   1953       if (! *pret || ! d_add_substitution (di, ret))
   1954 	return NULL;
   1955       return ret;
   1956     }
   1957 
   1958   can_subst = 1;
   1959 
   1960   switch (peek)
   1961     {
   1962     case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
   1963     case 'h': case 'i': case 'j':           case 'l': case 'm': case 'n':
   1964     case 'o':                               case 's': case 't':
   1965     case 'v': case 'w': case 'x': case 'y': case 'z':
   1966       ret = d_make_builtin_type (di,
   1967 				 &cplus_demangle_builtin_types[peek - 'a']);
   1968       di->expansion += ret->u.s_builtin.type->len;
   1969       can_subst = 0;
   1970       d_advance (di, 1);
   1971       break;
   1972 
   1973     case 'u':
   1974       d_advance (di, 1);
   1975       ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
   1976 			 d_source_name (di), NULL);
   1977       break;
   1978 
   1979     case 'F':
   1980       ret = d_function_type (di);
   1981       break;
   1982 
   1983     case '0': case '1': case '2': case '3': case '4':
   1984     case '5': case '6': case '7': case '8': case '9':
   1985     case 'N':
   1986     case 'Z':
   1987       ret = d_class_enum_type (di);
   1988       break;
   1989 
   1990     case 'A':
   1991       ret = d_array_type (di);
   1992       break;
   1993 
   1994     case 'M':
   1995       ret = d_pointer_to_member_type (di);
   1996       break;
   1997 
   1998     case 'T':
   1999       ret = d_template_param (di);
   2000       if (d_peek_char (di) == 'I')
   2001 	{
   2002 	  /* This is <template-template-param> <template-args>.  The
   2003 	     <template-template-param> part is a substitution
   2004 	     candidate.  */
   2005 	  if (! d_add_substitution (di, ret))
   2006 	    return NULL;
   2007 	  ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
   2008 			     d_template_args (di));
   2009 	}
   2010       break;
   2011 
   2012     case 'S':
   2013       /* If this is a special substitution, then it is the start of
   2014 	 <class-enum-type>.  */
   2015       {
   2016 	char peek_next;
   2017 
   2018 	peek_next = d_peek_next_char (di);
   2019 	if (IS_DIGIT (peek_next)
   2020 	    || peek_next == '_'
   2021 	    || IS_UPPER (peek_next))
   2022 	  {
   2023 	    ret = d_substitution (di, 0);
   2024 	    /* The substituted name may have been a template name and
   2025 	       may be followed by tepmlate args.  */
   2026 	    if (d_peek_char (di) == 'I')
   2027 	      ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
   2028 				 d_template_args (di));
   2029 	    else
   2030 	      can_subst = 0;
   2031 	  }
   2032 	else
   2033 	  {
   2034 	    ret = d_class_enum_type (di);
   2035 	    /* If the substitution was a complete type, then it is not
   2036 	       a new substitution candidate.  However, if the
   2037 	       substitution was followed by template arguments, then
   2038 	       the whole thing is a substitution candidate.  */
   2039 	    if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
   2040 	      can_subst = 0;
   2041 	  }
   2042       }
   2043       break;
   2044 
   2045     case 'O':
   2046       d_advance (di, 1);
   2047       ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE,
   2048                          cplus_demangle_type (di), NULL);
   2049       break;
   2050 
   2051     case 'P':
   2052       d_advance (di, 1);
   2053       ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
   2054 			 cplus_demangle_type (di), NULL);
   2055       break;
   2056 
   2057     case 'R':
   2058       d_advance (di, 1);
   2059       ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
   2060                          cplus_demangle_type (di), NULL);
   2061       break;
   2062 
   2063     case 'C':
   2064       d_advance (di, 1);
   2065       ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
   2066 			 cplus_demangle_type (di), NULL);
   2067       break;
   2068 
   2069     case 'G':
   2070       d_advance (di, 1);
   2071       ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
   2072 			 cplus_demangle_type (di), NULL);
   2073       break;
   2074 
   2075     case 'U':
   2076       d_advance (di, 1);
   2077       ret = d_source_name (di);
   2078       ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
   2079 			 cplus_demangle_type (di), ret);
   2080       break;
   2081 
   2082     case 'D':
   2083       can_subst = 0;
   2084       d_advance (di, 1);
   2085       peek = d_next_char (di);
   2086       switch (peek)
   2087 	{
   2088 	case 'T':
   2089 	case 't':
   2090 	  /* decltype (expression) */
   2091 	  ret = d_make_comp (di, DEMANGLE_COMPONENT_DECLTYPE,
   2092 			     d_expression (di), NULL);
   2093 	  if (ret && d_next_char (di) != 'E')
   2094 	    ret = NULL;
   2095 	  break;
   2096 
   2097 	case 'p':
   2098 	  /* Pack expansion.  */
   2099 	  ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
   2100 			     cplus_demangle_type (di), NULL);
   2101 	  break;
   2102 
   2103 	case 'f':
   2104 	  /* 32-bit decimal floating point */
   2105 	  ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[26]);
   2106 	  di->expansion += ret->u.s_builtin.type->len;
   2107 	  break;
   2108 	case 'd':
   2109 	  /* 64-bit DFP */
   2110 	  ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[27]);
   2111 	  di->expansion += ret->u.s_builtin.type->len;
   2112 	  break;
   2113 	case 'e':
   2114 	  /* 128-bit DFP */
   2115 	  ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[28]);
   2116 	  di->expansion += ret->u.s_builtin.type->len;
   2117 	  break;
   2118 	case 'h':
   2119 	  /* 16-bit half-precision FP */
   2120 	  ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[29]);
   2121 	  di->expansion += ret->u.s_builtin.type->len;
   2122 	  break;
   2123 	case 's':
   2124 	  /* char16_t */
   2125 	  ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[30]);
   2126 	  di->expansion += ret->u.s_builtin.type->len;
   2127 	  break;
   2128 	case 'i':
   2129 	  /* char32_t */
   2130 	  ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[31]);
   2131 	  di->expansion += ret->u.s_builtin.type->len;
   2132 	  break;
   2133 	}
   2134       break;
   2135 
   2136     default:
   2137       return NULL;
   2138     }
   2139 
   2140   if (can_subst)
   2141     {
   2142       if (! d_add_substitution (di, ret))
   2143 	return NULL;
   2144     }
   2145 
   2146   return ret;
   2147 }
   2148 
   2149 /* <CV-qualifiers> ::= [r] [V] [K]  */
   2150 
   2151 static struct demangle_component **
   2152 d_cv_qualifiers (struct d_info *di,
   2153                  struct demangle_component **pret, int member_fn)
   2154 {
   2155   char peek;
   2156 
   2157   peek = d_peek_char (di);
   2158   while (peek == 'r' || peek == 'V' || peek == 'K')
   2159     {
   2160       enum demangle_component_type t;
   2161 
   2162       d_advance (di, 1);
   2163       if (peek == 'r')
   2164 	{
   2165 	  t = (member_fn
   2166 	       ? DEMANGLE_COMPONENT_RESTRICT_THIS
   2167 	       : DEMANGLE_COMPONENT_RESTRICT);
   2168 	  di->expansion += sizeof "restrict";
   2169 	}
   2170       else if (peek == 'V')
   2171 	{
   2172 	  t = (member_fn
   2173 	       ? DEMANGLE_COMPONENT_VOLATILE_THIS
   2174 	       : DEMANGLE_COMPONENT_VOLATILE);
   2175 	  di->expansion += sizeof "volatile";
   2176 	}
   2177       else
   2178 	{
   2179 	  t = (member_fn
   2180 	       ? DEMANGLE_COMPONENT_CONST_THIS
   2181 	       : DEMANGLE_COMPONENT_CONST);
   2182 	  di->expansion += sizeof "const";
   2183 	}
   2184 
   2185       *pret = d_make_comp (di, t, NULL, NULL);
   2186       if (*pret == NULL)
   2187 	return NULL;
   2188       pret = &d_left (*pret);
   2189 
   2190       peek = d_peek_char (di);
   2191     }
   2192 
   2193   return pret;
   2194 }
   2195 
   2196 /* <function-type> ::= F [Y] <bare-function-type> E  */
   2197 
   2198 static struct demangle_component *
   2199 d_function_type (struct d_info *di)
   2200 {
   2201   struct demangle_component *ret;
   2202 
   2203   if (! d_check_char (di, 'F'))
   2204     return NULL;
   2205   if (d_peek_char (di) == 'Y')
   2206     {
   2207       /* Function has C linkage.  We don't print this information.
   2208 	 FIXME: We should print it in verbose mode.  */
   2209       d_advance (di, 1);
   2210     }
   2211   ret = d_bare_function_type (di, 1);
   2212   if (! d_check_char (di, 'E'))
   2213     return NULL;
   2214   return ret;
   2215 }
   2216 
   2217 /* <bare-function-type> ::= [J]<type>+  */
   2218 
   2219 static struct demangle_component *
   2220 d_bare_function_type (struct d_info *di, int has_return_tipe)
   2221 {
   2222   struct demangle_component *return_type;
   2223   struct demangle_component *tl;
   2224   struct demangle_component **ptl;
   2225   char peek;
   2226 
   2227   /* Detect special qualifier indicating that the first argument
   2228      is the return type.  */
   2229   peek = d_peek_char (di);
   2230   if (peek == 'J')
   2231     {
   2232       d_advance (di, 1);
   2233       has_return_tipe = 1;
   2234     }
   2235 
   2236   return_type = NULL;
   2237   tl = NULL;
   2238   ptl = &tl;
   2239   while (1)
   2240     {
   2241       struct demangle_component *type;
   2242 
   2243       peek = d_peek_char (di);
   2244       if (peek == '\0' || peek == 'E')
   2245 	break;
   2246       type = cplus_demangle_type (di);
   2247       if (type == NULL)
   2248 	return NULL;
   2249       if (has_return_tipe)
   2250 	{
   2251 	  return_type = type;
   2252 	  has_return_tipe = 0;
   2253 	}
   2254       else
   2255 	{
   2256 	  *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
   2257 	  if (*ptl == NULL)
   2258 	    return NULL;
   2259 	  ptl = &d_right (*ptl);
   2260 	}
   2261     }
   2262 
   2263   /* There should be at least one parameter type besides the optional
   2264      return type.  A function which takes no arguments will have a
   2265      single parameter type void.  */
   2266   if (tl == NULL)
   2267     return NULL;
   2268 
   2269   /* If we have a single parameter type void, omit it.  */
   2270   if (d_right (tl) == NULL
   2271       && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
   2272       && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
   2273     {
   2274       di->expansion -= d_left (tl)->u.s_builtin.type->len;
   2275       tl = NULL;
   2276     }
   2277 
   2278   return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE, return_type, tl);
   2279 }
   2280 
   2281 /* <class-enum-type> ::= <name>  */
   2282 
   2283 static struct demangle_component *
   2284 d_class_enum_type (struct d_info *di)
   2285 {
   2286   return d_name (di);
   2287 }
   2288 
   2289 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
   2290                 ::= A [<(dimension) expression>] _ <(element) type>
   2291 */
   2292 
   2293 static struct demangle_component *
   2294 d_array_type (struct d_info *di)
   2295 {
   2296   char peek;
   2297   struct demangle_component *dim;
   2298 
   2299   if (! d_check_char (di, 'A'))
   2300     return NULL;
   2301 
   2302   peek = d_peek_char (di);
   2303   if (peek == '_')
   2304     dim = NULL;
   2305   else if (IS_DIGIT (peek))
   2306     {
   2307       const char *s;
   2308 
   2309       s = d_str (di);
   2310       do
   2311 	{
   2312 	  d_advance (di, 1);
   2313 	  peek = d_peek_char (di);
   2314 	}
   2315       while (IS_DIGIT (peek));
   2316       dim = d_make_name (di, s, d_str (di) - s);
   2317       if (dim == NULL)
   2318 	return NULL;
   2319     }
   2320   else
   2321     {
   2322       dim = d_expression (di);
   2323       if (dim == NULL)
   2324 	return NULL;
   2325     }
   2326 
   2327   if (! d_check_char (di, '_'))
   2328     return NULL;
   2329 
   2330   return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
   2331 		      cplus_demangle_type (di));
   2332 }
   2333 
   2334 /* <pointer-to-member-type> ::= M <(class) type> <(member) type>  */
   2335 
   2336 static struct demangle_component *
   2337 d_pointer_to_member_type (struct d_info *di)
   2338 {
   2339   struct demangle_component *cl;
   2340   struct demangle_component *mem;
   2341   struct demangle_component **pmem;
   2342 
   2343   if (! d_check_char (di, 'M'))
   2344     return NULL;
   2345 
   2346   cl = cplus_demangle_type (di);
   2347 
   2348   /* The ABI specifies that any type can be a substitution source, and
   2349      that M is followed by two types, and that when a CV-qualified
   2350      type is seen both the base type and the CV-qualified types are
   2351      substitution sources.  The ABI also specifies that for a pointer
   2352      to a CV-qualified member function, the qualifiers are attached to
   2353      the second type.  Given the grammar, a plain reading of the ABI
   2354      suggests that both the CV-qualified member function and the
   2355      non-qualified member function are substitution sources.  However,
   2356      g++ does not work that way.  g++ treats only the CV-qualified
   2357      member function as a substitution source.  FIXME.  So to work
   2358      with g++, we need to pull off the CV-qualifiers here, in order to
   2359      avoid calling add_substitution() in cplus_demangle_type().  But
   2360      for a CV-qualified member which is not a function, g++ does
   2361      follow the ABI, so we need to handle that case here by calling
   2362      d_add_substitution ourselves.  */
   2363 
   2364   pmem = d_cv_qualifiers (di, &mem, 1);
   2365   if (pmem == NULL)
   2366     return NULL;
   2367   *pmem = cplus_demangle_type (di);
   2368   if (*pmem == NULL)
   2369     return NULL;
   2370 
   2371   if (pmem != &mem && (*pmem)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
   2372     {
   2373       if (! d_add_substitution (di, mem))
   2374 	return NULL;
   2375     }
   2376 
   2377   return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
   2378 }
   2379 
   2380 /* <template-param> ::= T_
   2381                     ::= T <(parameter-2 non-negative) number> _
   2382 */
   2383 
   2384 static struct demangle_component *
   2385 d_template_param (struct d_info *di)
   2386 {
   2387   long param;
   2388 
   2389   if (! d_check_char (di, 'T'))
   2390     return NULL;
   2391 
   2392   if (d_peek_char (di) == '_')
   2393     param = 0;
   2394   else
   2395     {
   2396       param = d_number (di);
   2397       if (param < 0)
   2398 	return NULL;
   2399       param += 1;
   2400     }
   2401 
   2402   if (! d_check_char (di, '_'))
   2403     return NULL;
   2404 
   2405   ++di->did_subs;
   2406 
   2407   return d_make_template_param (di, param);
   2408 }
   2409 
   2410 /* <template-args> ::= I <template-arg>+ E  */
   2411 
   2412 static struct demangle_component *
   2413 d_template_args (struct d_info *di)
   2414 {
   2415   struct demangle_component *hold_last_name;
   2416   struct demangle_component *al;
   2417   struct demangle_component **pal;
   2418 
   2419   /* Preserve the last name we saw--don't let the template arguments
   2420      clobber it, as that would give us the wrong name for a subsequent
   2421      constructor or destructor.  */
   2422   hold_last_name = di->last_name;
   2423 
   2424   if (! d_check_char (di, 'I'))
   2425     return NULL;
   2426 
   2427   if (d_peek_char (di) == 'E')
   2428     {
   2429       /* An argument pack can be empty.  */
   2430       d_advance (di, 1);
   2431       return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, NULL, NULL);
   2432     }
   2433 
   2434   al = NULL;
   2435   pal = &al;
   2436   while (1)
   2437     {
   2438       struct demangle_component *a;
   2439 
   2440       a = d_template_arg (di);
   2441       if (a == NULL)
   2442 	return NULL;
   2443 
   2444       *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
   2445       if (*pal == NULL)
   2446 	return NULL;
   2447       pal = &d_right (*pal);
   2448 
   2449       if (d_peek_char (di) == 'E')
   2450 	{
   2451 	  d_advance (di, 1);
   2452 	  break;
   2453 	}
   2454     }
   2455 
   2456   di->last_name = hold_last_name;
   2457 
   2458   return al;
   2459 }
   2460 
   2461 /* <template-arg> ::= <type>
   2462                   ::= X <expression> E
   2463                   ::= <expr-primary>
   2464 */
   2465 
   2466 static struct demangle_component *
   2467 d_template_arg (struct d_info *di)
   2468 {
   2469   struct demangle_component *ret;
   2470 
   2471   switch (d_peek_char (di))
   2472     {
   2473     case 'X':
   2474       d_advance (di, 1);
   2475       ret = d_expression (di);
   2476       if (! d_check_char (di, 'E'))
   2477 	return NULL;
   2478       return ret;
   2479 
   2480     case 'L':
   2481       return d_expr_primary (di);
   2482 
   2483     case 'I':
   2484       /* An argument pack.  */
   2485       return d_template_args (di);
   2486 
   2487     default:
   2488       return cplus_demangle_type (di);
   2489     }
   2490 }
   2491 
   2492 /* Subroutine of <expression> ::= cl <expression>+ E */
   2493 
   2494 static struct demangle_component *
   2495 d_exprlist (struct d_info *di)
   2496 {
   2497   struct demangle_component *list = NULL;
   2498   struct demangle_component **p = &list;
   2499 
   2500   if (d_peek_char (di) == 'E')
   2501     {
   2502       d_advance (di, 1);
   2503       return d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, NULL, NULL);
   2504     }
   2505 
   2506   while (1)
   2507     {
   2508       struct demangle_component *arg = d_expression (di);
   2509       if (arg == NULL)
   2510 	return NULL;
   2511 
   2512       *p = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, arg, NULL);
   2513       if (*p == NULL)
   2514 	return NULL;
   2515       p = &d_right (*p);
   2516 
   2517       if (d_peek_char (di) == 'E')
   2518 	{
   2519 	  d_advance (di, 1);
   2520 	  break;
   2521 	}
   2522     }
   2523 
   2524   return list;
   2525 }
   2526 
   2527 /* <expression> ::= <(unary) operator-name> <expression>
   2528                 ::= <(binary) operator-name> <expression> <expression>
   2529                 ::= <(trinary) operator-name> <expression> <expression> <expression>
   2530 		::= cl <expression>+ E
   2531                 ::= st <type>
   2532                 ::= <template-param>
   2533                 ::= sr <type> <unqualified-name>
   2534                 ::= sr <type> <unqualified-name> <template-args>
   2535                 ::= <expr-primary>
   2536 */
   2537 
   2538 static struct demangle_component *
   2539 d_expression (struct d_info *di)
   2540 {
   2541   char peek;
   2542 
   2543   peek = d_peek_char (di);
   2544   if (peek == 'L')
   2545     return d_expr_primary (di);
   2546   else if (peek == 'T')
   2547     return d_template_param (di);
   2548   else if (peek == 's' && d_peek_next_char (di) == 'r')
   2549     {
   2550       struct demangle_component *type;
   2551       struct demangle_component *name;
   2552 
   2553       d_advance (di, 2);
   2554       type = cplus_demangle_type (di);
   2555       name = d_unqualified_name (di);
   2556       if (d_peek_char (di) != 'I')
   2557 	return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
   2558       else
   2559 	return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
   2560 			    d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
   2561 					 d_template_args (di)));
   2562     }
   2563   else if (peek == 's' && d_peek_next_char (di) == 'T')
   2564     {
   2565       /* Just demangle a parameter placeholder as its type.  */
   2566       d_advance (di, 2);
   2567       return cplus_demangle_type (di);
   2568     }
   2569   else if (IS_DIGIT (peek))
   2570     {
   2571       /* We can get an unqualified name as an expression in the case of
   2572          a dependent member access, i.e. decltype(T().i).  */
   2573       struct demangle_component *name = d_unqualified_name (di);
   2574       if (name == NULL)
   2575 	return NULL;
   2576       if (d_peek_char (di) == 'I')
   2577 	return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
   2578 			    d_template_args (di));
   2579       else
   2580 	return name;
   2581     }
   2582   else
   2583     {
   2584       struct demangle_component *op;
   2585       int args;
   2586 
   2587       op = d_operator_name (di);
   2588       if (op == NULL)
   2589 	return NULL;
   2590 
   2591       if (op->type == DEMANGLE_COMPONENT_OPERATOR)
   2592 	di->expansion += op->u.s_operator.op->len - 2;
   2593 
   2594       if (op->type == DEMANGLE_COMPONENT_OPERATOR
   2595 	  && strcmp (op->u.s_operator.op->code, "st") == 0)
   2596 	return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
   2597 			    cplus_demangle_type (di));
   2598 
   2599       switch (op->type)
   2600 	{
   2601 	default:
   2602 	  return NULL;
   2603 	case DEMANGLE_COMPONENT_OPERATOR:
   2604 	  args = op->u.s_operator.op->args;
   2605 	  break;
   2606 	case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
   2607 	  args = op->u.s_extended_operator.args;
   2608 	  break;
   2609 	case DEMANGLE_COMPONENT_CAST:
   2610 	  if (d_peek_char (di) == 'v')
   2611 	    /* T() encoded as an operand of void.  */
   2612 	    return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
   2613 				cplus_demangle_type (di));
   2614 	  else
   2615 	    args = 1;
   2616 	  break;
   2617 	}
   2618 
   2619       switch (args)
   2620 	{
   2621 	case 1:
   2622 	  return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
   2623 			      d_expression (di));
   2624 	case 2:
   2625 	  {
   2626 	    struct demangle_component *left;
   2627 	    struct demangle_component *right;
   2628 
   2629 	    left = d_expression (di);
   2630 	    if (!strcmp (op->u.s_operator.op->code, "cl"))
   2631 	      right = d_exprlist (di);
   2632 	    else
   2633 	      right = d_expression (di);
   2634 
   2635 	    return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
   2636 				d_make_comp (di,
   2637 					     DEMANGLE_COMPONENT_BINARY_ARGS,
   2638 					     left, right));
   2639 	  }
   2640 	case 3:
   2641 	  {
   2642 	    struct demangle_component *first;
   2643 	    struct demangle_component *second;
   2644 
   2645 	    first = d_expression (di);
   2646 	    second = d_expression (di);
   2647 	    return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
   2648 				d_make_comp (di,
   2649 					     DEMANGLE_COMPONENT_TRINARY_ARG1,
   2650 					     first,
   2651 					     d_make_comp (di,
   2652 							  DEMANGLE_COMPONENT_TRINARY_ARG2,
   2653 							  second,
   2654 							  d_expression (di))));
   2655 	  }
   2656 	default:
   2657 	  return NULL;
   2658 	}
   2659     }
   2660 }
   2661 
   2662 /* <expr-primary> ::= L <type> <(value) number> E
   2663                   ::= L <type> <(value) float> E
   2664                   ::= L <mangled-name> E
   2665 */
   2666 
   2667 static struct demangle_component *
   2668 d_expr_primary (struct d_info *di)
   2669 {
   2670   struct demangle_component *ret;
   2671 
   2672   if (! d_check_char (di, 'L'))
   2673     return NULL;
   2674   if (d_peek_char (di) == '_')
   2675     ret = cplus_demangle_mangled_name (di, 0);
   2676   else
   2677     {
   2678       struct demangle_component *type;
   2679       enum demangle_component_type t;
   2680       const char *s;
   2681 
   2682       type = cplus_demangle_type (di);
   2683       if (type == NULL)
   2684 	return NULL;
   2685 
   2686       /* If we have a type we know how to print, we aren't going to
   2687 	 print the type name itself.  */
   2688       if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
   2689 	  && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
   2690 	di->expansion -= type->u.s_builtin.type->len;
   2691 
   2692       /* Rather than try to interpret the literal value, we just
   2693 	 collect it as a string.  Note that it's possible to have a
   2694 	 floating point literal here.  The ABI specifies that the
   2695 	 format of such literals is machine independent.  That's fine,
   2696 	 but what's not fine is that versions of g++ up to 3.2 with
   2697 	 -fabi-version=1 used upper case letters in the hex constant,
   2698 	 and dumped out gcc's internal representation.  That makes it
   2699 	 hard to tell where the constant ends, and hard to dump the
   2700 	 constant in any readable form anyhow.  We don't attempt to
   2701 	 handle these cases.  */
   2702 
   2703       t = DEMANGLE_COMPONENT_LITERAL;
   2704       if (d_peek_char (di) == 'n')
   2705 	{
   2706 	  t = DEMANGLE_COMPONENT_LITERAL_NEG;
   2707 	  d_advance (di, 1);
   2708 	}
   2709       s = d_str (di);
   2710       while (d_peek_char (di) != 'E')
   2711 	{
   2712 	  if (d_peek_char (di) == '\0')
   2713 	    return NULL;
   2714 	  d_advance (di, 1);
   2715 	}
   2716       ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
   2717     }
   2718   if (! d_check_char (di, 'E'))
   2719     return NULL;
   2720   return ret;
   2721 }
   2722 
   2723 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
   2724                 ::= Z <(function) encoding> E s [<discriminator>]
   2725 */
   2726 
   2727 static struct demangle_component *
   2728 d_local_name (struct d_info *di)
   2729 {
   2730   struct demangle_component *function;
   2731 
   2732   if (! d_check_char (di, 'Z'))
   2733     return NULL;
   2734 
   2735   function = d_encoding (di, 0);
   2736 
   2737   if (! d_check_char (di, 'E'))
   2738     return NULL;
   2739 
   2740   if (d_peek_char (di) == 's')
   2741     {
   2742       d_advance (di, 1);
   2743       if (! d_discriminator (di))
   2744 	return NULL;
   2745       return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
   2746 			  d_make_name (di, "string literal",
   2747 				       sizeof "string literal" - 1));
   2748     }
   2749   else
   2750     {
   2751       struct demangle_component *name;
   2752 
   2753       name = d_name (di);
   2754       if (! d_discriminator (di))
   2755 	return NULL;
   2756       return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
   2757     }
   2758 }
   2759 
   2760 /* <discriminator> ::= _ <(non-negative) number>
   2761 
   2762    We demangle the discriminator, but we don't print it out.  FIXME:
   2763    We should print it out in verbose mode.  */
   2764 
   2765 static int
   2766 d_discriminator (struct d_info *di)
   2767 {
   2768   long discrim;
   2769 
   2770   if (d_peek_char (di) != '_')
   2771     return 1;
   2772   d_advance (di, 1);
   2773   discrim = d_number (di);
   2774   if (discrim < 0)
   2775     return 0;
   2776   return 1;
   2777 }
   2778 
   2779 /* Add a new substitution.  */
   2780 
   2781 static int
   2782 d_add_substitution (struct d_info *di, struct demangle_component *dc)
   2783 {
   2784   if (dc == NULL)
   2785     return 0;
   2786   if (di->next_sub >= di->num_subs)
   2787     return 0;
   2788   di->subs[di->next_sub] = dc;
   2789   ++di->next_sub;
   2790   return 1;
   2791 }
   2792 
   2793 /* <substitution> ::= S <seq-id> _
   2794                   ::= S_
   2795                   ::= St
   2796                   ::= Sa
   2797                   ::= Sb
   2798                   ::= Ss
   2799                   ::= Si
   2800                   ::= So
   2801                   ::= Sd
   2802 
   2803    If PREFIX is non-zero, then this type is being used as a prefix in
   2804    a qualified name.  In this case, for the standard substitutions, we
   2805    need to check whether we are being used as a prefix for a
   2806    constructor or destructor, and return a full template name.
   2807    Otherwise we will get something like std::iostream::~iostream()
   2808    which does not correspond particularly well to any function which
   2809    actually appears in the source.
   2810 */
   2811 
   2812 static const struct d_standard_sub_info standard_subs[] =
   2813 {
   2814   { 't', NL ("std"),
   2815     NL ("std"),
   2816     NULL, 0 },
   2817   { 'a', NL ("std::allocator"),
   2818     NL ("std::allocator"),
   2819     NL ("allocator") },
   2820   { 'b', NL ("std::basic_string"),
   2821     NL ("std::basic_string"),
   2822     NL ("basic_string") },
   2823   { 's', NL ("std::string"),
   2824     NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
   2825     NL ("basic_string") },
   2826   { 'i', NL ("std::istream"),
   2827     NL ("std::basic_istream<char, std::char_traits<char> >"),
   2828     NL ("basic_istream") },
   2829   { 'o', NL ("std::ostream"),
   2830     NL ("std::basic_ostream<char, std::char_traits<char> >"),
   2831     NL ("basic_ostream") },
   2832   { 'd', NL ("std::iostream"),
   2833     NL ("std::basic_iostream<char, std::char_traits<char> >"),
   2834     NL ("basic_iostream") }
   2835 };
   2836 
   2837 static struct demangle_component *
   2838 d_substitution (struct d_info *di, int prefix)
   2839 {
   2840   char c;
   2841 
   2842   if (! d_check_char (di, 'S'))
   2843     return NULL;
   2844 
   2845   c = d_next_char (di);
   2846   if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
   2847     {
   2848       unsigned int id;
   2849 
   2850       id = 0;
   2851       if (c != '_')
   2852 	{
   2853 	  do
   2854 	    {
   2855 	      unsigned int new_id;
   2856 
   2857 	      if (IS_DIGIT (c))
   2858 		new_id = id * 36 + c - '0';
   2859 	      else if (IS_UPPER (c))
   2860 		new_id = id * 36 + c - 'A' + 10;
   2861 	      else
   2862 		return NULL;
   2863 	      if (new_id < id)
   2864 		return NULL;
   2865 	      id = new_id;
   2866 	      c = d_next_char (di);
   2867 	    }
   2868 	  while (c != '_');
   2869 
   2870 	  ++id;
   2871 	}
   2872 
   2873       if (id >= (unsigned int) di->next_sub)
   2874 	return NULL;
   2875 
   2876       ++di->did_subs;
   2877 
   2878       return di->subs[id];
   2879     }
   2880   else
   2881     {
   2882       int verbose;
   2883       const struct d_standard_sub_info *p;
   2884       const struct d_standard_sub_info *pend;
   2885 
   2886       verbose = (di->options & DMGL_VERBOSE) != 0;
   2887       if (! verbose && prefix)
   2888 	{
   2889 	  char peek;
   2890 
   2891 	  peek = d_peek_char (di);
   2892 	  if (peek == 'C' || peek == 'D')
   2893 	    verbose = 1;
   2894 	}
   2895 
   2896       pend = (&standard_subs[0]
   2897 	      + sizeof standard_subs / sizeof standard_subs[0]);
   2898       for (p = &standard_subs[0]; p < pend; ++p)
   2899 	{
   2900 	  if (c == p->code)
   2901 	    {
   2902 	      const char *s;
   2903 	      int len;
   2904 
   2905 	      if (p->set_last_name != NULL)
   2906 		di->last_name = d_make_sub (di, p->set_last_name,
   2907 					    p->set_last_name_len);
   2908 	      if (verbose)
   2909 		{
   2910 		  s = p->full_expansion;
   2911 		  len = p->full_len;
   2912 		}
   2913 	      else
   2914 		{
   2915 		  s = p->simple_expansion;
   2916 		  len = p->simple_len;
   2917 		}
   2918 	      di->expansion += len;
   2919 	      return d_make_sub (di, s, len);
   2920 	    }
   2921 	}
   2922 
   2923       return NULL;
   2924     }
   2925 }
   2926 
   2927 /* Initialize a growable string.  */
   2928 
   2929 static void
   2930 d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
   2931 {
   2932   dgs->buf = NULL;
   2933   dgs->len = 0;
   2934   dgs->alc = 0;
   2935   dgs->allocation_failure = 0;
   2936 
   2937   if (estimate > 0)
   2938     d_growable_string_resize (dgs, estimate);
   2939 }
   2940 
   2941 /* Grow a growable string to a given size.  */
   2942 
   2943 static inline void
   2944 d_growable_string_resize (struct d_growable_string *dgs, size_t need)
   2945 {
   2946   size_t newalc;
   2947   char *newbuf;
   2948 
   2949   if (dgs->allocation_failure)
   2950     return;
   2951 
   2952   /* Start allocation at two bytes to avoid any possibility of confusion
   2953      with the special value of 1 used as a return in *palc to indicate
   2954      allocation failures.  */
   2955   newalc = dgs->alc > 0 ? dgs->alc : 2;
   2956   while (newalc < need)
   2957     newalc <<= 1;
   2958 
   2959   newbuf = (char *) realloc ("demangle.dgsr.1", dgs->buf, newalc);
   2960   if (newbuf == NULL)
   2961     {
   2962       free (dgs->buf);
   2963       dgs->buf = NULL;
   2964       dgs->len = 0;
   2965       dgs->alc = 0;
   2966       dgs->allocation_failure = 1;
   2967       return;
   2968     }
   2969   dgs->buf = newbuf;
   2970   dgs->alc = newalc;
   2971 }
   2972 
   2973 /* Append a buffer to a growable string.  */
   2974 
   2975 static inline void
   2976 d_growable_string_append_buffer (struct d_growable_string *dgs,
   2977                                  const char *s, size_t l)
   2978 {
   2979   size_t need;
   2980 
   2981   need = dgs->len + l + 1;
   2982   if (need > dgs->alc)
   2983     d_growable_string_resize (dgs, need);
   2984 
   2985   if (dgs->allocation_failure)
   2986     return;
   2987 
   2988   memcpy (dgs->buf + dgs->len, s, l);
   2989   dgs->buf[dgs->len + l] = '\0';
   2990   dgs->len += l;
   2991 }
   2992 
   2993 /* Bridge growable strings to the callback mechanism.  */
   2994 
   2995 static void
   2996 d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
   2997 {
   2998   struct d_growable_string *dgs = (struct d_growable_string*) opaque;
   2999 
   3000   d_growable_string_append_buffer (dgs, s, l);
   3001 }
   3002 
   3003 /* Initialize a print information structure.  */
   3004 
   3005 static void
   3006 d_print_init (struct d_print_info *dpi, int options,
   3007               demangle_callbackref callback, void *opaque)
   3008 {
   3009   dpi->options = options;
   3010   dpi->len = 0;
   3011   dpi->last_char = '\0';
   3012   dpi->templates = NULL;
   3013   dpi->modifiers = NULL;
   3014 
   3015   dpi->callback = callback;
   3016   dpi->opaque = opaque;
   3017 
   3018   dpi->demangle_failure = 0;
   3019 }
   3020 
   3021 /* Indicate that an error occurred during printing, and test for error.  */
   3022 
   3023 static inline void
   3024 d_print_error (struct d_print_info *dpi)
   3025 {
   3026   dpi->demangle_failure = 1;
   3027 }
   3028 
   3029 static inline int
   3030 d_print_saw_error (struct d_print_info *dpi)
   3031 {
   3032   return dpi->demangle_failure != 0;
   3033 }
   3034 
   3035 /* Flush buffered characters to the callback.  */
   3036 
   3037 static inline void
   3038 d_print_flush (struct d_print_info *dpi)
   3039 {
   3040   dpi->buf[dpi->len] = '\0';
   3041   dpi->callback (dpi->buf, dpi->len, dpi->opaque);
   3042   dpi->len = 0;
   3043 }
   3044 
   3045 /* Append characters and buffers for printing.  */
   3046 
   3047 static inline void
   3048 d_append_char (struct d_print_info *dpi, char c)
   3049 {
   3050   if (dpi->len == sizeof (dpi->buf) - 1)
   3051     d_print_flush (dpi);
   3052 
   3053   dpi->buf[dpi->len++] = c;
   3054   dpi->last_char = c;
   3055 }
   3056 
   3057 static inline void
   3058 d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
   3059 {
   3060   size_t i;
   3061 
   3062   for (i = 0; i < l; i++)
   3063     d_append_char (dpi, s[i]);
   3064 }
   3065 
   3066 static inline void
   3067 d_append_string (struct d_print_info *dpi, const char *s)
   3068 {
   3069   d_append_buffer (dpi, s, strlen (s));
   3070 }
   3071 
   3072 static inline char
   3073 d_last_char (struct d_print_info *dpi)
   3074 {
   3075   return dpi->last_char;
   3076 }
   3077 
   3078 /* Turn components into a human readable string.  OPTIONS is the
   3079    options bits passed to the demangler.  DC is the tree to print.
   3080    CALLBACK is a function to call to flush demangled string segments
   3081    as they fill the intermediate buffer, and OPAQUE is a generalized
   3082    callback argument.  On success, this returns 1.  On failure,
   3083    it returns 0, indicating a bad parse.  It does not use heap
   3084    memory to build an output string, so cannot encounter memory
   3085    allocation failure.  */
   3086 
   3087 CP_STATIC_IF_GLIBCPP_V3
   3088 int
   3089 cplus_demangle_print_callback (int options,
   3090                                const struct demangle_component *dc,
   3091                                demangle_callbackref callback, void *opaque)
   3092 {
   3093   struct d_print_info dpi;
   3094 
   3095   d_print_init (&dpi, options, callback, opaque);
   3096 
   3097   d_print_comp (&dpi, dc);
   3098 
   3099   d_print_flush (&dpi);
   3100 
   3101   return ! d_print_saw_error (&dpi);
   3102 }
   3103 
   3104 /* Turn components into a human readable string.  OPTIONS is the
   3105    options bits passed to the demangler.  DC is the tree to print.
   3106    ESTIMATE is a guess at the length of the result.  This returns a
   3107    string allocated by malloc, or NULL on error.  On success, this
   3108    sets *PALC to the size of the allocated buffer.  On failure, this
   3109    sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
   3110    failure.  */
   3111 
   3112 CP_STATIC_IF_GLIBCPP_V3
   3113 char *
   3114 cplus_demangle_print (int options, const struct demangle_component *dc,
   3115                       int estimate, size_t *palc)
   3116 {
   3117   struct d_growable_string dgs;
   3118 
   3119   d_growable_string_init (&dgs, estimate);
   3120 
   3121   if (! cplus_demangle_print_callback (options, dc,
   3122                                        d_growable_string_callback_adapter,
   3123                                        &dgs))
   3124     {
   3125       free (dgs.buf);
   3126       *palc = 0;
   3127       return NULL;
   3128     }
   3129 
   3130   *palc = dgs.allocation_failure ? 1 : dgs.alc;
   3131   return dgs.buf;
   3132 }
   3133 
   3134 /* Returns the I'th element of the template arglist ARGS, or NULL on
   3135    failure.  */
   3136 
   3137 static struct demangle_component *
   3138 d_index_template_argument (struct demangle_component *args, int i)
   3139 {
   3140   struct demangle_component *a;
   3141 
   3142   for (a = args;
   3143        a != NULL;
   3144        a = d_right (a))
   3145     {
   3146       if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
   3147 	return NULL;
   3148       if (i <= 0)
   3149 	break;
   3150       --i;
   3151     }
   3152   if (i != 0 || a == NULL)
   3153     return NULL;
   3154 
   3155   return d_left (a);
   3156 }
   3157 
   3158 /* Returns the template argument from the current context indicated by DC,
   3159    which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL.  */
   3160 
   3161 static struct demangle_component *
   3162 d_lookup_template_argument (struct d_print_info *dpi,
   3163 			    const struct demangle_component *dc)
   3164 {
   3165   if (dpi->templates == NULL)
   3166     {
   3167       d_print_error (dpi);
   3168       return NULL;
   3169     }
   3170 
   3171   return d_index_template_argument
   3172     (d_right (dpi->templates->template_decl),
   3173      dc->u.s_number.number);
   3174 }
   3175 
   3176 /* Returns a template argument pack used in DC (any will do), or NULL.  */
   3177 
   3178 static struct demangle_component *
   3179 d_find_pack (struct d_print_info *dpi,
   3180 	     const struct demangle_component *dc)
   3181 {
   3182   struct demangle_component *a;
   3183   if (dc == NULL)
   3184     return NULL;
   3185 
   3186   switch (dc->type)
   3187     {
   3188     case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
   3189       a = d_lookup_template_argument (dpi, dc);
   3190       if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
   3191 	return a;
   3192       return NULL;
   3193 
   3194     case DEMANGLE_COMPONENT_PACK_EXPANSION:
   3195       return NULL;
   3196 
   3197     case DEMANGLE_COMPONENT_NAME:
   3198     case DEMANGLE_COMPONENT_OPERATOR:
   3199     case DEMANGLE_COMPONENT_BUILTIN_TYPE:
   3200     case DEMANGLE_COMPONENT_SUB_STD:
   3201     case DEMANGLE_COMPONENT_CHARACTER:
   3202       return NULL;
   3203 
   3204     case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
   3205       return d_find_pack (dpi, dc->u.s_extended_operator.name);
   3206     case DEMANGLE_COMPONENT_CTOR:
   3207       return d_find_pack (dpi, dc->u.s_ctor.name);
   3208     case DEMANGLE_COMPONENT_DTOR:
   3209       return d_find_pack (dpi, dc->u.s_dtor.name);
   3210 
   3211     default:
   3212       a = d_find_pack (dpi, d_left (dc));
   3213       if (a)
   3214 	return a;
   3215       return d_find_pack (dpi, d_right (dc));
   3216     }
   3217 }
   3218 
   3219 /* Returns the length of the template argument pack DC.  */
   3220 
   3221 static int
   3222 d_pack_length (const struct demangle_component *dc)
   3223 {
   3224   int count = 0;
   3225   while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
   3226 	 && d_left (dc) != NULL)
   3227     {
   3228       ++count;
   3229       dc = d_right (dc);
   3230     }
   3231   return count;
   3232 }
   3233 
   3234 /* DC is a component of a mangled expression.  Print it, wrapped in parens
   3235    if needed.  */
   3236 
   3237 static void
   3238 d_print_subexpr (struct d_print_info *dpi,
   3239 		 const struct demangle_component *dc)
   3240 {
   3241   int simple = 0;
   3242   if (dc->type == DEMANGLE_COMPONENT_NAME)
   3243     simple = 1;
   3244   if (!simple)
   3245     d_append_char (dpi, '(');
   3246   d_print_comp (dpi, dc);
   3247   if (!simple)
   3248     d_append_char (dpi, ')');
   3249 }
   3250 
   3251 /* Subroutine to handle components.  */
   3252 
   3253 static void
   3254 d_print_comp (struct d_print_info *dpi,
   3255               const struct demangle_component *dc)
   3256 {
   3257   if (dc == NULL)
   3258     {
   3259       d_print_error (dpi);
   3260       return;
   3261     }
   3262   if (d_print_saw_error (dpi))
   3263     return;
   3264 
   3265   switch (dc->type)
   3266     {
   3267     case DEMANGLE_COMPONENT_NAME:
   3268       if ((dpi->options & DMGL_JAVA) == 0)
   3269 	d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
   3270       else
   3271 	d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
   3272       return;
   3273 
   3274     case DEMANGLE_COMPONENT_QUAL_NAME:
   3275     case DEMANGLE_COMPONENT_LOCAL_NAME:
   3276       d_print_comp (dpi, d_left (dc));
   3277       if ((dpi->options & DMGL_JAVA) == 0)
   3278 	d_append_string (dpi, "::");
   3279       else
   3280 	d_append_char (dpi, '.');
   3281       d_print_comp (dpi, d_right (dc));
   3282       return;
   3283 
   3284     case DEMANGLE_COMPONENT_TYPED_NAME:
   3285       {
   3286 	struct d_print_mod *hold_modifiers;
   3287 	struct demangle_component *typed_name;
   3288 	struct d_print_mod adpm[4];
   3289 	unsigned int i;
   3290 	struct d_print_template dpt;
   3291 
   3292 	/* Pass the name down to the type so that it can be printed in
   3293 	   the right place for the type.  We also have to pass down
   3294 	   any CV-qualifiers, which apply to the this parameter.  */
   3295 	hold_modifiers = dpi->modifiers;
   3296 	i = 0;
   3297 	typed_name = d_left (dc);
   3298 	while (typed_name != NULL)
   3299 	  {
   3300 	    if (i >= sizeof adpm / sizeof adpm[0])
   3301 	      {
   3302 		d_print_error (dpi);
   3303 		return;
   3304 	      }
   3305 
   3306 	    adpm[i].next = dpi->modifiers;
   3307 	    dpi->modifiers = &adpm[i];
   3308 	    adpm[i].mod = typed_name;
   3309 	    adpm[i].printed = 0;
   3310 	    adpm[i].templates = dpi->templates;
   3311 	    ++i;
   3312 
   3313 	    if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
   3314 		&& typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
   3315 		&& typed_name->type != DEMANGLE_COMPONENT_CONST_THIS)
   3316 	      break;
   3317 
   3318 	    typed_name = d_left (typed_name);
   3319 	  }
   3320 
   3321 	if (typed_name == NULL)
   3322 	  {
   3323 	    d_print_error (dpi);
   3324 	    return;
   3325 	  }
   3326 
   3327 	/* If typed_name is a template, then it applies to the
   3328 	   function type as well.  */
   3329 	if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
   3330 	  {
   3331 	    dpt.next = dpi->templates;
   3332 	    dpi->templates = &dpt;
   3333 	    dpt.template_decl = typed_name;
   3334 	  }
   3335 
   3336 	/* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
   3337 	   there may be CV-qualifiers on its right argument which
   3338 	   really apply here; this happens when parsing a class which
   3339 	   is local to a function.  */
   3340 	if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
   3341 	  {
   3342 	    struct demangle_component *local_name;
   3343 
   3344 	    local_name = d_right (typed_name);
   3345 	    while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
   3346 		   || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
   3347 		   || local_name->type == DEMANGLE_COMPONENT_CONST_THIS)
   3348 	      {
   3349 		if (i >= sizeof adpm / sizeof adpm[0])
   3350 		  {
   3351 		    d_print_error (dpi);
   3352 		    return;
   3353 		  }
   3354 
   3355 		adpm[i] = adpm[i - 1];
   3356 		adpm[i].next = &adpm[i - 1];
   3357 		dpi->modifiers = &adpm[i];
   3358 
   3359 		adpm[i - 1].mod = local_name;
   3360 		adpm[i - 1].printed = 0;
   3361 		adpm[i - 1].templates = dpi->templates;
   3362 		++i;
   3363 
   3364 		local_name = d_left (local_name);
   3365 	      }
   3366 	  }
   3367 
   3368 	d_print_comp (dpi, d_right (dc));
   3369 
   3370 	if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
   3371 	  dpi->templates = dpt.next;
   3372 
   3373 	/* If the modifiers didn't get printed by the type, print them
   3374 	   now.  */
   3375 	while (i > 0)
   3376 	  {
   3377 	    --i;
   3378 	    if (! adpm[i].printed)
   3379 	      {
   3380 		d_append_char (dpi, ' ');
   3381 		d_print_mod (dpi, adpm[i].mod);
   3382 	      }
   3383 	  }
   3384 
   3385 	dpi->modifiers = hold_modifiers;
   3386 
   3387 	return;
   3388       }
   3389 
   3390     case DEMANGLE_COMPONENT_TEMPLATE:
   3391       {
   3392 	struct d_print_mod *hold_dpm;
   3393 	struct demangle_component *dcl;
   3394 
   3395 	/* Don't push modifiers into a template definition.  Doing so
   3396 	   could give the wrong definition for a template argument.
   3397 	   Instead, treat the template essentially as a name.  */
   3398 
   3399 	hold_dpm = dpi->modifiers;
   3400 	dpi->modifiers = NULL;
   3401 
   3402         dcl = d_left (dc);
   3403 
   3404         if ((dpi->options & DMGL_JAVA) != 0
   3405             && dcl->type == DEMANGLE_COMPONENT_NAME
   3406             && dcl->u.s_name.len == 6
   3407             && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
   3408           {
   3409             /* Special-case Java arrays, so that JArray<TYPE> appears
   3410                instead as TYPE[].  */
   3411 
   3412             d_print_comp (dpi, d_right (dc));
   3413             d_append_string (dpi, "[]");
   3414           }
   3415         else
   3416           {
   3417 	    d_print_comp (dpi, dcl);
   3418 	    if (d_last_char (dpi) == '<')
   3419 	      d_append_char (dpi, ' ');
   3420 	    d_append_char (dpi, '<');
   3421 	    d_print_comp (dpi, d_right (dc));
   3422 	    /* Avoid generating two consecutive '>' characters, to avoid
   3423 	       the C++ syntactic ambiguity.  */
   3424 	    if (d_last_char (dpi) == '>')
   3425 	      d_append_char (dpi, ' ');
   3426 	    d_append_char (dpi, '>');
   3427           }
   3428 
   3429 	dpi->modifiers = hold_dpm;
   3430 
   3431 	return;
   3432       }
   3433 
   3434     case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
   3435       {
   3436 	struct d_print_template *hold_dpt;
   3437 	struct demangle_component *a = d_lookup_template_argument (dpi, dc);
   3438 
   3439 	if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
   3440 	  a = d_index_template_argument (a, dpi->pack_index);
   3441 
   3442 	if (a == NULL)
   3443 	  {
   3444 	    d_print_error (dpi);
   3445 	    return;
   3446 	  }
   3447 
   3448 	/* While processing this parameter, we need to pop the list of
   3449 	   templates.  This is because the template parameter may
   3450 	   itself be a reference to a parameter of an outer
   3451 	   template.  */
   3452 
   3453 	hold_dpt = dpi->templates;
   3454 	dpi->templates = hold_dpt->next;
   3455 
   3456 	d_print_comp (dpi, a);
   3457 
   3458 	dpi->templates = hold_dpt;
   3459 
   3460 	return;
   3461       }
   3462 
   3463     case DEMANGLE_COMPONENT_CTOR:
   3464       d_print_comp (dpi, dc->u.s_ctor.name);
   3465       return;
   3466 
   3467     case DEMANGLE_COMPONENT_DTOR:
   3468       d_append_char (dpi, '~');
   3469       d_print_comp (dpi, dc->u.s_dtor.name);
   3470       return;
   3471 
   3472     case DEMANGLE_COMPONENT_VTABLE:
   3473       d_append_string (dpi, "vtable for ");
   3474       d_print_comp (dpi, d_left (dc));
   3475       return;
   3476 
   3477     case DEMANGLE_COMPONENT_VTT:
   3478       d_append_string (dpi, "VTT for ");
   3479       d_print_comp (dpi, d_left (dc));
   3480       return;
   3481 
   3482     case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
   3483       d_append_string (dpi, "construction vtable for ");
   3484       d_print_comp (dpi, d_left (dc));
   3485       d_append_string (dpi, "-in-");
   3486       d_print_comp (dpi, d_right (dc));
   3487       return;
   3488 
   3489     case DEMANGLE_COMPONENT_TYPEINFO:
   3490       d_append_string (dpi, "typeinfo for ");
   3491       d_print_comp (dpi, d_left (dc));
   3492       return;
   3493 
   3494     case DEMANGLE_COMPONENT_TYPEINFO_NAME:
   3495       d_append_string (dpi, "typeinfo name for ");
   3496       d_print_comp (dpi, d_left (dc));
   3497       return;
   3498 
   3499     case DEMANGLE_COMPONENT_TYPEINFO_FN:
   3500       d_append_string (dpi, "typeinfo fn for ");
   3501       d_print_comp (dpi, d_left (dc));
   3502       return;
   3503 
   3504     case DEMANGLE_COMPONENT_THUNK:
   3505       d_append_string (dpi, "non-virtual thunk to ");
   3506       d_print_comp (dpi, d_left (dc));
   3507       return;
   3508 
   3509     case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
   3510       d_append_string (dpi, "virtual thunk to ");
   3511       d_print_comp (dpi, d_left (dc));
   3512       return;
   3513 
   3514     case DEMANGLE_COMPONENT_COVARIANT_THUNK:
   3515       d_append_string (dpi, "covariant return thunk to ");
   3516       d_print_comp (dpi, d_left (dc));
   3517       return;
   3518 
   3519     case DEMANGLE_COMPONENT_JAVA_CLASS:
   3520       d_append_string (dpi, "java Class for ");
   3521       d_print_comp (dpi, d_left (dc));
   3522       return;
   3523 
   3524     case DEMANGLE_COMPONENT_GUARD:
   3525       d_append_string (dpi, "guard variable for ");
   3526       d_print_comp (dpi, d_left (dc));
   3527       return;
   3528 
   3529     case DEMANGLE_COMPONENT_REFTEMP:
   3530       d_append_string (dpi, "reference temporary for ");
   3531       d_print_comp (dpi, d_left (dc));
   3532       return;
   3533 
   3534     case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
   3535       d_append_string (dpi, "hidden alias for ");
   3536       d_print_comp (dpi, d_left (dc));
   3537       return;
   3538 
   3539     case DEMANGLE_COMPONENT_SUB_STD:
   3540       d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
   3541       return;
   3542 
   3543     case DEMANGLE_COMPONENT_RESTRICT:
   3544     case DEMANGLE_COMPONENT_VOLATILE:
   3545     case DEMANGLE_COMPONENT_CONST:
   3546       {
   3547 	struct d_print_mod *pdpm;
   3548 
   3549 	/* When printing arrays, it's possible to have cases where the
   3550 	   same CV-qualifier gets pushed on the stack multiple times.
   3551 	   We only need to print it once.  */
   3552 
   3553 	for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
   3554 	  {
   3555 	    if (! pdpm->printed)
   3556 	      {
   3557 		if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
   3558 		    && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
   3559 		    && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
   3560 		  break;
   3561 		if (pdpm->mod->type == dc->type)
   3562 		  {
   3563 		    d_print_comp (dpi, d_left (dc));
   3564 		    return;
   3565 		  }
   3566 	      }
   3567 	  }
   3568       }
   3569       /* Fall through.  */
   3570     case DEMANGLE_COMPONENT_RESTRICT_THIS:
   3571     case DEMANGLE_COMPONENT_VOLATILE_THIS:
   3572     case DEMANGLE_COMPONENT_CONST_THIS:
   3573     case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
   3574     case DEMANGLE_COMPONENT_POINTER:
   3575     case DEMANGLE_COMPONENT_REFERENCE:
   3576     case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
   3577     case DEMANGLE_COMPONENT_COMPLEX:
   3578     case DEMANGLE_COMPONENT_IMAGINARY:
   3579       {
   3580 	/* We keep a list of modifiers on the stack.  */
   3581 	struct d_print_mod dpm;
   3582 
   3583 	dpm.next = dpi->modifiers;
   3584 	dpi->modifiers = &dpm;
   3585 	dpm.mod = dc;
   3586 	dpm.printed = 0;
   3587 	dpm.templates = dpi->templates;
   3588 
   3589 	d_print_comp (dpi, d_left (dc));
   3590 
   3591 	/* If the modifier didn't get printed by the type, print it
   3592 	   now.  */
   3593 	if (! dpm.printed)
   3594 	  d_print_mod (dpi, dc);
   3595 
   3596 	dpi->modifiers = dpm.next;
   3597 
   3598 	return;
   3599       }
   3600 
   3601     case DEMANGLE_COMPONENT_BUILTIN_TYPE:
   3602       if ((dpi->options & DMGL_JAVA) == 0)
   3603 	d_append_buffer (dpi, dc->u.s_builtin.type->name,
   3604 			 dc->u.s_builtin.type->len);
   3605       else
   3606 	d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
   3607 			 dc->u.s_builtin.type->java_len);
   3608       return;
   3609 
   3610     case DEMANGLE_COMPONENT_VENDOR_TYPE:
   3611       d_print_comp (dpi, d_left (dc));
   3612       return;
   3613 
   3614     case DEMANGLE_COMPONENT_FUNCTION_TYPE:
   3615       {
   3616 	if ((dpi->options & DMGL_RET_POSTFIX) != 0)
   3617 	  d_print_function_type (dpi, dc, dpi->modifiers);
   3618 
   3619 	/* Print return type if present */
   3620 	if (d_left (dc) != NULL)
   3621 	  {
   3622 	    struct d_print_mod dpm;
   3623 
   3624 	    /* We must pass this type down as a modifier in order to
   3625 	       print it in the right location.  */
   3626 	    dpm.next = dpi->modifiers;
   3627 	    dpi->modifiers = &dpm;
   3628 	    dpm.mod = dc;
   3629 	    dpm.printed = 0;
   3630 	    dpm.templates = dpi->templates;
   3631 
   3632 	    d_print_comp (dpi, d_left (dc));
   3633 
   3634 	    dpi->modifiers = dpm.next;
   3635 
   3636 	    if (dpm.printed)
   3637 	      return;
   3638 
   3639 	    /* In standard prefix notation, there is a space between the
   3640 	       return type and the function signature.  */
   3641 	    if ((dpi->options & DMGL_RET_POSTFIX) == 0)
   3642 	      d_append_char (dpi, ' ');
   3643 	  }
   3644 
   3645 	if ((dpi->options & DMGL_RET_POSTFIX) == 0)
   3646 	  d_print_function_type (dpi, dc, dpi->modifiers);
   3647 
   3648 	return;
   3649       }
   3650 
   3651     case DEMANGLE_COMPONENT_ARRAY_TYPE:
   3652       {
   3653 	struct d_print_mod *hold_modifiers;
   3654 	struct d_print_mod adpm[4];
   3655 	unsigned int i;
   3656 	struct d_print_mod *pdpm;
   3657 
   3658 	/* We must pass this type down as a modifier in order to print
   3659 	   multi-dimensional arrays correctly.  If the array itself is
   3660 	   CV-qualified, we act as though the element type were
   3661 	   CV-qualified.  We do this by copying the modifiers down
   3662 	   rather than fiddling pointers, so that we don't wind up
   3663 	   with a d_print_mod higher on the stack pointing into our
   3664 	   stack frame after we return.  */
   3665 
   3666 	hold_modifiers = dpi->modifiers;
   3667 
   3668 	adpm[0].next = hold_modifiers;
   3669 	dpi->modifiers = &adpm[0];
   3670 	adpm[0].mod = dc;
   3671 	adpm[0].printed = 0;
   3672 	adpm[0].templates = dpi->templates;
   3673 
   3674 	i = 1;
   3675 	pdpm = hold_modifiers;
   3676 	while (pdpm != NULL
   3677 	       && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
   3678 		   || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
   3679 		   || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
   3680 	  {
   3681 	    if (! pdpm->printed)
   3682 	      {
   3683 		if (i >= sizeof adpm / sizeof adpm[0])
   3684 		  {
   3685 		    d_print_error (dpi);
   3686 		    return;
   3687 		  }
   3688 
   3689 		adpm[i] = *pdpm;
   3690 		adpm[i].next = dpi->modifiers;
   3691 		dpi->modifiers = &adpm[i];
   3692 		pdpm->printed = 1;
   3693 		++i;
   3694 	      }
   3695 
   3696 	    pdpm = pdpm->next;
   3697 	  }
   3698 
   3699 	d_print_comp (dpi, d_right (dc));
   3700 
   3701 	dpi->modifiers = hold_modifiers;
   3702 
   3703 	if (adpm[0].printed)
   3704 	  return;
   3705 
   3706 	while (i > 1)
   3707 	  {
   3708 	    --i;
   3709 	    d_print_mod (dpi, adpm[i].mod);
   3710 	  }
   3711 
   3712 	d_print_array_type (dpi, dc, dpi->modifiers);
   3713 
   3714 	return;
   3715       }
   3716 
   3717     case DEMANGLE_COMPONENT_PTRMEM_TYPE:
   3718       {
   3719 	struct d_print_mod dpm;
   3720 
   3721 	dpm.next = dpi->modifiers;
   3722 	dpi->modifiers = &dpm;
   3723 	dpm.mod = dc;
   3724 	dpm.printed = 0;
   3725 	dpm.templates = dpi->templates;
   3726 
   3727 	d_print_comp (dpi, d_right (dc));
   3728 
   3729 	/* If the modifier didn't get printed by the type, print it
   3730 	   now.  */
   3731 	if (! dpm.printed)
   3732 	  {
   3733 	    d_append_char (dpi, ' ');
   3734 	    d_print_comp (dpi, d_left (dc));
   3735 	    d_append_string (dpi, "::*");
   3736 	  }
   3737 
   3738 	dpi->modifiers = dpm.next;
   3739 
   3740 	return;
   3741       }
   3742 
   3743     case DEMANGLE_COMPONENT_ARGLIST:
   3744     case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
   3745       if (d_left (dc) != NULL)
   3746 	d_print_comp (dpi, d_left (dc));
   3747       if (d_right (dc) != NULL)
   3748 	{
   3749 	  d_append_string (dpi, ", ");
   3750 	  d_print_comp (dpi, d_right (dc));
   3751 	}
   3752       return;
   3753 
   3754     case DEMANGLE_COMPONENT_OPERATOR:
   3755       {
   3756 	char c;
   3757 
   3758 	d_append_string (dpi, "operator");
   3759 	c = dc->u.s_operator.op->name[0];
   3760 	if (IS_LOWER (c))
   3761 	  d_append_char (dpi, ' ');
   3762 	d_append_buffer (dpi, dc->u.s_operator.op->name,
   3763 			 dc->u.s_operator.op->len);
   3764 	return;
   3765       }
   3766 
   3767     case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
   3768       d_append_string (dpi, "operator ");
   3769       d_print_comp (dpi, dc->u.s_extended_operator.name);
   3770       return;
   3771 
   3772     case DEMANGLE_COMPONENT_CAST:
   3773       d_append_string (dpi, "operator ");
   3774       d_print_cast (dpi, dc);
   3775       return;
   3776 
   3777     case DEMANGLE_COMPONENT_UNARY:
   3778       if (d_left (dc)->type != DEMANGLE_COMPONENT_CAST)
   3779 	d_print_expr_op (dpi, d_left (dc));
   3780       else
   3781 	{
   3782 	  d_append_char (dpi, '(');
   3783 	  d_print_cast (dpi, d_left (dc));
   3784 	  d_append_char (dpi, ')');
   3785 	}
   3786       if (d_left (dc)->type == DEMANGLE_COMPONENT_CAST
   3787 	  && d_right (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
   3788 	/* type() -- FIXME what about type(multiple,args) */
   3789 	d_append_string (dpi, "()");
   3790       else
   3791 	d_print_subexpr (dpi, d_right (dc));
   3792       return;
   3793 
   3794     case DEMANGLE_COMPONENT_BINARY:
   3795       if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
   3796 	{
   3797 	  d_print_error (dpi);
   3798 	  return;
   3799 	}
   3800 
   3801       /* We wrap an expression which uses the greater-than operator in
   3802 	 an extra layer of parens so that it does not get confused
   3803 	 with the '>' which ends the template parameters.  */
   3804       if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
   3805 	  && d_left (dc)->u.s_operator.op->len == 1
   3806 	  && d_left (dc)->u.s_operator.op->name[0] == '>')
   3807 	d_append_char (dpi, '(');
   3808 
   3809       d_print_subexpr (dpi, d_left (d_right (dc)));
   3810       if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") != 0)
   3811 	d_print_expr_op (dpi, d_left (dc));
   3812       d_print_subexpr (dpi, d_right (d_right (dc)));
   3813 
   3814       if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
   3815 	  && d_left (dc)->u.s_operator.op->len == 1
   3816 	  && d_left (dc)->u.s_operator.op->name[0] == '>')
   3817 	d_append_char (dpi, ')');
   3818 
   3819       return;
   3820 
   3821     case DEMANGLE_COMPONENT_BINARY_ARGS:
   3822       /* We should only see this as part of DEMANGLE_COMPONENT_BINARY.  */
   3823       d_print_error (dpi);
   3824       return;
   3825 
   3826     case DEMANGLE_COMPONENT_TRINARY:
   3827       if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
   3828 	  || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
   3829 	{
   3830 	  d_print_error (dpi);
   3831 	  return;
   3832 	}
   3833       d_print_subexpr (dpi, d_left (d_right (dc)));
   3834       d_print_expr_op (dpi, d_left (dc));
   3835       d_print_subexpr (dpi, d_left (d_right (d_right (dc))));
   3836       d_append_string (dpi, " : ");
   3837       d_print_subexpr (dpi, d_right (d_right (d_right (dc))));
   3838       return;
   3839 
   3840     case DEMANGLE_COMPONENT_TRINARY_ARG1:
   3841     case DEMANGLE_COMPONENT_TRINARY_ARG2:
   3842       /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY.  */
   3843       d_print_error (dpi);
   3844       return;
   3845 
   3846     case DEMANGLE_COMPONENT_LITERAL:
   3847     case DEMANGLE_COMPONENT_LITERAL_NEG:
   3848       {
   3849 	enum d_builtin_type_print tp;
   3850 
   3851 	/* For some builtin types, produce simpler output.  */
   3852 	tp = D_PRINT_DEFAULT;
   3853 	if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
   3854 	  {
   3855 	    tp = d_left (dc)->u.s_builtin.type->print;
   3856 	    switch (tp)
   3857 	      {
   3858 	      case D_PRINT_INT:
   3859 	      case D_PRINT_UNSIGNED:
   3860 	      case D_PRINT_LONG:
   3861 	      case D_PRINT_UNSIGNED_LONG:
   3862 	      case D_PRINT_LONG_LONG:
   3863 	      case D_PRINT_UNSIGNED_LONG_LONG:
   3864 		if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
   3865 		  {
   3866 		    if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
   3867 		      d_append_char (dpi, '-');
   3868 		    d_print_comp (dpi, d_right (dc));
   3869 		    switch (tp)
   3870 		      {
   3871 		      default:
   3872 			break;
   3873 		      case D_PRINT_UNSIGNED:
   3874 			d_append_char (dpi, 'u');
   3875 			break;
   3876 		      case D_PRINT_LONG:
   3877 			d_append_char (dpi, 'l');
   3878 			break;
   3879 		      case D_PRINT_UNSIGNED_LONG:
   3880 			d_append_string (dpi, "ul");
   3881 			break;
   3882 		      case D_PRINT_LONG_LONG:
   3883 			d_append_string (dpi, "ll");
   3884 			break;
   3885 		      case D_PRINT_UNSIGNED_LONG_LONG:
   3886 			d_append_string (dpi, "ull");
   3887 			break;
   3888 		      }
   3889 		    return;
   3890 		  }
   3891 		break;
   3892 
   3893 	      case D_PRINT_BOOL:
   3894 		if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
   3895 		    && d_right (dc)->u.s_name.len == 1
   3896 		    && dc->type == DEMANGLE_COMPONENT_LITERAL)
   3897 		  {
   3898 		    switch (d_right (dc)->u.s_name.s[0])
   3899 		      {
   3900 		      case '0':
   3901 			d_append_string (dpi, "false");
   3902 			return;
   3903 		      case '1':
   3904 			d_append_string (dpi, "true");
   3905 			return;
   3906 		      default:
   3907 			break;
   3908 		      }
   3909 		  }
   3910 		break;
   3911 
   3912 	      default:
   3913 		break;
   3914 	      }
   3915 	  }
   3916 
   3917 	d_append_char (dpi, '(');
   3918 	d_print_comp (dpi, d_left (dc));
   3919 	d_append_char (dpi, ')');
   3920 	if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
   3921 	  d_append_char (dpi, '-');
   3922 	if (tp == D_PRINT_FLOAT)
   3923 	  d_append_char (dpi, '[');
   3924 	d_print_comp (dpi, d_right (dc));
   3925 	if (tp == D_PRINT_FLOAT)
   3926 	  d_append_char (dpi, ']');
   3927       }
   3928       return;
   3929 
   3930     case DEMANGLE_COMPONENT_JAVA_RESOURCE:
   3931       d_append_string (dpi, "java resource ");
   3932       d_print_comp (dpi, d_left (dc));
   3933       return;
   3934 
   3935     case DEMANGLE_COMPONENT_COMPOUND_NAME:
   3936       d_print_comp (dpi, d_left (dc));
   3937       d_print_comp (dpi, d_right (dc));
   3938       return;
   3939 
   3940     case DEMANGLE_COMPONENT_CHARACTER:
   3941       d_append_char (dpi, dc->u.s_character.character);
   3942       return;
   3943 
   3944     case DEMANGLE_COMPONENT_DECLTYPE:
   3945       d_append_string (dpi, "decltype (");
   3946       d_print_comp (dpi, d_left (dc));
   3947       d_append_char (dpi, ')');
   3948       return;
   3949 
   3950     case DEMANGLE_COMPONENT_PACK_EXPANSION:
   3951       {
   3952 	struct demangle_component *a = d_find_pack (dpi, d_left (dc));
   3953 	int len = d_pack_length (a);
   3954 	int i;
   3955 
   3956 	dc = d_left (dc);
   3957 	for (i = 0; i < len; ++i)
   3958 	  {
   3959 	    dpi->pack_index = i;
   3960 	    d_print_comp (dpi, dc);
   3961 	    if (i < len-1)
   3962 	      d_append_string (dpi, ", ");
   3963 	  }
   3964       }
   3965       return;
   3966 
   3967     default:
   3968       d_print_error (dpi);
   3969       return;
   3970     }
   3971 }
   3972 
   3973 /* Print a Java dentifier.  For Java we try to handle encoded extended
   3974    Unicode characters.  The C++ ABI doesn't mention Unicode encoding,
   3975    so we don't it for C++.  Characters are encoded as
   3976    __U<hex-char>+_.  */
   3977 
   3978 static void
   3979 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
   3980 {
   3981   const char *p;
   3982   const char *end;
   3983 
   3984   end = name + len;
   3985   for (p = name; p < end; ++p)
   3986     {
   3987       if (end - p > 3
   3988 	  && p[0] == '_'
   3989 	  && p[1] == '_'
   3990 	  && p[2] == 'U')
   3991 	{
   3992 	  unsigned long c;
   3993 	  const char *q;
   3994 
   3995 	  c = 0;
   3996 	  for (q = p + 3; q < end; ++q)
   3997 	    {
   3998 	      int dig;
   3999 
   4000 	      if (IS_DIGIT (*q))
   4001 		dig = *q - '0';
   4002 	      else if (*q >= 'A' && *q <= 'F')
   4003 		dig = *q - 'A' + 10;
   4004 	      else if (*q >= 'a' && *q <= 'f')
   4005 		dig = *q - 'a' + 10;
   4006 	      else
   4007 		break;
   4008 
   4009 	      c = c * 16 + dig;
   4010 	    }
   4011 	  /* If the Unicode character is larger than 256, we don't try
   4012 	     to deal with it here.  FIXME.  */
   4013 	  if (q < end && *q == '_' && c < 256)
   4014 	    {
   4015 	      d_append_char (dpi, c);
   4016 	      p = q;
   4017 	      continue;
   4018 	    }
   4019 	}
   4020 
   4021       d_append_char (dpi, *p);
   4022     }
   4023 }
   4024 
   4025 /* Print a list of modifiers.  SUFFIX is 1 if we are printing
   4026    qualifiers on this after printing a function.  */
   4027 
   4028 static void
   4029 d_print_mod_list (struct d_print_info *dpi,
   4030                   struct d_print_mod *mods, int suffix)
   4031 {
   4032   struct d_print_template *hold_dpt;
   4033 
   4034   if (mods == NULL || d_print_saw_error (dpi))
   4035     return;
   4036 
   4037   if (mods->printed
   4038       || (! suffix
   4039 	  && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
   4040 	      || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
   4041 	      || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS)))
   4042     {
   4043       d_print_mod_list (dpi, mods->next, suffix);
   4044       return;
   4045     }
   4046 
   4047   mods->printed = 1;
   4048 
   4049   hold_dpt = dpi->templates;
   4050   dpi->templates = mods->templates;
   4051 
   4052   if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
   4053     {
   4054       d_print_function_type (dpi, mods->mod, mods->next);
   4055       dpi->templates = hold_dpt;
   4056       return;
   4057     }
   4058   else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
   4059     {
   4060       d_print_array_type (dpi, mods->mod, mods->next);
   4061       dpi->templates = hold_dpt;
   4062       return;
   4063     }
   4064   else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
   4065     {
   4066       struct d_print_mod *hold_modifiers;
   4067       struct demangle_component *dc;
   4068 
   4069       /* When this is on the modifier stack, we have pulled any
   4070 	 qualifiers off the right argument already.  Otherwise, we
   4071 	 print it as usual, but don't let the left argument see any
   4072 	 modifiers.  */
   4073 
   4074       hold_modifiers = dpi->modifiers;
   4075       dpi->modifiers = NULL;
   4076       d_print_comp (dpi, d_left (mods->mod));
   4077       dpi->modifiers = hold_modifiers;
   4078 
   4079       if ((dpi->options & DMGL_JAVA) == 0)
   4080 	d_append_string (dpi, "::");
   4081       else
   4082 	d_append_char (dpi, '.');
   4083 
   4084       dc = d_right (mods->mod);
   4085       while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
   4086 	     || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
   4087 	     || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
   4088 	dc = d_left (dc);
   4089 
   4090       d_print_comp (dpi, dc);
   4091 
   4092       dpi->templates = hold_dpt;
   4093       return;
   4094     }
   4095 
   4096   d_print_mod (dpi, mods->mod);
   4097 
   4098   dpi->templates = hold_dpt;
   4099 
   4100   d_print_mod_list (dpi, mods->next, suffix);
   4101 }
   4102 
   4103 /* Print a modifier.  */
   4104 
   4105 static void
   4106 d_print_mod (struct d_print_info *dpi,
   4107              const struct demangle_component *mod)
   4108 {
   4109   switch (mod->type)
   4110     {
   4111     case DEMANGLE_COMPONENT_RESTRICT:
   4112     case DEMANGLE_COMPONENT_RESTRICT_THIS:
   4113       d_append_string (dpi, " restrict");
   4114       return;
   4115     case DEMANGLE_COMPONENT_VOLATILE:
   4116     case DEMANGLE_COMPONENT_VOLATILE_THIS:
   4117       d_append_string (dpi, " volatile");
   4118       return;
   4119     case DEMANGLE_COMPONENT_CONST:
   4120     case DEMANGLE_COMPONENT_CONST_THIS:
   4121       d_append_string (dpi, " const");
   4122       return;
   4123     case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
   4124       d_append_char (dpi, ' ');
   4125       d_print_comp (dpi, d_right (mod));
   4126       return;
   4127     case DEMANGLE_COMPONENT_POINTER:
   4128       /* There is no pointer symbol in Java.  */
   4129       if ((dpi->options & DMGL_JAVA) == 0)
   4130 	d_append_char (dpi, '*');
   4131       return;
   4132     case DEMANGLE_COMPONENT_REFERENCE:
   4133       d_append_char (dpi, '&');
   4134       return;
   4135     case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
   4136       d_append_string (dpi, "&&");
   4137       return;
   4138     case DEMANGLE_COMPONENT_COMPLEX:
   4139       d_append_string (dpi, "complex ");
   4140       return;
   4141     case DEMANGLE_COMPONENT_IMAGINARY:
   4142       d_append_string (dpi, "imaginary ");
   4143       return;
   4144     case DEMANGLE_COMPONENT_PTRMEM_TYPE:
   4145       if (d_last_char (dpi) != '(')
   4146 	d_append_char (dpi, ' ');
   4147       d_print_comp (dpi, d_left (mod));
   4148       d_append_string (dpi, "::*");
   4149       return;
   4150     case DEMANGLE_COMPONENT_TYPED_NAME:
   4151       d_print_comp (dpi, d_left (mod));
   4152       return;
   4153     default:
   4154       /* Otherwise, we have something that won't go back on the
   4155 	 modifier stack, so we can just print it.  */
   4156       d_print_comp (dpi, mod);
   4157       return;
   4158     }
   4159 }
   4160 
   4161 /* Print a function type, except for the return type.  */
   4162 
   4163 static void
   4164 d_print_function_type (struct d_print_info *dpi,
   4165                        const struct demangle_component *dc,
   4166                        struct d_print_mod *mods)
   4167 {
   4168   int need_paren;
   4169   int saw_mod;
   4170   int need_space;
   4171   struct d_print_mod *p;
   4172   struct d_print_mod *hold_modifiers;
   4173 
   4174   need_paren = 0;
   4175   saw_mod = 0;
   4176   need_space = 0;
   4177   for (p = mods; p != NULL; p = p->next)
   4178     {
   4179       if (p->printed)
   4180 	break;
   4181 
   4182       saw_mod = 1;
   4183       switch (p->mod->type)
   4184 	{
   4185 	case DEMANGLE_COMPONENT_POINTER:
   4186 	case DEMANGLE_COMPONENT_REFERENCE:
   4187 	case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
   4188 	  need_paren = 1;
   4189 	  break;
   4190 	case DEMANGLE_COMPONENT_RESTRICT:
   4191 	case DEMANGLE_COMPONENT_VOLATILE:
   4192 	case DEMANGLE_COMPONENT_CONST:
   4193 	case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
   4194 	case DEMANGLE_COMPONENT_COMPLEX:
   4195 	case DEMANGLE_COMPONENT_IMAGINARY:
   4196 	case DEMANGLE_COMPONENT_PTRMEM_TYPE:
   4197 	  need_space = 1;
   4198 	  need_paren = 1;
   4199 	  break;
   4200 	case DEMANGLE_COMPONENT_RESTRICT_THIS:
   4201 	case DEMANGLE_COMPONENT_VOLATILE_THIS:
   4202 	case DEMANGLE_COMPONENT_CONST_THIS:
   4203 	  break;
   4204 	default:
   4205 	  break;
   4206 	}
   4207       if (need_paren)
   4208 	break;
   4209     }
   4210 
   4211   if (d_left (dc) != NULL && ! saw_mod)
   4212     need_paren = 1;
   4213 
   4214   if (need_paren)
   4215     {
   4216       if (! need_space)
   4217 	{
   4218 	  if (d_last_char (dpi) != '('
   4219 	      && d_last_char (dpi) != '*')
   4220 	    need_space = 1;
   4221 	}
   4222       if (need_space && d_last_char (dpi) != ' ')
   4223 	d_append_char (dpi, ' ');
   4224       d_append_char (dpi, '(');
   4225     }
   4226 
   4227   hold_modifiers = dpi->modifiers;
   4228   dpi->modifiers = NULL;
   4229 
   4230   d_print_mod_list (dpi, mods, 0);
   4231 
   4232   if (need_paren)
   4233     d_append_char (dpi, ')');
   4234 
   4235   d_append_char (dpi, '(');
   4236 
   4237   if (d_right (dc) != NULL)
   4238     d_print_comp (dpi, d_right (dc));
   4239 
   4240   d_append_char (dpi, ')');
   4241 
   4242   d_print_mod_list (dpi, mods, 1);
   4243 
   4244   dpi->modifiers = hold_modifiers;
   4245 }
   4246 
   4247 /* Print an array type, except for the element type.  */
   4248 
   4249 static void
   4250 d_print_array_type (struct d_print_info *dpi,
   4251                     const struct demangle_component *dc,
   4252                     struct d_print_mod *mods)
   4253 {
   4254   int need_space;
   4255 
   4256   need_space = 1;
   4257   if (mods != NULL)
   4258     {
   4259       int need_paren;
   4260       struct d_print_mod *p;
   4261 
   4262       need_paren = 0;
   4263       for (p = mods; p != NULL; p = p->next)
   4264 	{
   4265 	  if (! p->printed)
   4266 	    {
   4267 	      if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
   4268 		{
   4269 		  need_space = 0;
   4270 		  break;
   4271 		}
   4272 	      else
   4273 		{
   4274 		  need_paren = 1;
   4275 		  need_space = 1;
   4276 		  break;
   4277 		}
   4278 	    }
   4279 	}
   4280 
   4281       if (need_paren)
   4282 	d_append_string (dpi, " (");
   4283 
   4284       d_print_mod_list (dpi, mods, 0);
   4285 
   4286       if (need_paren)
   4287 	d_append_char (dpi, ')');
   4288     }
   4289 
   4290   if (need_space)
   4291     d_append_char (dpi, ' ');
   4292 
   4293   d_append_char (dpi, '[');
   4294 
   4295   if (d_left (dc) != NULL)
   4296     d_print_comp (dpi, d_left (dc));
   4297 
   4298   d_append_char (dpi, ']');
   4299 }
   4300 
   4301 /* Print an operator in an expression.  */
   4302 
   4303 static void
   4304 d_print_expr_op (struct d_print_info *dpi,
   4305                  const struct demangle_component *dc)
   4306 {
   4307   if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
   4308     d_append_buffer (dpi, dc->u.s_operator.op->name,
   4309 		     dc->u.s_operator.op->len);
   4310   else
   4311     d_print_comp (dpi, dc);
   4312 }
   4313 
   4314 /* Print a cast.  */
   4315 
   4316 static void
   4317 d_print_cast (struct d_print_info *dpi,
   4318               const struct demangle_component *dc)
   4319 {
   4320   if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
   4321     d_print_comp (dpi, d_left (dc));
   4322   else
   4323     {
   4324       struct d_print_mod *hold_dpm;
   4325       struct d_print_template dpt;
   4326 
   4327       /* It appears that for a templated cast operator, we need to put
   4328 	 the template parameters in scope for the operator name, but
   4329 	 not for the parameters.  The effect is that we need to handle
   4330 	 the template printing here.  */
   4331 
   4332       hold_dpm = dpi->modifiers;
   4333       dpi->modifiers = NULL;
   4334 
   4335       dpt.next = dpi->templates;
   4336       dpi->templates = &dpt;
   4337       dpt.template_decl = d_left (dc);
   4338 
   4339       d_print_comp (dpi, d_left (d_left (dc)));
   4340 
   4341       dpi->templates = dpt.next;
   4342 
   4343       if (d_last_char (dpi) == '<')
   4344 	d_append_char (dpi, ' ');
   4345       d_append_char (dpi, '<');
   4346       d_print_comp (dpi, d_right (d_left (dc)));
   4347       /* Avoid generating two consecutive '>' characters, to avoid
   4348 	 the C++ syntactic ambiguity.  */
   4349       if (d_last_char (dpi) == '>')
   4350 	d_append_char (dpi, ' ');
   4351       d_append_char (dpi, '>');
   4352 
   4353       dpi->modifiers = hold_dpm;
   4354     }
   4355 }
   4356 
   4357 /* Initialize the information structure we use to pass around
   4358    information.  */
   4359 
   4360 CP_STATIC_IF_GLIBCPP_V3
   4361 void
   4362 cplus_demangle_init_info (const char *mangled, int options, size_t len,
   4363                           struct d_info *di)
   4364 {
   4365   di->s = mangled;
   4366   di->send = mangled + len;
   4367   di->options = options;
   4368 
   4369   di->n = mangled;
   4370 
   4371   /* We can not need more components than twice the number of chars in
   4372      the mangled string.  Most components correspond directly to
   4373      chars, but the ARGLIST types are exceptions.  */
   4374   di->num_comps = 2 * len;
   4375   di->next_comp = 0;
   4376 
   4377   /* Similarly, we can not need more substitutions than there are
   4378      chars in the mangled string.  */
   4379   di->num_subs = len;
   4380   di->next_sub = 0;
   4381   di->did_subs = 0;
   4382 
   4383   di->last_name = NULL;
   4384 
   4385   di->expansion = 0;
   4386 }
   4387 
   4388 /* Internal implementation for the demangler.  If MANGLED is a g++ v3 ABI
   4389    mangled name, return strings in repeated callback giving the demangled
   4390    name.  OPTIONS is the usual libiberty demangler options.  On success,
   4391    this returns 1.  On failure, returns 0.  */
   4392 
   4393 static int
   4394 d_demangle_callback (const char *mangled, int options,
   4395                      demangle_callbackref callback, void *opaque)
   4396 {
   4397   int type;
   4398   struct d_info di;
   4399   struct demangle_component *dc;
   4400   int status;
   4401 
   4402   if (mangled[0] == '_' && mangled[1] == 'Z')
   4403     type = 0;
   4404   else if (strncmp (mangled, "_GLOBAL_", 8) == 0
   4405 	   && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
   4406 	   && (mangled[9] == 'D' || mangled[9] == 'I')
   4407 	   && mangled[10] == '_')
   4408     {
   4409       const char *intro;
   4410 
   4411       intro = (mangled[9] == 'I')
   4412               ? "global constructors keyed to "
   4413               : "global destructors keyed to ";
   4414 
   4415       callback (intro, strlen (intro), opaque);
   4416       callback (mangled + 11, strlen (mangled + 11), opaque);
   4417       return 1;
   4418     }
   4419   else
   4420     {
   4421       if ((options & DMGL_TYPES) == 0)
   4422 	return 0;
   4423       type = 1;
   4424     }
   4425 
   4426   cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
   4427 
   4428   {
   4429 #ifdef CP_DYNAMIC_ARRAYS
   4430     __extension__ struct demangle_component comps[di.num_comps];
   4431     __extension__ struct demangle_component *subs[di.num_subs];
   4432 
   4433     di.comps = comps;
   4434     di.subs = subs;
   4435 #else
   4436     di.comps = alloca (di.num_comps * sizeof (*di.comps));
   4437     di.subs = alloca (di.num_subs * sizeof (*di.subs));
   4438 #endif
   4439 
   4440     if (type)
   4441       dc = cplus_demangle_type (&di);
   4442     else
   4443       dc = cplus_demangle_mangled_name (&di, 1);
   4444 
   4445     /* If DMGL_PARAMS is set, then if we didn't consume the entire
   4446        mangled string, then we didn't successfully demangle it.  If
   4447        DMGL_PARAMS is not set, we didn't look at the trailing
   4448        parameters.  */
   4449     if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
   4450       dc = NULL;
   4451 
   4452 #ifdef CP_DEMANGLE_DEBUG
   4453     d_dump (dc, 0);
   4454 #endif
   4455 
   4456     status = (dc != NULL)
   4457              ? cplus_demangle_print_callback (options, dc, callback, opaque)
   4458              : 0;
   4459   }
   4460 
   4461   return status;
   4462 }
   4463 
   4464 /* Entry point for the demangler.  If MANGLED is a g++ v3 ABI mangled
   4465    name, return a buffer allocated with malloc holding the demangled
   4466    name.  OPTIONS is the usual libiberty demangler options.  On
   4467    success, this sets *PALC to the allocated size of the returned
   4468    buffer.  On failure, this sets *PALC to 0 for a bad name, or 1 for
   4469    a memory allocation failure, and returns NULL.  */
   4470 
   4471 static char *
   4472 d_demangle (const char *mangled, int options, size_t *palc)
   4473 {
   4474   struct d_growable_string dgs;
   4475   int status;
   4476 
   4477   d_growable_string_init (&dgs, 0);
   4478 
   4479   status = d_demangle_callback (mangled, options,
   4480                                 d_growable_string_callback_adapter, &dgs);
   4481   if (status == 0)
   4482     {
   4483       free (dgs.buf);
   4484       *palc = 0;
   4485       return NULL;
   4486     }
   4487 
   4488   *palc = dgs.allocation_failure ? 1 : 0;
   4489   return dgs.buf;
   4490 }
   4491 
   4492 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
   4493 
   4494 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
   4495 
   4496 /* ia64 ABI-mandated entry point in the C++ runtime library for
   4497    performing demangling.  MANGLED_NAME is a NUL-terminated character
   4498    string containing the name to be demangled.
   4499 
   4500    OUTPUT_BUFFER is a region of memory, allocated with malloc, of
   4501    *LENGTH bytes, into which the demangled name is stored.  If
   4502    OUTPUT_BUFFER is not long enough, it is expanded using realloc.
   4503    OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
   4504    is placed in a region of memory allocated with malloc.
   4505 
   4506    If LENGTH is non-NULL, the length of the buffer containing the
   4507    demangled name, is placed in *LENGTH.
   4508 
   4509    The return value is a pointer to the start of the NUL-terminated
   4510    demangled name, or NULL if the demangling fails.  The caller is
   4511    responsible for deallocating this memory using free.
   4512 
   4513    *STATUS is set to one of the following values:
   4514       0: The demangling operation succeeded.
   4515      -1: A memory allocation failure occurred.
   4516      -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
   4517      -3: One of the arguments is invalid.
   4518 
   4519    The demangling is performed using the C++ ABI mangling rules, with
   4520    GNU extensions.  */
   4521 
   4522 char *
   4523 __cxa_demangle (const char *mangled_name, char *output_buffer,
   4524                 size_t *length, int *status)
   4525 {
   4526   char *demangled;
   4527   size_t alc;
   4528 
   4529   if (mangled_name == NULL)
   4530     {
   4531       if (status != NULL)
   4532 	*status = -3;
   4533       return NULL;
   4534     }
   4535 
   4536   if (output_buffer != NULL && length == NULL)
   4537     {
   4538       if (status != NULL)
   4539 	*status = -3;
   4540       return NULL;
   4541     }
   4542 
   4543   demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
   4544 
   4545   if (demangled == NULL)
   4546     {
   4547       if (status != NULL)
   4548 	{
   4549 	  if (alc == 1)
   4550 	    *status = -1;
   4551 	  else
   4552 	    *status = -2;
   4553 	}
   4554       return NULL;
   4555     }
   4556 
   4557   if (output_buffer == NULL)
   4558     {
   4559       if (length != NULL)
   4560 	*length = alc;
   4561     }
   4562   else
   4563     {
   4564       if (strlen (demangled) < *length)
   4565 	{
   4566 	  strcpy (output_buffer, demangled);
   4567 	  free (demangled);
   4568 	  demangled = output_buffer;
   4569 	}
   4570       else
   4571 	{
   4572 	  free (output_buffer);
   4573 	  *length = alc;
   4574 	}
   4575     }
   4576 
   4577   if (status != NULL)
   4578     *status = 0;
   4579 
   4580   return demangled;
   4581 }
   4582 
   4583 extern int __gcclibcxx_demangle_callback (const char *,
   4584                                           void (*)
   4585                                             (const char *, size_t, void *),
   4586                                           void *);
   4587 
   4588 /* Alternative, allocationless entry point in the C++ runtime library
   4589    for performing demangling.  MANGLED_NAME is a NUL-terminated character
   4590    string containing the name to be demangled.
   4591 
   4592    CALLBACK is a callback function, called with demangled string
   4593    segments as demangling progresses; it is called at least once,
   4594    but may be called more than once.  OPAQUE is a generalized pointer
   4595    used as a callback argument.
   4596 
   4597    The return code is one of the following values, equivalent to
   4598    the STATUS values of __cxa_demangle() (excluding -1, since this
   4599    function performs no memory allocations):
   4600       0: The demangling operation succeeded.
   4601      -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
   4602      -3: One of the arguments is invalid.
   4603 
   4604    The demangling is performed using the C++ ABI mangling rules, with
   4605    GNU extensions.  */
   4606 
   4607 int
   4608 __gcclibcxx_demangle_callback (const char *mangled_name,
   4609                                void (*callback) (const char *, size_t, void *),
   4610                                void *opaque)
   4611 {
   4612   int status;
   4613 
   4614   if (mangled_name == NULL || callback == NULL)
   4615     return -3;
   4616 
   4617   status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES,
   4618                                 callback, opaque);
   4619   if (status == 0)
   4620     return -2;
   4621 
   4622   return 0;
   4623 }
   4624 
   4625 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
   4626 
   4627 /* Entry point for libiberty demangler.  If MANGLED is a g++ v3 ABI
   4628    mangled name, return a buffer allocated with malloc holding the
   4629    demangled name.  Otherwise, return NULL.  */
   4630 
   4631 char *
   4632 cplus_demangle_v3 (const char *mangled, int options)
   4633 {
   4634   size_t alc;
   4635 
   4636   return d_demangle (mangled, options, &alc);
   4637 }
   4638 
   4639 int
   4640 cplus_demangle_v3_callback (const char *mangled, int options,
   4641                             demangle_callbackref callback, void *opaque)
   4642 {
   4643   return d_demangle_callback (mangled, options, callback, opaque);
   4644 }
   4645 
   4646 /* Demangle a Java symbol.  Java uses a subset of the V3 ABI C++ mangling
   4647    conventions, but the output formatting is a little different.
   4648    This instructs the C++ demangler not to emit pointer characters ("*"), to
   4649    use Java's namespace separator symbol ("." instead of "::"), and to output
   4650    JArray<TYPE> as TYPE[].  */
   4651 
   4652 char *
   4653 java_demangle_v3 (const char *mangled)
   4654 {
   4655   size_t alc;
   4656 
   4657   return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
   4658 }
   4659 
   4660 int
   4661 java_demangle_v3_callback (const char *mangled,
   4662                            demangle_callbackref callback, void *opaque)
   4663 {
   4664   return d_demangle_callback (mangled,
   4665                               DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
   4666                               callback, opaque);
   4667 }
   4668 
   4669 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
   4670 
   4671 #ifndef IN_GLIBCPP_V3
   4672 
   4673 /* Demangle a string in order to find out whether it is a constructor
   4674    or destructor.  Return non-zero on success.  Set *CTOR_KIND and
   4675    *DTOR_KIND appropriately.  */
   4676 
   4677 static int
   4678 is_ctor_or_dtor (const char *mangled,
   4679                  enum gnu_v3_ctor_kinds *ctor_kind,
   4680                  enum gnu_v3_dtor_kinds *dtor_kind)
   4681 {
   4682   struct d_info di;
   4683   struct demangle_component *dc;
   4684   int ret;
   4685 
   4686   *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
   4687   *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
   4688 
   4689   cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
   4690 
   4691   {
   4692 #ifdef CP_DYNAMIC_ARRAYS
   4693     __extension__ struct demangle_component comps[di.num_comps];
   4694     __extension__ struct demangle_component *subs[di.num_subs];
   4695 
   4696     di.comps = comps;
   4697     di.subs = subs;
   4698 #else
   4699     di.comps = alloca (di.num_comps * sizeof (*di.comps));
   4700     di.subs = alloca (di.num_subs * sizeof (*di.subs));
   4701 #endif
   4702 
   4703     dc = cplus_demangle_mangled_name (&di, 1);
   4704 
   4705     /* Note that because we did not pass DMGL_PARAMS, we don't expect
   4706        to demangle the entire string.  */
   4707 
   4708     ret = 0;
   4709     while (dc != NULL)
   4710       {
   4711 	switch (dc->type)
   4712 	  {
   4713 	  default:
   4714 	    dc = NULL;
   4715 	    break;
   4716 	  case DEMANGLE_COMPONENT_TYPED_NAME:
   4717 	  case DEMANGLE_COMPONENT_TEMPLATE:
   4718 	  case DEMANGLE_COMPONENT_RESTRICT_THIS:
   4719 	  case DEMANGLE_COMPONENT_VOLATILE_THIS:
   4720 	  case DEMANGLE_COMPONENT_CONST_THIS:
   4721 	    dc = d_left (dc);
   4722 	    break;
   4723 	  case DEMANGLE_COMPONENT_QUAL_NAME:
   4724 	  case DEMANGLE_COMPONENT_LOCAL_NAME:
   4725 	    dc = d_right (dc);
   4726 	    break;
   4727 	  case DEMANGLE_COMPONENT_CTOR:
   4728 	    *ctor_kind = dc->u.s_ctor.kind;
   4729 	    ret = 1;
   4730 	    dc = NULL;
   4731 	    break;
   4732 	  case DEMANGLE_COMPONENT_DTOR:
   4733 	    *dtor_kind = dc->u.s_dtor.kind;
   4734 	    ret = 1;
   4735 	    dc = NULL;
   4736 	    break;
   4737 	  }
   4738       }
   4739   }
   4740 
   4741   return ret;
   4742 }
   4743 
   4744 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
   4745    name.  A non-zero return indicates the type of constructor.  */
   4746 
   4747 enum gnu_v3_ctor_kinds
   4748 is_gnu_v3_mangled_ctor (const char *name)
   4749 {
   4750   enum gnu_v3_ctor_kinds ctor_kind;
   4751   enum gnu_v3_dtor_kinds dtor_kind;
   4752 
   4753   if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
   4754     return (enum gnu_v3_ctor_kinds) 0;
   4755   return ctor_kind;
   4756 }
   4757 
   4758 
   4759 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
   4760    name.  A non-zero return indicates the type of destructor.  */
   4761 
   4762 enum gnu_v3_dtor_kinds
   4763 is_gnu_v3_mangled_dtor (const char *name)
   4764 {
   4765   enum gnu_v3_ctor_kinds ctor_kind;
   4766   enum gnu_v3_dtor_kinds dtor_kind;
   4767 
   4768   if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
   4769     return (enum gnu_v3_dtor_kinds) 0;
   4770   return dtor_kind;
   4771 }
   4772 
   4773 #endif /* IN_GLIBCPP_V3 */
   4774 
   4775 #ifdef STANDALONE_DEMANGLER
   4776 
   4777 #if 0 /* in valgrind */
   4778 #include "getopt.h"
   4779 #include "dyn-string.h"
   4780 #endif /* ! in valgrind */
   4781 
   4782 static void print_usage (FILE* fp, int exit_value);
   4783 
   4784 #define IS_ALPHA(CHAR)                                                  \
   4785   (((CHAR) >= 'a' && (CHAR) <= 'z')                                     \
   4786    || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
   4787 
   4788 /* Non-zero if CHAR is a character than can occur in a mangled name.  */
   4789 #define is_mangled_char(CHAR)                                           \
   4790   (IS_ALPHA (CHAR) || IS_DIGIT (CHAR)                                   \
   4791    || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
   4792 
   4793 /* The name of this program, as invoked.  */
   4794 const char* program_name;
   4795 
   4796 /* Prints usage summary to FP and then exits with EXIT_VALUE.  */
   4797 
   4798 static void
   4799 print_usage (FILE* fp, int exit_value)
   4800 {
   4801   fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
   4802   fprintf (fp, "Options:\n");
   4803   fprintf (fp, "  -h,--help       Display this message.\n");
   4804   fprintf (fp, "  -p,--no-params  Don't display function parameters\n");
   4805   fprintf (fp, "  -v,--verbose    Produce verbose demanglings.\n");
   4806   fprintf (fp, "If names are provided, they are demangled.  Otherwise filters standard input.\n");
   4807 
   4808   exit (exit_value);
   4809 }
   4810 
   4811 /* Option specification for getopt_long.  */
   4812 static const struct option long_options[] =
   4813 {
   4814   { "help",	 no_argument, NULL, 'h' },
   4815   { "no-params", no_argument, NULL, 'p' },
   4816   { "verbose",   no_argument, NULL, 'v' },
   4817   { NULL,        no_argument, NULL, 0   },
   4818 };
   4819 
   4820 /* Main entry for a demangling filter executable.  It will demangle
   4821    its command line arguments, if any.  If none are provided, it will
   4822    filter stdin to stdout, replacing any recognized mangled C++ names
   4823    with their demangled equivalents.  */
   4824 
   4825 int
   4826 main (int argc, char *argv[])
   4827 {
   4828   int i;
   4829   int opt_char;
   4830   int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
   4831 
   4832   /* Use the program name of this program, as invoked.  */
   4833   program_name = argv[0];
   4834 
   4835   /* Parse options.  */
   4836   do
   4837     {
   4838       opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
   4839       switch (opt_char)
   4840 	{
   4841 	case '?':  /* Unrecognized option.  */
   4842 	  print_usage (stderr, 1);
   4843 	  break;
   4844 
   4845 	case 'h':
   4846 	  print_usage (stdout, 0);
   4847 	  break;
   4848 
   4849 	case 'p':
   4850 	  options &= ~ DMGL_PARAMS;
   4851 	  break;
   4852 
   4853 	case 'v':
   4854 	  options |= DMGL_VERBOSE;
   4855 	  break;
   4856 	}
   4857     }
   4858   while (opt_char != -1);
   4859 
   4860   if (optind == argc)
   4861     /* No command line arguments were provided.  Filter stdin.  */
   4862     {
   4863       dyn_string_t mangled = dyn_string_new (3);
   4864       char *s;
   4865 
   4866       /* Read all of input.  */
   4867       while (!feof (stdin))
   4868 	{
   4869 	  char c;
   4870 
   4871 	  /* Pile characters into mangled until we hit one that can't
   4872 	     occur in a mangled name.  */
   4873 	  c = getchar ();
   4874 	  while (!feof (stdin) && is_mangled_char (c))
   4875 	    {
   4876 	      dyn_string_append_char (mangled, c);
   4877 	      if (feof (stdin))
   4878 		break;
   4879 	      c = getchar ();
   4880 	    }
   4881 
   4882 	  if (dyn_string_length (mangled) > 0)
   4883 	    {
   4884 #ifdef IN_GLIBCPP_V3
   4885 	      s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
   4886 #else
   4887 	      s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
   4888 #endif
   4889 
   4890 	      if (s != NULL)
   4891 		{
   4892 		  fputs (s, stdout);
   4893 		  free (s);
   4894 		}
   4895 	      else
   4896 		{
   4897 		  /* It might not have been a mangled name.  Print the
   4898 		     original text.  */
   4899 		  fputs (dyn_string_buf (mangled), stdout);
   4900 		}
   4901 
   4902 	      dyn_string_clear (mangled);
   4903 	    }
   4904 
   4905 	  /* If we haven't hit EOF yet, we've read one character that
   4906 	     can't occur in a mangled name, so print it out.  */
   4907 	  if (!feof (stdin))
   4908 	    putchar (c);
   4909 	}
   4910 
   4911       dyn_string_delete (mangled);
   4912     }
   4913   else
   4914     /* Demangle command line arguments.  */
   4915     {
   4916       /* Loop over command line arguments.  */
   4917       for (i = optind; i < argc; ++i)
   4918 	{
   4919 	  char *s;
   4920 #ifdef IN_GLIBCPP_V3
   4921 	  int status;
   4922 #endif
   4923 
   4924 	  /* Attempt to demangle.  */
   4925 #ifdef IN_GLIBCPP_V3
   4926 	  s = __cxa_demangle (argv[i], NULL, NULL, &status);
   4927 #else
   4928 	  s = cplus_demangle_v3 (argv[i], options);
   4929 #endif
   4930 
   4931 	  /* If it worked, print the demangled name.  */
   4932 	  if (s != NULL)
   4933 	    {
   4934 	      printf ("%s\n", s);
   4935 	      free (s);
   4936 	    }
   4937 	  else
   4938 	    {
   4939 #ifdef IN_GLIBCPP_V3
   4940 	      fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
   4941 #else
   4942 	      fprintf (stderr, "Failed: %s\n", argv[i]);
   4943 #endif
   4944 	    }
   4945 	}
   4946     }
   4947 
   4948   return 0;
   4949 }
   4950 
   4951 #endif /* STANDALONE_DEMANGLER */
   4952