1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003-2022 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@wasabisystems.com>.
5 This file is part of the libiberty library, which is part of GCC.
7 This file is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combined
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
31 /* This code implements a demangler for the g++ V3 ABI. The ABI is
32 described on this web page:
33 https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling
35 This code was written while looking at the demangler written by
36 Alex Samuel <samuel@codesourcery.com>.
38 This code first pulls the mangled name apart into a list of
39 components, and then walks the list generating the demangled
42 This file will normally define the following functions, q.v.:
43 char *cplus_demangle_v3(const char *mangled, int options)
44 char *java_demangle_v3(const char *mangled)
45 int cplus_demangle_v3_callback(const char *mangled, int options,
46 demangle_callbackref callback)
47 int java_demangle_v3_callback(const char *mangled,
48 demangle_callbackref callback)
49 enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
50 enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
52 Also, the interface to the component list is public, and defined in
53 demangle.h. The interface consists of these types, which are
54 defined in demangle.h:
55 enum demangle_component_type
56 struct demangle_component
58 and these functions defined in this file:
59 cplus_demangle_fill_name
60 cplus_demangle_fill_extended_operator
61 cplus_demangle_fill_ctor
62 cplus_demangle_fill_dtor
64 cplus_demangle_print_callback
65 and other functions defined in the file cp-demint.c.
67 This file also defines some other functions and variables which are
68 only to be used by the file cp-demint.c.
70 Preprocessor macros you can define while compiling this file:
73 If defined, this file defines the following functions, q.v.:
74 char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
76 int __gcclibcxx_demangle_callback (const char *,
78 (const char *, size_t, void *),
80 instead of cplus_demangle_v3[_callback]() and
81 java_demangle_v3[_callback]().
84 If defined, this file defines only __cxa_demangle() and
85 __gcclibcxx_demangle_callback(), and no other publically visible
86 functions or variables.
89 If defined, this file defines a main() function which demangles
90 any arguments, or, if none, demangles stdin.
93 If defined, turns on debugging mode, which prints information on
94 stdout about the mangled string. This is not generally useful.
97 If defined, additional sanity checks will be performed. It will
98 cause some slowdown, but will allow to catch out-of-bound access
99 errors earlier. This macro is intended for testing and debugging. */
101 #if defined (_AIX) && !defined (__GNUC__)
123 # define alloca __builtin_alloca
125 extern char *alloca ();
126 # endif /* __GNUC__ */
128 #endif /* HAVE_ALLOCA_H */
134 # define INT_MAX (int)(((unsigned int) ~0) >> 1) /* 0x7FFFFFFF */
137 #include "ansidecl.h"
138 #include "libiberty.h"
139 #include "demangle.h"
140 #include "cp-demangle.h"
142 /* If IN_GLIBCPP_V3 is defined, some functions are made static. We
143 also rename them via #define to avoid compiler errors when the
144 static definition conflicts with the extern declaration in a header
148 #define CP_STATIC_IF_GLIBCPP_V3 static
150 #define cplus_demangle_fill_name d_fill_name
151 static int d_fill_name (struct demangle_component
*, const char *, int);
153 #define cplus_demangle_fill_extended_operator d_fill_extended_operator
155 d_fill_extended_operator (struct demangle_component
*, int,
156 struct demangle_component
*);
158 #define cplus_demangle_fill_ctor d_fill_ctor
160 d_fill_ctor (struct demangle_component
*, enum gnu_v3_ctor_kinds
,
161 struct demangle_component
*);
163 #define cplus_demangle_fill_dtor d_fill_dtor
165 d_fill_dtor (struct demangle_component
*, enum gnu_v3_dtor_kinds
,
166 struct demangle_component
*);
168 #define cplus_demangle_mangled_name d_mangled_name
169 static struct demangle_component
*d_mangled_name (struct d_info
*, int);
171 #define cplus_demangle_type d_type
172 static struct demangle_component
*d_type (struct d_info
*);
174 #define cplus_demangle_print d_print
175 static char *d_print (int, struct demangle_component
*, int, size_t *);
177 #define cplus_demangle_print_callback d_print_callback
178 static int d_print_callback (int, struct demangle_component
*,
179 demangle_callbackref
, void *);
181 #define cplus_demangle_init_info d_init_info
182 static void d_init_info (const char *, int, size_t, struct d_info
*);
184 #else /* ! defined(IN_GLIBCPP_V3) */
185 #define CP_STATIC_IF_GLIBCPP_V3
186 #endif /* ! defined(IN_GLIBCPP_V3) */
188 /* See if the compiler supports dynamic arrays. */
191 #define CP_DYNAMIC_ARRAYS
194 #ifdef __STDC_VERSION__
195 #if __STDC_VERSION__ >= 199901L && !__STDC_NO_VLA__
196 #define CP_DYNAMIC_ARRAYS
197 #endif /* __STDC_VERSION__ >= 199901L && !__STDC_NO_VLA__ */
198 #endif /* defined (__STDC_VERSION__) */
199 #endif /* defined (__STDC__) */
200 #endif /* ! defined (__GNUC__) */
202 /* We avoid pulling in the ctype tables, to prevent pulling in
203 additional unresolved symbols when this code is used in a library.
204 FIXME: Is this really a valid reason? This comes from the original
207 As of this writing this file has the following undefined references
208 when compiled with -DIN_GLIBCPP_V3: realloc, free, memcpy, strcpy,
211 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
212 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
213 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
215 /* The prefix prepended by GCC to an identifier represnting the
216 anonymous namespace. */
217 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
218 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
219 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
221 /* Information we keep for the standard substitutions. */
223 struct d_standard_sub_info
225 /* The code for this substitution. */
227 /* The simple string it expands to. */
228 const char *simple_expansion
;
229 /* The length of the simple expansion. */
231 /* The results of a full, verbose, expansion. This is used when
232 qualifying a constructor/destructor, or when in verbose mode. */
233 const char *full_expansion
;
234 /* The length of the full expansion. */
236 /* What to set the last_name field of d_info to; NULL if we should
237 not set it. This is only relevant when qualifying a
238 constructor/destructor. */
239 const char *set_last_name
;
240 /* The length of set_last_name. */
241 int set_last_name_len
;
244 /* Accessors for subtrees of struct demangle_component. */
246 #define d_left(dc) ((dc)->u.s_binary.left)
247 #define d_right(dc) ((dc)->u.s_binary.right)
249 /* A list of templates. This is used while printing. */
251 struct d_print_template
253 /* Next template on the list. */
254 struct d_print_template
*next
;
256 const struct demangle_component
*template_decl
;
259 /* A list of type modifiers. This is used while printing. */
263 /* Next modifier on the list. These are in the reverse of the order
264 in which they appeared in the mangled string. */
265 struct d_print_mod
*next
;
267 struct demangle_component
*mod
;
268 /* Whether this modifier was printed. */
270 /* The list of templates which applies to this modifier. */
271 struct d_print_template
*templates
;
274 /* We use these structures to hold information during printing. */
276 struct d_growable_string
278 /* Buffer holding the result. */
280 /* Current length of data in buffer. */
282 /* Allocated size of buffer. */
284 /* Set to 1 if we had a memory allocation failure. */
285 int allocation_failure
;
288 /* Stack of components, innermost first, used to avoid loops. */
290 struct d_component_stack
292 /* This component. */
293 const struct demangle_component
*dc
;
294 /* This component's parent. */
295 const struct d_component_stack
*parent
;
298 /* A demangle component and some scope captured when it was first
303 /* The component whose scope this is. */
304 const struct demangle_component
*container
;
305 /* The list of templates, if any, that was current when this
306 scope was captured. */
307 struct d_print_template
*templates
;
310 /* Checkpoint structure to allow backtracking. This holds copies
311 of the fields of struct d_info that need to be restored
312 if a trial parse needs to be backtracked over. */
314 struct d_info_checkpoint
322 /* Maximum number of times d_print_comp may be called recursively. */
323 #define MAX_RECURSION_COUNT 1024
325 enum { D_PRINT_BUFFER_LENGTH
= 256 };
328 /* Fixed-length allocated buffer for demangled data, flushed to the
329 callback with a NUL termination once full. */
330 char buf
[D_PRINT_BUFFER_LENGTH
];
331 /* Current length of data in buffer. */
333 /* The last character printed, saved individually so that it survives
336 /* Callback function to handle demangled buffer flush. */
337 demangle_callbackref callback
;
338 /* Opaque callback argument. */
340 /* The current list of templates, if any. */
341 struct d_print_template
*templates
;
342 /* The current list of modifiers (e.g., pointer, reference, etc.),
344 struct d_print_mod
*modifiers
;
345 /* Set to 1 if we saw a demangling error. */
346 int demangle_failure
;
347 /* Number of times d_print_comp was recursively called. Should not
348 be bigger than MAX_RECURSION_COUNT. */
350 /* Non-zero if we're printing a lambda argument. A template
351 parameter reference actually means 'auto'. */
353 /* The current index into any template argument packs we are using
354 for printing, or -1 to print the whole pack. */
356 /* Number of d_print_flush calls so far. */
357 unsigned long int flush_count
;
358 /* Stack of components, innermost first, used to avoid loops. */
359 const struct d_component_stack
*component_stack
;
360 /* Array of saved scopes for evaluating substitutions. */
361 struct d_saved_scope
*saved_scopes
;
362 /* Index of the next unused saved scope in the above array. */
363 int next_saved_scope
;
364 /* Number of saved scopes in the above array. */
365 int num_saved_scopes
;
366 /* Array of templates for saving into scopes. */
367 struct d_print_template
*copy_templates
;
368 /* Index of the next unused copy template in the above array. */
369 int next_copy_template
;
370 /* Number of copy templates in the above array. */
371 int num_copy_templates
;
372 /* The nearest enclosing template, if any. */
373 const struct demangle_component
*current_template
;
376 #ifdef CP_DEMANGLE_DEBUG
377 static void d_dump (struct demangle_component
*, int);
380 static struct demangle_component
*
381 d_make_empty (struct d_info
*);
383 static struct demangle_component
*
384 d_make_comp (struct d_info
*, enum demangle_component_type
,
385 struct demangle_component
*,
386 struct demangle_component
*);
388 static struct demangle_component
*
389 d_make_name (struct d_info
*, const char *, int);
391 static struct demangle_component
*
392 d_make_demangle_mangled_name (struct d_info
*, const char *);
394 static struct demangle_component
*
395 d_make_builtin_type (struct d_info
*,
396 const struct demangle_builtin_type_info
*);
398 static struct demangle_component
*
399 d_make_operator (struct d_info
*,
400 const struct demangle_operator_info
*);
402 static struct demangle_component
*
403 d_make_extended_operator (struct d_info
*, int,
404 struct demangle_component
*);
406 static struct demangle_component
*
407 d_make_ctor (struct d_info
*, enum gnu_v3_ctor_kinds
,
408 struct demangle_component
*);
410 static struct demangle_component
*
411 d_make_dtor (struct d_info
*, enum gnu_v3_dtor_kinds
,
412 struct demangle_component
*);
414 static struct demangle_component
*
415 d_make_template_param (struct d_info
*, int);
417 static struct demangle_component
*
418 d_make_sub (struct d_info
*, const char *, int);
421 has_return_type (struct demangle_component
*);
424 is_ctor_dtor_or_conversion (struct demangle_component
*);
426 static struct demangle_component
*d_encoding (struct d_info
*, int);
428 static struct demangle_component
*d_name (struct d_info
*, int substable
);
430 static struct demangle_component
*d_nested_name (struct d_info
*);
432 static int d_maybe_module_name (struct d_info
*, struct demangle_component
**);
434 static struct demangle_component
*d_prefix (struct d_info
*, int);
436 static struct demangle_component
*d_unqualified_name (struct d_info
*,
437 struct demangle_component
*scope
, struct demangle_component
*module
);
439 static struct demangle_component
*d_source_name (struct d_info
*);
441 static int d_number (struct d_info
*);
443 static struct demangle_component
*d_identifier (struct d_info
*, int);
445 static struct demangle_component
*d_operator_name (struct d_info
*);
447 static struct demangle_component
*d_special_name (struct d_info
*);
449 static struct demangle_component
*d_parmlist (struct d_info
*);
451 static int d_call_offset (struct d_info
*, int);
453 static struct demangle_component
*d_ctor_dtor_name (struct d_info
*);
455 static struct demangle_component
**
456 d_cv_qualifiers (struct d_info
*, struct demangle_component
**, int);
458 static struct demangle_component
*
459 d_ref_qualifier (struct d_info
*, struct demangle_component
*);
461 static struct demangle_component
*
462 d_function_type (struct d_info
*);
464 static struct demangle_component
*
465 d_bare_function_type (struct d_info
*, int);
467 static struct demangle_component
*
468 d_class_enum_type (struct d_info
*, int);
470 static struct demangle_component
*d_array_type (struct d_info
*);
472 static struct demangle_component
*d_vector_type (struct d_info
*);
474 static struct demangle_component
*
475 d_pointer_to_member_type (struct d_info
*);
477 static struct demangle_component
*
478 d_template_param (struct d_info
*);
480 static struct demangle_component
*d_template_args (struct d_info
*);
481 static struct demangle_component
*d_template_args_1 (struct d_info
*);
483 static struct demangle_component
*
484 d_template_arg (struct d_info
*);
486 static struct demangle_component
*d_expression (struct d_info
*);
488 static struct demangle_component
*d_expr_primary (struct d_info
*);
490 static struct demangle_component
*d_local_name (struct d_info
*);
492 static int d_discriminator (struct d_info
*);
494 static struct demangle_component
*d_lambda (struct d_info
*);
496 static struct demangle_component
*d_unnamed_type (struct d_info
*);
498 static struct demangle_component
*
499 d_clone_suffix (struct d_info
*, struct demangle_component
*);
502 d_add_substitution (struct d_info
*, struct demangle_component
*);
504 static struct demangle_component
*d_substitution (struct d_info
*, int);
506 static void d_checkpoint (struct d_info
*, struct d_info_checkpoint
*);
508 static void d_backtrack (struct d_info
*, struct d_info_checkpoint
*);
510 static void d_growable_string_init (struct d_growable_string
*, size_t);
513 d_growable_string_resize (struct d_growable_string
*, size_t);
516 d_growable_string_append_buffer (struct d_growable_string
*,
517 const char *, size_t);
519 d_growable_string_callback_adapter (const char *, size_t, void *);
522 d_print_init (struct d_print_info
*, demangle_callbackref
, void *,
523 struct demangle_component
*);
525 static inline void d_print_error (struct d_print_info
*);
527 static inline int d_print_saw_error (struct d_print_info
*);
529 static inline void d_print_flush (struct d_print_info
*);
531 static inline void d_append_char (struct d_print_info
*, char);
533 static inline void d_append_buffer (struct d_print_info
*,
534 const char *, size_t);
536 static inline void d_append_string (struct d_print_info
*, const char *);
538 static inline char d_last_char (struct d_print_info
*);
541 d_print_comp (struct d_print_info
*, int, struct demangle_component
*);
544 d_print_java_identifier (struct d_print_info
*, const char *, int);
547 d_print_mod_list (struct d_print_info
*, int, struct d_print_mod
*, int);
550 d_print_mod (struct d_print_info
*, int, struct demangle_component
*);
553 d_print_function_type (struct d_print_info
*, int,
554 struct demangle_component
*,
555 struct d_print_mod
*);
558 d_print_array_type (struct d_print_info
*, int,
559 struct demangle_component
*,
560 struct d_print_mod
*);
563 d_print_expr_op (struct d_print_info
*, int, struct demangle_component
*);
565 static void d_print_cast (struct d_print_info
*, int,
566 struct demangle_component
*);
567 static void d_print_conversion (struct d_print_info
*, int,
568 struct demangle_component
*);
570 static int d_demangle_callback (const char *, int,
571 demangle_callbackref
, void *);
572 static char *d_demangle (const char *, int, size_t *);
574 #define FNQUAL_COMPONENT_CASE \
575 case DEMANGLE_COMPONENT_RESTRICT_THIS: \
576 case DEMANGLE_COMPONENT_VOLATILE_THIS: \
577 case DEMANGLE_COMPONENT_CONST_THIS: \
578 case DEMANGLE_COMPONENT_REFERENCE_THIS: \
579 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS: \
580 case DEMANGLE_COMPONENT_TRANSACTION_SAFE: \
581 case DEMANGLE_COMPONENT_NOEXCEPT: \
582 case DEMANGLE_COMPONENT_THROW_SPEC
584 /* True iff TYPE is a demangling component representing a
585 function-type-qualifier. */
588 is_fnqual_component_type (enum demangle_component_type type
)
592 FNQUAL_COMPONENT_CASE
:
601 #ifdef CP_DEMANGLE_DEBUG
604 d_dump (struct demangle_component
*dc
, int indent
)
611 printf ("failed demangling\n");
615 for (i
= 0; i
< indent
; ++i
)
620 case DEMANGLE_COMPONENT_NAME
:
621 printf ("name '%.*s'\n", dc
->u
.s_name
.len
, dc
->u
.s_name
.s
);
623 case DEMANGLE_COMPONENT_TAGGED_NAME
:
624 printf ("tagged name\n");
625 d_dump (dc
->u
.s_binary
.left
, indent
+ 2);
626 d_dump (dc
->u
.s_binary
.right
, indent
+ 2);
628 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
629 printf ("template parameter %ld\n", dc
->u
.s_number
.number
);
631 case DEMANGLE_COMPONENT_TPARM_OBJ
:
632 printf ("template parameter object\n");
634 case DEMANGLE_COMPONENT_FUNCTION_PARAM
:
635 printf ("function parameter %ld\n", dc
->u
.s_number
.number
);
637 case DEMANGLE_COMPONENT_CTOR
:
638 printf ("constructor %d\n", (int) dc
->u
.s_ctor
.kind
);
639 d_dump (dc
->u
.s_ctor
.name
, indent
+ 2);
641 case DEMANGLE_COMPONENT_DTOR
:
642 printf ("destructor %d\n", (int) dc
->u
.s_dtor
.kind
);
643 d_dump (dc
->u
.s_dtor
.name
, indent
+ 2);
645 case DEMANGLE_COMPONENT_SUB_STD
:
646 printf ("standard substitution %s\n", dc
->u
.s_string
.string
);
648 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
649 printf ("builtin type %s\n", dc
->u
.s_builtin
.type
->name
);
651 case DEMANGLE_COMPONENT_OPERATOR
:
652 printf ("operator %s\n", dc
->u
.s_operator
.op
->name
);
654 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
655 printf ("extended operator with %d args\n",
656 dc
->u
.s_extended_operator
.args
);
657 d_dump (dc
->u
.s_extended_operator
.name
, indent
+ 2);
660 case DEMANGLE_COMPONENT_QUAL_NAME
:
661 printf ("qualified name\n");
663 case DEMANGLE_COMPONENT_LOCAL_NAME
:
664 printf ("local name\n");
666 case DEMANGLE_COMPONENT_TYPED_NAME
:
667 printf ("typed name\n");
669 case DEMANGLE_COMPONENT_TEMPLATE
:
670 printf ("template\n");
672 case DEMANGLE_COMPONENT_VTABLE
:
675 case DEMANGLE_COMPONENT_VTT
:
678 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
679 printf ("construction vtable\n");
681 case DEMANGLE_COMPONENT_TYPEINFO
:
682 printf ("typeinfo\n");
684 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
685 printf ("typeinfo name\n");
687 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
688 printf ("typeinfo function\n");
690 case DEMANGLE_COMPONENT_THUNK
:
693 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
694 printf ("virtual thunk\n");
696 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
697 printf ("covariant thunk\n");
699 case DEMANGLE_COMPONENT_JAVA_CLASS
:
700 printf ("java class\n");
702 case DEMANGLE_COMPONENT_GUARD
:
705 case DEMANGLE_COMPONENT_REFTEMP
:
706 printf ("reference temporary\n");
708 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
709 printf ("hidden alias\n");
711 case DEMANGLE_COMPONENT_TRANSACTION_CLONE
:
712 printf ("transaction clone\n");
714 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
:
715 printf ("non-transaction clone\n");
717 case DEMANGLE_COMPONENT_RESTRICT
:
718 printf ("restrict\n");
720 case DEMANGLE_COMPONENT_VOLATILE
:
721 printf ("volatile\n");
723 case DEMANGLE_COMPONENT_CONST
:
726 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
727 printf ("restrict this\n");
729 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
730 printf ("volatile this\n");
732 case DEMANGLE_COMPONENT_CONST_THIS
:
733 printf ("const this\n");
735 case DEMANGLE_COMPONENT_REFERENCE_THIS
:
736 printf ("reference this\n");
738 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
:
739 printf ("rvalue reference this\n");
741 case DEMANGLE_COMPONENT_TRANSACTION_SAFE
:
742 printf ("transaction_safe this\n");
744 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
745 printf ("vendor type qualifier\n");
747 case DEMANGLE_COMPONENT_POINTER
:
748 printf ("pointer\n");
750 case DEMANGLE_COMPONENT_REFERENCE
:
751 printf ("reference\n");
753 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
754 printf ("rvalue reference\n");
756 case DEMANGLE_COMPONENT_COMPLEX
:
757 printf ("complex\n");
759 case DEMANGLE_COMPONENT_IMAGINARY
:
760 printf ("imaginary\n");
762 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
763 printf ("vendor type\n");
765 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
766 printf ("function type\n");
768 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
769 printf ("array type\n");
771 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
772 printf ("pointer to member type\n");
774 case DEMANGLE_COMPONENT_FIXED_TYPE
:
775 printf ("fixed-point type, accum? %d, sat? %d\n",
776 dc
->u
.s_fixed
.accum
, dc
->u
.s_fixed
.sat
);
777 d_dump (dc
->u
.s_fixed
.length
, indent
+ 2);
779 case DEMANGLE_COMPONENT_ARGLIST
:
780 printf ("argument list\n");
782 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
783 printf ("template argument list\n");
785 case DEMANGLE_COMPONENT_INITIALIZER_LIST
:
786 printf ("initializer list\n");
788 case DEMANGLE_COMPONENT_CAST
:
791 case DEMANGLE_COMPONENT_CONVERSION
:
792 printf ("conversion operator\n");
794 case DEMANGLE_COMPONENT_NULLARY
:
795 printf ("nullary operator\n");
797 case DEMANGLE_COMPONENT_UNARY
:
798 printf ("unary operator\n");
800 case DEMANGLE_COMPONENT_BINARY
:
801 printf ("binary operator\n");
803 case DEMANGLE_COMPONENT_BINARY_ARGS
:
804 printf ("binary operator arguments\n");
806 case DEMANGLE_COMPONENT_TRINARY
:
807 printf ("trinary operator\n");
809 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
810 printf ("trinary operator arguments 1\n");
812 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
813 printf ("trinary operator arguments 1\n");
815 case DEMANGLE_COMPONENT_LITERAL
:
816 printf ("literal\n");
818 case DEMANGLE_COMPONENT_LITERAL_NEG
:
819 printf ("negative literal\n");
821 case DEMANGLE_COMPONENT_VENDOR_EXPR
:
822 printf ("vendor expression\n");
824 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
825 printf ("java resource\n");
827 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
828 printf ("compound name\n");
830 case DEMANGLE_COMPONENT_CHARACTER
:
831 printf ("character '%c'\n", dc
->u
.s_character
.character
);
833 case DEMANGLE_COMPONENT_NUMBER
:
834 printf ("number %ld\n", dc
->u
.s_number
.number
);
836 case DEMANGLE_COMPONENT_DECLTYPE
:
837 printf ("decltype\n");
839 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
840 printf ("pack expansion\n");
842 case DEMANGLE_COMPONENT_TLS_INIT
:
843 printf ("tls init function\n");
845 case DEMANGLE_COMPONENT_TLS_WRAPPER
:
846 printf ("tls wrapper function\n");
848 case DEMANGLE_COMPONENT_DEFAULT_ARG
:
849 printf ("default argument %d\n", dc
->u
.s_unary_num
.num
);
850 d_dump (dc
->u
.s_unary_num
.sub
, indent
+2);
852 case DEMANGLE_COMPONENT_LAMBDA
:
853 printf ("lambda %d\n", dc
->u
.s_unary_num
.num
);
854 d_dump (dc
->u
.s_unary_num
.sub
, indent
+2);
858 d_dump (d_left (dc
), indent
+ 2);
859 d_dump (d_right (dc
), indent
+ 2);
862 #endif /* CP_DEMANGLE_DEBUG */
864 /* Fill in a DEMANGLE_COMPONENT_NAME. */
866 CP_STATIC_IF_GLIBCPP_V3
868 cplus_demangle_fill_name (struct demangle_component
*p
, const char *s
, int len
)
870 if (p
== NULL
|| s
== NULL
|| len
<= 0)
874 p
->type
= DEMANGLE_COMPONENT_NAME
;
876 p
->u
.s_name
.len
= len
;
880 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
882 CP_STATIC_IF_GLIBCPP_V3
884 cplus_demangle_fill_extended_operator (struct demangle_component
*p
, int args
,
885 struct demangle_component
*name
)
887 if (p
== NULL
|| args
< 0 || name
== NULL
)
891 p
->type
= DEMANGLE_COMPONENT_EXTENDED_OPERATOR
;
892 p
->u
.s_extended_operator
.args
= args
;
893 p
->u
.s_extended_operator
.name
= name
;
897 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
899 CP_STATIC_IF_GLIBCPP_V3
901 cplus_demangle_fill_ctor (struct demangle_component
*p
,
902 enum gnu_v3_ctor_kinds kind
,
903 struct demangle_component
*name
)
907 || (int) kind
< gnu_v3_complete_object_ctor
908 || (int) kind
> gnu_v3_object_ctor_group
)
912 p
->type
= DEMANGLE_COMPONENT_CTOR
;
913 p
->u
.s_ctor
.kind
= kind
;
914 p
->u
.s_ctor
.name
= name
;
918 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
920 CP_STATIC_IF_GLIBCPP_V3
922 cplus_demangle_fill_dtor (struct demangle_component
*p
,
923 enum gnu_v3_dtor_kinds kind
,
924 struct demangle_component
*name
)
928 || (int) kind
< gnu_v3_deleting_dtor
929 || (int) kind
> gnu_v3_object_dtor_group
)
933 p
->type
= DEMANGLE_COMPONENT_DTOR
;
934 p
->u
.s_dtor
.kind
= kind
;
935 p
->u
.s_dtor
.name
= name
;
939 /* Add a new component. */
941 static struct demangle_component
*
942 d_make_empty (struct d_info
*di
)
944 struct demangle_component
*p
;
946 if (di
->next_comp
>= di
->num_comps
)
948 p
= &di
->comps
[di
->next_comp
];
955 /* Add a new generic component. */
957 static struct demangle_component
*
958 d_make_comp (struct d_info
*di
, enum demangle_component_type type
,
959 struct demangle_component
*left
,
960 struct demangle_component
*right
)
962 struct demangle_component
*p
;
964 /* We check for errors here. A typical error would be a NULL return
965 from a subroutine. We catch those here, and return NULL
969 /* These types require two parameters. */
970 case DEMANGLE_COMPONENT_QUAL_NAME
:
971 case DEMANGLE_COMPONENT_LOCAL_NAME
:
972 case DEMANGLE_COMPONENT_TYPED_NAME
:
973 case DEMANGLE_COMPONENT_TAGGED_NAME
:
974 case DEMANGLE_COMPONENT_TEMPLATE
:
975 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
976 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
977 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
978 case DEMANGLE_COMPONENT_UNARY
:
979 case DEMANGLE_COMPONENT_BINARY
:
980 case DEMANGLE_COMPONENT_BINARY_ARGS
:
981 case DEMANGLE_COMPONENT_TRINARY
:
982 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
983 case DEMANGLE_COMPONENT_LITERAL
:
984 case DEMANGLE_COMPONENT_LITERAL_NEG
:
985 case DEMANGLE_COMPONENT_VENDOR_EXPR
:
986 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
987 case DEMANGLE_COMPONENT_VECTOR_TYPE
:
988 case DEMANGLE_COMPONENT_CLONE
:
989 case DEMANGLE_COMPONENT_MODULE_ENTITY
:
990 if (left
== NULL
|| right
== NULL
)
994 /* These types only require one parameter. */
995 case DEMANGLE_COMPONENT_VTABLE
:
996 case DEMANGLE_COMPONENT_VTT
:
997 case DEMANGLE_COMPONENT_TYPEINFO
:
998 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
999 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
1000 case DEMANGLE_COMPONENT_THUNK
:
1001 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
1002 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
1003 case DEMANGLE_COMPONENT_JAVA_CLASS
:
1004 case DEMANGLE_COMPONENT_GUARD
:
1005 case DEMANGLE_COMPONENT_TLS_INIT
:
1006 case DEMANGLE_COMPONENT_TLS_WRAPPER
:
1007 case DEMANGLE_COMPONENT_REFTEMP
:
1008 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
1009 case DEMANGLE_COMPONENT_TRANSACTION_CLONE
:
1010 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
:
1011 case DEMANGLE_COMPONENT_POINTER
:
1012 case DEMANGLE_COMPONENT_REFERENCE
:
1013 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
1014 case DEMANGLE_COMPONENT_COMPLEX
:
1015 case DEMANGLE_COMPONENT_IMAGINARY
:
1016 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
1017 case DEMANGLE_COMPONENT_CAST
:
1018 case DEMANGLE_COMPONENT_CONVERSION
:
1019 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
1020 case DEMANGLE_COMPONENT_DECLTYPE
:
1021 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
1022 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
:
1023 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS
:
1024 case DEMANGLE_COMPONENT_NULLARY
:
1025 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
1026 case DEMANGLE_COMPONENT_TPARM_OBJ
:
1027 case DEMANGLE_COMPONENT_STRUCTURED_BINDING
:
1028 case DEMANGLE_COMPONENT_MODULE_INIT
:
1033 /* This needs a right parameter, but the left parameter can be
1035 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
1036 case DEMANGLE_COMPONENT_INITIALIZER_LIST
:
1037 case DEMANGLE_COMPONENT_MODULE_NAME
:
1038 case DEMANGLE_COMPONENT_MODULE_PARTITION
:
1043 /* These are allowed to have no parameters--in some cases they
1044 will be filled in later. */
1045 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
1046 case DEMANGLE_COMPONENT_RESTRICT
:
1047 case DEMANGLE_COMPONENT_VOLATILE
:
1048 case DEMANGLE_COMPONENT_CONST
:
1049 case DEMANGLE_COMPONENT_ARGLIST
:
1050 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
1051 FNQUAL_COMPONENT_CASE
:
1054 /* Other types should not be seen here. */
1059 p
= d_make_empty (di
);
1063 p
->u
.s_binary
.left
= left
;
1064 p
->u
.s_binary
.right
= right
;
1069 /* Add a new demangle mangled name component. */
1071 static struct demangle_component
*
1072 d_make_demangle_mangled_name (struct d_info
*di
, const char *s
)
1074 if (d_peek_char (di
) != '_' || d_peek_next_char (di
) != 'Z')
1075 return d_make_name (di
, s
, strlen (s
));
1077 return d_encoding (di
, 0);
1080 /* Add a new name component. */
1082 static struct demangle_component
*
1083 d_make_name (struct d_info
*di
, const char *s
, int len
)
1085 struct demangle_component
*p
;
1087 p
= d_make_empty (di
);
1088 if (! cplus_demangle_fill_name (p
, s
, len
))
1093 /* Add a new builtin type component. */
1095 static struct demangle_component
*
1096 d_make_builtin_type (struct d_info
*di
,
1097 const struct demangle_builtin_type_info
*type
)
1099 struct demangle_component
*p
;
1103 p
= d_make_empty (di
);
1106 p
->type
= DEMANGLE_COMPONENT_BUILTIN_TYPE
;
1107 p
->u
.s_builtin
.type
= type
;
1112 /* Add a new operator component. */
1114 static struct demangle_component
*
1115 d_make_operator (struct d_info
*di
, const struct demangle_operator_info
*op
)
1117 struct demangle_component
*p
;
1119 p
= d_make_empty (di
);
1122 p
->type
= DEMANGLE_COMPONENT_OPERATOR
;
1123 p
->u
.s_operator
.op
= op
;
1128 /* Add a new extended operator component. */
1130 static struct demangle_component
*
1131 d_make_extended_operator (struct d_info
*di
, int args
,
1132 struct demangle_component
*name
)
1134 struct demangle_component
*p
;
1136 p
= d_make_empty (di
);
1137 if (! cplus_demangle_fill_extended_operator (p
, args
, name
))
1142 static struct demangle_component
*
1143 d_make_default_arg (struct d_info
*di
, int num
,
1144 struct demangle_component
*sub
)
1146 struct demangle_component
*p
= d_make_empty (di
);
1149 p
->type
= DEMANGLE_COMPONENT_DEFAULT_ARG
;
1150 p
->u
.s_unary_num
.num
= num
;
1151 p
->u
.s_unary_num
.sub
= sub
;
1156 /* Add a new constructor component. */
1158 static struct demangle_component
*
1159 d_make_ctor (struct d_info
*di
, enum gnu_v3_ctor_kinds kind
,
1160 struct demangle_component
*name
)
1162 struct demangle_component
*p
;
1164 p
= d_make_empty (di
);
1165 if (! cplus_demangle_fill_ctor (p
, kind
, name
))
1170 /* Add a new destructor component. */
1172 static struct demangle_component
*
1173 d_make_dtor (struct d_info
*di
, enum gnu_v3_dtor_kinds kind
,
1174 struct demangle_component
*name
)
1176 struct demangle_component
*p
;
1178 p
= d_make_empty (di
);
1179 if (! cplus_demangle_fill_dtor (p
, kind
, name
))
1184 /* Add a new template parameter. */
1186 static struct demangle_component
*
1187 d_make_template_param (struct d_info
*di
, int i
)
1189 struct demangle_component
*p
;
1191 p
= d_make_empty (di
);
1194 p
->type
= DEMANGLE_COMPONENT_TEMPLATE_PARAM
;
1195 p
->u
.s_number
.number
= i
;
1200 /* Add a new function parameter. */
1202 static struct demangle_component
*
1203 d_make_function_param (struct d_info
*di
, int i
)
1205 struct demangle_component
*p
;
1207 p
= d_make_empty (di
);
1210 p
->type
= DEMANGLE_COMPONENT_FUNCTION_PARAM
;
1211 p
->u
.s_number
.number
= i
;
1216 /* Add a new standard substitution component. */
1218 static struct demangle_component
*
1219 d_make_sub (struct d_info
*di
, const char *name
, int len
)
1221 struct demangle_component
*p
;
1223 p
= d_make_empty (di
);
1226 p
->type
= DEMANGLE_COMPONENT_SUB_STD
;
1227 p
->u
.s_string
.string
= name
;
1228 p
->u
.s_string
.len
= len
;
1233 /* <mangled-name> ::= _Z <encoding> [<clone-suffix>]*
1235 TOP_LEVEL is non-zero when called at the top level. */
1237 CP_STATIC_IF_GLIBCPP_V3
1238 struct demangle_component
*
1239 cplus_demangle_mangled_name (struct d_info
*di
, int top_level
)
1241 struct demangle_component
*p
;
1243 if (! d_check_char (di
, '_')
1244 /* Allow missing _ if not at toplevel to work around a
1245 bug in G++ abi-version=2 mangling; see the comment in
1246 write_template_arg. */
1249 if (! d_check_char (di
, 'Z'))
1251 p
= d_encoding (di
, top_level
);
1253 /* If at top level and parsing parameters, check for a clone
1255 if (top_level
&& (di
->options
& DMGL_PARAMS
) != 0)
1256 while (d_peek_char (di
) == '.'
1257 && (IS_LOWER (d_peek_next_char (di
))
1258 || d_peek_next_char (di
) == '_'
1259 || IS_DIGIT (d_peek_next_char (di
))))
1260 p
= d_clone_suffix (di
, p
);
1265 /* Return whether a function should have a return type. The argument
1266 is the function name, which may be qualified in various ways. The
1267 rules are that template functions have return types with some
1268 exceptions, function types which are not part of a function name
1269 mangling have return types with some exceptions, and non-template
1270 function names do not have return types. The exceptions are that
1271 constructors, destructors, and conversion operators do not have
1275 has_return_type (struct demangle_component
*dc
)
1283 case DEMANGLE_COMPONENT_LOCAL_NAME
:
1284 return has_return_type (d_right (dc
));
1285 case DEMANGLE_COMPONENT_TEMPLATE
:
1286 return ! is_ctor_dtor_or_conversion (d_left (dc
));
1287 FNQUAL_COMPONENT_CASE
:
1288 return has_return_type (d_left (dc
));
1292 /* Return whether a name is a constructor, a destructor, or a
1293 conversion operator. */
1296 is_ctor_dtor_or_conversion (struct demangle_component
*dc
)
1304 case DEMANGLE_COMPONENT_QUAL_NAME
:
1305 case DEMANGLE_COMPONENT_LOCAL_NAME
:
1306 return is_ctor_dtor_or_conversion (d_right (dc
));
1307 case DEMANGLE_COMPONENT_CTOR
:
1308 case DEMANGLE_COMPONENT_DTOR
:
1309 case DEMANGLE_COMPONENT_CONVERSION
:
1314 /* <encoding> ::= <(function) name> <bare-function-type>
1318 TOP_LEVEL is non-zero when called at the top level, in which case
1319 if DMGL_PARAMS is not set we do not demangle the function
1320 parameters. We only set this at the top level, because otherwise
1321 we would not correctly demangle names in local scopes. */
1323 static struct demangle_component
*
1324 d_encoding (struct d_info
*di
, int top_level
)
1326 char peek
= d_peek_char (di
);
1327 struct demangle_component
*dc
;
1329 if (peek
== 'G' || peek
== 'T')
1330 dc
= d_special_name (di
);
1333 dc
= d_name (di
, 0);
1336 /* Failed already. */;
1337 else if (top_level
&& (di
->options
& DMGL_PARAMS
) == 0)
1339 /* Strip off any initial CV-qualifiers, as they really apply
1340 to the `this' parameter, and they were not output by the
1341 v2 demangler without DMGL_PARAMS. */
1342 while (is_fnqual_component_type (dc
->type
))
1345 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1346 there may be function-qualifiers on its right argument which
1347 really apply here; this happens when parsing a class
1348 which is local to a function. */
1349 if (dc
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
1351 while (d_right (dc
) != NULL
1352 && is_fnqual_component_type (d_right (dc
)->type
))
1353 d_right (dc
) = d_left (d_right (dc
));
1355 if (d_right (dc
) == NULL
)
1361 peek
= d_peek_char (di
);
1362 if (peek
!= '\0' && peek
!= 'E')
1364 struct demangle_component
*ftype
;
1366 ftype
= d_bare_function_type (di
, has_return_type (dc
));
1369 /* If this is a non-top-level local-name, clear the
1370 return type, so it doesn't confuse the user by
1371 being confused with the return type of whaever
1372 this is nested within. */
1373 if (!top_level
&& dc
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
1374 && ftype
->type
== DEMANGLE_COMPONENT_FUNCTION_TYPE
)
1375 d_left (ftype
) = NULL
;
1377 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_TYPED_NAME
,
1389 /* <tagged-name> ::= <name> B <source-name> */
1391 static struct demangle_component
*
1392 d_abi_tags (struct d_info
*di
, struct demangle_component
*dc
)
1394 struct demangle_component
*hold_last_name
;
1397 /* Preserve the last name, so the ABI tag doesn't clobber it. */
1398 hold_last_name
= di
->last_name
;
1400 while (peek
= d_peek_char (di
),
1403 struct demangle_component
*tag
;
1405 tag
= d_source_name (di
);
1406 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_TAGGED_NAME
, dc
, tag
);
1409 di
->last_name
= hold_last_name
;
1414 /* <name> ::= <nested-name>
1416 ::= <unscoped-template-name> <template-args>
1419 <unscoped-name> ::= <unqualified-name>
1420 ::= St <unqualified-name>
1422 <unscoped-template-name> ::= <unscoped-name>
1426 static struct demangle_component
*
1427 d_name (struct d_info
*di
, int substable
)
1429 char peek
= d_peek_char (di
);
1430 struct demangle_component
*dc
= NULL
;
1431 struct demangle_component
*module
= NULL
;
1437 dc
= d_nested_name (di
);
1441 dc
= d_local_name (di
);
1445 dc
= d_unqualified_name (di
, NULL
, NULL
);
1450 if (d_peek_next_char (di
) == 't')
1453 dc
= d_make_name (di
, "std", 3);
1457 if (d_peek_char (di
) == 'S')
1459 module
= d_substitution (di
, 0);
1462 if (!(module
->type
== DEMANGLE_COMPONENT_MODULE_NAME
1463 || module
->type
== DEMANGLE_COMPONENT_MODULE_PARTITION
))
1478 dc
= d_unqualified_name (di
, dc
, module
);
1479 if (d_peek_char (di
) == 'I')
1481 /* This is <template-args>, which means that we just saw
1482 <unscoped-template-name>, which is a substitution
1484 if (!subst
&& !d_add_substitution (di
, dc
))
1486 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, dc
,
1487 d_template_args (di
));
1492 if (substable
&& !subst
&& !d_add_substitution (di
, dc
))
1497 /* <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1498 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
1501 static struct demangle_component
*
1502 d_nested_name (struct d_info
*di
)
1504 struct demangle_component
*ret
;
1505 struct demangle_component
**pret
;
1506 struct demangle_component
*rqual
;
1508 if (! d_check_char (di
, 'N'))
1511 pret
= d_cv_qualifiers (di
, &ret
, 1);
1515 /* Parse the ref-qualifier now and then attach it
1516 once we have something to attach it to. */
1517 rqual
= d_ref_qualifier (di
, NULL
);
1519 *pret
= d_prefix (di
, 1);
1525 d_left (rqual
) = ret
;
1529 if (! d_check_char (di
, 'E'))
1535 /* <prefix> ::= <prefix> <unqualified-name>
1536 ::= <template-prefix> <template-args>
1537 ::= <template-param>
1542 <template-prefix> ::= <prefix> <(template) unqualified-name>
1543 ::= <template-param>
1546 SUBST is true if we should add substitutions (as normal), false
1547 if not (in an unresolved-name). */
1549 static struct demangle_component
*
1550 d_prefix (struct d_info
*di
, int substable
)
1552 struct demangle_component
*ret
= NULL
;
1556 char peek
= d_peek_char (di
);
1558 /* The older code accepts a <local-name> here, but I don't see
1559 that in the grammar. The older code does not accept a
1560 <template-param> here. */
1563 && (d_peek_next_char (di
) == 'T'
1564 || d_peek_next_char (di
) == 't'))
1569 ret
= cplus_demangle_type (di
);
1571 else if (peek
== 'I')
1575 struct demangle_component
*dc
= d_template_args (di
);
1578 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
, dc
);
1580 else if (peek
== 'T')
1584 ret
= d_template_param (di
);
1586 else if (peek
== 'M')
1588 /* Initializer scope for a lambda. We don't need to represent
1589 this; the normal code will just treat the variable as a type
1590 scope, which gives appropriate output. */
1597 struct demangle_component
*module
= NULL
;
1600 module
= d_substitution (di
, 1);
1603 if (!(module
->type
== DEMANGLE_COMPONENT_MODULE_NAME
1604 || module
->type
== DEMANGLE_COMPONENT_MODULE_PARTITION
))
1612 ret
= d_unqualified_name (di
, ret
, module
);
1618 if (d_peek_char (di
) == 'E')
1621 if (substable
&& !d_add_substitution (di
, ret
))
1629 d_maybe_module_name (struct d_info
*di
, struct demangle_component
**name
)
1631 while (d_peek_char (di
) == 'W')
1634 enum demangle_component_type code
= DEMANGLE_COMPONENT_MODULE_NAME
;
1635 if (d_peek_char (di
) == 'P')
1637 code
= DEMANGLE_COMPONENT_MODULE_PARTITION
;
1641 *name
= d_make_comp (di
, code
, *name
, d_source_name (di
));
1644 if (!d_add_substitution (di
, *name
))
1650 /* <unqualified-name> ::= [<module-name>] <operator-name> [<abi-tags>]
1651 ::= [<module-name>] <ctor-dtor-name> [<abi-tags>]
1652 ::= [<module-name>] <source-name> [<abi-tags>]
1653 ::= [<module-name>] <local-source-name> [<abi-tags>]
1654 ::= [<module-name>] DC <source-name>+ E [<abi-tags>]
1655 <local-source-name> ::= L <source-name> <discriminator> [<abi-tags>]
1658 static struct demangle_component
*
1659 d_unqualified_name (struct d_info
*di
, struct demangle_component
*scope
,
1660 struct demangle_component
*module
)
1662 struct demangle_component
*ret
;
1665 if (!d_maybe_module_name (di
, &module
))
1668 peek
= d_peek_char (di
);
1669 if (IS_DIGIT (peek
))
1670 ret
= d_source_name (di
);
1671 else if (IS_LOWER (peek
))
1673 int was_expr
= di
->is_expression
;
1674 if (peek
== 'o' && d_peek_next_char (di
) == 'n')
1677 /* Treat cv as naming a conversion operator. */
1678 di
->is_expression
= 0;
1680 ret
= d_operator_name (di
);
1681 di
->is_expression
= was_expr
;
1682 if (ret
!= NULL
&& ret
->type
== DEMANGLE_COMPONENT_OPERATOR
)
1684 di
->expansion
+= sizeof "operator" + ret
->u
.s_operator
.op
->len
- 2;
1685 if (!strcmp (ret
->u
.s_operator
.op
->code
, "li"))
1686 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, ret
,
1687 d_source_name (di
));
1690 else if (peek
== 'D' && d_peek_next_char (di
) == 'C')
1692 // structured binding
1694 struct demangle_component
*prev
= NULL
;
1697 struct demangle_component
*next
=
1698 d_make_comp (di
, DEMANGLE_COMPONENT_STRUCTURED_BINDING
,
1699 d_source_name (di
), NULL
);
1701 d_right (prev
) = next
;
1706 while (prev
&& d_peek_char (di
) != 'E');
1712 else if (peek
== 'C' || peek
== 'D')
1713 ret
= d_ctor_dtor_name (di
);
1714 else if (peek
== 'L')
1718 ret
= d_source_name (di
);
1721 if (! d_discriminator (di
))
1724 else if (peek
== 'U')
1726 switch (d_peek_next_char (di
))
1729 ret
= d_lambda (di
);
1732 ret
= d_unnamed_type (di
);
1742 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_MODULE_ENTITY
, ret
, module
);
1743 if (d_peek_char (di
) == 'B')
1744 ret
= d_abi_tags (di
, ret
);
1746 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
, scope
, ret
);
1751 /* <source-name> ::= <(positive length) number> <identifier> */
1753 static struct demangle_component
*
1754 d_source_name (struct d_info
*di
)
1757 struct demangle_component
*ret
;
1759 len
= d_number (di
);
1762 ret
= d_identifier (di
, len
);
1763 di
->last_name
= ret
;
1767 /* number ::= [n] <(non-negative decimal integer)> */
1770 d_number (struct d_info
*di
)
1777 peek
= d_peek_char (di
);
1782 peek
= d_peek_char (di
);
1788 if (! IS_DIGIT (peek
))
1794 if (ret
> ((INT_MAX
- (peek
- '0')) / 10))
1796 ret
= ret
* 10 + (peek
- '0');
1798 peek
= d_peek_char (di
);
1802 /* Like d_number, but returns a demangle_component. */
1804 static struct demangle_component
*
1805 d_number_component (struct d_info
*di
)
1807 struct demangle_component
*ret
= d_make_empty (di
);
1810 ret
->type
= DEMANGLE_COMPONENT_NUMBER
;
1811 ret
->u
.s_number
.number
= d_number (di
);
1816 /* identifier ::= <(unqualified source code identifier)> */
1818 static struct demangle_component
*
1819 d_identifier (struct d_info
*di
, int len
)
1825 if (di
->send
- name
< len
)
1828 d_advance (di
, len
);
1830 /* A Java mangled name may have a trailing '$' if it is a C++
1831 keyword. This '$' is not included in the length count. We just
1833 if ((di
->options
& DMGL_JAVA
) != 0
1834 && d_peek_char (di
) == '$')
1837 /* Look for something which looks like a gcc encoding of an
1838 anonymous namespace, and replace it with a more user friendly
1840 if (len
>= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN
+ 2
1841 && memcmp (name
, ANONYMOUS_NAMESPACE_PREFIX
,
1842 ANONYMOUS_NAMESPACE_PREFIX_LEN
) == 0)
1846 s
= name
+ ANONYMOUS_NAMESPACE_PREFIX_LEN
;
1847 if ((*s
== '.' || *s
== '_' || *s
== '$')
1850 di
->expansion
-= len
- sizeof "(anonymous namespace)";
1851 return d_make_name (di
, "(anonymous namespace)",
1852 sizeof "(anonymous namespace)" - 1);
1856 return d_make_name (di
, name
, len
);
1859 /* operator_name ::= many different two character encodings.
1861 ::= v <digit> <source-name>
1863 This list is sorted for binary search. */
1865 #define NL(s) s, (sizeof s) - 1
1867 CP_STATIC_IF_GLIBCPP_V3
1868 const struct demangle_operator_info cplus_demangle_operators
[] =
1870 { "aN", NL ("&="), 2 },
1871 { "aS", NL ("="), 2 },
1872 { "aa", NL ("&&"), 2 },
1873 { "ad", NL ("&"), 1 },
1874 { "an", NL ("&"), 2 },
1875 { "at", NL ("alignof "), 1 },
1876 { "aw", NL ("co_await "), 1 },
1877 { "az", NL ("alignof "), 1 },
1878 { "cc", NL ("const_cast"), 2 },
1879 { "cl", NL ("()"), 2 },
1880 { "cm", NL (","), 2 },
1881 { "co", NL ("~"), 1 },
1882 { "dV", NL ("/="), 2 },
1883 { "dX", NL ("[...]="), 3 }, /* [expr...expr] = expr */
1884 { "da", NL ("delete[] "), 1 },
1885 { "dc", NL ("dynamic_cast"), 2 },
1886 { "de", NL ("*"), 1 },
1887 { "di", NL ("="), 2 }, /* .name = expr */
1888 { "dl", NL ("delete "), 1 },
1889 { "ds", NL (".*"), 2 },
1890 { "dt", NL ("."), 2 },
1891 { "dv", NL ("/"), 2 },
1892 { "dx", NL ("]="), 2 }, /* [expr] = expr */
1893 { "eO", NL ("^="), 2 },
1894 { "eo", NL ("^"), 2 },
1895 { "eq", NL ("=="), 2 },
1896 { "fL", NL ("..."), 3 },
1897 { "fR", NL ("..."), 3 },
1898 { "fl", NL ("..."), 2 },
1899 { "fr", NL ("..."), 2 },
1900 { "ge", NL (">="), 2 },
1901 { "gs", NL ("::"), 1 },
1902 { "gt", NL (">"), 2 },
1903 { "ix", NL ("[]"), 2 },
1904 { "lS", NL ("<<="), 2 },
1905 { "le", NL ("<="), 2 },
1906 { "li", NL ("operator\"\" "), 1 },
1907 { "ls", NL ("<<"), 2 },
1908 { "lt", NL ("<"), 2 },
1909 { "mI", NL ("-="), 2 },
1910 { "mL", NL ("*="), 2 },
1911 { "mi", NL ("-"), 2 },
1912 { "ml", NL ("*"), 2 },
1913 { "mm", NL ("--"), 1 },
1914 { "na", NL ("new[]"), 3 },
1915 { "ne", NL ("!="), 2 },
1916 { "ng", NL ("-"), 1 },
1917 { "nt", NL ("!"), 1 },
1918 { "nw", NL ("new"), 3 },
1919 { "oR", NL ("|="), 2 },
1920 { "oo", NL ("||"), 2 },
1921 { "or", NL ("|"), 2 },
1922 { "pL", NL ("+="), 2 },
1923 { "pl", NL ("+"), 2 },
1924 { "pm", NL ("->*"), 2 },
1925 { "pp", NL ("++"), 1 },
1926 { "ps", NL ("+"), 1 },
1927 { "pt", NL ("->"), 2 },
1928 { "qu", NL ("?"), 3 },
1929 { "rM", NL ("%="), 2 },
1930 { "rS", NL (">>="), 2 },
1931 { "rc", NL ("reinterpret_cast"), 2 },
1932 { "rm", NL ("%"), 2 },
1933 { "rs", NL (">>"), 2 },
1934 { "sP", NL ("sizeof..."), 1 },
1935 { "sZ", NL ("sizeof..."), 1 },
1936 { "sc", NL ("static_cast"), 2 },
1937 { "ss", NL ("<=>"), 2 },
1938 { "st", NL ("sizeof "), 1 },
1939 { "sz", NL ("sizeof "), 1 },
1940 { "tr", NL ("throw"), 0 },
1941 { "tw", NL ("throw "), 1 },
1942 { NULL
, NULL
, 0, 0 }
1945 static struct demangle_component
*
1946 d_operator_name (struct d_info
*di
)
1951 c1
= d_next_char (di
);
1952 c2
= d_next_char (di
);
1953 if (c1
== 'v' && IS_DIGIT (c2
))
1954 return d_make_extended_operator (di
, c2
- '0', d_source_name (di
));
1955 else if (c1
== 'c' && c2
== 'v')
1957 struct demangle_component
*type
;
1958 int was_conversion
= di
->is_conversion
;
1959 struct demangle_component
*res
;
1961 di
->is_conversion
= ! di
->is_expression
;
1962 type
= cplus_demangle_type (di
);
1963 if (di
->is_conversion
)
1964 res
= d_make_comp (di
, DEMANGLE_COMPONENT_CONVERSION
, type
, NULL
);
1966 res
= d_make_comp (di
, DEMANGLE_COMPONENT_CAST
, type
, NULL
);
1967 di
->is_conversion
= was_conversion
;
1972 /* LOW is the inclusive lower bound. */
1974 /* HIGH is the exclusive upper bound. We subtract one to ignore
1975 the sentinel at the end of the array. */
1976 int high
= ((sizeof (cplus_demangle_operators
)
1977 / sizeof (cplus_demangle_operators
[0]))
1983 const struct demangle_operator_info
*p
;
1985 i
= low
+ (high
- low
) / 2;
1986 p
= cplus_demangle_operators
+ i
;
1988 if (c1
== p
->code
[0] && c2
== p
->code
[1])
1989 return d_make_operator (di
, p
);
1991 if (c1
< p
->code
[0] || (c1
== p
->code
[0] && c2
< p
->code
[1]))
2001 static struct demangle_component
*
2002 d_make_character (struct d_info
*di
, int c
)
2004 struct demangle_component
*p
;
2005 p
= d_make_empty (di
);
2008 p
->type
= DEMANGLE_COMPONENT_CHARACTER
;
2009 p
->u
.s_character
.character
= c
;
2014 static struct demangle_component
*
2015 d_java_resource (struct d_info
*di
)
2017 struct demangle_component
*p
= NULL
;
2018 struct demangle_component
*next
= NULL
;
2023 len
= d_number (di
);
2027 /* Eat the leading '_'. */
2028 if (d_next_char (di
) != '_')
2041 /* Each chunk is either a '$' escape... */
2059 next
= d_make_character (di
, c
);
2067 /* ... or a sequence of characters. */
2070 while (i
< len
&& str
[i
] && str
[i
] != '$')
2073 next
= d_make_name (di
, str
, i
);
2086 p
= d_make_comp (di
, DEMANGLE_COMPONENT_COMPOUND_NAME
, p
, next
);
2092 p
= d_make_comp (di
, DEMANGLE_COMPONENT_JAVA_RESOURCE
, p
, NULL
);
2097 /* <special-name> ::= TV <type>
2101 ::= TA <template-arg>
2102 ::= GV <(object) name>
2103 ::= T <call-offset> <(base) encoding>
2104 ::= Tc <call-offset> <call-offset> <(base) encoding>
2105 Also g++ extensions:
2106 ::= TC <type> <(offset) number> _ <(base) type>
2111 ::= Gr <resource name>
2116 static struct demangle_component
*
2117 d_special_name (struct d_info
*di
)
2119 di
->expansion
+= 20;
2120 if (d_check_char (di
, 'T'))
2122 switch (d_next_char (di
))
2126 return d_make_comp (di
, DEMANGLE_COMPONENT_VTABLE
,
2127 cplus_demangle_type (di
), NULL
);
2129 di
->expansion
-= 10;
2130 return d_make_comp (di
, DEMANGLE_COMPONENT_VTT
,
2131 cplus_demangle_type (di
), NULL
);
2133 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO
,
2134 cplus_demangle_type (di
), NULL
);
2136 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO_NAME
,
2137 cplus_demangle_type (di
), NULL
);
2140 if (! d_call_offset (di
, 'h'))
2142 return d_make_comp (di
, DEMANGLE_COMPONENT_THUNK
,
2143 d_encoding (di
, 0), NULL
);
2146 if (! d_call_offset (di
, 'v'))
2148 return d_make_comp (di
, DEMANGLE_COMPONENT_VIRTUAL_THUNK
,
2149 d_encoding (di
, 0), NULL
);
2152 if (! d_call_offset (di
, '\0'))
2154 if (! d_call_offset (di
, '\0'))
2156 return d_make_comp (di
, DEMANGLE_COMPONENT_COVARIANT_THUNK
,
2157 d_encoding (di
, 0), NULL
);
2161 struct demangle_component
*derived_type
;
2163 struct demangle_component
*base_type
;
2165 derived_type
= cplus_demangle_type (di
);
2166 offset
= d_number (di
);
2169 if (! d_check_char (di
, '_'))
2171 base_type
= cplus_demangle_type (di
);
2172 /* We don't display the offset. FIXME: We should display
2173 it in verbose mode. */
2175 return d_make_comp (di
, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
,
2176 base_type
, derived_type
);
2180 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO_FN
,
2181 cplus_demangle_type (di
), NULL
);
2183 return d_make_comp (di
, DEMANGLE_COMPONENT_JAVA_CLASS
,
2184 cplus_demangle_type (di
), NULL
);
2187 return d_make_comp (di
, DEMANGLE_COMPONENT_TLS_INIT
,
2188 d_name (di
, 0), NULL
);
2191 return d_make_comp (di
, DEMANGLE_COMPONENT_TLS_WRAPPER
,
2192 d_name (di
, 0), NULL
);
2195 return d_make_comp (di
, DEMANGLE_COMPONENT_TPARM_OBJ
,
2196 d_template_arg (di
), NULL
);
2202 else if (d_check_char (di
, 'G'))
2204 switch (d_next_char (di
))
2207 return d_make_comp (di
, DEMANGLE_COMPONENT_GUARD
,
2208 d_name (di
, 0), NULL
);
2212 struct demangle_component
*name
= d_name (di
, 0);
2213 return d_make_comp (di
, DEMANGLE_COMPONENT_REFTEMP
, name
,
2214 d_number_component (di
));
2218 return d_make_comp (di
, DEMANGLE_COMPONENT_HIDDEN_ALIAS
,
2219 d_encoding (di
, 0), NULL
);
2223 struct demangle_component
*module
= NULL
;
2224 if (!d_maybe_module_name (di
, &module
) || !module
)
2226 return d_make_comp (di
, DEMANGLE_COMPONENT_MODULE_INIT
,
2230 switch (d_next_char (di
))
2233 return d_make_comp (di
, DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
,
2234 d_encoding (di
, 0), NULL
);
2236 /* ??? The proposal is that other letters (such as 'h') stand
2237 for different variants of transaction cloning, such as
2238 compiling directly for hardware transaction support. But
2239 they still should all be transactional clones of some sort
2240 so go ahead and call them that. */
2242 return d_make_comp (di
, DEMANGLE_COMPONENT_TRANSACTION_CLONE
,
2243 d_encoding (di
, 0), NULL
);
2247 return d_java_resource (di
);
2257 /* <call-offset> ::= h <nv-offset> _
2260 <nv-offset> ::= <(offset) number>
2262 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
2264 The C parameter, if not '\0', is a character we just read which is
2265 the start of the <call-offset>.
2267 We don't display the offset information anywhere. FIXME: We should
2268 display it in verbose mode. */
2271 d_call_offset (struct d_info
*di
, int c
)
2274 c
= d_next_char (di
);
2281 if (! d_check_char (di
, '_'))
2288 if (! d_check_char (di
, '_'))
2294 /* <ctor-dtor-name> ::= C1
2302 static struct demangle_component
*
2303 d_ctor_dtor_name (struct d_info
*di
)
2305 if (di
->last_name
!= NULL
)
2307 if (di
->last_name
->type
== DEMANGLE_COMPONENT_NAME
)
2308 di
->expansion
+= di
->last_name
->u
.s_name
.len
;
2309 else if (di
->last_name
->type
== DEMANGLE_COMPONENT_SUB_STD
)
2310 di
->expansion
+= di
->last_name
->u
.s_string
.len
;
2312 switch (d_peek_char (di
))
2316 enum gnu_v3_ctor_kinds kind
;
2319 if (d_peek_next_char (di
) == 'I')
2325 switch (d_peek_next_char (di
))
2328 kind
= gnu_v3_complete_object_ctor
;
2331 kind
= gnu_v3_base_object_ctor
;
2334 kind
= gnu_v3_complete_object_allocating_ctor
;
2337 kind
= gnu_v3_unified_ctor
;
2340 kind
= gnu_v3_object_ctor_group
;
2349 cplus_demangle_type (di
);
2351 return d_make_ctor (di
, kind
, di
->last_name
);
2356 enum gnu_v3_dtor_kinds kind
;
2358 switch (d_peek_next_char (di
))
2361 kind
= gnu_v3_deleting_dtor
;
2364 kind
= gnu_v3_complete_object_dtor
;
2367 kind
= gnu_v3_base_object_dtor
;
2369 /* digit '3' is not used */
2371 kind
= gnu_v3_unified_dtor
;
2374 kind
= gnu_v3_object_dtor_group
;
2380 return d_make_dtor (di
, kind
, di
->last_name
);
2388 /* True iff we're looking at an order-insensitive type-qualifier, including
2389 function-type-qualifiers. */
2392 next_is_type_qual (struct d_info
*di
)
2394 char peek
= d_peek_char (di
);
2395 if (peek
== 'r' || peek
== 'V' || peek
== 'K')
2399 peek
= d_peek_next_char (di
);
2400 if (peek
== 'x' || peek
== 'o' || peek
== 'O' || peek
== 'w')
2406 /* <type> ::= <builtin-type>
2408 ::= <class-enum-type>
2410 ::= <pointer-to-member-type>
2411 ::= <template-param>
2412 ::= <template-template-param> <template-args>
2414 ::= <CV-qualifiers> <type>
2417 ::= O <type> (C++0x)
2420 ::= U <source-name> <type>
2422 <builtin-type> ::= various one letter codes
2426 CP_STATIC_IF_GLIBCPP_V3
2427 const struct demangle_builtin_type_info
2428 cplus_demangle_builtin_types
[D_BUILTIN_TYPE_COUNT
] =
2430 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT
},
2431 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL
},
2432 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT
},
2433 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT
},
2434 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT
},
2435 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT
},
2436 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT
},
2437 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT
},
2438 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT
},
2439 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED
},
2440 /* k */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2441 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG
},
2442 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG
},
2443 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT
},
2444 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
2446 /* p */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2447 /* q */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2448 /* r */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2449 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT
},
2450 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT
},
2451 /* u */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2452 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID
},
2453 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT
},
2454 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG
},
2455 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
2456 D_PRINT_UNSIGNED_LONG_LONG
},
2457 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT
},
2458 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT
},
2459 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT
},
2460 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT
},
2461 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT
},
2462 /* 30 */ { NL ("char8_t"), NL ("char8_t"), D_PRINT_DEFAULT
},
2463 /* 31 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT
},
2464 /* 32 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT
},
2465 /* 33 */ { NL ("decltype(nullptr)"), NL ("decltype(nullptr)"),
2469 CP_STATIC_IF_GLIBCPP_V3
2470 struct demangle_component
*
2471 cplus_demangle_type (struct d_info
*di
)
2474 struct demangle_component
*ret
;
2477 /* The ABI specifies that when CV-qualifiers are used, the base type
2478 is substitutable, and the fully qualified type is substitutable,
2479 but the base type with a strict subset of the CV-qualifiers is
2480 not substitutable. The natural recursive implementation of the
2481 CV-qualifiers would cause subsets to be substitutable, so instead
2482 we pull them all off now.
2484 FIXME: The ABI says that order-insensitive vendor qualifiers
2485 should be handled in the same way, but we have no way to tell
2486 which vendor qualifiers are order-insensitive and which are
2487 order-sensitive. So we just assume that they are all
2488 order-sensitive. g++ 3.4 supports only one vendor qualifier,
2489 __vector, and it treats it as order-sensitive when mangling
2492 if (next_is_type_qual (di
))
2494 struct demangle_component
**pret
;
2496 pret
= d_cv_qualifiers (di
, &ret
, 0);
2499 if (d_peek_char (di
) == 'F')
2501 /* cv-qualifiers before a function type apply to 'this',
2502 so avoid adding the unqualified function type to
2503 the substitution list. */
2504 *pret
= d_function_type (di
);
2507 *pret
= cplus_demangle_type (di
);
2510 if ((*pret
)->type
== DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
2511 || (*pret
)->type
== DEMANGLE_COMPONENT_REFERENCE_THIS
)
2513 /* Move the ref-qualifier outside the cv-qualifiers so that
2514 they are printed in the right order. */
2515 struct demangle_component
*fn
= d_left (*pret
);
2516 d_left (*pret
) = ret
;
2520 if (! d_add_substitution (di
, ret
))
2527 peek
= d_peek_char (di
);
2530 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2531 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
2532 case 'o': case 's': case 't':
2533 case 'v': case 'w': case 'x': case 'y': case 'z':
2534 ret
= d_make_builtin_type (di
,
2535 &cplus_demangle_builtin_types
[peek
- 'a']);
2536 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2543 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_VENDOR_TYPE
,
2544 d_source_name (di
), NULL
);
2548 ret
= d_function_type (di
);
2552 ret
= d_array_type (di
);
2556 ret
= d_pointer_to_member_type (di
);
2560 ret
= d_template_param (di
);
2561 if (d_peek_char (di
) == 'I')
2563 /* This may be <template-template-param> <template-args>.
2564 If this is the type for a conversion operator, we can
2565 have a <template-template-param> here only by following
2566 a derivation like this:
2569 -> <template-prefix> <template-args>
2570 -> <prefix> <template-unqualified-name> <template-args>
2571 -> <unqualified-name> <template-unqualified-name> <template-args>
2572 -> <source-name> <template-unqualified-name> <template-args>
2573 -> <source-name> <operator-name> <template-args>
2574 -> <source-name> cv <type> <template-args>
2575 -> <source-name> cv <template-template-param> <template-args> <template-args>
2577 where the <template-args> is followed by another.
2578 Otherwise, we must have a derivation like this:
2581 -> <template-prefix> <template-args>
2582 -> <prefix> <template-unqualified-name> <template-args>
2583 -> <unqualified-name> <template-unqualified-name> <template-args>
2584 -> <source-name> <template-unqualified-name> <template-args>
2585 -> <source-name> <operator-name> <template-args>
2586 -> <source-name> cv <type> <template-args>
2587 -> <source-name> cv <template-param> <template-args>
2589 where we need to leave the <template-args> to be processed
2590 by d_prefix (following the <template-prefix>).
2592 The <template-template-param> part is a substitution
2594 if (! di
->is_conversion
)
2596 if (! d_add_substitution (di
, ret
))
2598 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
,
2599 d_template_args (di
));
2603 struct demangle_component
*args
;
2604 struct d_info_checkpoint checkpoint
;
2606 d_checkpoint (di
, &checkpoint
);
2607 args
= d_template_args (di
);
2608 if (d_peek_char (di
) == 'I')
2610 if (! d_add_substitution (di
, ret
))
2612 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
,
2616 d_backtrack (di
, &checkpoint
);
2623 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_RVALUE_REFERENCE
,
2624 cplus_demangle_type (di
), NULL
);
2629 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_POINTER
,
2630 cplus_demangle_type (di
), NULL
);
2635 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_REFERENCE
,
2636 cplus_demangle_type (di
), NULL
);
2641 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_COMPLEX
,
2642 cplus_demangle_type (di
), NULL
);
2647 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_IMAGINARY
,
2648 cplus_demangle_type (di
), NULL
);
2653 ret
= d_source_name (di
);
2654 if (d_peek_char (di
) == 'I')
2655 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
,
2656 d_template_args (di
));
2657 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
,
2658 cplus_demangle_type (di
), ret
);
2664 peek
= d_next_char (di
);
2669 /* decltype (expression) */
2670 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_DECLTYPE
,
2671 d_expression (di
), NULL
);
2672 if (ret
&& d_next_char (di
) != 'E')
2678 /* Pack expansion. */
2679 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_PACK_EXPANSION
,
2680 cplus_demangle_type (di
), NULL
);
2686 ret
= d_make_name (di
, "auto", 4);
2689 /* decltype(auto) */
2690 ret
= d_make_name (di
, "decltype(auto)", 14);
2694 /* 32-bit decimal floating point */
2695 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[26]);
2696 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2700 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[27]);
2701 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2705 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[28]);
2706 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2709 /* 16-bit half-precision FP */
2710 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[29]);
2711 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2715 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[30]);
2716 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2720 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[31]);
2721 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2725 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[32]);
2726 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2730 /* Fixed point types. DF<int bits><length><fract bits><sat> */
2731 ret
= d_make_empty (di
);
2732 ret
->type
= DEMANGLE_COMPONENT_FIXED_TYPE
;
2733 if ((ret
->u
.s_fixed
.accum
= IS_DIGIT (d_peek_char (di
))))
2734 /* For demangling we don't care about the bits. */
2736 ret
->u
.s_fixed
.length
= cplus_demangle_type (di
);
2737 if (ret
->u
.s_fixed
.length
== NULL
)
2740 peek
= d_next_char (di
);
2741 ret
->u
.s_fixed
.sat
= (peek
== 's');
2745 ret
= d_vector_type (di
);
2750 /* decltype(nullptr) */
2751 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[33]);
2752 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2761 return d_class_enum_type (di
, 1);
2766 if (! d_add_substitution (di
, ret
))
2773 /* <CV-qualifiers> ::= [r] [V] [K] [Dx] */
2775 static struct demangle_component
**
2776 d_cv_qualifiers (struct d_info
*di
,
2777 struct demangle_component
**pret
, int member_fn
)
2779 struct demangle_component
**pstart
;
2783 peek
= d_peek_char (di
);
2784 while (next_is_type_qual (di
))
2786 enum demangle_component_type t
;
2787 struct demangle_component
*right
= NULL
;
2793 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2794 : DEMANGLE_COMPONENT_RESTRICT
);
2795 di
->expansion
+= sizeof "restrict";
2797 else if (peek
== 'V')
2800 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2801 : DEMANGLE_COMPONENT_VOLATILE
);
2802 di
->expansion
+= sizeof "volatile";
2804 else if (peek
== 'K')
2807 ? DEMANGLE_COMPONENT_CONST_THIS
2808 : DEMANGLE_COMPONENT_CONST
);
2809 di
->expansion
+= sizeof "const";
2813 peek
= d_next_char (di
);
2816 t
= DEMANGLE_COMPONENT_TRANSACTION_SAFE
;
2817 di
->expansion
+= sizeof "transaction_safe";
2819 else if (peek
== 'o'
2822 t
= DEMANGLE_COMPONENT_NOEXCEPT
;
2823 di
->expansion
+= sizeof "noexcept";
2826 right
= d_expression (di
);
2829 if (! d_check_char (di
, 'E'))
2833 else if (peek
== 'w')
2835 t
= DEMANGLE_COMPONENT_THROW_SPEC
;
2836 di
->expansion
+= sizeof "throw";
2837 right
= d_parmlist (di
);
2840 if (! d_check_char (di
, 'E'))
2847 *pret
= d_make_comp (di
, t
, NULL
, right
);
2850 pret
= &d_left (*pret
);
2852 peek
= d_peek_char (di
);
2855 if (!member_fn
&& peek
== 'F')
2857 while (pstart
!= pret
)
2859 switch ((*pstart
)->type
)
2861 case DEMANGLE_COMPONENT_RESTRICT
:
2862 (*pstart
)->type
= DEMANGLE_COMPONENT_RESTRICT_THIS
;
2864 case DEMANGLE_COMPONENT_VOLATILE
:
2865 (*pstart
)->type
= DEMANGLE_COMPONENT_VOLATILE_THIS
;
2867 case DEMANGLE_COMPONENT_CONST
:
2868 (*pstart
)->type
= DEMANGLE_COMPONENT_CONST_THIS
;
2873 pstart
= &d_left (*pstart
);
2880 /* <ref-qualifier> ::= R
2883 static struct demangle_component
*
2884 d_ref_qualifier (struct d_info
*di
, struct demangle_component
*sub
)
2886 struct demangle_component
*ret
= sub
;
2889 peek
= d_peek_char (di
);
2890 if (peek
== 'R' || peek
== 'O')
2892 enum demangle_component_type t
;
2895 t
= DEMANGLE_COMPONENT_REFERENCE_THIS
;
2896 di
->expansion
+= sizeof "&";
2900 t
= DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
;
2901 di
->expansion
+= sizeof "&&";
2905 ret
= d_make_comp (di
, t
, ret
, NULL
);
2911 /* <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] [T] E */
2913 static struct demangle_component
*
2914 d_function_type (struct d_info
*di
)
2916 struct demangle_component
*ret
= NULL
;
2918 if ((di
->options
& DMGL_NO_RECURSE_LIMIT
) == 0)
2920 if (di
->recursion_level
> DEMANGLE_RECURSION_LIMIT
)
2921 /* FIXME: There ought to be a way to report
2922 that the recursion limit has been reached. */
2925 di
->recursion_level
++;
2928 if (d_check_char (di
, 'F'))
2930 if (d_peek_char (di
) == 'Y')
2932 /* Function has C linkage. We don't print this information.
2933 FIXME: We should print it in verbose mode. */
2936 ret
= d_bare_function_type (di
, 1);
2937 ret
= d_ref_qualifier (di
, ret
);
2939 if (! d_check_char (di
, 'E'))
2943 if ((di
->options
& DMGL_NO_RECURSE_LIMIT
) == 0)
2944 di
->recursion_level
--;
2950 static struct demangle_component
*
2951 d_parmlist (struct d_info
*di
)
2953 struct demangle_component
*tl
;
2954 struct demangle_component
**ptl
;
2960 struct demangle_component
*type
;
2962 char peek
= d_peek_char (di
);
2963 if (peek
== '\0' || peek
== 'E' || peek
== '.')
2965 if ((peek
== 'R' || peek
== 'O')
2966 && d_peek_next_char (di
) == 'E')
2967 /* Function ref-qualifier, not a ref prefix for a parameter type. */
2969 type
= cplus_demangle_type (di
);
2972 *ptl
= d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, type
, NULL
);
2975 ptl
= &d_right (*ptl
);
2978 /* There should be at least one parameter type besides the optional
2979 return type. A function which takes no arguments will have a
2980 single parameter type void. */
2984 /* If we have a single parameter type void, omit it. */
2985 if (d_right (tl
) == NULL
2986 && d_left (tl
)->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
2987 && d_left (tl
)->u
.s_builtin
.type
->print
== D_PRINT_VOID
)
2989 di
->expansion
-= d_left (tl
)->u
.s_builtin
.type
->len
;
2996 /* <bare-function-type> ::= [J]<type>+ */
2998 static struct demangle_component
*
2999 d_bare_function_type (struct d_info
*di
, int has_return_type
)
3001 struct demangle_component
*return_type
;
3002 struct demangle_component
*tl
;
3005 /* Detect special qualifier indicating that the first argument
3006 is the return type. */
3007 peek
= d_peek_char (di
);
3011 has_return_type
= 1;
3014 if (has_return_type
)
3016 return_type
= cplus_demangle_type (di
);
3017 if (return_type
== NULL
)
3023 tl
= d_parmlist (di
);
3027 return d_make_comp (di
, DEMANGLE_COMPONENT_FUNCTION_TYPE
,
3031 /* <class-enum-type> ::= <name> */
3033 static struct demangle_component
*
3034 d_class_enum_type (struct d_info
*di
, int substable
)
3036 return d_name (di
, substable
);
3039 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
3040 ::= A [<(dimension) expression>] _ <(element) type>
3043 static struct demangle_component
*
3044 d_array_type (struct d_info
*di
)
3047 struct demangle_component
*dim
;
3049 if (! d_check_char (di
, 'A'))
3052 peek
= d_peek_char (di
);
3055 else if (IS_DIGIT (peek
))
3063 peek
= d_peek_char (di
);
3065 while (IS_DIGIT (peek
));
3066 dim
= d_make_name (di
, s
, d_str (di
) - s
);
3072 dim
= d_expression (di
);
3077 if (! d_check_char (di
, '_'))
3080 return d_make_comp (di
, DEMANGLE_COMPONENT_ARRAY_TYPE
, dim
,
3081 cplus_demangle_type (di
));
3084 /* <vector-type> ::= Dv <number> _ <type>
3085 ::= Dv _ <expression> _ <type> */
3087 static struct demangle_component
*
3088 d_vector_type (struct d_info
*di
)
3091 struct demangle_component
*dim
;
3093 peek
= d_peek_char (di
);
3097 dim
= d_expression (di
);
3100 dim
= d_number_component (di
);
3105 if (! d_check_char (di
, '_'))
3108 return d_make_comp (di
, DEMANGLE_COMPONENT_VECTOR_TYPE
, dim
,
3109 cplus_demangle_type (di
));
3112 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
3114 static struct demangle_component
*
3115 d_pointer_to_member_type (struct d_info
*di
)
3117 struct demangle_component
*cl
;
3118 struct demangle_component
*mem
;
3120 if (! d_check_char (di
, 'M'))
3123 cl
= cplus_demangle_type (di
);
3127 /* The ABI says, "The type of a non-static member function is considered
3128 to be different, for the purposes of substitution, from the type of a
3129 namespace-scope or static member function whose type appears
3130 similar. The types of two non-static member functions are considered
3131 to be different, for the purposes of substitution, if the functions
3132 are members of different classes. In other words, for the purposes of
3133 substitution, the class of which the function is a member is
3134 considered part of the type of function."
3136 For a pointer to member function, this call to cplus_demangle_type
3137 will end up adding a (possibly qualified) non-member function type to
3138 the substitution table, which is not correct; however, the member
3139 function type will never be used in a substitution, so putting the
3140 wrong type in the substitution table is harmless. */
3142 mem
= cplus_demangle_type (di
);
3146 return d_make_comp (di
, DEMANGLE_COMPONENT_PTRMEM_TYPE
, cl
, mem
);
3149 /* <non-negative number> _ */
3152 d_compact_number (struct d_info
*di
)
3155 if (d_peek_char (di
) == '_')
3157 else if (d_peek_char (di
) == 'n')
3160 num
= d_number (di
) + 1;
3162 if (num
< 0 || ! d_check_char (di
, '_'))
3167 /* <template-param> ::= T_
3168 ::= T <(parameter-2 non-negative) number> _
3171 static struct demangle_component
*
3172 d_template_param (struct d_info
*di
)
3176 if (! d_check_char (di
, 'T'))
3179 param
= d_compact_number (di
);
3183 return d_make_template_param (di
, param
);
3186 /* <template-args> ::= I <template-arg>+ E */
3188 static struct demangle_component
*
3189 d_template_args (struct d_info
*di
)
3191 if (d_peek_char (di
) != 'I'
3192 && d_peek_char (di
) != 'J')
3196 return d_template_args_1 (di
);
3199 /* <template-arg>* E */
3201 static struct demangle_component
*
3202 d_template_args_1 (struct d_info
*di
)
3204 struct demangle_component
*hold_last_name
;
3205 struct demangle_component
*al
;
3206 struct demangle_component
**pal
;
3208 /* Preserve the last name we saw--don't let the template arguments
3209 clobber it, as that would give us the wrong name for a subsequent
3210 constructor or destructor. */
3211 hold_last_name
= di
->last_name
;
3213 if (d_peek_char (di
) == 'E')
3215 /* An argument pack can be empty. */
3217 return d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
, NULL
, NULL
);
3224 struct demangle_component
*a
;
3226 a
= d_template_arg (di
);
3230 *pal
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
, a
, NULL
);
3233 pal
= &d_right (*pal
);
3235 if (d_peek_char (di
) == 'E')
3242 di
->last_name
= hold_last_name
;
3247 /* <template-arg> ::= <type>
3248 ::= X <expression> E
3252 static struct demangle_component
*
3253 d_template_arg (struct d_info
*di
)
3255 struct demangle_component
*ret
;
3257 switch (d_peek_char (di
))
3261 ret
= d_expression (di
);
3262 if (! d_check_char (di
, 'E'))
3267 return d_expr_primary (di
);
3271 /* An argument pack. */
3272 return d_template_args (di
);
3275 return cplus_demangle_type (di
);
3279 /* Parse a sequence of expressions until we hit the terminator
3282 static struct demangle_component
*
3283 d_exprlist (struct d_info
*di
, char terminator
)
3285 struct demangle_component
*list
= NULL
;
3286 struct demangle_component
**p
= &list
;
3288 if (d_peek_char (di
) == terminator
)
3291 return d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, NULL
, NULL
);
3296 struct demangle_component
*arg
= d_expression (di
);
3300 *p
= d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, arg
, NULL
);
3305 if (d_peek_char (di
) == terminator
)
3315 /* Returns nonzero iff OP is an operator for a C++ cast: const_cast,
3316 dynamic_cast, static_cast or reinterpret_cast. */
3319 op_is_new_cast (struct demangle_component
*op
)
3321 const char *code
= op
->u
.s_operator
.op
->code
;
3322 return (code
[1] == 'c'
3323 && (code
[0] == 's' || code
[0] == 'd'
3324 || code
[0] == 'c' || code
[0] == 'r'));
3327 /* <unresolved-name> ::= [gs] <base-unresolved-name> # x or (with "gs") ::x
3328 ::= sr <unresolved-type> <base-unresolved-name> # T::x / decltype(p)::x
3329 # T::N::x /decltype(p)::N::x
3330 ::= srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3331 # A::x, N::y, A<T>::z; "gs" means leading "::"
3332 ::= [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3334 "gs" is handled elsewhere, as a unary operator. */
3336 static struct demangle_component
*
3337 d_unresolved_name (struct d_info
*di
)
3339 struct demangle_component
*type
;
3340 struct demangle_component
*name
;
3343 /* Consume the "sr". */
3346 peek
= d_peek_char (di
);
3347 if (di
->unresolved_name_state
3354 /* The third production is ambiguous with the old unresolved-name syntax
3355 of <type> <base-unresolved-name>; in the old mangling, A::x was mangled
3356 as sr1A1x, now sr1AE1x. So we first try to demangle using the new
3357 mangling, then with the old if that fails. */
3358 di
->unresolved_name_state
= -1;
3359 type
= d_prefix (di
, 0);
3360 if (d_peek_char (di
) == 'E')
3364 type
= cplus_demangle_type (di
);
3365 name
= d_unqualified_name (di
, type
, NULL
);
3366 if (d_peek_char (di
) == 'I')
3367 name
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, name
,
3368 d_template_args (di
));
3372 /* <expression> ::= <(unary) operator-name> <expression>
3373 ::= <(binary) operator-name> <expression> <expression>
3374 ::= <(trinary) operator-name> <expression> <expression> <expression>
3375 ::= cl <expression>+ E
3377 ::= <template-param>
3378 ::= u <source-name> <template-arg>* E # vendor extended expression
3379 ::= <unresolved-name>
3382 <braced-expression> ::= <expression>
3383 ::= di <field source-name> <braced-expression> # .name = expr
3384 ::= dx <index expression> <braced-expression> # [expr] = expr
3385 ::= dX <range begin expression> <range end expression> <braced-expression>
3386 # [expr ... expr] = expr
3389 static struct demangle_component
*
3390 d_expression_1 (struct d_info
*di
)
3394 peek
= d_peek_char (di
);
3396 return d_expr_primary (di
);
3397 else if (peek
== 'T')
3398 return d_template_param (di
);
3399 else if (peek
== 's' && d_peek_next_char (di
) == 'r')
3400 return d_unresolved_name (di
);
3401 else if (peek
== 's' && d_peek_next_char (di
) == 'p')
3404 return d_make_comp (di
, DEMANGLE_COMPONENT_PACK_EXPANSION
,
3405 d_expression_1 (di
), NULL
);
3407 else if (peek
== 'f' && d_peek_next_char (di
) == 'p')
3409 /* Function parameter used in a late-specified return type. */
3412 if (d_peek_char (di
) == 'T')
3414 /* 'this' parameter. */
3420 index
= d_compact_number (di
);
3421 if (index
== INT_MAX
|| index
== -1)
3425 return d_make_function_param (di
, index
);
3427 else if (IS_DIGIT (peek
)
3428 || (peek
== 'o' && d_peek_next_char (di
) == 'n'))
3430 /* We can get an unqualified name as an expression in the case of
3431 a dependent function call, i.e. decltype(f(t)). */
3432 struct demangle_component
*name
;
3435 /* operator-function-id, i.e. operator+(t). */
3438 name
= d_unqualified_name (di
, NULL
, NULL
);
3441 if (d_peek_char (di
) == 'I')
3442 return d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, name
,
3443 d_template_args (di
));
3447 else if ((peek
== 'i' || peek
== 't')
3448 && d_peek_next_char (di
) == 'l')
3450 /* Brace-enclosed initializer list, untyped or typed. */
3451 struct demangle_component
*type
= NULL
;
3454 type
= cplus_demangle_type (di
);
3455 if (!d_peek_char (di
) || !d_peek_next_char (di
))
3457 return d_make_comp (di
, DEMANGLE_COMPONENT_INITIALIZER_LIST
,
3458 type
, d_exprlist (di
, 'E'));
3460 else if (peek
== 'u')
3462 /* A vendor extended expression. */
3463 struct demangle_component
*name
, *args
;
3465 name
= d_source_name (di
);
3466 args
= d_template_args_1 (di
);
3467 return d_make_comp (di
, DEMANGLE_COMPONENT_VENDOR_EXPR
, name
, args
);
3471 struct demangle_component
*op
;
3472 const char *code
= NULL
;
3475 op
= d_operator_name (di
);
3479 if (op
->type
== DEMANGLE_COMPONENT_OPERATOR
)
3481 code
= op
->u
.s_operator
.op
->code
;
3482 di
->expansion
+= op
->u
.s_operator
.op
->len
- 2;
3483 if (strcmp (code
, "st") == 0)
3484 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
3485 cplus_demangle_type (di
));
3492 case DEMANGLE_COMPONENT_OPERATOR
:
3493 args
= op
->u
.s_operator
.op
->args
;
3495 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
3496 args
= op
->u
.s_extended_operator
.args
;
3498 case DEMANGLE_COMPONENT_CAST
:
3506 return d_make_comp (di
, DEMANGLE_COMPONENT_NULLARY
, op
, NULL
);
3510 struct demangle_component
*operand
;
3513 if (code
&& (code
[0] == 'p' || code
[0] == 'm')
3514 && code
[1] == code
[0])
3515 /* pp_ and mm_ are the prefix variants. */
3516 suffix
= !d_check_char (di
, '_');
3518 if (op
->type
== DEMANGLE_COMPONENT_CAST
3519 && d_check_char (di
, '_'))
3520 operand
= d_exprlist (di
, 'E');
3521 else if (code
&& !strcmp (code
, "sP"))
3522 operand
= d_template_args_1 (di
);
3524 operand
= d_expression_1 (di
);
3527 /* Indicate the suffix variant for d_print_comp. */
3528 operand
= d_make_comp (di
, DEMANGLE_COMPONENT_BINARY_ARGS
,
3531 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
, operand
);
3535 struct demangle_component
*left
;
3536 struct demangle_component
*right
;
3540 if (op_is_new_cast (op
))
3541 left
= cplus_demangle_type (di
);
3542 else if (code
[0] == 'f')
3543 /* fold-expression. */
3544 left
= d_operator_name (di
);
3545 else if (!strcmp (code
, "di"))
3546 left
= d_unqualified_name (di
, NULL
, NULL
);
3548 left
= d_expression_1 (di
);
3549 if (!strcmp (code
, "cl"))
3550 right
= d_exprlist (di
, 'E');
3551 else if (!strcmp (code
, "dt") || !strcmp (code
, "pt"))
3553 peek
= d_peek_char (di
);
3554 /* These codes start a qualified name. */
3555 if ((peek
== 'g' && d_peek_next_char (di
) == 's')
3556 || (peek
== 's' && d_peek_next_char (di
) == 'r'))
3557 right
= d_expression_1 (di
);
3560 /* Otherwise it's an unqualified name. We use
3561 d_unqualified_name rather than d_expression_1 here for
3562 old mangled names that didn't add 'on' before operator
3564 right
= d_unqualified_name (di
, NULL
, NULL
);
3565 if (d_peek_char (di
) == 'I')
3566 right
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
,
3567 right
, d_template_args (di
));
3571 right
= d_expression_1 (di
);
3573 return d_make_comp (di
, DEMANGLE_COMPONENT_BINARY
, op
,
3575 DEMANGLE_COMPONENT_BINARY_ARGS
,
3580 struct demangle_component
*first
;
3581 struct demangle_component
*second
;
3582 struct demangle_component
*third
;
3586 else if (!strcmp (code
, "qu")
3587 || !strcmp (code
, "dX"))
3589 /* ?: expression. */
3590 first
= d_expression_1 (di
);
3591 second
= d_expression_1 (di
);
3592 third
= d_expression_1 (di
);
3596 else if (code
[0] == 'f')
3598 /* fold-expression. */
3599 first
= d_operator_name (di
);
3600 second
= d_expression_1 (di
);
3601 third
= d_expression_1 (di
);
3605 else if (code
[0] == 'n')
3607 /* new-expression. */
3608 if (code
[1] != 'w' && code
[1] != 'a')
3610 first
= d_exprlist (di
, '_');
3611 second
= cplus_demangle_type (di
);
3612 if (d_peek_char (di
) == 'E')
3617 else if (d_peek_char (di
) == 'p'
3618 && d_peek_next_char (di
) == 'i')
3620 /* Parenthesized initializer. */
3622 third
= d_exprlist (di
, 'E');
3624 else if (d_peek_char (di
) == 'i'
3625 && d_peek_next_char (di
) == 'l')
3626 /* initializer-list. */
3627 third
= d_expression_1 (di
);
3633 return d_make_comp (di
, DEMANGLE_COMPONENT_TRINARY
, op
,
3635 DEMANGLE_COMPONENT_TRINARY_ARG1
,
3638 DEMANGLE_COMPONENT_TRINARY_ARG2
,
3647 static struct demangle_component
*
3648 d_expression (struct d_info
*di
)
3650 struct demangle_component
*ret
;
3651 int was_expression
= di
->is_expression
;
3653 di
->is_expression
= 1;
3654 ret
= d_expression_1 (di
);
3655 di
->is_expression
= was_expression
;
3659 /* <expr-primary> ::= L <type> <(value) number> E
3660 ::= L <type> <(value) float> E
3661 ::= L <mangled-name> E
3664 static struct demangle_component
*
3665 d_expr_primary (struct d_info
*di
)
3667 struct demangle_component
*ret
;
3669 if (! d_check_char (di
, 'L'))
3671 if (d_peek_char (di
) == '_'
3672 /* Workaround for G++ bug; see comment in write_template_arg. */
3673 || d_peek_char (di
) == 'Z')
3674 ret
= cplus_demangle_mangled_name (di
, 0);
3677 struct demangle_component
*type
;
3678 enum demangle_component_type t
;
3681 type
= cplus_demangle_type (di
);
3685 /* If we have a type we know how to print, we aren't going to
3686 print the type name itself. */
3687 if (type
->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
3688 && type
->u
.s_builtin
.type
->print
!= D_PRINT_DEFAULT
)
3689 di
->expansion
-= type
->u
.s_builtin
.type
->len
;
3691 if (type
->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
3692 && strcmp (type
->u
.s_builtin
.type
->name
,
3693 cplus_demangle_builtin_types
[33].name
) == 0)
3695 if (d_peek_char (di
) == 'E')
3702 /* Rather than try to interpret the literal value, we just
3703 collect it as a string. Note that it's possible to have a
3704 floating point literal here. The ABI specifies that the
3705 format of such literals is machine independent. That's fine,
3706 but what's not fine is that versions of g++ up to 3.2 with
3707 -fabi-version=1 used upper case letters in the hex constant,
3708 and dumped out gcc's internal representation. That makes it
3709 hard to tell where the constant ends, and hard to dump the
3710 constant in any readable form anyhow. We don't attempt to
3711 handle these cases. */
3713 t
= DEMANGLE_COMPONENT_LITERAL
;
3714 if (d_peek_char (di
) == 'n')
3716 t
= DEMANGLE_COMPONENT_LITERAL_NEG
;
3720 while (d_peek_char (di
) != 'E')
3722 if (d_peek_char (di
) == '\0')
3726 ret
= d_make_comp (di
, t
, type
, d_make_name (di
, s
, d_str (di
) - s
));
3728 if (! d_check_char (di
, 'E'))
3733 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
3734 ::= Z <(function) encoding> E s [<discriminator>]
3735 ::= Z <(function) encoding> E d [<parameter> number>] _ <entity name>
3738 static struct demangle_component
*
3739 d_local_name (struct d_info
*di
)
3741 struct demangle_component
*function
;
3742 struct demangle_component
*name
;
3744 if (! d_check_char (di
, 'Z'))
3747 function
= d_encoding (di
, 0);
3751 if (! d_check_char (di
, 'E'))
3754 if (d_peek_char (di
) == 's')
3757 if (! d_discriminator (di
))
3759 name
= d_make_name (di
, "string literal", sizeof "string literal" - 1);
3765 if (d_peek_char (di
) == 'd')
3767 /* Default argument scope: d <number> _. */
3769 num
= d_compact_number (di
);
3774 name
= d_name (di
, 0);
3777 /* Lambdas and unnamed types have internal discriminators
3778 and are not functions. */
3779 && name
->type
!= DEMANGLE_COMPONENT_LAMBDA
3780 && name
->type
!= DEMANGLE_COMPONENT_UNNAMED_TYPE
)
3782 /* Read and ignore an optional discriminator. */
3783 if (! d_discriminator (di
))
3788 name
= d_make_default_arg (di
, num
, name
);
3791 /* Elide the return type of the containing function so as to not
3792 confuse the user thinking it is the return type of whatever local
3793 function we might be containing. */
3794 if (function
->type
== DEMANGLE_COMPONENT_TYPED_NAME
3795 && d_right (function
)->type
== DEMANGLE_COMPONENT_FUNCTION_TYPE
)
3796 d_left (d_right (function
)) = NULL
;
3798 return d_make_comp (di
, DEMANGLE_COMPONENT_LOCAL_NAME
, function
, name
);
3801 /* <discriminator> ::= _ <number> # when number < 10
3802 ::= __ <number> _ # when number >= 10
3804 <discriminator> ::= _ <number> # when number >=10
3805 is also accepted to support gcc versions that wrongly mangled that way.
3807 We demangle the discriminator, but we don't print it out. FIXME:
3808 We should print it out in verbose mode. */
3811 d_discriminator (struct d_info
*di
)
3813 int discrim
, num_underscores
= 1;
3815 if (d_peek_char (di
) != '_')
3818 if (d_peek_char (di
) == '_')
3824 discrim
= d_number (di
);
3827 if (num_underscores
> 1 && discrim
>= 10)
3829 if (d_peek_char (di
) == '_')
3838 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ */
3840 static struct demangle_component
*
3841 d_lambda (struct d_info
*di
)
3843 struct demangle_component
*tl
;
3844 struct demangle_component
*ret
;
3847 if (! d_check_char (di
, 'U'))
3849 if (! d_check_char (di
, 'l'))
3852 tl
= d_parmlist (di
);
3856 if (! d_check_char (di
, 'E'))
3859 num
= d_compact_number (di
);
3863 ret
= d_make_empty (di
);
3866 ret
->type
= DEMANGLE_COMPONENT_LAMBDA
;
3867 ret
->u
.s_unary_num
.sub
= tl
;
3868 ret
->u
.s_unary_num
.num
= num
;
3874 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
3876 static struct demangle_component
*
3877 d_unnamed_type (struct d_info
*di
)
3879 struct demangle_component
*ret
;
3882 if (! d_check_char (di
, 'U'))
3884 if (! d_check_char (di
, 't'))
3887 num
= d_compact_number (di
);
3891 ret
= d_make_empty (di
);
3894 ret
->type
= DEMANGLE_COMPONENT_UNNAMED_TYPE
;
3895 ret
->u
.s_number
.number
= num
;
3898 if (! d_add_substitution (di
, ret
))
3904 /* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]*
3907 static struct demangle_component
*
3908 d_clone_suffix (struct d_info
*di
, struct demangle_component
*encoding
)
3910 const char *suffix
= d_str (di
);
3911 const char *pend
= suffix
;
3912 struct demangle_component
*n
;
3914 if (*pend
== '.' && (IS_LOWER (pend
[1]) || IS_DIGIT (pend
[1])
3918 while (IS_LOWER (*pend
) || IS_DIGIT (*pend
) || *pend
== '_')
3921 while (*pend
== '.' && IS_DIGIT (pend
[1]))
3924 while (IS_DIGIT (*pend
))
3927 d_advance (di
, pend
- suffix
);
3928 n
= d_make_name (di
, suffix
, pend
- suffix
);
3929 return d_make_comp (di
, DEMANGLE_COMPONENT_CLONE
, encoding
, n
);
3932 /* Add a new substitution. */
3935 d_add_substitution (struct d_info
*di
, struct demangle_component
*dc
)
3939 if (di
->next_sub
>= di
->num_subs
)
3941 di
->subs
[di
->next_sub
] = dc
;
3946 /* <substitution> ::= S <seq-id> _
3956 If PREFIX is non-zero, then this type is being used as a prefix in
3957 a qualified name. In this case, for the standard substitutions, we
3958 need to check whether we are being used as a prefix for a
3959 constructor or destructor, and return a full template name.
3960 Otherwise we will get something like std::iostream::~iostream()
3961 which does not correspond particularly well to any function which
3962 actually appears in the source.
3965 static const struct d_standard_sub_info standard_subs
[] =
3970 { 'a', NL ("std::allocator"),
3971 NL ("std::allocator"),
3973 { 'b', NL ("std::basic_string"),
3974 NL ("std::basic_string"),
3975 NL ("basic_string") },
3976 { 's', NL ("std::string"),
3977 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
3978 NL ("basic_string") },
3979 { 'i', NL ("std::istream"),
3980 NL ("std::basic_istream<char, std::char_traits<char> >"),
3981 NL ("basic_istream") },
3982 { 'o', NL ("std::ostream"),
3983 NL ("std::basic_ostream<char, std::char_traits<char> >"),
3984 NL ("basic_ostream") },
3985 { 'd', NL ("std::iostream"),
3986 NL ("std::basic_iostream<char, std::char_traits<char> >"),
3987 NL ("basic_iostream") }
3990 static struct demangle_component
*
3991 d_substitution (struct d_info
*di
, int prefix
)
3995 if (! d_check_char (di
, 'S'))
3998 c
= d_next_char (di
);
3999 if (c
== '_' || IS_DIGIT (c
) || IS_UPPER (c
))
4008 unsigned int new_id
;
4011 new_id
= id
* 36 + c
- '0';
4012 else if (IS_UPPER (c
))
4013 new_id
= id
* 36 + c
- 'A' + 10;
4019 c
= d_next_char (di
);
4026 if (id
>= (unsigned int) di
->next_sub
)
4029 return di
->subs
[id
];
4034 const struct d_standard_sub_info
*p
;
4035 const struct d_standard_sub_info
*pend
;
4037 verbose
= (di
->options
& DMGL_VERBOSE
) != 0;
4038 if (! verbose
&& prefix
)
4042 peek
= d_peek_char (di
);
4043 if (peek
== 'C' || peek
== 'D')
4047 pend
= (&standard_subs
[0]
4048 + sizeof standard_subs
/ sizeof standard_subs
[0]);
4049 for (p
= &standard_subs
[0]; p
< pend
; ++p
)
4055 struct demangle_component
*dc
;
4057 if (p
->set_last_name
!= NULL
)
4058 di
->last_name
= d_make_sub (di
, p
->set_last_name
,
4059 p
->set_last_name_len
);
4062 s
= p
->full_expansion
;
4067 s
= p
->simple_expansion
;
4068 len
= p
->simple_len
;
4070 di
->expansion
+= len
;
4071 dc
= d_make_sub (di
, s
, len
);
4072 if (d_peek_char (di
) == 'B')
4074 /* If there are ABI tags on the abbreviation, it becomes
4075 a substitution candidate. */
4076 dc
= d_abi_tags (di
, dc
);
4077 if (! d_add_substitution (di
, dc
))
4089 d_checkpoint (struct d_info
*di
, struct d_info_checkpoint
*checkpoint
)
4091 checkpoint
->n
= di
->n
;
4092 checkpoint
->next_comp
= di
->next_comp
;
4093 checkpoint
->next_sub
= di
->next_sub
;
4094 checkpoint
->expansion
= di
->expansion
;
4098 d_backtrack (struct d_info
*di
, struct d_info_checkpoint
*checkpoint
)
4100 di
->n
= checkpoint
->n
;
4101 di
->next_comp
= checkpoint
->next_comp
;
4102 di
->next_sub
= checkpoint
->next_sub
;
4103 di
->expansion
= checkpoint
->expansion
;
4106 /* Initialize a growable string. */
4109 d_growable_string_init (struct d_growable_string
*dgs
, size_t estimate
)
4114 dgs
->allocation_failure
= 0;
4117 d_growable_string_resize (dgs
, estimate
);
4120 /* Grow a growable string to a given size. */
4123 d_growable_string_resize (struct d_growable_string
*dgs
, size_t need
)
4128 if (dgs
->allocation_failure
)
4131 /* Start allocation at two bytes to avoid any possibility of confusion
4132 with the special value of 1 used as a return in *palc to indicate
4133 allocation failures. */
4134 newalc
= dgs
->alc
> 0 ? dgs
->alc
: 2;
4135 while (newalc
< need
)
4138 newbuf
= (char *) realloc (dgs
->buf
, newalc
);
4145 dgs
->allocation_failure
= 1;
4152 /* Append a buffer to a growable string. */
4155 d_growable_string_append_buffer (struct d_growable_string
*dgs
,
4156 const char *s
, size_t l
)
4160 need
= dgs
->len
+ l
+ 1;
4161 if (need
> dgs
->alc
)
4162 d_growable_string_resize (dgs
, need
);
4164 if (dgs
->allocation_failure
)
4167 memcpy (dgs
->buf
+ dgs
->len
, s
, l
);
4168 dgs
->buf
[dgs
->len
+ l
] = '\0';
4172 /* Bridge growable strings to the callback mechanism. */
4175 d_growable_string_callback_adapter (const char *s
, size_t l
, void *opaque
)
4177 struct d_growable_string
*dgs
= (struct d_growable_string
*) opaque
;
4179 d_growable_string_append_buffer (dgs
, s
, l
);
4182 /* Walk the tree, counting the number of templates encountered, and
4183 the number of times a scope might be saved. These counts will be
4184 used to allocate data structures for d_print_comp, so the logic
4185 here must mirror the logic d_print_comp will use. It is not
4186 important that the resulting numbers are exact, so long as they
4187 are larger than the actual numbers encountered. */
4190 d_count_templates_scopes (struct d_print_info
*dpi
,
4191 struct demangle_component
*dc
)
4193 if (dc
== NULL
|| dc
->d_counting
> 1 || dpi
->recursion
> MAX_RECURSION_COUNT
)
4200 case DEMANGLE_COMPONENT_NAME
:
4201 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
4202 case DEMANGLE_COMPONENT_FUNCTION_PARAM
:
4203 case DEMANGLE_COMPONENT_SUB_STD
:
4204 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
4205 case DEMANGLE_COMPONENT_OPERATOR
:
4206 case DEMANGLE_COMPONENT_CHARACTER
:
4207 case DEMANGLE_COMPONENT_NUMBER
:
4208 case DEMANGLE_COMPONENT_UNNAMED_TYPE
:
4209 case DEMANGLE_COMPONENT_STRUCTURED_BINDING
:
4210 case DEMANGLE_COMPONENT_MODULE_NAME
:
4211 case DEMANGLE_COMPONENT_MODULE_PARTITION
:
4212 case DEMANGLE_COMPONENT_MODULE_INIT
:
4215 case DEMANGLE_COMPONENT_TEMPLATE
:
4216 dpi
->num_copy_templates
++;
4217 goto recurse_left_right
;
4219 case DEMANGLE_COMPONENT_REFERENCE
:
4220 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
4221 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_TEMPLATE_PARAM
)
4222 dpi
->num_saved_scopes
++;
4223 goto recurse_left_right
;
4225 case DEMANGLE_COMPONENT_QUAL_NAME
:
4226 case DEMANGLE_COMPONENT_LOCAL_NAME
:
4227 case DEMANGLE_COMPONENT_TYPED_NAME
:
4228 case DEMANGLE_COMPONENT_VTABLE
:
4229 case DEMANGLE_COMPONENT_VTT
:
4230 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
4231 case DEMANGLE_COMPONENT_TYPEINFO
:
4232 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
4233 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
4234 case DEMANGLE_COMPONENT_THUNK
:
4235 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
4236 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
4237 case DEMANGLE_COMPONENT_JAVA_CLASS
:
4238 case DEMANGLE_COMPONENT_GUARD
:
4239 case DEMANGLE_COMPONENT_TLS_INIT
:
4240 case DEMANGLE_COMPONENT_TLS_WRAPPER
:
4241 case DEMANGLE_COMPONENT_REFTEMP
:
4242 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
4243 case DEMANGLE_COMPONENT_RESTRICT
:
4244 case DEMANGLE_COMPONENT_VOLATILE
:
4245 case DEMANGLE_COMPONENT_CONST
:
4246 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
4247 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
4248 case DEMANGLE_COMPONENT_CONST_THIS
:
4249 case DEMANGLE_COMPONENT_REFERENCE_THIS
:
4250 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
:
4251 case DEMANGLE_COMPONENT_TRANSACTION_SAFE
:
4252 case DEMANGLE_COMPONENT_NOEXCEPT
:
4253 case DEMANGLE_COMPONENT_THROW_SPEC
:
4254 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
4255 case DEMANGLE_COMPONENT_POINTER
:
4256 case DEMANGLE_COMPONENT_COMPLEX
:
4257 case DEMANGLE_COMPONENT_IMAGINARY
:
4258 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
4259 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
4260 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
4261 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
4262 case DEMANGLE_COMPONENT_VECTOR_TYPE
:
4263 case DEMANGLE_COMPONENT_ARGLIST
:
4264 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
4265 case DEMANGLE_COMPONENT_TPARM_OBJ
:
4266 case DEMANGLE_COMPONENT_INITIALIZER_LIST
:
4267 case DEMANGLE_COMPONENT_CAST
:
4268 case DEMANGLE_COMPONENT_CONVERSION
:
4269 case DEMANGLE_COMPONENT_NULLARY
:
4270 case DEMANGLE_COMPONENT_UNARY
:
4271 case DEMANGLE_COMPONENT_BINARY
:
4272 case DEMANGLE_COMPONENT_BINARY_ARGS
:
4273 case DEMANGLE_COMPONENT_TRINARY
:
4274 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
4275 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
4276 case DEMANGLE_COMPONENT_LITERAL
:
4277 case DEMANGLE_COMPONENT_LITERAL_NEG
:
4278 case DEMANGLE_COMPONENT_VENDOR_EXPR
:
4279 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
4280 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
4281 case DEMANGLE_COMPONENT_DECLTYPE
:
4282 case DEMANGLE_COMPONENT_TRANSACTION_CLONE
:
4283 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
:
4284 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
4285 case DEMANGLE_COMPONENT_TAGGED_NAME
:
4286 case DEMANGLE_COMPONENT_CLONE
:
4288 /* PR 89394 - Check for too much recursion. */
4289 if (dpi
->recursion
> DEMANGLE_RECURSION_LIMIT
)
4290 /* FIXME: There ought to be a way to report to the
4291 user that the recursion limit has been reached. */
4295 d_count_templates_scopes (dpi
, d_left (dc
));
4296 d_count_templates_scopes (dpi
, d_right (dc
));
4300 case DEMANGLE_COMPONENT_CTOR
:
4301 d_count_templates_scopes (dpi
, dc
->u
.s_ctor
.name
);
4304 case DEMANGLE_COMPONENT_DTOR
:
4305 d_count_templates_scopes (dpi
, dc
->u
.s_dtor
.name
);
4308 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
4309 d_count_templates_scopes (dpi
, dc
->u
.s_extended_operator
.name
);
4312 case DEMANGLE_COMPONENT_FIXED_TYPE
:
4313 d_count_templates_scopes (dpi
, dc
->u
.s_fixed
.length
);
4316 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
:
4317 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS
:
4318 case DEMANGLE_COMPONENT_MODULE_ENTITY
:
4319 d_count_templates_scopes (dpi
, d_left (dc
));
4322 case DEMANGLE_COMPONENT_LAMBDA
:
4323 case DEMANGLE_COMPONENT_DEFAULT_ARG
:
4324 d_count_templates_scopes (dpi
, dc
->u
.s_unary_num
.sub
);
4329 /* Initialize a print information structure. */
4332 d_print_init (struct d_print_info
*dpi
, demangle_callbackref callback
,
4333 void *opaque
, struct demangle_component
*dc
)
4336 dpi
->last_char
= '\0';
4337 dpi
->templates
= NULL
;
4338 dpi
->modifiers
= NULL
;
4339 dpi
->pack_index
= 0;
4340 dpi
->flush_count
= 0;
4342 dpi
->callback
= callback
;
4343 dpi
->opaque
= opaque
;
4345 dpi
->demangle_failure
= 0;
4347 dpi
->is_lambda_arg
= 0;
4349 dpi
->component_stack
= NULL
;
4351 dpi
->saved_scopes
= NULL
;
4352 dpi
->next_saved_scope
= 0;
4353 dpi
->num_saved_scopes
= 0;
4355 dpi
->copy_templates
= NULL
;
4356 dpi
->next_copy_template
= 0;
4357 dpi
->num_copy_templates
= 0;
4359 d_count_templates_scopes (dpi
, dc
);
4360 /* If we did not reach the recursion limit, then reset the
4361 current recursion value back to 0, so that we can print
4363 if (dpi
->recursion
< DEMANGLE_RECURSION_LIMIT
)
4365 dpi
->num_copy_templates
*= dpi
->num_saved_scopes
;
4367 dpi
->current_template
= NULL
;
4370 /* Indicate that an error occurred during printing, and test for error. */
4373 d_print_error (struct d_print_info
*dpi
)
4375 dpi
->demangle_failure
= 1;
4379 d_print_saw_error (struct d_print_info
*dpi
)
4381 return dpi
->demangle_failure
!= 0;
4384 /* Flush buffered characters to the callback. */
4387 d_print_flush (struct d_print_info
*dpi
)
4389 dpi
->buf
[dpi
->len
] = '\0';
4390 dpi
->callback (dpi
->buf
, dpi
->len
, dpi
->opaque
);
4395 /* Append characters and buffers for printing. */
4398 d_append_char (struct d_print_info
*dpi
, char c
)
4400 if (dpi
->len
== sizeof (dpi
->buf
) - 1)
4401 d_print_flush (dpi
);
4403 dpi
->buf
[dpi
->len
++] = c
;
4408 d_append_buffer (struct d_print_info
*dpi
, const char *s
, size_t l
)
4412 for (i
= 0; i
< l
; i
++)
4413 d_append_char (dpi
, s
[i
]);
4417 d_append_string (struct d_print_info
*dpi
, const char *s
)
4419 d_append_buffer (dpi
, s
, strlen (s
));
4423 d_append_num (struct d_print_info
*dpi
, int l
)
4426 sprintf (buf
,"%d", l
);
4427 d_append_string (dpi
, buf
);
4431 d_last_char (struct d_print_info
*dpi
)
4433 return dpi
->last_char
;
4436 /* Turn components into a human readable string. OPTIONS is the
4437 options bits passed to the demangler. DC is the tree to print.
4438 CALLBACK is a function to call to flush demangled string segments
4439 as they fill the intermediate buffer, and OPAQUE is a generalized
4440 callback argument. On success, this returns 1. On failure,
4441 it returns 0, indicating a bad parse. It does not use heap
4442 memory to build an output string, so cannot encounter memory
4443 allocation failure. */
4445 CP_STATIC_IF_GLIBCPP_V3
4447 cplus_demangle_print_callback (int options
,
4448 struct demangle_component
*dc
,
4449 demangle_callbackref callback
, void *opaque
)
4451 struct d_print_info dpi
;
4453 d_print_init (&dpi
, callback
, opaque
, dc
);
4456 #ifdef CP_DYNAMIC_ARRAYS
4457 /* Avoid zero-length VLAs, which are prohibited by the C99 standard
4458 and flagged as errors by Address Sanitizer. */
4459 __extension__
struct d_saved_scope scopes
[(dpi
.num_saved_scopes
> 0)
4460 ? dpi
.num_saved_scopes
: 1];
4461 __extension__
struct d_print_template temps
[(dpi
.num_copy_templates
> 0)
4462 ? dpi
.num_copy_templates
: 1];
4464 dpi
.saved_scopes
= scopes
;
4465 dpi
.copy_templates
= temps
;
4467 dpi
.saved_scopes
= alloca (dpi
.num_saved_scopes
4468 * sizeof (*dpi
.saved_scopes
));
4469 dpi
.copy_templates
= alloca (dpi
.num_copy_templates
4470 * sizeof (*dpi
.copy_templates
));
4473 d_print_comp (&dpi
, options
, dc
);
4476 d_print_flush (&dpi
);
4478 return ! d_print_saw_error (&dpi
);
4481 /* Turn components into a human readable string. OPTIONS is the
4482 options bits passed to the demangler. DC is the tree to print.
4483 ESTIMATE is a guess at the length of the result. This returns a
4484 string allocated by malloc, or NULL on error. On success, this
4485 sets *PALC to the size of the allocated buffer. On failure, this
4486 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
4489 CP_STATIC_IF_GLIBCPP_V3
4491 cplus_demangle_print (int options
, struct demangle_component
*dc
,
4492 int estimate
, size_t *palc
)
4494 struct d_growable_string dgs
;
4496 d_growable_string_init (&dgs
, estimate
);
4498 if (! cplus_demangle_print_callback (options
, dc
,
4499 d_growable_string_callback_adapter
,
4507 *palc
= dgs
.allocation_failure
? 1 : dgs
.alc
;
4511 /* Returns the I'th element of the template arglist ARGS, or NULL on
4512 failure. If I is negative, return the entire arglist. */
4514 static struct demangle_component
*
4515 d_index_template_argument (struct demangle_component
*args
, int i
)
4517 struct demangle_component
*a
;
4520 /* Print the whole argument pack. */
4527 if (a
->type
!= DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
4533 if (i
!= 0 || a
== NULL
)
4539 /* Returns the template argument from the current context indicated by DC,
4540 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
4542 static struct demangle_component
*
4543 d_lookup_template_argument (struct d_print_info
*dpi
,
4544 const struct demangle_component
*dc
)
4546 if (dpi
->templates
== NULL
)
4548 d_print_error (dpi
);
4552 return d_index_template_argument
4553 (d_right (dpi
->templates
->template_decl
),
4554 dc
->u
.s_number
.number
);
4557 /* Returns a template argument pack used in DC (any will do), or NULL. */
4559 static struct demangle_component
*
4560 d_find_pack (struct d_print_info
*dpi
,
4561 const struct demangle_component
*dc
)
4563 struct demangle_component
*a
;
4569 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
4570 a
= d_lookup_template_argument (dpi
, dc
);
4571 if (a
&& a
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
4575 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
4578 case DEMANGLE_COMPONENT_LAMBDA
:
4579 case DEMANGLE_COMPONENT_NAME
:
4580 case DEMANGLE_COMPONENT_TAGGED_NAME
:
4581 case DEMANGLE_COMPONENT_OPERATOR
:
4582 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
4583 case DEMANGLE_COMPONENT_SUB_STD
:
4584 case DEMANGLE_COMPONENT_CHARACTER
:
4585 case DEMANGLE_COMPONENT_FUNCTION_PARAM
:
4586 case DEMANGLE_COMPONENT_UNNAMED_TYPE
:
4587 case DEMANGLE_COMPONENT_FIXED_TYPE
:
4588 case DEMANGLE_COMPONENT_DEFAULT_ARG
:
4589 case DEMANGLE_COMPONENT_NUMBER
:
4592 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
4593 return d_find_pack (dpi
, dc
->u
.s_extended_operator
.name
);
4594 case DEMANGLE_COMPONENT_CTOR
:
4595 return d_find_pack (dpi
, dc
->u
.s_ctor
.name
);
4596 case DEMANGLE_COMPONENT_DTOR
:
4597 return d_find_pack (dpi
, dc
->u
.s_dtor
.name
);
4600 a
= d_find_pack (dpi
, d_left (dc
));
4603 return d_find_pack (dpi
, d_right (dc
));
4607 /* Returns the length of the template argument pack DC. */
4610 d_pack_length (const struct demangle_component
*dc
)
4613 while (dc
&& dc
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
4614 && d_left (dc
) != NULL
)
4622 /* Returns the number of template args in DC, expanding any pack expansions
4626 d_args_length (struct d_print_info
*dpi
, const struct demangle_component
*dc
)
4629 for (; dc
&& dc
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
;
4632 struct demangle_component
*elt
= d_left (dc
);
4635 if (elt
->type
== DEMANGLE_COMPONENT_PACK_EXPANSION
)
4637 struct demangle_component
*a
= d_find_pack (dpi
, d_left (elt
));
4638 count
+= d_pack_length (a
);
4646 /* DC is a component of a mangled expression. Print it, wrapped in parens
4650 d_print_subexpr (struct d_print_info
*dpi
, int options
,
4651 struct demangle_component
*dc
)
4654 if (dc
->type
== DEMANGLE_COMPONENT_NAME
4655 || dc
->type
== DEMANGLE_COMPONENT_QUAL_NAME
4656 || dc
->type
== DEMANGLE_COMPONENT_INITIALIZER_LIST
4657 || dc
->type
== DEMANGLE_COMPONENT_FUNCTION_PARAM
)
4660 d_append_char (dpi
, '(');
4661 d_print_comp (dpi
, options
, dc
);
4663 d_append_char (dpi
, ')');
4666 /* Save the current scope. */
4669 d_save_scope (struct d_print_info
*dpi
,
4670 const struct demangle_component
*container
)
4672 struct d_saved_scope
*scope
;
4673 struct d_print_template
*src
, **link
;
4675 if (dpi
->next_saved_scope
>= dpi
->num_saved_scopes
)
4677 d_print_error (dpi
);
4680 scope
= &dpi
->saved_scopes
[dpi
->next_saved_scope
];
4681 dpi
->next_saved_scope
++;
4683 scope
->container
= container
;
4684 link
= &scope
->templates
;
4686 for (src
= dpi
->templates
; src
!= NULL
; src
= src
->next
)
4688 struct d_print_template
*dst
;
4690 if (dpi
->next_copy_template
>= dpi
->num_copy_templates
)
4692 d_print_error (dpi
);
4695 dst
= &dpi
->copy_templates
[dpi
->next_copy_template
];
4696 dpi
->next_copy_template
++;
4698 dst
->template_decl
= src
->template_decl
;
4706 /* Attempt to locate a previously saved scope. Returns NULL if no
4707 corresponding saved scope was found. */
4709 static struct d_saved_scope
*
4710 d_get_saved_scope (struct d_print_info
*dpi
,
4711 const struct demangle_component
*container
)
4715 for (i
= 0; i
< dpi
->next_saved_scope
; i
++)
4716 if (dpi
->saved_scopes
[i
].container
== container
)
4717 return &dpi
->saved_scopes
[i
];
4722 /* If DC is a C++17 fold-expression, print it and return true; otherwise
4726 d_maybe_print_fold_expression (struct d_print_info
*dpi
, int options
,
4727 struct demangle_component
*dc
)
4729 struct demangle_component
*ops
, *operator_
, *op1
, *op2
;
4732 const char *fold_code
= d_left (dc
)->u
.s_operator
.op
->code
;
4733 if (fold_code
[0] != 'f')
4737 operator_
= d_left (ops
);
4738 op1
= d_right (ops
);
4740 if (op1
->type
== DEMANGLE_COMPONENT_TRINARY_ARG2
)
4742 op2
= d_right (op1
);
4746 /* Print the whole pack. */
4747 save_idx
= dpi
->pack_index
;
4748 dpi
->pack_index
= -1;
4750 switch (fold_code
[1])
4752 /* Unary left fold, (... + X). */
4754 d_append_string (dpi
, "(...");
4755 d_print_expr_op (dpi
, options
, operator_
);
4756 d_print_subexpr (dpi
, options
, op1
);
4757 d_append_char (dpi
, ')');
4760 /* Unary right fold, (X + ...). */
4762 d_append_char (dpi
, '(');
4763 d_print_subexpr (dpi
, options
, op1
);
4764 d_print_expr_op (dpi
, options
, operator_
);
4765 d_append_string (dpi
, "...)");
4768 /* Binary left fold, (42 + ... + X). */
4770 /* Binary right fold, (X + ... + 42). */
4772 d_append_char (dpi
, '(');
4773 d_print_subexpr (dpi
, options
, op1
);
4774 d_print_expr_op (dpi
, options
, operator_
);
4775 d_append_string (dpi
, "...");
4776 d_print_expr_op (dpi
, options
, operator_
);
4777 d_print_subexpr (dpi
, options
, op2
);
4778 d_append_char (dpi
, ')');
4782 dpi
->pack_index
= save_idx
;
4786 /* True iff DC represents a C99-style designated initializer. */
4789 is_designated_init (struct demangle_component
*dc
)
4791 if (dc
->type
!= DEMANGLE_COMPONENT_BINARY
4792 && dc
->type
!= DEMANGLE_COMPONENT_TRINARY
)
4795 struct demangle_component
*op
= d_left (dc
);
4796 const char *code
= op
->u
.s_operator
.op
->code
;
4797 return (code
[0] == 'd'
4798 && (code
[1] == 'i' || code
[1] == 'x' || code
[1] == 'X'));
4801 /* If DC represents a C99-style designated initializer, print it and return
4802 true; otherwise, return false. */
4805 d_maybe_print_designated_init (struct d_print_info
*dpi
, int options
,
4806 struct demangle_component
*dc
)
4808 if (!is_designated_init (dc
))
4811 const char *code
= d_left (dc
)->u
.s_operator
.op
->code
;
4813 struct demangle_component
*operands
= d_right (dc
);
4814 struct demangle_component
*op1
= d_left (operands
);
4815 struct demangle_component
*op2
= d_right (operands
);
4818 d_append_char (dpi
, '.');
4820 d_append_char (dpi
, '[');
4822 d_print_comp (dpi
, options
, op1
);
4825 d_append_string (dpi
, " ... ");
4826 d_print_comp (dpi
, options
, d_left (op2
));
4827 op2
= d_right (op2
);
4830 d_append_char (dpi
, ']');
4831 if (is_designated_init (op2
))
4833 /* Don't put '=' or '(' between chained designators. */
4834 d_print_comp (dpi
, options
, op2
);
4838 d_append_char (dpi
, '=');
4839 d_print_subexpr (dpi
, options
, op2
);
4844 /* Subroutine to handle components. */
4847 d_print_comp_inner (struct d_print_info
*dpi
, int options
,
4848 struct demangle_component
*dc
)
4850 /* Magic variable to let reference smashing skip over the next modifier
4851 without needing to modify *dc. */
4852 struct demangle_component
*mod_inner
= NULL
;
4854 /* Variable used to store the current templates while a previously
4855 captured scope is used. */
4856 struct d_print_template
*saved_templates
;
4858 /* Nonzero if templates have been stored in the above variable. */
4859 int need_template_restore
= 0;
4863 d_print_error (dpi
);
4866 if (d_print_saw_error (dpi
))
4871 case DEMANGLE_COMPONENT_NAME
:
4872 if ((options
& DMGL_JAVA
) == 0)
4873 d_append_buffer (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
4875 d_print_java_identifier (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
4878 case DEMANGLE_COMPONENT_TAGGED_NAME
:
4879 d_print_comp (dpi
, options
, d_left (dc
));
4880 d_append_string (dpi
, "[abi:");
4881 d_print_comp (dpi
, options
, d_right (dc
));
4882 d_append_char (dpi
, ']');
4885 case DEMANGLE_COMPONENT_STRUCTURED_BINDING
:
4886 d_append_char (dpi
, '[');
4889 d_print_comp (dpi
, options
, d_left (dc
));
4893 d_append_string (dpi
, ", ");
4895 d_append_char (dpi
, ']');
4898 case DEMANGLE_COMPONENT_MODULE_ENTITY
:
4899 d_print_comp (dpi
, options
, d_left (dc
));
4900 d_append_char (dpi
, '@');
4901 d_print_comp (dpi
, options
, d_right (dc
));
4904 case DEMANGLE_COMPONENT_MODULE_NAME
:
4905 case DEMANGLE_COMPONENT_MODULE_PARTITION
:
4908 d_print_comp (dpi
, options
, d_left (dc
));
4909 char c
= dc
->type
== DEMANGLE_COMPONENT_MODULE_PARTITION
4910 ? ':' : d_left (dc
) ? '.' : 0;
4912 d_append_char (dpi
, c
);
4913 d_print_comp (dpi
, options
, d_right (dc
));
4917 case DEMANGLE_COMPONENT_QUAL_NAME
:
4918 case DEMANGLE_COMPONENT_LOCAL_NAME
:
4919 d_print_comp (dpi
, options
, d_left (dc
));
4920 if ((options
& DMGL_JAVA
) == 0)
4921 d_append_string (dpi
, "::");
4923 d_append_char (dpi
, '.');
4925 struct demangle_component
*local_name
= d_right (dc
);
4926 if (local_name
->type
== DEMANGLE_COMPONENT_DEFAULT_ARG
)
4928 d_append_string (dpi
, "{default arg#");
4929 d_append_num (dpi
, local_name
->u
.s_unary_num
.num
+ 1);
4930 d_append_string (dpi
, "}::");
4931 local_name
= local_name
->u
.s_unary_num
.sub
;
4933 d_print_comp (dpi
, options
, local_name
);
4937 case DEMANGLE_COMPONENT_TYPED_NAME
:
4939 struct d_print_mod
*hold_modifiers
;
4940 struct demangle_component
*typed_name
;
4941 struct d_print_mod adpm
[4];
4943 struct d_print_template dpt
;
4945 /* Pass the name down to the type so that it can be printed in
4946 the right place for the type. We also have to pass down
4947 any CV-qualifiers, which apply to the this parameter. */
4948 hold_modifiers
= dpi
->modifiers
;
4951 typed_name
= d_left (dc
);
4952 while (typed_name
!= NULL
)
4954 if (i
>= sizeof adpm
/ sizeof adpm
[0])
4956 d_print_error (dpi
);
4960 adpm
[i
].next
= dpi
->modifiers
;
4961 dpi
->modifiers
= &adpm
[i
];
4962 adpm
[i
].mod
= typed_name
;
4963 adpm
[i
].printed
= 0;
4964 adpm
[i
].templates
= dpi
->templates
;
4967 if (!is_fnqual_component_type (typed_name
->type
))
4970 typed_name
= d_left (typed_name
);
4973 if (typed_name
== NULL
)
4975 d_print_error (dpi
);
4979 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
4980 there may be CV-qualifiers on its right argument which
4981 really apply here; this happens when parsing a class that
4982 is local to a function. */
4983 if (typed_name
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
4985 typed_name
= d_right (typed_name
);
4986 if (typed_name
->type
== DEMANGLE_COMPONENT_DEFAULT_ARG
)
4987 typed_name
= typed_name
->u
.s_unary_num
.sub
;
4988 while (typed_name
!= NULL
4989 && is_fnqual_component_type (typed_name
->type
))
4991 if (i
>= sizeof adpm
/ sizeof adpm
[0])
4993 d_print_error (dpi
);
4997 adpm
[i
] = adpm
[i
- 1];
4998 adpm
[i
].next
= &adpm
[i
- 1];
4999 dpi
->modifiers
= &adpm
[i
];
5001 adpm
[i
- 1].mod
= typed_name
;
5002 adpm
[i
- 1].printed
= 0;
5003 adpm
[i
- 1].templates
= dpi
->templates
;
5006 typed_name
= d_left (typed_name
);
5008 if (typed_name
== NULL
)
5010 d_print_error (dpi
);
5015 /* If typed_name is a template, then it applies to the
5016 function type as well. */
5017 if (typed_name
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
5019 dpt
.next
= dpi
->templates
;
5020 dpi
->templates
= &dpt
;
5021 dpt
.template_decl
= typed_name
;
5024 d_print_comp (dpi
, options
, d_right (dc
));
5026 if (typed_name
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
5027 dpi
->templates
= dpt
.next
;
5029 /* If the modifiers didn't get printed by the type, print them
5034 if (! adpm
[i
].printed
)
5036 d_append_char (dpi
, ' ');
5037 d_print_mod (dpi
, options
, adpm
[i
].mod
);
5041 dpi
->modifiers
= hold_modifiers
;
5046 case DEMANGLE_COMPONENT_TEMPLATE
:
5048 struct d_print_mod
*hold_dpm
;
5049 struct demangle_component
*dcl
;
5050 const struct demangle_component
*hold_current
;
5052 /* This template may need to be referenced by a cast operator
5053 contained in its subtree. */
5054 hold_current
= dpi
->current_template
;
5055 dpi
->current_template
= dc
;
5057 /* Don't push modifiers into a template definition. Doing so
5058 could give the wrong definition for a template argument.
5059 Instead, treat the template essentially as a name. */
5061 hold_dpm
= dpi
->modifiers
;
5062 dpi
->modifiers
= NULL
;
5066 if ((options
& DMGL_JAVA
) != 0
5067 && dcl
->type
== DEMANGLE_COMPONENT_NAME
5068 && dcl
->u
.s_name
.len
== 6
5069 && strncmp (dcl
->u
.s_name
.s
, "JArray", 6) == 0)
5071 /* Special-case Java arrays, so that JArray<TYPE> appears
5072 instead as TYPE[]. */
5074 d_print_comp (dpi
, options
, d_right (dc
));
5075 d_append_string (dpi
, "[]");
5079 d_print_comp (dpi
, options
, dcl
);
5080 if (d_last_char (dpi
) == '<')
5081 d_append_char (dpi
, ' ');
5082 d_append_char (dpi
, '<');
5083 d_print_comp (dpi
, options
, d_right (dc
));
5084 /* Avoid generating two consecutive '>' characters, to avoid
5085 the C++ syntactic ambiguity. */
5086 if (d_last_char (dpi
) == '>')
5087 d_append_char (dpi
, ' ');
5088 d_append_char (dpi
, '>');
5091 dpi
->modifiers
= hold_dpm
;
5092 dpi
->current_template
= hold_current
;
5097 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
5098 if (dpi
->is_lambda_arg
)
5100 /* Show the template parm index, as that's how g++ displays
5101 these, and future proofs us against potential
5102 '[]<typename T> (T *a, T *b) {...}'. */
5103 d_append_buffer (dpi
, "auto:", 5);
5104 d_append_num (dpi
, dc
->u
.s_number
.number
+ 1);
5108 struct d_print_template
*hold_dpt
;
5109 struct demangle_component
*a
= d_lookup_template_argument (dpi
, dc
);
5111 if (a
&& a
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
5112 a
= d_index_template_argument (a
, dpi
->pack_index
);
5116 d_print_error (dpi
);
5120 /* While processing this parameter, we need to pop the list
5121 of templates. This is because the template parameter may
5122 itself be a reference to a parameter of an outer
5125 hold_dpt
= dpi
->templates
;
5126 dpi
->templates
= hold_dpt
->next
;
5128 d_print_comp (dpi
, options
, a
);
5130 dpi
->templates
= hold_dpt
;
5134 case DEMANGLE_COMPONENT_TPARM_OBJ
:
5135 d_append_string (dpi
, "template parameter object for ");
5136 d_print_comp (dpi
, options
, d_left (dc
));
5139 case DEMANGLE_COMPONENT_CTOR
:
5140 d_print_comp (dpi
, options
, dc
->u
.s_ctor
.name
);
5143 case DEMANGLE_COMPONENT_DTOR
:
5144 d_append_char (dpi
, '~');
5145 d_print_comp (dpi
, options
, dc
->u
.s_dtor
.name
);
5148 case DEMANGLE_COMPONENT_MODULE_INIT
:
5149 d_append_string (dpi
, "initializer for module ");
5150 d_print_comp (dpi
, options
, d_left (dc
));
5153 case DEMANGLE_COMPONENT_VTABLE
:
5154 d_append_string (dpi
, "vtable for ");
5155 d_print_comp (dpi
, options
, d_left (dc
));
5158 case DEMANGLE_COMPONENT_VTT
:
5159 d_append_string (dpi
, "VTT for ");
5160 d_print_comp (dpi
, options
, d_left (dc
));
5163 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
5164 d_append_string (dpi
, "construction vtable for ");
5165 d_print_comp (dpi
, options
, d_left (dc
));
5166 d_append_string (dpi
, "-in-");
5167 d_print_comp (dpi
, options
, d_right (dc
));
5170 case DEMANGLE_COMPONENT_TYPEINFO
:
5171 d_append_string (dpi
, "typeinfo for ");
5172 d_print_comp (dpi
, options
, d_left (dc
));
5175 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
5176 d_append_string (dpi
, "typeinfo name for ");
5177 d_print_comp (dpi
, options
, d_left (dc
));
5180 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
5181 d_append_string (dpi
, "typeinfo fn for ");
5182 d_print_comp (dpi
, options
, d_left (dc
));
5185 case DEMANGLE_COMPONENT_THUNK
:
5186 d_append_string (dpi
, "non-virtual thunk to ");
5187 d_print_comp (dpi
, options
, d_left (dc
));
5190 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
5191 d_append_string (dpi
, "virtual thunk to ");
5192 d_print_comp (dpi
, options
, d_left (dc
));
5195 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
5196 d_append_string (dpi
, "covariant return thunk to ");
5197 d_print_comp (dpi
, options
, d_left (dc
));
5200 case DEMANGLE_COMPONENT_JAVA_CLASS
:
5201 d_append_string (dpi
, "java Class for ");
5202 d_print_comp (dpi
, options
, d_left (dc
));
5205 case DEMANGLE_COMPONENT_GUARD
:
5206 d_append_string (dpi
, "guard variable for ");
5207 d_print_comp (dpi
, options
, d_left (dc
));
5210 case DEMANGLE_COMPONENT_TLS_INIT
:
5211 d_append_string (dpi
, "TLS init function for ");
5212 d_print_comp (dpi
, options
, d_left (dc
));
5215 case DEMANGLE_COMPONENT_TLS_WRAPPER
:
5216 d_append_string (dpi
, "TLS wrapper function for ");
5217 d_print_comp (dpi
, options
, d_left (dc
));
5220 case DEMANGLE_COMPONENT_REFTEMP
:
5221 d_append_string (dpi
, "reference temporary #");
5222 d_print_comp (dpi
, options
, d_right (dc
));
5223 d_append_string (dpi
, " for ");
5224 d_print_comp (dpi
, options
, d_left (dc
));
5227 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
5228 d_append_string (dpi
, "hidden alias for ");
5229 d_print_comp (dpi
, options
, d_left (dc
));
5232 case DEMANGLE_COMPONENT_TRANSACTION_CLONE
:
5233 d_append_string (dpi
, "transaction clone for ");
5234 d_print_comp (dpi
, options
, d_left (dc
));
5237 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
:
5238 d_append_string (dpi
, "non-transaction clone for ");
5239 d_print_comp (dpi
, options
, d_left (dc
));
5242 case DEMANGLE_COMPONENT_SUB_STD
:
5243 d_append_buffer (dpi
, dc
->u
.s_string
.string
, dc
->u
.s_string
.len
);
5246 case DEMANGLE_COMPONENT_RESTRICT
:
5247 case DEMANGLE_COMPONENT_VOLATILE
:
5248 case DEMANGLE_COMPONENT_CONST
:
5250 struct d_print_mod
*pdpm
;
5252 /* When printing arrays, it's possible to have cases where the
5253 same CV-qualifier gets pushed on the stack multiple times.
5254 We only need to print it once. */
5256 for (pdpm
= dpi
->modifiers
; pdpm
!= NULL
; pdpm
= pdpm
->next
)
5258 if (! pdpm
->printed
)
5260 if (pdpm
->mod
->type
!= DEMANGLE_COMPONENT_RESTRICT
5261 && pdpm
->mod
->type
!= DEMANGLE_COMPONENT_VOLATILE
5262 && pdpm
->mod
->type
!= DEMANGLE_COMPONENT_CONST
)
5264 if (pdpm
->mod
->type
== dc
->type
)
5266 d_print_comp (dpi
, options
, d_left (dc
));
5274 case DEMANGLE_COMPONENT_REFERENCE
:
5275 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
5277 /* Handle reference smashing: & + && = &. */
5278 struct demangle_component
*sub
= d_left (dc
);
5279 if (!dpi
->is_lambda_arg
5280 && sub
->type
== DEMANGLE_COMPONENT_TEMPLATE_PARAM
)
5282 struct d_saved_scope
*scope
= d_get_saved_scope (dpi
, sub
);
5283 struct demangle_component
*a
;
5287 /* This is the first time SUB has been traversed.
5288 We need to capture the current templates so
5289 they can be restored if SUB is reentered as a
5291 d_save_scope (dpi
, sub
);
5292 if (d_print_saw_error (dpi
))
5297 const struct d_component_stack
*dcse
;
5298 int found_self_or_parent
= 0;
5300 /* This traversal is reentering SUB as a substition.
5301 If we are not beneath SUB or DC in the tree then we
5302 need to restore SUB's template stack temporarily. */
5303 for (dcse
= dpi
->component_stack
; dcse
!= NULL
;
5304 dcse
= dcse
->parent
)
5308 && dcse
!= dpi
->component_stack
))
5310 found_self_or_parent
= 1;
5315 if (!found_self_or_parent
)
5317 saved_templates
= dpi
->templates
;
5318 dpi
->templates
= scope
->templates
;
5319 need_template_restore
= 1;
5323 a
= d_lookup_template_argument (dpi
, sub
);
5324 if (a
&& a
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
5325 a
= d_index_template_argument (a
, dpi
->pack_index
);
5329 if (need_template_restore
)
5330 dpi
->templates
= saved_templates
;
5332 d_print_error (dpi
);
5339 if (sub
->type
== DEMANGLE_COMPONENT_REFERENCE
5340 || sub
->type
== dc
->type
)
5342 else if (sub
->type
== DEMANGLE_COMPONENT_RVALUE_REFERENCE
)
5343 mod_inner
= d_left (sub
);
5347 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
5348 case DEMANGLE_COMPONENT_POINTER
:
5349 case DEMANGLE_COMPONENT_COMPLEX
:
5350 case DEMANGLE_COMPONENT_IMAGINARY
:
5351 FNQUAL_COMPONENT_CASE
:
5354 /* We keep a list of modifiers on the stack. */
5355 struct d_print_mod dpm
;
5357 dpm
.next
= dpi
->modifiers
;
5358 dpi
->modifiers
= &dpm
;
5361 dpm
.templates
= dpi
->templates
;
5364 mod_inner
= d_left (dc
);
5366 d_print_comp (dpi
, options
, mod_inner
);
5368 /* If the modifier didn't get printed by the type, print it
5371 d_print_mod (dpi
, options
, dc
);
5373 dpi
->modifiers
= dpm
.next
;
5375 if (need_template_restore
)
5376 dpi
->templates
= saved_templates
;
5381 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
5382 if ((options
& DMGL_JAVA
) == 0)
5383 d_append_buffer (dpi
, dc
->u
.s_builtin
.type
->name
,
5384 dc
->u
.s_builtin
.type
->len
);
5386 d_append_buffer (dpi
, dc
->u
.s_builtin
.type
->java_name
,
5387 dc
->u
.s_builtin
.type
->java_len
);
5390 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
5391 d_print_comp (dpi
, options
, d_left (dc
));
5394 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
5396 if ((options
& DMGL_RET_POSTFIX
) != 0)
5397 d_print_function_type (dpi
,
5398 options
& ~(DMGL_RET_POSTFIX
| DMGL_RET_DROP
),
5399 dc
, dpi
->modifiers
);
5401 /* Print return type if present */
5402 if (d_left (dc
) != NULL
&& (options
& DMGL_RET_POSTFIX
) != 0)
5403 d_print_comp (dpi
, options
& ~(DMGL_RET_POSTFIX
| DMGL_RET_DROP
),
5405 else if (d_left (dc
) != NULL
&& (options
& DMGL_RET_DROP
) == 0)
5407 struct d_print_mod dpm
;
5409 /* We must pass this type down as a modifier in order to
5410 print it in the right location. */
5411 dpm
.next
= dpi
->modifiers
;
5412 dpi
->modifiers
= &dpm
;
5415 dpm
.templates
= dpi
->templates
;
5417 d_print_comp (dpi
, options
& ~(DMGL_RET_POSTFIX
| DMGL_RET_DROP
),
5420 dpi
->modifiers
= dpm
.next
;
5425 /* In standard prefix notation, there is a space between the
5426 return type and the function signature. */
5427 if ((options
& DMGL_RET_POSTFIX
) == 0)
5428 d_append_char (dpi
, ' ');
5431 if ((options
& DMGL_RET_POSTFIX
) == 0)
5432 d_print_function_type (dpi
,
5433 options
& ~(DMGL_RET_POSTFIX
| DMGL_RET_DROP
),
5434 dc
, dpi
->modifiers
);
5439 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
5441 struct d_print_mod
*hold_modifiers
;
5442 struct d_print_mod adpm
[4];
5444 struct d_print_mod
*pdpm
;
5446 /* We must pass this type down as a modifier in order to print
5447 multi-dimensional arrays correctly. If the array itself is
5448 CV-qualified, we act as though the element type were
5449 CV-qualified. We do this by copying the modifiers down
5450 rather than fiddling pointers, so that we don't wind up
5451 with a d_print_mod higher on the stack pointing into our
5452 stack frame after we return. */
5454 hold_modifiers
= dpi
->modifiers
;
5456 adpm
[0].next
= hold_modifiers
;
5457 dpi
->modifiers
= &adpm
[0];
5459 adpm
[0].printed
= 0;
5460 adpm
[0].templates
= dpi
->templates
;
5463 pdpm
= hold_modifiers
;
5465 && (pdpm
->mod
->type
== DEMANGLE_COMPONENT_RESTRICT
5466 || pdpm
->mod
->type
== DEMANGLE_COMPONENT_VOLATILE
5467 || pdpm
->mod
->type
== DEMANGLE_COMPONENT_CONST
))
5469 if (! pdpm
->printed
)
5471 if (i
>= sizeof adpm
/ sizeof adpm
[0])
5473 d_print_error (dpi
);
5478 adpm
[i
].next
= dpi
->modifiers
;
5479 dpi
->modifiers
= &adpm
[i
];
5487 d_print_comp (dpi
, options
, d_right (dc
));
5489 dpi
->modifiers
= hold_modifiers
;
5491 if (adpm
[0].printed
)
5497 d_print_mod (dpi
, options
, adpm
[i
].mod
);
5500 d_print_array_type (dpi
, options
, dc
, dpi
->modifiers
);
5505 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
5506 case DEMANGLE_COMPONENT_VECTOR_TYPE
:
5508 struct d_print_mod dpm
;
5510 dpm
.next
= dpi
->modifiers
;
5511 dpi
->modifiers
= &dpm
;
5514 dpm
.templates
= dpi
->templates
;
5516 d_print_comp (dpi
, options
, d_right (dc
));
5518 /* If the modifier didn't get printed by the type, print it
5521 d_print_mod (dpi
, options
, dc
);
5523 dpi
->modifiers
= dpm
.next
;
5528 case DEMANGLE_COMPONENT_FIXED_TYPE
:
5529 if (dc
->u
.s_fixed
.sat
)
5530 d_append_string (dpi
, "_Sat ");
5531 /* Don't print "int _Accum". */
5532 if (dc
->u
.s_fixed
.length
->u
.s_builtin
.type
5533 != &cplus_demangle_builtin_types
['i'-'a'])
5535 d_print_comp (dpi
, options
, dc
->u
.s_fixed
.length
);
5536 d_append_char (dpi
, ' ');
5538 if (dc
->u
.s_fixed
.accum
)
5539 d_append_string (dpi
, "_Accum");
5541 d_append_string (dpi
, "_Fract");
5544 case DEMANGLE_COMPONENT_ARGLIST
:
5545 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
5546 if (d_left (dc
) != NULL
)
5547 d_print_comp (dpi
, options
, d_left (dc
));
5548 if (d_right (dc
) != NULL
)
5551 unsigned long int flush_count
;
5552 /* Make sure ", " isn't flushed by d_append_string, otherwise
5553 dpi->len -= 2 wouldn't work. */
5554 if (dpi
->len
>= sizeof (dpi
->buf
) - 2)
5555 d_print_flush (dpi
);
5556 d_append_string (dpi
, ", ");
5558 flush_count
= dpi
->flush_count
;
5559 d_print_comp (dpi
, options
, d_right (dc
));
5560 /* If that didn't print anything (which can happen with empty
5561 template argument packs), remove the comma and space. */
5562 if (dpi
->flush_count
== flush_count
&& dpi
->len
== len
)
5567 case DEMANGLE_COMPONENT_INITIALIZER_LIST
:
5569 struct demangle_component
*type
= d_left (dc
);
5570 struct demangle_component
*list
= d_right (dc
);
5573 d_print_comp (dpi
, options
, type
);
5574 d_append_char (dpi
, '{');
5575 d_print_comp (dpi
, options
, list
);
5576 d_append_char (dpi
, '}');
5580 case DEMANGLE_COMPONENT_OPERATOR
:
5582 const struct demangle_operator_info
*op
= dc
->u
.s_operator
.op
;
5585 d_append_string (dpi
, "operator");
5586 /* Add a space before new/delete. */
5587 if (IS_LOWER (op
->name
[0]))
5588 d_append_char (dpi
, ' ');
5589 /* Omit a trailing space. */
5590 if (op
->name
[len
-1] == ' ')
5592 d_append_buffer (dpi
, op
->name
, len
);
5596 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
5597 d_append_string (dpi
, "operator ");
5598 d_print_comp (dpi
, options
, dc
->u
.s_extended_operator
.name
);
5601 case DEMANGLE_COMPONENT_CONVERSION
:
5602 d_append_string (dpi
, "operator ");
5603 d_print_conversion (dpi
, options
, dc
);
5606 case DEMANGLE_COMPONENT_NULLARY
:
5607 d_print_expr_op (dpi
, options
, d_left (dc
));
5610 case DEMANGLE_COMPONENT_UNARY
:
5612 struct demangle_component
*op
= d_left (dc
);
5613 struct demangle_component
*operand
= d_right (dc
);
5614 const char *code
= NULL
;
5616 if (op
->type
== DEMANGLE_COMPONENT_OPERATOR
)
5618 code
= op
->u
.s_operator
.op
->code
;
5619 if (!strcmp (code
, "ad"))
5621 /* Don't print the argument list for the address of a
5623 if (operand
->type
== DEMANGLE_COMPONENT_TYPED_NAME
5624 && d_left (operand
)->type
== DEMANGLE_COMPONENT_QUAL_NAME
5625 && d_right (operand
)->type
== DEMANGLE_COMPONENT_FUNCTION_TYPE
)
5626 operand
= d_left (operand
);
5628 if (operand
->type
== DEMANGLE_COMPONENT_BINARY_ARGS
)
5630 /* This indicates a suffix operator. */
5631 operand
= d_left (operand
);
5632 d_print_subexpr (dpi
, options
, operand
);
5633 d_print_expr_op (dpi
, options
, op
);
5638 /* For sizeof..., just print the pack length. */
5639 if (code
&& !strcmp (code
, "sZ"))
5641 struct demangle_component
*a
= d_find_pack (dpi
, operand
);
5642 int len
= d_pack_length (a
);
5643 d_append_num (dpi
, len
);
5646 else if (code
&& !strcmp (code
, "sP"))
5648 int len
= d_args_length (dpi
, operand
);
5649 d_append_num (dpi
, len
);
5653 if (op
->type
!= DEMANGLE_COMPONENT_CAST
)
5654 d_print_expr_op (dpi
, options
, op
);
5657 d_append_char (dpi
, '(');
5658 d_print_cast (dpi
, options
, op
);
5659 d_append_char (dpi
, ')');
5661 if (code
&& !strcmp (code
, "gs"))
5662 /* Avoid parens after '::'. */
5663 d_print_comp (dpi
, options
, operand
);
5664 else if (code
&& !strcmp (code
, "st"))
5665 /* Always print parens for sizeof (type). */
5667 d_append_char (dpi
, '(');
5668 d_print_comp (dpi
, options
, operand
);
5669 d_append_char (dpi
, ')');
5672 d_print_subexpr (dpi
, options
, operand
);
5676 case DEMANGLE_COMPONENT_BINARY
:
5677 if (d_right (dc
)->type
!= DEMANGLE_COMPONENT_BINARY_ARGS
)
5679 d_print_error (dpi
);
5683 if (op_is_new_cast (d_left (dc
)))
5685 d_print_expr_op (dpi
, options
, d_left (dc
));
5686 d_append_char (dpi
, '<');
5687 d_print_comp (dpi
, options
, d_left (d_right (dc
)));
5688 d_append_string (dpi
, ">(");
5689 d_print_comp (dpi
, options
, d_right (d_right (dc
)));
5690 d_append_char (dpi
, ')');
5694 if (d_maybe_print_fold_expression (dpi
, options
, dc
))
5697 if (d_maybe_print_designated_init (dpi
, options
, dc
))
5700 /* We wrap an expression which uses the greater-than operator in
5701 an extra layer of parens so that it does not get confused
5702 with the '>' which ends the template parameters. */
5703 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_OPERATOR
5704 && d_left (dc
)->u
.s_operator
.op
->len
== 1
5705 && d_left (dc
)->u
.s_operator
.op
->name
[0] == '>')
5706 d_append_char (dpi
, '(');
5708 if (strcmp (d_left (dc
)->u
.s_operator
.op
->code
, "cl") == 0
5709 && d_left (d_right (dc
))->type
== DEMANGLE_COMPONENT_TYPED_NAME
)
5711 /* Function call used in an expression should not have printed types
5712 of the function arguments. Values of the function arguments still
5713 get printed below. */
5715 const struct demangle_component
*func
= d_left (d_right (dc
));
5717 if (d_right (func
)->type
!= DEMANGLE_COMPONENT_FUNCTION_TYPE
)
5718 d_print_error (dpi
);
5719 d_print_subexpr (dpi
, options
, d_left (func
));
5722 d_print_subexpr (dpi
, options
, d_left (d_right (dc
)));
5723 if (strcmp (d_left (dc
)->u
.s_operator
.op
->code
, "ix") == 0)
5725 d_append_char (dpi
, '[');
5726 d_print_comp (dpi
, options
, d_right (d_right (dc
)));
5727 d_append_char (dpi
, ']');
5731 if (strcmp (d_left (dc
)->u
.s_operator
.op
->code
, "cl") != 0)
5732 d_print_expr_op (dpi
, options
, d_left (dc
));
5733 d_print_subexpr (dpi
, options
, d_right (d_right (dc
)));
5736 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_OPERATOR
5737 && d_left (dc
)->u
.s_operator
.op
->len
== 1
5738 && d_left (dc
)->u
.s_operator
.op
->name
[0] == '>')
5739 d_append_char (dpi
, ')');
5743 case DEMANGLE_COMPONENT_BINARY_ARGS
:
5744 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
5745 d_print_error (dpi
);
5748 case DEMANGLE_COMPONENT_TRINARY
:
5749 if (d_right (dc
)->type
!= DEMANGLE_COMPONENT_TRINARY_ARG1
5750 || d_right (d_right (dc
))->type
!= DEMANGLE_COMPONENT_TRINARY_ARG2
)
5752 d_print_error (dpi
);
5755 if (d_maybe_print_fold_expression (dpi
, options
, dc
))
5757 if (d_maybe_print_designated_init (dpi
, options
, dc
))
5760 struct demangle_component
*op
= d_left (dc
);
5761 struct demangle_component
*first
= d_left (d_right (dc
));
5762 struct demangle_component
*second
= d_left (d_right (d_right (dc
)));
5763 struct demangle_component
*third
= d_right (d_right (d_right (dc
)));
5765 if (!strcmp (op
->u
.s_operator
.op
->code
, "qu"))
5767 d_print_subexpr (dpi
, options
, first
);
5768 d_print_expr_op (dpi
, options
, op
);
5769 d_print_subexpr (dpi
, options
, second
);
5770 d_append_string (dpi
, " : ");
5771 d_print_subexpr (dpi
, options
, third
);
5775 d_append_string (dpi
, "new ");
5776 if (d_left (first
) != NULL
)
5778 d_print_subexpr (dpi
, options
, first
);
5779 d_append_char (dpi
, ' ');
5781 d_print_comp (dpi
, options
, second
);
5783 d_print_subexpr (dpi
, options
, third
);
5788 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
5789 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
5790 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
5791 d_print_error (dpi
);
5794 case DEMANGLE_COMPONENT_LITERAL
:
5795 case DEMANGLE_COMPONENT_LITERAL_NEG
:
5797 enum d_builtin_type_print tp
;
5799 /* For some builtin types, produce simpler output. */
5800 tp
= D_PRINT_DEFAULT
;
5801 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
)
5803 tp
= d_left (dc
)->u
.s_builtin
.type
->print
;
5807 case D_PRINT_UNSIGNED
:
5809 case D_PRINT_UNSIGNED_LONG
:
5810 case D_PRINT_LONG_LONG
:
5811 case D_PRINT_UNSIGNED_LONG_LONG
:
5812 if (d_right (dc
)->type
== DEMANGLE_COMPONENT_NAME
)
5814 if (dc
->type
== DEMANGLE_COMPONENT_LITERAL_NEG
)
5815 d_append_char (dpi
, '-');
5816 d_print_comp (dpi
, options
, d_right (dc
));
5821 case D_PRINT_UNSIGNED
:
5822 d_append_char (dpi
, 'u');
5825 d_append_char (dpi
, 'l');
5827 case D_PRINT_UNSIGNED_LONG
:
5828 d_append_string (dpi
, "ul");
5830 case D_PRINT_LONG_LONG
:
5831 d_append_string (dpi
, "ll");
5833 case D_PRINT_UNSIGNED_LONG_LONG
:
5834 d_append_string (dpi
, "ull");
5842 if (d_right (dc
)->type
== DEMANGLE_COMPONENT_NAME
5843 && d_right (dc
)->u
.s_name
.len
== 1
5844 && dc
->type
== DEMANGLE_COMPONENT_LITERAL
)
5846 switch (d_right (dc
)->u
.s_name
.s
[0])
5849 d_append_string (dpi
, "false");
5852 d_append_string (dpi
, "true");
5865 d_append_char (dpi
, '(');
5866 d_print_comp (dpi
, options
, d_left (dc
));
5867 d_append_char (dpi
, ')');
5868 if (dc
->type
== DEMANGLE_COMPONENT_LITERAL_NEG
)
5869 d_append_char (dpi
, '-');
5870 if (tp
== D_PRINT_FLOAT
)
5871 d_append_char (dpi
, '[');
5872 d_print_comp (dpi
, options
, d_right (dc
));
5873 if (tp
== D_PRINT_FLOAT
)
5874 d_append_char (dpi
, ']');
5878 case DEMANGLE_COMPONENT_VENDOR_EXPR
:
5879 d_print_comp (dpi
, options
, d_left (dc
));
5880 d_append_char (dpi
, '(');
5881 d_print_comp (dpi
, options
, d_right (dc
));
5882 d_append_char (dpi
, ')');
5885 case DEMANGLE_COMPONENT_NUMBER
:
5886 d_append_num (dpi
, dc
->u
.s_number
.number
);
5889 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
5890 d_append_string (dpi
, "java resource ");
5891 d_print_comp (dpi
, options
, d_left (dc
));
5894 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
5895 d_print_comp (dpi
, options
, d_left (dc
));
5896 d_print_comp (dpi
, options
, d_right (dc
));
5899 case DEMANGLE_COMPONENT_CHARACTER
:
5900 d_append_char (dpi
, dc
->u
.s_character
.character
);
5903 case DEMANGLE_COMPONENT_DECLTYPE
:
5904 d_append_string (dpi
, "decltype (");
5905 d_print_comp (dpi
, options
, d_left (dc
));
5906 d_append_char (dpi
, ')');
5909 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
5913 struct demangle_component
*a
= d_find_pack (dpi
, d_left (dc
));
5916 /* d_find_pack won't find anything if the only packs involved
5917 in this expansion are function parameter packs; in that
5918 case, just print the pattern and "...". */
5919 d_print_subexpr (dpi
, options
, d_left (dc
));
5920 d_append_string (dpi
, "...");
5924 len
= d_pack_length (a
);
5926 for (i
= 0; i
< len
; ++i
)
5928 dpi
->pack_index
= i
;
5929 d_print_comp (dpi
, options
, dc
);
5931 d_append_string (dpi
, ", ");
5936 case DEMANGLE_COMPONENT_FUNCTION_PARAM
:
5938 long num
= dc
->u
.s_number
.number
;
5940 d_append_string (dpi
, "this");
5943 d_append_string (dpi
, "{parm#");
5944 d_append_num (dpi
, num
);
5945 d_append_char (dpi
, '}');
5950 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
:
5951 d_append_string (dpi
, "global constructors keyed to ");
5952 d_print_comp (dpi
, options
, dc
->u
.s_binary
.left
);
5955 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS
:
5956 d_append_string (dpi
, "global destructors keyed to ");
5957 d_print_comp (dpi
, options
, dc
->u
.s_binary
.left
);
5960 case DEMANGLE_COMPONENT_LAMBDA
:
5961 d_append_string (dpi
, "{lambda(");
5962 /* Generic lambda auto parms are mangled as the template type
5964 dpi
->is_lambda_arg
++;
5965 d_print_comp (dpi
, options
, dc
->u
.s_unary_num
.sub
);
5966 dpi
->is_lambda_arg
--;
5967 d_append_string (dpi
, ")#");
5968 d_append_num (dpi
, dc
->u
.s_unary_num
.num
+ 1);
5969 d_append_char (dpi
, '}');
5972 case DEMANGLE_COMPONENT_UNNAMED_TYPE
:
5973 d_append_string (dpi
, "{unnamed type#");
5974 d_append_num (dpi
, dc
->u
.s_number
.number
+ 1);
5975 d_append_char (dpi
, '}');
5978 case DEMANGLE_COMPONENT_CLONE
:
5979 d_print_comp (dpi
, options
, d_left (dc
));
5980 d_append_string (dpi
, " [clone ");
5981 d_print_comp (dpi
, options
, d_right (dc
));
5982 d_append_char (dpi
, ']');
5986 d_print_error (dpi
);
5992 d_print_comp (struct d_print_info
*dpi
, int options
,
5993 struct demangle_component
*dc
)
5995 struct d_component_stack self
;
5996 if (dc
== NULL
|| dc
->d_printing
> 1 || dpi
->recursion
> MAX_RECURSION_COUNT
)
5998 d_print_error (dpi
);
6006 self
.parent
= dpi
->component_stack
;
6007 dpi
->component_stack
= &self
;
6009 d_print_comp_inner (dpi
, options
, dc
);
6011 dpi
->component_stack
= self
.parent
;
6016 /* Print a Java dentifier. For Java we try to handle encoded extended
6017 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
6018 so we don't it for C++. Characters are encoded as
6022 d_print_java_identifier (struct d_print_info
*dpi
, const char *name
, int len
)
6028 for (p
= name
; p
< end
; ++p
)
6039 for (q
= p
+ 3; q
< end
; ++q
)
6045 else if (*q
>= 'A' && *q
<= 'F')
6046 dig
= *q
- 'A' + 10;
6047 else if (*q
>= 'a' && *q
<= 'f')
6048 dig
= *q
- 'a' + 10;
6054 /* If the Unicode character is larger than 256, we don't try
6055 to deal with it here. FIXME. */
6056 if (q
< end
&& *q
== '_' && c
< 256)
6058 d_append_char (dpi
, c
);
6064 d_append_char (dpi
, *p
);
6068 /* Print a list of modifiers. SUFFIX is 1 if we are printing
6069 qualifiers on this after printing a function. */
6072 d_print_mod_list (struct d_print_info
*dpi
, int options
,
6073 struct d_print_mod
*mods
, int suffix
)
6075 struct d_print_template
*hold_dpt
;
6077 if (mods
== NULL
|| d_print_saw_error (dpi
))
6082 && (is_fnqual_component_type (mods
->mod
->type
))))
6084 d_print_mod_list (dpi
, options
, mods
->next
, suffix
);
6090 hold_dpt
= dpi
->templates
;
6091 dpi
->templates
= mods
->templates
;
6093 if (mods
->mod
->type
== DEMANGLE_COMPONENT_FUNCTION_TYPE
)
6095 d_print_function_type (dpi
, options
, mods
->mod
, mods
->next
);
6096 dpi
->templates
= hold_dpt
;
6099 else if (mods
->mod
->type
== DEMANGLE_COMPONENT_ARRAY_TYPE
)
6101 d_print_array_type (dpi
, options
, mods
->mod
, mods
->next
);
6102 dpi
->templates
= hold_dpt
;
6105 else if (mods
->mod
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
6107 struct d_print_mod
*hold_modifiers
;
6108 struct demangle_component
*dc
;
6110 /* When this is on the modifier stack, we have pulled any
6111 qualifiers off the right argument already. Otherwise, we
6112 print it as usual, but don't let the left argument see any
6115 hold_modifiers
= dpi
->modifiers
;
6116 dpi
->modifiers
= NULL
;
6117 d_print_comp (dpi
, options
, d_left (mods
->mod
));
6118 dpi
->modifiers
= hold_modifiers
;
6120 if ((options
& DMGL_JAVA
) == 0)
6121 d_append_string (dpi
, "::");
6123 d_append_char (dpi
, '.');
6125 dc
= d_right (mods
->mod
);
6127 if (dc
->type
== DEMANGLE_COMPONENT_DEFAULT_ARG
)
6129 d_append_string (dpi
, "{default arg#");
6130 d_append_num (dpi
, dc
->u
.s_unary_num
.num
+ 1);
6131 d_append_string (dpi
, "}::");
6132 dc
= dc
->u
.s_unary_num
.sub
;
6135 while (is_fnqual_component_type (dc
->type
))
6138 d_print_comp (dpi
, options
, dc
);
6140 dpi
->templates
= hold_dpt
;
6144 d_print_mod (dpi
, options
, mods
->mod
);
6146 dpi
->templates
= hold_dpt
;
6148 d_print_mod_list (dpi
, options
, mods
->next
, suffix
);
6151 /* Print a modifier. */
6154 d_print_mod (struct d_print_info
*dpi
, int options
,
6155 struct demangle_component
*mod
)
6159 case DEMANGLE_COMPONENT_RESTRICT
:
6160 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
6161 d_append_string (dpi
, " restrict");
6163 case DEMANGLE_COMPONENT_VOLATILE
:
6164 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
6165 d_append_string (dpi
, " volatile");
6167 case DEMANGLE_COMPONENT_CONST
:
6168 case DEMANGLE_COMPONENT_CONST_THIS
:
6169 d_append_string (dpi
, " const");
6171 case DEMANGLE_COMPONENT_TRANSACTION_SAFE
:
6172 d_append_string (dpi
, " transaction_safe");
6174 case DEMANGLE_COMPONENT_NOEXCEPT
:
6175 d_append_string (dpi
, " noexcept");
6178 d_append_char (dpi
, '(');
6179 d_print_comp (dpi
, options
, d_right (mod
));
6180 d_append_char (dpi
, ')');
6183 case DEMANGLE_COMPONENT_THROW_SPEC
:
6184 d_append_string (dpi
, " throw");
6187 d_append_char (dpi
, '(');
6188 d_print_comp (dpi
, options
, d_right (mod
));
6189 d_append_char (dpi
, ')');
6192 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
6193 d_append_char (dpi
, ' ');
6194 d_print_comp (dpi
, options
, d_right (mod
));
6196 case DEMANGLE_COMPONENT_POINTER
:
6197 /* There is no pointer symbol in Java. */
6198 if ((options
& DMGL_JAVA
) == 0)
6199 d_append_char (dpi
, '*');
6201 case DEMANGLE_COMPONENT_REFERENCE_THIS
:
6202 /* For the ref-qualifier, put a space before the &. */
6203 d_append_char (dpi
, ' ');
6205 case DEMANGLE_COMPONENT_REFERENCE
:
6206 d_append_char (dpi
, '&');
6208 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
:
6209 d_append_char (dpi
, ' ');
6211 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
6212 d_append_string (dpi
, "&&");
6214 case DEMANGLE_COMPONENT_COMPLEX
:
6215 d_append_string (dpi
, " _Complex");
6217 case DEMANGLE_COMPONENT_IMAGINARY
:
6218 d_append_string (dpi
, " _Imaginary");
6220 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
6221 if (d_last_char (dpi
) != '(')
6222 d_append_char (dpi
, ' ');
6223 d_print_comp (dpi
, options
, d_left (mod
));
6224 d_append_string (dpi
, "::*");
6226 case DEMANGLE_COMPONENT_TYPED_NAME
:
6227 d_print_comp (dpi
, options
, d_left (mod
));
6229 case DEMANGLE_COMPONENT_VECTOR_TYPE
:
6230 d_append_string (dpi
, " __vector(");
6231 d_print_comp (dpi
, options
, d_left (mod
));
6232 d_append_char (dpi
, ')');
6236 /* Otherwise, we have something that won't go back on the
6237 modifier stack, so we can just print it. */
6238 d_print_comp (dpi
, options
, mod
);
6243 /* Print a function type, except for the return type. */
6246 d_print_function_type (struct d_print_info
*dpi
, int options
,
6247 struct demangle_component
*dc
,
6248 struct d_print_mod
*mods
)
6252 struct d_print_mod
*p
;
6253 struct d_print_mod
*hold_modifiers
;
6257 for (p
= mods
; p
!= NULL
; p
= p
->next
)
6262 switch (p
->mod
->type
)
6264 case DEMANGLE_COMPONENT_POINTER
:
6265 case DEMANGLE_COMPONENT_REFERENCE
:
6266 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
6269 case DEMANGLE_COMPONENT_RESTRICT
:
6270 case DEMANGLE_COMPONENT_VOLATILE
:
6271 case DEMANGLE_COMPONENT_CONST
:
6272 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
6273 case DEMANGLE_COMPONENT_COMPLEX
:
6274 case DEMANGLE_COMPONENT_IMAGINARY
:
6275 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
6279 FNQUAL_COMPONENT_CASE
:
6292 if (d_last_char (dpi
) != '('
6293 && d_last_char (dpi
) != '*')
6296 if (need_space
&& d_last_char (dpi
) != ' ')
6297 d_append_char (dpi
, ' ');
6298 d_append_char (dpi
, '(');
6301 hold_modifiers
= dpi
->modifiers
;
6302 dpi
->modifiers
= NULL
;
6304 d_print_mod_list (dpi
, options
, mods
, 0);
6307 d_append_char (dpi
, ')');
6309 d_append_char (dpi
, '(');
6311 if (d_right (dc
) != NULL
)
6312 d_print_comp (dpi
, options
, d_right (dc
));
6314 d_append_char (dpi
, ')');
6316 d_print_mod_list (dpi
, options
, mods
, 1);
6318 dpi
->modifiers
= hold_modifiers
;
6321 /* Print an array type, except for the element type. */
6324 d_print_array_type (struct d_print_info
*dpi
, int options
,
6325 struct demangle_component
*dc
,
6326 struct d_print_mod
*mods
)
6334 struct d_print_mod
*p
;
6337 for (p
= mods
; p
!= NULL
; p
= p
->next
)
6341 if (p
->mod
->type
== DEMANGLE_COMPONENT_ARRAY_TYPE
)
6356 d_append_string (dpi
, " (");
6358 d_print_mod_list (dpi
, options
, mods
, 0);
6361 d_append_char (dpi
, ')');
6365 d_append_char (dpi
, ' ');
6367 d_append_char (dpi
, '[');
6369 if (d_left (dc
) != NULL
)
6370 d_print_comp (dpi
, options
, d_left (dc
));
6372 d_append_char (dpi
, ']');
6375 /* Print an operator in an expression. */
6378 d_print_expr_op (struct d_print_info
*dpi
, int options
,
6379 struct demangle_component
*dc
)
6381 if (dc
->type
== DEMANGLE_COMPONENT_OPERATOR
)
6382 d_append_buffer (dpi
, dc
->u
.s_operator
.op
->name
,
6383 dc
->u
.s_operator
.op
->len
);
6385 d_print_comp (dpi
, options
, dc
);
6391 d_print_cast (struct d_print_info
*dpi
, int options
,
6392 struct demangle_component
*dc
)
6394 d_print_comp (dpi
, options
, d_left (dc
));
6397 /* Print a conversion operator. */
6400 d_print_conversion (struct d_print_info
*dpi
, int options
,
6401 struct demangle_component
*dc
)
6403 struct d_print_template dpt
;
6405 /* For a conversion operator, we need the template parameters from
6406 the enclosing template in scope for processing the type. */
6407 if (dpi
->current_template
!= NULL
)
6409 dpt
.next
= dpi
->templates
;
6410 dpi
->templates
= &dpt
;
6411 dpt
.template_decl
= dpi
->current_template
;
6414 if (d_left (dc
)->type
!= DEMANGLE_COMPONENT_TEMPLATE
)
6416 d_print_comp (dpi
, options
, d_left (dc
));
6417 if (dpi
->current_template
!= NULL
)
6418 dpi
->templates
= dpt
.next
;
6422 d_print_comp (dpi
, options
, d_left (d_left (dc
)));
6424 /* For a templated cast operator, we need to remove the template
6425 parameters from scope after printing the operator name,
6426 so we need to handle the template printing here. */
6427 if (dpi
->current_template
!= NULL
)
6428 dpi
->templates
= dpt
.next
;
6430 if (d_last_char (dpi
) == '<')
6431 d_append_char (dpi
, ' ');
6432 d_append_char (dpi
, '<');
6433 d_print_comp (dpi
, options
, d_right (d_left (dc
)));
6434 /* Avoid generating two consecutive '>' characters, to avoid
6435 the C++ syntactic ambiguity. */
6436 if (d_last_char (dpi
) == '>')
6437 d_append_char (dpi
, ' ');
6438 d_append_char (dpi
, '>');
6442 /* Initialize the information structure we use to pass around
6445 CP_STATIC_IF_GLIBCPP_V3
6447 cplus_demangle_init_info (const char *mangled
, int options
, size_t len
,
6451 di
->send
= mangled
+ len
;
6452 di
->options
= options
;
6456 /* We cannot need more components than twice the number of chars in
6457 the mangled string. Most components correspond directly to
6458 chars, but the ARGLIST types are exceptions. */
6459 di
->num_comps
= 2 * len
;
6462 /* Similarly, we cannot need more substitutions than there are
6463 chars in the mangled string. */
6467 di
->last_name
= NULL
;
6470 di
->is_expression
= 0;
6471 di
->is_conversion
= 0;
6472 di
->recursion_level
= 0;
6475 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
6476 mangled name, return strings in repeated callback giving the demangled
6477 name. OPTIONS is the usual libiberty demangler options. On success,
6478 this returns 1. On failure, returns 0. */
6481 d_demangle_callback (const char *mangled
, int options
,
6482 demangle_callbackref callback
, void *opaque
)
6493 struct demangle_component
*dc
;
6496 if (mangled
[0] == '_' && mangled
[1] == 'Z')
6498 else if (strncmp (mangled
, "_GLOBAL_", 8) == 0
6499 && (mangled
[8] == '.' || mangled
[8] == '_' || mangled
[8] == '$')
6500 && (mangled
[9] == 'D' || mangled
[9] == 'I')
6501 && mangled
[10] == '_')
6502 type
= mangled
[9] == 'I' ? DCT_GLOBAL_CTORS
: DCT_GLOBAL_DTORS
;
6505 if ((options
& DMGL_TYPES
) == 0)
6510 di
.unresolved_name_state
= 1;
6513 cplus_demangle_init_info (mangled
, options
, strlen (mangled
), &di
);
6515 /* PR 87675 - Check for a mangled string that is so long
6516 that we do not have enough stack space to demangle it. */
6517 if (((options
& DMGL_NO_RECURSE_LIMIT
) == 0)
6518 /* This check is a bit arbitrary, since what we really want to do is to
6519 compare the sizes of the di.comps and di.subs arrays against the
6520 amount of stack space remaining. But there is no portable way to do
6521 this, so instead we use the recursion limit as a guide to the maximum
6522 size of the arrays. */
6523 && (unsigned long) di
.num_comps
> DEMANGLE_RECURSION_LIMIT
)
6525 /* FIXME: We need a way to indicate that a stack limit has been reached. */
6530 #ifdef CP_DYNAMIC_ARRAYS
6531 __extension__
struct demangle_component comps
[di
.num_comps
];
6532 __extension__
struct demangle_component
*subs
[di
.num_subs
];
6537 di
.comps
= alloca (di
.num_comps
* sizeof (*di
.comps
));
6538 di
.subs
= alloca (di
.num_subs
* sizeof (*di
.subs
));
6544 dc
= cplus_demangle_type (&di
);
6547 dc
= cplus_demangle_mangled_name (&di
, 1);
6549 case DCT_GLOBAL_CTORS
:
6550 case DCT_GLOBAL_DTORS
:
6551 d_advance (&di
, 11);
6552 dc
= d_make_comp (&di
,
6553 (type
== DCT_GLOBAL_CTORS
6554 ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
6555 : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS
),
6556 d_make_demangle_mangled_name (&di
, d_str (&di
)),
6558 d_advance (&di
, strlen (d_str (&di
)));
6561 abort (); /* We have listed all the cases. */
6564 /* If DMGL_PARAMS is set, then if we didn't consume the entire
6565 mangled string, then we didn't successfully demangle it. If
6566 DMGL_PARAMS is not set, we didn't look at the trailing
6568 if (((options
& DMGL_PARAMS
) != 0) && d_peek_char (&di
) != '\0')
6571 /* See discussion in d_unresolved_name. */
6572 if (dc
== NULL
&& di
.unresolved_name_state
== -1)
6574 di
.unresolved_name_state
= 0;
6578 #ifdef CP_DEMANGLE_DEBUG
6582 status
= (dc
!= NULL
)
6583 ? cplus_demangle_print_callback (options
, dc
, callback
, opaque
)
6590 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
6591 name, return a buffer allocated with malloc holding the demangled
6592 name. OPTIONS is the usual libiberty demangler options. On
6593 success, this sets *PALC to the allocated size of the returned
6594 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
6595 a memory allocation failure, and returns NULL. */
6598 d_demangle (const char *mangled
, int options
, size_t *palc
)
6600 struct d_growable_string dgs
;
6603 d_growable_string_init (&dgs
, 0);
6605 status
= d_demangle_callback (mangled
, options
,
6606 d_growable_string_callback_adapter
, &dgs
);
6614 *palc
= dgs
.allocation_failure
? 1 : dgs
.alc
;
6618 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
6620 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
6622 /* ia64 ABI-mandated entry point in the C++ runtime library for
6623 performing demangling. MANGLED_NAME is a NUL-terminated character
6624 string containing the name to be demangled.
6626 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
6627 *LENGTH bytes, into which the demangled name is stored. If
6628 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
6629 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
6630 is placed in a region of memory allocated with malloc.
6632 If LENGTH is non-NULL, the length of the buffer containing the
6633 demangled name, is placed in *LENGTH.
6635 The return value is a pointer to the start of the NUL-terminated
6636 demangled name, or NULL if the demangling fails. The caller is
6637 responsible for deallocating this memory using free.
6639 *STATUS is set to one of the following values:
6640 0: The demangling operation succeeded.
6641 -1: A memory allocation failure occurred.
6642 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
6643 -3: One of the arguments is invalid.
6645 The demangling is performed using the C++ ABI mangling rules, with
6649 __cxa_demangle (const char *mangled_name
, char *output_buffer
,
6650 size_t *length
, int *status
)
6655 if (mangled_name
== NULL
)
6662 if (output_buffer
!= NULL
&& length
== NULL
)
6669 demangled
= d_demangle (mangled_name
, DMGL_PARAMS
| DMGL_TYPES
, &alc
);
6671 if (demangled
== NULL
)
6683 if (output_buffer
== NULL
)
6690 if (strlen (demangled
) < *length
)
6692 strcpy (output_buffer
, demangled
);
6694 demangled
= output_buffer
;
6698 free (output_buffer
);
6709 extern int __gcclibcxx_demangle_callback (const char *,
6711 (const char *, size_t, void *),
6714 /* Alternative, allocationless entry point in the C++ runtime library
6715 for performing demangling. MANGLED_NAME is a NUL-terminated character
6716 string containing the name to be demangled.
6718 CALLBACK is a callback function, called with demangled string
6719 segments as demangling progresses; it is called at least once,
6720 but may be called more than once. OPAQUE is a generalized pointer
6721 used as a callback argument.
6723 The return code is one of the following values, equivalent to
6724 the STATUS values of __cxa_demangle() (excluding -1, since this
6725 function performs no memory allocations):
6726 0: The demangling operation succeeded.
6727 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
6728 -3: One of the arguments is invalid.
6730 The demangling is performed using the C++ ABI mangling rules, with
6734 __gcclibcxx_demangle_callback (const char *mangled_name
,
6735 void (*callback
) (const char *, size_t, void *),
6740 if (mangled_name
== NULL
|| callback
== NULL
)
6743 status
= d_demangle_callback (mangled_name
, DMGL_PARAMS
| DMGL_TYPES
,
6751 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
6753 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
6754 mangled name, return a buffer allocated with malloc holding the
6755 demangled name. Otherwise, return NULL. */
6758 cplus_demangle_v3 (const char *mangled
, int options
)
6762 return d_demangle (mangled
, options
, &alc
);
6766 cplus_demangle_v3_callback (const char *mangled
, int options
,
6767 demangle_callbackref callback
, void *opaque
)
6769 return d_demangle_callback (mangled
, options
, callback
, opaque
);
6772 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
6773 conventions, but the output formatting is a little different.
6774 This instructs the C++ demangler not to emit pointer characters ("*"), to
6775 use Java's namespace separator symbol ("." instead of "::"), and to output
6776 JArray<TYPE> as TYPE[]. */
6779 java_demangle_v3 (const char *mangled
)
6783 return d_demangle (mangled
, DMGL_JAVA
| DMGL_PARAMS
| DMGL_RET_POSTFIX
, &alc
);
6787 java_demangle_v3_callback (const char *mangled
,
6788 demangle_callbackref callback
, void *opaque
)
6790 return d_demangle_callback (mangled
,
6791 DMGL_JAVA
| DMGL_PARAMS
| DMGL_RET_POSTFIX
,
6795 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
6797 #ifndef IN_GLIBCPP_V3
6799 /* Demangle a string in order to find out whether it is a constructor
6800 or destructor. Return non-zero on success. Set *CTOR_KIND and
6801 *DTOR_KIND appropriately. */
6804 is_ctor_or_dtor (const char *mangled
,
6805 enum gnu_v3_ctor_kinds
*ctor_kind
,
6806 enum gnu_v3_dtor_kinds
*dtor_kind
)
6809 struct demangle_component
*dc
;
6812 *ctor_kind
= (enum gnu_v3_ctor_kinds
) 0;
6813 *dtor_kind
= (enum gnu_v3_dtor_kinds
) 0;
6815 cplus_demangle_init_info (mangled
, DMGL_GNU_V3
, strlen (mangled
), &di
);
6818 #ifdef CP_DYNAMIC_ARRAYS
6819 __extension__
struct demangle_component comps
[di
.num_comps
];
6820 __extension__
struct demangle_component
*subs
[di
.num_subs
];
6825 di
.comps
= alloca (di
.num_comps
* sizeof (*di
.comps
));
6826 di
.subs
= alloca (di
.num_subs
* sizeof (*di
.subs
));
6829 dc
= cplus_demangle_mangled_name (&di
, 1);
6831 /* Note that because we did not pass DMGL_PARAMS, we don't expect
6832 to demangle the entire string. */
6839 /* These cannot appear on a constructor or destructor. */
6840 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
6841 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
6842 case DEMANGLE_COMPONENT_CONST_THIS
:
6843 case DEMANGLE_COMPONENT_REFERENCE_THIS
:
6844 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
:
6848 case DEMANGLE_COMPONENT_TYPED_NAME
:
6849 case DEMANGLE_COMPONENT_TEMPLATE
:
6852 case DEMANGLE_COMPONENT_QUAL_NAME
:
6853 case DEMANGLE_COMPONENT_LOCAL_NAME
:
6856 case DEMANGLE_COMPONENT_CTOR
:
6857 *ctor_kind
= dc
->u
.s_ctor
.kind
;
6861 case DEMANGLE_COMPONENT_DTOR
:
6862 *dtor_kind
= dc
->u
.s_dtor
.kind
;
6873 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
6874 name. A non-zero return indicates the type of constructor. */
6876 enum gnu_v3_ctor_kinds
6877 is_gnu_v3_mangled_ctor (const char *name
)
6879 enum gnu_v3_ctor_kinds ctor_kind
;
6880 enum gnu_v3_dtor_kinds dtor_kind
;
6882 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
6883 return (enum gnu_v3_ctor_kinds
) 0;
6888 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
6889 name. A non-zero return indicates the type of destructor. */
6891 enum gnu_v3_dtor_kinds
6892 is_gnu_v3_mangled_dtor (const char *name
)
6894 enum gnu_v3_ctor_kinds ctor_kind
;
6895 enum gnu_v3_dtor_kinds dtor_kind
;
6897 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
6898 return (enum gnu_v3_dtor_kinds
) 0;
6902 #endif /* IN_GLIBCPP_V3 */
6904 #ifdef STANDALONE_DEMANGLER
6907 #include "dyn-string.h"
6909 static void print_usage (FILE* fp
, int exit_value
);
6911 #define IS_ALPHA(CHAR) \
6912 (((CHAR) >= 'a' && (CHAR) <= 'z') \
6913 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
6915 /* Non-zero if CHAR is a character than can occur in a mangled name. */
6916 #define is_mangled_char(CHAR) \
6917 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
6918 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
6920 /* The name of this program, as invoked. */
6921 const char* program_name
;
6923 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
6926 print_usage (FILE* fp
, int exit_value
)
6928 fprintf (fp
, "Usage: %s [options] [names ...]\n", program_name
);
6929 fprintf (fp
, "Options:\n");
6930 fprintf (fp
, " -h,--help Display this message.\n");
6931 fprintf (fp
, " -p,--no-params Don't display function parameters\n");
6932 fprintf (fp
, " -v,--verbose Produce verbose demanglings.\n");
6933 fprintf (fp
, "If names are provided, they are demangled. Otherwise filters standard input.\n");
6938 /* Option specification for getopt_long. */
6939 static const struct option long_options
[] =
6941 { "help", no_argument
, NULL
, 'h' },
6942 { "no-params", no_argument
, NULL
, 'p' },
6943 { "verbose", no_argument
, NULL
, 'v' },
6944 { NULL
, no_argument
, NULL
, 0 },
6947 /* Main entry for a demangling filter executable. It will demangle
6948 its command line arguments, if any. If none are provided, it will
6949 filter stdin to stdout, replacing any recognized mangled C++ names
6950 with their demangled equivalents. */
6953 main (int argc
, char *argv
[])
6957 int options
= DMGL_PARAMS
| DMGL_ANSI
| DMGL_TYPES
;
6959 /* Use the program name of this program, as invoked. */
6960 program_name
= argv
[0];
6962 /* Parse options. */
6965 opt_char
= getopt_long (argc
, argv
, "hpv", long_options
, NULL
);
6968 case '?': /* Unrecognized option. */
6969 print_usage (stderr
, 1);
6973 print_usage (stdout
, 0);
6977 options
&= ~ DMGL_PARAMS
;
6981 options
|= DMGL_VERBOSE
;
6985 while (opt_char
!= -1);
6988 /* No command line arguments were provided. Filter stdin. */
6990 dyn_string_t mangled
= dyn_string_new (3);
6993 /* Read all of input. */
6994 while (!feof (stdin
))
6998 /* Pile characters into mangled until we hit one that can't
6999 occur in a mangled name. */
7001 while (!feof (stdin
) && is_mangled_char (c
))
7003 dyn_string_append_char (mangled
, c
);
7009 if (dyn_string_length (mangled
) > 0)
7011 #ifdef IN_GLIBCPP_V3
7012 s
= __cxa_demangle (dyn_string_buf (mangled
), NULL
, NULL
, NULL
);
7014 s
= cplus_demangle_v3 (dyn_string_buf (mangled
), options
);
7024 /* It might not have been a mangled name. Print the
7026 fputs (dyn_string_buf (mangled
), stdout
);
7029 dyn_string_clear (mangled
);
7032 /* If we haven't hit EOF yet, we've read one character that
7033 can't occur in a mangled name, so print it out. */
7038 dyn_string_delete (mangled
);
7041 /* Demangle command line arguments. */
7043 /* Loop over command line arguments. */
7044 for (i
= optind
; i
< argc
; ++i
)
7047 #ifdef IN_GLIBCPP_V3
7051 /* Attempt to demangle. */
7052 #ifdef IN_GLIBCPP_V3
7053 s
= __cxa_demangle (argv
[i
], NULL
, NULL
, &status
);
7055 s
= cplus_demangle_v3 (argv
[i
], options
);
7058 /* If it worked, print the demangled name. */
7066 #ifdef IN_GLIBCPP_V3
7067 fprintf (stderr
, "Failed: %s (status %d)\n", argv
[i
], status
);
7069 fprintf (stderr
, "Failed: %s\n", argv
[i
]);
7078 #endif /* STANDALONE_DEMANGLER */