Back to index

cell-binutils  2.17cvs20070401
cp-demangle.c
Go to the documentation of this file.
00001 /* Demangler for g++ V3 ABI.
00002    Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
00003    Written by Ian Lance Taylor <ian@wasabisystems.com>.
00004 
00005    This file is part of the libiberty library, which is part of GCC.
00006 
00007    This file is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2 of the License, or
00010    (at your option) any later version.
00011 
00012    In addition to the permissions in the GNU General Public License, the
00013    Free Software Foundation gives you unlimited permission to link the
00014    compiled version of this file into combinations with other programs,
00015    and to distribute those combinations without any restriction coming
00016    from the use of this file.  (The General Public License restrictions
00017    do apply in other respects; for example, they cover modification of
00018    the file, and distribution when not linked into a combined
00019    executable.)
00020 
00021    This program is distributed in the hope that it will be useful,
00022    but WITHOUT ANY WARRANTY; without even the implied warranty of
00023    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024    GNU General Public License for more details.
00025 
00026    You should have received a copy of the GNU General Public License
00027    along with this program; if not, write to the Free Software
00028    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. 
00029 */
00030 
00031 /* This code implements a demangler for the g++ V3 ABI.  The ABI is
00032    described on this web page:
00033        http://www.codesourcery.com/cxx-abi/abi.html#mangling
00034 
00035    This code was written while looking at the demangler written by
00036    Alex Samuel <samuel@codesourcery.com>.
00037 
00038    This code first pulls the mangled name apart into a list of
00039    components, and then walks the list generating the demangled
00040    name.
00041 
00042    This file will normally define the following functions, q.v.:
00043       char *cplus_demangle_v3(const char *mangled, int options)
00044       char *java_demangle_v3(const char *mangled)
00045       int cplus_demangle_v3_callback(const char *mangled, int options,
00046                                      demangle_callbackref callback)
00047       int java_demangle_v3_callback(const char *mangled,
00048                                     demangle_callbackref callback)
00049       enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
00050       enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
00051 
00052    Also, the interface to the component list is public, and defined in
00053    demangle.h.  The interface consists of these types, which are
00054    defined in demangle.h:
00055       enum demangle_component_type
00056       struct demangle_component
00057       demangle_callbackref
00058    and these functions defined in this file:
00059       cplus_demangle_fill_name
00060       cplus_demangle_fill_extended_operator
00061       cplus_demangle_fill_ctor
00062       cplus_demangle_fill_dtor
00063       cplus_demangle_print
00064       cplus_demangle_print_callback
00065    and other functions defined in the file cp-demint.c.
00066 
00067    This file also defines some other functions and variables which are
00068    only to be used by the file cp-demint.c.
00069 
00070    Preprocessor macros you can define while compiling this file:
00071 
00072    IN_LIBGCC2
00073       If defined, this file defines the following functions, q.v.:
00074          char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
00075                                int *status)
00076          int __gcclibcxx_demangle_callback (const char *,
00077                                             void (*)
00078                                               (const char *, size_t, void *),
00079                                             void *)
00080       instead of cplus_demangle_v3[_callback]() and
00081       java_demangle_v3[_callback]().
00082 
00083    IN_GLIBCPP_V3
00084       If defined, this file defines only __cxa_demangle() and
00085       __gcclibcxx_demangle_callback(), and no other publically visible
00086       functions or variables.
00087 
00088    STANDALONE_DEMANGLER
00089       If defined, this file defines a main() function which demangles
00090       any arguments, or, if none, demangles stdin.
00091 
00092    CP_DEMANGLE_DEBUG
00093       If defined, turns on debugging mode, which prints information on
00094       stdout about the mangled string.  This is not generally useful.
00095 */
00096 
00097 #if defined (_AIX) && !defined (__GNUC__)
00098  #pragma alloca
00099 #endif
00100 
00101 #ifdef HAVE_CONFIG_H
00102 #include "config.h"
00103 #endif
00104 
00105 #include <stdio.h>
00106 
00107 #ifdef HAVE_STDLIB_H
00108 #include <stdlib.h>
00109 #endif
00110 #ifdef HAVE_STRING_H
00111 #include <string.h>
00112 #endif
00113 
00114 #ifdef HAVE_ALLOCA_H
00115 # include <alloca.h>
00116 #else
00117 # ifndef alloca
00118 #  ifdef __GNUC__
00119 #   define alloca __builtin_alloca
00120 #  else
00121 extern char *alloca ();
00122 #  endif /* __GNUC__ */
00123 # endif /* alloca */
00124 #endif /* HAVE_ALLOCA_H */
00125 
00126 #include "ansidecl.h"
00127 #include "libiberty.h"
00128 #include "demangle.h"
00129 #include "cp-demangle.h"
00130 
00131 /* If IN_GLIBCPP_V3 is defined, some functions are made static.  We
00132    also rename them via #define to avoid compiler errors when the
00133    static definition conflicts with the extern declaration in a header
00134    file.  */
00135 #ifdef IN_GLIBCPP_V3
00136 
00137 #define CP_STATIC_IF_GLIBCPP_V3 static
00138 
00139 #define cplus_demangle_fill_name d_fill_name
00140 static int d_fill_name (struct demangle_component *, const char *, int);
00141 
00142 #define cplus_demangle_fill_extended_operator d_fill_extended_operator
00143 static int
00144 d_fill_extended_operator (struct demangle_component *, int,
00145                           struct demangle_component *);
00146 
00147 #define cplus_demangle_fill_ctor d_fill_ctor
00148 static int
00149 d_fill_ctor (struct demangle_component *, enum gnu_v3_ctor_kinds,
00150              struct demangle_component *);
00151 
00152 #define cplus_demangle_fill_dtor d_fill_dtor
00153 static int
00154 d_fill_dtor (struct demangle_component *, enum gnu_v3_dtor_kinds,
00155              struct demangle_component *);
00156 
00157 #define cplus_demangle_mangled_name d_mangled_name
00158 static struct demangle_component *d_mangled_name (struct d_info *, int);
00159 
00160 #define cplus_demangle_type d_type
00161 static struct demangle_component *d_type (struct d_info *);
00162 
00163 #define cplus_demangle_print d_print
00164 static char *d_print (int, const struct demangle_component *, int, size_t *);
00165 
00166 #define cplus_demangle_print_callback d_print_callback
00167 static int d_print_callback (int, const struct demangle_component *,
00168                              demangle_callbackref, void *);
00169 
00170 #define cplus_demangle_init_info d_init_info
00171 static void d_init_info (const char *, int, size_t, struct d_info *);
00172 
00173 #else /* ! defined(IN_GLIBCPP_V3) */
00174 #define CP_STATIC_IF_GLIBCPP_V3
00175 #endif /* ! defined(IN_GLIBCPP_V3) */
00176 
00177 /* See if the compiler supports dynamic arrays.  */
00178 
00179 #ifdef __GNUC__
00180 #define CP_DYNAMIC_ARRAYS
00181 #else
00182 #ifdef __STDC__
00183 #ifdef __STDC_VERSION__
00184 #if __STDC_VERSION__ >= 199901L
00185 #define CP_DYNAMIC_ARRAYS
00186 #endif /* __STDC__VERSION >= 199901L */
00187 #endif /* defined (__STDC_VERSION__) */
00188 #endif /* defined (__STDC__) */
00189 #endif /* ! defined (__GNUC__) */
00190 
00191 /* We avoid pulling in the ctype tables, to prevent pulling in
00192    additional unresolved symbols when this code is used in a library.
00193    FIXME: Is this really a valid reason?  This comes from the original
00194    V3 demangler code.
00195 
00196    As of this writing this file has the following undefined references
00197    when compiled with -DIN_GLIBCPP_V3: realloc, free, memcpy, strcpy,
00198    strcat, strlen.  */
00199 
00200 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
00201 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
00202 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
00203 
00204 /* The prefix prepended by GCC to an identifier represnting the
00205    anonymous namespace.  */
00206 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
00207 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
00208   (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
00209 
00210 /* Information we keep for the standard substitutions.  */
00211 
00212 struct d_standard_sub_info
00213 {
00214   /* The code for this substitution.  */
00215   char code;
00216   /* The simple string it expands to.  */
00217   const char *simple_expansion;
00218   /* The length of the simple expansion.  */
00219   int simple_len;
00220   /* The results of a full, verbose, expansion.  This is used when
00221      qualifying a constructor/destructor, or when in verbose mode.  */
00222   const char *full_expansion;
00223   /* The length of the full expansion.  */
00224   int full_len;
00225   /* What to set the last_name field of d_info to; NULL if we should
00226      not set it.  This is only relevant when qualifying a
00227      constructor/destructor.  */
00228   const char *set_last_name;
00229   /* The length of set_last_name.  */
00230   int set_last_name_len;
00231 };
00232 
00233 /* Accessors for subtrees of struct demangle_component.  */
00234 
00235 #define d_left(dc) ((dc)->u.s_binary.left)
00236 #define d_right(dc) ((dc)->u.s_binary.right)
00237 
00238 /* A list of templates.  This is used while printing.  */
00239 
00240 struct d_print_template
00241 {
00242   /* Next template on the list.  */
00243   struct d_print_template *next;
00244   /* This template.  */
00245   const struct demangle_component *template_decl;
00246 };
00247 
00248 /* A list of type modifiers.  This is used while printing.  */
00249 
00250 struct d_print_mod
00251 {
00252   /* Next modifier on the list.  These are in the reverse of the order
00253      in which they appeared in the mangled string.  */
00254   struct d_print_mod *next;
00255   /* The modifier.  */
00256   const struct demangle_component *mod;
00257   /* Whether this modifier was printed.  */
00258   int printed;
00259   /* The list of templates which applies to this modifier.  */
00260   struct d_print_template *templates;
00261 };
00262 
00263 /* We use these structures to hold information during printing.  */
00264 
00265 struct d_growable_string
00266 {
00267   /* Buffer holding the result.  */
00268   char *buf;
00269   /* Current length of data in buffer.  */
00270   size_t len;
00271   /* Allocated size of buffer.  */
00272   size_t alc;
00273   /* Set to 1 if we had a memory allocation failure.  */
00274   int allocation_failure;
00275 };
00276 
00277 enum { D_PRINT_BUFFER_LENGTH = 256 };
00278 struct d_print_info
00279 {
00280   /* The options passed to the demangler.  */
00281   int options;
00282   /* Fixed-length allocated buffer for demangled data, flushed to the
00283      callback with a NUL termination once full.  */
00284   char buf[D_PRINT_BUFFER_LENGTH];
00285   /* Current length of data in buffer.  */
00286   size_t len;
00287   /* The last character printed, saved individually so that it survives
00288      any buffer flush.  */
00289   char last_char;
00290   /* Callback function to handle demangled buffer flush.  */
00291   demangle_callbackref callback;
00292   /* Opaque callback argument.  */
00293   void *opaque;
00294   /* The current list of templates, if any.  */
00295   struct d_print_template *templates;
00296   /* The current list of modifiers (e.g., pointer, reference, etc.),
00297      if any.  */
00298   struct d_print_mod *modifiers;
00299   /* Set to 1 if we saw a demangling error.  */
00300   int demangle_failure;
00301 };
00302 
00303 #ifdef CP_DEMANGLE_DEBUG
00304 static void d_dump (struct demangle_component *, int);
00305 #endif
00306 
00307 static struct demangle_component *
00308 d_make_empty (struct d_info *);
00309 
00310 static struct demangle_component *
00311 d_make_comp (struct d_info *, enum demangle_component_type,
00312              struct demangle_component *,
00313              struct demangle_component *);
00314 
00315 static struct demangle_component *
00316 d_make_name (struct d_info *, const char *, int);
00317 
00318 static struct demangle_component *
00319 d_make_builtin_type (struct d_info *,
00320                      const struct demangle_builtin_type_info *);
00321 
00322 static struct demangle_component *
00323 d_make_operator (struct d_info *,
00324                  const struct demangle_operator_info *);
00325 
00326 static struct demangle_component *
00327 d_make_extended_operator (struct d_info *, int,
00328                           struct demangle_component *);
00329 
00330 static struct demangle_component *
00331 d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
00332              struct demangle_component *);
00333 
00334 static struct demangle_component *
00335 d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
00336              struct demangle_component *);
00337 
00338 static struct demangle_component *
00339 d_make_template_param (struct d_info *, long);
00340 
00341 static struct demangle_component *
00342 d_make_sub (struct d_info *, const char *, int);
00343 
00344 static int
00345 has_return_type (struct demangle_component *);
00346 
00347 static int
00348 is_ctor_dtor_or_conversion (struct demangle_component *);
00349 
00350 static struct demangle_component *d_encoding (struct d_info *, int);
00351 
00352 static struct demangle_component *d_name (struct d_info *);
00353 
00354 static struct demangle_component *d_nested_name (struct d_info *);
00355 
00356 static struct demangle_component *d_prefix (struct d_info *);
00357 
00358 static struct demangle_component *d_unqualified_name (struct d_info *);
00359 
00360 static struct demangle_component *d_source_name (struct d_info *);
00361 
00362 static long d_number (struct d_info *);
00363 
00364 static struct demangle_component *d_identifier (struct d_info *, int);
00365 
00366 static struct demangle_component *d_operator_name (struct d_info *);
00367 
00368 static struct demangle_component *d_special_name (struct d_info *);
00369 
00370 static int d_call_offset (struct d_info *, int);
00371 
00372 static struct demangle_component *d_ctor_dtor_name (struct d_info *);
00373 
00374 static struct demangle_component **
00375 d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
00376 
00377 static struct demangle_component *
00378 d_function_type (struct d_info *);
00379 
00380 static struct demangle_component *
00381 d_bare_function_type (struct d_info *, int);
00382 
00383 static struct demangle_component *
00384 d_class_enum_type (struct d_info *);
00385 
00386 static struct demangle_component *d_array_type (struct d_info *);
00387 
00388 static struct demangle_component *
00389 d_pointer_to_member_type (struct d_info *);
00390 
00391 static struct demangle_component *
00392 d_template_param (struct d_info *);
00393 
00394 static struct demangle_component *d_template_args (struct d_info *);
00395 
00396 static struct demangle_component *
00397 d_template_arg (struct d_info *);
00398 
00399 static struct demangle_component *d_expression (struct d_info *);
00400 
00401 static struct demangle_component *d_expr_primary (struct d_info *);
00402 
00403 static struct demangle_component *d_local_name (struct d_info *);
00404 
00405 static int d_discriminator (struct d_info *);
00406 
00407 static int
00408 d_add_substitution (struct d_info *, struct demangle_component *);
00409 
00410 static struct demangle_component *d_substitution (struct d_info *, int);
00411 
00412 static void d_growable_string_init (struct d_growable_string *, size_t);
00413 
00414 static inline void
00415 d_growable_string_resize (struct d_growable_string *, size_t);
00416 
00417 static inline void
00418 d_growable_string_append_buffer (struct d_growable_string *,
00419                                  const char *, size_t);
00420 static void
00421 d_growable_string_callback_adapter (const char *, size_t, void *);
00422 
00423 static void
00424 d_print_init (struct d_print_info *, int, demangle_callbackref, void *);
00425 
00426 static inline void d_print_error (struct d_print_info *);
00427 
00428 static inline int d_print_saw_error (struct d_print_info *);
00429 
00430 static inline void d_print_flush (struct d_print_info *);
00431 
00432 static inline void d_append_char (struct d_print_info *, char);
00433 
00434 static inline void d_append_buffer (struct d_print_info *,
00435                                     const char *, size_t);
00436 
00437 static inline void d_append_string (struct d_print_info *, const char *);
00438 
00439 static inline char d_last_char (struct d_print_info *);
00440 
00441 static void
00442 d_print_comp (struct d_print_info *, const struct demangle_component *);
00443 
00444 static void
00445 d_print_java_identifier (struct d_print_info *, const char *, int);
00446 
00447 static void
00448 d_print_mod_list (struct d_print_info *, struct d_print_mod *, int);
00449 
00450 static void
00451 d_print_mod (struct d_print_info *, const struct demangle_component *);
00452 
00453 static void
00454 d_print_function_type (struct d_print_info *,
00455                        const struct demangle_component *,
00456                        struct d_print_mod *);
00457 
00458 static void
00459 d_print_array_type (struct d_print_info *,
00460                     const struct demangle_component *,
00461                     struct d_print_mod *);
00462 
00463 static void
00464 d_print_expr_op (struct d_print_info *, const struct demangle_component *);
00465 
00466 static void
00467 d_print_cast (struct d_print_info *, const struct demangle_component *);
00468 
00469 static int d_demangle_callback (const char *, int,
00470                                 demangle_callbackref, void *);
00471 static char *d_demangle (const char *, int, size_t *);
00472 
00473 #ifdef CP_DEMANGLE_DEBUG
00474 
00475 static void
00476 d_dump (struct demangle_component *dc, int indent)
00477 {
00478   int i;
00479 
00480   if (dc == NULL)
00481     {
00482       if (indent == 0)
00483         printf ("failed demangling\n");
00484       return;
00485     }
00486 
00487   for (i = 0; i < indent; ++i)
00488     putchar (' ');
00489 
00490   switch (dc->type)
00491     {
00492     case DEMANGLE_COMPONENT_NAME:
00493       printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
00494       return;
00495     case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
00496       printf ("template parameter %ld\n", dc->u.s_number.number);
00497       return;
00498     case DEMANGLE_COMPONENT_CTOR:
00499       printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
00500       d_dump (dc->u.s_ctor.name, indent + 2);
00501       return;
00502     case DEMANGLE_COMPONENT_DTOR:
00503       printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
00504       d_dump (dc->u.s_dtor.name, indent + 2);
00505       return;
00506     case DEMANGLE_COMPONENT_SUB_STD:
00507       printf ("standard substitution %s\n", dc->u.s_string.string);
00508       return;
00509     case DEMANGLE_COMPONENT_BUILTIN_TYPE:
00510       printf ("builtin type %s\n", dc->u.s_builtin.type->name);
00511       return;
00512     case DEMANGLE_COMPONENT_OPERATOR:
00513       printf ("operator %s\n", dc->u.s_operator.op->name);
00514       return;
00515     case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
00516       printf ("extended operator with %d args\n",
00517              dc->u.s_extended_operator.args);
00518       d_dump (dc->u.s_extended_operator.name, indent + 2);
00519       return;
00520 
00521     case DEMANGLE_COMPONENT_QUAL_NAME:
00522       printf ("qualified name\n");
00523       break;
00524     case DEMANGLE_COMPONENT_LOCAL_NAME:
00525       printf ("local name\n");
00526       break;
00527     case DEMANGLE_COMPONENT_TYPED_NAME:
00528       printf ("typed name\n");
00529       break;
00530     case DEMANGLE_COMPONENT_TEMPLATE:
00531       printf ("template\n");
00532       break;
00533     case DEMANGLE_COMPONENT_VTABLE:
00534       printf ("vtable\n");
00535       break;
00536     case DEMANGLE_COMPONENT_VTT:
00537       printf ("VTT\n");
00538       break;
00539     case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
00540       printf ("construction vtable\n");
00541       break;
00542     case DEMANGLE_COMPONENT_TYPEINFO:
00543       printf ("typeinfo\n");
00544       break;
00545     case DEMANGLE_COMPONENT_TYPEINFO_NAME:
00546       printf ("typeinfo name\n");
00547       break;
00548     case DEMANGLE_COMPONENT_TYPEINFO_FN:
00549       printf ("typeinfo function\n");
00550       break;
00551     case DEMANGLE_COMPONENT_THUNK:
00552       printf ("thunk\n");
00553       break;
00554     case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
00555       printf ("virtual thunk\n");
00556       break;
00557     case DEMANGLE_COMPONENT_COVARIANT_THUNK:
00558       printf ("covariant thunk\n");
00559       break;
00560     case DEMANGLE_COMPONENT_JAVA_CLASS:
00561       printf ("java class\n");
00562       break;
00563     case DEMANGLE_COMPONENT_GUARD:
00564       printf ("guard\n");
00565       break;
00566     case DEMANGLE_COMPONENT_REFTEMP:
00567       printf ("reference temporary\n");
00568       break;
00569     case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
00570       printf ("hidden alias\n");
00571       break;
00572     case DEMANGLE_COMPONENT_RESTRICT:
00573       printf ("restrict\n");
00574       break;
00575     case DEMANGLE_COMPONENT_VOLATILE:
00576       printf ("volatile\n");
00577       break;
00578     case DEMANGLE_COMPONENT_CONST:
00579       printf ("const\n");
00580       break;
00581     case DEMANGLE_COMPONENT_RESTRICT_THIS:
00582       printf ("restrict this\n");
00583       break;
00584     case DEMANGLE_COMPONENT_VOLATILE_THIS:
00585       printf ("volatile this\n");
00586       break;
00587     case DEMANGLE_COMPONENT_CONST_THIS:
00588       printf ("const this\n");
00589       break;
00590     case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
00591       printf ("vendor type qualifier\n");
00592       break;
00593     case DEMANGLE_COMPONENT_POINTER:
00594       printf ("pointer\n");
00595       break;
00596     case DEMANGLE_COMPONENT_REFERENCE:
00597       printf ("reference\n");
00598       break;
00599     case DEMANGLE_COMPONENT_COMPLEX:
00600       printf ("complex\n");
00601       break;
00602     case DEMANGLE_COMPONENT_IMAGINARY:
00603       printf ("imaginary\n");
00604       break;
00605     case DEMANGLE_COMPONENT_VENDOR_TYPE:
00606       printf ("vendor type\n");
00607       break;
00608     case DEMANGLE_COMPONENT_FUNCTION_TYPE:
00609       printf ("function type\n");
00610       break;
00611     case DEMANGLE_COMPONENT_ARRAY_TYPE:
00612       printf ("array type\n");
00613       break;
00614     case DEMANGLE_COMPONENT_PTRMEM_TYPE:
00615       printf ("pointer to member type\n");
00616       break;
00617     case DEMANGLE_COMPONENT_ARGLIST:
00618       printf ("argument list\n");
00619       break;
00620     case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
00621       printf ("template argument list\n");
00622       break;
00623     case DEMANGLE_COMPONENT_CAST:
00624       printf ("cast\n");
00625       break;
00626     case DEMANGLE_COMPONENT_UNARY:
00627       printf ("unary operator\n");
00628       break;
00629     case DEMANGLE_COMPONENT_BINARY:
00630       printf ("binary operator\n");
00631       break;
00632     case DEMANGLE_COMPONENT_BINARY_ARGS:
00633       printf ("binary operator arguments\n");
00634       break;
00635     case DEMANGLE_COMPONENT_TRINARY:
00636       printf ("trinary operator\n");
00637       break;
00638     case DEMANGLE_COMPONENT_TRINARY_ARG1:
00639       printf ("trinary operator arguments 1\n");
00640       break;
00641     case DEMANGLE_COMPONENT_TRINARY_ARG2:
00642       printf ("trinary operator arguments 1\n");
00643       break;
00644     case DEMANGLE_COMPONENT_LITERAL:
00645       printf ("literal\n");
00646       break;
00647     case DEMANGLE_COMPONENT_LITERAL_NEG:
00648       printf ("negative literal\n");
00649       break;
00650     }
00651 
00652   d_dump (d_left (dc), indent + 2);
00653   d_dump (d_right (dc), indent + 2);
00654 }
00655 
00656 #endif /* CP_DEMANGLE_DEBUG */
00657 
00658 /* Fill in a DEMANGLE_COMPONENT_NAME.  */
00659 
00660 CP_STATIC_IF_GLIBCPP_V3
00661 int
00662 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
00663 {
00664   if (p == NULL || s == NULL || len == 0)
00665     return 0;
00666   p->type = DEMANGLE_COMPONENT_NAME;
00667   p->u.s_name.s = s;
00668   p->u.s_name.len = len;
00669   return 1;
00670 }
00671 
00672 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR.  */
00673 
00674 CP_STATIC_IF_GLIBCPP_V3
00675 int
00676 cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
00677                                        struct demangle_component *name)
00678 {
00679   if (p == NULL || args < 0 || name == NULL)
00680     return 0;
00681   p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
00682   p->u.s_extended_operator.args = args;
00683   p->u.s_extended_operator.name = name;
00684   return 1;
00685 }
00686 
00687 /* Fill in a DEMANGLE_COMPONENT_CTOR.  */
00688 
00689 CP_STATIC_IF_GLIBCPP_V3
00690 int
00691 cplus_demangle_fill_ctor (struct demangle_component *p,
00692                           enum gnu_v3_ctor_kinds kind,
00693                           struct demangle_component *name)
00694 {
00695   if (p == NULL
00696       || name == NULL
00697       || (kind < gnu_v3_complete_object_ctor
00698          && kind > gnu_v3_complete_object_allocating_ctor))
00699     return 0;
00700   p->type = DEMANGLE_COMPONENT_CTOR;
00701   p->u.s_ctor.kind = kind;
00702   p->u.s_ctor.name = name;
00703   return 1;
00704 }
00705 
00706 /* Fill in a DEMANGLE_COMPONENT_DTOR.  */
00707 
00708 CP_STATIC_IF_GLIBCPP_V3
00709 int
00710 cplus_demangle_fill_dtor (struct demangle_component *p,
00711                           enum gnu_v3_dtor_kinds kind,
00712                           struct demangle_component *name)
00713 {
00714   if (p == NULL
00715       || name == NULL
00716       || (kind < gnu_v3_deleting_dtor
00717          && kind > gnu_v3_base_object_dtor))
00718     return 0;
00719   p->type = DEMANGLE_COMPONENT_DTOR;
00720   p->u.s_dtor.kind = kind;
00721   p->u.s_dtor.name = name;
00722   return 1;
00723 }
00724 
00725 /* Add a new component.  */
00726 
00727 static struct demangle_component *
00728 d_make_empty (struct d_info *di)
00729 {
00730   struct demangle_component *p;
00731 
00732   if (di->next_comp >= di->num_comps)
00733     return NULL;
00734   p = &di->comps[di->next_comp];
00735   ++di->next_comp;
00736   return p;
00737 }
00738 
00739 /* Add a new generic component.  */
00740 
00741 static struct demangle_component *
00742 d_make_comp (struct d_info *di, enum demangle_component_type type,
00743              struct demangle_component *left,
00744              struct demangle_component *right)
00745 {
00746   struct demangle_component *p;
00747 
00748   /* We check for errors here.  A typical error would be a NULL return
00749      from a subroutine.  We catch those here, and return NULL
00750      upward.  */
00751   switch (type)
00752     {
00753       /* These types require two parameters.  */
00754     case DEMANGLE_COMPONENT_QUAL_NAME:
00755     case DEMANGLE_COMPONENT_LOCAL_NAME:
00756     case DEMANGLE_COMPONENT_TYPED_NAME:
00757     case DEMANGLE_COMPONENT_TEMPLATE:
00758     case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
00759     case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
00760     case DEMANGLE_COMPONENT_PTRMEM_TYPE:
00761     case DEMANGLE_COMPONENT_UNARY:
00762     case DEMANGLE_COMPONENT_BINARY:
00763     case DEMANGLE_COMPONENT_BINARY_ARGS:
00764     case DEMANGLE_COMPONENT_TRINARY:
00765     case DEMANGLE_COMPONENT_TRINARY_ARG1:
00766     case DEMANGLE_COMPONENT_TRINARY_ARG2:
00767     case DEMANGLE_COMPONENT_LITERAL:
00768     case DEMANGLE_COMPONENT_LITERAL_NEG:
00769       if (left == NULL || right == NULL)
00770        return NULL;
00771       break;
00772 
00773       /* These types only require one parameter.  */
00774     case DEMANGLE_COMPONENT_VTABLE:
00775     case DEMANGLE_COMPONENT_VTT:
00776     case DEMANGLE_COMPONENT_TYPEINFO:
00777     case DEMANGLE_COMPONENT_TYPEINFO_NAME:
00778     case DEMANGLE_COMPONENT_TYPEINFO_FN:
00779     case DEMANGLE_COMPONENT_THUNK:
00780     case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
00781     case DEMANGLE_COMPONENT_COVARIANT_THUNK:
00782     case DEMANGLE_COMPONENT_JAVA_CLASS:
00783     case DEMANGLE_COMPONENT_GUARD:
00784     case DEMANGLE_COMPONENT_REFTEMP:
00785     case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
00786     case DEMANGLE_COMPONENT_POINTER:
00787     case DEMANGLE_COMPONENT_REFERENCE:
00788     case DEMANGLE_COMPONENT_COMPLEX:
00789     case DEMANGLE_COMPONENT_IMAGINARY:
00790     case DEMANGLE_COMPONENT_VENDOR_TYPE:
00791     case DEMANGLE_COMPONENT_ARGLIST:
00792     case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
00793     case DEMANGLE_COMPONENT_CAST:
00794       if (left == NULL)
00795        return NULL;
00796       break;
00797 
00798       /* This needs a right parameter, but the left parameter can be
00799         empty.  */
00800     case DEMANGLE_COMPONENT_ARRAY_TYPE:
00801       if (right == NULL)
00802        return NULL;
00803       break;
00804 
00805       /* These are allowed to have no parameters--in some cases they
00806         will be filled in later.  */
00807     case DEMANGLE_COMPONENT_FUNCTION_TYPE:
00808     case DEMANGLE_COMPONENT_RESTRICT:
00809     case DEMANGLE_COMPONENT_VOLATILE:
00810     case DEMANGLE_COMPONENT_CONST:
00811     case DEMANGLE_COMPONENT_RESTRICT_THIS:
00812     case DEMANGLE_COMPONENT_VOLATILE_THIS:
00813     case DEMANGLE_COMPONENT_CONST_THIS:
00814       break;
00815 
00816       /* Other types should not be seen here.  */
00817     default:
00818       return NULL;
00819     }
00820 
00821   p = d_make_empty (di);
00822   if (p != NULL)
00823     {
00824       p->type = type;
00825       p->u.s_binary.left = left;
00826       p->u.s_binary.right = right;
00827     }
00828   return p;
00829 }
00830 
00831 /* Add a new name component.  */
00832 
00833 static struct demangle_component *
00834 d_make_name (struct d_info *di, const char *s, int len)
00835 {
00836   struct demangle_component *p;
00837 
00838   p = d_make_empty (di);
00839   if (! cplus_demangle_fill_name (p, s, len))
00840     return NULL;
00841   return p;
00842 }
00843 
00844 /* Add a new builtin type component.  */
00845 
00846 static struct demangle_component *
00847 d_make_builtin_type (struct d_info *di,
00848                      const struct demangle_builtin_type_info *type)
00849 {
00850   struct demangle_component *p;
00851 
00852   if (type == NULL)
00853     return NULL;
00854   p = d_make_empty (di);
00855   if (p != NULL)
00856     {
00857       p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
00858       p->u.s_builtin.type = type;
00859     }
00860   return p;
00861 }
00862 
00863 /* Add a new operator component.  */
00864 
00865 static struct demangle_component *
00866 d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
00867 {
00868   struct demangle_component *p;
00869 
00870   p = d_make_empty (di);
00871   if (p != NULL)
00872     {
00873       p->type = DEMANGLE_COMPONENT_OPERATOR;
00874       p->u.s_operator.op = op;
00875     }
00876   return p;
00877 }
00878 
00879 /* Add a new extended operator component.  */
00880 
00881 static struct demangle_component *
00882 d_make_extended_operator (struct d_info *di, int args,
00883                           struct demangle_component *name)
00884 {
00885   struct demangle_component *p;
00886 
00887   p = d_make_empty (di);
00888   if (! cplus_demangle_fill_extended_operator (p, args, name))
00889     return NULL;
00890   return p;
00891 }
00892 
00893 /* Add a new constructor component.  */
00894 
00895 static struct demangle_component *
00896 d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
00897              struct demangle_component *name)
00898 {
00899   struct demangle_component *p;
00900 
00901   p = d_make_empty (di);
00902   if (! cplus_demangle_fill_ctor (p, kind, name))
00903     return NULL;
00904   return p;
00905 }
00906 
00907 /* Add a new destructor component.  */
00908 
00909 static struct demangle_component *
00910 d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
00911              struct demangle_component *name)
00912 {
00913   struct demangle_component *p;
00914 
00915   p = d_make_empty (di);
00916   if (! cplus_demangle_fill_dtor (p, kind, name))
00917     return NULL;
00918   return p;
00919 }
00920 
00921 /* Add a new template parameter.  */
00922 
00923 static struct demangle_component *
00924 d_make_template_param (struct d_info *di, long i)
00925 {
00926   struct demangle_component *p;
00927 
00928   p = d_make_empty (di);
00929   if (p != NULL)
00930     {
00931       p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
00932       p->u.s_number.number = i;
00933     }
00934   return p;
00935 }
00936 
00937 /* Add a new standard substitution component.  */
00938 
00939 static struct demangle_component *
00940 d_make_sub (struct d_info *di, const char *name, int len)
00941 {
00942   struct demangle_component *p;
00943 
00944   p = d_make_empty (di);
00945   if (p != NULL)
00946     {
00947       p->type = DEMANGLE_COMPONENT_SUB_STD;
00948       p->u.s_string.string = name;
00949       p->u.s_string.len = len;
00950     }
00951   return p;
00952 }
00953 
00954 /* <mangled-name> ::= _Z <encoding>
00955 
00956    TOP_LEVEL is non-zero when called at the top level.  */
00957 
00958 CP_STATIC_IF_GLIBCPP_V3
00959 struct demangle_component *
00960 cplus_demangle_mangled_name (struct d_info *di, int top_level)
00961 {
00962   if (! d_check_char (di, '_'))
00963     return NULL;
00964   if (! d_check_char (di, 'Z'))
00965     return NULL;
00966   return d_encoding (di, top_level);
00967 }
00968 
00969 /* Return whether a function should have a return type.  The argument
00970    is the function name, which may be qualified in various ways.  The
00971    rules are that template functions have return types with some
00972    exceptions, function types which are not part of a function name
00973    mangling have return types with some exceptions, and non-template
00974    function names do not have return types.  The exceptions are that
00975    constructors, destructors, and conversion operators do not have
00976    return types.  */
00977 
00978 static int
00979 has_return_type (struct demangle_component *dc)
00980 {
00981   if (dc == NULL)
00982     return 0;
00983   switch (dc->type)
00984     {
00985     default:
00986       return 0;
00987     case DEMANGLE_COMPONENT_TEMPLATE:
00988       return ! is_ctor_dtor_or_conversion (d_left (dc));
00989     case DEMANGLE_COMPONENT_RESTRICT_THIS:
00990     case DEMANGLE_COMPONENT_VOLATILE_THIS:
00991     case DEMANGLE_COMPONENT_CONST_THIS:
00992       return has_return_type (d_left (dc));
00993     }
00994 }
00995 
00996 /* Return whether a name is a constructor, a destructor, or a
00997    conversion operator.  */
00998 
00999 static int
01000 is_ctor_dtor_or_conversion (struct demangle_component *dc)
01001 {
01002   if (dc == NULL)
01003     return 0;
01004   switch (dc->type)
01005     {
01006     default:
01007       return 0;
01008     case DEMANGLE_COMPONENT_QUAL_NAME:
01009     case DEMANGLE_COMPONENT_LOCAL_NAME:
01010       return is_ctor_dtor_or_conversion (d_right (dc));
01011     case DEMANGLE_COMPONENT_CTOR:
01012     case DEMANGLE_COMPONENT_DTOR:
01013     case DEMANGLE_COMPONENT_CAST:
01014       return 1;
01015     }
01016 }
01017 
01018 /* <encoding> ::= <(function) name> <bare-function-type>
01019               ::= <(data) name>
01020               ::= <special-name>
01021 
01022    TOP_LEVEL is non-zero when called at the top level, in which case
01023    if DMGL_PARAMS is not set we do not demangle the function
01024    parameters.  We only set this at the top level, because otherwise
01025    we would not correctly demangle names in local scopes.  */
01026 
01027 static struct demangle_component *
01028 d_encoding (struct d_info *di, int top_level)
01029 {
01030   char peek = d_peek_char (di);
01031 
01032   if (peek == 'G' || peek == 'T')
01033     return d_special_name (di);
01034   else
01035     {
01036       struct demangle_component *dc;
01037 
01038       dc = d_name (di);
01039 
01040       if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
01041        {
01042          /* Strip off any initial CV-qualifiers, as they really apply
01043             to the `this' parameter, and they were not output by the
01044             v2 demangler without DMGL_PARAMS.  */
01045          while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
01046                || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
01047                || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
01048            dc = d_left (dc);
01049 
01050          /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
01051             there may be CV-qualifiers on its right argument which
01052             really apply here; this happens when parsing a class
01053             which is local to a function.  */
01054          if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
01055            {
01056              struct demangle_component *dcr;
01057 
01058              dcr = d_right (dc);
01059              while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS
01060                    || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS
01061                    || dcr->type == DEMANGLE_COMPONENT_CONST_THIS)
01062               dcr = d_left (dcr);
01063              dc->u.s_binary.right = dcr;
01064            }
01065 
01066          return dc;
01067        }
01068 
01069       peek = d_peek_char (di);
01070       if (dc == NULL || peek == '\0' || peek == 'E')
01071        return dc;
01072       return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
01073                        d_bare_function_type (di, has_return_type (dc)));
01074     }
01075 }
01076 
01077 /* <name> ::= <nested-name>
01078           ::= <unscoped-name>
01079           ::= <unscoped-template-name> <template-args>
01080           ::= <local-name>
01081 
01082    <unscoped-name> ::= <unqualified-name>
01083                    ::= St <unqualified-name>
01084 
01085    <unscoped-template-name> ::= <unscoped-name>
01086                             ::= <substitution>
01087 */
01088 
01089 static struct demangle_component *
01090 d_name (struct d_info *di)
01091 {
01092   char peek = d_peek_char (di);
01093   struct demangle_component *dc;
01094 
01095   switch (peek)
01096     {
01097     case 'N':
01098       return d_nested_name (di);
01099 
01100     case 'Z':
01101       return d_local_name (di);
01102 
01103     case 'S':
01104       {
01105        int subst;
01106 
01107        if (d_peek_next_char (di) != 't')
01108          {
01109            dc = d_substitution (di, 0);
01110            subst = 1;
01111          }
01112        else
01113          {
01114            d_advance (di, 2);
01115            dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
01116                            d_make_name (di, "std", 3),
01117                            d_unqualified_name (di));
01118            di->expansion += 3;
01119            subst = 0;
01120          }
01121 
01122        if (d_peek_char (di) != 'I')
01123          {
01124            /* The grammar does not permit this case to occur if we
01125               called d_substitution() above (i.e., subst == 1).  We
01126               don't bother to check.  */
01127          }
01128        else
01129          {
01130            /* This is <template-args>, which means that we just saw
01131               <unscoped-template-name>, which is a substitution
01132               candidate if we didn't just get it from a
01133               substitution.  */
01134            if (! subst)
01135              {
01136               if (! d_add_substitution (di, dc))
01137                 return NULL;
01138              }
01139            dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
01140                            d_template_args (di));
01141          }
01142 
01143        return dc;
01144       }
01145 
01146     default:
01147       dc = d_unqualified_name (di);
01148       if (d_peek_char (di) == 'I')
01149        {
01150          /* This is <template-args>, which means that we just saw
01151             <unscoped-template-name>, which is a substitution
01152             candidate.  */
01153          if (! d_add_substitution (di, dc))
01154            return NULL;
01155          dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
01156                          d_template_args (di));
01157        }
01158       return dc;
01159     }
01160 }
01161 
01162 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
01163                  ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
01164 */
01165 
01166 static struct demangle_component *
01167 d_nested_name (struct d_info *di)
01168 {
01169   struct demangle_component *ret;
01170   struct demangle_component **pret;
01171 
01172   if (! d_check_char (di, 'N'))
01173     return NULL;
01174 
01175   pret = d_cv_qualifiers (di, &ret, 1);
01176   if (pret == NULL)
01177     return NULL;
01178 
01179   *pret = d_prefix (di);
01180   if (*pret == NULL)
01181     return NULL;
01182 
01183   if (! d_check_char (di, 'E'))
01184     return NULL;
01185 
01186   return ret;
01187 }
01188 
01189 /* <prefix> ::= <prefix> <unqualified-name>
01190             ::= <template-prefix> <template-args>
01191             ::= <template-param>
01192             ::=
01193             ::= <substitution>
01194 
01195    <template-prefix> ::= <prefix> <(template) unqualified-name>
01196                      ::= <template-param>
01197                      ::= <substitution>
01198 */
01199 
01200 static struct demangle_component *
01201 d_prefix (struct d_info *di)
01202 {
01203   struct demangle_component *ret = NULL;
01204 
01205   while (1)
01206     {
01207       char peek;
01208       enum demangle_component_type comb_type;
01209       struct demangle_component *dc;
01210 
01211       peek = d_peek_char (di);
01212       if (peek == '\0')
01213        return NULL;
01214 
01215       /* The older code accepts a <local-name> here, but I don't see
01216         that in the grammar.  The older code does not accept a
01217         <template-param> here.  */
01218 
01219       comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
01220       if (IS_DIGIT (peek)
01221          || IS_LOWER (peek)
01222          || peek == 'C'
01223          || peek == 'D')
01224        dc = d_unqualified_name (di);
01225       else if (peek == 'S')
01226        dc = d_substitution (di, 1);
01227       else if (peek == 'I')
01228        {
01229          if (ret == NULL)
01230            return NULL;
01231          comb_type = DEMANGLE_COMPONENT_TEMPLATE;
01232          dc = d_template_args (di);
01233        }
01234       else if (peek == 'T')
01235        dc = d_template_param (di);
01236       else if (peek == 'E')
01237        return ret;
01238       else
01239        return NULL;
01240 
01241       if (ret == NULL)
01242        ret = dc;
01243       else
01244        ret = d_make_comp (di, comb_type, ret, dc);
01245 
01246       if (peek != 'S' && d_peek_char (di) != 'E')
01247        {
01248          if (! d_add_substitution (di, ret))
01249            return NULL;
01250        }
01251     }
01252 }
01253 
01254 /* <unqualified-name> ::= <operator-name>
01255                       ::= <ctor-dtor-name>
01256                       ::= <source-name>
01257 */
01258 
01259 static struct demangle_component *
01260 d_unqualified_name (struct d_info *di)
01261 {
01262   char peek;
01263 
01264   peek = d_peek_char (di);
01265   if (IS_DIGIT (peek))
01266     return d_source_name (di);
01267   else if (IS_LOWER (peek))
01268     {
01269       struct demangle_component *ret;
01270 
01271       ret = d_operator_name (di);
01272       if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
01273        di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
01274       return ret;
01275     }
01276   else if (peek == 'C' || peek == 'D')
01277     return d_ctor_dtor_name (di);
01278   else
01279     return NULL;
01280 }
01281 
01282 /* <source-name> ::= <(positive length) number> <identifier>  */
01283 
01284 static struct demangle_component *
01285 d_source_name (struct d_info *di)
01286 {
01287   long len;
01288   struct demangle_component *ret;
01289 
01290   len = d_number (di);
01291   if (len <= 0)
01292     return NULL;
01293   ret = d_identifier (di, len);
01294   di->last_name = ret;
01295   return ret;
01296 }
01297 
01298 /* number ::= [n] <(non-negative decimal integer)>  */
01299 
01300 static long
01301 d_number (struct d_info *di)
01302 {
01303   int negative;
01304   char peek;
01305   long ret;
01306 
01307   negative = 0;
01308   peek = d_peek_char (di);
01309   if (peek == 'n')
01310     {
01311       negative = 1;
01312       d_advance (di, 1);
01313       peek = d_peek_char (di);
01314     }
01315 
01316   ret = 0;
01317   while (1)
01318     {
01319       if (! IS_DIGIT (peek))
01320        {
01321          if (negative)
01322            ret = - ret;
01323          return ret;
01324        }
01325       ret = ret * 10 + peek - '0';
01326       d_advance (di, 1);
01327       peek = d_peek_char (di);
01328     }
01329 }
01330 
01331 /* identifier ::= <(unqualified source code identifier)>  */
01332 
01333 static struct demangle_component *
01334 d_identifier (struct d_info *di, int len)
01335 {
01336   const char *name;
01337 
01338   name = d_str (di);
01339 
01340   if (di->send - name < len)
01341     return NULL;
01342 
01343   d_advance (di, len);
01344 
01345   /* A Java mangled name may have a trailing '$' if it is a C++
01346      keyword.  This '$' is not included in the length count.  We just
01347      ignore the '$'.  */
01348   if ((di->options & DMGL_JAVA) != 0
01349       && d_peek_char (di) == '$')
01350     d_advance (di, 1);
01351 
01352   /* Look for something which looks like a gcc encoding of an
01353      anonymous namespace, and replace it with a more user friendly
01354      name.  */
01355   if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
01356       && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
01357                ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
01358     {
01359       const char *s;
01360 
01361       s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
01362       if ((*s == '.' || *s == '_' || *s == '$')
01363          && s[1] == 'N')
01364        {
01365          di->expansion -= len - sizeof "(anonymous namespace)";
01366          return d_make_name (di, "(anonymous namespace)",
01367                            sizeof "(anonymous namespace)" - 1);
01368        }
01369     }
01370 
01371   return d_make_name (di, name, len);
01372 }
01373 
01374 /* operator_name ::= many different two character encodings.
01375                  ::= cv <type>
01376                  ::= v <digit> <source-name>
01377 */
01378 
01379 #define NL(s) s, (sizeof s) - 1
01380 
01381 CP_STATIC_IF_GLIBCPP_V3
01382 const struct demangle_operator_info cplus_demangle_operators[] =
01383 {
01384   { "aN", NL ("&="),        2 },
01385   { "aS", NL ("="),         2 },
01386   { "aa", NL ("&&"),        2 },
01387   { "ad", NL ("&"),         1 },
01388   { "an", NL ("&"),         2 },
01389   { "cl", NL ("()"),        0 },
01390   { "cm", NL (","),         2 },
01391   { "co", NL ("~"),         1 },
01392   { "dV", NL ("/="),        2 },
01393   { "da", NL ("delete[]"),  1 },
01394   { "de", NL ("*"),         1 },
01395   { "dl", NL ("delete"),    1 },
01396   { "dv", NL ("/"),         2 },
01397   { "eO", NL ("^="),        2 },
01398   { "eo", NL ("^"),         2 },
01399   { "eq", NL ("=="),        2 },
01400   { "ge", NL (">="),        2 },
01401   { "gt", NL (">"),         2 },
01402   { "ix", NL ("[]"),        2 },
01403   { "lS", NL ("<<="),       2 },
01404   { "le", NL ("<="),        2 },
01405   { "ls", NL ("<<"),        2 },
01406   { "lt", NL ("<"),         2 },
01407   { "mI", NL ("-="),        2 },
01408   { "mL", NL ("*="),        2 },
01409   { "mi", NL ("-"),         2 },
01410   { "ml", NL ("*"),         2 },
01411   { "mm", NL ("--"),        1 },
01412   { "na", NL ("new[]"),     1 },
01413   { "ne", NL ("!="),        2 },
01414   { "ng", NL ("-"),         1 },
01415   { "nt", NL ("!"),         1 },
01416   { "nw", NL ("new"),       1 },
01417   { "oR", NL ("|="),        2 },
01418   { "oo", NL ("||"),        2 },
01419   { "or", NL ("|"),         2 },
01420   { "pL", NL ("+="),        2 },
01421   { "pl", NL ("+"),         2 },
01422   { "pm", NL ("->*"),       2 },
01423   { "pp", NL ("++"),        1 },
01424   { "ps", NL ("+"),         1 },
01425   { "pt", NL ("->"),        2 },
01426   { "qu", NL ("?"),         3 },
01427   { "rM", NL ("%="),        2 },
01428   { "rS", NL (">>="),       2 },
01429   { "rm", NL ("%"),         2 },
01430   { "rs", NL (">>"),        2 },
01431   { "st", NL ("sizeof "),   1 },
01432   { "sz", NL ("sizeof "),   1 },
01433   { NULL, NULL, 0,          0 }
01434 };
01435 
01436 static struct demangle_component *
01437 d_operator_name (struct d_info *di)
01438 {
01439   char c1;
01440   char c2;
01441 
01442   c1 = d_next_char (di);
01443   c2 = d_next_char (di);
01444   if (c1 == 'v' && IS_DIGIT (c2))
01445     return d_make_extended_operator (di, c2 - '0', d_source_name (di));
01446   else if (c1 == 'c' && c2 == 'v')
01447     return d_make_comp (di, DEMANGLE_COMPONENT_CAST,
01448                      cplus_demangle_type (di), NULL);
01449   else
01450     {
01451       /* LOW is the inclusive lower bound.  */
01452       int low = 0;
01453       /* HIGH is the exclusive upper bound.  We subtract one to ignore
01454         the sentinel at the end of the array.  */
01455       int high = ((sizeof (cplus_demangle_operators)
01456                  / sizeof (cplus_demangle_operators[0]))
01457                 - 1);
01458 
01459       while (1)
01460        {
01461          int i;
01462          const struct demangle_operator_info *p;
01463 
01464          i = low + (high - low) / 2;
01465          p = cplus_demangle_operators + i;
01466 
01467          if (c1 == p->code[0] && c2 == p->code[1])
01468            return d_make_operator (di, p);
01469 
01470          if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
01471            high = i;
01472          else
01473            low = i + 1;
01474          if (low == high)
01475            return NULL;
01476        }
01477     }
01478 }
01479 
01480 /* <special-name> ::= TV <type>
01481                   ::= TT <type>
01482                   ::= TI <type>
01483                   ::= TS <type>
01484                   ::= GV <(object) name>
01485                   ::= T <call-offset> <(base) encoding>
01486                   ::= Tc <call-offset> <call-offset> <(base) encoding>
01487    Also g++ extensions:
01488                   ::= TC <type> <(offset) number> _ <(base) type>
01489                   ::= TF <type>
01490                   ::= TJ <type>
01491                   ::= GR <name>
01492                 ::= GA <encoding>
01493 */
01494 
01495 static struct demangle_component *
01496 d_special_name (struct d_info *di)
01497 {
01498   di->expansion += 20;
01499   if (d_check_char (di, 'T'))
01500     {
01501       switch (d_next_char (di))
01502        {
01503        case 'V':
01504          di->expansion -= 5;
01505          return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
01506                            cplus_demangle_type (di), NULL);
01507        case 'T':
01508          di->expansion -= 10;
01509          return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
01510                            cplus_demangle_type (di), NULL);
01511        case 'I':
01512          return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
01513                            cplus_demangle_type (di), NULL);
01514        case 'S':
01515          return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
01516                            cplus_demangle_type (di), NULL);
01517 
01518        case 'h':
01519          if (! d_call_offset (di, 'h'))
01520            return NULL;
01521          return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
01522                            d_encoding (di, 0), NULL);
01523 
01524        case 'v':
01525          if (! d_call_offset (di, 'v'))
01526            return NULL;
01527          return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
01528                            d_encoding (di, 0), NULL);
01529 
01530        case 'c':
01531          if (! d_call_offset (di, '\0'))
01532            return NULL;
01533          if (! d_call_offset (di, '\0'))
01534            return NULL;
01535          return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
01536                            d_encoding (di, 0), NULL);
01537 
01538        case 'C':
01539          {
01540            struct demangle_component *derived_type;
01541            long offset;
01542            struct demangle_component *base_type;
01543 
01544            derived_type = cplus_demangle_type (di);
01545            offset = d_number (di);
01546            if (offset < 0)
01547              return NULL;
01548            if (! d_check_char (di, '_'))
01549              return NULL;
01550            base_type = cplus_demangle_type (di);
01551            /* We don't display the offset.  FIXME: We should display
01552               it in verbose mode.  */
01553            di->expansion += 5;
01554            return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
01555                             base_type, derived_type);
01556          }
01557 
01558        case 'F':
01559          return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
01560                            cplus_demangle_type (di), NULL);
01561        case 'J':
01562          return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
01563                            cplus_demangle_type (di), NULL);
01564 
01565        default:
01566          return NULL;
01567        }
01568     }
01569   else if (d_check_char (di, 'G'))
01570     {
01571       switch (d_next_char (di))
01572        {
01573        case 'V':
01574          return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
01575 
01576        case 'R':
01577          return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, d_name (di),
01578                            NULL);
01579 
01580        case 'A':
01581          return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
01582                            d_encoding (di, 0), NULL);
01583 
01584        default:
01585          return NULL;
01586        }
01587     }
01588   else
01589     return NULL;
01590 }
01591 
01592 /* <call-offset> ::= h <nv-offset> _
01593                  ::= v <v-offset> _
01594 
01595    <nv-offset> ::= <(offset) number>
01596 
01597    <v-offset> ::= <(offset) number> _ <(virtual offset) number>
01598 
01599    The C parameter, if not '\0', is a character we just read which is
01600    the start of the <call-offset>.
01601 
01602    We don't display the offset information anywhere.  FIXME: We should
01603    display it in verbose mode.  */
01604 
01605 static int
01606 d_call_offset (struct d_info *di, int c)
01607 {
01608   if (c == '\0')
01609     c = d_next_char (di);
01610 
01611   if (c == 'h')
01612     d_number (di);
01613   else if (c == 'v')
01614     {
01615       d_number (di);
01616       if (! d_check_char (di, '_'))
01617        return 0;
01618       d_number (di);
01619     }
01620   else
01621     return 0;
01622 
01623   if (! d_check_char (di, '_'))
01624     return 0;
01625 
01626   return 1;
01627 }
01628 
01629 /* <ctor-dtor-name> ::= C1
01630                     ::= C2
01631                     ::= C3
01632                     ::= D0
01633                     ::= D1
01634                     ::= D2
01635 */
01636 
01637 static struct demangle_component *
01638 d_ctor_dtor_name (struct d_info *di)
01639 {
01640   if (di->last_name != NULL)
01641     {
01642       if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
01643        di->expansion += di->last_name->u.s_name.len;
01644       else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
01645        di->expansion += di->last_name->u.s_string.len;
01646     }
01647   switch (d_peek_char (di))
01648     {
01649     case 'C':
01650       {
01651        enum gnu_v3_ctor_kinds kind;
01652 
01653        switch (d_peek_next_char (di))
01654          {
01655          case '1':
01656            kind = gnu_v3_complete_object_ctor;
01657            break;
01658          case '2':
01659            kind = gnu_v3_base_object_ctor;
01660            break;
01661          case '3':
01662            kind = gnu_v3_complete_object_allocating_ctor;
01663            break;
01664          default:
01665            return NULL;
01666          }
01667        d_advance (di, 2);
01668        return d_make_ctor (di, kind, di->last_name);
01669       }
01670 
01671     case 'D':
01672       {
01673        enum gnu_v3_dtor_kinds kind;
01674 
01675        switch (d_peek_next_char (di))
01676          {
01677          case '0':
01678            kind = gnu_v3_deleting_dtor;
01679            break;
01680          case '1':
01681            kind = gnu_v3_complete_object_dtor;
01682            break;
01683          case '2':
01684            kind = gnu_v3_base_object_dtor;
01685            break;
01686          default:
01687            return NULL;
01688          }
01689        d_advance (di, 2);
01690        return d_make_dtor (di, kind, di->last_name);
01691       }
01692 
01693     default:
01694       return NULL;
01695     }
01696 }
01697 
01698 /* <type> ::= <builtin-type>
01699           ::= <function-type>
01700           ::= <class-enum-type>
01701           ::= <array-type>
01702           ::= <pointer-to-member-type>
01703           ::= <template-param>
01704           ::= <template-template-param> <template-args>
01705           ::= <substitution>
01706           ::= <CV-qualifiers> <type>
01707           ::= P <type>
01708           ::= R <type>
01709           ::= C <type>
01710           ::= G <type>
01711           ::= U <source-name> <type>
01712 
01713    <builtin-type> ::= various one letter codes
01714                   ::= u <source-name>
01715 */
01716 
01717 CP_STATIC_IF_GLIBCPP_V3
01718 const struct demangle_builtin_type_info
01719 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
01720 {
01721   /* a */ { NL ("signed char"),    NL ("signed char"),  D_PRINT_DEFAULT },
01722   /* b */ { NL ("bool"),    NL ("boolean"),             D_PRINT_BOOL },
01723   /* c */ { NL ("char"),    NL ("byte"),         D_PRINT_DEFAULT },
01724   /* d */ { NL ("double"),  NL ("double"),              D_PRINT_FLOAT },
01725   /* e */ { NL ("long double"),    NL ("long double"),  D_PRINT_FLOAT },
01726   /* f */ { NL ("float"),   NL ("float"),        D_PRINT_FLOAT },
01727   /* g */ { NL ("__float128"),     NL ("__float128"),   D_PRINT_FLOAT },
01728   /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
01729   /* i */ { NL ("int"),            NL ("int"),          D_PRINT_INT },
01730   /* j */ { NL ("unsigned int"), NL ("unsigned"),       D_PRINT_UNSIGNED },
01731   /* k */ { NULL, 0,        NULL, 0,             D_PRINT_DEFAULT },
01732   /* l */ { NL ("long"),    NL ("long"),         D_PRINT_LONG },
01733   /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
01734   /* n */ { NL ("__int128"),       NL ("__int128"),     D_PRINT_DEFAULT },
01735   /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
01736            D_PRINT_DEFAULT },
01737   /* p */ { NULL, 0,        NULL, 0,             D_PRINT_DEFAULT },
01738   /* q */ { NULL, 0,        NULL, 0,             D_PRINT_DEFAULT },
01739   /* r */ { NULL, 0,        NULL, 0,             D_PRINT_DEFAULT },
01740   /* s */ { NL ("short"),   NL ("short"),        D_PRINT_DEFAULT },
01741   /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
01742   /* u */ { NULL, 0,        NULL, 0,             D_PRINT_DEFAULT },
01743   /* v */ { NL ("void"),    NL ("void"),         D_PRINT_VOID },
01744   /* w */ { NL ("wchar_t"), NL ("char"),         D_PRINT_DEFAULT },
01745   /* x */ { NL ("long long"),      NL ("long"),         D_PRINT_LONG_LONG },
01746   /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
01747            D_PRINT_UNSIGNED_LONG_LONG },
01748   /* z */ { NL ("..."),            NL ("..."),          D_PRINT_DEFAULT },
01749 };
01750 
01751 CP_STATIC_IF_GLIBCPP_V3
01752 struct demangle_component *
01753 cplus_demangle_type (struct d_info *di)
01754 {
01755   char peek;
01756   struct demangle_component *ret;
01757   int can_subst;
01758 
01759   /* The ABI specifies that when CV-qualifiers are used, the base type
01760      is substitutable, and the fully qualified type is substitutable,
01761      but the base type with a strict subset of the CV-qualifiers is
01762      not substitutable.  The natural recursive implementation of the
01763      CV-qualifiers would cause subsets to be substitutable, so instead
01764      we pull them all off now.
01765 
01766      FIXME: The ABI says that order-insensitive vendor qualifiers
01767      should be handled in the same way, but we have no way to tell
01768      which vendor qualifiers are order-insensitive and which are
01769      order-sensitive.  So we just assume that they are all
01770      order-sensitive.  g++ 3.4 supports only one vendor qualifier,
01771      __vector, and it treats it as order-sensitive when mangling
01772      names.  */
01773 
01774   peek = d_peek_char (di);
01775   if (peek == 'r' || peek == 'V' || peek == 'K')
01776     {
01777       struct demangle_component **pret;
01778 
01779       pret = d_cv_qualifiers (di, &ret, 0);
01780       if (pret == NULL)
01781        return NULL;
01782       *pret = cplus_demangle_type (di);
01783       if (! *pret || ! d_add_substitution (di, ret))
01784        return NULL;
01785       return ret;
01786     }
01787 
01788   can_subst = 1;
01789 
01790   switch (peek)
01791     {
01792     case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
01793     case 'h': case 'i': case 'j':           case 'l': case 'm': case 'n':
01794     case 'o':                               case 's': case 't':
01795     case 'v': case 'w': case 'x': case 'y': case 'z':
01796       ret = d_make_builtin_type (di,
01797                              &cplus_demangle_builtin_types[peek - 'a']);
01798       di->expansion += ret->u.s_builtin.type->len;
01799       can_subst = 0;
01800       d_advance (di, 1);
01801       break;
01802 
01803     case 'u':
01804       d_advance (di, 1);
01805       ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
01806                       d_source_name (di), NULL);
01807       break;
01808 
01809     case 'F':
01810       ret = d_function_type (di);
01811       break;
01812 
01813     case '0': case '1': case '2': case '3': case '4':
01814     case '5': case '6': case '7': case '8': case '9':
01815     case 'N':
01816     case 'Z':
01817       ret = d_class_enum_type (di);
01818       break;
01819 
01820     case 'A':
01821       ret = d_array_type (di);
01822       break;
01823 
01824     case 'M':
01825       ret = d_pointer_to_member_type (di);
01826       break;
01827 
01828     case 'T':
01829       ret = d_template_param (di);
01830       if (d_peek_char (di) == 'I')
01831        {
01832          /* This is <template-template-param> <template-args>.  The
01833             <template-template-param> part is a substitution
01834             candidate.  */
01835          if (! d_add_substitution (di, ret))
01836            return NULL;
01837          ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
01838                           d_template_args (di));
01839        }
01840       break;
01841 
01842     case 'S':
01843       /* If this is a special substitution, then it is the start of
01844         <class-enum-type>.  */
01845       {
01846        char peek_next;
01847 
01848        peek_next = d_peek_next_char (di);
01849        if (IS_DIGIT (peek_next)
01850            || peek_next == '_'
01851            || IS_UPPER (peek_next))
01852          {
01853            ret = d_substitution (di, 0);
01854            /* The substituted name may have been a template name and
01855               may be followed by tepmlate args.  */
01856            if (d_peek_char (di) == 'I')
01857              ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
01858                              d_template_args (di));
01859            else
01860              can_subst = 0;
01861          }
01862        else
01863          {
01864            ret = d_class_enum_type (di);
01865            /* If the substitution was a complete type, then it is not
01866               a new substitution candidate.  However, if the
01867               substitution was followed by template arguments, then
01868               the whole thing is a substitution candidate.  */
01869            if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
01870              can_subst = 0;
01871          }
01872       }
01873       break;
01874 
01875     case 'P':
01876       d_advance (di, 1);
01877       ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
01878                       cplus_demangle_type (di), NULL);
01879       break;
01880 
01881     case 'R':
01882       d_advance (di, 1);
01883       ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
01884                       cplus_demangle_type (di), NULL);
01885       break;
01886 
01887     case 'C':
01888       d_advance (di, 1);
01889       ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
01890                       cplus_demangle_type (di), NULL);
01891       break;
01892 
01893     case 'G':
01894       d_advance (di, 1);
01895       ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
01896                       cplus_demangle_type (di), NULL);
01897       break;
01898 
01899     case 'U':
01900       d_advance (di, 1);
01901       ret = d_source_name (di);
01902       ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
01903                       cplus_demangle_type (di), ret);
01904       break;
01905 
01906     default:
01907       return NULL;
01908     }
01909 
01910   if (can_subst)
01911     {
01912       if (! d_add_substitution (di, ret))
01913        return NULL;
01914     }
01915 
01916   return ret;
01917 }
01918 
01919 /* <CV-qualifiers> ::= [r] [V] [K]  */
01920 
01921 static struct demangle_component **
01922 d_cv_qualifiers (struct d_info *di,
01923                  struct demangle_component **pret, int member_fn)
01924 {
01925   char peek;
01926 
01927   peek = d_peek_char (di);
01928   while (peek == 'r' || peek == 'V' || peek == 'K')
01929     {
01930       enum demangle_component_type t;
01931 
01932       d_advance (di, 1);
01933       if (peek == 'r')
01934        {
01935          t = (member_fn
01936               ? DEMANGLE_COMPONENT_RESTRICT_THIS
01937               : DEMANGLE_COMPONENT_RESTRICT);
01938          di->expansion += sizeof "restrict";
01939        }
01940       else if (peek == 'V')
01941        {
01942          t = (member_fn
01943               ? DEMANGLE_COMPONENT_VOLATILE_THIS
01944               : DEMANGLE_COMPONENT_VOLATILE);
01945          di->expansion += sizeof "volatile";
01946        }
01947       else
01948        {
01949          t = (member_fn
01950               ? DEMANGLE_COMPONENT_CONST_THIS
01951               : DEMANGLE_COMPONENT_CONST);
01952          di->expansion += sizeof "const";
01953        }
01954 
01955       *pret = d_make_comp (di, t, NULL, NULL);
01956       if (*pret == NULL)
01957        return NULL;
01958       pret = &d_left (*pret);
01959 
01960       peek = d_peek_char (di);
01961     }
01962 
01963   return pret;
01964 }
01965 
01966 /* <function-type> ::= F [Y] <bare-function-type> E  */
01967 
01968 static struct demangle_component *
01969 d_function_type (struct d_info *di)
01970 {
01971   struct demangle_component *ret;
01972 
01973   if (! d_check_char (di, 'F'))
01974     return NULL;
01975   if (d_peek_char (di) == 'Y')
01976     {
01977       /* Function has C linkage.  We don't print this information.
01978         FIXME: We should print it in verbose mode.  */
01979       d_advance (di, 1);
01980     }
01981   ret = d_bare_function_type (di, 1);
01982   if (! d_check_char (di, 'E'))
01983     return NULL;
01984   return ret;
01985 }
01986 
01987 /* <bare-function-type> ::= [J]<type>+  */
01988 
01989 static struct demangle_component *
01990 d_bare_function_type (struct d_info *di, int has_return_type)
01991 {
01992   struct demangle_component *return_type;
01993   struct demangle_component *tl;
01994   struct demangle_component **ptl;
01995   char peek;
01996 
01997   /* Detect special qualifier indicating that the first argument
01998      is the return type.  */
01999   peek = d_peek_char (di);
02000   if (peek == 'J')
02001     {
02002       d_advance (di, 1);
02003       has_return_type = 1;
02004     }
02005 
02006   return_type = NULL;
02007   tl = NULL;
02008   ptl = &tl;
02009   while (1)
02010     {
02011       struct demangle_component *type;
02012 
02013       peek = d_peek_char (di);
02014       if (peek == '\0' || peek == 'E')
02015        break;
02016       type = cplus_demangle_type (di);
02017       if (type == NULL)
02018        return NULL;
02019       if (has_return_type)
02020        {
02021          return_type = type;
02022          has_return_type = 0;
02023        }
02024       else
02025        {
02026          *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
02027          if (*ptl == NULL)
02028            return NULL;
02029          ptl = &d_right (*ptl);
02030        }
02031     }
02032 
02033   /* There should be at least one parameter type besides the optional
02034      return type.  A function which takes no arguments will have a
02035      single parameter type void.  */
02036   if (tl == NULL)
02037     return NULL;
02038 
02039   /* If we have a single parameter type void, omit it.  */
02040   if (d_right (tl) == NULL
02041       && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
02042       && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
02043     {
02044       di->expansion -= d_left (tl)->u.s_builtin.type->len;
02045       tl = NULL;
02046     }
02047 
02048   return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE, return_type, tl);
02049 }
02050 
02051 /* <class-enum-type> ::= <name>  */
02052 
02053 static struct demangle_component *
02054 d_class_enum_type (struct d_info *di)
02055 {
02056   return d_name (di);
02057 }
02058 
02059 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
02060                 ::= A [<(dimension) expression>] _ <(element) type>
02061 */
02062 
02063 static struct demangle_component *
02064 d_array_type (struct d_info *di)
02065 {
02066   char peek;
02067   struct demangle_component *dim;
02068 
02069   if (! d_check_char (di, 'A'))
02070     return NULL;
02071 
02072   peek = d_peek_char (di);
02073   if (peek == '_')
02074     dim = NULL;
02075   else if (IS_DIGIT (peek))
02076     {
02077       const char *s;
02078 
02079       s = d_str (di);
02080       do
02081        {
02082          d_advance (di, 1);
02083          peek = d_peek_char (di);
02084        }
02085       while (IS_DIGIT (peek));
02086       dim = d_make_name (di, s, d_str (di) - s);
02087       if (dim == NULL)
02088        return NULL;
02089     }
02090   else
02091     {
02092       dim = d_expression (di);
02093       if (dim == NULL)
02094        return NULL;
02095     }
02096 
02097   if (! d_check_char (di, '_'))
02098     return NULL;
02099 
02100   return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
02101                     cplus_demangle_type (di));
02102 }
02103 
02104 /* <pointer-to-member-type> ::= M <(class) type> <(member) type>  */
02105 
02106 static struct demangle_component *
02107 d_pointer_to_member_type (struct d_info *di)
02108 {
02109   struct demangle_component *cl;
02110   struct demangle_component *mem;
02111   struct demangle_component **pmem;
02112 
02113   if (! d_check_char (di, 'M'))
02114     return NULL;
02115 
02116   cl = cplus_demangle_type (di);
02117 
02118   /* The ABI specifies that any type can be a substitution source, and
02119      that M is followed by two types, and that when a CV-qualified
02120      type is seen both the base type and the CV-qualified types are
02121      substitution sources.  The ABI also specifies that for a pointer
02122      to a CV-qualified member function, the qualifiers are attached to
02123      the second type.  Given the grammar, a plain reading of the ABI
02124      suggests that both the CV-qualified member function and the
02125      non-qualified member function are substitution sources.  However,
02126      g++ does not work that way.  g++ treats only the CV-qualified
02127      member function as a substitution source.  FIXME.  So to work
02128      with g++, we need to pull off the CV-qualifiers here, in order to
02129      avoid calling add_substitution() in cplus_demangle_type().  But
02130      for a CV-qualified member which is not a function, g++ does
02131      follow the ABI, so we need to handle that case here by calling
02132      d_add_substitution ourselves.  */
02133 
02134   pmem = d_cv_qualifiers (di, &mem, 1);
02135   if (pmem == NULL)
02136     return NULL;
02137   *pmem = cplus_demangle_type (di);
02138   if (*pmem == NULL)
02139     return NULL;
02140 
02141   if (pmem != &mem && (*pmem)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
02142     {
02143       if (! d_add_substitution (di, mem))
02144        return NULL;
02145     }
02146 
02147   return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
02148 }
02149 
02150 /* <template-param> ::= T_
02151                     ::= T <(parameter-2 non-negative) number> _
02152 */
02153 
02154 static struct demangle_component *
02155 d_template_param (struct d_info *di)
02156 {
02157   long param;
02158 
02159   if (! d_check_char (di, 'T'))
02160     return NULL;
02161 
02162   if (d_peek_char (di) == '_')
02163     param = 0;
02164   else
02165     {
02166       param = d_number (di);
02167       if (param < 0)
02168        return NULL;
02169       param += 1;
02170     }
02171 
02172   if (! d_check_char (di, '_'))
02173     return NULL;
02174 
02175   ++di->did_subs;
02176 
02177   return d_make_template_param (di, param);
02178 }
02179 
02180 /* <template-args> ::= I <template-arg>+ E  */
02181 
02182 static struct demangle_component *
02183 d_template_args (struct d_info *di)
02184 {
02185   struct demangle_component *hold_last_name;
02186   struct demangle_component *al;
02187   struct demangle_component **pal;
02188 
02189   /* Preserve the last name we saw--don't let the template arguments
02190      clobber it, as that would give us the wrong name for a subsequent
02191      constructor or destructor.  */
02192   hold_last_name = di->last_name;
02193 
02194   if (! d_check_char (di, 'I'))
02195     return NULL;
02196 
02197   al = NULL;
02198   pal = &al;
02199   while (1)
02200     {
02201       struct demangle_component *a;
02202 
02203       a = d_template_arg (di);
02204       if (a == NULL)
02205        return NULL;
02206 
02207       *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
02208       if (*pal == NULL)
02209        return NULL;
02210       pal = &d_right (*pal);
02211 
02212       if (d_peek_char (di) == 'E')
02213        {
02214          d_advance (di, 1);
02215          break;
02216        }
02217     }
02218 
02219   di->last_name = hold_last_name;
02220 
02221   return al;
02222 }
02223 
02224 /* <template-arg> ::= <type>
02225                   ::= X <expression> E
02226                   ::= <expr-primary>
02227 */
02228 
02229 static struct demangle_component *
02230 d_template_arg (struct d_info *di)
02231 {
02232   struct demangle_component *ret;
02233 
02234   switch (d_peek_char (di))
02235     {
02236     case 'X':
02237       d_advance (di, 1);
02238       ret = d_expression (di);
02239       if (! d_check_char (di, 'E'))
02240        return NULL;
02241       return ret;
02242 
02243     case 'L':
02244       return d_expr_primary (di);
02245 
02246     default:
02247       return cplus_demangle_type (di);
02248     }
02249 }
02250 
02251 /* <expression> ::= <(unary) operator-name> <expression>
02252                 ::= <(binary) operator-name> <expression> <expression>
02253                 ::= <(trinary) operator-name> <expression> <expression> <expression>
02254                 ::= st <type>
02255                 ::= <template-param>
02256                 ::= sr <type> <unqualified-name>
02257                 ::= sr <type> <unqualified-name> <template-args>
02258                 ::= <expr-primary>
02259 */
02260 
02261 static struct demangle_component *
02262 d_expression (struct d_info *di)
02263 {
02264   char peek;
02265 
02266   peek = d_peek_char (di);
02267   if (peek == 'L')
02268     return d_expr_primary (di);
02269   else if (peek == 'T')
02270     return d_template_param (di);
02271   else if (peek == 's' && d_peek_next_char (di) == 'r')
02272     {
02273       struct demangle_component *type;
02274       struct demangle_component *name;
02275 
02276       d_advance (di, 2);
02277       type = cplus_demangle_type (di);
02278       name = d_unqualified_name (di);
02279       if (d_peek_char (di) != 'I')
02280        return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
02281       else
02282        return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
02283                          d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
02284                                     d_template_args (di)));
02285     }
02286   else
02287     {
02288       struct demangle_component *op;
02289       int args;
02290 
02291       op = d_operator_name (di);
02292       if (op == NULL)
02293        return NULL;
02294 
02295       if (op->type == DEMANGLE_COMPONENT_OPERATOR)
02296        di->expansion += op->u.s_operator.op->len - 2;
02297 
02298       if (op->type == DEMANGLE_COMPONENT_OPERATOR
02299          && strcmp (op->u.s_operator.op->code, "st") == 0)
02300        return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
02301                          cplus_demangle_type (di));
02302 
02303       switch (op->type)
02304        {
02305        default:
02306          return NULL;
02307        case DEMANGLE_COMPONENT_OPERATOR:
02308          args = op->u.s_operator.op->args;
02309          break;
02310        case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
02311          args = op->u.s_extended_operator.args;
02312          break;
02313        case DEMANGLE_COMPONENT_CAST:
02314          args = 1;
02315          break;
02316        }
02317 
02318       switch (args)
02319        {
02320        case 1:
02321          return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
02322                            d_expression (di));
02323        case 2:
02324          {
02325            struct demangle_component *left;
02326 
02327            left = d_expression (di);
02328            return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
02329                             d_make_comp (di,
02330                                         DEMANGLE_COMPONENT_BINARY_ARGS,
02331                                         left,
02332                                         d_expression (di)));
02333          }
02334        case 3:
02335          {
02336            struct demangle_component *first;
02337            struct demangle_component *second;
02338 
02339            first = d_expression (di);
02340            second = d_expression (di);
02341            return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
02342                             d_make_comp (di,
02343                                         DEMANGLE_COMPONENT_TRINARY_ARG1,
02344                                         first,
02345                                         d_make_comp (di,
02346                                                    DEMANGLE_COMPONENT_TRINARY_ARG2,
02347                                                    second,
02348                                                    d_expression (di))));
02349          }
02350        default:
02351          return NULL;
02352        }
02353     }
02354 }
02355 
02356 /* <expr-primary> ::= L <type> <(value) number> E
02357                   ::= L <type> <(value) float> E
02358                   ::= L <mangled-name> E
02359 */
02360 
02361 static struct demangle_component *
02362 d_expr_primary (struct d_info *di)
02363 {
02364   struct demangle_component *ret;
02365 
02366   if (! d_check_char (di, 'L'))
02367     return NULL;
02368   if (d_peek_char (di) == '_')
02369     ret = cplus_demangle_mangled_name (di, 0);
02370   else
02371     {
02372       struct demangle_component *type;
02373       enum demangle_component_type t;
02374       const char *s;
02375 
02376       type = cplus_demangle_type (di);
02377       if (type == NULL)
02378        return NULL;
02379 
02380       /* If we have a type we know how to print, we aren't going to
02381         print the type name itself.  */
02382       if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
02383          && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
02384        di->expansion -= type->u.s_builtin.type->len;
02385 
02386       /* Rather than try to interpret the literal value, we just
02387         collect it as a string.  Note that it's possible to have a
02388         floating point literal here.  The ABI specifies that the
02389         format of such literals is machine independent.  That's fine,
02390         but what's not fine is that versions of g++ up to 3.2 with
02391         -fabi-version=1 used upper case letters in the hex constant,
02392         and dumped out gcc's internal representation.  That makes it
02393         hard to tell where the constant ends, and hard to dump the
02394         constant in any readable form anyhow.  We don't attempt to
02395         handle these cases.  */
02396 
02397       t = DEMANGLE_COMPONENT_LITERAL;
02398       if (d_peek_char (di) == 'n')
02399        {
02400          t = DEMANGLE_COMPONENT_LITERAL_NEG;
02401          d_advance (di, 1);
02402        }
02403       s = d_str (di);
02404       while (d_peek_char (di) != 'E')
02405        {
02406          if (d_peek_char (di) == '\0')
02407            return NULL;
02408          d_advance (di, 1);
02409        }
02410       ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
02411     }
02412   if (! d_check_char (di, 'E'))
02413     return NULL;
02414   return ret;
02415 }
02416 
02417 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
02418                 ::= Z <(function) encoding> E s [<discriminator>]
02419 */
02420 
02421 static struct demangle_component *
02422 d_local_name (struct d_info *di)
02423 {
02424   struct demangle_component *function;
02425 
02426   if (! d_check_char (di, 'Z'))
02427     return NULL;
02428 
02429   function = d_encoding (di, 0);
02430 
02431   if (! d_check_char (di, 'E'))
02432     return NULL;
02433 
02434   if (d_peek_char (di) == 's')
02435     {
02436       d_advance (di, 1);
02437       if (! d_discriminator (di))
02438        return NULL;
02439       return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
02440                        d_make_name (di, "string literal",
02441                                    sizeof "string literal" - 1));
02442     }
02443   else
02444     {
02445       struct demangle_component *name;
02446 
02447       name = d_name (di);
02448       if (! d_discriminator (di))
02449        return NULL;
02450       return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
02451     }
02452 }
02453 
02454 /* <discriminator> ::= _ <(non-negative) number>
02455 
02456    We demangle the discriminator, but we don't print it out.  FIXME:
02457    We should print it out in verbose mode.  */
02458 
02459 static int
02460 d_discriminator (struct d_info *di)
02461 {
02462   long discrim;
02463 
02464   if (d_peek_char (di) != '_')
02465     return 1;
02466   d_advance (di, 1);
02467   discrim = d_number (di);
02468   if (discrim < 0)
02469     return 0;
02470   return 1;
02471 }
02472 
02473 /* Add a new substitution.  */
02474 
02475 static int
02476 d_add_substitution (struct d_info *di, struct demangle_component *dc)
02477 {
02478   if (dc == NULL)
02479     return 0;
02480   if (di->next_sub >= di->num_subs)
02481     return 0;
02482   di->subs[di->next_sub] = dc;
02483   ++di->next_sub;
02484   return 1;
02485 }
02486 
02487 /* <substitution> ::= S <seq-id> _
02488                   ::= S_
02489                   ::= St
02490                   ::= Sa
02491                   ::= Sb
02492                   ::= Ss
02493                   ::= Si
02494                   ::= So
02495                   ::= Sd
02496 
02497    If PREFIX is non-zero, then this type is being used as a prefix in
02498    a qualified name.  In this case, for the standard substitutions, we
02499    need to check whether we are being used as a prefix for a
02500    constructor or destructor, and return a full template name.
02501    Otherwise we will get something like std::iostream::~iostream()
02502    which does not correspond particularly well to any function which
02503    actually appears in the source.
02504 */
02505 
02506 static const struct d_standard_sub_info standard_subs[] =
02507 {
02508   { 't', NL ("std"),
02509     NL ("std"),
02510     NULL, 0 },
02511   { 'a', NL ("std::allocator"),
02512     NL ("std::allocator"),
02513     NL ("allocator") },
02514   { 'b', NL ("std::basic_string"),
02515     NL ("std::basic_string"),
02516     NL ("basic_string") },
02517   { 's', NL ("std::string"),
02518     NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
02519     NL ("basic_string") },
02520   { 'i', NL ("std::istream"),
02521     NL ("std::basic_istream<char, std::char_traits<char> >"),
02522     NL ("basic_istream") },
02523   { 'o', NL ("std::ostream"),
02524     NL ("std::basic_ostream<char, std::char_traits<char> >"),
02525     NL ("basic_ostream") },
02526   { 'd', NL ("std::iostream"),
02527     NL ("std::basic_iostream<char, std::char_traits<char> >"),
02528     NL ("basic_iostream") }
02529 };
02530 
02531 static struct demangle_component *
02532 d_substitution (struct d_info *di, int prefix)
02533 {
02534   char c;
02535 
02536   if (! d_check_char (di, 'S'))
02537     return NULL;
02538 
02539   c = d_next_char (di);
02540   if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
02541     {
02542       int id;
02543 
02544       id = 0;
02545       if (c != '_')
02546        {
02547          do
02548            {
02549              if (IS_DIGIT (c))
02550               id = id * 36 + c - '0';
02551              else if (IS_UPPER (c))
02552               id = id * 36 + c - 'A' + 10;
02553              else
02554               return NULL;
02555              if (id < 0)
02556               return NULL;
02557              c = d_next_char (di);
02558            }
02559          while (c != '_');
02560 
02561          ++id;
02562        }
02563 
02564       if (id >= di->next_sub)
02565        return NULL;
02566 
02567       ++di->did_subs;
02568 
02569       return di->subs[id];
02570     }
02571   else
02572     {
02573       int verbose;
02574       const struct d_standard_sub_info *p;
02575       const struct d_standard_sub_info *pend;
02576 
02577       verbose = (di->options & DMGL_VERBOSE) != 0;
02578       if (! verbose && prefix)
02579        {
02580          char peek;
02581 
02582          peek = d_peek_char (di);
02583          if (peek == 'C' || peek == 'D')
02584            verbose = 1;
02585        }
02586 
02587       pend = (&standard_subs[0]
02588              + sizeof standard_subs / sizeof standard_subs[0]);
02589       for (p = &standard_subs[0]; p < pend; ++p)
02590        {
02591          if (c == p->code)
02592            {
02593              const char *s;
02594              int len;
02595 
02596              if (p->set_last_name != NULL)
02597               di->last_name = d_make_sub (di, p->set_last_name,
02598                                        p->set_last_name_len);
02599              if (verbose)
02600               {
02601                 s = p->full_expansion;
02602                 len = p->full_len;
02603               }
02604              else
02605               {
02606                 s = p->simple_expansion;
02607                 len = p->simple_len;
02608               }
02609              di->expansion += len;
02610              return d_make_sub (di, s, len);
02611            }
02612        }
02613 
02614       return NULL;
02615     }
02616 }
02617 
02618 /* Initialize a growable string.  */
02619 
02620 static void
02621 d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
02622 {
02623   dgs->buf = NULL;
02624   dgs->len = 0;
02625   dgs->alc = 0;
02626   dgs->allocation_failure = 0;
02627 
02628   if (estimate > 0)
02629     d_growable_string_resize (dgs, estimate);
02630 }
02631 
02632 /* Grow a growable string to a given size.  */
02633 
02634 static inline void
02635 d_growable_string_resize (struct d_growable_string *dgs, size_t need)
02636 {
02637   size_t newalc;
02638   char *newbuf;
02639 
02640   if (dgs->allocation_failure)
02641     return;
02642 
02643   /* Start allocation at two bytes to avoid any possibility of confusion
02644      with the special value of 1 used as a return in *palc to indicate
02645      allocation failures.  */
02646   newalc = dgs->alc > 0 ? dgs->alc : 2;
02647   while (newalc < need)
02648     newalc <<= 1;
02649 
02650   newbuf = (char *) realloc (dgs->buf, newalc);
02651   if (newbuf == NULL)
02652     {
02653       free (dgs->buf);
02654       dgs->buf = NULL;
02655       dgs->len = 0;
02656       dgs->alc = 0;
02657       dgs->allocation_failure = 1;
02658       return;
02659     }
02660   dgs->buf = newbuf;
02661   dgs->alc = newalc;
02662 }
02663 
02664 /* Append a buffer to a growable string.  */
02665 
02666 static inline void
02667 d_growable_string_append_buffer (struct d_growable_string *dgs,
02668                                  const char *s, size_t l)
02669 {
02670   size_t need;
02671 
02672   need = dgs->len + l + 1;
02673   if (need > dgs->alc)
02674     d_growable_string_resize (dgs, need);
02675 
02676   if (dgs->allocation_failure)
02677     return;
02678 
02679   memcpy (dgs->buf + dgs->len, s, l);
02680   dgs->buf[dgs->len + l] = '\0';
02681   dgs->len += l;
02682 }
02683 
02684 /* Bridge growable strings to the callback mechanism.  */
02685 
02686 static void
02687 d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
02688 {
02689   struct d_growable_string *dgs = (struct d_growable_string*) opaque;
02690 
02691   d_growable_string_append_buffer (dgs, s, l);
02692 }
02693 
02694 /* Initialize a print information structure.  */
02695 
02696 static void
02697 d_print_init (struct d_print_info *dpi, int options,
02698               demangle_callbackref callback, void *opaque)
02699 {
02700   dpi->options = options;
02701   dpi->len = 0;
02702   dpi->last_char = '\0';
02703   dpi->templates = NULL;
02704   dpi->modifiers = NULL;
02705 
02706   dpi->callback = callback;
02707   dpi->opaque = opaque;
02708 
02709   dpi->demangle_failure = 0;
02710 }
02711 
02712 /* Indicate that an error occurred during printing, and test for error.  */
02713 
02714 static inline void
02715 d_print_error (struct d_print_info *dpi)
02716 {
02717   dpi->demangle_failure = 1;
02718 }
02719 
02720 static inline int
02721 d_print_saw_error (struct d_print_info *dpi)
02722 {
02723   return dpi->demangle_failure != 0;
02724 }
02725 
02726 /* Flush buffered characters to the callback.  */
02727 
02728 static inline void
02729 d_print_flush (struct d_print_info *dpi)
02730 {
02731   dpi->buf[dpi->len] = '\0';
02732   dpi->callback (dpi->buf, dpi->len, dpi->opaque);
02733   dpi->len = 0;
02734 }
02735 
02736 /* Append characters and buffers for printing.  */
02737 
02738 static inline void
02739 d_append_char (struct d_print_info *dpi, char c)
02740 {
02741   if (dpi->len == sizeof (dpi->buf) - 1)
02742     d_print_flush (dpi);
02743 
02744   dpi->buf[dpi->len++] = c;
02745   dpi->last_char = c;
02746 }
02747 
02748 static inline void
02749 d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
02750 {
02751   size_t i;
02752 
02753   for (i = 0; i < l; i++)
02754     d_append_char (dpi, s[i]);
02755 }
02756 
02757 static inline void
02758 d_append_string (struct d_print_info *dpi, const char *s)
02759 {
02760   d_append_buffer (dpi, s, strlen (s));
02761 }
02762 
02763 static inline char
02764 d_last_char (struct d_print_info *dpi)
02765 {
02766   return dpi->last_char;
02767 }
02768 
02769 /* Turn components into a human readable string.  OPTIONS is the
02770    options bits passed to the demangler.  DC is the tree to print.
02771    CALLBACK is a function to call to flush demangled string segments
02772    as they fill the intermediate buffer, and OPAQUE is a generalized
02773    callback argument.  On success, this returns 1.  On failure,
02774    it returns 0, indicating a bad parse.  It does not use heap
02775    memory to build an output string, so cannot encounter memory
02776    allocation failure.  */
02777 
02778 CP_STATIC_IF_GLIBCPP_V3
02779 int
02780 cplus_demangle_print_callback (int options,
02781                                const struct demangle_component *dc,
02782                                demangle_callbackref callback, void *opaque)
02783 {
02784   struct d_print_info dpi;
02785 
02786   d_print_init (&dpi, options, callback, opaque);
02787 
02788   d_print_comp (&dpi, dc);
02789 
02790   d_print_flush (&dpi);
02791 
02792   return ! d_print_saw_error (&dpi);
02793 }
02794 
02795 /* Turn components into a human readable string.  OPTIONS is the
02796    options bits passed to the demangler.  DC is the tree to print.
02797    ESTIMATE is a guess at the length of the result.  This returns a
02798    string allocated by malloc, or NULL on error.  On success, this
02799    sets *PALC to the size of the allocated buffer.  On failure, this
02800    sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
02801    failure.  */
02802 
02803 CP_STATIC_IF_GLIBCPP_V3
02804 char *
02805 cplus_demangle_print (int options, const struct demangle_component *dc,
02806                       int estimate, size_t *palc)
02807 {
02808   struct d_growable_string dgs;
02809 
02810   d_growable_string_init (&dgs, estimate);
02811 
02812   if (! cplus_demangle_print_callback (options, dc,
02813                                        d_growable_string_callback_adapter,
02814                                        &dgs))
02815     {
02816       free (dgs.buf);
02817       *palc = 0;
02818       return NULL;
02819     }
02820 
02821   *palc = dgs.allocation_failure ? 1 : dgs.alc;
02822   return dgs.buf;
02823 }
02824 
02825 /* Subroutine to handle components.  */
02826 
02827 static void
02828 d_print_comp (struct d_print_info *dpi,
02829               const struct demangle_component *dc)
02830 {
02831   if (dc == NULL)
02832     {
02833       d_print_error (dpi);
02834       return;
02835     }
02836   if (d_print_saw_error (dpi))
02837     return;
02838 
02839   switch (dc->type)
02840     {
02841     case DEMANGLE_COMPONENT_NAME:
02842       if ((dpi->options & DMGL_JAVA) == 0)
02843        d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
02844       else
02845        d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
02846       return;
02847 
02848     case DEMANGLE_COMPONENT_QUAL_NAME:
02849     case DEMANGLE_COMPONENT_LOCAL_NAME:
02850       d_print_comp (dpi, d_left (dc));
02851       if ((dpi->options & DMGL_JAVA) == 0)
02852        d_append_string (dpi, "::");
02853       else
02854        d_append_char (dpi, '.');
02855       d_print_comp (dpi, d_right (dc));
02856       return;
02857 
02858     case DEMANGLE_COMPONENT_TYPED_NAME:
02859       {
02860        struct d_print_mod *hold_modifiers;
02861        struct demangle_component *typed_name;
02862        struct d_print_mod adpm[4];
02863        unsigned int i;
02864        struct d_print_template dpt;
02865 
02866        /* Pass the name down to the type so that it can be printed in
02867           the right place for the type.  We also have to pass down
02868           any CV-qualifiers, which apply to the this parameter.  */
02869        hold_modifiers = dpi->modifiers;
02870        i = 0;
02871        typed_name = d_left (dc);
02872        while (typed_name != NULL)
02873          {
02874            if (i >= sizeof adpm / sizeof adpm[0])
02875              {
02876               d_print_error (dpi);
02877               return;
02878              }
02879 
02880            adpm[i].next = dpi->modifiers;
02881            dpi->modifiers = &adpm[i];
02882            adpm[i].mod = typed_name;
02883            adpm[i].printed = 0;
02884            adpm[i].templates = dpi->templates;
02885            ++i;
02886 
02887            if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
02888               && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
02889               && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS)
02890              break;
02891 
02892            typed_name = d_left (typed_name);
02893          }
02894 
02895        /* If typed_name is a template, then it applies to the
02896           function type as well.  */
02897        if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
02898          {
02899            dpt.next = dpi->templates;
02900            dpi->templates = &dpt;
02901            dpt.template_decl = typed_name;
02902          }
02903 
02904        /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
02905           there may be CV-qualifiers on its right argument which
02906           really apply here; this happens when parsing a class which
02907           is local to a function.  */
02908        if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
02909          {
02910            struct demangle_component *local_name;
02911 
02912            local_name = d_right (typed_name);
02913            while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
02914                  || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
02915                  || local_name->type == DEMANGLE_COMPONENT_CONST_THIS)
02916              {
02917               if (i >= sizeof adpm / sizeof adpm[0])
02918                 {
02919                   d_print_error (dpi);
02920                   return;
02921                 }
02922 
02923               adpm[i] = adpm[i - 1];
02924               adpm[i].next = &adpm[i - 1];
02925               dpi->modifiers = &adpm[i];
02926 
02927               adpm[i - 1].mod = local_name;
02928               adpm[i - 1].printed = 0;
02929               adpm[i - 1].templates = dpi->templates;
02930               ++i;
02931 
02932               local_name = d_left (local_name);
02933              }
02934          }
02935 
02936        d_print_comp (dpi, d_right (dc));
02937 
02938        if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
02939          dpi->templates = dpt.next;
02940 
02941        /* If the modifiers didn't get printed by the type, print them
02942           now.  */
02943        while (i > 0)
02944          {
02945            --i;
02946            if (! adpm[i].printed)
02947              {
02948               d_append_char (dpi, ' ');
02949               d_print_mod (dpi, adpm[i].mod);
02950              }
02951          }
02952 
02953        dpi->modifiers = hold_modifiers;
02954 
02955        return;
02956       }
02957 
02958     case DEMANGLE_COMPONENT_TEMPLATE:
02959       {
02960        struct d_print_mod *hold_dpm;
02961        struct demangle_component *dcl;
02962 
02963        /* Don't push modifiers into a template definition.  Doing so
02964           could give the wrong definition for a template argument.
02965           Instead, treat the template essentially as a name.  */
02966 
02967        hold_dpm = dpi->modifiers;
02968        dpi->modifiers = NULL;
02969 
02970         dcl = d_left (dc);
02971 
02972         if ((dpi->options & DMGL_JAVA) != 0
02973             && dcl->type == DEMANGLE_COMPONENT_NAME
02974             && dcl->u.s_name.len == 6
02975             && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
02976           {
02977             /* Special-case Java arrays, so that JArray<TYPE> appears
02978                instead as TYPE[].  */
02979 
02980             d_print_comp (dpi, d_right (dc));
02981             d_append_string (dpi, "[]");
02982           }
02983         else
02984           {
02985            d_print_comp (dpi, dcl);
02986            if (d_last_char (dpi) == '<')
02987              d_append_char (dpi, ' ');
02988            d_append_char (dpi, '<');
02989            d_print_comp (dpi, d_right (dc));
02990            /* Avoid generating two consecutive '>' characters, to avoid
02991               the C++ syntactic ambiguity.  */
02992            if (d_last_char (dpi) == '>')
02993              d_append_char (dpi, ' ');
02994            d_append_char (dpi, '>');
02995           }
02996 
02997        dpi->modifiers = hold_dpm;
02998 
02999        return;
03000       }
03001 
03002     case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
03003       {
03004        long i;
03005        struct demangle_component *a;
03006        struct d_print_template *hold_dpt;
03007 
03008        if (dpi->templates == NULL)
03009          {
03010            d_print_error (dpi);
03011            return;
03012          }
03013        i = dc->u.s_number.number;
03014        for (a = d_right (dpi->templates->template_decl);
03015             a != NULL;
03016             a = d_right (a))
03017          {
03018            if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
03019              {
03020               d_print_error (dpi);
03021               return;
03022              }
03023            if (i <= 0)
03024              break;
03025            --i;
03026          }
03027        if (i != 0 || a == NULL)
03028          {
03029            d_print_error (dpi);
03030            return;
03031          }
03032 
03033        /* While processing this parameter, we need to pop the list of
03034           templates.  This is because the template parameter may
03035           itself be a reference to a parameter of an outer
03036           template.  */
03037 
03038        hold_dpt = dpi->templates;
03039        dpi->templates = hold_dpt->next;
03040 
03041        d_print_comp (dpi, d_left (a));
03042 
03043        dpi->templates = hold_dpt;
03044 
03045        return;
03046       }
03047 
03048     case DEMANGLE_COMPONENT_CTOR:
03049       d_print_comp (dpi, dc->u.s_ctor.name);
03050       return;
03051 
03052     case DEMANGLE_COMPONENT_DTOR:
03053       d_append_char (dpi, '~');
03054       d_print_comp (dpi, dc->u.s_dtor.name);
03055       return;
03056 
03057     case DEMANGLE_COMPONENT_VTABLE:
03058       d_append_string (dpi, "vtable for ");
03059       d_print_comp (dpi, d_left (dc));
03060       return;
03061 
03062     case DEMANGLE_COMPONENT_VTT:
03063       d_append_string (dpi, "VTT for ");
03064       d_print_comp (dpi, d_left (dc));
03065       return;
03066 
03067     case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
03068       d_append_string (dpi, "construction vtable for ");
03069       d_print_comp (dpi, d_left (dc));
03070       d_append_string (dpi, "-in-");
03071       d_print_comp (dpi, d_right (dc));
03072       return;
03073 
03074     case DEMANGLE_COMPONENT_TYPEINFO:
03075       d_append_string (dpi, "typeinfo for ");
03076       d_print_comp (dpi, d_left (dc));
03077       return;
03078 
03079     case DEMANGLE_COMPONENT_TYPEINFO_NAME:
03080       d_append_string (dpi, "typeinfo name for ");
03081       d_print_comp (dpi, d_left (dc));
03082       return;
03083 
03084     case DEMANGLE_COMPONENT_TYPEINFO_FN:
03085       d_append_string (dpi, "typeinfo fn for ");
03086       d_print_comp (dpi, d_left (dc));
03087       return;
03088 
03089     case DEMANGLE_COMPONENT_THUNK:
03090       d_append_string (dpi, "non-virtual thunk to ");
03091       d_print_comp (dpi, d_left (dc));
03092       return;
03093 
03094     case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
03095       d_append_string (dpi, "virtual thunk to ");
03096       d_print_comp (dpi, d_left (dc));
03097       return;
03098 
03099     case DEMANGLE_COMPONENT_COVARIANT_THUNK:
03100       d_append_string (dpi, "covariant return thunk to ");
03101       d_print_comp (dpi, d_left (dc));
03102       return;
03103 
03104     case DEMANGLE_COMPONENT_JAVA_CLASS:
03105       d_append_string (dpi, "java Class for ");
03106       d_print_comp (dpi, d_left (dc));
03107       return;
03108 
03109     case DEMANGLE_COMPONENT_GUARD:
03110       d_append_string (dpi, "guard variable for ");
03111       d_print_comp (dpi, d_left (dc));
03112       return;
03113 
03114     case DEMANGLE_COMPONENT_REFTEMP:
03115       d_append_string (dpi, "reference temporary for ");
03116       d_print_comp (dpi, d_left (dc));
03117       return;
03118 
03119     case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
03120       d_append_string (dpi, "hidden alias for ");
03121       d_print_comp (dpi, d_left (dc));
03122       return;
03123 
03124     case DEMANGLE_COMPONENT_SUB_STD:
03125       d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
03126       return;
03127 
03128     case DEMANGLE_COMPONENT_RESTRICT:
03129     case DEMANGLE_COMPONENT_VOLATILE:
03130     case DEMANGLE_COMPONENT_CONST:
03131       {
03132        struct d_print_mod *pdpm;
03133 
03134        /* When printing arrays, it's possible to have cases where the
03135           same CV-qualifier gets pushed on the stack multiple times.
03136           We only need to print it once.  */
03137 
03138        for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
03139          {
03140            if (! pdpm->printed)
03141              {
03142               if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
03143                   && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
03144                   && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
03145                 break;
03146               if (pdpm->mod->type == dc->type)
03147                 {
03148                   d_print_comp (dpi, d_left (dc));
03149                   return;
03150                 }
03151              }
03152          }
03153       }
03154       /* Fall through.  */
03155     case DEMANGLE_COMPONENT_RESTRICT_THIS:
03156     case DEMANGLE_COMPONENT_VOLATILE_THIS:
03157     case DEMANGLE_COMPONENT_CONST_THIS:
03158     case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
03159     case DEMANGLE_COMPONENT_POINTER:
03160     case DEMANGLE_COMPONENT_REFERENCE:
03161     case DEMANGLE_COMPONENT_COMPLEX:
03162     case DEMANGLE_COMPONENT_IMAGINARY:
03163       {
03164        /* We keep a list of modifiers on the stack.  */
03165        struct d_print_mod dpm;
03166 
03167        dpm.next = dpi->modifiers;
03168        dpi->modifiers = &dpm;
03169        dpm.mod = dc;
03170        dpm.printed = 0;
03171        dpm.templates = dpi->templates;
03172 
03173        d_print_comp (dpi, d_left (dc));
03174 
03175        /* If the modifier didn't get printed by the type, print it
03176           now.  */
03177        if (! dpm.printed)
03178          d_print_mod (dpi, dc);
03179 
03180        dpi->modifiers = dpm.next;
03181 
03182        return;
03183       }
03184 
03185     case DEMANGLE_COMPONENT_BUILTIN_TYPE:
03186       if ((dpi->options & DMGL_JAVA) == 0)
03187        d_append_buffer (dpi, dc->u.s_builtin.type->name,
03188                       dc->u.s_builtin.type->len);
03189       else
03190        d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
03191                       dc->u.s_builtin.type->java_len);
03192       return;
03193 
03194     case DEMANGLE_COMPONENT_VENDOR_TYPE:
03195       d_print_comp (dpi, d_left (dc));
03196       return;
03197 
03198     case DEMANGLE_COMPONENT_FUNCTION_TYPE:
03199       {
03200        if ((dpi->options & DMGL_RET_POSTFIX) != 0)
03201          d_print_function_type (dpi, dc, dpi->modifiers);
03202 
03203        /* Print return type if present */
03204        if (d_left (dc) != NULL)
03205          {
03206            struct d_print_mod dpm;
03207 
03208            /* We must pass this type down as a modifier in order to
03209               print it in the right location.  */
03210            dpm.next = dpi->modifiers;
03211            dpi->modifiers = &dpm;
03212            dpm.mod = dc;
03213            dpm.printed = 0;
03214            dpm.templates = dpi->templates;
03215 
03216            d_print_comp (dpi, d_left (dc));
03217 
03218            dpi->modifiers = dpm.next;
03219 
03220            if (dpm.printed)
03221              return;
03222 
03223            /* In standard prefix notation, there is a space between the
03224               return type and the function signature.  */
03225            if ((dpi->options & DMGL_RET_POSTFIX) == 0)
03226              d_append_char (dpi, ' ');
03227          }
03228 
03229        if ((dpi->options & DMGL_RET_POSTFIX) == 0) 
03230          d_print_function_type (dpi, dc, dpi->modifiers);
03231 
03232        return;
03233       }
03234 
03235     case DEMANGLE_COMPONENT_ARRAY_TYPE:
03236       {
03237        struct d_print_mod *hold_modifiers;
03238        struct d_print_mod adpm[4];
03239        unsigned int i;
03240        struct d_print_mod *pdpm;
03241 
03242        /* We must pass this type down as a modifier in order to print
03243           multi-dimensional arrays correctly.  If the array itself is
03244           CV-qualified, we act as though the element type were
03245           CV-qualified.  We do this by copying the modifiers down
03246           rather than fiddling pointers, so that we don't wind up
03247           with a d_print_mod higher on the stack pointing into our
03248           stack frame after we return.  */
03249 
03250        hold_modifiers = dpi->modifiers;
03251 
03252        adpm[0].next = hold_modifiers;
03253        dpi->modifiers = &adpm[0];
03254        adpm[0].mod = dc;
03255        adpm[0].printed = 0;
03256        adpm[0].templates = dpi->templates;
03257 
03258        i = 1;
03259        pdpm = hold_modifiers;
03260        while (pdpm != NULL
03261               && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
03262                  || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
03263                  || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
03264          {
03265            if (! pdpm->printed)
03266              {
03267               if (i >= sizeof adpm / sizeof adpm[0])
03268                 {
03269                   d_print_error (dpi);
03270                   return;
03271                 }
03272 
03273               adpm[i] = *pdpm;
03274               adpm[i].next = dpi->modifiers;
03275               dpi->modifiers = &adpm[i];
03276               pdpm->printed = 1;
03277               ++i;
03278              }
03279 
03280            pdpm = pdpm->next;
03281          }
03282 
03283        d_print_comp (dpi, d_right (dc));
03284 
03285        dpi->modifiers = hold_modifiers;
03286 
03287        if (adpm[0].printed)
03288          return;
03289 
03290        while (i > 1)
03291          {
03292            --i;
03293            d_print_mod (dpi, adpm[i].mod);
03294          }
03295 
03296        d_print_array_type (dpi, dc, dpi->modifiers);
03297 
03298        return;
03299       }
03300 
03301     case DEMANGLE_COMPONENT_PTRMEM_TYPE:
03302       {
03303        struct d_print_mod dpm;
03304 
03305        dpm.next = dpi->modifiers;
03306        dpi->modifiers = &dpm;
03307        dpm.mod = dc;
03308        dpm.printed = 0;
03309        dpm.templates = dpi->templates;
03310 
03311        d_print_comp (dpi, d_right (dc));
03312 
03313        /* If the modifier didn't get printed by the type, print it
03314           now.  */
03315        if (! dpm.printed)
03316          {
03317            d_append_char (dpi, ' ');
03318            d_print_comp (dpi, d_left (dc));
03319            d_append_string (dpi, "::*");
03320          }
03321 
03322        dpi->modifiers = dpm.next;
03323 
03324        return;
03325       }
03326 
03327     case DEMANGLE_COMPONENT_ARGLIST:
03328     case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
03329       d_print_comp (dpi, d_left (dc));
03330       if (d_right (dc) != NULL)
03331        {
03332          d_append_string (dpi, ", ");
03333          d_print_comp (dpi, d_right (dc));
03334        }
03335       return;
03336 
03337     case DEMANGLE_COMPONENT_OPERATOR:
03338       {
03339        char c;
03340 
03341        d_append_string (dpi, "operator");
03342        c = dc->u.s_operator.op->name[0];
03343        if (IS_LOWER (c))
03344          d_append_char (dpi, ' ');
03345        d_append_buffer (dpi, dc->u.s_operator.op->name,
03346                       dc->u.s_operator.op->len);
03347        return;
03348       }
03349 
03350     case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
03351       d_append_string (dpi, "operator ");
03352       d_print_comp (dpi, dc->u.s_extended_operator.name);
03353       return;
03354 
03355     case DEMANGLE_COMPONENT_CAST:
03356       d_append_string (dpi, "operator ");
03357       d_print_cast (dpi, dc);
03358       return;
03359 
03360     case DEMANGLE_COMPONENT_UNARY:
03361       if (d_left (dc)->type != DEMANGLE_COMPONENT_CAST)
03362        d_print_expr_op (dpi, d_left (dc));
03363       else
03364        {
03365          d_append_char (dpi, '(');
03366          d_print_cast (dpi, d_left (dc));
03367          d_append_char (dpi, ')');
03368        }
03369       d_append_char (dpi, '(');
03370       d_print_comp (dpi, d_right (dc));
03371       d_append_char (dpi, ')');
03372       return;
03373 
03374     case DEMANGLE_COMPONENT_BINARY:
03375       if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
03376        {
03377          d_print_error (dpi);
03378          return;
03379        }
03380 
03381       /* We wrap an expression which uses the greater-than operator in
03382         an extra layer of parens so that it does not get confused
03383         with the '>' which ends the template parameters.  */
03384       if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
03385          && d_left (dc)->u.s_operator.op->len == 1
03386          && d_left (dc)->u.s_operator.op->name[0] == '>')
03387        d_append_char (dpi, '(');
03388 
03389       d_append_char (dpi, '(');
03390       d_print_comp (dpi, d_left (d_right (dc)));
03391       d_append_string (dpi, ") ");
03392       d_print_expr_op (dpi, d_left (dc));
03393       d_append_string (dpi, " (");
03394       d_print_comp (dpi, d_right (d_right (dc)));
03395       d_append_char (dpi, ')');
03396 
03397       if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
03398          && d_left (dc)->u.s_operator.op->len == 1
03399          && d_left (dc)->u.s_operator.op->name[0] == '>')
03400        d_append_char (dpi, ')');
03401 
03402       return;
03403 
03404     case DEMANGLE_COMPONENT_BINARY_ARGS:
03405       /* We should only see this as part of DEMANGLE_COMPONENT_BINARY.  */
03406       d_print_error (dpi);
03407       return;
03408 
03409     case DEMANGLE_COMPONENT_TRINARY:
03410       if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
03411          || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
03412        {
03413          d_print_error (dpi);
03414          return;
03415        }
03416       d_append_char (dpi, '(');
03417       d_print_comp (dpi, d_left (d_right (dc)));
03418       d_append_string (dpi, ") ");
03419       d_print_expr_op (dpi, d_left (dc));
03420       d_append_string (dpi, " (");
03421       d_print_comp (dpi, d_left (d_right (d_right (dc))));
03422       d_append_string (dpi, ") : (");
03423       d_print_comp (dpi, d_right (d_right (d_right (dc))));
03424       d_append_char (dpi, ')');
03425       return;
03426 
03427     case DEMANGLE_COMPONENT_TRINARY_ARG1:
03428     case DEMANGLE_COMPONENT_TRINARY_ARG2:
03429       /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY.  */
03430       d_print_error (dpi);
03431       return;
03432 
03433     case DEMANGLE_COMPONENT_LITERAL:
03434     case DEMANGLE_COMPONENT_LITERAL_NEG:
03435       {
03436        enum d_builtin_type_print tp;
03437 
03438        /* For some builtin types, produce simpler output.  */
03439        tp = D_PRINT_DEFAULT;
03440        if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
03441          {
03442            tp = d_left (dc)->u.s_builtin.type->print;
03443            switch (tp)
03444              {
03445              case D_PRINT_INT:
03446              case D_PRINT_UNSIGNED:
03447              case D_PRINT_LONG:
03448              case D_PRINT_UNSIGNED_LONG:
03449              case D_PRINT_LONG_LONG:
03450              case D_PRINT_UNSIGNED_LONG_LONG:
03451               if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
03452                 {
03453                   if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
03454                     d_append_char (dpi, '-');
03455                   d_print_comp (dpi, d_right (dc));
03456                   switch (tp)
03457                     {
03458                     default:
03459                      break;
03460                     case D_PRINT_UNSIGNED:
03461                      d_append_char (dpi, 'u');
03462                      break;
03463                     case D_PRINT_LONG:
03464                      d_append_char (dpi, 'l');
03465                      break;
03466                     case D_PRINT_UNSIGNED_LONG:
03467                      d_append_string (dpi, "ul");
03468                      break;
03469                     case D_PRINT_LONG_LONG:
03470                      d_append_string (dpi, "ll");
03471                      break;
03472                     case D_PRINT_UNSIGNED_LONG_LONG:
03473                      d_append_string (dpi, "ull");
03474                      break;
03475                     }
03476                   return;
03477                 }
03478               break;
03479 
03480              case D_PRINT_BOOL:
03481               if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
03482                   && d_right (dc)->u.s_name.len == 1
03483                   && dc->type == DEMANGLE_COMPONENT_LITERAL)
03484                 {
03485                   switch (d_right (dc)->u.s_name.s[0])
03486                     {
03487                     case '0':
03488                      d_append_string (dpi, "false");
03489                      return;
03490                     case '1':
03491                      d_append_string (dpi, "true");
03492                      return;
03493                     default:
03494                      break;
03495                     }
03496                 }
03497               break;
03498 
03499              default:
03500               break;
03501              }
03502          }
03503 
03504        d_append_char (dpi, '(');
03505        d_print_comp (dpi, d_left (dc));
03506        d_append_char (dpi, ')');
03507        if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
03508          d_append_char (dpi, '-');
03509        if (tp == D_PRINT_FLOAT)
03510          d_append_char (dpi, '[');
03511        d_print_comp (dpi, d_right (dc));
03512        if (tp == D_PRINT_FLOAT)
03513          d_append_char (dpi, ']');
03514       }
03515       return;
03516 
03517     default:
03518       d_print_error (dpi);
03519       return;
03520     }
03521 }
03522 
03523 /* Print a Java dentifier.  For Java we try to handle encoded extended
03524    Unicode characters.  The C++ ABI doesn't mention Unicode encoding,
03525    so we don't it for C++.  Characters are encoded as
03526    __U<hex-char>+_.  */
03527 
03528 static void
03529 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
03530 {
03531   const char *p;
03532   const char *end;
03533 
03534   end = name + len;
03535   for (p = name; p < end; ++p)
03536     {
03537       if (end - p > 3
03538          && p[0] == '_'
03539          && p[1] == '_'
03540          && p[2] == 'U')
03541        {
03542          unsigned long c;
03543          const char *q;
03544 
03545          c = 0;
03546          for (q = p + 3; q < end; ++q)
03547            {
03548              int dig;
03549 
03550              if (IS_DIGIT (*q))
03551               dig = *q - '0';
03552              else if (*q >= 'A' && *q <= 'F')
03553               dig = *q - 'A' + 10;
03554              else if (*q >= 'a' && *q <= 'f')
03555               dig = *q - 'a' + 10;
03556              else
03557               break;
03558 
03559              c = c * 16 + dig;
03560            }
03561          /* If the Unicode character is larger than 256, we don't try
03562             to deal with it here.  FIXME.  */
03563          if (q < end && *q == '_' && c < 256)
03564            {
03565              d_append_char (dpi, c);
03566              p = q;
03567              continue;
03568            }
03569        }
03570 
03571       d_append_char (dpi, *p);
03572     }
03573 }
03574 
03575 /* Print a list of modifiers.  SUFFIX is 1 if we are printing
03576    qualifiers on this after printing a function.  */
03577 
03578 static void
03579 d_print_mod_list (struct d_print_info *dpi,
03580                   struct d_print_mod *mods, int suffix)
03581 {
03582   struct d_print_template *hold_dpt;
03583 
03584   if (mods == NULL || d_print_saw_error (dpi))
03585     return;
03586 
03587   if (mods->printed
03588       || (! suffix
03589          && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
03590              || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
03591              || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS)))
03592     {
03593       d_print_mod_list (dpi, mods->next, suffix);
03594       return;
03595     }
03596 
03597   mods->printed = 1;
03598 
03599   hold_dpt = dpi->templates;
03600   dpi->templates = mods->templates;
03601 
03602   if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
03603     {
03604       d_print_function_type (dpi, mods->mod, mods->next);
03605       dpi->templates = hold_dpt;
03606       return;
03607     }
03608   else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
03609     {
03610       d_print_array_type (dpi, mods->mod, mods->next);
03611       dpi->templates = hold_dpt;
03612       return;
03613     }
03614   else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
03615     {
03616       struct d_print_mod *hold_modifiers;
03617       struct demangle_component *dc;
03618 
03619       /* When this is on the modifier stack, we have pulled any
03620         qualifiers off the right argument already.  Otherwise, we
03621         print it as usual, but don't let the left argument see any
03622         modifiers.  */
03623 
03624       hold_modifiers = dpi->modifiers;
03625       dpi->modifiers = NULL;
03626       d_print_comp (dpi, d_left (mods->mod));
03627       dpi->modifiers = hold_modifiers;
03628 
03629       if ((dpi->options & DMGL_JAVA) == 0)
03630        d_append_string (dpi, "::");
03631       else
03632        d_append_char (dpi, '.');
03633 
03634       dc = d_right (mods->mod);
03635       while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
03636             || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
03637             || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
03638        dc = d_left (dc);
03639 
03640       d_print_comp (dpi, dc);
03641 
03642       dpi->templates = hold_dpt;
03643       return;
03644     }
03645 
03646   d_print_mod (dpi, mods->mod);
03647 
03648   dpi->templates = hold_dpt;
03649 
03650   d_print_mod_list (dpi, mods->next, suffix);
03651 }
03652 
03653 /* Print a modifier.  */
03654 
03655 static void
03656 d_print_mod (struct d_print_info *dpi,
03657              const struct demangle_component *mod)
03658 {
03659   switch (mod->type)
03660     {
03661     case DEMANGLE_COMPONENT_RESTRICT:
03662     case DEMANGLE_COMPONENT_RESTRICT_THIS:
03663       d_append_string (dpi, " restrict");
03664       return;
03665     case DEMANGLE_COMPONENT_VOLATILE:
03666     case DEMANGLE_COMPONENT_VOLATILE_THIS:
03667       d_append_string (dpi, " volatile");
03668       return;
03669     case DEMANGLE_COMPONENT_CONST:
03670     case DEMANGLE_COMPONENT_CONST_THIS:
03671       d_append_string (dpi, " const");
03672       return;
03673     case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
03674       d_append_char (dpi, ' ');
03675       d_print_comp (dpi, d_right (mod));
03676       return;
03677     case DEMANGLE_COMPONENT_POINTER:
03678       /* There is no pointer symbol in Java.  */
03679       if ((dpi->options & DMGL_JAVA) == 0)
03680        d_append_char (dpi, '*');
03681       return;
03682     case DEMANGLE_COMPONENT_REFERENCE:
03683       d_append_char (dpi, '&');
03684       return;
03685     case DEMANGLE_COMPONENT_COMPLEX:
03686       d_append_string (dpi, "complex ");
03687       return;
03688     case DEMANGLE_COMPONENT_IMAGINARY:
03689       d_append_string (dpi, "imaginary ");
03690       return;
03691     case DEMANGLE_COMPONENT_PTRMEM_TYPE:
03692       if (d_last_char (dpi) != '(')
03693        d_append_char (dpi, ' ');
03694       d_print_comp (dpi, d_left (mod));
03695       d_append_string (dpi, "::*");
03696       return;
03697     case DEMANGLE_COMPONENT_TYPED_NAME:
03698       d_print_comp (dpi, d_left (mod));
03699       return;
03700     default:
03701       /* Otherwise, we have something that won't go back on the
03702         modifier stack, so we can just print it.  */
03703       d_print_comp (dpi, mod);
03704       return;
03705     }
03706 }
03707 
03708 /* Print a function type, except for the return type.  */
03709 
03710 static void
03711 d_print_function_type (struct d_print_info *dpi,
03712                        const struct demangle_component *dc,
03713                        struct d_print_mod *mods)
03714 {
03715   int need_paren;
03716   int saw_mod;
03717   int need_space;
03718   struct d_print_mod *p;
03719   struct d_print_mod *hold_modifiers;
03720 
03721   need_paren = 0;
03722   saw_mod = 0;
03723   need_space = 0;
03724   for (p = mods; p != NULL; p = p->next)
03725     {
03726       if (p->printed)
03727        break;
03728 
03729       saw_mod = 1;
03730       switch (p->mod->type)
03731        {
03732        case DEMANGLE_COMPONENT_POINTER:
03733        case DEMANGLE_COMPONENT_REFERENCE:
03734          need_paren = 1;
03735          break;
03736        case DEMANGLE_COMPONENT_RESTRICT:
03737        case DEMANGLE_COMPONENT_VOLATILE:
03738        case DEMANGLE_COMPONENT_CONST:
03739        case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
03740        case DEMANGLE_COMPONENT_COMPLEX:
03741        case DEMANGLE_COMPONENT_IMAGINARY:
03742        case DEMANGLE_COMPONENT_PTRMEM_TYPE:
03743          need_space = 1;
03744          need_paren = 1;
03745          break;
03746        case DEMANGLE_COMPONENT_RESTRICT_THIS:
03747        case DEMANGLE_COMPONENT_VOLATILE_THIS:
03748        case DEMANGLE_COMPONENT_CONST_THIS:
03749          break;
03750        default:
03751          break;
03752        }
03753       if (need_paren)
03754        break;
03755     }
03756 
03757   if (d_left (dc) != NULL && ! saw_mod)
03758     need_paren = 1;
03759 
03760   if (need_paren)
03761     {
03762       if (! need_space)
03763        {
03764          if (d_last_char (dpi) != '('
03765              && d_last_char (dpi) != '*')
03766            need_space = 1;
03767        }
03768       if (need_space && d_last_char (dpi) != ' ')
03769        d_append_char (dpi, ' ');
03770       d_append_char (dpi, '(');
03771     }
03772 
03773   hold_modifiers = dpi->modifiers;
03774   dpi->modifiers = NULL;
03775 
03776   d_print_mod_list (dpi, mods, 0);
03777 
03778   if (need_paren)
03779     d_append_char (dpi, ')');
03780 
03781   d_append_char (dpi, '(');
03782 
03783   if (d_right (dc) != NULL)
03784     d_print_comp (dpi, d_right (dc));
03785 
03786   d_append_char (dpi, ')');
03787 
03788   d_print_mod_list (dpi, mods, 1);
03789 
03790   dpi->modifiers = hold_modifiers;
03791 }
03792 
03793 /* Print an array type, except for the element type.  */
03794 
03795 static void
03796 d_print_array_type (struct d_print_info *dpi,
03797                     const struct demangle_component *dc,
03798                     struct d_print_mod *mods)
03799 {
03800   int need_space;
03801 
03802   need_space = 1;
03803   if (mods != NULL)
03804     {
03805       int need_paren;
03806       struct d_print_mod *p;
03807 
03808       need_paren = 0;
03809       for (p = mods; p != NULL; p = p->next)
03810        {
03811          if (! p->printed)
03812            {
03813              if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
03814               {
03815                 need_space = 0;
03816                 break;
03817               }
03818              else
03819               {
03820                 need_paren = 1;
03821                 need_space = 1;
03822                 break;
03823               }
03824            }
03825        }
03826 
03827       if (need_paren)
03828        d_append_string (dpi, " (");
03829 
03830       d_print_mod_list (dpi, mods, 0);
03831 
03832       if (need_paren)
03833        d_append_char (dpi, ')');
03834     }
03835 
03836   if (need_space)
03837     d_append_char (dpi, ' ');
03838 
03839   d_append_char (dpi, '[');
03840 
03841   if (d_left (dc) != NULL)
03842     d_print_comp (dpi, d_left (dc));
03843 
03844   d_append_char (dpi, ']');
03845 }
03846 
03847 /* Print an operator in an expression.  */
03848 
03849 static void
03850 d_print_expr_op (struct d_print_info *dpi,
03851                  const struct demangle_component *dc)
03852 {
03853   if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
03854     d_append_buffer (dpi, dc->u.s_operator.op->name,
03855                    dc->u.s_operator.op->len);
03856   else
03857     d_print_comp (dpi, dc);
03858 }
03859 
03860 /* Print a cast.  */
03861 
03862 static void
03863 d_print_cast (struct d_print_info *dpi,
03864               const struct demangle_component *dc)
03865 {
03866   if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
03867     d_print_comp (dpi, d_left (dc));
03868   else
03869     {
03870       struct d_print_mod *hold_dpm;
03871       struct d_print_template dpt;
03872 
03873       /* It appears that for a templated cast operator, we need to put
03874         the template parameters in scope for the operator name, but
03875         not for the parameters.  The effect is that we need to handle
03876         the template printing here.  */
03877 
03878       hold_dpm = dpi->modifiers;
03879       dpi->modifiers = NULL;
03880 
03881       dpt.next = dpi->templates;
03882       dpi->templates = &dpt;
03883       dpt.template_decl = d_left (dc);
03884 
03885       d_print_comp (dpi, d_left (d_left (dc)));
03886 
03887       dpi->templates = dpt.next;
03888 
03889       if (d_last_char (dpi) == '<')
03890        d_append_char (dpi, ' ');
03891       d_append_char (dpi, '<');
03892       d_print_comp (dpi, d_right (d_left (dc)));
03893       /* Avoid generating two consecutive '>' characters, to avoid
03894         the C++ syntactic ambiguity.  */
03895       if (d_last_char (dpi) == '>')
03896        d_append_char (dpi, ' ');
03897       d_append_char (dpi, '>');
03898 
03899       dpi->modifiers = hold_dpm;
03900     }
03901 }
03902 
03903 /* Initialize the information structure we use to pass around
03904    information.  */
03905 
03906 CP_STATIC_IF_GLIBCPP_V3
03907 void
03908 cplus_demangle_init_info (const char *mangled, int options, size_t len,
03909                           struct d_info *di)
03910 {
03911   di->s = mangled;
03912   di->send = mangled + len;
03913   di->options = options;
03914 
03915   di->n = mangled;
03916 
03917   /* We can not need more components than twice the number of chars in
03918      the mangled string.  Most components correspond directly to
03919      chars, but the ARGLIST types are exceptions.  */
03920   di->num_comps = 2 * len;
03921   di->next_comp = 0;
03922 
03923   /* Similarly, we can not need more substitutions than there are
03924      chars in the mangled string.  */
03925   di->num_subs = len;
03926   di->next_sub = 0;
03927   di->did_subs = 0;
03928 
03929   di->last_name = NULL;
03930 
03931   di->expansion = 0;
03932 }
03933 
03934 /* Internal implementation for the demangler.  If MANGLED is a g++ v3 ABI
03935    mangled name, return strings in repeated callback giving the demangled
03936    name.  OPTIONS is the usual libiberty demangler options.  On success,
03937    this returns 1.  On failure, returns 0.  */
03938 
03939 static int
03940 d_demangle_callback (const char *mangled, int options,
03941                      demangle_callbackref callback, void *opaque)
03942 {
03943   int type;
03944   struct d_info di;
03945   struct demangle_component *dc;
03946   int status;
03947 
03948   if (mangled[0] == '_' && mangled[1] == 'Z')
03949     type = 0;
03950   else if (strncmp (mangled, "_GLOBAL_", 8) == 0
03951           && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
03952           && (mangled[9] == 'D' || mangled[9] == 'I')
03953           && mangled[10] == '_')
03954     {
03955       const char *intro;
03956 
03957       intro = (mangled[9] == 'I')
03958               ? "global constructors keyed to "
03959               : "global destructors keyed to ";
03960 
03961       callback (intro, strlen (intro), opaque);
03962       callback (mangled + 11, strlen (mangled + 11), opaque);
03963       return 1;
03964     }
03965   else
03966     {
03967       if ((options & DMGL_TYPES) == 0)
03968        return 0;
03969       type = 1;
03970     }
03971 
03972   cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
03973 
03974   {
03975 #ifdef CP_DYNAMIC_ARRAYS
03976     __extension__ struct demangle_component comps[di.num_comps];
03977     __extension__ struct demangle_component *subs[di.num_subs];
03978 
03979     di.comps = comps;
03980     di.subs = subs;
03981 #else
03982     di.comps = alloca (di.num_comps * sizeof (*di.comps));
03983     di.subs = alloca (di.num_subs * sizeof (*di.subs));
03984 #endif
03985 
03986     if (type)
03987       dc = cplus_demangle_type (&di);
03988     else
03989       dc = cplus_demangle_mangled_name (&di, 1);
03990 
03991     /* If DMGL_PARAMS is set, then if we didn't consume the entire
03992        mangled string, then we didn't successfully demangle it.  If
03993        DMGL_PARAMS is not set, we didn't look at the trailing
03994        parameters.  */
03995     if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
03996       dc = NULL;
03997 
03998 #ifdef CP_DEMANGLE_DEBUG
03999     d_dump (dc, 0);
04000 #endif
04001 
04002     status = (dc != NULL)
04003              ? cplus_demangle_print_callback (options, dc, callback, opaque)
04004              : 0;
04005   }
04006 
04007   return status;
04008 }
04009 
04010 /* Entry point for the demangler.  If MANGLED is a g++ v3 ABI mangled
04011    name, return a buffer allocated with malloc holding the demangled
04012    name.  OPTIONS is the usual libiberty demangler options.  On
04013    success, this sets *PALC to the allocated size of the returned
04014    buffer.  On failure, this sets *PALC to 0 for a bad name, or 1 for
04015    a memory allocation failure, and returns NULL.  */
04016 
04017 static char *
04018 d_demangle (const char *mangled, int options, size_t *palc)
04019 {
04020   struct d_growable_string dgs;
04021   int status;
04022 
04023   d_growable_string_init (&dgs, 0);
04024 
04025   status = d_demangle_callback (mangled, options,
04026                                 d_growable_string_callback_adapter, &dgs);
04027   if (status == 0)
04028     {
04029       free (dgs.buf);
04030       *palc = 0;
04031       return NULL;
04032     }
04033 
04034   *palc = dgs.allocation_failure ? 1 : 0;
04035   return dgs.buf;
04036 }
04037 
04038 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
04039 
04040 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
04041 
04042 /* ia64 ABI-mandated entry point in the C++ runtime library for
04043    performing demangling.  MANGLED_NAME is a NUL-terminated character
04044    string containing the name to be demangled.
04045 
04046    OUTPUT_BUFFER is a region of memory, allocated with malloc, of
04047    *LENGTH bytes, into which the demangled name is stored.  If
04048    OUTPUT_BUFFER is not long enough, it is expanded using realloc.
04049    OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
04050    is placed in a region of memory allocated with malloc.
04051 
04052    If LENGTH is non-NULL, the length of the buffer containing the
04053    demangled name, is placed in *LENGTH.
04054 
04055    The return value is a pointer to the start of the NUL-terminated
04056    demangled name, or NULL if the demangling fails.  The caller is
04057    responsible for deallocating this memory using free.
04058 
04059    *STATUS is set to one of the following values:
04060       0: The demangling operation succeeded.
04061      -1: A memory allocation failure occurred.
04062      -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
04063      -3: One of the arguments is invalid.
04064 
04065    The demangling is performed using the C++ ABI mangling rules, with
04066    GNU extensions.  */
04067 
04068 char *
04069 __cxa_demangle (const char *mangled_name, char *output_buffer,
04070                 size_t *length, int *status)
04071 {
04072   char *demangled;
04073   size_t alc;
04074 
04075   if (mangled_name == NULL)
04076     {
04077       if (status != NULL)
04078        *status = -3;
04079       return NULL;
04080     }
04081 
04082   if (output_buffer != NULL && length == NULL)
04083     {
04084       if (status != NULL)
04085        *status = -3;
04086       return NULL;
04087     }
04088 
04089   demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
04090 
04091   if (demangled == NULL)
04092     {
04093       if (status != NULL)
04094        {
04095          if (alc == 1)
04096            *status = -1;
04097          else
04098            *status = -2;
04099        }
04100       return NULL;
04101     }
04102 
04103   if (output_buffer == NULL)
04104     {
04105       if (length != NULL)
04106        *length = alc;
04107     }
04108   else
04109     {
04110       if (strlen (demangled) < *length)
04111        {
04112          strcpy (output_buffer, demangled);
04113          free (demangled);
04114          demangled = output_buffer;
04115        }
04116       else
04117        {
04118          free (output_buffer);
04119          *length = alc;
04120        }
04121     }
04122 
04123   if (status != NULL)
04124     *status = 0;
04125 
04126   return demangled;
04127 }
04128 
04129 extern int __gcclibcxx_demangle_callback (const char *,
04130                                           void (*)
04131                                             (const char *, size_t, void *),
04132                                           void *);
04133 
04134 /* Alternative, allocationless entry point in the C++ runtime library
04135    for performing demangling.  MANGLED_NAME is a NUL-terminated character
04136    string containing the name to be demangled.
04137 
04138    CALLBACK is a callback function, called with demangled string
04139    segments as demangling progresses; it is called at least once,
04140    but may be called more than once.  OPAQUE is a generalized pointer
04141    used as a callback argument.
04142 
04143    The return code is one of the following values, equivalent to
04144    the STATUS values of __cxa_demangle() (excluding -1, since this
04145    function performs no memory allocations):
04146       0: The demangling operation succeeded.
04147      -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
04148      -3: One of the arguments is invalid.
04149 
04150    The demangling is performed using the C++ ABI mangling rules, with
04151    GNU extensions.  */
04152 
04153 int
04154 __gcclibcxx_demangle_callback (const char *mangled_name,
04155                                void (*callback) (const char *, size_t, void *),
04156                                void *opaque)
04157 {
04158   int status;
04159 
04160   if (mangled_name == NULL || callback == NULL)
04161     return -3;
04162 
04163   status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES,
04164                                 callback, opaque);
04165   if (status == 0)
04166     return -2;
04167 
04168   return 0;
04169 }
04170 
04171 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
04172 
04173 /* Entry point for libiberty demangler.  If MANGLED is a g++ v3 ABI
04174    mangled name, return a buffer allocated with malloc holding the
04175    demangled name.  Otherwise, return NULL.  */
04176 
04177 char *
04178 cplus_demangle_v3 (const char *mangled, int options)
04179 {
04180   size_t alc;
04181 
04182   return d_demangle (mangled, options, &alc);
04183 }
04184 
04185 int
04186 cplus_demangle_v3_callback (const char *mangled, int options,
04187                             demangle_callbackref callback, void *opaque)
04188 {
04189   return d_demangle_callback (mangled, options, callback, opaque);
04190 }
04191 
04192 /* Demangle a Java symbol.  Java uses a subset of the V3 ABI C++ mangling 
04193    conventions, but the output formatting is a little different.
04194    This instructs the C++ demangler not to emit pointer characters ("*"), to
04195    use Java's namespace separator symbol ("." instead of "::"), and to output
04196    JArray<TYPE> as TYPE[].  */
04197 
04198 char *
04199 java_demangle_v3 (const char *mangled)
04200 {
04201   size_t alc;
04202 
04203   return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
04204 }
04205 
04206 int
04207 java_demangle_v3_callback (const char *mangled,
04208                            demangle_callbackref callback, void *opaque)
04209 {
04210   return d_demangle_callback (mangled,
04211                               DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
04212                               callback, opaque);
04213 }
04214 
04215 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
04216 
04217 #ifndef IN_GLIBCPP_V3
04218 
04219 /* Demangle a string in order to find out whether it is a constructor
04220    or destructor.  Return non-zero on success.  Set *CTOR_KIND and
04221    *DTOR_KIND appropriately.  */
04222 
04223 static int
04224 is_ctor_or_dtor (const char *mangled,
04225                  enum gnu_v3_ctor_kinds *ctor_kind,
04226                  enum gnu_v3_dtor_kinds *dtor_kind)
04227 {
04228   struct d_info di;
04229   struct demangle_component *dc;
04230   int ret;
04231 
04232   *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
04233   *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
04234 
04235   cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
04236 
04237   {
04238 #ifdef CP_DYNAMIC_ARRAYS
04239     __extension__ struct demangle_component comps[di.num_comps];
04240     __extension__ struct demangle_component *subs[di.num_subs];
04241 
04242     di.comps = comps;
04243     di.subs = subs;
04244 #else
04245     di.comps = alloca (di.num_comps * sizeof (*di.comps));
04246     di.subs = alloca (di.num_subs * sizeof (*di.subs));
04247 #endif
04248 
04249     dc = cplus_demangle_mangled_name (&di, 1);
04250 
04251     /* Note that because we did not pass DMGL_PARAMS, we don't expect
04252        to demangle the entire string.  */
04253 
04254     ret = 0;
04255     while (dc != NULL)
04256       {
04257        switch (dc->type)
04258          {
04259          default:
04260            dc = NULL;
04261            break;
04262          case DEMANGLE_COMPONENT_TYPED_NAME:
04263          case DEMANGLE_COMPONENT_TEMPLATE:
04264          case DEMANGLE_COMPONENT_RESTRICT_THIS:
04265          case DEMANGLE_COMPONENT_VOLATILE_THIS:
04266          case DEMANGLE_COMPONENT_CONST_THIS:
04267            dc = d_left (dc);
04268            break;
04269          case DEMANGLE_COMPONENT_QUAL_NAME:
04270          case DEMANGLE_COMPONENT_LOCAL_NAME:
04271            dc = d_right (dc);
04272            break;
04273          case DEMANGLE_COMPONENT_CTOR:
04274            *ctor_kind = dc->u.s_ctor.kind;
04275            ret = 1;
04276            dc = NULL;
04277            break;
04278          case DEMANGLE_COMPONENT_DTOR:
04279            *dtor_kind = dc->u.s_dtor.kind;
04280            ret = 1;
04281            dc = NULL;
04282            break;
04283          }
04284       }
04285   }
04286 
04287   return ret;
04288 }
04289 
04290 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
04291    name.  A non-zero return indicates the type of constructor.  */
04292 
04293 enum gnu_v3_ctor_kinds
04294 is_gnu_v3_mangled_ctor (const char *name)
04295 {
04296   enum gnu_v3_ctor_kinds ctor_kind;
04297   enum gnu_v3_dtor_kinds dtor_kind;
04298 
04299   if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
04300     return (enum gnu_v3_ctor_kinds) 0;
04301   return ctor_kind;
04302 }
04303 
04304 
04305 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
04306    name.  A non-zero return indicates the type of destructor.  */
04307 
04308 enum gnu_v3_dtor_kinds
04309 is_gnu_v3_mangled_dtor (const char *name)
04310 {
04311   enum gnu_v3_ctor_kinds ctor_kind;
04312   enum gnu_v3_dtor_kinds dtor_kind;
04313 
04314   if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
04315     return (enum gnu_v3_dtor_kinds) 0;
04316   return dtor_kind;
04317 }
04318 
04319 #endif /* IN_GLIBCPP_V3 */
04320 
04321 #ifdef STANDALONE_DEMANGLER
04322 
04323 #include "getopt.h"
04324 #include "dyn-string.h"
04325 
04326 static void print_usage (FILE* fp, int exit_value);
04327 
04328 #define IS_ALPHA(CHAR)                                                  \
04329   (((CHAR) >= 'a' && (CHAR) <= 'z')                                     \
04330    || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
04331 
04332 /* Non-zero if CHAR is a character than can occur in a mangled name.  */
04333 #define is_mangled_char(CHAR)                                           \
04334   (IS_ALPHA (CHAR) || IS_DIGIT (CHAR)                                   \
04335    || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
04336 
04337 /* The name of this program, as invoked.  */
04338 const char* program_name;
04339 
04340 /* Prints usage summary to FP and then exits with EXIT_VALUE.  */
04341 
04342 static void
04343 print_usage (FILE* fp, int exit_value)
04344 {
04345   fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
04346   fprintf (fp, "Options:\n");
04347   fprintf (fp, "  -h,--help       Display this message.\n");
04348   fprintf (fp, "  -p,--no-params  Don't display function parameters\n");
04349   fprintf (fp, "  -v,--verbose    Produce verbose demanglings.\n");
04350   fprintf (fp, "If names are provided, they are demangled.  Otherwise filters standard input.\n");
04351 
04352   exit (exit_value);
04353 }
04354 
04355 /* Option specification for getopt_long.  */
04356 static const struct option long_options[] = 
04357 {
04358   { "help",    no_argument, NULL, 'h' },
04359   { "no-params", no_argument, NULL, 'p' },
04360   { "verbose",   no_argument, NULL, 'v' },
04361   { NULL,        no_argument, NULL, 0   },
04362 };
04363 
04364 /* Main entry for a demangling filter executable.  It will demangle
04365    its command line arguments, if any.  If none are provided, it will
04366    filter stdin to stdout, replacing any recognized mangled C++ names
04367    with their demangled equivalents.  */
04368 
04369 int
04370 main (int argc, char *argv[])
04371 {
04372   int i;
04373   int opt_char;
04374   int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
04375 
04376   /* Use the program name of this program, as invoked.  */
04377   program_name = argv[0];
04378 
04379   /* Parse options.  */
04380   do 
04381     {
04382       opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
04383       switch (opt_char)
04384        {
04385        case '?':  /* Unrecognized option.  */
04386          print_usage (stderr, 1);
04387          break;
04388 
04389        case 'h':
04390          print_usage (stdout, 0);
04391          break;
04392 
04393        case 'p':
04394          options &= ~ DMGL_PARAMS;
04395          break;
04396 
04397        case 'v':
04398          options |= DMGL_VERBOSE;
04399          break;
04400        }
04401     }
04402   while (opt_char != -1);
04403 
04404   if (optind == argc) 
04405     /* No command line arguments were provided.  Filter stdin.  */
04406     {
04407       dyn_string_t mangled = dyn_string_new (3);
04408       char *s;
04409 
04410       /* Read all of input.  */
04411       while (!feof (stdin))
04412        {
04413          char c;
04414 
04415          /* Pile characters into mangled until we hit one that can't
04416             occur in a mangled name.  */
04417          c = getchar ();
04418          while (!feof (stdin) && is_mangled_char (c))
04419            {
04420              dyn_string_append_char (mangled, c);
04421              if (feof (stdin))
04422               break;
04423              c = getchar ();
04424            }
04425 
04426          if (dyn_string_length (mangled) > 0)
04427            {
04428 #ifdef IN_GLIBCPP_V3
04429              s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
04430 #else
04431              s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
04432 #endif
04433 
04434              if (s != NULL)
04435               {
04436                 fputs (s, stdout);
04437                 free (s);
04438               }
04439              else
04440               {
04441                 /* It might not have been a mangled name.  Print the
04442                    original text.  */
04443                 fputs (dyn_string_buf (mangled), stdout);
04444               }
04445 
04446              dyn_string_clear (mangled);
04447            }
04448 
04449          /* If we haven't hit EOF yet, we've read one character that
04450             can't occur in a mangled name, so print it out.  */
04451          if (!feof (stdin))
04452            putchar (c);
04453        }
04454 
04455       dyn_string_delete (mangled);
04456     }
04457   else
04458     /* Demangle command line arguments.  */
04459     {
04460       /* Loop over command line arguments.  */
04461       for (i = optind; i < argc; ++i)
04462        {
04463          char *s;
04464 #ifdef IN_GLIBCPP_V3
04465          int status;
04466 #endif
04467 
04468          /* Attempt to demangle.  */
04469 #ifdef IN_GLIBCPP_V3
04470          s = __cxa_demangle (argv[i], NULL, NULL, &status);
04471 #else
04472          s = cplus_demangle_v3 (argv[i], options);
04473 #endif
04474 
04475          /* If it worked, print the demangled name.  */
04476          if (s != NULL)
04477            {
04478              printf ("%s\n", s);
04479              free (s);
04480            }
04481          else
04482            {
04483 #ifdef IN_GLIBCPP_V3
04484              fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
04485 #else
04486              fprintf (stderr, "Failed: %s\n", argv[i]);
04487 #endif
04488            }
04489        }
04490     }
04491 
04492   return 0;
04493 }
04494 
04495 #endif /* STANDALONE_DEMANGLER */