1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor <ian@wasabisystems.com>.
6 This file is part of the libiberty library, which is part of GCC.
8 This file is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 In addition to the permissions in the GNU General Public License, the
14 Free Software Foundation gives you unlimited permission to link the
15 compiled version of this file into combinations with other programs,
16 and to distribute those combinations without any restriction coming
17 from the use of this file. (The General Public License restrictions
18 do apply in other respects; for example, they cover modification of
19 the file, and distribution when not linked into a combined
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
32 /* This code implements a demangler for the g++ V3 ABI. The ABI is
33 described on this web page:
34 http://www.codesourcery.com/cxx-abi/abi.html#mangling
36 This code was written while looking at the demangler written by
37 Alex Samuel <samuel@codesourcery.com>.
39 This code first pulls the mangled name apart into a list of
40 components, and then walks the list generating the demangled
43 This file will normally define the following functions, q.v.:
44 char *cplus_demangle_v3(const char *mangled, int options)
45 char *java_demangle_v3(const char *mangled)
46 int cplus_demangle_v3_callback(const char *mangled, int options,
47 demangle_callbackref callback)
48 int java_demangle_v3_callback(const char *mangled,
49 demangle_callbackref callback)
50 enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
51 enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
53 Also, the interface to the component list is public, and defined in
54 demangle.h. The interface consists of these types, which are
55 defined in demangle.h:
56 enum demangle_component_type
57 struct demangle_component
59 and these functions defined in this file:
60 cplus_demangle_fill_name
61 cplus_demangle_fill_extended_operator
62 cplus_demangle_fill_ctor
63 cplus_demangle_fill_dtor
65 cplus_demangle_print_callback
66 and other functions defined in the file cp-demint.c.
68 This file also defines some other functions and variables which are
69 only to be used by the file cp-demint.c.
71 Preprocessor macros you can define while compiling this file:
74 If defined, this file defines the following functions, q.v.:
75 char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
77 int __gcclibcxx_demangle_callback (const char *,
79 (const char *, size_t, void *),
81 instead of cplus_demangle_v3[_callback]() and
82 java_demangle_v3[_callback]().
85 If defined, this file defines only __cxa_demangle() and
86 __gcclibcxx_demangle_callback(), and no other publically visible
87 functions or variables.
90 If defined, this file defines a main() function which demangles
91 any arguments, or, if none, demangles stdin.
94 If defined, turns on debugging mode, which prints information on
95 stdout about the mangled string. This is not generally useful.
98 #if defined (_AIX) && !defined (__GNUC__)
120 # define alloca __builtin_alloca
122 extern char *alloca ();
123 # endif /* __GNUC__ */
125 #endif /* HAVE_ALLOCA_H */
127 #include "ansidecl.h"
128 #include "libiberty.h"
129 #include "demangle.h"
130 #include "cp-demangle.h"
132 /* If IN_GLIBCPP_V3 is defined, some functions are made static. We
133 also rename them via #define to avoid compiler errors when the
134 static definition conflicts with the extern declaration in a header
138 #define CP_STATIC_IF_GLIBCPP_V3 static
140 #define cplus_demangle_fill_name d_fill_name
141 static int d_fill_name (struct demangle_component
*, const char *, int);
143 #define cplus_demangle_fill_extended_operator d_fill_extended_operator
145 d_fill_extended_operator (struct demangle_component
*, int,
146 struct demangle_component
*);
148 #define cplus_demangle_fill_ctor d_fill_ctor
150 d_fill_ctor (struct demangle_component
*, enum gnu_v3_ctor_kinds
,
151 struct demangle_component
*);
153 #define cplus_demangle_fill_dtor d_fill_dtor
155 d_fill_dtor (struct demangle_component
*, enum gnu_v3_dtor_kinds
,
156 struct demangle_component
*);
158 #define cplus_demangle_mangled_name d_mangled_name
159 static struct demangle_component
*d_mangled_name (struct d_info
*, int);
161 #define cplus_demangle_type d_type
162 static struct demangle_component
*d_type (struct d_info
*);
164 #define cplus_demangle_print d_print
165 static char *d_print (int, const struct demangle_component
*, int, size_t *);
167 #define cplus_demangle_print_callback d_print_callback
168 static int d_print_callback (int, const struct demangle_component
*,
169 demangle_callbackref
, void *);
171 #define cplus_demangle_init_info d_init_info
172 static void d_init_info (const char *, int, size_t, struct d_info
*);
174 #else /* ! defined(IN_GLIBCPP_V3) */
175 #define CP_STATIC_IF_GLIBCPP_V3
176 #endif /* ! defined(IN_GLIBCPP_V3) */
178 /* See if the compiler supports dynamic arrays. */
181 #define CP_DYNAMIC_ARRAYS
184 #ifdef __STDC_VERSION__
185 #if __STDC_VERSION__ >= 199901L
186 #define CP_DYNAMIC_ARRAYS
187 #endif /* __STDC__VERSION >= 199901L */
188 #endif /* defined (__STDC_VERSION__) */
189 #endif /* defined (__STDC__) */
190 #endif /* ! defined (__GNUC__) */
192 /* We avoid pulling in the ctype tables, to prevent pulling in
193 additional unresolved symbols when this code is used in a library.
194 FIXME: Is this really a valid reason? This comes from the original
197 As of this writing this file has the following undefined references
198 when compiled with -DIN_GLIBCPP_V3: realloc, free, memcpy, strcpy,
201 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
202 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
203 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
205 /* The prefix prepended by GCC to an identifier represnting the
206 anonymous namespace. */
207 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
208 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
209 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
211 /* Information we keep for the standard substitutions. */
213 struct d_standard_sub_info
215 /* The code for this substitution. */
217 /* The simple string it expands to. */
218 const char *simple_expansion
;
219 /* The length of the simple expansion. */
221 /* The results of a full, verbose, expansion. This is used when
222 qualifying a constructor/destructor, or when in verbose mode. */
223 const char *full_expansion
;
224 /* The length of the full expansion. */
226 /* What to set the last_name field of d_info to; NULL if we should
227 not set it. This is only relevant when qualifying a
228 constructor/destructor. */
229 const char *set_last_name
;
230 /* The length of set_last_name. */
231 int set_last_name_len
;
234 /* Accessors for subtrees of struct demangle_component. */
236 #define d_left(dc) ((dc)->u.s_binary.left)
237 #define d_right(dc) ((dc)->u.s_binary.right)
239 /* A list of templates. This is used while printing. */
241 struct d_print_template
243 /* Next template on the list. */
244 struct d_print_template
*next
;
246 const struct demangle_component
*template_decl
;
249 /* A list of type modifiers. This is used while printing. */
253 /* Next modifier on the list. These are in the reverse of the order
254 in which they appeared in the mangled string. */
255 struct d_print_mod
*next
;
257 const struct demangle_component
*mod
;
258 /* Whether this modifier was printed. */
260 /* The list of templates which applies to this modifier. */
261 struct d_print_template
*templates
;
264 /* We use these structures to hold information during printing. */
266 struct d_growable_string
268 /* Buffer holding the result. */
270 /* Current length of data in buffer. */
272 /* Allocated size of buffer. */
274 /* Set to 1 if we had a memory allocation failure. */
275 int allocation_failure
;
278 enum { D_PRINT_BUFFER_LENGTH
= 256 };
281 /* The options passed to the demangler. */
283 /* Fixed-length allocated buffer for demangled data, flushed to the
284 callback with a NUL termination once full. */
285 char buf
[D_PRINT_BUFFER_LENGTH
];
286 /* Current length of data in buffer. */
288 /* The last character printed, saved individually so that it survives
291 /* Callback function to handle demangled buffer flush. */
292 demangle_callbackref callback
;
293 /* Opaque callback argument. */
295 /* The current list of templates, if any. */
296 struct d_print_template
*templates
;
297 /* The current list of modifiers (e.g., pointer, reference, etc.),
299 struct d_print_mod
*modifiers
;
300 /* Set to 1 if we saw a demangling error. */
301 int demangle_failure
;
302 /* The current index into any template argument packs we are using
307 #ifdef CP_DEMANGLE_DEBUG
308 static void d_dump (struct demangle_component
*, int);
311 static struct demangle_component
*
312 d_make_empty (struct d_info
*);
314 static struct demangle_component
*
315 d_make_comp (struct d_info
*, enum demangle_component_type
,
316 struct demangle_component
*,
317 struct demangle_component
*);
319 static struct demangle_component
*
320 d_make_name (struct d_info
*, const char *, int);
322 static struct demangle_component
*
323 d_make_builtin_type (struct d_info
*,
324 const struct demangle_builtin_type_info
*);
326 static struct demangle_component
*
327 d_make_operator (struct d_info
*,
328 const struct demangle_operator_info
*);
330 static struct demangle_component
*
331 d_make_extended_operator (struct d_info
*, int,
332 struct demangle_component
*);
334 static struct demangle_component
*
335 d_make_ctor (struct d_info
*, enum gnu_v3_ctor_kinds
,
336 struct demangle_component
*);
338 static struct demangle_component
*
339 d_make_dtor (struct d_info
*, enum gnu_v3_dtor_kinds
,
340 struct demangle_component
*);
342 static struct demangle_component
*
343 d_make_template_param (struct d_info
*, long);
345 static struct demangle_component
*
346 d_make_sub (struct d_info
*, const char *, int);
349 has_return_type (struct demangle_component
*);
352 is_ctor_dtor_or_conversion (struct demangle_component
*);
354 static struct demangle_component
*d_encoding (struct d_info
*, int);
356 static struct demangle_component
*d_name (struct d_info
*);
358 static struct demangle_component
*d_nested_name (struct d_info
*);
360 static struct demangle_component
*d_prefix (struct d_info
*);
362 static struct demangle_component
*d_unqualified_name (struct d_info
*);
364 static struct demangle_component
*d_source_name (struct d_info
*);
366 static long d_number (struct d_info
*);
368 static struct demangle_component
*d_identifier (struct d_info
*, int);
370 static struct demangle_component
*d_operator_name (struct d_info
*);
372 static struct demangle_component
*d_special_name (struct d_info
*);
374 static int d_call_offset (struct d_info
*, int);
376 static struct demangle_component
*d_ctor_dtor_name (struct d_info
*);
378 static struct demangle_component
**
379 d_cv_qualifiers (struct d_info
*, struct demangle_component
**, int);
381 static struct demangle_component
*
382 d_function_type (struct d_info
*);
384 static struct demangle_component
*
385 d_bare_function_type (struct d_info
*, int);
387 static struct demangle_component
*
388 d_class_enum_type (struct d_info
*);
390 static struct demangle_component
*d_array_type (struct d_info
*);
392 static struct demangle_component
*
393 d_pointer_to_member_type (struct d_info
*);
395 static struct demangle_component
*
396 d_template_param (struct d_info
*);
398 static struct demangle_component
*d_template_args (struct d_info
*);
400 static struct demangle_component
*
401 d_template_arg (struct d_info
*);
403 static struct demangle_component
*d_expression (struct d_info
*);
405 static struct demangle_component
*d_expr_primary (struct d_info
*);
407 static struct demangle_component
*d_local_name (struct d_info
*);
409 static int d_discriminator (struct d_info
*);
412 d_add_substitution (struct d_info
*, struct demangle_component
*);
414 static struct demangle_component
*d_substitution (struct d_info
*, int);
416 static void d_growable_string_init (struct d_growable_string
*, size_t);
419 d_growable_string_resize (struct d_growable_string
*, size_t);
422 d_growable_string_append_buffer (struct d_growable_string
*,
423 const char *, size_t);
425 d_growable_string_callback_adapter (const char *, size_t, void *);
428 d_print_init (struct d_print_info
*, int, demangle_callbackref
, void *);
430 static inline void d_print_error (struct d_print_info
*);
432 static inline int d_print_saw_error (struct d_print_info
*);
434 static inline void d_print_flush (struct d_print_info
*);
436 static inline void d_append_char (struct d_print_info
*, char);
438 static inline void d_append_buffer (struct d_print_info
*,
439 const char *, size_t);
441 static inline void d_append_string (struct d_print_info
*, const char *);
443 static inline char d_last_char (struct d_print_info
*);
446 d_print_comp (struct d_print_info
*, const struct demangle_component
*);
449 d_print_java_identifier (struct d_print_info
*, const char *, int);
452 d_print_mod_list (struct d_print_info
*, struct d_print_mod
*, int);
455 d_print_mod (struct d_print_info
*, const struct demangle_component
*);
458 d_print_function_type (struct d_print_info
*,
459 const struct demangle_component
*,
460 struct d_print_mod
*);
463 d_print_array_type (struct d_print_info
*,
464 const struct demangle_component
*,
465 struct d_print_mod
*);
468 d_print_expr_op (struct d_print_info
*, const struct demangle_component
*);
471 d_print_cast (struct d_print_info
*, const struct demangle_component
*);
473 static int d_demangle_callback (const char *, int,
474 demangle_callbackref
, void *);
475 static char *d_demangle (const char *, int, size_t *);
477 #ifdef CP_DEMANGLE_DEBUG
480 d_dump (struct demangle_component
*dc
, int indent
)
487 printf ("failed demangling\n");
491 for (i
= 0; i
< indent
; ++i
)
496 case DEMANGLE_COMPONENT_NAME
:
497 printf ("name '%.*s'\n", dc
->u
.s_name
.len
, dc
->u
.s_name
.s
);
499 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
500 printf ("template parameter %ld\n", dc
->u
.s_number
.number
);
502 case DEMANGLE_COMPONENT_CTOR
:
503 printf ("constructor %d\n", (int) dc
->u
.s_ctor
.kind
);
504 d_dump (dc
->u
.s_ctor
.name
, indent
+ 2);
506 case DEMANGLE_COMPONENT_DTOR
:
507 printf ("destructor %d\n", (int) dc
->u
.s_dtor
.kind
);
508 d_dump (dc
->u
.s_dtor
.name
, indent
+ 2);
510 case DEMANGLE_COMPONENT_SUB_STD
:
511 printf ("standard substitution %s\n", dc
->u
.s_string
.string
);
513 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
514 printf ("builtin type %s\n", dc
->u
.s_builtin
.type
->name
);
516 case DEMANGLE_COMPONENT_OPERATOR
:
517 printf ("operator %s\n", dc
->u
.s_operator
.op
->name
);
519 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
520 printf ("extended operator with %d args\n",
521 dc
->u
.s_extended_operator
.args
);
522 d_dump (dc
->u
.s_extended_operator
.name
, indent
+ 2);
525 case DEMANGLE_COMPONENT_QUAL_NAME
:
526 printf ("qualified name\n");
528 case DEMANGLE_COMPONENT_LOCAL_NAME
:
529 printf ("local name\n");
531 case DEMANGLE_COMPONENT_TYPED_NAME
:
532 printf ("typed name\n");
534 case DEMANGLE_COMPONENT_TEMPLATE
:
535 printf ("template\n");
537 case DEMANGLE_COMPONENT_VTABLE
:
540 case DEMANGLE_COMPONENT_VTT
:
543 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
544 printf ("construction vtable\n");
546 case DEMANGLE_COMPONENT_TYPEINFO
:
547 printf ("typeinfo\n");
549 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
550 printf ("typeinfo name\n");
552 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
553 printf ("typeinfo function\n");
555 case DEMANGLE_COMPONENT_THUNK
:
558 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
559 printf ("virtual thunk\n");
561 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
562 printf ("covariant thunk\n");
564 case DEMANGLE_COMPONENT_JAVA_CLASS
:
565 printf ("java class\n");
567 case DEMANGLE_COMPONENT_GUARD
:
570 case DEMANGLE_COMPONENT_REFTEMP
:
571 printf ("reference temporary\n");
573 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
574 printf ("hidden alias\n");
576 case DEMANGLE_COMPONENT_RESTRICT
:
577 printf ("restrict\n");
579 case DEMANGLE_COMPONENT_VOLATILE
:
580 printf ("volatile\n");
582 case DEMANGLE_COMPONENT_CONST
:
585 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
586 printf ("restrict this\n");
588 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
589 printf ("volatile this\n");
591 case DEMANGLE_COMPONENT_CONST_THIS
:
592 printf ("const this\n");
594 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
595 printf ("vendor type qualifier\n");
597 case DEMANGLE_COMPONENT_POINTER
:
598 printf ("pointer\n");
600 case DEMANGLE_COMPONENT_REFERENCE
:
601 printf ("reference\n");
603 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
604 printf ("rvalue reference\n");
606 case DEMANGLE_COMPONENT_COMPLEX
:
607 printf ("complex\n");
609 case DEMANGLE_COMPONENT_IMAGINARY
:
610 printf ("imaginary\n");
612 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
613 printf ("vendor type\n");
615 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
616 printf ("function type\n");
618 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
619 printf ("array type\n");
621 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
622 printf ("pointer to member type\n");
624 case DEMANGLE_COMPONENT_FIXED_TYPE
:
625 printf ("fixed-point type\n");
627 case DEMANGLE_COMPONENT_ARGLIST
:
628 printf ("argument list\n");
630 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
631 printf ("template argument list\n");
633 case DEMANGLE_COMPONENT_CAST
:
636 case DEMANGLE_COMPONENT_UNARY
:
637 printf ("unary operator\n");
639 case DEMANGLE_COMPONENT_BINARY
:
640 printf ("binary operator\n");
642 case DEMANGLE_COMPONENT_BINARY_ARGS
:
643 printf ("binary operator arguments\n");
645 case DEMANGLE_COMPONENT_TRINARY
:
646 printf ("trinary operator\n");
648 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
649 printf ("trinary operator arguments 1\n");
651 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
652 printf ("trinary operator arguments 1\n");
654 case DEMANGLE_COMPONENT_LITERAL
:
655 printf ("literal\n");
657 case DEMANGLE_COMPONENT_LITERAL_NEG
:
658 printf ("negative literal\n");
660 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
661 printf ("java resource\n");
663 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
664 printf ("compound name\n");
666 case DEMANGLE_COMPONENT_CHARACTER
:
667 printf ("character '%c'\n", dc
->u
.s_character
.character
);
669 case DEMANGLE_COMPONENT_DECLTYPE
:
670 printf ("decltype\n");
672 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
673 printf ("pack expansion\n");
677 d_dump (d_left (dc
), indent
+ 2);
678 d_dump (d_right (dc
), indent
+ 2);
681 #endif /* CP_DEMANGLE_DEBUG */
683 /* Fill in a DEMANGLE_COMPONENT_NAME. */
685 CP_STATIC_IF_GLIBCPP_V3
687 cplus_demangle_fill_name (struct demangle_component
*p
, const char *s
, int len
)
689 if (p
== NULL
|| s
== NULL
|| len
== 0)
691 p
->type
= DEMANGLE_COMPONENT_NAME
;
693 p
->u
.s_name
.len
= len
;
697 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
699 CP_STATIC_IF_GLIBCPP_V3
701 cplus_demangle_fill_extended_operator (struct demangle_component
*p
, int args
,
702 struct demangle_component
*name
)
704 if (p
== NULL
|| args
< 0 || name
== NULL
)
706 p
->type
= DEMANGLE_COMPONENT_EXTENDED_OPERATOR
;
707 p
->u
.s_extended_operator
.args
= args
;
708 p
->u
.s_extended_operator
.name
= name
;
712 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
714 CP_STATIC_IF_GLIBCPP_V3
716 cplus_demangle_fill_ctor (struct demangle_component
*p
,
717 enum gnu_v3_ctor_kinds kind
,
718 struct demangle_component
*name
)
722 || (kind
< gnu_v3_complete_object_ctor
723 && kind
> gnu_v3_complete_object_allocating_ctor
))
725 p
->type
= DEMANGLE_COMPONENT_CTOR
;
726 p
->u
.s_ctor
.kind
= kind
;
727 p
->u
.s_ctor
.name
= name
;
731 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
733 CP_STATIC_IF_GLIBCPP_V3
735 cplus_demangle_fill_dtor (struct demangle_component
*p
,
736 enum gnu_v3_dtor_kinds kind
,
737 struct demangle_component
*name
)
741 || (kind
< gnu_v3_deleting_dtor
742 && kind
> gnu_v3_base_object_dtor
))
744 p
->type
= DEMANGLE_COMPONENT_DTOR
;
745 p
->u
.s_dtor
.kind
= kind
;
746 p
->u
.s_dtor
.name
= name
;
750 /* Add a new component. */
752 static struct demangle_component
*
753 d_make_empty (struct d_info
*di
)
755 struct demangle_component
*p
;
757 if (di
->next_comp
>= di
->num_comps
)
759 p
= &di
->comps
[di
->next_comp
];
764 /* Add a new generic component. */
766 static struct demangle_component
*
767 d_make_comp (struct d_info
*di
, enum demangle_component_type type
,
768 struct demangle_component
*left
,
769 struct demangle_component
*right
)
771 struct demangle_component
*p
;
773 /* We check for errors here. A typical error would be a NULL return
774 from a subroutine. We catch those here, and return NULL
778 /* These types require two parameters. */
779 case DEMANGLE_COMPONENT_QUAL_NAME
:
780 case DEMANGLE_COMPONENT_LOCAL_NAME
:
781 case DEMANGLE_COMPONENT_TYPED_NAME
:
782 case DEMANGLE_COMPONENT_TEMPLATE
:
783 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
784 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
785 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
786 case DEMANGLE_COMPONENT_UNARY
:
787 case DEMANGLE_COMPONENT_BINARY
:
788 case DEMANGLE_COMPONENT_BINARY_ARGS
:
789 case DEMANGLE_COMPONENT_TRINARY
:
790 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
791 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
792 case DEMANGLE_COMPONENT_LITERAL
:
793 case DEMANGLE_COMPONENT_LITERAL_NEG
:
794 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
795 if (left
== NULL
|| right
== NULL
)
799 /* These types only require one parameter. */
800 case DEMANGLE_COMPONENT_VTABLE
:
801 case DEMANGLE_COMPONENT_VTT
:
802 case DEMANGLE_COMPONENT_TYPEINFO
:
803 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
804 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
805 case DEMANGLE_COMPONENT_THUNK
:
806 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
807 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
808 case DEMANGLE_COMPONENT_JAVA_CLASS
:
809 case DEMANGLE_COMPONENT_GUARD
:
810 case DEMANGLE_COMPONENT_REFTEMP
:
811 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
812 case DEMANGLE_COMPONENT_POINTER
:
813 case DEMANGLE_COMPONENT_REFERENCE
:
814 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
815 case DEMANGLE_COMPONENT_COMPLEX
:
816 case DEMANGLE_COMPONENT_IMAGINARY
:
817 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
818 case DEMANGLE_COMPONENT_CAST
:
819 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
820 case DEMANGLE_COMPONENT_DECLTYPE
:
821 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
826 /* This needs a right parameter, but the left parameter can be
828 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
833 /* These are allowed to have no parameters--in some cases they
834 will be filled in later. */
835 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
836 case DEMANGLE_COMPONENT_RESTRICT
:
837 case DEMANGLE_COMPONENT_VOLATILE
:
838 case DEMANGLE_COMPONENT_CONST
:
839 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
840 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
841 case DEMANGLE_COMPONENT_CONST_THIS
:
842 case DEMANGLE_COMPONENT_ARGLIST
:
843 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
846 /* Other types should not be seen here. */
851 p
= d_make_empty (di
);
855 p
->u
.s_binary
.left
= left
;
856 p
->u
.s_binary
.right
= right
;
861 /* Add a new name component. */
863 static struct demangle_component
*
864 d_make_name (struct d_info
*di
, const char *s
, int len
)
866 struct demangle_component
*p
;
868 p
= d_make_empty (di
);
869 if (! cplus_demangle_fill_name (p
, s
, len
))
874 /* Add a new builtin type component. */
876 static struct demangle_component
*
877 d_make_builtin_type (struct d_info
*di
,
878 const struct demangle_builtin_type_info
*type
)
880 struct demangle_component
*p
;
884 p
= d_make_empty (di
);
887 p
->type
= DEMANGLE_COMPONENT_BUILTIN_TYPE
;
888 p
->u
.s_builtin
.type
= type
;
893 /* Add a new operator component. */
895 static struct demangle_component
*
896 d_make_operator (struct d_info
*di
, const struct demangle_operator_info
*op
)
898 struct demangle_component
*p
;
900 p
= d_make_empty (di
);
903 p
->type
= DEMANGLE_COMPONENT_OPERATOR
;
904 p
->u
.s_operator
.op
= op
;
909 /* Add a new extended operator component. */
911 static struct demangle_component
*
912 d_make_extended_operator (struct d_info
*di
, int args
,
913 struct demangle_component
*name
)
915 struct demangle_component
*p
;
917 p
= d_make_empty (di
);
918 if (! cplus_demangle_fill_extended_operator (p
, args
, name
))
923 /* Add a new constructor component. */
925 static struct demangle_component
*
926 d_make_ctor (struct d_info
*di
, enum gnu_v3_ctor_kinds kind
,
927 struct demangle_component
*name
)
929 struct demangle_component
*p
;
931 p
= d_make_empty (di
);
932 if (! cplus_demangle_fill_ctor (p
, kind
, name
))
937 /* Add a new destructor component. */
939 static struct demangle_component
*
940 d_make_dtor (struct d_info
*di
, enum gnu_v3_dtor_kinds kind
,
941 struct demangle_component
*name
)
943 struct demangle_component
*p
;
945 p
= d_make_empty (di
);
946 if (! cplus_demangle_fill_dtor (p
, kind
, name
))
951 /* Add a new template parameter. */
953 static struct demangle_component
*
954 d_make_template_param (struct d_info
*di
, long i
)
956 struct demangle_component
*p
;
958 p
= d_make_empty (di
);
961 p
->type
= DEMANGLE_COMPONENT_TEMPLATE_PARAM
;
962 p
->u
.s_number
.number
= i
;
967 /* Add a new standard substitution component. */
969 static struct demangle_component
*
970 d_make_sub (struct d_info
*di
, const char *name
, int len
)
972 struct demangle_component
*p
;
974 p
= d_make_empty (di
);
977 p
->type
= DEMANGLE_COMPONENT_SUB_STD
;
978 p
->u
.s_string
.string
= name
;
979 p
->u
.s_string
.len
= len
;
984 /* <mangled-name> ::= _Z <encoding>
986 TOP_LEVEL is non-zero when called at the top level. */
988 CP_STATIC_IF_GLIBCPP_V3
989 struct demangle_component
*
990 cplus_demangle_mangled_name (struct d_info
*di
, int top_level
)
992 if (! d_check_char (di
, '_'))
994 if (! d_check_char (di
, 'Z'))
996 return d_encoding (di
, top_level
);
999 /* Return whether a function should have a return type. The argument
1000 is the function name, which may be qualified in various ways. The
1001 rules are that template functions have return types with some
1002 exceptions, function types which are not part of a function name
1003 mangling have return types with some exceptions, and non-template
1004 function names do not have return types. The exceptions are that
1005 constructors, destructors, and conversion operators do not have
1009 has_return_type (struct demangle_component
*dc
)
1017 case DEMANGLE_COMPONENT_TEMPLATE
:
1018 return ! is_ctor_dtor_or_conversion (d_left (dc
));
1019 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
1020 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
1021 case DEMANGLE_COMPONENT_CONST_THIS
:
1022 return has_return_type (d_left (dc
));
1026 /* Return whether a name is a constructor, a destructor, or a
1027 conversion operator. */
1030 is_ctor_dtor_or_conversion (struct demangle_component
*dc
)
1038 case DEMANGLE_COMPONENT_QUAL_NAME
:
1039 case DEMANGLE_COMPONENT_LOCAL_NAME
:
1040 return is_ctor_dtor_or_conversion (d_right (dc
));
1041 case DEMANGLE_COMPONENT_CTOR
:
1042 case DEMANGLE_COMPONENT_DTOR
:
1043 case DEMANGLE_COMPONENT_CAST
:
1048 /* <encoding> ::= <(function) name> <bare-function-type>
1052 TOP_LEVEL is non-zero when called at the top level, in which case
1053 if DMGL_PARAMS is not set we do not demangle the function
1054 parameters. We only set this at the top level, because otherwise
1055 we would not correctly demangle names in local scopes. */
1057 static struct demangle_component
*
1058 d_encoding (struct d_info
*di
, int top_level
)
1060 char peek
= d_peek_char (di
);
1062 if (peek
== 'G' || peek
== 'T')
1063 return d_special_name (di
);
1066 struct demangle_component
*dc
;
1070 if (dc
!= NULL
&& top_level
&& (di
->options
& DMGL_PARAMS
) == 0)
1072 /* Strip off any initial CV-qualifiers, as they really apply
1073 to the `this' parameter, and they were not output by the
1074 v2 demangler without DMGL_PARAMS. */
1075 while (dc
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
1076 || dc
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
1077 || dc
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
1080 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1081 there may be CV-qualifiers on its right argument which
1082 really apply here; this happens when parsing a class
1083 which is local to a function. */
1084 if (dc
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
1086 struct demangle_component
*dcr
;
1089 while (dcr
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
1090 || dcr
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
1091 || dcr
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
1093 dc
->u
.s_binary
.right
= dcr
;
1099 peek
= d_peek_char (di
);
1100 if (dc
== NULL
|| peek
== '\0' || peek
== 'E')
1102 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPED_NAME
, dc
,
1103 d_bare_function_type (di
, has_return_type (dc
)));
1107 /* <name> ::= <nested-name>
1109 ::= <unscoped-template-name> <template-args>
1112 <unscoped-name> ::= <unqualified-name>
1113 ::= St <unqualified-name>
1115 <unscoped-template-name> ::= <unscoped-name>
1119 static struct demangle_component
*
1120 d_name (struct d_info
*di
)
1122 char peek
= d_peek_char (di
);
1123 struct demangle_component
*dc
;
1128 return d_nested_name (di
);
1131 return d_local_name (di
);
1134 return d_unqualified_name (di
);
1140 if (d_peek_next_char (di
) != 't')
1142 dc
= d_substitution (di
, 0);
1148 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
,
1149 d_make_name (di
, "std", 3),
1150 d_unqualified_name (di
));
1155 if (d_peek_char (di
) != 'I')
1157 /* The grammar does not permit this case to occur if we
1158 called d_substitution() above (i.e., subst == 1). We
1159 don't bother to check. */
1163 /* This is <template-args>, which means that we just saw
1164 <unscoped-template-name>, which is a substitution
1165 candidate if we didn't just get it from a
1169 if (! d_add_substitution (di
, dc
))
1172 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, dc
,
1173 d_template_args (di
));
1180 dc
= d_unqualified_name (di
);
1181 if (d_peek_char (di
) == 'I')
1183 /* This is <template-args>, which means that we just saw
1184 <unscoped-template-name>, which is a substitution
1186 if (! d_add_substitution (di
, dc
))
1188 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, dc
,
1189 d_template_args (di
));
1195 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1196 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1199 static struct demangle_component
*
1200 d_nested_name (struct d_info
*di
)
1202 struct demangle_component
*ret
;
1203 struct demangle_component
**pret
;
1205 if (! d_check_char (di
, 'N'))
1208 pret
= d_cv_qualifiers (di
, &ret
, 1);
1212 *pret
= d_prefix (di
);
1216 if (! d_check_char (di
, 'E'))
1222 /* <prefix> ::= <prefix> <unqualified-name>
1223 ::= <template-prefix> <template-args>
1224 ::= <template-param>
1228 <template-prefix> ::= <prefix> <(template) unqualified-name>
1229 ::= <template-param>
1233 static struct demangle_component
*
1234 d_prefix (struct d_info
*di
)
1236 struct demangle_component
*ret
= NULL
;
1241 enum demangle_component_type comb_type
;
1242 struct demangle_component
*dc
;
1244 peek
= d_peek_char (di
);
1248 /* The older code accepts a <local-name> here, but I don't see
1249 that in the grammar. The older code does not accept a
1250 <template-param> here. */
1252 comb_type
= DEMANGLE_COMPONENT_QUAL_NAME
;
1258 dc
= d_unqualified_name (di
);
1259 else if (peek
== 'S')
1260 dc
= d_substitution (di
, 1);
1261 else if (peek
== 'I')
1265 comb_type
= DEMANGLE_COMPONENT_TEMPLATE
;
1266 dc
= d_template_args (di
);
1268 else if (peek
== 'T')
1269 dc
= d_template_param (di
);
1270 else if (peek
== 'E')
1278 ret
= d_make_comp (di
, comb_type
, ret
, dc
);
1280 if (peek
!= 'S' && d_peek_char (di
) != 'E')
1282 if (! d_add_substitution (di
, ret
))
1288 /* <unqualified-name> ::= <operator-name>
1289 ::= <ctor-dtor-name>
1291 ::= <local-source-name>
1293 <local-source-name> ::= L <source-name> <discriminator>
1296 static struct demangle_component
*
1297 d_unqualified_name (struct d_info
*di
)
1301 peek
= d_peek_char (di
);
1302 if (IS_DIGIT (peek
))
1303 return d_source_name (di
);
1304 else if (IS_LOWER (peek
))
1306 struct demangle_component
*ret
;
1308 ret
= d_operator_name (di
);
1309 if (ret
!= NULL
&& ret
->type
== DEMANGLE_COMPONENT_OPERATOR
)
1310 di
->expansion
+= sizeof "operator" + ret
->u
.s_operator
.op
->len
- 2;
1313 else if (peek
== 'C' || peek
== 'D')
1314 return d_ctor_dtor_name (di
);
1315 else if (peek
== 'L')
1317 struct demangle_component
* ret
;
1321 ret
= d_source_name (di
);
1324 if (! d_discriminator (di
))
1332 /* <source-name> ::= <(positive length) number> <identifier> */
1334 static struct demangle_component
*
1335 d_source_name (struct d_info
*di
)
1338 struct demangle_component
*ret
;
1340 len
= d_number (di
);
1343 ret
= d_identifier (di
, len
);
1344 di
->last_name
= ret
;
1348 /* number ::= [n] <(non-negative decimal integer)> */
1351 d_number (struct d_info
*di
)
1358 peek
= d_peek_char (di
);
1363 peek
= d_peek_char (di
);
1369 if (! IS_DIGIT (peek
))
1375 ret
= ret
* 10 + peek
- '0';
1377 peek
= d_peek_char (di
);
1381 /* identifier ::= <(unqualified source code identifier)> */
1383 static struct demangle_component
*
1384 d_identifier (struct d_info
*di
, int len
)
1390 if (di
->send
- name
< len
)
1393 d_advance (di
, len
);
1395 /* A Java mangled name may have a trailing '$' if it is a C++
1396 keyword. This '$' is not included in the length count. We just
1398 if ((di
->options
& DMGL_JAVA
) != 0
1399 && d_peek_char (di
) == '$')
1402 /* Look for something which looks like a gcc encoding of an
1403 anonymous namespace, and replace it with a more user friendly
1405 if (len
>= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN
+ 2
1406 && memcmp (name
, ANONYMOUS_NAMESPACE_PREFIX
,
1407 ANONYMOUS_NAMESPACE_PREFIX_LEN
) == 0)
1411 s
= name
+ ANONYMOUS_NAMESPACE_PREFIX_LEN
;
1412 if ((*s
== '.' || *s
== '_' || *s
== '$')
1415 di
->expansion
-= len
- sizeof "(anonymous namespace)";
1416 return d_make_name (di
, "(anonymous namespace)",
1417 sizeof "(anonymous namespace)" - 1);
1421 return d_make_name (di
, name
, len
);
1424 /* operator_name ::= many different two character encodings.
1426 ::= v <digit> <source-name>
1429 #define NL(s) s, (sizeof s) - 1
1431 CP_STATIC_IF_GLIBCPP_V3
1432 const struct demangle_operator_info cplus_demangle_operators
[] =
1434 { "aN", NL ("&="), 2 },
1435 { "aS", NL ("="), 2 },
1436 { "aa", NL ("&&"), 2 },
1437 { "ad", NL ("&"), 1 },
1438 { "an", NL ("&"), 2 },
1439 { "cl", NL ("()"), 2 },
1440 { "cm", NL (","), 2 },
1441 { "co", NL ("~"), 1 },
1442 { "dV", NL ("/="), 2 },
1443 { "da", NL ("delete[]"), 1 },
1444 { "de", NL ("*"), 1 },
1445 { "dl", NL ("delete"), 1 },
1446 { "dt", NL ("."), 2 },
1447 { "dv", NL ("/"), 2 },
1448 { "eO", NL ("^="), 2 },
1449 { "eo", NL ("^"), 2 },
1450 { "eq", NL ("=="), 2 },
1451 { "ge", NL (">="), 2 },
1452 { "gt", NL (">"), 2 },
1453 { "ix", NL ("[]"), 2 },
1454 { "lS", NL ("<<="), 2 },
1455 { "le", NL ("<="), 2 },
1456 { "ls", NL ("<<"), 2 },
1457 { "lt", NL ("<"), 2 },
1458 { "mI", NL ("-="), 2 },
1459 { "mL", NL ("*="), 2 },
1460 { "mi", NL ("-"), 2 },
1461 { "ml", NL ("*"), 2 },
1462 { "mm", NL ("--"), 1 },
1463 { "na", NL ("new[]"), 1 },
1464 { "ne", NL ("!="), 2 },
1465 { "ng", NL ("-"), 1 },
1466 { "nt", NL ("!"), 1 },
1467 { "nw", NL ("new"), 1 },
1468 { "oR", NL ("|="), 2 },
1469 { "oo", NL ("||"), 2 },
1470 { "or", NL ("|"), 2 },
1471 { "pL", NL ("+="), 2 },
1472 { "pl", NL ("+"), 2 },
1473 { "pm", NL ("->*"), 2 },
1474 { "pp", NL ("++"), 1 },
1475 { "ps", NL ("+"), 1 },
1476 { "pt", NL ("->"), 2 },
1477 { "qu", NL ("?"), 3 },
1478 { "rM", NL ("%="), 2 },
1479 { "rS", NL (">>="), 2 },
1480 { "rm", NL ("%"), 2 },
1481 { "rs", NL (">>"), 2 },
1482 { "st", NL ("sizeof "), 1 },
1483 { "sz", NL ("sizeof "), 1 },
1484 { NULL
, NULL
, 0, 0 }
1487 static struct demangle_component
*
1488 d_operator_name (struct d_info
*di
)
1493 c1
= d_next_char (di
);
1494 c2
= d_next_char (di
);
1495 if (c1
== 'v' && IS_DIGIT (c2
))
1496 return d_make_extended_operator (di
, c2
- '0', d_source_name (di
));
1497 else if (c1
== 'c' && c2
== 'v')
1498 return d_make_comp (di
, DEMANGLE_COMPONENT_CAST
,
1499 cplus_demangle_type (di
), NULL
);
1502 /* LOW is the inclusive lower bound. */
1504 /* HIGH is the exclusive upper bound. We subtract one to ignore
1505 the sentinel at the end of the array. */
1506 int high
= ((sizeof (cplus_demangle_operators
)
1507 / sizeof (cplus_demangle_operators
[0]))
1513 const struct demangle_operator_info
*p
;
1515 i
= low
+ (high
- low
) / 2;
1516 p
= cplus_demangle_operators
+ i
;
1518 if (c1
== p
->code
[0] && c2
== p
->code
[1])
1519 return d_make_operator (di
, p
);
1521 if (c1
< p
->code
[0] || (c1
== p
->code
[0] && c2
< p
->code
[1]))
1531 static struct demangle_component
*
1532 d_make_character (struct d_info
*di
, int c
)
1534 struct demangle_component
*p
;
1535 p
= d_make_empty (di
);
1538 p
->type
= DEMANGLE_COMPONENT_CHARACTER
;
1539 p
->u
.s_character
.character
= c
;
1544 static struct demangle_component
*
1545 d_java_resource (struct d_info
*di
)
1547 struct demangle_component
*p
= NULL
;
1548 struct demangle_component
*next
= NULL
;
1553 len
= d_number (di
);
1557 /* Eat the leading '_'. */
1558 if (d_next_char (di
) != '_')
1571 /* Each chunk is either a '$' escape... */
1589 next
= d_make_character (di
, c
);
1597 /* ... or a sequence of characters. */
1600 while (i
< len
&& str
[i
] && str
[i
] != '$')
1603 next
= d_make_name (di
, str
, i
);
1616 p
= d_make_comp (di
, DEMANGLE_COMPONENT_COMPOUND_NAME
, p
, next
);
1622 p
= d_make_comp (di
, DEMANGLE_COMPONENT_JAVA_RESOURCE
, p
, NULL
);
1627 /* <special-name> ::= TV <type>
1631 ::= GV <(object) name>
1632 ::= T <call-offset> <(base) encoding>
1633 ::= Tc <call-offset> <call-offset> <(base) encoding>
1634 Also g++ extensions:
1635 ::= TC <type> <(offset) number> _ <(base) type>
1640 ::= Gr <resource name>
1643 static struct demangle_component
*
1644 d_special_name (struct d_info
*di
)
1646 di
->expansion
+= 20;
1647 if (d_check_char (di
, 'T'))
1649 switch (d_next_char (di
))
1653 return d_make_comp (di
, DEMANGLE_COMPONENT_VTABLE
,
1654 cplus_demangle_type (di
), NULL
);
1656 di
->expansion
-= 10;
1657 return d_make_comp (di
, DEMANGLE_COMPONENT_VTT
,
1658 cplus_demangle_type (di
), NULL
);
1660 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO
,
1661 cplus_demangle_type (di
), NULL
);
1663 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO_NAME
,
1664 cplus_demangle_type (di
), NULL
);
1667 if (! d_call_offset (di
, 'h'))
1669 return d_make_comp (di
, DEMANGLE_COMPONENT_THUNK
,
1670 d_encoding (di
, 0), NULL
);
1673 if (! d_call_offset (di
, 'v'))
1675 return d_make_comp (di
, DEMANGLE_COMPONENT_VIRTUAL_THUNK
,
1676 d_encoding (di
, 0), NULL
);
1679 if (! d_call_offset (di
, '\0'))
1681 if (! d_call_offset (di
, '\0'))
1683 return d_make_comp (di
, DEMANGLE_COMPONENT_COVARIANT_THUNK
,
1684 d_encoding (di
, 0), NULL
);
1688 struct demangle_component
*derived_type
;
1690 struct demangle_component
*base_type
;
1692 derived_type
= cplus_demangle_type (di
);
1693 offset
= d_number (di
);
1696 if (! d_check_char (di
, '_'))
1698 base_type
= cplus_demangle_type (di
);
1699 /* We don't display the offset. FIXME: We should display
1700 it in verbose mode. */
1702 return d_make_comp (di
, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
,
1703 base_type
, derived_type
);
1707 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO_FN
,
1708 cplus_demangle_type (di
), NULL
);
1710 return d_make_comp (di
, DEMANGLE_COMPONENT_JAVA_CLASS
,
1711 cplus_demangle_type (di
), NULL
);
1717 else if (d_check_char (di
, 'G'))
1719 switch (d_next_char (di
))
1722 return d_make_comp (di
, DEMANGLE_COMPONENT_GUARD
, d_name (di
), NULL
);
1725 return d_make_comp (di
, DEMANGLE_COMPONENT_REFTEMP
, d_name (di
),
1729 return d_make_comp (di
, DEMANGLE_COMPONENT_HIDDEN_ALIAS
,
1730 d_encoding (di
, 0), NULL
);
1733 return d_java_resource (di
);
1743 /* <call-offset> ::= h <nv-offset> _
1746 <nv-offset> ::= <(offset) number>
1748 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1750 The C parameter, if not '\0', is a character we just read which is
1751 the start of the <call-offset>.
1753 We don't display the offset information anywhere. FIXME: We should
1754 display it in verbose mode. */
1757 d_call_offset (struct d_info
*di
, int c
)
1760 c
= d_next_char (di
);
1767 if (! d_check_char (di
, '_'))
1774 if (! d_check_char (di
, '_'))
1780 /* <ctor-dtor-name> ::= C1
1788 static struct demangle_component
*
1789 d_ctor_dtor_name (struct d_info
*di
)
1791 if (di
->last_name
!= NULL
)
1793 if (di
->last_name
->type
== DEMANGLE_COMPONENT_NAME
)
1794 di
->expansion
+= di
->last_name
->u
.s_name
.len
;
1795 else if (di
->last_name
->type
== DEMANGLE_COMPONENT_SUB_STD
)
1796 di
->expansion
+= di
->last_name
->u
.s_string
.len
;
1798 switch (d_peek_char (di
))
1802 enum gnu_v3_ctor_kinds kind
;
1804 switch (d_peek_next_char (di
))
1807 kind
= gnu_v3_complete_object_ctor
;
1810 kind
= gnu_v3_base_object_ctor
;
1813 kind
= gnu_v3_complete_object_allocating_ctor
;
1819 return d_make_ctor (di
, kind
, di
->last_name
);
1824 enum gnu_v3_dtor_kinds kind
;
1826 switch (d_peek_next_char (di
))
1829 kind
= gnu_v3_deleting_dtor
;
1832 kind
= gnu_v3_complete_object_dtor
;
1835 kind
= gnu_v3_base_object_dtor
;
1841 return d_make_dtor (di
, kind
, di
->last_name
);
1849 /* <type> ::= <builtin-type>
1851 ::= <class-enum-type>
1853 ::= <pointer-to-member-type>
1854 ::= <template-param>
1855 ::= <template-template-param> <template-args>
1857 ::= <CV-qualifiers> <type>
1860 ::= O <type> (C++0x)
1863 ::= U <source-name> <type>
1865 <builtin-type> ::= various one letter codes
1869 CP_STATIC_IF_GLIBCPP_V3
1870 const struct demangle_builtin_type_info
1871 cplus_demangle_builtin_types
[D_BUILTIN_TYPE_COUNT
] =
1873 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT
},
1874 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL
},
1875 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT
},
1876 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT
},
1877 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT
},
1878 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT
},
1879 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT
},
1880 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT
},
1881 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT
},
1882 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED
},
1883 /* k */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
1884 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG
},
1885 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG
},
1886 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT
},
1887 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
1889 /* p */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
1890 /* q */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
1891 /* r */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
1892 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT
},
1893 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT
},
1894 /* u */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
1895 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID
},
1896 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT
},
1897 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG
},
1898 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
1899 D_PRINT_UNSIGNED_LONG_LONG
},
1900 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT
},
1901 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT
},
1902 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT
},
1903 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT
},
1904 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT
},
1905 /* 30 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT
},
1906 /* 31 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT
},
1909 CP_STATIC_IF_GLIBCPP_V3
1910 struct demangle_component
*
1911 cplus_demangle_type (struct d_info
*di
)
1914 struct demangle_component
*ret
;
1917 /* The ABI specifies that when CV-qualifiers are used, the base type
1918 is substitutable, and the fully qualified type is substitutable,
1919 but the base type with a strict subset of the CV-qualifiers is
1920 not substitutable. The natural recursive implementation of the
1921 CV-qualifiers would cause subsets to be substitutable, so instead
1922 we pull them all off now.
1924 FIXME: The ABI says that order-insensitive vendor qualifiers
1925 should be handled in the same way, but we have no way to tell
1926 which vendor qualifiers are order-insensitive and which are
1927 order-sensitive. So we just assume that they are all
1928 order-sensitive. g++ 3.4 supports only one vendor qualifier,
1929 __vector, and it treats it as order-sensitive when mangling
1932 peek
= d_peek_char (di
);
1933 if (peek
== 'r' || peek
== 'V' || peek
== 'K')
1935 struct demangle_component
**pret
;
1937 pret
= d_cv_qualifiers (di
, &ret
, 0);
1940 *pret
= cplus_demangle_type (di
);
1941 if (! *pret
|| ! d_add_substitution (di
, ret
))
1950 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1951 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
1952 case 'o': case 's': case 't':
1953 case 'v': case 'w': case 'x': case 'y': case 'z':
1954 ret
= d_make_builtin_type (di
,
1955 &cplus_demangle_builtin_types
[peek
- 'a']);
1956 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
1963 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_VENDOR_TYPE
,
1964 d_source_name (di
), NULL
);
1968 ret
= d_function_type (di
);
1971 case '0': case '1': case '2': case '3': case '4':
1972 case '5': case '6': case '7': case '8': case '9':
1975 ret
= d_class_enum_type (di
);
1979 ret
= d_array_type (di
);
1983 ret
= d_pointer_to_member_type (di
);
1987 ret
= d_template_param (di
);
1988 if (d_peek_char (di
) == 'I')
1990 /* This is <template-template-param> <template-args>. The
1991 <template-template-param> part is a substitution
1993 if (! d_add_substitution (di
, ret
))
1995 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
,
1996 d_template_args (di
));
2001 /* If this is a special substitution, then it is the start of
2002 <class-enum-type>. */
2006 peek_next
= d_peek_next_char (di
);
2007 if (IS_DIGIT (peek_next
)
2009 || IS_UPPER (peek_next
))
2011 ret
= d_substitution (di
, 0);
2012 /* The substituted name may have been a template name and
2013 may be followed by tepmlate args. */
2014 if (d_peek_char (di
) == 'I')
2015 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
,
2016 d_template_args (di
));
2022 ret
= d_class_enum_type (di
);
2023 /* If the substitution was a complete type, then it is not
2024 a new substitution candidate. However, if the
2025 substitution was followed by template arguments, then
2026 the whole thing is a substitution candidate. */
2027 if (ret
!= NULL
&& ret
->type
== DEMANGLE_COMPONENT_SUB_STD
)
2035 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_RVALUE_REFERENCE
,
2036 cplus_demangle_type (di
), NULL
);
2041 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_POINTER
,
2042 cplus_demangle_type (di
), NULL
);
2047 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_REFERENCE
,
2048 cplus_demangle_type (di
), NULL
);
2053 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_COMPLEX
,
2054 cplus_demangle_type (di
), NULL
);
2059 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_IMAGINARY
,
2060 cplus_demangle_type (di
), NULL
);
2065 ret
= d_source_name (di
);
2066 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
,
2067 cplus_demangle_type (di
), ret
);
2073 peek
= d_next_char (di
);
2078 /* decltype (expression) */
2079 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_DECLTYPE
,
2080 d_expression (di
), NULL
);
2081 if (ret
&& d_next_char (di
) != 'E')
2086 /* Pack expansion. */
2087 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_PACK_EXPANSION
,
2088 cplus_demangle_type (di
), NULL
);
2092 /* 32-bit decimal floating point */
2093 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[26]);
2094 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2098 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[27]);
2099 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2103 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[28]);
2104 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2107 /* 16-bit half-precision FP */
2108 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[29]);
2109 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2113 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[30]);
2114 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2118 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[31]);
2119 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2123 /* Fixed point types. DF<int bits><length><fract bits><sat> */
2124 ret
= d_make_empty (di
);
2125 ret
->type
= DEMANGLE_COMPONENT_FIXED_TYPE
;
2126 if ((ret
->u
.s_fixed
.accum
= IS_DIGIT (d_peek_char (di
))))
2127 /* For demangling we don't care about the bits. */
2129 ret
->u
.s_fixed
.length
= cplus_demangle_type (di
);
2131 peek
= d_next_char (di
);
2132 ret
->u
.s_fixed
.sat
= (peek
== 's');
2143 if (! d_add_substitution (di
, ret
))
2150 /* <CV-qualifiers> ::= [r] [V] [K] */
2152 static struct demangle_component
**
2153 d_cv_qualifiers (struct d_info
*di
,
2154 struct demangle_component
**pret
, int member_fn
)
2158 peek
= d_peek_char (di
);
2159 while (peek
== 'r' || peek
== 'V' || peek
== 'K')
2161 enum demangle_component_type t
;
2167 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2168 : DEMANGLE_COMPONENT_RESTRICT
);
2169 di
->expansion
+= sizeof "restrict";
2171 else if (peek
== 'V')
2174 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2175 : DEMANGLE_COMPONENT_VOLATILE
);
2176 di
->expansion
+= sizeof "volatile";
2181 ? DEMANGLE_COMPONENT_CONST_THIS
2182 : DEMANGLE_COMPONENT_CONST
);
2183 di
->expansion
+= sizeof "const";
2186 *pret
= d_make_comp (di
, t
, NULL
, NULL
);
2189 pret
= &d_left (*pret
);
2191 peek
= d_peek_char (di
);
2197 /* <function-type> ::= F [Y] <bare-function-type> E */
2199 static struct demangle_component
*
2200 d_function_type (struct d_info
*di
)
2202 struct demangle_component
*ret
;
2204 if (! d_check_char (di
, 'F'))
2206 if (d_peek_char (di
) == 'Y')
2208 /* Function has C linkage. We don't print this information.
2209 FIXME: We should print it in verbose mode. */
2212 ret
= d_bare_function_type (di
, 1);
2213 if (! d_check_char (di
, 'E'))
2218 /* <bare-function-type> ::= [J]<type>+ */
2220 static struct demangle_component
*
2221 d_bare_function_type (struct d_info
*di
, int has_return_type
)
2223 struct demangle_component
*return_type
;
2224 struct demangle_component
*tl
;
2225 struct demangle_component
**ptl
;
2228 /* Detect special qualifier indicating that the first argument
2229 is the return type. */
2230 peek
= d_peek_char (di
);
2234 has_return_type
= 1;
2242 struct demangle_component
*type
;
2244 peek
= d_peek_char (di
);
2245 if (peek
== '\0' || peek
== 'E')
2247 type
= cplus_demangle_type (di
);
2250 if (has_return_type
)
2253 has_return_type
= 0;
2257 *ptl
= d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, type
, NULL
);
2260 ptl
= &d_right (*ptl
);
2264 /* There should be at least one parameter type besides the optional
2265 return type. A function which takes no arguments will have a
2266 single parameter type void. */
2270 /* If we have a single parameter type void, omit it. */
2271 if (d_right (tl
) == NULL
2272 && d_left (tl
)->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
2273 && d_left (tl
)->u
.s_builtin
.type
->print
== D_PRINT_VOID
)
2275 di
->expansion
-= d_left (tl
)->u
.s_builtin
.type
->len
;
2279 return d_make_comp (di
, DEMANGLE_COMPONENT_FUNCTION_TYPE
, return_type
, tl
);
2282 /* <class-enum-type> ::= <name> */
2284 static struct demangle_component
*
2285 d_class_enum_type (struct d_info
*di
)
2290 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2291 ::= A [<(dimension) expression>] _ <(element) type>
2294 static struct demangle_component
*
2295 d_array_type (struct d_info
*di
)
2298 struct demangle_component
*dim
;
2300 if (! d_check_char (di
, 'A'))
2303 peek
= d_peek_char (di
);
2306 else if (IS_DIGIT (peek
))
2314 peek
= d_peek_char (di
);
2316 while (IS_DIGIT (peek
));
2317 dim
= d_make_name (di
, s
, d_str (di
) - s
);
2323 dim
= d_expression (di
);
2328 if (! d_check_char (di
, '_'))
2331 return d_make_comp (di
, DEMANGLE_COMPONENT_ARRAY_TYPE
, dim
,
2332 cplus_demangle_type (di
));
2335 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2337 static struct demangle_component
*
2338 d_pointer_to_member_type (struct d_info
*di
)
2340 struct demangle_component
*cl
;
2341 struct demangle_component
*mem
;
2342 struct demangle_component
**pmem
;
2344 if (! d_check_char (di
, 'M'))
2347 cl
= cplus_demangle_type (di
);
2349 /* The ABI specifies that any type can be a substitution source, and
2350 that M is followed by two types, and that when a CV-qualified
2351 type is seen both the base type and the CV-qualified types are
2352 substitution sources. The ABI also specifies that for a pointer
2353 to a CV-qualified member function, the qualifiers are attached to
2354 the second type. Given the grammar, a plain reading of the ABI
2355 suggests that both the CV-qualified member function and the
2356 non-qualified member function are substitution sources. However,
2357 g++ does not work that way. g++ treats only the CV-qualified
2358 member function as a substitution source. FIXME. So to work
2359 with g++, we need to pull off the CV-qualifiers here, in order to
2360 avoid calling add_substitution() in cplus_demangle_type(). But
2361 for a CV-qualified member which is not a function, g++ does
2362 follow the ABI, so we need to handle that case here by calling
2363 d_add_substitution ourselves. */
2365 pmem
= d_cv_qualifiers (di
, &mem
, 1);
2368 *pmem
= cplus_demangle_type (di
);
2372 if (pmem
!= &mem
&& (*pmem
)->type
!= DEMANGLE_COMPONENT_FUNCTION_TYPE
)
2374 if (! d_add_substitution (di
, mem
))
2378 return d_make_comp (di
, DEMANGLE_COMPONENT_PTRMEM_TYPE
, cl
, mem
);
2381 /* <template-param> ::= T_
2382 ::= T <(parameter-2 non-negative) number> _
2385 static struct demangle_component
*
2386 d_template_param (struct d_info
*di
)
2390 if (! d_check_char (di
, 'T'))
2393 if (d_peek_char (di
) == '_')
2397 param
= d_number (di
);
2403 if (! d_check_char (di
, '_'))
2408 return d_make_template_param (di
, param
);
2411 /* <template-args> ::= I <template-arg>+ E */
2413 static struct demangle_component
*
2414 d_template_args (struct d_info
*di
)
2416 struct demangle_component
*hold_last_name
;
2417 struct demangle_component
*al
;
2418 struct demangle_component
**pal
;
2420 /* Preserve the last name we saw--don't let the template arguments
2421 clobber it, as that would give us the wrong name for a subsequent
2422 constructor or destructor. */
2423 hold_last_name
= di
->last_name
;
2425 if (! d_check_char (di
, 'I'))
2428 if (d_peek_char (di
) == 'E')
2430 /* An argument pack can be empty. */
2432 return d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
, NULL
, NULL
);
2439 struct demangle_component
*a
;
2441 a
= d_template_arg (di
);
2445 *pal
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
, a
, NULL
);
2448 pal
= &d_right (*pal
);
2450 if (d_peek_char (di
) == 'E')
2457 di
->last_name
= hold_last_name
;
2462 /* <template-arg> ::= <type>
2463 ::= X <expression> E
2467 static struct demangle_component
*
2468 d_template_arg (struct d_info
*di
)
2470 struct demangle_component
*ret
;
2472 switch (d_peek_char (di
))
2476 ret
= d_expression (di
);
2477 if (! d_check_char (di
, 'E'))
2482 return d_expr_primary (di
);
2485 /* An argument pack. */
2486 return d_template_args (di
);
2489 return cplus_demangle_type (di
);
2493 /* Subroutine of <expression> ::= cl <expression>+ E */
2495 static struct demangle_component
*
2496 d_exprlist (struct d_info
*di
)
2498 struct demangle_component
*list
= NULL
;
2499 struct demangle_component
**p
= &list
;
2501 if (d_peek_char (di
) == 'E')
2504 return d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, NULL
, NULL
);
2509 struct demangle_component
*arg
= d_expression (di
);
2513 *p
= d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, arg
, NULL
);
2518 if (d_peek_char (di
) == 'E')
2528 /* <expression> ::= <(unary) operator-name> <expression>
2529 ::= <(binary) operator-name> <expression> <expression>
2530 ::= <(trinary) operator-name> <expression> <expression> <expression>
2531 ::= cl <expression>+ E
2533 ::= <template-param>
2534 ::= sr <type> <unqualified-name>
2535 ::= sr <type> <unqualified-name> <template-args>
2539 static struct demangle_component
*
2540 d_expression (struct d_info
*di
)
2544 peek
= d_peek_char (di
);
2546 return d_expr_primary (di
);
2547 else if (peek
== 'T')
2548 return d_template_param (di
);
2549 else if (peek
== 's' && d_peek_next_char (di
) == 'r')
2551 struct demangle_component
*type
;
2552 struct demangle_component
*name
;
2555 type
= cplus_demangle_type (di
);
2556 name
= d_unqualified_name (di
);
2557 if (d_peek_char (di
) != 'I')
2558 return d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
, type
, name
);
2560 return d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
, type
,
2561 d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, name
,
2562 d_template_args (di
)));
2564 else if (peek
== 's'
2565 && (d_peek_next_char (di
) == 'T' || d_peek_next_char (di
) == 'R'))
2567 /* Just demangle a parameter placeholder as its type. */
2569 return cplus_demangle_type (di
);
2571 else if (IS_DIGIT (peek
))
2573 /* We can get an unqualified name as an expression in the case of
2574 a dependent member access, i.e. decltype(T().i). */
2575 struct demangle_component
*name
= d_unqualified_name (di
);
2578 if (d_peek_char (di
) == 'I')
2579 return d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, name
,
2580 d_template_args (di
));
2586 struct demangle_component
*op
;
2589 op
= d_operator_name (di
);
2593 if (op
->type
== DEMANGLE_COMPONENT_OPERATOR
)
2594 di
->expansion
+= op
->u
.s_operator
.op
->len
- 2;
2596 if (op
->type
== DEMANGLE_COMPONENT_OPERATOR
2597 && strcmp (op
->u
.s_operator
.op
->code
, "st") == 0)
2598 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
2599 cplus_demangle_type (di
));
2605 case DEMANGLE_COMPONENT_OPERATOR
:
2606 args
= op
->u
.s_operator
.op
->args
;
2608 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
2609 args
= op
->u
.s_extended_operator
.args
;
2611 case DEMANGLE_COMPONENT_CAST
:
2612 if (d_peek_char (di
) == 'v')
2613 /* T() encoded as an operand of void. */
2614 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
2615 cplus_demangle_type (di
));
2624 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
2628 struct demangle_component
*left
;
2629 struct demangle_component
*right
;
2631 left
= d_expression (di
);
2632 if (!strcmp (op
->u
.s_operator
.op
->code
, "cl"))
2633 right
= d_exprlist (di
);
2635 right
= d_expression (di
);
2637 return d_make_comp (di
, DEMANGLE_COMPONENT_BINARY
, op
,
2639 DEMANGLE_COMPONENT_BINARY_ARGS
,
2644 struct demangle_component
*first
;
2645 struct demangle_component
*second
;
2647 first
= d_expression (di
);
2648 second
= d_expression (di
);
2649 return d_make_comp (di
, DEMANGLE_COMPONENT_TRINARY
, op
,
2651 DEMANGLE_COMPONENT_TRINARY_ARG1
,
2654 DEMANGLE_COMPONENT_TRINARY_ARG2
,
2656 d_expression (di
))));
2664 /* <expr-primary> ::= L <type> <(value) number> E
2665 ::= L <type> <(value) float> E
2666 ::= L <mangled-name> E
2669 static struct demangle_component
*
2670 d_expr_primary (struct d_info
*di
)
2672 struct demangle_component
*ret
;
2674 if (! d_check_char (di
, 'L'))
2676 if (d_peek_char (di
) == '_')
2677 ret
= cplus_demangle_mangled_name (di
, 0);
2680 struct demangle_component
*type
;
2681 enum demangle_component_type t
;
2684 type
= cplus_demangle_type (di
);
2688 /* If we have a type we know how to print, we aren't going to
2689 print the type name itself. */
2690 if (type
->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
2691 && type
->u
.s_builtin
.type
->print
!= D_PRINT_DEFAULT
)
2692 di
->expansion
-= type
->u
.s_builtin
.type
->len
;
2694 /* Rather than try to interpret the literal value, we just
2695 collect it as a string. Note that it's possible to have a
2696 floating point literal here. The ABI specifies that the
2697 format of such literals is machine independent. That's fine,
2698 but what's not fine is that versions of g++ up to 3.2 with
2699 -fabi-version=1 used upper case letters in the hex constant,
2700 and dumped out gcc's internal representation. That makes it
2701 hard to tell where the constant ends, and hard to dump the
2702 constant in any readable form anyhow. We don't attempt to
2703 handle these cases. */
2705 t
= DEMANGLE_COMPONENT_LITERAL
;
2706 if (d_peek_char (di
) == 'n')
2708 t
= DEMANGLE_COMPONENT_LITERAL_NEG
;
2712 while (d_peek_char (di
) != 'E')
2714 if (d_peek_char (di
) == '\0')
2718 ret
= d_make_comp (di
, t
, type
, d_make_name (di
, s
, d_str (di
) - s
));
2720 if (! d_check_char (di
, 'E'))
2725 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2726 ::= Z <(function) encoding> E s [<discriminator>]
2729 static struct demangle_component
*
2730 d_local_name (struct d_info
*di
)
2732 struct demangle_component
*function
;
2734 if (! d_check_char (di
, 'Z'))
2737 function
= d_encoding (di
, 0);
2739 if (! d_check_char (di
, 'E'))
2742 if (d_peek_char (di
) == 's')
2745 if (! d_discriminator (di
))
2747 return d_make_comp (di
, DEMANGLE_COMPONENT_LOCAL_NAME
, function
,
2748 d_make_name (di
, "string literal",
2749 sizeof "string literal" - 1));
2753 struct demangle_component
*name
;
2756 if (! d_discriminator (di
))
2758 return d_make_comp (di
, DEMANGLE_COMPONENT_LOCAL_NAME
, function
, name
);
2762 /* <discriminator> ::= _ <(non-negative) number>
2764 We demangle the discriminator, but we don't print it out. FIXME:
2765 We should print it out in verbose mode. */
2768 d_discriminator (struct d_info
*di
)
2772 if (d_peek_char (di
) != '_')
2775 discrim
= d_number (di
);
2781 /* Add a new substitution. */
2784 d_add_substitution (struct d_info
*di
, struct demangle_component
*dc
)
2788 if (di
->next_sub
>= di
->num_subs
)
2790 di
->subs
[di
->next_sub
] = dc
;
2795 /* <substitution> ::= S <seq-id> _
2805 If PREFIX is non-zero, then this type is being used as a prefix in
2806 a qualified name. In this case, for the standard substitutions, we
2807 need to check whether we are being used as a prefix for a
2808 constructor or destructor, and return a full template name.
2809 Otherwise we will get something like std::iostream::~iostream()
2810 which does not correspond particularly well to any function which
2811 actually appears in the source.
2814 static const struct d_standard_sub_info standard_subs
[] =
2819 { 'a', NL ("std::allocator"),
2820 NL ("std::allocator"),
2822 { 'b', NL ("std::basic_string"),
2823 NL ("std::basic_string"),
2824 NL ("basic_string") },
2825 { 's', NL ("std::string"),
2826 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
2827 NL ("basic_string") },
2828 { 'i', NL ("std::istream"),
2829 NL ("std::basic_istream<char, std::char_traits<char> >"),
2830 NL ("basic_istream") },
2831 { 'o', NL ("std::ostream"),
2832 NL ("std::basic_ostream<char, std::char_traits<char> >"),
2833 NL ("basic_ostream") },
2834 { 'd', NL ("std::iostream"),
2835 NL ("std::basic_iostream<char, std::char_traits<char> >"),
2836 NL ("basic_iostream") }
2839 static struct demangle_component
*
2840 d_substitution (struct d_info
*di
, int prefix
)
2844 if (! d_check_char (di
, 'S'))
2847 c
= d_next_char (di
);
2848 if (c
== '_' || IS_DIGIT (c
) || IS_UPPER (c
))
2857 unsigned int new_id
;
2860 new_id
= id
* 36 + c
- '0';
2861 else if (IS_UPPER (c
))
2862 new_id
= id
* 36 + c
- 'A' + 10;
2868 c
= d_next_char (di
);
2875 if (id
>= (unsigned int) di
->next_sub
)
2880 return di
->subs
[id
];
2885 const struct d_standard_sub_info
*p
;
2886 const struct d_standard_sub_info
*pend
;
2888 verbose
= (di
->options
& DMGL_VERBOSE
) != 0;
2889 if (! verbose
&& prefix
)
2893 peek
= d_peek_char (di
);
2894 if (peek
== 'C' || peek
== 'D')
2898 pend
= (&standard_subs
[0]
2899 + sizeof standard_subs
/ sizeof standard_subs
[0]);
2900 for (p
= &standard_subs
[0]; p
< pend
; ++p
)
2907 if (p
->set_last_name
!= NULL
)
2908 di
->last_name
= d_make_sub (di
, p
->set_last_name
,
2909 p
->set_last_name_len
);
2912 s
= p
->full_expansion
;
2917 s
= p
->simple_expansion
;
2918 len
= p
->simple_len
;
2920 di
->expansion
+= len
;
2921 return d_make_sub (di
, s
, len
);
2929 /* Initialize a growable string. */
2932 d_growable_string_init (struct d_growable_string
*dgs
, size_t estimate
)
2937 dgs
->allocation_failure
= 0;
2940 d_growable_string_resize (dgs
, estimate
);
2943 /* Grow a growable string to a given size. */
2946 d_growable_string_resize (struct d_growable_string
*dgs
, size_t need
)
2951 if (dgs
->allocation_failure
)
2954 /* Start allocation at two bytes to avoid any possibility of confusion
2955 with the special value of 1 used as a return in *palc to indicate
2956 allocation failures. */
2957 newalc
= dgs
->alc
> 0 ? dgs
->alc
: 2;
2958 while (newalc
< need
)
2961 newbuf
= (char *) realloc (dgs
->buf
, newalc
);
2968 dgs
->allocation_failure
= 1;
2975 /* Append a buffer to a growable string. */
2978 d_growable_string_append_buffer (struct d_growable_string
*dgs
,
2979 const char *s
, size_t l
)
2983 need
= dgs
->len
+ l
+ 1;
2984 if (need
> dgs
->alc
)
2985 d_growable_string_resize (dgs
, need
);
2987 if (dgs
->allocation_failure
)
2990 memcpy (dgs
->buf
+ dgs
->len
, s
, l
);
2991 dgs
->buf
[dgs
->len
+ l
] = '\0';
2995 /* Bridge growable strings to the callback mechanism. */
2998 d_growable_string_callback_adapter (const char *s
, size_t l
, void *opaque
)
3000 struct d_growable_string
*dgs
= (struct d_growable_string
*) opaque
;
3002 d_growable_string_append_buffer (dgs
, s
, l
);
3005 /* Initialize a print information structure. */
3008 d_print_init (struct d_print_info
*dpi
, int options
,
3009 demangle_callbackref callback
, void *opaque
)
3011 dpi
->options
= options
;
3013 dpi
->last_char
= '\0';
3014 dpi
->templates
= NULL
;
3015 dpi
->modifiers
= NULL
;
3017 dpi
->callback
= callback
;
3018 dpi
->opaque
= opaque
;
3020 dpi
->demangle_failure
= 0;
3023 /* Indicate that an error occurred during printing, and test for error. */
3026 d_print_error (struct d_print_info
*dpi
)
3028 dpi
->demangle_failure
= 1;
3032 d_print_saw_error (struct d_print_info
*dpi
)
3034 return dpi
->demangle_failure
!= 0;
3037 /* Flush buffered characters to the callback. */
3040 d_print_flush (struct d_print_info
*dpi
)
3042 dpi
->buf
[dpi
->len
] = '\0';
3043 dpi
->callback (dpi
->buf
, dpi
->len
, dpi
->opaque
);
3047 /* Append characters and buffers for printing. */
3050 d_append_char (struct d_print_info
*dpi
, char c
)
3052 if (dpi
->len
== sizeof (dpi
->buf
) - 1)
3053 d_print_flush (dpi
);
3055 dpi
->buf
[dpi
->len
++] = c
;
3060 d_append_buffer (struct d_print_info
*dpi
, const char *s
, size_t l
)
3064 for (i
= 0; i
< l
; i
++)
3065 d_append_char (dpi
, s
[i
]);
3069 d_append_string (struct d_print_info
*dpi
, const char *s
)
3071 d_append_buffer (dpi
, s
, strlen (s
));
3075 d_last_char (struct d_print_info
*dpi
)
3077 return dpi
->last_char
;
3080 /* Turn components into a human readable string. OPTIONS is the
3081 options bits passed to the demangler. DC is the tree to print.
3082 CALLBACK is a function to call to flush demangled string segments
3083 as they fill the intermediate buffer, and OPAQUE is a generalized
3084 callback argument. On success, this returns 1. On failure,
3085 it returns 0, indicating a bad parse. It does not use heap
3086 memory to build an output string, so cannot encounter memory
3087 allocation failure. */
3089 CP_STATIC_IF_GLIBCPP_V3
3091 cplus_demangle_print_callback (int options
,
3092 const struct demangle_component
*dc
,
3093 demangle_callbackref callback
, void *opaque
)
3095 struct d_print_info dpi
;
3097 d_print_init (&dpi
, options
, callback
, opaque
);
3099 d_print_comp (&dpi
, dc
);
3101 d_print_flush (&dpi
);
3103 return ! d_print_saw_error (&dpi
);
3106 /* Turn components into a human readable string. OPTIONS is the
3107 options bits passed to the demangler. DC is the tree to print.
3108 ESTIMATE is a guess at the length of the result. This returns a
3109 string allocated by malloc, or NULL on error. On success, this
3110 sets *PALC to the size of the allocated buffer. On failure, this
3111 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
3114 CP_STATIC_IF_GLIBCPP_V3
3116 cplus_demangle_print (int options
, const struct demangle_component
*dc
,
3117 int estimate
, size_t *palc
)
3119 struct d_growable_string dgs
;
3121 d_growable_string_init (&dgs
, estimate
);
3123 if (! cplus_demangle_print_callback (options
, dc
,
3124 d_growable_string_callback_adapter
,
3132 *palc
= dgs
.allocation_failure
? 1 : dgs
.alc
;
3136 /* Returns the I'th element of the template arglist ARGS, or NULL on
3139 static struct demangle_component
*
3140 d_index_template_argument (struct demangle_component
*args
, int i
)
3142 struct demangle_component
*a
;
3148 if (a
->type
!= DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
3154 if (i
!= 0 || a
== NULL
)
3160 /* Returns the template argument from the current context indicated by DC,
3161 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
3163 static struct demangle_component
*
3164 d_lookup_template_argument (struct d_print_info
*dpi
,
3165 const struct demangle_component
*dc
)
3167 if (dpi
->templates
== NULL
)
3169 d_print_error (dpi
);
3173 return d_index_template_argument
3174 (d_right (dpi
->templates
->template_decl
),
3175 dc
->u
.s_number
.number
);
3178 /* Returns a template argument pack used in DC (any will do), or NULL. */
3180 static struct demangle_component
*
3181 d_find_pack (struct d_print_info
*dpi
,
3182 const struct demangle_component
*dc
)
3184 struct demangle_component
*a
;
3190 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
3191 a
= d_lookup_template_argument (dpi
, dc
);
3192 if (a
&& a
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
3196 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
3199 case DEMANGLE_COMPONENT_NAME
:
3200 case DEMANGLE_COMPONENT_OPERATOR
:
3201 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
3202 case DEMANGLE_COMPONENT_SUB_STD
:
3203 case DEMANGLE_COMPONENT_CHARACTER
:
3206 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
3207 return d_find_pack (dpi
, dc
->u
.s_extended_operator
.name
);
3208 case DEMANGLE_COMPONENT_CTOR
:
3209 return d_find_pack (dpi
, dc
->u
.s_ctor
.name
);
3210 case DEMANGLE_COMPONENT_DTOR
:
3211 return d_find_pack (dpi
, dc
->u
.s_dtor
.name
);
3214 a
= d_find_pack (dpi
, d_left (dc
));
3217 return d_find_pack (dpi
, d_right (dc
));
3221 /* Returns the length of the template argument pack DC. */
3224 d_pack_length (const struct demangle_component
*dc
)
3227 while (dc
&& dc
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
3228 && d_left (dc
) != NULL
)
3236 /* DC is a component of a mangled expression. Print it, wrapped in parens
3240 d_print_subexpr (struct d_print_info
*dpi
,
3241 const struct demangle_component
*dc
)
3244 if (dc
->type
== DEMANGLE_COMPONENT_NAME
)
3247 d_append_char (dpi
, '(');
3248 d_print_comp (dpi
, dc
);
3250 d_append_char (dpi
, ')');
3253 /* Subroutine to handle components. */
3256 d_print_comp (struct d_print_info
*dpi
,
3257 const struct demangle_component
*dc
)
3261 d_print_error (dpi
);
3264 if (d_print_saw_error (dpi
))
3269 case DEMANGLE_COMPONENT_NAME
:
3270 if ((dpi
->options
& DMGL_JAVA
) == 0)
3271 d_append_buffer (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
3273 d_print_java_identifier (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
3276 case DEMANGLE_COMPONENT_QUAL_NAME
:
3277 case DEMANGLE_COMPONENT_LOCAL_NAME
:
3278 d_print_comp (dpi
, d_left (dc
));
3279 if ((dpi
->options
& DMGL_JAVA
) == 0)
3280 d_append_string (dpi
, "::");
3282 d_append_char (dpi
, '.');
3283 d_print_comp (dpi
, d_right (dc
));
3286 case DEMANGLE_COMPONENT_TYPED_NAME
:
3288 struct d_print_mod
*hold_modifiers
;
3289 struct demangle_component
*typed_name
;
3290 struct d_print_mod adpm
[4];
3292 struct d_print_template dpt
;
3294 /* Pass the name down to the type so that it can be printed in
3295 the right place for the type. We also have to pass down
3296 any CV-qualifiers, which apply to the this parameter. */
3297 hold_modifiers
= dpi
->modifiers
;
3299 typed_name
= d_left (dc
);
3300 while (typed_name
!= NULL
)
3302 if (i
>= sizeof adpm
/ sizeof adpm
[0])
3304 d_print_error (dpi
);
3308 adpm
[i
].next
= dpi
->modifiers
;
3309 dpi
->modifiers
= &adpm
[i
];
3310 adpm
[i
].mod
= typed_name
;
3311 adpm
[i
].printed
= 0;
3312 adpm
[i
].templates
= dpi
->templates
;
3315 if (typed_name
->type
!= DEMANGLE_COMPONENT_RESTRICT_THIS
3316 && typed_name
->type
!= DEMANGLE_COMPONENT_VOLATILE_THIS
3317 && typed_name
->type
!= DEMANGLE_COMPONENT_CONST_THIS
)
3320 typed_name
= d_left (typed_name
);
3323 if (typed_name
== NULL
)
3325 d_print_error (dpi
);
3329 /* If typed_name is a template, then it applies to the
3330 function type as well. */
3331 if (typed_name
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
3333 dpt
.next
= dpi
->templates
;
3334 dpi
->templates
= &dpt
;
3335 dpt
.template_decl
= typed_name
;
3338 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
3339 there may be CV-qualifiers on its right argument which
3340 really apply here; this happens when parsing a class which
3341 is local to a function. */
3342 if (typed_name
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
3344 struct demangle_component
*local_name
;
3346 local_name
= d_right (typed_name
);
3347 while (local_name
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
3348 || local_name
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
3349 || local_name
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
3351 if (i
>= sizeof adpm
/ sizeof adpm
[0])
3353 d_print_error (dpi
);
3357 adpm
[i
] = adpm
[i
- 1];
3358 adpm
[i
].next
= &adpm
[i
- 1];
3359 dpi
->modifiers
= &adpm
[i
];
3361 adpm
[i
- 1].mod
= local_name
;
3362 adpm
[i
- 1].printed
= 0;
3363 adpm
[i
- 1].templates
= dpi
->templates
;
3366 local_name
= d_left (local_name
);
3370 d_print_comp (dpi
, d_right (dc
));
3372 if (typed_name
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
3373 dpi
->templates
= dpt
.next
;
3375 /* If the modifiers didn't get printed by the type, print them
3380 if (! adpm
[i
].printed
)
3382 d_append_char (dpi
, ' ');
3383 d_print_mod (dpi
, adpm
[i
].mod
);
3387 dpi
->modifiers
= hold_modifiers
;
3392 case DEMANGLE_COMPONENT_TEMPLATE
:
3394 struct d_print_mod
*hold_dpm
;
3395 struct demangle_component
*dcl
;
3397 /* Don't push modifiers into a template definition. Doing so
3398 could give the wrong definition for a template argument.
3399 Instead, treat the template essentially as a name. */
3401 hold_dpm
= dpi
->modifiers
;
3402 dpi
->modifiers
= NULL
;
3406 if ((dpi
->options
& DMGL_JAVA
) != 0
3407 && dcl
->type
== DEMANGLE_COMPONENT_NAME
3408 && dcl
->u
.s_name
.len
== 6
3409 && strncmp (dcl
->u
.s_name
.s
, "JArray", 6) == 0)
3411 /* Special-case Java arrays, so that JArray<TYPE> appears
3412 instead as TYPE[]. */
3414 d_print_comp (dpi
, d_right (dc
));
3415 d_append_string (dpi
, "[]");
3419 d_print_comp (dpi
, dcl
);
3420 if (d_last_char (dpi
) == '<')
3421 d_append_char (dpi
, ' ');
3422 d_append_char (dpi
, '<');
3423 d_print_comp (dpi
, d_right (dc
));
3424 /* Avoid generating two consecutive '>' characters, to avoid
3425 the C++ syntactic ambiguity. */
3426 if (d_last_char (dpi
) == '>')
3427 d_append_char (dpi
, ' ');
3428 d_append_char (dpi
, '>');
3431 dpi
->modifiers
= hold_dpm
;
3436 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
3438 struct d_print_template
*hold_dpt
;
3439 struct demangle_component
*a
= d_lookup_template_argument (dpi
, dc
);
3441 if (a
&& a
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
3442 a
= d_index_template_argument (a
, dpi
->pack_index
);
3446 d_print_error (dpi
);
3450 /* While processing this parameter, we need to pop the list of
3451 templates. This is because the template parameter may
3452 itself be a reference to a parameter of an outer
3455 hold_dpt
= dpi
->templates
;
3456 dpi
->templates
= hold_dpt
->next
;
3458 d_print_comp (dpi
, a
);
3460 dpi
->templates
= hold_dpt
;
3465 case DEMANGLE_COMPONENT_CTOR
:
3466 d_print_comp (dpi
, dc
->u
.s_ctor
.name
);
3469 case DEMANGLE_COMPONENT_DTOR
:
3470 d_append_char (dpi
, '~');
3471 d_print_comp (dpi
, dc
->u
.s_dtor
.name
);
3474 case DEMANGLE_COMPONENT_VTABLE
:
3475 d_append_string (dpi
, "vtable for ");
3476 d_print_comp (dpi
, d_left (dc
));
3479 case DEMANGLE_COMPONENT_VTT
:
3480 d_append_string (dpi
, "VTT for ");
3481 d_print_comp (dpi
, d_left (dc
));
3484 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
3485 d_append_string (dpi
, "construction vtable for ");
3486 d_print_comp (dpi
, d_left (dc
));
3487 d_append_string (dpi
, "-in-");
3488 d_print_comp (dpi
, d_right (dc
));
3491 case DEMANGLE_COMPONENT_TYPEINFO
:
3492 d_append_string (dpi
, "typeinfo for ");
3493 d_print_comp (dpi
, d_left (dc
));
3496 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
3497 d_append_string (dpi
, "typeinfo name for ");
3498 d_print_comp (dpi
, d_left (dc
));
3501 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
3502 d_append_string (dpi
, "typeinfo fn for ");
3503 d_print_comp (dpi
, d_left (dc
));
3506 case DEMANGLE_COMPONENT_THUNK
:
3507 d_append_string (dpi
, "non-virtual thunk to ");
3508 d_print_comp (dpi
, d_left (dc
));
3511 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
3512 d_append_string (dpi
, "virtual thunk to ");
3513 d_print_comp (dpi
, d_left (dc
));
3516 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
3517 d_append_string (dpi
, "covariant return thunk to ");
3518 d_print_comp (dpi
, d_left (dc
));
3521 case DEMANGLE_COMPONENT_JAVA_CLASS
:
3522 d_append_string (dpi
, "java Class for ");
3523 d_print_comp (dpi
, d_left (dc
));
3526 case DEMANGLE_COMPONENT_GUARD
:
3527 d_append_string (dpi
, "guard variable for ");
3528 d_print_comp (dpi
, d_left (dc
));
3531 case DEMANGLE_COMPONENT_REFTEMP
:
3532 d_append_string (dpi
, "reference temporary for ");
3533 d_print_comp (dpi
, d_left (dc
));
3536 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
3537 d_append_string (dpi
, "hidden alias for ");
3538 d_print_comp (dpi
, d_left (dc
));
3541 case DEMANGLE_COMPONENT_SUB_STD
:
3542 d_append_buffer (dpi
, dc
->u
.s_string
.string
, dc
->u
.s_string
.len
);
3545 case DEMANGLE_COMPONENT_RESTRICT
:
3546 case DEMANGLE_COMPONENT_VOLATILE
:
3547 case DEMANGLE_COMPONENT_CONST
:
3549 struct d_print_mod
*pdpm
;
3551 /* When printing arrays, it's possible to have cases where the
3552 same CV-qualifier gets pushed on the stack multiple times.
3553 We only need to print it once. */
3555 for (pdpm
= dpi
->modifiers
; pdpm
!= NULL
; pdpm
= pdpm
->next
)
3557 if (! pdpm
->printed
)
3559 if (pdpm
->mod
->type
!= DEMANGLE_COMPONENT_RESTRICT
3560 && pdpm
->mod
->type
!= DEMANGLE_COMPONENT_VOLATILE
3561 && pdpm
->mod
->type
!= DEMANGLE_COMPONENT_CONST
)
3563 if (pdpm
->mod
->type
== dc
->type
)
3565 d_print_comp (dpi
, d_left (dc
));
3572 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
3573 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
3574 case DEMANGLE_COMPONENT_CONST_THIS
:
3575 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
3576 case DEMANGLE_COMPONENT_POINTER
:
3577 case DEMANGLE_COMPONENT_REFERENCE
:
3578 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
3579 case DEMANGLE_COMPONENT_COMPLEX
:
3580 case DEMANGLE_COMPONENT_IMAGINARY
:
3582 /* We keep a list of modifiers on the stack. */
3583 struct d_print_mod dpm
;
3585 dpm
.next
= dpi
->modifiers
;
3586 dpi
->modifiers
= &dpm
;
3589 dpm
.templates
= dpi
->templates
;
3591 d_print_comp (dpi
, d_left (dc
));
3593 /* If the modifier didn't get printed by the type, print it
3596 d_print_mod (dpi
, dc
);
3598 dpi
->modifiers
= dpm
.next
;
3603 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
3604 if ((dpi
->options
& DMGL_JAVA
) == 0)
3605 d_append_buffer (dpi
, dc
->u
.s_builtin
.type
->name
,
3606 dc
->u
.s_builtin
.type
->len
);
3608 d_append_buffer (dpi
, dc
->u
.s_builtin
.type
->java_name
,
3609 dc
->u
.s_builtin
.type
->java_len
);
3612 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
3613 d_print_comp (dpi
, d_left (dc
));
3616 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
3618 if ((dpi
->options
& DMGL_RET_POSTFIX
) != 0)
3619 d_print_function_type (dpi
, dc
, dpi
->modifiers
);
3621 /* Print return type if present */
3622 if (d_left (dc
) != NULL
)
3624 struct d_print_mod dpm
;
3626 /* We must pass this type down as a modifier in order to
3627 print it in the right location. */
3628 dpm
.next
= dpi
->modifiers
;
3629 dpi
->modifiers
= &dpm
;
3632 dpm
.templates
= dpi
->templates
;
3634 d_print_comp (dpi
, d_left (dc
));
3636 dpi
->modifiers
= dpm
.next
;
3641 /* In standard prefix notation, there is a space between the
3642 return type and the function signature. */
3643 if ((dpi
->options
& DMGL_RET_POSTFIX
) == 0)
3644 d_append_char (dpi
, ' ');
3647 if ((dpi
->options
& DMGL_RET_POSTFIX
) == 0)
3648 d_print_function_type (dpi
, dc
, dpi
->modifiers
);
3653 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
3655 struct d_print_mod
*hold_modifiers
;
3656 struct d_print_mod adpm
[4];
3658 struct d_print_mod
*pdpm
;
3660 /* We must pass this type down as a modifier in order to print
3661 multi-dimensional arrays correctly. If the array itself is
3662 CV-qualified, we act as though the element type were
3663 CV-qualified. We do this by copying the modifiers down
3664 rather than fiddling pointers, so that we don't wind up
3665 with a d_print_mod higher on the stack pointing into our
3666 stack frame after we return. */
3668 hold_modifiers
= dpi
->modifiers
;
3670 adpm
[0].next
= hold_modifiers
;
3671 dpi
->modifiers
= &adpm
[0];
3673 adpm
[0].printed
= 0;
3674 adpm
[0].templates
= dpi
->templates
;
3677 pdpm
= hold_modifiers
;
3679 && (pdpm
->mod
->type
== DEMANGLE_COMPONENT_RESTRICT
3680 || pdpm
->mod
->type
== DEMANGLE_COMPONENT_VOLATILE
3681 || pdpm
->mod
->type
== DEMANGLE_COMPONENT_CONST
))
3683 if (! pdpm
->printed
)
3685 if (i
>= sizeof adpm
/ sizeof adpm
[0])
3687 d_print_error (dpi
);
3692 adpm
[i
].next
= dpi
->modifiers
;
3693 dpi
->modifiers
= &adpm
[i
];
3701 d_print_comp (dpi
, d_right (dc
));
3703 dpi
->modifiers
= hold_modifiers
;
3705 if (adpm
[0].printed
)
3711 d_print_mod (dpi
, adpm
[i
].mod
);
3714 d_print_array_type (dpi
, dc
, dpi
->modifiers
);
3719 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
3721 struct d_print_mod dpm
;
3723 dpm
.next
= dpi
->modifiers
;
3724 dpi
->modifiers
= &dpm
;
3727 dpm
.templates
= dpi
->templates
;
3729 d_print_comp (dpi
, d_right (dc
));
3731 /* If the modifier didn't get printed by the type, print it
3735 d_append_char (dpi
, ' ');
3736 d_print_comp (dpi
, d_left (dc
));
3737 d_append_string (dpi
, "::*");
3740 dpi
->modifiers
= dpm
.next
;
3745 case DEMANGLE_COMPONENT_FIXED_TYPE
:
3746 if (dc
->u
.s_fixed
.sat
)
3747 d_append_string (dpi
, "_Sat ");
3748 /* Don't print "int _Accum". */
3749 if (dc
->u
.s_fixed
.length
->u
.s_builtin
.type
3750 != &cplus_demangle_builtin_types
['i'-'a'])
3752 d_print_comp (dpi
, dc
->u
.s_fixed
.length
);
3753 d_append_char (dpi
, ' ');
3755 if (dc
->u
.s_fixed
.accum
)
3756 d_append_string (dpi
, "_Accum");
3758 d_append_string (dpi
, "_Fract");
3761 case DEMANGLE_COMPONENT_ARGLIST
:
3762 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
3763 if (d_left (dc
) != NULL
)
3764 d_print_comp (dpi
, d_left (dc
));
3765 if (d_right (dc
) != NULL
)
3768 d_append_string (dpi
, ", ");
3770 d_print_comp (dpi
, d_right (dc
));
3771 /* If that didn't print anything (which can happen with empty
3772 template argument packs), remove the comma and space. */
3773 if (dpi
->len
== len
)
3778 case DEMANGLE_COMPONENT_OPERATOR
:
3782 d_append_string (dpi
, "operator");
3783 c
= dc
->u
.s_operator
.op
->name
[0];
3785 d_append_char (dpi
, ' ');
3786 d_append_buffer (dpi
, dc
->u
.s_operator
.op
->name
,
3787 dc
->u
.s_operator
.op
->len
);
3791 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
3792 d_append_string (dpi
, "operator ");
3793 d_print_comp (dpi
, dc
->u
.s_extended_operator
.name
);
3796 case DEMANGLE_COMPONENT_CAST
:
3797 d_append_string (dpi
, "operator ");
3798 d_print_cast (dpi
, dc
);
3801 case DEMANGLE_COMPONENT_UNARY
:
3802 if (d_left (dc
)->type
!= DEMANGLE_COMPONENT_CAST
)
3803 d_print_expr_op (dpi
, d_left (dc
));
3806 d_append_char (dpi
, '(');
3807 d_print_cast (dpi
, d_left (dc
));
3808 d_append_char (dpi
, ')');
3810 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_CAST
3811 && d_right (dc
)->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
)
3812 /* type() -- FIXME what about type(multiple,args) */
3813 d_append_string (dpi
, "()");
3815 d_print_subexpr (dpi
, d_right (dc
));
3818 case DEMANGLE_COMPONENT_BINARY
:
3819 if (d_right (dc
)->type
!= DEMANGLE_COMPONENT_BINARY_ARGS
)
3821 d_print_error (dpi
);
3825 /* We wrap an expression which uses the greater-than operator in
3826 an extra layer of parens so that it does not get confused
3827 with the '>' which ends the template parameters. */
3828 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_OPERATOR
3829 && d_left (dc
)->u
.s_operator
.op
->len
== 1
3830 && d_left (dc
)->u
.s_operator
.op
->name
[0] == '>')
3831 d_append_char (dpi
, '(');
3833 d_print_subexpr (dpi
, d_left (d_right (dc
)));
3834 if (strcmp (d_left (dc
)->u
.s_operator
.op
->code
, "cl") != 0)
3835 d_print_expr_op (dpi
, d_left (dc
));
3836 d_print_subexpr (dpi
, d_right (d_right (dc
)));
3838 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_OPERATOR
3839 && d_left (dc
)->u
.s_operator
.op
->len
== 1
3840 && d_left (dc
)->u
.s_operator
.op
->name
[0] == '>')
3841 d_append_char (dpi
, ')');
3845 case DEMANGLE_COMPONENT_BINARY_ARGS
:
3846 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
3847 d_print_error (dpi
);
3850 case DEMANGLE_COMPONENT_TRINARY
:
3851 if (d_right (dc
)->type
!= DEMANGLE_COMPONENT_TRINARY_ARG1
3852 || d_right (d_right (dc
))->type
!= DEMANGLE_COMPONENT_TRINARY_ARG2
)
3854 d_print_error (dpi
);
3857 d_print_subexpr (dpi
, d_left (d_right (dc
)));
3858 d_print_expr_op (dpi
, d_left (dc
));
3859 d_print_subexpr (dpi
, d_left (d_right (d_right (dc
))));
3860 d_append_string (dpi
, " : ");
3861 d_print_subexpr (dpi
, d_right (d_right (d_right (dc
))));
3864 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
3865 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
3866 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
3867 d_print_error (dpi
);
3870 case DEMANGLE_COMPONENT_LITERAL
:
3871 case DEMANGLE_COMPONENT_LITERAL_NEG
:
3873 enum d_builtin_type_print tp
;
3875 /* For some builtin types, produce simpler output. */
3876 tp
= D_PRINT_DEFAULT
;
3877 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
)
3879 tp
= d_left (dc
)->u
.s_builtin
.type
->print
;
3883 case D_PRINT_UNSIGNED
:
3885 case D_PRINT_UNSIGNED_LONG
:
3886 case D_PRINT_LONG_LONG
:
3887 case D_PRINT_UNSIGNED_LONG_LONG
:
3888 if (d_right (dc
)->type
== DEMANGLE_COMPONENT_NAME
)
3890 if (dc
->type
== DEMANGLE_COMPONENT_LITERAL_NEG
)
3891 d_append_char (dpi
, '-');
3892 d_print_comp (dpi
, d_right (dc
));
3897 case D_PRINT_UNSIGNED
:
3898 d_append_char (dpi
, 'u');
3901 d_append_char (dpi
, 'l');
3903 case D_PRINT_UNSIGNED_LONG
:
3904 d_append_string (dpi
, "ul");
3906 case D_PRINT_LONG_LONG
:
3907 d_append_string (dpi
, "ll");
3909 case D_PRINT_UNSIGNED_LONG_LONG
:
3910 d_append_string (dpi
, "ull");
3918 if (d_right (dc
)->type
== DEMANGLE_COMPONENT_NAME
3919 && d_right (dc
)->u
.s_name
.len
== 1
3920 && dc
->type
== DEMANGLE_COMPONENT_LITERAL
)
3922 switch (d_right (dc
)->u
.s_name
.s
[0])
3925 d_append_string (dpi
, "false");
3928 d_append_string (dpi
, "true");
3941 d_append_char (dpi
, '(');
3942 d_print_comp (dpi
, d_left (dc
));
3943 d_append_char (dpi
, ')');
3944 if (dc
->type
== DEMANGLE_COMPONENT_LITERAL_NEG
)
3945 d_append_char (dpi
, '-');
3946 if (tp
== D_PRINT_FLOAT
)
3947 d_append_char (dpi
, '[');
3948 d_print_comp (dpi
, d_right (dc
));
3949 if (tp
== D_PRINT_FLOAT
)
3950 d_append_char (dpi
, ']');
3954 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
3955 d_append_string (dpi
, "java resource ");
3956 d_print_comp (dpi
, d_left (dc
));
3959 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
3960 d_print_comp (dpi
, d_left (dc
));
3961 d_print_comp (dpi
, d_right (dc
));
3964 case DEMANGLE_COMPONENT_CHARACTER
:
3965 d_append_char (dpi
, dc
->u
.s_character
.character
);
3968 case DEMANGLE_COMPONENT_DECLTYPE
:
3969 d_append_string (dpi
, "decltype (");
3970 d_print_comp (dpi
, d_left (dc
));
3971 d_append_char (dpi
, ')');
3974 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
3976 struct demangle_component
*a
= d_find_pack (dpi
, d_left (dc
));
3977 int len
= d_pack_length (a
);
3981 for (i
= 0; i
< len
; ++i
)
3983 dpi
->pack_index
= i
;
3984 d_print_comp (dpi
, dc
);
3986 d_append_string (dpi
, ", ");
3992 d_print_error (dpi
);
3997 /* Print a Java dentifier. For Java we try to handle encoded extended
3998 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
3999 so we don't it for C++. Characters are encoded as
4003 d_print_java_identifier (struct d_print_info
*dpi
, const char *name
, int len
)
4009 for (p
= name
; p
< end
; ++p
)
4020 for (q
= p
+ 3; q
< end
; ++q
)
4026 else if (*q
>= 'A' && *q
<= 'F')
4027 dig
= *q
- 'A' + 10;
4028 else if (*q
>= 'a' && *q
<= 'f')
4029 dig
= *q
- 'a' + 10;
4035 /* If the Unicode character is larger than 256, we don't try
4036 to deal with it here. FIXME. */
4037 if (q
< end
&& *q
== '_' && c
< 256)
4039 d_append_char (dpi
, c
);
4045 d_append_char (dpi
, *p
);
4049 /* Print a list of modifiers. SUFFIX is 1 if we are printing
4050 qualifiers on this after printing a function. */
4053 d_print_mod_list (struct d_print_info
*dpi
,
4054 struct d_print_mod
*mods
, int suffix
)
4056 struct d_print_template
*hold_dpt
;
4058 if (mods
== NULL
|| d_print_saw_error (dpi
))
4063 && (mods
->mod
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
4064 || mods
->mod
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
4065 || mods
->mod
->type
== DEMANGLE_COMPONENT_CONST_THIS
)))
4067 d_print_mod_list (dpi
, mods
->next
, suffix
);
4073 hold_dpt
= dpi
->templates
;
4074 dpi
->templates
= mods
->templates
;
4076 if (mods
->mod
->type
== DEMANGLE_COMPONENT_FUNCTION_TYPE
)
4078 d_print_function_type (dpi
, mods
->mod
, mods
->next
);
4079 dpi
->templates
= hold_dpt
;
4082 else if (mods
->mod
->type
== DEMANGLE_COMPONENT_ARRAY_TYPE
)
4084 d_print_array_type (dpi
, mods
->mod
, mods
->next
);
4085 dpi
->templates
= hold_dpt
;
4088 else if (mods
->mod
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
4090 struct d_print_mod
*hold_modifiers
;
4091 struct demangle_component
*dc
;
4093 /* When this is on the modifier stack, we have pulled any
4094 qualifiers off the right argument already. Otherwise, we
4095 print it as usual, but don't let the left argument see any
4098 hold_modifiers
= dpi
->modifiers
;
4099 dpi
->modifiers
= NULL
;
4100 d_print_comp (dpi
, d_left (mods
->mod
));
4101 dpi
->modifiers
= hold_modifiers
;
4103 if ((dpi
->options
& DMGL_JAVA
) == 0)
4104 d_append_string (dpi
, "::");
4106 d_append_char (dpi
, '.');
4108 dc
= d_right (mods
->mod
);
4109 while (dc
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
4110 || dc
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
4111 || dc
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
4114 d_print_comp (dpi
, dc
);
4116 dpi
->templates
= hold_dpt
;
4120 d_print_mod (dpi
, mods
->mod
);
4122 dpi
->templates
= hold_dpt
;
4124 d_print_mod_list (dpi
, mods
->next
, suffix
);
4127 /* Print a modifier. */
4130 d_print_mod (struct d_print_info
*dpi
,
4131 const struct demangle_component
*mod
)
4135 case DEMANGLE_COMPONENT_RESTRICT
:
4136 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
4137 d_append_string (dpi
, " restrict");
4139 case DEMANGLE_COMPONENT_VOLATILE
:
4140 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
4141 d_append_string (dpi
, " volatile");
4143 case DEMANGLE_COMPONENT_CONST
:
4144 case DEMANGLE_COMPONENT_CONST_THIS
:
4145 d_append_string (dpi
, " const");
4147 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
4148 d_append_char (dpi
, ' ');
4149 d_print_comp (dpi
, d_right (mod
));
4151 case DEMANGLE_COMPONENT_POINTER
:
4152 /* There is no pointer symbol in Java. */
4153 if ((dpi
->options
& DMGL_JAVA
) == 0)
4154 d_append_char (dpi
, '*');
4156 case DEMANGLE_COMPONENT_REFERENCE
:
4157 d_append_char (dpi
, '&');
4159 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
4160 d_append_string (dpi
, "&&");
4162 case DEMANGLE_COMPONENT_COMPLEX
:
4163 d_append_string (dpi
, "complex ");
4165 case DEMANGLE_COMPONENT_IMAGINARY
:
4166 d_append_string (dpi
, "imaginary ");
4168 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
4169 if (d_last_char (dpi
) != '(')
4170 d_append_char (dpi
, ' ');
4171 d_print_comp (dpi
, d_left (mod
));
4172 d_append_string (dpi
, "::*");
4174 case DEMANGLE_COMPONENT_TYPED_NAME
:
4175 d_print_comp (dpi
, d_left (mod
));
4178 /* Otherwise, we have something that won't go back on the
4179 modifier stack, so we can just print it. */
4180 d_print_comp (dpi
, mod
);
4185 /* Print a function type, except for the return type. */
4188 d_print_function_type (struct d_print_info
*dpi
,
4189 const struct demangle_component
*dc
,
4190 struct d_print_mod
*mods
)
4195 struct d_print_mod
*p
;
4196 struct d_print_mod
*hold_modifiers
;
4201 for (p
= mods
; p
!= NULL
; p
= p
->next
)
4207 switch (p
->mod
->type
)
4209 case DEMANGLE_COMPONENT_POINTER
:
4210 case DEMANGLE_COMPONENT_REFERENCE
:
4211 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
4214 case DEMANGLE_COMPONENT_RESTRICT
:
4215 case DEMANGLE_COMPONENT_VOLATILE
:
4216 case DEMANGLE_COMPONENT_CONST
:
4217 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
4218 case DEMANGLE_COMPONENT_COMPLEX
:
4219 case DEMANGLE_COMPONENT_IMAGINARY
:
4220 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
4224 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
4225 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
4226 case DEMANGLE_COMPONENT_CONST_THIS
:
4235 if (d_left (dc
) != NULL
&& ! saw_mod
)
4242 if (d_last_char (dpi
) != '('
4243 && d_last_char (dpi
) != '*')
4246 if (need_space
&& d_last_char (dpi
) != ' ')
4247 d_append_char (dpi
, ' ');
4248 d_append_char (dpi
, '(');
4251 hold_modifiers
= dpi
->modifiers
;
4252 dpi
->modifiers
= NULL
;
4254 d_print_mod_list (dpi
, mods
, 0);
4257 d_append_char (dpi
, ')');
4259 d_append_char (dpi
, '(');
4261 if (d_right (dc
) != NULL
)
4262 d_print_comp (dpi
, d_right (dc
));
4264 d_append_char (dpi
, ')');
4266 d_print_mod_list (dpi
, mods
, 1);
4268 dpi
->modifiers
= hold_modifiers
;
4271 /* Print an array type, except for the element type. */
4274 d_print_array_type (struct d_print_info
*dpi
,
4275 const struct demangle_component
*dc
,
4276 struct d_print_mod
*mods
)
4284 struct d_print_mod
*p
;
4287 for (p
= mods
; p
!= NULL
; p
= p
->next
)
4291 if (p
->mod
->type
== DEMANGLE_COMPONENT_ARRAY_TYPE
)
4306 d_append_string (dpi
, " (");
4308 d_print_mod_list (dpi
, mods
, 0);
4311 d_append_char (dpi
, ')');
4315 d_append_char (dpi
, ' ');
4317 d_append_char (dpi
, '[');
4319 if (d_left (dc
) != NULL
)
4320 d_print_comp (dpi
, d_left (dc
));
4322 d_append_char (dpi
, ']');
4325 /* Print an operator in an expression. */
4328 d_print_expr_op (struct d_print_info
*dpi
,
4329 const struct demangle_component
*dc
)
4331 if (dc
->type
== DEMANGLE_COMPONENT_OPERATOR
)
4332 d_append_buffer (dpi
, dc
->u
.s_operator
.op
->name
,
4333 dc
->u
.s_operator
.op
->len
);
4335 d_print_comp (dpi
, dc
);
4341 d_print_cast (struct d_print_info
*dpi
,
4342 const struct demangle_component
*dc
)
4344 if (d_left (dc
)->type
!= DEMANGLE_COMPONENT_TEMPLATE
)
4345 d_print_comp (dpi
, d_left (dc
));
4348 struct d_print_mod
*hold_dpm
;
4349 struct d_print_template dpt
;
4351 /* It appears that for a templated cast operator, we need to put
4352 the template parameters in scope for the operator name, but
4353 not for the parameters. The effect is that we need to handle
4354 the template printing here. */
4356 hold_dpm
= dpi
->modifiers
;
4357 dpi
->modifiers
= NULL
;
4359 dpt
.next
= dpi
->templates
;
4360 dpi
->templates
= &dpt
;
4361 dpt
.template_decl
= d_left (dc
);
4363 d_print_comp (dpi
, d_left (d_left (dc
)));
4365 dpi
->templates
= dpt
.next
;
4367 if (d_last_char (dpi
) == '<')
4368 d_append_char (dpi
, ' ');
4369 d_append_char (dpi
, '<');
4370 d_print_comp (dpi
, d_right (d_left (dc
)));
4371 /* Avoid generating two consecutive '>' characters, to avoid
4372 the C++ syntactic ambiguity. */
4373 if (d_last_char (dpi
) == '>')
4374 d_append_char (dpi
, ' ');
4375 d_append_char (dpi
, '>');
4377 dpi
->modifiers
= hold_dpm
;
4381 /* Initialize the information structure we use to pass around
4384 CP_STATIC_IF_GLIBCPP_V3
4386 cplus_demangle_init_info (const char *mangled
, int options
, size_t len
,
4390 di
->send
= mangled
+ len
;
4391 di
->options
= options
;
4395 /* We can not need more components than twice the number of chars in
4396 the mangled string. Most components correspond directly to
4397 chars, but the ARGLIST types are exceptions. */
4398 di
->num_comps
= 2 * len
;
4401 /* Similarly, we can not need more substitutions than there are
4402 chars in the mangled string. */
4407 di
->last_name
= NULL
;
4412 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
4413 mangled name, return strings in repeated callback giving the demangled
4414 name. OPTIONS is the usual libiberty demangler options. On success,
4415 this returns 1. On failure, returns 0. */
4418 d_demangle_callback (const char *mangled
, int options
,
4419 demangle_callbackref callback
, void *opaque
)
4423 struct demangle_component
*dc
;
4426 if (mangled
[0] == '_' && mangled
[1] == 'Z')
4428 else if (strncmp (mangled
, "_GLOBAL_", 8) == 0
4429 && (mangled
[8] == '.' || mangled
[8] == '_' || mangled
[8] == '$')
4430 && (mangled
[9] == 'D' || mangled
[9] == 'I')
4431 && mangled
[10] == '_')
4435 intro
= (mangled
[9] == 'I')
4436 ? "global constructors keyed to "
4437 : "global destructors keyed to ";
4439 callback (intro
, strlen (intro
), opaque
);
4440 callback (mangled
+ 11, strlen (mangled
+ 11), opaque
);
4445 if ((options
& DMGL_TYPES
) == 0)
4450 cplus_demangle_init_info (mangled
, options
, strlen (mangled
), &di
);
4453 #ifdef CP_DYNAMIC_ARRAYS
4454 __extension__
struct demangle_component comps
[di
.num_comps
];
4455 __extension__
struct demangle_component
*subs
[di
.num_subs
];
4460 di
.comps
= alloca (di
.num_comps
* sizeof (*di
.comps
));
4461 di
.subs
= alloca (di
.num_subs
* sizeof (*di
.subs
));
4465 dc
= cplus_demangle_type (&di
);
4467 dc
= cplus_demangle_mangled_name (&di
, 1);
4469 /* If DMGL_PARAMS is set, then if we didn't consume the entire
4470 mangled string, then we didn't successfully demangle it. If
4471 DMGL_PARAMS is not set, we didn't look at the trailing
4473 if (((options
& DMGL_PARAMS
) != 0) && d_peek_char (&di
) != '\0')
4476 #ifdef CP_DEMANGLE_DEBUG
4480 status
= (dc
!= NULL
)
4481 ? cplus_demangle_print_callback (options
, dc
, callback
, opaque
)
4488 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
4489 name, return a buffer allocated with malloc holding the demangled
4490 name. OPTIONS is the usual libiberty demangler options. On
4491 success, this sets *PALC to the allocated size of the returned
4492 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
4493 a memory allocation failure, and returns NULL. */
4496 d_demangle (const char *mangled
, int options
, size_t *palc
)
4498 struct d_growable_string dgs
;
4501 d_growable_string_init (&dgs
, 0);
4503 status
= d_demangle_callback (mangled
, options
,
4504 d_growable_string_callback_adapter
, &dgs
);
4512 *palc
= dgs
.allocation_failure
? 1 : 0;
4516 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
4518 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
4520 /* ia64 ABI-mandated entry point in the C++ runtime library for
4521 performing demangling. MANGLED_NAME is a NUL-terminated character
4522 string containing the name to be demangled.
4524 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
4525 *LENGTH bytes, into which the demangled name is stored. If
4526 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
4527 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
4528 is placed in a region of memory allocated with malloc.
4530 If LENGTH is non-NULL, the length of the buffer containing the
4531 demangled name, is placed in *LENGTH.
4533 The return value is a pointer to the start of the NUL-terminated
4534 demangled name, or NULL if the demangling fails. The caller is
4535 responsible for deallocating this memory using free.
4537 *STATUS is set to one of the following values:
4538 0: The demangling operation succeeded.
4539 -1: A memory allocation failure occurred.
4540 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
4541 -3: One of the arguments is invalid.
4543 The demangling is performed using the C++ ABI mangling rules, with
4547 __cxa_demangle (const char *mangled_name
, char *output_buffer
,
4548 size_t *length
, int *status
)
4553 if (mangled_name
== NULL
)
4560 if (output_buffer
!= NULL
&& length
== NULL
)
4567 demangled
= d_demangle (mangled_name
, DMGL_PARAMS
| DMGL_TYPES
, &alc
);
4569 if (demangled
== NULL
)
4581 if (output_buffer
== NULL
)
4588 if (strlen (demangled
) < *length
)
4590 strcpy (output_buffer
, demangled
);
4592 demangled
= output_buffer
;
4596 free (output_buffer
);
4607 extern int __gcclibcxx_demangle_callback (const char *,
4609 (const char *, size_t, void *),
4612 /* Alternative, allocationless entry point in the C++ runtime library
4613 for performing demangling. MANGLED_NAME is a NUL-terminated character
4614 string containing the name to be demangled.
4616 CALLBACK is a callback function, called with demangled string
4617 segments as demangling progresses; it is called at least once,
4618 but may be called more than once. OPAQUE is a generalized pointer
4619 used as a callback argument.
4621 The return code is one of the following values, equivalent to
4622 the STATUS values of __cxa_demangle() (excluding -1, since this
4623 function performs no memory allocations):
4624 0: The demangling operation succeeded.
4625 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
4626 -3: One of the arguments is invalid.
4628 The demangling is performed using the C++ ABI mangling rules, with
4632 __gcclibcxx_demangle_callback (const char *mangled_name
,
4633 void (*callback
) (const char *, size_t, void *),
4638 if (mangled_name
== NULL
|| callback
== NULL
)
4641 status
= d_demangle_callback (mangled_name
, DMGL_PARAMS
| DMGL_TYPES
,
4649 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
4651 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
4652 mangled name, return a buffer allocated with malloc holding the
4653 demangled name. Otherwise, return NULL. */
4656 cplus_demangle_v3 (const char *mangled
, int options
)
4660 return d_demangle (mangled
, options
, &alc
);
4664 cplus_demangle_v3_callback (const char *mangled
, int options
,
4665 demangle_callbackref callback
, void *opaque
)
4667 return d_demangle_callback (mangled
, options
, callback
, opaque
);
4670 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
4671 conventions, but the output formatting is a little different.
4672 This instructs the C++ demangler not to emit pointer characters ("*"), to
4673 use Java's namespace separator symbol ("." instead of "::"), and to output
4674 JArray<TYPE> as TYPE[]. */
4677 java_demangle_v3 (const char *mangled
)
4681 return d_demangle (mangled
, DMGL_JAVA
| DMGL_PARAMS
| DMGL_RET_POSTFIX
, &alc
);
4685 java_demangle_v3_callback (const char *mangled
,
4686 demangle_callbackref callback
, void *opaque
)
4688 return d_demangle_callback (mangled
,
4689 DMGL_JAVA
| DMGL_PARAMS
| DMGL_RET_POSTFIX
,
4693 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
4695 #ifndef IN_GLIBCPP_V3
4697 /* Demangle a string in order to find out whether it is a constructor
4698 or destructor. Return non-zero on success. Set *CTOR_KIND and
4699 *DTOR_KIND appropriately. */
4702 is_ctor_or_dtor (const char *mangled
,
4703 enum gnu_v3_ctor_kinds
*ctor_kind
,
4704 enum gnu_v3_dtor_kinds
*dtor_kind
)
4707 struct demangle_component
*dc
;
4710 *ctor_kind
= (enum gnu_v3_ctor_kinds
) 0;
4711 *dtor_kind
= (enum gnu_v3_dtor_kinds
) 0;
4713 cplus_demangle_init_info (mangled
, DMGL_GNU_V3
, strlen (mangled
), &di
);
4716 #ifdef CP_DYNAMIC_ARRAYS
4717 __extension__
struct demangle_component comps
[di
.num_comps
];
4718 __extension__
struct demangle_component
*subs
[di
.num_subs
];
4723 di
.comps
= alloca (di
.num_comps
* sizeof (*di
.comps
));
4724 di
.subs
= alloca (di
.num_subs
* sizeof (*di
.subs
));
4727 dc
= cplus_demangle_mangled_name (&di
, 1);
4729 /* Note that because we did not pass DMGL_PARAMS, we don't expect
4730 to demangle the entire string. */
4740 case DEMANGLE_COMPONENT_TYPED_NAME
:
4741 case DEMANGLE_COMPONENT_TEMPLATE
:
4742 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
4743 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
4744 case DEMANGLE_COMPONENT_CONST_THIS
:
4747 case DEMANGLE_COMPONENT_QUAL_NAME
:
4748 case DEMANGLE_COMPONENT_LOCAL_NAME
:
4751 case DEMANGLE_COMPONENT_CTOR
:
4752 *ctor_kind
= dc
->u
.s_ctor
.kind
;
4756 case DEMANGLE_COMPONENT_DTOR
:
4757 *dtor_kind
= dc
->u
.s_dtor
.kind
;
4768 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
4769 name. A non-zero return indicates the type of constructor. */
4771 enum gnu_v3_ctor_kinds
4772 is_gnu_v3_mangled_ctor (const char *name
)
4774 enum gnu_v3_ctor_kinds ctor_kind
;
4775 enum gnu_v3_dtor_kinds dtor_kind
;
4777 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
4778 return (enum gnu_v3_ctor_kinds
) 0;
4783 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
4784 name. A non-zero return indicates the type of destructor. */
4786 enum gnu_v3_dtor_kinds
4787 is_gnu_v3_mangled_dtor (const char *name
)
4789 enum gnu_v3_ctor_kinds ctor_kind
;
4790 enum gnu_v3_dtor_kinds dtor_kind
;
4792 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
4793 return (enum gnu_v3_dtor_kinds
) 0;
4797 #endif /* IN_GLIBCPP_V3 */
4799 #ifdef STANDALONE_DEMANGLER
4802 #include "dyn-string.h"
4804 static void print_usage (FILE* fp
, int exit_value
);
4806 #define IS_ALPHA(CHAR) \
4807 (((CHAR) >= 'a' && (CHAR) <= 'z') \
4808 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
4810 /* Non-zero if CHAR is a character than can occur in a mangled name. */
4811 #define is_mangled_char(CHAR) \
4812 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
4813 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
4815 /* The name of this program, as invoked. */
4816 const char* program_name
;
4818 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
4821 print_usage (FILE* fp
, int exit_value
)
4823 fprintf (fp
, "Usage: %s [options] [names ...]\n", program_name
);
4824 fprintf (fp
, "Options:\n");
4825 fprintf (fp
, " -h,--help Display this message.\n");
4826 fprintf (fp
, " -p,--no-params Don't display function parameters\n");
4827 fprintf (fp
, " -v,--verbose Produce verbose demanglings.\n");
4828 fprintf (fp
, "If names are provided, they are demangled. Otherwise filters standard input.\n");
4833 /* Option specification for getopt_long. */
4834 static const struct option long_options
[] =
4836 { "help", no_argument
, NULL
, 'h' },
4837 { "no-params", no_argument
, NULL
, 'p' },
4838 { "verbose", no_argument
, NULL
, 'v' },
4839 { NULL
, no_argument
, NULL
, 0 },
4842 /* Main entry for a demangling filter executable. It will demangle
4843 its command line arguments, if any. If none are provided, it will
4844 filter stdin to stdout, replacing any recognized mangled C++ names
4845 with their demangled equivalents. */
4848 main (int argc
, char *argv
[])
4852 int options
= DMGL_PARAMS
| DMGL_ANSI
| DMGL_TYPES
;
4854 /* Use the program name of this program, as invoked. */
4855 program_name
= argv
[0];
4857 /* Parse options. */
4860 opt_char
= getopt_long (argc
, argv
, "hpv", long_options
, NULL
);
4863 case '?': /* Unrecognized option. */
4864 print_usage (stderr
, 1);
4868 print_usage (stdout
, 0);
4872 options
&= ~ DMGL_PARAMS
;
4876 options
|= DMGL_VERBOSE
;
4880 while (opt_char
!= -1);
4883 /* No command line arguments were provided. Filter stdin. */
4885 dyn_string_t mangled
= dyn_string_new (3);
4888 /* Read all of input. */
4889 while (!feof (stdin
))
4893 /* Pile characters into mangled until we hit one that can't
4894 occur in a mangled name. */
4896 while (!feof (stdin
) && is_mangled_char (c
))
4898 dyn_string_append_char (mangled
, c
);
4904 if (dyn_string_length (mangled
) > 0)
4906 #ifdef IN_GLIBCPP_V3
4907 s
= __cxa_demangle (dyn_string_buf (mangled
), NULL
, NULL
, NULL
);
4909 s
= cplus_demangle_v3 (dyn_string_buf (mangled
), options
);
4919 /* It might not have been a mangled name. Print the
4921 fputs (dyn_string_buf (mangled
), stdout
);
4924 dyn_string_clear (mangled
);
4927 /* If we haven't hit EOF yet, we've read one character that
4928 can't occur in a mangled name, so print it out. */
4933 dyn_string_delete (mangled
);
4936 /* Demangle command line arguments. */
4938 /* Loop over command line arguments. */
4939 for (i
= optind
; i
< argc
; ++i
)
4942 #ifdef IN_GLIBCPP_V3
4946 /* Attempt to demangle. */
4947 #ifdef IN_GLIBCPP_V3
4948 s
= __cxa_demangle (argv
[i
], NULL
, NULL
, &status
);
4950 s
= cplus_demangle_v3 (argv
[i
], options
);
4953 /* If it worked, print the demangled name. */
4961 #ifdef IN_GLIBCPP_V3
4962 fprintf (stderr
, "Failed: %s (status %d)\n", argv
[i
], status
);
4964 fprintf (stderr
, "Failed: %s\n", argv
[i
]);
4973 #endif /* STANDALONE_DEMANGLER */