1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
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
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 /* A demangle component and some scope captured when it was first
283 /* The component whose scope this is. */
284 const struct demangle_component *container;
285 /* The list of templates, if any, that was current when this
286 scope was captured. */
287 struct d_print_template *templates;
290 /* Checkpoint structure to allow backtracking. This holds copies
291 of the fields of struct d_info that need to be restored
292 if a trial parse needs to be backtracked over. */
294 struct d_info_checkpoint
303 enum { D_PRINT_BUFFER_LENGTH = 256 };
306 /* Fixed-length allocated buffer for demangled data, flushed to the
307 callback with a NUL termination once full. */
308 char buf[D_PRINT_BUFFER_LENGTH];
309 /* Current length of data in buffer. */
311 /* The last character printed, saved individually so that it survives
314 /* Callback function to handle demangled buffer flush. */
315 demangle_callbackref callback;
316 /* Opaque callback argument. */
318 /* The current list of templates, if any. */
319 struct d_print_template *templates;
320 /* The current list of modifiers (e.g., pointer, reference, etc.),
322 struct d_print_mod *modifiers;
323 /* Set to 1 if we saw a demangling error. */
324 int demangle_failure;
325 /* The current index into any template argument packs we are using
328 /* Number of d_print_flush calls so far. */
329 unsigned long int flush_count;
330 /* Array of saved scopes for evaluating substitutions. */
331 struct d_saved_scope *saved_scopes;
332 /* Number of saved scopes in the above array. */
333 int num_saved_scopes;
334 /* The nearest enclosing template, if any. */
335 const struct demangle_component *current_template;
338 #ifdef CP_DEMANGLE_DEBUG
339 static void d_dump (struct demangle_component *, int);
342 static struct demangle_component *
343 d_make_empty (struct d_info *);
345 static struct demangle_component *
346 d_make_comp (struct d_info *, enum demangle_component_type,
347 struct demangle_component *,
348 struct demangle_component *);
350 static struct demangle_component *
351 d_make_name (struct d_info *, const char *, int);
353 static struct demangle_component *
354 d_make_demangle_mangled_name (struct d_info *, const char *);
356 static struct demangle_component *
357 d_make_builtin_type (struct d_info *,
358 const struct demangle_builtin_type_info *);
360 static struct demangle_component *
361 d_make_operator (struct d_info *,
362 const struct demangle_operator_info *);
364 static struct demangle_component *
365 d_make_extended_operator (struct d_info *, int,
366 struct demangle_component *);
368 static struct demangle_component *
369 d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
370 struct demangle_component *);
372 static struct demangle_component *
373 d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
374 struct demangle_component *);
376 static struct demangle_component *
377 d_make_template_param (struct d_info *, long);
379 static struct demangle_component *
380 d_make_sub (struct d_info *, const char *, int);
383 has_return_type (struct demangle_component *);
386 is_ctor_dtor_or_conversion (struct demangle_component *);
388 static struct demangle_component *d_encoding (struct d_info *, int);
390 static struct demangle_component *d_name (struct d_info *);
392 static struct demangle_component *d_nested_name (struct d_info *);
394 static struct demangle_component *d_prefix (struct d_info *);
396 static struct demangle_component *d_unqualified_name (struct d_info *);
398 static struct demangle_component *d_source_name (struct d_info *);
400 static long d_number (struct d_info *);
402 static struct demangle_component *d_identifier (struct d_info *, int);
404 static struct demangle_component *d_operator_name (struct d_info *);
406 static struct demangle_component *d_special_name (struct d_info *);
408 static int d_call_offset (struct d_info *, int);
410 static struct demangle_component *d_ctor_dtor_name (struct d_info *);
412 static struct demangle_component **
413 d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
415 static struct demangle_component *
416 d_ref_qualifier (struct d_info *, struct demangle_component *);
418 static struct demangle_component *
419 d_function_type (struct d_info *);
421 static struct demangle_component *
422 d_bare_function_type (struct d_info *, int);
424 static struct demangle_component *
425 d_class_enum_type (struct d_info *);
427 static struct demangle_component *d_array_type (struct d_info *);
429 static struct demangle_component *d_vector_type (struct d_info *);
431 static struct demangle_component *
432 d_pointer_to_member_type (struct d_info *);
434 static struct demangle_component *
435 d_template_param (struct d_info *);
437 static struct demangle_component *d_template_args (struct d_info *);
439 static struct demangle_component *
440 d_template_arg (struct d_info *);
442 static struct demangle_component *d_expression (struct d_info *);
444 static struct demangle_component *d_expr_primary (struct d_info *);
446 static struct demangle_component *d_local_name (struct d_info *);
448 static int d_discriminator (struct d_info *);
450 static struct demangle_component *d_lambda (struct d_info *);
452 static struct demangle_component *d_unnamed_type (struct d_info *);
454 static struct demangle_component *
455 d_clone_suffix (struct d_info *, struct demangle_component *);
458 d_add_substitution (struct d_info *, struct demangle_component *);
460 static struct demangle_component *d_substitution (struct d_info *, int);
462 static void d_checkpoint (struct d_info *, struct d_info_checkpoint *);
464 static void d_backtrack (struct d_info *, struct d_info_checkpoint *);
466 static void d_growable_string_init (struct d_growable_string *, size_t);
469 d_growable_string_resize (struct d_growable_string *, size_t);
472 d_growable_string_append_buffer (struct d_growable_string *,
473 const char *, size_t);
475 d_growable_string_callback_adapter (const char *, size_t, void *);
478 d_print_init (struct d_print_info *, demangle_callbackref, void *);
480 static inline void d_print_error (struct d_print_info *);
482 static inline int d_print_saw_error (struct d_print_info *);
484 static inline void d_print_flush (struct d_print_info *);
486 static inline void d_append_char (struct d_print_info *, char);
488 static inline void d_append_buffer (struct d_print_info *,
489 const char *, size_t);
491 static inline void d_append_string (struct d_print_info *, const char *);
493 static inline char d_last_char (struct d_print_info *);
496 d_print_comp (struct d_print_info *, int, const struct demangle_component *);
499 d_print_java_identifier (struct d_print_info *, const char *, int);
502 d_print_mod_list (struct d_print_info *, int, struct d_print_mod *, int);
505 d_print_mod (struct d_print_info *, int, const struct demangle_component *);
508 d_print_function_type (struct d_print_info *, int,
509 const struct demangle_component *,
510 struct d_print_mod *);
513 d_print_array_type (struct d_print_info *, int,
514 const struct demangle_component *,
515 struct d_print_mod *);
518 d_print_expr_op (struct d_print_info *, int, const struct demangle_component *);
521 d_print_cast (struct d_print_info *, int, const struct demangle_component *);
523 static int d_demangle_callback (const char *, int,
524 demangle_callbackref, void *);
525 static char *d_demangle (const char *, int, size_t *);
527 #ifdef CP_DEMANGLE_DEBUG
530 d_dump (struct demangle_component *dc, int indent)
537 printf ("failed demangling\n");
541 for (i = 0; i < indent; ++i)
546 case DEMANGLE_COMPONENT_NAME:
547 printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
549 case DEMANGLE_COMPONENT_TAGGED_NAME:
550 printf ("tagged name\n");
551 d_dump (dc->u.s_binary.left, indent + 2);
552 d_dump (dc->u.s_binary.right, indent + 2);
554 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
555 printf ("template parameter %ld\n", dc->u.s_number.number);
557 case DEMANGLE_COMPONENT_CTOR:
558 printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
559 d_dump (dc->u.s_ctor.name, indent + 2);
561 case DEMANGLE_COMPONENT_DTOR:
562 printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
563 d_dump (dc->u.s_dtor.name, indent + 2);
565 case DEMANGLE_COMPONENT_SUB_STD:
566 printf ("standard substitution %s\n", dc->u.s_string.string);
568 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
569 printf ("builtin type %s\n", dc->u.s_builtin.type->name);
571 case DEMANGLE_COMPONENT_OPERATOR:
572 printf ("operator %s\n", dc->u.s_operator.op->name);
574 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
575 printf ("extended operator with %d args\n",
576 dc->u.s_extended_operator.args);
577 d_dump (dc->u.s_extended_operator.name, indent + 2);
580 case DEMANGLE_COMPONENT_QUAL_NAME:
581 printf ("qualified name\n");
583 case DEMANGLE_COMPONENT_LOCAL_NAME:
584 printf ("local name\n");
586 case DEMANGLE_COMPONENT_TYPED_NAME:
587 printf ("typed name\n");
589 case DEMANGLE_COMPONENT_TEMPLATE:
590 printf ("template\n");
592 case DEMANGLE_COMPONENT_VTABLE:
595 case DEMANGLE_COMPONENT_VTT:
598 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
599 printf ("construction vtable\n");
601 case DEMANGLE_COMPONENT_TYPEINFO:
602 printf ("typeinfo\n");
604 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
605 printf ("typeinfo name\n");
607 case DEMANGLE_COMPONENT_TYPEINFO_FN:
608 printf ("typeinfo function\n");
610 case DEMANGLE_COMPONENT_THUNK:
613 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
614 printf ("virtual thunk\n");
616 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
617 printf ("covariant thunk\n");
619 case DEMANGLE_COMPONENT_JAVA_CLASS:
620 printf ("java class\n");
622 case DEMANGLE_COMPONENT_GUARD:
625 case DEMANGLE_COMPONENT_REFTEMP:
626 printf ("reference temporary\n");
628 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
629 printf ("hidden alias\n");
631 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
632 printf ("transaction clone\n");
634 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
635 printf ("non-transaction clone\n");
637 case DEMANGLE_COMPONENT_RESTRICT:
638 printf ("restrict\n");
640 case DEMANGLE_COMPONENT_VOLATILE:
641 printf ("volatile\n");
643 case DEMANGLE_COMPONENT_CONST:
646 case DEMANGLE_COMPONENT_RESTRICT_THIS:
647 printf ("restrict this\n");
649 case DEMANGLE_COMPONENT_VOLATILE_THIS:
650 printf ("volatile this\n");
652 case DEMANGLE_COMPONENT_CONST_THIS:
653 printf ("const this\n");
655 case DEMANGLE_COMPONENT_REFERENCE_THIS:
656 printf ("reference this\n");
658 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
659 printf ("rvalue reference this\n");
661 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
662 printf ("vendor type qualifier\n");
664 case DEMANGLE_COMPONENT_POINTER:
665 printf ("pointer\n");
667 case DEMANGLE_COMPONENT_REFERENCE:
668 printf ("reference\n");
670 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
671 printf ("rvalue reference\n");
673 case DEMANGLE_COMPONENT_COMPLEX:
674 printf ("complex\n");
676 case DEMANGLE_COMPONENT_IMAGINARY:
677 printf ("imaginary\n");
679 case DEMANGLE_COMPONENT_VENDOR_TYPE:
680 printf ("vendor type\n");
682 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
683 printf ("function type\n");
685 case DEMANGLE_COMPONENT_ARRAY_TYPE:
686 printf ("array type\n");
688 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
689 printf ("pointer to member type\n");
691 case DEMANGLE_COMPONENT_FIXED_TYPE:
692 printf ("fixed-point type\n");
694 case DEMANGLE_COMPONENT_ARGLIST:
695 printf ("argument list\n");
697 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
698 printf ("template argument list\n");
700 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
701 printf ("initializer list\n");
703 case DEMANGLE_COMPONENT_CAST:
706 case DEMANGLE_COMPONENT_NULLARY:
707 printf ("nullary operator\n");
709 case DEMANGLE_COMPONENT_UNARY:
710 printf ("unary operator\n");
712 case DEMANGLE_COMPONENT_BINARY:
713 printf ("binary operator\n");
715 case DEMANGLE_COMPONENT_BINARY_ARGS:
716 printf ("binary operator arguments\n");
718 case DEMANGLE_COMPONENT_TRINARY:
719 printf ("trinary operator\n");
721 case DEMANGLE_COMPONENT_TRINARY_ARG1:
722 printf ("trinary operator arguments 1\n");
724 case DEMANGLE_COMPONENT_TRINARY_ARG2:
725 printf ("trinary operator arguments 1\n");
727 case DEMANGLE_COMPONENT_LITERAL:
728 printf ("literal\n");
730 case DEMANGLE_COMPONENT_LITERAL_NEG:
731 printf ("negative literal\n");
733 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
734 printf ("java resource\n");
736 case DEMANGLE_COMPONENT_COMPOUND_NAME:
737 printf ("compound name\n");
739 case DEMANGLE_COMPONENT_CHARACTER:
740 printf ("character '%c'\n", dc->u.s_character.character);
742 case DEMANGLE_COMPONENT_DECLTYPE:
743 printf ("decltype\n");
745 case DEMANGLE_COMPONENT_PACK_EXPANSION:
746 printf ("pack expansion\n");
748 case DEMANGLE_COMPONENT_TLS_INIT:
749 printf ("tls init function\n");
751 case DEMANGLE_COMPONENT_TLS_WRAPPER:
752 printf ("tls wrapper function\n");
754 case DEMANGLE_COMPONENT_DEFAULT_ARG:
755 printf ("default argument %d\n", dc->u.s_unary_num.num);
756 d_dump (dc->u.s_unary_num.sub, indent+2);
758 case DEMANGLE_COMPONENT_LAMBDA:
759 printf ("lambda %d\n", dc->u.s_unary_num.num);
760 d_dump (dc->u.s_unary_num.sub, indent+2);
764 d_dump (d_left (dc), indent + 2);
765 d_dump (d_right (dc), indent + 2);
768 #endif /* CP_DEMANGLE_DEBUG */
770 /* Fill in a DEMANGLE_COMPONENT_NAME. */
772 CP_STATIC_IF_GLIBCPP_V3
774 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
776 if (p == NULL || s == NULL || len == 0)
778 p->type = DEMANGLE_COMPONENT_NAME;
780 p->u.s_name.len = len;
784 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
786 CP_STATIC_IF_GLIBCPP_V3
788 cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
789 struct demangle_component *name)
791 if (p == NULL || args < 0 || name == NULL)
793 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
794 p->u.s_extended_operator.args = args;
795 p->u.s_extended_operator.name = name;
799 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
801 CP_STATIC_IF_GLIBCPP_V3
803 cplus_demangle_fill_ctor (struct demangle_component *p,
804 enum gnu_v3_ctor_kinds kind,
805 struct demangle_component *name)
809 || (int) kind < gnu_v3_complete_object_ctor
810 || (int) kind > gnu_v3_object_ctor_group)
812 p->type = DEMANGLE_COMPONENT_CTOR;
813 p->u.s_ctor.kind = kind;
814 p->u.s_ctor.name = name;
818 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
820 CP_STATIC_IF_GLIBCPP_V3
822 cplus_demangle_fill_dtor (struct demangle_component *p,
823 enum gnu_v3_dtor_kinds kind,
824 struct demangle_component *name)
828 || (int) kind < gnu_v3_deleting_dtor
829 || (int) kind > gnu_v3_object_dtor_group)
831 p->type = DEMANGLE_COMPONENT_DTOR;
832 p->u.s_dtor.kind = kind;
833 p->u.s_dtor.name = name;
837 /* Add a new component. */
839 static struct demangle_component *
840 d_make_empty (struct d_info *di)
842 struct demangle_component *p;
844 if (di->next_comp >= di->num_comps)
846 p = &di->comps[di->next_comp];
851 /* Add a new generic component. */
853 static struct demangle_component *
854 d_make_comp (struct d_info *di, enum demangle_component_type type,
855 struct demangle_component *left,
856 struct demangle_component *right)
858 struct demangle_component *p;
860 /* We check for errors here. A typical error would be a NULL return
861 from a subroutine. We catch those here, and return NULL
865 /* These types require two parameters. */
866 case DEMANGLE_COMPONENT_QUAL_NAME:
867 case DEMANGLE_COMPONENT_LOCAL_NAME:
868 case DEMANGLE_COMPONENT_TYPED_NAME:
869 case DEMANGLE_COMPONENT_TAGGED_NAME:
870 case DEMANGLE_COMPONENT_TEMPLATE:
871 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
872 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
873 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
874 case DEMANGLE_COMPONENT_UNARY:
875 case DEMANGLE_COMPONENT_BINARY:
876 case DEMANGLE_COMPONENT_BINARY_ARGS:
877 case DEMANGLE_COMPONENT_TRINARY:
878 case DEMANGLE_COMPONENT_TRINARY_ARG1:
879 case DEMANGLE_COMPONENT_LITERAL:
880 case DEMANGLE_COMPONENT_LITERAL_NEG:
881 case DEMANGLE_COMPONENT_COMPOUND_NAME:
882 case DEMANGLE_COMPONENT_VECTOR_TYPE:
883 case DEMANGLE_COMPONENT_CLONE:
884 if (left == NULL || right == NULL)
888 /* These types only require one parameter. */
889 case DEMANGLE_COMPONENT_VTABLE:
890 case DEMANGLE_COMPONENT_VTT:
891 case DEMANGLE_COMPONENT_TYPEINFO:
892 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
893 case DEMANGLE_COMPONENT_TYPEINFO_FN:
894 case DEMANGLE_COMPONENT_THUNK:
895 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
896 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
897 case DEMANGLE_COMPONENT_JAVA_CLASS:
898 case DEMANGLE_COMPONENT_GUARD:
899 case DEMANGLE_COMPONENT_TLS_INIT:
900 case DEMANGLE_COMPONENT_TLS_WRAPPER:
901 case DEMANGLE_COMPONENT_REFTEMP:
902 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
903 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
904 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
905 case DEMANGLE_COMPONENT_POINTER:
906 case DEMANGLE_COMPONENT_REFERENCE:
907 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
908 case DEMANGLE_COMPONENT_COMPLEX:
909 case DEMANGLE_COMPONENT_IMAGINARY:
910 case DEMANGLE_COMPONENT_VENDOR_TYPE:
911 case DEMANGLE_COMPONENT_CAST:
912 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
913 case DEMANGLE_COMPONENT_DECLTYPE:
914 case DEMANGLE_COMPONENT_PACK_EXPANSION:
915 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
916 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
917 case DEMANGLE_COMPONENT_NULLARY:
918 case DEMANGLE_COMPONENT_TRINARY_ARG2:
923 /* This needs a right parameter, but the left parameter can be
925 case DEMANGLE_COMPONENT_ARRAY_TYPE:
926 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
931 /* These are allowed to have no parameters--in some cases they
932 will be filled in later. */
933 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
934 case DEMANGLE_COMPONENT_RESTRICT:
935 case DEMANGLE_COMPONENT_VOLATILE:
936 case DEMANGLE_COMPONENT_CONST:
937 case DEMANGLE_COMPONENT_RESTRICT_THIS:
938 case DEMANGLE_COMPONENT_VOLATILE_THIS:
939 case DEMANGLE_COMPONENT_CONST_THIS:
940 case DEMANGLE_COMPONENT_REFERENCE_THIS:
941 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
942 case DEMANGLE_COMPONENT_ARGLIST:
943 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
946 /* Other types should not be seen here. */
951 p = d_make_empty (di);
955 p->u.s_binary.left = left;
956 p->u.s_binary.right = right;
961 /* Add a new demangle mangled name component. */
963 static struct demangle_component *
964 d_make_demangle_mangled_name (struct d_info *di, const char *s)
966 if (d_peek_char (di) != '_' || d_peek_next_char (di) != 'Z')
967 return d_make_name (di, s, strlen (s));
969 return d_encoding (di, 0);
972 /* Add a new name component. */
974 static struct demangle_component *
975 d_make_name (struct d_info *di, const char *s, int len)
977 struct demangle_component *p;
979 p = d_make_empty (di);
980 if (! cplus_demangle_fill_name (p, s, len))
985 /* Add a new builtin type component. */
987 static struct demangle_component *
988 d_make_builtin_type (struct d_info *di,
989 const struct demangle_builtin_type_info *type)
991 struct demangle_component *p;
995 p = d_make_empty (di);
998 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
999 p->u.s_builtin.type = type;
1004 /* Add a new operator component. */
1006 static struct demangle_component *
1007 d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
1009 struct demangle_component *p;
1011 p = d_make_empty (di);
1014 p->type = DEMANGLE_COMPONENT_OPERATOR;
1015 p->u.s_operator.op = op;
1020 /* Add a new extended operator component. */
1022 static struct demangle_component *
1023 d_make_extended_operator (struct d_info *di, int args,
1024 struct demangle_component *name)
1026 struct demangle_component *p;
1028 p = d_make_empty (di);
1029 if (! cplus_demangle_fill_extended_operator (p, args, name))
1034 static struct demangle_component *
1035 d_make_default_arg (struct d_info *di, int num,
1036 struct demangle_component *sub)
1038 struct demangle_component *p = d_make_empty (di);
1041 p->type = DEMANGLE_COMPONENT_DEFAULT_ARG;
1042 p->u.s_unary_num.num = num;
1043 p->u.s_unary_num.sub = sub;
1048 /* Add a new constructor component. */
1050 static struct demangle_component *
1051 d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
1052 struct demangle_component *name)
1054 struct demangle_component *p;
1056 p = d_make_empty (di);
1057 if (! cplus_demangle_fill_ctor (p, kind, name))
1062 /* Add a new destructor component. */
1064 static struct demangle_component *
1065 d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
1066 struct demangle_component *name)
1068 struct demangle_component *p;
1070 p = d_make_empty (di);
1071 if (! cplus_demangle_fill_dtor (p, kind, name))
1076 /* Add a new template parameter. */
1078 static struct demangle_component *
1079 d_make_template_param (struct d_info *di, long i)
1081 struct demangle_component *p;
1083 p = d_make_empty (di);
1086 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
1087 p->u.s_number.number = i;
1092 /* Add a new function parameter. */
1094 static struct demangle_component *
1095 d_make_function_param (struct d_info *di, long i)
1097 struct demangle_component *p;
1099 p = d_make_empty (di);
1102 p->type = DEMANGLE_COMPONENT_FUNCTION_PARAM;
1103 p->u.s_number.number = i;
1108 /* Add a new standard substitution component. */
1110 static struct demangle_component *
1111 d_make_sub (struct d_info *di, const char *name, int len)
1113 struct demangle_component *p;
1115 p = d_make_empty (di);
1118 p->type = DEMANGLE_COMPONENT_SUB_STD;
1119 p->u.s_string.string = name;
1120 p->u.s_string.len = len;
1125 /* <mangled-name> ::= _Z <encoding> [<clone-suffix>]*
1127 TOP_LEVEL is non-zero when called at the top level. */
1129 CP_STATIC_IF_GLIBCPP_V3
1130 struct demangle_component *
1131 cplus_demangle_mangled_name (struct d_info *di, int top_level)
1133 struct demangle_component *p;
1135 if (! d_check_char (di, '_')
1136 /* Allow missing _ if not at toplevel to work around a
1137 bug in G++ abi-version=2 mangling; see the comment in
1138 write_template_arg. */
1141 if (! d_check_char (di, 'Z'))
1143 p = d_encoding (di, top_level);
1145 /* If at top level and parsing parameters, check for a clone
1147 if (top_level && (di->options & DMGL_PARAMS) != 0)
1148 while (d_peek_char (di) == '.'
1149 && (IS_LOWER (d_peek_next_char (di))
1150 || d_peek_next_char (di) == '_'
1151 || IS_DIGIT (d_peek_next_char (di))))
1152 p = d_clone_suffix (di, p);
1157 /* Return whether a function should have a return type. The argument
1158 is the function name, which may be qualified in various ways. The
1159 rules are that template functions have return types with some
1160 exceptions, function types which are not part of a function name
1161 mangling have return types with some exceptions, and non-template
1162 function names do not have return types. The exceptions are that
1163 constructors, destructors, and conversion operators do not have
1167 has_return_type (struct demangle_component *dc)
1175 case DEMANGLE_COMPONENT_TEMPLATE:
1176 return ! is_ctor_dtor_or_conversion (d_left (dc));
1177 case DEMANGLE_COMPONENT_RESTRICT_THIS:
1178 case DEMANGLE_COMPONENT_VOLATILE_THIS:
1179 case DEMANGLE_COMPONENT_CONST_THIS:
1180 case DEMANGLE_COMPONENT_REFERENCE_THIS:
1181 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
1182 return has_return_type (d_left (dc));
1186 /* Return whether a name is a constructor, a destructor, or a
1187 conversion operator. */
1190 is_ctor_dtor_or_conversion (struct demangle_component *dc)
1198 case DEMANGLE_COMPONENT_QUAL_NAME:
1199 case DEMANGLE_COMPONENT_LOCAL_NAME:
1200 return is_ctor_dtor_or_conversion (d_right (dc));
1201 case DEMANGLE_COMPONENT_CTOR:
1202 case DEMANGLE_COMPONENT_DTOR:
1203 case DEMANGLE_COMPONENT_CAST:
1208 /* <encoding> ::= <(function) name> <bare-function-type>
1212 TOP_LEVEL is non-zero when called at the top level, in which case
1213 if DMGL_PARAMS is not set we do not demangle the function
1214 parameters. We only set this at the top level, because otherwise
1215 we would not correctly demangle names in local scopes. */
1217 static struct demangle_component *
1218 d_encoding (struct d_info *di, int top_level)
1220 char peek = d_peek_char (di);
1222 if (peek == 'G' || peek == 'T')
1223 return d_special_name (di);
1226 struct demangle_component *dc;
1230 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
1232 /* Strip off any initial CV-qualifiers, as they really apply
1233 to the `this' parameter, and they were not output by the
1234 v2 demangler without DMGL_PARAMS. */
1235 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1236 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1237 || dc->type == DEMANGLE_COMPONENT_CONST_THIS
1238 || dc->type == DEMANGLE_COMPONENT_REFERENCE_THIS
1239 || dc->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS)
1242 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1243 there may be CV-qualifiers on its right argument which
1244 really apply here; this happens when parsing a class
1245 which is local to a function. */
1246 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1248 struct demangle_component *dcr;
1251 while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1252 || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1253 || dcr->type == DEMANGLE_COMPONENT_CONST_THIS
1254 || dcr->type == DEMANGLE_COMPONENT_REFERENCE_THIS
1255 || dcr->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS)
1257 dc->u.s_binary.right = dcr;
1263 peek = d_peek_char (di);
1264 if (dc == NULL || peek == '\0' || peek == 'E')
1266 return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
1267 d_bare_function_type (di, has_return_type (dc)));
1271 /* <tagged-name> ::= <name> B <source-name> */
1273 static struct demangle_component *
1274 d_abi_tags (struct d_info *di, struct demangle_component *dc)
1277 while (peek = d_peek_char (di),
1280 struct demangle_component *tag;
1282 tag = d_source_name (di);
1283 dc = d_make_comp (di, DEMANGLE_COMPONENT_TAGGED_NAME, dc, tag);
1288 /* <name> ::= <nested-name>
1290 ::= <unscoped-template-name> <template-args>
1293 <unscoped-name> ::= <unqualified-name>
1294 ::= St <unqualified-name>
1296 <unscoped-template-name> ::= <unscoped-name>
1300 static struct demangle_component *
1301 d_name (struct d_info *di)
1303 char peek = d_peek_char (di);
1304 struct demangle_component *dc;
1309 return d_nested_name (di);
1312 return d_local_name (di);
1315 return d_unqualified_name (di);
1321 if (d_peek_next_char (di) != 't')
1323 dc = d_substitution (di, 0);
1329 dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
1330 d_make_name (di, "std", 3),
1331 d_unqualified_name (di));
1336 if (d_peek_char (di) != 'I')
1338 /* The grammar does not permit this case to occur if we
1339 called d_substitution() above (i.e., subst == 1). We
1340 don't bother to check. */
1344 /* This is <template-args>, which means that we just saw
1345 <unscoped-template-name>, which is a substitution
1346 candidate if we didn't just get it from a
1350 if (! d_add_substitution (di, dc))
1353 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1354 d_template_args (di));
1362 dc = d_unqualified_name (di);
1363 if (d_peek_char (di) == 'I')
1365 /* This is <template-args>, which means that we just saw
1366 <unscoped-template-name>, which is a substitution
1368 if (! d_add_substitution (di, dc))
1370 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1371 d_template_args (di));
1377 /* <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1378 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
1381 static struct demangle_component *
1382 d_nested_name (struct d_info *di)
1384 struct demangle_component *ret;
1385 struct demangle_component **pret;
1386 struct demangle_component *rqual;
1388 if (! d_check_char (di, 'N'))
1391 pret = d_cv_qualifiers (di, &ret, 1);
1395 /* Parse the ref-qualifier now and then attach it
1396 once we have something to attach it to. */
1397 rqual = d_ref_qualifier (di, NULL);
1399 *pret = d_prefix (di);
1405 d_left (rqual) = ret;
1409 if (! d_check_char (di, 'E'))
1415 /* <prefix> ::= <prefix> <unqualified-name>
1416 ::= <template-prefix> <template-args>
1417 ::= <template-param>
1422 <template-prefix> ::= <prefix> <(template) unqualified-name>
1423 ::= <template-param>
1427 static struct demangle_component *
1428 d_prefix (struct d_info *di)
1430 struct demangle_component *ret = NULL;
1435 enum demangle_component_type comb_type;
1436 struct demangle_component *dc;
1438 peek = d_peek_char (di);
1442 /* The older code accepts a <local-name> here, but I don't see
1443 that in the grammar. The older code does not accept a
1444 <template-param> here. */
1446 comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
1449 char peek2 = d_peek_next_char (di);
1450 if (peek2 == 'T' || peek2 == 't')
1452 dc = cplus_demangle_type (di);
1454 /* Destructor name. */
1455 dc = d_unqualified_name (di);
1457 else if (IS_DIGIT (peek)
1462 dc = d_unqualified_name (di);
1463 else if (peek == 'S')
1464 dc = d_substitution (di, 1);
1465 else if (peek == 'I')
1469 comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1470 dc = d_template_args (di);
1472 else if (peek == 'T')
1473 dc = d_template_param (di);
1474 else if (peek == 'E')
1476 else if (peek == 'M')
1478 /* Initializer scope for a lambda. We don't need to represent
1479 this; the normal code will just treat the variable as a type
1480 scope, which gives appropriate output. */
1492 ret = d_make_comp (di, comb_type, ret, dc);
1494 if (peek != 'S' && d_peek_char (di) != 'E')
1496 if (! d_add_substitution (di, ret))
1502 /* <unqualified-name> ::= <operator-name>
1503 ::= <ctor-dtor-name>
1505 ::= <local-source-name>
1507 <local-source-name> ::= L <source-name> <discriminator>
1510 static struct demangle_component *
1511 d_unqualified_name (struct d_info *di)
1513 struct demangle_component *ret;
1516 peek = d_peek_char (di);
1517 if (IS_DIGIT (peek))
1518 ret = d_source_name (di);
1519 else if (IS_LOWER (peek))
1521 ret = d_operator_name (di);
1522 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1524 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1525 if (!strcmp (ret->u.s_operator.op->code, "li"))
1526 ret = d_make_comp (di, DEMANGLE_COMPONENT_UNARY, ret,
1527 d_source_name (di));
1530 else if (peek == 'C' || peek == 'D')
1531 ret = d_ctor_dtor_name (di);
1532 else if (peek == 'L')
1536 ret = d_source_name (di);
1539 if (! d_discriminator (di))
1542 else if (peek == 'U')
1544 switch (d_peek_next_char (di))
1547 ret = d_lambda (di);
1550 ret = d_unnamed_type (di);
1559 if (d_peek_char (di) == 'B')
1560 ret = d_abi_tags (di, ret);
1564 /* <source-name> ::= <(positive length) number> <identifier> */
1566 static struct demangle_component *
1567 d_source_name (struct d_info *di)
1570 struct demangle_component *ret;
1572 len = d_number (di);
1575 ret = d_identifier (di, len);
1576 di->last_name = ret;
1580 /* number ::= [n] <(non-negative decimal integer)> */
1583 d_number (struct d_info *di)
1590 peek = d_peek_char (di);
1595 peek = d_peek_char (di);
1601 if (! IS_DIGIT (peek))
1607 ret = ret * 10 + peek - '0';
1609 peek = d_peek_char (di);
1613 /* Like d_number, but returns a demangle_component. */
1615 static struct demangle_component *
1616 d_number_component (struct d_info *di)
1618 struct demangle_component *ret = d_make_empty (di);
1621 ret->type = DEMANGLE_COMPONENT_NUMBER;
1622 ret->u.s_number.number = d_number (di);
1627 /* identifier ::= <(unqualified source code identifier)> */
1629 static struct demangle_component *
1630 d_identifier (struct d_info *di, int len)
1636 if (di->send - name < len)
1639 d_advance (di, len);
1641 /* A Java mangled name may have a trailing '$' if it is a C++
1642 keyword. This '$' is not included in the length count. We just
1644 if ((di->options & DMGL_JAVA) != 0
1645 && d_peek_char (di) == '$')
1648 /* Look for something which looks like a gcc encoding of an
1649 anonymous namespace, and replace it with a more user friendly
1651 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1652 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1653 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1657 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1658 if ((*s == '.' || *s == '_' || *s == '$')
1661 di->expansion -= len - sizeof "(anonymous namespace)";
1662 return d_make_name (di, "(anonymous namespace)",
1663 sizeof "(anonymous namespace)" - 1);
1667 return d_make_name (di, name, len);
1670 /* operator_name ::= many different two character encodings.
1672 ::= v <digit> <source-name>
1674 This list is sorted for binary search. */
1676 #define NL(s) s, (sizeof s) - 1
1678 CP_STATIC_IF_GLIBCPP_V3
1679 const struct demangle_operator_info cplus_demangle_operators[] =
1681 { "aN", NL ("&="), 2 },
1682 { "aS", NL ("="), 2 },
1683 { "aa", NL ("&&"), 2 },
1684 { "ad", NL ("&"), 1 },
1685 { "an", NL ("&"), 2 },
1686 { "at", NL ("alignof "), 1 },
1687 { "az", NL ("alignof "), 1 },
1688 { "cc", NL ("const_cast"), 2 },
1689 { "cl", NL ("()"), 2 },
1690 { "cm", NL (","), 2 },
1691 { "co", NL ("~"), 1 },
1692 { "dV", NL ("/="), 2 },
1693 { "da", NL ("delete[] "), 1 },
1694 { "dc", NL ("dynamic_cast"), 2 },
1695 { "de", NL ("*"), 1 },
1696 { "dl", NL ("delete "), 1 },
1697 { "ds", NL (".*"), 2 },
1698 { "dt", NL ("."), 2 },
1699 { "dv", NL ("/"), 2 },
1700 { "eO", NL ("^="), 2 },
1701 { "eo", NL ("^"), 2 },
1702 { "eq", NL ("=="), 2 },
1703 { "ge", NL (">="), 2 },
1704 { "gs", NL ("::"), 1 },
1705 { "gt", NL (">"), 2 },
1706 { "ix", NL ("[]"), 2 },
1707 { "lS", NL ("<<="), 2 },
1708 { "le", NL ("<="), 2 },
1709 { "li", NL ("operator\"\" "), 1 },
1710 { "ls", NL ("<<"), 2 },
1711 { "lt", NL ("<"), 2 },
1712 { "mI", NL ("-="), 2 },
1713 { "mL", NL ("*="), 2 },
1714 { "mi", NL ("-"), 2 },
1715 { "ml", NL ("*"), 2 },
1716 { "mm", NL ("--"), 1 },
1717 { "na", NL ("new[]"), 3 },
1718 { "ne", NL ("!="), 2 },
1719 { "ng", NL ("-"), 1 },
1720 { "nt", NL ("!"), 1 },
1721 { "nw", NL ("new"), 3 },
1722 { "oR", NL ("|="), 2 },
1723 { "oo", NL ("||"), 2 },
1724 { "or", NL ("|"), 2 },
1725 { "pL", NL ("+="), 2 },
1726 { "pl", NL ("+"), 2 },
1727 { "pm", NL ("->*"), 2 },
1728 { "pp", NL ("++"), 1 },
1729 { "ps", NL ("+"), 1 },
1730 { "pt", NL ("->"), 2 },
1731 { "qu", NL ("?"), 3 },
1732 { "rM", NL ("%="), 2 },
1733 { "rS", NL (">>="), 2 },
1734 { "rc", NL ("reinterpret_cast"), 2 },
1735 { "rm", NL ("%"), 2 },
1736 { "rs", NL (">>"), 2 },
1737 { "sc", NL ("static_cast"), 2 },
1738 { "st", NL ("sizeof "), 1 },
1739 { "sz", NL ("sizeof "), 1 },
1740 { "tr", NL ("throw"), 0 },
1741 { "tw", NL ("throw "), 1 },
1742 { NULL, NULL, 0, 0 }
1745 static struct demangle_component *
1746 d_operator_name (struct d_info *di)
1751 c1 = d_next_char (di);
1752 c2 = d_next_char (di);
1753 if (c1 == 'v' && IS_DIGIT (c2))
1754 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1755 else if (c1 == 'c' && c2 == 'v')
1757 struct demangle_component *type;
1758 int was_conversion = di->is_conversion;
1760 di->is_conversion = ! di->is_expression;
1761 type = cplus_demangle_type (di);
1762 di->is_conversion = was_conversion;
1763 return d_make_comp (di, DEMANGLE_COMPONENT_CAST, type, NULL);
1767 /* LOW is the inclusive lower bound. */
1769 /* HIGH is the exclusive upper bound. We subtract one to ignore
1770 the sentinel at the end of the array. */
1771 int high = ((sizeof (cplus_demangle_operators)
1772 / sizeof (cplus_demangle_operators[0]))
1778 const struct demangle_operator_info *p;
1780 i = low + (high - low) / 2;
1781 p = cplus_demangle_operators + i;
1783 if (c1 == p->code[0] && c2 == p->code[1])
1784 return d_make_operator (di, p);
1786 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1796 static struct demangle_component *
1797 d_make_character (struct d_info *di, int c)
1799 struct demangle_component *p;
1800 p = d_make_empty (di);
1803 p->type = DEMANGLE_COMPONENT_CHARACTER;
1804 p->u.s_character.character = c;
1809 static struct demangle_component *
1810 d_java_resource (struct d_info *di)
1812 struct demangle_component *p = NULL;
1813 struct demangle_component *next = NULL;
1818 len = d_number (di);
1822 /* Eat the leading '_'. */
1823 if (d_next_char (di) != '_')
1836 /* Each chunk is either a '$' escape... */
1854 next = d_make_character (di, c);
1862 /* ... or a sequence of characters. */
1865 while (i < len && str[i] && str[i] != '$')
1868 next = d_make_name (di, str, i);
1881 p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next);
1887 p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL);
1892 /* <special-name> ::= TV <type>
1896 ::= GV <(object) name>
1897 ::= T <call-offset> <(base) encoding>
1898 ::= Tc <call-offset> <call-offset> <(base) encoding>
1899 Also g++ extensions:
1900 ::= TC <type> <(offset) number> _ <(base) type>
1905 ::= Gr <resource name>
1910 static struct demangle_component *
1911 d_special_name (struct d_info *di)
1913 di->expansion += 20;
1914 if (d_check_char (di, 'T'))
1916 switch (d_next_char (di))
1920 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
1921 cplus_demangle_type (di), NULL);
1923 di->expansion -= 10;
1924 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
1925 cplus_demangle_type (di), NULL);
1927 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
1928 cplus_demangle_type (di), NULL);
1930 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
1931 cplus_demangle_type (di), NULL);
1934 if (! d_call_offset (di, 'h'))
1936 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
1937 d_encoding (di, 0), NULL);
1940 if (! d_call_offset (di, 'v'))
1942 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
1943 d_encoding (di, 0), NULL);
1946 if (! d_call_offset (di, '\0'))
1948 if (! d_call_offset (di, '\0'))
1950 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
1951 d_encoding (di, 0), NULL);
1955 struct demangle_component *derived_type;
1957 struct demangle_component *base_type;
1959 derived_type = cplus_demangle_type (di);
1960 offset = d_number (di);
1963 if (! d_check_char (di, '_'))
1965 base_type = cplus_demangle_type (di);
1966 /* We don't display the offset. FIXME: We should display
1967 it in verbose mode. */
1969 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
1970 base_type, derived_type);
1974 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
1975 cplus_demangle_type (di), NULL);
1977 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
1978 cplus_demangle_type (di), NULL);
1981 return d_make_comp (di, DEMANGLE_COMPONENT_TLS_INIT,
1985 return d_make_comp (di, DEMANGLE_COMPONENT_TLS_WRAPPER,
1992 else if (d_check_char (di, 'G'))
1994 switch (d_next_char (di))
1997 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
2001 struct demangle_component *name = d_name (di);
2002 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, name,
2003 d_number_component (di));
2007 return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
2008 d_encoding (di, 0), NULL);
2011 switch (d_next_char (di))
2014 return d_make_comp (di, DEMANGLE_COMPONENT_NONTRANSACTION_CLONE,
2015 d_encoding (di, 0), NULL);
2017 /* ??? The proposal is that other letters (such as 'h') stand
2018 for different variants of transaction cloning, such as
2019 compiling directly for hardware transaction support. But
2020 they still should all be transactional clones of some sort
2021 so go ahead and call them that. */
2023 return d_make_comp (di, DEMANGLE_COMPONENT_TRANSACTION_CLONE,
2024 d_encoding (di, 0), NULL);
2028 return d_java_resource (di);
2038 /* <call-offset> ::= h <nv-offset> _
2041 <nv-offset> ::= <(offset) number>
2043 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
2045 The C parameter, if not '\0', is a character we just read which is
2046 the start of the <call-offset>.
2048 We don't display the offset information anywhere. FIXME: We should
2049 display it in verbose mode. */
2052 d_call_offset (struct d_info *di, int c)
2055 c = d_next_char (di);
2062 if (! d_check_char (di, '_'))
2069 if (! d_check_char (di, '_'))
2075 /* <ctor-dtor-name> ::= C1
2083 static struct demangle_component *
2084 d_ctor_dtor_name (struct d_info *di)
2086 if (di->last_name != NULL)
2088 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
2089 di->expansion += di->last_name->u.s_name.len;
2090 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
2091 di->expansion += di->last_name->u.s_string.len;
2093 switch (d_peek_char (di))
2097 enum gnu_v3_ctor_kinds kind;
2099 switch (d_peek_next_char (di))
2102 kind = gnu_v3_complete_object_ctor;
2105 kind = gnu_v3_base_object_ctor;
2108 kind = gnu_v3_complete_object_allocating_ctor;
2111 kind = gnu_v3_unified_ctor;
2114 kind = gnu_v3_object_ctor_group;
2120 return d_make_ctor (di, kind, di->last_name);
2125 enum gnu_v3_dtor_kinds kind;
2127 switch (d_peek_next_char (di))
2130 kind = gnu_v3_deleting_dtor;
2133 kind = gnu_v3_complete_object_dtor;
2136 kind = gnu_v3_base_object_dtor;
2138 /* digit '3' is not used */
2140 kind = gnu_v3_unified_dtor;
2143 kind = gnu_v3_object_dtor_group;
2149 return d_make_dtor (di, kind, di->last_name);
2157 /* <type> ::= <builtin-type>
2159 ::= <class-enum-type>
2161 ::= <pointer-to-member-type>
2162 ::= <template-param>
2163 ::= <template-template-param> <template-args>
2165 ::= <CV-qualifiers> <type>
2168 ::= O <type> (C++0x)
2171 ::= U <source-name> <type>
2173 <builtin-type> ::= various one letter codes
2177 CP_STATIC_IF_GLIBCPP_V3
2178 const struct demangle_builtin_type_info
2179 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
2181 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
2182 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
2183 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
2184 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
2185 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
2186 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
2187 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
2188 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
2189 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
2190 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
2191 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2192 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
2193 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
2194 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
2195 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
2197 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2198 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2199 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2200 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
2201 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
2202 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
2203 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
2204 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
2205 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
2206 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
2207 D_PRINT_UNSIGNED_LONG_LONG },
2208 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
2209 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT },
2210 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT },
2211 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT },
2212 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT },
2213 /* 30 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT },
2214 /* 31 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT },
2215 /* 32 */ { NL ("decltype(nullptr)"), NL ("decltype(nullptr)"),
2219 CP_STATIC_IF_GLIBCPP_V3
2220 struct demangle_component *
2221 cplus_demangle_type (struct d_info *di)
2224 struct demangle_component *ret;
2227 /* The ABI specifies that when CV-qualifiers are used, the base type
2228 is substitutable, and the fully qualified type is substitutable,
2229 but the base type with a strict subset of the CV-qualifiers is
2230 not substitutable. The natural recursive implementation of the
2231 CV-qualifiers would cause subsets to be substitutable, so instead
2232 we pull them all off now.
2234 FIXME: The ABI says that order-insensitive vendor qualifiers
2235 should be handled in the same way, but we have no way to tell
2236 which vendor qualifiers are order-insensitive and which are
2237 order-sensitive. So we just assume that they are all
2238 order-sensitive. g++ 3.4 supports only one vendor qualifier,
2239 __vector, and it treats it as order-sensitive when mangling
2242 peek = d_peek_char (di);
2243 if (peek == 'r' || peek == 'V' || peek == 'K')
2245 struct demangle_component **pret;
2247 pret = d_cv_qualifiers (di, &ret, 0);
2250 if (d_peek_char (di) == 'F')
2252 /* cv-qualifiers before a function type apply to 'this',
2253 so avoid adding the unqualified function type to
2254 the substitution list. */
2255 *pret = d_function_type (di);
2258 *pret = cplus_demangle_type (di);
2261 if ((*pret)->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
2262 || (*pret)->type == DEMANGLE_COMPONENT_REFERENCE_THIS)
2264 /* Move the ref-qualifier outside the cv-qualifiers so that
2265 they are printed in the right order. */
2266 struct demangle_component *fn = d_left (*pret);
2267 d_left (*pret) = ret;
2271 if (! d_add_substitution (di, ret))
2280 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2281 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
2282 case 'o': case 's': case 't':
2283 case 'v': case 'w': case 'x': case 'y': case 'z':
2284 ret = d_make_builtin_type (di,
2285 &cplus_demangle_builtin_types[peek - 'a']);
2286 di->expansion += ret->u.s_builtin.type->len;
2293 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
2294 d_source_name (di), NULL);
2298 ret = d_function_type (di);
2301 case '0': case '1': case '2': case '3': case '4':
2302 case '5': case '6': case '7': case '8': case '9':
2305 ret = d_class_enum_type (di);
2309 ret = d_array_type (di);
2313 ret = d_pointer_to_member_type (di);
2317 ret = d_template_param (di);
2318 if (d_peek_char (di) == 'I')
2320 /* This may be <template-template-param> <template-args>.
2321 If this is the type for a conversion operator, we can
2322 have a <template-template-param> here only by following
2323 a derivation like this:
2326 -> <template-prefix> <template-args>
2327 -> <prefix> <template-unqualified-name> <template-args>
2328 -> <unqualified-name> <template-unqualified-name> <template-args>
2329 -> <source-name> <template-unqualified-name> <template-args>
2330 -> <source-name> <operator-name> <template-args>
2331 -> <source-name> cv <type> <template-args>
2332 -> <source-name> cv <template-template-param> <template-args> <template-args>
2334 where the <template-args> is followed by another.
2335 Otherwise, we must have a derivation like this:
2338 -> <template-prefix> <template-args>
2339 -> <prefix> <template-unqualified-name> <template-args>
2340 -> <unqualified-name> <template-unqualified-name> <template-args>
2341 -> <source-name> <template-unqualified-name> <template-args>
2342 -> <source-name> <operator-name> <template-args>
2343 -> <source-name> cv <type> <template-args>
2344 -> <source-name> cv <template-param> <template-args>
2346 where we need to leave the <template-args> to be processed
2347 by d_prefix (following the <template-prefix>).
2349 The <template-template-param> part is a substitution
2351 if (! di->is_conversion)
2353 if (! d_add_substitution (di, ret))
2355 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2356 d_template_args (di));
2360 struct demangle_component *args;
2361 struct d_info_checkpoint checkpoint;
2363 d_checkpoint (di, &checkpoint);
2364 args = d_template_args (di);
2365 if (d_peek_char (di) == 'I')
2367 if (! d_add_substitution (di, ret))
2369 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2373 d_backtrack (di, &checkpoint);
2379 /* If this is a special substitution, then it is the start of
2380 <class-enum-type>. */
2384 peek_next = d_peek_next_char (di);
2385 if (IS_DIGIT (peek_next)
2387 || IS_UPPER (peek_next))
2389 ret = d_substitution (di, 0);
2390 /* The substituted name may have been a template name and
2391 may be followed by tepmlate args. */
2392 if (d_peek_char (di) == 'I')
2393 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
2394 d_template_args (di));
2400 ret = d_class_enum_type (di);
2401 /* If the substitution was a complete type, then it is not
2402 a new substitution candidate. However, if the
2403 substitution was followed by template arguments, then
2404 the whole thing is a substitution candidate. */
2405 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
2413 ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE,
2414 cplus_demangle_type (di), NULL);
2419 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
2420 cplus_demangle_type (di), NULL);
2425 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
2426 cplus_demangle_type (di), NULL);
2431 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
2432 cplus_demangle_type (di), NULL);
2437 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
2438 cplus_demangle_type (di), NULL);
2443 ret = d_source_name (di);
2444 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
2445 cplus_demangle_type (di), ret);
2451 peek = d_next_char (di);
2456 /* decltype (expression) */
2457 ret = d_make_comp (di, DEMANGLE_COMPONENT_DECLTYPE,
2458 d_expression (di), NULL);
2459 if (ret && d_next_char (di) != 'E')
2465 /* Pack expansion. */
2466 ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
2467 cplus_demangle_type (di), NULL);
2473 ret = d_make_name (di, "auto", 4);
2477 /* 32-bit decimal floating point */
2478 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[26]);
2479 di->expansion += ret->u.s_builtin.type->len;
2483 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[27]);
2484 di->expansion += ret->u.s_builtin.type->len;
2488 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[28]);
2489 di->expansion += ret->u.s_builtin.type->len;
2492 /* 16-bit half-precision FP */
2493 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[29]);
2494 di->expansion += ret->u.s_builtin.type->len;
2498 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[30]);
2499 di->expansion += ret->u.s_builtin.type->len;
2503 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[31]);
2504 di->expansion += ret->u.s_builtin.type->len;
2508 /* Fixed point types. DF<int bits><length><fract bits><sat> */
2509 ret = d_make_empty (di);
2510 ret->type = DEMANGLE_COMPONENT_FIXED_TYPE;
2511 if ((ret->u.s_fixed.accum = IS_DIGIT (d_peek_char (di))))
2512 /* For demangling we don't care about the bits. */
2514 ret->u.s_fixed.length = cplus_demangle_type (di);
2515 if (ret->u.s_fixed.length == NULL)
2518 peek = d_next_char (di);
2519 ret->u.s_fixed.sat = (peek == 's');
2523 ret = d_vector_type (di);
2528 /* decltype(nullptr) */
2529 ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[32]);
2530 di->expansion += ret->u.s_builtin.type->len;
2544 if (! d_add_substitution (di, ret))
2551 /* <CV-qualifiers> ::= [r] [V] [K] */
2553 static struct demangle_component **
2554 d_cv_qualifiers (struct d_info *di,
2555 struct demangle_component **pret, int member_fn)
2557 struct demangle_component **pstart;
2561 peek = d_peek_char (di);
2562 while (peek == 'r' || peek == 'V' || peek == 'K')
2564 enum demangle_component_type t;
2570 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2571 : DEMANGLE_COMPONENT_RESTRICT);
2572 di->expansion += sizeof "restrict";
2574 else if (peek == 'V')
2577 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2578 : DEMANGLE_COMPONENT_VOLATILE);
2579 di->expansion += sizeof "volatile";
2584 ? DEMANGLE_COMPONENT_CONST_THIS
2585 : DEMANGLE_COMPONENT_CONST);
2586 di->expansion += sizeof "const";
2589 *pret = d_make_comp (di, t, NULL, NULL);
2592 pret = &d_left (*pret);
2594 peek = d_peek_char (di);
2597 if (!member_fn && peek == 'F')
2599 while (pstart != pret)
2601 switch ((*pstart)->type)
2603 case DEMANGLE_COMPONENT_RESTRICT:
2604 (*pstart)->type = DEMANGLE_COMPONENT_RESTRICT_THIS;
2606 case DEMANGLE_COMPONENT_VOLATILE:
2607 (*pstart)->type = DEMANGLE_COMPONENT_VOLATILE_THIS;
2609 case DEMANGLE_COMPONENT_CONST:
2610 (*pstart)->type = DEMANGLE_COMPONENT_CONST_THIS;
2615 pstart = &d_left (*pstart);
2622 /* <ref-qualifier> ::= R
2625 static struct demangle_component *
2626 d_ref_qualifier (struct d_info *di, struct demangle_component *sub)
2628 struct demangle_component *ret = sub;
2631 peek = d_peek_char (di);
2632 if (peek == 'R' || peek == 'O')
2634 enum demangle_component_type t;
2637 t = DEMANGLE_COMPONENT_REFERENCE_THIS;
2638 di->expansion += sizeof "&";
2642 t = DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS;
2643 di->expansion += sizeof "&&";
2647 ret = d_make_comp (di, t, ret, NULL);
2653 /* <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] E */
2655 static struct demangle_component *
2656 d_function_type (struct d_info *di)
2658 struct demangle_component *ret;
2660 if (! d_check_char (di, 'F'))
2662 if (d_peek_char (di) == 'Y')
2664 /* Function has C linkage. We don't print this information.
2665 FIXME: We should print it in verbose mode. */
2668 ret = d_bare_function_type (di, 1);
2669 ret = d_ref_qualifier (di, ret);
2671 if (! d_check_char (di, 'E'))
2678 static struct demangle_component *
2679 d_parmlist (struct d_info *di)
2681 struct demangle_component *tl;
2682 struct demangle_component **ptl;
2688 struct demangle_component *type;
2690 char peek = d_peek_char (di);
2691 if (peek == '\0' || peek == 'E' || peek == '.')
2693 if ((peek == 'R' || peek == 'O')
2694 && d_peek_next_char (di) == 'E')
2695 /* Function ref-qualifier, not a ref prefix for a parameter type. */
2697 type = cplus_demangle_type (di);
2700 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
2703 ptl = &d_right (*ptl);
2706 /* There should be at least one parameter type besides the optional
2707 return type. A function which takes no arguments will have a
2708 single parameter type void. */
2712 /* If we have a single parameter type void, omit it. */
2713 if (d_right (tl) == NULL
2714 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2715 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
2717 di->expansion -= d_left (tl)->u.s_builtin.type->len;
2724 /* <bare-function-type> ::= [J]<type>+ */
2726 static struct demangle_component *
2727 d_bare_function_type (struct d_info *di, int has_return_type)
2729 struct demangle_component *return_type;
2730 struct demangle_component *tl;
2733 /* Detect special qualifier indicating that the first argument
2734 is the return type. */
2735 peek = d_peek_char (di);
2739 has_return_type = 1;
2742 if (has_return_type)
2744 return_type = cplus_demangle_type (di);
2745 if (return_type == NULL)
2751 tl = d_parmlist (di);
2755 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE,
2759 /* <class-enum-type> ::= <name> */
2761 static struct demangle_component *
2762 d_class_enum_type (struct d_info *di)
2767 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2768 ::= A [<(dimension) expression>] _ <(element) type>
2771 static struct demangle_component *
2772 d_array_type (struct d_info *di)
2775 struct demangle_component *dim;
2777 if (! d_check_char (di, 'A'))
2780 peek = d_peek_char (di);
2783 else if (IS_DIGIT (peek))
2791 peek = d_peek_char (di);
2793 while (IS_DIGIT (peek));
2794 dim = d_make_name (di, s, d_str (di) - s);
2800 dim = d_expression (di);
2805 if (! d_check_char (di, '_'))
2808 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2809 cplus_demangle_type (di));
2812 /* <vector-type> ::= Dv <number> _ <type>
2813 ::= Dv _ <expression> _ <type> */
2815 static struct demangle_component *
2816 d_vector_type (struct d_info *di)
2819 struct demangle_component *dim;
2821 peek = d_peek_char (di);
2825 dim = d_expression (di);
2828 dim = d_number_component (di);
2833 if (! d_check_char (di, '_'))
2836 return d_make_comp (di, DEMANGLE_COMPONENT_VECTOR_TYPE, dim,
2837 cplus_demangle_type (di));
2840 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2842 static struct demangle_component *
2843 d_pointer_to_member_type (struct d_info *di)
2845 struct demangle_component *cl;
2846 struct demangle_component *mem;
2848 if (! d_check_char (di, 'M'))
2851 cl = cplus_demangle_type (di);
2855 /* The ABI says, "The type of a non-static member function is considered
2856 to be different, for the purposes of substitution, from the type of a
2857 namespace-scope or static member function whose type appears
2858 similar. The types of two non-static member functions are considered
2859 to be different, for the purposes of substitution, if the functions
2860 are members of different classes. In other words, for the purposes of
2861 substitution, the class of which the function is a member is
2862 considered part of the type of function."
2864 For a pointer to member function, this call to cplus_demangle_type
2865 will end up adding a (possibly qualified) non-member function type to
2866 the substitution table, which is not correct; however, the member
2867 function type will never be used in a substitution, so putting the
2868 wrong type in the substitution table is harmless. */
2870 mem = cplus_demangle_type (di);
2874 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
2877 /* <non-negative number> _ */
2880 d_compact_number (struct d_info *di)
2883 if (d_peek_char (di) == '_')
2885 else if (d_peek_char (di) == 'n')
2888 num = d_number (di) + 1;
2890 if (! d_check_char (di, '_'))
2895 /* <template-param> ::= T_
2896 ::= T <(parameter-2 non-negative) number> _
2899 static struct demangle_component *
2900 d_template_param (struct d_info *di)
2904 if (! d_check_char (di, 'T'))
2907 param = d_compact_number (di);
2913 return d_make_template_param (di, param);
2916 /* <template-args> ::= I <template-arg>+ E */
2918 static struct demangle_component *
2919 d_template_args (struct d_info *di)
2921 struct demangle_component *hold_last_name;
2922 struct demangle_component *al;
2923 struct demangle_component **pal;
2925 /* Preserve the last name we saw--don't let the template arguments
2926 clobber it, as that would give us the wrong name for a subsequent
2927 constructor or destructor. */
2928 hold_last_name = di->last_name;
2930 if (d_peek_char (di) != 'I'
2931 && d_peek_char (di) != 'J')
2935 if (d_peek_char (di) == 'E')
2937 /* An argument pack can be empty. */
2939 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, NULL, NULL);
2946 struct demangle_component *a;
2948 a = d_template_arg (di);
2952 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
2955 pal = &d_right (*pal);
2957 if (d_peek_char (di) == 'E')
2964 di->last_name = hold_last_name;
2969 /* <template-arg> ::= <type>
2970 ::= X <expression> E
2974 static struct demangle_component *
2975 d_template_arg (struct d_info *di)
2977 struct demangle_component *ret;
2979 switch (d_peek_char (di))
2983 ret = d_expression (di);
2984 if (! d_check_char (di, 'E'))
2989 return d_expr_primary (di);
2993 /* An argument pack. */
2994 return d_template_args (di);
2997 return cplus_demangle_type (di);
3001 /* Parse a sequence of expressions until we hit the terminator
3004 static struct demangle_component *
3005 d_exprlist (struct d_info *di, char terminator)
3007 struct demangle_component *list = NULL;
3008 struct demangle_component **p = &list;
3010 if (d_peek_char (di) == terminator)
3013 return d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, NULL, NULL);
3018 struct demangle_component *arg = d_expression (di);
3022 *p = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, arg, NULL);
3027 if (d_peek_char (di) == terminator)
3037 /* Returns nonzero iff OP is an operator for a C++ cast: const_cast,
3038 dynamic_cast, static_cast or reinterpret_cast. */
3041 op_is_new_cast (struct demangle_component *op)
3043 const char *code = op->u.s_operator.op->code;
3044 return (code[1] == 'c'
3045 && (code[0] == 's' || code[0] == 'd'
3046 || code[0] == 'c' || code[0] == 'r'));
3049 /* <expression> ::= <(unary) operator-name> <expression>
3050 ::= <(binary) operator-name> <expression> <expression>
3051 ::= <(trinary) operator-name> <expression> <expression> <expression>
3052 ::= cl <expression>+ E
3054 ::= <template-param>
3055 ::= sr <type> <unqualified-name>
3056 ::= sr <type> <unqualified-name> <template-args>
3060 static inline struct demangle_component *
3061 d_expression_1 (struct d_info *di)
3065 peek = d_peek_char (di);
3067 return d_expr_primary (di);
3068 else if (peek == 'T')
3069 return d_template_param (di);
3070 else if (peek == 's' && d_peek_next_char (di) == 'r')
3072 struct demangle_component *type;
3073 struct demangle_component *name;
3076 type = cplus_demangle_type (di);
3077 name = d_unqualified_name (di);
3078 if (d_peek_char (di) != 'I')
3079 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
3081 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
3082 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
3083 d_template_args (di)));
3085 else if (peek == 's' && d_peek_next_char (di) == 'p')
3088 return d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
3089 d_expression_1 (di), NULL);
3091 else if (peek == 'f' && d_peek_next_char (di) == 'p')
3093 /* Function parameter used in a late-specified return type. */
3096 if (d_peek_char (di) == 'T')
3098 /* 'this' parameter. */
3104 index = d_compact_number (di) + 1;
3108 return d_make_function_param (di, index);
3110 else if (IS_DIGIT (peek)
3111 || (peek == 'o' && d_peek_next_char (di) == 'n'))
3113 /* We can get an unqualified name as an expression in the case of
3114 a dependent function call, i.e. decltype(f(t)). */
3115 struct demangle_component *name;
3118 /* operator-function-id, i.e. operator+(t). */
3121 name = d_unqualified_name (di);
3124 if (d_peek_char (di) == 'I')
3125 return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
3126 d_template_args (di));
3130 else if ((peek == 'i' || peek == 't')
3131 && d_peek_next_char (di) == 'l')
3133 /* Brace-enclosed initializer list, untyped or typed. */
3134 struct demangle_component *type = NULL;
3136 type = cplus_demangle_type (di);
3138 return d_make_comp (di, DEMANGLE_COMPONENT_INITIALIZER_LIST,
3139 type, d_exprlist (di, 'E'));
3143 struct demangle_component *op;
3144 const char *code = NULL;
3147 op = d_operator_name (di);
3151 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
3153 code = op->u.s_operator.op->code;
3154 di->expansion += op->u.s_operator.op->len - 2;
3155 if (strcmp (code, "st") == 0)
3156 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3157 cplus_demangle_type (di));
3164 case DEMANGLE_COMPONENT_OPERATOR:
3165 args = op->u.s_operator.op->args;
3167 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3168 args = op->u.s_extended_operator.args;
3170 case DEMANGLE_COMPONENT_CAST:
3178 return d_make_comp (di, DEMANGLE_COMPONENT_NULLARY, op, NULL);
3182 struct demangle_component *operand;
3185 if (code && (code[0] == 'p' || code[0] == 'm')
3186 && code[1] == code[0])
3187 /* pp_ and mm_ are the prefix variants. */
3188 suffix = !d_check_char (di, '_');
3190 if (op->type == DEMANGLE_COMPONENT_CAST
3191 && d_check_char (di, '_'))
3192 operand = d_exprlist (di, 'E');
3194 operand = d_expression_1 (di);
3197 /* Indicate the suffix variant for d_print_comp. */
3198 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3200 DEMANGLE_COMPONENT_BINARY_ARGS,
3203 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
3208 struct demangle_component *left;
3209 struct demangle_component *right;
3211 if (op_is_new_cast (op))
3212 left = cplus_demangle_type (di);
3214 left = d_expression_1 (di);
3215 if (!strcmp (code, "cl"))
3216 right = d_exprlist (di, 'E');
3217 else if (!strcmp (code, "dt") || !strcmp (code, "pt"))
3219 right = d_unqualified_name (di);
3220 if (d_peek_char (di) == 'I')
3221 right = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE,
3222 right, d_template_args (di));
3225 right = d_expression_1 (di);
3227 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
3229 DEMANGLE_COMPONENT_BINARY_ARGS,
3234 struct demangle_component *first;
3235 struct demangle_component *second;
3236 struct demangle_component *third;
3238 if (!strcmp (code, "qu"))
3240 /* ?: expression. */
3241 first = d_expression_1 (di);
3242 second = d_expression_1 (di);
3243 third = d_expression_1 (di);
3245 else if (code[0] == 'n')
3247 /* new-expression. */
3248 if (code[1] != 'w' && code[1] != 'a')
3250 first = d_exprlist (di, '_');
3251 second = cplus_demangle_type (di);
3252 if (d_peek_char (di) == 'E')
3257 else if (d_peek_char (di) == 'p'
3258 && d_peek_next_char (di) == 'i')
3260 /* Parenthesized initializer. */
3262 third = d_exprlist (di, 'E');
3264 else if (d_peek_char (di) == 'i'
3265 && d_peek_next_char (di) == 'l')
3266 /* initializer-list. */
3267 third = d_expression_1 (di);
3273 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
3275 DEMANGLE_COMPONENT_TRINARY_ARG1,
3278 DEMANGLE_COMPONENT_TRINARY_ARG2,
3287 static struct demangle_component *
3288 d_expression (struct d_info *di)
3290 struct demangle_component *ret;
3291 int was_expression = di->is_expression;
3293 di->is_expression = 1;
3294 ret = d_expression_1 (di);
3295 di->is_expression = was_expression;
3299 /* <expr-primary> ::= L <type> <(value) number> E
3300 ::= L <type> <(value) float> E
3301 ::= L <mangled-name> E
3304 static struct demangle_component *
3305 d_expr_primary (struct d_info *di)
3307 struct demangle_component *ret;
3309 if (! d_check_char (di, 'L'))
3311 if (d_peek_char (di) == '_'
3312 /* Workaround for G++ bug; see comment in write_template_arg. */
3313 || d_peek_char (di) == 'Z')
3314 ret = cplus_demangle_mangled_name (di, 0);
3317 struct demangle_component *type;
3318 enum demangle_component_type t;
3321 type = cplus_demangle_type (di);
3325 /* If we have a type we know how to print, we aren't going to
3326 print the type name itself. */
3327 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
3328 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
3329 di->expansion -= type->u.s_builtin.type->len;
3331 /* Rather than try to interpret the literal value, we just
3332 collect it as a string. Note that it's possible to have a
3333 floating point literal here. The ABI specifies that the
3334 format of such literals is machine independent. That's fine,
3335 but what's not fine is that versions of g++ up to 3.2 with
3336 -fabi-version=1 used upper case letters in the hex constant,
3337 and dumped out gcc's internal representation. That makes it
3338 hard to tell where the constant ends, and hard to dump the
3339 constant in any readable form anyhow. We don't attempt to
3340 handle these cases. */
3342 t = DEMANGLE_COMPONENT_LITERAL;
3343 if (d_peek_char (di) == 'n')
3345 t = DEMANGLE_COMPONENT_LITERAL_NEG;
3349 while (d_peek_char (di) != 'E')
3351 if (d_peek_char (di) == '\0')
3355 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
3357 if (! d_check_char (di, 'E'))
3362 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
3363 ::= Z <(function) encoding> E s [<discriminator>]
3364 ::= Z <(function) encoding> E d [<parameter> number>] _ <entity name>
3367 static struct demangle_component *
3368 d_local_name (struct d_info *di)
3370 struct demangle_component *function;
3372 if (! d_check_char (di, 'Z'))
3375 function = d_encoding (di, 0);
3377 if (! d_check_char (di, 'E'))
3380 if (d_peek_char (di) == 's')
3383 if (! d_discriminator (di))
3385 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
3386 d_make_name (di, "string literal",
3387 sizeof "string literal" - 1));
3391 struct demangle_component *name;
3394 if (d_peek_char (di) == 'd')
3396 /* Default argument scope: d <number> _. */
3398 num = d_compact_number (di);
3407 /* Lambdas and unnamed types have internal discriminators. */
3408 case DEMANGLE_COMPONENT_LAMBDA:
3409 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
3412 if (! d_discriminator (di))
3416 name = d_make_default_arg (di, num, name);
3417 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
3421 /* <discriminator> ::= _ <(non-negative) number>
3423 We demangle the discriminator, but we don't print it out. FIXME:
3424 We should print it out in verbose mode. */
3427 d_discriminator (struct d_info *di)
3431 if (d_peek_char (di) != '_')
3434 discrim = d_number (di);
3440 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ */
3442 static struct demangle_component *
3443 d_lambda (struct d_info *di)
3445 struct demangle_component *tl;
3446 struct demangle_component *ret;
3449 if (! d_check_char (di, 'U'))
3451 if (! d_check_char (di, 'l'))
3454 tl = d_parmlist (di);
3458 if (! d_check_char (di, 'E'))
3461 num = d_compact_number (di);
3465 ret = d_make_empty (di);
3468 ret->type = DEMANGLE_COMPONENT_LAMBDA;
3469 ret->u.s_unary_num.sub = tl;
3470 ret->u.s_unary_num.num = num;
3473 if (! d_add_substitution (di, ret))
3479 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
3481 static struct demangle_component *
3482 d_unnamed_type (struct d_info *di)
3484 struct demangle_component *ret;
3487 if (! d_check_char (di, 'U'))
3489 if (! d_check_char (di, 't'))
3492 num = d_compact_number (di);
3496 ret = d_make_empty (di);
3499 ret->type = DEMANGLE_COMPONENT_UNNAMED_TYPE;
3500 ret->u.s_number.number = num;
3503 if (! d_add_substitution (di, ret))
3509 /* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]*
3512 static struct demangle_component *
3513 d_clone_suffix (struct d_info *di, struct demangle_component *encoding)
3515 const char *suffix = d_str (di);
3516 const char *pend = suffix;
3517 struct demangle_component *n;
3519 if (*pend == '.' && (IS_LOWER (pend[1]) || pend[1] == '_'))
3522 while (IS_LOWER (*pend) || *pend == '_')
3525 while (*pend == '.' && IS_DIGIT (pend[1]))
3528 while (IS_DIGIT (*pend))
3531 d_advance (di, pend - suffix);
3532 n = d_make_name (di, suffix, pend - suffix);
3533 return d_make_comp (di, DEMANGLE_COMPONENT_CLONE, encoding, n);
3536 /* Add a new substitution. */
3539 d_add_substitution (struct d_info *di, struct demangle_component *dc)
3543 if (di->next_sub >= di->num_subs)
3545 di->subs[di->next_sub] = dc;
3550 /* <substitution> ::= S <seq-id> _
3560 If PREFIX is non-zero, then this type is being used as a prefix in
3561 a qualified name. In this case, for the standard substitutions, we
3562 need to check whether we are being used as a prefix for a
3563 constructor or destructor, and return a full template name.
3564 Otherwise we will get something like std::iostream::~iostream()
3565 which does not correspond particularly well to any function which
3566 actually appears in the source.
3569 static const struct d_standard_sub_info standard_subs[] =
3574 { 'a', NL ("std::allocator"),
3575 NL ("std::allocator"),
3577 { 'b', NL ("std::basic_string"),
3578 NL ("std::basic_string"),
3579 NL ("basic_string") },
3580 { 's', NL ("std::string"),
3581 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
3582 NL ("basic_string") },
3583 { 'i', NL ("std::istream"),
3584 NL ("std::basic_istream<char, std::char_traits<char> >"),
3585 NL ("basic_istream") },
3586 { 'o', NL ("std::ostream"),
3587 NL ("std::basic_ostream<char, std::char_traits<char> >"),
3588 NL ("basic_ostream") },
3589 { 'd', NL ("std::iostream"),
3590 NL ("std::basic_iostream<char, std::char_traits<char> >"),
3591 NL ("basic_iostream") }
3594 static struct demangle_component *
3595 d_substitution (struct d_info *di, int prefix)
3599 if (! d_check_char (di, 'S'))
3602 c = d_next_char (di);
3603 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
3612 unsigned int new_id;
3615 new_id = id * 36 + c - '0';
3616 else if (IS_UPPER (c))
3617 new_id = id * 36 + c - 'A' + 10;
3623 c = d_next_char (di);
3630 if (id >= (unsigned int) di->next_sub)
3635 return di->subs[id];
3640 const struct d_standard_sub_info *p;
3641 const struct d_standard_sub_info *pend;
3643 verbose = (di->options & DMGL_VERBOSE) != 0;
3644 if (! verbose && prefix)
3648 peek = d_peek_char (di);
3649 if (peek == 'C' || peek == 'D')
3653 pend = (&standard_subs[0]
3654 + sizeof standard_subs / sizeof standard_subs[0]);
3655 for (p = &standard_subs[0]; p < pend; ++p)
3662 if (p->set_last_name != NULL)
3663 di->last_name = d_make_sub (di, p->set_last_name,
3664 p->set_last_name_len);
3667 s = p->full_expansion;
3672 s = p->simple_expansion;
3673 len = p->simple_len;
3675 di->expansion += len;
3676 return d_make_sub (di, s, len);
3685 d_checkpoint (struct d_info *di, struct d_info_checkpoint *checkpoint)
3687 checkpoint->n = di->n;
3688 checkpoint->next_comp = di->next_comp;
3689 checkpoint->next_sub = di->next_sub;
3690 checkpoint->did_subs = di->did_subs;
3691 checkpoint->expansion = di->expansion;
3695 d_backtrack (struct d_info *di, struct d_info_checkpoint *checkpoint)
3697 di->n = checkpoint->n;
3698 di->next_comp = checkpoint->next_comp;
3699 di->next_sub = checkpoint->next_sub;
3700 di->did_subs = checkpoint->did_subs;
3701 di->expansion = checkpoint->expansion;
3704 /* Initialize a growable string. */
3707 d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
3712 dgs->allocation_failure = 0;
3715 d_growable_string_resize (dgs, estimate);
3718 /* Grow a growable string to a given size. */
3721 d_growable_string_resize (struct d_growable_string *dgs, size_t need)
3726 if (dgs->allocation_failure)
3729 /* Start allocation at two bytes to avoid any possibility of confusion
3730 with the special value of 1 used as a return in *palc to indicate
3731 allocation failures. */
3732 newalc = dgs->alc > 0 ? dgs->alc : 2;
3733 while (newalc < need)
3736 newbuf = (char *) realloc (dgs->buf, newalc);
3743 dgs->allocation_failure = 1;
3750 /* Append a buffer to a growable string. */
3753 d_growable_string_append_buffer (struct d_growable_string *dgs,
3754 const char *s, size_t l)
3758 need = dgs->len + l + 1;
3759 if (need > dgs->alc)
3760 d_growable_string_resize (dgs, need);
3762 if (dgs->allocation_failure)
3765 memcpy (dgs->buf + dgs->len, s, l);
3766 dgs->buf[dgs->len + l] = '\0';
3770 /* Bridge growable strings to the callback mechanism. */
3773 d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
3775 struct d_growable_string *dgs = (struct d_growable_string*) opaque;
3777 d_growable_string_append_buffer (dgs, s, l);
3780 /* Initialize a print information structure. */
3783 d_print_init (struct d_print_info *dpi, demangle_callbackref callback,
3787 dpi->last_char = '\0';
3788 dpi->templates = NULL;
3789 dpi->modifiers = NULL;
3790 dpi->pack_index = 0;
3791 dpi->flush_count = 0;
3793 dpi->callback = callback;
3794 dpi->opaque = opaque;
3796 dpi->demangle_failure = 0;
3798 dpi->saved_scopes = NULL;
3799 dpi->num_saved_scopes = 0;
3800 dpi->current_template = NULL;
3803 /* Free a print information structure. */
3806 d_print_free (struct d_print_info *dpi)
3810 for (i = 0; i < dpi->num_saved_scopes; i++)
3812 struct d_print_template *ts, *tn;
3814 for (ts = dpi->saved_scopes[i].templates; ts != NULL; ts = tn)
3821 free (dpi->saved_scopes);
3824 /* Indicate that an error occurred during printing, and test for error. */
3827 d_print_error (struct d_print_info *dpi)
3829 dpi->demangle_failure = 1;
3833 d_print_saw_error (struct d_print_info *dpi)
3835 return dpi->demangle_failure != 0;
3838 /* Flush buffered characters to the callback. */
3841 d_print_flush (struct d_print_info *dpi)
3843 dpi->buf[dpi->len] = '\0';
3844 dpi->callback (dpi->buf, dpi->len, dpi->opaque);
3849 /* Append characters and buffers for printing. */
3852 d_append_char (struct d_print_info *dpi, char c)
3854 if (dpi->len == sizeof (dpi->buf) - 1)
3855 d_print_flush (dpi);
3857 dpi->buf[dpi->len++] = c;
3862 d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
3866 for (i = 0; i < l; i++)
3867 d_append_char (dpi, s[i]);
3871 d_append_string (struct d_print_info *dpi, const char *s)
3873 d_append_buffer (dpi, s, strlen (s));
3877 d_append_num (struct d_print_info *dpi, long l)
3880 sprintf (buf,"%ld", l);
3881 d_append_string (dpi, buf);
3885 d_last_char (struct d_print_info *dpi)
3887 return dpi->last_char;
3890 /* Turn components into a human readable string. OPTIONS is the
3891 options bits passed to the demangler. DC is the tree to print.
3892 CALLBACK is a function to call to flush demangled string segments
3893 as they fill the intermediate buffer, and OPAQUE is a generalized
3894 callback argument. On success, this returns 1. On failure,
3895 it returns 0, indicating a bad parse. It does not use heap
3896 memory to build an output string, so cannot encounter memory
3897 allocation failure. */
3899 CP_STATIC_IF_GLIBCPP_V3
3901 cplus_demangle_print_callback (int options,
3902 const struct demangle_component *dc,
3903 demangle_callbackref callback, void *opaque)
3905 struct d_print_info dpi;
3908 d_print_init (&dpi, callback, opaque);
3910 d_print_comp (&dpi, options, dc);
3912 d_print_flush (&dpi);
3914 success = ! d_print_saw_error (&dpi);
3915 d_print_free (&dpi);
3919 /* Turn components into a human readable string. OPTIONS is the
3920 options bits passed to the demangler. DC is the tree to print.
3921 ESTIMATE is a guess at the length of the result. This returns a
3922 string allocated by malloc, or NULL on error. On success, this
3923 sets *PALC to the size of the allocated buffer. On failure, this
3924 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
3927 CP_STATIC_IF_GLIBCPP_V3
3929 cplus_demangle_print (int options, const struct demangle_component *dc,
3930 int estimate, size_t *palc)
3932 struct d_growable_string dgs;
3934 d_growable_string_init (&dgs, estimate);
3936 if (! cplus_demangle_print_callback (options, dc,
3937 d_growable_string_callback_adapter,
3945 *palc = dgs.allocation_failure ? 1 : dgs.alc;
3949 /* Returns the I'th element of the template arglist ARGS, or NULL on
3952 static struct demangle_component *
3953 d_index_template_argument (struct demangle_component *args, int i)
3955 struct demangle_component *a;
3961 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
3967 if (i != 0 || a == NULL)
3973 /* Returns the template argument from the current context indicated by DC,
3974 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
3976 static struct demangle_component *
3977 d_lookup_template_argument (struct d_print_info *dpi,
3978 const struct demangle_component *dc)
3980 if (dpi->templates == NULL)
3982 d_print_error (dpi);
3986 return d_index_template_argument
3987 (d_right (dpi->templates->template_decl),
3988 dc->u.s_number.number);
3991 /* Returns a template argument pack used in DC (any will do), or NULL. */
3993 static struct demangle_component *
3994 d_find_pack (struct d_print_info *dpi,
3995 const struct demangle_component *dc)
3997 struct demangle_component *a;
4003 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
4004 a = d_lookup_template_argument (dpi, dc);
4005 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4009 case DEMANGLE_COMPONENT_PACK_EXPANSION:
4012 case DEMANGLE_COMPONENT_LAMBDA:
4013 case DEMANGLE_COMPONENT_NAME:
4014 case DEMANGLE_COMPONENT_TAGGED_NAME:
4015 case DEMANGLE_COMPONENT_OPERATOR:
4016 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
4017 case DEMANGLE_COMPONENT_SUB_STD:
4018 case DEMANGLE_COMPONENT_CHARACTER:
4019 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
4020 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
4023 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
4024 return d_find_pack (dpi, dc->u.s_extended_operator.name);
4025 case DEMANGLE_COMPONENT_CTOR:
4026 return d_find_pack (dpi, dc->u.s_ctor.name);
4027 case DEMANGLE_COMPONENT_DTOR:
4028 return d_find_pack (dpi, dc->u.s_dtor.name);
4031 a = d_find_pack (dpi, d_left (dc));
4034 return d_find_pack (dpi, d_right (dc));
4038 /* Returns the length of the template argument pack DC. */
4041 d_pack_length (const struct demangle_component *dc)
4044 while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
4045 && d_left (dc) != NULL)
4053 /* DC is a component of a mangled expression. Print it, wrapped in parens
4057 d_print_subexpr (struct d_print_info *dpi, int options,
4058 const struct demangle_component *dc)
4061 if (dc->type == DEMANGLE_COMPONENT_NAME
4062 || dc->type == DEMANGLE_COMPONENT_QUAL_NAME
4063 || dc->type == DEMANGLE_COMPONENT_INITIALIZER_LIST
4064 || dc->type == DEMANGLE_COMPONENT_FUNCTION_PARAM)
4067 d_append_char (dpi, '(');
4068 d_print_comp (dpi, options, dc);
4070 d_append_char (dpi, ')');
4073 /* Return a shallow copy of the current list of templates.
4074 On error d_print_error is called and a partial list may
4075 be returned. Whatever is returned must be freed. */
4077 static struct d_print_template *
4078 d_copy_templates (struct d_print_info *dpi)
4080 struct d_print_template *src, *result, **link = &result;
4082 for (src = dpi->templates; src != NULL; src = src->next)
4084 struct d_print_template *dst =
4085 (struct d_print_template *) malloc (sizeof (struct d_print_template));
4089 d_print_error (dpi);
4093 dst->template_decl = src->template_decl;
4103 /* Subroutine to handle components. */
4106 d_print_comp (struct d_print_info *dpi, int options,
4107 const struct demangle_component *dc)
4109 /* Magic variable to let reference smashing skip over the next modifier
4110 without needing to modify *dc. */
4111 const struct demangle_component *mod_inner = NULL;
4113 /* Variable used to store the current templates while a previously
4114 captured scope is used. */
4115 struct d_print_template *saved_templates;
4117 /* Nonzero if templates have been stored in the above variable. */
4118 int need_template_restore = 0;
4122 d_print_error (dpi);
4125 if (d_print_saw_error (dpi))
4130 case DEMANGLE_COMPONENT_NAME:
4131 if ((options & DMGL_JAVA) == 0)
4132 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
4134 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
4137 case DEMANGLE_COMPONENT_TAGGED_NAME:
4138 d_print_comp (dpi, options, d_left (dc));
4139 d_append_string (dpi, "[abi:");
4140 d_print_comp (dpi, options, d_right (dc));
4141 d_append_char (dpi, ']');
4144 case DEMANGLE_COMPONENT_QUAL_NAME:
4145 case DEMANGLE_COMPONENT_LOCAL_NAME:
4146 d_print_comp (dpi, options, d_left (dc));
4147 if ((options & DMGL_JAVA) == 0)
4148 d_append_string (dpi, "::");
4150 d_append_char (dpi, '.');
4152 struct demangle_component *local_name = d_right (dc);
4153 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
4155 d_append_string (dpi, "{default arg#");
4156 d_append_num (dpi, local_name->u.s_unary_num.num + 1);
4157 d_append_string (dpi, "}::");
4158 local_name = local_name->u.s_unary_num.sub;
4160 d_print_comp (dpi, options, local_name);
4164 case DEMANGLE_COMPONENT_TYPED_NAME:
4166 struct d_print_mod *hold_modifiers;
4167 struct demangle_component *typed_name;
4168 struct d_print_mod adpm[4];
4170 struct d_print_template dpt;
4172 /* Pass the name down to the type so that it can be printed in
4173 the right place for the type. We also have to pass down
4174 any CV-qualifiers, which apply to the this parameter. */
4175 hold_modifiers = dpi->modifiers;
4178 typed_name = d_left (dc);
4179 while (typed_name != NULL)
4181 if (i >= sizeof adpm / sizeof adpm[0])
4183 d_print_error (dpi);
4187 adpm[i].next = dpi->modifiers;
4188 dpi->modifiers = &adpm[i];
4189 adpm[i].mod = typed_name;
4190 adpm[i].printed = 0;
4191 adpm[i].templates = dpi->templates;
4194 if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
4195 && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
4196 && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS
4197 && typed_name->type != DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
4198 && typed_name->type != DEMANGLE_COMPONENT_REFERENCE_THIS)
4201 typed_name = d_left (typed_name);
4204 if (typed_name == NULL)
4206 d_print_error (dpi);
4210 /* If typed_name is a template, then it applies to the
4211 function type as well. */
4212 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
4214 dpt.next = dpi->templates;
4215 dpi->templates = &dpt;
4216 dpt.template_decl = typed_name;
4219 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
4220 there may be CV-qualifiers on its right argument which
4221 really apply here; this happens when parsing a class which
4222 is local to a function. */
4223 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
4225 struct demangle_component *local_name;
4227 local_name = d_right (typed_name);
4228 if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
4229 local_name = local_name->u.s_unary_num.sub;
4230 while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
4231 || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
4232 || local_name->type == DEMANGLE_COMPONENT_CONST_THIS
4233 || local_name->type == DEMANGLE_COMPONENT_REFERENCE_THIS
4234 || (local_name->type
4235 == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS))
4237 if (i >= sizeof adpm / sizeof adpm[0])
4239 d_print_error (dpi);
4243 adpm[i] = adpm[i - 1];
4244 adpm[i].next = &adpm[i - 1];
4245 dpi->modifiers = &adpm[i];
4247 adpm[i - 1].mod = local_name;
4248 adpm[i - 1].printed = 0;
4249 adpm[i - 1].templates = dpi->templates;
4252 local_name = d_left (local_name);
4256 d_print_comp (dpi, options, d_right (dc));
4258 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
4259 dpi->templates = dpt.next;
4261 /* If the modifiers didn't get printed by the type, print them
4266 if (! adpm[i].printed)
4268 d_append_char (dpi, ' ');
4269 d_print_mod (dpi, options, adpm[i].mod);
4273 dpi->modifiers = hold_modifiers;
4278 case DEMANGLE_COMPONENT_TEMPLATE:
4280 struct d_print_mod *hold_dpm;
4281 struct demangle_component *dcl;
4282 const struct demangle_component *hold_current;
4284 /* This template may need to be referenced by a cast operator
4285 contained in its subtree. */
4286 hold_current = dpi->current_template;
4287 dpi->current_template = dc;
4289 /* Don't push modifiers into a template definition. Doing so
4290 could give the wrong definition for a template argument.
4291 Instead, treat the template essentially as a name. */
4293 hold_dpm = dpi->modifiers;
4294 dpi->modifiers = NULL;
4298 if ((options & DMGL_JAVA) != 0
4299 && dcl->type == DEMANGLE_COMPONENT_NAME
4300 && dcl->u.s_name.len == 6
4301 && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
4303 /* Special-case Java arrays, so that JArray<TYPE> appears
4304 instead as TYPE[]. */
4306 d_print_comp (dpi, options, d_right (dc));
4307 d_append_string (dpi, "[]");
4311 d_print_comp (dpi, options, dcl);
4312 if (d_last_char (dpi) == '<')
4313 d_append_char (dpi, ' ');
4314 d_append_char (dpi, '<');
4315 d_print_comp (dpi, options, d_right (dc));
4316 /* Avoid generating two consecutive '>' characters, to avoid
4317 the C++ syntactic ambiguity. */
4318 if (d_last_char (dpi) == '>')
4319 d_append_char (dpi, ' ');
4320 d_append_char (dpi, '>');
4323 dpi->modifiers = hold_dpm;
4324 dpi->current_template = hold_current;
4329 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
4331 struct d_print_template *hold_dpt;
4332 struct demangle_component *a = d_lookup_template_argument (dpi, dc);
4334 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4335 a = d_index_template_argument (a, dpi->pack_index);
4339 d_print_error (dpi);
4343 /* While processing this parameter, we need to pop the list of
4344 templates. This is because the template parameter may
4345 itself be a reference to a parameter of an outer
4348 hold_dpt = dpi->templates;
4349 dpi->templates = hold_dpt->next;
4351 d_print_comp (dpi, options, a);
4353 dpi->templates = hold_dpt;
4358 case DEMANGLE_COMPONENT_CTOR:
4359 d_print_comp (dpi, options, dc->u.s_ctor.name);
4362 case DEMANGLE_COMPONENT_DTOR:
4363 d_append_char (dpi, '~');
4364 d_print_comp (dpi, options, dc->u.s_dtor.name);
4367 case DEMANGLE_COMPONENT_VTABLE:
4368 d_append_string (dpi, "vtable for ");
4369 d_print_comp (dpi, options, d_left (dc));
4372 case DEMANGLE_COMPONENT_VTT:
4373 d_append_string (dpi, "VTT for ");
4374 d_print_comp (dpi, options, d_left (dc));
4377 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
4378 d_append_string (dpi, "construction vtable for ");
4379 d_print_comp (dpi, options, d_left (dc));
4380 d_append_string (dpi, "-in-");
4381 d_print_comp (dpi, options, d_right (dc));
4384 case DEMANGLE_COMPONENT_TYPEINFO:
4385 d_append_string (dpi, "typeinfo for ");
4386 d_print_comp (dpi, options, d_left (dc));
4389 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
4390 d_append_string (dpi, "typeinfo name for ");
4391 d_print_comp (dpi, options, d_left (dc));
4394 case DEMANGLE_COMPONENT_TYPEINFO_FN:
4395 d_append_string (dpi, "typeinfo fn for ");
4396 d_print_comp (dpi, options, d_left (dc));
4399 case DEMANGLE_COMPONENT_THUNK:
4400 d_append_string (dpi, "non-virtual thunk to ");
4401 d_print_comp (dpi, options, d_left (dc));
4404 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
4405 d_append_string (dpi, "virtual thunk to ");
4406 d_print_comp (dpi, options, d_left (dc));
4409 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
4410 d_append_string (dpi, "covariant return thunk to ");
4411 d_print_comp (dpi, options, d_left (dc));
4414 case DEMANGLE_COMPONENT_JAVA_CLASS:
4415 d_append_string (dpi, "java Class for ");
4416 d_print_comp (dpi, options, d_left (dc));
4419 case DEMANGLE_COMPONENT_GUARD:
4420 d_append_string (dpi, "guard variable for ");
4421 d_print_comp (dpi, options, d_left (dc));
4424 case DEMANGLE_COMPONENT_TLS_INIT:
4425 d_append_string (dpi, "TLS init function for ");
4426 d_print_comp (dpi, options, d_left (dc));
4429 case DEMANGLE_COMPONENT_TLS_WRAPPER:
4430 d_append_string (dpi, "TLS wrapper function for ");
4431 d_print_comp (dpi, options, d_left (dc));
4434 case DEMANGLE_COMPONENT_REFTEMP:
4435 d_append_string (dpi, "reference temporary #");
4436 d_print_comp (dpi, options, d_right (dc));
4437 d_append_string (dpi, " for ");
4438 d_print_comp (dpi, options, d_left (dc));
4441 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
4442 d_append_string (dpi, "hidden alias for ");
4443 d_print_comp (dpi, options, d_left (dc));
4446 case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
4447 d_append_string (dpi, "transaction clone for ");
4448 d_print_comp (dpi, options, d_left (dc));
4451 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
4452 d_append_string (dpi, "non-transaction clone for ");
4453 d_print_comp (dpi, options, d_left (dc));
4456 case DEMANGLE_COMPONENT_SUB_STD:
4457 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
4460 case DEMANGLE_COMPONENT_RESTRICT:
4461 case DEMANGLE_COMPONENT_VOLATILE:
4462 case DEMANGLE_COMPONENT_CONST:
4464 struct d_print_mod *pdpm;
4466 /* When printing arrays, it's possible to have cases where the
4467 same CV-qualifier gets pushed on the stack multiple times.
4468 We only need to print it once. */
4470 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
4472 if (! pdpm->printed)
4474 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
4475 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
4476 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
4478 if (pdpm->mod->type == dc->type)
4480 d_print_comp (dpi, options, d_left (dc));
4488 case DEMANGLE_COMPONENT_REFERENCE:
4489 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
4491 /* Handle reference smashing: & + && = &. */
4492 const struct demangle_component *sub = d_left (dc);
4493 if (sub->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
4495 struct demangle_component *a;
4496 struct d_saved_scope *scope = NULL, *scopes;
4499 for (i = 0; i < dpi->num_saved_scopes; i++)
4500 if (dpi->saved_scopes[i].container == sub)
4501 scope = &dpi->saved_scopes[i];
4507 /* This is the first time SUB has been traversed.
4508 We need to capture the current templates so
4509 they can be restored if SUB is reentered as a
4511 ++dpi->num_saved_scopes;
4512 size = sizeof (struct d_saved_scope) * dpi->num_saved_scopes;
4513 scopes = (struct d_saved_scope *) realloc (dpi->saved_scopes,
4517 d_print_error (dpi);
4521 dpi->saved_scopes = scopes;
4522 scope = dpi->saved_scopes + (dpi->num_saved_scopes - 1);
4524 scope->container = sub;
4525 scope->templates = d_copy_templates (dpi);
4526 if (d_print_saw_error (dpi))
4531 /* This traversal is reentering SUB as a substition.
4532 Restore the original templates temporarily. */
4533 saved_templates = dpi->templates;
4534 dpi->templates = scope->templates;
4535 need_template_restore = 1;
4538 a = d_lookup_template_argument (dpi, sub);
4539 if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
4540 a = d_index_template_argument (a, dpi->pack_index);
4544 if (need_template_restore)
4545 dpi->templates = saved_templates;
4547 d_print_error (dpi);
4554 if (sub->type == DEMANGLE_COMPONENT_REFERENCE
4555 || sub->type == dc->type)
4557 else if (sub->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE)
4558 mod_inner = d_left (sub);
4562 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4563 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4564 case DEMANGLE_COMPONENT_CONST_THIS:
4565 case DEMANGLE_COMPONENT_REFERENCE_THIS:
4566 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
4567 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
4568 case DEMANGLE_COMPONENT_POINTER:
4569 case DEMANGLE_COMPONENT_COMPLEX:
4570 case DEMANGLE_COMPONENT_IMAGINARY:
4573 /* We keep a list of modifiers on the stack. */
4574 struct d_print_mod dpm;
4576 dpm.next = dpi->modifiers;
4577 dpi->modifiers = &dpm;
4580 dpm.templates = dpi->templates;
4583 mod_inner = d_left (dc);
4585 d_print_comp (dpi, options, mod_inner);
4587 /* If the modifier didn't get printed by the type, print it
4590 d_print_mod (dpi, options, dc);
4592 dpi->modifiers = dpm.next;
4594 if (need_template_restore)
4595 dpi->templates = saved_templates;
4600 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
4601 if ((options & DMGL_JAVA) == 0)
4602 d_append_buffer (dpi, dc->u.s_builtin.type->name,
4603 dc->u.s_builtin.type->len);
4605 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
4606 dc->u.s_builtin.type->java_len);
4609 case DEMANGLE_COMPONENT_VENDOR_TYPE:
4610 d_print_comp (dpi, options, d_left (dc));
4613 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
4615 if ((options & DMGL_RET_POSTFIX) != 0)
4616 d_print_function_type (dpi,
4617 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4618 dc, dpi->modifiers);
4620 /* Print return type if present */
4621 if (d_left (dc) != NULL && (options & DMGL_RET_POSTFIX) != 0)
4622 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4624 else if (d_left (dc) != NULL && (options & DMGL_RET_DROP) == 0)
4626 struct d_print_mod dpm;
4628 /* We must pass this type down as a modifier in order to
4629 print it in the right location. */
4630 dpm.next = dpi->modifiers;
4631 dpi->modifiers = &dpm;
4634 dpm.templates = dpi->templates;
4636 d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4639 dpi->modifiers = dpm.next;
4644 /* In standard prefix notation, there is a space between the
4645 return type and the function signature. */
4646 if ((options & DMGL_RET_POSTFIX) == 0)
4647 d_append_char (dpi, ' ');
4650 if ((options & DMGL_RET_POSTFIX) == 0)
4651 d_print_function_type (dpi,
4652 options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
4653 dc, dpi->modifiers);
4658 case DEMANGLE_COMPONENT_ARRAY_TYPE:
4660 struct d_print_mod *hold_modifiers;
4661 struct d_print_mod adpm[4];
4663 struct d_print_mod *pdpm;
4665 /* We must pass this type down as a modifier in order to print
4666 multi-dimensional arrays correctly. If the array itself is
4667 CV-qualified, we act as though the element type were
4668 CV-qualified. We do this by copying the modifiers down
4669 rather than fiddling pointers, so that we don't wind up
4670 with a d_print_mod higher on the stack pointing into our
4671 stack frame after we return. */
4673 hold_modifiers = dpi->modifiers;
4675 adpm[0].next = hold_modifiers;
4676 dpi->modifiers = &adpm[0];
4678 adpm[0].printed = 0;
4679 adpm[0].templates = dpi->templates;
4682 pdpm = hold_modifiers;
4684 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
4685 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
4686 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
4688 if (! pdpm->printed)
4690 if (i >= sizeof adpm / sizeof adpm[0])
4692 d_print_error (dpi);
4697 adpm[i].next = dpi->modifiers;
4698 dpi->modifiers = &adpm[i];
4706 d_print_comp (dpi, options, d_right (dc));
4708 dpi->modifiers = hold_modifiers;
4710 if (adpm[0].printed)
4716 d_print_mod (dpi, options, adpm[i].mod);
4719 d_print_array_type (dpi, options, dc, dpi->modifiers);
4724 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
4725 case DEMANGLE_COMPONENT_VECTOR_TYPE:
4727 struct d_print_mod dpm;
4729 dpm.next = dpi->modifiers;
4730 dpi->modifiers = &dpm;
4733 dpm.templates = dpi->templates;
4735 d_print_comp (dpi, options, d_right (dc));
4737 /* If the modifier didn't get printed by the type, print it
4740 d_print_mod (dpi, options, dc);
4742 dpi->modifiers = dpm.next;
4747 case DEMANGLE_COMPONENT_FIXED_TYPE:
4748 if (dc->u.s_fixed.sat)
4749 d_append_string (dpi, "_Sat ");
4750 /* Don't print "int _Accum". */
4751 if (dc->u.s_fixed.length->u.s_builtin.type
4752 != &cplus_demangle_builtin_types['i'-'a'])
4754 d_print_comp (dpi, options, dc->u.s_fixed.length);
4755 d_append_char (dpi, ' ');
4757 if (dc->u.s_fixed.accum)
4758 d_append_string (dpi, "_Accum");
4760 d_append_string (dpi, "_Fract");
4763 case DEMANGLE_COMPONENT_ARGLIST:
4764 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
4765 if (d_left (dc) != NULL)
4766 d_print_comp (dpi, options, d_left (dc));
4767 if (d_right (dc) != NULL)
4770 unsigned long int flush_count;
4771 /* Make sure ", " isn't flushed by d_append_string, otherwise
4772 dpi->len -= 2 wouldn't work. */
4773 if (dpi->len >= sizeof (dpi->buf) - 2)
4774 d_print_flush (dpi);
4775 d_append_string (dpi, ", ");
4777 flush_count = dpi->flush_count;
4778 d_print_comp (dpi, options, d_right (dc));
4779 /* If that didn't print anything (which can happen with empty
4780 template argument packs), remove the comma and space. */
4781 if (dpi->flush_count == flush_count && dpi->len == len)
4786 case DEMANGLE_COMPONENT_INITIALIZER_LIST:
4788 struct demangle_component *type = d_left (dc);
4789 struct demangle_component *list = d_right (dc);
4792 d_print_comp (dpi, options, type);
4793 d_append_char (dpi, '{');
4794 d_print_comp (dpi, options, list);
4795 d_append_char (dpi, '}');
4799 case DEMANGLE_COMPONENT_OPERATOR:
4801 const struct demangle_operator_info *op = dc->u.s_operator.op;
4804 d_append_string (dpi, "operator");
4805 /* Add a space before new/delete. */
4806 if (IS_LOWER (op->name[0]))
4807 d_append_char (dpi, ' ');
4808 /* Omit a trailing space. */
4809 if (op->name[len-1] == ' ')
4811 d_append_buffer (dpi, op->name, len);
4815 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
4816 d_append_string (dpi, "operator ");
4817 d_print_comp (dpi, options, dc->u.s_extended_operator.name);
4820 case DEMANGLE_COMPONENT_CAST:
4821 d_append_string (dpi, "operator ");
4822 d_print_cast (dpi, options, dc);
4825 case DEMANGLE_COMPONENT_NULLARY:
4826 d_print_expr_op (dpi, options, d_left (dc));
4829 case DEMANGLE_COMPONENT_UNARY:
4831 struct demangle_component *op = d_left (dc);
4832 struct demangle_component *operand = d_right (dc);
4833 const char *code = NULL;
4835 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
4837 code = op->u.s_operator.op->code;
4838 if (!strcmp (code, "ad"))
4840 /* Don't print the argument list for the address of a
4842 if (operand->type == DEMANGLE_COMPONENT_TYPED_NAME
4843 && d_left (operand)->type == DEMANGLE_COMPONENT_QUAL_NAME
4844 && d_right (operand)->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
4845 operand = d_left (operand);
4847 if (operand->type == DEMANGLE_COMPONENT_BINARY_ARGS)
4849 /* This indicates a suffix operator. */
4850 operand = d_left (operand);
4851 d_print_subexpr (dpi, options, operand);
4852 d_print_expr_op (dpi, options, op);
4857 if (op->type != DEMANGLE_COMPONENT_CAST)
4858 d_print_expr_op (dpi, options, op);
4861 d_append_char (dpi, '(');
4862 d_print_cast (dpi, options, op);
4863 d_append_char (dpi, ')');
4865 if (code && !strcmp (code, "gs"))
4866 /* Avoid parens after '::'. */
4867 d_print_comp (dpi, options, operand);
4868 else if (code && !strcmp (code, "st"))
4869 /* Always print parens for sizeof (type). */
4871 d_append_char (dpi, '(');
4872 d_print_comp (dpi, options, operand);
4873 d_append_char (dpi, ')');
4876 d_print_subexpr (dpi, options, operand);
4880 case DEMANGLE_COMPONENT_BINARY:
4881 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
4883 d_print_error (dpi);
4887 if (op_is_new_cast (d_left (dc)))
4889 d_print_expr_op (dpi, options, d_left (dc));
4890 d_append_char (dpi, '<');
4891 d_print_comp (dpi, options, d_left (d_right (dc)));
4892 d_append_string (dpi, ">(");
4893 d_print_comp (dpi, options, d_right (d_right (dc)));
4894 d_append_char (dpi, ')');
4898 /* We wrap an expression which uses the greater-than operator in
4899 an extra layer of parens so that it does not get confused
4900 with the '>' which ends the template parameters. */
4901 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
4902 && d_left (dc)->u.s_operator.op->len == 1
4903 && d_left (dc)->u.s_operator.op->name[0] == '>')
4904 d_append_char (dpi, '(');
4906 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") == 0
4907 && d_left (d_right (dc))->type == DEMANGLE_COMPONENT_TYPED_NAME)
4909 /* Function call used in an expression should not have printed types
4910 of the function arguments. Values of the function arguments still
4911 get printed below. */
4913 const struct demangle_component *func = d_left (d_right (dc));
4915 if (d_right (func)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
4916 d_print_error (dpi);
4917 d_print_subexpr (dpi, options, d_left (func));
4920 d_print_subexpr (dpi, options, d_left (d_right (dc)));
4921 if (strcmp (d_left (dc)->u.s_operator.op->code, "ix") == 0)
4923 d_append_char (dpi, '[');
4924 d_print_comp (dpi, options, d_right (d_right (dc)));
4925 d_append_char (dpi, ']');
4929 if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") != 0)
4930 d_print_expr_op (dpi, options, d_left (dc));
4931 d_print_subexpr (dpi, options, d_right (d_right (dc)));
4934 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
4935 && d_left (dc)->u.s_operator.op->len == 1
4936 && d_left (dc)->u.s_operator.op->name[0] == '>')
4937 d_append_char (dpi, ')');
4941 case DEMANGLE_COMPONENT_BINARY_ARGS:
4942 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
4943 d_print_error (dpi);
4946 case DEMANGLE_COMPONENT_TRINARY:
4947 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
4948 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
4950 d_print_error (dpi);
4954 struct demangle_component *op = d_left (dc);
4955 struct demangle_component *first = d_left (d_right (dc));
4956 struct demangle_component *second = d_left (d_right (d_right (dc)));
4957 struct demangle_component *third = d_right (d_right (d_right (dc)));
4959 if (!strcmp (op->u.s_operator.op->code, "qu"))
4961 d_print_subexpr (dpi, options, first);
4962 d_print_expr_op (dpi, options, op);
4963 d_print_subexpr (dpi, options, second);
4964 d_append_string (dpi, " : ");
4965 d_print_subexpr (dpi, options, third);
4969 d_append_string (dpi, "new ");
4970 if (d_left (first) != NULL)
4972 d_print_subexpr (dpi, options, first);
4973 d_append_char (dpi, ' ');
4975 d_print_comp (dpi, options, second);
4977 d_print_subexpr (dpi, options, third);
4982 case DEMANGLE_COMPONENT_TRINARY_ARG1:
4983 case DEMANGLE_COMPONENT_TRINARY_ARG2:
4984 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
4985 d_print_error (dpi);
4988 case DEMANGLE_COMPONENT_LITERAL:
4989 case DEMANGLE_COMPONENT_LITERAL_NEG:
4991 enum d_builtin_type_print tp;
4993 /* For some builtin types, produce simpler output. */
4994 tp = D_PRINT_DEFAULT;
4995 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
4997 tp = d_left (dc)->u.s_builtin.type->print;
5001 case D_PRINT_UNSIGNED:
5003 case D_PRINT_UNSIGNED_LONG:
5004 case D_PRINT_LONG_LONG:
5005 case D_PRINT_UNSIGNED_LONG_LONG:
5006 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
5008 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
5009 d_append_char (dpi, '-');
5010 d_print_comp (dpi, options, d_right (dc));
5015 case D_PRINT_UNSIGNED:
5016 d_append_char (dpi, 'u');
5019 d_append_char (dpi, 'l');
5021 case D_PRINT_UNSIGNED_LONG:
5022 d_append_string (dpi, "ul");
5024 case D_PRINT_LONG_LONG:
5025 d_append_string (dpi, "ll");
5027 case D_PRINT_UNSIGNED_LONG_LONG:
5028 d_append_string (dpi, "ull");
5036 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
5037 && d_right (dc)->u.s_name.len == 1
5038 && dc->type == DEMANGLE_COMPONENT_LITERAL)
5040 switch (d_right (dc)->u.s_name.s[0])
5043 d_append_string (dpi, "false");
5046 d_append_string (dpi, "true");
5059 d_append_char (dpi, '(');
5060 d_print_comp (dpi, options, d_left (dc));
5061 d_append_char (dpi, ')');
5062 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
5063 d_append_char (dpi, '-');
5064 if (tp == D_PRINT_FLOAT)
5065 d_append_char (dpi, '[');
5066 d_print_comp (dpi, options, d_right (dc));
5067 if (tp == D_PRINT_FLOAT)
5068 d_append_char (dpi, ']');
5072 case DEMANGLE_COMPONENT_NUMBER:
5073 d_append_num (dpi, dc->u.s_number.number);
5076 case DEMANGLE_COMPONENT_JAVA_RESOURCE:
5077 d_append_string (dpi, "java resource ");
5078 d_print_comp (dpi, options, d_left (dc));
5081 case DEMANGLE_COMPONENT_COMPOUND_NAME:
5082 d_print_comp (dpi, options, d_left (dc));
5083 d_print_comp (dpi, options, d_right (dc));
5086 case DEMANGLE_COMPONENT_CHARACTER:
5087 d_append_char (dpi, dc->u.s_character.character);
5090 case DEMANGLE_COMPONENT_DECLTYPE:
5091 d_append_string (dpi, "decltype (");
5092 d_print_comp (dpi, options, d_left (dc));
5093 d_append_char (dpi, ')');
5096 case DEMANGLE_COMPONENT_PACK_EXPANSION:
5100 struct demangle_component *a = d_find_pack (dpi, d_left (dc));
5103 /* d_find_pack won't find anything if the only packs involved
5104 in this expansion are function parameter packs; in that
5105 case, just print the pattern and "...". */
5106 d_print_subexpr (dpi, options, d_left (dc));
5107 d_append_string (dpi, "...");
5111 len = d_pack_length (a);
5113 for (i = 0; i < len; ++i)
5115 dpi->pack_index = i;
5116 d_print_comp (dpi, options, dc);
5118 d_append_string (dpi, ", ");
5123 case DEMANGLE_COMPONENT_FUNCTION_PARAM:
5125 long num = dc->u.s_number.number;
5127 d_append_string (dpi, "this");
5130 d_append_string (dpi, "{parm#");
5131 d_append_num (dpi, num);
5132 d_append_char (dpi, '}');
5137 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
5138 d_append_string (dpi, "global constructors keyed to ");
5139 d_print_comp (dpi, options, dc->u.s_binary.left);
5142 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
5143 d_append_string (dpi, "global destructors keyed to ");
5144 d_print_comp (dpi, options, dc->u.s_binary.left);
5147 case DEMANGLE_COMPONENT_LAMBDA:
5148 d_append_string (dpi, "{lambda(");
5149 d_print_comp (dpi, options, dc->u.s_unary_num.sub);
5150 d_append_string (dpi, ")#");
5151 d_append_num (dpi, dc->u.s_unary_num.num + 1);
5152 d_append_char (dpi, '}');
5155 case DEMANGLE_COMPONENT_UNNAMED_TYPE:
5156 d_append_string (dpi, "{unnamed type#");
5157 d_append_num (dpi, dc->u.s_number.number + 1);
5158 d_append_char (dpi, '}');
5161 case DEMANGLE_COMPONENT_CLONE:
5162 d_print_comp (dpi, options, d_left (dc));
5163 d_append_string (dpi, " [clone ");
5164 d_print_comp (dpi, options, d_right (dc));
5165 d_append_char (dpi, ']');
5169 d_print_error (dpi);
5174 /* Print a Java dentifier. For Java we try to handle encoded extended
5175 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
5176 so we don't it for C++. Characters are encoded as
5180 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
5186 for (p = name; p < end; ++p)
5197 for (q = p + 3; q < end; ++q)
5203 else if (*q >= 'A' && *q <= 'F')
5204 dig = *q - 'A' + 10;
5205 else if (*q >= 'a' && *q <= 'f')
5206 dig = *q - 'a' + 10;
5212 /* If the Unicode character is larger than 256, we don't try
5213 to deal with it here. FIXME. */
5214 if (q < end && *q == '_' && c < 256)
5216 d_append_char (dpi, c);
5222 d_append_char (dpi, *p);
5226 /* Print a list of modifiers. SUFFIX is 1 if we are printing
5227 qualifiers on this after printing a function. */
5230 d_print_mod_list (struct d_print_info *dpi, int options,
5231 struct d_print_mod *mods, int suffix)
5233 struct d_print_template *hold_dpt;
5235 if (mods == NULL || d_print_saw_error (dpi))
5240 && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
5241 || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
5242 || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS
5243 || mods->mod->type == DEMANGLE_COMPONENT_REFERENCE_THIS
5245 == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS))))
5247 d_print_mod_list (dpi, options, mods->next, suffix);
5253 hold_dpt = dpi->templates;
5254 dpi->templates = mods->templates;
5256 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
5258 d_print_function_type (dpi, options, mods->mod, mods->next);
5259 dpi->templates = hold_dpt;
5262 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
5264 d_print_array_type (dpi, options, mods->mod, mods->next);
5265 dpi->templates = hold_dpt;
5268 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
5270 struct d_print_mod *hold_modifiers;
5271 struct demangle_component *dc;
5273 /* When this is on the modifier stack, we have pulled any
5274 qualifiers off the right argument already. Otherwise, we
5275 print it as usual, but don't let the left argument see any
5278 hold_modifiers = dpi->modifiers;
5279 dpi->modifiers = NULL;
5280 d_print_comp (dpi, options, d_left (mods->mod));
5281 dpi->modifiers = hold_modifiers;
5283 if ((options & DMGL_JAVA) == 0)
5284 d_append_string (dpi, "::");
5286 d_append_char (dpi, '.');
5288 dc = d_right (mods->mod);
5290 if (dc->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
5292 d_append_string (dpi, "{default arg#");
5293 d_append_num (dpi, dc->u.s_unary_num.num + 1);
5294 d_append_string (dpi, "}::");
5295 dc = dc->u.s_unary_num.sub;
5298 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
5299 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
5300 || dc->type == DEMANGLE_COMPONENT_CONST_THIS
5301 || dc->type == DEMANGLE_COMPONENT_REFERENCE_THIS
5302 || dc->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS)
5305 d_print_comp (dpi, options, dc);
5307 dpi->templates = hold_dpt;
5311 d_print_mod (dpi, options, mods->mod);
5313 dpi->templates = hold_dpt;
5315 d_print_mod_list (dpi, options, mods->next, suffix);
5318 /* Print a modifier. */
5321 d_print_mod (struct d_print_info *dpi, int options,
5322 const struct demangle_component *mod)
5326 case DEMANGLE_COMPONENT_RESTRICT:
5327 case DEMANGLE_COMPONENT_RESTRICT_THIS:
5328 d_append_string (dpi, " restrict");
5330 case DEMANGLE_COMPONENT_VOLATILE:
5331 case DEMANGLE_COMPONENT_VOLATILE_THIS:
5332 d_append_string (dpi, " volatile");
5334 case DEMANGLE_COMPONENT_CONST:
5335 case DEMANGLE_COMPONENT_CONST_THIS:
5336 d_append_string (dpi, " const");
5338 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5339 d_append_char (dpi, ' ');
5340 d_print_comp (dpi, options, d_right (mod));
5342 case DEMANGLE_COMPONENT_POINTER:
5343 /* There is no pointer symbol in Java. */
5344 if ((options & DMGL_JAVA) == 0)
5345 d_append_char (dpi, '*');
5347 case DEMANGLE_COMPONENT_REFERENCE_THIS:
5348 /* For the ref-qualifier, put a space before the &. */
5349 d_append_char (dpi, ' ');
5350 case DEMANGLE_COMPONENT_REFERENCE:
5351 d_append_char (dpi, '&');
5353 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
5354 d_append_char (dpi, ' ');
5355 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
5356 d_append_string (dpi, "&&");
5358 case DEMANGLE_COMPONENT_COMPLEX:
5359 d_append_string (dpi, "complex ");
5361 case DEMANGLE_COMPONENT_IMAGINARY:
5362 d_append_string (dpi, "imaginary ");
5364 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5365 if (d_last_char (dpi) != '(')
5366 d_append_char (dpi, ' ');
5367 d_print_comp (dpi, options, d_left (mod));
5368 d_append_string (dpi, "::*");
5370 case DEMANGLE_COMPONENT_TYPED_NAME:
5371 d_print_comp (dpi, options, d_left (mod));
5373 case DEMANGLE_COMPONENT_VECTOR_TYPE:
5374 d_append_string (dpi, " __vector(");
5375 d_print_comp (dpi, options, d_left (mod));
5376 d_append_char (dpi, ')');
5380 /* Otherwise, we have something that won't go back on the
5381 modifier stack, so we can just print it. */
5382 d_print_comp (dpi, options, mod);
5387 /* Print a function type, except for the return type. */
5390 d_print_function_type (struct d_print_info *dpi, int options,
5391 const struct demangle_component *dc,
5392 struct d_print_mod *mods)
5396 struct d_print_mod *p;
5397 struct d_print_mod *hold_modifiers;
5401 for (p = mods; p != NULL; p = p->next)
5406 switch (p->mod->type)
5408 case DEMANGLE_COMPONENT_POINTER:
5409 case DEMANGLE_COMPONENT_REFERENCE:
5410 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
5413 case DEMANGLE_COMPONENT_RESTRICT:
5414 case DEMANGLE_COMPONENT_VOLATILE:
5415 case DEMANGLE_COMPONENT_CONST:
5416 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5417 case DEMANGLE_COMPONENT_COMPLEX:
5418 case DEMANGLE_COMPONENT_IMAGINARY:
5419 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5423 case DEMANGLE_COMPONENT_RESTRICT_THIS:
5424 case DEMANGLE_COMPONENT_VOLATILE_THIS:
5425 case DEMANGLE_COMPONENT_CONST_THIS:
5426 case DEMANGLE_COMPONENT_REFERENCE_THIS:
5427 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
5440 if (d_last_char (dpi) != '('
5441 && d_last_char (dpi) != '*')
5444 if (need_space && d_last_char (dpi) != ' ')
5445 d_append_char (dpi, ' ');
5446 d_append_char (dpi, '(');
5449 hold_modifiers = dpi->modifiers;
5450 dpi->modifiers = NULL;
5452 d_print_mod_list (dpi, options, mods, 0);
5455 d_append_char (dpi, ')');
5457 d_append_char (dpi, '(');
5459 if (d_right (dc) != NULL)
5460 d_print_comp (dpi, options, d_right (dc));
5462 d_append_char (dpi, ')');
5464 d_print_mod_list (dpi, options, mods, 1);
5466 dpi->modifiers = hold_modifiers;
5469 /* Print an array type, except for the element type. */
5472 d_print_array_type (struct d_print_info *dpi, int options,
5473 const struct demangle_component *dc,
5474 struct d_print_mod *mods)
5482 struct d_print_mod *p;
5485 for (p = mods; p != NULL; p = p->next)
5489 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
5504 d_append_string (dpi, " (");
5506 d_print_mod_list (dpi, options, mods, 0);
5509 d_append_char (dpi, ')');
5513 d_append_char (dpi, ' ');
5515 d_append_char (dpi, '[');
5517 if (d_left (dc) != NULL)
5518 d_print_comp (dpi, options, d_left (dc));
5520 d_append_char (dpi, ']');
5523 /* Print an operator in an expression. */
5526 d_print_expr_op (struct d_print_info *dpi, int options,
5527 const struct demangle_component *dc)
5529 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
5530 d_append_buffer (dpi, dc->u.s_operator.op->name,
5531 dc->u.s_operator.op->len);
5533 d_print_comp (dpi, options, dc);
5539 d_print_cast (struct d_print_info *dpi, int options,
5540 const struct demangle_component *dc)
5542 struct d_print_template dpt;
5544 /* For a cast operator, we need the template parameters from
5545 the enclosing template in scope for processing the type. */
5546 if (dpi->current_template != NULL)
5548 dpt.next = dpi->templates;
5549 dpi->templates = &dpt;
5550 dpt.template_decl = dpi->current_template;
5553 if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
5555 d_print_comp (dpi, options, d_left (dc));
5556 if (dpi->current_template != NULL)
5557 dpi->templates = dpt.next;
5561 d_print_comp (dpi, options, d_left (d_left (dc)));
5563 /* For a templated cast operator, we need to remove the template
5564 parameters from scope after printing the operator name,
5565 so we need to handle the template printing here. */
5566 if (dpi->current_template != NULL)
5567 dpi->templates = dpt.next;
5569 if (d_last_char (dpi) == '<')
5570 d_append_char (dpi, ' ');
5571 d_append_char (dpi, '<');
5572 d_print_comp (dpi, options, d_right (d_left (dc)));
5573 /* Avoid generating two consecutive '>' characters, to avoid
5574 the C++ syntactic ambiguity. */
5575 if (d_last_char (dpi) == '>')
5576 d_append_char (dpi, ' ');
5577 d_append_char (dpi, '>');
5581 /* Initialize the information structure we use to pass around
5584 CP_STATIC_IF_GLIBCPP_V3
5586 cplus_demangle_init_info (const char *mangled, int options, size_t len,
5590 di->send = mangled + len;
5591 di->options = options;
5595 /* We can not need more components than twice the number of chars in
5596 the mangled string. Most components correspond directly to
5597 chars, but the ARGLIST types are exceptions. */
5598 di->num_comps = 2 * len;
5601 /* Similarly, we can not need more substitutions than there are
5602 chars in the mangled string. */
5607 di->last_name = NULL;
5610 di->is_expression = 0;
5611 di->is_conversion = 0;
5614 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
5615 mangled name, return strings in repeated callback giving the demangled
5616 name. OPTIONS is the usual libiberty demangler options. On success,
5617 this returns 1. On failure, returns 0. */
5620 d_demangle_callback (const char *mangled, int options,
5621 demangle_callbackref callback, void *opaque)
5632 struct demangle_component *dc;
5635 if (mangled[0] == '_' && mangled[1] == 'Z')
5637 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
5638 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
5639 && (mangled[9] == 'D' || mangled[9] == 'I')
5640 && mangled[10] == '_')
5641 type = mangled[9] == 'I' ? DCT_GLOBAL_CTORS : DCT_GLOBAL_DTORS;
5644 if ((options & DMGL_TYPES) == 0)
5649 cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
5652 #ifdef CP_DYNAMIC_ARRAYS
5653 __extension__ struct demangle_component comps[di.num_comps];
5654 __extension__ struct demangle_component *subs[di.num_subs];
5659 di.comps = alloca (di.num_comps * sizeof (*di.comps));
5660 di.subs = alloca (di.num_subs * sizeof (*di.subs));
5666 dc = cplus_demangle_type (&di);
5669 dc = cplus_demangle_mangled_name (&di, 1);
5671 case DCT_GLOBAL_CTORS:
5672 case DCT_GLOBAL_DTORS:
5673 d_advance (&di, 11);
5674 dc = d_make_comp (&di,
5675 (type == DCT_GLOBAL_CTORS
5676 ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
5677 : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS),
5678 d_make_demangle_mangled_name (&di, d_str (&di)),
5680 d_advance (&di, strlen (d_str (&di)));
5684 /* If DMGL_PARAMS is set, then if we didn't consume the entire
5685 mangled string, then we didn't successfully demangle it. If
5686 DMGL_PARAMS is not set, we didn't look at the trailing
5688 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
5691 #ifdef CP_DEMANGLE_DEBUG
5695 status = (dc != NULL)
5696 ? cplus_demangle_print_callback (options, dc, callback, opaque)
5703 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
5704 name, return a buffer allocated with malloc holding the demangled
5705 name. OPTIONS is the usual libiberty demangler options. On
5706 success, this sets *PALC to the allocated size of the returned
5707 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
5708 a memory allocation failure, and returns NULL. */
5711 d_demangle (const char *mangled, int options, size_t *palc)
5713 struct d_growable_string dgs;
5716 d_growable_string_init (&dgs, 0);
5718 status = d_demangle_callback (mangled, options,
5719 d_growable_string_callback_adapter, &dgs);
5727 *palc = dgs.allocation_failure ? 1 : dgs.alc;
5731 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
5733 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
5735 /* ia64 ABI-mandated entry point in the C++ runtime library for
5736 performing demangling. MANGLED_NAME is a NUL-terminated character
5737 string containing the name to be demangled.
5739 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
5740 *LENGTH bytes, into which the demangled name is stored. If
5741 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
5742 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
5743 is placed in a region of memory allocated with malloc.
5745 If LENGTH is non-NULL, the length of the buffer containing the
5746 demangled name, is placed in *LENGTH.
5748 The return value is a pointer to the start of the NUL-terminated
5749 demangled name, or NULL if the demangling fails. The caller is
5750 responsible for deallocating this memory using free.
5752 *STATUS is set to one of the following values:
5753 0: The demangling operation succeeded.
5754 -1: A memory allocation failure occurred.
5755 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
5756 -3: One of the arguments is invalid.
5758 The demangling is performed using the C++ ABI mangling rules, with
5762 __cxa_demangle (const char *mangled_name, char *output_buffer,
5763 size_t *length, int *status)
5768 if (mangled_name == NULL)
5775 if (output_buffer != NULL && length == NULL)
5782 demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
5784 if (demangled == NULL)
5796 if (output_buffer == NULL)
5803 if (strlen (demangled) < *length)
5805 strcpy (output_buffer, demangled);
5807 demangled = output_buffer;
5811 free (output_buffer);
5822 extern int __gcclibcxx_demangle_callback (const char *,
5824 (const char *, size_t, void *),
5827 /* Alternative, allocationless entry point in the C++ runtime library
5828 for performing demangling. MANGLED_NAME is a NUL-terminated character
5829 string containing the name to be demangled.
5831 CALLBACK is a callback function, called with demangled string
5832 segments as demangling progresses; it is called at least once,
5833 but may be called more than once. OPAQUE is a generalized pointer
5834 used as a callback argument.
5836 The return code is one of the following values, equivalent to
5837 the STATUS values of __cxa_demangle() (excluding -1, since this
5838 function performs no memory allocations):
5839 0: The demangling operation succeeded.
5840 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
5841 -3: One of the arguments is invalid.
5843 The demangling is performed using the C++ ABI mangling rules, with
5847 __gcclibcxx_demangle_callback (const char *mangled_name,
5848 void (*callback) (const char *, size_t, void *),
5853 if (mangled_name == NULL || callback == NULL)
5856 status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES,
5864 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
5866 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
5867 mangled name, return a buffer allocated with malloc holding the
5868 demangled name. Otherwise, return NULL. */
5871 cplus_demangle_v3 (const char *mangled, int options)
5875 return d_demangle (mangled, options, &alc);
5879 cplus_demangle_v3_callback (const char *mangled, int options,
5880 demangle_callbackref callback, void *opaque)
5882 return d_demangle_callback (mangled, options, callback, opaque);
5885 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
5886 conventions, but the output formatting is a little different.
5887 This instructs the C++ demangler not to emit pointer characters ("*"), to
5888 use Java's namespace separator symbol ("." instead of "::"), and to output
5889 JArray<TYPE> as TYPE[]. */
5892 java_demangle_v3 (const char *mangled)
5896 return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
5900 java_demangle_v3_callback (const char *mangled,
5901 demangle_callbackref callback, void *opaque)
5903 return d_demangle_callback (mangled,
5904 DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
5908 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
5910 #ifndef IN_GLIBCPP_V3
5912 /* Demangle a string in order to find out whether it is a constructor
5913 or destructor. Return non-zero on success. Set *CTOR_KIND and
5914 *DTOR_KIND appropriately. */
5917 is_ctor_or_dtor (const char *mangled,
5918 enum gnu_v3_ctor_kinds *ctor_kind,
5919 enum gnu_v3_dtor_kinds *dtor_kind)
5922 struct demangle_component *dc;
5925 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
5926 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
5928 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
5931 #ifdef CP_DYNAMIC_ARRAYS
5932 __extension__ struct demangle_component comps[di.num_comps];
5933 __extension__ struct demangle_component *subs[di.num_subs];
5938 di.comps = alloca (di.num_comps * sizeof (*di.comps));
5939 di.subs = alloca (di.num_subs * sizeof (*di.subs));
5942 dc = cplus_demangle_mangled_name (&di, 1);
5944 /* Note that because we did not pass DMGL_PARAMS, we don't expect
5945 to demangle the entire string. */
5952 /* These cannot appear on a constructor or destructor. */
5953 case DEMANGLE_COMPONENT_RESTRICT_THIS:
5954 case DEMANGLE_COMPONENT_VOLATILE_THIS:
5955 case DEMANGLE_COMPONENT_CONST_THIS:
5956 case DEMANGLE_COMPONENT_REFERENCE_THIS:
5957 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
5961 case DEMANGLE_COMPONENT_TYPED_NAME:
5962 case DEMANGLE_COMPONENT_TEMPLATE:
5965 case DEMANGLE_COMPONENT_QUAL_NAME:
5966 case DEMANGLE_COMPONENT_LOCAL_NAME:
5969 case DEMANGLE_COMPONENT_CTOR:
5970 *ctor_kind = dc->u.s_ctor.kind;
5974 case DEMANGLE_COMPONENT_DTOR:
5975 *dtor_kind = dc->u.s_dtor.kind;
5986 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
5987 name. A non-zero return indicates the type of constructor. */
5989 enum gnu_v3_ctor_kinds
5990 is_gnu_v3_mangled_ctor (const char *name)
5992 enum gnu_v3_ctor_kinds ctor_kind;
5993 enum gnu_v3_dtor_kinds dtor_kind;
5995 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
5996 return (enum gnu_v3_ctor_kinds) 0;
6001 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
6002 name. A non-zero return indicates the type of destructor. */
6004 enum gnu_v3_dtor_kinds
6005 is_gnu_v3_mangled_dtor (const char *name)
6007 enum gnu_v3_ctor_kinds ctor_kind;
6008 enum gnu_v3_dtor_kinds dtor_kind;
6010 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
6011 return (enum gnu_v3_dtor_kinds) 0;
6015 #endif /* IN_GLIBCPP_V3 */
6017 #ifdef STANDALONE_DEMANGLER
6020 #include "dyn-string.h"
6022 static void print_usage (FILE* fp, int exit_value);
6024 #define IS_ALPHA(CHAR) \
6025 (((CHAR) >= 'a' && (CHAR) <= 'z') \
6026 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
6028 /* Non-zero if CHAR is a character than can occur in a mangled name. */
6029 #define is_mangled_char(CHAR) \
6030 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
6031 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
6033 /* The name of this program, as invoked. */
6034 const char* program_name;
6036 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
6039 print_usage (FILE* fp, int exit_value)
6041 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
6042 fprintf (fp, "Options:\n");
6043 fprintf (fp, " -h,--help Display this message.\n");
6044 fprintf (fp, " -p,--no-params Don't display function parameters\n");
6045 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
6046 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
6051 /* Option specification for getopt_long. */
6052 static const struct option long_options[] =
6054 { "help", no_argument, NULL, 'h' },
6055 { "no-params", no_argument, NULL, 'p' },
6056 { "verbose", no_argument, NULL, 'v' },
6057 { NULL, no_argument, NULL, 0 },
6060 /* Main entry for a demangling filter executable. It will demangle
6061 its command line arguments, if any. If none are provided, it will
6062 filter stdin to stdout, replacing any recognized mangled C++ names
6063 with their demangled equivalents. */
6066 main (int argc, char *argv[])
6070 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
6072 /* Use the program name of this program, as invoked. */
6073 program_name = argv[0];
6075 /* Parse options. */
6078 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
6081 case '?': /* Unrecognized option. */
6082 print_usage (stderr, 1);
6086 print_usage (stdout, 0);
6090 options &= ~ DMGL_PARAMS;
6094 options |= DMGL_VERBOSE;
6098 while (opt_char != -1);
6101 /* No command line arguments were provided. Filter stdin. */
6103 dyn_string_t mangled = dyn_string_new (3);
6106 /* Read all of input. */
6107 while (!feof (stdin))
6111 /* Pile characters into mangled until we hit one that can't
6112 occur in a mangled name. */
6114 while (!feof (stdin) && is_mangled_char (c))
6116 dyn_string_append_char (mangled, c);
6122 if (dyn_string_length (mangled) > 0)
6124 #ifdef IN_GLIBCPP_V3
6125 s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
6127 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
6137 /* It might not have been a mangled name. Print the
6139 fputs (dyn_string_buf (mangled), stdout);
6142 dyn_string_clear (mangled);
6145 /* If we haven't hit EOF yet, we've read one character that
6146 can't occur in a mangled name, so print it out. */
6151 dyn_string_delete (mangled);
6154 /* Demangle command line arguments. */
6156 /* Loop over command line arguments. */
6157 for (i = optind; i < argc; ++i)
6160 #ifdef IN_GLIBCPP_V3
6164 /* Attempt to demangle. */
6165 #ifdef IN_GLIBCPP_V3
6166 s = __cxa_demangle (argv[i], NULL, NULL, &status);
6168 s = cplus_demangle_v3 (argv[i], options);
6171 /* If it worked, print the demangled name. */
6179 #ifdef IN_GLIBCPP_V3
6180 fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
6182 fprintf (stderr, "Failed: %s\n", argv[i]);
6191 #endif /* STANDALONE_DEMANGLER */