1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003-2017 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 http://www.codesourcery.com/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, const struct demangle_component
*, int, size_t *);
177 #define cplus_demangle_print_callback d_print_callback
178 static int d_print_callback (int, const 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
196 #define CP_DYNAMIC_ARRAYS
197 #endif /* __STDC__VERSION >= 199901L */
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 const 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
323 enum { D_PRINT_BUFFER_LENGTH
= 256 };
326 /* Fixed-length allocated buffer for demangled data, flushed to the
327 callback with a NUL termination once full. */
328 char buf
[D_PRINT_BUFFER_LENGTH
];
329 /* Current length of data in buffer. */
331 /* The last character printed, saved individually so that it survives
334 /* Callback function to handle demangled buffer flush. */
335 demangle_callbackref callback
;
336 /* Opaque callback argument. */
338 /* The current list of templates, if any. */
339 struct d_print_template
*templates
;
340 /* The current list of modifiers (e.g., pointer, reference, etc.),
342 struct d_print_mod
*modifiers
;
343 /* Set to 1 if we saw a demangling error. */
344 int demangle_failure
;
345 /* Non-zero if we're printing a lambda argument. A template
346 parameter reference actually means 'auto'. */
348 /* The current index into any template argument packs we are using
349 for printing, or -1 to print the whole pack. */
351 /* Number of d_print_flush calls so far. */
352 unsigned long int flush_count
;
353 /* Stack of components, innermost first, used to avoid loops. */
354 const struct d_component_stack
*component_stack
;
355 /* Array of saved scopes for evaluating substitutions. */
356 struct d_saved_scope
*saved_scopes
;
357 /* Index of the next unused saved scope in the above array. */
358 int next_saved_scope
;
359 /* Number of saved scopes in the above array. */
360 int num_saved_scopes
;
361 /* Array of templates for saving into scopes. */
362 struct d_print_template
*copy_templates
;
363 /* Index of the next unused copy template in the above array. */
364 int next_copy_template
;
365 /* Number of copy templates in the above array. */
366 int num_copy_templates
;
367 /* The nearest enclosing template, if any. */
368 const struct demangle_component
*current_template
;
371 #ifdef CP_DEMANGLE_DEBUG
372 static void d_dump (struct demangle_component
*, int);
375 static struct demangle_component
*
376 d_make_empty (struct d_info
*);
378 static struct demangle_component
*
379 d_make_comp (struct d_info
*, enum demangle_component_type
,
380 struct demangle_component
*,
381 struct demangle_component
*);
383 static struct demangle_component
*
384 d_make_name (struct d_info
*, const char *, int);
386 static struct demangle_component
*
387 d_make_demangle_mangled_name (struct d_info
*, const char *);
389 static struct demangle_component
*
390 d_make_builtin_type (struct d_info
*,
391 const struct demangle_builtin_type_info
*);
393 static struct demangle_component
*
394 d_make_operator (struct d_info
*,
395 const struct demangle_operator_info
*);
397 static struct demangle_component
*
398 d_make_extended_operator (struct d_info
*, int,
399 struct demangle_component
*);
401 static struct demangle_component
*
402 d_make_ctor (struct d_info
*, enum gnu_v3_ctor_kinds
,
403 struct demangle_component
*);
405 static struct demangle_component
*
406 d_make_dtor (struct d_info
*, enum gnu_v3_dtor_kinds
,
407 struct demangle_component
*);
409 static struct demangle_component
*
410 d_make_template_param (struct d_info
*, int);
412 static struct demangle_component
*
413 d_make_sub (struct d_info
*, const char *, int);
416 has_return_type (struct demangle_component
*);
419 is_ctor_dtor_or_conversion (struct demangle_component
*);
421 static struct demangle_component
*d_encoding (struct d_info
*, int);
423 static struct demangle_component
*d_name (struct d_info
*);
425 static struct demangle_component
*d_nested_name (struct d_info
*);
427 static struct demangle_component
*d_prefix (struct d_info
*);
429 static struct demangle_component
*d_unqualified_name (struct d_info
*);
431 static struct demangle_component
*d_source_name (struct d_info
*);
433 static int d_number (struct d_info
*);
435 static struct demangle_component
*d_identifier (struct d_info
*, int);
437 static struct demangle_component
*d_operator_name (struct d_info
*);
439 static struct demangle_component
*d_special_name (struct d_info
*);
441 static struct demangle_component
*d_parmlist (struct d_info
*);
443 static int d_call_offset (struct d_info
*, int);
445 static struct demangle_component
*d_ctor_dtor_name (struct d_info
*);
447 static struct demangle_component
**
448 d_cv_qualifiers (struct d_info
*, struct demangle_component
**, int);
450 static struct demangle_component
*
451 d_ref_qualifier (struct d_info
*, struct demangle_component
*);
453 static struct demangle_component
*
454 d_function_type (struct d_info
*);
456 static struct demangle_component
*
457 d_bare_function_type (struct d_info
*, int);
459 static struct demangle_component
*
460 d_class_enum_type (struct d_info
*);
462 static struct demangle_component
*d_array_type (struct d_info
*);
464 static struct demangle_component
*d_vector_type (struct d_info
*);
466 static struct demangle_component
*
467 d_pointer_to_member_type (struct d_info
*);
469 static struct demangle_component
*
470 d_template_param (struct d_info
*);
472 static struct demangle_component
*d_template_args (struct d_info
*);
473 static struct demangle_component
*d_template_args_1 (struct d_info
*);
475 static struct demangle_component
*
476 d_template_arg (struct d_info
*);
478 static struct demangle_component
*d_expression (struct d_info
*);
480 static struct demangle_component
*d_expr_primary (struct d_info
*);
482 static struct demangle_component
*d_local_name (struct d_info
*);
484 static int d_discriminator (struct d_info
*);
486 static struct demangle_component
*d_lambda (struct d_info
*);
488 static struct demangle_component
*d_unnamed_type (struct d_info
*);
490 static struct demangle_component
*
491 d_clone_suffix (struct d_info
*, struct demangle_component
*);
494 d_add_substitution (struct d_info
*, struct demangle_component
*);
496 static struct demangle_component
*d_substitution (struct d_info
*, int);
498 static void d_checkpoint (struct d_info
*, struct d_info_checkpoint
*);
500 static void d_backtrack (struct d_info
*, struct d_info_checkpoint
*);
502 static void d_growable_string_init (struct d_growable_string
*, size_t);
505 d_growable_string_resize (struct d_growable_string
*, size_t);
508 d_growable_string_append_buffer (struct d_growable_string
*,
509 const char *, size_t);
511 d_growable_string_callback_adapter (const char *, size_t, void *);
514 d_print_init (struct d_print_info
*, demangle_callbackref
, void *,
515 const struct demangle_component
*);
517 static inline void d_print_error (struct d_print_info
*);
519 static inline int d_print_saw_error (struct d_print_info
*);
521 static inline void d_print_flush (struct d_print_info
*);
523 static inline void d_append_char (struct d_print_info
*, char);
525 static inline void d_append_buffer (struct d_print_info
*,
526 const char *, size_t);
528 static inline void d_append_string (struct d_print_info
*, const char *);
530 static inline char d_last_char (struct d_print_info
*);
533 d_print_comp (struct d_print_info
*, int, const struct demangle_component
*);
536 d_print_java_identifier (struct d_print_info
*, const char *, int);
539 d_print_mod_list (struct d_print_info
*, int, struct d_print_mod
*, int);
542 d_print_mod (struct d_print_info
*, int, const struct demangle_component
*);
545 d_print_function_type (struct d_print_info
*, int,
546 const struct demangle_component
*,
547 struct d_print_mod
*);
550 d_print_array_type (struct d_print_info
*, int,
551 const struct demangle_component
*,
552 struct d_print_mod
*);
555 d_print_expr_op (struct d_print_info
*, int, const struct demangle_component
*);
557 static void d_print_cast (struct d_print_info
*, int,
558 const struct demangle_component
*);
559 static void d_print_conversion (struct d_print_info
*, int,
560 const struct demangle_component
*);
562 static int d_demangle_callback (const char *, int,
563 demangle_callbackref
, void *);
564 static char *d_demangle (const char *, int, size_t *);
566 /* True iff TYPE is a demangling component representing a
567 function-type-qualifier. */
570 is_fnqual_component_type (enum demangle_component_type type
)
572 return (type
== DEMANGLE_COMPONENT_RESTRICT_THIS
573 || type
== DEMANGLE_COMPONENT_VOLATILE_THIS
574 || type
== DEMANGLE_COMPONENT_CONST_THIS
575 || type
== DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
576 || type
== DEMANGLE_COMPONENT_TRANSACTION_SAFE
577 || type
== DEMANGLE_COMPONENT_NOEXCEPT
578 || type
== DEMANGLE_COMPONENT_THROW_SPEC
579 || type
== DEMANGLE_COMPONENT_REFERENCE_THIS
);
582 #define FNQUAL_COMPONENT_CASE \
583 case DEMANGLE_COMPONENT_RESTRICT_THIS: \
584 case DEMANGLE_COMPONENT_VOLATILE_THIS: \
585 case DEMANGLE_COMPONENT_CONST_THIS: \
586 case DEMANGLE_COMPONENT_REFERENCE_THIS: \
587 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS: \
588 case DEMANGLE_COMPONENT_TRANSACTION_SAFE: \
589 case DEMANGLE_COMPONENT_NOEXCEPT: \
590 case DEMANGLE_COMPONENT_THROW_SPEC
592 #ifdef CP_DEMANGLE_DEBUG
595 d_dump (struct demangle_component
*dc
, int indent
)
602 printf ("failed demangling\n");
606 for (i
= 0; i
< indent
; ++i
)
611 case DEMANGLE_COMPONENT_NAME
:
612 printf ("name '%.*s'\n", dc
->u
.s_name
.len
, dc
->u
.s_name
.s
);
614 case DEMANGLE_COMPONENT_TAGGED_NAME
:
615 printf ("tagged name\n");
616 d_dump (dc
->u
.s_binary
.left
, indent
+ 2);
617 d_dump (dc
->u
.s_binary
.right
, indent
+ 2);
619 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
620 printf ("template parameter %ld\n", dc
->u
.s_number
.number
);
622 case DEMANGLE_COMPONENT_FUNCTION_PARAM
:
623 printf ("function parameter %ld\n", dc
->u
.s_number
.number
);
625 case DEMANGLE_COMPONENT_CTOR
:
626 printf ("constructor %d\n", (int) dc
->u
.s_ctor
.kind
);
627 d_dump (dc
->u
.s_ctor
.name
, indent
+ 2);
629 case DEMANGLE_COMPONENT_DTOR
:
630 printf ("destructor %d\n", (int) dc
->u
.s_dtor
.kind
);
631 d_dump (dc
->u
.s_dtor
.name
, indent
+ 2);
633 case DEMANGLE_COMPONENT_SUB_STD
:
634 printf ("standard substitution %s\n", dc
->u
.s_string
.string
);
636 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
637 printf ("builtin type %s\n", dc
->u
.s_builtin
.type
->name
);
639 case DEMANGLE_COMPONENT_OPERATOR
:
640 printf ("operator %s\n", dc
->u
.s_operator
.op
->name
);
642 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
643 printf ("extended operator with %d args\n",
644 dc
->u
.s_extended_operator
.args
);
645 d_dump (dc
->u
.s_extended_operator
.name
, indent
+ 2);
648 case DEMANGLE_COMPONENT_QUAL_NAME
:
649 printf ("qualified name\n");
651 case DEMANGLE_COMPONENT_LOCAL_NAME
:
652 printf ("local name\n");
654 case DEMANGLE_COMPONENT_TYPED_NAME
:
655 printf ("typed name\n");
657 case DEMANGLE_COMPONENT_TEMPLATE
:
658 printf ("template\n");
660 case DEMANGLE_COMPONENT_VTABLE
:
663 case DEMANGLE_COMPONENT_VTT
:
666 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
667 printf ("construction vtable\n");
669 case DEMANGLE_COMPONENT_TYPEINFO
:
670 printf ("typeinfo\n");
672 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
673 printf ("typeinfo name\n");
675 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
676 printf ("typeinfo function\n");
678 case DEMANGLE_COMPONENT_THUNK
:
681 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
682 printf ("virtual thunk\n");
684 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
685 printf ("covariant thunk\n");
687 case DEMANGLE_COMPONENT_JAVA_CLASS
:
688 printf ("java class\n");
690 case DEMANGLE_COMPONENT_GUARD
:
693 case DEMANGLE_COMPONENT_REFTEMP
:
694 printf ("reference temporary\n");
696 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
697 printf ("hidden alias\n");
699 case DEMANGLE_COMPONENT_TRANSACTION_CLONE
:
700 printf ("transaction clone\n");
702 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
:
703 printf ("non-transaction clone\n");
705 case DEMANGLE_COMPONENT_RESTRICT
:
706 printf ("restrict\n");
708 case DEMANGLE_COMPONENT_VOLATILE
:
709 printf ("volatile\n");
711 case DEMANGLE_COMPONENT_CONST
:
714 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
715 printf ("restrict this\n");
717 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
718 printf ("volatile this\n");
720 case DEMANGLE_COMPONENT_CONST_THIS
:
721 printf ("const this\n");
723 case DEMANGLE_COMPONENT_REFERENCE_THIS
:
724 printf ("reference this\n");
726 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
:
727 printf ("rvalue reference this\n");
729 case DEMANGLE_COMPONENT_TRANSACTION_SAFE
:
730 printf ("transaction_safe this\n");
732 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
733 printf ("vendor type qualifier\n");
735 case DEMANGLE_COMPONENT_POINTER
:
736 printf ("pointer\n");
738 case DEMANGLE_COMPONENT_REFERENCE
:
739 printf ("reference\n");
741 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
742 printf ("rvalue reference\n");
744 case DEMANGLE_COMPONENT_COMPLEX
:
745 printf ("complex\n");
747 case DEMANGLE_COMPONENT_IMAGINARY
:
748 printf ("imaginary\n");
750 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
751 printf ("vendor type\n");
753 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
754 printf ("function type\n");
756 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
757 printf ("array type\n");
759 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
760 printf ("pointer to member type\n");
762 case DEMANGLE_COMPONENT_FIXED_TYPE
:
763 printf ("fixed-point type, accum? %d, sat? %d\n",
764 dc
->u
.s_fixed
.accum
, dc
->u
.s_fixed
.sat
);
765 d_dump (dc
->u
.s_fixed
.length
, indent
+ 2);
767 case DEMANGLE_COMPONENT_ARGLIST
:
768 printf ("argument list\n");
770 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
771 printf ("template argument list\n");
773 case DEMANGLE_COMPONENT_INITIALIZER_LIST
:
774 printf ("initializer list\n");
776 case DEMANGLE_COMPONENT_CAST
:
779 case DEMANGLE_COMPONENT_CONVERSION
:
780 printf ("conversion operator\n");
782 case DEMANGLE_COMPONENT_NULLARY
:
783 printf ("nullary operator\n");
785 case DEMANGLE_COMPONENT_UNARY
:
786 printf ("unary operator\n");
788 case DEMANGLE_COMPONENT_BINARY
:
789 printf ("binary operator\n");
791 case DEMANGLE_COMPONENT_BINARY_ARGS
:
792 printf ("binary operator arguments\n");
794 case DEMANGLE_COMPONENT_TRINARY
:
795 printf ("trinary operator\n");
797 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
798 printf ("trinary operator arguments 1\n");
800 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
801 printf ("trinary operator arguments 1\n");
803 case DEMANGLE_COMPONENT_LITERAL
:
804 printf ("literal\n");
806 case DEMANGLE_COMPONENT_LITERAL_NEG
:
807 printf ("negative literal\n");
809 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
810 printf ("java resource\n");
812 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
813 printf ("compound name\n");
815 case DEMANGLE_COMPONENT_CHARACTER
:
816 printf ("character '%c'\n", dc
->u
.s_character
.character
);
818 case DEMANGLE_COMPONENT_NUMBER
:
819 printf ("number %ld\n", dc
->u
.s_number
.number
);
821 case DEMANGLE_COMPONENT_DECLTYPE
:
822 printf ("decltype\n");
824 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
825 printf ("pack expansion\n");
827 case DEMANGLE_COMPONENT_TLS_INIT
:
828 printf ("tls init function\n");
830 case DEMANGLE_COMPONENT_TLS_WRAPPER
:
831 printf ("tls wrapper function\n");
833 case DEMANGLE_COMPONENT_DEFAULT_ARG
:
834 printf ("default argument %d\n", dc
->u
.s_unary_num
.num
);
835 d_dump (dc
->u
.s_unary_num
.sub
, indent
+2);
837 case DEMANGLE_COMPONENT_LAMBDA
:
838 printf ("lambda %d\n", dc
->u
.s_unary_num
.num
);
839 d_dump (dc
->u
.s_unary_num
.sub
, indent
+2);
843 d_dump (d_left (dc
), indent
+ 2);
844 d_dump (d_right (dc
), indent
+ 2);
847 #endif /* CP_DEMANGLE_DEBUG */
849 /* Fill in a DEMANGLE_COMPONENT_NAME. */
851 CP_STATIC_IF_GLIBCPP_V3
853 cplus_demangle_fill_name (struct demangle_component
*p
, const char *s
, int len
)
855 if (p
== NULL
|| s
== NULL
|| len
== 0)
857 p
->type
= DEMANGLE_COMPONENT_NAME
;
859 p
->u
.s_name
.len
= len
;
863 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
865 CP_STATIC_IF_GLIBCPP_V3
867 cplus_demangle_fill_extended_operator (struct demangle_component
*p
, int args
,
868 struct demangle_component
*name
)
870 if (p
== NULL
|| args
< 0 || name
== NULL
)
872 p
->type
= DEMANGLE_COMPONENT_EXTENDED_OPERATOR
;
873 p
->u
.s_extended_operator
.args
= args
;
874 p
->u
.s_extended_operator
.name
= name
;
878 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
880 CP_STATIC_IF_GLIBCPP_V3
882 cplus_demangle_fill_ctor (struct demangle_component
*p
,
883 enum gnu_v3_ctor_kinds kind
,
884 struct demangle_component
*name
)
888 || (int) kind
< gnu_v3_complete_object_ctor
889 || (int) kind
> gnu_v3_object_ctor_group
)
891 p
->type
= DEMANGLE_COMPONENT_CTOR
;
892 p
->u
.s_ctor
.kind
= kind
;
893 p
->u
.s_ctor
.name
= name
;
897 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
899 CP_STATIC_IF_GLIBCPP_V3
901 cplus_demangle_fill_dtor (struct demangle_component
*p
,
902 enum gnu_v3_dtor_kinds kind
,
903 struct demangle_component
*name
)
907 || (int) kind
< gnu_v3_deleting_dtor
908 || (int) kind
> gnu_v3_object_dtor_group
)
910 p
->type
= DEMANGLE_COMPONENT_DTOR
;
911 p
->u
.s_dtor
.kind
= kind
;
912 p
->u
.s_dtor
.name
= name
;
916 /* Add a new component. */
918 static struct demangle_component
*
919 d_make_empty (struct d_info
*di
)
921 struct demangle_component
*p
;
923 if (di
->next_comp
>= di
->num_comps
)
925 p
= &di
->comps
[di
->next_comp
];
930 /* Add a new generic component. */
932 static struct demangle_component
*
933 d_make_comp (struct d_info
*di
, enum demangle_component_type type
,
934 struct demangle_component
*left
,
935 struct demangle_component
*right
)
937 struct demangle_component
*p
;
939 /* We check for errors here. A typical error would be a NULL return
940 from a subroutine. We catch those here, and return NULL
944 /* These types require two parameters. */
945 case DEMANGLE_COMPONENT_QUAL_NAME
:
946 case DEMANGLE_COMPONENT_LOCAL_NAME
:
947 case DEMANGLE_COMPONENT_TYPED_NAME
:
948 case DEMANGLE_COMPONENT_TAGGED_NAME
:
949 case DEMANGLE_COMPONENT_TEMPLATE
:
950 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
951 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
952 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
953 case DEMANGLE_COMPONENT_UNARY
:
954 case DEMANGLE_COMPONENT_BINARY
:
955 case DEMANGLE_COMPONENT_BINARY_ARGS
:
956 case DEMANGLE_COMPONENT_TRINARY
:
957 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
958 case DEMANGLE_COMPONENT_LITERAL
:
959 case DEMANGLE_COMPONENT_LITERAL_NEG
:
960 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
961 case DEMANGLE_COMPONENT_VECTOR_TYPE
:
962 case DEMANGLE_COMPONENT_CLONE
:
963 if (left
== NULL
|| right
== NULL
)
967 /* These types only require one parameter. */
968 case DEMANGLE_COMPONENT_VTABLE
:
969 case DEMANGLE_COMPONENT_VTT
:
970 case DEMANGLE_COMPONENT_TYPEINFO
:
971 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
972 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
973 case DEMANGLE_COMPONENT_THUNK
:
974 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
975 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
976 case DEMANGLE_COMPONENT_JAVA_CLASS
:
977 case DEMANGLE_COMPONENT_GUARD
:
978 case DEMANGLE_COMPONENT_TLS_INIT
:
979 case DEMANGLE_COMPONENT_TLS_WRAPPER
:
980 case DEMANGLE_COMPONENT_REFTEMP
:
981 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
982 case DEMANGLE_COMPONENT_TRANSACTION_CLONE
:
983 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
:
984 case DEMANGLE_COMPONENT_POINTER
:
985 case DEMANGLE_COMPONENT_REFERENCE
:
986 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
987 case DEMANGLE_COMPONENT_COMPLEX
:
988 case DEMANGLE_COMPONENT_IMAGINARY
:
989 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
990 case DEMANGLE_COMPONENT_CAST
:
991 case DEMANGLE_COMPONENT_CONVERSION
:
992 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
993 case DEMANGLE_COMPONENT_DECLTYPE
:
994 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
995 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
:
996 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS
:
997 case DEMANGLE_COMPONENT_NULLARY
:
998 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
1003 /* This needs a right parameter, but the left parameter can be
1005 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
1006 case DEMANGLE_COMPONENT_INITIALIZER_LIST
:
1011 /* These are allowed to have no parameters--in some cases they
1012 will be filled in later. */
1013 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
1014 case DEMANGLE_COMPONENT_RESTRICT
:
1015 case DEMANGLE_COMPONENT_VOLATILE
:
1016 case DEMANGLE_COMPONENT_CONST
:
1017 case DEMANGLE_COMPONENT_ARGLIST
:
1018 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
1019 FNQUAL_COMPONENT_CASE
:
1022 /* Other types should not be seen here. */
1027 p
= d_make_empty (di
);
1031 p
->u
.s_binary
.left
= left
;
1032 p
->u
.s_binary
.right
= right
;
1037 /* Add a new demangle mangled name component. */
1039 static struct demangle_component
*
1040 d_make_demangle_mangled_name (struct d_info
*di
, const char *s
)
1042 if (d_peek_char (di
) != '_' || d_peek_next_char (di
) != 'Z')
1043 return d_make_name (di
, s
, strlen (s
));
1045 return d_encoding (di
, 0);
1048 /* Add a new name component. */
1050 static struct demangle_component
*
1051 d_make_name (struct d_info
*di
, const char *s
, int len
)
1053 struct demangle_component
*p
;
1055 p
= d_make_empty (di
);
1056 if (! cplus_demangle_fill_name (p
, s
, len
))
1061 /* Add a new builtin type component. */
1063 static struct demangle_component
*
1064 d_make_builtin_type (struct d_info
*di
,
1065 const struct demangle_builtin_type_info
*type
)
1067 struct demangle_component
*p
;
1071 p
= d_make_empty (di
);
1074 p
->type
= DEMANGLE_COMPONENT_BUILTIN_TYPE
;
1075 p
->u
.s_builtin
.type
= type
;
1080 /* Add a new operator component. */
1082 static struct demangle_component
*
1083 d_make_operator (struct d_info
*di
, const struct demangle_operator_info
*op
)
1085 struct demangle_component
*p
;
1087 p
= d_make_empty (di
);
1090 p
->type
= DEMANGLE_COMPONENT_OPERATOR
;
1091 p
->u
.s_operator
.op
= op
;
1096 /* Add a new extended operator component. */
1098 static struct demangle_component
*
1099 d_make_extended_operator (struct d_info
*di
, int args
,
1100 struct demangle_component
*name
)
1102 struct demangle_component
*p
;
1104 p
= d_make_empty (di
);
1105 if (! cplus_demangle_fill_extended_operator (p
, args
, name
))
1110 static struct demangle_component
*
1111 d_make_default_arg (struct d_info
*di
, int num
,
1112 struct demangle_component
*sub
)
1114 struct demangle_component
*p
= d_make_empty (di
);
1117 p
->type
= DEMANGLE_COMPONENT_DEFAULT_ARG
;
1118 p
->u
.s_unary_num
.num
= num
;
1119 p
->u
.s_unary_num
.sub
= sub
;
1124 /* Add a new constructor component. */
1126 static struct demangle_component
*
1127 d_make_ctor (struct d_info
*di
, enum gnu_v3_ctor_kinds kind
,
1128 struct demangle_component
*name
)
1130 struct demangle_component
*p
;
1132 p
= d_make_empty (di
);
1133 if (! cplus_demangle_fill_ctor (p
, kind
, name
))
1138 /* Add a new destructor component. */
1140 static struct demangle_component
*
1141 d_make_dtor (struct d_info
*di
, enum gnu_v3_dtor_kinds kind
,
1142 struct demangle_component
*name
)
1144 struct demangle_component
*p
;
1146 p
= d_make_empty (di
);
1147 if (! cplus_demangle_fill_dtor (p
, kind
, name
))
1152 /* Add a new template parameter. */
1154 static struct demangle_component
*
1155 d_make_template_param (struct d_info
*di
, int i
)
1157 struct demangle_component
*p
;
1159 p
= d_make_empty (di
);
1162 p
->type
= DEMANGLE_COMPONENT_TEMPLATE_PARAM
;
1163 p
->u
.s_number
.number
= i
;
1168 /* Add a new function parameter. */
1170 static struct demangle_component
*
1171 d_make_function_param (struct d_info
*di
, int i
)
1173 struct demangle_component
*p
;
1175 p
= d_make_empty (di
);
1178 p
->type
= DEMANGLE_COMPONENT_FUNCTION_PARAM
;
1179 p
->u
.s_number
.number
= i
;
1184 /* Add a new standard substitution component. */
1186 static struct demangle_component
*
1187 d_make_sub (struct d_info
*di
, const char *name
, int len
)
1189 struct demangle_component
*p
;
1191 p
= d_make_empty (di
);
1194 p
->type
= DEMANGLE_COMPONENT_SUB_STD
;
1195 p
->u
.s_string
.string
= name
;
1196 p
->u
.s_string
.len
= len
;
1201 /* <mangled-name> ::= _Z <encoding> [<clone-suffix>]*
1203 TOP_LEVEL is non-zero when called at the top level. */
1205 CP_STATIC_IF_GLIBCPP_V3
1206 struct demangle_component
*
1207 cplus_demangle_mangled_name (struct d_info
*di
, int top_level
)
1209 struct demangle_component
*p
;
1211 if (! d_check_char (di
, '_')
1212 /* Allow missing _ if not at toplevel to work around a
1213 bug in G++ abi-version=2 mangling; see the comment in
1214 write_template_arg. */
1217 if (! d_check_char (di
, 'Z'))
1219 p
= d_encoding (di
, top_level
);
1221 /* If at top level and parsing parameters, check for a clone
1223 if (top_level
&& (di
->options
& DMGL_PARAMS
) != 0)
1224 while (d_peek_char (di
) == '.'
1225 && (IS_LOWER (d_peek_next_char (di
))
1226 || d_peek_next_char (di
) == '_'
1227 || IS_DIGIT (d_peek_next_char (di
))))
1228 p
= d_clone_suffix (di
, p
);
1233 /* Return whether a function should have a return type. The argument
1234 is the function name, which may be qualified in various ways. The
1235 rules are that template functions have return types with some
1236 exceptions, function types which are not part of a function name
1237 mangling have return types with some exceptions, and non-template
1238 function names do not have return types. The exceptions are that
1239 constructors, destructors, and conversion operators do not have
1243 has_return_type (struct demangle_component
*dc
)
1251 case DEMANGLE_COMPONENT_TEMPLATE
:
1252 return ! is_ctor_dtor_or_conversion (d_left (dc
));
1253 FNQUAL_COMPONENT_CASE
:
1254 return has_return_type (d_left (dc
));
1258 /* Return whether a name is a constructor, a destructor, or a
1259 conversion operator. */
1262 is_ctor_dtor_or_conversion (struct demangle_component
*dc
)
1270 case DEMANGLE_COMPONENT_QUAL_NAME
:
1271 case DEMANGLE_COMPONENT_LOCAL_NAME
:
1272 return is_ctor_dtor_or_conversion (d_right (dc
));
1273 case DEMANGLE_COMPONENT_CTOR
:
1274 case DEMANGLE_COMPONENT_DTOR
:
1275 case DEMANGLE_COMPONENT_CONVERSION
:
1280 /* <encoding> ::= <(function) name> <bare-function-type>
1284 TOP_LEVEL is non-zero when called at the top level, in which case
1285 if DMGL_PARAMS is not set we do not demangle the function
1286 parameters. We only set this at the top level, because otherwise
1287 we would not correctly demangle names in local scopes. */
1289 static struct demangle_component
*
1290 d_encoding (struct d_info
*di
, int top_level
)
1292 char peek
= d_peek_char (di
);
1294 if (peek
== 'G' || peek
== 'T')
1295 return d_special_name (di
);
1298 struct demangle_component
*dc
;
1302 if (dc
!= NULL
&& top_level
&& (di
->options
& DMGL_PARAMS
) == 0)
1304 /* Strip off any initial CV-qualifiers, as they really apply
1305 to the `this' parameter, and they were not output by the
1306 v2 demangler without DMGL_PARAMS. */
1307 while (dc
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
1308 || dc
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
1309 || dc
->type
== DEMANGLE_COMPONENT_CONST_THIS
1310 || dc
->type
== DEMANGLE_COMPONENT_REFERENCE_THIS
1311 || dc
->type
== DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
)
1314 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1315 there may be function-qualifiers on its right argument which
1316 really apply here; this happens when parsing a class
1317 which is local to a function. */
1318 if (dc
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
1320 struct demangle_component
*dcr
;
1323 while (is_fnqual_component_type (dcr
->type
))
1325 dc
->u
.s_binary
.right
= dcr
;
1331 peek
= d_peek_char (di
);
1332 if (dc
== NULL
|| peek
== '\0' || peek
== 'E')
1334 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPED_NAME
, dc
,
1335 d_bare_function_type (di
, has_return_type (dc
)));
1339 /* <tagged-name> ::= <name> B <source-name> */
1341 static struct demangle_component
*
1342 d_abi_tags (struct d_info
*di
, struct demangle_component
*dc
)
1344 struct demangle_component
*hold_last_name
;
1347 /* Preserve the last name, so the ABI tag doesn't clobber it. */
1348 hold_last_name
= di
->last_name
;
1350 while (peek
= d_peek_char (di
),
1353 struct demangle_component
*tag
;
1355 tag
= d_source_name (di
);
1356 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_TAGGED_NAME
, dc
, tag
);
1359 di
->last_name
= hold_last_name
;
1364 /* <name> ::= <nested-name>
1366 ::= <unscoped-template-name> <template-args>
1369 <unscoped-name> ::= <unqualified-name>
1370 ::= St <unqualified-name>
1372 <unscoped-template-name> ::= <unscoped-name>
1376 static struct demangle_component
*
1377 d_name (struct d_info
*di
)
1379 char peek
= d_peek_char (di
);
1380 struct demangle_component
*dc
;
1385 return d_nested_name (di
);
1388 return d_local_name (di
);
1391 return d_unqualified_name (di
);
1397 if (d_peek_next_char (di
) != 't')
1399 dc
= d_substitution (di
, 0);
1405 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
,
1406 d_make_name (di
, "std", 3),
1407 d_unqualified_name (di
));
1412 if (d_peek_char (di
) != 'I')
1414 /* The grammar does not permit this case to occur if we
1415 called d_substitution() above (i.e., subst == 1). We
1416 don't bother to check. */
1420 /* This is <template-args>, which means that we just saw
1421 <unscoped-template-name>, which is a substitution
1422 candidate if we didn't just get it from a
1426 if (! d_add_substitution (di
, dc
))
1429 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, dc
,
1430 d_template_args (di
));
1438 dc
= d_unqualified_name (di
);
1439 if (d_peek_char (di
) == 'I')
1441 /* This is <template-args>, which means that we just saw
1442 <unscoped-template-name>, which is a substitution
1444 if (! d_add_substitution (di
, dc
))
1446 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, dc
,
1447 d_template_args (di
));
1453 /* <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1454 ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
1457 static struct demangle_component
*
1458 d_nested_name (struct d_info
*di
)
1460 struct demangle_component
*ret
;
1461 struct demangle_component
**pret
;
1462 struct demangle_component
*rqual
;
1464 if (! d_check_char (di
, 'N'))
1467 pret
= d_cv_qualifiers (di
, &ret
, 1);
1471 /* Parse the ref-qualifier now and then attach it
1472 once we have something to attach it to. */
1473 rqual
= d_ref_qualifier (di
, NULL
);
1475 *pret
= d_prefix (di
);
1481 d_left (rqual
) = ret
;
1485 if (! d_check_char (di
, 'E'))
1491 /* <prefix> ::= <prefix> <unqualified-name>
1492 ::= <template-prefix> <template-args>
1493 ::= <template-param>
1498 <template-prefix> ::= <prefix> <(template) unqualified-name>
1499 ::= <template-param>
1503 static struct demangle_component
*
1504 d_prefix (struct d_info
*di
)
1506 struct demangle_component
*ret
= NULL
;
1511 enum demangle_component_type comb_type
;
1512 struct demangle_component
*dc
;
1514 peek
= d_peek_char (di
);
1518 /* The older code accepts a <local-name> here, but I don't see
1519 that in the grammar. The older code does not accept a
1520 <template-param> here. */
1522 comb_type
= DEMANGLE_COMPONENT_QUAL_NAME
;
1525 char peek2
= d_peek_next_char (di
);
1526 if (peek2
== 'T' || peek2
== 't')
1528 dc
= cplus_demangle_type (di
);
1530 /* Destructor name. */
1531 dc
= d_unqualified_name (di
);
1533 else if (IS_DIGIT (peek
)
1538 dc
= d_unqualified_name (di
);
1539 else if (peek
== 'S')
1540 dc
= d_substitution (di
, 1);
1541 else if (peek
== 'I')
1545 comb_type
= DEMANGLE_COMPONENT_TEMPLATE
;
1546 dc
= d_template_args (di
);
1548 else if (peek
== 'T')
1549 dc
= d_template_param (di
);
1550 else if (peek
== 'E')
1552 else if (peek
== 'M')
1554 /* Initializer scope for a lambda. We don't need to represent
1555 this; the normal code will just treat the variable as a type
1556 scope, which gives appropriate output. */
1568 ret
= d_make_comp (di
, comb_type
, ret
, dc
);
1570 if (peek
!= 'S' && d_peek_char (di
) != 'E')
1572 if (! d_add_substitution (di
, ret
))
1578 /* <unqualified-name> ::= <operator-name>
1579 ::= <ctor-dtor-name>
1581 ::= <local-source-name>
1583 <local-source-name> ::= L <source-name> <discriminator>
1586 static struct demangle_component
*
1587 d_unqualified_name (struct d_info
*di
)
1589 struct demangle_component
*ret
;
1592 peek
= d_peek_char (di
);
1593 if (IS_DIGIT (peek
))
1594 ret
= d_source_name (di
);
1595 else if (IS_LOWER (peek
))
1597 if (peek
== 'o' && d_peek_next_char (di
) == 'n')
1599 ret
= d_operator_name (di
);
1600 if (ret
!= NULL
&& ret
->type
== DEMANGLE_COMPONENT_OPERATOR
)
1602 di
->expansion
+= sizeof "operator" + ret
->u
.s_operator
.op
->len
- 2;
1603 if (!strcmp (ret
->u
.s_operator
.op
->code
, "li"))
1604 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, ret
,
1605 d_source_name (di
));
1608 else if (peek
== 'C' || peek
== 'D')
1609 ret
= d_ctor_dtor_name (di
);
1610 else if (peek
== 'L')
1614 ret
= d_source_name (di
);
1617 if (! d_discriminator (di
))
1620 else if (peek
== 'U')
1622 switch (d_peek_next_char (di
))
1625 ret
= d_lambda (di
);
1628 ret
= d_unnamed_type (di
);
1637 if (d_peek_char (di
) == 'B')
1638 ret
= d_abi_tags (di
, ret
);
1642 /* <source-name> ::= <(positive length) number> <identifier> */
1644 static struct demangle_component
*
1645 d_source_name (struct d_info
*di
)
1648 struct demangle_component
*ret
;
1650 len
= d_number (di
);
1653 ret
= d_identifier (di
, len
);
1654 di
->last_name
= ret
;
1658 /* number ::= [n] <(non-negative decimal integer)> */
1661 d_number (struct d_info
*di
)
1668 peek
= d_peek_char (di
);
1673 peek
= d_peek_char (di
);
1679 if (! IS_DIGIT (peek
))
1685 ret
= ret
* 10 + peek
- '0';
1687 peek
= d_peek_char (di
);
1691 /* Like d_number, but returns a demangle_component. */
1693 static struct demangle_component
*
1694 d_number_component (struct d_info
*di
)
1696 struct demangle_component
*ret
= d_make_empty (di
);
1699 ret
->type
= DEMANGLE_COMPONENT_NUMBER
;
1700 ret
->u
.s_number
.number
= d_number (di
);
1705 /* identifier ::= <(unqualified source code identifier)> */
1707 static struct demangle_component
*
1708 d_identifier (struct d_info
*di
, int len
)
1714 if (di
->send
- name
< len
)
1717 d_advance (di
, len
);
1719 /* A Java mangled name may have a trailing '$' if it is a C++
1720 keyword. This '$' is not included in the length count. We just
1722 if ((di
->options
& DMGL_JAVA
) != 0
1723 && d_peek_char (di
) == '$')
1726 /* Look for something which looks like a gcc encoding of an
1727 anonymous namespace, and replace it with a more user friendly
1729 if (len
>= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN
+ 2
1730 && memcmp (name
, ANONYMOUS_NAMESPACE_PREFIX
,
1731 ANONYMOUS_NAMESPACE_PREFIX_LEN
) == 0)
1735 s
= name
+ ANONYMOUS_NAMESPACE_PREFIX_LEN
;
1736 if ((*s
== '.' || *s
== '_' || *s
== '$')
1739 di
->expansion
-= len
- sizeof "(anonymous namespace)";
1740 return d_make_name (di
, "(anonymous namespace)",
1741 sizeof "(anonymous namespace)" - 1);
1745 return d_make_name (di
, name
, len
);
1748 /* operator_name ::= many different two character encodings.
1750 ::= v <digit> <source-name>
1752 This list is sorted for binary search. */
1754 #define NL(s) s, (sizeof s) - 1
1756 CP_STATIC_IF_GLIBCPP_V3
1757 const struct demangle_operator_info cplus_demangle_operators
[] =
1759 { "aN", NL ("&="), 2 },
1760 { "aS", NL ("="), 2 },
1761 { "aa", NL ("&&"), 2 },
1762 { "ad", NL ("&"), 1 },
1763 { "an", NL ("&"), 2 },
1764 { "at", NL ("alignof "), 1 },
1765 { "az", NL ("alignof "), 1 },
1766 { "cc", NL ("const_cast"), 2 },
1767 { "cl", NL ("()"), 2 },
1768 { "cm", NL (","), 2 },
1769 { "co", NL ("~"), 1 },
1770 { "dV", NL ("/="), 2 },
1771 { "da", NL ("delete[] "), 1 },
1772 { "dc", NL ("dynamic_cast"), 2 },
1773 { "de", NL ("*"), 1 },
1774 { "dl", NL ("delete "), 1 },
1775 { "ds", NL (".*"), 2 },
1776 { "dt", NL ("."), 2 },
1777 { "dv", NL ("/"), 2 },
1778 { "eO", NL ("^="), 2 },
1779 { "eo", NL ("^"), 2 },
1780 { "eq", NL ("=="), 2 },
1781 { "fL", NL ("..."), 3 },
1782 { "fR", NL ("..."), 3 },
1783 { "fl", NL ("..."), 2 },
1784 { "fr", NL ("..."), 2 },
1785 { "ge", NL (">="), 2 },
1786 { "gs", NL ("::"), 1 },
1787 { "gt", NL (">"), 2 },
1788 { "ix", NL ("[]"), 2 },
1789 { "lS", NL ("<<="), 2 },
1790 { "le", NL ("<="), 2 },
1791 { "li", NL ("operator\"\" "), 1 },
1792 { "ls", NL ("<<"), 2 },
1793 { "lt", NL ("<"), 2 },
1794 { "mI", NL ("-="), 2 },
1795 { "mL", NL ("*="), 2 },
1796 { "mi", NL ("-"), 2 },
1797 { "ml", NL ("*"), 2 },
1798 { "mm", NL ("--"), 1 },
1799 { "na", NL ("new[]"), 3 },
1800 { "ne", NL ("!="), 2 },
1801 { "ng", NL ("-"), 1 },
1802 { "nt", NL ("!"), 1 },
1803 { "nw", NL ("new"), 3 },
1804 { "oR", NL ("|="), 2 },
1805 { "oo", NL ("||"), 2 },
1806 { "or", NL ("|"), 2 },
1807 { "pL", NL ("+="), 2 },
1808 { "pl", NL ("+"), 2 },
1809 { "pm", NL ("->*"), 2 },
1810 { "pp", NL ("++"), 1 },
1811 { "ps", NL ("+"), 1 },
1812 { "pt", NL ("->"), 2 },
1813 { "qu", NL ("?"), 3 },
1814 { "rM", NL ("%="), 2 },
1815 { "rS", NL (">>="), 2 },
1816 { "rc", NL ("reinterpret_cast"), 2 },
1817 { "rm", NL ("%"), 2 },
1818 { "rs", NL (">>"), 2 },
1819 { "sP", NL ("sizeof..."), 1 },
1820 { "sZ", NL ("sizeof..."), 1 },
1821 { "sc", NL ("static_cast"), 2 },
1822 { "st", NL ("sizeof "), 1 },
1823 { "sz", NL ("sizeof "), 1 },
1824 { "tr", NL ("throw"), 0 },
1825 { "tw", NL ("throw "), 1 },
1826 { NULL
, NULL
, 0, 0 }
1829 static struct demangle_component
*
1830 d_operator_name (struct d_info
*di
)
1835 c1
= d_next_char (di
);
1836 c2
= d_next_char (di
);
1837 if (c1
== 'v' && IS_DIGIT (c2
))
1838 return d_make_extended_operator (di
, c2
- '0', d_source_name (di
));
1839 else if (c1
== 'c' && c2
== 'v')
1841 struct demangle_component
*type
;
1842 int was_conversion
= di
->is_conversion
;
1843 struct demangle_component
*res
;
1845 di
->is_conversion
= ! di
->is_expression
;
1846 type
= cplus_demangle_type (di
);
1847 if (di
->is_conversion
)
1848 res
= d_make_comp (di
, DEMANGLE_COMPONENT_CONVERSION
, type
, NULL
);
1850 res
= d_make_comp (di
, DEMANGLE_COMPONENT_CAST
, type
, NULL
);
1851 di
->is_conversion
= was_conversion
;
1856 /* LOW is the inclusive lower bound. */
1858 /* HIGH is the exclusive upper bound. We subtract one to ignore
1859 the sentinel at the end of the array. */
1860 int high
= ((sizeof (cplus_demangle_operators
)
1861 / sizeof (cplus_demangle_operators
[0]))
1867 const struct demangle_operator_info
*p
;
1869 i
= low
+ (high
- low
) / 2;
1870 p
= cplus_demangle_operators
+ i
;
1872 if (c1
== p
->code
[0] && c2
== p
->code
[1])
1873 return d_make_operator (di
, p
);
1875 if (c1
< p
->code
[0] || (c1
== p
->code
[0] && c2
< p
->code
[1]))
1885 static struct demangle_component
*
1886 d_make_character (struct d_info
*di
, int c
)
1888 struct demangle_component
*p
;
1889 p
= d_make_empty (di
);
1892 p
->type
= DEMANGLE_COMPONENT_CHARACTER
;
1893 p
->u
.s_character
.character
= c
;
1898 static struct demangle_component
*
1899 d_java_resource (struct d_info
*di
)
1901 struct demangle_component
*p
= NULL
;
1902 struct demangle_component
*next
= NULL
;
1907 len
= d_number (di
);
1911 /* Eat the leading '_'. */
1912 if (d_next_char (di
) != '_')
1925 /* Each chunk is either a '$' escape... */
1943 next
= d_make_character (di
, c
);
1951 /* ... or a sequence of characters. */
1954 while (i
< len
&& str
[i
] && str
[i
] != '$')
1957 next
= d_make_name (di
, str
, i
);
1970 p
= d_make_comp (di
, DEMANGLE_COMPONENT_COMPOUND_NAME
, p
, next
);
1976 p
= d_make_comp (di
, DEMANGLE_COMPONENT_JAVA_RESOURCE
, p
, NULL
);
1981 /* <special-name> ::= TV <type>
1985 ::= GV <(object) name>
1986 ::= T <call-offset> <(base) encoding>
1987 ::= Tc <call-offset> <call-offset> <(base) encoding>
1988 Also g++ extensions:
1989 ::= TC <type> <(offset) number> _ <(base) type>
1994 ::= Gr <resource name>
1999 static struct demangle_component
*
2000 d_special_name (struct d_info
*di
)
2002 di
->expansion
+= 20;
2003 if (d_check_char (di
, 'T'))
2005 switch (d_next_char (di
))
2009 return d_make_comp (di
, DEMANGLE_COMPONENT_VTABLE
,
2010 cplus_demangle_type (di
), NULL
);
2012 di
->expansion
-= 10;
2013 return d_make_comp (di
, DEMANGLE_COMPONENT_VTT
,
2014 cplus_demangle_type (di
), NULL
);
2016 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO
,
2017 cplus_demangle_type (di
), NULL
);
2019 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO_NAME
,
2020 cplus_demangle_type (di
), NULL
);
2023 if (! d_call_offset (di
, 'h'))
2025 return d_make_comp (di
, DEMANGLE_COMPONENT_THUNK
,
2026 d_encoding (di
, 0), NULL
);
2029 if (! d_call_offset (di
, 'v'))
2031 return d_make_comp (di
, DEMANGLE_COMPONENT_VIRTUAL_THUNK
,
2032 d_encoding (di
, 0), NULL
);
2035 if (! d_call_offset (di
, '\0'))
2037 if (! d_call_offset (di
, '\0'))
2039 return d_make_comp (di
, DEMANGLE_COMPONENT_COVARIANT_THUNK
,
2040 d_encoding (di
, 0), NULL
);
2044 struct demangle_component
*derived_type
;
2046 struct demangle_component
*base_type
;
2048 derived_type
= cplus_demangle_type (di
);
2049 offset
= d_number (di
);
2052 if (! d_check_char (di
, '_'))
2054 base_type
= cplus_demangle_type (di
);
2055 /* We don't display the offset. FIXME: We should display
2056 it in verbose mode. */
2058 return d_make_comp (di
, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
,
2059 base_type
, derived_type
);
2063 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO_FN
,
2064 cplus_demangle_type (di
), NULL
);
2066 return d_make_comp (di
, DEMANGLE_COMPONENT_JAVA_CLASS
,
2067 cplus_demangle_type (di
), NULL
);
2070 return d_make_comp (di
, DEMANGLE_COMPONENT_TLS_INIT
,
2074 return d_make_comp (di
, DEMANGLE_COMPONENT_TLS_WRAPPER
,
2081 else if (d_check_char (di
, 'G'))
2083 switch (d_next_char (di
))
2086 return d_make_comp (di
, DEMANGLE_COMPONENT_GUARD
, d_name (di
), NULL
);
2090 struct demangle_component
*name
= d_name (di
);
2091 return d_make_comp (di
, DEMANGLE_COMPONENT_REFTEMP
, name
,
2092 d_number_component (di
));
2096 return d_make_comp (di
, DEMANGLE_COMPONENT_HIDDEN_ALIAS
,
2097 d_encoding (di
, 0), NULL
);
2100 switch (d_next_char (di
))
2103 return d_make_comp (di
, DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
,
2104 d_encoding (di
, 0), NULL
);
2106 /* ??? The proposal is that other letters (such as 'h') stand
2107 for different variants of transaction cloning, such as
2108 compiling directly for hardware transaction support. But
2109 they still should all be transactional clones of some sort
2110 so go ahead and call them that. */
2112 return d_make_comp (di
, DEMANGLE_COMPONENT_TRANSACTION_CLONE
,
2113 d_encoding (di
, 0), NULL
);
2117 return d_java_resource (di
);
2127 /* <call-offset> ::= h <nv-offset> _
2130 <nv-offset> ::= <(offset) number>
2132 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
2134 The C parameter, if not '\0', is a character we just read which is
2135 the start of the <call-offset>.
2137 We don't display the offset information anywhere. FIXME: We should
2138 display it in verbose mode. */
2141 d_call_offset (struct d_info
*di
, int c
)
2144 c
= d_next_char (di
);
2151 if (! d_check_char (di
, '_'))
2158 if (! d_check_char (di
, '_'))
2164 /* <ctor-dtor-name> ::= C1
2172 static struct demangle_component
*
2173 d_ctor_dtor_name (struct d_info
*di
)
2175 if (di
->last_name
!= NULL
)
2177 if (di
->last_name
->type
== DEMANGLE_COMPONENT_NAME
)
2178 di
->expansion
+= di
->last_name
->u
.s_name
.len
;
2179 else if (di
->last_name
->type
== DEMANGLE_COMPONENT_SUB_STD
)
2180 di
->expansion
+= di
->last_name
->u
.s_string
.len
;
2182 switch (d_peek_char (di
))
2186 enum gnu_v3_ctor_kinds kind
;
2189 if (d_peek_next_char (di
) == 'I')
2195 switch (d_peek_next_char (di
))
2198 kind
= gnu_v3_complete_object_ctor
;
2201 kind
= gnu_v3_base_object_ctor
;
2204 kind
= gnu_v3_complete_object_allocating_ctor
;
2207 kind
= gnu_v3_unified_ctor
;
2210 kind
= gnu_v3_object_ctor_group
;
2219 cplus_demangle_type (di
);
2221 return d_make_ctor (di
, kind
, di
->last_name
);
2226 enum gnu_v3_dtor_kinds kind
;
2228 switch (d_peek_next_char (di
))
2231 kind
= gnu_v3_deleting_dtor
;
2234 kind
= gnu_v3_complete_object_dtor
;
2237 kind
= gnu_v3_base_object_dtor
;
2239 /* digit '3' is not used */
2241 kind
= gnu_v3_unified_dtor
;
2244 kind
= gnu_v3_object_dtor_group
;
2250 return d_make_dtor (di
, kind
, di
->last_name
);
2258 /* True iff we're looking at an order-insensitive type-qualifier, including
2259 function-type-qualifiers. */
2262 next_is_type_qual (struct d_info
*di
)
2264 char peek
= d_peek_char (di
);
2265 if (peek
== 'r' || peek
== 'V' || peek
== 'K')
2269 peek
= d_peek_next_char (di
);
2270 if (peek
== 'x' || peek
== 'o' || peek
== 'O' || peek
== 'w')
2276 /* <type> ::= <builtin-type>
2278 ::= <class-enum-type>
2280 ::= <pointer-to-member-type>
2281 ::= <template-param>
2282 ::= <template-template-param> <template-args>
2284 ::= <CV-qualifiers> <type>
2287 ::= O <type> (C++0x)
2290 ::= U <source-name> <type>
2292 <builtin-type> ::= various one letter codes
2296 CP_STATIC_IF_GLIBCPP_V3
2297 const struct demangle_builtin_type_info
2298 cplus_demangle_builtin_types
[D_BUILTIN_TYPE_COUNT
] =
2300 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT
},
2301 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL
},
2302 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT
},
2303 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT
},
2304 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT
},
2305 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT
},
2306 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT
},
2307 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT
},
2308 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT
},
2309 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED
},
2310 /* k */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2311 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG
},
2312 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG
},
2313 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT
},
2314 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
2316 /* p */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2317 /* q */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2318 /* r */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2319 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT
},
2320 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT
},
2321 /* u */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2322 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID
},
2323 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT
},
2324 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG
},
2325 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
2326 D_PRINT_UNSIGNED_LONG_LONG
},
2327 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT
},
2328 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT
},
2329 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT
},
2330 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT
},
2331 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT
},
2332 /* 30 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT
},
2333 /* 31 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT
},
2334 /* 32 */ { NL ("decltype(nullptr)"), NL ("decltype(nullptr)"),
2338 CP_STATIC_IF_GLIBCPP_V3
2339 struct demangle_component
*
2340 cplus_demangle_type (struct d_info
*di
)
2343 struct demangle_component
*ret
;
2346 /* The ABI specifies that when CV-qualifiers are used, the base type
2347 is substitutable, and the fully qualified type is substitutable,
2348 but the base type with a strict subset of the CV-qualifiers is
2349 not substitutable. The natural recursive implementation of the
2350 CV-qualifiers would cause subsets to be substitutable, so instead
2351 we pull them all off now.
2353 FIXME: The ABI says that order-insensitive vendor qualifiers
2354 should be handled in the same way, but we have no way to tell
2355 which vendor qualifiers are order-insensitive and which are
2356 order-sensitive. So we just assume that they are all
2357 order-sensitive. g++ 3.4 supports only one vendor qualifier,
2358 __vector, and it treats it as order-sensitive when mangling
2361 if (next_is_type_qual (di
))
2363 struct demangle_component
**pret
;
2365 pret
= d_cv_qualifiers (di
, &ret
, 0);
2368 if (d_peek_char (di
) == 'F')
2370 /* cv-qualifiers before a function type apply to 'this',
2371 so avoid adding the unqualified function type to
2372 the substitution list. */
2373 *pret
= d_function_type (di
);
2376 *pret
= cplus_demangle_type (di
);
2379 if ((*pret
)->type
== DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
2380 || (*pret
)->type
== DEMANGLE_COMPONENT_REFERENCE_THIS
)
2382 /* Move the ref-qualifier outside the cv-qualifiers so that
2383 they are printed in the right order. */
2384 struct demangle_component
*fn
= d_left (*pret
);
2385 d_left (*pret
) = ret
;
2389 if (! d_add_substitution (di
, ret
))
2396 peek
= d_peek_char (di
);
2399 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2400 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
2401 case 'o': case 's': case 't':
2402 case 'v': case 'w': case 'x': case 'y': case 'z':
2403 ret
= d_make_builtin_type (di
,
2404 &cplus_demangle_builtin_types
[peek
- 'a']);
2405 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2412 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_VENDOR_TYPE
,
2413 d_source_name (di
), NULL
);
2417 ret
= d_function_type (di
);
2420 case '0': case '1': case '2': case '3': case '4':
2421 case '5': case '6': case '7': case '8': case '9':
2424 ret
= d_class_enum_type (di
);
2428 ret
= d_array_type (di
);
2432 ret
= d_pointer_to_member_type (di
);
2436 ret
= d_template_param (di
);
2437 if (d_peek_char (di
) == 'I')
2439 /* This may be <template-template-param> <template-args>.
2440 If this is the type for a conversion operator, we can
2441 have a <template-template-param> here only by following
2442 a derivation like this:
2445 -> <template-prefix> <template-args>
2446 -> <prefix> <template-unqualified-name> <template-args>
2447 -> <unqualified-name> <template-unqualified-name> <template-args>
2448 -> <source-name> <template-unqualified-name> <template-args>
2449 -> <source-name> <operator-name> <template-args>
2450 -> <source-name> cv <type> <template-args>
2451 -> <source-name> cv <template-template-param> <template-args> <template-args>
2453 where the <template-args> is followed by another.
2454 Otherwise, we must have a derivation like this:
2457 -> <template-prefix> <template-args>
2458 -> <prefix> <template-unqualified-name> <template-args>
2459 -> <unqualified-name> <template-unqualified-name> <template-args>
2460 -> <source-name> <template-unqualified-name> <template-args>
2461 -> <source-name> <operator-name> <template-args>
2462 -> <source-name> cv <type> <template-args>
2463 -> <source-name> cv <template-param> <template-args>
2465 where we need to leave the <template-args> to be processed
2466 by d_prefix (following the <template-prefix>).
2468 The <template-template-param> part is a substitution
2470 if (! di
->is_conversion
)
2472 if (! d_add_substitution (di
, ret
))
2474 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
,
2475 d_template_args (di
));
2479 struct demangle_component
*args
;
2480 struct d_info_checkpoint checkpoint
;
2482 d_checkpoint (di
, &checkpoint
);
2483 args
= d_template_args (di
);
2484 if (d_peek_char (di
) == 'I')
2486 if (! d_add_substitution (di
, ret
))
2488 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
,
2492 d_backtrack (di
, &checkpoint
);
2498 /* If this is a special substitution, then it is the start of
2499 <class-enum-type>. */
2503 peek_next
= d_peek_next_char (di
);
2504 if (IS_DIGIT (peek_next
)
2506 || IS_UPPER (peek_next
))
2508 ret
= d_substitution (di
, 0);
2509 /* The substituted name may have been a template name and
2510 may be followed by tepmlate args. */
2511 if (d_peek_char (di
) == 'I')
2512 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
,
2513 d_template_args (di
));
2519 ret
= d_class_enum_type (di
);
2520 /* If the substitution was a complete type, then it is not
2521 a new substitution candidate. However, if the
2522 substitution was followed by template arguments, then
2523 the whole thing is a substitution candidate. */
2524 if (ret
!= NULL
&& ret
->type
== DEMANGLE_COMPONENT_SUB_STD
)
2532 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_RVALUE_REFERENCE
,
2533 cplus_demangle_type (di
), NULL
);
2538 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_POINTER
,
2539 cplus_demangle_type (di
), NULL
);
2544 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_REFERENCE
,
2545 cplus_demangle_type (di
), NULL
);
2550 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_COMPLEX
,
2551 cplus_demangle_type (di
), NULL
);
2556 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_IMAGINARY
,
2557 cplus_demangle_type (di
), NULL
);
2562 ret
= d_source_name (di
);
2563 if (d_peek_char (di
) == 'I')
2564 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
,
2565 d_template_args (di
));
2566 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
,
2567 cplus_demangle_type (di
), ret
);
2573 peek
= d_next_char (di
);
2578 /* decltype (expression) */
2579 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_DECLTYPE
,
2580 d_expression (di
), NULL
);
2581 if (ret
&& d_next_char (di
) != 'E')
2587 /* Pack expansion. */
2588 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_PACK_EXPANSION
,
2589 cplus_demangle_type (di
), NULL
);
2595 ret
= d_make_name (di
, "auto", 4);
2598 /* decltype(auto) */
2599 ret
= d_make_name (di
, "decltype(auto)", 14);
2603 /* 32-bit decimal floating point */
2604 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[26]);
2605 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2609 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[27]);
2610 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2614 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[28]);
2615 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2618 /* 16-bit half-precision FP */
2619 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[29]);
2620 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2624 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[30]);
2625 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2629 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[31]);
2630 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2634 /* Fixed point types. DF<int bits><length><fract bits><sat> */
2635 ret
= d_make_empty (di
);
2636 ret
->type
= DEMANGLE_COMPONENT_FIXED_TYPE
;
2637 if ((ret
->u
.s_fixed
.accum
= IS_DIGIT (d_peek_char (di
))))
2638 /* For demangling we don't care about the bits. */
2640 ret
->u
.s_fixed
.length
= cplus_demangle_type (di
);
2641 if (ret
->u
.s_fixed
.length
== NULL
)
2644 peek
= d_next_char (di
);
2645 ret
->u
.s_fixed
.sat
= (peek
== 's');
2649 ret
= d_vector_type (di
);
2654 /* decltype(nullptr) */
2655 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[32]);
2656 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2670 if (! d_add_substitution (di
, ret
))
2677 /* <CV-qualifiers> ::= [r] [V] [K] [Dx] */
2679 static struct demangle_component
**
2680 d_cv_qualifiers (struct d_info
*di
,
2681 struct demangle_component
**pret
, int member_fn
)
2683 struct demangle_component
**pstart
;
2687 peek
= d_peek_char (di
);
2688 while (next_is_type_qual (di
))
2690 enum demangle_component_type t
;
2691 struct demangle_component
*right
= NULL
;
2697 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2698 : DEMANGLE_COMPONENT_RESTRICT
);
2699 di
->expansion
+= sizeof "restrict";
2701 else if (peek
== 'V')
2704 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2705 : DEMANGLE_COMPONENT_VOLATILE
);
2706 di
->expansion
+= sizeof "volatile";
2708 else if (peek
== 'K')
2711 ? DEMANGLE_COMPONENT_CONST_THIS
2712 : DEMANGLE_COMPONENT_CONST
);
2713 di
->expansion
+= sizeof "const";
2717 peek
= d_next_char (di
);
2720 t
= DEMANGLE_COMPONENT_TRANSACTION_SAFE
;
2721 di
->expansion
+= sizeof "transaction_safe";
2723 else if (peek
== 'o'
2726 t
= DEMANGLE_COMPONENT_NOEXCEPT
;
2727 di
->expansion
+= sizeof "noexcept";
2730 right
= d_expression (di
);
2733 if (! d_check_char (di
, 'E'))
2737 else if (peek
== 'w')
2739 t
= DEMANGLE_COMPONENT_THROW_SPEC
;
2740 di
->expansion
+= sizeof "throw";
2741 right
= d_parmlist (di
);
2744 if (! d_check_char (di
, 'E'))
2751 *pret
= d_make_comp (di
, t
, NULL
, right
);
2754 pret
= &d_left (*pret
);
2756 peek
= d_peek_char (di
);
2759 if (!member_fn
&& peek
== 'F')
2761 while (pstart
!= pret
)
2763 switch ((*pstart
)->type
)
2765 case DEMANGLE_COMPONENT_RESTRICT
:
2766 (*pstart
)->type
= DEMANGLE_COMPONENT_RESTRICT_THIS
;
2768 case DEMANGLE_COMPONENT_VOLATILE
:
2769 (*pstart
)->type
= DEMANGLE_COMPONENT_VOLATILE_THIS
;
2771 case DEMANGLE_COMPONENT_CONST
:
2772 (*pstart
)->type
= DEMANGLE_COMPONENT_CONST_THIS
;
2777 pstart
= &d_left (*pstart
);
2784 /* <ref-qualifier> ::= R
2787 static struct demangle_component
*
2788 d_ref_qualifier (struct d_info
*di
, struct demangle_component
*sub
)
2790 struct demangle_component
*ret
= sub
;
2793 peek
= d_peek_char (di
);
2794 if (peek
== 'R' || peek
== 'O')
2796 enum demangle_component_type t
;
2799 t
= DEMANGLE_COMPONENT_REFERENCE_THIS
;
2800 di
->expansion
+= sizeof "&";
2804 t
= DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
;
2805 di
->expansion
+= sizeof "&&";
2809 ret
= d_make_comp (di
, t
, ret
, NULL
);
2815 /* <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] [T] E */
2817 static struct demangle_component
*
2818 d_function_type (struct d_info
*di
)
2820 struct demangle_component
*ret
;
2822 if (! d_check_char (di
, 'F'))
2824 if (d_peek_char (di
) == 'Y')
2826 /* Function has C linkage. We don't print this information.
2827 FIXME: We should print it in verbose mode. */
2830 ret
= d_bare_function_type (di
, 1);
2831 ret
= d_ref_qualifier (di
, ret
);
2833 if (! d_check_char (di
, 'E'))
2840 static struct demangle_component
*
2841 d_parmlist (struct d_info
*di
)
2843 struct demangle_component
*tl
;
2844 struct demangle_component
**ptl
;
2850 struct demangle_component
*type
;
2852 char peek
= d_peek_char (di
);
2853 if (peek
== '\0' || peek
== 'E' || peek
== '.')
2855 if ((peek
== 'R' || peek
== 'O')
2856 && d_peek_next_char (di
) == 'E')
2857 /* Function ref-qualifier, not a ref prefix for a parameter type. */
2859 type
= cplus_demangle_type (di
);
2862 *ptl
= d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, type
, NULL
);
2865 ptl
= &d_right (*ptl
);
2868 /* There should be at least one parameter type besides the optional
2869 return type. A function which takes no arguments will have a
2870 single parameter type void. */
2874 /* If we have a single parameter type void, omit it. */
2875 if (d_right (tl
) == NULL
2876 && d_left (tl
)->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
2877 && d_left (tl
)->u
.s_builtin
.type
->print
== D_PRINT_VOID
)
2879 di
->expansion
-= d_left (tl
)->u
.s_builtin
.type
->len
;
2886 /* <bare-function-type> ::= [J]<type>+ */
2888 static struct demangle_component
*
2889 d_bare_function_type (struct d_info
*di
, int has_return_type
)
2891 struct demangle_component
*return_type
;
2892 struct demangle_component
*tl
;
2895 /* Detect special qualifier indicating that the first argument
2896 is the return type. */
2897 peek
= d_peek_char (di
);
2901 has_return_type
= 1;
2904 if (has_return_type
)
2906 return_type
= cplus_demangle_type (di
);
2907 if (return_type
== NULL
)
2913 tl
= d_parmlist (di
);
2917 return d_make_comp (di
, DEMANGLE_COMPONENT_FUNCTION_TYPE
,
2921 /* <class-enum-type> ::= <name> */
2923 static struct demangle_component
*
2924 d_class_enum_type (struct d_info
*di
)
2929 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2930 ::= A [<(dimension) expression>] _ <(element) type>
2933 static struct demangle_component
*
2934 d_array_type (struct d_info
*di
)
2937 struct demangle_component
*dim
;
2939 if (! d_check_char (di
, 'A'))
2942 peek
= d_peek_char (di
);
2945 else if (IS_DIGIT (peek
))
2953 peek
= d_peek_char (di
);
2955 while (IS_DIGIT (peek
));
2956 dim
= d_make_name (di
, s
, d_str (di
) - s
);
2962 dim
= d_expression (di
);
2967 if (! d_check_char (di
, '_'))
2970 return d_make_comp (di
, DEMANGLE_COMPONENT_ARRAY_TYPE
, dim
,
2971 cplus_demangle_type (di
));
2974 /* <vector-type> ::= Dv <number> _ <type>
2975 ::= Dv _ <expression> _ <type> */
2977 static struct demangle_component
*
2978 d_vector_type (struct d_info
*di
)
2981 struct demangle_component
*dim
;
2983 peek
= d_peek_char (di
);
2987 dim
= d_expression (di
);
2990 dim
= d_number_component (di
);
2995 if (! d_check_char (di
, '_'))
2998 return d_make_comp (di
, DEMANGLE_COMPONENT_VECTOR_TYPE
, dim
,
2999 cplus_demangle_type (di
));
3002 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
3004 static struct demangle_component
*
3005 d_pointer_to_member_type (struct d_info
*di
)
3007 struct demangle_component
*cl
;
3008 struct demangle_component
*mem
;
3010 if (! d_check_char (di
, 'M'))
3013 cl
= cplus_demangle_type (di
);
3017 /* The ABI says, "The type of a non-static member function is considered
3018 to be different, for the purposes of substitution, from the type of a
3019 namespace-scope or static member function whose type appears
3020 similar. The types of two non-static member functions are considered
3021 to be different, for the purposes of substitution, if the functions
3022 are members of different classes. In other words, for the purposes of
3023 substitution, the class of which the function is a member is
3024 considered part of the type of function."
3026 For a pointer to member function, this call to cplus_demangle_type
3027 will end up adding a (possibly qualified) non-member function type to
3028 the substitution table, which is not correct; however, the member
3029 function type will never be used in a substitution, so putting the
3030 wrong type in the substitution table is harmless. */
3032 mem
= cplus_demangle_type (di
);
3036 return d_make_comp (di
, DEMANGLE_COMPONENT_PTRMEM_TYPE
, cl
, mem
);
3039 /* <non-negative number> _ */
3042 d_compact_number (struct d_info
*di
)
3045 if (d_peek_char (di
) == '_')
3047 else if (d_peek_char (di
) == 'n')
3050 num
= d_number (di
) + 1;
3052 if (num
< 0 || ! d_check_char (di
, '_'))
3057 /* <template-param> ::= T_
3058 ::= T <(parameter-2 non-negative) number> _
3061 static struct demangle_component
*
3062 d_template_param (struct d_info
*di
)
3066 if (! d_check_char (di
, 'T'))
3069 param
= d_compact_number (di
);
3075 return d_make_template_param (di
, param
);
3078 /* <template-args> ::= I <template-arg>+ E */
3080 static struct demangle_component
*
3081 d_template_args (struct d_info
*di
)
3083 if (d_peek_char (di
) != 'I'
3084 && d_peek_char (di
) != 'J')
3088 return d_template_args_1 (di
);
3091 /* <template-arg>* E */
3093 static struct demangle_component
*
3094 d_template_args_1 (struct d_info
*di
)
3096 struct demangle_component
*hold_last_name
;
3097 struct demangle_component
*al
;
3098 struct demangle_component
**pal
;
3100 /* Preserve the last name we saw--don't let the template arguments
3101 clobber it, as that would give us the wrong name for a subsequent
3102 constructor or destructor. */
3103 hold_last_name
= di
->last_name
;
3105 if (d_peek_char (di
) == 'E')
3107 /* An argument pack can be empty. */
3109 return d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
, NULL
, NULL
);
3116 struct demangle_component
*a
;
3118 a
= d_template_arg (di
);
3122 *pal
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
, a
, NULL
);
3125 pal
= &d_right (*pal
);
3127 if (d_peek_char (di
) == 'E')
3134 di
->last_name
= hold_last_name
;
3139 /* <template-arg> ::= <type>
3140 ::= X <expression> E
3144 static struct demangle_component
*
3145 d_template_arg (struct d_info
*di
)
3147 struct demangle_component
*ret
;
3149 switch (d_peek_char (di
))
3153 ret
= d_expression (di
);
3154 if (! d_check_char (di
, 'E'))
3159 return d_expr_primary (di
);
3163 /* An argument pack. */
3164 return d_template_args (di
);
3167 return cplus_demangle_type (di
);
3171 /* Parse a sequence of expressions until we hit the terminator
3174 static struct demangle_component
*
3175 d_exprlist (struct d_info
*di
, char terminator
)
3177 struct demangle_component
*list
= NULL
;
3178 struct demangle_component
**p
= &list
;
3180 if (d_peek_char (di
) == terminator
)
3183 return d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, NULL
, NULL
);
3188 struct demangle_component
*arg
= d_expression (di
);
3192 *p
= d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, arg
, NULL
);
3197 if (d_peek_char (di
) == terminator
)
3207 /* Returns nonzero iff OP is an operator for a C++ cast: const_cast,
3208 dynamic_cast, static_cast or reinterpret_cast. */
3211 op_is_new_cast (struct demangle_component
*op
)
3213 const char *code
= op
->u
.s_operator
.op
->code
;
3214 return (code
[1] == 'c'
3215 && (code
[0] == 's' || code
[0] == 'd'
3216 || code
[0] == 'c' || code
[0] == 'r'));
3219 /* <expression> ::= <(unary) operator-name> <expression>
3220 ::= <(binary) operator-name> <expression> <expression>
3221 ::= <(trinary) operator-name> <expression> <expression> <expression>
3222 ::= cl <expression>+ E
3224 ::= <template-param>
3225 ::= sr <type> <unqualified-name>
3226 ::= sr <type> <unqualified-name> <template-args>
3230 static inline struct demangle_component
*
3231 d_expression_1 (struct d_info
*di
)
3235 peek
= d_peek_char (di
);
3237 return d_expr_primary (di
);
3238 else if (peek
== 'T')
3239 return d_template_param (di
);
3240 else if (peek
== 's' && d_peek_next_char (di
) == 'r')
3242 struct demangle_component
*type
;
3243 struct demangle_component
*name
;
3246 type
= cplus_demangle_type (di
);
3247 name
= d_unqualified_name (di
);
3248 if (d_peek_char (di
) != 'I')
3249 return d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
, type
, name
);
3251 return d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
, type
,
3252 d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, name
,
3253 d_template_args (di
)));
3255 else if (peek
== 's' && d_peek_next_char (di
) == 'p')
3258 return d_make_comp (di
, DEMANGLE_COMPONENT_PACK_EXPANSION
,
3259 d_expression_1 (di
), NULL
);
3261 else if (peek
== 'f' && d_peek_next_char (di
) == 'p')
3263 /* Function parameter used in a late-specified return type. */
3266 if (d_peek_char (di
) == 'T')
3268 /* 'this' parameter. */
3274 index
= d_compact_number (di
);
3275 if (index
== INT_MAX
|| index
== -1)
3279 return d_make_function_param (di
, index
);
3281 else if (IS_DIGIT (peek
)
3282 || (peek
== 'o' && d_peek_next_char (di
) == 'n'))
3284 /* We can get an unqualified name as an expression in the case of
3285 a dependent function call, i.e. decltype(f(t)). */
3286 struct demangle_component
*name
;
3289 /* operator-function-id, i.e. operator+(t). */
3292 name
= d_unqualified_name (di
);
3295 if (d_peek_char (di
) == 'I')
3296 return d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, name
,
3297 d_template_args (di
));
3301 else if ((peek
== 'i' || peek
== 't')
3302 && d_peek_next_char (di
) == 'l')
3304 /* Brace-enclosed initializer list, untyped or typed. */
3305 struct demangle_component
*type
= NULL
;
3307 type
= cplus_demangle_type (di
);
3308 if (!d_peek_next_char (di
))
3311 return d_make_comp (di
, DEMANGLE_COMPONENT_INITIALIZER_LIST
,
3312 type
, d_exprlist (di
, 'E'));
3316 struct demangle_component
*op
;
3317 const char *code
= NULL
;
3320 op
= d_operator_name (di
);
3324 if (op
->type
== DEMANGLE_COMPONENT_OPERATOR
)
3326 code
= op
->u
.s_operator
.op
->code
;
3327 di
->expansion
+= op
->u
.s_operator
.op
->len
- 2;
3328 if (strcmp (code
, "st") == 0)
3329 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
3330 cplus_demangle_type (di
));
3337 case DEMANGLE_COMPONENT_OPERATOR
:
3338 args
= op
->u
.s_operator
.op
->args
;
3340 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
3341 args
= op
->u
.s_extended_operator
.args
;
3343 case DEMANGLE_COMPONENT_CAST
:
3351 return d_make_comp (di
, DEMANGLE_COMPONENT_NULLARY
, op
, NULL
);
3355 struct demangle_component
*operand
;
3358 if (code
&& (code
[0] == 'p' || code
[0] == 'm')
3359 && code
[1] == code
[0])
3360 /* pp_ and mm_ are the prefix variants. */
3361 suffix
= !d_check_char (di
, '_');
3363 if (op
->type
== DEMANGLE_COMPONENT_CAST
3364 && d_check_char (di
, '_'))
3365 operand
= d_exprlist (di
, 'E');
3366 else if (code
&& !strcmp (code
, "sP"))
3367 operand
= d_template_args_1 (di
);
3369 operand
= d_expression_1 (di
);
3372 /* Indicate the suffix variant for d_print_comp. */
3373 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
3375 DEMANGLE_COMPONENT_BINARY_ARGS
,
3378 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
3383 struct demangle_component
*left
;
3384 struct demangle_component
*right
;
3388 if (op_is_new_cast (op
))
3389 left
= cplus_demangle_type (di
);
3390 else if (code
[0] == 'f')
3391 /* fold-expression. */
3392 left
= d_operator_name (di
);
3394 left
= d_expression_1 (di
);
3395 if (!strcmp (code
, "cl"))
3396 right
= d_exprlist (di
, 'E');
3397 else if (!strcmp (code
, "dt") || !strcmp (code
, "pt"))
3399 right
= d_unqualified_name (di
);
3400 if (d_peek_char (di
) == 'I')
3401 right
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
,
3402 right
, d_template_args (di
));
3405 right
= d_expression_1 (di
);
3407 return d_make_comp (di
, DEMANGLE_COMPONENT_BINARY
, op
,
3409 DEMANGLE_COMPONENT_BINARY_ARGS
,
3414 struct demangle_component
*first
;
3415 struct demangle_component
*second
;
3416 struct demangle_component
*third
;
3420 else if (!strcmp (code
, "qu"))
3422 /* ?: expression. */
3423 first
= d_expression_1 (di
);
3424 second
= d_expression_1 (di
);
3425 third
= d_expression_1 (di
);
3429 else if (code
[0] == 'f')
3431 /* fold-expression. */
3432 first
= d_operator_name (di
);
3433 second
= d_expression_1 (di
);
3434 third
= d_expression_1 (di
);
3438 else if (code
[0] == 'n')
3440 /* new-expression. */
3441 if (code
[1] != 'w' && code
[1] != 'a')
3443 first
= d_exprlist (di
, '_');
3444 second
= cplus_demangle_type (di
);
3445 if (d_peek_char (di
) == 'E')
3450 else if (d_peek_char (di
) == 'p'
3451 && d_peek_next_char (di
) == 'i')
3453 /* Parenthesized initializer. */
3455 third
= d_exprlist (di
, 'E');
3457 else if (d_peek_char (di
) == 'i'
3458 && d_peek_next_char (di
) == 'l')
3459 /* initializer-list. */
3460 third
= d_expression_1 (di
);
3466 return d_make_comp (di
, DEMANGLE_COMPONENT_TRINARY
, op
,
3468 DEMANGLE_COMPONENT_TRINARY_ARG1
,
3471 DEMANGLE_COMPONENT_TRINARY_ARG2
,
3480 static struct demangle_component
*
3481 d_expression (struct d_info
*di
)
3483 struct demangle_component
*ret
;
3484 int was_expression
= di
->is_expression
;
3486 di
->is_expression
= 1;
3487 ret
= d_expression_1 (di
);
3488 di
->is_expression
= was_expression
;
3492 /* <expr-primary> ::= L <type> <(value) number> E
3493 ::= L <type> <(value) float> E
3494 ::= L <mangled-name> E
3497 static struct demangle_component
*
3498 d_expr_primary (struct d_info
*di
)
3500 struct demangle_component
*ret
;
3502 if (! d_check_char (di
, 'L'))
3504 if (d_peek_char (di
) == '_'
3505 /* Workaround for G++ bug; see comment in write_template_arg. */
3506 || d_peek_char (di
) == 'Z')
3507 ret
= cplus_demangle_mangled_name (di
, 0);
3510 struct demangle_component
*type
;
3511 enum demangle_component_type t
;
3514 type
= cplus_demangle_type (di
);
3518 /* If we have a type we know how to print, we aren't going to
3519 print the type name itself. */
3520 if (type
->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
3521 && type
->u
.s_builtin
.type
->print
!= D_PRINT_DEFAULT
)
3522 di
->expansion
-= type
->u
.s_builtin
.type
->len
;
3524 /* Rather than try to interpret the literal value, we just
3525 collect it as a string. Note that it's possible to have a
3526 floating point literal here. The ABI specifies that the
3527 format of such literals is machine independent. That's fine,
3528 but what's not fine is that versions of g++ up to 3.2 with
3529 -fabi-version=1 used upper case letters in the hex constant,
3530 and dumped out gcc's internal representation. That makes it
3531 hard to tell where the constant ends, and hard to dump the
3532 constant in any readable form anyhow. We don't attempt to
3533 handle these cases. */
3535 t
= DEMANGLE_COMPONENT_LITERAL
;
3536 if (d_peek_char (di
) == 'n')
3538 t
= DEMANGLE_COMPONENT_LITERAL_NEG
;
3542 while (d_peek_char (di
) != 'E')
3544 if (d_peek_char (di
) == '\0')
3548 ret
= d_make_comp (di
, t
, type
, d_make_name (di
, s
, d_str (di
) - s
));
3550 if (! d_check_char (di
, 'E'))
3555 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
3556 ::= Z <(function) encoding> E s [<discriminator>]
3557 ::= Z <(function) encoding> E d [<parameter> number>] _ <entity name>
3560 static struct demangle_component
*
3561 d_local_name (struct d_info
*di
)
3563 struct demangle_component
*function
;
3565 if (! d_check_char (di
, 'Z'))
3568 function
= d_encoding (di
, 0);
3570 if (! d_check_char (di
, 'E'))
3573 if (d_peek_char (di
) == 's')
3576 if (! d_discriminator (di
))
3578 return d_make_comp (di
, DEMANGLE_COMPONENT_LOCAL_NAME
, function
,
3579 d_make_name (di
, "string literal",
3580 sizeof "string literal" - 1));
3584 struct demangle_component
*name
;
3587 if (d_peek_char (di
) == 'd')
3589 /* Default argument scope: d <number> _. */
3591 num
= d_compact_number (di
);
3600 /* Lambdas and unnamed types have internal discriminators. */
3601 case DEMANGLE_COMPONENT_LAMBDA
:
3602 case DEMANGLE_COMPONENT_UNNAMED_TYPE
:
3605 if (! d_discriminator (di
))
3609 name
= d_make_default_arg (di
, num
, name
);
3610 return d_make_comp (di
, DEMANGLE_COMPONENT_LOCAL_NAME
, function
, name
);
3614 /* <discriminator> ::= _ <number> # when number < 10
3615 ::= __ <number> _ # when number >= 10
3617 <discriminator> ::= _ <number> # when number >=10
3618 is also accepted to support gcc versions that wrongly mangled that way.
3620 We demangle the discriminator, but we don't print it out. FIXME:
3621 We should print it out in verbose mode. */
3624 d_discriminator (struct d_info
*di
)
3626 int discrim
, num_underscores
= 1;
3628 if (d_peek_char (di
) != '_')
3631 if (d_peek_char (di
) == '_')
3637 discrim
= d_number (di
);
3640 if (num_underscores
> 1 && discrim
>= 10)
3642 if (d_peek_char (di
) == '_')
3651 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ */
3653 static struct demangle_component
*
3654 d_lambda (struct d_info
*di
)
3656 struct demangle_component
*tl
;
3657 struct demangle_component
*ret
;
3660 if (! d_check_char (di
, 'U'))
3662 if (! d_check_char (di
, 'l'))
3665 tl
= d_parmlist (di
);
3669 if (! d_check_char (di
, 'E'))
3672 num
= d_compact_number (di
);
3676 ret
= d_make_empty (di
);
3679 ret
->type
= DEMANGLE_COMPONENT_LAMBDA
;
3680 ret
->u
.s_unary_num
.sub
= tl
;
3681 ret
->u
.s_unary_num
.num
= num
;
3684 if (! d_add_substitution (di
, ret
))
3690 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
3692 static struct demangle_component
*
3693 d_unnamed_type (struct d_info
*di
)
3695 struct demangle_component
*ret
;
3698 if (! d_check_char (di
, 'U'))
3700 if (! d_check_char (di
, 't'))
3703 num
= d_compact_number (di
);
3707 ret
= d_make_empty (di
);
3710 ret
->type
= DEMANGLE_COMPONENT_UNNAMED_TYPE
;
3711 ret
->u
.s_number
.number
= num
;
3714 if (! d_add_substitution (di
, ret
))
3720 /* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]*
3723 static struct demangle_component
*
3724 d_clone_suffix (struct d_info
*di
, struct demangle_component
*encoding
)
3726 const char *suffix
= d_str (di
);
3727 const char *pend
= suffix
;
3728 struct demangle_component
*n
;
3730 if (*pend
== '.' && (IS_LOWER (pend
[1]) || pend
[1] == '_'))
3733 while (IS_LOWER (*pend
) || *pend
== '_')
3736 while (*pend
== '.' && IS_DIGIT (pend
[1]))
3739 while (IS_DIGIT (*pend
))
3742 d_advance (di
, pend
- suffix
);
3743 n
= d_make_name (di
, suffix
, pend
- suffix
);
3744 return d_make_comp (di
, DEMANGLE_COMPONENT_CLONE
, encoding
, n
);
3747 /* Add a new substitution. */
3750 d_add_substitution (struct d_info
*di
, struct demangle_component
*dc
)
3754 if (di
->next_sub
>= di
->num_subs
)
3756 di
->subs
[di
->next_sub
] = dc
;
3761 /* <substitution> ::= S <seq-id> _
3771 If PREFIX is non-zero, then this type is being used as a prefix in
3772 a qualified name. In this case, for the standard substitutions, we
3773 need to check whether we are being used as a prefix for a
3774 constructor or destructor, and return a full template name.
3775 Otherwise we will get something like std::iostream::~iostream()
3776 which does not correspond particularly well to any function which
3777 actually appears in the source.
3780 static const struct d_standard_sub_info standard_subs
[] =
3785 { 'a', NL ("std::allocator"),
3786 NL ("std::allocator"),
3788 { 'b', NL ("std::basic_string"),
3789 NL ("std::basic_string"),
3790 NL ("basic_string") },
3791 { 's', NL ("std::string"),
3792 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
3793 NL ("basic_string") },
3794 { 'i', NL ("std::istream"),
3795 NL ("std::basic_istream<char, std::char_traits<char> >"),
3796 NL ("basic_istream") },
3797 { 'o', NL ("std::ostream"),
3798 NL ("std::basic_ostream<char, std::char_traits<char> >"),
3799 NL ("basic_ostream") },
3800 { 'd', NL ("std::iostream"),
3801 NL ("std::basic_iostream<char, std::char_traits<char> >"),
3802 NL ("basic_iostream") }
3805 static struct demangle_component
*
3806 d_substitution (struct d_info
*di
, int prefix
)
3810 if (! d_check_char (di
, 'S'))
3813 c
= d_next_char (di
);
3814 if (c
== '_' || IS_DIGIT (c
) || IS_UPPER (c
))
3823 unsigned int new_id
;
3826 new_id
= id
* 36 + c
- '0';
3827 else if (IS_UPPER (c
))
3828 new_id
= id
* 36 + c
- 'A' + 10;
3834 c
= d_next_char (di
);
3841 if (id
>= (unsigned int) di
->next_sub
)
3846 return di
->subs
[id
];
3851 const struct d_standard_sub_info
*p
;
3852 const struct d_standard_sub_info
*pend
;
3854 verbose
= (di
->options
& DMGL_VERBOSE
) != 0;
3855 if (! verbose
&& prefix
)
3859 peek
= d_peek_char (di
);
3860 if (peek
== 'C' || peek
== 'D')
3864 pend
= (&standard_subs
[0]
3865 + sizeof standard_subs
/ sizeof standard_subs
[0]);
3866 for (p
= &standard_subs
[0]; p
< pend
; ++p
)
3872 struct demangle_component
*dc
;
3874 if (p
->set_last_name
!= NULL
)
3875 di
->last_name
= d_make_sub (di
, p
->set_last_name
,
3876 p
->set_last_name_len
);
3879 s
= p
->full_expansion
;
3884 s
= p
->simple_expansion
;
3885 len
= p
->simple_len
;
3887 di
->expansion
+= len
;
3888 dc
= d_make_sub (di
, s
, len
);
3889 if (d_peek_char (di
) == 'B')
3891 /* If there are ABI tags on the abbreviation, it becomes
3892 a substitution candidate. */
3893 dc
= d_abi_tags (di
, dc
);
3894 d_add_substitution (di
, dc
);
3905 d_checkpoint (struct d_info
*di
, struct d_info_checkpoint
*checkpoint
)
3907 checkpoint
->n
= di
->n
;
3908 checkpoint
->next_comp
= di
->next_comp
;
3909 checkpoint
->next_sub
= di
->next_sub
;
3910 checkpoint
->did_subs
= di
->did_subs
;
3911 checkpoint
->expansion
= di
->expansion
;
3915 d_backtrack (struct d_info
*di
, struct d_info_checkpoint
*checkpoint
)
3917 di
->n
= checkpoint
->n
;
3918 di
->next_comp
= checkpoint
->next_comp
;
3919 di
->next_sub
= checkpoint
->next_sub
;
3920 di
->did_subs
= checkpoint
->did_subs
;
3921 di
->expansion
= checkpoint
->expansion
;
3924 /* Initialize a growable string. */
3927 d_growable_string_init (struct d_growable_string
*dgs
, size_t estimate
)
3932 dgs
->allocation_failure
= 0;
3935 d_growable_string_resize (dgs
, estimate
);
3938 /* Grow a growable string to a given size. */
3941 d_growable_string_resize (struct d_growable_string
*dgs
, size_t need
)
3946 if (dgs
->allocation_failure
)
3949 /* Start allocation at two bytes to avoid any possibility of confusion
3950 with the special value of 1 used as a return in *palc to indicate
3951 allocation failures. */
3952 newalc
= dgs
->alc
> 0 ? dgs
->alc
: 2;
3953 while (newalc
< need
)
3956 newbuf
= (char *) realloc (dgs
->buf
, newalc
);
3963 dgs
->allocation_failure
= 1;
3970 /* Append a buffer to a growable string. */
3973 d_growable_string_append_buffer (struct d_growable_string
*dgs
,
3974 const char *s
, size_t l
)
3978 need
= dgs
->len
+ l
+ 1;
3979 if (need
> dgs
->alc
)
3980 d_growable_string_resize (dgs
, need
);
3982 if (dgs
->allocation_failure
)
3985 memcpy (dgs
->buf
+ dgs
->len
, s
, l
);
3986 dgs
->buf
[dgs
->len
+ l
] = '\0';
3990 /* Bridge growable strings to the callback mechanism. */
3993 d_growable_string_callback_adapter (const char *s
, size_t l
, void *opaque
)
3995 struct d_growable_string
*dgs
= (struct d_growable_string
*) opaque
;
3997 d_growable_string_append_buffer (dgs
, s
, l
);
4000 /* Walk the tree, counting the number of templates encountered, and
4001 the number of times a scope might be saved. These counts will be
4002 used to allocate data structures for d_print_comp, so the logic
4003 here must mirror the logic d_print_comp will use. It is not
4004 important that the resulting numbers are exact, so long as they
4005 are larger than the actual numbers encountered. */
4008 d_count_templates_scopes (int *num_templates
, int *num_scopes
,
4009 const struct demangle_component
*dc
)
4016 case DEMANGLE_COMPONENT_NAME
:
4017 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
4018 case DEMANGLE_COMPONENT_FUNCTION_PARAM
:
4019 case DEMANGLE_COMPONENT_SUB_STD
:
4020 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
4021 case DEMANGLE_COMPONENT_OPERATOR
:
4022 case DEMANGLE_COMPONENT_CHARACTER
:
4023 case DEMANGLE_COMPONENT_NUMBER
:
4024 case DEMANGLE_COMPONENT_UNNAMED_TYPE
:
4027 case DEMANGLE_COMPONENT_TEMPLATE
:
4029 goto recurse_left_right
;
4031 case DEMANGLE_COMPONENT_REFERENCE
:
4032 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
4033 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_TEMPLATE_PARAM
)
4035 goto recurse_left_right
;
4037 case DEMANGLE_COMPONENT_QUAL_NAME
:
4038 case DEMANGLE_COMPONENT_LOCAL_NAME
:
4039 case DEMANGLE_COMPONENT_TYPED_NAME
:
4040 case DEMANGLE_COMPONENT_VTABLE
:
4041 case DEMANGLE_COMPONENT_VTT
:
4042 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
4043 case DEMANGLE_COMPONENT_TYPEINFO
:
4044 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
4045 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
4046 case DEMANGLE_COMPONENT_THUNK
:
4047 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
4048 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
4049 case DEMANGLE_COMPONENT_JAVA_CLASS
:
4050 case DEMANGLE_COMPONENT_GUARD
:
4051 case DEMANGLE_COMPONENT_TLS_INIT
:
4052 case DEMANGLE_COMPONENT_TLS_WRAPPER
:
4053 case DEMANGLE_COMPONENT_REFTEMP
:
4054 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
4055 case DEMANGLE_COMPONENT_RESTRICT
:
4056 case DEMANGLE_COMPONENT_VOLATILE
:
4057 case DEMANGLE_COMPONENT_CONST
:
4058 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
4059 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
4060 case DEMANGLE_COMPONENT_CONST_THIS
:
4061 case DEMANGLE_COMPONENT_REFERENCE_THIS
:
4062 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
:
4063 case DEMANGLE_COMPONENT_TRANSACTION_SAFE
:
4064 case DEMANGLE_COMPONENT_NOEXCEPT
:
4065 case DEMANGLE_COMPONENT_THROW_SPEC
:
4066 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
4067 case DEMANGLE_COMPONENT_POINTER
:
4068 case DEMANGLE_COMPONENT_COMPLEX
:
4069 case DEMANGLE_COMPONENT_IMAGINARY
:
4070 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
4071 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
4072 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
4073 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
4074 case DEMANGLE_COMPONENT_VECTOR_TYPE
:
4075 case DEMANGLE_COMPONENT_ARGLIST
:
4076 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
4077 case DEMANGLE_COMPONENT_INITIALIZER_LIST
:
4078 case DEMANGLE_COMPONENT_CAST
:
4079 case DEMANGLE_COMPONENT_CONVERSION
:
4080 case DEMANGLE_COMPONENT_NULLARY
:
4081 case DEMANGLE_COMPONENT_UNARY
:
4082 case DEMANGLE_COMPONENT_BINARY
:
4083 case DEMANGLE_COMPONENT_BINARY_ARGS
:
4084 case DEMANGLE_COMPONENT_TRINARY
:
4085 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
4086 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
4087 case DEMANGLE_COMPONENT_LITERAL
:
4088 case DEMANGLE_COMPONENT_LITERAL_NEG
:
4089 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
4090 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
4091 case DEMANGLE_COMPONENT_DECLTYPE
:
4092 case DEMANGLE_COMPONENT_TRANSACTION_CLONE
:
4093 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
:
4094 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
4095 case DEMANGLE_COMPONENT_TAGGED_NAME
:
4096 case DEMANGLE_COMPONENT_CLONE
:
4098 d_count_templates_scopes (num_templates
, num_scopes
,
4100 d_count_templates_scopes (num_templates
, num_scopes
,
4104 case DEMANGLE_COMPONENT_CTOR
:
4105 d_count_templates_scopes (num_templates
, num_scopes
,
4109 case DEMANGLE_COMPONENT_DTOR
:
4110 d_count_templates_scopes (num_templates
, num_scopes
,
4114 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
4115 d_count_templates_scopes (num_templates
, num_scopes
,
4116 dc
->u
.s_extended_operator
.name
);
4119 case DEMANGLE_COMPONENT_FIXED_TYPE
:
4120 d_count_templates_scopes (num_templates
, num_scopes
,
4121 dc
->u
.s_fixed
.length
);
4124 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
:
4125 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS
:
4126 d_count_templates_scopes (num_templates
, num_scopes
,
4130 case DEMANGLE_COMPONENT_LAMBDA
:
4131 case DEMANGLE_COMPONENT_DEFAULT_ARG
:
4132 d_count_templates_scopes (num_templates
, num_scopes
,
4133 dc
->u
.s_unary_num
.sub
);
4138 /* Initialize a print information structure. */
4141 d_print_init (struct d_print_info
*dpi
, demangle_callbackref callback
,
4142 void *opaque
, const struct demangle_component
*dc
)
4145 dpi
->last_char
= '\0';
4146 dpi
->templates
= NULL
;
4147 dpi
->modifiers
= NULL
;
4148 dpi
->pack_index
= 0;
4149 dpi
->flush_count
= 0;
4151 dpi
->callback
= callback
;
4152 dpi
->opaque
= opaque
;
4154 dpi
->demangle_failure
= 0;
4155 dpi
->is_lambda_arg
= 0;
4157 dpi
->component_stack
= NULL
;
4159 dpi
->saved_scopes
= NULL
;
4160 dpi
->next_saved_scope
= 0;
4161 dpi
->num_saved_scopes
= 0;
4163 dpi
->copy_templates
= NULL
;
4164 dpi
->next_copy_template
= 0;
4165 dpi
->num_copy_templates
= 0;
4167 d_count_templates_scopes (&dpi
->num_copy_templates
,
4168 &dpi
->num_saved_scopes
, dc
);
4169 dpi
->num_copy_templates
*= dpi
->num_saved_scopes
;
4171 dpi
->current_template
= NULL
;
4174 /* Indicate that an error occurred during printing, and test for error. */
4177 d_print_error (struct d_print_info
*dpi
)
4179 dpi
->demangle_failure
= 1;
4183 d_print_saw_error (struct d_print_info
*dpi
)
4185 return dpi
->demangle_failure
!= 0;
4188 /* Flush buffered characters to the callback. */
4191 d_print_flush (struct d_print_info
*dpi
)
4193 dpi
->buf
[dpi
->len
] = '\0';
4194 dpi
->callback (dpi
->buf
, dpi
->len
, dpi
->opaque
);
4199 /* Append characters and buffers for printing. */
4202 d_append_char (struct d_print_info
*dpi
, char c
)
4204 if (dpi
->len
== sizeof (dpi
->buf
) - 1)
4205 d_print_flush (dpi
);
4207 dpi
->buf
[dpi
->len
++] = c
;
4212 d_append_buffer (struct d_print_info
*dpi
, const char *s
, size_t l
)
4216 for (i
= 0; i
< l
; i
++)
4217 d_append_char (dpi
, s
[i
]);
4221 d_append_string (struct d_print_info
*dpi
, const char *s
)
4223 d_append_buffer (dpi
, s
, strlen (s
));
4227 d_append_num (struct d_print_info
*dpi
, int l
)
4230 sprintf (buf
,"%d", l
);
4231 d_append_string (dpi
, buf
);
4235 d_last_char (struct d_print_info
*dpi
)
4237 return dpi
->last_char
;
4240 /* Turn components into a human readable string. OPTIONS is the
4241 options bits passed to the demangler. DC is the tree to print.
4242 CALLBACK is a function to call to flush demangled string segments
4243 as they fill the intermediate buffer, and OPAQUE is a generalized
4244 callback argument. On success, this returns 1. On failure,
4245 it returns 0, indicating a bad parse. It does not use heap
4246 memory to build an output string, so cannot encounter memory
4247 allocation failure. */
4249 CP_STATIC_IF_GLIBCPP_V3
4251 cplus_demangle_print_callback (int options
,
4252 const struct demangle_component
*dc
,
4253 demangle_callbackref callback
, void *opaque
)
4255 struct d_print_info dpi
;
4257 d_print_init (&dpi
, callback
, opaque
, dc
);
4260 #ifdef CP_DYNAMIC_ARRAYS
4261 /* Avoid zero-length VLAs, which are prohibited by the C99 standard
4262 and flagged as errors by Address Sanitizer. */
4263 __extension__
struct d_saved_scope scopes
[(dpi
.num_saved_scopes
> 0)
4264 ? dpi
.num_saved_scopes
: 1];
4265 __extension__
struct d_print_template temps
[(dpi
.num_copy_templates
> 0)
4266 ? dpi
.num_copy_templates
: 1];
4268 dpi
.saved_scopes
= scopes
;
4269 dpi
.copy_templates
= temps
;
4271 dpi
.saved_scopes
= alloca (dpi
.num_saved_scopes
4272 * sizeof (*dpi
.saved_scopes
));
4273 dpi
.copy_templates
= alloca (dpi
.num_copy_templates
4274 * sizeof (*dpi
.copy_templates
));
4277 d_print_comp (&dpi
, options
, dc
);
4280 d_print_flush (&dpi
);
4282 return ! d_print_saw_error (&dpi
);
4285 /* Turn components into a human readable string. OPTIONS is the
4286 options bits passed to the demangler. DC is the tree to print.
4287 ESTIMATE is a guess at the length of the result. This returns a
4288 string allocated by malloc, or NULL on error. On success, this
4289 sets *PALC to the size of the allocated buffer. On failure, this
4290 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
4293 CP_STATIC_IF_GLIBCPP_V3
4295 cplus_demangle_print (int options
, const struct demangle_component
*dc
,
4296 int estimate
, size_t *palc
)
4298 struct d_growable_string dgs
;
4300 d_growable_string_init (&dgs
, estimate
);
4302 if (! cplus_demangle_print_callback (options
, dc
,
4303 d_growable_string_callback_adapter
,
4311 *palc
= dgs
.allocation_failure
? 1 : dgs
.alc
;
4315 /* Returns the I'th element of the template arglist ARGS, or NULL on
4316 failure. If I is negative, return the entire arglist. */
4318 static struct demangle_component
*
4319 d_index_template_argument (struct demangle_component
*args
, int i
)
4321 struct demangle_component
*a
;
4324 /* Print the whole argument pack. */
4331 if (a
->type
!= DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
4337 if (i
!= 0 || a
== NULL
)
4343 /* Returns the template argument from the current context indicated by DC,
4344 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
4346 static struct demangle_component
*
4347 d_lookup_template_argument (struct d_print_info
*dpi
,
4348 const struct demangle_component
*dc
)
4350 if (dpi
->templates
== NULL
)
4352 d_print_error (dpi
);
4356 return d_index_template_argument
4357 (d_right (dpi
->templates
->template_decl
),
4358 dc
->u
.s_number
.number
);
4361 /* Returns a template argument pack used in DC (any will do), or NULL. */
4363 static struct demangle_component
*
4364 d_find_pack (struct d_print_info
*dpi
,
4365 const struct demangle_component
*dc
)
4367 struct demangle_component
*a
;
4373 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
4374 a
= d_lookup_template_argument (dpi
, dc
);
4375 if (a
&& a
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
4379 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
4382 case DEMANGLE_COMPONENT_LAMBDA
:
4383 case DEMANGLE_COMPONENT_NAME
:
4384 case DEMANGLE_COMPONENT_TAGGED_NAME
:
4385 case DEMANGLE_COMPONENT_OPERATOR
:
4386 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
4387 case DEMANGLE_COMPONENT_SUB_STD
:
4388 case DEMANGLE_COMPONENT_CHARACTER
:
4389 case DEMANGLE_COMPONENT_FUNCTION_PARAM
:
4390 case DEMANGLE_COMPONENT_UNNAMED_TYPE
:
4391 case DEMANGLE_COMPONENT_FIXED_TYPE
:
4392 case DEMANGLE_COMPONENT_DEFAULT_ARG
:
4393 case DEMANGLE_COMPONENT_NUMBER
:
4396 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
4397 return d_find_pack (dpi
, dc
->u
.s_extended_operator
.name
);
4398 case DEMANGLE_COMPONENT_CTOR
:
4399 return d_find_pack (dpi
, dc
->u
.s_ctor
.name
);
4400 case DEMANGLE_COMPONENT_DTOR
:
4401 return d_find_pack (dpi
, dc
->u
.s_dtor
.name
);
4404 a
= d_find_pack (dpi
, d_left (dc
));
4407 return d_find_pack (dpi
, d_right (dc
));
4411 /* Returns the length of the template argument pack DC. */
4414 d_pack_length (const struct demangle_component
*dc
)
4417 while (dc
&& dc
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
4418 && d_left (dc
) != NULL
)
4426 /* Returns the number of template args in DC, expanding any pack expansions
4430 d_args_length (struct d_print_info
*dpi
, const struct demangle_component
*dc
)
4433 for (; dc
&& dc
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
;
4436 struct demangle_component
*elt
= d_left (dc
);
4439 if (elt
->type
== DEMANGLE_COMPONENT_PACK_EXPANSION
)
4441 struct demangle_component
*a
= d_find_pack (dpi
, d_left (elt
));
4442 count
+= d_pack_length (a
);
4450 /* DC is a component of a mangled expression. Print it, wrapped in parens
4454 d_print_subexpr (struct d_print_info
*dpi
, int options
,
4455 const struct demangle_component
*dc
)
4458 if (dc
->type
== DEMANGLE_COMPONENT_NAME
4459 || dc
->type
== DEMANGLE_COMPONENT_QUAL_NAME
4460 || dc
->type
== DEMANGLE_COMPONENT_INITIALIZER_LIST
4461 || dc
->type
== DEMANGLE_COMPONENT_FUNCTION_PARAM
)
4464 d_append_char (dpi
, '(');
4465 d_print_comp (dpi
, options
, dc
);
4467 d_append_char (dpi
, ')');
4470 /* Save the current scope. */
4473 d_save_scope (struct d_print_info
*dpi
,
4474 const struct demangle_component
*container
)
4476 struct d_saved_scope
*scope
;
4477 struct d_print_template
*src
, **link
;
4479 if (dpi
->next_saved_scope
>= dpi
->num_saved_scopes
)
4481 d_print_error (dpi
);
4484 scope
= &dpi
->saved_scopes
[dpi
->next_saved_scope
];
4485 dpi
->next_saved_scope
++;
4487 scope
->container
= container
;
4488 link
= &scope
->templates
;
4490 for (src
= dpi
->templates
; src
!= NULL
; src
= src
->next
)
4492 struct d_print_template
*dst
;
4494 if (dpi
->next_copy_template
>= dpi
->num_copy_templates
)
4496 d_print_error (dpi
);
4499 dst
= &dpi
->copy_templates
[dpi
->next_copy_template
];
4500 dpi
->next_copy_template
++;
4502 dst
->template_decl
= src
->template_decl
;
4510 /* Attempt to locate a previously saved scope. Returns NULL if no
4511 corresponding saved scope was found. */
4513 static struct d_saved_scope
*
4514 d_get_saved_scope (struct d_print_info
*dpi
,
4515 const struct demangle_component
*container
)
4519 for (i
= 0; i
< dpi
->next_saved_scope
; i
++)
4520 if (dpi
->saved_scopes
[i
].container
== container
)
4521 return &dpi
->saved_scopes
[i
];
4526 /* If DC is a C++17 fold-expression, print it and return true; otherwise
4530 d_maybe_print_fold_expression (struct d_print_info
*dpi
, int options
,
4531 const struct demangle_component
*dc
)
4533 const struct demangle_component
*ops
, *operator_
, *op1
, *op2
;
4536 const char *fold_code
= d_left (dc
)->u
.s_operator
.op
->code
;
4537 if (fold_code
[0] != 'f')
4541 operator_
= d_left (ops
);
4542 op1
= d_right (ops
);
4544 if (op1
->type
== DEMANGLE_COMPONENT_TRINARY_ARG2
)
4546 op2
= d_right (op1
);
4550 /* Print the whole pack. */
4551 save_idx
= dpi
->pack_index
;
4552 dpi
->pack_index
= -1;
4554 switch (fold_code
[1])
4556 /* Unary left fold, (... + X). */
4558 d_append_string (dpi
, "(...");
4559 d_print_expr_op (dpi
, options
, operator_
);
4560 d_print_subexpr (dpi
, options
, op1
);
4561 d_append_char (dpi
, ')');
4564 /* Unary right fold, (X + ...). */
4566 d_append_char (dpi
, '(');
4567 d_print_subexpr (dpi
, options
, op1
);
4568 d_print_expr_op (dpi
, options
, operator_
);
4569 d_append_string (dpi
, "...)");
4572 /* Binary left fold, (42 + ... + X). */
4574 /* Binary right fold, (X + ... + 42). */
4576 d_append_char (dpi
, '(');
4577 d_print_subexpr (dpi
, options
, op1
);
4578 d_print_expr_op (dpi
, options
, operator_
);
4579 d_append_string (dpi
, "...");
4580 d_print_expr_op (dpi
, options
, operator_
);
4581 d_print_subexpr (dpi
, options
, op2
);
4582 d_append_char (dpi
, ')');
4586 dpi
->pack_index
= save_idx
;
4590 /* Subroutine to handle components. */
4593 d_print_comp_inner (struct d_print_info
*dpi
, int options
,
4594 const struct demangle_component
*dc
)
4596 /* Magic variable to let reference smashing skip over the next modifier
4597 without needing to modify *dc. */
4598 const struct demangle_component
*mod_inner
= NULL
;
4600 /* Variable used to store the current templates while a previously
4601 captured scope is used. */
4602 struct d_print_template
*saved_templates
;
4604 /* Nonzero if templates have been stored in the above variable. */
4605 int need_template_restore
= 0;
4609 d_print_error (dpi
);
4612 if (d_print_saw_error (dpi
))
4617 case DEMANGLE_COMPONENT_NAME
:
4618 if ((options
& DMGL_JAVA
) == 0)
4619 d_append_buffer (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
4621 d_print_java_identifier (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
4624 case DEMANGLE_COMPONENT_TAGGED_NAME
:
4625 d_print_comp (dpi
, options
, d_left (dc
));
4626 d_append_string (dpi
, "[abi:");
4627 d_print_comp (dpi
, options
, d_right (dc
));
4628 d_append_char (dpi
, ']');
4631 case DEMANGLE_COMPONENT_QUAL_NAME
:
4632 case DEMANGLE_COMPONENT_LOCAL_NAME
:
4633 d_print_comp (dpi
, options
, d_left (dc
));
4634 if ((options
& DMGL_JAVA
) == 0)
4635 d_append_string (dpi
, "::");
4637 d_append_char (dpi
, '.');
4639 struct demangle_component
*local_name
= d_right (dc
);
4640 if (local_name
->type
== DEMANGLE_COMPONENT_DEFAULT_ARG
)
4642 d_append_string (dpi
, "{default arg#");
4643 d_append_num (dpi
, local_name
->u
.s_unary_num
.num
+ 1);
4644 d_append_string (dpi
, "}::");
4645 local_name
= local_name
->u
.s_unary_num
.sub
;
4647 d_print_comp (dpi
, options
, local_name
);
4651 case DEMANGLE_COMPONENT_TYPED_NAME
:
4653 struct d_print_mod
*hold_modifiers
;
4654 struct demangle_component
*typed_name
;
4655 struct d_print_mod adpm
[4];
4657 struct d_print_template dpt
;
4659 /* Pass the name down to the type so that it can be printed in
4660 the right place for the type. We also have to pass down
4661 any CV-qualifiers, which apply to the this parameter. */
4662 hold_modifiers
= dpi
->modifiers
;
4665 typed_name
= d_left (dc
);
4666 while (typed_name
!= NULL
)
4668 if (i
>= sizeof adpm
/ sizeof adpm
[0])
4670 d_print_error (dpi
);
4674 adpm
[i
].next
= dpi
->modifiers
;
4675 dpi
->modifiers
= &adpm
[i
];
4676 adpm
[i
].mod
= typed_name
;
4677 adpm
[i
].printed
= 0;
4678 adpm
[i
].templates
= dpi
->templates
;
4681 if (!is_fnqual_component_type (typed_name
->type
))
4684 typed_name
= d_left (typed_name
);
4687 if (typed_name
== NULL
)
4689 d_print_error (dpi
);
4693 /* If typed_name is a template, then it applies to the
4694 function type as well. */
4695 if (typed_name
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
4697 dpt
.next
= dpi
->templates
;
4698 dpi
->templates
= &dpt
;
4699 dpt
.template_decl
= typed_name
;
4702 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
4703 there may be CV-qualifiers on its right argument which
4704 really apply here; this happens when parsing a class which
4705 is local to a function. */
4706 if (typed_name
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
4708 struct demangle_component
*local_name
;
4710 local_name
= d_right (typed_name
);
4711 if (local_name
->type
== DEMANGLE_COMPONENT_DEFAULT_ARG
)
4712 local_name
= local_name
->u
.s_unary_num
.sub
;
4713 if (local_name
== NULL
)
4715 d_print_error (dpi
);
4718 while (is_fnqual_component_type (local_name
->type
))
4720 if (i
>= sizeof adpm
/ sizeof adpm
[0])
4722 d_print_error (dpi
);
4726 adpm
[i
] = adpm
[i
- 1];
4727 adpm
[i
].next
= &adpm
[i
- 1];
4728 dpi
->modifiers
= &adpm
[i
];
4730 adpm
[i
- 1].mod
= local_name
;
4731 adpm
[i
- 1].printed
= 0;
4732 adpm
[i
- 1].templates
= dpi
->templates
;
4735 local_name
= d_left (local_name
);
4739 d_print_comp (dpi
, options
, d_right (dc
));
4741 if (typed_name
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
4742 dpi
->templates
= dpt
.next
;
4744 /* If the modifiers didn't get printed by the type, print them
4749 if (! adpm
[i
].printed
)
4751 d_append_char (dpi
, ' ');
4752 d_print_mod (dpi
, options
, adpm
[i
].mod
);
4756 dpi
->modifiers
= hold_modifiers
;
4761 case DEMANGLE_COMPONENT_TEMPLATE
:
4763 struct d_print_mod
*hold_dpm
;
4764 struct demangle_component
*dcl
;
4765 const struct demangle_component
*hold_current
;
4767 /* This template may need to be referenced by a cast operator
4768 contained in its subtree. */
4769 hold_current
= dpi
->current_template
;
4770 dpi
->current_template
= dc
;
4772 /* Don't push modifiers into a template definition. Doing so
4773 could give the wrong definition for a template argument.
4774 Instead, treat the template essentially as a name. */
4776 hold_dpm
= dpi
->modifiers
;
4777 dpi
->modifiers
= NULL
;
4781 if ((options
& DMGL_JAVA
) != 0
4782 && dcl
->type
== DEMANGLE_COMPONENT_NAME
4783 && dcl
->u
.s_name
.len
== 6
4784 && strncmp (dcl
->u
.s_name
.s
, "JArray", 6) == 0)
4786 /* Special-case Java arrays, so that JArray<TYPE> appears
4787 instead as TYPE[]. */
4789 d_print_comp (dpi
, options
, d_right (dc
));
4790 d_append_string (dpi
, "[]");
4794 d_print_comp (dpi
, options
, dcl
);
4795 if (d_last_char (dpi
) == '<')
4796 d_append_char (dpi
, ' ');
4797 d_append_char (dpi
, '<');
4798 d_print_comp (dpi
, options
, d_right (dc
));
4799 /* Avoid generating two consecutive '>' characters, to avoid
4800 the C++ syntactic ambiguity. */
4801 if (d_last_char (dpi
) == '>')
4802 d_append_char (dpi
, ' ');
4803 d_append_char (dpi
, '>');
4806 dpi
->modifiers
= hold_dpm
;
4807 dpi
->current_template
= hold_current
;
4812 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
4813 if (dpi
->is_lambda_arg
)
4815 /* Show the template parm index, as that's how g++ displays
4816 these, and future proofs us against potential
4817 '[]<typename T> (T *a, T *b) {...}'. */
4818 d_append_buffer (dpi
, "auto:", 5);
4819 d_append_num (dpi
, dc
->u
.s_number
.number
+ 1);
4823 struct d_print_template
*hold_dpt
;
4824 struct demangle_component
*a
= d_lookup_template_argument (dpi
, dc
);
4826 if (a
&& a
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
4827 a
= d_index_template_argument (a
, dpi
->pack_index
);
4831 d_print_error (dpi
);
4835 /* While processing this parameter, we need to pop the list
4836 of templates. This is because the template parameter may
4837 itself be a reference to a parameter of an outer
4840 hold_dpt
= dpi
->templates
;
4841 dpi
->templates
= hold_dpt
->next
;
4843 d_print_comp (dpi
, options
, a
);
4845 dpi
->templates
= hold_dpt
;
4849 case DEMANGLE_COMPONENT_CTOR
:
4850 d_print_comp (dpi
, options
, dc
->u
.s_ctor
.name
);
4853 case DEMANGLE_COMPONENT_DTOR
:
4854 d_append_char (dpi
, '~');
4855 d_print_comp (dpi
, options
, dc
->u
.s_dtor
.name
);
4858 case DEMANGLE_COMPONENT_VTABLE
:
4859 d_append_string (dpi
, "vtable for ");
4860 d_print_comp (dpi
, options
, d_left (dc
));
4863 case DEMANGLE_COMPONENT_VTT
:
4864 d_append_string (dpi
, "VTT for ");
4865 d_print_comp (dpi
, options
, d_left (dc
));
4868 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
4869 d_append_string (dpi
, "construction vtable for ");
4870 d_print_comp (dpi
, options
, d_left (dc
));
4871 d_append_string (dpi
, "-in-");
4872 d_print_comp (dpi
, options
, d_right (dc
));
4875 case DEMANGLE_COMPONENT_TYPEINFO
:
4876 d_append_string (dpi
, "typeinfo for ");
4877 d_print_comp (dpi
, options
, d_left (dc
));
4880 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
4881 d_append_string (dpi
, "typeinfo name for ");
4882 d_print_comp (dpi
, options
, d_left (dc
));
4885 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
4886 d_append_string (dpi
, "typeinfo fn for ");
4887 d_print_comp (dpi
, options
, d_left (dc
));
4890 case DEMANGLE_COMPONENT_THUNK
:
4891 d_append_string (dpi
, "non-virtual thunk to ");
4892 d_print_comp (dpi
, options
, d_left (dc
));
4895 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
4896 d_append_string (dpi
, "virtual thunk to ");
4897 d_print_comp (dpi
, options
, d_left (dc
));
4900 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
4901 d_append_string (dpi
, "covariant return thunk to ");
4902 d_print_comp (dpi
, options
, d_left (dc
));
4905 case DEMANGLE_COMPONENT_JAVA_CLASS
:
4906 d_append_string (dpi
, "java Class for ");
4907 d_print_comp (dpi
, options
, d_left (dc
));
4910 case DEMANGLE_COMPONENT_GUARD
:
4911 d_append_string (dpi
, "guard variable for ");
4912 d_print_comp (dpi
, options
, d_left (dc
));
4915 case DEMANGLE_COMPONENT_TLS_INIT
:
4916 d_append_string (dpi
, "TLS init function for ");
4917 d_print_comp (dpi
, options
, d_left (dc
));
4920 case DEMANGLE_COMPONENT_TLS_WRAPPER
:
4921 d_append_string (dpi
, "TLS wrapper function for ");
4922 d_print_comp (dpi
, options
, d_left (dc
));
4925 case DEMANGLE_COMPONENT_REFTEMP
:
4926 d_append_string (dpi
, "reference temporary #");
4927 d_print_comp (dpi
, options
, d_right (dc
));
4928 d_append_string (dpi
, " for ");
4929 d_print_comp (dpi
, options
, d_left (dc
));
4932 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
4933 d_append_string (dpi
, "hidden alias for ");
4934 d_print_comp (dpi
, options
, d_left (dc
));
4937 case DEMANGLE_COMPONENT_TRANSACTION_CLONE
:
4938 d_append_string (dpi
, "transaction clone for ");
4939 d_print_comp (dpi
, options
, d_left (dc
));
4942 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
:
4943 d_append_string (dpi
, "non-transaction clone for ");
4944 d_print_comp (dpi
, options
, d_left (dc
));
4947 case DEMANGLE_COMPONENT_SUB_STD
:
4948 d_append_buffer (dpi
, dc
->u
.s_string
.string
, dc
->u
.s_string
.len
);
4951 case DEMANGLE_COMPONENT_RESTRICT
:
4952 case DEMANGLE_COMPONENT_VOLATILE
:
4953 case DEMANGLE_COMPONENT_CONST
:
4955 struct d_print_mod
*pdpm
;
4957 /* When printing arrays, it's possible to have cases where the
4958 same CV-qualifier gets pushed on the stack multiple times.
4959 We only need to print it once. */
4961 for (pdpm
= dpi
->modifiers
; pdpm
!= NULL
; pdpm
= pdpm
->next
)
4963 if (! pdpm
->printed
)
4965 if (pdpm
->mod
->type
!= DEMANGLE_COMPONENT_RESTRICT
4966 && pdpm
->mod
->type
!= DEMANGLE_COMPONENT_VOLATILE
4967 && pdpm
->mod
->type
!= DEMANGLE_COMPONENT_CONST
)
4969 if (pdpm
->mod
->type
== dc
->type
)
4971 d_print_comp (dpi
, options
, d_left (dc
));
4979 case DEMANGLE_COMPONENT_REFERENCE
:
4980 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
4982 /* Handle reference smashing: & + && = &. */
4983 const struct demangle_component
*sub
= d_left (dc
);
4984 if (!dpi
->is_lambda_arg
4985 && sub
->type
== DEMANGLE_COMPONENT_TEMPLATE_PARAM
)
4987 struct d_saved_scope
*scope
= d_get_saved_scope (dpi
, sub
);
4988 struct demangle_component
*a
;
4992 /* This is the first time SUB has been traversed.
4993 We need to capture the current templates so
4994 they can be restored if SUB is reentered as a
4996 d_save_scope (dpi
, sub
);
4997 if (d_print_saw_error (dpi
))
5002 const struct d_component_stack
*dcse
;
5003 int found_self_or_parent
= 0;
5005 /* This traversal is reentering SUB as a substition.
5006 If we are not beneath SUB or DC in the tree then we
5007 need to restore SUB's template stack temporarily. */
5008 for (dcse
= dpi
->component_stack
; dcse
!= NULL
;
5009 dcse
= dcse
->parent
)
5013 && dcse
!= dpi
->component_stack
))
5015 found_self_or_parent
= 1;
5020 if (!found_self_or_parent
)
5022 saved_templates
= dpi
->templates
;
5023 dpi
->templates
= scope
->templates
;
5024 need_template_restore
= 1;
5028 a
= d_lookup_template_argument (dpi
, sub
);
5029 if (a
&& a
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
5030 a
= d_index_template_argument (a
, dpi
->pack_index
);
5034 if (need_template_restore
)
5035 dpi
->templates
= saved_templates
;
5037 d_print_error (dpi
);
5044 if (sub
->type
== DEMANGLE_COMPONENT_REFERENCE
5045 || sub
->type
== dc
->type
)
5047 else if (sub
->type
== DEMANGLE_COMPONENT_RVALUE_REFERENCE
)
5048 mod_inner
= d_left (sub
);
5052 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
5053 case DEMANGLE_COMPONENT_POINTER
:
5054 case DEMANGLE_COMPONENT_COMPLEX
:
5055 case DEMANGLE_COMPONENT_IMAGINARY
:
5056 FNQUAL_COMPONENT_CASE
:
5059 /* We keep a list of modifiers on the stack. */
5060 struct d_print_mod dpm
;
5062 dpm
.next
= dpi
->modifiers
;
5063 dpi
->modifiers
= &dpm
;
5066 dpm
.templates
= dpi
->templates
;
5069 mod_inner
= d_left (dc
);
5071 d_print_comp (dpi
, options
, mod_inner
);
5073 /* If the modifier didn't get printed by the type, print it
5076 d_print_mod (dpi
, options
, dc
);
5078 dpi
->modifiers
= dpm
.next
;
5080 if (need_template_restore
)
5081 dpi
->templates
= saved_templates
;
5086 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
5087 if ((options
& DMGL_JAVA
) == 0)
5088 d_append_buffer (dpi
, dc
->u
.s_builtin
.type
->name
,
5089 dc
->u
.s_builtin
.type
->len
);
5091 d_append_buffer (dpi
, dc
->u
.s_builtin
.type
->java_name
,
5092 dc
->u
.s_builtin
.type
->java_len
);
5095 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
5096 d_print_comp (dpi
, options
, d_left (dc
));
5099 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
5101 if ((options
& DMGL_RET_POSTFIX
) != 0)
5102 d_print_function_type (dpi
,
5103 options
& ~(DMGL_RET_POSTFIX
| DMGL_RET_DROP
),
5104 dc
, dpi
->modifiers
);
5106 /* Print return type if present */
5107 if (d_left (dc
) != NULL
&& (options
& DMGL_RET_POSTFIX
) != 0)
5108 d_print_comp (dpi
, options
& ~(DMGL_RET_POSTFIX
| DMGL_RET_DROP
),
5110 else if (d_left (dc
) != NULL
&& (options
& DMGL_RET_DROP
) == 0)
5112 struct d_print_mod dpm
;
5114 /* We must pass this type down as a modifier in order to
5115 print it in the right location. */
5116 dpm
.next
= dpi
->modifiers
;
5117 dpi
->modifiers
= &dpm
;
5120 dpm
.templates
= dpi
->templates
;
5122 d_print_comp (dpi
, options
& ~(DMGL_RET_POSTFIX
| DMGL_RET_DROP
),
5125 dpi
->modifiers
= dpm
.next
;
5130 /* In standard prefix notation, there is a space between the
5131 return type and the function signature. */
5132 if ((options
& DMGL_RET_POSTFIX
) == 0)
5133 d_append_char (dpi
, ' ');
5136 if ((options
& DMGL_RET_POSTFIX
) == 0)
5137 d_print_function_type (dpi
,
5138 options
& ~(DMGL_RET_POSTFIX
| DMGL_RET_DROP
),
5139 dc
, dpi
->modifiers
);
5144 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
5146 struct d_print_mod
*hold_modifiers
;
5147 struct d_print_mod adpm
[4];
5149 struct d_print_mod
*pdpm
;
5151 /* We must pass this type down as a modifier in order to print
5152 multi-dimensional arrays correctly. If the array itself is
5153 CV-qualified, we act as though the element type were
5154 CV-qualified. We do this by copying the modifiers down
5155 rather than fiddling pointers, so that we don't wind up
5156 with a d_print_mod higher on the stack pointing into our
5157 stack frame after we return. */
5159 hold_modifiers
= dpi
->modifiers
;
5161 adpm
[0].next
= hold_modifiers
;
5162 dpi
->modifiers
= &adpm
[0];
5164 adpm
[0].printed
= 0;
5165 adpm
[0].templates
= dpi
->templates
;
5168 pdpm
= hold_modifiers
;
5170 && (pdpm
->mod
->type
== DEMANGLE_COMPONENT_RESTRICT
5171 || pdpm
->mod
->type
== DEMANGLE_COMPONENT_VOLATILE
5172 || pdpm
->mod
->type
== DEMANGLE_COMPONENT_CONST
))
5174 if (! pdpm
->printed
)
5176 if (i
>= sizeof adpm
/ sizeof adpm
[0])
5178 d_print_error (dpi
);
5183 adpm
[i
].next
= dpi
->modifiers
;
5184 dpi
->modifiers
= &adpm
[i
];
5192 d_print_comp (dpi
, options
, d_right (dc
));
5194 dpi
->modifiers
= hold_modifiers
;
5196 if (adpm
[0].printed
)
5202 d_print_mod (dpi
, options
, adpm
[i
].mod
);
5205 d_print_array_type (dpi
, options
, dc
, dpi
->modifiers
);
5210 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
5211 case DEMANGLE_COMPONENT_VECTOR_TYPE
:
5213 struct d_print_mod dpm
;
5215 dpm
.next
= dpi
->modifiers
;
5216 dpi
->modifiers
= &dpm
;
5219 dpm
.templates
= dpi
->templates
;
5221 d_print_comp (dpi
, options
, d_right (dc
));
5223 /* If the modifier didn't get printed by the type, print it
5226 d_print_mod (dpi
, options
, dc
);
5228 dpi
->modifiers
= dpm
.next
;
5233 case DEMANGLE_COMPONENT_FIXED_TYPE
:
5234 if (dc
->u
.s_fixed
.sat
)
5235 d_append_string (dpi
, "_Sat ");
5236 /* Don't print "int _Accum". */
5237 if (dc
->u
.s_fixed
.length
->u
.s_builtin
.type
5238 != &cplus_demangle_builtin_types
['i'-'a'])
5240 d_print_comp (dpi
, options
, dc
->u
.s_fixed
.length
);
5241 d_append_char (dpi
, ' ');
5243 if (dc
->u
.s_fixed
.accum
)
5244 d_append_string (dpi
, "_Accum");
5246 d_append_string (dpi
, "_Fract");
5249 case DEMANGLE_COMPONENT_ARGLIST
:
5250 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
5251 if (d_left (dc
) != NULL
)
5252 d_print_comp (dpi
, options
, d_left (dc
));
5253 if (d_right (dc
) != NULL
)
5256 unsigned long int flush_count
;
5257 /* Make sure ", " isn't flushed by d_append_string, otherwise
5258 dpi->len -= 2 wouldn't work. */
5259 if (dpi
->len
>= sizeof (dpi
->buf
) - 2)
5260 d_print_flush (dpi
);
5261 d_append_string (dpi
, ", ");
5263 flush_count
= dpi
->flush_count
;
5264 d_print_comp (dpi
, options
, d_right (dc
));
5265 /* If that didn't print anything (which can happen with empty
5266 template argument packs), remove the comma and space. */
5267 if (dpi
->flush_count
== flush_count
&& dpi
->len
== len
)
5272 case DEMANGLE_COMPONENT_INITIALIZER_LIST
:
5274 struct demangle_component
*type
= d_left (dc
);
5275 struct demangle_component
*list
= d_right (dc
);
5278 d_print_comp (dpi
, options
, type
);
5279 d_append_char (dpi
, '{');
5280 d_print_comp (dpi
, options
, list
);
5281 d_append_char (dpi
, '}');
5285 case DEMANGLE_COMPONENT_OPERATOR
:
5287 const struct demangle_operator_info
*op
= dc
->u
.s_operator
.op
;
5290 d_append_string (dpi
, "operator");
5291 /* Add a space before new/delete. */
5292 if (IS_LOWER (op
->name
[0]))
5293 d_append_char (dpi
, ' ');
5294 /* Omit a trailing space. */
5295 if (op
->name
[len
-1] == ' ')
5297 d_append_buffer (dpi
, op
->name
, len
);
5301 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
5302 d_append_string (dpi
, "operator ");
5303 d_print_comp (dpi
, options
, dc
->u
.s_extended_operator
.name
);
5306 case DEMANGLE_COMPONENT_CONVERSION
:
5307 d_append_string (dpi
, "operator ");
5308 d_print_conversion (dpi
, options
, dc
);
5311 case DEMANGLE_COMPONENT_NULLARY
:
5312 d_print_expr_op (dpi
, options
, d_left (dc
));
5315 case DEMANGLE_COMPONENT_UNARY
:
5317 struct demangle_component
*op
= d_left (dc
);
5318 struct demangle_component
*operand
= d_right (dc
);
5319 const char *code
= NULL
;
5321 if (op
->type
== DEMANGLE_COMPONENT_OPERATOR
)
5323 code
= op
->u
.s_operator
.op
->code
;
5324 if (!strcmp (code
, "ad"))
5326 /* Don't print the argument list for the address of a
5328 if (operand
->type
== DEMANGLE_COMPONENT_TYPED_NAME
5329 && d_left (operand
)->type
== DEMANGLE_COMPONENT_QUAL_NAME
5330 && d_right (operand
)->type
== DEMANGLE_COMPONENT_FUNCTION_TYPE
)
5331 operand
= d_left (operand
);
5333 if (operand
->type
== DEMANGLE_COMPONENT_BINARY_ARGS
)
5335 /* This indicates a suffix operator. */
5336 operand
= d_left (operand
);
5337 d_print_subexpr (dpi
, options
, operand
);
5338 d_print_expr_op (dpi
, options
, op
);
5343 /* For sizeof..., just print the pack length. */
5344 if (code
&& !strcmp (code
, "sZ"))
5346 struct demangle_component
*a
= d_find_pack (dpi
, operand
);
5347 int len
= d_pack_length (a
);
5348 d_append_num (dpi
, len
);
5351 else if (code
&& !strcmp (code
, "sP"))
5353 int len
= d_args_length (dpi
, operand
);
5354 d_append_num (dpi
, len
);
5358 if (op
->type
!= DEMANGLE_COMPONENT_CAST
)
5359 d_print_expr_op (dpi
, options
, op
);
5362 d_append_char (dpi
, '(');
5363 d_print_cast (dpi
, options
, op
);
5364 d_append_char (dpi
, ')');
5366 if (code
&& !strcmp (code
, "gs"))
5367 /* Avoid parens after '::'. */
5368 d_print_comp (dpi
, options
, operand
);
5369 else if (code
&& !strcmp (code
, "st"))
5370 /* Always print parens for sizeof (type). */
5372 d_append_char (dpi
, '(');
5373 d_print_comp (dpi
, options
, operand
);
5374 d_append_char (dpi
, ')');
5377 d_print_subexpr (dpi
, options
, operand
);
5381 case DEMANGLE_COMPONENT_BINARY
:
5382 if (d_right (dc
)->type
!= DEMANGLE_COMPONENT_BINARY_ARGS
)
5384 d_print_error (dpi
);
5388 if (op_is_new_cast (d_left (dc
)))
5390 d_print_expr_op (dpi
, options
, d_left (dc
));
5391 d_append_char (dpi
, '<');
5392 d_print_comp (dpi
, options
, d_left (d_right (dc
)));
5393 d_append_string (dpi
, ">(");
5394 d_print_comp (dpi
, options
, d_right (d_right (dc
)));
5395 d_append_char (dpi
, ')');
5399 if (d_maybe_print_fold_expression (dpi
, options
, dc
))
5402 /* We wrap an expression which uses the greater-than operator in
5403 an extra layer of parens so that it does not get confused
5404 with the '>' which ends the template parameters. */
5405 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_OPERATOR
5406 && d_left (dc
)->u
.s_operator
.op
->len
== 1
5407 && d_left (dc
)->u
.s_operator
.op
->name
[0] == '>')
5408 d_append_char (dpi
, '(');
5410 if (strcmp (d_left (dc
)->u
.s_operator
.op
->code
, "cl") == 0
5411 && d_left (d_right (dc
))->type
== DEMANGLE_COMPONENT_TYPED_NAME
)
5413 /* Function call used in an expression should not have printed types
5414 of the function arguments. Values of the function arguments still
5415 get printed below. */
5417 const struct demangle_component
*func
= d_left (d_right (dc
));
5419 if (d_right (func
)->type
!= DEMANGLE_COMPONENT_FUNCTION_TYPE
)
5420 d_print_error (dpi
);
5421 d_print_subexpr (dpi
, options
, d_left (func
));
5424 d_print_subexpr (dpi
, options
, d_left (d_right (dc
)));
5425 if (strcmp (d_left (dc
)->u
.s_operator
.op
->code
, "ix") == 0)
5427 d_append_char (dpi
, '[');
5428 d_print_comp (dpi
, options
, d_right (d_right (dc
)));
5429 d_append_char (dpi
, ']');
5433 if (strcmp (d_left (dc
)->u
.s_operator
.op
->code
, "cl") != 0)
5434 d_print_expr_op (dpi
, options
, d_left (dc
));
5435 d_print_subexpr (dpi
, options
, d_right (d_right (dc
)));
5438 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_OPERATOR
5439 && d_left (dc
)->u
.s_operator
.op
->len
== 1
5440 && d_left (dc
)->u
.s_operator
.op
->name
[0] == '>')
5441 d_append_char (dpi
, ')');
5445 case DEMANGLE_COMPONENT_BINARY_ARGS
:
5446 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
5447 d_print_error (dpi
);
5450 case DEMANGLE_COMPONENT_TRINARY
:
5451 if (d_right (dc
)->type
!= DEMANGLE_COMPONENT_TRINARY_ARG1
5452 || d_right (d_right (dc
))->type
!= DEMANGLE_COMPONENT_TRINARY_ARG2
)
5454 d_print_error (dpi
);
5457 if (d_maybe_print_fold_expression (dpi
, options
, dc
))
5460 struct demangle_component
*op
= d_left (dc
);
5461 struct demangle_component
*first
= d_left (d_right (dc
));
5462 struct demangle_component
*second
= d_left (d_right (d_right (dc
)));
5463 struct demangle_component
*third
= d_right (d_right (d_right (dc
)));
5465 if (!strcmp (op
->u
.s_operator
.op
->code
, "qu"))
5467 d_print_subexpr (dpi
, options
, first
);
5468 d_print_expr_op (dpi
, options
, op
);
5469 d_print_subexpr (dpi
, options
, second
);
5470 d_append_string (dpi
, " : ");
5471 d_print_subexpr (dpi
, options
, third
);
5475 d_append_string (dpi
, "new ");
5476 if (d_left (first
) != NULL
)
5478 d_print_subexpr (dpi
, options
, first
);
5479 d_append_char (dpi
, ' ');
5481 d_print_comp (dpi
, options
, second
);
5483 d_print_subexpr (dpi
, options
, third
);
5488 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
5489 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
5490 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
5491 d_print_error (dpi
);
5494 case DEMANGLE_COMPONENT_LITERAL
:
5495 case DEMANGLE_COMPONENT_LITERAL_NEG
:
5497 enum d_builtin_type_print tp
;
5499 /* For some builtin types, produce simpler output. */
5500 tp
= D_PRINT_DEFAULT
;
5501 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
)
5503 tp
= d_left (dc
)->u
.s_builtin
.type
->print
;
5507 case D_PRINT_UNSIGNED
:
5509 case D_PRINT_UNSIGNED_LONG
:
5510 case D_PRINT_LONG_LONG
:
5511 case D_PRINT_UNSIGNED_LONG_LONG
:
5512 if (d_right (dc
)->type
== DEMANGLE_COMPONENT_NAME
)
5514 if (dc
->type
== DEMANGLE_COMPONENT_LITERAL_NEG
)
5515 d_append_char (dpi
, '-');
5516 d_print_comp (dpi
, options
, d_right (dc
));
5521 case D_PRINT_UNSIGNED
:
5522 d_append_char (dpi
, 'u');
5525 d_append_char (dpi
, 'l');
5527 case D_PRINT_UNSIGNED_LONG
:
5528 d_append_string (dpi
, "ul");
5530 case D_PRINT_LONG_LONG
:
5531 d_append_string (dpi
, "ll");
5533 case D_PRINT_UNSIGNED_LONG_LONG
:
5534 d_append_string (dpi
, "ull");
5542 if (d_right (dc
)->type
== DEMANGLE_COMPONENT_NAME
5543 && d_right (dc
)->u
.s_name
.len
== 1
5544 && dc
->type
== DEMANGLE_COMPONENT_LITERAL
)
5546 switch (d_right (dc
)->u
.s_name
.s
[0])
5549 d_append_string (dpi
, "false");
5552 d_append_string (dpi
, "true");
5565 d_append_char (dpi
, '(');
5566 d_print_comp (dpi
, options
, d_left (dc
));
5567 d_append_char (dpi
, ')');
5568 if (dc
->type
== DEMANGLE_COMPONENT_LITERAL_NEG
)
5569 d_append_char (dpi
, '-');
5570 if (tp
== D_PRINT_FLOAT
)
5571 d_append_char (dpi
, '[');
5572 d_print_comp (dpi
, options
, d_right (dc
));
5573 if (tp
== D_PRINT_FLOAT
)
5574 d_append_char (dpi
, ']');
5578 case DEMANGLE_COMPONENT_NUMBER
:
5579 d_append_num (dpi
, dc
->u
.s_number
.number
);
5582 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
5583 d_append_string (dpi
, "java resource ");
5584 d_print_comp (dpi
, options
, d_left (dc
));
5587 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
5588 d_print_comp (dpi
, options
, d_left (dc
));
5589 d_print_comp (dpi
, options
, d_right (dc
));
5592 case DEMANGLE_COMPONENT_CHARACTER
:
5593 d_append_char (dpi
, dc
->u
.s_character
.character
);
5596 case DEMANGLE_COMPONENT_DECLTYPE
:
5597 d_append_string (dpi
, "decltype (");
5598 d_print_comp (dpi
, options
, d_left (dc
));
5599 d_append_char (dpi
, ')');
5602 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
5606 struct demangle_component
*a
= d_find_pack (dpi
, d_left (dc
));
5609 /* d_find_pack won't find anything if the only packs involved
5610 in this expansion are function parameter packs; in that
5611 case, just print the pattern and "...". */
5612 d_print_subexpr (dpi
, options
, d_left (dc
));
5613 d_append_string (dpi
, "...");
5617 len
= d_pack_length (a
);
5619 for (i
= 0; i
< len
; ++i
)
5621 dpi
->pack_index
= i
;
5622 d_print_comp (dpi
, options
, dc
);
5624 d_append_string (dpi
, ", ");
5629 case DEMANGLE_COMPONENT_FUNCTION_PARAM
:
5631 long num
= dc
->u
.s_number
.number
;
5633 d_append_string (dpi
, "this");
5636 d_append_string (dpi
, "{parm#");
5637 d_append_num (dpi
, num
);
5638 d_append_char (dpi
, '}');
5643 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
:
5644 d_append_string (dpi
, "global constructors keyed to ");
5645 d_print_comp (dpi
, options
, dc
->u
.s_binary
.left
);
5648 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS
:
5649 d_append_string (dpi
, "global destructors keyed to ");
5650 d_print_comp (dpi
, options
, dc
->u
.s_binary
.left
);
5653 case DEMANGLE_COMPONENT_LAMBDA
:
5654 d_append_string (dpi
, "{lambda(");
5655 /* Generic lambda auto parms are mangled as the template type
5657 dpi
->is_lambda_arg
++;
5658 d_print_comp (dpi
, options
, dc
->u
.s_unary_num
.sub
);
5659 dpi
->is_lambda_arg
--;
5660 d_append_string (dpi
, ")#");
5661 d_append_num (dpi
, dc
->u
.s_unary_num
.num
+ 1);
5662 d_append_char (dpi
, '}');
5665 case DEMANGLE_COMPONENT_UNNAMED_TYPE
:
5666 d_append_string (dpi
, "{unnamed type#");
5667 d_append_num (dpi
, dc
->u
.s_number
.number
+ 1);
5668 d_append_char (dpi
, '}');
5671 case DEMANGLE_COMPONENT_CLONE
:
5672 d_print_comp (dpi
, options
, d_left (dc
));
5673 d_append_string (dpi
, " [clone ");
5674 d_print_comp (dpi
, options
, d_right (dc
));
5675 d_append_char (dpi
, ']');
5679 d_print_error (dpi
);
5685 d_print_comp (struct d_print_info
*dpi
, int options
,
5686 const struct demangle_component
*dc
)
5688 struct d_component_stack self
;
5691 self
.parent
= dpi
->component_stack
;
5692 dpi
->component_stack
= &self
;
5694 d_print_comp_inner (dpi
, options
, dc
);
5696 dpi
->component_stack
= self
.parent
;
5699 /* Print a Java dentifier. For Java we try to handle encoded extended
5700 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
5701 so we don't it for C++. Characters are encoded as
5705 d_print_java_identifier (struct d_print_info
*dpi
, const char *name
, int len
)
5711 for (p
= name
; p
< end
; ++p
)
5722 for (q
= p
+ 3; q
< end
; ++q
)
5728 else if (*q
>= 'A' && *q
<= 'F')
5729 dig
= *q
- 'A' + 10;
5730 else if (*q
>= 'a' && *q
<= 'f')
5731 dig
= *q
- 'a' + 10;
5737 /* If the Unicode character is larger than 256, we don't try
5738 to deal with it here. FIXME. */
5739 if (q
< end
&& *q
== '_' && c
< 256)
5741 d_append_char (dpi
, c
);
5747 d_append_char (dpi
, *p
);
5751 /* Print a list of modifiers. SUFFIX is 1 if we are printing
5752 qualifiers on this after printing a function. */
5755 d_print_mod_list (struct d_print_info
*dpi
, int options
,
5756 struct d_print_mod
*mods
, int suffix
)
5758 struct d_print_template
*hold_dpt
;
5760 if (mods
== NULL
|| d_print_saw_error (dpi
))
5765 && (is_fnqual_component_type (mods
->mod
->type
))))
5767 d_print_mod_list (dpi
, options
, mods
->next
, suffix
);
5773 hold_dpt
= dpi
->templates
;
5774 dpi
->templates
= mods
->templates
;
5776 if (mods
->mod
->type
== DEMANGLE_COMPONENT_FUNCTION_TYPE
)
5778 d_print_function_type (dpi
, options
, mods
->mod
, mods
->next
);
5779 dpi
->templates
= hold_dpt
;
5782 else if (mods
->mod
->type
== DEMANGLE_COMPONENT_ARRAY_TYPE
)
5784 d_print_array_type (dpi
, options
, mods
->mod
, mods
->next
);
5785 dpi
->templates
= hold_dpt
;
5788 else if (mods
->mod
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
5790 struct d_print_mod
*hold_modifiers
;
5791 struct demangle_component
*dc
;
5793 /* When this is on the modifier stack, we have pulled any
5794 qualifiers off the right argument already. Otherwise, we
5795 print it as usual, but don't let the left argument see any
5798 hold_modifiers
= dpi
->modifiers
;
5799 dpi
->modifiers
= NULL
;
5800 d_print_comp (dpi
, options
, d_left (mods
->mod
));
5801 dpi
->modifiers
= hold_modifiers
;
5803 if ((options
& DMGL_JAVA
) == 0)
5804 d_append_string (dpi
, "::");
5806 d_append_char (dpi
, '.');
5808 dc
= d_right (mods
->mod
);
5810 if (dc
->type
== DEMANGLE_COMPONENT_DEFAULT_ARG
)
5812 d_append_string (dpi
, "{default arg#");
5813 d_append_num (dpi
, dc
->u
.s_unary_num
.num
+ 1);
5814 d_append_string (dpi
, "}::");
5815 dc
= dc
->u
.s_unary_num
.sub
;
5818 while (is_fnqual_component_type (dc
->type
))
5821 d_print_comp (dpi
, options
, dc
);
5823 dpi
->templates
= hold_dpt
;
5827 d_print_mod (dpi
, options
, mods
->mod
);
5829 dpi
->templates
= hold_dpt
;
5831 d_print_mod_list (dpi
, options
, mods
->next
, suffix
);
5834 /* Print a modifier. */
5837 d_print_mod (struct d_print_info
*dpi
, int options
,
5838 const struct demangle_component
*mod
)
5842 case DEMANGLE_COMPONENT_RESTRICT
:
5843 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
5844 d_append_string (dpi
, " restrict");
5846 case DEMANGLE_COMPONENT_VOLATILE
:
5847 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
5848 d_append_string (dpi
, " volatile");
5850 case DEMANGLE_COMPONENT_CONST
:
5851 case DEMANGLE_COMPONENT_CONST_THIS
:
5852 d_append_string (dpi
, " const");
5854 case DEMANGLE_COMPONENT_TRANSACTION_SAFE
:
5855 d_append_string (dpi
, " transaction_safe");
5857 case DEMANGLE_COMPONENT_NOEXCEPT
:
5858 d_append_string (dpi
, " noexcept");
5861 d_append_char (dpi
, '(');
5862 d_print_comp (dpi
, options
, d_right (mod
));
5863 d_append_char (dpi
, ')');
5866 case DEMANGLE_COMPONENT_THROW_SPEC
:
5867 d_append_string (dpi
, " throw");
5870 d_append_char (dpi
, '(');
5871 d_print_comp (dpi
, options
, d_right (mod
));
5872 d_append_char (dpi
, ')');
5875 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
5876 d_append_char (dpi
, ' ');
5877 d_print_comp (dpi
, options
, d_right (mod
));
5879 case DEMANGLE_COMPONENT_POINTER
:
5880 /* There is no pointer symbol in Java. */
5881 if ((options
& DMGL_JAVA
) == 0)
5882 d_append_char (dpi
, '*');
5884 case DEMANGLE_COMPONENT_REFERENCE_THIS
:
5885 /* For the ref-qualifier, put a space before the &. */
5886 d_append_char (dpi
, ' ');
5888 case DEMANGLE_COMPONENT_REFERENCE
:
5889 d_append_char (dpi
, '&');
5891 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
:
5892 d_append_char (dpi
, ' ');
5894 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
5895 d_append_string (dpi
, "&&");
5897 case DEMANGLE_COMPONENT_COMPLEX
:
5898 d_append_string (dpi
, "complex ");
5900 case DEMANGLE_COMPONENT_IMAGINARY
:
5901 d_append_string (dpi
, "imaginary ");
5903 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
5904 if (d_last_char (dpi
) != '(')
5905 d_append_char (dpi
, ' ');
5906 d_print_comp (dpi
, options
, d_left (mod
));
5907 d_append_string (dpi
, "::*");
5909 case DEMANGLE_COMPONENT_TYPED_NAME
:
5910 d_print_comp (dpi
, options
, d_left (mod
));
5912 case DEMANGLE_COMPONENT_VECTOR_TYPE
:
5913 d_append_string (dpi
, " __vector(");
5914 d_print_comp (dpi
, options
, d_left (mod
));
5915 d_append_char (dpi
, ')');
5919 /* Otherwise, we have something that won't go back on the
5920 modifier stack, so we can just print it. */
5921 d_print_comp (dpi
, options
, mod
);
5926 /* Print a function type, except for the return type. */
5929 d_print_function_type (struct d_print_info
*dpi
, int options
,
5930 const struct demangle_component
*dc
,
5931 struct d_print_mod
*mods
)
5935 struct d_print_mod
*p
;
5936 struct d_print_mod
*hold_modifiers
;
5940 for (p
= mods
; p
!= NULL
; p
= p
->next
)
5945 switch (p
->mod
->type
)
5947 case DEMANGLE_COMPONENT_POINTER
:
5948 case DEMANGLE_COMPONENT_REFERENCE
:
5949 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
5952 case DEMANGLE_COMPONENT_RESTRICT
:
5953 case DEMANGLE_COMPONENT_VOLATILE
:
5954 case DEMANGLE_COMPONENT_CONST
:
5955 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
5956 case DEMANGLE_COMPONENT_COMPLEX
:
5957 case DEMANGLE_COMPONENT_IMAGINARY
:
5958 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
5962 FNQUAL_COMPONENT_CASE
:
5975 if (d_last_char (dpi
) != '('
5976 && d_last_char (dpi
) != '*')
5979 if (need_space
&& d_last_char (dpi
) != ' ')
5980 d_append_char (dpi
, ' ');
5981 d_append_char (dpi
, '(');
5984 hold_modifiers
= dpi
->modifiers
;
5985 dpi
->modifiers
= NULL
;
5987 d_print_mod_list (dpi
, options
, mods
, 0);
5990 d_append_char (dpi
, ')');
5992 d_append_char (dpi
, '(');
5994 if (d_right (dc
) != NULL
)
5995 d_print_comp (dpi
, options
, d_right (dc
));
5997 d_append_char (dpi
, ')');
5999 d_print_mod_list (dpi
, options
, mods
, 1);
6001 dpi
->modifiers
= hold_modifiers
;
6004 /* Print an array type, except for the element type. */
6007 d_print_array_type (struct d_print_info
*dpi
, int options
,
6008 const struct demangle_component
*dc
,
6009 struct d_print_mod
*mods
)
6017 struct d_print_mod
*p
;
6020 for (p
= mods
; p
!= NULL
; p
= p
->next
)
6024 if (p
->mod
->type
== DEMANGLE_COMPONENT_ARRAY_TYPE
)
6039 d_append_string (dpi
, " (");
6041 d_print_mod_list (dpi
, options
, mods
, 0);
6044 d_append_char (dpi
, ')');
6048 d_append_char (dpi
, ' ');
6050 d_append_char (dpi
, '[');
6052 if (d_left (dc
) != NULL
)
6053 d_print_comp (dpi
, options
, d_left (dc
));
6055 d_append_char (dpi
, ']');
6058 /* Print an operator in an expression. */
6061 d_print_expr_op (struct d_print_info
*dpi
, int options
,
6062 const struct demangle_component
*dc
)
6064 if (dc
->type
== DEMANGLE_COMPONENT_OPERATOR
)
6065 d_append_buffer (dpi
, dc
->u
.s_operator
.op
->name
,
6066 dc
->u
.s_operator
.op
->len
);
6068 d_print_comp (dpi
, options
, dc
);
6074 d_print_cast (struct d_print_info
*dpi
, int options
,
6075 const struct demangle_component
*dc
)
6077 d_print_comp (dpi
, options
, d_left (dc
));
6080 /* Print a conversion operator. */
6083 d_print_conversion (struct d_print_info
*dpi
, int options
,
6084 const struct demangle_component
*dc
)
6086 struct d_print_template dpt
;
6088 /* For a conversion operator, we need the template parameters from
6089 the enclosing template in scope for processing the type. */
6090 if (dpi
->current_template
!= NULL
)
6092 dpt
.next
= dpi
->templates
;
6093 dpi
->templates
= &dpt
;
6094 dpt
.template_decl
= dpi
->current_template
;
6097 if (d_left (dc
)->type
!= DEMANGLE_COMPONENT_TEMPLATE
)
6099 d_print_comp (dpi
, options
, d_left (dc
));
6100 if (dpi
->current_template
!= NULL
)
6101 dpi
->templates
= dpt
.next
;
6105 d_print_comp (dpi
, options
, d_left (d_left (dc
)));
6107 /* For a templated cast operator, we need to remove the template
6108 parameters from scope after printing the operator name,
6109 so we need to handle the template printing here. */
6110 if (dpi
->current_template
!= NULL
)
6111 dpi
->templates
= dpt
.next
;
6113 if (d_last_char (dpi
) == '<')
6114 d_append_char (dpi
, ' ');
6115 d_append_char (dpi
, '<');
6116 d_print_comp (dpi
, options
, d_right (d_left (dc
)));
6117 /* Avoid generating two consecutive '>' characters, to avoid
6118 the C++ syntactic ambiguity. */
6119 if (d_last_char (dpi
) == '>')
6120 d_append_char (dpi
, ' ');
6121 d_append_char (dpi
, '>');
6125 /* Initialize the information structure we use to pass around
6128 CP_STATIC_IF_GLIBCPP_V3
6130 cplus_demangle_init_info (const char *mangled
, int options
, size_t len
,
6134 di
->send
= mangled
+ len
;
6135 di
->options
= options
;
6139 /* We can not need more components than twice the number of chars in
6140 the mangled string. Most components correspond directly to
6141 chars, but the ARGLIST types are exceptions. */
6142 di
->num_comps
= 2 * len
;
6145 /* Similarly, we can not need more substitutions than there are
6146 chars in the mangled string. */
6151 di
->last_name
= NULL
;
6154 di
->is_expression
= 0;
6155 di
->is_conversion
= 0;
6158 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
6159 mangled name, return strings in repeated callback giving the demangled
6160 name. OPTIONS is the usual libiberty demangler options. On success,
6161 this returns 1. On failure, returns 0. */
6164 d_demangle_callback (const char *mangled
, int options
,
6165 demangle_callbackref callback
, void *opaque
)
6176 struct demangle_component
*dc
;
6179 if (mangled
[0] == '_' && mangled
[1] == 'Z')
6181 else if (strncmp (mangled
, "_GLOBAL_", 8) == 0
6182 && (mangled
[8] == '.' || mangled
[8] == '_' || mangled
[8] == '$')
6183 && (mangled
[9] == 'D' || mangled
[9] == 'I')
6184 && mangled
[10] == '_')
6185 type
= mangled
[9] == 'I' ? DCT_GLOBAL_CTORS
: DCT_GLOBAL_DTORS
;
6188 if ((options
& DMGL_TYPES
) == 0)
6193 cplus_demangle_init_info (mangled
, options
, strlen (mangled
), &di
);
6196 #ifdef CP_DYNAMIC_ARRAYS
6197 __extension__
struct demangle_component comps
[di
.num_comps
];
6198 __extension__
struct demangle_component
*subs
[di
.num_subs
];
6203 di
.comps
= alloca (di
.num_comps
* sizeof (*di
.comps
));
6204 di
.subs
= alloca (di
.num_subs
* sizeof (*di
.subs
));
6210 dc
= cplus_demangle_type (&di
);
6213 dc
= cplus_demangle_mangled_name (&di
, 1);
6215 case DCT_GLOBAL_CTORS
:
6216 case DCT_GLOBAL_DTORS
:
6217 d_advance (&di
, 11);
6218 dc
= d_make_comp (&di
,
6219 (type
== DCT_GLOBAL_CTORS
6220 ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
6221 : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS
),
6222 d_make_demangle_mangled_name (&di
, d_str (&di
)),
6224 d_advance (&di
, strlen (d_str (&di
)));
6227 abort (); /* We have listed all the cases. */
6230 /* If DMGL_PARAMS is set, then if we didn't consume the entire
6231 mangled string, then we didn't successfully demangle it. If
6232 DMGL_PARAMS is not set, we didn't look at the trailing
6234 if (((options
& DMGL_PARAMS
) != 0) && d_peek_char (&di
) != '\0')
6237 #ifdef CP_DEMANGLE_DEBUG
6241 status
= (dc
!= NULL
)
6242 ? cplus_demangle_print_callback (options
, dc
, callback
, opaque
)
6249 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
6250 name, return a buffer allocated with malloc holding the demangled
6251 name. OPTIONS is the usual libiberty demangler options. On
6252 success, this sets *PALC to the allocated size of the returned
6253 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
6254 a memory allocation failure, and returns NULL. */
6257 d_demangle (const char *mangled
, int options
, size_t *palc
)
6259 struct d_growable_string dgs
;
6262 d_growable_string_init (&dgs
, 0);
6264 status
= d_demangle_callback (mangled
, options
,
6265 d_growable_string_callback_adapter
, &dgs
);
6273 *palc
= dgs
.allocation_failure
? 1 : dgs
.alc
;
6277 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
6279 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
6281 /* ia64 ABI-mandated entry point in the C++ runtime library for
6282 performing demangling. MANGLED_NAME is a NUL-terminated character
6283 string containing the name to be demangled.
6285 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
6286 *LENGTH bytes, into which the demangled name is stored. If
6287 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
6288 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
6289 is placed in a region of memory allocated with malloc.
6291 If LENGTH is non-NULL, the length of the buffer containing the
6292 demangled name, is placed in *LENGTH.
6294 The return value is a pointer to the start of the NUL-terminated
6295 demangled name, or NULL if the demangling fails. The caller is
6296 responsible for deallocating this memory using free.
6298 *STATUS is set to one of the following values:
6299 0: The demangling operation succeeded.
6300 -1: A memory allocation failure occurred.
6301 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
6302 -3: One of the arguments is invalid.
6304 The demangling is performed using the C++ ABI mangling rules, with
6308 __cxa_demangle (const char *mangled_name
, char *output_buffer
,
6309 size_t *length
, int *status
)
6314 if (mangled_name
== NULL
)
6321 if (output_buffer
!= NULL
&& length
== NULL
)
6328 demangled
= d_demangle (mangled_name
, DMGL_PARAMS
| DMGL_TYPES
, &alc
);
6330 if (demangled
== NULL
)
6342 if (output_buffer
== NULL
)
6349 if (strlen (demangled
) < *length
)
6351 strcpy (output_buffer
, demangled
);
6353 demangled
= output_buffer
;
6357 free (output_buffer
);
6368 extern int __gcclibcxx_demangle_callback (const char *,
6370 (const char *, size_t, void *),
6373 /* Alternative, allocationless entry point in the C++ runtime library
6374 for performing demangling. MANGLED_NAME is a NUL-terminated character
6375 string containing the name to be demangled.
6377 CALLBACK is a callback function, called with demangled string
6378 segments as demangling progresses; it is called at least once,
6379 but may be called more than once. OPAQUE is a generalized pointer
6380 used as a callback argument.
6382 The return code is one of the following values, equivalent to
6383 the STATUS values of __cxa_demangle() (excluding -1, since this
6384 function performs no memory allocations):
6385 0: The demangling operation succeeded.
6386 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
6387 -3: One of the arguments is invalid.
6389 The demangling is performed using the C++ ABI mangling rules, with
6393 __gcclibcxx_demangle_callback (const char *mangled_name
,
6394 void (*callback
) (const char *, size_t, void *),
6399 if (mangled_name
== NULL
|| callback
== NULL
)
6402 status
= d_demangle_callback (mangled_name
, DMGL_PARAMS
| DMGL_TYPES
,
6410 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
6412 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
6413 mangled name, return a buffer allocated with malloc holding the
6414 demangled name. Otherwise, return NULL. */
6417 cplus_demangle_v3 (const char *mangled
, int options
)
6421 return d_demangle (mangled
, options
, &alc
);
6425 cplus_demangle_v3_callback (const char *mangled
, int options
,
6426 demangle_callbackref callback
, void *opaque
)
6428 return d_demangle_callback (mangled
, options
, callback
, opaque
);
6431 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
6432 conventions, but the output formatting is a little different.
6433 This instructs the C++ demangler not to emit pointer characters ("*"), to
6434 use Java's namespace separator symbol ("." instead of "::"), and to output
6435 JArray<TYPE> as TYPE[]. */
6438 java_demangle_v3 (const char *mangled
)
6442 return d_demangle (mangled
, DMGL_JAVA
| DMGL_PARAMS
| DMGL_RET_POSTFIX
, &alc
);
6446 java_demangle_v3_callback (const char *mangled
,
6447 demangle_callbackref callback
, void *opaque
)
6449 return d_demangle_callback (mangled
,
6450 DMGL_JAVA
| DMGL_PARAMS
| DMGL_RET_POSTFIX
,
6454 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
6456 #ifndef IN_GLIBCPP_V3
6458 /* Demangle a string in order to find out whether it is a constructor
6459 or destructor. Return non-zero on success. Set *CTOR_KIND and
6460 *DTOR_KIND appropriately. */
6463 is_ctor_or_dtor (const char *mangled
,
6464 enum gnu_v3_ctor_kinds
*ctor_kind
,
6465 enum gnu_v3_dtor_kinds
*dtor_kind
)
6468 struct demangle_component
*dc
;
6471 *ctor_kind
= (enum gnu_v3_ctor_kinds
) 0;
6472 *dtor_kind
= (enum gnu_v3_dtor_kinds
) 0;
6474 cplus_demangle_init_info (mangled
, DMGL_GNU_V3
, strlen (mangled
), &di
);
6477 #ifdef CP_DYNAMIC_ARRAYS
6478 __extension__
struct demangle_component comps
[di
.num_comps
];
6479 __extension__
struct demangle_component
*subs
[di
.num_subs
];
6484 di
.comps
= alloca (di
.num_comps
* sizeof (*di
.comps
));
6485 di
.subs
= alloca (di
.num_subs
* sizeof (*di
.subs
));
6488 dc
= cplus_demangle_mangled_name (&di
, 1);
6490 /* Note that because we did not pass DMGL_PARAMS, we don't expect
6491 to demangle the entire string. */
6498 /* These cannot appear on a constructor or destructor. */
6499 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
6500 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
6501 case DEMANGLE_COMPONENT_CONST_THIS
:
6502 case DEMANGLE_COMPONENT_REFERENCE_THIS
:
6503 case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
:
6507 case DEMANGLE_COMPONENT_TYPED_NAME
:
6508 case DEMANGLE_COMPONENT_TEMPLATE
:
6511 case DEMANGLE_COMPONENT_QUAL_NAME
:
6512 case DEMANGLE_COMPONENT_LOCAL_NAME
:
6515 case DEMANGLE_COMPONENT_CTOR
:
6516 *ctor_kind
= dc
->u
.s_ctor
.kind
;
6520 case DEMANGLE_COMPONENT_DTOR
:
6521 *dtor_kind
= dc
->u
.s_dtor
.kind
;
6532 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
6533 name. A non-zero return indicates the type of constructor. */
6535 enum gnu_v3_ctor_kinds
6536 is_gnu_v3_mangled_ctor (const char *name
)
6538 enum gnu_v3_ctor_kinds ctor_kind
;
6539 enum gnu_v3_dtor_kinds dtor_kind
;
6541 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
6542 return (enum gnu_v3_ctor_kinds
) 0;
6547 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
6548 name. A non-zero return indicates the type of destructor. */
6550 enum gnu_v3_dtor_kinds
6551 is_gnu_v3_mangled_dtor (const char *name
)
6553 enum gnu_v3_ctor_kinds ctor_kind
;
6554 enum gnu_v3_dtor_kinds dtor_kind
;
6556 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
6557 return (enum gnu_v3_dtor_kinds
) 0;
6561 #endif /* IN_GLIBCPP_V3 */
6563 #ifdef STANDALONE_DEMANGLER
6566 #include "dyn-string.h"
6568 static void print_usage (FILE* fp
, int exit_value
);
6570 #define IS_ALPHA(CHAR) \
6571 (((CHAR) >= 'a' && (CHAR) <= 'z') \
6572 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
6574 /* Non-zero if CHAR is a character than can occur in a mangled name. */
6575 #define is_mangled_char(CHAR) \
6576 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
6577 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
6579 /* The name of this program, as invoked. */
6580 const char* program_name
;
6582 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
6585 print_usage (FILE* fp
, int exit_value
)
6587 fprintf (fp
, "Usage: %s [options] [names ...]\n", program_name
);
6588 fprintf (fp
, "Options:\n");
6589 fprintf (fp
, " -h,--help Display this message.\n");
6590 fprintf (fp
, " -p,--no-params Don't display function parameters\n");
6591 fprintf (fp
, " -v,--verbose Produce verbose demanglings.\n");
6592 fprintf (fp
, "If names are provided, they are demangled. Otherwise filters standard input.\n");
6597 /* Option specification for getopt_long. */
6598 static const struct option long_options
[] =
6600 { "help", no_argument
, NULL
, 'h' },
6601 { "no-params", no_argument
, NULL
, 'p' },
6602 { "verbose", no_argument
, NULL
, 'v' },
6603 { NULL
, no_argument
, NULL
, 0 },
6606 /* Main entry for a demangling filter executable. It will demangle
6607 its command line arguments, if any. If none are provided, it will
6608 filter stdin to stdout, replacing any recognized mangled C++ names
6609 with their demangled equivalents. */
6612 main (int argc
, char *argv
[])
6616 int options
= DMGL_PARAMS
| DMGL_ANSI
| DMGL_TYPES
;
6618 /* Use the program name of this program, as invoked. */
6619 program_name
= argv
[0];
6621 /* Parse options. */
6624 opt_char
= getopt_long (argc
, argv
, "hpv", long_options
, NULL
);
6627 case '?': /* Unrecognized option. */
6628 print_usage (stderr
, 1);
6632 print_usage (stdout
, 0);
6636 options
&= ~ DMGL_PARAMS
;
6640 options
|= DMGL_VERBOSE
;
6644 while (opt_char
!= -1);
6647 /* No command line arguments were provided. Filter stdin. */
6649 dyn_string_t mangled
= dyn_string_new (3);
6652 /* Read all of input. */
6653 while (!feof (stdin
))
6657 /* Pile characters into mangled until we hit one that can't
6658 occur in a mangled name. */
6660 while (!feof (stdin
) && is_mangled_char (c
))
6662 dyn_string_append_char (mangled
, c
);
6668 if (dyn_string_length (mangled
) > 0)
6670 #ifdef IN_GLIBCPP_V3
6671 s
= __cxa_demangle (dyn_string_buf (mangled
), NULL
, NULL
, NULL
);
6673 s
= cplus_demangle_v3 (dyn_string_buf (mangled
), options
);
6683 /* It might not have been a mangled name. Print the
6685 fputs (dyn_string_buf (mangled
), stdout
);
6688 dyn_string_clear (mangled
);
6691 /* If we haven't hit EOF yet, we've read one character that
6692 can't occur in a mangled name, so print it out. */
6697 dyn_string_delete (mangled
);
6700 /* Demangle command line arguments. */
6702 /* Loop over command line arguments. */
6703 for (i
= optind
; i
< argc
; ++i
)
6706 #ifdef IN_GLIBCPP_V3
6710 /* Attempt to demangle. */
6711 #ifdef IN_GLIBCPP_V3
6712 s
= __cxa_demangle (argv
[i
], NULL
, NULL
, &status
);
6714 s
= cplus_demangle_v3 (argv
[i
], options
);
6717 /* If it worked, print the demangled name. */
6725 #ifdef IN_GLIBCPP_V3
6726 fprintf (stderr
, "Failed: %s (status %d)\n", argv
[i
], status
);
6728 fprintf (stderr
, "Failed: %s\n", argv
[i
]);
6737 #endif /* STANDALONE_DEMANGLER */