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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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;
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_RESTRICT
:
524 printf ("restrict\n");
526 case DEMANGLE_COMPONENT_VOLATILE
:
527 printf ("volatile\n");
529 case DEMANGLE_COMPONENT_CONST
:
532 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
533 printf ("restrict this\n");
535 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
536 printf ("volatile this\n");
538 case DEMANGLE_COMPONENT_CONST_THIS
:
539 printf ("const this\n");
541 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
542 printf ("vendor type qualifier\n");
544 case DEMANGLE_COMPONENT_POINTER
:
545 printf ("pointer\n");
547 case DEMANGLE_COMPONENT_REFERENCE
:
548 printf ("reference\n");
550 case DEMANGLE_COMPONENT_COMPLEX
:
551 printf ("complex\n");
553 case DEMANGLE_COMPONENT_IMAGINARY
:
554 printf ("imaginary\n");
556 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
557 printf ("vendor type\n");
559 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
560 printf ("function type\n");
562 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
563 printf ("array type\n");
565 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
566 printf ("pointer to member type\n");
568 case DEMANGLE_COMPONENT_ARGLIST
:
569 printf ("argument list\n");
571 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
572 printf ("template argument list\n");
574 case DEMANGLE_COMPONENT_CAST
:
577 case DEMANGLE_COMPONENT_UNARY
:
578 printf ("unary operator\n");
580 case DEMANGLE_COMPONENT_BINARY
:
581 printf ("binary operator\n");
583 case DEMANGLE_COMPONENT_BINARY_ARGS
:
584 printf ("binary operator arguments\n");
586 case DEMANGLE_COMPONENT_TRINARY
:
587 printf ("trinary operator\n");
589 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
590 printf ("trinary operator arguments 1\n");
592 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
593 printf ("trinary operator arguments 1\n");
595 case DEMANGLE_COMPONENT_LITERAL
:
596 printf ("literal\n");
598 case DEMANGLE_COMPONENT_LITERAL_NEG
:
599 printf ("negative literal\n");
603 d_dump (d_left (dc
), indent
+ 2);
604 d_dump (d_right (dc
), indent
+ 2);
607 #endif /* CP_DEMANGLE_DEBUG */
609 /* Fill in a DEMANGLE_COMPONENT_NAME. */
611 CP_STATIC_IF_GLIBCPP_V3
613 cplus_demangle_fill_name (struct demangle_component
*p
, const char *s
, int len
)
615 if (p
== NULL
|| s
== NULL
|| len
== 0)
617 p
->type
= DEMANGLE_COMPONENT_NAME
;
619 p
->u
.s_name
.len
= len
;
623 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
625 CP_STATIC_IF_GLIBCPP_V3
627 cplus_demangle_fill_extended_operator (struct demangle_component
*p
, int args
,
628 struct demangle_component
*name
)
630 if (p
== NULL
|| args
< 0 || name
== NULL
)
632 p
->type
= DEMANGLE_COMPONENT_EXTENDED_OPERATOR
;
633 p
->u
.s_extended_operator
.args
= args
;
634 p
->u
.s_extended_operator
.name
= name
;
638 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
640 CP_STATIC_IF_GLIBCPP_V3
642 cplus_demangle_fill_ctor (struct demangle_component
*p
,
643 enum gnu_v3_ctor_kinds kind
,
644 struct demangle_component
*name
)
648 || (kind
< gnu_v3_complete_object_ctor
649 && kind
> gnu_v3_complete_object_allocating_ctor
))
651 p
->type
= DEMANGLE_COMPONENT_CTOR
;
652 p
->u
.s_ctor
.kind
= kind
;
653 p
->u
.s_ctor
.name
= name
;
657 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
659 CP_STATIC_IF_GLIBCPP_V3
661 cplus_demangle_fill_dtor (struct demangle_component
*p
,
662 enum gnu_v3_dtor_kinds kind
,
663 struct demangle_component
*name
)
667 || (kind
< gnu_v3_deleting_dtor
668 && kind
> gnu_v3_base_object_dtor
))
670 p
->type
= DEMANGLE_COMPONENT_DTOR
;
671 p
->u
.s_dtor
.kind
= kind
;
672 p
->u
.s_dtor
.name
= name
;
676 /* Add a new component. */
678 static struct demangle_component
*
679 d_make_empty (struct d_info
*di
)
681 struct demangle_component
*p
;
683 if (di
->next_comp
>= di
->num_comps
)
685 p
= &di
->comps
[di
->next_comp
];
690 /* Add a new generic component. */
692 static struct demangle_component
*
693 d_make_comp (struct d_info
*di
, enum demangle_component_type type
,
694 struct demangle_component
*left
,
695 struct demangle_component
*right
)
697 struct demangle_component
*p
;
699 /* We check for errors here. A typical error would be a NULL return
700 from a subroutine. We catch those here, and return NULL
704 /* These types require two parameters. */
705 case DEMANGLE_COMPONENT_QUAL_NAME
:
706 case DEMANGLE_COMPONENT_LOCAL_NAME
:
707 case DEMANGLE_COMPONENT_TYPED_NAME
:
708 case DEMANGLE_COMPONENT_TEMPLATE
:
709 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
710 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
711 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
712 case DEMANGLE_COMPONENT_UNARY
:
713 case DEMANGLE_COMPONENT_BINARY
:
714 case DEMANGLE_COMPONENT_BINARY_ARGS
:
715 case DEMANGLE_COMPONENT_TRINARY
:
716 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
717 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
718 case DEMANGLE_COMPONENT_LITERAL
:
719 case DEMANGLE_COMPONENT_LITERAL_NEG
:
720 if (left
== NULL
|| right
== NULL
)
724 /* These types only require one parameter. */
725 case DEMANGLE_COMPONENT_VTABLE
:
726 case DEMANGLE_COMPONENT_VTT
:
727 case DEMANGLE_COMPONENT_TYPEINFO
:
728 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
729 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
730 case DEMANGLE_COMPONENT_THUNK
:
731 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
732 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
733 case DEMANGLE_COMPONENT_JAVA_CLASS
:
734 case DEMANGLE_COMPONENT_GUARD
:
735 case DEMANGLE_COMPONENT_REFTEMP
:
736 case DEMANGLE_COMPONENT_POINTER
:
737 case DEMANGLE_COMPONENT_REFERENCE
:
738 case DEMANGLE_COMPONENT_COMPLEX
:
739 case DEMANGLE_COMPONENT_IMAGINARY
:
740 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
741 case DEMANGLE_COMPONENT_ARGLIST
:
742 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
743 case DEMANGLE_COMPONENT_CAST
:
748 /* This needs a right parameter, but the left parameter can be
750 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
755 /* These are allowed to have no parameters--in some cases they
756 will be filled in later. */
757 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
758 case DEMANGLE_COMPONENT_RESTRICT
:
759 case DEMANGLE_COMPONENT_VOLATILE
:
760 case DEMANGLE_COMPONENT_CONST
:
761 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
762 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
763 case DEMANGLE_COMPONENT_CONST_THIS
:
766 /* Other types should not be seen here. */
771 p
= d_make_empty (di
);
775 p
->u
.s_binary
.left
= left
;
776 p
->u
.s_binary
.right
= right
;
781 /* Add a new name component. */
783 static struct demangle_component
*
784 d_make_name (struct d_info
*di
, const char *s
, int len
)
786 struct demangle_component
*p
;
788 p
= d_make_empty (di
);
789 if (! cplus_demangle_fill_name (p
, s
, len
))
794 /* Add a new builtin type component. */
796 static struct demangle_component
*
797 d_make_builtin_type (struct d_info
*di
,
798 const struct demangle_builtin_type_info
*type
)
800 struct demangle_component
*p
;
804 p
= d_make_empty (di
);
807 p
->type
= DEMANGLE_COMPONENT_BUILTIN_TYPE
;
808 p
->u
.s_builtin
.type
= type
;
813 /* Add a new operator component. */
815 static struct demangle_component
*
816 d_make_operator (struct d_info
*di
, const struct demangle_operator_info
*op
)
818 struct demangle_component
*p
;
820 p
= d_make_empty (di
);
823 p
->type
= DEMANGLE_COMPONENT_OPERATOR
;
824 p
->u
.s_operator
.op
= op
;
829 /* Add a new extended operator component. */
831 static struct demangle_component
*
832 d_make_extended_operator (struct d_info
*di
, int args
,
833 struct demangle_component
*name
)
835 struct demangle_component
*p
;
837 p
= d_make_empty (di
);
838 if (! cplus_demangle_fill_extended_operator (p
, args
, name
))
843 /* Add a new constructor component. */
845 static struct demangle_component
*
846 d_make_ctor (struct d_info
*di
, enum gnu_v3_ctor_kinds kind
,
847 struct demangle_component
*name
)
849 struct demangle_component
*p
;
851 p
= d_make_empty (di
);
852 if (! cplus_demangle_fill_ctor (p
, kind
, name
))
857 /* Add a new destructor component. */
859 static struct demangle_component
*
860 d_make_dtor (struct d_info
*di
, enum gnu_v3_dtor_kinds kind
,
861 struct demangle_component
*name
)
863 struct demangle_component
*p
;
865 p
= d_make_empty (di
);
866 if (! cplus_demangle_fill_dtor (p
, kind
, name
))
871 /* Add a new template parameter. */
873 static struct demangle_component
*
874 d_make_template_param (struct d_info
*di
, long i
)
876 struct demangle_component
*p
;
878 p
= d_make_empty (di
);
881 p
->type
= DEMANGLE_COMPONENT_TEMPLATE_PARAM
;
882 p
->u
.s_number
.number
= i
;
887 /* Add a new standard substitution component. */
889 static struct demangle_component
*
890 d_make_sub (struct d_info
*di
, const char *name
, int len
)
892 struct demangle_component
*p
;
894 p
= d_make_empty (di
);
897 p
->type
= DEMANGLE_COMPONENT_SUB_STD
;
898 p
->u
.s_string
.string
= name
;
899 p
->u
.s_string
.len
= len
;
904 /* <mangled-name> ::= _Z <encoding>
906 TOP_LEVEL is non-zero when called at the top level. */
908 CP_STATIC_IF_GLIBCPP_V3
909 struct demangle_component
*
910 cplus_demangle_mangled_name (struct d_info
*di
, int top_level
)
912 if (d_next_char (di
) != '_')
914 if (d_next_char (di
) != 'Z')
916 return d_encoding (di
, top_level
);
919 /* Return whether a function should have a return type. The argument
920 is the function name, which may be qualified in various ways. The
921 rules are that template functions have return types with some
922 exceptions, function types which are not part of a function name
923 mangling have return types with some exceptions, and non-template
924 function names do not have return types. The exceptions are that
925 constructors, destructors, and conversion operators do not have
929 has_return_type (struct demangle_component
*dc
)
937 case DEMANGLE_COMPONENT_TEMPLATE
:
938 return ! is_ctor_dtor_or_conversion (d_left (dc
));
939 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
940 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
941 case DEMANGLE_COMPONENT_CONST_THIS
:
942 return has_return_type (d_left (dc
));
946 /* Return whether a name is a constructor, a destructor, or a
947 conversion operator. */
950 is_ctor_dtor_or_conversion (struct demangle_component
*dc
)
958 case DEMANGLE_COMPONENT_QUAL_NAME
:
959 case DEMANGLE_COMPONENT_LOCAL_NAME
:
960 return is_ctor_dtor_or_conversion (d_right (dc
));
961 case DEMANGLE_COMPONENT_CTOR
:
962 case DEMANGLE_COMPONENT_DTOR
:
963 case DEMANGLE_COMPONENT_CAST
:
968 /* <encoding> ::= <(function) name> <bare-function-type>
972 TOP_LEVEL is non-zero when called at the top level, in which case
973 if DMGL_PARAMS is not set we do not demangle the function
974 parameters. We only set this at the top level, because otherwise
975 we would not correctly demangle names in local scopes. */
977 static struct demangle_component
*
978 d_encoding (struct d_info
*di
, int top_level
)
980 char peek
= d_peek_char (di
);
982 if (peek
== 'G' || peek
== 'T')
983 return d_special_name (di
);
986 struct demangle_component
*dc
;
990 if (dc
!= NULL
&& top_level
&& (di
->options
& DMGL_PARAMS
) == 0)
992 /* Strip off any initial CV-qualifiers, as they really apply
993 to the `this' parameter, and they were not output by the
994 v2 demangler without DMGL_PARAMS. */
995 while (dc
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
996 || dc
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
997 || dc
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
1000 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1001 there may be CV-qualifiers on its right argument which
1002 really apply here; this happens when parsing a class
1003 which is local to a function. */
1004 if (dc
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
1006 struct demangle_component
*dcr
;
1009 while (dcr
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
1010 || dcr
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
1011 || dcr
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
1013 dc
->u
.s_binary
.right
= dcr
;
1019 peek
= d_peek_char (di
);
1020 if (peek
== '\0' || peek
== 'E')
1022 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPED_NAME
, dc
,
1023 d_bare_function_type (di
, has_return_type (dc
)));
1027 /* <name> ::= <nested-name>
1029 ::= <unscoped-template-name> <template-args>
1032 <unscoped-name> ::= <unqualified-name>
1033 ::= St <unqualified-name>
1035 <unscoped-template-name> ::= <unscoped-name>
1039 static struct demangle_component
*
1040 d_name (struct d_info
*di
)
1042 char peek
= d_peek_char (di
);
1043 struct demangle_component
*dc
;
1048 return d_nested_name (di
);
1051 return d_local_name (di
);
1057 if (d_peek_next_char (di
) != 't')
1059 dc
= d_substitution (di
, 0);
1065 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
,
1066 d_make_name (di
, "std", 3),
1067 d_unqualified_name (di
));
1072 if (d_peek_char (di
) != 'I')
1074 /* The grammar does not permit this case to occur if we
1075 called d_substitution() above (i.e., subst == 1). We
1076 don't bother to check. */
1080 /* This is <template-args>, which means that we just saw
1081 <unscoped-template-name>, which is a substitution
1082 candidate if we didn't just get it from a
1086 if (! d_add_substitution (di
, dc
))
1089 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, dc
,
1090 d_template_args (di
));
1097 dc
= d_unqualified_name (di
);
1098 if (d_peek_char (di
) == 'I')
1100 /* This is <template-args>, which means that we just saw
1101 <unscoped-template-name>, which is a substitution
1103 if (! d_add_substitution (di
, dc
))
1105 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, dc
,
1106 d_template_args (di
));
1112 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1113 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1116 static struct demangle_component
*
1117 d_nested_name (struct d_info
*di
)
1119 struct demangle_component
*ret
;
1120 struct demangle_component
**pret
;
1122 if (d_next_char (di
) != 'N')
1125 pret
= d_cv_qualifiers (di
, &ret
, 1);
1129 *pret
= d_prefix (di
);
1133 if (d_next_char (di
) != 'E')
1139 /* <prefix> ::= <prefix> <unqualified-name>
1140 ::= <template-prefix> <template-args>
1141 ::= <template-param>
1145 <template-prefix> ::= <prefix> <(template) unqualified-name>
1146 ::= <template-param>
1150 static struct demangle_component
*
1151 d_prefix (struct d_info
*di
)
1153 struct demangle_component
*ret
= NULL
;
1158 enum demangle_component_type comb_type
;
1159 struct demangle_component
*dc
;
1161 peek
= d_peek_char (di
);
1165 /* The older code accepts a <local-name> here, but I don't see
1166 that in the grammar. The older code does not accept a
1167 <template-param> here. */
1169 comb_type
= DEMANGLE_COMPONENT_QUAL_NAME
;
1174 dc
= d_unqualified_name (di
);
1175 else if (peek
== 'S')
1176 dc
= d_substitution (di
, 1);
1177 else if (peek
== 'I')
1181 comb_type
= DEMANGLE_COMPONENT_TEMPLATE
;
1182 dc
= d_template_args (di
);
1184 else if (peek
== 'T')
1185 dc
= d_template_param (di
);
1186 else if (peek
== 'E')
1194 ret
= d_make_comp (di
, comb_type
, ret
, dc
);
1196 if (peek
!= 'S' && d_peek_char (di
) != 'E')
1198 if (! d_add_substitution (di
, ret
))
1204 /* <unqualified-name> ::= <operator-name>
1205 ::= <ctor-dtor-name>
1209 static struct demangle_component
*
1210 d_unqualified_name (struct d_info
*di
)
1214 peek
= d_peek_char (di
);
1215 if (IS_DIGIT (peek
))
1216 return d_source_name (di
);
1217 else if (IS_LOWER (peek
))
1219 struct demangle_component
*ret
;
1221 ret
= d_operator_name (di
);
1222 if (ret
!= NULL
&& ret
->type
== DEMANGLE_COMPONENT_OPERATOR
)
1223 di
->expansion
+= sizeof "operator" + ret
->u
.s_operator
.op
->len
- 2;
1226 else if (peek
== 'C' || peek
== 'D')
1227 return d_ctor_dtor_name (di
);
1232 /* <source-name> ::= <(positive length) number> <identifier> */
1234 static struct demangle_component
*
1235 d_source_name (struct d_info
*di
)
1238 struct demangle_component
*ret
;
1240 len
= d_number (di
);
1243 ret
= d_identifier (di
, len
);
1244 di
->last_name
= ret
;
1248 /* number ::= [n] <(non-negative decimal integer)> */
1251 d_number (struct d_info
*di
)
1258 peek
= d_peek_char (di
);
1263 peek
= d_peek_char (di
);
1269 if (! IS_DIGIT (peek
))
1275 ret
= ret
* 10 + peek
- '0';
1277 peek
= d_peek_char (di
);
1281 /* identifier ::= <(unqualified source code identifier)> */
1283 static struct demangle_component
*
1284 d_identifier (struct d_info
*di
, int len
)
1290 if (di
->send
- name
< len
)
1293 d_advance (di
, len
);
1295 /* A Java mangled name may have a trailing '$' if it is a C++
1296 keyword. This '$' is not included in the length count. We just
1298 if ((di
->options
& DMGL_JAVA
) != 0
1299 && d_peek_char (di
) == '$')
1302 /* Look for something which looks like a gcc encoding of an
1303 anonymous namespace, and replace it with a more user friendly
1305 if (len
>= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN
+ 2
1306 && memcmp (name
, ANONYMOUS_NAMESPACE_PREFIX
,
1307 ANONYMOUS_NAMESPACE_PREFIX_LEN
) == 0)
1311 s
= name
+ ANONYMOUS_NAMESPACE_PREFIX_LEN
;
1312 if ((*s
== '.' || *s
== '_' || *s
== '$')
1315 di
->expansion
-= len
- sizeof "(anonymous namespace)";
1316 return d_make_name (di
, "(anonymous namespace)",
1317 sizeof "(anonymous namespace)" - 1);
1321 return d_make_name (di
, name
, len
);
1324 /* operator_name ::= many different two character encodings.
1326 ::= v <digit> <source-name>
1329 #define NL(s) s, (sizeof s) - 1
1331 CP_STATIC_IF_GLIBCPP_V3
1332 const struct demangle_operator_info cplus_demangle_operators
[] =
1334 { "aN", NL ("&="), 2 },
1335 { "aS", NL ("="), 2 },
1336 { "aa", NL ("&&"), 2 },
1337 { "ad", NL ("&"), 1 },
1338 { "an", NL ("&"), 2 },
1339 { "cl", NL ("()"), 0 },
1340 { "cm", NL (","), 2 },
1341 { "co", NL ("~"), 1 },
1342 { "dV", NL ("/="), 2 },
1343 { "da", NL ("delete[]"), 1 },
1344 { "de", NL ("*"), 1 },
1345 { "dl", NL ("delete"), 1 },
1346 { "dv", NL ("/"), 2 },
1347 { "eO", NL ("^="), 2 },
1348 { "eo", NL ("^"), 2 },
1349 { "eq", NL ("=="), 2 },
1350 { "ge", NL (">="), 2 },
1351 { "gt", NL (">"), 2 },
1352 { "ix", NL ("[]"), 2 },
1353 { "lS", NL ("<<="), 2 },
1354 { "le", NL ("<="), 2 },
1355 { "ls", NL ("<<"), 2 },
1356 { "lt", NL ("<"), 2 },
1357 { "mI", NL ("-="), 2 },
1358 { "mL", NL ("*="), 2 },
1359 { "mi", NL ("-"), 2 },
1360 { "ml", NL ("*"), 2 },
1361 { "mm", NL ("--"), 1 },
1362 { "na", NL ("new[]"), 1 },
1363 { "ne", NL ("!="), 2 },
1364 { "ng", NL ("-"), 1 },
1365 { "nt", NL ("!"), 1 },
1366 { "nw", NL ("new"), 1 },
1367 { "oR", NL ("|="), 2 },
1368 { "oo", NL ("||"), 2 },
1369 { "or", NL ("|"), 2 },
1370 { "pL", NL ("+="), 2 },
1371 { "pl", NL ("+"), 2 },
1372 { "pm", NL ("->*"), 2 },
1373 { "pp", NL ("++"), 1 },
1374 { "ps", NL ("+"), 1 },
1375 { "pt", NL ("->"), 2 },
1376 { "qu", NL ("?"), 3 },
1377 { "rM", NL ("%="), 2 },
1378 { "rS", NL (">>="), 2 },
1379 { "rm", NL ("%"), 2 },
1380 { "rs", NL (">>"), 2 },
1381 { "st", NL ("sizeof "), 1 },
1382 { "sz", NL ("sizeof "), 1 },
1383 { NULL
, NULL
, 0, 0 }
1386 static struct demangle_component
*
1387 d_operator_name (struct d_info
*di
)
1392 c1
= d_next_char (di
);
1393 c2
= d_next_char (di
);
1394 if (c1
== 'v' && IS_DIGIT (c2
))
1395 return d_make_extended_operator (di
, c2
- '0', d_source_name (di
));
1396 else if (c1
== 'c' && c2
== 'v')
1397 return d_make_comp (di
, DEMANGLE_COMPONENT_CAST
,
1398 cplus_demangle_type (di
), NULL
);
1401 /* LOW is the inclusive lower bound. */
1403 /* HIGH is the exclusive upper bound. We subtract one to ignore
1404 the sentinel at the end of the array. */
1405 int high
= ((sizeof (cplus_demangle_operators
)
1406 / sizeof (cplus_demangle_operators
[0]))
1412 const struct demangle_operator_info
*p
;
1414 i
= low
+ (high
- low
) / 2;
1415 p
= cplus_demangle_operators
+ i
;
1417 if (c1
== p
->code
[0] && c2
== p
->code
[1])
1418 return d_make_operator (di
, p
);
1420 if (c1
< p
->code
[0] || (c1
== p
->code
[0] && c2
< p
->code
[1]))
1430 /* <special-name> ::= TV <type>
1434 ::= GV <(object) name>
1435 ::= T <call-offset> <(base) encoding>
1436 ::= Tc <call-offset> <call-offset> <(base) encoding>
1437 Also g++ extensions:
1438 ::= TC <type> <(offset) number> _ <(base) type>
1444 static struct demangle_component
*
1445 d_special_name (struct d_info
*di
)
1449 di
->expansion
+= 20;
1450 c
= d_next_char (di
);
1453 switch (d_next_char (di
))
1457 return d_make_comp (di
, DEMANGLE_COMPONENT_VTABLE
,
1458 cplus_demangle_type (di
), NULL
);
1460 di
->expansion
-= 10;
1461 return d_make_comp (di
, DEMANGLE_COMPONENT_VTT
,
1462 cplus_demangle_type (di
), NULL
);
1464 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO
,
1465 cplus_demangle_type (di
), NULL
);
1467 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO_NAME
,
1468 cplus_demangle_type (di
), NULL
);
1471 if (! d_call_offset (di
, 'h'))
1473 return d_make_comp (di
, DEMANGLE_COMPONENT_THUNK
,
1474 d_encoding (di
, 0), NULL
);
1477 if (! d_call_offset (di
, 'v'))
1479 return d_make_comp (di
, DEMANGLE_COMPONENT_VIRTUAL_THUNK
,
1480 d_encoding (di
, 0), NULL
);
1483 if (! d_call_offset (di
, '\0'))
1485 if (! d_call_offset (di
, '\0'))
1487 return d_make_comp (di
, DEMANGLE_COMPONENT_COVARIANT_THUNK
,
1488 d_encoding (di
, 0), NULL
);
1492 struct demangle_component
*derived_type
;
1494 struct demangle_component
*base_type
;
1496 derived_type
= cplus_demangle_type (di
);
1497 offset
= d_number (di
);
1500 if (d_next_char (di
) != '_')
1502 base_type
= cplus_demangle_type (di
);
1503 /* We don't display the offset. FIXME: We should display
1504 it in verbose mode. */
1506 return d_make_comp (di
, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
,
1507 base_type
, derived_type
);
1511 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO_FN
,
1512 cplus_demangle_type (di
), NULL
);
1514 return d_make_comp (di
, DEMANGLE_COMPONENT_JAVA_CLASS
,
1515 cplus_demangle_type (di
), NULL
);
1523 switch (d_next_char (di
))
1526 return d_make_comp (di
, DEMANGLE_COMPONENT_GUARD
, d_name (di
), NULL
);
1529 return d_make_comp (di
, DEMANGLE_COMPONENT_REFTEMP
, d_name (di
),
1540 /* <call-offset> ::= h <nv-offset> _
1543 <nv-offset> ::= <(offset) number>
1545 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1547 The C parameter, if not '\0', is a character we just read which is
1548 the start of the <call-offset>.
1550 We don't display the offset information anywhere. FIXME: We should
1551 display it in verbose mode. */
1554 d_call_offset (struct d_info
*di
, int c
)
1557 c
= d_next_char (di
);
1564 if (d_next_char (di
) != '_')
1571 if (d_next_char (di
) != '_')
1577 /* <ctor-dtor-name> ::= C1
1585 static struct demangle_component
*
1586 d_ctor_dtor_name (struct d_info
*di
)
1588 if (di
->last_name
!= NULL
)
1590 if (di
->last_name
->type
== DEMANGLE_COMPONENT_NAME
)
1591 di
->expansion
+= di
->last_name
->u
.s_name
.len
;
1592 else if (di
->last_name
->type
== DEMANGLE_COMPONENT_SUB_STD
)
1593 di
->expansion
+= di
->last_name
->u
.s_string
.len
;
1595 switch (d_next_char (di
))
1599 enum gnu_v3_ctor_kinds kind
;
1601 switch (d_next_char (di
))
1604 kind
= gnu_v3_complete_object_ctor
;
1607 kind
= gnu_v3_base_object_ctor
;
1610 kind
= gnu_v3_complete_object_allocating_ctor
;
1615 return d_make_ctor (di
, kind
, di
->last_name
);
1620 enum gnu_v3_dtor_kinds kind
;
1622 switch (d_next_char (di
))
1625 kind
= gnu_v3_deleting_dtor
;
1628 kind
= gnu_v3_complete_object_dtor
;
1631 kind
= gnu_v3_base_object_dtor
;
1636 return d_make_dtor (di
, kind
, di
->last_name
);
1644 /* <type> ::= <builtin-type>
1646 ::= <class-enum-type>
1648 ::= <pointer-to-member-type>
1649 ::= <template-param>
1650 ::= <template-template-param> <template-args>
1652 ::= <CV-qualifiers> <type>
1657 ::= U <source-name> <type>
1659 <builtin-type> ::= various one letter codes
1663 CP_STATIC_IF_GLIBCPP_V3
1664 const struct demangle_builtin_type_info
1665 cplus_demangle_builtin_types
[D_BUILTIN_TYPE_COUNT
] =
1667 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT
},
1668 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL
},
1669 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT
},
1670 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT
},
1671 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT
},
1672 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT
},
1673 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT
},
1674 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT
},
1675 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT
},
1676 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED
},
1677 /* k */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
1678 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG
},
1679 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG
},
1680 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT
},
1681 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
1683 /* p */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
1684 /* q */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
1685 /* r */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
1686 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT
},
1687 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT
},
1688 /* u */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
1689 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID
},
1690 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT
},
1691 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG
},
1692 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
1693 D_PRINT_UNSIGNED_LONG_LONG
},
1694 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT
},
1697 CP_STATIC_IF_GLIBCPP_V3
1698 struct demangle_component
*
1699 cplus_demangle_type (struct d_info
*di
)
1702 struct demangle_component
*ret
;
1705 /* The ABI specifies that when CV-qualifiers are used, the base type
1706 is substitutable, and the fully qualified type is substitutable,
1707 but the base type with a strict subset of the CV-qualifiers is
1708 not substitutable. The natural recursive implementation of the
1709 CV-qualifiers would cause subsets to be substitutable, so instead
1710 we pull them all off now.
1712 FIXME: The ABI says that order-insensitive vendor qualifiers
1713 should be handled in the same way, but we have no way to tell
1714 which vendor qualifiers are order-insensitive and which are
1715 order-sensitive. So we just assume that they are all
1716 order-sensitive. g++ 3.4 supports only one vendor qualifier,
1717 __vector, and it treats it as order-sensitive when mangling
1720 peek
= d_peek_char (di
);
1721 if (peek
== 'r' || peek
== 'V' || peek
== 'K')
1723 struct demangle_component
**pret
;
1725 pret
= d_cv_qualifiers (di
, &ret
, 0);
1728 *pret
= cplus_demangle_type (di
);
1729 if (! d_add_substitution (di
, ret
))
1738 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1739 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
1740 case 'o': case 's': case 't':
1741 case 'v': case 'w': case 'x': case 'y': case 'z':
1742 ret
= d_make_builtin_type (di
,
1743 &cplus_demangle_builtin_types
[peek
- 'a']);
1744 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
1751 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_VENDOR_TYPE
,
1752 d_source_name (di
), NULL
);
1756 ret
= d_function_type (di
);
1759 case '0': case '1': case '2': case '3': case '4':
1760 case '5': case '6': case '7': case '8': case '9':
1763 ret
= d_class_enum_type (di
);
1767 ret
= d_array_type (di
);
1771 ret
= d_pointer_to_member_type (di
);
1775 ret
= d_template_param (di
);
1776 if (d_peek_char (di
) == 'I')
1778 /* This is <template-template-param> <template-args>. The
1779 <template-template-param> part is a substitution
1781 if (! d_add_substitution (di
, ret
))
1783 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
,
1784 d_template_args (di
));
1789 /* If this is a special substitution, then it is the start of
1790 <class-enum-type>. */
1794 peek_next
= d_peek_next_char (di
);
1795 if (IS_DIGIT (peek_next
)
1797 || IS_UPPER (peek_next
))
1799 ret
= d_substitution (di
, 0);
1800 /* The substituted name may have been a template name and
1801 may be followed by tepmlate args. */
1802 if (d_peek_char (di
) == 'I')
1803 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
,
1804 d_template_args (di
));
1810 ret
= d_class_enum_type (di
);
1811 /* If the substitution was a complete type, then it is not
1812 a new substitution candidate. However, if the
1813 substitution was followed by template arguments, then
1814 the whole thing is a substitution candidate. */
1815 if (ret
!= NULL
&& ret
->type
== DEMANGLE_COMPONENT_SUB_STD
)
1823 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_POINTER
,
1824 cplus_demangle_type (di
), NULL
);
1829 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_REFERENCE
,
1830 cplus_demangle_type (di
), NULL
);
1835 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_COMPLEX
,
1836 cplus_demangle_type (di
), NULL
);
1841 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_IMAGINARY
,
1842 cplus_demangle_type (di
), NULL
);
1847 ret
= d_source_name (di
);
1848 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
,
1849 cplus_demangle_type (di
), ret
);
1858 if (! d_add_substitution (di
, ret
))
1865 /* <CV-qualifiers> ::= [r] [V] [K] */
1867 static struct demangle_component
**
1868 d_cv_qualifiers (struct d_info
*di
,
1869 struct demangle_component
**pret
, int member_fn
)
1873 peek
= d_peek_char (di
);
1874 while (peek
== 'r' || peek
== 'V' || peek
== 'K')
1876 enum demangle_component_type t
;
1882 ? DEMANGLE_COMPONENT_RESTRICT_THIS
1883 : DEMANGLE_COMPONENT_RESTRICT
);
1884 di
->expansion
+= sizeof "restrict";
1886 else if (peek
== 'V')
1889 ? DEMANGLE_COMPONENT_VOLATILE_THIS
1890 : DEMANGLE_COMPONENT_VOLATILE
);
1891 di
->expansion
+= sizeof "volatile";
1896 ? DEMANGLE_COMPONENT_CONST_THIS
1897 : DEMANGLE_COMPONENT_CONST
);
1898 di
->expansion
+= sizeof "const";
1901 *pret
= d_make_comp (di
, t
, NULL
, NULL
);
1904 pret
= &d_left (*pret
);
1906 peek
= d_peek_char (di
);
1912 /* <function-type> ::= F [Y] <bare-function-type> E */
1914 static struct demangle_component
*
1915 d_function_type (struct d_info
*di
)
1917 struct demangle_component
*ret
;
1919 if (d_next_char (di
) != 'F')
1921 if (d_peek_char (di
) == 'Y')
1923 /* Function has C linkage. We don't print this information.
1924 FIXME: We should print it in verbose mode. */
1927 ret
= d_bare_function_type (di
, 1);
1928 if (d_next_char (di
) != 'E')
1933 /* <bare-function-type> ::= <type>+ */
1935 static struct demangle_component
*
1936 d_bare_function_type (struct d_info
*di
, int has_return_type
)
1938 struct demangle_component
*return_type
;
1939 struct demangle_component
*tl
;
1940 struct demangle_component
**ptl
;
1948 struct demangle_component
*type
;
1950 peek
= d_peek_char (di
);
1951 if (peek
== '\0' || peek
== 'E')
1953 type
= cplus_demangle_type (di
);
1956 if (has_return_type
)
1959 has_return_type
= 0;
1963 *ptl
= d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, type
, NULL
);
1966 ptl
= &d_right (*ptl
);
1970 /* There should be at least one parameter type besides the optional
1971 return type. A function which takes no arguments will have a
1972 single parameter type void. */
1976 /* If we have a single parameter type void, omit it. */
1977 if (d_right (tl
) == NULL
1978 && d_left (tl
)->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
1979 && d_left (tl
)->u
.s_builtin
.type
->print
== D_PRINT_VOID
)
1981 di
->expansion
-= d_left (tl
)->u
.s_builtin
.type
->len
;
1985 return d_make_comp (di
, DEMANGLE_COMPONENT_FUNCTION_TYPE
, return_type
, tl
);
1988 /* <class-enum-type> ::= <name> */
1990 static struct demangle_component
*
1991 d_class_enum_type (struct d_info
*di
)
1996 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
1997 ::= A [<(dimension) expression>] _ <(element) type>
2000 static struct demangle_component
*
2001 d_array_type (struct d_info
*di
)
2004 struct demangle_component
*dim
;
2006 if (d_next_char (di
) != 'A')
2009 peek
= d_peek_char (di
);
2012 else if (IS_DIGIT (peek
))
2020 peek
= d_peek_char (di
);
2022 while (IS_DIGIT (peek
));
2023 dim
= d_make_name (di
, s
, d_str (di
) - s
);
2029 dim
= d_expression (di
);
2034 if (d_next_char (di
) != '_')
2037 return d_make_comp (di
, DEMANGLE_COMPONENT_ARRAY_TYPE
, dim
,
2038 cplus_demangle_type (di
));
2041 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2043 static struct demangle_component
*
2044 d_pointer_to_member_type (struct d_info
*di
)
2046 struct demangle_component
*cl
;
2047 struct demangle_component
*mem
;
2048 struct demangle_component
**pmem
;
2050 if (d_next_char (di
) != 'M')
2053 cl
= cplus_demangle_type (di
);
2055 /* The ABI specifies that any type can be a substitution source, and
2056 that M is followed by two types, and that when a CV-qualified
2057 type is seen both the base type and the CV-qualified types are
2058 substitution sources. The ABI also specifies that for a pointer
2059 to a CV-qualified member function, the qualifiers are attached to
2060 the second type. Given the grammar, a plain reading of the ABI
2061 suggests that both the CV-qualified member function and the
2062 non-qualified member function are substitution sources. However,
2063 g++ does not work that way. g++ treats only the CV-qualified
2064 member function as a substitution source. FIXME. So to work
2065 with g++, we need to pull off the CV-qualifiers here, in order to
2066 avoid calling add_substitution() in cplus_demangle_type(). */
2068 pmem
= d_cv_qualifiers (di
, &mem
, 1);
2071 *pmem
= cplus_demangle_type (di
);
2073 return d_make_comp (di
, DEMANGLE_COMPONENT_PTRMEM_TYPE
, cl
, mem
);
2076 /* <template-param> ::= T_
2077 ::= T <(parameter-2 non-negative) number> _
2080 static struct demangle_component
*
2081 d_template_param (struct d_info
*di
)
2085 if (d_next_char (di
) != 'T')
2088 if (d_peek_char (di
) == '_')
2092 param
= d_number (di
);
2098 if (d_next_char (di
) != '_')
2103 return d_make_template_param (di
, param
);
2106 /* <template-args> ::= I <template-arg>+ E */
2108 static struct demangle_component
*
2109 d_template_args (struct d_info
*di
)
2111 struct demangle_component
*hold_last_name
;
2112 struct demangle_component
*al
;
2113 struct demangle_component
**pal
;
2115 /* Preserve the last name we saw--don't let the template arguments
2116 clobber it, as that would give us the wrong name for a subsequent
2117 constructor or destructor. */
2118 hold_last_name
= di
->last_name
;
2120 if (d_next_char (di
) != 'I')
2127 struct demangle_component
*a
;
2129 a
= d_template_arg (di
);
2133 *pal
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
, a
, NULL
);
2136 pal
= &d_right (*pal
);
2138 if (d_peek_char (di
) == 'E')
2145 di
->last_name
= hold_last_name
;
2150 /* <template-arg> ::= <type>
2151 ::= X <expression> E
2155 static struct demangle_component
*
2156 d_template_arg (struct d_info
*di
)
2158 struct demangle_component
*ret
;
2160 switch (d_peek_char (di
))
2164 ret
= d_expression (di
);
2165 if (d_next_char (di
) != 'E')
2170 return d_expr_primary (di
);
2173 return cplus_demangle_type (di
);
2177 /* <expression> ::= <(unary) operator-name> <expression>
2178 ::= <(binary) operator-name> <expression> <expression>
2179 ::= <(trinary) operator-name> <expression> <expression> <expression>
2181 ::= <template-param>
2182 ::= sr <type> <unqualified-name>
2183 ::= sr <type> <unqualified-name> <template-args>
2187 static struct demangle_component
*
2188 d_expression (struct d_info
*di
)
2192 peek
= d_peek_char (di
);
2194 return d_expr_primary (di
);
2195 else if (peek
== 'T')
2196 return d_template_param (di
);
2197 else if (peek
== 's' && d_peek_next_char (di
) == 'r')
2199 struct demangle_component
*type
;
2200 struct demangle_component
*name
;
2203 type
= cplus_demangle_type (di
);
2204 name
= d_unqualified_name (di
);
2205 if (d_peek_char (di
) != 'I')
2206 return d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
, type
, name
);
2208 return d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
, type
,
2209 d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, name
,
2210 d_template_args (di
)));
2214 struct demangle_component
*op
;
2217 op
= d_operator_name (di
);
2221 if (op
->type
== DEMANGLE_COMPONENT_OPERATOR
)
2222 di
->expansion
+= op
->u
.s_operator
.op
->len
- 2;
2224 if (op
->type
== DEMANGLE_COMPONENT_OPERATOR
2225 && strcmp (op
->u
.s_operator
.op
->code
, "st") == 0)
2226 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
2227 cplus_demangle_type (di
));
2233 case DEMANGLE_COMPONENT_OPERATOR
:
2234 args
= op
->u
.s_operator
.op
->args
;
2236 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
2237 args
= op
->u
.s_extended_operator
.args
;
2239 case DEMANGLE_COMPONENT_CAST
:
2247 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
2251 struct demangle_component
*left
;
2253 left
= d_expression (di
);
2254 return d_make_comp (di
, DEMANGLE_COMPONENT_BINARY
, op
,
2256 DEMANGLE_COMPONENT_BINARY_ARGS
,
2258 d_expression (di
)));
2262 struct demangle_component
*first
;
2263 struct demangle_component
*second
;
2265 first
= d_expression (di
);
2266 second
= d_expression (di
);
2267 return d_make_comp (di
, DEMANGLE_COMPONENT_TRINARY
, op
,
2269 DEMANGLE_COMPONENT_TRINARY_ARG1
,
2272 DEMANGLE_COMPONENT_TRINARY_ARG2
,
2274 d_expression (di
))));
2282 /* <expr-primary> ::= L <type> <(value) number> E
2283 ::= L <type> <(value) float> E
2284 ::= L <mangled-name> E
2287 static struct demangle_component
*
2288 d_expr_primary (struct d_info
*di
)
2290 struct demangle_component
*ret
;
2292 if (d_next_char (di
) != 'L')
2294 if (d_peek_char (di
) == '_')
2295 ret
= cplus_demangle_mangled_name (di
, 0);
2298 struct demangle_component
*type
;
2299 enum demangle_component_type t
;
2302 type
= cplus_demangle_type (di
);
2306 /* If we have a type we know how to print, we aren't going to
2307 print the type name itself. */
2308 if (type
->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
2309 && type
->u
.s_builtin
.type
->print
!= D_PRINT_DEFAULT
)
2310 di
->expansion
-= type
->u
.s_builtin
.type
->len
;
2312 /* Rather than try to interpret the literal value, we just
2313 collect it as a string. Note that it's possible to have a
2314 floating point literal here. The ABI specifies that the
2315 format of such literals is machine independent. That's fine,
2316 but what's not fine is that versions of g++ up to 3.2 with
2317 -fabi-version=1 used upper case letters in the hex constant,
2318 and dumped out gcc's internal representation. That makes it
2319 hard to tell where the constant ends, and hard to dump the
2320 constant in any readable form anyhow. We don't attempt to
2321 handle these cases. */
2323 t
= DEMANGLE_COMPONENT_LITERAL
;
2324 if (d_peek_char (di
) == 'n')
2326 t
= DEMANGLE_COMPONENT_LITERAL_NEG
;
2330 while (d_peek_char (di
) != 'E')
2332 ret
= d_make_comp (di
, t
, type
, d_make_name (di
, s
, d_str (di
) - s
));
2334 if (d_next_char (di
) != 'E')
2339 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2340 ::= Z <(function) encoding> E s [<discriminator>]
2343 static struct demangle_component
*
2344 d_local_name (struct d_info
*di
)
2346 struct demangle_component
*function
;
2348 if (d_next_char (di
) != 'Z')
2351 function
= d_encoding (di
, 0);
2353 if (d_next_char (di
) != 'E')
2356 if (d_peek_char (di
) == 's')
2359 if (! d_discriminator (di
))
2361 return d_make_comp (di
, DEMANGLE_COMPONENT_LOCAL_NAME
, function
,
2362 d_make_name (di
, "string literal",
2363 sizeof "string literal" - 1));
2367 struct demangle_component
*name
;
2370 if (! d_discriminator (di
))
2372 return d_make_comp (di
, DEMANGLE_COMPONENT_LOCAL_NAME
, function
, name
);
2376 /* <discriminator> ::= _ <(non-negative) number>
2378 We demangle the discriminator, but we don't print it out. FIXME:
2379 We should print it out in verbose mode. */
2382 d_discriminator (struct d_info
*di
)
2386 if (d_peek_char (di
) != '_')
2389 discrim
= d_number (di
);
2395 /* Add a new substitution. */
2398 d_add_substitution (struct d_info
*di
, struct demangle_component
*dc
)
2402 if (di
->next_sub
>= di
->num_subs
)
2404 di
->subs
[di
->next_sub
] = dc
;
2409 /* <substitution> ::= S <seq-id> _
2419 If PREFIX is non-zero, then this type is being used as a prefix in
2420 a qualified name. In this case, for the standard substitutions, we
2421 need to check whether we are being used as a prefix for a
2422 constructor or destructor, and return a full template name.
2423 Otherwise we will get something like std::iostream::~iostream()
2424 which does not correspond particularly well to any function which
2425 actually appears in the source.
2428 static const struct d_standard_sub_info standard_subs
[] =
2433 { 'a', NL ("std::allocator"),
2434 NL ("std::allocator"),
2436 { 'b', NL ("std::basic_string"),
2437 NL ("std::basic_string"),
2438 NL ("basic_string") },
2439 { 's', NL ("std::string"),
2440 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
2441 NL ("basic_string") },
2442 { 'i', NL ("std::istream"),
2443 NL ("std::basic_istream<char, std::char_traits<char> >"),
2444 NL ("basic_istream") },
2445 { 'o', NL ("std::ostream"),
2446 NL ("std::basic_ostream<char, std::char_traits<char> >"),
2447 NL ("basic_ostream") },
2448 { 'd', NL ("std::iostream"),
2449 NL ("std::basic_iostream<char, std::char_traits<char> >"),
2450 NL ("basic_iostream") }
2453 static struct demangle_component
*
2454 d_substitution (struct d_info
*di
, int prefix
)
2458 if (d_next_char (di
) != 'S')
2461 c
= d_next_char (di
);
2462 if (c
== '_' || IS_DIGIT (c
) || IS_UPPER (c
))
2472 id
= id
* 36 + c
- '0';
2473 else if (IS_UPPER (c
))
2474 id
= id
* 36 + c
- 'A' + 10;
2477 c
= d_next_char (di
);
2484 if (id
>= di
->next_sub
)
2489 return di
->subs
[id
];
2494 const struct d_standard_sub_info
*p
;
2495 const struct d_standard_sub_info
*pend
;
2497 verbose
= (di
->options
& DMGL_VERBOSE
) != 0;
2498 if (! verbose
&& prefix
)
2502 peek
= d_peek_char (di
);
2503 if (peek
== 'C' || peek
== 'D')
2507 pend
= (&standard_subs
[0]
2508 + sizeof standard_subs
/ sizeof standard_subs
[0]);
2509 for (p
= &standard_subs
[0]; p
< pend
; ++p
)
2516 if (p
->set_last_name
!= NULL
)
2517 di
->last_name
= d_make_sub (di
, p
->set_last_name
,
2518 p
->set_last_name_len
);
2521 s
= p
->full_expansion
;
2526 s
= p
->simple_expansion
;
2527 len
= p
->simple_len
;
2529 di
->expansion
+= len
;
2530 return d_make_sub (di
, s
, len
);
2538 /* Resize the print buffer. */
2541 d_print_resize (struct d_print_info
*dpi
, size_t add
)
2545 if (dpi
->buf
== NULL
)
2547 need
= dpi
->len
+ add
;
2548 while (need
> dpi
->alc
)
2553 newalc
= dpi
->alc
* 2;
2554 newbuf
= realloc (dpi
->buf
, newalc
);
2559 dpi
->allocation_failure
= 1;
2567 /* Append a character to the print buffer. */
2570 d_print_append_char (struct d_print_info
*dpi
, int c
)
2572 if (dpi
->buf
!= NULL
)
2574 if (dpi
->len
>= dpi
->alc
)
2576 d_print_resize (dpi
, 1);
2577 if (dpi
->buf
== NULL
)
2581 dpi
->buf
[dpi
->len
] = c
;
2586 /* Append a buffer to the print buffer. */
2589 d_print_append_buffer (struct d_print_info
*dpi
, const char *s
, size_t l
)
2591 if (dpi
->buf
!= NULL
)
2593 if (dpi
->len
+ l
> dpi
->alc
)
2595 d_print_resize (dpi
, l
);
2596 if (dpi
->buf
== NULL
)
2600 memcpy (dpi
->buf
+ dpi
->len
, s
, l
);
2605 /* Indicate that an error occurred during printing. */
2608 d_print_error (struct d_print_info
*dpi
)
2614 /* Turn components into a human readable string. OPTIONS is the
2615 options bits passed to the demangler. DC is the tree to print.
2616 ESTIMATE is a guess at the length of the result. This returns a
2617 string allocated by malloc, or NULL on error. On success, this
2618 sets *PALC to the size of the allocated buffer. On failure, this
2619 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
2622 CP_STATIC_IF_GLIBCPP_V3
2624 cplus_demangle_print (int options
, const struct demangle_component
*dc
,
2625 int estimate
, size_t *palc
)
2627 struct d_print_info dpi
;
2629 dpi
.options
= options
;
2631 dpi
.alc
= estimate
+ 1;
2632 dpi
.buf
= malloc (dpi
.alc
);
2633 if (dpi
.buf
== NULL
)
2640 dpi
.templates
= NULL
;
2641 dpi
.modifiers
= NULL
;
2643 dpi
.allocation_failure
= 0;
2645 d_print_comp (&dpi
, dc
);
2647 d_append_char (&dpi
, '\0');
2649 if (dpi
.buf
!= NULL
)
2652 *palc
= dpi
.allocation_failure
;
2657 /* Subroutine to handle components. */
2660 d_print_comp (struct d_print_info
*dpi
,
2661 const struct demangle_component
*dc
)
2665 d_print_error (dpi
);
2668 if (d_print_saw_error (dpi
))
2673 case DEMANGLE_COMPONENT_NAME
:
2674 if ((dpi
->options
& DMGL_JAVA
) == 0)
2675 d_append_buffer (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
2677 d_print_java_identifier (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
2680 case DEMANGLE_COMPONENT_QUAL_NAME
:
2681 case DEMANGLE_COMPONENT_LOCAL_NAME
:
2682 d_print_comp (dpi
, d_left (dc
));
2683 if ((dpi
->options
& DMGL_JAVA
) == 0)
2684 d_append_string_constant (dpi
, "::");
2686 d_append_char (dpi
, '.');
2687 d_print_comp (dpi
, d_right (dc
));
2690 case DEMANGLE_COMPONENT_TYPED_NAME
:
2692 struct d_print_mod
*hold_modifiers
;
2693 struct demangle_component
*typed_name
;
2694 struct d_print_mod adpm
[4];
2696 struct d_print_template dpt
;
2698 /* Pass the name down to the type so that it can be printed in
2699 the right place for the type. We also have to pass down
2700 any CV-qualifiers, which apply to the this parameter. */
2701 hold_modifiers
= dpi
->modifiers
;
2703 typed_name
= d_left (dc
);
2704 while (typed_name
!= NULL
)
2706 if (i
>= sizeof adpm
/ sizeof adpm
[0])
2708 d_print_error (dpi
);
2712 adpm
[i
].next
= dpi
->modifiers
;
2713 dpi
->modifiers
= &adpm
[i
];
2714 adpm
[i
].mod
= typed_name
;
2715 adpm
[i
].printed
= 0;
2716 adpm
[i
].templates
= dpi
->templates
;
2719 if (typed_name
->type
!= DEMANGLE_COMPONENT_RESTRICT_THIS
2720 && typed_name
->type
!= DEMANGLE_COMPONENT_VOLATILE_THIS
2721 && typed_name
->type
!= DEMANGLE_COMPONENT_CONST_THIS
)
2724 typed_name
= d_left (typed_name
);
2727 /* If typed_name is a template, then it applies to the
2728 function type as well. */
2729 if (typed_name
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
2731 dpt
.next
= dpi
->templates
;
2732 dpi
->templates
= &dpt
;
2733 dpt
.template = typed_name
;
2736 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
2737 there may be CV-qualifiers on its right argument which
2738 really apply here; this happens when parsing a class which
2739 is local to a function. */
2740 if (typed_name
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
2742 struct demangle_component
*local_name
;
2744 local_name
= d_right (typed_name
);
2745 while (local_name
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
2746 || local_name
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
2747 || local_name
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
2749 if (i
>= sizeof adpm
/ sizeof adpm
[0])
2751 d_print_error (dpi
);
2755 adpm
[i
] = adpm
[i
- 1];
2756 adpm
[i
].next
= &adpm
[i
- 1];
2757 dpi
->modifiers
= &adpm
[i
];
2759 adpm
[i
- 1].mod
= local_name
;
2760 adpm
[i
- 1].printed
= 0;
2761 adpm
[i
- 1].templates
= dpi
->templates
;
2764 local_name
= d_left (local_name
);
2768 d_print_comp (dpi
, d_right (dc
));
2770 if (typed_name
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
2771 dpi
->templates
= dpt
.next
;
2773 /* If the modifiers didn't get printed by the type, print them
2778 if (! adpm
[i
].printed
)
2780 d_append_char (dpi
, ' ');
2781 d_print_mod (dpi
, adpm
[i
].mod
);
2785 dpi
->modifiers
= hold_modifiers
;
2790 case DEMANGLE_COMPONENT_TEMPLATE
:
2792 struct d_print_mod
*hold_dpm
;
2794 /* Don't push modifiers into a template definition. Doing so
2795 could give the wrong definition for a template argument.
2796 Instead, treat the template essentially as a name. */
2798 hold_dpm
= dpi
->modifiers
;
2799 dpi
->modifiers
= NULL
;
2801 d_print_comp (dpi
, d_left (dc
));
2802 if (d_last_char (dpi
) == '<')
2803 d_append_char (dpi
, ' ');
2804 d_append_char (dpi
, '<');
2805 d_print_comp (dpi
, d_right (dc
));
2806 /* Avoid generating two consecutive '>' characters, to avoid
2807 the C++ syntactic ambiguity. */
2808 if (d_last_char (dpi
) == '>')
2809 d_append_char (dpi
, ' ');
2810 d_append_char (dpi
, '>');
2812 dpi
->modifiers
= hold_dpm
;
2817 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
2820 struct demangle_component
*a
;
2821 struct d_print_template
*hold_dpt
;
2823 if (dpi
->templates
== NULL
)
2825 d_print_error (dpi
);
2828 i
= dc
->u
.s_number
.number
;
2829 for (a
= d_right (dpi
->templates
->template);
2833 if (a
->type
!= DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
2835 d_print_error (dpi
);
2842 if (i
!= 0 || a
== NULL
)
2844 d_print_error (dpi
);
2848 /* While processing this parameter, we need to pop the list of
2849 templates. This is because the template parameter may
2850 itself be a reference to a parameter of an outer
2853 hold_dpt
= dpi
->templates
;
2854 dpi
->templates
= hold_dpt
->next
;
2856 d_print_comp (dpi
, d_left (a
));
2858 dpi
->templates
= hold_dpt
;
2863 case DEMANGLE_COMPONENT_CTOR
:
2864 d_print_comp (dpi
, dc
->u
.s_ctor
.name
);
2867 case DEMANGLE_COMPONENT_DTOR
:
2868 d_append_char (dpi
, '~');
2869 d_print_comp (dpi
, dc
->u
.s_dtor
.name
);
2872 case DEMANGLE_COMPONENT_VTABLE
:
2873 d_append_string_constant (dpi
, "vtable for ");
2874 d_print_comp (dpi
, d_left (dc
));
2877 case DEMANGLE_COMPONENT_VTT
:
2878 d_append_string_constant (dpi
, "VTT for ");
2879 d_print_comp (dpi
, d_left (dc
));
2882 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
2883 d_append_string_constant (dpi
, "construction vtable for ");
2884 d_print_comp (dpi
, d_left (dc
));
2885 d_append_string_constant (dpi
, "-in-");
2886 d_print_comp (dpi
, d_right (dc
));
2889 case DEMANGLE_COMPONENT_TYPEINFO
:
2890 d_append_string_constant (dpi
, "typeinfo for ");
2891 d_print_comp (dpi
, d_left (dc
));
2894 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
2895 d_append_string_constant (dpi
, "typeinfo name for ");
2896 d_print_comp (dpi
, d_left (dc
));
2899 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
2900 d_append_string_constant (dpi
, "typeinfo fn for ");
2901 d_print_comp (dpi
, d_left (dc
));
2904 case DEMANGLE_COMPONENT_THUNK
:
2905 d_append_string_constant (dpi
, "non-virtual thunk to ");
2906 d_print_comp (dpi
, d_left (dc
));
2909 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
2910 d_append_string_constant (dpi
, "virtual thunk to ");
2911 d_print_comp (dpi
, d_left (dc
));
2914 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
2915 d_append_string_constant (dpi
, "covariant return thunk to ");
2916 d_print_comp (dpi
, d_left (dc
));
2919 case DEMANGLE_COMPONENT_JAVA_CLASS
:
2920 d_append_string_constant (dpi
, "java Class for ");
2921 d_print_comp (dpi
, d_left (dc
));
2924 case DEMANGLE_COMPONENT_GUARD
:
2925 d_append_string_constant (dpi
, "guard variable for ");
2926 d_print_comp (dpi
, d_left (dc
));
2929 case DEMANGLE_COMPONENT_REFTEMP
:
2930 d_append_string_constant (dpi
, "reference temporary for ");
2931 d_print_comp (dpi
, d_left (dc
));
2934 case DEMANGLE_COMPONENT_SUB_STD
:
2935 d_append_buffer (dpi
, dc
->u
.s_string
.string
, dc
->u
.s_string
.len
);
2938 case DEMANGLE_COMPONENT_RESTRICT
:
2939 case DEMANGLE_COMPONENT_VOLATILE
:
2940 case DEMANGLE_COMPONENT_CONST
:
2942 struct d_print_mod
*pdpm
;
2944 /* When printing arrays, it's possible to have cases where the
2945 same CV-qualifier gets pushed on the stack multiple times.
2946 We only need to print it once. */
2948 for (pdpm
= dpi
->modifiers
; pdpm
!= NULL
; pdpm
= pdpm
->next
)
2950 if (! pdpm
->printed
)
2952 if (pdpm
->mod
->type
!= DEMANGLE_COMPONENT_RESTRICT
2953 && pdpm
->mod
->type
!= DEMANGLE_COMPONENT_VOLATILE
2954 && pdpm
->mod
->type
!= DEMANGLE_COMPONENT_CONST
)
2956 if (pdpm
->mod
->type
== dc
->type
)
2958 d_print_comp (dpi
, d_left (dc
));
2965 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
2966 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
2967 case DEMANGLE_COMPONENT_CONST_THIS
:
2968 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
2969 case DEMANGLE_COMPONENT_POINTER
:
2970 case DEMANGLE_COMPONENT_REFERENCE
:
2971 case DEMANGLE_COMPONENT_COMPLEX
:
2972 case DEMANGLE_COMPONENT_IMAGINARY
:
2974 /* We keep a list of modifiers on the stack. */
2975 struct d_print_mod dpm
;
2977 dpm
.next
= dpi
->modifiers
;
2978 dpi
->modifiers
= &dpm
;
2981 dpm
.templates
= dpi
->templates
;
2983 d_print_comp (dpi
, d_left (dc
));
2985 /* If the modifier didn't get printed by the type, print it
2988 d_print_mod (dpi
, dc
);
2990 dpi
->modifiers
= dpm
.next
;
2995 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
2996 if ((dpi
->options
& DMGL_JAVA
) == 0)
2997 d_append_buffer (dpi
, dc
->u
.s_builtin
.type
->name
,
2998 dc
->u
.s_builtin
.type
->len
);
3000 d_append_buffer (dpi
, dc
->u
.s_builtin
.type
->java_name
,
3001 dc
->u
.s_builtin
.type
->java_len
);
3004 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
3005 d_print_comp (dpi
, d_left (dc
));
3008 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
3010 if (d_left (dc
) != NULL
)
3012 struct d_print_mod dpm
;
3014 /* We must pass this type down as a modifier in order to
3015 print it in the right location. */
3017 dpm
.next
= dpi
->modifiers
;
3018 dpi
->modifiers
= &dpm
;
3021 dpm
.templates
= dpi
->templates
;
3023 d_print_comp (dpi
, d_left (dc
));
3025 dpi
->modifiers
= dpm
.next
;
3030 d_append_char (dpi
, ' ');
3033 d_print_function_type (dpi
, dc
, dpi
->modifiers
);
3038 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
3040 struct d_print_mod
*hold_modifiers
;
3041 struct d_print_mod adpm
[4];
3043 struct d_print_mod
*pdpm
;
3045 /* We must pass this type down as a modifier in order to print
3046 multi-dimensional arrays correctly. If the array itself is
3047 CV-qualified, we act as though the element type were
3048 CV-qualified. We do this by copying the modifiers down
3049 rather than fiddling pointers, so that we don't wind up
3050 with a d_print_mod higher on the stack pointing into our
3051 stack frame after we return. */
3053 hold_modifiers
= dpi
->modifiers
;
3055 adpm
[0].next
= hold_modifiers
;
3056 dpi
->modifiers
= &adpm
[0];
3058 adpm
[0].printed
= 0;
3059 adpm
[0].templates
= dpi
->templates
;
3062 pdpm
= hold_modifiers
;
3064 && (pdpm
->mod
->type
== DEMANGLE_COMPONENT_RESTRICT
3065 || pdpm
->mod
->type
== DEMANGLE_COMPONENT_VOLATILE
3066 || pdpm
->mod
->type
== DEMANGLE_COMPONENT_CONST
))
3068 if (! pdpm
->printed
)
3070 if (i
>= sizeof adpm
/ sizeof adpm
[0])
3072 d_print_error (dpi
);
3077 adpm
[i
].next
= dpi
->modifiers
;
3078 dpi
->modifiers
= &adpm
[i
];
3086 d_print_comp (dpi
, d_right (dc
));
3088 dpi
->modifiers
= hold_modifiers
;
3090 if (adpm
[0].printed
)
3096 d_print_mod (dpi
, adpm
[i
].mod
);
3099 d_print_array_type (dpi
, dc
, dpi
->modifiers
);
3104 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
3106 struct d_print_mod dpm
;
3108 dpm
.next
= dpi
->modifiers
;
3109 dpi
->modifiers
= &dpm
;
3112 dpm
.templates
= dpi
->templates
;
3114 d_print_comp (dpi
, d_right (dc
));
3116 /* If the modifier didn't get printed by the type, print it
3120 d_append_char (dpi
, ' ');
3121 d_print_comp (dpi
, d_left (dc
));
3122 d_append_string_constant (dpi
, "::*");
3125 dpi
->modifiers
= dpm
.next
;
3130 case DEMANGLE_COMPONENT_ARGLIST
:
3131 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
3132 d_print_comp (dpi
, d_left (dc
));
3133 if (d_right (dc
) != NULL
)
3135 d_append_string_constant (dpi
, ", ");
3136 d_print_comp (dpi
, d_right (dc
));
3140 case DEMANGLE_COMPONENT_OPERATOR
:
3144 d_append_string_constant (dpi
, "operator");
3145 c
= dc
->u
.s_operator
.op
->name
[0];
3147 d_append_char (dpi
, ' ');
3148 d_append_buffer (dpi
, dc
->u
.s_operator
.op
->name
,
3149 dc
->u
.s_operator
.op
->len
);
3153 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
3154 d_append_string_constant (dpi
, "operator ");
3155 d_print_comp (dpi
, dc
->u
.s_extended_operator
.name
);
3158 case DEMANGLE_COMPONENT_CAST
:
3159 d_append_string_constant (dpi
, "operator ");
3160 d_print_cast (dpi
, dc
);
3163 case DEMANGLE_COMPONENT_UNARY
:
3164 if (d_left (dc
)->type
!= DEMANGLE_COMPONENT_CAST
)
3165 d_print_expr_op (dpi
, d_left (dc
));
3168 d_append_char (dpi
, '(');
3169 d_print_cast (dpi
, d_left (dc
));
3170 d_append_char (dpi
, ')');
3172 d_append_char (dpi
, '(');
3173 d_print_comp (dpi
, d_right (dc
));
3174 d_append_char (dpi
, ')');
3177 case DEMANGLE_COMPONENT_BINARY
:
3178 if (d_right (dc
)->type
!= DEMANGLE_COMPONENT_BINARY_ARGS
)
3180 d_print_error (dpi
);
3184 /* We wrap an expression which uses the greater-than operator in
3185 an extra layer of parens so that it does not get confused
3186 with the '>' which ends the template parameters. */
3187 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_OPERATOR
3188 && d_left (dc
)->u
.s_operator
.op
->len
== 1
3189 && d_left (dc
)->u
.s_operator
.op
->name
[0] == '>')
3190 d_append_char (dpi
, '(');
3192 d_append_char (dpi
, '(');
3193 d_print_comp (dpi
, d_left (d_right (dc
)));
3194 d_append_string_constant (dpi
, ") ");
3195 d_print_expr_op (dpi
, d_left (dc
));
3196 d_append_string_constant (dpi
, " (");
3197 d_print_comp (dpi
, d_right (d_right (dc
)));
3198 d_append_char (dpi
, ')');
3200 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_OPERATOR
3201 && d_left (dc
)->u
.s_operator
.op
->len
== 1
3202 && d_left (dc
)->u
.s_operator
.op
->name
[0] == '>')
3203 d_append_char (dpi
, ')');
3207 case DEMANGLE_COMPONENT_BINARY_ARGS
:
3208 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
3209 d_print_error (dpi
);
3212 case DEMANGLE_COMPONENT_TRINARY
:
3213 if (d_right (dc
)->type
!= DEMANGLE_COMPONENT_TRINARY_ARG1
3214 || d_right (d_right (dc
))->type
!= DEMANGLE_COMPONENT_TRINARY_ARG2
)
3216 d_print_error (dpi
);
3219 d_append_char (dpi
, '(');
3220 d_print_comp (dpi
, d_left (d_right (dc
)));
3221 d_append_string_constant (dpi
, ") ");
3222 d_print_expr_op (dpi
, d_left (dc
));
3223 d_append_string_constant (dpi
, " (");
3224 d_print_comp (dpi
, d_left (d_right (d_right (dc
))));
3225 d_append_string_constant (dpi
, ") : (");
3226 d_print_comp (dpi
, d_right (d_right (d_right (dc
))));
3227 d_append_char (dpi
, ')');
3230 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
3231 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
3232 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
3233 d_print_error (dpi
);
3236 case DEMANGLE_COMPONENT_LITERAL
:
3237 case DEMANGLE_COMPONENT_LITERAL_NEG
:
3239 enum d_builtin_type_print tp
;
3241 /* For some builtin types, produce simpler output. */
3242 tp
= D_PRINT_DEFAULT
;
3243 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
)
3245 tp
= d_left (dc
)->u
.s_builtin
.type
->print
;
3249 case D_PRINT_UNSIGNED
:
3251 case D_PRINT_UNSIGNED_LONG
:
3252 case D_PRINT_LONG_LONG
:
3253 case D_PRINT_UNSIGNED_LONG_LONG
:
3254 if (d_right (dc
)->type
== DEMANGLE_COMPONENT_NAME
)
3256 if (dc
->type
== DEMANGLE_COMPONENT_LITERAL_NEG
)
3257 d_append_char (dpi
, '-');
3258 d_print_comp (dpi
, d_right (dc
));
3263 case D_PRINT_UNSIGNED
:
3264 d_append_char (dpi
, 'u');
3267 d_append_char (dpi
, 'l');
3269 case D_PRINT_UNSIGNED_LONG
:
3270 d_append_string_constant (dpi
, "ul");
3272 case D_PRINT_LONG_LONG
:
3273 d_append_string_constant (dpi
, "ll");
3275 case D_PRINT_UNSIGNED_LONG_LONG
:
3276 d_append_string_constant (dpi
, "ull");
3284 if (d_right (dc
)->type
== DEMANGLE_COMPONENT_NAME
3285 && d_right (dc
)->u
.s_name
.len
== 1
3286 && dc
->type
== DEMANGLE_COMPONENT_LITERAL
)
3288 switch (d_right (dc
)->u
.s_name
.s
[0])
3291 d_append_string_constant (dpi
, "false");
3294 d_append_string_constant (dpi
, "true");
3307 d_append_char (dpi
, '(');
3308 d_print_comp (dpi
, d_left (dc
));
3309 d_append_char (dpi
, ')');
3310 if (dc
->type
== DEMANGLE_COMPONENT_LITERAL_NEG
)
3311 d_append_char (dpi
, '-');
3312 if (tp
== D_PRINT_FLOAT
)
3313 d_append_char (dpi
, '[');
3314 d_print_comp (dpi
, d_right (dc
));
3315 if (tp
== D_PRINT_FLOAT
)
3316 d_append_char (dpi
, ']');
3321 d_print_error (dpi
);
3326 /* Print a Java dentifier. For Java we try to handle encoded extended
3327 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
3328 so we don't it for C++. Characters are encoded as
3332 d_print_java_identifier (struct d_print_info
*dpi
, const char *name
, int len
)
3338 for (p
= name
; p
< end
; ++p
)
3349 for (q
= p
+ 3; q
< end
; ++q
)
3355 else if (*q
>= 'A' && *q
<= 'F')
3356 dig
= *q
- 'A' + 10;
3357 else if (*q
>= 'a' && *q
<= 'f')
3358 dig
= *q
- 'a' + 10;
3364 /* If the Unicode character is larger than 256, we don't try
3365 to deal with it here. FIXME. */
3366 if (q
< end
&& *q
== '_' && c
< 256)
3368 d_append_char (dpi
, c
);
3374 d_append_char (dpi
, *p
);
3378 /* Print a list of modifiers. SUFFIX is 1 if we are printing
3379 qualifiers on this after printing a function. */
3382 d_print_mod_list (struct d_print_info
*dpi
,
3383 struct d_print_mod
*mods
, int suffix
)
3385 struct d_print_template
*hold_dpt
;
3387 if (mods
== NULL
|| d_print_saw_error (dpi
))
3392 && (mods
->mod
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
3393 || mods
->mod
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
3394 || mods
->mod
->type
== DEMANGLE_COMPONENT_CONST_THIS
)))
3396 d_print_mod_list (dpi
, mods
->next
, suffix
);
3402 hold_dpt
= dpi
->templates
;
3403 dpi
->templates
= mods
->templates
;
3405 if (mods
->mod
->type
== DEMANGLE_COMPONENT_FUNCTION_TYPE
)
3407 d_print_function_type (dpi
, mods
->mod
, mods
->next
);
3408 dpi
->templates
= hold_dpt
;
3411 else if (mods
->mod
->type
== DEMANGLE_COMPONENT_ARRAY_TYPE
)
3413 d_print_array_type (dpi
, mods
->mod
, mods
->next
);
3414 dpi
->templates
= hold_dpt
;
3417 else if (mods
->mod
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
3419 struct d_print_mod
*hold_modifiers
;
3420 struct demangle_component
*dc
;
3422 /* When this is on the modifier stack, we have pulled any
3423 qualifiers off the right argument already. Otherwise, we
3424 print it as usual, but don't let the left argument see any
3427 hold_modifiers
= dpi
->modifiers
;
3428 dpi
->modifiers
= NULL
;
3429 d_print_comp (dpi
, d_left (mods
->mod
));
3430 dpi
->modifiers
= hold_modifiers
;
3432 if ((dpi
->options
& DMGL_JAVA
) == 0)
3433 d_append_string_constant (dpi
, "::");
3435 d_append_char (dpi
, '.');
3437 dc
= d_right (mods
->mod
);
3438 while (dc
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
3439 || dc
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
3440 || dc
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
3443 d_print_comp (dpi
, dc
);
3445 dpi
->templates
= hold_dpt
;
3449 d_print_mod (dpi
, mods
->mod
);
3451 dpi
->templates
= hold_dpt
;
3453 d_print_mod_list (dpi
, mods
->next
, suffix
);
3456 /* Print a modifier. */
3459 d_print_mod (struct d_print_info
*dpi
,
3460 const struct demangle_component
*mod
)
3464 case DEMANGLE_COMPONENT_RESTRICT
:
3465 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
3466 d_append_string_constant (dpi
, " restrict");
3468 case DEMANGLE_COMPONENT_VOLATILE
:
3469 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
3470 d_append_string_constant (dpi
, " volatile");
3472 case DEMANGLE_COMPONENT_CONST
:
3473 case DEMANGLE_COMPONENT_CONST_THIS
:
3474 d_append_string_constant (dpi
, " const");
3476 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
3477 d_append_char (dpi
, ' ');
3478 d_print_comp (dpi
, d_right (mod
));
3480 case DEMANGLE_COMPONENT_POINTER
:
3481 /* There is no pointer symbol in Java. */
3482 if ((dpi
->options
& DMGL_JAVA
) == 0)
3483 d_append_char (dpi
, '*');
3485 case DEMANGLE_COMPONENT_REFERENCE
:
3486 d_append_char (dpi
, '&');
3488 case DEMANGLE_COMPONENT_COMPLEX
:
3489 d_append_string_constant (dpi
, "complex ");
3491 case DEMANGLE_COMPONENT_IMAGINARY
:
3492 d_append_string_constant (dpi
, "imaginary ");
3494 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
3495 if (d_last_char (dpi
) != '(')
3496 d_append_char (dpi
, ' ');
3497 d_print_comp (dpi
, d_left (mod
));
3498 d_append_string_constant (dpi
, "::*");
3500 case DEMANGLE_COMPONENT_TYPED_NAME
:
3501 d_print_comp (dpi
, d_left (mod
));
3504 /* Otherwise, we have something that won't go back on the
3505 modifier stack, so we can just print it. */
3506 d_print_comp (dpi
, mod
);
3511 /* Print a function type, except for the return type. */
3514 d_print_function_type (struct d_print_info
*dpi
,
3515 const struct demangle_component
*dc
,
3516 struct d_print_mod
*mods
)
3521 struct d_print_mod
*p
;
3522 struct d_print_mod
*hold_modifiers
;
3527 for (p
= mods
; p
!= NULL
; p
= p
->next
)
3533 switch (p
->mod
->type
)
3535 case DEMANGLE_COMPONENT_POINTER
:
3536 case DEMANGLE_COMPONENT_REFERENCE
:
3539 case DEMANGLE_COMPONENT_RESTRICT
:
3540 case DEMANGLE_COMPONENT_VOLATILE
:
3541 case DEMANGLE_COMPONENT_CONST
:
3542 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
3543 case DEMANGLE_COMPONENT_COMPLEX
:
3544 case DEMANGLE_COMPONENT_IMAGINARY
:
3545 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
3549 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
3550 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
3551 case DEMANGLE_COMPONENT_CONST_THIS
:
3560 if (d_left (dc
) != NULL
&& ! saw_mod
)
3567 if (d_last_char (dpi
) != '('
3568 && d_last_char (dpi
) != '*')
3571 if (need_space
&& d_last_char (dpi
) != ' ')
3572 d_append_char (dpi
, ' ');
3573 d_append_char (dpi
, '(');
3576 hold_modifiers
= dpi
->modifiers
;
3577 dpi
->modifiers
= NULL
;
3579 d_print_mod_list (dpi
, mods
, 0);
3582 d_append_char (dpi
, ')');
3584 d_append_char (dpi
, '(');
3586 if (d_right (dc
) != NULL
)
3587 d_print_comp (dpi
, d_right (dc
));
3589 d_append_char (dpi
, ')');
3591 d_print_mod_list (dpi
, mods
, 1);
3593 dpi
->modifiers
= hold_modifiers
;
3596 /* Print an array type, except for the element type. */
3599 d_print_array_type (struct d_print_info
*dpi
,
3600 const struct demangle_component
*dc
,
3601 struct d_print_mod
*mods
)
3609 struct d_print_mod
*p
;
3612 for (p
= mods
; p
!= NULL
; p
= p
->next
)
3616 if (p
->mod
->type
== DEMANGLE_COMPONENT_ARRAY_TYPE
)
3631 d_append_string_constant (dpi
, " (");
3633 d_print_mod_list (dpi
, mods
, 0);
3636 d_append_char (dpi
, ')');
3640 d_append_char (dpi
, ' ');
3642 d_append_char (dpi
, '[');
3644 if (d_left (dc
) != NULL
)
3645 d_print_comp (dpi
, d_left (dc
));
3647 d_append_char (dpi
, ']');
3650 /* Print an operator in an expression. */
3653 d_print_expr_op (struct d_print_info
*dpi
,
3654 const struct demangle_component
*dc
)
3656 if (dc
->type
== DEMANGLE_COMPONENT_OPERATOR
)
3657 d_append_buffer (dpi
, dc
->u
.s_operator
.op
->name
,
3658 dc
->u
.s_operator
.op
->len
);
3660 d_print_comp (dpi
, dc
);
3666 d_print_cast (struct d_print_info
*dpi
,
3667 const struct demangle_component
*dc
)
3669 if (d_left (dc
)->type
!= DEMANGLE_COMPONENT_TEMPLATE
)
3670 d_print_comp (dpi
, d_left (dc
));
3673 struct d_print_mod
*hold_dpm
;
3674 struct d_print_template dpt
;
3676 /* It appears that for a templated cast operator, we need to put
3677 the template parameters in scope for the operator name, but
3678 not for the parameters. The effect is that we need to handle
3679 the template printing here. */
3681 hold_dpm
= dpi
->modifiers
;
3682 dpi
->modifiers
= NULL
;
3684 dpt
.next
= dpi
->templates
;
3685 dpi
->templates
= &dpt
;
3686 dpt
.template = d_left (dc
);
3688 d_print_comp (dpi
, d_left (d_left (dc
)));
3690 dpi
->templates
= dpt
.next
;
3692 if (d_last_char (dpi
) == '<')
3693 d_append_char (dpi
, ' ');
3694 d_append_char (dpi
, '<');
3695 d_print_comp (dpi
, d_right (d_left (dc
)));
3696 /* Avoid generating two consecutive '>' characters, to avoid
3697 the C++ syntactic ambiguity. */
3698 if (d_last_char (dpi
) == '>')
3699 d_append_char (dpi
, ' ');
3700 d_append_char (dpi
, '>');
3702 dpi
->modifiers
= hold_dpm
;
3706 /* Initialize the information structure we use to pass around
3709 CP_STATIC_IF_GLIBCPP_V3
3711 cplus_demangle_init_info (const char *mangled
, int options
, size_t len
,
3715 di
->send
= mangled
+ len
;
3716 di
->options
= options
;
3720 /* We can not need more components than twice the number of chars in
3721 the mangled string. Most components correspond directly to
3722 chars, but the ARGLIST types are exceptions. */
3723 di
->num_comps
= 2 * len
;
3726 /* Similarly, we can not need more substitutions than there are
3727 chars in the mangled string. */
3732 di
->last_name
= NULL
;
3737 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
3738 name, return a buffer allocated with malloc holding the demangled
3739 name. OPTIONS is the usual libiberty demangler options. On
3740 success, this sets *PALC to the allocated size of the returned
3741 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
3742 a memory allocation failure. On failure, this returns NULL. */
3745 d_demangle (const char* mangled
, int options
, size_t *palc
)
3750 struct demangle_component
*dc
;
3756 len
= strlen (mangled
);
3758 if (mangled
[0] == '_' && mangled
[1] == 'Z')
3760 else if (strncmp (mangled
, "_GLOBAL_", 8) == 0
3761 && (mangled
[8] == '.' || mangled
[8] == '_' || mangled
[8] == '$')
3762 && (mangled
[9] == 'D' || mangled
[9] == 'I')
3763 && mangled
[10] == '_')
3767 r
= malloc (40 + len
- 11);
3772 if (mangled
[9] == 'I')
3773 strcpy (r
, "global constructors keyed to ");
3775 strcpy (r
, "global destructors keyed to ");
3776 strcat (r
, mangled
+ 11);
3782 if ((options
& DMGL_TYPES
) == 0)
3787 cplus_demangle_init_info (mangled
, options
, len
, &di
);
3790 #ifdef CP_DYNAMIC_ARRAYS
3791 __extension__
struct demangle_component comps
[di
.num_comps
];
3792 __extension__
struct demangle_component
*subs
[di
.num_subs
];
3794 di
.comps
= &comps
[0];
3797 di
.comps
= ((struct demangle_component
*)
3798 malloc (di
.num_comps
* sizeof (struct demangle_component
)));
3799 di
.subs
= ((struct demangle_component
**)
3800 malloc (di
.num_subs
* sizeof (struct demangle_component
*)));
3801 if (di
.comps
== NULL
|| di
.subs
== NULL
)
3803 if (di
.comps
!= NULL
)
3805 if (di
.subs
!= NULL
)
3813 dc
= cplus_demangle_mangled_name (&di
, 1);
3815 dc
= cplus_demangle_type (&di
);
3817 /* If DMGL_PARAMS is set, then if we didn't consume the entire
3818 mangled string, then we didn't successfully demangle it. If
3819 DMGL_PARAMS is not set, we didn't look at the trailing
3821 if (((options
& DMGL_PARAMS
) != 0) && d_peek_char (&di
) != '\0')
3824 #ifdef CP_DEMANGLE_DEBUG
3826 printf ("failed demangling\n");
3831 /* We try to guess the length of the demangled string, to minimize
3832 calls to realloc during demangling. */
3833 estimate
= len
+ di
.expansion
+ 10 * di
.did_subs
;
3834 estimate
+= estimate
/ 8;
3838 ret
= cplus_demangle_print (options
, dc
, estimate
, palc
);
3840 #ifndef CP_DYNAMIC_ARRAYS
3845 #ifdef CP_DEMANGLE_DEBUG
3850 rlen
= strlen (ret
);
3851 if (rlen
> 2 * estimate
)
3852 printf ("*** Length %d much greater than estimate %d\n",
3854 else if (rlen
> estimate
)
3855 printf ("*** Length %d greater than estimate %d\n",
3857 else if (rlen
< estimate
/ 2)
3858 printf ("*** Length %d much less than estimate %d\n",
3867 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
3869 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
3871 /* ia64 ABI-mandated entry point in the C++ runtime library for
3872 performing demangling. MANGLED_NAME is a NUL-terminated character
3873 string containing the name to be demangled.
3875 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
3876 *LENGTH bytes, into which the demangled name is stored. If
3877 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
3878 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
3879 is placed in a region of memory allocated with malloc.
3881 If LENGTH is non-NULL, the length of the buffer conaining the
3882 demangled name, is placed in *LENGTH.
3884 The return value is a pointer to the start of the NUL-terminated
3885 demangled name, or NULL if the demangling fails. The caller is
3886 responsible for deallocating this memory using free.
3888 *STATUS is set to one of the following values:
3889 0: The demangling operation succeeded.
3890 -1: A memory allocation failure occurred.
3891 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
3892 -3: One of the arguments is invalid.
3894 The demangling is performed using the C++ ABI mangling rules, with
3898 __cxa_demangle (const char *mangled_name
, char *output_buffer
,
3899 size_t *length
, int *status
)
3904 if (mangled_name
== NULL
)
3911 if (output_buffer
!= NULL
&& length
== NULL
)
3918 demangled
= d_demangle (mangled_name
, DMGL_PARAMS
| DMGL_TYPES
, &alc
);
3920 if (demangled
== NULL
)
3932 if (output_buffer
== NULL
)
3939 if (strlen (demangled
) < *length
)
3941 strcpy (output_buffer
, demangled
);
3943 demangled
= output_buffer
;
3947 free (output_buffer
);
3958 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
3960 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
3961 mangled name, return a buffer allocated with malloc holding the
3962 demangled name. Otherwise, return NULL. */
3965 cplus_demangle_v3 (const char* mangled
, int options
)
3969 return d_demangle (mangled
, options
, &alc
);
3972 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
3973 conventions, but the output formatting is a little different.
3974 This instructs the C++ demangler not to emit pointer characters ("*"), and
3975 to use Java's namespace separator symbol ("." instead of "::"). It then
3976 does an additional pass over the demangled output to replace instances
3977 of JArray<TYPE> with TYPE[]. */
3980 java_demangle_v3 (const char* mangled
)
3988 demangled
= d_demangle (mangled
, DMGL_JAVA
| DMGL_PARAMS
, &alc
);
3990 if (demangled
== NULL
)
3996 while (*from
!= '\0')
3998 if (strncmp (from
, "JArray<", 7) == 0)
4003 else if (nesting
> 0 && *from
== '>')
4005 while (to
> demangled
&& to
[-1] == ' ')
4021 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
4023 #ifndef IN_GLIBCPP_V3
4025 /* Demangle a string in order to find out whether it is a constructor
4026 or destructor. Return non-zero on success. Set *CTOR_KIND and
4027 *DTOR_KIND appropriately. */
4030 is_ctor_or_dtor (const char *mangled
,
4031 enum gnu_v3_ctor_kinds
*ctor_kind
,
4032 enum gnu_v3_dtor_kinds
*dtor_kind
)
4035 struct demangle_component
*dc
;
4038 *ctor_kind
= (enum gnu_v3_ctor_kinds
) 0;
4039 *dtor_kind
= (enum gnu_v3_dtor_kinds
) 0;
4041 cplus_demangle_init_info (mangled
, DMGL_GNU_V3
, strlen (mangled
), &di
);
4044 #ifdef CP_DYNAMIC_ARRAYS
4045 __extension__
struct demangle_component comps
[di
.num_comps
];
4046 __extension__
struct demangle_component
*subs
[di
.num_subs
];
4048 di
.comps
= &comps
[0];
4051 di
.comps
= ((struct demangle_component
*)
4052 malloc (di
.num_comps
* sizeof (struct demangle_component
)));
4053 di
.subs
= ((struct demangle_component
**)
4054 malloc (di
.num_subs
* sizeof (struct demangle_component
*)));
4055 if (di
.comps
== NULL
|| di
.subs
== NULL
)
4057 if (di
.comps
!= NULL
)
4059 if (di
.subs
!= NULL
)
4065 dc
= cplus_demangle_mangled_name (&di
, 1);
4067 /* Note that because we did not pass DMGL_PARAMS, we don't expect
4068 to demangle the entire string. */
4078 case DEMANGLE_COMPONENT_TYPED_NAME
:
4079 case DEMANGLE_COMPONENT_TEMPLATE
:
4080 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
4081 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
4082 case DEMANGLE_COMPONENT_CONST_THIS
:
4085 case DEMANGLE_COMPONENT_QUAL_NAME
:
4086 case DEMANGLE_COMPONENT_LOCAL_NAME
:
4089 case DEMANGLE_COMPONENT_CTOR
:
4090 *ctor_kind
= dc
->u
.s_ctor
.kind
;
4094 case DEMANGLE_COMPONENT_DTOR
:
4095 *dtor_kind
= dc
->u
.s_dtor
.kind
;
4102 #ifndef CP_DYNAMIC_ARRAYS
4111 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
4112 name. A non-zero return indicates the type of constructor. */
4114 enum gnu_v3_ctor_kinds
4115 is_gnu_v3_mangled_ctor (const char *name
)
4117 enum gnu_v3_ctor_kinds ctor_kind
;
4118 enum gnu_v3_dtor_kinds dtor_kind
;
4120 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
4121 return (enum gnu_v3_ctor_kinds
) 0;
4126 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
4127 name. A non-zero return indicates the type of destructor. */
4129 enum gnu_v3_dtor_kinds
4130 is_gnu_v3_mangled_dtor (const char *name
)
4132 enum gnu_v3_ctor_kinds ctor_kind
;
4133 enum gnu_v3_dtor_kinds dtor_kind
;
4135 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
4136 return (enum gnu_v3_dtor_kinds
) 0;
4140 #endif /* IN_GLIBCPP_V3 */
4142 #ifdef STANDALONE_DEMANGLER
4145 #include "dyn-string.h"
4147 static void print_usage (FILE* fp
, int exit_value
);
4149 #define IS_ALPHA(CHAR) \
4150 (((CHAR) >= 'a' && (CHAR) <= 'z') \
4151 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
4153 /* Non-zero if CHAR is a character than can occur in a mangled name. */
4154 #define is_mangled_char(CHAR) \
4155 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
4156 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
4158 /* The name of this program, as invoked. */
4159 const char* program_name
;
4161 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
4164 print_usage (FILE* fp
, int exit_value
)
4166 fprintf (fp
, "Usage: %s [options] [names ...]\n", program_name
);
4167 fprintf (fp
, "Options:\n");
4168 fprintf (fp
, " -h,--help Display this message.\n");
4169 fprintf (fp
, " -p,--no-params Don't display function parameters\n");
4170 fprintf (fp
, " -v,--verbose Produce verbose demanglings.\n");
4171 fprintf (fp
, "If names are provided, they are demangled. Otherwise filters standard input.\n");
4176 /* Option specification for getopt_long. */
4177 static const struct option long_options
[] =
4179 { "help", no_argument
, NULL
, 'h' },
4180 { "no-params", no_argument
, NULL
, 'p' },
4181 { "verbose", no_argument
, NULL
, 'v' },
4182 { NULL
, no_argument
, NULL
, 0 },
4185 /* Main entry for a demangling filter executable. It will demangle
4186 its command line arguments, if any. If none are provided, it will
4187 filter stdin to stdout, replacing any recognized mangled C++ names
4188 with their demangled equivalents. */
4191 main (int argc
, char *argv
[])
4195 int options
= DMGL_PARAMS
| DMGL_ANSI
| DMGL_TYPES
;
4197 /* Use the program name of this program, as invoked. */
4198 program_name
= argv
[0];
4200 /* Parse options. */
4203 opt_char
= getopt_long (argc
, argv
, "hpv", long_options
, NULL
);
4206 case '?': /* Unrecognized option. */
4207 print_usage (stderr
, 1);
4211 print_usage (stdout
, 0);
4215 options
&= ~ DMGL_PARAMS
;
4219 options
|= DMGL_VERBOSE
;
4223 while (opt_char
!= -1);
4226 /* No command line arguments were provided. Filter stdin. */
4228 dyn_string_t mangled
= dyn_string_new (3);
4231 /* Read all of input. */
4232 while (!feof (stdin
))
4236 /* Pile characters into mangled until we hit one that can't
4237 occur in a mangled name. */
4239 while (!feof (stdin
) && is_mangled_char (c
))
4241 dyn_string_append_char (mangled
, c
);
4247 if (dyn_string_length (mangled
) > 0)
4249 #ifdef IN_GLIBCPP_V3
4250 s
= __cxa_demangle (dyn_string_buf (mangled
), NULL
, NULL
, NULL
);
4252 s
= cplus_demangle_v3 (dyn_string_buf (mangled
), options
);
4262 /* It might not have been a mangled name. Print the
4264 fputs (dyn_string_buf (mangled
), stdout
);
4267 dyn_string_clear (mangled
);
4270 /* If we haven't hit EOF yet, we've read one character that
4271 can't occur in a mangled name, so print it out. */
4276 dyn_string_delete (mangled
);
4279 /* Demangle command line arguments. */
4281 /* Loop over command line arguments. */
4282 for (i
= optind
; i
< argc
; ++i
)
4285 #ifdef IN_GLIBCPP_V3
4289 /* Attempt to demangle. */
4290 #ifdef IN_GLIBCPP_V3
4291 s
= __cxa_demangle (argv
[i
], NULL
, NULL
, &status
);
4293 s
= cplus_demangle_v3 (argv
[i
], options
);
4296 /* If it worked, print the demangled name. */
4304 #ifdef IN_GLIBCPP_V3
4305 fprintf (stderr
, "Failed: %s (status %d)\n", argv
[i
], status
);
4307 fprintf (stderr
, "Failed: %s\n", argv
[i
]);
4316 #endif /* STANDALONE_DEMANGLER */