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_TEMPLATE_PARAM
:
512 printf ("template parameter %ld\n", dc
->u
.s_number
.number
);
514 case DEMANGLE_COMPONENT_CTOR
:
515 printf ("constructor %d\n", (int) dc
->u
.s_ctor
.kind
);
516 d_dump (dc
->u
.s_ctor
.name
, indent
+ 2);
518 case DEMANGLE_COMPONENT_DTOR
:
519 printf ("destructor %d\n", (int) dc
->u
.s_dtor
.kind
);
520 d_dump (dc
->u
.s_dtor
.name
, indent
+ 2);
522 case DEMANGLE_COMPONENT_SUB_STD
:
523 printf ("standard substitution %s\n", dc
->u
.s_string
.string
);
525 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
526 printf ("builtin type %s\n", dc
->u
.s_builtin
.type
->name
);
528 case DEMANGLE_COMPONENT_OPERATOR
:
529 printf ("operator %s\n", dc
->u
.s_operator
.op
->name
);
531 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
532 printf ("extended operator with %d args\n",
533 dc
->u
.s_extended_operator
.args
);
534 d_dump (dc
->u
.s_extended_operator
.name
, indent
+ 2);
537 case DEMANGLE_COMPONENT_QUAL_NAME
:
538 printf ("qualified name\n");
540 case DEMANGLE_COMPONENT_LOCAL_NAME
:
541 printf ("local name\n");
543 case DEMANGLE_COMPONENT_TYPED_NAME
:
544 printf ("typed name\n");
546 case DEMANGLE_COMPONENT_TEMPLATE
:
547 printf ("template\n");
549 case DEMANGLE_COMPONENT_VTABLE
:
552 case DEMANGLE_COMPONENT_VTT
:
555 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
556 printf ("construction vtable\n");
558 case DEMANGLE_COMPONENT_TYPEINFO
:
559 printf ("typeinfo\n");
561 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
562 printf ("typeinfo name\n");
564 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
565 printf ("typeinfo function\n");
567 case DEMANGLE_COMPONENT_THUNK
:
570 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
571 printf ("virtual thunk\n");
573 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
574 printf ("covariant thunk\n");
576 case DEMANGLE_COMPONENT_JAVA_CLASS
:
577 printf ("java class\n");
579 case DEMANGLE_COMPONENT_GUARD
:
582 case DEMANGLE_COMPONENT_REFTEMP
:
583 printf ("reference temporary\n");
585 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
586 printf ("hidden alias\n");
588 case DEMANGLE_COMPONENT_TRANSACTION_CLONE
:
589 printf ("transaction clone\n");
591 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
:
592 printf ("non-transaction clone\n");
594 case DEMANGLE_COMPONENT_RESTRICT
:
595 printf ("restrict\n");
597 case DEMANGLE_COMPONENT_VOLATILE
:
598 printf ("volatile\n");
600 case DEMANGLE_COMPONENT_CONST
:
603 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
604 printf ("restrict this\n");
606 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
607 printf ("volatile this\n");
609 case DEMANGLE_COMPONENT_CONST_THIS
:
610 printf ("const this\n");
612 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
613 printf ("vendor type qualifier\n");
615 case DEMANGLE_COMPONENT_POINTER
:
616 printf ("pointer\n");
618 case DEMANGLE_COMPONENT_REFERENCE
:
619 printf ("reference\n");
621 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
622 printf ("rvalue reference\n");
624 case DEMANGLE_COMPONENT_COMPLEX
:
625 printf ("complex\n");
627 case DEMANGLE_COMPONENT_IMAGINARY
:
628 printf ("imaginary\n");
630 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
631 printf ("vendor type\n");
633 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
634 printf ("function type\n");
636 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
637 printf ("array type\n");
639 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
640 printf ("pointer to member type\n");
642 case DEMANGLE_COMPONENT_FIXED_TYPE
:
643 printf ("fixed-point type\n");
645 case DEMANGLE_COMPONENT_ARGLIST
:
646 printf ("argument list\n");
648 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
649 printf ("template argument list\n");
651 case DEMANGLE_COMPONENT_CAST
:
654 case DEMANGLE_COMPONENT_UNARY
:
655 printf ("unary operator\n");
657 case DEMANGLE_COMPONENT_BINARY
:
658 printf ("binary operator\n");
660 case DEMANGLE_COMPONENT_BINARY_ARGS
:
661 printf ("binary operator arguments\n");
663 case DEMANGLE_COMPONENT_TRINARY
:
664 printf ("trinary operator\n");
666 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
667 printf ("trinary operator arguments 1\n");
669 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
670 printf ("trinary operator arguments 1\n");
672 case DEMANGLE_COMPONENT_LITERAL
:
673 printf ("literal\n");
675 case DEMANGLE_COMPONENT_LITERAL_NEG
:
676 printf ("negative literal\n");
678 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
679 printf ("java resource\n");
681 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
682 printf ("compound name\n");
684 case DEMANGLE_COMPONENT_CHARACTER
:
685 printf ("character '%c'\n", dc
->u
.s_character
.character
);
687 case DEMANGLE_COMPONENT_DECLTYPE
:
688 printf ("decltype\n");
690 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
691 printf ("pack expansion\n");
695 d_dump (d_left (dc
), indent
+ 2);
696 d_dump (d_right (dc
), indent
+ 2);
699 #endif /* CP_DEMANGLE_DEBUG */
701 /* Fill in a DEMANGLE_COMPONENT_NAME. */
703 CP_STATIC_IF_GLIBCPP_V3
705 cplus_demangle_fill_name (struct demangle_component
*p
, const char *s
, int len
)
707 if (p
== NULL
|| s
== NULL
|| len
== 0)
709 p
->type
= DEMANGLE_COMPONENT_NAME
;
711 p
->u
.s_name
.len
= len
;
715 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
717 CP_STATIC_IF_GLIBCPP_V3
719 cplus_demangle_fill_extended_operator (struct demangle_component
*p
, int args
,
720 struct demangle_component
*name
)
722 if (p
== NULL
|| args
< 0 || name
== NULL
)
724 p
->type
= DEMANGLE_COMPONENT_EXTENDED_OPERATOR
;
725 p
->u
.s_extended_operator
.args
= args
;
726 p
->u
.s_extended_operator
.name
= name
;
730 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
732 CP_STATIC_IF_GLIBCPP_V3
734 cplus_demangle_fill_ctor (struct demangle_component
*p
,
735 enum gnu_v3_ctor_kinds kind
,
736 struct demangle_component
*name
)
740 || (int) kind
< gnu_v3_complete_object_ctor
741 || (int) kind
> gnu_v3_object_ctor_group
)
743 p
->type
= DEMANGLE_COMPONENT_CTOR
;
744 p
->u
.s_ctor
.kind
= kind
;
745 p
->u
.s_ctor
.name
= name
;
749 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
751 CP_STATIC_IF_GLIBCPP_V3
753 cplus_demangle_fill_dtor (struct demangle_component
*p
,
754 enum gnu_v3_dtor_kinds kind
,
755 struct demangle_component
*name
)
759 || (int) kind
< gnu_v3_deleting_dtor
760 || (int) kind
> gnu_v3_object_dtor_group
)
762 p
->type
= DEMANGLE_COMPONENT_DTOR
;
763 p
->u
.s_dtor
.kind
= kind
;
764 p
->u
.s_dtor
.name
= name
;
768 /* Add a new component. */
770 static struct demangle_component
*
771 d_make_empty (struct d_info
*di
)
773 struct demangle_component
*p
;
775 if (di
->next_comp
>= di
->num_comps
)
777 p
= &di
->comps
[di
->next_comp
];
782 /* Add a new generic component. */
784 static struct demangle_component
*
785 d_make_comp (struct d_info
*di
, enum demangle_component_type type
,
786 struct demangle_component
*left
,
787 struct demangle_component
*right
)
789 struct demangle_component
*p
;
791 /* We check for errors here. A typical error would be a NULL return
792 from a subroutine. We catch those here, and return NULL
796 /* These types require two parameters. */
797 case DEMANGLE_COMPONENT_QUAL_NAME
:
798 case DEMANGLE_COMPONENT_LOCAL_NAME
:
799 case DEMANGLE_COMPONENT_TYPED_NAME
:
800 case DEMANGLE_COMPONENT_TEMPLATE
:
801 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
802 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
803 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
804 case DEMANGLE_COMPONENT_UNARY
:
805 case DEMANGLE_COMPONENT_BINARY
:
806 case DEMANGLE_COMPONENT_BINARY_ARGS
:
807 case DEMANGLE_COMPONENT_TRINARY
:
808 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
809 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
810 case DEMANGLE_COMPONENT_LITERAL
:
811 case DEMANGLE_COMPONENT_LITERAL_NEG
:
812 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
813 case DEMANGLE_COMPONENT_VECTOR_TYPE
:
814 case DEMANGLE_COMPONENT_CLONE
:
815 if (left
== NULL
|| right
== NULL
)
819 /* These types only require one parameter. */
820 case DEMANGLE_COMPONENT_VTABLE
:
821 case DEMANGLE_COMPONENT_VTT
:
822 case DEMANGLE_COMPONENT_TYPEINFO
:
823 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
824 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
825 case DEMANGLE_COMPONENT_THUNK
:
826 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
827 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
828 case DEMANGLE_COMPONENT_JAVA_CLASS
:
829 case DEMANGLE_COMPONENT_GUARD
:
830 case DEMANGLE_COMPONENT_REFTEMP
:
831 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
832 case DEMANGLE_COMPONENT_TRANSACTION_CLONE
:
833 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
:
834 case DEMANGLE_COMPONENT_POINTER
:
835 case DEMANGLE_COMPONENT_REFERENCE
:
836 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
837 case DEMANGLE_COMPONENT_COMPLEX
:
838 case DEMANGLE_COMPONENT_IMAGINARY
:
839 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
840 case DEMANGLE_COMPONENT_CAST
:
841 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
842 case DEMANGLE_COMPONENT_DECLTYPE
:
843 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
844 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
:
845 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS
:
850 /* This needs a right parameter, but the left parameter can be
852 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
857 /* These are allowed to have no parameters--in some cases they
858 will be filled in later. */
859 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
860 case DEMANGLE_COMPONENT_RESTRICT
:
861 case DEMANGLE_COMPONENT_VOLATILE
:
862 case DEMANGLE_COMPONENT_CONST
:
863 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
864 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
865 case DEMANGLE_COMPONENT_CONST_THIS
:
866 case DEMANGLE_COMPONENT_ARGLIST
:
867 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
870 /* Other types should not be seen here. */
875 p
= d_make_empty (di
);
879 p
->u
.s_binary
.left
= left
;
880 p
->u
.s_binary
.right
= right
;
885 /* Add a new demangle mangled name component. */
887 static struct demangle_component
*
888 d_make_demangle_mangled_name (struct d_info
*di
, const char *s
)
890 if (d_peek_char (di
) != '_' || d_peek_next_char (di
) != 'Z')
891 return d_make_name (di
, s
, strlen (s
));
893 return d_encoding (di
, 0);
896 /* Add a new name component. */
898 static struct demangle_component
*
899 d_make_name (struct d_info
*di
, const char *s
, int len
)
901 struct demangle_component
*p
;
903 p
= d_make_empty (di
);
904 if (! cplus_demangle_fill_name (p
, s
, len
))
909 /* Add a new builtin type component. */
911 static struct demangle_component
*
912 d_make_builtin_type (struct d_info
*di
,
913 const struct demangle_builtin_type_info
*type
)
915 struct demangle_component
*p
;
919 p
= d_make_empty (di
);
922 p
->type
= DEMANGLE_COMPONENT_BUILTIN_TYPE
;
923 p
->u
.s_builtin
.type
= type
;
928 /* Add a new operator component. */
930 static struct demangle_component
*
931 d_make_operator (struct d_info
*di
, const struct demangle_operator_info
*op
)
933 struct demangle_component
*p
;
935 p
= d_make_empty (di
);
938 p
->type
= DEMANGLE_COMPONENT_OPERATOR
;
939 p
->u
.s_operator
.op
= op
;
944 /* Add a new extended operator component. */
946 static struct demangle_component
*
947 d_make_extended_operator (struct d_info
*di
, int args
,
948 struct demangle_component
*name
)
950 struct demangle_component
*p
;
952 p
= d_make_empty (di
);
953 if (! cplus_demangle_fill_extended_operator (p
, args
, name
))
958 static struct demangle_component
*
959 d_make_default_arg (struct d_info
*di
, int num
,
960 struct demangle_component
*sub
)
962 struct demangle_component
*p
= d_make_empty (di
);
965 p
->type
= DEMANGLE_COMPONENT_DEFAULT_ARG
;
966 p
->u
.s_unary_num
.num
= num
;
967 p
->u
.s_unary_num
.sub
= sub
;
972 /* Add a new constructor component. */
974 static struct demangle_component
*
975 d_make_ctor (struct d_info
*di
, enum gnu_v3_ctor_kinds kind
,
976 struct demangle_component
*name
)
978 struct demangle_component
*p
;
980 p
= d_make_empty (di
);
981 if (! cplus_demangle_fill_ctor (p
, kind
, name
))
986 /* Add a new destructor component. */
988 static struct demangle_component
*
989 d_make_dtor (struct d_info
*di
, enum gnu_v3_dtor_kinds kind
,
990 struct demangle_component
*name
)
992 struct demangle_component
*p
;
994 p
= d_make_empty (di
);
995 if (! cplus_demangle_fill_dtor (p
, kind
, name
))
1000 /* Add a new template parameter. */
1002 static struct demangle_component
*
1003 d_make_template_param (struct d_info
*di
, long i
)
1005 struct demangle_component
*p
;
1007 p
= d_make_empty (di
);
1010 p
->type
= DEMANGLE_COMPONENT_TEMPLATE_PARAM
;
1011 p
->u
.s_number
.number
= i
;
1016 /* Add a new function parameter. */
1018 static struct demangle_component
*
1019 d_make_function_param (struct d_info
*di
, long i
)
1021 struct demangle_component
*p
;
1023 p
= d_make_empty (di
);
1026 p
->type
= DEMANGLE_COMPONENT_FUNCTION_PARAM
;
1027 p
->u
.s_number
.number
= i
;
1032 /* Add a new standard substitution component. */
1034 static struct demangle_component
*
1035 d_make_sub (struct d_info
*di
, const char *name
, int len
)
1037 struct demangle_component
*p
;
1039 p
= d_make_empty (di
);
1042 p
->type
= DEMANGLE_COMPONENT_SUB_STD
;
1043 p
->u
.s_string
.string
= name
;
1044 p
->u
.s_string
.len
= len
;
1049 /* <mangled-name> ::= _Z <encoding> [<clone-suffix>]*
1051 TOP_LEVEL is non-zero when called at the top level. */
1053 CP_STATIC_IF_GLIBCPP_V3
1054 struct demangle_component
*
1055 cplus_demangle_mangled_name (struct d_info
*di
, int top_level
)
1057 struct demangle_component
*p
;
1059 if (! d_check_char (di
, '_')
1060 /* Allow missing _ if not at toplevel to work around a
1061 bug in G++ abi-version=2 mangling; see the comment in
1062 write_template_arg. */
1065 if (! d_check_char (di
, 'Z'))
1067 p
= d_encoding (di
, top_level
);
1069 /* If at top level and parsing parameters, check for a clone
1071 if (top_level
&& (di
->options
& DMGL_PARAMS
) != 0)
1072 while (d_peek_char (di
) == '.'
1073 && (IS_LOWER (d_peek_next_char (di
))
1074 || d_peek_next_char (di
) == '_'
1075 || IS_DIGIT (d_peek_next_char (di
))))
1076 p
= d_clone_suffix (di
, p
);
1081 /* Return whether a function should have a return type. The argument
1082 is the function name, which may be qualified in various ways. The
1083 rules are that template functions have return types with some
1084 exceptions, function types which are not part of a function name
1085 mangling have return types with some exceptions, and non-template
1086 function names do not have return types. The exceptions are that
1087 constructors, destructors, and conversion operators do not have
1091 has_return_type (struct demangle_component
*dc
)
1099 case DEMANGLE_COMPONENT_TEMPLATE
:
1100 return ! is_ctor_dtor_or_conversion (d_left (dc
));
1101 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
1102 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
1103 case DEMANGLE_COMPONENT_CONST_THIS
:
1104 return has_return_type (d_left (dc
));
1108 /* Return whether a name is a constructor, a destructor, or a
1109 conversion operator. */
1112 is_ctor_dtor_or_conversion (struct demangle_component
*dc
)
1120 case DEMANGLE_COMPONENT_QUAL_NAME
:
1121 case DEMANGLE_COMPONENT_LOCAL_NAME
:
1122 return is_ctor_dtor_or_conversion (d_right (dc
));
1123 case DEMANGLE_COMPONENT_CTOR
:
1124 case DEMANGLE_COMPONENT_DTOR
:
1125 case DEMANGLE_COMPONENT_CAST
:
1130 /* <encoding> ::= <(function) name> <bare-function-type>
1134 TOP_LEVEL is non-zero when called at the top level, in which case
1135 if DMGL_PARAMS is not set we do not demangle the function
1136 parameters. We only set this at the top level, because otherwise
1137 we would not correctly demangle names in local scopes. */
1139 static struct demangle_component
*
1140 d_encoding (struct d_info
*di
, int top_level
)
1142 char peek
= d_peek_char (di
);
1144 if (peek
== 'G' || peek
== 'T')
1145 return d_special_name (di
);
1148 struct demangle_component
*dc
;
1152 if (dc
!= NULL
&& top_level
&& (di
->options
& DMGL_PARAMS
) == 0)
1154 /* Strip off any initial CV-qualifiers, as they really apply
1155 to the `this' parameter, and they were not output by the
1156 v2 demangler without DMGL_PARAMS. */
1157 while (dc
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
1158 || dc
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
1159 || dc
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
1162 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1163 there may be CV-qualifiers on its right argument which
1164 really apply here; this happens when parsing a class
1165 which is local to a function. */
1166 if (dc
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
1168 struct demangle_component
*dcr
;
1171 while (dcr
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
1172 || dcr
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
1173 || dcr
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
1175 dc
->u
.s_binary
.right
= dcr
;
1181 peek
= d_peek_char (di
);
1182 if (dc
== NULL
|| peek
== '\0' || peek
== 'E')
1184 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPED_NAME
, dc
,
1185 d_bare_function_type (di
, has_return_type (dc
)));
1189 /* <name> ::= <nested-name>
1191 ::= <unscoped-template-name> <template-args>
1194 <unscoped-name> ::= <unqualified-name>
1195 ::= St <unqualified-name>
1197 <unscoped-template-name> ::= <unscoped-name>
1201 static struct demangle_component
*
1202 d_name (struct d_info
*di
)
1204 char peek
= d_peek_char (di
);
1205 struct demangle_component
*dc
;
1210 return d_nested_name (di
);
1213 return d_local_name (di
);
1217 return d_unqualified_name (di
);
1223 if (d_peek_next_char (di
) != 't')
1225 dc
= d_substitution (di
, 0);
1231 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
,
1232 d_make_name (di
, "std", 3),
1233 d_unqualified_name (di
));
1238 if (d_peek_char (di
) != 'I')
1240 /* The grammar does not permit this case to occur if we
1241 called d_substitution() above (i.e., subst == 1). We
1242 don't bother to check. */
1246 /* This is <template-args>, which means that we just saw
1247 <unscoped-template-name>, which is a substitution
1248 candidate if we didn't just get it from a
1252 if (! d_add_substitution (di
, dc
))
1255 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, dc
,
1256 d_template_args (di
));
1263 dc
= d_unqualified_name (di
);
1264 if (d_peek_char (di
) == 'I')
1266 /* This is <template-args>, which means that we just saw
1267 <unscoped-template-name>, which is a substitution
1269 if (! d_add_substitution (di
, dc
))
1271 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, dc
,
1272 d_template_args (di
));
1278 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1279 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1282 static struct demangle_component
*
1283 d_nested_name (struct d_info
*di
)
1285 struct demangle_component
*ret
;
1286 struct demangle_component
**pret
;
1288 if (! d_check_char (di
, 'N'))
1291 pret
= d_cv_qualifiers (di
, &ret
, 1);
1295 *pret
= d_prefix (di
);
1299 if (! d_check_char (di
, 'E'))
1305 /* <prefix> ::= <prefix> <unqualified-name>
1306 ::= <template-prefix> <template-args>
1307 ::= <template-param>
1312 <template-prefix> ::= <prefix> <(template) unqualified-name>
1313 ::= <template-param>
1317 static struct demangle_component
*
1318 d_prefix (struct d_info
*di
)
1320 struct demangle_component
*ret
= NULL
;
1325 enum demangle_component_type comb_type
;
1326 struct demangle_component
*dc
;
1328 peek
= d_peek_char (di
);
1332 /* The older code accepts a <local-name> here, but I don't see
1333 that in the grammar. The older code does not accept a
1334 <template-param> here. */
1336 comb_type
= DEMANGLE_COMPONENT_QUAL_NAME
;
1339 char peek2
= d_peek_next_char (di
);
1340 if (peek2
== 'T' || peek2
== 't')
1342 dc
= cplus_demangle_type (di
);
1344 /* Destructor name. */
1345 dc
= d_unqualified_name (di
);
1347 else if (IS_DIGIT (peek
)
1352 dc
= d_unqualified_name (di
);
1353 else if (peek
== 'S')
1354 dc
= d_substitution (di
, 1);
1355 else if (peek
== 'I')
1359 comb_type
= DEMANGLE_COMPONENT_TEMPLATE
;
1360 dc
= d_template_args (di
);
1362 else if (peek
== 'T')
1363 dc
= d_template_param (di
);
1364 else if (peek
== 'E')
1366 else if (peek
== 'M')
1368 /* Initializer scope for a lambda. We don't need to represent
1369 this; the normal code will just treat the variable as a type
1370 scope, which gives appropriate output. */
1382 ret
= d_make_comp (di
, comb_type
, ret
, dc
);
1384 if (peek
!= 'S' && d_peek_char (di
) != 'E')
1386 if (! d_add_substitution (di
, ret
))
1392 /* <unqualified-name> ::= <operator-name>
1393 ::= <ctor-dtor-name>
1395 ::= <local-source-name>
1397 <local-source-name> ::= L <source-name> <discriminator>
1400 static struct demangle_component
*
1401 d_unqualified_name (struct d_info
*di
)
1405 peek
= d_peek_char (di
);
1406 if (IS_DIGIT (peek
))
1407 return d_source_name (di
);
1408 else if (IS_LOWER (peek
))
1410 struct demangle_component
*ret
;
1412 ret
= d_operator_name (di
);
1413 if (ret
!= NULL
&& ret
->type
== DEMANGLE_COMPONENT_OPERATOR
)
1414 di
->expansion
+= sizeof "operator" + ret
->u
.s_operator
.op
->len
- 2;
1417 else if (peek
== 'C' || peek
== 'D')
1418 return d_ctor_dtor_name (di
);
1419 else if (peek
== 'L')
1421 struct demangle_component
* ret
;
1425 ret
= d_source_name (di
);
1428 if (! d_discriminator (di
))
1432 else if (peek
== 'U')
1434 switch (d_peek_next_char (di
))
1437 return d_lambda (di
);
1439 return d_unnamed_type (di
);
1448 /* <source-name> ::= <(positive length) number> <identifier> */
1450 static struct demangle_component
*
1451 d_source_name (struct d_info
*di
)
1454 struct demangle_component
*ret
;
1456 len
= d_number (di
);
1459 ret
= d_identifier (di
, len
);
1460 di
->last_name
= ret
;
1464 /* number ::= [n] <(non-negative decimal integer)> */
1467 d_number (struct d_info
*di
)
1474 peek
= d_peek_char (di
);
1479 peek
= d_peek_char (di
);
1485 if (! IS_DIGIT (peek
))
1491 ret
= ret
* 10 + peek
- '0';
1493 peek
= d_peek_char (di
);
1497 /* Like d_number, but returns a demangle_component. */
1499 static struct demangle_component
*
1500 d_number_component (struct d_info
*di
)
1502 struct demangle_component
*ret
= d_make_empty (di
);
1505 ret
->type
= DEMANGLE_COMPONENT_NUMBER
;
1506 ret
->u
.s_number
.number
= d_number (di
);
1511 /* identifier ::= <(unqualified source code identifier)> */
1513 static struct demangle_component
*
1514 d_identifier (struct d_info
*di
, int len
)
1520 if (di
->send
- name
< len
)
1523 d_advance (di
, len
);
1525 /* A Java mangled name may have a trailing '$' if it is a C++
1526 keyword. This '$' is not included in the length count. We just
1528 if ((di
->options
& DMGL_JAVA
) != 0
1529 && d_peek_char (di
) == '$')
1532 /* Look for something which looks like a gcc encoding of an
1533 anonymous namespace, and replace it with a more user friendly
1535 if (len
>= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN
+ 2
1536 && memcmp (name
, ANONYMOUS_NAMESPACE_PREFIX
,
1537 ANONYMOUS_NAMESPACE_PREFIX_LEN
) == 0)
1541 s
= name
+ ANONYMOUS_NAMESPACE_PREFIX_LEN
;
1542 if ((*s
== '.' || *s
== '_' || *s
== '$')
1545 di
->expansion
-= len
- sizeof "(anonymous namespace)";
1546 return d_make_name (di
, "(anonymous namespace)",
1547 sizeof "(anonymous namespace)" - 1);
1551 return d_make_name (di
, name
, len
);
1554 /* operator_name ::= many different two character encodings.
1556 ::= v <digit> <source-name>
1559 #define NL(s) s, (sizeof s) - 1
1561 CP_STATIC_IF_GLIBCPP_V3
1562 const struct demangle_operator_info cplus_demangle_operators
[] =
1564 { "aN", NL ("&="), 2 },
1565 { "aS", NL ("="), 2 },
1566 { "aa", NL ("&&"), 2 },
1567 { "ad", NL ("&"), 1 },
1568 { "an", NL ("&"), 2 },
1569 { "cl", NL ("()"), 2 },
1570 { "cm", NL (","), 2 },
1571 { "co", NL ("~"), 1 },
1572 { "dV", NL ("/="), 2 },
1573 { "da", NL ("delete[]"), 1 },
1574 { "de", NL ("*"), 1 },
1575 { "dl", NL ("delete"), 1 },
1576 { "dt", NL ("."), 2 },
1577 { "dv", NL ("/"), 2 },
1578 { "eO", NL ("^="), 2 },
1579 { "eo", NL ("^"), 2 },
1580 { "eq", NL ("=="), 2 },
1581 { "ge", NL (">="), 2 },
1582 { "gt", NL (">"), 2 },
1583 { "ix", NL ("[]"), 2 },
1584 { "lS", NL ("<<="), 2 },
1585 { "le", NL ("<="), 2 },
1586 { "ls", NL ("<<"), 2 },
1587 { "lt", NL ("<"), 2 },
1588 { "mI", NL ("-="), 2 },
1589 { "mL", NL ("*="), 2 },
1590 { "mi", NL ("-"), 2 },
1591 { "ml", NL ("*"), 2 },
1592 { "mm", NL ("--"), 1 },
1593 { "na", NL ("new[]"), 1 },
1594 { "ne", NL ("!="), 2 },
1595 { "ng", NL ("-"), 1 },
1596 { "nt", NL ("!"), 1 },
1597 { "nw", NL ("new"), 1 },
1598 { "oR", NL ("|="), 2 },
1599 { "oo", NL ("||"), 2 },
1600 { "or", NL ("|"), 2 },
1601 { "pL", NL ("+="), 2 },
1602 { "pl", NL ("+"), 2 },
1603 { "pm", NL ("->*"), 2 },
1604 { "pp", NL ("++"), 1 },
1605 { "ps", NL ("+"), 1 },
1606 { "pt", NL ("->"), 2 },
1607 { "qu", NL ("?"), 3 },
1608 { "rM", NL ("%="), 2 },
1609 { "rS", NL (">>="), 2 },
1610 { "rm", NL ("%"), 2 },
1611 { "rs", NL (">>"), 2 },
1612 { "st", NL ("sizeof "), 1 },
1613 { "sz", NL ("sizeof "), 1 },
1614 { "at", NL ("alignof "), 1 },
1615 { "az", NL ("alignof "), 1 },
1616 { NULL
, NULL
, 0, 0 }
1619 static struct demangle_component
*
1620 d_operator_name (struct d_info
*di
)
1625 c1
= d_next_char (di
);
1626 c2
= d_next_char (di
);
1627 if (c1
== 'v' && IS_DIGIT (c2
))
1628 return d_make_extended_operator (di
, c2
- '0', d_source_name (di
));
1629 else if (c1
== 'c' && c2
== 'v')
1630 return d_make_comp (di
, DEMANGLE_COMPONENT_CAST
,
1631 cplus_demangle_type (di
), NULL
);
1634 /* LOW is the inclusive lower bound. */
1636 /* HIGH is the exclusive upper bound. We subtract one to ignore
1637 the sentinel at the end of the array. */
1638 int high
= ((sizeof (cplus_demangle_operators
)
1639 / sizeof (cplus_demangle_operators
[0]))
1645 const struct demangle_operator_info
*p
;
1647 i
= low
+ (high
- low
) / 2;
1648 p
= cplus_demangle_operators
+ i
;
1650 if (c1
== p
->code
[0] && c2
== p
->code
[1])
1651 return d_make_operator (di
, p
);
1653 if (c1
< p
->code
[0] || (c1
== p
->code
[0] && c2
< p
->code
[1]))
1663 static struct demangle_component
*
1664 d_make_character (struct d_info
*di
, int c
)
1666 struct demangle_component
*p
;
1667 p
= d_make_empty (di
);
1670 p
->type
= DEMANGLE_COMPONENT_CHARACTER
;
1671 p
->u
.s_character
.character
= c
;
1676 static struct demangle_component
*
1677 d_java_resource (struct d_info
*di
)
1679 struct demangle_component
*p
= NULL
;
1680 struct demangle_component
*next
= NULL
;
1685 len
= d_number (di
);
1689 /* Eat the leading '_'. */
1690 if (d_next_char (di
) != '_')
1703 /* Each chunk is either a '$' escape... */
1721 next
= d_make_character (di
, c
);
1729 /* ... or a sequence of characters. */
1732 while (i
< len
&& str
[i
] && str
[i
] != '$')
1735 next
= d_make_name (di
, str
, i
);
1748 p
= d_make_comp (di
, DEMANGLE_COMPONENT_COMPOUND_NAME
, p
, next
);
1754 p
= d_make_comp (di
, DEMANGLE_COMPONENT_JAVA_RESOURCE
, p
, NULL
);
1759 /* <special-name> ::= TV <type>
1763 ::= GV <(object) name>
1764 ::= T <call-offset> <(base) encoding>
1765 ::= Tc <call-offset> <call-offset> <(base) encoding>
1766 Also g++ extensions:
1767 ::= TC <type> <(offset) number> _ <(base) type>
1772 ::= Gr <resource name>
1777 static struct demangle_component
*
1778 d_special_name (struct d_info
*di
)
1780 di
->expansion
+= 20;
1781 if (d_check_char (di
, 'T'))
1783 switch (d_next_char (di
))
1787 return d_make_comp (di
, DEMANGLE_COMPONENT_VTABLE
,
1788 cplus_demangle_type (di
), NULL
);
1790 di
->expansion
-= 10;
1791 return d_make_comp (di
, DEMANGLE_COMPONENT_VTT
,
1792 cplus_demangle_type (di
), NULL
);
1794 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO
,
1795 cplus_demangle_type (di
), NULL
);
1797 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO_NAME
,
1798 cplus_demangle_type (di
), NULL
);
1801 if (! d_call_offset (di
, 'h'))
1803 return d_make_comp (di
, DEMANGLE_COMPONENT_THUNK
,
1804 d_encoding (di
, 0), NULL
);
1807 if (! d_call_offset (di
, 'v'))
1809 return d_make_comp (di
, DEMANGLE_COMPONENT_VIRTUAL_THUNK
,
1810 d_encoding (di
, 0), NULL
);
1813 if (! d_call_offset (di
, '\0'))
1815 if (! d_call_offset (di
, '\0'))
1817 return d_make_comp (di
, DEMANGLE_COMPONENT_COVARIANT_THUNK
,
1818 d_encoding (di
, 0), NULL
);
1822 struct demangle_component
*derived_type
;
1824 struct demangle_component
*base_type
;
1826 derived_type
= cplus_demangle_type (di
);
1827 offset
= d_number (di
);
1830 if (! d_check_char (di
, '_'))
1832 base_type
= cplus_demangle_type (di
);
1833 /* We don't display the offset. FIXME: We should display
1834 it in verbose mode. */
1836 return d_make_comp (di
, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
,
1837 base_type
, derived_type
);
1841 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO_FN
,
1842 cplus_demangle_type (di
), NULL
);
1844 return d_make_comp (di
, DEMANGLE_COMPONENT_JAVA_CLASS
,
1845 cplus_demangle_type (di
), NULL
);
1851 else if (d_check_char (di
, 'G'))
1853 switch (d_next_char (di
))
1856 return d_make_comp (di
, DEMANGLE_COMPONENT_GUARD
, d_name (di
), NULL
);
1860 struct demangle_component
*name
= d_name (di
);
1861 return d_make_comp (di
, DEMANGLE_COMPONENT_REFTEMP
, name
,
1862 d_number_component (di
));
1866 return d_make_comp (di
, DEMANGLE_COMPONENT_HIDDEN_ALIAS
,
1867 d_encoding (di
, 0), NULL
);
1870 switch (d_next_char (di
))
1873 return d_make_comp (di
, DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
,
1874 d_encoding (di
, 0), NULL
);
1876 /* ??? The proposal is that other letters (such as 'h') stand
1877 for different variants of transaction cloning, such as
1878 compiling directly for hardware transaction support. But
1879 they still should all be transactional clones of some sort
1880 so go ahead and call them that. */
1882 return d_make_comp (di
, DEMANGLE_COMPONENT_TRANSACTION_CLONE
,
1883 d_encoding (di
, 0), NULL
);
1887 return d_java_resource (di
);
1897 /* <call-offset> ::= h <nv-offset> _
1900 <nv-offset> ::= <(offset) number>
1902 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1904 The C parameter, if not '\0', is a character we just read which is
1905 the start of the <call-offset>.
1907 We don't display the offset information anywhere. FIXME: We should
1908 display it in verbose mode. */
1911 d_call_offset (struct d_info
*di
, int c
)
1914 c
= d_next_char (di
);
1921 if (! d_check_char (di
, '_'))
1928 if (! d_check_char (di
, '_'))
1934 /* <ctor-dtor-name> ::= C1
1942 static struct demangle_component
*
1943 d_ctor_dtor_name (struct d_info
*di
)
1945 if (di
->last_name
!= NULL
)
1947 if (di
->last_name
->type
== DEMANGLE_COMPONENT_NAME
)
1948 di
->expansion
+= di
->last_name
->u
.s_name
.len
;
1949 else if (di
->last_name
->type
== DEMANGLE_COMPONENT_SUB_STD
)
1950 di
->expansion
+= di
->last_name
->u
.s_string
.len
;
1952 switch (d_peek_char (di
))
1956 enum gnu_v3_ctor_kinds kind
;
1958 switch (d_peek_next_char (di
))
1961 kind
= gnu_v3_complete_object_ctor
;
1964 kind
= gnu_v3_base_object_ctor
;
1967 kind
= gnu_v3_complete_object_allocating_ctor
;
1970 kind
= gnu_v3_object_ctor_group
;
1976 return d_make_ctor (di
, kind
, di
->last_name
);
1981 enum gnu_v3_dtor_kinds kind
;
1983 switch (d_peek_next_char (di
))
1986 kind
= gnu_v3_deleting_dtor
;
1989 kind
= gnu_v3_complete_object_dtor
;
1992 kind
= gnu_v3_base_object_dtor
;
1995 kind
= gnu_v3_object_dtor_group
;
2001 return d_make_dtor (di
, kind
, di
->last_name
);
2009 /* <type> ::= <builtin-type>
2011 ::= <class-enum-type>
2013 ::= <pointer-to-member-type>
2014 ::= <template-param>
2015 ::= <template-template-param> <template-args>
2017 ::= <CV-qualifiers> <type>
2020 ::= O <type> (C++0x)
2023 ::= U <source-name> <type>
2025 <builtin-type> ::= various one letter codes
2029 CP_STATIC_IF_GLIBCPP_V3
2030 const struct demangle_builtin_type_info
2031 cplus_demangle_builtin_types
[D_BUILTIN_TYPE_COUNT
] =
2033 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT
},
2034 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL
},
2035 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT
},
2036 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT
},
2037 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT
},
2038 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT
},
2039 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT
},
2040 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT
},
2041 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT
},
2042 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED
},
2043 /* k */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2044 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG
},
2045 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG
},
2046 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT
},
2047 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
2049 /* p */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2050 /* q */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2051 /* r */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2052 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT
},
2053 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT
},
2054 /* u */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2055 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID
},
2056 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT
},
2057 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG
},
2058 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
2059 D_PRINT_UNSIGNED_LONG_LONG
},
2060 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT
},
2061 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT
},
2062 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT
},
2063 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT
},
2064 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT
},
2065 /* 30 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT
},
2066 /* 31 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT
},
2067 /* 32 */ { NL ("decltype(nullptr)"), NL ("decltype(nullptr)"),
2071 CP_STATIC_IF_GLIBCPP_V3
2072 struct demangle_component
*
2073 cplus_demangle_type (struct d_info
*di
)
2076 struct demangle_component
*ret
;
2079 /* The ABI specifies that when CV-qualifiers are used, the base type
2080 is substitutable, and the fully qualified type is substitutable,
2081 but the base type with a strict subset of the CV-qualifiers is
2082 not substitutable. The natural recursive implementation of the
2083 CV-qualifiers would cause subsets to be substitutable, so instead
2084 we pull them all off now.
2086 FIXME: The ABI says that order-insensitive vendor qualifiers
2087 should be handled in the same way, but we have no way to tell
2088 which vendor qualifiers are order-insensitive and which are
2089 order-sensitive. So we just assume that they are all
2090 order-sensitive. g++ 3.4 supports only one vendor qualifier,
2091 __vector, and it treats it as order-sensitive when mangling
2094 peek
= d_peek_char (di
);
2095 if (peek
== 'r' || peek
== 'V' || peek
== 'K')
2097 struct demangle_component
**pret
;
2099 pret
= d_cv_qualifiers (di
, &ret
, 0);
2102 *pret
= cplus_demangle_type (di
);
2103 if (! *pret
|| ! d_add_substitution (di
, ret
))
2112 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2113 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
2114 case 'o': case 's': case 't':
2115 case 'v': case 'w': case 'x': case 'y': case 'z':
2116 ret
= d_make_builtin_type (di
,
2117 &cplus_demangle_builtin_types
[peek
- 'a']);
2118 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2125 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_VENDOR_TYPE
,
2126 d_source_name (di
), NULL
);
2130 ret
= d_function_type (di
);
2133 case '0': case '1': case '2': case '3': case '4':
2134 case '5': case '6': case '7': case '8': case '9':
2137 ret
= d_class_enum_type (di
);
2141 ret
= d_array_type (di
);
2145 ret
= d_pointer_to_member_type (di
);
2149 ret
= d_template_param (di
);
2150 if (d_peek_char (di
) == 'I')
2152 /* This is <template-template-param> <template-args>. The
2153 <template-template-param> part is a substitution
2155 if (! d_add_substitution (di
, ret
))
2157 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
,
2158 d_template_args (di
));
2163 /* If this is a special substitution, then it is the start of
2164 <class-enum-type>. */
2168 peek_next
= d_peek_next_char (di
);
2169 if (IS_DIGIT (peek_next
)
2171 || IS_UPPER (peek_next
))
2173 ret
= d_substitution (di
, 0);
2174 /* The substituted name may have been a template name and
2175 may be followed by tepmlate args. */
2176 if (d_peek_char (di
) == 'I')
2177 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
,
2178 d_template_args (di
));
2184 ret
= d_class_enum_type (di
);
2185 /* If the substitution was a complete type, then it is not
2186 a new substitution candidate. However, if the
2187 substitution was followed by template arguments, then
2188 the whole thing is a substitution candidate. */
2189 if (ret
!= NULL
&& ret
->type
== DEMANGLE_COMPONENT_SUB_STD
)
2197 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_RVALUE_REFERENCE
,
2198 cplus_demangle_type (di
), NULL
);
2203 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_POINTER
,
2204 cplus_demangle_type (di
), NULL
);
2209 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_REFERENCE
,
2210 cplus_demangle_type (di
), NULL
);
2215 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_COMPLEX
,
2216 cplus_demangle_type (di
), NULL
);
2221 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_IMAGINARY
,
2222 cplus_demangle_type (di
), NULL
);
2227 ret
= d_source_name (di
);
2228 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
,
2229 cplus_demangle_type (di
), ret
);
2235 peek
= d_next_char (di
);
2240 /* decltype (expression) */
2241 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_DECLTYPE
,
2242 d_expression (di
), NULL
);
2243 if (ret
&& d_next_char (di
) != 'E')
2248 /* Pack expansion. */
2249 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_PACK_EXPANSION
,
2250 cplus_demangle_type (di
), NULL
);
2254 /* 32-bit decimal floating point */
2255 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[26]);
2256 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2260 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[27]);
2261 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2265 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[28]);
2266 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2269 /* 16-bit half-precision FP */
2270 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[29]);
2271 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2275 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[30]);
2276 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2280 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[31]);
2281 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2285 /* Fixed point types. DF<int bits><length><fract bits><sat> */
2286 ret
= d_make_empty (di
);
2287 ret
->type
= DEMANGLE_COMPONENT_FIXED_TYPE
;
2288 if ((ret
->u
.s_fixed
.accum
= IS_DIGIT (d_peek_char (di
))))
2289 /* For demangling we don't care about the bits. */
2291 ret
->u
.s_fixed
.length
= cplus_demangle_type (di
);
2292 if (ret
->u
.s_fixed
.length
== NULL
)
2295 peek
= d_next_char (di
);
2296 ret
->u
.s_fixed
.sat
= (peek
== 's');
2300 ret
= d_vector_type (di
);
2304 /* decltype(nullptr) */
2305 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[32]);
2306 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2320 if (! d_add_substitution (di
, ret
))
2327 /* <CV-qualifiers> ::= [r] [V] [K] */
2329 static struct demangle_component
**
2330 d_cv_qualifiers (struct d_info
*di
,
2331 struct demangle_component
**pret
, int member_fn
)
2333 struct demangle_component
**pstart
;
2337 peek
= d_peek_char (di
);
2338 while (peek
== 'r' || peek
== 'V' || peek
== 'K')
2340 enum demangle_component_type t
;
2346 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2347 : DEMANGLE_COMPONENT_RESTRICT
);
2348 di
->expansion
+= sizeof "restrict";
2350 else if (peek
== 'V')
2353 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2354 : DEMANGLE_COMPONENT_VOLATILE
);
2355 di
->expansion
+= sizeof "volatile";
2360 ? DEMANGLE_COMPONENT_CONST_THIS
2361 : DEMANGLE_COMPONENT_CONST
);
2362 di
->expansion
+= sizeof "const";
2365 *pret
= d_make_comp (di
, t
, NULL
, NULL
);
2368 pret
= &d_left (*pret
);
2370 peek
= d_peek_char (di
);
2373 if (!member_fn
&& peek
== 'F')
2375 while (pstart
!= pret
)
2377 switch ((*pstart
)->type
)
2379 case DEMANGLE_COMPONENT_RESTRICT
:
2380 (*pstart
)->type
= DEMANGLE_COMPONENT_RESTRICT_THIS
;
2382 case DEMANGLE_COMPONENT_VOLATILE
:
2383 (*pstart
)->type
= DEMANGLE_COMPONENT_VOLATILE_THIS
;
2385 case DEMANGLE_COMPONENT_CONST
:
2386 (*pstart
)->type
= DEMANGLE_COMPONENT_CONST_THIS
;
2391 pstart
= &d_left (*pstart
);
2398 /* <function-type> ::= F [Y] <bare-function-type> E */
2400 static struct demangle_component
*
2401 d_function_type (struct d_info
*di
)
2403 struct demangle_component
*ret
;
2405 if (! d_check_char (di
, 'F'))
2407 if (d_peek_char (di
) == 'Y')
2409 /* Function has C linkage. We don't print this information.
2410 FIXME: We should print it in verbose mode. */
2413 ret
= d_bare_function_type (di
, 1);
2414 if (! d_check_char (di
, 'E'))
2421 static struct demangle_component
*
2422 d_parmlist (struct d_info
*di
)
2424 struct demangle_component
*tl
;
2425 struct demangle_component
**ptl
;
2431 struct demangle_component
*type
;
2433 char peek
= d_peek_char (di
);
2434 if (peek
== '\0' || peek
== 'E' || peek
== '.')
2436 type
= cplus_demangle_type (di
);
2439 *ptl
= d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, type
, NULL
);
2442 ptl
= &d_right (*ptl
);
2445 /* There should be at least one parameter type besides the optional
2446 return type. A function which takes no arguments will have a
2447 single parameter type void. */
2451 /* If we have a single parameter type void, omit it. */
2452 if (d_right (tl
) == NULL
2453 && d_left (tl
)->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
2454 && d_left (tl
)->u
.s_builtin
.type
->print
== D_PRINT_VOID
)
2456 di
->expansion
-= d_left (tl
)->u
.s_builtin
.type
->len
;
2463 /* <bare-function-type> ::= [J]<type>+ */
2465 static struct demangle_component
*
2466 d_bare_function_type (struct d_info
*di
, int has_return_type
)
2468 struct demangle_component
*return_type
;
2469 struct demangle_component
*tl
;
2472 /* Detect special qualifier indicating that the first argument
2473 is the return type. */
2474 peek
= d_peek_char (di
);
2478 has_return_type
= 1;
2481 if (has_return_type
)
2483 return_type
= cplus_demangle_type (di
);
2484 if (return_type
== NULL
)
2490 tl
= d_parmlist (di
);
2494 return d_make_comp (di
, DEMANGLE_COMPONENT_FUNCTION_TYPE
,
2498 /* <class-enum-type> ::= <name> */
2500 static struct demangle_component
*
2501 d_class_enum_type (struct d_info
*di
)
2506 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2507 ::= A [<(dimension) expression>] _ <(element) type>
2510 static struct demangle_component
*
2511 d_array_type (struct d_info
*di
)
2514 struct demangle_component
*dim
;
2516 if (! d_check_char (di
, 'A'))
2519 peek
= d_peek_char (di
);
2522 else if (IS_DIGIT (peek
))
2530 peek
= d_peek_char (di
);
2532 while (IS_DIGIT (peek
));
2533 dim
= d_make_name (di
, s
, d_str (di
) - s
);
2539 dim
= d_expression (di
);
2544 if (! d_check_char (di
, '_'))
2547 return d_make_comp (di
, DEMANGLE_COMPONENT_ARRAY_TYPE
, dim
,
2548 cplus_demangle_type (di
));
2551 /* <vector-type> ::= Dv <number> _ <type>
2552 ::= Dv _ <expression> _ <type> */
2554 static struct demangle_component
*
2555 d_vector_type (struct d_info
*di
)
2558 struct demangle_component
*dim
;
2560 peek
= d_peek_char (di
);
2564 dim
= d_expression (di
);
2567 dim
= d_number_component (di
);
2572 if (! d_check_char (di
, '_'))
2575 return d_make_comp (di
, DEMANGLE_COMPONENT_VECTOR_TYPE
, dim
,
2576 cplus_demangle_type (di
));
2579 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2581 static struct demangle_component
*
2582 d_pointer_to_member_type (struct d_info
*di
)
2584 struct demangle_component
*cl
;
2585 struct demangle_component
*mem
;
2586 struct demangle_component
**pmem
;
2588 if (! d_check_char (di
, 'M'))
2591 cl
= cplus_demangle_type (di
);
2593 /* The ABI specifies that any type can be a substitution source, and
2594 that M is followed by two types, and that when a CV-qualified
2595 type is seen both the base type and the CV-qualified types are
2596 substitution sources. The ABI also specifies that for a pointer
2597 to a CV-qualified member function, the qualifiers are attached to
2598 the second type. Given the grammar, a plain reading of the ABI
2599 suggests that both the CV-qualified member function and the
2600 non-qualified member function are substitution sources. However,
2601 g++ does not work that way. g++ treats only the CV-qualified
2602 member function as a substitution source. FIXME. So to work
2603 with g++, we need to pull off the CV-qualifiers here, in order to
2604 avoid calling add_substitution() in cplus_demangle_type(). But
2605 for a CV-qualified member which is not a function, g++ does
2606 follow the ABI, so we need to handle that case here by calling
2607 d_add_substitution ourselves. */
2609 pmem
= d_cv_qualifiers (di
, &mem
, 1);
2612 *pmem
= cplus_demangle_type (di
);
2616 if (pmem
!= &mem
&& (*pmem
)->type
!= DEMANGLE_COMPONENT_FUNCTION_TYPE
)
2618 if (! d_add_substitution (di
, mem
))
2622 return d_make_comp (di
, DEMANGLE_COMPONENT_PTRMEM_TYPE
, cl
, mem
);
2625 /* <non-negative number> _ */
2628 d_compact_number (struct d_info
*di
)
2631 if (d_peek_char (di
) == '_')
2633 else if (d_peek_char (di
) == 'n')
2636 num
= d_number (di
) + 1;
2638 if (! d_check_char (di
, '_'))
2643 /* <template-param> ::= T_
2644 ::= T <(parameter-2 non-negative) number> _
2647 static struct demangle_component
*
2648 d_template_param (struct d_info
*di
)
2652 if (! d_check_char (di
, 'T'))
2655 param
= d_compact_number (di
);
2661 return d_make_template_param (di
, param
);
2664 /* <template-args> ::= I <template-arg>+ E */
2666 static struct demangle_component
*
2667 d_template_args (struct d_info
*di
)
2669 struct demangle_component
*hold_last_name
;
2670 struct demangle_component
*al
;
2671 struct demangle_component
**pal
;
2673 /* Preserve the last name we saw--don't let the template arguments
2674 clobber it, as that would give us the wrong name for a subsequent
2675 constructor or destructor. */
2676 hold_last_name
= di
->last_name
;
2678 if (! d_check_char (di
, 'I'))
2681 if (d_peek_char (di
) == 'E')
2683 /* An argument pack can be empty. */
2685 return d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
, NULL
, NULL
);
2692 struct demangle_component
*a
;
2694 a
= d_template_arg (di
);
2698 *pal
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
, a
, NULL
);
2701 pal
= &d_right (*pal
);
2703 if (d_peek_char (di
) == 'E')
2710 di
->last_name
= hold_last_name
;
2715 /* <template-arg> ::= <type>
2716 ::= X <expression> E
2720 static struct demangle_component
*
2721 d_template_arg (struct d_info
*di
)
2723 struct demangle_component
*ret
;
2725 switch (d_peek_char (di
))
2729 ret
= d_expression (di
);
2730 if (! d_check_char (di
, 'E'))
2735 return d_expr_primary (di
);
2738 /* An argument pack. */
2739 return d_template_args (di
);
2742 return cplus_demangle_type (di
);
2746 /* Subroutine of <expression> ::= cl <expression>+ E */
2748 static struct demangle_component
*
2749 d_exprlist (struct d_info
*di
)
2751 struct demangle_component
*list
= NULL
;
2752 struct demangle_component
**p
= &list
;
2754 if (d_peek_char (di
) == 'E')
2757 return d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, NULL
, NULL
);
2762 struct demangle_component
*arg
= d_expression (di
);
2766 *p
= d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, arg
, NULL
);
2771 if (d_peek_char (di
) == 'E')
2781 /* <expression> ::= <(unary) operator-name> <expression>
2782 ::= <(binary) operator-name> <expression> <expression>
2783 ::= <(trinary) operator-name> <expression> <expression> <expression>
2784 ::= cl <expression>+ E
2786 ::= <template-param>
2787 ::= sr <type> <unqualified-name>
2788 ::= sr <type> <unqualified-name> <template-args>
2792 static struct demangle_component
*
2793 d_expression (struct d_info
*di
)
2797 peek
= d_peek_char (di
);
2799 return d_expr_primary (di
);
2800 else if (peek
== 'T')
2801 return d_template_param (di
);
2802 else if (peek
== 's' && d_peek_next_char (di
) == 'r')
2804 struct demangle_component
*type
;
2805 struct demangle_component
*name
;
2808 type
= cplus_demangle_type (di
);
2809 name
= d_unqualified_name (di
);
2810 if (d_peek_char (di
) != 'I')
2811 return d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
, type
, name
);
2813 return d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
, type
,
2814 d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, name
,
2815 d_template_args (di
)));
2817 else if (peek
== 's' && d_peek_next_char (di
) == 'p')
2820 return d_make_comp (di
, DEMANGLE_COMPONENT_PACK_EXPANSION
,
2821 d_expression (di
), NULL
);
2823 else if (peek
== 'f' && d_peek_next_char (di
) == 'p')
2825 /* Function parameter used in a late-specified return type. */
2828 if (d_peek_char (di
) == 'T')
2830 /* 'this' parameter. */
2836 index
= d_compact_number (di
) + 1;
2840 return d_make_function_param (di
, index
);
2842 else if (IS_DIGIT (peek
)
2843 || (peek
== 'o' && d_peek_next_char (di
) == 'n'))
2845 /* We can get an unqualified name as an expression in the case of
2846 a dependent function call, i.e. decltype(f(t)). */
2847 struct demangle_component
*name
;
2850 /* operator-function-id, i.e. operator+(t). */
2853 name
= d_unqualified_name (di
);
2856 if (d_peek_char (di
) == 'I')
2857 return d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, name
,
2858 d_template_args (di
));
2864 struct demangle_component
*op
;
2867 op
= d_operator_name (di
);
2871 if (op
->type
== DEMANGLE_COMPONENT_OPERATOR
)
2872 di
->expansion
+= op
->u
.s_operator
.op
->len
- 2;
2874 if (op
->type
== DEMANGLE_COMPONENT_OPERATOR
2875 && strcmp (op
->u
.s_operator
.op
->code
, "st") == 0)
2876 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
2877 cplus_demangle_type (di
));
2883 case DEMANGLE_COMPONENT_OPERATOR
:
2884 args
= op
->u
.s_operator
.op
->args
;
2886 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
2887 args
= op
->u
.s_extended_operator
.args
;
2889 case DEMANGLE_COMPONENT_CAST
:
2898 struct demangle_component
*operand
;
2899 if (op
->type
== DEMANGLE_COMPONENT_CAST
2900 && d_check_char (di
, '_'))
2901 operand
= d_exprlist (di
);
2903 operand
= d_expression (di
);
2904 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
2909 struct demangle_component
*left
;
2910 struct demangle_component
*right
;
2911 const char *code
= op
->u
.s_operator
.op
->code
;
2913 left
= d_expression (di
);
2914 if (!strcmp (code
, "cl"))
2915 right
= d_exprlist (di
);
2916 else if (!strcmp (code
, "dt") || !strcmp (code
, "pt"))
2918 right
= d_unqualified_name (di
);
2919 if (d_peek_char (di
) == 'I')
2920 right
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
,
2921 right
, d_template_args (di
));
2924 right
= d_expression (di
);
2926 return d_make_comp (di
, DEMANGLE_COMPONENT_BINARY
, op
,
2928 DEMANGLE_COMPONENT_BINARY_ARGS
,
2933 struct demangle_component
*first
;
2934 struct demangle_component
*second
;
2936 first
= d_expression (di
);
2937 second
= d_expression (di
);
2938 return d_make_comp (di
, DEMANGLE_COMPONENT_TRINARY
, op
,
2940 DEMANGLE_COMPONENT_TRINARY_ARG1
,
2943 DEMANGLE_COMPONENT_TRINARY_ARG2
,
2945 d_expression (di
))));
2953 /* <expr-primary> ::= L <type> <(value) number> E
2954 ::= L <type> <(value) float> E
2955 ::= L <mangled-name> E
2958 static struct demangle_component
*
2959 d_expr_primary (struct d_info
*di
)
2961 struct demangle_component
*ret
;
2963 if (! d_check_char (di
, 'L'))
2965 if (d_peek_char (di
) == '_'
2966 /* Workaround for G++ bug; see comment in write_template_arg. */
2967 || d_peek_char (di
) == 'Z')
2968 ret
= cplus_demangle_mangled_name (di
, 0);
2971 struct demangle_component
*type
;
2972 enum demangle_component_type t
;
2975 type
= cplus_demangle_type (di
);
2979 /* If we have a type we know how to print, we aren't going to
2980 print the type name itself. */
2981 if (type
->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
2982 && type
->u
.s_builtin
.type
->print
!= D_PRINT_DEFAULT
)
2983 di
->expansion
-= type
->u
.s_builtin
.type
->len
;
2985 /* Rather than try to interpret the literal value, we just
2986 collect it as a string. Note that it's possible to have a
2987 floating point literal here. The ABI specifies that the
2988 format of such literals is machine independent. That's fine,
2989 but what's not fine is that versions of g++ up to 3.2 with
2990 -fabi-version=1 used upper case letters in the hex constant,
2991 and dumped out gcc's internal representation. That makes it
2992 hard to tell where the constant ends, and hard to dump the
2993 constant in any readable form anyhow. We don't attempt to
2994 handle these cases. */
2996 t
= DEMANGLE_COMPONENT_LITERAL
;
2997 if (d_peek_char (di
) == 'n')
2999 t
= DEMANGLE_COMPONENT_LITERAL_NEG
;
3003 while (d_peek_char (di
) != 'E')
3005 if (d_peek_char (di
) == '\0')
3009 ret
= d_make_comp (di
, t
, type
, d_make_name (di
, s
, d_str (di
) - s
));
3011 if (! d_check_char (di
, 'E'))
3016 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
3017 ::= Z <(function) encoding> E s [<discriminator>]
3020 static struct demangle_component
*
3021 d_local_name (struct d_info
*di
)
3023 struct demangle_component
*function
;
3025 if (! d_check_char (di
, 'Z'))
3028 function
= d_encoding (di
, 0);
3030 if (! d_check_char (di
, 'E'))
3033 if (d_peek_char (di
) == 's')
3036 if (! d_discriminator (di
))
3038 return d_make_comp (di
, DEMANGLE_COMPONENT_LOCAL_NAME
, function
,
3039 d_make_name (di
, "string literal",
3040 sizeof "string literal" - 1));
3044 struct demangle_component
*name
;
3047 if (d_peek_char (di
) == 'd')
3049 /* Default argument scope: d <number> _. */
3051 num
= d_compact_number (di
);
3060 /* Lambdas and unnamed types have internal discriminators. */
3061 case DEMANGLE_COMPONENT_LAMBDA
:
3062 case DEMANGLE_COMPONENT_UNNAMED_TYPE
:
3065 if (! d_discriminator (di
))
3069 name
= d_make_default_arg (di
, num
, name
);
3070 return d_make_comp (di
, DEMANGLE_COMPONENT_LOCAL_NAME
, function
, name
);
3074 /* <discriminator> ::= _ <(non-negative) number>
3076 We demangle the discriminator, but we don't print it out. FIXME:
3077 We should print it out in verbose mode. */
3080 d_discriminator (struct d_info
*di
)
3084 if (d_peek_char (di
) != '_')
3087 discrim
= d_number (di
);
3093 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ */
3095 static struct demangle_component
*
3096 d_lambda (struct d_info
*di
)
3098 struct demangle_component
*tl
;
3099 struct demangle_component
*ret
;
3102 if (! d_check_char (di
, 'U'))
3104 if (! d_check_char (di
, 'l'))
3107 tl
= d_parmlist (di
);
3111 if (! d_check_char (di
, 'E'))
3114 num
= d_compact_number (di
);
3118 ret
= d_make_empty (di
);
3121 ret
->type
= DEMANGLE_COMPONENT_LAMBDA
;
3122 ret
->u
.s_unary_num
.sub
= tl
;
3123 ret
->u
.s_unary_num
.num
= num
;
3126 if (! d_add_substitution (di
, ret
))
3132 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
3134 static struct demangle_component
*
3135 d_unnamed_type (struct d_info
*di
)
3137 struct demangle_component
*ret
;
3140 if (! d_check_char (di
, 'U'))
3142 if (! d_check_char (di
, 't'))
3145 num
= d_compact_number (di
);
3149 ret
= d_make_empty (di
);
3152 ret
->type
= DEMANGLE_COMPONENT_UNNAMED_TYPE
;
3153 ret
->u
.s_number
.number
= num
;
3156 if (! d_add_substitution (di
, ret
))
3162 /* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]*
3165 static struct demangle_component
*
3166 d_clone_suffix (struct d_info
*di
, struct demangle_component
*encoding
)
3168 const char *suffix
= d_str (di
);
3169 const char *pend
= suffix
;
3170 struct demangle_component
*n
;
3172 if (*pend
== '.' && (IS_LOWER (pend
[1]) || pend
[1] == '_'))
3175 while (IS_LOWER (*pend
) || *pend
== '_')
3178 while (*pend
== '.' && IS_DIGIT (pend
[1]))
3181 while (IS_DIGIT (*pend
))
3184 d_advance (di
, pend
- suffix
);
3185 n
= d_make_name (di
, suffix
, pend
- suffix
);
3186 return d_make_comp (di
, DEMANGLE_COMPONENT_CLONE
, encoding
, n
);
3189 /* Add a new substitution. */
3192 d_add_substitution (struct d_info
*di
, struct demangle_component
*dc
)
3196 if (di
->next_sub
>= di
->num_subs
)
3198 di
->subs
[di
->next_sub
] = dc
;
3203 /* <substitution> ::= S <seq-id> _
3213 If PREFIX is non-zero, then this type is being used as a prefix in
3214 a qualified name. In this case, for the standard substitutions, we
3215 need to check whether we are being used as a prefix for a
3216 constructor or destructor, and return a full template name.
3217 Otherwise we will get something like std::iostream::~iostream()
3218 which does not correspond particularly well to any function which
3219 actually appears in the source.
3222 static const struct d_standard_sub_info standard_subs
[] =
3227 { 'a', NL ("std::allocator"),
3228 NL ("std::allocator"),
3230 { 'b', NL ("std::basic_string"),
3231 NL ("std::basic_string"),
3232 NL ("basic_string") },
3233 { 's', NL ("std::string"),
3234 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
3235 NL ("basic_string") },
3236 { 'i', NL ("std::istream"),
3237 NL ("std::basic_istream<char, std::char_traits<char> >"),
3238 NL ("basic_istream") },
3239 { 'o', NL ("std::ostream"),
3240 NL ("std::basic_ostream<char, std::char_traits<char> >"),
3241 NL ("basic_ostream") },
3242 { 'd', NL ("std::iostream"),
3243 NL ("std::basic_iostream<char, std::char_traits<char> >"),
3244 NL ("basic_iostream") }
3247 static struct demangle_component
*
3248 d_substitution (struct d_info
*di
, int prefix
)
3252 if (! d_check_char (di
, 'S'))
3255 c
= d_next_char (di
);
3256 if (c
== '_' || IS_DIGIT (c
) || IS_UPPER (c
))
3265 unsigned int new_id
;
3268 new_id
= id
* 36 + c
- '0';
3269 else if (IS_UPPER (c
))
3270 new_id
= id
* 36 + c
- 'A' + 10;
3276 c
= d_next_char (di
);
3283 if (id
>= (unsigned int) di
->next_sub
)
3288 return di
->subs
[id
];
3293 const struct d_standard_sub_info
*p
;
3294 const struct d_standard_sub_info
*pend
;
3296 verbose
= (di
->options
& DMGL_VERBOSE
) != 0;
3297 if (! verbose
&& prefix
)
3301 peek
= d_peek_char (di
);
3302 if (peek
== 'C' || peek
== 'D')
3306 pend
= (&standard_subs
[0]
3307 + sizeof standard_subs
/ sizeof standard_subs
[0]);
3308 for (p
= &standard_subs
[0]; p
< pend
; ++p
)
3315 if (p
->set_last_name
!= NULL
)
3316 di
->last_name
= d_make_sub (di
, p
->set_last_name
,
3317 p
->set_last_name_len
);
3320 s
= p
->full_expansion
;
3325 s
= p
->simple_expansion
;
3326 len
= p
->simple_len
;
3328 di
->expansion
+= len
;
3329 return d_make_sub (di
, s
, len
);
3337 /* Initialize a growable string. */
3340 d_growable_string_init (struct d_growable_string
*dgs
, size_t estimate
)
3345 dgs
->allocation_failure
= 0;
3348 d_growable_string_resize (dgs
, estimate
);
3351 /* Grow a growable string to a given size. */
3354 d_growable_string_resize (struct d_growable_string
*dgs
, size_t need
)
3359 if (dgs
->allocation_failure
)
3362 /* Start allocation at two bytes to avoid any possibility of confusion
3363 with the special value of 1 used as a return in *palc to indicate
3364 allocation failures. */
3365 newalc
= dgs
->alc
> 0 ? dgs
->alc
: 2;
3366 while (newalc
< need
)
3369 newbuf
= (char *) realloc (dgs
->buf
, newalc
);
3376 dgs
->allocation_failure
= 1;
3383 /* Append a buffer to a growable string. */
3386 d_growable_string_append_buffer (struct d_growable_string
*dgs
,
3387 const char *s
, size_t l
)
3391 need
= dgs
->len
+ l
+ 1;
3392 if (need
> dgs
->alc
)
3393 d_growable_string_resize (dgs
, need
);
3395 if (dgs
->allocation_failure
)
3398 memcpy (dgs
->buf
+ dgs
->len
, s
, l
);
3399 dgs
->buf
[dgs
->len
+ l
] = '\0';
3403 /* Bridge growable strings to the callback mechanism. */
3406 d_growable_string_callback_adapter (const char *s
, size_t l
, void *opaque
)
3408 struct d_growable_string
*dgs
= (struct d_growable_string
*) opaque
;
3410 d_growable_string_append_buffer (dgs
, s
, l
);
3413 /* Initialize a print information structure. */
3416 d_print_init (struct d_print_info
*dpi
, demangle_callbackref callback
,
3420 dpi
->last_char
= '\0';
3421 dpi
->templates
= NULL
;
3422 dpi
->modifiers
= NULL
;
3423 dpi
->pack_index
= 0;
3424 dpi
->flush_count
= 0;
3426 dpi
->callback
= callback
;
3427 dpi
->opaque
= opaque
;
3429 dpi
->demangle_failure
= 0;
3432 /* Indicate that an error occurred during printing, and test for error. */
3435 d_print_error (struct d_print_info
*dpi
)
3437 dpi
->demangle_failure
= 1;
3441 d_print_saw_error (struct d_print_info
*dpi
)
3443 return dpi
->demangle_failure
!= 0;
3446 /* Flush buffered characters to the callback. */
3449 d_print_flush (struct d_print_info
*dpi
)
3451 dpi
->buf
[dpi
->len
] = '\0';
3452 dpi
->callback (dpi
->buf
, dpi
->len
, dpi
->opaque
);
3457 /* Append characters and buffers for printing. */
3460 d_append_char (struct d_print_info
*dpi
, char c
)
3462 if (dpi
->len
== sizeof (dpi
->buf
) - 1)
3463 d_print_flush (dpi
);
3465 dpi
->buf
[dpi
->len
++] = c
;
3470 d_append_buffer (struct d_print_info
*dpi
, const char *s
, size_t l
)
3474 for (i
= 0; i
< l
; i
++)
3475 d_append_char (dpi
, s
[i
]);
3479 d_append_string (struct d_print_info
*dpi
, const char *s
)
3481 d_append_buffer (dpi
, s
, strlen (s
));
3485 d_append_num (struct d_print_info
*dpi
, long l
)
3488 sprintf (buf
,"%ld", l
);
3489 d_append_string (dpi
, buf
);
3493 d_last_char (struct d_print_info
*dpi
)
3495 return dpi
->last_char
;
3498 /* Turn components into a human readable string. OPTIONS is the
3499 options bits passed to the demangler. DC is the tree to print.
3500 CALLBACK is a function to call to flush demangled string segments
3501 as they fill the intermediate buffer, and OPAQUE is a generalized
3502 callback argument. On success, this returns 1. On failure,
3503 it returns 0, indicating a bad parse. It does not use heap
3504 memory to build an output string, so cannot encounter memory
3505 allocation failure. */
3507 CP_STATIC_IF_GLIBCPP_V3
3509 cplus_demangle_print_callback (int options
,
3510 const struct demangle_component
*dc
,
3511 demangle_callbackref callback
, void *opaque
)
3513 struct d_print_info dpi
;
3515 d_print_init (&dpi
, callback
, opaque
);
3517 d_print_comp (&dpi
, options
, dc
);
3519 d_print_flush (&dpi
);
3521 return ! d_print_saw_error (&dpi
);
3524 /* Turn components into a human readable string. OPTIONS is the
3525 options bits passed to the demangler. DC is the tree to print.
3526 ESTIMATE is a guess at the length of the result. This returns a
3527 string allocated by malloc, or NULL on error. On success, this
3528 sets *PALC to the size of the allocated buffer. On failure, this
3529 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
3532 CP_STATIC_IF_GLIBCPP_V3
3534 cplus_demangle_print (int options
, const struct demangle_component
*dc
,
3535 int estimate
, size_t *palc
)
3537 struct d_growable_string dgs
;
3539 d_growable_string_init (&dgs
, estimate
);
3541 if (! cplus_demangle_print_callback (options
, dc
,
3542 d_growable_string_callback_adapter
,
3550 *palc
= dgs
.allocation_failure
? 1 : dgs
.alc
;
3554 /* Returns the I'th element of the template arglist ARGS, or NULL on
3557 static struct demangle_component
*
3558 d_index_template_argument (struct demangle_component
*args
, int i
)
3560 struct demangle_component
*a
;
3566 if (a
->type
!= DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
3572 if (i
!= 0 || a
== NULL
)
3578 /* Returns the template argument from the current context indicated by DC,
3579 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
3581 static struct demangle_component
*
3582 d_lookup_template_argument (struct d_print_info
*dpi
,
3583 const struct demangle_component
*dc
)
3585 if (dpi
->templates
== NULL
)
3587 d_print_error (dpi
);
3591 return d_index_template_argument
3592 (d_right (dpi
->templates
->template_decl
),
3593 dc
->u
.s_number
.number
);
3596 /* Returns a template argument pack used in DC (any will do), or NULL. */
3598 static struct demangle_component
*
3599 d_find_pack (struct d_print_info
*dpi
,
3600 const struct demangle_component
*dc
)
3602 struct demangle_component
*a
;
3608 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
3609 a
= d_lookup_template_argument (dpi
, dc
);
3610 if (a
&& a
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
3614 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
3617 case DEMANGLE_COMPONENT_LAMBDA
:
3618 case DEMANGLE_COMPONENT_NAME
:
3619 case DEMANGLE_COMPONENT_OPERATOR
:
3620 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
3621 case DEMANGLE_COMPONENT_SUB_STD
:
3622 case DEMANGLE_COMPONENT_CHARACTER
:
3623 case DEMANGLE_COMPONENT_FUNCTION_PARAM
:
3626 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
3627 return d_find_pack (dpi
, dc
->u
.s_extended_operator
.name
);
3628 case DEMANGLE_COMPONENT_CTOR
:
3629 return d_find_pack (dpi
, dc
->u
.s_ctor
.name
);
3630 case DEMANGLE_COMPONENT_DTOR
:
3631 return d_find_pack (dpi
, dc
->u
.s_dtor
.name
);
3634 a
= d_find_pack (dpi
, d_left (dc
));
3637 return d_find_pack (dpi
, d_right (dc
));
3641 /* Returns the length of the template argument pack DC. */
3644 d_pack_length (const struct demangle_component
*dc
)
3647 while (dc
&& dc
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
3648 && d_left (dc
) != NULL
)
3656 /* DC is a component of a mangled expression. Print it, wrapped in parens
3660 d_print_subexpr (struct d_print_info
*dpi
, int options
,
3661 const struct demangle_component
*dc
)
3664 if (dc
->type
== DEMANGLE_COMPONENT_NAME
3665 || dc
->type
== DEMANGLE_COMPONENT_FUNCTION_PARAM
)
3668 d_append_char (dpi
, '(');
3669 d_print_comp (dpi
, options
, dc
);
3671 d_append_char (dpi
, ')');
3674 /* Subroutine to handle components. */
3677 d_print_comp (struct d_print_info
*dpi
, int options
,
3678 const struct demangle_component
*dc
)
3680 /* Magic variable to let reference smashing skip over the next modifier
3681 without needing to modify *dc. */
3682 const struct demangle_component
*mod_inner
= NULL
;
3686 d_print_error (dpi
);
3689 if (d_print_saw_error (dpi
))
3694 case DEMANGLE_COMPONENT_NAME
:
3695 if ((options
& DMGL_JAVA
) == 0)
3696 d_append_buffer (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
3698 d_print_java_identifier (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
3701 case DEMANGLE_COMPONENT_QUAL_NAME
:
3702 case DEMANGLE_COMPONENT_LOCAL_NAME
:
3703 d_print_comp (dpi
, options
, d_left (dc
));
3704 if ((options
& DMGL_JAVA
) == 0)
3705 d_append_string (dpi
, "::");
3707 d_append_char (dpi
, '.');
3708 d_print_comp (dpi
, options
, d_right (dc
));
3711 case DEMANGLE_COMPONENT_TYPED_NAME
:
3713 struct d_print_mod
*hold_modifiers
;
3714 struct demangle_component
*typed_name
;
3715 struct d_print_mod adpm
[4];
3717 struct d_print_template dpt
;
3719 /* Pass the name down to the type so that it can be printed in
3720 the right place for the type. We also have to pass down
3721 any CV-qualifiers, which apply to the this parameter. */
3722 hold_modifiers
= dpi
->modifiers
;
3725 typed_name
= d_left (dc
);
3726 while (typed_name
!= NULL
)
3728 if (i
>= sizeof adpm
/ sizeof adpm
[0])
3730 d_print_error (dpi
);
3734 adpm
[i
].next
= dpi
->modifiers
;
3735 dpi
->modifiers
= &adpm
[i
];
3736 adpm
[i
].mod
= typed_name
;
3737 adpm
[i
].printed
= 0;
3738 adpm
[i
].templates
= dpi
->templates
;
3741 if (typed_name
->type
!= DEMANGLE_COMPONENT_RESTRICT_THIS
3742 && typed_name
->type
!= DEMANGLE_COMPONENT_VOLATILE_THIS
3743 && typed_name
->type
!= DEMANGLE_COMPONENT_CONST_THIS
)
3746 typed_name
= d_left (typed_name
);
3749 if (typed_name
== NULL
)
3751 d_print_error (dpi
);
3755 /* If typed_name is a template, then it applies to the
3756 function type as well. */
3757 if (typed_name
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
3759 dpt
.next
= dpi
->templates
;
3760 dpi
->templates
= &dpt
;
3761 dpt
.template_decl
= typed_name
;
3764 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
3765 there may be CV-qualifiers on its right argument which
3766 really apply here; this happens when parsing a class which
3767 is local to a function. */
3768 if (typed_name
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
3770 struct demangle_component
*local_name
;
3772 local_name
= d_right (typed_name
);
3773 if (local_name
->type
== DEMANGLE_COMPONENT_DEFAULT_ARG
)
3774 local_name
= local_name
->u
.s_unary_num
.sub
;
3775 while (local_name
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
3776 || local_name
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
3777 || local_name
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
3779 if (i
>= sizeof adpm
/ sizeof adpm
[0])
3781 d_print_error (dpi
);
3785 adpm
[i
] = adpm
[i
- 1];
3786 adpm
[i
].next
= &adpm
[i
- 1];
3787 dpi
->modifiers
= &adpm
[i
];
3789 adpm
[i
- 1].mod
= local_name
;
3790 adpm
[i
- 1].printed
= 0;
3791 adpm
[i
- 1].templates
= dpi
->templates
;
3794 local_name
= d_left (local_name
);
3798 d_print_comp (dpi
, options
, d_right (dc
));
3800 if (typed_name
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
3801 dpi
->templates
= dpt
.next
;
3803 /* If the modifiers didn't get printed by the type, print them
3808 if (! adpm
[i
].printed
)
3810 d_append_char (dpi
, ' ');
3811 d_print_mod (dpi
, options
, adpm
[i
].mod
);
3815 dpi
->modifiers
= hold_modifiers
;
3820 case DEMANGLE_COMPONENT_TEMPLATE
:
3822 struct d_print_mod
*hold_dpm
;
3823 struct demangle_component
*dcl
;
3825 /* Don't push modifiers into a template definition. Doing so
3826 could give the wrong definition for a template argument.
3827 Instead, treat the template essentially as a name. */
3829 hold_dpm
= dpi
->modifiers
;
3830 dpi
->modifiers
= NULL
;
3834 if ((options
& DMGL_JAVA
) != 0
3835 && dcl
->type
== DEMANGLE_COMPONENT_NAME
3836 && dcl
->u
.s_name
.len
== 6
3837 && strncmp (dcl
->u
.s_name
.s
, "JArray", 6) == 0)
3839 /* Special-case Java arrays, so that JArray<TYPE> appears
3840 instead as TYPE[]. */
3842 d_print_comp (dpi
, options
, d_right (dc
));
3843 d_append_string (dpi
, "[]");
3847 d_print_comp (dpi
, options
, dcl
);
3848 if (d_last_char (dpi
) == '<')
3849 d_append_char (dpi
, ' ');
3850 d_append_char (dpi
, '<');
3851 d_print_comp (dpi
, options
, d_right (dc
));
3852 /* Avoid generating two consecutive '>' characters, to avoid
3853 the C++ syntactic ambiguity. */
3854 if (d_last_char (dpi
) == '>')
3855 d_append_char (dpi
, ' ');
3856 d_append_char (dpi
, '>');
3859 dpi
->modifiers
= hold_dpm
;
3864 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
3866 struct d_print_template
*hold_dpt
;
3867 struct demangle_component
*a
= d_lookup_template_argument (dpi
, dc
);
3869 if (a
&& a
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
3870 a
= d_index_template_argument (a
, dpi
->pack_index
);
3874 d_print_error (dpi
);
3878 /* While processing this parameter, we need to pop the list of
3879 templates. This is because the template parameter may
3880 itself be a reference to a parameter of an outer
3883 hold_dpt
= dpi
->templates
;
3884 dpi
->templates
= hold_dpt
->next
;
3886 d_print_comp (dpi
, options
, a
);
3888 dpi
->templates
= hold_dpt
;
3893 case DEMANGLE_COMPONENT_CTOR
:
3894 d_print_comp (dpi
, options
, dc
->u
.s_ctor
.name
);
3897 case DEMANGLE_COMPONENT_DTOR
:
3898 d_append_char (dpi
, '~');
3899 d_print_comp (dpi
, options
, dc
->u
.s_dtor
.name
);
3902 case DEMANGLE_COMPONENT_VTABLE
:
3903 d_append_string (dpi
, "vtable for ");
3904 d_print_comp (dpi
, options
, d_left (dc
));
3907 case DEMANGLE_COMPONENT_VTT
:
3908 d_append_string (dpi
, "VTT for ");
3909 d_print_comp (dpi
, options
, d_left (dc
));
3912 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
3913 d_append_string (dpi
, "construction vtable for ");
3914 d_print_comp (dpi
, options
, d_left (dc
));
3915 d_append_string (dpi
, "-in-");
3916 d_print_comp (dpi
, options
, d_right (dc
));
3919 case DEMANGLE_COMPONENT_TYPEINFO
:
3920 d_append_string (dpi
, "typeinfo for ");
3921 d_print_comp (dpi
, options
, d_left (dc
));
3924 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
3925 d_append_string (dpi
, "typeinfo name for ");
3926 d_print_comp (dpi
, options
, d_left (dc
));
3929 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
3930 d_append_string (dpi
, "typeinfo fn for ");
3931 d_print_comp (dpi
, options
, d_left (dc
));
3934 case DEMANGLE_COMPONENT_THUNK
:
3935 d_append_string (dpi
, "non-virtual thunk to ");
3936 d_print_comp (dpi
, options
, d_left (dc
));
3939 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
3940 d_append_string (dpi
, "virtual thunk to ");
3941 d_print_comp (dpi
, options
, d_left (dc
));
3944 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
3945 d_append_string (dpi
, "covariant return thunk to ");
3946 d_print_comp (dpi
, options
, d_left (dc
));
3949 case DEMANGLE_COMPONENT_JAVA_CLASS
:
3950 d_append_string (dpi
, "java Class for ");
3951 d_print_comp (dpi
, options
, d_left (dc
));
3954 case DEMANGLE_COMPONENT_GUARD
:
3955 d_append_string (dpi
, "guard variable for ");
3956 d_print_comp (dpi
, options
, d_left (dc
));
3959 case DEMANGLE_COMPONENT_REFTEMP
:
3960 d_append_string (dpi
, "reference temporary #");
3961 d_print_comp (dpi
, options
, d_right (dc
));
3962 d_append_string (dpi
, " for ");
3963 d_print_comp (dpi
, options
, d_left (dc
));
3966 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
3967 d_append_string (dpi
, "hidden alias for ");
3968 d_print_comp (dpi
, options
, d_left (dc
));
3971 case DEMANGLE_COMPONENT_TRANSACTION_CLONE
:
3972 d_append_string (dpi
, "transaction clone for ");
3973 d_print_comp (dpi
, options
, d_left (dc
));
3976 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
:
3977 d_append_string (dpi
, "non-transaction clone for ");
3978 d_print_comp (dpi
, options
, d_left (dc
));
3981 case DEMANGLE_COMPONENT_SUB_STD
:
3982 d_append_buffer (dpi
, dc
->u
.s_string
.string
, dc
->u
.s_string
.len
);
3985 case DEMANGLE_COMPONENT_RESTRICT
:
3986 case DEMANGLE_COMPONENT_VOLATILE
:
3987 case DEMANGLE_COMPONENT_CONST
:
3989 struct d_print_mod
*pdpm
;
3991 /* When printing arrays, it's possible to have cases where the
3992 same CV-qualifier gets pushed on the stack multiple times.
3993 We only need to print it once. */
3995 for (pdpm
= dpi
->modifiers
; pdpm
!= NULL
; pdpm
= pdpm
->next
)
3997 if (! pdpm
->printed
)
3999 if (pdpm
->mod
->type
!= DEMANGLE_COMPONENT_RESTRICT
4000 && pdpm
->mod
->type
!= DEMANGLE_COMPONENT_VOLATILE
4001 && pdpm
->mod
->type
!= DEMANGLE_COMPONENT_CONST
)
4003 if (pdpm
->mod
->type
== dc
->type
)
4005 d_print_comp (dpi
, options
, d_left (dc
));
4013 case DEMANGLE_COMPONENT_REFERENCE
:
4014 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
4016 /* Handle reference smashing: & + && = &. */
4017 const struct demangle_component
*sub
= d_left (dc
);
4018 if (sub
->type
== DEMANGLE_COMPONENT_TEMPLATE_PARAM
)
4020 struct demangle_component
*a
= d_lookup_template_argument (dpi
, sub
);
4021 if (a
&& a
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
4022 a
= d_index_template_argument (a
, dpi
->pack_index
);
4026 d_print_error (dpi
);
4033 if (sub
->type
== DEMANGLE_COMPONENT_REFERENCE
4034 || sub
->type
== dc
->type
)
4036 else if (sub
->type
== DEMANGLE_COMPONENT_RVALUE_REFERENCE
)
4037 mod_inner
= d_left (sub
);
4041 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
4042 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
4043 case DEMANGLE_COMPONENT_CONST_THIS
:
4044 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
4045 case DEMANGLE_COMPONENT_POINTER
:
4046 case DEMANGLE_COMPONENT_COMPLEX
:
4047 case DEMANGLE_COMPONENT_IMAGINARY
:
4050 /* We keep a list of modifiers on the stack. */
4051 struct d_print_mod dpm
;
4053 dpm
.next
= dpi
->modifiers
;
4054 dpi
->modifiers
= &dpm
;
4057 dpm
.templates
= dpi
->templates
;
4060 mod_inner
= d_left (dc
);
4062 d_print_comp (dpi
, options
, mod_inner
);
4064 /* If the modifier didn't get printed by the type, print it
4067 d_print_mod (dpi
, options
, dc
);
4069 dpi
->modifiers
= dpm
.next
;
4074 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
4075 if ((options
& DMGL_JAVA
) == 0)
4076 d_append_buffer (dpi
, dc
->u
.s_builtin
.type
->name
,
4077 dc
->u
.s_builtin
.type
->len
);
4079 d_append_buffer (dpi
, dc
->u
.s_builtin
.type
->java_name
,
4080 dc
->u
.s_builtin
.type
->java_len
);
4083 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
4084 d_print_comp (dpi
, options
, d_left (dc
));
4087 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
4089 if ((options
& DMGL_RET_POSTFIX
) != 0)
4090 d_print_function_type (dpi
,
4091 options
& ~(DMGL_RET_POSTFIX
| DMGL_RET_DROP
),
4092 dc
, dpi
->modifiers
);
4094 /* Print return type if present */
4095 if (d_left (dc
) != NULL
&& (options
& DMGL_RET_POSTFIX
) != 0)
4096 d_print_comp (dpi
, options
& ~(DMGL_RET_POSTFIX
| DMGL_RET_DROP
),
4098 else if (d_left (dc
) != NULL
&& (options
& DMGL_RET_DROP
) == 0)
4100 struct d_print_mod dpm
;
4102 /* We must pass this type down as a modifier in order to
4103 print it in the right location. */
4104 dpm
.next
= dpi
->modifiers
;
4105 dpi
->modifiers
= &dpm
;
4108 dpm
.templates
= dpi
->templates
;
4110 d_print_comp (dpi
, options
& ~(DMGL_RET_POSTFIX
| DMGL_RET_DROP
),
4113 dpi
->modifiers
= dpm
.next
;
4118 /* In standard prefix notation, there is a space between the
4119 return type and the function signature. */
4120 if ((options
& DMGL_RET_POSTFIX
) == 0)
4121 d_append_char (dpi
, ' ');
4124 if ((options
& DMGL_RET_POSTFIX
) == 0)
4125 d_print_function_type (dpi
,
4126 options
& ~(DMGL_RET_POSTFIX
| DMGL_RET_DROP
),
4127 dc
, dpi
->modifiers
);
4132 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
4134 struct d_print_mod
*hold_modifiers
;
4135 struct d_print_mod adpm
[4];
4137 struct d_print_mod
*pdpm
;
4139 /* We must pass this type down as a modifier in order to print
4140 multi-dimensional arrays correctly. If the array itself is
4141 CV-qualified, we act as though the element type were
4142 CV-qualified. We do this by copying the modifiers down
4143 rather than fiddling pointers, so that we don't wind up
4144 with a d_print_mod higher on the stack pointing into our
4145 stack frame after we return. */
4147 hold_modifiers
= dpi
->modifiers
;
4149 adpm
[0].next
= hold_modifiers
;
4150 dpi
->modifiers
= &adpm
[0];
4152 adpm
[0].printed
= 0;
4153 adpm
[0].templates
= dpi
->templates
;
4156 pdpm
= hold_modifiers
;
4158 && (pdpm
->mod
->type
== DEMANGLE_COMPONENT_RESTRICT
4159 || pdpm
->mod
->type
== DEMANGLE_COMPONENT_VOLATILE
4160 || pdpm
->mod
->type
== DEMANGLE_COMPONENT_CONST
))
4162 if (! pdpm
->printed
)
4164 if (i
>= sizeof adpm
/ sizeof adpm
[0])
4166 d_print_error (dpi
);
4171 adpm
[i
].next
= dpi
->modifiers
;
4172 dpi
->modifiers
= &adpm
[i
];
4180 d_print_comp (dpi
, options
, d_right (dc
));
4182 dpi
->modifiers
= hold_modifiers
;
4184 if (adpm
[0].printed
)
4190 d_print_mod (dpi
, options
, adpm
[i
].mod
);
4193 d_print_array_type (dpi
, options
, dc
, dpi
->modifiers
);
4198 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
4199 case DEMANGLE_COMPONENT_VECTOR_TYPE
:
4201 struct d_print_mod dpm
;
4203 dpm
.next
= dpi
->modifiers
;
4204 dpi
->modifiers
= &dpm
;
4207 dpm
.templates
= dpi
->templates
;
4209 d_print_comp (dpi
, options
, d_right (dc
));
4211 /* If the modifier didn't get printed by the type, print it
4214 d_print_mod (dpi
, options
, dc
);
4216 dpi
->modifiers
= dpm
.next
;
4221 case DEMANGLE_COMPONENT_FIXED_TYPE
:
4222 if (dc
->u
.s_fixed
.sat
)
4223 d_append_string (dpi
, "_Sat ");
4224 /* Don't print "int _Accum". */
4225 if (dc
->u
.s_fixed
.length
->u
.s_builtin
.type
4226 != &cplus_demangle_builtin_types
['i'-'a'])
4228 d_print_comp (dpi
, options
, dc
->u
.s_fixed
.length
);
4229 d_append_char (dpi
, ' ');
4231 if (dc
->u
.s_fixed
.accum
)
4232 d_append_string (dpi
, "_Accum");
4234 d_append_string (dpi
, "_Fract");
4237 case DEMANGLE_COMPONENT_ARGLIST
:
4238 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
4239 if (d_left (dc
) != NULL
)
4240 d_print_comp (dpi
, options
, d_left (dc
));
4241 if (d_right (dc
) != NULL
)
4244 unsigned long int flush_count
;
4245 /* Make sure ", " isn't flushed by d_append_string, otherwise
4246 dpi->len -= 2 wouldn't work. */
4247 if (dpi
->len
>= sizeof (dpi
->buf
) - 2)
4248 d_print_flush (dpi
);
4249 d_append_string (dpi
, ", ");
4251 flush_count
= dpi
->flush_count
;
4252 d_print_comp (dpi
, options
, d_right (dc
));
4253 /* If that didn't print anything (which can happen with empty
4254 template argument packs), remove the comma and space. */
4255 if (dpi
->flush_count
== flush_count
&& dpi
->len
== len
)
4260 case DEMANGLE_COMPONENT_OPERATOR
:
4264 d_append_string (dpi
, "operator");
4265 c
= dc
->u
.s_operator
.op
->name
[0];
4267 d_append_char (dpi
, ' ');
4268 d_append_buffer (dpi
, dc
->u
.s_operator
.op
->name
,
4269 dc
->u
.s_operator
.op
->len
);
4273 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
4274 d_append_string (dpi
, "operator ");
4275 d_print_comp (dpi
, options
, dc
->u
.s_extended_operator
.name
);
4278 case DEMANGLE_COMPONENT_CAST
:
4279 d_append_string (dpi
, "operator ");
4280 d_print_cast (dpi
, options
, dc
);
4283 case DEMANGLE_COMPONENT_UNARY
:
4284 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_OPERATOR
4285 && d_left (dc
)->u
.s_operator
.op
->len
== 1
4286 && d_left (dc
)->u
.s_operator
.op
->name
[0] == '&'
4287 && d_right (dc
)->type
== DEMANGLE_COMPONENT_TYPED_NAME
4288 && d_left (d_right (dc
))->type
== DEMANGLE_COMPONENT_QUAL_NAME
4289 && d_right (d_right (dc
))->type
== DEMANGLE_COMPONENT_FUNCTION_TYPE
)
4291 /* Address of a function (therefore in an expression context) must
4292 have its argument list suppressed.
4294 unary operator ... dc
4295 operator & ... d_left (dc)
4296 typed name ... d_right (dc)
4297 qualified name ... d_left (d_right (dc))
4299 function type ... d_right (d_right (dc))
4303 d_print_expr_op (dpi
, options
, d_left (dc
));
4304 d_print_comp (dpi
, options
, d_left (d_right (dc
)));
4307 else if (d_left (dc
)->type
== DEMANGLE_COMPONENT_OPERATOR
4308 && d_left (dc
)->u
.s_operator
.op
->len
== 1
4309 && d_left (dc
)->u
.s_operator
.op
->name
[0] == '&'
4310 && d_right (dc
)->type
== DEMANGLE_COMPONENT_QUAL_NAME
)
4312 /* Keep also already processed variant without the argument list.
4314 unary operator ... dc
4315 operator & ... d_left (dc)
4316 qualified name ... d_right (dc)
4319 d_print_expr_op (dpi
, options
, d_left (dc
));
4320 d_print_comp (dpi
, options
, d_right (dc
));
4323 else if (d_left (dc
)->type
!= DEMANGLE_COMPONENT_CAST
)
4324 d_print_expr_op (dpi
, options
, d_left (dc
));
4327 d_append_char (dpi
, '(');
4328 d_print_cast (dpi
, options
, d_left (dc
));
4329 d_append_char (dpi
, ')');
4331 d_print_subexpr (dpi
, options
, d_right (dc
));
4334 case DEMANGLE_COMPONENT_BINARY
:
4335 if (d_right (dc
)->type
!= DEMANGLE_COMPONENT_BINARY_ARGS
)
4337 d_print_error (dpi
);
4341 /* We wrap an expression which uses the greater-than operator in
4342 an extra layer of parens so that it does not get confused
4343 with the '>' which ends the template parameters. */
4344 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_OPERATOR
4345 && d_left (dc
)->u
.s_operator
.op
->len
== 1
4346 && d_left (dc
)->u
.s_operator
.op
->name
[0] == '>')
4347 d_append_char (dpi
, '(');
4349 if (strcmp (d_left (dc
)->u
.s_operator
.op
->code
, "cl") == 0
4350 && d_left (d_right (dc
))->type
== DEMANGLE_COMPONENT_TYPED_NAME
)
4352 /* Function call used in an expression should not have printed types
4353 of the function arguments. Values of the function arguments still
4354 get printed below. */
4356 const struct demangle_component
*func
= d_left (d_right (dc
));
4358 if (d_right (func
)->type
!= DEMANGLE_COMPONENT_FUNCTION_TYPE
)
4359 d_print_error (dpi
);
4360 d_print_subexpr (dpi
, options
, d_left (func
));
4363 d_print_subexpr (dpi
, options
, d_left (d_right (dc
)));
4364 if (strcmp (d_left (dc
)->u
.s_operator
.op
->code
, "ix") == 0)
4366 d_append_char (dpi
, '[');
4367 d_print_comp (dpi
, options
, d_right (d_right (dc
)));
4368 d_append_char (dpi
, ']');
4372 if (strcmp (d_left (dc
)->u
.s_operator
.op
->code
, "cl") != 0)
4373 d_print_expr_op (dpi
, options
, d_left (dc
));
4374 d_print_subexpr (dpi
, options
, d_right (d_right (dc
)));
4377 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_OPERATOR
4378 && d_left (dc
)->u
.s_operator
.op
->len
== 1
4379 && d_left (dc
)->u
.s_operator
.op
->name
[0] == '>')
4380 d_append_char (dpi
, ')');
4384 case DEMANGLE_COMPONENT_BINARY_ARGS
:
4385 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
4386 d_print_error (dpi
);
4389 case DEMANGLE_COMPONENT_TRINARY
:
4390 if (d_right (dc
)->type
!= DEMANGLE_COMPONENT_TRINARY_ARG1
4391 || d_right (d_right (dc
))->type
!= DEMANGLE_COMPONENT_TRINARY_ARG2
)
4393 d_print_error (dpi
);
4396 d_print_subexpr (dpi
, options
, d_left (d_right (dc
)));
4397 d_print_expr_op (dpi
, options
, d_left (dc
));
4398 d_print_subexpr (dpi
, options
, d_left (d_right (d_right (dc
))));
4399 d_append_string (dpi
, " : ");
4400 d_print_subexpr (dpi
, options
, d_right (d_right (d_right (dc
))));
4403 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
4404 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
4405 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
4406 d_print_error (dpi
);
4409 case DEMANGLE_COMPONENT_LITERAL
:
4410 case DEMANGLE_COMPONENT_LITERAL_NEG
:
4412 enum d_builtin_type_print tp
;
4414 /* For some builtin types, produce simpler output. */
4415 tp
= D_PRINT_DEFAULT
;
4416 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
)
4418 tp
= d_left (dc
)->u
.s_builtin
.type
->print
;
4422 case D_PRINT_UNSIGNED
:
4424 case D_PRINT_UNSIGNED_LONG
:
4425 case D_PRINT_LONG_LONG
:
4426 case D_PRINT_UNSIGNED_LONG_LONG
:
4427 if (d_right (dc
)->type
== DEMANGLE_COMPONENT_NAME
)
4429 if (dc
->type
== DEMANGLE_COMPONENT_LITERAL_NEG
)
4430 d_append_char (dpi
, '-');
4431 d_print_comp (dpi
, options
, d_right (dc
));
4436 case D_PRINT_UNSIGNED
:
4437 d_append_char (dpi
, 'u');
4440 d_append_char (dpi
, 'l');
4442 case D_PRINT_UNSIGNED_LONG
:
4443 d_append_string (dpi
, "ul");
4445 case D_PRINT_LONG_LONG
:
4446 d_append_string (dpi
, "ll");
4448 case D_PRINT_UNSIGNED_LONG_LONG
:
4449 d_append_string (dpi
, "ull");
4457 if (d_right (dc
)->type
== DEMANGLE_COMPONENT_NAME
4458 && d_right (dc
)->u
.s_name
.len
== 1
4459 && dc
->type
== DEMANGLE_COMPONENT_LITERAL
)
4461 switch (d_right (dc
)->u
.s_name
.s
[0])
4464 d_append_string (dpi
, "false");
4467 d_append_string (dpi
, "true");
4480 d_append_char (dpi
, '(');
4481 d_print_comp (dpi
, options
, d_left (dc
));
4482 d_append_char (dpi
, ')');
4483 if (dc
->type
== DEMANGLE_COMPONENT_LITERAL_NEG
)
4484 d_append_char (dpi
, '-');
4485 if (tp
== D_PRINT_FLOAT
)
4486 d_append_char (dpi
, '[');
4487 d_print_comp (dpi
, options
, d_right (dc
));
4488 if (tp
== D_PRINT_FLOAT
)
4489 d_append_char (dpi
, ']');
4493 case DEMANGLE_COMPONENT_NUMBER
:
4494 d_append_num (dpi
, dc
->u
.s_number
.number
);
4497 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
4498 d_append_string (dpi
, "java resource ");
4499 d_print_comp (dpi
, options
, d_left (dc
));
4502 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
4503 d_print_comp (dpi
, options
, d_left (dc
));
4504 d_print_comp (dpi
, options
, d_right (dc
));
4507 case DEMANGLE_COMPONENT_CHARACTER
:
4508 d_append_char (dpi
, dc
->u
.s_character
.character
);
4511 case DEMANGLE_COMPONENT_DECLTYPE
:
4512 d_append_string (dpi
, "decltype (");
4513 d_print_comp (dpi
, options
, d_left (dc
));
4514 d_append_char (dpi
, ')');
4517 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
4521 struct demangle_component
*a
= d_find_pack (dpi
, d_left (dc
));
4524 /* d_find_pack won't find anything if the only packs involved
4525 in this expansion are function parameter packs; in that
4526 case, just print the pattern and "...". */
4527 d_print_subexpr (dpi
, options
, d_left (dc
));
4528 d_append_string (dpi
, "...");
4532 len
= d_pack_length (a
);
4534 for (i
= 0; i
< len
; ++i
)
4536 dpi
->pack_index
= i
;
4537 d_print_comp (dpi
, options
, dc
);
4539 d_append_string (dpi
, ", ");
4544 case DEMANGLE_COMPONENT_FUNCTION_PARAM
:
4546 long num
= dc
->u
.s_number
.number
;
4548 d_append_string (dpi
, "this");
4551 d_append_string (dpi
, "{parm#");
4552 d_append_num (dpi
, num
);
4553 d_append_char (dpi
, '}');
4558 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
:
4559 d_append_string (dpi
, "global constructors keyed to ");
4560 d_print_comp (dpi
, options
, dc
->u
.s_binary
.left
);
4563 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS
:
4564 d_append_string (dpi
, "global destructors keyed to ");
4565 d_print_comp (dpi
, options
, dc
->u
.s_binary
.left
);
4568 case DEMANGLE_COMPONENT_LAMBDA
:
4569 d_append_string (dpi
, "{lambda(");
4570 d_print_comp (dpi
, options
, dc
->u
.s_unary_num
.sub
);
4571 d_append_string (dpi
, ")#");
4572 d_append_num (dpi
, dc
->u
.s_unary_num
.num
+ 1);
4573 d_append_char (dpi
, '}');
4576 case DEMANGLE_COMPONENT_UNNAMED_TYPE
:
4577 d_append_string (dpi
, "{unnamed type#");
4578 d_append_num (dpi
, dc
->u
.s_number
.number
+ 1);
4579 d_append_char (dpi
, '}');
4582 case DEMANGLE_COMPONENT_CLONE
:
4583 d_print_comp (dpi
, options
, d_left (dc
));
4584 d_append_string (dpi
, " [clone ");
4585 d_print_comp (dpi
, options
, d_right (dc
));
4586 d_append_char (dpi
, ']');
4590 d_print_error (dpi
);
4595 /* Print a Java dentifier. For Java we try to handle encoded extended
4596 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
4597 so we don't it for C++. Characters are encoded as
4601 d_print_java_identifier (struct d_print_info
*dpi
, const char *name
, int len
)
4607 for (p
= name
; p
< end
; ++p
)
4618 for (q
= p
+ 3; q
< end
; ++q
)
4624 else if (*q
>= 'A' && *q
<= 'F')
4625 dig
= *q
- 'A' + 10;
4626 else if (*q
>= 'a' && *q
<= 'f')
4627 dig
= *q
- 'a' + 10;
4633 /* If the Unicode character is larger than 256, we don't try
4634 to deal with it here. FIXME. */
4635 if (q
< end
&& *q
== '_' && c
< 256)
4637 d_append_char (dpi
, c
);
4643 d_append_char (dpi
, *p
);
4647 /* Print a list of modifiers. SUFFIX is 1 if we are printing
4648 qualifiers on this after printing a function. */
4651 d_print_mod_list (struct d_print_info
*dpi
, int options
,
4652 struct d_print_mod
*mods
, int suffix
)
4654 struct d_print_template
*hold_dpt
;
4656 if (mods
== NULL
|| d_print_saw_error (dpi
))
4661 && (mods
->mod
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
4662 || mods
->mod
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
4663 || mods
->mod
->type
== DEMANGLE_COMPONENT_CONST_THIS
)))
4665 d_print_mod_list (dpi
, options
, mods
->next
, suffix
);
4671 hold_dpt
= dpi
->templates
;
4672 dpi
->templates
= mods
->templates
;
4674 if (mods
->mod
->type
== DEMANGLE_COMPONENT_FUNCTION_TYPE
)
4676 d_print_function_type (dpi
, options
, mods
->mod
, mods
->next
);
4677 dpi
->templates
= hold_dpt
;
4680 else if (mods
->mod
->type
== DEMANGLE_COMPONENT_ARRAY_TYPE
)
4682 d_print_array_type (dpi
, options
, mods
->mod
, mods
->next
);
4683 dpi
->templates
= hold_dpt
;
4686 else if (mods
->mod
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
4688 struct d_print_mod
*hold_modifiers
;
4689 struct demangle_component
*dc
;
4691 /* When this is on the modifier stack, we have pulled any
4692 qualifiers off the right argument already. Otherwise, we
4693 print it as usual, but don't let the left argument see any
4696 hold_modifiers
= dpi
->modifiers
;
4697 dpi
->modifiers
= NULL
;
4698 d_print_comp (dpi
, options
, d_left (mods
->mod
));
4699 dpi
->modifiers
= hold_modifiers
;
4701 if ((options
& DMGL_JAVA
) == 0)
4702 d_append_string (dpi
, "::");
4704 d_append_char (dpi
, '.');
4706 dc
= d_right (mods
->mod
);
4708 if (dc
->type
== DEMANGLE_COMPONENT_DEFAULT_ARG
)
4710 d_append_string (dpi
, "{default arg#");
4711 d_append_num (dpi
, dc
->u
.s_unary_num
.num
+ 1);
4712 d_append_string (dpi
, "}::");
4713 dc
= dc
->u
.s_unary_num
.sub
;
4716 while (dc
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
4717 || dc
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
4718 || dc
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
4721 d_print_comp (dpi
, options
, dc
);
4723 dpi
->templates
= hold_dpt
;
4727 d_print_mod (dpi
, options
, mods
->mod
);
4729 dpi
->templates
= hold_dpt
;
4731 d_print_mod_list (dpi
, options
, mods
->next
, suffix
);
4734 /* Print a modifier. */
4737 d_print_mod (struct d_print_info
*dpi
, int options
,
4738 const struct demangle_component
*mod
)
4742 case DEMANGLE_COMPONENT_RESTRICT
:
4743 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
4744 d_append_string (dpi
, " restrict");
4746 case DEMANGLE_COMPONENT_VOLATILE
:
4747 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
4748 d_append_string (dpi
, " volatile");
4750 case DEMANGLE_COMPONENT_CONST
:
4751 case DEMANGLE_COMPONENT_CONST_THIS
:
4752 d_append_string (dpi
, " const");
4754 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
4755 d_append_char (dpi
, ' ');
4756 d_print_comp (dpi
, options
, d_right (mod
));
4758 case DEMANGLE_COMPONENT_POINTER
:
4759 /* There is no pointer symbol in Java. */
4760 if ((options
& DMGL_JAVA
) == 0)
4761 d_append_char (dpi
, '*');
4763 case DEMANGLE_COMPONENT_REFERENCE
:
4764 d_append_char (dpi
, '&');
4766 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
4767 d_append_string (dpi
, "&&");
4769 case DEMANGLE_COMPONENT_COMPLEX
:
4770 d_append_string (dpi
, "complex ");
4772 case DEMANGLE_COMPONENT_IMAGINARY
:
4773 d_append_string (dpi
, "imaginary ");
4775 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
4776 if (d_last_char (dpi
) != '(')
4777 d_append_char (dpi
, ' ');
4778 d_print_comp (dpi
, options
, d_left (mod
));
4779 d_append_string (dpi
, "::*");
4781 case DEMANGLE_COMPONENT_TYPED_NAME
:
4782 d_print_comp (dpi
, options
, d_left (mod
));
4784 case DEMANGLE_COMPONENT_VECTOR_TYPE
:
4785 d_append_string (dpi
, " __vector(");
4786 d_print_comp (dpi
, options
, d_left (mod
));
4787 d_append_char (dpi
, ')');
4791 /* Otherwise, we have something that won't go back on the
4792 modifier stack, so we can just print it. */
4793 d_print_comp (dpi
, options
, mod
);
4798 /* Print a function type, except for the return type. */
4801 d_print_function_type (struct d_print_info
*dpi
, int options
,
4802 const struct demangle_component
*dc
,
4803 struct d_print_mod
*mods
)
4807 struct d_print_mod
*p
;
4808 struct d_print_mod
*hold_modifiers
;
4812 for (p
= mods
; p
!= NULL
; p
= p
->next
)
4817 switch (p
->mod
->type
)
4819 case DEMANGLE_COMPONENT_POINTER
:
4820 case DEMANGLE_COMPONENT_REFERENCE
:
4821 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
4824 case DEMANGLE_COMPONENT_RESTRICT
:
4825 case DEMANGLE_COMPONENT_VOLATILE
:
4826 case DEMANGLE_COMPONENT_CONST
:
4827 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
4828 case DEMANGLE_COMPONENT_COMPLEX
:
4829 case DEMANGLE_COMPONENT_IMAGINARY
:
4830 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
4834 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
4835 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
4836 case DEMANGLE_COMPONENT_CONST_THIS
:
4849 if (d_last_char (dpi
) != '('
4850 && d_last_char (dpi
) != '*')
4853 if (need_space
&& d_last_char (dpi
) != ' ')
4854 d_append_char (dpi
, ' ');
4855 d_append_char (dpi
, '(');
4858 hold_modifiers
= dpi
->modifiers
;
4859 dpi
->modifiers
= NULL
;
4861 d_print_mod_list (dpi
, options
, mods
, 0);
4864 d_append_char (dpi
, ')');
4866 d_append_char (dpi
, '(');
4868 if (d_right (dc
) != NULL
)
4869 d_print_comp (dpi
, options
, d_right (dc
));
4871 d_append_char (dpi
, ')');
4873 d_print_mod_list (dpi
, options
, mods
, 1);
4875 dpi
->modifiers
= hold_modifiers
;
4878 /* Print an array type, except for the element type. */
4881 d_print_array_type (struct d_print_info
*dpi
, int options
,
4882 const struct demangle_component
*dc
,
4883 struct d_print_mod
*mods
)
4891 struct d_print_mod
*p
;
4894 for (p
= mods
; p
!= NULL
; p
= p
->next
)
4898 if (p
->mod
->type
== DEMANGLE_COMPONENT_ARRAY_TYPE
)
4913 d_append_string (dpi
, " (");
4915 d_print_mod_list (dpi
, options
, mods
, 0);
4918 d_append_char (dpi
, ')');
4922 d_append_char (dpi
, ' ');
4924 d_append_char (dpi
, '[');
4926 if (d_left (dc
) != NULL
)
4927 d_print_comp (dpi
, options
, d_left (dc
));
4929 d_append_char (dpi
, ']');
4932 /* Print an operator in an expression. */
4935 d_print_expr_op (struct d_print_info
*dpi
, int options
,
4936 const struct demangle_component
*dc
)
4938 if (dc
->type
== DEMANGLE_COMPONENT_OPERATOR
)
4939 d_append_buffer (dpi
, dc
->u
.s_operator
.op
->name
,
4940 dc
->u
.s_operator
.op
->len
);
4942 d_print_comp (dpi
, options
, dc
);
4948 d_print_cast (struct d_print_info
*dpi
, int options
,
4949 const struct demangle_component
*dc
)
4951 if (d_left (dc
)->type
!= DEMANGLE_COMPONENT_TEMPLATE
)
4952 d_print_comp (dpi
, options
, d_left (dc
));
4955 struct d_print_mod
*hold_dpm
;
4956 struct d_print_template dpt
;
4958 /* It appears that for a templated cast operator, we need to put
4959 the template parameters in scope for the operator name, but
4960 not for the parameters. The effect is that we need to handle
4961 the template printing here. */
4963 hold_dpm
= dpi
->modifiers
;
4964 dpi
->modifiers
= NULL
;
4966 dpt
.next
= dpi
->templates
;
4967 dpi
->templates
= &dpt
;
4968 dpt
.template_decl
= d_left (dc
);
4970 d_print_comp (dpi
, options
, d_left (d_left (dc
)));
4972 dpi
->templates
= dpt
.next
;
4974 if (d_last_char (dpi
) == '<')
4975 d_append_char (dpi
, ' ');
4976 d_append_char (dpi
, '<');
4977 d_print_comp (dpi
, options
, d_right (d_left (dc
)));
4978 /* Avoid generating two consecutive '>' characters, to avoid
4979 the C++ syntactic ambiguity. */
4980 if (d_last_char (dpi
) == '>')
4981 d_append_char (dpi
, ' ');
4982 d_append_char (dpi
, '>');
4984 dpi
->modifiers
= hold_dpm
;
4988 /* Initialize the information structure we use to pass around
4991 CP_STATIC_IF_GLIBCPP_V3
4993 cplus_demangle_init_info (const char *mangled
, int options
, size_t len
,
4997 di
->send
= mangled
+ len
;
4998 di
->options
= options
;
5002 /* We can not need more components than twice the number of chars in
5003 the mangled string. Most components correspond directly to
5004 chars, but the ARGLIST types are exceptions. */
5005 di
->num_comps
= 2 * len
;
5008 /* Similarly, we can not need more substitutions than there are
5009 chars in the mangled string. */
5014 di
->last_name
= NULL
;
5019 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
5020 mangled name, return strings in repeated callback giving the demangled
5021 name. OPTIONS is the usual libiberty demangler options. On success,
5022 this returns 1. On failure, returns 0. */
5025 d_demangle_callback (const char *mangled
, int options
,
5026 demangle_callbackref callback
, void *opaque
)
5037 struct demangle_component
*dc
;
5040 if (mangled
[0] == '_' && mangled
[1] == 'Z')
5042 else if (strncmp (mangled
, "_GLOBAL_", 8) == 0
5043 && (mangled
[8] == '.' || mangled
[8] == '_' || mangled
[8] == '$')
5044 && (mangled
[9] == 'D' || mangled
[9] == 'I')
5045 && mangled
[10] == '_')
5046 type
= mangled
[9] == 'I' ? DCT_GLOBAL_CTORS
: DCT_GLOBAL_DTORS
;
5049 if ((options
& DMGL_TYPES
) == 0)
5054 cplus_demangle_init_info (mangled
, options
, strlen (mangled
), &di
);
5057 #ifdef CP_DYNAMIC_ARRAYS
5058 __extension__
struct demangle_component comps
[di
.num_comps
];
5059 __extension__
struct demangle_component
*subs
[di
.num_subs
];
5064 di
.comps
= alloca (di
.num_comps
* sizeof (*di
.comps
));
5065 di
.subs
= alloca (di
.num_subs
* sizeof (*di
.subs
));
5071 dc
= cplus_demangle_type (&di
);
5074 dc
= cplus_demangle_mangled_name (&di
, 1);
5076 case DCT_GLOBAL_CTORS
:
5077 case DCT_GLOBAL_DTORS
:
5078 d_advance (&di
, 11);
5079 dc
= d_make_comp (&di
,
5080 (type
== DCT_GLOBAL_CTORS
5081 ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
5082 : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS
),
5083 d_make_demangle_mangled_name (&di
, d_str (&di
)),
5085 d_advance (&di
, strlen (d_str (&di
)));
5089 /* If DMGL_PARAMS is set, then if we didn't consume the entire
5090 mangled string, then we didn't successfully demangle it. If
5091 DMGL_PARAMS is not set, we didn't look at the trailing
5093 if (((options
& DMGL_PARAMS
) != 0) && d_peek_char (&di
) != '\0')
5096 #ifdef CP_DEMANGLE_DEBUG
5100 status
= (dc
!= NULL
)
5101 ? cplus_demangle_print_callback (options
, dc
, callback
, opaque
)
5108 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
5109 name, return a buffer allocated with malloc holding the demangled
5110 name. OPTIONS is the usual libiberty demangler options. On
5111 success, this sets *PALC to the allocated size of the returned
5112 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
5113 a memory allocation failure, and returns NULL. */
5116 d_demangle (const char *mangled
, int options
, size_t *palc
)
5118 struct d_growable_string dgs
;
5121 d_growable_string_init (&dgs
, 0);
5123 status
= d_demangle_callback (mangled
, options
,
5124 d_growable_string_callback_adapter
, &dgs
);
5132 *palc
= dgs
.allocation_failure
? 1 : dgs
.alc
;
5136 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
5138 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
5140 /* ia64 ABI-mandated entry point in the C++ runtime library for
5141 performing demangling. MANGLED_NAME is a NUL-terminated character
5142 string containing the name to be demangled.
5144 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
5145 *LENGTH bytes, into which the demangled name is stored. If
5146 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
5147 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
5148 is placed in a region of memory allocated with malloc.
5150 If LENGTH is non-NULL, the length of the buffer containing the
5151 demangled name, is placed in *LENGTH.
5153 The return value is a pointer to the start of the NUL-terminated
5154 demangled name, or NULL if the demangling fails. The caller is
5155 responsible for deallocating this memory using free.
5157 *STATUS is set to one of the following values:
5158 0: The demangling operation succeeded.
5159 -1: A memory allocation failure occurred.
5160 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
5161 -3: One of the arguments is invalid.
5163 The demangling is performed using the C++ ABI mangling rules, with
5167 __cxa_demangle (const char *mangled_name
, char *output_buffer
,
5168 size_t *length
, int *status
)
5173 if (mangled_name
== NULL
)
5180 if (output_buffer
!= NULL
&& length
== NULL
)
5187 demangled
= d_demangle (mangled_name
, DMGL_PARAMS
| DMGL_TYPES
, &alc
);
5189 if (demangled
== NULL
)
5201 if (output_buffer
== NULL
)
5208 if (strlen (demangled
) < *length
)
5210 strcpy (output_buffer
, demangled
);
5212 demangled
= output_buffer
;
5216 free (output_buffer
);
5227 extern int __gcclibcxx_demangle_callback (const char *,
5229 (const char *, size_t, void *),
5232 /* Alternative, allocationless entry point in the C++ runtime library
5233 for performing demangling. MANGLED_NAME is a NUL-terminated character
5234 string containing the name to be demangled.
5236 CALLBACK is a callback function, called with demangled string
5237 segments as demangling progresses; it is called at least once,
5238 but may be called more than once. OPAQUE is a generalized pointer
5239 used as a callback argument.
5241 The return code is one of the following values, equivalent to
5242 the STATUS values of __cxa_demangle() (excluding -1, since this
5243 function performs no memory allocations):
5244 0: The demangling operation succeeded.
5245 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
5246 -3: One of the arguments is invalid.
5248 The demangling is performed using the C++ ABI mangling rules, with
5252 __gcclibcxx_demangle_callback (const char *mangled_name
,
5253 void (*callback
) (const char *, size_t, void *),
5258 if (mangled_name
== NULL
|| callback
== NULL
)
5261 status
= d_demangle_callback (mangled_name
, DMGL_PARAMS
| DMGL_TYPES
,
5269 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
5271 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
5272 mangled name, return a buffer allocated with malloc holding the
5273 demangled name. Otherwise, return NULL. */
5276 cplus_demangle_v3 (const char *mangled
, int options
)
5280 return d_demangle (mangled
, options
, &alc
);
5284 cplus_demangle_v3_callback (const char *mangled
, int options
,
5285 demangle_callbackref callback
, void *opaque
)
5287 return d_demangle_callback (mangled
, options
, callback
, opaque
);
5290 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
5291 conventions, but the output formatting is a little different.
5292 This instructs the C++ demangler not to emit pointer characters ("*"), to
5293 use Java's namespace separator symbol ("." instead of "::"), and to output
5294 JArray<TYPE> as TYPE[]. */
5297 java_demangle_v3 (const char *mangled
)
5301 return d_demangle (mangled
, DMGL_JAVA
| DMGL_PARAMS
| DMGL_RET_POSTFIX
, &alc
);
5305 java_demangle_v3_callback (const char *mangled
,
5306 demangle_callbackref callback
, void *opaque
)
5308 return d_demangle_callback (mangled
,
5309 DMGL_JAVA
| DMGL_PARAMS
| DMGL_RET_POSTFIX
,
5313 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
5315 #ifndef IN_GLIBCPP_V3
5317 /* Demangle a string in order to find out whether it is a constructor
5318 or destructor. Return non-zero on success. Set *CTOR_KIND and
5319 *DTOR_KIND appropriately. */
5322 is_ctor_or_dtor (const char *mangled
,
5323 enum gnu_v3_ctor_kinds
*ctor_kind
,
5324 enum gnu_v3_dtor_kinds
*dtor_kind
)
5327 struct demangle_component
*dc
;
5330 *ctor_kind
= (enum gnu_v3_ctor_kinds
) 0;
5331 *dtor_kind
= (enum gnu_v3_dtor_kinds
) 0;
5333 cplus_demangle_init_info (mangled
, DMGL_GNU_V3
, strlen (mangled
), &di
);
5336 #ifdef CP_DYNAMIC_ARRAYS
5337 __extension__
struct demangle_component comps
[di
.num_comps
];
5338 __extension__
struct demangle_component
*subs
[di
.num_subs
];
5343 di
.comps
= alloca (di
.num_comps
* sizeof (*di
.comps
));
5344 di
.subs
= alloca (di
.num_subs
* sizeof (*di
.subs
));
5347 dc
= cplus_demangle_mangled_name (&di
, 1);
5349 /* Note that because we did not pass DMGL_PARAMS, we don't expect
5350 to demangle the entire string. */
5360 case DEMANGLE_COMPONENT_TYPED_NAME
:
5361 case DEMANGLE_COMPONENT_TEMPLATE
:
5362 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
5363 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
5364 case DEMANGLE_COMPONENT_CONST_THIS
:
5367 case DEMANGLE_COMPONENT_QUAL_NAME
:
5368 case DEMANGLE_COMPONENT_LOCAL_NAME
:
5371 case DEMANGLE_COMPONENT_CTOR
:
5372 *ctor_kind
= dc
->u
.s_ctor
.kind
;
5376 case DEMANGLE_COMPONENT_DTOR
:
5377 *dtor_kind
= dc
->u
.s_dtor
.kind
;
5388 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
5389 name. A non-zero return indicates the type of constructor. */
5391 enum gnu_v3_ctor_kinds
5392 is_gnu_v3_mangled_ctor (const char *name
)
5394 enum gnu_v3_ctor_kinds ctor_kind
;
5395 enum gnu_v3_dtor_kinds dtor_kind
;
5397 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
5398 return (enum gnu_v3_ctor_kinds
) 0;
5403 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
5404 name. A non-zero return indicates the type of destructor. */
5406 enum gnu_v3_dtor_kinds
5407 is_gnu_v3_mangled_dtor (const char *name
)
5409 enum gnu_v3_ctor_kinds ctor_kind
;
5410 enum gnu_v3_dtor_kinds dtor_kind
;
5412 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
5413 return (enum gnu_v3_dtor_kinds
) 0;
5417 #endif /* IN_GLIBCPP_V3 */
5419 #ifdef STANDALONE_DEMANGLER
5422 #include "dyn-string.h"
5424 static void print_usage (FILE* fp
, int exit_value
);
5426 #define IS_ALPHA(CHAR) \
5427 (((CHAR) >= 'a' && (CHAR) <= 'z') \
5428 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
5430 /* Non-zero if CHAR is a character than can occur in a mangled name. */
5431 #define is_mangled_char(CHAR) \
5432 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
5433 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
5435 /* The name of this program, as invoked. */
5436 const char* program_name
;
5438 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
5441 print_usage (FILE* fp
, int exit_value
)
5443 fprintf (fp
, "Usage: %s [options] [names ...]\n", program_name
);
5444 fprintf (fp
, "Options:\n");
5445 fprintf (fp
, " -h,--help Display this message.\n");
5446 fprintf (fp
, " -p,--no-params Don't display function parameters\n");
5447 fprintf (fp
, " -v,--verbose Produce verbose demanglings.\n");
5448 fprintf (fp
, "If names are provided, they are demangled. Otherwise filters standard input.\n");
5453 /* Option specification for getopt_long. */
5454 static const struct option long_options
[] =
5456 { "help", no_argument
, NULL
, 'h' },
5457 { "no-params", no_argument
, NULL
, 'p' },
5458 { "verbose", no_argument
, NULL
, 'v' },
5459 { NULL
, no_argument
, NULL
, 0 },
5462 /* Main entry for a demangling filter executable. It will demangle
5463 its command line arguments, if any. If none are provided, it will
5464 filter stdin to stdout, replacing any recognized mangled C++ names
5465 with their demangled equivalents. */
5468 main (int argc
, char *argv
[])
5472 int options
= DMGL_PARAMS
| DMGL_ANSI
| DMGL_TYPES
;
5474 /* Use the program name of this program, as invoked. */
5475 program_name
= argv
[0];
5477 /* Parse options. */
5480 opt_char
= getopt_long (argc
, argv
, "hpv", long_options
, NULL
);
5483 case '?': /* Unrecognized option. */
5484 print_usage (stderr
, 1);
5488 print_usage (stdout
, 0);
5492 options
&= ~ DMGL_PARAMS
;
5496 options
|= DMGL_VERBOSE
;
5500 while (opt_char
!= -1);
5503 /* No command line arguments were provided. Filter stdin. */
5505 dyn_string_t mangled
= dyn_string_new (3);
5508 /* Read all of input. */
5509 while (!feof (stdin
))
5513 /* Pile characters into mangled until we hit one that can't
5514 occur in a mangled name. */
5516 while (!feof (stdin
) && is_mangled_char (c
))
5518 dyn_string_append_char (mangled
, c
);
5524 if (dyn_string_length (mangled
) > 0)
5526 #ifdef IN_GLIBCPP_V3
5527 s
= __cxa_demangle (dyn_string_buf (mangled
), NULL
, NULL
, NULL
);
5529 s
= cplus_demangle_v3 (dyn_string_buf (mangled
), options
);
5539 /* It might not have been a mangled name. Print the
5541 fputs (dyn_string_buf (mangled
), stdout
);
5544 dyn_string_clear (mangled
);
5547 /* If we haven't hit EOF yet, we've read one character that
5548 can't occur in a mangled name, so print it out. */
5553 dyn_string_delete (mangled
);
5556 /* Demangle command line arguments. */
5558 /* Loop over command line arguments. */
5559 for (i
= optind
; i
< argc
; ++i
)
5562 #ifdef IN_GLIBCPP_V3
5566 /* Attempt to demangle. */
5567 #ifdef IN_GLIBCPP_V3
5568 s
= __cxa_demangle (argv
[i
], NULL
, NULL
, &status
);
5570 s
= cplus_demangle_v3 (argv
[i
], options
);
5573 /* If it worked, print the demangled name. */
5581 #ifdef IN_GLIBCPP_V3
5582 fprintf (stderr
, "Failed: %s (status %d)\n", argv
[i
], status
);
5584 fprintf (stderr
, "Failed: %s\n", argv
[i
]);
5593 #endif /* STANDALONE_DEMANGLER */