1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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"
132 /* If IN_GLIBCPP_V3 is defined, some functions are made static. We
133 also rename them via #define to avoid compiler errors when the
134 static definition conflicts with the extern declaration in a header
138 #define CP_STATIC_IF_GLIBCPP_V3 static
140 #define cplus_demangle_fill_name d_fill_name
141 static int d_fill_name (struct demangle_component *, const char *, int);
143 #define cplus_demangle_fill_extended_operator d_fill_extended_operator
145 d_fill_extended_operator (struct demangle_component *, int,
146 struct demangle_component *);
148 #define cplus_demangle_fill_ctor d_fill_ctor
150 d_fill_ctor (struct demangle_component *, enum gnu_v3_ctor_kinds,
151 struct demangle_component *);
153 #define cplus_demangle_fill_dtor d_fill_dtor
155 d_fill_dtor (struct demangle_component *, enum gnu_v3_dtor_kinds,
156 struct demangle_component *);
158 #define cplus_demangle_mangled_name d_mangled_name
159 static struct demangle_component *d_mangled_name (struct d_info *, int);
161 #define cplus_demangle_type d_type
162 static struct demangle_component *d_type (struct d_info *);
164 #define cplus_demangle_print d_print
165 static char *d_print (int, const struct demangle_component *, int, size_t *);
167 #define cplus_demangle_print_callback d_print_callback
168 static int d_print_callback (int, const struct demangle_component *,
169 demangle_callbackref, void *);
171 #define cplus_demangle_init_info d_init_info
172 static void d_init_info (const char *, int, size_t, struct d_info *);
174 #else /* ! defined(IN_GLIBCPP_V3) */
175 #define CP_STATIC_IF_GLIBCPP_V3
176 #endif /* ! defined(IN_GLIBCPP_V3) */
178 /* See if the compiler supports dynamic arrays. */
181 #define CP_DYNAMIC_ARRAYS
184 #ifdef __STDC_VERSION__
185 #if __STDC_VERSION__ >= 199901L
186 #define CP_DYNAMIC_ARRAYS
187 #endif /* __STDC__VERSION >= 199901L */
188 #endif /* defined (__STDC_VERSION__) */
189 #endif /* defined (__STDC__) */
190 #endif /* ! defined (__GNUC__) */
192 /* We avoid pulling in the ctype tables, to prevent pulling in
193 additional unresolved symbols when this code is used in a library.
194 FIXME: Is this really a valid reason? This comes from the original
197 As of this writing this file has the following undefined references
198 when compiled with -DIN_GLIBCPP_V3: realloc, free, memcpy, strcpy,
201 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
202 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
203 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
205 /* The prefix prepended by GCC to an identifier represnting the
206 anonymous namespace. */
207 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
208 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
209 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
211 /* Information we keep for the standard substitutions. */
213 struct d_standard_sub_info
215 /* The code for this substitution. */
217 /* The simple string it expands to. */
218 const char *simple_expansion;
219 /* The length of the simple expansion. */
221 /* The results of a full, verbose, expansion. This is used when
222 qualifying a constructor/destructor, or when in verbose mode. */
223 const char *full_expansion;
224 /* The length of the full expansion. */
226 /* What to set the last_name field of d_info to; NULL if we should
227 not set it. This is only relevant when qualifying a
228 constructor/destructor. */
229 const char *set_last_name;
230 /* The length of set_last_name. */
231 int set_last_name_len;
234 /* Accessors for subtrees of struct demangle_component. */
236 #define d_left(dc) ((dc)->u.s_binary.left)
237 #define d_right(dc) ((dc)->u.s_binary.right)
239 /* A list of templates. This is used while printing. */
241 struct d_print_template
243 /* Next template on the list. */
244 struct d_print_template *next;
246 const struct demangle_component *template_decl;
249 /* A list of type modifiers. This is used while printing. */
253 /* Next modifier on the list. These are in the reverse of the order
254 in which they appeared in the mangled string. */
255 struct d_print_mod *next;
257 const struct demangle_component *mod;
258 /* Whether this modifier was printed. */
260 /* The list of templates which applies to this modifier. */
261 struct d_print_template *templates;
264 /* We use these structures to hold information during printing. */
266 struct d_growable_string
268 /* Buffer holding the result. */
270 /* Current length of data in buffer. */
272 /* Allocated size of buffer. */
274 /* Set to 1 if we had a memory allocation failure. */
275 int allocation_failure;
278 enum { D_PRINT_BUFFER_LENGTH = 256 };
281 /* The options passed to the demangler. */
283 /* Fixed-length allocated buffer for demangled data, flushed to the
284 callback with a NUL termination once full. */
285 char buf[D_PRINT_BUFFER_LENGTH];
286 /* Current length of data in buffer. */
288 /* The last character printed, saved individually so that it survives
291 /* Callback function to handle demangled buffer flush. */
292 demangle_callbackref callback;
293 /* Opaque callback argument. */
295 /* The current list of templates, if any. */
296 struct d_print_template *templates;
297 /* The current list of modifiers (e.g., pointer, reference, etc.),
299 struct d_print_mod *modifiers;
300 /* Set to 1 if we saw a demangling error. */
301 int demangle_failure;
302 /* The current index into any template argument packs we are using
305 /* Number of d_print_flush calls so far. */
306 unsigned long int flush_count;
309 #ifdef CP_DEMANGLE_DEBUG
310 static void d_dump (struct demangle_component *, int);
313 static struct demangle_component *
314 d_make_empty (struct d_info *);
316 static struct demangle_component *
317 d_make_comp (struct d_info *, enum demangle_component_type,
318 struct demangle_component *,
319 struct demangle_component *);
321 static struct demangle_component *
322 d_make_name (struct d_info *, const char *, int);
324 static struct demangle_component *
325 d_make_builtin_type (struct d_info *,
326 const struct demangle_builtin_type_info *);
328 static struct demangle_component *
329 d_make_operator (struct d_info *,
330 const struct demangle_operator_info *);
332 static struct demangle_component *
333 d_make_extended_operator (struct d_info *, int,
334 struct demangle_component *);
336 static struct demangle_component *
337 d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
338 struct demangle_component *);
340 static struct demangle_component *
341 d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
342 struct demangle_component *);
344 static struct demangle_component *
345 d_make_template_param (struct d_info *, long);
347 static struct demangle_component *
348 d_make_sub (struct d_info *, const char *, int);
351 has_return_type (struct demangle_component *);
354 is_ctor_dtor_or_conversion (struct demangle_component *);
356 static struct demangle_component *d_encoding (struct d_info *, int);
358 static struct demangle_component *d_name (struct d_info *);
360 static struct demangle_component *d_nested_name (struct d_info *);
362 static struct demangle_component *d_prefix (struct d_info *);
364 static struct demangle_component *d_unqualified_name (struct d_info *);
366 static struct demangle_component *d_source_name (struct d_info *);
368 static long d_number (struct d_info *);
370 static struct demangle_component *d_identifier (struct d_info *, int);
372 static struct demangle_component *d_operator_name (struct d_info *);
374 static struct demangle_component *d_special_name (struct d_info *);
376 static int d_call_offset (struct d_info *, int);
378 static struct demangle_component *d_ctor_dtor_name (struct d_info *);
380 static struct demangle_component **
381 d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
383 static struct demangle_component *
384 d_function_type (struct d_info *);
386 static struct demangle_component *
387 d_bare_function_type (struct d_info *, int);
389 static struct demangle_component *
390 d_class_enum_type (struct d_info *);
392 static struct demangle_component *d_array_type (struct d_info *);
394 static struct demangle_component *
395 d_pointer_to_member_type (struct d_info *);
397 static struct demangle_component *
398 d_template_param (struct d_info *);
400 static struct demangle_component *d_template_args (struct d_info *);
402 static struct demangle_component *
403 d_template_arg (struct d_info *);
405 static struct demangle_component *d_expression (struct d_info *);
407 static struct demangle_component *d_expr_primary (struct d_info *);
409 static struct demangle_component *d_local_name (struct d_info *);
411 static int d_discriminator (struct d_info *);
414 d_add_substitution (struct d_info *, struct demangle_component *);
416 static struct demangle_component *d_substitution (struct d_info *, int);
418 static void d_growable_string_init (struct d_growable_string *, size_t);
421 d_growable_string_resize (struct d_growable_string *, size_t);
424 d_growable_string_append_buffer (struct d_growable_string *,
425 const char *, size_t);
427 d_growable_string_callback_adapter (const char *, size_t, void *);
430 d_print_init (struct d_print_info *, int, demangle_callbackref, void *);
432 static inline void d_print_error (struct d_print_info *);
434 static inline int d_print_saw_error (struct d_print_info *);
436 static inline void d_print_flush (struct d_print_info *);
438 static inline void d_append_char (struct d_print_info *, char);
440 static inline void d_append_buffer (struct d_print_info *,
441 const char *, size_t);
443 static inline void d_append_string (struct d_print_info *, const char *);
445 static inline char d_last_char (struct d_print_info *);
448 d_print_comp (struct d_print_info *, const struct demangle_component *);
451 d_print_java_identifier (struct d_print_info *, const char *, int);
454 d_print_mod_list (struct d_print_info *, struct d_print_mod *, int);
457 d_print_mod (struct d_print_info *, const struct demangle_component *);
460 d_print_function_type (struct d_print_info *,
461 const struct demangle_component *,
462 struct d_print_mod *);
465 d_print_array_type (struct d_print_info *,
466 const struct demangle_component *,
467 struct d_print_mod *);
470 d_print_expr_op (struct d_print_info *, const struct demangle_component *);
473 d_print_cast (struct d_print_info *, const struct demangle_component *);
475 static int d_demangle_callback (const char *, int,
476 demangle_callbackref, void *);
477 static char *d_demangle (const char *, int, size_t *);
479 #ifdef CP_DEMANGLE_DEBUG
482 d_dump (struct demangle_component *dc, int indent)
489 printf ("failed demangling\n");
493 for (i = 0; i < indent; ++i)
498 case DEMANGLE_COMPONENT_NAME:
499 printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
501 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
502 printf ("template parameter %ld\n", dc->u.s_number.number);
504 case DEMANGLE_COMPONENT_CTOR:
505 printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
506 d_dump (dc->u.s_ctor.name, indent + 2);
508 case DEMANGLE_COMPONENT_DTOR:
509 printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
510 d_dump (dc->u.s_dtor.name, indent + 2);
512 case DEMANGLE_COMPONENT_SUB_STD:
513 printf ("standard substitution %s\n", dc->u.s_string.string);
515 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
516 printf ("builtin type %s\n", dc->u.s_builtin.type->name);
518 case DEMANGLE_COMPONENT_OPERATOR:
519 printf ("operator %s\n", dc->u.s_operator.op->name);
521 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
522 printf ("extended operator with %d args\n",
523 dc->u.s_extended_operator.args);
524 d_dump (dc->u.s_extended_operator.name, indent + 2);
527 case DEMANGLE_COMPONENT_QUAL_NAME:
528 printf ("qualified name\n");
530 case DEMANGLE_COMPONENT_LOCAL_NAME:
531 printf ("local name\n");
533 case DEMANGLE_COMPONENT_TYPED_NAME:
534 printf ("typed name\n");
536 case DEMANGLE_COMPONENT_TEMPLATE:
537 printf ("template\n");
539 case DEMANGLE_COMPONENT_VTABLE:
542 case DEMANGLE_COMPONENT_VTT:
545 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
546 printf ("construction vtable\n");
548 case DEMANGLE_COMPONENT_TYPEINFO:
549 printf ("typeinfo\n");
551 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
552 printf ("typeinfo name\n");
554 case DEMANGLE_COMPONENT_TYPEINFO_FN:
555 printf ("typeinfo function\n");
557 case DEMANGLE_COMPONENT_THUNK:
560 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
561 printf ("virtual thunk\n");
563 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
564 printf ("covariant thunk\n");
566 case DEMANGLE_COMPONENT_JAVA_CLASS:
567 printf ("java class\n");
569 case DEMANGLE_COMPONENT_GUARD:
572 case DEMANGLE_COMPONENT_REFTEMP:
573 printf ("reference temporary\n");
575 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
576 printf ("hidden alias\n");
578 case DEMANGLE_COMPONENT_RESTRICT:
579 printf ("restrict\n");
581 case DEMANGLE_COMPONENT_VOLATILE:
582 printf ("volatile\n");
584 case DEMANGLE_COMPONENT_CONST:
587 case DEMANGLE_COMPONENT_RESTRICT_THIS:
588 printf ("restrict this\n");
590 case DEMANGLE_COMPONENT_VOLATILE_THIS:
591 printf ("volatile this\n");
593 case DEMANGLE_COMPONENT_CONST_THIS:
594 printf ("const this\n");
596 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
597 printf ("vendor type qualifier\n");
599 case DEMANGLE_COMPONENT_POINTER:
600 printf ("pointer\n");
602 case DEMANGLE_COMPONENT_REFERENCE:
603 printf ("reference\n");
605 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
606 printf ("rvalue reference\n");
608 case DEMANGLE_COMPONENT_COMPLEX:
609 printf ("complex\n");
611 case DEMANGLE_COMPONENT_IMAGINARY:
612 printf ("imaginary\n");
614 case DEMANGLE_COMPONENT_VENDOR_TYPE:
615 printf ("vendor type\n");
617 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
618 printf ("function type\n");
620 case DEMANGLE_COMPONENT_ARRAY_TYPE:
621 printf ("array type\n");
623 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
624 printf ("pointer to member type\n");
626 case DEMANGLE_COMPONENT_FIXED_TYPE:
627 printf ("fixed-point type\n");
629 case DEMANGLE_COMPONENT_ARGLIST:
630 printf ("argument list\n");
632 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
633 printf ("template argument list\n");
635 case DEMANGLE_COMPONENT_CAST:
638 case DEMANGLE_COMPONENT_UNARY:
639 printf ("unary operator\n");
641 case DEMANGLE_COMPONENT_BINARY:
642 printf ("binary operator\n");
644 case DEMANGLE_COMPONENT_BINARY_ARGS:
645 printf ("binary operator arguments\n");
647 case DEMANGLE_COMPONENT_TRINARY:
648 printf ("trinary operator\n");
650 case DEMANGLE_COMPONENT_TRINARY_ARG1:
651 printf ("trinary operator arguments 1\n");
653 case DEMANGLE_COMPONENT_TRINARY_ARG2:
654 printf ("trinary operator arguments 1\n");
656 case DEMANGLE_COMPONENT_LITERAL:
657 printf ("literal\n");
659 case DEMANGLE_COMPONENT_LITERAL_NEG:
660 printf ("negative literal\n");
662 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
663 printf ("java resource\n");
665 case DEMANGLE_COMPONENT_COMPOUND_NAME:
666 printf ("compound name\n");
668 case DEMANGLE_COMPONENT_CHARACTER:
669 printf ("character '%c'\n", dc->u.s_character.character);
671 case DEMANGLE_COMPONENT_DECLTYPE:
672 printf ("decltype\n");
674 case DEMANGLE_COMPONENT_PACK_EXPANSION:
675 printf ("pack expansion\n");
679 d_dump (d_left (dc), indent + 2);
680 d_dump (d_right (dc), indent + 2);
683 #endif /* CP_DEMANGLE_DEBUG */
685 /* Fill in a DEMANGLE_COMPONENT_NAME. */
687 CP_STATIC_IF_GLIBCPP_V3
689 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
691 if (p == NULL || s == NULL || len == 0)
693 p->type = DEMANGLE_COMPONENT_NAME;
695 p->u.s_name.len = len;
699 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
701 CP_STATIC_IF_GLIBCPP_V3
703 cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
704 struct demangle_component *name)
706 if (p == NULL || args < 0 || name == NULL)
708 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
709 p->u.s_extended_operator.args = args;
710 p->u.s_extended_operator.name = name;
714 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
716 CP_STATIC_IF_GLIBCPP_V3
718 cplus_demangle_fill_ctor (struct demangle_component *p,
719 enum gnu_v3_ctor_kinds kind,
720 struct demangle_component *name)
724 || (kind < gnu_v3_complete_object_ctor
725 && kind > gnu_v3_complete_object_allocating_ctor))
727 p->type = DEMANGLE_COMPONENT_CTOR;
728 p->u.s_ctor.kind = kind;
729 p->u.s_ctor.name = name;
733 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
735 CP_STATIC_IF_GLIBCPP_V3
737 cplus_demangle_fill_dtor (struct demangle_component *p,
738 enum gnu_v3_dtor_kinds kind,
739 struct demangle_component *name)
743 || (kind < gnu_v3_deleting_dtor
744 && kind > gnu_v3_base_object_dtor))
746 p->type = DEMANGLE_COMPONENT_DTOR;
747 p->u.s_dtor.kind = kind;
748 p->u.s_dtor.name = name;
752 /* Add a new component. */
754 static struct demangle_component *
755 d_make_empty (struct d_info *di)
757 struct demangle_component *p;
759 if (di->next_comp >= di->num_comps)
761 p = &di->comps[di->next_comp];
766 /* Add a new generic component. */
768 static struct demangle_component *
769 d_make_comp (struct d_info *di, enum demangle_component_type type,
770 struct demangle_component *left,
771 struct demangle_component *right)
773 struct demangle_component *p;
775 /* We check for errors here. A typical error would be a NULL return
776 from a subroutine. We catch those here, and return NULL
780 /* These types require two parameters. */
781 case DEMANGLE_COMPONENT_QUAL_NAME:
782 case DEMANGLE_COMPONENT_LOCAL_NAME:
783 case DEMANGLE_COMPONENT_TYPED_NAME:
784 case DEMANGLE_COMPONENT_TEMPLATE:
785 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
786 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
787 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
788 case DEMANGLE_COMPONENT_UNARY:
789 case DEMANGLE_COMPONENT_BINARY:
790 case DEMANGLE_COMPONENT_BINARY_ARGS:
791 case DEMANGLE_COMPONENT_TRINARY:
792 case DEMANGLE_COMPONENT_TRINARY_ARG1:
793 case DEMANGLE_COMPONENT_TRINARY_ARG2:
794 case DEMANGLE_COMPONENT_LITERAL:
795 case DEMANGLE_COMPONENT_LITERAL_NEG:
796 case DEMANGLE_COMPONENT_COMPOUND_NAME:
797 if (left == NULL || right == NULL)
801 /* These types only require one parameter. */
802 case DEMANGLE_COMPONENT_VTABLE:
803 case DEMANGLE_COMPONENT_VTT:
804 case DEMANGLE_COMPONENT_TYPEINFO:
805 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
806 case DEMANGLE_COMPONENT_TYPEINFO_FN:
807 case DEMANGLE_COMPONENT_THUNK:
808 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
809 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
810 case DEMANGLE_COMPONENT_JAVA_CLASS:
811 case DEMANGLE_COMPONENT_GUARD:
812 case DEMANGLE_COMPONENT_REFTEMP:
813 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
814 case DEMANGLE_COMPONENT_POINTER:
815 case DEMANGLE_COMPONENT_REFERENCE:
816 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
817 case DEMANGLE_COMPONENT_COMPLEX:
818 case DEMANGLE_COMPONENT_IMAGINARY:
819 case DEMANGLE_COMPONENT_VENDOR_TYPE:
820 case DEMANGLE_COMPONENT_CAST:
821 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
822 case DEMANGLE_COMPONENT_DECLTYPE:
823 case DEMANGLE_COMPONENT_PACK_EXPANSION:
828 /* This needs a right parameter, but the left parameter can be
830 case DEMANGLE_COMPONENT_ARRAY_TYPE:
835 /* These are allowed to have no parameters--in some cases they
836 will be filled in later. */
837 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
838 case DEMANGLE_COMPONENT_RESTRICT:
839 case DEMANGLE_COMPONENT_VOLATILE:
840 case DEMANGLE_COMPONENT_CONST:
841 case DEMANGLE_COMPONENT_RESTRICT_THIS:
842 case DEMANGLE_COMPONENT_VOLATILE_THIS:
843 case DEMANGLE_COMPONENT_CONST_THIS:
844 case DEMANGLE_COMPONENT_ARGLIST:
845 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
848 /* Other types should not be seen here. */
853 p = d_make_empty (di);
857 p->u.s_binary.left = left;
858 p->u.s_binary.right = right;
863 /* Add a new name component. */
865 static struct demangle_component *
866 d_make_name (struct d_info *di, const char *s, int len)
868 struct demangle_component *p;
870 p = d_make_empty (di);
871 if (! cplus_demangle_fill_name (p, s, len))
876 /* Add a new builtin type component. */
878 static struct demangle_component *
879 d_make_builtin_type (struct d_info *di,
880 const struct demangle_builtin_type_info *type)
882 struct demangle_component *p;
886 p = d_make_empty (di);
889 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
890 p->u.s_builtin.type = type;
895 /* Add a new operator component. */
897 static struct demangle_component *
898 d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
900 struct demangle_component *p;
902 p = d_make_empty (di);
905 p->type = DEMANGLE_COMPONENT_OPERATOR;
906 p->u.s_operator.op = op;
911 /* Add a new extended operator component. */
913 static struct demangle_component *
914 d_make_extended_operator (struct d_info *di, int args,
915 struct demangle_component *name)
917 struct demangle_component *p;
919 p = d_make_empty (di);
920 if (! cplus_demangle_fill_extended_operator (p, args, name))
925 /* Add a new constructor component. */
927 static struct demangle_component *
928 d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
929 struct demangle_component *name)
931 struct demangle_component *p;
933 p = d_make_empty (di);
934 if (! cplus_demangle_fill_ctor (p, kind, name))
939 /* Add a new destructor component. */
941 static struct demangle_component *
942 d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
943 struct demangle_component *name)
945 struct demangle_component *p;
947 p = d_make_empty (di);
948 if (! cplus_demangle_fill_dtor (p, kind, name))
953 /* Add a new template parameter. */
955 static struct demangle_component *
956 d_make_template_param (struct d_info *di, long i)
958 struct demangle_component *p;
960 p = d_make_empty (di);
963 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
964 p->u.s_number.number = i;
969 /* Add a new function parameter. */
971 static struct demangle_component *
972 d_make_function_param (struct d_info *di, long i)
974 struct demangle_component *p;
976 p = d_make_empty (di);
979 p->type = DEMANGLE_COMPONENT_FUNCTION_PARAM;
980 p->u.s_number.number = i;
985 /* Add a new standard substitution component. */
987 static struct demangle_component *
988 d_make_sub (struct d_info *di, const char *name, int len)
990 struct demangle_component *p;
992 p = d_make_empty (di);
995 p->type = DEMANGLE_COMPONENT_SUB_STD;
996 p->u.s_string.string = name;
997 p->u.s_string.len = len;
1002 /* <mangled-name> ::= _Z <encoding>
1004 TOP_LEVEL is non-zero when called at the top level. */
1006 CP_STATIC_IF_GLIBCPP_V3
1007 struct demangle_component *
1008 cplus_demangle_mangled_name (struct d_info *di, int top_level)
1010 if (! d_check_char (di, '_')
1011 /* Allow missing _ if not at toplevel to work around a
1012 bug in G++ abi-version=2 mangling; see the comment in
1013 write_template_arg. */
1016 if (! d_check_char (di, 'Z'))
1018 return d_encoding (di, top_level);
1021 /* Return whether a function should have a return type. The argument
1022 is the function name, which may be qualified in various ways. The
1023 rules are that template functions have return types with some
1024 exceptions, function types which are not part of a function name
1025 mangling have return types with some exceptions, and non-template
1026 function names do not have return types. The exceptions are that
1027 constructors, destructors, and conversion operators do not have
1031 has_return_type (struct demangle_component *dc)
1039 case DEMANGLE_COMPONENT_TEMPLATE:
1040 return ! is_ctor_dtor_or_conversion (d_left (dc));
1041 case DEMANGLE_COMPONENT_RESTRICT_THIS:
1042 case DEMANGLE_COMPONENT_VOLATILE_THIS:
1043 case DEMANGLE_COMPONENT_CONST_THIS:
1044 return has_return_type (d_left (dc));
1048 /* Return whether a name is a constructor, a destructor, or a
1049 conversion operator. */
1052 is_ctor_dtor_or_conversion (struct demangle_component *dc)
1060 case DEMANGLE_COMPONENT_QUAL_NAME:
1061 case DEMANGLE_COMPONENT_LOCAL_NAME:
1062 return is_ctor_dtor_or_conversion (d_right (dc));
1063 case DEMANGLE_COMPONENT_CTOR:
1064 case DEMANGLE_COMPONENT_DTOR:
1065 case DEMANGLE_COMPONENT_CAST:
1070 /* <encoding> ::= <(function) name> <bare-function-type>
1074 TOP_LEVEL is non-zero when called at the top level, in which case
1075 if DMGL_PARAMS is not set we do not demangle the function
1076 parameters. We only set this at the top level, because otherwise
1077 we would not correctly demangle names in local scopes. */
1079 static struct demangle_component *
1080 d_encoding (struct d_info *di, int top_level)
1082 char peek = d_peek_char (di);
1084 if (peek == 'G' || peek == 'T')
1085 return d_special_name (di);
1088 struct demangle_component *dc;
1092 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
1094 /* Strip off any initial CV-qualifiers, as they really apply
1095 to the `this' parameter, and they were not output by the
1096 v2 demangler without DMGL_PARAMS. */
1097 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1098 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1099 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
1102 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1103 there may be CV-qualifiers on its right argument which
1104 really apply here; this happens when parsing a class
1105 which is local to a function. */
1106 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1108 struct demangle_component *dcr;
1111 while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1112 || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1113 || dcr->type == DEMANGLE_COMPONENT_CONST_THIS)
1115 dc->u.s_binary.right = dcr;
1121 peek = d_peek_char (di);
1122 if (dc == NULL || peek == '\0' || peek == 'E')
1124 return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
1125 d_bare_function_type (di, has_return_type (dc)));
1129 /* <name> ::= <nested-name>
1131 ::= <unscoped-template-name> <template-args>
1134 <unscoped-name> ::= <unqualified-name>
1135 ::= St <unqualified-name>
1137 <unscoped-template-name> ::= <unscoped-name>
1141 static struct demangle_component *
1142 d_name (struct d_info *di)
1144 char peek = d_peek_char (di);
1145 struct demangle_component *dc;
1150 return d_nested_name (di);
1153 return d_local_name (di);
1156 return d_unqualified_name (di);
1162 if (d_peek_next_char (di) != 't')
1164 dc = d_substitution (di, 0);
1170 dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
1171 d_make_name (di, "std", 3),
1172 d_unqualified_name (di));
1177 if (d_peek_char (di) != 'I')
1179 /* The grammar does not permit this case to occur if we
1180 called d_substitution() above (i.e., subst == 1). We
1181 don't bother to check. */
1185 /* This is <template-args>, which means that we just saw
1186 <unscoped-template-name>, which is a substitution
1187 candidate if we didn't just get it from a
1191 if (! d_add_substitution (di, dc))
1194 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1195 d_template_args (di));
1202 dc = d_unqualified_name (di);
1203 if (d_peek_char (di) == 'I')
1205 /* This is <template-args>, which means that we just saw
1206 <unscoped-template-name>, which is a substitution
1208 if (! d_add_substitution (di, dc))
1210 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1211 d_template_args (di));
1217 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1218 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1221 static struct demangle_component *
1222 d_nested_name (struct d_info *di)
1224 struct demangle_component *ret;
1225 struct demangle_component **pret;
1227 if (! d_check_char (di, 'N'))
1230 pret = d_cv_qualifiers (di, &ret, 1);
1234 *pret = d_prefix (di);
1238 if (! d_check_char (di, 'E'))
1244 /* <prefix> ::= <prefix> <unqualified-name>
1245 ::= <template-prefix> <template-args>
1246 ::= <template-param>
1250 <template-prefix> ::= <prefix> <(template) unqualified-name>
1251 ::= <template-param>
1255 static struct demangle_component *
1256 d_prefix (struct d_info *di)
1258 struct demangle_component *ret = NULL;
1263 enum demangle_component_type comb_type;
1264 struct demangle_component *dc;
1266 peek = d_peek_char (di);
1270 /* The older code accepts a <local-name> here, but I don't see
1271 that in the grammar. The older code does not accept a
1272 <template-param> here. */
1274 comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
1280 dc = d_unqualified_name (di);
1281 else if (peek == 'S')
1282 dc = d_substitution (di, 1);
1283 else if (peek == 'I')
1287 comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1288 dc = d_template_args (di);
1290 else if (peek == 'T')
1291 dc = d_template_param (di);
1292 else if (peek == 'E')
1300 ret = d_make_comp (di, comb_type, ret, dc);
1302 if (peek != 'S' && d_peek_char (di) != 'E')
1304 if (! d_add_substitution (di, ret))
1310 /* <unqualified-name> ::= <operator-name>
1311 ::= <ctor-dtor-name>
1313 ::= <local-source-name>
1315 <local-source-name> ::= L <source-name> <discriminator>
1318 static struct demangle_component *
1319 d_unqualified_name (struct d_info *di)
1323 peek = d_peek_char (di);
1324 if (IS_DIGIT (peek))
1325 return d_source_name (di);
1326 else if (IS_LOWER (peek))
1328 struct demangle_component *ret;
1330 ret = d_operator_name (di);
1331 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1332 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1335 else if (peek == 'C' || peek == 'D')
1336 return d_ctor_dtor_name (di);
1337 else if (peek == 'L')
1339 struct demangle_component * ret;
1343 ret = d_source_name (di);
1346 if (! d_discriminator (di))
1354 /* <source-name> ::= <(positive length) number> <identifier> */
1356 static struct demangle_component *
1357 d_source_name (struct d_info *di)
1360 struct demangle_component *ret;
1362 len = d_number (di);
1365 ret = d_identifier (di, len);
1366 di->last_name = ret;
1370 /* number ::= [n] <(non-negative decimal integer)> */
1373 d_number (struct d_info *di)
1380 peek = d_peek_char (di);
1385 peek = d_peek_char (di);
1391 if (! IS_DIGIT (peek))
1397 ret = ret * 10 + peek - '0';
1399 peek = d_peek_char (di);
1403 /* identifier ::= <(unqualified source code identifier)> */
1405 static struct demangle_component *
1406 d_identifier (struct d_info *di, int len)
1412 if (di->send - name < len)
1415 d_advance (di, len);
1417 /* A Java mangled name may have a trailing '$' if it is a C++
1418 keyword. This '$' is not included in the length count. We just
1420 if ((di->options & DMGL_JAVA) != 0
1421 && d_peek_char (di) == '$')
1424 /* Look for something which looks like a gcc encoding of an
1425 anonymous namespace, and replace it with a more user friendly
1427 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1428 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1429 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1433 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1434 if ((*s == '.' || *s == '_' || *s == '$')
1437 di->expansion -= len - sizeof "(anonymous namespace)";
1438 return d_make_name (di, "(anonymous namespace)",
1439 sizeof "(anonymous namespace)" - 1);
1443 return d_make_name (di, name, len);
1446 /* operator_name ::= many different two character encodings.
1448 ::= v <digit> <source-name>
1451 #define NL(s) s, (sizeof s) - 1
1453 CP_STATIC_IF_GLIBCPP_V3
1454 const struct demangle_operator_info cplus_demangle_operators[] =
1456 { "aN", NL ("&="), 2 },
1457 { "aS", NL ("="), 2 },
1458 { "aa", NL ("&&"), 2 },
1459 { "ad", NL ("&"), 1 },
1460 { "an", NL ("&"), 2 },
1461 { "cl", NL ("()"), 2 },
1462 { "cm", NL (","), 2 },
1463 { "co", NL ("~"), 1 },
1464 { "dV", NL ("/="), 2 },
1465 { "da", NL ("delete[]"), 1 },
1466 { "de", NL ("*"), 1 },
1467 { "dl", NL ("delete"), 1 },
1468 { "dt", NL ("."), 2 },
1469 { "dv", NL ("/"), 2 },
1470 { "eO", NL ("^="), 2 },
1471 { "eo", NL ("^"), 2 },
1472 { "eq", NL ("=="), 2 },
1473 { "ge", NL (">="), 2 },
1474 { "gt", NL (">"), 2 },
1475 { "ix", NL ("[]"), 2 },
1476 { "lS", NL ("<<="), 2 },
1477 { "le", NL ("<="), 2 },
1478 { "ls", NL ("<<"), 2 },
1479 { "lt", NL ("<"), 2 },
1480 { "mI", NL ("-="), 2 },
1481 { "mL", NL ("*="), 2 },
1482 { "mi", NL ("-"), 2 },
1483 { "ml", NL ("*"), 2 },
1484 { "mm", NL ("--"), 1 },
1485 { "na", NL ("new[]"), 1 },
1486 { "ne", NL ("!="), 2 },
1487 { "ng", NL ("-"), 1 },
1488 { "nt", NL ("!"), 1 },
1489 { "nw", NL ("new"), 1 },
1490 { "oR", NL ("|="), 2 },
1491 { "oo", NL ("||"), 2 },
1492 { "or", NL ("|"), 2 },
1493 { "pL", NL ("+="), 2 },
1494 { "pl", NL ("+"), 2 },
1495 { "pm", NL ("->*"), 2 },
1496 { "pp", NL ("++"), 1 },
1497 { "ps", NL ("+"), 1 },
1498 { "pt", NL ("->"), 2 },
1499 { "qu", NL ("?"), 3 },
1500 { "rM", NL ("%="), 2 },
1501 { "rS", NL (">>="), 2 },
1502 { "rm", NL ("%"), 2 },
1503 { "rs", NL (">>"), 2 },
1504 { "st", NL ("sizeof "), 1 },
1505 { "sz", NL ("sizeof "), 1 },
1506 { "at", NL ("alignof "), 1 },
1507 { "az", NL ("alignof "), 1 },
1508 { NULL, NULL, 0, 0 }
1511 static struct demangle_component *
1512 d_operator_name (struct d_info *di)
1517 c1 = d_next_char (di);
1518 c2 = d_next_char (di);
1519 if (c1 == 'v' && IS_DIGIT (c2))
1520 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1521 else if (c1 == 'c' && c2 == 'v')
1522 return d_make_comp (di, DEMANGLE_COMPONENT_CAST,
1523 cplus_demangle_type (di), NULL);
1526 /* LOW is the inclusive lower bound. */
1528 /* HIGH is the exclusive upper bound. We subtract one to ignore
1529 the sentinel at the end of the array. */
1530 int high = ((sizeof (cplus_demangle_operators)
1531 / sizeof (cplus_demangle_operators[0]))
1537 const struct demangle_operator_info *p;
1539 i = low + (high - low) / 2;
1540 p = cplus_demangle_operators + i;
1542 if (c1 == p->code[0] && c2 == p->code[1])
1543 return d_make_operator (di, p);
1545 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1555 static struct demangle_component *
1556 d_make_character (struct d_info *di, int c)
1558 struct demangle_component *p;
1559 p = d_make_empty (di);
1562 p->type = DEMANGLE_COMPONENT_CHARACTER;
1563 p->u.s_character.character = c;
1568 static struct demangle_component *
1569 d_java_resource (struct d_info *di)
1571 struct demangle_component *p = NULL;
1572 struct demangle_component *next = NULL;
1577 len = d_number (di);
1581 /* Eat the leading '_'. */
1582 if (d_next_char (di) != '_')
1595 /* Each chunk is either a '$' escape... */
1613 next = d_make_character (di, c);
1621 /* ... or a sequence of characters. */
1624 while (i < len && str[i] && str[i] != '$')
1627 next = d_make_name (di, str, i);
1640 p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next);
1646 p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL);
1651 /* <special-name> ::= TV <type>
1655 ::= GV <(object) name>
1656 ::= T <call-offset> <(base) encoding>
1657 ::= Tc <call-offset> <call-offset> <(base) encoding>
1658 Also g++ extensions:
1659 ::= TC <type> <(offset) number> _ <(base) type>
1664 ::= Gr <resource name>
1667 static struct demangle_component *
1668 d_special_name (struct d_info *di)
1670 di->expansion += 20;
1671 if (d_check_char (di, 'T'))
1673 switch (d_next_char (di))
1677 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
1678 cplus_demangle_type (di), NULL);
1680 di->expansion -= 10;
1681 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
1682 cplus_demangle_type (di), NULL);
1684 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
1685 cplus_demangle_type (di), NULL);
1687 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
1688 cplus_demangle_type (di), NULL);
1691 if (! d_call_offset (di, 'h'))
1693 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
1694 d_encoding (di, 0), NULL);
1697 if (! d_call_offset (di, 'v'))
1699 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
1700 d_encoding (di, 0), NULL);
1703 if (! d_call_offset (di, '\0'))
1705 if (! d_call_offset (di, '\0'))
1707 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
1708 d_encoding (di, 0), NULL);
1712 struct demangle_component *derived_type;
1714 struct demangle_component *base_type;
1716 derived_type = cplus_demangle_type (di);
1717 offset = d_number (di);
1720 if (! d_check_char (di, '_'))
1722 base_type = cplus_demangle_type (di);
1723 /* We don't display the offset. FIXME: We should display
1724 it in verbose mode. */
1726 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
1727 base_type, derived_type);
1731 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
1732 cplus_demangle_type (di), NULL);
1734 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
1735 cplus_demangle_type (di), NULL);
1741 else if (d_check_char (di, 'G'))
1743 switch (d_next_char (di))
1746 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
1749 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, d_name (di),
1753 return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
1754 d_encoding (di, 0), NULL);
1757 return d_java_resource (di);
1767 /* <call-offset> ::= h <nv-offset> _
1770 <nv-offset> ::= <(offset) number>
1772 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1774 The C parameter, if not '\0', is a character we just read which is
1775 the start of the <call-offset>.
1777 We don't display the offset information anywhere. FIXME: We should
1778 display it in verbose mode. */
1781 d_call_offset (struct d_info *di, int c)
1784 c = d_next_char (di);
1791 if (! d_check_char (di, '_'))
1798 if (! d_check_char (di, '_'))
1804 /* <ctor-dtor-name> ::= C1
1812 static struct demangle_component *
1813 d_ctor_dtor_name (struct d_info *di)
1815 if (di->last_name != NULL)
1817 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
1818 di->expansion += di->last_name->u.s_name.len;
1819 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
1820 di->expansion += di->last_name->u.s_string.len;
1822 switch (d_peek_char (di))
1826 enum gnu_v3_ctor_kinds kind;
1828 switch (d_peek_next_char (di))
1831 kind = gnu_v3_complete_object_ctor;
1834 kind = gnu_v3_base_object_ctor;
1837 kind = gnu_v3_complete_object_allocating_ctor;
1843 return d_make_ctor (di, kind, di->last_name);
1848 enum gnu_v3_dtor_kinds kind;
1850 switch (d_peek_next_char (di))
1853 kind = gnu_v3_deleting_dtor;
1856 kind = gnu_v3_complete_object_dtor;
1859 kind = gnu_v3_base_object_dtor;
1865 return d_make_dtor (di, kind, di->last_name);
1873 /* <type> ::= <builtin-type>
1875 ::= <class-enum-type>
1877 ::= <pointer-to-member-type>
1878 ::= <template-param>
1879 ::= <template-template-param> <template-args>
1881 ::= <CV-qualifiers> <type>
1884 ::= O <type> (C++0x)
1887 ::= U <source-name> <type>
1889 <builtin-type> ::= various one letter codes
1893 CP_STATIC_IF_GLIBCPP_V3
1894 const struct demangle_builtin_type_info
1895 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
1897 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
1898 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
1899 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
1900 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
1901 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
1902 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
1903 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
1904 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
1905 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
1906 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
1907 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1908 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
1909 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
1910 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
1911 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
1913 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1914 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1915 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1916 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
1917 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
1918 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1919 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
1920 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
1921 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
1922 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
1923 D_PRINT_UNSIGNED_LONG_LONG },
1924 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
1925 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT },
1926 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT },
1927 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT },
1928 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT },
1929 /* 30 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT },
1930 /* 31 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT },
1933 CP_STATIC_IF_GLIBCPP_V3
1934 struct demangle_component *
1935 cplus_demangle_type (struct d_info *di)
1938 struct demangle_component *ret;
1941 /* The ABI specifies that when CV-qualifiers are used, the base type
1942 is substitutable, and the fully qualified type is substitutable,
1943 but the base type with a strict subset of the CV-qualifiers is
1944 not substitutable. The natural recursive implementation of the
1945 CV-qualifiers would cause subsets to be substitutable, so instead
1946 we pull them all off now.
1948 FIXME: The ABI says that order-insensitive vendor qualifiers
1949 should be handled in the same way, but we have no way to tell
1950 which vendor qualifiers are order-insensitive and which are
1951 order-sensitive. So we just assume that they are all
1952 order-sensitive. g++ 3.4 supports only one vendor qualifier,
1953 __vector, and it treats it as order-sensitive when mangling
1956 peek = d_peek_char (di);
1957 if (peek == 'r' || peek == 'V' || peek == 'K')
1959 struct demangle_component **pret;
1961 pret = d_cv_qualifiers (di, &ret, 0);
1964 *pret = cplus_demangle_type (di);
1965 if (! *pret || ! d_add_substitution (di, ret))
1974 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1975 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
1976 case 'o': case 's': case 't':
1977 case 'v': case 'w': case 'x': case 'y': case 'z':
1978 ret = d_make_builtin_type (di,
1979 &cplus_demangle_builtin_types[peek - 'a']);
1980 di->expansion += ret->u.s_builtin.type->len;
1987 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
1988 d_source_name (di), NULL);
1992 ret = d_function_type (di);
1995 case '0': case '1': case '2': case '3': case '4':
1996 case '5': case '6': case '7': case '8': case '9':
1999 ret = d_class_enum_type (di);
2003 ret = d_array_type (di);
2007 ret = d_pointer_to_member_type (di);
2011 ret = d_template_param (di);
2012 if (d_peek_char (di) == 'I')
2014 /* This is <template-template-param> <template-args>. The
2015 <template-template-param> part is a substitution
2017 if (! d_add_substitution (di, ret))
2019 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2020 d_template_args (di));
2025 /* If this is a special substitution, then it is the start of
2026 <class-enum-type>. */
2030 peek_next = d_peek_next_char (di);
2031 if (IS_DIGIT (peek_next)
2033 || IS_UPPER (peek_next))
2035 ret = d_substitution (di, 0);
2036 /* The substituted name may have been a template name and
2037 may be followed by tepmlate args. */
2038 if (d_peek_char (di) == 'I')
2039 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2040 d_template_args (di));
2046 ret = d_class_enum_type (di);
2047 /* If the substitution was a complete type, then it is not
2048 a new substitution candidate. However, if the
2049 substitution was followed by template arguments, then
2050 the whole thing is a substitution candidate. */
2051 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
2059 ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE,
2060 cplus_demangle_type (di), NULL);
2065 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
2066 cplus_demangle_type (di), NULL);
2071 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
2072 cplus_demangle_type (di), NULL);
2077 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
2078 cplus_demangle_type (di), NULL);
2083 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
2084 cplus_demangle_type (di), NULL);
2089 ret = d_source_name (di);
2090 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
2091 cplus_demangle_type (di), ret);
2097 peek = d_next_char (di);
2102 /* decltype (expression) */
2103 ret = d_make_comp (di, DEMANGLE_COMPONENT_DECLTYPE,
2104 d_expression (di), NULL);
2105 if (ret && d_next_char (di) != 'E')
2110 /* Pack expansion. */
2111 ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2112 cplus_demangle_type (di), NULL);
2116 /* 32-bit decimal floating point */
2117 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[26]);
2118 di->expansion += ret->u.s_builtin.type->len;
2122 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[27]);
2123 di->expansion += ret->u.s_builtin.type->len;
2127 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[28]);
2128 di->expansion += ret->u.s_builtin.type->len;
2131 /* 16-bit half-precision FP */
2132 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[29]);
2133 di->expansion += ret->u.s_builtin.type->len;
2137 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[30]);
2138 di->expansion += ret->u.s_builtin.type->len;
2142 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[31]);
2143 di->expansion += ret->u.s_builtin.type->len;
2147 /* Fixed point types. DF<int bits><length><fract bits><sat> */
2148 ret = d_make_empty (di);
2149 ret->type = DEMANGLE_COMPONENT_FIXED_TYPE;
2150 if ((ret->u.s_fixed.accum = IS_DIGIT (d_peek_char (di))))
2151 /* For demangling we don't care about the bits. */
2153 ret->u.s_fixed.length = cplus_demangle_type (di);
2154 if (ret->u.s_fixed.length == NULL)
2157 peek = d_next_char (di);
2158 ret->u.s_fixed.sat = (peek == 's');
2172 if (! d_add_substitution (di, ret))
2179 /* <CV-qualifiers> ::= [r] [V] [K] */
2181 static struct demangle_component **
2182 d_cv_qualifiers (struct d_info *di,
2183 struct demangle_component **pret, int member_fn)
2187 peek = d_peek_char (di);
2188 while (peek == 'r' || peek == 'V' || peek == 'K')
2190 enum demangle_component_type t;
2196 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2197 : DEMANGLE_COMPONENT_RESTRICT);
2198 di->expansion += sizeof "restrict";
2200 else if (peek == 'V')
2203 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2204 : DEMANGLE_COMPONENT_VOLATILE);
2205 di->expansion += sizeof "volatile";
2210 ? DEMANGLE_COMPONENT_CONST_THIS
2211 : DEMANGLE_COMPONENT_CONST);
2212 di->expansion += sizeof "const";
2215 *pret = d_make_comp (di, t, NULL, NULL);
2218 pret = &d_left (*pret);
2220 peek = d_peek_char (di);
2226 /* <function-type> ::= F [Y] <bare-function-type> E */
2228 static struct demangle_component *
2229 d_function_type (struct d_info *di)
2231 struct demangle_component *ret;
2233 if (! d_check_char (di, 'F'))
2235 if (d_peek_char (di) == 'Y')
2237 /* Function has C linkage. We don't print this information.
2238 FIXME: We should print it in verbose mode. */
2241 ret = d_bare_function_type (di, 1);
2242 if (! d_check_char (di, 'E'))
2247 /* <bare-function-type> ::= [J]<type>+ */
2249 static struct demangle_component *
2250 d_bare_function_type (struct d_info *di, int has_return_type)
2252 struct demangle_component *return_type;
2253 struct demangle_component *tl;
2254 struct demangle_component **ptl;
2257 /* Detect special qualifier indicating that the first argument
2258 is the return type. */
2259 peek = d_peek_char (di);
2263 has_return_type = 1;
2271 struct demangle_component *type;
2273 peek = d_peek_char (di);
2274 if (peek == '\0' || peek == 'E')
2276 type = cplus_demangle_type (di);
2279 if (has_return_type)
2282 has_return_type = 0;
2286 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
2289 ptl = &d_right (*ptl);
2293 /* There should be at least one parameter type besides the optional
2294 return type. A function which takes no arguments will have a
2295 single parameter type void. */
2299 /* If we have a single parameter type void, omit it. */
2300 if (d_right (tl) == NULL
2301 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2302 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
2304 di->expansion -= d_left (tl)->u.s_builtin.type->len;
2308 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE, return_type, tl);
2311 /* <class-enum-type> ::= <name> */
2313 static struct demangle_component *
2314 d_class_enum_type (struct d_info *di)
2319 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2320 ::= A [<(dimension) expression>] _ <(element) type>
2323 static struct demangle_component *
2324 d_array_type (struct d_info *di)
2327 struct demangle_component *dim;
2329 if (! d_check_char (di, 'A'))
2332 peek = d_peek_char (di);
2335 else if (IS_DIGIT (peek))
2343 peek = d_peek_char (di);
2345 while (IS_DIGIT (peek));
2346 dim = d_make_name (di, s, d_str (di) - s);
2352 dim = d_expression (di);
2357 if (! d_check_char (di, '_'))
2360 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2361 cplus_demangle_type (di));
2364 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2366 static struct demangle_component *
2367 d_pointer_to_member_type (struct d_info *di)
2369 struct demangle_component *cl;
2370 struct demangle_component *mem;
2371 struct demangle_component **pmem;
2373 if (! d_check_char (di, 'M'))
2376 cl = cplus_demangle_type (di);
2378 /* The ABI specifies that any type can be a substitution source, and
2379 that M is followed by two types, and that when a CV-qualified
2380 type is seen both the base type and the CV-qualified types are
2381 substitution sources. The ABI also specifies that for a pointer
2382 to a CV-qualified member function, the qualifiers are attached to
2383 the second type. Given the grammar, a plain reading of the ABI
2384 suggests that both the CV-qualified member function and the
2385 non-qualified member function are substitution sources. However,
2386 g++ does not work that way. g++ treats only the CV-qualified
2387 member function as a substitution source. FIXME. So to work
2388 with g++, we need to pull off the CV-qualifiers here, in order to
2389 avoid calling add_substitution() in cplus_demangle_type(). But
2390 for a CV-qualified member which is not a function, g++ does
2391 follow the ABI, so we need to handle that case here by calling
2392 d_add_substitution ourselves. */
2394 pmem = d_cv_qualifiers (di, &mem, 1);
2397 *pmem = cplus_demangle_type (di);
2401 if (pmem != &mem && (*pmem)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
2403 if (! d_add_substitution (di, mem))
2407 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
2410 /* <template-param> ::= T_
2411 ::= T <(parameter-2 non-negative) number> _
2414 static struct demangle_component *
2415 d_template_param (struct d_info *di)
2419 if (! d_check_char (di, 'T'))
2422 if (d_peek_char (di) == '_')
2426 param = d_number (di);
2432 if (! d_check_char (di, '_'))
2437 return d_make_template_param (di, param);
2440 /* <template-args> ::= I <template-arg>+ E */
2442 static struct demangle_component *
2443 d_template_args (struct d_info *di)
2445 struct demangle_component *hold_last_name;
2446 struct demangle_component *al;
2447 struct demangle_component **pal;
2449 /* Preserve the last name we saw--don't let the template arguments
2450 clobber it, as that would give us the wrong name for a subsequent
2451 constructor or destructor. */
2452 hold_last_name = di->last_name;
2454 if (! d_check_char (di, 'I'))
2457 if (d_peek_char (di) == 'E')
2459 /* An argument pack can be empty. */
2461 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, NULL, NULL);
2468 struct demangle_component *a;
2470 a = d_template_arg (di);
2474 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
2477 pal = &d_right (*pal);
2479 if (d_peek_char (di) == 'E')
2486 di->last_name = hold_last_name;
2491 /* <template-arg> ::= <type>
2492 ::= X <expression> E
2496 static struct demangle_component *
2497 d_template_arg (struct d_info *di)
2499 struct demangle_component *ret;
2501 switch (d_peek_char (di))
2505 ret = d_expression (di);
2506 if (! d_check_char (di, 'E'))
2511 return d_expr_primary (di);
2514 /* An argument pack. */
2515 return d_template_args (di);
2518 return cplus_demangle_type (di);
2522 /* Subroutine of <expression> ::= cl <expression>+ E */
2524 static struct demangle_component *
2525 d_exprlist (struct d_info *di)
2527 struct demangle_component *list = NULL;
2528 struct demangle_component **p = &list;
2530 if (d_peek_char (di) == 'E')
2533 return d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, NULL, NULL);
2538 struct demangle_component *arg = d_expression (di);
2542 *p = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, arg, NULL);
2547 if (d_peek_char (di) == 'E')
2557 /* <expression> ::= <(unary) operator-name> <expression>
2558 ::= <(binary) operator-name> <expression> <expression>
2559 ::= <(trinary) operator-name> <expression> <expression> <expression>
2560 ::= cl <expression>+ E
2562 ::= <template-param>
2563 ::= sr <type> <unqualified-name>
2564 ::= sr <type> <unqualified-name> <template-args>
2568 static struct demangle_component *
2569 d_expression (struct d_info *di)
2573 peek = d_peek_char (di);
2575 return d_expr_primary (di);
2576 else if (peek == 'T')
2577 return d_template_param (di);
2578 else if (peek == 's' && d_peek_next_char (di) == 'r')
2580 struct demangle_component *type;
2581 struct demangle_component *name;
2584 type = cplus_demangle_type (di);
2585 name = d_unqualified_name (di);
2586 if (d_peek_char (di) != 'I')
2587 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
2589 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
2590 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2591 d_template_args (di)));
2593 else if (peek == 's' && d_peek_next_char (di) == 'p')
2596 return d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2597 d_expression (di), NULL);
2599 else if (peek == 'f' && d_peek_next_char (di) == 'p')
2601 /* Function parameter used in a late-specified return type. */
2604 if (d_peek_char (di) == '_')
2608 index = d_number (di);
2614 if (! d_check_char (di, '_'))
2617 return d_make_function_param (di, index);
2619 else if (IS_DIGIT (peek))
2621 /* We can get an unqualified name as an expression in the case of
2622 a dependent member access, i.e. decltype(T().i). */
2623 struct demangle_component *name = d_unqualified_name (di);
2626 if (d_peek_char (di) == 'I')
2627 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2628 d_template_args (di));
2634 struct demangle_component *op;
2637 op = d_operator_name (di);
2641 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
2642 di->expansion += op->u.s_operator.op->len - 2;
2644 if (op->type == DEMANGLE_COMPONENT_OPERATOR
2645 && strcmp (op->u.s_operator.op->code, "st") == 0)
2646 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2647 cplus_demangle_type (di));
2653 case DEMANGLE_COMPONENT_OPERATOR:
2654 args = op->u.s_operator.op->args;
2656 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
2657 args = op->u.s_extended_operator.args;
2659 case DEMANGLE_COMPONENT_CAST:
2668 struct demangle_component *operand;
2669 if (op->type == DEMANGLE_COMPONENT_CAST
2670 && d_check_char (di, '_'))
2671 operand = d_exprlist (di);
2673 operand = d_expression (di);
2674 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2679 struct demangle_component *left;
2680 struct demangle_component *right;
2682 left = d_expression (di);
2683 if (!strcmp (op->u.s_operator.op->code, "cl"))
2684 right = d_exprlist (di);
2686 right = d_expression (di);
2688 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
2690 DEMANGLE_COMPONENT_BINARY_ARGS,
2695 struct demangle_component *first;
2696 struct demangle_component *second;
2698 first = d_expression (di);
2699 second = d_expression (di);
2700 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
2702 DEMANGLE_COMPONENT_TRINARY_ARG1,
2705 DEMANGLE_COMPONENT_TRINARY_ARG2,
2707 d_expression (di))));
2715 /* <expr-primary> ::= L <type> <(value) number> E
2716 ::= L <type> <(value) float> E
2717 ::= L <mangled-name> E
2720 static struct demangle_component *
2721 d_expr_primary (struct d_info *di)
2723 struct demangle_component *ret;
2725 if (! d_check_char (di, 'L'))
2727 if (d_peek_char (di) == '_'
2728 /* Workaround for G++ bug; see comment in write_template_arg. */
2729 || d_peek_char (di) == 'Z')
2730 ret = cplus_demangle_mangled_name (di, 0);
2733 struct demangle_component *type;
2734 enum demangle_component_type t;
2737 type = cplus_demangle_type (di);
2741 /* If we have a type we know how to print, we aren't going to
2742 print the type name itself. */
2743 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2744 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
2745 di->expansion -= type->u.s_builtin.type->len;
2747 /* Rather than try to interpret the literal value, we just
2748 collect it as a string. Note that it's possible to have a
2749 floating point literal here. The ABI specifies that the
2750 format of such literals is machine independent. That's fine,
2751 but what's not fine is that versions of g++ up to 3.2 with
2752 -fabi-version=1 used upper case letters in the hex constant,
2753 and dumped out gcc's internal representation. That makes it
2754 hard to tell where the constant ends, and hard to dump the
2755 constant in any readable form anyhow. We don't attempt to
2756 handle these cases. */
2758 t = DEMANGLE_COMPONENT_LITERAL;
2759 if (d_peek_char (di) == 'n')
2761 t = DEMANGLE_COMPONENT_LITERAL_NEG;
2765 while (d_peek_char (di) != 'E')
2767 if (d_peek_char (di) == '\0')
2771 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
2773 if (! d_check_char (di, 'E'))
2778 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2779 ::= Z <(function) encoding> E s [<discriminator>]
2782 static struct demangle_component *
2783 d_local_name (struct d_info *di)
2785 struct demangle_component *function;
2787 if (! d_check_char (di, 'Z'))
2790 function = d_encoding (di, 0);
2792 if (! d_check_char (di, 'E'))
2795 if (d_peek_char (di) == 's')
2798 if (! d_discriminator (di))
2800 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
2801 d_make_name (di, "string literal",
2802 sizeof "string literal" - 1));
2806 struct demangle_component *name;
2809 if (! d_discriminator (di))
2811 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
2815 /* <discriminator> ::= _ <(non-negative) number>
2817 We demangle the discriminator, but we don't print it out. FIXME:
2818 We should print it out in verbose mode. */
2821 d_discriminator (struct d_info *di)
2825 if (d_peek_char (di) != '_')
2828 discrim = d_number (di);
2834 /* Add a new substitution. */
2837 d_add_substitution (struct d_info *di, struct demangle_component *dc)
2841 if (di->next_sub >= di->num_subs)
2843 di->subs[di->next_sub] = dc;
2848 /* <substitution> ::= S <seq-id> _
2858 If PREFIX is non-zero, then this type is being used as a prefix in
2859 a qualified name. In this case, for the standard substitutions, we
2860 need to check whether we are being used as a prefix for a
2861 constructor or destructor, and return a full template name.
2862 Otherwise we will get something like std::iostream::~iostream()
2863 which does not correspond particularly well to any function which
2864 actually appears in the source.
2867 static const struct d_standard_sub_info standard_subs[] =
2872 { 'a', NL ("std::allocator"),
2873 NL ("std::allocator"),
2875 { 'b', NL ("std::basic_string"),
2876 NL ("std::basic_string"),
2877 NL ("basic_string") },
2878 { 's', NL ("std::string"),
2879 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
2880 NL ("basic_string") },
2881 { 'i', NL ("std::istream"),
2882 NL ("std::basic_istream<char, std::char_traits<char> >"),
2883 NL ("basic_istream") },
2884 { 'o', NL ("std::ostream"),
2885 NL ("std::basic_ostream<char, std::char_traits<char> >"),
2886 NL ("basic_ostream") },
2887 { 'd', NL ("std::iostream"),
2888 NL ("std::basic_iostream<char, std::char_traits<char> >"),
2889 NL ("basic_iostream") }
2892 static struct demangle_component *
2893 d_substitution (struct d_info *di, int prefix)
2897 if (! d_check_char (di, 'S'))
2900 c = d_next_char (di);
2901 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
2910 unsigned int new_id;
2913 new_id = id * 36 + c - '0';
2914 else if (IS_UPPER (c))
2915 new_id = id * 36 + c - 'A' + 10;
2921 c = d_next_char (di);
2928 if (id >= (unsigned int) di->next_sub)
2933 return di->subs[id];
2938 const struct d_standard_sub_info *p;
2939 const struct d_standard_sub_info *pend;
2941 verbose = (di->options & DMGL_VERBOSE) != 0;
2942 if (! verbose && prefix)
2946 peek = d_peek_char (di);
2947 if (peek == 'C' || peek == 'D')
2951 pend = (&standard_subs[0]
2952 + sizeof standard_subs / sizeof standard_subs[0]);
2953 for (p = &standard_subs[0]; p < pend; ++p)
2960 if (p->set_last_name != NULL)
2961 di->last_name = d_make_sub (di, p->set_last_name,
2962 p->set_last_name_len);
2965 s = p->full_expansion;
2970 s = p->simple_expansion;
2971 len = p->simple_len;
2973 di->expansion += len;
2974 return d_make_sub (di, s, len);
2982 /* Initialize a growable string. */
2985 d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
2990 dgs->allocation_failure = 0;
2993 d_growable_string_resize (dgs, estimate);
2996 /* Grow a growable string to a given size. */
2999 d_growable_string_resize (struct d_growable_string *dgs, size_t need)
3004 if (dgs->allocation_failure)
3007 /* Start allocation at two bytes to avoid any possibility of confusion
3008 with the special value of 1 used as a return in *palc to indicate
3009 allocation failures. */
3010 newalc = dgs->alc > 0 ? dgs->alc : 2;
3011 while (newalc < need)
3014 newbuf = (char *) realloc (dgs->buf, newalc);
3021 dgs->allocation_failure = 1;
3028 /* Append a buffer to a growable string. */
3031 d_growable_string_append_buffer (struct d_growable_string *dgs,
3032 const char *s, size_t l)
3036 need = dgs->len + l + 1;
3037 if (need > dgs->alc)
3038 d_growable_string_resize (dgs, need);
3040 if (dgs->allocation_failure)
3043 memcpy (dgs->buf + dgs->len, s, l);
3044 dgs->buf[dgs->len + l] = '\0';
3048 /* Bridge growable strings to the callback mechanism. */
3051 d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
3053 struct d_growable_string *dgs = (struct d_growable_string*) opaque;
3055 d_growable_string_append_buffer (dgs, s, l);
3058 /* Initialize a print information structure. */
3061 d_print_init (struct d_print_info *dpi, int options,
3062 demangle_callbackref callback, void *opaque)
3064 dpi->options = options;
3066 dpi->last_char = '\0';
3067 dpi->templates = NULL;
3068 dpi->modifiers = NULL;
3069 dpi->flush_count = 0;
3071 dpi->callback = callback;
3072 dpi->opaque = opaque;
3074 dpi->demangle_failure = 0;
3077 /* Indicate that an error occurred during printing, and test for error. */
3080 d_print_error (struct d_print_info *dpi)
3082 dpi->demangle_failure = 1;
3086 d_print_saw_error (struct d_print_info *dpi)
3088 return dpi->demangle_failure != 0;
3091 /* Flush buffered characters to the callback. */
3094 d_print_flush (struct d_print_info *dpi)
3096 dpi->buf[dpi->len] = '\0';
3097 dpi->callback (dpi->buf, dpi->len, dpi->opaque);
3102 /* Append characters and buffers for printing. */
3105 d_append_char (struct d_print_info *dpi, char c)
3107 if (dpi->len == sizeof (dpi->buf) - 1)
3108 d_print_flush (dpi);
3110 dpi->buf[dpi->len++] = c;
3115 d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
3119 for (i = 0; i < l; i++)
3120 d_append_char (dpi, s[i]);
3124 d_append_string (struct d_print_info *dpi, const char *s)
3126 d_append_buffer (dpi, s, strlen (s));
3130 d_last_char (struct d_print_info *dpi)
3132 return dpi->last_char;
3135 /* Turn components into a human readable string. OPTIONS is the
3136 options bits passed to the demangler. DC is the tree to print.
3137 CALLBACK is a function to call to flush demangled string segments
3138 as they fill the intermediate buffer, and OPAQUE is a generalized
3139 callback argument. On success, this returns 1. On failure,
3140 it returns 0, indicating a bad parse. It does not use heap
3141 memory to build an output string, so cannot encounter memory
3142 allocation failure. */
3144 CP_STATIC_IF_GLIBCPP_V3
3146 cplus_demangle_print_callback (int options,
3147 const struct demangle_component *dc,
3148 demangle_callbackref callback, void *opaque)
3150 struct d_print_info dpi;
3152 d_print_init (&dpi, options, callback, opaque);
3154 d_print_comp (&dpi, dc);
3156 d_print_flush (&dpi);
3158 return ! d_print_saw_error (&dpi);
3161 /* Turn components into a human readable string. OPTIONS is the
3162 options bits passed to the demangler. DC is the tree to print.
3163 ESTIMATE is a guess at the length of the result. This returns a
3164 string allocated by malloc, or NULL on error. On success, this
3165 sets *PALC to the size of the allocated buffer. On failure, this
3166 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
3169 CP_STATIC_IF_GLIBCPP_V3
3171 cplus_demangle_print (int options, const struct demangle_component *dc,
3172 int estimate, size_t *palc)
3174 struct d_growable_string dgs;
3176 d_growable_string_init (&dgs, estimate);
3178 if (! cplus_demangle_print_callback (options, dc,
3179 d_growable_string_callback_adapter,
3187 *palc = dgs.allocation_failure ? 1 : dgs.alc;
3191 /* Returns the I'th element of the template arglist ARGS, or NULL on
3194 static struct demangle_component *
3195 d_index_template_argument (struct demangle_component *args, int i)
3197 struct demangle_component *a;
3203 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3209 if (i != 0 || a == NULL)
3215 /* Returns the template argument from the current context indicated by DC,
3216 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
3218 static struct demangle_component *
3219 d_lookup_template_argument (struct d_print_info *dpi,
3220 const struct demangle_component *dc)
3222 if (dpi->templates == NULL)
3224 d_print_error (dpi);
3228 return d_index_template_argument
3229 (d_right (dpi->templates->template_decl),
3230 dc->u.s_number.number);
3233 /* Returns a template argument pack used in DC (any will do), or NULL. */
3235 static struct demangle_component *
3236 d_find_pack (struct d_print_info *dpi,
3237 const struct demangle_component *dc)
3239 struct demangle_component *a;
3245 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
3246 a = d_lookup_template_argument (dpi, dc);
3247 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3251 case DEMANGLE_COMPONENT_PACK_EXPANSION:
3254 case DEMANGLE_COMPONENT_NAME:
3255 case DEMANGLE_COMPONENT_OPERATOR:
3256 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3257 case DEMANGLE_COMPONENT_SUB_STD:
3258 case DEMANGLE_COMPONENT_CHARACTER:
3259 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
3262 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3263 return d_find_pack (dpi, dc->u.s_extended_operator.name);
3264 case DEMANGLE_COMPONENT_CTOR:
3265 return d_find_pack (dpi, dc->u.s_ctor.name);
3266 case DEMANGLE_COMPONENT_DTOR:
3267 return d_find_pack (dpi, dc->u.s_dtor.name);
3270 a = d_find_pack (dpi, d_left (dc));
3273 return d_find_pack (dpi, d_right (dc));
3277 /* Returns the length of the template argument pack DC. */
3280 d_pack_length (const struct demangle_component *dc)
3283 while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
3284 && d_left (dc) != NULL)
3292 /* DC is a component of a mangled expression. Print it, wrapped in parens
3296 d_print_subexpr (struct d_print_info *dpi,
3297 const struct demangle_component *dc)
3300 if (dc->type == DEMANGLE_COMPONENT_NAME
3301 || dc->type == DEMANGLE_COMPONENT_FUNCTION_PARAM)
3304 d_append_char (dpi, '(');
3305 d_print_comp (dpi, dc);
3307 d_append_char (dpi, ')');
3310 /* Subroutine to handle components. */
3313 d_print_comp (struct d_print_info *dpi,
3314 const struct demangle_component *dc)
3318 d_print_error (dpi);
3321 if (d_print_saw_error (dpi))
3326 case DEMANGLE_COMPONENT_NAME:
3327 if ((dpi->options & DMGL_JAVA) == 0)
3328 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
3330 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
3333 case DEMANGLE_COMPONENT_QUAL_NAME:
3334 case DEMANGLE_COMPONENT_LOCAL_NAME:
3335 d_print_comp (dpi, d_left (dc));
3336 if ((dpi->options & DMGL_JAVA) == 0)
3337 d_append_string (dpi, "::");
3339 d_append_char (dpi, '.');
3340 d_print_comp (dpi, d_right (dc));
3343 case DEMANGLE_COMPONENT_TYPED_NAME:
3345 struct d_print_mod *hold_modifiers;
3346 struct demangle_component *typed_name;
3347 struct d_print_mod adpm[4];
3349 struct d_print_template dpt;
3351 /* Pass the name down to the type so that it can be printed in
3352 the right place for the type. We also have to pass down
3353 any CV-qualifiers, which apply to the this parameter. */
3354 hold_modifiers = dpi->modifiers;
3357 typed_name = d_left (dc);
3358 while (typed_name != NULL)
3360 if (i >= sizeof adpm / sizeof adpm[0])
3362 d_print_error (dpi);
3366 adpm[i].next = dpi->modifiers;
3367 dpi->modifiers = &adpm[i];
3368 adpm[i].mod = typed_name;
3369 adpm[i].printed = 0;
3370 adpm[i].templates = dpi->templates;
3373 if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
3374 && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
3375 && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS)
3378 typed_name = d_left (typed_name);
3381 if (typed_name == NULL)
3383 d_print_error (dpi);
3387 /* If typed_name is a template, then it applies to the
3388 function type as well. */
3389 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
3391 dpt.next = dpi->templates;
3392 dpi->templates = &dpt;
3393 dpt.template_decl = typed_name;
3396 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
3397 there may be CV-qualifiers on its right argument which
3398 really apply here; this happens when parsing a class which
3399 is local to a function. */
3400 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
3402 struct demangle_component *local_name;
3404 local_name = d_right (typed_name);
3405 while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3406 || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3407 || local_name->type == DEMANGLE_COMPONENT_CONST_THIS)
3409 if (i >= sizeof adpm / sizeof adpm[0])
3411 d_print_error (dpi);
3415 adpm[i] = adpm[i - 1];
3416 adpm[i].next = &adpm[i - 1];
3417 dpi->modifiers = &adpm[i];
3419 adpm[i - 1].mod = local_name;
3420 adpm[i - 1].printed = 0;
3421 adpm[i - 1].templates = dpi->templates;
3424 local_name = d_left (local_name);
3428 d_print_comp (dpi, d_right (dc));
3430 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
3431 dpi->templates = dpt.next;
3433 /* If the modifiers didn't get printed by the type, print them
3438 if (! adpm[i].printed)
3440 d_append_char (dpi, ' ');
3441 d_print_mod (dpi, adpm[i].mod);
3445 dpi->modifiers = hold_modifiers;
3450 case DEMANGLE_COMPONENT_TEMPLATE:
3452 struct d_print_mod *hold_dpm;
3453 struct demangle_component *dcl;
3455 /* Don't push modifiers into a template definition. Doing so
3456 could give the wrong definition for a template argument.
3457 Instead, treat the template essentially as a name. */
3459 hold_dpm = dpi->modifiers;
3460 dpi->modifiers = NULL;
3464 if ((dpi->options & DMGL_JAVA) != 0
3465 && dcl->type == DEMANGLE_COMPONENT_NAME
3466 && dcl->u.s_name.len == 6
3467 && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
3469 /* Special-case Java arrays, so that JArray<TYPE> appears
3470 instead as TYPE[]. */
3472 d_print_comp (dpi, d_right (dc));
3473 d_append_string (dpi, "[]");
3477 d_print_comp (dpi, dcl);
3478 if (d_last_char (dpi) == '<')
3479 d_append_char (dpi, ' ');
3480 d_append_char (dpi, '<');
3481 d_print_comp (dpi, d_right (dc));
3482 /* Avoid generating two consecutive '>' characters, to avoid
3483 the C++ syntactic ambiguity. */
3484 if (d_last_char (dpi) == '>')
3485 d_append_char (dpi, ' ');
3486 d_append_char (dpi, '>');
3489 dpi->modifiers = hold_dpm;
3494 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
3496 struct d_print_template *hold_dpt;
3497 struct demangle_component *a = d_lookup_template_argument (dpi, dc);
3499 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3500 a = d_index_template_argument (a, dpi->pack_index);
3504 d_print_error (dpi);
3508 /* While processing this parameter, we need to pop the list of
3509 templates. This is because the template parameter may
3510 itself be a reference to a parameter of an outer
3513 hold_dpt = dpi->templates;
3514 dpi->templates = hold_dpt->next;
3516 d_print_comp (dpi, a);
3518 dpi->templates = hold_dpt;
3523 case DEMANGLE_COMPONENT_CTOR:
3524 d_print_comp (dpi, dc->u.s_ctor.name);
3527 case DEMANGLE_COMPONENT_DTOR:
3528 d_append_char (dpi, '~');
3529 d_print_comp (dpi, dc->u.s_dtor.name);
3532 case DEMANGLE_COMPONENT_VTABLE:
3533 d_append_string (dpi, "vtable for ");
3534 d_print_comp (dpi, d_left (dc));
3537 case DEMANGLE_COMPONENT_VTT:
3538 d_append_string (dpi, "VTT for ");
3539 d_print_comp (dpi, d_left (dc));
3542 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
3543 d_append_string (dpi, "construction vtable for ");
3544 d_print_comp (dpi, d_left (dc));
3545 d_append_string (dpi, "-in-");
3546 d_print_comp (dpi, d_right (dc));
3549 case DEMANGLE_COMPONENT_TYPEINFO:
3550 d_append_string (dpi, "typeinfo for ");
3551 d_print_comp (dpi, d_left (dc));
3554 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
3555 d_append_string (dpi, "typeinfo name for ");
3556 d_print_comp (dpi, d_left (dc));
3559 case DEMANGLE_COMPONENT_TYPEINFO_FN:
3560 d_append_string (dpi, "typeinfo fn for ");
3561 d_print_comp (dpi, d_left (dc));
3564 case DEMANGLE_COMPONENT_THUNK:
3565 d_append_string (dpi, "non-virtual thunk to ");
3566 d_print_comp (dpi, d_left (dc));
3569 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
3570 d_append_string (dpi, "virtual thunk to ");
3571 d_print_comp (dpi, d_left (dc));
3574 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
3575 d_append_string (dpi, "covariant return thunk to ");
3576 d_print_comp (dpi, d_left (dc));
3579 case DEMANGLE_COMPONENT_JAVA_CLASS:
3580 d_append_string (dpi, "java Class for ");
3581 d_print_comp (dpi, d_left (dc));
3584 case DEMANGLE_COMPONENT_GUARD:
3585 d_append_string (dpi, "guard variable for ");
3586 d_print_comp (dpi, d_left (dc));
3589 case DEMANGLE_COMPONENT_REFTEMP:
3590 d_append_string (dpi, "reference temporary for ");
3591 d_print_comp (dpi, d_left (dc));
3594 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
3595 d_append_string (dpi, "hidden alias for ");
3596 d_print_comp (dpi, d_left (dc));
3599 case DEMANGLE_COMPONENT_SUB_STD:
3600 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
3603 case DEMANGLE_COMPONENT_RESTRICT:
3604 case DEMANGLE_COMPONENT_VOLATILE:
3605 case DEMANGLE_COMPONENT_CONST:
3607 struct d_print_mod *pdpm;
3609 /* When printing arrays, it's possible to have cases where the
3610 same CV-qualifier gets pushed on the stack multiple times.
3611 We only need to print it once. */
3613 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
3615 if (! pdpm->printed)
3617 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
3618 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
3619 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
3621 if (pdpm->mod->type == dc->type)
3623 d_print_comp (dpi, d_left (dc));
3630 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3631 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3632 case DEMANGLE_COMPONENT_CONST_THIS:
3633 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3634 case DEMANGLE_COMPONENT_POINTER:
3635 case DEMANGLE_COMPONENT_REFERENCE:
3636 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
3637 case DEMANGLE_COMPONENT_COMPLEX:
3638 case DEMANGLE_COMPONENT_IMAGINARY:
3640 /* We keep a list of modifiers on the stack. */
3641 struct d_print_mod dpm;
3643 dpm.next = dpi->modifiers;
3644 dpi->modifiers = &dpm;
3647 dpm.templates = dpi->templates;
3649 d_print_comp (dpi, d_left (dc));
3651 /* If the modifier didn't get printed by the type, print it
3654 d_print_mod (dpi, dc);
3656 dpi->modifiers = dpm.next;
3661 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3662 if ((dpi->options & DMGL_JAVA) == 0)
3663 d_append_buffer (dpi, dc->u.s_builtin.type->name,
3664 dc->u.s_builtin.type->len);
3666 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
3667 dc->u.s_builtin.type->java_len);
3670 case DEMANGLE_COMPONENT_VENDOR_TYPE:
3671 d_print_comp (dpi, d_left (dc));
3674 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
3676 if ((dpi->options & DMGL_RET_POSTFIX) != 0)
3677 d_print_function_type (dpi, dc, dpi->modifiers);
3679 /* Print return type if present */
3680 if (d_left (dc) != NULL)
3682 struct d_print_mod dpm;
3684 /* We must pass this type down as a modifier in order to
3685 print it in the right location. */
3686 dpm.next = dpi->modifiers;
3687 dpi->modifiers = &dpm;
3690 dpm.templates = dpi->templates;
3692 d_print_comp (dpi, d_left (dc));
3694 dpi->modifiers = dpm.next;
3699 /* In standard prefix notation, there is a space between the
3700 return type and the function signature. */
3701 if ((dpi->options & DMGL_RET_POSTFIX) == 0)
3702 d_append_char (dpi, ' ');
3705 if ((dpi->options & DMGL_RET_POSTFIX) == 0)
3706 d_print_function_type (dpi, dc, dpi->modifiers);
3711 case DEMANGLE_COMPONENT_ARRAY_TYPE:
3713 struct d_print_mod *hold_modifiers;
3714 struct d_print_mod adpm[4];
3716 struct d_print_mod *pdpm;
3718 /* We must pass this type down as a modifier in order to print
3719 multi-dimensional arrays correctly. If the array itself is
3720 CV-qualified, we act as though the element type were
3721 CV-qualified. We do this by copying the modifiers down
3722 rather than fiddling pointers, so that we don't wind up
3723 with a d_print_mod higher on the stack pointing into our
3724 stack frame after we return. */
3726 hold_modifiers = dpi->modifiers;
3728 adpm[0].next = hold_modifiers;
3729 dpi->modifiers = &adpm[0];
3731 adpm[0].printed = 0;
3732 adpm[0].templates = dpi->templates;
3735 pdpm = hold_modifiers;
3737 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
3738 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
3739 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
3741 if (! pdpm->printed)
3743 if (i >= sizeof adpm / sizeof adpm[0])
3745 d_print_error (dpi);
3750 adpm[i].next = dpi->modifiers;
3751 dpi->modifiers = &adpm[i];
3759 d_print_comp (dpi, d_right (dc));
3761 dpi->modifiers = hold_modifiers;
3763 if (adpm[0].printed)
3769 d_print_mod (dpi, adpm[i].mod);
3772 d_print_array_type (dpi, dc, dpi->modifiers);
3777 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3779 struct d_print_mod dpm;
3781 dpm.next = dpi->modifiers;
3782 dpi->modifiers = &dpm;
3785 dpm.templates = dpi->templates;
3787 d_print_comp (dpi, d_right (dc));
3789 /* If the modifier didn't get printed by the type, print it
3793 d_append_char (dpi, ' ');
3794 d_print_comp (dpi, d_left (dc));
3795 d_append_string (dpi, "::*");
3798 dpi->modifiers = dpm.next;
3803 case DEMANGLE_COMPONENT_FIXED_TYPE:
3804 if (dc->u.s_fixed.sat)
3805 d_append_string (dpi, "_Sat ");
3806 /* Don't print "int _Accum". */
3807 if (dc->u.s_fixed.length->u.s_builtin.type
3808 != &cplus_demangle_builtin_types['i'-'a'])
3810 d_print_comp (dpi, dc->u.s_fixed.length);
3811 d_append_char (dpi, ' ');
3813 if (dc->u.s_fixed.accum)
3814 d_append_string (dpi, "_Accum");
3816 d_append_string (dpi, "_Fract");
3819 case DEMANGLE_COMPONENT_ARGLIST:
3820 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
3821 if (d_left (dc) != NULL)
3822 d_print_comp (dpi, d_left (dc));
3823 if (d_right (dc) != NULL)
3826 unsigned long int flush_count;
3827 /* Make sure ", " isn't flushed by d_append_string, otherwise
3828 dpi->len -= 2 wouldn't work. */
3829 if (dpi->len >= sizeof (dpi->buf) - 2)
3830 d_print_flush (dpi);
3831 d_append_string (dpi, ", ");
3833 flush_count = dpi->flush_count;
3834 d_print_comp (dpi, d_right (dc));
3835 /* If that didn't print anything (which can happen with empty
3836 template argument packs), remove the comma and space. */
3837 if (dpi->flush_count == flush_count && dpi->len == len)
3842 case DEMANGLE_COMPONENT_OPERATOR:
3846 d_append_string (dpi, "operator");
3847 c = dc->u.s_operator.op->name[0];
3849 d_append_char (dpi, ' ');
3850 d_append_buffer (dpi, dc->u.s_operator.op->name,
3851 dc->u.s_operator.op->len);
3855 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3856 d_append_string (dpi, "operator ");
3857 d_print_comp (dpi, dc->u.s_extended_operator.name);
3860 case DEMANGLE_COMPONENT_CAST:
3861 d_append_string (dpi, "operator ");
3862 d_print_cast (dpi, dc);
3865 case DEMANGLE_COMPONENT_UNARY:
3866 if (d_left (dc)->type != DEMANGLE_COMPONENT_CAST)
3867 d_print_expr_op (dpi, d_left (dc));
3870 d_append_char (dpi, '(');
3871 d_print_cast (dpi, d_left (dc));
3872 d_append_char (dpi, ')');
3874 d_print_subexpr (dpi, d_right (dc));
3877 case DEMANGLE_COMPONENT_BINARY:
3878 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
3880 d_print_error (dpi);
3884 /* We wrap an expression which uses the greater-than operator in
3885 an extra layer of parens so that it does not get confused
3886 with the '>' which ends the template parameters. */
3887 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
3888 && d_left (dc)->u.s_operator.op->len == 1
3889 && d_left (dc)->u.s_operator.op->name[0] == '>')
3890 d_append_char (dpi, '(');
3892 d_print_subexpr (dpi, d_left (d_right (dc)));
3893 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") != 0)
3894 d_print_expr_op (dpi, d_left (dc));
3895 d_print_subexpr (dpi, d_right (d_right (dc)));
3897 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
3898 && d_left (dc)->u.s_operator.op->len == 1
3899 && d_left (dc)->u.s_operator.op->name[0] == '>')
3900 d_append_char (dpi, ')');
3904 case DEMANGLE_COMPONENT_BINARY_ARGS:
3905 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
3906 d_print_error (dpi);
3909 case DEMANGLE_COMPONENT_TRINARY:
3910 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
3911 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
3913 d_print_error (dpi);
3916 d_print_subexpr (dpi, d_left (d_right (dc)));
3917 d_print_expr_op (dpi, d_left (dc));
3918 d_print_subexpr (dpi, d_left (d_right (d_right (dc))));
3919 d_append_string (dpi, " : ");
3920 d_print_subexpr (dpi, d_right (d_right (d_right (dc))));
3923 case DEMANGLE_COMPONENT_TRINARY_ARG1:
3924 case DEMANGLE_COMPONENT_TRINARY_ARG2:
3925 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
3926 d_print_error (dpi);
3929 case DEMANGLE_COMPONENT_LITERAL:
3930 case DEMANGLE_COMPONENT_LITERAL_NEG:
3932 enum d_builtin_type_print tp;
3934 /* For some builtin types, produce simpler output. */
3935 tp = D_PRINT_DEFAULT;
3936 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
3938 tp = d_left (dc)->u.s_builtin.type->print;
3942 case D_PRINT_UNSIGNED:
3944 case D_PRINT_UNSIGNED_LONG:
3945 case D_PRINT_LONG_LONG:
3946 case D_PRINT_UNSIGNED_LONG_LONG:
3947 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
3949 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
3950 d_append_char (dpi, '-');
3951 d_print_comp (dpi, d_right (dc));
3956 case D_PRINT_UNSIGNED:
3957 d_append_char (dpi, 'u');
3960 d_append_char (dpi, 'l');
3962 case D_PRINT_UNSIGNED_LONG:
3963 d_append_string (dpi, "ul");
3965 case D_PRINT_LONG_LONG:
3966 d_append_string (dpi, "ll");
3968 case D_PRINT_UNSIGNED_LONG_LONG:
3969 d_append_string (dpi, "ull");
3977 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
3978 && d_right (dc)->u.s_name.len == 1
3979 && dc->type == DEMANGLE_COMPONENT_LITERAL)
3981 switch (d_right (dc)->u.s_name.s[0])
3984 d_append_string (dpi, "false");
3987 d_append_string (dpi, "true");
4000 d_append_char (dpi, '(');
4001 d_print_comp (dpi, d_left (dc));
4002 d_append_char (dpi, ')');
4003 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
4004 d_append_char (dpi, '-');
4005 if (tp == D_PRINT_FLOAT)
4006 d_append_char (dpi, '[');
4007 d_print_comp (dpi, d_right (dc));
4008 if (tp == D_PRINT_FLOAT)
4009 d_append_char (dpi, ']');
4013 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
4014 d_append_string (dpi, "java resource ");
4015 d_print_comp (dpi, d_left (dc));
4018 case DEMANGLE_COMPONENT_COMPOUND_NAME:
4019 d_print_comp (dpi, d_left (dc));
4020 d_print_comp (dpi, d_right (dc));
4023 case DEMANGLE_COMPONENT_CHARACTER:
4024 d_append_char (dpi, dc->u.s_character.character);
4027 case DEMANGLE_COMPONENT_DECLTYPE:
4028 d_append_string (dpi, "decltype (");
4029 d_print_comp (dpi, d_left (dc));
4030 d_append_char (dpi, ')');
4033 case DEMANGLE_COMPONENT_PACK_EXPANSION:
4037 struct demangle_component *a = d_find_pack (dpi, d_left (dc));
4040 /* d_find_pack won't find anything if the only packs involved
4041 in this expansion are function parameter packs; in that
4042 case, just print the pattern and "...". */
4043 d_print_subexpr (dpi, d_left (dc));
4044 d_append_string (dpi, "...");
4048 len = d_pack_length (a);
4050 for (i = 0; i < len; ++i)
4052 dpi->pack_index = i;
4053 d_print_comp (dpi, dc);
4055 d_append_string (dpi, ", ");
4060 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
4063 d_append_string (dpi, "parm#");
4064 sprintf(buf,"%ld", dc->u.s_number.number);
4065 d_append_string (dpi, buf);
4070 d_print_error (dpi);
4075 /* Print a Java dentifier. For Java we try to handle encoded extended
4076 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
4077 so we don't it for C++. Characters are encoded as
4081 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
4087 for (p = name; p < end; ++p)
4098 for (q = p + 3; q < end; ++q)
4104 else if (*q >= 'A' && *q <= 'F')
4105 dig = *q - 'A' + 10;
4106 else if (*q >= 'a' && *q <= 'f')
4107 dig = *q - 'a' + 10;
4113 /* If the Unicode character is larger than 256, we don't try
4114 to deal with it here. FIXME. */
4115 if (q < end && *q == '_' && c < 256)
4117 d_append_char (dpi, c);
4123 d_append_char (dpi, *p);
4127 /* Print a list of modifiers. SUFFIX is 1 if we are printing
4128 qualifiers on this after printing a function. */
4131 d_print_mod_list (struct d_print_info *dpi,
4132 struct d_print_mod *mods, int suffix)
4134 struct d_print_template *hold_dpt;
4136 if (mods == NULL || d_print_saw_error (dpi))
4141 && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
4142 || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
4143 || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS)))
4145 d_print_mod_list (dpi, mods->next, suffix);
4151 hold_dpt = dpi->templates;
4152 dpi->templates = mods->templates;
4154 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
4156 d_print_function_type (dpi, mods->mod, mods->next);
4157 dpi->templates = hold_dpt;
4160 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
4162 d_print_array_type (dpi, mods->mod, mods->next);
4163 dpi->templates = hold_dpt;
4166 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
4168 struct d_print_mod *hold_modifiers;
4169 struct demangle_component *dc;
4171 /* When this is on the modifier stack, we have pulled any
4172 qualifiers off the right argument already. Otherwise, we
4173 print it as usual, but don't let the left argument see any
4176 hold_modifiers = dpi->modifiers;
4177 dpi->modifiers = NULL;
4178 d_print_comp (dpi, d_left (mods->mod));
4179 dpi->modifiers = hold_modifiers;
4181 if ((dpi->options & DMGL_JAVA) == 0)
4182 d_append_string (dpi, "::");
4184 d_append_char (dpi, '.');
4186 dc = d_right (mods->mod);
4187 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
4188 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
4189 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
4192 d_print_comp (dpi, dc);
4194 dpi->templates = hold_dpt;
4198 d_print_mod (dpi, mods->mod);
4200 dpi->templates = hold_dpt;
4202 d_print_mod_list (dpi, mods->next, suffix);
4205 /* Print a modifier. */
4208 d_print_mod (struct d_print_info *dpi,
4209 const struct demangle_component *mod)
4213 case DEMANGLE_COMPONENT_RESTRICT:
4214 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4215 d_append_string (dpi, " restrict");
4217 case DEMANGLE_COMPONENT_VOLATILE:
4218 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4219 d_append_string (dpi, " volatile");
4221 case DEMANGLE_COMPONENT_CONST:
4222 case DEMANGLE_COMPONENT_CONST_THIS:
4223 d_append_string (dpi, " const");
4225 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4226 d_append_char (dpi, ' ');
4227 d_print_comp (dpi, d_right (mod));
4229 case DEMANGLE_COMPONENT_POINTER:
4230 /* There is no pointer symbol in Java. */
4231 if ((dpi->options & DMGL_JAVA) == 0)
4232 d_append_char (dpi, '*');
4234 case DEMANGLE_COMPONENT_REFERENCE:
4235 d_append_char (dpi, '&');
4237 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4238 d_append_string (dpi, "&&");
4240 case DEMANGLE_COMPONENT_COMPLEX:
4241 d_append_string (dpi, "complex ");
4243 case DEMANGLE_COMPONENT_IMAGINARY:
4244 d_append_string (dpi, "imaginary ");
4246 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4247 if (d_last_char (dpi) != '(')
4248 d_append_char (dpi, ' ');
4249 d_print_comp (dpi, d_left (mod));
4250 d_append_string (dpi, "::*");
4252 case DEMANGLE_COMPONENT_TYPED_NAME:
4253 d_print_comp (dpi, d_left (mod));
4256 /* Otherwise, we have something that won't go back on the
4257 modifier stack, so we can just print it. */
4258 d_print_comp (dpi, mod);
4263 /* Print a function type, except for the return type. */
4266 d_print_function_type (struct d_print_info *dpi,
4267 const struct demangle_component *dc,
4268 struct d_print_mod *mods)
4273 struct d_print_mod *p;
4274 struct d_print_mod *hold_modifiers;
4279 for (p = mods; p != NULL; p = p->next)
4285 switch (p->mod->type)
4287 case DEMANGLE_COMPONENT_POINTER:
4288 case DEMANGLE_COMPONENT_REFERENCE:
4289 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4292 case DEMANGLE_COMPONENT_RESTRICT:
4293 case DEMANGLE_COMPONENT_VOLATILE:
4294 case DEMANGLE_COMPONENT_CONST:
4295 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4296 case DEMANGLE_COMPONENT_COMPLEX:
4297 case DEMANGLE_COMPONENT_IMAGINARY:
4298 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4302 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4303 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4304 case DEMANGLE_COMPONENT_CONST_THIS:
4313 if (d_left (dc) != NULL && ! saw_mod)
4320 if (d_last_char (dpi) != '('
4321 && d_last_char (dpi) != '*')
4324 if (need_space && d_last_char (dpi) != ' ')
4325 d_append_char (dpi, ' ');
4326 d_append_char (dpi, '(');
4329 hold_modifiers = dpi->modifiers;
4330 dpi->modifiers = NULL;
4332 d_print_mod_list (dpi, mods, 0);
4335 d_append_char (dpi, ')');
4337 d_append_char (dpi, '(');
4339 if (d_right (dc) != NULL)
4340 d_print_comp (dpi, d_right (dc));
4342 d_append_char (dpi, ')');
4344 d_print_mod_list (dpi, mods, 1);
4346 dpi->modifiers = hold_modifiers;
4349 /* Print an array type, except for the element type. */
4352 d_print_array_type (struct d_print_info *dpi,
4353 const struct demangle_component *dc,
4354 struct d_print_mod *mods)
4362 struct d_print_mod *p;
4365 for (p = mods; p != NULL; p = p->next)
4369 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
4384 d_append_string (dpi, " (");
4386 d_print_mod_list (dpi, mods, 0);
4389 d_append_char (dpi, ')');
4393 d_append_char (dpi, ' ');
4395 d_append_char (dpi, '[');
4397 if (d_left (dc) != NULL)
4398 d_print_comp (dpi, d_left (dc));
4400 d_append_char (dpi, ']');
4403 /* Print an operator in an expression. */
4406 d_print_expr_op (struct d_print_info *dpi,
4407 const struct demangle_component *dc)
4409 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
4410 d_append_buffer (dpi, dc->u.s_operator.op->name,
4411 dc->u.s_operator.op->len);
4413 d_print_comp (dpi, dc);
4419 d_print_cast (struct d_print_info *dpi,
4420 const struct demangle_component *dc)
4422 if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
4423 d_print_comp (dpi, d_left (dc));
4426 struct d_print_mod *hold_dpm;
4427 struct d_print_template dpt;
4429 /* It appears that for a templated cast operator, we need to put
4430 the template parameters in scope for the operator name, but
4431 not for the parameters. The effect is that we need to handle
4432 the template printing here. */
4434 hold_dpm = dpi->modifiers;
4435 dpi->modifiers = NULL;
4437 dpt.next = dpi->templates;
4438 dpi->templates = &dpt;
4439 dpt.template_decl = d_left (dc);
4441 d_print_comp (dpi, d_left (d_left (dc)));
4443 dpi->templates = dpt.next;
4445 if (d_last_char (dpi) == '<')
4446 d_append_char (dpi, ' ');
4447 d_append_char (dpi, '<');
4448 d_print_comp (dpi, d_right (d_left (dc)));
4449 /* Avoid generating two consecutive '>' characters, to avoid
4450 the C++ syntactic ambiguity. */
4451 if (d_last_char (dpi) == '>')
4452 d_append_char (dpi, ' ');
4453 d_append_char (dpi, '>');
4455 dpi->modifiers = hold_dpm;
4459 /* Initialize the information structure we use to pass around
4462 CP_STATIC_IF_GLIBCPP_V3
4464 cplus_demangle_init_info (const char *mangled, int options, size_t len,
4468 di->send = mangled + len;
4469 di->options = options;
4473 /* We can not need more components than twice the number of chars in
4474 the mangled string. Most components correspond directly to
4475 chars, but the ARGLIST types are exceptions. */
4476 di->num_comps = 2 * len;
4479 /* Similarly, we can not need more substitutions than there are
4480 chars in the mangled string. */
4485 di->last_name = NULL;
4490 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
4491 mangled name, return strings in repeated callback giving the demangled
4492 name. OPTIONS is the usual libiberty demangler options. On success,
4493 this returns 1. On failure, returns 0. */
4496 d_demangle_callback (const char *mangled, int options,
4497 demangle_callbackref callback, void *opaque)
4501 struct demangle_component *dc;
4504 if (mangled[0] == '_' && mangled[1] == 'Z')
4506 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
4507 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
4508 && (mangled[9] == 'D' || mangled[9] == 'I')
4509 && mangled[10] == '_')
4513 intro = (mangled[9] == 'I')
4514 ? "global constructors keyed to "
4515 : "global destructors keyed to ";
4517 callback (intro, strlen (intro), opaque);
4518 callback (mangled + 11, strlen (mangled + 11), opaque);
4523 if ((options & DMGL_TYPES) == 0)
4528 cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
4531 #ifdef CP_DYNAMIC_ARRAYS
4532 __extension__ struct demangle_component comps[di.num_comps];
4533 __extension__ struct demangle_component *subs[di.num_subs];
4538 di.comps = alloca (di.num_comps * sizeof (*di.comps));
4539 di.subs = alloca (di.num_subs * sizeof (*di.subs));
4543 dc = cplus_demangle_type (&di);
4545 dc = cplus_demangle_mangled_name (&di, 1);
4547 /* If DMGL_PARAMS is set, then if we didn't consume the entire
4548 mangled string, then we didn't successfully demangle it. If
4549 DMGL_PARAMS is not set, we didn't look at the trailing
4551 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
4554 #ifdef CP_DEMANGLE_DEBUG
4558 status = (dc != NULL)
4559 ? cplus_demangle_print_callback (options, dc, callback, opaque)
4566 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
4567 name, return a buffer allocated with malloc holding the demangled
4568 name. OPTIONS is the usual libiberty demangler options. On
4569 success, this sets *PALC to the allocated size of the returned
4570 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
4571 a memory allocation failure, and returns NULL. */
4574 d_demangle (const char *mangled, int options, size_t *palc)
4576 struct d_growable_string dgs;
4579 d_growable_string_init (&dgs, 0);
4581 status = d_demangle_callback (mangled, options,
4582 d_growable_string_callback_adapter, &dgs);
4590 *palc = dgs.allocation_failure ? 1 : 0;
4594 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
4596 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
4598 /* ia64 ABI-mandated entry point in the C++ runtime library for
4599 performing demangling. MANGLED_NAME is a NUL-terminated character
4600 string containing the name to be demangled.
4602 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
4603 *LENGTH bytes, into which the demangled name is stored. If
4604 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
4605 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
4606 is placed in a region of memory allocated with malloc.
4608 If LENGTH is non-NULL, the length of the buffer containing the
4609 demangled name, is placed in *LENGTH.
4611 The return value is a pointer to the start of the NUL-terminated
4612 demangled name, or NULL if the demangling fails. The caller is
4613 responsible for deallocating this memory using free.
4615 *STATUS is set to one of the following values:
4616 0: The demangling operation succeeded.
4617 -1: A memory allocation failure occurred.
4618 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
4619 -3: One of the arguments is invalid.
4621 The demangling is performed using the C++ ABI mangling rules, with
4625 __cxa_demangle (const char *mangled_name, char *output_buffer,
4626 size_t *length, int *status)
4631 if (mangled_name == NULL)
4638 if (output_buffer != NULL && length == NULL)
4645 demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
4647 if (demangled == NULL)
4659 if (output_buffer == NULL)
4666 if (strlen (demangled) < *length)
4668 strcpy (output_buffer, demangled);
4670 demangled = output_buffer;
4674 free (output_buffer);
4685 extern int __gcclibcxx_demangle_callback (const char *,
4687 (const char *, size_t, void *),
4690 /* Alternative, allocationless entry point in the C++ runtime library
4691 for performing demangling. MANGLED_NAME is a NUL-terminated character
4692 string containing the name to be demangled.
4694 CALLBACK is a callback function, called with demangled string
4695 segments as demangling progresses; it is called at least once,
4696 but may be called more than once. OPAQUE is a generalized pointer
4697 used as a callback argument.
4699 The return code is one of the following values, equivalent to
4700 the STATUS values of __cxa_demangle() (excluding -1, since this
4701 function performs no memory allocations):
4702 0: The demangling operation succeeded.
4703 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
4704 -3: One of the arguments is invalid.
4706 The demangling is performed using the C++ ABI mangling rules, with
4710 __gcclibcxx_demangle_callback (const char *mangled_name,
4711 void (*callback) (const char *, size_t, void *),
4716 if (mangled_name == NULL || callback == NULL)
4719 status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES,
4727 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
4729 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
4730 mangled name, return a buffer allocated with malloc holding the
4731 demangled name. Otherwise, return NULL. */
4734 cplus_demangle_v3 (const char *mangled, int options)
4738 return d_demangle (mangled, options, &alc);
4742 cplus_demangle_v3_callback (const char *mangled, int options,
4743 demangle_callbackref callback, void *opaque)
4745 return d_demangle_callback (mangled, options, callback, opaque);
4748 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
4749 conventions, but the output formatting is a little different.
4750 This instructs the C++ demangler not to emit pointer characters ("*"), to
4751 use Java's namespace separator symbol ("." instead of "::"), and to output
4752 JArray<TYPE> as TYPE[]. */
4755 java_demangle_v3 (const char *mangled)
4759 return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
4763 java_demangle_v3_callback (const char *mangled,
4764 demangle_callbackref callback, void *opaque)
4766 return d_demangle_callback (mangled,
4767 DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
4771 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
4773 #ifndef IN_GLIBCPP_V3
4775 /* Demangle a string in order to find out whether it is a constructor
4776 or destructor. Return non-zero on success. Set *CTOR_KIND and
4777 *DTOR_KIND appropriately. */
4780 is_ctor_or_dtor (const char *mangled,
4781 enum gnu_v3_ctor_kinds *ctor_kind,
4782 enum gnu_v3_dtor_kinds *dtor_kind)
4785 struct demangle_component *dc;
4788 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
4789 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
4791 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
4794 #ifdef CP_DYNAMIC_ARRAYS
4795 __extension__ struct demangle_component comps[di.num_comps];
4796 __extension__ struct demangle_component *subs[di.num_subs];
4801 di.comps = alloca (di.num_comps * sizeof (*di.comps));
4802 di.subs = alloca (di.num_subs * sizeof (*di.subs));
4805 dc = cplus_demangle_mangled_name (&di, 1);
4807 /* Note that because we did not pass DMGL_PARAMS, we don't expect
4808 to demangle the entire string. */
4818 case DEMANGLE_COMPONENT_TYPED_NAME:
4819 case DEMANGLE_COMPONENT_TEMPLATE:
4820 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4821 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4822 case DEMANGLE_COMPONENT_CONST_THIS:
4825 case DEMANGLE_COMPONENT_QUAL_NAME:
4826 case DEMANGLE_COMPONENT_LOCAL_NAME:
4829 case DEMANGLE_COMPONENT_CTOR:
4830 *ctor_kind = dc->u.s_ctor.kind;
4834 case DEMANGLE_COMPONENT_DTOR:
4835 *dtor_kind = dc->u.s_dtor.kind;
4846 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
4847 name. A non-zero return indicates the type of constructor. */
4849 enum gnu_v3_ctor_kinds
4850 is_gnu_v3_mangled_ctor (const char *name)
4852 enum gnu_v3_ctor_kinds ctor_kind;
4853 enum gnu_v3_dtor_kinds dtor_kind;
4855 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
4856 return (enum gnu_v3_ctor_kinds) 0;
4861 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
4862 name. A non-zero return indicates the type of destructor. */
4864 enum gnu_v3_dtor_kinds
4865 is_gnu_v3_mangled_dtor (const char *name)
4867 enum gnu_v3_ctor_kinds ctor_kind;
4868 enum gnu_v3_dtor_kinds dtor_kind;
4870 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
4871 return (enum gnu_v3_dtor_kinds) 0;
4875 #endif /* IN_GLIBCPP_V3 */
4877 #ifdef STANDALONE_DEMANGLER
4880 #include "dyn-string.h"
4882 static void print_usage (FILE* fp, int exit_value);
4884 #define IS_ALPHA(CHAR) \
4885 (((CHAR) >= 'a' && (CHAR) <= 'z') \
4886 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
4888 /* Non-zero if CHAR is a character than can occur in a mangled name. */
4889 #define is_mangled_char(CHAR) \
4890 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
4891 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
4893 /* The name of this program, as invoked. */
4894 const char* program_name;
4896 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
4899 print_usage (FILE* fp, int exit_value)
4901 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
4902 fprintf (fp, "Options:\n");
4903 fprintf (fp, " -h,--help Display this message.\n");
4904 fprintf (fp, " -p,--no-params Don't display function parameters\n");
4905 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
4906 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
4911 /* Option specification for getopt_long. */
4912 static const struct option long_options[] =
4914 { "help", no_argument, NULL, 'h' },
4915 { "no-params", no_argument, NULL, 'p' },
4916 { "verbose", no_argument, NULL, 'v' },
4917 { NULL, no_argument, NULL, 0 },
4920 /* Main entry for a demangling filter executable. It will demangle
4921 its command line arguments, if any. If none are provided, it will
4922 filter stdin to stdout, replacing any recognized mangled C++ names
4923 with their demangled equivalents. */
4926 main (int argc, char *argv[])
4930 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
4932 /* Use the program name of this program, as invoked. */
4933 program_name = argv[0];
4935 /* Parse options. */
4938 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
4941 case '?': /* Unrecognized option. */
4942 print_usage (stderr, 1);
4946 print_usage (stdout, 0);
4950 options &= ~ DMGL_PARAMS;
4954 options |= DMGL_VERBOSE;
4958 while (opt_char != -1);
4961 /* No command line arguments were provided. Filter stdin. */
4963 dyn_string_t mangled = dyn_string_new (3);
4966 /* Read all of input. */
4967 while (!feof (stdin))
4971 /* Pile characters into mangled until we hit one that can't
4972 occur in a mangled name. */
4974 while (!feof (stdin) && is_mangled_char (c))
4976 dyn_string_append_char (mangled, c);
4982 if (dyn_string_length (mangled) > 0)
4984 #ifdef IN_GLIBCPP_V3
4985 s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
4987 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
4997 /* It might not have been a mangled name. Print the
4999 fputs (dyn_string_buf (mangled), stdout);
5002 dyn_string_clear (mangled);
5005 /* If we haven't hit EOF yet, we've read one character that
5006 can't occur in a mangled name, so print it out. */
5011 dyn_string_delete (mangled);
5014 /* Demangle command line arguments. */
5016 /* Loop over command line arguments. */
5017 for (i = optind; i < argc; ++i)
5020 #ifdef IN_GLIBCPP_V3
5024 /* Attempt to demangle. */
5025 #ifdef IN_GLIBCPP_V3
5026 s = __cxa_demangle (argv[i], NULL, NULL, &status);
5028 s = cplus_demangle_v3 (argv[i], options);
5031 /* If it worked, print the demangled name. */
5039 #ifdef IN_GLIBCPP_V3
5040 fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
5042 fprintf (stderr, "Failed: %s\n", argv[i]);
5051 #endif /* STANDALONE_DEMANGLER */