1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
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
307 #ifdef CP_DEMANGLE_DEBUG
308 static void d_dump (struct demangle_component *, int);
311 static struct demangle_component *
312 d_make_empty (struct d_info *);
314 static struct demangle_component *
315 d_make_comp (struct d_info *, enum demangle_component_type,
316 struct demangle_component *,
317 struct demangle_component *);
319 static struct demangle_component *
320 d_make_name (struct d_info *, const char *, int);
322 static struct demangle_component *
323 d_make_builtin_type (struct d_info *,
324 const struct demangle_builtin_type_info *);
326 static struct demangle_component *
327 d_make_operator (struct d_info *,
328 const struct demangle_operator_info *);
330 static struct demangle_component *
331 d_make_extended_operator (struct d_info *, int,
332 struct demangle_component *);
334 static struct demangle_component *
335 d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
336 struct demangle_component *);
338 static struct demangle_component *
339 d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
340 struct demangle_component *);
342 static struct demangle_component *
343 d_make_template_param (struct d_info *, long);
345 static struct demangle_component *
346 d_make_sub (struct d_info *, const char *, int);
349 has_return_type (struct demangle_component *);
352 is_ctor_dtor_or_conversion (struct demangle_component *);
354 static struct demangle_component *d_encoding (struct d_info *, int);
356 static struct demangle_component *d_name (struct d_info *);
358 static struct demangle_component *d_nested_name (struct d_info *);
360 static struct demangle_component *d_prefix (struct d_info *);
362 static struct demangle_component *d_unqualified_name (struct d_info *);
364 static struct demangle_component *d_source_name (struct d_info *);
366 static long d_number (struct d_info *);
368 static struct demangle_component *d_identifier (struct d_info *, int);
370 static struct demangle_component *d_operator_name (struct d_info *);
372 static struct demangle_component *d_special_name (struct d_info *);
374 static int d_call_offset (struct d_info *, int);
376 static struct demangle_component *d_ctor_dtor_name (struct d_info *);
378 static struct demangle_component **
379 d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
381 static struct demangle_component *
382 d_function_type (struct d_info *);
384 static struct demangle_component *
385 d_bare_function_type (struct d_info *, int);
387 static struct demangle_component *
388 d_class_enum_type (struct d_info *);
390 static struct demangle_component *d_array_type (struct d_info *);
392 static struct demangle_component *
393 d_pointer_to_member_type (struct d_info *);
395 static struct demangle_component *
396 d_template_param (struct d_info *);
398 static struct demangle_component *d_template_args (struct d_info *);
400 static struct demangle_component *
401 d_template_arg (struct d_info *);
403 static struct demangle_component *d_expression (struct d_info *);
405 static struct demangle_component *d_expr_primary (struct d_info *);
407 static struct demangle_component *d_local_name (struct d_info *);
409 static int d_discriminator (struct d_info *);
412 d_add_substitution (struct d_info *, struct demangle_component *);
414 static struct demangle_component *d_substitution (struct d_info *, int);
416 static void d_growable_string_init (struct d_growable_string *, size_t);
419 d_growable_string_resize (struct d_growable_string *, size_t);
422 d_growable_string_append_buffer (struct d_growable_string *,
423 const char *, size_t);
425 d_growable_string_callback_adapter (const char *, size_t, void *);
428 d_print_init (struct d_print_info *, int, demangle_callbackref, void *);
430 static inline void d_print_error (struct d_print_info *);
432 static inline int d_print_saw_error (struct d_print_info *);
434 static inline void d_print_flush (struct d_print_info *);
436 static inline void d_append_char (struct d_print_info *, char);
438 static inline void d_append_buffer (struct d_print_info *,
439 const char *, size_t);
441 static inline void d_append_string (struct d_print_info *, const char *);
443 static inline char d_last_char (struct d_print_info *);
446 d_print_comp (struct d_print_info *, const struct demangle_component *);
449 d_print_java_identifier (struct d_print_info *, const char *, int);
452 d_print_mod_list (struct d_print_info *, struct d_print_mod *, int);
455 d_print_mod (struct d_print_info *, const struct demangle_component *);
458 d_print_function_type (struct d_print_info *,
459 const struct demangle_component *,
460 struct d_print_mod *);
463 d_print_array_type (struct d_print_info *,
464 const struct demangle_component *,
465 struct d_print_mod *);
468 d_print_expr_op (struct d_print_info *, const struct demangle_component *);
471 d_print_cast (struct d_print_info *, const struct demangle_component *);
473 static int d_demangle_callback (const char *, int,
474 demangle_callbackref, void *);
475 static char *d_demangle (const char *, int, size_t *);
477 #ifdef CP_DEMANGLE_DEBUG
480 d_dump (struct demangle_component *dc, int indent)
487 printf ("failed demangling\n");
491 for (i = 0; i < indent; ++i)
496 case DEMANGLE_COMPONENT_NAME:
497 printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
499 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
500 printf ("template parameter %ld\n", dc->u.s_number.number);
502 case DEMANGLE_COMPONENT_CTOR:
503 printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
504 d_dump (dc->u.s_ctor.name, indent + 2);
506 case DEMANGLE_COMPONENT_DTOR:
507 printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
508 d_dump (dc->u.s_dtor.name, indent + 2);
510 case DEMANGLE_COMPONENT_SUB_STD:
511 printf ("standard substitution %s\n", dc->u.s_string.string);
513 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
514 printf ("builtin type %s\n", dc->u.s_builtin.type->name);
516 case DEMANGLE_COMPONENT_OPERATOR:
517 printf ("operator %s\n", dc->u.s_operator.op->name);
519 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
520 printf ("extended operator with %d args\n",
521 dc->u.s_extended_operator.args);
522 d_dump (dc->u.s_extended_operator.name, indent + 2);
525 case DEMANGLE_COMPONENT_QUAL_NAME:
526 printf ("qualified name\n");
528 case DEMANGLE_COMPONENT_LOCAL_NAME:
529 printf ("local name\n");
531 case DEMANGLE_COMPONENT_TYPED_NAME:
532 printf ("typed name\n");
534 case DEMANGLE_COMPONENT_TEMPLATE:
535 printf ("template\n");
537 case DEMANGLE_COMPONENT_VTABLE:
540 case DEMANGLE_COMPONENT_VTT:
543 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
544 printf ("construction vtable\n");
546 case DEMANGLE_COMPONENT_TYPEINFO:
547 printf ("typeinfo\n");
549 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
550 printf ("typeinfo name\n");
552 case DEMANGLE_COMPONENT_TYPEINFO_FN:
553 printf ("typeinfo function\n");
555 case DEMANGLE_COMPONENT_THUNK:
558 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
559 printf ("virtual thunk\n");
561 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
562 printf ("covariant thunk\n");
564 case DEMANGLE_COMPONENT_JAVA_CLASS:
565 printf ("java class\n");
567 case DEMANGLE_COMPONENT_GUARD:
570 case DEMANGLE_COMPONENT_REFTEMP:
571 printf ("reference temporary\n");
573 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
574 printf ("hidden alias\n");
576 case DEMANGLE_COMPONENT_RESTRICT:
577 printf ("restrict\n");
579 case DEMANGLE_COMPONENT_VOLATILE:
580 printf ("volatile\n");
582 case DEMANGLE_COMPONENT_CONST:
585 case DEMANGLE_COMPONENT_RESTRICT_THIS:
586 printf ("restrict this\n");
588 case DEMANGLE_COMPONENT_VOLATILE_THIS:
589 printf ("volatile this\n");
591 case DEMANGLE_COMPONENT_CONST_THIS:
592 printf ("const this\n");
594 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
595 printf ("vendor type qualifier\n");
597 case DEMANGLE_COMPONENT_POINTER:
598 printf ("pointer\n");
600 case DEMANGLE_COMPONENT_REFERENCE:
601 printf ("reference\n");
603 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
604 printf ("rvalue reference\n");
606 case DEMANGLE_COMPONENT_COMPLEX:
607 printf ("complex\n");
609 case DEMANGLE_COMPONENT_IMAGINARY:
610 printf ("imaginary\n");
612 case DEMANGLE_COMPONENT_VENDOR_TYPE:
613 printf ("vendor type\n");
615 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
616 printf ("function type\n");
618 case DEMANGLE_COMPONENT_ARRAY_TYPE:
619 printf ("array type\n");
621 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
622 printf ("pointer to member type\n");
624 case DEMANGLE_COMPONENT_FIXED_TYPE:
625 printf ("fixed-point type\n");
627 case DEMANGLE_COMPONENT_ARGLIST:
628 printf ("argument list\n");
630 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
631 printf ("template argument list\n");
633 case DEMANGLE_COMPONENT_CAST:
636 case DEMANGLE_COMPONENT_UNARY:
637 printf ("unary operator\n");
639 case DEMANGLE_COMPONENT_BINARY:
640 printf ("binary operator\n");
642 case DEMANGLE_COMPONENT_BINARY_ARGS:
643 printf ("binary operator arguments\n");
645 case DEMANGLE_COMPONENT_TRINARY:
646 printf ("trinary operator\n");
648 case DEMANGLE_COMPONENT_TRINARY_ARG1:
649 printf ("trinary operator arguments 1\n");
651 case DEMANGLE_COMPONENT_TRINARY_ARG2:
652 printf ("trinary operator arguments 1\n");
654 case DEMANGLE_COMPONENT_LITERAL:
655 printf ("literal\n");
657 case DEMANGLE_COMPONENT_LITERAL_NEG:
658 printf ("negative literal\n");
660 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
661 printf ("java resource\n");
663 case DEMANGLE_COMPONENT_COMPOUND_NAME:
664 printf ("compound name\n");
666 case DEMANGLE_COMPONENT_CHARACTER:
667 printf ("character '%c'\n", dc->u.s_character.character);
669 case DEMANGLE_COMPONENT_DECLTYPE:
670 printf ("decltype\n");
672 case DEMANGLE_COMPONENT_PACK_EXPANSION:
673 printf ("pack expansion\n");
677 d_dump (d_left (dc), indent + 2);
678 d_dump (d_right (dc), indent + 2);
681 #endif /* CP_DEMANGLE_DEBUG */
683 /* Fill in a DEMANGLE_COMPONENT_NAME. */
685 CP_STATIC_IF_GLIBCPP_V3
687 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
689 if (p == NULL || s == NULL || len == 0)
691 p->type = DEMANGLE_COMPONENT_NAME;
693 p->u.s_name.len = len;
697 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
699 CP_STATIC_IF_GLIBCPP_V3
701 cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
702 struct demangle_component *name)
704 if (p == NULL || args < 0 || name == NULL)
706 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
707 p->u.s_extended_operator.args = args;
708 p->u.s_extended_operator.name = name;
712 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
714 CP_STATIC_IF_GLIBCPP_V3
716 cplus_demangle_fill_ctor (struct demangle_component *p,
717 enum gnu_v3_ctor_kinds kind,
718 struct demangle_component *name)
722 || (kind < gnu_v3_complete_object_ctor
723 && kind > gnu_v3_complete_object_allocating_ctor))
725 p->type = DEMANGLE_COMPONENT_CTOR;
726 p->u.s_ctor.kind = kind;
727 p->u.s_ctor.name = name;
731 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
733 CP_STATIC_IF_GLIBCPP_V3
735 cplus_demangle_fill_dtor (struct demangle_component *p,
736 enum gnu_v3_dtor_kinds kind,
737 struct demangle_component *name)
741 || (kind < gnu_v3_deleting_dtor
742 && kind > gnu_v3_base_object_dtor))
744 p->type = DEMANGLE_COMPONENT_DTOR;
745 p->u.s_dtor.kind = kind;
746 p->u.s_dtor.name = name;
750 /* Add a new component. */
752 static struct demangle_component *
753 d_make_empty (struct d_info *di)
755 struct demangle_component *p;
757 if (di->next_comp >= di->num_comps)
759 p = &di->comps[di->next_comp];
764 /* Add a new generic component. */
766 static struct demangle_component *
767 d_make_comp (struct d_info *di, enum demangle_component_type type,
768 struct demangle_component *left,
769 struct demangle_component *right)
771 struct demangle_component *p;
773 /* We check for errors here. A typical error would be a NULL return
774 from a subroutine. We catch those here, and return NULL
778 /* These types require two parameters. */
779 case DEMANGLE_COMPONENT_QUAL_NAME:
780 case DEMANGLE_COMPONENT_LOCAL_NAME:
781 case DEMANGLE_COMPONENT_TYPED_NAME:
782 case DEMANGLE_COMPONENT_TEMPLATE:
783 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
784 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
785 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
786 case DEMANGLE_COMPONENT_UNARY:
787 case DEMANGLE_COMPONENT_BINARY:
788 case DEMANGLE_COMPONENT_BINARY_ARGS:
789 case DEMANGLE_COMPONENT_TRINARY:
790 case DEMANGLE_COMPONENT_TRINARY_ARG1:
791 case DEMANGLE_COMPONENT_TRINARY_ARG2:
792 case DEMANGLE_COMPONENT_LITERAL:
793 case DEMANGLE_COMPONENT_LITERAL_NEG:
794 case DEMANGLE_COMPONENT_COMPOUND_NAME:
795 if (left == NULL || right == NULL)
799 /* These types only require one parameter. */
800 case DEMANGLE_COMPONENT_VTABLE:
801 case DEMANGLE_COMPONENT_VTT:
802 case DEMANGLE_COMPONENT_TYPEINFO:
803 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
804 case DEMANGLE_COMPONENT_TYPEINFO_FN:
805 case DEMANGLE_COMPONENT_THUNK:
806 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
807 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
808 case DEMANGLE_COMPONENT_JAVA_CLASS:
809 case DEMANGLE_COMPONENT_GUARD:
810 case DEMANGLE_COMPONENT_REFTEMP:
811 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
812 case DEMANGLE_COMPONENT_POINTER:
813 case DEMANGLE_COMPONENT_REFERENCE:
814 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
815 case DEMANGLE_COMPONENT_COMPLEX:
816 case DEMANGLE_COMPONENT_IMAGINARY:
817 case DEMANGLE_COMPONENT_VENDOR_TYPE:
818 case DEMANGLE_COMPONENT_CAST:
819 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
820 case DEMANGLE_COMPONENT_DECLTYPE:
821 case DEMANGLE_COMPONENT_PACK_EXPANSION:
826 /* This needs a right parameter, but the left parameter can be
828 case DEMANGLE_COMPONENT_ARRAY_TYPE:
833 /* These are allowed to have no parameters--in some cases they
834 will be filled in later. */
835 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
836 case DEMANGLE_COMPONENT_RESTRICT:
837 case DEMANGLE_COMPONENT_VOLATILE:
838 case DEMANGLE_COMPONENT_CONST:
839 case DEMANGLE_COMPONENT_RESTRICT_THIS:
840 case DEMANGLE_COMPONENT_VOLATILE_THIS:
841 case DEMANGLE_COMPONENT_CONST_THIS:
842 case DEMANGLE_COMPONENT_ARGLIST:
843 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
846 /* Other types should not be seen here. */
851 p = d_make_empty (di);
855 p->u.s_binary.left = left;
856 p->u.s_binary.right = right;
861 /* Add a new name component. */
863 static struct demangle_component *
864 d_make_name (struct d_info *di, const char *s, int len)
866 struct demangle_component *p;
868 p = d_make_empty (di);
869 if (! cplus_demangle_fill_name (p, s, len))
874 /* Add a new builtin type component. */
876 static struct demangle_component *
877 d_make_builtin_type (struct d_info *di,
878 const struct demangle_builtin_type_info *type)
880 struct demangle_component *p;
884 p = d_make_empty (di);
887 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
888 p->u.s_builtin.type = type;
893 /* Add a new operator component. */
895 static struct demangle_component *
896 d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
898 struct demangle_component *p;
900 p = d_make_empty (di);
903 p->type = DEMANGLE_COMPONENT_OPERATOR;
904 p->u.s_operator.op = op;
909 /* Add a new extended operator component. */
911 static struct demangle_component *
912 d_make_extended_operator (struct d_info *di, int args,
913 struct demangle_component *name)
915 struct demangle_component *p;
917 p = d_make_empty (di);
918 if (! cplus_demangle_fill_extended_operator (p, args, name))
923 /* Add a new constructor component. */
925 static struct demangle_component *
926 d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
927 struct demangle_component *name)
929 struct demangle_component *p;
931 p = d_make_empty (di);
932 if (! cplus_demangle_fill_ctor (p, kind, name))
937 /* Add a new destructor component. */
939 static struct demangle_component *
940 d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
941 struct demangle_component *name)
943 struct demangle_component *p;
945 p = d_make_empty (di);
946 if (! cplus_demangle_fill_dtor (p, kind, name))
951 /* Add a new template parameter. */
953 static struct demangle_component *
954 d_make_template_param (struct d_info *di, long i)
956 struct demangle_component *p;
958 p = d_make_empty (di);
961 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
962 p->u.s_number.number = i;
967 /* Add a new function parameter. */
969 static struct demangle_component *
970 d_make_function_param (struct d_info *di, long i)
972 struct demangle_component *p;
974 p = d_make_empty (di);
977 p->type = DEMANGLE_COMPONENT_FUNCTION_PARAM;
978 p->u.s_number.number = i;
983 /* Add a new standard substitution component. */
985 static struct demangle_component *
986 d_make_sub (struct d_info *di, const char *name, int len)
988 struct demangle_component *p;
990 p = d_make_empty (di);
993 p->type = DEMANGLE_COMPONENT_SUB_STD;
994 p->u.s_string.string = name;
995 p->u.s_string.len = len;
1000 /* <mangled-name> ::= _Z <encoding>
1002 TOP_LEVEL is non-zero when called at the top level. */
1004 CP_STATIC_IF_GLIBCPP_V3
1005 struct demangle_component *
1006 cplus_demangle_mangled_name (struct d_info *di, int top_level)
1008 if (! d_check_char (di, '_')
1009 /* Allow missing _ if not at toplevel to work around a
1010 bug in G++ abi-version=2 mangling; see the comment in
1011 write_template_arg. */
1014 if (! d_check_char (di, 'Z'))
1016 return d_encoding (di, top_level);
1019 /* Return whether a function should have a return type. The argument
1020 is the function name, which may be qualified in various ways. The
1021 rules are that template functions have return types with some
1022 exceptions, function types which are not part of a function name
1023 mangling have return types with some exceptions, and non-template
1024 function names do not have return types. The exceptions are that
1025 constructors, destructors, and conversion operators do not have
1029 has_return_type (struct demangle_component *dc)
1037 case DEMANGLE_COMPONENT_TEMPLATE:
1038 return ! is_ctor_dtor_or_conversion (d_left (dc));
1039 case DEMANGLE_COMPONENT_RESTRICT_THIS:
1040 case DEMANGLE_COMPONENT_VOLATILE_THIS:
1041 case DEMANGLE_COMPONENT_CONST_THIS:
1042 return has_return_type (d_left (dc));
1046 /* Return whether a name is a constructor, a destructor, or a
1047 conversion operator. */
1050 is_ctor_dtor_or_conversion (struct demangle_component *dc)
1058 case DEMANGLE_COMPONENT_QUAL_NAME:
1059 case DEMANGLE_COMPONENT_LOCAL_NAME:
1060 return is_ctor_dtor_or_conversion (d_right (dc));
1061 case DEMANGLE_COMPONENT_CTOR:
1062 case DEMANGLE_COMPONENT_DTOR:
1063 case DEMANGLE_COMPONENT_CAST:
1068 /* <encoding> ::= <(function) name> <bare-function-type>
1072 TOP_LEVEL is non-zero when called at the top level, in which case
1073 if DMGL_PARAMS is not set we do not demangle the function
1074 parameters. We only set this at the top level, because otherwise
1075 we would not correctly demangle names in local scopes. */
1077 static struct demangle_component *
1078 d_encoding (struct d_info *di, int top_level)
1080 char peek = d_peek_char (di);
1082 if (peek == 'G' || peek == 'T')
1083 return d_special_name (di);
1086 struct demangle_component *dc;
1090 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
1092 /* Strip off any initial CV-qualifiers, as they really apply
1093 to the `this' parameter, and they were not output by the
1094 v2 demangler without DMGL_PARAMS. */
1095 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1096 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1097 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
1100 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1101 there may be CV-qualifiers on its right argument which
1102 really apply here; this happens when parsing a class
1103 which is local to a function. */
1104 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1106 struct demangle_component *dcr;
1109 while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1110 || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1111 || dcr->type == DEMANGLE_COMPONENT_CONST_THIS)
1113 dc->u.s_binary.right = dcr;
1119 peek = d_peek_char (di);
1120 if (dc == NULL || peek == '\0' || peek == 'E')
1122 return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
1123 d_bare_function_type (di, has_return_type (dc)));
1127 /* <name> ::= <nested-name>
1129 ::= <unscoped-template-name> <template-args>
1132 <unscoped-name> ::= <unqualified-name>
1133 ::= St <unqualified-name>
1135 <unscoped-template-name> ::= <unscoped-name>
1139 static struct demangle_component *
1140 d_name (struct d_info *di)
1142 char peek = d_peek_char (di);
1143 struct demangle_component *dc;
1148 return d_nested_name (di);
1151 return d_local_name (di);
1154 return d_unqualified_name (di);
1160 if (d_peek_next_char (di) != 't')
1162 dc = d_substitution (di, 0);
1168 dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
1169 d_make_name (di, "std", 3),
1170 d_unqualified_name (di));
1175 if (d_peek_char (di) != 'I')
1177 /* The grammar does not permit this case to occur if we
1178 called d_substitution() above (i.e., subst == 1). We
1179 don't bother to check. */
1183 /* This is <template-args>, which means that we just saw
1184 <unscoped-template-name>, which is a substitution
1185 candidate if we didn't just get it from a
1189 if (! d_add_substitution (di, dc))
1192 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1193 d_template_args (di));
1200 dc = d_unqualified_name (di);
1201 if (d_peek_char (di) == 'I')
1203 /* This is <template-args>, which means that we just saw
1204 <unscoped-template-name>, which is a substitution
1206 if (! d_add_substitution (di, dc))
1208 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1209 d_template_args (di));
1215 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1216 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1219 static struct demangle_component *
1220 d_nested_name (struct d_info *di)
1222 struct demangle_component *ret;
1223 struct demangle_component **pret;
1225 if (! d_check_char (di, 'N'))
1228 pret = d_cv_qualifiers (di, &ret, 1);
1232 *pret = d_prefix (di);
1236 if (! d_check_char (di, 'E'))
1242 /* <prefix> ::= <prefix> <unqualified-name>
1243 ::= <template-prefix> <template-args>
1244 ::= <template-param>
1248 <template-prefix> ::= <prefix> <(template) unqualified-name>
1249 ::= <template-param>
1253 static struct demangle_component *
1254 d_prefix (struct d_info *di)
1256 struct demangle_component *ret = NULL;
1261 enum demangle_component_type comb_type;
1262 struct demangle_component *dc;
1264 peek = d_peek_char (di);
1268 /* The older code accepts a <local-name> here, but I don't see
1269 that in the grammar. The older code does not accept a
1270 <template-param> here. */
1272 comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
1278 dc = d_unqualified_name (di);
1279 else if (peek == 'S')
1280 dc = d_substitution (di, 1);
1281 else if (peek == 'I')
1285 comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1286 dc = d_template_args (di);
1288 else if (peek == 'T')
1289 dc = d_template_param (di);
1290 else if (peek == 'E')
1298 ret = d_make_comp (di, comb_type, ret, dc);
1300 if (peek != 'S' && d_peek_char (di) != 'E')
1302 if (! d_add_substitution (di, ret))
1308 /* <unqualified-name> ::= <operator-name>
1309 ::= <ctor-dtor-name>
1311 ::= <local-source-name>
1313 <local-source-name> ::= L <source-name> <discriminator>
1316 static struct demangle_component *
1317 d_unqualified_name (struct d_info *di)
1321 peek = d_peek_char (di);
1322 if (IS_DIGIT (peek))
1323 return d_source_name (di);
1324 else if (IS_LOWER (peek))
1326 struct demangle_component *ret;
1328 ret = d_operator_name (di);
1329 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1330 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1333 else if (peek == 'C' || peek == 'D')
1334 return d_ctor_dtor_name (di);
1335 else if (peek == 'L')
1337 struct demangle_component * ret;
1341 ret = d_source_name (di);
1344 if (! d_discriminator (di))
1352 /* <source-name> ::= <(positive length) number> <identifier> */
1354 static struct demangle_component *
1355 d_source_name (struct d_info *di)
1358 struct demangle_component *ret;
1360 len = d_number (di);
1363 ret = d_identifier (di, len);
1364 di->last_name = ret;
1368 /* number ::= [n] <(non-negative decimal integer)> */
1371 d_number (struct d_info *di)
1378 peek = d_peek_char (di);
1383 peek = d_peek_char (di);
1389 if (! IS_DIGIT (peek))
1395 ret = ret * 10 + peek - '0';
1397 peek = d_peek_char (di);
1401 /* identifier ::= <(unqualified source code identifier)> */
1403 static struct demangle_component *
1404 d_identifier (struct d_info *di, int len)
1410 if (di->send - name < len)
1413 d_advance (di, len);
1415 /* A Java mangled name may have a trailing '$' if it is a C++
1416 keyword. This '$' is not included in the length count. We just
1418 if ((di->options & DMGL_JAVA) != 0
1419 && d_peek_char (di) == '$')
1422 /* Look for something which looks like a gcc encoding of an
1423 anonymous namespace, and replace it with a more user friendly
1425 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1426 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1427 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1431 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1432 if ((*s == '.' || *s == '_' || *s == '$')
1435 di->expansion -= len - sizeof "(anonymous namespace)";
1436 return d_make_name (di, "(anonymous namespace)",
1437 sizeof "(anonymous namespace)" - 1);
1441 return d_make_name (di, name, len);
1444 /* operator_name ::= many different two character encodings.
1446 ::= v <digit> <source-name>
1449 #define NL(s) s, (sizeof s) - 1
1451 CP_STATIC_IF_GLIBCPP_V3
1452 const struct demangle_operator_info cplus_demangle_operators[] =
1454 { "aN", NL ("&="), 2 },
1455 { "aS", NL ("="), 2 },
1456 { "aa", NL ("&&"), 2 },
1457 { "ad", NL ("&"), 1 },
1458 { "an", NL ("&"), 2 },
1459 { "cl", NL ("()"), 2 },
1460 { "cm", NL (","), 2 },
1461 { "co", NL ("~"), 1 },
1462 { "dV", NL ("/="), 2 },
1463 { "da", NL ("delete[]"), 1 },
1464 { "de", NL ("*"), 1 },
1465 { "dl", NL ("delete"), 1 },
1466 { "dt", NL ("."), 2 },
1467 { "dv", NL ("/"), 2 },
1468 { "eO", NL ("^="), 2 },
1469 { "eo", NL ("^"), 2 },
1470 { "eq", NL ("=="), 2 },
1471 { "ge", NL (">="), 2 },
1472 { "gt", NL (">"), 2 },
1473 { "ix", NL ("[]"), 2 },
1474 { "lS", NL ("<<="), 2 },
1475 { "le", NL ("<="), 2 },
1476 { "ls", NL ("<<"), 2 },
1477 { "lt", NL ("<"), 2 },
1478 { "mI", NL ("-="), 2 },
1479 { "mL", NL ("*="), 2 },
1480 { "mi", NL ("-"), 2 },
1481 { "ml", NL ("*"), 2 },
1482 { "mm", NL ("--"), 1 },
1483 { "na", NL ("new[]"), 1 },
1484 { "ne", NL ("!="), 2 },
1485 { "ng", NL ("-"), 1 },
1486 { "nt", NL ("!"), 1 },
1487 { "nw", NL ("new"), 1 },
1488 { "oR", NL ("|="), 2 },
1489 { "oo", NL ("||"), 2 },
1490 { "or", NL ("|"), 2 },
1491 { "pL", NL ("+="), 2 },
1492 { "pl", NL ("+"), 2 },
1493 { "pm", NL ("->*"), 2 },
1494 { "pp", NL ("++"), 1 },
1495 { "ps", NL ("+"), 1 },
1496 { "pt", NL ("->"), 2 },
1497 { "qu", NL ("?"), 3 },
1498 { "rM", NL ("%="), 2 },
1499 { "rS", NL (">>="), 2 },
1500 { "rm", NL ("%"), 2 },
1501 { "rs", NL (">>"), 2 },
1502 { "st", NL ("sizeof "), 1 },
1503 { "sz", NL ("sizeof "), 1 },
1504 { "at", NL ("alignof "), 1 },
1505 { "az", NL ("alignof "), 1 },
1506 { NULL, NULL, 0, 0 }
1509 static struct demangle_component *
1510 d_operator_name (struct d_info *di)
1515 c1 = d_next_char (di);
1516 c2 = d_next_char (di);
1517 if (c1 == 'v' && IS_DIGIT (c2))
1518 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1519 else if (c1 == 'c' && c2 == 'v')
1520 return d_make_comp (di, DEMANGLE_COMPONENT_CAST,
1521 cplus_demangle_type (di), NULL);
1524 /* LOW is the inclusive lower bound. */
1526 /* HIGH is the exclusive upper bound. We subtract one to ignore
1527 the sentinel at the end of the array. */
1528 int high = ((sizeof (cplus_demangle_operators)
1529 / sizeof (cplus_demangle_operators[0]))
1535 const struct demangle_operator_info *p;
1537 i = low + (high - low) / 2;
1538 p = cplus_demangle_operators + i;
1540 if (c1 == p->code[0] && c2 == p->code[1])
1541 return d_make_operator (di, p);
1543 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1553 static struct demangle_component *
1554 d_make_character (struct d_info *di, int c)
1556 struct demangle_component *p;
1557 p = d_make_empty (di);
1560 p->type = DEMANGLE_COMPONENT_CHARACTER;
1561 p->u.s_character.character = c;
1566 static struct demangle_component *
1567 d_java_resource (struct d_info *di)
1569 struct demangle_component *p = NULL;
1570 struct demangle_component *next = NULL;
1575 len = d_number (di);
1579 /* Eat the leading '_'. */
1580 if (d_next_char (di) != '_')
1593 /* Each chunk is either a '$' escape... */
1611 next = d_make_character (di, c);
1619 /* ... or a sequence of characters. */
1622 while (i < len && str[i] && str[i] != '$')
1625 next = d_make_name (di, str, i);
1638 p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next);
1644 p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL);
1649 /* <special-name> ::= TV <type>
1653 ::= GV <(object) name>
1654 ::= T <call-offset> <(base) encoding>
1655 ::= Tc <call-offset> <call-offset> <(base) encoding>
1656 Also g++ extensions:
1657 ::= TC <type> <(offset) number> _ <(base) type>
1662 ::= Gr <resource name>
1665 static struct demangle_component *
1666 d_special_name (struct d_info *di)
1668 di->expansion += 20;
1669 if (d_check_char (di, 'T'))
1671 switch (d_next_char (di))
1675 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
1676 cplus_demangle_type (di), NULL);
1678 di->expansion -= 10;
1679 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
1680 cplus_demangle_type (di), NULL);
1682 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
1683 cplus_demangle_type (di), NULL);
1685 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
1686 cplus_demangle_type (di), NULL);
1689 if (! d_call_offset (di, 'h'))
1691 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
1692 d_encoding (di, 0), NULL);
1695 if (! d_call_offset (di, 'v'))
1697 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
1698 d_encoding (di, 0), NULL);
1701 if (! d_call_offset (di, '\0'))
1703 if (! d_call_offset (di, '\0'))
1705 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
1706 d_encoding (di, 0), NULL);
1710 struct demangle_component *derived_type;
1712 struct demangle_component *base_type;
1714 derived_type = cplus_demangle_type (di);
1715 offset = d_number (di);
1718 if (! d_check_char (di, '_'))
1720 base_type = cplus_demangle_type (di);
1721 /* We don't display the offset. FIXME: We should display
1722 it in verbose mode. */
1724 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
1725 base_type, derived_type);
1729 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
1730 cplus_demangle_type (di), NULL);
1732 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
1733 cplus_demangle_type (di), NULL);
1739 else if (d_check_char (di, 'G'))
1741 switch (d_next_char (di))
1744 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
1747 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, d_name (di),
1751 return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
1752 d_encoding (di, 0), NULL);
1755 return d_java_resource (di);
1765 /* <call-offset> ::= h <nv-offset> _
1768 <nv-offset> ::= <(offset) number>
1770 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1772 The C parameter, if not '\0', is a character we just read which is
1773 the start of the <call-offset>.
1775 We don't display the offset information anywhere. FIXME: We should
1776 display it in verbose mode. */
1779 d_call_offset (struct d_info *di, int c)
1782 c = d_next_char (di);
1789 if (! d_check_char (di, '_'))
1796 if (! d_check_char (di, '_'))
1802 /* <ctor-dtor-name> ::= C1
1810 static struct demangle_component *
1811 d_ctor_dtor_name (struct d_info *di)
1813 if (di->last_name != NULL)
1815 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
1816 di->expansion += di->last_name->u.s_name.len;
1817 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
1818 di->expansion += di->last_name->u.s_string.len;
1820 switch (d_peek_char (di))
1824 enum gnu_v3_ctor_kinds kind;
1826 switch (d_peek_next_char (di))
1829 kind = gnu_v3_complete_object_ctor;
1832 kind = gnu_v3_base_object_ctor;
1835 kind = gnu_v3_complete_object_allocating_ctor;
1841 return d_make_ctor (di, kind, di->last_name);
1846 enum gnu_v3_dtor_kinds kind;
1848 switch (d_peek_next_char (di))
1851 kind = gnu_v3_deleting_dtor;
1854 kind = gnu_v3_complete_object_dtor;
1857 kind = gnu_v3_base_object_dtor;
1863 return d_make_dtor (di, kind, di->last_name);
1871 /* <type> ::= <builtin-type>
1873 ::= <class-enum-type>
1875 ::= <pointer-to-member-type>
1876 ::= <template-param>
1877 ::= <template-template-param> <template-args>
1879 ::= <CV-qualifiers> <type>
1882 ::= O <type> (C++0x)
1885 ::= U <source-name> <type>
1887 <builtin-type> ::= various one letter codes
1891 CP_STATIC_IF_GLIBCPP_V3
1892 const struct demangle_builtin_type_info
1893 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
1895 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
1896 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
1897 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
1898 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
1899 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
1900 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
1901 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
1902 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
1903 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
1904 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
1905 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1906 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
1907 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
1908 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
1909 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
1911 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1912 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1913 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1914 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
1915 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
1916 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1917 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
1918 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
1919 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
1920 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
1921 D_PRINT_UNSIGNED_LONG_LONG },
1922 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
1923 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT },
1924 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT },
1925 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT },
1926 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT },
1927 /* 30 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT },
1928 /* 31 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT },
1931 CP_STATIC_IF_GLIBCPP_V3
1932 struct demangle_component *
1933 cplus_demangle_type (struct d_info *di)
1936 struct demangle_component *ret;
1939 /* The ABI specifies that when CV-qualifiers are used, the base type
1940 is substitutable, and the fully qualified type is substitutable,
1941 but the base type with a strict subset of the CV-qualifiers is
1942 not substitutable. The natural recursive implementation of the
1943 CV-qualifiers would cause subsets to be substitutable, so instead
1944 we pull them all off now.
1946 FIXME: The ABI says that order-insensitive vendor qualifiers
1947 should be handled in the same way, but we have no way to tell
1948 which vendor qualifiers are order-insensitive and which are
1949 order-sensitive. So we just assume that they are all
1950 order-sensitive. g++ 3.4 supports only one vendor qualifier,
1951 __vector, and it treats it as order-sensitive when mangling
1954 peek = d_peek_char (di);
1955 if (peek == 'r' || peek == 'V' || peek == 'K')
1957 struct demangle_component **pret;
1959 pret = d_cv_qualifiers (di, &ret, 0);
1962 *pret = cplus_demangle_type (di);
1963 if (! *pret || ! d_add_substitution (di, ret))
1972 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1973 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
1974 case 'o': case 's': case 't':
1975 case 'v': case 'w': case 'x': case 'y': case 'z':
1976 ret = d_make_builtin_type (di,
1977 &cplus_demangle_builtin_types[peek - 'a']);
1978 di->expansion += ret->u.s_builtin.type->len;
1985 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
1986 d_source_name (di), NULL);
1990 ret = d_function_type (di);
1993 case '0': case '1': case '2': case '3': case '4':
1994 case '5': case '6': case '7': case '8': case '9':
1997 ret = d_class_enum_type (di);
2001 ret = d_array_type (di);
2005 ret = d_pointer_to_member_type (di);
2009 ret = d_template_param (di);
2010 if (d_peek_char (di) == 'I')
2012 /* This is <template-template-param> <template-args>. The
2013 <template-template-param> part is a substitution
2015 if (! d_add_substitution (di, ret))
2017 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2018 d_template_args (di));
2023 /* If this is a special substitution, then it is the start of
2024 <class-enum-type>. */
2028 peek_next = d_peek_next_char (di);
2029 if (IS_DIGIT (peek_next)
2031 || IS_UPPER (peek_next))
2033 ret = d_substitution (di, 0);
2034 /* The substituted name may have been a template name and
2035 may be followed by tepmlate args. */
2036 if (d_peek_char (di) == 'I')
2037 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2038 d_template_args (di));
2044 ret = d_class_enum_type (di);
2045 /* If the substitution was a complete type, then it is not
2046 a new substitution candidate. However, if the
2047 substitution was followed by template arguments, then
2048 the whole thing is a substitution candidate. */
2049 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
2057 ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE,
2058 cplus_demangle_type (di), NULL);
2063 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
2064 cplus_demangle_type (di), NULL);
2069 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
2070 cplus_demangle_type (di), NULL);
2075 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
2076 cplus_demangle_type (di), NULL);
2081 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
2082 cplus_demangle_type (di), NULL);
2087 ret = d_source_name (di);
2088 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
2089 cplus_demangle_type (di), ret);
2095 peek = d_next_char (di);
2100 /* decltype (expression) */
2101 ret = d_make_comp (di, DEMANGLE_COMPONENT_DECLTYPE,
2102 d_expression (di), NULL);
2103 if (ret && d_next_char (di) != 'E')
2108 /* Pack expansion. */
2109 ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2110 cplus_demangle_type (di), NULL);
2114 /* 32-bit decimal floating point */
2115 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[26]);
2116 di->expansion += ret->u.s_builtin.type->len;
2120 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[27]);
2121 di->expansion += ret->u.s_builtin.type->len;
2125 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[28]);
2126 di->expansion += ret->u.s_builtin.type->len;
2129 /* 16-bit half-precision FP */
2130 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[29]);
2131 di->expansion += ret->u.s_builtin.type->len;
2135 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[30]);
2136 di->expansion += ret->u.s_builtin.type->len;
2140 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[31]);
2141 di->expansion += ret->u.s_builtin.type->len;
2145 /* Fixed point types. DF<int bits><length><fract bits><sat> */
2146 ret = d_make_empty (di);
2147 ret->type = DEMANGLE_COMPONENT_FIXED_TYPE;
2148 if ((ret->u.s_fixed.accum = IS_DIGIT (d_peek_char (di))))
2149 /* For demangling we don't care about the bits. */
2151 ret->u.s_fixed.length = cplus_demangle_type (di);
2153 peek = d_next_char (di);
2154 ret->u.s_fixed.sat = (peek == 's');
2168 if (! d_add_substitution (di, ret))
2175 /* <CV-qualifiers> ::= [r] [V] [K] */
2177 static struct demangle_component **
2178 d_cv_qualifiers (struct d_info *di,
2179 struct demangle_component **pret, int member_fn)
2183 peek = d_peek_char (di);
2184 while (peek == 'r' || peek == 'V' || peek == 'K')
2186 enum demangle_component_type t;
2192 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2193 : DEMANGLE_COMPONENT_RESTRICT);
2194 di->expansion += sizeof "restrict";
2196 else if (peek == 'V')
2199 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2200 : DEMANGLE_COMPONENT_VOLATILE);
2201 di->expansion += sizeof "volatile";
2206 ? DEMANGLE_COMPONENT_CONST_THIS
2207 : DEMANGLE_COMPONENT_CONST);
2208 di->expansion += sizeof "const";
2211 *pret = d_make_comp (di, t, NULL, NULL);
2214 pret = &d_left (*pret);
2216 peek = d_peek_char (di);
2222 /* <function-type> ::= F [Y] <bare-function-type> E */
2224 static struct demangle_component *
2225 d_function_type (struct d_info *di)
2227 struct demangle_component *ret;
2229 if (! d_check_char (di, 'F'))
2231 if (d_peek_char (di) == 'Y')
2233 /* Function has C linkage. We don't print this information.
2234 FIXME: We should print it in verbose mode. */
2237 ret = d_bare_function_type (di, 1);
2238 if (! d_check_char (di, 'E'))
2243 /* <bare-function-type> ::= [J]<type>+ */
2245 static struct demangle_component *
2246 d_bare_function_type (struct d_info *di, int has_return_type)
2248 struct demangle_component *return_type;
2249 struct demangle_component *tl;
2250 struct demangle_component **ptl;
2253 /* Detect special qualifier indicating that the first argument
2254 is the return type. */
2255 peek = d_peek_char (di);
2259 has_return_type = 1;
2267 struct demangle_component *type;
2269 peek = d_peek_char (di);
2270 if (peek == '\0' || peek == 'E')
2272 type = cplus_demangle_type (di);
2275 if (has_return_type)
2278 has_return_type = 0;
2282 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
2285 ptl = &d_right (*ptl);
2289 /* There should be at least one parameter type besides the optional
2290 return type. A function which takes no arguments will have a
2291 single parameter type void. */
2295 /* If we have a single parameter type void, omit it. */
2296 if (d_right (tl) == NULL
2297 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2298 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
2300 di->expansion -= d_left (tl)->u.s_builtin.type->len;
2304 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE, return_type, tl);
2307 /* <class-enum-type> ::= <name> */
2309 static struct demangle_component *
2310 d_class_enum_type (struct d_info *di)
2315 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2316 ::= A [<(dimension) expression>] _ <(element) type>
2319 static struct demangle_component *
2320 d_array_type (struct d_info *di)
2323 struct demangle_component *dim;
2325 if (! d_check_char (di, 'A'))
2328 peek = d_peek_char (di);
2331 else if (IS_DIGIT (peek))
2339 peek = d_peek_char (di);
2341 while (IS_DIGIT (peek));
2342 dim = d_make_name (di, s, d_str (di) - s);
2348 dim = d_expression (di);
2353 if (! d_check_char (di, '_'))
2356 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2357 cplus_demangle_type (di));
2360 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2362 static struct demangle_component *
2363 d_pointer_to_member_type (struct d_info *di)
2365 struct demangle_component *cl;
2366 struct demangle_component *mem;
2367 struct demangle_component **pmem;
2369 if (! d_check_char (di, 'M'))
2372 cl = cplus_demangle_type (di);
2374 /* The ABI specifies that any type can be a substitution source, and
2375 that M is followed by two types, and that when a CV-qualified
2376 type is seen both the base type and the CV-qualified types are
2377 substitution sources. The ABI also specifies that for a pointer
2378 to a CV-qualified member function, the qualifiers are attached to
2379 the second type. Given the grammar, a plain reading of the ABI
2380 suggests that both the CV-qualified member function and the
2381 non-qualified member function are substitution sources. However,
2382 g++ does not work that way. g++ treats only the CV-qualified
2383 member function as a substitution source. FIXME. So to work
2384 with g++, we need to pull off the CV-qualifiers here, in order to
2385 avoid calling add_substitution() in cplus_demangle_type(). But
2386 for a CV-qualified member which is not a function, g++ does
2387 follow the ABI, so we need to handle that case here by calling
2388 d_add_substitution ourselves. */
2390 pmem = d_cv_qualifiers (di, &mem, 1);
2393 *pmem = cplus_demangle_type (di);
2397 if (pmem != &mem && (*pmem)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
2399 if (! d_add_substitution (di, mem))
2403 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
2406 /* <template-param> ::= T_
2407 ::= T <(parameter-2 non-negative) number> _
2410 static struct demangle_component *
2411 d_template_param (struct d_info *di)
2415 if (! d_check_char (di, 'T'))
2418 if (d_peek_char (di) == '_')
2422 param = d_number (di);
2428 if (! d_check_char (di, '_'))
2433 return d_make_template_param (di, param);
2436 /* <template-args> ::= I <template-arg>+ E */
2438 static struct demangle_component *
2439 d_template_args (struct d_info *di)
2441 struct demangle_component *hold_last_name;
2442 struct demangle_component *al;
2443 struct demangle_component **pal;
2445 /* Preserve the last name we saw--don't let the template arguments
2446 clobber it, as that would give us the wrong name for a subsequent
2447 constructor or destructor. */
2448 hold_last_name = di->last_name;
2450 if (! d_check_char (di, 'I'))
2453 if (d_peek_char (di) == 'E')
2455 /* An argument pack can be empty. */
2457 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, NULL, NULL);
2464 struct demangle_component *a;
2466 a = d_template_arg (di);
2470 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
2473 pal = &d_right (*pal);
2475 if (d_peek_char (di) == 'E')
2482 di->last_name = hold_last_name;
2487 /* <template-arg> ::= <type>
2488 ::= X <expression> E
2492 static struct demangle_component *
2493 d_template_arg (struct d_info *di)
2495 struct demangle_component *ret;
2497 switch (d_peek_char (di))
2501 ret = d_expression (di);
2502 if (! d_check_char (di, 'E'))
2507 return d_expr_primary (di);
2510 /* An argument pack. */
2511 return d_template_args (di);
2514 return cplus_demangle_type (di);
2518 /* Subroutine of <expression> ::= cl <expression>+ E */
2520 static struct demangle_component *
2521 d_exprlist (struct d_info *di)
2523 struct demangle_component *list = NULL;
2524 struct demangle_component **p = &list;
2526 if (d_peek_char (di) == 'E')
2529 return d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, NULL, NULL);
2534 struct demangle_component *arg = d_expression (di);
2538 *p = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, arg, NULL);
2543 if (d_peek_char (di) == 'E')
2553 /* <expression> ::= <(unary) operator-name> <expression>
2554 ::= <(binary) operator-name> <expression> <expression>
2555 ::= <(trinary) operator-name> <expression> <expression> <expression>
2556 ::= cl <expression>+ E
2558 ::= <template-param>
2559 ::= sr <type> <unqualified-name>
2560 ::= sr <type> <unqualified-name> <template-args>
2564 static struct demangle_component *
2565 d_expression (struct d_info *di)
2569 peek = d_peek_char (di);
2571 return d_expr_primary (di);
2572 else if (peek == 'T')
2573 return d_template_param (di);
2574 else if (peek == 's' && d_peek_next_char (di) == 'r')
2576 struct demangle_component *type;
2577 struct demangle_component *name;
2580 type = cplus_demangle_type (di);
2581 name = d_unqualified_name (di);
2582 if (d_peek_char (di) != 'I')
2583 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
2585 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
2586 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2587 d_template_args (di)));
2589 else if (peek == 'f' && d_peek_next_char (di) == 'p')
2591 /* Function parameter used in a late-specified return type. */
2594 if (d_peek_char (di) == '_')
2598 index = d_number (di);
2604 if (! d_check_char (di, '_'))
2607 return d_make_function_param (di, index);
2609 else if (IS_DIGIT (peek))
2611 /* We can get an unqualified name as an expression in the case of
2612 a dependent member access, i.e. decltype(T().i). */
2613 struct demangle_component *name = d_unqualified_name (di);
2616 if (d_peek_char (di) == 'I')
2617 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2618 d_template_args (di));
2624 struct demangle_component *op;
2627 op = d_operator_name (di);
2631 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
2632 di->expansion += op->u.s_operator.op->len - 2;
2634 if (op->type == DEMANGLE_COMPONENT_OPERATOR
2635 && strcmp (op->u.s_operator.op->code, "st") == 0)
2636 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2637 cplus_demangle_type (di));
2643 case DEMANGLE_COMPONENT_OPERATOR:
2644 args = op->u.s_operator.op->args;
2646 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
2647 args = op->u.s_extended_operator.args;
2649 case DEMANGLE_COMPONENT_CAST:
2658 struct demangle_component *operand;
2659 if (op->type == DEMANGLE_COMPONENT_CAST
2660 && d_check_char (di, '_'))
2661 operand = d_exprlist (di);
2663 operand = d_expression (di);
2664 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2669 struct demangle_component *left;
2670 struct demangle_component *right;
2672 left = d_expression (di);
2673 if (!strcmp (op->u.s_operator.op->code, "cl"))
2674 right = d_exprlist (di);
2676 right = d_expression (di);
2678 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
2680 DEMANGLE_COMPONENT_BINARY_ARGS,
2685 struct demangle_component *first;
2686 struct demangle_component *second;
2688 first = d_expression (di);
2689 second = d_expression (di);
2690 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
2692 DEMANGLE_COMPONENT_TRINARY_ARG1,
2695 DEMANGLE_COMPONENT_TRINARY_ARG2,
2697 d_expression (di))));
2705 /* <expr-primary> ::= L <type> <(value) number> E
2706 ::= L <type> <(value) float> E
2707 ::= L <mangled-name> E
2710 static struct demangle_component *
2711 d_expr_primary (struct d_info *di)
2713 struct demangle_component *ret;
2715 if (! d_check_char (di, 'L'))
2717 if (d_peek_char (di) == '_'
2718 /* Workaround for G++ bug; see comment in write_template_arg. */
2719 || d_peek_char (di) == 'Z')
2720 ret = cplus_demangle_mangled_name (di, 0);
2723 struct demangle_component *type;
2724 enum demangle_component_type t;
2727 type = cplus_demangle_type (di);
2731 /* If we have a type we know how to print, we aren't going to
2732 print the type name itself. */
2733 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2734 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
2735 di->expansion -= type->u.s_builtin.type->len;
2737 /* Rather than try to interpret the literal value, we just
2738 collect it as a string. Note that it's possible to have a
2739 floating point literal here. The ABI specifies that the
2740 format of such literals is machine independent. That's fine,
2741 but what's not fine is that versions of g++ up to 3.2 with
2742 -fabi-version=1 used upper case letters in the hex constant,
2743 and dumped out gcc's internal representation. That makes it
2744 hard to tell where the constant ends, and hard to dump the
2745 constant in any readable form anyhow. We don't attempt to
2746 handle these cases. */
2748 t = DEMANGLE_COMPONENT_LITERAL;
2749 if (d_peek_char (di) == 'n')
2751 t = DEMANGLE_COMPONENT_LITERAL_NEG;
2755 while (d_peek_char (di) != 'E')
2757 if (d_peek_char (di) == '\0')
2761 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
2763 if (! d_check_char (di, 'E'))
2768 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2769 ::= Z <(function) encoding> E s [<discriminator>]
2772 static struct demangle_component *
2773 d_local_name (struct d_info *di)
2775 struct demangle_component *function;
2777 if (! d_check_char (di, 'Z'))
2780 function = d_encoding (di, 0);
2782 if (! d_check_char (di, 'E'))
2785 if (d_peek_char (di) == 's')
2788 if (! d_discriminator (di))
2790 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
2791 d_make_name (di, "string literal",
2792 sizeof "string literal" - 1));
2796 struct demangle_component *name;
2799 if (! d_discriminator (di))
2801 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
2805 /* <discriminator> ::= _ <(non-negative) number>
2807 We demangle the discriminator, but we don't print it out. FIXME:
2808 We should print it out in verbose mode. */
2811 d_discriminator (struct d_info *di)
2815 if (d_peek_char (di) != '_')
2818 discrim = d_number (di);
2824 /* Add a new substitution. */
2827 d_add_substitution (struct d_info *di, struct demangle_component *dc)
2831 if (di->next_sub >= di->num_subs)
2833 di->subs[di->next_sub] = dc;
2838 /* <substitution> ::= S <seq-id> _
2848 If PREFIX is non-zero, then this type is being used as a prefix in
2849 a qualified name. In this case, for the standard substitutions, we
2850 need to check whether we are being used as a prefix for a
2851 constructor or destructor, and return a full template name.
2852 Otherwise we will get something like std::iostream::~iostream()
2853 which does not correspond particularly well to any function which
2854 actually appears in the source.
2857 static const struct d_standard_sub_info standard_subs[] =
2862 { 'a', NL ("std::allocator"),
2863 NL ("std::allocator"),
2865 { 'b', NL ("std::basic_string"),
2866 NL ("std::basic_string"),
2867 NL ("basic_string") },
2868 { 's', NL ("std::string"),
2869 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
2870 NL ("basic_string") },
2871 { 'i', NL ("std::istream"),
2872 NL ("std::basic_istream<char, std::char_traits<char> >"),
2873 NL ("basic_istream") },
2874 { 'o', NL ("std::ostream"),
2875 NL ("std::basic_ostream<char, std::char_traits<char> >"),
2876 NL ("basic_ostream") },
2877 { 'd', NL ("std::iostream"),
2878 NL ("std::basic_iostream<char, std::char_traits<char> >"),
2879 NL ("basic_iostream") }
2882 static struct demangle_component *
2883 d_substitution (struct d_info *di, int prefix)
2887 if (! d_check_char (di, 'S'))
2890 c = d_next_char (di);
2891 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
2900 unsigned int new_id;
2903 new_id = id * 36 + c - '0';
2904 else if (IS_UPPER (c))
2905 new_id = id * 36 + c - 'A' + 10;
2911 c = d_next_char (di);
2918 if (id >= (unsigned int) di->next_sub)
2923 return di->subs[id];
2928 const struct d_standard_sub_info *p;
2929 const struct d_standard_sub_info *pend;
2931 verbose = (di->options & DMGL_VERBOSE) != 0;
2932 if (! verbose && prefix)
2936 peek = d_peek_char (di);
2937 if (peek == 'C' || peek == 'D')
2941 pend = (&standard_subs[0]
2942 + sizeof standard_subs / sizeof standard_subs[0]);
2943 for (p = &standard_subs[0]; p < pend; ++p)
2950 if (p->set_last_name != NULL)
2951 di->last_name = d_make_sub (di, p->set_last_name,
2952 p->set_last_name_len);
2955 s = p->full_expansion;
2960 s = p->simple_expansion;
2961 len = p->simple_len;
2963 di->expansion += len;
2964 return d_make_sub (di, s, len);
2972 /* Initialize a growable string. */
2975 d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
2980 dgs->allocation_failure = 0;
2983 d_growable_string_resize (dgs, estimate);
2986 /* Grow a growable string to a given size. */
2989 d_growable_string_resize (struct d_growable_string *dgs, size_t need)
2994 if (dgs->allocation_failure)
2997 /* Start allocation at two bytes to avoid any possibility of confusion
2998 with the special value of 1 used as a return in *palc to indicate
2999 allocation failures. */
3000 newalc = dgs->alc > 0 ? dgs->alc : 2;
3001 while (newalc < need)
3004 newbuf = (char *) realloc (dgs->buf, newalc);
3011 dgs->allocation_failure = 1;
3018 /* Append a buffer to a growable string. */
3021 d_growable_string_append_buffer (struct d_growable_string *dgs,
3022 const char *s, size_t l)
3026 need = dgs->len + l + 1;
3027 if (need > dgs->alc)
3028 d_growable_string_resize (dgs, need);
3030 if (dgs->allocation_failure)
3033 memcpy (dgs->buf + dgs->len, s, l);
3034 dgs->buf[dgs->len + l] = '\0';
3038 /* Bridge growable strings to the callback mechanism. */
3041 d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
3043 struct d_growable_string *dgs = (struct d_growable_string*) opaque;
3045 d_growable_string_append_buffer (dgs, s, l);
3048 /* Initialize a print information structure. */
3051 d_print_init (struct d_print_info *dpi, int options,
3052 demangle_callbackref callback, void *opaque)
3054 dpi->options = options;
3056 dpi->last_char = '\0';
3057 dpi->templates = NULL;
3058 dpi->modifiers = NULL;
3060 dpi->callback = callback;
3061 dpi->opaque = opaque;
3063 dpi->demangle_failure = 0;
3066 /* Indicate that an error occurred during printing, and test for error. */
3069 d_print_error (struct d_print_info *dpi)
3071 dpi->demangle_failure = 1;
3075 d_print_saw_error (struct d_print_info *dpi)
3077 return dpi->demangle_failure != 0;
3080 /* Flush buffered characters to the callback. */
3083 d_print_flush (struct d_print_info *dpi)
3085 dpi->buf[dpi->len] = '\0';
3086 dpi->callback (dpi->buf, dpi->len, dpi->opaque);
3090 /* Append characters and buffers for printing. */
3093 d_append_char (struct d_print_info *dpi, char c)
3095 if (dpi->len == sizeof (dpi->buf) - 1)
3096 d_print_flush (dpi);
3098 dpi->buf[dpi->len++] = c;
3103 d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
3107 for (i = 0; i < l; i++)
3108 d_append_char (dpi, s[i]);
3112 d_append_string (struct d_print_info *dpi, const char *s)
3114 d_append_buffer (dpi, s, strlen (s));
3118 d_last_char (struct d_print_info *dpi)
3120 return dpi->last_char;
3123 /* Turn components into a human readable string. OPTIONS is the
3124 options bits passed to the demangler. DC is the tree to print.
3125 CALLBACK is a function to call to flush demangled string segments
3126 as they fill the intermediate buffer, and OPAQUE is a generalized
3127 callback argument. On success, this returns 1. On failure,
3128 it returns 0, indicating a bad parse. It does not use heap
3129 memory to build an output string, so cannot encounter memory
3130 allocation failure. */
3132 CP_STATIC_IF_GLIBCPP_V3
3134 cplus_demangle_print_callback (int options,
3135 const struct demangle_component *dc,
3136 demangle_callbackref callback, void *opaque)
3138 struct d_print_info dpi;
3140 d_print_init (&dpi, options, callback, opaque);
3142 d_print_comp (&dpi, dc);
3144 d_print_flush (&dpi);
3146 return ! d_print_saw_error (&dpi);
3149 /* Turn components into a human readable string. OPTIONS is the
3150 options bits passed to the demangler. DC is the tree to print.
3151 ESTIMATE is a guess at the length of the result. This returns a
3152 string allocated by malloc, or NULL on error. On success, this
3153 sets *PALC to the size of the allocated buffer. On failure, this
3154 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
3157 CP_STATIC_IF_GLIBCPP_V3
3159 cplus_demangle_print (int options, const struct demangle_component *dc,
3160 int estimate, size_t *palc)
3162 struct d_growable_string dgs;
3164 d_growable_string_init (&dgs, estimate);
3166 if (! cplus_demangle_print_callback (options, dc,
3167 d_growable_string_callback_adapter,
3175 *palc = dgs.allocation_failure ? 1 : dgs.alc;
3179 /* Returns the I'th element of the template arglist ARGS, or NULL on
3182 static struct demangle_component *
3183 d_index_template_argument (struct demangle_component *args, int i)
3185 struct demangle_component *a;
3191 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3197 if (i != 0 || a == NULL)
3203 /* Returns the template argument from the current context indicated by DC,
3204 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
3206 static struct demangle_component *
3207 d_lookup_template_argument (struct d_print_info *dpi,
3208 const struct demangle_component *dc)
3210 if (dpi->templates == NULL)
3212 d_print_error (dpi);
3216 return d_index_template_argument
3217 (d_right (dpi->templates->template_decl),
3218 dc->u.s_number.number);
3221 /* Returns a template argument pack used in DC (any will do), or NULL. */
3223 static struct demangle_component *
3224 d_find_pack (struct d_print_info *dpi,
3225 const struct demangle_component *dc)
3227 struct demangle_component *a;
3233 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
3234 a = d_lookup_template_argument (dpi, dc);
3235 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3239 case DEMANGLE_COMPONENT_PACK_EXPANSION:
3242 case DEMANGLE_COMPONENT_NAME:
3243 case DEMANGLE_COMPONENT_OPERATOR:
3244 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3245 case DEMANGLE_COMPONENT_SUB_STD:
3246 case DEMANGLE_COMPONENT_CHARACTER:
3249 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3250 return d_find_pack (dpi, dc->u.s_extended_operator.name);
3251 case DEMANGLE_COMPONENT_CTOR:
3252 return d_find_pack (dpi, dc->u.s_ctor.name);
3253 case DEMANGLE_COMPONENT_DTOR:
3254 return d_find_pack (dpi, dc->u.s_dtor.name);
3257 a = d_find_pack (dpi, d_left (dc));
3260 return d_find_pack (dpi, d_right (dc));
3264 /* Returns the length of the template argument pack DC. */
3267 d_pack_length (const struct demangle_component *dc)
3270 while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
3271 && d_left (dc) != NULL)
3279 /* DC is a component of a mangled expression. Print it, wrapped in parens
3283 d_print_subexpr (struct d_print_info *dpi,
3284 const struct demangle_component *dc)
3287 if (dc->type == DEMANGLE_COMPONENT_NAME)
3290 d_append_char (dpi, '(');
3291 d_print_comp (dpi, dc);
3293 d_append_char (dpi, ')');
3296 /* Subroutine to handle components. */
3299 d_print_comp (struct d_print_info *dpi,
3300 const struct demangle_component *dc)
3304 d_print_error (dpi);
3307 if (d_print_saw_error (dpi))
3312 case DEMANGLE_COMPONENT_NAME:
3313 if ((dpi->options & DMGL_JAVA) == 0)
3314 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
3316 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
3319 case DEMANGLE_COMPONENT_QUAL_NAME:
3320 case DEMANGLE_COMPONENT_LOCAL_NAME:
3321 d_print_comp (dpi, d_left (dc));
3322 if ((dpi->options & DMGL_JAVA) == 0)
3323 d_append_string (dpi, "::");
3325 d_append_char (dpi, '.');
3326 d_print_comp (dpi, d_right (dc));
3329 case DEMANGLE_COMPONENT_TYPED_NAME:
3331 struct d_print_mod *hold_modifiers;
3332 struct demangle_component *typed_name;
3333 struct d_print_mod adpm[4];
3335 struct d_print_template dpt;
3337 /* Pass the name down to the type so that it can be printed in
3338 the right place for the type. We also have to pass down
3339 any CV-qualifiers, which apply to the this parameter. */
3340 hold_modifiers = dpi->modifiers;
3343 typed_name = d_left (dc);
3344 while (typed_name != NULL)
3346 if (i >= sizeof adpm / sizeof adpm[0])
3348 d_print_error (dpi);
3352 adpm[i].next = dpi->modifiers;
3353 dpi->modifiers = &adpm[i];
3354 adpm[i].mod = typed_name;
3355 adpm[i].printed = 0;
3356 adpm[i].templates = dpi->templates;
3359 if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
3360 && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
3361 && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS)
3364 typed_name = d_left (typed_name);
3367 if (typed_name == NULL)
3369 d_print_error (dpi);
3373 /* If typed_name is a template, then it applies to the
3374 function type as well. */
3375 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
3377 dpt.next = dpi->templates;
3378 dpi->templates = &dpt;
3379 dpt.template_decl = typed_name;
3382 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
3383 there may be CV-qualifiers on its right argument which
3384 really apply here; this happens when parsing a class which
3385 is local to a function. */
3386 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
3388 struct demangle_component *local_name;
3390 local_name = d_right (typed_name);
3391 while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3392 || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3393 || local_name->type == DEMANGLE_COMPONENT_CONST_THIS)
3395 if (i >= sizeof adpm / sizeof adpm[0])
3397 d_print_error (dpi);
3401 adpm[i] = adpm[i - 1];
3402 adpm[i].next = &adpm[i - 1];
3403 dpi->modifiers = &adpm[i];
3405 adpm[i - 1].mod = local_name;
3406 adpm[i - 1].printed = 0;
3407 adpm[i - 1].templates = dpi->templates;
3410 local_name = d_left (local_name);
3414 d_print_comp (dpi, d_right (dc));
3416 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
3417 dpi->templates = dpt.next;
3419 /* If the modifiers didn't get printed by the type, print them
3424 if (! adpm[i].printed)
3426 d_append_char (dpi, ' ');
3427 d_print_mod (dpi, adpm[i].mod);
3431 dpi->modifiers = hold_modifiers;
3436 case DEMANGLE_COMPONENT_TEMPLATE:
3438 struct d_print_mod *hold_dpm;
3439 struct demangle_component *dcl;
3441 /* Don't push modifiers into a template definition. Doing so
3442 could give the wrong definition for a template argument.
3443 Instead, treat the template essentially as a name. */
3445 hold_dpm = dpi->modifiers;
3446 dpi->modifiers = NULL;
3450 if ((dpi->options & DMGL_JAVA) != 0
3451 && dcl->type == DEMANGLE_COMPONENT_NAME
3452 && dcl->u.s_name.len == 6
3453 && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
3455 /* Special-case Java arrays, so that JArray<TYPE> appears
3456 instead as TYPE[]. */
3458 d_print_comp (dpi, d_right (dc));
3459 d_append_string (dpi, "[]");
3463 d_print_comp (dpi, dcl);
3464 if (d_last_char (dpi) == '<')
3465 d_append_char (dpi, ' ');
3466 d_append_char (dpi, '<');
3467 d_print_comp (dpi, d_right (dc));
3468 /* Avoid generating two consecutive '>' characters, to avoid
3469 the C++ syntactic ambiguity. */
3470 if (d_last_char (dpi) == '>')
3471 d_append_char (dpi, ' ');
3472 d_append_char (dpi, '>');
3475 dpi->modifiers = hold_dpm;
3480 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
3482 struct d_print_template *hold_dpt;
3483 struct demangle_component *a = d_lookup_template_argument (dpi, dc);
3485 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3486 a = d_index_template_argument (a, dpi->pack_index);
3490 d_print_error (dpi);
3494 /* While processing this parameter, we need to pop the list of
3495 templates. This is because the template parameter may
3496 itself be a reference to a parameter of an outer
3499 hold_dpt = dpi->templates;
3500 dpi->templates = hold_dpt->next;
3502 d_print_comp (dpi, a);
3504 dpi->templates = hold_dpt;
3509 case DEMANGLE_COMPONENT_CTOR:
3510 d_print_comp (dpi, dc->u.s_ctor.name);
3513 case DEMANGLE_COMPONENT_DTOR:
3514 d_append_char (dpi, '~');
3515 d_print_comp (dpi, dc->u.s_dtor.name);
3518 case DEMANGLE_COMPONENT_VTABLE:
3519 d_append_string (dpi, "vtable for ");
3520 d_print_comp (dpi, d_left (dc));
3523 case DEMANGLE_COMPONENT_VTT:
3524 d_append_string (dpi, "VTT for ");
3525 d_print_comp (dpi, d_left (dc));
3528 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
3529 d_append_string (dpi, "construction vtable for ");
3530 d_print_comp (dpi, d_left (dc));