1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@wasabisystems.com>.
5 This file is part of the libiberty library, which is part of GCC.
7 This file is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combined
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
31 /* This code implements a demangler for the g++ V3 ABI. The ABI is
32 described on this web page:
33 http://www.codesourcery.com/cxx-abi/abi.html#mangling
35 This code was written while looking at the demangler written by
36 Alex Samuel <samuel@codesourcery.com>.
38 This code first pulls the mangled name apart into a list of
39 components, and then walks the list generating the demangled
42 This file will normally define the following functions, q.v.:
43 char *cplus_demangle_v3(const char *mangled, int options)
44 char *java_demangle_v3(const char *mangled)
45 enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
46 enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
48 Also, the interface to the component list is public, and defined in
49 demangle.h. The interface consists of these types, which are
50 defined in demangle.h:
51 enum demangle_component_type
52 struct demangle_component
53 and these functions defined in this file:
54 cplus_demangle_fill_name
55 cplus_demangle_fill_extended_operator
56 cplus_demangle_fill_ctor
57 cplus_demangle_fill_dtor
59 and other functions defined in the file cp-demint.c.
61 This file also defines some other functions and variables which are
62 only to be used by the file cp-demint.c.
64 Preprocessor macros you can define while compiling this file:
67 If defined, this file defines the following function, q.v.:
68 char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
70 instead of cplus_demangle_v3() and java_demangle_v3().
73 If defined, this file defines only __cxa_demangle(), and no other
74 publically visible functions or variables.
77 If defined, this file defines a main() function which demangles
78 any arguments, or, if none, demangles stdin.
81 If defined, turns on debugging mode, which prints information on
82 stdout about the mangled string. This is not generally useful.
99 #include "libiberty.h"
100 #include "demangle.h"
101 #include "cp-demangle.h"
103 /* If IN_GLIBCPP_V3 is defined, some functions are made static. We
104 also rename them via #define to avoid compiler errors when the
105 static definition conflicts with the extern declaration in a header
109 #define CP_STATIC_IF_GLIBCPP_V3 static
111 #define cplus_demangle_fill_name d_fill_name
112 static int d_fill_name (struct demangle_component
*, const char *, int);
114 #define cplus_demangle_fill_extended_operator d_fill_extended_operator
116 d_fill_extended_operator (struct demangle_component
*, int,
117 struct demangle_component
*);
119 #define cplus_demangle_fill_ctor d_fill_ctor
121 d_fill_ctor (struct demangle_component
*, enum gnu_v3_ctor_kinds
,
122 struct demangle_component
*);
124 #define cplus_demangle_fill_dtor d_fill_dtor
126 d_fill_dtor (struct demangle_component
*, enum gnu_v3_dtor_kinds
,
127 struct demangle_component
*);
129 #define cplus_demangle_mangled_name d_mangled_name
130 static struct demangle_component
*d_mangled_name (struct d_info
*, int);
132 #define cplus_demangle_type d_type
133 static struct demangle_component
*d_type (struct d_info
*);
135 #define cplus_demangle_print d_print
136 static char *d_print (int, const struct demangle_component
*, int, size_t *);
138 #define cplus_demangle_init_info d_init_info
139 static void d_init_info (const char *, int, size_t, struct d_info
*);
141 #else /* ! defined(IN_GLIBCPP_V3) */
142 #define CP_STATIC_IF_GLIBCPP_V3
143 #endif /* ! defined(IN_GLIBCPP_V3) */
145 /* See if the compiler supports dynamic arrays. */
148 #define CP_DYNAMIC_ARRAYS
151 #ifdef __STDC_VERSION__
152 #if __STDC_VERSION__ >= 199901L
153 #define CP_DYNAMIC_ARRAYS
154 #endif /* __STDC__VERSION >= 199901L */
155 #endif /* defined (__STDC_VERSION__) */
156 #endif /* defined (__STDC__) */
157 #endif /* ! defined (__GNUC__) */
159 /* We avoid pulling in the ctype tables, to prevent pulling in
160 additional unresolved symbols when this code is used in a library.
161 FIXME: Is this really a valid reason? This comes from the original
164 As of this writing this file has the following undefined references
165 when compiled with -DIN_GLIBCPP_V3: malloc, realloc, free, memcpy,
166 strcpy, strcat, strlen. */
168 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
169 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
170 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
172 /* The prefix prepended by GCC to an identifier represnting the
173 anonymous namespace. */
174 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
175 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
176 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
178 /* Information we keep for the standard substitutions. */
180 struct d_standard_sub_info
182 /* The code for this substitution. */
184 /* The simple string it expands to. */
185 const char *simple_expansion
;
186 /* The length of the simple expansion. */
188 /* The results of a full, verbose, expansion. This is used when
189 qualifying a constructor/destructor, or when in verbose mode. */
190 const char *full_expansion
;
191 /* The length of the full expansion. */
193 /* What to set the last_name field of d_info to; NULL if we should
194 not set it. This is only relevant when qualifying a
195 constructor/destructor. */
196 const char *set_last_name
;
197 /* The length of set_last_name. */
198 int set_last_name_len
;
201 /* Accessors for subtrees of struct demangle_component. */
203 #define d_left(dc) ((dc)->u.s_binary.left)
204 #define d_right(dc) ((dc)->u.s_binary.right)
206 /* A list of templates. This is used while printing. */
208 struct d_print_template
210 /* Next template on the list. */
211 struct d_print_template
*next
;
213 const struct demangle_component
*template_decl
;
216 /* A list of type modifiers. This is used while printing. */
220 /* Next modifier on the list. These are in the reverse of the order
221 in which they appeared in the mangled string. */
222 struct d_print_mod
*next
;
224 const struct demangle_component
*mod
;
225 /* Whether this modifier was printed. */
227 /* The list of templates which applies to this modifier. */
228 struct d_print_template
*templates
;
231 /* We use this structure to hold information during printing. */
235 /* The options passed to the demangler. */
237 /* Buffer holding the result. */
239 /* Current length of data in buffer. */
241 /* Allocated size of buffer. */
243 /* The current list of templates, if any. */
244 struct d_print_template
*templates
;
245 /* The current list of modifiers (e.g., pointer, reference, etc.),
247 struct d_print_mod
*modifiers
;
248 /* Set to 1 if we had a memory allocation failure. */
249 int allocation_failure
;
252 #define d_print_saw_error(dpi) ((dpi)->buf == NULL)
254 #define d_append_char(dpi, c) \
257 if ((dpi)->buf != NULL && (dpi)->len < (dpi)->alc) \
258 (dpi)->buf[(dpi)->len++] = (c); \
260 d_print_append_char ((dpi), (c)); \
264 #define d_append_buffer(dpi, s, l) \
267 if ((dpi)->buf != NULL && (dpi)->len + (l) <= (dpi)->alc) \
269 memcpy ((dpi)->buf + (dpi)->len, (s), (l)); \
273 d_print_append_buffer ((dpi), (s), (l)); \
277 #define d_append_string_constant(dpi, s) \
278 d_append_buffer (dpi, (s), sizeof (s) - 1)
280 #define d_last_char(dpi) \
281 ((dpi)->buf == NULL || (dpi)->len == 0 ? '\0' : (dpi)->buf[(dpi)->len - 1])
283 #ifdef CP_DEMANGLE_DEBUG
284 static void d_dump (struct demangle_component
*, int);
287 static struct demangle_component
*
288 d_make_empty (struct d_info
*);
290 static struct demangle_component
*
291 d_make_comp (struct d_info
*, enum demangle_component_type
,
292 struct demangle_component
*,
293 struct demangle_component
*);
295 static struct demangle_component
*
296 d_make_name (struct d_info
*, const char *, int);
298 static struct demangle_component
*
299 d_make_builtin_type (struct d_info
*,
300 const struct demangle_builtin_type_info
*);
302 static struct demangle_component
*
303 d_make_operator (struct d_info
*,
304 const struct demangle_operator_info
*);
306 static struct demangle_component
*
307 d_make_extended_operator (struct d_info
*, int,
308 struct demangle_component
*);
310 static struct demangle_component
*
311 d_make_ctor (struct d_info
*, enum gnu_v3_ctor_kinds
,
312 struct demangle_component
*);
314 static struct demangle_component
*
315 d_make_dtor (struct d_info
*, enum gnu_v3_dtor_kinds
,
316 struct demangle_component
*);
318 static struct demangle_component
*
319 d_make_template_param (struct d_info
*, long);
321 static struct demangle_component
*
322 d_make_sub (struct d_info
*, const char *, int);
325 has_return_type (struct demangle_component
*);
328 is_ctor_dtor_or_conversion (struct demangle_component
*);
330 static struct demangle_component
*d_encoding (struct d_info
*, int);
332 static struct demangle_component
*d_name (struct d_info
*);
334 static struct demangle_component
*d_nested_name (struct d_info
*);
336 static struct demangle_component
*d_prefix (struct d_info
*);
338 static struct demangle_component
*d_unqualified_name (struct d_info
*);
340 static struct demangle_component
*d_source_name (struct d_info
*);
342 static long d_number (struct d_info
*);
344 static struct demangle_component
*d_identifier (struct d_info
*, int);
346 static struct demangle_component
*d_operator_name (struct d_info
*);
348 static struct demangle_component
*d_special_name (struct d_info
*);
350 static int d_call_offset (struct d_info
*, int);
352 static struct demangle_component
*d_ctor_dtor_name (struct d_info
*);
354 static struct demangle_component
**
355 d_cv_qualifiers (struct d_info
*, struct demangle_component
**, int);
357 static struct demangle_component
*
358 d_function_type (struct d_info
*);
360 static struct demangle_component
*
361 d_bare_function_type (struct d_info
*, int);
363 static struct demangle_component
*
364 d_class_enum_type (struct d_info
*);
366 static struct demangle_component
*d_array_type (struct d_info
*);
368 static struct demangle_component
*
369 d_pointer_to_member_type (struct d_info
*);
371 static struct demangle_component
*
372 d_template_param (struct d_info
*);
374 static struct demangle_component
*d_template_args (struct d_info
*);
376 static struct demangle_component
*
377 d_template_arg (struct d_info
*);
379 static struct demangle_component
*d_expression (struct d_info
*);
381 static struct demangle_component
*d_expr_primary (struct d_info
*);
383 static struct demangle_component
*d_local_name (struct d_info
*);
385 static int d_discriminator (struct d_info
*);
388 d_add_substitution (struct d_info
*, struct demangle_component
*);
390 static struct demangle_component
*d_substitution (struct d_info
*, int);
392 static void d_print_resize (struct d_print_info
*, size_t);
394 static void d_print_append_char (struct d_print_info
*, int);
397 d_print_append_buffer (struct d_print_info
*, const char *, size_t);
399 static void d_print_error (struct d_print_info
*);
402 d_print_comp (struct d_print_info
*, const struct demangle_component
*);
405 d_print_java_identifier (struct d_print_info
*, const char *, int);
408 d_print_mod_list (struct d_print_info
*, struct d_print_mod
*, int);
411 d_print_mod (struct d_print_info
*, const struct demangle_component
*);
414 d_print_function_type (struct d_print_info
*,
415 const struct demangle_component
*,
416 struct d_print_mod
*);
419 d_print_array_type (struct d_print_info
*,
420 const struct demangle_component
*,
421 struct d_print_mod
*);
424 d_print_expr_op (struct d_print_info
*, const struct demangle_component
*);
427 d_print_cast (struct d_print_info
*, const struct demangle_component
*);
429 static char *d_demangle (const char *, int, size_t *);
431 #ifdef CP_DEMANGLE_DEBUG
434 d_dump (struct demangle_component
*dc
, int indent
)
441 for (i
= 0; i
< indent
; ++i
)
446 case DEMANGLE_COMPONENT_NAME
:
447 printf ("name '%.*s'\n", dc
->u
.s_name
.len
, dc
->u
.s_name
.s
);
449 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
450 printf ("template parameter %ld\n", dc
->u
.s_number
.number
);
452 case DEMANGLE_COMPONENT_CTOR
:
453 printf ("constructor %d\n", (int) dc
->u
.s_ctor
.kind
);
454 d_dump (dc
->u
.s_ctor
.name
, indent
+ 2);
456 case DEMANGLE_COMPONENT_DTOR
:
457 printf ("destructor %d\n", (int) dc
->u
.s_dtor
.kind
);
458 d_dump (dc
->u
.s_dtor
.name
, indent
+ 2);
460 case DEMANGLE_COMPONENT_SUB_STD
:
461 printf ("standard substitution %s\n", dc
->u
.s_string
.string
);
463 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
464 printf ("builtin type %s\n", dc
->u
.s_builtin
.type
->name
);
466 case DEMANGLE_COMPONENT_OPERATOR
:
467 printf ("operator %s\n", dc
->u
.s_operator
.op
->name
);
469 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
470 printf ("extended operator with %d args\n",
471 dc
->u
.s_extended_operator
.args
);
472 d_dump (dc
->u
.s_extended_operator
.name
, indent
+ 2);
475 case DEMANGLE_COMPONENT_QUAL_NAME
:
476 printf ("qualified name\n");
478 case DEMANGLE_COMPONENT_LOCAL_NAME
:
479 printf ("local name\n");
481 case DEMANGLE_COMPONENT_TYPED_NAME
:
482 printf ("typed name\n");
484 case DEMANGLE_COMPONENT_TEMPLATE
:
485 printf ("template\n");
487 case DEMANGLE_COMPONENT_VTABLE
:
490 case DEMANGLE_COMPONENT_VTT
:
493 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
494 printf ("construction vtable\n");
496 case DEMANGLE_COMPONENT_TYPEINFO
:
497 printf ("typeinfo\n");
499 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
500 printf ("typeinfo name\n");
502 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
503 printf ("typeinfo function\n");
505 case DEMANGLE_COMPONENT_THUNK
:
508 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
509 printf ("virtual thunk\n");
511 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
512 printf ("covariant thunk\n");
514 case DEMANGLE_COMPONENT_JAVA_CLASS
:
515 printf ("java class\n");
517 case DEMANGLE_COMPONENT_GUARD
:
520 case DEMANGLE_COMPONENT_REFTEMP
:
521 printf ("reference temporary\n");
523 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
524 printf ("hidden alias\n");
526 case DEMANGLE_COMPONENT_RESTRICT
:
527 printf ("restrict\n");
529 case DEMANGLE_COMPONENT_VOLATILE
:
530 printf ("volatile\n");
532 case DEMANGLE_COMPONENT_CONST
:
535 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
536 printf ("restrict this\n");
538 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
539 printf ("volatile this\n");
541 case DEMANGLE_COMPONENT_CONST_THIS
:
542 printf ("const this\n");
544 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
545 printf ("vendor type qualifier\n");
547 case DEMANGLE_COMPONENT_POINTER
:
548 printf ("pointer\n");
550 case DEMANGLE_COMPONENT_REFERENCE
:
551 printf ("reference\n");
553 case DEMANGLE_COMPONENT_COMPLEX
:
554 printf ("complex\n");
556 case DEMANGLE_COMPONENT_IMAGINARY
:
557 printf ("imaginary\n");
559 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
560 printf ("vendor type\n");
562 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
563 printf ("function type\n");
565 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
566 printf ("array type\n");
568 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
569 printf ("pointer to member type\n");
571 case DEMANGLE_COMPONENT_ARGLIST
:
572 printf ("argument list\n");
574 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
575 printf ("template argument list\n");
577 case DEMANGLE_COMPONENT_CAST
:
580 case DEMANGLE_COMPONENT_UNARY
:
581 printf ("unary operator\n");
583 case DEMANGLE_COMPONENT_BINARY
:
584 printf ("binary operator\n");
586 case DEMANGLE_COMPONENT_BINARY_ARGS
:
587 printf ("binary operator arguments\n");
589 case DEMANGLE_COMPONENT_TRINARY
:
590 printf ("trinary operator\n");
592 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
593 printf ("trinary operator arguments 1\n");
595 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
596 printf ("trinary operator arguments 1\n");
598 case DEMANGLE_COMPONENT_LITERAL
:
599 printf ("literal\n");
601 case DEMANGLE_COMPONENT_LITERAL_NEG
:
602 printf ("negative literal\n");
606 d_dump (d_left (dc
), indent
+ 2);
607 d_dump (d_right (dc
), indent
+ 2);
610 #endif /* CP_DEMANGLE_DEBUG */
612 /* Fill in a DEMANGLE_COMPONENT_NAME. */
614 CP_STATIC_IF_GLIBCPP_V3
616 cplus_demangle_fill_name (struct demangle_component
*p
, const char *s
, int len
)
618 if (p
== NULL
|| s
== NULL
|| len
== 0)
620 p
->type
= DEMANGLE_COMPONENT_NAME
;
622 p
->u
.s_name
.len
= len
;
626 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
628 CP_STATIC_IF_GLIBCPP_V3
630 cplus_demangle_fill_extended_operator (struct demangle_component
*p
, int args
,
631 struct demangle_component
*name
)
633 if (p
== NULL
|| args
< 0 || name
== NULL
)
635 p
->type
= DEMANGLE_COMPONENT_EXTENDED_OPERATOR
;
636 p
->u
.s_extended_operator
.args
= args
;
637 p
->u
.s_extended_operator
.name
= name
;
641 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
643 CP_STATIC_IF_GLIBCPP_V3
645 cplus_demangle_fill_ctor (struct demangle_component
*p
,
646 enum gnu_v3_ctor_kinds kind
,
647 struct demangle_component
*name
)
651 || (kind
< gnu_v3_complete_object_ctor
652 && kind
> gnu_v3_complete_object_allocating_ctor
))
654 p
->type
= DEMANGLE_COMPONENT_CTOR
;
655 p
->u
.s_ctor
.kind
= kind
;
656 p
->u
.s_ctor
.name
= name
;
660 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
662 CP_STATIC_IF_GLIBCPP_V3
664 cplus_demangle_fill_dtor (struct demangle_component
*p
,
665 enum gnu_v3_dtor_kinds kind
,
666 struct demangle_component
*name
)
670 || (kind
< gnu_v3_deleting_dtor
671 && kind
> gnu_v3_base_object_dtor
))
673 p
->type
= DEMANGLE_COMPONENT_DTOR
;
674 p
->u
.s_dtor
.kind
= kind
;
675 p
->u
.s_dtor
.name
= name
;
679 /* Add a new component. */
681 static struct demangle_component
*
682 d_make_empty (struct d_info
*di
)
684 struct demangle_component
*p
;
686 if (di
->next_comp
>= di
->num_comps
)
688 p
= &di
->comps
[di
->next_comp
];
693 /* Add a new generic component. */
695 static struct demangle_component
*
696 d_make_comp (struct d_info
*di
, enum demangle_component_type type
,
697 struct demangle_component
*left
,
698 struct demangle_component
*right
)
700 struct demangle_component
*p
;
702 /* We check for errors here. A typical error would be a NULL return
703 from a subroutine. We catch those here, and return NULL
707 /* These types require two parameters. */
708 case DEMANGLE_COMPONENT_QUAL_NAME
:
709 case DEMANGLE_COMPONENT_LOCAL_NAME
:
710 case DEMANGLE_COMPONENT_TYPED_NAME
:
711 case DEMANGLE_COMPONENT_TEMPLATE
:
712 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
713 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
714 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
715 case DEMANGLE_COMPONENT_UNARY
:
716 case DEMANGLE_COMPONENT_BINARY
:
717 case DEMANGLE_COMPONENT_BINARY_ARGS
:
718 case DEMANGLE_COMPONENT_TRINARY
:
719 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
720 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
721 case DEMANGLE_COMPONENT_LITERAL
:
722 case DEMANGLE_COMPONENT_LITERAL_NEG
:
723 if (left
== NULL
|| right
== NULL
)
727 /* These types only require one parameter. */
728 case DEMANGLE_COMPONENT_VTABLE
:
729 case DEMANGLE_COMPONENT_VTT
:
730 case DEMANGLE_COMPONENT_TYPEINFO
:
731 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
732 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
733 case DEMANGLE_COMPONENT_THUNK
:
734 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
735 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
736 case DEMANGLE_COMPONENT_JAVA_CLASS
:
737 case DEMANGLE_COMPONENT_GUARD
:
738 case DEMANGLE_COMPONENT_REFTEMP
:
739 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
740 case DEMANGLE_COMPONENT_POINTER
:
741 case DEMANGLE_COMPONENT_REFERENCE
:
742 case DEMANGLE_COMPONENT_COMPLEX
:
743 case DEMANGLE_COMPONENT_IMAGINARY
:
744 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
745 case DEMANGLE_COMPONENT_ARGLIST
:
746 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
747 case DEMANGLE_COMPONENT_CAST
:
752 /* This needs a right parameter, but the left parameter can be
754 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
759 /* These are allowed to have no parameters--in some cases they
760 will be filled in later. */
761 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
762 case DEMANGLE_COMPONENT_RESTRICT
:
763 case DEMANGLE_COMPONENT_VOLATILE
:
764 case DEMANGLE_COMPONENT_CONST
:
765 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
766 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
767 case DEMANGLE_COMPONENT_CONST_THIS
:
770 /* Other types should not be seen here. */
775 p
= d_make_empty (di
);
779 p
->u
.s_binary
.left
= left
;
780 p
->u
.s_binary
.right
= right
;
785 /* Add a new name component. */
787 static struct demangle_component
*
788 d_make_name (struct d_info
*di
, const char *s
, int len
)
790 struct demangle_component
*p
;
792 p
= d_make_empty (di
);
793 if (! cplus_demangle_fill_name (p
, s
, len
))
798 /* Add a new builtin type component. */
800 static struct demangle_component
*
801 d_make_builtin_type (struct d_info
*di
,
802 const struct demangle_builtin_type_info
*type
)
804 struct demangle_component
*p
;
808 p
= d_make_empty (di
);
811 p
->type
= DEMANGLE_COMPONENT_BUILTIN_TYPE
;
812 p
->u
.s_builtin
.type
= type
;
817 /* Add a new operator component. */
819 static struct demangle_component
*
820 d_make_operator (struct d_info
*di
, const struct demangle_operator_info
*op
)
822 struct demangle_component
*p
;
824 p
= d_make_empty (di
);
827 p
->type
= DEMANGLE_COMPONENT_OPERATOR
;
828 p
->u
.s_operator
.op
= op
;
833 /* Add a new extended operator component. */
835 static struct demangle_component
*
836 d_make_extended_operator (struct d_info
*di
, int args
,
837 struct demangle_component
*name
)
839 struct demangle_component
*p
;
841 p
= d_make_empty (di
);
842 if (! cplus_demangle_fill_extended_operator (p
, args
, name
))
847 /* Add a new constructor component. */
849 static struct demangle_component
*
850 d_make_ctor (struct d_info
*di
, enum gnu_v3_ctor_kinds kind
,
851 struct demangle_component
*name
)
853 struct demangle_component
*p
;
855 p
= d_make_empty (di
);
856 if (! cplus_demangle_fill_ctor (p
, kind
, name
))
861 /* Add a new destructor component. */
863 static struct demangle_component
*
864 d_make_dtor (struct d_info
*di
, enum gnu_v3_dtor_kinds kind
,
865 struct demangle_component
*name
)
867 struct demangle_component
*p
;
869 p
= d_make_empty (di
);
870 if (! cplus_demangle_fill_dtor (p
, kind
, name
))
875 /* Add a new template parameter. */
877 static struct demangle_component
*
878 d_make_template_param (struct d_info
*di
, long i
)
880 struct demangle_component
*p
;
882 p
= d_make_empty (di
);
885 p
->type
= DEMANGLE_COMPONENT_TEMPLATE_PARAM
;
886 p
->u
.s_number
.number
= i
;
891 /* Add a new standard substitution component. */
893 static struct demangle_component
*
894 d_make_sub (struct d_info
*di
, const char *name
, int len
)
896 struct demangle_component
*p
;
898 p
= d_make_empty (di
);
901 p
->type
= DEMANGLE_COMPONENT_SUB_STD
;
902 p
->u
.s_string
.string
= name
;
903 p
->u
.s_string
.len
= len
;
908 /* <mangled-name> ::= _Z <encoding>
910 TOP_LEVEL is non-zero when called at the top level. */
912 CP_STATIC_IF_GLIBCPP_V3
913 struct demangle_component
*
914 cplus_demangle_mangled_name (struct d_info
*di
, int top_level
)
916 if (d_next_char (di
) != '_')
918 if (d_next_char (di
) != 'Z')
920 return d_encoding (di
, top_level
);
923 /* Return whether a function should have a return type. The argument
924 is the function name, which may be qualified in various ways. The
925 rules are that template functions have return types with some
926 exceptions, function types which are not part of a function name
927 mangling have return types with some exceptions, and non-template
928 function names do not have return types. The exceptions are that
929 constructors, destructors, and conversion operators do not have
933 has_return_type (struct demangle_component
*dc
)
941 case DEMANGLE_COMPONENT_TEMPLATE
:
942 return ! is_ctor_dtor_or_conversion (d_left (dc
));
943 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
944 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
945 case DEMANGLE_COMPONENT_CONST_THIS
:
946 return has_return_type (d_left (dc
));
950 /* Return whether a name is a constructor, a destructor, or a
951 conversion operator. */
954 is_ctor_dtor_or_conversion (struct demangle_component
*dc
)
962 case DEMANGLE_COMPONENT_QUAL_NAME
:
963 case DEMANGLE_COMPONENT_LOCAL_NAME
:
964 return is_ctor_dtor_or_conversion (d_right (dc
));
965 case DEMANGLE_COMPONENT_CTOR
:
966 case DEMANGLE_COMPONENT_DTOR
:
967 case DEMANGLE_COMPONENT_CAST
:
972 /* <encoding> ::= <(function) name> <bare-function-type>
976 TOP_LEVEL is non-zero when called at the top level, in which case
977 if DMGL_PARAMS is not set we do not demangle the function
978 parameters. We only set this at the top level, because otherwise
979 we would not correctly demangle names in local scopes. */
981 static struct demangle_component
*
982 d_encoding (struct d_info
*di
, int top_level
)
984 char peek
= d_peek_char (di
);
986 if (peek
== 'G' || peek
== 'T')
987 return d_special_name (di
);
990 struct demangle_component
*dc
;
994 if (dc
!= NULL
&& top_level
&& (di
->options
& DMGL_PARAMS
) == 0)
996 /* Strip off any initial CV-qualifiers, as they really apply
997 to the `this' parameter, and they were not output by the
998 v2 demangler without DMGL_PARAMS. */
999 while (dc
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
1000 || dc
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
1001 || dc
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
1004 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1005 there may be CV-qualifiers on its right argument which
1006 really apply here; this happens when parsing a class
1007 which is local to a function. */
1008 if (dc
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
1010 struct demangle_component
*dcr
;
1013 while (dcr
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
1014 || dcr
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
1015 || dcr
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
1017 dc
->u
.s_binary
.right
= dcr
;
1023 peek
= d_peek_char (di
);
1024 if (peek
== '\0' || peek
== 'E')
1026 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPED_NAME
, dc
,
1027 d_bare_function_type (di
, has_return_type (dc
)));
1031 /* <name> ::= <nested-name>
1033 ::= <unscoped-template-name> <template-args>
1036 <unscoped-name> ::= <unqualified-name>
1037 ::= St <unqualified-name>
1039 <unscoped-template-name> ::= <unscoped-name>
1043 static struct demangle_component
*
1044 d_name (struct d_info
*di
)
1046 char peek
= d_peek_char (di
);
1047 struct demangle_component
*dc
;
1052 return d_nested_name (di
);
1055 return d_local_name (di
);
1061 if (d_peek_next_char (di
) != 't')
1063 dc
= d_substitution (di
, 0);
1069 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
,
1070 d_make_name (di
, "std", 3),
1071 d_unqualified_name (di
));
1076 if (d_peek_char (di
) != 'I')
1078 /* The grammar does not permit this case to occur if we
1079 called d_substitution() above (i.e., subst == 1). We
1080 don't bother to check. */
1084 /* This is <template-args>, which means that we just saw
1085 <unscoped-template-name>, which is a substitution
1086 candidate if we didn't just get it from a
1090 if (! d_add_substitution (di
, dc
))
1093 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, dc
,
1094 d_template_args (di
));
1101 dc
= d_unqualified_name (di
);
1102 if (d_peek_char (di
) == 'I')
1104 /* This is <template-args>, which means that we just saw
1105 <unscoped-template-name>, which is a substitution
1107 if (! d_add_substitution (di
, dc
))
1109 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, dc
,
1110 d_template_args (di
));
1116 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1117 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1120 static struct demangle_component
*
1121 d_nested_name (struct d_info
*di
)
1123 struct demangle_component
*ret
;
1124 struct demangle_component
**pret
;
1126 if (d_next_char (di
) != 'N')
1129 pret
= d_cv_qualifiers (di
, &ret
, 1);
1133 *pret
= d_prefix (di
);
1137 if (d_next_char (di
) != 'E')
1143 /* <prefix> ::= <prefix> <unqualified-name>
1144 ::= <template-prefix> <template-args>
1145 ::= <template-param>
1149 <template-prefix> ::= <prefix> <(template) unqualified-name>
1150 ::= <template-param>
1154 static struct demangle_component
*
1155 d_prefix (struct d_info
*di
)
1157 struct demangle_component
*ret
= NULL
;
1162 enum demangle_component_type comb_type
;
1163 struct demangle_component
*dc
;
1165 peek
= d_peek_char (di
);
1169 /* The older code accepts a <local-name> here, but I don't see
1170 that in the grammar. The older code does not accept a
1171 <template-param> here. */
1173 comb_type
= DEMANGLE_COMPONENT_QUAL_NAME
;
1178 dc
= d_unqualified_name (di
);
1179 else if (peek
== 'S')
1180 dc
= d_substitution (di
, 1);
1181 else if (peek
== 'I')
1185 comb_type
= DEMANGLE_COMPONENT_TEMPLATE
;
1186 dc
= d_template_args (di
);
1188 else if (peek
== 'T')
1189 dc
= d_template_param (di
);
1190 else if (peek
== 'E')
1198 ret
= d_make_comp (di
, comb_type
, ret
, dc
);
1200 if (peek
!= 'S' && d_peek_char (di
) != 'E')
1202 if (! d_add_substitution (di
, ret
))
1208 /* <unqualified-name> ::= <operator-name>
1209 ::= <ctor-dtor-name>
1213 static struct demangle_component
*
1214 d_unqualified_name (struct d_info
*di
)
1218 peek
= d_peek_char (di
);
1219 if (IS_DIGIT (peek
))
1220 return d_source_name (di
);
1221 else if (IS_LOWER (peek
))
1223 struct demangle_component
*ret
;
1225 ret
= d_operator_name (di
);
1226 if (ret
!= NULL
&& ret
->type
== DEMANGLE_COMPONENT_OPERATOR
)
1227 di
->expansion
+= sizeof "operator" + ret
->u
.s_operator
.op
->len
- 2;
1230 else if (peek
== 'C' || peek
== 'D')
1231 return d_ctor_dtor_name (di
);
1236 /* <source-name> ::= <(positive length) number> <identifier> */
1238 static struct demangle_component
*
1239 d_source_name (struct d_info
*di
)
1242 struct demangle_component
*ret
;
1244 len
= d_number (di
);
1247 ret
= d_identifier (di
, len
);
1248 di
->last_name
= ret
;
1252 /* number ::= [n] <(non-negative decimal integer)> */
1255 d_number (struct d_info
*di
)
1262 peek
= d_peek_char (di
);
1267 peek
= d_peek_char (di
);
1273 if (! IS_DIGIT (peek
))
1279 ret
= ret
* 10 + peek
- '0';
1281 peek
= d_peek_char (di
);
1285 /* identifier ::= <(unqualified source code identifier)> */
1287 static struct demangle_component
*
1288 d_identifier (struct d_info
*di
, int len
)
1294 if (di
->send
- name
< len
)
1297 d_advance (di
, len
);
1299 /* A Java mangled name may have a trailing '$' if it is a C++
1300 keyword. This '$' is not included in the length count. We just
1302 if ((di
->options
& DMGL_JAVA
) != 0
1303 && d_peek_char (di
) == '$')
1306 /* Look for something which looks like a gcc encoding of an
1307 anonymous namespace, and replace it with a more user friendly
1309 if (len
>= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN
+ 2
1310 && memcmp (name
, ANONYMOUS_NAMESPACE_PREFIX
,
1311 ANONYMOUS_NAMESPACE_PREFIX_LEN
) == 0)
1315 s
= name
+ ANONYMOUS_NAMESPACE_PREFIX_LEN
;
1316 if ((*s
== '.' || *s
== '_' || *s
== '$')
1319 di
->expansion
-= len
- sizeof "(anonymous namespace)";
1320 return d_make_name (di
, "(anonymous namespace)",
1321 sizeof "(anonymous namespace)" - 1);
1325 return d_make_name (di
, name
, len
);
1328 /* operator_name ::= many different two character encodings.
1330 ::= v <digit> <source-name>
1333 #define NL(s) s, (sizeof s) - 1
1335 CP_STATIC_IF_GLIBCPP_V3
1336 const struct demangle_operator_info cplus_demangle_operators
[] =
1338 { "aN", NL ("&="), 2 },
1339 { "aS", NL ("="), 2 },
1340 { "aa", NL ("&&"), 2 },
1341 { "ad", NL ("&"), 1 },
1342 { "an", NL ("&"), 2 },
1343 { "cl", NL ("()"), 0 },
1344 { "cm", NL (","), 2 },
1345 { "co", NL ("~"), 1 },
1346 { "dV", NL ("/="), 2 },
1347 { "da", NL ("delete[]"), 1 },
1348 { "de", NL ("*"), 1 },
1349 { "dl", NL ("delete"), 1 },
1350 { "dv", NL ("/"), 2 },
1351 { "eO", NL ("^="), 2 },
1352 { "eo", NL ("^"), 2 },
1353 { "eq", NL ("=="), 2 },
1354 { "ge", NL (">="), 2 },
1355 { "gt", NL (">"), 2 },
1356 { "ix", NL ("[]"), 2 },
1357 { "lS", NL ("<<="), 2 },
1358 { "le", NL ("<="), 2 },
1359 { "ls", NL ("<<"), 2 },
1360 { "lt", NL ("<"), 2 },
1361 { "mI", NL ("-="), 2 },
1362 { "mL", NL ("*="), 2 },
1363 { "mi", NL ("-"), 2 },
1364 { "ml", NL ("*"), 2 },
1365 { "mm", NL ("--"), 1 },
1366 { "na", NL ("new[]"), 1 },
1367 { "ne", NL ("!="), 2 },
1368 { "ng", NL ("-"), 1 },
1369 { "nt", NL ("!"), 1 },
1370 { "nw", NL ("new"), 1 },
1371 { "oR", NL ("|="), 2 },
1372 { "oo", NL ("||"), 2 },
1373 { "or", NL ("|"), 2 },
1374 { "pL", NL ("+="), 2 },
1375 { "pl", NL ("+"), 2 },
1376 { "pm", NL ("->*"), 2 },
1377 { "pp", NL ("++"), 1 },
1378 { "ps", NL ("+"), 1 },
1379 { "pt", NL ("->"), 2 },
1380 { "qu", NL ("?"), 3 },
1381 { "rM", NL ("%="), 2 },
1382 { "rS", NL (">>="), 2 },
1383 { "rm", NL ("%"), 2 },
1384 { "rs", NL (">>"), 2 },
1385 { "st", NL ("sizeof "), 1 },
1386 { "sz", NL ("sizeof "), 1 },
1387 { NULL
, NULL
, 0, 0 }
1390 static struct demangle_component
*
1391 d_operator_name (struct d_info
*di
)
1396 c1
= d_next_char (di
);
1397 c2
= d_next_char (di
);
1398 if (c1
== 'v' && IS_DIGIT (c2
))
1399 return d_make_extended_operator (di
, c2
- '0', d_source_name (di
));
1400 else if (c1
== 'c' && c2
== 'v')
1401 return d_make_comp (di
, DEMANGLE_COMPONENT_CAST
,
1402 cplus_demangle_type (di
), NULL
);
1405 /* LOW is the inclusive lower bound. */
1407 /* HIGH is the exclusive upper bound. We subtract one to ignore
1408 the sentinel at the end of the array. */
1409 int high
= ((sizeof (cplus_demangle_operators
)
1410 / sizeof (cplus_demangle_operators
[0]))
1416 const struct demangle_operator_info
*p
;
1418 i
= low
+ (high
- low
) / 2;
1419 p
= cplus_demangle_operators
+ i
;
1421 if (c1
== p
->code
[0] && c2
== p
->code
[1])
1422 return d_make_operator (di
, p
);
1424 if (c1
< p
->code
[0] || (c1
== p
->code
[0] && c2
< p
->code
[1]))
1434 /* <special-name> ::= TV <type>
1438 ::= GV <(object) name>
1439 ::= T <call-offset> <(base) encoding>
1440 ::= Tc <call-offset> <call-offset> <(base) encoding>
1441 Also g++ extensions:
1442 ::= TC <type> <(offset) number> _ <(base) type>
1449 static struct demangle_component
*
1450 d_special_name (struct d_info
*di
)
1454 di
->expansion
+= 20;
1455 c
= d_next_char (di
);
1458 switch (d_next_char (di
))
1462 return d_make_comp (di
, DEMANGLE_COMPONENT_VTABLE
,
1463 cplus_demangle_type (di
), NULL
);
1465 di
->expansion
-= 10;
1466 return d_make_comp (di
, DEMANGLE_COMPONENT_VTT
,
1467 cplus_demangle_type (di
), NULL
);
1469 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO
,
1470 cplus_demangle_type (di
), NULL
);
1472 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO_NAME
,
1473 cplus_demangle_type (di
), NULL
);
1476 if (! d_call_offset (di
, 'h'))
1478 return d_make_comp (di
, DEMANGLE_COMPONENT_THUNK
,
1479 d_encoding (di
, 0), NULL
);
1482 if (! d_call_offset (di
, 'v'))
1484 return d_make_comp (di
, DEMANGLE_COMPONENT_VIRTUAL_THUNK
,
1485 d_encoding (di
, 0), NULL
);
1488 if (! d_call_offset (di
, '\0'))
1490 if (! d_call_offset (di
, '\0'))
1492 return d_make_comp (di
, DEMANGLE_COMPONENT_COVARIANT_THUNK
,
1493 d_encoding (di
, 0), NULL
);
1497 struct demangle_component
*derived_type
;
1499 struct demangle_component
*base_type
;
1501 derived_type
= cplus_demangle_type (di
);
1502 offset
= d_number (di
);
1505 if (d_next_char (di
) != '_')
1507 base_type
= cplus_demangle_type (di
);
1508 /* We don't display the offset. FIXME: We should display
1509 it in verbose mode. */
1511 return d_make_comp (di
, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
,
1512 base_type
, derived_type
);
1516 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO_FN
,
1517 cplus_demangle_type (di
), NULL
);
1519 return d_make_comp (di
, DEMANGLE_COMPONENT_JAVA_CLASS
,
1520 cplus_demangle_type (di
), NULL
);
1528 switch (d_next_char (di
))
1531 return d_make_comp (di
, DEMANGLE_COMPONENT_GUARD
, d_name (di
), NULL
);
1534 return d_make_comp (di
, DEMANGLE_COMPONENT_REFTEMP
, d_name (di
),
1538 return d_make_comp (di
, DEMANGLE_COMPONENT_HIDDEN_ALIAS
,
1539 d_encoding (di
, 0), NULL
);
1549 /* <call-offset> ::= h <nv-offset> _
1552 <nv-offset> ::= <(offset) number>
1554 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1556 The C parameter, if not '\0', is a character we just read which is
1557 the start of the <call-offset>.
1559 We don't display the offset information anywhere. FIXME: We should
1560 display it in verbose mode. */
1563 d_call_offset (struct d_info
*di
, int c
)
1566 c
= d_next_char (di
);
1573 if (d_next_char (di
) != '_')
1580 if (d_next_char (di
) != '_')
1586 /* <ctor-dtor-name> ::= C1
1594 static struct demangle_component
*
1595 d_ctor_dtor_name (struct d_info
*di
)
1597 if (di
->last_name
!= NULL
)
1599 if (di
->last_name
->type
== DEMANGLE_COMPONENT_NAME
)
1600 di
->expansion
+= di
->last_name
->u
.s_name
.len
;
1601 else if (di
->last_name
->type
== DEMANGLE_COMPONENT_SUB_STD
)
1602 di
->expansion
+= di
->last_name
->u
.s_string
.len
;
1604 switch (d_next_char (di
))
1608 enum gnu_v3_ctor_kinds kind
;
1610 switch (d_next_char (di
))
1613 kind
= gnu_v3_complete_object_ctor
;
1616 kind
= gnu_v3_base_object_ctor
;
1619 kind
= gnu_v3_complete_object_allocating_ctor
;
1624 return d_make_ctor (di
, kind
, di
->last_name
);
1629 enum gnu_v3_dtor_kinds kind
;
1631 switch (d_next_char (di
))
1634 kind
= gnu_v3_deleting_dtor
;
1637 kind
= gnu_v3_complete_object_dtor
;
1640 kind
= gnu_v3_base_object_dtor
;
1645 return d_make_dtor (di
, kind
, di
->last_name
);
1653 /* <type> ::= <builtin-type>
1655 ::= <class-enum-type>
1657 ::= <pointer-to-member-type>
1658 ::= <template-param>
1659 ::= <template-template-param> <template-args>
1661 ::= <CV-qualifiers> <type>
1666 ::= U <source-name> <type>
1668 <builtin-type> ::= various one letter codes
1672 CP_STATIC_IF_GLIBCPP_V3
1673 const struct demangle_builtin_type_info
1674 cplus_demangle_builtin_types
[D_BUILTIN_TYPE_COUNT
] =
1676 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT
},
1677 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL
},
1678 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT
},
1679 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT
},
1680 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT
},
1681 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT
},
1682 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT
},
1683 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT
},
1684 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT
},
1685 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED
},
1686 /* k */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
1687 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG
},
1688 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG
},
1689 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT
},
1690 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
1692 /* p */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
1693 /* q */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
1694 /* r */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
1695 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT
},
1696 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT
},
1697 /* u */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
1698 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID
},
1699 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT
},
1700 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG
},
1701 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
1702 D_PRINT_UNSIGNED_LONG_LONG
},
1703 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT
},
1706 CP_STATIC_IF_GLIBCPP_V3
1707 struct demangle_component
*
1708 cplus_demangle_type (struct d_info
*di
)
1711 struct demangle_component
*ret
;
1714 /* The ABI specifies that when CV-qualifiers are used, the base type
1715 is substitutable, and the fully qualified type is substitutable,
1716 but the base type with a strict subset of the CV-qualifiers is
1717 not substitutable. The natural recursive implementation of the
1718 CV-qualifiers would cause subsets to be substitutable, so instead
1719 we pull them all off now.
1721 FIXME: The ABI says that order-insensitive vendor qualifiers
1722 should be handled in the same way, but we have no way to tell
1723 which vendor qualifiers are order-insensitive and which are
1724 order-sensitive. So we just assume that they are all
1725 order-sensitive. g++ 3.4 supports only one vendor qualifier,
1726 __vector, and it treats it as order-sensitive when mangling
1729 peek
= d_peek_char (di
);
1730 if (peek
== 'r' || peek
== 'V' || peek
== 'K')
1732 struct demangle_component
**pret
;
1734 pret
= d_cv_qualifiers (di
, &ret
, 0);
1737 *pret
= cplus_demangle_type (di
);
1738 if (! d_add_substitution (di
, ret
))
1747 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1748 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
1749 case 'o': case 's': case 't':
1750 case 'v': case 'w': case 'x': case 'y': case 'z':
1751 ret
= d_make_builtin_type (di
,
1752 &cplus_demangle_builtin_types
[peek
- 'a']);
1753 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
1760 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_VENDOR_TYPE
,
1761 d_source_name (di
), NULL
);
1765 ret
= d_function_type (di
);
1768 case '0': case '1': case '2': case '3': case '4':
1769 case '5': case '6': case '7': case '8': case '9':
1772 ret
= d_class_enum_type (di
);
1776 ret
= d_array_type (di
);
1780 ret
= d_pointer_to_member_type (di
);
1784 ret
= d_template_param (di
);
1785 if (d_peek_char (di
) == 'I')
1787 /* This is <template-template-param> <template-args>. The
1788 <template-template-param> part is a substitution
1790 if (! d_add_substitution (di
, ret
))
1792 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
,
1793 d_template_args (di
));
1798 /* If this is a special substitution, then it is the start of
1799 <class-enum-type>. */
1803 peek_next
= d_peek_next_char (di
);
1804 if (IS_DIGIT (peek_next
)
1806 || IS_UPPER (peek_next
))
1808 ret
= d_substitution (di
, 0);
1809 /* The substituted name may have been a template name and
1810 may be followed by tepmlate args. */
1811 if (d_peek_char (di
) == 'I')
1812 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
,
1813 d_template_args (di
));
1819 ret
= d_class_enum_type (di
);
1820 /* If the substitution was a complete type, then it is not
1821 a new substitution candidate. However, if the
1822 substitution was followed by template arguments, then
1823 the whole thing is a substitution candidate. */
1824 if (ret
!= NULL
&& ret
->type
== DEMANGLE_COMPONENT_SUB_STD
)
1832 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_POINTER
,
1833 cplus_demangle_type (di
), NULL
);
1838 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_REFERENCE
,
1839 cplus_demangle_type (di
), NULL
);
1844 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_COMPLEX
,
1845 cplus_demangle_type (di
), NULL
);
1850 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_IMAGINARY
,
1851 cplus_demangle_type (di
), NULL
);
1856 ret
= d_source_name (di
);
1857 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
,
1858 cplus_demangle_type (di
), ret
);
1867 if (! d_add_substitution (di
, ret
))
1874 /* <CV-qualifiers> ::= [r] [V] [K] */
1876 static struct demangle_component
**
1877 d_cv_qualifiers (struct d_info
*di
,
1878 struct demangle_component
**pret
, int member_fn
)
1882 peek
= d_peek_char (di
);
1883 while (peek
== 'r' || peek
== 'V' || peek
== 'K')
1885 enum demangle_component_type t
;
1891 ? DEMANGLE_COMPONENT_RESTRICT_THIS
1892 : DEMANGLE_COMPONENT_RESTRICT
);
1893 di
->expansion
+= sizeof "restrict";
1895 else if (peek
== 'V')
1898 ? DEMANGLE_COMPONENT_VOLATILE_THIS
1899 : DEMANGLE_COMPONENT_VOLATILE
);
1900 di
->expansion
+= sizeof "volatile";
1905 ? DEMANGLE_COMPONENT_CONST_THIS
1906 : DEMANGLE_COMPONENT_CONST
);
1907 di
->expansion
+= sizeof "const";
1910 *pret
= d_make_comp (di
, t
, NULL
, NULL
);
1913 pret
= &d_left (*pret
);
1915 peek
= d_peek_char (di
);
1921 /* <function-type> ::= F [Y] <bare-function-type> E */
1923 static struct demangle_component
*
1924 d_function_type (struct d_info
*di
)
1926 struct demangle_component
*ret
;
1928 if (d_next_char (di
) != 'F')
1930 if (d_peek_char (di
) == 'Y')
1932 /* Function has C linkage. We don't print this information.
1933 FIXME: We should print it in verbose mode. */
1936 ret
= d_bare_function_type (di
, 1);
1937 if (d_next_char (di
) != 'E')
1942 /* <bare-function-type> ::= [J]<type>+ */
1944 static struct demangle_component
*
1945 d_bare_function_type (struct d_info
*di
, int has_return_type
)
1947 struct demangle_component
*return_type
;
1948 struct demangle_component
*tl
;
1949 struct demangle_component
**ptl
;
1952 /* Detect special qualifier indicating that the first argument
1953 is the return type. */
1954 peek
= d_peek_char (di
);
1958 has_return_type
= 1;
1966 struct demangle_component
*type
;
1968 peek
= d_peek_char (di
);
1969 if (peek
== '\0' || peek
== 'E')
1971 type
= cplus_demangle_type (di
);
1974 if (has_return_type
)
1977 has_return_type
= 0;
1981 *ptl
= d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, type
, NULL
);
1984 ptl
= &d_right (*ptl
);
1988 /* There should be at least one parameter type besides the optional
1989 return type. A function which takes no arguments will have a
1990 single parameter type void. */
1994 /* If we have a single parameter type void, omit it. */
1995 if (d_right (tl
) == NULL
1996 && d_left (tl
)->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
1997 && d_left (tl
)->u
.s_builtin
.type
->print
== D_PRINT_VOID
)
1999 di
->expansion
-= d_left (tl
)->u
.s_builtin
.type
->len
;
2003 return d_make_comp (di
, DEMANGLE_COMPONENT_FUNCTION_TYPE
, return_type
, tl
);
2006 /* <class-enum-type> ::= <name> */
2008 static struct demangle_component
*
2009 d_class_enum_type (struct d_info
*di
)
2014 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2015 ::= A [<(dimension) expression>] _ <(element) type>
2018 static struct demangle_component
*
2019 d_array_type (struct d_info
*di
)
2022 struct demangle_component
*dim
;
2024 if (d_next_char (di
) != 'A')
2027 peek
= d_peek_char (di
);
2030 else if (IS_DIGIT (peek
))
2038 peek
= d_peek_char (di
);
2040 while (IS_DIGIT (peek
));
2041 dim
= d_make_name (di
, s
, d_str (di
) - s
);
2047 dim
= d_expression (di
);
2052 if (d_next_char (di
) != '_')
2055 return d_make_comp (di
, DEMANGLE_COMPONENT_ARRAY_TYPE
, dim
,
2056 cplus_demangle_type (di
));
2059 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2061 static struct demangle_component
*
2062 d_pointer_to_member_type (struct d_info
*di
)
2064 struct demangle_component
*cl
;
2065 struct demangle_component
*mem
;
2066 struct demangle_component
**pmem
;
2068 if (d_next_char (di
) != 'M')
2071 cl
= cplus_demangle_type (di
);
2073 /* The ABI specifies that any type can be a substitution source, and
2074 that M is followed by two types, and that when a CV-qualified
2075 type is seen both the base type and the CV-qualified types are
2076 substitution sources. The ABI also specifies that for a pointer
2077 to a CV-qualified member function, the qualifiers are attached to
2078 the second type. Given the grammar, a plain reading of the ABI
2079 suggests that both the CV-qualified member function and the
2080 non-qualified member function are substitution sources. However,
2081 g++ does not work that way. g++ treats only the CV-qualified
2082 member function as a substitution source. FIXME. So to work
2083 with g++, we need to pull off the CV-qualifiers here, in order to
2084 avoid calling add_substitution() in cplus_demangle_type(). But
2085 for a CV-qualified member which is not a function, g++ does
2086 follow the ABI, so we need to handle that case here by calling
2087 d_add_substitution ourselves. */
2089 pmem
= d_cv_qualifiers (di
, &mem
, 1);
2092 *pmem
= cplus_demangle_type (di
);
2094 if (pmem
!= &mem
&& (*pmem
)->type
!= DEMANGLE_COMPONENT_FUNCTION_TYPE
)
2096 if (! d_add_substitution (di
, mem
))
2100 return d_make_comp (di
, DEMANGLE_COMPONENT_PTRMEM_TYPE
, cl
, mem
);
2103 /* <template-param> ::= T_
2104 ::= T <(parameter-2 non-negative) number> _
2107 static struct demangle_component
*
2108 d_template_param (struct d_info
*di
)
2112 if (d_next_char (di
) != 'T')
2115 if (d_peek_char (di
) == '_')
2119 param
= d_number (di
);
2125 if (d_next_char (di
) != '_')
2130 return d_make_template_param (di
, param
);
2133 /* <template-args> ::= I <template-arg>+ E */
2135 static struct demangle_component
*
2136 d_template_args (struct d_info
*di
)
2138 struct demangle_component
*hold_last_name
;
2139 struct demangle_component
*al
;
2140 struct demangle_component
**pal
;
2142 /* Preserve the last name we saw--don't let the template arguments
2143 clobber it, as that would give us the wrong name for a subsequent
2144 constructor or destructor. */
2145 hold_last_name
= di
->last_name
;
2147 if (d_next_char (di
) != 'I')
2154 struct demangle_component
*a
;
2156 a
= d_template_arg (di
);
2160 *pal
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
, a
, NULL
);
2163 pal
= &d_right (*pal
);
2165 if (d_peek_char (di
) == 'E')
2172 di
->last_name
= hold_last_name
;
2177 /* <template-arg> ::= <type>
2178 ::= X <expression> E
2182 static struct demangle_component
*
2183 d_template_arg (struct d_info
*di
)
2185 struct demangle_component
*ret
;
2187 switch (d_peek_char (di
))
2191 ret
= d_expression (di
);
2192 if (d_next_char (di
) != 'E')
2197 return d_expr_primary (di
);
2200 return cplus_demangle_type (di
);
2204 /* <expression> ::= <(unary) operator-name> <expression>
2205 ::= <(binary) operator-name> <expression> <expression>
2206 ::= <(trinary) operator-name> <expression> <expression> <expression>
2208 ::= <template-param>
2209 ::= sr <type> <unqualified-name>
2210 ::= sr <type> <unqualified-name> <template-args>
2214 static struct demangle_component
*
2215 d_expression (struct d_info
*di
)
2219 peek
= d_peek_char (di
);
2221 return d_expr_primary (di
);
2222 else if (peek
== 'T')
2223 return d_template_param (di
);
2224 else if (peek
== 's' && d_peek_next_char (di
) == 'r')
2226 struct demangle_component
*type
;
2227 struct demangle_component
*name
;
2230 type
= cplus_demangle_type (di
);
2231 name
= d_unqualified_name (di
);
2232 if (d_peek_char (di
) != 'I')
2233 return d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
, type
, name
);
2235 return d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
, type
,
2236 d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, name
,
2237 d_template_args (di
)));
2241 struct demangle_component
*op
;
2244 op
= d_operator_name (di
);
2248 if (op
->type
== DEMANGLE_COMPONENT_OPERATOR
)
2249 di
->expansion
+= op
->u
.s_operator
.op
->len
- 2;
2251 if (op
->type
== DEMANGLE_COMPONENT_OPERATOR
2252 && strcmp (op
->u
.s_operator
.op
->code
, "st") == 0)
2253 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
2254 cplus_demangle_type (di
));
2260 case DEMANGLE_COMPONENT_OPERATOR
:
2261 args
= op
->u
.s_operator
.op
->args
;
2263 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
2264 args
= op
->u
.s_extended_operator
.args
;
2266 case DEMANGLE_COMPONENT_CAST
:
2274 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
2278 struct demangle_component
*left
;
2280 left
= d_expression (di
);
2281 return d_make_comp (di
, DEMANGLE_COMPONENT_BINARY
, op
,
2283 DEMANGLE_COMPONENT_BINARY_ARGS
,
2285 d_expression (di
)));
2289 struct demangle_component
*first
;
2290 struct demangle_component
*second
;
2292 first
= d_expression (di
);
2293 second
= d_expression (di
);
2294 return d_make_comp (di
, DEMANGLE_COMPONENT_TRINARY
, op
,
2296 DEMANGLE_COMPONENT_TRINARY_ARG1
,
2299 DEMANGLE_COMPONENT_TRINARY_ARG2
,
2301 d_expression (di
))));
2309 /* <expr-primary> ::= L <type> <(value) number> E
2310 ::= L <type> <(value) float> E
2311 ::= L <mangled-name> E
2314 static struct demangle_component
*
2315 d_expr_primary (struct d_info
*di
)
2317 struct demangle_component
*ret
;
2319 if (d_next_char (di
) != 'L')
2321 if (d_peek_char (di
) == '_')
2322 ret
= cplus_demangle_mangled_name (di
, 0);
2325 struct demangle_component
*type
;
2326 enum demangle_component_type t
;
2329 type
= cplus_demangle_type (di
);
2333 /* If we have a type we know how to print, we aren't going to
2334 print the type name itself. */
2335 if (type
->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
2336 && type
->u
.s_builtin
.type
->print
!= D_PRINT_DEFAULT
)
2337 di
->expansion
-= type
->u
.s_builtin
.type
->len
;
2339 /* Rather than try to interpret the literal value, we just
2340 collect it as a string. Note that it's possible to have a
2341 floating point literal here. The ABI specifies that the
2342 format of such literals is machine independent. That's fine,
2343 but what's not fine is that versions of g++ up to 3.2 with
2344 -fabi-version=1 used upper case letters in the hex constant,
2345 and dumped out gcc's internal representation. That makes it
2346 hard to tell where the constant ends, and hard to dump the
2347 constant in any readable form anyhow. We don't attempt to
2348 handle these cases. */
2350 t
= DEMANGLE_COMPONENT_LITERAL
;
2351 if (d_peek_char (di
) == 'n')
2353 t
= DEMANGLE_COMPONENT_LITERAL_NEG
;
2357 while (d_peek_char (di
) != 'E')
2359 if (d_peek_char (di
) == '\0')
2363 ret
= d_make_comp (di
, t
, type
, d_make_name (di
, s
, d_str (di
) - s
));
2365 if (d_next_char (di
) != 'E')
2370 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2371 ::= Z <(function) encoding> E s [<discriminator>]
2374 static struct demangle_component
*
2375 d_local_name (struct d_info
*di
)
2377 struct demangle_component
*function
;
2379 if (d_next_char (di
) != 'Z')
2382 function
= d_encoding (di
, 0);
2384 if (d_next_char (di
) != 'E')
2387 if (d_peek_char (di
) == 's')
2390 if (! d_discriminator (di
))
2392 return d_make_comp (di
, DEMANGLE_COMPONENT_LOCAL_NAME
, function
,
2393 d_make_name (di
, "string literal",
2394 sizeof "string literal" - 1));
2398 struct demangle_component
*name
;
2401 if (! d_discriminator (di
))
2403 return d_make_comp (di
, DEMANGLE_COMPONENT_LOCAL_NAME
, function
, name
);
2407 /* <discriminator> ::= _ <(non-negative) number>
2409 We demangle the discriminator, but we don't print it out. FIXME:
2410 We should print it out in verbose mode. */
2413 d_discriminator (struct d_info
*di
)
2417 if (d_peek_char (di
) != '_')
2420 discrim
= d_number (di
);
2426 /* Add a new substitution. */
2429 d_add_substitution (struct d_info
*di
, struct demangle_component
*dc
)
2433 if (di
->next_sub
>= di
->num_subs
)
2435 di
->subs
[di
->next_sub
] = dc
;
2440 /* <substitution> ::= S <seq-id> _
2450 If PREFIX is non-zero, then this type is being used as a prefix in
2451 a qualified name. In this case, for the standard substitutions, we
2452 need to check whether we are being used as a prefix for a
2453 constructor or destructor, and return a full template name.
2454 Otherwise we will get something like std::iostream::~iostream()
2455 which does not correspond particularly well to any function which
2456 actually appears in the source.
2459 static const struct d_standard_sub_info standard_subs
[] =
2464 { 'a', NL ("std::allocator"),
2465 NL ("std::allocator"),
2467 { 'b', NL ("std::basic_string"),
2468 NL ("std::basic_string"),
2469 NL ("basic_string") },
2470 { 's', NL ("std::string"),
2471 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
2472 NL ("basic_string") },
2473 { 'i', NL ("std::istream"),
2474 NL ("std::basic_istream<char, std::char_traits<char> >"),
2475 NL ("basic_istream") },
2476 { 'o', NL ("std::ostream"),
2477 NL ("std::basic_ostream<char, std::char_traits<char> >"),
2478 NL ("basic_ostream") },
2479 { 'd', NL ("std::iostream"),
2480 NL ("std::basic_iostream<char, std::char_traits<char> >"),
2481 NL ("basic_iostream") }
2484 static struct demangle_component
*
2485 d_substitution (struct d_info
*di
, int prefix
)
2489 if (d_next_char (di
) != 'S')
2492 c
= d_next_char (di
);
2493 if (c
== '_' || IS_DIGIT (c
) || IS_UPPER (c
))
2503 id
= id
* 36 + c
- '0';
2504 else if (IS_UPPER (c
))
2505 id
= id
* 36 + c
- 'A' + 10;
2508 c
= d_next_char (di
);
2515 if (id
>= di
->next_sub
)
2520 return di
->subs
[id
];
2525 const struct d_standard_sub_info
*p
;
2526 const struct d_standard_sub_info
*pend
;
2528 verbose
= (di
->options
& DMGL_VERBOSE
) != 0;
2529 if (! verbose
&& prefix
)
2533 peek
= d_peek_char (di
);
2534 if (peek
== 'C' || peek
== 'D')
2538 pend
= (&standard_subs
[0]
2539 + sizeof standard_subs
/ sizeof standard_subs
[0]);
2540 for (p
= &standard_subs
[0]; p
< pend
; ++p
)
2547 if (p
->set_last_name
!= NULL
)
2548 di
->last_name
= d_make_sub (di
, p
->set_last_name
,
2549 p
->set_last_name_len
);
2552 s
= p
->full_expansion
;
2557 s
= p
->simple_expansion
;
2558 len
= p
->simple_len
;
2560 di
->expansion
+= len
;
2561 return d_make_sub (di
, s
, len
);
2569 /* Resize the print buffer. */
2572 d_print_resize (struct d_print_info
*dpi
, size_t add
)
2576 if (dpi
->buf
== NULL
)
2578 need
= dpi
->len
+ add
;
2579 while (need
> dpi
->alc
)
2584 newalc
= dpi
->alc
* 2;
2585 newbuf
= (char *) realloc (dpi
->buf
, newalc
);
2590 dpi
->allocation_failure
= 1;
2598 /* Append a character to the print buffer. */
2601 d_print_append_char (struct d_print_info
*dpi
, int c
)
2603 if (dpi
->buf
!= NULL
)
2605 if (dpi
->len
>= dpi
->alc
)
2607 d_print_resize (dpi
, 1);
2608 if (dpi
->buf
== NULL
)
2612 dpi
->buf
[dpi
->len
] = c
;
2617 /* Append a buffer to the print buffer. */
2620 d_print_append_buffer (struct d_print_info
*dpi
, const char *s
, size_t l
)
2622 if (dpi
->buf
!= NULL
)
2624 if (dpi
->len
+ l
> dpi
->alc
)
2626 d_print_resize (dpi
, l
);
2627 if (dpi
->buf
== NULL
)
2631 memcpy (dpi
->buf
+ dpi
->len
, s
, l
);
2636 /* Indicate that an error occurred during printing. */
2639 d_print_error (struct d_print_info
*dpi
)
2645 /* Turn components into a human readable string. OPTIONS is the
2646 options bits passed to the demangler. DC is the tree to print.
2647 ESTIMATE is a guess at the length of the result. This returns a
2648 string allocated by malloc, or NULL on error. On success, this
2649 sets *PALC to the size of the allocated buffer. On failure, this
2650 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
2653 CP_STATIC_IF_GLIBCPP_V3
2655 cplus_demangle_print (int options
, const struct demangle_component
*dc
,
2656 int estimate
, size_t *palc
)
2658 struct d_print_info dpi
;
2660 dpi
.options
= options
;
2662 dpi
.alc
= estimate
+ 1;
2663 dpi
.buf
= (char *) malloc (dpi
.alc
);
2664 if (dpi
.buf
== NULL
)
2671 dpi
.templates
= NULL
;
2672 dpi
.modifiers
= NULL
;
2674 dpi
.allocation_failure
= 0;
2676 d_print_comp (&dpi
, dc
);
2678 d_append_char (&dpi
, '\0');
2680 if (dpi
.buf
!= NULL
)
2683 *palc
= dpi
.allocation_failure
;
2688 /* Subroutine to handle components. */
2691 d_print_comp (struct d_print_info
*dpi
,
2692 const struct demangle_component
*dc
)
2696 d_print_error (dpi
);
2699 if (d_print_saw_error (dpi
))
2704 case DEMANGLE_COMPONENT_NAME
:
2705 if ((dpi
->options
& DMGL_JAVA
) == 0)
2706 d_append_buffer (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
2708 d_print_java_identifier (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
2711 case DEMANGLE_COMPONENT_QUAL_NAME
:
2712 case DEMANGLE_COMPONENT_LOCAL_NAME
:
2713 d_print_comp (dpi
, d_left (dc
));
2714 if ((dpi
->options
& DMGL_JAVA
) == 0)
2715 d_append_string_constant (dpi
, "::");
2717 d_append_char (dpi
, '.');
2718 d_print_comp (dpi
, d_right (dc
));
2721 case DEMANGLE_COMPONENT_TYPED_NAME
:
2723 struct d_print_mod
*hold_modifiers
;
2724 struct demangle_component
*typed_name
;
2725 struct d_print_mod adpm
[4];
2727 struct d_print_template dpt
;
2729 /* Pass the name down to the type so that it can be printed in
2730 the right place for the type. We also have to pass down
2731 any CV-qualifiers, which apply to the this parameter. */
2732 hold_modifiers
= dpi
->modifiers
;
2734 typed_name
= d_left (dc
);
2735 while (typed_name
!= NULL
)
2737 if (i
>= sizeof adpm
/ sizeof adpm
[0])
2739 d_print_error (dpi
);
2743 adpm
[i
].next
= dpi
->modifiers
;
2744 dpi
->modifiers
= &adpm
[i
];
2745 adpm
[i
].mod
= typed_name
;
2746 adpm
[i
].printed
= 0;
2747 adpm
[i
].templates
= dpi
->templates
;
2750 if (typed_name
->type
!= DEMANGLE_COMPONENT_RESTRICT_THIS
2751 && typed_name
->type
!= DEMANGLE_COMPONENT_VOLATILE_THIS
2752 && typed_name
->type
!= DEMANGLE_COMPONENT_CONST_THIS
)
2755 typed_name
= d_left (typed_name
);
2758 /* If typed_name is a template, then it applies to the
2759 function type as well. */
2760 if (typed_name
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
2762 dpt
.next
= dpi
->templates
;
2763 dpi
->templates
= &dpt
;
2764 dpt
.template_decl
= typed_name
;
2767 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
2768 there may be CV-qualifiers on its right argument which
2769 really apply here; this happens when parsing a class which
2770 is local to a function. */
2771 if (typed_name
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
2773 struct demangle_component
*local_name
;
2775 local_name
= d_right (typed_name
);
2776 while (local_name
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
2777 || local_name
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
2778 || local_name
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
2780 if (i
>= sizeof adpm
/ sizeof adpm
[0])
2782 d_print_error (dpi
);
2786 adpm
[i
] = adpm
[i
- 1];
2787 adpm
[i
].next
= &adpm
[i
- 1];
2788 dpi
->modifiers
= &adpm
[i
];
2790 adpm
[i
- 1].mod
= local_name
;
2791 adpm
[i
- 1].printed
= 0;
2792 adpm
[i
- 1].templates
= dpi
->templates
;
2795 local_name
= d_left (local_name
);
2799 d_print_comp (dpi
, d_right (dc
));
2801 if (typed_name
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
2802 dpi
->templates
= dpt
.next
;
2804 /* If the modifiers didn't get printed by the type, print them
2809 if (! adpm
[i
].printed
)
2811 d_append_char (dpi
, ' ');
2812 d_print_mod (dpi
, adpm
[i
].mod
);
2816 dpi
->modifiers
= hold_modifiers
;
2821 case DEMANGLE_COMPONENT_TEMPLATE
:
2823 struct d_print_mod
*hold_dpm
;
2825 /* Don't push modifiers into a template definition. Doing so
2826 could give the wrong definition for a template argument.
2827 Instead, treat the template essentially as a name. */
2829 hold_dpm
= dpi
->modifiers
;
2830 dpi
->modifiers
= NULL
;
2832 d_print_comp (dpi
, d_left (dc
));
2833 if (d_last_char (dpi
) == '<')
2834 d_append_char (dpi
, ' ');
2835 d_append_char (dpi
, '<');
2836 d_print_comp (dpi
, d_right (dc
));
2837 /* Avoid generating two consecutive '>' characters, to avoid
2838 the C++ syntactic ambiguity. */
2839 if (d_last_char (dpi
) == '>')
2840 d_append_char (dpi
, ' ');
2841 d_append_char (dpi
, '>');
2843 dpi
->modifiers
= hold_dpm
;
2848 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
2851 struct demangle_component
*a
;
2852 struct d_print_template
*hold_dpt
;
2854 if (dpi
->templates
== NULL
)
2856 d_print_error (dpi
);
2859 i
= dc
->u
.s_number
.number
;
2860 for (a
= d_right (dpi
->templates
->template_decl
);
2864 if (a
->type
!= DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
2866 d_print_error (dpi
);
2873 if (i
!= 0 || a
== NULL
)
2875 d_print_error (dpi
);
2879 /* While processing this parameter, we need to pop the list of
2880 templates. This is because the template parameter may
2881 itself be a reference to a parameter of an outer
2884 hold_dpt
= dpi
->templates
;
2885 dpi
->templates
= hold_dpt
->next
;
2887 d_print_comp (dpi
, d_left (a
));
2889 dpi
->templates
= hold_dpt
;
2894 case DEMANGLE_COMPONENT_CTOR
:
2895 d_print_comp (dpi
, dc
->u
.s_ctor
.name
);
2898 case DEMANGLE_COMPONENT_DTOR
:
2899 d_append_char (dpi
, '~');
2900 d_print_comp (dpi
, dc
->u
.s_dtor
.name
);
2903 case DEMANGLE_COMPONENT_VTABLE
:
2904 d_append_string_constant (dpi
, "vtable for ");
2905 d_print_comp (dpi
, d_left (dc
));
2908 case DEMANGLE_COMPONENT_VTT
:
2909 d_append_string_constant (dpi
, "VTT for ");
2910 d_print_comp (dpi
, d_left (dc
));
2913 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
2914 d_append_string_constant (dpi
, "construction vtable for ");
2915 d_print_comp (dpi
, d_left (dc
));
2916 d_append_string_constant (dpi
, "-in-");
2917 d_print_comp (dpi
, d_right (dc
));
2920 case DEMANGLE_COMPONENT_TYPEINFO
:
2921 d_append_string_constant (dpi
, "typeinfo for ");
2922 d_print_comp (dpi
, d_left (dc
));
2925 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
2926 d_append_string_constant (dpi
, "typeinfo name for ");
2927 d_print_comp (dpi
, d_left (dc
));
2930 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
2931 d_append_string_constant (dpi
, "typeinfo fn for ");
2932 d_print_comp (dpi
, d_left (dc
));
2935 case DEMANGLE_COMPONENT_THUNK
:
2936 d_append_string_constant (dpi
, "non-virtual thunk to ");
2937 d_print_comp (dpi
, d_left (dc
));
2940 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
2941 d_append_string_constant (dpi
, "virtual thunk to ");
2942 d_print_comp (dpi
, d_left (dc
));
2945 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
2946 d_append_string_constant (dpi
, "covariant return thunk to ");
2947 d_print_comp (dpi
, d_left (dc
));
2950 case DEMANGLE_COMPONENT_JAVA_CLASS
:
2951 d_append_string_constant (dpi
, "java Class for ");
2952 d_print_comp (dpi
, d_left (dc
));
2955 case DEMANGLE_COMPONENT_GUARD
:
2956 d_append_string_constant (dpi
, "guard variable for ");
2957 d_print_comp (dpi
, d_left (dc
));
2960 case DEMANGLE_COMPONENT_REFTEMP
:
2961 d_append_string_constant (dpi
, "reference temporary for ");
2962 d_print_comp (dpi
, d_left (dc
));
2965 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
2966 d_append_string_constant (dpi
, "hidden alias for ");
2967 d_print_comp (dpi
, d_left (dc
));
2970 case DEMANGLE_COMPONENT_SUB_STD
:
2971 d_append_buffer (dpi
, dc
->u
.s_string
.string
, dc
->u
.s_string
.len
);
2974 case DEMANGLE_COMPONENT_RESTRICT
:
2975 case DEMANGLE_COMPONENT_VOLATILE
:
2976 case DEMANGLE_COMPONENT_CONST
:
2978 struct d_print_mod
*pdpm
;
2980 /* When printing arrays, it's possible to have cases where the
2981 same CV-qualifier gets pushed on the stack multiple times.
2982 We only need to print it once. */
2984 for (pdpm
= dpi
->modifiers
; pdpm
!= NULL
; pdpm
= pdpm
->next
)
2986 if (! pdpm
->printed
)
2988 if (pdpm
->mod
->type
!= DEMANGLE_COMPONENT_RESTRICT
2989 && pdpm
->mod
->type
!= DEMANGLE_COMPONENT_VOLATILE
2990 && pdpm
->mod
->type
!= DEMANGLE_COMPONENT_CONST
)
2992 if (pdpm
->mod
->type
== dc
->type
)
2994 d_print_comp (dpi
, d_left (dc
));
3001 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
3002 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
3003 case DEMANGLE_COMPONENT_CONST_THIS
:
3004 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
3005 case DEMANGLE_COMPONENT_POINTER
:
3006 case DEMANGLE_COMPONENT_REFERENCE
:
3007 case DEMANGLE_COMPONENT_COMPLEX
:
3008 case DEMANGLE_COMPONENT_IMAGINARY
:
3010 /* We keep a list of modifiers on the stack. */
3011 struct d_print_mod dpm
;
3013 dpm
.next
= dpi
->modifiers
;
3014 dpi
->modifiers
= &dpm
;
3017 dpm
.templates
= dpi
->templates
;
3019 d_print_comp (dpi
, d_left (dc
));
3021 /* If the modifier didn't get printed by the type, print it
3024 d_print_mod (dpi
, dc
);
3026 dpi
->modifiers
= dpm
.next
;
3031 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
3032 if ((dpi
->options
& DMGL_JAVA
) == 0)
3033 d_append_buffer (dpi
, dc
->u
.s_builtin
.type
->name
,
3034 dc
->u
.s_builtin
.type
->len
);
3036 d_append_buffer (dpi
, dc
->u
.s_builtin
.type
->java_name
,
3037 dc
->u
.s_builtin
.type
->java_len
);
3040 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
3041 d_print_comp (dpi
, d_left (dc
));
3044 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
3046 if ((dpi
->options
& DMGL_RET_POSTFIX
) != 0)
3047 d_print_function_type (dpi
, dc
, dpi
->modifiers
);
3049 /* Print return type if present */
3050 if (d_left (dc
) != NULL
)
3052 struct d_print_mod dpm
;
3054 /* We must pass this type down as a modifier in order to
3055 print it in the right location. */
3056 dpm
.next
= dpi
->modifiers
;
3057 dpi
->modifiers
= &dpm
;
3060 dpm
.templates
= dpi
->templates
;
3062 d_print_comp (dpi
, d_left (dc
));
3064 dpi
->modifiers
= dpm
.next
;
3069 /* In standard prefix notation, there is a space between the
3070 return type and the function signature. */
3071 if ((dpi
->options
& DMGL_RET_POSTFIX
) == 0)
3072 d_append_char (dpi
, ' ');
3075 if ((dpi
->options
& DMGL_RET_POSTFIX
) == 0)
3076 d_print_function_type (dpi
, dc
, dpi
->modifiers
);
3081 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
3083 struct d_print_mod
*hold_modifiers
;
3084 struct d_print_mod adpm
[4];
3086 struct d_print_mod
*pdpm
;
3088 /* We must pass this type down as a modifier in order to print
3089 multi-dimensional arrays correctly. If the array itself is
3090 CV-qualified, we act as though the element type were
3091 CV-qualified. We do this by copying the modifiers down
3092 rather than fiddling pointers, so that we don't wind up
3093 with a d_print_mod higher on the stack pointing into our
3094 stack frame after we return. */
3096 hold_modifiers
= dpi
->modifiers
;
3098 adpm
[0].next
= hold_modifiers
;
3099 dpi
->modifiers
= &adpm
[0];
3101 adpm
[0].printed
= 0;
3102 adpm
[0].templates
= dpi
->templates
;
3105 pdpm
= hold_modifiers
;
3107 && (pdpm
->mod
->type
== DEMANGLE_COMPONENT_RESTRICT
3108 || pdpm
->mod
->type
== DEMANGLE_COMPONENT_VOLATILE
3109 || pdpm
->mod
->type
== DEMANGLE_COMPONENT_CONST
))
3111 if (! pdpm
->printed
)
3113 if (i
>= sizeof adpm
/ sizeof adpm
[0])
3115 d_print_error (dpi
);
3120 adpm
[i
].next
= dpi
->modifiers
;
3121 dpi
->modifiers
= &adpm
[i
];
3129 d_print_comp (dpi
, d_right (dc
));
3131 dpi
->modifiers
= hold_modifiers
;
3133 if (adpm
[0].printed
)
3139 d_print_mod (dpi
, adpm
[i
].mod
);
3142 d_print_array_type (dpi
, dc
, dpi
->modifiers
);
3147 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
3149 struct d_print_mod dpm
;
3151 dpm
.next
= dpi
->modifiers
;
3152 dpi
->modifiers
= &dpm
;
3155 dpm
.templates
= dpi
->templates
;
3157 d_print_comp (dpi
, d_right (dc
));
3159 /* If the modifier didn't get printed by the type, print it
3163 d_append_char (dpi
, ' ');
3164 d_print_comp (dpi
, d_left (dc
));
3165 d_append_string_constant (dpi
, "::*");
3168 dpi
->modifiers
= dpm
.next
;
3173 case DEMANGLE_COMPONENT_ARGLIST
:
3174 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
3175 d_print_comp (dpi
, d_left (dc
));
3176 if (d_right (dc
) != NULL
)
3178 d_append_string_constant (dpi
, ", ");
3179 d_print_comp (dpi
, d_right (dc
));
3183 case DEMANGLE_COMPONENT_OPERATOR
:
3187 d_append_string_constant (dpi
, "operator");
3188 c
= dc
->u
.s_operator
.op
->name
[0];
3190 d_append_char (dpi
, ' ');
3191 d_append_buffer (dpi
, dc
->u
.s_operator
.op
->name
,
3192 dc
->u
.s_operator
.op
->len
);
3196 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
3197 d_append_string_constant (dpi
, "operator ");
3198 d_print_comp (dpi
, dc
->u
.s_extended_operator
.name
);
3201 case DEMANGLE_COMPONENT_CAST
:
3202 d_append_string_constant (dpi
, "operator ");
3203 d_print_cast (dpi
, dc
);
3206 case DEMANGLE_COMPONENT_UNARY
:
3207 if (d_left (dc
)->type
!= DEMANGLE_COMPONENT_CAST
)
3208 d_print_expr_op (dpi
, d_left (dc
));
3211 d_append_char (dpi
, '(');
3212 d_print_cast (dpi
, d_left (dc
));
3213 d_append_char (dpi
, ')');
3215 d_append_char (dpi
, '(');
3216 d_print_comp (dpi
, d_right (dc
));
3217 d_append_char (dpi
, ')');
3220 case DEMANGLE_COMPONENT_BINARY
:
3221 if (d_right (dc
)->type
!= DEMANGLE_COMPONENT_BINARY_ARGS
)
3223 d_print_error (dpi
);
3227 /* We wrap an expression which uses the greater-than operator in
3228 an extra layer of parens so that it does not get confused
3229 with the '>' which ends the template parameters. */
3230 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_OPERATOR
3231 && d_left (dc
)->u
.s_operator
.op
->len
== 1
3232 && d_left (dc
)->u
.s_operator
.op
->name
[0] == '>')
3233 d_append_char (dpi
, '(');
3235 d_append_char (dpi
, '(');
3236 d_print_comp (dpi
, d_left (d_right (dc
)));
3237 d_append_string_constant (dpi
, ") ");
3238 d_print_expr_op (dpi
, d_left (dc
));
3239 d_append_string_constant (dpi
, " (");
3240 d_print_comp (dpi
, d_right (d_right (dc
)));
3241 d_append_char (dpi
, ')');
3243 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_OPERATOR
3244 && d_left (dc
)->u
.s_operator
.op
->len
== 1
3245 && d_left (dc
)->u
.s_operator
.op
->name
[0] == '>')
3246 d_append_char (dpi
, ')');
3250 case DEMANGLE_COMPONENT_BINARY_ARGS
:
3251 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
3252 d_print_error (dpi
);
3255 case DEMANGLE_COMPONENT_TRINARY
:
3256 if (d_right (dc
)->type
!= DEMANGLE_COMPONENT_TRINARY_ARG1
3257 || d_right (d_right (dc
))->type
!= DEMANGLE_COMPONENT_TRINARY_ARG2
)
3259 d_print_error (dpi
);
3262 d_append_char (dpi
, '(');
3263 d_print_comp (dpi
, d_left (d_right (dc
)));
3264 d_append_string_constant (dpi
, ") ");
3265 d_print_expr_op (dpi
, d_left (dc
));
3266 d_append_string_constant (dpi
, " (");
3267 d_print_comp (dpi
, d_left (d_right (d_right (dc
))));
3268 d_append_string_constant (dpi
, ") : (");
3269 d_print_comp (dpi
, d_right (d_right (d_right (dc
))));
3270 d_append_char (dpi
, ')');
3273 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
3274 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
3275 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
3276 d_print_error (dpi
);
3279 case DEMANGLE_COMPONENT_LITERAL
:
3280 case DEMANGLE_COMPONENT_LITERAL_NEG
:
3282 enum d_builtin_type_print tp
;
3284 /* For some builtin types, produce simpler output. */
3285 tp
= D_PRINT_DEFAULT
;
3286 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
)
3288 tp
= d_left (dc
)->u
.s_builtin
.type
->print
;
3292 case D_PRINT_UNSIGNED
:
3294 case D_PRINT_UNSIGNED_LONG
:
3295 case D_PRINT_LONG_LONG
:
3296 case D_PRINT_UNSIGNED_LONG_LONG
:
3297 if (d_right (dc
)->type
== DEMANGLE_COMPONENT_NAME
)
3299 if (dc
->type
== DEMANGLE_COMPONENT_LITERAL_NEG
)
3300 d_append_char (dpi
, '-');
3301 d_print_comp (dpi
, d_right (dc
));
3306 case D_PRINT_UNSIGNED
:
3307 d_append_char (dpi
, 'u');
3310 d_append_char (dpi
, 'l');
3312 case D_PRINT_UNSIGNED_LONG
:
3313 d_append_string_constant (dpi
, "ul");
3315 case D_PRINT_LONG_LONG
:
3316 d_append_string_constant (dpi
, "ll");
3318 case D_PRINT_UNSIGNED_LONG_LONG
:
3319 d_append_string_constant (dpi
, "ull");
3327 if (d_right (dc
)->type
== DEMANGLE_COMPONENT_NAME
3328 && d_right (dc
)->u
.s_name
.len
== 1
3329 && dc
->type
== DEMANGLE_COMPONENT_LITERAL
)
3331 switch (d_right (dc
)->u
.s_name
.s
[0])
3334 d_append_string_constant (dpi
, "false");
3337 d_append_string_constant (dpi
, "true");
3350 d_append_char (dpi
, '(');
3351 d_print_comp (dpi
, d_left (dc
));
3352 d_append_char (dpi
, ')');
3353 if (dc
->type
== DEMANGLE_COMPONENT_LITERAL_NEG
)
3354 d_append_char (dpi
, '-');
3355 if (tp
== D_PRINT_FLOAT
)
3356 d_append_char (dpi
, '[');
3357 d_print_comp (dpi
, d_right (dc
));
3358 if (tp
== D_PRINT_FLOAT
)
3359 d_append_char (dpi
, ']');
3364 d_print_error (dpi
);
3369 /* Print a Java dentifier. For Java we try to handle encoded extended
3370 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
3371 so we don't it for C++. Characters are encoded as
3375 d_print_java_identifier (struct d_print_info
*dpi
, const char *name
, int len
)
3381 for (p
= name
; p
< end
; ++p
)
3392 for (q
= p
+ 3; q
< end
; ++q
)
3398 else if (*q
>= 'A' && *q
<= 'F')
3399 dig
= *q
- 'A' + 10;
3400 else if (*q
>= 'a' && *q
<= 'f')
3401 dig
= *q
- 'a' + 10;
3407 /* If the Unicode character is larger than 256, we don't try
3408 to deal with it here. FIXME. */
3409 if (q
< end
&& *q
== '_' && c
< 256)
3411 d_append_char (dpi
, c
);
3417 d_append_char (dpi
, *p
);
3421 /* Print a list of modifiers. SUFFIX is 1 if we are printing
3422 qualifiers on this after printing a function. */
3425 d_print_mod_list (struct d_print_info
*dpi
,
3426 struct d_print_mod
*mods
, int suffix
)
3428 struct d_print_template
*hold_dpt
;
3430 if (mods
== NULL
|| d_print_saw_error (dpi
))
3435 && (mods
->mod
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
3436 || mods
->mod
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
3437 || mods
->mod
->type
== DEMANGLE_COMPONENT_CONST_THIS
)))
3439 d_print_mod_list (dpi
, mods
->next
, suffix
);
3445 hold_dpt
= dpi
->templates
;
3446 dpi
->templates
= mods
->templates
;
3448 if (mods
->mod
->type
== DEMANGLE_COMPONENT_FUNCTION_TYPE
)
3450 d_print_function_type (dpi
, mods
->mod
, mods
->next
);
3451 dpi
->templates
= hold_dpt
;
3454 else if (mods
->mod
->type
== DEMANGLE_COMPONENT_ARRAY_TYPE
)
3456 d_print_array_type (dpi
, mods
->mod
, mods
->next
);
3457 dpi
->templates
= hold_dpt
;
3460 else if (mods
->mod
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
3462 struct d_print_mod
*hold_modifiers
;
3463 struct demangle_component
*dc
;
3465 /* When this is on the modifier stack, we have pulled any
3466 qualifiers off the right argument already. Otherwise, we
3467 print it as usual, but don't let the left argument see any
3470 hold_modifiers
= dpi
->modifiers
;
3471 dpi
->modifiers
= NULL
;
3472 d_print_comp (dpi
, d_left (mods
->mod
));
3473 dpi
->modifiers
= hold_modifiers
;
3475 if ((dpi
->options
& DMGL_JAVA
) == 0)
3476 d_append_string_constant (dpi
, "::");
3478 d_append_char (dpi
, '.');
3480 dc
= d_right (mods
->mod
);
3481 while (dc
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
3482 || dc
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
3483 || dc
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
3486 d_print_comp (dpi
, dc
);
3488 dpi
->templates
= hold_dpt
;
3492 d_print_mod (dpi
, mods
->mod
);
3494 dpi
->templates
= hold_dpt
;
3496 d_print_mod_list (dpi
, mods
->next
, suffix
);
3499 /* Print a modifier. */
3502 d_print_mod (struct d_print_info
*dpi
,
3503 const struct demangle_component
*mod
)
3507 case DEMANGLE_COMPONENT_RESTRICT
:
3508 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
3509 d_append_string_constant (dpi
, " restrict");
3511 case DEMANGLE_COMPONENT_VOLATILE
:
3512 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
3513 d_append_string_constant (dpi
, " volatile");
3515 case DEMANGLE_COMPONENT_CONST
:
3516 case DEMANGLE_COMPONENT_CONST_THIS
:
3517 d_append_string_constant (dpi
, " const");
3519 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
3520 d_append_char (dpi
, ' ');
3521 d_print_comp (dpi
, d_right (mod
));
3523 case DEMANGLE_COMPONENT_POINTER
:
3524 /* There is no pointer symbol in Java. */
3525 if ((dpi
->options
& DMGL_JAVA
) == 0)
3526 d_append_char (dpi
, '*');
3528 case DEMANGLE_COMPONENT_REFERENCE
:
3529 d_append_char (dpi
, '&');
3531 case DEMANGLE_COMPONENT_COMPLEX
:
3532 d_append_string_constant (dpi
, "complex ");
3534 case DEMANGLE_COMPONENT_IMAGINARY
:
3535 d_append_string_constant (dpi
, "imaginary ");
3537 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
3538 if (d_last_char (dpi
) != '(')
3539 d_append_char (dpi
, ' ');
3540 d_print_comp (dpi
, d_left (mod
));
3541 d_append_string_constant (dpi
, "::*");
3543 case DEMANGLE_COMPONENT_TYPED_NAME
:
3544 d_print_comp (dpi
, d_left (mod
));
3547 /* Otherwise, we have something that won't go back on the
3548 modifier stack, so we can just print it. */
3549 d_print_comp (dpi
, mod
);
3554 /* Print a function type, except for the return type. */
3557 d_print_function_type (struct d_print_info
*dpi
,
3558 const struct demangle_component
*dc
,
3559 struct d_print_mod
*mods
)
3564 struct d_print_mod
*p
;
3565 struct d_print_mod
*hold_modifiers
;
3570 for (p
= mods
; p
!= NULL
; p
= p
->next
)
3576 switch (p
->mod
->type
)
3578 case DEMANGLE_COMPONENT_POINTER
:
3579 case DEMANGLE_COMPONENT_REFERENCE
:
3582 case DEMANGLE_COMPONENT_RESTRICT
:
3583 case DEMANGLE_COMPONENT_VOLATILE
:
3584 case DEMANGLE_COMPONENT_CONST
:
3585 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
3586 case DEMANGLE_COMPONENT_COMPLEX
:
3587 case DEMANGLE_COMPONENT_IMAGINARY
:
3588 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
3592 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
3593 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
3594 case DEMANGLE_COMPONENT_CONST_THIS
:
3603 if (d_left (dc
) != NULL
&& ! saw_mod
)
3610 if (d_last_char (dpi
) != '('
3611 && d_last_char (dpi
) != '*')
3614 if (need_space
&& d_last_char (dpi
) != ' ')
3615 d_append_char (dpi
, ' ');
3616 d_append_char (dpi
, '(');
3619 hold_modifiers
= dpi
->modifiers
;
3620 dpi
->modifiers
= NULL
;
3622 d_print_mod_list (dpi
, mods
, 0);
3625 d_append_char (dpi
, ')');
3627 d_append_char (dpi
, '(');
3629 if (d_right (dc
) != NULL
)
3630 d_print_comp (dpi
, d_right (dc
));
3632 d_append_char (dpi
, ')');
3634 d_print_mod_list (dpi
, mods
, 1);
3636 dpi
->modifiers
= hold_modifiers
;
3639 /* Print an array type, except for the element type. */
3642 d_print_array_type (struct d_print_info
*dpi
,
3643 const struct demangle_component
*dc
,
3644 struct d_print_mod
*mods
)
3652 struct d_print_mod
*p
;
3655 for (p
= mods
; p
!= NULL
; p
= p
->next
)
3659 if (p
->mod
->type
== DEMANGLE_COMPONENT_ARRAY_TYPE
)
3674 d_append_string_constant (dpi
, " (");
3676 d_print_mod_list (dpi
, mods
, 0);
3679 d_append_char (dpi
, ')');
3683 d_append_char (dpi
, ' ');
3685 d_append_char (dpi
, '[');
3687 if (d_left (dc
) != NULL
)
3688 d_print_comp (dpi
, d_left (dc
));
3690 d_append_char (dpi
, ']');
3693 /* Print an operator in an expression. */
3696 d_print_expr_op (struct d_print_info
*dpi
,
3697 const struct demangle_component
*dc
)
3699 if (dc
->type
== DEMANGLE_COMPONENT_OPERATOR
)
3700 d_append_buffer (dpi
, dc
->u
.s_operator
.op
->name
,
3701 dc
->u
.s_operator
.op
->len
);
3703 d_print_comp (dpi
, dc
);
3709 d_print_cast (struct d_print_info
*dpi
,
3710 const struct demangle_component
*dc
)
3712 if (d_left (dc
)->type
!= DEMANGLE_COMPONENT_TEMPLATE
)
3713 d_print_comp (dpi
, d_left (dc
));
3716 struct d_print_mod
*hold_dpm
;
3717 struct d_print_template dpt
;
3719 /* It appears that for a templated cast operator, we need to put
3720 the template parameters in scope for the operator name, but
3721 not for the parameters. The effect is that we need to handle
3722 the template printing here. */
3724 hold_dpm
= dpi
->modifiers
;
3725 dpi
->modifiers
= NULL
;
3727 dpt
.next
= dpi
->templates
;
3728 dpi
->templates
= &dpt
;
3729 dpt
.template_decl
= d_left (dc
);
3731 d_print_comp (dpi
, d_left (d_left (dc
)));
3733 dpi
->templates
= dpt
.next
;
3735 if (d_last_char (dpi
) == '<')
3736 d_append_char (dpi
, ' ');
3737 d_append_char (dpi
, '<');
3738 d_print_comp (dpi
, d_right (d_left (dc
)));
3739 /* Avoid generating two consecutive '>' characters, to avoid
3740 the C++ syntactic ambiguity. */
3741 if (d_last_char (dpi
) == '>')
3742 d_append_char (dpi
, ' ');
3743 d_append_char (dpi
, '>');
3745 dpi
->modifiers
= hold_dpm
;
3749 /* Initialize the information structure we use to pass around
3752 CP_STATIC_IF_GLIBCPP_V3
3754 cplus_demangle_init_info (const char *mangled
, int options
, size_t len
,
3758 di
->send
= mangled
+ len
;
3759 di
->options
= options
;
3763 /* We can not need more components than twice the number of chars in
3764 the mangled string. Most components correspond directly to
3765 chars, but the ARGLIST types are exceptions. */
3766 di
->num_comps
= 2 * len
;
3769 /* Similarly, we can not need more substitutions than there are
3770 chars in the mangled string. */
3775 di
->last_name
= NULL
;
3780 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
3781 name, return a buffer allocated with malloc holding the demangled
3782 name. OPTIONS is the usual libiberty demangler options. On
3783 success, this sets *PALC to the allocated size of the returned
3784 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
3785 a memory allocation failure. On failure, this returns NULL. */
3788 d_demangle (const char* mangled
, int options
, size_t *palc
)
3793 struct demangle_component
*dc
;
3799 len
= strlen (mangled
);
3801 if (mangled
[0] == '_' && mangled
[1] == 'Z')
3803 else if (strncmp (mangled
, "_GLOBAL_", 8) == 0
3804 && (mangled
[8] == '.' || mangled
[8] == '_' || mangled
[8] == '$')
3805 && (mangled
[9] == 'D' || mangled
[9] == 'I')
3806 && mangled
[10] == '_')
3810 r
= (char *) malloc (40 + len
- 11);
3815 if (mangled
[9] == 'I')
3816 strcpy (r
, "global constructors keyed to ");
3818 strcpy (r
, "global destructors keyed to ");
3819 strcat (r
, mangled
+ 11);
3825 if ((options
& DMGL_TYPES
) == 0)
3830 cplus_demangle_init_info (mangled
, options
, len
, &di
);
3833 #ifdef CP_DYNAMIC_ARRAYS
3834 __extension__
struct demangle_component comps
[di
.num_comps
];
3835 __extension__
struct demangle_component
*subs
[di
.num_subs
];
3837 di
.comps
= &comps
[0];
3840 di
.comps
= ((struct demangle_component
*)
3841 malloc (di
.num_comps
* sizeof (struct demangle_component
)));
3842 di
.subs
= ((struct demangle_component
**)
3843 malloc (di
.num_subs
* sizeof (struct demangle_component
*)));
3844 if (di
.comps
== NULL
|| di
.subs
== NULL
)
3846 if (di
.comps
!= NULL
)
3848 if (di
.subs
!= NULL
)
3856 dc
= cplus_demangle_mangled_name (&di
, 1);
3858 dc
= cplus_demangle_type (&di
);
3860 /* If DMGL_PARAMS is set, then if we didn't consume the entire
3861 mangled string, then we didn't successfully demangle it. If
3862 DMGL_PARAMS is not set, we didn't look at the trailing
3864 if (((options
& DMGL_PARAMS
) != 0) && d_peek_char (&di
) != '\0')
3867 #ifdef CP_DEMANGLE_DEBUG
3869 printf ("failed demangling\n");
3874 /* We try to guess the length of the demangled string, to minimize
3875 calls to realloc during demangling. */
3876 estimate
= len
+ di
.expansion
+ 10 * di
.did_subs
;
3877 estimate
+= estimate
/ 8;
3881 ret
= cplus_demangle_print (options
, dc
, estimate
, palc
);
3883 #ifndef CP_DYNAMIC_ARRAYS
3888 #ifdef CP_DEMANGLE_DEBUG
3893 rlen
= strlen (ret
);
3894 if (rlen
> 2 * estimate
)
3895 printf ("*** Length %d much greater than estimate %d\n",
3897 else if (rlen
> estimate
)
3898 printf ("*** Length %d greater than estimate %d\n",
3900 else if (rlen
< estimate
/ 2)
3901 printf ("*** Length %d much less than estimate %d\n",
3910 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
3912 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
3914 /* ia64 ABI-mandated entry point in the C++ runtime library for
3915 performing demangling. MANGLED_NAME is a NUL-terminated character
3916 string containing the name to be demangled.
3918 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
3919 *LENGTH bytes, into which the demangled name is stored. If
3920 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
3921 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
3922 is placed in a region of memory allocated with malloc.
3924 If LENGTH is non-NULL, the length of the buffer conaining the
3925 demangled name, is placed in *LENGTH.
3927 The return value is a pointer to the start of the NUL-terminated
3928 demangled name, or NULL if the demangling fails. The caller is
3929 responsible for deallocating this memory using free.
3931 *STATUS is set to one of the following values:
3932 0: The demangling operation succeeded.
3933 -1: A memory allocation failure occurred.
3934 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
3935 -3: One of the arguments is invalid.
3937 The demangling is performed using the C++ ABI mangling rules, with
3941 __cxa_demangle (const char *mangled_name
, char *output_buffer
,
3942 size_t *length
, int *status
)
3947 if (mangled_name
== NULL
)
3954 if (output_buffer
!= NULL
&& length
== NULL
)
3961 demangled
= d_demangle (mangled_name
, DMGL_PARAMS
| DMGL_TYPES
, &alc
);
3963 if (demangled
== NULL
)
3975 if (output_buffer
== NULL
)
3982 if (strlen (demangled
) < *length
)
3984 strcpy (output_buffer
, demangled
);
3986 demangled
= output_buffer
;
3990 free (output_buffer
);
4001 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
4003 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
4004 mangled name, return a buffer allocated with malloc holding the
4005 demangled name. Otherwise, return NULL. */
4008 cplus_demangle_v3 (const char* mangled
, int options
)
4012 return d_demangle (mangled
, options
, &alc
);
4015 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
4016 conventions, but the output formatting is a little different.
4017 This instructs the C++ demangler not to emit pointer characters ("*"), and
4018 to use Java's namespace separator symbol ("." instead of "::"). It then
4019 does an additional pass over the demangled output to replace instances
4020 of JArray<TYPE> with TYPE[]. */
4023 java_demangle_v3 (const char* mangled
)
4031 demangled
= d_demangle (mangled
, DMGL_JAVA
| DMGL_PARAMS
| DMGL_RET_POSTFIX
,
4034 if (demangled
== NULL
)
4040 while (*from
!= '\0')
4042 if (strncmp (from
, "JArray<", 7) == 0)
4047 else if (nesting
> 0 && *from
== '>')
4049 while (to
> demangled
&& to
[-1] == ' ')
4065 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
4067 #ifndef IN_GLIBCPP_V3
4069 /* Demangle a string in order to find out whether it is a constructor
4070 or destructor. Return non-zero on success. Set *CTOR_KIND and
4071 *DTOR_KIND appropriately. */
4074 is_ctor_or_dtor (const char *mangled
,
4075 enum gnu_v3_ctor_kinds
*ctor_kind
,
4076 enum gnu_v3_dtor_kinds
*dtor_kind
)
4079 struct demangle_component
*dc
;
4082 *ctor_kind
= (enum gnu_v3_ctor_kinds
) 0;
4083 *dtor_kind
= (enum gnu_v3_dtor_kinds
) 0;
4085 cplus_demangle_init_info (mangled
, DMGL_GNU_V3
, strlen (mangled
), &di
);
4088 #ifdef CP_DYNAMIC_ARRAYS
4089 __extension__
struct demangle_component comps
[di
.num_comps
];
4090 __extension__
struct demangle_component
*subs
[di
.num_subs
];
4092 di
.comps
= &comps
[0];
4095 di
.comps
= ((struct demangle_component
*)
4096 malloc (di
.num_comps
* sizeof (struct demangle_component
)));
4097 di
.subs
= ((struct demangle_component
**)
4098 malloc (di
.num_subs
* sizeof (struct demangle_component
*)));
4099 if (di
.comps
== NULL
|| di
.subs
== NULL
)
4101 if (di
.comps
!= NULL
)
4103 if (di
.subs
!= NULL
)
4109 dc
= cplus_demangle_mangled_name (&di
, 1);
4111 /* Note that because we did not pass DMGL_PARAMS, we don't expect
4112 to demangle the entire string. */
4122 case DEMANGLE_COMPONENT_TYPED_NAME
:
4123 case DEMANGLE_COMPONENT_TEMPLATE
:
4124 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
4125 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
4126 case DEMANGLE_COMPONENT_CONST_THIS
:
4129 case DEMANGLE_COMPONENT_QUAL_NAME
:
4130 case DEMANGLE_COMPONENT_LOCAL_NAME
:
4133 case DEMANGLE_COMPONENT_CTOR
:
4134 *ctor_kind
= dc
->u
.s_ctor
.kind
;
4138 case DEMANGLE_COMPONENT_DTOR
:
4139 *dtor_kind
= dc
->u
.s_dtor
.kind
;
4146 #ifndef CP_DYNAMIC_ARRAYS
4155 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
4156 name. A non-zero return indicates the type of constructor. */
4158 enum gnu_v3_ctor_kinds
4159 is_gnu_v3_mangled_ctor (const char *name
)
4161 enum gnu_v3_ctor_kinds ctor_kind
;
4162 enum gnu_v3_dtor_kinds dtor_kind
;
4164 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
4165 return (enum gnu_v3_ctor_kinds
) 0;
4170 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
4171 name. A non-zero return indicates the type of destructor. */
4173 enum gnu_v3_dtor_kinds
4174 is_gnu_v3_mangled_dtor (const char *name
)
4176 enum gnu_v3_ctor_kinds ctor_kind
;
4177 enum gnu_v3_dtor_kinds dtor_kind
;
4179 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
4180 return (enum gnu_v3_dtor_kinds
) 0;
4184 #endif /* IN_GLIBCPP_V3 */
4186 #ifdef STANDALONE_DEMANGLER
4189 #include "dyn-string.h"
4191 static void print_usage (FILE* fp
, int exit_value
);
4193 #define IS_ALPHA(CHAR) \
4194 (((CHAR) >= 'a' && (CHAR) <= 'z') \
4195 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
4197 /* Non-zero if CHAR is a character than can occur in a mangled name. */
4198 #define is_mangled_char(CHAR) \
4199 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
4200 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
4202 /* The name of this program, as invoked. */
4203 const char* program_name
;
4205 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
4208 print_usage (FILE* fp
, int exit_value
)
4210 fprintf (fp
, "Usage: %s [options] [names ...]\n", program_name
);
4211 fprintf (fp
, "Options:\n");
4212 fprintf (fp
, " -h,--help Display this message.\n");
4213 fprintf (fp
, " -p,--no-params Don't display function parameters\n");
4214 fprintf (fp
, " -v,--verbose Produce verbose demanglings.\n");
4215 fprintf (fp
, "If names are provided, they are demangled. Otherwise filters standard input.\n");
4220 /* Option specification for getopt_long. */
4221 static const struct option long_options
[] =
4223 { "help", no_argument
, NULL
, 'h' },
4224 { "no-params", no_argument
, NULL
, 'p' },
4225 { "verbose", no_argument
, NULL
, 'v' },
4226 { NULL
, no_argument
, NULL
, 0 },
4229 /* Main entry for a demangling filter executable. It will demangle
4230 its command line arguments, if any. If none are provided, it will
4231 filter stdin to stdout, replacing any recognized mangled C++ names
4232 with their demangled equivalents. */
4235 main (int argc
, char *argv
[])
4239 int options
= DMGL_PARAMS
| DMGL_ANSI
| DMGL_TYPES
;
4241 /* Use the program name of this program, as invoked. */
4242 program_name
= argv
[0];
4244 /* Parse options. */
4247 opt_char
= getopt_long (argc
, argv
, "hpv", long_options
, NULL
);
4250 case '?': /* Unrecognized option. */
4251 print_usage (stderr
, 1);
4255 print_usage (stdout
, 0);
4259 options
&= ~ DMGL_PARAMS
;
4263 options
|= DMGL_VERBOSE
;
4267 while (opt_char
!= -1);
4270 /* No command line arguments were provided. Filter stdin. */
4272 dyn_string_t mangled
= dyn_string_new (3);
4275 /* Read all of input. */
4276 while (!feof (stdin
))
4280 /* Pile characters into mangled until we hit one that can't
4281 occur in a mangled name. */
4283 while (!feof (stdin
) && is_mangled_char (c
))
4285 dyn_string_append_char (mangled
, c
);
4291 if (dyn_string_length (mangled
) > 0)
4293 #ifdef IN_GLIBCPP_V3
4294 s
= __cxa_demangle (dyn_string_buf (mangled
), NULL
, NULL
, NULL
);
4296 s
= cplus_demangle_v3 (dyn_string_buf (mangled
), options
);
4306 /* It might not have been a mangled name. Print the
4308 fputs (dyn_string_buf (mangled
), stdout
);
4311 dyn_string_clear (mangled
);
4314 /* If we haven't hit EOF yet, we've read one character that
4315 can't occur in a mangled name, so print it out. */
4320 dyn_string_delete (mangled
);
4323 /* Demangle command line arguments. */
4325 /* Loop over command line arguments. */
4326 for (i
= optind
; i
< argc
; ++i
)
4329 #ifdef IN_GLIBCPP_V3
4333 /* Attempt to demangle. */
4334 #ifdef IN_GLIBCPP_V3
4335 s
= __cxa_demangle (argv
[i
], NULL
, NULL
, &status
);
4337 s
= cplus_demangle_v3 (argv
[i
], options
);
4340 /* If it worked, print the demangled name. */
4348 #ifdef IN_GLIBCPP_V3
4349 fprintf (stderr
, "Failed: %s (status %d)\n", argv
[i
], status
);
4351 fprintf (stderr
, "Failed: %s\n", argv
[i
]);
4360 #endif /* STANDALONE_DEMANGLER */