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