1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2014
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor <ian@wasabisystems.com>.
6 This file is part of the libiberty library, which is part of GCC.
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.
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
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.
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.
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
36 This code was written while looking at the demangler written by
37 Alex Samuel <samuel@codesourcery.com>.
39 This code first pulls the mangled name apart into a list of
40 components, and then walks the list generating the demangled
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)
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
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
65 cplus_demangle_print_callback
66 and other functions defined in the file cp-demint.c.
68 This file also defines some other functions and variables which are
69 only to be used by the file cp-demint.c.
71 Preprocessor macros you can define while compiling this file:
74 If defined, this file defines the following functions, q.v.:
75 char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
77 int __gcclibcxx_demangle_callback (const char *,
79 (const char *, size_t, void *),
81 instead of cplus_demangle_v3[_callback]() and
82 java_demangle_v3[_callback]().
85 If defined, this file defines only __cxa_demangle() and
86 __gcclibcxx_demangle_callback(), and no other publically visible
87 functions or variables.
90 If defined, this file defines a main() function which demangles
91 any arguments, or, if none, demangles stdin.
94 If defined, turns on debugging mode, which prints information on
95 stdout about the mangled string. This is not generally useful.
98 #if defined (_AIX) && !defined (__GNUC__)
120 # define alloca __builtin_alloca
122 extern char *alloca ();
123 # endif /* __GNUC__ */
125 #endif /* HAVE_ALLOCA_H */
131 # define INT_MAX (int)(((unsigned int) ~0) >> 1) /* 0x7FFFFFFF */
134 #include "ansidecl.h"
135 #include "libiberty.h"
136 #include "demangle.h"
137 #include "cp-demangle.h"
139 /* If IN_GLIBCPP_V3 is defined, some functions are made static. We
140 also rename them via #define to avoid compiler errors when the
141 static definition conflicts with the extern declaration in a header
145 #define CP_STATIC_IF_GLIBCPP_V3 static
147 #define cplus_demangle_fill_name d_fill_name
148 static int d_fill_name (struct demangle_component *, const char *, int);
150 #define cplus_demangle_fill_extended_operator d_fill_extended_operator
152 d_fill_extended_operator (struct demangle_component *, int,
153 struct demangle_component *);
155 #define cplus_demangle_fill_ctor d_fill_ctor
157 d_fill_ctor (struct demangle_component *, enum gnu_v3_ctor_kinds,
158 struct demangle_component *);
160 #define cplus_demangle_fill_dtor d_fill_dtor
162 d_fill_dtor (struct demangle_component *, enum gnu_v3_dtor_kinds,
163 struct demangle_component *);
165 #define cplus_demangle_mangled_name d_mangled_name
166 static struct demangle_component *d_mangled_name (struct d_info *, int);
168 #define cplus_demangle_type d_type
169 static struct demangle_component *d_type (struct d_info *);
171 #define cplus_demangle_print d_print
172 static char *d_print (int, const struct demangle_component *, int, size_t *);
174 #define cplus_demangle_print_callback d_print_callback
175 static int d_print_callback (int, const struct demangle_component *,
176 demangle_callbackref, void *);
178 #define cplus_demangle_init_info d_init_info
179 static void d_init_info (const char *, int, size_t, struct d_info *);
181 #else /* ! defined(IN_GLIBCPP_V3) */
182 #define CP_STATIC_IF_GLIBCPP_V3
183 #endif /* ! defined(IN_GLIBCPP_V3) */
185 /* See if the compiler supports dynamic arrays. */
188 #define CP_DYNAMIC_ARRAYS
191 #ifdef __STDC_VERSION__
192 #if __STDC_VERSION__ >= 199901L
193 #define CP_DYNAMIC_ARRAYS
194 #endif /* __STDC__VERSION >= 199901L */
195 #endif /* defined (__STDC_VERSION__) */
196 #endif /* defined (__STDC__) */
197 #endif /* ! defined (__GNUC__) */
199 /* We avoid pulling in the ctype tables, to prevent pulling in
200 additional unresolved symbols when this code is used in a library.
201 FIXME: Is this really a valid reason? This comes from the original
204 As of this writing this file has the following undefined references
205 when compiled with -DIN_GLIBCPP_V3: realloc, free, memcpy, strcpy,
208 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
209 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
210 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
212 /* The prefix prepended by GCC to an identifier represnting the
213 anonymous namespace. */
214 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
215 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
216 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
218 /* Information we keep for the standard substitutions. */
220 struct d_standard_sub_info
222 /* The code for this substitution. */
224 /* The simple string it expands to. */
225 const char *simple_expansion;
226 /* The length of the simple expansion. */
228 /* The results of a full, verbose, expansion. This is used when
229 qualifying a constructor/destructor, or when in verbose mode. */
230 const char *full_expansion;
231 /* The length of the full expansion. */
233 /* What to set the last_name field of d_info to; NULL if we should
234 not set it. This is only relevant when qualifying a
235 constructor/destructor. */
236 const char *set_last_name;
237 /* The length of set_last_name. */
238 int set_last_name_len;
241 /* Accessors for subtrees of struct demangle_component. */
243 #define d_left(dc) ((dc)->u.s_binary.left)
244 #define d_right(dc) ((dc)->u.s_binary.right)
246 /* A list of templates. This is used while printing. */
248 struct d_print_template
250 /* Next template on the list. */
251 struct d_print_template *next;
253 const struct demangle_component *template_decl;
256 /* A list of type modifiers. This is used while printing. */
260 /* Next modifier on the list. These are in the reverse of the order
261 in which they appeared in the mangled string. */
262 struct d_print_mod *next;
264 const struct demangle_component *mod;
265 /* Whether this modifier was printed. */
267 /* The list of templates which applies to this modifier. */
268 struct d_print_template *templates;
271 /* We use these structures to hold information during printing. */
273 struct d_growable_string
275 /* Buffer holding the result. */
277 /* Current length of data in buffer. */
279 /* Allocated size of buffer. */
281 /* Set to 1 if we had a memory allocation failure. */
282 int allocation_failure;
285 /* Stack of components, innermost first, used to avoid loops. */
287 struct d_component_stack
289 /* This component. */
290 const struct demangle_component *dc;
291 /* This component's parent. */
292 const struct d_component_stack *parent;
295 /* A demangle component and some scope captured when it was first
300 /* The component whose scope this is. */
301 const struct demangle_component *container;
302 /* The list of templates, if any, that was current when this
303 scope was captured. */
304 struct d_print_template *templates;
307 /* Checkpoint structure to allow backtracking. This holds copies
308 of the fields of struct d_info that need to be restored
309 if a trial parse needs to be backtracked over. */
311 struct d_info_checkpoint
320 enum { D_PRINT_BUFFER_LENGTH = 256 };
323 /* Fixed-length allocated buffer for demangled data, flushed to the
324 callback with a NUL termination once full. */
325 char buf[D_PRINT_BUFFER_LENGTH];
326 /* Current length of data in buffer. */
328 /* The last character printed, saved individually so that it survives
331 /* Callback function to handle demangled buffer flush. */
332 demangle_callbackref callback;
333 /* Opaque callback argument. */
335 /* The current list of templates, if any. */
336 struct d_print_template *templates;
337 /* The current list of modifiers (e.g., pointer, reference, etc.),
339 struct d_print_mod *modifiers;
340 /* Set to 1 if we saw a demangling error. */
341 int demangle_failure;
342 /* The current index into any template argument packs we are using
345 /* Number of d_print_flush calls so far. */
346 unsigned long int flush_count;
347 /* Stack of components, innermost first, used to avoid loops. */
348 const struct d_component_stack *component_stack;
349 /* Array of saved scopes for evaluating substitutions. */
350 struct d_saved_scope *saved_scopes;
351 /* Index of the next unused saved scope in the above array. */
352 int next_saved_scope;
353 /* Number of saved scopes in the above array. */
354 int num_saved_scopes;
355 /* Array of templates for saving into scopes. */
356 struct d_print_template *copy_templates;
357 /* Index of the next unused copy template in the above array. */
358 int next_copy_template;
359 /* Number of copy templates in the above array. */
360 int num_copy_templates;
361 /* The nearest enclosing template, if any. */
362 const struct demangle_component *current_template;
365 #ifdef CP_DEMANGLE_DEBUG
366 static void d_dump (struct demangle_component *, int);
369 static struct demangle_component *
370 d_make_empty (struct d_info *);
372 static struct demangle_component *
373 d_make_comp (struct d_info *, enum demangle_component_type,
374 struct demangle_component *,
375 struct demangle_component *);
377 static struct demangle_component *
378 d_make_name (struct d_info *, const char *, int);
380 static struct demangle_component *
381 d_make_demangle_mangled_name (struct d_info *, const char *);
383 static struct demangle_component *
384 d_make_builtin_type (struct d_info *,
385 const struct demangle_builtin_type_info *);
387 static struct demangle_component *
388 d_make_operator (struct d_info *,
389 const struct demangle_operator_info *);
391 static struct demangle_component *
392 d_make_extended_operator (struct d_info *, int,
393 struct demangle_component *);
395 static struct demangle_component *
396 d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
397 struct demangle_component *);
399 static struct demangle_component *
400 d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
401 struct demangle_component *);
403 static struct demangle_component *
404 d_make_template_param (struct d_info *, int);
406 static struct demangle_component *
407 d_make_sub (struct d_info *, const char *, int);
410 has_return_type (struct demangle_component *);
413 is_ctor_dtor_or_conversion (struct demangle_component *);
415 static struct demangle_component *d_encoding (struct d_info *, int);
417 static struct demangle_component *d_name (struct d_info *);
419 static struct demangle_component *d_nested_name (struct d_info *);
421 static struct demangle_component *d_prefix (struct d_info *);
423 static struct demangle_component *d_unqualified_name (struct d_info *);
425 static struct demangle_component *d_source_name (struct d_info *);
427 static int d_number (struct d_info *);
429 static struct demangle_component *d_identifier (struct d_info *, int);
431 static struct demangle_component *d_operator_name (struct d_info *);
433 static struct demangle_component *d_special_name (struct d_info *);
435 static int d_call_offset (struct d_info *, int);
437 static struct demangle_component *d_ctor_dtor_name (struct d_info *);
439 static struct demangle_component **
440 d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
442 static struct demangle_component *
443 d_ref_qualifier (struct d_info *, struct demangle_component *);
445 static struct demangle_component *
446 d_function_type (struct d_info *);
448 static struct demangle_component *
449 d_bare_function_type (struct d_info *, int);
451 static struct demangle_component *
452 d_class_enum_type (struct d_info *);
454 static struct demangle_component *d_array_type (struct d_info *);
456 static struct demangle_component *d_vector_type (struct d_info *);
458 static struct demangle_component *
459 d_pointer_to_member_type (struct d_info *);
461 static struct demangle_component *
462 d_template_param (struct d_info *);
464 static struct demangle_component *d_template_args (struct d_info *);
466 static struct demangle_component *
467 d_template_arg (struct d_info *);
469 static struct demangle_component *d_expression (struct d_info *);
471 static struct demangle_component *d_expr_primary (struct d_info *);
473 static struct demangle_component *d_local_name (struct d_info *);
475 static int d_discriminator (struct d_info *);
477 static struct demangle_component *d_lambda (struct d_info *);
479 static struct demangle_component *d_unnamed_type (struct d_info *);
481 static struct demangle_component *
482 d_clone_suffix (struct d_info *, struct demangle_component *);
485 d_add_substitution (struct d_info *, struct demangle_component *);
487 static struct demangle_component *d_substitution (struct d_info *, int);
489 static void d_checkpoint (struct d_info *, struct d_info_checkpoint *);
491 static void d_backtrack (struct d_info *, struct d_info_checkpoint *);
493 static void d_growable_string_init (struct d_growable_string *, size_t);
496 d_growable_string_resize (struct d_growable_string *, size_t);
499 d_growable_string_append_buffer (struct d_growable_string *,
500 const char *, size_t);
502 d_growable_string_callback_adapter (const char *, size_t, void *);
505 d_print_init (struct d_print_info *, demangle_callbackref, void *,
506 const struct demangle_component *);
508 static inline void d_print_error (struct d_print_info *);
510 static inline int d_print_saw_error (struct d_print_info *);
512 static inline void d_print_flush (struct d_print_info *);
514 static inline void d_append_char (struct d_print_info *, char);
516 static inline void d_append_buffer (struct d_print_info *,
517 const char *, size_t);
519 static inline void d_append_string (struct d_print_info *, const char *);
521 static inline char d_last_char (struct d_print_info *);
524 d_print_comp (struct d_print_info *, int, const struct demangle_component *);
527 d_print_java_identifier (struct d_print_info *, const char *, int);
530 d_print_mod_list (struct d_print_info *, int, struct d_print_mod *, int);
533 d_print_mod (struct d_print_info *, int, const struct demangle_component *);
536 d_print_function_type (struct d_print_info *, int,
537 const struct demangle_component *,
538 struct d_print_mod *);
541 d_print_array_type (struct d_print_info *, int,
542 const struct demangle_component *,
543 struct d_print_mod *);
546 d_print_expr_op (struct d_print_info *, int, const struct demangle_component *);
548 static void d_print_cast (struct d_print_info *, int,
549 const struct demangle_component *);
550 static void d_print_conversion (struct d_print_info *, int,
551 const struct demangle_component *);
553 static int d_demangle_callback (const char *, int,
554 demangle_callbackref, void *);
555 static char *d_demangle (const char *, int, size_t *);
557 #ifdef CP_DEMANGLE_DEBUG
560 d_dump (struct demangle_component *dc, int indent)
567 printf ("failed demangling\n");
571 for (i = 0; i < indent; ++i)
576 case DEMANGLE_COMPONENT_NAME:
577 printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
579 case DEMANGLE_COMPONENT_TAGGED_NAME:
580 printf ("tagged name\n");
581 d_dump (dc->u.s_binary.left, indent + 2);
582 d_dump (dc->u.s_binary.right, indent + 2);
584 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
585 printf ("template parameter %ld\n", dc->u.s_number.number);
587 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
588 printf ("function parameter %ld\n", dc->u.s_number.number);
590 case DEMANGLE_COMPONENT_CTOR:
591 printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
592 d_dump (dc->u.s_ctor.name, indent + 2);
594 case DEMANGLE_COMPONENT_DTOR:
595 printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
596 d_dump (dc->u.s_dtor.name, indent + 2);
598 case DEMANGLE_COMPONENT_SUB_STD:
599 printf ("standard substitution %s\n", dc->u.s_string.string);
601 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
602 printf ("builtin type %s\n", dc->u.s_builtin.type->name);
604 case DEMANGLE_COMPONENT_OPERATOR:
605 printf ("operator %s\n", dc->u.s_operator.op->name);
607 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
608 printf ("extended operator with %d args\n",
609 dc->u.s_extended_operator.args);
610 d_dump (dc->u.s_extended_operator.name, indent + 2);
613 case DEMANGLE_COMPONENT_QUAL_NAME:
614 printf ("qualified name\n");
616 case DEMANGLE_COMPONENT_LOCAL_NAME:
617 printf ("local name\n");
619 case DEMANGLE_COMPONENT_TYPED_NAME:
620 printf ("typed name\n");
622 case DEMANGLE_COMPONENT_TEMPLATE:
623 printf ("template\n");
625 case DEMANGLE_COMPONENT_VTABLE:
628 case DEMANGLE_COMPONENT_VTT:
631 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
632 printf ("construction vtable\n");
634 case DEMANGLE_COMPONENT_TYPEINFO:
635 printf ("typeinfo\n");
637 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
638 printf ("typeinfo name\n");
640 case DEMANGLE_COMPONENT_TYPEINFO_FN:
641 printf ("typeinfo function\n");
643 case DEMANGLE_COMPONENT_THUNK:
646 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
647 printf ("virtual thunk\n");
649 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
650 printf ("covariant thunk\n");
652 case DEMANGLE_COMPONENT_JAVA_CLASS:
653 printf ("java class\n");
655 case DEMANGLE_COMPONENT_GUARD:
658 case DEMANGLE_COMPONENT_REFTEMP:
659 printf ("reference temporary\n");
661 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
662 printf ("hidden alias\n");
664 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
665 printf ("transaction clone\n");
667 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
668 printf ("non-transaction clone\n");
670 case DEMANGLE_COMPONENT_RESTRICT:
671 printf ("restrict\n");
673 case DEMANGLE_COMPONENT_VOLATILE:
674 printf ("volatile\n");
676 case DEMANGLE_COMPONENT_CONST:
679 case DEMANGLE_COMPONENT_RESTRICT_THIS:
680 printf ("restrict this\n");
682 case DEMANGLE_COMPONENT_VOLATILE_THIS:
683 printf ("volatile this\n");
685 case DEMANGLE_COMPONENT_CONST_THIS:
686 printf ("const this\n");
688 case DEMANGLE_COMPONENT_REFERENCE_THIS:
689 printf ("reference this\n");
691 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
692 printf ("rvalue reference this\n");
694 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
695 printf ("vendor type qualifier\n");
697 case DEMANGLE_COMPONENT_POINTER:
698 printf ("pointer\n");
700 case DEMANGLE_COMPONENT_REFERENCE:
701 printf ("reference\n");
703 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
704 printf ("rvalue reference\n");
706 case DEMANGLE_COMPONENT_COMPLEX:
707 printf ("complex\n");
709 case DEMANGLE_COMPONENT_IMAGINARY:
710 printf ("imaginary\n");
712 case DEMANGLE_COMPONENT_VENDOR_TYPE:
713 printf ("vendor type\n");
715 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
716 printf ("function type\n");
718 case DEMANGLE_COMPONENT_ARRAY_TYPE:
719 printf ("array type\n");
721 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
722 printf ("pointer to member type\n");
724 case DEMANGLE_COMPONENT_FIXED_TYPE:
725 printf ("fixed-point type, accum? %d, sat? %d\n",
726 dc->u.s_fixed.accum, dc->u.s_fixed.sat);
727 d_dump (dc->u.s_fixed.length, indent + 2);
729 case DEMANGLE_COMPONENT_ARGLIST:
730 printf ("argument list\n");
732 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
733 printf ("template argument list\n");
735 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
736 printf ("initializer list\n");
738 case DEMANGLE_COMPONENT_CAST:
741 case DEMANGLE_COMPONENT_CONVERSION:
742 printf ("conversion operator\n");
744 case DEMANGLE_COMPONENT_NULLARY:
745 printf ("nullary operator\n");
747 case DEMANGLE_COMPONENT_UNARY:
748 printf ("unary operator\n");
750 case DEMANGLE_COMPONENT_BINARY:
751 printf ("binary operator\n");
753 case DEMANGLE_COMPONENT_BINARY_ARGS:
754 printf ("binary operator arguments\n");
756 case DEMANGLE_COMPONENT_TRINARY:
757 printf ("trinary operator\n");
759 case DEMANGLE_COMPONENT_TRINARY_ARG1:
760 printf ("trinary operator arguments 1\n");
762 case DEMANGLE_COMPONENT_TRINARY_ARG2:
763 printf ("trinary operator arguments 1\n");
765 case DEMANGLE_COMPONENT_LITERAL:
766 printf ("literal\n");
768 case DEMANGLE_COMPONENT_LITERAL_NEG:
769 printf ("negative literal\n");
771 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
772 printf ("java resource\n");
774 case DEMANGLE_COMPONENT_COMPOUND_NAME:
775 printf ("compound name\n");
777 case DEMANGLE_COMPONENT_CHARACTER:
778 printf ("character '%c'\n", dc->u.s_character.character);
780 case DEMANGLE_COMPONENT_NUMBER:
781 printf ("number %ld\n", dc->u.s_number.number);
783 case DEMANGLE_COMPONENT_DECLTYPE:
784 printf ("decltype\n");
786 case DEMANGLE_COMPONENT_PACK_EXPANSION:
787 printf ("pack expansion\n");
789 case DEMANGLE_COMPONENT_TLS_INIT:
790 printf ("tls init function\n");
792 case DEMANGLE_COMPONENT_TLS_WRAPPER:
793 printf ("tls wrapper function\n");
795 case DEMANGLE_COMPONENT_DEFAULT_ARG:
796 printf ("default argument %d\n", dc->u.s_unary_num.num);
797 d_dump (dc->u.s_unary_num.sub, indent+2);
799 case DEMANGLE_COMPONENT_LAMBDA:
800 printf ("lambda %d\n", dc->u.s_unary_num.num);
801 d_dump (dc->u.s_unary_num.sub, indent+2);
805 d_dump (d_left (dc), indent + 2);
806 d_dump (d_right (dc), indent + 2);
809 #endif /* CP_DEMANGLE_DEBUG */
811 /* Fill in a DEMANGLE_COMPONENT_NAME. */
813 CP_STATIC_IF_GLIBCPP_V3
815 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
817 if (p == NULL || s == NULL || len == 0)
819 p->type = DEMANGLE_COMPONENT_NAME;
821 p->u.s_name.len = len;
825 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
827 CP_STATIC_IF_GLIBCPP_V3
829 cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
830 struct demangle_component *name)
832 if (p == NULL || args < 0 || name == NULL)
834 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
835 p->u.s_extended_operator.args = args;
836 p->u.s_extended_operator.name = name;
840 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
842 CP_STATIC_IF_GLIBCPP_V3
844 cplus_demangle_fill_ctor (struct demangle_component *p,
845 enum gnu_v3_ctor_kinds kind,
846 struct demangle_component *name)
850 || (int) kind < gnu_v3_complete_object_ctor
851 || (int) kind > gnu_v3_object_ctor_group)
853 p->type = DEMANGLE_COMPONENT_CTOR;
854 p->u.s_ctor.kind = kind;
855 p->u.s_ctor.name = name;
859 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
861 CP_STATIC_IF_GLIBCPP_V3
863 cplus_demangle_fill_dtor (struct demangle_component *p,
864 enum gnu_v3_dtor_kinds kind,
865 struct demangle_component *name)
869 || (int) kind < gnu_v3_deleting_dtor
870 || (int) kind > gnu_v3_object_dtor_group)
872 p->type = DEMANGLE_COMPONENT_DTOR;
873 p->u.s_dtor.kind = kind;
874 p->u.s_dtor.name = name;
878 /* Add a new component. */
880 static struct demangle_component *
881 d_make_empty (struct d_info *di)
883 struct demangle_component *p;
885 if (di->next_comp >= di->num_comps)
887 p = &di->comps[di->next_comp];
892 /* Add a new generic component. */
894 static struct demangle_component *
895 d_make_comp (struct d_info *di, enum demangle_component_type type,
896 struct demangle_component *left,
897 struct demangle_component *right)
899 struct demangle_component *p;
901 /* We check for errors here. A typical error would be a NULL return
902 from a subroutine. We catch those here, and return NULL
906 /* These types require two parameters. */
907 case DEMANGLE_COMPONENT_QUAL_NAME:
908 case DEMANGLE_COMPONENT_LOCAL_NAME:
909 case DEMANGLE_COMPONENT_TYPED_NAME:
910 case DEMANGLE_COMPONENT_TAGGED_NAME:
911 case DEMANGLE_COMPONENT_TEMPLATE:
912 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
913 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
914 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
915 case DEMANGLE_COMPONENT_UNARY:
916 case DEMANGLE_COMPONENT_BINARY:
917 case DEMANGLE_COMPONENT_BINARY_ARGS:
918 case DEMANGLE_COMPONENT_TRINARY:
919 case DEMANGLE_COMPONENT_TRINARY_ARG1:
920 case DEMANGLE_COMPONENT_LITERAL:
921 case DEMANGLE_COMPONENT_LITERAL_NEG:
922 case DEMANGLE_COMPONENT_COMPOUND_NAME:
923 case DEMANGLE_COMPONENT_VECTOR_TYPE:
924 case DEMANGLE_COMPONENT_CLONE:
925 if (left == NULL || right == NULL)
929 /* These types only require one parameter. */
930 case DEMANGLE_COMPONENT_VTABLE:
931 case DEMANGLE_COMPONENT_VTT:
932 case DEMANGLE_COMPONENT_TYPEINFO:
933 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
934 case DEMANGLE_COMPONENT_TYPEINFO_FN:
935 case DEMANGLE_COMPONENT_THUNK:
936 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
937 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
938 case DEMANGLE_COMPONENT_JAVA_CLASS:
939 case DEMANGLE_COMPONENT_GUARD:
940 case DEMANGLE_COMPONENT_TLS_INIT:
941 case DEMANGLE_COMPONENT_TLS_WRAPPER:
942 case DEMANGLE_COMPONENT_REFTEMP:
943 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
944 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
945 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
946 case DEMANGLE_COMPONENT_POINTER:
947 case DEMANGLE_COMPONENT_REFERENCE:
948 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
949 case DEMANGLE_COMPONENT_COMPLEX:
950 case DEMANGLE_COMPONENT_IMAGINARY:
951 case DEMANGLE_COMPONENT_VENDOR_TYPE:
952 case DEMANGLE_COMPONENT_CAST:
953 case DEMANGLE_COMPONENT_CONVERSION:
954 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
955 case DEMANGLE_COMPONENT_DECLTYPE:
956 case DEMANGLE_COMPONENT_PACK_EXPANSION:
957 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
958 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
959 case DEMANGLE_COMPONENT_NULLARY:
960 case DEMANGLE_COMPONENT_TRINARY_ARG2:
965 /* This needs a right parameter, but the left parameter can be
967 case DEMANGLE_COMPONENT_ARRAY_TYPE:
968 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
973 /* These are allowed to have no parameters--in some cases they
974 will be filled in later. */
975 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
976 case DEMANGLE_COMPONENT_RESTRICT:
977 case DEMANGLE_COMPONENT_VOLATILE:
978 case DEMANGLE_COMPONENT_CONST:
979 case DEMANGLE_COMPONENT_RESTRICT_THIS:
980 case DEMANGLE_COMPONENT_VOLATILE_THIS:
981 case DEMANGLE_COMPONENT_CONST_THIS:
982 case DEMANGLE_COMPONENT_REFERENCE_THIS:
983 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
984 case DEMANGLE_COMPONENT_ARGLIST:
985 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
988 /* Other types should not be seen here. */
993 p = d_make_empty (di);
997 p->u.s_binary.left = left;
998 p->u.s_binary.right = right;
1003 /* Add a new demangle mangled name component. */
1005 static struct demangle_component *
1006 d_make_demangle_mangled_name (struct d_info *di, const char *s)
1008 if (d_peek_char (di) != '_' || d_peek_next_char (di) != 'Z')
1009 return d_make_name (di, s, strlen (s));
1011 return d_encoding (di, 0);
1014 /* Add a new name component. */
1016 static struct demangle_component *
1017 d_make_name (struct d_info *di, const char *s, int len)
1019 struct demangle_component *p;
1021 p = d_make_empty (di);
1022 if (! cplus_demangle_fill_name (p, s, len))
1027 /* Add a new builtin type component. */
1029 static struct demangle_component *
1030 d_make_builtin_type (struct d_info *di,
1031 const struct demangle_builtin_type_info *type)
1033 struct demangle_component *p;
1037 p = d_make_empty (di);
1040 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
1041 p->u.s_builtin.type = type;
1046 /* Add a new operator component. */
1048 static struct demangle_component *
1049 d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
1051 struct demangle_component *p;
1053 p = d_make_empty (di);
1056 p->type = DEMANGLE_COMPONENT_OPERATOR;
1057 p->u.s_operator.op = op;
1062 /* Add a new extended operator component. */
1064 static struct demangle_component *
1065 d_make_extended_operator (struct d_info *di, int args,
1066 struct demangle_component *name)
1068 struct demangle_component *p;
1070 p = d_make_empty (di);
1071 if (! cplus_demangle_fill_extended_operator (p, args, name))
1076 static struct demangle_component *
1077 d_make_default_arg (struct d_info *di, int num,
1078 struct demangle_component *sub)
1080 struct demangle_component *p = d_make_empty (di);
1083 p->type = DEMANGLE_COMPONENT_DEFAULT_ARG;
1084 p->u.s_unary_num.num = num;
1085 p->u.s_unary_num.sub = sub;
1090 /* Add a new constructor component. */
1092 static struct demangle_component *
1093 d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
1094 struct demangle_component *name)
1096 struct demangle_component *p;
1098 p = d_make_empty (di);
1099 if (! cplus_demangle_fill_ctor (p, kind, name))
1104 /* Add a new destructor component. */
1106 static struct demangle_component *
1107 d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
1108 struct demangle_component *name)
1110 struct demangle_component *p;
1112 p = d_make_empty (di);
1113 if (! cplus_demangle_fill_dtor (p, kind, name))
1118 /* Add a new template parameter. */
1120 static struct demangle_component *
1121 d_make_template_param (struct d_info *di, int i)
1123 struct demangle_component *p;
1125 p = d_make_empty (di);
1128 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
1129 p->u.s_number.number = i;
1134 /* Add a new function parameter. */
1136 static struct demangle_component *
1137 d_make_function_param (struct d_info *di, int i)
1139 struct demangle_component *p;
1141 p = d_make_empty (di);
1144 p->type = DEMANGLE_COMPONENT_FUNCTION_PARAM;
1145 p->u.s_number.number = i;
1150 /* Add a new standard substitution component. */
1152 static struct demangle_component *
1153 d_make_sub (struct d_info *di, const char *name, int len)
1155 struct demangle_component *p;
1157 p = d_make_empty (di);
1160 p->type = DEMANGLE_COMPONENT_SUB_STD;
1161 p->u.s_string.string = name;
1162 p->u.s_string.len = len;
1167 /* <mangled-name> ::= _Z <encoding> [<clone-suffix>]*
1169 TOP_LEVEL is non-zero when called at the top level. */
1171 CP_STATIC_IF_GLIBCPP_V3
1172 struct demangle_component *
1173 cplus_demangle_mangled_name (struct d_info *di, int top_level)
1175 struct demangle_component *p;
1177 if (! d_check_char (di, '_')
1178 /* Allow missing _ if not at toplevel to work around a
1179 bug in G++ abi-version=2 mangling; see the comment in
1180 write_template_arg. */
1183 if (! d_check_char (di, 'Z'))
1185 p = d_encoding (di, top_level);
1187 /* If at top level and parsing parameters, check for a clone
1189 if (top_level && (di->options & DMGL_PARAMS) != 0)
1190 while (d_peek_char (di) == '.'
1191 && (IS_LOWER (d_peek_next_char (di))
1192 || d_peek_next_char (di) == '_'
1193 || IS_DIGIT (d_peek_next_char (di))))
1194 p = d_clone_suffix (di, p);
1199 /* Return whether a function should have a return type. The argument
1200 is the function name, which may be qualified in various ways. The
1201 rules are that template functions have return types with some
1202 exceptions, function types which are not part of a function name
1203 mangling have return types with some exceptions, and non-template
1204 function names do not have return types. The exceptions are that
1205 constructors, destructors, and conversion operators do not have
1209 has_return_type (struct demangle_component *dc)
1217 case DEMANGLE_COMPONENT_TEMPLATE:
1218 return ! is_ctor_dtor_or_conversion (d_left (dc));
1219 case DEMANGLE_COMPONENT_RESTRICT_THIS:
1220 case DEMANGLE_COMPONENT_VOLATILE_THIS:
1221 case DEMANGLE_COMPONENT_CONST_THIS:
1222 case DEMANGLE_COMPONENT_REFERENCE_THIS:
1223 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
1224 return has_return_type (d_left (dc));
1228 /* Return whether a name is a constructor, a destructor, or a
1229 conversion operator. */
1232 is_ctor_dtor_or_conversion (struct demangle_component *dc)
1240 case DEMANGLE_COMPONENT_QUAL_NAME:
1241 case DEMANGLE_COMPONENT_LOCAL_NAME:
1242 return is_ctor_dtor_or_conversion (d_right (dc));
1243 case DEMANGLE_COMPONENT_CTOR:
1244 case DEMANGLE_COMPONENT_DTOR:
1245 case DEMANGLE_COMPONENT_CONVERSION:
1250 /* <encoding> ::= <(function) name> <bare-function-type>
1254 TOP_LEVEL is non-zero when called at the top level, in which case
1255 if DMGL_PARAMS is not set we do not demangle the function
1256 parameters. We only set this at the top level, because otherwise
1257 we would not correctly demangle names in local scopes. */
1259 static struct demangle_component *
1260 d_encoding (struct d_info *di, int top_level)
1262 char peek = d_peek_char (di);
1264 if (peek == 'G' || peek == 'T')
1265 return d_special_name (di);
1268 struct demangle_component *dc;
1272 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
1274 /* Strip off any initial CV-qualifiers, as they really apply
1275 to the `this' parameter, and they were not output by the
1276 v2 demangler without DMGL_PARAMS. */
1277 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1278 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1279 || dc->type == DEMANGLE_COMPONENT_CONST_THIS
1280 || dc->type == DEMANGLE_COMPONENT_REFERENCE_THIS
1281 || dc->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS)
1284 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1285 there may be CV-qualifiers on its right argument which
1286 really apply here; this happens when parsing a class
1287 which is local to a function. */
1288 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1290 struct demangle_component *dcr;
1293 while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1294 || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1295 || dcr->type == DEMANGLE_COMPONENT_CONST_THIS
1296 || dcr->type == DEMANGLE_COMPONENT_REFERENCE_THIS
1297 || dcr->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS)
1299 dc->u.s_binary.right = dcr;
1305 peek = d_peek_char (di);
1306 if (dc == NULL || peek == '\0' || peek == 'E')
1308 return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
1309 d_bare_function_type (di, has_return_type (dc)));
1313 /* <tagged-name> ::= <name> B <source-name> */
1315 static struct demangle_component *
1316 d_abi_tags (struct d_info *di, struct demangle_component *dc)
1319 while (peek = d_peek_char (di),
1322 struct demangle_component *tag;
1324 tag = d_source_name (di);
1325 dc = d_make_comp (di, DEMANGLE_COMPONENT_TAGGED_NAME, dc, tag);
1330 /* <name> ::= <nested-name>
1332 ::= <unscoped-template-name> <template-args>
1335 <unscoped-name> ::= <unqualified-name>
1336 ::= St <unqualified-name>
1338 <unscoped-template-name> ::= <unscoped-name>
1342 static struct demangle_component *
1343 d_name (struct d_info *di)
1345 char peek = d_peek_char (di);
1346 struct demangle_component *dc;
1351 return d_nested_name (di);
1354 return d_local_name (di);
1357 return d_unqualified_name (di);
1363 if (d_peek_next_char (di) != 't')
1365 dc = d_substitution (di, 0);
1371 dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
1372 d_make_name (di, "std", 3),
1373 d_unqualified_name (di));
1378 if (d_peek_char (di) != 'I')
1380 /* The grammar does not permit this case to occur if we
1381 called d_substitution() above (i.e., subst == 1). We
1382 don't bother to check. */
1386 /* This is <template-args>, which means that we just saw
1387 <unscoped-template-name>, which is a substitution
1388 candidate if we didn't just get it from a
1392 if (! d_add_substitution (di, dc))
1395 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1396 d_template_args (di));
1404 dc = d_unqualified_name (di);
1405 if (d_peek_char (di) == 'I')
1407 /* This is <template-args>, which means that we just saw
1408 <unscoped-template-name>, which is a substitution
1410 if (! d_add_substitution (di, dc))
1412 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1413 d_template_args (di));
1419 /* <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1420 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
1423 static struct demangle_component *
1424 d_nested_name (struct d_info *di)
1426 struct demangle_component *ret;
1427 struct demangle_component **pret;
1428 struct demangle_component *rqual;
1430 if (! d_check_char (di, 'N'))
1433 pret = d_cv_qualifiers (di, &ret, 1);
1437 /* Parse the ref-qualifier now and then attach it
1438 once we have something to attach it to. */
1439 rqual = d_ref_qualifier (di, NULL);
1441 *pret = d_prefix (di);
1447 d_left (rqual) = ret;
1451 if (! d_check_char (di, 'E'))
1457 /* <prefix> ::= <prefix> <unqualified-name>
1458 ::= <template-prefix> <template-args>
1459 ::= <template-param>
1464 <template-prefix> ::= <prefix> <(template) unqualified-name>
1465 ::= <template-param>
1469 static struct demangle_component *
1470 d_prefix (struct d_info *di)
1472 struct demangle_component *ret = NULL;
1477 enum demangle_component_type comb_type;
1478 struct demangle_component *dc;
1480 peek = d_peek_char (di);
1484 /* The older code accepts a <local-name> here, but I don't see
1485 that in the grammar. The older code does not accept a
1486 <template-param> here. */
1488 comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
1491 char peek2 = d_peek_next_char (di);
1492 if (peek2 == 'T' || peek2 == 't')
1494 dc = cplus_demangle_type (di);
1496 /* Destructor name. */
1497 dc = d_unqualified_name (di);
1499 else if (IS_DIGIT (peek)
1504 dc = d_unqualified_name (di);
1505 else if (peek == 'S')
1506 dc = d_substitution (di, 1);
1507 else if (peek == 'I')
1511 comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1512 dc = d_template_args (di);
1514 else if (peek == 'T')
1515 dc = d_template_param (di);
1516 else if (peek == 'E')
1518 else if (peek == 'M')
1520 /* Initializer scope for a lambda. We don't need to represent
1521 this; the normal code will just treat the variable as a type
1522 scope, which gives appropriate output. */
1534 ret = d_make_comp (di, comb_type, ret, dc);
1536 if (peek != 'S' && d_peek_char (di) != 'E')
1538 if (! d_add_substitution (di, ret))
1544 /* <unqualified-name> ::= <operator-name>
1545 ::= <ctor-dtor-name>
1547 ::= <local-source-name>
1549 <local-source-name> ::= L <source-name> <discriminator>
1552 static struct demangle_component *
1553 d_unqualified_name (struct d_info *di)
1555 struct demangle_component *ret;
1558 peek = d_peek_char (di);
1559 if (IS_DIGIT (peek))
1560 ret = d_source_name (di);
1561 else if (IS_LOWER (peek))
1563 ret = d_operator_name (di);
1564 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1566 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1567 if (!strcmp (ret->u.s_operator.op->code, "li"))
1568 ret = d_make_comp (di, DEMANGLE_COMPONENT_UNARY, ret,
1569 d_source_name (di));
1572 else if (peek == 'C' || peek == 'D')
1573 ret = d_ctor_dtor_name (di);
1574 else if (peek == 'L')
1578 ret = d_source_name (di);
1581 if (! d_discriminator (di))
1584 else if (peek == 'U')
1586 switch (d_peek_next_char (di))
1589 ret = d_lambda (di);
1592 ret = d_unnamed_type (di);
1601 if (d_peek_char (di) == 'B')
1602 ret = d_abi_tags (di, ret);
1606 /* <source-name> ::= <(positive length) number> <identifier> */
1608 static struct demangle_component *
1609 d_source_name (struct d_info *di)
1612 struct demangle_component *ret;
1614 len = d_number (di);
1617 ret = d_identifier (di, len);
1618 di->last_name = ret;
1622 /* number ::= [n] <(non-negative decimal integer)> */
1625 d_number (struct d_info *di)
1632 peek = d_peek_char (di);
1637 peek = d_peek_char (di);
1643 if (! IS_DIGIT (peek))
1649 ret = ret * 10 + peek - '0';
1651 peek = d_peek_char (di);
1655 /* Like d_number, but returns a demangle_component. */
1657 static struct demangle_component *
1658 d_number_component (struct d_info *di)
1660 struct demangle_component *ret = d_make_empty (di);
1663 ret->type = DEMANGLE_COMPONENT_NUMBER;
1664 ret->u.s_number.number = d_number (di);
1669 /* identifier ::= <(unqualified source code identifier)> */
1671 static struct demangle_component *
1672 d_identifier (struct d_info *di, int len)
1678 if (di->send - name < len)
1681 d_advance (di, len);
1683 /* A Java mangled name may have a trailing '$' if it is a C++
1684 keyword. This '$' is not included in the length count. We just
1686 if ((di->options & DMGL_JAVA) != 0
1687 && d_peek_char (di) == '$')
1690 /* Look for something which looks like a gcc encoding of an
1691 anonymous namespace, and replace it with a more user friendly
1693 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1694 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1695 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1699 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1700 if ((*s == '.' || *s == '_' || *s == '$')
1703 di->expansion -= len - sizeof "(anonymous namespace)";
1704 return d_make_name (di, "(anonymous namespace)",
1705 sizeof "(anonymous namespace)" - 1);
1709 return d_make_name (di, name, len);
1712 /* operator_name ::= many different two character encodings.
1714 ::= v <digit> <source-name>
1716 This list is sorted for binary search. */
1718 #define NL(s) s, (sizeof s) - 1
1720 CP_STATIC_IF_GLIBCPP_V3
1721 const struct demangle_operator_info cplus_demangle_operators[] =
1723 { "aN", NL ("&="), 2 },
1724 { "aS", NL ("="), 2 },
1725 { "aa", NL ("&&"), 2 },
1726 { "ad", NL ("&"), 1 },
1727 { "an", NL ("&"), 2 },
1728 { "at", NL ("alignof "), 1 },
1729 { "az", NL ("alignof "), 1 },
1730 { "cc", NL ("const_cast"), 2 },
1731 { "cl", NL ("()"), 2 },
1732 { "cm", NL (","), 2 },
1733 { "co", NL ("~"), 1 },
1734 { "dV", NL ("/="), 2 },
1735 { "da", NL ("delete[] "), 1 },
1736 { "dc", NL ("dynamic_cast"), 2 },
1737 { "de", NL ("*"), 1 },
1738 { "dl", NL ("delete "), 1 },
1739 { "ds", NL (".*"), 2 },
1740 { "dt", NL ("."), 2 },
1741 { "dv", NL ("/"), 2 },
1742 { "eO", NL ("^="), 2 },
1743 { "eo", NL ("^"), 2 },
1744 { "eq", NL ("=="), 2 },
1745 { "ge", NL (">="), 2 },
1746 { "gs", NL ("::"), 1 },
1747 { "gt", NL (">"), 2 },
1748 { "ix", NL ("[]"), 2 },
1749 { "lS", NL ("<<="), 2 },
1750 { "le", NL ("<="), 2 },
1751 { "li", NL ("operator\"\" "), 1 },
1752 { "ls", NL ("<<"), 2 },
1753 { "lt", NL ("<"), 2 },
1754 { "mI", NL ("-="), 2 },
1755 { "mL", NL ("*="), 2 },
1756 { "mi", NL ("-"), 2 },
1757 { "ml", NL ("*"), 2 },
1758 { "mm", NL ("--"), 1 },
1759 { "na", NL ("new[]"), 3 },
1760 { "ne", NL ("!="), 2 },
1761 { "ng", NL ("-"), 1 },
1762 { "nt", NL ("!"), 1 },
1763 { "nw", NL ("new"), 3 },
1764 { "oR", NL ("|="), 2 },
1765 { "oo", NL ("||"), 2 },
1766 { "or", NL ("|"), 2 },
1767 { "pL", NL ("+="), 2 },
1768 { "pl", NL ("+"), 2 },
1769 { "pm", NL ("->*"), 2 },
1770 { "pp", NL ("++"), 1 },
1771 { "ps", NL ("+"), 1 },
1772 { "pt", NL ("->"), 2 },
1773 { "qu", NL ("?"), 3 },
1774 { "rM", NL ("%="), 2 },
1775 { "rS", NL (">>="), 2 },
1776 { "rc", NL ("reinterpret_cast"), 2 },
1777 { "rm", NL ("%"), 2 },
1778 { "rs", NL (">>"), 2 },
1779 { "sc", NL ("static_cast"), 2 },
1780 { "st", NL ("sizeof "), 1 },
1781 { "sz", NL ("sizeof "), 1 },
1782 { "tr", NL ("throw"), 0 },
1783 { "tw", NL ("throw "), 1 },
1784 { NULL, NULL, 0, 0 }
1787 static struct demangle_component *
1788 d_operator_name (struct d_info *di)
1793 c1 = d_next_char (di);
1794 c2 = d_next_char (di);
1795 if (c1 == 'v' && IS_DIGIT (c2))
1796 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1797 else if (c1 == 'c' && c2 == 'v')
1799 struct demangle_component *type;
1800 int was_conversion = di->is_conversion;
1801 struct demangle_component *res;
1803 di->is_conversion = ! di->is_expression;
1804 type = cplus_demangle_type (di);
1805 if (di->is_conversion)
1806 res = d_make_comp (di, DEMANGLE_COMPONENT_CONVERSION, type, NULL);
1808 res = d_make_comp (di, DEMANGLE_COMPONENT_CAST, type, NULL);
1809 di->is_conversion = was_conversion;
1814 /* LOW is the inclusive lower bound. */
1816 /* HIGH is the exclusive upper bound. We subtract one to ignore
1817 the sentinel at the end of the array. */
1818 int high = ((sizeof (cplus_demangle_operators)
1819 / sizeof (cplus_demangle_operators[0]))
1825 const struct demangle_operator_info *p;
1827 i = low + (high - low) / 2;
1828 p = cplus_demangle_operators + i;
1830 if (c1 == p->code[0] && c2 == p->code[1])
1831 return d_make_operator (di, p);
1833 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1843 static struct demangle_component *
1844 d_make_character (struct d_info *di, int c)
1846 struct demangle_component *p;
1847 p = d_make_empty (di);
1850 p->type = DEMANGLE_COMPONENT_CHARACTER;
1851 p->u.s_character.character = c;
1856 static struct demangle_component *
1857 d_java_resource (struct d_info *di)
1859 struct demangle_component *p = NULL;
1860 struct demangle_component *next = NULL;
1865 len = d_number (di);
1869 /* Eat the leading '_'. */
1870 if (d_next_char (di) != '_')
1883 /* Each chunk is either a '$' escape... */
1901 next = d_make_character (di, c);
1909 /* ... or a sequence of characters. */
1912 while (i < len && str[i] && str[i] != '$')
1915 next = d_make_name (di, str, i);
1928 p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next);
1934 p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL);
1939 /* <special-name> ::= TV <type>
1943 ::= GV <(object) name>
1944 ::= T <call-offset> <(base) encoding>
1945 ::= Tc <call-offset> <call-offset> <(base) encoding>
1946 Also g++ extensions:
1947 ::= TC <type> <(offset) number> _ <(base) type>
1952 ::= Gr <resource name>
1957 static struct demangle_component *
1958 d_special_name (struct d_info *di)
1960 di->expansion += 20;
1961 if (d_check_char (di, 'T'))
1963 switch (d_next_char (di))
1967 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
1968 cplus_demangle_type (di), NULL);
1970 di->expansion -= 10;
1971 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
1972 cplus_demangle_type (di), NULL);
1974 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
1975 cplus_demangle_type (di), NULL);
1977 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
1978 cplus_demangle_type (di), NULL);
1981 if (! d_call_offset (di, 'h'))
1983 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
1984 d_encoding (di, 0), NULL);
1987 if (! d_call_offset (di, 'v'))
1989 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
1990 d_encoding (di, 0), NULL);
1993 if (! d_call_offset (di, '\0'))
1995 if (! d_call_offset (di, '\0'))
1997 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
1998 d_encoding (di, 0), NULL);
2002 struct demangle_component *derived_type;
2004 struct demangle_component *base_type;
2006 derived_type = cplus_demangle_type (di);
2007 offset = d_number (di);
2010 if (! d_check_char (di, '_'))
2012 base_type = cplus_demangle_type (di);
2013 /* We don't display the offset. FIXME: We should display
2014 it in verbose mode. */
2016 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
2017 base_type, derived_type);
2021 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
2022 cplus_demangle_type (di), NULL);
2024 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
2025 cplus_demangle_type (di), NULL);
2028 return d_make_comp (di, DEMANGLE_COMPONENT_TLS_INIT,
2032 return d_make_comp (di, DEMANGLE_COMPONENT_TLS_WRAPPER,
2039 else if (d_check_char (di, 'G'))
2041 switch (d_next_char (di))
2044 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
2048 struct demangle_component *name = d_name (di);
2049 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, name,
2050 d_number_component (di));
2054 return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
2055 d_encoding (di, 0), NULL);
2058 switch (d_next_char (di))
2061 return d_make_comp (di, DEMANGLE_COMPONENT_NONTRANSACTION_CLONE,
2062 d_encoding (di, 0), NULL);
2064 /* ??? The proposal is that other letters (such as 'h') stand
2065 for different variants of transaction cloning, such as
2066 compiling directly for hardware transaction support. But
2067 they still should all be transactional clones of some sort
2068 so go ahead and call them that. */
2070 return d_make_comp (di, DEMANGLE_COMPONENT_TRANSACTION_CLONE,
2071 d_encoding (di, 0), NULL);
2075 return d_java_resource (di);
2085 /* <call-offset> ::= h <nv-offset> _
2088 <nv-offset> ::= <(offset) number>
2090 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
2092 The C parameter, if not '\0', is a character we just read which is
2093 the start of the <call-offset>.
2095 We don't display the offset information anywhere. FIXME: We should
2096 display it in verbose mode. */
2099 d_call_offset (struct d_info *di, int c)
2102 c = d_next_char (di);
2109 if (! d_check_char (di, '_'))
2116 if (! d_check_char (di, '_'))
2122 /* <ctor-dtor-name> ::= C1
2130 static struct demangle_component *
2131 d_ctor_dtor_name (struct d_info *di)
2133 if (di->last_name != NULL)
2135 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
2136 di->expansion += di->last_name->u.s_name.len;
2137 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
2138 di->expansion += di->last_name->u.s_string.len;
2140 switch (d_peek_char (di))
2144 enum gnu_v3_ctor_kinds kind;
2146 switch (d_peek_next_char (di))
2149 kind = gnu_v3_complete_object_ctor;
2152 kind = gnu_v3_base_object_ctor;
2155 kind = gnu_v3_complete_object_allocating_ctor;
2158 kind = gnu_v3_unified_ctor;
2161 kind = gnu_v3_object_ctor_group;
2167 return d_make_ctor (di, kind, di->last_name);
2172 enum gnu_v3_dtor_kinds kind;
2174 switch (d_peek_next_char (di))
2177 kind = gnu_v3_deleting_dtor;
2180 kind = gnu_v3_complete_object_dtor;
2183 kind = gnu_v3_base_object_dtor;
2185 /* digit '3' is not used */
2187 kind = gnu_v3_unified_dtor;
2190 kind = gnu_v3_object_dtor_group;
2196 return d_make_dtor (di, kind, di->last_name);
2204 /* <type> ::= <builtin-type>
2206 ::= <class-enum-type>
2208 ::= <pointer-to-member-type>
2209 ::= <template-param>
2210 ::= <template-template-param> <template-args>
2212 ::= <CV-qualifiers> <type>
2215 ::= O <type> (C++0x)
2218 ::= U <source-name> <type>
2220 <builtin-type> ::= various one letter codes
2224 CP_STATIC_IF_GLIBCPP_V3
2225 const struct demangle_builtin_type_info
2226 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
2228 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
2229 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
2230 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
2231 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
2232 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
2233 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
2234 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
2235 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
2236 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
2237 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
2238 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2239 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
2240 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
2241 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
2242 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
2244 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2245 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2246 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2247 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
2248 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
2249 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2250 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
2251 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
2252 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
2253 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
2254 D_PRINT_UNSIGNED_LONG_LONG },
2255 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
2256 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT },
2257 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT },
2258 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT },
2259 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT },
2260 /* 30 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT },
2261 /* 31 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT },
2262 /* 32 */ { NL ("decltype(nullptr)"), NL ("decltype(nullptr)"),
2266 CP_STATIC_IF_GLIBCPP_V3
2267 struct demangle_component *
2268 cplus_demangle_type (struct d_info *di)
2271 struct demangle_component *ret;
2274 /* The ABI specifies that when CV-qualifiers are used, the base type
2275 is substitutable, and the fully qualified type is substitutable,
2276 but the base type with a strict subset of the CV-qualifiers is
2277 not substitutable. The natural recursive implementation of the
2278 CV-qualifiers would cause subsets to be substitutable, so instead
2279 we pull them all off now.
2281 FIXME: The ABI says that order-insensitive vendor qualifiers
2282 should be handled in the same way, but we have no way to tell
2283 which vendor qualifiers are order-insensitive and which are
2284 order-sensitive. So we just assume that they are all
2285 order-sensitive. g++ 3.4 supports only one vendor qualifier,
2286 __vector, and it treats it as order-sensitive when mangling
2289 peek = d_peek_char (di);
2290 if (peek == 'r' || peek == 'V' || peek == 'K')
2292 struct demangle_component **pret;
2294 pret = d_cv_qualifiers (di, &ret, 0);
2297 if (d_peek_char (di) == 'F')
2299 /* cv-qualifiers before a function type apply to 'this',
2300 so avoid adding the unqualified function type to
2301 the substitution list. */
2302 *pret = d_function_type (di);
2305 *pret = cplus_demangle_type (di);
2308 if ((*pret)->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
2309 || (*pret)->type == DEMANGLE_COMPONENT_REFERENCE_THIS)
2311 /* Move the ref-qualifier outside the cv-qualifiers so that
2312 they are printed in the right order. */
2313 struct demangle_component *fn = d_left (*pret);
2314 d_left (*pret) = ret;
2318 if (! d_add_substitution (di, ret))
2327 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2328 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
2329 case 'o': case 's': case 't':
2330 case 'v': case 'w': case 'x': case 'y': case 'z':
2331 ret = d_make_builtin_type (di,
2332 &cplus_demangle_builtin_types[peek - 'a']);
2333 di->expansion += ret->u.s_builtin.type->len;
2340 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
2341 d_source_name (di), NULL);
2345 ret = d_function_type (di);
2348 case '0': case '1': case '2': case '3': case '4':
2349 case '5': case '6': case '7': case '8': case '9':
2352 ret = d_class_enum_type (di);
2356 ret = d_array_type (di);
2360 ret = d_pointer_to_member_type (di);
2364 ret = d_template_param (di);
2365 if (d_peek_char (di) == 'I')
2367 /* This may be <template-template-param> <template-args>.
2368 If this is the type for a conversion operator, we can
2369 have a <template-template-param> here only by following
2370 a derivation like this:
2373 -> <template-prefix> <template-args>
2374 -> <prefix> <template-unqualified-name> <template-args>
2375 -> <unqualified-name> <template-unqualified-name> <template-args>
2376 -> <source-name> <template-unqualified-name> <template-args>
2377 -> <source-name> <operator-name> <template-args>
2378 -> <source-name> cv <type> <template-args>
2379 -> <source-name> cv <template-template-param> <template-args> <template-args>
2381 where the <template-args> is followed by another.
2382 Otherwise, we must have a derivation like this:
2385 -> <template-prefix> <template-args>
2386 -> <prefix> <template-unqualified-name> <template-args>
2387 -> <unqualified-name> <template-unqualified-name> <template-args>
2388 -> <source-name> <template-unqualified-name> <template-args>
2389 -> <source-name> <operator-name> <template-args>
2390 -> <source-name> cv <type> <template-args>
2391 -> <source-name> cv <template-param> <template-args>
2393 where we need to leave the <template-args> to be processed
2394 by d_prefix (following the <template-prefix>).
2396 The <template-template-param> part is a substitution
2398 if (! di->is_conversion)
2400 if (! d_add_substitution (di, ret))
2402 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2403 d_template_args (di));
2407 struct demangle_component *args;
2408 struct d_info_checkpoint checkpoint;
2410 d_checkpoint (di, &checkpoint);
2411 args = d_template_args (di);
2412 if (d_peek_char (di) == 'I')
2414 if (! d_add_substitution (di, ret))
2416 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2420 d_backtrack (di, &checkpoint);
2426 /* If this is a special substitution, then it is the start of
2427 <class-enum-type>. */
2431 peek_next = d_peek_next_char (di);
2432 if (IS_DIGIT (peek_next)
2434 || IS_UPPER (peek_next))
2436 ret = d_substitution (di, 0);
2437 /* The substituted name may have been a template name and
2438 may be followed by tepmlate args. */
2439 if (d_peek_char (di) == 'I')
2440 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2441 d_template_args (di));
2447 ret = d_class_enum_type (di);
2448 /* If the substitution was a complete type, then it is not
2449 a new substitution candidate. However, if the
2450 substitution was followed by template arguments, then
2451 the whole thing is a substitution candidate. */
2452 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
2460 ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE,
2461 cplus_demangle_type (di), NULL);
2466 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
2467 cplus_demangle_type (di), NULL);
2472 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
2473 cplus_demangle_type (di), NULL);
2478 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
2479 cplus_demangle_type (di), NULL);
2484 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
2485 cplus_demangle_type (di), NULL);
2490 ret = d_source_name (di);
2491 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
2492 cplus_demangle_type (di), ret);
2498 peek = d_next_char (di);
2503 /* decltype (expression) */
2504 ret = d_make_comp (di, DEMANGLE_COMPONENT_DECLTYPE,
2505 d_expression (di), NULL);
2506 if (ret && d_next_char (di) != 'E')
2512 /* Pack expansion. */
2513 ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2514 cplus_demangle_type (di), NULL);
2520 ret = d_make_name (di, "auto", 4);
2524 /* 32-bit decimal floating point */
2525 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[26]);
2526 di->expansion += ret->u.s_builtin.type->len;
2530 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[27]);
2531 di->expansion += ret->u.s_builtin.type->len;
2535 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[28]);
2536 di->expansion += ret->u.s_builtin.type->len;
2539 /* 16-bit half-precision FP */
2540 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[29]);
2541 di->expansion += ret->u.s_builtin.type->len;
2545 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[30]);
2546 di->expansion += ret->u.s_builtin.type->len;
2550 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[31]);
2551 di->expansion += ret->u.s_builtin.type->len;
2555 /* Fixed point types. DF<int bits><length><fract bits><sat> */
2556 ret = d_make_empty (di);
2557 ret->type = DEMANGLE_COMPONENT_FIXED_TYPE;
2558 if ((ret->u.s_fixed.accum = IS_DIGIT (d_peek_char (di))))
2559 /* For demangling we don't care about the bits. */
2561 ret->u.s_fixed.length = cplus_demangle_type (di);
2562 if (ret->u.s_fixed.length == NULL)
2565 peek = d_next_char (di);
2566 ret->u.s_fixed.sat = (peek == 's');
2570 ret = d_vector_type (di);
2575 /* decltype(nullptr) */
2576 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[32]);
2577 di->expansion += ret->u.s_builtin.type->len;
2591 if (! d_add_substitution (di, ret))
2598 /* <CV-qualifiers> ::= [r] [V] [K] */
2600 static struct demangle_component **
2601 d_cv_qualifiers (struct d_info *di,
2602 struct demangle_component **pret, int member_fn)
2604 struct demangle_component **pstart;
2608 peek = d_peek_char (di);
2609 while (peek == 'r' || peek == 'V' || peek == 'K')
2611 enum demangle_component_type t;
2617 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2618 : DEMANGLE_COMPONENT_RESTRICT);
2619 di->expansion += sizeof "restrict";
2621 else if (peek == 'V')
2624 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2625 : DEMANGLE_COMPONENT_VOLATILE);
2626 di->expansion += sizeof "volatile";
2631 ? DEMANGLE_COMPONENT_CONST_THIS
2632 : DEMANGLE_COMPONENT_CONST);
2633 di->expansion += sizeof "const";
2636 *pret = d_make_comp (di, t, NULL, NULL);
2639 pret = &d_left (*pret);
2641 peek = d_peek_char (di);
2644 if (!member_fn && peek == 'F')
2646 while (pstart != pret)
2648 switch ((*pstart)->type)
2650 case DEMANGLE_COMPONENT_RESTRICT:
2651 (*pstart)->type = DEMANGLE_COMPONENT_RESTRICT_THIS;
2653 case DEMANGLE_COMPONENT_VOLATILE:
2654 (*pstart)->type = DEMANGLE_COMPONENT_VOLATILE_THIS;
2656 case DEMANGLE_COMPONENT_CONST:
2657 (*pstart)->type = DEMANGLE_COMPONENT_CONST_THIS;
2662 pstart = &d_left (*pstart);
2669 /* <ref-qualifier> ::= R
2672 static struct demangle_component *
2673 d_ref_qualifier (struct d_info *di, struct demangle_component *sub)
2675 struct demangle_component *ret = sub;
2678 peek = d_peek_char (di);
2679 if (peek == 'R' || peek == 'O')
2681 enum demangle_component_type t;
2684 t = DEMANGLE_COMPONENT_REFERENCE_THIS;
2685 di->expansion += sizeof "&";
2689 t = DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS;
2690 di->expansion += sizeof "&&";
2694 ret = d_make_comp (di, t, ret, NULL);
2700 /* <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] E */
2702 static struct demangle_component *
2703 d_function_type (struct d_info *di)
2705 struct demangle_component *ret;
2707 if (! d_check_char (di, 'F'))
2709 if (d_peek_char (di) == 'Y')
2711 /* Function has C linkage. We don't print this information.
2712 FIXME: We should print it in verbose mode. */
2715 ret = d_bare_function_type (di, 1);
2716 ret = d_ref_qualifier (di, ret);
2718 if (! d_check_char (di, 'E'))
2725 static struct demangle_component *
2726 d_parmlist (struct d_info *di)
2728 struct demangle_component *tl;
2729 struct demangle_component **ptl;
2735 struct demangle_component *type;
2737 char peek = d_peek_char (di);
2738 if (peek == '\0' || peek == 'E' || peek == '.')
2740 if ((peek == 'R' || peek == 'O')
2741 && d_peek_next_char (di) == 'E')
2742 /* Function ref-qualifier, not a ref prefix for a parameter type. */
2744 type = cplus_demangle_type (di);
2747 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
2750 ptl = &d_right (*ptl);
2753 /* There should be at least one parameter type besides the optional
2754 return type. A function which takes no arguments will have a
2755 single parameter type void. */
2759 /* If we have a single parameter type void, omit it. */
2760 if (d_right (tl) == NULL
2761 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2762 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
2764 di->expansion -= d_left (tl)->u.s_builtin.type->len;
2771 /* <bare-function-type> ::= [J]<type>+ */
2773 static struct demangle_component *
2774 d_bare_function_type (struct d_info *di, int has_return_type)
2776 struct demangle_component *return_type;
2777 struct demangle_component *tl;
2780 /* Detect special qualifier indicating that the first argument
2781 is the return type. */
2782 peek = d_peek_char (di);
2786 has_return_type = 1;
2789 if (has_return_type)
2791 return_type = cplus_demangle_type (di);
2792 if (return_type == NULL)
2798 tl = d_parmlist (di);
2802 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE,
2806 /* <class-enum-type> ::= <name> */
2808 static struct demangle_component *
2809 d_class_enum_type (struct d_info *di)
2814 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2815 ::= A [<(dimension) expression>] _ <(element) type>
2818 static struct demangle_component *
2819 d_array_type (struct d_info *di)
2822 struct demangle_component *dim;
2824 if (! d_check_char (di, 'A'))
2827 peek = d_peek_char (di);
2830 else if (IS_DIGIT (peek))
2838 peek = d_peek_char (di);
2840 while (IS_DIGIT (peek));
2841 dim = d_make_name (di, s, d_str (di) - s);
2847 dim = d_expression (di);
2852 if (! d_check_char (di, '_'))
2855 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2856 cplus_demangle_type (di));
2859 /* <vector-type> ::= Dv <number> _ <type>
2860 ::= Dv _ <expression> _ <type> */
2862 static struct demangle_component *
2863 d_vector_type (struct d_info *di)
2866 struct demangle_component *dim;
2868 peek = d_peek_char (di);
2872 dim = d_expression (di);
2875 dim = d_number_component (di);
2880 if (! d_check_char (di, '_'))
2883 return d_make_comp (di, DEMANGLE_COMPONENT_VECTOR_TYPE, dim,
2884 cplus_demangle_type (di));
2887 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2889 static struct demangle_component *
2890 d_pointer_to_member_type (struct d_info *di)
2892 struct demangle_component *cl;
2893 struct demangle_component *mem;
2895 if (! d_check_char (di, 'M'))
2898 cl = cplus_demangle_type (di);
2902 /* The ABI says, "The type of a non-static member function is considered
2903 to be different, for the purposes of substitution, from the type of a
2904 namespace-scope or static member function whose type appears
2905 similar. The types of two non-static member functions are considered
2906 to be different, for the purposes of substitution, if the functions
2907 are members of different classes. In other words, for the purposes of
2908 substitution, the class of which the function is a member is
2909 considered part of the type of function."
2911 For a pointer to member function, this call to cplus_demangle_type
2912 will end up adding a (possibly qualified) non-member function type to
2913 the substitution table, which is not correct; however, the member
2914 function type will never be used in a substitution, so putting the
2915 wrong type in the substitution table is harmless. */
2917 mem = cplus_demangle_type (di);
2921 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
2924 /* <non-negative number> _ */
2927 d_compact_number (struct d_info *di)
2930 if (d_peek_char (di) == '_')
2932 else if (d_peek_char (di) == 'n')
2935 num = d_number (di) + 1;
2937 if (num < 0 || ! d_check_char (di, '_'))
2942 /* <template-param> ::= T_
2943 ::= T <(parameter-2 non-negative) number> _
2946 static struct demangle_component *
2947 d_template_param (struct d_info *di)
2951 if (! d_check_char (di, 'T'))
2954 param = d_compact_number (di);
2960 return d_make_template_param (di, param);
2963 /* <template-args> ::= I <template-arg>+ E */
2965 static struct demangle_component *
2966 d_template_args (struct d_info *di)
2968 struct demangle_component *hold_last_name;
2969 struct demangle_component *al;
2970 struct demangle_component **pal;
2972 /* Preserve the last name we saw--don't let the template arguments
2973 clobber it, as that would give us the wrong name for a subsequent
2974 constructor or destructor. */
2975 hold_last_name = di->last_name;
2977 if (d_peek_char (di) != 'I'
2978 && d_peek_char (di) != 'J')
2982 if (d_peek_char (di) == 'E')
2984 /* An argument pack can be empty. */
2986 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, NULL, NULL);
2993 struct demangle_component *a;
2995 a = d_template_arg (di);
2999 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
3002 pal = &d_right (*pal);
3004 if (d_peek_char (di) == 'E')
3011 di->last_name = hold_last_name;
3016 /* <template-arg> ::= <type>
3017 ::= X <expression> E
3021 static struct demangle_component *
3022 d_template_arg (struct d_info *di)
3024 struct demangle_component *ret;
3026 switch (d_peek_char (di))
3030 ret = d_expression (di);
3031 if (! d_check_char (di, 'E'))
3036 return d_expr_primary (di);
3040 /* An argument pack. */
3041 return d_template_args (di);
3044 return cplus_demangle_type (di);
3048 /* Parse a sequence of expressions until we hit the terminator
3051 static struct demangle_component *
3052 d_exprlist (struct d_info *di, char terminator)
3054 struct demangle_component *list = NULL;
3055 struct demangle_component **p = &list;
3057 if (d_peek_char (di) == terminator)
3060 return d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, NULL, NULL);
3065 struct demangle_component *arg = d_expression (di);
3069 *p = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, arg, NULL);
3074 if (d_peek_char (di) == terminator)
3084 /* Returns nonzero iff OP is an operator for a C++ cast: const_cast,
3085 dynamic_cast, static_cast or reinterpret_cast. */
3088 op_is_new_cast (struct demangle_component *op)
3090 const char *code = op->u.s_operator.op->code;
3091 return (code[1] == 'c'
3092 && (code[0] == 's' || code[0] == 'd'
3093 || code[0] == 'c' || code[0] == 'r'));
3096 /* <expression> ::= <(unary) operator-name> <expression>
3097 ::= <(binary) operator-name> <expression> <expression>
3098 ::= <(trinary) operator-name> <expression> <expression> <expression>
3099 ::= cl <expression>+ E
3101 ::= <template-param>
3102 ::= sr <type> <unqualified-name>
3103 ::= sr <type> <unqualified-name> <template-args>
3107 static inline struct demangle_component *
3108 d_expression_1 (struct d_info *di)
3112 peek = d_peek_char (di);
3114 return d_expr_primary (di);
3115 else if (peek == 'T')
3116 return d_template_param (di);
3117 else if (peek == 's' && d_peek_next_char (di) == 'r')
3119 struct demangle_component *type;
3120 struct demangle_component *name;
3123 type = cplus_demangle_type (di);
3124 name = d_unqualified_name (di);
3125 if (d_peek_char (di) != 'I')
3126 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
3128 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
3129 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
3130 d_template_args (di)));
3132 else if (peek == 's' && d_peek_next_char (di) == 'p')
3135 return d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
3136 d_expression_1 (di), NULL);
3138 else if (peek == 'f' && d_peek_next_char (di) == 'p')
3140 /* Function parameter used in a late-specified return type. */
3143 if (d_peek_char (di) == 'T')
3145 /* 'this' parameter. */
3151 index = d_compact_number (di);
3152 if (index == INT_MAX || index == -1)
3156 return d_make_function_param (di, index);
3158 else if (IS_DIGIT (peek)
3159 || (peek == 'o' && d_peek_next_char (di) == 'n'))
3161 /* We can get an unqualified name as an expression in the case of
3162 a dependent function call, i.e. decltype(f(t)). */
3163 struct demangle_component *name;
3166 /* operator-function-id, i.e. operator+(t). */
3169 name = d_unqualified_name (di);
3172 if (d_peek_char (di) == 'I')
3173 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
3174 d_template_args (di));
3178 else if ((peek == 'i' || peek == 't')
3179 && d_peek_next_char (di) == 'l')
3181 /* Brace-enclosed initializer list, untyped or typed. */
3182 struct demangle_component *type = NULL;
3184 type = cplus_demangle_type (di);
3185 if (!d_peek_next_char (di))
3188 return d_make_comp (di, DEMANGLE_COMPONENT_INITIALIZER_LIST,
3189 type, d_exprlist (di, 'E'));
3193 struct demangle_component *op;
3194 const char *code = NULL;
3197 op = d_operator_name (di);
3201 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
3203 code = op->u.s_operator.op->code;
3204 di->expansion += op->u.s_operator.op->len - 2;
3205 if (strcmp (code, "st") == 0)
3206 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3207 cplus_demangle_type (di));
3214 case DEMANGLE_COMPONENT_OPERATOR:
3215 args = op->u.s_operator.op->args;
3217 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3218 args = op->u.s_extended_operator.args;
3220 case DEMANGLE_COMPONENT_CAST:
3228 return d_make_comp (di, DEMANGLE_COMPONENT_NULLARY, op, NULL);
3232 struct demangle_component *operand;
3235 if (code && (code[0] == 'p' || code[0] == 'm')
3236 && code[1] == code[0])
3237 /* pp_ and mm_ are the prefix variants. */
3238 suffix = !d_check_char (di, '_');
3240 if (op->type == DEMANGLE_COMPONENT_CAST
3241 && d_check_char (di, '_'))
3242 operand = d_exprlist (di, 'E');
3244 operand = d_expression_1 (di);
3247 /* Indicate the suffix variant for d_print_comp. */
3248 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3250 DEMANGLE_COMPONENT_BINARY_ARGS,
3253 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3258 struct demangle_component *left;
3259 struct demangle_component *right;
3263 if (op_is_new_cast (op))
3264 left = cplus_demangle_type (di);
3266 left = d_expression_1 (di);
3267 if (!strcmp (code, "cl"))
3268 right = d_exprlist (di, 'E');
3269 else if (!strcmp (code, "dt") || !strcmp (code, "pt"))
3271 right = d_unqualified_name (di);
3272 if (d_peek_char (di) == 'I')
3273 right = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE,
3274 right, d_template_args (di));
3277 right = d_expression_1 (di);
3279 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
3281 DEMANGLE_COMPONENT_BINARY_ARGS,
3286 struct demangle_component *first;
3287 struct demangle_component *second;
3288 struct demangle_component *third;
3292 else if (!strcmp (code, "qu"))
3294 /* ?: expression. */
3295 first = d_expression_1 (di);
3296 second = d_expression_1 (di);
3297 third = d_expression_1 (di);
3299 else if (code[0] == 'n')
3301 /* new-expression. */
3302 if (code[1] != 'w' && code[1] != 'a')
3304 first = d_exprlist (di, '_');
3305 second = cplus_demangle_type (di);
3306 if (d_peek_char (di) == 'E')
3311 else if (d_peek_char (di) == 'p'
3312 && d_peek_next_char (di) == 'i')
3314 /* Parenthesized initializer. */
3316 third = d_exprlist (di, 'E');
3318 else if (d_peek_char (di) == 'i'
3319 && d_peek_next_char (di) == 'l')
3320 /* initializer-list. */
3321 third = d_expression_1 (di);
3327 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
3329 DEMANGLE_COMPONENT_TRINARY_ARG1,
3332 DEMANGLE_COMPONENT_TRINARY_ARG2,
3341 static struct demangle_component *
3342 d_expression (struct d_info *di)
3344 struct demangle_component *ret;
3345 int was_expression = di->is_expression;
3347 di->is_expression = 1;
3348 ret = d_expression_1 (di);
3349 di->is_expression = was_expression;
3353 /* <expr-primary> ::= L <type> <(value) number> E
3354 ::= L <type> <(value) float> E
3355 ::= L <mangled-name> E
3358 static struct demangle_component *
3359 d_expr_primary (struct d_info *di)
3361 struct demangle_component *ret;
3363 if (! d_check_char (di, 'L'))
3365 if (d_peek_char (di) == '_'
3366 /* Workaround for G++ bug; see comment in write_template_arg. */
3367 || d_peek_char (di) == 'Z')
3368 ret = cplus_demangle_mangled_name (di, 0);
3371 struct demangle_component *type;
3372 enum demangle_component_type t;
3375 type = cplus_demangle_type (di);
3379 /* If we have a type we know how to print, we aren't going to
3380 print the type name itself. */
3381 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
3382 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
3383 di->expansion -= type->u.s_builtin.type->len;
3385 /* Rather than try to interpret the literal value, we just
3386 collect it as a string. Note that it's possible to have a
3387 floating point literal here. The ABI specifies that the
3388 format of such literals is machine independent. That's fine,
3389 but what's not fine is that versions of g++ up to 3.2 with
3390 -fabi-version=1 used upper case letters in the hex constant,
3391 and dumped out gcc's internal representation. That makes it
3392 hard to tell where the constant ends, and hard to dump the
3393 constant in any readable form anyhow. We don't attempt to
3394 handle these cases. */
3396 t = DEMANGLE_COMPONENT_LITERAL;
3397 if (d_peek_char (di) == 'n')
3399 t = DEMANGLE_COMPONENT_LITERAL_NEG;
3403 while (d_peek_char (di) != 'E')
3405 if (d_peek_char (di) == '\0')
3409 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
3411 if (! d_check_char (di, 'E'))
3416 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
3417 ::= Z <(function) encoding> E s [<discriminator>]
3418 ::= Z <(function) encoding> E d [<parameter> number>] _ <entity name>
3421 static struct demangle_component *
3422 d_local_name (struct d_info *di)
3424 struct demangle_component *function;
3426 if (! d_check_char (di, 'Z'))
3429 function = d_encoding (di, 0);
3431 if (! d_check_char (di, 'E'))
3434 if (d_peek_char (di) == 's')
3437 if (! d_discriminator (di))
3439 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
3440 d_make_name (di, "string literal",
3441 sizeof "string literal" - 1));
3445 struct demangle_component *name;
3448 if (d_peek_char (di) == 'd')
3450 /* Default argument scope: d <number> _. */
3452 num = d_compact_number (di);
3461 /* Lambdas and unnamed types have internal discriminators. */
3462 case DEMANGLE_COMPONENT_LAMBDA:
3463 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
3466 if (! d_discriminator (di))
3470 name = d_make_default_arg (di, num, name);
3471 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
3475 /* <discriminator> ::= _ <(non-negative) number>
3477 We demangle the discriminator, but we don't print it out. FIXME:
3478 We should print it out in verbose mode. */
3481 d_discriminator (struct d_info *di)
3485 if (d_peek_char (di) != '_')
3488 discrim = d_number (di);
3494 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ */
3496 static struct demangle_component *
3497 d_lambda (struct d_info *di)
3499 struct demangle_component *tl;
3500 struct demangle_component *ret;
3503 if (! d_check_char (di, 'U'))
3505 if (! d_check_char (di, 'l'))
3508 tl = d_parmlist (di);
3512 if (! d_check_char (di, 'E'))
3515 num = d_compact_number (di);
3519 ret = d_make_empty (di);
3522 ret->type = DEMANGLE_COMPONENT_LAMBDA;
3523 ret->u.s_unary_num.sub = tl;
3524 ret->u.s_unary_num.num = num;
3527 if (! d_add_substitution (di, ret))
3533 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
3535 static struct demangle_component *
3536 d_unnamed_type (struct d_info *di)
3538 struct demangle_component *ret;
3541 if (! d_check_char (di, 'U'))
3543 if (! d_check_char (di, 't'))
3546 num = d_compact_number (di);
3550 ret = d_make_empty (di);
3553 ret->type = DEMANGLE_COMPONENT_UNNAMED_TYPE;
3554 ret->u.s_number.number = num;
3557 if (! d_add_substitution (di, ret))
3563 /* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]*
3566 static struct demangle_component *
3567 d_clone_suffix (struct d_info *di, struct demangle_component *encoding)
3569 const char *suffix = d_str (di);
3570 const char *pend = suffix;
3571 struct demangle_component *n;
3573 if (*pend == '.' && (IS_LOWER (pend[1]) || pend[1] == '_'))
3576 while (IS_LOWER (*pend) || *pend == '_')
3579 while (*pend == '.' && IS_DIGIT (pend[1]))
3582 while (IS_DIGIT (*pend))
3585 d_advance (di, pend - suffix);
3586 n = d_make_name (di, suffix, pend - suffix);
3587 return d_make_comp (di, DEMANGLE_COMPONENT_CLONE, encoding, n);
3590 /* Add a new substitution. */
3593 d_add_substitution (struct d_info *di, struct demangle_component *dc)
3597 if (di->next_sub >= di->num_subs)
3599 di->subs[di->next_sub] = dc;
3604 /* <substitution> ::= S <seq-id> _
3614 If PREFIX is non-zero, then this type is being used as a prefix in
3615 a qualified name. In this case, for the standard substitutions, we
3616 need to check whether we are being used as a prefix for a
3617 constructor or destructor, and return a full template name.
3618 Otherwise we will get something like std::iostream::~iostream()
3619 which does not correspond particularly well to any function which
3620 actually appears in the source.
3623 static const struct d_standard_sub_info standard_subs[] =
3628 { 'a', NL ("std::allocator"),
3629 NL ("std::allocator"),
3631 { 'b', NL ("std::basic_string"),
3632 NL ("std::basic_string"),
3633 NL ("basic_string") },
3634 { 's', NL ("std::string"),
3635 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
3636 NL ("basic_string") },
3637 { 'i', NL ("std::istream"),
3638 NL ("std::basic_istream<char, std::char_traits<char> >"),
3639 NL ("basic_istream") },
3640 { 'o', NL ("std::ostream"),
3641 NL ("std::basic_ostream<char, std::char_traits<char> >"),
3642 NL ("basic_ostream") },
3643 { 'd', NL ("std::iostream"),
3644 NL ("std::basic_iostream<char, std::char_traits<char> >"),
3645 NL ("basic_iostream") }
3648 static struct demangle_component *
3649 d_substitution (struct d_info *di, int prefix)
3653 if (! d_check_char (di, 'S'))
3656 c = d_next_char (di);
3657 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
3666 unsigned int new_id;
3669 new_id = id * 36 + c - '0';
3670 else if (IS_UPPER (c))
3671 new_id = id * 36 + c - 'A' + 10;
3677 c = d_next_char (di);
3684 if (id >= (unsigned int) di->next_sub)
3689 return di->subs[id];
3694 const struct d_standard_sub_info *p;
3695 const struct d_standard_sub_info *pend;
3697 verbose = (di->options & DMGL_VERBOSE) != 0;
3698 if (! verbose && prefix)
3702 peek = d_peek_char (di);
3703 if (peek == 'C' || peek == 'D')
3707 pend = (&standard_subs[0]
3708 + sizeof standard_subs / sizeof standard_subs[0]);
3709 for (p = &standard_subs[0]; p < pend; ++p)
3715 struct demangle_component *c;
3717 if (p->set_last_name != NULL)
3718 di->last_name = d_make_sub (di, p->set_last_name,
3719 p->set_last_name_len);
3722 s = p->full_expansion;
3727 s = p->simple_expansion;
3728 len = p->simple_len;
3730 di->expansion += len;
3731 c = d_make_sub (di, s, len);
3732 if (d_peek_char (di) == 'B')
3734 /* If there are ABI tags on the abbreviation, it becomes
3735 a substitution candidate. */
3736 c = d_abi_tags (di, c);
3737 d_add_substitution (di, c);
3748 d_checkpoint (struct d_info *di, struct d_info_checkpoint *checkpoint)
3750 checkpoint->n = di->n;
3751 checkpoint->next_comp = di->next_comp;
3752 checkpoint->next_sub = di->next_sub;
3753 checkpoint->did_subs = di->did_subs;
3754 checkpoint->expansion = di->expansion;
3758 d_backtrack (struct d_info *di, struct d_info_checkpoint *checkpoint)
3760 di->n = checkpoint->n;
3761 di->next_comp = checkpoint->next_comp;
3762 di->next_sub = checkpoint->next_sub;
3763 di->did_subs = checkpoint->did_subs;
3764 di->expansion = checkpoint->expansion;
3767 /* Initialize a growable string. */
3770 d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
3775 dgs->allocation_failure = 0;
3778 d_growable_string_resize (dgs, estimate);
3781 /* Grow a growable string to a given size. */
3784 d_growable_string_resize (struct d_growable_string *dgs, size_t need)
3789 if (dgs->allocation_failure)
3792 /* Start allocation at two bytes to avoid any possibility of confusion
3793 with the special value of 1 used as a return in *palc to indicate
3794 allocation failures. */
3795 newalc = dgs->alc > 0 ? dgs->alc : 2;
3796 while (newalc < need)
3799 newbuf = (char *) realloc (dgs->buf, newalc);
3806 dgs->allocation_failure = 1;
3813 /* Append a buffer to a growable string. */
3816 d_growable_string_append_buffer (struct d_growable_string *dgs,
3817 const char *s, size_t l)
3821 need = dgs->len + l + 1;
3822 if (need > dgs->alc)
3823 d_growable_string_resize (dgs, need);
3825 if (dgs->allocation_failure)
3828 memcpy (dgs->buf + dgs->len, s, l);
3829 dgs->buf[dgs->len + l] = '\0';
3833 /* Bridge growable strings to the callback mechanism. */
3836 d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
3838 struct d_growable_string *dgs = (struct d_growable_string*) opaque;
3840 d_growable_string_append_buffer (dgs, s, l);
3843 /* Walk the tree, counting the number of templates encountered, and
3844 the number of times a scope might be saved. These counts will be
3845 used to allocate data structures for d_print_comp, so the logic
3846 here must mirror the logic d_print_comp will use. It is not
3847 important that the resulting numbers are exact, so long as they
3848 are larger than the actual numbers encountered. */
3851 d_count_templates_scopes (int *num_templates, int *num_scopes,
3852 const struct demangle_component *dc)
3859 case DEMANGLE_COMPONENT_NAME:
3860 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
3861 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
3862 case DEMANGLE_COMPONENT_SUB_STD:
3863 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3864 case DEMANGLE_COMPONENT_OPERATOR:
3865 case DEMANGLE_COMPONENT_CHARACTER:
3866 case DEMANGLE_COMPONENT_NUMBER:
3867 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
3870 case DEMANGLE_COMPONENT_TEMPLATE:
3872 goto recurse_left_right;
3874 case DEMANGLE_COMPONENT_REFERENCE:
3875 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
3876 if (d_left (dc)->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
3878 goto recurse_left_right;
3880 case DEMANGLE_COMPONENT_QUAL_NAME:
3881 case DEMANGLE_COMPONENT_LOCAL_NAME:
3882 case DEMANGLE_COMPONENT_TYPED_NAME:
3883 case DEMANGLE_COMPONENT_VTABLE:
3884 case DEMANGLE_COMPONENT_VTT:
3885 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
3886 case DEMANGLE_COMPONENT_TYPEINFO:
3887 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
3888 case DEMANGLE_COMPONENT_TYPEINFO_FN:
3889 case DEMANGLE_COMPONENT_THUNK:
3890 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
3891 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
3892 case DEMANGLE_COMPONENT_JAVA_CLASS:
3893 case DEMANGLE_COMPONENT_GUARD:
3894 case DEMANGLE_COMPONENT_TLS_INIT:
3895 case DEMANGLE_COMPONENT_TLS_WRAPPER:
3896 case DEMANGLE_COMPONENT_REFTEMP:
3897 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
3898 case DEMANGLE_COMPONENT_RESTRICT:
3899 case DEMANGLE_COMPONENT_VOLATILE:
3900 case DEMANGLE_COMPONENT_CONST:
3901 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3902 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3903 case DEMANGLE_COMPONENT_CONST_THIS:
3904 case DEMANGLE_COMPONENT_REFERENCE_THIS:
3905 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
3906 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3907 case DEMANGLE_COMPONENT_POINTER:
3908 case DEMANGLE_COMPONENT_COMPLEX:
3909 case DEMANGLE_COMPONENT_IMAGINARY:
3910 case DEMANGLE_COMPONENT_VENDOR_TYPE:
3911 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
3912 case DEMANGLE_COMPONENT_ARRAY_TYPE:
3913 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3914 case DEMANGLE_COMPONENT_VECTOR_TYPE:
3915 case DEMANGLE_COMPONENT_ARGLIST:
3916 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
3917 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
3918 case DEMANGLE_COMPONENT_CAST:
3919 case DEMANGLE_COMPONENT_CONVERSION:
3920 case DEMANGLE_COMPONENT_NULLARY:
3921 case DEMANGLE_COMPONENT_UNARY:
3922 case DEMANGLE_COMPONENT_BINARY:
3923 case DEMANGLE_COMPONENT_BINARY_ARGS:
3924 case DEMANGLE_COMPONENT_TRINARY:
3925 case DEMANGLE_COMPONENT_TRINARY_ARG1:
3926 case DEMANGLE_COMPONENT_TRINARY_ARG2:
3927 case DEMANGLE_COMPONENT_LITERAL:
3928 case DEMANGLE_COMPONENT_LITERAL_NEG:
3929 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
3930 case DEMANGLE_COMPONENT_COMPOUND_NAME:
3931 case DEMANGLE_COMPONENT_DECLTYPE:
3932 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
3933 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
3934 case DEMANGLE_COMPONENT_PACK_EXPANSION:
3935 case DEMANGLE_COMPONENT_TAGGED_NAME:
3936 case DEMANGLE_COMPONENT_CLONE:
3938 d_count_templates_scopes (num_templates, num_scopes,
3940 d_count_templates_scopes (num_templates, num_scopes,
3944 case DEMANGLE_COMPONENT_CTOR:
3945 d_count_templates_scopes (num_templates, num_scopes,
3949 case DEMANGLE_COMPONENT_DTOR:
3950 d_count_templates_scopes (num_templates, num_scopes,
3954 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3955 d_count_templates_scopes (num_templates, num_scopes,
3956 dc->u.s_extended_operator.name);
3959 case DEMANGLE_COMPONENT_FIXED_TYPE:
3960 d_count_templates_scopes (num_templates, num_scopes,
3961 dc->u.s_fixed.length);
3964 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
3965 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
3966 d_count_templates_scopes (num_templates, num_scopes,
3970 case DEMANGLE_COMPONENT_LAMBDA:
3971 case DEMANGLE_COMPONENT_DEFAULT_ARG:
3972 d_count_templates_scopes (num_templates, num_scopes,
3973 dc->u.s_unary_num.sub);
3978 /* Initialize a print information structure. */
3981 d_print_init (struct d_print_info *dpi, demangle_callbackref callback,
3982 void *opaque, const struct demangle_component *dc)
3985 dpi->last_char = '\0';
3986 dpi->templates = NULL;
3987 dpi->modifiers = NULL;
3988 dpi->pack_index = 0;
3989 dpi->flush_count = 0;
3991 dpi->callback = callback;
3992 dpi->opaque = opaque;
3994 dpi->demangle_failure = 0;
3996 dpi->component_stack = NULL;
3998 dpi->saved_scopes = NULL;
3999 dpi->next_saved_scope = 0;
4000 dpi->num_saved_scopes = 0;
4002 dpi->copy_templates = NULL;
4003 dpi->next_copy_template = 0;
4004 dpi->num_copy_templates = 0;
4006 d_count_templates_scopes (&dpi->num_copy_templates,
4007 &dpi->num_saved_scopes, dc);
4008 dpi->num_copy_templates *= dpi->num_saved_scopes;
4010 dpi->current_template = NULL;
4013 /* Indicate that an error occurred during printing, and test for error. */
4016 d_print_error (struct d_print_info *dpi)
4018 dpi->demangle_failure = 1;
4022 d_print_saw_error (struct d_print_info *dpi)
4024 return dpi->demangle_failure != 0;
4027 /* Flush buffered characters to the callback. */
4030 d_print_flush (struct d_print_info *dpi)
4032 dpi->buf[dpi->len] = '\0';
4033 dpi->callback (dpi->buf, dpi->len, dpi->opaque);
4038 /* Append characters and buffers for printing. */
4041 d_append_char (struct d_print_info *dpi, char c)
4043 if (dpi->len == sizeof (dpi->buf) - 1)
4044 d_print_flush (dpi);
4046 dpi->buf[dpi->len++] = c;
4051 d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
4055 for (i = 0; i < l; i++)
4056 d_append_char (dpi, s[i]);
4060 d_append_string (struct d_print_info *dpi, const char *s)
4062 d_append_buffer (dpi, s, strlen (s));
4066 d_append_num (struct d_print_info *dpi, int l)
4069 sprintf (buf,"%d", l);
4070 d_append_string (dpi, buf);
4074 d_last_char (struct d_print_info *dpi)
4076 return dpi->last_char;
4079 /* Turn components into a human readable string. OPTIONS is the
4080 options bits passed to the demangler. DC is the tree to print.
4081 CALLBACK is a function to call to flush demangled string segments
4082 as they fill the intermediate buffer, and OPAQUE is a generalized
4083 callback argument. On success, this returns 1. On failure,
4084 it returns 0, indicating a bad parse. It does not use heap
4085 memory to build an output string, so cannot encounter memory
4086 allocation failure. */
4088 CP_STATIC_IF_GLIBCPP_V3
4090 cplus_demangle_print_callback (int options,
4091 const struct demangle_component *dc,
4092 demangle_callbackref callback, void *opaque)
4094 struct d_print_info dpi;
4096 d_print_init (&dpi, callback, opaque, dc);
4099 #ifdef CP_DYNAMIC_ARRAYS
4100 __extension__ struct d_saved_scope scopes[dpi.num_saved_scopes];
4101 __extension__ struct d_print_template temps[dpi.num_copy_templates];
4103 dpi.saved_scopes = scopes;
4104 dpi.copy_templates = temps;
4106 dpi.saved_scopes = alloca (dpi.num_saved_scopes
4107 * sizeof (*dpi.saved_scopes));
4108 dpi.copy_templates = alloca (dpi.num_copy_templates
4109 * sizeof (*dpi.copy_templates));
4112 d_print_comp (&dpi, options, dc);
4115 d_print_flush (&dpi);
4117 return ! d_print_saw_error (&dpi);
4120 /* Turn components into a human readable string. OPTIONS is the
4121 options bits passed to the demangler. DC is the tree to print.
4122 ESTIMATE is a guess at the length of the result. This returns a
4123 string allocated by malloc, or NULL on error. On success, this
4124 sets *PALC to the size of the allocated buffer. On failure, this
4125 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
4128 CP_STATIC_IF_GLIBCPP_V3
4130 cplus_demangle_print (int options, const struct demangle_component *dc,
4131 int estimate, size_t *palc)
4133 struct d_growable_string dgs;
4135 d_growable_string_init (&dgs, estimate);
4137 if (! cplus_demangle_print_callback (options, dc,
4138 d_growable_string_callback_adapter,
4146 *palc = dgs.allocation_failure ? 1 : dgs.alc;
4150 /* Returns the I'th element of the template arglist ARGS, or NULL on
4153 static struct demangle_component *
4154 d_index_template_argument (struct demangle_component *args, int i)
4156 struct demangle_component *a;
4162 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4168 if (i != 0 || a == NULL)
4174 /* Returns the template argument from the current context indicated by DC,
4175 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
4177 static struct demangle_component *
4178 d_lookup_template_argument (struct d_print_info *dpi,
4179 const struct demangle_component *dc)
4181 if (dpi->templates == NULL)
4183 d_print_error (dpi);
4187 return d_index_template_argument
4188 (d_right (dpi->templates->template_decl),
4189 dc->u.s_number.number);
4192 /* Returns a template argument pack used in DC (any will do), or NULL. */
4194 static struct demangle_component *
4195 d_find_pack (struct d_print_info *dpi,
4196 const struct demangle_component *dc)
4198 struct demangle_component *a;
4204 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
4205 a = d_lookup_template_argument (dpi, dc);
4206 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4210 case DEMANGLE_COMPONENT_PACK_EXPANSION:
4213 case DEMANGLE_COMPONENT_LAMBDA:
4214 case DEMANGLE_COMPONENT_NAME:
4215 case DEMANGLE_COMPONENT_TAGGED_NAME:
4216 case DEMANGLE_COMPONENT_OPERATOR:
4217 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
4218 case DEMANGLE_COMPONENT_SUB_STD:
4219 case DEMANGLE_COMPONENT_CHARACTER:
4220 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
4221 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
4222 case DEMANGLE_COMPONENT_FIXED_TYPE:
4223 case DEMANGLE_COMPONENT_DEFAULT_ARG:
4224 case DEMANGLE_COMPONENT_NUMBER:
4227 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
4228 return d_find_pack (dpi, dc->u.s_extended_operator.name);
4229 case DEMANGLE_COMPONENT_CTOR:
4230 return d_find_pack (dpi, dc->u.s_ctor.name);
4231 case DEMANGLE_COMPONENT_DTOR:
4232 return d_find_pack (dpi, dc->u.s_dtor.name);
4235 a = d_find_pack (dpi, d_left (dc));
4238 return d_find_pack (dpi, d_right (dc));
4242 /* Returns the length of the template argument pack DC. */
4245 d_pack_length (const struct demangle_component *dc)
4248 while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
4249 && d_left (dc) != NULL)
4257 /* DC is a component of a mangled expression. Print it, wrapped in parens
4261 d_print_subexpr (struct d_print_info *dpi, int options,
4262 const struct demangle_component *dc)
4265 if (dc->type == DEMANGLE_COMPONENT_NAME
4266 || dc->type == DEMANGLE_COMPONENT_QUAL_NAME
4267 || dc->type == DEMANGLE_COMPONENT_INITIALIZER_LIST
4268 || dc->type == DEMANGLE_COMPONENT_FUNCTION_PARAM)
4271 d_append_char (dpi, '(');
4272 d_print_comp (dpi, options, dc);
4274 d_append_char (dpi, ')');
4277 /* Save the current scope. */
4280 d_save_scope (struct d_print_info *dpi,
4281 const struct demangle_component *container)
4283 struct d_saved_scope *scope;
4284 struct d_print_template *src, **link;
4286 if (dpi->next_saved_scope >= dpi->num_saved_scopes)
4288 d_print_error (dpi);
4291 scope = &dpi->saved_scopes[dpi->next_saved_scope];
4292 dpi->next_saved_scope++;
4294 scope->container = container;
4295 link = &scope->templates;
4297 for (src = dpi->templates; src != NULL; src = src->next)
4299 struct d_print_template *dst;
4301 if (dpi->next_copy_template >= dpi->num_copy_templates)
4303 d_print_error (dpi);
4306 dst = &dpi->copy_templates[dpi->next_copy_template];
4307 dpi->next_copy_template++;
4309 dst->template_decl = src->template_decl;
4317 /* Attempt to locate a previously saved scope. Returns NULL if no
4318 corresponding saved scope was found. */
4320 static struct d_saved_scope *
4321 d_get_saved_scope (struct d_print_info *dpi,
4322 const struct demangle_component *container)
4326 for (i = 0; i < dpi->next_saved_scope; i++)
4327 if (dpi->saved_scopes[i].container == container)
4328 return &dpi->saved_scopes[i];
4333 /* Subroutine to handle components. */
4336 d_print_comp_inner (struct d_print_info *dpi, int options,
4337 const struct demangle_component *dc)
4339 /* Magic variable to let reference smashing skip over the next modifier
4340 without needing to modify *dc. */
4341 const struct demangle_component *mod_inner = NULL;
4343 /* Variable used to store the current templates while a previously
4344 captured scope is used. */
4345 struct d_print_template *saved_templates;
4347 /* Nonzero if templates have been stored in the above variable. */
4348 int need_template_restore = 0;
4352 d_print_error (dpi);
4355 if (d_print_saw_error (dpi))
4360 case DEMANGLE_COMPONENT_NAME:
4361 if ((options & DMGL_JAVA) == 0)
4362 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
4364 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
4367 case DEMANGLE_COMPONENT_TAGGED_NAME:
4368 d_print_comp (dpi, options, d_left (dc));
4369 d_append_string (dpi, "[abi:");
4370 d_print_comp (dpi, options, d_right (dc));
4371 d_append_char (dpi, ']');
4374 case DEMANGLE_COMPONENT_QUAL_NAME:
4375 case DEMANGLE_COMPONENT_LOCAL_NAME:
4376 d_print_comp (dpi, options, d_left (dc));
4377 if ((options & DMGL_JAVA) == 0)
4378 d_append_string (dpi, "::");
4380 d_append_char (dpi, '.');
4382 struct demangle_component *local_name = d_right (dc);
4383 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
4385 d_append_string (dpi, "{default arg#");
4386 d_append_num (dpi, local_name->u.s_unary_num.num + 1);
4387 d_append_string (dpi, "}::");
4388 local_name = local_name->u.s_unary_num.sub;
4390 d_print_comp (dpi, options, local_name);
4394 case DEMANGLE_COMPONENT_TYPED_NAME:
4396 struct d_print_mod *hold_modifiers;
4397 struct demangle_component *typed_name;
4398 struct d_print_mod adpm[4];
4400 struct d_print_template dpt;
4402 /* Pass the name down to the type so that it can be printed in
4403 the right place for the type. We also have to pass down
4404 any CV-qualifiers, which apply to the this parameter. */
4405 hold_modifiers = dpi->modifiers;
4408 typed_name = d_left (dc);
4409 while (typed_name != NULL)
4411 if (i >= sizeof adpm / sizeof adpm[0])
4413 d_print_error (dpi);
4417 adpm[i].next = dpi->modifiers;
4418 dpi->modifiers = &adpm[i];
4419 adpm[i].mod = typed_name;
4420 adpm[i].printed = 0;
4421 adpm[i].templates = dpi->templates;
4424 if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
4425 && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
4426 && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS
4427 && typed_name->type != DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
4428 && typed_name->type != DEMANGLE_COMPONENT_REFERENCE_THIS)
4431 typed_name = d_left (typed_name);
4434 if (typed_name == NULL)
4436 d_print_error (dpi);
4440 /* If typed_name is a template, then it applies to the
4441 function type as well. */
4442 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
4444 dpt.next = dpi->templates;
4445 dpi->templates = &dpt;
4446 dpt.template_decl = typed_name;
4449 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
4450 there may be CV-qualifiers on its right argument which
4451 really apply here; this happens when parsing a class which
4452 is local to a function. */
4453 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
4455 struct demangle_component *local_name;
4457 local_name = d_right (typed_name);
4458 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
4459 local_name = local_name->u.s_unary_num.sub;
4460 if (local_name == NULL)
4462 d_print_error (dpi);
4465 while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
4466 || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
4467 || local_name->type == DEMANGLE_COMPONENT_CONST_THIS
4468 || local_name->type == DEMANGLE_COMPONENT_REFERENCE_THIS
4469 || (local_name->type
4470 == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS))
4472 if (i >= sizeof adpm / sizeof adpm[0])
4474 d_print_error (dpi);
4478 adpm[i] = adpm[i - 1];
4479 adpm[i].next = &adpm[i - 1];
4480 dpi->modifiers = &adpm[i];
4482 adpm[i - 1].mod = local_name;
4483 adpm[i - 1].printed = 0;
4484 adpm[i - 1].templates = dpi->templates;
4487 local_name = d_left (local_name);
4491 d_print_comp (dpi, options, d_right (dc));
4493 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
4494 dpi->templates = dpt.next;
4496 /* If the modifiers didn't get printed by the type, print them
4501 if (! adpm[i].printed)
4503 d_append_char (dpi, ' ');
4504 d_print_mod (dpi, options, adpm[i].mod);
4508 dpi->modifiers = hold_modifiers;
4513 case DEMANGLE_COMPONENT_TEMPLATE:
4515 struct d_print_mod *hold_dpm;
4516 struct demangle_component *dcl;
4517 const struct demangle_component *hold_current;
4519 /* This template may need to be referenced by a cast operator
4520 contained in its subtree. */
4521 hold_current = dpi->current_template;
4522 dpi->current_template = dc;
4524 /* Don't push modifiers into a template definition. Doing so
4525 could give the wrong definition for a template argument.
4526 Instead, treat the template essentially as a name. */
4528 hold_dpm = dpi->modifiers;
4529 dpi->modifiers = NULL;
4533 if ((options & DMGL_JAVA) != 0
4534 && dcl->type == DEMANGLE_COMPONENT_NAME
4535 && dcl->u.s_name.len == 6
4536 && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
4538 /* Special-case Java arrays, so that JArray<TYPE> appears
4539 instead as TYPE[]. */
4541 d_print_comp (dpi, options, d_right (dc));
4542 d_append_string (dpi, "[]");
4546 d_print_comp (dpi, options, dcl);
4547 if (d_last_char (dpi) == '<')
4548 d_append_char (dpi, ' ');
4549 d_append_char (dpi, '<');
4550 d_print_comp (dpi, options, d_right (dc));
4551 /* Avoid generating two consecutive '>' characters, to avoid
4552 the C++ syntactic ambiguity. */
4553 if (d_last_char (dpi) == '>')
4554 d_append_char (dpi, ' ');
4555 d_append_char (dpi, '>');
4558 dpi->modifiers = hold_dpm;
4559 dpi->current_template = hold_current;
4564 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
4566 struct d_print_template *hold_dpt;
4567 struct demangle_component *a = d_lookup_template_argument (dpi, dc);
4569 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4570 a = d_index_template_argument (a, dpi->pack_index);
4574 d_print_error (dpi);
4578 /* While processing this parameter, we need to pop the list of
4579 templates. This is because the template parameter may
4580 itself be a reference to a parameter of an outer
4583 hold_dpt = dpi->templates;
4584 dpi->templates = hold_dpt->next;
4586 d_print_comp (dpi, options, a);
4588 dpi->templates = hold_dpt;
4593 case DEMANGLE_COMPONENT_CTOR:
4594 d_print_comp (dpi, options, dc->u.s_ctor.name);
4597 case DEMANGLE_COMPONENT_DTOR:
4598 d_append_char (dpi, '~');
4599 d_print_comp (dpi, options, dc->u.s_dtor.name);
4602 case DEMANGLE_COMPONENT_VTABLE:
4603 d_append_string (dpi, "vtable for ");
4604 d_print_comp (dpi, options, d_left (dc));
4607 case DEMANGLE_COMPONENT_VTT:
4608 d_append_string (dpi, "VTT for ");
4609 d_print_comp (dpi, options, d_left (dc));
4612 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
4613 d_append_string (dpi, "construction vtable for ");
4614 d_print_comp (dpi, options, d_left (dc));
4615 d_append_string (dpi, "-in-");
4616 d_print_comp (dpi, options, d_right (dc));
4619 case DEMANGLE_COMPONENT_TYPEINFO:
4620 d_append_string (dpi, "typeinfo for ");
4621 d_print_comp (dpi, options, d_left (dc));
4624 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
4625 d_append_string (dpi, "typeinfo name for ");
4626 d_print_comp (dpi, options, d_left (dc));
4629 case DEMANGLE_COMPONENT_TYPEINFO_FN:
4630 d_append_string (dpi, "typeinfo fn for ");
4631 d_print_comp (dpi, options, d_left (dc));
4634 case DEMANGLE_COMPONENT_THUNK:
4635 d_append_string (dpi, "non-virtual thunk to ");
4636 d_print_comp (dpi, options, d_left (dc));
4639 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
4640 d_append_string (dpi, "virtual thunk to ");
4641 d_print_comp (dpi, options, d_left (dc));
4644 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
4645 d_append_string (dpi, "covariant return thunk to ");
4646 d_print_comp (dpi, options, d_left (dc));
4649 case DEMANGLE_COMPONENT_JAVA_CLASS:
4650 d_append_string (dpi, "java Class for ");
4651 d_print_comp (dpi, options, d_left (dc));
4654 case DEMANGLE_COMPONENT_GUARD:
4655 d_append_string (dpi, "guard variable for ");
4656 d_print_comp (dpi, options, d_left (dc));
4659 case DEMANGLE_COMPONENT_TLS_INIT:
4660 d_append_string (dpi, "TLS init function for ");
4661 d_print_comp (dpi, options, d_left (dc));
4664 case DEMANGLE_COMPONENT_TLS_WRAPPER:
4665 d_append_string (dpi, "TLS wrapper function for ");
4666 d_print_comp (dpi, options, d_left (dc));
4669 case DEMANGLE_COMPONENT_REFTEMP:
4670 d_append_string (dpi, "reference temporary #");
4671 d_print_comp (dpi, options, d_right (dc));
4672 d_append_string (dpi, " for ");
4673 d_print_comp (dpi, options, d_left (dc));
4676 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
4677 d_append_string (dpi, "hidden alias for ");
4678 d_print_comp (dpi, options, d_left (dc));
4681 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
4682 d_append_string (dpi, "transaction clone for ");
4683 d_print_comp (dpi, options, d_left (dc));
4686 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
4687 d_append_string (dpi, "non-transaction clone for ");
4688 d_print_comp (dpi, options, d_left (dc));
4691 case DEMANGLE_COMPONENT_SUB_STD:
4692 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
4695 case DEMANGLE_COMPONENT_RESTRICT:
4696 case DEMANGLE_COMPONENT_VOLATILE:
4697 case DEMANGLE_COMPONENT_CONST:
4699 struct d_print_mod *pdpm;
4701 /* When printing arrays, it's possible to have cases where the
4702 same CV-qualifier gets pushed on the stack multiple times.
4703 We only need to print it once. */
4705 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
4707 if (! pdpm->printed)
4709 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
4710 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
4711 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
4713 if (pdpm->mod->type == dc->type)
4715 d_print_comp (dpi, options, d_left (dc));
4723 case DEMANGLE_COMPONENT_REFERENCE:
4724 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4726 /* Handle reference smashing: & + && = &. */
4727 const struct demangle_component *sub = d_left (dc);
4728 if (sub->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
4730 struct d_saved_scope *scope = d_get_saved_scope (dpi, sub);
4731 struct demangle_component *a;
4735 /* This is the first time SUB has been traversed.
4736 We need to capture the current templates so
4737 they can be restored if SUB is reentered as a
4739 d_save_scope (dpi, sub);
4740 if (d_print_saw_error (dpi))
4745 const struct d_component_stack *dcse;
4746 int found_self_or_parent = 0;
4748 /* This traversal is reentering SUB as a substition.
4749 If we are not beneath SUB or DC in the tree then we
4750 need to restore SUB's template stack temporarily. */
4751 for (dcse = dpi->component_stack; dcse != NULL;
4752 dcse = dcse->parent)
4756 && dcse != dpi->component_stack))
4758 found_self_or_parent = 1;
4763 if (!found_self_or_parent)
4765 saved_templates = dpi->templates;
4766 dpi->templates = scope->templates;
4767 need_template_restore = 1;
4771 a = d_lookup_template_argument (dpi, sub);
4772 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4773 a = d_index_template_argument (a, dpi->pack_index);
4777 if (need_template_restore)
4778 dpi->templates = saved_templates;
4780 d_print_error (dpi);
4787 if (sub->type == DEMANGLE_COMPONENT_REFERENCE
4788 || sub->type == dc->type)
4790 else if (sub->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE)
4791 mod_inner = d_left (sub);
4795 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4796 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4797 case DEMANGLE_COMPONENT_CONST_THIS:
4798 case DEMANGLE_COMPONENT_REFERENCE_THIS:
4799 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
4800 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4801 case DEMANGLE_COMPONENT_POINTER:
4802 case DEMANGLE_COMPONENT_COMPLEX:
4803 case DEMANGLE_COMPONENT_IMAGINARY:
4806 /* We keep a list of modifiers on the stack. */
4807 struct d_print_mod dpm;
4809 dpm.next = dpi->modifiers;
4810 dpi->modifiers = &dpm;
4813 dpm.templates = dpi->templates;
4816 mod_inner = d_left (dc);
4818 d_print_comp (dpi, options, mod_inner);
4820 /* If the modifier didn't get printed by the type, print it
4823 d_print_mod (dpi, options, dc);
4825 dpi->modifiers = dpm.next;
4827 if (need_template_restore)
4828 dpi->templates = saved_templates;
4833 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
4834 if ((options & DMGL_JAVA) == 0)
4835 d_append_buffer (dpi, dc->u.s_builtin.type->name,
4836 dc->u.s_builtin.type->len);
4838 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
4839 dc->u.s_builtin.type->java_len);
4842 case DEMANGLE_COMPONENT_VENDOR_TYPE:
4843 d_print_comp (dpi, options, d_left (dc));
4846 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
4848 if ((options & DMGL_RET_POSTFIX) != 0)
4849 d_print_function_type (dpi,
4850 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4851 dc, dpi->modifiers);
4853 /* Print return type if present */
4854 if (d_left (dc) != NULL && (options & DMGL_RET_POSTFIX) != 0)
4855 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4857 else if (d_left (dc) != NULL && (options & DMGL_RET_DROP) == 0)
4859 struct d_print_mod dpm;
4861 /* We must pass this type down as a modifier in order to
4862 print it in the right location. */
4863 dpm.next = dpi->modifiers;
4864 dpi->modifiers = &dpm;
4867 dpm.templates = dpi->templates;
4869 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4872 dpi->modifiers = dpm.next;
4877 /* In standard prefix notation, there is a space between the
4878 return type and the function signature. */
4879 if ((options & DMGL_RET_POSTFIX) == 0)
4880 d_append_char (dpi, ' ');
4883 if ((options & DMGL_RET_POSTFIX) == 0)
4884 d_print_function_type (dpi,
4885 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4886 dc, dpi->modifiers);
4891 case DEMANGLE_COMPONENT_ARRAY_TYPE:
4893 struct d_print_mod *hold_modifiers;
4894 struct d_print_mod adpm[4];
4896 struct d_print_mod *pdpm;
4898 /* We must pass this type down as a modifier in order to print
4899 multi-dimensional arrays correctly. If the array itself is
4900 CV-qualified, we act as though the element type were
4901 CV-qualified. We do this by copying the modifiers down
4902 rather than fiddling pointers, so that we don't wind up
4903 with a d_print_mod higher on the stack pointing into our
4904 stack frame after we return. */
4906 hold_modifiers = dpi->modifiers;
4908 adpm[0].next = hold_modifiers;
4909 dpi->modifiers = &adpm[0];
4911 adpm[0].printed = 0;
4912 adpm[0].templates = dpi->templates;
4915 pdpm = hold_modifiers;
4917 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
4918 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
4919 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
4921 if (! pdpm->printed)
4923 if (i >= sizeof adpm / sizeof adpm[0])
4925 d_print_error (dpi);
4930 adpm[i].next = dpi->modifiers;
4931 dpi->modifiers = &adpm[i];
4939 d_print_comp (dpi, options, d_right (dc));
4941 dpi->modifiers = hold_modifiers;
4943 if (adpm[0].printed)
4949 d_print_mod (dpi, options, adpm[i].mod);
4952 d_print_array_type (dpi, options, dc, dpi->modifiers);
4957 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4958 case DEMANGLE_COMPONENT_VECTOR_TYPE:
4960 struct d_print_mod dpm;
4962 dpm.next = dpi->modifiers;
4963 dpi->modifiers = &dpm;
4966 dpm.templates = dpi->templates;
4968 d_print_comp (dpi, options, d_right (dc));
4970 /* If the modifier didn't get printed by the type, print it
4973 d_print_mod (dpi, options, dc);
4975 dpi->modifiers = dpm.next;
4980 case DEMANGLE_COMPONENT_FIXED_TYPE:
4981 if (dc->u.s_fixed.sat)
4982 d_append_string (dpi, "_Sat ");
4983 /* Don't print "int _Accum". */
4984 if (dc->u.s_fixed.length->u.s_builtin.type
4985 != &cplus_demangle_builtin_types['i'-'a'])
4987 d_print_comp (dpi, options, dc->u.s_fixed.length);
4988 d_append_char (dpi, ' ');
4990 if (dc->u.s_fixed.accum)
4991 d_append_string (dpi, "_Accum");
4993 d_append_string (dpi, "_Fract");
4996 case DEMANGLE_COMPONENT_ARGLIST:
4997 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
4998 if (d_left (dc) != NULL)
4999 d_print_comp (dpi, options, d_left (dc));
5000 if (d_right (dc) != NULL)
5003 unsigned long int flush_count;
5004 /* Make sure ", " isn't flushed by d_append_string, otherwise
5005 dpi->len -= 2 wouldn't work. */
5006 if (dpi->len >= sizeof (dpi->buf) - 2)
5007 d_print_flush (dpi);
5008 d_append_string (dpi, ", ");
5010 flush_count = dpi->flush_count;
5011 d_print_comp (dpi, options, d_right (dc));
5012 /* If that didn't print anything (which can happen with empty
5013 template argument packs), remove the comma and space. */
5014 if (dpi->flush_count == flush_count && dpi->len == len)
5019 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
5021 struct demangle_component *type = d_left (dc);
5022 struct demangle_component *list = d_right (dc);
5025 d_print_comp (dpi, options, type);
5026 d_append_char (dpi, '{');
5027 d_print_comp (dpi, options, list);
5028 d_append_char (dpi, '}');
5032 case DEMANGLE_COMPONENT_OPERATOR:
5034 const struct demangle_operator_info *op = dc->u.s_operator.op;
5037 d_append_string (dpi, "operator");
5038 /* Add a space before new/delete. */
5039 if (IS_LOWER (op->name[0]))
5040 d_append_char (dpi, ' ');
5041 /* Omit a trailing space. */
5042 if (op->name[len-1] == ' ')
5044 d_append_buffer (dpi, op->name, len);
5048 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
5049 d_append_string (dpi, "operator ");
5050 d_print_comp (dpi, options, dc->u.s_extended_operator.name);
5053 case DEMANGLE_COMPONENT_CONVERSION:
5054 d_append_string (dpi, "operator ");
5055 d_print_conversion (dpi, options, dc);
5058 case DEMANGLE_COMPONENT_NULLARY:
5059 d_print_expr_op (dpi, options, d_left (dc));
5062 case DEMANGLE_COMPONENT_UNARY:
5064 struct demangle_component *op = d_left (dc);
5065 struct demangle_component *operand = d_right (dc);
5066 const char *code = NULL;
5068 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
5070 code = op->u.s_operator.op->code;
5071 if (!strcmp (code, "ad"))
5073 /* Don't print the argument list for the address of a
5075 if (operand->type == DEMANGLE_COMPONENT_TYPED_NAME
5076 && d_left (operand)->type == DEMANGLE_COMPONENT_QUAL_NAME
5077 && d_right (operand)->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
5078 operand = d_left (operand);
5080 if (operand->type == DEMANGLE_COMPONENT_BINARY_ARGS)
5082 /* This indicates a suffix operator. */
5083 operand = d_left (operand);
5084 d_print_subexpr (dpi, options, operand);
5085 d_print_expr_op (dpi, options, op);
5090 if (op->type != DEMANGLE_COMPONENT_CAST)
5091 d_print_expr_op (dpi, options, op);
5094 d_append_char (dpi, '(');
5095 d_print_cast (dpi, options, op);
5096 d_append_char (dpi, ')');
5098 if (code && !strcmp (code, "gs"))
5099 /* Avoid parens after '::'. */
5100 d_print_comp (dpi, options, operand);
5101 else if (code && !strcmp (code, "st"))
5102 /* Always print parens for sizeof (type). */
5104 d_append_char (dpi, '(');
5105 d_print_comp (dpi, options, operand);
5106 d_append_char (dpi, ')');
5109 d_print_subexpr (dpi, options, operand);
5113 case DEMANGLE_COMPONENT_BINARY:
5114 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
5116 d_print_error (dpi);
5120 if (op_is_new_cast (d_left (dc)))
5122 d_print_expr_op (dpi, options, d_left (dc));
5123 d_append_char (dpi, '<');
5124 d_print_comp (dpi, options, d_left (d_right (dc)));
5125 d_append_string (dpi, ">(");
5126 d_print_comp (dpi, options, d_right (d_right (dc)));
5127 d_append_char (dpi, ')');
5131 /* We wrap an expression which uses the greater-than operator in
5132 an extra layer of parens so that it does not get confused
5133 with the '>' which ends the template parameters. */
5134 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
5135 && d_left (dc)->u.s_operator.op->len == 1
5136 && d_left (dc)->u.s_operator.op->name[0] == '>')
5137 d_append_char (dpi, '(');
5139 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") == 0
5140 && d_left (d_right (dc))->type == DEMANGLE_COMPONENT_TYPED_NAME)
5142 /* Function call used in an expression should not have printed types
5143 of the function arguments. Values of the function arguments still
5144 get printed below. */
5146 const struct demangle_component *func = d_left (d_right (dc));
5148 if (d_right (func)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
5149 d_print_error (dpi);
5150 d_print_subexpr (dpi, options, d_left (func));
5153 d_print_subexpr (dpi, options, d_left (d_right (dc)));
5154 if (strcmp (d_left (dc)->u.s_operator.op->code, "ix") == 0)
5156 d_append_char (dpi, '[');
5157 d_print_comp (dpi, options, d_right (d_right (dc)));
5158 d_append_char (dpi, ']');
5162 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") != 0)
5163 d_print_expr_op (dpi, options, d_left (dc));
5164 d_print_subexpr (dpi, options, d_right (d_right (dc)));
5167 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
5168 && d_left (dc)->u.s_operator.op->len == 1
5169 && d_left (dc)->u.s_operator.op->name[0] == '>')
5170 d_append_char (dpi, ')');
5174 case DEMANGLE_COMPONENT_BINARY_ARGS:
5175 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
5176 d_print_error (dpi);
5179 case DEMANGLE_COMPONENT_TRINARY:
5180 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
5181 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
5183 d_print_error (dpi);
5187 struct demangle_component *op = d_left (dc);
5188 struct demangle_component *first = d_left (d_right (dc));
5189 struct demangle_component *second = d_left (d_right (d_right (dc)));
5190 struct demangle_component *third = d_right (d_right (d_right (dc)));
5192 if (!strcmp (op->u.s_operator.op->code, "qu"))
5194 d_print_subexpr (dpi, options, first);
5195 d_print_expr_op (dpi, options, op);
5196 d_print_subexpr (dpi, options, second);
5197 d_append_string (dpi, " : ");
5198 d_print_subexpr (dpi, options, third);
5202 d_append_string (dpi, "new ");
5203 if (d_left (first) != NULL)
5205 d_print_subexpr (dpi, options, first);
5206 d_append_char (dpi, ' ');
5208 d_print_comp (dpi, options, second);
5210 d_print_subexpr (dpi, options, third);
5215 case DEMANGLE_COMPONENT_TRINARY_ARG1:
5216 case DEMANGLE_COMPONENT_TRINARY_ARG2:
5217 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
5218 d_print_error (dpi);
5221 case DEMANGLE_COMPONENT_LITERAL:
5222 case DEMANGLE_COMPONENT_LITERAL_NEG:
5224 enum d_builtin_type_print tp;
5226 /* For some builtin types, produce simpler output. */
5227 tp = D_PRINT_DEFAULT;
5228 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
5230 tp = d_left (dc)->u.s_builtin.type->print;
5234 case D_PRINT_UNSIGNED:
5236 case D_PRINT_UNSIGNED_LONG:
5237 case D_PRINT_LONG_LONG:
5238 case D_PRINT_UNSIGNED_LONG_LONG:
5239 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
5241 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
5242 d_append_char (dpi, '-');
5243 d_print_comp (dpi, options, d_right (dc));
5248 case D_PRINT_UNSIGNED:
5249 d_append_char (dpi, 'u');
5252 d_append_char (dpi, 'l');
5254 case D_PRINT_UNSIGNED_LONG:
5255 d_append_string (dpi, "ul");
5257 case D_PRINT_LONG_LONG:
5258 d_append_string (dpi, "ll");
5260 case D_PRINT_UNSIGNED_LONG_LONG:
5261 d_append_string (dpi, "ull");
5269 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
5270 && d_right (dc)->u.s_name.len == 1
5271 && dc->type == DEMANGLE_COMPONENT_LITERAL)
5273 switch (d_right (dc)->u.s_name.s[0])
5276 d_append_string (dpi, "false");
5279 d_append_string (dpi, "true");
5292 d_append_char (dpi, '(');
5293 d_print_comp (dpi, options, d_left (dc));
5294 d_append_char (dpi, ')');
5295 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
5296 d_append_char (dpi, '-');
5297 if (tp == D_PRINT_FLOAT)
5298 d_append_char (dpi, '[');
5299 d_print_comp (dpi, options, d_right (dc));
5300 if (tp == D_PRINT_FLOAT)
5301 d_append_char (dpi, ']');
5305 case DEMANGLE_COMPONENT_NUMBER:
5306 d_append_num (dpi, dc->u.s_number.number);
5309 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
5310 d_append_string (dpi, "java resource ");
5311 d_print_comp (dpi, options, d_left (dc));
5314 case DEMANGLE_COMPONENT_COMPOUND_NAME:
5315 d_print_comp (dpi, options, d_left (dc));
5316 d_print_comp (dpi, options, d_right (dc));
5319 case DEMANGLE_COMPONENT_CHARACTER:
5320 d_append_char (dpi, dc->u.s_character.character);
5323 case DEMANGLE_COMPONENT_DECLTYPE:
5324 d_append_string (dpi, "decltype (");
5325 d_print_comp (dpi, options, d_left (dc));
5326 d_append_char (dpi, ')');
5329 case DEMANGLE_COMPONENT_PACK_EXPANSION:
5333 struct demangle_component *a = d_find_pack (dpi, d_left (dc));
5336 /* d_find_pack won't find anything if the only packs involved
5337 in this expansion are function parameter packs; in that
5338 case, just print the pattern and "...". */
5339 d_print_subexpr (dpi, options, d_left (dc));
5340 d_append_string (dpi, "...");
5344 len = d_pack_length (a);
5346 for (i = 0; i < len; ++i)
5348 dpi->pack_index = i;
5349 d_print_comp (dpi, options, dc);
5351 d_append_string (dpi, ", ");
5356 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
5358 long num = dc->u.s_number.number;
5360 d_append_string (dpi, "this");
5363 d_append_string (dpi, "{parm#");
5364 d_append_num (dpi, num);
5365 d_append_char (dpi, '}');
5370 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
5371 d_append_string (dpi, "global constructors keyed to ");
5372 d_print_comp (dpi, options, dc->u.s_binary.left);
5375 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
5376 d_append_string (dpi, "global destructors keyed to ");
5377 d_print_comp (dpi, options, dc->u.s_binary.left);
5380 case DEMANGLE_COMPONENT_LAMBDA:
5381 d_append_string (dpi, "{lambda(");
5382 d_print_comp (dpi, options, dc->u.s_unary_num.sub);
5383 d_append_string (dpi, ")#");
5384 d_append_num (dpi, dc->u.s_unary_num.num + 1);
5385 d_append_char (dpi, '}');
5388 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
5389 d_append_string (dpi, "{unnamed type#");
5390 d_append_num (dpi, dc->u.s_number.number + 1);
5391 d_append_char (dpi, '}');
5394 case DEMANGLE_COMPONENT_CLONE:
5395 d_print_comp (dpi, options, d_left (dc));
5396 d_append_string (dpi, " [clone ");
5397 d_print_comp (dpi, options, d_right (dc));
5398 d_append_char (dpi, ']');
5402 d_print_error (dpi);
5408 d_print_comp (struct d_print_info *dpi, int options,
5409 const struct demangle_component *dc)
5411 struct d_component_stack self;
5414 self.parent = dpi->component_stack;
5415 dpi->component_stack = &self;
5417 d_print_comp_inner (dpi, options, dc);
5419 dpi->component_stack = self.parent;
5422 /* Print a Java dentifier. For Java we try to handle encoded extended
5423 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
5424 so we don't it for C++. Characters are encoded as
5428 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
5434 for (p = name; p < end; ++p)
5445 for (q = p + 3; q < end; ++q)
5451 else if (*q >= 'A' && *q <= 'F')
5452 dig = *q - 'A' + 10;
5453 else if (*q >= 'a' && *q <= 'f')
5454 dig = *q - 'a' + 10;
5460 /* If the Unicode character is larger than 256, we don't try
5461 to deal with it here. FIXME. */
5462 if (q < end && *q == '_' && c < 256)
5464 d_append_char (dpi, c);
5470 d_append_char (dpi, *p);
5474 /* Print a list of modifiers. SUFFIX is 1 if we are printing
5475 qualifiers on this after printing a function. */
5478 d_print_mod_list (struct d_print_info *dpi, int options,
5479 struct d_print_mod *mods, int suffix)
5481 struct d_print_template *hold_dpt;
5483 if (mods == NULL || d_print_saw_error (dpi))
5488 && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
5489 || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
5490 || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS
5491 || mods->mod->type == DEMANGLE_COMPONENT_REFERENCE_THIS
5493 == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS))))
5495 d_print_mod_list (dpi, options, mods->next, suffix);
5501 hold_dpt = dpi->templates;
5502 dpi->templates = mods->templates;
5504 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
5506 d_print_function_type (dpi, options, mods->mod, mods->next);
5507 dpi->templates = hold_dpt;
5510 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
5512 d_print_array_type (dpi, options, mods->mod, mods->next);
5513 dpi->templates = hold_dpt;
5516 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
5518 struct d_print_mod *hold_modifiers;
5519 struct demangle_component *dc;
5521 /* When this is on the modifier stack, we have pulled any
5522 qualifiers off the right argument already. Otherwise, we
5523 print it as usual, but don't let the left argument see any
5526 hold_modifiers = dpi->modifiers;
5527 dpi->modifiers = NULL;
5528 d_print_comp (dpi, options, d_left (mods->mod));
5529 dpi->modifiers = hold_modifiers;
5531 if ((options & DMGL_JAVA) == 0)
5532 d_append_string (dpi, "::");
5534 d_append_char (dpi, '.');
5536 dc = d_right (mods->mod);
5538 if (dc->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
5540 d_append_string (dpi, "{default arg#");
5541 d_append_num (dpi, dc->u.s_unary_num.num + 1);
5542 d_append_string (dpi, "}::");
5543 dc = dc->u.s_unary_num.sub;
5546 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
5547 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
5548 || dc->type == DEMANGLE_COMPONENT_CONST_THIS
5549 || dc->type == DEMANGLE_COMPONENT_REFERENCE_THIS
5550 || dc->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS)
5553 d_print_comp (dpi, options, dc);
5555 dpi->templates = hold_dpt;
5559 d_print_mod (dpi, options, mods->mod);
5561 dpi->templates = hold_dpt;
5563 d_print_mod_list (dpi, options, mods->next, suffix);
5566 /* Print a modifier. */
5569 d_print_mod (struct d_print_info *dpi, int options,
5570 const struct demangle_component *mod)
5574 case DEMANGLE_COMPONENT_RESTRICT:
5575 case DEMANGLE_COMPONENT_RESTRICT_THIS:
5576 d_append_string (dpi, " restrict");
5578 case DEMANGLE_COMPONENT_VOLATILE:
5579 case DEMANGLE_COMPONENT_VOLATILE_THIS:
5580 d_append_string (dpi, " volatile");
5582 case DEMANGLE_COMPONENT_CONST:
5583 case DEMANGLE_COMPONENT_CONST_THIS:
5584 d_append_string (dpi, " const");
5586 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5587 d_append_char (dpi, ' ');
5588 d_print_comp (dpi, options, d_right (mod));
5590 case DEMANGLE_COMPONENT_POINTER:
5591 /* There is no pointer symbol in Java. */
5592 if ((options & DMGL_JAVA) == 0)
5593 d_append_char (dpi, '*');
5595 case DEMANGLE_COMPONENT_REFERENCE_THIS:
5596 /* For the ref-qualifier, put a space before the &. */
5597 d_append_char (dpi, ' ');
5598 case DEMANGLE_COMPONENT_REFERENCE:
5599 d_append_char (dpi, '&');
5601 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
5602 d_append_char (dpi, ' ');
5603 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
5604 d_append_string (dpi, "&&");
5606 case DEMANGLE_COMPONENT_COMPLEX:
5607 d_append_string (dpi, "complex ");
5609 case DEMANGLE_COMPONENT_IMAGINARY:
5610 d_append_string (dpi, "imaginary ");
5612 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5613 if (d_last_char (dpi) != '(')
5614 d_append_char (dpi, ' ');
5615 d_print_comp (dpi, options, d_left (mod));
5616 d_append_string (dpi, "::*");
5618 case DEMANGLE_COMPONENT_TYPED_NAME:
5619 d_print_comp (dpi, options, d_left (mod));
5621 case DEMANGLE_COMPONENT_VECTOR_TYPE:
5622 d_append_string (dpi, " __vector(");
5623 d_print_comp (dpi, options, d_left (mod));
5624 d_append_char (dpi, ')');
5628 /* Otherwise, we have something that won't go back on the
5629 modifier stack, so we can just print it. */
5630 d_print_comp (dpi, options, mod);
5635 /* Print a function type, except for the return type. */
5638 d_print_function_type (struct d_print_info *dpi, int options,
5639 const struct demangle_component *dc,
5640 struct d_print_mod *mods)
5644 struct d_print_mod *p;
5645 struct d_print_mod *hold_modifiers;
5649 for (p = mods; p != NULL; p = p->next)
5654 switch (p->mod->type)
5656 case DEMANGLE_COMPONENT_POINTER:
5657 case DEMANGLE_COMPONENT_REFERENCE:
5658 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
5661 case DEMANGLE_COMPONENT_RESTRICT:
5662 case DEMANGLE_COMPONENT_VOLATILE:
5663 case DEMANGLE_COMPONENT_CONST:
5664 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5665 case DEMANGLE_COMPONENT_COMPLEX:
5666 case DEMANGLE_COMPONENT_IMAGINARY:
5667 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5671 case DEMANGLE_COMPONENT_RESTRICT_THIS:
5672 case DEMANGLE_COMPONENT_VOLATILE_THIS:
5673 case DEMANGLE_COMPONENT_CONST_THIS:
5674 case DEMANGLE_COMPONENT_REFERENCE_THIS:
5675 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
5688 if (d_last_char (dpi) != '('
5689 && d_last_char (dpi) != '*')
5692 if (need_space && d_last_char (dpi) != ' ')
5693 d_append_char (dpi, ' ');
5694 d_append_char (dpi, '(');
5697 hold_modifiers = dpi->modifiers;
5698 dpi->modifiers = NULL;
5700 d_print_mod_list (dpi, options, mods, 0);
5703 d_append_char (dpi, ')');
5705 d_append_char (dpi, '(');
5707 if (d_right (dc) != NULL)
5708 d_print_comp (dpi, options, d_right (dc));
5710 d_append_char (dpi, ')');
5712 d_print_mod_list (dpi, options, mods, 1);
5714 dpi->modifiers = hold_modifiers;
5717 /* Print an array type, except for the element type. */
5720 d_print_array_type (struct d_print_info *dpi, int options,
5721 const struct demangle_component *dc,
5722 struct d_print_mod *mods)
5730 struct d_print_mod *p;
5733 for (p = mods; p != NULL; p = p->next)
5737 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
5752 d_append_string (dpi, " (");
5754 d_print_mod_list (dpi, options, mods, 0);
5757 d_append_char (dpi, ')');
5761 d_append_char (dpi, ' ');
5763 d_append_char (dpi, '[');
5765 if (d_left (dc) != NULL)
5766 d_print_comp (dpi, options, d_left (dc));
5768 d_append_char (dpi, ']');
5771 /* Print an operator in an expression. */
5774 d_print_expr_op (struct d_print_info *dpi, int options,
5775 const struct demangle_component *dc)
5777 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
5778 d_append_buffer (dpi, dc->u.s_operator.op->name,
5779 dc->u.s_operator.op->len);
5781 d_print_comp (dpi, options, dc);
5787 d_print_cast (struct d_print_info *dpi, int options,
5788 const struct demangle_component *dc)
5790 d_print_comp (dpi, options, d_left (dc));
5793 /* Print a conversion operator. */
5796 d_print_conversion (struct d_print_info *dpi, int options,
5797 const struct demangle_component *dc)
5799 struct d_print_template dpt;
5801 /* For a conversion operator, we need the template parameters from
5802 the enclosing template in scope for processing the type. */
5803 if (dpi->current_template != NULL)
5805 dpt.next = dpi->templates;
5806 dpi->templates = &dpt;
5807 dpt.template_decl = dpi->current_template;
5810 if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
5812 d_print_comp (dpi, options, d_left (dc));
5813 if (dpi->current_template != NULL)
5814 dpi->templates = dpt.next;
5818 d_print_comp (dpi, options, d_left (d_left (dc)));
5820 /* For a templated cast operator, we need to remove the template
5821 parameters from scope after printing the operator name,
5822 so we need to handle the template printing here. */
5823 if (dpi->current_template != NULL)
5824 dpi->templates = dpt.next;
5826 if (d_last_char (dpi) == '<')
5827 d_append_char (dpi, ' ');
5828 d_append_char (dpi, '<');
5829 d_print_comp (dpi, options, d_right (d_left (dc)));
5830 /* Avoid generating two consecutive '>' characters, to avoid
5831 the C++ syntactic ambiguity. */
5832 if (d_last_char (dpi) == '>')
5833 d_append_char (dpi, ' ');
5834 d_append_char (dpi, '>');
5838 /* Initialize the information structure we use to pass around
5841 CP_STATIC_IF_GLIBCPP_V3
5843 cplus_demangle_init_info (const char *mangled, int options, size_t len,
5847 di->send = mangled + len;
5848 di->options = options;
5852 /* We can not need more components than twice the number of chars in
5853 the mangled string. Most components correspond directly to
5854 chars, but the ARGLIST types are exceptions. */
5855 di->num_comps = 2 * len;
5858 /* Similarly, we can not need more substitutions than there are
5859 chars in the mangled string. */
5864 di->last_name = NULL;
5867 di->is_expression = 0;
5868 di->is_conversion = 0;
5871 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
5872 mangled name, return strings in repeated callback giving the demangled
5873 name. OPTIONS is the usual libiberty demangler options. On success,
5874 this returns 1. On failure, returns 0. */
5877 d_demangle_callback (const char *mangled, int options,
5878 demangle_callbackref callback, void *opaque)
5889 struct demangle_component *dc;
5892 if (mangled[0] == '_' && mangled[1] == 'Z')
5894 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
5895 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
5896 && (mangled[9] == 'D' || mangled[9] == 'I')
5897 && mangled[10] == '_')
5898 type = mangled[9] == 'I' ? DCT_GLOBAL_CTORS : DCT_GLOBAL_DTORS;
5901 if ((options & DMGL_TYPES) == 0)
5906 cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
5909 #ifdef CP_DYNAMIC_ARRAYS
5910 __extension__ struct demangle_component comps[di.num_comps];
5911 __extension__ struct demangle_component *subs[di.num_subs];
5916 di.comps = alloca (di.num_comps * sizeof (*di.comps));
5917 di.subs = alloca (di.num_subs * sizeof (*di.subs));
5923 dc = cplus_demangle_type (&di);
5926 dc = cplus_demangle_mangled_name (&di, 1);
5928 case DCT_GLOBAL_CTORS:
5929 case DCT_GLOBAL_DTORS:
5930 d_advance (&di, 11);
5931 dc = d_make_comp (&di,
5932 (type == DCT_GLOBAL_CTORS
5933 ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
5934 : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS),
5935 d_make_demangle_mangled_name (&di, d_str (&di)),
5937 d_advance (&di, strlen (d_str (&di)));
5940 abort (); /* We have listed all the cases. */
5943 /* If DMGL_PARAMS is set, then if we didn't consume the entire
5944 mangled string, then we didn't successfully demangle it. If
5945 DMGL_PARAMS is not set, we didn't look at the trailing
5947 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
5950 #ifdef CP_DEMANGLE_DEBUG
5954 status = (dc != NULL)
5955 ? cplus_demangle_print_callback (options, dc, callback, opaque)
5962 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
5963 name, return a buffer allocated with malloc holding the demangled
5964 name. OPTIONS is the usual libiberty demangler options. On
5965 success, this sets *PALC to the allocated size of the returned
5966 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
5967 a memory allocation failure, and returns NULL. */
5970 d_demangle (const char *mangled, int options, size_t *palc)
5972 struct d_growable_string dgs;
5975 d_growable_string_init (&dgs, 0);
5977 status = d_demangle_callback (mangled, options,
5978 d_growable_string_callback_adapter, &dgs);
5986 *palc = dgs.allocation_failure ? 1 : dgs.alc;
5990 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
5992 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
5994 /* ia64 ABI-mandated entry point in the C++ runtime library for
5995 performing demangling. MANGLED_NAME is a NUL-terminated character
5996 string containing the name to be demangled.
5998 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
5999 *LENGTH bytes, into which the demangled name is stored. If
6000 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
6001 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
6002 is placed in a region of memory allocated with malloc.
6004 If LENGTH is non-NULL, the length of the buffer containing the
6005 demangled name, is placed in *LENGTH.
6007 The return value is a pointer to the start of the NUL-terminated
6008 demangled name, or NULL if the demangling fails. The caller is
6009 responsible for deallocating this memory using free.
6011 *STATUS is set to one of the following values:
6012 0: The demangling operation succeeded.
6013 -1: A memory allocation failure occurred.
6014 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
6015 -3: One of the arguments is invalid.
6017 The demangling is performed using the C++ ABI mangling rules, with
6021 __cxa_demangle (const char *mangled_name, char *output_buffer,
6022 size_t *length, int *status)
6027 if (mangled_name == NULL)
6034 if (output_buffer != NULL && length == NULL)
6041 demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
6043 if (demangled == NULL)
6055 if (output_buffer == NULL)
6062 if (strlen (demangled) < *length)
6064 strcpy (output_buffer, demangled);
6066 demangled = output_buffer;
6070 free (output_buffer);
6081 extern int __gcclibcxx_demangle_callback (const char *,
6083 (const char *, size_t, void *),
6086 /* Alternative, allocationless entry point in the C++ runtime library
6087 for performing demangling. MANGLED_NAME is a NUL-terminated character
6088 string containing the name to be demangled.
6090 CALLBACK is a callback function, called with demangled string
6091 segments as demangling progresses; it is called at least once,
6092 but may be called more than once. OPAQUE is a generalized pointer
6093 used as a callback argument.
6095 The return code is one of the following values, equivalent to
6096 the STATUS values of __cxa_demangle() (excluding -1, since this
6097 function performs no memory allocations):
6098 0: The demangling operation succeeded.
6099 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
6100 -3: One of the arguments is invalid.
6102 The demangling is performed using the C++ ABI mangling rules, with
6106 __gcclibcxx_demangle_callback (const char *mangled_name,
6107 void (*callback) (const char *, size_t, void *),
6112 if (mangled_name == NULL || callback == NULL)
6115 status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES,
6123 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
6125 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
6126 mangled name, return a buffer allocated with malloc holding the
6127 demangled name. Otherwise, return NULL. */
6130 cplus_demangle_v3 (const char *mangled, int options)
6134 return d_demangle (mangled, options, &alc);
6138 cplus_demangle_v3_callback (const char *mangled, int options,
6139 demangle_callbackref callback, void *opaque)
6141 return d_demangle_callback (mangled, options, callback, opaque);
6144 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
6145 conventions, but the output formatting is a little different.
6146 This instructs the C++ demangler not to emit pointer characters ("*"), to
6147 use Java's namespace separator symbol ("." instead of "::"), and to output
6148 JArray<TYPE> as TYPE[]. */
6151 java_demangle_v3 (const char *mangled)
6155 return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
6159 java_demangle_v3_callback (const char *mangled,
6160 demangle_callbackref callback, void *opaque)
6162 return d_demangle_callback (mangled,
6163 DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
6167 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
6169 #ifndef IN_GLIBCPP_V3
6171 /* Demangle a string in order to find out whether it is a constructor
6172 or destructor. Return non-zero on success. Set *CTOR_KIND and
6173 *DTOR_KIND appropriately. */
6176 is_ctor_or_dtor (const char *mangled,
6177 enum gnu_v3_ctor_kinds *ctor_kind,
6178 enum gnu_v3_dtor_kinds *dtor_kind)
6181 struct demangle_component *dc;
6184 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
6185 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
6187 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
6190 #ifdef CP_DYNAMIC_ARRAYS
6191 __extension__ struct demangle_component comps[di.num_comps];
6192 __extension__ struct demangle_component *subs[di.num_subs];
6197 di.comps = alloca (di.num_comps * sizeof (*di.comps));
6198 di.subs = alloca (di.num_subs * sizeof (*di.subs));
6201 dc = cplus_demangle_mangled_name (&di, 1);
6203 /* Note that because we did not pass DMGL_PARAMS, we don't expect
6204 to demangle the entire string. */
6211 /* These cannot appear on a constructor or destructor. */
6212 case DEMANGLE_COMPONENT_RESTRICT_THIS:
6213 case DEMANGLE_COMPONENT_VOLATILE_THIS:
6214 case DEMANGLE_COMPONENT_CONST_THIS:
6215 case DEMANGLE_COMPONENT_REFERENCE_THIS:
6216 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
6220 case DEMANGLE_COMPONENT_TYPED_NAME:
6221 case DEMANGLE_COMPONENT_TEMPLATE:
6224 case DEMANGLE_COMPONENT_QUAL_NAME:
6225 case DEMANGLE_COMPONENT_LOCAL_NAME:
6228 case DEMANGLE_COMPONENT_CTOR:
6229 *ctor_kind = dc->u.s_ctor.kind;
6233 case DEMANGLE_COMPONENT_DTOR:
6234 *dtor_kind = dc->u.s_dtor.kind;
6245 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
6246 name. A non-zero return indicates the type of constructor. */
6248 enum gnu_v3_ctor_kinds
6249 is_gnu_v3_mangled_ctor (const char *name)
6251 enum gnu_v3_ctor_kinds ctor_kind;
6252 enum gnu_v3_dtor_kinds dtor_kind;
6254 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
6255 return (enum gnu_v3_ctor_kinds) 0;
6260 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
6261 name. A non-zero return indicates the type of destructor. */
6263 enum gnu_v3_dtor_kinds
6264 is_gnu_v3_mangled_dtor (const char *name)
6266 enum gnu_v3_ctor_kinds ctor_kind;
6267 enum gnu_v3_dtor_kinds dtor_kind;
6269 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
6270 return (enum gnu_v3_dtor_kinds) 0;
6274 #endif /* IN_GLIBCPP_V3 */
6276 #ifdef STANDALONE_DEMANGLER
6279 #include "dyn-string.h"
6281 static void print_usage (FILE* fp, int exit_value);
6283 #define IS_ALPHA(CHAR) \
6284 (((CHAR) >= 'a' && (CHAR) <= 'z') \
6285 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
6287 /* Non-zero if CHAR is a character than can occur in a mangled name. */
6288 #define is_mangled_char(CHAR) \
6289 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
6290 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
6292 /* The name of this program, as invoked. */
6293 const char* program_name;
6295 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
6298 print_usage (FILE* fp, int exit_value)
6300 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
6301 fprintf (fp, "Options:\n");
6302 fprintf (fp, " -h,--help Display this message.\n");
6303 fprintf (fp, " -p,--no-params Don't display function parameters\n");
6304 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
6305 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
6310 /* Option specification for getopt_long. */
6311 static const struct option long_options[] =
6313 { "help", no_argument, NULL, 'h' },
6314 { "no-params", no_argument, NULL, 'p' },
6315 { "verbose", no_argument, NULL, 'v' },
6316 { NULL, no_argument, NULL, 0 },
6319 /* Main entry for a demangling filter executable. It will demangle
6320 its command line arguments, if any. If none are provided, it will
6321 filter stdin to stdout, replacing any recognized mangled C++ names
6322 with their demangled equivalents. */
6325 main (int argc, char *argv[])
6329 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
6331 /* Use the program name of this program, as invoked. */
6332 program_name = argv[0];
6334 /* Parse options. */
6337 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
6340 case '?': /* Unrecognized option. */
6341 print_usage (stderr, 1);
6345 print_usage (stdout, 0);
6349 options &= ~ DMGL_PARAMS;
6353 options |= DMGL_VERBOSE;
6357 while (opt_char != -1);
6360 /* No command line arguments were provided. Filter stdin. */
6362 dyn_string_t mangled = dyn_string_new (3);
6365 /* Read all of input. */
6366 while (!feof (stdin))
6370 /* Pile characters into mangled until we hit one that can't
6371 occur in a mangled name. */
6373 while (!feof (stdin) && is_mangled_char (c))
6375 dyn_string_append_char (mangled, c);
6381 if (dyn_string_length (mangled) > 0)
6383 #ifdef IN_GLIBCPP_V3
6384 s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
6386 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
6396 /* It might not have been a mangled name. Print the
6398 fputs (dyn_string_buf (mangled), stdout);
6401 dyn_string_clear (mangled);
6404 /* If we haven't hit EOF yet, we've read one character that
6405 can't occur in a mangled name, so print it out. */
6410 dyn_string_delete (mangled);
6413 /* Demangle command line arguments. */
6415 /* Loop over command line arguments. */
6416 for (i = optind; i < argc; ++i)
6419 #ifdef IN_GLIBCPP_V3
6423 /* Attempt to demangle. */
6424 #ifdef IN_GLIBCPP_V3
6425 s = __cxa_demangle (argv[i], NULL, NULL, &status);
6427 s = cplus_demangle_v3 (argv[i], options);
6430 /* If it worked, print the demangled name. */
6438 #ifdef IN_GLIBCPP_V3
6439 fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
6441 fprintf (stderr, "Failed: %s\n", argv[i]);
6450 #endif /* STANDALONE_DEMANGLER */