1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
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 /* The options passed to the demangler. */
283 /* Fixed-length allocated buffer for demangled data, flushed to the
284 callback with a NUL termination once full. */
285 char buf
[D_PRINT_BUFFER_LENGTH
];
286 /* Current length of data in buffer. */
288 /* The last character printed, saved individually so that it survives
291 /* Callback function to handle demangled buffer flush. */
292 demangle_callbackref callback
;
293 /* Opaque callback argument. */
295 /* The current list of templates, if any. */
296 struct d_print_template
*templates
;
297 /* The current list of modifiers (e.g., pointer, reference, etc.),
299 struct d_print_mod
*modifiers
;
300 /* Set to 1 if we saw a demangling error. */
301 int demangle_failure
;
304 #ifdef CP_DEMANGLE_DEBUG
305 static void d_dump (struct demangle_component
*, int);
308 static struct demangle_component
*
309 d_make_empty (struct d_info
*);
311 static struct demangle_component
*
312 d_make_comp (struct d_info
*, enum demangle_component_type
,
313 struct demangle_component
*,
314 struct demangle_component
*);
316 static struct demangle_component
*
317 d_make_name (struct d_info
*, const char *, int);
319 static struct demangle_component
*
320 d_make_builtin_type (struct d_info
*,
321 const struct demangle_builtin_type_info
*);
323 static struct demangle_component
*
324 d_make_operator (struct d_info
*,
325 const struct demangle_operator_info
*);
327 static struct demangle_component
*
328 d_make_extended_operator (struct d_info
*, int,
329 struct demangle_component
*);
331 static struct demangle_component
*
332 d_make_ctor (struct d_info
*, enum gnu_v3_ctor_kinds
,
333 struct demangle_component
*);
335 static struct demangle_component
*
336 d_make_dtor (struct d_info
*, enum gnu_v3_dtor_kinds
,
337 struct demangle_component
*);
339 static struct demangle_component
*
340 d_make_template_param (struct d_info
*, long);
342 static struct demangle_component
*
343 d_make_sub (struct d_info
*, const char *, int);
346 has_return_type (struct demangle_component
*);
349 is_ctor_dtor_or_conversion (struct demangle_component
*);
351 static struct demangle_component
*d_encoding (struct d_info
*, int);
353 static struct demangle_component
*d_name (struct d_info
*);
355 static struct demangle_component
*d_nested_name (struct d_info
*);
357 static struct demangle_component
*d_prefix (struct d_info
*);
359 static struct demangle_component
*d_unqualified_name (struct d_info
*);
361 static struct demangle_component
*d_source_name (struct d_info
*);
363 static long d_number (struct d_info
*);
365 static struct demangle_component
*d_identifier (struct d_info
*, int);
367 static struct demangle_component
*d_operator_name (struct d_info
*);
369 static struct demangle_component
*d_special_name (struct d_info
*);
371 static int d_call_offset (struct d_info
*, int);
373 static struct demangle_component
*d_ctor_dtor_name (struct d_info
*);
375 static struct demangle_component
**
376 d_cv_qualifiers (struct d_info
*, struct demangle_component
**, int);
378 static struct demangle_component
*
379 d_function_type (struct d_info
*);
381 static struct demangle_component
*
382 d_bare_function_type (struct d_info
*, int);
384 static struct demangle_component
*
385 d_class_enum_type (struct d_info
*);
387 static struct demangle_component
*d_array_type (struct d_info
*);
389 static struct demangle_component
*
390 d_pointer_to_member_type (struct d_info
*);
392 static struct demangle_component
*
393 d_template_param (struct d_info
*);
395 static struct demangle_component
*d_template_args (struct d_info
*);
397 static struct demangle_component
*
398 d_template_arg (struct d_info
*);
400 static struct demangle_component
*d_expression (struct d_info
*);
402 static struct demangle_component
*d_expr_primary (struct d_info
*);
404 static struct demangle_component
*d_local_name (struct d_info
*);
406 static int d_discriminator (struct d_info
*);
409 d_add_substitution (struct d_info
*, struct demangle_component
*);
411 static struct demangle_component
*d_substitution (struct d_info
*, int);
413 static void d_growable_string_init (struct d_growable_string
*, size_t);
416 d_growable_string_resize (struct d_growable_string
*, size_t);
419 d_growable_string_append_buffer (struct d_growable_string
*,
420 const char *, size_t);
422 d_growable_string_callback_adapter (const char *, size_t, void *);
425 d_print_init (struct d_print_info
*, int, demangle_callbackref
, void *);
427 static inline void d_print_error (struct d_print_info
*);
429 static inline int d_print_saw_error (struct d_print_info
*);
431 static inline void d_print_flush (struct d_print_info
*);
433 static inline void d_append_char (struct d_print_info
*, char);
435 static inline void d_append_buffer (struct d_print_info
*,
436 const char *, size_t);
438 static inline void d_append_string (struct d_print_info
*, const char *);
440 static inline char d_last_char (struct d_print_info
*);
443 d_print_comp (struct d_print_info
*, const struct demangle_component
*);
446 d_print_java_identifier (struct d_print_info
*, const char *, int);
449 d_print_mod_list (struct d_print_info
*, struct d_print_mod
*, int);
452 d_print_mod (struct d_print_info
*, const struct demangle_component
*);
455 d_print_function_type (struct d_print_info
*,
456 const struct demangle_component
*,
457 struct d_print_mod
*);
460 d_print_array_type (struct d_print_info
*,
461 const struct demangle_component
*,
462 struct d_print_mod
*);
465 d_print_expr_op (struct d_print_info
*, const struct demangle_component
*);
468 d_print_cast (struct d_print_info
*, const struct demangle_component
*);
470 static int d_demangle_callback (const char *, int,
471 demangle_callbackref
, void *);
472 static char *d_demangle (const char *, int, size_t *);
474 #ifdef CP_DEMANGLE_DEBUG
477 d_dump (struct demangle_component
*dc
, int indent
)
484 printf ("failed demangling\n");
488 for (i
= 0; i
< indent
; ++i
)
493 case DEMANGLE_COMPONENT_NAME
:
494 printf ("name '%.*s'\n", dc
->u
.s_name
.len
, dc
->u
.s_name
.s
);
496 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
497 printf ("template parameter %ld\n", dc
->u
.s_number
.number
);
499 case DEMANGLE_COMPONENT_CTOR
:
500 printf ("constructor %d\n", (int) dc
->u
.s_ctor
.kind
);
501 d_dump (dc
->u
.s_ctor
.name
, indent
+ 2);
503 case DEMANGLE_COMPONENT_DTOR
:
504 printf ("destructor %d\n", (int) dc
->u
.s_dtor
.kind
);
505 d_dump (dc
->u
.s_dtor
.name
, indent
+ 2);
507 case DEMANGLE_COMPONENT_SUB_STD
:
508 printf ("standard substitution %s\n", dc
->u
.s_string
.string
);
510 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
511 printf ("builtin type %s\n", dc
->u
.s_builtin
.type
->name
);
513 case DEMANGLE_COMPONENT_OPERATOR
:
514 printf ("operator %s\n", dc
->u
.s_operator
.op
->name
);
516 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
517 printf ("extended operator with %d args\n",
518 dc
->u
.s_extended_operator
.args
);
519 d_dump (dc
->u
.s_extended_operator
.name
, indent
+ 2);
522 case DEMANGLE_COMPONENT_QUAL_NAME
:
523 printf ("qualified name\n");
525 case DEMANGLE_COMPONENT_LOCAL_NAME
:
526 printf ("local name\n");
528 case DEMANGLE_COMPONENT_TYPED_NAME
:
529 printf ("typed name\n");
531 case DEMANGLE_COMPONENT_TEMPLATE
:
532 printf ("template\n");
534 case DEMANGLE_COMPONENT_VTABLE
:
537 case DEMANGLE_COMPONENT_VTT
:
540 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
541 printf ("construction vtable\n");
543 case DEMANGLE_COMPONENT_TYPEINFO
:
544 printf ("typeinfo\n");
546 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
547 printf ("typeinfo name\n");
549 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
550 printf ("typeinfo function\n");
552 case DEMANGLE_COMPONENT_THUNK
:
555 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
556 printf ("virtual thunk\n");
558 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
559 printf ("covariant thunk\n");
561 case DEMANGLE_COMPONENT_JAVA_CLASS
:
562 printf ("java class\n");
564 case DEMANGLE_COMPONENT_GUARD
:
567 case DEMANGLE_COMPONENT_REFTEMP
:
568 printf ("reference temporary\n");
570 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
571 printf ("hidden alias\n");
573 case DEMANGLE_COMPONENT_RESTRICT
:
574 printf ("restrict\n");
576 case DEMANGLE_COMPONENT_VOLATILE
:
577 printf ("volatile\n");
579 case DEMANGLE_COMPONENT_CONST
:
582 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
583 printf ("restrict this\n");
585 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
586 printf ("volatile this\n");
588 case DEMANGLE_COMPONENT_CONST_THIS
:
589 printf ("const this\n");
591 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
592 printf ("vendor type qualifier\n");
594 case DEMANGLE_COMPONENT_POINTER
:
595 printf ("pointer\n");
597 case DEMANGLE_COMPONENT_REFERENCE
:
598 printf ("reference\n");
600 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
601 printf ("rvalue reference\n");
603 case DEMANGLE_COMPONENT_COMPLEX
:
604 printf ("complex\n");
606 case DEMANGLE_COMPONENT_IMAGINARY
:
607 printf ("imaginary\n");
609 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
610 printf ("vendor type\n");
612 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
613 printf ("function type\n");
615 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
616 printf ("array type\n");
618 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
619 printf ("pointer to member type\n");
621 case DEMANGLE_COMPONENT_ARGLIST
:
622 printf ("argument list\n");
624 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
625 printf ("template argument list\n");
627 case DEMANGLE_COMPONENT_CAST
:
630 case DEMANGLE_COMPONENT_UNARY
:
631 printf ("unary operator\n");
633 case DEMANGLE_COMPONENT_BINARY
:
634 printf ("binary operator\n");
636 case DEMANGLE_COMPONENT_BINARY_ARGS
:
637 printf ("binary operator arguments\n");
639 case DEMANGLE_COMPONENT_TRINARY
:
640 printf ("trinary operator\n");
642 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
643 printf ("trinary operator arguments 1\n");
645 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
646 printf ("trinary operator arguments 1\n");
648 case DEMANGLE_COMPONENT_LITERAL
:
649 printf ("literal\n");
651 case DEMANGLE_COMPONENT_LITERAL_NEG
:
652 printf ("negative literal\n");
654 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
655 printf ("java resource\n");
657 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
658 printf ("compound name\n");
660 case DEMANGLE_COMPONENT_CHARACTER
:
661 printf ("character '%c'\n", dc
->u
.s_character
.character
);
663 case DEMANGLE_COMPONENT_DECLTYPE
:
664 printf ("decltype\n");
668 d_dump (d_left (dc
), indent
+ 2);
669 d_dump (d_right (dc
), indent
+ 2);
672 #endif /* CP_DEMANGLE_DEBUG */
674 /* Fill in a DEMANGLE_COMPONENT_NAME. */
676 CP_STATIC_IF_GLIBCPP_V3
678 cplus_demangle_fill_name (struct demangle_component
*p
, const char *s
, int len
)
680 if (p
== NULL
|| s
== NULL
|| len
== 0)
682 p
->type
= DEMANGLE_COMPONENT_NAME
;
684 p
->u
.s_name
.len
= len
;
688 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
690 CP_STATIC_IF_GLIBCPP_V3
692 cplus_demangle_fill_extended_operator (struct demangle_component
*p
, int args
,
693 struct demangle_component
*name
)
695 if (p
== NULL
|| args
< 0 || name
== NULL
)
697 p
->type
= DEMANGLE_COMPONENT_EXTENDED_OPERATOR
;
698 p
->u
.s_extended_operator
.args
= args
;
699 p
->u
.s_extended_operator
.name
= name
;
703 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
705 CP_STATIC_IF_GLIBCPP_V3
707 cplus_demangle_fill_ctor (struct demangle_component
*p
,
708 enum gnu_v3_ctor_kinds kind
,
709 struct demangle_component
*name
)
713 || (kind
< gnu_v3_complete_object_ctor
714 && kind
> gnu_v3_complete_object_allocating_ctor
))
716 p
->type
= DEMANGLE_COMPONENT_CTOR
;
717 p
->u
.s_ctor
.kind
= kind
;
718 p
->u
.s_ctor
.name
= name
;
722 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
724 CP_STATIC_IF_GLIBCPP_V3
726 cplus_demangle_fill_dtor (struct demangle_component
*p
,
727 enum gnu_v3_dtor_kinds kind
,
728 struct demangle_component
*name
)
732 || (kind
< gnu_v3_deleting_dtor
733 && kind
> gnu_v3_base_object_dtor
))
735 p
->type
= DEMANGLE_COMPONENT_DTOR
;
736 p
->u
.s_dtor
.kind
= kind
;
737 p
->u
.s_dtor
.name
= name
;
741 /* Add a new component. */
743 static struct demangle_component
*
744 d_make_empty (struct d_info
*di
)
746 struct demangle_component
*p
;
748 if (di
->next_comp
>= di
->num_comps
)
750 p
= &di
->comps
[di
->next_comp
];
755 /* Add a new generic component. */
757 static struct demangle_component
*
758 d_make_comp (struct d_info
*di
, enum demangle_component_type type
,
759 struct demangle_component
*left
,
760 struct demangle_component
*right
)
762 struct demangle_component
*p
;
764 /* We check for errors here. A typical error would be a NULL return
765 from a subroutine. We catch those here, and return NULL
769 /* These types require two parameters. */
770 case DEMANGLE_COMPONENT_QUAL_NAME
:
771 case DEMANGLE_COMPONENT_LOCAL_NAME
:
772 case DEMANGLE_COMPONENT_TYPED_NAME
:
773 case DEMANGLE_COMPONENT_TEMPLATE
:
774 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
775 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
776 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
777 case DEMANGLE_COMPONENT_UNARY
:
778 case DEMANGLE_COMPONENT_BINARY
:
779 case DEMANGLE_COMPONENT_BINARY_ARGS
:
780 case DEMANGLE_COMPONENT_TRINARY
:
781 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
782 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
783 case DEMANGLE_COMPONENT_LITERAL
:
784 case DEMANGLE_COMPONENT_LITERAL_NEG
:
785 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
786 if (left
== NULL
|| right
== NULL
)
790 /* These types only require one parameter. */
791 case DEMANGLE_COMPONENT_VTABLE
:
792 case DEMANGLE_COMPONENT_VTT
:
793 case DEMANGLE_COMPONENT_TYPEINFO
:
794 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
795 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
796 case DEMANGLE_COMPONENT_THUNK
:
797 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
798 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
799 case DEMANGLE_COMPONENT_JAVA_CLASS
:
800 case DEMANGLE_COMPONENT_GUARD
:
801 case DEMANGLE_COMPONENT_REFTEMP
:
802 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
803 case DEMANGLE_COMPONENT_POINTER
:
804 case DEMANGLE_COMPONENT_REFERENCE
:
805 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
806 case DEMANGLE_COMPONENT_COMPLEX
:
807 case DEMANGLE_COMPONENT_IMAGINARY
:
808 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
809 case DEMANGLE_COMPONENT_ARGLIST
:
810 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
811 case DEMANGLE_COMPONENT_CAST
:
812 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
813 case DEMANGLE_COMPONENT_DECLTYPE
:
818 /* This needs a right parameter, but the left parameter can be
820 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
825 /* These are allowed to have no parameters--in some cases they
826 will be filled in later. */
827 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
828 case DEMANGLE_COMPONENT_RESTRICT
:
829 case DEMANGLE_COMPONENT_VOLATILE
:
830 case DEMANGLE_COMPONENT_CONST
:
831 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
832 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
833 case DEMANGLE_COMPONENT_CONST_THIS
:
836 /* Other types should not be seen here. */
841 p
= d_make_empty (di
);
845 p
->u
.s_binary
.left
= left
;
846 p
->u
.s_binary
.right
= right
;
851 /* Add a new name component. */
853 static struct demangle_component
*
854 d_make_name (struct d_info
*di
, const char *s
, int len
)
856 struct demangle_component
*p
;
858 p
= d_make_empty (di
);
859 if (! cplus_demangle_fill_name (p
, s
, len
))
864 /* Add a new builtin type component. */
866 static struct demangle_component
*
867 d_make_builtin_type (struct d_info
*di
,
868 const struct demangle_builtin_type_info
*type
)
870 struct demangle_component
*p
;
874 p
= d_make_empty (di
);
877 p
->type
= DEMANGLE_COMPONENT_BUILTIN_TYPE
;
878 p
->u
.s_builtin
.type
= type
;
883 /* Add a new operator component. */
885 static struct demangle_component
*
886 d_make_operator (struct d_info
*di
, const struct demangle_operator_info
*op
)
888 struct demangle_component
*p
;
890 p
= d_make_empty (di
);
893 p
->type
= DEMANGLE_COMPONENT_OPERATOR
;
894 p
->u
.s_operator
.op
= op
;
899 /* Add a new extended operator component. */
901 static struct demangle_component
*
902 d_make_extended_operator (struct d_info
*di
, int args
,
903 struct demangle_component
*name
)
905 struct demangle_component
*p
;
907 p
= d_make_empty (di
);
908 if (! cplus_demangle_fill_extended_operator (p
, args
, name
))
913 /* Add a new constructor component. */
915 static struct demangle_component
*
916 d_make_ctor (struct d_info
*di
, enum gnu_v3_ctor_kinds kind
,
917 struct demangle_component
*name
)
919 struct demangle_component
*p
;
921 p
= d_make_empty (di
);
922 if (! cplus_demangle_fill_ctor (p
, kind
, name
))
927 /* Add a new destructor component. */
929 static struct demangle_component
*
930 d_make_dtor (struct d_info
*di
, enum gnu_v3_dtor_kinds kind
,
931 struct demangle_component
*name
)
933 struct demangle_component
*p
;
935 p
= d_make_empty (di
);
936 if (! cplus_demangle_fill_dtor (p
, kind
, name
))
941 /* Add a new template parameter. */
943 static struct demangle_component
*
944 d_make_template_param (struct d_info
*di
, long i
)
946 struct demangle_component
*p
;
948 p
= d_make_empty (di
);
951 p
->type
= DEMANGLE_COMPONENT_TEMPLATE_PARAM
;
952 p
->u
.s_number
.number
= i
;
957 /* Add a new standard substitution component. */
959 static struct demangle_component
*
960 d_make_sub (struct d_info
*di
, const char *name
, int len
)
962 struct demangle_component
*p
;
964 p
= d_make_empty (di
);
967 p
->type
= DEMANGLE_COMPONENT_SUB_STD
;
968 p
->u
.s_string
.string
= name
;
969 p
->u
.s_string
.len
= len
;
974 /* <mangled-name> ::= _Z <encoding>
976 TOP_LEVEL is non-zero when called at the top level. */
978 CP_STATIC_IF_GLIBCPP_V3
979 struct demangle_component
*
980 cplus_demangle_mangled_name (struct d_info
*di
, int top_level
)
982 if (! d_check_char (di
, '_'))
984 if (! d_check_char (di
, 'Z'))
986 return d_encoding (di
, top_level
);
989 /* Return whether a function should have a return type. The argument
990 is the function name, which may be qualified in various ways. The
991 rules are that template functions have return types with some
992 exceptions, function types which are not part of a function name
993 mangling have return types with some exceptions, and non-template
994 function names do not have return types. The exceptions are that
995 constructors, destructors, and conversion operators do not have
999 has_return_type (struct demangle_component
*dc
)
1007 case DEMANGLE_COMPONENT_TEMPLATE
:
1008 return ! is_ctor_dtor_or_conversion (d_left (dc
));
1009 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
1010 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
1011 case DEMANGLE_COMPONENT_CONST_THIS
:
1012 return has_return_type (d_left (dc
));
1016 /* Return whether a name is a constructor, a destructor, or a
1017 conversion operator. */
1020 is_ctor_dtor_or_conversion (struct demangle_component
*dc
)
1028 case DEMANGLE_COMPONENT_QUAL_NAME
:
1029 case DEMANGLE_COMPONENT_LOCAL_NAME
:
1030 return is_ctor_dtor_or_conversion (d_right (dc
));
1031 case DEMANGLE_COMPONENT_CTOR
:
1032 case DEMANGLE_COMPONENT_DTOR
:
1033 case DEMANGLE_COMPONENT_CAST
:
1038 /* <encoding> ::= <(function) name> <bare-function-type>
1042 TOP_LEVEL is non-zero when called at the top level, in which case
1043 if DMGL_PARAMS is not set we do not demangle the function
1044 parameters. We only set this at the top level, because otherwise
1045 we would not correctly demangle names in local scopes. */
1047 static struct demangle_component
*
1048 d_encoding (struct d_info
*di
, int top_level
)
1050 char peek
= d_peek_char (di
);
1052 if (peek
== 'G' || peek
== 'T')
1053 return d_special_name (di
);
1056 struct demangle_component
*dc
;
1060 if (dc
!= NULL
&& top_level
&& (di
->options
& DMGL_PARAMS
) == 0)
1062 /* Strip off any initial CV-qualifiers, as they really apply
1063 to the `this' parameter, and they were not output by the
1064 v2 demangler without DMGL_PARAMS. */
1065 while (dc
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
1066 || dc
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
1067 || dc
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
1070 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1071 there may be CV-qualifiers on its right argument which
1072 really apply here; this happens when parsing a class
1073 which is local to a function. */
1074 if (dc
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
1076 struct demangle_component
*dcr
;
1079 while (dcr
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
1080 || dcr
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
1081 || dcr
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
1083 dc
->u
.s_binary
.right
= dcr
;
1089 peek
= d_peek_char (di
);
1090 if (dc
== NULL
|| peek
== '\0' || peek
== 'E')
1092 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPED_NAME
, dc
,
1093 d_bare_function_type (di
, has_return_type (dc
)));
1097 /* <name> ::= <nested-name>
1099 ::= <unscoped-template-name> <template-args>
1102 <unscoped-name> ::= <unqualified-name>
1103 ::= St <unqualified-name>
1105 <unscoped-template-name> ::= <unscoped-name>
1109 static struct demangle_component
*
1110 d_name (struct d_info
*di
)
1112 char peek
= d_peek_char (di
);
1113 struct demangle_component
*dc
;
1118 return d_nested_name (di
);
1121 return d_local_name (di
);
1124 return d_unqualified_name (di
);
1130 if (d_peek_next_char (di
) != 't')
1132 dc
= d_substitution (di
, 0);
1138 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
,
1139 d_make_name (di
, "std", 3),
1140 d_unqualified_name (di
));
1145 if (d_peek_char (di
) != 'I')
1147 /* The grammar does not permit this case to occur if we
1148 called d_substitution() above (i.e., subst == 1). We
1149 don't bother to check. */
1153 /* This is <template-args>, which means that we just saw
1154 <unscoped-template-name>, which is a substitution
1155 candidate if we didn't just get it from a
1159 if (! d_add_substitution (di
, dc
))
1162 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, dc
,
1163 d_template_args (di
));
1170 dc
= d_unqualified_name (di
);
1171 if (d_peek_char (di
) == 'I')
1173 /* This is <template-args>, which means that we just saw
1174 <unscoped-template-name>, which is a substitution
1176 if (! d_add_substitution (di
, dc
))
1178 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, dc
,
1179 d_template_args (di
));
1185 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1186 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1189 static struct demangle_component
*
1190 d_nested_name (struct d_info
*di
)
1192 struct demangle_component
*ret
;
1193 struct demangle_component
**pret
;
1195 if (! d_check_char (di
, 'N'))
1198 pret
= d_cv_qualifiers (di
, &ret
, 1);
1202 *pret
= d_prefix (di
);
1206 if (! d_check_char (di
, 'E'))
1212 /* <prefix> ::= <prefix> <unqualified-name>
1213 ::= <template-prefix> <template-args>
1214 ::= <template-param>
1218 <template-prefix> ::= <prefix> <(template) unqualified-name>
1219 ::= <template-param>
1223 static struct demangle_component
*
1224 d_prefix (struct d_info
*di
)
1226 struct demangle_component
*ret
= NULL
;
1231 enum demangle_component_type comb_type
;
1232 struct demangle_component
*dc
;
1234 peek
= d_peek_char (di
);
1238 /* The older code accepts a <local-name> here, but I don't see
1239 that in the grammar. The older code does not accept a
1240 <template-param> here. */
1242 comb_type
= DEMANGLE_COMPONENT_QUAL_NAME
;
1248 dc
= d_unqualified_name (di
);
1249 else if (peek
== 'S')
1250 dc
= d_substitution (di
, 1);
1251 else if (peek
== 'I')
1255 comb_type
= DEMANGLE_COMPONENT_TEMPLATE
;
1256 dc
= d_template_args (di
);
1258 else if (peek
== 'T')
1259 dc
= d_template_param (di
);
1260 else if (peek
== 'E')
1268 ret
= d_make_comp (di
, comb_type
, ret
, dc
);
1270 if (peek
!= 'S' && d_peek_char (di
) != 'E')
1272 if (! d_add_substitution (di
, ret
))
1278 /* <unqualified-name> ::= <operator-name>
1279 ::= <ctor-dtor-name>
1281 ::= <local-source-name>
1283 <local-source-name> ::= L <source-name> <discriminator>
1286 static struct demangle_component
*
1287 d_unqualified_name (struct d_info
*di
)
1291 peek
= d_peek_char (di
);
1292 if (IS_DIGIT (peek
))
1293 return d_source_name (di
);
1294 else if (IS_LOWER (peek
))
1296 struct demangle_component
*ret
;
1298 ret
= d_operator_name (di
);
1299 if (ret
!= NULL
&& ret
->type
== DEMANGLE_COMPONENT_OPERATOR
)
1300 di
->expansion
+= sizeof "operator" + ret
->u
.s_operator
.op
->len
- 2;
1303 else if (peek
== 'C' || peek
== 'D')
1304 return d_ctor_dtor_name (di
);
1305 else if (peek
== 'L')
1307 struct demangle_component
* ret
;
1311 ret
= d_source_name (di
);
1314 if (! d_discriminator (di
))
1322 /* <source-name> ::= <(positive length) number> <identifier> */
1324 static struct demangle_component
*
1325 d_source_name (struct d_info
*di
)
1328 struct demangle_component
*ret
;
1330 len
= d_number (di
);
1333 ret
= d_identifier (di
, len
);
1334 di
->last_name
= ret
;
1338 /* number ::= [n] <(non-negative decimal integer)> */
1341 d_number (struct d_info
*di
)
1348 peek
= d_peek_char (di
);
1353 peek
= d_peek_char (di
);
1359 if (! IS_DIGIT (peek
))
1365 ret
= ret
* 10 + peek
- '0';
1367 peek
= d_peek_char (di
);
1371 /* identifier ::= <(unqualified source code identifier)> */
1373 static struct demangle_component
*
1374 d_identifier (struct d_info
*di
, int len
)
1380 if (di
->send
- name
< len
)
1383 d_advance (di
, len
);
1385 /* A Java mangled name may have a trailing '$' if it is a C++
1386 keyword. This '$' is not included in the length count. We just
1388 if ((di
->options
& DMGL_JAVA
) != 0
1389 && d_peek_char (di
) == '$')
1392 /* Look for something which looks like a gcc encoding of an
1393 anonymous namespace, and replace it with a more user friendly
1395 if (len
>= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN
+ 2
1396 && memcmp (name
, ANONYMOUS_NAMESPACE_PREFIX
,
1397 ANONYMOUS_NAMESPACE_PREFIX_LEN
) == 0)
1401 s
= name
+ ANONYMOUS_NAMESPACE_PREFIX_LEN
;
1402 if ((*s
== '.' || *s
== '_' || *s
== '$')
1405 di
->expansion
-= len
- sizeof "(anonymous namespace)";
1406 return d_make_name (di
, "(anonymous namespace)",
1407 sizeof "(anonymous namespace)" - 1);
1411 return d_make_name (di
, name
, len
);
1414 /* operator_name ::= many different two character encodings.
1416 ::= v <digit> <source-name>
1419 #define NL(s) s, (sizeof s) - 1
1421 CP_STATIC_IF_GLIBCPP_V3
1422 const struct demangle_operator_info cplus_demangle_operators
[] =
1424 { "aN", NL ("&="), 2 },
1425 { "aS", NL ("="), 2 },
1426 { "aa", NL ("&&"), 2 },
1427 { "ad", NL ("&"), 1 },
1428 { "an", NL ("&"), 2 },
1429 { "cl", NL ("()"), 2 },
1430 { "cm", NL (","), 2 },
1431 { "co", NL ("~"), 1 },
1432 { "dV", NL ("/="), 2 },
1433 { "da", NL ("delete[]"), 1 },
1434 { "de", NL ("*"), 1 },
1435 { "dl", NL ("delete"), 1 },
1436 { "dv", NL ("/"), 2 },
1437 { "eO", NL ("^="), 2 },
1438 { "eo", NL ("^"), 2 },
1439 { "eq", NL ("=="), 2 },
1440 { "ge", NL (">="), 2 },
1441 { "gt", NL (">"), 2 },
1442 { "ix", NL ("[]"), 2 },
1443 { "lS", NL ("<<="), 2 },
1444 { "le", NL ("<="), 2 },
1445 { "ls", NL ("<<"), 2 },
1446 { "lt", NL ("<"), 2 },
1447 { "mI", NL ("-="), 2 },
1448 { "mL", NL ("*="), 2 },
1449 { "mi", NL ("-"), 2 },
1450 { "ml", NL ("*"), 2 },
1451 { "mm", NL ("--"), 1 },
1452 { "na", NL ("new[]"), 1 },
1453 { "ne", NL ("!="), 2 },
1454 { "ng", NL ("-"), 1 },
1455 { "nt", NL ("!"), 1 },
1456 { "nw", NL ("new"), 1 },
1457 { "oR", NL ("|="), 2 },
1458 { "oo", NL ("||"), 2 },
1459 { "or", NL ("|"), 2 },
1460 { "pL", NL ("+="), 2 },
1461 { "pl", NL ("+"), 2 },
1462 { "pm", NL ("->*"), 2 },
1463 { "pp", NL ("++"), 1 },
1464 { "ps", NL ("+"), 1 },
1465 { "pt", NL ("->"), 2 },
1466 { "qu", NL ("?"), 3 },
1467 { "rM", NL ("%="), 2 },
1468 { "rS", NL (">>="), 2 },
1469 { "rm", NL ("%"), 2 },
1470 { "rs", NL (">>"), 2 },
1471 { "st", NL ("sizeof "), 1 },
1472 { "sz", NL ("sizeof "), 1 },
1473 { NULL
, NULL
, 0, 0 }
1476 static struct demangle_component
*
1477 d_operator_name (struct d_info
*di
)
1482 c1
= d_next_char (di
);
1483 c2
= d_next_char (di
);
1484 if (c1
== 'v' && IS_DIGIT (c2
))
1485 return d_make_extended_operator (di
, c2
- '0', d_source_name (di
));
1486 else if (c1
== 'c' && c2
== 'v')
1487 return d_make_comp (di
, DEMANGLE_COMPONENT_CAST
,
1488 cplus_demangle_type (di
), NULL
);
1491 /* LOW is the inclusive lower bound. */
1493 /* HIGH is the exclusive upper bound. We subtract one to ignore
1494 the sentinel at the end of the array. */
1495 int high
= ((sizeof (cplus_demangle_operators
)
1496 / sizeof (cplus_demangle_operators
[0]))
1502 const struct demangle_operator_info
*p
;
1504 i
= low
+ (high
- low
) / 2;
1505 p
= cplus_demangle_operators
+ i
;
1507 if (c1
== p
->code
[0] && c2
== p
->code
[1])
1508 return d_make_operator (di
, p
);
1510 if (c1
< p
->code
[0] || (c1
== p
->code
[0] && c2
< p
->code
[1]))
1520 static struct demangle_component
*
1521 d_make_character (struct d_info
*di
, int c
)
1523 struct demangle_component
*p
;
1524 p
= d_make_empty (di
);
1527 p
->type
= DEMANGLE_COMPONENT_CHARACTER
;
1528 p
->u
.s_character
.character
= c
;
1533 static struct demangle_component
*
1534 d_java_resource (struct d_info
*di
)
1536 struct demangle_component
*p
= NULL
;
1537 struct demangle_component
*next
= NULL
;
1542 len
= d_number (di
);
1546 /* Eat the leading '_'. */
1547 if (d_next_char (di
) != '_')
1560 /* Each chunk is either a '$' escape... */
1578 next
= d_make_character (di
, c
);
1586 /* ... or a sequence of characters. */
1589 while (i
< len
&& str
[i
] && str
[i
] != '$')
1592 next
= d_make_name (di
, str
, i
);
1605 p
= d_make_comp (di
, DEMANGLE_COMPONENT_COMPOUND_NAME
, p
, next
);
1611 p
= d_make_comp (di
, DEMANGLE_COMPONENT_JAVA_RESOURCE
, p
, NULL
);
1616 /* <special-name> ::= TV <type>
1620 ::= GV <(object) name>
1621 ::= T <call-offset> <(base) encoding>
1622 ::= Tc <call-offset> <call-offset> <(base) encoding>
1623 Also g++ extensions:
1624 ::= TC <type> <(offset) number> _ <(base) type>
1629 ::= Gr <resource name>
1632 static struct demangle_component
*
1633 d_special_name (struct d_info
*di
)
1635 di
->expansion
+= 20;
1636 if (d_check_char (di
, 'T'))
1638 switch (d_next_char (di
))
1642 return d_make_comp (di
, DEMANGLE_COMPONENT_VTABLE
,
1643 cplus_demangle_type (di
), NULL
);
1645 di
->expansion
-= 10;
1646 return d_make_comp (di
, DEMANGLE_COMPONENT_VTT
,
1647 cplus_demangle_type (di
), NULL
);
1649 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO
,
1650 cplus_demangle_type (di
), NULL
);
1652 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO_NAME
,
1653 cplus_demangle_type (di
), NULL
);
1656 if (! d_call_offset (di
, 'h'))
1658 return d_make_comp (di
, DEMANGLE_COMPONENT_THUNK
,
1659 d_encoding (di
, 0), NULL
);
1662 if (! d_call_offset (di
, 'v'))
1664 return d_make_comp (di
, DEMANGLE_COMPONENT_VIRTUAL_THUNK
,
1665 d_encoding (di
, 0), NULL
);
1668 if (! d_call_offset (di
, '\0'))
1670 if (! d_call_offset (di
, '\0'))
1672 return d_make_comp (di
, DEMANGLE_COMPONENT_COVARIANT_THUNK
,
1673 d_encoding (di
, 0), NULL
);
1677 struct demangle_component
*derived_type
;
1679 struct demangle_component
*base_type
;
1681 derived_type
= cplus_demangle_type (di
);
1682 offset
= d_number (di
);
1685 if (! d_check_char (di
, '_'))
1687 base_type
= cplus_demangle_type (di
);
1688 /* We don't display the offset. FIXME: We should display
1689 it in verbose mode. */
1691 return d_make_comp (di
, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
,
1692 base_type
, derived_type
);
1696 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO_FN
,
1697 cplus_demangle_type (di
), NULL
);
1699 return d_make_comp (di
, DEMANGLE_COMPONENT_JAVA_CLASS
,
1700 cplus_demangle_type (di
), NULL
);
1706 else if (d_check_char (di
, 'G'))
1708 switch (d_next_char (di
))
1711 return d_make_comp (di
, DEMANGLE_COMPONENT_GUARD
, d_name (di
), NULL
);
1714 return d_make_comp (di
, DEMANGLE_COMPONENT_REFTEMP
, d_name (di
),
1718 return d_make_comp (di
, DEMANGLE_COMPONENT_HIDDEN_ALIAS
,
1719 d_encoding (di
, 0), NULL
);
1722 return d_java_resource (di
);
1732 /* <call-offset> ::= h <nv-offset> _
1735 <nv-offset> ::= <(offset) number>
1737 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1739 The C parameter, if not '\0', is a character we just read which is
1740 the start of the <call-offset>.
1742 We don't display the offset information anywhere. FIXME: We should
1743 display it in verbose mode. */
1746 d_call_offset (struct d_info
*di
, int c
)
1749 c
= d_next_char (di
);
1756 if (! d_check_char (di
, '_'))
1763 if (! d_check_char (di
, '_'))
1769 /* <ctor-dtor-name> ::= C1
1777 static struct demangle_component
*
1778 d_ctor_dtor_name (struct d_info
*di
)
1780 if (di
->last_name
!= NULL
)
1782 if (di
->last_name
->type
== DEMANGLE_COMPONENT_NAME
)
1783 di
->expansion
+= di
->last_name
->u
.s_name
.len
;
1784 else if (di
->last_name
->type
== DEMANGLE_COMPONENT_SUB_STD
)
1785 di
->expansion
+= di
->last_name
->u
.s_string
.len
;
1787 switch (d_peek_char (di
))
1791 enum gnu_v3_ctor_kinds kind
;
1793 switch (d_peek_next_char (di
))
1796 kind
= gnu_v3_complete_object_ctor
;
1799 kind
= gnu_v3_base_object_ctor
;
1802 kind
= gnu_v3_complete_object_allocating_ctor
;
1808 return d_make_ctor (di
, kind
, di
->last_name
);
1813 enum gnu_v3_dtor_kinds kind
;
1815 switch (d_peek_next_char (di
))
1818 kind
= gnu_v3_deleting_dtor
;
1821 kind
= gnu_v3_complete_object_dtor
;
1824 kind
= gnu_v3_base_object_dtor
;
1830 return d_make_dtor (di
, kind
, di
->last_name
);
1838 /* <type> ::= <builtin-type>
1840 ::= <class-enum-type>
1842 ::= <pointer-to-member-type>
1843 ::= <template-param>
1844 ::= <template-template-param> <template-args>
1846 ::= <CV-qualifiers> <type>
1849 ::= O <type> (C++0x)
1852 ::= U <source-name> <type>
1854 <builtin-type> ::= various one letter codes
1858 CP_STATIC_IF_GLIBCPP_V3
1859 const struct demangle_builtin_type_info
1860 cplus_demangle_builtin_types
[D_BUILTIN_TYPE_COUNT
] =
1862 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT
},
1863 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL
},
1864 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT
},
1865 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT
},
1866 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT
},
1867 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT
},
1868 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT
},
1869 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT
},
1870 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT
},
1871 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED
},
1872 /* k */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
1873 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG
},
1874 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG
},
1875 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT
},
1876 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
1878 /* The decimal floating point and half-precision floating point types
1879 don't use the normal builtin type encoding, they're just stuck into
1880 holes in the table for convenience. */
1881 /* p */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT
},
1882 /* q */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT
},
1883 /* r */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT
},
1884 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT
},
1885 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT
},
1886 /* u */ { NL ("half"), NL ("half"), D_PRINT_FLOAT
},
1887 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID
},
1888 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT
},
1889 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG
},
1890 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
1891 D_PRINT_UNSIGNED_LONG_LONG
},
1892 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT
},
1895 CP_STATIC_IF_GLIBCPP_V3
1896 struct demangle_component
*
1897 cplus_demangle_type (struct d_info
*di
)
1900 struct demangle_component
*ret
;
1903 /* The ABI specifies that when CV-qualifiers are used, the base type
1904 is substitutable, and the fully qualified type is substitutable,
1905 but the base type with a strict subset of the CV-qualifiers is
1906 not substitutable. The natural recursive implementation of the
1907 CV-qualifiers would cause subsets to be substitutable, so instead
1908 we pull them all off now.
1910 FIXME: The ABI says that order-insensitive vendor qualifiers
1911 should be handled in the same way, but we have no way to tell
1912 which vendor qualifiers are order-insensitive and which are
1913 order-sensitive. So we just assume that they are all
1914 order-sensitive. g++ 3.4 supports only one vendor qualifier,
1915 __vector, and it treats it as order-sensitive when mangling
1918 peek
= d_peek_char (di
);
1919 if (peek
== 'r' || peek
== 'V' || peek
== 'K')
1921 struct demangle_component
**pret
;
1923 pret
= d_cv_qualifiers (di
, &ret
, 0);
1926 *pret
= cplus_demangle_type (di
);
1927 if (! *pret
|| ! d_add_substitution (di
, ret
))
1936 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1937 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
1938 case 'o': case 's': case 't':
1939 case 'v': case 'w': case 'x': case 'y': case 'z':
1940 ret
= d_make_builtin_type (di
,
1941 &cplus_demangle_builtin_types
[peek
- 'a']);
1942 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
1949 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_VENDOR_TYPE
,
1950 d_source_name (di
), NULL
);
1954 ret
= d_function_type (di
);
1957 case '0': case '1': case '2': case '3': case '4':
1958 case '5': case '6': case '7': case '8': case '9':
1961 ret
= d_class_enum_type (di
);
1965 ret
= d_array_type (di
);
1969 ret
= d_pointer_to_member_type (di
);
1973 ret
= d_template_param (di
);
1974 if (d_peek_char (di
) == 'I')
1976 /* This is <template-template-param> <template-args>. The
1977 <template-template-param> part is a substitution
1979 if (! d_add_substitution (di
, ret
))
1981 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
,
1982 d_template_args (di
));
1987 /* If this is a special substitution, then it is the start of
1988 <class-enum-type>. */
1992 peek_next
= d_peek_next_char (di
);
1993 if (IS_DIGIT (peek_next
)
1995 || IS_UPPER (peek_next
))
1997 ret
= d_substitution (di
, 0);
1998 /* The substituted name may have been a template name and
1999 may be followed by tepmlate args. */
2000 if (d_peek_char (di
) == 'I')
2001 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
,
2002 d_template_args (di
));
2008 ret
= d_class_enum_type (di
);
2009 /* If the substitution was a complete type, then it is not
2010 a new substitution candidate. However, if the
2011 substitution was followed by template arguments, then
2012 the whole thing is a substitution candidate. */
2013 if (ret
!= NULL
&& ret
->type
== DEMANGLE_COMPONENT_SUB_STD
)
2021 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_RVALUE_REFERENCE
,
2022 cplus_demangle_type (di
), NULL
);
2027 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_POINTER
,
2028 cplus_demangle_type (di
), NULL
);
2033 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_REFERENCE
,
2034 cplus_demangle_type (di
), NULL
);
2039 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_COMPLEX
,
2040 cplus_demangle_type (di
), NULL
);
2045 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_IMAGINARY
,
2046 cplus_demangle_type (di
), NULL
);
2051 ret
= d_source_name (di
);
2052 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
,
2053 cplus_demangle_type (di
), ret
);
2059 peek
= d_next_char (di
);
2064 /* decltype (expression) */
2065 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_DECLTYPE
,
2066 d_expression (di
), NULL
);
2067 if (ret
&& d_next_char (di
) != 'E')
2072 /* Pack expansion. */
2077 ret
= d_make_builtin_type (di
,
2078 &cplus_demangle_builtin_types
['p' - 'a']);
2079 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2082 /* 64-bit decimal floating point */
2083 ret
= d_make_builtin_type (di
,
2084 &cplus_demangle_builtin_types
['q' - 'a']);
2085 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2089 ret
= d_make_builtin_type (di
,
2090 &cplus_demangle_builtin_types
['r' - 'a']);
2091 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2094 /* 16-bit half-precision FP */
2095 ret
= d_make_builtin_type (di
,
2096 &cplus_demangle_builtin_types
['u' - 'a']);
2097 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2108 if (! d_add_substitution (di
, ret
))
2115 /* <CV-qualifiers> ::= [r] [V] [K] */
2117 static struct demangle_component
**
2118 d_cv_qualifiers (struct d_info
*di
,
2119 struct demangle_component
**pret
, int member_fn
)
2123 peek
= d_peek_char (di
);
2124 while (peek
== 'r' || peek
== 'V' || peek
== 'K')
2126 enum demangle_component_type t
;
2132 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2133 : DEMANGLE_COMPONENT_RESTRICT
);
2134 di
->expansion
+= sizeof "restrict";
2136 else if (peek
== 'V')
2139 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2140 : DEMANGLE_COMPONENT_VOLATILE
);
2141 di
->expansion
+= sizeof "volatile";
2146 ? DEMANGLE_COMPONENT_CONST_THIS
2147 : DEMANGLE_COMPONENT_CONST
);
2148 di
->expansion
+= sizeof "const";
2151 *pret
= d_make_comp (di
, t
, NULL
, NULL
);
2154 pret
= &d_left (*pret
);
2156 peek
= d_peek_char (di
);
2162 /* <function-type> ::= F [Y] <bare-function-type> E */
2164 static struct demangle_component
*
2165 d_function_type (struct d_info
*di
)
2167 struct demangle_component
*ret
;
2169 if (! d_check_char (di
, 'F'))
2171 if (d_peek_char (di
) == 'Y')
2173 /* Function has C linkage. We don't print this information.
2174 FIXME: We should print it in verbose mode. */
2177 ret
= d_bare_function_type (di
, 1);
2178 if (! d_check_char (di
, 'E'))
2183 /* <bare-function-type> ::= [J]<type>+ */
2185 static struct demangle_component
*
2186 d_bare_function_type (struct d_info
*di
, int has_return_type
)
2188 struct demangle_component
*return_type
;
2189 struct demangle_component
*tl
;
2190 struct demangle_component
**ptl
;
2193 /* Detect special qualifier indicating that the first argument
2194 is the return type. */
2195 peek
= d_peek_char (di
);
2199 has_return_type
= 1;
2207 struct demangle_component
*type
;
2209 peek
= d_peek_char (di
);
2210 if (peek
== '\0' || peek
== 'E')
2212 type
= cplus_demangle_type (di
);
2215 if (has_return_type
)
2218 has_return_type
= 0;
2222 *ptl
= d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, type
, NULL
);
2225 ptl
= &d_right (*ptl
);
2229 /* There should be at least one parameter type besides the optional
2230 return type. A function which takes no arguments will have a
2231 single parameter type void. */
2235 /* If we have a single parameter type void, omit it. */
2236 if (d_right (tl
) == NULL
2237 && d_left (tl
)->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
2238 && d_left (tl
)->u
.s_builtin
.type
->print
== D_PRINT_VOID
)
2240 di
->expansion
-= d_left (tl
)->u
.s_builtin
.type
->len
;
2244 return d_make_comp (di
, DEMANGLE_COMPONENT_FUNCTION_TYPE
, return_type
, tl
);
2247 /* <class-enum-type> ::= <name> */
2249 static struct demangle_component
*
2250 d_class_enum_type (struct d_info
*di
)
2255 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2256 ::= A [<(dimension) expression>] _ <(element) type>
2259 static struct demangle_component
*
2260 d_array_type (struct d_info
*di
)
2263 struct demangle_component
*dim
;
2265 if (! d_check_char (di
, 'A'))
2268 peek
= d_peek_char (di
);
2271 else if (IS_DIGIT (peek
))
2279 peek
= d_peek_char (di
);
2281 while (IS_DIGIT (peek
));
2282 dim
= d_make_name (di
, s
, d_str (di
) - s
);
2288 dim
= d_expression (di
);
2293 if (! d_check_char (di
, '_'))
2296 return d_make_comp (di
, DEMANGLE_COMPONENT_ARRAY_TYPE
, dim
,
2297 cplus_demangle_type (di
));
2300 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2302 static struct demangle_component
*
2303 d_pointer_to_member_type (struct d_info
*di
)
2305 struct demangle_component
*cl
;
2306 struct demangle_component
*mem
;
2307 struct demangle_component
**pmem
;
2309 if (! d_check_char (di
, 'M'))
2312 cl
= cplus_demangle_type (di
);
2314 /* The ABI specifies that any type can be a substitution source, and
2315 that M is followed by two types, and that when a CV-qualified
2316 type is seen both the base type and the CV-qualified types are
2317 substitution sources. The ABI also specifies that for a pointer
2318 to a CV-qualified member function, the qualifiers are attached to
2319 the second type. Given the grammar, a plain reading of the ABI
2320 suggests that both the CV-qualified member function and the
2321 non-qualified member function are substitution sources. However,
2322 g++ does not work that way. g++ treats only the CV-qualified
2323 member function as a substitution source. FIXME. So to work
2324 with g++, we need to pull off the CV-qualifiers here, in order to
2325 avoid calling add_substitution() in cplus_demangle_type(). But
2326 for a CV-qualified member which is not a function, g++ does
2327 follow the ABI, so we need to handle that case here by calling
2328 d_add_substitution ourselves. */
2330 pmem
= d_cv_qualifiers (di
, &mem
, 1);
2333 *pmem
= cplus_demangle_type (di
);
2337 if (pmem
!= &mem
&& (*pmem
)->type
!= DEMANGLE_COMPONENT_FUNCTION_TYPE
)
2339 if (! d_add_substitution (di
, mem
))
2343 return d_make_comp (di
, DEMANGLE_COMPONENT_PTRMEM_TYPE
, cl
, mem
);
2346 /* <template-param> ::= T_
2347 ::= T <(parameter-2 non-negative) number> _
2350 static struct demangle_component
*
2351 d_template_param (struct d_info
*di
)
2355 if (! d_check_char (di
, 'T'))
2358 if (d_peek_char (di
) == '_')
2362 param
= d_number (di
);
2368 if (! d_check_char (di
, '_'))
2373 return d_make_template_param (di
, param
);
2376 /* <template-args> ::= I <template-arg>+ E */
2378 static struct demangle_component
*
2379 d_template_args (struct d_info
*di
)
2381 struct demangle_component
*hold_last_name
;
2382 struct demangle_component
*al
;
2383 struct demangle_component
**pal
;
2385 /* Preserve the last name we saw--don't let the template arguments
2386 clobber it, as that would give us the wrong name for a subsequent
2387 constructor or destructor. */
2388 hold_last_name
= di
->last_name
;
2390 if (! d_check_char (di
, 'I'))
2397 struct demangle_component
*a
;
2399 a
= d_template_arg (di
);
2403 *pal
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
, a
, NULL
);
2406 pal
= &d_right (*pal
);
2408 if (d_peek_char (di
) == 'E')
2415 di
->last_name
= hold_last_name
;
2420 /* <template-arg> ::= <type>
2421 ::= X <expression> E
2425 static struct demangle_component
*
2426 d_template_arg (struct d_info
*di
)
2428 struct demangle_component
*ret
;
2430 switch (d_peek_char (di
))
2434 ret
= d_expression (di
);
2435 if (! d_check_char (di
, 'E'))
2440 return d_expr_primary (di
);
2443 return cplus_demangle_type (di
);
2447 /* Subroutine of <expression> ::= cl <expression>+ E */
2449 static struct demangle_component
*
2450 d_exprlist (struct d_info
*di
)
2452 struct demangle_component
*list
= NULL
;
2453 struct demangle_component
**p
= &list
;
2457 struct demangle_component
*arg
= d_expression (di
);
2461 *p
= d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, arg
, NULL
);
2466 if (d_peek_char (di
) == 'E')
2476 /* <expression> ::= <(unary) operator-name> <expression>
2477 ::= <(binary) operator-name> <expression> <expression>
2478 ::= <(trinary) operator-name> <expression> <expression> <expression>
2479 ::= cl <expression>+ E
2481 ::= <template-param>
2482 ::= sr <type> <unqualified-name>
2483 ::= sr <type> <unqualified-name> <template-args>
2487 static struct demangle_component
*
2488 d_expression (struct d_info
*di
)
2492 peek
= d_peek_char (di
);
2494 return d_expr_primary (di
);
2495 else if (peek
== 'T')
2496 return d_template_param (di
);
2497 else if (peek
== 's' && d_peek_next_char (di
) == 'r')
2499 struct demangle_component
*type
;
2500 struct demangle_component
*name
;
2503 type
= cplus_demangle_type (di
);
2504 name
= d_unqualified_name (di
);
2505 if (d_peek_char (di
) != 'I')
2506 return d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
, type
, name
);
2508 return d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
, type
,
2509 d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, name
,
2510 d_template_args (di
)));
2512 else if (peek
== 's' && d_peek_next_char (di
) == 'T')
2514 /* Just demangle a parameter placeholder as its type. */
2516 return cplus_demangle_type (di
);
2520 struct demangle_component
*op
;
2523 op
= d_operator_name (di
);
2527 if (op
->type
== DEMANGLE_COMPONENT_OPERATOR
)
2528 di
->expansion
+= op
->u
.s_operator
.op
->len
- 2;
2530 if (op
->type
== DEMANGLE_COMPONENT_OPERATOR
2531 && strcmp (op
->u
.s_operator
.op
->code
, "st") == 0)
2532 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
2533 cplus_demangle_type (di
));
2539 case DEMANGLE_COMPONENT_OPERATOR
:
2540 args
= op
->u
.s_operator
.op
->args
;
2542 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
2543 args
= op
->u
.s_extended_operator
.args
;
2545 case DEMANGLE_COMPONENT_CAST
:
2546 if (d_peek_char (di
) == 'v')
2547 /* T() encoded as an operand of void. */
2548 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
2549 cplus_demangle_type (di
));
2558 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
2562 struct demangle_component
*left
;
2563 struct demangle_component
*right
;
2565 left
= d_expression (di
);
2566 if (!strcmp (op
->u
.s_operator
.op
->code
, "cl"))
2567 right
= d_exprlist (di
);
2569 right
= d_expression (di
);
2571 return d_make_comp (di
, DEMANGLE_COMPONENT_BINARY
, op
,
2573 DEMANGLE_COMPONENT_BINARY_ARGS
,
2578 struct demangle_component
*first
;
2579 struct demangle_component
*second
;
2581 first
= d_expression (di
);
2582 second
= d_expression (di
);
2583 return d_make_comp (di
, DEMANGLE_COMPONENT_TRINARY
, op
,
2585 DEMANGLE_COMPONENT_TRINARY_ARG1
,
2588 DEMANGLE_COMPONENT_TRINARY_ARG2
,
2590 d_expression (di
))));
2598 /* <expr-primary> ::= L <type> <(value) number> E
2599 ::= L <type> <(value) float> E
2600 ::= L <mangled-name> E
2603 static struct demangle_component
*
2604 d_expr_primary (struct d_info
*di
)
2606 struct demangle_component
*ret
;
2608 if (! d_check_char (di
, 'L'))
2610 if (d_peek_char (di
) == '_')
2611 ret
= cplus_demangle_mangled_name (di
, 0);
2614 struct demangle_component
*type
;
2615 enum demangle_component_type t
;
2618 type
= cplus_demangle_type (di
);
2622 /* If we have a type we know how to print, we aren't going to
2623 print the type name itself. */
2624 if (type
->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
2625 && type
->u
.s_builtin
.type
->print
!= D_PRINT_DEFAULT
)
2626 di
->expansion
-= type
->u
.s_builtin
.type
->len
;
2628 /* Rather than try to interpret the literal value, we just
2629 collect it as a string. Note that it's possible to have a
2630 floating point literal here. The ABI specifies that the
2631 format of such literals is machine independent. That's fine,
2632 but what's not fine is that versions of g++ up to 3.2 with
2633 -fabi-version=1 used upper case letters in the hex constant,
2634 and dumped out gcc's internal representation. That makes it
2635 hard to tell where the constant ends, and hard to dump the
2636 constant in any readable form anyhow. We don't attempt to
2637 handle these cases. */
2639 t
= DEMANGLE_COMPONENT_LITERAL
;
2640 if (d_peek_char (di
) == 'n')
2642 t
= DEMANGLE_COMPONENT_LITERAL_NEG
;
2646 while (d_peek_char (di
) != 'E')
2648 if (d_peek_char (di
) == '\0')
2652 ret
= d_make_comp (di
, t
, type
, d_make_name (di
, s
, d_str (di
) - s
));
2654 if (! d_check_char (di
, 'E'))
2659 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2660 ::= Z <(function) encoding> E s [<discriminator>]
2663 static struct demangle_component
*
2664 d_local_name (struct d_info
*di
)
2666 struct demangle_component
*function
;
2668 if (! d_check_char (di
, 'Z'))
2671 function
= d_encoding (di
, 0);
2673 if (! d_check_char (di
, 'E'))
2676 if (d_peek_char (di
) == 's')
2679 if (! d_discriminator (di
))
2681 return d_make_comp (di
, DEMANGLE_COMPONENT_LOCAL_NAME
, function
,
2682 d_make_name (di
, "string literal",
2683 sizeof "string literal" - 1));
2687 struct demangle_component
*name
;
2690 if (! d_discriminator (di
))
2692 return d_make_comp (di
, DEMANGLE_COMPONENT_LOCAL_NAME
, function
, name
);
2696 /* <discriminator> ::= _ <(non-negative) number>
2698 We demangle the discriminator, but we don't print it out. FIXME:
2699 We should print it out in verbose mode. */
2702 d_discriminator (struct d_info
*di
)
2706 if (d_peek_char (di
) != '_')
2709 discrim
= d_number (di
);
2715 /* Add a new substitution. */
2718 d_add_substitution (struct d_info
*di
, struct demangle_component
*dc
)
2722 if (di
->next_sub
>= di
->num_subs
)
2724 di
->subs
[di
->next_sub
] = dc
;
2729 /* <substitution> ::= S <seq-id> _
2739 If PREFIX is non-zero, then this type is being used as a prefix in
2740 a qualified name. In this case, for the standard substitutions, we
2741 need to check whether we are being used as a prefix for a
2742 constructor or destructor, and return a full template name.
2743 Otherwise we will get something like std::iostream::~iostream()
2744 which does not correspond particularly well to any function which
2745 actually appears in the source.
2748 static const struct d_standard_sub_info standard_subs
[] =
2753 { 'a', NL ("std::allocator"),
2754 NL ("std::allocator"),
2756 { 'b', NL ("std::basic_string"),
2757 NL ("std::basic_string"),
2758 NL ("basic_string") },
2759 { 's', NL ("std::string"),
2760 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
2761 NL ("basic_string") },
2762 { 'i', NL ("std::istream"),
2763 NL ("std::basic_istream<char, std::char_traits<char> >"),
2764 NL ("basic_istream") },
2765 { 'o', NL ("std::ostream"),
2766 NL ("std::basic_ostream<char, std::char_traits<char> >"),
2767 NL ("basic_ostream") },
2768 { 'd', NL ("std::iostream"),
2769 NL ("std::basic_iostream<char, std::char_traits<char> >"),
2770 NL ("basic_iostream") }
2773 static struct demangle_component
*
2774 d_substitution (struct d_info
*di
, int prefix
)
2778 if (! d_check_char (di
, 'S'))
2781 c
= d_next_char (di
);
2782 if (c
== '_' || IS_DIGIT (c
) || IS_UPPER (c
))
2791 unsigned int new_id
;
2794 new_id
= id
* 36 + c
- '0';
2795 else if (IS_UPPER (c
))
2796 new_id
= id
* 36 + c
- 'A' + 10;
2802 c
= d_next_char (di
);
2809 if (id
>= (unsigned int) di
->next_sub
)
2814 return di
->subs
[id
];
2819 const struct d_standard_sub_info
*p
;
2820 const struct d_standard_sub_info
*pend
;
2822 verbose
= (di
->options
& DMGL_VERBOSE
) != 0;
2823 if (! verbose
&& prefix
)
2827 peek
= d_peek_char (di
);
2828 if (peek
== 'C' || peek
== 'D')
2832 pend
= (&standard_subs
[0]
2833 + sizeof standard_subs
/ sizeof standard_subs
[0]);
2834 for (p
= &standard_subs
[0]; p
< pend
; ++p
)
2841 if (p
->set_last_name
!= NULL
)
2842 di
->last_name
= d_make_sub (di
, p
->set_last_name
,
2843 p
->set_last_name_len
);
2846 s
= p
->full_expansion
;
2851 s
= p
->simple_expansion
;
2852 len
= p
->simple_len
;
2854 di
->expansion
+= len
;
2855 return d_make_sub (di
, s
, len
);
2863 /* Initialize a growable string. */
2866 d_growable_string_init (struct d_growable_string
*dgs
, size_t estimate
)
2871 dgs
->allocation_failure
= 0;
2874 d_growable_string_resize (dgs
, estimate
);
2877 /* Grow a growable string to a given size. */
2880 d_growable_string_resize (struct d_growable_string
*dgs
, size_t need
)
2885 if (dgs
->allocation_failure
)
2888 /* Start allocation at two bytes to avoid any possibility of confusion
2889 with the special value of 1 used as a return in *palc to indicate
2890 allocation failures. */
2891 newalc
= dgs
->alc
> 0 ? dgs
->alc
: 2;
2892 while (newalc
< need
)
2895 newbuf
= (char *) realloc (dgs
->buf
, newalc
);
2902 dgs
->allocation_failure
= 1;
2909 /* Append a buffer to a growable string. */
2912 d_growable_string_append_buffer (struct d_growable_string
*dgs
,
2913 const char *s
, size_t l
)
2917 need
= dgs
->len
+ l
+ 1;
2918 if (need
> dgs
->alc
)
2919 d_growable_string_resize (dgs
, need
);
2921 if (dgs
->allocation_failure
)
2924 memcpy (dgs
->buf
+ dgs
->len
, s
, l
);
2925 dgs
->buf
[dgs
->len
+ l
] = '\0';
2929 /* Bridge growable strings to the callback mechanism. */
2932 d_growable_string_callback_adapter (const char *s
, size_t l
, void *opaque
)
2934 struct d_growable_string
*dgs
= (struct d_growable_string
*) opaque
;
2936 d_growable_string_append_buffer (dgs
, s
, l
);
2939 /* Initialize a print information structure. */
2942 d_print_init (struct d_print_info
*dpi
, int options
,
2943 demangle_callbackref callback
, void *opaque
)
2945 dpi
->options
= options
;
2947 dpi
->last_char
= '\0';
2948 dpi
->templates
= NULL
;
2949 dpi
->modifiers
= NULL
;
2951 dpi
->callback
= callback
;
2952 dpi
->opaque
= opaque
;
2954 dpi
->demangle_failure
= 0;
2957 /* Indicate that an error occurred during printing, and test for error. */
2960 d_print_error (struct d_print_info
*dpi
)
2962 dpi
->demangle_failure
= 1;
2966 d_print_saw_error (struct d_print_info
*dpi
)
2968 return dpi
->demangle_failure
!= 0;
2971 /* Flush buffered characters to the callback. */
2974 d_print_flush (struct d_print_info
*dpi
)
2976 dpi
->buf
[dpi
->len
] = '\0';
2977 dpi
->callback (dpi
->buf
, dpi
->len
, dpi
->opaque
);
2981 /* Append characters and buffers for printing. */
2984 d_append_char (struct d_print_info
*dpi
, char c
)
2986 if (dpi
->len
== sizeof (dpi
->buf
) - 1)
2987 d_print_flush (dpi
);
2989 dpi
->buf
[dpi
->len
++] = c
;
2994 d_append_buffer (struct d_print_info
*dpi
, const char *s
, size_t l
)
2998 for (i
= 0; i
< l
; i
++)
2999 d_append_char (dpi
, s
[i
]);
3003 d_append_string (struct d_print_info
*dpi
, const char *s
)
3005 d_append_buffer (dpi
, s
, strlen (s
));
3009 d_last_char (struct d_print_info
*dpi
)
3011 return dpi
->last_char
;
3014 /* Turn components into a human readable string. OPTIONS is the
3015 options bits passed to the demangler. DC is the tree to print.
3016 CALLBACK is a function to call to flush demangled string segments
3017 as they fill the intermediate buffer, and OPAQUE is a generalized
3018 callback argument. On success, this returns 1. On failure,
3019 it returns 0, indicating a bad parse. It does not use heap
3020 memory to build an output string, so cannot encounter memory
3021 allocation failure. */
3023 CP_STATIC_IF_GLIBCPP_V3
3025 cplus_demangle_print_callback (int options
,
3026 const struct demangle_component
*dc
,
3027 demangle_callbackref callback
, void *opaque
)
3029 struct d_print_info dpi
;
3031 d_print_init (&dpi
, options
, callback
, opaque
);
3033 d_print_comp (&dpi
, dc
);
3035 d_print_flush (&dpi
);
3037 return ! d_print_saw_error (&dpi
);
3040 /* Turn components into a human readable string. OPTIONS is the
3041 options bits passed to the demangler. DC is the tree to print.
3042 ESTIMATE is a guess at the length of the result. This returns a
3043 string allocated by malloc, or NULL on error. On success, this
3044 sets *PALC to the size of the allocated buffer. On failure, this
3045 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
3048 CP_STATIC_IF_GLIBCPP_V3
3050 cplus_demangle_print (int options
, const struct demangle_component
*dc
,
3051 int estimate
, size_t *palc
)
3053 struct d_growable_string dgs
;
3055 d_growable_string_init (&dgs
, estimate
);
3057 if (! cplus_demangle_print_callback (options
, dc
,
3058 d_growable_string_callback_adapter
,
3066 *palc
= dgs
.allocation_failure
? 1 : dgs
.alc
;
3070 /* Subroutine to handle components. */
3073 d_print_comp (struct d_print_info
*dpi
,
3074 const struct demangle_component
*dc
)
3078 d_print_error (dpi
);
3081 if (d_print_saw_error (dpi
))
3086 case DEMANGLE_COMPONENT_NAME
:
3087 if ((dpi
->options
& DMGL_JAVA
) == 0)
3088 d_append_buffer (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
3090 d_print_java_identifier (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
3093 case DEMANGLE_COMPONENT_QUAL_NAME
:
3094 case DEMANGLE_COMPONENT_LOCAL_NAME
:
3095 d_print_comp (dpi
, d_left (dc
));
3096 if ((dpi
->options
& DMGL_JAVA
) == 0)
3097 d_append_string (dpi
, "::");
3099 d_append_char (dpi
, '.');
3100 d_print_comp (dpi
, d_right (dc
));
3103 case DEMANGLE_COMPONENT_TYPED_NAME
:
3105 struct d_print_mod
*hold_modifiers
;
3106 struct demangle_component
*typed_name
;
3107 struct d_print_mod adpm
[4];
3109 struct d_print_template dpt
;
3111 /* Pass the name down to the type so that it can be printed in
3112 the right place for the type. We also have to pass down
3113 any CV-qualifiers, which apply to the this parameter. */
3114 hold_modifiers
= dpi
->modifiers
;
3116 typed_name
= d_left (dc
);
3117 while (typed_name
!= NULL
)
3119 if (i
>= sizeof adpm
/ sizeof adpm
[0])
3121 d_print_error (dpi
);
3125 adpm
[i
].next
= dpi
->modifiers
;
3126 dpi
->modifiers
= &adpm
[i
];
3127 adpm
[i
].mod
= typed_name
;
3128 adpm
[i
].printed
= 0;
3129 adpm
[i
].templates
= dpi
->templates
;
3132 if (typed_name
->type
!= DEMANGLE_COMPONENT_RESTRICT_THIS
3133 && typed_name
->type
!= DEMANGLE_COMPONENT_VOLATILE_THIS
3134 && typed_name
->type
!= DEMANGLE_COMPONENT_CONST_THIS
)
3137 typed_name
= d_left (typed_name
);
3140 if (typed_name
== NULL
)
3142 d_print_error (dpi
);
3146 /* If typed_name is a template, then it applies to the
3147 function type as well. */
3148 if (typed_name
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
3150 dpt
.next
= dpi
->templates
;
3151 dpi
->templates
= &dpt
;
3152 dpt
.template_decl
= typed_name
;
3155 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
3156 there may be CV-qualifiers on its right argument which
3157 really apply here; this happens when parsing a class which
3158 is local to a function. */
3159 if (typed_name
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
3161 struct demangle_component
*local_name
;
3163 local_name
= d_right (typed_name
);
3164 while (local_name
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
3165 || local_name
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
3166 || local_name
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
3168 if (i
>= sizeof adpm
/ sizeof adpm
[0])
3170 d_print_error (dpi
);
3174 adpm
[i
] = adpm
[i
- 1];
3175 adpm
[i
].next
= &adpm
[i
- 1];
3176 dpi
->modifiers
= &adpm
[i
];
3178 adpm
[i
- 1].mod
= local_name
;
3179 adpm
[i
- 1].printed
= 0;
3180 adpm
[i
- 1].templates
= dpi
->templates
;
3183 local_name
= d_left (local_name
);
3187 d_print_comp (dpi
, d_right (dc
));
3189 if (typed_name
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
3190 dpi
->templates
= dpt
.next
;
3192 /* If the modifiers didn't get printed by the type, print them
3197 if (! adpm
[i
].printed
)
3199 d_append_char (dpi
, ' ');
3200 d_print_mod (dpi
, adpm
[i
].mod
);
3204 dpi
->modifiers
= hold_modifiers
;
3209 case DEMANGLE_COMPONENT_TEMPLATE
:
3211 struct d_print_mod
*hold_dpm
;
3212 struct demangle_component
*dcl
;
3214 /* Don't push modifiers into a template definition. Doing so
3215 could give the wrong definition for a template argument.
3216 Instead, treat the template essentially as a name. */
3218 hold_dpm
= dpi
->modifiers
;
3219 dpi
->modifiers
= NULL
;
3223 if ((dpi
->options
& DMGL_JAVA
) != 0
3224 && dcl
->type
== DEMANGLE_COMPONENT_NAME
3225 && dcl
->u
.s_name
.len
== 6
3226 && strncmp (dcl
->u
.s_name
.s
, "JArray", 6) == 0)
3228 /* Special-case Java arrays, so that JArray<TYPE> appears
3229 instead as TYPE[]. */
3231 d_print_comp (dpi
, d_right (dc
));
3232 d_append_string (dpi
, "[]");
3236 d_print_comp (dpi
, dcl
);
3237 if (d_last_char (dpi
) == '<')
3238 d_append_char (dpi
, ' ');
3239 d_append_char (dpi
, '<');
3240 d_print_comp (dpi
, d_right (dc
));
3241 /* Avoid generating two consecutive '>' characters, to avoid
3242 the C++ syntactic ambiguity. */
3243 if (d_last_char (dpi
) == '>')
3244 d_append_char (dpi
, ' ');
3245 d_append_char (dpi
, '>');
3248 dpi
->modifiers
= hold_dpm
;
3253 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
3256 struct demangle_component
*a
;
3257 struct d_print_template
*hold_dpt
;
3259 if (dpi
->templates
== NULL
)
3261 d_print_error (dpi
);
3264 i
= dc
->u
.s_number
.number
;
3265 for (a
= d_right (dpi
->templates
->template_decl
);
3269 if (a
->type
!= DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
3271 d_print_error (dpi
);
3278 if (i
!= 0 || a
== NULL
)
3280 d_print_error (dpi
);
3284 /* While processing this parameter, we need to pop the list of
3285 templates. This is because the template parameter may
3286 itself be a reference to a parameter of an outer
3289 hold_dpt
= dpi
->templates
;
3290 dpi
->templates
= hold_dpt
->next
;
3292 d_print_comp (dpi
, d_left (a
));
3294 dpi
->templates
= hold_dpt
;
3299 case DEMANGLE_COMPONENT_CTOR
:
3300 d_print_comp (dpi
, dc
->u
.s_ctor
.name
);
3303 case DEMANGLE_COMPONENT_DTOR
:
3304 d_append_char (dpi
, '~');
3305 d_print_comp (dpi
, dc
->u
.s_dtor
.name
);
3308 case DEMANGLE_COMPONENT_VTABLE
:
3309 d_append_string (dpi
, "vtable for ");
3310 d_print_comp (dpi
, d_left (dc
));
3313 case DEMANGLE_COMPONENT_VTT
:
3314 d_append_string (dpi
, "VTT for ");
3315 d_print_comp (dpi
, d_left (dc
));
3318 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
3319 d_append_string (dpi
, "construction vtable for ");
3320 d_print_comp (dpi
, d_left (dc
));
3321 d_append_string (dpi
, "-in-");
3322 d_print_comp (dpi
, d_right (dc
));
3325 case DEMANGLE_COMPONENT_TYPEINFO
:
3326 d_append_string (dpi
, "typeinfo for ");
3327 d_print_comp (dpi
, d_left (dc
));
3330 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
3331 d_append_string (dpi
, "typeinfo name for ");
3332 d_print_comp (dpi
, d_left (dc
));
3335 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
3336 d_append_string (dpi
, "typeinfo fn for ");
3337 d_print_comp (dpi
, d_left (dc
));
3340 case DEMANGLE_COMPONENT_THUNK
:
3341 d_append_string (dpi
, "non-virtual thunk to ");
3342 d_print_comp (dpi
, d_left (dc
));
3345 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
3346 d_append_string (dpi
, "virtual thunk to ");
3347 d_print_comp (dpi
, d_left (dc
));
3350 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
3351 d_append_string (dpi
, "covariant return thunk to ");
3352 d_print_comp (dpi
, d_left (dc
));
3355 case DEMANGLE_COMPONENT_JAVA_CLASS
:
3356 d_append_string (dpi
, "java Class for ");
3357 d_print_comp (dpi
, d_left (dc
));
3360 case DEMANGLE_COMPONENT_GUARD
:
3361 d_append_string (dpi
, "guard variable for ");
3362 d_print_comp (dpi
, d_left (dc
));
3365 case DEMANGLE_COMPONENT_REFTEMP
:
3366 d_append_string (dpi
, "reference temporary for ");
3367 d_print_comp (dpi
, d_left (dc
));
3370 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
3371 d_append_string (dpi
, "hidden alias for ");
3372 d_print_comp (dpi
, d_left (dc
));
3375 case DEMANGLE_COMPONENT_SUB_STD
:
3376 d_append_buffer (dpi
, dc
->u
.s_string
.string
, dc
->u
.s_string
.len
);
3379 case DEMANGLE_COMPONENT_RESTRICT
:
3380 case DEMANGLE_COMPONENT_VOLATILE
:
3381 case DEMANGLE_COMPONENT_CONST
:
3383 struct d_print_mod
*pdpm
;
3385 /* When printing arrays, it's possible to have cases where the
3386 same CV-qualifier gets pushed on the stack multiple times.
3387 We only need to print it once. */
3389 for (pdpm
= dpi
->modifiers
; pdpm
!= NULL
; pdpm
= pdpm
->next
)
3391 if (! pdpm
->printed
)
3393 if (pdpm
->mod
->type
!= DEMANGLE_COMPONENT_RESTRICT
3394 && pdpm
->mod
->type
!= DEMANGLE_COMPONENT_VOLATILE
3395 && pdpm
->mod
->type
!= DEMANGLE_COMPONENT_CONST
)
3397 if (pdpm
->mod
->type
== dc
->type
)
3399 d_print_comp (dpi
, d_left (dc
));
3406 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
3407 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
3408 case DEMANGLE_COMPONENT_CONST_THIS
:
3409 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
3410 case DEMANGLE_COMPONENT_POINTER
:
3411 case DEMANGLE_COMPONENT_REFERENCE
:
3412 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
3413 case DEMANGLE_COMPONENT_COMPLEX
:
3414 case DEMANGLE_COMPONENT_IMAGINARY
:
3416 /* We keep a list of modifiers on the stack. */
3417 struct d_print_mod dpm
;
3419 dpm
.next
= dpi
->modifiers
;
3420 dpi
->modifiers
= &dpm
;
3423 dpm
.templates
= dpi
->templates
;
3425 d_print_comp (dpi
, d_left (dc
));
3427 /* If the modifier didn't get printed by the type, print it
3430 d_print_mod (dpi
, dc
);
3432 dpi
->modifiers
= dpm
.next
;
3437 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
3438 if ((dpi
->options
& DMGL_JAVA
) == 0)
3439 d_append_buffer (dpi
, dc
->u
.s_builtin
.type
->name
,
3440 dc
->u
.s_builtin
.type
->len
);
3442 d_append_buffer (dpi
, dc
->u
.s_builtin
.type
->java_name
,
3443 dc
->u
.s_builtin
.type
->java_len
);
3446 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
3447 d_print_comp (dpi
, d_left (dc
));
3450 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
3452 if ((dpi
->options
& DMGL_RET_POSTFIX
) != 0)
3453 d_print_function_type (dpi
, dc
, dpi
->modifiers
);
3455 /* Print return type if present */
3456 if (d_left (dc
) != NULL
)
3458 struct d_print_mod dpm
;
3460 /* We must pass this type down as a modifier in order to
3461 print it in the right location. */
3462 dpm
.next
= dpi
->modifiers
;
3463 dpi
->modifiers
= &dpm
;
3466 dpm
.templates
= dpi
->templates
;
3468 d_print_comp (dpi
, d_left (dc
));
3470 dpi
->modifiers
= dpm
.next
;
3475 /* In standard prefix notation, there is a space between the
3476 return type and the function signature. */
3477 if ((dpi
->options
& DMGL_RET_POSTFIX
) == 0)
3478 d_append_char (dpi
, ' ');
3481 if ((dpi
->options
& DMGL_RET_POSTFIX
) == 0)
3482 d_print_function_type (dpi
, dc
, dpi
->modifiers
);
3487 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
3489 struct d_print_mod
*hold_modifiers
;
3490 struct d_print_mod adpm
[4];
3492 struct d_print_mod
*pdpm
;
3494 /* We must pass this type down as a modifier in order to print
3495 multi-dimensional arrays correctly. If the array itself is
3496 CV-qualified, we act as though the element type were
3497 CV-qualified. We do this by copying the modifiers down
3498 rather than fiddling pointers, so that we don't wind up
3499 with a d_print_mod higher on the stack pointing into our
3500 stack frame after we return. */
3502 hold_modifiers
= dpi
->modifiers
;
3504 adpm
[0].next
= hold_modifiers
;
3505 dpi
->modifiers
= &adpm
[0];
3507 adpm
[0].printed
= 0;
3508 adpm
[0].templates
= dpi
->templates
;
3511 pdpm
= hold_modifiers
;
3513 && (pdpm
->mod
->type
== DEMANGLE_COMPONENT_RESTRICT
3514 || pdpm
->mod
->type
== DEMANGLE_COMPONENT_VOLATILE
3515 || pdpm
->mod
->type
== DEMANGLE_COMPONENT_CONST
))
3517 if (! pdpm
->printed
)
3519 if (i
>= sizeof adpm
/ sizeof adpm
[0])
3521 d_print_error (dpi
);
3526 adpm
[i
].next
= dpi
->modifiers
;
3527 dpi
->modifiers
= &adpm
[i
];
3535 d_print_comp (dpi
, d_right (dc
));
3537 dpi
->modifiers
= hold_modifiers
;
3539 if (adpm
[0].printed
)
3545 d_print_mod (dpi
, adpm
[i
].mod
);
3548 d_print_array_type (dpi
, dc
, dpi
->modifiers
);
3553 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
3555 struct d_print_mod dpm
;
3557 dpm
.next
= dpi
->modifiers
;
3558 dpi
->modifiers
= &dpm
;
3561 dpm
.templates
= dpi
->templates
;
3563 d_print_comp (dpi
, d_right (dc
));
3565 /* If the modifier didn't get printed by the type, print it
3569 d_append_char (dpi
, ' ');
3570 d_print_comp (dpi
, d_left (dc
));
3571 d_append_string (dpi
, "::*");
3574 dpi
->modifiers
= dpm
.next
;
3579 case DEMANGLE_COMPONENT_ARGLIST
:
3580 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
3581 d_print_comp (dpi
, d_left (dc
));
3582 if (d_right (dc
) != NULL
)
3584 d_append_string (dpi
, ", ");
3585 d_print_comp (dpi
, d_right (dc
));
3589 case DEMANGLE_COMPONENT_OPERATOR
:
3593 d_append_string (dpi
, "operator");
3594 c
= dc
->u
.s_operator
.op
->name
[0];
3596 d_append_char (dpi
, ' ');
3597 d_append_buffer (dpi
, dc
->u
.s_operator
.op
->name
,
3598 dc
->u
.s_operator
.op
->len
);
3602 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
3603 d_append_string (dpi
, "operator ");
3604 d_print_comp (dpi
, dc
->u
.s_extended_operator
.name
);
3607 case DEMANGLE_COMPONENT_CAST
:
3608 d_append_string (dpi
, "operator ");
3609 d_print_cast (dpi
, dc
);
3612 case DEMANGLE_COMPONENT_UNARY
:
3613 if (d_left (dc
)->type
!= DEMANGLE_COMPONENT_CAST
)
3614 d_print_expr_op (dpi
, d_left (dc
));
3617 d_append_char (dpi
, '(');
3618 d_print_cast (dpi
, d_left (dc
));
3619 d_append_char (dpi
, ')');
3621 d_append_char (dpi
, '(');
3622 if (d_left (dc
)->type
!= DEMANGLE_COMPONENT_CAST
3623 || d_right (dc
)->type
!= DEMANGLE_COMPONENT_BUILTIN_TYPE
)
3624 d_print_comp (dpi
, d_right (dc
));
3625 d_append_char (dpi
, ')');
3628 case DEMANGLE_COMPONENT_BINARY
:
3629 if (d_right (dc
)->type
!= DEMANGLE_COMPONENT_BINARY_ARGS
)
3631 d_print_error (dpi
);
3635 if (!strcmp (d_left (dc
)->u
.s_operator
.op
->code
, "cl"))
3637 d_print_comp (dpi
, d_left (d_right (dc
)));
3638 d_append_string (dpi
, " (");
3639 d_print_comp (dpi
, d_right (d_right (dc
)));
3640 d_append_char (dpi
, ')');
3644 /* We wrap an expression which uses the greater-than operator in
3645 an extra layer of parens so that it does not get confused
3646 with the '>' which ends the template parameters. */
3647 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_OPERATOR
3648 && d_left (dc
)->u
.s_operator
.op
->len
== 1
3649 && d_left (dc
)->u
.s_operator
.op
->name
[0] == '>')
3650 d_append_char (dpi
, '(');
3652 d_append_char (dpi
, '(');
3653 d_print_comp (dpi
, d_left (d_right (dc
)));
3654 d_append_string (dpi
, ") ");
3655 d_print_expr_op (dpi
, d_left (dc
));
3656 d_append_string (dpi
, " (");
3657 d_print_comp (dpi
, d_right (d_right (dc
)));
3658 d_append_char (dpi
, ')');
3660 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_OPERATOR
3661 && d_left (dc
)->u
.s_operator
.op
->len
== 1
3662 && d_left (dc
)->u
.s_operator
.op
->name
[0] == '>')
3663 d_append_char (dpi
, ')');
3667 case DEMANGLE_COMPONENT_BINARY_ARGS
:
3668 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
3669 d_print_error (dpi
);
3672 case DEMANGLE_COMPONENT_TRINARY
:
3673 if (d_right (dc
)->type
!= DEMANGLE_COMPONENT_TRINARY_ARG1
3674 || d_right (d_right (dc
))->type
!= DEMANGLE_COMPONENT_TRINARY_ARG2
)
3676 d_print_error (dpi
);
3679 d_append_char (dpi
, '(');
3680 d_print_comp (dpi
, d_left (d_right (dc
)));
3681 d_append_string (dpi
, ") ");
3682 d_print_expr_op (dpi
, d_left (dc
));
3683 d_append_string (dpi
, " (");
3684 d_print_comp (dpi
, d_left (d_right (d_right (dc
))));
3685 d_append_string (dpi
, ") : (");
3686 d_print_comp (dpi
, d_right (d_right (d_right (dc
))));
3687 d_append_char (dpi
, ')');
3690 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
3691 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
3692 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
3693 d_print_error (dpi
);
3696 case DEMANGLE_COMPONENT_LITERAL
:
3697 case DEMANGLE_COMPONENT_LITERAL_NEG
:
3699 enum d_builtin_type_print tp
;
3701 /* For some builtin types, produce simpler output. */
3702 tp
= D_PRINT_DEFAULT
;
3703 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
)
3705 tp
= d_left (dc
)->u
.s_builtin
.type
->print
;
3709 case D_PRINT_UNSIGNED
:
3711 case D_PRINT_UNSIGNED_LONG
:
3712 case D_PRINT_LONG_LONG
:
3713 case D_PRINT_UNSIGNED_LONG_LONG
:
3714 if (d_right (dc
)->type
== DEMANGLE_COMPONENT_NAME
)
3716 if (dc
->type
== DEMANGLE_COMPONENT_LITERAL_NEG
)
3717 d_append_char (dpi
, '-');
3718 d_print_comp (dpi
, d_right (dc
));
3723 case D_PRINT_UNSIGNED
:
3724 d_append_char (dpi
, 'u');
3727 d_append_char (dpi
, 'l');
3729 case D_PRINT_UNSIGNED_LONG
:
3730 d_append_string (dpi
, "ul");
3732 case D_PRINT_LONG_LONG
:
3733 d_append_string (dpi
, "ll");
3735 case D_PRINT_UNSIGNED_LONG_LONG
:
3736 d_append_string (dpi
, "ull");
3744 if (d_right (dc
)->type
== DEMANGLE_COMPONENT_NAME
3745 && d_right (dc
)->u
.s_name
.len
== 1
3746 && dc
->type
== DEMANGLE_COMPONENT_LITERAL
)
3748 switch (d_right (dc
)->u
.s_name
.s
[0])
3751 d_append_string (dpi
, "false");
3754 d_append_string (dpi
, "true");
3767 d_append_char (dpi
, '(');
3768 d_print_comp (dpi
, d_left (dc
));
3769 d_append_char (dpi
, ')');
3770 if (dc
->type
== DEMANGLE_COMPONENT_LITERAL_NEG
)
3771 d_append_char (dpi
, '-');
3772 if (tp
== D_PRINT_FLOAT
)
3773 d_append_char (dpi
, '[');
3774 d_print_comp (dpi
, d_right (dc
));
3775 if (tp
== D_PRINT_FLOAT
)
3776 d_append_char (dpi
, ']');
3780 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
3781 d_append_string (dpi
, "java resource ");
3782 d_print_comp (dpi
, d_left (dc
));
3785 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
3786 d_print_comp (dpi
, d_left (dc
));
3787 d_print_comp (dpi
, d_right (dc
));
3790 case DEMANGLE_COMPONENT_CHARACTER
:
3791 d_append_char (dpi
, dc
->u
.s_character
.character
);
3794 case DEMANGLE_COMPONENT_DECLTYPE
:
3795 d_append_string (dpi
, "decltype (");
3796 d_print_comp (dpi
, d_left (dc
));
3797 d_append_char (dpi
, ')');
3801 d_print_error (dpi
);
3806 /* Print a Java dentifier. For Java we try to handle encoded extended
3807 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
3808 so we don't it for C++. Characters are encoded as
3812 d_print_java_identifier (struct d_print_info
*dpi
, const char *name
, int len
)
3818 for (p
= name
; p
< end
; ++p
)
3829 for (q
= p
+ 3; q
< end
; ++q
)
3835 else if (*q
>= 'A' && *q
<= 'F')
3836 dig
= *q
- 'A' + 10;
3837 else if (*q
>= 'a' && *q
<= 'f')
3838 dig
= *q
- 'a' + 10;
3844 /* If the Unicode character is larger than 256, we don't try
3845 to deal with it here. FIXME. */
3846 if (q
< end
&& *q
== '_' && c
< 256)
3848 d_append_char (dpi
, c
);
3854 d_append_char (dpi
, *p
);
3858 /* Print a list of modifiers. SUFFIX is 1 if we are printing
3859 qualifiers on this after printing a function. */
3862 d_print_mod_list (struct d_print_info
*dpi
,
3863 struct d_print_mod
*mods
, int suffix
)
3865 struct d_print_template
*hold_dpt
;
3867 if (mods
== NULL
|| d_print_saw_error (dpi
))
3872 && (mods
->mod
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
3873 || mods
->mod
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
3874 || mods
->mod
->type
== DEMANGLE_COMPONENT_CONST_THIS
)))
3876 d_print_mod_list (dpi
, mods
->next
, suffix
);
3882 hold_dpt
= dpi
->templates
;
3883 dpi
->templates
= mods
->templates
;
3885 if (mods
->mod
->type
== DEMANGLE_COMPONENT_FUNCTION_TYPE
)
3887 d_print_function_type (dpi
, mods
->mod
, mods
->next
);
3888 dpi
->templates
= hold_dpt
;
3891 else if (mods
->mod
->type
== DEMANGLE_COMPONENT_ARRAY_TYPE
)
3893 d_print_array_type (dpi
, mods
->mod
, mods
->next
);
3894 dpi
->templates
= hold_dpt
;
3897 else if (mods
->mod
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
3899 struct d_print_mod
*hold_modifiers
;
3900 struct demangle_component
*dc
;
3902 /* When this is on the modifier stack, we have pulled any
3903 qualifiers off the right argument already. Otherwise, we
3904 print it as usual, but don't let the left argument see any
3907 hold_modifiers
= dpi
->modifiers
;
3908 dpi
->modifiers
= NULL
;
3909 d_print_comp (dpi
, d_left (mods
->mod
));
3910 dpi
->modifiers
= hold_modifiers
;
3912 if ((dpi
->options
& DMGL_JAVA
) == 0)
3913 d_append_string (dpi
, "::");
3915 d_append_char (dpi
, '.');
3917 dc
= d_right (mods
->mod
);
3918 while (dc
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
3919 || dc
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
3920 || dc
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
3923 d_print_comp (dpi
, dc
);
3925 dpi
->templates
= hold_dpt
;
3929 d_print_mod (dpi
, mods
->mod
);
3931 dpi
->templates
= hold_dpt
;
3933 d_print_mod_list (dpi
, mods
->next
, suffix
);
3936 /* Print a modifier. */
3939 d_print_mod (struct d_print_info
*dpi
,
3940 const struct demangle_component
*mod
)
3944 case DEMANGLE_COMPONENT_RESTRICT
:
3945 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
3946 d_append_string (dpi
, " restrict");
3948 case DEMANGLE_COMPONENT_VOLATILE
:
3949 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
3950 d_append_string (dpi
, " volatile");
3952 case DEMANGLE_COMPONENT_CONST
:
3953 case DEMANGLE_COMPONENT_CONST_THIS
:
3954 d_append_string (dpi
, " const");
3956 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
3957 d_append_char (dpi
, ' ');
3958 d_print_comp (dpi
, d_right (mod
));
3960 case DEMANGLE_COMPONENT_POINTER
:
3961 /* There is no pointer symbol in Java. */
3962 if ((dpi
->options
& DMGL_JAVA
) == 0)
3963 d_append_char (dpi
, '*');
3965 case DEMANGLE_COMPONENT_REFERENCE
:
3966 d_append_char (dpi
, '&');
3968 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
3969 d_append_string (dpi
, "&&");
3971 case DEMANGLE_COMPONENT_COMPLEX
:
3972 d_append_string (dpi
, "complex ");
3974 case DEMANGLE_COMPONENT_IMAGINARY
:
3975 d_append_string (dpi
, "imaginary ");
3977 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
3978 if (d_last_char (dpi
) != '(')
3979 d_append_char (dpi
, ' ');
3980 d_print_comp (dpi
, d_left (mod
));
3981 d_append_string (dpi
, "::*");
3983 case DEMANGLE_COMPONENT_TYPED_NAME
:
3984 d_print_comp (dpi
, d_left (mod
));
3987 /* Otherwise, we have something that won't go back on the
3988 modifier stack, so we can just print it. */
3989 d_print_comp (dpi
, mod
);
3994 /* Print a function type, except for the return type. */
3997 d_print_function_type (struct d_print_info
*dpi
,
3998 const struct demangle_component
*dc
,
3999 struct d_print_mod
*mods
)
4004 struct d_print_mod
*p
;
4005 struct d_print_mod
*hold_modifiers
;
4010 for (p
= mods
; p
!= NULL
; p
= p
->next
)
4016 switch (p
->mod
->type
)
4018 case DEMANGLE_COMPONENT_POINTER
:
4019 case DEMANGLE_COMPONENT_REFERENCE
:
4020 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
4023 case DEMANGLE_COMPONENT_RESTRICT
:
4024 case DEMANGLE_COMPONENT_VOLATILE
:
4025 case DEMANGLE_COMPONENT_CONST
:
4026 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
4027 case DEMANGLE_COMPONENT_COMPLEX
:
4028 case DEMANGLE_COMPONENT_IMAGINARY
:
4029 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
4033 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
4034 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
4035 case DEMANGLE_COMPONENT_CONST_THIS
:
4044 if (d_left (dc
) != NULL
&& ! saw_mod
)
4051 if (d_last_char (dpi
) != '('
4052 && d_last_char (dpi
) != '*')
4055 if (need_space
&& d_last_char (dpi
) != ' ')
4056 d_append_char (dpi
, ' ');
4057 d_append_char (dpi
, '(');
4060 hold_modifiers
= dpi
->modifiers
;
4061 dpi
->modifiers
= NULL
;
4063 d_print_mod_list (dpi
, mods
, 0);
4066 d_append_char (dpi
, ')');
4068 d_append_char (dpi
, '(');
4070 if (d_right (dc
) != NULL
)
4071 d_print_comp (dpi
, d_right (dc
));
4073 d_append_char (dpi
, ')');
4075 d_print_mod_list (dpi
, mods
, 1);
4077 dpi
->modifiers
= hold_modifiers
;
4080 /* Print an array type, except for the element type. */
4083 d_print_array_type (struct d_print_info
*dpi
,
4084 const struct demangle_component
*dc
,
4085 struct d_print_mod
*mods
)
4093 struct d_print_mod
*p
;
4096 for (p
= mods
; p
!= NULL
; p
= p
->next
)
4100 if (p
->mod
->type
== DEMANGLE_COMPONENT_ARRAY_TYPE
)
4115 d_append_string (dpi
, " (");
4117 d_print_mod_list (dpi
, mods
, 0);
4120 d_append_char (dpi
, ')');
4124 d_append_char (dpi
, ' ');
4126 d_append_char (dpi
, '[');
4128 if (d_left (dc
) != NULL
)
4129 d_print_comp (dpi
, d_left (dc
));
4131 d_append_char (dpi
, ']');
4134 /* Print an operator in an expression. */
4137 d_print_expr_op (struct d_print_info
*dpi
,
4138 const struct demangle_component
*dc
)
4140 if (dc
->type
== DEMANGLE_COMPONENT_OPERATOR
)
4141 d_append_buffer (dpi
, dc
->u
.s_operator
.op
->name
,
4142 dc
->u
.s_operator
.op
->len
);
4144 d_print_comp (dpi
, dc
);
4150 d_print_cast (struct d_print_info
*dpi
,
4151 const struct demangle_component
*dc
)
4153 if (d_left (dc
)->type
!= DEMANGLE_COMPONENT_TEMPLATE
)
4154 d_print_comp (dpi
, d_left (dc
));
4157 struct d_print_mod
*hold_dpm
;
4158 struct d_print_template dpt
;
4160 /* It appears that for a templated cast operator, we need to put
4161 the template parameters in scope for the operator name, but
4162 not for the parameters. The effect is that we need to handle
4163 the template printing here. */
4165 hold_dpm
= dpi
->modifiers
;
4166 dpi
->modifiers
= NULL
;
4168 dpt
.next
= dpi
->templates
;
4169 dpi
->templates
= &dpt
;
4170 dpt
.template_decl
= d_left (dc
);
4172 d_print_comp (dpi
, d_left (d_left (dc
)));
4174 dpi
->templates
= dpt
.next
;
4176 if (d_last_char (dpi
) == '<')
4177 d_append_char (dpi
, ' ');
4178 d_append_char (dpi
, '<');
4179 d_print_comp (dpi
, d_right (d_left (dc
)));
4180 /* Avoid generating two consecutive '>' characters, to avoid
4181 the C++ syntactic ambiguity. */
4182 if (d_last_char (dpi
) == '>')
4183 d_append_char (dpi
, ' ');
4184 d_append_char (dpi
, '>');
4186 dpi
->modifiers
= hold_dpm
;
4190 /* Initialize the information structure we use to pass around
4193 CP_STATIC_IF_GLIBCPP_V3
4195 cplus_demangle_init_info (const char *mangled
, int options
, size_t len
,
4199 di
->send
= mangled
+ len
;
4200 di
->options
= options
;
4204 /* We can not need more components than twice the number of chars in
4205 the mangled string. Most components correspond directly to
4206 chars, but the ARGLIST types are exceptions. */
4207 di
->num_comps
= 2 * len
;
4210 /* Similarly, we can not need more substitutions than there are
4211 chars in the mangled string. */
4216 di
->last_name
= NULL
;
4221 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
4222 mangled name, return strings in repeated callback giving the demangled
4223 name. OPTIONS is the usual libiberty demangler options. On success,
4224 this returns 1. On failure, returns 0. */
4227 d_demangle_callback (const char *mangled
, int options
,
4228 demangle_callbackref callback
, void *opaque
)
4232 struct demangle_component
*dc
;
4235 if (mangled
[0] == '_' && mangled
[1] == 'Z')
4237 else if (strncmp (mangled
, "_GLOBAL_", 8) == 0
4238 && (mangled
[8] == '.' || mangled
[8] == '_' || mangled
[8] == '$')
4239 && (mangled
[9] == 'D' || mangled
[9] == 'I')
4240 && mangled
[10] == '_')
4244 intro
= (mangled
[9] == 'I')
4245 ? "global constructors keyed to "
4246 : "global destructors keyed to ";
4248 callback (intro
, strlen (intro
), opaque
);
4249 callback (mangled
+ 11, strlen (mangled
+ 11), opaque
);
4254 if ((options
& DMGL_TYPES
) == 0)
4259 cplus_demangle_init_info (mangled
, options
, strlen (mangled
), &di
);
4262 #ifdef CP_DYNAMIC_ARRAYS
4263 __extension__
struct demangle_component comps
[di
.num_comps
];
4264 __extension__
struct demangle_component
*subs
[di
.num_subs
];
4269 di
.comps
= alloca (di
.num_comps
* sizeof (*di
.comps
));
4270 di
.subs
= alloca (di
.num_subs
* sizeof (*di
.subs
));
4274 dc
= cplus_demangle_type (&di
);
4276 dc
= cplus_demangle_mangled_name (&di
, 1);
4278 /* If DMGL_PARAMS is set, then if we didn't consume the entire
4279 mangled string, then we didn't successfully demangle it. If
4280 DMGL_PARAMS is not set, we didn't look at the trailing
4282 if (((options
& DMGL_PARAMS
) != 0) && d_peek_char (&di
) != '\0')
4285 #ifdef CP_DEMANGLE_DEBUG
4289 status
= (dc
!= NULL
)
4290 ? cplus_demangle_print_callback (options
, dc
, callback
, opaque
)
4297 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
4298 name, return a buffer allocated with malloc holding the demangled
4299 name. OPTIONS is the usual libiberty demangler options. On
4300 success, this sets *PALC to the allocated size of the returned
4301 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
4302 a memory allocation failure, and returns NULL. */
4305 d_demangle (const char *mangled
, int options
, size_t *palc
)
4307 struct d_growable_string dgs
;
4310 d_growable_string_init (&dgs
, 0);
4312 status
= d_demangle_callback (mangled
, options
,
4313 d_growable_string_callback_adapter
, &dgs
);
4321 *palc
= dgs
.allocation_failure
? 1 : 0;
4325 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
4327 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
4329 /* ia64 ABI-mandated entry point in the C++ runtime library for
4330 performing demangling. MANGLED_NAME is a NUL-terminated character
4331 string containing the name to be demangled.
4333 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
4334 *LENGTH bytes, into which the demangled name is stored. If
4335 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
4336 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
4337 is placed in a region of memory allocated with malloc.
4339 If LENGTH is non-NULL, the length of the buffer containing the
4340 demangled name, is placed in *LENGTH.
4342 The return value is a pointer to the start of the NUL-terminated
4343 demangled name, or NULL if the demangling fails. The caller is
4344 responsible for deallocating this memory using free.
4346 *STATUS is set to one of the following values:
4347 0: The demangling operation succeeded.
4348 -1: A memory allocation failure occurred.
4349 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
4350 -3: One of the arguments is invalid.
4352 The demangling is performed using the C++ ABI mangling rules, with
4356 __cxa_demangle (const char *mangled_name
, char *output_buffer
,
4357 size_t *length
, int *status
)
4362 if (mangled_name
== NULL
)
4369 if (output_buffer
!= NULL
&& length
== NULL
)
4376 demangled
= d_demangle (mangled_name
, DMGL_PARAMS
| DMGL_TYPES
, &alc
);
4378 if (demangled
== NULL
)
4390 if (output_buffer
== NULL
)
4397 if (strlen (demangled
) < *length
)
4399 strcpy (output_buffer
, demangled
);
4401 demangled
= output_buffer
;
4405 free (output_buffer
);
4416 extern int __gcclibcxx_demangle_callback (const char *,
4418 (const char *, size_t, void *),
4421 /* Alternative, allocationless entry point in the C++ runtime library
4422 for performing demangling. MANGLED_NAME is a NUL-terminated character
4423 string containing the name to be demangled.
4425 CALLBACK is a callback function, called with demangled string
4426 segments as demangling progresses; it is called at least once,
4427 but may be called more than once. OPAQUE is a generalized pointer
4428 used as a callback argument.
4430 The return code is one of the following values, equivalent to
4431 the STATUS values of __cxa_demangle() (excluding -1, since this
4432 function performs no memory allocations):
4433 0: The demangling operation succeeded.
4434 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
4435 -3: One of the arguments is invalid.
4437 The demangling is performed using the C++ ABI mangling rules, with
4441 __gcclibcxx_demangle_callback (const char *mangled_name
,
4442 void (*callback
) (const char *, size_t, void *),
4447 if (mangled_name
== NULL
|| callback
== NULL
)
4450 status
= d_demangle_callback (mangled_name
, DMGL_PARAMS
| DMGL_TYPES
,
4458 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
4460 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
4461 mangled name, return a buffer allocated with malloc holding the
4462 demangled name. Otherwise, return NULL. */
4465 cplus_demangle_v3 (const char *mangled
, int options
)
4469 return d_demangle (mangled
, options
, &alc
);
4473 cplus_demangle_v3_callback (const char *mangled
, int options
,
4474 demangle_callbackref callback
, void *opaque
)
4476 return d_demangle_callback (mangled
, options
, callback
, opaque
);
4479 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
4480 conventions, but the output formatting is a little different.
4481 This instructs the C++ demangler not to emit pointer characters ("*"), to
4482 use Java's namespace separator symbol ("." instead of "::"), and to output
4483 JArray<TYPE> as TYPE[]. */
4486 java_demangle_v3 (const char *mangled
)
4490 return d_demangle (mangled
, DMGL_JAVA
| DMGL_PARAMS
| DMGL_RET_POSTFIX
, &alc
);
4494 java_demangle_v3_callback (const char *mangled
,
4495 demangle_callbackref callback
, void *opaque
)
4497 return d_demangle_callback (mangled
,
4498 DMGL_JAVA
| DMGL_PARAMS
| DMGL_RET_POSTFIX
,
4502 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
4504 #ifndef IN_GLIBCPP_V3
4506 /* Demangle a string in order to find out whether it is a constructor
4507 or destructor. Return non-zero on success. Set *CTOR_KIND and
4508 *DTOR_KIND appropriately. */
4511 is_ctor_or_dtor (const char *mangled
,
4512 enum gnu_v3_ctor_kinds
*ctor_kind
,
4513 enum gnu_v3_dtor_kinds
*dtor_kind
)
4516 struct demangle_component
*dc
;
4519 *ctor_kind
= (enum gnu_v3_ctor_kinds
) 0;
4520 *dtor_kind
= (enum gnu_v3_dtor_kinds
) 0;
4522 cplus_demangle_init_info (mangled
, DMGL_GNU_V3
, strlen (mangled
), &di
);
4525 #ifdef CP_DYNAMIC_ARRAYS
4526 __extension__
struct demangle_component comps
[di
.num_comps
];
4527 __extension__
struct demangle_component
*subs
[di
.num_subs
];
4532 di
.comps
= alloca (di
.num_comps
* sizeof (*di
.comps
));
4533 di
.subs
= alloca (di
.num_subs
* sizeof (*di
.subs
));
4536 dc
= cplus_demangle_mangled_name (&di
, 1);
4538 /* Note that because we did not pass DMGL_PARAMS, we don't expect
4539 to demangle the entire string. */
4549 case DEMANGLE_COMPONENT_TYPED_NAME
:
4550 case DEMANGLE_COMPONENT_TEMPLATE
:
4551 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
4552 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
4553 case DEMANGLE_COMPONENT_CONST_THIS
:
4556 case DEMANGLE_COMPONENT_QUAL_NAME
:
4557 case DEMANGLE_COMPONENT_LOCAL_NAME
:
4560 case DEMANGLE_COMPONENT_CTOR
:
4561 *ctor_kind
= dc
->u
.s_ctor
.kind
;
4565 case DEMANGLE_COMPONENT_DTOR
:
4566 *dtor_kind
= dc
->u
.s_dtor
.kind
;
4577 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
4578 name. A non-zero return indicates the type of constructor. */
4580 enum gnu_v3_ctor_kinds
4581 is_gnu_v3_mangled_ctor (const char *name
)
4583 enum gnu_v3_ctor_kinds ctor_kind
;
4584 enum gnu_v3_dtor_kinds dtor_kind
;
4586 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
4587 return (enum gnu_v3_ctor_kinds
) 0;
4592 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
4593 name. A non-zero return indicates the type of destructor. */
4595 enum gnu_v3_dtor_kinds
4596 is_gnu_v3_mangled_dtor (const char *name
)
4598 enum gnu_v3_ctor_kinds ctor_kind
;
4599 enum gnu_v3_dtor_kinds dtor_kind
;
4601 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
4602 return (enum gnu_v3_dtor_kinds
) 0;
4606 #endif /* IN_GLIBCPP_V3 */
4608 #ifdef STANDALONE_DEMANGLER
4611 #include "dyn-string.h"
4613 static void print_usage (FILE* fp
, int exit_value
);
4615 #define IS_ALPHA(CHAR) \
4616 (((CHAR) >= 'a' && (CHAR) <= 'z') \
4617 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
4619 /* Non-zero if CHAR is a character than can occur in a mangled name. */
4620 #define is_mangled_char(CHAR) \
4621 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
4622 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
4624 /* The name of this program, as invoked. */
4625 const char* program_name
;
4627 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
4630 print_usage (FILE* fp
, int exit_value
)
4632 fprintf (fp
, "Usage: %s [options] [names ...]\n", program_name
);
4633 fprintf (fp
, "Options:\n");
4634 fprintf (fp
, " -h,--help Display this message.\n");
4635 fprintf (fp
, " -p,--no-params Don't display function parameters\n");
4636 fprintf (fp
, " -v,--verbose Produce verbose demanglings.\n");
4637 fprintf (fp
, "If names are provided, they are demangled. Otherwise filters standard input.\n");
4642 /* Option specification for getopt_long. */
4643 static const struct option long_options
[] =
4645 { "help", no_argument
, NULL
, 'h' },
4646 { "no-params", no_argument
, NULL
, 'p' },
4647 { "verbose", no_argument
, NULL
, 'v' },
4648 { NULL
, no_argument
, NULL
, 0 },
4651 /* Main entry for a demangling filter executable. It will demangle
4652 its command line arguments, if any. If none are provided, it will
4653 filter stdin to stdout, replacing any recognized mangled C++ names
4654 with their demangled equivalents. */
4657 main (int argc
, char *argv
[])
4661 int options
= DMGL_PARAMS
| DMGL_ANSI
| DMGL_TYPES
;
4663 /* Use the program name of this program, as invoked. */
4664 program_name
= argv
[0];
4666 /* Parse options. */
4669 opt_char
= getopt_long (argc
, argv
, "hpv", long_options
, NULL
);
4672 case '?': /* Unrecognized option. */
4673 print_usage (stderr
, 1);
4677 print_usage (stdout
, 0);
4681 options
&= ~ DMGL_PARAMS
;
4685 options
|= DMGL_VERBOSE
;
4689 while (opt_char
!= -1);
4692 /* No command line arguments were provided. Filter stdin. */
4694 dyn_string_t mangled
= dyn_string_new (3);
4697 /* Read all of input. */
4698 while (!feof (stdin
))
4702 /* Pile characters into mangled until we hit one that can't
4703 occur in a mangled name. */
4705 while (!feof (stdin
) && is_mangled_char (c
))
4707 dyn_string_append_char (mangled
, c
);
4713 if (dyn_string_length (mangled
) > 0)
4715 #ifdef IN_GLIBCPP_V3
4716 s
= __cxa_demangle (dyn_string_buf (mangled
), NULL
, NULL
, NULL
);
4718 s
= cplus_demangle_v3 (dyn_string_buf (mangled
), options
);
4728 /* It might not have been a mangled name. Print the
4730 fputs (dyn_string_buf (mangled
), stdout
);
4733 dyn_string_clear (mangled
);
4736 /* If we haven't hit EOF yet, we've read one character that
4737 can't occur in a mangled name, so print it out. */
4742 dyn_string_delete (mangled
);
4745 /* Demangle command line arguments. */
4747 /* Loop over command line arguments. */
4748 for (i
= optind
; i
< argc
; ++i
)
4751 #ifdef IN_GLIBCPP_V3
4755 /* Attempt to demangle. */
4756 #ifdef IN_GLIBCPP_V3
4757 s
= __cxa_demangle (argv
[i
], NULL
, NULL
, &status
);
4759 s
= cplus_demangle_v3 (argv
[i
], options
);
4762 /* If it worked, print the demangled name. */
4770 #ifdef IN_GLIBCPP_V3
4771 fprintf (stderr
, "Failed: %s (status %d)\n", argv
[i
], status
);
4773 fprintf (stderr
, "Failed: %s\n", argv
[i
]);
4782 #endif /* STANDALONE_DEMANGLER */