Home | History | Annotate | Download | only in m_demangle
      1 /* Demangler for g++ V3 ABI.
      2    Copyright (C) 2003-2017 Free Software Foundation, Inc.
      3    Written by Ian Lance Taylor <ian (at) wasabisystems.com>.
      4 
      5    This file is part of the libiberty library, which is part of GCC.
      6 
      7    This file is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 2 of the License, or
     10    (at your option) any later version.
     11 
     12    In addition to the permissions in the GNU General Public License, the
     13    Free Software Foundation gives you unlimited permission to link the
     14    compiled version of this file into combinations with other programs,
     15    and to distribute those combinations without any restriction coming
     16    from the use of this file.  (The General Public License restrictions
     17    do apply in other respects; for example, they cover modification of
     18    the file, and distribution when not linked into a combined
     19    executable.)
     20 
     21    This program is distributed in the hope that it will be useful,
     22    but WITHOUT ANY WARRANTY; without even the implied warranty of
     23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     24    GNU General Public License for more details.
     25 
     26    You should have received a copy of the GNU General Public License
     27    along with this program; if not, write to the Free Software
     28    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
     29 */
     30 
     31 /* This code implements a demangler for the g++ V3 ABI.  The ABI is
     32    described on this web page:
     33        http://www.codesourcery.com/cxx-abi/abi.html#mangling
     34 
     35    This code was written while looking at the demangler written by
     36    Alex Samuel <samuel (at) codesourcery.com>.
     37 
     38    This code first pulls the mangled name apart into a list of
     39    components, and then walks the list generating the demangled
     40    name.
     41 
     42    This file will normally define the following functions, q.v.:
     43       char *cplus_demangle_v3(const char *mangled, int options)
     44       char *java_demangle_v3(const char *mangled)
     45       int cplus_demangle_v3_callback(const char *mangled, int options,
     46                                      demangle_callbackref callback)
     47       int java_demangle_v3_callback(const char *mangled,
     48                                     demangle_callbackref callback)
     49       enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
     50       enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
     51 
     52    Also, the interface to the component list is public, and defined in
     53    demangle.h.  The interface consists of these types, which are
     54    defined in demangle.h:
     55       enum demangle_component_type
     56       struct demangle_component
     57       demangle_callbackref
     58    and these functions defined in this file:
     59       cplus_demangle_fill_name
     60       cplus_demangle_fill_extended_operator
     61       cplus_demangle_fill_ctor
     62       cplus_demangle_fill_dtor
     63       cplus_demangle_print
     64       cplus_demangle_print_callback
     65    and other functions defined in the file cp-demint.c.
     66 
     67    This file also defines some other functions and variables which are
     68    only to be used by the file cp-demint.c.
     69 
     70    Preprocessor macros you can define while compiling this file:
     71 
     72    IN_LIBGCC2
     73       If defined, this file defines the following functions, q.v.:
     74          char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
     75                                int *status)
     76          int __gcclibcxx_demangle_callback (const char *,
     77                                             void (*)
     78                                               (const char *, size_t, void *),
     79                                             void *)
     80       instead of cplus_demangle_v3[_callback]() and
     81       java_demangle_v3[_callback]().
     82 
     83    IN_GLIBCPP_V3
     84       If defined, this file defines only __cxa_demangle() and
     85       __gcclibcxx_demangle_callback(), and no other publically visible
     86       functions or variables.
     87 
     88    STANDALONE_DEMANGLER
     89       If defined, this file defines a main() function which demangles
     90       any arguments, or, if none, demangles stdin.
     91 
     92    CP_DEMANGLE_DEBUG
     93       If defined, turns on debugging mode, which prints information on
     94       stdout about the mangled string.  This is not generally useful.
     95 
     96    CHECK_DEMANGLER
     97       If defined, additional sanity checks will be performed.  It will
     98       cause some slowdown, but will allow to catch out-of-bound access
     99       errors earlier.  This macro is intended for testing and debugging.  */
    100 
    101 #if 0 /* in valgrind */
    102 #if defined (_AIX) && !defined (__GNUC__)
    103  #pragma alloca
    104 #endif
    105 #endif /* ! in valgrind */
    106 
    107 #if 0 /* in valgrind */
    108 #ifdef HAVE_CONFIG_H
    109 #include "config.h"
    110 #endif
    111 #endif /* ! in valgrind */
    112 
    113 #if 0 /* in valgrind */
    114 #include <stdio.h>
    115 #endif /* ! in valgrind */
    116 
    117 #if 0 /* in valgrind */
    118 #ifdef HAVE_STDLIB_H
    119 #include <stdlib.h>
    120 #endif
    121 #ifdef HAVE_STRING_H
    122 #include <string.h>
    123 #endif
    124 #endif /* ! in valgrind */
    125 
    126 #if 0 /* in valgrind */
    127 #ifdef HAVE_ALLOCA_H
    128 # include <alloca.h>
    129 #else
    130 # ifndef alloca
    131 #  ifdef __GNUC__
    132 #   define alloca __builtin_alloca
    133 #  else
    134 extern char *alloca ();
    135 #  endif /* __GNUC__ */
    136 # endif /* alloca */
    137 #endif /* HAVE_ALLOCA_H */
    138 #endif /* ! in valgrind */
    139 
    140 #if 0 /* in valgrind */
    141 #ifdef HAVE_LIMITS_H
    142 #include <limits.h>
    143 #endif
    144 #endif /* ! in valgrind */
    145 #ifndef INT_MAX
    146 # define INT_MAX       (int)(((unsigned int) ~0) >> 1)          /* 0x7FFFFFFF */
    147 #endif
    148 
    149 #if 0 /* in valgrind */
    150 #include "ansidecl.h"
    151 #include "libiberty.h"
    152 #endif /* ! in valgrind */
    153 
    154 #include "vg_libciface.h"
    155 
    156 #include "demangle.h"
    157 #include "cp-demangle.h"
    158 
    159 /* If IN_GLIBCPP_V3 is defined, some functions are made static.  We
    160    also rename them via #define to avoid compiler errors when the
    161    static definition conflicts with the extern declaration in a header
    162    file.  */
    163 #ifdef IN_GLIBCPP_V3
    164 
    165 #define CP_STATIC_IF_GLIBCPP_V3 static
    166 
    167 #define cplus_demangle_fill_name d_fill_name
    168 static int d_fill_name (struct demangle_component *, const char *, int);
    169 
    170 #define cplus_demangle_fill_extended_operator d_fill_extended_operator
    171 static int
    172 d_fill_extended_operator (struct demangle_component *, int,
    173                           struct demangle_component *);
    174 
    175 #define cplus_demangle_fill_ctor d_fill_ctor
    176 static int
    177 d_fill_ctor (struct demangle_component *, enum gnu_v3_ctor_kinds,
    178              struct demangle_component *);
    179 
    180 #define cplus_demangle_fill_dtor d_fill_dtor
    181 static int
    182 d_fill_dtor (struct demangle_component *, enum gnu_v3_dtor_kinds,
    183              struct demangle_component *);
    184 
    185 #define cplus_demangle_mangled_name d_mangled_name
    186 static struct demangle_component *d_mangled_name (struct d_info *, int);
    187 
    188 #define cplus_demangle_type d_type
    189 static struct demangle_component *d_type (struct d_info *);
    190 
    191 #define cplus_demangle_print d_print
    192 static char *d_print (int, struct demangle_component *, int, size_t *);
    193 
    194 #define cplus_demangle_print_callback d_print_callback
    195 static int d_print_callback (int, struct demangle_component *,
    196                              demangle_callbackref, void *);
    197 
    198 #define cplus_demangle_init_info d_init_info
    199 static void d_init_info (const char *, int, size_t, struct d_info *);
    200 
    201 #else /* ! defined(IN_GLIBCPP_V3) */
    202 #define CP_STATIC_IF_GLIBCPP_V3
    203 #endif /* ! defined(IN_GLIBCPP_V3) */
    204 
    205 /* See if the compiler supports dynamic arrays.  */
    206 
    207 #ifdef __GNUC__
    208 #define CP_DYNAMIC_ARRAYS
    209 #else
    210 #ifdef __STDC__
    211 #ifdef __STDC_VERSION__
    212 #if __STDC_VERSION__ >= 199901L
    213 #define CP_DYNAMIC_ARRAYS
    214 #endif /* __STDC__VERSION >= 199901L */
    215 #endif /* defined (__STDC_VERSION__) */
    216 #endif /* defined (__STDC__) */
    217 #endif /* ! defined (__GNUC__) */
    218 
    219 /* We avoid pulling in the ctype tables, to prevent pulling in
    220    additional unresolved symbols when this code is used in a library.
    221    FIXME: Is this really a valid reason?  This comes from the original
    222    V3 demangler code.
    223 
    224    As of this writing this file has the following undefined references
    225    when compiled with -DIN_GLIBCPP_V3: realloc, free, memcpy, strcpy,
    226    strcat, strlen.  */
    227 
    228 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
    229 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
    230 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
    231 
    232 /* The prefix prepended by GCC to an identifier represnting the
    233    anonymous namespace.  */
    234 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
    235 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
    236   (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
    237 
    238 /* Information we keep for the standard substitutions.  */
    239 
    240 struct d_standard_sub_info
    241 {
    242   /* The code for this substitution.  */
    243   char code;
    244   /* The simple string it expands to.  */
    245   const char *simple_expansion;
    246   /* The length of the simple expansion.  */
    247   int simple_len;
    248   /* The results of a full, verbose, expansion.  This is used when
    249      qualifying a constructor/destructor, or when in verbose mode.  */
    250   const char *full_expansion;
    251   /* The length of the full expansion.  */
    252   int full_len;
    253   /* What to set the last_name field of d_info to; NULL if we should
    254      not set it.  This is only relevant when qualifying a
    255      constructor/destructor.  */
    256   const char *set_last_name;
    257   /* The length of set_last_name.  */
    258   int set_last_name_len;
    259 };
    260 
    261 /* Accessors for subtrees of struct demangle_component.  */
    262 
    263 #define d_left(dc) ((dc)->u.s_binary.left)
    264 #define d_right(dc) ((dc)->u.s_binary.right)
    265 
    266 /* A list of templates.  This is used while printing.  */
    267 
    268 struct d_print_template
    269 {
    270   /* Next template on the list.  */
    271   struct d_print_template *next;
    272   /* This template.  */
    273   const struct demangle_component *template_decl;
    274 };
    275 
    276 /* A list of type modifiers.  This is used while printing.  */
    277 
    278 struct d_print_mod
    279 {
    280   /* Next modifier on the list.  These are in the reverse of the order
    281      in which they appeared in the mangled string.  */
    282   struct d_print_mod *next;
    283   /* The modifier.  */
    284   struct demangle_component *mod;
    285   /* Whether this modifier was printed.  */
    286   int printed;
    287   /* The list of templates which applies to this modifier.  */
    288   struct d_print_template *templates;
    289 };
    290 
    291 /* We use these structures to hold information during printing.  */
    292 
    293 struct d_growable_string
    294 {
    295   /* Buffer holding the result.  */
    296   char *buf;
    297   /* Current length of data in buffer.  */
    298   size_t len;
    299   /* Allocated size of buffer.  */
    300   size_t alc;
    301   /* Set to 1 if we had a memory allocation failure.  */
    302   int allocation_failure;
    303 };
    304 
    305 /* Stack of components, innermost first, used to avoid loops.  */
    306 
    307 struct d_component_stack
    308 {
    309   /* This component.  */
    310   const struct demangle_component *dc;
    311   /* This component's parent.  */
    312   const struct d_component_stack *parent;
    313 };
    314 
    315 /* A demangle component and some scope captured when it was first
    316    traversed.  */
    317 
    318 struct d_saved_scope
    319 {
    320   /* The component whose scope this is.  */
    321   const struct demangle_component *container;
    322   /* The list of templates, if any, that was current when this
    323      scope was captured.  */
    324   struct d_print_template *templates;
    325 };
    326 
    327 /* Checkpoint structure to allow backtracking.  This holds copies
    328    of the fields of struct d_info that need to be restored
    329    if a trial parse needs to be backtracked over.  */
    330 
    331 struct d_info_checkpoint
    332 {
    333   const char *n;
    334   int next_comp;
    335   int next_sub;
    336   int did_subs;
    337   int expansion;
    338 };
    339 
    340 enum { D_PRINT_BUFFER_LENGTH = 256 };
    341 struct d_print_info
    342 {
    343   /* Fixed-length allocated buffer for demangled data, flushed to the
    344      callback with a NUL termination once full.  */
    345   char buf[D_PRINT_BUFFER_LENGTH];
    346   /* Current length of data in buffer.  */
    347   size_t len;
    348   /* The last character printed, saved individually so that it survives
    349      any buffer flush.  */
    350   char last_char;
    351   /* Callback function to handle demangled buffer flush.  */
    352   demangle_callbackref callback;
    353   /* Opaque callback argument.  */
    354   void *opaque;
    355   /* The current list of templates, if any.  */
    356   struct d_print_template *templates;
    357   /* The current list of modifiers (e.g., pointer, reference, etc.),
    358      if any.  */
    359   struct d_print_mod *modifiers;
    360   /* Set to 1 if we saw a demangling error.  */
    361   int demangle_failure;
    362   /* Non-zero if we're printing a lambda argument.  A template
    363      parameter reference actually means 'auto'.  */
    364   int is_lambda_arg;
    365   /* The current index into any template argument packs we are using
    366      for printing, or -1 to print the whole pack.  */
    367   int pack_index;
    368   /* Number of d_print_flush calls so far.  */
    369   unsigned long int flush_count;
    370   /* Stack of components, innermost first, used to avoid loops.  */
    371   const struct d_component_stack *component_stack;
    372   /* Array of saved scopes for evaluating substitutions.  */
    373   struct d_saved_scope *saved_scopes;
    374   /* Index of the next unused saved scope in the above array.  */
    375   int next_saved_scope;
    376   /* Number of saved scopes in the above array.  */
    377   int num_saved_scopes;
    378   /* Array of templates for saving into scopes.  */
    379   struct d_print_template *copy_templates;
    380   /* Index of the next unused copy template in the above array.  */
    381   int next_copy_template;
    382   /* Number of copy templates in the above array.  */
    383   int num_copy_templates;
    384   /* The nearest enclosing template, if any.  */
    385   const struct demangle_component *current_template;
    386 };
    387 
    388 #ifdef CP_DEMANGLE_DEBUG
    389 static void d_dump (struct demangle_component *, int);
    390 #endif
    391 
    392 static struct demangle_component *
    393 d_make_empty (struct d_info *);
    394 
    395 static struct demangle_component *
    396 d_make_comp (struct d_info *, enum demangle_component_type,
    397              struct demangle_component *,
    398              struct demangle_component *);
    399 
    400 static struct demangle_component *
    401 d_make_name (struct d_info *, const char *, int);
    402 
    403 static struct demangle_component *
    404 d_make_demangle_mangled_name (struct d_info *, const char *);
    405 
    406 static struct demangle_component *
    407 d_make_builtin_type (struct d_info *,
    408                      const struct demangle_builtin_type_info *);
    409 
    410 static struct demangle_component *
    411 d_make_operator (struct d_info *,
    412                  const struct demangle_operator_info *);
    413 
    414 static struct demangle_component *
    415 d_make_extended_operator (struct d_info *, int,
    416                           struct demangle_component *);
    417 
    418 static struct demangle_component *
    419 d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
    420              struct demangle_component *);
    421 
    422 static struct demangle_component *
    423 d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
    424              struct demangle_component *);
    425 
    426 static struct demangle_component *
    427 d_make_template_param (struct d_info *, int);
    428 
    429 static struct demangle_component *
    430 d_make_sub (struct d_info *, const char *, int);
    431 
    432 static int
    433 has_return_type (struct demangle_component *);
    434 
    435 static int
    436 is_ctor_dtor_or_conversion (struct demangle_component *);
    437 
    438 static struct demangle_component *d_encoding (struct d_info *, int);
    439 
    440 static struct demangle_component *d_name (struct d_info *);
    441 
    442 static struct demangle_component *d_nested_name (struct d_info *);
    443 
    444 static struct demangle_component *d_prefix (struct d_info *);
    445 
    446 static struct demangle_component *d_unqualified_name (struct d_info *);
    447 
    448 static struct demangle_component *d_source_name (struct d_info *);
    449 
    450 static int d_number (struct d_info *);
    451 
    452 static struct demangle_component *d_identifier (struct d_info *, int);
    453 
    454 static struct demangle_component *d_operator_name (struct d_info *);
    455 
    456 static struct demangle_component *d_special_name (struct d_info *);
    457 
    458 static struct demangle_component *d_parmlist (struct d_info *);
    459 
    460 static int d_call_offset (struct d_info *, int);
    461 
    462 static struct demangle_component *d_ctor_dtor_name (struct d_info *);
    463 
    464 static struct demangle_component **
    465 d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
    466 
    467 static struct demangle_component *
    468 d_ref_qualifier (struct d_info *, struct demangle_component *);
    469 
    470 static struct demangle_component *
    471 d_function_type (struct d_info *);
    472 
    473 static struct demangle_component *
    474 d_bare_function_type (struct d_info *, int);
    475 
    476 static struct demangle_component *
    477 d_class_enum_type (struct d_info *);
    478 
    479 static struct demangle_component *d_array_type (struct d_info *);
    480 
    481 static struct demangle_component *d_vector_type (struct d_info *);
    482 
    483 static struct demangle_component *
    484 d_pointer_to_member_type (struct d_info *);
    485 
    486 static struct demangle_component *
    487 d_template_param (struct d_info *);
    488 
    489 static struct demangle_component *d_template_args (struct d_info *);
    490 static struct demangle_component *d_template_args_1 (struct d_info *);
    491 
    492 static struct demangle_component *
    493 d_template_arg (struct d_info *);
    494 
    495 static struct demangle_component *d_expression (struct d_info *);
    496 
    497 static struct demangle_component *d_expr_primary (struct d_info *);
    498 
    499 static struct demangle_component *d_local_name (struct d_info *);
    500 
    501 static int d_discriminator (struct d_info *);
    502 
    503 static struct demangle_component *d_lambda (struct d_info *);
    504 
    505 static struct demangle_component *d_unnamed_type (struct d_info *);
    506 
    507 static struct demangle_component *
    508 d_clone_suffix (struct d_info *, struct demangle_component *);
    509 
    510 static int
    511 d_add_substitution (struct d_info *, struct demangle_component *);
    512 
    513 static struct demangle_component *d_substitution (struct d_info *, int);
    514 
    515 static void d_checkpoint (struct d_info *, struct d_info_checkpoint *);
    516 
    517 static void d_backtrack (struct d_info *, struct d_info_checkpoint *);
    518 
    519 static void d_growable_string_init (struct d_growable_string *, size_t);
    520 
    521 static inline void
    522 d_growable_string_resize (struct d_growable_string *, size_t);
    523 
    524 static inline void
    525 d_growable_string_append_buffer (struct d_growable_string *,
    526                                  const char *, size_t);
    527 static void
    528 d_growable_string_callback_adapter (const char *, size_t, void *);
    529 
    530 static void
    531 d_print_init (struct d_print_info *, demangle_callbackref, void *,
    532 	      const struct demangle_component *);
    533 
    534 static inline void d_print_error (struct d_print_info *);
    535 
    536 static inline int d_print_saw_error (struct d_print_info *);
    537 
    538 static inline void d_print_flush (struct d_print_info *);
    539 
    540 static inline void d_append_char (struct d_print_info *, char);
    541 
    542 static inline void d_append_buffer (struct d_print_info *,
    543                                     const char *, size_t);
    544 
    545 static inline void d_append_string (struct d_print_info *, const char *);
    546 
    547 static inline char d_last_char (struct d_print_info *);
    548 
    549 static void
    550 d_print_comp (struct d_print_info *, int, struct demangle_component *);
    551 
    552 static void
    553 d_print_java_identifier (struct d_print_info *, const char *, int);
    554 
    555 static void
    556 d_print_mod_list (struct d_print_info *, int, struct d_print_mod *, int);
    557 
    558 static void
    559 d_print_mod (struct d_print_info *, int, struct demangle_component *);
    560 
    561 static void
    562 d_print_function_type (struct d_print_info *, int,
    563                        struct demangle_component *,
    564                        struct d_print_mod *);
    565 
    566 static void
    567 d_print_array_type (struct d_print_info *, int,
    568                     struct demangle_component *,
    569                     struct d_print_mod *);
    570 
    571 static void
    572 d_print_expr_op (struct d_print_info *, int, struct demangle_component *);
    573 
    574 static void d_print_cast (struct d_print_info *, int,
    575 			  struct demangle_component *);
    576 static void d_print_conversion (struct d_print_info *, int,
    577 				struct demangle_component *);
    578 
    579 static int d_demangle_callback (const char *, int,
    580                                 demangle_callbackref, void *);
    581 static char *d_demangle (const char *, int, size_t *);
    582 
    583 /* True iff TYPE is a demangling component representing a
    584    function-type-qualifier.  */
    585 
    586 static int
    587 is_fnqual_component_type (enum demangle_component_type type)
    588 {
    589   return (type == DEMANGLE_COMPONENT_RESTRICT_THIS
    590 	  || type == DEMANGLE_COMPONENT_VOLATILE_THIS
    591 	  || type == DEMANGLE_COMPONENT_CONST_THIS
    592 	  || type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
    593 	  || type == DEMANGLE_COMPONENT_TRANSACTION_SAFE
    594 	  || type == DEMANGLE_COMPONENT_NOEXCEPT
    595 	  || type == DEMANGLE_COMPONENT_THROW_SPEC
    596 	  || type == DEMANGLE_COMPONENT_REFERENCE_THIS);
    597 }
    598 
    599 #define FNQUAL_COMPONENT_CASE				\
    600     case DEMANGLE_COMPONENT_RESTRICT_THIS:		\
    601     case DEMANGLE_COMPONENT_VOLATILE_THIS:		\
    602     case DEMANGLE_COMPONENT_CONST_THIS:			\
    603     case DEMANGLE_COMPONENT_REFERENCE_THIS:		\
    604     case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:	\
    605     case DEMANGLE_COMPONENT_TRANSACTION_SAFE:		\
    606     case DEMANGLE_COMPONENT_NOEXCEPT:			\
    607     case DEMANGLE_COMPONENT_THROW_SPEC
    608 
    609 #ifdef CP_DEMANGLE_DEBUG
    610 
    611 static void
    612 d_dump (struct demangle_component *dc, int indent)
    613 {
    614   int i;
    615 
    616   if (dc == NULL)
    617     {
    618       if (indent == 0)
    619         printf ("failed demangling\n");
    620       return;
    621     }
    622 
    623   for (i = 0; i < indent; ++i)
    624     putchar (' ');
    625 
    626   switch (dc->type)
    627     {
    628     case DEMANGLE_COMPONENT_NAME:
    629       printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
    630       return;
    631     case DEMANGLE_COMPONENT_TAGGED_NAME:
    632       printf ("tagged name\n");
    633       d_dump (dc->u.s_binary.left, indent + 2);
    634       d_dump (dc->u.s_binary.right, indent + 2);
    635       return;
    636     case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
    637       printf ("template parameter %ld\n", dc->u.s_number.number);
    638       return;
    639     case DEMANGLE_COMPONENT_FUNCTION_PARAM:
    640       printf ("function parameter %ld\n", dc->u.s_number.number);
    641       return;
    642     case DEMANGLE_COMPONENT_CTOR:
    643       printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
    644       d_dump (dc->u.s_ctor.name, indent + 2);
    645       return;
    646     case DEMANGLE_COMPONENT_DTOR:
    647       printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
    648       d_dump (dc->u.s_dtor.name, indent + 2);
    649       return;
    650     case DEMANGLE_COMPONENT_SUB_STD:
    651       printf ("standard substitution %s\n", dc->u.s_string.string);
    652       return;
    653     case DEMANGLE_COMPONENT_BUILTIN_TYPE:
    654       printf ("builtin type %s\n", dc->u.s_builtin.type->name);
    655       return;
    656     case DEMANGLE_COMPONENT_OPERATOR:
    657       printf ("operator %s\n", dc->u.s_operator.op->name);
    658       return;
    659     case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
    660       printf ("extended operator with %d args\n",
    661 	      dc->u.s_extended_operator.args);
    662       d_dump (dc->u.s_extended_operator.name, indent + 2);
    663       return;
    664 
    665     case DEMANGLE_COMPONENT_QUAL_NAME:
    666       printf ("qualified name\n");
    667       break;
    668     case DEMANGLE_COMPONENT_LOCAL_NAME:
    669       printf ("local name\n");
    670       break;
    671     case DEMANGLE_COMPONENT_TYPED_NAME:
    672       printf ("typed name\n");
    673       break;
    674     case DEMANGLE_COMPONENT_TEMPLATE:
    675       printf ("template\n");
    676       break;
    677     case DEMANGLE_COMPONENT_VTABLE:
    678       printf ("vtable\n");
    679       break;
    680     case DEMANGLE_COMPONENT_VTT:
    681       printf ("VTT\n");
    682       break;
    683     case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
    684       printf ("construction vtable\n");
    685       break;
    686     case DEMANGLE_COMPONENT_TYPEINFO:
    687       printf ("typeinfo\n");
    688       break;
    689     case DEMANGLE_COMPONENT_TYPEINFO_NAME:
    690       printf ("typeinfo name\n");
    691       break;
    692     case DEMANGLE_COMPONENT_TYPEINFO_FN:
    693       printf ("typeinfo function\n");
    694       break;
    695     case DEMANGLE_COMPONENT_THUNK:
    696       printf ("thunk\n");
    697       break;
    698     case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
    699       printf ("virtual thunk\n");
    700       break;
    701     case DEMANGLE_COMPONENT_COVARIANT_THUNK:
    702       printf ("covariant thunk\n");
    703       break;
    704     case DEMANGLE_COMPONENT_JAVA_CLASS:
    705       printf ("java class\n");
    706       break;
    707     case DEMANGLE_COMPONENT_GUARD:
    708       printf ("guard\n");
    709       break;
    710     case DEMANGLE_COMPONENT_REFTEMP:
    711       printf ("reference temporary\n");
    712       break;
    713     case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
    714       printf ("hidden alias\n");
    715       break;
    716     case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
    717       printf ("transaction clone\n");
    718       break;
    719     case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
    720       printf ("non-transaction clone\n");
    721       break;
    722     case DEMANGLE_COMPONENT_RESTRICT:
    723       printf ("restrict\n");
    724       break;
    725     case DEMANGLE_COMPONENT_VOLATILE:
    726       printf ("volatile\n");
    727       break;
    728     case DEMANGLE_COMPONENT_CONST:
    729       printf ("const\n");
    730       break;
    731     case DEMANGLE_COMPONENT_RESTRICT_THIS:
    732       printf ("restrict this\n");
    733       break;
    734     case DEMANGLE_COMPONENT_VOLATILE_THIS:
    735       printf ("volatile this\n");
    736       break;
    737     case DEMANGLE_COMPONENT_CONST_THIS:
    738       printf ("const this\n");
    739       break;
    740     case DEMANGLE_COMPONENT_REFERENCE_THIS:
    741       printf ("reference this\n");
    742       break;
    743     case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
    744       printf ("rvalue reference this\n");
    745       break;
    746     case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
    747       printf ("transaction_safe this\n");
    748       break;
    749     case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
    750       printf ("vendor type qualifier\n");
    751       break;
    752     case DEMANGLE_COMPONENT_POINTER:
    753       printf ("pointer\n");
    754       break;
    755     case DEMANGLE_COMPONENT_REFERENCE:
    756       printf ("reference\n");
    757       break;
    758     case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
    759       printf ("rvalue reference\n");
    760       break;
    761     case DEMANGLE_COMPONENT_COMPLEX:
    762       printf ("complex\n");
    763       break;
    764     case DEMANGLE_COMPONENT_IMAGINARY:
    765       printf ("imaginary\n");
    766       break;
    767     case DEMANGLE_COMPONENT_VENDOR_TYPE:
    768       printf ("vendor type\n");
    769       break;
    770     case DEMANGLE_COMPONENT_FUNCTION_TYPE:
    771       printf ("function type\n");
    772       break;
    773     case DEMANGLE_COMPONENT_ARRAY_TYPE:
    774       printf ("array type\n");
    775       break;
    776     case DEMANGLE_COMPONENT_PTRMEM_TYPE:
    777       printf ("pointer to member type\n");
    778       break;
    779     case DEMANGLE_COMPONENT_FIXED_TYPE:
    780       printf ("fixed-point type, accum? %d, sat? %d\n",
    781               dc->u.s_fixed.accum, dc->u.s_fixed.sat);
    782       d_dump (dc->u.s_fixed.length, indent + 2);
    783       break;
    784     case DEMANGLE_COMPONENT_ARGLIST:
    785       printf ("argument list\n");
    786       break;
    787     case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
    788       printf ("template argument list\n");
    789       break;
    790     case DEMANGLE_COMPONENT_INITIALIZER_LIST:
    791       printf ("initializer list\n");
    792       break;
    793     case DEMANGLE_COMPONENT_CAST:
    794       printf ("cast\n");
    795       break;
    796     case DEMANGLE_COMPONENT_CONVERSION:
    797       printf ("conversion operator\n");
    798       break;
    799     case DEMANGLE_COMPONENT_NULLARY:
    800       printf ("nullary operator\n");
    801       break;
    802     case DEMANGLE_COMPONENT_UNARY:
    803       printf ("unary operator\n");
    804       break;
    805     case DEMANGLE_COMPONENT_BINARY:
    806       printf ("binary operator\n");
    807       break;
    808     case DEMANGLE_COMPONENT_BINARY_ARGS:
    809       printf ("binary operator arguments\n");
    810       break;
    811     case DEMANGLE_COMPONENT_TRINARY:
    812       printf ("trinary operator\n");
    813       break;
    814     case DEMANGLE_COMPONENT_TRINARY_ARG1:
    815       printf ("trinary operator arguments 1\n");
    816       break;
    817     case DEMANGLE_COMPONENT_TRINARY_ARG2:
    818       printf ("trinary operator arguments 1\n");
    819       break;
    820     case DEMANGLE_COMPONENT_LITERAL:
    821       printf ("literal\n");
    822       break;
    823     case DEMANGLE_COMPONENT_LITERAL_NEG:
    824       printf ("negative literal\n");
    825       break;
    826     case DEMANGLE_COMPONENT_JAVA_RESOURCE:
    827       printf ("java resource\n");
    828       break;
    829     case DEMANGLE_COMPONENT_COMPOUND_NAME:
    830       printf ("compound name\n");
    831       break;
    832     case DEMANGLE_COMPONENT_CHARACTER:
    833       printf ("character '%c'\n",  dc->u.s_character.character);
    834       return;
    835     case DEMANGLE_COMPONENT_NUMBER:
    836       printf ("number %ld\n", dc->u.s_number.number);
    837       return;
    838     case DEMANGLE_COMPONENT_DECLTYPE:
    839       printf ("decltype\n");
    840       break;
    841     case DEMANGLE_COMPONENT_PACK_EXPANSION:
    842       printf ("pack expansion\n");
    843       break;
    844     case DEMANGLE_COMPONENT_TLS_INIT:
    845       printf ("tls init function\n");
    846       break;
    847     case DEMANGLE_COMPONENT_TLS_WRAPPER:
    848       printf ("tls wrapper function\n");
    849       break;
    850     case DEMANGLE_COMPONENT_DEFAULT_ARG:
    851       printf ("default argument %d\n", dc->u.s_unary_num.num);
    852       d_dump (dc->u.s_unary_num.sub, indent+2);
    853       return;
    854     case DEMANGLE_COMPONENT_LAMBDA:
    855       printf ("lambda %d\n", dc->u.s_unary_num.num);
    856       d_dump (dc->u.s_unary_num.sub, indent+2);
    857       return;
    858     }
    859 
    860   d_dump (d_left (dc), indent + 2);
    861   d_dump (d_right (dc), indent + 2);
    862 }
    863 
    864 #endif /* CP_DEMANGLE_DEBUG */
    865 
    866 /* Fill in a DEMANGLE_COMPONENT_NAME.  */
    867 
    868 CP_STATIC_IF_GLIBCPP_V3
    869 int
    870 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
    871 {
    872   if (p == NULL || s == NULL || len == 0)
    873     return 0;
    874   p->d_printing = 0;
    875   p->type = DEMANGLE_COMPONENT_NAME;
    876   p->u.s_name.s = s;
    877   p->u.s_name.len = len;
    878   return 1;
    879 }
    880 
    881 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR.  */
    882 
    883 CP_STATIC_IF_GLIBCPP_V3
    884 int
    885 cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
    886                                        struct demangle_component *name)
    887 {
    888   if (p == NULL || args < 0 || name == NULL)
    889     return 0;
    890   p->d_printing = 0;
    891   p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
    892   p->u.s_extended_operator.args = args;
    893   p->u.s_extended_operator.name = name;
    894   return 1;
    895 }
    896 
    897 /* Fill in a DEMANGLE_COMPONENT_CTOR.  */
    898 
    899 CP_STATIC_IF_GLIBCPP_V3
    900 int
    901 cplus_demangle_fill_ctor (struct demangle_component *p,
    902                           enum gnu_v3_ctor_kinds kind,
    903                           struct demangle_component *name)
    904 {
    905   if (p == NULL
    906       || name == NULL
    907       || (int) kind < gnu_v3_complete_object_ctor
    908       || (int) kind > gnu_v3_object_ctor_group)
    909     return 0;
    910   p->d_printing = 0;
    911   p->type = DEMANGLE_COMPONENT_CTOR;
    912   p->u.s_ctor.kind = kind;
    913   p->u.s_ctor.name = name;
    914   return 1;
    915 }
    916 
    917 /* Fill in a DEMANGLE_COMPONENT_DTOR.  */
    918 
    919 CP_STATIC_IF_GLIBCPP_V3
    920 int
    921 cplus_demangle_fill_dtor (struct demangle_component *p,
    922                           enum gnu_v3_dtor_kinds kind,
    923                           struct demangle_component *name)
    924 {
    925   if (p == NULL
    926       || name == NULL
    927       || (int) kind < gnu_v3_deleting_dtor
    928       || (int) kind > gnu_v3_object_dtor_group)
    929     return 0;
    930   p->d_printing = 0;
    931   p->type = DEMANGLE_COMPONENT_DTOR;
    932   p->u.s_dtor.kind = kind;
    933   p->u.s_dtor.name = name;
    934   return 1;
    935 }
    936 
    937 /* Add a new component.  */
    938 
    939 static struct demangle_component *
    940 d_make_empty (struct d_info *di)
    941 {
    942   struct demangle_component *p;
    943 
    944   if (di->next_comp >= di->num_comps)
    945     return NULL;
    946   p = &di->comps[di->next_comp];
    947   p->d_printing = 0;
    948   ++di->next_comp;
    949   return p;
    950 }
    951 
    952 /* Add a new generic component.  */
    953 
    954 static struct demangle_component *
    955 d_make_comp (struct d_info *di, enum demangle_component_type type,
    956              struct demangle_component *left,
    957              struct demangle_component *right)
    958 {
    959   struct demangle_component *p;
    960 
    961   /* We check for errors here.  A typical error would be a NULL return
    962      from a subroutine.  We catch those here, and return NULL
    963      upward.  */
    964   switch (type)
    965     {
    966       /* These types require two parameters.  */
    967     case DEMANGLE_COMPONENT_QUAL_NAME:
    968     case DEMANGLE_COMPONENT_LOCAL_NAME:
    969     case DEMANGLE_COMPONENT_TYPED_NAME:
    970     case DEMANGLE_COMPONENT_TAGGED_NAME:
    971     case DEMANGLE_COMPONENT_TEMPLATE:
    972     case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
    973     case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
    974     case DEMANGLE_COMPONENT_PTRMEM_TYPE:
    975     case DEMANGLE_COMPONENT_UNARY:
    976     case DEMANGLE_COMPONENT_BINARY:
    977     case DEMANGLE_COMPONENT_BINARY_ARGS:
    978     case DEMANGLE_COMPONENT_TRINARY:
    979     case DEMANGLE_COMPONENT_TRINARY_ARG1:
    980     case DEMANGLE_COMPONENT_LITERAL:
    981     case DEMANGLE_COMPONENT_LITERAL_NEG:
    982     case DEMANGLE_COMPONENT_COMPOUND_NAME:
    983     case DEMANGLE_COMPONENT_VECTOR_TYPE:
    984     case DEMANGLE_COMPONENT_CLONE:
    985       if (left == NULL || right == NULL)
    986 	return NULL;
    987       break;
    988 
    989       /* These types only require one parameter.  */
    990     case DEMANGLE_COMPONENT_VTABLE:
    991     case DEMANGLE_COMPONENT_VTT:
    992     case DEMANGLE_COMPONENT_TYPEINFO:
    993     case DEMANGLE_COMPONENT_TYPEINFO_NAME:
    994     case DEMANGLE_COMPONENT_TYPEINFO_FN:
    995     case DEMANGLE_COMPONENT_THUNK:
    996     case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
    997     case DEMANGLE_COMPONENT_COVARIANT_THUNK:
    998     case DEMANGLE_COMPONENT_JAVA_CLASS:
    999     case DEMANGLE_COMPONENT_GUARD:
   1000     case DEMANGLE_COMPONENT_TLS_INIT:
   1001     case DEMANGLE_COMPONENT_TLS_WRAPPER:
   1002     case DEMANGLE_COMPONENT_REFTEMP:
   1003     case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
   1004     case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
   1005     case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
   1006     case DEMANGLE_COMPONENT_POINTER:
   1007     case DEMANGLE_COMPONENT_REFERENCE:
   1008     case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
   1009     case DEMANGLE_COMPONENT_COMPLEX:
   1010     case DEMANGLE_COMPONENT_IMAGINARY:
   1011     case DEMANGLE_COMPONENT_VENDOR_TYPE:
   1012     case DEMANGLE_COMPONENT_CAST:
   1013     case DEMANGLE_COMPONENT_CONVERSION:
   1014     case DEMANGLE_COMPONENT_JAVA_RESOURCE:
   1015     case DEMANGLE_COMPONENT_DECLTYPE:
   1016     case DEMANGLE_COMPONENT_PACK_EXPANSION:
   1017     case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
   1018     case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
   1019     case DEMANGLE_COMPONENT_NULLARY:
   1020     case DEMANGLE_COMPONENT_TRINARY_ARG2:
   1021       if (left == NULL)
   1022 	return NULL;
   1023       break;
   1024 
   1025       /* This needs a right parameter, but the left parameter can be
   1026 	 empty.  */
   1027     case DEMANGLE_COMPONENT_ARRAY_TYPE:
   1028     case DEMANGLE_COMPONENT_INITIALIZER_LIST:
   1029       if (right == NULL)
   1030 	return NULL;
   1031       break;
   1032 
   1033       /* These are allowed to have no parameters--in some cases they
   1034 	 will be filled in later.  */
   1035     case DEMANGLE_COMPONENT_FUNCTION_TYPE:
   1036     case DEMANGLE_COMPONENT_RESTRICT:
   1037     case DEMANGLE_COMPONENT_VOLATILE:
   1038     case DEMANGLE_COMPONENT_CONST:
   1039     case DEMANGLE_COMPONENT_ARGLIST:
   1040     case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
   1041     FNQUAL_COMPONENT_CASE:
   1042       break;
   1043 
   1044       /* Other types should not be seen here.  */
   1045     default:
   1046       return NULL;
   1047     }
   1048 
   1049   p = d_make_empty (di);
   1050   if (p != NULL)
   1051     {
   1052       p->type = type;
   1053       p->u.s_binary.left = left;
   1054       p->u.s_binary.right = right;
   1055     }
   1056   return p;
   1057 }
   1058 
   1059 /* Add a new demangle mangled name component.  */
   1060 
   1061 static struct demangle_component *
   1062 d_make_demangle_mangled_name (struct d_info *di, const char *s)
   1063 {
   1064   if (d_peek_char (di) != '_' || d_peek_next_char (di) != 'Z')
   1065     return d_make_name (di, s, strlen (s));
   1066   d_advance (di, 2);
   1067   return d_encoding (di, 0);
   1068 }
   1069 
   1070 /* Add a new name component.  */
   1071 
   1072 static struct demangle_component *
   1073 d_make_name (struct d_info *di, const char *s, int len)
   1074 {
   1075   struct demangle_component *p;
   1076 
   1077   p = d_make_empty (di);
   1078   if (! cplus_demangle_fill_name (p, s, len))
   1079     return NULL;
   1080   return p;
   1081 }
   1082 
   1083 /* Add a new builtin type component.  */
   1084 
   1085 static struct demangle_component *
   1086 d_make_builtin_type (struct d_info *di,
   1087                      const struct demangle_builtin_type_info *type)
   1088 {
   1089   struct demangle_component *p;
   1090 
   1091   if (type == NULL)
   1092     return NULL;
   1093   p = d_make_empty (di);
   1094   if (p != NULL)
   1095     {
   1096       p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
   1097       p->u.s_builtin.type = type;
   1098     }
   1099   return p;
   1100 }
   1101 
   1102 /* Add a new operator component.  */
   1103 
   1104 static struct demangle_component *
   1105 d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
   1106 {
   1107   struct demangle_component *p;
   1108 
   1109   p = d_make_empty (di);
   1110   if (p != NULL)
   1111     {
   1112       p->type = DEMANGLE_COMPONENT_OPERATOR;
   1113       p->u.s_operator.op = op;
   1114     }
   1115   return p;
   1116 }
   1117 
   1118 /* Add a new extended operator component.  */
   1119 
   1120 static struct demangle_component *
   1121 d_make_extended_operator (struct d_info *di, int args,
   1122                           struct demangle_component *name)
   1123 {
   1124   struct demangle_component *p;
   1125 
   1126   p = d_make_empty (di);
   1127   if (! cplus_demangle_fill_extended_operator (p, args, name))
   1128     return NULL;
   1129   return p;
   1130 }
   1131 
   1132 static struct demangle_component *
   1133 d_make_default_arg (struct d_info *di, int num,
   1134 		    struct demangle_component *sub)
   1135 {
   1136   struct demangle_component *p = d_make_empty (di);
   1137   if (p)
   1138     {
   1139       p->type = DEMANGLE_COMPONENT_DEFAULT_ARG;
   1140       p->u.s_unary_num.num = num;
   1141       p->u.s_unary_num.sub = sub;
   1142     }
   1143   return p;
   1144 }
   1145 
   1146 /* Add a new constructor component.  */
   1147 
   1148 static struct demangle_component *
   1149 d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
   1150              struct demangle_component *name)
   1151 {
   1152   struct demangle_component *p;
   1153 
   1154   p = d_make_empty (di);
   1155   if (! cplus_demangle_fill_ctor (p, kind, name))
   1156     return NULL;
   1157   return p;
   1158 }
   1159 
   1160 /* Add a new destructor component.  */
   1161 
   1162 static struct demangle_component *
   1163 d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
   1164              struct demangle_component *name)
   1165 {
   1166   struct demangle_component *p;
   1167 
   1168   p = d_make_empty (di);
   1169   if (! cplus_demangle_fill_dtor (p, kind, name))
   1170     return NULL;
   1171   return p;
   1172 }
   1173 
   1174 /* Add a new template parameter.  */
   1175 
   1176 static struct demangle_component *
   1177 d_make_template_param (struct d_info *di, int i)
   1178 {
   1179   struct demangle_component *p;
   1180 
   1181   p = d_make_empty (di);
   1182   if (p != NULL)
   1183     {
   1184       p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
   1185       p->u.s_number.number = i;
   1186     }
   1187   return p;
   1188 }
   1189 
   1190 /* Add a new function parameter.  */
   1191 
   1192 static struct demangle_component *
   1193 d_make_function_param (struct d_info *di, int i)
   1194 {
   1195   struct demangle_component *p;
   1196 
   1197   p = d_make_empty (di);
   1198   if (p != NULL)
   1199     {
   1200       p->type = DEMANGLE_COMPONENT_FUNCTION_PARAM;
   1201       p->u.s_number.number = i;
   1202     }
   1203   return p;
   1204 }
   1205 
   1206 /* Add a new standard substitution component.  */
   1207 
   1208 static struct demangle_component *
   1209 d_make_sub (struct d_info *di, const char *name, int len)
   1210 {
   1211   struct demangle_component *p;
   1212 
   1213   p = d_make_empty (di);
   1214   if (p != NULL)
   1215     {
   1216       p->type = DEMANGLE_COMPONENT_SUB_STD;
   1217       p->u.s_string.string = name;
   1218       p->u.s_string.len = len;
   1219     }
   1220   return p;
   1221 }
   1222 
   1223 /* <mangled-name> ::= _Z <encoding> [<clone-suffix>]*
   1224 
   1225    TOP_LEVEL is non-zero when called at the top level.  */
   1226 
   1227 CP_STATIC_IF_GLIBCPP_V3
   1228 struct demangle_component *
   1229 cplus_demangle_mangled_name (struct d_info *di, int top_level)
   1230 {
   1231   struct demangle_component *p;
   1232 
   1233   if (! d_check_char (di, '_')
   1234       /* Allow missing _ if not at toplevel to work around a
   1235 	 bug in G++ abi-version=2 mangling; see the comment in
   1236 	 write_template_arg.  */
   1237       && top_level)
   1238     return NULL;
   1239   if (! d_check_char (di, 'Z'))
   1240     return NULL;
   1241   p = d_encoding (di, top_level);
   1242 
   1243   /* If at top level and parsing parameters, check for a clone
   1244      suffix.  */
   1245   if (top_level && (di->options & DMGL_PARAMS) != 0)
   1246     while (d_peek_char (di) == '.'
   1247 	   && (IS_LOWER (d_peek_next_char (di))
   1248 	       || d_peek_next_char (di) == '_'
   1249 	       || IS_DIGIT (d_peek_next_char (di))))
   1250       p = d_clone_suffix (di, p);
   1251 
   1252   return p;
   1253 }
   1254 
   1255 /* Return whether a function should have a return type.  The argument
   1256    is the function name, which may be qualified in various ways.  The
   1257    rules are that template functions have return types with some
   1258    exceptions, function types which are not part of a function name
   1259    mangling have return types with some exceptions, and non-template
   1260    function names do not have return types.  The exceptions are that
   1261    constructors, destructors, and conversion operators do not have
   1262    return types.  */
   1263 
   1264 static int
   1265 has_return_type (struct demangle_component *dc)
   1266 {
   1267   if (dc == NULL)
   1268     return 0;
   1269   switch (dc->type)
   1270     {
   1271     default:
   1272       return 0;
   1273     case DEMANGLE_COMPONENT_TEMPLATE:
   1274       return ! is_ctor_dtor_or_conversion (d_left (dc));
   1275     FNQUAL_COMPONENT_CASE:
   1276       return has_return_type (d_left (dc));
   1277     }
   1278 }
   1279 
   1280 /* Return whether a name is a constructor, a destructor, or a
   1281    conversion operator.  */
   1282 
   1283 static int
   1284 is_ctor_dtor_or_conversion (struct demangle_component *dc)
   1285 {
   1286   if (dc == NULL)
   1287     return 0;
   1288   switch (dc->type)
   1289     {
   1290     default:
   1291       return 0;
   1292     case DEMANGLE_COMPONENT_QUAL_NAME:
   1293     case DEMANGLE_COMPONENT_LOCAL_NAME:
   1294       return is_ctor_dtor_or_conversion (d_right (dc));
   1295     case DEMANGLE_COMPONENT_CTOR:
   1296     case DEMANGLE_COMPONENT_DTOR:
   1297     case DEMANGLE_COMPONENT_CONVERSION:
   1298       return 1;
   1299     }
   1300 }
   1301 
   1302 /* <encoding> ::= <(function) name> <bare-function-type>
   1303               ::= <(data) name>
   1304               ::= <special-name>
   1305 
   1306    TOP_LEVEL is non-zero when called at the top level, in which case
   1307    if DMGL_PARAMS is not set we do not demangle the function
   1308    parameters.  We only set this at the top level, because otherwise
   1309    we would not correctly demangle names in local scopes.  */
   1310 
   1311 static struct demangle_component *
   1312 d_encoding (struct d_info *di, int top_level)
   1313 {
   1314   char peek = d_peek_char (di);
   1315 
   1316   if (peek == 'G' || peek == 'T')
   1317     return d_special_name (di);
   1318   else
   1319     {
   1320       struct demangle_component *dc;
   1321 
   1322       dc = d_name (di);
   1323 
   1324       if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
   1325 	{
   1326 	  /* Strip off any initial CV-qualifiers, as they really apply
   1327 	     to the `this' parameter, and they were not output by the
   1328 	     v2 demangler without DMGL_PARAMS.  */
   1329 	  while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
   1330 		 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
   1331 		 || dc->type == DEMANGLE_COMPONENT_CONST_THIS
   1332 		 || dc->type == DEMANGLE_COMPONENT_REFERENCE_THIS
   1333 		 || dc->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS)
   1334 	    dc = d_left (dc);
   1335 
   1336 	  /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
   1337 	     there may be function-qualifiers on its right argument which
   1338 	     really apply here; this happens when parsing a class
   1339 	     which is local to a function.  */
   1340 	  if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
   1341 	    {
   1342 	      struct demangle_component *dcr;
   1343 
   1344 	      dcr = d_right (dc);
   1345 	      while (is_fnqual_component_type (dcr->type))
   1346 		dcr = d_left (dcr);
   1347 	      dc->u.s_binary.right = dcr;
   1348 	    }
   1349 
   1350 	  return dc;
   1351 	}
   1352 
   1353       peek = d_peek_char (di);
   1354       if (dc == NULL || peek == '\0' || peek == 'E')
   1355 	return dc;
   1356       return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
   1357 			  d_bare_function_type (di, has_return_type (dc)));
   1358     }
   1359 }
   1360 
   1361 /* <tagged-name> ::= <name> B <source-name> */
   1362 
   1363 static struct demangle_component *
   1364 d_abi_tags (struct d_info *di, struct demangle_component *dc)
   1365 {
   1366   struct demangle_component *hold_last_name;
   1367   char peek;
   1368 
   1369   /* Preserve the last name, so the ABI tag doesn't clobber it.  */
   1370   hold_last_name = di->last_name;
   1371 
   1372   while (peek = d_peek_char (di),
   1373 	 peek == 'B')
   1374     {
   1375       struct demangle_component *tag;
   1376       d_advance (di, 1);
   1377       tag = d_source_name (di);
   1378       dc = d_make_comp (di, DEMANGLE_COMPONENT_TAGGED_NAME, dc, tag);
   1379     }
   1380 
   1381   di->last_name = hold_last_name;
   1382 
   1383   return dc;
   1384 }
   1385 
   1386 /* <name> ::= <nested-name>
   1387           ::= <unscoped-name>
   1388           ::= <unscoped-template-name> <template-args>
   1389           ::= <local-name>
   1390 
   1391    <unscoped-name> ::= <unqualified-name>
   1392                    ::= St <unqualified-name>
   1393 
   1394    <unscoped-template-name> ::= <unscoped-name>
   1395                             ::= <substitution>
   1396 */
   1397 
   1398 static struct demangle_component *
   1399 d_name (struct d_info *di)
   1400 {
   1401   char peek = d_peek_char (di);
   1402   struct demangle_component *dc;
   1403 
   1404   switch (peek)
   1405     {
   1406     case 'N':
   1407       return d_nested_name (di);
   1408 
   1409     case 'Z':
   1410       return d_local_name (di);
   1411 
   1412     case 'U':
   1413       return d_unqualified_name (di);
   1414 
   1415     case 'S':
   1416       {
   1417 	int subst;
   1418 
   1419 	if (d_peek_next_char (di) != 't')
   1420 	  {
   1421 	    dc = d_substitution (di, 0);
   1422 	    subst = 1;
   1423 	  }
   1424 	else
   1425 	  {
   1426 	    d_advance (di, 2);
   1427 	    dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
   1428 			      d_make_name (di, "std", 3),
   1429 			      d_unqualified_name (di));
   1430 	    di->expansion += 3;
   1431 	    subst = 0;
   1432 	  }
   1433 
   1434 	if (d_peek_char (di) != 'I')
   1435 	  {
   1436 	    /* The grammar does not permit this case to occur if we
   1437 	       called d_substitution() above (i.e., subst == 1).  We
   1438 	       don't bother to check.  */
   1439 	  }
   1440 	else
   1441 	  {
   1442 	    /* This is <template-args>, which means that we just saw
   1443 	       <unscoped-template-name>, which is a substitution
   1444 	       candidate if we didn't just get it from a
   1445 	       substitution.  */
   1446 	    if (! subst)
   1447 	      {
   1448 		if (! d_add_substitution (di, dc))
   1449 		  return NULL;
   1450 	      }
   1451 	    dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
   1452 			      d_template_args (di));
   1453 	  }
   1454 
   1455 	return dc;
   1456       }
   1457 
   1458     case 'L':
   1459     default:
   1460       dc = d_unqualified_name (di);
   1461       if (d_peek_char (di) == 'I')
   1462 	{
   1463 	  /* This is <template-args>, which means that we just saw
   1464 	     <unscoped-template-name>, which is a substitution
   1465 	     candidate.  */
   1466 	  if (! d_add_substitution (di, dc))
   1467 	    return NULL;
   1468 	  dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
   1469 			    d_template_args (di));
   1470 	}
   1471       return dc;
   1472     }
   1473 }
   1474 
   1475 /* <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
   1476                  ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
   1477 */
   1478 
   1479 static struct demangle_component *
   1480 d_nested_name (struct d_info *di)
   1481 {
   1482   struct demangle_component *ret;
   1483   struct demangle_component **pret;
   1484   struct demangle_component *rqual;
   1485 
   1486   if (! d_check_char (di, 'N'))
   1487     return NULL;
   1488 
   1489   pret = d_cv_qualifiers (di, &ret, 1);
   1490   if (pret == NULL)
   1491     return NULL;
   1492 
   1493   /* Parse the ref-qualifier now and then attach it
   1494      once we have something to attach it to.  */
   1495   rqual = d_ref_qualifier (di, NULL);
   1496 
   1497   *pret = d_prefix (di);
   1498   if (*pret == NULL)
   1499     return NULL;
   1500 
   1501   if (rqual)
   1502     {
   1503       d_left (rqual) = ret;
   1504       ret = rqual;
   1505     }
   1506 
   1507   if (! d_check_char (di, 'E'))
   1508     return NULL;
   1509 
   1510   return ret;
   1511 }
   1512 
   1513 /* <prefix> ::= <prefix> <unqualified-name>
   1514             ::= <template-prefix> <template-args>
   1515             ::= <template-param>
   1516             ::= <decltype>
   1517             ::=
   1518             ::= <substitution>
   1519 
   1520    <template-prefix> ::= <prefix> <(template) unqualified-name>
   1521                      ::= <template-param>
   1522                      ::= <substitution>
   1523 */
   1524 
   1525 static struct demangle_component *
   1526 d_prefix (struct d_info *di)
   1527 {
   1528   struct demangle_component *ret = NULL;
   1529 
   1530   while (1)
   1531     {
   1532       char peek;
   1533       enum demangle_component_type comb_type;
   1534       struct demangle_component *dc;
   1535 
   1536       peek = d_peek_char (di);
   1537       if (peek == '\0')
   1538 	return NULL;
   1539 
   1540       /* The older code accepts a <local-name> here, but I don't see
   1541 	 that in the grammar.  The older code does not accept a
   1542 	 <template-param> here.  */
   1543 
   1544       comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
   1545       if (peek == 'D')
   1546 	{
   1547 	  char peek2 = d_peek_next_char (di);
   1548 	  if (peek2 == 'T' || peek2 == 't')
   1549 	    /* Decltype.  */
   1550 	    dc = cplus_demangle_type (di);
   1551 	  else
   1552 	    /* Destructor name.  */
   1553 	    dc = d_unqualified_name (di);
   1554 	}
   1555       else if (IS_DIGIT (peek)
   1556 	  || IS_LOWER (peek)
   1557 	  || peek == 'C'
   1558 	  || peek == 'U'
   1559 	  || peek == 'L')
   1560 	dc = d_unqualified_name (di);
   1561       else if (peek == 'S')
   1562 	dc = d_substitution (di, 1);
   1563       else if (peek == 'I')
   1564 	{
   1565 	  if (ret == NULL)
   1566 	    return NULL;
   1567 	  comb_type = DEMANGLE_COMPONENT_TEMPLATE;
   1568 	  dc = d_template_args (di);
   1569 	}
   1570       else if (peek == 'T')
   1571 	dc = d_template_param (di);
   1572       else if (peek == 'E')
   1573 	return ret;
   1574       else if (peek == 'M')
   1575 	{
   1576 	  /* Initializer scope for a lambda.  We don't need to represent
   1577 	     this; the normal code will just treat the variable as a type
   1578 	     scope, which gives appropriate output.  */
   1579 	  if (ret == NULL)
   1580 	    return NULL;
   1581 	  d_advance (di, 1);
   1582 	  continue;
   1583 	}
   1584       else
   1585 	return NULL;
   1586 
   1587       if (ret == NULL)
   1588 	ret = dc;
   1589       else
   1590 	ret = d_make_comp (di, comb_type, ret, dc);
   1591 
   1592       if (peek != 'S' && d_peek_char (di) != 'E')
   1593 	{
   1594 	  if (! d_add_substitution (di, ret))
   1595 	    return NULL;
   1596 	}
   1597     }
   1598 }
   1599 
   1600 /* <unqualified-name> ::= <operator-name>
   1601                       ::= <ctor-dtor-name>
   1602                       ::= <source-name>
   1603 		      ::= <local-source-name>
   1604 
   1605     <local-source-name>	::= L <source-name> <discriminator>
   1606 */
   1607 
   1608 static struct demangle_component *
   1609 d_unqualified_name (struct d_info *di)
   1610 {
   1611   struct demangle_component *ret;
   1612   char peek;
   1613 
   1614   peek = d_peek_char (di);
   1615   if (IS_DIGIT (peek))
   1616     ret = d_source_name (di);
   1617   else if (IS_LOWER (peek))
   1618     {
   1619       if (peek == 'o' && d_peek_next_char (di) == 'n')
   1620 	d_advance (di, 2);
   1621       ret = d_operator_name (di);
   1622       if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
   1623 	{
   1624 	  di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
   1625 	  if (!strcmp (ret->u.s_operator.op->code, "li"))
   1626 	    ret = d_make_comp (di, DEMANGLE_COMPONENT_UNARY, ret,
   1627 			       d_source_name (di));
   1628 	}
   1629     }
   1630   else if (peek == 'C' || peek == 'D')
   1631     ret = d_ctor_dtor_name (di);
   1632   else if (peek == 'L')
   1633     {
   1634       d_advance (di, 1);
   1635 
   1636       ret = d_source_name (di);
   1637       if (ret == NULL)
   1638 	return NULL;
   1639       if (! d_discriminator (di))
   1640 	return NULL;
   1641     }
   1642   else if (peek == 'U')
   1643     {
   1644       switch (d_peek_next_char (di))
   1645 	{
   1646 	case 'l':
   1647 	  ret = d_lambda (di);
   1648 	  break;
   1649 	case 't':
   1650 	  ret = d_unnamed_type (di);
   1651 	  break;
   1652 	default:
   1653 	  return NULL;
   1654 	}
   1655     }
   1656   else
   1657     return NULL;
   1658 
   1659   if (d_peek_char (di) == 'B')
   1660     ret = d_abi_tags (di, ret);
   1661   return ret;
   1662 }
   1663 
   1664 /* <source-name> ::= <(positive length) number> <identifier>  */
   1665 
   1666 static struct demangle_component *
   1667 d_source_name (struct d_info *di)
   1668 {
   1669   int len;
   1670   struct demangle_component *ret;
   1671 
   1672   len = d_number (di);
   1673   if (len <= 0)
   1674     return NULL;
   1675   ret = d_identifier (di, len);
   1676   di->last_name = ret;
   1677   return ret;
   1678 }
   1679 
   1680 /* number ::= [n] <(non-negative decimal integer)>  */
   1681 
   1682 static int
   1683 d_number (struct d_info *di)
   1684 {
   1685   int negative;
   1686   char peek;
   1687   int ret;
   1688 
   1689   negative = 0;
   1690   peek = d_peek_char (di);
   1691   if (peek == 'n')
   1692     {
   1693       negative = 1;
   1694       d_advance (di, 1);
   1695       peek = d_peek_char (di);
   1696     }
   1697 
   1698   ret = 0;
   1699   while (1)
   1700     {
   1701       if (! IS_DIGIT (peek))
   1702 	{
   1703 	  if (negative)
   1704 	    ret = - ret;
   1705 	  return ret;
   1706 	}
   1707       ret = ret * 10 + peek - '0';
   1708       d_advance (di, 1);
   1709       peek = d_peek_char (di);
   1710     }
   1711 }
   1712 
   1713 /* Like d_number, but returns a demangle_component.  */
   1714 
   1715 static struct demangle_component *
   1716 d_number_component (struct d_info *di)
   1717 {
   1718   struct demangle_component *ret = d_make_empty (di);
   1719   if (ret)
   1720     {
   1721       ret->type = DEMANGLE_COMPONENT_NUMBER;
   1722       ret->u.s_number.number = d_number (di);
   1723     }
   1724   return ret;
   1725 }
   1726 
   1727 /* identifier ::= <(unqualified source code identifier)>  */
   1728 
   1729 static struct demangle_component *
   1730 d_identifier (struct d_info *di, int len)
   1731 {
   1732   const char *name;
   1733 
   1734   name = d_str (di);
   1735 
   1736   if (di->send - name < len)
   1737     return NULL;
   1738 
   1739   d_advance (di, len);
   1740 
   1741   /* A Java mangled name may have a trailing '$' if it is a C++
   1742      keyword.  This '$' is not included in the length count.  We just
   1743      ignore the '$'.  */
   1744   if ((di->options & DMGL_JAVA) != 0
   1745       && d_peek_char (di) == '$')
   1746     d_advance (di, 1);
   1747 
   1748   /* Look for something which looks like a gcc encoding of an
   1749      anonymous namespace, and replace it with a more user friendly
   1750      name.  */
   1751   if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
   1752       && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
   1753 		 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
   1754     {
   1755       const char *s;
   1756 
   1757       s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
   1758       if ((*s == '.' || *s == '_' || *s == '$')
   1759 	  && s[1] == 'N')
   1760 	{
   1761 	  di->expansion -= len - sizeof "(anonymous namespace)";
   1762 	  return d_make_name (di, "(anonymous namespace)",
   1763 			      sizeof "(anonymous namespace)" - 1);
   1764 	}
   1765     }
   1766 
   1767   return d_make_name (di, name, len);
   1768 }
   1769 
   1770 /* operator_name ::= many different two character encodings.
   1771                  ::= cv <type>
   1772                  ::= v <digit> <source-name>
   1773 
   1774    This list is sorted for binary search.  */
   1775 
   1776 #define NL(s) s, (sizeof s) - 1
   1777 
   1778 CP_STATIC_IF_GLIBCPP_V3
   1779 const struct demangle_operator_info cplus_demangle_operators[] =
   1780 {
   1781   { "aN", NL ("&="),        2 },
   1782   { "aS", NL ("="),         2 },
   1783   { "aa", NL ("&&"),        2 },
   1784   { "ad", NL ("&"),         1 },
   1785   { "an", NL ("&"),         2 },
   1786   { "at", NL ("alignof "),   1 },
   1787   { "az", NL ("alignof "),   1 },
   1788   { "cc", NL ("const_cast"), 2 },
   1789   { "cl", NL ("()"),        2 },
   1790   { "cm", NL (","),         2 },
   1791   { "co", NL ("~"),         1 },
   1792   { "dV", NL ("/="),        2 },
   1793   { "da", NL ("delete[] "), 1 },
   1794   { "dc", NL ("dynamic_cast"), 2 },
   1795   { "de", NL ("*"),         1 },
   1796   { "dl", NL ("delete "),   1 },
   1797   { "ds", NL (".*"),        2 },
   1798   { "dt", NL ("."),         2 },
   1799   { "dv", NL ("/"),         2 },
   1800   { "eO", NL ("^="),        2 },
   1801   { "eo", NL ("^"),         2 },
   1802   { "eq", NL ("=="),        2 },
   1803   { "fL", NL ("..."),       3 },
   1804   { "fR", NL ("..."),       3 },
   1805   { "fl", NL ("..."),       2 },
   1806   { "fr", NL ("..."),       2 },
   1807   { "ge", NL (">="),        2 },
   1808   { "gs", NL ("::"),	    1 },
   1809   { "gt", NL (">"),         2 },
   1810   { "ix", NL ("[]"),        2 },
   1811   { "lS", NL ("<<="),       2 },
   1812   { "le", NL ("<="),        2 },
   1813   { "li", NL ("operator\"\" "), 1 },
   1814   { "ls", NL ("<<"),        2 },
   1815   { "lt", NL ("<"),         2 },
   1816   { "mI", NL ("-="),        2 },
   1817   { "mL", NL ("*="),        2 },
   1818   { "mi", NL ("-"),         2 },
   1819   { "ml", NL ("*"),         2 },
   1820   { "mm", NL ("--"),        1 },
   1821   { "na", NL ("new[]"),     3 },
   1822   { "ne", NL ("!="),        2 },
   1823   { "ng", NL ("-"),         1 },
   1824   { "nt", NL ("!"),         1 },
   1825   { "nw", NL ("new"),       3 },
   1826   { "oR", NL ("|="),        2 },
   1827   { "oo", NL ("||"),        2 },
   1828   { "or", NL ("|"),         2 },
   1829   { "pL", NL ("+="),        2 },
   1830   { "pl", NL ("+"),         2 },
   1831   { "pm", NL ("->*"),       2 },
   1832   { "pp", NL ("++"),        1 },
   1833   { "ps", NL ("+"),         1 },
   1834   { "pt", NL ("->"),        2 },
   1835   { "qu", NL ("?"),         3 },
   1836   { "rM", NL ("%="),        2 },
   1837   { "rS", NL (">>="),       2 },
   1838   { "rc", NL ("reinterpret_cast"), 2 },
   1839   { "rm", NL ("%"),         2 },
   1840   { "rs", NL (">>"),        2 },
   1841   { "sP", NL ("sizeof..."), 1 },
   1842   { "sZ", NL ("sizeof..."), 1 },
   1843   { "sc", NL ("static_cast"), 2 },
   1844   { "st", NL ("sizeof "),   1 },
   1845   { "sz", NL ("sizeof "),   1 },
   1846   { "tr", NL ("throw"),     0 },
   1847   { "tw", NL ("throw "),    1 },
   1848   { NULL, NULL, 0,          0 }
   1849 };
   1850 
   1851 static struct demangle_component *
   1852 d_operator_name (struct d_info *di)
   1853 {
   1854   char c1;
   1855   char c2;
   1856 
   1857   c1 = d_next_char (di);
   1858   c2 = d_next_char (di);
   1859   if (c1 == 'v' && IS_DIGIT (c2))
   1860     return d_make_extended_operator (di, c2 - '0', d_source_name (di));
   1861   else if (c1 == 'c' && c2 == 'v')
   1862     {
   1863       struct demangle_component *type;
   1864       int was_conversion = di->is_conversion;
   1865       struct demangle_component *res;
   1866 
   1867       di->is_conversion = ! di->is_expression;
   1868       type = cplus_demangle_type (di);
   1869       if (di->is_conversion)
   1870 	res = d_make_comp (di, DEMANGLE_COMPONENT_CONVERSION, type, NULL);
   1871       else
   1872 	res = d_make_comp (di, DEMANGLE_COMPONENT_CAST, type, NULL);
   1873       di->is_conversion = was_conversion;
   1874       return res;
   1875     }
   1876   else
   1877     {
   1878       /* LOW is the inclusive lower bound.  */
   1879       int low = 0;
   1880       /* HIGH is the exclusive upper bound.  We subtract one to ignore
   1881 	 the sentinel at the end of the array.  */
   1882       int high = ((sizeof (cplus_demangle_operators)
   1883 		   / sizeof (cplus_demangle_operators[0]))
   1884 		  - 1);
   1885 
   1886       while (1)
   1887 	{
   1888 	  int i;
   1889 	  const struct demangle_operator_info *p;
   1890 
   1891 	  i = low + (high - low) / 2;
   1892 	  p = cplus_demangle_operators + i;
   1893 
   1894 	  if (c1 == p->code[0] && c2 == p->code[1])
   1895 	    return d_make_operator (di, p);
   1896 
   1897 	  if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
   1898 	    high = i;
   1899 	  else
   1900 	    low = i + 1;
   1901 	  if (low == high)
   1902 	    return NULL;
   1903 	}
   1904     }
   1905 }
   1906 
   1907 static struct demangle_component *
   1908 d_make_character (struct d_info *di, int c)
   1909 {
   1910   struct demangle_component *p;
   1911   p = d_make_empty (di);
   1912   if (p != NULL)
   1913     {
   1914       p->type = DEMANGLE_COMPONENT_CHARACTER;
   1915       p->u.s_character.character = c;
   1916     }
   1917   return p;
   1918 }
   1919 
   1920 static struct demangle_component *
   1921 d_java_resource (struct d_info *di)
   1922 {
   1923   struct demangle_component *p = NULL;
   1924   struct demangle_component *next = NULL;
   1925   int len, i;
   1926   char c;
   1927   const char *str;
   1928 
   1929   len = d_number (di);
   1930   if (len <= 1)
   1931     return NULL;
   1932 
   1933   /* Eat the leading '_'.  */
   1934   if (d_next_char (di) != '_')
   1935     return NULL;
   1936   len--;
   1937 
   1938   str = d_str (di);
   1939   i = 0;
   1940 
   1941   while (len > 0)
   1942     {
   1943       c = str[i];
   1944       if (!c)
   1945 	return NULL;
   1946 
   1947       /* Each chunk is either a '$' escape...  */
   1948       if (c == '$')
   1949 	{
   1950 	  i++;
   1951 	  switch (str[i++])
   1952 	    {
   1953 	    case 'S':
   1954 	      c = '/';
   1955 	      break;
   1956 	    case '_':
   1957 	      c = '.';
   1958 	      break;
   1959 	    case '$':
   1960 	      c = '$';
   1961 	      break;
   1962 	    default:
   1963 	      return NULL;
   1964 	    }
   1965 	  next = d_make_character (di, c);
   1966 	  d_advance (di, i);
   1967 	  str = d_str (di);
   1968 	  len -= i;
   1969 	  i = 0;
   1970 	  if (next == NULL)
   1971 	    return NULL;
   1972 	}
   1973       /* ... or a sequence of characters.  */
   1974       else
   1975 	{
   1976 	  while (i < len && str[i] && str[i] != '$')
   1977 	    i++;
   1978 
   1979 	  next = d_make_name (di, str, i);
   1980 	  d_advance (di, i);
   1981 	  str = d_str (di);
   1982 	  len -= i;
   1983 	  i = 0;
   1984 	  if (next == NULL)
   1985 	    return NULL;
   1986 	}
   1987 
   1988       if (p == NULL)
   1989 	p = next;
   1990       else
   1991 	{
   1992 	  p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next);
   1993 	  if (p == NULL)
   1994 	    return NULL;
   1995 	}
   1996     }
   1997 
   1998   p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL);
   1999 
   2000   return p;
   2001 }
   2002 
   2003 /* <special-name> ::= TV <type>
   2004                   ::= TT <type>
   2005                   ::= TI <type>
   2006                   ::= TS <type>
   2007                   ::= GV <(object) name>
   2008                   ::= T <call-offset> <(base) encoding>
   2009                   ::= Tc <call-offset> <call-offset> <(base) encoding>
   2010    Also g++ extensions:
   2011                   ::= TC <type> <(offset) number> _ <(base) type>
   2012                   ::= TF <type>
   2013                   ::= TJ <type>
   2014                   ::= GR <name>
   2015 		  ::= GA <encoding>
   2016 		  ::= Gr <resource name>
   2017 		  ::= GTt <encoding>
   2018 		  ::= GTn <encoding>
   2019 */
   2020 
   2021 static struct demangle_component *
   2022 d_special_name (struct d_info *di)
   2023 {
   2024   di->expansion += 20;
   2025   if (d_check_char (di, 'T'))
   2026     {
   2027       switch (d_next_char (di))
   2028 	{
   2029 	case 'V':
   2030 	  di->expansion -= 5;
   2031 	  return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
   2032 			      cplus_demangle_type (di), NULL);
   2033 	case 'T':
   2034 	  di->expansion -= 10;
   2035 	  return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
   2036 			      cplus_demangle_type (di), NULL);
   2037 	case 'I':
   2038 	  return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
   2039 			      cplus_demangle_type (di), NULL);
   2040 	case 'S':
   2041 	  return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
   2042 			      cplus_demangle_type (di), NULL);
   2043 
   2044 	case 'h':
   2045 	  if (! d_call_offset (di, 'h'))
   2046 	    return NULL;
   2047 	  return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
   2048 			      d_encoding (di, 0), NULL);
   2049 
   2050 	case 'v':
   2051 	  if (! d_call_offset (di, 'v'))
   2052 	    return NULL;
   2053 	  return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
   2054 			      d_encoding (di, 0), NULL);
   2055 
   2056 	case 'c':
   2057 	  if (! d_call_offset (di, '\0'))
   2058 	    return NULL;
   2059 	  if (! d_call_offset (di, '\0'))
   2060 	    return NULL;
   2061 	  return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
   2062 			      d_encoding (di, 0), NULL);
   2063 
   2064 	case 'C':
   2065 	  {
   2066 	    struct demangle_component *derived_type;
   2067 	    int offset;
   2068 	    struct demangle_component *base_type;
   2069 
   2070 	    derived_type = cplus_demangle_type (di);
   2071 	    offset = d_number (di);
   2072 	    if (offset < 0)
   2073 	      return NULL;
   2074 	    if (! d_check_char (di, '_'))
   2075 	      return NULL;
   2076 	    base_type = cplus_demangle_type (di);
   2077 	    /* We don't display the offset.  FIXME: We should display
   2078 	       it in verbose mode.  */
   2079 	    di->expansion += 5;
   2080 	    return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
   2081 				base_type, derived_type);
   2082 	  }
   2083 
   2084 	case 'F':
   2085 	  return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
   2086 			      cplus_demangle_type (di), NULL);
   2087 	case 'J':
   2088 	  return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
   2089 			      cplus_demangle_type (di), NULL);
   2090 
   2091 	case 'H':
   2092 	  return d_make_comp (di, DEMANGLE_COMPONENT_TLS_INIT,
   2093 			      d_name (di), NULL);
   2094 
   2095 	case 'W':
   2096 	  return d_make_comp (di, DEMANGLE_COMPONENT_TLS_WRAPPER,
   2097 			      d_name (di), NULL);
   2098 
   2099 	default:
   2100 	  return NULL;
   2101 	}
   2102     }
   2103   else if (d_check_char (di, 'G'))
   2104     {
   2105       switch (d_next_char (di))
   2106 	{
   2107 	case 'V':
   2108 	  return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
   2109 
   2110 	case 'R':
   2111 	  {
   2112 	    struct demangle_component *name = d_name (di);
   2113 	    return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, name,
   2114 				d_number_component (di));
   2115 	  }
   2116 
   2117 	case 'A':
   2118 	  return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
   2119 			      d_encoding (di, 0), NULL);
   2120 
   2121 	case 'T':
   2122 	  switch (d_next_char (di))
   2123 	    {
   2124 	    case 'n':
   2125 	      return d_make_comp (di, DEMANGLE_COMPONENT_NONTRANSACTION_CLONE,
   2126 				  d_encoding (di, 0), NULL);
   2127 	    default:
   2128 	      /* ??? The proposal is that other letters (such as 'h') stand
   2129 		 for different variants of transaction cloning, such as
   2130 		 compiling directly for hardware transaction support.  But
   2131 		 they still should all be transactional clones of some sort
   2132 		 so go ahead and call them that.  */
   2133 	    case 't':
   2134 	      return d_make_comp (di, DEMANGLE_COMPONENT_TRANSACTION_CLONE,
   2135 				  d_encoding (di, 0), NULL);
   2136 	    }
   2137 
   2138 	case 'r':
   2139 	  return d_java_resource (di);
   2140 
   2141 	default:
   2142 	  return NULL;
   2143 	}
   2144     }
   2145   else
   2146     return NULL;
   2147 }
   2148 
   2149 /* <call-offset> ::= h <nv-offset> _
   2150                  ::= v <v-offset> _
   2151 
   2152    <nv-offset> ::= <(offset) number>
   2153 
   2154    <v-offset> ::= <(offset) number> _ <(virtual offset) number>
   2155 
   2156    The C parameter, if not '\0', is a character we just read which is
   2157    the start of the <call-offset>.
   2158 
   2159    We don't display the offset information anywhere.  FIXME: We should
   2160    display it in verbose mode.  */
   2161 
   2162 static int
   2163 d_call_offset (struct d_info *di, int c)
   2164 {
   2165   if (c == '\0')
   2166     c = d_next_char (di);
   2167 
   2168   if (c == 'h')
   2169     d_number (di);
   2170   else if (c == 'v')
   2171     {
   2172       d_number (di);
   2173       if (! d_check_char (di, '_'))
   2174 	return 0;
   2175       d_number (di);
   2176     }
   2177   else
   2178     return 0;
   2179 
   2180   if (! d_check_char (di, '_'))
   2181     return 0;
   2182 
   2183   return 1;
   2184 }
   2185 
   2186 /* <ctor-dtor-name> ::= C1
   2187                     ::= C2
   2188                     ::= C3
   2189                     ::= D0
   2190                     ::= D1
   2191                     ::= D2
   2192 */
   2193 
   2194 static struct demangle_component *
   2195 d_ctor_dtor_name (struct d_info *di)
   2196 {
   2197   if (di->last_name != NULL)
   2198     {
   2199       if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
   2200 	di->expansion += di->last_name->u.s_name.len;
   2201       else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
   2202 	di->expansion += di->last_name->u.s_string.len;
   2203     }
   2204   switch (d_peek_char (di))
   2205     {
   2206     case 'C':
   2207       {
   2208 	enum gnu_v3_ctor_kinds kind;
   2209 	int inheriting = 0;
   2210 
   2211 	if (d_peek_next_char (di) == 'I')
   2212 	  {
   2213 	    inheriting = 1;
   2214 	    d_advance (di, 1);
   2215 	  }
   2216 
   2217 	switch (d_peek_next_char (di))
   2218 	  {
   2219 	  case '1':
   2220 	    kind = gnu_v3_complete_object_ctor;
   2221 	    break;
   2222 	  case '2':
   2223 	    kind = gnu_v3_base_object_ctor;
   2224 	    break;
   2225 	  case '3':
   2226 	    kind = gnu_v3_complete_object_allocating_ctor;
   2227 	    break;
   2228           case '4':
   2229 	    kind = gnu_v3_unified_ctor;
   2230 	    break;
   2231 	  case '5':
   2232 	    kind = gnu_v3_object_ctor_group;
   2233 	    break;
   2234 	  default:
   2235 	    return NULL;
   2236 	  }
   2237 
   2238 	d_advance (di, 2);
   2239 
   2240 	if (inheriting)
   2241 	  cplus_demangle_type (di);
   2242 
   2243 	return d_make_ctor (di, kind, di->last_name);
   2244       }
   2245 
   2246     case 'D':
   2247       {
   2248 	enum gnu_v3_dtor_kinds kind;
   2249 
   2250 	switch (d_peek_next_char (di))
   2251 	  {
   2252 	  case '0':
   2253 	    kind = gnu_v3_deleting_dtor;
   2254 	    break;
   2255 	  case '1':
   2256 	    kind = gnu_v3_complete_object_dtor;
   2257 	    break;
   2258 	  case '2':
   2259 	    kind = gnu_v3_base_object_dtor;
   2260 	    break;
   2261           /*  digit '3' is not used */
   2262 	  case '4':
   2263 	    kind = gnu_v3_unified_dtor;
   2264 	    break;
   2265 	  case '5':
   2266 	    kind = gnu_v3_object_dtor_group;
   2267 	    break;
   2268 	  default:
   2269 	    return NULL;
   2270 	  }
   2271 	d_advance (di, 2);
   2272 	return d_make_dtor (di, kind, di->last_name);
   2273       }
   2274 
   2275     default:
   2276       return NULL;
   2277     }
   2278 }
   2279 
   2280 /* True iff we're looking at an order-insensitive type-qualifier, including
   2281    function-type-qualifiers.  */
   2282 
   2283 static int
   2284 next_is_type_qual (struct d_info *di)
   2285 {
   2286   char peek = d_peek_char (di);
   2287   if (peek == 'r' || peek == 'V' || peek == 'K')
   2288     return 1;
   2289   if (peek == 'D')
   2290     {
   2291       peek = d_peek_next_char (di);
   2292       if (peek == 'x' || peek == 'o' || peek == 'O' || peek == 'w')
   2293 	return 1;
   2294     }
   2295   return 0;
   2296 }
   2297 
   2298 /* <type> ::= <builtin-type>
   2299           ::= <function-type>
   2300           ::= <class-enum-type>
   2301           ::= <array-type>
   2302           ::= <pointer-to-member-type>
   2303           ::= <template-param>
   2304           ::= <template-template-param> <template-args>
   2305           ::= <substitution>
   2306           ::= <CV-qualifiers> <type>
   2307           ::= P <type>
   2308           ::= R <type>
   2309           ::= O <type> (C++0x)
   2310           ::= C <type>
   2311           ::= G <type>
   2312           ::= U <source-name> <type>
   2313 
   2314    <builtin-type> ::= various one letter codes
   2315                   ::= u <source-name>
   2316 */
   2317 
   2318 CP_STATIC_IF_GLIBCPP_V3
   2319 const struct demangle_builtin_type_info
   2320 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
   2321 {
   2322   /* a */ { NL ("signed char"),	NL ("signed char"),	D_PRINT_DEFAULT },
   2323   /* b */ { NL ("bool"),	NL ("boolean"),		D_PRINT_BOOL },
   2324   /* c */ { NL ("char"),	NL ("byte"),		D_PRINT_DEFAULT },
   2325   /* d */ { NL ("double"),	NL ("double"),		D_PRINT_FLOAT },
   2326   /* e */ { NL ("long double"),	NL ("long double"),	D_PRINT_FLOAT },
   2327   /* f */ { NL ("float"),	NL ("float"),		D_PRINT_FLOAT },
   2328   /* g */ { NL ("__float128"),	NL ("__float128"),	D_PRINT_FLOAT },
   2329   /* h */ { NL ("unsigned char"), NL ("unsigned char"),	D_PRINT_DEFAULT },
   2330   /* i */ { NL ("int"),		NL ("int"),		D_PRINT_INT },
   2331   /* j */ { NL ("unsigned int"), NL ("unsigned"),	D_PRINT_UNSIGNED },
   2332   /* k */ { NULL, 0,		NULL, 0,		D_PRINT_DEFAULT },
   2333   /* l */ { NL ("long"),	NL ("long"),		D_PRINT_LONG },
   2334   /* m */ { NL ("unsigned long"), NL ("unsigned long"),	D_PRINT_UNSIGNED_LONG },
   2335   /* n */ { NL ("__int128"),	NL ("__int128"),	D_PRINT_DEFAULT },
   2336   /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
   2337 	    D_PRINT_DEFAULT },
   2338   /* p */ { NULL, 0,		NULL, 0,		D_PRINT_DEFAULT },
   2339   /* q */ { NULL, 0,		NULL, 0,		D_PRINT_DEFAULT },
   2340   /* r */ { NULL, 0,		NULL, 0,		D_PRINT_DEFAULT },
   2341   /* s */ { NL ("short"),	NL ("short"),		D_PRINT_DEFAULT },
   2342   /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
   2343   /* u */ { NULL, 0,		NULL, 0,		D_PRINT_DEFAULT },
   2344   /* v */ { NL ("void"),	NL ("void"),		D_PRINT_VOID },
   2345   /* w */ { NL ("wchar_t"),	NL ("char"),		D_PRINT_DEFAULT },
   2346   /* x */ { NL ("long long"),	NL ("long"),		D_PRINT_LONG_LONG },
   2347   /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
   2348 	    D_PRINT_UNSIGNED_LONG_LONG },
   2349   /* z */ { NL ("..."),		NL ("..."),		D_PRINT_DEFAULT },
   2350   /* 26 */ { NL ("decimal32"),	NL ("decimal32"),	D_PRINT_DEFAULT },
   2351   /* 27 */ { NL ("decimal64"),	NL ("decimal64"),	D_PRINT_DEFAULT },
   2352   /* 28 */ { NL ("decimal128"),	NL ("decimal128"),	D_PRINT_DEFAULT },
   2353   /* 29 */ { NL ("half"),	NL ("half"),		D_PRINT_FLOAT },
   2354   /* 30 */ { NL ("char16_t"),	NL ("char16_t"),	D_PRINT_DEFAULT },
   2355   /* 31 */ { NL ("char32_t"),	NL ("char32_t"),	D_PRINT_DEFAULT },
   2356   /* 32 */ { NL ("decltype(nullptr)"),	NL ("decltype(nullptr)"),
   2357 	     D_PRINT_DEFAULT },
   2358 };
   2359 
   2360 CP_STATIC_IF_GLIBCPP_V3
   2361 struct demangle_component *
   2362 cplus_demangle_type (struct d_info *di)
   2363 {
   2364   char peek;
   2365   struct demangle_component *ret = NULL;
   2366   int can_subst;
   2367 
   2368   /* The ABI specifies that when CV-qualifiers are used, the base type
   2369      is substitutable, and the fully qualified type is substitutable,
   2370      but the base type with a strict subset of the CV-qualifiers is
   2371      not substitutable.  The natural recursive implementation of the
   2372      CV-qualifiers would cause subsets to be substitutable, so instead
   2373      we pull them all off now.
   2374 
   2375      FIXME: The ABI says that order-insensitive vendor qualifiers
   2376      should be handled in the same way, but we have no way to tell
   2377      which vendor qualifiers are order-insensitive and which are
   2378      order-sensitive.  So we just assume that they are all
   2379      order-sensitive.  g++ 3.4 supports only one vendor qualifier,
   2380      __vector, and it treats it as order-sensitive when mangling
   2381      names.  */
   2382 
   2383   if (next_is_type_qual (di))
   2384     {
   2385       struct demangle_component **pret;
   2386 
   2387       pret = d_cv_qualifiers (di, &ret, 0);
   2388       if (pret == NULL)
   2389 	return NULL;
   2390       if (d_peek_char (di) == 'F')
   2391 	{
   2392 	  /* cv-qualifiers before a function type apply to 'this',
   2393 	     so avoid adding the unqualified function type to
   2394 	     the substitution list.  */
   2395 	  *pret = d_function_type (di);
   2396 	}
   2397       else
   2398 	*pret = cplus_demangle_type (di);
   2399       if (!*pret)
   2400 	return NULL;
   2401       if ((*pret)->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
   2402 	  || (*pret)->type == DEMANGLE_COMPONENT_REFERENCE_THIS)
   2403 	{
   2404 	  /* Move the ref-qualifier outside the cv-qualifiers so that
   2405 	     they are printed in the right order.  */
   2406 	  struct demangle_component *fn = d_left (*pret);
   2407 	  d_left (*pret) = ret;
   2408 	  ret = *pret;
   2409 	  *pret = fn;
   2410 	}
   2411       if (! d_add_substitution (di, ret))
   2412 	return NULL;
   2413       return ret;
   2414     }
   2415 
   2416   can_subst = 1;
   2417 
   2418   peek = d_peek_char (di);
   2419   switch (peek)
   2420     {
   2421     case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
   2422     case 'h': case 'i': case 'j':           case 'l': case 'm': case 'n':
   2423     case 'o':                               case 's': case 't':
   2424     case 'v': case 'w': case 'x': case 'y': case 'z':
   2425       ret = d_make_builtin_type (di,
   2426 				 &cplus_demangle_builtin_types[peek - 'a']);
   2427       di->expansion += ret->u.s_builtin.type->len;
   2428       can_subst = 0;
   2429       d_advance (di, 1);
   2430       break;
   2431 
   2432     case 'u':
   2433       d_advance (di, 1);
   2434       ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
   2435 			 d_source_name (di), NULL);
   2436       break;
   2437 
   2438     case 'F':
   2439       ret = d_function_type (di);
   2440       break;
   2441 
   2442     case '0': case '1': case '2': case '3': case '4':
   2443     case '5': case '6': case '7': case '8': case '9':
   2444     case 'N':
   2445     case 'Z':
   2446       ret = d_class_enum_type (di);
   2447       break;
   2448 
   2449     case 'A':
   2450       ret = d_array_type (di);
   2451       break;
   2452 
   2453     case 'M':
   2454       ret = d_pointer_to_member_type (di);
   2455       break;
   2456 
   2457     case 'T':
   2458       ret = d_template_param (di);
   2459       if (d_peek_char (di) == 'I')
   2460 	{
   2461 	  /* This may be <template-template-param> <template-args>.
   2462 	     If this is the type for a conversion operator, we can
   2463 	     have a <template-template-param> here only by following
   2464 	     a derivation like this:
   2465 
   2466 	     <nested-name>
   2467 	     -> <template-prefix> <template-args>
   2468 	     -> <prefix> <template-unqualified-name> <template-args>
   2469 	     -> <unqualified-name> <template-unqualified-name> <template-args>
   2470 	     -> <source-name> <template-unqualified-name> <template-args>
   2471 	     -> <source-name> <operator-name> <template-args>
   2472 	     -> <source-name> cv <type> <template-args>
   2473 	     -> <source-name> cv <template-template-param> <template-args> <template-args>
   2474 
   2475 	     where the <template-args> is followed by another.
   2476 	     Otherwise, we must have a derivation like this:
   2477 
   2478 	     <nested-name>
   2479 	     -> <template-prefix> <template-args>
   2480 	     -> <prefix> <template-unqualified-name> <template-args>
   2481 	     -> <unqualified-name> <template-unqualified-name> <template-args>
   2482 	     -> <source-name> <template-unqualified-name> <template-args>
   2483 	     -> <source-name> <operator-name> <template-args>
   2484 	     -> <source-name> cv <type> <template-args>
   2485 	     -> <source-name> cv <template-param> <template-args>
   2486 
   2487 	     where we need to leave the <template-args> to be processed
   2488 	     by d_prefix (following the <template-prefix>).
   2489 
   2490 	     The <template-template-param> part is a substitution
   2491 	     candidate.  */
   2492 	  if (! di->is_conversion)
   2493 	    {
   2494 	      if (! d_add_substitution (di, ret))
   2495 		return NULL;
   2496 	      ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
   2497 				 d_template_args (di));
   2498 	    }
   2499 	  else
   2500 	    {
   2501 	      struct demangle_component *args;
   2502 	      struct d_info_checkpoint checkpoint;
   2503 
   2504 	      d_checkpoint (di, &checkpoint);
   2505 	      args = d_template_args (di);
   2506 	      if (d_peek_char (di) == 'I')
   2507 		{
   2508 		  if (! d_add_substitution (di, ret))
   2509 		    return NULL;
   2510 		  ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
   2511 				     args);
   2512 		}
   2513 	      else
   2514 		d_backtrack (di, &checkpoint);
   2515 	    }
   2516 	}
   2517       break;
   2518 
   2519     case 'S':
   2520       /* If this is a special substitution, then it is the start of
   2521 	 <class-enum-type>.  */
   2522       {
   2523 	char peek_next;
   2524 
   2525 	peek_next = d_peek_next_char (di);
   2526 	if (IS_DIGIT (peek_next)
   2527 	    || peek_next == '_'
   2528 	    || IS_UPPER (peek_next))
   2529 	  {
   2530 	    ret = d_substitution (di, 0);
   2531 	    /* The substituted name may have been a template name and
   2532 	       may be followed by tepmlate args.  */
   2533 	    if (d_peek_char (di) == 'I')
   2534 	      ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
   2535 				 d_template_args (di));
   2536 	    else
   2537 	      can_subst = 0;
   2538 	  }
   2539 	else
   2540 	  {
   2541 	    ret = d_class_enum_type (di);
   2542 	    /* If the substitution was a complete type, then it is not
   2543 	       a new substitution candidate.  However, if the
   2544 	       substitution was followed by template arguments, then
   2545 	       the whole thing is a substitution candidate.  */
   2546 	    if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
   2547 	      can_subst = 0;
   2548 	  }
   2549       }
   2550       break;
   2551 
   2552     case 'O':
   2553       d_advance (di, 1);
   2554       ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE,
   2555                          cplus_demangle_type (di), NULL);
   2556       break;
   2557 
   2558     case 'P':
   2559       d_advance (di, 1);
   2560       ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
   2561 			 cplus_demangle_type (di), NULL);
   2562       break;
   2563 
   2564     case 'R':
   2565       d_advance (di, 1);
   2566       ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
   2567                          cplus_demangle_type (di), NULL);
   2568       break;
   2569 
   2570     case 'C':
   2571       d_advance (di, 1);
   2572       ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
   2573 			 cplus_demangle_type (di), NULL);
   2574       break;
   2575 
   2576     case 'G':
   2577       d_advance (di, 1);
   2578       ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
   2579 			 cplus_demangle_type (di), NULL);
   2580       break;
   2581 
   2582     case 'U':
   2583       d_advance (di, 1);
   2584       ret = d_source_name (di);
   2585       if (d_peek_char (di) == 'I')
   2586 	ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
   2587 			   d_template_args (di));
   2588       ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
   2589 			 cplus_demangle_type (di), ret);
   2590       break;
   2591 
   2592     case 'D':
   2593       can_subst = 0;
   2594       d_advance (di, 1);
   2595       peek = d_next_char (di);
   2596       switch (peek)
   2597 	{
   2598 	case 'T':
   2599 	case 't':
   2600 	  /* decltype (expression) */
   2601 	  ret = d_make_comp (di, DEMANGLE_COMPONENT_DECLTYPE,
   2602 			     d_expression (di), NULL);
   2603 	  if (ret && d_next_char (di) != 'E')
   2604 	    ret = NULL;
   2605 	  can_subst = 1;
   2606 	  break;
   2607 
   2608 	case 'p':
   2609 	  /* Pack expansion.  */
   2610 	  ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
   2611 			     cplus_demangle_type (di), NULL);
   2612 	  can_subst = 1;
   2613 	  break;
   2614 
   2615 	case 'a':
   2616 	  /* auto */
   2617 	  ret = d_make_name (di, "auto", 4);
   2618 	  break;
   2619 	case 'c':
   2620 	  /* decltype(auto) */
   2621 	  ret = d_make_name (di, "decltype(auto)", 14);
   2622 	  break;
   2623 
   2624 	case 'f':
   2625 	  /* 32-bit decimal floating point */
   2626 	  ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[26]);
   2627 	  di->expansion += ret->u.s_builtin.type->len;
   2628 	  break;
   2629 	case 'd':
   2630 	  /* 64-bit DFP */
   2631 	  ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[27]);
   2632 	  di->expansion += ret->u.s_builtin.type->len;
   2633 	  break;
   2634 	case 'e':
   2635 	  /* 128-bit DFP */
   2636 	  ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[28]);
   2637 	  di->expansion += ret->u.s_builtin.type->len;
   2638 	  break;
   2639 	case 'h':
   2640 	  /* 16-bit half-precision FP */
   2641 	  ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[29]);
   2642 	  di->expansion += ret->u.s_builtin.type->len;
   2643 	  break;
   2644 	case 's':
   2645 	  /* char16_t */
   2646 	  ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[30]);
   2647 	  di->expansion += ret->u.s_builtin.type->len;
   2648 	  break;
   2649 	case 'i':
   2650 	  /* char32_t */
   2651 	  ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[31]);
   2652 	  di->expansion += ret->u.s_builtin.type->len;
   2653 	  break;
   2654 
   2655 	case 'F':
   2656 	  /* Fixed point types. DF<int bits><length><fract bits><sat>  */
   2657 	  ret = d_make_empty (di);
   2658 	  ret->type = DEMANGLE_COMPONENT_FIXED_TYPE;
   2659 	  if ((ret->u.s_fixed.accum = IS_DIGIT (d_peek_char (di))))
   2660 	    /* For demangling we don't care about the bits.  */
   2661 	    d_number (di);
   2662 	  ret->u.s_fixed.length = cplus_demangle_type (di);
   2663 	  if (ret->u.s_fixed.length == NULL)
   2664 	    return NULL;
   2665 	  d_number (di);
   2666 	  peek = d_next_char (di);
   2667 	  ret->u.s_fixed.sat = (peek == 's');
   2668 	  break;
   2669 
   2670 	case 'v':
   2671 	  ret = d_vector_type (di);
   2672 	  can_subst = 1;
   2673 	  break;
   2674 
   2675         case 'n':
   2676           /* decltype(nullptr) */
   2677 	  ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[32]);
   2678 	  di->expansion += ret->u.s_builtin.type->len;
   2679 	  break;
   2680 
   2681 	default:
   2682 	  return NULL;
   2683 	}
   2684       break;
   2685 
   2686     default:
   2687       return NULL;
   2688     }
   2689 
   2690   if (can_subst)
   2691     {
   2692       if (! d_add_substitution (di, ret))
   2693 	return NULL;
   2694     }
   2695 
   2696   return ret;
   2697 }
   2698 
   2699 /* <CV-qualifiers> ::= [r] [V] [K] [Dx] */
   2700 
   2701 static struct demangle_component **
   2702 d_cv_qualifiers (struct d_info *di,
   2703                  struct demangle_component **pret, int member_fn)
   2704 {
   2705   struct demangle_component **pstart;
   2706   char peek;
   2707 
   2708   pstart = pret;
   2709   peek = d_peek_char (di);
   2710   while (next_is_type_qual (di))
   2711     {
   2712       enum demangle_component_type t;
   2713       struct demangle_component *right = NULL;
   2714 
   2715       d_advance (di, 1);
   2716       if (peek == 'r')
   2717 	{
   2718 	  t = (member_fn
   2719 	       ? DEMANGLE_COMPONENT_RESTRICT_THIS
   2720 	       : DEMANGLE_COMPONENT_RESTRICT);
   2721 	  di->expansion += sizeof "restrict";
   2722 	}
   2723       else if (peek == 'V')
   2724 	{
   2725 	  t = (member_fn
   2726 	       ? DEMANGLE_COMPONENT_VOLATILE_THIS
   2727 	       : DEMANGLE_COMPONENT_VOLATILE);
   2728 	  di->expansion += sizeof "volatile";
   2729 	}
   2730       else if (peek == 'K')
   2731 	{
   2732 	  t = (member_fn
   2733 	       ? DEMANGLE_COMPONENT_CONST_THIS
   2734 	       : DEMANGLE_COMPONENT_CONST);
   2735 	  di->expansion += sizeof "const";
   2736 	}
   2737       else
   2738 	{
   2739 	  peek = d_next_char (di);
   2740 	  if (peek == 'x')
   2741 	    {
   2742 	      t = DEMANGLE_COMPONENT_TRANSACTION_SAFE;
   2743 	      di->expansion += sizeof "transaction_safe";
   2744 	    }
   2745 	  else if (peek == 'o'
   2746 		   || peek == 'O')
   2747 	    {
   2748 	      t = DEMANGLE_COMPONENT_NOEXCEPT;
   2749 	      di->expansion += sizeof "noexcept";
   2750 	      if (peek == 'O')
   2751 		{
   2752 		  right = d_expression (di);
   2753 		  if (right == NULL)
   2754 		    return NULL;
   2755 		  if (! d_check_char (di, 'E'))
   2756 		    return NULL;
   2757 		}
   2758 	    }
   2759 	  else if (peek == 'w')
   2760 	    {
   2761 	      t = DEMANGLE_COMPONENT_THROW_SPEC;
   2762 	      di->expansion += sizeof "throw";
   2763 	      right = d_parmlist (di);
   2764 	      if (right == NULL)
   2765 		return NULL;
   2766 	      if (! d_check_char (di, 'E'))
   2767 		return NULL;
   2768 	    }
   2769 	  else
   2770 	    return NULL;
   2771 	}
   2772 
   2773       *pret = d_make_comp (di, t, NULL, right);
   2774       if (*pret == NULL)
   2775 	return NULL;
   2776       pret = &d_left (*pret);
   2777 
   2778       peek = d_peek_char (di);
   2779     }
   2780 
   2781   if (!member_fn && peek == 'F')
   2782     {
   2783       while (pstart != pret)
   2784 	{
   2785 	  switch ((*pstart)->type)
   2786 	    {
   2787 	    case DEMANGLE_COMPONENT_RESTRICT:
   2788 	      (*pstart)->type = DEMANGLE_COMPONENT_RESTRICT_THIS;
   2789 	      break;
   2790 	    case DEMANGLE_COMPONENT_VOLATILE:
   2791 	      (*pstart)->type = DEMANGLE_COMPONENT_VOLATILE_THIS;
   2792 	      break;
   2793 	    case DEMANGLE_COMPONENT_CONST:
   2794 	      (*pstart)->type = DEMANGLE_COMPONENT_CONST_THIS;
   2795 	      break;
   2796 	    default:
   2797 	      break;
   2798 	    }
   2799 	  pstart = &d_left (*pstart);
   2800 	}
   2801     }
   2802 
   2803   return pret;
   2804 }
   2805 
   2806 /* <ref-qualifier> ::= R
   2807                    ::= O */
   2808 
   2809 static struct demangle_component *
   2810 d_ref_qualifier (struct d_info *di, struct demangle_component *sub)
   2811 {
   2812   struct demangle_component *ret = sub;
   2813   char peek;
   2814 
   2815   peek = d_peek_char (di);
   2816   if (peek == 'R' || peek == 'O')
   2817     {
   2818       enum demangle_component_type t;
   2819       if (peek == 'R')
   2820 	{
   2821 	  t = DEMANGLE_COMPONENT_REFERENCE_THIS;
   2822 	  di->expansion += sizeof "&";
   2823 	}
   2824       else
   2825 	{
   2826 	  t = DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS;
   2827 	  di->expansion += sizeof "&&";
   2828 	}
   2829       d_advance (di, 1);
   2830 
   2831       ret = d_make_comp (di, t, ret, NULL);
   2832     }
   2833 
   2834   return ret;
   2835 }
   2836 
   2837 /* <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] [T] E  */
   2838 
   2839 static struct demangle_component *
   2840 d_function_type (struct d_info *di)
   2841 {
   2842   struct demangle_component *ret;
   2843 
   2844   if (! d_check_char (di, 'F'))
   2845     return NULL;
   2846   if (d_peek_char (di) == 'Y')
   2847     {
   2848       /* Function has C linkage.  We don't print this information.
   2849 	 FIXME: We should print it in verbose mode.  */
   2850       d_advance (di, 1);
   2851     }
   2852   ret = d_bare_function_type (di, 1);
   2853   ret = d_ref_qualifier (di, ret);
   2854 
   2855   if (! d_check_char (di, 'E'))
   2856     return NULL;
   2857   return ret;
   2858 }
   2859 
   2860 /* <type>+ */
   2861 
   2862 static struct demangle_component *
   2863 d_parmlist (struct d_info *di)
   2864 {
   2865   struct demangle_component *tl;
   2866   struct demangle_component **ptl;
   2867 
   2868   tl = NULL;
   2869   ptl = &tl;
   2870   while (1)
   2871     {
   2872       struct demangle_component *type;
   2873 
   2874       char peek = d_peek_char (di);
   2875       if (peek == '\0' || peek == 'E' || peek == '.')
   2876 	break;
   2877       if ((peek == 'R' || peek == 'O')
   2878 	  && d_peek_next_char (di) == 'E')
   2879 	/* Function ref-qualifier, not a ref prefix for a parameter type.  */
   2880 	break;
   2881       type = cplus_demangle_type (di);
   2882       if (type == NULL)
   2883 	return NULL;
   2884       *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
   2885       if (*ptl == NULL)
   2886 	return NULL;
   2887       ptl = &d_right (*ptl);
   2888     }
   2889 
   2890   /* There should be at least one parameter type besides the optional
   2891      return type.  A function which takes no arguments will have a
   2892      single parameter type void.  */
   2893   if (tl == NULL)
   2894     return NULL;
   2895 
   2896   /* If we have a single parameter type void, omit it.  */
   2897   if (d_right (tl) == NULL
   2898       && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
   2899       && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
   2900     {
   2901       di->expansion -= d_left (tl)->u.s_builtin.type->len;
   2902       d_left (tl) = NULL;
   2903     }
   2904 
   2905   return tl;
   2906 }
   2907 
   2908 /* <bare-function-type> ::= [J]<type>+  */
   2909 
   2910 static struct demangle_component *
   2911 d_bare_function_type (struct d_info *di, int has_return_tipe)
   2912 {
   2913   struct demangle_component *return_type;
   2914   struct demangle_component *tl;
   2915   char peek;
   2916 
   2917   /* Detect special qualifier indicating that the first argument
   2918      is the return type.  */
   2919   peek = d_peek_char (di);
   2920   if (peek == 'J')
   2921     {
   2922       d_advance (di, 1);
   2923       has_return_tipe = 1;
   2924     }
   2925 
   2926   if (has_return_tipe)
   2927     {
   2928       return_type = cplus_demangle_type (di);
   2929       if (return_type == NULL)
   2930 	return NULL;
   2931     }
   2932   else
   2933     return_type = NULL;
   2934 
   2935   tl = d_parmlist (di);
   2936   if (tl == NULL)
   2937     return NULL;
   2938 
   2939   return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE,
   2940 		      return_type, tl);
   2941 }
   2942 
   2943 /* <class-enum-type> ::= <name>  */
   2944 
   2945 static struct demangle_component *
   2946 d_class_enum_type (struct d_info *di)
   2947 {
   2948   return d_name (di);
   2949 }
   2950 
   2951 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
   2952                 ::= A [<(dimension) expression>] _ <(element) type>
   2953 */
   2954 
   2955 static struct demangle_component *
   2956 d_array_type (struct d_info *di)
   2957 {
   2958   char peek;
   2959   struct demangle_component *dim;
   2960 
   2961   if (! d_check_char (di, 'A'))
   2962     return NULL;
   2963 
   2964   peek = d_peek_char (di);
   2965   if (peek == '_')
   2966     dim = NULL;
   2967   else if (IS_DIGIT (peek))
   2968     {
   2969       const char *s;
   2970 
   2971       s = d_str (di);
   2972       do
   2973 	{
   2974 	  d_advance (di, 1);
   2975 	  peek = d_peek_char (di);
   2976 	}
   2977       while (IS_DIGIT (peek));
   2978       dim = d_make_name (di, s, d_str (di) - s);
   2979       if (dim == NULL)
   2980 	return NULL;
   2981     }
   2982   else
   2983     {
   2984       dim = d_expression (di);
   2985       if (dim == NULL)
   2986 	return NULL;
   2987     }
   2988 
   2989   if (! d_check_char (di, '_'))
   2990     return NULL;
   2991 
   2992   return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
   2993 		      cplus_demangle_type (di));
   2994 }
   2995 
   2996 /* <vector-type> ::= Dv <number> _ <type>
   2997                  ::= Dv _ <expression> _ <type> */
   2998 
   2999 static struct demangle_component *
   3000 d_vector_type (struct d_info *di)
   3001 {
   3002   char peek;
   3003   struct demangle_component *dim;
   3004 
   3005   peek = d_peek_char (di);
   3006   if (peek == '_')
   3007     {
   3008       d_advance (di, 1);
   3009       dim = d_expression (di);
   3010     }
   3011   else
   3012     dim = d_number_component (di);
   3013 
   3014   if (dim == NULL)
   3015     return NULL;
   3016 
   3017   if (! d_check_char (di, '_'))
   3018     return NULL;
   3019 
   3020   return d_make_comp (di, DEMANGLE_COMPONENT_VECTOR_TYPE, dim,
   3021 		      cplus_demangle_type (di));
   3022 }
   3023 
   3024 /* <pointer-to-member-type> ::= M <(class) type> <(member) type>  */
   3025 
   3026 static struct demangle_component *
   3027 d_pointer_to_member_type (struct d_info *di)
   3028 {
   3029   struct demangle_component *cl;
   3030   struct demangle_component *mem;
   3031 
   3032   if (! d_check_char (di, 'M'))
   3033     return NULL;
   3034 
   3035   cl = cplus_demangle_type (di);
   3036   if (cl == NULL)
   3037     return NULL;
   3038 
   3039   /* The ABI says, "The type of a non-static member function is considered
   3040      to be different, for the purposes of substitution, from the type of a
   3041      namespace-scope or static member function whose type appears
   3042      similar. The types of two non-static member functions are considered
   3043      to be different, for the purposes of substitution, if the functions
   3044      are members of different classes. In other words, for the purposes of
   3045      substitution, the class of which the function is a member is
   3046      considered part of the type of function."
   3047 
   3048      For a pointer to member function, this call to cplus_demangle_type
   3049      will end up adding a (possibly qualified) non-member function type to
   3050      the substitution table, which is not correct; however, the member
   3051      function type will never be used in a substitution, so putting the
   3052      wrong type in the substitution table is harmless.  */
   3053 
   3054   mem = cplus_demangle_type (di);
   3055   if (mem == NULL)
   3056     return NULL;
   3057 
   3058   return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
   3059 }
   3060 
   3061 /* <non-negative number> _ */
   3062 
   3063 static int
   3064 d_compact_number (struct d_info *di)
   3065 {
   3066   int num;
   3067   if (d_peek_char (di) == '_')
   3068     num = 0;
   3069   else if (d_peek_char (di) == 'n')
   3070     return -1;
   3071   else
   3072     num = d_number (di) + 1;
   3073 
   3074   if (num < 0 || ! d_check_char (di, '_'))
   3075     return -1;
   3076   return num;
   3077 }
   3078 
   3079 /* <template-param> ::= T_
   3080                     ::= T <(parameter-2 non-negative) number> _
   3081 */
   3082 
   3083 static struct demangle_component *
   3084 d_template_param (struct d_info *di)
   3085 {
   3086   int param;
   3087 
   3088   if (! d_check_char (di, 'T'))
   3089     return NULL;
   3090 
   3091   param = d_compact_number (di);
   3092   if (param < 0)
   3093     return NULL;
   3094 
   3095   ++di->did_subs;
   3096 
   3097   return d_make_template_param (di, param);
   3098 }
   3099 
   3100 /* <template-args> ::= I <template-arg>+ E  */
   3101 
   3102 static struct demangle_component *
   3103 d_template_args (struct d_info *di)
   3104 {
   3105   if (d_peek_char (di) != 'I'
   3106       && d_peek_char (di) != 'J')
   3107     return NULL;
   3108   d_advance (di, 1);
   3109 
   3110   return d_template_args_1 (di);
   3111 }
   3112 
   3113 /* <template-arg>* E  */
   3114 
   3115 static struct demangle_component *
   3116 d_template_args_1 (struct d_info *di)
   3117 {
   3118   struct demangle_component *hold_last_name;
   3119   struct demangle_component *al;
   3120   struct demangle_component **pal;
   3121 
   3122   /* Preserve the last name we saw--don't let the template arguments
   3123      clobber it, as that would give us the wrong name for a subsequent
   3124      constructor or destructor.  */
   3125   hold_last_name = di->last_name;
   3126 
   3127   if (d_peek_char (di) == 'E')
   3128     {
   3129       /* An argument pack can be empty.  */
   3130       d_advance (di, 1);
   3131       return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, NULL, NULL);
   3132     }
   3133 
   3134   al = NULL;
   3135   pal = &al;
   3136   while (1)
   3137     {
   3138       struct demangle_component *a;
   3139 
   3140       a = d_template_arg (di);
   3141       if (a == NULL)
   3142 	return NULL;
   3143 
   3144       *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
   3145       if (*pal == NULL)
   3146 	return NULL;
   3147       pal = &d_right (*pal);
   3148 
   3149       if (d_peek_char (di) == 'E')
   3150 	{
   3151 	  d_advance (di, 1);
   3152 	  break;
   3153 	}
   3154     }
   3155 
   3156   di->last_name = hold_last_name;
   3157 
   3158   return al;
   3159 }
   3160 
   3161 /* <template-arg> ::= <type>
   3162                   ::= X <expression> E
   3163                   ::= <expr-primary>
   3164 */
   3165 
   3166 static struct demangle_component *
   3167 d_template_arg (struct d_info *di)
   3168 {
   3169   struct demangle_component *ret;
   3170 
   3171   switch (d_peek_char (di))
   3172     {
   3173     case 'X':
   3174       d_advance (di, 1);
   3175       ret = d_expression (di);
   3176       if (! d_check_char (di, 'E'))
   3177 	return NULL;
   3178       return ret;
   3179 
   3180     case 'L':
   3181       return d_expr_primary (di);
   3182 
   3183     case 'I':
   3184     case 'J':
   3185       /* An argument pack.  */
   3186       return d_template_args (di);
   3187 
   3188     default:
   3189       return cplus_demangle_type (di);
   3190     }
   3191 }
   3192 
   3193 /* Parse a sequence of expressions until we hit the terminator
   3194    character.  */
   3195 
   3196 static struct demangle_component *
   3197 d_exprlist (struct d_info *di, char terminator)
   3198 {
   3199   struct demangle_component *list = NULL;
   3200   struct demangle_component **p = &list;
   3201 
   3202   if (d_peek_char (di) == terminator)
   3203     {
   3204       d_advance (di, 1);
   3205       return d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, NULL, NULL);
   3206     }
   3207 
   3208   while (1)
   3209     {
   3210       struct demangle_component *arg = d_expression (di);
   3211       if (arg == NULL)
   3212 	return NULL;
   3213 
   3214       *p = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, arg, NULL);
   3215       if (*p == NULL)
   3216 	return NULL;
   3217       p = &d_right (*p);
   3218 
   3219       if (d_peek_char (di) == terminator)
   3220 	{
   3221 	  d_advance (di, 1);
   3222 	  break;
   3223 	}
   3224     }
   3225 
   3226   return list;
   3227 }
   3228 
   3229 /* Returns nonzero iff OP is an operator for a C++ cast: const_cast,
   3230    dynamic_cast, static_cast or reinterpret_cast.  */
   3231 
   3232 static int
   3233 op_is_new_cast (struct demangle_component *op)
   3234 {
   3235   const char *code = op->u.s_operator.op->code;
   3236   return (code[1] == 'c'
   3237 	  && (code[0] == 's' || code[0] == 'd'
   3238 	      || code[0] == 'c' || code[0] == 'r'));
   3239 }
   3240 
   3241 /* <expression> ::= <(unary) operator-name> <expression>
   3242                 ::= <(binary) operator-name> <expression> <expression>
   3243                 ::= <(trinary) operator-name> <expression> <expression> <expression>
   3244 		::= cl <expression>+ E
   3245                 ::= st <type>
   3246                 ::= <template-param>
   3247                 ::= sr <type> <unqualified-name>
   3248                 ::= sr <type> <unqualified-name> <template-args>
   3249                 ::= <expr-primary>
   3250 */
   3251 
   3252 static inline struct demangle_component *
   3253 d_expression_1 (struct d_info *di)
   3254 {
   3255   char peek;
   3256 
   3257   peek = d_peek_char (di);
   3258   if (peek == 'L')
   3259     return d_expr_primary (di);
   3260   else if (peek == 'T')
   3261     return d_template_param (di);
   3262   else if (peek == 's' && d_peek_next_char (di) == 'r')
   3263     {
   3264       struct demangle_component *type;
   3265       struct demangle_component *name;
   3266 
   3267       d_advance (di, 2);
   3268       type = cplus_demangle_type (di);
   3269       name = d_unqualified_name (di);
   3270       if (d_peek_char (di) != 'I')
   3271 	return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
   3272       else
   3273 	return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
   3274 			    d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
   3275 					 d_template_args (di)));
   3276     }
   3277   else if (peek == 's' && d_peek_next_char (di) == 'p')
   3278     {
   3279       d_advance (di, 2);
   3280       return d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
   3281 			  d_expression_1 (di), NULL);
   3282     }
   3283   else if (peek == 'f' && d_peek_next_char (di) == 'p')
   3284     {
   3285       /* Function parameter used in a late-specified return type.  */
   3286       int index;
   3287       d_advance (di, 2);
   3288       if (d_peek_char (di) == 'T')
   3289 	{
   3290 	  /* 'this' parameter.  */
   3291 	  d_advance (di, 1);
   3292 	  index = 0;
   3293 	}
   3294       else
   3295 	{
   3296 	  index = d_compact_number (di);
   3297 	  if (index == INT_MAX || index == -1)
   3298 	    return NULL;
   3299 	  index++;
   3300 	}
   3301       return d_make_function_param (di, index);
   3302     }
   3303   else if (IS_DIGIT (peek)
   3304 	   || (peek == 'o' && d_peek_next_char (di) == 'n'))
   3305     {
   3306       /* We can get an unqualified name as an expression in the case of
   3307          a dependent function call, i.e. decltype(f(t)).  */
   3308       struct demangle_component *name;
   3309 
   3310       if (peek == 'o')
   3311 	/* operator-function-id, i.e. operator+(t).  */
   3312 	d_advance (di, 2);
   3313 
   3314       name = d_unqualified_name (di);
   3315       if (name == NULL)
   3316 	return NULL;
   3317       if (d_peek_char (di) == 'I')
   3318 	return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
   3319 			    d_template_args (di));
   3320       else
   3321 	return name;
   3322     }
   3323   else if ((peek == 'i' || peek == 't')
   3324 	   && d_peek_next_char (di) == 'l')
   3325     {
   3326       /* Brace-enclosed initializer list, untyped or typed.  */
   3327       struct demangle_component *type = NULL;
   3328       if (peek == 't')
   3329 	type = cplus_demangle_type (di);
   3330       if (!d_peek_next_char (di))
   3331 	return NULL;
   3332       d_advance (di, 2);
   3333       return d_make_comp (di, DEMANGLE_COMPONENT_INITIALIZER_LIST,
   3334 			  type, d_exprlist (di, 'E'));
   3335     }
   3336   else
   3337     {
   3338       struct demangle_component *op;
   3339       const char *code = NULL;
   3340       int args;
   3341 
   3342       op = d_operator_name (di);
   3343       if (op == NULL)
   3344 	return NULL;
   3345 
   3346       if (op->type == DEMANGLE_COMPONENT_OPERATOR)
   3347 	{
   3348 	  code = op->u.s_operator.op->code;
   3349 	  di->expansion += op->u.s_operator.op->len - 2;
   3350 	  if (strcmp (code, "st") == 0)
   3351 	    return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
   3352 				cplus_demangle_type (di));
   3353 	}
   3354 
   3355       switch (op->type)
   3356 	{
   3357 	default:
   3358 	  return NULL;
   3359 	case DEMANGLE_COMPONENT_OPERATOR:
   3360 	  args = op->u.s_operator.op->args;
   3361 	  break;
   3362 	case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
   3363 	  args = op->u.s_extended_operator.args;
   3364 	  break;
   3365 	case DEMANGLE_COMPONENT_CAST:
   3366 	  args = 1;
   3367 	  break;
   3368 	}
   3369 
   3370       switch (args)
   3371 	{
   3372 	case 0:
   3373 	  return d_make_comp (di, DEMANGLE_COMPONENT_NULLARY, op, NULL);
   3374 
   3375 	case 1:
   3376 	  {
   3377 	    struct demangle_component *operand;
   3378 	    int suffix = 0;
   3379 
   3380 	    if (code && (code[0] == 'p' || code[0] == 'm')
   3381 		&& code[1] == code[0])
   3382 	      /* pp_ and mm_ are the prefix variants.  */
   3383 	      suffix = !d_check_char (di, '_');
   3384 
   3385 	    if (op->type == DEMANGLE_COMPONENT_CAST
   3386 		&& d_check_char (di, '_'))
   3387 	      operand = d_exprlist (di, 'E');
   3388 	    else if (code && !strcmp (code, "sP"))
   3389 	      operand = d_template_args_1 (di);
   3390 	    else
   3391 	      operand = d_expression_1 (di);
   3392 
   3393 	    if (suffix)
   3394 	      /* Indicate the suffix variant for d_print_comp.  */
   3395 	      return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
   3396 				  d_make_comp (di,
   3397 					       DEMANGLE_COMPONENT_BINARY_ARGS,
   3398 					       operand, operand));
   3399 	    else
   3400 	      return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
   3401 				  operand);
   3402 	  }
   3403 	case 2:
   3404 	  {
   3405 	    struct demangle_component *left;
   3406 	    struct demangle_component *right;
   3407 
   3408 	    if (code == NULL)
   3409 	      return NULL;
   3410 	    if (op_is_new_cast (op))
   3411 	      left = cplus_demangle_type (di);
   3412 	    else if (code[0] == 'f')
   3413 	      /* fold-expression.  */
   3414 	      left = d_operator_name (di);
   3415 	    else
   3416 	      left = d_expression_1 (di);
   3417 	    if (!strcmp (code, "cl"))
   3418 	      right = d_exprlist (di, 'E');
   3419 	    else if (!strcmp (code, "dt") || !strcmp (code, "pt"))
   3420 	      {
   3421 		right = d_unqualified_name (di);
   3422 		if (d_peek_char (di) == 'I')
   3423 		  right = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE,
   3424 				       right, d_template_args (di));
   3425 	      }
   3426 	    else
   3427 	      right = d_expression_1 (di);
   3428 
   3429 	    return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
   3430 				d_make_comp (di,
   3431 					     DEMANGLE_COMPONENT_BINARY_ARGS,
   3432 					     left, right));
   3433 	  }
   3434 	case 3:
   3435 	  {
   3436 	    struct demangle_component *first;
   3437 	    struct demangle_component *second;
   3438 	    struct demangle_component *third;
   3439 
   3440 	    if (code == NULL)
   3441 	      return NULL;
   3442 	    else if (!strcmp (code, "qu"))
   3443 	      {
   3444 		/* ?: expression.  */
   3445 		first = d_expression_1 (di);
   3446 		second = d_expression_1 (di);
   3447 		third = d_expression_1 (di);
   3448 		if (third == NULL)
   3449 		  return NULL;
   3450 	      }
   3451 	    else if (code[0] == 'f')
   3452 	      {
   3453 		/* fold-expression.  */
   3454 		first = d_operator_name (di);
   3455 		second = d_expression_1 (di);
   3456 		third = d_expression_1 (di);
   3457 		if (third == NULL)
   3458 		  return NULL;
   3459 	      }
   3460 	    else if (code[0] == 'n')
   3461 	      {
   3462 		/* new-expression.  */
   3463 		if (code[1] != 'w' && code[1] != 'a')
   3464 		  return NULL;
   3465 		first = d_exprlist (di, '_');
   3466 		second = cplus_demangle_type (di);
   3467 		if (d_peek_char (di) == 'E')
   3468 		  {
   3469 		    d_advance (di, 1);
   3470 		    third = NULL;
   3471 		  }
   3472 		else if (d_peek_char (di) == 'p'
   3473 			 && d_peek_next_char (di) == 'i')
   3474 		  {
   3475 		    /* Parenthesized initializer.  */
   3476 		    d_advance (di, 2);
   3477 		    third = d_exprlist (di, 'E');
   3478 		  }
   3479 		else if (d_peek_char (di) == 'i'
   3480 			 && d_peek_next_char (di) == 'l')
   3481 		  /* initializer-list.  */
   3482 		  third = d_expression_1 (di);
   3483 		else
   3484 		  return NULL;
   3485 	      }
   3486 	    else
   3487 	      return NULL;
   3488 	    return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
   3489 				d_make_comp (di,
   3490 					     DEMANGLE_COMPONENT_TRINARY_ARG1,
   3491 					     first,
   3492 					     d_make_comp (di,
   3493 							  DEMANGLE_COMPONENT_TRINARY_ARG2,
   3494 							  second, third)));
   3495 	  }
   3496 	default:
   3497 	  return NULL;
   3498 	}
   3499     }
   3500 }
   3501 
   3502 static struct demangle_component *
   3503 d_expression (struct d_info *di)
   3504 {
   3505   struct demangle_component *ret;
   3506   int was_expression = di->is_expression;
   3507 
   3508   di->is_expression = 1;
   3509   ret = d_expression_1 (di);
   3510   di->is_expression = was_expression;
   3511   return ret;
   3512 }
   3513 
   3514 /* <expr-primary> ::= L <type> <(value) number> E
   3515                   ::= L <type> <(value) float> E
   3516                   ::= L <mangled-name> E
   3517 */
   3518 
   3519 static struct demangle_component *
   3520 d_expr_primary (struct d_info *di)
   3521 {
   3522   struct demangle_component *ret;
   3523 
   3524   if (! d_check_char (di, 'L'))
   3525     return NULL;
   3526   if (d_peek_char (di) == '_'
   3527       /* Workaround for G++ bug; see comment in write_template_arg.  */
   3528       || d_peek_char (di) == 'Z')
   3529     ret = cplus_demangle_mangled_name (di, 0);
   3530   else
   3531     {
   3532       struct demangle_component *type;
   3533       enum demangle_component_type t;
   3534       const char *s;
   3535 
   3536       type = cplus_demangle_type (di);
   3537       if (type == NULL)
   3538 	return NULL;
   3539 
   3540       /* If we have a type we know how to print, we aren't going to
   3541 	 print the type name itself.  */
   3542       if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
   3543 	  && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
   3544 	di->expansion -= type->u.s_builtin.type->len;
   3545 
   3546       /* Rather than try to interpret the literal value, we just
   3547 	 collect it as a string.  Note that it's possible to have a
   3548 	 floating point literal here.  The ABI specifies that the
   3549 	 format of such literals is machine independent.  That's fine,
   3550 	 but what's not fine is that versions of g++ up to 3.2 with
   3551 	 -fabi-version=1 used upper case letters in the hex constant,
   3552 	 and dumped out gcc's internal representation.  That makes it
   3553 	 hard to tell where the constant ends, and hard to dump the
   3554 	 constant in any readable form anyhow.  We don't attempt to
   3555 	 handle these cases.  */
   3556 
   3557       t = DEMANGLE_COMPONENT_LITERAL;
   3558       if (d_peek_char (di) == 'n')
   3559 	{
   3560 	  t = DEMANGLE_COMPONENT_LITERAL_NEG;
   3561 	  d_advance (di, 1);
   3562 	}
   3563       s = d_str (di);
   3564       while (d_peek_char (di) != 'E')
   3565 	{
   3566 	  if (d_peek_char (di) == '\0')
   3567 	    return NULL;
   3568 	  d_advance (di, 1);
   3569 	}
   3570       ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
   3571     }
   3572   if (! d_check_char (di, 'E'))
   3573     return NULL;
   3574   return ret;
   3575 }
   3576 
   3577 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
   3578                 ::= Z <(function) encoding> E s [<discriminator>]
   3579                 ::= Z <(function) encoding> E d [<parameter> number>] _ <entity name>
   3580 */
   3581 
   3582 static struct demangle_component *
   3583 d_local_name (struct d_info *di)
   3584 {
   3585   struct demangle_component *function;
   3586 
   3587   if (! d_check_char (di, 'Z'))
   3588     return NULL;
   3589 
   3590   function = d_encoding (di, 0);
   3591 
   3592   if (! d_check_char (di, 'E'))
   3593     return NULL;
   3594 
   3595   if (d_peek_char (di) == 's')
   3596     {
   3597       d_advance (di, 1);
   3598       if (! d_discriminator (di))
   3599 	return NULL;
   3600       return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
   3601 			  d_make_name (di, "string literal",
   3602 				       sizeof "string literal" - 1));
   3603     }
   3604   else
   3605     {
   3606       struct demangle_component *name;
   3607       int num = -1;
   3608 
   3609       if (d_peek_char (di) == 'd')
   3610 	{
   3611 	  /* Default argument scope: d <number> _.  */
   3612 	  d_advance (di, 1);
   3613 	  num = d_compact_number (di);
   3614 	  if (num < 0)
   3615 	    return NULL;
   3616 	}
   3617 
   3618       name = d_name (di);
   3619       if (name)
   3620 	switch (name->type)
   3621 	  {
   3622 	    /* Lambdas and unnamed types have internal discriminators.  */
   3623 	  case DEMANGLE_COMPONENT_LAMBDA:
   3624 	  case DEMANGLE_COMPONENT_UNNAMED_TYPE:
   3625 	    break;
   3626 	  default:
   3627 	    if (! d_discriminator (di))
   3628 	      return NULL;
   3629 	  }
   3630       if (num >= 0)
   3631 	name = d_make_default_arg (di, num, name);
   3632       return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
   3633     }
   3634 }
   3635 
   3636 /* <discriminator> ::= _ <number>    # when number < 10
   3637                    ::= __ <number> _ # when number >= 10
   3638 
   3639    <discriminator> ::= _ <number>    # when number >=10
   3640    is also accepted to support gcc versions that wrongly mangled that way.
   3641 
   3642    We demangle the discriminator, but we don't print it out.  FIXME:
   3643    We should print it out in verbose mode.  */
   3644 
   3645 static int
   3646 d_discriminator (struct d_info *di)
   3647 {
   3648   int discrim, num_underscores = 1;
   3649 
   3650   if (d_peek_char (di) != '_')
   3651     return 1;
   3652   d_advance (di, 1);
   3653   if (d_peek_char (di) == '_')
   3654     {
   3655       ++num_underscores;
   3656       d_advance (di, 1);
   3657     }
   3658 
   3659   discrim = d_number (di);
   3660   if (discrim < 0)
   3661     return 0;
   3662   if (num_underscores > 1 && discrim >= 10)
   3663     {
   3664       if (d_peek_char (di) == '_')
   3665 	d_advance (di, 1);
   3666       else
   3667 	return 0;
   3668     }
   3669 
   3670   return 1;
   3671 }
   3672 
   3673 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ */
   3674 
   3675 static struct demangle_component *
   3676 d_lambda (struct d_info *di)
   3677 {
   3678   struct demangle_component *tl;
   3679   struct demangle_component *ret;
   3680   int num;
   3681 
   3682   if (! d_check_char (di, 'U'))
   3683     return NULL;
   3684   if (! d_check_char (di, 'l'))
   3685     return NULL;
   3686 
   3687   tl = d_parmlist (di);
   3688   if (tl == NULL)
   3689     return NULL;
   3690 
   3691   if (! d_check_char (di, 'E'))
   3692     return NULL;
   3693 
   3694   num = d_compact_number (di);
   3695   if (num < 0)
   3696     return NULL;
   3697 
   3698   ret = d_make_empty (di);
   3699   if (ret)
   3700     {
   3701       ret->type = DEMANGLE_COMPONENT_LAMBDA;
   3702       ret->u.s_unary_num.sub = tl;
   3703       ret->u.s_unary_num.num = num;
   3704     }
   3705 
   3706   if (! d_add_substitution (di, ret))
   3707     return NULL;
   3708 
   3709   return ret;
   3710 }
   3711 
   3712 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
   3713 
   3714 static struct demangle_component *
   3715 d_unnamed_type (struct d_info *di)
   3716 {
   3717   struct demangle_component *ret;
   3718   int num;
   3719 
   3720   if (! d_check_char (di, 'U'))
   3721     return NULL;
   3722   if (! d_check_char (di, 't'))
   3723     return NULL;
   3724 
   3725   num = d_compact_number (di);
   3726   if (num < 0)
   3727     return NULL;
   3728 
   3729   ret = d_make_empty (di);
   3730   if (ret)
   3731     {
   3732       ret->type = DEMANGLE_COMPONENT_UNNAMED_TYPE;
   3733       ret->u.s_number.number = num;
   3734     }
   3735 
   3736   if (! d_add_substitution (di, ret))
   3737     return NULL;
   3738 
   3739   return ret;
   3740 }
   3741 
   3742 /* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]*
   3743 */
   3744 
   3745 static struct demangle_component *
   3746 d_clone_suffix (struct d_info *di, struct demangle_component *encoding)
   3747 {
   3748   const char *suffix = d_str (di);
   3749   const char *pend = suffix;
   3750   struct demangle_component *n;
   3751 
   3752   if (*pend == '.' && (IS_LOWER (pend[1]) || pend[1] == '_'))
   3753     {
   3754       pend += 2;
   3755       while (IS_LOWER (*pend) || *pend == '_')
   3756 	++pend;
   3757     }
   3758   while (*pend == '.' && IS_DIGIT (pend[1]))
   3759     {
   3760       pend += 2;
   3761       while (IS_DIGIT (*pend))
   3762 	++pend;
   3763     }
   3764   d_advance (di, pend - suffix);
   3765   n = d_make_name (di, suffix, pend - suffix);
   3766   return d_make_comp (di, DEMANGLE_COMPONENT_CLONE, encoding, n);
   3767 }
   3768 
   3769 /* Add a new substitution.  */
   3770 
   3771 static int
   3772 d_add_substitution (struct d_info *di, struct demangle_component *dc)
   3773 {
   3774   if (dc == NULL)
   3775     return 0;
   3776   if (di->next_sub >= di->num_subs)
   3777     return 0;
   3778   di->subs[di->next_sub] = dc;
   3779   ++di->next_sub;
   3780   return 1;
   3781 }
   3782 
   3783 /* <substitution> ::= S <seq-id> _
   3784                   ::= S_
   3785                   ::= St
   3786                   ::= Sa
   3787                   ::= Sb
   3788                   ::= Ss
   3789                   ::= Si
   3790                   ::= So
   3791                   ::= Sd
   3792 
   3793    If PREFIX is non-zero, then this type is being used as a prefix in
   3794    a qualified name.  In this case, for the standard substitutions, we
   3795    need to check whether we are being used as a prefix for a
   3796    constructor or destructor, and return a full template name.
   3797    Otherwise we will get something like std::iostream::~iostream()
   3798    which does not correspond particularly well to any function which
   3799    actually appears in the source.
   3800 */
   3801 
   3802 static const struct d_standard_sub_info standard_subs[] =
   3803 {
   3804   { 't', NL ("std"),
   3805     NL ("std"),
   3806     NULL, 0 },
   3807   { 'a', NL ("std::allocator"),
   3808     NL ("std::allocator"),
   3809     NL ("allocator") },
   3810   { 'b', NL ("std::basic_string"),
   3811     NL ("std::basic_string"),
   3812     NL ("basic_string") },
   3813   { 's', NL ("std::string"),
   3814     NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
   3815     NL ("basic_string") },
   3816   { 'i', NL ("std::istream"),
   3817     NL ("std::basic_istream<char, std::char_traits<char> >"),
   3818     NL ("basic_istream") },
   3819   { 'o', NL ("std::ostream"),
   3820     NL ("std::basic_ostream<char, std::char_traits<char> >"),
   3821     NL ("basic_ostream") },
   3822   { 'd', NL ("std::iostream"),
   3823     NL ("std::basic_iostream<char, std::char_traits<char> >"),
   3824     NL ("basic_iostream") }
   3825 };
   3826 
   3827 static struct demangle_component *
   3828 d_substitution (struct d_info *di, int prefix)
   3829 {
   3830   char c;
   3831 
   3832   if (! d_check_char (di, 'S'))
   3833     return NULL;
   3834 
   3835   c = d_next_char (di);
   3836   if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
   3837     {
   3838       unsigned int id;
   3839 
   3840       id = 0;
   3841       if (c != '_')
   3842 	{
   3843 	  do
   3844 	    {
   3845 	      unsigned int new_id;
   3846 
   3847 	      if (IS_DIGIT (c))
   3848 		new_id = id * 36 + c - '0';
   3849 	      else if (IS_UPPER (c))
   3850 		new_id = id * 36 + c - 'A' + 10;
   3851 	      else
   3852 		return NULL;
   3853 	      if (new_id < id)
   3854 		return NULL;
   3855 	      id = new_id;
   3856 	      c = d_next_char (di);
   3857 	    }
   3858 	  while (c != '_');
   3859 
   3860 	  ++id;
   3861 	}
   3862 
   3863       if (id >= (unsigned int) di->next_sub)
   3864 	return NULL;
   3865 
   3866       ++di->did_subs;
   3867 
   3868       return di->subs[id];
   3869     }
   3870   else
   3871     {
   3872       int verbose;
   3873       const struct d_standard_sub_info *p;
   3874       const struct d_standard_sub_info *pend;
   3875 
   3876       verbose = (di->options & DMGL_VERBOSE) != 0;
   3877       if (! verbose && prefix)
   3878 	{
   3879 	  char peek;
   3880 
   3881 	  peek = d_peek_char (di);
   3882 	  if (peek == 'C' || peek == 'D')
   3883 	    verbose = 1;
   3884 	}
   3885 
   3886       pend = (&standard_subs[0]
   3887 	      + sizeof standard_subs / sizeof standard_subs[0]);
   3888       for (p = &standard_subs[0]; p < pend; ++p)
   3889 	{
   3890 	  if (c == p->code)
   3891 	    {
   3892 	      const char *s;
   3893 	      int len;
   3894 	      struct demangle_component *dc;
   3895 
   3896 	      if (p->set_last_name != NULL)
   3897 		di->last_name = d_make_sub (di, p->set_last_name,
   3898 					    p->set_last_name_len);
   3899 	      if (verbose)
   3900 		{
   3901 		  s = p->full_expansion;
   3902 		  len = p->full_len;
   3903 		}
   3904 	      else
   3905 		{
   3906 		  s = p->simple_expansion;
   3907 		  len = p->simple_len;
   3908 		}
   3909 	      di->expansion += len;
   3910 	      dc = d_make_sub (di, s, len);
   3911 	      if (d_peek_char (di) == 'B')
   3912 		{
   3913 		  /* If there are ABI tags on the abbreviation, it becomes
   3914 		     a substitution candidate.  */
   3915 		  dc = d_abi_tags (di, dc);
   3916 		  d_add_substitution (di, dc);
   3917 		}
   3918 	      return dc;
   3919 	    }
   3920 	}
   3921 
   3922       return NULL;
   3923     }
   3924 }
   3925 
   3926 static void
   3927 d_checkpoint (struct d_info *di, struct d_info_checkpoint *checkpoint)
   3928 {
   3929   checkpoint->n = di->n;
   3930   checkpoint->next_comp = di->next_comp;
   3931   checkpoint->next_sub = di->next_sub;
   3932   checkpoint->did_subs = di->did_subs;
   3933   checkpoint->expansion = di->expansion;
   3934 }
   3935 
   3936 static void
   3937 d_backtrack (struct d_info *di, struct d_info_checkpoint *checkpoint)
   3938 {
   3939   di->n = checkpoint->n;
   3940   di->next_comp = checkpoint->next_comp;
   3941   di->next_sub = checkpoint->next_sub;
   3942   di->did_subs = checkpoint->did_subs;
   3943   di->expansion = checkpoint->expansion;
   3944 }
   3945 
   3946 /* Initialize a growable string.  */
   3947 
   3948 static void
   3949 d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
   3950 {
   3951   dgs->buf = NULL;
   3952   dgs->len = 0;
   3953   dgs->alc = 0;
   3954   dgs->allocation_failure = 0;
   3955 
   3956   if (estimate > 0)
   3957     d_growable_string_resize (dgs, estimate);
   3958 }
   3959 
   3960 /* Grow a growable string to a given size.  */
   3961 
   3962 static inline void
   3963 d_growable_string_resize (struct d_growable_string *dgs, size_t need)
   3964 {
   3965   size_t newalc;
   3966   char *newbuf;
   3967 
   3968   if (dgs->allocation_failure)
   3969     return;
   3970 
   3971   /* Start allocation at two bytes to avoid any possibility of confusion
   3972      with the special value of 1 used as a return in *palc to indicate
   3973      allocation failures.  */
   3974   newalc = dgs->alc > 0 ? dgs->alc : 2;
   3975   while (newalc < need)
   3976     newalc <<= 1;
   3977 
   3978   newbuf = (char *) realloc ("demangle.dgsr.1", dgs->buf, newalc);
   3979   if (newbuf == NULL)
   3980     {
   3981       free (dgs->buf);
   3982       dgs->buf = NULL;
   3983       dgs->len = 0;
   3984       dgs->alc = 0;
   3985       dgs->allocation_failure = 1;
   3986       return;
   3987     }
   3988   dgs->buf = newbuf;
   3989   dgs->alc = newalc;
   3990 }
   3991 
   3992 /* Append a buffer to a growable string.  */
   3993 
   3994 static inline void
   3995 d_growable_string_append_buffer (struct d_growable_string *dgs,
   3996                                  const char *s, size_t l)
   3997 {
   3998   size_t need;
   3999 
   4000   need = dgs->len + l + 1;
   4001   if (need > dgs->alc)
   4002     d_growable_string_resize (dgs, need);
   4003 
   4004   if (dgs->allocation_failure)
   4005     return;
   4006 
   4007   memcpy (dgs->buf + dgs->len, s, l);
   4008   dgs->buf[dgs->len + l] = '\0';
   4009   dgs->len += l;
   4010 }
   4011 
   4012 /* Bridge growable strings to the callback mechanism.  */
   4013 
   4014 static void
   4015 d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
   4016 {
   4017   struct d_growable_string *dgs = (struct d_growable_string*) opaque;
   4018 
   4019   d_growable_string_append_buffer (dgs, s, l);
   4020 }
   4021 
   4022 /* Walk the tree, counting the number of templates encountered, and
   4023    the number of times a scope might be saved.  These counts will be
   4024    used to allocate data structures for d_print_comp, so the logic
   4025    here must mirror the logic d_print_comp will use.  It is not
   4026    important that the resulting numbers are exact, so long as they
   4027    are larger than the actual numbers encountered.  */
   4028 
   4029 static void
   4030 d_count_templates_scopes (int *num_templates, int *num_scopes,
   4031 			  const struct demangle_component *dc)
   4032 {
   4033   if (dc == NULL)
   4034     return;
   4035 
   4036   switch (dc->type)
   4037     {
   4038     case DEMANGLE_COMPONENT_NAME:
   4039     case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
   4040     case DEMANGLE_COMPONENT_FUNCTION_PARAM:
   4041     case DEMANGLE_COMPONENT_SUB_STD:
   4042     case DEMANGLE_COMPONENT_BUILTIN_TYPE:
   4043     case DEMANGLE_COMPONENT_OPERATOR:
   4044     case DEMANGLE_COMPONENT_CHARACTER:
   4045     case DEMANGLE_COMPONENT_NUMBER:
   4046     case DEMANGLE_COMPONENT_UNNAMED_TYPE:
   4047       break;
   4048 
   4049     case DEMANGLE_COMPONENT_TEMPLATE:
   4050       (*num_templates)++;
   4051       goto recurse_left_right;
   4052 
   4053     case DEMANGLE_COMPONENT_REFERENCE:
   4054     case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
   4055       if (d_left (dc)->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
   4056 	(*num_scopes)++;
   4057       goto recurse_left_right;
   4058 
   4059     case DEMANGLE_COMPONENT_QUAL_NAME:
   4060     case DEMANGLE_COMPONENT_LOCAL_NAME:
   4061     case DEMANGLE_COMPONENT_TYPED_NAME:
   4062     case DEMANGLE_COMPONENT_VTABLE:
   4063     case DEMANGLE_COMPONENT_VTT:
   4064     case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
   4065     case DEMANGLE_COMPONENT_TYPEINFO:
   4066     case DEMANGLE_COMPONENT_TYPEINFO_NAME:
   4067     case DEMANGLE_COMPONENT_TYPEINFO_FN:
   4068     case DEMANGLE_COMPONENT_THUNK:
   4069     case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
   4070     case DEMANGLE_COMPONENT_COVARIANT_THUNK:
   4071     case DEMANGLE_COMPONENT_JAVA_CLASS:
   4072     case DEMANGLE_COMPONENT_GUARD:
   4073     case DEMANGLE_COMPONENT_TLS_INIT:
   4074     case DEMANGLE_COMPONENT_TLS_WRAPPER:
   4075     case DEMANGLE_COMPONENT_REFTEMP:
   4076     case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
   4077     case DEMANGLE_COMPONENT_RESTRICT:
   4078     case DEMANGLE_COMPONENT_VOLATILE:
   4079     case DEMANGLE_COMPONENT_CONST:
   4080     case DEMANGLE_COMPONENT_RESTRICT_THIS:
   4081     case DEMANGLE_COMPONENT_VOLATILE_THIS:
   4082     case DEMANGLE_COMPONENT_CONST_THIS:
   4083     case DEMANGLE_COMPONENT_REFERENCE_THIS:
   4084     case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
   4085     case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
   4086     case DEMANGLE_COMPONENT_NOEXCEPT:
   4087     case DEMANGLE_COMPONENT_THROW_SPEC:
   4088     case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
   4089     case DEMANGLE_COMPONENT_POINTER:
   4090     case DEMANGLE_COMPONENT_COMPLEX:
   4091     case DEMANGLE_COMPONENT_IMAGINARY:
   4092     case DEMANGLE_COMPONENT_VENDOR_TYPE:
   4093     case DEMANGLE_COMPONENT_FUNCTION_TYPE:
   4094     case DEMANGLE_COMPONENT_ARRAY_TYPE:
   4095     case DEMANGLE_COMPONENT_PTRMEM_TYPE:
   4096     case DEMANGLE_COMPONENT_VECTOR_TYPE:
   4097     case DEMANGLE_COMPONENT_ARGLIST:
   4098     case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
   4099     case DEMANGLE_COMPONENT_INITIALIZER_LIST:
   4100     case DEMANGLE_COMPONENT_CAST:
   4101     case DEMANGLE_COMPONENT_CONVERSION:
   4102     case DEMANGLE_COMPONENT_NULLARY:
   4103     case DEMANGLE_COMPONENT_UNARY:
   4104     case DEMANGLE_COMPONENT_BINARY:
   4105     case DEMANGLE_COMPONENT_BINARY_ARGS:
   4106     case DEMANGLE_COMPONENT_TRINARY:
   4107     case DEMANGLE_COMPONENT_TRINARY_ARG1:
   4108     case DEMANGLE_COMPONENT_TRINARY_ARG2:
   4109     case DEMANGLE_COMPONENT_LITERAL:
   4110     case DEMANGLE_COMPONENT_LITERAL_NEG:
   4111     case DEMANGLE_COMPONENT_JAVA_RESOURCE:
   4112     case DEMANGLE_COMPONENT_COMPOUND_NAME:
   4113     case DEMANGLE_COMPONENT_DECLTYPE:
   4114     case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
   4115     case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
   4116     case DEMANGLE_COMPONENT_PACK_EXPANSION:
   4117     case DEMANGLE_COMPONENT_TAGGED_NAME:
   4118     case DEMANGLE_COMPONENT_CLONE:
   4119     recurse_left_right:
   4120       d_count_templates_scopes (num_templates, num_scopes,
   4121 				d_left (dc));
   4122       d_count_templates_scopes (num_templates, num_scopes,
   4123 				d_right (dc));
   4124       break;
   4125 
   4126     case DEMANGLE_COMPONENT_CTOR:
   4127       d_count_templates_scopes (num_templates, num_scopes,
   4128 				dc->u.s_ctor.name);
   4129       break;
   4130 
   4131     case DEMANGLE_COMPONENT_DTOR:
   4132       d_count_templates_scopes (num_templates, num_scopes,
   4133 				dc->u.s_dtor.name);
   4134       break;
   4135 
   4136     case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
   4137       d_count_templates_scopes (num_templates, num_scopes,
   4138 				dc->u.s_extended_operator.name);
   4139       break;
   4140 
   4141     case DEMANGLE_COMPONENT_FIXED_TYPE:
   4142       d_count_templates_scopes (num_templates, num_scopes,
   4143                                 dc->u.s_fixed.length);
   4144       break;
   4145 
   4146     case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
   4147     case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
   4148       d_count_templates_scopes (num_templates, num_scopes,
   4149 				d_left (dc));
   4150       break;
   4151 
   4152     case DEMANGLE_COMPONENT_LAMBDA:
   4153     case DEMANGLE_COMPONENT_DEFAULT_ARG:
   4154       d_count_templates_scopes (num_templates, num_scopes,
   4155 				dc->u.s_unary_num.sub);
   4156       break;
   4157     }
   4158 }
   4159 
   4160 /* Initialize a print information structure.  */
   4161 
   4162 static void
   4163 d_print_init (struct d_print_info *dpi, demangle_callbackref callback,
   4164 	      void *opaque, const struct demangle_component *dc)
   4165 {
   4166   dpi->len = 0;
   4167   dpi->last_char = '\0';
   4168   dpi->templates = NULL;
   4169   dpi->modifiers = NULL;
   4170   dpi->pack_index = 0;
   4171   dpi->flush_count = 0;
   4172 
   4173   dpi->callback = callback;
   4174   dpi->opaque = opaque;
   4175 
   4176   dpi->demangle_failure = 0;
   4177   dpi->is_lambda_arg = 0;
   4178 
   4179   dpi->component_stack = NULL;
   4180 
   4181   dpi->saved_scopes = NULL;
   4182   dpi->next_saved_scope = 0;
   4183   dpi->num_saved_scopes = 0;
   4184 
   4185   dpi->copy_templates = NULL;
   4186   dpi->next_copy_template = 0;
   4187   dpi->num_copy_templates = 0;
   4188 
   4189   d_count_templates_scopes (&dpi->num_copy_templates,
   4190 			    &dpi->num_saved_scopes, dc);
   4191   dpi->num_copy_templates *= dpi->num_saved_scopes;
   4192 
   4193   dpi->current_template = NULL;
   4194 }
   4195 
   4196 /* Indicate that an error occurred during printing, and test for error.  */
   4197 
   4198 static inline void
   4199 d_print_error (struct d_print_info *dpi)
   4200 {
   4201   dpi->demangle_failure = 1;
   4202 }
   4203 
   4204 static inline int
   4205 d_print_saw_error (struct d_print_info *dpi)
   4206 {
   4207   return dpi->demangle_failure != 0;
   4208 }
   4209 
   4210 /* Flush buffered characters to the callback.  */
   4211 
   4212 static inline void
   4213 d_print_flush (struct d_print_info *dpi)
   4214 {
   4215   dpi->buf[dpi->len] = '\0';
   4216   dpi->callback (dpi->buf, dpi->len, dpi->opaque);
   4217   dpi->len = 0;
   4218   dpi->flush_count++;
   4219 }
   4220 
   4221 /* Append characters and buffers for printing.  */
   4222 
   4223 static inline void
   4224 d_append_char (struct d_print_info *dpi, char c)
   4225 {
   4226   if (dpi->len == sizeof (dpi->buf) - 1)
   4227     d_print_flush (dpi);
   4228 
   4229   dpi->buf[dpi->len++] = c;
   4230   dpi->last_char = c;
   4231 }
   4232 
   4233 static inline void
   4234 d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
   4235 {
   4236   size_t i;
   4237 
   4238   for (i = 0; i < l; i++)
   4239     d_append_char (dpi, s[i]);
   4240 }
   4241 
   4242 static inline void
   4243 d_append_string (struct d_print_info *dpi, const char *s)
   4244 {
   4245   d_append_buffer (dpi, s, strlen (s));
   4246 }
   4247 
   4248 static inline void
   4249 d_append_num (struct d_print_info *dpi, int l)
   4250 {
   4251   char buf[25];
   4252   sprintf (buf,"%d", l);
   4253   d_append_string (dpi, buf);
   4254 }
   4255 
   4256 static inline char
   4257 d_last_char (struct d_print_info *dpi)
   4258 {
   4259   return dpi->last_char;
   4260 }
   4261 
   4262 /* Turn components into a human readable string.  OPTIONS is the
   4263    options bits passed to the demangler.  DC is the tree to print.
   4264    CALLBACK is a function to call to flush demangled string segments
   4265    as they fill the intermediate buffer, and OPAQUE is a generalized
   4266    callback argument.  On success, this returns 1.  On failure,
   4267    it returns 0, indicating a bad parse.  It does not use heap
   4268    memory to build an output string, so cannot encounter memory
   4269    allocation failure.  */
   4270 
   4271 CP_STATIC_IF_GLIBCPP_V3
   4272 int
   4273 cplus_demangle_print_callback (int options,
   4274                                struct demangle_component *dc,
   4275                                demangle_callbackref callback, void *opaque)
   4276 {
   4277   struct d_print_info dpi;
   4278 
   4279   d_print_init (&dpi, callback, opaque, dc);
   4280 
   4281   {
   4282 #if 0 /* in valgrind */
   4283 #ifdef CP_DYNAMIC_ARRAYS
   4284     /* Avoid zero-length VLAs, which are prohibited by the C99 standard
   4285        and flagged as errors by Address Sanitizer.  */
   4286     __extension__ struct d_saved_scope scopes[(dpi.num_saved_scopes > 0)
   4287                                               ? dpi.num_saved_scopes : 1];
   4288     __extension__ struct d_print_template temps[(dpi.num_copy_templates > 0)
   4289                                                 ? dpi.num_copy_templates : 1];
   4290 
   4291     dpi.saved_scopes = scopes;
   4292     dpi.copy_templates = temps;
   4293 #else
   4294     dpi.saved_scopes = alloca (dpi.num_saved_scopes
   4295 			       * sizeof (*dpi.saved_scopes));
   4296     dpi.copy_templates = alloca (dpi.num_copy_templates
   4297 				 * sizeof (*dpi.copy_templates));
   4298 #endif
   4299 #else
   4300     /* Allocate memory dynamically to avoid VLAs as valgrind stack
   4301        is a scarce resource */
   4302     dpi.saved_scopes = xmalloc(dpi.num_saved_scopes
   4303 			       * sizeof (*dpi.saved_scopes));
   4304     dpi.copy_templates = xmalloc (dpi.num_copy_templates
   4305                                   * sizeof (*dpi.copy_templates));
   4306 #endif /* ! in valgrind */
   4307     d_print_comp (&dpi, options, dc);
   4308   }
   4309 
   4310   d_print_flush (&dpi);
   4311 
   4312   int status = ! d_print_saw_error (&dpi);
   4313 
   4314 #if 0 /* in valgrind */
   4315 #else
   4316   free (dpi.saved_scopes);
   4317   free (dpi.copy_templates);
   4318 #endif  /* in valgrind */
   4319 
   4320   return status;
   4321 }
   4322 
   4323 /* Turn components into a human readable string.  OPTIONS is the
   4324    options bits passed to the demangler.  DC is the tree to print.
   4325    ESTIMATE is a guess at the length of the result.  This returns a
   4326    string allocated by malloc, or NULL on error.  On success, this
   4327    sets *PALC to the size of the allocated buffer.  On failure, this
   4328    sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
   4329    failure.  */
   4330 
   4331 CP_STATIC_IF_GLIBCPP_V3
   4332 char *
   4333 cplus_demangle_print (int options, struct demangle_component *dc,
   4334                       int estimate, size_t *palc)
   4335 {
   4336   struct d_growable_string dgs;
   4337 
   4338   d_growable_string_init (&dgs, estimate);
   4339 
   4340   if (! cplus_demangle_print_callback (options, dc,
   4341                                        d_growable_string_callback_adapter,
   4342                                        &dgs))
   4343     {
   4344       free (dgs.buf);
   4345       *palc = 0;
   4346       return NULL;
   4347     }
   4348 
   4349   *palc = dgs.allocation_failure ? 1 : dgs.alc;
   4350   return dgs.buf;
   4351 }
   4352 
   4353 /* Returns the I'th element of the template arglist ARGS, or NULL on
   4354    failure.  If I is negative, return the entire arglist.  */
   4355 
   4356 static struct demangle_component *
   4357 d_index_template_argument (struct demangle_component *args, int i)
   4358 {
   4359   struct demangle_component *a;
   4360 
   4361   if (i < 0)
   4362     /* Print the whole argument pack.  */
   4363     return args;
   4364 
   4365   for (a = args;
   4366        a != NULL;
   4367        a = d_right (a))
   4368     {
   4369       if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
   4370 	return NULL;
   4371       if (i <= 0)
   4372 	break;
   4373       --i;
   4374     }
   4375   if (i != 0 || a == NULL)
   4376     return NULL;
   4377 
   4378   return d_left (a);
   4379 }
   4380 
   4381 /* Returns the template argument from the current context indicated by DC,
   4382    which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL.  */
   4383 
   4384 static struct demangle_component *
   4385 d_lookup_template_argument (struct d_print_info *dpi,
   4386 			    const struct demangle_component *dc)
   4387 {
   4388   if (dpi->templates == NULL)
   4389     {
   4390       d_print_error (dpi);
   4391       return NULL;
   4392     }
   4393 
   4394   return d_index_template_argument
   4395     (d_right (dpi->templates->template_decl),
   4396      dc->u.s_number.number);
   4397 }
   4398 
   4399 /* Returns a template argument pack used in DC (any will do), or NULL.  */
   4400 
   4401 static struct demangle_component *
   4402 d_find_pack (struct d_print_info *dpi,
   4403 	     const struct demangle_component *dc)
   4404 {
   4405   struct demangle_component *a;
   4406   if (dc == NULL)
   4407     return NULL;
   4408 
   4409   switch (dc->type)
   4410     {
   4411     case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
   4412       a = d_lookup_template_argument (dpi, dc);
   4413       if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
   4414 	return a;
   4415       return NULL;
   4416 
   4417     case DEMANGLE_COMPONENT_PACK_EXPANSION:
   4418       return NULL;
   4419 
   4420     case DEMANGLE_COMPONENT_LAMBDA:
   4421     case DEMANGLE_COMPONENT_NAME:
   4422     case DEMANGLE_COMPONENT_TAGGED_NAME:
   4423     case DEMANGLE_COMPONENT_OPERATOR:
   4424     case DEMANGLE_COMPONENT_BUILTIN_TYPE:
   4425     case DEMANGLE_COMPONENT_SUB_STD:
   4426     case DEMANGLE_COMPONENT_CHARACTER:
   4427     case DEMANGLE_COMPONENT_FUNCTION_PARAM:
   4428     case DEMANGLE_COMPONENT_UNNAMED_TYPE:
   4429     case DEMANGLE_COMPONENT_FIXED_TYPE:
   4430     case DEMANGLE_COMPONENT_DEFAULT_ARG:
   4431     case DEMANGLE_COMPONENT_NUMBER:
   4432       return NULL;
   4433 
   4434     case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
   4435       return d_find_pack (dpi, dc->u.s_extended_operator.name);
   4436     case DEMANGLE_COMPONENT_CTOR:
   4437       return d_find_pack (dpi, dc->u.s_ctor.name);
   4438     case DEMANGLE_COMPONENT_DTOR:
   4439       return d_find_pack (dpi, dc->u.s_dtor.name);
   4440 
   4441     default:
   4442       a = d_find_pack (dpi, d_left (dc));
   4443       if (a)
   4444 	return a;
   4445       return d_find_pack (dpi, d_right (dc));
   4446     }
   4447 }
   4448 
   4449 /* Returns the length of the template argument pack DC.  */
   4450 
   4451 static int
   4452 d_pack_length (const struct demangle_component *dc)
   4453 {
   4454   int count = 0;
   4455   while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
   4456 	 && d_left (dc) != NULL)
   4457     {
   4458       ++count;
   4459       dc = d_right (dc);
   4460     }
   4461   return count;
   4462 }
   4463 
   4464 /* Returns the number of template args in DC, expanding any pack expansions
   4465    found there.  */
   4466 
   4467 static int
   4468 d_args_length (struct d_print_info *dpi, const struct demangle_component *dc)
   4469 {
   4470   int count = 0;
   4471   for (; dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST;
   4472        dc = d_right (dc))
   4473     {
   4474       struct demangle_component *elt = d_left (dc);
   4475       if (elt == NULL)
   4476 	break;
   4477       if (elt->type == DEMANGLE_COMPONENT_PACK_EXPANSION)
   4478 	{
   4479 	  struct demangle_component *a = d_find_pack (dpi, d_left (elt));
   4480 	  count += d_pack_length (a);
   4481 	}
   4482       else
   4483 	++count;
   4484     }
   4485   return count;
   4486 }
   4487 
   4488 /* DC is a component of a mangled expression.  Print it, wrapped in parens
   4489    if needed.  */
   4490 
   4491 static void
   4492 d_print_subexpr (struct d_print_info *dpi, int options,
   4493 		 struct demangle_component *dc)
   4494 {
   4495   int simple = 0;
   4496   if (dc->type == DEMANGLE_COMPONENT_NAME
   4497       || dc->type == DEMANGLE_COMPONENT_QUAL_NAME
   4498       || dc->type == DEMANGLE_COMPONENT_INITIALIZER_LIST
   4499       || dc->type == DEMANGLE_COMPONENT_FUNCTION_PARAM)
   4500     simple = 1;
   4501   if (!simple)
   4502     d_append_char (dpi, '(');
   4503   d_print_comp (dpi, options, dc);
   4504   if (!simple)
   4505     d_append_char (dpi, ')');
   4506 }
   4507 
   4508 /* Save the current scope.  */
   4509 
   4510 static void
   4511 d_save_scope (struct d_print_info *dpi,
   4512 	      const struct demangle_component *container)
   4513 {
   4514   struct d_saved_scope *scope;
   4515   struct d_print_template *src, **link;
   4516 
   4517   if (dpi->next_saved_scope >= dpi->num_saved_scopes)
   4518     {
   4519       d_print_error (dpi);
   4520       return;
   4521     }
   4522   scope = &dpi->saved_scopes[dpi->next_saved_scope];
   4523   dpi->next_saved_scope++;
   4524 
   4525   scope->container = container;
   4526   link = &scope->templates;
   4527 
   4528   for (src = dpi->templates; src != NULL; src = src->next)
   4529     {
   4530       struct d_print_template *dst;
   4531 
   4532       if (dpi->next_copy_template >= dpi->num_copy_templates)
   4533 	{
   4534 	  d_print_error (dpi);
   4535 	  return;
   4536 	}
   4537       dst = &dpi->copy_templates[dpi->next_copy_template];
   4538       dpi->next_copy_template++;
   4539 
   4540       dst->template_decl = src->template_decl;
   4541       *link = dst;
   4542       link = &dst->next;
   4543     }
   4544 
   4545   *link = NULL;
   4546 }
   4547 
   4548 /* Attempt to locate a previously saved scope.  Returns NULL if no
   4549    corresponding saved scope was found.  */
   4550 
   4551 static struct d_saved_scope *
   4552 d_get_saved_scope (struct d_print_info *dpi,
   4553 		   const struct demangle_component *container)
   4554 {
   4555   int i;
   4556 
   4557   for (i = 0; i < dpi->next_saved_scope; i++)
   4558     if (dpi->saved_scopes[i].container == container)
   4559       return &dpi->saved_scopes[i];
   4560 
   4561   return NULL;
   4562 }
   4563 
   4564 /* If DC is a C++17 fold-expression, print it and return true; otherwise
   4565    return false.  */
   4566 
   4567 static int
   4568 d_maybe_print_fold_expression (struct d_print_info *dpi, int options,
   4569 			       struct demangle_component *dc)
   4570 {
   4571   struct demangle_component *ops, *operator_, *op1, *op2;
   4572   int save_idx;
   4573 
   4574   const char *fold_code = d_left (dc)->u.s_operator.op->code;
   4575   if (fold_code[0] != 'f')
   4576     return 0;
   4577 
   4578   ops = d_right (dc);
   4579   operator_ = d_left (ops);
   4580   op1 = d_right (ops);
   4581   op2 = 0;
   4582   if (op1->type == DEMANGLE_COMPONENT_TRINARY_ARG2)
   4583     {
   4584       op2 = d_right (op1);
   4585       op1 = d_left (op1);
   4586     }
   4587 
   4588   /* Print the whole pack.  */
   4589   save_idx = dpi->pack_index;
   4590   dpi->pack_index = -1;
   4591 
   4592   switch (fold_code[1])
   4593     {
   4594       /* Unary left fold, (... + X).  */
   4595     case 'l':
   4596       d_append_string (dpi, "(...");
   4597       d_print_expr_op (dpi, options, operator_);
   4598       d_print_subexpr (dpi, options, op1);
   4599       d_append_char (dpi, ')');
   4600       break;
   4601 
   4602       /* Unary right fold, (X + ...).  */
   4603     case 'r':
   4604       d_append_char (dpi, '(');
   4605       d_print_subexpr (dpi, options, op1);
   4606       d_print_expr_op (dpi, options, operator_);
   4607       d_append_string (dpi, "...)");
   4608       break;
   4609 
   4610       /* Binary left fold, (42 + ... + X).  */
   4611     case 'L':
   4612       /* Binary right fold, (X + ... + 42).  */
   4613     case 'R':
   4614       d_append_char (dpi, '(');
   4615       d_print_subexpr (dpi, options, op1);
   4616       d_print_expr_op (dpi, options, operator_);
   4617       d_append_string (dpi, "...");
   4618       d_print_expr_op (dpi, options, operator_);
   4619       d_print_subexpr (dpi, options, op2);
   4620       d_append_char (dpi, ')');
   4621       break;
   4622     }
   4623 
   4624   dpi->pack_index = save_idx;
   4625   return 1;
   4626 }
   4627 
   4628 /* Subroutine to handle components.  */
   4629 
   4630 static void
   4631 d_print_comp_inner (struct d_print_info *dpi, int options,
   4632 		    struct demangle_component *dc)
   4633 {
   4634   /* Magic variable to let reference smashing skip over the next modifier
   4635      without needing to modify *dc.  */
   4636   struct demangle_component *mod_inner = NULL;
   4637 
   4638   /* Variable used to store the current templates while a previously
   4639      captured scope is used.  */
   4640   struct d_print_template *saved_templates = NULL; /* silence GCC */
   4641 
   4642   /* Nonzero if templates have been stored in the above variable.  */
   4643   int need_template_restore = 0;
   4644 
   4645   if (dc == NULL)
   4646     {
   4647       d_print_error (dpi);
   4648       return;
   4649     }
   4650   if (d_print_saw_error (dpi))
   4651     return;
   4652 
   4653   switch (dc->type)
   4654     {
   4655     case DEMANGLE_COMPONENT_NAME:
   4656       if ((options & DMGL_JAVA) == 0)
   4657 	d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
   4658       else
   4659 	d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
   4660       return;
   4661 
   4662     case DEMANGLE_COMPONENT_TAGGED_NAME:
   4663       d_print_comp (dpi, options, d_left (dc));
   4664       d_append_string (dpi, "[abi:");
   4665       d_print_comp (dpi, options, d_right (dc));
   4666       d_append_char (dpi, ']');
   4667       return;
   4668 
   4669     case DEMANGLE_COMPONENT_QUAL_NAME:
   4670     case DEMANGLE_COMPONENT_LOCAL_NAME:
   4671       d_print_comp (dpi, options, d_left (dc));
   4672       if ((options & DMGL_JAVA) == 0)
   4673 	d_append_string (dpi, "::");
   4674       else
   4675 	d_append_char (dpi, '.');
   4676       {
   4677 	struct demangle_component *local_name = d_right (dc);
   4678 	if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
   4679 	  {
   4680 	    d_append_string (dpi, "{default arg#");
   4681 	    d_append_num (dpi, local_name->u.s_unary_num.num + 1);
   4682 	    d_append_string (dpi, "}::");
   4683 	    local_name = local_name->u.s_unary_num.sub;
   4684 	  }
   4685 	d_print_comp (dpi, options, local_name);
   4686       }
   4687       return;
   4688 
   4689     case DEMANGLE_COMPONENT_TYPED_NAME:
   4690       {
   4691 	struct d_print_mod *hold_modifiers;
   4692 	struct demangle_component *typed_name;
   4693 	struct d_print_mod adpm[4];
   4694 	unsigned int i;
   4695 	struct d_print_template dpt;
   4696 
   4697 	/* Pass the name down to the type so that it can be printed in
   4698 	   the right place for the type.  We also have to pass down
   4699 	   any CV-qualifiers, which apply to the this parameter.  */
   4700 	hold_modifiers = dpi->modifiers;
   4701 	dpi->modifiers = 0;
   4702 	i = 0;
   4703 	typed_name = d_left (dc);
   4704 	while (typed_name != NULL)
   4705 	  {
   4706 	    if (i >= sizeof adpm / sizeof adpm[0])
   4707 	      {
   4708 		d_print_error (dpi);
   4709 		return;
   4710 	      }
   4711 
   4712 	    adpm[i].next = dpi->modifiers;
   4713 	    dpi->modifiers = &adpm[i];
   4714 	    adpm[i].mod = typed_name;
   4715 	    adpm[i].printed = 0;
   4716 	    adpm[i].templates = dpi->templates;
   4717 	    ++i;
   4718 
   4719 	    if (!is_fnqual_component_type (typed_name->type))
   4720 	      break;
   4721 
   4722 	    typed_name = d_left (typed_name);
   4723 	  }
   4724 
   4725 	if (typed_name == NULL)
   4726 	  {
   4727 	    d_print_error (dpi);
   4728 	    return;
   4729 	  }
   4730 
   4731 	/* If typed_name is a template, then it applies to the
   4732 	   function type as well.  */
   4733 	if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
   4734 	  {
   4735 	    dpt.next = dpi->templates;
   4736 	    dpi->templates = &dpt;
   4737 	    dpt.template_decl = typed_name;
   4738 	  }
   4739 
   4740 	/* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
   4741 	   there may be CV-qualifiers on its right argument which
   4742 	   really apply here; this happens when parsing a class which
   4743 	   is local to a function.  */
   4744 	if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
   4745 	  {
   4746 	    struct demangle_component *local_name;
   4747 
   4748 	    local_name = d_right (typed_name);
   4749 	    if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
   4750 	      local_name = local_name->u.s_unary_num.sub;
   4751 	    if (local_name == NULL)
   4752 	      {
   4753 		d_print_error (dpi);
   4754 		return;
   4755 	      }
   4756 	    while (is_fnqual_component_type (local_name->type))
   4757 	      {
   4758 		if (i >= sizeof adpm / sizeof adpm[0])
   4759 		  {
   4760 		    d_print_error (dpi);
   4761 		    return;
   4762 		  }
   4763 
   4764 		adpm[i] = adpm[i - 1];
   4765 		adpm[i].next = &adpm[i - 1];
   4766 		dpi->modifiers = &adpm[i];
   4767 
   4768 		adpm[i - 1].mod = local_name;
   4769 		adpm[i - 1].printed = 0;
   4770 		adpm[i - 1].templates = dpi->templates;
   4771 		++i;
   4772 
   4773 		local_name = d_left (local_name);
   4774 	      }
   4775 	  }
   4776 
   4777 	d_print_comp (dpi, options, d_right (dc));
   4778 
   4779 	if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
   4780 	  dpi->templates = dpt.next;
   4781 
   4782 	/* If the modifiers didn't get printed by the type, print them
   4783 	   now.  */
   4784 	while (i > 0)
   4785 	  {
   4786 	    --i;
   4787 	    if (! adpm[i].printed)
   4788 	      {
   4789 		d_append_char (dpi, ' ');
   4790 		d_print_mod (dpi, options, adpm[i].mod);
   4791 	      }
   4792 	  }
   4793 
   4794 	dpi->modifiers = hold_modifiers;
   4795 
   4796 	return;
   4797       }
   4798 
   4799     case DEMANGLE_COMPONENT_TEMPLATE:
   4800       {
   4801 	struct d_print_mod *hold_dpm;
   4802 	struct demangle_component *dcl;
   4803 	const struct demangle_component *hold_current;
   4804 
   4805 	/* This template may need to be referenced by a cast operator
   4806 	   contained in its subtree.  */
   4807 	hold_current = dpi->current_template;
   4808 	dpi->current_template = dc;
   4809 
   4810 	/* Don't push modifiers into a template definition.  Doing so
   4811 	   could give the wrong definition for a template argument.
   4812 	   Instead, treat the template essentially as a name.  */
   4813 
   4814 	hold_dpm = dpi->modifiers;
   4815 	dpi->modifiers = NULL;
   4816 
   4817         dcl = d_left (dc);
   4818 
   4819         if ((options & DMGL_JAVA) != 0
   4820             && dcl->type == DEMANGLE_COMPONENT_NAME
   4821             && dcl->u.s_name.len == 6
   4822             && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
   4823           {
   4824             /* Special-case Java arrays, so that JArray<TYPE> appears
   4825                instead as TYPE[].  */
   4826 
   4827             d_print_comp (dpi, options, d_right (dc));
   4828             d_append_string (dpi, "[]");
   4829           }
   4830         else
   4831           {
   4832 	    d_print_comp (dpi, options, dcl);
   4833 	    if (d_last_char (dpi) == '<')
   4834 	      d_append_char (dpi, ' ');
   4835 	    d_append_char (dpi, '<');
   4836 	    d_print_comp (dpi, options, d_right (dc));
   4837 	    /* Avoid generating two consecutive '>' characters, to avoid
   4838 	       the C++ syntactic ambiguity.  */
   4839 	    if (d_last_char (dpi) == '>')
   4840 	      d_append_char (dpi, ' ');
   4841 	    d_append_char (dpi, '>');
   4842           }
   4843 
   4844 	dpi->modifiers = hold_dpm;
   4845 	dpi->current_template = hold_current;
   4846 
   4847 	return;
   4848       }
   4849 
   4850     case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
   4851       if (dpi->is_lambda_arg)
   4852 	{
   4853 	  /* Show the template parm index, as that's how g++ displays
   4854 	     these, and future proofs us against potential
   4855 	     '[]<typename T> (T *a, T *b) {...}'.  */
   4856 	  d_append_buffer (dpi, "auto:", 5);
   4857 	  d_append_num (dpi, dc->u.s_number.number + 1);
   4858 	}
   4859       else
   4860 	{
   4861 	  struct d_print_template *hold_dpt;
   4862 	  struct demangle_component *a = d_lookup_template_argument (dpi, dc);
   4863 
   4864 	  if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
   4865 	    a = d_index_template_argument (a, dpi->pack_index);
   4866 
   4867 	  if (a == NULL)
   4868 	    {
   4869 	      d_print_error (dpi);
   4870 	      return;
   4871 	    }
   4872 
   4873 	  /* While processing this parameter, we need to pop the list
   4874 	     of templates.  This is because the template parameter may
   4875 	     itself be a reference to a parameter of an outer
   4876 	     template.  */
   4877 
   4878 	  hold_dpt = dpi->templates;
   4879 	  dpi->templates = hold_dpt->next;
   4880 
   4881 	  d_print_comp (dpi, options, a);
   4882 
   4883 	  dpi->templates = hold_dpt;
   4884 	}
   4885       return;
   4886 
   4887     case DEMANGLE_COMPONENT_CTOR:
   4888       d_print_comp (dpi, options, dc->u.s_ctor.name);
   4889       return;
   4890 
   4891     case DEMANGLE_COMPONENT_DTOR:
   4892       d_append_char (dpi, '~');
   4893       d_print_comp (dpi, options, dc->u.s_dtor.name);
   4894       return;
   4895 
   4896     case DEMANGLE_COMPONENT_VTABLE:
   4897       d_append_string (dpi, "vtable for ");
   4898       d_print_comp (dpi, options, d_left (dc));
   4899       return;
   4900 
   4901     case DEMANGLE_COMPONENT_VTT:
   4902       d_append_string (dpi, "VTT for ");
   4903       d_print_comp (dpi, options, d_left (dc));
   4904       return;
   4905 
   4906     case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
   4907       d_append_string (dpi, "construction vtable for ");
   4908       d_print_comp (dpi, options, d_left (dc));
   4909       d_append_string (dpi, "-in-");
   4910       d_print_comp (dpi, options, d_right (dc));
   4911       return;
   4912 
   4913     case DEMANGLE_COMPONENT_TYPEINFO:
   4914       d_append_string (dpi, "typeinfo for ");
   4915       d_print_comp (dpi, options, d_left (dc));
   4916       return;
   4917 
   4918     case DEMANGLE_COMPONENT_TYPEINFO_NAME:
   4919       d_append_string (dpi, "typeinfo name for ");
   4920       d_print_comp (dpi, options, d_left (dc));
   4921       return;
   4922 
   4923     case DEMANGLE_COMPONENT_TYPEINFO_FN:
   4924       d_append_string (dpi, "typeinfo fn for ");
   4925       d_print_comp (dpi, options, d_left (dc));
   4926       return;
   4927 
   4928     case DEMANGLE_COMPONENT_THUNK:
   4929       d_append_string (dpi, "non-virtual thunk to ");
   4930       d_print_comp (dpi, options, d_left (dc));
   4931       return;
   4932 
   4933     case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
   4934       d_append_string (dpi, "virtual thunk to ");
   4935       d_print_comp (dpi, options, d_left (dc));
   4936       return;
   4937 
   4938     case DEMANGLE_COMPONENT_COVARIANT_THUNK:
   4939       d_append_string (dpi, "covariant return thunk to ");
   4940       d_print_comp (dpi, options, d_left (dc));
   4941       return;
   4942 
   4943     case DEMANGLE_COMPONENT_JAVA_CLASS:
   4944       d_append_string (dpi, "java Class for ");
   4945       d_print_comp (dpi, options, d_left (dc));
   4946       return;
   4947 
   4948     case DEMANGLE_COMPONENT_GUARD:
   4949       d_append_string (dpi, "guard variable for ");
   4950       d_print_comp (dpi, options, d_left (dc));
   4951       return;
   4952 
   4953     case DEMANGLE_COMPONENT_TLS_INIT:
   4954       d_append_string (dpi, "TLS init function for ");
   4955       d_print_comp (dpi, options, d_left (dc));
   4956       return;
   4957 
   4958     case DEMANGLE_COMPONENT_TLS_WRAPPER:
   4959       d_append_string (dpi, "TLS wrapper function for ");
   4960       d_print_comp (dpi, options, d_left (dc));
   4961       return;
   4962 
   4963     case DEMANGLE_COMPONENT_REFTEMP:
   4964       d_append_string (dpi, "reference temporary #");
   4965       d_print_comp (dpi, options, d_right (dc));
   4966       d_append_string (dpi, " for ");
   4967       d_print_comp (dpi, options, d_left (dc));
   4968       return;
   4969 
   4970     case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
   4971       d_append_string (dpi, "hidden alias for ");
   4972       d_print_comp (dpi, options, d_left (dc));
   4973       return;
   4974 
   4975     case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
   4976       d_append_string (dpi, "transaction clone for ");
   4977       d_print_comp (dpi, options, d_left (dc));
   4978       return;
   4979 
   4980     case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
   4981       d_append_string (dpi, "non-transaction clone for ");
   4982       d_print_comp (dpi, options, d_left (dc));
   4983       return;
   4984 
   4985     case DEMANGLE_COMPONENT_SUB_STD:
   4986       d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
   4987       return;
   4988 
   4989     case DEMANGLE_COMPONENT_RESTRICT:
   4990     case DEMANGLE_COMPONENT_VOLATILE:
   4991     case DEMANGLE_COMPONENT_CONST:
   4992       {
   4993 	struct d_print_mod *pdpm;
   4994 
   4995 	/* When printing arrays, it's possible to have cases where the
   4996 	   same CV-qualifier gets pushed on the stack multiple times.
   4997 	   We only need to print it once.  */
   4998 
   4999 	for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
   5000 	  {
   5001 	    if (! pdpm->printed)
   5002 	      {
   5003 		if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
   5004 		    && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
   5005 		    && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
   5006 		  break;
   5007 		if (pdpm->mod->type == dc->type)
   5008 		  {
   5009 		    d_print_comp (dpi, options, d_left (dc));
   5010 		    return;
   5011 		  }
   5012 	      }
   5013 	  }
   5014       }
   5015       goto modifier;
   5016 
   5017     case DEMANGLE_COMPONENT_REFERENCE:
   5018     case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
   5019       {
   5020 	/* Handle reference smashing: & + && = &.  */
   5021 	struct demangle_component *sub = d_left (dc);
   5022 	if (!dpi->is_lambda_arg
   5023 	    && sub->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
   5024 	  {
   5025 	    struct d_saved_scope *scope = d_get_saved_scope (dpi, sub);
   5026 	    struct demangle_component *a;
   5027 
   5028 	    if (scope == NULL)
   5029 	      {
   5030 		/* This is the first time SUB has been traversed.
   5031 		   We need to capture the current templates so
   5032 		   they can be restored if SUB is reentered as a
   5033 		   substitution.  */
   5034 		d_save_scope (dpi, sub);
   5035 		if (d_print_saw_error (dpi))
   5036 		  return;
   5037 	      }
   5038 	    else
   5039 	      {
   5040 		const struct d_component_stack *dcse;
   5041 		int found_self_or_parent = 0;
   5042 
   5043 		/* This traversal is reentering SUB as a substition.
   5044 		   If we are not beneath SUB or DC in the tree then we
   5045 		   need to restore SUB's template stack temporarily.  */
   5046 		for (dcse = dpi->component_stack; dcse != NULL;
   5047 		     dcse = dcse->parent)
   5048 		  {
   5049 		    if (dcse->dc == sub
   5050 			|| (dcse->dc == dc
   5051 			    && dcse != dpi->component_stack))
   5052 		      {
   5053 			found_self_or_parent = 1;
   5054 			break;
   5055 		      }
   5056 		  }
   5057 
   5058 		if (!found_self_or_parent)
   5059 		  {
   5060 		    saved_templates = dpi->templates;
   5061 		    dpi->templates = scope->templates;
   5062 		    need_template_restore = 1;
   5063 		  }
   5064 	      }
   5065 
   5066 	    a = d_lookup_template_argument (dpi, sub);
   5067 	    if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
   5068 	      a = d_index_template_argument (a, dpi->pack_index);
   5069 
   5070 	    if (a == NULL)
   5071 	      {
   5072 		if (need_template_restore)
   5073 		  dpi->templates = saved_templates;
   5074 
   5075 		d_print_error (dpi);
   5076 		return;
   5077 	      }
   5078 
   5079 	    sub = a;
   5080 	  }
   5081 
   5082 	if (sub->type == DEMANGLE_COMPONENT_REFERENCE
   5083 	    || sub->type == dc->type)
   5084 	  dc = sub;
   5085 	else if (sub->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE)
   5086 	  mod_inner = d_left (sub);
   5087       }
   5088       /* Fall through.  */
   5089 
   5090     case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
   5091     case DEMANGLE_COMPONENT_POINTER:
   5092     case DEMANGLE_COMPONENT_COMPLEX:
   5093     case DEMANGLE_COMPONENT_IMAGINARY:
   5094     FNQUAL_COMPONENT_CASE:
   5095     modifier:
   5096       {
   5097 	/* We keep a list of modifiers on the stack.  */
   5098 	struct d_print_mod dpm;
   5099 
   5100 	dpm.next = dpi->modifiers;
   5101 	dpi->modifiers = &dpm;
   5102 	dpm.mod = dc;
   5103 	dpm.printed = 0;
   5104 	dpm.templates = dpi->templates;
   5105 
   5106 	if (!mod_inner)
   5107 	  mod_inner = d_left (dc);
   5108 
   5109 	d_print_comp (dpi, options, mod_inner);
   5110 
   5111 	/* If the modifier didn't get printed by the type, print it
   5112 	   now.  */
   5113 	if (! dpm.printed)
   5114 	  d_print_mod (dpi, options, dc);
   5115 
   5116 	dpi->modifiers = dpm.next;
   5117 
   5118 	if (need_template_restore)
   5119 	  dpi->templates = saved_templates;
   5120 
   5121 	return;
   5122       }
   5123 
   5124     case DEMANGLE_COMPONENT_BUILTIN_TYPE:
   5125       if ((options & DMGL_JAVA) == 0)
   5126 	d_append_buffer (dpi, dc->u.s_builtin.type->name,
   5127 			 dc->u.s_builtin.type->len);
   5128       else
   5129 	d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
   5130 			 dc->u.s_builtin.type->java_len);
   5131       return;
   5132 
   5133     case DEMANGLE_COMPONENT_VENDOR_TYPE:
   5134       d_print_comp (dpi, options, d_left (dc));
   5135       return;
   5136 
   5137     case DEMANGLE_COMPONENT_FUNCTION_TYPE:
   5138       {
   5139 	if ((options & DMGL_RET_POSTFIX) != 0)
   5140 	  d_print_function_type (dpi,
   5141 				 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
   5142 				 dc, dpi->modifiers);
   5143 
   5144 	/* Print return type if present */
   5145 	if (d_left (dc) != NULL && (options & DMGL_RET_POSTFIX) != 0)
   5146 	  d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
   5147 			d_left (dc));
   5148 	else if (d_left (dc) != NULL && (options & DMGL_RET_DROP) == 0)
   5149 	  {
   5150 	    struct d_print_mod dpm;
   5151 
   5152 	    /* We must pass this type down as a modifier in order to
   5153 	       print it in the right location.  */
   5154 	    dpm.next = dpi->modifiers;
   5155 	    dpi->modifiers = &dpm;
   5156 	    dpm.mod = dc;
   5157 	    dpm.printed = 0;
   5158 	    dpm.templates = dpi->templates;
   5159 
   5160 	    d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
   5161 			  d_left (dc));
   5162 
   5163 	    dpi->modifiers = dpm.next;
   5164 
   5165 	    if (dpm.printed)
   5166 	      return;
   5167 
   5168 	    /* In standard prefix notation, there is a space between the
   5169 	       return type and the function signature.  */
   5170 	    if ((options & DMGL_RET_POSTFIX) == 0)
   5171 	      d_append_char (dpi, ' ');
   5172 	  }
   5173 
   5174 	if ((options & DMGL_RET_POSTFIX) == 0)
   5175 	  d_print_function_type (dpi,
   5176 				 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
   5177 				 dc, dpi->modifiers);
   5178 
   5179 	return;
   5180       }
   5181 
   5182     case DEMANGLE_COMPONENT_ARRAY_TYPE:
   5183       {
   5184 	struct d_print_mod *hold_modifiers;
   5185 	struct d_print_mod adpm[4];
   5186 	unsigned int i;
   5187 	struct d_print_mod *pdpm;
   5188 
   5189 	/* We must pass this type down as a modifier in order to print
   5190 	   multi-dimensional arrays correctly.  If the array itself is
   5191 	   CV-qualified, we act as though the element type were
   5192 	   CV-qualified.  We do this by copying the modifiers down
   5193 	   rather than fiddling pointers, so that we don't wind up
   5194 	   with a d_print_mod higher on the stack pointing into our
   5195 	   stack frame after we return.  */
   5196 
   5197 	hold_modifiers = dpi->modifiers;
   5198 
   5199 	adpm[0].next = hold_modifiers;
   5200 	dpi->modifiers = &adpm[0];
   5201 	adpm[0].mod = dc;
   5202 	adpm[0].printed = 0;
   5203 	adpm[0].templates = dpi->templates;
   5204 
   5205 	i = 1;
   5206 	pdpm = hold_modifiers;
   5207 	while (pdpm != NULL
   5208 	       && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
   5209 		   || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
   5210 		   || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
   5211 	  {
   5212 	    if (! pdpm->printed)
   5213 	      {
   5214 		if (i >= sizeof adpm / sizeof adpm[0])
   5215 		  {
   5216 		    d_print_error (dpi);
   5217 		    return;
   5218 		  }
   5219 
   5220 		adpm[i] = *pdpm;
   5221 		adpm[i].next = dpi->modifiers;
   5222 		dpi->modifiers = &adpm[i];
   5223 		pdpm->printed = 1;
   5224 		++i;
   5225 	      }
   5226 
   5227 	    pdpm = pdpm->next;
   5228 	  }
   5229 
   5230 	d_print_comp (dpi, options, d_right (dc));
   5231 
   5232 	dpi->modifiers = hold_modifiers;
   5233 
   5234 	if (adpm[0].printed)
   5235 	  return;
   5236 
   5237 	while (i > 1)
   5238 	  {
   5239 	    --i;
   5240 	    d_print_mod (dpi, options, adpm[i].mod);
   5241 	  }
   5242 
   5243 	d_print_array_type (dpi, options, dc, dpi->modifiers);
   5244 
   5245 	return;
   5246       }
   5247 
   5248     case DEMANGLE_COMPONENT_PTRMEM_TYPE:
   5249     case DEMANGLE_COMPONENT_VECTOR_TYPE:
   5250       {
   5251 	struct d_print_mod dpm;
   5252 
   5253 	dpm.next = dpi->modifiers;
   5254 	dpi->modifiers = &dpm;
   5255 	dpm.mod = dc;
   5256 	dpm.printed = 0;
   5257 	dpm.templates = dpi->templates;
   5258 
   5259 	d_print_comp (dpi, options, d_right (dc));
   5260 
   5261 	/* If the modifier didn't get printed by the type, print it
   5262 	   now.  */
   5263 	if (! dpm.printed)
   5264 	  d_print_mod (dpi, options, dc);
   5265 
   5266 	dpi->modifiers = dpm.next;
   5267 
   5268 	return;
   5269       }
   5270 
   5271     case DEMANGLE_COMPONENT_FIXED_TYPE:
   5272       if (dc->u.s_fixed.sat)
   5273 	d_append_string (dpi, "_Sat ");
   5274       /* Don't print "int _Accum".  */
   5275       if (dc->u.s_fixed.length->u.s_builtin.type
   5276 	  != &cplus_demangle_builtin_types['i'-'a'])
   5277 	{
   5278 	  d_print_comp (dpi, options, dc->u.s_fixed.length);
   5279 	  d_append_char (dpi, ' ');
   5280 	}
   5281       if (dc->u.s_fixed.accum)
   5282 	d_append_string (dpi, "_Accum");
   5283       else
   5284 	d_append_string (dpi, "_Fract");
   5285       return;
   5286 
   5287     case DEMANGLE_COMPONENT_ARGLIST:
   5288     case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
   5289       if (d_left (dc) != NULL)
   5290 	d_print_comp (dpi, options, d_left (dc));
   5291       if (d_right (dc) != NULL)
   5292 	{
   5293 	  size_t len;
   5294 	  unsigned long int flush_count;
   5295 	  /* Make sure ", " isn't flushed by d_append_string, otherwise
   5296 	     dpi->len -= 2 wouldn't work.  */
   5297 	  if (dpi->len >= sizeof (dpi->buf) - 2)
   5298 	    d_print_flush (dpi);
   5299 	  d_append_string (dpi, ", ");
   5300 	  len = dpi->len;
   5301 	  flush_count = dpi->flush_count;
   5302 	  d_print_comp (dpi, options, d_right (dc));
   5303 	  /* If that didn't print anything (which can happen with empty
   5304 	     template argument packs), remove the comma and space.  */
   5305 	  if (dpi->flush_count == flush_count && dpi->len == len)
   5306 	    dpi->len -= 2;
   5307 	}
   5308       return;
   5309 
   5310     case DEMANGLE_COMPONENT_INITIALIZER_LIST:
   5311       {
   5312 	struct demangle_component *type = d_left (dc);
   5313 	struct demangle_component *list = d_right (dc);
   5314 
   5315 	if (type)
   5316 	  d_print_comp (dpi, options, type);
   5317 	d_append_char (dpi, '{');
   5318 	d_print_comp (dpi, options, list);
   5319 	d_append_char (dpi, '}');
   5320       }
   5321       return;
   5322 
   5323     case DEMANGLE_COMPONENT_OPERATOR:
   5324       {
   5325 	const struct demangle_operator_info *op = dc->u.s_operator.op;
   5326 	int len = op->len;
   5327 
   5328 	d_append_string (dpi, "operator");
   5329 	/* Add a space before new/delete.  */
   5330 	if (IS_LOWER (op->name[0]))
   5331 	  d_append_char (dpi, ' ');
   5332 	/* Omit a trailing space.  */
   5333 	if (op->name[len-1] == ' ')
   5334 	  --len;
   5335 	d_append_buffer (dpi, op->name, len);
   5336 	return;
   5337       }
   5338 
   5339     case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
   5340       d_append_string (dpi, "operator ");
   5341       d_print_comp (dpi, options, dc->u.s_extended_operator.name);
   5342       return;
   5343 
   5344     case DEMANGLE_COMPONENT_CONVERSION:
   5345       d_append_string (dpi, "operator ");
   5346       d_print_conversion (dpi, options, dc);
   5347       return;
   5348 
   5349     case DEMANGLE_COMPONENT_NULLARY:
   5350       d_print_expr_op (dpi, options, d_left (dc));
   5351       return;
   5352 
   5353     case DEMANGLE_COMPONENT_UNARY:
   5354       {
   5355 	struct demangle_component *op = d_left (dc);
   5356 	struct demangle_component *operand = d_right (dc);
   5357 	const char *code = NULL;
   5358 
   5359 	if (op->type == DEMANGLE_COMPONENT_OPERATOR)
   5360 	  {
   5361 	    code = op->u.s_operator.op->code;
   5362 	    if (!strcmp (code, "ad"))
   5363 	      {
   5364 		/* Don't print the argument list for the address of a
   5365 		   function.  */
   5366 		if (operand->type == DEMANGLE_COMPONENT_TYPED_NAME
   5367 		    && d_left (operand)->type == DEMANGLE_COMPONENT_QUAL_NAME
   5368 		    && d_right (operand)->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
   5369 		  operand = d_left (operand);
   5370 	      }
   5371 	    if (operand->type == DEMANGLE_COMPONENT_BINARY_ARGS)
   5372 	      {
   5373 		/* This indicates a suffix operator.  */
   5374 		operand = d_left (operand);
   5375 		d_print_subexpr (dpi, options, operand);
   5376 		d_print_expr_op (dpi, options, op);
   5377 		return;
   5378 	      }
   5379 	  }
   5380 
   5381 	/* For sizeof..., just print the pack length.  */
   5382 	if (code && !strcmp (code, "sZ"))
   5383 	  {
   5384 	    struct demangle_component *a = d_find_pack (dpi, operand);
   5385 	    int len = d_pack_length (a);
   5386 	    d_append_num (dpi, len);
   5387 	    return;
   5388 	  }
   5389 	else if (code && !strcmp (code, "sP"))
   5390 	  {
   5391 	    int len = d_args_length (dpi, operand);
   5392 	    d_append_num (dpi, len);
   5393 	    return;
   5394 	  }
   5395 
   5396 	if (op->type != DEMANGLE_COMPONENT_CAST)
   5397 	  d_print_expr_op (dpi, options, op);
   5398 	else
   5399 	  {
   5400 	    d_append_char (dpi, '(');
   5401 	    d_print_cast (dpi, options, op);
   5402 	    d_append_char (dpi, ')');
   5403 	  }
   5404 	if (code && !strcmp (code, "gs"))
   5405 	  /* Avoid parens after '::'.  */
   5406 	  d_print_comp (dpi, options, operand);
   5407 	else if (code && !strcmp (code, "st"))
   5408 	  /* Always print parens for sizeof (type).  */
   5409 	  {
   5410 	    d_append_char (dpi, '(');
   5411 	    d_print_comp (dpi, options, operand);
   5412 	    d_append_char (dpi, ')');
   5413 	  }
   5414 	else
   5415 	  d_print_subexpr (dpi, options, operand);
   5416       }
   5417       return;
   5418 
   5419     case DEMANGLE_COMPONENT_BINARY:
   5420       if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
   5421 	{
   5422 	  d_print_error (dpi);
   5423 	  return;
   5424 	}
   5425 
   5426       if (op_is_new_cast (d_left (dc)))
   5427 	{
   5428 	  d_print_expr_op (dpi, options, d_left (dc));
   5429 	  d_append_char (dpi, '<');
   5430 	  d_print_comp (dpi, options, d_left (d_right (dc)));
   5431 	  d_append_string (dpi, ">(");
   5432 	  d_print_comp (dpi, options, d_right (d_right (dc)));
   5433 	  d_append_char (dpi, ')');
   5434 	  return;
   5435 	}
   5436 
   5437       if (d_maybe_print_fold_expression (dpi, options, dc))
   5438 	return;
   5439 
   5440       /* We wrap an expression which uses the greater-than operator in
   5441 	 an extra layer of parens so that it does not get confused
   5442 	 with the '>' which ends the template parameters.  */
   5443       if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
   5444 	  && d_left (dc)->u.s_operator.op->len == 1
   5445 	  && d_left (dc)->u.s_operator.op->name[0] == '>')
   5446 	d_append_char (dpi, '(');
   5447 
   5448       if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") == 0
   5449           && d_left (d_right (dc))->type == DEMANGLE_COMPONENT_TYPED_NAME)
   5450 	{
   5451 	  /* Function call used in an expression should not have printed types
   5452 	     of the function arguments.  Values of the function arguments still
   5453 	     get printed below.  */
   5454 
   5455 	  const struct demangle_component *func = d_left (d_right (dc));
   5456 
   5457 	  if (d_right (func)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
   5458 	    d_print_error (dpi);
   5459 	  d_print_subexpr (dpi, options, d_left (func));
   5460 	}
   5461       else
   5462 	d_print_subexpr (dpi, options, d_left (d_right (dc)));
   5463       if (strcmp (d_left (dc)->u.s_operator.op->code, "ix") == 0)
   5464 	{
   5465 	  d_append_char (dpi, '[');
   5466 	  d_print_comp (dpi, options, d_right (d_right (dc)));
   5467 	  d_append_char (dpi, ']');
   5468 	}
   5469       else
   5470 	{
   5471 	  if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") != 0)
   5472 	    d_print_expr_op (dpi, options, d_left (dc));
   5473 	  d_print_subexpr (dpi, options, d_right (d_right (dc)));
   5474 	}
   5475 
   5476       if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
   5477 	  && d_left (dc)->u.s_operator.op->len == 1
   5478 	  && d_left (dc)->u.s_operator.op->name[0] == '>')
   5479 	d_append_char (dpi, ')');
   5480 
   5481       return;
   5482 
   5483     case DEMANGLE_COMPONENT_BINARY_ARGS:
   5484       /* We should only see this as part of DEMANGLE_COMPONENT_BINARY.  */
   5485       d_print_error (dpi);
   5486       return;
   5487 
   5488     case DEMANGLE_COMPONENT_TRINARY:
   5489       if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
   5490 	  || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
   5491 	{
   5492 	  d_print_error (dpi);
   5493 	  return;
   5494 	}
   5495       if (d_maybe_print_fold_expression (dpi, options, dc))
   5496 	return;
   5497       {
   5498 	struct demangle_component *op = d_left (dc);
   5499 	struct demangle_component *first = d_left (d_right (dc));
   5500 	struct demangle_component *second = d_left (d_right (d_right (dc)));
   5501 	struct demangle_component *third = d_right (d_right (d_right (dc)));
   5502 
   5503 	if (!strcmp (op->u.s_operator.op->code, "qu"))
   5504 	  {
   5505 	    d_print_subexpr (dpi, options, first);
   5506 	    d_print_expr_op (dpi, options, op);
   5507 	    d_print_subexpr (dpi, options, second);
   5508 	    d_append_string (dpi, " : ");
   5509 	    d_print_subexpr (dpi, options, third);
   5510 	  }
   5511 	else
   5512 	  {
   5513 	    d_append_string (dpi, "new ");
   5514 	    if (d_left (first) != NULL)
   5515 	      {
   5516 		d_print_subexpr (dpi, options, first);
   5517 		d_append_char (dpi, ' ');
   5518 	      }
   5519 	    d_print_comp (dpi, options, second);
   5520 	    if (third)
   5521 	      d_print_subexpr (dpi, options, third);
   5522 	  }
   5523       }
   5524       return;
   5525 
   5526     case DEMANGLE_COMPONENT_TRINARY_ARG1:
   5527     case DEMANGLE_COMPONENT_TRINARY_ARG2:
   5528       /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY.  */
   5529       d_print_error (dpi);
   5530       return;
   5531 
   5532     case DEMANGLE_COMPONENT_LITERAL:
   5533     case DEMANGLE_COMPONENT_LITERAL_NEG:
   5534       {
   5535 	enum d_builtin_type_print tp;
   5536 
   5537 	/* For some builtin types, produce simpler output.  */
   5538 	tp = D_PRINT_DEFAULT;
   5539 	if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
   5540 	  {
   5541 	    tp = d_left (dc)->u.s_builtin.type->print;
   5542 	    switch (tp)
   5543 	      {
   5544 	      case D_PRINT_INT:
   5545 	      case D_PRINT_UNSIGNED:
   5546 	      case D_PRINT_LONG:
   5547 	      case D_PRINT_UNSIGNED_LONG:
   5548 	      case D_PRINT_LONG_LONG:
   5549 	      case D_PRINT_UNSIGNED_LONG_LONG:
   5550 		if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
   5551 		  {
   5552 		    if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
   5553 		      d_append_char (dpi, '-');
   5554 		    d_print_comp (dpi, options, d_right (dc));
   5555 		    switch (tp)
   5556 		      {
   5557 		      default:
   5558 			break;
   5559 		      case D_PRINT_UNSIGNED:
   5560 			d_append_char (dpi, 'u');
   5561 			break;
   5562 		      case D_PRINT_LONG:
   5563 			d_append_char (dpi, 'l');
   5564 			break;
   5565 		      case D_PRINT_UNSIGNED_LONG:
   5566 			d_append_string (dpi, "ul");
   5567 			break;
   5568 		      case D_PRINT_LONG_LONG:
   5569 			d_append_string (dpi, "ll");
   5570 			break;
   5571 		      case D_PRINT_UNSIGNED_LONG_LONG:
   5572 			d_append_string (dpi, "ull");
   5573 			break;
   5574 		      }
   5575 		    return;
   5576 		  }
   5577 		break;
   5578 
   5579 	      case D_PRINT_BOOL:
   5580 		if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
   5581 		    && d_right (dc)->u.s_name.len == 1
   5582 		    && dc->type == DEMANGLE_COMPONENT_LITERAL)
   5583 		  {
   5584 		    switch (d_right (dc)->u.s_name.s[0])
   5585 		      {
   5586 		      case '0':
   5587 			d_append_string (dpi, "false");
   5588 			return;
   5589 		      case '1':
   5590 			d_append_string (dpi, "true");
   5591 			return;
   5592 		      default:
   5593 			break;
   5594 		      }
   5595 		  }
   5596 		break;
   5597 
   5598 	      default:
   5599 		break;
   5600 	      }
   5601 	  }
   5602 
   5603 	d_append_char (dpi, '(');
   5604 	d_print_comp (dpi, options, d_left (dc));
   5605 	d_append_char (dpi, ')');
   5606 	if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
   5607 	  d_append_char (dpi, '-');
   5608 	if (tp == D_PRINT_FLOAT)
   5609 	  d_append_char (dpi, '[');
   5610 	d_print_comp (dpi, options, d_right (dc));
   5611 	if (tp == D_PRINT_FLOAT)
   5612 	  d_append_char (dpi, ']');
   5613       }
   5614       return;
   5615 
   5616     case DEMANGLE_COMPONENT_NUMBER:
   5617       d_append_num (dpi, dc->u.s_number.number);
   5618       return;
   5619 
   5620     case DEMANGLE_COMPONENT_JAVA_RESOURCE:
   5621       d_append_string (dpi, "java resource ");
   5622       d_print_comp (dpi, options, d_left (dc));
   5623       return;
   5624 
   5625     case DEMANGLE_COMPONENT_COMPOUND_NAME:
   5626       d_print_comp (dpi, options, d_left (dc));
   5627       d_print_comp (dpi, options, d_right (dc));
   5628       return;
   5629 
   5630     case DEMANGLE_COMPONENT_CHARACTER:
   5631       d_append_char (dpi, dc->u.s_character.character);
   5632       return;
   5633 
   5634     case DEMANGLE_COMPONENT_DECLTYPE:
   5635       d_append_string (dpi, "decltype (");
   5636       d_print_comp (dpi, options, d_left (dc));
   5637       d_append_char (dpi, ')');
   5638       return;
   5639 
   5640     case DEMANGLE_COMPONENT_PACK_EXPANSION:
   5641       {
   5642 	int len;
   5643 	int i;
   5644 	struct demangle_component *a = d_find_pack (dpi, d_left (dc));
   5645 	if (a == NULL)
   5646 	  {
   5647 	    /* d_find_pack won't find anything if the only packs involved
   5648 	       in this expansion are function parameter packs; in that
   5649 	       case, just print the pattern and "...".  */
   5650 	    d_print_subexpr (dpi, options, d_left (dc));
   5651 	    d_append_string (dpi, "...");
   5652 	    return;
   5653 	  }
   5654 
   5655 	len = d_pack_length (a);
   5656 	dc = d_left (dc);
   5657 	for (i = 0; i < len; ++i)
   5658 	  {
   5659 	    dpi->pack_index = i;
   5660 	    d_print_comp (dpi, options, dc);
   5661 	    if (i < len-1)
   5662 	      d_append_string (dpi, ", ");
   5663 	  }
   5664       }
   5665       return;
   5666 
   5667     case DEMANGLE_COMPONENT_FUNCTION_PARAM:
   5668       {
   5669 	long num = dc->u.s_number.number;
   5670 	if (num == 0)
   5671 	  d_append_string (dpi, "this");
   5672 	else
   5673 	  {
   5674 	    d_append_string (dpi, "{parm#");
   5675 	    d_append_num (dpi, num);
   5676 	    d_append_char (dpi, '}');
   5677 	  }
   5678       }
   5679       return;
   5680 
   5681     case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
   5682       d_append_string (dpi, "global constructors keyed to ");
   5683       d_print_comp (dpi, options, dc->u.s_binary.left);
   5684       return;
   5685 
   5686     case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
   5687       d_append_string (dpi, "global destructors keyed to ");
   5688       d_print_comp (dpi, options, dc->u.s_binary.left);
   5689       return;
   5690 
   5691     case DEMANGLE_COMPONENT_LAMBDA:
   5692       d_append_string (dpi, "{lambda(");
   5693       /* Generic lambda auto parms are mangled as the template type
   5694 	 parm they are.  */
   5695       dpi->is_lambda_arg++;
   5696       d_print_comp (dpi, options, dc->u.s_unary_num.sub);
   5697       dpi->is_lambda_arg--;
   5698       d_append_string (dpi, ")#");
   5699       d_append_num (dpi, dc->u.s_unary_num.num + 1);
   5700       d_append_char (dpi, '}');
   5701       return;
   5702 
   5703     case DEMANGLE_COMPONENT_UNNAMED_TYPE:
   5704       d_append_string (dpi, "{unnamed type#");
   5705       d_append_num (dpi, dc->u.s_number.number + 1);
   5706       d_append_char (dpi, '}');
   5707       return;
   5708 
   5709     case DEMANGLE_COMPONENT_CLONE:
   5710       d_print_comp (dpi, options, d_left (dc));
   5711       d_append_string (dpi, " [clone ");
   5712       d_print_comp (dpi, options, d_right (dc));
   5713       d_append_char (dpi, ']');
   5714       return;
   5715 
   5716     default:
   5717       d_print_error (dpi);
   5718       return;
   5719     }
   5720 }
   5721 
   5722 static void
   5723 d_print_comp (struct d_print_info *dpi, int options,
   5724 	      struct demangle_component *dc)
   5725 {
   5726   struct d_component_stack self;
   5727   if (dc == NULL || dc->d_printing > 1)
   5728     {
   5729       d_print_error (dpi);
   5730       return;
   5731     }
   5732   else
   5733     dc->d_printing++;
   5734 
   5735   self.dc = dc;
   5736   self.parent = dpi->component_stack;
   5737   dpi->component_stack = &self;
   5738 
   5739   d_print_comp_inner (dpi, options, dc);
   5740 
   5741   dpi->component_stack = self.parent;
   5742   dc->d_printing--;
   5743 }
   5744 
   5745 /* Print a Java dentifier.  For Java we try to handle encoded extended
   5746    Unicode characters.  The C++ ABI doesn't mention Unicode encoding,
   5747    so we don't it for C++.  Characters are encoded as
   5748    __U<hex-char>+_.  */
   5749 
   5750 static void
   5751 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
   5752 {
   5753   const char *p;
   5754   const char *end;
   5755 
   5756   end = name + len;
   5757   for (p = name; p < end; ++p)
   5758     {
   5759       if (end - p > 3
   5760 	  && p[0] == '_'
   5761 	  && p[1] == '_'
   5762 	  && p[2] == 'U')
   5763 	{
   5764 	  unsigned long c;
   5765 	  const char *q;
   5766 
   5767 	  c = 0;
   5768 	  for (q = p + 3; q < end; ++q)
   5769 	    {
   5770 	      int dig;
   5771 
   5772 	      if (IS_DIGIT (*q))
   5773 		dig = *q - '0';
   5774 	      else if (*q >= 'A' && *q <= 'F')
   5775 		dig = *q - 'A' + 10;
   5776 	      else if (*q >= 'a' && *q <= 'f')
   5777 		dig = *q - 'a' + 10;
   5778 	      else
   5779 		break;
   5780 
   5781 	      c = c * 16 + dig;
   5782 	    }
   5783 	  /* If the Unicode character is larger than 256, we don't try
   5784 	     to deal with it here.  FIXME.  */
   5785 	  if (q < end && *q == '_' && c < 256)
   5786 	    {
   5787 	      d_append_char (dpi, c);
   5788 	      p = q;
   5789 	      continue;
   5790 	    }
   5791 	}
   5792 
   5793       d_append_char (dpi, *p);
   5794     }
   5795 }
   5796 
   5797 /* Print a list of modifiers.  SUFFIX is 1 if we are printing
   5798    qualifiers on this after printing a function.  */
   5799 
   5800 static void
   5801 d_print_mod_list (struct d_print_info *dpi, int options,
   5802                   struct d_print_mod *mods, int suffix)
   5803 {
   5804   struct d_print_template *hold_dpt;
   5805 
   5806   if (mods == NULL || d_print_saw_error (dpi))
   5807     return;
   5808 
   5809   if (mods->printed
   5810       || (! suffix
   5811 	  && (is_fnqual_component_type (mods->mod->type))))
   5812     {
   5813       d_print_mod_list (dpi, options, mods->next, suffix);
   5814       return;
   5815     }
   5816 
   5817   mods->printed = 1;
   5818 
   5819   hold_dpt = dpi->templates;
   5820   dpi->templates = mods->templates;
   5821 
   5822   if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
   5823     {
   5824       d_print_function_type (dpi, options, mods->mod, mods->next);
   5825       dpi->templates = hold_dpt;
   5826       return;
   5827     }
   5828   else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
   5829     {
   5830       d_print_array_type (dpi, options, mods->mod, mods->next);
   5831       dpi->templates = hold_dpt;
   5832       return;
   5833     }
   5834   else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
   5835     {
   5836       struct d_print_mod *hold_modifiers;
   5837       struct demangle_component *dc;
   5838 
   5839       /* When this is on the modifier stack, we have pulled any
   5840 	 qualifiers off the right argument already.  Otherwise, we
   5841 	 print it as usual, but don't let the left argument see any
   5842 	 modifiers.  */
   5843 
   5844       hold_modifiers = dpi->modifiers;
   5845       dpi->modifiers = NULL;
   5846       d_print_comp (dpi, options, d_left (mods->mod));
   5847       dpi->modifiers = hold_modifiers;
   5848 
   5849       if ((options & DMGL_JAVA) == 0)
   5850 	d_append_string (dpi, "::");
   5851       else
   5852 	d_append_char (dpi, '.');
   5853 
   5854       dc = d_right (mods->mod);
   5855 
   5856       if (dc->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
   5857 	{
   5858 	  d_append_string (dpi, "{default arg#");
   5859 	  d_append_num (dpi, dc->u.s_unary_num.num + 1);
   5860 	  d_append_string (dpi, "}::");
   5861 	  dc = dc->u.s_unary_num.sub;
   5862 	}
   5863 
   5864       while (is_fnqual_component_type (dc->type))
   5865 	dc = d_left (dc);
   5866 
   5867       d_print_comp (dpi, options, dc);
   5868 
   5869       dpi->templates = hold_dpt;
   5870       return;
   5871     }
   5872 
   5873   d_print_mod (dpi, options, mods->mod);
   5874 
   5875   dpi->templates = hold_dpt;
   5876 
   5877   d_print_mod_list (dpi, options, mods->next, suffix);
   5878 }
   5879 
   5880 /* Print a modifier.  */
   5881 
   5882 static void
   5883 d_print_mod (struct d_print_info *dpi, int options,
   5884              struct demangle_component *mod)
   5885 {
   5886   switch (mod->type)
   5887     {
   5888     case DEMANGLE_COMPONENT_RESTRICT:
   5889     case DEMANGLE_COMPONENT_RESTRICT_THIS:
   5890       d_append_string (dpi, " restrict");
   5891       return;
   5892     case DEMANGLE_COMPONENT_VOLATILE:
   5893     case DEMANGLE_COMPONENT_VOLATILE_THIS:
   5894       d_append_string (dpi, " volatile");
   5895       return;
   5896     case DEMANGLE_COMPONENT_CONST:
   5897     case DEMANGLE_COMPONENT_CONST_THIS:
   5898       d_append_string (dpi, " const");
   5899       return;
   5900     case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
   5901       d_append_string (dpi, " transaction_safe");
   5902       return;
   5903     case DEMANGLE_COMPONENT_NOEXCEPT:
   5904       d_append_string (dpi, " noexcept");
   5905       if (d_right (mod))
   5906 	{
   5907 	  d_append_char (dpi, '(');
   5908 	  d_print_comp (dpi, options, d_right (mod));
   5909 	  d_append_char (dpi, ')');
   5910 	}
   5911       return;
   5912     case DEMANGLE_COMPONENT_THROW_SPEC:
   5913       d_append_string (dpi, " throw");
   5914       if (d_right (mod))
   5915 	{
   5916 	  d_append_char (dpi, '(');
   5917 	  d_print_comp (dpi, options, d_right (mod));
   5918 	  d_append_char (dpi, ')');
   5919 	}
   5920       return;
   5921     case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
   5922       d_append_char (dpi, ' ');
   5923       d_print_comp (dpi, options, d_right (mod));
   5924       return;
   5925     case DEMANGLE_COMPONENT_POINTER:
   5926       /* There is no pointer symbol in Java.  */
   5927       if ((options & DMGL_JAVA) == 0)
   5928 	d_append_char (dpi, '*');
   5929       return;
   5930     case DEMANGLE_COMPONENT_REFERENCE_THIS:
   5931       /* For the ref-qualifier, put a space before the &.  */
   5932       d_append_char (dpi, ' ');
   5933       /* FALLTHRU */
   5934     case DEMANGLE_COMPONENT_REFERENCE:
   5935       d_append_char (dpi, '&');
   5936       return;
   5937     case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
   5938       d_append_char (dpi, ' ');
   5939       /* FALLTHRU */
   5940     case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
   5941       d_append_string (dpi, "&&");
   5942       return;
   5943     case DEMANGLE_COMPONENT_COMPLEX:
   5944       d_append_string (dpi, "complex ");
   5945       return;
   5946     case DEMANGLE_COMPONENT_IMAGINARY:
   5947       d_append_string (dpi, "imaginary ");
   5948       return;
   5949     case DEMANGLE_COMPONENT_PTRMEM_TYPE:
   5950       if (d_last_char (dpi) != '(')
   5951 	d_append_char (dpi, ' ');
   5952       d_print_comp (dpi, options, d_left (mod));
   5953       d_append_string (dpi, "::*");
   5954       return;
   5955     case DEMANGLE_COMPONENT_TYPED_NAME:
   5956       d_print_comp (dpi, options, d_left (mod));
   5957       return;
   5958     case DEMANGLE_COMPONENT_VECTOR_TYPE:
   5959       d_append_string (dpi, " __vector(");
   5960       d_print_comp (dpi, options, d_left (mod));
   5961       d_append_char (dpi, ')');
   5962       return;
   5963 
   5964     default:
   5965       /* Otherwise, we have something that won't go back on the
   5966 	 modifier stack, so we can just print it.  */
   5967       d_print_comp (dpi, options, mod);
   5968       return;
   5969     }
   5970 }
   5971 
   5972 /* Print a function type, except for the return type.  */
   5973 
   5974 static void
   5975 d_print_function_type (struct d_print_info *dpi, int options,
   5976                        struct demangle_component *dc,
   5977                        struct d_print_mod *mods)
   5978 {
   5979   int need_paren;
   5980   int need_space;
   5981   struct d_print_mod *p;
   5982   struct d_print_mod *hold_modifiers;
   5983 
   5984   need_paren = 0;
   5985   need_space = 0;
   5986   for (p = mods; p != NULL; p = p->next)
   5987     {
   5988       if (p->printed)
   5989 	break;
   5990 
   5991       switch (p->mod->type)
   5992 	{
   5993 	case DEMANGLE_COMPONENT_POINTER:
   5994 	case DEMANGLE_COMPONENT_REFERENCE:
   5995 	case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
   5996 	  need_paren = 1;
   5997 	  break;
   5998 	case DEMANGLE_COMPONENT_RESTRICT:
   5999 	case DEMANGLE_COMPONENT_VOLATILE:
   6000 	case DEMANGLE_COMPONENT_CONST:
   6001 	case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
   6002 	case DEMANGLE_COMPONENT_COMPLEX:
   6003 	case DEMANGLE_COMPONENT_IMAGINARY:
   6004 	case DEMANGLE_COMPONENT_PTRMEM_TYPE:
   6005 	  need_space = 1;
   6006 	  need_paren = 1;
   6007 	  break;
   6008 	FNQUAL_COMPONENT_CASE:
   6009 	  break;
   6010 	default:
   6011 	  break;
   6012 	}
   6013       if (need_paren)
   6014 	break;
   6015     }
   6016 
   6017   if (need_paren)
   6018     {
   6019       if (! need_space)
   6020 	{
   6021 	  if (d_last_char (dpi) != '('
   6022 	      && d_last_char (dpi) != '*')
   6023 	    need_space = 1;
   6024 	}
   6025       if (need_space && d_last_char (dpi) != ' ')
   6026 	d_append_char (dpi, ' ');
   6027       d_append_char (dpi, '(');
   6028     }
   6029 
   6030   hold_modifiers = dpi->modifiers;
   6031   dpi->modifiers = NULL;
   6032 
   6033   d_print_mod_list (dpi, options, mods, 0);
   6034 
   6035   if (need_paren)
   6036     d_append_char (dpi, ')');
   6037 
   6038   d_append_char (dpi, '(');
   6039 
   6040   if (d_right (dc) != NULL)
   6041     d_print_comp (dpi, options, d_right (dc));
   6042 
   6043   d_append_char (dpi, ')');
   6044 
   6045   d_print_mod_list (dpi, options, mods, 1);
   6046 
   6047   dpi->modifiers = hold_modifiers;
   6048 }
   6049 
   6050 /* Print an array type, except for the element type.  */
   6051 
   6052 static void
   6053 d_print_array_type (struct d_print_info *dpi, int options,
   6054                     struct demangle_component *dc,
   6055                     struct d_print_mod *mods)
   6056 {
   6057   int need_space;
   6058 
   6059   need_space = 1;
   6060   if (mods != NULL)
   6061     {
   6062       int need_paren;
   6063       struct d_print_mod *p;
   6064 
   6065       need_paren = 0;
   6066       for (p = mods; p != NULL; p = p->next)
   6067 	{
   6068 	  if (! p->printed)
   6069 	    {
   6070 	      if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
   6071 		{
   6072 		  need_space = 0;
   6073 		  break;
   6074 		}
   6075 	      else
   6076 		{
   6077 		  need_paren = 1;
   6078 		  need_space = 1;
   6079 		  break;
   6080 		}
   6081 	    }
   6082 	}
   6083 
   6084       if (need_paren)
   6085 	d_append_string (dpi, " (");
   6086 
   6087       d_print_mod_list (dpi, options, mods, 0);
   6088 
   6089       if (need_paren)
   6090 	d_append_char (dpi, ')');
   6091     }
   6092 
   6093   if (need_space)
   6094     d_append_char (dpi, ' ');
   6095 
   6096   d_append_char (dpi, '[');
   6097 
   6098   if (d_left (dc) != NULL)
   6099     d_print_comp (dpi, options, d_left (dc));
   6100 
   6101   d_append_char (dpi, ']');
   6102 }
   6103 
   6104 /* Print an operator in an expression.  */
   6105 
   6106 static void
   6107 d_print_expr_op (struct d_print_info *dpi, int options,
   6108                  struct demangle_component *dc)
   6109 {
   6110   if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
   6111     d_append_buffer (dpi, dc->u.s_operator.op->name,
   6112 		     dc->u.s_operator.op->len);
   6113   else
   6114     d_print_comp (dpi, options, dc);
   6115 }
   6116 
   6117 /* Print a cast.  */
   6118 
   6119 static void
   6120 d_print_cast (struct d_print_info *dpi, int options,
   6121 	      struct demangle_component *dc)
   6122 {
   6123   d_print_comp (dpi, options, d_left (dc));
   6124 }
   6125 
   6126 /* Print a conversion operator.  */
   6127 
   6128 static void
   6129 d_print_conversion (struct d_print_info *dpi, int options,
   6130 		    struct demangle_component *dc)
   6131 {
   6132   struct d_print_template dpt;
   6133 
   6134   /* For a conversion operator, we need the template parameters from
   6135      the enclosing template in scope for processing the type.  */
   6136   if (dpi->current_template != NULL)
   6137     {
   6138       dpt.next = dpi->templates;
   6139       dpi->templates = &dpt;
   6140       dpt.template_decl = dpi->current_template;
   6141     }
   6142 
   6143   if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
   6144     {
   6145       d_print_comp (dpi, options, d_left (dc));
   6146       if (dpi->current_template != NULL)
   6147 	dpi->templates = dpt.next;
   6148     }
   6149   else
   6150     {
   6151       d_print_comp (dpi, options, d_left (d_left (dc)));
   6152 
   6153       /* For a templated cast operator, we need to remove the template
   6154 	 parameters from scope after printing the operator name,
   6155 	 so we need to handle the template printing here.  */
   6156       if (dpi->current_template != NULL)
   6157 	dpi->templates = dpt.next;
   6158 
   6159       if (d_last_char (dpi) == '<')
   6160 	d_append_char (dpi, ' ');
   6161       d_append_char (dpi, '<');
   6162       d_print_comp (dpi, options, d_right (d_left (dc)));
   6163       /* Avoid generating two consecutive '>' characters, to avoid
   6164 	 the C++ syntactic ambiguity.  */
   6165       if (d_last_char (dpi) == '>')
   6166 	d_append_char (dpi, ' ');
   6167       d_append_char (dpi, '>');
   6168     }
   6169 }
   6170 
   6171 /* Initialize the information structure we use to pass around
   6172    information.  */
   6173 
   6174 CP_STATIC_IF_GLIBCPP_V3
   6175 void
   6176 cplus_demangle_init_info (const char *mangled, int options, size_t len,
   6177                           struct d_info *di)
   6178 {
   6179   di->s = mangled;
   6180   di->send = mangled + len;
   6181   di->options = options;
   6182 
   6183   di->n = mangled;
   6184 
   6185   /* We can not need more components than twice the number of chars in
   6186      the mangled string.  Most components correspond directly to
   6187      chars, but the ARGLIST types are exceptions.  */
   6188   di->num_comps = 2 * len;
   6189   di->next_comp = 0;
   6190 
   6191   /* Similarly, we can not need more substitutions than there are
   6192      chars in the mangled string.  */
   6193   di->num_subs = len;
   6194   di->next_sub = 0;
   6195   di->did_subs = 0;
   6196 
   6197   di->last_name = NULL;
   6198 
   6199   di->expansion = 0;
   6200   di->is_expression = 0;
   6201   di->is_conversion = 0;
   6202 }
   6203 
   6204 /* Internal implementation for the demangler.  If MANGLED is a g++ v3 ABI
   6205    mangled name, return strings in repeated callback giving the demangled
   6206    name.  OPTIONS is the usual libiberty demangler options.  On success,
   6207    this returns 1.  On failure, returns 0.  */
   6208 
   6209 static int
   6210 d_demangle_callback (const char *mangled, int options,
   6211                      demangle_callbackref callback, void *opaque)
   6212 {
   6213   enum
   6214     {
   6215       DCT_TYPE,
   6216       DCT_MANGLED,
   6217       DCT_GLOBAL_CTORS,
   6218       DCT_GLOBAL_DTORS
   6219     }
   6220   type;
   6221   struct d_info di;
   6222   struct demangle_component *dc;
   6223   int status;
   6224 
   6225   if (mangled[0] == '_' && mangled[1] == 'Z')
   6226     type = DCT_MANGLED;
   6227   else if (strncmp (mangled, "_GLOBAL_", 8) == 0
   6228 	   && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
   6229 	   && (mangled[9] == 'D' || mangled[9] == 'I')
   6230 	   && mangled[10] == '_')
   6231     type = mangled[9] == 'I' ? DCT_GLOBAL_CTORS : DCT_GLOBAL_DTORS;
   6232   else
   6233     {
   6234       if ((options & DMGL_TYPES) == 0)
   6235 	return 0;
   6236       type = DCT_TYPE;
   6237     }
   6238 
   6239   cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
   6240 
   6241   {
   6242 #if 0 /* in valgrind */
   6243 #ifdef CP_DYNAMIC_ARRAYS
   6244     __extension__ struct demangle_component comps[di.num_comps];
   6245     __extension__ struct demangle_component *subs[di.num_subs];
   6246 
   6247     di.comps = comps;
   6248     di.subs = subs;
   6249 #else
   6250     di.comps = alloca (di.num_comps * sizeof (*di.comps));
   6251     di.subs = alloca (di.num_subs * sizeof (*di.subs));
   6252 #endif
   6253 #else
   6254     /* Allocate memory dynamically to avoid VLAs as valgrind stack
   6255        is a scarce resource */
   6256     di.comps = xmalloc (di.num_comps * sizeof (*di.comps));
   6257     di.subs = xmalloc (di.num_subs * sizeof (*di.subs));
   6258 #endif /* ! in valgrind */
   6259 
   6260     switch (type)
   6261       {
   6262       case DCT_TYPE:
   6263 	dc = cplus_demangle_type (&di);
   6264 	break;
   6265       case DCT_MANGLED:
   6266 	dc = cplus_demangle_mangled_name (&di, 1);
   6267 	break;
   6268       case DCT_GLOBAL_CTORS:
   6269       case DCT_GLOBAL_DTORS:
   6270 	d_advance (&di, 11);
   6271 	dc = d_make_comp (&di,
   6272 			  (type == DCT_GLOBAL_CTORS
   6273 			   ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
   6274 			   : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS),
   6275 			  d_make_demangle_mangled_name (&di, d_str (&di)),
   6276 			  NULL);
   6277 	d_advance (&di, strlen (d_str (&di)));
   6278 	break;
   6279       default:
   6280 	abort (); /* We have listed all the cases.  */
   6281       }
   6282 
   6283     /* If DMGL_PARAMS is set, then if we didn't consume the entire
   6284        mangled string, then we didn't successfully demangle it.  If
   6285        DMGL_PARAMS is not set, we didn't look at the trailing
   6286        parameters.  */
   6287     if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
   6288       dc = NULL;
   6289 
   6290 #ifdef CP_DEMANGLE_DEBUG
   6291     d_dump (dc, 0);
   6292 #endif
   6293 
   6294     status = (dc != NULL)
   6295              ? cplus_demangle_print_callback (options, dc, callback, opaque)
   6296              : 0;
   6297   }
   6298 
   6299 #if 0 /* in valgrind */
   6300 #else
   6301   free (di.comps);
   6302   free (di.subs);
   6303 #endif  /* in valgrind */
   6304 
   6305   return status;
   6306 }
   6307 
   6308 /* Entry point for the demangler.  If MANGLED is a g++ v3 ABI mangled
   6309    name, return a buffer allocated with malloc holding the demangled
   6310    name.  OPTIONS is the usual libiberty demangler options.  On
   6311    success, this sets *PALC to the allocated size of the returned
   6312    buffer.  On failure, this sets *PALC to 0 for a bad name, or 1 for
   6313    a memory allocation failure, and returns NULL.  */
   6314 
   6315 static char *
   6316 d_demangle (const char *mangled, int options, size_t *palc)
   6317 {
   6318   struct d_growable_string dgs;
   6319   int status;
   6320 
   6321   d_growable_string_init (&dgs, 0);
   6322 
   6323   status = d_demangle_callback (mangled, options,
   6324                                 d_growable_string_callback_adapter, &dgs);
   6325   if (status == 0)
   6326     {
   6327       free (dgs.buf);
   6328       *palc = 0;
   6329       return NULL;
   6330     }
   6331 
   6332   *palc = dgs.allocation_failure ? 1 : dgs.alc;
   6333   return dgs.buf;
   6334 }
   6335 
   6336 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
   6337 
   6338 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
   6339 
   6340 /* ia64 ABI-mandated entry point in the C++ runtime library for
   6341    performing demangling.  MANGLED_NAME is a NUL-terminated character
   6342    string containing the name to be demangled.
   6343 
   6344    OUTPUT_BUFFER is a region of memory, allocated with malloc, of
   6345    *LENGTH bytes, into which the demangled name is stored.  If
   6346    OUTPUT_BUFFER is not long enough, it is expanded using realloc.
   6347    OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
   6348    is placed in a region of memory allocated with malloc.
   6349 
   6350    If LENGTH is non-NULL, the length of the buffer containing the
   6351    demangled name, is placed in *LENGTH.
   6352 
   6353    The return value is a pointer to the start of the NUL-terminated
   6354    demangled name, or NULL if the demangling fails.  The caller is
   6355    responsible for deallocating this memory using free.
   6356 
   6357    *STATUS is set to one of the following values:
   6358       0: The demangling operation succeeded.
   6359      -1: A memory allocation failure occurred.
   6360      -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
   6361      -3: One of the arguments is invalid.
   6362 
   6363    The demangling is performed using the C++ ABI mangling rules, with
   6364    GNU extensions.  */
   6365 
   6366 char *
   6367 __cxa_demangle (const char *mangled_name, char *output_buffer,
   6368                 size_t *length, int *status)
   6369 {
   6370   char *demangled;
   6371   size_t alc;
   6372 
   6373   if (mangled_name == NULL)
   6374     {
   6375       if (status != NULL)
   6376 	*status = -3;
   6377       return NULL;
   6378     }
   6379 
   6380   if (output_buffer != NULL && length == NULL)
   6381     {
   6382       if (status != NULL)
   6383 	*status = -3;
   6384       return NULL;
   6385     }
   6386 
   6387   demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
   6388 
   6389   if (demangled == NULL)
   6390     {
   6391       if (status != NULL)
   6392 	{
   6393 	  if (alc == 1)
   6394 	    *status = -1;
   6395 	  else
   6396 	    *status = -2;
   6397 	}
   6398       return NULL;
   6399     }
   6400 
   6401   if (output_buffer == NULL)
   6402     {
   6403       if (length != NULL)
   6404 	*length = alc;
   6405     }
   6406   else
   6407     {
   6408       if (strlen (demangled) < *length)
   6409 	{
   6410 	  strcpy (output_buffer, demangled);
   6411 	  free (demangled);
   6412 	  demangled = output_buffer;
   6413 	}
   6414       else
   6415 	{
   6416 	  free (output_buffer);
   6417 	  *length = alc;
   6418 	}
   6419     }
   6420 
   6421   if (status != NULL)
   6422     *status = 0;
   6423 
   6424   return demangled;
   6425 }
   6426 
   6427 extern int __gcclibcxx_demangle_callback (const char *,
   6428                                           void (*)
   6429                                             (const char *, size_t, void *),
   6430                                           void *);
   6431 
   6432 /* Alternative, allocationless entry point in the C++ runtime library
   6433    for performing demangling.  MANGLED_NAME is a NUL-terminated character
   6434    string containing the name to be demangled.
   6435 
   6436    CALLBACK is a callback function, called with demangled string
   6437    segments as demangling progresses; it is called at least once,
   6438    but may be called more than once.  OPAQUE is a generalized pointer
   6439    used as a callback argument.
   6440 
   6441    The return code is one of the following values, equivalent to
   6442    the STATUS values of __cxa_demangle() (excluding -1, since this
   6443    function performs no memory allocations):
   6444       0: The demangling operation succeeded.
   6445      -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
   6446      -3: One of the arguments is invalid.
   6447 
   6448    The demangling is performed using the C++ ABI mangling rules, with
   6449    GNU extensions.  */
   6450 
   6451 int
   6452 __gcclibcxx_demangle_callback (const char *mangled_name,
   6453                                void (*callback) (const char *, size_t, void *),
   6454                                void *opaque)
   6455 {
   6456   int status;
   6457 
   6458   if (mangled_name == NULL || callback == NULL)
   6459     return -3;
   6460 
   6461   status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES,
   6462                                 callback, opaque);
   6463   if (status == 0)
   6464     return -2;
   6465 
   6466   return 0;
   6467 }
   6468 
   6469 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
   6470 
   6471 /* Entry point for libiberty demangler.  If MANGLED is a g++ v3 ABI
   6472    mangled name, return a buffer allocated with malloc holding the
   6473    demangled name.  Otherwise, return NULL.  */
   6474 
   6475 char *
   6476 cplus_demangle_v3 (const char *mangled, int options)
   6477 {
   6478   size_t alc;
   6479 
   6480   return d_demangle (mangled, options, &alc);
   6481 }
   6482 
   6483 int
   6484 cplus_demangle_v3_callback (const char *mangled, int options,
   6485                             demangle_callbackref callback, void *opaque)
   6486 {
   6487   return d_demangle_callback (mangled, options, callback, opaque);
   6488 }
   6489 
   6490 /* Demangle a Java symbol.  Java uses a subset of the V3 ABI C++ mangling
   6491    conventions, but the output formatting is a little different.
   6492    This instructs the C++ demangler not to emit pointer characters ("*"), to
   6493    use Java's namespace separator symbol ("." instead of "::"), and to output
   6494    JArray<TYPE> as TYPE[].  */
   6495 
   6496 char *
   6497 java_demangle_v3 (const char *mangled)
   6498 {
   6499   size_t alc;
   6500 
   6501   return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
   6502 }
   6503 
   6504 int
   6505 java_demangle_v3_callback (const char *mangled,
   6506                            demangle_callbackref callback, void *opaque)
   6507 {
   6508   return d_demangle_callback (mangled,
   6509                               DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
   6510                               callback, opaque);
   6511 }
   6512 
   6513 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
   6514 
   6515 #ifndef IN_GLIBCPP_V3
   6516 
   6517 /* Demangle a string in order to find out whether it is a constructor
   6518    or destructor.  Return non-zero on success.  Set *CTOR_KIND and
   6519    *DTOR_KIND appropriately.  */
   6520 
   6521 static int
   6522 is_ctor_or_dtor (const char *mangled,
   6523                  enum gnu_v3_ctor_kinds *ctor_kind,
   6524                  enum gnu_v3_dtor_kinds *dtor_kind)
   6525 {
   6526   struct d_info di;
   6527   struct demangle_component *dc;
   6528   int ret;
   6529 
   6530   *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
   6531   *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
   6532 
   6533   cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
   6534 
   6535   {
   6536 #if 0 /* in valgrind */
   6537 #ifdef CP_DYNAMIC_ARRAYS
   6538     __extension__ struct demangle_component comps[di.num_comps];
   6539     __extension__ struct demangle_component *subs[di.num_subs];
   6540 
   6541     di.comps = comps;
   6542     di.subs = subs;
   6543 #else
   6544     di.comps = alloca (di.num_comps * sizeof (*di.comps));
   6545     di.subs = alloca (di.num_subs * sizeof (*di.subs));
   6546 #endif
   6547 #else
   6548     /* Allocate memory dynamically to avoid VLAs as valgrind stack
   6549        is a scarce resource */
   6550     di.comps = xmalloc (di.num_comps * sizeof (*di.comps));
   6551     di.subs = xmalloc (di.num_subs * sizeof (*di.subs));
   6552 #endif /* ! in valgrind */
   6553     dc = cplus_demangle_mangled_name (&di, 1);
   6554 
   6555     /* Note that because we did not pass DMGL_PARAMS, we don't expect
   6556        to demangle the entire string.  */
   6557 
   6558     ret = 0;
   6559     while (dc != NULL)
   6560       {
   6561 	switch (dc->type)
   6562 	  {
   6563 	    /* These cannot appear on a constructor or destructor.  */
   6564 	  case DEMANGLE_COMPONENT_RESTRICT_THIS:
   6565 	  case DEMANGLE_COMPONENT_VOLATILE_THIS:
   6566 	  case DEMANGLE_COMPONENT_CONST_THIS:
   6567 	  case DEMANGLE_COMPONENT_REFERENCE_THIS:
   6568 	  case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
   6569 	  default:
   6570 	    dc = NULL;
   6571 	    break;
   6572 	  case DEMANGLE_COMPONENT_TYPED_NAME:
   6573 	  case DEMANGLE_COMPONENT_TEMPLATE:
   6574 	    dc = d_left (dc);
   6575 	    break;
   6576 	  case DEMANGLE_COMPONENT_QUAL_NAME:
   6577 	  case DEMANGLE_COMPONENT_LOCAL_NAME:
   6578 	    dc = d_right (dc);
   6579 	    break;
   6580 	  case DEMANGLE_COMPONENT_CTOR:
   6581 	    *ctor_kind = dc->u.s_ctor.kind;
   6582 	    ret = 1;
   6583 	    dc = NULL;
   6584 	    break;
   6585 	  case DEMANGLE_COMPONENT_DTOR:
   6586 	    *dtor_kind = dc->u.s_dtor.kind;
   6587 	    ret = 1;
   6588 	    dc = NULL;
   6589 	    break;
   6590 	  }
   6591       }
   6592   }
   6593 
   6594 #if 0 /* in valgrind */
   6595 #else
   6596   free (di.comps);
   6597   free (di.subs);
   6598 #endif  /* in valgrind */
   6599 
   6600   return ret;
   6601 }
   6602 
   6603 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
   6604    name.  A non-zero return indicates the type of constructor.  */
   6605 
   6606 enum gnu_v3_ctor_kinds
   6607 is_gnu_v3_mangled_ctor (const char *name)
   6608 {
   6609   enum gnu_v3_ctor_kinds ctor_kind;
   6610   enum gnu_v3_dtor_kinds dtor_kind;
   6611 
   6612   if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
   6613     return (enum gnu_v3_ctor_kinds) 0;
   6614   return ctor_kind;
   6615 }
   6616 
   6617 
   6618 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
   6619    name.  A non-zero return indicates the type of destructor.  */
   6620 
   6621 enum gnu_v3_dtor_kinds
   6622 is_gnu_v3_mangled_dtor (const char *name)
   6623 {
   6624   enum gnu_v3_ctor_kinds ctor_kind;
   6625   enum gnu_v3_dtor_kinds dtor_kind;
   6626 
   6627   if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
   6628     return (enum gnu_v3_dtor_kinds) 0;
   6629   return dtor_kind;
   6630 }
   6631 
   6632 #endif /* IN_GLIBCPP_V3 */
   6633 
   6634 #ifdef STANDALONE_DEMANGLER
   6635 
   6636 #if 0 /* in valgrind */
   6637 #include "getopt.h"
   6638 #include "dyn-string.h"
   6639 #endif /* ! in valgrind */
   6640 
   6641 static void print_usage (FILE* fp, int exit_value);
   6642 
   6643 #define IS_ALPHA(CHAR)                                                  \
   6644   (((CHAR) >= 'a' && (CHAR) <= 'z')                                     \
   6645    || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
   6646 
   6647 /* Non-zero if CHAR is a character than can occur in a mangled name.  */
   6648 #define is_mangled_char(CHAR)                                           \
   6649   (IS_ALPHA (CHAR) || IS_DIGIT (CHAR)                                   \
   6650    || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
   6651 
   6652 /* The name of this program, as invoked.  */
   6653 const char* program_name;
   6654 
   6655 /* Prints usage summary to FP and then exits with EXIT_VALUE.  */
   6656 
   6657 static void
   6658 print_usage (FILE* fp, int exit_value)
   6659 {
   6660   fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
   6661   fprintf (fp, "Options:\n");
   6662   fprintf (fp, "  -h,--help       Display this message.\n");
   6663   fprintf (fp, "  -p,--no-params  Don't display function parameters\n");
   6664   fprintf (fp, "  -v,--verbose    Produce verbose demanglings.\n");
   6665   fprintf (fp, "If names are provided, they are demangled.  Otherwise filters standard input.\n");
   6666 
   6667   exit (exit_value);
   6668 }
   6669 
   6670 /* Option specification for getopt_long.  */
   6671 static const struct option long_options[] =
   6672 {
   6673   { "help",	 no_argument, NULL, 'h' },
   6674   { "no-params", no_argument, NULL, 'p' },
   6675   { "verbose",   no_argument, NULL, 'v' },
   6676   { NULL,        no_argument, NULL, 0   },
   6677 };
   6678 
   6679 /* Main entry for a demangling filter executable.  It will demangle
   6680    its command line arguments, if any.  If none are provided, it will
   6681    filter stdin to stdout, replacing any recognized mangled C++ names
   6682    with their demangled equivalents.  */
   6683 
   6684 int
   6685 main (int argc, char *argv[])
   6686 {
   6687   int i;
   6688   int opt_char;
   6689   int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
   6690 
   6691   /* Use the program name of this program, as invoked.  */
   6692   program_name = argv[0];
   6693 
   6694   /* Parse options.  */
   6695   do
   6696     {
   6697       opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
   6698       switch (opt_char)
   6699 	{
   6700 	case '?':  /* Unrecognized option.  */
   6701 	  print_usage (stderr, 1);
   6702 	  break;
   6703 
   6704 	case 'h':
   6705 	  print_usage (stdout, 0);
   6706 	  break;
   6707 
   6708 	case 'p':
   6709 	  options &= ~ DMGL_PARAMS;
   6710 	  break;
   6711 
   6712 	case 'v':
   6713 	  options |= DMGL_VERBOSE;
   6714 	  break;
   6715 	}
   6716     }
   6717   while (opt_char != -1);
   6718 
   6719   if (optind == argc)
   6720     /* No command line arguments were provided.  Filter stdin.  */
   6721     {
   6722       dyn_string_t mangled = dyn_string_new (3);
   6723       char *s;
   6724 
   6725       /* Read all of input.  */
   6726       while (!feof (stdin))
   6727 	{
   6728 	  char c;
   6729 
   6730 	  /* Pile characters into mangled until we hit one that can't
   6731 	     occur in a mangled name.  */
   6732 	  c = getchar ();
   6733 	  while (!feof (stdin) && is_mangled_char (c))
   6734 	    {
   6735 	      dyn_string_append_char (mangled, c);
   6736 	      if (feof (stdin))
   6737 		break;
   6738 	      c = getchar ();
   6739 	    }
   6740 
   6741 	  if (dyn_string_length (mangled) > 0)
   6742 	    {
   6743 #ifdef IN_GLIBCPP_V3
   6744 	      s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
   6745 #else
   6746 	      s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
   6747 #endif
   6748 
   6749 	      if (s != NULL)
   6750 		{
   6751 		  fputs (s, stdout);
   6752 		  free (s);
   6753 		}
   6754 	      else
   6755 		{
   6756 		  /* It might not have been a mangled name.  Print the
   6757 		     original text.  */
   6758 		  fputs (dyn_string_buf (mangled), stdout);
   6759 		}
   6760 
   6761 	      dyn_string_clear (mangled);
   6762 	    }
   6763 
   6764 	  /* If we haven't hit EOF yet, we've read one character that
   6765 	     can't occur in a mangled name, so print it out.  */
   6766 	  if (!feof (stdin))
   6767 	    putchar (c);
   6768 	}
   6769 
   6770       dyn_string_delete (mangled);
   6771     }
   6772   else
   6773     /* Demangle command line arguments.  */
   6774     {
   6775       /* Loop over command line arguments.  */
   6776       for (i = optind; i < argc; ++i)
   6777 	{
   6778 	  char *s;
   6779 #ifdef IN_GLIBCPP_V3
   6780 	  int status;
   6781 #endif
   6782 
   6783 	  /* Attempt to demangle.  */
   6784 #ifdef IN_GLIBCPP_V3
   6785 	  s = __cxa_demangle (argv[i], NULL, NULL, &status);
   6786 #else
   6787 	  s = cplus_demangle_v3 (argv[i], options);
   6788 #endif
   6789 
   6790 	  /* If it worked, print the demangled name.  */
   6791 	  if (s != NULL)
   6792 	    {
   6793 	      printf ("%s\n", s);
   6794 	      free (s);
   6795 	    }
   6796 	  else
   6797 	    {
   6798 #ifdef IN_GLIBCPP_V3
   6799 	      fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
   6800 #else
   6801 	      fprintf (stderr, "Failed: %s\n", argv[i]);
   6802 #endif
   6803 	    }
   6804 	}
   6805     }
   6806 
   6807   return 0;
   6808 }
   6809 
   6810 #endif /* STANDALONE_DEMANGLER */
   6811