1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor <ian@wasabisystems.com>.
6 This file is part of the libiberty library, which is part of GCC.
8 This file is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 In addition to the permissions in the GNU General Public License, the
14 Free Software Foundation gives you unlimited permission to link the
15 compiled version of this file into combinations with other programs,
16 and to distribute those combinations without any restriction coming
17 from the use of this file. (The General Public License restrictions
18 do apply in other respects; for example, they cover modification of
19 the file, and distribution when not linked into a combined
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
32 /* This code implements a demangler for the g++ V3 ABI. The ABI is
33 described on this web page:
34 http://www.codesourcery.com/cxx-abi/abi.html#mangling
36 This code was written while looking at the demangler written by
37 Alex Samuel <samuel@codesourcery.com>.
39 This code first pulls the mangled name apart into a list of
40 components, and then walks the list generating the demangled
43 This file will normally define the following functions, q.v.:
44 char *cplus_demangle_v3(const char *mangled, int options)
45 char *java_demangle_v3(const char *mangled)
46 int cplus_demangle_v3_callback(const char *mangled, int options,
47 demangle_callbackref callback)
48 int java_demangle_v3_callback(const char *mangled,
49 demangle_callbackref callback)
50 enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
51 enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
53 Also, the interface to the component list is public, and defined in
54 demangle.h. The interface consists of these types, which are
55 defined in demangle.h:
56 enum demangle_component_type
57 struct demangle_component
59 and these functions defined in this file:
60 cplus_demangle_fill_name
61 cplus_demangle_fill_extended_operator
62 cplus_demangle_fill_ctor
63 cplus_demangle_fill_dtor
65 cplus_demangle_print_callback
66 and other functions defined in the file cp-demint.c.
68 This file also defines some other functions and variables which are
69 only to be used by the file cp-demint.c.
71 Preprocessor macros you can define while compiling this file:
74 If defined, this file defines the following functions, q.v.:
75 char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
77 int __gcclibcxx_demangle_callback (const char *,
79 (const char *, size_t, void *),
81 instead of cplus_demangle_v3[_callback]() and
82 java_demangle_v3[_callback]().
85 If defined, this file defines only __cxa_demangle() and
86 __gcclibcxx_demangle_callback(), and no other publically visible
87 functions or variables.
90 If defined, this file defines a main() function which demangles
91 any arguments, or, if none, demangles stdin.
94 If defined, turns on debugging mode, which prints information on
95 stdout about the mangled string. This is not generally useful.
98 #if defined (_AIX) && !defined (__GNUC__)
120 # define alloca __builtin_alloca
122 extern char *alloca ();
123 # endif /* __GNUC__ */
125 #endif /* HAVE_ALLOCA_H */
127 #include "ansidecl.h"
128 #include "libiberty.h"
129 #include "demangle.h"
130 #include "cp-demangle.h"
132 /* If IN_GLIBCPP_V3 is defined, some functions are made static. We
133 also rename them via #define to avoid compiler errors when the
134 static definition conflicts with the extern declaration in a header
138 #define CP_STATIC_IF_GLIBCPP_V3 static
140 #define cplus_demangle_fill_name d_fill_name
141 static int d_fill_name (struct demangle_component
*, const char *, int);
143 #define cplus_demangle_fill_extended_operator d_fill_extended_operator
145 d_fill_extended_operator (struct demangle_component
*, int,
146 struct demangle_component
*);
148 #define cplus_demangle_fill_ctor d_fill_ctor
150 d_fill_ctor (struct demangle_component
*, enum gnu_v3_ctor_kinds
,
151 struct demangle_component
*);
153 #define cplus_demangle_fill_dtor d_fill_dtor
155 d_fill_dtor (struct demangle_component
*, enum gnu_v3_dtor_kinds
,
156 struct demangle_component
*);
158 #define cplus_demangle_mangled_name d_mangled_name
159 static struct demangle_component
*d_mangled_name (struct d_info
*, int);
161 #define cplus_demangle_type d_type
162 static struct demangle_component
*d_type (struct d_info
*);
164 #define cplus_demangle_print d_print
165 static char *d_print (int, const struct demangle_component
*, int, size_t *);
167 #define cplus_demangle_print_callback d_print_callback
168 static int d_print_callback (int, const struct demangle_component
*,
169 demangle_callbackref
, void *);
171 #define cplus_demangle_init_info d_init_info
172 static void d_init_info (const char *, int, size_t, struct d_info
*);
174 #else /* ! defined(IN_GLIBCPP_V3) */
175 #define CP_STATIC_IF_GLIBCPP_V3
176 #endif /* ! defined(IN_GLIBCPP_V3) */
178 /* See if the compiler supports dynamic arrays. */
181 #define CP_DYNAMIC_ARRAYS
184 #ifdef __STDC_VERSION__
185 #if __STDC_VERSION__ >= 199901L
186 #define CP_DYNAMIC_ARRAYS
187 #endif /* __STDC__VERSION >= 199901L */
188 #endif /* defined (__STDC_VERSION__) */
189 #endif /* defined (__STDC__) */
190 #endif /* ! defined (__GNUC__) */
192 /* We avoid pulling in the ctype tables, to prevent pulling in
193 additional unresolved symbols when this code is used in a library.
194 FIXME: Is this really a valid reason? This comes from the original
197 As of this writing this file has the following undefined references
198 when compiled with -DIN_GLIBCPP_V3: realloc, free, memcpy, strcpy,
201 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
202 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
203 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
205 /* The prefix prepended by GCC to an identifier represnting the
206 anonymous namespace. */
207 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
208 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
209 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
211 /* Information we keep for the standard substitutions. */
213 struct d_standard_sub_info
215 /* The code for this substitution. */
217 /* The simple string it expands to. */
218 const char *simple_expansion
;
219 /* The length of the simple expansion. */
221 /* The results of a full, verbose, expansion. This is used when
222 qualifying a constructor/destructor, or when in verbose mode. */
223 const char *full_expansion
;
224 /* The length of the full expansion. */
226 /* What to set the last_name field of d_info to; NULL if we should
227 not set it. This is only relevant when qualifying a
228 constructor/destructor. */
229 const char *set_last_name
;
230 /* The length of set_last_name. */
231 int set_last_name_len
;
234 /* Accessors for subtrees of struct demangle_component. */
236 #define d_left(dc) ((dc)->u.s_binary.left)
237 #define d_right(dc) ((dc)->u.s_binary.right)
239 /* A list of templates. This is used while printing. */
241 struct d_print_template
243 /* Next template on the list. */
244 struct d_print_template
*next
;
246 const struct demangle_component
*template_decl
;
249 /* A list of type modifiers. This is used while printing. */
253 /* Next modifier on the list. These are in the reverse of the order
254 in which they appeared in the mangled string. */
255 struct d_print_mod
*next
;
257 const struct demangle_component
*mod
;
258 /* Whether this modifier was printed. */
260 /* The list of templates which applies to this modifier. */
261 struct d_print_template
*templates
;
264 /* We use these structures to hold information during printing. */
266 struct d_growable_string
268 /* Buffer holding the result. */
270 /* Current length of data in buffer. */
272 /* Allocated size of buffer. */
274 /* Set to 1 if we had a memory allocation failure. */
275 int allocation_failure
;
278 enum { D_PRINT_BUFFER_LENGTH
= 256 };
281 /* Fixed-length allocated buffer for demangled data, flushed to the
282 callback with a NUL termination once full. */
283 char buf
[D_PRINT_BUFFER_LENGTH
];
284 /* Current length of data in buffer. */
286 /* The last character printed, saved individually so that it survives
289 /* Callback function to handle demangled buffer flush. */
290 demangle_callbackref callback
;
291 /* Opaque callback argument. */
293 /* The current list of templates, if any. */
294 struct d_print_template
*templates
;
295 /* The current list of modifiers (e.g., pointer, reference, etc.),
297 struct d_print_mod
*modifiers
;
298 /* Set to 1 if we saw a demangling error. */
299 int demangle_failure
;
300 /* The current index into any template argument packs we are using
303 /* Number of d_print_flush calls so far. */
304 unsigned long int flush_count
;
307 #ifdef CP_DEMANGLE_DEBUG
308 static void d_dump (struct demangle_component
*, int);
311 static struct demangle_component
*
312 d_make_empty (struct d_info
*);
314 static struct demangle_component
*
315 d_make_comp (struct d_info
*, enum demangle_component_type
,
316 struct demangle_component
*,
317 struct demangle_component
*);
319 static struct demangle_component
*
320 d_make_name (struct d_info
*, const char *, int);
322 static struct demangle_component
*
323 d_make_demangle_mangled_name (struct d_info
*, const char *);
325 static struct demangle_component
*
326 d_make_builtin_type (struct d_info
*,
327 const struct demangle_builtin_type_info
*);
329 static struct demangle_component
*
330 d_make_operator (struct d_info
*,
331 const struct demangle_operator_info
*);
333 static struct demangle_component
*
334 d_make_extended_operator (struct d_info
*, int,
335 struct demangle_component
*);
337 static struct demangle_component
*
338 d_make_ctor (struct d_info
*, enum gnu_v3_ctor_kinds
,
339 struct demangle_component
*);
341 static struct demangle_component
*
342 d_make_dtor (struct d_info
*, enum gnu_v3_dtor_kinds
,
343 struct demangle_component
*);
345 static struct demangle_component
*
346 d_make_template_param (struct d_info
*, long);
348 static struct demangle_component
*
349 d_make_sub (struct d_info
*, const char *, int);
352 has_return_type (struct demangle_component
*);
355 is_ctor_dtor_or_conversion (struct demangle_component
*);
357 static struct demangle_component
*d_encoding (struct d_info
*, int);
359 static struct demangle_component
*d_name (struct d_info
*);
361 static struct demangle_component
*d_nested_name (struct d_info
*);
363 static struct demangle_component
*d_prefix (struct d_info
*);
365 static struct demangle_component
*d_unqualified_name (struct d_info
*);
367 static struct demangle_component
*d_source_name (struct d_info
*);
369 static long d_number (struct d_info
*);
371 static struct demangle_component
*d_identifier (struct d_info
*, int);
373 static struct demangle_component
*d_operator_name (struct d_info
*);
375 static struct demangle_component
*d_special_name (struct d_info
*);
377 static int d_call_offset (struct d_info
*, int);
379 static struct demangle_component
*d_ctor_dtor_name (struct d_info
*);
381 static struct demangle_component
**
382 d_cv_qualifiers (struct d_info
*, struct demangle_component
**, int);
384 static struct demangle_component
*
385 d_function_type (struct d_info
*);
387 static struct demangle_component
*
388 d_bare_function_type (struct d_info
*, int);
390 static struct demangle_component
*
391 d_class_enum_type (struct d_info
*);
393 static struct demangle_component
*d_array_type (struct d_info
*);
395 static struct demangle_component
*d_vector_type (struct d_info
*);
397 static struct demangle_component
*
398 d_pointer_to_member_type (struct d_info
*);
400 static struct demangle_component
*
401 d_template_param (struct d_info
*);
403 static struct demangle_component
*d_template_args (struct d_info
*);
405 static struct demangle_component
*
406 d_template_arg (struct d_info
*);
408 static struct demangle_component
*d_expression (struct d_info
*);
410 static struct demangle_component
*d_expr_primary (struct d_info
*);
412 static struct demangle_component
*d_local_name (struct d_info
*);
414 static int d_discriminator (struct d_info
*);
416 static struct demangle_component
*d_lambda (struct d_info
*);
418 static struct demangle_component
*d_unnamed_type (struct d_info
*);
420 static struct demangle_component
*
421 d_clone_suffix (struct d_info
*, struct demangle_component
*);
424 d_add_substitution (struct d_info
*, struct demangle_component
*);
426 static struct demangle_component
*d_substitution (struct d_info
*, int);
428 static void d_growable_string_init (struct d_growable_string
*, size_t);
431 d_growable_string_resize (struct d_growable_string
*, size_t);
434 d_growable_string_append_buffer (struct d_growable_string
*,
435 const char *, size_t);
437 d_growable_string_callback_adapter (const char *, size_t, void *);
440 d_print_init (struct d_print_info
*, demangle_callbackref
, void *);
442 static inline void d_print_error (struct d_print_info
*);
444 static inline int d_print_saw_error (struct d_print_info
*);
446 static inline void d_print_flush (struct d_print_info
*);
448 static inline void d_append_char (struct d_print_info
*, char);
450 static inline void d_append_buffer (struct d_print_info
*,
451 const char *, size_t);
453 static inline void d_append_string (struct d_print_info
*, const char *);
455 static inline char d_last_char (struct d_print_info
*);
458 d_print_comp (struct d_print_info
*, int, const struct demangle_component
*);
461 d_print_java_identifier (struct d_print_info
*, const char *, int);
464 d_print_mod_list (struct d_print_info
*, int, struct d_print_mod
*, int);
467 d_print_mod (struct d_print_info
*, int, const struct demangle_component
*);
470 d_print_function_type (struct d_print_info
*, int,
471 const struct demangle_component
*,
472 struct d_print_mod
*);
475 d_print_array_type (struct d_print_info
*, int,
476 const struct demangle_component
*,
477 struct d_print_mod
*);
480 d_print_expr_op (struct d_print_info
*, int, const struct demangle_component
*);
483 d_print_cast (struct d_print_info
*, int, const struct demangle_component
*);
485 static int d_demangle_callback (const char *, int,
486 demangle_callbackref
, void *);
487 static char *d_demangle (const char *, int, size_t *);
489 #ifdef CP_DEMANGLE_DEBUG
492 d_dump (struct demangle_component
*dc
, int indent
)
499 printf ("failed demangling\n");
503 for (i
= 0; i
< indent
; ++i
)
508 case DEMANGLE_COMPONENT_NAME
:
509 printf ("name '%.*s'\n", dc
->u
.s_name
.len
, dc
->u
.s_name
.s
);
511 case DEMANGLE_COMPONENT_TAGGED_NAME
:
512 printf ("tagged name\n");
513 d_dump (dc
->u
.s_binary
.left
, indent
+ 2);
514 d_dump (dc
->u
.s_binary
.right
, indent
+ 2);
516 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
517 printf ("template parameter %ld\n", dc
->u
.s_number
.number
);
519 case DEMANGLE_COMPONENT_CTOR
:
520 printf ("constructor %d\n", (int) dc
->u
.s_ctor
.kind
);
521 d_dump (dc
->u
.s_ctor
.name
, indent
+ 2);
523 case DEMANGLE_COMPONENT_DTOR
:
524 printf ("destructor %d\n", (int) dc
->u
.s_dtor
.kind
);
525 d_dump (dc
->u
.s_dtor
.name
, indent
+ 2);
527 case DEMANGLE_COMPONENT_SUB_STD
:
528 printf ("standard substitution %s\n", dc
->u
.s_string
.string
);
530 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
531 printf ("builtin type %s\n", dc
->u
.s_builtin
.type
->name
);
533 case DEMANGLE_COMPONENT_OPERATOR
:
534 printf ("operator %s\n", dc
->u
.s_operator
.op
->name
);
536 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
537 printf ("extended operator with %d args\n",
538 dc
->u
.s_extended_operator
.args
);
539 d_dump (dc
->u
.s_extended_operator
.name
, indent
+ 2);
542 case DEMANGLE_COMPONENT_QUAL_NAME
:
543 printf ("qualified name\n");
545 case DEMANGLE_COMPONENT_LOCAL_NAME
:
546 printf ("local name\n");
548 case DEMANGLE_COMPONENT_TYPED_NAME
:
549 printf ("typed name\n");
551 case DEMANGLE_COMPONENT_TEMPLATE
:
552 printf ("template\n");
554 case DEMANGLE_COMPONENT_VTABLE
:
557 case DEMANGLE_COMPONENT_VTT
:
560 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
561 printf ("construction vtable\n");
563 case DEMANGLE_COMPONENT_TYPEINFO
:
564 printf ("typeinfo\n");
566 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
567 printf ("typeinfo name\n");
569 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
570 printf ("typeinfo function\n");
572 case DEMANGLE_COMPONENT_THUNK
:
575 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
576 printf ("virtual thunk\n");
578 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
579 printf ("covariant thunk\n");
581 case DEMANGLE_COMPONENT_JAVA_CLASS
:
582 printf ("java class\n");
584 case DEMANGLE_COMPONENT_GUARD
:
587 case DEMANGLE_COMPONENT_REFTEMP
:
588 printf ("reference temporary\n");
590 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
591 printf ("hidden alias\n");
593 case DEMANGLE_COMPONENT_TRANSACTION_CLONE
:
594 printf ("transaction clone\n");
596 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
:
597 printf ("non-transaction clone\n");
599 case DEMANGLE_COMPONENT_RESTRICT
:
600 printf ("restrict\n");
602 case DEMANGLE_COMPONENT_VOLATILE
:
603 printf ("volatile\n");
605 case DEMANGLE_COMPONENT_CONST
:
608 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
609 printf ("restrict this\n");
611 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
612 printf ("volatile this\n");
614 case DEMANGLE_COMPONENT_CONST_THIS
:
615 printf ("const this\n");
617 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
618 printf ("vendor type qualifier\n");
620 case DEMANGLE_COMPONENT_POINTER
:
621 printf ("pointer\n");
623 case DEMANGLE_COMPONENT_REFERENCE
:
624 printf ("reference\n");
626 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
627 printf ("rvalue reference\n");
629 case DEMANGLE_COMPONENT_COMPLEX
:
630 printf ("complex\n");
632 case DEMANGLE_COMPONENT_IMAGINARY
:
633 printf ("imaginary\n");
635 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
636 printf ("vendor type\n");
638 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
639 printf ("function type\n");
641 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
642 printf ("array type\n");
644 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
645 printf ("pointer to member type\n");
647 case DEMANGLE_COMPONENT_FIXED_TYPE
:
648 printf ("fixed-point type\n");
650 case DEMANGLE_COMPONENT_ARGLIST
:
651 printf ("argument list\n");
653 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
654 printf ("template argument list\n");
656 case DEMANGLE_COMPONENT_INITIALIZER_LIST
:
657 printf ("initializer list\n");
659 case DEMANGLE_COMPONENT_CAST
:
662 case DEMANGLE_COMPONENT_NULLARY
:
663 printf ("nullary operator\n");
665 case DEMANGLE_COMPONENT_UNARY
:
666 printf ("unary operator\n");
668 case DEMANGLE_COMPONENT_BINARY
:
669 printf ("binary operator\n");
671 case DEMANGLE_COMPONENT_BINARY_ARGS
:
672 printf ("binary operator arguments\n");
674 case DEMANGLE_COMPONENT_TRINARY
:
675 printf ("trinary operator\n");
677 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
678 printf ("trinary operator arguments 1\n");
680 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
681 printf ("trinary operator arguments 1\n");
683 case DEMANGLE_COMPONENT_LITERAL
:
684 printf ("literal\n");
686 case DEMANGLE_COMPONENT_LITERAL_NEG
:
687 printf ("negative literal\n");
689 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
690 printf ("java resource\n");
692 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
693 printf ("compound name\n");
695 case DEMANGLE_COMPONENT_CHARACTER
:
696 printf ("character '%c'\n", dc
->u
.s_character
.character
);
698 case DEMANGLE_COMPONENT_DECLTYPE
:
699 printf ("decltype\n");
701 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
702 printf ("pack expansion\n");
704 case DEMANGLE_COMPONENT_TLS_INIT
:
705 printf ("tls init function\n");
707 case DEMANGLE_COMPONENT_TLS_WRAPPER
:
708 printf ("tls wrapper function\n");
712 d_dump (d_left (dc
), indent
+ 2);
713 d_dump (d_right (dc
), indent
+ 2);
716 #endif /* CP_DEMANGLE_DEBUG */
718 /* Fill in a DEMANGLE_COMPONENT_NAME. */
720 CP_STATIC_IF_GLIBCPP_V3
722 cplus_demangle_fill_name (struct demangle_component
*p
, const char *s
, int len
)
724 if (p
== NULL
|| s
== NULL
|| len
== 0)
726 p
->type
= DEMANGLE_COMPONENT_NAME
;
728 p
->u
.s_name
.len
= len
;
732 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
734 CP_STATIC_IF_GLIBCPP_V3
736 cplus_demangle_fill_extended_operator (struct demangle_component
*p
, int args
,
737 struct demangle_component
*name
)
739 if (p
== NULL
|| args
< 0 || name
== NULL
)
741 p
->type
= DEMANGLE_COMPONENT_EXTENDED_OPERATOR
;
742 p
->u
.s_extended_operator
.args
= args
;
743 p
->u
.s_extended_operator
.name
= name
;
747 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
749 CP_STATIC_IF_GLIBCPP_V3
751 cplus_demangle_fill_ctor (struct demangle_component
*p
,
752 enum gnu_v3_ctor_kinds kind
,
753 struct demangle_component
*name
)
757 || (int) kind
< gnu_v3_complete_object_ctor
758 || (int) kind
> gnu_v3_object_ctor_group
)
760 p
->type
= DEMANGLE_COMPONENT_CTOR
;
761 p
->u
.s_ctor
.kind
= kind
;
762 p
->u
.s_ctor
.name
= name
;
766 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
768 CP_STATIC_IF_GLIBCPP_V3
770 cplus_demangle_fill_dtor (struct demangle_component
*p
,
771 enum gnu_v3_dtor_kinds kind
,
772 struct demangle_component
*name
)
776 || (int) kind
< gnu_v3_deleting_dtor
777 || (int) kind
> gnu_v3_object_dtor_group
)
779 p
->type
= DEMANGLE_COMPONENT_DTOR
;
780 p
->u
.s_dtor
.kind
= kind
;
781 p
->u
.s_dtor
.name
= name
;
785 /* Add a new component. */
787 static struct demangle_component
*
788 d_make_empty (struct d_info
*di
)
790 struct demangle_component
*p
;
792 if (di
->next_comp
>= di
->num_comps
)
794 p
= &di
->comps
[di
->next_comp
];
799 /* Add a new generic component. */
801 static struct demangle_component
*
802 d_make_comp (struct d_info
*di
, enum demangle_component_type type
,
803 struct demangle_component
*left
,
804 struct demangle_component
*right
)
806 struct demangle_component
*p
;
808 /* We check for errors here. A typical error would be a NULL return
809 from a subroutine. We catch those here, and return NULL
813 /* These types require two parameters. */
814 case DEMANGLE_COMPONENT_QUAL_NAME
:
815 case DEMANGLE_COMPONENT_LOCAL_NAME
:
816 case DEMANGLE_COMPONENT_TYPED_NAME
:
817 case DEMANGLE_COMPONENT_TAGGED_NAME
:
818 case DEMANGLE_COMPONENT_TEMPLATE
:
819 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
820 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
821 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
822 case DEMANGLE_COMPONENT_UNARY
:
823 case DEMANGLE_COMPONENT_BINARY
:
824 case DEMANGLE_COMPONENT_BINARY_ARGS
:
825 case DEMANGLE_COMPONENT_TRINARY
:
826 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
827 case DEMANGLE_COMPONENT_LITERAL
:
828 case DEMANGLE_COMPONENT_LITERAL_NEG
:
829 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
830 case DEMANGLE_COMPONENT_VECTOR_TYPE
:
831 case DEMANGLE_COMPONENT_CLONE
:
832 if (left
== NULL
|| right
== NULL
)
836 /* These types only require one parameter. */
837 case DEMANGLE_COMPONENT_VTABLE
:
838 case DEMANGLE_COMPONENT_VTT
:
839 case DEMANGLE_COMPONENT_TYPEINFO
:
840 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
841 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
842 case DEMANGLE_COMPONENT_THUNK
:
843 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
844 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
845 case DEMANGLE_COMPONENT_JAVA_CLASS
:
846 case DEMANGLE_COMPONENT_GUARD
:
847 case DEMANGLE_COMPONENT_TLS_INIT
:
848 case DEMANGLE_COMPONENT_TLS_WRAPPER
:
849 case DEMANGLE_COMPONENT_REFTEMP
:
850 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
851 case DEMANGLE_COMPONENT_TRANSACTION_CLONE
:
852 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
:
853 case DEMANGLE_COMPONENT_POINTER
:
854 case DEMANGLE_COMPONENT_REFERENCE
:
855 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
856 case DEMANGLE_COMPONENT_COMPLEX
:
857 case DEMANGLE_COMPONENT_IMAGINARY
:
858 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
859 case DEMANGLE_COMPONENT_CAST
:
860 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
861 case DEMANGLE_COMPONENT_DECLTYPE
:
862 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
863 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
:
864 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS
:
865 case DEMANGLE_COMPONENT_NULLARY
:
866 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
871 /* This needs a right parameter, but the left parameter can be
873 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
874 case DEMANGLE_COMPONENT_INITIALIZER_LIST
:
879 /* These are allowed to have no parameters--in some cases they
880 will be filled in later. */
881 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
882 case DEMANGLE_COMPONENT_RESTRICT
:
883 case DEMANGLE_COMPONENT_VOLATILE
:
884 case DEMANGLE_COMPONENT_CONST
:
885 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
886 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
887 case DEMANGLE_COMPONENT_CONST_THIS
:
888 case DEMANGLE_COMPONENT_ARGLIST
:
889 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
892 /* Other types should not be seen here. */
897 p
= d_make_empty (di
);
901 p
->u
.s_binary
.left
= left
;
902 p
->u
.s_binary
.right
= right
;
907 /* Add a new demangle mangled name component. */
909 static struct demangle_component
*
910 d_make_demangle_mangled_name (struct d_info
*di
, const char *s
)
912 if (d_peek_char (di
) != '_' || d_peek_next_char (di
) != 'Z')
913 return d_make_name (di
, s
, strlen (s
));
915 return d_encoding (di
, 0);
918 /* Add a new name component. */
920 static struct demangle_component
*
921 d_make_name (struct d_info
*di
, const char *s
, int len
)
923 struct demangle_component
*p
;
925 p
= d_make_empty (di
);
926 if (! cplus_demangle_fill_name (p
, s
, len
))
931 /* Add a new builtin type component. */
933 static struct demangle_component
*
934 d_make_builtin_type (struct d_info
*di
,
935 const struct demangle_builtin_type_info
*type
)
937 struct demangle_component
*p
;
941 p
= d_make_empty (di
);
944 p
->type
= DEMANGLE_COMPONENT_BUILTIN_TYPE
;
945 p
->u
.s_builtin
.type
= type
;
950 /* Add a new operator component. */
952 static struct demangle_component
*
953 d_make_operator (struct d_info
*di
, const struct demangle_operator_info
*op
)
955 struct demangle_component
*p
;
957 p
= d_make_empty (di
);
960 p
->type
= DEMANGLE_COMPONENT_OPERATOR
;
961 p
->u
.s_operator
.op
= op
;
966 /* Add a new extended operator component. */
968 static struct demangle_component
*
969 d_make_extended_operator (struct d_info
*di
, int args
,
970 struct demangle_component
*name
)
972 struct demangle_component
*p
;
974 p
= d_make_empty (di
);
975 if (! cplus_demangle_fill_extended_operator (p
, args
, name
))
980 static struct demangle_component
*
981 d_make_default_arg (struct d_info
*di
, int num
,
982 struct demangle_component
*sub
)
984 struct demangle_component
*p
= d_make_empty (di
);
987 p
->type
= DEMANGLE_COMPONENT_DEFAULT_ARG
;
988 p
->u
.s_unary_num
.num
= num
;
989 p
->u
.s_unary_num
.sub
= sub
;
994 /* Add a new constructor component. */
996 static struct demangle_component
*
997 d_make_ctor (struct d_info
*di
, enum gnu_v3_ctor_kinds kind
,
998 struct demangle_component
*name
)
1000 struct demangle_component
*p
;
1002 p
= d_make_empty (di
);
1003 if (! cplus_demangle_fill_ctor (p
, kind
, name
))
1008 /* Add a new destructor component. */
1010 static struct demangle_component
*
1011 d_make_dtor (struct d_info
*di
, enum gnu_v3_dtor_kinds kind
,
1012 struct demangle_component
*name
)
1014 struct demangle_component
*p
;
1016 p
= d_make_empty (di
);
1017 if (! cplus_demangle_fill_dtor (p
, kind
, name
))
1022 /* Add a new template parameter. */
1024 static struct demangle_component
*
1025 d_make_template_param (struct d_info
*di
, long i
)
1027 struct demangle_component
*p
;
1029 p
= d_make_empty (di
);
1032 p
->type
= DEMANGLE_COMPONENT_TEMPLATE_PARAM
;
1033 p
->u
.s_number
.number
= i
;
1038 /* Add a new function parameter. */
1040 static struct demangle_component
*
1041 d_make_function_param (struct d_info
*di
, long i
)
1043 struct demangle_component
*p
;
1045 p
= d_make_empty (di
);
1048 p
->type
= DEMANGLE_COMPONENT_FUNCTION_PARAM
;
1049 p
->u
.s_number
.number
= i
;
1054 /* Add a new standard substitution component. */
1056 static struct demangle_component
*
1057 d_make_sub (struct d_info
*di
, const char *name
, int len
)
1059 struct demangle_component
*p
;
1061 p
= d_make_empty (di
);
1064 p
->type
= DEMANGLE_COMPONENT_SUB_STD
;
1065 p
->u
.s_string
.string
= name
;
1066 p
->u
.s_string
.len
= len
;
1071 /* <mangled-name> ::= _Z <encoding> [<clone-suffix>]*
1073 TOP_LEVEL is non-zero when called at the top level. */
1075 CP_STATIC_IF_GLIBCPP_V3
1076 struct demangle_component
*
1077 cplus_demangle_mangled_name (struct d_info
*di
, int top_level
)
1079 struct demangle_component
*p
;
1081 if (! d_check_char (di
, '_')
1082 /* Allow missing _ if not at toplevel to work around a
1083 bug in G++ abi-version=2 mangling; see the comment in
1084 write_template_arg. */
1087 if (! d_check_char (di
, 'Z'))
1089 p
= d_encoding (di
, top_level
);
1091 /* If at top level and parsing parameters, check for a clone
1093 if (top_level
&& (di
->options
& DMGL_PARAMS
) != 0)
1094 while (d_peek_char (di
) == '.'
1095 && (IS_LOWER (d_peek_next_char (di
))
1096 || d_peek_next_char (di
) == '_'
1097 || IS_DIGIT (d_peek_next_char (di
))))
1098 p
= d_clone_suffix (di
, p
);
1103 /* Return whether a function should have a return type. The argument
1104 is the function name, which may be qualified in various ways. The
1105 rules are that template functions have return types with some
1106 exceptions, function types which are not part of a function name
1107 mangling have return types with some exceptions, and non-template
1108 function names do not have return types. The exceptions are that
1109 constructors, destructors, and conversion operators do not have
1113 has_return_type (struct demangle_component
*dc
)
1121 case DEMANGLE_COMPONENT_TEMPLATE
:
1122 return ! is_ctor_dtor_or_conversion (d_left (dc
));
1123 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
1124 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
1125 case DEMANGLE_COMPONENT_CONST_THIS
:
1126 return has_return_type (d_left (dc
));
1130 /* Return whether a name is a constructor, a destructor, or a
1131 conversion operator. */
1134 is_ctor_dtor_or_conversion (struct demangle_component
*dc
)
1142 case DEMANGLE_COMPONENT_QUAL_NAME
:
1143 case DEMANGLE_COMPONENT_LOCAL_NAME
:
1144 return is_ctor_dtor_or_conversion (d_right (dc
));
1145 case DEMANGLE_COMPONENT_CTOR
:
1146 case DEMANGLE_COMPONENT_DTOR
:
1147 case DEMANGLE_COMPONENT_CAST
:
1152 /* <encoding> ::= <(function) name> <bare-function-type>
1156 TOP_LEVEL is non-zero when called at the top level, in which case
1157 if DMGL_PARAMS is not set we do not demangle the function
1158 parameters. We only set this at the top level, because otherwise
1159 we would not correctly demangle names in local scopes. */
1161 static struct demangle_component
*
1162 d_encoding (struct d_info
*di
, int top_level
)
1164 char peek
= d_peek_char (di
);
1166 if (peek
== 'G' || peek
== 'T')
1167 return d_special_name (di
);
1170 struct demangle_component
*dc
;
1174 if (dc
!= NULL
&& top_level
&& (di
->options
& DMGL_PARAMS
) == 0)
1176 /* Strip off any initial CV-qualifiers, as they really apply
1177 to the `this' parameter, and they were not output by the
1178 v2 demangler without DMGL_PARAMS. */
1179 while (dc
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
1180 || dc
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
1181 || dc
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
1184 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1185 there may be CV-qualifiers on its right argument which
1186 really apply here; this happens when parsing a class
1187 which is local to a function. */
1188 if (dc
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
1190 struct demangle_component
*dcr
;
1193 while (dcr
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
1194 || dcr
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
1195 || dcr
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
1197 dc
->u
.s_binary
.right
= dcr
;
1203 peek
= d_peek_char (di
);
1204 if (dc
== NULL
|| peek
== '\0' || peek
== 'E')
1206 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPED_NAME
, dc
,
1207 d_bare_function_type (di
, has_return_type (dc
)));
1211 /* <tagged-name> ::= <name> B <source-name> */
1213 static struct demangle_component
*
1214 d_abi_tags (struct d_info
*di
, struct demangle_component
*dc
)
1217 while (peek
= d_peek_char (di
),
1220 struct demangle_component
*tag
;
1222 tag
= d_source_name (di
);
1223 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_TAGGED_NAME
, dc
, tag
);
1228 /* <name> ::= <nested-name>
1230 ::= <unscoped-template-name> <template-args>
1233 <unscoped-name> ::= <unqualified-name>
1234 ::= St <unqualified-name>
1236 <unscoped-template-name> ::= <unscoped-name>
1240 static struct demangle_component
*
1241 d_name (struct d_info
*di
)
1243 char peek
= d_peek_char (di
);
1244 struct demangle_component
*dc
;
1249 dc
= d_nested_name (di
);
1253 dc
= d_local_name (di
);
1258 dc
= d_unqualified_name (di
);
1265 if (d_peek_next_char (di
) != 't')
1267 dc
= d_substitution (di
, 0);
1273 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
,
1274 d_make_name (di
, "std", 3),
1275 d_unqualified_name (di
));
1280 if (d_peek_char (di
) != 'I')
1282 /* The grammar does not permit this case to occur if we
1283 called d_substitution() above (i.e., subst == 1). We
1284 don't bother to check. */
1288 /* This is <template-args>, which means that we just saw
1289 <unscoped-template-name>, which is a substitution
1290 candidate if we didn't just get it from a
1294 if (! d_add_substitution (di
, dc
))
1297 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, dc
,
1298 d_template_args (di
));
1305 dc
= d_unqualified_name (di
);
1306 if (d_peek_char (di
) == 'I')
1308 /* This is <template-args>, which means that we just saw
1309 <unscoped-template-name>, which is a substitution
1311 if (! d_add_substitution (di
, dc
))
1313 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, dc
,
1314 d_template_args (di
));
1319 if (d_peek_char (di
) == 'B')
1320 dc
= d_abi_tags (di
, dc
);
1324 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1325 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1328 static struct demangle_component
*
1329 d_nested_name (struct d_info
*di
)
1331 struct demangle_component
*ret
;
1332 struct demangle_component
**pret
;
1334 if (! d_check_char (di
, 'N'))
1337 pret
= d_cv_qualifiers (di
, &ret
, 1);
1341 *pret
= d_prefix (di
);
1345 if (! d_check_char (di
, 'E'))
1351 /* <prefix> ::= <prefix> <unqualified-name>
1352 ::= <template-prefix> <template-args>
1353 ::= <template-param>
1358 <template-prefix> ::= <prefix> <(template) unqualified-name>
1359 ::= <template-param>
1363 static struct demangle_component
*
1364 d_prefix (struct d_info
*di
)
1366 struct demangle_component
*ret
= NULL
;
1371 enum demangle_component_type comb_type
;
1372 struct demangle_component
*dc
;
1374 peek
= d_peek_char (di
);
1378 /* The older code accepts a <local-name> here, but I don't see
1379 that in the grammar. The older code does not accept a
1380 <template-param> here. */
1382 comb_type
= DEMANGLE_COMPONENT_QUAL_NAME
;
1385 char peek2
= d_peek_next_char (di
);
1386 if (peek2
== 'T' || peek2
== 't')
1388 dc
= cplus_demangle_type (di
);
1390 /* Destructor name. */
1391 dc
= d_unqualified_name (di
);
1393 else if (IS_DIGIT (peek
)
1398 dc
= d_unqualified_name (di
);
1399 else if (peek
== 'S')
1400 dc
= d_substitution (di
, 1);
1401 else if (peek
== 'I')
1405 comb_type
= DEMANGLE_COMPONENT_TEMPLATE
;
1406 dc
= d_template_args (di
);
1408 else if (peek
== 'T')
1409 dc
= d_template_param (di
);
1410 else if (peek
== 'E')
1412 else if (peek
== 'M')
1414 /* Initializer scope for a lambda. We don't need to represent
1415 this; the normal code will just treat the variable as a type
1416 scope, which gives appropriate output. */
1428 ret
= d_make_comp (di
, comb_type
, ret
, dc
);
1430 if (peek
!= 'S' && d_peek_char (di
) != 'E')
1432 if (! d_add_substitution (di
, ret
))
1438 /* <unqualified-name> ::= <operator-name>
1439 ::= <ctor-dtor-name>
1441 ::= <local-source-name>
1443 <local-source-name> ::= L <source-name> <discriminator>
1446 static struct demangle_component
*
1447 d_unqualified_name (struct d_info
*di
)
1451 peek
= d_peek_char (di
);
1452 if (IS_DIGIT (peek
))
1453 return d_source_name (di
);
1454 else if (IS_LOWER (peek
))
1456 struct demangle_component
*ret
;
1458 ret
= d_operator_name (di
);
1459 if (ret
!= NULL
&& ret
->type
== DEMANGLE_COMPONENT_OPERATOR
)
1461 di
->expansion
+= sizeof "operator" + ret
->u
.s_operator
.op
->len
- 2;
1462 if (!strcmp (ret
->u
.s_operator
.op
->code
, "li"))
1463 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, ret
,
1464 d_source_name (di
));
1468 else if (peek
== 'C' || peek
== 'D')
1469 return d_ctor_dtor_name (di
);
1470 else if (peek
== 'L')
1472 struct demangle_component
* ret
;
1476 ret
= d_source_name (di
);
1479 if (! d_discriminator (di
))
1483 else if (peek
== 'U')
1485 switch (d_peek_next_char (di
))
1488 return d_lambda (di
);
1490 return d_unnamed_type (di
);
1499 /* <source-name> ::= <(positive length) number> <identifier> */
1501 static struct demangle_component
*
1502 d_source_name (struct d_info
*di
)
1505 struct demangle_component
*ret
;
1507 len
= d_number (di
);
1510 ret
= d_identifier (di
, len
);
1511 di
->last_name
= ret
;
1515 /* number ::= [n] <(non-negative decimal integer)> */
1518 d_number (struct d_info
*di
)
1525 peek
= d_peek_char (di
);
1530 peek
= d_peek_char (di
);
1536 if (! IS_DIGIT (peek
))
1542 ret
= ret
* 10 + peek
- '0';
1544 peek
= d_peek_char (di
);
1548 /* Like d_number, but returns a demangle_component. */
1550 static struct demangle_component
*
1551 d_number_component (struct d_info
*di
)
1553 struct demangle_component
*ret
= d_make_empty (di
);
1556 ret
->type
= DEMANGLE_COMPONENT_NUMBER
;
1557 ret
->u
.s_number
.number
= d_number (di
);
1562 /* identifier ::= <(unqualified source code identifier)> */
1564 static struct demangle_component
*
1565 d_identifier (struct d_info
*di
, int len
)
1571 if (di
->send
- name
< len
)
1574 d_advance (di
, len
);
1576 /* A Java mangled name may have a trailing '$' if it is a C++
1577 keyword. This '$' is not included in the length count. We just
1579 if ((di
->options
& DMGL_JAVA
) != 0
1580 && d_peek_char (di
) == '$')
1583 /* Look for something which looks like a gcc encoding of an
1584 anonymous namespace, and replace it with a more user friendly
1586 if (len
>= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN
+ 2
1587 && memcmp (name
, ANONYMOUS_NAMESPACE_PREFIX
,
1588 ANONYMOUS_NAMESPACE_PREFIX_LEN
) == 0)
1592 s
= name
+ ANONYMOUS_NAMESPACE_PREFIX_LEN
;
1593 if ((*s
== '.' || *s
== '_' || *s
== '$')
1596 di
->expansion
-= len
- sizeof "(anonymous namespace)";
1597 return d_make_name (di
, "(anonymous namespace)",
1598 sizeof "(anonymous namespace)" - 1);
1602 return d_make_name (di
, name
, len
);
1605 /* operator_name ::= many different two character encodings.
1607 ::= v <digit> <source-name>
1609 This list is sorted for binary search. */
1611 #define NL(s) s, (sizeof s) - 1
1613 CP_STATIC_IF_GLIBCPP_V3
1614 const struct demangle_operator_info cplus_demangle_operators
[] =
1616 { "aN", NL ("&="), 2 },
1617 { "aS", NL ("="), 2 },
1618 { "aa", NL ("&&"), 2 },
1619 { "ad", NL ("&"), 1 },
1620 { "an", NL ("&"), 2 },
1621 { "at", NL ("alignof "), 1 },
1622 { "az", NL ("alignof "), 1 },
1623 { "cc", NL ("const_cast"), 2 },
1624 { "cl", NL ("()"), 2 },
1625 { "cm", NL (","), 2 },
1626 { "co", NL ("~"), 1 },
1627 { "dV", NL ("/="), 2 },
1628 { "da", NL ("delete[] "), 1 },
1629 { "dc", NL ("dynamic_cast"), 2 },
1630 { "de", NL ("*"), 1 },
1631 { "dl", NL ("delete "), 1 },
1632 { "ds", NL (".*"), 2 },
1633 { "dt", NL ("."), 2 },
1634 { "dv", NL ("/"), 2 },
1635 { "eO", NL ("^="), 2 },
1636 { "eo", NL ("^"), 2 },
1637 { "eq", NL ("=="), 2 },
1638 { "ge", NL (">="), 2 },
1639 { "gs", NL ("::"), 1 },
1640 { "gt", NL (">"), 2 },
1641 { "ix", NL ("[]"), 2 },
1642 { "lS", NL ("<<="), 2 },
1643 { "le", NL ("<="), 2 },
1644 { "li", NL ("operator\"\" "), 1 },
1645 { "ls", NL ("<<"), 2 },
1646 { "lt", NL ("<"), 2 },
1647 { "mI", NL ("-="), 2 },
1648 { "mL", NL ("*="), 2 },
1649 { "mi", NL ("-"), 2 },
1650 { "ml", NL ("*"), 2 },
1651 { "mm", NL ("--"), 1 },
1652 { "na", NL ("new[]"), 3 },
1653 { "ne", NL ("!="), 2 },
1654 { "ng", NL ("-"), 1 },
1655 { "nt", NL ("!"), 1 },
1656 { "nw", NL ("new"), 3 },
1657 { "oR", NL ("|="), 2 },
1658 { "oo", NL ("||"), 2 },
1659 { "or", NL ("|"), 2 },
1660 { "pL", NL ("+="), 2 },
1661 { "pl", NL ("+"), 2 },
1662 { "pm", NL ("->*"), 2 },
1663 { "pp", NL ("++"), 1 },
1664 { "ps", NL ("+"), 1 },
1665 { "pt", NL ("->"), 2 },
1666 { "qu", NL ("?"), 3 },
1667 { "rM", NL ("%="), 2 },
1668 { "rS", NL (">>="), 2 },
1669 { "rc", NL ("reinterpret_cast"), 2 },
1670 { "rm", NL ("%"), 2 },
1671 { "rs", NL (">>"), 2 },
1672 { "sc", NL ("static_cast"), 2 },
1673 { "st", NL ("sizeof "), 1 },
1674 { "sz", NL ("sizeof "), 1 },
1675 { "tr", NL ("throw"), 0 },
1676 { "tw", NL ("throw "), 1 },
1677 { NULL
, NULL
, 0, 0 }
1680 static struct demangle_component
*
1681 d_operator_name (struct d_info
*di
)
1686 c1
= d_next_char (di
);
1687 c2
= d_next_char (di
);
1688 if (c1
== 'v' && IS_DIGIT (c2
))
1689 return d_make_extended_operator (di
, c2
- '0', d_source_name (di
));
1690 else if (c1
== 'c' && c2
== 'v')
1691 return d_make_comp (di
, DEMANGLE_COMPONENT_CAST
,
1692 cplus_demangle_type (di
), NULL
);
1695 /* LOW is the inclusive lower bound. */
1697 /* HIGH is the exclusive upper bound. We subtract one to ignore
1698 the sentinel at the end of the array. */
1699 int high
= ((sizeof (cplus_demangle_operators
)
1700 / sizeof (cplus_demangle_operators
[0]))
1706 const struct demangle_operator_info
*p
;
1708 i
= low
+ (high
- low
) / 2;
1709 p
= cplus_demangle_operators
+ i
;
1711 if (c1
== p
->code
[0] && c2
== p
->code
[1])
1712 return d_make_operator (di
, p
);
1714 if (c1
< p
->code
[0] || (c1
== p
->code
[0] && c2
< p
->code
[1]))
1724 static struct demangle_component
*
1725 d_make_character (struct d_info
*di
, int c
)
1727 struct demangle_component
*p
;
1728 p
= d_make_empty (di
);
1731 p
->type
= DEMANGLE_COMPONENT_CHARACTER
;
1732 p
->u
.s_character
.character
= c
;
1737 static struct demangle_component
*
1738 d_java_resource (struct d_info
*di
)
1740 struct demangle_component
*p
= NULL
;
1741 struct demangle_component
*next
= NULL
;
1746 len
= d_number (di
);
1750 /* Eat the leading '_'. */
1751 if (d_next_char (di
) != '_')
1764 /* Each chunk is either a '$' escape... */
1782 next
= d_make_character (di
, c
);
1790 /* ... or a sequence of characters. */
1793 while (i
< len
&& str
[i
] && str
[i
] != '$')
1796 next
= d_make_name (di
, str
, i
);
1809 p
= d_make_comp (di
, DEMANGLE_COMPONENT_COMPOUND_NAME
, p
, next
);
1815 p
= d_make_comp (di
, DEMANGLE_COMPONENT_JAVA_RESOURCE
, p
, NULL
);
1820 /* <special-name> ::= TV <type>
1824 ::= GV <(object) name>
1825 ::= T <call-offset> <(base) encoding>
1826 ::= Tc <call-offset> <call-offset> <(base) encoding>
1827 Also g++ extensions:
1828 ::= TC <type> <(offset) number> _ <(base) type>
1833 ::= Gr <resource name>
1838 static struct demangle_component
*
1839 d_special_name (struct d_info
*di
)
1841 di
->expansion
+= 20;
1842 if (d_check_char (di
, 'T'))
1844 switch (d_next_char (di
))
1848 return d_make_comp (di
, DEMANGLE_COMPONENT_VTABLE
,
1849 cplus_demangle_type (di
), NULL
);
1851 di
->expansion
-= 10;
1852 return d_make_comp (di
, DEMANGLE_COMPONENT_VTT
,
1853 cplus_demangle_type (di
), NULL
);
1855 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO
,
1856 cplus_demangle_type (di
), NULL
);
1858 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO_NAME
,
1859 cplus_demangle_type (di
), NULL
);
1862 if (! d_call_offset (di
, 'h'))
1864 return d_make_comp (di
, DEMANGLE_COMPONENT_THUNK
,
1865 d_encoding (di
, 0), NULL
);
1868 if (! d_call_offset (di
, 'v'))
1870 return d_make_comp (di
, DEMANGLE_COMPONENT_VIRTUAL_THUNK
,
1871 d_encoding (di
, 0), NULL
);
1874 if (! d_call_offset (di
, '\0'))
1876 if (! d_call_offset (di
, '\0'))
1878 return d_make_comp (di
, DEMANGLE_COMPONENT_COVARIANT_THUNK
,
1879 d_encoding (di
, 0), NULL
);
1883 struct demangle_component
*derived_type
;
1885 struct demangle_component
*base_type
;
1887 derived_type
= cplus_demangle_type (di
);
1888 offset
= d_number (di
);
1891 if (! d_check_char (di
, '_'))
1893 base_type
= cplus_demangle_type (di
);
1894 /* We don't display the offset. FIXME: We should display
1895 it in verbose mode. */
1897 return d_make_comp (di
, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
,
1898 base_type
, derived_type
);
1902 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO_FN
,
1903 cplus_demangle_type (di
), NULL
);
1905 return d_make_comp (di
, DEMANGLE_COMPONENT_JAVA_CLASS
,
1906 cplus_demangle_type (di
), NULL
);
1909 return d_make_comp (di
, DEMANGLE_COMPONENT_TLS_INIT
,
1913 return d_make_comp (di
, DEMANGLE_COMPONENT_TLS_WRAPPER
,
1920 else if (d_check_char (di
, 'G'))
1922 switch (d_next_char (di
))
1925 return d_make_comp (di
, DEMANGLE_COMPONENT_GUARD
, d_name (di
), NULL
);
1929 struct demangle_component
*name
= d_name (di
);
1930 return d_make_comp (di
, DEMANGLE_COMPONENT_REFTEMP
, name
,
1931 d_number_component (di
));
1935 return d_make_comp (di
, DEMANGLE_COMPONENT_HIDDEN_ALIAS
,
1936 d_encoding (di
, 0), NULL
);
1939 switch (d_next_char (di
))
1942 return d_make_comp (di
, DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
,
1943 d_encoding (di
, 0), NULL
);
1945 /* ??? The proposal is that other letters (such as 'h') stand
1946 for different variants of transaction cloning, such as
1947 compiling directly for hardware transaction support. But
1948 they still should all be transactional clones of some sort
1949 so go ahead and call them that. */
1951 return d_make_comp (di
, DEMANGLE_COMPONENT_TRANSACTION_CLONE
,
1952 d_encoding (di
, 0), NULL
);
1956 return d_java_resource (di
);
1966 /* <call-offset> ::= h <nv-offset> _
1969 <nv-offset> ::= <(offset) number>
1971 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1973 The C parameter, if not '\0', is a character we just read which is
1974 the start of the <call-offset>.
1976 We don't display the offset information anywhere. FIXME: We should
1977 display it in verbose mode. */
1980 d_call_offset (struct d_info
*di
, int c
)
1983 c
= d_next_char (di
);
1990 if (! d_check_char (di
, '_'))
1997 if (! d_check_char (di
, '_'))
2003 /* <ctor-dtor-name> ::= C1
2011 static struct demangle_component
*
2012 d_ctor_dtor_name (struct d_info
*di
)
2014 if (di
->last_name
!= NULL
)
2016 if (di
->last_name
->type
== DEMANGLE_COMPONENT_NAME
)
2017 di
->expansion
+= di
->last_name
->u
.s_name
.len
;
2018 else if (di
->last_name
->type
== DEMANGLE_COMPONENT_SUB_STD
)
2019 di
->expansion
+= di
->last_name
->u
.s_string
.len
;
2021 switch (d_peek_char (di
))
2025 enum gnu_v3_ctor_kinds kind
;
2027 switch (d_peek_next_char (di
))
2030 kind
= gnu_v3_complete_object_ctor
;
2033 kind
= gnu_v3_base_object_ctor
;
2036 kind
= gnu_v3_complete_object_allocating_ctor
;
2039 kind
= gnu_v3_object_ctor_group
;
2045 return d_make_ctor (di
, kind
, di
->last_name
);
2050 enum gnu_v3_dtor_kinds kind
;
2052 switch (d_peek_next_char (di
))
2055 kind
= gnu_v3_deleting_dtor
;
2058 kind
= gnu_v3_complete_object_dtor
;
2061 kind
= gnu_v3_base_object_dtor
;
2064 kind
= gnu_v3_object_dtor_group
;
2070 return d_make_dtor (di
, kind
, di
->last_name
);
2078 /* <type> ::= <builtin-type>
2080 ::= <class-enum-type>
2082 ::= <pointer-to-member-type>
2083 ::= <template-param>
2084 ::= <template-template-param> <template-args>
2086 ::= <CV-qualifiers> <type>
2089 ::= O <type> (C++0x)
2092 ::= U <source-name> <type>
2094 <builtin-type> ::= various one letter codes
2098 CP_STATIC_IF_GLIBCPP_V3
2099 const struct demangle_builtin_type_info
2100 cplus_demangle_builtin_types
[D_BUILTIN_TYPE_COUNT
] =
2102 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT
},
2103 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL
},
2104 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT
},
2105 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT
},
2106 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT
},
2107 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT
},
2108 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT
},
2109 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT
},
2110 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT
},
2111 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED
},
2112 /* k */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2113 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG
},
2114 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG
},
2115 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT
},
2116 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
2118 /* p */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2119 /* q */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2120 /* r */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2121 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT
},
2122 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT
},
2123 /* u */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2124 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID
},
2125 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT
},
2126 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG
},
2127 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
2128 D_PRINT_UNSIGNED_LONG_LONG
},
2129 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT
},
2130 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT
},
2131 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT
},
2132 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT
},
2133 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT
},
2134 /* 30 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT
},
2135 /* 31 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT
},
2136 /* 32 */ { NL ("decltype(nullptr)"), NL ("decltype(nullptr)"),
2140 CP_STATIC_IF_GLIBCPP_V3
2141 struct demangle_component
*
2142 cplus_demangle_type (struct d_info
*di
)
2145 struct demangle_component
*ret
;
2148 /* The ABI specifies that when CV-qualifiers are used, the base type
2149 is substitutable, and the fully qualified type is substitutable,
2150 but the base type with a strict subset of the CV-qualifiers is
2151 not substitutable. The natural recursive implementation of the
2152 CV-qualifiers would cause subsets to be substitutable, so instead
2153 we pull them all off now.
2155 FIXME: The ABI says that order-insensitive vendor qualifiers
2156 should be handled in the same way, but we have no way to tell
2157 which vendor qualifiers are order-insensitive and which are
2158 order-sensitive. So we just assume that they are all
2159 order-sensitive. g++ 3.4 supports only one vendor qualifier,
2160 __vector, and it treats it as order-sensitive when mangling
2163 peek
= d_peek_char (di
);
2164 if (peek
== 'r' || peek
== 'V' || peek
== 'K')
2166 struct demangle_component
**pret
;
2168 pret
= d_cv_qualifiers (di
, &ret
, 0);
2171 *pret
= cplus_demangle_type (di
);
2172 if (! *pret
|| ! d_add_substitution (di
, ret
))
2181 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2182 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
2183 case 'o': case 's': case 't':
2184 case 'v': case 'w': case 'x': case 'y': case 'z':
2185 ret
= d_make_builtin_type (di
,
2186 &cplus_demangle_builtin_types
[peek
- 'a']);
2187 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2194 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_VENDOR_TYPE
,
2195 d_source_name (di
), NULL
);
2199 ret
= d_function_type (di
);
2202 case '0': case '1': case '2': case '3': case '4':
2203 case '5': case '6': case '7': case '8': case '9':
2206 ret
= d_class_enum_type (di
);
2210 ret
= d_array_type (di
);
2214 ret
= d_pointer_to_member_type (di
);
2218 ret
= d_template_param (di
);
2219 if (d_peek_char (di
) == 'I')
2221 /* This is <template-template-param> <template-args>. The
2222 <template-template-param> part is a substitution
2224 if (! d_add_substitution (di
, ret
))
2226 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
,
2227 d_template_args (di
));
2232 /* If this is a special substitution, then it is the start of
2233 <class-enum-type>. */
2237 peek_next
= d_peek_next_char (di
);
2238 if (IS_DIGIT (peek_next
)
2240 || IS_UPPER (peek_next
))
2242 ret
= d_substitution (di
, 0);
2243 /* The substituted name may have been a template name and
2244 may be followed by tepmlate args. */
2245 if (d_peek_char (di
) == 'I')
2246 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
,
2247 d_template_args (di
));
2253 ret
= d_class_enum_type (di
);
2254 /* If the substitution was a complete type, then it is not
2255 a new substitution candidate. However, if the
2256 substitution was followed by template arguments, then
2257 the whole thing is a substitution candidate. */
2258 if (ret
!= NULL
&& ret
->type
== DEMANGLE_COMPONENT_SUB_STD
)
2266 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_RVALUE_REFERENCE
,
2267 cplus_demangle_type (di
), NULL
);
2272 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_POINTER
,
2273 cplus_demangle_type (di
), NULL
);
2278 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_REFERENCE
,
2279 cplus_demangle_type (di
), NULL
);
2284 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_COMPLEX
,
2285 cplus_demangle_type (di
), NULL
);
2290 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_IMAGINARY
,
2291 cplus_demangle_type (di
), NULL
);
2296 ret
= d_source_name (di
);
2297 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
,
2298 cplus_demangle_type (di
), ret
);
2304 peek
= d_next_char (di
);
2309 /* decltype (expression) */
2310 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_DECLTYPE
,
2311 d_expression (di
), NULL
);
2312 if (ret
&& d_next_char (di
) != 'E')
2318 /* Pack expansion. */
2319 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_PACK_EXPANSION
,
2320 cplus_demangle_type (di
), NULL
);
2326 ret
= d_make_name (di
, "auto", 4);
2330 /* 32-bit decimal floating point */
2331 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[26]);
2332 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2336 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[27]);
2337 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2341 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[28]);
2342 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2345 /* 16-bit half-precision FP */
2346 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[29]);
2347 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2351 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[30]);
2352 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2356 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[31]);
2357 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2361 /* Fixed point types. DF<int bits><length><fract bits><sat> */
2362 ret
= d_make_empty (di
);
2363 ret
->type
= DEMANGLE_COMPONENT_FIXED_TYPE
;
2364 if ((ret
->u
.s_fixed
.accum
= IS_DIGIT (d_peek_char (di
))))
2365 /* For demangling we don't care about the bits. */
2367 ret
->u
.s_fixed
.length
= cplus_demangle_type (di
);
2368 if (ret
->u
.s_fixed
.length
== NULL
)
2371 peek
= d_next_char (di
);
2372 ret
->u
.s_fixed
.sat
= (peek
== 's');
2376 ret
= d_vector_type (di
);
2381 /* decltype(nullptr) */
2382 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[32]);
2383 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2397 if (! d_add_substitution (di
, ret
))
2404 /* <CV-qualifiers> ::= [r] [V] [K] */
2406 static struct demangle_component
**
2407 d_cv_qualifiers (struct d_info
*di
,
2408 struct demangle_component
**pret
, int member_fn
)
2410 struct demangle_component
**pstart
;
2414 peek
= d_peek_char (di
);
2415 while (peek
== 'r' || peek
== 'V' || peek
== 'K')
2417 enum demangle_component_type t
;
2423 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2424 : DEMANGLE_COMPONENT_RESTRICT
);
2425 di
->expansion
+= sizeof "restrict";
2427 else if (peek
== 'V')
2430 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2431 : DEMANGLE_COMPONENT_VOLATILE
);
2432 di
->expansion
+= sizeof "volatile";
2437 ? DEMANGLE_COMPONENT_CONST_THIS
2438 : DEMANGLE_COMPONENT_CONST
);
2439 di
->expansion
+= sizeof "const";
2442 *pret
= d_make_comp (di
, t
, NULL
, NULL
);
2445 pret
= &d_left (*pret
);
2447 peek
= d_peek_char (di
);
2450 if (!member_fn
&& peek
== 'F')
2452 while (pstart
!= pret
)
2454 switch ((*pstart
)->type
)
2456 case DEMANGLE_COMPONENT_RESTRICT
:
2457 (*pstart
)->type
= DEMANGLE_COMPONENT_RESTRICT_THIS
;
2459 case DEMANGLE_COMPONENT_VOLATILE
:
2460 (*pstart
)->type
= DEMANGLE_COMPONENT_VOLATILE_THIS
;
2462 case DEMANGLE_COMPONENT_CONST
:
2463 (*pstart
)->type
= DEMANGLE_COMPONENT_CONST_THIS
;
2468 pstart
= &d_left (*pstart
);
2475 /* <function-type> ::= F [Y] <bare-function-type> E */
2477 static struct demangle_component
*
2478 d_function_type (struct d_info
*di
)
2480 struct demangle_component
*ret
;
2482 if (! d_check_char (di
, 'F'))
2484 if (d_peek_char (di
) == 'Y')
2486 /* Function has C linkage. We don't print this information.
2487 FIXME: We should print it in verbose mode. */
2490 ret
= d_bare_function_type (di
, 1);
2491 if (! d_check_char (di
, 'E'))
2498 static struct demangle_component
*
2499 d_parmlist (struct d_info
*di
)
2501 struct demangle_component
*tl
;
2502 struct demangle_component
**ptl
;
2508 struct demangle_component
*type
;
2510 char peek
= d_peek_char (di
);
2511 if (peek
== '\0' || peek
== 'E' || peek
== '.')
2513 type
= cplus_demangle_type (di
);
2516 *ptl
= d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, type
, NULL
);
2519 ptl
= &d_right (*ptl
);
2522 /* There should be at least one parameter type besides the optional
2523 return type. A function which takes no arguments will have a
2524 single parameter type void. */
2528 /* If we have a single parameter type void, omit it. */
2529 if (d_right (tl
) == NULL
2530 && d_left (tl
)->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
2531 && d_left (tl
)->u
.s_builtin
.type
->print
== D_PRINT_VOID
)
2533 di
->expansion
-= d_left (tl
)->u
.s_builtin
.type
->len
;
2540 /* <bare-function-type> ::= [J]<type>+ */
2542 static struct demangle_component
*
2543 d_bare_function_type (struct d_info
*di
, int has_return_type
)
2545 struct demangle_component
*return_type
;
2546 struct demangle_component
*tl
;
2549 /* Detect special qualifier indicating that the first argument
2550 is the return type. */
2551 peek
= d_peek_char (di
);
2555 has_return_type
= 1;
2558 if (has_return_type
)
2560 return_type
= cplus_demangle_type (di
);
2561 if (return_type
== NULL
)
2567 tl
= d_parmlist (di
);
2571 return d_make_comp (di
, DEMANGLE_COMPONENT_FUNCTION_TYPE
,
2575 /* <class-enum-type> ::= <name> */
2577 static struct demangle_component
*
2578 d_class_enum_type (struct d_info
*di
)
2583 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2584 ::= A [<(dimension) expression>] _ <(element) type>
2587 static struct demangle_component
*
2588 d_array_type (struct d_info
*di
)
2591 struct demangle_component
*dim
;
2593 if (! d_check_char (di
, 'A'))
2596 peek
= d_peek_char (di
);
2599 else if (IS_DIGIT (peek
))
2607 peek
= d_peek_char (di
);
2609 while (IS_DIGIT (peek
));
2610 dim
= d_make_name (di
, s
, d_str (di
) - s
);
2616 dim
= d_expression (di
);
2621 if (! d_check_char (di
, '_'))
2624 return d_make_comp (di
, DEMANGLE_COMPONENT_ARRAY_TYPE
, dim
,
2625 cplus_demangle_type (di
));
2628 /* <vector-type> ::= Dv <number> _ <type>
2629 ::= Dv _ <expression> _ <type> */
2631 static struct demangle_component
*
2632 d_vector_type (struct d_info
*di
)
2635 struct demangle_component
*dim
;
2637 peek
= d_peek_char (di
);
2641 dim
= d_expression (di
);
2644 dim
= d_number_component (di
);
2649 if (! d_check_char (di
, '_'))
2652 return d_make_comp (di
, DEMANGLE_COMPONENT_VECTOR_TYPE
, dim
,
2653 cplus_demangle_type (di
));
2656 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2658 static struct demangle_component
*
2659 d_pointer_to_member_type (struct d_info
*di
)
2661 struct demangle_component
*cl
;
2662 struct demangle_component
*mem
;
2663 struct demangle_component
**pmem
;
2665 if (! d_check_char (di
, 'M'))
2668 cl
= cplus_demangle_type (di
);
2670 /* The ABI specifies that any type can be a substitution source, and
2671 that M is followed by two types, and that when a CV-qualified
2672 type is seen both the base type and the CV-qualified types are
2673 substitution sources. The ABI also specifies that for a pointer
2674 to a CV-qualified member function, the qualifiers are attached to
2675 the second type. Given the grammar, a plain reading of the ABI
2676 suggests that both the CV-qualified member function and the
2677 non-qualified member function are substitution sources. However,
2678 g++ does not work that way. g++ treats only the CV-qualified
2679 member function as a substitution source. FIXME. So to work
2680 with g++, we need to pull off the CV-qualifiers here, in order to
2681 avoid calling add_substitution() in cplus_demangle_type(). But
2682 for a CV-qualified member which is not a function, g++ does
2683 follow the ABI, so we need to handle that case here by calling
2684 d_add_substitution ourselves. */
2686 pmem
= d_cv_qualifiers (di
, &mem
, 1);
2689 *pmem
= cplus_demangle_type (di
);
2693 if (pmem
!= &mem
&& (*pmem
)->type
!= DEMANGLE_COMPONENT_FUNCTION_TYPE
)
2695 if (! d_add_substitution (di
, mem
))
2699 return d_make_comp (di
, DEMANGLE_COMPONENT_PTRMEM_TYPE
, cl
, mem
);
2702 /* <non-negative number> _ */
2705 d_compact_number (struct d_info
*di
)
2708 if (d_peek_char (di
) == '_')
2710 else if (d_peek_char (di
) == 'n')
2713 num
= d_number (di
) + 1;
2715 if (! d_check_char (di
, '_'))
2720 /* <template-param> ::= T_
2721 ::= T <(parameter-2 non-negative) number> _
2724 static struct demangle_component
*
2725 d_template_param (struct d_info
*di
)
2729 if (! d_check_char (di
, 'T'))
2732 param
= d_compact_number (di
);
2738 return d_make_template_param (di
, param
);
2741 /* <template-args> ::= I <template-arg>+ E */
2743 static struct demangle_component
*
2744 d_template_args (struct d_info
*di
)
2746 struct demangle_component
*hold_last_name
;
2747 struct demangle_component
*al
;
2748 struct demangle_component
**pal
;
2750 /* Preserve the last name we saw--don't let the template arguments
2751 clobber it, as that would give us the wrong name for a subsequent
2752 constructor or destructor. */
2753 hold_last_name
= di
->last_name
;
2755 if (d_peek_char (di
) != 'I'
2756 && d_peek_char (di
) != 'J')
2760 if (d_peek_char (di
) == 'E')
2762 /* An argument pack can be empty. */
2764 return d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
, NULL
, NULL
);
2771 struct demangle_component
*a
;
2773 a
= d_template_arg (di
);
2777 *pal
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
, a
, NULL
);
2780 pal
= &d_right (*pal
);
2782 if (d_peek_char (di
) == 'E')
2789 di
->last_name
= hold_last_name
;
2794 /* <template-arg> ::= <type>
2795 ::= X <expression> E
2799 static struct demangle_component
*
2800 d_template_arg (struct d_info
*di
)
2802 struct demangle_component
*ret
;
2804 switch (d_peek_char (di
))
2808 ret
= d_expression (di
);
2809 if (! d_check_char (di
, 'E'))
2814 return d_expr_primary (di
);
2818 /* An argument pack. */
2819 return d_template_args (di
);
2822 return cplus_demangle_type (di
);
2826 /* Parse a sequence of expressions until we hit the terminator
2829 static struct demangle_component
*
2830 d_exprlist (struct d_info
*di
, char terminator
)
2832 struct demangle_component
*list
= NULL
;
2833 struct demangle_component
**p
= &list
;
2835 if (d_peek_char (di
) == terminator
)
2838 return d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, NULL
, NULL
);
2843 struct demangle_component
*arg
= d_expression (di
);
2847 *p
= d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, arg
, NULL
);
2852 if (d_peek_char (di
) == terminator
)
2862 /* Returns nonzero iff OP is an operator for a C++ cast: const_cast,
2863 dynamic_cast, static_cast or reinterpret_cast. */
2866 op_is_new_cast (struct demangle_component
*op
)
2868 const char *code
= op
->u
.s_operator
.op
->code
;
2869 return (code
[1] == 'c'
2870 && (code
[0] == 's' || code
[0] == 'd'
2871 || code
[0] == 'c' || code
[0] == 'r'));
2874 /* <expression> ::= <(unary) operator-name> <expression>
2875 ::= <(binary) operator-name> <expression> <expression>
2876 ::= <(trinary) operator-name> <expression> <expression> <expression>
2877 ::= cl <expression>+ E
2879 ::= <template-param>
2880 ::= sr <type> <unqualified-name>
2881 ::= sr <type> <unqualified-name> <template-args>
2885 static struct demangle_component
*
2886 d_expression (struct d_info
*di
)
2890 peek
= d_peek_char (di
);
2892 return d_expr_primary (di
);
2893 else if (peek
== 'T')
2894 return d_template_param (di
);
2895 else if (peek
== 's' && d_peek_next_char (di
) == 'r')
2897 struct demangle_component
*type
;
2898 struct demangle_component
*name
;
2901 type
= cplus_demangle_type (di
);
2902 name
= d_unqualified_name (di
);
2903 if (d_peek_char (di
) != 'I')
2904 return d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
, type
, name
);
2906 return d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
, type
,
2907 d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, name
,
2908 d_template_args (di
)));
2910 else if (peek
== 's' && d_peek_next_char (di
) == 'p')
2913 return d_make_comp (di
, DEMANGLE_COMPONENT_PACK_EXPANSION
,
2914 d_expression (di
), NULL
);
2916 else if (peek
== 'f' && d_peek_next_char (di
) == 'p')
2918 /* Function parameter used in a late-specified return type. */
2921 if (d_peek_char (di
) == 'T')
2923 /* 'this' parameter. */
2929 index
= d_compact_number (di
) + 1;
2933 return d_make_function_param (di
, index
);
2935 else if (IS_DIGIT (peek
)
2936 || (peek
== 'o' && d_peek_next_char (di
) == 'n'))
2938 /* We can get an unqualified name as an expression in the case of
2939 a dependent function call, i.e. decltype(f(t)). */
2940 struct demangle_component
*name
;
2943 /* operator-function-id, i.e. operator+(t). */
2946 name
= d_unqualified_name (di
);
2949 if (d_peek_char (di
) == 'I')
2950 return d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, name
,
2951 d_template_args (di
));
2955 else if ((peek
== 'i' || peek
== 't')
2956 && d_peek_next_char (di
) == 'l')
2958 /* Brace-enclosed initializer list, untyped or typed. */
2959 struct demangle_component
*type
= NULL
;
2961 type
= cplus_demangle_type (di
);
2963 return d_make_comp (di
, DEMANGLE_COMPONENT_INITIALIZER_LIST
,
2964 type
, d_exprlist (di
, 'E'));
2968 struct demangle_component
*op
;
2969 const char *code
= NULL
;
2972 op
= d_operator_name (di
);
2976 if (op
->type
== DEMANGLE_COMPONENT_OPERATOR
)
2978 code
= op
->u
.s_operator
.op
->code
;
2979 di
->expansion
+= op
->u
.s_operator
.op
->len
- 2;
2980 if (strcmp (code
, "st") == 0)
2981 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
2982 cplus_demangle_type (di
));
2989 case DEMANGLE_COMPONENT_OPERATOR
:
2990 args
= op
->u
.s_operator
.op
->args
;
2992 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
2993 args
= op
->u
.s_extended_operator
.args
;
2995 case DEMANGLE_COMPONENT_CAST
:
3003 return d_make_comp (di
, DEMANGLE_COMPONENT_NULLARY
, op
, NULL
);
3007 struct demangle_component
*operand
;
3010 if (code
&& (code
[0] == 'p' || code
[0] == 'm')
3011 && code
[1] == code
[0])
3012 /* pp_ and mm_ are the prefix variants. */
3013 suffix
= !d_check_char (di
, '_');
3015 if (op
->type
== DEMANGLE_COMPONENT_CAST
3016 && d_check_char (di
, '_'))
3017 operand
= d_exprlist (di
, 'E');
3019 operand
= d_expression (di
);
3022 /* Indicate the suffix variant for d_print_comp. */
3023 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
3025 DEMANGLE_COMPONENT_BINARY_ARGS
,
3028 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
3033 struct demangle_component
*left
;
3034 struct demangle_component
*right
;
3036 if (op_is_new_cast (op
))
3037 left
= cplus_demangle_type (di
);
3039 left
= d_expression (di
);
3040 if (!strcmp (code
, "cl"))
3041 right
= d_exprlist (di
, 'E');
3042 else if (!strcmp (code
, "dt") || !strcmp (code
, "pt"))
3044 right
= d_unqualified_name (di
);
3045 if (d_peek_char (di
) == 'I')
3046 right
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
,
3047 right
, d_template_args (di
));
3050 right
= d_expression (di
);
3052 return d_make_comp (di
, DEMANGLE_COMPONENT_BINARY
, op
,
3054 DEMANGLE_COMPONENT_BINARY_ARGS
,
3059 struct demangle_component
*first
;
3060 struct demangle_component
*second
;
3061 struct demangle_component
*third
;
3063 if (!strcmp (code
, "qu"))
3065 /* ?: expression. */
3066 first
= d_expression (di
);
3067 second
= d_expression (di
);
3068 third
= d_expression (di
);
3070 else if (code
[0] == 'n')
3072 /* new-expression. */
3073 if (code
[1] != 'w' && code
[1] != 'a')
3075 first
= d_exprlist (di
, '_');
3076 second
= cplus_demangle_type (di
);
3077 if (d_peek_char (di
) == 'E')
3082 else if (d_peek_char (di
) == 'p'
3083 && d_peek_next_char (di
) == 'i')
3085 /* Parenthesized initializer. */
3087 third
= d_exprlist (di
, 'E');
3089 else if (d_peek_char (di
) == 'i'
3090 && d_peek_next_char (di
) == 'l')
3091 /* initializer-list. */
3092 third
= d_expression (di
);
3098 return d_make_comp (di
, DEMANGLE_COMPONENT_TRINARY
, op
,
3100 DEMANGLE_COMPONENT_TRINARY_ARG1
,
3103 DEMANGLE_COMPONENT_TRINARY_ARG2
,
3112 /* <expr-primary> ::= L <type> <(value) number> E
3113 ::= L <type> <(value) float> E
3114 ::= L <mangled-name> E
3117 static struct demangle_component
*
3118 d_expr_primary (struct d_info
*di
)
3120 struct demangle_component
*ret
;
3122 if (! d_check_char (di
, 'L'))
3124 if (d_peek_char (di
) == '_'
3125 /* Workaround for G++ bug; see comment in write_template_arg. */
3126 || d_peek_char (di
) == 'Z')
3127 ret
= cplus_demangle_mangled_name (di
, 0);
3130 struct demangle_component
*type
;
3131 enum demangle_component_type t
;
3134 type
= cplus_demangle_type (di
);
3138 /* If we have a type we know how to print, we aren't going to
3139 print the type name itself. */
3140 if (type
->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
3141 && type
->u
.s_builtin
.type
->print
!= D_PRINT_DEFAULT
)
3142 di
->expansion
-= type
->u
.s_builtin
.type
->len
;
3144 /* Rather than try to interpret the literal value, we just
3145 collect it as a string. Note that it's possible to have a
3146 floating point literal here. The ABI specifies that the
3147 format of such literals is machine independent. That's fine,
3148 but what's not fine is that versions of g++ up to 3.2 with
3149 -fabi-version=1 used upper case letters in the hex constant,
3150 and dumped out gcc's internal representation. That makes it
3151 hard to tell where the constant ends, and hard to dump the
3152 constant in any readable form anyhow. We don't attempt to
3153 handle these cases. */
3155 t
= DEMANGLE_COMPONENT_LITERAL
;
3156 if (d_peek_char (di
) == 'n')
3158 t
= DEMANGLE_COMPONENT_LITERAL_NEG
;
3162 while (d_peek_char (di
) != 'E')
3164 if (d_peek_char (di
) == '\0')
3168 ret
= d_make_comp (di
, t
, type
, d_make_name (di
, s
, d_str (di
) - s
));
3170 if (! d_check_char (di
, 'E'))
3175 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
3176 ::= Z <(function) encoding> E s [<discriminator>]
3179 static struct demangle_component
*
3180 d_local_name (struct d_info
*di
)
3182 struct demangle_component
*function
;
3184 if (! d_check_char (di
, 'Z'))
3187 function
= d_encoding (di
, 0);
3189 if (! d_check_char (di
, 'E'))
3192 if (d_peek_char (di
) == 's')
3195 if (! d_discriminator (di
))
3197 return d_make_comp (di
, DEMANGLE_COMPONENT_LOCAL_NAME
, function
,
3198 d_make_name (di
, "string literal",
3199 sizeof "string literal" - 1));
3203 struct demangle_component
*name
;
3206 if (d_peek_char (di
) == 'd')
3208 /* Default argument scope: d <number> _. */
3210 num
= d_compact_number (di
);
3219 /* Lambdas and unnamed types have internal discriminators. */
3220 case DEMANGLE_COMPONENT_LAMBDA
:
3221 case DEMANGLE_COMPONENT_UNNAMED_TYPE
:
3224 if (! d_discriminator (di
))
3228 name
= d_make_default_arg (di
, num
, name
);
3229 return d_make_comp (di
, DEMANGLE_COMPONENT_LOCAL_NAME
, function
, name
);
3233 /* <discriminator> ::= _ <(non-negative) number>
3235 We demangle the discriminator, but we don't print it out. FIXME:
3236 We should print it out in verbose mode. */
3239 d_discriminator (struct d_info
*di
)
3243 if (d_peek_char (di
) != '_')
3246 discrim
= d_number (di
);
3252 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ */
3254 static struct demangle_component
*
3255 d_lambda (struct d_info
*di
)
3257 struct demangle_component
*tl
;
3258 struct demangle_component
*ret
;
3261 if (! d_check_char (di
, 'U'))
3263 if (! d_check_char (di
, 'l'))
3266 tl
= d_parmlist (di
);
3270 if (! d_check_char (di
, 'E'))
3273 num
= d_compact_number (di
);
3277 ret
= d_make_empty (di
);
3280 ret
->type
= DEMANGLE_COMPONENT_LAMBDA
;
3281 ret
->u
.s_unary_num
.sub
= tl
;
3282 ret
->u
.s_unary_num
.num
= num
;
3285 if (! d_add_substitution (di
, ret
))
3291 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
3293 static struct demangle_component
*
3294 d_unnamed_type (struct d_info
*di
)
3296 struct demangle_component
*ret
;
3299 if (! d_check_char (di
, 'U'))
3301 if (! d_check_char (di
, 't'))
3304 num
= d_compact_number (di
);
3308 ret
= d_make_empty (di
);
3311 ret
->type
= DEMANGLE_COMPONENT_UNNAMED_TYPE
;
3312 ret
->u
.s_number
.number
= num
;
3315 if (! d_add_substitution (di
, ret
))
3321 /* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]*
3324 static struct demangle_component
*
3325 d_clone_suffix (struct d_info
*di
, struct demangle_component
*encoding
)
3327 const char *suffix
= d_str (di
);
3328 const char *pend
= suffix
;
3329 struct demangle_component
*n
;
3331 if (*pend
== '.' && (IS_LOWER (pend
[1]) || pend
[1] == '_'))
3334 while (IS_LOWER (*pend
) || *pend
== '_')
3337 while (*pend
== '.' && IS_DIGIT (pend
[1]))
3340 while (IS_DIGIT (*pend
))
3343 d_advance (di
, pend
- suffix
);
3344 n
= d_make_name (di
, suffix
, pend
- suffix
);
3345 return d_make_comp (di
, DEMANGLE_COMPONENT_CLONE
, encoding
, n
);
3348 /* Add a new substitution. */
3351 d_add_substitution (struct d_info
*di
, struct demangle_component
*dc
)
3355 if (di
->next_sub
>= di
->num_subs
)
3357 di
->subs
[di
->next_sub
] = dc
;
3362 /* <substitution> ::= S <seq-id> _
3372 If PREFIX is non-zero, then this type is being used as a prefix in
3373 a qualified name. In this case, for the standard substitutions, we
3374 need to check whether we are being used as a prefix for a
3375 constructor or destructor, and return a full template name.
3376 Otherwise we will get something like std::iostream::~iostream()
3377 which does not correspond particularly well to any function which
3378 actually appears in the source.
3381 static const struct d_standard_sub_info standard_subs
[] =
3386 { 'a', NL ("std::allocator"),
3387 NL ("std::allocator"),
3389 { 'b', NL ("std::basic_string"),
3390 NL ("std::basic_string"),
3391 NL ("basic_string") },
3392 { 's', NL ("std::string"),
3393 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
3394 NL ("basic_string") },
3395 { 'i', NL ("std::istream"),
3396 NL ("std::basic_istream<char, std::char_traits<char> >"),
3397 NL ("basic_istream") },
3398 { 'o', NL ("std::ostream"),
3399 NL ("std::basic_ostream<char, std::char_traits<char> >"),
3400 NL ("basic_ostream") },
3401 { 'd', NL ("std::iostream"),
3402 NL ("std::basic_iostream<char, std::char_traits<char> >"),
3403 NL ("basic_iostream") }
3406 static struct demangle_component
*
3407 d_substitution (struct d_info
*di
, int prefix
)
3411 if (! d_check_char (di
, 'S'))
3414 c
= d_next_char (di
);
3415 if (c
== '_' || IS_DIGIT (c
) || IS_UPPER (c
))
3424 unsigned int new_id
;
3427 new_id
= id
* 36 + c
- '0';
3428 else if (IS_UPPER (c
))
3429 new_id
= id
* 36 + c
- 'A' + 10;
3435 c
= d_next_char (di
);
3442 if (id
>= (unsigned int) di
->next_sub
)
3447 return di
->subs
[id
];
3452 const struct d_standard_sub_info
*p
;
3453 const struct d_standard_sub_info
*pend
;
3455 verbose
= (di
->options
& DMGL_VERBOSE
) != 0;
3456 if (! verbose
&& prefix
)
3460 peek
= d_peek_char (di
);
3461 if (peek
== 'C' || peek
== 'D')
3465 pend
= (&standard_subs
[0]
3466 + sizeof standard_subs
/ sizeof standard_subs
[0]);
3467 for (p
= &standard_subs
[0]; p
< pend
; ++p
)
3474 if (p
->set_last_name
!= NULL
)
3475 di
->last_name
= d_make_sub (di
, p
->set_last_name
,
3476 p
->set_last_name_len
);
3479 s
= p
->full_expansion
;
3484 s
= p
->simple_expansion
;
3485 len
= p
->simple_len
;
3487 di
->expansion
+= len
;
3488 return d_make_sub (di
, s
, len
);
3496 /* Initialize a growable string. */
3499 d_growable_string_init (struct d_growable_string
*dgs
, size_t estimate
)
3504 dgs
->allocation_failure
= 0;
3507 d_growable_string_resize (dgs
, estimate
);
3510 /* Grow a growable string to a given size. */
3513 d_growable_string_resize (struct d_growable_string
*dgs
, size_t need
)
3518 if (dgs
->allocation_failure
)
3521 /* Start allocation at two bytes to avoid any possibility of confusion
3522 with the special value of 1 used as a return in *palc to indicate
3523 allocation failures. */
3524 newalc
= dgs
->alc
> 0 ? dgs
->alc
: 2;
3525 while (newalc
< need
)
3528 newbuf
= (char *) realloc (dgs
->buf
, newalc
);
3535 dgs
->allocation_failure
= 1;
3542 /* Append a buffer to a growable string. */
3545 d_growable_string_append_buffer (struct d_growable_string
*dgs
,
3546 const char *s
, size_t l
)
3550 need
= dgs
->len
+ l
+ 1;
3551 if (need
> dgs
->alc
)
3552 d_growable_string_resize (dgs
, need
);
3554 if (dgs
->allocation_failure
)
3557 memcpy (dgs
->buf
+ dgs
->len
, s
, l
);
3558 dgs
->buf
[dgs
->len
+ l
] = '\0';
3562 /* Bridge growable strings to the callback mechanism. */
3565 d_growable_string_callback_adapter (const char *s
, size_t l
, void *opaque
)
3567 struct d_growable_string
*dgs
= (struct d_growable_string
*) opaque
;
3569 d_growable_string_append_buffer (dgs
, s
, l
);
3572 /* Initialize a print information structure. */
3575 d_print_init (struct d_print_info
*dpi
, demangle_callbackref callback
,
3579 dpi
->last_char
= '\0';
3580 dpi
->templates
= NULL
;
3581 dpi
->modifiers
= NULL
;
3582 dpi
->pack_index
= 0;
3583 dpi
->flush_count
= 0;
3585 dpi
->callback
= callback
;
3586 dpi
->opaque
= opaque
;
3588 dpi
->demangle_failure
= 0;
3591 /* Indicate that an error occurred during printing, and test for error. */
3594 d_print_error (struct d_print_info
*dpi
)
3596 dpi
->demangle_failure
= 1;
3600 d_print_saw_error (struct d_print_info
*dpi
)
3602 return dpi
->demangle_failure
!= 0;
3605 /* Flush buffered characters to the callback. */
3608 d_print_flush (struct d_print_info
*dpi
)
3610 dpi
->buf
[dpi
->len
] = '\0';
3611 dpi
->callback (dpi
->buf
, dpi
->len
, dpi
->opaque
);
3616 /* Append characters and buffers for printing. */
3619 d_append_char (struct d_print_info
*dpi
, char c
)
3621 if (dpi
->len
== sizeof (dpi
->buf
) - 1)
3622 d_print_flush (dpi
);
3624 dpi
->buf
[dpi
->len
++] = c
;
3629 d_append_buffer (struct d_print_info
*dpi
, const char *s
, size_t l
)
3633 for (i
= 0; i
< l
; i
++)
3634 d_append_char (dpi
, s
[i
]);
3638 d_append_string (struct d_print_info
*dpi
, const char *s
)
3640 d_append_buffer (dpi
, s
, strlen (s
));
3644 d_append_num (struct d_print_info
*dpi
, long l
)
3647 sprintf (buf
,"%ld", l
);
3648 d_append_string (dpi
, buf
);
3652 d_last_char (struct d_print_info
*dpi
)
3654 return dpi
->last_char
;
3657 /* Turn components into a human readable string. OPTIONS is the
3658 options bits passed to the demangler. DC is the tree to print.
3659 CALLBACK is a function to call to flush demangled string segments
3660 as they fill the intermediate buffer, and OPAQUE is a generalized
3661 callback argument. On success, this returns 1. On failure,
3662 it returns 0, indicating a bad parse. It does not use heap
3663 memory to build an output string, so cannot encounter memory
3664 allocation failure. */
3666 CP_STATIC_IF_GLIBCPP_V3
3668 cplus_demangle_print_callback (int options
,
3669 const struct demangle_component
*dc
,
3670 demangle_callbackref callback
, void *opaque
)
3672 struct d_print_info dpi
;
3674 d_print_init (&dpi
, callback
, opaque
);
3676 d_print_comp (&dpi
, options
, dc
);
3678 d_print_flush (&dpi
);
3680 return ! d_print_saw_error (&dpi
);
3683 /* Turn components into a human readable string. OPTIONS is the
3684 options bits passed to the demangler. DC is the tree to print.
3685 ESTIMATE is a guess at the length of the result. This returns a
3686 string allocated by malloc, or NULL on error. On success, this
3687 sets *PALC to the size of the allocated buffer. On failure, this
3688 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
3691 CP_STATIC_IF_GLIBCPP_V3
3693 cplus_demangle_print (int options
, const struct demangle_component
*dc
,
3694 int estimate
, size_t *palc
)
3696 struct d_growable_string dgs
;
3698 d_growable_string_init (&dgs
, estimate
);
3700 if (! cplus_demangle_print_callback (options
, dc
,
3701 d_growable_string_callback_adapter
,
3709 *palc
= dgs
.allocation_failure
? 1 : dgs
.alc
;
3713 /* Returns the I'th element of the template arglist ARGS, or NULL on
3716 static struct demangle_component
*
3717 d_index_template_argument (struct demangle_component
*args
, int i
)
3719 struct demangle_component
*a
;
3725 if (a
->type
!= DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
3731 if (i
!= 0 || a
== NULL
)
3737 /* Returns the template argument from the current context indicated by DC,
3738 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
3740 static struct demangle_component
*
3741 d_lookup_template_argument (struct d_print_info
*dpi
,
3742 const struct demangle_component
*dc
)
3744 if (dpi
->templates
== NULL
)
3746 d_print_error (dpi
);
3750 return d_index_template_argument
3751 (d_right (dpi
->templates
->template_decl
),
3752 dc
->u
.s_number
.number
);
3755 /* Returns a template argument pack used in DC (any will do), or NULL. */
3757 static struct demangle_component
*
3758 d_find_pack (struct d_print_info
*dpi
,
3759 const struct demangle_component
*dc
)
3761 struct demangle_component
*a
;
3767 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
3768 a
= d_lookup_template_argument (dpi
, dc
);
3769 if (a
&& a
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
3773 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
3776 case DEMANGLE_COMPONENT_LAMBDA
:
3777 case DEMANGLE_COMPONENT_NAME
:
3778 case DEMANGLE_COMPONENT_TAGGED_NAME
:
3779 case DEMANGLE_COMPONENT_OPERATOR
:
3780 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
3781 case DEMANGLE_COMPONENT_SUB_STD
:
3782 case DEMANGLE_COMPONENT_CHARACTER
:
3783 case DEMANGLE_COMPONENT_FUNCTION_PARAM
:
3784 case DEMANGLE_COMPONENT_UNNAMED_TYPE
:
3787 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
3788 return d_find_pack (dpi
, dc
->u
.s_extended_operator
.name
);
3789 case DEMANGLE_COMPONENT_CTOR
:
3790 return d_find_pack (dpi
, dc
->u
.s_ctor
.name
);
3791 case DEMANGLE_COMPONENT_DTOR
:
3792 return d_find_pack (dpi
, dc
->u
.s_dtor
.name
);
3795 a
= d_find_pack (dpi
, d_left (dc
));
3798 return d_find_pack (dpi
, d_right (dc
));
3802 /* Returns the length of the template argument pack DC. */
3805 d_pack_length (const struct demangle_component
*dc
)
3808 while (dc
&& dc
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
3809 && d_left (dc
) != NULL
)
3817 /* DC is a component of a mangled expression. Print it, wrapped in parens
3821 d_print_subexpr (struct d_print_info
*dpi
, int options
,
3822 const struct demangle_component
*dc
)
3825 if (dc
->type
== DEMANGLE_COMPONENT_NAME
3826 || dc
->type
== DEMANGLE_COMPONENT_QUAL_NAME
3827 || dc
->type
== DEMANGLE_COMPONENT_INITIALIZER_LIST
3828 || dc
->type
== DEMANGLE_COMPONENT_FUNCTION_PARAM
)
3831 d_append_char (dpi
, '(');
3832 d_print_comp (dpi
, options
, dc
);
3834 d_append_char (dpi
, ')');
3837 /* Subroutine to handle components. */
3840 d_print_comp (struct d_print_info
*dpi
, int options
,
3841 const struct demangle_component
*dc
)
3843 /* Magic variable to let reference smashing skip over the next modifier
3844 without needing to modify *dc. */
3845 const struct demangle_component
*mod_inner
= NULL
;
3849 d_print_error (dpi
);
3852 if (d_print_saw_error (dpi
))
3857 case DEMANGLE_COMPONENT_NAME
:
3858 if ((options
& DMGL_JAVA
) == 0)
3859 d_append_buffer (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
3861 d_print_java_identifier (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
3864 case DEMANGLE_COMPONENT_TAGGED_NAME
:
3865 d_print_comp (dpi
, options
, d_left (dc
));
3866 d_append_string (dpi
, "[abi:");
3867 d_print_comp (dpi
, options
, d_right (dc
));
3868 d_append_char (dpi
, ']');
3871 case DEMANGLE_COMPONENT_QUAL_NAME
:
3872 case DEMANGLE_COMPONENT_LOCAL_NAME
:
3873 d_print_comp (dpi
, options
, d_left (dc
));
3874 if ((options
& DMGL_JAVA
) == 0)
3875 d_append_string (dpi
, "::");
3877 d_append_char (dpi
, '.');
3878 d_print_comp (dpi
, options
, d_right (dc
));
3881 case DEMANGLE_COMPONENT_TYPED_NAME
:
3883 struct d_print_mod
*hold_modifiers
;
3884 struct demangle_component
*typed_name
;
3885 struct d_print_mod adpm
[4];
3887 struct d_print_template dpt
;
3889 /* Pass the name down to the type so that it can be printed in
3890 the right place for the type. We also have to pass down
3891 any CV-qualifiers, which apply to the this parameter. */
3892 hold_modifiers
= dpi
->modifiers
;
3895 typed_name
= d_left (dc
);
3896 while (typed_name
!= NULL
)
3898 if (i
>= sizeof adpm
/ sizeof adpm
[0])
3900 d_print_error (dpi
);
3904 adpm
[i
].next
= dpi
->modifiers
;
3905 dpi
->modifiers
= &adpm
[i
];
3906 adpm
[i
].mod
= typed_name
;
3907 adpm
[i
].printed
= 0;
3908 adpm
[i
].templates
= dpi
->templates
;
3911 if (typed_name
->type
!= DEMANGLE_COMPONENT_RESTRICT_THIS
3912 && typed_name
->type
!= DEMANGLE_COMPONENT_VOLATILE_THIS
3913 && typed_name
->type
!= DEMANGLE_COMPONENT_CONST_THIS
)
3916 typed_name
= d_left (typed_name
);
3919 if (typed_name
== NULL
)
3921 d_print_error (dpi
);
3925 /* If typed_name is a template, then it applies to the
3926 function type as well. */
3927 if (typed_name
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
3929 dpt
.next
= dpi
->templates
;
3930 dpi
->templates
= &dpt
;
3931 dpt
.template_decl
= typed_name
;
3934 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
3935 there may be CV-qualifiers on its right argument which
3936 really apply here; this happens when parsing a class which
3937 is local to a function. */
3938 if (typed_name
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
3940 struct demangle_component
*local_name
;
3942 local_name
= d_right (typed_name
);
3943 if (local_name
->type
== DEMANGLE_COMPONENT_DEFAULT_ARG
)
3944 local_name
= local_name
->u
.s_unary_num
.sub
;
3945 while (local_name
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
3946 || local_name
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
3947 || local_name
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
3949 if (i
>= sizeof adpm
/ sizeof adpm
[0])
3951 d_print_error (dpi
);
3955 adpm
[i
] = adpm
[i
- 1];
3956 adpm
[i
].next
= &adpm
[i
- 1];
3957 dpi
->modifiers
= &adpm
[i
];
3959 adpm
[i
- 1].mod
= local_name
;
3960 adpm
[i
- 1].printed
= 0;
3961 adpm
[i
- 1].templates
= dpi
->templates
;
3964 local_name
= d_left (local_name
);
3968 d_print_comp (dpi
, options
, d_right (dc
));
3970 if (typed_name
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
3971 dpi
->templates
= dpt
.next
;
3973 /* If the modifiers didn't get printed by the type, print them
3978 if (! adpm
[i
].printed
)
3980 d_append_char (dpi
, ' ');
3981 d_print_mod (dpi
, options
, adpm
[i
].mod
);
3985 dpi
->modifiers
= hold_modifiers
;
3990 case DEMANGLE_COMPONENT_TEMPLATE
:
3992 struct d_print_mod
*hold_dpm
;
3993 struct demangle_component
*dcl
;
3995 /* Don't push modifiers into a template definition. Doing so
3996 could give the wrong definition for a template argument.
3997 Instead, treat the template essentially as a name. */
3999 hold_dpm
= dpi
->modifiers
;
4000 dpi
->modifiers
= NULL
;
4004 if ((options
& DMGL_JAVA
) != 0
4005 && dcl
->type
== DEMANGLE_COMPONENT_NAME
4006 && dcl
->u
.s_name
.len
== 6
4007 && strncmp (dcl
->u
.s_name
.s
, "JArray", 6) == 0)
4009 /* Special-case Java arrays, so that JArray<TYPE> appears
4010 instead as TYPE[]. */
4012 d_print_comp (dpi
, options
, d_right (dc
));
4013 d_append_string (dpi
, "[]");
4017 d_print_comp (dpi
, options
, dcl
);
4018 if (d_last_char (dpi
) == '<')
4019 d_append_char (dpi
, ' ');
4020 d_append_char (dpi
, '<');
4021 d_print_comp (dpi
, options
, d_right (dc
));
4022 /* Avoid generating two consecutive '>' characters, to avoid
4023 the C++ syntactic ambiguity. */
4024 if (d_last_char (dpi
) == '>')
4025 d_append_char (dpi
, ' ');
4026 d_append_char (dpi
, '>');
4029 dpi
->modifiers
= hold_dpm
;
4034 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
4036 struct d_print_template
*hold_dpt
;
4037 struct demangle_component
*a
= d_lookup_template_argument (dpi
, dc
);
4039 if (a
&& a
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
4040 a
= d_index_template_argument (a
, dpi
->pack_index
);
4044 d_print_error (dpi
);
4048 /* While processing this parameter, we need to pop the list of
4049 templates. This is because the template parameter may
4050 itself be a reference to a parameter of an outer
4053 hold_dpt
= dpi
->templates
;
4054 dpi
->templates
= hold_dpt
->next
;
4056 d_print_comp (dpi
, options
, a
);
4058 dpi
->templates
= hold_dpt
;
4063 case DEMANGLE_COMPONENT_CTOR
:
4064 d_print_comp (dpi
, options
, dc
->u
.s_ctor
.name
);
4067 case DEMANGLE_COMPONENT_DTOR
:
4068 d_append_char (dpi
, '~');
4069 d_print_comp (dpi
, options
, dc
->u
.s_dtor
.name
);
4072 case DEMANGLE_COMPONENT_VTABLE
:
4073 d_append_string (dpi
, "vtable for ");
4074 d_print_comp (dpi
, options
, d_left (dc
));
4077 case DEMANGLE_COMPONENT_VTT
:
4078 d_append_string (dpi
, "VTT for ");
4079 d_print_comp (dpi
, options
, d_left (dc
));
4082 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
4083 d_append_string (dpi
, "construction vtable for ");
4084 d_print_comp (dpi
, options
, d_left (dc
));
4085 d_append_string (dpi
, "-in-");
4086 d_print_comp (dpi
, options
, d_right (dc
));
4089 case DEMANGLE_COMPONENT_TYPEINFO
:
4090 d_append_string (dpi
, "typeinfo for ");
4091 d_print_comp (dpi
, options
, d_left (dc
));
4094 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
4095 d_append_string (dpi
, "typeinfo name for ");
4096 d_print_comp (dpi
, options
, d_left (dc
));
4099 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
4100 d_append_string (dpi
, "typeinfo fn for ");
4101 d_print_comp (dpi
, options
, d_left (dc
));
4104 case DEMANGLE_COMPONENT_THUNK
:
4105 d_append_string (dpi
, "non-virtual thunk to ");
4106 d_print_comp (dpi
, options
, d_left (dc
));
4109 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
4110 d_append_string (dpi
, "virtual thunk to ");
4111 d_print_comp (dpi
, options
, d_left (dc
));
4114 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
4115 d_append_string (dpi
, "covariant return thunk to ");
4116 d_print_comp (dpi
, options
, d_left (dc
));
4119 case DEMANGLE_COMPONENT_JAVA_CLASS
:
4120 d_append_string (dpi
, "java Class for ");
4121 d_print_comp (dpi
, options
, d_left (dc
));
4124 case DEMANGLE_COMPONENT_GUARD
:
4125 d_append_string (dpi
, "guard variable for ");
4126 d_print_comp (dpi
, options
, d_left (dc
));
4129 case DEMANGLE_COMPONENT_TLS_INIT
:
4130 d_append_string (dpi
, "TLS init function for ");
4131 d_print_comp (dpi
, options
, d_left (dc
));
4134 case DEMANGLE_COMPONENT_TLS_WRAPPER
:
4135 d_append_string (dpi
, "TLS wrapper function for ");
4136 d_print_comp (dpi
, options
, d_left (dc
));
4139 case DEMANGLE_COMPONENT_REFTEMP
:
4140 d_append_string (dpi
, "reference temporary #");
4141 d_print_comp (dpi
, options
, d_right (dc
));
4142 d_append_string (dpi
, " for ");
4143 d_print_comp (dpi
, options
, d_left (dc
));
4146 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
4147 d_append_string (dpi
, "hidden alias for ");
4148 d_print_comp (dpi
, options
, d_left (dc
));
4151 case DEMANGLE_COMPONENT_TRANSACTION_CLONE
:
4152 d_append_string (dpi
, "transaction clone for ");
4153 d_print_comp (dpi
, options
, d_left (dc
));
4156 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
:
4157 d_append_string (dpi
, "non-transaction clone for ");
4158 d_print_comp (dpi
, options
, d_left (dc
));
4161 case DEMANGLE_COMPONENT_SUB_STD
:
4162 d_append_buffer (dpi
, dc
->u
.s_string
.string
, dc
->u
.s_string
.len
);
4165 case DEMANGLE_COMPONENT_RESTRICT
:
4166 case DEMANGLE_COMPONENT_VOLATILE
:
4167 case DEMANGLE_COMPONENT_CONST
:
4169 struct d_print_mod
*pdpm
;
4171 /* When printing arrays, it's possible to have cases where the
4172 same CV-qualifier gets pushed on the stack multiple times.
4173 We only need to print it once. */
4175 for (pdpm
= dpi
->modifiers
; pdpm
!= NULL
; pdpm
= pdpm
->next
)
4177 if (! pdpm
->printed
)
4179 if (pdpm
->mod
->type
!= DEMANGLE_COMPONENT_RESTRICT
4180 && pdpm
->mod
->type
!= DEMANGLE_COMPONENT_VOLATILE
4181 && pdpm
->mod
->type
!= DEMANGLE_COMPONENT_CONST
)
4183 if (pdpm
->mod
->type
== dc
->type
)
4185 d_print_comp (dpi
, options
, d_left (dc
));
4193 case DEMANGLE_COMPONENT_REFERENCE
:
4194 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
4196 /* Handle reference smashing: & + && = &. */
4197 const struct demangle_component
*sub
= d_left (dc
);
4198 if (sub
->type
== DEMANGLE_COMPONENT_TEMPLATE_PARAM
)
4200 struct demangle_component
*a
= d_lookup_template_argument (dpi
, sub
);
4201 if (a
&& a
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
4202 a
= d_index_template_argument (a
, dpi
->pack_index
);
4206 d_print_error (dpi
);
4213 if (sub
->type
== DEMANGLE_COMPONENT_REFERENCE
4214 || sub
->type
== dc
->type
)
4216 else if (sub
->type
== DEMANGLE_COMPONENT_RVALUE_REFERENCE
)
4217 mod_inner
= d_left (sub
);
4221 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
4222 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
4223 case DEMANGLE_COMPONENT_CONST_THIS
:
4224 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
4225 case DEMANGLE_COMPONENT_POINTER
:
4226 case DEMANGLE_COMPONENT_COMPLEX
:
4227 case DEMANGLE_COMPONENT_IMAGINARY
:
4230 /* We keep a list of modifiers on the stack. */
4231 struct d_print_mod dpm
;
4233 dpm
.next
= dpi
->modifiers
;
4234 dpi
->modifiers
= &dpm
;
4237 dpm
.templates
= dpi
->templates
;
4240 mod_inner
= d_left (dc
);
4242 d_print_comp (dpi
, options
, mod_inner
);
4244 /* If the modifier didn't get printed by the type, print it
4247 d_print_mod (dpi
, options
, dc
);
4249 dpi
->modifiers
= dpm
.next
;
4254 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
4255 if ((options
& DMGL_JAVA
) == 0)
4256 d_append_buffer (dpi
, dc
->u
.s_builtin
.type
->name
,
4257 dc
->u
.s_builtin
.type
->len
);
4259 d_append_buffer (dpi
, dc
->u
.s_builtin
.type
->java_name
,
4260 dc
->u
.s_builtin
.type
->java_len
);
4263 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
4264 d_print_comp (dpi
, options
, d_left (dc
));
4267 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
4269 if ((options
& DMGL_RET_POSTFIX
) != 0)
4270 d_print_function_type (dpi
,
4271 options
& ~(DMGL_RET_POSTFIX
| DMGL_RET_DROP
),
4272 dc
, dpi
->modifiers
);
4274 /* Print return type if present */
4275 if (d_left (dc
) != NULL
&& (options
& DMGL_RET_POSTFIX
) != 0)
4276 d_print_comp (dpi
, options
& ~(DMGL_RET_POSTFIX
| DMGL_RET_DROP
),
4278 else if (d_left (dc
) != NULL
&& (options
& DMGL_RET_DROP
) == 0)
4280 struct d_print_mod dpm
;
4282 /* We must pass this type down as a modifier in order to
4283 print it in the right location. */
4284 dpm
.next
= dpi
->modifiers
;
4285 dpi
->modifiers
= &dpm
;
4288 dpm
.templates
= dpi
->templates
;
4290 d_print_comp (dpi
, options
& ~(DMGL_RET_POSTFIX
| DMGL_RET_DROP
),
4293 dpi
->modifiers
= dpm
.next
;
4298 /* In standard prefix notation, there is a space between the
4299 return type and the function signature. */
4300 if ((options
& DMGL_RET_POSTFIX
) == 0)
4301 d_append_char (dpi
, ' ');
4304 if ((options
& DMGL_RET_POSTFIX
) == 0)
4305 d_print_function_type (dpi
,
4306 options
& ~(DMGL_RET_POSTFIX
| DMGL_RET_DROP
),
4307 dc
, dpi
->modifiers
);
4312 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
4314 struct d_print_mod
*hold_modifiers
;
4315 struct d_print_mod adpm
[4];
4317 struct d_print_mod
*pdpm
;
4319 /* We must pass this type down as a modifier in order to print
4320 multi-dimensional arrays correctly. If the array itself is
4321 CV-qualified, we act as though the element type were
4322 CV-qualified. We do this by copying the modifiers down
4323 rather than fiddling pointers, so that we don't wind up
4324 with a d_print_mod higher on the stack pointing into our
4325 stack frame after we return. */
4327 hold_modifiers
= dpi
->modifiers
;
4329 adpm
[0].next
= hold_modifiers
;
4330 dpi
->modifiers
= &adpm
[0];
4332 adpm
[0].printed
= 0;
4333 adpm
[0].templates
= dpi
->templates
;
4336 pdpm
= hold_modifiers
;
4338 && (pdpm
->mod
->type
== DEMANGLE_COMPONENT_RESTRICT
4339 || pdpm
->mod
->type
== DEMANGLE_COMPONENT_VOLATILE
4340 || pdpm
->mod
->type
== DEMANGLE_COMPONENT_CONST
))
4342 if (! pdpm
->printed
)
4344 if (i
>= sizeof adpm
/ sizeof adpm
[0])
4346 d_print_error (dpi
);
4351 adpm
[i
].next
= dpi
->modifiers
;
4352 dpi
->modifiers
= &adpm
[i
];
4360 d_print_comp (dpi
, options
, d_right (dc
));
4362 dpi
->modifiers
= hold_modifiers
;
4364 if (adpm
[0].printed
)
4370 d_print_mod (dpi
, options
, adpm
[i
].mod
);
4373 d_print_array_type (dpi
, options
, dc
, dpi
->modifiers
);
4378 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
4379 case DEMANGLE_COMPONENT_VECTOR_TYPE
:
4381 struct d_print_mod dpm
;
4383 dpm
.next
= dpi
->modifiers
;
4384 dpi
->modifiers
= &dpm
;
4387 dpm
.templates
= dpi
->templates
;
4389 d_print_comp (dpi
, options
, d_right (dc
));
4391 /* If the modifier didn't get printed by the type, print it
4394 d_print_mod (dpi
, options
, dc
);
4396 dpi
->modifiers
= dpm
.next
;
4401 case DEMANGLE_COMPONENT_FIXED_TYPE
:
4402 if (dc
->u
.s_fixed
.sat
)
4403 d_append_string (dpi
, "_Sat ");
4404 /* Don't print "int _Accum". */
4405 if (dc
->u
.s_fixed
.length
->u
.s_builtin
.type
4406 != &cplus_demangle_builtin_types
['i'-'a'])
4408 d_print_comp (dpi
, options
, dc
->u
.s_fixed
.length
);
4409 d_append_char (dpi
, ' ');
4411 if (dc
->u
.s_fixed
.accum
)
4412 d_append_string (dpi
, "_Accum");
4414 d_append_string (dpi
, "_Fract");
4417 case DEMANGLE_COMPONENT_ARGLIST
:
4418 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
4419 if (d_left (dc
) != NULL
)
4420 d_print_comp (dpi
, options
, d_left (dc
));
4421 if (d_right (dc
) != NULL
)
4424 unsigned long int flush_count
;
4425 /* Make sure ", " isn't flushed by d_append_string, otherwise
4426 dpi->len -= 2 wouldn't work. */
4427 if (dpi
->len
>= sizeof (dpi
->buf
) - 2)
4428 d_print_flush (dpi
);
4429 d_append_string (dpi
, ", ");
4431 flush_count
= dpi
->flush_count
;
4432 d_print_comp (dpi
, options
, d_right (dc
));
4433 /* If that didn't print anything (which can happen with empty
4434 template argument packs), remove the comma and space. */
4435 if (dpi
->flush_count
== flush_count
&& dpi
->len
== len
)
4440 case DEMANGLE_COMPONENT_INITIALIZER_LIST
:
4442 struct demangle_component
*type
= d_left (dc
);
4443 struct demangle_component
*list
= d_right (dc
);
4446 d_print_comp (dpi
, options
, type
);
4447 d_append_char (dpi
, '{');
4448 d_print_comp (dpi
, options
, list
);
4449 d_append_char (dpi
, '}');
4453 case DEMANGLE_COMPONENT_OPERATOR
:
4455 const struct demangle_operator_info
*op
= dc
->u
.s_operator
.op
;
4458 d_append_string (dpi
, "operator");
4459 /* Add a space before new/delete. */
4460 if (IS_LOWER (op
->name
[0]))
4461 d_append_char (dpi
, ' ');
4462 /* Omit a trailing space. */
4463 if (op
->name
[len
-1] == ' ')
4465 d_append_buffer (dpi
, op
->name
, len
);
4469 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
4470 d_append_string (dpi
, "operator ");
4471 d_print_comp (dpi
, options
, dc
->u
.s_extended_operator
.name
);
4474 case DEMANGLE_COMPONENT_CAST
:
4475 d_append_string (dpi
, "operator ");
4476 d_print_cast (dpi
, options
, dc
);
4479 case DEMANGLE_COMPONENT_NULLARY
:
4480 d_print_expr_op (dpi
, options
, d_left (dc
));
4483 case DEMANGLE_COMPONENT_UNARY
:
4485 struct demangle_component
*op
= d_left (dc
);
4486 struct demangle_component
*operand
= d_right (dc
);
4487 const char *code
= NULL
;
4489 if (op
->type
== DEMANGLE_COMPONENT_OPERATOR
)
4491 code
= op
->u
.s_operator
.op
->code
;
4492 if (!strcmp (code
, "ad"))
4494 /* Don't print the argument list for the address of a
4496 if (operand
->type
== DEMANGLE_COMPONENT_TYPED_NAME
4497 && d_left (operand
)->type
== DEMANGLE_COMPONENT_QUAL_NAME
4498 && d_right (operand
)->type
== DEMANGLE_COMPONENT_FUNCTION_TYPE
)
4499 operand
= d_left (operand
);
4501 if (operand
->type
== DEMANGLE_COMPONENT_BINARY_ARGS
)
4503 /* This indicates a suffix operator. */
4504 operand
= d_left (operand
);
4505 d_print_subexpr (dpi
, options
, operand
);
4506 d_print_expr_op (dpi
, options
, op
);
4511 if (op
->type
!= DEMANGLE_COMPONENT_CAST
)
4512 d_print_expr_op (dpi
, options
, op
);
4515 d_append_char (dpi
, '(');
4516 d_print_cast (dpi
, options
, op
);
4517 d_append_char (dpi
, ')');
4519 if (code
&& !strcmp (code
, "gs"))
4520 /* Avoid parens after '::'. */
4521 d_print_comp (dpi
, options
, operand
);
4522 else if (code
&& !strcmp (code
, "st"))
4523 /* Always print parens for sizeof (type). */
4525 d_append_char (dpi
, '(');
4526 d_print_comp (dpi
, options
, operand
);
4527 d_append_char (dpi
, ')');
4530 d_print_subexpr (dpi
, options
, operand
);
4534 case DEMANGLE_COMPONENT_BINARY
:
4535 if (d_right (dc
)->type
!= DEMANGLE_COMPONENT_BINARY_ARGS
)
4537 d_print_error (dpi
);
4541 if (op_is_new_cast (d_left (dc
)))
4543 d_print_expr_op (dpi
, options
, d_left (dc
));
4544 d_append_char (dpi
, '<');
4545 d_print_comp (dpi
, options
, d_left (d_right (dc
)));
4546 d_append_string (dpi
, ">(");
4547 d_print_comp (dpi
, options
, d_right (d_right (dc
)));
4548 d_append_char (dpi
, ')');
4552 /* We wrap an expression which uses the greater-than operator in
4553 an extra layer of parens so that it does not get confused
4554 with the '>' which ends the template parameters. */
4555 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_OPERATOR
4556 && d_left (dc
)->u
.s_operator
.op
->len
== 1
4557 && d_left (dc
)->u
.s_operator
.op
->name
[0] == '>')
4558 d_append_char (dpi
, '(');
4560 if (strcmp (d_left (dc
)->u
.s_operator
.op
->code
, "cl") == 0
4561 && d_left (d_right (dc
))->type
== DEMANGLE_COMPONENT_TYPED_NAME
)
4563 /* Function call used in an expression should not have printed types
4564 of the function arguments. Values of the function arguments still
4565 get printed below. */
4567 const struct demangle_component
*func
= d_left (d_right (dc
));
4569 if (d_right (func
)->type
!= DEMANGLE_COMPONENT_FUNCTION_TYPE
)
4570 d_print_error (dpi
);
4571 d_print_subexpr (dpi
, options
, d_left (func
));
4574 d_print_subexpr (dpi
, options
, d_left (d_right (dc
)));
4575 if (strcmp (d_left (dc
)->u
.s_operator
.op
->code
, "ix") == 0)
4577 d_append_char (dpi
, '[');
4578 d_print_comp (dpi
, options
, d_right (d_right (dc
)));
4579 d_append_char (dpi
, ']');
4583 if (strcmp (d_left (dc
)->u
.s_operator
.op
->code
, "cl") != 0)
4584 d_print_expr_op (dpi
, options
, d_left (dc
));
4585 d_print_subexpr (dpi
, options
, d_right (d_right (dc
)));
4588 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_OPERATOR
4589 && d_left (dc
)->u
.s_operator
.op
->len
== 1
4590 && d_left (dc
)->u
.s_operator
.op
->name
[0] == '>')
4591 d_append_char (dpi
, ')');
4595 case DEMANGLE_COMPONENT_BINARY_ARGS
:
4596 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
4597 d_print_error (dpi
);
4600 case DEMANGLE_COMPONENT_TRINARY
:
4601 if (d_right (dc
)->type
!= DEMANGLE_COMPONENT_TRINARY_ARG1
4602 || d_right (d_right (dc
))->type
!= DEMANGLE_COMPONENT_TRINARY_ARG2
)
4604 d_print_error (dpi
);
4608 struct demangle_component
*op
= d_left (dc
);
4609 struct demangle_component
*first
= d_left (d_right (dc
));
4610 struct demangle_component
*second
= d_left (d_right (d_right (dc
)));
4611 struct demangle_component
*third
= d_right (d_right (d_right (dc
)));
4613 if (!strcmp (op
->u
.s_operator
.op
->code
, "qu"))
4615 d_print_subexpr (dpi
, options
, first
);
4616 d_print_expr_op (dpi
, options
, op
);
4617 d_print_subexpr (dpi
, options
, second
);
4618 d_append_string (dpi
, " : ");
4619 d_print_subexpr (dpi
, options
, third
);
4623 d_append_string (dpi
, "new ");
4624 if (d_left (first
) != NULL
)
4626 d_print_subexpr (dpi
, options
, first
);
4627 d_append_char (dpi
, ' ');
4629 d_print_comp (dpi
, options
, second
);
4631 d_print_subexpr (dpi
, options
, third
);
4636 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
4637 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
4638 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
4639 d_print_error (dpi
);
4642 case DEMANGLE_COMPONENT_LITERAL
:
4643 case DEMANGLE_COMPONENT_LITERAL_NEG
:
4645 enum d_builtin_type_print tp
;
4647 /* For some builtin types, produce simpler output. */
4648 tp
= D_PRINT_DEFAULT
;
4649 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
)
4651 tp
= d_left (dc
)->u
.s_builtin
.type
->print
;
4655 case D_PRINT_UNSIGNED
:
4657 case D_PRINT_UNSIGNED_LONG
:
4658 case D_PRINT_LONG_LONG
:
4659 case D_PRINT_UNSIGNED_LONG_LONG
:
4660 if (d_right (dc
)->type
== DEMANGLE_COMPONENT_NAME
)
4662 if (dc
->type
== DEMANGLE_COMPONENT_LITERAL_NEG
)
4663 d_append_char (dpi
, '-');
4664 d_print_comp (dpi
, options
, d_right (dc
));
4669 case D_PRINT_UNSIGNED
:
4670 d_append_char (dpi
, 'u');
4673 d_append_char (dpi
, 'l');
4675 case D_PRINT_UNSIGNED_LONG
:
4676 d_append_string (dpi
, "ul");
4678 case D_PRINT_LONG_LONG
:
4679 d_append_string (dpi
, "ll");
4681 case D_PRINT_UNSIGNED_LONG_LONG
:
4682 d_append_string (dpi
, "ull");
4690 if (d_right (dc
)->type
== DEMANGLE_COMPONENT_NAME
4691 && d_right (dc
)->u
.s_name
.len
== 1
4692 && dc
->type
== DEMANGLE_COMPONENT_LITERAL
)
4694 switch (d_right (dc
)->u
.s_name
.s
[0])
4697 d_append_string (dpi
, "false");
4700 d_append_string (dpi
, "true");
4713 d_append_char (dpi
, '(');
4714 d_print_comp (dpi
, options
, d_left (dc
));
4715 d_append_char (dpi
, ')');
4716 if (dc
->type
== DEMANGLE_COMPONENT_LITERAL_NEG
)
4717 d_append_char (dpi
, '-');
4718 if (tp
== D_PRINT_FLOAT
)
4719 d_append_char (dpi
, '[');
4720 d_print_comp (dpi
, options
, d_right (dc
));
4721 if (tp
== D_PRINT_FLOAT
)
4722 d_append_char (dpi
, ']');
4726 case DEMANGLE_COMPONENT_NUMBER
:
4727 d_append_num (dpi
, dc
->u
.s_number
.number
);
4730 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
4731 d_append_string (dpi
, "java resource ");
4732 d_print_comp (dpi
, options
, d_left (dc
));
4735 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
4736 d_print_comp (dpi
, options
, d_left (dc
));
4737 d_print_comp (dpi
, options
, d_right (dc
));
4740 case DEMANGLE_COMPONENT_CHARACTER
:
4741 d_append_char (dpi
, dc
->u
.s_character
.character
);
4744 case DEMANGLE_COMPONENT_DECLTYPE
:
4745 d_append_string (dpi
, "decltype (");
4746 d_print_comp (dpi
, options
, d_left (dc
));
4747 d_append_char (dpi
, ')');
4750 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
4754 struct demangle_component
*a
= d_find_pack (dpi
, d_left (dc
));
4757 /* d_find_pack won't find anything if the only packs involved
4758 in this expansion are function parameter packs; in that
4759 case, just print the pattern and "...". */
4760 d_print_subexpr (dpi
, options
, d_left (dc
));
4761 d_append_string (dpi
, "...");
4765 len
= d_pack_length (a
);
4767 for (i
= 0; i
< len
; ++i
)
4769 dpi
->pack_index
= i
;
4770 d_print_comp (dpi
, options
, dc
);
4772 d_append_string (dpi
, ", ");
4777 case DEMANGLE_COMPONENT_FUNCTION_PARAM
:
4779 long num
= dc
->u
.s_number
.number
;
4781 d_append_string (dpi
, "this");
4784 d_append_string (dpi
, "{parm#");
4785 d_append_num (dpi
, num
);
4786 d_append_char (dpi
, '}');
4791 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
:
4792 d_append_string (dpi
, "global constructors keyed to ");
4793 d_print_comp (dpi
, options
, dc
->u
.s_binary
.left
);
4796 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS
:
4797 d_append_string (dpi
, "global destructors keyed to ");
4798 d_print_comp (dpi
, options
, dc
->u
.s_binary
.left
);
4801 case DEMANGLE_COMPONENT_LAMBDA
:
4802 d_append_string (dpi
, "{lambda(");
4803 d_print_comp (dpi
, options
, dc
->u
.s_unary_num
.sub
);
4804 d_append_string (dpi
, ")#");
4805 d_append_num (dpi
, dc
->u
.s_unary_num
.num
+ 1);
4806 d_append_char (dpi
, '}');
4809 case DEMANGLE_COMPONENT_UNNAMED_TYPE
:
4810 d_append_string (dpi
, "{unnamed type#");
4811 d_append_num (dpi
, dc
->u
.s_number
.number
+ 1);
4812 d_append_char (dpi
, '}');
4815 case DEMANGLE_COMPONENT_CLONE
:
4816 d_print_comp (dpi
, options
, d_left (dc
));
4817 d_append_string (dpi
, " [clone ");
4818 d_print_comp (dpi
, options
, d_right (dc
));
4819 d_append_char (dpi
, ']');
4823 d_print_error (dpi
);
4828 /* Print a Java dentifier. For Java we try to handle encoded extended
4829 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
4830 so we don't it for C++. Characters are encoded as
4834 d_print_java_identifier (struct d_print_info
*dpi
, const char *name
, int len
)
4840 for (p
= name
; p
< end
; ++p
)
4851 for (q
= p
+ 3; q
< end
; ++q
)
4857 else if (*q
>= 'A' && *q
<= 'F')
4858 dig
= *q
- 'A' + 10;
4859 else if (*q
>= 'a' && *q
<= 'f')
4860 dig
= *q
- 'a' + 10;
4866 /* If the Unicode character is larger than 256, we don't try
4867 to deal with it here. FIXME. */
4868 if (q
< end
&& *q
== '_' && c
< 256)
4870 d_append_char (dpi
, c
);
4876 d_append_char (dpi
, *p
);
4880 /* Print a list of modifiers. SUFFIX is 1 if we are printing
4881 qualifiers on this after printing a function. */
4884 d_print_mod_list (struct d_print_info
*dpi
, int options
,
4885 struct d_print_mod
*mods
, int suffix
)
4887 struct d_print_template
*hold_dpt
;
4889 if (mods
== NULL
|| d_print_saw_error (dpi
))
4894 && (mods
->mod
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
4895 || mods
->mod
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
4896 || mods
->mod
->type
== DEMANGLE_COMPONENT_CONST_THIS
)))
4898 d_print_mod_list (dpi
, options
, mods
->next
, suffix
);
4904 hold_dpt
= dpi
->templates
;
4905 dpi
->templates
= mods
->templates
;
4907 if (mods
->mod
->type
== DEMANGLE_COMPONENT_FUNCTION_TYPE
)
4909 d_print_function_type (dpi
, options
, mods
->mod
, mods
->next
);
4910 dpi
->templates
= hold_dpt
;
4913 else if (mods
->mod
->type
== DEMANGLE_COMPONENT_ARRAY_TYPE
)
4915 d_print_array_type (dpi
, options
, mods
->mod
, mods
->next
);
4916 dpi
->templates
= hold_dpt
;
4919 else if (mods
->mod
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
4921 struct d_print_mod
*hold_modifiers
;
4922 struct demangle_component
*dc
;
4924 /* When this is on the modifier stack, we have pulled any
4925 qualifiers off the right argument already. Otherwise, we
4926 print it as usual, but don't let the left argument see any
4929 hold_modifiers
= dpi
->modifiers
;
4930 dpi
->modifiers
= NULL
;
4931 d_print_comp (dpi
, options
, d_left (mods
->mod
));
4932 dpi
->modifiers
= hold_modifiers
;
4934 if ((options
& DMGL_JAVA
) == 0)
4935 d_append_string (dpi
, "::");
4937 d_append_char (dpi
, '.');
4939 dc
= d_right (mods
->mod
);
4941 if (dc
->type
== DEMANGLE_COMPONENT_DEFAULT_ARG
)
4943 d_append_string (dpi
, "{default arg#");
4944 d_append_num (dpi
, dc
->u
.s_unary_num
.num
+ 1);
4945 d_append_string (dpi
, "}::");
4946 dc
= dc
->u
.s_unary_num
.sub
;
4949 while (dc
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
4950 || dc
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
4951 || dc
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
4954 d_print_comp (dpi
, options
, dc
);
4956 dpi
->templates
= hold_dpt
;
4960 d_print_mod (dpi
, options
, mods
->mod
);
4962 dpi
->templates
= hold_dpt
;
4964 d_print_mod_list (dpi
, options
, mods
->next
, suffix
);
4967 /* Print a modifier. */
4970 d_print_mod (struct d_print_info
*dpi
, int options
,
4971 const struct demangle_component
*mod
)
4975 case DEMANGLE_COMPONENT_RESTRICT
:
4976 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
4977 d_append_string (dpi
, " restrict");
4979 case DEMANGLE_COMPONENT_VOLATILE
:
4980 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
4981 d_append_string (dpi
, " volatile");
4983 case DEMANGLE_COMPONENT_CONST
:
4984 case DEMANGLE_COMPONENT_CONST_THIS
:
4985 d_append_string (dpi
, " const");
4987 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
4988 d_append_char (dpi
, ' ');
4989 d_print_comp (dpi
, options
, d_right (mod
));
4991 case DEMANGLE_COMPONENT_POINTER
:
4992 /* There is no pointer symbol in Java. */
4993 if ((options
& DMGL_JAVA
) == 0)
4994 d_append_char (dpi
, '*');
4996 case DEMANGLE_COMPONENT_REFERENCE
:
4997 d_append_char (dpi
, '&');
4999 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
5000 d_append_string (dpi
, "&&");
5002 case DEMANGLE_COMPONENT_COMPLEX
:
5003 d_append_string (dpi
, "complex ");
5005 case DEMANGLE_COMPONENT_IMAGINARY
:
5006 d_append_string (dpi
, "imaginary ");
5008 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
5009 if (d_last_char (dpi
) != '(')
5010 d_append_char (dpi
, ' ');
5011 d_print_comp (dpi
, options
, d_left (mod
));
5012 d_append_string (dpi
, "::*");
5014 case DEMANGLE_COMPONENT_TYPED_NAME
:
5015 d_print_comp (dpi
, options
, d_left (mod
));
5017 case DEMANGLE_COMPONENT_VECTOR_TYPE
:
5018 d_append_string (dpi
, " __vector(");
5019 d_print_comp (dpi
, options
, d_left (mod
));
5020 d_append_char (dpi
, ')');
5024 /* Otherwise, we have something that won't go back on the
5025 modifier stack, so we can just print it. */
5026 d_print_comp (dpi
, options
, mod
);
5031 /* Print a function type, except for the return type. */
5034 d_print_function_type (struct d_print_info
*dpi
, int options
,
5035 const struct demangle_component
*dc
,
5036 struct d_print_mod
*mods
)
5040 struct d_print_mod
*p
;
5041 struct d_print_mod
*hold_modifiers
;
5045 for (p
= mods
; p
!= NULL
; p
= p
->next
)
5050 switch (p
->mod
->type
)
5052 case DEMANGLE_COMPONENT_POINTER
:
5053 case DEMANGLE_COMPONENT_REFERENCE
:
5054 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
5057 case DEMANGLE_COMPONENT_RESTRICT
:
5058 case DEMANGLE_COMPONENT_VOLATILE
:
5059 case DEMANGLE_COMPONENT_CONST
:
5060 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
5061 case DEMANGLE_COMPONENT_COMPLEX
:
5062 case DEMANGLE_COMPONENT_IMAGINARY
:
5063 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
5067 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
5068 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
5069 case DEMANGLE_COMPONENT_CONST_THIS
:
5082 if (d_last_char (dpi
) != '('
5083 && d_last_char (dpi
) != '*')
5086 if (need_space
&& d_last_char (dpi
) != ' ')
5087 d_append_char (dpi
, ' ');
5088 d_append_char (dpi
, '(');
5091 hold_modifiers
= dpi
->modifiers
;
5092 dpi
->modifiers
= NULL
;
5094 d_print_mod_list (dpi
, options
, mods
, 0);
5097 d_append_char (dpi
, ')');
5099 d_append_char (dpi
, '(');
5101 if (d_right (dc
) != NULL
)
5102 d_print_comp (dpi
, options
, d_right (dc
));
5104 d_append_char (dpi
, ')');
5106 d_print_mod_list (dpi
, options
, mods
, 1);
5108 dpi
->modifiers
= hold_modifiers
;
5111 /* Print an array type, except for the element type. */
5114 d_print_array_type (struct d_print_info
*dpi
, int options
,
5115 const struct demangle_component
*dc
,
5116 struct d_print_mod
*mods
)
5124 struct d_print_mod
*p
;
5127 for (p
= mods
; p
!= NULL
; p
= p
->next
)
5131 if (p
->mod
->type
== DEMANGLE_COMPONENT_ARRAY_TYPE
)
5146 d_append_string (dpi
, " (");
5148 d_print_mod_list (dpi
, options
, mods
, 0);
5151 d_append_char (dpi
, ')');
5155 d_append_char (dpi
, ' ');
5157 d_append_char (dpi
, '[');
5159 if (d_left (dc
) != NULL
)
5160 d_print_comp (dpi
, options
, d_left (dc
));
5162 d_append_char (dpi
, ']');
5165 /* Print an operator in an expression. */
5168 d_print_expr_op (struct d_print_info
*dpi
, int options
,
5169 const struct demangle_component
*dc
)
5171 if (dc
->type
== DEMANGLE_COMPONENT_OPERATOR
)
5172 d_append_buffer (dpi
, dc
->u
.s_operator
.op
->name
,
5173 dc
->u
.s_operator
.op
->len
);
5175 d_print_comp (dpi
, options
, dc
);
5181 d_print_cast (struct d_print_info
*dpi
, int options
,
5182 const struct demangle_component
*dc
)
5184 if (d_left (dc
)->type
!= DEMANGLE_COMPONENT_TEMPLATE
)
5185 d_print_comp (dpi
, options
, d_left (dc
));
5188 struct d_print_mod
*hold_dpm
;
5189 struct d_print_template dpt
;
5191 /* It appears that for a templated cast operator, we need to put
5192 the template parameters in scope for the operator name, but
5193 not for the parameters. The effect is that we need to handle
5194 the template printing here. */
5196 hold_dpm
= dpi
->modifiers
;
5197 dpi
->modifiers
= NULL
;
5199 dpt
.next
= dpi
->templates
;
5200 dpi
->templates
= &dpt
;
5201 dpt
.template_decl
= d_left (dc
);
5203 d_print_comp (dpi
, options
, d_left (d_left (dc
)));
5205 dpi
->templates
= dpt
.next
;
5207 if (d_last_char (dpi
) == '<')
5208 d_append_char (dpi
, ' ');
5209 d_append_char (dpi
, '<');
5210 d_print_comp (dpi
, options
, d_right (d_left (dc
)));
5211 /* Avoid generating two consecutive '>' characters, to avoid
5212 the C++ syntactic ambiguity. */
5213 if (d_last_char (dpi
) == '>')
5214 d_append_char (dpi
, ' ');
5215 d_append_char (dpi
, '>');
5217 dpi
->modifiers
= hold_dpm
;
5221 /* Initialize the information structure we use to pass around
5224 CP_STATIC_IF_GLIBCPP_V3
5226 cplus_demangle_init_info (const char *mangled
, int options
, size_t len
,
5230 di
->send
= mangled
+ len
;
5231 di
->options
= options
;
5235 /* We can not need more components than twice the number of chars in
5236 the mangled string. Most components correspond directly to
5237 chars, but the ARGLIST types are exceptions. */
5238 di
->num_comps
= 2 * len
;
5241 /* Similarly, we can not need more substitutions than there are
5242 chars in the mangled string. */
5247 di
->last_name
= NULL
;
5252 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
5253 mangled name, return strings in repeated callback giving the demangled
5254 name. OPTIONS is the usual libiberty demangler options. On success,
5255 this returns 1. On failure, returns 0. */
5258 d_demangle_callback (const char *mangled
, int options
,
5259 demangle_callbackref callback
, void *opaque
)
5270 struct demangle_component
*dc
;
5273 if (mangled
[0] == '_' && mangled
[1] == 'Z')
5275 else if (strncmp (mangled
, "_GLOBAL_", 8) == 0
5276 && (mangled
[8] == '.' || mangled
[8] == '_' || mangled
[8] == '$')
5277 && (mangled
[9] == 'D' || mangled
[9] == 'I')
5278 && mangled
[10] == '_')
5279 type
= mangled
[9] == 'I' ? DCT_GLOBAL_CTORS
: DCT_GLOBAL_DTORS
;
5282 if ((options
& DMGL_TYPES
) == 0)
5287 cplus_demangle_init_info (mangled
, options
, strlen (mangled
), &di
);
5290 #ifdef CP_DYNAMIC_ARRAYS
5291 __extension__
struct demangle_component comps
[di
.num_comps
];
5292 __extension__
struct demangle_component
*subs
[di
.num_subs
];
5297 di
.comps
= alloca (di
.num_comps
* sizeof (*di
.comps
));
5298 di
.subs
= alloca (di
.num_subs
* sizeof (*di
.subs
));
5304 dc
= cplus_demangle_type (&di
);
5307 dc
= cplus_demangle_mangled_name (&di
, 1);
5309 case DCT_GLOBAL_CTORS
:
5310 case DCT_GLOBAL_DTORS
:
5311 d_advance (&di
, 11);
5312 dc
= d_make_comp (&di
,
5313 (type
== DCT_GLOBAL_CTORS
5314 ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
5315 : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS
),
5316 d_make_demangle_mangled_name (&di
, d_str (&di
)),
5318 d_advance (&di
, strlen (d_str (&di
)));
5322 /* If DMGL_PARAMS is set, then if we didn't consume the entire
5323 mangled string, then we didn't successfully demangle it. If
5324 DMGL_PARAMS is not set, we didn't look at the trailing
5326 if (((options
& DMGL_PARAMS
) != 0) && d_peek_char (&di
) != '\0')
5329 #ifdef CP_DEMANGLE_DEBUG
5333 status
= (dc
!= NULL
)
5334 ? cplus_demangle_print_callback (options
, dc
, callback
, opaque
)
5341 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
5342 name, return a buffer allocated with malloc holding the demangled
5343 name. OPTIONS is the usual libiberty demangler options. On
5344 success, this sets *PALC to the allocated size of the returned
5345 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
5346 a memory allocation failure, and returns NULL. */
5349 d_demangle (const char *mangled
, int options
, size_t *palc
)
5351 struct d_growable_string dgs
;
5354 d_growable_string_init (&dgs
, 0);
5356 status
= d_demangle_callback (mangled
, options
,
5357 d_growable_string_callback_adapter
, &dgs
);
5365 *palc
= dgs
.allocation_failure
? 1 : dgs
.alc
;
5369 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
5371 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
5373 /* ia64 ABI-mandated entry point in the C++ runtime library for
5374 performing demangling. MANGLED_NAME is a NUL-terminated character
5375 string containing the name to be demangled.
5377 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
5378 *LENGTH bytes, into which the demangled name is stored. If
5379 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
5380 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
5381 is placed in a region of memory allocated with malloc.
5383 If LENGTH is non-NULL, the length of the buffer containing the
5384 demangled name, is placed in *LENGTH.
5386 The return value is a pointer to the start of the NUL-terminated
5387 demangled name, or NULL if the demangling fails. The caller is
5388 responsible for deallocating this memory using free.
5390 *STATUS is set to one of the following values:
5391 0: The demangling operation succeeded.
5392 -1: A memory allocation failure occurred.
5393 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
5394 -3: One of the arguments is invalid.
5396 The demangling is performed using the C++ ABI mangling rules, with
5400 __cxa_demangle (const char *mangled_name
, char *output_buffer
,
5401 size_t *length
, int *status
)
5406 if (mangled_name
== NULL
)
5413 if (output_buffer
!= NULL
&& length
== NULL
)
5420 demangled
= d_demangle (mangled_name
, DMGL_PARAMS
| DMGL_TYPES
, &alc
);
5422 if (demangled
== NULL
)
5434 if (output_buffer
== NULL
)
5441 if (strlen (demangled
) < *length
)
5443 strcpy (output_buffer
, demangled
);
5445 demangled
= output_buffer
;
5449 free (output_buffer
);
5460 extern int __gcclibcxx_demangle_callback (const char *,
5462 (const char *, size_t, void *),
5465 /* Alternative, allocationless entry point in the C++ runtime library
5466 for performing demangling. MANGLED_NAME is a NUL-terminated character
5467 string containing the name to be demangled.
5469 CALLBACK is a callback function, called with demangled string
5470 segments as demangling progresses; it is called at least once,
5471 but may be called more than once. OPAQUE is a generalized pointer
5472 used as a callback argument.
5474 The return code is one of the following values, equivalent to
5475 the STATUS values of __cxa_demangle() (excluding -1, since this
5476 function performs no memory allocations):
5477 0: The demangling operation succeeded.
5478 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
5479 -3: One of the arguments is invalid.
5481 The demangling is performed using the C++ ABI mangling rules, with
5485 __gcclibcxx_demangle_callback (const char *mangled_name
,
5486 void (*callback
) (const char *, size_t, void *),
5491 if (mangled_name
== NULL
|| callback
== NULL
)
5494 status
= d_demangle_callback (mangled_name
, DMGL_PARAMS
| DMGL_TYPES
,
5502 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
5504 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
5505 mangled name, return a buffer allocated with malloc holding the
5506 demangled name. Otherwise, return NULL. */
5509 cplus_demangle_v3 (const char *mangled
, int options
)
5513 return d_demangle (mangled
, options
, &alc
);
5517 cplus_demangle_v3_callback (const char *mangled
, int options
,
5518 demangle_callbackref callback
, void *opaque
)
5520 return d_demangle_callback (mangled
, options
, callback
, opaque
);
5523 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
5524 conventions, but the output formatting is a little different.
5525 This instructs the C++ demangler not to emit pointer characters ("*"), to
5526 use Java's namespace separator symbol ("." instead of "::"), and to output
5527 JArray<TYPE> as TYPE[]. */
5530 java_demangle_v3 (const char *mangled
)
5534 return d_demangle (mangled
, DMGL_JAVA
| DMGL_PARAMS
| DMGL_RET_POSTFIX
, &alc
);
5538 java_demangle_v3_callback (const char *mangled
,
5539 demangle_callbackref callback
, void *opaque
)
5541 return d_demangle_callback (mangled
,
5542 DMGL_JAVA
| DMGL_PARAMS
| DMGL_RET_POSTFIX
,
5546 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
5548 #ifndef IN_GLIBCPP_V3
5550 /* Demangle a string in order to find out whether it is a constructor
5551 or destructor. Return non-zero on success. Set *CTOR_KIND and
5552 *DTOR_KIND appropriately. */
5555 is_ctor_or_dtor (const char *mangled
,
5556 enum gnu_v3_ctor_kinds
*ctor_kind
,
5557 enum gnu_v3_dtor_kinds
*dtor_kind
)
5560 struct demangle_component
*dc
;
5563 *ctor_kind
= (enum gnu_v3_ctor_kinds
) 0;
5564 *dtor_kind
= (enum gnu_v3_dtor_kinds
) 0;
5566 cplus_demangle_init_info (mangled
, DMGL_GNU_V3
, strlen (mangled
), &di
);
5569 #ifdef CP_DYNAMIC_ARRAYS
5570 __extension__
struct demangle_component comps
[di
.num_comps
];
5571 __extension__
struct demangle_component
*subs
[di
.num_subs
];
5576 di
.comps
= alloca (di
.num_comps
* sizeof (*di
.comps
));
5577 di
.subs
= alloca (di
.num_subs
* sizeof (*di
.subs
));
5580 dc
= cplus_demangle_mangled_name (&di
, 1);
5582 /* Note that because we did not pass DMGL_PARAMS, we don't expect
5583 to demangle the entire string. */
5593 case DEMANGLE_COMPONENT_TYPED_NAME
:
5594 case DEMANGLE_COMPONENT_TEMPLATE
:
5595 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
5596 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
5597 case DEMANGLE_COMPONENT_CONST_THIS
:
5600 case DEMANGLE_COMPONENT_QUAL_NAME
:
5601 case DEMANGLE_COMPONENT_LOCAL_NAME
:
5604 case DEMANGLE_COMPONENT_CTOR
:
5605 *ctor_kind
= dc
->u
.s_ctor
.kind
;
5609 case DEMANGLE_COMPONENT_DTOR
:
5610 *dtor_kind
= dc
->u
.s_dtor
.kind
;
5621 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
5622 name. A non-zero return indicates the type of constructor. */
5624 enum gnu_v3_ctor_kinds
5625 is_gnu_v3_mangled_ctor (const char *name
)
5627 enum gnu_v3_ctor_kinds ctor_kind
;
5628 enum gnu_v3_dtor_kinds dtor_kind
;
5630 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
5631 return (enum gnu_v3_ctor_kinds
) 0;
5636 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
5637 name. A non-zero return indicates the type of destructor. */
5639 enum gnu_v3_dtor_kinds
5640 is_gnu_v3_mangled_dtor (const char *name
)
5642 enum gnu_v3_ctor_kinds ctor_kind
;
5643 enum gnu_v3_dtor_kinds dtor_kind
;
5645 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
5646 return (enum gnu_v3_dtor_kinds
) 0;
5650 #endif /* IN_GLIBCPP_V3 */
5652 #ifdef STANDALONE_DEMANGLER
5655 #include "dyn-string.h"
5657 static void print_usage (FILE* fp
, int exit_value
);
5659 #define IS_ALPHA(CHAR) \
5660 (((CHAR) >= 'a' && (CHAR) <= 'z') \
5661 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
5663 /* Non-zero if CHAR is a character than can occur in a mangled name. */
5664 #define is_mangled_char(CHAR) \
5665 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
5666 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
5668 /* The name of this program, as invoked. */
5669 const char* program_name
;
5671 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
5674 print_usage (FILE* fp
, int exit_value
)
5676 fprintf (fp
, "Usage: %s [options] [names ...]\n", program_name
);
5677 fprintf (fp
, "Options:\n");
5678 fprintf (fp
, " -h,--help Display this message.\n");
5679 fprintf (fp
, " -p,--no-params Don't display function parameters\n");
5680 fprintf (fp
, " -v,--verbose Produce verbose demanglings.\n");
5681 fprintf (fp
, "If names are provided, they are demangled. Otherwise filters standard input.\n");
5686 /* Option specification for getopt_long. */
5687 static const struct option long_options
[] =
5689 { "help", no_argument
, NULL
, 'h' },
5690 { "no-params", no_argument
, NULL
, 'p' },
5691 { "verbose", no_argument
, NULL
, 'v' },
5692 { NULL
, no_argument
, NULL
, 0 },
5695 /* Main entry for a demangling filter executable. It will demangle
5696 its command line arguments, if any. If none are provided, it will
5697 filter stdin to stdout, replacing any recognized mangled C++ names
5698 with their demangled equivalents. */
5701 main (int argc
, char *argv
[])
5705 int options
= DMGL_PARAMS
| DMGL_ANSI
| DMGL_TYPES
;
5707 /* Use the program name of this program, as invoked. */
5708 program_name
= argv
[0];
5710 /* Parse options. */
5713 opt_char
= getopt_long (argc
, argv
, "hpv", long_options
, NULL
);
5716 case '?': /* Unrecognized option. */
5717 print_usage (stderr
, 1);
5721 print_usage (stdout
, 0);
5725 options
&= ~ DMGL_PARAMS
;
5729 options
|= DMGL_VERBOSE
;
5733 while (opt_char
!= -1);
5736 /* No command line arguments were provided. Filter stdin. */
5738 dyn_string_t mangled
= dyn_string_new (3);
5741 /* Read all of input. */
5742 while (!feof (stdin
))
5746 /* Pile characters into mangled until we hit one that can't
5747 occur in a mangled name. */
5749 while (!feof (stdin
) && is_mangled_char (c
))
5751 dyn_string_append_char (mangled
, c
);
5757 if (dyn_string_length (mangled
) > 0)
5759 #ifdef IN_GLIBCPP_V3
5760 s
= __cxa_demangle (dyn_string_buf (mangled
), NULL
, NULL
, NULL
);
5762 s
= cplus_demangle_v3 (dyn_string_buf (mangled
), options
);
5772 /* It might not have been a mangled name. Print the
5774 fputs (dyn_string_buf (mangled
), stdout
);
5777 dyn_string_clear (mangled
);
5780 /* If we haven't hit EOF yet, we've read one character that
5781 can't occur in a mangled name, so print it out. */
5786 dyn_string_delete (mangled
);
5789 /* Demangle command line arguments. */
5791 /* Loop over command line arguments. */
5792 for (i
= optind
; i
< argc
; ++i
)
5795 #ifdef IN_GLIBCPP_V3
5799 /* Attempt to demangle. */
5800 #ifdef IN_GLIBCPP_V3
5801 s
= __cxa_demangle (argv
[i
], NULL
, NULL
, &status
);
5803 s
= cplus_demangle_v3 (argv
[i
], options
);
5806 /* If it worked, print the demangled name. */
5814 #ifdef IN_GLIBCPP_V3
5815 fprintf (stderr
, "Failed: %s (status %d)\n", argv
[i
], status
);
5817 fprintf (stderr
, "Failed: %s\n", argv
[i
]);
5826 #endif /* STANDALONE_DEMANGLER */