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
;
302 /* The current index into any template argument packs we are using
307 #ifdef CP_DEMANGLE_DEBUG
308 static void d_dump (struct demangle_component
*, int);
311 static struct demangle_component
*
312 d_make_empty (struct d_info
*);
314 static struct demangle_component
*
315 d_make_comp (struct d_info
*, enum demangle_component_type
,
316 struct demangle_component
*,
317 struct demangle_component
*);
319 static struct demangle_component
*
320 d_make_name (struct d_info
*, const char *, int);
322 static struct demangle_component
*
323 d_make_builtin_type (struct d_info
*,
324 const struct demangle_builtin_type_info
*);
326 static struct demangle_component
*
327 d_make_operator (struct d_info
*,
328 const struct demangle_operator_info
*);
330 static struct demangle_component
*
331 d_make_extended_operator (struct d_info
*, int,
332 struct demangle_component
*);
334 static struct demangle_component
*
335 d_make_ctor (struct d_info
*, enum gnu_v3_ctor_kinds
,
336 struct demangle_component
*);
338 static struct demangle_component
*
339 d_make_dtor (struct d_info
*, enum gnu_v3_dtor_kinds
,
340 struct demangle_component
*);
342 static struct demangle_component
*
343 d_make_template_param (struct d_info
*, long);
345 static struct demangle_component
*
346 d_make_sub (struct d_info
*, const char *, int);
349 has_return_type (struct demangle_component
*);
352 is_ctor_dtor_or_conversion (struct demangle_component
*);
354 static struct demangle_component
*d_encoding (struct d_info
*, int);
356 static struct demangle_component
*d_name (struct d_info
*);
358 static struct demangle_component
*d_nested_name (struct d_info
*);
360 static struct demangle_component
*d_prefix (struct d_info
*);
362 static struct demangle_component
*d_unqualified_name (struct d_info
*);
364 static struct demangle_component
*d_source_name (struct d_info
*);
366 static long d_number (struct d_info
*);
368 static struct demangle_component
*d_identifier (struct d_info
*, int);
370 static struct demangle_component
*d_operator_name (struct d_info
*);
372 static struct demangle_component
*d_special_name (struct d_info
*);
374 static int d_call_offset (struct d_info
*, int);
376 static struct demangle_component
*d_ctor_dtor_name (struct d_info
*);
378 static struct demangle_component
**
379 d_cv_qualifiers (struct d_info
*, struct demangle_component
**, int);
381 static struct demangle_component
*
382 d_function_type (struct d_info
*);
384 static struct demangle_component
*
385 d_bare_function_type (struct d_info
*, int);
387 static struct demangle_component
*
388 d_class_enum_type (struct d_info
*);
390 static struct demangle_component
*d_array_type (struct d_info
*);
392 static struct demangle_component
*
393 d_pointer_to_member_type (struct d_info
*);
395 static struct demangle_component
*
396 d_template_param (struct d_info
*);
398 static struct demangle_component
*d_template_args (struct d_info
*);
400 static struct demangle_component
*
401 d_template_arg (struct d_info
*);
403 static struct demangle_component
*d_expression (struct d_info
*);
405 static struct demangle_component
*d_expr_primary (struct d_info
*);
407 static struct demangle_component
*d_local_name (struct d_info
*);
409 static int d_discriminator (struct d_info
*);
412 d_add_substitution (struct d_info
*, struct demangle_component
*);
414 static struct demangle_component
*d_substitution (struct d_info
*, int);
416 static void d_growable_string_init (struct d_growable_string
*, size_t);
419 d_growable_string_resize (struct d_growable_string
*, size_t);
422 d_growable_string_append_buffer (struct d_growable_string
*,
423 const char *, size_t);
425 d_growable_string_callback_adapter (const char *, size_t, void *);
428 d_print_init (struct d_print_info
*, int, demangle_callbackref
, void *);
430 static inline void d_print_error (struct d_print_info
*);
432 static inline int d_print_saw_error (struct d_print_info
*);
434 static inline void d_print_flush (struct d_print_info
*);
436 static inline void d_append_char (struct d_print_info
*, char);
438 static inline void d_append_buffer (struct d_print_info
*,
439 const char *, size_t);
441 static inline void d_append_string (struct d_print_info
*, const char *);
443 static inline char d_last_char (struct d_print_info
*);
446 d_print_comp (struct d_print_info
*, const struct demangle_component
*);
449 d_print_java_identifier (struct d_print_info
*, const char *, int);
452 d_print_mod_list (struct d_print_info
*, struct d_print_mod
*, int);
455 d_print_mod (struct d_print_info
*, const struct demangle_component
*);
458 d_print_function_type (struct d_print_info
*,
459 const struct demangle_component
*,
460 struct d_print_mod
*);
463 d_print_array_type (struct d_print_info
*,
464 const struct demangle_component
*,
465 struct d_print_mod
*);
468 d_print_expr_op (struct d_print_info
*, const struct demangle_component
*);
471 d_print_cast (struct d_print_info
*, const struct demangle_component
*);
473 static int d_demangle_callback (const char *, int,
474 demangle_callbackref
, void *);
475 static char *d_demangle (const char *, int, size_t *);
477 #ifdef CP_DEMANGLE_DEBUG
480 d_dump (struct demangle_component
*dc
, int indent
)
487 printf ("failed demangling\n");
491 for (i
= 0; i
< indent
; ++i
)
496 case DEMANGLE_COMPONENT_NAME
:
497 printf ("name '%.*s'\n", dc
->u
.s_name
.len
, dc
->u
.s_name
.s
);
499 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
500 printf ("template parameter %ld\n", dc
->u
.s_number
.number
);
502 case DEMANGLE_COMPONENT_CTOR
:
503 printf ("constructor %d\n", (int) dc
->u
.s_ctor
.kind
);
504 d_dump (dc
->u
.s_ctor
.name
, indent
+ 2);
506 case DEMANGLE_COMPONENT_DTOR
:
507 printf ("destructor %d\n", (int) dc
->u
.s_dtor
.kind
);
508 d_dump (dc
->u
.s_dtor
.name
, indent
+ 2);
510 case DEMANGLE_COMPONENT_SUB_STD
:
511 printf ("standard substitution %s\n", dc
->u
.s_string
.string
);
513 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
514 printf ("builtin type %s\n", dc
->u
.s_builtin
.type
->name
);
516 case DEMANGLE_COMPONENT_OPERATOR
:
517 printf ("operator %s\n", dc
->u
.s_operator
.op
->name
);
519 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
520 printf ("extended operator with %d args\n",
521 dc
->u
.s_extended_operator
.args
);
522 d_dump (dc
->u
.s_extended_operator
.name
, indent
+ 2);
525 case DEMANGLE_COMPONENT_QUAL_NAME
:
526 printf ("qualified name\n");
528 case DEMANGLE_COMPONENT_LOCAL_NAME
:
529 printf ("local name\n");
531 case DEMANGLE_COMPONENT_TYPED_NAME
:
532 printf ("typed name\n");
534 case DEMANGLE_COMPONENT_TEMPLATE
:
535 printf ("template\n");
537 case DEMANGLE_COMPONENT_VTABLE
:
540 case DEMANGLE_COMPONENT_VTT
:
543 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
544 printf ("construction vtable\n");
546 case DEMANGLE_COMPONENT_TYPEINFO
:
547 printf ("typeinfo\n");
549 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
550 printf ("typeinfo name\n");
552 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
553 printf ("typeinfo function\n");
555 case DEMANGLE_COMPONENT_THUNK
:
558 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
559 printf ("virtual thunk\n");
561 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
562 printf ("covariant thunk\n");
564 case DEMANGLE_COMPONENT_JAVA_CLASS
:
565 printf ("java class\n");
567 case DEMANGLE_COMPONENT_GUARD
:
570 case DEMANGLE_COMPONENT_REFTEMP
:
571 printf ("reference temporary\n");
573 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
574 printf ("hidden alias\n");
576 case DEMANGLE_COMPONENT_RESTRICT
:
577 printf ("restrict\n");
579 case DEMANGLE_COMPONENT_VOLATILE
:
580 printf ("volatile\n");
582 case DEMANGLE_COMPONENT_CONST
:
585 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
586 printf ("restrict this\n");
588 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
589 printf ("volatile this\n");
591 case DEMANGLE_COMPONENT_CONST_THIS
:
592 printf ("const this\n");
594 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
595 printf ("vendor type qualifier\n");
597 case DEMANGLE_COMPONENT_POINTER
:
598 printf ("pointer\n");
600 case DEMANGLE_COMPONENT_REFERENCE
:
601 printf ("reference\n");
603 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
604 printf ("rvalue reference\n");
606 case DEMANGLE_COMPONENT_COMPLEX
:
607 printf ("complex\n");
609 case DEMANGLE_COMPONENT_IMAGINARY
:
610 printf ("imaginary\n");
612 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
613 printf ("vendor type\n");
615 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
616 printf ("function type\n");
618 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
619 printf ("array type\n");
621 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
622 printf ("pointer to member type\n");
624 case DEMANGLE_COMPONENT_ARGLIST
:
625 printf ("argument list\n");
627 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
628 printf ("template argument list\n");
630 case DEMANGLE_COMPONENT_CAST
:
633 case DEMANGLE_COMPONENT_UNARY
:
634 printf ("unary operator\n");
636 case DEMANGLE_COMPONENT_BINARY
:
637 printf ("binary operator\n");
639 case DEMANGLE_COMPONENT_BINARY_ARGS
:
640 printf ("binary operator arguments\n");
642 case DEMANGLE_COMPONENT_TRINARY
:
643 printf ("trinary operator\n");
645 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
646 printf ("trinary operator arguments 1\n");
648 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
649 printf ("trinary operator arguments 1\n");
651 case DEMANGLE_COMPONENT_LITERAL
:
652 printf ("literal\n");
654 case DEMANGLE_COMPONENT_LITERAL_NEG
:
655 printf ("negative literal\n");
657 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
658 printf ("java resource\n");
660 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
661 printf ("compound name\n");
663 case DEMANGLE_COMPONENT_CHARACTER
:
664 printf ("character '%c'\n", dc
->u
.s_character
.character
);
666 case DEMANGLE_COMPONENT_DECLTYPE
:
667 printf ("decltype\n");
669 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
670 printf ("pack expansion\n");
674 d_dump (d_left (dc
), indent
+ 2);
675 d_dump (d_right (dc
), indent
+ 2);
678 #endif /* CP_DEMANGLE_DEBUG */
680 /* Fill in a DEMANGLE_COMPONENT_NAME. */
682 CP_STATIC_IF_GLIBCPP_V3
684 cplus_demangle_fill_name (struct demangle_component
*p
, const char *s
, int len
)
686 if (p
== NULL
|| s
== NULL
|| len
== 0)
688 p
->type
= DEMANGLE_COMPONENT_NAME
;
690 p
->u
.s_name
.len
= len
;
694 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
696 CP_STATIC_IF_GLIBCPP_V3
698 cplus_demangle_fill_extended_operator (struct demangle_component
*p
, int args
,
699 struct demangle_component
*name
)
701 if (p
== NULL
|| args
< 0 || name
== NULL
)
703 p
->type
= DEMANGLE_COMPONENT_EXTENDED_OPERATOR
;
704 p
->u
.s_extended_operator
.args
= args
;
705 p
->u
.s_extended_operator
.name
= name
;
709 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
711 CP_STATIC_IF_GLIBCPP_V3
713 cplus_demangle_fill_ctor (struct demangle_component
*p
,
714 enum gnu_v3_ctor_kinds kind
,
715 struct demangle_component
*name
)
719 || (kind
< gnu_v3_complete_object_ctor
720 && kind
> gnu_v3_complete_object_allocating_ctor
))
722 p
->type
= DEMANGLE_COMPONENT_CTOR
;
723 p
->u
.s_ctor
.kind
= kind
;
724 p
->u
.s_ctor
.name
= name
;
728 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
730 CP_STATIC_IF_GLIBCPP_V3
732 cplus_demangle_fill_dtor (struct demangle_component
*p
,
733 enum gnu_v3_dtor_kinds kind
,
734 struct demangle_component
*name
)
738 || (kind
< gnu_v3_deleting_dtor
739 && kind
> gnu_v3_base_object_dtor
))
741 p
->type
= DEMANGLE_COMPONENT_DTOR
;
742 p
->u
.s_dtor
.kind
= kind
;
743 p
->u
.s_dtor
.name
= name
;
747 /* Add a new component. */
749 static struct demangle_component
*
750 d_make_empty (struct d_info
*di
)
752 struct demangle_component
*p
;
754 if (di
->next_comp
>= di
->num_comps
)
756 p
= &di
->comps
[di
->next_comp
];
761 /* Add a new generic component. */
763 static struct demangle_component
*
764 d_make_comp (struct d_info
*di
, enum demangle_component_type type
,
765 struct demangle_component
*left
,
766 struct demangle_component
*right
)
768 struct demangle_component
*p
;
770 /* We check for errors here. A typical error would be a NULL return
771 from a subroutine. We catch those here, and return NULL
775 /* These types require two parameters. */
776 case DEMANGLE_COMPONENT_QUAL_NAME
:
777 case DEMANGLE_COMPONENT_LOCAL_NAME
:
778 case DEMANGLE_COMPONENT_TYPED_NAME
:
779 case DEMANGLE_COMPONENT_TEMPLATE
:
780 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
781 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
782 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
783 case DEMANGLE_COMPONENT_UNARY
:
784 case DEMANGLE_COMPONENT_BINARY
:
785 case DEMANGLE_COMPONENT_BINARY_ARGS
:
786 case DEMANGLE_COMPONENT_TRINARY
:
787 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
788 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
789 case DEMANGLE_COMPONENT_LITERAL
:
790 case DEMANGLE_COMPONENT_LITERAL_NEG
:
791 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
792 if (left
== NULL
|| right
== NULL
)
796 /* These types only require one parameter. */
797 case DEMANGLE_COMPONENT_VTABLE
:
798 case DEMANGLE_COMPONENT_VTT
:
799 case DEMANGLE_COMPONENT_TYPEINFO
:
800 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
801 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
802 case DEMANGLE_COMPONENT_THUNK
:
803 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
804 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
805 case DEMANGLE_COMPONENT_JAVA_CLASS
:
806 case DEMANGLE_COMPONENT_GUARD
:
807 case DEMANGLE_COMPONENT_REFTEMP
:
808 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
809 case DEMANGLE_COMPONENT_POINTER
:
810 case DEMANGLE_COMPONENT_REFERENCE
:
811 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
812 case DEMANGLE_COMPONENT_COMPLEX
:
813 case DEMANGLE_COMPONENT_IMAGINARY
:
814 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
815 case DEMANGLE_COMPONENT_CAST
:
816 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
817 case DEMANGLE_COMPONENT_DECLTYPE
:
818 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
823 /* This needs a right parameter, but the left parameter can be
825 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
830 /* These are allowed to have no parameters--in some cases they
831 will be filled in later. */
832 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
833 case DEMANGLE_COMPONENT_RESTRICT
:
834 case DEMANGLE_COMPONENT_VOLATILE
:
835 case DEMANGLE_COMPONENT_CONST
:
836 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
837 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
838 case DEMANGLE_COMPONENT_CONST_THIS
:
839 case DEMANGLE_COMPONENT_ARGLIST
:
840 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
843 /* Other types should not be seen here. */
848 p
= d_make_empty (di
);
852 p
->u
.s_binary
.left
= left
;
853 p
->u
.s_binary
.right
= right
;
858 /* Add a new name component. */
860 static struct demangle_component
*
861 d_make_name (struct d_info
*di
, const char *s
, int len
)
863 struct demangle_component
*p
;
865 p
= d_make_empty (di
);
866 if (! cplus_demangle_fill_name (p
, s
, len
))
871 /* Add a new builtin type component. */
873 static struct demangle_component
*
874 d_make_builtin_type (struct d_info
*di
,
875 const struct demangle_builtin_type_info
*type
)
877 struct demangle_component
*p
;
881 p
= d_make_empty (di
);
884 p
->type
= DEMANGLE_COMPONENT_BUILTIN_TYPE
;
885 p
->u
.s_builtin
.type
= type
;
890 /* Add a new operator component. */
892 static struct demangle_component
*
893 d_make_operator (struct d_info
*di
, const struct demangle_operator_info
*op
)
895 struct demangle_component
*p
;
897 p
= d_make_empty (di
);
900 p
->type
= DEMANGLE_COMPONENT_OPERATOR
;
901 p
->u
.s_operator
.op
= op
;
906 /* Add a new extended operator component. */
908 static struct demangle_component
*
909 d_make_extended_operator (struct d_info
*di
, int args
,
910 struct demangle_component
*name
)
912 struct demangle_component
*p
;
914 p
= d_make_empty (di
);
915 if (! cplus_demangle_fill_extended_operator (p
, args
, name
))
920 /* Add a new constructor component. */
922 static struct demangle_component
*
923 d_make_ctor (struct d_info
*di
, enum gnu_v3_ctor_kinds kind
,
924 struct demangle_component
*name
)
926 struct demangle_component
*p
;
928 p
= d_make_empty (di
);
929 if (! cplus_demangle_fill_ctor (p
, kind
, name
))
934 /* Add a new destructor component. */
936 static struct demangle_component
*
937 d_make_dtor (struct d_info
*di
, enum gnu_v3_dtor_kinds kind
,
938 struct demangle_component
*name
)
940 struct demangle_component
*p
;
942 p
= d_make_empty (di
);
943 if (! cplus_demangle_fill_dtor (p
, kind
, name
))
948 /* Add a new template parameter. */
950 static struct demangle_component
*
951 d_make_template_param (struct d_info
*di
, long i
)
953 struct demangle_component
*p
;
955 p
= d_make_empty (di
);
958 p
->type
= DEMANGLE_COMPONENT_TEMPLATE_PARAM
;
959 p
->u
.s_number
.number
= i
;
964 /* Add a new standard substitution component. */
966 static struct demangle_component
*
967 d_make_sub (struct d_info
*di
, const char *name
, int len
)
969 struct demangle_component
*p
;
971 p
= d_make_empty (di
);
974 p
->type
= DEMANGLE_COMPONENT_SUB_STD
;
975 p
->u
.s_string
.string
= name
;
976 p
->u
.s_string
.len
= len
;
981 /* <mangled-name> ::= _Z <encoding>
983 TOP_LEVEL is non-zero when called at the top level. */
985 CP_STATIC_IF_GLIBCPP_V3
986 struct demangle_component
*
987 cplus_demangle_mangled_name (struct d_info
*di
, int top_level
)
989 if (! d_check_char (di
, '_'))
991 if (! d_check_char (di
, 'Z'))
993 return d_encoding (di
, top_level
);
996 /* Return whether a function should have a return type. The argument
997 is the function name, which may be qualified in various ways. The
998 rules are that template functions have return types with some
999 exceptions, function types which are not part of a function name
1000 mangling have return types with some exceptions, and non-template
1001 function names do not have return types. The exceptions are that
1002 constructors, destructors, and conversion operators do not have
1006 has_return_type (struct demangle_component
*dc
)
1014 case DEMANGLE_COMPONENT_TEMPLATE
:
1015 return ! is_ctor_dtor_or_conversion (d_left (dc
));
1016 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
1017 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
1018 case DEMANGLE_COMPONENT_CONST_THIS
:
1019 return has_return_type (d_left (dc
));
1023 /* Return whether a name is a constructor, a destructor, or a
1024 conversion operator. */
1027 is_ctor_dtor_or_conversion (struct demangle_component
*dc
)
1035 case DEMANGLE_COMPONENT_QUAL_NAME
:
1036 case DEMANGLE_COMPONENT_LOCAL_NAME
:
1037 return is_ctor_dtor_or_conversion (d_right (dc
));
1038 case DEMANGLE_COMPONENT_CTOR
:
1039 case DEMANGLE_COMPONENT_DTOR
:
1040 case DEMANGLE_COMPONENT_CAST
:
1045 /* <encoding> ::= <(function) name> <bare-function-type>
1049 TOP_LEVEL is non-zero when called at the top level, in which case
1050 if DMGL_PARAMS is not set we do not demangle the function
1051 parameters. We only set this at the top level, because otherwise
1052 we would not correctly demangle names in local scopes. */
1054 static struct demangle_component
*
1055 d_encoding (struct d_info
*di
, int top_level
)
1057 char peek
= d_peek_char (di
);
1059 if (peek
== 'G' || peek
== 'T')
1060 return d_special_name (di
);
1063 struct demangle_component
*dc
;
1067 if (dc
!= NULL
&& top_level
&& (di
->options
& DMGL_PARAMS
) == 0)
1069 /* Strip off any initial CV-qualifiers, as they really apply
1070 to the `this' parameter, and they were not output by the
1071 v2 demangler without DMGL_PARAMS. */
1072 while (dc
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
1073 || dc
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
1074 || dc
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
1077 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1078 there may be CV-qualifiers on its right argument which
1079 really apply here; this happens when parsing a class
1080 which is local to a function. */
1081 if (dc
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
1083 struct demangle_component
*dcr
;
1086 while (dcr
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
1087 || dcr
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
1088 || dcr
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
1090 dc
->u
.s_binary
.right
= dcr
;
1096 peek
= d_peek_char (di
);
1097 if (dc
== NULL
|| peek
== '\0' || peek
== 'E')
1099 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPED_NAME
, dc
,
1100 d_bare_function_type (di
, has_return_type (dc
)));
1104 /* <name> ::= <nested-name>
1106 ::= <unscoped-template-name> <template-args>
1109 <unscoped-name> ::= <unqualified-name>
1110 ::= St <unqualified-name>
1112 <unscoped-template-name> ::= <unscoped-name>
1116 static struct demangle_component
*
1117 d_name (struct d_info
*di
)
1119 char peek
= d_peek_char (di
);
1120 struct demangle_component
*dc
;
1125 return d_nested_name (di
);
1128 return d_local_name (di
);
1131 return d_unqualified_name (di
);
1137 if (d_peek_next_char (di
) != 't')
1139 dc
= d_substitution (di
, 0);
1145 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
,
1146 d_make_name (di
, "std", 3),
1147 d_unqualified_name (di
));
1152 if (d_peek_char (di
) != 'I')
1154 /* The grammar does not permit this case to occur if we
1155 called d_substitution() above (i.e., subst == 1). We
1156 don't bother to check. */
1160 /* This is <template-args>, which means that we just saw
1161 <unscoped-template-name>, which is a substitution
1162 candidate if we didn't just get it from a
1166 if (! d_add_substitution (di
, dc
))
1169 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, dc
,
1170 d_template_args (di
));
1177 dc
= d_unqualified_name (di
);
1178 if (d_peek_char (di
) == 'I')
1180 /* This is <template-args>, which means that we just saw
1181 <unscoped-template-name>, which is a substitution
1183 if (! d_add_substitution (di
, dc
))
1185 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, dc
,
1186 d_template_args (di
));
1192 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1193 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1196 static struct demangle_component
*
1197 d_nested_name (struct d_info
*di
)
1199 struct demangle_component
*ret
;
1200 struct demangle_component
**pret
;
1202 if (! d_check_char (di
, 'N'))
1205 pret
= d_cv_qualifiers (di
, &ret
, 1);
1209 *pret
= d_prefix (di
);
1213 if (! d_check_char (di
, 'E'))
1219 /* <prefix> ::= <prefix> <unqualified-name>
1220 ::= <template-prefix> <template-args>
1221 ::= <template-param>
1225 <template-prefix> ::= <prefix> <(template) unqualified-name>
1226 ::= <template-param>
1230 static struct demangle_component
*
1231 d_prefix (struct d_info
*di
)
1233 struct demangle_component
*ret
= NULL
;
1238 enum demangle_component_type comb_type
;
1239 struct demangle_component
*dc
;
1241 peek
= d_peek_char (di
);
1245 /* The older code accepts a <local-name> here, but I don't see
1246 that in the grammar. The older code does not accept a
1247 <template-param> here. */
1249 comb_type
= DEMANGLE_COMPONENT_QUAL_NAME
;
1255 dc
= d_unqualified_name (di
);
1256 else if (peek
== 'S')
1257 dc
= d_substitution (di
, 1);
1258 else if (peek
== 'I')
1262 comb_type
= DEMANGLE_COMPONENT_TEMPLATE
;
1263 dc
= d_template_args (di
);
1265 else if (peek
== 'T')
1266 dc
= d_template_param (di
);
1267 else if (peek
== 'E')
1275 ret
= d_make_comp (di
, comb_type
, ret
, dc
);
1277 if (peek
!= 'S' && d_peek_char (di
) != 'E')
1279 if (! d_add_substitution (di
, ret
))
1285 /* <unqualified-name> ::= <operator-name>
1286 ::= <ctor-dtor-name>
1288 ::= <local-source-name>
1290 <local-source-name> ::= L <source-name> <discriminator>
1293 static struct demangle_component
*
1294 d_unqualified_name (struct d_info
*di
)
1298 peek
= d_peek_char (di
);
1299 if (IS_DIGIT (peek
))
1300 return d_source_name (di
);
1301 else if (IS_LOWER (peek
))
1303 struct demangle_component
*ret
;
1305 ret
= d_operator_name (di
);
1306 if (ret
!= NULL
&& ret
->type
== DEMANGLE_COMPONENT_OPERATOR
)
1307 di
->expansion
+= sizeof "operator" + ret
->u
.s_operator
.op
->len
- 2;
1310 else if (peek
== 'C' || peek
== 'D')
1311 return d_ctor_dtor_name (di
);
1312 else if (peek
== 'L')
1314 struct demangle_component
* ret
;
1318 ret
= d_source_name (di
);
1321 if (! d_discriminator (di
))
1329 /* <source-name> ::= <(positive length) number> <identifier> */
1331 static struct demangle_component
*
1332 d_source_name (struct d_info
*di
)
1335 struct demangle_component
*ret
;
1337 len
= d_number (di
);
1340 ret
= d_identifier (di
, len
);
1341 di
->last_name
= ret
;
1345 /* number ::= [n] <(non-negative decimal integer)> */
1348 d_number (struct d_info
*di
)
1355 peek
= d_peek_char (di
);
1360 peek
= d_peek_char (di
);
1366 if (! IS_DIGIT (peek
))
1372 ret
= ret
* 10 + peek
- '0';
1374 peek
= d_peek_char (di
);
1378 /* identifier ::= <(unqualified source code identifier)> */
1380 static struct demangle_component
*
1381 d_identifier (struct d_info
*di
, int len
)
1387 if (di
->send
- name
< len
)
1390 d_advance (di
, len
);
1392 /* A Java mangled name may have a trailing '$' if it is a C++
1393 keyword. This '$' is not included in the length count. We just
1395 if ((di
->options
& DMGL_JAVA
) != 0
1396 && d_peek_char (di
) == '$')
1399 /* Look for something which looks like a gcc encoding of an
1400 anonymous namespace, and replace it with a more user friendly
1402 if (len
>= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN
+ 2
1403 && memcmp (name
, ANONYMOUS_NAMESPACE_PREFIX
,
1404 ANONYMOUS_NAMESPACE_PREFIX_LEN
) == 0)
1408 s
= name
+ ANONYMOUS_NAMESPACE_PREFIX_LEN
;
1409 if ((*s
== '.' || *s
== '_' || *s
== '$')
1412 di
->expansion
-= len
- sizeof "(anonymous namespace)";
1413 return d_make_name (di
, "(anonymous namespace)",
1414 sizeof "(anonymous namespace)" - 1);
1418 return d_make_name (di
, name
, len
);
1421 /* operator_name ::= many different two character encodings.
1423 ::= v <digit> <source-name>
1426 #define NL(s) s, (sizeof s) - 1
1428 CP_STATIC_IF_GLIBCPP_V3
1429 const struct demangle_operator_info cplus_demangle_operators
[] =
1431 { "aN", NL ("&="), 2 },
1432 { "aS", NL ("="), 2 },
1433 { "aa", NL ("&&"), 2 },
1434 { "ad", NL ("&"), 1 },
1435 { "an", NL ("&"), 2 },
1436 { "cl", NL ("()"), 2 },
1437 { "cm", NL (","), 2 },
1438 { "co", NL ("~"), 1 },
1439 { "dV", NL ("/="), 2 },
1440 { "da", NL ("delete[]"), 1 },
1441 { "de", NL ("*"), 1 },
1442 { "dl", NL ("delete"), 1 },
1443 { "dt", NL ("."), 2 },
1444 { "dv", NL ("/"), 2 },
1445 { "eO", NL ("^="), 2 },
1446 { "eo", NL ("^"), 2 },
1447 { "eq", NL ("=="), 2 },
1448 { "ge", NL (">="), 2 },
1449 { "gt", NL (">"), 2 },
1450 { "ix", NL ("[]"), 2 },
1451 { "lS", NL ("<<="), 2 },
1452 { "le", NL ("<="), 2 },
1453 { "ls", NL ("<<"), 2 },
1454 { "lt", NL ("<"), 2 },
1455 { "mI", NL ("-="), 2 },
1456 { "mL", NL ("*="), 2 },
1457 { "mi", NL ("-"), 2 },
1458 { "ml", NL ("*"), 2 },
1459 { "mm", NL ("--"), 1 },
1460 { "na", NL ("new[]"), 1 },
1461 { "ne", NL ("!="), 2 },
1462 { "ng", NL ("-"), 1 },
1463 { "nt", NL ("!"), 1 },
1464 { "nw", NL ("new"), 1 },
1465 { "oR", NL ("|="), 2 },
1466 { "oo", NL ("||"), 2 },
1467 { "or", NL ("|"), 2 },
1468 { "pL", NL ("+="), 2 },
1469 { "pl", NL ("+"), 2 },
1470 { "pm", NL ("->*"), 2 },
1471 { "pp", NL ("++"), 1 },
1472 { "ps", NL ("+"), 1 },
1473 { "pt", NL ("->"), 2 },
1474 { "qu", NL ("?"), 3 },
1475 { "rM", NL ("%="), 2 },
1476 { "rS", NL (">>="), 2 },
1477 { "rm", NL ("%"), 2 },
1478 { "rs", NL (">>"), 2 },
1479 { "st", NL ("sizeof "), 1 },
1480 { "sz", NL ("sizeof "), 1 },
1481 { NULL
, NULL
, 0, 0 }
1484 static struct demangle_component
*
1485 d_operator_name (struct d_info
*di
)
1490 c1
= d_next_char (di
);
1491 c2
= d_next_char (di
);
1492 if (c1
== 'v' && IS_DIGIT (c2
))
1493 return d_make_extended_operator (di
, c2
- '0', d_source_name (di
));
1494 else if (c1
== 'c' && c2
== 'v')
1495 return d_make_comp (di
, DEMANGLE_COMPONENT_CAST
,
1496 cplus_demangle_type (di
), NULL
);
1499 /* LOW is the inclusive lower bound. */
1501 /* HIGH is the exclusive upper bound. We subtract one to ignore
1502 the sentinel at the end of the array. */
1503 int high
= ((sizeof (cplus_demangle_operators
)
1504 / sizeof (cplus_demangle_operators
[0]))
1510 const struct demangle_operator_info
*p
;
1512 i
= low
+ (high
- low
) / 2;
1513 p
= cplus_demangle_operators
+ i
;
1515 if (c1
== p
->code
[0] && c2
== p
->code
[1])
1516 return d_make_operator (di
, p
);
1518 if (c1
< p
->code
[0] || (c1
== p
->code
[0] && c2
< p
->code
[1]))
1528 static struct demangle_component
*
1529 d_make_character (struct d_info
*di
, int c
)
1531 struct demangle_component
*p
;
1532 p
= d_make_empty (di
);
1535 p
->type
= DEMANGLE_COMPONENT_CHARACTER
;
1536 p
->u
.s_character
.character
= c
;
1541 static struct demangle_component
*
1542 d_java_resource (struct d_info
*di
)
1544 struct demangle_component
*p
= NULL
;
1545 struct demangle_component
*next
= NULL
;
1550 len
= d_number (di
);
1554 /* Eat the leading '_'. */
1555 if (d_next_char (di
) != '_')
1568 /* Each chunk is either a '$' escape... */
1586 next
= d_make_character (di
, c
);
1594 /* ... or a sequence of characters. */
1597 while (i
< len
&& str
[i
] && str
[i
] != '$')
1600 next
= d_make_name (di
, str
, i
);
1613 p
= d_make_comp (di
, DEMANGLE_COMPONENT_COMPOUND_NAME
, p
, next
);
1619 p
= d_make_comp (di
, DEMANGLE_COMPONENT_JAVA_RESOURCE
, p
, NULL
);
1624 /* <special-name> ::= TV <type>
1628 ::= GV <(object) name>
1629 ::= T <call-offset> <(base) encoding>
1630 ::= Tc <call-offset> <call-offset> <(base) encoding>
1631 Also g++ extensions:
1632 ::= TC <type> <(offset) number> _ <(base) type>
1637 ::= Gr <resource name>
1640 static struct demangle_component
*
1641 d_special_name (struct d_info
*di
)
1643 di
->expansion
+= 20;
1644 if (d_check_char (di
, 'T'))
1646 switch (d_next_char (di
))
1650 return d_make_comp (di
, DEMANGLE_COMPONENT_VTABLE
,
1651 cplus_demangle_type (di
), NULL
);
1653 di
->expansion
-= 10;
1654 return d_make_comp (di
, DEMANGLE_COMPONENT_VTT
,
1655 cplus_demangle_type (di
), NULL
);
1657 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO
,
1658 cplus_demangle_type (di
), NULL
);
1660 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO_NAME
,
1661 cplus_demangle_type (di
), NULL
);
1664 if (! d_call_offset (di
, 'h'))
1666 return d_make_comp (di
, DEMANGLE_COMPONENT_THUNK
,
1667 d_encoding (di
, 0), NULL
);
1670 if (! d_call_offset (di
, 'v'))
1672 return d_make_comp (di
, DEMANGLE_COMPONENT_VIRTUAL_THUNK
,
1673 d_encoding (di
, 0), NULL
);
1676 if (! d_call_offset (di
, '\0'))
1678 if (! d_call_offset (di
, '\0'))
1680 return d_make_comp (di
, DEMANGLE_COMPONENT_COVARIANT_THUNK
,
1681 d_encoding (di
, 0), NULL
);
1685 struct demangle_component
*derived_type
;
1687 struct demangle_component
*base_type
;
1689 derived_type
= cplus_demangle_type (di
);
1690 offset
= d_number (di
);
1693 if (! d_check_char (di
, '_'))
1695 base_type
= cplus_demangle_type (di
);
1696 /* We don't display the offset. FIXME: We should display
1697 it in verbose mode. */
1699 return d_make_comp (di
, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
,
1700 base_type
, derived_type
);
1704 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO_FN
,
1705 cplus_demangle_type (di
), NULL
);
1707 return d_make_comp (di
, DEMANGLE_COMPONENT_JAVA_CLASS
,
1708 cplus_demangle_type (di
), NULL
);
1714 else if (d_check_char (di
, 'G'))
1716 switch (d_next_char (di
))
1719 return d_make_comp (di
, DEMANGLE_COMPONENT_GUARD
, d_name (di
), NULL
);
1722 return d_make_comp (di
, DEMANGLE_COMPONENT_REFTEMP
, d_name (di
),
1726 return d_make_comp (di
, DEMANGLE_COMPONENT_HIDDEN_ALIAS
,
1727 d_encoding (di
, 0), NULL
);
1730 return d_java_resource (di
);
1740 /* <call-offset> ::= h <nv-offset> _
1743 <nv-offset> ::= <(offset) number>
1745 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1747 The C parameter, if not '\0', is a character we just read which is
1748 the start of the <call-offset>.
1750 We don't display the offset information anywhere. FIXME: We should
1751 display it in verbose mode. */
1754 d_call_offset (struct d_info
*di
, int c
)
1757 c
= d_next_char (di
);
1764 if (! d_check_char (di
, '_'))
1771 if (! d_check_char (di
, '_'))
1777 /* <ctor-dtor-name> ::= C1
1785 static struct demangle_component
*
1786 d_ctor_dtor_name (struct d_info
*di
)
1788 if (di
->last_name
!= NULL
)
1790 if (di
->last_name
->type
== DEMANGLE_COMPONENT_NAME
)
1791 di
->expansion
+= di
->last_name
->u
.s_name
.len
;
1792 else if (di
->last_name
->type
== DEMANGLE_COMPONENT_SUB_STD
)
1793 di
->expansion
+= di
->last_name
->u
.s_string
.len
;
1795 switch (d_peek_char (di
))
1799 enum gnu_v3_ctor_kinds kind
;
1801 switch (d_peek_next_char (di
))
1804 kind
= gnu_v3_complete_object_ctor
;
1807 kind
= gnu_v3_base_object_ctor
;
1810 kind
= gnu_v3_complete_object_allocating_ctor
;
1816 return d_make_ctor (di
, kind
, di
->last_name
);
1821 enum gnu_v3_dtor_kinds kind
;
1823 switch (d_peek_next_char (di
))
1826 kind
= gnu_v3_deleting_dtor
;
1829 kind
= gnu_v3_complete_object_dtor
;
1832 kind
= gnu_v3_base_object_dtor
;
1838 return d_make_dtor (di
, kind
, di
->last_name
);
1846 /* <type> ::= <builtin-type>
1848 ::= <class-enum-type>
1850 ::= <pointer-to-member-type>
1851 ::= <template-param>
1852 ::= <template-template-param> <template-args>
1854 ::= <CV-qualifiers> <type>
1857 ::= O <type> (C++0x)
1860 ::= U <source-name> <type>
1862 <builtin-type> ::= various one letter codes
1866 CP_STATIC_IF_GLIBCPP_V3
1867 const struct demangle_builtin_type_info
1868 cplus_demangle_builtin_types
[D_BUILTIN_TYPE_COUNT
] =
1870 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT
},
1871 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL
},
1872 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT
},
1873 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT
},
1874 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT
},
1875 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT
},
1876 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT
},
1877 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT
},
1878 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT
},
1879 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED
},
1880 /* k */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
1881 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG
},
1882 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG
},
1883 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT
},
1884 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
1886 /* p */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
1887 /* q */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
1888 /* r */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
1889 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT
},
1890 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT
},
1891 /* u */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
1892 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID
},
1893 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT
},
1894 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG
},
1895 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
1896 D_PRINT_UNSIGNED_LONG_LONG
},
1897 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT
},
1898 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT
},
1899 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT
},
1900 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT
},
1901 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT
},
1902 /* 30 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT
},
1903 /* 31 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT
},
1906 CP_STATIC_IF_GLIBCPP_V3
1907 struct demangle_component
*
1908 cplus_demangle_type (struct d_info
*di
)
1911 struct demangle_component
*ret
;
1914 /* The ABI specifies that when CV-qualifiers are used, the base type
1915 is substitutable, and the fully qualified type is substitutable,
1916 but the base type with a strict subset of the CV-qualifiers is
1917 not substitutable. The natural recursive implementation of the
1918 CV-qualifiers would cause subsets to be substitutable, so instead
1919 we pull them all off now.
1921 FIXME: The ABI says that order-insensitive vendor qualifiers
1922 should be handled in the same way, but we have no way to tell
1923 which vendor qualifiers are order-insensitive and which are
1924 order-sensitive. So we just assume that they are all
1925 order-sensitive. g++ 3.4 supports only one vendor qualifier,
1926 __vector, and it treats it as order-sensitive when mangling
1929 peek
= d_peek_char (di
);
1930 if (peek
== 'r' || peek
== 'V' || peek
== 'K')
1932 struct demangle_component
**pret
;
1934 pret
= d_cv_qualifiers (di
, &ret
, 0);
1937 *pret
= cplus_demangle_type (di
);
1938 if (! *pret
|| ! d_add_substitution (di
, ret
))
1947 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1948 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
1949 case 'o': case 's': case 't':
1950 case 'v': case 'w': case 'x': case 'y': case 'z':
1951 ret
= d_make_builtin_type (di
,
1952 &cplus_demangle_builtin_types
[peek
- 'a']);
1953 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
1960 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_VENDOR_TYPE
,
1961 d_source_name (di
), NULL
);
1965 ret
= d_function_type (di
);
1968 case '0': case '1': case '2': case '3': case '4':
1969 case '5': case '6': case '7': case '8': case '9':
1972 ret
= d_class_enum_type (di
);
1976 ret
= d_array_type (di
);
1980 ret
= d_pointer_to_member_type (di
);
1984 ret
= d_template_param (di
);
1985 if (d_peek_char (di
) == 'I')
1987 /* This is <template-template-param> <template-args>. The
1988 <template-template-param> part is a substitution
1990 if (! d_add_substitution (di
, ret
))
1992 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
,
1993 d_template_args (di
));
1998 /* If this is a special substitution, then it is the start of
1999 <class-enum-type>. */
2003 peek_next
= d_peek_next_char (di
);
2004 if (IS_DIGIT (peek_next
)
2006 || IS_UPPER (peek_next
))
2008 ret
= d_substitution (di
, 0);
2009 /* The substituted name may have been a template name and
2010 may be followed by tepmlate args. */
2011 if (d_peek_char (di
) == 'I')
2012 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
,
2013 d_template_args (di
));
2019 ret
= d_class_enum_type (di
);
2020 /* If the substitution was a complete type, then it is not
2021 a new substitution candidate. However, if the
2022 substitution was followed by template arguments, then
2023 the whole thing is a substitution candidate. */
2024 if (ret
!= NULL
&& ret
->type
== DEMANGLE_COMPONENT_SUB_STD
)
2032 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_RVALUE_REFERENCE
,
2033 cplus_demangle_type (di
), NULL
);
2038 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_POINTER
,
2039 cplus_demangle_type (di
), NULL
);
2044 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_REFERENCE
,
2045 cplus_demangle_type (di
), NULL
);
2050 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_COMPLEX
,
2051 cplus_demangle_type (di
), NULL
);
2056 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_IMAGINARY
,
2057 cplus_demangle_type (di
), NULL
);
2062 ret
= d_source_name (di
);
2063 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
,
2064 cplus_demangle_type (di
), ret
);
2070 peek
= d_next_char (di
);
2075 /* decltype (expression) */
2076 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_DECLTYPE
,
2077 d_expression (di
), NULL
);
2078 if (ret
&& d_next_char (di
) != 'E')
2083 /* Pack expansion. */
2084 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_PACK_EXPANSION
,
2085 cplus_demangle_type (di
), NULL
);
2089 /* 32-bit decimal floating point */
2090 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[26]);
2091 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2095 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[27]);
2096 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2100 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[28]);
2101 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2104 /* 16-bit half-precision FP */
2105 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[29]);
2106 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2110 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[30]);
2111 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2115 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[31]);
2116 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2127 if (! d_add_substitution (di
, ret
))
2134 /* <CV-qualifiers> ::= [r] [V] [K] */
2136 static struct demangle_component
**
2137 d_cv_qualifiers (struct d_info
*di
,
2138 struct demangle_component
**pret
, int member_fn
)
2142 peek
= d_peek_char (di
);
2143 while (peek
== 'r' || peek
== 'V' || peek
== 'K')
2145 enum demangle_component_type t
;
2151 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2152 : DEMANGLE_COMPONENT_RESTRICT
);
2153 di
->expansion
+= sizeof "restrict";
2155 else if (peek
== 'V')
2158 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2159 : DEMANGLE_COMPONENT_VOLATILE
);
2160 di
->expansion
+= sizeof "volatile";
2165 ? DEMANGLE_COMPONENT_CONST_THIS
2166 : DEMANGLE_COMPONENT_CONST
);
2167 di
->expansion
+= sizeof "const";
2170 *pret
= d_make_comp (di
, t
, NULL
, NULL
);
2173 pret
= &d_left (*pret
);
2175 peek
= d_peek_char (di
);
2181 /* <function-type> ::= F [Y] <bare-function-type> E */
2183 static struct demangle_component
*
2184 d_function_type (struct d_info
*di
)
2186 struct demangle_component
*ret
;
2188 if (! d_check_char (di
, 'F'))
2190 if (d_peek_char (di
) == 'Y')
2192 /* Function has C linkage. We don't print this information.
2193 FIXME: We should print it in verbose mode. */
2196 ret
= d_bare_function_type (di
, 1);
2197 if (! d_check_char (di
, 'E'))
2202 /* <bare-function-type> ::= [J]<type>+ */
2204 static struct demangle_component
*
2205 d_bare_function_type (struct d_info
*di
, int has_return_type
)
2207 struct demangle_component
*return_type
;
2208 struct demangle_component
*tl
;
2209 struct demangle_component
**ptl
;
2212 /* Detect special qualifier indicating that the first argument
2213 is the return type. */
2214 peek
= d_peek_char (di
);
2218 has_return_type
= 1;
2226 struct demangle_component
*type
;
2228 peek
= d_peek_char (di
);
2229 if (peek
== '\0' || peek
== 'E')
2231 type
= cplus_demangle_type (di
);
2234 if (has_return_type
)
2237 has_return_type
= 0;
2241 *ptl
= d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, type
, NULL
);
2244 ptl
= &d_right (*ptl
);
2248 /* There should be at least one parameter type besides the optional
2249 return type. A function which takes no arguments will have a
2250 single parameter type void. */
2254 /* If we have a single parameter type void, omit it. */
2255 if (d_right (tl
) == NULL
2256 && d_left (tl
)->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
2257 && d_left (tl
)->u
.s_builtin
.type
->print
== D_PRINT_VOID
)
2259 di
->expansion
-= d_left (tl
)->u
.s_builtin
.type
->len
;
2263 return d_make_comp (di
, DEMANGLE_COMPONENT_FUNCTION_TYPE
, return_type
, tl
);
2266 /* <class-enum-type> ::= <name> */
2268 static struct demangle_component
*
2269 d_class_enum_type (struct d_info
*di
)
2274 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2275 ::= A [<(dimension) expression>] _ <(element) type>
2278 static struct demangle_component
*
2279 d_array_type (struct d_info
*di
)
2282 struct demangle_component
*dim
;
2284 if (! d_check_char (di
, 'A'))
2287 peek
= d_peek_char (di
);
2290 else if (IS_DIGIT (peek
))
2298 peek
= d_peek_char (di
);
2300 while (IS_DIGIT (peek
));
2301 dim
= d_make_name (di
, s
, d_str (di
) - s
);
2307 dim
= d_expression (di
);
2312 if (! d_check_char (di
, '_'))
2315 return d_make_comp (di
, DEMANGLE_COMPONENT_ARRAY_TYPE
, dim
,
2316 cplus_demangle_type (di
));
2319 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2321 static struct demangle_component
*
2322 d_pointer_to_member_type (struct d_info
*di
)
2324 struct demangle_component
*cl
;
2325 struct demangle_component
*mem
;
2326 struct demangle_component
**pmem
;
2328 if (! d_check_char (di
, 'M'))
2331 cl
= cplus_demangle_type (di
);
2333 /* The ABI specifies that any type can be a substitution source, and
2334 that M is followed by two types, and that when a CV-qualified
2335 type is seen both the base type and the CV-qualified types are
2336 substitution sources. The ABI also specifies that for a pointer
2337 to a CV-qualified member function, the qualifiers are attached to
2338 the second type. Given the grammar, a plain reading of the ABI
2339 suggests that both the CV-qualified member function and the
2340 non-qualified member function are substitution sources. However,
2341 g++ does not work that way. g++ treats only the CV-qualified
2342 member function as a substitution source. FIXME. So to work
2343 with g++, we need to pull off the CV-qualifiers here, in order to
2344 avoid calling add_substitution() in cplus_demangle_type(). But
2345 for a CV-qualified member which is not a function, g++ does
2346 follow the ABI, so we need to handle that case here by calling
2347 d_add_substitution ourselves. */
2349 pmem
= d_cv_qualifiers (di
, &mem
, 1);
2352 *pmem
= cplus_demangle_type (di
);
2356 if (pmem
!= &mem
&& (*pmem
)->type
!= DEMANGLE_COMPONENT_FUNCTION_TYPE
)
2358 if (! d_add_substitution (di
, mem
))
2362 return d_make_comp (di
, DEMANGLE_COMPONENT_PTRMEM_TYPE
, cl
, mem
);
2365 /* <template-param> ::= T_
2366 ::= T <(parameter-2 non-negative) number> _
2369 static struct demangle_component
*
2370 d_template_param (struct d_info
*di
)
2374 if (! d_check_char (di
, 'T'))
2377 if (d_peek_char (di
) == '_')
2381 param
= d_number (di
);
2387 if (! d_check_char (di
, '_'))
2392 return d_make_template_param (di
, param
);
2395 /* <template-args> ::= I <template-arg>+ E */
2397 static struct demangle_component
*
2398 d_template_args (struct d_info
*di
)
2400 struct demangle_component
*hold_last_name
;
2401 struct demangle_component
*al
;
2402 struct demangle_component
**pal
;
2404 /* Preserve the last name we saw--don't let the template arguments
2405 clobber it, as that would give us the wrong name for a subsequent
2406 constructor or destructor. */
2407 hold_last_name
= di
->last_name
;
2409 if (! d_check_char (di
, 'I'))
2412 if (d_peek_char (di
) == 'E')
2414 /* An argument pack can be empty. */
2416 return d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
, NULL
, NULL
);
2423 struct demangle_component
*a
;
2425 a
= d_template_arg (di
);
2429 *pal
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
, a
, NULL
);
2432 pal
= &d_right (*pal
);
2434 if (d_peek_char (di
) == 'E')
2441 di
->last_name
= hold_last_name
;
2446 /* <template-arg> ::= <type>
2447 ::= X <expression> E
2451 static struct demangle_component
*
2452 d_template_arg (struct d_info
*di
)
2454 struct demangle_component
*ret
;
2456 switch (d_peek_char (di
))
2460 ret
= d_expression (di
);
2461 if (! d_check_char (di
, 'E'))
2466 return d_expr_primary (di
);
2469 /* An argument pack. */
2470 return d_template_args (di
);
2473 return cplus_demangle_type (di
);
2477 /* Subroutine of <expression> ::= cl <expression>+ E */
2479 static struct demangle_component
*
2480 d_exprlist (struct d_info
*di
)
2482 struct demangle_component
*list
= NULL
;
2483 struct demangle_component
**p
= &list
;
2485 if (d_peek_char (di
) == 'E')
2488 return d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, NULL
, NULL
);
2493 struct demangle_component
*arg
= d_expression (di
);
2497 *p
= d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, arg
, NULL
);
2502 if (d_peek_char (di
) == 'E')
2512 /* <expression> ::= <(unary) operator-name> <expression>
2513 ::= <(binary) operator-name> <expression> <expression>
2514 ::= <(trinary) operator-name> <expression> <expression> <expression>
2515 ::= cl <expression>+ E
2517 ::= <template-param>
2518 ::= sr <type> <unqualified-name>
2519 ::= sr <type> <unqualified-name> <template-args>
2523 static struct demangle_component
*
2524 d_expression (struct d_info
*di
)
2528 peek
= d_peek_char (di
);
2530 return d_expr_primary (di
);
2531 else if (peek
== 'T')
2532 return d_template_param (di
);
2533 else if (peek
== 's' && d_peek_next_char (di
) == 'r')
2535 struct demangle_component
*type
;
2536 struct demangle_component
*name
;
2539 type
= cplus_demangle_type (di
);
2540 name
= d_unqualified_name (di
);
2541 if (d_peek_char (di
) != 'I')
2542 return d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
, type
, name
);
2544 return d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
, type
,
2545 d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, name
,
2546 d_template_args (di
)));
2548 else if (peek
== 's' && d_peek_next_char (di
) == 'T')
2550 /* Just demangle a parameter placeholder as its type. */
2552 return cplus_demangle_type (di
);
2554 else if (IS_DIGIT (peek
))
2556 /* We can get an unqualified name as an expression in the case of
2557 a dependent member access, i.e. decltype(T().i). */
2558 struct demangle_component
*name
= d_unqualified_name (di
);
2561 if (d_peek_char (di
) == 'I')
2562 return d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, name
,
2563 d_template_args (di
));
2569 struct demangle_component
*op
;
2572 op
= d_operator_name (di
);
2576 if (op
->type
== DEMANGLE_COMPONENT_OPERATOR
)
2577 di
->expansion
+= op
->u
.s_operator
.op
->len
- 2;
2579 if (op
->type
== DEMANGLE_COMPONENT_OPERATOR
2580 && strcmp (op
->u
.s_operator
.op
->code
, "st") == 0)
2581 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
2582 cplus_demangle_type (di
));
2588 case DEMANGLE_COMPONENT_OPERATOR
:
2589 args
= op
->u
.s_operator
.op
->args
;
2591 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
2592 args
= op
->u
.s_extended_operator
.args
;
2594 case DEMANGLE_COMPONENT_CAST
:
2595 if (d_peek_char (di
) == 'v')
2596 /* T() encoded as an operand of void. */
2597 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
2598 cplus_demangle_type (di
));
2607 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
2611 struct demangle_component
*left
;
2612 struct demangle_component
*right
;
2614 left
= d_expression (di
);
2615 if (!strcmp (op
->u
.s_operator
.op
->code
, "cl"))
2616 right
= d_exprlist (di
);
2618 right
= d_expression (di
);
2620 return d_make_comp (di
, DEMANGLE_COMPONENT_BINARY
, op
,
2622 DEMANGLE_COMPONENT_BINARY_ARGS
,
2627 struct demangle_component
*first
;
2628 struct demangle_component
*second
;
2630 first
= d_expression (di
);
2631 second
= d_expression (di
);
2632 return d_make_comp (di
, DEMANGLE_COMPONENT_TRINARY
, op
,
2634 DEMANGLE_COMPONENT_TRINARY_ARG1
,
2637 DEMANGLE_COMPONENT_TRINARY_ARG2
,
2639 d_expression (di
))));
2647 /* <expr-primary> ::= L <type> <(value) number> E
2648 ::= L <type> <(value) float> E
2649 ::= L <mangled-name> E
2652 static struct demangle_component
*
2653 d_expr_primary (struct d_info
*di
)
2655 struct demangle_component
*ret
;
2657 if (! d_check_char (di
, 'L'))
2659 if (d_peek_char (di
) == '_')
2660 ret
= cplus_demangle_mangled_name (di
, 0);
2663 struct demangle_component
*type
;
2664 enum demangle_component_type t
;
2667 type
= cplus_demangle_type (di
);
2671 /* If we have a type we know how to print, we aren't going to
2672 print the type name itself. */
2673 if (type
->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
2674 && type
->u
.s_builtin
.type
->print
!= D_PRINT_DEFAULT
)
2675 di
->expansion
-= type
->u
.s_builtin
.type
->len
;
2677 /* Rather than try to interpret the literal value, we just
2678 collect it as a string. Note that it's possible to have a
2679 floating point literal here. The ABI specifies that the
2680 format of such literals is machine independent. That's fine,
2681 but what's not fine is that versions of g++ up to 3.2 with
2682 -fabi-version=1 used upper case letters in the hex constant,
2683 and dumped out gcc's internal representation. That makes it
2684 hard to tell where the constant ends, and hard to dump the
2685 constant in any readable form anyhow. We don't attempt to
2686 handle these cases. */
2688 t
= DEMANGLE_COMPONENT_LITERAL
;
2689 if (d_peek_char (di
) == 'n')
2691 t
= DEMANGLE_COMPONENT_LITERAL_NEG
;
2695 while (d_peek_char (di
) != 'E')
2697 if (d_peek_char (di
) == '\0')
2701 ret
= d_make_comp (di
, t
, type
, d_make_name (di
, s
, d_str (di
) - s
));
2703 if (! d_check_char (di
, 'E'))
2708 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2709 ::= Z <(function) encoding> E s [<discriminator>]
2712 static struct demangle_component
*
2713 d_local_name (struct d_info
*di
)
2715 struct demangle_component
*function
;
2717 if (! d_check_char (di
, 'Z'))
2720 function
= d_encoding (di
, 0);
2722 if (! d_check_char (di
, 'E'))
2725 if (d_peek_char (di
) == 's')
2728 if (! d_discriminator (di
))
2730 return d_make_comp (di
, DEMANGLE_COMPONENT_LOCAL_NAME
, function
,
2731 d_make_name (di
, "string literal",
2732 sizeof "string literal" - 1));
2736 struct demangle_component
*name
;
2739 if (! d_discriminator (di
))
2741 return d_make_comp (di
, DEMANGLE_COMPONENT_LOCAL_NAME
, function
, name
);
2745 /* <discriminator> ::= _ <(non-negative) number>
2747 We demangle the discriminator, but we don't print it out. FIXME:
2748 We should print it out in verbose mode. */
2751 d_discriminator (struct d_info
*di
)
2755 if (d_peek_char (di
) != '_')
2758 discrim
= d_number (di
);
2764 /* Add a new substitution. */
2767 d_add_substitution (struct d_info
*di
, struct demangle_component
*dc
)
2771 if (di
->next_sub
>= di
->num_subs
)
2773 di
->subs
[di
->next_sub
] = dc
;
2778 /* <substitution> ::= S <seq-id> _
2788 If PREFIX is non-zero, then this type is being used as a prefix in
2789 a qualified name. In this case, for the standard substitutions, we
2790 need to check whether we are being used as a prefix for a
2791 constructor or destructor, and return a full template name.
2792 Otherwise we will get something like std::iostream::~iostream()
2793 which does not correspond particularly well to any function which
2794 actually appears in the source.
2797 static const struct d_standard_sub_info standard_subs
[] =
2802 { 'a', NL ("std::allocator"),
2803 NL ("std::allocator"),
2805 { 'b', NL ("std::basic_string"),
2806 NL ("std::basic_string"),
2807 NL ("basic_string") },
2808 { 's', NL ("std::string"),
2809 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
2810 NL ("basic_string") },
2811 { 'i', NL ("std::istream"),
2812 NL ("std::basic_istream<char, std::char_traits<char> >"),
2813 NL ("basic_istream") },
2814 { 'o', NL ("std::ostream"),
2815 NL ("std::basic_ostream<char, std::char_traits<char> >"),
2816 NL ("basic_ostream") },
2817 { 'd', NL ("std::iostream"),
2818 NL ("std::basic_iostream<char, std::char_traits<char> >"),
2819 NL ("basic_iostream") }
2822 static struct demangle_component
*
2823 d_substitution (struct d_info
*di
, int prefix
)
2827 if (! d_check_char (di
, 'S'))
2830 c
= d_next_char (di
);
2831 if (c
== '_' || IS_DIGIT (c
) || IS_UPPER (c
))
2840 unsigned int new_id
;
2843 new_id
= id
* 36 + c
- '0';
2844 else if (IS_UPPER (c
))
2845 new_id
= id
* 36 + c
- 'A' + 10;
2851 c
= d_next_char (di
);
2858 if (id
>= (unsigned int) di
->next_sub
)
2863 return di
->subs
[id
];
2868 const struct d_standard_sub_info
*p
;
2869 const struct d_standard_sub_info
*pend
;
2871 verbose
= (di
->options
& DMGL_VERBOSE
) != 0;
2872 if (! verbose
&& prefix
)
2876 peek
= d_peek_char (di
);
2877 if (peek
== 'C' || peek
== 'D')
2881 pend
= (&standard_subs
[0]
2882 + sizeof standard_subs
/ sizeof standard_subs
[0]);
2883 for (p
= &standard_subs
[0]; p
< pend
; ++p
)
2890 if (p
->set_last_name
!= NULL
)
2891 di
->last_name
= d_make_sub (di
, p
->set_last_name
,
2892 p
->set_last_name_len
);
2895 s
= p
->full_expansion
;
2900 s
= p
->simple_expansion
;
2901 len
= p
->simple_len
;
2903 di
->expansion
+= len
;
2904 return d_make_sub (di
, s
, len
);
2912 /* Initialize a growable string. */
2915 d_growable_string_init (struct d_growable_string
*dgs
, size_t estimate
)
2920 dgs
->allocation_failure
= 0;
2923 d_growable_string_resize (dgs
, estimate
);
2926 /* Grow a growable string to a given size. */
2929 d_growable_string_resize (struct d_growable_string
*dgs
, size_t need
)
2934 if (dgs
->allocation_failure
)
2937 /* Start allocation at two bytes to avoid any possibility of confusion
2938 with the special value of 1 used as a return in *palc to indicate
2939 allocation failures. */
2940 newalc
= dgs
->alc
> 0 ? dgs
->alc
: 2;
2941 while (newalc
< need
)
2944 newbuf
= (char *) realloc (dgs
->buf
, newalc
);
2951 dgs
->allocation_failure
= 1;
2958 /* Append a buffer to a growable string. */
2961 d_growable_string_append_buffer (struct d_growable_string
*dgs
,
2962 const char *s
, size_t l
)
2966 need
= dgs
->len
+ l
+ 1;
2967 if (need
> dgs
->alc
)
2968 d_growable_string_resize (dgs
, need
);
2970 if (dgs
->allocation_failure
)
2973 memcpy (dgs
->buf
+ dgs
->len
, s
, l
);
2974 dgs
->buf
[dgs
->len
+ l
] = '\0';
2978 /* Bridge growable strings to the callback mechanism. */
2981 d_growable_string_callback_adapter (const char *s
, size_t l
, void *opaque
)
2983 struct d_growable_string
*dgs
= (struct d_growable_string
*) opaque
;
2985 d_growable_string_append_buffer (dgs
, s
, l
);
2988 /* Initialize a print information structure. */
2991 d_print_init (struct d_print_info
*dpi
, int options
,
2992 demangle_callbackref callback
, void *opaque
)
2994 dpi
->options
= options
;
2996 dpi
->last_char
= '\0';
2997 dpi
->templates
= NULL
;
2998 dpi
->modifiers
= NULL
;
3000 dpi
->callback
= callback
;
3001 dpi
->opaque
= opaque
;
3003 dpi
->demangle_failure
= 0;
3006 /* Indicate that an error occurred during printing, and test for error. */
3009 d_print_error (struct d_print_info
*dpi
)
3011 dpi
->demangle_failure
= 1;
3015 d_print_saw_error (struct d_print_info
*dpi
)
3017 return dpi
->demangle_failure
!= 0;
3020 /* Flush buffered characters to the callback. */
3023 d_print_flush (struct d_print_info
*dpi
)
3025 dpi
->buf
[dpi
->len
] = '\0';
3026 dpi
->callback (dpi
->buf
, dpi
->len
, dpi
->opaque
);
3030 /* Append characters and buffers for printing. */
3033 d_append_char (struct d_print_info
*dpi
, char c
)
3035 if (dpi
->len
== sizeof (dpi
->buf
) - 1)
3036 d_print_flush (dpi
);
3038 dpi
->buf
[dpi
->len
++] = c
;
3043 d_append_buffer (struct d_print_info
*dpi
, const char *s
, size_t l
)
3047 for (i
= 0; i
< l
; i
++)
3048 d_append_char (dpi
, s
[i
]);
3052 d_append_string (struct d_print_info
*dpi
, const char *s
)
3054 d_append_buffer (dpi
, s
, strlen (s
));
3058 d_last_char (struct d_print_info
*dpi
)
3060 return dpi
->last_char
;
3063 /* Turn components into a human readable string. OPTIONS is the
3064 options bits passed to the demangler. DC is the tree to print.
3065 CALLBACK is a function to call to flush demangled string segments
3066 as they fill the intermediate buffer, and OPAQUE is a generalized
3067 callback argument. On success, this returns 1. On failure,
3068 it returns 0, indicating a bad parse. It does not use heap
3069 memory to build an output string, so cannot encounter memory
3070 allocation failure. */
3072 CP_STATIC_IF_GLIBCPP_V3
3074 cplus_demangle_print_callback (int options
,
3075 const struct demangle_component
*dc
,
3076 demangle_callbackref callback
, void *opaque
)
3078 struct d_print_info dpi
;
3080 d_print_init (&dpi
, options
, callback
, opaque
);
3082 d_print_comp (&dpi
, dc
);
3084 d_print_flush (&dpi
);
3086 return ! d_print_saw_error (&dpi
);
3089 /* Turn components into a human readable string. OPTIONS is the
3090 options bits passed to the demangler. DC is the tree to print.
3091 ESTIMATE is a guess at the length of the result. This returns a
3092 string allocated by malloc, or NULL on error. On success, this
3093 sets *PALC to the size of the allocated buffer. On failure, this
3094 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
3097 CP_STATIC_IF_GLIBCPP_V3
3099 cplus_demangle_print (int options
, const struct demangle_component
*dc
,
3100 int estimate
, size_t *palc
)
3102 struct d_growable_string dgs
;
3104 d_growable_string_init (&dgs
, estimate
);
3106 if (! cplus_demangle_print_callback (options
, dc
,
3107 d_growable_string_callback_adapter
,
3115 *palc
= dgs
.allocation_failure
? 1 : dgs
.alc
;
3119 /* Returns the I'th element of the template arglist ARGS, or NULL on
3122 static struct demangle_component
*
3123 d_index_template_argument (struct demangle_component
*args
, int i
)
3125 struct demangle_component
*a
;
3131 if (a
->type
!= DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
3137 if (i
!= 0 || a
== NULL
)
3143 /* Returns the template argument from the current context indicated by DC,
3144 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
3146 static struct demangle_component
*
3147 d_lookup_template_argument (struct d_print_info
*dpi
,
3148 const struct demangle_component
*dc
)
3150 if (dpi
->templates
== NULL
)
3152 d_print_error (dpi
);
3156 return d_index_template_argument
3157 (d_right (dpi
->templates
->template_decl
),
3158 dc
->u
.s_number
.number
);
3161 /* Returns a template argument pack used in DC (any will do), or NULL. */
3163 static struct demangle_component
*
3164 d_find_pack (struct d_print_info
*dpi
,
3165 const struct demangle_component
*dc
)
3167 struct demangle_component
*a
;
3173 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
3174 a
= d_lookup_template_argument (dpi
, dc
);
3175 if (a
&& a
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
3179 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
3182 case DEMANGLE_COMPONENT_NAME
:
3183 case DEMANGLE_COMPONENT_OPERATOR
:
3184 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
3185 case DEMANGLE_COMPONENT_SUB_STD
:
3186 case DEMANGLE_COMPONENT_CHARACTER
:
3189 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
3190 return d_find_pack (dpi
, dc
->u
.s_extended_operator
.name
);
3191 case DEMANGLE_COMPONENT_CTOR
:
3192 return d_find_pack (dpi
, dc
->u
.s_ctor
.name
);
3193 case DEMANGLE_COMPONENT_DTOR
:
3194 return d_find_pack (dpi
, dc
->u
.s_dtor
.name
);
3197 a
= d_find_pack (dpi
, d_left (dc
));
3200 return d_find_pack (dpi
, d_right (dc
));
3204 /* Returns the length of the template argument pack DC. */
3207 d_pack_length (const struct demangle_component
*dc
)
3210 while (dc
&& dc
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
3211 && d_left (dc
) != NULL
)
3219 /* DC is a component of a mangled expression. Print it, wrapped in parens
3223 d_print_subexpr (struct d_print_info
*dpi
,
3224 const struct demangle_component
*dc
)
3227 if (dc
->type
== DEMANGLE_COMPONENT_NAME
)
3230 d_append_char (dpi
, '(');
3231 d_print_comp (dpi
, dc
);
3233 d_append_char (dpi
, ')');
3236 /* Subroutine to handle components. */
3239 d_print_comp (struct d_print_info
*dpi
,
3240 const struct demangle_component
*dc
)
3244 d_print_error (dpi
);
3247 if (d_print_saw_error (dpi
))
3252 case DEMANGLE_COMPONENT_NAME
:
3253 if ((dpi
->options
& DMGL_JAVA
) == 0)
3254 d_append_buffer (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
3256 d_print_java_identifier (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
3259 case DEMANGLE_COMPONENT_QUAL_NAME
:
3260 case DEMANGLE_COMPONENT_LOCAL_NAME
:
3261 d_print_comp (dpi
, d_left (dc
));
3262 if ((dpi
->options
& DMGL_JAVA
) == 0)
3263 d_append_string (dpi
, "::");
3265 d_append_char (dpi
, '.');
3266 d_print_comp (dpi
, d_right (dc
));
3269 case DEMANGLE_COMPONENT_TYPED_NAME
:
3271 struct d_print_mod
*hold_modifiers
;
3272 struct demangle_component
*typed_name
;
3273 struct d_print_mod adpm
[4];
3275 struct d_print_template dpt
;
3277 /* Pass the name down to the type so that it can be printed in
3278 the right place for the type. We also have to pass down
3279 any CV-qualifiers, which apply to the this parameter. */
3280 hold_modifiers
= dpi
->modifiers
;
3282 typed_name
= d_left (dc
);
3283 while (typed_name
!= NULL
)
3285 if (i
>= sizeof adpm
/ sizeof adpm
[0])
3287 d_print_error (dpi
);
3291 adpm
[i
].next
= dpi
->modifiers
;
3292 dpi
->modifiers
= &adpm
[i
];
3293 adpm
[i
].mod
= typed_name
;
3294 adpm
[i
].printed
= 0;
3295 adpm
[i
].templates
= dpi
->templates
;
3298 if (typed_name
->type
!= DEMANGLE_COMPONENT_RESTRICT_THIS
3299 && typed_name
->type
!= DEMANGLE_COMPONENT_VOLATILE_THIS
3300 && typed_name
->type
!= DEMANGLE_COMPONENT_CONST_THIS
)
3303 typed_name
= d_left (typed_name
);
3306 if (typed_name
== NULL
)
3308 d_print_error (dpi
);
3312 /* If typed_name is a template, then it applies to the
3313 function type as well. */
3314 if (typed_name
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
3316 dpt
.next
= dpi
->templates
;
3317 dpi
->templates
= &dpt
;
3318 dpt
.template_decl
= typed_name
;
3321 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
3322 there may be CV-qualifiers on its right argument which
3323 really apply here; this happens when parsing a class which
3324 is local to a function. */
3325 if (typed_name
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
3327 struct demangle_component
*local_name
;
3329 local_name
= d_right (typed_name
);
3330 while (local_name
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
3331 || local_name
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
3332 || local_name
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
3334 if (i
>= sizeof adpm
/ sizeof adpm
[0])
3336 d_print_error (dpi
);
3340 adpm
[i
] = adpm
[i
- 1];
3341 adpm
[i
].next
= &adpm
[i
- 1];
3342 dpi
->modifiers
= &adpm
[i
];
3344 adpm
[i
- 1].mod
= local_name
;
3345 adpm
[i
- 1].printed
= 0;
3346 adpm
[i
- 1].templates
= dpi
->templates
;
3349 local_name
= d_left (local_name
);
3353 d_print_comp (dpi
, d_right (dc
));
3355 if (typed_name
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
3356 dpi
->templates
= dpt
.next
;
3358 /* If the modifiers didn't get printed by the type, print them
3363 if (! adpm
[i
].printed
)
3365 d_append_char (dpi
, ' ');
3366 d_print_mod (dpi
, adpm
[i
].mod
);
3370 dpi
->modifiers
= hold_modifiers
;
3375 case DEMANGLE_COMPONENT_TEMPLATE
:
3377 struct d_print_mod
*hold_dpm
;
3378 struct demangle_component
*dcl
;
3380 /* Don't push modifiers into a template definition. Doing so
3381 could give the wrong definition for a template argument.
3382 Instead, treat the template essentially as a name. */
3384 hold_dpm
= dpi
->modifiers
;
3385 dpi
->modifiers
= NULL
;
3389 if ((dpi
->options
& DMGL_JAVA
) != 0
3390 && dcl
->type
== DEMANGLE_COMPONENT_NAME
3391 && dcl
->u
.s_name
.len
== 6
3392 && strncmp (dcl
->u
.s_name
.s
, "JArray", 6) == 0)
3394 /* Special-case Java arrays, so that JArray<TYPE> appears
3395 instead as TYPE[]. */
3397 d_print_comp (dpi
, d_right (dc
));
3398 d_append_string (dpi
, "[]");
3402 d_print_comp (dpi
, dcl
);
3403 if (d_last_char (dpi
) == '<')
3404 d_append_char (dpi
, ' ');
3405 d_append_char (dpi
, '<');
3406 d_print_comp (dpi
, d_right (dc
));
3407 /* Avoid generating two consecutive '>' characters, to avoid
3408 the C++ syntactic ambiguity. */
3409 if (d_last_char (dpi
) == '>')
3410 d_append_char (dpi
, ' ');
3411 d_append_char (dpi
, '>');
3414 dpi
->modifiers
= hold_dpm
;
3419 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
3421 struct d_print_template
*hold_dpt
;
3422 struct demangle_component
*a
= d_lookup_template_argument (dpi
, dc
);
3424 if (a
&& a
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
3425 a
= d_index_template_argument (a
, dpi
->pack_index
);
3429 d_print_error (dpi
);
3433 /* While processing this parameter, we need to pop the list of
3434 templates. This is because the template parameter may
3435 itself be a reference to a parameter of an outer
3438 hold_dpt
= dpi
->templates
;
3439 dpi
->templates
= hold_dpt
->next
;
3441 d_print_comp (dpi
, a
);
3443 dpi
->templates
= hold_dpt
;
3448 case DEMANGLE_COMPONENT_CTOR
:
3449 d_print_comp (dpi
, dc
->u
.s_ctor
.name
);
3452 case DEMANGLE_COMPONENT_DTOR
:
3453 d_append_char (dpi
, '~');
3454 d_print_comp (dpi
, dc
->u
.s_dtor
.name
);
3457 case DEMANGLE_COMPONENT_VTABLE
:
3458 d_append_string (dpi
, "vtable for ");
3459 d_print_comp (dpi
, d_left (dc
));
3462 case DEMANGLE_COMPONENT_VTT
:
3463 d_append_string (dpi
, "VTT for ");
3464 d_print_comp (dpi
, d_left (dc
));
3467 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
3468 d_append_string (dpi
, "construction vtable for ");
3469 d_print_comp (dpi
, d_left (dc
));
3470 d_append_string (dpi
, "-in-");
3471 d_print_comp (dpi
, d_right (dc
));
3474 case DEMANGLE_COMPONENT_TYPEINFO
:
3475 d_append_string (dpi
, "typeinfo for ");
3476 d_print_comp (dpi
, d_left (dc
));
3479 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
3480 d_append_string (dpi
, "typeinfo name for ");
3481 d_print_comp (dpi
, d_left (dc
));
3484 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
3485 d_append_string (dpi
, "typeinfo fn for ");
3486 d_print_comp (dpi
, d_left (dc
));
3489 case DEMANGLE_COMPONENT_THUNK
:
3490 d_append_string (dpi
, "non-virtual thunk to ");
3491 d_print_comp (dpi
, d_left (dc
));
3494 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
3495 d_append_string (dpi
, "virtual thunk to ");
3496 d_print_comp (dpi
, d_left (dc
));
3499 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
3500 d_append_string (dpi
, "covariant return thunk to ");
3501 d_print_comp (dpi
, d_left (dc
));
3504 case DEMANGLE_COMPONENT_JAVA_CLASS
:
3505 d_append_string (dpi
, "java Class for ");
3506 d_print_comp (dpi
, d_left (dc
));
3509 case DEMANGLE_COMPONENT_GUARD
:
3510 d_append_string (dpi
, "guard variable for ");
3511 d_print_comp (dpi
, d_left (dc
));
3514 case DEMANGLE_COMPONENT_REFTEMP
:
3515 d_append_string (dpi
, "reference temporary for ");
3516 d_print_comp (dpi
, d_left (dc
));
3519 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
3520 d_append_string (dpi
, "hidden alias for ");
3521 d_print_comp (dpi
, d_left (dc
));
3524 case DEMANGLE_COMPONENT_SUB_STD
:
3525 d_append_buffer (dpi
, dc
->u
.s_string
.string
, dc
->u
.s_string
.len
);
3528 case DEMANGLE_COMPONENT_RESTRICT
:
3529 case DEMANGLE_COMPONENT_VOLATILE
:
3530 case DEMANGLE_COMPONENT_CONST
:
3532 struct d_print_mod
*pdpm
;
3534 /* When printing arrays, it's possible to have cases where the
3535 same CV-qualifier gets pushed on the stack multiple times.
3536 We only need to print it once. */
3538 for (pdpm
= dpi
->modifiers
; pdpm
!= NULL
; pdpm
= pdpm
->next
)
3540 if (! pdpm
->printed
)
3542 if (pdpm
->mod
->type
!= DEMANGLE_COMPONENT_RESTRICT
3543 && pdpm
->mod
->type
!= DEMANGLE_COMPONENT_VOLATILE
3544 && pdpm
->mod
->type
!= DEMANGLE_COMPONENT_CONST
)
3546 if (pdpm
->mod
->type
== dc
->type
)
3548 d_print_comp (dpi
, d_left (dc
));
3555 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
3556 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
3557 case DEMANGLE_COMPONENT_CONST_THIS
:
3558 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
3559 case DEMANGLE_COMPONENT_POINTER
:
3560 case DEMANGLE_COMPONENT_REFERENCE
:
3561 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
3562 case DEMANGLE_COMPONENT_COMPLEX
:
3563 case DEMANGLE_COMPONENT_IMAGINARY
:
3565 /* We keep a list of modifiers on the stack. */
3566 struct d_print_mod dpm
;
3568 dpm
.next
= dpi
->modifiers
;
3569 dpi
->modifiers
= &dpm
;
3572 dpm
.templates
= dpi
->templates
;
3574 d_print_comp (dpi
, d_left (dc
));
3576 /* If the modifier didn't get printed by the type, print it
3579 d_print_mod (dpi
, dc
);
3581 dpi
->modifiers
= dpm
.next
;
3586 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
3587 if ((dpi
->options
& DMGL_JAVA
) == 0)
3588 d_append_buffer (dpi
, dc
->u
.s_builtin
.type
->name
,
3589 dc
->u
.s_builtin
.type
->len
);
3591 d_append_buffer (dpi
, dc
->u
.s_builtin
.type
->java_name
,
3592 dc
->u
.s_builtin
.type
->java_len
);
3595 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
3596 d_print_comp (dpi
, d_left (dc
));
3599 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
3601 if ((dpi
->options
& DMGL_RET_POSTFIX
) != 0)
3602 d_print_function_type (dpi
, dc
, dpi
->modifiers
);
3604 /* Print return type if present */
3605 if (d_left (dc
) != NULL
)
3607 struct d_print_mod dpm
;
3609 /* We must pass this type down as a modifier in order to
3610 print it in the right location. */
3611 dpm
.next
= dpi
->modifiers
;
3612 dpi
->modifiers
= &dpm
;
3615 dpm
.templates
= dpi
->templates
;
3617 d_print_comp (dpi
, d_left (dc
));
3619 dpi
->modifiers
= dpm
.next
;
3624 /* In standard prefix notation, there is a space between the
3625 return type and the function signature. */
3626 if ((dpi
->options
& DMGL_RET_POSTFIX
) == 0)
3627 d_append_char (dpi
, ' ');
3630 if ((dpi
->options
& DMGL_RET_POSTFIX
) == 0)
3631 d_print_function_type (dpi
, dc
, dpi
->modifiers
);
3636 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
3638 struct d_print_mod
*hold_modifiers
;
3639 struct d_print_mod adpm
[4];
3641 struct d_print_mod
*pdpm
;
3643 /* We must pass this type down as a modifier in order to print
3644 multi-dimensional arrays correctly. If the array itself is
3645 CV-qualified, we act as though the element type were
3646 CV-qualified. We do this by copying the modifiers down
3647 rather than fiddling pointers, so that we don't wind up
3648 with a d_print_mod higher on the stack pointing into our
3649 stack frame after we return. */
3651 hold_modifiers
= dpi
->modifiers
;
3653 adpm
[0].next
= hold_modifiers
;
3654 dpi
->modifiers
= &adpm
[0];
3656 adpm
[0].printed
= 0;
3657 adpm
[0].templates
= dpi
->templates
;
3660 pdpm
= hold_modifiers
;
3662 && (pdpm
->mod
->type
== DEMANGLE_COMPONENT_RESTRICT
3663 || pdpm
->mod
->type
== DEMANGLE_COMPONENT_VOLATILE
3664 || pdpm
->mod
->type
== DEMANGLE_COMPONENT_CONST
))
3666 if (! pdpm
->printed
)
3668 if (i
>= sizeof adpm
/ sizeof adpm
[0])
3670 d_print_error (dpi
);
3675 adpm
[i
].next
= dpi
->modifiers
;
3676 dpi
->modifiers
= &adpm
[i
];
3684 d_print_comp (dpi
, d_right (dc
));
3686 dpi
->modifiers
= hold_modifiers
;
3688 if (adpm
[0].printed
)
3694 d_print_mod (dpi
, adpm
[i
].mod
);
3697 d_print_array_type (dpi
, dc
, dpi
->modifiers
);
3702 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
3704 struct d_print_mod dpm
;
3706 dpm
.next
= dpi
->modifiers
;
3707 dpi
->modifiers
= &dpm
;
3710 dpm
.templates
= dpi
->templates
;
3712 d_print_comp (dpi
, d_right (dc
));
3714 /* If the modifier didn't get printed by the type, print it
3718 d_append_char (dpi
, ' ');
3719 d_print_comp (dpi
, d_left (dc
));
3720 d_append_string (dpi
, "::*");
3723 dpi
->modifiers
= dpm
.next
;
3728 case DEMANGLE_COMPONENT_ARGLIST
:
3729 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
3730 if (d_left (dc
) != NULL
)
3731 d_print_comp (dpi
, d_left (dc
));
3732 if (d_right (dc
) != NULL
)
3734 d_append_string (dpi
, ", ");
3735 d_print_comp (dpi
, d_right (dc
));
3739 case DEMANGLE_COMPONENT_OPERATOR
:
3743 d_append_string (dpi
, "operator");
3744 c
= dc
->u
.s_operator
.op
->name
[0];
3746 d_append_char (dpi
, ' ');
3747 d_append_buffer (dpi
, dc
->u
.s_operator
.op
->name
,
3748 dc
->u
.s_operator
.op
->len
);
3752 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
3753 d_append_string (dpi
, "operator ");
3754 d_print_comp (dpi
, dc
->u
.s_extended_operator
.name
);
3757 case DEMANGLE_COMPONENT_CAST
:
3758 d_append_string (dpi
, "operator ");
3759 d_print_cast (dpi
, dc
);
3762 case DEMANGLE_COMPONENT_UNARY
:
3763 if (d_left (dc
)->type
!= DEMANGLE_COMPONENT_CAST
)
3764 d_print_expr_op (dpi
, d_left (dc
));
3767 d_append_char (dpi
, '(');
3768 d_print_cast (dpi
, d_left (dc
));
3769 d_append_char (dpi
, ')');
3771 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_CAST
3772 && d_right (dc
)->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
)
3773 /* type() -- FIXME what about type(multiple,args) */
3774 d_append_string (dpi
, "()");
3776 d_print_subexpr (dpi
, d_right (dc
));
3779 case DEMANGLE_COMPONENT_BINARY
:
3780 if (d_right (dc
)->type
!= DEMANGLE_COMPONENT_BINARY_ARGS
)
3782 d_print_error (dpi
);
3786 /* We wrap an expression which uses the greater-than operator in
3787 an extra layer of parens so that it does not get confused
3788 with the '>' which ends the template parameters. */
3789 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_OPERATOR
3790 && d_left (dc
)->u
.s_operator
.op
->len
== 1
3791 && d_left (dc
)->u
.s_operator
.op
->name
[0] == '>')
3792 d_append_char (dpi
, '(');
3794 d_print_subexpr (dpi
, d_left (d_right (dc
)));
3795 if (strcmp (d_left (dc
)->u
.s_operator
.op
->code
, "cl") != 0)
3796 d_print_expr_op (dpi
, d_left (dc
));
3797 d_print_subexpr (dpi
, d_right (d_right (dc
)));
3799 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_OPERATOR
3800 && d_left (dc
)->u
.s_operator
.op
->len
== 1
3801 && d_left (dc
)->u
.s_operator
.op
->name
[0] == '>')
3802 d_append_char (dpi
, ')');
3806 case DEMANGLE_COMPONENT_BINARY_ARGS
:
3807 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
3808 d_print_error (dpi
);
3811 case DEMANGLE_COMPONENT_TRINARY
:
3812 if (d_right (dc
)->type
!= DEMANGLE_COMPONENT_TRINARY_ARG1
3813 || d_right (d_right (dc
))->type
!= DEMANGLE_COMPONENT_TRINARY_ARG2
)
3815 d_print_error (dpi
);
3818 d_print_subexpr (dpi
, d_left (d_right (dc
)));
3819 d_print_expr_op (dpi
, d_left (dc
));
3820 d_print_subexpr (dpi
, d_left (d_right (d_right (dc
))));
3821 d_append_string (dpi
, " : ");
3822 d_print_subexpr (dpi
, d_right (d_right (d_right (dc
))));
3825 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
3826 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
3827 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
3828 d_print_error (dpi
);
3831 case DEMANGLE_COMPONENT_LITERAL
:
3832 case DEMANGLE_COMPONENT_LITERAL_NEG
:
3834 enum d_builtin_type_print tp
;
3836 /* For some builtin types, produce simpler output. */
3837 tp
= D_PRINT_DEFAULT
;
3838 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
)
3840 tp
= d_left (dc
)->u
.s_builtin
.type
->print
;
3844 case D_PRINT_UNSIGNED
:
3846 case D_PRINT_UNSIGNED_LONG
:
3847 case D_PRINT_LONG_LONG
:
3848 case D_PRINT_UNSIGNED_LONG_LONG
:
3849 if (d_right (dc
)->type
== DEMANGLE_COMPONENT_NAME
)
3851 if (dc
->type
== DEMANGLE_COMPONENT_LITERAL_NEG
)
3852 d_append_char (dpi
, '-');
3853 d_print_comp (dpi
, d_right (dc
));
3858 case D_PRINT_UNSIGNED
:
3859 d_append_char (dpi
, 'u');
3862 d_append_char (dpi
, 'l');
3864 case D_PRINT_UNSIGNED_LONG
:
3865 d_append_string (dpi
, "ul");
3867 case D_PRINT_LONG_LONG
:
3868 d_append_string (dpi
, "ll");
3870 case D_PRINT_UNSIGNED_LONG_LONG
:
3871 d_append_string (dpi
, "ull");
3879 if (d_right (dc
)->type
== DEMANGLE_COMPONENT_NAME
3880 && d_right (dc
)->u
.s_name
.len
== 1
3881 && dc
->type
== DEMANGLE_COMPONENT_LITERAL
)
3883 switch (d_right (dc
)->u
.s_name
.s
[0])
3886 d_append_string (dpi
, "false");
3889 d_append_string (dpi
, "true");
3902 d_append_char (dpi
, '(');
3903 d_print_comp (dpi
, d_left (dc
));
3904 d_append_char (dpi
, ')');
3905 if (dc
->type
== DEMANGLE_COMPONENT_LITERAL_NEG
)
3906 d_append_char (dpi
, '-');
3907 if (tp
== D_PRINT_FLOAT
)
3908 d_append_char (dpi
, '[');
3909 d_print_comp (dpi
, d_right (dc
));
3910 if (tp
== D_PRINT_FLOAT
)
3911 d_append_char (dpi
, ']');
3915 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
3916 d_append_string (dpi
, "java resource ");
3917 d_print_comp (dpi
, d_left (dc
));
3920 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
3921 d_print_comp (dpi
, d_left (dc
));
3922 d_print_comp (dpi
, d_right (dc
));
3925 case DEMANGLE_COMPONENT_CHARACTER
:
3926 d_append_char (dpi
, dc
->u
.s_character
.character
);
3929 case DEMANGLE_COMPONENT_DECLTYPE
:
3930 d_append_string (dpi
, "decltype (");
3931 d_print_comp (dpi
, d_left (dc
));
3932 d_append_char (dpi
, ')');
3935 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
3937 struct demangle_component
*a
= d_find_pack (dpi
, d_left (dc
));
3938 int len
= d_pack_length (a
);
3942 for (i
= 0; i
< len
; ++i
)
3944 dpi
->pack_index
= i
;
3945 d_print_comp (dpi
, dc
);
3947 d_append_string (dpi
, ", ");
3953 d_print_error (dpi
);
3958 /* Print a Java dentifier. For Java we try to handle encoded extended
3959 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
3960 so we don't it for C++. Characters are encoded as
3964 d_print_java_identifier (struct d_print_info
*dpi
, const char *name
, int len
)
3970 for (p
= name
; p
< end
; ++p
)
3981 for (q
= p
+ 3; q
< end
; ++q
)
3987 else if (*q
>= 'A' && *q
<= 'F')
3988 dig
= *q
- 'A' + 10;
3989 else if (*q
>= 'a' && *q
<= 'f')
3990 dig
= *q
- 'a' + 10;
3996 /* If the Unicode character is larger than 256, we don't try
3997 to deal with it here. FIXME. */
3998 if (q
< end
&& *q
== '_' && c
< 256)
4000 d_append_char (dpi
, c
);
4006 d_append_char (dpi
, *p
);
4010 /* Print a list of modifiers. SUFFIX is 1 if we are printing
4011 qualifiers on this after printing a function. */
4014 d_print_mod_list (struct d_print_info
*dpi
,
4015 struct d_print_mod
*mods
, int suffix
)
4017 struct d_print_template
*hold_dpt
;
4019 if (mods
== NULL
|| d_print_saw_error (dpi
))
4024 && (mods
->mod
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
4025 || mods
->mod
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
4026 || mods
->mod
->type
== DEMANGLE_COMPONENT_CONST_THIS
)))
4028 d_print_mod_list (dpi
, mods
->next
, suffix
);
4034 hold_dpt
= dpi
->templates
;
4035 dpi
->templates
= mods
->templates
;
4037 if (mods
->mod
->type
== DEMANGLE_COMPONENT_FUNCTION_TYPE
)
4039 d_print_function_type (dpi
, mods
->mod
, mods
->next
);
4040 dpi
->templates
= hold_dpt
;
4043 else if (mods
->mod
->type
== DEMANGLE_COMPONENT_ARRAY_TYPE
)
4045 d_print_array_type (dpi
, mods
->mod
, mods
->next
);
4046 dpi
->templates
= hold_dpt
;
4049 else if (mods
->mod
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
4051 struct d_print_mod
*hold_modifiers
;
4052 struct demangle_component
*dc
;
4054 /* When this is on the modifier stack, we have pulled any
4055 qualifiers off the right argument already. Otherwise, we
4056 print it as usual, but don't let the left argument see any
4059 hold_modifiers
= dpi
->modifiers
;
4060 dpi
->modifiers
= NULL
;
4061 d_print_comp (dpi
, d_left (mods
->mod
));
4062 dpi
->modifiers
= hold_modifiers
;
4064 if ((dpi
->options
& DMGL_JAVA
) == 0)
4065 d_append_string (dpi
, "::");
4067 d_append_char (dpi
, '.');
4069 dc
= d_right (mods
->mod
);
4070 while (dc
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
4071 || dc
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
4072 || dc
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
4075 d_print_comp (dpi
, dc
);
4077 dpi
->templates
= hold_dpt
;
4081 d_print_mod (dpi
, mods
->mod
);
4083 dpi
->templates
= hold_dpt
;
4085 d_print_mod_list (dpi
, mods
->next
, suffix
);
4088 /* Print a modifier. */
4091 d_print_mod (struct d_print_info
*dpi
,
4092 const struct demangle_component
*mod
)
4096 case DEMANGLE_COMPONENT_RESTRICT
:
4097 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
4098 d_append_string (dpi
, " restrict");
4100 case DEMANGLE_COMPONENT_VOLATILE
:
4101 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
4102 d_append_string (dpi
, " volatile");
4104 case DEMANGLE_COMPONENT_CONST
:
4105 case DEMANGLE_COMPONENT_CONST_THIS
:
4106 d_append_string (dpi
, " const");
4108 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
4109 d_append_char (dpi
, ' ');
4110 d_print_comp (dpi
, d_right (mod
));
4112 case DEMANGLE_COMPONENT_POINTER
:
4113 /* There is no pointer symbol in Java. */
4114 if ((dpi
->options
& DMGL_JAVA
) == 0)
4115 d_append_char (dpi
, '*');
4117 case DEMANGLE_COMPONENT_REFERENCE
:
4118 d_append_char (dpi
, '&');
4120 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
4121 d_append_string (dpi
, "&&");
4123 case DEMANGLE_COMPONENT_COMPLEX
:
4124 d_append_string (dpi
, "complex ");
4126 case DEMANGLE_COMPONENT_IMAGINARY
:
4127 d_append_string (dpi
, "imaginary ");
4129 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
4130 if (d_last_char (dpi
) != '(')
4131 d_append_char (dpi
, ' ');
4132 d_print_comp (dpi
, d_left (mod
));
4133 d_append_string (dpi
, "::*");
4135 case DEMANGLE_COMPONENT_TYPED_NAME
:
4136 d_print_comp (dpi
, d_left (mod
));
4139 /* Otherwise, we have something that won't go back on the
4140 modifier stack, so we can just print it. */
4141 d_print_comp (dpi
, mod
);
4146 /* Print a function type, except for the return type. */
4149 d_print_function_type (struct d_print_info
*dpi
,
4150 const struct demangle_component
*dc
,
4151 struct d_print_mod
*mods
)
4156 struct d_print_mod
*p
;
4157 struct d_print_mod
*hold_modifiers
;
4162 for (p
= mods
; p
!= NULL
; p
= p
->next
)
4168 switch (p
->mod
->type
)
4170 case DEMANGLE_COMPONENT_POINTER
:
4171 case DEMANGLE_COMPONENT_REFERENCE
:
4172 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
4175 case DEMANGLE_COMPONENT_RESTRICT
:
4176 case DEMANGLE_COMPONENT_VOLATILE
:
4177 case DEMANGLE_COMPONENT_CONST
:
4178 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
4179 case DEMANGLE_COMPONENT_COMPLEX
:
4180 case DEMANGLE_COMPONENT_IMAGINARY
:
4181 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
4185 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
4186 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
4187 case DEMANGLE_COMPONENT_CONST_THIS
:
4196 if (d_left (dc
) != NULL
&& ! saw_mod
)
4203 if (d_last_char (dpi
) != '('
4204 && d_last_char (dpi
) != '*')
4207 if (need_space
&& d_last_char (dpi
) != ' ')
4208 d_append_char (dpi
, ' ');
4209 d_append_char (dpi
, '(');
4212 hold_modifiers
= dpi
->modifiers
;
4213 dpi
->modifiers
= NULL
;
4215 d_print_mod_list (dpi
, mods
, 0);
4218 d_append_char (dpi
, ')');
4220 d_append_char (dpi
, '(');
4222 if (d_right (dc
) != NULL
)
4223 d_print_comp (dpi
, d_right (dc
));
4225 d_append_char (dpi
, ')');
4227 d_print_mod_list (dpi
, mods
, 1);
4229 dpi
->modifiers
= hold_modifiers
;
4232 /* Print an array type, except for the element type. */
4235 d_print_array_type (struct d_print_info
*dpi
,
4236 const struct demangle_component
*dc
,
4237 struct d_print_mod
*mods
)
4245 struct d_print_mod
*p
;
4248 for (p
= mods
; p
!= NULL
; p
= p
->next
)
4252 if (p
->mod
->type
== DEMANGLE_COMPONENT_ARRAY_TYPE
)
4267 d_append_string (dpi
, " (");
4269 d_print_mod_list (dpi
, mods
, 0);
4272 d_append_char (dpi
, ')');
4276 d_append_char (dpi
, ' ');
4278 d_append_char (dpi
, '[');
4280 if (d_left (dc
) != NULL
)
4281 d_print_comp (dpi
, d_left (dc
));
4283 d_append_char (dpi
, ']');
4286 /* Print an operator in an expression. */
4289 d_print_expr_op (struct d_print_info
*dpi
,
4290 const struct demangle_component
*dc
)
4292 if (dc
->type
== DEMANGLE_COMPONENT_OPERATOR
)
4293 d_append_buffer (dpi
, dc
->u
.s_operator
.op
->name
,
4294 dc
->u
.s_operator
.op
->len
);
4296 d_print_comp (dpi
, dc
);
4302 d_print_cast (struct d_print_info
*dpi
,
4303 const struct demangle_component
*dc
)
4305 if (d_left (dc
)->type
!= DEMANGLE_COMPONENT_TEMPLATE
)
4306 d_print_comp (dpi
, d_left (dc
));
4309 struct d_print_mod
*hold_dpm
;
4310 struct d_print_template dpt
;
4312 /* It appears that for a templated cast operator, we need to put
4313 the template parameters in scope for the operator name, but
4314 not for the parameters. The effect is that we need to handle
4315 the template printing here. */
4317 hold_dpm
= dpi
->modifiers
;
4318 dpi
->modifiers
= NULL
;
4320 dpt
.next
= dpi
->templates
;
4321 dpi
->templates
= &dpt
;
4322 dpt
.template_decl
= d_left (dc
);
4324 d_print_comp (dpi
, d_left (d_left (dc
)));
4326 dpi
->templates
= dpt
.next
;
4328 if (d_last_char (dpi
) == '<')
4329 d_append_char (dpi
, ' ');
4330 d_append_char (dpi
, '<');
4331 d_print_comp (dpi
, d_right (d_left (dc
)));
4332 /* Avoid generating two consecutive '>' characters, to avoid
4333 the C++ syntactic ambiguity. */
4334 if (d_last_char (dpi
) == '>')
4335 d_append_char (dpi
, ' ');
4336 d_append_char (dpi
, '>');
4338 dpi
->modifiers
= hold_dpm
;
4342 /* Initialize the information structure we use to pass around
4345 CP_STATIC_IF_GLIBCPP_V3
4347 cplus_demangle_init_info (const char *mangled
, int options
, size_t len
,
4351 di
->send
= mangled
+ len
;
4352 di
->options
= options
;
4356 /* We can not need more components than twice the number of chars in
4357 the mangled string. Most components correspond directly to
4358 chars, but the ARGLIST types are exceptions. */
4359 di
->num_comps
= 2 * len
;
4362 /* Similarly, we can not need more substitutions than there are
4363 chars in the mangled string. */
4368 di
->last_name
= NULL
;
4373 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
4374 mangled name, return strings in repeated callback giving the demangled
4375 name. OPTIONS is the usual libiberty demangler options. On success,
4376 this returns 1. On failure, returns 0. */
4379 d_demangle_callback (const char *mangled
, int options
,
4380 demangle_callbackref callback
, void *opaque
)
4384 struct demangle_component
*dc
;
4387 if (mangled
[0] == '_' && mangled
[1] == 'Z')
4389 else if (strncmp (mangled
, "_GLOBAL_", 8) == 0
4390 && (mangled
[8] == '.' || mangled
[8] == '_' || mangled
[8] == '$')
4391 && (mangled
[9] == 'D' || mangled
[9] == 'I')
4392 && mangled
[10] == '_')
4396 intro
= (mangled
[9] == 'I')
4397 ? "global constructors keyed to "
4398 : "global destructors keyed to ";
4400 callback (intro
, strlen (intro
), opaque
);
4401 callback (mangled
+ 11, strlen (mangled
+ 11), opaque
);
4406 if ((options
& DMGL_TYPES
) == 0)
4411 cplus_demangle_init_info (mangled
, options
, strlen (mangled
), &di
);
4414 #ifdef CP_DYNAMIC_ARRAYS
4415 __extension__
struct demangle_component comps
[di
.num_comps
];
4416 __extension__
struct demangle_component
*subs
[di
.num_subs
];
4421 di
.comps
= alloca (di
.num_comps
* sizeof (*di
.comps
));
4422 di
.subs
= alloca (di
.num_subs
* sizeof (*di
.subs
));
4426 dc
= cplus_demangle_type (&di
);
4428 dc
= cplus_demangle_mangled_name (&di
, 1);
4430 /* If DMGL_PARAMS is set, then if we didn't consume the entire
4431 mangled string, then we didn't successfully demangle it. If
4432 DMGL_PARAMS is not set, we didn't look at the trailing
4434 if (((options
& DMGL_PARAMS
) != 0) && d_peek_char (&di
) != '\0')
4437 #ifdef CP_DEMANGLE_DEBUG
4441 status
= (dc
!= NULL
)
4442 ? cplus_demangle_print_callback (options
, dc
, callback
, opaque
)
4449 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
4450 name, return a buffer allocated with malloc holding the demangled
4451 name. OPTIONS is the usual libiberty demangler options. On
4452 success, this sets *PALC to the allocated size of the returned
4453 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
4454 a memory allocation failure, and returns NULL. */
4457 d_demangle (const char *mangled
, int options
, size_t *palc
)
4459 struct d_growable_string dgs
;
4462 d_growable_string_init (&dgs
, 0);
4464 status
= d_demangle_callback (mangled
, options
,
4465 d_growable_string_callback_adapter
, &dgs
);
4473 *palc
= dgs
.allocation_failure
? 1 : 0;
4477 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
4479 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
4481 /* ia64 ABI-mandated entry point in the C++ runtime library for
4482 performing demangling. MANGLED_NAME is a NUL-terminated character
4483 string containing the name to be demangled.
4485 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
4486 *LENGTH bytes, into which the demangled name is stored. If
4487 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
4488 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
4489 is placed in a region of memory allocated with malloc.
4491 If LENGTH is non-NULL, the length of the buffer containing the
4492 demangled name, is placed in *LENGTH.
4494 The return value is a pointer to the start of the NUL-terminated
4495 demangled name, or NULL if the demangling fails. The caller is
4496 responsible for deallocating this memory using free.
4498 *STATUS is set to one of the following values:
4499 0: The demangling operation succeeded.
4500 -1: A memory allocation failure occurred.
4501 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
4502 -3: One of the arguments is invalid.
4504 The demangling is performed using the C++ ABI mangling rules, with
4508 __cxa_demangle (const char *mangled_name
, char *output_buffer
,
4509 size_t *length
, int *status
)
4514 if (mangled_name
== NULL
)
4521 if (output_buffer
!= NULL
&& length
== NULL
)
4528 demangled
= d_demangle (mangled_name
, DMGL_PARAMS
| DMGL_TYPES
, &alc
);
4530 if (demangled
== NULL
)
4542 if (output_buffer
== NULL
)
4549 if (strlen (demangled
) < *length
)
4551 strcpy (output_buffer
, demangled
);
4553 demangled
= output_buffer
;
4557 free (output_buffer
);
4568 extern int __gcclibcxx_demangle_callback (const char *,
4570 (const char *, size_t, void *),
4573 /* Alternative, allocationless entry point in the C++ runtime library
4574 for performing demangling. MANGLED_NAME is a NUL-terminated character
4575 string containing the name to be demangled.
4577 CALLBACK is a callback function, called with demangled string
4578 segments as demangling progresses; it is called at least once,
4579 but may be called more than once. OPAQUE is a generalized pointer
4580 used as a callback argument.
4582 The return code is one of the following values, equivalent to
4583 the STATUS values of __cxa_demangle() (excluding -1, since this
4584 function performs no memory allocations):
4585 0: The demangling operation succeeded.
4586 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
4587 -3: One of the arguments is invalid.
4589 The demangling is performed using the C++ ABI mangling rules, with
4593 __gcclibcxx_demangle_callback (const char *mangled_name
,
4594 void (*callback
) (const char *, size_t, void *),
4599 if (mangled_name
== NULL
|| callback
== NULL
)
4602 status
= d_demangle_callback (mangled_name
, DMGL_PARAMS
| DMGL_TYPES
,
4610 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
4612 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
4613 mangled name, return a buffer allocated with malloc holding the
4614 demangled name. Otherwise, return NULL. */
4617 cplus_demangle_v3 (const char *mangled
, int options
)
4621 return d_demangle (mangled
, options
, &alc
);
4625 cplus_demangle_v3_callback (const char *mangled
, int options
,
4626 demangle_callbackref callback
, void *opaque
)
4628 return d_demangle_callback (mangled
, options
, callback
, opaque
);
4631 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
4632 conventions, but the output formatting is a little different.
4633 This instructs the C++ demangler not to emit pointer characters ("*"), to
4634 use Java's namespace separator symbol ("." instead of "::"), and to output
4635 JArray<TYPE> as TYPE[]. */
4638 java_demangle_v3 (const char *mangled
)
4642 return d_demangle (mangled
, DMGL_JAVA
| DMGL_PARAMS
| DMGL_RET_POSTFIX
, &alc
);
4646 java_demangle_v3_callback (const char *mangled
,
4647 demangle_callbackref callback
, void *opaque
)
4649 return d_demangle_callback (mangled
,
4650 DMGL_JAVA
| DMGL_PARAMS
| DMGL_RET_POSTFIX
,
4654 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
4656 #ifndef IN_GLIBCPP_V3
4658 /* Demangle a string in order to find out whether it is a constructor
4659 or destructor. Return non-zero on success. Set *CTOR_KIND and
4660 *DTOR_KIND appropriately. */
4663 is_ctor_or_dtor (const char *mangled
,
4664 enum gnu_v3_ctor_kinds
*ctor_kind
,
4665 enum gnu_v3_dtor_kinds
*dtor_kind
)
4668 struct demangle_component
*dc
;
4671 *ctor_kind
= (enum gnu_v3_ctor_kinds
) 0;
4672 *dtor_kind
= (enum gnu_v3_dtor_kinds
) 0;
4674 cplus_demangle_init_info (mangled
, DMGL_GNU_V3
, strlen (mangled
), &di
);
4677 #ifdef CP_DYNAMIC_ARRAYS
4678 __extension__
struct demangle_component comps
[di
.num_comps
];
4679 __extension__
struct demangle_component
*subs
[di
.num_subs
];
4684 di
.comps
= alloca (di
.num_comps
* sizeof (*di
.comps
));
4685 di
.subs
= alloca (di
.num_subs
* sizeof (*di
.subs
));
4688 dc
= cplus_demangle_mangled_name (&di
, 1);
4690 /* Note that because we did not pass DMGL_PARAMS, we don't expect
4691 to demangle the entire string. */
4701 case DEMANGLE_COMPONENT_TYPED_NAME
:
4702 case DEMANGLE_COMPONENT_TEMPLATE
:
4703 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
4704 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
4705 case DEMANGLE_COMPONENT_CONST_THIS
:
4708 case DEMANGLE_COMPONENT_QUAL_NAME
:
4709 case DEMANGLE_COMPONENT_LOCAL_NAME
:
4712 case DEMANGLE_COMPONENT_CTOR
:
4713 *ctor_kind
= dc
->u
.s_ctor
.kind
;
4717 case DEMANGLE_COMPONENT_DTOR
:
4718 *dtor_kind
= dc
->u
.s_dtor
.kind
;
4729 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
4730 name. A non-zero return indicates the type of constructor. */
4732 enum gnu_v3_ctor_kinds
4733 is_gnu_v3_mangled_ctor (const char *name
)
4735 enum gnu_v3_ctor_kinds ctor_kind
;
4736 enum gnu_v3_dtor_kinds dtor_kind
;
4738 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
4739 return (enum gnu_v3_ctor_kinds
) 0;
4744 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
4745 name. A non-zero return indicates the type of destructor. */
4747 enum gnu_v3_dtor_kinds
4748 is_gnu_v3_mangled_dtor (const char *name
)
4750 enum gnu_v3_ctor_kinds ctor_kind
;
4751 enum gnu_v3_dtor_kinds dtor_kind
;
4753 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
4754 return (enum gnu_v3_dtor_kinds
) 0;
4758 #endif /* IN_GLIBCPP_V3 */
4760 #ifdef STANDALONE_DEMANGLER
4763 #include "dyn-string.h"
4765 static void print_usage (FILE* fp
, int exit_value
);
4767 #define IS_ALPHA(CHAR) \
4768 (((CHAR) >= 'a' && (CHAR) <= 'z') \
4769 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
4771 /* Non-zero if CHAR is a character than can occur in a mangled name. */
4772 #define is_mangled_char(CHAR) \
4773 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
4774 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
4776 /* The name of this program, as invoked. */
4777 const char* program_name
;
4779 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
4782 print_usage (FILE* fp
, int exit_value
)
4784 fprintf (fp
, "Usage: %s [options] [names ...]\n", program_name
);
4785 fprintf (fp
, "Options:\n");
4786 fprintf (fp
, " -h,--help Display this message.\n");
4787 fprintf (fp
, " -p,--no-params Don't display function parameters\n");
4788 fprintf (fp
, " -v,--verbose Produce verbose demanglings.\n");
4789 fprintf (fp
, "If names are provided, they are demangled. Otherwise filters standard input.\n");
4794 /* Option specification for getopt_long. */
4795 static const struct option long_options
[] =
4797 { "help", no_argument
, NULL
, 'h' },
4798 { "no-params", no_argument
, NULL
, 'p' },
4799 { "verbose", no_argument
, NULL
, 'v' },
4800 { NULL
, no_argument
, NULL
, 0 },
4803 /* Main entry for a demangling filter executable. It will demangle
4804 its command line arguments, if any. If none are provided, it will
4805 filter stdin to stdout, replacing any recognized mangled C++ names
4806 with their demangled equivalents. */
4809 main (int argc
, char *argv
[])
4813 int options
= DMGL_PARAMS
| DMGL_ANSI
| DMGL_TYPES
;
4815 /* Use the program name of this program, as invoked. */
4816 program_name
= argv
[0];
4818 /* Parse options. */
4821 opt_char
= getopt_long (argc
, argv
, "hpv", long_options
, NULL
);
4824 case '?': /* Unrecognized option. */
4825 print_usage (stderr
, 1);
4829 print_usage (stdout
, 0);
4833 options
&= ~ DMGL_PARAMS
;
4837 options
|= DMGL_VERBOSE
;
4841 while (opt_char
!= -1);
4844 /* No command line arguments were provided. Filter stdin. */
4846 dyn_string_t mangled
= dyn_string_new (3);
4849 /* Read all of input. */
4850 while (!feof (stdin
))
4854 /* Pile characters into mangled until we hit one that can't
4855 occur in a mangled name. */
4857 while (!feof (stdin
) && is_mangled_char (c
))
4859 dyn_string_append_char (mangled
, c
);
4865 if (dyn_string_length (mangled
) > 0)
4867 #ifdef IN_GLIBCPP_V3
4868 s
= __cxa_demangle (dyn_string_buf (mangled
), NULL
, NULL
, NULL
);
4870 s
= cplus_demangle_v3 (dyn_string_buf (mangled
), options
);
4880 /* It might not have been a mangled name. Print the
4882 fputs (dyn_string_buf (mangled
), stdout
);
4885 dyn_string_clear (mangled
);
4888 /* If we haven't hit EOF yet, we've read one character that
4889 can't occur in a mangled name, so print it out. */
4894 dyn_string_delete (mangled
);
4897 /* Demangle command line arguments. */
4899 /* Loop over command line arguments. */
4900 for (i
= optind
; i
< argc
; ++i
)
4903 #ifdef IN_GLIBCPP_V3
4907 /* Attempt to demangle. */
4908 #ifdef IN_GLIBCPP_V3
4909 s
= __cxa_demangle (argv
[i
], NULL
, NULL
, &status
);
4911 s
= cplus_demangle_v3 (argv
[i
], options
);
4914 /* If it worked, print the demangled name. */
4922 #ifdef IN_GLIBCPP_V3
4923 fprintf (stderr
, "Failed: %s (status %d)\n", argv
[i
], status
);
4925 fprintf (stderr
, "Failed: %s\n", argv
[i
]);
4934 #endif /* STANDALONE_DEMANGLER */