1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor <ian@wasabisystems.com>.
6 This file is part of the libiberty library, which is part of GCC.
8 This file is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 In addition to the permissions in the GNU General Public License, the
14 Free Software Foundation gives you unlimited permission to link the
15 compiled version of this file into combinations with other programs,
16 and to distribute those combinations without any restriction coming
17 from the use of this file. (The General Public License restrictions
18 do apply in other respects; for example, they cover modification of
19 the file, and distribution when not linked into a combined
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
32 /* This code implements a demangler for the g++ V3 ABI. The ABI is
33 described on this web page:
34 http://www.codesourcery.com/cxx-abi/abi.html#mangling
36 This code was written while looking at the demangler written by
37 Alex Samuel <samuel@codesourcery.com>.
39 This code first pulls the mangled name apart into a list of
40 components, and then walks the list generating the demangled
43 This file will normally define the following functions, q.v.:
44 char *cplus_demangle_v3(const char *mangled, int options)
45 char *java_demangle_v3(const char *mangled)
46 int cplus_demangle_v3_callback(const char *mangled, int options,
47 demangle_callbackref callback)
48 int java_demangle_v3_callback(const char *mangled,
49 demangle_callbackref callback)
50 enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
51 enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
53 Also, the interface to the component list is public, and defined in
54 demangle.h. The interface consists of these types, which are
55 defined in demangle.h:
56 enum demangle_component_type
57 struct demangle_component
59 and these functions defined in this file:
60 cplus_demangle_fill_name
61 cplus_demangle_fill_extended_operator
62 cplus_demangle_fill_ctor
63 cplus_demangle_fill_dtor
65 cplus_demangle_print_callback
66 and other functions defined in the file cp-demint.c.
68 This file also defines some other functions and variables which are
69 only to be used by the file cp-demint.c.
71 Preprocessor macros you can define while compiling this file:
74 If defined, this file defines the following functions, q.v.:
75 char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
77 int __gcclibcxx_demangle_callback (const char *,
79 (const char *, size_t, void *),
81 instead of cplus_demangle_v3[_callback]() and
82 java_demangle_v3[_callback]().
85 If defined, this file defines only __cxa_demangle() and
86 __gcclibcxx_demangle_callback(), and no other publically visible
87 functions or variables.
90 If defined, this file defines a main() function which demangles
91 any arguments, or, if none, demangles stdin.
94 If defined, turns on debugging mode, which prints information on
95 stdout about the mangled string. This is not generally useful.
98 #if defined (_AIX) && !defined (__GNUC__)
120 # define alloca __builtin_alloca
122 extern char *alloca ();
123 # endif /* __GNUC__ */
125 #endif /* HAVE_ALLOCA_H */
127 #include "ansidecl.h"
128 #include "libiberty.h"
129 #include "demangle.h"
130 #include "cp-demangle.h"
133 /* If IN_GLIBCPP_V3 is defined, some functions are made static. We
134 also rename them via #define to avoid compiler errors when the
135 static definition conflicts with the extern declaration in a header
139 #define CP_STATIC_IF_GLIBCPP_V3 static
141 #define cplus_demangle_fill_name d_fill_name
142 static int d_fill_name (struct demangle_component *, const char *, int);
144 #define cplus_demangle_fill_extended_operator d_fill_extended_operator
146 d_fill_extended_operator (struct demangle_component *, int,
147 struct demangle_component *);
149 #define cplus_demangle_fill_ctor d_fill_ctor
151 d_fill_ctor (struct demangle_component *, enum gnu_v3_ctor_kinds,
152 struct demangle_component *);
154 #define cplus_demangle_fill_dtor d_fill_dtor
156 d_fill_dtor (struct demangle_component *, enum gnu_v3_dtor_kinds,
157 struct demangle_component *);
159 #define cplus_demangle_mangled_name d_mangled_name
160 static struct demangle_component *d_mangled_name (struct d_info *, int);
162 #define cplus_demangle_type d_type
163 static struct demangle_component *d_type (struct d_info *);
165 #define cplus_demangle_print d_print
166 static char *d_print (int, const struct demangle_component *, int, size_t *);
168 #define cplus_demangle_print_callback d_print_callback
169 static int d_print_callback (int, const struct demangle_component *,
170 demangle_callbackref, void *);
172 #define cplus_demangle_init_info d_init_info
173 static void d_init_info (const char *, int, size_t, struct d_info *);
175 #else /* ! defined(IN_GLIBCPP_V3) */
176 #define CP_STATIC_IF_GLIBCPP_V3
177 #endif /* ! defined(IN_GLIBCPP_V3) */
179 /* See if the compiler supports dynamic arrays. */
182 #define CP_DYNAMIC_ARRAYS
185 #ifdef __STDC_VERSION__
186 #if __STDC_VERSION__ >= 199901L
187 #define CP_DYNAMIC_ARRAYS
188 #endif /* __STDC__VERSION >= 199901L */
189 #endif /* defined (__STDC_VERSION__) */
190 #endif /* defined (__STDC__) */
191 #endif /* ! defined (__GNUC__) */
193 /* We avoid pulling in the ctype tables, to prevent pulling in
194 additional unresolved symbols when this code is used in a library.
195 FIXME: Is this really a valid reason? This comes from the original
198 As of this writing this file has the following undefined references
199 when compiled with -DIN_GLIBCPP_V3: realloc, free, memcpy, strcpy,
202 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
203 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
204 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
206 /* The prefix prepended by GCC to an identifier represnting the
207 anonymous namespace. */
208 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
209 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
210 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
212 /* Information we keep for the standard substitutions. */
214 struct d_standard_sub_info
216 /* The code for this substitution. */
218 /* The simple string it expands to. */
219 const char *simple_expansion;
220 /* The length of the simple expansion. */
222 /* The results of a full, verbose, expansion. This is used when
223 qualifying a constructor/destructor, or when in verbose mode. */
224 const char *full_expansion;
225 /* The length of the full expansion. */
227 /* What to set the last_name field of d_info to; NULL if we should
228 not set it. This is only relevant when qualifying a
229 constructor/destructor. */
230 const char *set_last_name;
231 /* The length of set_last_name. */
232 int set_last_name_len;
235 /* Accessors for subtrees of struct demangle_component. */
237 #define d_left(dc) ((dc)->u.s_binary.left)
238 #define d_right(dc) ((dc)->u.s_binary.right)
240 /* A list of templates. This is used while printing. */
242 struct d_print_template
244 /* Next template on the list. */
245 struct d_print_template *next;
247 const struct demangle_component *template_decl;
250 /* A list of type modifiers. This is used while printing. */
254 /* Next modifier on the list. These are in the reverse of the order
255 in which they appeared in the mangled string. */
256 struct d_print_mod *next;
258 const struct demangle_component *mod;
259 /* Whether this modifier was printed. */
261 /* The list of templates which applies to this modifier. */
262 struct d_print_template *templates;
265 /* We use these structures to hold information during printing. */
267 struct d_growable_string
269 /* Buffer holding the result. */
271 /* Current length of data in buffer. */
273 /* Allocated size of buffer. */
275 /* Set to 1 if we had a memory allocation failure. */
276 int allocation_failure;
279 enum { D_PRINT_BUFFER_LENGTH = 256 };
282 /* Fixed-length allocated buffer for demangled data, flushed to the
283 callback with a NUL termination once full. */
284 char buf[D_PRINT_BUFFER_LENGTH];
285 /* Current length of data in buffer. */
287 /* The last character printed, saved individually so that it survives
290 /* Callback function to handle demangled buffer flush. */
291 demangle_callbackref callback;
292 /* Opaque callback argument. */
294 /* The current list of templates, if any. */
295 struct d_print_template *templates;
296 /* The current list of modifiers (e.g., pointer, reference, etc.),
298 struct d_print_mod *modifiers;
299 /* Set to 1 if we saw a demangling error. */
300 int demangle_failure;
301 /* The current index into any template argument packs we are using
304 /* Number of d_print_flush calls so far. */
305 unsigned long int flush_count;
306 /* Table mapping demangle components to scopes saved when first
307 traversing those components. These are used while evaluating
312 #ifdef CP_DEMANGLE_DEBUG
313 static void d_dump (struct demangle_component *, int);
316 static struct demangle_component *
317 d_make_empty (struct d_info *);
319 static struct demangle_component *
320 d_make_comp (struct d_info *, enum demangle_component_type,
321 struct demangle_component *,
322 struct demangle_component *);
324 static struct demangle_component *
325 d_make_name (struct d_info *, const char *, int);
327 static struct demangle_component *
328 d_make_demangle_mangled_name (struct d_info *, const char *);
330 static struct demangle_component *
331 d_make_builtin_type (struct d_info *,
332 const struct demangle_builtin_type_info *);
334 static struct demangle_component *
335 d_make_operator (struct d_info *,
336 const struct demangle_operator_info *);
338 static struct demangle_component *
339 d_make_extended_operator (struct d_info *, int,
340 struct demangle_component *);
342 static struct demangle_component *
343 d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
344 struct demangle_component *);
346 static struct demangle_component *
347 d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
348 struct demangle_component *);
350 static struct demangle_component *
351 d_make_template_param (struct d_info *, long);
353 static struct demangle_component *
354 d_make_sub (struct d_info *, const char *, int);
357 has_return_type (struct demangle_component *);
360 is_ctor_dtor_or_conversion (struct demangle_component *);
362 static struct demangle_component *d_encoding (struct d_info *, int);
364 static struct demangle_component *d_name (struct d_info *);
366 static struct demangle_component *d_nested_name (struct d_info *);
368 static struct demangle_component *d_prefix (struct d_info *);
370 static struct demangle_component *d_unqualified_name (struct d_info *);
372 static struct demangle_component *d_source_name (struct d_info *);
374 static long d_number (struct d_info *);
376 static struct demangle_component *d_identifier (struct d_info *, int);
378 static struct demangle_component *d_operator_name (struct d_info *);
380 static struct demangle_component *d_special_name (struct d_info *);
382 static int d_call_offset (struct d_info *, int);
384 static struct demangle_component *d_ctor_dtor_name (struct d_info *);
386 static struct demangle_component **
387 d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
389 static struct demangle_component *
390 d_ref_qualifier (struct d_info *, struct demangle_component *);
392 static struct demangle_component *
393 d_function_type (struct d_info *);
395 static struct demangle_component *
396 d_bare_function_type (struct d_info *, int);
398 static struct demangle_component *
399 d_class_enum_type (struct d_info *);
401 static struct demangle_component *d_array_type (struct d_info *);
403 static struct demangle_component *d_vector_type (struct d_info *);
405 static struct demangle_component *
406 d_pointer_to_member_type (struct d_info *);
408 static struct demangle_component *
409 d_template_param (struct d_info *);
411 static struct demangle_component *d_template_args (struct d_info *);
413 static struct demangle_component *
414 d_template_arg (struct d_info *);
416 static struct demangle_component *d_expression (struct d_info *);
418 static struct demangle_component *d_expr_primary (struct d_info *);
420 static struct demangle_component *d_local_name (struct d_info *);
422 static int d_discriminator (struct d_info *);
424 static struct demangle_component *d_lambda (struct d_info *);
426 static struct demangle_component *d_unnamed_type (struct d_info *);
428 static struct demangle_component *
429 d_clone_suffix (struct d_info *, struct demangle_component *);
432 d_add_substitution (struct d_info *, struct demangle_component *);
434 static struct demangle_component *d_substitution (struct d_info *, int);
436 static void d_growable_string_init (struct d_growable_string *, size_t);
439 d_growable_string_resize (struct d_growable_string *, size_t);
442 d_growable_string_append_buffer (struct d_growable_string *,
443 const char *, size_t);
445 d_growable_string_callback_adapter (const char *, size_t, void *);
448 d_print_init (struct d_print_info *, demangle_callbackref, void *);
450 static inline void d_print_error (struct d_print_info *);
452 static inline int d_print_saw_error (struct d_print_info *);
454 static inline void d_print_flush (struct d_print_info *);
456 static inline void d_append_char (struct d_print_info *, char);
458 static inline void d_append_buffer (struct d_print_info *,
459 const char *, size_t);
461 static inline void d_append_string (struct d_print_info *, const char *);
463 static inline char d_last_char (struct d_print_info *);
466 d_print_comp (struct d_print_info *, int, const struct demangle_component *);
469 d_print_java_identifier (struct d_print_info *, const char *, int);
472 d_print_mod_list (struct d_print_info *, int, struct d_print_mod *, int);
475 d_print_mod (struct d_print_info *, int, const struct demangle_component *);
478 d_print_function_type (struct d_print_info *, int,
479 const struct demangle_component *,
480 struct d_print_mod *);
483 d_print_array_type (struct d_print_info *, int,
484 const struct demangle_component *,
485 struct d_print_mod *);
488 d_print_expr_op (struct d_print_info *, int, const struct demangle_component *);
491 d_print_cast (struct d_print_info *, int, const struct demangle_component *);
493 static int d_demangle_callback (const char *, int,
494 demangle_callbackref, void *);
495 static char *d_demangle (const char *, int, size_t *);
497 #ifdef CP_DEMANGLE_DEBUG
500 d_dump (struct demangle_component *dc, int indent)
507 printf ("failed demangling\n");
511 for (i = 0; i < indent; ++i)
516 case DEMANGLE_COMPONENT_NAME:
517 printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
519 case DEMANGLE_COMPONENT_TAGGED_NAME:
520 printf ("tagged name\n");
521 d_dump (dc->u.s_binary.left, indent + 2);
522 d_dump (dc->u.s_binary.right, indent + 2);
524 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
525 printf ("template parameter %ld\n", dc->u.s_number.number);
527 case DEMANGLE_COMPONENT_CTOR:
528 printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
529 d_dump (dc->u.s_ctor.name, indent + 2);
531 case DEMANGLE_COMPONENT_DTOR:
532 printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
533 d_dump (dc->u.s_dtor.name, indent + 2);
535 case DEMANGLE_COMPONENT_SUB_STD:
536 printf ("standard substitution %s\n", dc->u.s_string.string);
538 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
539 printf ("builtin type %s\n", dc->u.s_builtin.type->name);
541 case DEMANGLE_COMPONENT_OPERATOR:
542 printf ("operator %s\n", dc->u.s_operator.op->name);
544 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
545 printf ("extended operator with %d args\n",
546 dc->u.s_extended_operator.args);
547 d_dump (dc->u.s_extended_operator.name, indent + 2);
550 case DEMANGLE_COMPONENT_QUAL_NAME:
551 printf ("qualified name\n");
553 case DEMANGLE_COMPONENT_LOCAL_NAME:
554 printf ("local name\n");
556 case DEMANGLE_COMPONENT_TYPED_NAME:
557 printf ("typed name\n");
559 case DEMANGLE_COMPONENT_TEMPLATE:
560 printf ("template\n");
562 case DEMANGLE_COMPONENT_VTABLE:
565 case DEMANGLE_COMPONENT_VTT:
568 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
569 printf ("construction vtable\n");
571 case DEMANGLE_COMPONENT_TYPEINFO:
572 printf ("typeinfo\n");
574 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
575 printf ("typeinfo name\n");
577 case DEMANGLE_COMPONENT_TYPEINFO_FN:
578 printf ("typeinfo function\n");
580 case DEMANGLE_COMPONENT_THUNK:
583 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
584 printf ("virtual thunk\n");
586 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
587 printf ("covariant thunk\n");
589 case DEMANGLE_COMPONENT_JAVA_CLASS:
590 printf ("java class\n");
592 case DEMANGLE_COMPONENT_GUARD:
595 case DEMANGLE_COMPONENT_REFTEMP:
596 printf ("reference temporary\n");
598 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
599 printf ("hidden alias\n");
601 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
602 printf ("transaction clone\n");
604 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
605 printf ("non-transaction clone\n");
607 case DEMANGLE_COMPONENT_RESTRICT:
608 printf ("restrict\n");
610 case DEMANGLE_COMPONENT_VOLATILE:
611 printf ("volatile\n");
613 case DEMANGLE_COMPONENT_CONST:
616 case DEMANGLE_COMPONENT_RESTRICT_THIS:
617 printf ("restrict this\n");
619 case DEMANGLE_COMPONENT_VOLATILE_THIS:
620 printf ("volatile this\n");
622 case DEMANGLE_COMPONENT_CONST_THIS:
623 printf ("const this\n");
625 case DEMANGLE_COMPONENT_REFERENCE_THIS:
626 printf ("reference this\n");
628 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
629 printf ("rvalue reference this\n");
631 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
632 printf ("vendor type qualifier\n");
634 case DEMANGLE_COMPONENT_POINTER:
635 printf ("pointer\n");
637 case DEMANGLE_COMPONENT_REFERENCE:
638 printf ("reference\n");
640 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
641 printf ("rvalue reference\n");
643 case DEMANGLE_COMPONENT_COMPLEX:
644 printf ("complex\n");
646 case DEMANGLE_COMPONENT_IMAGINARY:
647 printf ("imaginary\n");
649 case DEMANGLE_COMPONENT_VENDOR_TYPE:
650 printf ("vendor type\n");
652 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
653 printf ("function type\n");
655 case DEMANGLE_COMPONENT_ARRAY_TYPE:
656 printf ("array type\n");
658 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
659 printf ("pointer to member type\n");
661 case DEMANGLE_COMPONENT_FIXED_TYPE:
662 printf ("fixed-point type\n");
664 case DEMANGLE_COMPONENT_ARGLIST:
665 printf ("argument list\n");
667 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
668 printf ("template argument list\n");
670 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
671 printf ("initializer list\n");
673 case DEMANGLE_COMPONENT_CAST:
676 case DEMANGLE_COMPONENT_NULLARY:
677 printf ("nullary operator\n");
679 case DEMANGLE_COMPONENT_UNARY:
680 printf ("unary operator\n");
682 case DEMANGLE_COMPONENT_BINARY:
683 printf ("binary operator\n");
685 case DEMANGLE_COMPONENT_BINARY_ARGS:
686 printf ("binary operator arguments\n");
688 case DEMANGLE_COMPONENT_TRINARY:
689 printf ("trinary operator\n");
691 case DEMANGLE_COMPONENT_TRINARY_ARG1:
692 printf ("trinary operator arguments 1\n");
694 case DEMANGLE_COMPONENT_TRINARY_ARG2:
695 printf ("trinary operator arguments 1\n");
697 case DEMANGLE_COMPONENT_LITERAL:
698 printf ("literal\n");
700 case DEMANGLE_COMPONENT_LITERAL_NEG:
701 printf ("negative literal\n");
703 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
704 printf ("java resource\n");
706 case DEMANGLE_COMPONENT_COMPOUND_NAME:
707 printf ("compound name\n");
709 case DEMANGLE_COMPONENT_CHARACTER:
710 printf ("character '%c'\n", dc->u.s_character.character);
712 case DEMANGLE_COMPONENT_DECLTYPE:
713 printf ("decltype\n");
715 case DEMANGLE_COMPONENT_PACK_EXPANSION:
716 printf ("pack expansion\n");
718 case DEMANGLE_COMPONENT_TLS_INIT:
719 printf ("tls init function\n");
721 case DEMANGLE_COMPONENT_TLS_WRAPPER:
722 printf ("tls wrapper function\n");
724 case DEMANGLE_COMPONENT_DEFAULT_ARG:
725 printf ("default argument %d\n", dc->u.s_unary_num.num);
726 d_dump (dc->u.s_unary_num.sub, indent+2);
728 case DEMANGLE_COMPONENT_LAMBDA:
729 printf ("lambda %d\n", dc->u.s_unary_num.num);
730 d_dump (dc->u.s_unary_num.sub, indent+2);
734 d_dump (d_left (dc), indent + 2);
735 d_dump (d_right (dc), indent + 2);
738 #endif /* CP_DEMANGLE_DEBUG */
740 /* Fill in a DEMANGLE_COMPONENT_NAME. */
742 CP_STATIC_IF_GLIBCPP_V3
744 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
746 if (p == NULL || s == NULL || len == 0)
748 p->type = DEMANGLE_COMPONENT_NAME;
750 p->u.s_name.len = len;
754 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
756 CP_STATIC_IF_GLIBCPP_V3
758 cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
759 struct demangle_component *name)
761 if (p == NULL || args < 0 || name == NULL)
763 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
764 p->u.s_extended_operator.args = args;
765 p->u.s_extended_operator.name = name;
769 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
771 CP_STATIC_IF_GLIBCPP_V3
773 cplus_demangle_fill_ctor (struct demangle_component *p,
774 enum gnu_v3_ctor_kinds kind,
775 struct demangle_component *name)
779 || (int) kind < gnu_v3_complete_object_ctor
780 || (int) kind > gnu_v3_object_ctor_group)
782 p->type = DEMANGLE_COMPONENT_CTOR;
783 p->u.s_ctor.kind = kind;
784 p->u.s_ctor.name = name;
788 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
790 CP_STATIC_IF_GLIBCPP_V3
792 cplus_demangle_fill_dtor (struct demangle_component *p,
793 enum gnu_v3_dtor_kinds kind,
794 struct demangle_component *name)
798 || (int) kind < gnu_v3_deleting_dtor
799 || (int) kind > gnu_v3_object_dtor_group)
801 p->type = DEMANGLE_COMPONENT_DTOR;
802 p->u.s_dtor.kind = kind;
803 p->u.s_dtor.name = name;
807 /* Add a new component. */
809 static struct demangle_component *
810 d_make_empty (struct d_info *di)
812 struct demangle_component *p;
814 if (di->next_comp >= di->num_comps)
816 p = &di->comps[di->next_comp];
821 /* Add a new generic component. */
823 static struct demangle_component *
824 d_make_comp (struct d_info *di, enum demangle_component_type type,
825 struct demangle_component *left,
826 struct demangle_component *right)
828 struct demangle_component *p;
830 /* We check for errors here. A typical error would be a NULL return
831 from a subroutine. We catch those here, and return NULL
835 /* These types require two parameters. */
836 case DEMANGLE_COMPONENT_QUAL_NAME:
837 case DEMANGLE_COMPONENT_LOCAL_NAME:
838 case DEMANGLE_COMPONENT_TYPED_NAME:
839 case DEMANGLE_COMPONENT_TAGGED_NAME:
840 case DEMANGLE_COMPONENT_TEMPLATE:
841 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
842 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
843 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
844 case DEMANGLE_COMPONENT_UNARY:
845 case DEMANGLE_COMPONENT_BINARY:
846 case DEMANGLE_COMPONENT_BINARY_ARGS:
847 case DEMANGLE_COMPONENT_TRINARY:
848 case DEMANGLE_COMPONENT_TRINARY_ARG1:
849 case DEMANGLE_COMPONENT_LITERAL:
850 case DEMANGLE_COMPONENT_LITERAL_NEG:
851 case DEMANGLE_COMPONENT_COMPOUND_NAME:
852 case DEMANGLE_COMPONENT_VECTOR_TYPE:
853 case DEMANGLE_COMPONENT_CLONE:
854 if (left == NULL || right == NULL)
858 /* These types only require one parameter. */
859 case DEMANGLE_COMPONENT_VTABLE:
860 case DEMANGLE_COMPONENT_VTT:
861 case DEMANGLE_COMPONENT_TYPEINFO:
862 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
863 case DEMANGLE_COMPONENT_TYPEINFO_FN:
864 case DEMANGLE_COMPONENT_THUNK:
865 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
866 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
867 case DEMANGLE_COMPONENT_JAVA_CLASS:
868 case DEMANGLE_COMPONENT_GUARD:
869 case DEMANGLE_COMPONENT_TLS_INIT:
870 case DEMANGLE_COMPONENT_TLS_WRAPPER:
871 case DEMANGLE_COMPONENT_REFTEMP:
872 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
873 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
874 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
875 case DEMANGLE_COMPONENT_POINTER:
876 case DEMANGLE_COMPONENT_REFERENCE:
877 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
878 case DEMANGLE_COMPONENT_COMPLEX:
879 case DEMANGLE_COMPONENT_IMAGINARY:
880 case DEMANGLE_COMPONENT_VENDOR_TYPE:
881 case DEMANGLE_COMPONENT_CAST:
882 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
883 case DEMANGLE_COMPONENT_DECLTYPE:
884 case DEMANGLE_COMPONENT_PACK_EXPANSION:
885 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
886 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
887 case DEMANGLE_COMPONENT_NULLARY:
888 case DEMANGLE_COMPONENT_TRINARY_ARG2:
893 /* This needs a right parameter, but the left parameter can be
895 case DEMANGLE_COMPONENT_ARRAY_TYPE:
896 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
901 /* These are allowed to have no parameters--in some cases they
902 will be filled in later. */
903 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
904 case DEMANGLE_COMPONENT_RESTRICT:
905 case DEMANGLE_COMPONENT_VOLATILE:
906 case DEMANGLE_COMPONENT_CONST:
907 case DEMANGLE_COMPONENT_RESTRICT_THIS:
908 case DEMANGLE_COMPONENT_VOLATILE_THIS:
909 case DEMANGLE_COMPONENT_CONST_THIS:
910 case DEMANGLE_COMPONENT_REFERENCE_THIS:
911 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
912 case DEMANGLE_COMPONENT_ARGLIST:
913 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
916 /* Other types should not be seen here. */
921 p = d_make_empty (di);
925 p->u.s_binary.left = left;
926 p->u.s_binary.right = right;
931 /* Add a new demangle mangled name component. */
933 static struct demangle_component *
934 d_make_demangle_mangled_name (struct d_info *di, const char *s)
936 if (d_peek_char (di) != '_' || d_peek_next_char (di) != 'Z')
937 return d_make_name (di, s, strlen (s));
939 return d_encoding (di, 0);
942 /* Add a new name component. */
944 static struct demangle_component *
945 d_make_name (struct d_info *di, const char *s, int len)
947 struct demangle_component *p;
949 p = d_make_empty (di);
950 if (! cplus_demangle_fill_name (p, s, len))
955 /* Add a new builtin type component. */
957 static struct demangle_component *
958 d_make_builtin_type (struct d_info *di,
959 const struct demangle_builtin_type_info *type)
961 struct demangle_component *p;
965 p = d_make_empty (di);
968 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
969 p->u.s_builtin.type = type;
974 /* Add a new operator component. */
976 static struct demangle_component *
977 d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
979 struct demangle_component *p;
981 p = d_make_empty (di);
984 p->type = DEMANGLE_COMPONENT_OPERATOR;
985 p->u.s_operator.op = op;
990 /* Add a new extended operator component. */
992 static struct demangle_component *
993 d_make_extended_operator (struct d_info *di, int args,
994 struct demangle_component *name)
996 struct demangle_component *p;
998 p = d_make_empty (di);
999 if (! cplus_demangle_fill_extended_operator (p, args, name))
1004 static struct demangle_component *
1005 d_make_default_arg (struct d_info *di, int num,
1006 struct demangle_component *sub)
1008 struct demangle_component *p = d_make_empty (di);
1011 p->type = DEMANGLE_COMPONENT_DEFAULT_ARG;
1012 p->u.s_unary_num.num = num;
1013 p->u.s_unary_num.sub = sub;
1018 /* Add a new constructor component. */
1020 static struct demangle_component *
1021 d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
1022 struct demangle_component *name)
1024 struct demangle_component *p;
1026 p = d_make_empty (di);
1027 if (! cplus_demangle_fill_ctor (p, kind, name))
1032 /* Add a new destructor component. */
1034 static struct demangle_component *
1035 d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
1036 struct demangle_component *name)
1038 struct demangle_component *p;
1040 p = d_make_empty (di);
1041 if (! cplus_demangle_fill_dtor (p, kind, name))
1046 /* Add a new template parameter. */
1048 static struct demangle_component *
1049 d_make_template_param (struct d_info *di, long i)
1051 struct demangle_component *p;
1053 p = d_make_empty (di);
1056 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
1057 p->u.s_number.number = i;
1062 /* Add a new function parameter. */
1064 static struct demangle_component *
1065 d_make_function_param (struct d_info *di, long i)
1067 struct demangle_component *p;
1069 p = d_make_empty (di);
1072 p->type = DEMANGLE_COMPONENT_FUNCTION_PARAM;
1073 p->u.s_number.number = i;
1078 /* Add a new standard substitution component. */
1080 static struct demangle_component *
1081 d_make_sub (struct d_info *di, const char *name, int len)
1083 struct demangle_component *p;
1085 p = d_make_empty (di);
1088 p->type = DEMANGLE_COMPONENT_SUB_STD;
1089 p->u.s_string.string = name;
1090 p->u.s_string.len = len;
1095 /* <mangled-name> ::= _Z <encoding> [<clone-suffix>]*
1097 TOP_LEVEL is non-zero when called at the top level. */
1099 CP_STATIC_IF_GLIBCPP_V3
1100 struct demangle_component *
1101 cplus_demangle_mangled_name (struct d_info *di, int top_level)
1103 struct demangle_component *p;
1105 if (! d_check_char (di, '_')
1106 /* Allow missing _ if not at toplevel to work around a
1107 bug in G++ abi-version=2 mangling; see the comment in
1108 write_template_arg. */
1111 if (! d_check_char (di, 'Z'))
1113 p = d_encoding (di, top_level);
1115 /* If at top level and parsing parameters, check for a clone
1117 if (top_level && (di->options & DMGL_PARAMS) != 0)
1118 while (d_peek_char (di) == '.'
1119 && (IS_LOWER (d_peek_next_char (di))
1120 || d_peek_next_char (di) == '_'
1121 || IS_DIGIT (d_peek_next_char (di))))
1122 p = d_clone_suffix (di, p);
1127 /* Return whether a function should have a return type. The argument
1128 is the function name, which may be qualified in various ways. The
1129 rules are that template functions have return types with some
1130 exceptions, function types which are not part of a function name
1131 mangling have return types with some exceptions, and non-template
1132 function names do not have return types. The exceptions are that
1133 constructors, destructors, and conversion operators do not have
1137 has_return_type (struct demangle_component *dc)
1145 case DEMANGLE_COMPONENT_TEMPLATE:
1146 return ! is_ctor_dtor_or_conversion (d_left (dc));
1147 case DEMANGLE_COMPONENT_RESTRICT_THIS:
1148 case DEMANGLE_COMPONENT_VOLATILE_THIS:
1149 case DEMANGLE_COMPONENT_CONST_THIS:
1150 case DEMANGLE_COMPONENT_REFERENCE_THIS:
1151 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
1152 return has_return_type (d_left (dc));
1156 /* Return whether a name is a constructor, a destructor, or a
1157 conversion operator. */
1160 is_ctor_dtor_or_conversion (struct demangle_component *dc)
1168 case DEMANGLE_COMPONENT_QUAL_NAME:
1169 case DEMANGLE_COMPONENT_LOCAL_NAME:
1170 return is_ctor_dtor_or_conversion (d_right (dc));
1171 case DEMANGLE_COMPONENT_CTOR:
1172 case DEMANGLE_COMPONENT_DTOR:
1173 case DEMANGLE_COMPONENT_CAST:
1178 /* <encoding> ::= <(function) name> <bare-function-type>
1182 TOP_LEVEL is non-zero when called at the top level, in which case
1183 if DMGL_PARAMS is not set we do not demangle the function
1184 parameters. We only set this at the top level, because otherwise
1185 we would not correctly demangle names in local scopes. */
1187 static struct demangle_component *
1188 d_encoding (struct d_info *di, int top_level)
1190 char peek = d_peek_char (di);
1192 if (peek == 'G' || peek == 'T')
1193 return d_special_name (di);
1196 struct demangle_component *dc;
1200 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
1202 /* Strip off any initial CV-qualifiers, as they really apply
1203 to the `this' parameter, and they were not output by the
1204 v2 demangler without DMGL_PARAMS. */
1205 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1206 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1207 || dc->type == DEMANGLE_COMPONENT_CONST_THIS
1208 || dc->type == DEMANGLE_COMPONENT_REFERENCE_THIS
1209 || dc->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS)
1212 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1213 there may be CV-qualifiers on its right argument which
1214 really apply here; this happens when parsing a class
1215 which is local to a function. */
1216 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1218 struct demangle_component *dcr;
1221 while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1222 || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1223 || dcr->type == DEMANGLE_COMPONENT_CONST_THIS
1224 || dcr->type == DEMANGLE_COMPONENT_REFERENCE_THIS
1225 || dcr->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS)
1227 dc->u.s_binary.right = dcr;
1233 peek = d_peek_char (di);
1234 if (dc == NULL || peek == '\0' || peek == 'E')
1236 return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
1237 d_bare_function_type (di, has_return_type (dc)));
1241 /* <tagged-name> ::= <name> B <source-name> */
1243 static struct demangle_component *
1244 d_abi_tags (struct d_info *di, struct demangle_component *dc)
1247 while (peek = d_peek_char (di),
1250 struct demangle_component *tag;
1252 tag = d_source_name (di);
1253 dc = d_make_comp (di, DEMANGLE_COMPONENT_TAGGED_NAME, dc, tag);
1258 /* <name> ::= <nested-name>
1260 ::= <unscoped-template-name> <template-args>
1263 <unscoped-name> ::= <unqualified-name>
1264 ::= St <unqualified-name>
1266 <unscoped-template-name> ::= <unscoped-name>
1270 static struct demangle_component *
1271 d_name (struct d_info *di)
1273 char peek = d_peek_char (di);
1274 struct demangle_component *dc;
1279 return d_nested_name (di);
1282 return d_local_name (di);
1286 return d_unqualified_name (di);
1292 if (d_peek_next_char (di) != 't')
1294 dc = d_substitution (di, 0);
1300 dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
1301 d_make_name (di, "std", 3),
1302 d_unqualified_name (di));
1307 if (d_peek_char (di) != 'I')
1309 /* The grammar does not permit this case to occur if we
1310 called d_substitution() above (i.e., subst == 1). We
1311 don't bother to check. */
1315 /* This is <template-args>, which means that we just saw
1316 <unscoped-template-name>, which is a substitution
1317 candidate if we didn't just get it from a
1321 if (! d_add_substitution (di, dc))
1324 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1325 d_template_args (di));
1332 dc = d_unqualified_name (di);
1333 if (d_peek_char (di) == 'I')
1335 /* This is <template-args>, which means that we just saw
1336 <unscoped-template-name>, which is a substitution
1338 if (! d_add_substitution (di, dc))
1340 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1341 d_template_args (di));
1347 /* <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1348 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
1351 static struct demangle_component *
1352 d_nested_name (struct d_info *di)
1354 struct demangle_component *ret;
1355 struct demangle_component **pret;
1356 struct demangle_component *rqual;
1358 if (! d_check_char (di, 'N'))
1361 pret = d_cv_qualifiers (di, &ret, 1);
1365 /* Parse the ref-qualifier now and then attach it
1366 once we have something to attach it to. */
1367 rqual = d_ref_qualifier (di, NULL);
1369 *pret = d_prefix (di);
1375 d_left (rqual) = ret;
1379 if (! d_check_char (di, 'E'))
1385 /* <prefix> ::= <prefix> <unqualified-name>
1386 ::= <template-prefix> <template-args>
1387 ::= <template-param>
1392 <template-prefix> ::= <prefix> <(template) unqualified-name>
1393 ::= <template-param>
1397 static struct demangle_component *
1398 d_prefix (struct d_info *di)
1400 struct demangle_component *ret = NULL;
1405 enum demangle_component_type comb_type;
1406 struct demangle_component *dc;
1408 peek = d_peek_char (di);
1412 /* The older code accepts a <local-name> here, but I don't see
1413 that in the grammar. The older code does not accept a
1414 <template-param> here. */
1416 comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
1419 char peek2 = d_peek_next_char (di);
1420 if (peek2 == 'T' || peek2 == 't')
1422 dc = cplus_demangle_type (di);
1424 /* Destructor name. */
1425 dc = d_unqualified_name (di);
1427 else if (IS_DIGIT (peek)
1432 dc = d_unqualified_name (di);
1433 else if (peek == 'S')
1434 dc = d_substitution (di, 1);
1435 else if (peek == 'I')
1439 comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1440 dc = d_template_args (di);
1442 else if (peek == 'T')
1443 dc = d_template_param (di);
1444 else if (peek == 'E')
1446 else if (peek == 'M')
1448 /* Initializer scope for a lambda. We don't need to represent
1449 this; the normal code will just treat the variable as a type
1450 scope, which gives appropriate output. */
1462 ret = d_make_comp (di, comb_type, ret, dc);
1464 if (peek != 'S' && d_peek_char (di) != 'E')
1466 if (! d_add_substitution (di, ret))
1472 /* <unqualified-name> ::= <operator-name>
1473 ::= <ctor-dtor-name>
1475 ::= <local-source-name>
1477 <local-source-name> ::= L <source-name> <discriminator>
1480 static struct demangle_component *
1481 d_unqualified_name (struct d_info *di)
1483 struct demangle_component *ret;
1486 peek = d_peek_char (di);
1487 if (IS_DIGIT (peek))
1488 ret = d_source_name (di);
1489 else if (IS_LOWER (peek))
1491 ret = d_operator_name (di);
1492 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1494 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1495 if (!strcmp (ret->u.s_operator.op->code, "li"))
1496 ret = d_make_comp (di, DEMANGLE_COMPONENT_UNARY, ret,
1497 d_source_name (di));
1500 else if (peek == 'C' || peek == 'D')
1501 ret = d_ctor_dtor_name (di);
1502 else if (peek == 'L')
1506 ret = d_source_name (di);
1509 if (! d_discriminator (di))
1512 else if (peek == 'U')
1514 switch (d_peek_next_char (di))
1517 ret = d_lambda (di);
1520 ret = d_unnamed_type (di);
1529 if (d_peek_char (di) == 'B')
1530 ret = d_abi_tags (di, ret);
1534 /* <source-name> ::= <(positive length) number> <identifier> */
1536 static struct demangle_component *
1537 d_source_name (struct d_info *di)
1540 struct demangle_component *ret;
1542 len = d_number (di);
1545 ret = d_identifier (di, len);
1546 di->last_name = ret;
1550 /* number ::= [n] <(non-negative decimal integer)> */
1553 d_number (struct d_info *di)
1560 peek = d_peek_char (di);
1565 peek = d_peek_char (di);
1571 if (! IS_DIGIT (peek))
1577 ret = ret * 10 + peek - '0';
1579 peek = d_peek_char (di);
1583 /* Like d_number, but returns a demangle_component. */
1585 static struct demangle_component *
1586 d_number_component (struct d_info *di)
1588 struct demangle_component *ret = d_make_empty (di);
1591 ret->type = DEMANGLE_COMPONENT_NUMBER;
1592 ret->u.s_number.number = d_number (di);
1597 /* identifier ::= <(unqualified source code identifier)> */
1599 static struct demangle_component *
1600 d_identifier (struct d_info *di, int len)
1606 if (di->send - name < len)
1609 d_advance (di, len);
1611 /* A Java mangled name may have a trailing '$' if it is a C++
1612 keyword. This '$' is not included in the length count. We just
1614 if ((di->options & DMGL_JAVA) != 0
1615 && d_peek_char (di) == '$')
1618 /* Look for something which looks like a gcc encoding of an
1619 anonymous namespace, and replace it with a more user friendly
1621 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1622 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1623 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1627 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1628 if ((*s == '.' || *s == '_' || *s == '$')
1631 di->expansion -= len - sizeof "(anonymous namespace)";
1632 return d_make_name (di, "(anonymous namespace)",
1633 sizeof "(anonymous namespace)" - 1);
1637 return d_make_name (di, name, len);
1640 /* operator_name ::= many different two character encodings.
1642 ::= v <digit> <source-name>
1644 This list is sorted for binary search. */
1646 #define NL(s) s, (sizeof s) - 1
1648 CP_STATIC_IF_GLIBCPP_V3
1649 const struct demangle_operator_info cplus_demangle_operators[] =
1651 { "aN", NL ("&="), 2 },
1652 { "aS", NL ("="), 2 },
1653 { "aa", NL ("&&"), 2 },
1654 { "ad", NL ("&"), 1 },
1655 { "an", NL ("&"), 2 },
1656 { "at", NL ("alignof "), 1 },
1657 { "az", NL ("alignof "), 1 },
1658 { "cc", NL ("const_cast"), 2 },
1659 { "cl", NL ("()"), 2 },
1660 { "cm", NL (","), 2 },
1661 { "co", NL ("~"), 1 },
1662 { "dV", NL ("/="), 2 },
1663 { "da", NL ("delete[] "), 1 },
1664 { "dc", NL ("dynamic_cast"), 2 },
1665 { "de", NL ("*"), 1 },
1666 { "dl", NL ("delete "), 1 },
1667 { "ds", NL (".*"), 2 },
1668 { "dt", NL ("."), 2 },
1669 { "dv", NL ("/"), 2 },
1670 { "eO", NL ("^="), 2 },
1671 { "eo", NL ("^"), 2 },
1672 { "eq", NL ("=="), 2 },
1673 { "ge", NL (">="), 2 },
1674 { "gs", NL ("::"), 1 },
1675 { "gt", NL (">"), 2 },
1676 { "ix", NL ("[]"), 2 },
1677 { "lS", NL ("<<="), 2 },
1678 { "le", NL ("<="), 2 },
1679 { "li", NL ("operator\"\" "), 1 },
1680 { "ls", NL ("<<"), 2 },
1681 { "lt", NL ("<"), 2 },
1682 { "mI", NL ("-="), 2 },
1683 { "mL", NL ("*="), 2 },
1684 { "mi", NL ("-"), 2 },
1685 { "ml", NL ("*"), 2 },
1686 { "mm", NL ("--"), 1 },
1687 { "na", NL ("new[]"), 3 },
1688 { "ne", NL ("!="), 2 },
1689 { "ng", NL ("-"), 1 },
1690 { "nt", NL ("!"), 1 },
1691 { "nw", NL ("new"), 3 },
1692 { "oR", NL ("|="), 2 },
1693 { "oo", NL ("||"), 2 },
1694 { "or", NL ("|"), 2 },
1695 { "pL", NL ("+="), 2 },
1696 { "pl", NL ("+"), 2 },
1697 { "pm", NL ("->*"), 2 },
1698 { "pp", NL ("++"), 1 },
1699 { "ps", NL ("+"), 1 },
1700 { "pt", NL ("->"), 2 },
1701 { "qu", NL ("?"), 3 },
1702 { "rM", NL ("%="), 2 },
1703 { "rS", NL (">>="), 2 },
1704 { "rc", NL ("reinterpret_cast"), 2 },
1705 { "rm", NL ("%"), 2 },
1706 { "rs", NL (">>"), 2 },
1707 { "sc", NL ("static_cast"), 2 },
1708 { "st", NL ("sizeof "), 1 },
1709 { "sz", NL ("sizeof "), 1 },
1710 { "tr", NL ("throw"), 0 },
1711 { "tw", NL ("throw "), 1 },
1712 { NULL, NULL, 0, 0 }
1715 static struct demangle_component *
1716 d_operator_name (struct d_info *di)
1721 c1 = d_next_char (di);
1722 c2 = d_next_char (di);
1723 if (c1 == 'v' && IS_DIGIT (c2))
1724 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1725 else if (c1 == 'c' && c2 == 'v')
1726 return d_make_comp (di, DEMANGLE_COMPONENT_CAST,
1727 cplus_demangle_type (di), NULL);
1730 /* LOW is the inclusive lower bound. */
1732 /* HIGH is the exclusive upper bound. We subtract one to ignore
1733 the sentinel at the end of the array. */
1734 int high = ((sizeof (cplus_demangle_operators)
1735 / sizeof (cplus_demangle_operators[0]))
1741 const struct demangle_operator_info *p;
1743 i = low + (high - low) / 2;
1744 p = cplus_demangle_operators + i;
1746 if (c1 == p->code[0] && c2 == p->code[1])
1747 return d_make_operator (di, p);
1749 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1759 static struct demangle_component *
1760 d_make_character (struct d_info *di, int c)
1762 struct demangle_component *p;
1763 p = d_make_empty (di);
1766 p->type = DEMANGLE_COMPONENT_CHARACTER;
1767 p->u.s_character.character = c;
1772 static struct demangle_component *
1773 d_java_resource (struct d_info *di)
1775 struct demangle_component *p = NULL;
1776 struct demangle_component *next = NULL;
1781 len = d_number (di);
1785 /* Eat the leading '_'. */
1786 if (d_next_char (di) != '_')
1799 /* Each chunk is either a '$' escape... */
1817 next = d_make_character (di, c);
1825 /* ... or a sequence of characters. */
1828 while (i < len && str[i] && str[i] != '$')
1831 next = d_make_name (di, str, i);
1844 p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next);
1850 p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL);
1855 /* <special-name> ::= TV <type>
1859 ::= GV <(object) name>
1860 ::= T <call-offset> <(base) encoding>
1861 ::= Tc <call-offset> <call-offset> <(base) encoding>
1862 Also g++ extensions:
1863 ::= TC <type> <(offset) number> _ <(base) type>
1868 ::= Gr <resource name>
1873 static struct demangle_component *
1874 d_special_name (struct d_info *di)
1876 di->expansion += 20;
1877 if (d_check_char (di, 'T'))
1879 switch (d_next_char (di))
1883 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
1884 cplus_demangle_type (di), NULL);
1886 di->expansion -= 10;
1887 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
1888 cplus_demangle_type (di), NULL);
1890 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
1891 cplus_demangle_type (di), NULL);
1893 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
1894 cplus_demangle_type (di), NULL);
1897 if (! d_call_offset (di, 'h'))
1899 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
1900 d_encoding (di, 0), NULL);
1903 if (! d_call_offset (di, 'v'))
1905 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
1906 d_encoding (di, 0), NULL);
1909 if (! d_call_offset (di, '\0'))
1911 if (! d_call_offset (di, '\0'))
1913 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
1914 d_encoding (di, 0), NULL);
1918 struct demangle_component *derived_type;
1920 struct demangle_component *base_type;
1922 derived_type = cplus_demangle_type (di);
1923 offset = d_number (di);
1926 if (! d_check_char (di, '_'))
1928 base_type = cplus_demangle_type (di);
1929 /* We don't display the offset. FIXME: We should display
1930 it in verbose mode. */
1932 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
1933 base_type, derived_type);
1937 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
1938 cplus_demangle_type (di), NULL);
1940 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
1941 cplus_demangle_type (di), NULL);
1944 return d_make_comp (di, DEMANGLE_COMPONENT_TLS_INIT,
1948 return d_make_comp (di, DEMANGLE_COMPONENT_TLS_WRAPPER,
1955 else if (d_check_char (di, 'G'))
1957 switch (d_next_char (di))
1960 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
1964 struct demangle_component *name = d_name (di);
1965 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, name,
1966 d_number_component (di));
1970 return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
1971 d_encoding (di, 0), NULL);
1974 switch (d_next_char (di))
1977 return d_make_comp (di, DEMANGLE_COMPONENT_NONTRANSACTION_CLONE,
1978 d_encoding (di, 0), NULL);
1980 /* ??? The proposal is that other letters (such as 'h') stand
1981 for different variants of transaction cloning, such as
1982 compiling directly for hardware transaction support. But
1983 they still should all be transactional clones of some sort
1984 so go ahead and call them that. */
1986 return d_make_comp (di, DEMANGLE_COMPONENT_TRANSACTION_CLONE,
1987 d_encoding (di, 0), NULL);
1991 return d_java_resource (di);
2001 /* <call-offset> ::= h <nv-offset> _
2004 <nv-offset> ::= <(offset) number>
2006 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
2008 The C parameter, if not '\0', is a character we just read which is
2009 the start of the <call-offset>.
2011 We don't display the offset information anywhere. FIXME: We should
2012 display it in verbose mode. */
2015 d_call_offset (struct d_info *di, int c)
2018 c = d_next_char (di);
2025 if (! d_check_char (di, '_'))
2032 if (! d_check_char (di, '_'))
2038 /* <ctor-dtor-name> ::= C1
2046 static struct demangle_component *
2047 d_ctor_dtor_name (struct d_info *di)
2049 if (di->last_name != NULL)
2051 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
2052 di->expansion += di->last_name->u.s_name.len;
2053 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
2054 di->expansion += di->last_name->u.s_string.len;
2056 switch (d_peek_char (di))
2060 enum gnu_v3_ctor_kinds kind;
2062 switch (d_peek_next_char (di))
2065 kind = gnu_v3_complete_object_ctor;
2068 kind = gnu_v3_base_object_ctor;
2071 kind = gnu_v3_complete_object_allocating_ctor;
2074 kind = gnu_v3_object_ctor_group;
2080 return d_make_ctor (di, kind, di->last_name);
2085 enum gnu_v3_dtor_kinds kind;
2087 switch (d_peek_next_char (di))
2090 kind = gnu_v3_deleting_dtor;
2093 kind = gnu_v3_complete_object_dtor;
2096 kind = gnu_v3_base_object_dtor;
2099 kind = gnu_v3_object_dtor_group;
2105 return d_make_dtor (di, kind, di->last_name);
2113 /* <type> ::= <builtin-type>
2115 ::= <class-enum-type>
2117 ::= <pointer-to-member-type>
2118 ::= <template-param>
2119 ::= <template-template-param> <template-args>
2121 ::= <CV-qualifiers> <type>
2124 ::= O <type> (C++0x)
2127 ::= U <source-name> <type>
2129 <builtin-type> ::= various one letter codes
2133 CP_STATIC_IF_GLIBCPP_V3
2134 const struct demangle_builtin_type_info
2135 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
2137 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
2138 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
2139 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
2140 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
2141 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
2142 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
2143 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
2144 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
2145 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
2146 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
2147 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2148 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
2149 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
2150 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
2151 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
2153 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2154 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2155 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2156 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
2157 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
2158 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2159 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
2160 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
2161 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
2162 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
2163 D_PRINT_UNSIGNED_LONG_LONG },
2164 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
2165 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT },
2166 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT },
2167 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT },
2168 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT },
2169 /* 30 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT },
2170 /* 31 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT },
2171 /* 32 */ { NL ("decltype(nullptr)"), NL ("decltype(nullptr)"),
2175 CP_STATIC_IF_GLIBCPP_V3
2176 struct demangle_component *
2177 cplus_demangle_type (struct d_info *di)
2180 struct demangle_component *ret;
2183 /* The ABI specifies that when CV-qualifiers are used, the base type
2184 is substitutable, and the fully qualified type is substitutable,
2185 but the base type with a strict subset of the CV-qualifiers is
2186 not substitutable. The natural recursive implementation of the
2187 CV-qualifiers would cause subsets to be substitutable, so instead
2188 we pull them all off now.
2190 FIXME: The ABI says that order-insensitive vendor qualifiers
2191 should be handled in the same way, but we have no way to tell
2192 which vendor qualifiers are order-insensitive and which are
2193 order-sensitive. So we just assume that they are all
2194 order-sensitive. g++ 3.4 supports only one vendor qualifier,
2195 __vector, and it treats it as order-sensitive when mangling
2198 peek = d_peek_char (di);
2199 if (peek == 'r' || peek == 'V' || peek == 'K')
2201 struct demangle_component **pret;
2203 pret = d_cv_qualifiers (di, &ret, 0);
2206 if (d_peek_char (di) == 'F')
2208 /* cv-qualifiers before a function type apply to 'this',
2209 so avoid adding the unqualified function type to
2210 the substitution list. */
2211 *pret = d_function_type (di);
2214 *pret = cplus_demangle_type (di);
2217 if ((*pret)->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
2218 || (*pret)->type == DEMANGLE_COMPONENT_REFERENCE_THIS)
2220 /* Move the ref-qualifier outside the cv-qualifiers so that
2221 they are printed in the right order. */
2222 struct demangle_component *fn = d_left (*pret);
2223 d_left (*pret) = ret;
2227 if (! d_add_substitution (di, ret))
2236 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2237 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
2238 case 'o': case 's': case 't':
2239 case 'v': case 'w': case 'x': case 'y': case 'z':
2240 ret = d_make_builtin_type (di,
2241 &cplus_demangle_builtin_types[peek - 'a']);
2242 di->expansion += ret->u.s_builtin.type->len;
2249 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
2250 d_source_name (di), NULL);
2254 ret = d_function_type (di);
2257 case '0': case '1': case '2': case '3': case '4':
2258 case '5': case '6': case '7': case '8': case '9':
2261 ret = d_class_enum_type (di);
2265 ret = d_array_type (di);
2269 ret = d_pointer_to_member_type (di);
2273 ret = d_template_param (di);
2274 if (d_peek_char (di) == 'I')
2276 /* This is <template-template-param> <template-args>. The
2277 <template-template-param> part is a substitution
2279 if (! d_add_substitution (di, ret))
2281 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2282 d_template_args (di));
2287 /* If this is a special substitution, then it is the start of
2288 <class-enum-type>. */
2292 peek_next = d_peek_next_char (di);
2293 if (IS_DIGIT (peek_next)
2295 || IS_UPPER (peek_next))
2297 ret = d_substitution (di, 0);
2298 /* The substituted name may have been a template name and
2299 may be followed by tepmlate args. */
2300 if (d_peek_char (di) == 'I')
2301 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2302 d_template_args (di));
2308 ret = d_class_enum_type (di);
2309 /* If the substitution was a complete type, then it is not
2310 a new substitution candidate. However, if the
2311 substitution was followed by template arguments, then
2312 the whole thing is a substitution candidate. */
2313 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
2321 ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE,
2322 cplus_demangle_type (di), NULL);
2327 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
2328 cplus_demangle_type (di), NULL);
2333 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
2334 cplus_demangle_type (di), NULL);
2339 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
2340 cplus_demangle_type (di), NULL);
2345 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
2346 cplus_demangle_type (di), NULL);
2351 ret = d_source_name (di);
2352 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
2353 cplus_demangle_type (di), ret);
2359 peek = d_next_char (di);
2364 /* decltype (expression) */
2365 ret = d_make_comp (di, DEMANGLE_COMPONENT_DECLTYPE,
2366 d_expression (di), NULL);
2367 if (ret && d_next_char (di) != 'E')
2373 /* Pack expansion. */
2374 ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2375 cplus_demangle_type (di), NULL);
2381 ret = d_make_name (di, "auto", 4);
2385 /* 32-bit decimal floating point */
2386 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[26]);
2387 di->expansion += ret->u.s_builtin.type->len;
2391 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[27]);
2392 di->expansion += ret->u.s_builtin.type->len;
2396 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[28]);
2397 di->expansion += ret->u.s_builtin.type->len;
2400 /* 16-bit half-precision FP */
2401 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[29]);
2402 di->expansion += ret->u.s_builtin.type->len;
2406 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[30]);
2407 di->expansion += ret->u.s_builtin.type->len;
2411 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[31]);
2412 di->expansion += ret->u.s_builtin.type->len;
2416 /* Fixed point types. DF<int bits><length><fract bits><sat> */
2417 ret = d_make_empty (di);
2418 ret->type = DEMANGLE_COMPONENT_FIXED_TYPE;
2419 if ((ret->u.s_fixed.accum = IS_DIGIT (d_peek_char (di))))
2420 /* For demangling we don't care about the bits. */
2422 ret->u.s_fixed.length = cplus_demangle_type (di);
2423 if (ret->u.s_fixed.length == NULL)
2426 peek = d_next_char (di);
2427 ret->u.s_fixed.sat = (peek == 's');
2431 ret = d_vector_type (di);
2436 /* decltype(nullptr) */
2437 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[32]);
2438 di->expansion += ret->u.s_builtin.type->len;
2452 if (! d_add_substitution (di, ret))
2459 /* <CV-qualifiers> ::= [r] [V] [K] */
2461 static struct demangle_component **
2462 d_cv_qualifiers (struct d_info *di,
2463 struct demangle_component **pret, int member_fn)
2465 struct demangle_component **pstart;
2469 peek = d_peek_char (di);
2470 while (peek == 'r' || peek == 'V' || peek == 'K')
2472 enum demangle_component_type t;
2478 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2479 : DEMANGLE_COMPONENT_RESTRICT);
2480 di->expansion += sizeof "restrict";
2482 else if (peek == 'V')
2485 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2486 : DEMANGLE_COMPONENT_VOLATILE);
2487 di->expansion += sizeof "volatile";
2492 ? DEMANGLE_COMPONENT_CONST_THIS
2493 : DEMANGLE_COMPONENT_CONST);
2494 di->expansion += sizeof "const";
2497 *pret = d_make_comp (di, t, NULL, NULL);
2500 pret = &d_left (*pret);
2502 peek = d_peek_char (di);
2505 if (!member_fn && peek == 'F')
2507 while (pstart != pret)
2509 switch ((*pstart)->type)
2511 case DEMANGLE_COMPONENT_RESTRICT:
2512 (*pstart)->type = DEMANGLE_COMPONENT_RESTRICT_THIS;
2514 case DEMANGLE_COMPONENT_VOLATILE:
2515 (*pstart)->type = DEMANGLE_COMPONENT_VOLATILE_THIS;
2517 case DEMANGLE_COMPONENT_CONST:
2518 (*pstart)->type = DEMANGLE_COMPONENT_CONST_THIS;
2523 pstart = &d_left (*pstart);
2530 /* <ref-qualifier> ::= R
2533 static struct demangle_component *
2534 d_ref_qualifier (struct d_info *di, struct demangle_component *sub)
2536 struct demangle_component *ret = sub;
2539 peek = d_peek_char (di);
2540 if (peek == 'R' || peek == 'O')
2542 enum demangle_component_type t;
2545 t = DEMANGLE_COMPONENT_REFERENCE_THIS;
2546 di->expansion += sizeof "&";
2550 t = DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS;
2551 di->expansion += sizeof "&&";
2555 ret = d_make_comp (di, t, ret, NULL);
2561 /* <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] E */
2563 static struct demangle_component *
2564 d_function_type (struct d_info *di)
2566 struct demangle_component *ret;
2568 if (! d_check_char (di, 'F'))
2570 if (d_peek_char (di) == 'Y')
2572 /* Function has C linkage. We don't print this information.
2573 FIXME: We should print it in verbose mode. */
2576 ret = d_bare_function_type (di, 1);
2577 ret = d_ref_qualifier (di, ret);
2579 if (! d_check_char (di, 'E'))
2586 static struct demangle_component *
2587 d_parmlist (struct d_info *di)
2589 struct demangle_component *tl;
2590 struct demangle_component **ptl;
2596 struct demangle_component *type;
2598 char peek = d_peek_char (di);
2599 if (peek == '\0' || peek == 'E' || peek == '.')
2601 if ((peek == 'R' || peek == 'O')
2602 && d_peek_next_char (di) == 'E')
2603 /* Function ref-qualifier, not a ref prefix for a parameter type. */
2605 type = cplus_demangle_type (di);
2608 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
2611 ptl = &d_right (*ptl);
2614 /* There should be at least one parameter type besides the optional
2615 return type. A function which takes no arguments will have a
2616 single parameter type void. */
2620 /* If we have a single parameter type void, omit it. */
2621 if (d_right (tl) == NULL
2622 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2623 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
2625 di->expansion -= d_left (tl)->u.s_builtin.type->len;
2632 /* <bare-function-type> ::= [J]<type>+ */
2634 static struct demangle_component *
2635 d_bare_function_type (struct d_info *di, int has_return_type)
2637 struct demangle_component *return_type;
2638 struct demangle_component *tl;
2641 /* Detect special qualifier indicating that the first argument
2642 is the return type. */
2643 peek = d_peek_char (di);
2647 has_return_type = 1;
2650 if (has_return_type)
2652 return_type = cplus_demangle_type (di);
2653 if (return_type == NULL)
2659 tl = d_parmlist (di);
2663 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE,
2667 /* <class-enum-type> ::= <name> */
2669 static struct demangle_component *
2670 d_class_enum_type (struct d_info *di)
2675 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2676 ::= A [<(dimension) expression>] _ <(element) type>
2679 static struct demangle_component *
2680 d_array_type (struct d_info *di)
2683 struct demangle_component *dim;
2685 if (! d_check_char (di, 'A'))
2688 peek = d_peek_char (di);
2691 else if (IS_DIGIT (peek))
2699 peek = d_peek_char (di);
2701 while (IS_DIGIT (peek));
2702 dim = d_make_name (di, s, d_str (di) - s);
2708 dim = d_expression (di);
2713 if (! d_check_char (di, '_'))
2716 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2717 cplus_demangle_type (di));
2720 /* <vector-type> ::= Dv <number> _ <type>
2721 ::= Dv _ <expression> _ <type> */
2723 static struct demangle_component *
2724 d_vector_type (struct d_info *di)
2727 struct demangle_component *dim;
2729 peek = d_peek_char (di);
2733 dim = d_expression (di);
2736 dim = d_number_component (di);
2741 if (! d_check_char (di, '_'))
2744 return d_make_comp (di, DEMANGLE_COMPONENT_VECTOR_TYPE, dim,
2745 cplus_demangle_type (di));
2748 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2750 static struct demangle_component *
2751 d_pointer_to_member_type (struct d_info *di)
2753 struct demangle_component *cl;
2754 struct demangle_component *mem;
2756 if (! d_check_char (di, 'M'))
2759 cl = cplus_demangle_type (di);
2763 /* The ABI says, "The type of a non-static member function is considered
2764 to be different, for the purposes of substitution, from the type of a
2765 namespace-scope or static member function whose type appears
2766 similar. The types of two non-static member functions are considered
2767 to be different, for the purposes of substitution, if the functions
2768 are members of different classes. In other words, for the purposes of
2769 substitution, the class of which the function is a member is
2770 considered part of the type of function."
2772 For a pointer to member function, this call to cplus_demangle_type
2773 will end up adding a (possibly qualified) non-member function type to
2774 the substitution table, which is not correct; however, the member
2775 function type will never be used in a substitution, so putting the
2776 wrong type in the substitution table is harmless. */
2778 mem = cplus_demangle_type (di);
2782 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
2785 /* <non-negative number> _ */
2788 d_compact_number (struct d_info *di)
2791 if (d_peek_char (di) == '_')
2793 else if (d_peek_char (di) == 'n')
2796 num = d_number (di) + 1;
2798 if (! d_check_char (di, '_'))
2803 /* <template-param> ::= T_
2804 ::= T <(parameter-2 non-negative) number> _
2807 static struct demangle_component *
2808 d_template_param (struct d_info *di)
2812 if (! d_check_char (di, 'T'))
2815 param = d_compact_number (di);
2821 return d_make_template_param (di, param);
2824 /* <template-args> ::= I <template-arg>+ E */
2826 static struct demangle_component *
2827 d_template_args (struct d_info *di)
2829 struct demangle_component *hold_last_name;
2830 struct demangle_component *al;
2831 struct demangle_component **pal;
2833 /* Preserve the last name we saw--don't let the template arguments
2834 clobber it, as that would give us the wrong name for a subsequent
2835 constructor or destructor. */
2836 hold_last_name = di->last_name;
2838 if (d_peek_char (di) != 'I'
2839 && d_peek_char (di) != 'J')
2843 if (d_peek_char (di) == 'E')
2845 /* An argument pack can be empty. */
2847 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, NULL, NULL);
2854 struct demangle_component *a;
2856 a = d_template_arg (di);
2860 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
2863 pal = &d_right (*pal);
2865 if (d_peek_char (di) == 'E')
2872 di->last_name = hold_last_name;
2877 /* <template-arg> ::= <type>
2878 ::= X <expression> E
2882 static struct demangle_component *
2883 d_template_arg (struct d_info *di)
2885 struct demangle_component *ret;
2887 switch (d_peek_char (di))
2891 ret = d_expression (di);
2892 if (! d_check_char (di, 'E'))
2897 return d_expr_primary (di);
2901 /* An argument pack. */
2902 return d_template_args (di);
2905 return cplus_demangle_type (di);
2909 /* Parse a sequence of expressions until we hit the terminator
2912 static struct demangle_component *
2913 d_exprlist (struct d_info *di, char terminator)
2915 struct demangle_component *list = NULL;
2916 struct demangle_component **p = &list;
2918 if (d_peek_char (di) == terminator)
2921 return d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, NULL, NULL);
2926 struct demangle_component *arg = d_expression (di);
2930 *p = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, arg, NULL);
2935 if (d_peek_char (di) == terminator)
2945 /* Returns nonzero iff OP is an operator for a C++ cast: const_cast,
2946 dynamic_cast, static_cast or reinterpret_cast. */
2949 op_is_new_cast (struct demangle_component *op)
2951 const char *code = op->u.s_operator.op->code;
2952 return (code[1] == 'c'
2953 && (code[0] == 's' || code[0] == 'd'
2954 || code[0] == 'c' || code[0] == 'r'));
2957 /* <expression> ::= <(unary) operator-name> <expression>
2958 ::= <(binary) operator-name> <expression> <expression>
2959 ::= <(trinary) operator-name> <expression> <expression> <expression>
2960 ::= cl <expression>+ E
2962 ::= <template-param>
2963 ::= sr <type> <unqualified-name>
2964 ::= sr <type> <unqualified-name> <template-args>
2968 static struct demangle_component *
2969 d_expression (struct d_info *di)
2973 peek = d_peek_char (di);
2975 return d_expr_primary (di);
2976 else if (peek == 'T')
2977 return d_template_param (di);
2978 else if (peek == 's' && d_peek_next_char (di) == 'r')
2980 struct demangle_component *type;
2981 struct demangle_component *name;
2984 type = cplus_demangle_type (di);
2985 name = d_unqualified_name (di);
2986 if (d_peek_char (di) != 'I')
2987 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
2989 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
2990 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2991 d_template_args (di)));
2993 else if (peek == 's' && d_peek_next_char (di) == 'p')
2996 return d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2997 d_expression (di), NULL);
2999 else if (peek == 'f' && d_peek_next_char (di) == 'p')
3001 /* Function parameter used in a late-specified return type. */
3004 if (d_peek_char (di) == 'T')
3006 /* 'this' parameter. */
3012 index = d_compact_number (di) + 1;
3016 return d_make_function_param (di, index);
3018 else if (IS_DIGIT (peek)
3019 || (peek == 'o' && d_peek_next_char (di) == 'n'))
3021 /* We can get an unqualified name as an expression in the case of
3022 a dependent function call, i.e. decltype(f(t)). */
3023 struct demangle_component *name;
3026 /* operator-function-id, i.e. operator+(t). */
3029 name = d_unqualified_name (di);
3032 if (d_peek_char (di) == 'I')
3033 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
3034 d_template_args (di));
3038 else if ((peek == 'i' || peek == 't')
3039 && d_peek_next_char (di) == 'l')
3041 /* Brace-enclosed initializer list, untyped or typed. */
3042 struct demangle_component *type = NULL;
3044 type = cplus_demangle_type (di);
3046 return d_make_comp (di, DEMANGLE_COMPONENT_INITIALIZER_LIST,
3047 type, d_exprlist (di, 'E'));
3051 struct demangle_component *op;
3052 const char *code = NULL;
3055 op = d_operator_name (di);
3059 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
3061 code = op->u.s_operator.op->code;
3062 di->expansion += op->u.s_operator.op->len - 2;
3063 if (strcmp (code, "st") == 0)
3064 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3065 cplus_demangle_type (di));
3072 case DEMANGLE_COMPONENT_OPERATOR:
3073 args = op->u.s_operator.op->args;
3075 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3076 args = op->u.s_extended_operator.args;
3078 case DEMANGLE_COMPONENT_CAST:
3086 return d_make_comp (di, DEMANGLE_COMPONENT_NULLARY, op, NULL);
3090 struct demangle_component *operand;
3093 if (code && (code[0] == 'p' || code[0] == 'm')
3094 && code[1] == code[0])
3095 /* pp_ and mm_ are the prefix variants. */
3096 suffix = !d_check_char (di, '_');
3098 if (op->type == DEMANGLE_COMPONENT_CAST
3099 && d_check_char (di, '_'))
3100 operand = d_exprlist (di, 'E');
3102 operand = d_expression (di);
3105 /* Indicate the suffix variant for d_print_comp. */
3106 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3108 DEMANGLE_COMPONENT_BINARY_ARGS,
3111 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3116 struct demangle_component *left;
3117 struct demangle_component *right;
3119 if (op_is_new_cast (op))
3120 left = cplus_demangle_type (di);
3122 left = d_expression (di);
3123 if (!strcmp (code, "cl"))
3124 right = d_exprlist (di, 'E');
3125 else if (!strcmp (code, "dt") || !strcmp (code, "pt"))
3127 right = d_unqualified_name (di);
3128 if (d_peek_char (di) == 'I')
3129 right = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE,
3130 right, d_template_args (di));
3133 right = d_expression (di);
3135 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
3137 DEMANGLE_COMPONENT_BINARY_ARGS,
3142 struct demangle_component *first;
3143 struct demangle_component *second;
3144 struct demangle_component *third;
3146 if (!strcmp (code, "qu"))
3148 /* ?: expression. */
3149 first = d_expression (di);
3150 second = d_expression (di);
3151 third = d_expression (di);
3153 else if (code[0] == 'n')
3155 /* new-expression. */
3156 if (code[1] != 'w' && code[1] != 'a')
3158 first = d_exprlist (di, '_');
3159 second = cplus_demangle_type (di);
3160 if (d_peek_char (di) == 'E')
3165 else if (d_peek_char (di) == 'p'
3166 && d_peek_next_char (di) == 'i')
3168 /* Parenthesized initializer. */
3170 third = d_exprlist (di, 'E');
3172 else if (d_peek_char (di) == 'i'
3173 && d_peek_next_char (di) == 'l')
3174 /* initializer-list. */
3175 third = d_expression (di);
3181 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
3183 DEMANGLE_COMPONENT_TRINARY_ARG1,
3186 DEMANGLE_COMPONENT_TRINARY_ARG2,
3195 /* <expr-primary> ::= L <type> <(value) number> E
3196 ::= L <type> <(value) float> E
3197 ::= L <mangled-name> E
3200 static struct demangle_component *
3201 d_expr_primary (struct d_info *di)
3203 struct demangle_component *ret;
3205 if (! d_check_char (di, 'L'))
3207 if (d_peek_char (di) == '_'
3208 /* Workaround for G++ bug; see comment in write_template_arg. */
3209 || d_peek_char (di) == 'Z')
3210 ret = cplus_demangle_mangled_name (di, 0);
3213 struct demangle_component *type;
3214 enum demangle_component_type t;
3217 type = cplus_demangle_type (di);
3221 /* If we have a type we know how to print, we aren't going to
3222 print the type name itself. */
3223 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
3224 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
3225 di->expansion -= type->u.s_builtin.type->len;
3227 /* Rather than try to interpret the literal value, we just
3228 collect it as a string. Note that it's possible to have a
3229 floating point literal here. The ABI specifies that the
3230 format of such literals is machine independent. That's fine,
3231 but what's not fine is that versions of g++ up to 3.2 with
3232 -fabi-version=1 used upper case letters in the hex constant,
3233 and dumped out gcc's internal representation. That makes it
3234 hard to tell where the constant ends, and hard to dump the
3235 constant in any readable form anyhow. We don't attempt to
3236 handle these cases. */
3238 t = DEMANGLE_COMPONENT_LITERAL;
3239 if (d_peek_char (di) == 'n')
3241 t = DEMANGLE_COMPONENT_LITERAL_NEG;
3245 while (d_peek_char (di) != 'E')
3247 if (d_peek_char (di) == '\0')
3251 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
3253 if (! d_check_char (di, 'E'))
3258 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
3259 ::= Z <(function) encoding> E s [<discriminator>]
3260 ::= Z <(function) encoding> E d [<parameter> number>] _ <entity name>
3263 static struct demangle_component *
3264 d_local_name (struct d_info *di)
3266 struct demangle_component *function;
3268 if (! d_check_char (di, 'Z'))
3271 function = d_encoding (di, 0);
3273 if (! d_check_char (di, 'E'))
3276 if (d_peek_char (di) == 's')
3279 if (! d_discriminator (di))
3281 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
3282 d_make_name (di, "string literal",
3283 sizeof "string literal" - 1));
3287 struct demangle_component *name;
3290 if (d_peek_char (di) == 'd')
3292 /* Default argument scope: d <number> _. */
3294 num = d_compact_number (di);
3303 /* Lambdas and unnamed types have internal discriminators. */
3304 case DEMANGLE_COMPONENT_LAMBDA:
3305 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
3308 if (! d_discriminator (di))
3312 name = d_make_default_arg (di, num, name);
3313 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
3317 /* <discriminator> ::= _ <(non-negative) number>
3319 We demangle the discriminator, but we don't print it out. FIXME:
3320 We should print it out in verbose mode. */
3323 d_discriminator (struct d_info *di)
3327 if (d_peek_char (di) != '_')
3330 discrim = d_number (di);
3336 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ */
3338 static struct demangle_component *
3339 d_lambda (struct d_info *di)
3341 struct demangle_component *tl;
3342 struct demangle_component *ret;
3345 if (! d_check_char (di, 'U'))
3347 if (! d_check_char (di, 'l'))
3350 tl = d_parmlist (di);
3354 if (! d_check_char (di, 'E'))
3357 num = d_compact_number (di);
3361 ret = d_make_empty (di);
3364 ret->type = DEMANGLE_COMPONENT_LAMBDA;
3365 ret->u.s_unary_num.sub = tl;
3366 ret->u.s_unary_num.num = num;
3369 if (! d_add_substitution (di, ret))
3375 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
3377 static struct demangle_component *
3378 d_unnamed_type (struct d_info *di)
3380 struct demangle_component *ret;
3383 if (! d_check_char (di, 'U'))
3385 if (! d_check_char (di, 't'))
3388 num = d_compact_number (di);
3392 ret = d_make_empty (di);
3395 ret->type = DEMANGLE_COMPONENT_UNNAMED_TYPE;
3396 ret->u.s_number.number = num;
3399 if (! d_add_substitution (di, ret))
3405 /* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]*
3408 static struct demangle_component *
3409 d_clone_suffix (struct d_info *di, struct demangle_component *encoding)
3411 const char *suffix = d_str (di);
3412 const char *pend = suffix;
3413 struct demangle_component *n;
3415 if (*pend == '.' && (IS_LOWER (pend[1]) || pend[1] == '_'))
3418 while (IS_LOWER (*pend) || *pend == '_')
3421 while (*pend == '.' && IS_DIGIT (pend[1]))
3424 while (IS_DIGIT (*pend))
3427 d_advance (di, pend - suffix);
3428 n = d_make_name (di, suffix, pend - suffix);
3429 return d_make_comp (di, DEMANGLE_COMPONENT_CLONE, encoding, n);
3432 /* Add a new substitution. */
3435 d_add_substitution (struct d_info *di, struct demangle_component *dc)
3439 if (di->next_sub >= di->num_subs)
3441 di->subs[di->next_sub] = dc;
3446 /* <substitution> ::= S <seq-id> _
3456 If PREFIX is non-zero, then this type is being used as a prefix in
3457 a qualified name. In this case, for the standard substitutions, we
3458 need to check whether we are being used as a prefix for a
3459 constructor or destructor, and return a full template name.
3460 Otherwise we will get something like std::iostream::~iostream()
3461 which does not correspond particularly well to any function which
3462 actually appears in the source.
3465 static const struct d_standard_sub_info standard_subs[] =
3470 { 'a', NL ("std::allocator"),
3471 NL ("std::allocator"),
3473 { 'b', NL ("std::basic_string"),
3474 NL ("std::basic_string"),
3475 NL ("basic_string") },
3476 { 's', NL ("std::string"),
3477 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
3478 NL ("basic_string") },
3479 { 'i', NL ("std::istream"),
3480 NL ("std::basic_istream<char, std::char_traits<char> >"),
3481 NL ("basic_istream") },
3482 { 'o', NL ("std::ostream"),
3483 NL ("std::basic_ostream<char, std::char_traits<char> >"),
3484 NL ("basic_ostream") },
3485 { 'd', NL ("std::iostream"),
3486 NL ("std::basic_iostream<char, std::char_traits<char> >"),
3487 NL ("basic_iostream") }
3490 static struct demangle_component *
3491 d_substitution (struct d_info *di, int prefix)
3495 if (! d_check_char (di, 'S'))
3498 c = d_next_char (di);
3499 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
3508 unsigned int new_id;
3511 new_id = id * 36 + c - '0';
3512 else if (IS_UPPER (c))
3513 new_id = id * 36 + c - 'A' + 10;
3519 c = d_next_char (di);
3526 if (id >= (unsigned int) di->next_sub)
3531 return di->subs[id];
3536 const struct d_standard_sub_info *p;
3537 const struct d_standard_sub_info *pend;
3539 verbose = (di->options & DMGL_VERBOSE) != 0;
3540 if (! verbose && prefix)
3544 peek = d_peek_char (di);
3545 if (peek == 'C' || peek == 'D')
3549 pend = (&standard_subs[0]
3550 + sizeof standard_subs / sizeof standard_subs[0]);
3551 for (p = &standard_subs[0]; p < pend; ++p)
3558 if (p->set_last_name != NULL)
3559 di->last_name = d_make_sub (di, p->set_last_name,
3560 p->set_last_name_len);
3563 s = p->full_expansion;
3568 s = p->simple_expansion;
3569 len = p->simple_len;
3571 di->expansion += len;
3572 return d_make_sub (di, s, len);
3580 /* Initialize a growable string. */
3583 d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
3588 dgs->allocation_failure = 0;
3591 d_growable_string_resize (dgs, estimate);
3594 /* Grow a growable string to a given size. */
3597 d_growable_string_resize (struct d_growable_string *dgs, size_t need)
3602 if (dgs->allocation_failure)
3605 /* Start allocation at two bytes to avoid any possibility of confusion
3606 with the special value of 1 used as a return in *palc to indicate
3607 allocation failures. */
3608 newalc = dgs->alc > 0 ? dgs->alc : 2;
3609 while (newalc < need)
3612 newbuf = (char *) realloc (dgs->buf, newalc);
3619 dgs->allocation_failure = 1;
3626 /* Append a buffer to a growable string. */
3629 d_growable_string_append_buffer (struct d_growable_string *dgs,
3630 const char *s, size_t l)
3634 need = dgs->len + l + 1;
3635 if (need > dgs->alc)
3636 d_growable_string_resize (dgs, need);
3638 if (dgs->allocation_failure)
3641 memcpy (dgs->buf + dgs->len, s, l);
3642 dgs->buf[dgs->len + l] = '\0';
3646 /* Bridge growable strings to the callback mechanism. */
3649 d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
3651 struct d_growable_string *dgs = (struct d_growable_string*) opaque;
3653 d_growable_string_append_buffer (dgs, s, l);
3656 /* Initialize a print information structure. */
3659 d_print_init (struct d_print_info *dpi, demangle_callbackref callback,
3663 dpi->last_char = '\0';
3664 dpi->templates = NULL;
3665 dpi->modifiers = NULL;
3666 dpi->pack_index = 0;
3667 dpi->flush_count = 0;
3669 dpi->callback = callback;
3670 dpi->opaque = opaque;
3672 dpi->demangle_failure = 0;
3674 dpi->saved_scopes = NULL;
3677 /* Free a print information structure. */
3680 d_print_free (struct d_print_info *dpi)
3682 if (dpi->saved_scopes != NULL)
3683 htab_delete (dpi->saved_scopes);
3686 /* Indicate that an error occurred during printing, and test for error. */
3689 d_print_error (struct d_print_info *dpi)
3691 dpi->demangle_failure = 1;
3695 d_print_saw_error (struct d_print_info *dpi)
3697 return dpi->demangle_failure != 0;
3700 /* Flush buffered characters to the callback. */
3703 d_print_flush (struct d_print_info *dpi)
3705 dpi->buf[dpi->len] = '\0';
3706 dpi->callback (dpi->buf, dpi->len, dpi->opaque);
3711 /* Append characters and buffers for printing. */
3714 d_append_char (struct d_print_info *dpi, char c)
3716 if (dpi->len == sizeof (dpi->buf) - 1)
3717 d_print_flush (dpi);
3719 dpi->buf[dpi->len++] = c;
3724 d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
3728 for (i = 0; i < l; i++)
3729 d_append_char (dpi, s[i]);
3733 d_append_string (struct d_print_info *dpi, const char *s)
3735 d_append_buffer (dpi, s, strlen (s));
3739 d_append_num (struct d_print_info *dpi, long l)
3742 sprintf (buf,"%ld", l);
3743 d_append_string (dpi, buf);
3747 d_last_char (struct d_print_info *dpi)
3749 return dpi->last_char;
3752 /* Turn components into a human readable string. OPTIONS is the
3753 options bits passed to the demangler. DC is the tree to print.
3754 CALLBACK is a function to call to flush demangled string segments
3755 as they fill the intermediate buffer, and OPAQUE is a generalized
3756 callback argument. On success, this returns 1. On failure,
3757 it returns 0, indicating a bad parse. It does not use heap
3758 memory to build an output string, so cannot encounter memory
3759 allocation failure. */
3761 CP_STATIC_IF_GLIBCPP_V3
3763 cplus_demangle_print_callback (int options,
3764 const struct demangle_component *dc,
3765 demangle_callbackref callback, void *opaque)
3767 struct d_print_info dpi;
3770 d_print_init (&dpi, callback, opaque);
3772 d_print_comp (&dpi, options, dc);
3774 d_print_flush (&dpi);
3776 success = ! d_print_saw_error (&dpi);
3777 d_print_free (&dpi);
3781 /* Turn components into a human readable string. OPTIONS is the
3782 options bits passed to the demangler. DC is the tree to print.
3783 ESTIMATE is a guess at the length of the result. This returns a
3784 string allocated by malloc, or NULL on error. On success, this
3785 sets *PALC to the size of the allocated buffer. On failure, this
3786 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
3789 CP_STATIC_IF_GLIBCPP_V3
3791 cplus_demangle_print (int options, const struct demangle_component *dc,
3792 int estimate, size_t *palc)
3794 struct d_growable_string dgs;
3796 d_growable_string_init (&dgs, estimate);
3798 if (! cplus_demangle_print_callback (options, dc,
3799 d_growable_string_callback_adapter,
3807 *palc = dgs.allocation_failure ? 1 : dgs.alc;
3811 /* Returns the I'th element of the template arglist ARGS, or NULL on
3814 static struct demangle_component *
3815 d_index_template_argument (struct demangle_component *args, int i)
3817 struct demangle_component *a;
3823 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3829 if (i != 0 || a == NULL)
3835 /* Returns the template argument from the current context indicated by DC,
3836 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
3838 static struct demangle_component *
3839 d_lookup_template_argument (struct d_print_info *dpi,
3840 const struct demangle_component *dc)
3842 if (dpi->templates == NULL)
3844 d_print_error (dpi);
3848 return d_index_template_argument
3849 (d_right (dpi->templates->template_decl),
3850 dc->u.s_number.number);
3853 /* Returns a template argument pack used in DC (any will do), or NULL. */
3855 static struct demangle_component *
3856 d_find_pack (struct d_print_info *dpi,
3857 const struct demangle_component *dc)
3859 struct demangle_component *a;
3865 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
3866 a = d_lookup_template_argument (dpi, dc);
3867 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3871 case DEMANGLE_COMPONENT_PACK_EXPANSION:
3874 case DEMANGLE_COMPONENT_LAMBDA:
3875 case DEMANGLE_COMPONENT_NAME:
3876 case DEMANGLE_COMPONENT_TAGGED_NAME:
3877 case DEMANGLE_COMPONENT_OPERATOR:
3878 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3879 case DEMANGLE_COMPONENT_SUB_STD:
3880 case DEMANGLE_COMPONENT_CHARACTER:
3881 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
3882 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
3885 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3886 return d_find_pack (dpi, dc->u.s_extended_operator.name);
3887 case DEMANGLE_COMPONENT_CTOR:
3888 return d_find_pack (dpi, dc->u.s_ctor.name);
3889 case DEMANGLE_COMPONENT_DTOR:
3890 return d_find_pack (dpi, dc->u.s_dtor.name);
3893 a = d_find_pack (dpi, d_left (dc));
3896 return d_find_pack (dpi, d_right (dc));
3900 /* Returns the length of the template argument pack DC. */
3903 d_pack_length (const struct demangle_component *dc)
3906 while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
3907 && d_left (dc) != NULL)
3915 /* DC is a component of a mangled expression. Print it, wrapped in parens
3919 d_print_subexpr (struct d_print_info *dpi, int options,
3920 const struct demangle_component *dc)
3923 if (dc->type == DEMANGLE_COMPONENT_NAME
3924 || dc->type == DEMANGLE_COMPONENT_QUAL_NAME
3925 || dc->type == DEMANGLE_COMPONENT_INITIALIZER_LIST
3926 || dc->type == DEMANGLE_COMPONENT_FUNCTION_PARAM)
3929 d_append_char (dpi, '(');
3930 d_print_comp (dpi, options, dc);
3932 d_append_char (dpi, ')');
3935 /* A demangle component and some scope captured when it was first
3938 struct d_saved_scope
3940 /* The component whose scope this is. Used as the key for the
3941 saved_scopes hashtable in d_print_info. May be NULL if this
3942 scope will not be inserted into that table. */
3943 const struct demangle_component *container;
3944 /* Nonzero if the below items are copies and require freeing
3945 when this scope is freed. */
3947 /* The list of templates, if any, that was current when this
3948 scope was captured. */
3949 struct d_print_template *templates;
3952 /* Allocate a scope and populate it with the current values from DPI.
3953 CONTAINER is the demangle component to which the scope refers, and
3954 is used as the key for the saved_scopes hashtable in d_print_info.
3955 CONTAINER may be NULL if this scope will not be inserted into that
3956 table. If COPY is nonzero then items that may have been allocated
3957 on the stack will be copied before storing. */
3959 static struct d_saved_scope *
3960 d_store_scope (const struct d_print_info *dpi,
3961 const struct demangle_component *container, int copy)
3963 struct d_saved_scope *scope = XNEW (struct d_saved_scope);
3965 scope->container = container;
3966 scope->is_copy = copy;
3970 struct d_print_template *ts, **tl = &scope->templates;
3972 for (ts = dpi->templates; ts != NULL; ts = ts->next)
3974 struct d_print_template *td = XNEW (struct d_print_template);
3978 td->template_decl = ts->template_decl;
3983 scope->templates = dpi->templates;
3988 /* Free a scope allocated by d_store_scope. */
3991 d_free_scope (void *p)
3993 struct d_saved_scope *scope = (struct d_saved_scope *) p;
3997 struct d_print_template *ts, *tn;
3999 for (ts = scope->templates; ts != NULL; ts = tn)
4009 /* Restore a stored scope to DPI, optionally freeing it afterwards. */
4012 d_restore_scope (struct d_print_info *dpi, struct d_saved_scope *scope,
4015 dpi->templates = scope->templates;
4018 d_free_scope (scope);
4021 /* Returns a hash code for the saved scope referenced by p. */
4024 d_hash_saved_scope (const void *p)
4026 const struct d_saved_scope *s = (const struct d_saved_scope *) p;
4028 return htab_hash_pointer (s->container);
4031 /* Returns non-zero if the saved scopes referenced by p1 and p2
4035 d_equal_saved_scope (const void *p1, const void *p2)
4037 const struct d_saved_scope *s1 = (const struct d_saved_scope *) p1;
4038 const struct d_saved_scope *s2 = (const struct d_saved_scope *) p2;
4040 return s1->container == s2->container;
4043 /* Subroutine to handle components. */
4046 d_print_comp (struct d_print_info *dpi, int options,
4047 const struct demangle_component *dc)
4049 /* Magic variable to let reference smashing skip over the next modifier
4050 without needing to modify *dc. */
4051 const struct demangle_component *mod_inner = NULL;
4053 /* Variable used to store the current scope while a previously
4054 captured scope is used. */
4055 struct d_saved_scope *saved_scope = NULL;
4059 d_print_error (dpi);
4062 if (d_print_saw_error (dpi))
4067 case DEMANGLE_COMPONENT_NAME:
4068 if ((options & DMGL_JAVA) == 0)
4069 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
4071 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
4074 case DEMANGLE_COMPONENT_TAGGED_NAME:
4075 d_print_comp (dpi, options, d_left (dc));
4076 d_append_string (dpi, "[abi:");
4077 d_print_comp (dpi, options, d_right (dc));
4078 d_append_char (dpi, ']');
4081 case DEMANGLE_COMPONENT_QUAL_NAME:
4082 case DEMANGLE_COMPONENT_LOCAL_NAME:
4083 d_print_comp (dpi, options, d_left (dc));
4084 if ((options & DMGL_JAVA) == 0)
4085 d_append_string (dpi, "::");
4087 d_append_char (dpi, '.');
4089 struct demangle_component *local_name = d_right (dc);
4090 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
4092 d_append_string (dpi, "{default arg#");
4093 d_append_num (dpi, local_name->u.s_unary_num.num + 1);
4094 d_append_string (dpi, "}::");
4095 local_name = local_name->u.s_unary_num.sub;
4097 d_print_comp (dpi, options, local_name);
4101 case DEMANGLE_COMPONENT_TYPED_NAME:
4103 struct d_print_mod *hold_modifiers;
4104 struct demangle_component *typed_name;
4105 struct d_print_mod adpm[4];
4107 struct d_print_template dpt;
4109 /* Pass the name down to the type so that it can be printed in
4110 the right place for the type. We also have to pass down
4111 any CV-qualifiers, which apply to the this parameter. */
4112 hold_modifiers = dpi->modifiers;
4115 typed_name = d_left (dc);
4116 while (typed_name != NULL)
4118 if (i >= sizeof adpm / sizeof adpm[0])
4120 d_print_error (dpi);
4124 adpm[i].next = dpi->modifiers;
4125 dpi->modifiers = &adpm[i];
4126 adpm[i].mod = typed_name;
4127 adpm[i].printed = 0;
4128 adpm[i].templates = dpi->templates;
4131 if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
4132 && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
4133 && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS
4134 && typed_name->type != DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
4135 && typed_name->type != DEMANGLE_COMPONENT_REFERENCE_THIS)
4138 typed_name = d_left (typed_name);
4141 if (typed_name == NULL)
4143 d_print_error (dpi);
4147 /* If typed_name is a template, then it applies to the
4148 function type as well. */
4149 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
4151 dpt.next = dpi->templates;
4152 dpi->templates = &dpt;
4153 dpt.template_decl = typed_name;
4156 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
4157 there may be CV-qualifiers on its right argument which
4158 really apply here; this happens when parsing a class which
4159 is local to a function. */
4160 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
4162 struct demangle_component *local_name;
4164 local_name = d_right (typed_name);
4165 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
4166 local_name = local_name->u.s_unary_num.sub;
4167 while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
4168 || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
4169 || local_name->type == DEMANGLE_COMPONENT_CONST_THIS
4170 || local_name->type == DEMANGLE_COMPONENT_REFERENCE_THIS
4171 || (local_name->type
4172 == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS))
4174 if (i >= sizeof adpm / sizeof adpm[0])
4176 d_print_error (dpi);
4180 adpm[i] = adpm[i - 1];
4181 adpm[i].next = &adpm[i - 1];
4182 dpi->modifiers = &adpm[i];
4184 adpm[i - 1].mod = local_name;
4185 adpm[i - 1].printed = 0;
4186 adpm[i - 1].templates = dpi->templates;
4189 local_name = d_left (local_name);
4193 d_print_comp (dpi, options, d_right (dc));
4195 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
4196 dpi->templates = dpt.next;
4198 /* If the modifiers didn't get printed by the type, print them
4203 if (! adpm[i].printed)
4205 d_append_char (dpi, ' ');
4206 d_print_mod (dpi, options, adpm[i].mod);
4210 dpi->modifiers = hold_modifiers;
4215 case DEMANGLE_COMPONENT_TEMPLATE:
4217 struct d_print_mod *hold_dpm;
4218 struct demangle_component *dcl;
4220 /* Don't push modifiers into a template definition. Doing so
4221 could give the wrong definition for a template argument.
4222 Instead, treat the template essentially as a name. */
4224 hold_dpm = dpi->modifiers;
4225 dpi->modifiers = NULL;
4229 if ((options & DMGL_JAVA) != 0
4230 && dcl->type == DEMANGLE_COMPONENT_NAME
4231 && dcl->u.s_name.len == 6
4232 && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
4234 /* Special-case Java arrays, so that JArray<TYPE> appears
4235 instead as TYPE[]. */
4237 d_print_comp (dpi, options, d_right (dc));
4238 d_append_string (dpi, "[]");
4242 d_print_comp (dpi, options, dcl);
4243 if (d_last_char (dpi) == '<')
4244 d_append_char (dpi, ' ');
4245 d_append_char (dpi, '<');
4246 d_print_comp (dpi, options, d_right (dc));
4247 /* Avoid generating two consecutive '>' characters, to avoid
4248 the C++ syntactic ambiguity. */
4249 if (d_last_char (dpi) == '>')
4250 d_append_char (dpi, ' ');
4251 d_append_char (dpi, '>');
4254 dpi->modifiers = hold_dpm;
4259 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
4261 struct d_print_template *hold_dpt;
4262 struct demangle_component *a = d_lookup_template_argument (dpi, dc);
4264 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4265 a = d_index_template_argument (a, dpi->pack_index);
4269 d_print_error (dpi);
4273 /* While processing this parameter, we need to pop the list of
4274 templates. This is because the template parameter may
4275 itself be a reference to a parameter of an outer
4278 hold_dpt = dpi->templates;
4279 dpi->templates = hold_dpt->next;
4281 d_print_comp (dpi, options, a);
4283 dpi->templates = hold_dpt;
4288 case DEMANGLE_COMPONENT_CTOR:
4289 d_print_comp (dpi, options, dc->u.s_ctor.name);
4292 case DEMANGLE_COMPONENT_DTOR:
4293 d_append_char (dpi, '~');
4294 d_print_comp (dpi, options, dc->u.s_dtor.name);
4297 case DEMANGLE_COMPONENT_VTABLE:
4298 d_append_string (dpi, "vtable for ");
4299 d_print_comp (dpi, options, d_left (dc));
4302 case DEMANGLE_COMPONENT_VTT:
4303 d_append_string (dpi, "VTT for ");
4304 d_print_comp (dpi, options, d_left (dc));
4307 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
4308 d_append_string (dpi, "construction vtable for ");
4309 d_print_comp (dpi, options, d_left (dc));
4310 d_append_string (dpi, "-in-");
4311 d_print_comp (dpi, options, d_right (dc));
4314 case DEMANGLE_COMPONENT_TYPEINFO:
4315 d_append_string (dpi, "typeinfo for ");
4316 d_print_comp (dpi, options, d_left (dc));
4319 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
4320 d_append_string (dpi, "typeinfo name for ");
4321 d_print_comp (dpi, options, d_left (dc));
4324 case DEMANGLE_COMPONENT_TYPEINFO_FN:
4325 d_append_string (dpi, "typeinfo fn for ");
4326 d_print_comp (dpi, options, d_left (dc));
4329 case DEMANGLE_COMPONENT_THUNK:
4330 d_append_string (dpi, "non-virtual thunk to ");
4331 d_print_comp (dpi, options, d_left (dc));
4334 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
4335 d_append_string (dpi, "virtual thunk to ");
4336 d_print_comp (dpi, options, d_left (dc));
4339 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
4340 d_append_string (dpi, "covariant return thunk to ");
4341 d_print_comp (dpi, options, d_left (dc));
4344 case DEMANGLE_COMPONENT_JAVA_CLASS:
4345 d_append_string (dpi, "java Class for ");
4346 d_print_comp (dpi, options, d_left (dc));
4349 case DEMANGLE_COMPONENT_GUARD:
4350 d_append_string (dpi, "guard variable for ");
4351 d_print_comp (dpi, options, d_left (dc));
4354 case DEMANGLE_COMPONENT_TLS_INIT:
4355 d_append_string (dpi, "TLS init function for ");
4356 d_print_comp (dpi, options, d_left (dc));
4359 case DEMANGLE_COMPONENT_TLS_WRAPPER:
4360 d_append_string (dpi, "TLS wrapper function for ");
4361 d_print_comp (dpi, options, d_left (dc));
4364 case DEMANGLE_COMPONENT_REFTEMP:
4365 d_append_string (dpi, "reference temporary #");
4366 d_print_comp (dpi, options, d_right (dc));
4367 d_append_string (dpi, " for ");
4368 d_print_comp (dpi, options, d_left (dc));
4371 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
4372 d_append_string (dpi, "hidden alias for ");
4373 d_print_comp (dpi, options, d_left (dc));
4376 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
4377 d_append_string (dpi, "transaction clone for ");
4378 d_print_comp (dpi, options, d_left (dc));
4381 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
4382 d_append_string (dpi, "non-transaction clone for ");
4383 d_print_comp (dpi, options, d_left (dc));
4386 case DEMANGLE_COMPONENT_SUB_STD:
4387 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
4390 case DEMANGLE_COMPONENT_RESTRICT:
4391 case DEMANGLE_COMPONENT_VOLATILE:
4392 case DEMANGLE_COMPONENT_CONST:
4394 struct d_print_mod *pdpm;
4396 /* When printing arrays, it's possible to have cases where the
4397 same CV-qualifier gets pushed on the stack multiple times.
4398 We only need to print it once. */
4400 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
4402 if (! pdpm->printed)
4404 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
4405 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
4406 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
4408 if (pdpm->mod->type == dc->type)
4410 d_print_comp (dpi, options, d_left (dc));
4418 case DEMANGLE_COMPONENT_REFERENCE:
4419 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4421 /* Handle reference smashing: & + && = &. */
4422 const struct demangle_component *sub = d_left (dc);
4423 if (sub->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
4425 struct demangle_component *a;
4426 struct d_saved_scope lookup;
4429 if (dpi->saved_scopes == NULL)
4430 dpi->saved_scopes = htab_create_alloc (1,
4432 d_equal_saved_scope,
4436 lookup.container = sub;
4437 slot = htab_find_slot (dpi->saved_scopes, &lookup, INSERT);
4438 if (*slot == HTAB_EMPTY_ENTRY)
4440 /* This is the first time SUB has been traversed.
4441 We need to capture some scope so it can be
4442 restored if SUB is reentered as a substitution. */
4443 *slot = d_store_scope (dpi, sub, 1);
4447 /* This traversal is reentering SUB as a substition.
4448 Restore the original scope temporarily. */
4449 saved_scope = d_store_scope (dpi, NULL, 0);
4450 d_restore_scope (dpi, (struct d_saved_scope *) *slot, 0);
4453 a = d_lookup_template_argument (dpi, sub);
4454 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4455 a = d_index_template_argument (a, dpi->pack_index);
4459 if (saved_scope != NULL)
4460 d_restore_scope (dpi, saved_scope, 1);
4462 d_print_error (dpi);
4469 if (sub->type == DEMANGLE_COMPONENT_REFERENCE
4470 || sub->type == dc->type)
4472 else if (sub->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE)
4473 mod_inner = d_left (sub);
4477 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4478 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4479 case DEMANGLE_COMPONENT_CONST_THIS:
4480 case DEMANGLE_COMPONENT_REFERENCE_THIS:
4481 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
4482 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4483 case DEMANGLE_COMPONENT_POINTER:
4484 case DEMANGLE_COMPONENT_COMPLEX:
4485 case DEMANGLE_COMPONENT_IMAGINARY:
4488 /* We keep a list of modifiers on the stack. */
4489 struct d_print_mod dpm;
4491 dpm.next = dpi->modifiers;
4492 dpi->modifiers = &dpm;
4495 dpm.templates = dpi->templates;
4498 mod_inner = d_left (dc);
4500 d_print_comp (dpi, options, mod_inner);
4502 /* If the modifier didn't get printed by the type, print it
4505 d_print_mod (dpi, options, dc);
4507 dpi->modifiers = dpm.next;
4509 if (saved_scope != NULL)
4510 d_restore_scope (dpi, saved_scope, 1);
4515 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
4516 if ((options & DMGL_JAVA) == 0)
4517 d_append_buffer (dpi, dc->u.s_builtin.type->name,
4518 dc->u.s_builtin.type->len);
4520 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
4521 dc->u.s_builtin.type->java_len);
4524 case DEMANGLE_COMPONENT_VENDOR_TYPE:
4525 d_print_comp (dpi, options, d_left (dc));
4528 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
4530 if ((options & DMGL_RET_POSTFIX) != 0)
4531 d_print_function_type (dpi,
4532 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4533 dc, dpi->modifiers);
4535 /* Print return type if present */
4536 if (d_left (dc) != NULL && (options & DMGL_RET_POSTFIX) != 0)
4537 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4539 else if (d_left (dc) != NULL && (options & DMGL_RET_DROP) == 0)
4541 struct d_print_mod dpm;
4543 /* We must pass this type down as a modifier in order to
4544 print it in the right location. */
4545 dpm.next = dpi->modifiers;
4546 dpi->modifiers = &dpm;
4549 dpm.templates = dpi->templates;
4551 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4554 dpi->modifiers = dpm.next;
4559 /* In standard prefix notation, there is a space between the
4560 return type and the function signature. */
4561 if ((options & DMGL_RET_POSTFIX) == 0)
4562 d_append_char (dpi, ' ');
4565 if ((options & DMGL_RET_POSTFIX) == 0)
4566 d_print_function_type (dpi,
4567 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4568 dc, dpi->modifiers);
4573 case DEMANGLE_COMPONENT_ARRAY_TYPE:
4575 struct d_print_mod *hold_modifiers;
4576 struct d_print_mod adpm[4];
4578 struct d_print_mod *pdpm;
4580 /* We must pass this type down as a modifier in order to print
4581 multi-dimensional arrays correctly. If the array itself is
4582 CV-qualified, we act as though the element type were
4583 CV-qualified. We do this by copying the modifiers down
4584 rather than fiddling pointers, so that we don't wind up
4585 with a d_print_mod higher on the stack pointing into our
4586 stack frame after we return. */
4588 hold_modifiers = dpi->modifiers;
4590 adpm[0].next = hold_modifiers;
4591 dpi->modifiers = &adpm[0];
4593 adpm[0].printed = 0;
4594 adpm[0].templates = dpi->templates;
4597 pdpm = hold_modifiers;
4599 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
4600 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
4601 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
4603 if (! pdpm->printed)
4605 if (i >= sizeof adpm / sizeof adpm[0])
4607 d_print_error (dpi);
4612 adpm[i].next = dpi->modifiers;
4613 dpi->modifiers = &adpm[i];
4621 d_print_comp (dpi, options, d_right (dc));
4623 dpi->modifiers = hold_modifiers;
4625 if (adpm[0].printed)
4631 d_print_mod (dpi, options, adpm[i].mod);
4634 d_print_array_type (dpi, options, dc, dpi->modifiers);
4639 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4640 case DEMANGLE_COMPONENT_VECTOR_TYPE:
4642 struct d_print_mod dpm;
4644 dpm.next = dpi->modifiers;
4645 dpi->modifiers = &dpm;
4648 dpm.templates = dpi->templates;
4650 d_print_comp (dpi, options, d_right (dc));
4652 /* If the modifier didn't get printed by the type, print it
4655 d_print_mod (dpi, options, dc);
4657 dpi->modifiers = dpm.next;
4662 case DEMANGLE_COMPONENT_FIXED_TYPE:
4663 if (dc->u.s_fixed.sat)
4664 d_append_string (dpi, "_Sat ");
4665 /* Don't print "int _Accum". */
4666 if (dc->u.s_fixed.length->u.s_builtin.type
4667 != &cplus_demangle_builtin_types['i'-'a'])
4669 d_print_comp (dpi, options, dc->u.s_fixed.length);
4670 d_append_char (dpi, ' ');
4672 if (dc->u.s_fixed.accum)
4673 d_append_string (dpi, "_Accum");
4675 d_append_string (dpi, "_Fract");
4678 case DEMANGLE_COMPONENT_ARGLIST:
4679 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
4680 if (d_left (dc) != NULL)
4681 d_print_comp (dpi, options, d_left (dc));
4682 if (d_right (dc) != NULL)
4685 unsigned long int flush_count;
4686 /* Make sure ", " isn't flushed by d_append_string, otherwise
4687 dpi->len -= 2 wouldn't work. */
4688 if (dpi->len >= sizeof (dpi->buf) - 2)
4689 d_print_flush (dpi);
4690 d_append_string (dpi, ", ");
4692 flush_count = dpi->flush_count;
4693 d_print_comp (dpi, options, d_right (dc));
4694 /* If that didn't print anything (which can happen with empty
4695 template argument packs), remove the comma and space. */
4696 if (dpi->flush_count == flush_count && dpi->len == len)
4701 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
4703 struct demangle_component *type = d_left (dc);
4704 struct demangle_component *list = d_right (dc);
4707 d_print_comp (dpi, options, type);
4708 d_append_char (dpi, '{');
4709 d_print_comp (dpi, options, list);
4710 d_append_char (dpi, '}');
4714 case DEMANGLE_COMPONENT_OPERATOR:
4716 const struct demangle_operator_info *op = dc->u.s_operator.op;
4719 d_append_string (dpi, "operator");
4720 /* Add a space before new/delete. */
4721 if (IS_LOWER (op->name[0]))
4722 d_append_char (dpi, ' ');
4723 /* Omit a trailing space. */
4724 if (op->name[len-1] == ' ')
4726 d_append_buffer (dpi, op->name, len);
4730 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
4731 d_append_string (dpi, "operator ");
4732 d_print_comp (dpi, options, dc->u.s_extended_operator.name);
4735 case DEMANGLE_COMPONENT_CAST:
4736 d_append_string (dpi, "operator ");
4737 d_print_cast (dpi, options, dc);
4740 case DEMANGLE_COMPONENT_NULLARY:
4741 d_print_expr_op (dpi, options, d_left (dc));
4744 case DEMANGLE_COMPONENT_UNARY:
4746 struct demangle_component *op = d_left (dc);
4747 struct demangle_component *operand = d_right (dc);
4748 const char *code = NULL;
4750 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
4752 code = op->u.s_operator.op->code;
4753 if (!strcmp (code, "ad"))
4755 /* Don't print the argument list for the address of a
4757 if (operand->type == DEMANGLE_COMPONENT_TYPED_NAME
4758 && d_left (operand)->type == DEMANGLE_COMPONENT_QUAL_NAME
4759 && d_right (operand)->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
4760 operand = d_left (operand);
4762 if (operand->type == DEMANGLE_COMPONENT_BINARY_ARGS)
4764 /* This indicates a suffix operator. */
4765 operand = d_left (operand);
4766 d_print_subexpr (dpi, options, operand);
4767 d_print_expr_op (dpi, options, op);
4772 if (op->type != DEMANGLE_COMPONENT_CAST)
4773 d_print_expr_op (dpi, options, op);
4776 d_append_char (dpi, '(');
4777 d_print_cast (dpi, options, op);
4778 d_append_char (dpi, ')');
4780 if (code && !strcmp (code, "gs"))
4781 /* Avoid parens after '::'. */
4782 d_print_comp (dpi, options, operand);
4783 else if (code && !strcmp (code, "st"))
4784 /* Always print parens for sizeof (type). */
4786 d_append_char (dpi, '(');
4787 d_print_comp (dpi, options, operand);
4788 d_append_char (dpi, ')');
4791 d_print_subexpr (dpi, options, operand);
4795 case DEMANGLE_COMPONENT_BINARY:
4796 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
4798 d_print_error (dpi);
4802 if (op_is_new_cast (d_left (dc)))
4804 d_print_expr_op (dpi, options, d_left (dc));
4805 d_append_char (dpi, '<');
4806 d_print_comp (dpi, options, d_left (d_right (dc)));
4807 d_append_string (dpi, ">(");
4808 d_print_comp (dpi, options, d_right (d_right (dc)));
4809 d_append_char (dpi, ')');
4813 /* We wrap an expression which uses the greater-than operator in
4814 an extra layer of parens so that it does not get confused
4815 with the '>' which ends the template parameters. */
4816 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
4817 && d_left (dc)->u.s_operator.op->len == 1
4818 && d_left (dc)->u.s_operator.op->name[0] == '>')
4819 d_append_char (dpi, '(');
4821 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") == 0
4822 && d_left (d_right (dc))->type == DEMANGLE_COMPONENT_TYPED_NAME)
4824 /* Function call used in an expression should not have printed types
4825 of the function arguments. Values of the function arguments still
4826 get printed below. */
4828 const struct demangle_component *func = d_left (d_right (dc));
4830 if (d_right (func)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
4831 d_print_error (dpi);
4832 d_print_subexpr (dpi, options, d_left (func));
4835 d_print_subexpr (dpi, options, d_left (d_right (dc)));
4836 if (strcmp (d_left (dc)->u.s_operator.op->code, "ix") == 0)
4838 d_append_char (dpi, '[');
4839 d_print_comp (dpi, options, d_right (d_right (dc)));
4840 d_append_char (dpi, ']');
4844 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") != 0)
4845 d_print_expr_op (dpi, options, d_left (dc));
4846 d_print_subexpr (dpi, options, d_right (d_right (dc)));
4849 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
4850 && d_left (dc)->u.s_operator.op->len == 1
4851 && d_left (dc)->u.s_operator.op->name[0] == '>')
4852 d_append_char (dpi, ')');
4856 case DEMANGLE_COMPONENT_BINARY_ARGS:
4857 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
4858 d_print_error (dpi);
4861 case DEMANGLE_COMPONENT_TRINARY:
4862 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
4863 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
4865 d_print_error (dpi);
4869 struct demangle_component *op = d_left (dc);
4870 struct demangle_component *first = d_left (d_right (dc));
4871 struct demangle_component *second = d_left (d_right (d_right (dc)));
4872 struct demangle_component *third = d_right (d_right (d_right (dc)));
4874 if (!strcmp (op->u.s_operator.op->code, "qu"))
4876 d_print_subexpr (dpi, options, first);
4877 d_print_expr_op (dpi, options, op);
4878 d_print_subexpr (dpi, options, second);
4879 d_append_string (dpi, " : ");
4880 d_print_subexpr (dpi, options, third);
4884 d_append_string (dpi, "new ");
4885 if (d_left (first) != NULL)
4887 d_print_subexpr (dpi, options, first);
4888 d_append_char (dpi, ' ');
4890 d_print_comp (dpi, options, second);
4892 d_print_subexpr (dpi, options, third);
4897 case DEMANGLE_COMPONENT_TRINARY_ARG1:
4898 case DEMANGLE_COMPONENT_TRINARY_ARG2:
4899 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
4900 d_print_error (dpi);
4903 case DEMANGLE_COMPONENT_LITERAL:
4904 case DEMANGLE_COMPONENT_LITERAL_NEG:
4906 enum d_builtin_type_print tp;
4908 /* For some builtin types, produce simpler output. */
4909 tp = D_PRINT_DEFAULT;
4910 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
4912 tp = d_left (dc)->u.s_builtin.type->print;
4916 case D_PRINT_UNSIGNED:
4918 case D_PRINT_UNSIGNED_LONG:
4919 case D_PRINT_LONG_LONG:
4920 case D_PRINT_UNSIGNED_LONG_LONG:
4921 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
4923 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
4924 d_append_char (dpi, '-');
4925 d_print_comp (dpi, options, d_right (dc));
4930 case D_PRINT_UNSIGNED:
4931 d_append_char (dpi, 'u');
4934 d_append_char (dpi, 'l');
4936 case D_PRINT_UNSIGNED_LONG:
4937 d_append_string (dpi, "ul");
4939 case D_PRINT_LONG_LONG:
4940 d_append_string (dpi, "ll");
4942 case D_PRINT_UNSIGNED_LONG_LONG:
4943 d_append_string (dpi, "ull");
4951 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
4952 && d_right (dc)->u.s_name.len == 1
4953 && dc->type == DEMANGLE_COMPONENT_LITERAL)
4955 switch (d_right (dc)->u.s_name.s[0])
4958 d_append_string (dpi, "false");
4961 d_append_string (dpi, "true");
4974 d_append_char (dpi, '(');
4975 d_print_comp (dpi, options, d_left (dc));
4976 d_append_char (dpi, ')');
4977 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
4978 d_append_char (dpi, '-');
4979 if (tp == D_PRINT_FLOAT)
4980 d_append_char (dpi, '[');
4981 d_print_comp (dpi, options, d_right (dc));
4982 if (tp == D_PRINT_FLOAT)
4983 d_append_char (dpi, ']');
4987 case DEMANGLE_COMPONENT_NUMBER:
4988 d_append_num (dpi, dc->u.s_number.number);
4991 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
4992 d_append_string (dpi, "java resource ");
4993 d_print_comp (dpi, options, d_left (dc));
4996 case DEMANGLE_COMPONENT_COMPOUND_NAME:
4997 d_print_comp (dpi, options, d_left (dc));
4998 d_print_comp (dpi, options, d_right (dc));
5001 case DEMANGLE_COMPONENT_CHARACTER:
5002 d_append_char (dpi, dc->u.s_character.character);
5005 case DEMANGLE_COMPONENT_DECLTYPE:
5006 d_append_string (dpi, "decltype (");
5007 d_print_comp (dpi, options, d_left (dc));
5008 d_append_char (dpi, ')');
5011 case DEMANGLE_COMPONENT_PACK_EXPANSION:
5015 struct demangle_component *a = d_find_pack (dpi, d_left (dc));
5018 /* d_find_pack won't find anything if the only packs involved
5019 in this expansion are function parameter packs; in that
5020 case, just print the pattern and "...". */
5021 d_print_subexpr (dpi, options, d_left (dc));
5022 d_append_string (dpi, "...");
5026 len = d_pack_length (a);
5028 for (i = 0; i < len; ++i)
5030 dpi->pack_index = i;
5031 d_print_comp (dpi, options, dc);
5033 d_append_string (dpi, ", ");
5038 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
5040 long num = dc->u.s_number.number;
5042 d_append_string (dpi, "this");
5045 d_append_string (dpi, "{parm#");
5046 d_append_num (dpi, num);
5047 d_append_char (dpi, '}');
5052 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
5053 d_append_string (dpi, "global constructors keyed to ");
5054 d_print_comp (dpi, options, dc->u.s_binary.left);
5057 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
5058 d_append_string (dpi, "global destructors keyed to ");
5059 d_print_comp (dpi, options, dc->u.s_binary.left);
5062 case DEMANGLE_COMPONENT_LAMBDA:
5063 d_append_string (dpi, "{lambda(");
5064 d_print_comp (dpi, options, dc->u.s_unary_num.sub);
5065 d_append_string (dpi, ")#");
5066 d_append_num (dpi, dc->u.s_unary_num.num + 1);
5067 d_append_char (dpi, '}');
5070 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
5071 d_append_string (dpi, "{unnamed type#");
5072 d_append_num (dpi, dc->u.s_number.number + 1);
5073 d_append_char (dpi, '}');
5076 case DEMANGLE_COMPONENT_CLONE:
5077 d_print_comp (dpi, options, d_left (dc));
5078 d_append_string (dpi, " [clone ");
5079 d_print_comp (dpi, options, d_right (dc));
5080 d_append_char (dpi, ']');
5084 d_print_error (dpi);
5089 /* Print a Java dentifier. For Java we try to handle encoded extended
5090 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
5091 so we don't it for C++. Characters are encoded as
5095 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
5101 for (p = name; p < end; ++p)
5112 for (q = p + 3; q < end; ++q)
5118 else if (*q >= 'A' && *q <= 'F')
5119 dig = *q - 'A' + 10;
5120 else if (*q >= 'a' && *q <= 'f')
5121 dig = *q - 'a' + 10;
5127 /* If the Unicode character is larger than 256, we don't try
5128 to deal with it here. FIXME. */
5129 if (q < end && *q == '_' && c < 256)
5131 d_append_char (dpi, c);
5137 d_append_char (dpi, *p);
5141 /* Print a list of modifiers. SUFFIX is 1 if we are printing
5142 qualifiers on this after printing a function. */
5145 d_print_mod_list (struct d_print_info *dpi, int options,
5146 struct d_print_mod *mods, int suffix)
5148 struct d_print_template *hold_dpt;
5150 if (mods == NULL || d_print_saw_error (dpi))
5155 && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
5156 || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
5157 || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS
5158 || mods->mod->type == DEMANGLE_COMPONENT_REFERENCE_THIS
5160 == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS))))
5162 d_print_mod_list (dpi, options, mods->next, suffix);
5168 hold_dpt = dpi->templates;
5169 dpi->templates = mods->templates;
5171 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
5173 d_print_function_type (dpi, options, mods->mod, mods->next);
5174 dpi->templates = hold_dpt;
5177 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
5179 d_print_array_type (dpi, options, mods->mod, mods->next);
5180 dpi->templates = hold_dpt;
5183 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
5185 struct d_print_mod *hold_modifiers;
5186 struct demangle_component *dc;
5188 /* When this is on the modifier stack, we have pulled any
5189 qualifiers off the right argument already. Otherwise, we
5190 print it as usual, but don't let the left argument see any
5193 hold_modifiers = dpi->modifiers;
5194 dpi->modifiers = NULL;
5195 d_print_comp (dpi, options, d_left (mods->mod));
5196 dpi->modifiers = hold_modifiers;
5198 if ((options & DMGL_JAVA) == 0)
5199 d_append_string (dpi, "::");
5201 d_append_char (dpi, '.');
5203 dc = d_right (mods->mod);
5205 if (dc->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
5207 d_append_string (dpi, "{default arg#");
5208 d_append_num (dpi, dc->u.s_unary_num.num + 1);
5209 d_append_string (dpi, "}::");
5210 dc = dc->u.s_unary_num.sub;
5213 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
5214 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
5215 || dc->type == DEMANGLE_COMPONENT_CONST_THIS
5216 || dc->type == DEMANGLE_COMPONENT_REFERENCE_THIS
5217 || dc->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS)
5220 d_print_comp (dpi, options, dc);
5222 dpi->templates = hold_dpt;
5226 d_print_mod (dpi, options, mods->mod);
5228 dpi->templates = hold_dpt;
5230 d_print_mod_list (dpi, options, mods->next, suffix);
5233 /* Print a modifier. */
5236 d_print_mod (struct d_print_info *dpi, int options,
5237 const struct demangle_component *mod)
5241 case DEMANGLE_COMPONENT_RESTRICT:
5242 case DEMANGLE_COMPONENT_RESTRICT_THIS:
5243 d_append_string (dpi, " restrict");
5245 case DEMANGLE_COMPONENT_VOLATILE:
5246 case DEMANGLE_COMPONENT_VOLATILE_THIS:
5247 d_append_string (dpi, " volatile");
5249 case DEMANGLE_COMPONENT_CONST:
5250 case DEMANGLE_COMPONENT_CONST_THIS:
5251 d_append_string (dpi, " const");
5253 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5254 d_append_char (dpi, ' ');
5255 d_print_comp (dpi, options, d_right (mod));
5257 case DEMANGLE_COMPONENT_POINTER:
5258 /* There is no pointer symbol in Java. */
5259 if ((options & DMGL_JAVA) == 0)
5260 d_append_char (dpi, '*');
5262 case DEMANGLE_COMPONENT_REFERENCE_THIS:
5263 /* For the ref-qualifier, put a space before the &. */
5264 d_append_char (dpi, ' ');
5265 case DEMANGLE_COMPONENT_REFERENCE:
5266 d_append_char (dpi, '&');
5268 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
5269 d_append_char (dpi, ' ');
5270 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
5271 d_append_string (dpi, "&&");
5273 case DEMANGLE_COMPONENT_COMPLEX:
5274 d_append_string (dpi, "complex ");
5276 case DEMANGLE_COMPONENT_IMAGINARY:
5277 d_append_string (dpi, "imaginary ");
5279 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5280 if (d_last_char (dpi) != '(')
5281 d_append_char (dpi, ' ');
5282 d_print_comp (dpi, options, d_left (mod));
5283 d_append_string (dpi, "::*");
5285 case DEMANGLE_COMPONENT_TYPED_NAME:
5286 d_print_comp (dpi, options, d_left (mod));
5288 case DEMANGLE_COMPONENT_VECTOR_TYPE:
5289 d_append_string (dpi, " __vector(");
5290 d_print_comp (dpi, options, d_left (mod));
5291 d_append_char (dpi, ')');
5295 /* Otherwise, we have something that won't go back on the
5296 modifier stack, so we can just print it. */
5297 d_print_comp (dpi, options, mod);
5302 /* Print a function type, except for the return type. */
5305 d_print_function_type (struct d_print_info *dpi, int options,
5306 const struct demangle_component *dc,
5307 struct d_print_mod *mods)
5311 struct d_print_mod *p;
5312 struct d_print_mod *hold_modifiers;
5316 for (p = mods; p != NULL; p = p->next)
5321 switch (p->mod->type)
5323 case DEMANGLE_COMPONENT_POINTER:
5324 case DEMANGLE_COMPONENT_REFERENCE:
5325 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
5328 case DEMANGLE_COMPONENT_RESTRICT:
5329 case DEMANGLE_COMPONENT_VOLATILE:
5330 case DEMANGLE_COMPONENT_CONST:
5331 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5332 case DEMANGLE_COMPONENT_COMPLEX:
5333 case DEMANGLE_COMPONENT_IMAGINARY:
5334 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5338 case DEMANGLE_COMPONENT_RESTRICT_THIS:
5339 case DEMANGLE_COMPONENT_VOLATILE_THIS:
5340 case DEMANGLE_COMPONENT_CONST_THIS:
5341 case DEMANGLE_COMPONENT_REFERENCE_THIS:
5342 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
5355 if (d_last_char (dpi) != '('
5356 && d_last_char (dpi) != '*')
5359 if (need_space && d_last_char (dpi) != ' ')
5360 d_append_char (dpi, ' ');
5361 d_append_char (dpi, '(');
5364 hold_modifiers = dpi->modifiers;
5365 dpi->modifiers = NULL;
5367 d_print_mod_list (dpi, options, mods, 0);
5370 d_append_char (dpi, ')');
5372 d_append_char (dpi, '(');
5374 if (d_right (dc) != NULL)
5375 d_print_comp (dpi, options, d_right (dc));
5377 d_append_char (dpi, ')');
5379 d_print_mod_list (dpi, options, mods, 1);
5381 dpi->modifiers = hold_modifiers;
5384 /* Print an array type, except for the element type. */
5387 d_print_array_type (struct d_print_info *dpi, int options,
5388 const struct demangle_component *dc,
5389 struct d_print_mod *mods)
5397 struct d_print_mod *p;
5400 for (p = mods; p != NULL; p = p->next)
5404 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
5419 d_append_string (dpi, " (");
5421 d_print_mod_list (dpi, options, mods, 0);
5424 d_append_char (dpi, ')');
5428 d_append_char (dpi, ' ');
5430 d_append_char (dpi, '[');
5432 if (d_left (dc) != NULL)
5433 d_print_comp (dpi, options, d_left (dc));
5435 d_append_char (dpi, ']');
5438 /* Print an operator in an expression. */
5441 d_print_expr_op (struct d_print_info *dpi, int options,
5442 const struct demangle_component *dc)
5444 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
5445 d_append_buffer (dpi, dc->u.s_operator.op->name,
5446 dc->u.s_operator.op->len);
5448 d_print_comp (dpi, options, dc);
5454 d_print_cast (struct d_print_info *dpi, int options,
5455 const struct demangle_component *dc)
5457 if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
5458 d_print_comp (dpi, options, d_left (dc));
5461 struct d_print_mod *hold_dpm;
5462 struct d_print_template dpt;
5464 /* It appears that for a templated cast operator, we need to put
5465 the template parameters in scope for the operator name, but
5466 not for the parameters. The effect is that we need to handle
5467 the template printing here. */
5469 hold_dpm = dpi->modifiers;
5470 dpi->modifiers = NULL;
5472 dpt.next = dpi->templates;
5473 dpi->templates = &dpt;
5474 dpt.template_decl = d_left (dc);
5476 d_print_comp (dpi, options, d_left (d_left (dc)));
5478 dpi->templates = dpt.next;
5480 if (d_last_char (dpi) == '<')
5481 d_append_char (dpi, ' ');
5482 d_append_char (dpi, '<');
5483 d_print_comp (dpi, options, d_right (d_left (dc)));
5484 /* Avoid generating two consecutive '>' characters, to avoid
5485 the C++ syntactic ambiguity. */
5486 if (d_last_char (dpi) == '>')
5487 d_append_char (dpi, ' ');
5488 d_append_char (dpi, '>');
5490 dpi->modifiers = hold_dpm;
5494 /* Initialize the information structure we use to pass around
5497 CP_STATIC_IF_GLIBCPP_V3
5499 cplus_demangle_init_info (const char *mangled, int options, size_t len,
5503 di->send = mangled + len;
5504 di->options = options;
5508 /* We can not need more components than twice the number of chars in
5509 the mangled string. Most components correspond directly to
5510 chars, but the ARGLIST types are exceptions. */
5511 di->num_comps = 2 * len;
5514 /* Similarly, we can not need more substitutions than there are
5515 chars in the mangled string. */
5520 di->last_name = NULL;
5525 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
5526 mangled name, return strings in repeated callback giving the demangled
5527 name. OPTIONS is the usual libiberty demangler options. On success,
5528 this returns 1. On failure, returns 0. */
5531 d_demangle_callback (const char *mangled, int options,
5532 demangle_callbackref callback, void *opaque)
5543 struct demangle_component *dc;
5546 if (mangled[0] == '_' && mangled[1] == 'Z')
5548 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
5549 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
5550 && (mangled[9] == 'D' || mangled[9] == 'I')
5551 && mangled[10] == '_')
5552 type = mangled[9] == 'I' ? DCT_GLOBAL_CTORS : DCT_GLOBAL_DTORS;
5555 if ((options & DMGL_TYPES) == 0)
5560 cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
5563 #ifdef CP_DYNAMIC_ARRAYS
5564 __extension__ struct demangle_component comps[di.num_comps];
5565 __extension__ struct demangle_component *subs[di.num_subs];
5570 di.comps = alloca (di.num_comps * sizeof (*di.comps));
5571 di.subs = alloca (di.num_subs * sizeof (*di.subs));
5577 dc = cplus_demangle_type (&di);
5580 dc = cplus_demangle_mangled_name (&di, 1);
5582 case DCT_GLOBAL_CTORS:
5583 case DCT_GLOBAL_DTORS:
5584 d_advance (&di, 11);
5585 dc = d_make_comp (&di,
5586 (type == DCT_GLOBAL_CTORS
5587 ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
5588 : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS),
5589 d_make_demangle_mangled_name (&di, d_str (&di)),
5591 d_advance (&di, strlen (d_str (&di)));
5595 /* If DMGL_PARAMS is set, then if we didn't consume the entire
5596 mangled string, then we didn't successfully demangle it. If
5597 DMGL_PARAMS is not set, we didn't look at the trailing
5599 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
5602 #ifdef CP_DEMANGLE_DEBUG
5606 status = (dc != NULL)
5607 ? cplus_demangle_print_callback (options, dc, callback, opaque)
5614 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
5615 name, return a buffer allocated with malloc holding the demangled
5616 name. OPTIONS is the usual libiberty demangler options. On
5617 success, this sets *PALC to the allocated size of the returned
5618 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
5619 a memory allocation failure, and returns NULL. */
5622 d_demangle (const char *mangled, int options, size_t *palc)
5624 struct d_growable_string dgs;
5627 d_growable_string_init (&dgs, 0);
5629 status = d_demangle_callback (mangled, options,
5630 d_growable_string_callback_adapter, &dgs);
5638 *palc = dgs.allocation_failure ? 1 : dgs.alc;
5642 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
5644 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
5646 /* ia64 ABI-mandated entry point in the C++ runtime library for
5647 performing demangling. MANGLED_NAME is a NUL-terminated character
5648 string containing the name to be demangled.
5650 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
5651 *LENGTH bytes, into which the demangled name is stored. If
5652 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
5653 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
5654 is placed in a region of memory allocated with malloc.
5656 If LENGTH is non-NULL, the length of the buffer containing the
5657 demangled name, is placed in *LENGTH.
5659 The return value is a pointer to the start of the NUL-terminated
5660 demangled name, or NULL if the demangling fails. The caller is
5661 responsible for deallocating this memory using free.
5663 *STATUS is set to one of the following values:
5664 0: The demangling operation succeeded.
5665 -1: A memory allocation failure occurred.
5666 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
5667 -3: One of the arguments is invalid.
5669 The demangling is performed using the C++ ABI mangling rules, with
5673 __cxa_demangle (const char *mangled_name, char *output_buffer,
5674 size_t *length, int *status)
5679 if (mangled_name == NULL)
5686 if (output_buffer != NULL && length == NULL)
5693 demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
5695 if (demangled == NULL)
5707 if (output_buffer == NULL)
5714 if (strlen (demangled) < *length)
5716 strcpy (output_buffer, demangled);
5718 demangled = output_buffer;
5722 free (output_buffer);
5733 extern int __gcclibcxx_demangle_callback (const char *,
5735 (const char *, size_t, void *),
5738 /* Alternative, allocationless entry point in the C++ runtime library
5739 for performing demangling. MANGLED_NAME is a NUL-terminated character
5740 string containing the name to be demangled.
5742 CALLBACK is a callback function, called with demangled string
5743 segments as demangling progresses; it is called at least once,
5744 but may be called more than once. OPAQUE is a generalized pointer
5745 used as a callback argument.
5747 The return code is one of the following values, equivalent to
5748 the STATUS values of __cxa_demangle() (excluding -1, since this
5749 function performs no memory allocations):
5750 0: The demangling operation succeeded.
5751 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
5752 -3: One of the arguments is invalid.
5754 The demangling is performed using the C++ ABI mangling rules, with
5758 __gcclibcxx_demangle_callback (const char *mangled_name,
5759 void (*callback) (const char *, size_t, void *),
5764 if (mangled_name == NULL || callback == NULL)
5767 status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES,
5775 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
5777 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
5778 mangled name, return a buffer allocated with malloc holding the
5779 demangled name. Otherwise, return NULL. */
5782 cplus_demangle_v3 (const char *mangled, int options)
5786 return d_demangle (mangled, options, &alc);
5790 cplus_demangle_v3_callback (const char *mangled, int options,
5791 demangle_callbackref callback, void *opaque)
5793 return d_demangle_callback (mangled, options, callback, opaque);
5796 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
5797 conventions, but the output formatting is a little different.
5798 This instructs the C++ demangler not to emit pointer characters ("*"), to
5799 use Java's namespace separator symbol ("." instead of "::"), and to output
5800 JArray<TYPE> as TYPE[]. */
5803 java_demangle_v3 (const char *mangled)
5807 return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
5811 java_demangle_v3_callback (const char *mangled,
5812 demangle_callbackref callback, void *opaque)
5814 return d_demangle_callback (mangled,
5815 DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
5819 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
5821 #ifndef IN_GLIBCPP_V3
5823 /* Demangle a string in order to find out whether it is a constructor
5824 or destructor. Return non-zero on success. Set *CTOR_KIND and
5825 *DTOR_KIND appropriately. */
5828 is_ctor_or_dtor (const char *mangled,
5829 enum gnu_v3_ctor_kinds *ctor_kind,
5830 enum gnu_v3_dtor_kinds *dtor_kind)
5833 struct demangle_component *dc;
5836 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
5837 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
5839 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
5842 #ifdef CP_DYNAMIC_ARRAYS
5843 __extension__ struct demangle_component comps[di.num_comps];
5844 __extension__ struct demangle_component *subs[di.num_subs];
5849 di.comps = alloca (di.num_comps * sizeof (*di.comps));
5850 di.subs = alloca (di.num_subs * sizeof (*di.subs));
5853 dc = cplus_demangle_mangled_name (&di, 1);
5855 /* Note that because we did not pass DMGL_PARAMS, we don't expect
5856 to demangle the entire string. */
5863 /* These cannot appear on a constructor or destructor. */
5864 case DEMANGLE_COMPONENT_RESTRICT_THIS:
5865 case DEMANGLE_COMPONENT_VOLATILE_THIS:
5866 case DEMANGLE_COMPONENT_CONST_THIS:
5867 case DEMANGLE_COMPONENT_REFERENCE_THIS:
5868 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
5872 case DEMANGLE_COMPONENT_TYPED_NAME:
5873 case DEMANGLE_COMPONENT_TEMPLATE:
5876 case DEMANGLE_COMPONENT_QUAL_NAME:
5877 case DEMANGLE_COMPONENT_LOCAL_NAME:
5880 case DEMANGLE_COMPONENT_CTOR:
5881 *ctor_kind = dc->u.s_ctor.kind;
5885 case DEMANGLE_COMPONENT_DTOR:
5886 *dtor_kind = dc->u.s_dtor.kind;
5897 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
5898 name. A non-zero return indicates the type of constructor. */
5900 enum gnu_v3_ctor_kinds
5901 is_gnu_v3_mangled_ctor (const char *name)
5903 enum gnu_v3_ctor_kinds ctor_kind;
5904 enum gnu_v3_dtor_kinds dtor_kind;
5906 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
5907 return (enum gnu_v3_ctor_kinds) 0;
5912 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
5913 name. A non-zero return indicates the type of destructor. */
5915 enum gnu_v3_dtor_kinds
5916 is_gnu_v3_mangled_dtor (const char *name)
5918 enum gnu_v3_ctor_kinds ctor_kind;
5919 enum gnu_v3_dtor_kinds dtor_kind;
5921 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
5922 return (enum gnu_v3_dtor_kinds) 0;
5926 #endif /* IN_GLIBCPP_V3 */
5928 #ifdef STANDALONE_DEMANGLER
5931 #include "dyn-string.h"
5933 static void print_usage (FILE* fp, int exit_value);
5935 #define IS_ALPHA(CHAR) \
5936 (((CHAR) >= 'a' && (CHAR) <= 'z') \
5937 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
5939 /* Non-zero if CHAR is a character than can occur in a mangled name. */
5940 #define is_mangled_char(CHAR) \
5941 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
5942 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
5944 /* The name of this program, as invoked. */
5945 const char* program_name;
5947 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
5950 print_usage (FILE* fp, int exit_value)
5952 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
5953 fprintf (fp, "Options:\n");
5954 fprintf (fp, " -h,--help Display this message.\n");
5955 fprintf (fp, " -p,--no-params Don't display function parameters\n");
5956 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
5957 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
5962 /* Option specification for getopt_long. */
5963 static const struct option long_options[] =
5965 { "help", no_argument, NULL, 'h' },
5966 { "no-params", no_argument, NULL, 'p' },
5967 { "verbose", no_argument, NULL, 'v' },
5968 { NULL, no_argument, NULL, 0 },
5971 /* Main entry for a demangling filter executable. It will demangle
5972 its command line arguments, if any. If none are provided, it will
5973 filter stdin to stdout, replacing any recognized mangled C++ names
5974 with their demangled equivalents. */
5977 main (int argc, char *argv[])
5981 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
5983 /* Use the program name of this program, as invoked. */
5984 program_name = argv[0];
5986 /* Parse options. */
5989 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
5992 case '?': /* Unrecognized option. */
5993 print_usage (stderr, 1);
5997 print_usage (stdout, 0);
6001 options &= ~ DMGL_PARAMS;
6005 options |= DMGL_VERBOSE;
6009 while (opt_char != -1);
6012 /* No command line arguments were provided. Filter stdin. */
6014 dyn_string_t mangled = dyn_string_new (3);
6017 /* Read all of input. */
6018 while (!feof (stdin))
6022 /* Pile characters into mangled until we hit one that can't
6023 occur in a mangled name. */
6025 while (!feof (stdin) && is_mangled_char (c))
6027 dyn_string_append_char (mangled, c);
6033 if (dyn_string_length (mangled) > 0)
6035 #ifdef IN_GLIBCPP_V3
6036 s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
6038 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
6048 /* It might not have been a mangled name. Print the
6050 fputs (dyn_string_buf (mangled), stdout);
6053 dyn_string_clear (mangled);
6056 /* If we haven't hit EOF yet, we've read one character that
6057 can't occur in a mangled name, so print it out. */
6062 dyn_string_delete (mangled);
6065 /* Demangle command line arguments. */
6067 /* Loop over command line arguments. */
6068 for (i = optind; i < argc; ++i)
6071 #ifdef IN_GLIBCPP_V3
6075 /* Attempt to demangle. */
6076 #ifdef IN_GLIBCPP_V3
6077 s = __cxa_demangle (argv[i], NULL, NULL, &status);
6079 s = cplus_demangle_v3 (argv[i], options);
6082 /* If it worked, print the demangled name. */
6090 #ifdef IN_GLIBCPP_V3
6091 fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
6093 fprintf (stderr, "Failed: %s\n", argv[i]);
6102 #endif /* STANDALONE_DEMANGLER */