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, additional sanity checks will be performed. It will
99 cause some slowdown, but will allow to catch out-of-bound access
100 errors earlier. This macro is intended for testing and debugging. */
102 #if defined (_AIX) && !defined (__GNUC__)
124 # define alloca __builtin_alloca
126 extern char *alloca ();
127 # endif /* __GNUC__ */
129 #endif /* HAVE_ALLOCA_H */
135 # define INT_MAX (int)(((unsigned int) ~0) >> 1) /* 0x7FFFFFFF */
138 #include "ansidecl.h"
139 #include "libiberty.h"
140 #include "demangle.h"
141 #include "cp-demangle.h"
143 /* If IN_GLIBCPP_V3 is defined, some functions are made static. We
144 also rename them via #define to avoid compiler errors when the
145 static definition conflicts with the extern declaration in a header
149 #define CP_STATIC_IF_GLIBCPP_V3 static
151 #define cplus_demangle_fill_name d_fill_name
152 static int d_fill_name (struct demangle_component *, const char *, int);
154 #define cplus_demangle_fill_extended_operator d_fill_extended_operator
156 d_fill_extended_operator (struct demangle_component *, int,
157 struct demangle_component *);
159 #define cplus_demangle_fill_ctor d_fill_ctor
161 d_fill_ctor (struct demangle_component *, enum gnu_v3_ctor_kinds,
162 struct demangle_component *);
164 #define cplus_demangle_fill_dtor d_fill_dtor
166 d_fill_dtor (struct demangle_component *, enum gnu_v3_dtor_kinds,
167 struct demangle_component *);
169 #define cplus_demangle_mangled_name d_mangled_name
170 static struct demangle_component *d_mangled_name (struct d_info *, int);
172 #define cplus_demangle_type d_type
173 static struct demangle_component *d_type (struct d_info *);
175 #define cplus_demangle_print d_print
176 static char *d_print (int, const struct demangle_component *, int, size_t *);
178 #define cplus_demangle_print_callback d_print_callback
179 static int d_print_callback (int, const struct demangle_component *,
180 demangle_callbackref, void *);
182 #define cplus_demangle_init_info d_init_info
183 static void d_init_info (const char *, int, size_t, struct d_info *);
185 #else /* ! defined(IN_GLIBCPP_V3) */
186 #define CP_STATIC_IF_GLIBCPP_V3
187 #endif /* ! defined(IN_GLIBCPP_V3) */
189 /* See if the compiler supports dynamic arrays. */
192 #define CP_DYNAMIC_ARRAYS
195 #ifdef __STDC_VERSION__
196 #if __STDC_VERSION__ >= 199901L
197 #define CP_DYNAMIC_ARRAYS
198 #endif /* __STDC__VERSION >= 199901L */
199 #endif /* defined (__STDC_VERSION__) */
200 #endif /* defined (__STDC__) */
201 #endif /* ! defined (__GNUC__) */
203 /* We avoid pulling in the ctype tables, to prevent pulling in
204 additional unresolved symbols when this code is used in a library.
205 FIXME: Is this really a valid reason? This comes from the original
208 As of this writing this file has the following undefined references
209 when compiled with -DIN_GLIBCPP_V3: realloc, free, memcpy, strcpy,
212 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
213 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
214 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
216 /* The prefix prepended by GCC to an identifier represnting the
217 anonymous namespace. */
218 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
219 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
220 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
222 /* Information we keep for the standard substitutions. */
224 struct d_standard_sub_info
226 /* The code for this substitution. */
228 /* The simple string it expands to. */
229 const char *simple_expansion;
230 /* The length of the simple expansion. */
232 /* The results of a full, verbose, expansion. This is used when
233 qualifying a constructor/destructor, or when in verbose mode. */
234 const char *full_expansion;
235 /* The length of the full expansion. */
237 /* What to set the last_name field of d_info to; NULL if we should
238 not set it. This is only relevant when qualifying a
239 constructor/destructor. */
240 const char *set_last_name;
241 /* The length of set_last_name. */
242 int set_last_name_len;
245 /* Accessors for subtrees of struct demangle_component. */
247 #define d_left(dc) ((dc)->u.s_binary.left)
248 #define d_right(dc) ((dc)->u.s_binary.right)
250 /* A list of templates. This is used while printing. */
252 struct d_print_template
254 /* Next template on the list. */
255 struct d_print_template *next;
257 const struct demangle_component *template_decl;
260 /* A list of type modifiers. This is used while printing. */
264 /* Next modifier on the list. These are in the reverse of the order
265 in which they appeared in the mangled string. */
266 struct d_print_mod *next;
268 const struct demangle_component *mod;
269 /* Whether this modifier was printed. */
271 /* The list of templates which applies to this modifier. */
272 struct d_print_template *templates;
275 /* We use these structures to hold information during printing. */
277 struct d_growable_string
279 /* Buffer holding the result. */
281 /* Current length of data in buffer. */
283 /* Allocated size of buffer. */
285 /* Set to 1 if we had a memory allocation failure. */
286 int allocation_failure;
289 /* Stack of components, innermost first, used to avoid loops. */
291 struct d_component_stack
293 /* This component. */
294 const struct demangle_component *dc;
295 /* This component's parent. */
296 const struct d_component_stack *parent;
299 /* A demangle component and some scope captured when it was first
304 /* The component whose scope this is. */
305 const struct demangle_component *container;
306 /* The list of templates, if any, that was current when this
307 scope was captured. */
308 struct d_print_template *templates;
311 /* Checkpoint structure to allow backtracking. This holds copies
312 of the fields of struct d_info that need to be restored
313 if a trial parse needs to be backtracked over. */
315 struct d_info_checkpoint
324 enum { D_PRINT_BUFFER_LENGTH = 256 };
327 /* Fixed-length allocated buffer for demangled data, flushed to the
328 callback with a NUL termination once full. */
329 char buf[D_PRINT_BUFFER_LENGTH];
330 /* Current length of data in buffer. */
332 /* The last character printed, saved individually so that it survives
335 /* Callback function to handle demangled buffer flush. */
336 demangle_callbackref callback;
337 /* Opaque callback argument. */
339 /* The current list of templates, if any. */
340 struct d_print_template *templates;
341 /* The current list of modifiers (e.g., pointer, reference, etc.),
343 struct d_print_mod *modifiers;
344 /* Set to 1 if we saw a demangling error. */
345 int demangle_failure;
346 /* Non-zero if we're printing a lambda argument. A template
347 parameter reference actually means 'auto'. */
349 /* The current index into any template argument packs we are using
350 for printing, or -1 to print the whole pack. */
352 /* Number of d_print_flush calls so far. */
353 unsigned long int flush_count;
354 /* Stack of components, innermost first, used to avoid loops. */
355 const struct d_component_stack *component_stack;
356 /* Array of saved scopes for evaluating substitutions. */
357 struct d_saved_scope *saved_scopes;
358 /* Index of the next unused saved scope in the above array. */
359 int next_saved_scope;
360 /* Number of saved scopes in the above array. */
361 int num_saved_scopes;
362 /* Array of templates for saving into scopes. */
363 struct d_print_template *copy_templates;
364 /* Index of the next unused copy template in the above array. */
365 int next_copy_template;
366 /* Number of copy templates in the above array. */
367 int num_copy_templates;
368 /* The nearest enclosing template, if any. */
369 const struct demangle_component *current_template;
372 #ifdef CP_DEMANGLE_DEBUG
373 static void d_dump (struct demangle_component *, int);
376 static struct demangle_component *
377 d_make_empty (struct d_info *);
379 static struct demangle_component *
380 d_make_comp (struct d_info *, enum demangle_component_type,
381 struct demangle_component *,
382 struct demangle_component *);
384 static struct demangle_component *
385 d_make_name (struct d_info *, const char *, int);
387 static struct demangle_component *
388 d_make_demangle_mangled_name (struct d_info *, const char *);
390 static struct demangle_component *
391 d_make_builtin_type (struct d_info *,
392 const struct demangle_builtin_type_info *);
394 static struct demangle_component *
395 d_make_operator (struct d_info *,
396 const struct demangle_operator_info *);
398 static struct demangle_component *
399 d_make_extended_operator (struct d_info *, int,
400 struct demangle_component *);
402 static struct demangle_component *
403 d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
404 struct demangle_component *);
406 static struct demangle_component *
407 d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
408 struct demangle_component *);
410 static struct demangle_component *
411 d_make_template_param (struct d_info *, int);
413 static struct demangle_component *
414 d_make_sub (struct d_info *, const char *, int);
417 has_return_type (struct demangle_component *);
420 is_ctor_dtor_or_conversion (struct demangle_component *);
422 static struct demangle_component *d_encoding (struct d_info *, int);
424 static struct demangle_component *d_name (struct d_info *);
426 static struct demangle_component *d_nested_name (struct d_info *);
428 static struct demangle_component *d_prefix (struct d_info *);
430 static struct demangle_component *d_unqualified_name (struct d_info *);
432 static struct demangle_component *d_source_name (struct d_info *);
434 static int d_number (struct d_info *);
436 static struct demangle_component *d_identifier (struct d_info *, int);
438 static struct demangle_component *d_operator_name (struct d_info *);
440 static struct demangle_component *d_special_name (struct d_info *);
442 static struct demangle_component *d_parmlist (struct d_info *);
444 static int d_call_offset (struct d_info *, int);
446 static struct demangle_component *d_ctor_dtor_name (struct d_info *);
448 static struct demangle_component **
449 d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
451 static struct demangle_component *
452 d_ref_qualifier (struct d_info *, struct demangle_component *);
454 static struct demangle_component *
455 d_function_type (struct d_info *);
457 static struct demangle_component *
458 d_bare_function_type (struct d_info *, int);
460 static struct demangle_component *
461 d_class_enum_type (struct d_info *);
463 static struct demangle_component *d_array_type (struct d_info *);
465 static struct demangle_component *d_vector_type (struct d_info *);
467 static struct demangle_component *
468 d_pointer_to_member_type (struct d_info *);
470 static struct demangle_component *
471 d_template_param (struct d_info *);
473 static struct demangle_component *d_template_args (struct d_info *);
474 static struct demangle_component *d_template_args_1 (struct d_info *);
476 static struct demangle_component *
477 d_template_arg (struct d_info *);
479 static struct demangle_component *d_expression (struct d_info *);
481 static struct demangle_component *d_expr_primary (struct d_info *);
483 static struct demangle_component *d_local_name (struct d_info *);
485 static int d_discriminator (struct d_info *);
487 static struct demangle_component *d_lambda (struct d_info *);
489 static struct demangle_component *d_unnamed_type (struct d_info *);
491 static struct demangle_component *
492 d_clone_suffix (struct d_info *, struct demangle_component *);
495 d_add_substitution (struct d_info *, struct demangle_component *);
497 static struct demangle_component *d_substitution (struct d_info *, int);
499 static void d_checkpoint (struct d_info *, struct d_info_checkpoint *);
501 static void d_backtrack (struct d_info *, struct d_info_checkpoint *);
503 static void d_growable_string_init (struct d_growable_string *, size_t);
506 d_growable_string_resize (struct d_growable_string *, size_t);
509 d_growable_string_append_buffer (struct d_growable_string *,
510 const char *, size_t);
512 d_growable_string_callback_adapter (const char *, size_t, void *);
515 d_print_init (struct d_print_info *, demangle_callbackref, void *,
516 const struct demangle_component *);
518 static inline void d_print_error (struct d_print_info *);
520 static inline int d_print_saw_error (struct d_print_info *);
522 static inline void d_print_flush (struct d_print_info *);
524 static inline void d_append_char (struct d_print_info *, char);
526 static inline void d_append_buffer (struct d_print_info *,
527 const char *, size_t);
529 static inline void d_append_string (struct d_print_info *, const char *);
531 static inline char d_last_char (struct d_print_info *);
534 d_print_comp (struct d_print_info *, int, const struct demangle_component *);
537 d_print_java_identifier (struct d_print_info *, const char *, int);
540 d_print_mod_list (struct d_print_info *, int, struct d_print_mod *, int);
543 d_print_mod (struct d_print_info *, int, const struct demangle_component *);
546 d_print_function_type (struct d_print_info *, int,
547 const struct demangle_component *,
548 struct d_print_mod *);
551 d_print_array_type (struct d_print_info *, int,
552 const struct demangle_component *,
553 struct d_print_mod *);
556 d_print_expr_op (struct d_print_info *, int, const struct demangle_component *);
558 static void d_print_cast (struct d_print_info *, int,
559 const struct demangle_component *);
560 static void d_print_conversion (struct d_print_info *, int,
561 const struct demangle_component *);
563 static int d_demangle_callback (const char *, int,
564 demangle_callbackref, void *);
565 static char *d_demangle (const char *, int, size_t *);
567 /* True iff TYPE is a demangling component representing a
568 function-type-qualifier. */
571 is_fnqual_component_type (enum demangle_component_type type)
573 return (type == DEMANGLE_COMPONENT_RESTRICT_THIS
574 || type == DEMANGLE_COMPONENT_VOLATILE_THIS
575 || type == DEMANGLE_COMPONENT_CONST_THIS
576 || type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
577 || type == DEMANGLE_COMPONENT_TRANSACTION_SAFE
578 || type == DEMANGLE_COMPONENT_NOEXCEPT
579 || type == DEMANGLE_COMPONENT_THROW_SPEC
580 || type == DEMANGLE_COMPONENT_REFERENCE_THIS);
583 #define FNQUAL_COMPONENT_CASE \
584 case DEMANGLE_COMPONENT_RESTRICT_THIS: \
585 case DEMANGLE_COMPONENT_VOLATILE_THIS: \
586 case DEMANGLE_COMPONENT_CONST_THIS: \
587 case DEMANGLE_COMPONENT_REFERENCE_THIS: \
588 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS: \
589 case DEMANGLE_COMPONENT_TRANSACTION_SAFE: \
590 case DEMANGLE_COMPONENT_NOEXCEPT: \
591 case DEMANGLE_COMPONENT_THROW_SPEC
593 #ifdef CP_DEMANGLE_DEBUG
596 d_dump (struct demangle_component *dc, int indent)
603 printf ("failed demangling\n");
607 for (i = 0; i < indent; ++i)
612 case DEMANGLE_COMPONENT_NAME:
613 printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
615 case DEMANGLE_COMPONENT_TAGGED_NAME:
616 printf ("tagged name\n");
617 d_dump (dc->u.s_binary.left, indent + 2);
618 d_dump (dc->u.s_binary.right, indent + 2);
620 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
621 printf ("template parameter %ld\n", dc->u.s_number.number);
623 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
624 printf ("function parameter %ld\n", dc->u.s_number.number);
626 case DEMANGLE_COMPONENT_CTOR:
627 printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
628 d_dump (dc->u.s_ctor.name, indent + 2);
630 case DEMANGLE_COMPONENT_DTOR:
631 printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
632 d_dump (dc->u.s_dtor.name, indent + 2);
634 case DEMANGLE_COMPONENT_SUB_STD:
635 printf ("standard substitution %s\n", dc->u.s_string.string);
637 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
638 printf ("builtin type %s\n", dc->u.s_builtin.type->name);
640 case DEMANGLE_COMPONENT_OPERATOR:
641 printf ("operator %s\n", dc->u.s_operator.op->name);
643 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
644 printf ("extended operator with %d args\n",
645 dc->u.s_extended_operator.args);
646 d_dump (dc->u.s_extended_operator.name, indent + 2);
649 case DEMANGLE_COMPONENT_QUAL_NAME:
650 printf ("qualified name\n");
652 case DEMANGLE_COMPONENT_LOCAL_NAME:
653 printf ("local name\n");
655 case DEMANGLE_COMPONENT_TYPED_NAME:
656 printf ("typed name\n");
658 case DEMANGLE_COMPONENT_TEMPLATE:
659 printf ("template\n");
661 case DEMANGLE_COMPONENT_VTABLE:
664 case DEMANGLE_COMPONENT_VTT:
667 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
668 printf ("construction vtable\n");
670 case DEMANGLE_COMPONENT_TYPEINFO:
671 printf ("typeinfo\n");
673 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
674 printf ("typeinfo name\n");
676 case DEMANGLE_COMPONENT_TYPEINFO_FN:
677 printf ("typeinfo function\n");
679 case DEMANGLE_COMPONENT_THUNK:
682 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
683 printf ("virtual thunk\n");
685 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
686 printf ("covariant thunk\n");
688 case DEMANGLE_COMPONENT_JAVA_CLASS:
689 printf ("java class\n");
691 case DEMANGLE_COMPONENT_GUARD:
694 case DEMANGLE_COMPONENT_REFTEMP:
695 printf ("reference temporary\n");
697 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
698 printf ("hidden alias\n");
700 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
701 printf ("transaction clone\n");
703 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
704 printf ("non-transaction clone\n");
706 case DEMANGLE_COMPONENT_RESTRICT:
707 printf ("restrict\n");
709 case DEMANGLE_COMPONENT_VOLATILE:
710 printf ("volatile\n");
712 case DEMANGLE_COMPONENT_CONST:
715 case DEMANGLE_COMPONENT_RESTRICT_THIS:
716 printf ("restrict this\n");
718 case DEMANGLE_COMPONENT_VOLATILE_THIS:
719 printf ("volatile this\n");
721 case DEMANGLE_COMPONENT_CONST_THIS:
722 printf ("const this\n");
724 case DEMANGLE_COMPONENT_REFERENCE_THIS:
725 printf ("reference this\n");
727 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
728 printf ("rvalue reference this\n");
730 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
731 printf ("transaction_safe this\n");
733 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
734 printf ("vendor type qualifier\n");
736 case DEMANGLE_COMPONENT_POINTER:
737 printf ("pointer\n");
739 case DEMANGLE_COMPONENT_REFERENCE:
740 printf ("reference\n");
742 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
743 printf ("rvalue reference\n");
745 case DEMANGLE_COMPONENT_COMPLEX:
746 printf ("complex\n");
748 case DEMANGLE_COMPONENT_IMAGINARY:
749 printf ("imaginary\n");
751 case DEMANGLE_COMPONENT_VENDOR_TYPE:
752 printf ("vendor type\n");
754 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
755 printf ("function type\n");
757 case DEMANGLE_COMPONENT_ARRAY_TYPE:
758 printf ("array type\n");
760 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
761 printf ("pointer to member type\n");
763 case DEMANGLE_COMPONENT_FIXED_TYPE:
764 printf ("fixed-point type, accum? %d, sat? %d\n",
765 dc->u.s_fixed.accum, dc->u.s_fixed.sat);
766 d_dump (dc->u.s_fixed.length, indent + 2);
768 case DEMANGLE_COMPONENT_ARGLIST:
769 printf ("argument list\n");
771 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
772 printf ("template argument list\n");
774 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
775 printf ("initializer list\n");
777 case DEMANGLE_COMPONENT_CAST:
780 case DEMANGLE_COMPONENT_CONVERSION:
781 printf ("conversion operator\n");
783 case DEMANGLE_COMPONENT_NULLARY:
784 printf ("nullary operator\n");
786 case DEMANGLE_COMPONENT_UNARY:
787 printf ("unary operator\n");
789 case DEMANGLE_COMPONENT_BINARY:
790 printf ("binary operator\n");
792 case DEMANGLE_COMPONENT_BINARY_ARGS:
793 printf ("binary operator arguments\n");
795 case DEMANGLE_COMPONENT_TRINARY:
796 printf ("trinary operator\n");
798 case DEMANGLE_COMPONENT_TRINARY_ARG1:
799 printf ("trinary operator arguments 1\n");
801 case DEMANGLE_COMPONENT_TRINARY_ARG2:
802 printf ("trinary operator arguments 1\n");
804 case DEMANGLE_COMPONENT_LITERAL:
805 printf ("literal\n");
807 case DEMANGLE_COMPONENT_LITERAL_NEG:
808 printf ("negative literal\n");
810 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
811 printf ("java resource\n");
813 case DEMANGLE_COMPONENT_COMPOUND_NAME:
814 printf ("compound name\n");
816 case DEMANGLE_COMPONENT_CHARACTER:
817 printf ("character '%c'\n", dc->u.s_character.character);
819 case DEMANGLE_COMPONENT_NUMBER:
820 printf ("number %ld\n", dc->u.s_number.number);
822 case DEMANGLE_COMPONENT_DECLTYPE:
823 printf ("decltype\n");
825 case DEMANGLE_COMPONENT_PACK_EXPANSION:
826 printf ("pack expansion\n");
828 case DEMANGLE_COMPONENT_TLS_INIT:
829 printf ("tls init function\n");
831 case DEMANGLE_COMPONENT_TLS_WRAPPER:
832 printf ("tls wrapper function\n");
834 case DEMANGLE_COMPONENT_DEFAULT_ARG:
835 printf ("default argument %d\n", dc->u.s_unary_num.num);
836 d_dump (dc->u.s_unary_num.sub, indent+2);
838 case DEMANGLE_COMPONENT_LAMBDA:
839 printf ("lambda %d\n", dc->u.s_unary_num.num);
840 d_dump (dc->u.s_unary_num.sub, indent+2);
844 d_dump (d_left (dc), indent + 2);
845 d_dump (d_right (dc), indent + 2);
848 #endif /* CP_DEMANGLE_DEBUG */
850 /* Fill in a DEMANGLE_COMPONENT_NAME. */
852 CP_STATIC_IF_GLIBCPP_V3
854 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
856 if (p == NULL || s == NULL || len == 0)
858 p->type = DEMANGLE_COMPONENT_NAME;
860 p->u.s_name.len = len;
864 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
866 CP_STATIC_IF_GLIBCPP_V3
868 cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
869 struct demangle_component *name)
871 if (p == NULL || args < 0 || name == NULL)
873 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
874 p->u.s_extended_operator.args = args;
875 p->u.s_extended_operator.name = name;
879 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
881 CP_STATIC_IF_GLIBCPP_V3
883 cplus_demangle_fill_ctor (struct demangle_component *p,
884 enum gnu_v3_ctor_kinds kind,
885 struct demangle_component *name)
889 || (int) kind < gnu_v3_complete_object_ctor
890 || (int) kind > gnu_v3_object_ctor_group)
892 p->type = DEMANGLE_COMPONENT_CTOR;
893 p->u.s_ctor.kind = kind;
894 p->u.s_ctor.name = name;
898 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
900 CP_STATIC_IF_GLIBCPP_V3
902 cplus_demangle_fill_dtor (struct demangle_component *p,
903 enum gnu_v3_dtor_kinds kind,
904 struct demangle_component *name)
908 || (int) kind < gnu_v3_deleting_dtor
909 || (int) kind > gnu_v3_object_dtor_group)
911 p->type = DEMANGLE_COMPONENT_DTOR;
912 p->u.s_dtor.kind = kind;
913 p->u.s_dtor.name = name;
917 /* Add a new component. */
919 static struct demangle_component *
920 d_make_empty (struct d_info *di)
922 struct demangle_component *p;
924 if (di->next_comp >= di->num_comps)
926 p = &di->comps[di->next_comp];
931 /* Add a new generic component. */
933 static struct demangle_component *
934 d_make_comp (struct d_info *di, enum demangle_component_type type,
935 struct demangle_component *left,
936 struct demangle_component *right)
938 struct demangle_component *p;
940 /* We check for errors here. A typical error would be a NULL return
941 from a subroutine. We catch those here, and return NULL
945 /* These types require two parameters. */
946 case DEMANGLE_COMPONENT_QUAL_NAME:
947 case DEMANGLE_COMPONENT_LOCAL_NAME:
948 case DEMANGLE_COMPONENT_TYPED_NAME:
949 case DEMANGLE_COMPONENT_TAGGED_NAME:
950 case DEMANGLE_COMPONENT_TEMPLATE:
951 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
952 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
953 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
954 case DEMANGLE_COMPONENT_UNARY:
955 case DEMANGLE_COMPONENT_BINARY:
956 case DEMANGLE_COMPONENT_BINARY_ARGS:
957 case DEMANGLE_COMPONENT_TRINARY:
958 case DEMANGLE_COMPONENT_TRINARY_ARG1:
959 case DEMANGLE_COMPONENT_LITERAL:
960 case DEMANGLE_COMPONENT_LITERAL_NEG:
961 case DEMANGLE_COMPONENT_COMPOUND_NAME:
962 case DEMANGLE_COMPONENT_VECTOR_TYPE:
963 case DEMANGLE_COMPONENT_CLONE:
964 if (left == NULL || right == NULL)
968 /* These types only require one parameter. */
969 case DEMANGLE_COMPONENT_VTABLE:
970 case DEMANGLE_COMPONENT_VTT:
971 case DEMANGLE_COMPONENT_TYPEINFO:
972 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
973 case DEMANGLE_COMPONENT_TYPEINFO_FN:
974 case DEMANGLE_COMPONENT_THUNK:
975 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
976 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
977 case DEMANGLE_COMPONENT_JAVA_CLASS:
978 case DEMANGLE_COMPONENT_GUARD:
979 case DEMANGLE_COMPONENT_TLS_INIT:
980 case DEMANGLE_COMPONENT_TLS_WRAPPER:
981 case DEMANGLE_COMPONENT_REFTEMP:
982 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
983 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
984 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
985 case DEMANGLE_COMPONENT_POINTER:
986 case DEMANGLE_COMPONENT_REFERENCE:
987 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
988 case DEMANGLE_COMPONENT_COMPLEX:
989 case DEMANGLE_COMPONENT_IMAGINARY:
990 case DEMANGLE_COMPONENT_VENDOR_TYPE:
991 case DEMANGLE_COMPONENT_CAST:
992 case DEMANGLE_COMPONENT_CONVERSION:
993 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
994 case DEMANGLE_COMPONENT_DECLTYPE:
995 case DEMANGLE_COMPONENT_PACK_EXPANSION:
996 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
997 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
998 case DEMANGLE_COMPONENT_NULLARY:
999 case DEMANGLE_COMPONENT_TRINARY_ARG2:
1004 /* This needs a right parameter, but the left parameter can be
1006 case DEMANGLE_COMPONENT_ARRAY_TYPE:
1007 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
1012 /* These are allowed to have no parameters--in some cases they
1013 will be filled in later. */
1014 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
1015 case DEMANGLE_COMPONENT_RESTRICT:
1016 case DEMANGLE_COMPONENT_VOLATILE:
1017 case DEMANGLE_COMPONENT_CONST:
1018 case DEMANGLE_COMPONENT_ARGLIST:
1019 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
1020 FNQUAL_COMPONENT_CASE:
1023 /* Other types should not be seen here. */
1028 p = d_make_empty (di);
1032 p->u.s_binary.left = left;
1033 p->u.s_binary.right = right;
1038 /* Add a new demangle mangled name component. */
1040 static struct demangle_component *
1041 d_make_demangle_mangled_name (struct d_info *di, const char *s)
1043 if (d_peek_char (di) != '_' || d_peek_next_char (di) != 'Z')
1044 return d_make_name (di, s, strlen (s));
1046 return d_encoding (di, 0);
1049 /* Add a new name component. */
1051 static struct demangle_component *
1052 d_make_name (struct d_info *di, const char *s, int len)
1054 struct demangle_component *p;
1056 p = d_make_empty (di);
1057 if (! cplus_demangle_fill_name (p, s, len))
1062 /* Add a new builtin type component. */
1064 static struct demangle_component *
1065 d_make_builtin_type (struct d_info *di,
1066 const struct demangle_builtin_type_info *type)
1068 struct demangle_component *p;
1072 p = d_make_empty (di);
1075 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
1076 p->u.s_builtin.type = type;
1081 /* Add a new operator component. */
1083 static struct demangle_component *
1084 d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
1086 struct demangle_component *p;
1088 p = d_make_empty (di);
1091 p->type = DEMANGLE_COMPONENT_OPERATOR;
1092 p->u.s_operator.op = op;
1097 /* Add a new extended operator component. */
1099 static struct demangle_component *
1100 d_make_extended_operator (struct d_info *di, int args,
1101 struct demangle_component *name)
1103 struct demangle_component *p;
1105 p = d_make_empty (di);
1106 if (! cplus_demangle_fill_extended_operator (p, args, name))
1111 static struct demangle_component *
1112 d_make_default_arg (struct d_info *di, int num,
1113 struct demangle_component *sub)
1115 struct demangle_component *p = d_make_empty (di);
1118 p->type = DEMANGLE_COMPONENT_DEFAULT_ARG;
1119 p->u.s_unary_num.num = num;
1120 p->u.s_unary_num.sub = sub;
1125 /* Add a new constructor component. */
1127 static struct demangle_component *
1128 d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
1129 struct demangle_component *name)
1131 struct demangle_component *p;
1133 p = d_make_empty (di);
1134 if (! cplus_demangle_fill_ctor (p, kind, name))
1139 /* Add a new destructor component. */
1141 static struct demangle_component *
1142 d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
1143 struct demangle_component *name)
1145 struct demangle_component *p;
1147 p = d_make_empty (di);
1148 if (! cplus_demangle_fill_dtor (p, kind, name))
1153 /* Add a new template parameter. */
1155 static struct demangle_component *
1156 d_make_template_param (struct d_info *di, int i)
1158 struct demangle_component *p;
1160 p = d_make_empty (di);
1163 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
1164 p->u.s_number.number = i;
1169 /* Add a new function parameter. */
1171 static struct demangle_component *
1172 d_make_function_param (struct d_info *di, int i)
1174 struct demangle_component *p;
1176 p = d_make_empty (di);
1179 p->type = DEMANGLE_COMPONENT_FUNCTION_PARAM;
1180 p->u.s_number.number = i;
1185 /* Add a new standard substitution component. */
1187 static struct demangle_component *
1188 d_make_sub (struct d_info *di, const char *name, int len)
1190 struct demangle_component *p;
1192 p = d_make_empty (di);
1195 p->type = DEMANGLE_COMPONENT_SUB_STD;
1196 p->u.s_string.string = name;
1197 p->u.s_string.len = len;
1202 /* <mangled-name> ::= _Z <encoding> [<clone-suffix>]*
1204 TOP_LEVEL is non-zero when called at the top level. */
1206 CP_STATIC_IF_GLIBCPP_V3
1207 struct demangle_component *
1208 cplus_demangle_mangled_name (struct d_info *di, int top_level)
1210 struct demangle_component *p;
1212 if (! d_check_char (di, '_')
1213 /* Allow missing _ if not at toplevel to work around a
1214 bug in G++ abi-version=2 mangling; see the comment in
1215 write_template_arg. */
1218 if (! d_check_char (di, 'Z'))
1220 p = d_encoding (di, top_level);
1222 /* If at top level and parsing parameters, check for a clone
1224 if (top_level && (di->options & DMGL_PARAMS) != 0)
1225 while (d_peek_char (di) == '.'
1226 && (IS_LOWER (d_peek_next_char (di))
1227 || d_peek_next_char (di) == '_'
1228 || IS_DIGIT (d_peek_next_char (di))))
1229 p = d_clone_suffix (di, p);
1234 /* Return whether a function should have a return type. The argument
1235 is the function name, which may be qualified in various ways. The
1236 rules are that template functions have return types with some
1237 exceptions, function types which are not part of a function name
1238 mangling have return types with some exceptions, and non-template
1239 function names do not have return types. The exceptions are that
1240 constructors, destructors, and conversion operators do not have
1244 has_return_type (struct demangle_component *dc)
1252 case DEMANGLE_COMPONENT_TEMPLATE:
1253 return ! is_ctor_dtor_or_conversion (d_left (dc));
1254 FNQUAL_COMPONENT_CASE:
1255 return has_return_type (d_left (dc));
1259 /* Return whether a name is a constructor, a destructor, or a
1260 conversion operator. */
1263 is_ctor_dtor_or_conversion (struct demangle_component *dc)
1271 case DEMANGLE_COMPONENT_QUAL_NAME:
1272 case DEMANGLE_COMPONENT_LOCAL_NAME:
1273 return is_ctor_dtor_or_conversion (d_right (dc));
1274 case DEMANGLE_COMPONENT_CTOR:
1275 case DEMANGLE_COMPONENT_DTOR:
1276 case DEMANGLE_COMPONENT_CONVERSION:
1281 /* <encoding> ::= <(function) name> <bare-function-type>
1285 TOP_LEVEL is non-zero when called at the top level, in which case
1286 if DMGL_PARAMS is not set we do not demangle the function
1287 parameters. We only set this at the top level, because otherwise
1288 we would not correctly demangle names in local scopes. */
1290 static struct demangle_component *
1291 d_encoding (struct d_info *di, int top_level)
1293 char peek = d_peek_char (di);
1295 if (peek == 'G' || peek == 'T')
1296 return d_special_name (di);
1299 struct demangle_component *dc;
1303 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
1305 /* Strip off any initial CV-qualifiers, as they really apply
1306 to the `this' parameter, and they were not output by the
1307 v2 demangler without DMGL_PARAMS. */
1308 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1309 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1310 || dc->type == DEMANGLE_COMPONENT_CONST_THIS
1311 || dc->type == DEMANGLE_COMPONENT_REFERENCE_THIS
1312 || dc->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS)
1315 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1316 there may be function-qualifiers on its right argument which
1317 really apply here; this happens when parsing a class
1318 which is local to a function. */
1319 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1321 struct demangle_component *dcr;
1324 while (is_fnqual_component_type (dcr->type))
1326 dc->u.s_binary.right = dcr;
1332 peek = d_peek_char (di);
1333 if (dc == NULL || peek == '\0' || peek == 'E')
1335 return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
1336 d_bare_function_type (di, has_return_type (dc)));
1340 /* <tagged-name> ::= <name> B <source-name> */
1342 static struct demangle_component *
1343 d_abi_tags (struct d_info *di, struct demangle_component *dc)
1345 struct demangle_component *hold_last_name;
1348 /* Preserve the last name, so the ABI tag doesn't clobber it. */
1349 hold_last_name = di->last_name;
1351 while (peek = d_peek_char (di),
1354 struct demangle_component *tag;
1356 tag = d_source_name (di);
1357 dc = d_make_comp (di, DEMANGLE_COMPONENT_TAGGED_NAME, dc, tag);
1360 di->last_name = hold_last_name;
1365 /* <name> ::= <nested-name>
1367 ::= <unscoped-template-name> <template-args>
1370 <unscoped-name> ::= <unqualified-name>
1371 ::= St <unqualified-name>
1373 <unscoped-template-name> ::= <unscoped-name>
1377 static struct demangle_component *
1378 d_name (struct d_info *di)
1380 char peek = d_peek_char (di);
1381 struct demangle_component *dc;
1386 return d_nested_name (di);
1389 return d_local_name (di);
1392 return d_unqualified_name (di);
1398 if (d_peek_next_char (di) != 't')
1400 dc = d_substitution (di, 0);
1406 dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
1407 d_make_name (di, "std", 3),
1408 d_unqualified_name (di));
1413 if (d_peek_char (di) != 'I')
1415 /* The grammar does not permit this case to occur if we
1416 called d_substitution() above (i.e., subst == 1). We
1417 don't bother to check. */
1421 /* This is <template-args>, which means that we just saw
1422 <unscoped-template-name>, which is a substitution
1423 candidate if we didn't just get it from a
1427 if (! d_add_substitution (di, dc))
1430 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1431 d_template_args (di));
1439 dc = d_unqualified_name (di);
1440 if (d_peek_char (di) == 'I')
1442 /* This is <template-args>, which means that we just saw
1443 <unscoped-template-name>, which is a substitution
1445 if (! d_add_substitution (di, dc))
1447 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1448 d_template_args (di));
1454 /* <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1455 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
1458 static struct demangle_component *
1459 d_nested_name (struct d_info *di)
1461 struct demangle_component *ret;
1462 struct demangle_component **pret;
1463 struct demangle_component *rqual;
1465 if (! d_check_char (di, 'N'))
1468 pret = d_cv_qualifiers (di, &ret, 1);
1472 /* Parse the ref-qualifier now and then attach it
1473 once we have something to attach it to. */
1474 rqual = d_ref_qualifier (di, NULL);
1476 *pret = d_prefix (di);
1482 d_left (rqual) = ret;
1486 if (! d_check_char (di, 'E'))
1492 /* <prefix> ::= <prefix> <unqualified-name>
1493 ::= <template-prefix> <template-args>
1494 ::= <template-param>
1499 <template-prefix> ::= <prefix> <(template) unqualified-name>
1500 ::= <template-param>
1504 static struct demangle_component *
1505 d_prefix (struct d_info *di)
1507 struct demangle_component *ret = NULL;
1512 enum demangle_component_type comb_type;
1513 struct demangle_component *dc;
1515 peek = d_peek_char (di);
1519 /* The older code accepts a <local-name> here, but I don't see
1520 that in the grammar. The older code does not accept a
1521 <template-param> here. */
1523 comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
1526 char peek2 = d_peek_next_char (di);
1527 if (peek2 == 'T' || peek2 == 't')
1529 dc = cplus_demangle_type (di);
1531 /* Destructor name. */
1532 dc = d_unqualified_name (di);
1534 else if (IS_DIGIT (peek)
1539 dc = d_unqualified_name (di);
1540 else if (peek == 'S')
1541 dc = d_substitution (di, 1);
1542 else if (peek == 'I')
1546 comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1547 dc = d_template_args (di);
1549 else if (peek == 'T')
1550 dc = d_template_param (di);
1551 else if (peek == 'E')
1553 else if (peek == 'M')
1555 /* Initializer scope for a lambda. We don't need to represent
1556 this; the normal code will just treat the variable as a type
1557 scope, which gives appropriate output. */
1569 ret = d_make_comp (di, comb_type, ret, dc);
1571 if (peek != 'S' && d_peek_char (di) != 'E')
1573 if (! d_add_substitution (di, ret))
1579 /* <unqualified-name> ::= <operator-name>
1580 ::= <ctor-dtor-name>
1582 ::= <local-source-name>
1584 <local-source-name> ::= L <source-name> <discriminator>
1587 static struct demangle_component *
1588 d_unqualified_name (struct d_info *di)
1590 struct demangle_component *ret;
1593 peek = d_peek_char (di);
1594 if (IS_DIGIT (peek))
1595 ret = d_source_name (di);
1596 else if (IS_LOWER (peek))
1598 ret = d_operator_name (di);
1599 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1601 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1602 if (!strcmp (ret->u.s_operator.op->code, "li"))
1603 ret = d_make_comp (di, DEMANGLE_COMPONENT_UNARY, ret,
1604 d_source_name (di));
1607 else if (peek == 'C' || peek == 'D')
1608 ret = d_ctor_dtor_name (di);
1609 else if (peek == 'L')
1613 ret = d_source_name (di);
1616 if (! d_discriminator (di))
1619 else if (peek == 'U')
1621 switch (d_peek_next_char (di))
1624 ret = d_lambda (di);
1627 ret = d_unnamed_type (di);
1636 if (d_peek_char (di) == 'B')
1637 ret = d_abi_tags (di, ret);
1641 /* <source-name> ::= <(positive length) number> <identifier> */
1643 static struct demangle_component *
1644 d_source_name (struct d_info *di)
1647 struct demangle_component *ret;
1649 len = d_number (di);
1652 ret = d_identifier (di, len);
1653 di->last_name = ret;
1657 /* number ::= [n] <(non-negative decimal integer)> */
1660 d_number (struct d_info *di)
1667 peek = d_peek_char (di);
1672 peek = d_peek_char (di);
1678 if (! IS_DIGIT (peek))
1684 ret = ret * 10 + peek - '0';
1686 peek = d_peek_char (di);
1690 /* Like d_number, but returns a demangle_component. */
1692 static struct demangle_component *
1693 d_number_component (struct d_info *di)
1695 struct demangle_component *ret = d_make_empty (di);
1698 ret->type = DEMANGLE_COMPONENT_NUMBER;
1699 ret->u.s_number.number = d_number (di);
1704 /* identifier ::= <(unqualified source code identifier)> */
1706 static struct demangle_component *
1707 d_identifier (struct d_info *di, int len)
1713 if (di->send - name < len)
1716 d_advance (di, len);
1718 /* A Java mangled name may have a trailing '$' if it is a C++
1719 keyword. This '$' is not included in the length count. We just
1721 if ((di->options & DMGL_JAVA) != 0
1722 && d_peek_char (di) == '$')
1725 /* Look for something which looks like a gcc encoding of an
1726 anonymous namespace, and replace it with a more user friendly
1728 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1729 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1730 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1734 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1735 if ((*s == '.' || *s == '_' || *s == '$')
1738 di->expansion -= len - sizeof "(anonymous namespace)";
1739 return d_make_name (di, "(anonymous namespace)",
1740 sizeof "(anonymous namespace)" - 1);
1744 return d_make_name (di, name, len);
1747 /* operator_name ::= many different two character encodings.
1749 ::= v <digit> <source-name>
1751 This list is sorted for binary search. */
1753 #define NL(s) s, (sizeof s) - 1
1755 CP_STATIC_IF_GLIBCPP_V3
1756 const struct demangle_operator_info cplus_demangle_operators[] =
1758 { "aN", NL ("&="), 2 },
1759 { "aS", NL ("="), 2 },
1760 { "aa", NL ("&&"), 2 },
1761 { "ad", NL ("&"), 1 },
1762 { "an", NL ("&"), 2 },
1763 { "at", NL ("alignof "), 1 },
1764 { "az", NL ("alignof "), 1 },
1765 { "cc", NL ("const_cast"), 2 },
1766 { "cl", NL ("()"), 2 },
1767 { "cm", NL (","), 2 },
1768 { "co", NL ("~"), 1 },
1769 { "dV", NL ("/="), 2 },
1770 { "da", NL ("delete[] "), 1 },
1771 { "dc", NL ("dynamic_cast"), 2 },
1772 { "de", NL ("*"), 1 },
1773 { "dl", NL ("delete "), 1 },
1774 { "ds", NL (".*"), 2 },
1775 { "dt", NL ("."), 2 },
1776 { "dv", NL ("/"), 2 },
1777 { "eO", NL ("^="), 2 },
1778 { "eo", NL ("^"), 2 },
1779 { "eq", NL ("=="), 2 },
1780 { "fL", NL ("..."), 3 },
1781 { "fR", NL ("..."), 3 },
1782 { "fl", NL ("..."), 2 },
1783 { "fr", NL ("..."), 2 },
1784 { "ge", NL (">="), 2 },
1785 { "gs", NL ("::"), 1 },
1786 { "gt", NL (">"), 2 },
1787 { "ix", NL ("[]"), 2 },
1788 { "lS", NL ("<<="), 2 },
1789 { "le", NL ("<="), 2 },
1790 { "li", NL ("operator\"\" "), 1 },
1791 { "ls", NL ("<<"), 2 },
1792 { "lt", NL ("<"), 2 },
1793 { "mI", NL ("-="), 2 },
1794 { "mL", NL ("*="), 2 },
1795 { "mi", NL ("-"), 2 },
1796 { "ml", NL ("*"), 2 },
1797 { "mm", NL ("--"), 1 },
1798 { "na", NL ("new[]"), 3 },
1799 { "ne", NL ("!="), 2 },
1800 { "ng", NL ("-"), 1 },
1801 { "nt", NL ("!"), 1 },
1802 { "nw", NL ("new"), 3 },
1803 { "oR", NL ("|="), 2 },
1804 { "oo", NL ("||"), 2 },
1805 { "or", NL ("|"), 2 },
1806 { "pL", NL ("+="), 2 },
1807 { "pl", NL ("+"), 2 },
1808 { "pm", NL ("->*"), 2 },
1809 { "pp", NL ("++"), 1 },
1810 { "ps", NL ("+"), 1 },
1811 { "pt", NL ("->"), 2 },
1812 { "qu", NL ("?"), 3 },
1813 { "rM", NL ("%="), 2 },
1814 { "rS", NL (">>="), 2 },
1815 { "rc", NL ("reinterpret_cast"), 2 },
1816 { "rm", NL ("%"), 2 },
1817 { "rs", NL (">>"), 2 },
1818 { "sP", NL ("sizeof..."), 1 },
1819 { "sZ", NL ("sizeof..."), 1 },
1820 { "sc", NL ("static_cast"), 2 },
1821 { "st", NL ("sizeof "), 1 },
1822 { "sz", NL ("sizeof "), 1 },
1823 { "tr", NL ("throw"), 0 },
1824 { "tw", NL ("throw "), 1 },
1825 { NULL, NULL, 0, 0 }
1828 static struct demangle_component *
1829 d_operator_name (struct d_info *di)
1834 c1 = d_next_char (di);
1835 c2 = d_next_char (di);
1836 if (c1 == 'v' && IS_DIGIT (c2))
1837 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1838 else if (c1 == 'c' && c2 == 'v')
1840 struct demangle_component *type;
1841 int was_conversion = di->is_conversion;
1842 struct demangle_component *res;
1844 di->is_conversion = ! di->is_expression;
1845 type = cplus_demangle_type (di);
1846 if (di->is_conversion)
1847 res = d_make_comp (di, DEMANGLE_COMPONENT_CONVERSION, type, NULL);
1849 res = d_make_comp (di, DEMANGLE_COMPONENT_CAST, type, NULL);
1850 di->is_conversion = was_conversion;
1855 /* LOW is the inclusive lower bound. */
1857 /* HIGH is the exclusive upper bound. We subtract one to ignore
1858 the sentinel at the end of the array. */
1859 int high = ((sizeof (cplus_demangle_operators)
1860 / sizeof (cplus_demangle_operators[0]))
1866 const struct demangle_operator_info *p;
1868 i = low + (high - low) / 2;
1869 p = cplus_demangle_operators + i;
1871 if (c1 == p->code[0] && c2 == p->code[1])
1872 return d_make_operator (di, p);
1874 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1884 static struct demangle_component *
1885 d_make_character (struct d_info *di, int c)
1887 struct demangle_component *p;
1888 p = d_make_empty (di);
1891 p->type = DEMANGLE_COMPONENT_CHARACTER;
1892 p->u.s_character.character = c;
1897 static struct demangle_component *
1898 d_java_resource (struct d_info *di)
1900 struct demangle_component *p = NULL;
1901 struct demangle_component *next = NULL;
1906 len = d_number (di);
1910 /* Eat the leading '_'. */
1911 if (d_next_char (di) != '_')
1924 /* Each chunk is either a '$' escape... */
1942 next = d_make_character (di, c);
1950 /* ... or a sequence of characters. */
1953 while (i < len && str[i] && str[i] != '$')
1956 next = d_make_name (di, str, i);
1969 p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next);
1975 p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL);
1980 /* <special-name> ::= TV <type>
1984 ::= GV <(object) name>
1985 ::= T <call-offset> <(base) encoding>
1986 ::= Tc <call-offset> <call-offset> <(base) encoding>
1987 Also g++ extensions:
1988 ::= TC <type> <(offset) number> _ <(base) type>
1993 ::= Gr <resource name>
1998 static struct demangle_component *
1999 d_special_name (struct d_info *di)
2001 di->expansion += 20;
2002 if (d_check_char (di, 'T'))
2004 switch (d_next_char (di))
2008 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
2009 cplus_demangle_type (di), NULL);
2011 di->expansion -= 10;
2012 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
2013 cplus_demangle_type (di), NULL);
2015 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
2016 cplus_demangle_type (di), NULL);
2018 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
2019 cplus_demangle_type (di), NULL);
2022 if (! d_call_offset (di, 'h'))
2024 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
2025 d_encoding (di, 0), NULL);
2028 if (! d_call_offset (di, 'v'))
2030 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
2031 d_encoding (di, 0), NULL);
2034 if (! d_call_offset (di, '\0'))
2036 if (! d_call_offset (di, '\0'))
2038 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
2039 d_encoding (di, 0), NULL);
2043 struct demangle_component *derived_type;
2045 struct demangle_component *base_type;
2047 derived_type = cplus_demangle_type (di);
2048 offset = d_number (di);
2051 if (! d_check_char (di, '_'))
2053 base_type = cplus_demangle_type (di);
2054 /* We don't display the offset. FIXME: We should display
2055 it in verbose mode. */
2057 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
2058 base_type, derived_type);
2062 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
2063 cplus_demangle_type (di), NULL);
2065 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
2066 cplus_demangle_type (di), NULL);
2069 return d_make_comp (di, DEMANGLE_COMPONENT_TLS_INIT,
2073 return d_make_comp (di, DEMANGLE_COMPONENT_TLS_WRAPPER,
2080 else if (d_check_char (di, 'G'))
2082 switch (d_next_char (di))
2085 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
2089 struct demangle_component *name = d_name (di);
2090 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, name,
2091 d_number_component (di));
2095 return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
2096 d_encoding (di, 0), NULL);
2099 switch (d_next_char (di))
2102 return d_make_comp (di, DEMANGLE_COMPONENT_NONTRANSACTION_CLONE,
2103 d_encoding (di, 0), NULL);
2105 /* ??? The proposal is that other letters (such as 'h') stand
2106 for different variants of transaction cloning, such as
2107 compiling directly for hardware transaction support. But
2108 they still should all be transactional clones of some sort
2109 so go ahead and call them that. */
2111 return d_make_comp (di, DEMANGLE_COMPONENT_TRANSACTION_CLONE,
2112 d_encoding (di, 0), NULL);
2116 return d_java_resource (di);
2126 /* <call-offset> ::= h <nv-offset> _
2129 <nv-offset> ::= <(offset) number>
2131 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
2133 The C parameter, if not '\0', is a character we just read which is
2134 the start of the <call-offset>.
2136 We don't display the offset information anywhere. FIXME: We should
2137 display it in verbose mode. */
2140 d_call_offset (struct d_info *di, int c)
2143 c = d_next_char (di);
2150 if (! d_check_char (di, '_'))
2157 if (! d_check_char (di, '_'))
2163 /* <ctor-dtor-name> ::= C1
2171 static struct demangle_component *
2172 d_ctor_dtor_name (struct d_info *di)
2174 if (di->last_name != NULL)
2176 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
2177 di->expansion += di->last_name->u.s_name.len;
2178 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
2179 di->expansion += di->last_name->u.s_string.len;
2181 switch (d_peek_char (di))
2185 enum gnu_v3_ctor_kinds kind;
2188 if (d_peek_next_char (di) == 'I')
2194 switch (d_peek_next_char (di))
2197 kind = gnu_v3_complete_object_ctor;
2200 kind = gnu_v3_base_object_ctor;
2203 kind = gnu_v3_complete_object_allocating_ctor;
2206 kind = gnu_v3_unified_ctor;
2209 kind = gnu_v3_object_ctor_group;
2218 cplus_demangle_type (di);
2220 return d_make_ctor (di, kind, di->last_name);
2225 enum gnu_v3_dtor_kinds kind;
2227 switch (d_peek_next_char (di))
2230 kind = gnu_v3_deleting_dtor;
2233 kind = gnu_v3_complete_object_dtor;
2236 kind = gnu_v3_base_object_dtor;
2238 /* digit '3' is not used */
2240 kind = gnu_v3_unified_dtor;
2243 kind = gnu_v3_object_dtor_group;
2249 return d_make_dtor (di, kind, di->last_name);
2257 /* True iff we're looking at an order-insensitive type-qualifier, including
2258 function-type-qualifiers. */
2261 next_is_type_qual (struct d_info *di)
2263 char peek = d_peek_char (di);
2264 if (peek == 'r' || peek == 'V' || peek == 'K')
2268 peek = d_peek_next_char (di);
2269 if (peek == 'x' || peek == 'o' || peek == 'O' || peek == 'w')
2275 /* <type> ::= <builtin-type>
2277 ::= <class-enum-type>
2279 ::= <pointer-to-member-type>
2280 ::= <template-param>
2281 ::= <template-template-param> <template-args>
2283 ::= <CV-qualifiers> <type>
2286 ::= O <type> (C++0x)
2289 ::= U <source-name> <type>
2291 <builtin-type> ::= various one letter codes
2295 CP_STATIC_IF_GLIBCPP_V3
2296 const struct demangle_builtin_type_info
2297 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
2299 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
2300 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
2301 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
2302 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
2303 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
2304 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
2305 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
2306 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
2307 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
2308 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
2309 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2310 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
2311 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
2312 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
2313 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
2315 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2316 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2317 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2318 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
2319 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
2320 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2321 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
2322 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
2323 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
2324 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
2325 D_PRINT_UNSIGNED_LONG_LONG },
2326 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
2327 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT },
2328 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT },
2329 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT },
2330 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT },
2331 /* 30 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT },
2332 /* 31 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT },
2333 /* 32 */ { NL ("decltype(nullptr)"), NL ("decltype(nullptr)"),
2337 CP_STATIC_IF_GLIBCPP_V3
2338 struct demangle_component *
2339 cplus_demangle_type (struct d_info *di)
2342 struct demangle_component *ret;
2345 /* The ABI specifies that when CV-qualifiers are used, the base type
2346 is substitutable, and the fully qualified type is substitutable,
2347 but the base type with a strict subset of the CV-qualifiers is
2348 not substitutable. The natural recursive implementation of the
2349 CV-qualifiers would cause subsets to be substitutable, so instead
2350 we pull them all off now.
2352 FIXME: The ABI says that order-insensitive vendor qualifiers
2353 should be handled in the same way, but we have no way to tell
2354 which vendor qualifiers are order-insensitive and which are
2355 order-sensitive. So we just assume that they are all
2356 order-sensitive. g++ 3.4 supports only one vendor qualifier,
2357 __vector, and it treats it as order-sensitive when mangling
2360 if (next_is_type_qual (di))
2362 struct demangle_component **pret;
2364 pret = d_cv_qualifiers (di, &ret, 0);
2367 if (d_peek_char (di) == 'F')
2369 /* cv-qualifiers before a function type apply to 'this',
2370 so avoid adding the unqualified function type to
2371 the substitution list. */
2372 *pret = d_function_type (di);
2375 *pret = cplus_demangle_type (di);
2378 if ((*pret)->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
2379 || (*pret)->type == DEMANGLE_COMPONENT_REFERENCE_THIS)
2381 /* Move the ref-qualifier outside the cv-qualifiers so that
2382 they are printed in the right order. */
2383 struct demangle_component *fn = d_left (*pret);
2384 d_left (*pret) = ret;
2388 if (! d_add_substitution (di, ret))
2395 peek = d_peek_char (di);
2398 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2399 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
2400 case 'o': case 's': case 't':
2401 case 'v': case 'w': case 'x': case 'y': case 'z':
2402 ret = d_make_builtin_type (di,
2403 &cplus_demangle_builtin_types[peek - 'a']);
2404 di->expansion += ret->u.s_builtin.type->len;
2411 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
2412 d_source_name (di), NULL);
2416 ret = d_function_type (di);
2419 case '0': case '1': case '2': case '3': case '4':
2420 case '5': case '6': case '7': case '8': case '9':
2423 ret = d_class_enum_type (di);
2427 ret = d_array_type (di);
2431 ret = d_pointer_to_member_type (di);
2435 ret = d_template_param (di);
2436 if (d_peek_char (di) == 'I')
2438 /* This may be <template-template-param> <template-args>.
2439 If this is the type for a conversion operator, we can
2440 have a <template-template-param> here only by following
2441 a derivation like this:
2444 -> <template-prefix> <template-args>
2445 -> <prefix> <template-unqualified-name> <template-args>
2446 -> <unqualified-name> <template-unqualified-name> <template-args>
2447 -> <source-name> <template-unqualified-name> <template-args>
2448 -> <source-name> <operator-name> <template-args>
2449 -> <source-name> cv <type> <template-args>
2450 -> <source-name> cv <template-template-param> <template-args> <template-args>
2452 where the <template-args> is followed by another.
2453 Otherwise, we must have a derivation like this:
2456 -> <template-prefix> <template-args>
2457 -> <prefix> <template-unqualified-name> <template-args>
2458 -> <unqualified-name> <template-unqualified-name> <template-args>
2459 -> <source-name> <template-unqualified-name> <template-args>
2460 -> <source-name> <operator-name> <template-args>
2461 -> <source-name> cv <type> <template-args>
2462 -> <source-name> cv <template-param> <template-args>
2464 where we need to leave the <template-args> to be processed
2465 by d_prefix (following the <template-prefix>).
2467 The <template-template-param> part is a substitution
2469 if (! di->is_conversion)
2471 if (! d_add_substitution (di, ret))
2473 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2474 d_template_args (di));
2478 struct demangle_component *args;
2479 struct d_info_checkpoint checkpoint;
2481 d_checkpoint (di, &checkpoint);
2482 args = d_template_args (di);
2483 if (d_peek_char (di) == 'I')
2485 if (! d_add_substitution (di, ret))
2487 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2491 d_backtrack (di, &checkpoint);
2497 /* If this is a special substitution, then it is the start of
2498 <class-enum-type>. */
2502 peek_next = d_peek_next_char (di);
2503 if (IS_DIGIT (peek_next)
2505 || IS_UPPER (peek_next))
2507 ret = d_substitution (di, 0);
2508 /* The substituted name may have been a template name and
2509 may be followed by tepmlate args. */
2510 if (d_peek_char (di) == 'I')
2511 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2512 d_template_args (di));
2518 ret = d_class_enum_type (di);
2519 /* If the substitution was a complete type, then it is not
2520 a new substitution candidate. However, if the
2521 substitution was followed by template arguments, then
2522 the whole thing is a substitution candidate. */
2523 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
2531 ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE,
2532 cplus_demangle_type (di), NULL);
2537 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
2538 cplus_demangle_type (di), NULL);
2543 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
2544 cplus_demangle_type (di), NULL);
2549 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
2550 cplus_demangle_type (di), NULL);
2555 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
2556 cplus_demangle_type (di), NULL);
2561 ret = d_source_name (di);
2562 if (d_peek_char (di) == 'I')
2563 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2564 d_template_args (di));
2565 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
2566 cplus_demangle_type (di), ret);
2572 peek = d_next_char (di);
2577 /* decltype (expression) */
2578 ret = d_make_comp (di, DEMANGLE_COMPONENT_DECLTYPE,
2579 d_expression (di), NULL);
2580 if (ret && d_next_char (di) != 'E')
2586 /* Pack expansion. */
2587 ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2588 cplus_demangle_type (di), NULL);
2594 ret = d_make_name (di, "auto", 4);
2598 /* 32-bit decimal floating point */
2599 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[26]);
2600 di->expansion += ret->u.s_builtin.type->len;
2604 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[27]);
2605 di->expansion += ret->u.s_builtin.type->len;
2609 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[28]);
2610 di->expansion += ret->u.s_builtin.type->len;
2613 /* 16-bit half-precision FP */
2614 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[29]);
2615 di->expansion += ret->u.s_builtin.type->len;
2619 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[30]);
2620 di->expansion += ret->u.s_builtin.type->len;
2624 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[31]);
2625 di->expansion += ret->u.s_builtin.type->len;
2629 /* Fixed point types. DF<int bits><length><fract bits><sat> */
2630 ret = d_make_empty (di);
2631 ret->type = DEMANGLE_COMPONENT_FIXED_TYPE;
2632 if ((ret->u.s_fixed.accum = IS_DIGIT (d_peek_char (di))))
2633 /* For demangling we don't care about the bits. */
2635 ret->u.s_fixed.length = cplus_demangle_type (di);
2636 if (ret->u.s_fixed.length == NULL)
2639 peek = d_next_char (di);
2640 ret->u.s_fixed.sat = (peek == 's');
2644 ret = d_vector_type (di);
2649 /* decltype(nullptr) */
2650 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[32]);
2651 di->expansion += ret->u.s_builtin.type->len;
2665 if (! d_add_substitution (di, ret))
2672 /* <CV-qualifiers> ::= [r] [V] [K] [Dx] */
2674 static struct demangle_component **
2675 d_cv_qualifiers (struct d_info *di,
2676 struct demangle_component **pret, int member_fn)
2678 struct demangle_component **pstart;
2682 peek = d_peek_char (di);
2683 while (next_is_type_qual (di))
2685 enum demangle_component_type t;
2686 struct demangle_component *right = NULL;
2692 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2693 : DEMANGLE_COMPONENT_RESTRICT);
2694 di->expansion += sizeof "restrict";
2696 else if (peek == 'V')
2699 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2700 : DEMANGLE_COMPONENT_VOLATILE);
2701 di->expansion += sizeof "volatile";
2703 else if (peek == 'K')
2706 ? DEMANGLE_COMPONENT_CONST_THIS
2707 : DEMANGLE_COMPONENT_CONST);
2708 di->expansion += sizeof "const";
2712 peek = d_next_char (di);
2715 t = DEMANGLE_COMPONENT_TRANSACTION_SAFE;
2716 di->expansion += sizeof "transaction_safe";
2718 else if (peek == 'o'
2721 t = DEMANGLE_COMPONENT_NOEXCEPT;
2722 di->expansion += sizeof "noexcept";
2725 right = d_expression (di);
2728 if (! d_check_char (di, 'E'))
2732 else if (peek == 'w')
2734 t = DEMANGLE_COMPONENT_THROW_SPEC;
2735 di->expansion += sizeof "throw";
2736 right = d_parmlist (di);
2739 if (! d_check_char (di, 'E'))
2746 *pret = d_make_comp (di, t, NULL, right);
2749 pret = &d_left (*pret);
2751 peek = d_peek_char (di);
2754 if (!member_fn && peek == 'F')
2756 while (pstart != pret)
2758 switch ((*pstart)->type)
2760 case DEMANGLE_COMPONENT_RESTRICT:
2761 (*pstart)->type = DEMANGLE_COMPONENT_RESTRICT_THIS;
2763 case DEMANGLE_COMPONENT_VOLATILE:
2764 (*pstart)->type = DEMANGLE_COMPONENT_VOLATILE_THIS;
2766 case DEMANGLE_COMPONENT_CONST:
2767 (*pstart)->type = DEMANGLE_COMPONENT_CONST_THIS;
2772 pstart = &d_left (*pstart);
2779 /* <ref-qualifier> ::= R
2782 static struct demangle_component *
2783 d_ref_qualifier (struct d_info *di, struct demangle_component *sub)
2785 struct demangle_component *ret = sub;
2788 peek = d_peek_char (di);
2789 if (peek == 'R' || peek == 'O')
2791 enum demangle_component_type t;
2794 t = DEMANGLE_COMPONENT_REFERENCE_THIS;
2795 di->expansion += sizeof "&";
2799 t = DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS;
2800 di->expansion += sizeof "&&";
2804 ret = d_make_comp (di, t, ret, NULL);
2810 /* <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] [T] E */
2812 static struct demangle_component *
2813 d_function_type (struct d_info *di)
2815 struct demangle_component *ret;
2817 if (! d_check_char (di, 'F'))
2819 if (d_peek_char (di) == 'Y')
2821 /* Function has C linkage. We don't print this information.
2822 FIXME: We should print it in verbose mode. */
2825 ret = d_bare_function_type (di, 1);
2826 ret = d_ref_qualifier (di, ret);
2828 if (! d_check_char (di, 'E'))
2835 static struct demangle_component *
2836 d_parmlist (struct d_info *di)
2838 struct demangle_component *tl;
2839 struct demangle_component **ptl;
2845 struct demangle_component *type;
2847 char peek = d_peek_char (di);
2848 if (peek == '\0' || peek == 'E' || peek == '.')
2850 if ((peek == 'R' || peek == 'O')
2851 && d_peek_next_char (di) == 'E')
2852 /* Function ref-qualifier, not a ref prefix for a parameter type. */
2854 type = cplus_demangle_type (di);
2857 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
2860 ptl = &d_right (*ptl);
2863 /* There should be at least one parameter type besides the optional
2864 return type. A function which takes no arguments will have a
2865 single parameter type void. */
2869 /* If we have a single parameter type void, omit it. */
2870 if (d_right (tl) == NULL
2871 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2872 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
2874 di->expansion -= d_left (tl)->u.s_builtin.type->len;
2881 /* <bare-function-type> ::= [J]<type>+ */
2883 static struct demangle_component *
2884 d_bare_function_type (struct d_info *di, int has_return_type)
2886 struct demangle_component *return_type;
2887 struct demangle_component *tl;
2890 /* Detect special qualifier indicating that the first argument
2891 is the return type. */
2892 peek = d_peek_char (di);
2896 has_return_type = 1;
2899 if (has_return_type)
2901 return_type = cplus_demangle_type (di);
2902 if (return_type == NULL)
2908 tl = d_parmlist (di);
2912 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE,
2916 /* <class-enum-type> ::= <name> */
2918 static struct demangle_component *
2919 d_class_enum_type (struct d_info *di)
2924 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2925 ::= A [<(dimension) expression>] _ <(element) type>
2928 static struct demangle_component *
2929 d_array_type (struct d_info *di)
2932 struct demangle_component *dim;
2934 if (! d_check_char (di, 'A'))
2937 peek = d_peek_char (di);
2940 else if (IS_DIGIT (peek))
2948 peek = d_peek_char (di);
2950 while (IS_DIGIT (peek));
2951 dim = d_make_name (di, s, d_str (di) - s);
2957 dim = d_expression (di);
2962 if (! d_check_char (di, '_'))
2965 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2966 cplus_demangle_type (di));
2969 /* <vector-type> ::= Dv <number> _ <type>
2970 ::= Dv _ <expression> _ <type> */
2972 static struct demangle_component *
2973 d_vector_type (struct d_info *di)
2976 struct demangle_component *dim;
2978 peek = d_peek_char (di);
2982 dim = d_expression (di);
2985 dim = d_number_component (di);
2990 if (! d_check_char (di, '_'))
2993 return d_make_comp (di, DEMANGLE_COMPONENT_VECTOR_TYPE, dim,
2994 cplus_demangle_type (di));
2997 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2999 static struct demangle_component *
3000 d_pointer_to_member_type (struct d_info *di)
3002 struct demangle_component *cl;
3003 struct demangle_component *mem;
3005 if (! d_check_char (di, 'M'))
3008 cl = cplus_demangle_type (di);
3012 /* The ABI says, "The type of a non-static member function is considered
3013 to be different, for the purposes of substitution, from the type of a
3014 namespace-scope or static member function whose type appears
3015 similar. The types of two non-static member functions are considered
3016 to be different, for the purposes of substitution, if the functions
3017 are members of different classes. In other words, for the purposes of
3018 substitution, the class of which the function is a member is
3019 considered part of the type of function."
3021 For a pointer to member function, this call to cplus_demangle_type
3022 will end up adding a (possibly qualified) non-member function type to
3023 the substitution table, which is not correct; however, the member
3024 function type will never be used in a substitution, so putting the
3025 wrong type in the substitution table is harmless. */
3027 mem = cplus_demangle_type (di);
3031 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
3034 /* <non-negative number> _ */
3037 d_compact_number (struct d_info *di)
3040 if (d_peek_char (di) == '_')
3042 else if (d_peek_char (di) == 'n')
3045 num = d_number (di) + 1;
3047 if (num < 0 || ! d_check_char (di, '_'))
3052 /* <template-param> ::= T_
3053 ::= T <(parameter-2 non-negative) number> _
3056 static struct demangle_component *
3057 d_template_param (struct d_info *di)
3061 if (! d_check_char (di, 'T'))
3064 param = d_compact_number (di);
3070 return d_make_template_param (di, param);
3073 /* <template-args> ::= I <template-arg>+ E */
3075 static struct demangle_component *
3076 d_template_args (struct d_info *di)
3078 if (d_peek_char (di) != 'I'
3079 && d_peek_char (di) != 'J')
3083 return d_template_args_1 (di);
3086 /* <template-arg>* E */
3088 static struct demangle_component *
3089 d_template_args_1 (struct d_info *di)
3091 struct demangle_component *hold_last_name;
3092 struct demangle_component *al;
3093 struct demangle_component **pal;
3095 /* Preserve the last name we saw--don't let the template arguments
3096 clobber it, as that would give us the wrong name for a subsequent
3097 constructor or destructor. */
3098 hold_last_name = di->last_name;
3100 if (d_peek_char (di) == 'E')
3102 /* An argument pack can be empty. */
3104 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, NULL, NULL);
3111 struct demangle_component *a;
3113 a = d_template_arg (di);
3117 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
3120 pal = &d_right (*pal);
3122 if (d_peek_char (di) == 'E')
3129 di->last_name = hold_last_name;
3134 /* <template-arg> ::= <type>
3135 ::= X <expression> E
3139 static struct demangle_component *
3140 d_template_arg (struct d_info *di)
3142 struct demangle_component *ret;
3144 switch (d_peek_char (di))
3148 ret = d_expression (di);
3149 if (! d_check_char (di, 'E'))
3154 return d_expr_primary (di);
3158 /* An argument pack. */
3159 return d_template_args (di);
3162 return cplus_demangle_type (di);
3166 /* Parse a sequence of expressions until we hit the terminator
3169 static struct demangle_component *
3170 d_exprlist (struct d_info *di, char terminator)
3172 struct demangle_component *list = NULL;
3173 struct demangle_component **p = &list;
3175 if (d_peek_char (di) == terminator)
3178 return d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, NULL, NULL);
3183 struct demangle_component *arg = d_expression (di);
3187 *p = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, arg, NULL);
3192 if (d_peek_char (di) == terminator)
3202 /* Returns nonzero iff OP is an operator for a C++ cast: const_cast,
3203 dynamic_cast, static_cast or reinterpret_cast. */
3206 op_is_new_cast (struct demangle_component *op)
3208 const char *code = op->u.s_operator.op->code;
3209 return (code[1] == 'c'
3210 && (code[0] == 's' || code[0] == 'd'
3211 || code[0] == 'c' || code[0] == 'r'));
3214 /* <expression> ::= <(unary) operator-name> <expression>
3215 ::= <(binary) operator-name> <expression> <expression>
3216 ::= <(trinary) operator-name> <expression> <expression> <expression>
3217 ::= cl <expression>+ E
3219 ::= <template-param>
3220 ::= sr <type> <unqualified-name>
3221 ::= sr <type> <unqualified-name> <template-args>
3225 static inline struct demangle_component *
3226 d_expression_1 (struct d_info *di)
3230 peek = d_peek_char (di);
3232 return d_expr_primary (di);
3233 else if (peek == 'T')
3234 return d_template_param (di);
3235 else if (peek == 's' && d_peek_next_char (di) == 'r')
3237 struct demangle_component *type;
3238 struct demangle_component *name;
3241 type = cplus_demangle_type (di);
3242 name = d_unqualified_name (di);
3243 if (d_peek_char (di) != 'I')
3244 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
3246 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
3247 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
3248 d_template_args (di)));
3250 else if (peek == 's' && d_peek_next_char (di) == 'p')
3253 return d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
3254 d_expression_1 (di), NULL);
3256 else if (peek == 'f' && d_peek_next_char (di) == 'p')
3258 /* Function parameter used in a late-specified return type. */
3261 if (d_peek_char (di) == 'T')
3263 /* 'this' parameter. */
3269 index = d_compact_number (di);
3270 if (index == INT_MAX || index == -1)
3274 return d_make_function_param (di, index);
3276 else if (IS_DIGIT (peek)
3277 || (peek == 'o' && d_peek_next_char (di) == 'n'))
3279 /* We can get an unqualified name as an expression in the case of
3280 a dependent function call, i.e. decltype(f(t)). */
3281 struct demangle_component *name;
3284 /* operator-function-id, i.e. operator+(t). */
3287 name = d_unqualified_name (di);
3290 if (d_peek_char (di) == 'I')
3291 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
3292 d_template_args (di));
3296 else if ((peek == 'i' || peek == 't')
3297 && d_peek_next_char (di) == 'l')
3299 /* Brace-enclosed initializer list, untyped or typed. */
3300 struct demangle_component *type = NULL;
3302 type = cplus_demangle_type (di);
3303 if (!d_peek_next_char (di))
3306 return d_make_comp (di, DEMANGLE_COMPONENT_INITIALIZER_LIST,
3307 type, d_exprlist (di, 'E'));
3311 struct demangle_component *op;
3312 const char *code = NULL;
3315 op = d_operator_name (di);
3319 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
3321 code = op->u.s_operator.op->code;
3322 di->expansion += op->u.s_operator.op->len - 2;
3323 if (strcmp (code, "st") == 0)
3324 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3325 cplus_demangle_type (di));
3332 case DEMANGLE_COMPONENT_OPERATOR:
3333 args = op->u.s_operator.op->args;
3335 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3336 args = op->u.s_extended_operator.args;
3338 case DEMANGLE_COMPONENT_CAST:
3346 return d_make_comp (di, DEMANGLE_COMPONENT_NULLARY, op, NULL);
3350 struct demangle_component *operand;
3353 if (code && (code[0] == 'p' || code[0] == 'm')
3354 && code[1] == code[0])
3355 /* pp_ and mm_ are the prefix variants. */
3356 suffix = !d_check_char (di, '_');
3358 if (op->type == DEMANGLE_COMPONENT_CAST
3359 && d_check_char (di, '_'))
3360 operand = d_exprlist (di, 'E');
3361 else if (code && !strcmp (code, "sP"))
3362 operand = d_template_args_1 (di);
3364 operand = d_expression_1 (di);
3367 /* Indicate the suffix variant for d_print_comp. */
3368 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3370 DEMANGLE_COMPONENT_BINARY_ARGS,
3373 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3378 struct demangle_component *left;
3379 struct demangle_component *right;
3383 if (op_is_new_cast (op))
3384 left = cplus_demangle_type (di);
3385 else if (code[0] == 'f')
3386 /* fold-expression. */
3387 left = d_operator_name (di);
3389 left = d_expression_1 (di);
3390 if (!strcmp (code, "cl"))
3391 right = d_exprlist (di, 'E');
3392 else if (!strcmp (code, "dt") || !strcmp (code, "pt"))
3394 right = d_unqualified_name (di);
3395 if (d_peek_char (di) == 'I')
3396 right = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE,
3397 right, d_template_args (di));
3400 right = d_expression_1 (di);
3402 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
3404 DEMANGLE_COMPONENT_BINARY_ARGS,
3409 struct demangle_component *first;
3410 struct demangle_component *second;
3411 struct demangle_component *third;
3415 else if (!strcmp (code, "qu"))
3417 /* ?: expression. */
3418 first = d_expression_1 (di);
3419 second = d_expression_1 (di);
3420 third = d_expression_1 (di);
3424 else if (code[0] == 'f')
3426 /* fold-expression. */
3427 first = d_operator_name (di);
3428 second = d_expression_1 (di);
3429 third = d_expression_1 (di);
3433 else if (code[0] == 'n')
3435 /* new-expression. */
3436 if (code[1] != 'w' && code[1] != 'a')
3438 first = d_exprlist (di, '_');
3439 second = cplus_demangle_type (di);
3440 if (d_peek_char (di) == 'E')
3445 else if (d_peek_char (di) == 'p'
3446 && d_peek_next_char (di) == 'i')
3448 /* Parenthesized initializer. */
3450 third = d_exprlist (di, 'E');
3452 else if (d_peek_char (di) == 'i'
3453 && d_peek_next_char (di) == 'l')
3454 /* initializer-list. */
3455 third = d_expression_1 (di);
3461 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
3463 DEMANGLE_COMPONENT_TRINARY_ARG1,
3466 DEMANGLE_COMPONENT_TRINARY_ARG2,
3475 static struct demangle_component *
3476 d_expression (struct d_info *di)
3478 struct demangle_component *ret;
3479 int was_expression = di->is_expression;
3481 di->is_expression = 1;
3482 ret = d_expression_1 (di);
3483 di->is_expression = was_expression;
3487 /* <expr-primary> ::= L <type> <(value) number> E
3488 ::= L <type> <(value) float> E
3489 ::= L <mangled-name> E
3492 static struct demangle_component *
3493 d_expr_primary (struct d_info *di)
3495 struct demangle_component *ret;
3497 if (! d_check_char (di, 'L'))
3499 if (d_peek_char (di) == '_'
3500 /* Workaround for G++ bug; see comment in write_template_arg. */
3501 || d_peek_char (di) == 'Z')
3502 ret = cplus_demangle_mangled_name (di, 0);
3505 struct demangle_component *type;
3506 enum demangle_component_type t;
3509 type = cplus_demangle_type (di);
3513 /* If we have a type we know how to print, we aren't going to
3514 print the type name itself. */
3515 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
3516 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
3517 di->expansion -= type->u.s_builtin.type->len;
3519 /* Rather than try to interpret the literal value, we just
3520 collect it as a string. Note that it's possible to have a
3521 floating point literal here. The ABI specifies that the
3522 format of such literals is machine independent. That's fine,
3523 but what's not fine is that versions of g++ up to 3.2 with
3524 -fabi-version=1 used upper case letters in the hex constant,
3525 and dumped out gcc's internal representation. That makes it
3526 hard to tell where the constant ends, and hard to dump the
3527 constant in any readable form anyhow. We don't attempt to
3528 handle these cases. */
3530 t = DEMANGLE_COMPONENT_LITERAL;
3531 if (d_peek_char (di) == 'n')
3533 t = DEMANGLE_COMPONENT_LITERAL_NEG;
3537 while (d_peek_char (di) != 'E')
3539 if (d_peek_char (di) == '\0')
3543 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
3545 if (! d_check_char (di, 'E'))
3550 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
3551 ::= Z <(function) encoding> E s [<discriminator>]
3552 ::= Z <(function) encoding> E d [<parameter> number>] _ <entity name>
3555 static struct demangle_component *
3556 d_local_name (struct d_info *di)
3558 struct demangle_component *function;
3560 if (! d_check_char (di, 'Z'))
3563 function = d_encoding (di, 0);
3565 if (! d_check_char (di, 'E'))
3568 if (d_peek_char (di) == 's')
3571 if (! d_discriminator (di))
3573 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
3574 d_make_name (di, "string literal",
3575 sizeof "string literal" - 1));
3579 struct demangle_component *name;
3582 if (d_peek_char (di) == 'd')
3584 /* Default argument scope: d <number> _. */
3586 num = d_compact_number (di);
3595 /* Lambdas and unnamed types have internal discriminators. */
3596 case DEMANGLE_COMPONENT_LAMBDA:
3597 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
3600 if (! d_discriminator (di))
3604 name = d_make_default_arg (di, num, name);
3605 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
3609 /* <discriminator> ::= _ <(non-negative) number>
3611 We demangle the discriminator, but we don't print it out. FIXME:
3612 We should print it out in verbose mode. */
3615 d_discriminator (struct d_info *di)
3619 if (d_peek_char (di) != '_')
3622 discrim = d_number (di);
3628 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ */
3630 static struct demangle_component *
3631 d_lambda (struct d_info *di)
3633 struct demangle_component *tl;
3634 struct demangle_component *ret;
3637 if (! d_check_char (di, 'U'))
3639 if (! d_check_char (di, 'l'))
3642 tl = d_parmlist (di);
3646 if (! d_check_char (di, 'E'))
3649 num = d_compact_number (di);
3653 ret = d_make_empty (di);
3656 ret->type = DEMANGLE_COMPONENT_LAMBDA;
3657 ret->u.s_unary_num.sub = tl;
3658 ret->u.s_unary_num.num = num;
3661 if (! d_add_substitution (di, ret))
3667 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
3669 static struct demangle_component *
3670 d_unnamed_type (struct d_info *di)
3672 struct demangle_component *ret;
3675 if (! d_check_char (di, 'U'))
3677 if (! d_check_char (di, 't'))
3680 num = d_compact_number (di);
3684 ret = d_make_empty (di);
3687 ret->type = DEMANGLE_COMPONENT_UNNAMED_TYPE;
3688 ret->u.s_number.number = num;
3691 if (! d_add_substitution (di, ret))
3697 /* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]*
3700 static struct demangle_component *
3701 d_clone_suffix (struct d_info *di, struct demangle_component *encoding)
3703 const char *suffix = d_str (di);
3704 const char *pend = suffix;
3705 struct demangle_component *n;
3707 if (*pend == '.' && (IS_LOWER (pend[1]) || pend[1] == '_'))
3710 while (IS_LOWER (*pend) || *pend == '_')
3713 while (*pend == '.' && IS_DIGIT (pend[1]))
3716 while (IS_DIGIT (*pend))
3719 d_advance (di, pend - suffix);
3720 n = d_make_name (di, suffix, pend - suffix);
3721 return d_make_comp (di, DEMANGLE_COMPONENT_CLONE, encoding, n);
3724 /* Add a new substitution. */
3727 d_add_substitution (struct d_info *di, struct demangle_component *dc)
3731 if (di->next_sub >= di->num_subs)
3733 di->subs[di->next_sub] = dc;
3738 /* <substitution> ::= S <seq-id> _
3748 If PREFIX is non-zero, then this type is being used as a prefix in
3749 a qualified name. In this case, for the standard substitutions, we
3750 need to check whether we are being used as a prefix for a
3751 constructor or destructor, and return a full template name.
3752 Otherwise we will get something like std::iostream::~iostream()
3753 which does not correspond particularly well to any function which
3754 actually appears in the source.
3757 static const struct d_standard_sub_info standard_subs[] =
3762 { 'a', NL ("std::allocator"),
3763 NL ("std::allocator"),
3765 { 'b', NL ("std::basic_string"),
3766 NL ("std::basic_string"),
3767 NL ("basic_string") },
3768 { 's', NL ("std::string"),
3769 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
3770 NL ("basic_string") },
3771 { 'i', NL ("std::istream"),
3772 NL ("std::basic_istream<char, std::char_traits<char> >"),
3773 NL ("basic_istream") },
3774 { 'o', NL ("std::ostream"),
3775 NL ("std::basic_ostream<char, std::char_traits<char> >"),
3776 NL ("basic_ostream") },
3777 { 'd', NL ("std::iostream"),
3778 NL ("std::basic_iostream<char, std::char_traits<char> >"),
3779 NL ("basic_iostream") }
3782 static struct demangle_component *
3783 d_substitution (struct d_info *di, int prefix)
3787 if (! d_check_char (di, 'S'))
3790 c = d_next_char (di);
3791 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
3800 unsigned int new_id;
3803 new_id = id * 36 + c - '0';
3804 else if (IS_UPPER (c))
3805 new_id = id * 36 + c - 'A' + 10;
3811 c = d_next_char (di);
3818 if (id >= (unsigned int) di->next_sub)
3823 return di->subs[id];
3828 const struct d_standard_sub_info *p;
3829 const struct d_standard_sub_info *pend;
3831 verbose = (di->options & DMGL_VERBOSE) != 0;
3832 if (! verbose && prefix)
3836 peek = d_peek_char (di);
3837 if (peek == 'C' || peek == 'D')
3841 pend = (&standard_subs[0]
3842 + sizeof standard_subs / sizeof standard_subs[0]);
3843 for (p = &standard_subs[0]; p < pend; ++p)
3849 struct demangle_component *dc;
3851 if (p->set_last_name != NULL)
3852 di->last_name = d_make_sub (di, p->set_last_name,
3853 p->set_last_name_len);
3856 s = p->full_expansion;
3861 s = p->simple_expansion;
3862 len = p->simple_len;
3864 di->expansion += len;
3865 dc = d_make_sub (di, s, len);
3866 if (d_peek_char (di) == 'B')
3868 /* If there are ABI tags on the abbreviation, it becomes
3869 a substitution candidate. */
3870 dc = d_abi_tags (di, dc);
3871 d_add_substitution (di, dc);
3882 d_checkpoint (struct d_info *di, struct d_info_checkpoint *checkpoint)
3884 checkpoint->n = di->n;
3885 checkpoint->next_comp = di->next_comp;
3886 checkpoint->next_sub = di->next_sub;
3887 checkpoint->did_subs = di->did_subs;
3888 checkpoint->expansion = di->expansion;
3892 d_backtrack (struct d_info *di, struct d_info_checkpoint *checkpoint)
3894 di->n = checkpoint->n;
3895 di->next_comp = checkpoint->next_comp;
3896 di->next_sub = checkpoint->next_sub;
3897 di->did_subs = checkpoint->did_subs;
3898 di->expansion = checkpoint->expansion;
3901 /* Initialize a growable string. */
3904 d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
3909 dgs->allocation_failure = 0;
3912 d_growable_string_resize (dgs, estimate);
3915 /* Grow a growable string to a given size. */
3918 d_growable_string_resize (struct d_growable_string *dgs, size_t need)
3923 if (dgs->allocation_failure)
3926 /* Start allocation at two bytes to avoid any possibility of confusion
3927 with the special value of 1 used as a return in *palc to indicate
3928 allocation failures. */
3929 newalc = dgs->alc > 0 ? dgs->alc : 2;
3930 while (newalc < need)
3933 newbuf = (char *) realloc (dgs->buf, newalc);
3940 dgs->allocation_failure = 1;
3947 /* Append a buffer to a growable string. */
3950 d_growable_string_append_buffer (struct d_growable_string *dgs,
3951 const char *s, size_t l)
3955 need = dgs->len + l + 1;
3956 if (need > dgs->alc)
3957 d_growable_string_resize (dgs, need);
3959 if (dgs->allocation_failure)
3962 memcpy (dgs->buf + dgs->len, s, l);
3963 dgs->buf[dgs->len + l] = '\0';
3967 /* Bridge growable strings to the callback mechanism. */
3970 d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
3972 struct d_growable_string *dgs = (struct d_growable_string*) opaque;
3974 d_growable_string_append_buffer (dgs, s, l);
3977 /* Walk the tree, counting the number of templates encountered, and
3978 the number of times a scope might be saved. These counts will be
3979 used to allocate data structures for d_print_comp, so the logic
3980 here must mirror the logic d_print_comp will use. It is not
3981 important that the resulting numbers are exact, so long as they
3982 are larger than the actual numbers encountered. */
3985 d_count_templates_scopes (int *num_templates, int *num_scopes,
3986 const struct demangle_component *dc)
3993 case DEMANGLE_COMPONENT_NAME:
3994 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
3995 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
3996 case DEMANGLE_COMPONENT_SUB_STD:
3997 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3998 case DEMANGLE_COMPONENT_OPERATOR:
3999 case DEMANGLE_COMPONENT_CHARACTER:
4000 case DEMANGLE_COMPONENT_NUMBER:
4001 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
4004 case DEMANGLE_COMPONENT_TEMPLATE:
4006 goto recurse_left_right;
4008 case DEMANGLE_COMPONENT_REFERENCE:
4009 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4010 if (d_left (dc)->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
4012 goto recurse_left_right;
4014 case DEMANGLE_COMPONENT_QUAL_NAME:
4015 case DEMANGLE_COMPONENT_LOCAL_NAME:
4016 case DEMANGLE_COMPONENT_TYPED_NAME:
4017 case DEMANGLE_COMPONENT_VTABLE:
4018 case DEMANGLE_COMPONENT_VTT:
4019 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
4020 case DEMANGLE_COMPONENT_TYPEINFO:
4021 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
4022 case DEMANGLE_COMPONENT_TYPEINFO_FN:
4023 case DEMANGLE_COMPONENT_THUNK:
4024 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
4025 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
4026 case DEMANGLE_COMPONENT_JAVA_CLASS:
4027 case DEMANGLE_COMPONENT_GUARD:
4028 case DEMANGLE_COMPONENT_TLS_INIT:
4029 case DEMANGLE_COMPONENT_TLS_WRAPPER:
4030 case DEMANGLE_COMPONENT_REFTEMP:
4031 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
4032 case DEMANGLE_COMPONENT_RESTRICT:
4033 case DEMANGLE_COMPONENT_VOLATILE:
4034 case DEMANGLE_COMPONENT_CONST:
4035 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4036 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4037 case DEMANGLE_COMPONENT_CONST_THIS:
4038 case DEMANGLE_COMPONENT_REFERENCE_THIS:
4039 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
4040 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
4041 case DEMANGLE_COMPONENT_NOEXCEPT:
4042 case DEMANGLE_COMPONENT_THROW_SPEC:
4043 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4044 case DEMANGLE_COMPONENT_POINTER:
4045 case DEMANGLE_COMPONENT_COMPLEX:
4046 case DEMANGLE_COMPONENT_IMAGINARY:
4047 case DEMANGLE_COMPONENT_VENDOR_TYPE:
4048 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
4049 case DEMANGLE_COMPONENT_ARRAY_TYPE:
4050 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4051 case DEMANGLE_COMPONENT_VECTOR_TYPE:
4052 case DEMANGLE_COMPONENT_ARGLIST:
4053 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
4054 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
4055 case DEMANGLE_COMPONENT_CAST:
4056 case DEMANGLE_COMPONENT_CONVERSION:
4057 case DEMANGLE_COMPONENT_NULLARY:
4058 case DEMANGLE_COMPONENT_UNARY:
4059 case DEMANGLE_COMPONENT_BINARY:
4060 case DEMANGLE_COMPONENT_BINARY_ARGS:
4061 case DEMANGLE_COMPONENT_TRINARY:
4062 case DEMANGLE_COMPONENT_TRINARY_ARG1:
4063 case DEMANGLE_COMPONENT_TRINARY_ARG2:
4064 case DEMANGLE_COMPONENT_LITERAL:
4065 case DEMANGLE_COMPONENT_LITERAL_NEG:
4066 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
4067 case DEMANGLE_COMPONENT_COMPOUND_NAME:
4068 case DEMANGLE_COMPONENT_DECLTYPE:
4069 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
4070 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
4071 case DEMANGLE_COMPONENT_PACK_EXPANSION:
4072 case DEMANGLE_COMPONENT_TAGGED_NAME:
4073 case DEMANGLE_COMPONENT_CLONE:
4075 d_count_templates_scopes (num_templates, num_scopes,
4077 d_count_templates_scopes (num_templates, num_scopes,
4081 case DEMANGLE_COMPONENT_CTOR:
4082 d_count_templates_scopes (num_templates, num_scopes,
4086 case DEMANGLE_COMPONENT_DTOR:
4087 d_count_templates_scopes (num_templates, num_scopes,
4091 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
4092 d_count_templates_scopes (num_templates, num_scopes,
4093 dc->u.s_extended_operator.name);
4096 case DEMANGLE_COMPONENT_FIXED_TYPE:
4097 d_count_templates_scopes (num_templates, num_scopes,
4098 dc->u.s_fixed.length);
4101 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
4102 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
4103 d_count_templates_scopes (num_templates, num_scopes,
4107 case DEMANGLE_COMPONENT_LAMBDA:
4108 case DEMANGLE_COMPONENT_DEFAULT_ARG:
4109 d_count_templates_scopes (num_templates, num_scopes,
4110 dc->u.s_unary_num.sub);
4115 /* Initialize a print information structure. */
4118 d_print_init (struct d_print_info *dpi, demangle_callbackref callback,
4119 void *opaque, const struct demangle_component *dc)
4122 dpi->last_char = '\0';
4123 dpi->templates = NULL;
4124 dpi->modifiers = NULL;
4125 dpi->pack_index = 0;
4126 dpi->flush_count = 0;
4128 dpi->callback = callback;
4129 dpi->opaque = opaque;
4131 dpi->demangle_failure = 0;
4132 dpi->is_lambda_arg = 0;
4134 dpi->component_stack = NULL;
4136 dpi->saved_scopes = NULL;
4137 dpi->next_saved_scope = 0;
4138 dpi->num_saved_scopes = 0;
4140 dpi->copy_templates = NULL;
4141 dpi->next_copy_template = 0;
4142 dpi->num_copy_templates = 0;
4144 d_count_templates_scopes (&dpi->num_copy_templates,
4145 &dpi->num_saved_scopes, dc);
4146 dpi->num_copy_templates *= dpi->num_saved_scopes;
4148 dpi->current_template = NULL;
4151 /* Indicate that an error occurred during printing, and test for error. */
4154 d_print_error (struct d_print_info *dpi)
4156 dpi->demangle_failure = 1;
4160 d_print_saw_error (struct d_print_info *dpi)
4162 return dpi->demangle_failure != 0;
4165 /* Flush buffered characters to the callback. */
4168 d_print_flush (struct d_print_info *dpi)
4170 dpi->buf[dpi->len] = '\0';
4171 dpi->callback (dpi->buf, dpi->len, dpi->opaque);
4176 /* Append characters and buffers for printing. */
4179 d_append_char (struct d_print_info *dpi, char c)
4181 if (dpi->len == sizeof (dpi->buf) - 1)
4182 d_print_flush (dpi);
4184 dpi->buf[dpi->len++] = c;
4189 d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
4193 for (i = 0; i < l; i++)
4194 d_append_char (dpi, s[i]);
4198 d_append_string (struct d_print_info *dpi, const char *s)
4200 d_append_buffer (dpi, s, strlen (s));
4204 d_append_num (struct d_print_info *dpi, int l)
4207 sprintf (buf,"%d", l);
4208 d_append_string (dpi, buf);
4212 d_last_char (struct d_print_info *dpi)
4214 return dpi->last_char;
4217 /* Turn components into a human readable string. OPTIONS is the
4218 options bits passed to the demangler. DC is the tree to print.
4219 CALLBACK is a function to call to flush demangled string segments
4220 as they fill the intermediate buffer, and OPAQUE is a generalized
4221 callback argument. On success, this returns 1. On failure,
4222 it returns 0, indicating a bad parse. It does not use heap
4223 memory to build an output string, so cannot encounter memory
4224 allocation failure. */
4226 CP_STATIC_IF_GLIBCPP_V3
4228 cplus_demangle_print_callback (int options,
4229 const struct demangle_component *dc,
4230 demangle_callbackref callback, void *opaque)
4232 struct d_print_info dpi;
4234 d_print_init (&dpi, callback, opaque, dc);
4237 #ifdef CP_DYNAMIC_ARRAYS
4238 /* Avoid zero-length VLAs, which are prohibited by the C99 standard
4239 and flagged as errors by Address Sanitizer. */
4240 __extension__ struct d_saved_scope scopes[(dpi.num_saved_scopes > 0)
4241 ? dpi.num_saved_scopes : 1];
4242 __extension__ struct d_print_template temps[(dpi.num_copy_templates > 0)
4243 ? dpi.num_copy_templates : 1];
4245 dpi.saved_scopes = scopes;
4246 dpi.copy_templates = temps;
4248 dpi.saved_scopes = alloca (dpi.num_saved_scopes
4249 * sizeof (*dpi.saved_scopes));
4250 dpi.copy_templates = alloca (dpi.num_copy_templates
4251 * sizeof (*dpi.copy_templates));
4254 d_print_comp (&dpi, options, dc);
4257 d_print_flush (&dpi);
4259 return ! d_print_saw_error (&dpi);
4262 /* Turn components into a human readable string. OPTIONS is the
4263 options bits passed to the demangler. DC is the tree to print.
4264 ESTIMATE is a guess at the length of the result. This returns a
4265 string allocated by malloc, or NULL on error. On success, this
4266 sets *PALC to the size of the allocated buffer. On failure, this
4267 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
4270 CP_STATIC_IF_GLIBCPP_V3
4272 cplus_demangle_print (int options, const struct demangle_component *dc,
4273 int estimate, size_t *palc)
4275 struct d_growable_string dgs;
4277 d_growable_string_init (&dgs, estimate);
4279 if (! cplus_demangle_print_callback (options, dc,
4280 d_growable_string_callback_adapter,
4288 *palc = dgs.allocation_failure ? 1 : dgs.alc;
4292 /* Returns the I'th element of the template arglist ARGS, or NULL on
4293 failure. If I is negative, return the entire arglist. */
4295 static struct demangle_component *
4296 d_index_template_argument (struct demangle_component *args, int i)
4298 struct demangle_component *a;
4301 /* Print the whole argument pack. */
4308 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4314 if (i != 0 || a == NULL)
4320 /* Returns the template argument from the current context indicated by DC,
4321 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
4323 static struct demangle_component *
4324 d_lookup_template_argument (struct d_print_info *dpi,
4325 const struct demangle_component *dc)
4327 if (dpi->templates == NULL)
4329 d_print_error (dpi);
4333 return d_index_template_argument
4334 (d_right (dpi->templates->template_decl),
4335 dc->u.s_number.number);
4338 /* Returns a template argument pack used in DC (any will do), or NULL. */
4340 static struct demangle_component *
4341 d_find_pack (struct d_print_info *dpi,
4342 const struct demangle_component *dc)
4344 struct demangle_component *a;
4350 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
4351 a = d_lookup_template_argument (dpi, dc);
4352 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4356 case DEMANGLE_COMPONENT_PACK_EXPANSION:
4359 case DEMANGLE_COMPONENT_LAMBDA:
4360 case DEMANGLE_COMPONENT_NAME:
4361 case DEMANGLE_COMPONENT_TAGGED_NAME:
4362 case DEMANGLE_COMPONENT_OPERATOR:
4363 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
4364 case DEMANGLE_COMPONENT_SUB_STD:
4365 case DEMANGLE_COMPONENT_CHARACTER:
4366 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
4367 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
4368 case DEMANGLE_COMPONENT_FIXED_TYPE:
4369 case DEMANGLE_COMPONENT_DEFAULT_ARG:
4370 case DEMANGLE_COMPONENT_NUMBER:
4373 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
4374 return d_find_pack (dpi, dc->u.s_extended_operator.name);
4375 case DEMANGLE_COMPONENT_CTOR:
4376 return d_find_pack (dpi, dc->u.s_ctor.name);
4377 case DEMANGLE_COMPONENT_DTOR:
4378 return d_find_pack (dpi, dc->u.s_dtor.name);
4381 a = d_find_pack (dpi, d_left (dc));
4384 return d_find_pack (dpi, d_right (dc));
4388 /* Returns the length of the template argument pack DC. */
4391 d_pack_length (const struct demangle_component *dc)
4394 while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
4395 && d_left (dc) != NULL)
4403 /* Returns the number of template args in DC, expanding any pack expansions
4407 d_args_length (struct d_print_info *dpi, const struct demangle_component *dc)
4410 for (; dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST;
4413 struct demangle_component *elt = d_left (dc);
4416 if (elt->type == DEMANGLE_COMPONENT_PACK_EXPANSION)
4418 struct demangle_component *a = d_find_pack (dpi, d_left (elt));
4419 count += d_pack_length (a);
4427 /* DC is a component of a mangled expression. Print it, wrapped in parens
4431 d_print_subexpr (struct d_print_info *dpi, int options,
4432 const struct demangle_component *dc)
4435 if (dc->type == DEMANGLE_COMPONENT_NAME
4436 || dc->type == DEMANGLE_COMPONENT_QUAL_NAME
4437 || dc->type == DEMANGLE_COMPONENT_INITIALIZER_LIST
4438 || dc->type == DEMANGLE_COMPONENT_FUNCTION_PARAM)
4441 d_append_char (dpi, '(');
4442 d_print_comp (dpi, options, dc);
4444 d_append_char (dpi, ')');
4447 /* Save the current scope. */
4450 d_save_scope (struct d_print_info *dpi,
4451 const struct demangle_component *container)
4453 struct d_saved_scope *scope;
4454 struct d_print_template *src, **link;
4456 if (dpi->next_saved_scope >= dpi->num_saved_scopes)
4458 d_print_error (dpi);
4461 scope = &dpi->saved_scopes[dpi->next_saved_scope];
4462 dpi->next_saved_scope++;
4464 scope->container = container;
4465 link = &scope->templates;
4467 for (src = dpi->templates; src != NULL; src = src->next)
4469 struct d_print_template *dst;
4471 if (dpi->next_copy_template >= dpi->num_copy_templates)
4473 d_print_error (dpi);
4476 dst = &dpi->copy_templates[dpi->next_copy_template];
4477 dpi->next_copy_template++;
4479 dst->template_decl = src->template_decl;
4487 /* Attempt to locate a previously saved scope. Returns NULL if no
4488 corresponding saved scope was found. */
4490 static struct d_saved_scope *
4491 d_get_saved_scope (struct d_print_info *dpi,
4492 const struct demangle_component *container)
4496 for (i = 0; i < dpi->next_saved_scope; i++)
4497 if (dpi->saved_scopes[i].container == container)
4498 return &dpi->saved_scopes[i];
4503 /* If DC is a C++17 fold-expression, print it and return true; otherwise
4507 d_maybe_print_fold_expression (struct d_print_info *dpi, int options,
4508 const struct demangle_component *dc)
4510 const struct demangle_component *ops, *operator_, *op1, *op2;
4513 const char *fold_code = d_left (dc)->u.s_operator.op->code;
4514 if (fold_code[0] != 'f')
4518 operator_ = d_left (ops);
4519 op1 = d_right (ops);
4521 if (op1->type == DEMANGLE_COMPONENT_TRINARY_ARG2)
4523 op2 = d_right (op1);
4527 /* Print the whole pack. */
4528 save_idx = dpi->pack_index;
4529 dpi->pack_index = -1;
4531 switch (fold_code[1])
4533 /* Unary left fold, (... + X). */
4535 d_append_string (dpi, "(...");
4536 d_print_expr_op (dpi, options, operator_);
4537 d_print_subexpr (dpi, options, op1);
4538 d_append_char (dpi, ')');
4541 /* Unary right fold, (X + ...). */
4543 d_append_char (dpi, '(');
4544 d_print_subexpr (dpi, options, op1);
4545 d_print_expr_op (dpi, options, operator_);
4546 d_append_string (dpi, "...)");
4549 /* Binary left fold, (42 + ... + X). */
4551 /* Binary right fold, (X + ... + 42). */
4553 d_append_char (dpi, '(');
4554 d_print_subexpr (dpi, options, op1);
4555 d_print_expr_op (dpi, options, operator_);
4556 d_append_string (dpi, "...");
4557 d_print_expr_op (dpi, options, operator_);
4558 d_print_subexpr (dpi, options, op2);
4559 d_append_char (dpi, ')');
4563 dpi->pack_index = save_idx;
4567 /* Subroutine to handle components. */
4570 d_print_comp_inner (struct d_print_info *dpi, int options,
4571 const struct demangle_component *dc)
4573 /* Magic variable to let reference smashing skip over the next modifier
4574 without needing to modify *dc. */
4575 const struct demangle_component *mod_inner = NULL;
4577 /* Variable used to store the current templates while a previously
4578 captured scope is used. */
4579 struct d_print_template *saved_templates;
4581 /* Nonzero if templates have been stored in the above variable. */
4582 int need_template_restore = 0;
4586 d_print_error (dpi);
4589 if (d_print_saw_error (dpi))
4594 case DEMANGLE_COMPONENT_NAME:
4595 if ((options & DMGL_JAVA) == 0)
4596 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
4598 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
4601 case DEMANGLE_COMPONENT_TAGGED_NAME:
4602 d_print_comp (dpi, options, d_left (dc));
4603 d_append_string (dpi, "[abi:");
4604 d_print_comp (dpi, options, d_right (dc));
4605 d_append_char (dpi, ']');
4608 case DEMANGLE_COMPONENT_QUAL_NAME:
4609 case DEMANGLE_COMPONENT_LOCAL_NAME:
4610 d_print_comp (dpi, options, d_left (dc));
4611 if ((options & DMGL_JAVA) == 0)
4612 d_append_string (dpi, "::");
4614 d_append_char (dpi, '.');
4616 struct demangle_component *local_name = d_right (dc);
4617 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
4619 d_append_string (dpi, "{default arg#");
4620 d_append_num (dpi, local_name->u.s_unary_num.num + 1);
4621 d_append_string (dpi, "}::");
4622 local_name = local_name->u.s_unary_num.sub;
4624 d_print_comp (dpi, options, local_name);
4628 case DEMANGLE_COMPONENT_TYPED_NAME:
4630 struct d_print_mod *hold_modifiers;
4631 struct demangle_component *typed_name;
4632 struct d_print_mod adpm[4];
4634 struct d_print_template dpt;
4636 /* Pass the name down to the type so that it can be printed in
4637 the right place for the type. We also have to pass down
4638 any CV-qualifiers, which apply to the this parameter. */
4639 hold_modifiers = dpi->modifiers;
4642 typed_name = d_left (dc);
4643 while (typed_name != NULL)
4645 if (i >= sizeof adpm / sizeof adpm[0])
4647 d_print_error (dpi);
4651 adpm[i].next = dpi->modifiers;
4652 dpi->modifiers = &adpm[i];
4653 adpm[i].mod = typed_name;
4654 adpm[i].printed = 0;
4655 adpm[i].templates = dpi->templates;
4658 if (!is_fnqual_component_type (typed_name->type))
4661 typed_name = d_left (typed_name);
4664 if (typed_name == NULL)
4666 d_print_error (dpi);
4670 /* If typed_name is a template, then it applies to the
4671 function type as well. */
4672 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
4674 dpt.next = dpi->templates;
4675 dpi->templates = &dpt;
4676 dpt.template_decl = typed_name;
4679 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
4680 there may be CV-qualifiers on its right argument which
4681 really apply here; this happens when parsing a class which
4682 is local to a function. */
4683 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
4685 struct demangle_component *local_name;
4687 local_name = d_right (typed_name);
4688 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
4689 local_name = local_name->u.s_unary_num.sub;
4690 if (local_name == NULL)
4692 d_print_error (dpi);
4695 while (is_fnqual_component_type (local_name->type))
4697 if (i >= sizeof adpm / sizeof adpm[0])
4699 d_print_error (dpi);
4703 adpm[i] = adpm[i - 1];
4704 adpm[i].next = &adpm[i - 1];
4705 dpi->modifiers = &adpm[i];
4707 adpm[i - 1].mod = local_name;
4708 adpm[i - 1].printed = 0;
4709 adpm[i - 1].templates = dpi->templates;
4712 local_name = d_left (local_name);
4716 d_print_comp (dpi, options, d_right (dc));
4718 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
4719 dpi->templates = dpt.next;
4721 /* If the modifiers didn't get printed by the type, print them
4726 if (! adpm[i].printed)
4728 d_append_char (dpi, ' ');
4729 d_print_mod (dpi, options, adpm[i].mod);
4733 dpi->modifiers = hold_modifiers;
4738 case DEMANGLE_COMPONENT_TEMPLATE:
4740 struct d_print_mod *hold_dpm;
4741 struct demangle_component *dcl;
4742 const struct demangle_component *hold_current;
4744 /* This template may need to be referenced by a cast operator
4745 contained in its subtree. */
4746 hold_current = dpi->current_template;
4747 dpi->current_template = dc;
4749 /* Don't push modifiers into a template definition. Doing so
4750 could give the wrong definition for a template argument.
4751 Instead, treat the template essentially as a name. */
4753 hold_dpm = dpi->modifiers;
4754 dpi->modifiers = NULL;
4758 if ((options & DMGL_JAVA) != 0
4759 && dcl->type == DEMANGLE_COMPONENT_NAME
4760 && dcl->u.s_name.len == 6
4761 && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
4763 /* Special-case Java arrays, so that JArray<TYPE> appears
4764 instead as TYPE[]. */
4766 d_print_comp (dpi, options, d_right (dc));
4767 d_append_string (dpi, "[]");
4771 d_print_comp (dpi, options, dcl);
4772 if (d_last_char (dpi) == '<')
4773 d_append_char (dpi, ' ');
4774 d_append_char (dpi, '<');
4775 d_print_comp (dpi, options, d_right (dc));
4776 /* Avoid generating two consecutive '>' characters, to avoid
4777 the C++ syntactic ambiguity. */
4778 if (d_last_char (dpi) == '>')
4779 d_append_char (dpi, ' ');
4780 d_append_char (dpi, '>');
4783 dpi->modifiers = hold_dpm;
4784 dpi->current_template = hold_current;
4789 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
4790 if (dpi->is_lambda_arg)
4792 /* Show the template parm index, as that's how g++ displays
4793 these, and future proofs us against potential
4794 '[]<typename T> (T *a, T *b) {...}'. */
4795 d_append_buffer (dpi, "auto:", 5);
4796 d_append_num (dpi, dc->u.s_number.number + 1);
4800 struct d_print_template *hold_dpt;
4801 struct demangle_component *a = d_lookup_template_argument (dpi, dc);
4803 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4804 a = d_index_template_argument (a, dpi->pack_index);
4808 d_print_error (dpi);
4812 /* While processing this parameter, we need to pop the list
4813 of templates. This is because the template parameter may
4814 itself be a reference to a parameter of an outer
4817 hold_dpt = dpi->templates;
4818 dpi->templates = hold_dpt->next;
4820 d_print_comp (dpi, options, a);
4822 dpi->templates = hold_dpt;
4826 case DEMANGLE_COMPONENT_CTOR:
4827 d_print_comp (dpi, options, dc->u.s_ctor.name);
4830 case DEMANGLE_COMPONENT_DTOR:
4831 d_append_char (dpi, '~');
4832 d_print_comp (dpi, options, dc->u.s_dtor.name);
4835 case DEMANGLE_COMPONENT_VTABLE:
4836 d_append_string (dpi, "vtable for ");
4837 d_print_comp (dpi, options, d_left (dc));
4840 case DEMANGLE_COMPONENT_VTT:
4841 d_append_string (dpi, "VTT for ");
4842 d_print_comp (dpi, options, d_left (dc));
4845 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
4846 d_append_string (dpi, "construction vtable for ");
4847 d_print_comp (dpi, options, d_left (dc));
4848 d_append_string (dpi, "-in-");
4849 d_print_comp (dpi, options, d_right (dc));
4852 case DEMANGLE_COMPONENT_TYPEINFO:
4853 d_append_string (dpi, "typeinfo for ");
4854 d_print_comp (dpi, options, d_left (dc));
4857 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
4858 d_append_string (dpi, "typeinfo name for ");
4859 d_print_comp (dpi, options, d_left (dc));
4862 case DEMANGLE_COMPONENT_TYPEINFO_FN:
4863 d_append_string (dpi, "typeinfo fn for ");
4864 d_print_comp (dpi, options, d_left (dc));
4867 case DEMANGLE_COMPONENT_THUNK:
4868 d_append_string (dpi, "non-virtual thunk to ");
4869 d_print_comp (dpi, options, d_left (dc));
4872 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
4873 d_append_string (dpi, "virtual thunk to ");
4874 d_print_comp (dpi, options, d_left (dc));
4877 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
4878 d_append_string (dpi, "covariant return thunk to ");
4879 d_print_comp (dpi, options, d_left (dc));
4882 case DEMANGLE_COMPONENT_JAVA_CLASS:
4883 d_append_string (dpi, "java Class for ");
4884 d_print_comp (dpi, options, d_left (dc));
4887 case DEMANGLE_COMPONENT_GUARD:
4888 d_append_string (dpi, "guard variable for ");
4889 d_print_comp (dpi, options, d_left (dc));
4892 case DEMANGLE_COMPONENT_TLS_INIT:
4893 d_append_string (dpi, "TLS init function for ");
4894 d_print_comp (dpi, options, d_left (dc));
4897 case DEMANGLE_COMPONENT_TLS_WRAPPER:
4898 d_append_string (dpi, "TLS wrapper function for ");
4899 d_print_comp (dpi, options, d_left (dc));
4902 case DEMANGLE_COMPONENT_REFTEMP:
4903 d_append_string (dpi, "reference temporary #");
4904 d_print_comp (dpi, options, d_right (dc));
4905 d_append_string (dpi, " for ");
4906 d_print_comp (dpi, options, d_left (dc));
4909 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
4910 d_append_string (dpi, "hidden alias for ");
4911 d_print_comp (dpi, options, d_left (dc));
4914 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
4915 d_append_string (dpi, "transaction clone for ");
4916 d_print_comp (dpi, options, d_left (dc));
4919 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
4920 d_append_string (dpi, "non-transaction clone for ");
4921 d_print_comp (dpi, options, d_left (dc));
4924 case DEMANGLE_COMPONENT_SUB_STD:
4925 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
4928 case DEMANGLE_COMPONENT_RESTRICT:
4929 case DEMANGLE_COMPONENT_VOLATILE:
4930 case DEMANGLE_COMPONENT_CONST:
4932 struct d_print_mod *pdpm;
4934 /* When printing arrays, it's possible to have cases where the
4935 same CV-qualifier gets pushed on the stack multiple times.
4936 We only need to print it once. */
4938 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
4940 if (! pdpm->printed)
4942 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
4943 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
4944 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
4946 if (pdpm->mod->type == dc->type)
4948 d_print_comp (dpi, options, d_left (dc));
4956 case DEMANGLE_COMPONENT_REFERENCE:
4957 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4959 /* Handle reference smashing: & + && = &. */
4960 const struct demangle_component *sub = d_left (dc);
4961 if (!dpi->is_lambda_arg
4962 && sub->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
4964 struct d_saved_scope *scope = d_get_saved_scope (dpi, sub);
4965 struct demangle_component *a;
4969 /* This is the first time SUB has been traversed.
4970 We need to capture the current templates so
4971 they can be restored if SUB is reentered as a
4973 d_save_scope (dpi, sub);
4974 if (d_print_saw_error (dpi))
4979 const struct d_component_stack *dcse;
4980 int found_self_or_parent = 0;
4982 /* This traversal is reentering SUB as a substition.
4983 If we are not beneath SUB or DC in the tree then we
4984 need to restore SUB's template stack temporarily. */
4985 for (dcse = dpi->component_stack; dcse != NULL;
4986 dcse = dcse->parent)
4990 && dcse != dpi->component_stack))
4992 found_self_or_parent = 1;
4997 if (!found_self_or_parent)
4999 saved_templates = dpi->templates;
5000 dpi->templates = scope->templates;
5001 need_template_restore = 1;
5005 a = d_lookup_template_argument (dpi, sub);
5006 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
5007 a = d_index_template_argument (a, dpi->pack_index);
5011 if (need_template_restore)
5012 dpi->templates = saved_templates;
5014 d_print_error (dpi);
5021 if (sub->type == DEMANGLE_COMPONENT_REFERENCE
5022 || sub->type == dc->type)
5024 else if (sub->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE)
5025 mod_inner = d_left (sub);
5029 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5030 case DEMANGLE_COMPONENT_POINTER:
5031 case DEMANGLE_COMPONENT_COMPLEX:
5032 case DEMANGLE_COMPONENT_IMAGINARY:
5033 FNQUAL_COMPONENT_CASE:
5036 /* We keep a list of modifiers on the stack. */
5037 struct d_print_mod dpm;
5039 dpm.next = dpi->modifiers;
5040 dpi->modifiers = &dpm;
5043 dpm.templates = dpi->templates;
5046 mod_inner = d_left (dc);
5048 d_print_comp (dpi, options, mod_inner);
5050 /* If the modifier didn't get printed by the type, print it
5053 d_print_mod (dpi, options, dc);
5055 dpi->modifiers = dpm.next;
5057 if (need_template_restore)
5058 dpi->templates = saved_templates;
5063 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
5064 if ((options & DMGL_JAVA) == 0)
5065 d_append_buffer (dpi, dc->u.s_builtin.type->name,
5066 dc->u.s_builtin.type->len);
5068 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
5069 dc->u.s_builtin.type->java_len);
5072 case DEMANGLE_COMPONENT_VENDOR_TYPE:
5073 d_print_comp (dpi, options, d_left (dc));
5076 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
5078 if ((options & DMGL_RET_POSTFIX) != 0)
5079 d_print_function_type (dpi,
5080 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
5081 dc, dpi->modifiers);
5083 /* Print return type if present */
5084 if (d_left (dc) != NULL && (options & DMGL_RET_POSTFIX) != 0)
5085 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
5087 else if (d_left (dc) != NULL && (options & DMGL_RET_DROP) == 0)
5089 struct d_print_mod dpm;
5091 /* We must pass this type down as a modifier in order to
5092 print it in the right location. */
5093 dpm.next = dpi->modifiers;
5094 dpi->modifiers = &dpm;
5097 dpm.templates = dpi->templates;
5099 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
5102 dpi->modifiers = dpm.next;
5107 /* In standard prefix notation, there is a space between the
5108 return type and the function signature. */
5109 if ((options & DMGL_RET_POSTFIX) == 0)
5110 d_append_char (dpi, ' ');
5113 if ((options & DMGL_RET_POSTFIX) == 0)
5114 d_print_function_type (dpi,
5115 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
5116 dc, dpi->modifiers);
5121 case DEMANGLE_COMPONENT_ARRAY_TYPE:
5123 struct d_print_mod *hold_modifiers;
5124 struct d_print_mod adpm[4];
5126 struct d_print_mod *pdpm;
5128 /* We must pass this type down as a modifier in order to print
5129 multi-dimensional arrays correctly. If the array itself is
5130 CV-qualified, we act as though the element type were
5131 CV-qualified. We do this by copying the modifiers down
5132 rather than fiddling pointers, so that we don't wind up
5133 with a d_print_mod higher on the stack pointing into our
5134 stack frame after we return. */
5136 hold_modifiers = dpi->modifiers;
5138 adpm[0].next = hold_modifiers;
5139 dpi->modifiers = &adpm[0];
5141 adpm[0].printed = 0;
5142 adpm[0].templates = dpi->templates;
5145 pdpm = hold_modifiers;
5147 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
5148 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
5149 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
5151 if (! pdpm->printed)
5153 if (i >= sizeof adpm / sizeof adpm[0])
5155 d_print_error (dpi);
5160 adpm[i].next = dpi->modifiers;
5161 dpi->modifiers = &adpm[i];
5169 d_print_comp (dpi, options, d_right (dc));
5171 dpi->modifiers = hold_modifiers;
5173 if (adpm[0].printed)
5179 d_print_mod (dpi, options, adpm[i].mod);
5182 d_print_array_type (dpi, options, dc, dpi->modifiers);
5187 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5188 case DEMANGLE_COMPONENT_VECTOR_TYPE:
5190 struct d_print_mod dpm;
5192 dpm.next = dpi->modifiers;
5193 dpi->modifiers = &dpm;
5196 dpm.templates = dpi->templates;
5198 d_print_comp (dpi, options, d_right (dc));
5200 /* If the modifier didn't get printed by the type, print it
5203 d_print_mod (dpi, options, dc);
5205 dpi->modifiers = dpm.next;
5210 case DEMANGLE_COMPONENT_FIXED_TYPE:
5211 if (dc->u.s_fixed.sat)
5212 d_append_string (dpi, "_Sat ");
5213 /* Don't print "int _Accum". */
5214 if (dc->u.s_fixed.length->u.s_builtin.type
5215 != &cplus_demangle_builtin_types['i'-'a'])
5217 d_print_comp (dpi, options, dc->u.s_fixed.length);
5218 d_append_char (dpi, ' ');
5220 if (dc->u.s_fixed.accum)
5221 d_append_string (dpi, "_Accum");
5223 d_append_string (dpi, "_Fract");
5226 case DEMANGLE_COMPONENT_ARGLIST:
5227 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
5228 if (d_left (dc) != NULL)
5229 d_print_comp (dpi, options, d_left (dc));
5230 if (d_right (dc) != NULL)
5233 unsigned long int flush_count;
5234 /* Make sure ", " isn't flushed by d_append_string, otherwise
5235 dpi->len -= 2 wouldn't work. */
5236 if (dpi->len >= sizeof (dpi->buf) - 2)
5237 d_print_flush (dpi);
5238 d_append_string (dpi, ", ");
5240 flush_count = dpi->flush_count;
5241 d_print_comp (dpi, options, d_right (dc));
5242 /* If that didn't print anything (which can happen with empty
5243 template argument packs), remove the comma and space. */
5244 if (dpi->flush_count == flush_count && dpi->len == len)
5249 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
5251 struct demangle_component *type = d_left (dc);
5252 struct demangle_component *list = d_right (dc);
5255 d_print_comp (dpi, options, type);
5256 d_append_char (dpi, '{');
5257 d_print_comp (dpi, options, list);
5258 d_append_char (dpi, '}');
5262 case DEMANGLE_COMPONENT_OPERATOR:
5264 const struct demangle_operator_info *op = dc->u.s_operator.op;
5267 d_append_string (dpi, "operator");
5268 /* Add a space before new/delete. */
5269 if (IS_LOWER (op->name[0]))
5270 d_append_char (dpi, ' ');
5271 /* Omit a trailing space. */
5272 if (op->name[len-1] == ' ')
5274 d_append_buffer (dpi, op->name, len);
5278 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
5279 d_append_string (dpi, "operator ");
5280 d_print_comp (dpi, options, dc->u.s_extended_operator.name);
5283 case DEMANGLE_COMPONENT_CONVERSION:
5284 d_append_string (dpi, "operator ");
5285 d_print_conversion (dpi, options, dc);
5288 case DEMANGLE_COMPONENT_NULLARY:
5289 d_print_expr_op (dpi, options, d_left (dc));
5292 case DEMANGLE_COMPONENT_UNARY:
5294 struct demangle_component *op = d_left (dc);
5295 struct demangle_component *operand = d_right (dc);
5296 const char *code = NULL;
5298 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
5300 code = op->u.s_operator.op->code;
5301 if (!strcmp (code, "ad"))
5303 /* Don't print the argument list for the address of a
5305 if (operand->type == DEMANGLE_COMPONENT_TYPED_NAME
5306 && d_left (operand)->type == DEMANGLE_COMPONENT_QUAL_NAME
5307 && d_right (operand)->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
5308 operand = d_left (operand);
5310 if (operand->type == DEMANGLE_COMPONENT_BINARY_ARGS)
5312 /* This indicates a suffix operator. */
5313 operand = d_left (operand);
5314 d_print_subexpr (dpi, options, operand);
5315 d_print_expr_op (dpi, options, op);
5320 /* For sizeof..., just print the pack length. */
5321 if (code && !strcmp (code, "sZ"))
5323 struct demangle_component *a = d_find_pack (dpi, operand);
5324 int len = d_pack_length (a);
5325 d_append_num (dpi, len);
5328 else if (code && !strcmp (code, "sP"))
5330 int len = d_args_length (dpi, operand);
5331 d_append_num (dpi, len);
5335 if (op->type != DEMANGLE_COMPONENT_CAST)
5336 d_print_expr_op (dpi, options, op);
5339 d_append_char (dpi, '(');
5340 d_print_cast (dpi, options, op);
5341 d_append_char (dpi, ')');
5343 if (code && !strcmp (code, "gs"))
5344 /* Avoid parens after '::'. */
5345 d_print_comp (dpi, options, operand);
5346 else if (code && !strcmp (code, "st"))
5347 /* Always print parens for sizeof (type). */
5349 d_append_char (dpi, '(');
5350 d_print_comp (dpi, options, operand);
5351 d_append_char (dpi, ')');
5354 d_print_subexpr (dpi, options, operand);
5358 case DEMANGLE_COMPONENT_BINARY:
5359 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
5361 d_print_error (dpi);
5365 if (op_is_new_cast (d_left (dc)))
5367 d_print_expr_op (dpi, options, d_left (dc));
5368 d_append_char (dpi, '<');
5369 d_print_comp (dpi, options, d_left (d_right (dc)));
5370 d_append_string (dpi, ">(");
5371 d_print_comp (dpi, options, d_right (d_right (dc)));
5372 d_append_char (dpi, ')');
5376 if (d_maybe_print_fold_expression (dpi, options, dc))
5379 /* We wrap an expression which uses the greater-than operator in
5380 an extra layer of parens so that it does not get confused
5381 with the '>' which ends the template parameters. */
5382 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
5383 && d_left (dc)->u.s_operator.op->len == 1
5384 && d_left (dc)->u.s_operator.op->name[0] == '>')
5385 d_append_char (dpi, '(');
5387 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") == 0
5388 && d_left (d_right (dc))->type == DEMANGLE_COMPONENT_TYPED_NAME)
5390 /* Function call used in an expression should not have printed types
5391 of the function arguments. Values of the function arguments still
5392 get printed below. */
5394 const struct demangle_component *func = d_left (d_right (dc));
5396 if (d_right (func)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
5397 d_print_error (dpi);
5398 d_print_subexpr (dpi, options, d_left (func));
5401 d_print_subexpr (dpi, options, d_left (d_right (dc)));
5402 if (strcmp (d_left (dc)->u.s_operator.op->code, "ix") == 0)
5404 d_append_char (dpi, '[');
5405 d_print_comp (dpi, options, d_right (d_right (dc)));
5406 d_append_char (dpi, ']');
5410 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") != 0)
5411 d_print_expr_op (dpi, options, d_left (dc));
5412 d_print_subexpr (dpi, options, d_right (d_right (dc)));
5415 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
5416 && d_left (dc)->u.s_operator.op->len == 1
5417 && d_left (dc)->u.s_operator.op->name[0] == '>')
5418 d_append_char (dpi, ')');
5422 case DEMANGLE_COMPONENT_BINARY_ARGS:
5423 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
5424 d_print_error (dpi);
5427 case DEMANGLE_COMPONENT_TRINARY:
5428 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
5429 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
5431 d_print_error (dpi);
5434 if (d_maybe_print_fold_expression (dpi, options, dc))
5437 struct demangle_component *op = d_left (dc);
5438 struct demangle_component *first = d_left (d_right (dc));
5439 struct demangle_component *second = d_left (d_right (d_right (dc)));
5440 struct demangle_component *third = d_right (d_right (d_right (dc)));
5442 if (!strcmp (op->u.s_operator.op->code, "qu"))
5444 d_print_subexpr (dpi, options, first);
5445 d_print_expr_op (dpi, options, op);
5446 d_print_subexpr (dpi, options, second);
5447 d_append_string (dpi, " : ");
5448 d_print_subexpr (dpi, options, third);
5452 d_append_string (dpi, "new ");
5453 if (d_left (first) != NULL)
5455 d_print_subexpr (dpi, options, first);
5456 d_append_char (dpi, ' ');
5458 d_print_comp (dpi, options, second);
5460 d_print_subexpr (dpi, options, third);
5465 case DEMANGLE_COMPONENT_TRINARY_ARG1:
5466 case DEMANGLE_COMPONENT_TRINARY_ARG2:
5467 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
5468 d_print_error (dpi);
5471 case DEMANGLE_COMPONENT_LITERAL:
5472 case DEMANGLE_COMPONENT_LITERAL_NEG:
5474 enum d_builtin_type_print tp;
5476 /* For some builtin types, produce simpler output. */
5477 tp = D_PRINT_DEFAULT;
5478 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
5480 tp = d_left (dc)->u.s_builtin.type->print;
5484 case D_PRINT_UNSIGNED:
5486 case D_PRINT_UNSIGNED_LONG:
5487 case D_PRINT_LONG_LONG:
5488 case D_PRINT_UNSIGNED_LONG_LONG:
5489 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
5491 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
5492 d_append_char (dpi, '-');
5493 d_print_comp (dpi, options, d_right (dc));
5498 case D_PRINT_UNSIGNED:
5499 d_append_char (dpi, 'u');
5502 d_append_char (dpi, 'l');
5504 case D_PRINT_UNSIGNED_LONG:
5505 d_append_string (dpi, "ul");
5507 case D_PRINT_LONG_LONG:
5508 d_append_string (dpi, "ll");
5510 case D_PRINT_UNSIGNED_LONG_LONG:
5511 d_append_string (dpi, "ull");
5519 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
5520 && d_right (dc)->u.s_name.len == 1
5521 && dc->type == DEMANGLE_COMPONENT_LITERAL)
5523 switch (d_right (dc)->u.s_name.s[0])
5526 d_append_string (dpi, "false");
5529 d_append_string (dpi, "true");
5542 d_append_char (dpi, '(');
5543 d_print_comp (dpi, options, d_left (dc));
5544 d_append_char (dpi, ')');
5545 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
5546 d_append_char (dpi, '-');
5547 if (tp == D_PRINT_FLOAT)
5548 d_append_char (dpi, '[');
5549 d_print_comp (dpi, options, d_right (dc));
5550 if (tp == D_PRINT_FLOAT)
5551 d_append_char (dpi, ']');
5555 case DEMANGLE_COMPONENT_NUMBER:
5556 d_append_num (dpi, dc->u.s_number.number);
5559 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
5560 d_append_string (dpi, "java resource ");
5561 d_print_comp (dpi, options, d_left (dc));
5564 case DEMANGLE_COMPONENT_COMPOUND_NAME:
5565 d_print_comp (dpi, options, d_left (dc));
5566 d_print_comp (dpi, options, d_right (dc));
5569 case DEMANGLE_COMPONENT_CHARACTER:
5570 d_append_char (dpi, dc->u.s_character.character);
5573 case DEMANGLE_COMPONENT_DECLTYPE:
5574 d_append_string (dpi, "decltype (");
5575 d_print_comp (dpi, options, d_left (dc));
5576 d_append_char (dpi, ')');
5579 case DEMANGLE_COMPONENT_PACK_EXPANSION:
5583 struct demangle_component *a = d_find_pack (dpi, d_left (dc));
5586 /* d_find_pack won't find anything if the only packs involved
5587 in this expansion are function parameter packs; in that
5588 case, just print the pattern and "...". */
5589 d_print_subexpr (dpi, options, d_left (dc));
5590 d_append_string (dpi, "...");
5594 len = d_pack_length (a);
5596 for (i = 0; i < len; ++i)
5598 dpi->pack_index = i;
5599 d_print_comp (dpi, options, dc);
5601 d_append_string (dpi, ", ");
5606 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
5608 long num = dc->u.s_number.number;
5610 d_append_string (dpi, "this");
5613 d_append_string (dpi, "{parm#");
5614 d_append_num (dpi, num);
5615 d_append_char (dpi, '}');
5620 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
5621 d_append_string (dpi, "global constructors keyed to ");
5622 d_print_comp (dpi, options, dc->u.s_binary.left);
5625 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
5626 d_append_string (dpi, "global destructors keyed to ");
5627 d_print_comp (dpi, options, dc->u.s_binary.left);
5630 case DEMANGLE_COMPONENT_LAMBDA:
5631 d_append_string (dpi, "{lambda(");
5632 /* Generic lambda auto parms are mangled as the template type
5634 dpi->is_lambda_arg++;
5635 d_print_comp (dpi, options, dc->u.s_unary_num.sub);
5636 dpi->is_lambda_arg--;
5637 d_append_string (dpi, ")#");
5638 d_append_num (dpi, dc->u.s_unary_num.num + 1);
5639 d_append_char (dpi, '}');
5642 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
5643 d_append_string (dpi, "{unnamed type#");
5644 d_append_num (dpi, dc->u.s_number.number + 1);
5645 d_append_char (dpi, '}');
5648 case DEMANGLE_COMPONENT_CLONE:
5649 d_print_comp (dpi, options, d_left (dc));
5650 d_append_string (dpi, " [clone ");
5651 d_print_comp (dpi, options, d_right (dc));
5652 d_append_char (dpi, ']');
5656 d_print_error (dpi);
5662 d_print_comp (struct d_print_info *dpi, int options,
5663 const struct demangle_component *dc)
5665 struct d_component_stack self;
5668 self.parent = dpi->component_stack;
5669 dpi->component_stack = &self;
5671 d_print_comp_inner (dpi, options, dc);
5673 dpi->component_stack = self.parent;
5676 /* Print a Java dentifier. For Java we try to handle encoded extended
5677 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
5678 so we don't it for C++. Characters are encoded as
5682 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
5688 for (p = name; p < end; ++p)
5699 for (q = p + 3; q < end; ++q)
5705 else if (*q >= 'A' && *q <= 'F')
5706 dig = *q - 'A' + 10;
5707 else if (*q >= 'a' && *q <= 'f')
5708 dig = *q - 'a' + 10;
5714 /* If the Unicode character is larger than 256, we don't try
5715 to deal with it here. FIXME. */
5716 if (q < end && *q == '_' && c < 256)
5718 d_append_char (dpi, c);
5724 d_append_char (dpi, *p);
5728 /* Print a list of modifiers. SUFFIX is 1 if we are printing
5729 qualifiers on this after printing a function. */
5732 d_print_mod_list (struct d_print_info *dpi, int options,
5733 struct d_print_mod *mods, int suffix)
5735 struct d_print_template *hold_dpt;
5737 if (mods == NULL || d_print_saw_error (dpi))
5742 && (is_fnqual_component_type (mods->mod->type))))
5744 d_print_mod_list (dpi, options, mods->next, suffix);
5750 hold_dpt = dpi->templates;
5751 dpi->templates = mods->templates;
5753 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
5755 d_print_function_type (dpi, options, mods->mod, mods->next);
5756 dpi->templates = hold_dpt;
5759 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
5761 d_print_array_type (dpi, options, mods->mod, mods->next);
5762 dpi->templates = hold_dpt;
5765 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
5767 struct d_print_mod *hold_modifiers;
5768 struct demangle_component *dc;
5770 /* When this is on the modifier stack, we have pulled any
5771 qualifiers off the right argument already. Otherwise, we
5772 print it as usual, but don't let the left argument see any
5775 hold_modifiers = dpi->modifiers;
5776 dpi->modifiers = NULL;
5777 d_print_comp (dpi, options, d_left (mods->mod));
5778 dpi->modifiers = hold_modifiers;
5780 if ((options & DMGL_JAVA) == 0)
5781 d_append_string (dpi, "::");
5783 d_append_char (dpi, '.');
5785 dc = d_right (mods->mod);
5787 if (dc->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
5789 d_append_string (dpi, "{default arg#");
5790 d_append_num (dpi, dc->u.s_unary_num.num + 1);
5791 d_append_string (dpi, "}::");
5792 dc = dc->u.s_unary_num.sub;
5795 while (is_fnqual_component_type (dc->type))
5798 d_print_comp (dpi, options, dc);
5800 dpi->templates = hold_dpt;
5804 d_print_mod (dpi, options, mods->mod);
5806 dpi->templates = hold_dpt;
5808 d_print_mod_list (dpi, options, mods->next, suffix);
5811 /* Print a modifier. */
5814 d_print_mod (struct d_print_info *dpi, int options,
5815 const struct demangle_component *mod)
5819 case DEMANGLE_COMPONENT_RESTRICT:
5820 case DEMANGLE_COMPONENT_RESTRICT_THIS:
5821 d_append_string (dpi, " restrict");
5823 case DEMANGLE_COMPONENT_VOLATILE:
5824 case DEMANGLE_COMPONENT_VOLATILE_THIS:
5825 d_append_string (dpi, " volatile");
5827 case DEMANGLE_COMPONENT_CONST:
5828 case DEMANGLE_COMPONENT_CONST_THIS:
5829 d_append_string (dpi, " const");
5831 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
5832 d_append_string (dpi, " transaction_safe");
5834 case DEMANGLE_COMPONENT_NOEXCEPT:
5835 d_append_string (dpi, " noexcept");
5838 d_append_char (dpi, '(');
5839 d_print_comp (dpi, options, d_right (mod));
5840 d_append_char (dpi, ')');
5843 case DEMANGLE_COMPONENT_THROW_SPEC:
5844 d_append_string (dpi, " throw");
5847 d_append_char (dpi, '(');
5848 d_print_comp (dpi, options, d_right (mod));
5849 d_append_char (dpi, ')');
5852 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5853 d_append_char (dpi, ' ');
5854 d_print_comp (dpi, options, d_right (mod));
5856 case DEMANGLE_COMPONENT_POINTER:
5857 /* There is no pointer symbol in Java. */
5858 if ((options & DMGL_JAVA) == 0)
5859 d_append_char (dpi, '*');
5861 case DEMANGLE_COMPONENT_REFERENCE_THIS:
5862 /* For the ref-qualifier, put a space before the &. */
5863 d_append_char (dpi, ' ');
5865 case DEMANGLE_COMPONENT_REFERENCE:
5866 d_append_char (dpi, '&');
5868 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
5869 d_append_char (dpi, ' ');
5871 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
5872 d_append_string (dpi, "&&");
5874 case DEMANGLE_COMPONENT_COMPLEX:
5875 d_append_string (dpi, "complex ");
5877 case DEMANGLE_COMPONENT_IMAGINARY:
5878 d_append_string (dpi, "imaginary ");
5880 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5881 if (d_last_char (dpi) != '(')
5882 d_append_char (dpi, ' ');
5883 d_print_comp (dpi, options, d_left (mod));
5884 d_append_string (dpi, "::*");
5886 case DEMANGLE_COMPONENT_TYPED_NAME:
5887 d_print_comp (dpi, options, d_left (mod));
5889 case DEMANGLE_COMPONENT_VECTOR_TYPE:
5890 d_append_string (dpi, " __vector(");
5891 d_print_comp (dpi, options, d_left (mod));
5892 d_append_char (dpi, ')');
5896 /* Otherwise, we have something that won't go back on the
5897 modifier stack, so we can just print it. */
5898 d_print_comp (dpi, options, mod);
5903 /* Print a function type, except for the return type. */
5906 d_print_function_type (struct d_print_info *dpi, int options,
5907 const struct demangle_component *dc,
5908 struct d_print_mod *mods)
5912 struct d_print_mod *p;
5913 struct d_print_mod *hold_modifiers;
5917 for (p = mods; p != NULL; p = p->next)
5922 switch (p->mod->type)
5924 case DEMANGLE_COMPONENT_POINTER:
5925 case DEMANGLE_COMPONENT_REFERENCE:
5926 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
5929 case DEMANGLE_COMPONENT_RESTRICT:
5930 case DEMANGLE_COMPONENT_VOLATILE:
5931 case DEMANGLE_COMPONENT_CONST:
5932 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5933 case DEMANGLE_COMPONENT_COMPLEX:
5934 case DEMANGLE_COMPONENT_IMAGINARY:
5935 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5939 FNQUAL_COMPONENT_CASE:
5952 if (d_last_char (dpi) != '('
5953 && d_last_char (dpi) != '*')
5956 if (need_space && d_last_char (dpi) != ' ')
5957 d_append_char (dpi, ' ');
5958 d_append_char (dpi, '(');
5961 hold_modifiers = dpi->modifiers;
5962 dpi->modifiers = NULL;
5964 d_print_mod_list (dpi, options, mods, 0);
5967 d_append_char (dpi, ')');
5969 d_append_char (dpi, '(');
5971 if (d_right (dc) != NULL)
5972 d_print_comp (dpi, options, d_right (dc));
5974 d_append_char (dpi, ')');
5976 d_print_mod_list (dpi, options, mods, 1);
5978 dpi->modifiers = hold_modifiers;
5981 /* Print an array type, except for the element type. */
5984 d_print_array_type (struct d_print_info *dpi, int options,
5985 const struct demangle_component *dc,
5986 struct d_print_mod *mods)
5994 struct d_print_mod *p;
5997 for (p = mods; p != NULL; p = p->next)
6001 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
6016 d_append_string (dpi, " (");
6018 d_print_mod_list (dpi, options, mods, 0);
6021 d_append_char (dpi, ')');
6025 d_append_char (dpi, ' ');
6027 d_append_char (dpi, '[');
6029 if (d_left (dc) != NULL)
6030 d_print_comp (dpi, options, d_left (dc));
6032 d_append_char (dpi, ']');
6035 /* Print an operator in an expression. */
6038 d_print_expr_op (struct d_print_info *dpi, int options,
6039 const struct demangle_component *dc)
6041 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
6042 d_append_buffer (dpi, dc->u.s_operator.op->name,
6043 dc->u.s_operator.op->len);
6045 d_print_comp (dpi, options, dc);
6051 d_print_cast (struct d_print_info *dpi, int options,
6052 const struct demangle_component *dc)
6054 d_print_comp (dpi, options, d_left (dc));
6057 /* Print a conversion operator. */
6060 d_print_conversion (struct d_print_info *dpi, int options,
6061 const struct demangle_component *dc)
6063 struct d_print_template dpt;
6065 /* For a conversion operator, we need the template parameters from
6066 the enclosing template in scope for processing the type. */
6067 if (dpi->current_template != NULL)
6069 dpt.next = dpi->templates;
6070 dpi->templates = &dpt;
6071 dpt.template_decl = dpi->current_template;
6074 if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
6076 d_print_comp (dpi, options, d_left (dc));
6077 if (dpi->current_template != NULL)
6078 dpi->templates = dpt.next;
6082 d_print_comp (dpi, options, d_left (d_left (dc)));
6084 /* For a templated cast operator, we need to remove the template
6085 parameters from scope after printing the operator name,
6086 so we need to handle the template printing here. */
6087 if (dpi->current_template != NULL)
6088 dpi->templates = dpt.next;
6090 if (d_last_char (dpi) == '<')
6091 d_append_char (dpi, ' ');
6092 d_append_char (dpi, '<');
6093 d_print_comp (dpi, options, d_right (d_left (dc)));
6094 /* Avoid generating two consecutive '>' characters, to avoid
6095 the C++ syntactic ambiguity. */
6096 if (d_last_char (dpi) == '>')
6097 d_append_char (dpi, ' ');
6098 d_append_char (dpi, '>');
6102 /* Initialize the information structure we use to pass around
6105 CP_STATIC_IF_GLIBCPP_V3
6107 cplus_demangle_init_info (const char *mangled, int options, size_t len,
6111 di->send = mangled + len;
6112 di->options = options;
6116 /* We can not need more components than twice the number of chars in
6117 the mangled string. Most components correspond directly to
6118 chars, but the ARGLIST types are exceptions. */
6119 di->num_comps = 2 * len;
6122 /* Similarly, we can not need more substitutions than there are
6123 chars in the mangled string. */
6128 di->last_name = NULL;
6131 di->is_expression = 0;
6132 di->is_conversion = 0;
6135 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
6136 mangled name, return strings in repeated callback giving the demangled
6137 name. OPTIONS is the usual libiberty demangler options. On success,
6138 this returns 1. On failure, returns 0. */
6141 d_demangle_callback (const char *mangled, int options,
6142 demangle_callbackref callback, void *opaque)
6153 struct demangle_component *dc;
6156 if (mangled[0] == '_' && mangled[1] == 'Z')
6158 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
6159 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
6160 && (mangled[9] == 'D' || mangled[9] == 'I')
6161 && mangled[10] == '_')
6162 type = mangled[9] == 'I' ? DCT_GLOBAL_CTORS : DCT_GLOBAL_DTORS;
6165 if ((options & DMGL_TYPES) == 0)
6170 cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
6173 #ifdef CP_DYNAMIC_ARRAYS
6174 __extension__ struct demangle_component comps[di.num_comps];
6175 __extension__ struct demangle_component *subs[di.num_subs];
6180 di.comps = alloca (di.num_comps * sizeof (*di.comps));
6181 di.subs = alloca (di.num_subs * sizeof (*di.subs));
6187 dc = cplus_demangle_type (&di);
6190 dc = cplus_demangle_mangled_name (&di, 1);
6192 case DCT_GLOBAL_CTORS:
6193 case DCT_GLOBAL_DTORS:
6194 d_advance (&di, 11);
6195 dc = d_make_comp (&di,
6196 (type == DCT_GLOBAL_CTORS
6197 ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
6198 : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS),
6199 d_make_demangle_mangled_name (&di, d_str (&di)),
6201 d_advance (&di, strlen (d_str (&di)));
6204 abort (); /* We have listed all the cases. */
6207 /* If DMGL_PARAMS is set, then if we didn't consume the entire
6208 mangled string, then we didn't successfully demangle it. If
6209 DMGL_PARAMS is not set, we didn't look at the trailing
6211 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
6214 #ifdef CP_DEMANGLE_DEBUG
6218 status = (dc != NULL)
6219 ? cplus_demangle_print_callback (options, dc, callback, opaque)
6226 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
6227 name, return a buffer allocated with malloc holding the demangled
6228 name. OPTIONS is the usual libiberty demangler options. On
6229 success, this sets *PALC to the allocated size of the returned
6230 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
6231 a memory allocation failure, and returns NULL. */
6234 d_demangle (const char *mangled, int options, size_t *palc)
6236 struct d_growable_string dgs;
6239 d_growable_string_init (&dgs, 0);
6241 status = d_demangle_callback (mangled, options,
6242 d_growable_string_callback_adapter, &dgs);
6250 *palc = dgs.allocation_failure ? 1 : dgs.alc;
6254 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
6256 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
6258 /* ia64 ABI-mandated entry point in the C++ runtime library for
6259 performing demangling. MANGLED_NAME is a NUL-terminated character
6260 string containing the name to be demangled.
6262 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
6263 *LENGTH bytes, into which the demangled name is stored. If
6264 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
6265 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
6266 is placed in a region of memory allocated with malloc.
6268 If LENGTH is non-NULL, the length of the buffer containing the
6269 demangled name, is placed in *LENGTH.
6271 The return value is a pointer to the start of the NUL-terminated
6272 demangled name, or NULL if the demangling fails. The caller is
6273 responsible for deallocating this memory using free.
6275 *STATUS is set to one of the following values:
6276 0: The demangling operation succeeded.
6277 -1: A memory allocation failure occurred.
6278 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
6279 -3: One of the arguments is invalid.
6281 The demangling is performed using the C++ ABI mangling rules, with
6285 __cxa_demangle (const char *mangled_name, char *output_buffer,
6286 size_t *length, int *status)
6291 if (mangled_name == NULL)
6298 if (output_buffer != NULL && length == NULL)
6305 demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
6307 if (demangled == NULL)
6319 if (output_buffer == NULL)
6326 if (strlen (demangled) < *length)
6328 strcpy (output_buffer, demangled);
6330 demangled = output_buffer;
6334 free (output_buffer);
6345 extern int __gcclibcxx_demangle_callback (const char *,
6347 (const char *, size_t, void *),
6350 /* Alternative, allocationless entry point in the C++ runtime library
6351 for performing demangling. MANGLED_NAME is a NUL-terminated character
6352 string containing the name to be demangled.
6354 CALLBACK is a callback function, called with demangled string
6355 segments as demangling progresses; it is called at least once,
6356 but may be called more than once. OPAQUE is a generalized pointer
6357 used as a callback argument.
6359 The return code is one of the following values, equivalent to
6360 the STATUS values of __cxa_demangle() (excluding -1, since this
6361 function performs no memory allocations):
6362 0: The demangling operation succeeded.
6363 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
6364 -3: One of the arguments is invalid.
6366 The demangling is performed using the C++ ABI mangling rules, with
6370 __gcclibcxx_demangle_callback (const char *mangled_name,
6371 void (*callback) (const char *, size_t, void *),
6376 if (mangled_name == NULL || callback == NULL)
6379 status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES,
6387 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
6389 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
6390 mangled name, return a buffer allocated with malloc holding the
6391 demangled name. Otherwise, return NULL. */
6394 cplus_demangle_v3 (const char *mangled, int options)
6398 return d_demangle (mangled, options, &alc);
6402 cplus_demangle_v3_callback (const char *mangled, int options,
6403 demangle_callbackref callback, void *opaque)
6405 return d_demangle_callback (mangled, options, callback, opaque);
6408 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
6409 conventions, but the output formatting is a little different.
6410 This instructs the C++ demangler not to emit pointer characters ("*"), to
6411 use Java's namespace separator symbol ("." instead of "::"), and to output
6412 JArray<TYPE> as TYPE[]. */
6415 java_demangle_v3 (const char *mangled)
6419 return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
6423 java_demangle_v3_callback (const char *mangled,
6424 demangle_callbackref callback, void *opaque)
6426 return d_demangle_callback (mangled,
6427 DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
6431 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
6433 #ifndef IN_GLIBCPP_V3
6435 /* Demangle a string in order to find out whether it is a constructor
6436 or destructor. Return non-zero on success. Set *CTOR_KIND and
6437 *DTOR_KIND appropriately. */
6440 is_ctor_or_dtor (const char *mangled,
6441 enum gnu_v3_ctor_kinds *ctor_kind,
6442 enum gnu_v3_dtor_kinds *dtor_kind)
6445 struct demangle_component *dc;
6448 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
6449 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
6451 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
6454 #ifdef CP_DYNAMIC_ARRAYS
6455 __extension__ struct demangle_component comps[di.num_comps];
6456 __extension__ struct demangle_component *subs[di.num_subs];
6461 di.comps = alloca (di.num_comps * sizeof (*di.comps));
6462 di.subs = alloca (di.num_subs * sizeof (*di.subs));
6465 dc = cplus_demangle_mangled_name (&di, 1);
6467 /* Note that because we did not pass DMGL_PARAMS, we don't expect
6468 to demangle the entire string. */
6475 /* These cannot appear on a constructor or destructor. */
6476 case DEMANGLE_COMPONENT_RESTRICT_THIS:
6477 case DEMANGLE_COMPONENT_VOLATILE_THIS:
6478 case DEMANGLE_COMPONENT_CONST_THIS:
6479 case DEMANGLE_COMPONENT_REFERENCE_THIS:
6480 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
6484 case DEMANGLE_COMPONENT_TYPED_NAME:
6485 case DEMANGLE_COMPONENT_TEMPLATE:
6488 case DEMANGLE_COMPONENT_QUAL_NAME:
6489 case DEMANGLE_COMPONENT_LOCAL_NAME:
6492 case DEMANGLE_COMPONENT_CTOR:
6493 *ctor_kind = dc->u.s_ctor.kind;
6497 case DEMANGLE_COMPONENT_DTOR:
6498 *dtor_kind = dc->u.s_dtor.kind;
6509 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
6510 name. A non-zero return indicates the type of constructor. */
6512 enum gnu_v3_ctor_kinds
6513 is_gnu_v3_mangled_ctor (const char *name)
6515 enum gnu_v3_ctor_kinds ctor_kind;
6516 enum gnu_v3_dtor_kinds dtor_kind;
6518 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
6519 return (enum gnu_v3_ctor_kinds) 0;
6524 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
6525 name. A non-zero return indicates the type of destructor. */
6527 enum gnu_v3_dtor_kinds
6528 is_gnu_v3_mangled_dtor (const char *name)
6530 enum gnu_v3_ctor_kinds ctor_kind;
6531 enum gnu_v3_dtor_kinds dtor_kind;
6533 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
6534 return (enum gnu_v3_dtor_kinds) 0;
6538 #endif /* IN_GLIBCPP_V3 */
6540 #ifdef STANDALONE_DEMANGLER
6543 #include "dyn-string.h"
6545 static void print_usage (FILE* fp, int exit_value);
6547 #define IS_ALPHA(CHAR) \
6548 (((CHAR) >= 'a' && (CHAR) <= 'z') \
6549 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
6551 /* Non-zero if CHAR is a character than can occur in a mangled name. */
6552 #define is_mangled_char(CHAR) \
6553 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
6554 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
6556 /* The name of this program, as invoked. */
6557 const char* program_name;
6559 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
6562 print_usage (FILE* fp, int exit_value)
6564 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
6565 fprintf (fp, "Options:\n");
6566 fprintf (fp, " -h,--help Display this message.\n");
6567 fprintf (fp, " -p,--no-params Don't display function parameters\n");
6568 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
6569 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
6574 /* Option specification for getopt_long. */
6575 static const struct option long_options[] =
6577 { "help", no_argument, NULL, 'h' },
6578 { "no-params", no_argument, NULL, 'p' },
6579 { "verbose", no_argument, NULL, 'v' },
6580 { NULL, no_argument, NULL, 0 },
6583 /* Main entry for a demangling filter executable. It will demangle
6584 its command line arguments, if any. If none are provided, it will
6585 filter stdin to stdout, replacing any recognized mangled C++ names
6586 with their demangled equivalents. */
6589 main (int argc, char *argv[])
6593 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
6595 /* Use the program name of this program, as invoked. */
6596 program_name = argv[0];
6598 /* Parse options. */
6601 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
6604 case '?': /* Unrecognized option. */
6605 print_usage (stderr, 1);
6609 print_usage (stdout, 0);
6613 options &= ~ DMGL_PARAMS;
6617 options |= DMGL_VERBOSE;
6621 while (opt_char != -1);
6624 /* No command line arguments were provided. Filter stdin. */
6626 dyn_string_t mangled = dyn_string_new (3);
6629 /* Read all of input. */
6630 while (!feof (stdin))
6634 /* Pile characters into mangled until we hit one that can't
6635 occur in a mangled name. */
6637 while (!feof (stdin) && is_mangled_char (c))
6639 dyn_string_append_char (mangled, c);
6645 if (dyn_string_length (mangled) > 0)
6647 #ifdef IN_GLIBCPP_V3
6648 s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
6650 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
6660 /* It might not have been a mangled name. Print the
6662 fputs (dyn_string_buf (mangled), stdout);
6665 dyn_string_clear (mangled);
6668 /* If we haven't hit EOF yet, we've read one character that
6669 can't occur in a mangled name, so print it out. */
6674 dyn_string_delete (mangled);
6677 /* Demangle command line arguments. */
6679 /* Loop over command line arguments. */
6680 for (i = optind; i < argc; ++i)
6683 #ifdef IN_GLIBCPP_V3
6687 /* Attempt to demangle. */
6688 #ifdef IN_GLIBCPP_V3
6689 s = __cxa_demangle (argv[i], NULL, NULL, &status);
6691 s = cplus_demangle_v3 (argv[i], options);
6694 /* If it worked, print the demangled name. */
6702 #ifdef IN_GLIBCPP_V3
6703 fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
6705 fprintf (stderr, "Failed: %s\n", argv[i]);
6714 #endif /* STANDALONE_DEMANGLER */