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 return d_nested_name (di
);
1252 return d_local_name (di
);
1256 return d_unqualified_name (di
);
1262 if (d_peek_next_char (di
) != 't')
1264 dc
= d_substitution (di
, 0);
1270 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
,
1271 d_make_name (di
, "std", 3),
1272 d_unqualified_name (di
));
1277 if (d_peek_char (di
) != 'I')
1279 /* The grammar does not permit this case to occur if we
1280 called d_substitution() above (i.e., subst == 1). We
1281 don't bother to check. */
1285 /* This is <template-args>, which means that we just saw
1286 <unscoped-template-name>, which is a substitution
1287 candidate if we didn't just get it from a
1291 if (! d_add_substitution (di
, dc
))
1294 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, dc
,
1295 d_template_args (di
));
1302 dc
= d_unqualified_name (di
);
1303 if (d_peek_char (di
) == 'I')
1305 /* This is <template-args>, which means that we just saw
1306 <unscoped-template-name>, which is a substitution
1308 if (! d_add_substitution (di
, dc
))
1310 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, dc
,
1311 d_template_args (di
));
1317 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1318 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1321 static struct demangle_component
*
1322 d_nested_name (struct d_info
*di
)
1324 struct demangle_component
*ret
;
1325 struct demangle_component
**pret
;
1327 if (! d_check_char (di
, 'N'))
1330 pret
= d_cv_qualifiers (di
, &ret
, 1);
1334 *pret
= d_prefix (di
);
1338 if (! d_check_char (di
, 'E'))
1344 /* <prefix> ::= <prefix> <unqualified-name>
1345 ::= <template-prefix> <template-args>
1346 ::= <template-param>
1351 <template-prefix> ::= <prefix> <(template) unqualified-name>
1352 ::= <template-param>
1356 static struct demangle_component
*
1357 d_prefix (struct d_info
*di
)
1359 struct demangle_component
*ret
= NULL
;
1364 enum demangle_component_type comb_type
;
1365 struct demangle_component
*dc
;
1367 peek
= d_peek_char (di
);
1371 /* The older code accepts a <local-name> here, but I don't see
1372 that in the grammar. The older code does not accept a
1373 <template-param> here. */
1375 comb_type
= DEMANGLE_COMPONENT_QUAL_NAME
;
1378 char peek2
= d_peek_next_char (di
);
1379 if (peek2
== 'T' || peek2
== 't')
1381 dc
= cplus_demangle_type (di
);
1383 /* Destructor name. */
1384 dc
= d_unqualified_name (di
);
1386 else if (IS_DIGIT (peek
)
1391 dc
= d_unqualified_name (di
);
1392 else if (peek
== 'S')
1393 dc
= d_substitution (di
, 1);
1394 else if (peek
== 'I')
1398 comb_type
= DEMANGLE_COMPONENT_TEMPLATE
;
1399 dc
= d_template_args (di
);
1401 else if (peek
== 'T')
1402 dc
= d_template_param (di
);
1403 else if (peek
== 'E')
1405 else if (peek
== 'M')
1407 /* Initializer scope for a lambda. We don't need to represent
1408 this; the normal code will just treat the variable as a type
1409 scope, which gives appropriate output. */
1421 ret
= d_make_comp (di
, comb_type
, ret
, dc
);
1423 if (peek
!= 'S' && d_peek_char (di
) != 'E')
1425 if (! d_add_substitution (di
, ret
))
1431 /* <unqualified-name> ::= <operator-name>
1432 ::= <ctor-dtor-name>
1434 ::= <local-source-name>
1436 <local-source-name> ::= L <source-name> <discriminator>
1439 static struct demangle_component
*
1440 d_unqualified_name (struct d_info
*di
)
1442 struct demangle_component
*ret
;
1445 peek
= d_peek_char (di
);
1446 if (IS_DIGIT (peek
))
1447 ret
= d_source_name (di
);
1448 else if (IS_LOWER (peek
))
1450 ret
= d_operator_name (di
);
1451 if (ret
!= NULL
&& ret
->type
== DEMANGLE_COMPONENT_OPERATOR
)
1453 di
->expansion
+= sizeof "operator" + ret
->u
.s_operator
.op
->len
- 2;
1454 if (!strcmp (ret
->u
.s_operator
.op
->code
, "li"))
1455 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, ret
,
1456 d_source_name (di
));
1459 else if (peek
== 'C' || peek
== 'D')
1460 ret
= d_ctor_dtor_name (di
);
1461 else if (peek
== 'L')
1465 ret
= d_source_name (di
);
1468 if (! d_discriminator (di
))
1471 else if (peek
== 'U')
1473 switch (d_peek_next_char (di
))
1476 ret
= d_lambda (di
);
1479 ret
= d_unnamed_type (di
);
1488 if (d_peek_char (di
) == 'B')
1489 ret
= d_abi_tags (di
, ret
);
1493 /* <source-name> ::= <(positive length) number> <identifier> */
1495 static struct demangle_component
*
1496 d_source_name (struct d_info
*di
)
1499 struct demangle_component
*ret
;
1501 len
= d_number (di
);
1504 ret
= d_identifier (di
, len
);
1505 di
->last_name
= ret
;
1509 /* number ::= [n] <(non-negative decimal integer)> */
1512 d_number (struct d_info
*di
)
1519 peek
= d_peek_char (di
);
1524 peek
= d_peek_char (di
);
1530 if (! IS_DIGIT (peek
))
1536 ret
= ret
* 10 + peek
- '0';
1538 peek
= d_peek_char (di
);
1542 /* Like d_number, but returns a demangle_component. */
1544 static struct demangle_component
*
1545 d_number_component (struct d_info
*di
)
1547 struct demangle_component
*ret
= d_make_empty (di
);
1550 ret
->type
= DEMANGLE_COMPONENT_NUMBER
;
1551 ret
->u
.s_number
.number
= d_number (di
);
1556 /* identifier ::= <(unqualified source code identifier)> */
1558 static struct demangle_component
*
1559 d_identifier (struct d_info
*di
, int len
)
1565 if (di
->send
- name
< len
)
1568 d_advance (di
, len
);
1570 /* A Java mangled name may have a trailing '$' if it is a C++
1571 keyword. This '$' is not included in the length count. We just
1573 if ((di
->options
& DMGL_JAVA
) != 0
1574 && d_peek_char (di
) == '$')
1577 /* Look for something which looks like a gcc encoding of an
1578 anonymous namespace, and replace it with a more user friendly
1580 if (len
>= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN
+ 2
1581 && memcmp (name
, ANONYMOUS_NAMESPACE_PREFIX
,
1582 ANONYMOUS_NAMESPACE_PREFIX_LEN
) == 0)
1586 s
= name
+ ANONYMOUS_NAMESPACE_PREFIX_LEN
;
1587 if ((*s
== '.' || *s
== '_' || *s
== '$')
1590 di
->expansion
-= len
- sizeof "(anonymous namespace)";
1591 return d_make_name (di
, "(anonymous namespace)",
1592 sizeof "(anonymous namespace)" - 1);
1596 return d_make_name (di
, name
, len
);
1599 /* operator_name ::= many different two character encodings.
1601 ::= v <digit> <source-name>
1603 This list is sorted for binary search. */
1605 #define NL(s) s, (sizeof s) - 1
1607 CP_STATIC_IF_GLIBCPP_V3
1608 const struct demangle_operator_info cplus_demangle_operators
[] =
1610 { "aN", NL ("&="), 2 },
1611 { "aS", NL ("="), 2 },
1612 { "aa", NL ("&&"), 2 },
1613 { "ad", NL ("&"), 1 },
1614 { "an", NL ("&"), 2 },
1615 { "at", NL ("alignof "), 1 },
1616 { "az", NL ("alignof "), 1 },
1617 { "cc", NL ("const_cast"), 2 },
1618 { "cl", NL ("()"), 2 },
1619 { "cm", NL (","), 2 },
1620 { "co", NL ("~"), 1 },
1621 { "dV", NL ("/="), 2 },
1622 { "da", NL ("delete[] "), 1 },
1623 { "dc", NL ("dynamic_cast"), 2 },
1624 { "de", NL ("*"), 1 },
1625 { "dl", NL ("delete "), 1 },
1626 { "ds", NL (".*"), 2 },
1627 { "dt", NL ("."), 2 },
1628 { "dv", NL ("/"), 2 },
1629 { "eO", NL ("^="), 2 },
1630 { "eo", NL ("^"), 2 },
1631 { "eq", NL ("=="), 2 },
1632 { "ge", NL (">="), 2 },
1633 { "gs", NL ("::"), 1 },
1634 { "gt", NL (">"), 2 },
1635 { "ix", NL ("[]"), 2 },
1636 { "lS", NL ("<<="), 2 },
1637 { "le", NL ("<="), 2 },
1638 { "li", NL ("operator\"\" "), 1 },
1639 { "ls", NL ("<<"), 2 },
1640 { "lt", NL ("<"), 2 },
1641 { "mI", NL ("-="), 2 },
1642 { "mL", NL ("*="), 2 },
1643 { "mi", NL ("-"), 2 },
1644 { "ml", NL ("*"), 2 },
1645 { "mm", NL ("--"), 1 },
1646 { "na", NL ("new[]"), 3 },
1647 { "ne", NL ("!="), 2 },
1648 { "ng", NL ("-"), 1 },
1649 { "nt", NL ("!"), 1 },
1650 { "nw", NL ("new"), 3 },
1651 { "oR", NL ("|="), 2 },
1652 { "oo", NL ("||"), 2 },
1653 { "or", NL ("|"), 2 },
1654 { "pL", NL ("+="), 2 },
1655 { "pl", NL ("+"), 2 },
1656 { "pm", NL ("->*"), 2 },
1657 { "pp", NL ("++"), 1 },
1658 { "ps", NL ("+"), 1 },
1659 { "pt", NL ("->"), 2 },
1660 { "qu", NL ("?"), 3 },
1661 { "rM", NL ("%="), 2 },
1662 { "rS", NL (">>="), 2 },
1663 { "rc", NL ("reinterpret_cast"), 2 },
1664 { "rm", NL ("%"), 2 },
1665 { "rs", NL (">>"), 2 },
1666 { "sc", NL ("static_cast"), 2 },
1667 { "st", NL ("sizeof "), 1 },
1668 { "sz", NL ("sizeof "), 1 },
1669 { "tr", NL ("throw"), 0 },
1670 { "tw", NL ("throw "), 1 },
1671 { NULL
, NULL
, 0, 0 }
1674 static struct demangle_component
*
1675 d_operator_name (struct d_info
*di
)
1680 c1
= d_next_char (di
);
1681 c2
= d_next_char (di
);
1682 if (c1
== 'v' && IS_DIGIT (c2
))
1683 return d_make_extended_operator (di
, c2
- '0', d_source_name (di
));
1684 else if (c1
== 'c' && c2
== 'v')
1685 return d_make_comp (di
, DEMANGLE_COMPONENT_CAST
,
1686 cplus_demangle_type (di
), NULL
);
1689 /* LOW is the inclusive lower bound. */
1691 /* HIGH is the exclusive upper bound. We subtract one to ignore
1692 the sentinel at the end of the array. */
1693 int high
= ((sizeof (cplus_demangle_operators
)
1694 / sizeof (cplus_demangle_operators
[0]))
1700 const struct demangle_operator_info
*p
;
1702 i
= low
+ (high
- low
) / 2;
1703 p
= cplus_demangle_operators
+ i
;
1705 if (c1
== p
->code
[0] && c2
== p
->code
[1])
1706 return d_make_operator (di
, p
);
1708 if (c1
< p
->code
[0] || (c1
== p
->code
[0] && c2
< p
->code
[1]))
1718 static struct demangle_component
*
1719 d_make_character (struct d_info
*di
, int c
)
1721 struct demangle_component
*p
;
1722 p
= d_make_empty (di
);
1725 p
->type
= DEMANGLE_COMPONENT_CHARACTER
;
1726 p
->u
.s_character
.character
= c
;
1731 static struct demangle_component
*
1732 d_java_resource (struct d_info
*di
)
1734 struct demangle_component
*p
= NULL
;
1735 struct demangle_component
*next
= NULL
;
1740 len
= d_number (di
);
1744 /* Eat the leading '_'. */
1745 if (d_next_char (di
) != '_')
1758 /* Each chunk is either a '$' escape... */
1776 next
= d_make_character (di
, c
);
1784 /* ... or a sequence of characters. */
1787 while (i
< len
&& str
[i
] && str
[i
] != '$')
1790 next
= d_make_name (di
, str
, i
);
1803 p
= d_make_comp (di
, DEMANGLE_COMPONENT_COMPOUND_NAME
, p
, next
);
1809 p
= d_make_comp (di
, DEMANGLE_COMPONENT_JAVA_RESOURCE
, p
, NULL
);
1814 /* <special-name> ::= TV <type>
1818 ::= GV <(object) name>
1819 ::= T <call-offset> <(base) encoding>
1820 ::= Tc <call-offset> <call-offset> <(base) encoding>
1821 Also g++ extensions:
1822 ::= TC <type> <(offset) number> _ <(base) type>
1827 ::= Gr <resource name>
1832 static struct demangle_component
*
1833 d_special_name (struct d_info
*di
)
1835 di
->expansion
+= 20;
1836 if (d_check_char (di
, 'T'))
1838 switch (d_next_char (di
))
1842 return d_make_comp (di
, DEMANGLE_COMPONENT_VTABLE
,
1843 cplus_demangle_type (di
), NULL
);
1845 di
->expansion
-= 10;
1846 return d_make_comp (di
, DEMANGLE_COMPONENT_VTT
,
1847 cplus_demangle_type (di
), NULL
);
1849 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO
,
1850 cplus_demangle_type (di
), NULL
);
1852 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO_NAME
,
1853 cplus_demangle_type (di
), NULL
);
1856 if (! d_call_offset (di
, 'h'))
1858 return d_make_comp (di
, DEMANGLE_COMPONENT_THUNK
,
1859 d_encoding (di
, 0), NULL
);
1862 if (! d_call_offset (di
, 'v'))
1864 return d_make_comp (di
, DEMANGLE_COMPONENT_VIRTUAL_THUNK
,
1865 d_encoding (di
, 0), NULL
);
1868 if (! d_call_offset (di
, '\0'))
1870 if (! d_call_offset (di
, '\0'))
1872 return d_make_comp (di
, DEMANGLE_COMPONENT_COVARIANT_THUNK
,
1873 d_encoding (di
, 0), NULL
);
1877 struct demangle_component
*derived_type
;
1879 struct demangle_component
*base_type
;
1881 derived_type
= cplus_demangle_type (di
);
1882 offset
= d_number (di
);
1885 if (! d_check_char (di
, '_'))
1887 base_type
= cplus_demangle_type (di
);
1888 /* We don't display the offset. FIXME: We should display
1889 it in verbose mode. */
1891 return d_make_comp (di
, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
,
1892 base_type
, derived_type
);
1896 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO_FN
,
1897 cplus_demangle_type (di
), NULL
);
1899 return d_make_comp (di
, DEMANGLE_COMPONENT_JAVA_CLASS
,
1900 cplus_demangle_type (di
), NULL
);
1903 return d_make_comp (di
, DEMANGLE_COMPONENT_TLS_INIT
,
1907 return d_make_comp (di
, DEMANGLE_COMPONENT_TLS_WRAPPER
,
1914 else if (d_check_char (di
, 'G'))
1916 switch (d_next_char (di
))
1919 return d_make_comp (di
, DEMANGLE_COMPONENT_GUARD
, d_name (di
), NULL
);
1923 struct demangle_component
*name
= d_name (di
);
1924 return d_make_comp (di
, DEMANGLE_COMPONENT_REFTEMP
, name
,
1925 d_number_component (di
));
1929 return d_make_comp (di
, DEMANGLE_COMPONENT_HIDDEN_ALIAS
,
1930 d_encoding (di
, 0), NULL
);
1933 switch (d_next_char (di
))
1936 return d_make_comp (di
, DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
,
1937 d_encoding (di
, 0), NULL
);
1939 /* ??? The proposal is that other letters (such as 'h') stand
1940 for different variants of transaction cloning, such as
1941 compiling directly for hardware transaction support. But
1942 they still should all be transactional clones of some sort
1943 so go ahead and call them that. */
1945 return d_make_comp (di
, DEMANGLE_COMPONENT_TRANSACTION_CLONE
,
1946 d_encoding (di
, 0), NULL
);
1950 return d_java_resource (di
);
1960 /* <call-offset> ::= h <nv-offset> _
1963 <nv-offset> ::= <(offset) number>
1965 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1967 The C parameter, if not '\0', is a character we just read which is
1968 the start of the <call-offset>.
1970 We don't display the offset information anywhere. FIXME: We should
1971 display it in verbose mode. */
1974 d_call_offset (struct d_info
*di
, int c
)
1977 c
= d_next_char (di
);
1984 if (! d_check_char (di
, '_'))
1991 if (! d_check_char (di
, '_'))
1997 /* <ctor-dtor-name> ::= C1
2005 static struct demangle_component
*
2006 d_ctor_dtor_name (struct d_info
*di
)
2008 if (di
->last_name
!= NULL
)
2010 if (di
->last_name
->type
== DEMANGLE_COMPONENT_NAME
)
2011 di
->expansion
+= di
->last_name
->u
.s_name
.len
;
2012 else if (di
->last_name
->type
== DEMANGLE_COMPONENT_SUB_STD
)
2013 di
->expansion
+= di
->last_name
->u
.s_string
.len
;
2015 switch (d_peek_char (di
))
2019 enum gnu_v3_ctor_kinds kind
;
2021 switch (d_peek_next_char (di
))
2024 kind
= gnu_v3_complete_object_ctor
;
2027 kind
= gnu_v3_base_object_ctor
;
2030 kind
= gnu_v3_complete_object_allocating_ctor
;
2033 kind
= gnu_v3_object_ctor_group
;
2039 return d_make_ctor (di
, kind
, di
->last_name
);
2044 enum gnu_v3_dtor_kinds kind
;
2046 switch (d_peek_next_char (di
))
2049 kind
= gnu_v3_deleting_dtor
;
2052 kind
= gnu_v3_complete_object_dtor
;
2055 kind
= gnu_v3_base_object_dtor
;
2058 kind
= gnu_v3_object_dtor_group
;
2064 return d_make_dtor (di
, kind
, di
->last_name
);
2072 /* <type> ::= <builtin-type>
2074 ::= <class-enum-type>
2076 ::= <pointer-to-member-type>
2077 ::= <template-param>
2078 ::= <template-template-param> <template-args>
2080 ::= <CV-qualifiers> <type>
2083 ::= O <type> (C++0x)
2086 ::= U <source-name> <type>
2088 <builtin-type> ::= various one letter codes
2092 CP_STATIC_IF_GLIBCPP_V3
2093 const struct demangle_builtin_type_info
2094 cplus_demangle_builtin_types
[D_BUILTIN_TYPE_COUNT
] =
2096 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT
},
2097 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL
},
2098 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT
},
2099 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT
},
2100 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT
},
2101 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT
},
2102 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT
},
2103 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT
},
2104 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT
},
2105 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED
},
2106 /* k */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2107 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG
},
2108 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG
},
2109 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT
},
2110 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
2112 /* p */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2113 /* q */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2114 /* r */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2115 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT
},
2116 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT
},
2117 /* u */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2118 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID
},
2119 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT
},
2120 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG
},
2121 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
2122 D_PRINT_UNSIGNED_LONG_LONG
},
2123 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT
},
2124 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT
},
2125 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT
},
2126 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT
},
2127 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT
},
2128 /* 30 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT
},
2129 /* 31 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT
},
2130 /* 32 */ { NL ("decltype(nullptr)"), NL ("decltype(nullptr)"),
2134 CP_STATIC_IF_GLIBCPP_V3
2135 struct demangle_component
*
2136 cplus_demangle_type (struct d_info
*di
)
2139 struct demangle_component
*ret
;
2142 /* The ABI specifies that when CV-qualifiers are used, the base type
2143 is substitutable, and the fully qualified type is substitutable,
2144 but the base type with a strict subset of the CV-qualifiers is
2145 not substitutable. The natural recursive implementation of the
2146 CV-qualifiers would cause subsets to be substitutable, so instead
2147 we pull them all off now.
2149 FIXME: The ABI says that order-insensitive vendor qualifiers
2150 should be handled in the same way, but we have no way to tell
2151 which vendor qualifiers are order-insensitive and which are
2152 order-sensitive. So we just assume that they are all
2153 order-sensitive. g++ 3.4 supports only one vendor qualifier,
2154 __vector, and it treats it as order-sensitive when mangling
2157 peek
= d_peek_char (di
);
2158 if (peek
== 'r' || peek
== 'V' || peek
== 'K')
2160 struct demangle_component
**pret
;
2162 pret
= d_cv_qualifiers (di
, &ret
, 0);
2165 *pret
= cplus_demangle_type (di
);
2166 if (! *pret
|| ! d_add_substitution (di
, ret
))
2175 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2176 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
2177 case 'o': case 's': case 't':
2178 case 'v': case 'w': case 'x': case 'y': case 'z':
2179 ret
= d_make_builtin_type (di
,
2180 &cplus_demangle_builtin_types
[peek
- 'a']);
2181 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2188 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_VENDOR_TYPE
,
2189 d_source_name (di
), NULL
);
2193 ret
= d_function_type (di
);
2196 case '0': case '1': case '2': case '3': case '4':
2197 case '5': case '6': case '7': case '8': case '9':
2200 ret
= d_class_enum_type (di
);
2204 ret
= d_array_type (di
);
2208 ret
= d_pointer_to_member_type (di
);
2212 ret
= d_template_param (di
);
2213 if (d_peek_char (di
) == 'I')
2215 /* This is <template-template-param> <template-args>. The
2216 <template-template-param> part is a substitution
2218 if (! d_add_substitution (di
, ret
))
2220 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
,
2221 d_template_args (di
));
2226 /* If this is a special substitution, then it is the start of
2227 <class-enum-type>. */
2231 peek_next
= d_peek_next_char (di
);
2232 if (IS_DIGIT (peek_next
)
2234 || IS_UPPER (peek_next
))
2236 ret
= d_substitution (di
, 0);
2237 /* The substituted name may have been a template name and
2238 may be followed by tepmlate args. */
2239 if (d_peek_char (di
) == 'I')
2240 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
,
2241 d_template_args (di
));
2247 ret
= d_class_enum_type (di
);
2248 /* If the substitution was a complete type, then it is not
2249 a new substitution candidate. However, if the
2250 substitution was followed by template arguments, then
2251 the whole thing is a substitution candidate. */
2252 if (ret
!= NULL
&& ret
->type
== DEMANGLE_COMPONENT_SUB_STD
)
2260 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_RVALUE_REFERENCE
,
2261 cplus_demangle_type (di
), NULL
);
2266 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_POINTER
,
2267 cplus_demangle_type (di
), NULL
);
2272 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_REFERENCE
,
2273 cplus_demangle_type (di
), NULL
);
2278 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_COMPLEX
,
2279 cplus_demangle_type (di
), NULL
);
2284 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_IMAGINARY
,
2285 cplus_demangle_type (di
), NULL
);
2290 ret
= d_source_name (di
);
2291 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
,
2292 cplus_demangle_type (di
), ret
);
2298 peek
= d_next_char (di
);
2303 /* decltype (expression) */
2304 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_DECLTYPE
,
2305 d_expression (di
), NULL
);
2306 if (ret
&& d_next_char (di
) != 'E')
2312 /* Pack expansion. */
2313 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_PACK_EXPANSION
,
2314 cplus_demangle_type (di
), NULL
);
2320 ret
= d_make_name (di
, "auto", 4);
2324 /* 32-bit decimal floating point */
2325 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[26]);
2326 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2330 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[27]);
2331 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2335 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[28]);
2336 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2339 /* 16-bit half-precision FP */
2340 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[29]);
2341 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2345 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[30]);
2346 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2350 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[31]);
2351 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2355 /* Fixed point types. DF<int bits><length><fract bits><sat> */
2356 ret
= d_make_empty (di
);
2357 ret
->type
= DEMANGLE_COMPONENT_FIXED_TYPE
;
2358 if ((ret
->u
.s_fixed
.accum
= IS_DIGIT (d_peek_char (di
))))
2359 /* For demangling we don't care about the bits. */
2361 ret
->u
.s_fixed
.length
= cplus_demangle_type (di
);
2362 if (ret
->u
.s_fixed
.length
== NULL
)
2365 peek
= d_next_char (di
);
2366 ret
->u
.s_fixed
.sat
= (peek
== 's');
2370 ret
= d_vector_type (di
);
2375 /* decltype(nullptr) */
2376 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[32]);
2377 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2391 if (! d_add_substitution (di
, ret
))
2398 /* <CV-qualifiers> ::= [r] [V] [K] */
2400 static struct demangle_component
**
2401 d_cv_qualifiers (struct d_info
*di
,
2402 struct demangle_component
**pret
, int member_fn
)
2404 struct demangle_component
**pstart
;
2408 peek
= d_peek_char (di
);
2409 while (peek
== 'r' || peek
== 'V' || peek
== 'K')
2411 enum demangle_component_type t
;
2417 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2418 : DEMANGLE_COMPONENT_RESTRICT
);
2419 di
->expansion
+= sizeof "restrict";
2421 else if (peek
== 'V')
2424 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2425 : DEMANGLE_COMPONENT_VOLATILE
);
2426 di
->expansion
+= sizeof "volatile";
2431 ? DEMANGLE_COMPONENT_CONST_THIS
2432 : DEMANGLE_COMPONENT_CONST
);
2433 di
->expansion
+= sizeof "const";
2436 *pret
= d_make_comp (di
, t
, NULL
, NULL
);
2439 pret
= &d_left (*pret
);
2441 peek
= d_peek_char (di
);
2444 if (!member_fn
&& peek
== 'F')
2446 while (pstart
!= pret
)
2448 switch ((*pstart
)->type
)
2450 case DEMANGLE_COMPONENT_RESTRICT
:
2451 (*pstart
)->type
= DEMANGLE_COMPONENT_RESTRICT_THIS
;
2453 case DEMANGLE_COMPONENT_VOLATILE
:
2454 (*pstart
)->type
= DEMANGLE_COMPONENT_VOLATILE_THIS
;
2456 case DEMANGLE_COMPONENT_CONST
:
2457 (*pstart
)->type
= DEMANGLE_COMPONENT_CONST_THIS
;
2462 pstart
= &d_left (*pstart
);
2469 /* <function-type> ::= F [Y] <bare-function-type> E */
2471 static struct demangle_component
*
2472 d_function_type (struct d_info
*di
)
2474 struct demangle_component
*ret
;
2476 if (! d_check_char (di
, 'F'))
2478 if (d_peek_char (di
) == 'Y')
2480 /* Function has C linkage. We don't print this information.
2481 FIXME: We should print it in verbose mode. */
2484 ret
= d_bare_function_type (di
, 1);
2485 if (! d_check_char (di
, 'E'))
2492 static struct demangle_component
*
2493 d_parmlist (struct d_info
*di
)
2495 struct demangle_component
*tl
;
2496 struct demangle_component
**ptl
;
2502 struct demangle_component
*type
;
2504 char peek
= d_peek_char (di
);
2505 if (peek
== '\0' || peek
== 'E' || peek
== '.')
2507 type
= cplus_demangle_type (di
);
2510 *ptl
= d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, type
, NULL
);
2513 ptl
= &d_right (*ptl
);
2516 /* There should be at least one parameter type besides the optional
2517 return type. A function which takes no arguments will have a
2518 single parameter type void. */
2522 /* If we have a single parameter type void, omit it. */
2523 if (d_right (tl
) == NULL
2524 && d_left (tl
)->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
2525 && d_left (tl
)->u
.s_builtin
.type
->print
== D_PRINT_VOID
)
2527 di
->expansion
-= d_left (tl
)->u
.s_builtin
.type
->len
;
2534 /* <bare-function-type> ::= [J]<type>+ */
2536 static struct demangle_component
*
2537 d_bare_function_type (struct d_info
*di
, int has_return_type
)
2539 struct demangle_component
*return_type
;
2540 struct demangle_component
*tl
;
2543 /* Detect special qualifier indicating that the first argument
2544 is the return type. */
2545 peek
= d_peek_char (di
);
2549 has_return_type
= 1;
2552 if (has_return_type
)
2554 return_type
= cplus_demangle_type (di
);
2555 if (return_type
== NULL
)
2561 tl
= d_parmlist (di
);
2565 return d_make_comp (di
, DEMANGLE_COMPONENT_FUNCTION_TYPE
,
2569 /* <class-enum-type> ::= <name> */
2571 static struct demangle_component
*
2572 d_class_enum_type (struct d_info
*di
)
2577 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2578 ::= A [<(dimension) expression>] _ <(element) type>
2581 static struct demangle_component
*
2582 d_array_type (struct d_info
*di
)
2585 struct demangle_component
*dim
;
2587 if (! d_check_char (di
, 'A'))
2590 peek
= d_peek_char (di
);
2593 else if (IS_DIGIT (peek
))
2601 peek
= d_peek_char (di
);
2603 while (IS_DIGIT (peek
));
2604 dim
= d_make_name (di
, s
, d_str (di
) - s
);
2610 dim
= d_expression (di
);
2615 if (! d_check_char (di
, '_'))
2618 return d_make_comp (di
, DEMANGLE_COMPONENT_ARRAY_TYPE
, dim
,
2619 cplus_demangle_type (di
));
2622 /* <vector-type> ::= Dv <number> _ <type>
2623 ::= Dv _ <expression> _ <type> */
2625 static struct demangle_component
*
2626 d_vector_type (struct d_info
*di
)
2629 struct demangle_component
*dim
;
2631 peek
= d_peek_char (di
);
2635 dim
= d_expression (di
);
2638 dim
= d_number_component (di
);
2643 if (! d_check_char (di
, '_'))
2646 return d_make_comp (di
, DEMANGLE_COMPONENT_VECTOR_TYPE
, dim
,
2647 cplus_demangle_type (di
));
2650 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2652 static struct demangle_component
*
2653 d_pointer_to_member_type (struct d_info
*di
)
2655 struct demangle_component
*cl
;
2656 struct demangle_component
*mem
;
2657 struct demangle_component
**pmem
;
2659 if (! d_check_char (di
, 'M'))
2662 cl
= cplus_demangle_type (di
);
2664 /* The ABI specifies that any type can be a substitution source, and
2665 that M is followed by two types, and that when a CV-qualified
2666 type is seen both the base type and the CV-qualified types are
2667 substitution sources. The ABI also specifies that for a pointer
2668 to a CV-qualified member function, the qualifiers are attached to
2669 the second type. Given the grammar, a plain reading of the ABI
2670 suggests that both the CV-qualified member function and the
2671 non-qualified member function are substitution sources. However,
2672 g++ does not work that way. g++ treats only the CV-qualified
2673 member function as a substitution source. FIXME. So to work
2674 with g++, we need to pull off the CV-qualifiers here, in order to
2675 avoid calling add_substitution() in cplus_demangle_type(). But
2676 for a CV-qualified member which is not a function, g++ does
2677 follow the ABI, so we need to handle that case here by calling
2678 d_add_substitution ourselves. */
2680 pmem
= d_cv_qualifiers (di
, &mem
, 1);
2683 *pmem
= cplus_demangle_type (di
);
2687 if (pmem
!= &mem
&& (*pmem
)->type
!= DEMANGLE_COMPONENT_FUNCTION_TYPE
)
2689 if (! d_add_substitution (di
, mem
))
2693 return d_make_comp (di
, DEMANGLE_COMPONENT_PTRMEM_TYPE
, cl
, mem
);
2696 /* <non-negative number> _ */
2699 d_compact_number (struct d_info
*di
)
2702 if (d_peek_char (di
) == '_')
2704 else if (d_peek_char (di
) == 'n')
2707 num
= d_number (di
) + 1;
2709 if (! d_check_char (di
, '_'))
2714 /* <template-param> ::= T_
2715 ::= T <(parameter-2 non-negative) number> _
2718 static struct demangle_component
*
2719 d_template_param (struct d_info
*di
)
2723 if (! d_check_char (di
, 'T'))
2726 param
= d_compact_number (di
);
2732 return d_make_template_param (di
, param
);
2735 /* <template-args> ::= I <template-arg>+ E */
2737 static struct demangle_component
*
2738 d_template_args (struct d_info
*di
)
2740 struct demangle_component
*hold_last_name
;
2741 struct demangle_component
*al
;
2742 struct demangle_component
**pal
;
2744 /* Preserve the last name we saw--don't let the template arguments
2745 clobber it, as that would give us the wrong name for a subsequent
2746 constructor or destructor. */
2747 hold_last_name
= di
->last_name
;
2749 if (d_peek_char (di
) != 'I'
2750 && d_peek_char (di
) != 'J')
2754 if (d_peek_char (di
) == 'E')
2756 /* An argument pack can be empty. */
2758 return d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
, NULL
, NULL
);
2765 struct demangle_component
*a
;
2767 a
= d_template_arg (di
);
2771 *pal
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
, a
, NULL
);
2774 pal
= &d_right (*pal
);
2776 if (d_peek_char (di
) == 'E')
2783 di
->last_name
= hold_last_name
;
2788 /* <template-arg> ::= <type>
2789 ::= X <expression> E
2793 static struct demangle_component
*
2794 d_template_arg (struct d_info
*di
)
2796 struct demangle_component
*ret
;
2798 switch (d_peek_char (di
))
2802 ret
= d_expression (di
);
2803 if (! d_check_char (di
, 'E'))
2808 return d_expr_primary (di
);
2812 /* An argument pack. */
2813 return d_template_args (di
);
2816 return cplus_demangle_type (di
);
2820 /* Parse a sequence of expressions until we hit the terminator
2823 static struct demangle_component
*
2824 d_exprlist (struct d_info
*di
, char terminator
)
2826 struct demangle_component
*list
= NULL
;
2827 struct demangle_component
**p
= &list
;
2829 if (d_peek_char (di
) == terminator
)
2832 return d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, NULL
, NULL
);
2837 struct demangle_component
*arg
= d_expression (di
);
2841 *p
= d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, arg
, NULL
);
2846 if (d_peek_char (di
) == terminator
)
2856 /* Returns nonzero iff OP is an operator for a C++ cast: const_cast,
2857 dynamic_cast, static_cast or reinterpret_cast. */
2860 op_is_new_cast (struct demangle_component
*op
)
2862 const char *code
= op
->u
.s_operator
.op
->code
;
2863 return (code
[1] == 'c'
2864 && (code
[0] == 's' || code
[0] == 'd'
2865 || code
[0] == 'c' || code
[0] == 'r'));
2868 /* <expression> ::= <(unary) operator-name> <expression>
2869 ::= <(binary) operator-name> <expression> <expression>
2870 ::= <(trinary) operator-name> <expression> <expression> <expression>
2871 ::= cl <expression>+ E
2873 ::= <template-param>
2874 ::= sr <type> <unqualified-name>
2875 ::= sr <type> <unqualified-name> <template-args>
2879 static struct demangle_component
*
2880 d_expression (struct d_info
*di
)
2884 peek
= d_peek_char (di
);
2886 return d_expr_primary (di
);
2887 else if (peek
== 'T')
2888 return d_template_param (di
);
2889 else if (peek
== 's' && d_peek_next_char (di
) == 'r')
2891 struct demangle_component
*type
;
2892 struct demangle_component
*name
;
2895 type
= cplus_demangle_type (di
);
2896 name
= d_unqualified_name (di
);
2897 if (d_peek_char (di
) != 'I')
2898 return d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
, type
, name
);
2900 return d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
, type
,
2901 d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, name
,
2902 d_template_args (di
)));
2904 else if (peek
== 's' && d_peek_next_char (di
) == 'p')
2907 return d_make_comp (di
, DEMANGLE_COMPONENT_PACK_EXPANSION
,
2908 d_expression (di
), NULL
);
2910 else if (peek
== 'f' && d_peek_next_char (di
) == 'p')
2912 /* Function parameter used in a late-specified return type. */
2915 if (d_peek_char (di
) == 'T')
2917 /* 'this' parameter. */
2923 index
= d_compact_number (di
) + 1;
2927 return d_make_function_param (di
, index
);
2929 else if (IS_DIGIT (peek
)
2930 || (peek
== 'o' && d_peek_next_char (di
) == 'n'))
2932 /* We can get an unqualified name as an expression in the case of
2933 a dependent function call, i.e. decltype(f(t)). */
2934 struct demangle_component
*name
;
2937 /* operator-function-id, i.e. operator+(t). */
2940 name
= d_unqualified_name (di
);
2943 if (d_peek_char (di
) == 'I')
2944 return d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, name
,
2945 d_template_args (di
));
2949 else if ((peek
== 'i' || peek
== 't')
2950 && d_peek_next_char (di
) == 'l')
2952 /* Brace-enclosed initializer list, untyped or typed. */
2953 struct demangle_component
*type
= NULL
;
2955 type
= cplus_demangle_type (di
);
2957 return d_make_comp (di
, DEMANGLE_COMPONENT_INITIALIZER_LIST
,
2958 type
, d_exprlist (di
, 'E'));
2962 struct demangle_component
*op
;
2963 const char *code
= NULL
;
2966 op
= d_operator_name (di
);
2970 if (op
->type
== DEMANGLE_COMPONENT_OPERATOR
)
2972 code
= op
->u
.s_operator
.op
->code
;
2973 di
->expansion
+= op
->u
.s_operator
.op
->len
- 2;
2974 if (strcmp (code
, "st") == 0)
2975 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
2976 cplus_demangle_type (di
));
2983 case DEMANGLE_COMPONENT_OPERATOR
:
2984 args
= op
->u
.s_operator
.op
->args
;
2986 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
2987 args
= op
->u
.s_extended_operator
.args
;
2989 case DEMANGLE_COMPONENT_CAST
:
2997 return d_make_comp (di
, DEMANGLE_COMPONENT_NULLARY
, op
, NULL
);
3001 struct demangle_component
*operand
;
3004 if (code
&& (code
[0] == 'p' || code
[0] == 'm')
3005 && code
[1] == code
[0])
3006 /* pp_ and mm_ are the prefix variants. */
3007 suffix
= !d_check_char (di
, '_');
3009 if (op
->type
== DEMANGLE_COMPONENT_CAST
3010 && d_check_char (di
, '_'))
3011 operand
= d_exprlist (di
, 'E');
3013 operand
= d_expression (di
);
3016 /* Indicate the suffix variant for d_print_comp. */
3017 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
3019 DEMANGLE_COMPONENT_BINARY_ARGS
,
3022 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
3027 struct demangle_component
*left
;
3028 struct demangle_component
*right
;
3030 if (op_is_new_cast (op
))
3031 left
= cplus_demangle_type (di
);
3033 left
= d_expression (di
);
3034 if (!strcmp (code
, "cl"))
3035 right
= d_exprlist (di
, 'E');
3036 else if (!strcmp (code
, "dt") || !strcmp (code
, "pt"))
3038 right
= d_unqualified_name (di
);
3039 if (d_peek_char (di
) == 'I')
3040 right
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
,
3041 right
, d_template_args (di
));
3044 right
= d_expression (di
);
3046 return d_make_comp (di
, DEMANGLE_COMPONENT_BINARY
, op
,
3048 DEMANGLE_COMPONENT_BINARY_ARGS
,
3053 struct demangle_component
*first
;
3054 struct demangle_component
*second
;
3055 struct demangle_component
*third
;
3057 if (!strcmp (code
, "qu"))
3059 /* ?: expression. */
3060 first
= d_expression (di
);
3061 second
= d_expression (di
);
3062 third
= d_expression (di
);
3064 else if (code
[0] == 'n')
3066 /* new-expression. */
3067 if (code
[1] != 'w' && code
[1] != 'a')
3069 first
= d_exprlist (di
, '_');
3070 second
= cplus_demangle_type (di
);
3071 if (d_peek_char (di
) == 'E')
3076 else if (d_peek_char (di
) == 'p'
3077 && d_peek_next_char (di
) == 'i')
3079 /* Parenthesized initializer. */
3081 third
= d_exprlist (di
, 'E');
3083 else if (d_peek_char (di
) == 'i'
3084 && d_peek_next_char (di
) == 'l')
3085 /* initializer-list. */
3086 third
= d_expression (di
);
3092 return d_make_comp (di
, DEMANGLE_COMPONENT_TRINARY
, op
,
3094 DEMANGLE_COMPONENT_TRINARY_ARG1
,
3097 DEMANGLE_COMPONENT_TRINARY_ARG2
,
3106 /* <expr-primary> ::= L <type> <(value) number> E
3107 ::= L <type> <(value) float> E
3108 ::= L <mangled-name> E
3111 static struct demangle_component
*
3112 d_expr_primary (struct d_info
*di
)
3114 struct demangle_component
*ret
;
3116 if (! d_check_char (di
, 'L'))
3118 if (d_peek_char (di
) == '_'
3119 /* Workaround for G++ bug; see comment in write_template_arg. */
3120 || d_peek_char (di
) == 'Z')
3121 ret
= cplus_demangle_mangled_name (di
, 0);
3124 struct demangle_component
*type
;
3125 enum demangle_component_type t
;
3128 type
= cplus_demangle_type (di
);
3132 /* If we have a type we know how to print, we aren't going to
3133 print the type name itself. */
3134 if (type
->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
3135 && type
->u
.s_builtin
.type
->print
!= D_PRINT_DEFAULT
)
3136 di
->expansion
-= type
->u
.s_builtin
.type
->len
;
3138 /* Rather than try to interpret the literal value, we just
3139 collect it as a string. Note that it's possible to have a
3140 floating point literal here. The ABI specifies that the
3141 format of such literals is machine independent. That's fine,
3142 but what's not fine is that versions of g++ up to 3.2 with
3143 -fabi-version=1 used upper case letters in the hex constant,
3144 and dumped out gcc's internal representation. That makes it
3145 hard to tell where the constant ends, and hard to dump the
3146 constant in any readable form anyhow. We don't attempt to
3147 handle these cases. */
3149 t
= DEMANGLE_COMPONENT_LITERAL
;
3150 if (d_peek_char (di
) == 'n')
3152 t
= DEMANGLE_COMPONENT_LITERAL_NEG
;
3156 while (d_peek_char (di
) != 'E')
3158 if (d_peek_char (di
) == '\0')
3162 ret
= d_make_comp (di
, t
, type
, d_make_name (di
, s
, d_str (di
) - s
));
3164 if (! d_check_char (di
, 'E'))
3169 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
3170 ::= Z <(function) encoding> E s [<discriminator>]
3173 static struct demangle_component
*
3174 d_local_name (struct d_info
*di
)
3176 struct demangle_component
*function
;
3178 if (! d_check_char (di
, 'Z'))
3181 function
= d_encoding (di
, 0);
3183 if (! d_check_char (di
, 'E'))
3186 if (d_peek_char (di
) == 's')
3189 if (! d_discriminator (di
))
3191 return d_make_comp (di
, DEMANGLE_COMPONENT_LOCAL_NAME
, function
,
3192 d_make_name (di
, "string literal",
3193 sizeof "string literal" - 1));
3197 struct demangle_component
*name
;
3200 if (d_peek_char (di
) == 'd')
3202 /* Default argument scope: d <number> _. */
3204 num
= d_compact_number (di
);
3213 /* Lambdas and unnamed types have internal discriminators. */
3214 case DEMANGLE_COMPONENT_LAMBDA
:
3215 case DEMANGLE_COMPONENT_UNNAMED_TYPE
:
3218 if (! d_discriminator (di
))
3222 name
= d_make_default_arg (di
, num
, name
);
3223 return d_make_comp (di
, DEMANGLE_COMPONENT_LOCAL_NAME
, function
, name
);
3227 /* <discriminator> ::= _ <(non-negative) number>
3229 We demangle the discriminator, but we don't print it out. FIXME:
3230 We should print it out in verbose mode. */
3233 d_discriminator (struct d_info
*di
)
3237 if (d_peek_char (di
) != '_')
3240 discrim
= d_number (di
);
3246 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ */
3248 static struct demangle_component
*
3249 d_lambda (struct d_info
*di
)
3251 struct demangle_component
*tl
;
3252 struct demangle_component
*ret
;
3255 if (! d_check_char (di
, 'U'))
3257 if (! d_check_char (di
, 'l'))
3260 tl
= d_parmlist (di
);
3264 if (! d_check_char (di
, 'E'))
3267 num
= d_compact_number (di
);
3271 ret
= d_make_empty (di
);
3274 ret
->type
= DEMANGLE_COMPONENT_LAMBDA
;
3275 ret
->u
.s_unary_num
.sub
= tl
;
3276 ret
->u
.s_unary_num
.num
= num
;
3279 if (! d_add_substitution (di
, ret
))
3285 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
3287 static struct demangle_component
*
3288 d_unnamed_type (struct d_info
*di
)
3290 struct demangle_component
*ret
;
3293 if (! d_check_char (di
, 'U'))
3295 if (! d_check_char (di
, 't'))
3298 num
= d_compact_number (di
);
3302 ret
= d_make_empty (di
);
3305 ret
->type
= DEMANGLE_COMPONENT_UNNAMED_TYPE
;
3306 ret
->u
.s_number
.number
= num
;
3309 if (! d_add_substitution (di
, ret
))
3315 /* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]*
3318 static struct demangle_component
*
3319 d_clone_suffix (struct d_info
*di
, struct demangle_component
*encoding
)
3321 const char *suffix
= d_str (di
);
3322 const char *pend
= suffix
;
3323 struct demangle_component
*n
;
3325 if (*pend
== '.' && (IS_LOWER (pend
[1]) || pend
[1] == '_'))
3328 while (IS_LOWER (*pend
) || *pend
== '_')
3331 while (*pend
== '.' && IS_DIGIT (pend
[1]))
3334 while (IS_DIGIT (*pend
))
3337 d_advance (di
, pend
- suffix
);
3338 n
= d_make_name (di
, suffix
, pend
- suffix
);
3339 return d_make_comp (di
, DEMANGLE_COMPONENT_CLONE
, encoding
, n
);
3342 /* Add a new substitution. */
3345 d_add_substitution (struct d_info
*di
, struct demangle_component
*dc
)
3349 if (di
->next_sub
>= di
->num_subs
)
3351 di
->subs
[di
->next_sub
] = dc
;
3356 /* <substitution> ::= S <seq-id> _
3366 If PREFIX is non-zero, then this type is being used as a prefix in
3367 a qualified name. In this case, for the standard substitutions, we
3368 need to check whether we are being used as a prefix for a
3369 constructor or destructor, and return a full template name.
3370 Otherwise we will get something like std::iostream::~iostream()
3371 which does not correspond particularly well to any function which
3372 actually appears in the source.
3375 static const struct d_standard_sub_info standard_subs
[] =
3380 { 'a', NL ("std::allocator"),
3381 NL ("std::allocator"),
3383 { 'b', NL ("std::basic_string"),
3384 NL ("std::basic_string"),
3385 NL ("basic_string") },
3386 { 's', NL ("std::string"),
3387 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
3388 NL ("basic_string") },
3389 { 'i', NL ("std::istream"),
3390 NL ("std::basic_istream<char, std::char_traits<char> >"),
3391 NL ("basic_istream") },
3392 { 'o', NL ("std::ostream"),
3393 NL ("std::basic_ostream<char, std::char_traits<char> >"),
3394 NL ("basic_ostream") },
3395 { 'd', NL ("std::iostream"),
3396 NL ("std::basic_iostream<char, std::char_traits<char> >"),
3397 NL ("basic_iostream") }
3400 static struct demangle_component
*
3401 d_substitution (struct d_info
*di
, int prefix
)
3405 if (! d_check_char (di
, 'S'))
3408 c
= d_next_char (di
);
3409 if (c
== '_' || IS_DIGIT (c
) || IS_UPPER (c
))
3418 unsigned int new_id
;
3421 new_id
= id
* 36 + c
- '0';
3422 else if (IS_UPPER (c
))
3423 new_id
= id
* 36 + c
- 'A' + 10;
3429 c
= d_next_char (di
);
3436 if (id
>= (unsigned int) di
->next_sub
)
3441 return di
->subs
[id
];
3446 const struct d_standard_sub_info
*p
;
3447 const struct d_standard_sub_info
*pend
;
3449 verbose
= (di
->options
& DMGL_VERBOSE
) != 0;
3450 if (! verbose
&& prefix
)
3454 peek
= d_peek_char (di
);
3455 if (peek
== 'C' || peek
== 'D')
3459 pend
= (&standard_subs
[0]
3460 + sizeof standard_subs
/ sizeof standard_subs
[0]);
3461 for (p
= &standard_subs
[0]; p
< pend
; ++p
)
3468 if (p
->set_last_name
!= NULL
)
3469 di
->last_name
= d_make_sub (di
, p
->set_last_name
,
3470 p
->set_last_name_len
);
3473 s
= p
->full_expansion
;
3478 s
= p
->simple_expansion
;
3479 len
= p
->simple_len
;
3481 di
->expansion
+= len
;
3482 return d_make_sub (di
, s
, len
);
3490 /* Initialize a growable string. */
3493 d_growable_string_init (struct d_growable_string
*dgs
, size_t estimate
)
3498 dgs
->allocation_failure
= 0;
3501 d_growable_string_resize (dgs
, estimate
);
3504 /* Grow a growable string to a given size. */
3507 d_growable_string_resize (struct d_growable_string
*dgs
, size_t need
)
3512 if (dgs
->allocation_failure
)
3515 /* Start allocation at two bytes to avoid any possibility of confusion
3516 with the special value of 1 used as a return in *palc to indicate
3517 allocation failures. */
3518 newalc
= dgs
->alc
> 0 ? dgs
->alc
: 2;
3519 while (newalc
< need
)
3522 newbuf
= (char *) realloc (dgs
->buf
, newalc
);
3529 dgs
->allocation_failure
= 1;
3536 /* Append a buffer to a growable string. */
3539 d_growable_string_append_buffer (struct d_growable_string
*dgs
,
3540 const char *s
, size_t l
)
3544 need
= dgs
->len
+ l
+ 1;
3545 if (need
> dgs
->alc
)
3546 d_growable_string_resize (dgs
, need
);
3548 if (dgs
->allocation_failure
)
3551 memcpy (dgs
->buf
+ dgs
->len
, s
, l
);
3552 dgs
->buf
[dgs
->len
+ l
] = '\0';
3556 /* Bridge growable strings to the callback mechanism. */
3559 d_growable_string_callback_adapter (const char *s
, size_t l
, void *opaque
)
3561 struct d_growable_string
*dgs
= (struct d_growable_string
*) opaque
;
3563 d_growable_string_append_buffer (dgs
, s
, l
);
3566 /* Initialize a print information structure. */
3569 d_print_init (struct d_print_info
*dpi
, demangle_callbackref callback
,
3573 dpi
->last_char
= '\0';
3574 dpi
->templates
= NULL
;
3575 dpi
->modifiers
= NULL
;
3576 dpi
->pack_index
= 0;
3577 dpi
->flush_count
= 0;
3579 dpi
->callback
= callback
;
3580 dpi
->opaque
= opaque
;
3582 dpi
->demangle_failure
= 0;
3585 /* Indicate that an error occurred during printing, and test for error. */
3588 d_print_error (struct d_print_info
*dpi
)
3590 dpi
->demangle_failure
= 1;
3594 d_print_saw_error (struct d_print_info
*dpi
)
3596 return dpi
->demangle_failure
!= 0;
3599 /* Flush buffered characters to the callback. */
3602 d_print_flush (struct d_print_info
*dpi
)
3604 dpi
->buf
[dpi
->len
] = '\0';
3605 dpi
->callback (dpi
->buf
, dpi
->len
, dpi
->opaque
);
3610 /* Append characters and buffers for printing. */
3613 d_append_char (struct d_print_info
*dpi
, char c
)
3615 if (dpi
->len
== sizeof (dpi
->buf
) - 1)
3616 d_print_flush (dpi
);
3618 dpi
->buf
[dpi
->len
++] = c
;
3623 d_append_buffer (struct d_print_info
*dpi
, const char *s
, size_t l
)
3627 for (i
= 0; i
< l
; i
++)
3628 d_append_char (dpi
, s
[i
]);
3632 d_append_string (struct d_print_info
*dpi
, const char *s
)
3634 d_append_buffer (dpi
, s
, strlen (s
));
3638 d_append_num (struct d_print_info
*dpi
, long l
)
3641 sprintf (buf
,"%ld", l
);
3642 d_append_string (dpi
, buf
);
3646 d_last_char (struct d_print_info
*dpi
)
3648 return dpi
->last_char
;
3651 /* Turn components into a human readable string. OPTIONS is the
3652 options bits passed to the demangler. DC is the tree to print.
3653 CALLBACK is a function to call to flush demangled string segments
3654 as they fill the intermediate buffer, and OPAQUE is a generalized
3655 callback argument. On success, this returns 1. On failure,
3656 it returns 0, indicating a bad parse. It does not use heap
3657 memory to build an output string, so cannot encounter memory
3658 allocation failure. */
3660 CP_STATIC_IF_GLIBCPP_V3
3662 cplus_demangle_print_callback (int options
,
3663 const struct demangle_component
*dc
,
3664 demangle_callbackref callback
, void *opaque
)
3666 struct d_print_info dpi
;
3668 d_print_init (&dpi
, callback
, opaque
);
3670 d_print_comp (&dpi
, options
, dc
);
3672 d_print_flush (&dpi
);
3674 return ! d_print_saw_error (&dpi
);
3677 /* Turn components into a human readable string. OPTIONS is the
3678 options bits passed to the demangler. DC is the tree to print.
3679 ESTIMATE is a guess at the length of the result. This returns a
3680 string allocated by malloc, or NULL on error. On success, this
3681 sets *PALC to the size of the allocated buffer. On failure, this
3682 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
3685 CP_STATIC_IF_GLIBCPP_V3
3687 cplus_demangle_print (int options
, const struct demangle_component
*dc
,
3688 int estimate
, size_t *palc
)
3690 struct d_growable_string dgs
;
3692 d_growable_string_init (&dgs
, estimate
);
3694 if (! cplus_demangle_print_callback (options
, dc
,
3695 d_growable_string_callback_adapter
,
3703 *palc
= dgs
.allocation_failure
? 1 : dgs
.alc
;
3707 /* Returns the I'th element of the template arglist ARGS, or NULL on
3710 static struct demangle_component
*
3711 d_index_template_argument (struct demangle_component
*args
, int i
)
3713 struct demangle_component
*a
;
3719 if (a
->type
!= DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
3725 if (i
!= 0 || a
== NULL
)
3731 /* Returns the template argument from the current context indicated by DC,
3732 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
3734 static struct demangle_component
*
3735 d_lookup_template_argument (struct d_print_info
*dpi
,
3736 const struct demangle_component
*dc
)
3738 if (dpi
->templates
== NULL
)
3740 d_print_error (dpi
);
3744 return d_index_template_argument
3745 (d_right (dpi
->templates
->template_decl
),
3746 dc
->u
.s_number
.number
);
3749 /* Returns a template argument pack used in DC (any will do), or NULL. */
3751 static struct demangle_component
*
3752 d_find_pack (struct d_print_info
*dpi
,
3753 const struct demangle_component
*dc
)
3755 struct demangle_component
*a
;
3761 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
3762 a
= d_lookup_template_argument (dpi
, dc
);
3763 if (a
&& a
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
3767 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
3770 case DEMANGLE_COMPONENT_LAMBDA
:
3771 case DEMANGLE_COMPONENT_NAME
:
3772 case DEMANGLE_COMPONENT_TAGGED_NAME
:
3773 case DEMANGLE_COMPONENT_OPERATOR
:
3774 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
3775 case DEMANGLE_COMPONENT_SUB_STD
:
3776 case DEMANGLE_COMPONENT_CHARACTER
:
3777 case DEMANGLE_COMPONENT_FUNCTION_PARAM
:
3778 case DEMANGLE_COMPONENT_UNNAMED_TYPE
:
3781 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
3782 return d_find_pack (dpi
, dc
->u
.s_extended_operator
.name
);
3783 case DEMANGLE_COMPONENT_CTOR
:
3784 return d_find_pack (dpi
, dc
->u
.s_ctor
.name
);
3785 case DEMANGLE_COMPONENT_DTOR
:
3786 return d_find_pack (dpi
, dc
->u
.s_dtor
.name
);
3789 a
= d_find_pack (dpi
, d_left (dc
));
3792 return d_find_pack (dpi
, d_right (dc
));
3796 /* Returns the length of the template argument pack DC. */
3799 d_pack_length (const struct demangle_component
*dc
)
3802 while (dc
&& dc
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
3803 && d_left (dc
) != NULL
)
3811 /* DC is a component of a mangled expression. Print it, wrapped in parens
3815 d_print_subexpr (struct d_print_info
*dpi
, int options
,
3816 const struct demangle_component
*dc
)
3819 if (dc
->type
== DEMANGLE_COMPONENT_NAME
3820 || dc
->type
== DEMANGLE_COMPONENT_QUAL_NAME
3821 || dc
->type
== DEMANGLE_COMPONENT_INITIALIZER_LIST
3822 || dc
->type
== DEMANGLE_COMPONENT_FUNCTION_PARAM
)
3825 d_append_char (dpi
, '(');
3826 d_print_comp (dpi
, options
, dc
);
3828 d_append_char (dpi
, ')');
3831 /* Subroutine to handle components. */
3834 d_print_comp (struct d_print_info
*dpi
, int options
,
3835 const struct demangle_component
*dc
)
3837 /* Magic variable to let reference smashing skip over the next modifier
3838 without needing to modify *dc. */
3839 const struct demangle_component
*mod_inner
= NULL
;
3843 d_print_error (dpi
);
3846 if (d_print_saw_error (dpi
))
3851 case DEMANGLE_COMPONENT_NAME
:
3852 if ((options
& DMGL_JAVA
) == 0)
3853 d_append_buffer (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
3855 d_print_java_identifier (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
3858 case DEMANGLE_COMPONENT_TAGGED_NAME
:
3859 d_print_comp (dpi
, options
, d_left (dc
));
3860 d_append_string (dpi
, "[abi:");
3861 d_print_comp (dpi
, options
, d_right (dc
));
3862 d_append_char (dpi
, ']');
3865 case DEMANGLE_COMPONENT_QUAL_NAME
:
3866 case DEMANGLE_COMPONENT_LOCAL_NAME
:
3867 d_print_comp (dpi
, options
, d_left (dc
));
3868 if ((options
& DMGL_JAVA
) == 0)
3869 d_append_string (dpi
, "::");
3871 d_append_char (dpi
, '.');
3872 d_print_comp (dpi
, options
, d_right (dc
));
3875 case DEMANGLE_COMPONENT_TYPED_NAME
:
3877 struct d_print_mod
*hold_modifiers
;
3878 struct demangle_component
*typed_name
;
3879 struct d_print_mod adpm
[4];
3881 struct d_print_template dpt
;
3883 /* Pass the name down to the type so that it can be printed in
3884 the right place for the type. We also have to pass down
3885 any CV-qualifiers, which apply to the this parameter. */
3886 hold_modifiers
= dpi
->modifiers
;
3889 typed_name
= d_left (dc
);
3890 while (typed_name
!= NULL
)
3892 if (i
>= sizeof adpm
/ sizeof adpm
[0])
3894 d_print_error (dpi
);
3898 adpm
[i
].next
= dpi
->modifiers
;
3899 dpi
->modifiers
= &adpm
[i
];
3900 adpm
[i
].mod
= typed_name
;
3901 adpm
[i
].printed
= 0;
3902 adpm
[i
].templates
= dpi
->templates
;
3905 if (typed_name
->type
!= DEMANGLE_COMPONENT_RESTRICT_THIS
3906 && typed_name
->type
!= DEMANGLE_COMPONENT_VOLATILE_THIS
3907 && typed_name
->type
!= DEMANGLE_COMPONENT_CONST_THIS
)
3910 typed_name
= d_left (typed_name
);
3913 if (typed_name
== NULL
)
3915 d_print_error (dpi
);
3919 /* If typed_name is a template, then it applies to the
3920 function type as well. */
3921 if (typed_name
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
3923 dpt
.next
= dpi
->templates
;
3924 dpi
->templates
= &dpt
;
3925 dpt
.template_decl
= typed_name
;
3928 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
3929 there may be CV-qualifiers on its right argument which
3930 really apply here; this happens when parsing a class which
3931 is local to a function. */
3932 if (typed_name
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
3934 struct demangle_component
*local_name
;
3936 local_name
= d_right (typed_name
);
3937 if (local_name
->type
== DEMANGLE_COMPONENT_DEFAULT_ARG
)
3938 local_name
= local_name
->u
.s_unary_num
.sub
;
3939 while (local_name
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
3940 || local_name
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
3941 || local_name
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
3943 if (i
>= sizeof adpm
/ sizeof adpm
[0])
3945 d_print_error (dpi
);
3949 adpm
[i
] = adpm
[i
- 1];
3950 adpm
[i
].next
= &adpm
[i
- 1];
3951 dpi
->modifiers
= &adpm
[i
];
3953 adpm
[i
- 1].mod
= local_name
;
3954 adpm
[i
- 1].printed
= 0;
3955 adpm
[i
- 1].templates
= dpi
->templates
;
3958 local_name
= d_left (local_name
);
3962 d_print_comp (dpi
, options
, d_right (dc
));
3964 if (typed_name
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
3965 dpi
->templates
= dpt
.next
;
3967 /* If the modifiers didn't get printed by the type, print them
3972 if (! adpm
[i
].printed
)
3974 d_append_char (dpi
, ' ');
3975 d_print_mod (dpi
, options
, adpm
[i
].mod
);
3979 dpi
->modifiers
= hold_modifiers
;
3984 case DEMANGLE_COMPONENT_TEMPLATE
:
3986 struct d_print_mod
*hold_dpm
;
3987 struct demangle_component
*dcl
;
3989 /* Don't push modifiers into a template definition. Doing so
3990 could give the wrong definition for a template argument.
3991 Instead, treat the template essentially as a name. */
3993 hold_dpm
= dpi
->modifiers
;
3994 dpi
->modifiers
= NULL
;
3998 if ((options
& DMGL_JAVA
) != 0
3999 && dcl
->type
== DEMANGLE_COMPONENT_NAME
4000 && dcl
->u
.s_name
.len
== 6
4001 && strncmp (dcl
->u
.s_name
.s
, "JArray", 6) == 0)
4003 /* Special-case Java arrays, so that JArray<TYPE> appears
4004 instead as TYPE[]. */
4006 d_print_comp (dpi
, options
, d_right (dc
));
4007 d_append_string (dpi
, "[]");
4011 d_print_comp (dpi
, options
, dcl
);
4012 if (d_last_char (dpi
) == '<')
4013 d_append_char (dpi
, ' ');
4014 d_append_char (dpi
, '<');
4015 d_print_comp (dpi
, options
, d_right (dc
));
4016 /* Avoid generating two consecutive '>' characters, to avoid
4017 the C++ syntactic ambiguity. */
4018 if (d_last_char (dpi
) == '>')
4019 d_append_char (dpi
, ' ');
4020 d_append_char (dpi
, '>');
4023 dpi
->modifiers
= hold_dpm
;
4028 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
4030 struct d_print_template
*hold_dpt
;
4031 struct demangle_component
*a
= d_lookup_template_argument (dpi
, dc
);
4033 if (a
&& a
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
4034 a
= d_index_template_argument (a
, dpi
->pack_index
);
4038 d_print_error (dpi
);
4042 /* While processing this parameter, we need to pop the list of
4043 templates. This is because the template parameter may
4044 itself be a reference to a parameter of an outer
4047 hold_dpt
= dpi
->templates
;
4048 dpi
->templates
= hold_dpt
->next
;
4050 d_print_comp (dpi
, options
, a
);
4052 dpi
->templates
= hold_dpt
;
4057 case DEMANGLE_COMPONENT_CTOR
:
4058 d_print_comp (dpi
, options
, dc
->u
.s_ctor
.name
);
4061 case DEMANGLE_COMPONENT_DTOR
:
4062 d_append_char (dpi
, '~');
4063 d_print_comp (dpi
, options
, dc
->u
.s_dtor
.name
);
4066 case DEMANGLE_COMPONENT_VTABLE
:
4067 d_append_string (dpi
, "vtable for ");
4068 d_print_comp (dpi
, options
, d_left (dc
));
4071 case DEMANGLE_COMPONENT_VTT
:
4072 d_append_string (dpi
, "VTT for ");
4073 d_print_comp (dpi
, options
, d_left (dc
));
4076 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
4077 d_append_string (dpi
, "construction vtable for ");
4078 d_print_comp (dpi
, options
, d_left (dc
));
4079 d_append_string (dpi
, "-in-");
4080 d_print_comp (dpi
, options
, d_right (dc
));
4083 case DEMANGLE_COMPONENT_TYPEINFO
:
4084 d_append_string (dpi
, "typeinfo for ");
4085 d_print_comp (dpi
, options
, d_left (dc
));
4088 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
4089 d_append_string (dpi
, "typeinfo name for ");
4090 d_print_comp (dpi
, options
, d_left (dc
));
4093 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
4094 d_append_string (dpi
, "typeinfo fn for ");
4095 d_print_comp (dpi
, options
, d_left (dc
));
4098 case DEMANGLE_COMPONENT_THUNK
:
4099 d_append_string (dpi
, "non-virtual thunk to ");
4100 d_print_comp (dpi
, options
, d_left (dc
));
4103 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
4104 d_append_string (dpi
, "virtual thunk to ");
4105 d_print_comp (dpi
, options
, d_left (dc
));
4108 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
4109 d_append_string (dpi
, "covariant return thunk to ");
4110 d_print_comp (dpi
, options
, d_left (dc
));
4113 case DEMANGLE_COMPONENT_JAVA_CLASS
:
4114 d_append_string (dpi
, "java Class for ");
4115 d_print_comp (dpi
, options
, d_left (dc
));
4118 case DEMANGLE_COMPONENT_GUARD
:
4119 d_append_string (dpi
, "guard variable for ");
4120 d_print_comp (dpi
, options
, d_left (dc
));
4123 case DEMANGLE_COMPONENT_TLS_INIT
:
4124 d_append_string (dpi
, "TLS init function for ");
4125 d_print_comp (dpi
, options
, d_left (dc
));
4128 case DEMANGLE_COMPONENT_TLS_WRAPPER
:
4129 d_append_string (dpi
, "TLS wrapper function for ");
4130 d_print_comp (dpi
, options
, d_left (dc
));
4133 case DEMANGLE_COMPONENT_REFTEMP
:
4134 d_append_string (dpi
, "reference temporary #");
4135 d_print_comp (dpi
, options
, d_right (dc
));
4136 d_append_string (dpi
, " for ");
4137 d_print_comp (dpi
, options
, d_left (dc
));
4140 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
4141 d_append_string (dpi
, "hidden alias for ");
4142 d_print_comp (dpi
, options
, d_left (dc
));
4145 case DEMANGLE_COMPONENT_TRANSACTION_CLONE
:
4146 d_append_string (dpi
, "transaction clone for ");
4147 d_print_comp (dpi
, options
, d_left (dc
));
4150 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
:
4151 d_append_string (dpi
, "non-transaction clone for ");
4152 d_print_comp (dpi
, options
, d_left (dc
));
4155 case DEMANGLE_COMPONENT_SUB_STD
:
4156 d_append_buffer (dpi
, dc
->u
.s_string
.string
, dc
->u
.s_string
.len
);
4159 case DEMANGLE_COMPONENT_RESTRICT
:
4160 case DEMANGLE_COMPONENT_VOLATILE
:
4161 case DEMANGLE_COMPONENT_CONST
:
4163 struct d_print_mod
*pdpm
;
4165 /* When printing arrays, it's possible to have cases where the
4166 same CV-qualifier gets pushed on the stack multiple times.
4167 We only need to print it once. */
4169 for (pdpm
= dpi
->modifiers
; pdpm
!= NULL
; pdpm
= pdpm
->next
)
4171 if (! pdpm
->printed
)
4173 if (pdpm
->mod
->type
!= DEMANGLE_COMPONENT_RESTRICT
4174 && pdpm
->mod
->type
!= DEMANGLE_COMPONENT_VOLATILE
4175 && pdpm
->mod
->type
!= DEMANGLE_COMPONENT_CONST
)
4177 if (pdpm
->mod
->type
== dc
->type
)
4179 d_print_comp (dpi
, options
, d_left (dc
));
4187 case DEMANGLE_COMPONENT_REFERENCE
:
4188 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
4190 /* Handle reference smashing: & + && = &. */
4191 const struct demangle_component
*sub
= d_left (dc
);
4192 if (sub
->type
== DEMANGLE_COMPONENT_TEMPLATE_PARAM
)
4194 struct demangle_component
*a
= d_lookup_template_argument (dpi
, sub
);
4195 if (a
&& a
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
4196 a
= d_index_template_argument (a
, dpi
->pack_index
);
4200 d_print_error (dpi
);
4207 if (sub
->type
== DEMANGLE_COMPONENT_REFERENCE
4208 || sub
->type
== dc
->type
)
4210 else if (sub
->type
== DEMANGLE_COMPONENT_RVALUE_REFERENCE
)
4211 mod_inner
= d_left (sub
);
4215 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
4216 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
4217 case DEMANGLE_COMPONENT_CONST_THIS
:
4218 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
4219 case DEMANGLE_COMPONENT_POINTER
:
4220 case DEMANGLE_COMPONENT_COMPLEX
:
4221 case DEMANGLE_COMPONENT_IMAGINARY
:
4224 /* We keep a list of modifiers on the stack. */
4225 struct d_print_mod dpm
;
4227 dpm
.next
= dpi
->modifiers
;
4228 dpi
->modifiers
= &dpm
;
4231 dpm
.templates
= dpi
->templates
;
4234 mod_inner
= d_left (dc
);
4236 d_print_comp (dpi
, options
, mod_inner
);
4238 /* If the modifier didn't get printed by the type, print it
4241 d_print_mod (dpi
, options
, dc
);
4243 dpi
->modifiers
= dpm
.next
;
4248 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
4249 if ((options
& DMGL_JAVA
) == 0)
4250 d_append_buffer (dpi
, dc
->u
.s_builtin
.type
->name
,
4251 dc
->u
.s_builtin
.type
->len
);
4253 d_append_buffer (dpi
, dc
->u
.s_builtin
.type
->java_name
,
4254 dc
->u
.s_builtin
.type
->java_len
);
4257 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
4258 d_print_comp (dpi
, options
, d_left (dc
));
4261 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
4263 if ((options
& DMGL_RET_POSTFIX
) != 0)
4264 d_print_function_type (dpi
,
4265 options
& ~(DMGL_RET_POSTFIX
| DMGL_RET_DROP
),
4266 dc
, dpi
->modifiers
);
4268 /* Print return type if present */
4269 if (d_left (dc
) != NULL
&& (options
& DMGL_RET_POSTFIX
) != 0)
4270 d_print_comp (dpi
, options
& ~(DMGL_RET_POSTFIX
| DMGL_RET_DROP
),
4272 else if (d_left (dc
) != NULL
&& (options
& DMGL_RET_DROP
) == 0)
4274 struct d_print_mod dpm
;
4276 /* We must pass this type down as a modifier in order to
4277 print it in the right location. */
4278 dpm
.next
= dpi
->modifiers
;
4279 dpi
->modifiers
= &dpm
;
4282 dpm
.templates
= dpi
->templates
;
4284 d_print_comp (dpi
, options
& ~(DMGL_RET_POSTFIX
| DMGL_RET_DROP
),
4287 dpi
->modifiers
= dpm
.next
;
4292 /* In standard prefix notation, there is a space between the
4293 return type and the function signature. */
4294 if ((options
& DMGL_RET_POSTFIX
) == 0)
4295 d_append_char (dpi
, ' ');
4298 if ((options
& DMGL_RET_POSTFIX
) == 0)
4299 d_print_function_type (dpi
,
4300 options
& ~(DMGL_RET_POSTFIX
| DMGL_RET_DROP
),
4301 dc
, dpi
->modifiers
);
4306 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
4308 struct d_print_mod
*hold_modifiers
;
4309 struct d_print_mod adpm
[4];
4311 struct d_print_mod
*pdpm
;
4313 /* We must pass this type down as a modifier in order to print
4314 multi-dimensional arrays correctly. If the array itself is
4315 CV-qualified, we act as though the element type were
4316 CV-qualified. We do this by copying the modifiers down
4317 rather than fiddling pointers, so that we don't wind up
4318 with a d_print_mod higher on the stack pointing into our
4319 stack frame after we return. */
4321 hold_modifiers
= dpi
->modifiers
;
4323 adpm
[0].next
= hold_modifiers
;
4324 dpi
->modifiers
= &adpm
[0];
4326 adpm
[0].printed
= 0;
4327 adpm
[0].templates
= dpi
->templates
;
4330 pdpm
= hold_modifiers
;
4332 && (pdpm
->mod
->type
== DEMANGLE_COMPONENT_RESTRICT
4333 || pdpm
->mod
->type
== DEMANGLE_COMPONENT_VOLATILE
4334 || pdpm
->mod
->type
== DEMANGLE_COMPONENT_CONST
))
4336 if (! pdpm
->printed
)
4338 if (i
>= sizeof adpm
/ sizeof adpm
[0])
4340 d_print_error (dpi
);
4345 adpm
[i
].next
= dpi
->modifiers
;
4346 dpi
->modifiers
= &adpm
[i
];
4354 d_print_comp (dpi
, options
, d_right (dc
));
4356 dpi
->modifiers
= hold_modifiers
;
4358 if (adpm
[0].printed
)
4364 d_print_mod (dpi
, options
, adpm
[i
].mod
);
4367 d_print_array_type (dpi
, options
, dc
, dpi
->modifiers
);
4372 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
4373 case DEMANGLE_COMPONENT_VECTOR_TYPE
:
4375 struct d_print_mod dpm
;
4377 dpm
.next
= dpi
->modifiers
;
4378 dpi
->modifiers
= &dpm
;
4381 dpm
.templates
= dpi
->templates
;
4383 d_print_comp (dpi
, options
, d_right (dc
));
4385 /* If the modifier didn't get printed by the type, print it
4388 d_print_mod (dpi
, options
, dc
);
4390 dpi
->modifiers
= dpm
.next
;
4395 case DEMANGLE_COMPONENT_FIXED_TYPE
:
4396 if (dc
->u
.s_fixed
.sat
)
4397 d_append_string (dpi
, "_Sat ");
4398 /* Don't print "int _Accum". */
4399 if (dc
->u
.s_fixed
.length
->u
.s_builtin
.type
4400 != &cplus_demangle_builtin_types
['i'-'a'])
4402 d_print_comp (dpi
, options
, dc
->u
.s_fixed
.length
);
4403 d_append_char (dpi
, ' ');
4405 if (dc
->u
.s_fixed
.accum
)
4406 d_append_string (dpi
, "_Accum");
4408 d_append_string (dpi
, "_Fract");
4411 case DEMANGLE_COMPONENT_ARGLIST
:
4412 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
4413 if (d_left (dc
) != NULL
)
4414 d_print_comp (dpi
, options
, d_left (dc
));
4415 if (d_right (dc
) != NULL
)
4418 unsigned long int flush_count
;
4419 /* Make sure ", " isn't flushed by d_append_string, otherwise
4420 dpi->len -= 2 wouldn't work. */
4421 if (dpi
->len
>= sizeof (dpi
->buf
) - 2)
4422 d_print_flush (dpi
);
4423 d_append_string (dpi
, ", ");
4425 flush_count
= dpi
->flush_count
;
4426 d_print_comp (dpi
, options
, d_right (dc
));
4427 /* If that didn't print anything (which can happen with empty
4428 template argument packs), remove the comma and space. */
4429 if (dpi
->flush_count
== flush_count
&& dpi
->len
== len
)
4434 case DEMANGLE_COMPONENT_INITIALIZER_LIST
:
4436 struct demangle_component
*type
= d_left (dc
);
4437 struct demangle_component
*list
= d_right (dc
);
4440 d_print_comp (dpi
, options
, type
);
4441 d_append_char (dpi
, '{');
4442 d_print_comp (dpi
, options
, list
);
4443 d_append_char (dpi
, '}');
4447 case DEMANGLE_COMPONENT_OPERATOR
:
4449 const struct demangle_operator_info
*op
= dc
->u
.s_operator
.op
;
4452 d_append_string (dpi
, "operator");
4453 /* Add a space before new/delete. */
4454 if (IS_LOWER (op
->name
[0]))
4455 d_append_char (dpi
, ' ');
4456 /* Omit a trailing space. */
4457 if (op
->name
[len
-1] == ' ')
4459 d_append_buffer (dpi
, op
->name
, len
);
4463 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
4464 d_append_string (dpi
, "operator ");
4465 d_print_comp (dpi
, options
, dc
->u
.s_extended_operator
.name
);
4468 case DEMANGLE_COMPONENT_CAST
:
4469 d_append_string (dpi
, "operator ");
4470 d_print_cast (dpi
, options
, dc
);
4473 case DEMANGLE_COMPONENT_NULLARY
:
4474 d_print_expr_op (dpi
, options
, d_left (dc
));
4477 case DEMANGLE_COMPONENT_UNARY
:
4479 struct demangle_component
*op
= d_left (dc
);
4480 struct demangle_component
*operand
= d_right (dc
);
4481 const char *code
= NULL
;
4483 if (op
->type
== DEMANGLE_COMPONENT_OPERATOR
)
4485 code
= op
->u
.s_operator
.op
->code
;
4486 if (!strcmp (code
, "ad"))
4488 /* Don't print the argument list for the address of a
4490 if (operand
->type
== DEMANGLE_COMPONENT_TYPED_NAME
4491 && d_left (operand
)->type
== DEMANGLE_COMPONENT_QUAL_NAME
4492 && d_right (operand
)->type
== DEMANGLE_COMPONENT_FUNCTION_TYPE
)
4493 operand
= d_left (operand
);
4495 if (operand
->type
== DEMANGLE_COMPONENT_BINARY_ARGS
)
4497 /* This indicates a suffix operator. */
4498 operand
= d_left (operand
);
4499 d_print_subexpr (dpi
, options
, operand
);
4500 d_print_expr_op (dpi
, options
, op
);
4505 if (op
->type
!= DEMANGLE_COMPONENT_CAST
)
4506 d_print_expr_op (dpi
, options
, op
);
4509 d_append_char (dpi
, '(');
4510 d_print_cast (dpi
, options
, op
);
4511 d_append_char (dpi
, ')');
4513 if (code
&& !strcmp (code
, "gs"))
4514 /* Avoid parens after '::'. */
4515 d_print_comp (dpi
, options
, operand
);
4516 else if (code
&& !strcmp (code
, "st"))
4517 /* Always print parens for sizeof (type). */
4519 d_append_char (dpi
, '(');
4520 d_print_comp (dpi
, options
, operand
);
4521 d_append_char (dpi
, ')');
4524 d_print_subexpr (dpi
, options
, operand
);
4528 case DEMANGLE_COMPONENT_BINARY
:
4529 if (d_right (dc
)->type
!= DEMANGLE_COMPONENT_BINARY_ARGS
)
4531 d_print_error (dpi
);
4535 if (op_is_new_cast (d_left (dc
)))
4537 d_print_expr_op (dpi
, options
, d_left (dc
));
4538 d_append_char (dpi
, '<');
4539 d_print_comp (dpi
, options
, d_left (d_right (dc
)));
4540 d_append_string (dpi
, ">(");
4541 d_print_comp (dpi
, options
, d_right (d_right (dc
)));
4542 d_append_char (dpi
, ')');
4546 /* We wrap an expression which uses the greater-than operator in
4547 an extra layer of parens so that it does not get confused
4548 with the '>' which ends the template parameters. */
4549 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_OPERATOR
4550 && d_left (dc
)->u
.s_operator
.op
->len
== 1
4551 && d_left (dc
)->u
.s_operator
.op
->name
[0] == '>')
4552 d_append_char (dpi
, '(');
4554 if (strcmp (d_left (dc
)->u
.s_operator
.op
->code
, "cl") == 0
4555 && d_left (d_right (dc
))->type
== DEMANGLE_COMPONENT_TYPED_NAME
)
4557 /* Function call used in an expression should not have printed types
4558 of the function arguments. Values of the function arguments still
4559 get printed below. */
4561 const struct demangle_component
*func
= d_left (d_right (dc
));
4563 if (d_right (func
)->type
!= DEMANGLE_COMPONENT_FUNCTION_TYPE
)
4564 d_print_error (dpi
);
4565 d_print_subexpr (dpi
, options
, d_left (func
));
4568 d_print_subexpr (dpi
, options
, d_left (d_right (dc
)));
4569 if (strcmp (d_left (dc
)->u
.s_operator
.op
->code
, "ix") == 0)
4571 d_append_char (dpi
, '[');
4572 d_print_comp (dpi
, options
, d_right (d_right (dc
)));
4573 d_append_char (dpi
, ']');
4577 if (strcmp (d_left (dc
)->u
.s_operator
.op
->code
, "cl") != 0)
4578 d_print_expr_op (dpi
, options
, d_left (dc
));
4579 d_print_subexpr (dpi
, options
, d_right (d_right (dc
)));
4582 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_OPERATOR
4583 && d_left (dc
)->u
.s_operator
.op
->len
== 1
4584 && d_left (dc
)->u
.s_operator
.op
->name
[0] == '>')
4585 d_append_char (dpi
, ')');
4589 case DEMANGLE_COMPONENT_BINARY_ARGS
:
4590 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
4591 d_print_error (dpi
);
4594 case DEMANGLE_COMPONENT_TRINARY
:
4595 if (d_right (dc
)->type
!= DEMANGLE_COMPONENT_TRINARY_ARG1
4596 || d_right (d_right (dc
))->type
!= DEMANGLE_COMPONENT_TRINARY_ARG2
)
4598 d_print_error (dpi
);
4602 struct demangle_component
*op
= d_left (dc
);
4603 struct demangle_component
*first
= d_left (d_right (dc
));
4604 struct demangle_component
*second
= d_left (d_right (d_right (dc
)));
4605 struct demangle_component
*third
= d_right (d_right (d_right (dc
)));
4607 if (!strcmp (op
->u
.s_operator
.op
->code
, "qu"))
4609 d_print_subexpr (dpi
, options
, first
);
4610 d_print_expr_op (dpi
, options
, op
);
4611 d_print_subexpr (dpi
, options
, second
);
4612 d_append_string (dpi
, " : ");
4613 d_print_subexpr (dpi
, options
, third
);
4617 d_append_string (dpi
, "new ");
4618 if (d_left (first
) != NULL
)
4620 d_print_subexpr (dpi
, options
, first
);
4621 d_append_char (dpi
, ' ');
4623 d_print_comp (dpi
, options
, second
);
4625 d_print_subexpr (dpi
, options
, third
);
4630 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
4631 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
4632 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
4633 d_print_error (dpi
);
4636 case DEMANGLE_COMPONENT_LITERAL
:
4637 case DEMANGLE_COMPONENT_LITERAL_NEG
:
4639 enum d_builtin_type_print tp
;
4641 /* For some builtin types, produce simpler output. */
4642 tp
= D_PRINT_DEFAULT
;
4643 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
)
4645 tp
= d_left (dc
)->u
.s_builtin
.type
->print
;
4649 case D_PRINT_UNSIGNED
:
4651 case D_PRINT_UNSIGNED_LONG
:
4652 case D_PRINT_LONG_LONG
:
4653 case D_PRINT_UNSIGNED_LONG_LONG
:
4654 if (d_right (dc
)->type
== DEMANGLE_COMPONENT_NAME
)
4656 if (dc
->type
== DEMANGLE_COMPONENT_LITERAL_NEG
)
4657 d_append_char (dpi
, '-');
4658 d_print_comp (dpi
, options
, d_right (dc
));
4663 case D_PRINT_UNSIGNED
:
4664 d_append_char (dpi
, 'u');
4667 d_append_char (dpi
, 'l');
4669 case D_PRINT_UNSIGNED_LONG
:
4670 d_append_string (dpi
, "ul");
4672 case D_PRINT_LONG_LONG
:
4673 d_append_string (dpi
, "ll");
4675 case D_PRINT_UNSIGNED_LONG_LONG
:
4676 d_append_string (dpi
, "ull");
4684 if (d_right (dc
)->type
== DEMANGLE_COMPONENT_NAME
4685 && d_right (dc
)->u
.s_name
.len
== 1
4686 && dc
->type
== DEMANGLE_COMPONENT_LITERAL
)
4688 switch (d_right (dc
)->u
.s_name
.s
[0])
4691 d_append_string (dpi
, "false");
4694 d_append_string (dpi
, "true");
4707 d_append_char (dpi
, '(');
4708 d_print_comp (dpi
, options
, d_left (dc
));
4709 d_append_char (dpi
, ')');
4710 if (dc
->type
== DEMANGLE_COMPONENT_LITERAL_NEG
)
4711 d_append_char (dpi
, '-');
4712 if (tp
== D_PRINT_FLOAT
)
4713 d_append_char (dpi
, '[');
4714 d_print_comp (dpi
, options
, d_right (dc
));
4715 if (tp
== D_PRINT_FLOAT
)
4716 d_append_char (dpi
, ']');
4720 case DEMANGLE_COMPONENT_NUMBER
:
4721 d_append_num (dpi
, dc
->u
.s_number
.number
);
4724 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
4725 d_append_string (dpi
, "java resource ");
4726 d_print_comp (dpi
, options
, d_left (dc
));
4729 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
4730 d_print_comp (dpi
, options
, d_left (dc
));
4731 d_print_comp (dpi
, options
, d_right (dc
));
4734 case DEMANGLE_COMPONENT_CHARACTER
:
4735 d_append_char (dpi
, dc
->u
.s_character
.character
);
4738 case DEMANGLE_COMPONENT_DECLTYPE
:
4739 d_append_string (dpi
, "decltype (");
4740 d_print_comp (dpi
, options
, d_left (dc
));
4741 d_append_char (dpi
, ')');
4744 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
4748 struct demangle_component
*a
= d_find_pack (dpi
, d_left (dc
));
4751 /* d_find_pack won't find anything if the only packs involved
4752 in this expansion are function parameter packs; in that
4753 case, just print the pattern and "...". */
4754 d_print_subexpr (dpi
, options
, d_left (dc
));
4755 d_append_string (dpi
, "...");
4759 len
= d_pack_length (a
);
4761 for (i
= 0; i
< len
; ++i
)
4763 dpi
->pack_index
= i
;
4764 d_print_comp (dpi
, options
, dc
);
4766 d_append_string (dpi
, ", ");
4771 case DEMANGLE_COMPONENT_FUNCTION_PARAM
:
4773 long num
= dc
->u
.s_number
.number
;
4775 d_append_string (dpi
, "this");
4778 d_append_string (dpi
, "{parm#");
4779 d_append_num (dpi
, num
);
4780 d_append_char (dpi
, '}');
4785 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
:
4786 d_append_string (dpi
, "global constructors keyed to ");
4787 d_print_comp (dpi
, options
, dc
->u
.s_binary
.left
);
4790 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS
:
4791 d_append_string (dpi
, "global destructors keyed to ");
4792 d_print_comp (dpi
, options
, dc
->u
.s_binary
.left
);
4795 case DEMANGLE_COMPONENT_LAMBDA
:
4796 d_append_string (dpi
, "{lambda(");
4797 d_print_comp (dpi
, options
, dc
->u
.s_unary_num
.sub
);
4798 d_append_string (dpi
, ")#");
4799 d_append_num (dpi
, dc
->u
.s_unary_num
.num
+ 1);
4800 d_append_char (dpi
, '}');
4803 case DEMANGLE_COMPONENT_UNNAMED_TYPE
:
4804 d_append_string (dpi
, "{unnamed type#");
4805 d_append_num (dpi
, dc
->u
.s_number
.number
+ 1);
4806 d_append_char (dpi
, '}');
4809 case DEMANGLE_COMPONENT_CLONE
:
4810 d_print_comp (dpi
, options
, d_left (dc
));
4811 d_append_string (dpi
, " [clone ");
4812 d_print_comp (dpi
, options
, d_right (dc
));
4813 d_append_char (dpi
, ']');
4817 d_print_error (dpi
);
4822 /* Print a Java dentifier. For Java we try to handle encoded extended
4823 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
4824 so we don't it for C++. Characters are encoded as
4828 d_print_java_identifier (struct d_print_info
*dpi
, const char *name
, int len
)
4834 for (p
= name
; p
< end
; ++p
)
4845 for (q
= p
+ 3; q
< end
; ++q
)
4851 else if (*q
>= 'A' && *q
<= 'F')
4852 dig
= *q
- 'A' + 10;
4853 else if (*q
>= 'a' && *q
<= 'f')
4854 dig
= *q
- 'a' + 10;
4860 /* If the Unicode character is larger than 256, we don't try
4861 to deal with it here. FIXME. */
4862 if (q
< end
&& *q
== '_' && c
< 256)
4864 d_append_char (dpi
, c
);
4870 d_append_char (dpi
, *p
);
4874 /* Print a list of modifiers. SUFFIX is 1 if we are printing
4875 qualifiers on this after printing a function. */
4878 d_print_mod_list (struct d_print_info
*dpi
, int options
,
4879 struct d_print_mod
*mods
, int suffix
)
4881 struct d_print_template
*hold_dpt
;
4883 if (mods
== NULL
|| d_print_saw_error (dpi
))
4888 && (mods
->mod
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
4889 || mods
->mod
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
4890 || mods
->mod
->type
== DEMANGLE_COMPONENT_CONST_THIS
)))
4892 d_print_mod_list (dpi
, options
, mods
->next
, suffix
);
4898 hold_dpt
= dpi
->templates
;
4899 dpi
->templates
= mods
->templates
;
4901 if (mods
->mod
->type
== DEMANGLE_COMPONENT_FUNCTION_TYPE
)
4903 d_print_function_type (dpi
, options
, mods
->mod
, mods
->next
);
4904 dpi
->templates
= hold_dpt
;
4907 else if (mods
->mod
->type
== DEMANGLE_COMPONENT_ARRAY_TYPE
)
4909 d_print_array_type (dpi
, options
, mods
->mod
, mods
->next
);
4910 dpi
->templates
= hold_dpt
;
4913 else if (mods
->mod
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
4915 struct d_print_mod
*hold_modifiers
;
4916 struct demangle_component
*dc
;
4918 /* When this is on the modifier stack, we have pulled any
4919 qualifiers off the right argument already. Otherwise, we
4920 print it as usual, but don't let the left argument see any
4923 hold_modifiers
= dpi
->modifiers
;
4924 dpi
->modifiers
= NULL
;
4925 d_print_comp (dpi
, options
, d_left (mods
->mod
));
4926 dpi
->modifiers
= hold_modifiers
;
4928 if ((options
& DMGL_JAVA
) == 0)
4929 d_append_string (dpi
, "::");
4931 d_append_char (dpi
, '.');
4933 dc
= d_right (mods
->mod
);
4935 if (dc
->type
== DEMANGLE_COMPONENT_DEFAULT_ARG
)
4937 d_append_string (dpi
, "{default arg#");
4938 d_append_num (dpi
, dc
->u
.s_unary_num
.num
+ 1);
4939 d_append_string (dpi
, "}::");
4940 dc
= dc
->u
.s_unary_num
.sub
;
4943 while (dc
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
4944 || dc
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
4945 || dc
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
4948 d_print_comp (dpi
, options
, dc
);
4950 dpi
->templates
= hold_dpt
;
4954 d_print_mod (dpi
, options
, mods
->mod
);
4956 dpi
->templates
= hold_dpt
;
4958 d_print_mod_list (dpi
, options
, mods
->next
, suffix
);
4961 /* Print a modifier. */
4964 d_print_mod (struct d_print_info
*dpi
, int options
,
4965 const struct demangle_component
*mod
)
4969 case DEMANGLE_COMPONENT_RESTRICT
:
4970 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
4971 d_append_string (dpi
, " restrict");
4973 case DEMANGLE_COMPONENT_VOLATILE
:
4974 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
4975 d_append_string (dpi
, " volatile");
4977 case DEMANGLE_COMPONENT_CONST
:
4978 case DEMANGLE_COMPONENT_CONST_THIS
:
4979 d_append_string (dpi
, " const");
4981 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
4982 d_append_char (dpi
, ' ');
4983 d_print_comp (dpi
, options
, d_right (mod
));
4985 case DEMANGLE_COMPONENT_POINTER
:
4986 /* There is no pointer symbol in Java. */
4987 if ((options
& DMGL_JAVA
) == 0)
4988 d_append_char (dpi
, '*');
4990 case DEMANGLE_COMPONENT_REFERENCE
:
4991 d_append_char (dpi
, '&');
4993 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
4994 d_append_string (dpi
, "&&");
4996 case DEMANGLE_COMPONENT_COMPLEX
:
4997 d_append_string (dpi
, "complex ");
4999 case DEMANGLE_COMPONENT_IMAGINARY
:
5000 d_append_string (dpi
, "imaginary ");
5002 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
5003 if (d_last_char (dpi
) != '(')
5004 d_append_char (dpi
, ' ');
5005 d_print_comp (dpi
, options
, d_left (mod
));
5006 d_append_string (dpi
, "::*");
5008 case DEMANGLE_COMPONENT_TYPED_NAME
:
5009 d_print_comp (dpi
, options
, d_left (mod
));
5011 case DEMANGLE_COMPONENT_VECTOR_TYPE
:
5012 d_append_string (dpi
, " __vector(");
5013 d_print_comp (dpi
, options
, d_left (mod
));
5014 d_append_char (dpi
, ')');
5018 /* Otherwise, we have something that won't go back on the
5019 modifier stack, so we can just print it. */
5020 d_print_comp (dpi
, options
, mod
);
5025 /* Print a function type, except for the return type. */
5028 d_print_function_type (struct d_print_info
*dpi
, int options
,
5029 const struct demangle_component
*dc
,
5030 struct d_print_mod
*mods
)
5034 struct d_print_mod
*p
;
5035 struct d_print_mod
*hold_modifiers
;
5039 for (p
= mods
; p
!= NULL
; p
= p
->next
)
5044 switch (p
->mod
->type
)
5046 case DEMANGLE_COMPONENT_POINTER
:
5047 case DEMANGLE_COMPONENT_REFERENCE
:
5048 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
5051 case DEMANGLE_COMPONENT_RESTRICT
:
5052 case DEMANGLE_COMPONENT_VOLATILE
:
5053 case DEMANGLE_COMPONENT_CONST
:
5054 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
5055 case DEMANGLE_COMPONENT_COMPLEX
:
5056 case DEMANGLE_COMPONENT_IMAGINARY
:
5057 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
5061 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
5062 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
5063 case DEMANGLE_COMPONENT_CONST_THIS
:
5076 if (d_last_char (dpi
) != '('
5077 && d_last_char (dpi
) != '*')
5080 if (need_space
&& d_last_char (dpi
) != ' ')
5081 d_append_char (dpi
, ' ');
5082 d_append_char (dpi
, '(');
5085 hold_modifiers
= dpi
->modifiers
;
5086 dpi
->modifiers
= NULL
;
5088 d_print_mod_list (dpi
, options
, mods
, 0);
5091 d_append_char (dpi
, ')');
5093 d_append_char (dpi
, '(');
5095 if (d_right (dc
) != NULL
)
5096 d_print_comp (dpi
, options
, d_right (dc
));
5098 d_append_char (dpi
, ')');
5100 d_print_mod_list (dpi
, options
, mods
, 1);
5102 dpi
->modifiers
= hold_modifiers
;
5105 /* Print an array type, except for the element type. */
5108 d_print_array_type (struct d_print_info
*dpi
, int options
,
5109 const struct demangle_component
*dc
,
5110 struct d_print_mod
*mods
)
5118 struct d_print_mod
*p
;
5121 for (p
= mods
; p
!= NULL
; p
= p
->next
)
5125 if (p
->mod
->type
== DEMANGLE_COMPONENT_ARRAY_TYPE
)
5140 d_append_string (dpi
, " (");
5142 d_print_mod_list (dpi
, options
, mods
, 0);
5145 d_append_char (dpi
, ')');
5149 d_append_char (dpi
, ' ');
5151 d_append_char (dpi
, '[');
5153 if (d_left (dc
) != NULL
)
5154 d_print_comp (dpi
, options
, d_left (dc
));
5156 d_append_char (dpi
, ']');
5159 /* Print an operator in an expression. */
5162 d_print_expr_op (struct d_print_info
*dpi
, int options
,
5163 const struct demangle_component
*dc
)
5165 if (dc
->type
== DEMANGLE_COMPONENT_OPERATOR
)
5166 d_append_buffer (dpi
, dc
->u
.s_operator
.op
->name
,
5167 dc
->u
.s_operator
.op
->len
);
5169 d_print_comp (dpi
, options
, dc
);
5175 d_print_cast (struct d_print_info
*dpi
, int options
,
5176 const struct demangle_component
*dc
)
5178 if (d_left (dc
)->type
!= DEMANGLE_COMPONENT_TEMPLATE
)
5179 d_print_comp (dpi
, options
, d_left (dc
));
5182 struct d_print_mod
*hold_dpm
;
5183 struct d_print_template dpt
;
5185 /* It appears that for a templated cast operator, we need to put
5186 the template parameters in scope for the operator name, but
5187 not for the parameters. The effect is that we need to handle
5188 the template printing here. */
5190 hold_dpm
= dpi
->modifiers
;
5191 dpi
->modifiers
= NULL
;
5193 dpt
.next
= dpi
->templates
;
5194 dpi
->templates
= &dpt
;
5195 dpt
.template_decl
= d_left (dc
);
5197 d_print_comp (dpi
, options
, d_left (d_left (dc
)));
5199 dpi
->templates
= dpt
.next
;
5201 if (d_last_char (dpi
) == '<')
5202 d_append_char (dpi
, ' ');
5203 d_append_char (dpi
, '<');
5204 d_print_comp (dpi
, options
, d_right (d_left (dc
)));
5205 /* Avoid generating two consecutive '>' characters, to avoid
5206 the C++ syntactic ambiguity. */
5207 if (d_last_char (dpi
) == '>')
5208 d_append_char (dpi
, ' ');
5209 d_append_char (dpi
, '>');
5211 dpi
->modifiers
= hold_dpm
;
5215 /* Initialize the information structure we use to pass around
5218 CP_STATIC_IF_GLIBCPP_V3
5220 cplus_demangle_init_info (const char *mangled
, int options
, size_t len
,
5224 di
->send
= mangled
+ len
;
5225 di
->options
= options
;
5229 /* We can not need more components than twice the number of chars in
5230 the mangled string. Most components correspond directly to
5231 chars, but the ARGLIST types are exceptions. */
5232 di
->num_comps
= 2 * len
;
5235 /* Similarly, we can not need more substitutions than there are
5236 chars in the mangled string. */
5241 di
->last_name
= NULL
;
5246 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
5247 mangled name, return strings in repeated callback giving the demangled
5248 name. OPTIONS is the usual libiberty demangler options. On success,
5249 this returns 1. On failure, returns 0. */
5252 d_demangle_callback (const char *mangled
, int options
,
5253 demangle_callbackref callback
, void *opaque
)
5264 struct demangle_component
*dc
;
5267 if (mangled
[0] == '_' && mangled
[1] == 'Z')
5269 else if (strncmp (mangled
, "_GLOBAL_", 8) == 0
5270 && (mangled
[8] == '.' || mangled
[8] == '_' || mangled
[8] == '$')
5271 && (mangled
[9] == 'D' || mangled
[9] == 'I')
5272 && mangled
[10] == '_')
5273 type
= mangled
[9] == 'I' ? DCT_GLOBAL_CTORS
: DCT_GLOBAL_DTORS
;
5276 if ((options
& DMGL_TYPES
) == 0)
5281 cplus_demangle_init_info (mangled
, options
, strlen (mangled
), &di
);
5284 #ifdef CP_DYNAMIC_ARRAYS
5285 __extension__
struct demangle_component comps
[di
.num_comps
];
5286 __extension__
struct demangle_component
*subs
[di
.num_subs
];
5291 di
.comps
= alloca (di
.num_comps
* sizeof (*di
.comps
));
5292 di
.subs
= alloca (di
.num_subs
* sizeof (*di
.subs
));
5298 dc
= cplus_demangle_type (&di
);
5301 dc
= cplus_demangle_mangled_name (&di
, 1);
5303 case DCT_GLOBAL_CTORS
:
5304 case DCT_GLOBAL_DTORS
:
5305 d_advance (&di
, 11);
5306 dc
= d_make_comp (&di
,
5307 (type
== DCT_GLOBAL_CTORS
5308 ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
5309 : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS
),
5310 d_make_demangle_mangled_name (&di
, d_str (&di
)),
5312 d_advance (&di
, strlen (d_str (&di
)));
5316 /* If DMGL_PARAMS is set, then if we didn't consume the entire
5317 mangled string, then we didn't successfully demangle it. If
5318 DMGL_PARAMS is not set, we didn't look at the trailing
5320 if (((options
& DMGL_PARAMS
) != 0) && d_peek_char (&di
) != '\0')
5323 #ifdef CP_DEMANGLE_DEBUG
5327 status
= (dc
!= NULL
)
5328 ? cplus_demangle_print_callback (options
, dc
, callback
, opaque
)
5335 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
5336 name, return a buffer allocated with malloc holding the demangled
5337 name. OPTIONS is the usual libiberty demangler options. On
5338 success, this sets *PALC to the allocated size of the returned
5339 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
5340 a memory allocation failure, and returns NULL. */
5343 d_demangle (const char *mangled
, int options
, size_t *palc
)
5345 struct d_growable_string dgs
;
5348 d_growable_string_init (&dgs
, 0);
5350 status
= d_demangle_callback (mangled
, options
,
5351 d_growable_string_callback_adapter
, &dgs
);
5359 *palc
= dgs
.allocation_failure
? 1 : dgs
.alc
;
5363 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
5365 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
5367 /* ia64 ABI-mandated entry point in the C++ runtime library for
5368 performing demangling. MANGLED_NAME is a NUL-terminated character
5369 string containing the name to be demangled.
5371 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
5372 *LENGTH bytes, into which the demangled name is stored. If
5373 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
5374 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
5375 is placed in a region of memory allocated with malloc.
5377 If LENGTH is non-NULL, the length of the buffer containing the
5378 demangled name, is placed in *LENGTH.
5380 The return value is a pointer to the start of the NUL-terminated
5381 demangled name, or NULL if the demangling fails. The caller is
5382 responsible for deallocating this memory using free.
5384 *STATUS is set to one of the following values:
5385 0: The demangling operation succeeded.
5386 -1: A memory allocation failure occurred.
5387 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
5388 -3: One of the arguments is invalid.
5390 The demangling is performed using the C++ ABI mangling rules, with
5394 __cxa_demangle (const char *mangled_name
, char *output_buffer
,
5395 size_t *length
, int *status
)
5400 if (mangled_name
== NULL
)
5407 if (output_buffer
!= NULL
&& length
== NULL
)
5414 demangled
= d_demangle (mangled_name
, DMGL_PARAMS
| DMGL_TYPES
, &alc
);
5416 if (demangled
== NULL
)
5428 if (output_buffer
== NULL
)
5435 if (strlen (demangled
) < *length
)
5437 strcpy (output_buffer
, demangled
);
5439 demangled
= output_buffer
;
5443 free (output_buffer
);
5454 extern int __gcclibcxx_demangle_callback (const char *,
5456 (const char *, size_t, void *),
5459 /* Alternative, allocationless entry point in the C++ runtime library
5460 for performing demangling. MANGLED_NAME is a NUL-terminated character
5461 string containing the name to be demangled.
5463 CALLBACK is a callback function, called with demangled string
5464 segments as demangling progresses; it is called at least once,
5465 but may be called more than once. OPAQUE is a generalized pointer
5466 used as a callback argument.
5468 The return code is one of the following values, equivalent to
5469 the STATUS values of __cxa_demangle() (excluding -1, since this
5470 function performs no memory allocations):
5471 0: The demangling operation succeeded.
5472 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
5473 -3: One of the arguments is invalid.
5475 The demangling is performed using the C++ ABI mangling rules, with
5479 __gcclibcxx_demangle_callback (const char *mangled_name
,
5480 void (*callback
) (const char *, size_t, void *),
5485 if (mangled_name
== NULL
|| callback
== NULL
)
5488 status
= d_demangle_callback (mangled_name
, DMGL_PARAMS
| DMGL_TYPES
,
5496 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
5498 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
5499 mangled name, return a buffer allocated with malloc holding the
5500 demangled name. Otherwise, return NULL. */
5503 cplus_demangle_v3 (const char *mangled
, int options
)
5507 return d_demangle (mangled
, options
, &alc
);
5511 cplus_demangle_v3_callback (const char *mangled
, int options
,
5512 demangle_callbackref callback
, void *opaque
)
5514 return d_demangle_callback (mangled
, options
, callback
, opaque
);
5517 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
5518 conventions, but the output formatting is a little different.
5519 This instructs the C++ demangler not to emit pointer characters ("*"), to
5520 use Java's namespace separator symbol ("." instead of "::"), and to output
5521 JArray<TYPE> as TYPE[]. */
5524 java_demangle_v3 (const char *mangled
)
5528 return d_demangle (mangled
, DMGL_JAVA
| DMGL_PARAMS
| DMGL_RET_POSTFIX
, &alc
);
5532 java_demangle_v3_callback (const char *mangled
,
5533 demangle_callbackref callback
, void *opaque
)
5535 return d_demangle_callback (mangled
,
5536 DMGL_JAVA
| DMGL_PARAMS
| DMGL_RET_POSTFIX
,
5540 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
5542 #ifndef IN_GLIBCPP_V3
5544 /* Demangle a string in order to find out whether it is a constructor
5545 or destructor. Return non-zero on success. Set *CTOR_KIND and
5546 *DTOR_KIND appropriately. */
5549 is_ctor_or_dtor (const char *mangled
,
5550 enum gnu_v3_ctor_kinds
*ctor_kind
,
5551 enum gnu_v3_dtor_kinds
*dtor_kind
)
5554 struct demangle_component
*dc
;
5557 *ctor_kind
= (enum gnu_v3_ctor_kinds
) 0;
5558 *dtor_kind
= (enum gnu_v3_dtor_kinds
) 0;
5560 cplus_demangle_init_info (mangled
, DMGL_GNU_V3
, strlen (mangled
), &di
);
5563 #ifdef CP_DYNAMIC_ARRAYS
5564 __extension__
struct demangle_component comps
[di
.num_comps
];
5565 __extension__
struct demangle_component
*subs
[di
.num_subs
];
5570 di
.comps
= alloca (di
.num_comps
* sizeof (*di
.comps
));
5571 di
.subs
= alloca (di
.num_subs
* sizeof (*di
.subs
));
5574 dc
= cplus_demangle_mangled_name (&di
, 1);
5576 /* Note that because we did not pass DMGL_PARAMS, we don't expect
5577 to demangle the entire string. */
5587 case DEMANGLE_COMPONENT_TYPED_NAME
:
5588 case DEMANGLE_COMPONENT_TEMPLATE
:
5589 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
5590 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
5591 case DEMANGLE_COMPONENT_CONST_THIS
:
5594 case DEMANGLE_COMPONENT_QUAL_NAME
:
5595 case DEMANGLE_COMPONENT_LOCAL_NAME
:
5598 case DEMANGLE_COMPONENT_CTOR
:
5599 *ctor_kind
= dc
->u
.s_ctor
.kind
;
5603 case DEMANGLE_COMPONENT_DTOR
:
5604 *dtor_kind
= dc
->u
.s_dtor
.kind
;
5615 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
5616 name. A non-zero return indicates the type of constructor. */
5618 enum gnu_v3_ctor_kinds
5619 is_gnu_v3_mangled_ctor (const char *name
)
5621 enum gnu_v3_ctor_kinds ctor_kind
;
5622 enum gnu_v3_dtor_kinds dtor_kind
;
5624 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
5625 return (enum gnu_v3_ctor_kinds
) 0;
5630 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
5631 name. A non-zero return indicates the type of destructor. */
5633 enum gnu_v3_dtor_kinds
5634 is_gnu_v3_mangled_dtor (const char *name
)
5636 enum gnu_v3_ctor_kinds ctor_kind
;
5637 enum gnu_v3_dtor_kinds dtor_kind
;
5639 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
5640 return (enum gnu_v3_dtor_kinds
) 0;
5644 #endif /* IN_GLIBCPP_V3 */
5646 #ifdef STANDALONE_DEMANGLER
5649 #include "dyn-string.h"
5651 static void print_usage (FILE* fp
, int exit_value
);
5653 #define IS_ALPHA(CHAR) \
5654 (((CHAR) >= 'a' && (CHAR) <= 'z') \
5655 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
5657 /* Non-zero if CHAR is a character than can occur in a mangled name. */
5658 #define is_mangled_char(CHAR) \
5659 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
5660 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
5662 /* The name of this program, as invoked. */
5663 const char* program_name
;
5665 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
5668 print_usage (FILE* fp
, int exit_value
)
5670 fprintf (fp
, "Usage: %s [options] [names ...]\n", program_name
);
5671 fprintf (fp
, "Options:\n");
5672 fprintf (fp
, " -h,--help Display this message.\n");
5673 fprintf (fp
, " -p,--no-params Don't display function parameters\n");
5674 fprintf (fp
, " -v,--verbose Produce verbose demanglings.\n");
5675 fprintf (fp
, "If names are provided, they are demangled. Otherwise filters standard input.\n");
5680 /* Option specification for getopt_long. */
5681 static const struct option long_options
[] =
5683 { "help", no_argument
, NULL
, 'h' },
5684 { "no-params", no_argument
, NULL
, 'p' },
5685 { "verbose", no_argument
, NULL
, 'v' },
5686 { NULL
, no_argument
, NULL
, 0 },
5689 /* Main entry for a demangling filter executable. It will demangle
5690 its command line arguments, if any. If none are provided, it will
5691 filter stdin to stdout, replacing any recognized mangled C++ names
5692 with their demangled equivalents. */
5695 main (int argc
, char *argv
[])
5699 int options
= DMGL_PARAMS
| DMGL_ANSI
| DMGL_TYPES
;
5701 /* Use the program name of this program, as invoked. */
5702 program_name
= argv
[0];
5704 /* Parse options. */
5707 opt_char
= getopt_long (argc
, argv
, "hpv", long_options
, NULL
);
5710 case '?': /* Unrecognized option. */
5711 print_usage (stderr
, 1);
5715 print_usage (stdout
, 0);
5719 options
&= ~ DMGL_PARAMS
;
5723 options
|= DMGL_VERBOSE
;
5727 while (opt_char
!= -1);
5730 /* No command line arguments were provided. Filter stdin. */
5732 dyn_string_t mangled
= dyn_string_new (3);
5735 /* Read all of input. */
5736 while (!feof (stdin
))
5740 /* Pile characters into mangled until we hit one that can't
5741 occur in a mangled name. */
5743 while (!feof (stdin
) && is_mangled_char (c
))
5745 dyn_string_append_char (mangled
, c
);
5751 if (dyn_string_length (mangled
) > 0)
5753 #ifdef IN_GLIBCPP_V3
5754 s
= __cxa_demangle (dyn_string_buf (mangled
), NULL
, NULL
, NULL
);
5756 s
= cplus_demangle_v3 (dyn_string_buf (mangled
), options
);
5766 /* It might not have been a mangled name. Print the
5768 fputs (dyn_string_buf (mangled
), stdout
);
5771 dyn_string_clear (mangled
);
5774 /* If we haven't hit EOF yet, we've read one character that
5775 can't occur in a mangled name, so print it out. */
5780 dyn_string_delete (mangled
);
5783 /* Demangle command line arguments. */
5785 /* Loop over command line arguments. */
5786 for (i
= optind
; i
< argc
; ++i
)
5789 #ifdef IN_GLIBCPP_V3
5793 /* Attempt to demangle. */
5794 #ifdef IN_GLIBCPP_V3
5795 s
= __cxa_demangle (argv
[i
], NULL
, NULL
, &status
);
5797 s
= cplus_demangle_v3 (argv
[i
], options
);
5800 /* If it worked, print the demangled name. */
5808 #ifdef IN_GLIBCPP_V3
5809 fprintf (stderr
, "Failed: %s (status %d)\n", argv
[i
], status
);
5811 fprintf (stderr
, "Failed: %s\n", argv
[i
]);
5820 #endif /* STANDALONE_DEMANGLER */