1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2014
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor <ian@wasabisystems.com>.
6 This file is part of the libiberty library, which is part of GCC.
8 This file is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 In addition to the permissions in the GNU General Public License, the
14 Free Software Foundation gives you unlimited permission to link the
15 compiled version of this file into combinations with other programs,
16 and to distribute those combinations without any restriction coming
17 from the use of this file. (The General Public License restrictions
18 do apply in other respects; for example, they cover modification of
19 the file, and distribution when not linked into a combined
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
32 /* This code implements a demangler for the g++ V3 ABI. The ABI is
33 described on this web page:
34 http://www.codesourcery.com/cxx-abi/abi.html#mangling
36 This code was written while looking at the demangler written by
37 Alex Samuel <samuel@codesourcery.com>.
39 This code first pulls the mangled name apart into a list of
40 components, and then walks the list generating the demangled
43 This file will normally define the following functions, q.v.:
44 char *cplus_demangle_v3(const char *mangled, int options)
45 char *java_demangle_v3(const char *mangled)
46 int cplus_demangle_v3_callback(const char *mangled, int options,
47 demangle_callbackref callback)
48 int java_demangle_v3_callback(const char *mangled,
49 demangle_callbackref callback)
50 enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
51 enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
53 Also, the interface to the component list is public, and defined in
54 demangle.h. The interface consists of these types, which are
55 defined in demangle.h:
56 enum demangle_component_type
57 struct demangle_component
59 and these functions defined in this file:
60 cplus_demangle_fill_name
61 cplus_demangle_fill_extended_operator
62 cplus_demangle_fill_ctor
63 cplus_demangle_fill_dtor
65 cplus_demangle_print_callback
66 and other functions defined in the file cp-demint.c.
68 This file also defines some other functions and variables which are
69 only to be used by the file cp-demint.c.
71 Preprocessor macros you can define while compiling this file:
74 If defined, this file defines the following functions, q.v.:
75 char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
77 int __gcclibcxx_demangle_callback (const char *,
79 (const char *, size_t, void *),
81 instead of cplus_demangle_v3[_callback]() and
82 java_demangle_v3[_callback]().
85 If defined, this file defines only __cxa_demangle() and
86 __gcclibcxx_demangle_callback(), and no other publically visible
87 functions or variables.
90 If defined, this file defines a main() function which demangles
91 any arguments, or, if none, demangles stdin.
94 If defined, turns on debugging mode, which prints information on
95 stdout about the mangled string. This is not generally useful.
98 If defined, additional sanity checks will be performed. It will
99 cause some slowdown, but will allow to catch out-of-bound access
100 errors earlier. This macro is intended for testing and debugging. */
102 #if defined (_AIX) && !defined (__GNUC__)
124 # define alloca __builtin_alloca
126 extern char *alloca ();
127 # endif /* __GNUC__ */
129 #endif /* HAVE_ALLOCA_H */
131 #include "ansidecl.h"
132 #include "libiberty.h"
133 #include "demangle.h"
134 #include "cp-demangle.h"
136 /* If IN_GLIBCPP_V3 is defined, some functions are made static. We
137 also rename them via #define to avoid compiler errors when the
138 static definition conflicts with the extern declaration in a header
142 #define CP_STATIC_IF_GLIBCPP_V3 static
144 #define cplus_demangle_fill_name d_fill_name
145 static int d_fill_name (struct demangle_component
*, const char *, int);
147 #define cplus_demangle_fill_extended_operator d_fill_extended_operator
149 d_fill_extended_operator (struct demangle_component
*, int,
150 struct demangle_component
*);
152 #define cplus_demangle_fill_ctor d_fill_ctor
154 d_fill_ctor (struct demangle_component
*, enum gnu_v3_ctor_kinds
,
155 struct demangle_component
*);
157 #define cplus_demangle_fill_dtor d_fill_dtor
159 d_fill_dtor (struct demangle_component
*, enum gnu_v3_dtor_kinds
,
160 struct demangle_component
*);
162 #define cplus_demangle_mangled_name d_mangled_name
163 static struct demangle_component
*d_mangled_name (struct d_info
*, int);
165 #define cplus_demangle_type d_type
166 static struct demangle_component
*d_type (struct d_info
*);
168 #define cplus_demangle_print d_print
169 static char *d_print (int, const struct demangle_component
*, int, size_t *);
171 #define cplus_demangle_print_callback d_print_callback
172 static int d_print_callback (int, const struct demangle_component
*,
173 demangle_callbackref
, void *);
175 #define cplus_demangle_init_info d_init_info
176 static void d_init_info (const char *, int, size_t, struct d_info
*);
178 #else /* ! defined(IN_GLIBCPP_V3) */
179 #define CP_STATIC_IF_GLIBCPP_V3
180 #endif /* ! defined(IN_GLIBCPP_V3) */
182 /* See if the compiler supports dynamic arrays. */
185 #define CP_DYNAMIC_ARRAYS
188 #ifdef __STDC_VERSION__
189 #if __STDC_VERSION__ >= 199901L
190 #define CP_DYNAMIC_ARRAYS
191 #endif /* __STDC__VERSION >= 199901L */
192 #endif /* defined (__STDC_VERSION__) */
193 #endif /* defined (__STDC__) */
194 #endif /* ! defined (__GNUC__) */
196 /* We avoid pulling in the ctype tables, to prevent pulling in
197 additional unresolved symbols when this code is used in a library.
198 FIXME: Is this really a valid reason? This comes from the original
201 As of this writing this file has the following undefined references
202 when compiled with -DIN_GLIBCPP_V3: realloc, free, memcpy, strcpy,
205 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
206 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
207 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
209 /* The prefix prepended by GCC to an identifier represnting the
210 anonymous namespace. */
211 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
212 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
213 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
215 /* Information we keep for the standard substitutions. */
217 struct d_standard_sub_info
219 /* The code for this substitution. */
221 /* The simple string it expands to. */
222 const char *simple_expansion
;
223 /* The length of the simple expansion. */
225 /* The results of a full, verbose, expansion. This is used when
226 qualifying a constructor/destructor, or when in verbose mode. */
227 const char *full_expansion
;
228 /* The length of the full expansion. */
230 /* What to set the last_name field of d_info to; NULL if we should
231 not set it. This is only relevant when qualifying a
232 constructor/destructor. */
233 const char *set_last_name
;
234 /* The length of set_last_name. */
235 int set_last_name_len
;
238 /* Accessors for subtrees of struct demangle_component. */
240 #define d_left(dc) ((dc)->u.s_binary.left)
241 #define d_right(dc) ((dc)->u.s_binary.right)
243 /* A list of templates. This is used while printing. */
245 struct d_print_template
247 /* Next template on the list. */
248 struct d_print_template
*next
;
250 const struct demangle_component
*template_decl
;
253 /* A list of type modifiers. This is used while printing. */
257 /* Next modifier on the list. These are in the reverse of the order
258 in which they appeared in the mangled string. */
259 struct d_print_mod
*next
;
261 const struct demangle_component
*mod
;
262 /* Whether this modifier was printed. */
264 /* The list of templates which applies to this modifier. */
265 struct d_print_template
*templates
;
268 /* We use these structures to hold information during printing. */
270 struct d_growable_string
272 /* Buffer holding the result. */
274 /* Current length of data in buffer. */
276 /* Allocated size of buffer. */
278 /* Set to 1 if we had a memory allocation failure. */
279 int allocation_failure
;
282 /* Stack of components, innermost first, used to avoid loops. */
284 struct d_component_stack
286 /* This component. */
287 const struct demangle_component
*dc
;
288 /* This component's parent. */
289 const struct d_component_stack
*parent
;
292 /* A demangle component and some scope captured when it was first
297 /* The component whose scope this is. */
298 const struct demangle_component
*container
;
299 /* The list of templates, if any, that was current when this
300 scope was captured. */
301 struct d_print_template
*templates
;
304 /* Checkpoint structure to allow backtracking. This holds copies
305 of the fields of struct d_info that need to be restored
306 if a trial parse needs to be backtracked over. */
308 struct d_info_checkpoint
317 enum { D_PRINT_BUFFER_LENGTH
= 256 };
320 /* Fixed-length allocated buffer for demangled data, flushed to the
321 callback with a NUL termination once full. */
322 char buf
[D_PRINT_BUFFER_LENGTH
];
323 /* Current length of data in buffer. */
325 /* The last character printed, saved individually so that it survives
328 /* Callback function to handle demangled buffer flush. */
329 demangle_callbackref callback
;
330 /* Opaque callback argument. */
332 /* The current list of templates, if any. */
333 struct d_print_template
*templates
;
334 /* The current list of modifiers (e.g., pointer, reference, etc.),
336 struct d_print_mod
*modifiers
;
337 /* Set to 1 if we saw a demangling error. */
338 int demangle_failure
;
339 /* The current index into any template argument packs we are using
342 /* Number of d_print_flush calls so far. */
343 unsigned long int flush_count
;
344 /* Stack of components, innermost first, used to avoid loops. */
345 const struct d_component_stack
*component_stack
;
346 /* Array of saved scopes for evaluating substitutions. */
347 struct d_saved_scope
*saved_scopes
;
348 /* Index of the next unused saved scope in the above array. */
349 int next_saved_scope
;
350 /* Number of saved scopes in the above array. */
351 int num_saved_scopes
;
352 /* Array of templates for saving into scopes. */
353 struct d_print_template
*copy_templates
;
354 /* Index of the next unused copy template in the above array. */
355 int next_copy_template
;
356 /* Number of copy templates in the above array. */
357 int num_copy_templates
;
358 /* The nearest enclosing template, if any. */
359 const struct demangle_component
*current_template
;
362 #ifdef CP_DEMANGLE_DEBUG
363 static void d_dump (struct demangle_component
*, int);
366 static struct demangle_component
*
367 d_make_empty (struct d_info
*);
369 static struct demangle_component
*
370 d_make_comp (struct d_info
*, enum demangle_component_type
,
371 struct demangle_component
*,
372 struct demangle_component
*);
374 static struct demangle_component
*
375 d_make_name (struct d_info
*, const char *, int);
377 static struct demangle_component
*
378 d_make_demangle_mangled_name (struct d_info
*, const char *);
380 static struct demangle_component
*
381 d_make_builtin_type (struct d_info
*,
382 const struct demangle_builtin_type_info
*);
384 static struct demangle_component
*
385 d_make_operator (struct d_info
*,
386 const struct demangle_operator_info
*);
388 static struct demangle_component
*
389 d_make_extended_operator (struct d_info
*, int,
390 struct demangle_component
*);
392 static struct demangle_component
*
393 d_make_ctor (struct d_info
*, enum gnu_v3_ctor_kinds
,
394 struct demangle_component
*);
396 static struct demangle_component
*
397 d_make_dtor (struct d_info
*, enum gnu_v3_dtor_kinds
,
398 struct demangle_component
*);
400 static struct demangle_component
*
401 d_make_template_param (struct d_info
*, long);
403 static struct demangle_component
*
404 d_make_sub (struct d_info
*, const char *, int);
407 has_return_type (struct demangle_component
*);
410 is_ctor_dtor_or_conversion (struct demangle_component
*);
412 static struct demangle_component
*d_encoding (struct d_info
*, int);
414 static struct demangle_component
*d_name (struct d_info
*);
416 static struct demangle_component
*d_nested_name (struct d_info
*);
418 static struct demangle_component
*d_prefix (struct d_info
*);
420 static struct demangle_component
*d_unqualified_name (struct d_info
*);
422 static struct demangle_component
*d_source_name (struct d_info
*);
424 static long d_number (struct d_info
*);
426 static struct demangle_component
*d_identifier (struct d_info
*, long);
428 static struct demangle_component
*d_operator_name (struct d_info
*);
430 static struct demangle_component
*d_special_name (struct d_info
*);
432 static int d_call_offset (struct d_info
*, int);
434 static struct demangle_component
*d_ctor_dtor_name (struct d_info
*);
436 static struct demangle_component
**
437 d_cv_qualifiers (struct d_info
*, struct demangle_component
**, int);
439 static struct demangle_component
*
440 d_ref_qualifier (struct d_info
*, struct demangle_component
*);
442 static struct demangle_component
*
443 d_function_type (struct d_info
*);
445 static struct demangle_component
*
446 d_bare_function_type (struct d_info
*, int);
448 static struct demangle_component
*
449 d_class_enum_type (struct d_info
*);
451 static struct demangle_component
*d_array_type (struct d_info
*);
453 static struct demangle_component
*d_vector_type (struct d_info
*);
455 static struct demangle_component
*
456 d_pointer_to_member_type (struct d_info
*);
458 static struct demangle_component
*
459 d_template_param (struct d_info
*);
461 static struct demangle_component
*d_template_args (struct d_info
*);
463 static struct demangle_component
*
464 d_template_arg (struct d_info
*);
466 static struct demangle_component
*d_expression (struct d_info
*);
468 static struct demangle_component
*d_expr_primary (struct d_info
*);
470 static struct demangle_component
*d_local_name (struct d_info
*);
472 static int d_discriminator (struct d_info
*);
474 static struct demangle_component
*d_lambda (struct d_info
*);
476 static struct demangle_component
*d_unnamed_type (struct d_info
*);
478 static struct demangle_component
*
479 d_clone_suffix (struct d_info
*, struct demangle_component
*);
482 d_add_substitution (struct d_info
*, struct demangle_component
*);
484 static struct demangle_component
*d_substitution (struct d_info
*, int);
486 static void d_checkpoint (struct d_info
*, struct d_info_checkpoint
*);
488 static void d_backtrack (struct d_info
*, struct d_info_checkpoint
*);
490 static void d_growable_string_init (struct d_growable_string
*, size_t);
493 d_growable_string_resize (struct d_growable_string
*, size_t);
496 d_growable_string_append_buffer (struct d_growable_string
*,
497 const char *, size_t);
499 d_growable_string_callback_adapter (const char *, size_t, void *);
502 d_print_init (struct d_print_info
*, demangle_callbackref
, void *,
503 const struct demangle_component
*);
505 static inline void d_print_error (struct d_print_info
*);
507 static inline int d_print_saw_error (struct d_print_info
*);
509 static inline void d_print_flush (struct d_print_info
*);
511 static inline void d_append_char (struct d_print_info
*, char);
513 static inline void d_append_buffer (struct d_print_info
*,
514 const char *, size_t);
516 static inline void d_append_string (struct d_print_info
*, const char *);
518 static inline char d_last_char (struct d_print_info
*);
521 d_print_comp (struct d_print_info
*, int, const struct demangle_component
*);
524 d_print_java_identifier (struct d_print_info
*, const char *, int);
527 d_print_mod_list (struct d_print_info
*, int, struct d_print_mod
*, int);
530 d_print_mod (struct d_print_info
*, int, const struct demangle_component
*);
533 d_print_function_type (struct d_print_info
*, int,
534 const struct demangle_component
*,
535 struct d_print_mod
*);
538 d_print_array_type (struct d_print_info
*, int,
539 const struct demangle_component
*,
540 struct d_print_mod
*);
543 d_print_expr_op (struct d_print_info
*, int, const struct demangle_component
*);
546 d_print_cast (struct d_print_info
*, int, const struct demangle_component
*);
548 static int d_demangle_callback (const char *, int,
549 demangle_callbackref
, void *);
550 static char *d_demangle (const char *, int, size_t *);
552 #ifdef CP_DEMANGLE_DEBUG
555 d_dump (struct demangle_component
*dc
, int indent
)
562 printf ("failed demangling\n");
566 for (i
= 0; i
< indent
; ++i
)
571 case DEMANGLE_COMPONENT_NAME
:
572 printf ("name '%.*s'\n", dc
->u
.s_name
.len
, dc
->u
.s_name
.s
);
574 case DEMANGLE_COMPONENT_TAGGED_NAME
:
575 printf ("tagged name\n");
576 d_dump (dc
->u
.s_binary
.left
, indent
+ 2);
577 d_dump (dc
->u
.s_binary
.right
, indent
+ 2);
579 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
580 printf ("template parameter %ld\n", dc
->u
.s_number
.number
);
582 case DEMANGLE_COMPONENT_FUNCTION_PARAM
:
583 printf ("function parameter %ld\n", dc
->u
.s_number
.number
);
585 case DEMANGLE_COMPONENT_CTOR
:
586 printf ("constructor %d\n", (int) dc
->u
.s_ctor
.kind
);
587 d_dump (dc
->u
.s_ctor
.name
, indent
+ 2);
589 case DEMANGLE_COMPONENT_DTOR
:
590 printf ("destructor %d\n", (int) dc
->u
.s_dtor
.kind
);
591 d_dump (dc
->u
.s_dtor
.name
, indent
+ 2);
593 case DEMANGLE_COMPONENT_SUB_STD
:
594 printf ("standard substitution %s\n", dc
->u
.s_string
.string
);
596 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
597 printf ("builtin type %s\n", dc
->u
.s_builtin
.type
->name
);
599 case DEMANGLE_COMPONENT_OPERATOR
:
600 printf ("operator %s\n", dc
->u
.s_operator
.op
->name
);
602 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
603 printf ("extended operator with %d args\n",
604 dc
->u
.s_extended_operator
.args
);
605 d_dump (dc
->u
.s_extended_operator
.name
, indent
+ 2);
608 case DEMANGLE_COMPONENT_QUAL_NAME
:
609 printf ("qualified name\n");
611 case DEMANGLE_COMPONENT_LOCAL_NAME
:
612 printf ("local name\n");
614 case DEMANGLE_COMPONENT_TYPED_NAME
:
615 printf ("typed name\n");
617 case DEMANGLE_COMPONENT_TEMPLATE
:
618 printf ("template\n");
620 case DEMANGLE_COMPONENT_VTABLE
:
623 case DEMANGLE_COMPONENT_VTT
:
626 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
627 printf ("construction vtable\n");
629 case DEMANGLE_COMPONENT_TYPEINFO
:
630 printf ("typeinfo\n");
632 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
633 printf ("typeinfo name\n");
635 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
636 printf ("typeinfo function\n");
638 case DEMANGLE_COMPONENT_THUNK
:
641 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
642 printf ("virtual thunk\n");
644 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
645 printf ("covariant thunk\n");
647 case DEMANGLE_COMPONENT_JAVA_CLASS
:
648 printf ("java class\n");
650 case DEMANGLE_COMPONENT_GUARD
:
653 case DEMANGLE_COMPONENT_REFTEMP
:
654 printf ("reference temporary\n");
656 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
657 printf ("hidden alias\n");
659 case DEMANGLE_COMPONENT_TRANSACTION_CLONE
:
660 printf ("transaction clone\n");
662 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
:
663 printf ("non-transaction clone\n");
665 case DEMANGLE_COMPONENT_RESTRICT
:
666 printf ("restrict\n");
668 case DEMANGLE_COMPONENT_VOLATILE
:
669 printf ("volatile\n");
671 case DEMANGLE_COMPONENT_CONST
:
674 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
675 printf ("restrict this\n");
677 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
678 printf ("volatile this\n");
680 case DEMANGLE_COMPONENT_CONST_THIS
:
681 printf ("const this\n");
683 case DEMANGLE_COMPONENT_REFERENCE_THIS
:
684 printf ("reference this\n");
686 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
:
687 printf ("rvalue reference this\n");
689 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
690 printf ("vendor type qualifier\n");
692 case DEMANGLE_COMPONENT_POINTER
:
693 printf ("pointer\n");
695 case DEMANGLE_COMPONENT_REFERENCE
:
696 printf ("reference\n");
698 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
699 printf ("rvalue reference\n");
701 case DEMANGLE_COMPONENT_COMPLEX
:
702 printf ("complex\n");
704 case DEMANGLE_COMPONENT_IMAGINARY
:
705 printf ("imaginary\n");
707 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
708 printf ("vendor type\n");
710 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
711 printf ("function type\n");
713 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
714 printf ("array type\n");
716 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
717 printf ("pointer to member type\n");
719 case DEMANGLE_COMPONENT_FIXED_TYPE
:
720 printf ("fixed-point type, accum? %d, sat? %d\n",
721 dc
->u
.s_fixed
.accum
, dc
->u
.s_fixed
.sat
);
722 d_dump (dc
->u
.s_fixed
.length
, indent
+ 2);
724 case DEMANGLE_COMPONENT_ARGLIST
:
725 printf ("argument list\n");
727 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
728 printf ("template argument list\n");
730 case DEMANGLE_COMPONENT_INITIALIZER_LIST
:
731 printf ("initializer list\n");
733 case DEMANGLE_COMPONENT_CAST
:
736 case DEMANGLE_COMPONENT_NULLARY
:
737 printf ("nullary operator\n");
739 case DEMANGLE_COMPONENT_UNARY
:
740 printf ("unary operator\n");
742 case DEMANGLE_COMPONENT_BINARY
:
743 printf ("binary operator\n");
745 case DEMANGLE_COMPONENT_BINARY_ARGS
:
746 printf ("binary operator arguments\n");
748 case DEMANGLE_COMPONENT_TRINARY
:
749 printf ("trinary operator\n");
751 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
752 printf ("trinary operator arguments 1\n");
754 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
755 printf ("trinary operator arguments 1\n");
757 case DEMANGLE_COMPONENT_LITERAL
:
758 printf ("literal\n");
760 case DEMANGLE_COMPONENT_LITERAL_NEG
:
761 printf ("negative literal\n");
763 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
764 printf ("java resource\n");
766 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
767 printf ("compound name\n");
769 case DEMANGLE_COMPONENT_CHARACTER
:
770 printf ("character '%c'\n", dc
->u
.s_character
.character
);
772 case DEMANGLE_COMPONENT_NUMBER
:
773 printf ("number %ld\n", dc
->u
.s_number
.number
);
775 case DEMANGLE_COMPONENT_DECLTYPE
:
776 printf ("decltype\n");
778 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
779 printf ("pack expansion\n");
781 case DEMANGLE_COMPONENT_TLS_INIT
:
782 printf ("tls init function\n");
784 case DEMANGLE_COMPONENT_TLS_WRAPPER
:
785 printf ("tls wrapper function\n");
787 case DEMANGLE_COMPONENT_DEFAULT_ARG
:
788 printf ("default argument %d\n", dc
->u
.s_unary_num
.num
);
789 d_dump (dc
->u
.s_unary_num
.sub
, indent
+2);
791 case DEMANGLE_COMPONENT_LAMBDA
:
792 printf ("lambda %d\n", dc
->u
.s_unary_num
.num
);
793 d_dump (dc
->u
.s_unary_num
.sub
, indent
+2);
797 d_dump (d_left (dc
), indent
+ 2);
798 d_dump (d_right (dc
), indent
+ 2);
801 #endif /* CP_DEMANGLE_DEBUG */
803 /* Fill in a DEMANGLE_COMPONENT_NAME. */
805 CP_STATIC_IF_GLIBCPP_V3
807 cplus_demangle_fill_name (struct demangle_component
*p
, const char *s
, int len
)
809 if (p
== NULL
|| s
== NULL
|| len
== 0)
811 p
->type
= DEMANGLE_COMPONENT_NAME
;
813 p
->u
.s_name
.len
= len
;
817 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
819 CP_STATIC_IF_GLIBCPP_V3
821 cplus_demangle_fill_extended_operator (struct demangle_component
*p
, int args
,
822 struct demangle_component
*name
)
824 if (p
== NULL
|| args
< 0 || name
== NULL
)
826 p
->type
= DEMANGLE_COMPONENT_EXTENDED_OPERATOR
;
827 p
->u
.s_extended_operator
.args
= args
;
828 p
->u
.s_extended_operator
.name
= name
;
832 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
834 CP_STATIC_IF_GLIBCPP_V3
836 cplus_demangle_fill_ctor (struct demangle_component
*p
,
837 enum gnu_v3_ctor_kinds kind
,
838 struct demangle_component
*name
)
842 || (int) kind
< gnu_v3_complete_object_ctor
843 || (int) kind
> gnu_v3_object_ctor_group
)
845 p
->type
= DEMANGLE_COMPONENT_CTOR
;
846 p
->u
.s_ctor
.kind
= kind
;
847 p
->u
.s_ctor
.name
= name
;
851 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
853 CP_STATIC_IF_GLIBCPP_V3
855 cplus_demangle_fill_dtor (struct demangle_component
*p
,
856 enum gnu_v3_dtor_kinds kind
,
857 struct demangle_component
*name
)
861 || (int) kind
< gnu_v3_deleting_dtor
862 || (int) kind
> gnu_v3_object_dtor_group
)
864 p
->type
= DEMANGLE_COMPONENT_DTOR
;
865 p
->u
.s_dtor
.kind
= kind
;
866 p
->u
.s_dtor
.name
= name
;
870 /* Add a new component. */
872 static struct demangle_component
*
873 d_make_empty (struct d_info
*di
)
875 struct demangle_component
*p
;
877 if (di
->next_comp
>= di
->num_comps
)
879 p
= &di
->comps
[di
->next_comp
];
884 /* Add a new generic component. */
886 static struct demangle_component
*
887 d_make_comp (struct d_info
*di
, enum demangle_component_type type
,
888 struct demangle_component
*left
,
889 struct demangle_component
*right
)
891 struct demangle_component
*p
;
893 /* We check for errors here. A typical error would be a NULL return
894 from a subroutine. We catch those here, and return NULL
898 /* These types require two parameters. */
899 case DEMANGLE_COMPONENT_QUAL_NAME
:
900 case DEMANGLE_COMPONENT_LOCAL_NAME
:
901 case DEMANGLE_COMPONENT_TYPED_NAME
:
902 case DEMANGLE_COMPONENT_TAGGED_NAME
:
903 case DEMANGLE_COMPONENT_TEMPLATE
:
904 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
905 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
906 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
907 case DEMANGLE_COMPONENT_UNARY
:
908 case DEMANGLE_COMPONENT_BINARY
:
909 case DEMANGLE_COMPONENT_BINARY_ARGS
:
910 case DEMANGLE_COMPONENT_TRINARY
:
911 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
912 case DEMANGLE_COMPONENT_LITERAL
:
913 case DEMANGLE_COMPONENT_LITERAL_NEG
:
914 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
915 case DEMANGLE_COMPONENT_VECTOR_TYPE
:
916 case DEMANGLE_COMPONENT_CLONE
:
917 if (left
== NULL
|| right
== NULL
)
921 /* These types only require one parameter. */
922 case DEMANGLE_COMPONENT_VTABLE
:
923 case DEMANGLE_COMPONENT_VTT
:
924 case DEMANGLE_COMPONENT_TYPEINFO
:
925 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
926 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
927 case DEMANGLE_COMPONENT_THUNK
:
928 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
929 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
930 case DEMANGLE_COMPONENT_JAVA_CLASS
:
931 case DEMANGLE_COMPONENT_GUARD
:
932 case DEMANGLE_COMPONENT_TLS_INIT
:
933 case DEMANGLE_COMPONENT_TLS_WRAPPER
:
934 case DEMANGLE_COMPONENT_REFTEMP
:
935 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
936 case DEMANGLE_COMPONENT_TRANSACTION_CLONE
:
937 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
:
938 case DEMANGLE_COMPONENT_POINTER
:
939 case DEMANGLE_COMPONENT_REFERENCE
:
940 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
941 case DEMANGLE_COMPONENT_COMPLEX
:
942 case DEMANGLE_COMPONENT_IMAGINARY
:
943 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
944 case DEMANGLE_COMPONENT_CAST
:
945 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
946 case DEMANGLE_COMPONENT_DECLTYPE
:
947 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
948 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
:
949 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS
:
950 case DEMANGLE_COMPONENT_NULLARY
:
951 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
956 /* This needs a right parameter, but the left parameter can be
958 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
959 case DEMANGLE_COMPONENT_INITIALIZER_LIST
:
964 /* These are allowed to have no parameters--in some cases they
965 will be filled in later. */
966 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
967 case DEMANGLE_COMPONENT_RESTRICT
:
968 case DEMANGLE_COMPONENT_VOLATILE
:
969 case DEMANGLE_COMPONENT_CONST
:
970 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
971 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
972 case DEMANGLE_COMPONENT_CONST_THIS
:
973 case DEMANGLE_COMPONENT_REFERENCE_THIS
:
974 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
:
975 case DEMANGLE_COMPONENT_ARGLIST
:
976 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
979 /* Other types should not be seen here. */
984 p
= d_make_empty (di
);
988 p
->u
.s_binary
.left
= left
;
989 p
->u
.s_binary
.right
= right
;
994 /* Add a new demangle mangled name component. */
996 static struct demangle_component
*
997 d_make_demangle_mangled_name (struct d_info
*di
, const char *s
)
999 if (d_peek_char (di
) != '_' || d_peek_next_char (di
) != 'Z')
1000 return d_make_name (di
, s
, strlen (s
));
1002 return d_encoding (di
, 0);
1005 /* Add a new name component. */
1007 static struct demangle_component
*
1008 d_make_name (struct d_info
*di
, const char *s
, int len
)
1010 struct demangle_component
*p
;
1012 p
= d_make_empty (di
);
1013 if (! cplus_demangle_fill_name (p
, s
, len
))
1018 /* Add a new builtin type component. */
1020 static struct demangle_component
*
1021 d_make_builtin_type (struct d_info
*di
,
1022 const struct demangle_builtin_type_info
*type
)
1024 struct demangle_component
*p
;
1028 p
= d_make_empty (di
);
1031 p
->type
= DEMANGLE_COMPONENT_BUILTIN_TYPE
;
1032 p
->u
.s_builtin
.type
= type
;
1037 /* Add a new operator component. */
1039 static struct demangle_component
*
1040 d_make_operator (struct d_info
*di
, const struct demangle_operator_info
*op
)
1042 struct demangle_component
*p
;
1044 p
= d_make_empty (di
);
1047 p
->type
= DEMANGLE_COMPONENT_OPERATOR
;
1048 p
->u
.s_operator
.op
= op
;
1053 /* Add a new extended operator component. */
1055 static struct demangle_component
*
1056 d_make_extended_operator (struct d_info
*di
, int args
,
1057 struct demangle_component
*name
)
1059 struct demangle_component
*p
;
1061 p
= d_make_empty (di
);
1062 if (! cplus_demangle_fill_extended_operator (p
, args
, name
))
1067 static struct demangle_component
*
1068 d_make_default_arg (struct d_info
*di
, int num
,
1069 struct demangle_component
*sub
)
1071 struct demangle_component
*p
= d_make_empty (di
);
1074 p
->type
= DEMANGLE_COMPONENT_DEFAULT_ARG
;
1075 p
->u
.s_unary_num
.num
= num
;
1076 p
->u
.s_unary_num
.sub
= sub
;
1081 /* Add a new constructor component. */
1083 static struct demangle_component
*
1084 d_make_ctor (struct d_info
*di
, enum gnu_v3_ctor_kinds kind
,
1085 struct demangle_component
*name
)
1087 struct demangle_component
*p
;
1089 p
= d_make_empty (di
);
1090 if (! cplus_demangle_fill_ctor (p
, kind
, name
))
1095 /* Add a new destructor component. */
1097 static struct demangle_component
*
1098 d_make_dtor (struct d_info
*di
, enum gnu_v3_dtor_kinds kind
,
1099 struct demangle_component
*name
)
1101 struct demangle_component
*p
;
1103 p
= d_make_empty (di
);
1104 if (! cplus_demangle_fill_dtor (p
, kind
, name
))
1109 /* Add a new template parameter. */
1111 static struct demangle_component
*
1112 d_make_template_param (struct d_info
*di
, long i
)
1114 struct demangle_component
*p
;
1116 p
= d_make_empty (di
);
1119 p
->type
= DEMANGLE_COMPONENT_TEMPLATE_PARAM
;
1120 p
->u
.s_number
.number
= i
;
1125 /* Add a new function parameter. */
1127 static struct demangle_component
*
1128 d_make_function_param (struct d_info
*di
, long i
)
1130 struct demangle_component
*p
;
1132 p
= d_make_empty (di
);
1135 p
->type
= DEMANGLE_COMPONENT_FUNCTION_PARAM
;
1136 p
->u
.s_number
.number
= i
;
1141 /* Add a new standard substitution component. */
1143 static struct demangle_component
*
1144 d_make_sub (struct d_info
*di
, const char *name
, int len
)
1146 struct demangle_component
*p
;
1148 p
= d_make_empty (di
);
1151 p
->type
= DEMANGLE_COMPONENT_SUB_STD
;
1152 p
->u
.s_string
.string
= name
;
1153 p
->u
.s_string
.len
= len
;
1158 /* <mangled-name> ::= _Z <encoding> [<clone-suffix>]*
1160 TOP_LEVEL is non-zero when called at the top level. */
1162 CP_STATIC_IF_GLIBCPP_V3
1163 struct demangle_component
*
1164 cplus_demangle_mangled_name (struct d_info
*di
, int top_level
)
1166 struct demangle_component
*p
;
1168 if (! d_check_char (di
, '_')
1169 /* Allow missing _ if not at toplevel to work around a
1170 bug in G++ abi-version=2 mangling; see the comment in
1171 write_template_arg. */
1174 if (! d_check_char (di
, 'Z'))
1176 p
= d_encoding (di
, top_level
);
1178 /* If at top level and parsing parameters, check for a clone
1180 if (top_level
&& (di
->options
& DMGL_PARAMS
) != 0)
1181 while (d_peek_char (di
) == '.'
1182 && (IS_LOWER (d_peek_next_char (di
))
1183 || d_peek_next_char (di
) == '_'
1184 || IS_DIGIT (d_peek_next_char (di
))))
1185 p
= d_clone_suffix (di
, p
);
1190 /* Return whether a function should have a return type. The argument
1191 is the function name, which may be qualified in various ways. The
1192 rules are that template functions have return types with some
1193 exceptions, function types which are not part of a function name
1194 mangling have return types with some exceptions, and non-template
1195 function names do not have return types. The exceptions are that
1196 constructors, destructors, and conversion operators do not have
1200 has_return_type (struct demangle_component
*dc
)
1208 case DEMANGLE_COMPONENT_TEMPLATE
:
1209 return ! is_ctor_dtor_or_conversion (d_left (dc
));
1210 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
1211 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
1212 case DEMANGLE_COMPONENT_CONST_THIS
:
1213 case DEMANGLE_COMPONENT_REFERENCE_THIS
:
1214 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
:
1215 return has_return_type (d_left (dc
));
1219 /* Return whether a name is a constructor, a destructor, or a
1220 conversion operator. */
1223 is_ctor_dtor_or_conversion (struct demangle_component
*dc
)
1231 case DEMANGLE_COMPONENT_QUAL_NAME
:
1232 case DEMANGLE_COMPONENT_LOCAL_NAME
:
1233 return is_ctor_dtor_or_conversion (d_right (dc
));
1234 case DEMANGLE_COMPONENT_CTOR
:
1235 case DEMANGLE_COMPONENT_DTOR
:
1236 case DEMANGLE_COMPONENT_CAST
:
1241 /* <encoding> ::= <(function) name> <bare-function-type>
1245 TOP_LEVEL is non-zero when called at the top level, in which case
1246 if DMGL_PARAMS is not set we do not demangle the function
1247 parameters. We only set this at the top level, because otherwise
1248 we would not correctly demangle names in local scopes. */
1250 static struct demangle_component
*
1251 d_encoding (struct d_info
*di
, int top_level
)
1253 char peek
= d_peek_char (di
);
1255 if (peek
== 'G' || peek
== 'T')
1256 return d_special_name (di
);
1259 struct demangle_component
*dc
;
1263 if (dc
!= NULL
&& top_level
&& (di
->options
& DMGL_PARAMS
) == 0)
1265 /* Strip off any initial CV-qualifiers, as they really apply
1266 to the `this' parameter, and they were not output by the
1267 v2 demangler without DMGL_PARAMS. */
1268 while (dc
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
1269 || dc
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
1270 || dc
->type
== DEMANGLE_COMPONENT_CONST_THIS
1271 || dc
->type
== DEMANGLE_COMPONENT_REFERENCE_THIS
1272 || dc
->type
== DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
)
1275 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1276 there may be CV-qualifiers on its right argument which
1277 really apply here; this happens when parsing a class
1278 which is local to a function. */
1279 if (dc
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
1281 struct demangle_component
*dcr
;
1284 while (dcr
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
1285 || dcr
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
1286 || dcr
->type
== DEMANGLE_COMPONENT_CONST_THIS
1287 || dcr
->type
== DEMANGLE_COMPONENT_REFERENCE_THIS
1288 || dcr
->type
== DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
)
1290 dc
->u
.s_binary
.right
= dcr
;
1296 peek
= d_peek_char (di
);
1297 if (dc
== NULL
|| peek
== '\0' || peek
== 'E')
1299 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPED_NAME
, dc
,
1300 d_bare_function_type (di
, has_return_type (dc
)));
1304 /* <tagged-name> ::= <name> B <source-name> */
1306 static struct demangle_component
*
1307 d_abi_tags (struct d_info
*di
, struct demangle_component
*dc
)
1309 struct demangle_component
*hold_last_name
;
1312 /* Preserve the last name, so the ABI tag doesn't clobber it. */
1313 hold_last_name
= di
->last_name
;
1315 while (peek
= d_peek_char (di
),
1318 struct demangle_component
*tag
;
1320 tag
= d_source_name (di
);
1321 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_TAGGED_NAME
, dc
, tag
);
1324 di
->last_name
= hold_last_name
;
1329 /* <name> ::= <nested-name>
1331 ::= <unscoped-template-name> <template-args>
1334 <unscoped-name> ::= <unqualified-name>
1335 ::= St <unqualified-name>
1337 <unscoped-template-name> ::= <unscoped-name>
1341 static struct demangle_component
*
1342 d_name (struct d_info
*di
)
1344 char peek
= d_peek_char (di
);
1345 struct demangle_component
*dc
;
1350 return d_nested_name (di
);
1353 return d_local_name (di
);
1356 return d_unqualified_name (di
);
1362 if (d_peek_next_char (di
) != 't')
1364 dc
= d_substitution (di
, 0);
1370 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
,
1371 d_make_name (di
, "std", 3),
1372 d_unqualified_name (di
));
1377 if (d_peek_char (di
) != 'I')
1379 /* The grammar does not permit this case to occur if we
1380 called d_substitution() above (i.e., subst == 1). We
1381 don't bother to check. */
1385 /* This is <template-args>, which means that we just saw
1386 <unscoped-template-name>, which is a substitution
1387 candidate if we didn't just get it from a
1391 if (! d_add_substitution (di
, dc
))
1394 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, dc
,
1395 d_template_args (di
));
1403 dc
= d_unqualified_name (di
);
1404 if (d_peek_char (di
) == 'I')
1406 /* This is <template-args>, which means that we just saw
1407 <unscoped-template-name>, which is a substitution
1409 if (! d_add_substitution (di
, dc
))
1411 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, dc
,
1412 d_template_args (di
));
1418 /* <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1419 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
1422 static struct demangle_component
*
1423 d_nested_name (struct d_info
*di
)
1425 struct demangle_component
*ret
;
1426 struct demangle_component
**pret
;
1427 struct demangle_component
*rqual
;
1429 if (! d_check_char (di
, 'N'))
1432 pret
= d_cv_qualifiers (di
, &ret
, 1);
1436 /* Parse the ref-qualifier now and then attach it
1437 once we have something to attach it to. */
1438 rqual
= d_ref_qualifier (di
, NULL
);
1440 *pret
= d_prefix (di
);
1446 d_left (rqual
) = ret
;
1450 if (! d_check_char (di
, 'E'))
1456 /* <prefix> ::= <prefix> <unqualified-name>
1457 ::= <template-prefix> <template-args>
1458 ::= <template-param>
1463 <template-prefix> ::= <prefix> <(template) unqualified-name>
1464 ::= <template-param>
1468 static struct demangle_component
*
1469 d_prefix (struct d_info
*di
)
1471 struct demangle_component
*ret
= NULL
;
1476 enum demangle_component_type comb_type
;
1477 struct demangle_component
*dc
;
1479 peek
= d_peek_char (di
);
1483 /* The older code accepts a <local-name> here, but I don't see
1484 that in the grammar. The older code does not accept a
1485 <template-param> here. */
1487 comb_type
= DEMANGLE_COMPONENT_QUAL_NAME
;
1490 char peek2
= d_peek_next_char (di
);
1491 if (peek2
== 'T' || peek2
== 't')
1493 dc
= cplus_demangle_type (di
);
1495 /* Destructor name. */
1496 dc
= d_unqualified_name (di
);
1498 else if (IS_DIGIT (peek
)
1503 dc
= d_unqualified_name (di
);
1504 else if (peek
== 'S')
1505 dc
= d_substitution (di
, 1);
1506 else if (peek
== 'I')
1510 comb_type
= DEMANGLE_COMPONENT_TEMPLATE
;
1511 dc
= d_template_args (di
);
1513 else if (peek
== 'T')
1514 dc
= d_template_param (di
);
1515 else if (peek
== 'E')
1517 else if (peek
== 'M')
1519 /* Initializer scope for a lambda. We don't need to represent
1520 this; the normal code will just treat the variable as a type
1521 scope, which gives appropriate output. */
1533 ret
= d_make_comp (di
, comb_type
, ret
, dc
);
1535 if (peek
!= 'S' && d_peek_char (di
) != 'E')
1537 if (! d_add_substitution (di
, ret
))
1543 /* <unqualified-name> ::= <operator-name>
1544 ::= <ctor-dtor-name>
1546 ::= <local-source-name>
1548 <local-source-name> ::= L <source-name> <discriminator>
1551 static struct demangle_component
*
1552 d_unqualified_name (struct d_info
*di
)
1554 struct demangle_component
*ret
;
1557 peek
= d_peek_char (di
);
1558 if (IS_DIGIT (peek
))
1559 ret
= d_source_name (di
);
1560 else if (IS_LOWER (peek
))
1562 ret
= d_operator_name (di
);
1563 if (ret
!= NULL
&& ret
->type
== DEMANGLE_COMPONENT_OPERATOR
)
1565 di
->expansion
+= sizeof "operator" + ret
->u
.s_operator
.op
->len
- 2;
1566 if (!strcmp (ret
->u
.s_operator
.op
->code
, "li"))
1567 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, ret
,
1568 d_source_name (di
));
1571 else if (peek
== 'C' || peek
== 'D')
1572 ret
= d_ctor_dtor_name (di
);
1573 else if (peek
== 'L')
1577 ret
= d_source_name (di
);
1580 if (! d_discriminator (di
))
1583 else if (peek
== 'U')
1585 switch (d_peek_next_char (di
))
1588 ret
= d_lambda (di
);
1591 ret
= d_unnamed_type (di
);
1600 if (d_peek_char (di
) == 'B')
1601 ret
= d_abi_tags (di
, ret
);
1605 /* <source-name> ::= <(positive length) number> <identifier> */
1607 static struct demangle_component
*
1608 d_source_name (struct d_info
*di
)
1611 struct demangle_component
*ret
;
1613 len
= d_number (di
);
1616 ret
= d_identifier (di
, len
);
1617 di
->last_name
= ret
;
1621 /* number ::= [n] <(non-negative decimal integer)> */
1624 d_number (struct d_info
*di
)
1631 peek
= d_peek_char (di
);
1636 peek
= d_peek_char (di
);
1642 if (! IS_DIGIT (peek
))
1648 ret
= ret
* 10 + peek
- '0';
1650 peek
= d_peek_char (di
);
1654 /* Like d_number, but returns a demangle_component. */
1656 static struct demangle_component
*
1657 d_number_component (struct d_info
*di
)
1659 struct demangle_component
*ret
= d_make_empty (di
);
1662 ret
->type
= DEMANGLE_COMPONENT_NUMBER
;
1663 ret
->u
.s_number
.number
= d_number (di
);
1668 /* identifier ::= <(unqualified source code identifier)> */
1670 static struct demangle_component
*
1671 d_identifier (struct d_info
*di
, long len
)
1677 if (di
->send
- name
< len
)
1680 d_advance (di
, len
);
1682 /* A Java mangled name may have a trailing '$' if it is a C++
1683 keyword. This '$' is not included in the length count. We just
1685 if ((di
->options
& DMGL_JAVA
) != 0
1686 && d_peek_char (di
) == '$')
1689 /* Look for something which looks like a gcc encoding of an
1690 anonymous namespace, and replace it with a more user friendly
1692 if (len
>= (long) ANONYMOUS_NAMESPACE_PREFIX_LEN
+ 2
1693 && memcmp (name
, ANONYMOUS_NAMESPACE_PREFIX
,
1694 ANONYMOUS_NAMESPACE_PREFIX_LEN
) == 0)
1698 s
= name
+ ANONYMOUS_NAMESPACE_PREFIX_LEN
;
1699 if ((*s
== '.' || *s
== '_' || *s
== '$')
1702 di
->expansion
-= len
- sizeof "(anonymous namespace)";
1703 return d_make_name (di
, "(anonymous namespace)",
1704 sizeof "(anonymous namespace)" - 1);
1708 return d_make_name (di
, name
, len
);
1711 /* operator_name ::= many different two character encodings.
1713 ::= v <digit> <source-name>
1715 This list is sorted for binary search. */
1717 #define NL(s) s, (sizeof s) - 1
1719 CP_STATIC_IF_GLIBCPP_V3
1720 const struct demangle_operator_info cplus_demangle_operators
[] =
1722 { "aN", NL ("&="), 2 },
1723 { "aS", NL ("="), 2 },
1724 { "aa", NL ("&&"), 2 },
1725 { "ad", NL ("&"), 1 },
1726 { "an", NL ("&"), 2 },
1727 { "at", NL ("alignof "), 1 },
1728 { "az", NL ("alignof "), 1 },
1729 { "cc", NL ("const_cast"), 2 },
1730 { "cl", NL ("()"), 2 },
1731 { "cm", NL (","), 2 },
1732 { "co", NL ("~"), 1 },
1733 { "dV", NL ("/="), 2 },
1734 { "da", NL ("delete[] "), 1 },
1735 { "dc", NL ("dynamic_cast"), 2 },
1736 { "de", NL ("*"), 1 },
1737 { "dl", NL ("delete "), 1 },
1738 { "ds", NL (".*"), 2 },
1739 { "dt", NL ("."), 2 },
1740 { "dv", NL ("/"), 2 },
1741 { "eO", NL ("^="), 2 },
1742 { "eo", NL ("^"), 2 },
1743 { "eq", NL ("=="), 2 },
1744 { "ge", NL (">="), 2 },
1745 { "gs", NL ("::"), 1 },
1746 { "gt", NL (">"), 2 },
1747 { "ix", NL ("[]"), 2 },
1748 { "lS", NL ("<<="), 2 },
1749 { "le", NL ("<="), 2 },
1750 { "li", NL ("operator\"\" "), 1 },
1751 { "ls", NL ("<<"), 2 },
1752 { "lt", NL ("<"), 2 },
1753 { "mI", NL ("-="), 2 },
1754 { "mL", NL ("*="), 2 },
1755 { "mi", NL ("-"), 2 },
1756 { "ml", NL ("*"), 2 },
1757 { "mm", NL ("--"), 1 },
1758 { "na", NL ("new[]"), 3 },
1759 { "ne", NL ("!="), 2 },
1760 { "ng", NL ("-"), 1 },
1761 { "nt", NL ("!"), 1 },
1762 { "nw", NL ("new"), 3 },
1763 { "oR", NL ("|="), 2 },
1764 { "oo", NL ("||"), 2 },
1765 { "or", NL ("|"), 2 },
1766 { "pL", NL ("+="), 2 },
1767 { "pl", NL ("+"), 2 },
1768 { "pm", NL ("->*"), 2 },
1769 { "pp", NL ("++"), 1 },
1770 { "ps", NL ("+"), 1 },
1771 { "pt", NL ("->"), 2 },
1772 { "qu", NL ("?"), 3 },
1773 { "rM", NL ("%="), 2 },
1774 { "rS", NL (">>="), 2 },
1775 { "rc", NL ("reinterpret_cast"), 2 },
1776 { "rm", NL ("%"), 2 },
1777 { "rs", NL (">>"), 2 },
1778 { "sc", NL ("static_cast"), 2 },
1779 { "st", NL ("sizeof "), 1 },
1780 { "sz", NL ("sizeof "), 1 },
1781 { "tr", NL ("throw"), 0 },
1782 { "tw", NL ("throw "), 1 },
1783 { NULL
, NULL
, 0, 0 }
1786 static struct demangle_component
*
1787 d_operator_name (struct d_info
*di
)
1792 c1
= d_next_char (di
);
1793 c2
= d_next_char (di
);
1794 if (c1
== 'v' && IS_DIGIT (c2
))
1795 return d_make_extended_operator (di
, c2
- '0', d_source_name (di
));
1796 else if (c1
== 'c' && c2
== 'v')
1798 struct demangle_component
*type
;
1799 int was_conversion
= di
->is_conversion
;
1801 di
->is_conversion
= ! di
->is_expression
;
1802 type
= cplus_demangle_type (di
);
1803 di
->is_conversion
= was_conversion
;
1804 return d_make_comp (di
, DEMANGLE_COMPONENT_CAST
, type
, NULL
);
1808 /* LOW is the inclusive lower bound. */
1810 /* HIGH is the exclusive upper bound. We subtract one to ignore
1811 the sentinel at the end of the array. */
1812 int high
= ((sizeof (cplus_demangle_operators
)
1813 / sizeof (cplus_demangle_operators
[0]))
1819 const struct demangle_operator_info
*p
;
1821 i
= low
+ (high
- low
) / 2;
1822 p
= cplus_demangle_operators
+ i
;
1824 if (c1
== p
->code
[0] && c2
== p
->code
[1])
1825 return d_make_operator (di
, p
);
1827 if (c1
< p
->code
[0] || (c1
== p
->code
[0] && c2
< p
->code
[1]))
1837 static struct demangle_component
*
1838 d_make_character (struct d_info
*di
, int c
)
1840 struct demangle_component
*p
;
1841 p
= d_make_empty (di
);
1844 p
->type
= DEMANGLE_COMPONENT_CHARACTER
;
1845 p
->u
.s_character
.character
= c
;
1850 static struct demangle_component
*
1851 d_java_resource (struct d_info
*di
)
1853 struct demangle_component
*p
= NULL
;
1854 struct demangle_component
*next
= NULL
;
1859 len
= d_number (di
);
1863 /* Eat the leading '_'. */
1864 if (d_next_char (di
) != '_')
1877 /* Each chunk is either a '$' escape... */
1895 next
= d_make_character (di
, c
);
1903 /* ... or a sequence of characters. */
1906 while (i
< len
&& str
[i
] && str
[i
] != '$')
1909 next
= d_make_name (di
, str
, i
);
1922 p
= d_make_comp (di
, DEMANGLE_COMPONENT_COMPOUND_NAME
, p
, next
);
1928 p
= d_make_comp (di
, DEMANGLE_COMPONENT_JAVA_RESOURCE
, p
, NULL
);
1933 /* <special-name> ::= TV <type>
1937 ::= GV <(object) name>
1938 ::= T <call-offset> <(base) encoding>
1939 ::= Tc <call-offset> <call-offset> <(base) encoding>
1940 Also g++ extensions:
1941 ::= TC <type> <(offset) number> _ <(base) type>
1946 ::= Gr <resource name>
1951 static struct demangle_component
*
1952 d_special_name (struct d_info
*di
)
1954 di
->expansion
+= 20;
1955 if (d_check_char (di
, 'T'))
1957 switch (d_next_char (di
))
1961 return d_make_comp (di
, DEMANGLE_COMPONENT_VTABLE
,
1962 cplus_demangle_type (di
), NULL
);
1964 di
->expansion
-= 10;
1965 return d_make_comp (di
, DEMANGLE_COMPONENT_VTT
,
1966 cplus_demangle_type (di
), NULL
);
1968 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO
,
1969 cplus_demangle_type (di
), NULL
);
1971 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO_NAME
,
1972 cplus_demangle_type (di
), NULL
);
1975 if (! d_call_offset (di
, 'h'))
1977 return d_make_comp (di
, DEMANGLE_COMPONENT_THUNK
,
1978 d_encoding (di
, 0), NULL
);
1981 if (! d_call_offset (di
, 'v'))
1983 return d_make_comp (di
, DEMANGLE_COMPONENT_VIRTUAL_THUNK
,
1984 d_encoding (di
, 0), NULL
);
1987 if (! d_call_offset (di
, '\0'))
1989 if (! d_call_offset (di
, '\0'))
1991 return d_make_comp (di
, DEMANGLE_COMPONENT_COVARIANT_THUNK
,
1992 d_encoding (di
, 0), NULL
);
1996 struct demangle_component
*derived_type
;
1998 struct demangle_component
*base_type
;
2000 derived_type
= cplus_demangle_type (di
);
2001 offset
= d_number (di
);
2004 if (! d_check_char (di
, '_'))
2006 base_type
= cplus_demangle_type (di
);
2007 /* We don't display the offset. FIXME: We should display
2008 it in verbose mode. */
2010 return d_make_comp (di
, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
,
2011 base_type
, derived_type
);
2015 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO_FN
,
2016 cplus_demangle_type (di
), NULL
);
2018 return d_make_comp (di
, DEMANGLE_COMPONENT_JAVA_CLASS
,
2019 cplus_demangle_type (di
), NULL
);
2022 return d_make_comp (di
, DEMANGLE_COMPONENT_TLS_INIT
,
2026 return d_make_comp (di
, DEMANGLE_COMPONENT_TLS_WRAPPER
,
2033 else if (d_check_char (di
, 'G'))
2035 switch (d_next_char (di
))
2038 return d_make_comp (di
, DEMANGLE_COMPONENT_GUARD
, d_name (di
), NULL
);
2042 struct demangle_component
*name
= d_name (di
);
2043 return d_make_comp (di
, DEMANGLE_COMPONENT_REFTEMP
, name
,
2044 d_number_component (di
));
2048 return d_make_comp (di
, DEMANGLE_COMPONENT_HIDDEN_ALIAS
,
2049 d_encoding (di
, 0), NULL
);
2052 switch (d_next_char (di
))
2055 return d_make_comp (di
, DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
,
2056 d_encoding (di
, 0), NULL
);
2058 /* ??? The proposal is that other letters (such as 'h') stand
2059 for different variants of transaction cloning, such as
2060 compiling directly for hardware transaction support. But
2061 they still should all be transactional clones of some sort
2062 so go ahead and call them that. */
2064 return d_make_comp (di
, DEMANGLE_COMPONENT_TRANSACTION_CLONE
,
2065 d_encoding (di
, 0), NULL
);
2069 return d_java_resource (di
);
2079 /* <call-offset> ::= h <nv-offset> _
2082 <nv-offset> ::= <(offset) number>
2084 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
2086 The C parameter, if not '\0', is a character we just read which is
2087 the start of the <call-offset>.
2089 We don't display the offset information anywhere. FIXME: We should
2090 display it in verbose mode. */
2093 d_call_offset (struct d_info
*di
, int c
)
2096 c
= d_next_char (di
);
2103 if (! d_check_char (di
, '_'))
2110 if (! d_check_char (di
, '_'))
2116 /* <ctor-dtor-name> ::= C1
2124 static struct demangle_component
*
2125 d_ctor_dtor_name (struct d_info
*di
)
2127 if (di
->last_name
!= NULL
)
2129 if (di
->last_name
->type
== DEMANGLE_COMPONENT_NAME
)
2130 di
->expansion
+= di
->last_name
->u
.s_name
.len
;
2131 else if (di
->last_name
->type
== DEMANGLE_COMPONENT_SUB_STD
)
2132 di
->expansion
+= di
->last_name
->u
.s_string
.len
;
2134 switch (d_peek_char (di
))
2138 enum gnu_v3_ctor_kinds kind
;
2140 switch (d_peek_next_char (di
))
2143 kind
= gnu_v3_complete_object_ctor
;
2146 kind
= gnu_v3_base_object_ctor
;
2149 kind
= gnu_v3_complete_object_allocating_ctor
;
2152 kind
= gnu_v3_unified_ctor
;
2155 kind
= gnu_v3_object_ctor_group
;
2161 return d_make_ctor (di
, kind
, di
->last_name
);
2166 enum gnu_v3_dtor_kinds kind
;
2168 switch (d_peek_next_char (di
))
2171 kind
= gnu_v3_deleting_dtor
;
2174 kind
= gnu_v3_complete_object_dtor
;
2177 kind
= gnu_v3_base_object_dtor
;
2179 /* digit '3' is not used */
2181 kind
= gnu_v3_unified_dtor
;
2184 kind
= gnu_v3_object_dtor_group
;
2190 return d_make_dtor (di
, kind
, di
->last_name
);
2198 /* <type> ::= <builtin-type>
2200 ::= <class-enum-type>
2202 ::= <pointer-to-member-type>
2203 ::= <template-param>
2204 ::= <template-template-param> <template-args>
2206 ::= <CV-qualifiers> <type>
2209 ::= O <type> (C++0x)
2212 ::= U <source-name> <type>
2214 <builtin-type> ::= various one letter codes
2218 CP_STATIC_IF_GLIBCPP_V3
2219 const struct demangle_builtin_type_info
2220 cplus_demangle_builtin_types
[D_BUILTIN_TYPE_COUNT
] =
2222 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT
},
2223 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL
},
2224 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT
},
2225 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT
},
2226 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT
},
2227 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT
},
2228 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT
},
2229 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT
},
2230 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT
},
2231 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED
},
2232 /* k */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2233 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG
},
2234 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG
},
2235 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT
},
2236 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
2238 /* p */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2239 /* q */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2240 /* r */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2241 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT
},
2242 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT
},
2243 /* u */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2244 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID
},
2245 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT
},
2246 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG
},
2247 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
2248 D_PRINT_UNSIGNED_LONG_LONG
},
2249 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT
},
2250 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT
},
2251 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT
},
2252 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT
},
2253 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT
},
2254 /* 30 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT
},
2255 /* 31 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT
},
2256 /* 32 */ { NL ("decltype(nullptr)"), NL ("decltype(nullptr)"),
2260 CP_STATIC_IF_GLIBCPP_V3
2261 struct demangle_component
*
2262 cplus_demangle_type (struct d_info
*di
)
2265 struct demangle_component
*ret
;
2268 /* The ABI specifies that when CV-qualifiers are used, the base type
2269 is substitutable, and the fully qualified type is substitutable,
2270 but the base type with a strict subset of the CV-qualifiers is
2271 not substitutable. The natural recursive implementation of the
2272 CV-qualifiers would cause subsets to be substitutable, so instead
2273 we pull them all off now.
2275 FIXME: The ABI says that order-insensitive vendor qualifiers
2276 should be handled in the same way, but we have no way to tell
2277 which vendor qualifiers are order-insensitive and which are
2278 order-sensitive. So we just assume that they are all
2279 order-sensitive. g++ 3.4 supports only one vendor qualifier,
2280 __vector, and it treats it as order-sensitive when mangling
2283 peek
= d_peek_char (di
);
2284 if (peek
== 'r' || peek
== 'V' || peek
== 'K')
2286 struct demangle_component
**pret
;
2288 pret
= d_cv_qualifiers (di
, &ret
, 0);
2291 if (d_peek_char (di
) == 'F')
2293 /* cv-qualifiers before a function type apply to 'this',
2294 so avoid adding the unqualified function type to
2295 the substitution list. */
2296 *pret
= d_function_type (di
);
2299 *pret
= cplus_demangle_type (di
);
2302 if ((*pret
)->type
== DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
2303 || (*pret
)->type
== DEMANGLE_COMPONENT_REFERENCE_THIS
)
2305 /* Move the ref-qualifier outside the cv-qualifiers so that
2306 they are printed in the right order. */
2307 struct demangle_component
*fn
= d_left (*pret
);
2308 d_left (*pret
) = ret
;
2312 if (! d_add_substitution (di
, ret
))
2321 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2322 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
2323 case 'o': case 's': case 't':
2324 case 'v': case 'w': case 'x': case 'y': case 'z':
2325 ret
= d_make_builtin_type (di
,
2326 &cplus_demangle_builtin_types
[peek
- 'a']);
2327 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2334 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_VENDOR_TYPE
,
2335 d_source_name (di
), NULL
);
2339 ret
= d_function_type (di
);
2342 case '0': case '1': case '2': case '3': case '4':
2343 case '5': case '6': case '7': case '8': case '9':
2346 ret
= d_class_enum_type (di
);
2350 ret
= d_array_type (di
);
2354 ret
= d_pointer_to_member_type (di
);
2358 ret
= d_template_param (di
);
2359 if (d_peek_char (di
) == 'I')
2361 /* This may be <template-template-param> <template-args>.
2362 If this is the type for a conversion operator, we can
2363 have a <template-template-param> here only by following
2364 a derivation like this:
2367 -> <template-prefix> <template-args>
2368 -> <prefix> <template-unqualified-name> <template-args>
2369 -> <unqualified-name> <template-unqualified-name> <template-args>
2370 -> <source-name> <template-unqualified-name> <template-args>
2371 -> <source-name> <operator-name> <template-args>
2372 -> <source-name> cv <type> <template-args>
2373 -> <source-name> cv <template-template-param> <template-args> <template-args>
2375 where the <template-args> is followed by another.
2376 Otherwise, we must have a derivation like this:
2379 -> <template-prefix> <template-args>
2380 -> <prefix> <template-unqualified-name> <template-args>
2381 -> <unqualified-name> <template-unqualified-name> <template-args>
2382 -> <source-name> <template-unqualified-name> <template-args>
2383 -> <source-name> <operator-name> <template-args>
2384 -> <source-name> cv <type> <template-args>
2385 -> <source-name> cv <template-param> <template-args>
2387 where we need to leave the <template-args> to be processed
2388 by d_prefix (following the <template-prefix>).
2390 The <template-template-param> part is a substitution
2392 if (! di
->is_conversion
)
2394 if (! d_add_substitution (di
, ret
))
2396 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
,
2397 d_template_args (di
));
2401 struct demangle_component
*args
;
2402 struct d_info_checkpoint checkpoint
;
2404 d_checkpoint (di
, &checkpoint
);
2405 args
= d_template_args (di
);
2406 if (d_peek_char (di
) == 'I')
2408 if (! d_add_substitution (di
, ret
))
2410 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
,
2414 d_backtrack (di
, &checkpoint
);
2420 /* If this is a special substitution, then it is the start of
2421 <class-enum-type>. */
2425 peek_next
= d_peek_next_char (di
);
2426 if (IS_DIGIT (peek_next
)
2428 || IS_UPPER (peek_next
))
2430 ret
= d_substitution (di
, 0);
2431 /* The substituted name may have been a template name and
2432 may be followed by tepmlate args. */
2433 if (d_peek_char (di
) == 'I')
2434 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
,
2435 d_template_args (di
));
2441 ret
= d_class_enum_type (di
);
2442 /* If the substitution was a complete type, then it is not
2443 a new substitution candidate. However, if the
2444 substitution was followed by template arguments, then
2445 the whole thing is a substitution candidate. */
2446 if (ret
!= NULL
&& ret
->type
== DEMANGLE_COMPONENT_SUB_STD
)
2454 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_RVALUE_REFERENCE
,
2455 cplus_demangle_type (di
), NULL
);
2460 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_POINTER
,
2461 cplus_demangle_type (di
), NULL
);
2466 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_REFERENCE
,
2467 cplus_demangle_type (di
), NULL
);
2472 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_COMPLEX
,
2473 cplus_demangle_type (di
), NULL
);
2478 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_IMAGINARY
,
2479 cplus_demangle_type (di
), NULL
);
2484 ret
= d_source_name (di
);
2485 if (d_peek_char (di
) == 'I')
2486 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
,
2487 d_template_args (di
));
2488 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
,
2489 cplus_demangle_type (di
), ret
);
2495 peek
= d_next_char (di
);
2500 /* decltype (expression) */
2501 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_DECLTYPE
,
2502 d_expression (di
), NULL
);
2503 if (ret
&& d_next_char (di
) != 'E')
2509 /* Pack expansion. */
2510 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_PACK_EXPANSION
,
2511 cplus_demangle_type (di
), NULL
);
2517 ret
= d_make_name (di
, "auto", 4);
2521 /* 32-bit decimal floating point */
2522 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[26]);
2523 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2527 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[27]);
2528 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2532 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[28]);
2533 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2536 /* 16-bit half-precision FP */
2537 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[29]);
2538 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2542 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[30]);
2543 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2547 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[31]);
2548 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2552 /* Fixed point types. DF<int bits><length><fract bits><sat> */
2553 ret
= d_make_empty (di
);
2554 ret
->type
= DEMANGLE_COMPONENT_FIXED_TYPE
;
2555 if ((ret
->u
.s_fixed
.accum
= IS_DIGIT (d_peek_char (di
))))
2556 /* For demangling we don't care about the bits. */
2558 ret
->u
.s_fixed
.length
= cplus_demangle_type (di
);
2559 if (ret
->u
.s_fixed
.length
== NULL
)
2562 peek
= d_next_char (di
);
2563 ret
->u
.s_fixed
.sat
= (peek
== 's');
2567 ret
= d_vector_type (di
);
2572 /* decltype(nullptr) */
2573 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[32]);
2574 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2588 if (! d_add_substitution (di
, ret
))
2595 /* <CV-qualifiers> ::= [r] [V] [K] */
2597 static struct demangle_component
**
2598 d_cv_qualifiers (struct d_info
*di
,
2599 struct demangle_component
**pret
, int member_fn
)
2601 struct demangle_component
**pstart
;
2605 peek
= d_peek_char (di
);
2606 while (peek
== 'r' || peek
== 'V' || peek
== 'K')
2608 enum demangle_component_type t
;
2614 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2615 : DEMANGLE_COMPONENT_RESTRICT
);
2616 di
->expansion
+= sizeof "restrict";
2618 else if (peek
== 'V')
2621 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2622 : DEMANGLE_COMPONENT_VOLATILE
);
2623 di
->expansion
+= sizeof "volatile";
2628 ? DEMANGLE_COMPONENT_CONST_THIS
2629 : DEMANGLE_COMPONENT_CONST
);
2630 di
->expansion
+= sizeof "const";
2633 *pret
= d_make_comp (di
, t
, NULL
, NULL
);
2636 pret
= &d_left (*pret
);
2638 peek
= d_peek_char (di
);
2641 if (!member_fn
&& peek
== 'F')
2643 while (pstart
!= pret
)
2645 switch ((*pstart
)->type
)
2647 case DEMANGLE_COMPONENT_RESTRICT
:
2648 (*pstart
)->type
= DEMANGLE_COMPONENT_RESTRICT_THIS
;
2650 case DEMANGLE_COMPONENT_VOLATILE
:
2651 (*pstart
)->type
= DEMANGLE_COMPONENT_VOLATILE_THIS
;
2653 case DEMANGLE_COMPONENT_CONST
:
2654 (*pstart
)->type
= DEMANGLE_COMPONENT_CONST_THIS
;
2659 pstart
= &d_left (*pstart
);
2666 /* <ref-qualifier> ::= R
2669 static struct demangle_component
*
2670 d_ref_qualifier (struct d_info
*di
, struct demangle_component
*sub
)
2672 struct demangle_component
*ret
= sub
;
2675 peek
= d_peek_char (di
);
2676 if (peek
== 'R' || peek
== 'O')
2678 enum demangle_component_type t
;
2681 t
= DEMANGLE_COMPONENT_REFERENCE_THIS
;
2682 di
->expansion
+= sizeof "&";
2686 t
= DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
;
2687 di
->expansion
+= sizeof "&&";
2691 ret
= d_make_comp (di
, t
, ret
, NULL
);
2697 /* <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] E */
2699 static struct demangle_component
*
2700 d_function_type (struct d_info
*di
)
2702 struct demangle_component
*ret
;
2704 if (! d_check_char (di
, 'F'))
2706 if (d_peek_char (di
) == 'Y')
2708 /* Function has C linkage. We don't print this information.
2709 FIXME: We should print it in verbose mode. */
2712 ret
= d_bare_function_type (di
, 1);
2713 ret
= d_ref_qualifier (di
, ret
);
2715 if (! d_check_char (di
, 'E'))
2722 static struct demangle_component
*
2723 d_parmlist (struct d_info
*di
)
2725 struct demangle_component
*tl
;
2726 struct demangle_component
**ptl
;
2732 struct demangle_component
*type
;
2734 char peek
= d_peek_char (di
);
2735 if (peek
== '\0' || peek
== 'E' || peek
== '.')
2737 if ((peek
== 'R' || peek
== 'O')
2738 && d_peek_next_char (di
) == 'E')
2739 /* Function ref-qualifier, not a ref prefix for a parameter type. */
2741 type
= cplus_demangle_type (di
);
2744 *ptl
= d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, type
, NULL
);
2747 ptl
= &d_right (*ptl
);
2750 /* There should be at least one parameter type besides the optional
2751 return type. A function which takes no arguments will have a
2752 single parameter type void. */
2756 /* If we have a single parameter type void, omit it. */
2757 if (d_right (tl
) == NULL
2758 && d_left (tl
)->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
2759 && d_left (tl
)->u
.s_builtin
.type
->print
== D_PRINT_VOID
)
2761 di
->expansion
-= d_left (tl
)->u
.s_builtin
.type
->len
;
2768 /* <bare-function-type> ::= [J]<type>+ */
2770 static struct demangle_component
*
2771 d_bare_function_type (struct d_info
*di
, int has_return_type
)
2773 struct demangle_component
*return_type
;
2774 struct demangle_component
*tl
;
2777 /* Detect special qualifier indicating that the first argument
2778 is the return type. */
2779 peek
= d_peek_char (di
);
2783 has_return_type
= 1;
2786 if (has_return_type
)
2788 return_type
= cplus_demangle_type (di
);
2789 if (return_type
== NULL
)
2795 tl
= d_parmlist (di
);
2799 return d_make_comp (di
, DEMANGLE_COMPONENT_FUNCTION_TYPE
,
2803 /* <class-enum-type> ::= <name> */
2805 static struct demangle_component
*
2806 d_class_enum_type (struct d_info
*di
)
2811 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2812 ::= A [<(dimension) expression>] _ <(element) type>
2815 static struct demangle_component
*
2816 d_array_type (struct d_info
*di
)
2819 struct demangle_component
*dim
;
2821 if (! d_check_char (di
, 'A'))
2824 peek
= d_peek_char (di
);
2827 else if (IS_DIGIT (peek
))
2835 peek
= d_peek_char (di
);
2837 while (IS_DIGIT (peek
));
2838 dim
= d_make_name (di
, s
, d_str (di
) - s
);
2844 dim
= d_expression (di
);
2849 if (! d_check_char (di
, '_'))
2852 return d_make_comp (di
, DEMANGLE_COMPONENT_ARRAY_TYPE
, dim
,
2853 cplus_demangle_type (di
));
2856 /* <vector-type> ::= Dv <number> _ <type>
2857 ::= Dv _ <expression> _ <type> */
2859 static struct demangle_component
*
2860 d_vector_type (struct d_info
*di
)
2863 struct demangle_component
*dim
;
2865 peek
= d_peek_char (di
);
2869 dim
= d_expression (di
);
2872 dim
= d_number_component (di
);
2877 if (! d_check_char (di
, '_'))
2880 return d_make_comp (di
, DEMANGLE_COMPONENT_VECTOR_TYPE
, dim
,
2881 cplus_demangle_type (di
));
2884 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2886 static struct demangle_component
*
2887 d_pointer_to_member_type (struct d_info
*di
)
2889 struct demangle_component
*cl
;
2890 struct demangle_component
*mem
;
2892 if (! d_check_char (di
, 'M'))
2895 cl
= cplus_demangle_type (di
);
2899 /* The ABI says, "The type of a non-static member function is considered
2900 to be different, for the purposes of substitution, from the type of a
2901 namespace-scope or static member function whose type appears
2902 similar. The types of two non-static member functions are considered
2903 to be different, for the purposes of substitution, if the functions
2904 are members of different classes. In other words, for the purposes of
2905 substitution, the class of which the function is a member is
2906 considered part of the type of function."
2908 For a pointer to member function, this call to cplus_demangle_type
2909 will end up adding a (possibly qualified) non-member function type to
2910 the substitution table, which is not correct; however, the member
2911 function type will never be used in a substitution, so putting the
2912 wrong type in the substitution table is harmless. */
2914 mem
= cplus_demangle_type (di
);
2918 return d_make_comp (di
, DEMANGLE_COMPONENT_PTRMEM_TYPE
, cl
, mem
);
2921 /* <non-negative number> _ */
2924 d_compact_number (struct d_info
*di
)
2927 if (d_peek_char (di
) == '_')
2929 else if (d_peek_char (di
) == 'n')
2932 num
= d_number (di
) + 1;
2934 if (! d_check_char (di
, '_'))
2939 /* <template-param> ::= T_
2940 ::= T <(parameter-2 non-negative) number> _
2943 static struct demangle_component
*
2944 d_template_param (struct d_info
*di
)
2948 if (! d_check_char (di
, 'T'))
2951 param
= d_compact_number (di
);
2957 return d_make_template_param (di
, param
);
2960 /* <template-args> ::= I <template-arg>+ E */
2962 static struct demangle_component
*
2963 d_template_args (struct d_info
*di
)
2965 struct demangle_component
*hold_last_name
;
2966 struct demangle_component
*al
;
2967 struct demangle_component
**pal
;
2969 /* Preserve the last name we saw--don't let the template arguments
2970 clobber it, as that would give us the wrong name for a subsequent
2971 constructor or destructor. */
2972 hold_last_name
= di
->last_name
;
2974 if (d_peek_char (di
) != 'I'
2975 && d_peek_char (di
) != 'J')
2979 if (d_peek_char (di
) == 'E')
2981 /* An argument pack can be empty. */
2983 return d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
, NULL
, NULL
);
2990 struct demangle_component
*a
;
2992 a
= d_template_arg (di
);
2996 *pal
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
, a
, NULL
);
2999 pal
= &d_right (*pal
);
3001 if (d_peek_char (di
) == 'E')
3008 di
->last_name
= hold_last_name
;
3013 /* <template-arg> ::= <type>
3014 ::= X <expression> E
3018 static struct demangle_component
*
3019 d_template_arg (struct d_info
*di
)
3021 struct demangle_component
*ret
;
3023 switch (d_peek_char (di
))
3027 ret
= d_expression (di
);
3028 if (! d_check_char (di
, 'E'))
3033 return d_expr_primary (di
);
3037 /* An argument pack. */
3038 return d_template_args (di
);
3041 return cplus_demangle_type (di
);
3045 /* Parse a sequence of expressions until we hit the terminator
3048 static struct demangle_component
*
3049 d_exprlist (struct d_info
*di
, char terminator
)
3051 struct demangle_component
*list
= NULL
;
3052 struct demangle_component
**p
= &list
;
3054 if (d_peek_char (di
) == terminator
)
3057 return d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, NULL
, NULL
);
3062 struct demangle_component
*arg
= d_expression (di
);
3066 *p
= d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, arg
, NULL
);
3071 if (d_peek_char (di
) == terminator
)
3081 /* Returns nonzero iff OP is an operator for a C++ cast: const_cast,
3082 dynamic_cast, static_cast or reinterpret_cast. */
3085 op_is_new_cast (struct demangle_component
*op
)
3087 const char *code
= op
->u
.s_operator
.op
->code
;
3088 return (code
[1] == 'c'
3089 && (code
[0] == 's' || code
[0] == 'd'
3090 || code
[0] == 'c' || code
[0] == 'r'));
3093 /* <expression> ::= <(unary) operator-name> <expression>
3094 ::= <(binary) operator-name> <expression> <expression>
3095 ::= <(trinary) operator-name> <expression> <expression> <expression>
3096 ::= cl <expression>+ E
3098 ::= <template-param>
3099 ::= sr <type> <unqualified-name>
3100 ::= sr <type> <unqualified-name> <template-args>
3104 static inline struct demangle_component
*
3105 d_expression_1 (struct d_info
*di
)
3109 peek
= d_peek_char (di
);
3111 return d_expr_primary (di
);
3112 else if (peek
== 'T')
3113 return d_template_param (di
);
3114 else if (peek
== 's' && d_peek_next_char (di
) == 'r')
3116 struct demangle_component
*type
;
3117 struct demangle_component
*name
;
3120 type
= cplus_demangle_type (di
);
3121 name
= d_unqualified_name (di
);
3122 if (d_peek_char (di
) != 'I')
3123 return d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
, type
, name
);
3125 return d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
, type
,
3126 d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, name
,
3127 d_template_args (di
)));
3129 else if (peek
== 's' && d_peek_next_char (di
) == 'p')
3132 return d_make_comp (di
, DEMANGLE_COMPONENT_PACK_EXPANSION
,
3133 d_expression_1 (di
), NULL
);
3135 else if (peek
== 'f' && d_peek_next_char (di
) == 'p')
3137 /* Function parameter used in a late-specified return type. */
3140 if (d_peek_char (di
) == 'T')
3142 /* 'this' parameter. */
3148 index
= d_compact_number (di
) + 1;
3152 return d_make_function_param (di
, index
);
3154 else if (IS_DIGIT (peek
)
3155 || (peek
== 'o' && d_peek_next_char (di
) == 'n'))
3157 /* We can get an unqualified name as an expression in the case of
3158 a dependent function call, i.e. decltype(f(t)). */
3159 struct demangle_component
*name
;
3162 /* operator-function-id, i.e. operator+(t). */
3165 name
= d_unqualified_name (di
);
3168 if (d_peek_char (di
) == 'I')
3169 return d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, name
,
3170 d_template_args (di
));
3174 else if ((peek
== 'i' || peek
== 't')
3175 && d_peek_next_char (di
) == 'l')
3177 /* Brace-enclosed initializer list, untyped or typed. */
3178 struct demangle_component
*type
= NULL
;
3180 type
= cplus_demangle_type (di
);
3181 if (!d_peek_next_char (di
))
3184 return d_make_comp (di
, DEMANGLE_COMPONENT_INITIALIZER_LIST
,
3185 type
, d_exprlist (di
, 'E'));
3189 struct demangle_component
*op
;
3190 const char *code
= NULL
;
3193 op
= d_operator_name (di
);
3197 if (op
->type
== DEMANGLE_COMPONENT_OPERATOR
)
3199 code
= op
->u
.s_operator
.op
->code
;
3200 di
->expansion
+= op
->u
.s_operator
.op
->len
- 2;
3201 if (strcmp (code
, "st") == 0)
3202 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
3203 cplus_demangle_type (di
));
3210 case DEMANGLE_COMPONENT_OPERATOR
:
3211 args
= op
->u
.s_operator
.op
->args
;
3213 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
3214 args
= op
->u
.s_extended_operator
.args
;
3216 case DEMANGLE_COMPONENT_CAST
:
3224 return d_make_comp (di
, DEMANGLE_COMPONENT_NULLARY
, op
, NULL
);
3228 struct demangle_component
*operand
;
3231 if (code
&& (code
[0] == 'p' || code
[0] == 'm')
3232 && code
[1] == code
[0])
3233 /* pp_ and mm_ are the prefix variants. */
3234 suffix
= !d_check_char (di
, '_');
3236 if (op
->type
== DEMANGLE_COMPONENT_CAST
3237 && d_check_char (di
, '_'))
3238 operand
= d_exprlist (di
, 'E');
3240 operand
= d_expression_1 (di
);
3243 /* Indicate the suffix variant for d_print_comp. */
3244 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
3246 DEMANGLE_COMPONENT_BINARY_ARGS
,
3249 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
3254 struct demangle_component
*left
;
3255 struct demangle_component
*right
;
3259 if (op_is_new_cast (op
))
3260 left
= cplus_demangle_type (di
);
3262 left
= d_expression_1 (di
);
3263 if (!strcmp (code
, "cl"))
3264 right
= d_exprlist (di
, 'E');
3265 else if (!strcmp (code
, "dt") || !strcmp (code
, "pt"))
3267 right
= d_unqualified_name (di
);
3268 if (d_peek_char (di
) == 'I')
3269 right
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
,
3270 right
, d_template_args (di
));
3273 right
= d_expression_1 (di
);
3275 return d_make_comp (di
, DEMANGLE_COMPONENT_BINARY
, op
,
3277 DEMANGLE_COMPONENT_BINARY_ARGS
,
3282 struct demangle_component
*first
;
3283 struct demangle_component
*second
;
3284 struct demangle_component
*third
;
3288 else if (!strcmp (code
, "qu"))
3290 /* ?: expression. */
3291 first
= d_expression_1 (di
);
3292 second
= d_expression_1 (di
);
3293 third
= d_expression_1 (di
);
3295 else if (code
[0] == 'n')
3297 /* new-expression. */
3298 if (code
[1] != 'w' && code
[1] != 'a')
3300 first
= d_exprlist (di
, '_');
3301 second
= cplus_demangle_type (di
);
3302 if (d_peek_char (di
) == 'E')
3307 else if (d_peek_char (di
) == 'p'
3308 && d_peek_next_char (di
) == 'i')
3310 /* Parenthesized initializer. */
3312 third
= d_exprlist (di
, 'E');
3314 else if (d_peek_char (di
) == 'i'
3315 && d_peek_next_char (di
) == 'l')
3316 /* initializer-list. */
3317 third
= d_expression_1 (di
);
3323 return d_make_comp (di
, DEMANGLE_COMPONENT_TRINARY
, op
,
3325 DEMANGLE_COMPONENT_TRINARY_ARG1
,
3328 DEMANGLE_COMPONENT_TRINARY_ARG2
,
3337 static struct demangle_component
*
3338 d_expression (struct d_info
*di
)
3340 struct demangle_component
*ret
;
3341 int was_expression
= di
->is_expression
;
3343 di
->is_expression
= 1;
3344 ret
= d_expression_1 (di
);
3345 di
->is_expression
= was_expression
;
3349 /* <expr-primary> ::= L <type> <(value) number> E
3350 ::= L <type> <(value) float> E
3351 ::= L <mangled-name> E
3354 static struct demangle_component
*
3355 d_expr_primary (struct d_info
*di
)
3357 struct demangle_component
*ret
;
3359 if (! d_check_char (di
, 'L'))
3361 if (d_peek_char (di
) == '_'
3362 /* Workaround for G++ bug; see comment in write_template_arg. */
3363 || d_peek_char (di
) == 'Z')
3364 ret
= cplus_demangle_mangled_name (di
, 0);
3367 struct demangle_component
*type
;
3368 enum demangle_component_type t
;
3371 type
= cplus_demangle_type (di
);
3375 /* If we have a type we know how to print, we aren't going to
3376 print the type name itself. */
3377 if (type
->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
3378 && type
->u
.s_builtin
.type
->print
!= D_PRINT_DEFAULT
)
3379 di
->expansion
-= type
->u
.s_builtin
.type
->len
;
3381 /* Rather than try to interpret the literal value, we just
3382 collect it as a string. Note that it's possible to have a
3383 floating point literal here. The ABI specifies that the
3384 format of such literals is machine independent. That's fine,
3385 but what's not fine is that versions of g++ up to 3.2 with
3386 -fabi-version=1 used upper case letters in the hex constant,
3387 and dumped out gcc's internal representation. That makes it
3388 hard to tell where the constant ends, and hard to dump the
3389 constant in any readable form anyhow. We don't attempt to
3390 handle these cases. */
3392 t
= DEMANGLE_COMPONENT_LITERAL
;
3393 if (d_peek_char (di
) == 'n')
3395 t
= DEMANGLE_COMPONENT_LITERAL_NEG
;
3399 while (d_peek_char (di
) != 'E')
3401 if (d_peek_char (di
) == '\0')
3405 ret
= d_make_comp (di
, t
, type
, d_make_name (di
, s
, d_str (di
) - s
));
3407 if (! d_check_char (di
, 'E'))
3412 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
3413 ::= Z <(function) encoding> E s [<discriminator>]
3414 ::= Z <(function) encoding> E d [<parameter> number>] _ <entity name>
3417 static struct demangle_component
*
3418 d_local_name (struct d_info
*di
)
3420 struct demangle_component
*function
;
3422 if (! d_check_char (di
, 'Z'))
3425 function
= d_encoding (di
, 0);
3427 if (! d_check_char (di
, 'E'))
3430 if (d_peek_char (di
) == 's')
3433 if (! d_discriminator (di
))
3435 return d_make_comp (di
, DEMANGLE_COMPONENT_LOCAL_NAME
, function
,
3436 d_make_name (di
, "string literal",
3437 sizeof "string literal" - 1));
3441 struct demangle_component
*name
;
3444 if (d_peek_char (di
) == 'd')
3446 /* Default argument scope: d <number> _. */
3448 num
= d_compact_number (di
);
3457 /* Lambdas and unnamed types have internal discriminators. */
3458 case DEMANGLE_COMPONENT_LAMBDA
:
3459 case DEMANGLE_COMPONENT_UNNAMED_TYPE
:
3462 if (! d_discriminator (di
))
3466 name
= d_make_default_arg (di
, num
, name
);
3467 return d_make_comp (di
, DEMANGLE_COMPONENT_LOCAL_NAME
, function
, name
);
3471 /* <discriminator> ::= _ <(non-negative) number>
3473 We demangle the discriminator, but we don't print it out. FIXME:
3474 We should print it out in verbose mode. */
3477 d_discriminator (struct d_info
*di
)
3481 if (d_peek_char (di
) != '_')
3484 discrim
= d_number (di
);
3490 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ */
3492 static struct demangle_component
*
3493 d_lambda (struct d_info
*di
)
3495 struct demangle_component
*tl
;
3496 struct demangle_component
*ret
;
3499 if (! d_check_char (di
, 'U'))
3501 if (! d_check_char (di
, 'l'))
3504 tl
= d_parmlist (di
);
3508 if (! d_check_char (di
, 'E'))
3511 num
= d_compact_number (di
);
3515 ret
= d_make_empty (di
);
3518 ret
->type
= DEMANGLE_COMPONENT_LAMBDA
;
3519 ret
->u
.s_unary_num
.sub
= tl
;
3520 ret
->u
.s_unary_num
.num
= num
;
3523 if (! d_add_substitution (di
, ret
))
3529 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
3531 static struct demangle_component
*
3532 d_unnamed_type (struct d_info
*di
)
3534 struct demangle_component
*ret
;
3537 if (! d_check_char (di
, 'U'))
3539 if (! d_check_char (di
, 't'))
3542 num
= d_compact_number (di
);
3546 ret
= d_make_empty (di
);
3549 ret
->type
= DEMANGLE_COMPONENT_UNNAMED_TYPE
;
3550 ret
->u
.s_number
.number
= num
;
3553 if (! d_add_substitution (di
, ret
))
3559 /* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]*
3562 static struct demangle_component
*
3563 d_clone_suffix (struct d_info
*di
, struct demangle_component
*encoding
)
3565 const char *suffix
= d_str (di
);
3566 const char *pend
= suffix
;
3567 struct demangle_component
*n
;
3569 if (*pend
== '.' && (IS_LOWER (pend
[1]) || pend
[1] == '_'))
3572 while (IS_LOWER (*pend
) || *pend
== '_')
3575 while (*pend
== '.' && IS_DIGIT (pend
[1]))
3578 while (IS_DIGIT (*pend
))
3581 d_advance (di
, pend
- suffix
);
3582 n
= d_make_name (di
, suffix
, pend
- suffix
);
3583 return d_make_comp (di
, DEMANGLE_COMPONENT_CLONE
, encoding
, n
);
3586 /* Add a new substitution. */
3589 d_add_substitution (struct d_info
*di
, struct demangle_component
*dc
)
3593 if (di
->next_sub
>= di
->num_subs
)
3595 di
->subs
[di
->next_sub
] = dc
;
3600 /* <substitution> ::= S <seq-id> _
3610 If PREFIX is non-zero, then this type is being used as a prefix in
3611 a qualified name. In this case, for the standard substitutions, we
3612 need to check whether we are being used as a prefix for a
3613 constructor or destructor, and return a full template name.
3614 Otherwise we will get something like std::iostream::~iostream()
3615 which does not correspond particularly well to any function which
3616 actually appears in the source.
3619 static const struct d_standard_sub_info standard_subs
[] =
3624 { 'a', NL ("std::allocator"),
3625 NL ("std::allocator"),
3627 { 'b', NL ("std::basic_string"),
3628 NL ("std::basic_string"),
3629 NL ("basic_string") },
3630 { 's', NL ("std::string"),
3631 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
3632 NL ("basic_string") },
3633 { 'i', NL ("std::istream"),
3634 NL ("std::basic_istream<char, std::char_traits<char> >"),
3635 NL ("basic_istream") },
3636 { 'o', NL ("std::ostream"),
3637 NL ("std::basic_ostream<char, std::char_traits<char> >"),
3638 NL ("basic_ostream") },
3639 { 'd', NL ("std::iostream"),
3640 NL ("std::basic_iostream<char, std::char_traits<char> >"),
3641 NL ("basic_iostream") }
3644 static struct demangle_component
*
3645 d_substitution (struct d_info
*di
, int prefix
)
3649 if (! d_check_char (di
, 'S'))
3652 c
= d_next_char (di
);
3653 if (c
== '_' || IS_DIGIT (c
) || IS_UPPER (c
))
3662 unsigned int new_id
;
3665 new_id
= id
* 36 + c
- '0';
3666 else if (IS_UPPER (c
))
3667 new_id
= id
* 36 + c
- 'A' + 10;
3673 c
= d_next_char (di
);
3680 if (id
>= (unsigned int) di
->next_sub
)
3685 return di
->subs
[id
];
3690 const struct d_standard_sub_info
*p
;
3691 const struct d_standard_sub_info
*pend
;
3693 verbose
= (di
->options
& DMGL_VERBOSE
) != 0;
3694 if (! verbose
&& prefix
)
3698 peek
= d_peek_char (di
);
3699 if (peek
== 'C' || peek
== 'D')
3703 pend
= (&standard_subs
[0]
3704 + sizeof standard_subs
/ sizeof standard_subs
[0]);
3705 for (p
= &standard_subs
[0]; p
< pend
; ++p
)
3711 struct demangle_component
*c
;
3713 if (p
->set_last_name
!= NULL
)
3714 di
->last_name
= d_make_sub (di
, p
->set_last_name
,
3715 p
->set_last_name_len
);
3718 s
= p
->full_expansion
;
3723 s
= p
->simple_expansion
;
3724 len
= p
->simple_len
;
3726 di
->expansion
+= len
;
3727 c
= d_make_sub (di
, s
, len
);
3728 if (d_peek_char (di
) == 'B')
3730 /* If there are ABI tags on the abbreviation, it becomes
3731 a substitution candidate. */
3732 c
= d_abi_tags (di
, c
);
3733 d_add_substitution (di
, c
);
3744 d_checkpoint (struct d_info
*di
, struct d_info_checkpoint
*checkpoint
)
3746 checkpoint
->n
= di
->n
;
3747 checkpoint
->next_comp
= di
->next_comp
;
3748 checkpoint
->next_sub
= di
->next_sub
;
3749 checkpoint
->did_subs
= di
->did_subs
;
3750 checkpoint
->expansion
= di
->expansion
;
3754 d_backtrack (struct d_info
*di
, struct d_info_checkpoint
*checkpoint
)
3756 di
->n
= checkpoint
->n
;
3757 di
->next_comp
= checkpoint
->next_comp
;
3758 di
->next_sub
= checkpoint
->next_sub
;
3759 di
->did_subs
= checkpoint
->did_subs
;
3760 di
->expansion
= checkpoint
->expansion
;
3763 /* Initialize a growable string. */
3766 d_growable_string_init (struct d_growable_string
*dgs
, size_t estimate
)
3771 dgs
->allocation_failure
= 0;
3774 d_growable_string_resize (dgs
, estimate
);
3777 /* Grow a growable string to a given size. */
3780 d_growable_string_resize (struct d_growable_string
*dgs
, size_t need
)
3785 if (dgs
->allocation_failure
)
3788 /* Start allocation at two bytes to avoid any possibility of confusion
3789 with the special value of 1 used as a return in *palc to indicate
3790 allocation failures. */
3791 newalc
= dgs
->alc
> 0 ? dgs
->alc
: 2;
3792 while (newalc
< need
)
3795 newbuf
= (char *) realloc (dgs
->buf
, newalc
);
3802 dgs
->allocation_failure
= 1;
3809 /* Append a buffer to a growable string. */
3812 d_growable_string_append_buffer (struct d_growable_string
*dgs
,
3813 const char *s
, size_t l
)
3817 need
= dgs
->len
+ l
+ 1;
3818 if (need
> dgs
->alc
)
3819 d_growable_string_resize (dgs
, need
);
3821 if (dgs
->allocation_failure
)
3824 memcpy (dgs
->buf
+ dgs
->len
, s
, l
);
3825 dgs
->buf
[dgs
->len
+ l
] = '\0';
3829 /* Bridge growable strings to the callback mechanism. */
3832 d_growable_string_callback_adapter (const char *s
, size_t l
, void *opaque
)
3834 struct d_growable_string
*dgs
= (struct d_growable_string
*) opaque
;
3836 d_growable_string_append_buffer (dgs
, s
, l
);
3839 /* Walk the tree, counting the number of templates encountered, and
3840 the number of times a scope might be saved. These counts will be
3841 used to allocate data structures for d_print_comp, so the logic
3842 here must mirror the logic d_print_comp will use. It is not
3843 important that the resulting numbers are exact, so long as they
3844 are larger than the actual numbers encountered. */
3847 d_count_templates_scopes (int *num_templates
, int *num_scopes
,
3848 const struct demangle_component
*dc
)
3855 case DEMANGLE_COMPONENT_NAME
:
3856 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
3857 case DEMANGLE_COMPONENT_FUNCTION_PARAM
:
3858 case DEMANGLE_COMPONENT_SUB_STD
:
3859 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
3860 case DEMANGLE_COMPONENT_OPERATOR
:
3861 case DEMANGLE_COMPONENT_CHARACTER
:
3862 case DEMANGLE_COMPONENT_NUMBER
:
3863 case DEMANGLE_COMPONENT_UNNAMED_TYPE
:
3866 case DEMANGLE_COMPONENT_TEMPLATE
:
3868 goto recurse_left_right
;
3870 case DEMANGLE_COMPONENT_REFERENCE
:
3871 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
3872 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_TEMPLATE_PARAM
)
3874 goto recurse_left_right
;
3876 case DEMANGLE_COMPONENT_QUAL_NAME
:
3877 case DEMANGLE_COMPONENT_LOCAL_NAME
:
3878 case DEMANGLE_COMPONENT_TYPED_NAME
:
3879 case DEMANGLE_COMPONENT_VTABLE
:
3880 case DEMANGLE_COMPONENT_VTT
:
3881 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
3882 case DEMANGLE_COMPONENT_TYPEINFO
:
3883 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
3884 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
3885 case DEMANGLE_COMPONENT_THUNK
:
3886 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
3887 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
3888 case DEMANGLE_COMPONENT_JAVA_CLASS
:
3889 case DEMANGLE_COMPONENT_GUARD
:
3890 case DEMANGLE_COMPONENT_TLS_INIT
:
3891 case DEMANGLE_COMPONENT_TLS_WRAPPER
:
3892 case DEMANGLE_COMPONENT_REFTEMP
:
3893 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
3894 case DEMANGLE_COMPONENT_RESTRICT
:
3895 case DEMANGLE_COMPONENT_VOLATILE
:
3896 case DEMANGLE_COMPONENT_CONST
:
3897 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
3898 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
3899 case DEMANGLE_COMPONENT_CONST_THIS
:
3900 case DEMANGLE_COMPONENT_REFERENCE_THIS
:
3901 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
:
3902 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
3903 case DEMANGLE_COMPONENT_POINTER
:
3904 case DEMANGLE_COMPONENT_COMPLEX
:
3905 case DEMANGLE_COMPONENT_IMAGINARY
:
3906 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
3907 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
3908 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
3909 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
3910 case DEMANGLE_COMPONENT_VECTOR_TYPE
:
3911 case DEMANGLE_COMPONENT_ARGLIST
:
3912 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
3913 case DEMANGLE_COMPONENT_INITIALIZER_LIST
:
3914 case DEMANGLE_COMPONENT_CAST
:
3915 case DEMANGLE_COMPONENT_NULLARY
:
3916 case DEMANGLE_COMPONENT_UNARY
:
3917 case DEMANGLE_COMPONENT_BINARY
:
3918 case DEMANGLE_COMPONENT_BINARY_ARGS
:
3919 case DEMANGLE_COMPONENT_TRINARY
:
3920 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
3921 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
3922 case DEMANGLE_COMPONENT_LITERAL
:
3923 case DEMANGLE_COMPONENT_LITERAL_NEG
:
3924 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
3925 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
3926 case DEMANGLE_COMPONENT_DECLTYPE
:
3927 case DEMANGLE_COMPONENT_TRANSACTION_CLONE
:
3928 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
:
3929 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
3930 case DEMANGLE_COMPONENT_TAGGED_NAME
:
3931 case DEMANGLE_COMPONENT_CLONE
:
3933 d_count_templates_scopes (num_templates
, num_scopes
,
3935 d_count_templates_scopes (num_templates
, num_scopes
,
3939 case DEMANGLE_COMPONENT_CTOR
:
3940 d_count_templates_scopes (num_templates
, num_scopes
,
3944 case DEMANGLE_COMPONENT_DTOR
:
3945 d_count_templates_scopes (num_templates
, num_scopes
,
3949 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
3950 d_count_templates_scopes (num_templates
, num_scopes
,
3951 dc
->u
.s_extended_operator
.name
);
3954 case DEMANGLE_COMPONENT_FIXED_TYPE
:
3955 d_count_templates_scopes (num_templates
, num_scopes
,
3956 dc
->u
.s_fixed
.length
);
3959 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
:
3960 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS
:
3961 d_count_templates_scopes (num_templates
, num_scopes
,
3965 case DEMANGLE_COMPONENT_LAMBDA
:
3966 case DEMANGLE_COMPONENT_DEFAULT_ARG
:
3967 d_count_templates_scopes (num_templates
, num_scopes
,
3968 dc
->u
.s_unary_num
.sub
);
3973 /* Initialize a print information structure. */
3976 d_print_init (struct d_print_info
*dpi
, demangle_callbackref callback
,
3977 void *opaque
, const struct demangle_component
*dc
)
3980 dpi
->last_char
= '\0';
3981 dpi
->templates
= NULL
;
3982 dpi
->modifiers
= NULL
;
3983 dpi
->pack_index
= 0;
3984 dpi
->flush_count
= 0;
3986 dpi
->callback
= callback
;
3987 dpi
->opaque
= opaque
;
3989 dpi
->demangle_failure
= 0;
3991 dpi
->component_stack
= NULL
;
3993 dpi
->saved_scopes
= NULL
;
3994 dpi
->next_saved_scope
= 0;
3995 dpi
->num_saved_scopes
= 0;
3997 dpi
->copy_templates
= NULL
;
3998 dpi
->next_copy_template
= 0;
3999 dpi
->num_copy_templates
= 0;
4001 d_count_templates_scopes (&dpi
->num_copy_templates
,
4002 &dpi
->num_saved_scopes
, dc
);
4003 dpi
->num_copy_templates
*= dpi
->num_saved_scopes
;
4005 dpi
->current_template
= NULL
;
4008 /* Indicate that an error occurred during printing, and test for error. */
4011 d_print_error (struct d_print_info
*dpi
)
4013 dpi
->demangle_failure
= 1;
4017 d_print_saw_error (struct d_print_info
*dpi
)
4019 return dpi
->demangle_failure
!= 0;
4022 /* Flush buffered characters to the callback. */
4025 d_print_flush (struct d_print_info
*dpi
)
4027 dpi
->buf
[dpi
->len
] = '\0';
4028 dpi
->callback (dpi
->buf
, dpi
->len
, dpi
->opaque
);
4033 /* Append characters and buffers for printing. */
4036 d_append_char (struct d_print_info
*dpi
, char c
)
4038 if (dpi
->len
== sizeof (dpi
->buf
) - 1)
4039 d_print_flush (dpi
);
4041 dpi
->buf
[dpi
->len
++] = c
;
4046 d_append_buffer (struct d_print_info
*dpi
, const char *s
, size_t l
)
4050 for (i
= 0; i
< l
; i
++)
4051 d_append_char (dpi
, s
[i
]);
4055 d_append_string (struct d_print_info
*dpi
, const char *s
)
4057 d_append_buffer (dpi
, s
, strlen (s
));
4061 d_append_num (struct d_print_info
*dpi
, long l
)
4064 sprintf (buf
,"%ld", l
);
4065 d_append_string (dpi
, buf
);
4069 d_last_char (struct d_print_info
*dpi
)
4071 return dpi
->last_char
;
4074 /* Turn components into a human readable string. OPTIONS is the
4075 options bits passed to the demangler. DC is the tree to print.
4076 CALLBACK is a function to call to flush demangled string segments
4077 as they fill the intermediate buffer, and OPAQUE is a generalized
4078 callback argument. On success, this returns 1. On failure,
4079 it returns 0, indicating a bad parse. It does not use heap
4080 memory to build an output string, so cannot encounter memory
4081 allocation failure. */
4083 CP_STATIC_IF_GLIBCPP_V3
4085 cplus_demangle_print_callback (int options
,
4086 const struct demangle_component
*dc
,
4087 demangle_callbackref callback
, void *opaque
)
4089 struct d_print_info dpi
;
4091 d_print_init (&dpi
, callback
, opaque
, dc
);
4094 #ifdef CP_DYNAMIC_ARRAYS
4095 __extension__
struct d_saved_scope scopes
[dpi
.num_saved_scopes
];
4096 __extension__
struct d_print_template temps
[dpi
.num_copy_templates
];
4098 dpi
.saved_scopes
= scopes
;
4099 dpi
.copy_templates
= temps
;
4101 dpi
.saved_scopes
= alloca (dpi
.num_saved_scopes
4102 * sizeof (*dpi
.saved_scopes
));
4103 dpi
.copy_templates
= alloca (dpi
.num_copy_templates
4104 * sizeof (*dpi
.copy_templates
));
4107 d_print_comp (&dpi
, options
, dc
);
4110 d_print_flush (&dpi
);
4112 return ! d_print_saw_error (&dpi
);
4115 /* Turn components into a human readable string. OPTIONS is the
4116 options bits passed to the demangler. DC is the tree to print.
4117 ESTIMATE is a guess at the length of the result. This returns a
4118 string allocated by malloc, or NULL on error. On success, this
4119 sets *PALC to the size of the allocated buffer. On failure, this
4120 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
4123 CP_STATIC_IF_GLIBCPP_V3
4125 cplus_demangle_print (int options
, const struct demangle_component
*dc
,
4126 int estimate
, size_t *palc
)
4128 struct d_growable_string dgs
;
4130 d_growable_string_init (&dgs
, estimate
);
4132 if (! cplus_demangle_print_callback (options
, dc
,
4133 d_growable_string_callback_adapter
,
4141 *palc
= dgs
.allocation_failure
? 1 : dgs
.alc
;
4145 /* Returns the I'th element of the template arglist ARGS, or NULL on
4148 static struct demangle_component
*
4149 d_index_template_argument (struct demangle_component
*args
, int i
)
4151 struct demangle_component
*a
;
4157 if (a
->type
!= DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
4163 if (i
!= 0 || a
== NULL
)
4169 /* Returns the template argument from the current context indicated by DC,
4170 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
4172 static struct demangle_component
*
4173 d_lookup_template_argument (struct d_print_info
*dpi
,
4174 const struct demangle_component
*dc
)
4176 if (dpi
->templates
== NULL
)
4178 d_print_error (dpi
);
4182 return d_index_template_argument
4183 (d_right (dpi
->templates
->template_decl
),
4184 dc
->u
.s_number
.number
);
4187 /* Returns a template argument pack used in DC (any will do), or NULL. */
4189 static struct demangle_component
*
4190 d_find_pack (struct d_print_info
*dpi
,
4191 const struct demangle_component
*dc
)
4193 struct demangle_component
*a
;
4199 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
4200 a
= d_lookup_template_argument (dpi
, dc
);
4201 if (a
&& a
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
4205 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
4208 case DEMANGLE_COMPONENT_LAMBDA
:
4209 case DEMANGLE_COMPONENT_NAME
:
4210 case DEMANGLE_COMPONENT_TAGGED_NAME
:
4211 case DEMANGLE_COMPONENT_OPERATOR
:
4212 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
4213 case DEMANGLE_COMPONENT_SUB_STD
:
4214 case DEMANGLE_COMPONENT_CHARACTER
:
4215 case DEMANGLE_COMPONENT_FUNCTION_PARAM
:
4216 case DEMANGLE_COMPONENT_UNNAMED_TYPE
:
4217 case DEMANGLE_COMPONENT_FIXED_TYPE
:
4218 case DEMANGLE_COMPONENT_DEFAULT_ARG
:
4219 case DEMANGLE_COMPONENT_NUMBER
:
4222 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
4223 return d_find_pack (dpi
, dc
->u
.s_extended_operator
.name
);
4224 case DEMANGLE_COMPONENT_CTOR
:
4225 return d_find_pack (dpi
, dc
->u
.s_ctor
.name
);
4226 case DEMANGLE_COMPONENT_DTOR
:
4227 return d_find_pack (dpi
, dc
->u
.s_dtor
.name
);
4230 a
= d_find_pack (dpi
, d_left (dc
));
4233 return d_find_pack (dpi
, d_right (dc
));
4237 /* Returns the length of the template argument pack DC. */
4240 d_pack_length (const struct demangle_component
*dc
)
4243 while (dc
&& dc
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
4244 && d_left (dc
) != NULL
)
4252 /* DC is a component of a mangled expression. Print it, wrapped in parens
4256 d_print_subexpr (struct d_print_info
*dpi
, int options
,
4257 const struct demangle_component
*dc
)
4260 if (dc
->type
== DEMANGLE_COMPONENT_NAME
4261 || dc
->type
== DEMANGLE_COMPONENT_QUAL_NAME
4262 || dc
->type
== DEMANGLE_COMPONENT_INITIALIZER_LIST
4263 || dc
->type
== DEMANGLE_COMPONENT_FUNCTION_PARAM
)
4266 d_append_char (dpi
, '(');
4267 d_print_comp (dpi
, options
, dc
);
4269 d_append_char (dpi
, ')');
4272 /* Save the current scope. */
4275 d_save_scope (struct d_print_info
*dpi
,
4276 const struct demangle_component
*container
)
4278 struct d_saved_scope
*scope
;
4279 struct d_print_template
*src
, **link
;
4281 if (dpi
->next_saved_scope
>= dpi
->num_saved_scopes
)
4283 d_print_error (dpi
);
4286 scope
= &dpi
->saved_scopes
[dpi
->next_saved_scope
];
4287 dpi
->next_saved_scope
++;
4289 scope
->container
= container
;
4290 link
= &scope
->templates
;
4292 for (src
= dpi
->templates
; src
!= NULL
; src
= src
->next
)
4294 struct d_print_template
*dst
;
4296 if (dpi
->next_copy_template
>= dpi
->num_copy_templates
)
4298 d_print_error (dpi
);
4301 dst
= &dpi
->copy_templates
[dpi
->next_copy_template
];
4302 dpi
->next_copy_template
++;
4304 dst
->template_decl
= src
->template_decl
;
4312 /* Attempt to locate a previously saved scope. Returns NULL if no
4313 corresponding saved scope was found. */
4315 static struct d_saved_scope
*
4316 d_get_saved_scope (struct d_print_info
*dpi
,
4317 const struct demangle_component
*container
)
4321 for (i
= 0; i
< dpi
->next_saved_scope
; i
++)
4322 if (dpi
->saved_scopes
[i
].container
== container
)
4323 return &dpi
->saved_scopes
[i
];
4328 /* Subroutine to handle components. */
4331 d_print_comp_inner (struct d_print_info
*dpi
, int options
,
4332 const struct demangle_component
*dc
)
4334 /* Magic variable to let reference smashing skip over the next modifier
4335 without needing to modify *dc. */
4336 const struct demangle_component
*mod_inner
= NULL
;
4338 /* Variable used to store the current templates while a previously
4339 captured scope is used. */
4340 struct d_print_template
*saved_templates
;
4342 /* Nonzero if templates have been stored in the above variable. */
4343 int need_template_restore
= 0;
4347 d_print_error (dpi
);
4350 if (d_print_saw_error (dpi
))
4355 case DEMANGLE_COMPONENT_NAME
:
4356 if ((options
& DMGL_JAVA
) == 0)
4357 d_append_buffer (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
4359 d_print_java_identifier (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
4362 case DEMANGLE_COMPONENT_TAGGED_NAME
:
4363 d_print_comp (dpi
, options
, d_left (dc
));
4364 d_append_string (dpi
, "[abi:");
4365 d_print_comp (dpi
, options
, d_right (dc
));
4366 d_append_char (dpi
, ']');
4369 case DEMANGLE_COMPONENT_QUAL_NAME
:
4370 case DEMANGLE_COMPONENT_LOCAL_NAME
:
4371 d_print_comp (dpi
, options
, d_left (dc
));
4372 if ((options
& DMGL_JAVA
) == 0)
4373 d_append_string (dpi
, "::");
4375 d_append_char (dpi
, '.');
4377 struct demangle_component
*local_name
= d_right (dc
);
4378 if (local_name
->type
== DEMANGLE_COMPONENT_DEFAULT_ARG
)
4380 d_append_string (dpi
, "{default arg#");
4381 d_append_num (dpi
, local_name
->u
.s_unary_num
.num
+ 1);
4382 d_append_string (dpi
, "}::");
4383 local_name
= local_name
->u
.s_unary_num
.sub
;
4385 d_print_comp (dpi
, options
, local_name
);
4389 case DEMANGLE_COMPONENT_TYPED_NAME
:
4391 struct d_print_mod
*hold_modifiers
;
4392 struct demangle_component
*typed_name
;
4393 struct d_print_mod adpm
[4];
4395 struct d_print_template dpt
;
4397 /* Pass the name down to the type so that it can be printed in
4398 the right place for the type. We also have to pass down
4399 any CV-qualifiers, which apply to the this parameter. */
4400 hold_modifiers
= dpi
->modifiers
;
4403 typed_name
= d_left (dc
);
4404 while (typed_name
!= NULL
)
4406 if (i
>= sizeof adpm
/ sizeof adpm
[0])
4408 d_print_error (dpi
);
4412 adpm
[i
].next
= dpi
->modifiers
;
4413 dpi
->modifiers
= &adpm
[i
];
4414 adpm
[i
].mod
= typed_name
;
4415 adpm
[i
].printed
= 0;
4416 adpm
[i
].templates
= dpi
->templates
;
4419 if (typed_name
->type
!= DEMANGLE_COMPONENT_RESTRICT_THIS
4420 && typed_name
->type
!= DEMANGLE_COMPONENT_VOLATILE_THIS
4421 && typed_name
->type
!= DEMANGLE_COMPONENT_CONST_THIS
4422 && typed_name
->type
!= DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
4423 && typed_name
->type
!= DEMANGLE_COMPONENT_REFERENCE_THIS
)
4426 typed_name
= d_left (typed_name
);
4429 if (typed_name
== NULL
)
4431 d_print_error (dpi
);
4435 /* If typed_name is a template, then it applies to the
4436 function type as well. */
4437 if (typed_name
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
4439 dpt
.next
= dpi
->templates
;
4440 dpi
->templates
= &dpt
;
4441 dpt
.template_decl
= typed_name
;
4444 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
4445 there may be CV-qualifiers on its right argument which
4446 really apply here; this happens when parsing a class which
4447 is local to a function. */
4448 if (typed_name
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
4450 struct demangle_component
*local_name
;
4452 local_name
= d_right (typed_name
);
4453 if (local_name
->type
== DEMANGLE_COMPONENT_DEFAULT_ARG
)
4454 local_name
= local_name
->u
.s_unary_num
.sub
;
4455 if (local_name
== NULL
)
4457 d_print_error (dpi
);
4460 while (local_name
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
4461 || local_name
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
4462 || local_name
->type
== DEMANGLE_COMPONENT_CONST_THIS
4463 || local_name
->type
== DEMANGLE_COMPONENT_REFERENCE_THIS
4464 || (local_name
->type
4465 == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
))
4467 if (i
>= sizeof adpm
/ sizeof adpm
[0])
4469 d_print_error (dpi
);
4473 adpm
[i
] = adpm
[i
- 1];
4474 adpm
[i
].next
= &adpm
[i
- 1];
4475 dpi
->modifiers
= &adpm
[i
];
4477 adpm
[i
- 1].mod
= local_name
;
4478 adpm
[i
- 1].printed
= 0;
4479 adpm
[i
- 1].templates
= dpi
->templates
;
4482 local_name
= d_left (local_name
);
4486 d_print_comp (dpi
, options
, d_right (dc
));
4488 if (typed_name
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
4489 dpi
->templates
= dpt
.next
;
4491 /* If the modifiers didn't get printed by the type, print them
4496 if (! adpm
[i
].printed
)
4498 d_append_char (dpi
, ' ');
4499 d_print_mod (dpi
, options
, adpm
[i
].mod
);
4503 dpi
->modifiers
= hold_modifiers
;
4508 case DEMANGLE_COMPONENT_TEMPLATE
:
4510 struct d_print_mod
*hold_dpm
;
4511 struct demangle_component
*dcl
;
4512 const struct demangle_component
*hold_current
;
4514 /* This template may need to be referenced by a cast operator
4515 contained in its subtree. */
4516 hold_current
= dpi
->current_template
;
4517 dpi
->current_template
= dc
;
4519 /* Don't push modifiers into a template definition. Doing so
4520 could give the wrong definition for a template argument.
4521 Instead, treat the template essentially as a name. */
4523 hold_dpm
= dpi
->modifiers
;
4524 dpi
->modifiers
= NULL
;
4528 if ((options
& DMGL_JAVA
) != 0
4529 && dcl
->type
== DEMANGLE_COMPONENT_NAME
4530 && dcl
->u
.s_name
.len
== 6
4531 && strncmp (dcl
->u
.s_name
.s
, "JArray", 6) == 0)
4533 /* Special-case Java arrays, so that JArray<TYPE> appears
4534 instead as TYPE[]. */
4536 d_print_comp (dpi
, options
, d_right (dc
));
4537 d_append_string (dpi
, "[]");
4541 d_print_comp (dpi
, options
, dcl
);
4542 if (d_last_char (dpi
) == '<')
4543 d_append_char (dpi
, ' ');
4544 d_append_char (dpi
, '<');
4545 d_print_comp (dpi
, options
, d_right (dc
));
4546 /* Avoid generating two consecutive '>' characters, to avoid
4547 the C++ syntactic ambiguity. */
4548 if (d_last_char (dpi
) == '>')
4549 d_append_char (dpi
, ' ');
4550 d_append_char (dpi
, '>');
4553 dpi
->modifiers
= hold_dpm
;
4554 dpi
->current_template
= hold_current
;
4559 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
4561 struct d_print_template
*hold_dpt
;
4562 struct demangle_component
*a
= d_lookup_template_argument (dpi
, dc
);
4564 if (a
&& a
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
4565 a
= d_index_template_argument (a
, dpi
->pack_index
);
4569 d_print_error (dpi
);
4573 /* While processing this parameter, we need to pop the list of
4574 templates. This is because the template parameter may
4575 itself be a reference to a parameter of an outer
4578 hold_dpt
= dpi
->templates
;
4579 dpi
->templates
= hold_dpt
->next
;
4581 d_print_comp (dpi
, options
, a
);
4583 dpi
->templates
= hold_dpt
;
4588 case DEMANGLE_COMPONENT_CTOR
:
4589 d_print_comp (dpi
, options
, dc
->u
.s_ctor
.name
);
4592 case DEMANGLE_COMPONENT_DTOR
:
4593 d_append_char (dpi
, '~');
4594 d_print_comp (dpi
, options
, dc
->u
.s_dtor
.name
);
4597 case DEMANGLE_COMPONENT_VTABLE
:
4598 d_append_string (dpi
, "vtable for ");
4599 d_print_comp (dpi
, options
, d_left (dc
));
4602 case DEMANGLE_COMPONENT_VTT
:
4603 d_append_string (dpi
, "VTT for ");
4604 d_print_comp (dpi
, options
, d_left (dc
));
4607 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
4608 d_append_string (dpi
, "construction vtable for ");
4609 d_print_comp (dpi
, options
, d_left (dc
));
4610 d_append_string (dpi
, "-in-");
4611 d_print_comp (dpi
, options
, d_right (dc
));
4614 case DEMANGLE_COMPONENT_TYPEINFO
:
4615 d_append_string (dpi
, "typeinfo for ");
4616 d_print_comp (dpi
, options
, d_left (dc
));
4619 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
4620 d_append_string (dpi
, "typeinfo name for ");
4621 d_print_comp (dpi
, options
, d_left (dc
));
4624 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
4625 d_append_string (dpi
, "typeinfo fn for ");
4626 d_print_comp (dpi
, options
, d_left (dc
));
4629 case DEMANGLE_COMPONENT_THUNK
:
4630 d_append_string (dpi
, "non-virtual thunk to ");
4631 d_print_comp (dpi
, options
, d_left (dc
));
4634 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
4635 d_append_string (dpi
, "virtual thunk to ");
4636 d_print_comp (dpi
, options
, d_left (dc
));
4639 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
4640 d_append_string (dpi
, "covariant return thunk to ");
4641 d_print_comp (dpi
, options
, d_left (dc
));
4644 case DEMANGLE_COMPONENT_JAVA_CLASS
:
4645 d_append_string (dpi
, "java Class for ");
4646 d_print_comp (dpi
, options
, d_left (dc
));
4649 case DEMANGLE_COMPONENT_GUARD
:
4650 d_append_string (dpi
, "guard variable for ");
4651 d_print_comp (dpi
, options
, d_left (dc
));
4654 case DEMANGLE_COMPONENT_TLS_INIT
:
4655 d_append_string (dpi
, "TLS init function for ");
4656 d_print_comp (dpi
, options
, d_left (dc
));
4659 case DEMANGLE_COMPONENT_TLS_WRAPPER
:
4660 d_append_string (dpi
, "TLS wrapper function for ");
4661 d_print_comp (dpi
, options
, d_left (dc
));
4664 case DEMANGLE_COMPONENT_REFTEMP
:
4665 d_append_string (dpi
, "reference temporary #");
4666 d_print_comp (dpi
, options
, d_right (dc
));
4667 d_append_string (dpi
, " for ");
4668 d_print_comp (dpi
, options
, d_left (dc
));
4671 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
4672 d_append_string (dpi
, "hidden alias for ");
4673 d_print_comp (dpi
, options
, d_left (dc
));
4676 case DEMANGLE_COMPONENT_TRANSACTION_CLONE
:
4677 d_append_string (dpi
, "transaction clone for ");
4678 d_print_comp (dpi
, options
, d_left (dc
));
4681 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
:
4682 d_append_string (dpi
, "non-transaction clone for ");
4683 d_print_comp (dpi
, options
, d_left (dc
));
4686 case DEMANGLE_COMPONENT_SUB_STD
:
4687 d_append_buffer (dpi
, dc
->u
.s_string
.string
, dc
->u
.s_string
.len
);
4690 case DEMANGLE_COMPONENT_RESTRICT
:
4691 case DEMANGLE_COMPONENT_VOLATILE
:
4692 case DEMANGLE_COMPONENT_CONST
:
4694 struct d_print_mod
*pdpm
;
4696 /* When printing arrays, it's possible to have cases where the
4697 same CV-qualifier gets pushed on the stack multiple times.
4698 We only need to print it once. */
4700 for (pdpm
= dpi
->modifiers
; pdpm
!= NULL
; pdpm
= pdpm
->next
)
4702 if (! pdpm
->printed
)
4704 if (pdpm
->mod
->type
!= DEMANGLE_COMPONENT_RESTRICT
4705 && pdpm
->mod
->type
!= DEMANGLE_COMPONENT_VOLATILE
4706 && pdpm
->mod
->type
!= DEMANGLE_COMPONENT_CONST
)
4708 if (pdpm
->mod
->type
== dc
->type
)
4710 d_print_comp (dpi
, options
, d_left (dc
));
4718 case DEMANGLE_COMPONENT_REFERENCE
:
4719 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
4721 /* Handle reference smashing: & + && = &. */
4722 const struct demangle_component
*sub
= d_left (dc
);
4723 if (sub
->type
== DEMANGLE_COMPONENT_TEMPLATE_PARAM
)
4725 struct d_saved_scope
*scope
= d_get_saved_scope (dpi
, sub
);
4726 struct demangle_component
*a
;
4730 /* This is the first time SUB has been traversed.
4731 We need to capture the current templates so
4732 they can be restored if SUB is reentered as a
4734 d_save_scope (dpi
, sub
);
4735 if (d_print_saw_error (dpi
))
4740 const struct d_component_stack
*dcse
;
4741 int found_self_or_parent
= 0;
4743 /* This traversal is reentering SUB as a substition.
4744 If we are not beneath SUB or DC in the tree then we
4745 need to restore SUB's template stack temporarily. */
4746 for (dcse
= dpi
->component_stack
; dcse
!= NULL
;
4747 dcse
= dcse
->parent
)
4751 && dcse
!= dpi
->component_stack
))
4753 found_self_or_parent
= 1;
4758 if (!found_self_or_parent
)
4760 saved_templates
= dpi
->templates
;
4761 dpi
->templates
= scope
->templates
;
4762 need_template_restore
= 1;
4766 a
= d_lookup_template_argument (dpi
, sub
);
4767 if (a
&& a
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
4768 a
= d_index_template_argument (a
, dpi
->pack_index
);
4772 if (need_template_restore
)
4773 dpi
->templates
= saved_templates
;
4775 d_print_error (dpi
);
4782 if (sub
->type
== DEMANGLE_COMPONENT_REFERENCE
4783 || sub
->type
== dc
->type
)
4785 else if (sub
->type
== DEMANGLE_COMPONENT_RVALUE_REFERENCE
)
4786 mod_inner
= d_left (sub
);
4790 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
4791 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
4792 case DEMANGLE_COMPONENT_CONST_THIS
:
4793 case DEMANGLE_COMPONENT_REFERENCE_THIS
:
4794 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
:
4795 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
4796 case DEMANGLE_COMPONENT_POINTER
:
4797 case DEMANGLE_COMPONENT_COMPLEX
:
4798 case DEMANGLE_COMPONENT_IMAGINARY
:
4801 /* We keep a list of modifiers on the stack. */
4802 struct d_print_mod dpm
;
4804 dpm
.next
= dpi
->modifiers
;
4805 dpi
->modifiers
= &dpm
;
4808 dpm
.templates
= dpi
->templates
;
4811 mod_inner
= d_left (dc
);
4813 d_print_comp (dpi
, options
, mod_inner
);
4815 /* If the modifier didn't get printed by the type, print it
4818 d_print_mod (dpi
, options
, dc
);
4820 dpi
->modifiers
= dpm
.next
;
4822 if (need_template_restore
)
4823 dpi
->templates
= saved_templates
;
4828 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
4829 if ((options
& DMGL_JAVA
) == 0)
4830 d_append_buffer (dpi
, dc
->u
.s_builtin
.type
->name
,
4831 dc
->u
.s_builtin
.type
->len
);
4833 d_append_buffer (dpi
, dc
->u
.s_builtin
.type
->java_name
,
4834 dc
->u
.s_builtin
.type
->java_len
);
4837 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
4838 d_print_comp (dpi
, options
, d_left (dc
));
4841 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
4843 if ((options
& DMGL_RET_POSTFIX
) != 0)
4844 d_print_function_type (dpi
,
4845 options
& ~(DMGL_RET_POSTFIX
| DMGL_RET_DROP
),
4846 dc
, dpi
->modifiers
);
4848 /* Print return type if present */
4849 if (d_left (dc
) != NULL
&& (options
& DMGL_RET_POSTFIX
) != 0)
4850 d_print_comp (dpi
, options
& ~(DMGL_RET_POSTFIX
| DMGL_RET_DROP
),
4852 else if (d_left (dc
) != NULL
&& (options
& DMGL_RET_DROP
) == 0)
4854 struct d_print_mod dpm
;
4856 /* We must pass this type down as a modifier in order to
4857 print it in the right location. */
4858 dpm
.next
= dpi
->modifiers
;
4859 dpi
->modifiers
= &dpm
;
4862 dpm
.templates
= dpi
->templates
;
4864 d_print_comp (dpi
, options
& ~(DMGL_RET_POSTFIX
| DMGL_RET_DROP
),
4867 dpi
->modifiers
= dpm
.next
;
4872 /* In standard prefix notation, there is a space between the
4873 return type and the function signature. */
4874 if ((options
& DMGL_RET_POSTFIX
) == 0)
4875 d_append_char (dpi
, ' ');
4878 if ((options
& DMGL_RET_POSTFIX
) == 0)
4879 d_print_function_type (dpi
,
4880 options
& ~(DMGL_RET_POSTFIX
| DMGL_RET_DROP
),
4881 dc
, dpi
->modifiers
);
4886 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
4888 struct d_print_mod
*hold_modifiers
;
4889 struct d_print_mod adpm
[4];
4891 struct d_print_mod
*pdpm
;
4893 /* We must pass this type down as a modifier in order to print
4894 multi-dimensional arrays correctly. If the array itself is
4895 CV-qualified, we act as though the element type were
4896 CV-qualified. We do this by copying the modifiers down
4897 rather than fiddling pointers, so that we don't wind up
4898 with a d_print_mod higher on the stack pointing into our
4899 stack frame after we return. */
4901 hold_modifiers
= dpi
->modifiers
;
4903 adpm
[0].next
= hold_modifiers
;
4904 dpi
->modifiers
= &adpm
[0];
4906 adpm
[0].printed
= 0;
4907 adpm
[0].templates
= dpi
->templates
;
4910 pdpm
= hold_modifiers
;
4912 && (pdpm
->mod
->type
== DEMANGLE_COMPONENT_RESTRICT
4913 || pdpm
->mod
->type
== DEMANGLE_COMPONENT_VOLATILE
4914 || pdpm
->mod
->type
== DEMANGLE_COMPONENT_CONST
))
4916 if (! pdpm
->printed
)
4918 if (i
>= sizeof adpm
/ sizeof adpm
[0])
4920 d_print_error (dpi
);
4925 adpm
[i
].next
= dpi
->modifiers
;
4926 dpi
->modifiers
= &adpm
[i
];
4934 d_print_comp (dpi
, options
, d_right (dc
));
4936 dpi
->modifiers
= hold_modifiers
;
4938 if (adpm
[0].printed
)
4944 d_print_mod (dpi
, options
, adpm
[i
].mod
);
4947 d_print_array_type (dpi
, options
, dc
, dpi
->modifiers
);
4952 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
4953 case DEMANGLE_COMPONENT_VECTOR_TYPE
:
4955 struct d_print_mod dpm
;
4957 dpm
.next
= dpi
->modifiers
;
4958 dpi
->modifiers
= &dpm
;
4961 dpm
.templates
= dpi
->templates
;
4963 d_print_comp (dpi
, options
, d_right (dc
));
4965 /* If the modifier didn't get printed by the type, print it
4968 d_print_mod (dpi
, options
, dc
);
4970 dpi
->modifiers
= dpm
.next
;
4975 case DEMANGLE_COMPONENT_FIXED_TYPE
:
4976 if (dc
->u
.s_fixed
.sat
)
4977 d_append_string (dpi
, "_Sat ");
4978 /* Don't print "int _Accum". */
4979 if (dc
->u
.s_fixed
.length
->u
.s_builtin
.type
4980 != &cplus_demangle_builtin_types
['i'-'a'])
4982 d_print_comp (dpi
, options
, dc
->u
.s_fixed
.length
);
4983 d_append_char (dpi
, ' ');
4985 if (dc
->u
.s_fixed
.accum
)
4986 d_append_string (dpi
, "_Accum");
4988 d_append_string (dpi
, "_Fract");
4991 case DEMANGLE_COMPONENT_ARGLIST
:
4992 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
4993 if (d_left (dc
) != NULL
)
4994 d_print_comp (dpi
, options
, d_left (dc
));
4995 if (d_right (dc
) != NULL
)
4998 unsigned long int flush_count
;
4999 /* Make sure ", " isn't flushed by d_append_string, otherwise
5000 dpi->len -= 2 wouldn't work. */
5001 if (dpi
->len
>= sizeof (dpi
->buf
) - 2)
5002 d_print_flush (dpi
);
5003 d_append_string (dpi
, ", ");
5005 flush_count
= dpi
->flush_count
;
5006 d_print_comp (dpi
, options
, d_right (dc
));
5007 /* If that didn't print anything (which can happen with empty
5008 template argument packs), remove the comma and space. */
5009 if (dpi
->flush_count
== flush_count
&& dpi
->len
== len
)
5014 case DEMANGLE_COMPONENT_INITIALIZER_LIST
:
5016 struct demangle_component
*type
= d_left (dc
);
5017 struct demangle_component
*list
= d_right (dc
);
5020 d_print_comp (dpi
, options
, type
);
5021 d_append_char (dpi
, '{');
5022 d_print_comp (dpi
, options
, list
);
5023 d_append_char (dpi
, '}');
5027 case DEMANGLE_COMPONENT_OPERATOR
:
5029 const struct demangle_operator_info
*op
= dc
->u
.s_operator
.op
;
5032 d_append_string (dpi
, "operator");
5033 /* Add a space before new/delete. */
5034 if (IS_LOWER (op
->name
[0]))
5035 d_append_char (dpi
, ' ');
5036 /* Omit a trailing space. */
5037 if (op
->name
[len
-1] == ' ')
5039 d_append_buffer (dpi
, op
->name
, len
);
5043 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
5044 d_append_string (dpi
, "operator ");
5045 d_print_comp (dpi
, options
, dc
->u
.s_extended_operator
.name
);
5048 case DEMANGLE_COMPONENT_CAST
:
5049 d_append_string (dpi
, "operator ");
5050 d_print_cast (dpi
, options
, dc
);
5053 case DEMANGLE_COMPONENT_NULLARY
:
5054 d_print_expr_op (dpi
, options
, d_left (dc
));
5057 case DEMANGLE_COMPONENT_UNARY
:
5059 struct demangle_component
*op
= d_left (dc
);
5060 struct demangle_component
*operand
= d_right (dc
);
5061 const char *code
= NULL
;
5063 if (op
->type
== DEMANGLE_COMPONENT_OPERATOR
)
5065 code
= op
->u
.s_operator
.op
->code
;
5066 if (!strcmp (code
, "ad"))
5068 /* Don't print the argument list for the address of a
5070 if (operand
->type
== DEMANGLE_COMPONENT_TYPED_NAME
5071 && d_left (operand
)->type
== DEMANGLE_COMPONENT_QUAL_NAME
5072 && d_right (operand
)->type
== DEMANGLE_COMPONENT_FUNCTION_TYPE
)
5073 operand
= d_left (operand
);
5075 if (operand
->type
== DEMANGLE_COMPONENT_BINARY_ARGS
)
5077 /* This indicates a suffix operator. */
5078 operand
= d_left (operand
);
5079 d_print_subexpr (dpi
, options
, operand
);
5080 d_print_expr_op (dpi
, options
, op
);
5085 if (op
->type
!= DEMANGLE_COMPONENT_CAST
)
5086 d_print_expr_op (dpi
, options
, op
);
5089 d_append_char (dpi
, '(');
5090 d_print_cast (dpi
, options
, op
);
5091 d_append_char (dpi
, ')');
5093 if (code
&& !strcmp (code
, "gs"))
5094 /* Avoid parens after '::'. */
5095 d_print_comp (dpi
, options
, operand
);
5096 else if (code
&& !strcmp (code
, "st"))
5097 /* Always print parens for sizeof (type). */
5099 d_append_char (dpi
, '(');
5100 d_print_comp (dpi
, options
, operand
);
5101 d_append_char (dpi
, ')');
5104 d_print_subexpr (dpi
, options
, operand
);
5108 case DEMANGLE_COMPONENT_BINARY
:
5109 if (d_right (dc
)->type
!= DEMANGLE_COMPONENT_BINARY_ARGS
)
5111 d_print_error (dpi
);
5115 if (op_is_new_cast (d_left (dc
)))
5117 d_print_expr_op (dpi
, options
, d_left (dc
));
5118 d_append_char (dpi
, '<');
5119 d_print_comp (dpi
, options
, d_left (d_right (dc
)));
5120 d_append_string (dpi
, ">(");
5121 d_print_comp (dpi
, options
, d_right (d_right (dc
)));
5122 d_append_char (dpi
, ')');
5126 /* We wrap an expression which uses the greater-than operator in
5127 an extra layer of parens so that it does not get confused
5128 with the '>' which ends the template parameters. */
5129 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_OPERATOR
5130 && d_left (dc
)->u
.s_operator
.op
->len
== 1
5131 && d_left (dc
)->u
.s_operator
.op
->name
[0] == '>')
5132 d_append_char (dpi
, '(');
5134 if (strcmp (d_left (dc
)->u
.s_operator
.op
->code
, "cl") == 0
5135 && d_left (d_right (dc
))->type
== DEMANGLE_COMPONENT_TYPED_NAME
)
5137 /* Function call used in an expression should not have printed types
5138 of the function arguments. Values of the function arguments still
5139 get printed below. */
5141 const struct demangle_component
*func
= d_left (d_right (dc
));
5143 if (d_right (func
)->type
!= DEMANGLE_COMPONENT_FUNCTION_TYPE
)
5144 d_print_error (dpi
);
5145 d_print_subexpr (dpi
, options
, d_left (func
));
5148 d_print_subexpr (dpi
, options
, d_left (d_right (dc
)));
5149 if (strcmp (d_left (dc
)->u
.s_operator
.op
->code
, "ix") == 0)
5151 d_append_char (dpi
, '[');
5152 d_print_comp (dpi
, options
, d_right (d_right (dc
)));
5153 d_append_char (dpi
, ']');
5157 if (strcmp (d_left (dc
)->u
.s_operator
.op
->code
, "cl") != 0)
5158 d_print_expr_op (dpi
, options
, d_left (dc
));
5159 d_print_subexpr (dpi
, options
, d_right (d_right (dc
)));
5162 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_OPERATOR
5163 && d_left (dc
)->u
.s_operator
.op
->len
== 1
5164 && d_left (dc
)->u
.s_operator
.op
->name
[0] == '>')
5165 d_append_char (dpi
, ')');
5169 case DEMANGLE_COMPONENT_BINARY_ARGS
:
5170 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
5171 d_print_error (dpi
);
5174 case DEMANGLE_COMPONENT_TRINARY
:
5175 if (d_right (dc
)->type
!= DEMANGLE_COMPONENT_TRINARY_ARG1
5176 || d_right (d_right (dc
))->type
!= DEMANGLE_COMPONENT_TRINARY_ARG2
)
5178 d_print_error (dpi
);
5182 struct demangle_component
*op
= d_left (dc
);
5183 struct demangle_component
*first
= d_left (d_right (dc
));
5184 struct demangle_component
*second
= d_left (d_right (d_right (dc
)));
5185 struct demangle_component
*third
= d_right (d_right (d_right (dc
)));
5187 if (!strcmp (op
->u
.s_operator
.op
->code
, "qu"))
5189 d_print_subexpr (dpi
, options
, first
);
5190 d_print_expr_op (dpi
, options
, op
);
5191 d_print_subexpr (dpi
, options
, second
);
5192 d_append_string (dpi
, " : ");
5193 d_print_subexpr (dpi
, options
, third
);
5197 d_append_string (dpi
, "new ");
5198 if (d_left (first
) != NULL
)
5200 d_print_subexpr (dpi
, options
, first
);
5201 d_append_char (dpi
, ' ');
5203 d_print_comp (dpi
, options
, second
);
5205 d_print_subexpr (dpi
, options
, third
);
5210 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
5211 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
5212 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
5213 d_print_error (dpi
);
5216 case DEMANGLE_COMPONENT_LITERAL
:
5217 case DEMANGLE_COMPONENT_LITERAL_NEG
:
5219 enum d_builtin_type_print tp
;
5221 /* For some builtin types, produce simpler output. */
5222 tp
= D_PRINT_DEFAULT
;
5223 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
)
5225 tp
= d_left (dc
)->u
.s_builtin
.type
->print
;
5229 case D_PRINT_UNSIGNED
:
5231 case D_PRINT_UNSIGNED_LONG
:
5232 case D_PRINT_LONG_LONG
:
5233 case D_PRINT_UNSIGNED_LONG_LONG
:
5234 if (d_right (dc
)->type
== DEMANGLE_COMPONENT_NAME
)
5236 if (dc
->type
== DEMANGLE_COMPONENT_LITERAL_NEG
)
5237 d_append_char (dpi
, '-');
5238 d_print_comp (dpi
, options
, d_right (dc
));
5243 case D_PRINT_UNSIGNED
:
5244 d_append_char (dpi
, 'u');
5247 d_append_char (dpi
, 'l');
5249 case D_PRINT_UNSIGNED_LONG
:
5250 d_append_string (dpi
, "ul");
5252 case D_PRINT_LONG_LONG
:
5253 d_append_string (dpi
, "ll");
5255 case D_PRINT_UNSIGNED_LONG_LONG
:
5256 d_append_string (dpi
, "ull");
5264 if (d_right (dc
)->type
== DEMANGLE_COMPONENT_NAME
5265 && d_right (dc
)->u
.s_name
.len
== 1
5266 && dc
->type
== DEMANGLE_COMPONENT_LITERAL
)
5268 switch (d_right (dc
)->u
.s_name
.s
[0])
5271 d_append_string (dpi
, "false");
5274 d_append_string (dpi
, "true");
5287 d_append_char (dpi
, '(');
5288 d_print_comp (dpi
, options
, d_left (dc
));
5289 d_append_char (dpi
, ')');
5290 if (dc
->type
== DEMANGLE_COMPONENT_LITERAL_NEG
)
5291 d_append_char (dpi
, '-');
5292 if (tp
== D_PRINT_FLOAT
)
5293 d_append_char (dpi
, '[');
5294 d_print_comp (dpi
, options
, d_right (dc
));
5295 if (tp
== D_PRINT_FLOAT
)
5296 d_append_char (dpi
, ']');
5300 case DEMANGLE_COMPONENT_NUMBER
:
5301 d_append_num (dpi
, dc
->u
.s_number
.number
);
5304 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
5305 d_append_string (dpi
, "java resource ");
5306 d_print_comp (dpi
, options
, d_left (dc
));
5309 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
5310 d_print_comp (dpi
, options
, d_left (dc
));
5311 d_print_comp (dpi
, options
, d_right (dc
));
5314 case DEMANGLE_COMPONENT_CHARACTER
:
5315 d_append_char (dpi
, dc
->u
.s_character
.character
);
5318 case DEMANGLE_COMPONENT_DECLTYPE
:
5319 d_append_string (dpi
, "decltype (");
5320 d_print_comp (dpi
, options
, d_left (dc
));
5321 d_append_char (dpi
, ')');
5324 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
5328 struct demangle_component
*a
= d_find_pack (dpi
, d_left (dc
));
5331 /* d_find_pack won't find anything if the only packs involved
5332 in this expansion are function parameter packs; in that
5333 case, just print the pattern and "...". */
5334 d_print_subexpr (dpi
, options
, d_left (dc
));
5335 d_append_string (dpi
, "...");
5339 len
= d_pack_length (a
);
5341 for (i
= 0; i
< len
; ++i
)
5343 dpi
->pack_index
= i
;
5344 d_print_comp (dpi
, options
, dc
);
5346 d_append_string (dpi
, ", ");
5351 case DEMANGLE_COMPONENT_FUNCTION_PARAM
:
5353 long num
= dc
->u
.s_number
.number
;
5355 d_append_string (dpi
, "this");
5358 d_append_string (dpi
, "{parm#");
5359 d_append_num (dpi
, num
);
5360 d_append_char (dpi
, '}');
5365 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
:
5366 d_append_string (dpi
, "global constructors keyed to ");
5367 d_print_comp (dpi
, options
, dc
->u
.s_binary
.left
);
5370 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS
:
5371 d_append_string (dpi
, "global destructors keyed to ");
5372 d_print_comp (dpi
, options
, dc
->u
.s_binary
.left
);
5375 case DEMANGLE_COMPONENT_LAMBDA
:
5376 d_append_string (dpi
, "{lambda(");
5377 d_print_comp (dpi
, options
, dc
->u
.s_unary_num
.sub
);
5378 d_append_string (dpi
, ")#");
5379 d_append_num (dpi
, dc
->u
.s_unary_num
.num
+ 1);
5380 d_append_char (dpi
, '}');
5383 case DEMANGLE_COMPONENT_UNNAMED_TYPE
:
5384 d_append_string (dpi
, "{unnamed type#");
5385 d_append_num (dpi
, dc
->u
.s_number
.number
+ 1);
5386 d_append_char (dpi
, '}');
5389 case DEMANGLE_COMPONENT_CLONE
:
5390 d_print_comp (dpi
, options
, d_left (dc
));
5391 d_append_string (dpi
, " [clone ");
5392 d_print_comp (dpi
, options
, d_right (dc
));
5393 d_append_char (dpi
, ']');
5397 d_print_error (dpi
);
5403 d_print_comp (struct d_print_info
*dpi
, int options
,
5404 const struct demangle_component
*dc
)
5406 struct d_component_stack self
;
5409 self
.parent
= dpi
->component_stack
;
5410 dpi
->component_stack
= &self
;
5412 d_print_comp_inner (dpi
, options
, dc
);
5414 dpi
->component_stack
= self
.parent
;
5417 /* Print a Java dentifier. For Java we try to handle encoded extended
5418 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
5419 so we don't it for C++. Characters are encoded as
5423 d_print_java_identifier (struct d_print_info
*dpi
, const char *name
, int len
)
5429 for (p
= name
; p
< end
; ++p
)
5440 for (q
= p
+ 3; q
< end
; ++q
)
5446 else if (*q
>= 'A' && *q
<= 'F')
5447 dig
= *q
- 'A' + 10;
5448 else if (*q
>= 'a' && *q
<= 'f')
5449 dig
= *q
- 'a' + 10;
5455 /* If the Unicode character is larger than 256, we don't try
5456 to deal with it here. FIXME. */
5457 if (q
< end
&& *q
== '_' && c
< 256)
5459 d_append_char (dpi
, c
);
5465 d_append_char (dpi
, *p
);
5469 /* Print a list of modifiers. SUFFIX is 1 if we are printing
5470 qualifiers on this after printing a function. */
5473 d_print_mod_list (struct d_print_info
*dpi
, int options
,
5474 struct d_print_mod
*mods
, int suffix
)
5476 struct d_print_template
*hold_dpt
;
5478 if (mods
== NULL
|| d_print_saw_error (dpi
))
5483 && (mods
->mod
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
5484 || mods
->mod
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
5485 || mods
->mod
->type
== DEMANGLE_COMPONENT_CONST_THIS
5486 || mods
->mod
->type
== DEMANGLE_COMPONENT_REFERENCE_THIS
5488 == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
))))
5490 d_print_mod_list (dpi
, options
, mods
->next
, suffix
);
5496 hold_dpt
= dpi
->templates
;
5497 dpi
->templates
= mods
->templates
;
5499 if (mods
->mod
->type
== DEMANGLE_COMPONENT_FUNCTION_TYPE
)
5501 d_print_function_type (dpi
, options
, mods
->mod
, mods
->next
);
5502 dpi
->templates
= hold_dpt
;
5505 else if (mods
->mod
->type
== DEMANGLE_COMPONENT_ARRAY_TYPE
)
5507 d_print_array_type (dpi
, options
, mods
->mod
, mods
->next
);
5508 dpi
->templates
= hold_dpt
;
5511 else if (mods
->mod
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
5513 struct d_print_mod
*hold_modifiers
;
5514 struct demangle_component
*dc
;
5516 /* When this is on the modifier stack, we have pulled any
5517 qualifiers off the right argument already. Otherwise, we
5518 print it as usual, but don't let the left argument see any
5521 hold_modifiers
= dpi
->modifiers
;
5522 dpi
->modifiers
= NULL
;
5523 d_print_comp (dpi
, options
, d_left (mods
->mod
));
5524 dpi
->modifiers
= hold_modifiers
;
5526 if ((options
& DMGL_JAVA
) == 0)
5527 d_append_string (dpi
, "::");
5529 d_append_char (dpi
, '.');
5531 dc
= d_right (mods
->mod
);
5533 if (dc
->type
== DEMANGLE_COMPONENT_DEFAULT_ARG
)
5535 d_append_string (dpi
, "{default arg#");
5536 d_append_num (dpi
, dc
->u
.s_unary_num
.num
+ 1);
5537 d_append_string (dpi
, "}::");
5538 dc
= dc
->u
.s_unary_num
.sub
;
5541 while (dc
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
5542 || dc
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
5543 || dc
->type
== DEMANGLE_COMPONENT_CONST_THIS
5544 || dc
->type
== DEMANGLE_COMPONENT_REFERENCE_THIS
5545 || dc
->type
== DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
)
5548 d_print_comp (dpi
, options
, dc
);
5550 dpi
->templates
= hold_dpt
;
5554 d_print_mod (dpi
, options
, mods
->mod
);
5556 dpi
->templates
= hold_dpt
;
5558 d_print_mod_list (dpi
, options
, mods
->next
, suffix
);
5561 /* Print a modifier. */
5564 d_print_mod (struct d_print_info
*dpi
, int options
,
5565 const struct demangle_component
*mod
)
5569 case DEMANGLE_COMPONENT_RESTRICT
:
5570 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
5571 d_append_string (dpi
, " restrict");
5573 case DEMANGLE_COMPONENT_VOLATILE
:
5574 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
5575 d_append_string (dpi
, " volatile");
5577 case DEMANGLE_COMPONENT_CONST
:
5578 case DEMANGLE_COMPONENT_CONST_THIS
:
5579 d_append_string (dpi
, " const");
5581 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
5582 d_append_char (dpi
, ' ');
5583 d_print_comp (dpi
, options
, d_right (mod
));
5585 case DEMANGLE_COMPONENT_POINTER
:
5586 /* There is no pointer symbol in Java. */
5587 if ((options
& DMGL_JAVA
) == 0)
5588 d_append_char (dpi
, '*');
5590 case DEMANGLE_COMPONENT_REFERENCE_THIS
:
5591 /* For the ref-qualifier, put a space before the &. */
5592 d_append_char (dpi
, ' ');
5593 case DEMANGLE_COMPONENT_REFERENCE
:
5594 d_append_char (dpi
, '&');
5596 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
:
5597 d_append_char (dpi
, ' ');
5598 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
5599 d_append_string (dpi
, "&&");
5601 case DEMANGLE_COMPONENT_COMPLEX
:
5602 d_append_string (dpi
, "complex ");
5604 case DEMANGLE_COMPONENT_IMAGINARY
:
5605 d_append_string (dpi
, "imaginary ");
5607 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
5608 if (d_last_char (dpi
) != '(')
5609 d_append_char (dpi
, ' ');
5610 d_print_comp (dpi
, options
, d_left (mod
));
5611 d_append_string (dpi
, "::*");
5613 case DEMANGLE_COMPONENT_TYPED_NAME
:
5614 d_print_comp (dpi
, options
, d_left (mod
));
5616 case DEMANGLE_COMPONENT_VECTOR_TYPE
:
5617 d_append_string (dpi
, " __vector(");
5618 d_print_comp (dpi
, options
, d_left (mod
));
5619 d_append_char (dpi
, ')');
5623 /* Otherwise, we have something that won't go back on the
5624 modifier stack, so we can just print it. */
5625 d_print_comp (dpi
, options
, mod
);
5630 /* Print a function type, except for the return type. */
5633 d_print_function_type (struct d_print_info
*dpi
, int options
,
5634 const struct demangle_component
*dc
,
5635 struct d_print_mod
*mods
)
5639 struct d_print_mod
*p
;
5640 struct d_print_mod
*hold_modifiers
;
5644 for (p
= mods
; p
!= NULL
; p
= p
->next
)
5649 switch (p
->mod
->type
)
5651 case DEMANGLE_COMPONENT_POINTER
:
5652 case DEMANGLE_COMPONENT_REFERENCE
:
5653 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
5656 case DEMANGLE_COMPONENT_RESTRICT
:
5657 case DEMANGLE_COMPONENT_VOLATILE
:
5658 case DEMANGLE_COMPONENT_CONST
:
5659 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
5660 case DEMANGLE_COMPONENT_COMPLEX
:
5661 case DEMANGLE_COMPONENT_IMAGINARY
:
5662 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
5666 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
5667 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
5668 case DEMANGLE_COMPONENT_CONST_THIS
:
5669 case DEMANGLE_COMPONENT_REFERENCE_THIS
:
5670 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
:
5683 if (d_last_char (dpi
) != '('
5684 && d_last_char (dpi
) != '*')
5687 if (need_space
&& d_last_char (dpi
) != ' ')
5688 d_append_char (dpi
, ' ');
5689 d_append_char (dpi
, '(');
5692 hold_modifiers
= dpi
->modifiers
;
5693 dpi
->modifiers
= NULL
;
5695 d_print_mod_list (dpi
, options
, mods
, 0);
5698 d_append_char (dpi
, ')');
5700 d_append_char (dpi
, '(');
5702 if (d_right (dc
) != NULL
)
5703 d_print_comp (dpi
, options
, d_right (dc
));
5705 d_append_char (dpi
, ')');
5707 d_print_mod_list (dpi
, options
, mods
, 1);
5709 dpi
->modifiers
= hold_modifiers
;
5712 /* Print an array type, except for the element type. */
5715 d_print_array_type (struct d_print_info
*dpi
, int options
,
5716 const struct demangle_component
*dc
,
5717 struct d_print_mod
*mods
)
5725 struct d_print_mod
*p
;
5728 for (p
= mods
; p
!= NULL
; p
= p
->next
)
5732 if (p
->mod
->type
== DEMANGLE_COMPONENT_ARRAY_TYPE
)
5747 d_append_string (dpi
, " (");
5749 d_print_mod_list (dpi
, options
, mods
, 0);
5752 d_append_char (dpi
, ')');
5756 d_append_char (dpi
, ' ');
5758 d_append_char (dpi
, '[');
5760 if (d_left (dc
) != NULL
)
5761 d_print_comp (dpi
, options
, d_left (dc
));
5763 d_append_char (dpi
, ']');
5766 /* Print an operator in an expression. */
5769 d_print_expr_op (struct d_print_info
*dpi
, int options
,
5770 const struct demangle_component
*dc
)
5772 if (dc
->type
== DEMANGLE_COMPONENT_OPERATOR
)
5773 d_append_buffer (dpi
, dc
->u
.s_operator
.op
->name
,
5774 dc
->u
.s_operator
.op
->len
);
5776 d_print_comp (dpi
, options
, dc
);
5782 d_print_cast (struct d_print_info
*dpi
, int options
,
5783 const struct demangle_component
*dc
)
5785 struct d_print_template dpt
;
5787 /* For a cast operator, we need the template parameters from
5788 the enclosing template in scope for processing the type. */
5789 if (dpi
->current_template
!= NULL
)
5791 dpt
.next
= dpi
->templates
;
5792 dpi
->templates
= &dpt
;
5793 dpt
.template_decl
= dpi
->current_template
;
5796 if (d_left (dc
)->type
!= DEMANGLE_COMPONENT_TEMPLATE
)
5798 d_print_comp (dpi
, options
, d_left (dc
));
5799 if (dpi
->current_template
!= NULL
)
5800 dpi
->templates
= dpt
.next
;
5804 d_print_comp (dpi
, options
, d_left (d_left (dc
)));
5806 /* For a templated cast operator, we need to remove the template
5807 parameters from scope after printing the operator name,
5808 so we need to handle the template printing here. */
5809 if (dpi
->current_template
!= NULL
)
5810 dpi
->templates
= dpt
.next
;
5812 if (d_last_char (dpi
) == '<')
5813 d_append_char (dpi
, ' ');
5814 d_append_char (dpi
, '<');
5815 d_print_comp (dpi
, options
, d_right (d_left (dc
)));
5816 /* Avoid generating two consecutive '>' characters, to avoid
5817 the C++ syntactic ambiguity. */
5818 if (d_last_char (dpi
) == '>')
5819 d_append_char (dpi
, ' ');
5820 d_append_char (dpi
, '>');
5824 /* Initialize the information structure we use to pass around
5827 CP_STATIC_IF_GLIBCPP_V3
5829 cplus_demangle_init_info (const char *mangled
, int options
, size_t len
,
5833 di
->send
= mangled
+ len
;
5834 di
->options
= options
;
5838 /* We can not need more components than twice the number of chars in
5839 the mangled string. Most components correspond directly to
5840 chars, but the ARGLIST types are exceptions. */
5841 di
->num_comps
= 2 * len
;
5844 /* Similarly, we can not need more substitutions than there are
5845 chars in the mangled string. */
5850 di
->last_name
= NULL
;
5853 di
->is_expression
= 0;
5854 di
->is_conversion
= 0;
5857 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
5858 mangled name, return strings in repeated callback giving the demangled
5859 name. OPTIONS is the usual libiberty demangler options. On success,
5860 this returns 1. On failure, returns 0. */
5863 d_demangle_callback (const char *mangled
, int options
,
5864 demangle_callbackref callback
, void *opaque
)
5875 struct demangle_component
*dc
;
5878 if (mangled
[0] == '_' && mangled
[1] == 'Z')
5880 else if (strncmp (mangled
, "_GLOBAL_", 8) == 0
5881 && (mangled
[8] == '.' || mangled
[8] == '_' || mangled
[8] == '$')
5882 && (mangled
[9] == 'D' || mangled
[9] == 'I')
5883 && mangled
[10] == '_')
5884 type
= mangled
[9] == 'I' ? DCT_GLOBAL_CTORS
: DCT_GLOBAL_DTORS
;
5887 if ((options
& DMGL_TYPES
) == 0)
5892 cplus_demangle_init_info (mangled
, options
, strlen (mangled
), &di
);
5895 #ifdef CP_DYNAMIC_ARRAYS
5896 __extension__
struct demangle_component comps
[di
.num_comps
];
5897 __extension__
struct demangle_component
*subs
[di
.num_subs
];
5902 di
.comps
= alloca (di
.num_comps
* sizeof (*di
.comps
));
5903 di
.subs
= alloca (di
.num_subs
* sizeof (*di
.subs
));
5909 dc
= cplus_demangle_type (&di
);
5912 dc
= cplus_demangle_mangled_name (&di
, 1);
5914 case DCT_GLOBAL_CTORS
:
5915 case DCT_GLOBAL_DTORS
:
5916 d_advance (&di
, 11);
5917 dc
= d_make_comp (&di
,
5918 (type
== DCT_GLOBAL_CTORS
5919 ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
5920 : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS
),
5921 d_make_demangle_mangled_name (&di
, d_str (&di
)),
5923 d_advance (&di
, strlen (d_str (&di
)));
5926 abort (); /* We have listed all the cases. */
5929 /* If DMGL_PARAMS is set, then if we didn't consume the entire
5930 mangled string, then we didn't successfully demangle it. If
5931 DMGL_PARAMS is not set, we didn't look at the trailing
5933 if (((options
& DMGL_PARAMS
) != 0) && d_peek_char (&di
) != '\0')
5936 #ifdef CP_DEMANGLE_DEBUG
5940 status
= (dc
!= NULL
)
5941 ? cplus_demangle_print_callback (options
, dc
, callback
, opaque
)
5948 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
5949 name, return a buffer allocated with malloc holding the demangled
5950 name. OPTIONS is the usual libiberty demangler options. On
5951 success, this sets *PALC to the allocated size of the returned
5952 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
5953 a memory allocation failure, and returns NULL. */
5956 d_demangle (const char *mangled
, int options
, size_t *palc
)
5958 struct d_growable_string dgs
;
5961 d_growable_string_init (&dgs
, 0);
5963 status
= d_demangle_callback (mangled
, options
,
5964 d_growable_string_callback_adapter
, &dgs
);
5972 *palc
= dgs
.allocation_failure
? 1 : dgs
.alc
;
5976 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
5978 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
5980 /* ia64 ABI-mandated entry point in the C++ runtime library for
5981 performing demangling. MANGLED_NAME is a NUL-terminated character
5982 string containing the name to be demangled.
5984 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
5985 *LENGTH bytes, into which the demangled name is stored. If
5986 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
5987 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
5988 is placed in a region of memory allocated with malloc.
5990 If LENGTH is non-NULL, the length of the buffer containing the
5991 demangled name, is placed in *LENGTH.
5993 The return value is a pointer to the start of the NUL-terminated
5994 demangled name, or NULL if the demangling fails. The caller is
5995 responsible for deallocating this memory using free.
5997 *STATUS is set to one of the following values:
5998 0: The demangling operation succeeded.
5999 -1: A memory allocation failure occurred.
6000 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
6001 -3: One of the arguments is invalid.
6003 The demangling is performed using the C++ ABI mangling rules, with
6007 __cxa_demangle (const char *mangled_name
, char *output_buffer
,
6008 size_t *length
, int *status
)
6013 if (mangled_name
== NULL
)
6020 if (output_buffer
!= NULL
&& length
== NULL
)
6027 demangled
= d_demangle (mangled_name
, DMGL_PARAMS
| DMGL_TYPES
, &alc
);
6029 if (demangled
== NULL
)
6041 if (output_buffer
== NULL
)
6048 if (strlen (demangled
) < *length
)
6050 strcpy (output_buffer
, demangled
);
6052 demangled
= output_buffer
;
6056 free (output_buffer
);
6067 extern int __gcclibcxx_demangle_callback (const char *,
6069 (const char *, size_t, void *),
6072 /* Alternative, allocationless entry point in the C++ runtime library
6073 for performing demangling. MANGLED_NAME is a NUL-terminated character
6074 string containing the name to be demangled.
6076 CALLBACK is a callback function, called with demangled string
6077 segments as demangling progresses; it is called at least once,
6078 but may be called more than once. OPAQUE is a generalized pointer
6079 used as a callback argument.
6081 The return code is one of the following values, equivalent to
6082 the STATUS values of __cxa_demangle() (excluding -1, since this
6083 function performs no memory allocations):
6084 0: The demangling operation succeeded.
6085 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
6086 -3: One of the arguments is invalid.
6088 The demangling is performed using the C++ ABI mangling rules, with
6092 __gcclibcxx_demangle_callback (const char *mangled_name
,
6093 void (*callback
) (const char *, size_t, void *),
6098 if (mangled_name
== NULL
|| callback
== NULL
)
6101 status
= d_demangle_callback (mangled_name
, DMGL_PARAMS
| DMGL_TYPES
,
6109 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
6111 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
6112 mangled name, return a buffer allocated with malloc holding the
6113 demangled name. Otherwise, return NULL. */
6116 cplus_demangle_v3 (const char *mangled
, int options
)
6120 return d_demangle (mangled
, options
, &alc
);
6124 cplus_demangle_v3_callback (const char *mangled
, int options
,
6125 demangle_callbackref callback
, void *opaque
)
6127 return d_demangle_callback (mangled
, options
, callback
, opaque
);
6130 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
6131 conventions, but the output formatting is a little different.
6132 This instructs the C++ demangler not to emit pointer characters ("*"), to
6133 use Java's namespace separator symbol ("." instead of "::"), and to output
6134 JArray<TYPE> as TYPE[]. */
6137 java_demangle_v3 (const char *mangled
)
6141 return d_demangle (mangled
, DMGL_JAVA
| DMGL_PARAMS
| DMGL_RET_POSTFIX
, &alc
);
6145 java_demangle_v3_callback (const char *mangled
,
6146 demangle_callbackref callback
, void *opaque
)
6148 return d_demangle_callback (mangled
,
6149 DMGL_JAVA
| DMGL_PARAMS
| DMGL_RET_POSTFIX
,
6153 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
6155 #ifndef IN_GLIBCPP_V3
6157 /* Demangle a string in order to find out whether it is a constructor
6158 or destructor. Return non-zero on success. Set *CTOR_KIND and
6159 *DTOR_KIND appropriately. */
6162 is_ctor_or_dtor (const char *mangled
,
6163 enum gnu_v3_ctor_kinds
*ctor_kind
,
6164 enum gnu_v3_dtor_kinds
*dtor_kind
)
6167 struct demangle_component
*dc
;
6170 *ctor_kind
= (enum gnu_v3_ctor_kinds
) 0;
6171 *dtor_kind
= (enum gnu_v3_dtor_kinds
) 0;
6173 cplus_demangle_init_info (mangled
, DMGL_GNU_V3
, strlen (mangled
), &di
);
6176 #ifdef CP_DYNAMIC_ARRAYS
6177 __extension__
struct demangle_component comps
[di
.num_comps
];
6178 __extension__
struct demangle_component
*subs
[di
.num_subs
];
6183 di
.comps
= alloca (di
.num_comps
* sizeof (*di
.comps
));
6184 di
.subs
= alloca (di
.num_subs
* sizeof (*di
.subs
));
6187 dc
= cplus_demangle_mangled_name (&di
, 1);
6189 /* Note that because we did not pass DMGL_PARAMS, we don't expect
6190 to demangle the entire string. */
6197 /* These cannot appear on a constructor or destructor. */
6198 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
6199 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
6200 case DEMANGLE_COMPONENT_CONST_THIS
:
6201 case DEMANGLE_COMPONENT_REFERENCE_THIS
:
6202 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
:
6206 case DEMANGLE_COMPONENT_TYPED_NAME
:
6207 case DEMANGLE_COMPONENT_TEMPLATE
:
6210 case DEMANGLE_COMPONENT_QUAL_NAME
:
6211 case DEMANGLE_COMPONENT_LOCAL_NAME
:
6214 case DEMANGLE_COMPONENT_CTOR
:
6215 *ctor_kind
= dc
->u
.s_ctor
.kind
;
6219 case DEMANGLE_COMPONENT_DTOR
:
6220 *dtor_kind
= dc
->u
.s_dtor
.kind
;
6231 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
6232 name. A non-zero return indicates the type of constructor. */
6234 enum gnu_v3_ctor_kinds
6235 is_gnu_v3_mangled_ctor (const char *name
)
6237 enum gnu_v3_ctor_kinds ctor_kind
;
6238 enum gnu_v3_dtor_kinds dtor_kind
;
6240 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
6241 return (enum gnu_v3_ctor_kinds
) 0;
6246 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
6247 name. A non-zero return indicates the type of destructor. */
6249 enum gnu_v3_dtor_kinds
6250 is_gnu_v3_mangled_dtor (const char *name
)
6252 enum gnu_v3_ctor_kinds ctor_kind
;
6253 enum gnu_v3_dtor_kinds dtor_kind
;
6255 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
6256 return (enum gnu_v3_dtor_kinds
) 0;
6260 #endif /* IN_GLIBCPP_V3 */
6262 #ifdef STANDALONE_DEMANGLER
6265 #include "dyn-string.h"
6267 static void print_usage (FILE* fp
, int exit_value
);
6269 #define IS_ALPHA(CHAR) \
6270 (((CHAR) >= 'a' && (CHAR) <= 'z') \
6271 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
6273 /* Non-zero if CHAR is a character than can occur in a mangled name. */
6274 #define is_mangled_char(CHAR) \
6275 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
6276 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
6278 /* The name of this program, as invoked. */
6279 const char* program_name
;
6281 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
6284 print_usage (FILE* fp
, int exit_value
)
6286 fprintf (fp
, "Usage: %s [options] [names ...]\n", program_name
);
6287 fprintf (fp
, "Options:\n");
6288 fprintf (fp
, " -h,--help Display this message.\n");
6289 fprintf (fp
, " -p,--no-params Don't display function parameters\n");
6290 fprintf (fp
, " -v,--verbose Produce verbose demanglings.\n");
6291 fprintf (fp
, "If names are provided, they are demangled. Otherwise filters standard input.\n");
6296 /* Option specification for getopt_long. */
6297 static const struct option long_options
[] =
6299 { "help", no_argument
, NULL
, 'h' },
6300 { "no-params", no_argument
, NULL
, 'p' },
6301 { "verbose", no_argument
, NULL
, 'v' },
6302 { NULL
, no_argument
, NULL
, 0 },
6305 /* Main entry for a demangling filter executable. It will demangle
6306 its command line arguments, if any. If none are provided, it will
6307 filter stdin to stdout, replacing any recognized mangled C++ names
6308 with their demangled equivalents. */
6311 main (int argc
, char *argv
[])
6315 int options
= DMGL_PARAMS
| DMGL_ANSI
| DMGL_TYPES
;
6317 /* Use the program name of this program, as invoked. */
6318 program_name
= argv
[0];
6320 /* Parse options. */
6323 opt_char
= getopt_long (argc
, argv
, "hpv", long_options
, NULL
);
6326 case '?': /* Unrecognized option. */
6327 print_usage (stderr
, 1);
6331 print_usage (stdout
, 0);
6335 options
&= ~ DMGL_PARAMS
;
6339 options
|= DMGL_VERBOSE
;
6343 while (opt_char
!= -1);
6346 /* No command line arguments were provided. Filter stdin. */
6348 dyn_string_t mangled
= dyn_string_new (3);
6351 /* Read all of input. */
6352 while (!feof (stdin
))
6356 /* Pile characters into mangled until we hit one that can't
6357 occur in a mangled name. */
6359 while (!feof (stdin
) && is_mangled_char (c
))
6361 dyn_string_append_char (mangled
, c
);
6367 if (dyn_string_length (mangled
) > 0)
6369 #ifdef IN_GLIBCPP_V3
6370 s
= __cxa_demangle (dyn_string_buf (mangled
), NULL
, NULL
, NULL
);
6372 s
= cplus_demangle_v3 (dyn_string_buf (mangled
), options
);
6382 /* It might not have been a mangled name. Print the
6384 fputs (dyn_string_buf (mangled
), stdout
);
6387 dyn_string_clear (mangled
);
6390 /* If we haven't hit EOF yet, we've read one character that
6391 can't occur in a mangled name, so print it out. */
6396 dyn_string_delete (mangled
);
6399 /* Demangle command line arguments. */
6401 /* Loop over command line arguments. */
6402 for (i
= optind
; i
< argc
; ++i
)
6405 #ifdef IN_GLIBCPP_V3
6409 /* Attempt to demangle. */
6410 #ifdef IN_GLIBCPP_V3
6411 s
= __cxa_demangle (argv
[i
], NULL
, NULL
, &status
);
6413 s
= cplus_demangle_v3 (argv
[i
], options
);
6416 /* If it worked, print the demangled name. */
6424 #ifdef IN_GLIBCPP_V3
6425 fprintf (stderr
, "Failed: %s (status %d)\n", argv
[i
], status
);
6427 fprintf (stderr
, "Failed: %s\n", argv
[i
]);
6436 #endif /* STANDALONE_DEMANGLER */