1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003-2017 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@wasabisystems.com>.
5 This file is part of the libiberty library, which is part of GCC.
7 This file is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combined
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
31 /* This code implements a demangler for the g++ V3 ABI. The ABI is
32 described on this web page:
33 http://www.codesourcery.com/cxx-abi/abi.html#mangling
35 This code was written while looking at the demangler written by
36 Alex Samuel <samuel@codesourcery.com>.
38 This code first pulls the mangled name apart into a list of
39 components, and then walks the list generating the demangled
42 This file will normally define the following functions, q.v.:
43 char *cplus_demangle_v3(const char *mangled, int options)
44 char *java_demangle_v3(const char *mangled)
45 int cplus_demangle_v3_callback(const char *mangled, int options,
46 demangle_callbackref callback)
47 int java_demangle_v3_callback(const char *mangled,
48 demangle_callbackref callback)
49 enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
50 enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
52 Also, the interface to the component list is public, and defined in
53 demangle.h. The interface consists of these types, which are
54 defined in demangle.h:
55 enum demangle_component_type
56 struct demangle_component
58 and these functions defined in this file:
59 cplus_demangle_fill_name
60 cplus_demangle_fill_extended_operator
61 cplus_demangle_fill_ctor
62 cplus_demangle_fill_dtor
64 cplus_demangle_print_callback
65 and other functions defined in the file cp-demint.c.
67 This file also defines some other functions and variables which are
68 only to be used by the file cp-demint.c.
70 Preprocessor macros you can define while compiling this file:
73 If defined, this file defines the following functions, q.v.:
74 char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
76 int __gcclibcxx_demangle_callback (const char *,
78 (const char *, size_t, void *),
80 instead of cplus_demangle_v3[_callback]() and
81 java_demangle_v3[_callback]().
84 If defined, this file defines only __cxa_demangle() and
85 __gcclibcxx_demangle_callback(), and no other publically visible
86 functions or variables.
89 If defined, this file defines a main() function which demangles
90 any arguments, or, if none, demangles stdin.
93 If defined, turns on debugging mode, which prints information on
94 stdout about the mangled string. This is not generally useful.
97 If defined, additional sanity checks will be performed. It will
98 cause some slowdown, but will allow to catch out-of-bound access
99 errors earlier. This macro is intended for testing and debugging. */
101 #if defined (_AIX) && !defined (__GNUC__)
123 # define alloca __builtin_alloca
125 extern char *alloca ();
126 # endif /* __GNUC__ */
128 #endif /* HAVE_ALLOCA_H */
134 # define INT_MAX (int)(((unsigned int) ~0) >> 1) /* 0x7FFFFFFF */
137 #include "ansidecl.h"
138 #include "libiberty.h"
139 #include "demangle.h"
140 #include "cp-demangle.h"
142 /* If IN_GLIBCPP_V3 is defined, some functions are made static. We
143 also rename them via #define to avoid compiler errors when the
144 static definition conflicts with the extern declaration in a header
148 #define CP_STATIC_IF_GLIBCPP_V3 static
150 #define cplus_demangle_fill_name d_fill_name
151 static int d_fill_name (struct demangle_component *, const char *, int);
153 #define cplus_demangle_fill_extended_operator d_fill_extended_operator
155 d_fill_extended_operator (struct demangle_component *, int,
156 struct demangle_component *);
158 #define cplus_demangle_fill_ctor d_fill_ctor
160 d_fill_ctor (struct demangle_component *, enum gnu_v3_ctor_kinds,
161 struct demangle_component *);
163 #define cplus_demangle_fill_dtor d_fill_dtor
165 d_fill_dtor (struct demangle_component *, enum gnu_v3_dtor_kinds,
166 struct demangle_component *);
168 #define cplus_demangle_mangled_name d_mangled_name
169 static struct demangle_component *d_mangled_name (struct d_info *, int);
171 #define cplus_demangle_type d_type
172 static struct demangle_component *d_type (struct d_info *);
174 #define cplus_demangle_print d_print
175 static char *d_print (int, struct demangle_component *, int, size_t *);
177 #define cplus_demangle_print_callback d_print_callback
178 static int d_print_callback (int, struct demangle_component *,
179 demangle_callbackref, void *);
181 #define cplus_demangle_init_info d_init_info
182 static void d_init_info (const char *, int, size_t, struct d_info *);
184 #else /* ! defined(IN_GLIBCPP_V3) */
185 #define CP_STATIC_IF_GLIBCPP_V3
186 #endif /* ! defined(IN_GLIBCPP_V3) */
188 /* See if the compiler supports dynamic arrays. */
191 #define CP_DYNAMIC_ARRAYS
194 #ifdef __STDC_VERSION__
195 #if __STDC_VERSION__ >= 199901L
196 #define CP_DYNAMIC_ARRAYS
197 #endif /* __STDC__VERSION >= 199901L */
198 #endif /* defined (__STDC_VERSION__) */
199 #endif /* defined (__STDC__) */
200 #endif /* ! defined (__GNUC__) */
202 /* We avoid pulling in the ctype tables, to prevent pulling in
203 additional unresolved symbols when this code is used in a library.
204 FIXME: Is this really a valid reason? This comes from the original
207 As of this writing this file has the following undefined references
208 when compiled with -DIN_GLIBCPP_V3: realloc, free, memcpy, strcpy,
211 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
212 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
213 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
215 /* The prefix prepended by GCC to an identifier represnting the
216 anonymous namespace. */
217 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
218 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
219 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
221 /* Information we keep for the standard substitutions. */
223 struct d_standard_sub_info
225 /* The code for this substitution. */
227 /* The simple string it expands to. */
228 const char *simple_expansion;
229 /* The length of the simple expansion. */
231 /* The results of a full, verbose, expansion. This is used when
232 qualifying a constructor/destructor, or when in verbose mode. */
233 const char *full_expansion;
234 /* The length of the full expansion. */
236 /* What to set the last_name field of d_info to; NULL if we should
237 not set it. This is only relevant when qualifying a
238 constructor/destructor. */
239 const char *set_last_name;
240 /* The length of set_last_name. */
241 int set_last_name_len;
244 /* Accessors for subtrees of struct demangle_component. */
246 #define d_left(dc) ((dc)->u.s_binary.left)
247 #define d_right(dc) ((dc)->u.s_binary.right)
249 /* A list of templates. This is used while printing. */
251 struct d_print_template
253 /* Next template on the list. */
254 struct d_print_template *next;
256 const struct demangle_component *template_decl;
259 /* A list of type modifiers. This is used while printing. */
263 /* Next modifier on the list. These are in the reverse of the order
264 in which they appeared in the mangled string. */
265 struct d_print_mod *next;
267 struct demangle_component *mod;
268 /* Whether this modifier was printed. */
270 /* The list of templates which applies to this modifier. */
271 struct d_print_template *templates;
274 /* We use these structures to hold information during printing. */
276 struct d_growable_string
278 /* Buffer holding the result. */
280 /* Current length of data in buffer. */
282 /* Allocated size of buffer. */
284 /* Set to 1 if we had a memory allocation failure. */
285 int allocation_failure;
288 /* Stack of components, innermost first, used to avoid loops. */
290 struct d_component_stack
292 /* This component. */
293 const struct demangle_component *dc;
294 /* This component's parent. */
295 const struct d_component_stack *parent;
298 /* A demangle component and some scope captured when it was first
303 /* The component whose scope this is. */
304 const struct demangle_component *container;
305 /* The list of templates, if any, that was current when this
306 scope was captured. */
307 struct d_print_template *templates;
310 /* Checkpoint structure to allow backtracking. This holds copies
311 of the fields of struct d_info that need to be restored
312 if a trial parse needs to be backtracked over. */
314 struct d_info_checkpoint
323 enum { D_PRINT_BUFFER_LENGTH = 256 };
326 /* Fixed-length allocated buffer for demangled data, flushed to the
327 callback with a NUL termination once full. */
328 char buf[D_PRINT_BUFFER_LENGTH];
329 /* Current length of data in buffer. */
331 /* The last character printed, saved individually so that it survives
334 /* Callback function to handle demangled buffer flush. */
335 demangle_callbackref callback;
336 /* Opaque callback argument. */
338 /* The current list of templates, if any. */
339 struct d_print_template *templates;
340 /* The current list of modifiers (e.g., pointer, reference, etc.),
342 struct d_print_mod *modifiers;
343 /* Set to 1 if we saw a demangling error. */
344 int demangle_failure;
345 /* Non-zero if we're printing a lambda argument. A template
346 parameter reference actually means 'auto'. */
348 /* The current index into any template argument packs we are using
349 for printing, or -1 to print the whole pack. */
351 /* Number of d_print_flush calls so far. */
352 unsigned long int flush_count;
353 /* Stack of components, innermost first, used to avoid loops. */
354 const struct d_component_stack *component_stack;
355 /* Array of saved scopes for evaluating substitutions. */
356 struct d_saved_scope *saved_scopes;
357 /* Index of the next unused saved scope in the above array. */
358 int next_saved_scope;
359 /* Number of saved scopes in the above array. */
360 int num_saved_scopes;
361 /* Array of templates for saving into scopes. */
362 struct d_print_template *copy_templates;
363 /* Index of the next unused copy template in the above array. */
364 int next_copy_template;
365 /* Number of copy templates in the above array. */
366 int num_copy_templates;
367 /* The nearest enclosing template, if any. */
368 const struct demangle_component *current_template;
371 #ifdef CP_DEMANGLE_DEBUG
372 static void d_dump (struct demangle_component *, int);
375 static struct demangle_component *
376 d_make_empty (struct d_info *);
378 static struct demangle_component *
379 d_make_comp (struct d_info *, enum demangle_component_type,
380 struct demangle_component *,
381 struct demangle_component *);
383 static struct demangle_component *
384 d_make_name (struct d_info *, const char *, int);
386 static struct demangle_component *
387 d_make_demangle_mangled_name (struct d_info *, const char *);
389 static struct demangle_component *
390 d_make_builtin_type (struct d_info *,
391 const struct demangle_builtin_type_info *);
393 static struct demangle_component *
394 d_make_operator (struct d_info *,
395 const struct demangle_operator_info *);
397 static struct demangle_component *
398 d_make_extended_operator (struct d_info *, int,
399 struct demangle_component *);
401 static struct demangle_component *
402 d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
403 struct demangle_component *);
405 static struct demangle_component *
406 d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
407 struct demangle_component *);
409 static struct demangle_component *
410 d_make_template_param (struct d_info *, int);
412 static struct demangle_component *
413 d_make_sub (struct d_info *, const char *, int);
416 has_return_type (struct demangle_component *);
419 is_ctor_dtor_or_conversion (struct demangle_component *);
421 static struct demangle_component *d_encoding (struct d_info *, int);
423 static struct demangle_component *d_name (struct d_info *);
425 static struct demangle_component *d_nested_name (struct d_info *);
427 static struct demangle_component *d_prefix (struct d_info *);
429 static struct demangle_component *d_unqualified_name (struct d_info *);
431 static struct demangle_component *d_source_name (struct d_info *);
433 static int d_number (struct d_info *);
435 static struct demangle_component *d_identifier (struct d_info *, int);
437 static struct demangle_component *d_operator_name (struct d_info *);
439 static struct demangle_component *d_special_name (struct d_info *);
441 static struct demangle_component *d_parmlist (struct d_info *);
443 static int d_call_offset (struct d_info *, int);
445 static struct demangle_component *d_ctor_dtor_name (struct d_info *);
447 static struct demangle_component **
448 d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
450 static struct demangle_component *
451 d_ref_qualifier (struct d_info *, struct demangle_component *);
453 static struct demangle_component *
454 d_function_type (struct d_info *);
456 static struct demangle_component *
457 d_bare_function_type (struct d_info *, int);
459 static struct demangle_component *
460 d_class_enum_type (struct d_info *);
462 static struct demangle_component *d_array_type (struct d_info *);
464 static struct demangle_component *d_vector_type (struct d_info *);
466 static struct demangle_component *
467 d_pointer_to_member_type (struct d_info *);
469 static struct demangle_component *
470 d_template_param (struct d_info *);
472 static struct demangle_component *d_template_args (struct d_info *);
473 static struct demangle_component *d_template_args_1 (struct d_info *);
475 static struct demangle_component *
476 d_template_arg (struct d_info *);
478 static struct demangle_component *d_expression (struct d_info *);
480 static struct demangle_component *d_expr_primary (struct d_info *);
482 static struct demangle_component *d_local_name (struct d_info *);
484 static int d_discriminator (struct d_info *);
486 static struct demangle_component *d_lambda (struct d_info *);
488 static struct demangle_component *d_unnamed_type (struct d_info *);
490 static struct demangle_component *
491 d_clone_suffix (struct d_info *, struct demangle_component *);
494 d_add_substitution (struct d_info *, struct demangle_component *);
496 static struct demangle_component *d_substitution (struct d_info *, int);
498 static void d_checkpoint (struct d_info *, struct d_info_checkpoint *);
500 static void d_backtrack (struct d_info *, struct d_info_checkpoint *);
502 static void d_growable_string_init (struct d_growable_string *, size_t);
505 d_growable_string_resize (struct d_growable_string *, size_t);
508 d_growable_string_append_buffer (struct d_growable_string *,
509 const char *, size_t);
511 d_growable_string_callback_adapter (const char *, size_t, void *);
514 d_print_init (struct d_print_info *, demangle_callbackref, void *,
515 const struct demangle_component *);
517 static inline void d_print_error (struct d_print_info *);
519 static inline int d_print_saw_error (struct d_print_info *);
521 static inline void d_print_flush (struct d_print_info *);
523 static inline void d_append_char (struct d_print_info *, char);
525 static inline void d_append_buffer (struct d_print_info *,
526 const char *, size_t);
528 static inline void d_append_string (struct d_print_info *, const char *);
530 static inline char d_last_char (struct d_print_info *);
533 d_print_comp (struct d_print_info *, int, struct demangle_component *);
536 d_print_java_identifier (struct d_print_info *, const char *, int);
539 d_print_mod_list (struct d_print_info *, int, struct d_print_mod *, int);
542 d_print_mod (struct d_print_info *, int, struct demangle_component *);
545 d_print_function_type (struct d_print_info *, int,
546 struct demangle_component *,
547 struct d_print_mod *);
550 d_print_array_type (struct d_print_info *, int,
551 struct demangle_component *,
552 struct d_print_mod *);
555 d_print_expr_op (struct d_print_info *, int, struct demangle_component *);
557 static void d_print_cast (struct d_print_info *, int,
558 struct demangle_component *);
559 static void d_print_conversion (struct d_print_info *, int,
560 struct demangle_component *);
562 static int d_demangle_callback (const char *, int,
563 demangle_callbackref, void *);
564 static char *d_demangle (const char *, int, size_t *);
566 /* True iff TYPE is a demangling component representing a
567 function-type-qualifier. */
570 is_fnqual_component_type (enum demangle_component_type type)
572 return (type == DEMANGLE_COMPONENT_RESTRICT_THIS
573 || type == DEMANGLE_COMPONENT_VOLATILE_THIS
574 || type == DEMANGLE_COMPONENT_CONST_THIS
575 || type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
576 || type == DEMANGLE_COMPONENT_TRANSACTION_SAFE
577 || type == DEMANGLE_COMPONENT_NOEXCEPT
578 || type == DEMANGLE_COMPONENT_THROW_SPEC
579 || type == DEMANGLE_COMPONENT_REFERENCE_THIS);
582 #define FNQUAL_COMPONENT_CASE \
583 case DEMANGLE_COMPONENT_RESTRICT_THIS: \
584 case DEMANGLE_COMPONENT_VOLATILE_THIS: \
585 case DEMANGLE_COMPONENT_CONST_THIS: \
586 case DEMANGLE_COMPONENT_REFERENCE_THIS: \
587 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS: \
588 case DEMANGLE_COMPONENT_TRANSACTION_SAFE: \
589 case DEMANGLE_COMPONENT_NOEXCEPT: \
590 case DEMANGLE_COMPONENT_THROW_SPEC
592 #ifdef CP_DEMANGLE_DEBUG
595 d_dump (struct demangle_component *dc, int indent)
602 printf ("failed demangling\n");
606 for (i = 0; i < indent; ++i)
611 case DEMANGLE_COMPONENT_NAME:
612 printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
614 case DEMANGLE_COMPONENT_TAGGED_NAME:
615 printf ("tagged name\n");
616 d_dump (dc->u.s_binary.left, indent + 2);
617 d_dump (dc->u.s_binary.right, indent + 2);
619 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
620 printf ("template parameter %ld\n", dc->u.s_number.number);
622 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
623 printf ("function parameter %ld\n", dc->u.s_number.number);
625 case DEMANGLE_COMPONENT_CTOR:
626 printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
627 d_dump (dc->u.s_ctor.name, indent + 2);
629 case DEMANGLE_COMPONENT_DTOR:
630 printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
631 d_dump (dc->u.s_dtor.name, indent + 2);
633 case DEMANGLE_COMPONENT_SUB_STD:
634 printf ("standard substitution %s\n", dc->u.s_string.string);
636 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
637 printf ("builtin type %s\n", dc->u.s_builtin.type->name);
639 case DEMANGLE_COMPONENT_OPERATOR:
640 printf ("operator %s\n", dc->u.s_operator.op->name);
642 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
643 printf ("extended operator with %d args\n",
644 dc->u.s_extended_operator.args);
645 d_dump (dc->u.s_extended_operator.name, indent + 2);
648 case DEMANGLE_COMPONENT_QUAL_NAME:
649 printf ("qualified name\n");
651 case DEMANGLE_COMPONENT_LOCAL_NAME:
652 printf ("local name\n");
654 case DEMANGLE_COMPONENT_TYPED_NAME:
655 printf ("typed name\n");
657 case DEMANGLE_COMPONENT_TEMPLATE:
658 printf ("template\n");
660 case DEMANGLE_COMPONENT_VTABLE:
663 case DEMANGLE_COMPONENT_VTT:
666 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
667 printf ("construction vtable\n");
669 case DEMANGLE_COMPONENT_TYPEINFO:
670 printf ("typeinfo\n");
672 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
673 printf ("typeinfo name\n");
675 case DEMANGLE_COMPONENT_TYPEINFO_FN:
676 printf ("typeinfo function\n");
678 case DEMANGLE_COMPONENT_THUNK:
681 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
682 printf ("virtual thunk\n");
684 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
685 printf ("covariant thunk\n");
687 case DEMANGLE_COMPONENT_JAVA_CLASS:
688 printf ("java class\n");
690 case DEMANGLE_COMPONENT_GUARD:
693 case DEMANGLE_COMPONENT_REFTEMP:
694 printf ("reference temporary\n");
696 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
697 printf ("hidden alias\n");
699 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
700 printf ("transaction clone\n");
702 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
703 printf ("non-transaction clone\n");
705 case DEMANGLE_COMPONENT_RESTRICT:
706 printf ("restrict\n");
708 case DEMANGLE_COMPONENT_VOLATILE:
709 printf ("volatile\n");
711 case DEMANGLE_COMPONENT_CONST:
714 case DEMANGLE_COMPONENT_RESTRICT_THIS:
715 printf ("restrict this\n");
717 case DEMANGLE_COMPONENT_VOLATILE_THIS:
718 printf ("volatile this\n");
720 case DEMANGLE_COMPONENT_CONST_THIS:
721 printf ("const this\n");
723 case DEMANGLE_COMPONENT_REFERENCE_THIS:
724 printf ("reference this\n");
726 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
727 printf ("rvalue reference this\n");
729 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
730 printf ("transaction_safe this\n");
732 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
733 printf ("vendor type qualifier\n");
735 case DEMANGLE_COMPONENT_POINTER:
736 printf ("pointer\n");
738 case DEMANGLE_COMPONENT_REFERENCE:
739 printf ("reference\n");
741 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
742 printf ("rvalue reference\n");
744 case DEMANGLE_COMPONENT_COMPLEX:
745 printf ("complex\n");
747 case DEMANGLE_COMPONENT_IMAGINARY:
748 printf ("imaginary\n");
750 case DEMANGLE_COMPONENT_VENDOR_TYPE:
751 printf ("vendor type\n");
753 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
754 printf ("function type\n");
756 case DEMANGLE_COMPONENT_ARRAY_TYPE:
757 printf ("array type\n");
759 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
760 printf ("pointer to member type\n");
762 case DEMANGLE_COMPONENT_FIXED_TYPE:
763 printf ("fixed-point type, accum? %d, sat? %d\n",
764 dc->u.s_fixed.accum, dc->u.s_fixed.sat);
765 d_dump (dc->u.s_fixed.length, indent + 2);
767 case DEMANGLE_COMPONENT_ARGLIST:
768 printf ("argument list\n");
770 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
771 printf ("template argument list\n");
773 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
774 printf ("initializer list\n");
776 case DEMANGLE_COMPONENT_CAST:
779 case DEMANGLE_COMPONENT_CONVERSION:
780 printf ("conversion operator\n");
782 case DEMANGLE_COMPONENT_NULLARY:
783 printf ("nullary operator\n");
785 case DEMANGLE_COMPONENT_UNARY:
786 printf ("unary operator\n");
788 case DEMANGLE_COMPONENT_BINARY:
789 printf ("binary operator\n");
791 case DEMANGLE_COMPONENT_BINARY_ARGS:
792 printf ("binary operator arguments\n");
794 case DEMANGLE_COMPONENT_TRINARY:
795 printf ("trinary operator\n");
797 case DEMANGLE_COMPONENT_TRINARY_ARG1:
798 printf ("trinary operator arguments 1\n");
800 case DEMANGLE_COMPONENT_TRINARY_ARG2:
801 printf ("trinary operator arguments 1\n");
803 case DEMANGLE_COMPONENT_LITERAL:
804 printf ("literal\n");
806 case DEMANGLE_COMPONENT_LITERAL_NEG:
807 printf ("negative literal\n");
809 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
810 printf ("java resource\n");
812 case DEMANGLE_COMPONENT_COMPOUND_NAME:
813 printf ("compound name\n");
815 case DEMANGLE_COMPONENT_CHARACTER:
816 printf ("character '%c'\n", dc->u.s_character.character);
818 case DEMANGLE_COMPONENT_NUMBER:
819 printf ("number %ld\n", dc->u.s_number.number);
821 case DEMANGLE_COMPONENT_DECLTYPE:
822 printf ("decltype\n");
824 case DEMANGLE_COMPONENT_PACK_EXPANSION:
825 printf ("pack expansion\n");
827 case DEMANGLE_COMPONENT_TLS_INIT:
828 printf ("tls init function\n");
830 case DEMANGLE_COMPONENT_TLS_WRAPPER:
831 printf ("tls wrapper function\n");
833 case DEMANGLE_COMPONENT_DEFAULT_ARG:
834 printf ("default argument %d\n", dc->u.s_unary_num.num);
835 d_dump (dc->u.s_unary_num.sub, indent+2);
837 case DEMANGLE_COMPONENT_LAMBDA:
838 printf ("lambda %d\n", dc->u.s_unary_num.num);
839 d_dump (dc->u.s_unary_num.sub, indent+2);
843 d_dump (d_left (dc), indent + 2);
844 d_dump (d_right (dc), indent + 2);
847 #endif /* CP_DEMANGLE_DEBUG */
849 /* Fill in a DEMANGLE_COMPONENT_NAME. */
851 CP_STATIC_IF_GLIBCPP_V3
853 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
855 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)
874 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
875 p->u.s_extended_operator.args = args;
876 p->u.s_extended_operator.name = name;
880 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
882 CP_STATIC_IF_GLIBCPP_V3
884 cplus_demangle_fill_ctor (struct demangle_component *p,
885 enum gnu_v3_ctor_kinds kind,
886 struct demangle_component *name)
890 || (int) kind < gnu_v3_complete_object_ctor
891 || (int) kind > gnu_v3_object_ctor_group)
894 p->type = DEMANGLE_COMPONENT_CTOR;
895 p->u.s_ctor.kind = kind;
896 p->u.s_ctor.name = name;
900 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
902 CP_STATIC_IF_GLIBCPP_V3
904 cplus_demangle_fill_dtor (struct demangle_component *p,
905 enum gnu_v3_dtor_kinds kind,
906 struct demangle_component *name)
910 || (int) kind < gnu_v3_deleting_dtor
911 || (int) kind > gnu_v3_object_dtor_group)
914 p->type = DEMANGLE_COMPONENT_DTOR;
915 p->u.s_dtor.kind = kind;
916 p->u.s_dtor.name = name;
920 /* Add a new component. */
922 static struct demangle_component *
923 d_make_empty (struct d_info *di)
925 struct demangle_component *p;
927 if (di->next_comp >= di->num_comps)
929 p = &di->comps[di->next_comp];
935 /* Add a new generic component. */
937 static struct demangle_component *
938 d_make_comp (struct d_info *di, enum demangle_component_type type,
939 struct demangle_component *left,
940 struct demangle_component *right)
942 struct demangle_component *p;
944 /* We check for errors here. A typical error would be a NULL return
945 from a subroutine. We catch those here, and return NULL
949 /* These types require two parameters. */
950 case DEMANGLE_COMPONENT_QUAL_NAME:
951 case DEMANGLE_COMPONENT_LOCAL_NAME:
952 case DEMANGLE_COMPONENT_TYPED_NAME:
953 case DEMANGLE_COMPONENT_TAGGED_NAME:
954 case DEMANGLE_COMPONENT_TEMPLATE:
955 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
956 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
957 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
958 case DEMANGLE_COMPONENT_UNARY:
959 case DEMANGLE_COMPONENT_BINARY:
960 case DEMANGLE_COMPONENT_BINARY_ARGS:
961 case DEMANGLE_COMPONENT_TRINARY:
962 case DEMANGLE_COMPONENT_TRINARY_ARG1:
963 case DEMANGLE_COMPONENT_LITERAL:
964 case DEMANGLE_COMPONENT_LITERAL_NEG:
965 case DEMANGLE_COMPONENT_COMPOUND_NAME:
966 case DEMANGLE_COMPONENT_VECTOR_TYPE:
967 case DEMANGLE_COMPONENT_CLONE:
968 if (left == NULL || right == NULL)
972 /* These types only require one parameter. */
973 case DEMANGLE_COMPONENT_VTABLE:
974 case DEMANGLE_COMPONENT_VTT:
975 case DEMANGLE_COMPONENT_TYPEINFO:
976 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
977 case DEMANGLE_COMPONENT_TYPEINFO_FN:
978 case DEMANGLE_COMPONENT_THUNK:
979 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
980 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
981 case DEMANGLE_COMPONENT_JAVA_CLASS:
982 case DEMANGLE_COMPONENT_GUARD:
983 case DEMANGLE_COMPONENT_TLS_INIT:
984 case DEMANGLE_COMPONENT_TLS_WRAPPER:
985 case DEMANGLE_COMPONENT_REFTEMP:
986 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
987 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
988 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
989 case DEMANGLE_COMPONENT_POINTER:
990 case DEMANGLE_COMPONENT_REFERENCE:
991 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
992 case DEMANGLE_COMPONENT_COMPLEX:
993 case DEMANGLE_COMPONENT_IMAGINARY:
994 case DEMANGLE_COMPONENT_VENDOR_TYPE:
995 case DEMANGLE_COMPONENT_CAST:
996 case DEMANGLE_COMPONENT_CONVERSION:
997 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
998 case DEMANGLE_COMPONENT_DECLTYPE:
999 case DEMANGLE_COMPONENT_PACK_EXPANSION:
1000 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
1001 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
1002 case DEMANGLE_COMPONENT_NULLARY:
1003 case DEMANGLE_COMPONENT_TRINARY_ARG2:
1008 /* This needs a right parameter, but the left parameter can be
1010 case DEMANGLE_COMPONENT_ARRAY_TYPE:
1011 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
1016 /* These are allowed to have no parameters--in some cases they
1017 will be filled in later. */
1018 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
1019 case DEMANGLE_COMPONENT_RESTRICT:
1020 case DEMANGLE_COMPONENT_VOLATILE:
1021 case DEMANGLE_COMPONENT_CONST:
1022 case DEMANGLE_COMPONENT_ARGLIST:
1023 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
1024 FNQUAL_COMPONENT_CASE:
1027 /* Other types should not be seen here. */
1032 p = d_make_empty (di);
1036 p->u.s_binary.left = left;
1037 p->u.s_binary.right = right;
1042 /* Add a new demangle mangled name component. */
1044 static struct demangle_component *
1045 d_make_demangle_mangled_name (struct d_info *di, const char *s)
1047 if (d_peek_char (di) != '_' || d_peek_next_char (di) != 'Z')
1048 return d_make_name (di, s, strlen (s));
1050 return d_encoding (di, 0);
1053 /* Add a new name component. */
1055 static struct demangle_component *
1056 d_make_name (struct d_info *di, const char *s, int len)
1058 struct demangle_component *p;
1060 p = d_make_empty (di);
1061 if (! cplus_demangle_fill_name (p, s, len))
1066 /* Add a new builtin type component. */
1068 static struct demangle_component *
1069 d_make_builtin_type (struct d_info *di,
1070 const struct demangle_builtin_type_info *type)
1072 struct demangle_component *p;
1076 p = d_make_empty (di);
1079 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
1080 p->u.s_builtin.type = type;
1085 /* Add a new operator component. */
1087 static struct demangle_component *
1088 d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
1090 struct demangle_component *p;
1092 p = d_make_empty (di);
1095 p->type = DEMANGLE_COMPONENT_OPERATOR;
1096 p->u.s_operator.op = op;
1101 /* Add a new extended operator component. */
1103 static struct demangle_component *
1104 d_make_extended_operator (struct d_info *di, int args,
1105 struct demangle_component *name)
1107 struct demangle_component *p;
1109 p = d_make_empty (di);
1110 if (! cplus_demangle_fill_extended_operator (p, args, name))
1115 static struct demangle_component *
1116 d_make_default_arg (struct d_info *di, int num,
1117 struct demangle_component *sub)
1119 struct demangle_component *p = d_make_empty (di);
1122 p->type = DEMANGLE_COMPONENT_DEFAULT_ARG;
1123 p->u.s_unary_num.num = num;
1124 p->u.s_unary_num.sub = sub;
1129 /* Add a new constructor component. */
1131 static struct demangle_component *
1132 d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
1133 struct demangle_component *name)
1135 struct demangle_component *p;
1137 p = d_make_empty (di);
1138 if (! cplus_demangle_fill_ctor (p, kind, name))
1143 /* Add a new destructor component. */
1145 static struct demangle_component *
1146 d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
1147 struct demangle_component *name)
1149 struct demangle_component *p;
1151 p = d_make_empty (di);
1152 if (! cplus_demangle_fill_dtor (p, kind, name))
1157 /* Add a new template parameter. */
1159 static struct demangle_component *
1160 d_make_template_param (struct d_info *di, int i)
1162 struct demangle_component *p;
1164 p = d_make_empty (di);
1167 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
1168 p->u.s_number.number = i;
1173 /* Add a new function parameter. */
1175 static struct demangle_component *
1176 d_make_function_param (struct d_info *di, int i)
1178 struct demangle_component *p;
1180 p = d_make_empty (di);
1183 p->type = DEMANGLE_COMPONENT_FUNCTION_PARAM;
1184 p->u.s_number.number = i;
1189 /* Add a new standard substitution component. */
1191 static struct demangle_component *
1192 d_make_sub (struct d_info *di, const char *name, int len)
1194 struct demangle_component *p;
1196 p = d_make_empty (di);
1199 p->type = DEMANGLE_COMPONENT_SUB_STD;
1200 p->u.s_string.string = name;
1201 p->u.s_string.len = len;
1206 /* <mangled-name> ::= _Z <encoding> [<clone-suffix>]*
1208 TOP_LEVEL is non-zero when called at the top level. */
1210 CP_STATIC_IF_GLIBCPP_V3
1211 struct demangle_component *
1212 cplus_demangle_mangled_name (struct d_info *di, int top_level)
1214 struct demangle_component *p;
1216 if (! d_check_char (di, '_')
1217 /* Allow missing _ if not at toplevel to work around a
1218 bug in G++ abi-version=2 mangling; see the comment in
1219 write_template_arg. */
1222 if (! d_check_char (di, 'Z'))
1224 p = d_encoding (di, top_level);
1226 /* If at top level and parsing parameters, check for a clone
1228 if (top_level && (di->options & DMGL_PARAMS) != 0)
1229 while (d_peek_char (di) == '.'
1230 && (IS_LOWER (d_peek_next_char (di))
1231 || d_peek_next_char (di) == '_'
1232 || IS_DIGIT (d_peek_next_char (di))))
1233 p = d_clone_suffix (di, p);
1238 /* Return whether a function should have a return type. The argument
1239 is the function name, which may be qualified in various ways. The
1240 rules are that template functions have return types with some
1241 exceptions, function types which are not part of a function name
1242 mangling have return types with some exceptions, and non-template
1243 function names do not have return types. The exceptions are that
1244 constructors, destructors, and conversion operators do not have
1248 has_return_type (struct demangle_component *dc)
1256 case DEMANGLE_COMPONENT_TEMPLATE:
1257 return ! is_ctor_dtor_or_conversion (d_left (dc));
1258 FNQUAL_COMPONENT_CASE:
1259 return has_return_type (d_left (dc));
1263 /* Return whether a name is a constructor, a destructor, or a
1264 conversion operator. */
1267 is_ctor_dtor_or_conversion (struct demangle_component *dc)
1275 case DEMANGLE_COMPONENT_QUAL_NAME:
1276 case DEMANGLE_COMPONENT_LOCAL_NAME:
1277 return is_ctor_dtor_or_conversion (d_right (dc));
1278 case DEMANGLE_COMPONENT_CTOR:
1279 case DEMANGLE_COMPONENT_DTOR:
1280 case DEMANGLE_COMPONENT_CONVERSION:
1285 /* <encoding> ::= <(function) name> <bare-function-type>
1289 TOP_LEVEL is non-zero when called at the top level, in which case
1290 if DMGL_PARAMS is not set we do not demangle the function
1291 parameters. We only set this at the top level, because otherwise
1292 we would not correctly demangle names in local scopes. */
1294 static struct demangle_component *
1295 d_encoding (struct d_info *di, int top_level)
1297 char peek = d_peek_char (di);
1299 if (peek == 'G' || peek == 'T')
1300 return d_special_name (di);
1303 struct demangle_component *dc;
1307 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
1309 /* Strip off any initial CV-qualifiers, as they really apply
1310 to the `this' parameter, and they were not output by the
1311 v2 demangler without DMGL_PARAMS. */
1312 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1313 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1314 || dc->type == DEMANGLE_COMPONENT_CONST_THIS
1315 || dc->type == DEMANGLE_COMPONENT_REFERENCE_THIS
1316 || dc->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS)
1319 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1320 there may be function-qualifiers on its right argument which
1321 really apply here; this happens when parsing a class
1322 which is local to a function. */
1323 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1325 struct demangle_component *dcr;
1328 while (is_fnqual_component_type (dcr->type))
1330 dc->u.s_binary.right = dcr;
1336 peek = d_peek_char (di);
1337 if (dc == NULL || peek == '\0' || peek == 'E')
1339 return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
1340 d_bare_function_type (di, has_return_type (dc)));
1344 /* <tagged-name> ::= <name> B <source-name> */
1346 static struct demangle_component *
1347 d_abi_tags (struct d_info *di, struct demangle_component *dc)
1349 struct demangle_component *hold_last_name;
1352 /* Preserve the last name, so the ABI tag doesn't clobber it. */
1353 hold_last_name = di->last_name;
1355 while (peek = d_peek_char (di),
1358 struct demangle_component *tag;
1360 tag = d_source_name (di);
1361 dc = d_make_comp (di, DEMANGLE_COMPONENT_TAGGED_NAME, dc, tag);
1364 di->last_name = hold_last_name;
1369 /* <name> ::= <nested-name>
1371 ::= <unscoped-template-name> <template-args>
1374 <unscoped-name> ::= <unqualified-name>
1375 ::= St <unqualified-name>
1377 <unscoped-template-name> ::= <unscoped-name>
1381 static struct demangle_component *
1382 d_name (struct d_info *di)
1384 char peek = d_peek_char (di);
1385 struct demangle_component *dc;
1390 return d_nested_name (di);
1393 return d_local_name (di);
1396 return d_unqualified_name (di);
1402 if (d_peek_next_char (di) != 't')
1404 dc = d_substitution (di, 0);
1410 dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
1411 d_make_name (di, "std", 3),
1412 d_unqualified_name (di));
1417 if (d_peek_char (di) != 'I')
1419 /* The grammar does not permit this case to occur if we
1420 called d_substitution() above (i.e., subst == 1). We
1421 don't bother to check. */
1425 /* This is <template-args>, which means that we just saw
1426 <unscoped-template-name>, which is a substitution
1427 candidate if we didn't just get it from a
1431 if (! d_add_substitution (di, dc))
1434 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1435 d_template_args (di));
1443 dc = d_unqualified_name (di);
1444 if (d_peek_char (di) == 'I')
1446 /* This is <template-args>, which means that we just saw
1447 <unscoped-template-name>, which is a substitution
1449 if (! d_add_substitution (di, dc))
1451 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1452 d_template_args (di));
1458 /* <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1459 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
1462 static struct demangle_component *
1463 d_nested_name (struct d_info *di)
1465 struct demangle_component *ret;
1466 struct demangle_component **pret;
1467 struct demangle_component *rqual;
1469 if (! d_check_char (di, 'N'))
1472 pret = d_cv_qualifiers (di, &ret, 1);
1476 /* Parse the ref-qualifier now and then attach it
1477 once we have something to attach it to. */
1478 rqual = d_ref_qualifier (di, NULL);
1480 *pret = d_prefix (di);
1486 d_left (rqual) = ret;
1490 if (! d_check_char (di, 'E'))
1496 /* <prefix> ::= <prefix> <unqualified-name>
1497 ::= <template-prefix> <template-args>
1498 ::= <template-param>
1503 <template-prefix> ::= <prefix> <(template) unqualified-name>
1504 ::= <template-param>
1508 static struct demangle_component *
1509 d_prefix (struct d_info *di)
1511 struct demangle_component *ret = NULL;
1516 enum demangle_component_type comb_type;
1517 struct demangle_component *dc;
1519 peek = d_peek_char (di);
1523 /* The older code accepts a <local-name> here, but I don't see
1524 that in the grammar. The older code does not accept a
1525 <template-param> here. */
1527 comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
1530 char peek2 = d_peek_next_char (di);
1531 if (peek2 == 'T' || peek2 == 't')
1533 dc = cplus_demangle_type (di);
1535 /* Destructor name. */
1536 dc = d_unqualified_name (di);
1538 else if (IS_DIGIT (peek)
1543 dc = d_unqualified_name (di);
1544 else if (peek == 'S')
1545 dc = d_substitution (di, 1);
1546 else if (peek == 'I')
1550 comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1551 dc = d_template_args (di);
1553 else if (peek == 'T')
1554 dc = d_template_param (di);
1555 else if (peek == 'E')
1557 else if (peek == 'M')
1559 /* Initializer scope for a lambda. We don't need to represent
1560 this; the normal code will just treat the variable as a type
1561 scope, which gives appropriate output. */
1573 ret = d_make_comp (di, comb_type, ret, dc);
1575 if (peek != 'S' && d_peek_char (di) != 'E')
1577 if (! d_add_substitution (di, ret))
1583 /* <unqualified-name> ::= <operator-name>
1584 ::= <ctor-dtor-name>
1586 ::= <local-source-name>
1588 <local-source-name> ::= L <source-name> <discriminator>
1591 static struct demangle_component *
1592 d_unqualified_name (struct d_info *di)
1594 struct demangle_component *ret;
1597 peek = d_peek_char (di);
1598 if (IS_DIGIT (peek))
1599 ret = d_source_name (di);
1600 else if (IS_LOWER (peek))
1602 if (peek == 'o' && d_peek_next_char (di) == 'n')
1604 ret = d_operator_name (di);
1605 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1607 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1608 if (!strcmp (ret->u.s_operator.op->code, "li"))
1609 ret = d_make_comp (di, DEMANGLE_COMPONENT_UNARY, ret,
1610 d_source_name (di));
1613 else if (peek == 'C' || peek == 'D')
1614 ret = d_ctor_dtor_name (di);
1615 else if (peek == 'L')
1619 ret = d_source_name (di);
1622 if (! d_discriminator (di))
1625 else if (peek == 'U')
1627 switch (d_peek_next_char (di))
1630 ret = d_lambda (di);
1633 ret = d_unnamed_type (di);
1642 if (d_peek_char (di) == 'B')
1643 ret = d_abi_tags (di, ret);
1647 /* <source-name> ::= <(positive length) number> <identifier> */
1649 static struct demangle_component *
1650 d_source_name (struct d_info *di)
1653 struct demangle_component *ret;
1655 len = d_number (di);
1658 ret = d_identifier (di, len);
1659 di->last_name = ret;
1663 /* number ::= [n] <(non-negative decimal integer)> */
1666 d_number (struct d_info *di)
1673 peek = d_peek_char (di);
1678 peek = d_peek_char (di);
1684 if (! IS_DIGIT (peek))
1690 ret = ret * 10 + peek - '0';
1692 peek = d_peek_char (di);
1696 /* Like d_number, but returns a demangle_component. */
1698 static struct demangle_component *
1699 d_number_component (struct d_info *di)
1701 struct demangle_component *ret = d_make_empty (di);
1704 ret->type = DEMANGLE_COMPONENT_NUMBER;
1705 ret->u.s_number.number = d_number (di);
1710 /* identifier ::= <(unqualified source code identifier)> */
1712 static struct demangle_component *
1713 d_identifier (struct d_info *di, int len)
1719 if (di->send - name < len)
1722 d_advance (di, len);
1724 /* A Java mangled name may have a trailing '$' if it is a C++
1725 keyword. This '$' is not included in the length count. We just
1727 if ((di->options & DMGL_JAVA) != 0
1728 && d_peek_char (di) == '$')
1731 /* Look for something which looks like a gcc encoding of an
1732 anonymous namespace, and replace it with a more user friendly
1734 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1735 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1736 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1740 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1741 if ((*s == '.' || *s == '_' || *s == '$')
1744 di->expansion -= len - sizeof "(anonymous namespace)";
1745 return d_make_name (di, "(anonymous namespace)",
1746 sizeof "(anonymous namespace)" - 1);
1750 return d_make_name (di, name, len);
1753 /* operator_name ::= many different two character encodings.
1755 ::= v <digit> <source-name>
1757 This list is sorted for binary search. */
1759 #define NL(s) s, (sizeof s) - 1
1761 CP_STATIC_IF_GLIBCPP_V3
1762 const struct demangle_operator_info cplus_demangle_operators[] =
1764 { "aN", NL ("&="), 2 },
1765 { "aS", NL ("="), 2 },
1766 { "aa", NL ("&&"), 2 },
1767 { "ad", NL ("&"), 1 },
1768 { "an", NL ("&"), 2 },
1769 { "at", NL ("alignof "), 1 },
1770 { "az", NL ("alignof "), 1 },
1771 { "cc", NL ("const_cast"), 2 },
1772 { "cl", NL ("()"), 2 },
1773 { "cm", NL (","), 2 },
1774 { "co", NL ("~"), 1 },
1775 { "dV", NL ("/="), 2 },
1776 { "da", NL ("delete[] "), 1 },
1777 { "dc", NL ("dynamic_cast"), 2 },
1778 { "de", NL ("*"), 1 },
1779 { "dl", NL ("delete "), 1 },
1780 { "ds", NL (".*"), 2 },
1781 { "dt", NL ("."), 2 },
1782 { "dv", NL ("/"), 2 },
1783 { "eO", NL ("^="), 2 },
1784 { "eo", NL ("^"), 2 },
1785 { "eq", NL ("=="), 2 },
1786 { "fL", NL ("..."), 3 },
1787 { "fR", NL ("..."), 3 },
1788 { "fl", NL ("..."), 2 },
1789 { "fr", NL ("..."), 2 },
1790 { "ge", NL (">="), 2 },
1791 { "gs", NL ("::"), 1 },
1792 { "gt", NL (">"), 2 },
1793 { "ix", NL ("[]"), 2 },
1794 { "lS", NL ("<<="), 2 },
1795 { "le", NL ("<="), 2 },
1796 { "li", NL ("operator\"\" "), 1 },
1797 { "ls", NL ("<<"), 2 },
1798 { "lt", NL ("<"), 2 },
1799 { "mI", NL ("-="), 2 },
1800 { "mL", NL ("*="), 2 },
1801 { "mi", NL ("-"), 2 },
1802 { "ml", NL ("*"), 2 },
1803 { "mm", NL ("--"), 1 },
1804 { "na", NL ("new[]"), 3 },
1805 { "ne", NL ("!="), 2 },
1806 { "ng", NL ("-"), 1 },
1807 { "nt", NL ("!"), 1 },
1808 { "nw", NL ("new"), 3 },
1809 { "oR", NL ("|="), 2 },
1810 { "oo", NL ("||"), 2 },
1811 { "or", NL ("|"), 2 },
1812 { "pL", NL ("+="), 2 },
1813 { "pl", NL ("+"), 2 },
1814 { "pm", NL ("->*"), 2 },
1815 { "pp", NL ("++"), 1 },
1816 { "ps", NL ("+"), 1 },
1817 { "pt", NL ("->"), 2 },
1818 { "qu", NL ("?"), 3 },
1819 { "rM", NL ("%="), 2 },
1820 { "rS", NL (">>="), 2 },
1821 { "rc", NL ("reinterpret_cast"), 2 },
1822 { "rm", NL ("%"), 2 },
1823 { "rs", NL (">>"), 2 },
1824 { "sP", NL ("sizeof..."), 1 },
1825 { "sZ", NL ("sizeof..."), 1 },
1826 { "sc", NL ("static_cast"), 2 },
1827 { "st", NL ("sizeof "), 1 },
1828 { "sz", NL ("sizeof "), 1 },
1829 { "tr", NL ("throw"), 0 },
1830 { "tw", NL ("throw "), 1 },
1831 { NULL, NULL, 0, 0 }
1834 static struct demangle_component *
1835 d_operator_name (struct d_info *di)
1840 c1 = d_next_char (di);
1841 c2 = d_next_char (di);
1842 if (c1 == 'v' && IS_DIGIT (c2))
1843 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1844 else if (c1 == 'c' && c2 == 'v')
1846 struct demangle_component *type;
1847 int was_conversion = di->is_conversion;
1848 struct demangle_component *res;
1850 di->is_conversion = ! di->is_expression;
1851 type = cplus_demangle_type (di);
1852 if (di->is_conversion)
1853 res = d_make_comp (di, DEMANGLE_COMPONENT_CONVERSION, type, NULL);
1855 res = d_make_comp (di, DEMANGLE_COMPONENT_CAST, type, NULL);
1856 di->is_conversion = was_conversion;
1861 /* LOW is the inclusive lower bound. */
1863 /* HIGH is the exclusive upper bound. We subtract one to ignore
1864 the sentinel at the end of the array. */
1865 int high = ((sizeof (cplus_demangle_operators)
1866 / sizeof (cplus_demangle_operators[0]))
1872 const struct demangle_operator_info *p;
1874 i = low + (high - low) / 2;
1875 p = cplus_demangle_operators + i;
1877 if (c1 == p->code[0] && c2 == p->code[1])
1878 return d_make_operator (di, p);
1880 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1890 static struct demangle_component *
1891 d_make_character (struct d_info *di, int c)
1893 struct demangle_component *p;
1894 p = d_make_empty (di);
1897 p->type = DEMANGLE_COMPONENT_CHARACTER;
1898 p->u.s_character.character = c;
1903 static struct demangle_component *
1904 d_java_resource (struct d_info *di)
1906 struct demangle_component *p = NULL;
1907 struct demangle_component *next = NULL;
1912 len = d_number (di);
1916 /* Eat the leading '_'. */
1917 if (d_next_char (di) != '_')
1930 /* Each chunk is either a '$' escape... */
1948 next = d_make_character (di, c);
1956 /* ... or a sequence of characters. */
1959 while (i < len && str[i] && str[i] != '$')
1962 next = d_make_name (di, str, i);
1975 p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next);
1981 p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL);
1986 /* <special-name> ::= TV <type>
1990 ::= GV <(object) name>
1991 ::= T <call-offset> <(base) encoding>
1992 ::= Tc <call-offset> <call-offset> <(base) encoding>
1993 Also g++ extensions:
1994 ::= TC <type> <(offset) number> _ <(base) type>
1999 ::= Gr <resource name>
2004 static struct demangle_component *
2005 d_special_name (struct d_info *di)
2007 di->expansion += 20;
2008 if (d_check_char (di, 'T'))
2010 switch (d_next_char (di))
2014 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
2015 cplus_demangle_type (di), NULL);
2017 di->expansion -= 10;
2018 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
2019 cplus_demangle_type (di), NULL);
2021 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
2022 cplus_demangle_type (di), NULL);
2024 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
2025 cplus_demangle_type (di), NULL);
2028 if (! d_call_offset (di, 'h'))
2030 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
2031 d_encoding (di, 0), NULL);
2034 if (! d_call_offset (di, 'v'))
2036 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
2037 d_encoding (di, 0), NULL);
2040 if (! d_call_offset (di, '\0'))
2042 if (! d_call_offset (di, '\0'))
2044 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
2045 d_encoding (di, 0), NULL);
2049 struct demangle_component *derived_type;
2051 struct demangle_component *base_type;
2053 derived_type = cplus_demangle_type (di);
2054 offset = d_number (di);
2057 if (! d_check_char (di, '_'))
2059 base_type = cplus_demangle_type (di);
2060 /* We don't display the offset. FIXME: We should display
2061 it in verbose mode. */
2063 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
2064 base_type, derived_type);
2068 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
2069 cplus_demangle_type (di), NULL);
2071 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
2072 cplus_demangle_type (di), NULL);
2075 return d_make_comp (di, DEMANGLE_COMPONENT_TLS_INIT,
2079 return d_make_comp (di, DEMANGLE_COMPONENT_TLS_WRAPPER,
2086 else if (d_check_char (di, 'G'))
2088 switch (d_next_char (di))
2091 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
2095 struct demangle_component *name = d_name (di);
2096 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, name,
2097 d_number_component (di));
2101 return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
2102 d_encoding (di, 0), NULL);
2105 switch (d_next_char (di))
2108 return d_make_comp (di, DEMANGLE_COMPONENT_NONTRANSACTION_CLONE,
2109 d_encoding (di, 0), NULL);
2111 /* ??? The proposal is that other letters (such as 'h') stand
2112 for different variants of transaction cloning, such as
2113 compiling directly for hardware transaction support. But
2114 they still should all be transactional clones of some sort
2115 so go ahead and call them that. */
2117 return d_make_comp (di, DEMANGLE_COMPONENT_TRANSACTION_CLONE,
2118 d_encoding (di, 0), NULL);
2122 return d_java_resource (di);
2132 /* <call-offset> ::= h <nv-offset> _
2135 <nv-offset> ::= <(offset) number>
2137 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
2139 The C parameter, if not '\0', is a character we just read which is
2140 the start of the <call-offset>.
2142 We don't display the offset information anywhere. FIXME: We should
2143 display it in verbose mode. */
2146 d_call_offset (struct d_info *di, int c)
2149 c = d_next_char (di);
2156 if (! d_check_char (di, '_'))
2163 if (! d_check_char (di, '_'))
2169 /* <ctor-dtor-name> ::= C1
2177 static struct demangle_component *
2178 d_ctor_dtor_name (struct d_info *di)
2180 if (di->last_name != NULL)
2182 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
2183 di->expansion += di->last_name->u.s_name.len;
2184 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
2185 di->expansion += di->last_name->u.s_string.len;
2187 switch (d_peek_char (di))
2191 enum gnu_v3_ctor_kinds kind;
2194 if (d_peek_next_char (di) == 'I')
2200 switch (d_peek_next_char (di))
2203 kind = gnu_v3_complete_object_ctor;
2206 kind = gnu_v3_base_object_ctor;
2209 kind = gnu_v3_complete_object_allocating_ctor;
2212 kind = gnu_v3_unified_ctor;
2215 kind = gnu_v3_object_ctor_group;
2224 cplus_demangle_type (di);
2226 return d_make_ctor (di, kind, di->last_name);
2231 enum gnu_v3_dtor_kinds kind;
2233 switch (d_peek_next_char (di))
2236 kind = gnu_v3_deleting_dtor;
2239 kind = gnu_v3_complete_object_dtor;
2242 kind = gnu_v3_base_object_dtor;
2244 /* digit '3' is not used */
2246 kind = gnu_v3_unified_dtor;
2249 kind = gnu_v3_object_dtor_group;
2255 return d_make_dtor (di, kind, di->last_name);
2263 /* True iff we're looking at an order-insensitive type-qualifier, including
2264 function-type-qualifiers. */
2267 next_is_type_qual (struct d_info *di)
2269 char peek = d_peek_char (di);
2270 if (peek == 'r' || peek == 'V' || peek == 'K')
2274 peek = d_peek_next_char (di);
2275 if (peek == 'x' || peek == 'o' || peek == 'O' || peek == 'w')
2281 /* <type> ::= <builtin-type>
2283 ::= <class-enum-type>
2285 ::= <pointer-to-member-type>
2286 ::= <template-param>
2287 ::= <template-template-param> <template-args>
2289 ::= <CV-qualifiers> <type>
2292 ::= O <type> (C++0x)
2295 ::= U <source-name> <type>
2297 <builtin-type> ::= various one letter codes
2301 CP_STATIC_IF_GLIBCPP_V3
2302 const struct demangle_builtin_type_info
2303 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
2305 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
2306 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
2307 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
2308 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
2309 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
2310 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
2311 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
2312 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
2313 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
2314 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
2315 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2316 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
2317 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
2318 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
2319 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
2321 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2322 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2323 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2324 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
2325 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
2326 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2327 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
2328 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
2329 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
2330 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
2331 D_PRINT_UNSIGNED_LONG_LONG },
2332 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
2333 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT },
2334 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT },
2335 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT },
2336 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT },
2337 /* 30 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT },
2338 /* 31 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT },
2339 /* 32 */ { NL ("decltype(nullptr)"), NL ("decltype(nullptr)"),
2343 CP_STATIC_IF_GLIBCPP_V3
2344 struct demangle_component *
2345 cplus_demangle_type (struct d_info *di)
2348 struct demangle_component *ret;
2351 /* The ABI specifies that when CV-qualifiers are used, the base type
2352 is substitutable, and the fully qualified type is substitutable,
2353 but the base type with a strict subset of the CV-qualifiers is
2354 not substitutable. The natural recursive implementation of the
2355 CV-qualifiers would cause subsets to be substitutable, so instead
2356 we pull them all off now.
2358 FIXME: The ABI says that order-insensitive vendor qualifiers
2359 should be handled in the same way, but we have no way to tell
2360 which vendor qualifiers are order-insensitive and which are
2361 order-sensitive. So we just assume that they are all
2362 order-sensitive. g++ 3.4 supports only one vendor qualifier,
2363 __vector, and it treats it as order-sensitive when mangling
2366 if (next_is_type_qual (di))
2368 struct demangle_component **pret;
2370 pret = d_cv_qualifiers (di, &ret, 0);
2373 if (d_peek_char (di) == 'F')
2375 /* cv-qualifiers before a function type apply to 'this',
2376 so avoid adding the unqualified function type to
2377 the substitution list. */
2378 *pret = d_function_type (di);
2381 *pret = cplus_demangle_type (di);
2384 if ((*pret)->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
2385 || (*pret)->type == DEMANGLE_COMPONENT_REFERENCE_THIS)
2387 /* Move the ref-qualifier outside the cv-qualifiers so that
2388 they are printed in the right order. */
2389 struct demangle_component *fn = d_left (*pret);
2390 d_left (*pret) = ret;
2394 if (! d_add_substitution (di, ret))
2401 peek = d_peek_char (di);
2404 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2405 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
2406 case 'o': case 's': case 't':
2407 case 'v': case 'w': case 'x': case 'y': case 'z':
2408 ret = d_make_builtin_type (di,
2409 &cplus_demangle_builtin_types[peek - 'a']);
2410 di->expansion += ret->u.s_builtin.type->len;
2417 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
2418 d_source_name (di), NULL);
2422 ret = d_function_type (di);
2425 case '0': case '1': case '2': case '3': case '4':
2426 case '5': case '6': case '7': case '8': case '9':
2429 ret = d_class_enum_type (di);
2433 ret = d_array_type (di);
2437 ret = d_pointer_to_member_type (di);
2441 ret = d_template_param (di);
2442 if (d_peek_char (di) == 'I')
2444 /* This may be <template-template-param> <template-args>.
2445 If this is the type for a conversion operator, we can
2446 have a <template-template-param> here only by following
2447 a derivation like this:
2450 -> <template-prefix> <template-args>
2451 -> <prefix> <template-unqualified-name> <template-args>
2452 -> <unqualified-name> <template-unqualified-name> <template-args>
2453 -> <source-name> <template-unqualified-name> <template-args>
2454 -> <source-name> <operator-name> <template-args>
2455 -> <source-name> cv <type> <template-args>
2456 -> <source-name> cv <template-template-param> <template-args> <template-args>
2458 where the <template-args> is followed by another.
2459 Otherwise, we must have a derivation like this:
2462 -> <template-prefix> <template-args>
2463 -> <prefix> <template-unqualified-name> <template-args>
2464 -> <unqualified-name> <template-unqualified-name> <template-args>
2465 -> <source-name> <template-unqualified-name> <template-args>
2466 -> <source-name> <operator-name> <template-args>
2467 -> <source-name> cv <type> <template-args>
2468 -> <source-name> cv <template-param> <template-args>
2470 where we need to leave the <template-args> to be processed
2471 by d_prefix (following the <template-prefix>).
2473 The <template-template-param> part is a substitution
2475 if (! di->is_conversion)
2477 if (! d_add_substitution (di, ret))
2479 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2480 d_template_args (di));
2484 struct demangle_component *args;
2485 struct d_info_checkpoint checkpoint;
2487 d_checkpoint (di, &checkpoint);
2488 args = d_template_args (di);
2489 if (d_peek_char (di) == 'I')
2491 if (! d_add_substitution (di, ret))
2493 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2497 d_backtrack (di, &checkpoint);
2503 /* If this is a special substitution, then it is the start of
2504 <class-enum-type>. */
2508 peek_next = d_peek_next_char (di);
2509 if (IS_DIGIT (peek_next)
2511 || IS_UPPER (peek_next))
2513 ret = d_substitution (di, 0);
2514 /* The substituted name may have been a template name and
2515 may be followed by tepmlate args. */
2516 if (d_peek_char (di) == 'I')
2517 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2518 d_template_args (di));
2524 ret = d_class_enum_type (di);
2525 /* If the substitution was a complete type, then it is not
2526 a new substitution candidate. However, if the
2527 substitution was followed by template arguments, then
2528 the whole thing is a substitution candidate. */
2529 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
2537 ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE,
2538 cplus_demangle_type (di), NULL);
2543 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
2544 cplus_demangle_type (di), NULL);
2549 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
2550 cplus_demangle_type (di), NULL);
2555 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
2556 cplus_demangle_type (di), NULL);
2561 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
2562 cplus_demangle_type (di), NULL);
2567 ret = d_source_name (di);
2568 if (d_peek_char (di) == 'I')
2569 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2570 d_template_args (di));
2571 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
2572 cplus_demangle_type (di), ret);
2578 peek = d_next_char (di);
2583 /* decltype (expression) */
2584 ret = d_make_comp (di, DEMANGLE_COMPONENT_DECLTYPE,
2585 d_expression (di), NULL);
2586 if (ret && d_next_char (di) != 'E')
2592 /* Pack expansion. */
2593 ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2594 cplus_demangle_type (di), NULL);
2600 ret = d_make_name (di, "auto", 4);
2603 /* decltype(auto) */
2604 ret = d_make_name (di, "decltype(auto)", 14);
2608 /* 32-bit decimal floating point */
2609 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[26]);
2610 di->expansion += ret->u.s_builtin.type->len;
2614 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[27]);
2615 di->expansion += ret->u.s_builtin.type->len;
2619 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[28]);
2620 di->expansion += ret->u.s_builtin.type->len;
2623 /* 16-bit half-precision FP */
2624 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[29]);
2625 di->expansion += ret->u.s_builtin.type->len;
2629 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[30]);
2630 di->expansion += ret->u.s_builtin.type->len;
2634 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[31]);
2635 di->expansion += ret->u.s_builtin.type->len;
2639 /* Fixed point types. DF<int bits><length><fract bits><sat> */
2640 ret = d_make_empty (di);
2641 ret->type = DEMANGLE_COMPONENT_FIXED_TYPE;
2642 if ((ret->u.s_fixed.accum = IS_DIGIT (d_peek_char (di))))
2643 /* For demangling we don't care about the bits. */
2645 ret->u.s_fixed.length = cplus_demangle_type (di);
2646 if (ret->u.s_fixed.length == NULL)
2649 peek = d_next_char (di);
2650 ret->u.s_fixed.sat = (peek == 's');
2654 ret = d_vector_type (di);
2659 /* decltype(nullptr) */
2660 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[32]);
2661 di->expansion += ret->u.s_builtin.type->len;
2675 if (! d_add_substitution (di, ret))
2682 /* <CV-qualifiers> ::= [r] [V] [K] [Dx] */
2684 static struct demangle_component **
2685 d_cv_qualifiers (struct d_info *di,
2686 struct demangle_component **pret, int member_fn)
2688 struct demangle_component **pstart;
2692 peek = d_peek_char (di);
2693 while (next_is_type_qual (di))
2695 enum demangle_component_type t;
2696 struct demangle_component *right = NULL;
2702 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2703 : DEMANGLE_COMPONENT_RESTRICT);
2704 di->expansion += sizeof "restrict";
2706 else if (peek == 'V')
2709 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2710 : DEMANGLE_COMPONENT_VOLATILE);
2711 di->expansion += sizeof "volatile";
2713 else if (peek == 'K')
2716 ? DEMANGLE_COMPONENT_CONST_THIS
2717 : DEMANGLE_COMPONENT_CONST);
2718 di->expansion += sizeof "const";
2722 peek = d_next_char (di);
2725 t = DEMANGLE_COMPONENT_TRANSACTION_SAFE;
2726 di->expansion += sizeof "transaction_safe";
2728 else if (peek == 'o'
2731 t = DEMANGLE_COMPONENT_NOEXCEPT;
2732 di->expansion += sizeof "noexcept";
2735 right = d_expression (di);
2738 if (! d_check_char (di, 'E'))
2742 else if (peek == 'w')
2744 t = DEMANGLE_COMPONENT_THROW_SPEC;
2745 di->expansion += sizeof "throw";
2746 right = d_parmlist (di);
2749 if (! d_check_char (di, 'E'))
2756 *pret = d_make_comp (di, t, NULL, right);
2759 pret = &d_left (*pret);
2761 peek = d_peek_char (di);
2764 if (!member_fn && peek == 'F')
2766 while (pstart != pret)
2768 switch ((*pstart)->type)
2770 case DEMANGLE_COMPONENT_RESTRICT:
2771 (*pstart)->type = DEMANGLE_COMPONENT_RESTRICT_THIS;
2773 case DEMANGLE_COMPONENT_VOLATILE:
2774 (*pstart)->type = DEMANGLE_COMPONENT_VOLATILE_THIS;
2776 case DEMANGLE_COMPONENT_CONST:
2777 (*pstart)->type = DEMANGLE_COMPONENT_CONST_THIS;
2782 pstart = &d_left (*pstart);
2789 /* <ref-qualifier> ::= R
2792 static struct demangle_component *
2793 d_ref_qualifier (struct d_info *di, struct demangle_component *sub)
2795 struct demangle_component *ret = sub;
2798 peek = d_peek_char (di);
2799 if (peek == 'R' || peek == 'O')
2801 enum demangle_component_type t;
2804 t = DEMANGLE_COMPONENT_REFERENCE_THIS;
2805 di->expansion += sizeof "&";
2809 t = DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS;
2810 di->expansion += sizeof "&&";
2814 ret = d_make_comp (di, t, ret, NULL);
2820 /* <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] [T] E */
2822 static struct demangle_component *
2823 d_function_type (struct d_info *di)
2825 struct demangle_component *ret;
2827 if (! d_check_char (di, 'F'))
2829 if (d_peek_char (di) == 'Y')
2831 /* Function has C linkage. We don't print this information.
2832 FIXME: We should print it in verbose mode. */
2835 ret = d_bare_function_type (di, 1);
2836 ret = d_ref_qualifier (di, ret);
2838 if (! d_check_char (di, 'E'))
2845 static struct demangle_component *
2846 d_parmlist (struct d_info *di)
2848 struct demangle_component *tl;
2849 struct demangle_component **ptl;
2855 struct demangle_component *type;
2857 char peek = d_peek_char (di);
2858 if (peek == '\0' || peek == 'E' || peek == '.')
2860 if ((peek == 'R' || peek == 'O')
2861 && d_peek_next_char (di) == 'E')
2862 /* Function ref-qualifier, not a ref prefix for a parameter type. */
2864 type = cplus_demangle_type (di);
2867 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
2870 ptl = &d_right (*ptl);
2873 /* There should be at least one parameter type besides the optional
2874 return type. A function which takes no arguments will have a
2875 single parameter type void. */
2879 /* If we have a single parameter type void, omit it. */
2880 if (d_right (tl) == NULL
2881 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2882 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
2884 di->expansion -= d_left (tl)->u.s_builtin.type->len;
2891 /* <bare-function-type> ::= [J]<type>+ */
2893 static struct demangle_component *
2894 d_bare_function_type (struct d_info *di, int has_return_type)
2896 struct demangle_component *return_type;
2897 struct demangle_component *tl;
2900 /* Detect special qualifier indicating that the first argument
2901 is the return type. */
2902 peek = d_peek_char (di);
2906 has_return_type = 1;
2909 if (has_return_type)
2911 return_type = cplus_demangle_type (di);
2912 if (return_type == NULL)
2918 tl = d_parmlist (di);
2922 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE,
2926 /* <class-enum-type> ::= <name> */
2928 static struct demangle_component *
2929 d_class_enum_type (struct d_info *di)
2934 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2935 ::= A [<(dimension) expression>] _ <(element) type>
2938 static struct demangle_component *
2939 d_array_type (struct d_info *di)
2942 struct demangle_component *dim;
2944 if (! d_check_char (di, 'A'))
2947 peek = d_peek_char (di);
2950 else if (IS_DIGIT (peek))
2958 peek = d_peek_char (di);
2960 while (IS_DIGIT (peek));
2961 dim = d_make_name (di, s, d_str (di) - s);
2967 dim = d_expression (di);
2972 if (! d_check_char (di, '_'))
2975 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2976 cplus_demangle_type (di));
2979 /* <vector-type> ::= Dv <number> _ <type>
2980 ::= Dv _ <expression> _ <type> */
2982 static struct demangle_component *
2983 d_vector_type (struct d_info *di)
2986 struct demangle_component *dim;
2988 peek = d_peek_char (di);
2992 dim = d_expression (di);
2995 dim = d_number_component (di);
3000 if (! d_check_char (di, '_'))
3003 return d_make_comp (di, DEMANGLE_COMPONENT_VECTOR_TYPE, dim,
3004 cplus_demangle_type (di));
3007 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
3009 static struct demangle_component *
3010 d_pointer_to_member_type (struct d_info *di)
3012 struct demangle_component *cl;
3013 struct demangle_component *mem;
3015 if (! d_check_char (di, 'M'))
3018 cl = cplus_demangle_type (di);
3022 /* The ABI says, "The type of a non-static member function is considered
3023 to be different, for the purposes of substitution, from the type of a
3024 namespace-scope or static member function whose type appears
3025 similar. The types of two non-static member functions are considered
3026 to be different, for the purposes of substitution, if the functions
3027 are members of different classes. In other words, for the purposes of
3028 substitution, the class of which the function is a member is
3029 considered part of the type of function."
3031 For a pointer to member function, this call to cplus_demangle_type
3032 will end up adding a (possibly qualified) non-member function type to
3033 the substitution table, which is not correct; however, the member
3034 function type will never be used in a substitution, so putting the
3035 wrong type in the substitution table is harmless. */
3037 mem = cplus_demangle_type (di);
3041 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
3044 /* <non-negative number> _ */
3047 d_compact_number (struct d_info *di)
3050 if (d_peek_char (di) == '_')
3052 else if (d_peek_char (di) == 'n')
3055 num = d_number (di) + 1;
3057 if (num < 0 || ! d_check_char (di, '_'))
3062 /* <template-param> ::= T_
3063 ::= T <(parameter-2 non-negative) number> _
3066 static struct demangle_component *
3067 d_template_param (struct d_info *di)
3071 if (! d_check_char (di, 'T'))
3074 param = d_compact_number (di);
3080 return d_make_template_param (di, param);
3083 /* <template-args> ::= I <template-arg>+ E */
3085 static struct demangle_component *
3086 d_template_args (struct d_info *di)
3088 if (d_peek_char (di) != 'I'
3089 && d_peek_char (di) != 'J')
3093 return d_template_args_1 (di);
3096 /* <template-arg>* E */
3098 static struct demangle_component *
3099 d_template_args_1 (struct d_info *di)
3101 struct demangle_component *hold_last_name;
3102 struct demangle_component *al;
3103 struct demangle_component **pal;
3105 /* Preserve the last name we saw--don't let the template arguments
3106 clobber it, as that would give us the wrong name for a subsequent
3107 constructor or destructor. */
3108 hold_last_name = di->last_name;
3110 if (d_peek_char (di) == 'E')
3112 /* An argument pack can be empty. */
3114 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, NULL, NULL);
3121 struct demangle_component *a;
3123 a = d_template_arg (di);
3127 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
3130 pal = &d_right (*pal);
3132 if (d_peek_char (di) == 'E')
3139 di->last_name = hold_last_name;
3144 /* <template-arg> ::= <type>
3145 ::= X <expression> E
3149 static struct demangle_component *
3150 d_template_arg (struct d_info *di)
3152 struct demangle_component *ret;
3154 switch (d_peek_char (di))
3158 ret = d_expression (di);
3159 if (! d_check_char (di, 'E'))
3164 return d_expr_primary (di);
3168 /* An argument pack. */
3169 return d_template_args (di);
3172 return cplus_demangle_type (di);
3176 /* Parse a sequence of expressions until we hit the terminator
3179 static struct demangle_component *
3180 d_exprlist (struct d_info *di, char terminator)
3182 struct demangle_component *list = NULL;
3183 struct demangle_component **p = &list;
3185 if (d_peek_char (di) == terminator)
3188 return d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, NULL, NULL);
3193 struct demangle_component *arg = d_expression (di);
3197 *p = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, arg, NULL);
3202 if (d_peek_char (di) == terminator)
3212 /* Returns nonzero iff OP is an operator for a C++ cast: const_cast,
3213 dynamic_cast, static_cast or reinterpret_cast. */
3216 op_is_new_cast (struct demangle_component *op)
3218 const char *code = op->u.s_operator.op->code;
3219 return (code[1] == 'c'
3220 && (code[0] == 's' || code[0] == 'd'
3221 || code[0] == 'c' || code[0] == 'r'));
3224 /* <expression> ::= <(unary) operator-name> <expression>
3225 ::= <(binary) operator-name> <expression> <expression>
3226 ::= <(trinary) operator-name> <expression> <expression> <expression>
3227 ::= cl <expression>+ E
3229 ::= <template-param>
3230 ::= sr <type> <unqualified-name>
3231 ::= sr <type> <unqualified-name> <template-args>
3235 static inline struct demangle_component *
3236 d_expression_1 (struct d_info *di)
3240 peek = d_peek_char (di);
3242 return d_expr_primary (di);
3243 else if (peek == 'T')
3244 return d_template_param (di);
3245 else if (peek == 's' && d_peek_next_char (di) == 'r')
3247 struct demangle_component *type;
3248 struct demangle_component *name;
3251 type = cplus_demangle_type (di);
3252 name = d_unqualified_name (di);
3253 if (d_peek_char (di) != 'I')
3254 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
3256 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
3257 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
3258 d_template_args (di)));
3260 else if (peek == 's' && d_peek_next_char (di) == 'p')
3263 return d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
3264 d_expression_1 (di), NULL);
3266 else if (peek == 'f' && d_peek_next_char (di) == 'p')
3268 /* Function parameter used in a late-specified return type. */
3271 if (d_peek_char (di) == 'T')
3273 /* 'this' parameter. */
3279 index = d_compact_number (di);
3280 if (index == INT_MAX || index == -1)
3284 return d_make_function_param (di, index);
3286 else if (IS_DIGIT (peek)
3287 || (peek == 'o' && d_peek_next_char (di) == 'n'))
3289 /* We can get an unqualified name as an expression in the case of
3290 a dependent function call, i.e. decltype(f(t)). */
3291 struct demangle_component *name;
3294 /* operator-function-id, i.e. operator+(t). */
3297 name = d_unqualified_name (di);
3300 if (d_peek_char (di) == 'I')
3301 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
3302 d_template_args (di));
3306 else if ((peek == 'i' || peek == 't')
3307 && d_peek_next_char (di) == 'l')
3309 /* Brace-enclosed initializer list, untyped or typed. */
3310 struct demangle_component *type = NULL;
3312 type = cplus_demangle_type (di);
3313 if (!d_peek_next_char (di))
3316 return d_make_comp (di, DEMANGLE_COMPONENT_INITIALIZER_LIST,
3317 type, d_exprlist (di, 'E'));
3321 struct demangle_component *op;
3322 const char *code = NULL;
3325 op = d_operator_name (di);
3329 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
3331 code = op->u.s_operator.op->code;
3332 di->expansion += op->u.s_operator.op->len - 2;
3333 if (strcmp (code, "st") == 0)
3334 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3335 cplus_demangle_type (di));
3342 case DEMANGLE_COMPONENT_OPERATOR:
3343 args = op->u.s_operator.op->args;
3345 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3346 args = op->u.s_extended_operator.args;
3348 case DEMANGLE_COMPONENT_CAST:
3356 return d_make_comp (di, DEMANGLE_COMPONENT_NULLARY, op, NULL);
3360 struct demangle_component *operand;
3363 if (code && (code[0] == 'p' || code[0] == 'm')
3364 && code[1] == code[0])
3365 /* pp_ and mm_ are the prefix variants. */
3366 suffix = !d_check_char (di, '_');
3368 if (op->type == DEMANGLE_COMPONENT_CAST
3369 && d_check_char (di, '_'))
3370 operand = d_exprlist (di, 'E');
3371 else if (code && !strcmp (code, "sP"))
3372 operand = d_template_args_1 (di);
3374 operand = d_expression_1 (di);
3377 /* Indicate the suffix variant for d_print_comp. */
3378 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3380 DEMANGLE_COMPONENT_BINARY_ARGS,
3383 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3388 struct demangle_component *left;
3389 struct demangle_component *right;
3393 if (op_is_new_cast (op))
3394 left = cplus_demangle_type (di);
3395 else if (code[0] == 'f')
3396 /* fold-expression. */
3397 left = d_operator_name (di);
3399 left = d_expression_1 (di);
3400 if (!strcmp (code, "cl"))
3401 right = d_exprlist (di, 'E');
3402 else if (!strcmp (code, "dt") || !strcmp (code, "pt"))
3404 right = d_unqualified_name (di);
3405 if (d_peek_char (di) == 'I')
3406 right = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE,
3407 right, d_template_args (di));
3410 right = d_expression_1 (di);
3412 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
3414 DEMANGLE_COMPONENT_BINARY_ARGS,
3419 struct demangle_component *first;
3420 struct demangle_component *second;
3421 struct demangle_component *third;
3425 else if (!strcmp (code, "qu"))
3427 /* ?: expression. */
3428 first = d_expression_1 (di);
3429 second = d_expression_1 (di);
3430 third = d_expression_1 (di);
3434 else if (code[0] == 'f')
3436 /* fold-expression. */
3437 first = d_operator_name (di);
3438 second = d_expression_1 (di);
3439 third = d_expression_1 (di);
3443 else if (code[0] == 'n')
3445 /* new-expression. */
3446 if (code[1] != 'w' && code[1] != 'a')
3448 first = d_exprlist (di, '_');
3449 second = cplus_demangle_type (di);
3450 if (d_peek_char (di) == 'E')
3455 else if (d_peek_char (di) == 'p'
3456 && d_peek_next_char (di) == 'i')
3458 /* Parenthesized initializer. */
3460 third = d_exprlist (di, 'E');
3462 else if (d_peek_char (di) == 'i'
3463 && d_peek_next_char (di) == 'l')
3464 /* initializer-list. */
3465 third = d_expression_1 (di);
3471 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
3473 DEMANGLE_COMPONENT_TRINARY_ARG1,
3476 DEMANGLE_COMPONENT_TRINARY_ARG2,
3485 static struct demangle_component *
3486 d_expression (struct d_info *di)
3488 struct demangle_component *ret;
3489 int was_expression = di->is_expression;
3491 di->is_expression = 1;
3492 ret = d_expression_1 (di);
3493 di->is_expression = was_expression;
3497 /* <expr-primary> ::= L <type> <(value) number> E
3498 ::= L <type> <(value) float> E
3499 ::= L <mangled-name> E
3502 static struct demangle_component *
3503 d_expr_primary (struct d_info *di)
3505 struct demangle_component *ret;
3507 if (! d_check_char (di, 'L'))
3509 if (d_peek_char (di) == '_'
3510 /* Workaround for G++ bug; see comment in write_template_arg. */
3511 || d_peek_char (di) == 'Z')
3512 ret = cplus_demangle_mangled_name (di, 0);
3515 struct demangle_component *type;
3516 enum demangle_component_type t;
3519 type = cplus_demangle_type (di);
3523 /* If we have a type we know how to print, we aren't going to
3524 print the type name itself. */
3525 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
3526 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
3527 di->expansion -= type->u.s_builtin.type->len;
3529 /* Rather than try to interpret the literal value, we just
3530 collect it as a string. Note that it's possible to have a
3531 floating point literal here. The ABI specifies that the
3532 format of such literals is machine independent. That's fine,
3533 but what's not fine is that versions of g++ up to 3.2 with
3534 -fabi-version=1 used upper case letters in the hex constant,
3535 and dumped out gcc's internal representation. That makes it
3536 hard to tell where the constant ends, and hard to dump the
3537 constant in any readable form anyhow. We don't attempt to
3538 handle these cases. */
3540 t = DEMANGLE_COMPONENT_LITERAL;
3541 if (d_peek_char (di) == 'n')
3543 t = DEMANGLE_COMPONENT_LITERAL_NEG;
3547 while (d_peek_char (di) != 'E')
3549 if (d_peek_char (di) == '\0')
3553 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
3555 if (! d_check_char (di, 'E'))
3560 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
3561 ::= Z <(function) encoding> E s [<discriminator>]
3562 ::= Z <(function) encoding> E d [<parameter> number>] _ <entity name>
3565 static struct demangle_component *
3566 d_local_name (struct d_info *di)
3568 struct demangle_component *function;
3570 if (! d_check_char (di, 'Z'))
3573 function = d_encoding (di, 0);
3575 if (! d_check_char (di, 'E'))
3578 if (d_peek_char (di) == 's')
3581 if (! d_discriminator (di))
3583 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
3584 d_make_name (di, "string literal",
3585 sizeof "string literal" - 1));
3589 struct demangle_component *name;
3592 if (d_peek_char (di) == 'd')
3594 /* Default argument scope: d <number> _. */
3596 num = d_compact_number (di);
3605 /* Lambdas and unnamed types have internal discriminators. */
3606 case DEMANGLE_COMPONENT_LAMBDA:
3607 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
3610 if (! d_discriminator (di))
3614 name = d_make_default_arg (di, num, name);
3615 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
3619 /* <discriminator> ::= _ <number> # when number < 10
3620 ::= __ <number> _ # when number >= 10
3622 <discriminator> ::= _ <number> # when number >=10
3623 is also accepted to support gcc versions that wrongly mangled that way.
3625 We demangle the discriminator, but we don't print it out. FIXME:
3626 We should print it out in verbose mode. */
3629 d_discriminator (struct d_info *di)
3631 int discrim, num_underscores = 1;
3633 if (d_peek_char (di) != '_')
3636 if (d_peek_char (di) == '_')
3642 discrim = d_number (di);
3645 if (num_underscores > 1 && discrim >= 10)
3647 if (d_peek_char (di) == '_')
3656 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ */
3658 static struct demangle_component *
3659 d_lambda (struct d_info *di)
3661 struct demangle_component *tl;
3662 struct demangle_component *ret;
3665 if (! d_check_char (di, 'U'))
3667 if (! d_check_char (di, 'l'))
3670 tl = d_parmlist (di);
3674 if (! d_check_char (di, 'E'))
3677 num = d_compact_number (di);
3681 ret = d_make_empty (di);
3684 ret->type = DEMANGLE_COMPONENT_LAMBDA;
3685 ret->u.s_unary_num.sub = tl;
3686 ret->u.s_unary_num.num = num;
3689 if (! d_add_substitution (di, ret))
3695 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
3697 static struct demangle_component *
3698 d_unnamed_type (struct d_info *di)
3700 struct demangle_component *ret;
3703 if (! d_check_char (di, 'U'))
3705 if (! d_check_char (di, 't'))
3708 num = d_compact_number (di);
3712 ret = d_make_empty (di);
3715 ret->type = DEMANGLE_COMPONENT_UNNAMED_TYPE;
3716 ret->u.s_number.number = num;
3719 if (! d_add_substitution (di, ret))
3725 /* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]*
3728 static struct demangle_component *
3729 d_clone_suffix (struct d_info *di, struct demangle_component *encoding)
3731 const char *suffix = d_str (di);
3732 const char *pend = suffix;
3733 struct demangle_component *n;
3735 if (*pend == '.' && (IS_LOWER (pend[1]) || pend[1] == '_'))
3738 while (IS_LOWER (*pend) || *pend == '_')
3741 while (*pend == '.' && IS_DIGIT (pend[1]))
3744 while (IS_DIGIT (*pend))
3747 d_advance (di, pend - suffix);
3748 n = d_make_name (di, suffix, pend - suffix);
3749 return d_make_comp (di, DEMANGLE_COMPONENT_CLONE, encoding, n);
3752 /* Add a new substitution. */
3755 d_add_substitution (struct d_info *di, struct demangle_component *dc)
3759 if (di->next_sub >= di->num_subs)
3761 di->subs[di->next_sub] = dc;
3766 /* <substitution> ::= S <seq-id> _
3776 If PREFIX is non-zero, then this type is being used as a prefix in
3777 a qualified name. In this case, for the standard substitutions, we
3778 need to check whether we are being used as a prefix for a
3779 constructor or destructor, and return a full template name.
3780 Otherwise we will get something like std::iostream::~iostream()
3781 which does not correspond particularly well to any function which
3782 actually appears in the source.
3785 static const struct d_standard_sub_info standard_subs[] =
3790 { 'a', NL ("std::allocator"),
3791 NL ("std::allocator"),
3793 { 'b', NL ("std::basic_string"),
3794 NL ("std::basic_string"),
3795 NL ("basic_string") },
3796 { 's', NL ("std::string"),
3797 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
3798 NL ("basic_string") },
3799 { 'i', NL ("std::istream"),
3800 NL ("std::basic_istream<char, std::char_traits<char> >"),
3801 NL ("basic_istream") },
3802 { 'o', NL ("std::ostream"),
3803 NL ("std::basic_ostream<char, std::char_traits<char> >"),
3804 NL ("basic_ostream") },
3805 { 'd', NL ("std::iostream"),
3806 NL ("std::basic_iostream<char, std::char_traits<char> >"),
3807 NL ("basic_iostream") }
3810 static struct demangle_component *
3811 d_substitution (struct d_info *di, int prefix)
3815 if (! d_check_char (di, 'S'))
3818 c = d_next_char (di);
3819 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
3828 unsigned int new_id;
3831 new_id = id * 36 + c - '0';
3832 else if (IS_UPPER (c))
3833 new_id = id * 36 + c - 'A' + 10;
3839 c = d_next_char (di);
3846 if (id >= (unsigned int) di->next_sub)
3851 return di->subs[id];
3856 const struct d_standard_sub_info *p;
3857 const struct d_standard_sub_info *pend;
3859 verbose = (di->options & DMGL_VERBOSE) != 0;
3860 if (! verbose && prefix)
3864 peek = d_peek_char (di);
3865 if (peek == 'C' || peek == 'D')
3869 pend = (&standard_subs[0]
3870 + sizeof standard_subs / sizeof standard_subs[0]);
3871 for (p = &standard_subs[0]; p < pend; ++p)
3877 struct demangle_component *dc;
3879 if (p->set_last_name != NULL)
3880 di->last_name = d_make_sub (di, p->set_last_name,
3881 p->set_last_name_len);
3884 s = p->full_expansion;
3889 s = p->simple_expansion;
3890 len = p->simple_len;
3892 di->expansion += len;
3893 dc = d_make_sub (di, s, len);
3894 if (d_peek_char (di) == 'B')
3896 /* If there are ABI tags on the abbreviation, it becomes
3897 a substitution candidate. */
3898 dc = d_abi_tags (di, dc);
3899 d_add_substitution (di, dc);
3910 d_checkpoint (struct d_info *di, struct d_info_checkpoint *checkpoint)
3912 checkpoint->n = di->n;
3913 checkpoint->next_comp = di->next_comp;
3914 checkpoint->next_sub = di->next_sub;
3915 checkpoint->did_subs = di->did_subs;
3916 checkpoint->expansion = di->expansion;
3920 d_backtrack (struct d_info *di, struct d_info_checkpoint *checkpoint)
3922 di->n = checkpoint->n;
3923 di->next_comp = checkpoint->next_comp;
3924 di->next_sub = checkpoint->next_sub;
3925 di->did_subs = checkpoint->did_subs;
3926 di->expansion = checkpoint->expansion;
3929 /* Initialize a growable string. */
3932 d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
3937 dgs->allocation_failure = 0;
3940 d_growable_string_resize (dgs, estimate);
3943 /* Grow a growable string to a given size. */
3946 d_growable_string_resize (struct d_growable_string *dgs, size_t need)
3951 if (dgs->allocation_failure)
3954 /* Start allocation at two bytes to avoid any possibility of confusion
3955 with the special value of 1 used as a return in *palc to indicate
3956 allocation failures. */
3957 newalc = dgs->alc > 0 ? dgs->alc : 2;
3958 while (newalc < need)
3961 newbuf = (char *) realloc (dgs->buf, newalc);
3968 dgs->allocation_failure = 1;
3975 /* Append a buffer to a growable string. */
3978 d_growable_string_append_buffer (struct d_growable_string *dgs,
3979 const char *s, size_t l)
3983 need = dgs->len + l + 1;
3984 if (need > dgs->alc)
3985 d_growable_string_resize (dgs, need);
3987 if (dgs->allocation_failure)
3990 memcpy (dgs->buf + dgs->len, s, l);
3991 dgs->buf[dgs->len + l] = '\0';
3995 /* Bridge growable strings to the callback mechanism. */
3998 d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
4000 struct d_growable_string *dgs = (struct d_growable_string*) opaque;
4002 d_growable_string_append_buffer (dgs, s, l);
4005 /* Walk the tree, counting the number of templates encountered, and
4006 the number of times a scope might be saved. These counts will be
4007 used to allocate data structures for d_print_comp, so the logic
4008 here must mirror the logic d_print_comp will use. It is not
4009 important that the resulting numbers are exact, so long as they
4010 are larger than the actual numbers encountered. */
4013 d_count_templates_scopes (int *num_templates, int *num_scopes,
4014 const struct demangle_component *dc)
4021 case DEMANGLE_COMPONENT_NAME:
4022 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
4023 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
4024 case DEMANGLE_COMPONENT_SUB_STD:
4025 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
4026 case DEMANGLE_COMPONENT_OPERATOR:
4027 case DEMANGLE_COMPONENT_CHARACTER:
4028 case DEMANGLE_COMPONENT_NUMBER:
4029 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
4032 case DEMANGLE_COMPONENT_TEMPLATE:
4034 goto recurse_left_right;
4036 case DEMANGLE_COMPONENT_REFERENCE:
4037 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4038 if (d_left (dc)->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
4040 goto recurse_left_right;
4042 case DEMANGLE_COMPONENT_QUAL_NAME:
4043 case DEMANGLE_COMPONENT_LOCAL_NAME:
4044 case DEMANGLE_COMPONENT_TYPED_NAME:
4045 case DEMANGLE_COMPONENT_VTABLE:
4046 case DEMANGLE_COMPONENT_VTT:
4047 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
4048 case DEMANGLE_COMPONENT_TYPEINFO:
4049 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
4050 case DEMANGLE_COMPONENT_TYPEINFO_FN:
4051 case DEMANGLE_COMPONENT_THUNK:
4052 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
4053 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
4054 case DEMANGLE_COMPONENT_JAVA_CLASS:
4055 case DEMANGLE_COMPONENT_GUARD:
4056 case DEMANGLE_COMPONENT_TLS_INIT:
4057 case DEMANGLE_COMPONENT_TLS_WRAPPER:
4058 case DEMANGLE_COMPONENT_REFTEMP:
4059 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
4060 case DEMANGLE_COMPONENT_RESTRICT:
4061 case DEMANGLE_COMPONENT_VOLATILE:
4062 case DEMANGLE_COMPONENT_CONST:
4063 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4064 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4065 case DEMANGLE_COMPONENT_CONST_THIS:
4066 case DEMANGLE_COMPONENT_REFERENCE_THIS:
4067 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
4068 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
4069 case DEMANGLE_COMPONENT_NOEXCEPT:
4070 case DEMANGLE_COMPONENT_THROW_SPEC:
4071 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4072 case DEMANGLE_COMPONENT_POINTER:
4073 case DEMANGLE_COMPONENT_COMPLEX:
4074 case DEMANGLE_COMPONENT_IMAGINARY:
4075 case DEMANGLE_COMPONENT_VENDOR_TYPE:
4076 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
4077 case DEMANGLE_COMPONENT_ARRAY_TYPE:
4078 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4079 case DEMANGLE_COMPONENT_VECTOR_TYPE:
4080 case DEMANGLE_COMPONENT_ARGLIST:
4081 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
4082 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
4083 case DEMANGLE_COMPONENT_CAST:
4084 case DEMANGLE_COMPONENT_CONVERSION:
4085 case DEMANGLE_COMPONENT_NULLARY:
4086 case DEMANGLE_COMPONENT_UNARY:
4087 case DEMANGLE_COMPONENT_BINARY:
4088 case DEMANGLE_COMPONENT_BINARY_ARGS:
4089 case DEMANGLE_COMPONENT_TRINARY:
4090 case DEMANGLE_COMPONENT_TRINARY_ARG1:
4091 case DEMANGLE_COMPONENT_TRINARY_ARG2:
4092 case DEMANGLE_COMPONENT_LITERAL:
4093 case DEMANGLE_COMPONENT_LITERAL_NEG:
4094 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
4095 case DEMANGLE_COMPONENT_COMPOUND_NAME:
4096 case DEMANGLE_COMPONENT_DECLTYPE:
4097 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
4098 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
4099 case DEMANGLE_COMPONENT_PACK_EXPANSION:
4100 case DEMANGLE_COMPONENT_TAGGED_NAME:
4101 case DEMANGLE_COMPONENT_CLONE:
4103 d_count_templates_scopes (num_templates, num_scopes,
4105 d_count_templates_scopes (num_templates, num_scopes,
4109 case DEMANGLE_COMPONENT_CTOR:
4110 d_count_templates_scopes (num_templates, num_scopes,
4114 case DEMANGLE_COMPONENT_DTOR:
4115 d_count_templates_scopes (num_templates, num_scopes,
4119 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
4120 d_count_templates_scopes (num_templates, num_scopes,
4121 dc->u.s_extended_operator.name);
4124 case DEMANGLE_COMPONENT_FIXED_TYPE:
4125 d_count_templates_scopes (num_templates, num_scopes,
4126 dc->u.s_fixed.length);
4129 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
4130 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
4131 d_count_templates_scopes (num_templates, num_scopes,
4135 case DEMANGLE_COMPONENT_LAMBDA:
4136 case DEMANGLE_COMPONENT_DEFAULT_ARG:
4137 d_count_templates_scopes (num_templates, num_scopes,
4138 dc->u.s_unary_num.sub);
4143 /* Initialize a print information structure. */
4146 d_print_init (struct d_print_info *dpi, demangle_callbackref callback,
4147 void *opaque, const struct demangle_component *dc)
4150 dpi->last_char = '\0';
4151 dpi->templates = NULL;
4152 dpi->modifiers = NULL;
4153 dpi->pack_index = 0;
4154 dpi->flush_count = 0;
4156 dpi->callback = callback;
4157 dpi->opaque = opaque;
4159 dpi->demangle_failure = 0;
4160 dpi->is_lambda_arg = 0;
4162 dpi->component_stack = NULL;
4164 dpi->saved_scopes = NULL;
4165 dpi->next_saved_scope = 0;
4166 dpi->num_saved_scopes = 0;
4168 dpi->copy_templates = NULL;
4169 dpi->next_copy_template = 0;
4170 dpi->num_copy_templates = 0;
4172 d_count_templates_scopes (&dpi->num_copy_templates,
4173 &dpi->num_saved_scopes, dc);
4174 dpi->num_copy_templates *= dpi->num_saved_scopes;
4176 dpi->current_template = NULL;
4179 /* Indicate that an error occurred during printing, and test for error. */
4182 d_print_error (struct d_print_info *dpi)
4184 dpi->demangle_failure = 1;
4188 d_print_saw_error (struct d_print_info *dpi)
4190 return dpi->demangle_failure != 0;
4193 /* Flush buffered characters to the callback. */
4196 d_print_flush (struct d_print_info *dpi)
4198 dpi->buf[dpi->len] = '\0';
4199 dpi->callback (dpi->buf, dpi->len, dpi->opaque);
4204 /* Append characters and buffers for printing. */
4207 d_append_char (struct d_print_info *dpi, char c)
4209 if (dpi->len == sizeof (dpi->buf) - 1)
4210 d_print_flush (dpi);
4212 dpi->buf[dpi->len++] = c;
4217 d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
4221 for (i = 0; i < l; i++)
4222 d_append_char (dpi, s[i]);
4226 d_append_string (struct d_print_info *dpi, const char *s)
4228 d_append_buffer (dpi, s, strlen (s));
4232 d_append_num (struct d_print_info *dpi, int l)
4235 sprintf (buf,"%d", l);
4236 d_append_string (dpi, buf);
4240 d_last_char (struct d_print_info *dpi)
4242 return dpi->last_char;
4245 /* Turn components into a human readable string. OPTIONS is the
4246 options bits passed to the demangler. DC is the tree to print.
4247 CALLBACK is a function to call to flush demangled string segments
4248 as they fill the intermediate buffer, and OPAQUE is a generalized
4249 callback argument. On success, this returns 1. On failure,
4250 it returns 0, indicating a bad parse. It does not use heap
4251 memory to build an output string, so cannot encounter memory
4252 allocation failure. */
4254 CP_STATIC_IF_GLIBCPP_V3
4256 cplus_demangle_print_callback (int options,
4257 struct demangle_component *dc,
4258 demangle_callbackref callback, void *opaque)
4260 struct d_print_info dpi;
4262 d_print_init (&dpi, callback, opaque, dc);
4265 #ifdef CP_DYNAMIC_ARRAYS
4266 /* Avoid zero-length VLAs, which are prohibited by the C99 standard
4267 and flagged as errors by Address Sanitizer. */
4268 __extension__ struct d_saved_scope scopes[(dpi.num_saved_scopes > 0)
4269 ? dpi.num_saved_scopes : 1];
4270 __extension__ struct d_print_template temps[(dpi.num_copy_templates > 0)
4271 ? dpi.num_copy_templates : 1];
4273 dpi.saved_scopes = scopes;
4274 dpi.copy_templates = temps;
4276 dpi.saved_scopes = alloca (dpi.num_saved_scopes
4277 * sizeof (*dpi.saved_scopes));
4278 dpi.copy_templates = alloca (dpi.num_copy_templates
4279 * sizeof (*dpi.copy_templates));
4282 d_print_comp (&dpi, options, dc);
4285 d_print_flush (&dpi);
4287 return ! d_print_saw_error (&dpi);
4290 /* Turn components into a human readable string. OPTIONS is the
4291 options bits passed to the demangler. DC is the tree to print.
4292 ESTIMATE is a guess at the length of the result. This returns a
4293 string allocated by malloc, or NULL on error. On success, this
4294 sets *PALC to the size of the allocated buffer. On failure, this
4295 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
4298 CP_STATIC_IF_GLIBCPP_V3
4300 cplus_demangle_print (int options, struct demangle_component *dc,
4301 int estimate, size_t *palc)
4303 struct d_growable_string dgs;
4305 d_growable_string_init (&dgs, estimate);
4307 if (! cplus_demangle_print_callback (options, dc,
4308 d_growable_string_callback_adapter,
4316 *palc = dgs.allocation_failure ? 1 : dgs.alc;
4320 /* Returns the I'th element of the template arglist ARGS, or NULL on
4321 failure. If I is negative, return the entire arglist. */
4323 static struct demangle_component *
4324 d_index_template_argument (struct demangle_component *args, int i)
4326 struct demangle_component *a;
4329 /* Print the whole argument pack. */
4336 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4342 if (i != 0 || a == NULL)
4348 /* Returns the template argument from the current context indicated by DC,
4349 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
4351 static struct demangle_component *
4352 d_lookup_template_argument (struct d_print_info *dpi,
4353 const struct demangle_component *dc)
4355 if (dpi->templates == NULL)
4357 d_print_error (dpi);
4361 return d_index_template_argument
4362 (d_right (dpi->templates->template_decl),
4363 dc->u.s_number.number);
4366 /* Returns a template argument pack used in DC (any will do), or NULL. */
4368 static struct demangle_component *
4369 d_find_pack (struct d_print_info *dpi,
4370 const struct demangle_component *dc)
4372 struct demangle_component *a;
4378 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
4379 a = d_lookup_template_argument (dpi, dc);
4380 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4384 case DEMANGLE_COMPONENT_PACK_EXPANSION:
4387 case DEMANGLE_COMPONENT_LAMBDA:
4388 case DEMANGLE_COMPONENT_NAME:
4389 case DEMANGLE_COMPONENT_TAGGED_NAME:
4390 case DEMANGLE_COMPONENT_OPERATOR:
4391 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
4392 case DEMANGLE_COMPONENT_SUB_STD:
4393 case DEMANGLE_COMPONENT_CHARACTER:
4394 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
4395 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
4396 case DEMANGLE_COMPONENT_FIXED_TYPE:
4397 case DEMANGLE_COMPONENT_DEFAULT_ARG:
4398 case DEMANGLE_COMPONENT_NUMBER:
4401 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
4402 return d_find_pack (dpi, dc->u.s_extended_operator.name);
4403 case DEMANGLE_COMPONENT_CTOR:
4404 return d_find_pack (dpi, dc->u.s_ctor.name);
4405 case DEMANGLE_COMPONENT_DTOR:
4406 return d_find_pack (dpi, dc->u.s_dtor.name);
4409 a = d_find_pack (dpi, d_left (dc));
4412 return d_find_pack (dpi, d_right (dc));
4416 /* Returns the length of the template argument pack DC. */
4419 d_pack_length (const struct demangle_component *dc)
4422 while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
4423 && d_left (dc) != NULL)
4431 /* Returns the number of template args in DC, expanding any pack expansions
4435 d_args_length (struct d_print_info *dpi, const struct demangle_component *dc)
4438 for (; dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST;
4441 struct demangle_component *elt = d_left (dc);
4444 if (elt->type == DEMANGLE_COMPONENT_PACK_EXPANSION)
4446 struct demangle_component *a = d_find_pack (dpi, d_left (elt));
4447 count += d_pack_length (a);
4455 /* DC is a component of a mangled expression. Print it, wrapped in parens
4459 d_print_subexpr (struct d_print_info *dpi, int options,
4460 struct demangle_component *dc)
4463 if (dc->type == DEMANGLE_COMPONENT_NAME
4464 || dc->type == DEMANGLE_COMPONENT_QUAL_NAME
4465 || dc->type == DEMANGLE_COMPONENT_INITIALIZER_LIST
4466 || dc->type == DEMANGLE_COMPONENT_FUNCTION_PARAM)
4469 d_append_char (dpi, '(');
4470 d_print_comp (dpi, options, dc);
4472 d_append_char (dpi, ')');
4475 /* Save the current scope. */
4478 d_save_scope (struct d_print_info *dpi,
4479 const struct demangle_component *container)
4481 struct d_saved_scope *scope;
4482 struct d_print_template *src, **link;
4484 if (dpi->next_saved_scope >= dpi->num_saved_scopes)
4486 d_print_error (dpi);
4489 scope = &dpi->saved_scopes[dpi->next_saved_scope];
4490 dpi->next_saved_scope++;
4492 scope->container = container;
4493 link = &scope->templates;
4495 for (src = dpi->templates; src != NULL; src = src->next)
4497 struct d_print_template *dst;
4499 if (dpi->next_copy_template >= dpi->num_copy_templates)
4501 d_print_error (dpi);
4504 dst = &dpi->copy_templates[dpi->next_copy_template];
4505 dpi->next_copy_template++;
4507 dst->template_decl = src->template_decl;
4515 /* Attempt to locate a previously saved scope. Returns NULL if no
4516 corresponding saved scope was found. */
4518 static struct d_saved_scope *
4519 d_get_saved_scope (struct d_print_info *dpi,
4520 const struct demangle_component *container)
4524 for (i = 0; i < dpi->next_saved_scope; i++)
4525 if (dpi->saved_scopes[i].container == container)
4526 return &dpi->saved_scopes[i];
4531 /* If DC is a C++17 fold-expression, print it and return true; otherwise
4535 d_maybe_print_fold_expression (struct d_print_info *dpi, int options,
4536 struct demangle_component *dc)
4538 struct demangle_component *ops, *operator_, *op1, *op2;
4541 const char *fold_code = d_left (dc)->u.s_operator.op->code;
4542 if (fold_code[0] != 'f')
4546 operator_ = d_left (ops);
4547 op1 = d_right (ops);
4549 if (op1->type == DEMANGLE_COMPONENT_TRINARY_ARG2)
4551 op2 = d_right (op1);
4555 /* Print the whole pack. */
4556 save_idx = dpi->pack_index;
4557 dpi->pack_index = -1;
4559 switch (fold_code[1])
4561 /* Unary left fold, (... + X). */
4563 d_append_string (dpi, "(...");
4564 d_print_expr_op (dpi, options, operator_);
4565 d_print_subexpr (dpi, options, op1);
4566 d_append_char (dpi, ')');
4569 /* Unary right fold, (X + ...). */
4571 d_append_char (dpi, '(');
4572 d_print_subexpr (dpi, options, op1);
4573 d_print_expr_op (dpi, options, operator_);
4574 d_append_string (dpi, "...)");
4577 /* Binary left fold, (42 + ... + X). */
4579 /* Binary right fold, (X + ... + 42). */
4581 d_append_char (dpi, '(');
4582 d_print_subexpr (dpi, options, op1);
4583 d_print_expr_op (dpi, options, operator_);
4584 d_append_string (dpi, "...");
4585 d_print_expr_op (dpi, options, operator_);
4586 d_print_subexpr (dpi, options, op2);
4587 d_append_char (dpi, ')');
4591 dpi->pack_index = save_idx;
4595 /* Subroutine to handle components. */
4598 d_print_comp_inner (struct d_print_info *dpi, int options,
4599 struct demangle_component *dc)
4601 /* Magic variable to let reference smashing skip over the next modifier
4602 without needing to modify *dc. */
4603 struct demangle_component *mod_inner = NULL;
4605 /* Variable used to store the current templates while a previously
4606 captured scope is used. */
4607 struct d_print_template *saved_templates;
4609 /* Nonzero if templates have been stored in the above variable. */
4610 int need_template_restore = 0;
4614 d_print_error (dpi);
4617 if (d_print_saw_error (dpi))
4622 case DEMANGLE_COMPONENT_NAME:
4623 if ((options & DMGL_JAVA) == 0)
4624 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
4626 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
4629 case DEMANGLE_COMPONENT_TAGGED_NAME:
4630 d_print_comp (dpi, options, d_left (dc));
4631 d_append_string (dpi, "[abi:");
4632 d_print_comp (dpi, options, d_right (dc));
4633 d_append_char (dpi, ']');
4636 case DEMANGLE_COMPONENT_QUAL_NAME:
4637 case DEMANGLE_COMPONENT_LOCAL_NAME:
4638 d_print_comp (dpi, options, d_left (dc));
4639 if ((options & DMGL_JAVA) == 0)
4640 d_append_string (dpi, "::");
4642 d_append_char (dpi, '.');
4644 struct demangle_component *local_name = d_right (dc);
4645 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
4647 d_append_string (dpi, "{default arg#");
4648 d_append_num (dpi, local_name->u.s_unary_num.num + 1);
4649 d_append_string (dpi, "}::");
4650 local_name = local_name->u.s_unary_num.sub;
4652 d_print_comp (dpi, options, local_name);
4656 case DEMANGLE_COMPONENT_TYPED_NAME:
4658 struct d_print_mod *hold_modifiers;
4659 struct demangle_component *typed_name;
4660 struct d_print_mod adpm[4];
4662 struct d_print_template dpt;
4664 /* Pass the name down to the type so that it can be printed in
4665 the right place for the type. We also have to pass down
4666 any CV-qualifiers, which apply to the this parameter. */
4667 hold_modifiers = dpi->modifiers;
4670 typed_name = d_left (dc);
4671 while (typed_name != NULL)
4673 if (i >= sizeof adpm / sizeof adpm[0])
4675 d_print_error (dpi);
4679 adpm[i].next = dpi->modifiers;
4680 dpi->modifiers = &adpm[i];
4681 adpm[i].mod = typed_name;
4682 adpm[i].printed = 0;
4683 adpm[i].templates = dpi->templates;
4686 if (!is_fnqual_component_type (typed_name->type))
4689 typed_name = d_left (typed_name);
4692 if (typed_name == NULL)
4694 d_print_error (dpi);
4698 /* If typed_name is a template, then it applies to the
4699 function type as well. */
4700 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
4702 dpt.next = dpi->templates;
4703 dpi->templates = &dpt;
4704 dpt.template_decl = typed_name;
4707 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
4708 there may be CV-qualifiers on its right argument which
4709 really apply here; this happens when parsing a class which
4710 is local to a function. */
4711 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
4713 struct demangle_component *local_name;
4715 local_name = d_right (typed_name);
4716 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
4717 local_name = local_name->u.s_unary_num.sub;
4718 if (local_name == NULL)
4720 d_print_error (dpi);
4723 while (is_fnqual_component_type (local_name->type))
4725 if (i >= sizeof adpm / sizeof adpm[0])
4727 d_print_error (dpi);
4731 adpm[i] = adpm[i - 1];
4732 adpm[i].next = &adpm[i - 1];
4733 dpi->modifiers = &adpm[i];
4735 adpm[i - 1].mod = local_name;
4736 adpm[i - 1].printed = 0;
4737 adpm[i - 1].templates = dpi->templates;
4740 local_name = d_left (local_name);
4744 d_print_comp (dpi, options, d_right (dc));
4746 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
4747 dpi->templates = dpt.next;
4749 /* If the modifiers didn't get printed by the type, print them
4754 if (! adpm[i].printed)
4756 d_append_char (dpi, ' ');
4757 d_print_mod (dpi, options, adpm[i].mod);
4761 dpi->modifiers = hold_modifiers;
4766 case DEMANGLE_COMPONENT_TEMPLATE:
4768 struct d_print_mod *hold_dpm;
4769 struct demangle_component *dcl;
4770 const struct demangle_component *hold_current;
4772 /* This template may need to be referenced by a cast operator
4773 contained in its subtree. */
4774 hold_current = dpi->current_template;
4775 dpi->current_template = dc;
4777 /* Don't push modifiers into a template definition. Doing so
4778 could give the wrong definition for a template argument.
4779 Instead, treat the template essentially as a name. */
4781 hold_dpm = dpi->modifiers;
4782 dpi->modifiers = NULL;
4786 if ((options & DMGL_JAVA) != 0
4787 && dcl->type == DEMANGLE_COMPONENT_NAME
4788 && dcl->u.s_name.len == 6
4789 && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
4791 /* Special-case Java arrays, so that JArray<TYPE> appears
4792 instead as TYPE[]. */
4794 d_print_comp (dpi, options, d_right (dc));
4795 d_append_string (dpi, "[]");
4799 d_print_comp (dpi, options, dcl);
4800 if (d_last_char (dpi) == '<')
4801 d_append_char (dpi, ' ');
4802 d_append_char (dpi, '<');
4803 d_print_comp (dpi, options, d_right (dc));
4804 /* Avoid generating two consecutive '>' characters, to avoid
4805 the C++ syntactic ambiguity. */
4806 if (d_last_char (dpi) == '>')
4807 d_append_char (dpi, ' ');
4808 d_append_char (dpi, '>');
4811 dpi->modifiers = hold_dpm;
4812 dpi->current_template = hold_current;
4817 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
4818 if (dpi->is_lambda_arg)
4820 /* Show the template parm index, as that's how g++ displays
4821 these, and future proofs us against potential
4822 '[]<typename T> (T *a, T *b) {...}'. */
4823 d_append_buffer (dpi, "auto:", 5);
4824 d_append_num (dpi, dc->u.s_number.number + 1);
4828 struct d_print_template *hold_dpt;
4829 struct demangle_component *a = d_lookup_template_argument (dpi, dc);
4831 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4832 a = d_index_template_argument (a, dpi->pack_index);
4836 d_print_error (dpi);
4840 /* While processing this parameter, we need to pop the list
4841 of templates. This is because the template parameter may
4842 itself be a reference to a parameter of an outer
4845 hold_dpt = dpi->templates;
4846 dpi->templates = hold_dpt->next;
4848 d_print_comp (dpi, options, a);
4850 dpi->templates = hold_dpt;
4854 case DEMANGLE_COMPONENT_CTOR:
4855 d_print_comp (dpi, options, dc->u.s_ctor.name);
4858 case DEMANGLE_COMPONENT_DTOR:
4859 d_append_char (dpi, '~');
4860 d_print_comp (dpi, options, dc->u.s_dtor.name);
4863 case DEMANGLE_COMPONENT_VTABLE:
4864 d_append_string (dpi, "vtable for ");
4865 d_print_comp (dpi, options, d_left (dc));
4868 case DEMANGLE_COMPONENT_VTT:
4869 d_append_string (dpi, "VTT for ");
4870 d_print_comp (dpi, options, d_left (dc));
4873 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
4874 d_append_string (dpi, "construction vtable for ");
4875 d_print_comp (dpi, options, d_left (dc));
4876 d_append_string (dpi, "-in-");
4877 d_print_comp (dpi, options, d_right (dc));
4880 case DEMANGLE_COMPONENT_TYPEINFO:
4881 d_append_string (dpi, "typeinfo for ");
4882 d_print_comp (dpi, options, d_left (dc));
4885 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
4886 d_append_string (dpi, "typeinfo name for ");
4887 d_print_comp (dpi, options, d_left (dc));
4890 case DEMANGLE_COMPONENT_TYPEINFO_FN:
4891 d_append_string (dpi, "typeinfo fn for ");
4892 d_print_comp (dpi, options, d_left (dc));
4895 case DEMANGLE_COMPONENT_THUNK:
4896 d_append_string (dpi, "non-virtual thunk to ");
4897 d_print_comp (dpi, options, d_left (dc));
4900 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
4901 d_append_string (dpi, "virtual thunk to ");
4902 d_print_comp (dpi, options, d_left (dc));
4905 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
4906 d_append_string (dpi, "covariant return thunk to ");
4907 d_print_comp (dpi, options, d_left (dc));
4910 case DEMANGLE_COMPONENT_JAVA_CLASS:
4911 d_append_string (dpi, "java Class for ");
4912 d_print_comp (dpi, options, d_left (dc));
4915 case DEMANGLE_COMPONENT_GUARD:
4916 d_append_string (dpi, "guard variable for ");
4917 d_print_comp (dpi, options, d_left (dc));
4920 case DEMANGLE_COMPONENT_TLS_INIT:
4921 d_append_string (dpi, "TLS init function for ");
4922 d_print_comp (dpi, options, d_left (dc));
4925 case DEMANGLE_COMPONENT_TLS_WRAPPER:
4926 d_append_string (dpi, "TLS wrapper function for ");
4927 d_print_comp (dpi, options, d_left (dc));
4930 case DEMANGLE_COMPONENT_REFTEMP:
4931 d_append_string (dpi, "reference temporary #");
4932 d_print_comp (dpi, options, d_right (dc));
4933 d_append_string (dpi, " for ");
4934 d_print_comp (dpi, options, d_left (dc));
4937 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
4938 d_append_string (dpi, "hidden alias for ");
4939 d_print_comp (dpi, options, d_left (dc));
4942 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
4943 d_append_string (dpi, "transaction clone for ");
4944 d_print_comp (dpi, options, d_left (dc));
4947 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
4948 d_append_string (dpi, "non-transaction clone for ");
4949 d_print_comp (dpi, options, d_left (dc));
4952 case DEMANGLE_COMPONENT_SUB_STD:
4953 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
4956 case DEMANGLE_COMPONENT_RESTRICT:
4957 case DEMANGLE_COMPONENT_VOLATILE:
4958 case DEMANGLE_COMPONENT_CONST:
4960 struct d_print_mod *pdpm;
4962 /* When printing arrays, it's possible to have cases where the
4963 same CV-qualifier gets pushed on the stack multiple times.
4964 We only need to print it once. */
4966 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
4968 if (! pdpm->printed)
4970 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
4971 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
4972 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
4974 if (pdpm->mod->type == dc->type)
4976 d_print_comp (dpi, options, d_left (dc));
4984 case DEMANGLE_COMPONENT_REFERENCE:
4985 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4987 /* Handle reference smashing: & + && = &. */
4988 struct demangle_component *sub = d_left (dc);
4989 if (!dpi->is_lambda_arg
4990 && sub->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
4992 struct d_saved_scope *scope = d_get_saved_scope (dpi, sub);
4993 struct demangle_component *a;
4997 /* This is the first time SUB has been traversed.
4998 We need to capture the current templates so
4999 they can be restored if SUB is reentered as a
5001 d_save_scope (dpi, sub);
5002 if (d_print_saw_error (dpi))
5007 const struct d_component_stack *dcse;
5008 int found_self_or_parent = 0;
5010 /* This traversal is reentering SUB as a substition.
5011 If we are not beneath SUB or DC in the tree then we
5012 need to restore SUB's template stack temporarily. */
5013 for (dcse = dpi->component_stack; dcse != NULL;
5014 dcse = dcse->parent)
5018 && dcse != dpi->component_stack))
5020 found_self_or_parent = 1;
5025 if (!found_self_or_parent)
5027 saved_templates = dpi->templates;
5028 dpi->templates = scope->templates;
5029 need_template_restore = 1;
5033 a = d_lookup_template_argument (dpi, sub);
5034 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
5035 a = d_index_template_argument (a, dpi->pack_index);
5039 if (need_template_restore)
5040 dpi->templates = saved_templates;
5042 d_print_error (dpi);
5049 if (sub->type == DEMANGLE_COMPONENT_REFERENCE
5050 || sub->type == dc->type)
5052 else if (sub->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE)
5053 mod_inner = d_left (sub);
5057 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5058 case DEMANGLE_COMPONENT_POINTER:
5059 case DEMANGLE_COMPONENT_COMPLEX:
5060 case DEMANGLE_COMPONENT_IMAGINARY:
5061 FNQUAL_COMPONENT_CASE:
5064 /* We keep a list of modifiers on the stack. */
5065 struct d_print_mod dpm;
5067 dpm.next = dpi->modifiers;
5068 dpi->modifiers = &dpm;
5071 dpm.templates = dpi->templates;
5074 mod_inner = d_left (dc);
5076 d_print_comp (dpi, options, mod_inner);
5078 /* If the modifier didn't get printed by the type, print it
5081 d_print_mod (dpi, options, dc);
5083 dpi->modifiers = dpm.next;
5085 if (need_template_restore)
5086 dpi->templates = saved_templates;
5091 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
5092 if ((options & DMGL_JAVA) == 0)
5093 d_append_buffer (dpi, dc->u.s_builtin.type->name,
5094 dc->u.s_builtin.type->len);
5096 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
5097 dc->u.s_builtin.type->java_len);
5100 case DEMANGLE_COMPONENT_VENDOR_TYPE:
5101 d_print_comp (dpi, options, d_left (dc));
5104 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
5106 if ((options & DMGL_RET_POSTFIX) != 0)
5107 d_print_function_type (dpi,
5108 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
5109 dc, dpi->modifiers);
5111 /* Print return type if present */
5112 if (d_left (dc) != NULL && (options & DMGL_RET_POSTFIX) != 0)
5113 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
5115 else if (d_left (dc) != NULL && (options & DMGL_RET_DROP) == 0)
5117 struct d_print_mod dpm;
5119 /* We must pass this type down as a modifier in order to
5120 print it in the right location. */
5121 dpm.next = dpi->modifiers;
5122 dpi->modifiers = &dpm;
5125 dpm.templates = dpi->templates;
5127 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
5130 dpi->modifiers = dpm.next;
5135 /* In standard prefix notation, there is a space between the
5136 return type and the function signature. */
5137 if ((options & DMGL_RET_POSTFIX) == 0)
5138 d_append_char (dpi, ' ');
5141 if ((options & DMGL_RET_POSTFIX) == 0)
5142 d_print_function_type (dpi,
5143 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
5144 dc, dpi->modifiers);
5149 case DEMANGLE_COMPONENT_ARRAY_TYPE:
5151 struct d_print_mod *hold_modifiers;
5152 struct d_print_mod adpm[4];
5154 struct d_print_mod *pdpm;
5156 /* We must pass this type down as a modifier in order to print
5157 multi-dimensional arrays correctly. If the array itself is
5158 CV-qualified, we act as though the element type were
5159 CV-qualified. We do this by copying the modifiers down
5160 rather than fiddling pointers, so that we don't wind up
5161 with a d_print_mod higher on the stack pointing into our
5162 stack frame after we return. */
5164 hold_modifiers = dpi->modifiers;
5166 adpm[0].next = hold_modifiers;
5167 dpi->modifiers = &adpm[0];
5169 adpm[0].printed = 0;
5170 adpm[0].templates = dpi->templates;
5173 pdpm = hold_modifiers;
5175 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
5176 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
5177 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
5179 if (! pdpm->printed)
5181 if (i >= sizeof adpm / sizeof adpm[0])
5183 d_print_error (dpi);
5188 adpm[i].next = dpi->modifiers;
5189 dpi->modifiers = &adpm[i];
5197 d_print_comp (dpi, options, d_right (dc));
5199 dpi->modifiers = hold_modifiers;
5201 if (adpm[0].printed)
5207 d_print_mod (dpi, options, adpm[i].mod);
5210 d_print_array_type (dpi, options, dc, dpi->modifiers);
5215 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5216 case DEMANGLE_COMPONENT_VECTOR_TYPE:
5218 struct d_print_mod dpm;
5220 dpm.next = dpi->modifiers;
5221 dpi->modifiers = &dpm;
5224 dpm.templates = dpi->templates;
5226 d_print_comp (dpi, options, d_right (dc));
5228 /* If the modifier didn't get printed by the type, print it
5231 d_print_mod (dpi, options, dc);
5233 dpi->modifiers = dpm.next;
5238 case DEMANGLE_COMPONENT_FIXED_TYPE:
5239 if (dc->u.s_fixed.sat)
5240 d_append_string (dpi, "_Sat ");
5241 /* Don't print "int _Accum". */
5242 if (dc->u.s_fixed.length->u.s_builtin.type
5243 != &cplus_demangle_builtin_types['i'-'a'])
5245 d_print_comp (dpi, options, dc->u.s_fixed.length);
5246 d_append_char (dpi, ' ');
5248 if (dc->u.s_fixed.accum)
5249 d_append_string (dpi, "_Accum");
5251 d_append_string (dpi, "_Fract");
5254 case DEMANGLE_COMPONENT_ARGLIST:
5255 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
5256 if (d_left (dc) != NULL)
5257 d_print_comp (dpi, options, d_left (dc));
5258 if (d_right (dc) != NULL)
5261 unsigned long int flush_count;
5262 /* Make sure ", " isn't flushed by d_append_string, otherwise
5263 dpi->len -= 2 wouldn't work. */
5264 if (dpi->len >= sizeof (dpi->buf) - 2)
5265 d_print_flush (dpi);
5266 d_append_string (dpi, ", ");
5268 flush_count = dpi->flush_count;
5269 d_print_comp (dpi, options, d_right (dc));
5270 /* If that didn't print anything (which can happen with empty
5271 template argument packs), remove the comma and space. */
5272 if (dpi->flush_count == flush_count && dpi->len == len)
5277 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
5279 struct demangle_component *type = d_left (dc);
5280 struct demangle_component *list = d_right (dc);
5283 d_print_comp (dpi, options, type);
5284 d_append_char (dpi, '{');
5285 d_print_comp (dpi, options, list);
5286 d_append_char (dpi, '}');
5290 case DEMANGLE_COMPONENT_OPERATOR:
5292 const struct demangle_operator_info *op = dc->u.s_operator.op;
5295 d_append_string (dpi, "operator");
5296 /* Add a space before new/delete. */
5297 if (IS_LOWER (op->name[0]))
5298 d_append_char (dpi, ' ');
5299 /* Omit a trailing space. */
5300 if (op->name[len-1] == ' ')
5302 d_append_buffer (dpi, op->name, len);
5306 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
5307 d_append_string (dpi, "operator ");
5308 d_print_comp (dpi, options, dc->u.s_extended_operator.name);
5311 case DEMANGLE_COMPONENT_CONVERSION:
5312 d_append_string (dpi, "operator ");
5313 d_print_conversion (dpi, options, dc);
5316 case DEMANGLE_COMPONENT_NULLARY:
5317 d_print_expr_op (dpi, options, d_left (dc));
5320 case DEMANGLE_COMPONENT_UNARY:
5322 struct demangle_component *op = d_left (dc);
5323 struct demangle_component *operand = d_right (dc);
5324 const char *code = NULL;
5326 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
5328 code = op->u.s_operator.op->code;
5329 if (!strcmp (code, "ad"))
5331 /* Don't print the argument list for the address of a
5333 if (operand->type == DEMANGLE_COMPONENT_TYPED_NAME
5334 && d_left (operand)->type == DEMANGLE_COMPONENT_QUAL_NAME
5335 && d_right (operand)->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
5336 operand = d_left (operand);
5338 if (operand->type == DEMANGLE_COMPONENT_BINARY_ARGS)
5340 /* This indicates a suffix operator. */
5341 operand = d_left (operand);
5342 d_print_subexpr (dpi, options, operand);
5343 d_print_expr_op (dpi, options, op);
5348 /* For sizeof..., just print the pack length. */
5349 if (code && !strcmp (code, "sZ"))
5351 struct demangle_component *a = d_find_pack (dpi, operand);
5352 int len = d_pack_length (a);
5353 d_append_num (dpi, len);
5356 else if (code && !strcmp (code, "sP"))
5358 int len = d_args_length (dpi, operand);
5359 d_append_num (dpi, len);
5363 if (op->type != DEMANGLE_COMPONENT_CAST)
5364 d_print_expr_op (dpi, options, op);
5367 d_append_char (dpi, '(');
5368 d_print_cast (dpi, options, op);
5369 d_append_char (dpi, ')');
5371 if (code && !strcmp (code, "gs"))
5372 /* Avoid parens after '::'. */
5373 d_print_comp (dpi, options, operand);
5374 else if (code && !strcmp (code, "st"))
5375 /* Always print parens for sizeof (type). */
5377 d_append_char (dpi, '(');
5378 d_print_comp (dpi, options, operand);
5379 d_append_char (dpi, ')');
5382 d_print_subexpr (dpi, options, operand);
5386 case DEMANGLE_COMPONENT_BINARY:
5387 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
5389 d_print_error (dpi);
5393 if (op_is_new_cast (d_left (dc)))
5395 d_print_expr_op (dpi, options, d_left (dc));
5396 d_append_char (dpi, '<');
5397 d_print_comp (dpi, options, d_left (d_right (dc)));
5398 d_append_string (dpi, ">(");
5399 d_print_comp (dpi, options, d_right (d_right (dc)));
5400 d_append_char (dpi, ')');
5404 if (d_maybe_print_fold_expression (dpi, options, dc))
5407 /* We wrap an expression which uses the greater-than operator in
5408 an extra layer of parens so that it does not get confused
5409 with the '>' which ends the template parameters. */
5410 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
5411 && d_left (dc)->u.s_operator.op->len == 1
5412 && d_left (dc)->u.s_operator.op->name[0] == '>')
5413 d_append_char (dpi, '(');
5415 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") == 0
5416 && d_left (d_right (dc))->type == DEMANGLE_COMPONENT_TYPED_NAME)
5418 /* Function call used in an expression should not have printed types
5419 of the function arguments. Values of the function arguments still
5420 get printed below. */
5422 const struct demangle_component *func = d_left (d_right (dc));
5424 if (d_right (func)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
5425 d_print_error (dpi);
5426 d_print_subexpr (dpi, options, d_left (func));
5429 d_print_subexpr (dpi, options, d_left (d_right (dc)));
5430 if (strcmp (d_left (dc)->u.s_operator.op->code, "ix") == 0)
5432 d_append_char (dpi, '[');
5433 d_print_comp (dpi, options, d_right (d_right (dc)));
5434 d_append_char (dpi, ']');
5438 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") != 0)
5439 d_print_expr_op (dpi, options, d_left (dc));
5440 d_print_subexpr (dpi, options, d_right (d_right (dc)));
5443 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
5444 && d_left (dc)->u.s_operator.op->len == 1
5445 && d_left (dc)->u.s_operator.op->name[0] == '>')
5446 d_append_char (dpi, ')');
5450 case DEMANGLE_COMPONENT_BINARY_ARGS:
5451 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
5452 d_print_error (dpi);
5455 case DEMANGLE_COMPONENT_TRINARY:
5456 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
5457 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
5459 d_print_error (dpi);
5462 if (d_maybe_print_fold_expression (dpi, options, dc))
5465 struct demangle_component *op = d_left (dc);
5466 struct demangle_component *first = d_left (d_right (dc));
5467 struct demangle_component *second = d_left (d_right (d_right (dc)));
5468 struct demangle_component *third = d_right (d_right (d_right (dc)));
5470 if (!strcmp (op->u.s_operator.op->code, "qu"))
5472 d_print_subexpr (dpi, options, first);
5473 d_print_expr_op (dpi, options, op);
5474 d_print_subexpr (dpi, options, second);
5475 d_append_string (dpi, " : ");
5476 d_print_subexpr (dpi, options, third);
5480 d_append_string (dpi, "new ");
5481 if (d_left (first) != NULL)
5483 d_print_subexpr (dpi, options, first);
5484 d_append_char (dpi, ' ');
5486 d_print_comp (dpi, options, second);
5488 d_print_subexpr (dpi, options, third);
5493 case DEMANGLE_COMPONENT_TRINARY_ARG1:
5494 case DEMANGLE_COMPONENT_TRINARY_ARG2:
5495 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
5496 d_print_error (dpi);
5499 case DEMANGLE_COMPONENT_LITERAL:
5500 case DEMANGLE_COMPONENT_LITERAL_NEG:
5502 enum d_builtin_type_print tp;
5504 /* For some builtin types, produce simpler output. */
5505 tp = D_PRINT_DEFAULT;
5506 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
5508 tp = d_left (dc)->u.s_builtin.type->print;
5512 case D_PRINT_UNSIGNED:
5514 case D_PRINT_UNSIGNED_LONG:
5515 case D_PRINT_LONG_LONG:
5516 case D_PRINT_UNSIGNED_LONG_LONG:
5517 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
5519 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
5520 d_append_char (dpi, '-');
5521 d_print_comp (dpi, options, d_right (dc));
5526 case D_PRINT_UNSIGNED:
5527 d_append_char (dpi, 'u');
5530 d_append_char (dpi, 'l');
5532 case D_PRINT_UNSIGNED_LONG:
5533 d_append_string (dpi, "ul");
5535 case D_PRINT_LONG_LONG:
5536 d_append_string (dpi, "ll");
5538 case D_PRINT_UNSIGNED_LONG_LONG:
5539 d_append_string (dpi, "ull");
5547 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
5548 && d_right (dc)->u.s_name.len == 1
5549 && dc->type == DEMANGLE_COMPONENT_LITERAL)
5551 switch (d_right (dc)->u.s_name.s[0])
5554 d_append_string (dpi, "false");
5557 d_append_string (dpi, "true");
5570 d_append_char (dpi, '(');
5571 d_print_comp (dpi, options, d_left (dc));
5572 d_append_char (dpi, ')');
5573 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
5574 d_append_char (dpi, '-');
5575 if (tp == D_PRINT_FLOAT)
5576 d_append_char (dpi, '[');
5577 d_print_comp (dpi, options, d_right (dc));
5578 if (tp == D_PRINT_FLOAT)
5579 d_append_char (dpi, ']');
5583 case DEMANGLE_COMPONENT_NUMBER:
5584 d_append_num (dpi, dc->u.s_number.number);
5587 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
5588 d_append_string (dpi, "java resource ");
5589 d_print_comp (dpi, options, d_left (dc));
5592 case DEMANGLE_COMPONENT_COMPOUND_NAME:
5593 d_print_comp (dpi, options, d_left (dc));
5594 d_print_comp (dpi, options, d_right (dc));
5597 case DEMANGLE_COMPONENT_CHARACTER:
5598 d_append_char (dpi, dc->u.s_character.character);
5601 case DEMANGLE_COMPONENT_DECLTYPE:
5602 d_append_string (dpi, "decltype (");
5603 d_print_comp (dpi, options, d_left (dc));
5604 d_append_char (dpi, ')');
5607 case DEMANGLE_COMPONENT_PACK_EXPANSION:
5611 struct demangle_component *a = d_find_pack (dpi, d_left (dc));
5614 /* d_find_pack won't find anything if the only packs involved
5615 in this expansion are function parameter packs; in that
5616 case, just print the pattern and "...". */
5617 d_print_subexpr (dpi, options, d_left (dc));
5618 d_append_string (dpi, "...");
5622 len = d_pack_length (a);
5624 for (i = 0; i < len; ++i)
5626 dpi->pack_index = i;
5627 d_print_comp (dpi, options, dc);
5629 d_append_string (dpi, ", ");
5634 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
5636 long num = dc->u.s_number.number;
5638 d_append_string (dpi, "this");
5641 d_append_string (dpi, "{parm#");
5642 d_append_num (dpi, num);
5643 d_append_char (dpi, '}');
5648 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
5649 d_append_string (dpi, "global constructors keyed to ");
5650 d_print_comp (dpi, options, dc->u.s_binary.left);
5653 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
5654 d_append_string (dpi, "global destructors keyed to ");
5655 d_print_comp (dpi, options, dc->u.s_binary.left);
5658 case DEMANGLE_COMPONENT_LAMBDA:
5659 d_append_string (dpi, "{lambda(");
5660 /* Generic lambda auto parms are mangled as the template type
5662 dpi->is_lambda_arg++;
5663 d_print_comp (dpi, options, dc->u.s_unary_num.sub);
5664 dpi->is_lambda_arg--;
5665 d_append_string (dpi, ")#");
5666 d_append_num (dpi, dc->u.s_unary_num.num + 1);
5667 d_append_char (dpi, '}');
5670 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
5671 d_append_string (dpi, "{unnamed type#");
5672 d_append_num (dpi, dc->u.s_number.number + 1);
5673 d_append_char (dpi, '}');
5676 case DEMANGLE_COMPONENT_CLONE:
5677 d_print_comp (dpi, options, d_left (dc));
5678 d_append_string (dpi, " [clone ");
5679 d_print_comp (dpi, options, d_right (dc));
5680 d_append_char (dpi, ']');
5684 d_print_error (dpi);
5690 d_print_comp (struct d_print_info *dpi, int options,
5691 struct demangle_component *dc)
5693 struct d_component_stack self;
5694 if (dc == NULL || dc->d_printing > 1)
5696 d_print_error (dpi);
5703 self.parent = dpi->component_stack;
5704 dpi->component_stack = &self;
5706 d_print_comp_inner (dpi, options, dc);
5708 dpi->component_stack = self.parent;
5712 /* Print a Java dentifier. For Java we try to handle encoded extended
5713 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
5714 so we don't it for C++. Characters are encoded as
5718 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
5724 for (p = name; p < end; ++p)
5735 for (q = p + 3; q < end; ++q)
5741 else if (*q >= 'A' && *q <= 'F')
5742 dig = *q - 'A' + 10;
5743 else if (*q >= 'a' && *q <= 'f')
5744 dig = *q - 'a' + 10;
5750 /* If the Unicode character is larger than 256, we don't try
5751 to deal with it here. FIXME. */
5752 if (q < end && *q == '_' && c < 256)
5754 d_append_char (dpi, c);
5760 d_append_char (dpi, *p);
5764 /* Print a list of modifiers. SUFFIX is 1 if we are printing
5765 qualifiers on this after printing a function. */
5768 d_print_mod_list (struct d_print_info *dpi, int options,
5769 struct d_print_mod *mods, int suffix)
5771 struct d_print_template *hold_dpt;
5773 if (mods == NULL || d_print_saw_error (dpi))
5778 && (is_fnqual_component_type (mods->mod->type))))
5780 d_print_mod_list (dpi, options, mods->next, suffix);
5786 hold_dpt = dpi->templates;
5787 dpi->templates = mods->templates;
5789 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
5791 d_print_function_type (dpi, options, mods->mod, mods->next);
5792 dpi->templates = hold_dpt;
5795 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
5797 d_print_array_type (dpi, options, mods->mod, mods->next);
5798 dpi->templates = hold_dpt;
5801 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
5803 struct d_print_mod *hold_modifiers;
5804 struct demangle_component *dc;
5806 /* When this is on the modifier stack, we have pulled any
5807 qualifiers off the right argument already. Otherwise, we
5808 print it as usual, but don't let the left argument see any
5811 hold_modifiers = dpi->modifiers;
5812 dpi->modifiers = NULL;
5813 d_print_comp (dpi, options, d_left (mods->mod));
5814 dpi->modifiers = hold_modifiers;
5816 if ((options & DMGL_JAVA) == 0)
5817 d_append_string (dpi, "::");
5819 d_append_char (dpi, '.');
5821 dc = d_right (mods->mod);
5823 if (dc->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
5825 d_append_string (dpi, "{default arg#");
5826 d_append_num (dpi, dc->u.s_unary_num.num + 1);
5827 d_append_string (dpi, "}::");
5828 dc = dc->u.s_unary_num.sub;
5831 while (is_fnqual_component_type (dc->type))
5834 d_print_comp (dpi, options, dc);
5836 dpi->templates = hold_dpt;
5840 d_print_mod (dpi, options, mods->mod);
5842 dpi->templates = hold_dpt;
5844 d_print_mod_list (dpi, options, mods->next, suffix);
5847 /* Print a modifier. */
5850 d_print_mod (struct d_print_info *dpi, int options,
5851 struct demangle_component *mod)
5855 case DEMANGLE_COMPONENT_RESTRICT:
5856 case DEMANGLE_COMPONENT_RESTRICT_THIS:
5857 d_append_string (dpi, " restrict");
5859 case DEMANGLE_COMPONENT_VOLATILE:
5860 case DEMANGLE_COMPONENT_VOLATILE_THIS:
5861 d_append_string (dpi, " volatile");
5863 case DEMANGLE_COMPONENT_CONST:
5864 case DEMANGLE_COMPONENT_CONST_THIS:
5865 d_append_string (dpi, " const");
5867 case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
5868 d_append_string (dpi, " transaction_safe");
5870 case DEMANGLE_COMPONENT_NOEXCEPT:
5871 d_append_string (dpi, " noexcept");
5874 d_append_char (dpi, '(');
5875 d_print_comp (dpi, options, d_right (mod));
5876 d_append_char (dpi, ')');
5879 case DEMANGLE_COMPONENT_THROW_SPEC:
5880 d_append_string (dpi, " throw");
5883 d_append_char (dpi, '(');
5884 d_print_comp (dpi, options, d_right (mod));
5885 d_append_char (dpi, ')');
5888 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5889 d_append_char (dpi, ' ');
5890 d_print_comp (dpi, options, d_right (mod));
5892 case DEMANGLE_COMPONENT_POINTER:
5893 /* There is no pointer symbol in Java. */
5894 if ((options & DMGL_JAVA) == 0)
5895 d_append_char (dpi, '*');
5897 case DEMANGLE_COMPONENT_REFERENCE_THIS:
5898 /* For the ref-qualifier, put a space before the &. */
5899 d_append_char (dpi, ' ');
5901 case DEMANGLE_COMPONENT_REFERENCE:
5902 d_append_char (dpi, '&');
5904 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
5905 d_append_char (dpi, ' ');
5907 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
5908 d_append_string (dpi, "&&");
5910 case DEMANGLE_COMPONENT_COMPLEX:
5911 d_append_string (dpi, "complex ");
5913 case DEMANGLE_COMPONENT_IMAGINARY:
5914 d_append_string (dpi, "imaginary ");
5916 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5917 if (d_last_char (dpi) != '(')
5918 d_append_char (dpi, ' ');
5919 d_print_comp (dpi, options, d_left (mod));
5920 d_append_string (dpi, "::*");
5922 case DEMANGLE_COMPONENT_TYPED_NAME:
5923 d_print_comp (dpi, options, d_left (mod));
5925 case DEMANGLE_COMPONENT_VECTOR_TYPE:
5926 d_append_string (dpi, " __vector(");
5927 d_print_comp (dpi, options, d_left (mod));
5928 d_append_char (dpi, ')');
5932 /* Otherwise, we have something that won't go back on the
5933 modifier stack, so we can just print it. */
5934 d_print_comp (dpi, options, mod);
5939 /* Print a function type, except for the return type. */
5942 d_print_function_type (struct d_print_info *dpi, int options,
5943 struct demangle_component *dc,
5944 struct d_print_mod *mods)
5948 struct d_print_mod *p;
5949 struct d_print_mod *hold_modifiers;
5953 for (p = mods; p != NULL; p = p->next)
5958 switch (p->mod->type)
5960 case DEMANGLE_COMPONENT_POINTER:
5961 case DEMANGLE_COMPONENT_REFERENCE:
5962 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
5965 case DEMANGLE_COMPONENT_RESTRICT:
5966 case DEMANGLE_COMPONENT_VOLATILE:
5967 case DEMANGLE_COMPONENT_CONST:
5968 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5969 case DEMANGLE_COMPONENT_COMPLEX:
5970 case DEMANGLE_COMPONENT_IMAGINARY:
5971 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5975 FNQUAL_COMPONENT_CASE:
5988 if (d_last_char (dpi) != '('
5989 && d_last_char (dpi) != '*')
5992 if (need_space && d_last_char (dpi) != ' ')
5993 d_append_char (dpi, ' ');
5994 d_append_char (dpi, '(');
5997 hold_modifiers = dpi->modifiers;
5998 dpi->modifiers = NULL;
6000 d_print_mod_list (dpi, options, mods, 0);
6003 d_append_char (dpi, ')');
6005 d_append_char (dpi, '(');
6007 if (d_right (dc) != NULL)
6008 d_print_comp (dpi, options, d_right (dc));
6010 d_append_char (dpi, ')');
6012 d_print_mod_list (dpi, options, mods, 1);
6014 dpi->modifiers = hold_modifiers;
6017 /* Print an array type, except for the element type. */
6020 d_print_array_type (struct d_print_info *dpi, int options,
6021 struct demangle_component *dc,
6022 struct d_print_mod *mods)
6030 struct d_print_mod *p;
6033 for (p = mods; p != NULL; p = p->next)
6037 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
6052 d_append_string (dpi, " (");
6054 d_print_mod_list (dpi, options, mods, 0);
6057 d_append_char (dpi, ')');
6061 d_append_char (dpi, ' ');
6063 d_append_char (dpi, '[');
6065 if (d_left (dc) != NULL)
6066 d_print_comp (dpi, options, d_left (dc));
6068 d_append_char (dpi, ']');
6071 /* Print an operator in an expression. */
6074 d_print_expr_op (struct d_print_info *dpi, int options,
6075 struct demangle_component *dc)
6077 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
6078 d_append_buffer (dpi, dc->u.s_operator.op->name,
6079 dc->u.s_operator.op->len);
6081 d_print_comp (dpi, options, dc);
6087 d_print_cast (struct d_print_info *dpi, int options,
6088 struct demangle_component *dc)
6090 d_print_comp (dpi, options, d_left (dc));
6093 /* Print a conversion operator. */
6096 d_print_conversion (struct d_print_info *dpi, int options,
6097 struct demangle_component *dc)
6099 struct d_print_template dpt;
6101 /* For a conversion operator, we need the template parameters from
6102 the enclosing template in scope for processing the type. */
6103 if (dpi->current_template != NULL)
6105 dpt.next = dpi->templates;
6106 dpi->templates = &dpt;
6107 dpt.template_decl = dpi->current_template;
6110 if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
6112 d_print_comp (dpi, options, d_left (dc));
6113 if (dpi->current_template != NULL)
6114 dpi->templates = dpt.next;
6118 d_print_comp (dpi, options, d_left (d_left (dc)));
6120 /* For a templated cast operator, we need to remove the template
6121 parameters from scope after printing the operator name,
6122 so we need to handle the template printing here. */
6123 if (dpi->current_template != NULL)
6124 dpi->templates = dpt.next;
6126 if (d_last_char (dpi) == '<')
6127 d_append_char (dpi, ' ');
6128 d_append_char (dpi, '<');
6129 d_print_comp (dpi, options, d_right (d_left (dc)));
6130 /* Avoid generating two consecutive '>' characters, to avoid
6131 the C++ syntactic ambiguity. */
6132 if (d_last_char (dpi) == '>')
6133 d_append_char (dpi, ' ');
6134 d_append_char (dpi, '>');
6138 /* Initialize the information structure we use to pass around
6141 CP_STATIC_IF_GLIBCPP_V3
6143 cplus_demangle_init_info (const char *mangled, int options, size_t len,
6147 di->send = mangled + len;
6148 di->options = options;
6152 /* We can not need more components than twice the number of chars in
6153 the mangled string. Most components correspond directly to
6154 chars, but the ARGLIST types are exceptions. */
6155 di->num_comps = 2 * len;
6158 /* Similarly, we can not need more substitutions than there are
6159 chars in the mangled string. */
6164 di->last_name = NULL;
6167 di->is_expression = 0;
6168 di->is_conversion = 0;
6171 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
6172 mangled name, return strings in repeated callback giving the demangled
6173 name. OPTIONS is the usual libiberty demangler options. On success,
6174 this returns 1. On failure, returns 0. */
6177 d_demangle_callback (const char *mangled, int options,
6178 demangle_callbackref callback, void *opaque)
6189 struct demangle_component *dc;
6192 if (mangled[0] == '_' && mangled[1] == 'Z')
6194 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
6195 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
6196 && (mangled[9] == 'D' || mangled[9] == 'I')
6197 && mangled[10] == '_')
6198 type = mangled[9] == 'I' ? DCT_GLOBAL_CTORS : DCT_GLOBAL_DTORS;
6201 if ((options & DMGL_TYPES) == 0)
6206 cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
6209 #ifdef CP_DYNAMIC_ARRAYS
6210 __extension__ struct demangle_component comps[di.num_comps];
6211 __extension__ struct demangle_component *subs[di.num_subs];
6216 di.comps = alloca (di.num_comps * sizeof (*di.comps));
6217 di.subs = alloca (di.num_subs * sizeof (*di.subs));
6223 dc = cplus_demangle_type (&di);
6226 dc = cplus_demangle_mangled_name (&di, 1);
6228 case DCT_GLOBAL_CTORS:
6229 case DCT_GLOBAL_DTORS:
6230 d_advance (&di, 11);
6231 dc = d_make_comp (&di,
6232 (type == DCT_GLOBAL_CTORS
6233 ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
6234 : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS),
6235 d_make_demangle_mangled_name (&di, d_str (&di)),
6237 d_advance (&di, strlen (d_str (&di)));
6240 abort (); /* We have listed all the cases. */
6243 /* If DMGL_PARAMS is set, then if we didn't consume the entire
6244 mangled string, then we didn't successfully demangle it. If
6245 DMGL_PARAMS is not set, we didn't look at the trailing
6247 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
6250 #ifdef CP_DEMANGLE_DEBUG
6254 status = (dc != NULL)
6255 ? cplus_demangle_print_callback (options, dc, callback, opaque)
6262 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
6263 name, return a buffer allocated with malloc holding the demangled
6264 name. OPTIONS is the usual libiberty demangler options. On
6265 success, this sets *PALC to the allocated size of the returned
6266 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
6267 a memory allocation failure, and returns NULL. */
6270 d_demangle (const char *mangled, int options, size_t *palc)
6272 struct d_growable_string dgs;
6275 d_growable_string_init (&dgs, 0);
6277 status = d_demangle_callback (mangled, options,
6278 d_growable_string_callback_adapter, &dgs);
6286 *palc = dgs.allocation_failure ? 1 : dgs.alc;
6290 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
6292 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
6294 /* ia64 ABI-mandated entry point in the C++ runtime library for
6295 performing demangling. MANGLED_NAME is a NUL-terminated character
6296 string containing the name to be demangled.
6298 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
6299 *LENGTH bytes, into which the demangled name is stored. If
6300 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
6301 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
6302 is placed in a region of memory allocated with malloc.
6304 If LENGTH is non-NULL, the length of the buffer containing the
6305 demangled name, is placed in *LENGTH.
6307 The return value is a pointer to the start of the NUL-terminated
6308 demangled name, or NULL if the demangling fails. The caller is
6309 responsible for deallocating this memory using free.
6311 *STATUS is set to one of the following values:
6312 0: The demangling operation succeeded.
6313 -1: A memory allocation failure occurred.
6314 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
6315 -3: One of the arguments is invalid.
6317 The demangling is performed using the C++ ABI mangling rules, with
6321 __cxa_demangle (const char *mangled_name, char *output_buffer,
6322 size_t *length, int *status)
6327 if (mangled_name == NULL)
6334 if (output_buffer != NULL && length == NULL)
6341 demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
6343 if (demangled == NULL)
6355 if (output_buffer == NULL)
6362 if (strlen (demangled) < *length)
6364 strcpy (output_buffer, demangled);
6366 demangled = output_buffer;
6370 free (output_buffer);
6381 extern int __gcclibcxx_demangle_callback (const char *,
6383 (const char *, size_t, void *),
6386 /* Alternative, allocationless entry point in the C++ runtime library
6387 for performing demangling. MANGLED_NAME is a NUL-terminated character
6388 string containing the name to be demangled.
6390 CALLBACK is a callback function, called with demangled string
6391 segments as demangling progresses; it is called at least once,
6392 but may be called more than once. OPAQUE is a generalized pointer
6393 used as a callback argument.
6395 The return code is one of the following values, equivalent to
6396 the STATUS values of __cxa_demangle() (excluding -1, since this
6397 function performs no memory allocations):
6398 0: The demangling operation succeeded.
6399 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
6400 -3: One of the arguments is invalid.
6402 The demangling is performed using the C++ ABI mangling rules, with
6406 __gcclibcxx_demangle_callback (const char *mangled_name,
6407 void (*callback) (const char *, size_t, void *),
6412 if (mangled_name == NULL || callback == NULL)
6415 status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES,
6423 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
6425 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
6426 mangled name, return a buffer allocated with malloc holding the
6427 demangled name. Otherwise, return NULL. */
6430 cplus_demangle_v3 (const char *mangled, int options)
6434 return d_demangle (mangled, options, &alc);
6438 cplus_demangle_v3_callback (const char *mangled, int options,
6439 demangle_callbackref callback, void *opaque)
6441 return d_demangle_callback (mangled, options, callback, opaque);
6444 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
6445 conventions, but the output formatting is a little different.
6446 This instructs the C++ demangler not to emit pointer characters ("*"), to
6447 use Java's namespace separator symbol ("." instead of "::"), and to output
6448 JArray<TYPE> as TYPE[]. */
6451 java_demangle_v3 (const char *mangled)
6455 return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
6459 java_demangle_v3_callback (const char *mangled,
6460 demangle_callbackref callback, void *opaque)
6462 return d_demangle_callback (mangled,
6463 DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
6467 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
6469 #ifndef IN_GLIBCPP_V3
6471 /* Demangle a string in order to find out whether it is a constructor
6472 or destructor. Return non-zero on success. Set *CTOR_KIND and
6473 *DTOR_KIND appropriately. */
6476 is_ctor_or_dtor (const char *mangled,
6477 enum gnu_v3_ctor_kinds *ctor_kind,
6478 enum gnu_v3_dtor_kinds *dtor_kind)
6481 struct demangle_component *dc;
6484 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
6485 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
6487 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
6490 #ifdef CP_DYNAMIC_ARRAYS
6491 __extension__ struct demangle_component comps[di.num_comps];
6492 __extension__ struct demangle_component *subs[di.num_subs];
6497 di.comps = alloca (di.num_comps * sizeof (*di.comps));
6498 di.subs = alloca (di.num_subs * sizeof (*di.subs));
6501 dc = cplus_demangle_mangled_name (&di, 1);
6503 /* Note that because we did not pass DMGL_PARAMS, we don't expect
6504 to demangle the entire string. */
6511 /* These cannot appear on a constructor or destructor. */
6512 case DEMANGLE_COMPONENT_RESTRICT_THIS:
6513 case DEMANGLE_COMPONENT_VOLATILE_THIS:
6514 case DEMANGLE_COMPONENT_CONST_THIS:
6515 case DEMANGLE_COMPONENT_REFERENCE_THIS:
6516 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
6520 case DEMANGLE_COMPONENT_TYPED_NAME:
6521 case DEMANGLE_COMPONENT_TEMPLATE:
6524 case DEMANGLE_COMPONENT_QUAL_NAME:
6525 case DEMANGLE_COMPONENT_LOCAL_NAME:
6528 case DEMANGLE_COMPONENT_CTOR:
6529 *ctor_kind = dc->u.s_ctor.kind;
6533 case DEMANGLE_COMPONENT_DTOR:
6534 *dtor_kind = dc->u.s_dtor.kind;
6545 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
6546 name. A non-zero return indicates the type of constructor. */
6548 enum gnu_v3_ctor_kinds
6549 is_gnu_v3_mangled_ctor (const char *name)
6551 enum gnu_v3_ctor_kinds ctor_kind;
6552 enum gnu_v3_dtor_kinds dtor_kind;
6554 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
6555 return (enum gnu_v3_ctor_kinds) 0;
6560 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
6561 name. A non-zero return indicates the type of destructor. */
6563 enum gnu_v3_dtor_kinds
6564 is_gnu_v3_mangled_dtor (const char *name)
6566 enum gnu_v3_ctor_kinds ctor_kind;
6567 enum gnu_v3_dtor_kinds dtor_kind;
6569 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
6570 return (enum gnu_v3_dtor_kinds) 0;
6574 #endif /* IN_GLIBCPP_V3 */
6576 #ifdef STANDALONE_DEMANGLER
6579 #include "dyn-string.h"
6581 static void print_usage (FILE* fp, int exit_value);
6583 #define IS_ALPHA(CHAR) \
6584 (((CHAR) >= 'a' && (CHAR) <= 'z') \
6585 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
6587 /* Non-zero if CHAR is a character than can occur in a mangled name. */
6588 #define is_mangled_char(CHAR) \
6589 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
6590 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
6592 /* The name of this program, as invoked. */
6593 const char* program_name;
6595 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
6598 print_usage (FILE* fp, int exit_value)
6600 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
6601 fprintf (fp, "Options:\n");
6602 fprintf (fp, " -h,--help Display this message.\n");
6603 fprintf (fp, " -p,--no-params Don't display function parameters\n");
6604 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
6605 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
6610 /* Option specification for getopt_long. */
6611 static const struct option long_options[] =
6613 { "help", no_argument, NULL, 'h' },
6614 { "no-params", no_argument, NULL, 'p' },
6615 { "verbose", no_argument, NULL, 'v' },
6616 { NULL, no_argument, NULL, 0 },
6619 /* Main entry for a demangling filter executable. It will demangle
6620 its command line arguments, if any. If none are provided, it will
6621 filter stdin to stdout, replacing any recognized mangled C++ names
6622 with their demangled equivalents. */
6625 main (int argc, char *argv[])
6629 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
6631 /* Use the program name of this program, as invoked. */
6632 program_name = argv[0];
6634 /* Parse options. */
6637 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
6640 case '?': /* Unrecognized option. */
6641 print_usage (stderr, 1);
6645 print_usage (stdout, 0);
6649 options &= ~ DMGL_PARAMS;
6653 options |= DMGL_VERBOSE;
6657 while (opt_char != -1);
6660 /* No command line arguments were provided. Filter stdin. */
6662 dyn_string_t mangled = dyn_string_new (3);
6665 /* Read all of input. */
6666 while (!feof (stdin))
6670 /* Pile characters into mangled until we hit one that can't
6671 occur in a mangled name. */
6673 while (!feof (stdin) && is_mangled_char (c))
6675 dyn_string_append_char (mangled, c);
6681 if (dyn_string_length (mangled) > 0)
6683 #ifdef IN_GLIBCPP_V3
6684 s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
6686 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
6696 /* It might not have been a mangled name. Print the
6698 fputs (dyn_string_buf (mangled), stdout);
6701 dyn_string_clear (mangled);
6704 /* If we haven't hit EOF yet, we've read one character that
6705 can't occur in a mangled name, so print it out. */
6710 dyn_string_delete (mangled);
6713 /* Demangle command line arguments. */
6715 /* Loop over command line arguments. */
6716 for (i = optind; i < argc; ++i)
6719 #ifdef IN_GLIBCPP_V3
6723 /* Attempt to demangle. */
6724 #ifdef IN_GLIBCPP_V3
6725 s = __cxa_demangle (argv[i], NULL, NULL, &status);
6727 s = cplus_demangle_v3 (argv[i], options);
6730 /* If it worked, print the demangled name. */
6738 #ifdef IN_GLIBCPP_V3
6739 fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
6741 fprintf (stderr, "Failed: %s\n", argv[i]);
6750 #endif /* STANDALONE_DEMANGLER */