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