1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
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 /* Fixed-length allocated buffer for demangled data, flushed to the
282 callback with a NUL termination once full. */
283 char buf
[D_PRINT_BUFFER_LENGTH
];
284 /* Current length of data in buffer. */
286 /* The last character printed, saved individually so that it survives
289 /* Callback function to handle demangled buffer flush. */
290 demangle_callbackref callback
;
291 /* Opaque callback argument. */
293 /* The current list of templates, if any. */
294 struct d_print_template
*templates
;
295 /* The current list of modifiers (e.g., pointer, reference, etc.),
297 struct d_print_mod
*modifiers
;
298 /* Set to 1 if we saw a demangling error. */
299 int demangle_failure
;
300 /* The current index into any template argument packs we are using
303 /* Number of d_print_flush calls so far. */
304 unsigned long int flush_count
;
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_demangle_mangled_name (struct d_info
*, const char *);
325 static struct demangle_component
*
326 d_make_builtin_type (struct d_info
*,
327 const struct demangle_builtin_type_info
*);
329 static struct demangle_component
*
330 d_make_operator (struct d_info
*,
331 const struct demangle_operator_info
*);
333 static struct demangle_component
*
334 d_make_extended_operator (struct d_info
*, int,
335 struct demangle_component
*);
337 static struct demangle_component
*
338 d_make_ctor (struct d_info
*, enum gnu_v3_ctor_kinds
,
339 struct demangle_component
*);
341 static struct demangle_component
*
342 d_make_dtor (struct d_info
*, enum gnu_v3_dtor_kinds
,
343 struct demangle_component
*);
345 static struct demangle_component
*
346 d_make_template_param (struct d_info
*, long);
348 static struct demangle_component
*
349 d_make_sub (struct d_info
*, const char *, int);
352 has_return_type (struct demangle_component
*);
355 is_ctor_dtor_or_conversion (struct demangle_component
*);
357 static struct demangle_component
*d_encoding (struct d_info
*, int);
359 static struct demangle_component
*d_name (struct d_info
*);
361 static struct demangle_component
*d_nested_name (struct d_info
*);
363 static struct demangle_component
*d_prefix (struct d_info
*);
365 static struct demangle_component
*d_unqualified_name (struct d_info
*);
367 static struct demangle_component
*d_source_name (struct d_info
*);
369 static long d_number (struct d_info
*);
371 static struct demangle_component
*d_identifier (struct d_info
*, int);
373 static struct demangle_component
*d_operator_name (struct d_info
*);
375 static struct demangle_component
*d_special_name (struct d_info
*);
377 static int d_call_offset (struct d_info
*, int);
379 static struct demangle_component
*d_ctor_dtor_name (struct d_info
*);
381 static struct demangle_component
**
382 d_cv_qualifiers (struct d_info
*, struct demangle_component
**, int);
384 static struct demangle_component
*
385 d_function_type (struct d_info
*);
387 static struct demangle_component
*
388 d_bare_function_type (struct d_info
*, int);
390 static struct demangle_component
*
391 d_class_enum_type (struct d_info
*);
393 static struct demangle_component
*d_array_type (struct d_info
*);
395 static struct demangle_component
*d_vector_type (struct d_info
*);
397 static struct demangle_component
*
398 d_pointer_to_member_type (struct d_info
*);
400 static struct demangle_component
*
401 d_template_param (struct d_info
*);
403 static struct demangle_component
*d_template_args (struct d_info
*);
405 static struct demangle_component
*
406 d_template_arg (struct d_info
*);
408 static struct demangle_component
*d_expression (struct d_info
*);
410 static struct demangle_component
*d_expr_primary (struct d_info
*);
412 static struct demangle_component
*d_local_name (struct d_info
*);
414 static int d_discriminator (struct d_info
*);
416 static struct demangle_component
*d_lambda (struct d_info
*);
418 static struct demangle_component
*d_unnamed_type (struct d_info
*);
420 static struct demangle_component
*
421 d_clone_suffix (struct d_info
*, struct demangle_component
*);
424 d_add_substitution (struct d_info
*, struct demangle_component
*);
426 static struct demangle_component
*d_substitution (struct d_info
*, int);
428 static void d_growable_string_init (struct d_growable_string
*, size_t);
431 d_growable_string_resize (struct d_growable_string
*, size_t);
434 d_growable_string_append_buffer (struct d_growable_string
*,
435 const char *, size_t);
437 d_growable_string_callback_adapter (const char *, size_t, void *);
440 d_print_init (struct d_print_info
*, demangle_callbackref
, void *);
442 static inline void d_print_error (struct d_print_info
*);
444 static inline int d_print_saw_error (struct d_print_info
*);
446 static inline void d_print_flush (struct d_print_info
*);
448 static inline void d_append_char (struct d_print_info
*, char);
450 static inline void d_append_buffer (struct d_print_info
*,
451 const char *, size_t);
453 static inline void d_append_string (struct d_print_info
*, const char *);
455 static inline char d_last_char (struct d_print_info
*);
458 d_print_comp (struct d_print_info
*, int, const struct demangle_component
*);
461 d_print_java_identifier (struct d_print_info
*, const char *, int);
464 d_print_mod_list (struct d_print_info
*, int, struct d_print_mod
*, int);
467 d_print_mod (struct d_print_info
*, int, const struct demangle_component
*);
470 d_print_function_type (struct d_print_info
*, int,
471 const struct demangle_component
*,
472 struct d_print_mod
*);
475 d_print_array_type (struct d_print_info
*, int,
476 const struct demangle_component
*,
477 struct d_print_mod
*);
480 d_print_expr_op (struct d_print_info
*, int, const struct demangle_component
*);
483 d_print_cast (struct d_print_info
*, int, const struct demangle_component
*);
485 static int d_demangle_callback (const char *, int,
486 demangle_callbackref
, void *);
487 static char *d_demangle (const char *, int, size_t *);
489 #ifdef CP_DEMANGLE_DEBUG
492 d_dump (struct demangle_component
*dc
, int indent
)
499 printf ("failed demangling\n");
503 for (i
= 0; i
< indent
; ++i
)
508 case DEMANGLE_COMPONENT_NAME
:
509 printf ("name '%.*s'\n", dc
->u
.s_name
.len
, dc
->u
.s_name
.s
);
511 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
512 printf ("template parameter %ld\n", dc
->u
.s_number
.number
);
514 case DEMANGLE_COMPONENT_CTOR
:
515 printf ("constructor %d\n", (int) dc
->u
.s_ctor
.kind
);
516 d_dump (dc
->u
.s_ctor
.name
, indent
+ 2);
518 case DEMANGLE_COMPONENT_DTOR
:
519 printf ("destructor %d\n", (int) dc
->u
.s_dtor
.kind
);
520 d_dump (dc
->u
.s_dtor
.name
, indent
+ 2);
522 case DEMANGLE_COMPONENT_SUB_STD
:
523 printf ("standard substitution %s\n", dc
->u
.s_string
.string
);
525 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
526 printf ("builtin type %s\n", dc
->u
.s_builtin
.type
->name
);
528 case DEMANGLE_COMPONENT_OPERATOR
:
529 printf ("operator %s\n", dc
->u
.s_operator
.op
->name
);
531 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
532 printf ("extended operator with %d args\n",
533 dc
->u
.s_extended_operator
.args
);
534 d_dump (dc
->u
.s_extended_operator
.name
, indent
+ 2);
537 case DEMANGLE_COMPONENT_QUAL_NAME
:
538 printf ("qualified name\n");
540 case DEMANGLE_COMPONENT_LOCAL_NAME
:
541 printf ("local name\n");
543 case DEMANGLE_COMPONENT_TYPED_NAME
:
544 printf ("typed name\n");
546 case DEMANGLE_COMPONENT_TEMPLATE
:
547 printf ("template\n");
549 case DEMANGLE_COMPONENT_VTABLE
:
552 case DEMANGLE_COMPONENT_VTT
:
555 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
556 printf ("construction vtable\n");
558 case DEMANGLE_COMPONENT_TYPEINFO
:
559 printf ("typeinfo\n");
561 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
562 printf ("typeinfo name\n");
564 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
565 printf ("typeinfo function\n");
567 case DEMANGLE_COMPONENT_THUNK
:
570 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
571 printf ("virtual thunk\n");
573 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
574 printf ("covariant thunk\n");
576 case DEMANGLE_COMPONENT_JAVA_CLASS
:
577 printf ("java class\n");
579 case DEMANGLE_COMPONENT_GUARD
:
582 case DEMANGLE_COMPONENT_REFTEMP
:
583 printf ("reference temporary\n");
585 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
586 printf ("hidden alias\n");
588 case DEMANGLE_COMPONENT_TRANSACTION_CLONE
:
589 printf ("transaction clone\n");
591 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
:
592 printf ("non-transaction clone\n");
594 case DEMANGLE_COMPONENT_RESTRICT
:
595 printf ("restrict\n");
597 case DEMANGLE_COMPONENT_VOLATILE
:
598 printf ("volatile\n");
600 case DEMANGLE_COMPONENT_CONST
:
603 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
604 printf ("restrict this\n");
606 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
607 printf ("volatile this\n");
609 case DEMANGLE_COMPONENT_CONST_THIS
:
610 printf ("const this\n");
612 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
613 printf ("vendor type qualifier\n");
615 case DEMANGLE_COMPONENT_POINTER
:
616 printf ("pointer\n");
618 case DEMANGLE_COMPONENT_REFERENCE
:
619 printf ("reference\n");
621 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
622 printf ("rvalue reference\n");
624 case DEMANGLE_COMPONENT_COMPLEX
:
625 printf ("complex\n");
627 case DEMANGLE_COMPONENT_IMAGINARY
:
628 printf ("imaginary\n");
630 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
631 printf ("vendor type\n");
633 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
634 printf ("function type\n");
636 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
637 printf ("array type\n");
639 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
640 printf ("pointer to member type\n");
642 case DEMANGLE_COMPONENT_FIXED_TYPE
:
643 printf ("fixed-point type\n");
645 case DEMANGLE_COMPONENT_ARGLIST
:
646 printf ("argument list\n");
648 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
649 printf ("template argument list\n");
651 case DEMANGLE_COMPONENT_INITIALIZER_LIST
:
652 printf ("initializer list\n");
654 case DEMANGLE_COMPONENT_CAST
:
657 case DEMANGLE_COMPONENT_NULLARY
:
658 printf ("nullary operator\n");
660 case DEMANGLE_COMPONENT_UNARY
:
661 printf ("unary operator\n");
663 case DEMANGLE_COMPONENT_BINARY
:
664 printf ("binary operator\n");
666 case DEMANGLE_COMPONENT_BINARY_ARGS
:
667 printf ("binary operator arguments\n");
669 case DEMANGLE_COMPONENT_TRINARY
:
670 printf ("trinary operator\n");
672 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
673 printf ("trinary operator arguments 1\n");
675 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
676 printf ("trinary operator arguments 1\n");
678 case DEMANGLE_COMPONENT_LITERAL
:
679 printf ("literal\n");
681 case DEMANGLE_COMPONENT_LITERAL_NEG
:
682 printf ("negative literal\n");
684 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
685 printf ("java resource\n");
687 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
688 printf ("compound name\n");
690 case DEMANGLE_COMPONENT_CHARACTER
:
691 printf ("character '%c'\n", dc
->u
.s_character
.character
);
693 case DEMANGLE_COMPONENT_DECLTYPE
:
694 printf ("decltype\n");
696 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
697 printf ("pack expansion\n");
701 d_dump (d_left (dc
), indent
+ 2);
702 d_dump (d_right (dc
), indent
+ 2);
705 #endif /* CP_DEMANGLE_DEBUG */
707 /* Fill in a DEMANGLE_COMPONENT_NAME. */
709 CP_STATIC_IF_GLIBCPP_V3
711 cplus_demangle_fill_name (struct demangle_component
*p
, const char *s
, int len
)
713 if (p
== NULL
|| s
== NULL
|| len
== 0)
715 p
->type
= DEMANGLE_COMPONENT_NAME
;
717 p
->u
.s_name
.len
= len
;
721 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
723 CP_STATIC_IF_GLIBCPP_V3
725 cplus_demangle_fill_extended_operator (struct demangle_component
*p
, int args
,
726 struct demangle_component
*name
)
728 if (p
== NULL
|| args
< 0 || name
== NULL
)
730 p
->type
= DEMANGLE_COMPONENT_EXTENDED_OPERATOR
;
731 p
->u
.s_extended_operator
.args
= args
;
732 p
->u
.s_extended_operator
.name
= name
;
736 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
738 CP_STATIC_IF_GLIBCPP_V3
740 cplus_demangle_fill_ctor (struct demangle_component
*p
,
741 enum gnu_v3_ctor_kinds kind
,
742 struct demangle_component
*name
)
746 || (int) kind
< gnu_v3_complete_object_ctor
747 || (int) kind
> gnu_v3_object_ctor_group
)
749 p
->type
= DEMANGLE_COMPONENT_CTOR
;
750 p
->u
.s_ctor
.kind
= kind
;
751 p
->u
.s_ctor
.name
= name
;
755 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
757 CP_STATIC_IF_GLIBCPP_V3
759 cplus_demangle_fill_dtor (struct demangle_component
*p
,
760 enum gnu_v3_dtor_kinds kind
,
761 struct demangle_component
*name
)
765 || (int) kind
< gnu_v3_deleting_dtor
766 || (int) kind
> gnu_v3_object_dtor_group
)
768 p
->type
= DEMANGLE_COMPONENT_DTOR
;
769 p
->u
.s_dtor
.kind
= kind
;
770 p
->u
.s_dtor
.name
= name
;
774 /* Add a new component. */
776 static struct demangle_component
*
777 d_make_empty (struct d_info
*di
)
779 struct demangle_component
*p
;
781 if (di
->next_comp
>= di
->num_comps
)
783 p
= &di
->comps
[di
->next_comp
];
788 /* Add a new generic component. */
790 static struct demangle_component
*
791 d_make_comp (struct d_info
*di
, enum demangle_component_type type
,
792 struct demangle_component
*left
,
793 struct demangle_component
*right
)
795 struct demangle_component
*p
;
797 /* We check for errors here. A typical error would be a NULL return
798 from a subroutine. We catch those here, and return NULL
802 /* These types require two parameters. */
803 case DEMANGLE_COMPONENT_QUAL_NAME
:
804 case DEMANGLE_COMPONENT_LOCAL_NAME
:
805 case DEMANGLE_COMPONENT_TYPED_NAME
:
806 case DEMANGLE_COMPONENT_TEMPLATE
:
807 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
808 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
809 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
810 case DEMANGLE_COMPONENT_UNARY
:
811 case DEMANGLE_COMPONENT_BINARY
:
812 case DEMANGLE_COMPONENT_BINARY_ARGS
:
813 case DEMANGLE_COMPONENT_TRINARY
:
814 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
815 case DEMANGLE_COMPONENT_LITERAL
:
816 case DEMANGLE_COMPONENT_LITERAL_NEG
:
817 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
818 case DEMANGLE_COMPONENT_VECTOR_TYPE
:
819 case DEMANGLE_COMPONENT_CLONE
:
820 if (left
== NULL
|| right
== NULL
)
824 /* These types only require one parameter. */
825 case DEMANGLE_COMPONENT_VTABLE
:
826 case DEMANGLE_COMPONENT_VTT
:
827 case DEMANGLE_COMPONENT_TYPEINFO
:
828 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
829 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
830 case DEMANGLE_COMPONENT_THUNK
:
831 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
832 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
833 case DEMANGLE_COMPONENT_JAVA_CLASS
:
834 case DEMANGLE_COMPONENT_GUARD
:
835 case DEMANGLE_COMPONENT_REFTEMP
:
836 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
837 case DEMANGLE_COMPONENT_TRANSACTION_CLONE
:
838 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
:
839 case DEMANGLE_COMPONENT_POINTER
:
840 case DEMANGLE_COMPONENT_REFERENCE
:
841 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
842 case DEMANGLE_COMPONENT_COMPLEX
:
843 case DEMANGLE_COMPONENT_IMAGINARY
:
844 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
845 case DEMANGLE_COMPONENT_CAST
:
846 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
847 case DEMANGLE_COMPONENT_DECLTYPE
:
848 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
849 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
:
850 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS
:
851 case DEMANGLE_COMPONENT_NULLARY
:
852 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
857 /* This needs a right parameter, but the left parameter can be
859 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
860 case DEMANGLE_COMPONENT_INITIALIZER_LIST
:
865 /* These are allowed to have no parameters--in some cases they
866 will be filled in later. */
867 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
868 case DEMANGLE_COMPONENT_RESTRICT
:
869 case DEMANGLE_COMPONENT_VOLATILE
:
870 case DEMANGLE_COMPONENT_CONST
:
871 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
872 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
873 case DEMANGLE_COMPONENT_CONST_THIS
:
874 case DEMANGLE_COMPONENT_ARGLIST
:
875 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
878 /* Other types should not be seen here. */
883 p
= d_make_empty (di
);
887 p
->u
.s_binary
.left
= left
;
888 p
->u
.s_binary
.right
= right
;
893 /* Add a new demangle mangled name component. */
895 static struct demangle_component
*
896 d_make_demangle_mangled_name (struct d_info
*di
, const char *s
)
898 if (d_peek_char (di
) != '_' || d_peek_next_char (di
) != 'Z')
899 return d_make_name (di
, s
, strlen (s
));
901 return d_encoding (di
, 0);
904 /* Add a new name component. */
906 static struct demangle_component
*
907 d_make_name (struct d_info
*di
, const char *s
, int len
)
909 struct demangle_component
*p
;
911 p
= d_make_empty (di
);
912 if (! cplus_demangle_fill_name (p
, s
, len
))
917 /* Add a new builtin type component. */
919 static struct demangle_component
*
920 d_make_builtin_type (struct d_info
*di
,
921 const struct demangle_builtin_type_info
*type
)
923 struct demangle_component
*p
;
927 p
= d_make_empty (di
);
930 p
->type
= DEMANGLE_COMPONENT_BUILTIN_TYPE
;
931 p
->u
.s_builtin
.type
= type
;
936 /* Add a new operator component. */
938 static struct demangle_component
*
939 d_make_operator (struct d_info
*di
, const struct demangle_operator_info
*op
)
941 struct demangle_component
*p
;
943 p
= d_make_empty (di
);
946 p
->type
= DEMANGLE_COMPONENT_OPERATOR
;
947 p
->u
.s_operator
.op
= op
;
952 /* Add a new extended operator component. */
954 static struct demangle_component
*
955 d_make_extended_operator (struct d_info
*di
, int args
,
956 struct demangle_component
*name
)
958 struct demangle_component
*p
;
960 p
= d_make_empty (di
);
961 if (! cplus_demangle_fill_extended_operator (p
, args
, name
))
966 static struct demangle_component
*
967 d_make_default_arg (struct d_info
*di
, int num
,
968 struct demangle_component
*sub
)
970 struct demangle_component
*p
= d_make_empty (di
);
973 p
->type
= DEMANGLE_COMPONENT_DEFAULT_ARG
;
974 p
->u
.s_unary_num
.num
= num
;
975 p
->u
.s_unary_num
.sub
= sub
;
980 /* Add a new constructor component. */
982 static struct demangle_component
*
983 d_make_ctor (struct d_info
*di
, enum gnu_v3_ctor_kinds kind
,
984 struct demangle_component
*name
)
986 struct demangle_component
*p
;
988 p
= d_make_empty (di
);
989 if (! cplus_demangle_fill_ctor (p
, kind
, name
))
994 /* Add a new destructor component. */
996 static struct demangle_component
*
997 d_make_dtor (struct d_info
*di
, enum gnu_v3_dtor_kinds kind
,
998 struct demangle_component
*name
)
1000 struct demangle_component
*p
;
1002 p
= d_make_empty (di
);
1003 if (! cplus_demangle_fill_dtor (p
, kind
, name
))
1008 /* Add a new template parameter. */
1010 static struct demangle_component
*
1011 d_make_template_param (struct d_info
*di
, long i
)
1013 struct demangle_component
*p
;
1015 p
= d_make_empty (di
);
1018 p
->type
= DEMANGLE_COMPONENT_TEMPLATE_PARAM
;
1019 p
->u
.s_number
.number
= i
;
1024 /* Add a new function parameter. */
1026 static struct demangle_component
*
1027 d_make_function_param (struct d_info
*di
, long i
)
1029 struct demangle_component
*p
;
1031 p
= d_make_empty (di
);
1034 p
->type
= DEMANGLE_COMPONENT_FUNCTION_PARAM
;
1035 p
->u
.s_number
.number
= i
;
1040 /* Add a new standard substitution component. */
1042 static struct demangle_component
*
1043 d_make_sub (struct d_info
*di
, const char *name
, int len
)
1045 struct demangle_component
*p
;
1047 p
= d_make_empty (di
);
1050 p
->type
= DEMANGLE_COMPONENT_SUB_STD
;
1051 p
->u
.s_string
.string
= name
;
1052 p
->u
.s_string
.len
= len
;
1057 /* <mangled-name> ::= _Z <encoding> [<clone-suffix>]*
1059 TOP_LEVEL is non-zero when called at the top level. */
1061 CP_STATIC_IF_GLIBCPP_V3
1062 struct demangle_component
*
1063 cplus_demangle_mangled_name (struct d_info
*di
, int top_level
)
1065 struct demangle_component
*p
;
1067 if (! d_check_char (di
, '_')
1068 /* Allow missing _ if not at toplevel to work around a
1069 bug in G++ abi-version=2 mangling; see the comment in
1070 write_template_arg. */
1073 if (! d_check_char (di
, 'Z'))
1075 p
= d_encoding (di
, top_level
);
1077 /* If at top level and parsing parameters, check for a clone
1079 if (top_level
&& (di
->options
& DMGL_PARAMS
) != 0)
1080 while (d_peek_char (di
) == '.'
1081 && (IS_LOWER (d_peek_next_char (di
))
1082 || d_peek_next_char (di
) == '_'
1083 || IS_DIGIT (d_peek_next_char (di
))))
1084 p
= d_clone_suffix (di
, p
);
1089 /* Return whether a function should have a return type. The argument
1090 is the function name, which may be qualified in various ways. The
1091 rules are that template functions have return types with some
1092 exceptions, function types which are not part of a function name
1093 mangling have return types with some exceptions, and non-template
1094 function names do not have return types. The exceptions are that
1095 constructors, destructors, and conversion operators do not have
1099 has_return_type (struct demangle_component
*dc
)
1107 case DEMANGLE_COMPONENT_TEMPLATE
:
1108 return ! is_ctor_dtor_or_conversion (d_left (dc
));
1109 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
1110 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
1111 case DEMANGLE_COMPONENT_CONST_THIS
:
1112 return has_return_type (d_left (dc
));
1116 /* Return whether a name is a constructor, a destructor, or a
1117 conversion operator. */
1120 is_ctor_dtor_or_conversion (struct demangle_component
*dc
)
1128 case DEMANGLE_COMPONENT_QUAL_NAME
:
1129 case DEMANGLE_COMPONENT_LOCAL_NAME
:
1130 return is_ctor_dtor_or_conversion (d_right (dc
));
1131 case DEMANGLE_COMPONENT_CTOR
:
1132 case DEMANGLE_COMPONENT_DTOR
:
1133 case DEMANGLE_COMPONENT_CAST
:
1138 /* <encoding> ::= <(function) name> <bare-function-type>
1142 TOP_LEVEL is non-zero when called at the top level, in which case
1143 if DMGL_PARAMS is not set we do not demangle the function
1144 parameters. We only set this at the top level, because otherwise
1145 we would not correctly demangle names in local scopes. */
1147 static struct demangle_component
*
1148 d_encoding (struct d_info
*di
, int top_level
)
1150 char peek
= d_peek_char (di
);
1152 if (peek
== 'G' || peek
== 'T')
1153 return d_special_name (di
);
1156 struct demangle_component
*dc
;
1160 if (dc
!= NULL
&& top_level
&& (di
->options
& DMGL_PARAMS
) == 0)
1162 /* Strip off any initial CV-qualifiers, as they really apply
1163 to the `this' parameter, and they were not output by the
1164 v2 demangler without DMGL_PARAMS. */
1165 while (dc
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
1166 || dc
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
1167 || dc
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
1170 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1171 there may be CV-qualifiers on its right argument which
1172 really apply here; this happens when parsing a class
1173 which is local to a function. */
1174 if (dc
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
1176 struct demangle_component
*dcr
;
1179 while (dcr
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
1180 || dcr
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
1181 || dcr
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
1183 dc
->u
.s_binary
.right
= dcr
;
1189 peek
= d_peek_char (di
);
1190 if (dc
== NULL
|| peek
== '\0' || peek
== 'E')
1192 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPED_NAME
, dc
,
1193 d_bare_function_type (di
, has_return_type (dc
)));
1197 /* <name> ::= <nested-name>
1199 ::= <unscoped-template-name> <template-args>
1202 <unscoped-name> ::= <unqualified-name>
1203 ::= St <unqualified-name>
1205 <unscoped-template-name> ::= <unscoped-name>
1209 static struct demangle_component
*
1210 d_name (struct d_info
*di
)
1212 char peek
= d_peek_char (di
);
1213 struct demangle_component
*dc
;
1218 return d_nested_name (di
);
1221 return d_local_name (di
);
1225 return d_unqualified_name (di
);
1231 if (d_peek_next_char (di
) != 't')
1233 dc
= d_substitution (di
, 0);
1239 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
,
1240 d_make_name (di
, "std", 3),
1241 d_unqualified_name (di
));
1246 if (d_peek_char (di
) != 'I')
1248 /* The grammar does not permit this case to occur if we
1249 called d_substitution() above (i.e., subst == 1). We
1250 don't bother to check. */
1254 /* This is <template-args>, which means that we just saw
1255 <unscoped-template-name>, which is a substitution
1256 candidate if we didn't just get it from a
1260 if (! d_add_substitution (di
, dc
))
1263 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, dc
,
1264 d_template_args (di
));
1271 dc
= d_unqualified_name (di
);
1272 if (d_peek_char (di
) == 'I')
1274 /* This is <template-args>, which means that we just saw
1275 <unscoped-template-name>, which is a substitution
1277 if (! d_add_substitution (di
, dc
))
1279 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, dc
,
1280 d_template_args (di
));
1286 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1287 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1290 static struct demangle_component
*
1291 d_nested_name (struct d_info
*di
)
1293 struct demangle_component
*ret
;
1294 struct demangle_component
**pret
;
1296 if (! d_check_char (di
, 'N'))
1299 pret
= d_cv_qualifiers (di
, &ret
, 1);
1303 *pret
= d_prefix (di
);
1307 if (! d_check_char (di
, 'E'))
1313 /* <prefix> ::= <prefix> <unqualified-name>
1314 ::= <template-prefix> <template-args>
1315 ::= <template-param>
1320 <template-prefix> ::= <prefix> <(template) unqualified-name>
1321 ::= <template-param>
1325 static struct demangle_component
*
1326 d_prefix (struct d_info
*di
)
1328 struct demangle_component
*ret
= NULL
;
1333 enum demangle_component_type comb_type
;
1334 struct demangle_component
*dc
;
1336 peek
= d_peek_char (di
);
1340 /* The older code accepts a <local-name> here, but I don't see
1341 that in the grammar. The older code does not accept a
1342 <template-param> here. */
1344 comb_type
= DEMANGLE_COMPONENT_QUAL_NAME
;
1347 char peek2
= d_peek_next_char (di
);
1348 if (peek2
== 'T' || peek2
== 't')
1350 dc
= cplus_demangle_type (di
);
1352 /* Destructor name. */
1353 dc
= d_unqualified_name (di
);
1355 else if (IS_DIGIT (peek
)
1360 dc
= d_unqualified_name (di
);
1361 else if (peek
== 'S')
1362 dc
= d_substitution (di
, 1);
1363 else if (peek
== 'I')
1367 comb_type
= DEMANGLE_COMPONENT_TEMPLATE
;
1368 dc
= d_template_args (di
);
1370 else if (peek
== 'T')
1371 dc
= d_template_param (di
);
1372 else if (peek
== 'E')
1374 else if (peek
== 'M')
1376 /* Initializer scope for a lambda. We don't need to represent
1377 this; the normal code will just treat the variable as a type
1378 scope, which gives appropriate output. */
1390 ret
= d_make_comp (di
, comb_type
, ret
, dc
);
1392 if (peek
!= 'S' && d_peek_char (di
) != 'E')
1394 if (! d_add_substitution (di
, ret
))
1400 /* <unqualified-name> ::= <operator-name>
1401 ::= <ctor-dtor-name>
1403 ::= <local-source-name>
1405 <local-source-name> ::= L <source-name> <discriminator>
1408 static struct demangle_component
*
1409 d_unqualified_name (struct d_info
*di
)
1413 peek
= d_peek_char (di
);
1414 if (IS_DIGIT (peek
))
1415 return d_source_name (di
);
1416 else if (IS_LOWER (peek
))
1418 struct demangle_component
*ret
;
1420 ret
= d_operator_name (di
);
1421 if (ret
!= NULL
&& ret
->type
== DEMANGLE_COMPONENT_OPERATOR
)
1422 di
->expansion
+= sizeof "operator" + ret
->u
.s_operator
.op
->len
- 2;
1425 else if (peek
== 'C' || peek
== 'D')
1426 return d_ctor_dtor_name (di
);
1427 else if (peek
== 'L')
1429 struct demangle_component
* ret
;
1433 ret
= d_source_name (di
);
1436 if (! d_discriminator (di
))
1440 else if (peek
== 'U')
1442 switch (d_peek_next_char (di
))
1445 return d_lambda (di
);
1447 return d_unnamed_type (di
);
1456 /* <source-name> ::= <(positive length) number> <identifier> */
1458 static struct demangle_component
*
1459 d_source_name (struct d_info
*di
)
1462 struct demangle_component
*ret
;
1464 len
= d_number (di
);
1467 ret
= d_identifier (di
, len
);
1468 di
->last_name
= ret
;
1472 /* number ::= [n] <(non-negative decimal integer)> */
1475 d_number (struct d_info
*di
)
1482 peek
= d_peek_char (di
);
1487 peek
= d_peek_char (di
);
1493 if (! IS_DIGIT (peek
))
1499 ret
= ret
* 10 + peek
- '0';
1501 peek
= d_peek_char (di
);
1505 /* Like d_number, but returns a demangle_component. */
1507 static struct demangle_component
*
1508 d_number_component (struct d_info
*di
)
1510 struct demangle_component
*ret
= d_make_empty (di
);
1513 ret
->type
= DEMANGLE_COMPONENT_NUMBER
;
1514 ret
->u
.s_number
.number
= d_number (di
);
1519 /* identifier ::= <(unqualified source code identifier)> */
1521 static struct demangle_component
*
1522 d_identifier (struct d_info
*di
, int len
)
1528 if (di
->send
- name
< len
)
1531 d_advance (di
, len
);
1533 /* A Java mangled name may have a trailing '$' if it is a C++
1534 keyword. This '$' is not included in the length count. We just
1536 if ((di
->options
& DMGL_JAVA
) != 0
1537 && d_peek_char (di
) == '$')
1540 /* Look for something which looks like a gcc encoding of an
1541 anonymous namespace, and replace it with a more user friendly
1543 if (len
>= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN
+ 2
1544 && memcmp (name
, ANONYMOUS_NAMESPACE_PREFIX
,
1545 ANONYMOUS_NAMESPACE_PREFIX_LEN
) == 0)
1549 s
= name
+ ANONYMOUS_NAMESPACE_PREFIX_LEN
;
1550 if ((*s
== '.' || *s
== '_' || *s
== '$')
1553 di
->expansion
-= len
- sizeof "(anonymous namespace)";
1554 return d_make_name (di
, "(anonymous namespace)",
1555 sizeof "(anonymous namespace)" - 1);
1559 return d_make_name (di
, name
, len
);
1562 /* operator_name ::= many different two character encodings.
1564 ::= v <digit> <source-name>
1566 This list is sorted for binary search. */
1568 #define NL(s) s, (sizeof s) - 1
1570 CP_STATIC_IF_GLIBCPP_V3
1571 const struct demangle_operator_info cplus_demangle_operators
[] =
1573 { "aN", NL ("&="), 2 },
1574 { "aS", NL ("="), 2 },
1575 { "aa", NL ("&&"), 2 },
1576 { "ad", NL ("&"), 1 },
1577 { "an", NL ("&"), 2 },
1578 { "at", NL ("alignof "), 1 },
1579 { "az", NL ("alignof "), 1 },
1580 { "cl", NL ("()"), 2 },
1581 { "cm", NL (","), 2 },
1582 { "co", NL ("~"), 1 },
1583 { "dV", NL ("/="), 2 },
1584 { "da", NL ("delete[] "), 1 },
1585 { "de", NL ("*"), 1 },
1586 { "dl", NL ("delete "), 1 },
1587 { "ds", NL (".*"), 2 },
1588 { "dt", NL ("."), 2 },
1589 { "dv", NL ("/"), 2 },
1590 { "eO", NL ("^="), 2 },
1591 { "eo", NL ("^"), 2 },
1592 { "eq", NL ("=="), 2 },
1593 { "ge", NL (">="), 2 },
1594 { "gs", NL ("::"), 1 },
1595 { "gt", NL (">"), 2 },
1596 { "ix", NL ("[]"), 2 },
1597 { "lS", NL ("<<="), 2 },
1598 { "le", NL ("<="), 2 },
1599 { "ls", NL ("<<"), 2 },
1600 { "lt", NL ("<"), 2 },
1601 { "mI", NL ("-="), 2 },
1602 { "mL", NL ("*="), 2 },
1603 { "mi", NL ("-"), 2 },
1604 { "ml", NL ("*"), 2 },
1605 { "mm", NL ("--"), 1 },
1606 { "na", NL ("new[]"), 3 },
1607 { "ne", NL ("!="), 2 },
1608 { "ng", NL ("-"), 1 },
1609 { "nt", NL ("!"), 1 },
1610 { "nw", NL ("new"), 3 },
1611 { "oR", NL ("|="), 2 },
1612 { "oo", NL ("||"), 2 },
1613 { "or", NL ("|"), 2 },
1614 { "pL", NL ("+="), 2 },
1615 { "pl", NL ("+"), 2 },
1616 { "pm", NL ("->*"), 2 },
1617 { "pp", NL ("++"), 1 },
1618 { "ps", NL ("+"), 1 },
1619 { "pt", NL ("->"), 2 },
1620 { "qu", NL ("?"), 3 },
1621 { "rM", NL ("%="), 2 },
1622 { "rS", NL (">>="), 2 },
1623 { "rm", NL ("%"), 2 },
1624 { "rs", NL (">>"), 2 },
1625 { "st", NL ("sizeof "), 1 },
1626 { "sz", NL ("sizeof "), 1 },
1627 { "tr", NL ("throw"), 0 },
1628 { "tw", NL ("throw "), 1 },
1629 { NULL
, NULL
, 0, 0 }
1632 static struct demangle_component
*
1633 d_operator_name (struct d_info
*di
)
1638 c1
= d_next_char (di
);
1639 c2
= d_next_char (di
);
1640 if (c1
== 'v' && IS_DIGIT (c2
))
1641 return d_make_extended_operator (di
, c2
- '0', d_source_name (di
));
1642 else if (c1
== 'c' && c2
== 'v')
1643 return d_make_comp (di
, DEMANGLE_COMPONENT_CAST
,
1644 cplus_demangle_type (di
), NULL
);
1647 /* LOW is the inclusive lower bound. */
1649 /* HIGH is the exclusive upper bound. We subtract one to ignore
1650 the sentinel at the end of the array. */
1651 int high
= ((sizeof (cplus_demangle_operators
)
1652 / sizeof (cplus_demangle_operators
[0]))
1658 const struct demangle_operator_info
*p
;
1660 i
= low
+ (high
- low
) / 2;
1661 p
= cplus_demangle_operators
+ i
;
1663 if (c1
== p
->code
[0] && c2
== p
->code
[1])
1664 return d_make_operator (di
, p
);
1666 if (c1
< p
->code
[0] || (c1
== p
->code
[0] && c2
< p
->code
[1]))
1676 static struct demangle_component
*
1677 d_make_character (struct d_info
*di
, int c
)
1679 struct demangle_component
*p
;
1680 p
= d_make_empty (di
);
1683 p
->type
= DEMANGLE_COMPONENT_CHARACTER
;
1684 p
->u
.s_character
.character
= c
;
1689 static struct demangle_component
*
1690 d_java_resource (struct d_info
*di
)
1692 struct demangle_component
*p
= NULL
;
1693 struct demangle_component
*next
= NULL
;
1698 len
= d_number (di
);
1702 /* Eat the leading '_'. */
1703 if (d_next_char (di
) != '_')
1716 /* Each chunk is either a '$' escape... */
1734 next
= d_make_character (di
, c
);
1742 /* ... or a sequence of characters. */
1745 while (i
< len
&& str
[i
] && str
[i
] != '$')
1748 next
= d_make_name (di
, str
, i
);
1761 p
= d_make_comp (di
, DEMANGLE_COMPONENT_COMPOUND_NAME
, p
, next
);
1767 p
= d_make_comp (di
, DEMANGLE_COMPONENT_JAVA_RESOURCE
, p
, NULL
);
1772 /* <special-name> ::= TV <type>
1776 ::= GV <(object) name>
1777 ::= T <call-offset> <(base) encoding>
1778 ::= Tc <call-offset> <call-offset> <(base) encoding>
1779 Also g++ extensions:
1780 ::= TC <type> <(offset) number> _ <(base) type>
1785 ::= Gr <resource name>
1790 static struct demangle_component
*
1791 d_special_name (struct d_info
*di
)
1793 di
->expansion
+= 20;
1794 if (d_check_char (di
, 'T'))
1796 switch (d_next_char (di
))
1800 return d_make_comp (di
, DEMANGLE_COMPONENT_VTABLE
,
1801 cplus_demangle_type (di
), NULL
);
1803 di
->expansion
-= 10;
1804 return d_make_comp (di
, DEMANGLE_COMPONENT_VTT
,
1805 cplus_demangle_type (di
), NULL
);
1807 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO
,
1808 cplus_demangle_type (di
), NULL
);
1810 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO_NAME
,
1811 cplus_demangle_type (di
), NULL
);
1814 if (! d_call_offset (di
, 'h'))
1816 return d_make_comp (di
, DEMANGLE_COMPONENT_THUNK
,
1817 d_encoding (di
, 0), NULL
);
1820 if (! d_call_offset (di
, 'v'))
1822 return d_make_comp (di
, DEMANGLE_COMPONENT_VIRTUAL_THUNK
,
1823 d_encoding (di
, 0), NULL
);
1826 if (! d_call_offset (di
, '\0'))
1828 if (! d_call_offset (di
, '\0'))
1830 return d_make_comp (di
, DEMANGLE_COMPONENT_COVARIANT_THUNK
,
1831 d_encoding (di
, 0), NULL
);
1835 struct demangle_component
*derived_type
;
1837 struct demangle_component
*base_type
;
1839 derived_type
= cplus_demangle_type (di
);
1840 offset
= d_number (di
);
1843 if (! d_check_char (di
, '_'))
1845 base_type
= cplus_demangle_type (di
);
1846 /* We don't display the offset. FIXME: We should display
1847 it in verbose mode. */
1849 return d_make_comp (di
, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
,
1850 base_type
, derived_type
);
1854 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO_FN
,
1855 cplus_demangle_type (di
), NULL
);
1857 return d_make_comp (di
, DEMANGLE_COMPONENT_JAVA_CLASS
,
1858 cplus_demangle_type (di
), NULL
);
1864 else if (d_check_char (di
, 'G'))
1866 switch (d_next_char (di
))
1869 return d_make_comp (di
, DEMANGLE_COMPONENT_GUARD
, d_name (di
), NULL
);
1873 struct demangle_component
*name
= d_name (di
);
1874 return d_make_comp (di
, DEMANGLE_COMPONENT_REFTEMP
, name
,
1875 d_number_component (di
));
1879 return d_make_comp (di
, DEMANGLE_COMPONENT_HIDDEN_ALIAS
,
1880 d_encoding (di
, 0), NULL
);
1883 switch (d_next_char (di
))
1886 return d_make_comp (di
, DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
,
1887 d_encoding (di
, 0), NULL
);
1889 /* ??? The proposal is that other letters (such as 'h') stand
1890 for different variants of transaction cloning, such as
1891 compiling directly for hardware transaction support. But
1892 they still should all be transactional clones of some sort
1893 so go ahead and call them that. */
1895 return d_make_comp (di
, DEMANGLE_COMPONENT_TRANSACTION_CLONE
,
1896 d_encoding (di
, 0), NULL
);
1900 return d_java_resource (di
);
1910 /* <call-offset> ::= h <nv-offset> _
1913 <nv-offset> ::= <(offset) number>
1915 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1917 The C parameter, if not '\0', is a character we just read which is
1918 the start of the <call-offset>.
1920 We don't display the offset information anywhere. FIXME: We should
1921 display it in verbose mode. */
1924 d_call_offset (struct d_info
*di
, int c
)
1927 c
= d_next_char (di
);
1934 if (! d_check_char (di
, '_'))
1941 if (! d_check_char (di
, '_'))
1947 /* <ctor-dtor-name> ::= C1
1955 static struct demangle_component
*
1956 d_ctor_dtor_name (struct d_info
*di
)
1958 if (di
->last_name
!= NULL
)
1960 if (di
->last_name
->type
== DEMANGLE_COMPONENT_NAME
)
1961 di
->expansion
+= di
->last_name
->u
.s_name
.len
;
1962 else if (di
->last_name
->type
== DEMANGLE_COMPONENT_SUB_STD
)
1963 di
->expansion
+= di
->last_name
->u
.s_string
.len
;
1965 switch (d_peek_char (di
))
1969 enum gnu_v3_ctor_kinds kind
;
1971 switch (d_peek_next_char (di
))
1974 kind
= gnu_v3_complete_object_ctor
;
1977 kind
= gnu_v3_base_object_ctor
;
1980 kind
= gnu_v3_complete_object_allocating_ctor
;
1983 kind
= gnu_v3_object_ctor_group
;
1989 return d_make_ctor (di
, kind
, di
->last_name
);
1994 enum gnu_v3_dtor_kinds kind
;
1996 switch (d_peek_next_char (di
))
1999 kind
= gnu_v3_deleting_dtor
;
2002 kind
= gnu_v3_complete_object_dtor
;
2005 kind
= gnu_v3_base_object_dtor
;
2008 kind
= gnu_v3_object_dtor_group
;
2014 return d_make_dtor (di
, kind
, di
->last_name
);
2022 /* <type> ::= <builtin-type>
2024 ::= <class-enum-type>
2026 ::= <pointer-to-member-type>
2027 ::= <template-param>
2028 ::= <template-template-param> <template-args>
2030 ::= <CV-qualifiers> <type>
2033 ::= O <type> (C++0x)
2036 ::= U <source-name> <type>
2038 <builtin-type> ::= various one letter codes
2042 CP_STATIC_IF_GLIBCPP_V3
2043 const struct demangle_builtin_type_info
2044 cplus_demangle_builtin_types
[D_BUILTIN_TYPE_COUNT
] =
2046 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT
},
2047 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL
},
2048 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT
},
2049 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT
},
2050 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT
},
2051 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT
},
2052 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT
},
2053 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT
},
2054 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT
},
2055 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED
},
2056 /* k */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2057 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG
},
2058 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG
},
2059 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT
},
2060 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
2062 /* p */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2063 /* q */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2064 /* r */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2065 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT
},
2066 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT
},
2067 /* u */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2068 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID
},
2069 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT
},
2070 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG
},
2071 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
2072 D_PRINT_UNSIGNED_LONG_LONG
},
2073 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT
},
2074 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT
},
2075 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT
},
2076 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT
},
2077 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT
},
2078 /* 30 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT
},
2079 /* 31 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT
},
2080 /* 32 */ { NL ("decltype(nullptr)"), NL ("decltype(nullptr)"),
2084 CP_STATIC_IF_GLIBCPP_V3
2085 struct demangle_component
*
2086 cplus_demangle_type (struct d_info
*di
)
2089 struct demangle_component
*ret
;
2092 /* The ABI specifies that when CV-qualifiers are used, the base type
2093 is substitutable, and the fully qualified type is substitutable,
2094 but the base type with a strict subset of the CV-qualifiers is
2095 not substitutable. The natural recursive implementation of the
2096 CV-qualifiers would cause subsets to be substitutable, so instead
2097 we pull them all off now.
2099 FIXME: The ABI says that order-insensitive vendor qualifiers
2100 should be handled in the same way, but we have no way to tell
2101 which vendor qualifiers are order-insensitive and which are
2102 order-sensitive. So we just assume that they are all
2103 order-sensitive. g++ 3.4 supports only one vendor qualifier,
2104 __vector, and it treats it as order-sensitive when mangling
2107 peek
= d_peek_char (di
);
2108 if (peek
== 'r' || peek
== 'V' || peek
== 'K')
2110 struct demangle_component
**pret
;
2112 pret
= d_cv_qualifiers (di
, &ret
, 0);
2115 *pret
= cplus_demangle_type (di
);
2116 if (! *pret
|| ! d_add_substitution (di
, ret
))
2125 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2126 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
2127 case 'o': case 's': case 't':
2128 case 'v': case 'w': case 'x': case 'y': case 'z':
2129 ret
= d_make_builtin_type (di
,
2130 &cplus_demangle_builtin_types
[peek
- 'a']);
2131 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2138 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_VENDOR_TYPE
,
2139 d_source_name (di
), NULL
);
2143 ret
= d_function_type (di
);
2146 case '0': case '1': case '2': case '3': case '4':
2147 case '5': case '6': case '7': case '8': case '9':
2150 ret
= d_class_enum_type (di
);
2154 ret
= d_array_type (di
);
2158 ret
= d_pointer_to_member_type (di
);
2162 ret
= d_template_param (di
);
2163 if (d_peek_char (di
) == 'I')
2165 /* This is <template-template-param> <template-args>. The
2166 <template-template-param> part is a substitution
2168 if (! d_add_substitution (di
, ret
))
2170 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
,
2171 d_template_args (di
));
2176 /* If this is a special substitution, then it is the start of
2177 <class-enum-type>. */
2181 peek_next
= d_peek_next_char (di
);
2182 if (IS_DIGIT (peek_next
)
2184 || IS_UPPER (peek_next
))
2186 ret
= d_substitution (di
, 0);
2187 /* The substituted name may have been a template name and
2188 may be followed by tepmlate args. */
2189 if (d_peek_char (di
) == 'I')
2190 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
,
2191 d_template_args (di
));
2197 ret
= d_class_enum_type (di
);
2198 /* If the substitution was a complete type, then it is not
2199 a new substitution candidate. However, if the
2200 substitution was followed by template arguments, then
2201 the whole thing is a substitution candidate. */
2202 if (ret
!= NULL
&& ret
->type
== DEMANGLE_COMPONENT_SUB_STD
)
2210 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_RVALUE_REFERENCE
,
2211 cplus_demangle_type (di
), NULL
);
2216 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_POINTER
,
2217 cplus_demangle_type (di
), NULL
);
2222 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_REFERENCE
,
2223 cplus_demangle_type (di
), NULL
);
2228 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_COMPLEX
,
2229 cplus_demangle_type (di
), NULL
);
2234 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_IMAGINARY
,
2235 cplus_demangle_type (di
), NULL
);
2240 ret
= d_source_name (di
);
2241 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
,
2242 cplus_demangle_type (di
), ret
);
2248 peek
= d_next_char (di
);
2253 /* decltype (expression) */
2254 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_DECLTYPE
,
2255 d_expression (di
), NULL
);
2256 if (ret
&& d_next_char (di
) != 'E')
2262 /* Pack expansion. */
2263 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_PACK_EXPANSION
,
2264 cplus_demangle_type (di
), NULL
);
2269 /* 32-bit decimal floating point */
2270 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[26]);
2271 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2275 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[27]);
2276 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2280 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[28]);
2281 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2284 /* 16-bit half-precision FP */
2285 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[29]);
2286 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2290 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[30]);
2291 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2295 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[31]);
2296 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2300 /* Fixed point types. DF<int bits><length><fract bits><sat> */
2301 ret
= d_make_empty (di
);
2302 ret
->type
= DEMANGLE_COMPONENT_FIXED_TYPE
;
2303 if ((ret
->u
.s_fixed
.accum
= IS_DIGIT (d_peek_char (di
))))
2304 /* For demangling we don't care about the bits. */
2306 ret
->u
.s_fixed
.length
= cplus_demangle_type (di
);
2307 if (ret
->u
.s_fixed
.length
== NULL
)
2310 peek
= d_next_char (di
);
2311 ret
->u
.s_fixed
.sat
= (peek
== 's');
2315 ret
= d_vector_type (di
);
2320 /* decltype(nullptr) */
2321 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[32]);
2322 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2336 if (! d_add_substitution (di
, ret
))
2343 /* <CV-qualifiers> ::= [r] [V] [K] */
2345 static struct demangle_component
**
2346 d_cv_qualifiers (struct d_info
*di
,
2347 struct demangle_component
**pret
, int member_fn
)
2349 struct demangle_component
**pstart
;
2353 peek
= d_peek_char (di
);
2354 while (peek
== 'r' || peek
== 'V' || peek
== 'K')
2356 enum demangle_component_type t
;
2362 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2363 : DEMANGLE_COMPONENT_RESTRICT
);
2364 di
->expansion
+= sizeof "restrict";
2366 else if (peek
== 'V')
2369 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2370 : DEMANGLE_COMPONENT_VOLATILE
);
2371 di
->expansion
+= sizeof "volatile";
2376 ? DEMANGLE_COMPONENT_CONST_THIS
2377 : DEMANGLE_COMPONENT_CONST
);
2378 di
->expansion
+= sizeof "const";
2381 *pret
= d_make_comp (di
, t
, NULL
, NULL
);
2384 pret
= &d_left (*pret
);
2386 peek
= d_peek_char (di
);
2389 if (!member_fn
&& peek
== 'F')
2391 while (pstart
!= pret
)
2393 switch ((*pstart
)->type
)
2395 case DEMANGLE_COMPONENT_RESTRICT
:
2396 (*pstart
)->type
= DEMANGLE_COMPONENT_RESTRICT_THIS
;
2398 case DEMANGLE_COMPONENT_VOLATILE
:
2399 (*pstart
)->type
= DEMANGLE_COMPONENT_VOLATILE_THIS
;
2401 case DEMANGLE_COMPONENT_CONST
:
2402 (*pstart
)->type
= DEMANGLE_COMPONENT_CONST_THIS
;
2407 pstart
= &d_left (*pstart
);
2414 /* <function-type> ::= F [Y] <bare-function-type> E */
2416 static struct demangle_component
*
2417 d_function_type (struct d_info
*di
)
2419 struct demangle_component
*ret
;
2421 if (! d_check_char (di
, 'F'))
2423 if (d_peek_char (di
) == 'Y')
2425 /* Function has C linkage. We don't print this information.
2426 FIXME: We should print it in verbose mode. */
2429 ret
= d_bare_function_type (di
, 1);
2430 if (! d_check_char (di
, 'E'))
2437 static struct demangle_component
*
2438 d_parmlist (struct d_info
*di
)
2440 struct demangle_component
*tl
;
2441 struct demangle_component
**ptl
;
2447 struct demangle_component
*type
;
2449 char peek
= d_peek_char (di
);
2450 if (peek
== '\0' || peek
== 'E' || peek
== '.')
2452 type
= cplus_demangle_type (di
);
2455 *ptl
= d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, type
, NULL
);
2458 ptl
= &d_right (*ptl
);
2461 /* There should be at least one parameter type besides the optional
2462 return type. A function which takes no arguments will have a
2463 single parameter type void. */
2467 /* If we have a single parameter type void, omit it. */
2468 if (d_right (tl
) == NULL
2469 && d_left (tl
)->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
2470 && d_left (tl
)->u
.s_builtin
.type
->print
== D_PRINT_VOID
)
2472 di
->expansion
-= d_left (tl
)->u
.s_builtin
.type
->len
;
2479 /* <bare-function-type> ::= [J]<type>+ */
2481 static struct demangle_component
*
2482 d_bare_function_type (struct d_info
*di
, int has_return_type
)
2484 struct demangle_component
*return_type
;
2485 struct demangle_component
*tl
;
2488 /* Detect special qualifier indicating that the first argument
2489 is the return type. */
2490 peek
= d_peek_char (di
);
2494 has_return_type
= 1;
2497 if (has_return_type
)
2499 return_type
= cplus_demangle_type (di
);
2500 if (return_type
== NULL
)
2506 tl
= d_parmlist (di
);
2510 return d_make_comp (di
, DEMANGLE_COMPONENT_FUNCTION_TYPE
,
2514 /* <class-enum-type> ::= <name> */
2516 static struct demangle_component
*
2517 d_class_enum_type (struct d_info
*di
)
2522 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2523 ::= A [<(dimension) expression>] _ <(element) type>
2526 static struct demangle_component
*
2527 d_array_type (struct d_info
*di
)
2530 struct demangle_component
*dim
;
2532 if (! d_check_char (di
, 'A'))
2535 peek
= d_peek_char (di
);
2538 else if (IS_DIGIT (peek
))
2546 peek
= d_peek_char (di
);
2548 while (IS_DIGIT (peek
));
2549 dim
= d_make_name (di
, s
, d_str (di
) - s
);
2555 dim
= d_expression (di
);
2560 if (! d_check_char (di
, '_'))
2563 return d_make_comp (di
, DEMANGLE_COMPONENT_ARRAY_TYPE
, dim
,
2564 cplus_demangle_type (di
));
2567 /* <vector-type> ::= Dv <number> _ <type>
2568 ::= Dv _ <expression> _ <type> */
2570 static struct demangle_component
*
2571 d_vector_type (struct d_info
*di
)
2574 struct demangle_component
*dim
;
2576 peek
= d_peek_char (di
);
2580 dim
= d_expression (di
);
2583 dim
= d_number_component (di
);
2588 if (! d_check_char (di
, '_'))
2591 return d_make_comp (di
, DEMANGLE_COMPONENT_VECTOR_TYPE
, dim
,
2592 cplus_demangle_type (di
));
2595 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2597 static struct demangle_component
*
2598 d_pointer_to_member_type (struct d_info
*di
)
2600 struct demangle_component
*cl
;
2601 struct demangle_component
*mem
;
2602 struct demangle_component
**pmem
;
2604 if (! d_check_char (di
, 'M'))
2607 cl
= cplus_demangle_type (di
);
2609 /* The ABI specifies that any type can be a substitution source, and
2610 that M is followed by two types, and that when a CV-qualified
2611 type is seen both the base type and the CV-qualified types are
2612 substitution sources. The ABI also specifies that for a pointer
2613 to a CV-qualified member function, the qualifiers are attached to
2614 the second type. Given the grammar, a plain reading of the ABI
2615 suggests that both the CV-qualified member function and the
2616 non-qualified member function are substitution sources. However,
2617 g++ does not work that way. g++ treats only the CV-qualified
2618 member function as a substitution source. FIXME. So to work
2619 with g++, we need to pull off the CV-qualifiers here, in order to
2620 avoid calling add_substitution() in cplus_demangle_type(). But
2621 for a CV-qualified member which is not a function, g++ does
2622 follow the ABI, so we need to handle that case here by calling
2623 d_add_substitution ourselves. */
2625 pmem
= d_cv_qualifiers (di
, &mem
, 1);
2628 *pmem
= cplus_demangle_type (di
);
2632 if (pmem
!= &mem
&& (*pmem
)->type
!= DEMANGLE_COMPONENT_FUNCTION_TYPE
)
2634 if (! d_add_substitution (di
, mem
))
2638 return d_make_comp (di
, DEMANGLE_COMPONENT_PTRMEM_TYPE
, cl
, mem
);
2641 /* <non-negative number> _ */
2644 d_compact_number (struct d_info
*di
)
2647 if (d_peek_char (di
) == '_')
2649 else if (d_peek_char (di
) == 'n')
2652 num
= d_number (di
) + 1;
2654 if (! d_check_char (di
, '_'))
2659 /* <template-param> ::= T_
2660 ::= T <(parameter-2 non-negative) number> _
2663 static struct demangle_component
*
2664 d_template_param (struct d_info
*di
)
2668 if (! d_check_char (di
, 'T'))
2671 param
= d_compact_number (di
);
2677 return d_make_template_param (di
, param
);
2680 /* <template-args> ::= I <template-arg>+ E */
2682 static struct demangle_component
*
2683 d_template_args (struct d_info
*di
)
2685 struct demangle_component
*hold_last_name
;
2686 struct demangle_component
*al
;
2687 struct demangle_component
**pal
;
2689 /* Preserve the last name we saw--don't let the template arguments
2690 clobber it, as that would give us the wrong name for a subsequent
2691 constructor or destructor. */
2692 hold_last_name
= di
->last_name
;
2694 if (d_peek_char (di
) != 'I'
2695 && d_peek_char (di
) != 'J')
2699 if (d_peek_char (di
) == 'E')
2701 /* An argument pack can be empty. */
2703 return d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
, NULL
, NULL
);
2710 struct demangle_component
*a
;
2712 a
= d_template_arg (di
);
2716 *pal
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
, a
, NULL
);
2719 pal
= &d_right (*pal
);
2721 if (d_peek_char (di
) == 'E')
2728 di
->last_name
= hold_last_name
;
2733 /* <template-arg> ::= <type>
2734 ::= X <expression> E
2738 static struct demangle_component
*
2739 d_template_arg (struct d_info
*di
)
2741 struct demangle_component
*ret
;
2743 switch (d_peek_char (di
))
2747 ret
= d_expression (di
);
2748 if (! d_check_char (di
, 'E'))
2753 return d_expr_primary (di
);
2757 /* An argument pack. */
2758 return d_template_args (di
);
2761 return cplus_demangle_type (di
);
2765 /* Parse a sequence of expressions until we hit the terminator
2768 static struct demangle_component
*
2769 d_exprlist (struct d_info
*di
, char terminator
)
2771 struct demangle_component
*list
= NULL
;
2772 struct demangle_component
**p
= &list
;
2774 if (d_peek_char (di
) == terminator
)
2777 return d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, NULL
, NULL
);
2782 struct demangle_component
*arg
= d_expression (di
);
2786 *p
= d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, arg
, NULL
);
2791 if (d_peek_char (di
) == terminator
)
2801 /* <expression> ::= <(unary) operator-name> <expression>
2802 ::= <(binary) operator-name> <expression> <expression>
2803 ::= <(trinary) operator-name> <expression> <expression> <expression>
2804 ::= cl <expression>+ E
2806 ::= <template-param>
2807 ::= sr <type> <unqualified-name>
2808 ::= sr <type> <unqualified-name> <template-args>
2812 static struct demangle_component
*
2813 d_expression (struct d_info
*di
)
2817 peek
= d_peek_char (di
);
2819 return d_expr_primary (di
);
2820 else if (peek
== 'T')
2821 return d_template_param (di
);
2822 else if (peek
== 's' && d_peek_next_char (di
) == 'r')
2824 struct demangle_component
*type
;
2825 struct demangle_component
*name
;
2828 type
= cplus_demangle_type (di
);
2829 name
= d_unqualified_name (di
);
2830 if (d_peek_char (di
) != 'I')
2831 return d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
, type
, name
);
2833 return d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
, type
,
2834 d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, name
,
2835 d_template_args (di
)));
2837 else if (peek
== 's' && d_peek_next_char (di
) == 'p')
2840 return d_make_comp (di
, DEMANGLE_COMPONENT_PACK_EXPANSION
,
2841 d_expression (di
), NULL
);
2843 else if (peek
== 'f' && d_peek_next_char (di
) == 'p')
2845 /* Function parameter used in a late-specified return type. */
2848 if (d_peek_char (di
) == 'T')
2850 /* 'this' parameter. */
2856 index
= d_compact_number (di
) + 1;
2860 return d_make_function_param (di
, index
);
2862 else if (IS_DIGIT (peek
)
2863 || (peek
== 'o' && d_peek_next_char (di
) == 'n'))
2865 /* We can get an unqualified name as an expression in the case of
2866 a dependent function call, i.e. decltype(f(t)). */
2867 struct demangle_component
*name
;
2870 /* operator-function-id, i.e. operator+(t). */
2873 name
= d_unqualified_name (di
);
2876 if (d_peek_char (di
) == 'I')
2877 return d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, name
,
2878 d_template_args (di
));
2882 else if ((peek
== 'i' || peek
== 't')
2883 && d_peek_next_char (di
) == 'l')
2885 /* Brace-enclosed initializer list, untyped or typed. */
2886 struct demangle_component
*type
= NULL
;
2888 type
= cplus_demangle_type (di
);
2890 return d_make_comp (di
, DEMANGLE_COMPONENT_INITIALIZER_LIST
,
2891 type
, d_exprlist (di
, 'E'));
2895 struct demangle_component
*op
;
2896 const char *code
= NULL
;
2899 op
= d_operator_name (di
);
2903 if (op
->type
== DEMANGLE_COMPONENT_OPERATOR
)
2905 code
= op
->u
.s_operator
.op
->code
;
2906 di
->expansion
+= op
->u
.s_operator
.op
->len
- 2;
2907 if (strcmp (code
, "st") == 0)
2908 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
2909 cplus_demangle_type (di
));
2916 case DEMANGLE_COMPONENT_OPERATOR
:
2917 args
= op
->u
.s_operator
.op
->args
;
2919 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
2920 args
= op
->u
.s_extended_operator
.args
;
2922 case DEMANGLE_COMPONENT_CAST
:
2930 return d_make_comp (di
, DEMANGLE_COMPONENT_NULLARY
, op
, NULL
);
2934 struct demangle_component
*operand
;
2937 if (code
&& (code
[0] == 'p' || code
[0] == 'm')
2938 && code
[1] == code
[0])
2939 /* pp_ and mm_ are the prefix variants. */
2940 suffix
= !d_check_char (di
, '_');
2942 if (op
->type
== DEMANGLE_COMPONENT_CAST
2943 && d_check_char (di
, '_'))
2944 operand
= d_exprlist (di
, 'E');
2946 operand
= d_expression (di
);
2949 /* Indicate the suffix variant for d_print_comp. */
2950 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
2952 DEMANGLE_COMPONENT_BINARY_ARGS
,
2955 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
2960 struct demangle_component
*left
;
2961 struct demangle_component
*right
;
2963 left
= d_expression (di
);
2964 if (!strcmp (code
, "cl"))
2965 right
= d_exprlist (di
, 'E');
2966 else if (!strcmp (code
, "dt") || !strcmp (code
, "pt"))
2968 right
= d_unqualified_name (di
);
2969 if (d_peek_char (di
) == 'I')
2970 right
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
,
2971 right
, d_template_args (di
));
2974 right
= d_expression (di
);
2976 return d_make_comp (di
, DEMANGLE_COMPONENT_BINARY
, op
,
2978 DEMANGLE_COMPONENT_BINARY_ARGS
,
2983 struct demangle_component
*first
;
2984 struct demangle_component
*second
;
2985 struct demangle_component
*third
;
2987 if (!strcmp (code
, "qu"))
2989 /* ?: expression. */
2990 first
= d_expression (di
);
2991 second
= d_expression (di
);
2992 third
= d_expression (di
);
2994 else if (code
[0] == 'n')
2996 /* new-expression. */
2997 if (code
[1] != 'w' && code
[1] != 'a')
2999 first
= d_exprlist (di
, '_');
3000 second
= cplus_demangle_type (di
);
3001 if (d_peek_char (di
) == 'E')
3006 else if (d_peek_char (di
) == 'p'
3007 && d_peek_next_char (di
) == 'i')
3009 /* Parenthesized initializer. */
3011 third
= d_exprlist (di
, 'E');
3013 else if (d_peek_char (di
) == 'i'
3014 && d_peek_next_char (di
) == 'l')
3015 /* initializer-list. */
3016 third
= d_expression (di
);
3022 return d_make_comp (di
, DEMANGLE_COMPONENT_TRINARY
, op
,
3024 DEMANGLE_COMPONENT_TRINARY_ARG1
,
3027 DEMANGLE_COMPONENT_TRINARY_ARG2
,
3036 /* <expr-primary> ::= L <type> <(value) number> E
3037 ::= L <type> <(value) float> E
3038 ::= L <mangled-name> E
3041 static struct demangle_component
*
3042 d_expr_primary (struct d_info
*di
)
3044 struct demangle_component
*ret
;
3046 if (! d_check_char (di
, 'L'))
3048 if (d_peek_char (di
) == '_'
3049 /* Workaround for G++ bug; see comment in write_template_arg. */
3050 || d_peek_char (di
) == 'Z')
3051 ret
= cplus_demangle_mangled_name (di
, 0);
3054 struct demangle_component
*type
;
3055 enum demangle_component_type t
;
3058 type
= cplus_demangle_type (di
);
3062 /* If we have a type we know how to print, we aren't going to
3063 print the type name itself. */
3064 if (type
->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
3065 && type
->u
.s_builtin
.type
->print
!= D_PRINT_DEFAULT
)
3066 di
->expansion
-= type
->u
.s_builtin
.type
->len
;
3068 /* Rather than try to interpret the literal value, we just
3069 collect it as a string. Note that it's possible to have a
3070 floating point literal here. The ABI specifies that the
3071 format of such literals is machine independent. That's fine,
3072 but what's not fine is that versions of g++ up to 3.2 with
3073 -fabi-version=1 used upper case letters in the hex constant,
3074 and dumped out gcc's internal representation. That makes it
3075 hard to tell where the constant ends, and hard to dump the
3076 constant in any readable form anyhow. We don't attempt to
3077 handle these cases. */
3079 t
= DEMANGLE_COMPONENT_LITERAL
;
3080 if (d_peek_char (di
) == 'n')
3082 t
= DEMANGLE_COMPONENT_LITERAL_NEG
;
3086 while (d_peek_char (di
) != 'E')
3088 if (d_peek_char (di
) == '\0')
3092 ret
= d_make_comp (di
, t
, type
, d_make_name (di
, s
, d_str (di
) - s
));
3094 if (! d_check_char (di
, 'E'))
3099 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
3100 ::= Z <(function) encoding> E s [<discriminator>]
3103 static struct demangle_component
*
3104 d_local_name (struct d_info
*di
)
3106 struct demangle_component
*function
;
3108 if (! d_check_char (di
, 'Z'))
3111 function
= d_encoding (di
, 0);
3113 if (! d_check_char (di
, 'E'))
3116 if (d_peek_char (di
) == 's')
3119 if (! d_discriminator (di
))
3121 return d_make_comp (di
, DEMANGLE_COMPONENT_LOCAL_NAME
, function
,
3122 d_make_name (di
, "string literal",
3123 sizeof "string literal" - 1));
3127 struct demangle_component
*name
;
3130 if (d_peek_char (di
) == 'd')
3132 /* Default argument scope: d <number> _. */
3134 num
= d_compact_number (di
);
3143 /* Lambdas and unnamed types have internal discriminators. */
3144 case DEMANGLE_COMPONENT_LAMBDA
:
3145 case DEMANGLE_COMPONENT_UNNAMED_TYPE
:
3148 if (! d_discriminator (di
))
3152 name
= d_make_default_arg (di
, num
, name
);
3153 return d_make_comp (di
, DEMANGLE_COMPONENT_LOCAL_NAME
, function
, name
);
3157 /* <discriminator> ::= _ <(non-negative) number>
3159 We demangle the discriminator, but we don't print it out. FIXME:
3160 We should print it out in verbose mode. */
3163 d_discriminator (struct d_info
*di
)
3167 if (d_peek_char (di
) != '_')
3170 discrim
= d_number (di
);
3176 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ */
3178 static struct demangle_component
*
3179 d_lambda (struct d_info
*di
)
3181 struct demangle_component
*tl
;
3182 struct demangle_component
*ret
;
3185 if (! d_check_char (di
, 'U'))
3187 if (! d_check_char (di
, 'l'))
3190 tl
= d_parmlist (di
);
3194 if (! d_check_char (di
, 'E'))
3197 num
= d_compact_number (di
);
3201 ret
= d_make_empty (di
);
3204 ret
->type
= DEMANGLE_COMPONENT_LAMBDA
;
3205 ret
->u
.s_unary_num
.sub
= tl
;
3206 ret
->u
.s_unary_num
.num
= num
;
3209 if (! d_add_substitution (di
, ret
))
3215 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
3217 static struct demangle_component
*
3218 d_unnamed_type (struct d_info
*di
)
3220 struct demangle_component
*ret
;
3223 if (! d_check_char (di
, 'U'))
3225 if (! d_check_char (di
, 't'))
3228 num
= d_compact_number (di
);
3232 ret
= d_make_empty (di
);
3235 ret
->type
= DEMANGLE_COMPONENT_UNNAMED_TYPE
;
3236 ret
->u
.s_number
.number
= num
;
3239 if (! d_add_substitution (di
, ret
))
3245 /* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]*
3248 static struct demangle_component
*
3249 d_clone_suffix (struct d_info
*di
, struct demangle_component
*encoding
)
3251 const char *suffix
= d_str (di
);
3252 const char *pend
= suffix
;
3253 struct demangle_component
*n
;
3255 if (*pend
== '.' && (IS_LOWER (pend
[1]) || pend
[1] == '_'))
3258 while (IS_LOWER (*pend
) || *pend
== '_')
3261 while (*pend
== '.' && IS_DIGIT (pend
[1]))
3264 while (IS_DIGIT (*pend
))
3267 d_advance (di
, pend
- suffix
);
3268 n
= d_make_name (di
, suffix
, pend
- suffix
);
3269 return d_make_comp (di
, DEMANGLE_COMPONENT_CLONE
, encoding
, n
);
3272 /* Add a new substitution. */
3275 d_add_substitution (struct d_info
*di
, struct demangle_component
*dc
)
3279 if (di
->next_sub
>= di
->num_subs
)
3281 di
->subs
[di
->next_sub
] = dc
;
3286 /* <substitution> ::= S <seq-id> _
3296 If PREFIX is non-zero, then this type is being used as a prefix in
3297 a qualified name. In this case, for the standard substitutions, we
3298 need to check whether we are being used as a prefix for a
3299 constructor or destructor, and return a full template name.
3300 Otherwise we will get something like std::iostream::~iostream()
3301 which does not correspond particularly well to any function which
3302 actually appears in the source.
3305 static const struct d_standard_sub_info standard_subs
[] =
3310 { 'a', NL ("std::allocator"),
3311 NL ("std::allocator"),
3313 { 'b', NL ("std::basic_string"),
3314 NL ("std::basic_string"),
3315 NL ("basic_string") },
3316 { 's', NL ("std::string"),
3317 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
3318 NL ("basic_string") },
3319 { 'i', NL ("std::istream"),
3320 NL ("std::basic_istream<char, std::char_traits<char> >"),
3321 NL ("basic_istream") },
3322 { 'o', NL ("std::ostream"),
3323 NL ("std::basic_ostream<char, std::char_traits<char> >"),
3324 NL ("basic_ostream") },
3325 { 'd', NL ("std::iostream"),
3326 NL ("std::basic_iostream<char, std::char_traits<char> >"),
3327 NL ("basic_iostream") }
3330 static struct demangle_component
*
3331 d_substitution (struct d_info
*di
, int prefix
)
3335 if (! d_check_char (di
, 'S'))
3338 c
= d_next_char (di
);
3339 if (c
== '_' || IS_DIGIT (c
) || IS_UPPER (c
))
3348 unsigned int new_id
;
3351 new_id
= id
* 36 + c
- '0';
3352 else if (IS_UPPER (c
))
3353 new_id
= id
* 36 + c
- 'A' + 10;
3359 c
= d_next_char (di
);
3366 if (id
>= (unsigned int) di
->next_sub
)
3371 return di
->subs
[id
];
3376 const struct d_standard_sub_info
*p
;
3377 const struct d_standard_sub_info
*pend
;
3379 verbose
= (di
->options
& DMGL_VERBOSE
) != 0;
3380 if (! verbose
&& prefix
)
3384 peek
= d_peek_char (di
);
3385 if (peek
== 'C' || peek
== 'D')
3389 pend
= (&standard_subs
[0]
3390 + sizeof standard_subs
/ sizeof standard_subs
[0]);
3391 for (p
= &standard_subs
[0]; p
< pend
; ++p
)
3398 if (p
->set_last_name
!= NULL
)
3399 di
->last_name
= d_make_sub (di
, p
->set_last_name
,
3400 p
->set_last_name_len
);
3403 s
= p
->full_expansion
;
3408 s
= p
->simple_expansion
;
3409 len
= p
->simple_len
;
3411 di
->expansion
+= len
;
3412 return d_make_sub (di
, s
, len
);
3420 /* Initialize a growable string. */
3423 d_growable_string_init (struct d_growable_string
*dgs
, size_t estimate
)
3428 dgs
->allocation_failure
= 0;
3431 d_growable_string_resize (dgs
, estimate
);
3434 /* Grow a growable string to a given size. */
3437 d_growable_string_resize (struct d_growable_string
*dgs
, size_t need
)
3442 if (dgs
->allocation_failure
)
3445 /* Start allocation at two bytes to avoid any possibility of confusion
3446 with the special value of 1 used as a return in *palc to indicate
3447 allocation failures. */
3448 newalc
= dgs
->alc
> 0 ? dgs
->alc
: 2;
3449 while (newalc
< need
)
3452 newbuf
= (char *) realloc (dgs
->buf
, newalc
);
3459 dgs
->allocation_failure
= 1;
3466 /* Append a buffer to a growable string. */
3469 d_growable_string_append_buffer (struct d_growable_string
*dgs
,
3470 const char *s
, size_t l
)
3474 need
= dgs
->len
+ l
+ 1;
3475 if (need
> dgs
->alc
)
3476 d_growable_string_resize (dgs
, need
);
3478 if (dgs
->allocation_failure
)
3481 memcpy (dgs
->buf
+ dgs
->len
, s
, l
);
3482 dgs
->buf
[dgs
->len
+ l
] = '\0';
3486 /* Bridge growable strings to the callback mechanism. */
3489 d_growable_string_callback_adapter (const char *s
, size_t l
, void *opaque
)
3491 struct d_growable_string
*dgs
= (struct d_growable_string
*) opaque
;
3493 d_growable_string_append_buffer (dgs
, s
, l
);
3496 /* Initialize a print information structure. */
3499 d_print_init (struct d_print_info
*dpi
, demangle_callbackref callback
,
3503 dpi
->last_char
= '\0';
3504 dpi
->templates
= NULL
;
3505 dpi
->modifiers
= NULL
;
3506 dpi
->pack_index
= 0;
3507 dpi
->flush_count
= 0;
3509 dpi
->callback
= callback
;
3510 dpi
->opaque
= opaque
;
3512 dpi
->demangle_failure
= 0;
3515 /* Indicate that an error occurred during printing, and test for error. */
3518 d_print_error (struct d_print_info
*dpi
)
3520 dpi
->demangle_failure
= 1;
3524 d_print_saw_error (struct d_print_info
*dpi
)
3526 return dpi
->demangle_failure
!= 0;
3529 /* Flush buffered characters to the callback. */
3532 d_print_flush (struct d_print_info
*dpi
)
3534 dpi
->buf
[dpi
->len
] = '\0';
3535 dpi
->callback (dpi
->buf
, dpi
->len
, dpi
->opaque
);
3540 /* Append characters and buffers for printing. */
3543 d_append_char (struct d_print_info
*dpi
, char c
)
3545 if (dpi
->len
== sizeof (dpi
->buf
) - 1)
3546 d_print_flush (dpi
);
3548 dpi
->buf
[dpi
->len
++] = c
;
3553 d_append_buffer (struct d_print_info
*dpi
, const char *s
, size_t l
)
3557 for (i
= 0; i
< l
; i
++)
3558 d_append_char (dpi
, s
[i
]);
3562 d_append_string (struct d_print_info
*dpi
, const char *s
)
3564 d_append_buffer (dpi
, s
, strlen (s
));
3568 d_append_num (struct d_print_info
*dpi
, long l
)
3571 sprintf (buf
,"%ld", l
);
3572 d_append_string (dpi
, buf
);
3576 d_last_char (struct d_print_info
*dpi
)
3578 return dpi
->last_char
;
3581 /* Turn components into a human readable string. OPTIONS is the
3582 options bits passed to the demangler. DC is the tree to print.
3583 CALLBACK is a function to call to flush demangled string segments
3584 as they fill the intermediate buffer, and OPAQUE is a generalized
3585 callback argument. On success, this returns 1. On failure,
3586 it returns 0, indicating a bad parse. It does not use heap
3587 memory to build an output string, so cannot encounter memory
3588 allocation failure. */
3590 CP_STATIC_IF_GLIBCPP_V3
3592 cplus_demangle_print_callback (int options
,
3593 const struct demangle_component
*dc
,
3594 demangle_callbackref callback
, void *opaque
)
3596 struct d_print_info dpi
;
3598 d_print_init (&dpi
, callback
, opaque
);
3600 d_print_comp (&dpi
, options
, dc
);
3602 d_print_flush (&dpi
);
3604 return ! d_print_saw_error (&dpi
);
3607 /* Turn components into a human readable string. OPTIONS is the
3608 options bits passed to the demangler. DC is the tree to print.
3609 ESTIMATE is a guess at the length of the result. This returns a
3610 string allocated by malloc, or NULL on error. On success, this
3611 sets *PALC to the size of the allocated buffer. On failure, this
3612 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
3615 CP_STATIC_IF_GLIBCPP_V3
3617 cplus_demangle_print (int options
, const struct demangle_component
*dc
,
3618 int estimate
, size_t *palc
)
3620 struct d_growable_string dgs
;
3622 d_growable_string_init (&dgs
, estimate
);
3624 if (! cplus_demangle_print_callback (options
, dc
,
3625 d_growable_string_callback_adapter
,
3633 *palc
= dgs
.allocation_failure
? 1 : dgs
.alc
;
3637 /* Returns the I'th element of the template arglist ARGS, or NULL on
3640 static struct demangle_component
*
3641 d_index_template_argument (struct demangle_component
*args
, int i
)
3643 struct demangle_component
*a
;
3649 if (a
->type
!= DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
3655 if (i
!= 0 || a
== NULL
)
3661 /* Returns the template argument from the current context indicated by DC,
3662 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
3664 static struct demangle_component
*
3665 d_lookup_template_argument (struct d_print_info
*dpi
,
3666 const struct demangle_component
*dc
)
3668 if (dpi
->templates
== NULL
)
3670 d_print_error (dpi
);
3674 return d_index_template_argument
3675 (d_right (dpi
->templates
->template_decl
),
3676 dc
->u
.s_number
.number
);
3679 /* Returns a template argument pack used in DC (any will do), or NULL. */
3681 static struct demangle_component
*
3682 d_find_pack (struct d_print_info
*dpi
,
3683 const struct demangle_component
*dc
)
3685 struct demangle_component
*a
;
3691 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
3692 a
= d_lookup_template_argument (dpi
, dc
);
3693 if (a
&& a
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
3697 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
3700 case DEMANGLE_COMPONENT_LAMBDA
:
3701 case DEMANGLE_COMPONENT_NAME
:
3702 case DEMANGLE_COMPONENT_OPERATOR
:
3703 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
3704 case DEMANGLE_COMPONENT_SUB_STD
:
3705 case DEMANGLE_COMPONENT_CHARACTER
:
3706 case DEMANGLE_COMPONENT_FUNCTION_PARAM
:
3709 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
3710 return d_find_pack (dpi
, dc
->u
.s_extended_operator
.name
);
3711 case DEMANGLE_COMPONENT_CTOR
:
3712 return d_find_pack (dpi
, dc
->u
.s_ctor
.name
);
3713 case DEMANGLE_COMPONENT_DTOR
:
3714 return d_find_pack (dpi
, dc
->u
.s_dtor
.name
);
3717 a
= d_find_pack (dpi
, d_left (dc
));
3720 return d_find_pack (dpi
, d_right (dc
));
3724 /* Returns the length of the template argument pack DC. */
3727 d_pack_length (const struct demangle_component
*dc
)
3730 while (dc
&& dc
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
3731 && d_left (dc
) != NULL
)
3739 /* DC is a component of a mangled expression. Print it, wrapped in parens
3743 d_print_subexpr (struct d_print_info
*dpi
, int options
,
3744 const struct demangle_component
*dc
)
3747 if (dc
->type
== DEMANGLE_COMPONENT_NAME
3748 || dc
->type
== DEMANGLE_COMPONENT_QUAL_NAME
3749 || dc
->type
== DEMANGLE_COMPONENT_INITIALIZER_LIST
3750 || dc
->type
== DEMANGLE_COMPONENT_FUNCTION_PARAM
)
3753 d_append_char (dpi
, '(');
3754 d_print_comp (dpi
, options
, dc
);
3756 d_append_char (dpi
, ')');
3759 /* Subroutine to handle components. */
3762 d_print_comp (struct d_print_info
*dpi
, int options
,
3763 const struct demangle_component
*dc
)
3765 /* Magic variable to let reference smashing skip over the next modifier
3766 without needing to modify *dc. */
3767 const struct demangle_component
*mod_inner
= NULL
;
3771 d_print_error (dpi
);
3774 if (d_print_saw_error (dpi
))
3779 case DEMANGLE_COMPONENT_NAME
:
3780 if ((options
& DMGL_JAVA
) == 0)
3781 d_append_buffer (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
3783 d_print_java_identifier (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
3786 case DEMANGLE_COMPONENT_QUAL_NAME
:
3787 case DEMANGLE_COMPONENT_LOCAL_NAME
:
3788 d_print_comp (dpi
, options
, d_left (dc
));
3789 if ((options
& DMGL_JAVA
) == 0)
3790 d_append_string (dpi
, "::");
3792 d_append_char (dpi
, '.');
3793 d_print_comp (dpi
, options
, d_right (dc
));
3796 case DEMANGLE_COMPONENT_TYPED_NAME
:
3798 struct d_print_mod
*hold_modifiers
;
3799 struct demangle_component
*typed_name
;
3800 struct d_print_mod adpm
[4];
3802 struct d_print_template dpt
;
3804 /* Pass the name down to the type so that it can be printed in
3805 the right place for the type. We also have to pass down
3806 any CV-qualifiers, which apply to the this parameter. */
3807 hold_modifiers
= dpi
->modifiers
;
3810 typed_name
= d_left (dc
);
3811 while (typed_name
!= NULL
)
3813 if (i
>= sizeof adpm
/ sizeof adpm
[0])
3815 d_print_error (dpi
);
3819 adpm
[i
].next
= dpi
->modifiers
;
3820 dpi
->modifiers
= &adpm
[i
];
3821 adpm
[i
].mod
= typed_name
;
3822 adpm
[i
].printed
= 0;
3823 adpm
[i
].templates
= dpi
->templates
;
3826 if (typed_name
->type
!= DEMANGLE_COMPONENT_RESTRICT_THIS
3827 && typed_name
->type
!= DEMANGLE_COMPONENT_VOLATILE_THIS
3828 && typed_name
->type
!= DEMANGLE_COMPONENT_CONST_THIS
)
3831 typed_name
= d_left (typed_name
);
3834 if (typed_name
== NULL
)
3836 d_print_error (dpi
);
3840 /* If typed_name is a template, then it applies to the
3841 function type as well. */
3842 if (typed_name
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
3844 dpt
.next
= dpi
->templates
;
3845 dpi
->templates
= &dpt
;
3846 dpt
.template_decl
= typed_name
;
3849 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
3850 there may be CV-qualifiers on its right argument which
3851 really apply here; this happens when parsing a class which
3852 is local to a function. */
3853 if (typed_name
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
3855 struct demangle_component
*local_name
;
3857 local_name
= d_right (typed_name
);
3858 if (local_name
->type
== DEMANGLE_COMPONENT_DEFAULT_ARG
)
3859 local_name
= local_name
->u
.s_unary_num
.sub
;
3860 while (local_name
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
3861 || local_name
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
3862 || local_name
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
3864 if (i
>= sizeof adpm
/ sizeof adpm
[0])
3866 d_print_error (dpi
);
3870 adpm
[i
] = adpm
[i
- 1];
3871 adpm
[i
].next
= &adpm
[i
- 1];
3872 dpi
->modifiers
= &adpm
[i
];
3874 adpm
[i
- 1].mod
= local_name
;
3875 adpm
[i
- 1].printed
= 0;
3876 adpm
[i
- 1].templates
= dpi
->templates
;
3879 local_name
= d_left (local_name
);
3883 d_print_comp (dpi
, options
, d_right (dc
));
3885 if (typed_name
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
3886 dpi
->templates
= dpt
.next
;
3888 /* If the modifiers didn't get printed by the type, print them
3893 if (! adpm
[i
].printed
)
3895 d_append_char (dpi
, ' ');
3896 d_print_mod (dpi
, options
, adpm
[i
].mod
);
3900 dpi
->modifiers
= hold_modifiers
;
3905 case DEMANGLE_COMPONENT_TEMPLATE
:
3907 struct d_print_mod
*hold_dpm
;
3908 struct demangle_component
*dcl
;
3910 /* Don't push modifiers into a template definition. Doing so
3911 could give the wrong definition for a template argument.
3912 Instead, treat the template essentially as a name. */
3914 hold_dpm
= dpi
->modifiers
;
3915 dpi
->modifiers
= NULL
;
3919 if ((options
& DMGL_JAVA
) != 0
3920 && dcl
->type
== DEMANGLE_COMPONENT_NAME
3921 && dcl
->u
.s_name
.len
== 6
3922 && strncmp (dcl
->u
.s_name
.s
, "JArray", 6) == 0)
3924 /* Special-case Java arrays, so that JArray<TYPE> appears
3925 instead as TYPE[]. */
3927 d_print_comp (dpi
, options
, d_right (dc
));
3928 d_append_string (dpi
, "[]");
3932 d_print_comp (dpi
, options
, dcl
);
3933 if (d_last_char (dpi
) == '<')
3934 d_append_char (dpi
, ' ');
3935 d_append_char (dpi
, '<');
3936 d_print_comp (dpi
, options
, d_right (dc
));
3937 /* Avoid generating two consecutive '>' characters, to avoid
3938 the C++ syntactic ambiguity. */
3939 if (d_last_char (dpi
) == '>')
3940 d_append_char (dpi
, ' ');
3941 d_append_char (dpi
, '>');
3944 dpi
->modifiers
= hold_dpm
;
3949 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
3951 struct d_print_template
*hold_dpt
;
3952 struct demangle_component
*a
= d_lookup_template_argument (dpi
, dc
);
3954 if (a
&& a
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
3955 a
= d_index_template_argument (a
, dpi
->pack_index
);
3959 d_print_error (dpi
);
3963 /* While processing this parameter, we need to pop the list of
3964 templates. This is because the template parameter may
3965 itself be a reference to a parameter of an outer
3968 hold_dpt
= dpi
->templates
;
3969 dpi
->templates
= hold_dpt
->next
;
3971 d_print_comp (dpi
, options
, a
);
3973 dpi
->templates
= hold_dpt
;
3978 case DEMANGLE_COMPONENT_CTOR
:
3979 d_print_comp (dpi
, options
, dc
->u
.s_ctor
.name
);
3982 case DEMANGLE_COMPONENT_DTOR
:
3983 d_append_char (dpi
, '~');
3984 d_print_comp (dpi
, options
, dc
->u
.s_dtor
.name
);
3987 case DEMANGLE_COMPONENT_VTABLE
:
3988 d_append_string (dpi
, "vtable for ");
3989 d_print_comp (dpi
, options
, d_left (dc
));
3992 case DEMANGLE_COMPONENT_VTT
:
3993 d_append_string (dpi
, "VTT for ");
3994 d_print_comp (dpi
, options
, d_left (dc
));
3997 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
3998 d_append_string (dpi
, "construction vtable for ");
3999 d_print_comp (dpi
, options
, d_left (dc
));
4000 d_append_string (dpi
, "-in-");
4001 d_print_comp (dpi
, options
, d_right (dc
));
4004 case DEMANGLE_COMPONENT_TYPEINFO
:
4005 d_append_string (dpi
, "typeinfo for ");
4006 d_print_comp (dpi
, options
, d_left (dc
));
4009 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
4010 d_append_string (dpi
, "typeinfo name for ");
4011 d_print_comp (dpi
, options
, d_left (dc
));
4014 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
4015 d_append_string (dpi
, "typeinfo fn for ");
4016 d_print_comp (dpi
, options
, d_left (dc
));
4019 case DEMANGLE_COMPONENT_THUNK
:
4020 d_append_string (dpi
, "non-virtual thunk to ");
4021 d_print_comp (dpi
, options
, d_left (dc
));
4024 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
4025 d_append_string (dpi
, "virtual thunk to ");
4026 d_print_comp (dpi
, options
, d_left (dc
));
4029 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
4030 d_append_string (dpi
, "covariant return thunk to ");
4031 d_print_comp (dpi
, options
, d_left (dc
));
4034 case DEMANGLE_COMPONENT_JAVA_CLASS
:
4035 d_append_string (dpi
, "java Class for ");
4036 d_print_comp (dpi
, options
, d_left (dc
));
4039 case DEMANGLE_COMPONENT_GUARD
:
4040 d_append_string (dpi
, "guard variable for ");
4041 d_print_comp (dpi
, options
, d_left (dc
));
4044 case DEMANGLE_COMPONENT_REFTEMP
:
4045 d_append_string (dpi
, "reference temporary #");
4046 d_print_comp (dpi
, options
, d_right (dc
));
4047 d_append_string (dpi
, " for ");
4048 d_print_comp (dpi
, options
, d_left (dc
));
4051 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
4052 d_append_string (dpi
, "hidden alias for ");
4053 d_print_comp (dpi
, options
, d_left (dc
));
4056 case DEMANGLE_COMPONENT_TRANSACTION_CLONE
:
4057 d_append_string (dpi
, "transaction clone for ");
4058 d_print_comp (dpi
, options
, d_left (dc
));
4061 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
:
4062 d_append_string (dpi
, "non-transaction clone for ");
4063 d_print_comp (dpi
, options
, d_left (dc
));
4066 case DEMANGLE_COMPONENT_SUB_STD
:
4067 d_append_buffer (dpi
, dc
->u
.s_string
.string
, dc
->u
.s_string
.len
);
4070 case DEMANGLE_COMPONENT_RESTRICT
:
4071 case DEMANGLE_COMPONENT_VOLATILE
:
4072 case DEMANGLE_COMPONENT_CONST
:
4074 struct d_print_mod
*pdpm
;
4076 /* When printing arrays, it's possible to have cases where the
4077 same CV-qualifier gets pushed on the stack multiple times.
4078 We only need to print it once. */
4080 for (pdpm
= dpi
->modifiers
; pdpm
!= NULL
; pdpm
= pdpm
->next
)
4082 if (! pdpm
->printed
)
4084 if (pdpm
->mod
->type
!= DEMANGLE_COMPONENT_RESTRICT
4085 && pdpm
->mod
->type
!= DEMANGLE_COMPONENT_VOLATILE
4086 && pdpm
->mod
->type
!= DEMANGLE_COMPONENT_CONST
)
4088 if (pdpm
->mod
->type
== dc
->type
)
4090 d_print_comp (dpi
, options
, d_left (dc
));
4098 case DEMANGLE_COMPONENT_REFERENCE
:
4099 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
4101 /* Handle reference smashing: & + && = &. */
4102 const struct demangle_component
*sub
= d_left (dc
);
4103 if (sub
->type
== DEMANGLE_COMPONENT_TEMPLATE_PARAM
)
4105 struct demangle_component
*a
= d_lookup_template_argument (dpi
, sub
);
4106 if (a
&& a
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
4107 a
= d_index_template_argument (a
, dpi
->pack_index
);
4111 d_print_error (dpi
);
4118 if (sub
->type
== DEMANGLE_COMPONENT_REFERENCE
4119 || sub
->type
== dc
->type
)
4121 else if (sub
->type
== DEMANGLE_COMPONENT_RVALUE_REFERENCE
)
4122 mod_inner
= d_left (sub
);
4126 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
4127 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
4128 case DEMANGLE_COMPONENT_CONST_THIS
:
4129 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
4130 case DEMANGLE_COMPONENT_POINTER
:
4131 case DEMANGLE_COMPONENT_COMPLEX
:
4132 case DEMANGLE_COMPONENT_IMAGINARY
:
4135 /* We keep a list of modifiers on the stack. */
4136 struct d_print_mod dpm
;
4138 dpm
.next
= dpi
->modifiers
;
4139 dpi
->modifiers
= &dpm
;
4142 dpm
.templates
= dpi
->templates
;
4145 mod_inner
= d_left (dc
);
4147 d_print_comp (dpi
, options
, mod_inner
);
4149 /* If the modifier didn't get printed by the type, print it
4152 d_print_mod (dpi
, options
, dc
);
4154 dpi
->modifiers
= dpm
.next
;
4159 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
4160 if ((options
& DMGL_JAVA
) == 0)
4161 d_append_buffer (dpi
, dc
->u
.s_builtin
.type
->name
,
4162 dc
->u
.s_builtin
.type
->len
);
4164 d_append_buffer (dpi
, dc
->u
.s_builtin
.type
->java_name
,
4165 dc
->u
.s_builtin
.type
->java_len
);
4168 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
4169 d_print_comp (dpi
, options
, d_left (dc
));
4172 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
4174 if ((options
& DMGL_RET_POSTFIX
) != 0)
4175 d_print_function_type (dpi
,
4176 options
& ~(DMGL_RET_POSTFIX
| DMGL_RET_DROP
),
4177 dc
, dpi
->modifiers
);
4179 /* Print return type if present */
4180 if (d_left (dc
) != NULL
&& (options
& DMGL_RET_POSTFIX
) != 0)
4181 d_print_comp (dpi
, options
& ~(DMGL_RET_POSTFIX
| DMGL_RET_DROP
),
4183 else if (d_left (dc
) != NULL
&& (options
& DMGL_RET_DROP
) == 0)
4185 struct d_print_mod dpm
;
4187 /* We must pass this type down as a modifier in order to
4188 print it in the right location. */
4189 dpm
.next
= dpi
->modifiers
;
4190 dpi
->modifiers
= &dpm
;
4193 dpm
.templates
= dpi
->templates
;
4195 d_print_comp (dpi
, options
& ~(DMGL_RET_POSTFIX
| DMGL_RET_DROP
),
4198 dpi
->modifiers
= dpm
.next
;
4203 /* In standard prefix notation, there is a space between the
4204 return type and the function signature. */
4205 if ((options
& DMGL_RET_POSTFIX
) == 0)
4206 d_append_char (dpi
, ' ');
4209 if ((options
& DMGL_RET_POSTFIX
) == 0)
4210 d_print_function_type (dpi
,
4211 options
& ~(DMGL_RET_POSTFIX
| DMGL_RET_DROP
),
4212 dc
, dpi
->modifiers
);
4217 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
4219 struct d_print_mod
*hold_modifiers
;
4220 struct d_print_mod adpm
[4];
4222 struct d_print_mod
*pdpm
;
4224 /* We must pass this type down as a modifier in order to print
4225 multi-dimensional arrays correctly. If the array itself is
4226 CV-qualified, we act as though the element type were
4227 CV-qualified. We do this by copying the modifiers down
4228 rather than fiddling pointers, so that we don't wind up
4229 with a d_print_mod higher on the stack pointing into our
4230 stack frame after we return. */
4232 hold_modifiers
= dpi
->modifiers
;
4234 adpm
[0].next
= hold_modifiers
;
4235 dpi
->modifiers
= &adpm
[0];
4237 adpm
[0].printed
= 0;
4238 adpm
[0].templates
= dpi
->templates
;
4241 pdpm
= hold_modifiers
;
4243 && (pdpm
->mod
->type
== DEMANGLE_COMPONENT_RESTRICT
4244 || pdpm
->mod
->type
== DEMANGLE_COMPONENT_VOLATILE
4245 || pdpm
->mod
->type
== DEMANGLE_COMPONENT_CONST
))
4247 if (! pdpm
->printed
)
4249 if (i
>= sizeof adpm
/ sizeof adpm
[0])
4251 d_print_error (dpi
);
4256 adpm
[i
].next
= dpi
->modifiers
;
4257 dpi
->modifiers
= &adpm
[i
];
4265 d_print_comp (dpi
, options
, d_right (dc
));
4267 dpi
->modifiers
= hold_modifiers
;
4269 if (adpm
[0].printed
)
4275 d_print_mod (dpi
, options
, adpm
[i
].mod
);
4278 d_print_array_type (dpi
, options
, dc
, dpi
->modifiers
);
4283 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
4284 case DEMANGLE_COMPONENT_VECTOR_TYPE
:
4286 struct d_print_mod dpm
;
4288 dpm
.next
= dpi
->modifiers
;
4289 dpi
->modifiers
= &dpm
;
4292 dpm
.templates
= dpi
->templates
;
4294 d_print_comp (dpi
, options
, d_right (dc
));
4296 /* If the modifier didn't get printed by the type, print it
4299 d_print_mod (dpi
, options
, dc
);
4301 dpi
->modifiers
= dpm
.next
;
4306 case DEMANGLE_COMPONENT_FIXED_TYPE
:
4307 if (dc
->u
.s_fixed
.sat
)
4308 d_append_string (dpi
, "_Sat ");
4309 /* Don't print "int _Accum". */
4310 if (dc
->u
.s_fixed
.length
->u
.s_builtin
.type
4311 != &cplus_demangle_builtin_types
['i'-'a'])
4313 d_print_comp (dpi
, options
, dc
->u
.s_fixed
.length
);
4314 d_append_char (dpi
, ' ');
4316 if (dc
->u
.s_fixed
.accum
)
4317 d_append_string (dpi
, "_Accum");
4319 d_append_string (dpi
, "_Fract");
4322 case DEMANGLE_COMPONENT_ARGLIST
:
4323 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
4324 if (d_left (dc
) != NULL
)
4325 d_print_comp (dpi
, options
, d_left (dc
));
4326 if (d_right (dc
) != NULL
)
4329 unsigned long int flush_count
;
4330 /* Make sure ", " isn't flushed by d_append_string, otherwise
4331 dpi->len -= 2 wouldn't work. */
4332 if (dpi
->len
>= sizeof (dpi
->buf
) - 2)
4333 d_print_flush (dpi
);
4334 d_append_string (dpi
, ", ");
4336 flush_count
= dpi
->flush_count
;
4337 d_print_comp (dpi
, options
, d_right (dc
));
4338 /* If that didn't print anything (which can happen with empty
4339 template argument packs), remove the comma and space. */
4340 if (dpi
->flush_count
== flush_count
&& dpi
->len
== len
)
4345 case DEMANGLE_COMPONENT_INITIALIZER_LIST
:
4347 struct demangle_component
*type
= d_left (dc
);
4348 struct demangle_component
*list
= d_right (dc
);
4351 d_print_comp (dpi
, options
, type
);
4352 d_append_char (dpi
, '{');
4353 d_print_comp (dpi
, options
, list
);
4354 d_append_char (dpi
, '}');
4358 case DEMANGLE_COMPONENT_OPERATOR
:
4360 const struct demangle_operator_info
*op
= dc
->u
.s_operator
.op
;
4363 d_append_string (dpi
, "operator");
4364 /* Add a space before new/delete. */
4365 if (IS_LOWER (op
->name
[0]))
4366 d_append_char (dpi
, ' ');
4367 /* Omit a trailing space. */
4368 if (op
->name
[len
-1] == ' ')
4370 d_append_buffer (dpi
, op
->name
, len
);
4374 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
4375 d_append_string (dpi
, "operator ");
4376 d_print_comp (dpi
, options
, dc
->u
.s_extended_operator
.name
);
4379 case DEMANGLE_COMPONENT_CAST
:
4380 d_append_string (dpi
, "operator ");
4381 d_print_cast (dpi
, options
, dc
);
4384 case DEMANGLE_COMPONENT_NULLARY
:
4385 d_print_expr_op (dpi
, options
, d_left (dc
));
4388 case DEMANGLE_COMPONENT_UNARY
:
4390 struct demangle_component
*op
= d_left (dc
);
4391 struct demangle_component
*operand
= d_right (dc
);
4392 const char *code
= NULL
;
4394 if (op
->type
== DEMANGLE_COMPONENT_OPERATOR
)
4396 code
= op
->u
.s_operator
.op
->code
;
4397 if (!strcmp (code
, "ad"))
4399 /* Don't print the argument list for the address of a
4401 if (operand
->type
== DEMANGLE_COMPONENT_TYPED_NAME
4402 && d_left (operand
)->type
== DEMANGLE_COMPONENT_QUAL_NAME
4403 && d_right (operand
)->type
== DEMANGLE_COMPONENT_FUNCTION_TYPE
)
4404 operand
= d_left (operand
);
4406 if (operand
->type
== DEMANGLE_COMPONENT_BINARY_ARGS
)
4408 /* This indicates a suffix operator. */
4409 operand
= d_left (operand
);
4410 d_print_subexpr (dpi
, options
, operand
);
4411 d_print_expr_op (dpi
, options
, op
);
4416 if (op
->type
!= DEMANGLE_COMPONENT_CAST
)
4417 d_print_expr_op (dpi
, options
, op
);
4420 d_append_char (dpi
, '(');
4421 d_print_cast (dpi
, options
, op
);
4422 d_append_char (dpi
, ')');
4424 if (code
&& !strcmp (code
, "gs"))
4425 /* Avoid parens after '::'. */
4426 d_print_comp (dpi
, options
, operand
);
4427 else if (code
&& !strcmp (code
, "st"))
4428 /* Always print parens for sizeof (type). */
4430 d_append_char (dpi
, '(');
4431 d_print_comp (dpi
, options
, operand
);
4432 d_append_char (dpi
, ')');
4435 d_print_subexpr (dpi
, options
, operand
);
4439 case DEMANGLE_COMPONENT_BINARY
:
4440 if (d_right (dc
)->type
!= DEMANGLE_COMPONENT_BINARY_ARGS
)
4442 d_print_error (dpi
);
4446 /* We wrap an expression which uses the greater-than operator in
4447 an extra layer of parens so that it does not get confused
4448 with the '>' which ends the template parameters. */
4449 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_OPERATOR
4450 && d_left (dc
)->u
.s_operator
.op
->len
== 1
4451 && d_left (dc
)->u
.s_operator
.op
->name
[0] == '>')
4452 d_append_char (dpi
, '(');
4454 if (strcmp (d_left (dc
)->u
.s_operator
.op
->code
, "cl") == 0
4455 && d_left (d_right (dc
))->type
== DEMANGLE_COMPONENT_TYPED_NAME
)
4457 /* Function call used in an expression should not have printed types
4458 of the function arguments. Values of the function arguments still
4459 get printed below. */
4461 const struct demangle_component
*func
= d_left (d_right (dc
));
4463 if (d_right (func
)->type
!= DEMANGLE_COMPONENT_FUNCTION_TYPE
)
4464 d_print_error (dpi
);
4465 d_print_subexpr (dpi
, options
, d_left (func
));
4468 d_print_subexpr (dpi
, options
, d_left (d_right (dc
)));
4469 if (strcmp (d_left (dc
)->u
.s_operator
.op
->code
, "ix") == 0)
4471 d_append_char (dpi
, '[');
4472 d_print_comp (dpi
, options
, d_right (d_right (dc
)));
4473 d_append_char (dpi
, ']');
4477 if (strcmp (d_left (dc
)->u
.s_operator
.op
->code
, "cl") != 0)
4478 d_print_expr_op (dpi
, options
, d_left (dc
));
4479 d_print_subexpr (dpi
, options
, d_right (d_right (dc
)));
4482 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_OPERATOR
4483 && d_left (dc
)->u
.s_operator
.op
->len
== 1
4484 && d_left (dc
)->u
.s_operator
.op
->name
[0] == '>')
4485 d_append_char (dpi
, ')');
4489 case DEMANGLE_COMPONENT_BINARY_ARGS
:
4490 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
4491 d_print_error (dpi
);
4494 case DEMANGLE_COMPONENT_TRINARY
:
4495 if (d_right (dc
)->type
!= DEMANGLE_COMPONENT_TRINARY_ARG1
4496 || d_right (d_right (dc
))->type
!= DEMANGLE_COMPONENT_TRINARY_ARG2
)
4498 d_print_error (dpi
);
4502 struct demangle_component
*op
= d_left (dc
);
4503 struct demangle_component
*first
= d_left (d_right (dc
));
4504 struct demangle_component
*second
= d_left (d_right (d_right (dc
)));
4505 struct demangle_component
*third
= d_right (d_right (d_right (dc
)));
4507 if (!strcmp (op
->u
.s_operator
.op
->code
, "qu"))
4509 d_print_subexpr (dpi
, options
, first
);
4510 d_print_expr_op (dpi
, options
, op
);
4511 d_print_subexpr (dpi
, options
, second
);
4512 d_append_string (dpi
, " : ");
4513 d_print_subexpr (dpi
, options
, third
);
4517 d_append_string (dpi
, "new ");
4518 if (d_left (first
) != NULL
)
4520 d_print_subexpr (dpi
, options
, first
);
4521 d_append_char (dpi
, ' ');
4523 d_print_comp (dpi
, options
, second
);
4525 d_print_subexpr (dpi
, options
, third
);
4530 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
4531 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
4532 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
4533 d_print_error (dpi
);
4536 case DEMANGLE_COMPONENT_LITERAL
:
4537 case DEMANGLE_COMPONENT_LITERAL_NEG
:
4539 enum d_builtin_type_print tp
;
4541 /* For some builtin types, produce simpler output. */
4542 tp
= D_PRINT_DEFAULT
;
4543 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
)
4545 tp
= d_left (dc
)->u
.s_builtin
.type
->print
;
4549 case D_PRINT_UNSIGNED
:
4551 case D_PRINT_UNSIGNED_LONG
:
4552 case D_PRINT_LONG_LONG
:
4553 case D_PRINT_UNSIGNED_LONG_LONG
:
4554 if (d_right (dc
)->type
== DEMANGLE_COMPONENT_NAME
)
4556 if (dc
->type
== DEMANGLE_COMPONENT_LITERAL_NEG
)
4557 d_append_char (dpi
, '-');
4558 d_print_comp (dpi
, options
, d_right (dc
));
4563 case D_PRINT_UNSIGNED
:
4564 d_append_char (dpi
, 'u');
4567 d_append_char (dpi
, 'l');
4569 case D_PRINT_UNSIGNED_LONG
:
4570 d_append_string (dpi
, "ul");
4572 case D_PRINT_LONG_LONG
:
4573 d_append_string (dpi
, "ll");
4575 case D_PRINT_UNSIGNED_LONG_LONG
:
4576 d_append_string (dpi
, "ull");
4584 if (d_right (dc
)->type
== DEMANGLE_COMPONENT_NAME
4585 && d_right (dc
)->u
.s_name
.len
== 1
4586 && dc
->type
== DEMANGLE_COMPONENT_LITERAL
)
4588 switch (d_right (dc
)->u
.s_name
.s
[0])
4591 d_append_string (dpi
, "false");
4594 d_append_string (dpi
, "true");
4607 d_append_char (dpi
, '(');
4608 d_print_comp (dpi
, options
, d_left (dc
));
4609 d_append_char (dpi
, ')');
4610 if (dc
->type
== DEMANGLE_COMPONENT_LITERAL_NEG
)
4611 d_append_char (dpi
, '-');
4612 if (tp
== D_PRINT_FLOAT
)
4613 d_append_char (dpi
, '[');
4614 d_print_comp (dpi
, options
, d_right (dc
));
4615 if (tp
== D_PRINT_FLOAT
)
4616 d_append_char (dpi
, ']');
4620 case DEMANGLE_COMPONENT_NUMBER
:
4621 d_append_num (dpi
, dc
->u
.s_number
.number
);
4624 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
4625 d_append_string (dpi
, "java resource ");
4626 d_print_comp (dpi
, options
, d_left (dc
));
4629 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
4630 d_print_comp (dpi
, options
, d_left (dc
));
4631 d_print_comp (dpi
, options
, d_right (dc
));
4634 case DEMANGLE_COMPONENT_CHARACTER
:
4635 d_append_char (dpi
, dc
->u
.s_character
.character
);
4638 case DEMANGLE_COMPONENT_DECLTYPE
:
4639 d_append_string (dpi
, "decltype (");
4640 d_print_comp (dpi
, options
, d_left (dc
));
4641 d_append_char (dpi
, ')');
4644 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
4648 struct demangle_component
*a
= d_find_pack (dpi
, d_left (dc
));
4651 /* d_find_pack won't find anything if the only packs involved
4652 in this expansion are function parameter packs; in that
4653 case, just print the pattern and "...". */
4654 d_print_subexpr (dpi
, options
, d_left (dc
));
4655 d_append_string (dpi
, "...");
4659 len
= d_pack_length (a
);
4661 for (i
= 0; i
< len
; ++i
)
4663 dpi
->pack_index
= i
;
4664 d_print_comp (dpi
, options
, dc
);
4666 d_append_string (dpi
, ", ");
4671 case DEMANGLE_COMPONENT_FUNCTION_PARAM
:
4673 long num
= dc
->u
.s_number
.number
;
4675 d_append_string (dpi
, "this");
4678 d_append_string (dpi
, "{parm#");
4679 d_append_num (dpi
, num
);
4680 d_append_char (dpi
, '}');
4685 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
:
4686 d_append_string (dpi
, "global constructors keyed to ");
4687 d_print_comp (dpi
, options
, dc
->u
.s_binary
.left
);
4690 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS
:
4691 d_append_string (dpi
, "global destructors keyed to ");
4692 d_print_comp (dpi
, options
, dc
->u
.s_binary
.left
);
4695 case DEMANGLE_COMPONENT_LAMBDA
:
4696 d_append_string (dpi
, "{lambda(");
4697 d_print_comp (dpi
, options
, dc
->u
.s_unary_num
.sub
);
4698 d_append_string (dpi
, ")#");
4699 d_append_num (dpi
, dc
->u
.s_unary_num
.num
+ 1);
4700 d_append_char (dpi
, '}');
4703 case DEMANGLE_COMPONENT_UNNAMED_TYPE
:
4704 d_append_string (dpi
, "{unnamed type#");
4705 d_append_num (dpi
, dc
->u
.s_number
.number
+ 1);
4706 d_append_char (dpi
, '}');
4709 case DEMANGLE_COMPONENT_CLONE
:
4710 d_print_comp (dpi
, options
, d_left (dc
));
4711 d_append_string (dpi
, " [clone ");
4712 d_print_comp (dpi
, options
, d_right (dc
));
4713 d_append_char (dpi
, ']');
4717 d_print_error (dpi
);
4722 /* Print a Java dentifier. For Java we try to handle encoded extended
4723 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
4724 so we don't it for C++. Characters are encoded as
4728 d_print_java_identifier (struct d_print_info
*dpi
, const char *name
, int len
)
4734 for (p
= name
; p
< end
; ++p
)
4745 for (q
= p
+ 3; q
< end
; ++q
)
4751 else if (*q
>= 'A' && *q
<= 'F')
4752 dig
= *q
- 'A' + 10;
4753 else if (*q
>= 'a' && *q
<= 'f')
4754 dig
= *q
- 'a' + 10;
4760 /* If the Unicode character is larger than 256, we don't try
4761 to deal with it here. FIXME. */
4762 if (q
< end
&& *q
== '_' && c
< 256)
4764 d_append_char (dpi
, c
);
4770 d_append_char (dpi
, *p
);
4774 /* Print a list of modifiers. SUFFIX is 1 if we are printing
4775 qualifiers on this after printing a function. */
4778 d_print_mod_list (struct d_print_info
*dpi
, int options
,
4779 struct d_print_mod
*mods
, int suffix
)
4781 struct d_print_template
*hold_dpt
;
4783 if (mods
== NULL
|| d_print_saw_error (dpi
))
4788 && (mods
->mod
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
4789 || mods
->mod
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
4790 || mods
->mod
->type
== DEMANGLE_COMPONENT_CONST_THIS
)))
4792 d_print_mod_list (dpi
, options
, mods
->next
, suffix
);
4798 hold_dpt
= dpi
->templates
;
4799 dpi
->templates
= mods
->templates
;
4801 if (mods
->mod
->type
== DEMANGLE_COMPONENT_FUNCTION_TYPE
)
4803 d_print_function_type (dpi
, options
, mods
->mod
, mods
->next
);
4804 dpi
->templates
= hold_dpt
;
4807 else if (mods
->mod
->type
== DEMANGLE_COMPONENT_ARRAY_TYPE
)
4809 d_print_array_type (dpi
, options
, mods
->mod
, mods
->next
);
4810 dpi
->templates
= hold_dpt
;
4813 else if (mods
->mod
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
4815 struct d_print_mod
*hold_modifiers
;
4816 struct demangle_component
*dc
;
4818 /* When this is on the modifier stack, we have pulled any
4819 qualifiers off the right argument already. Otherwise, we
4820 print it as usual, but don't let the left argument see any
4823 hold_modifiers
= dpi
->modifiers
;
4824 dpi
->modifiers
= NULL
;
4825 d_print_comp (dpi
, options
, d_left (mods
->mod
));
4826 dpi
->modifiers
= hold_modifiers
;
4828 if ((options
& DMGL_JAVA
) == 0)
4829 d_append_string (dpi
, "::");
4831 d_append_char (dpi
, '.');
4833 dc
= d_right (mods
->mod
);
4835 if (dc
->type
== DEMANGLE_COMPONENT_DEFAULT_ARG
)
4837 d_append_string (dpi
, "{default arg#");
4838 d_append_num (dpi
, dc
->u
.s_unary_num
.num
+ 1);
4839 d_append_string (dpi
, "}::");
4840 dc
= dc
->u
.s_unary_num
.sub
;
4843 while (dc
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
4844 || dc
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
4845 || dc
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
4848 d_print_comp (dpi
, options
, dc
);
4850 dpi
->templates
= hold_dpt
;
4854 d_print_mod (dpi
, options
, mods
->mod
);
4856 dpi
->templates
= hold_dpt
;
4858 d_print_mod_list (dpi
, options
, mods
->next
, suffix
);
4861 /* Print a modifier. */
4864 d_print_mod (struct d_print_info
*dpi
, int options
,
4865 const struct demangle_component
*mod
)
4869 case DEMANGLE_COMPONENT_RESTRICT
:
4870 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
4871 d_append_string (dpi
, " restrict");
4873 case DEMANGLE_COMPONENT_VOLATILE
:
4874 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
4875 d_append_string (dpi
, " volatile");
4877 case DEMANGLE_COMPONENT_CONST
:
4878 case DEMANGLE_COMPONENT_CONST_THIS
:
4879 d_append_string (dpi
, " const");
4881 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
4882 d_append_char (dpi
, ' ');
4883 d_print_comp (dpi
, options
, d_right (mod
));
4885 case DEMANGLE_COMPONENT_POINTER
:
4886 /* There is no pointer symbol in Java. */
4887 if ((options
& DMGL_JAVA
) == 0)
4888 d_append_char (dpi
, '*');
4890 case DEMANGLE_COMPONENT_REFERENCE
:
4891 d_append_char (dpi
, '&');
4893 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
4894 d_append_string (dpi
, "&&");
4896 case DEMANGLE_COMPONENT_COMPLEX
:
4897 d_append_string (dpi
, "complex ");
4899 case DEMANGLE_COMPONENT_IMAGINARY
:
4900 d_append_string (dpi
, "imaginary ");
4902 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
4903 if (d_last_char (dpi
) != '(')
4904 d_append_char (dpi
, ' ');
4905 d_print_comp (dpi
, options
, d_left (mod
));
4906 d_append_string (dpi
, "::*");
4908 case DEMANGLE_COMPONENT_TYPED_NAME
:
4909 d_print_comp (dpi
, options
, d_left (mod
));
4911 case DEMANGLE_COMPONENT_VECTOR_TYPE
:
4912 d_append_string (dpi
, " __vector(");
4913 d_print_comp (dpi
, options
, d_left (mod
));
4914 d_append_char (dpi
, ')');
4918 /* Otherwise, we have something that won't go back on the
4919 modifier stack, so we can just print it. */
4920 d_print_comp (dpi
, options
, mod
);
4925 /* Print a function type, except for the return type. */
4928 d_print_function_type (struct d_print_info
*dpi
, int options
,
4929 const struct demangle_component
*dc
,
4930 struct d_print_mod
*mods
)
4934 struct d_print_mod
*p
;
4935 struct d_print_mod
*hold_modifiers
;
4939 for (p
= mods
; p
!= NULL
; p
= p
->next
)
4944 switch (p
->mod
->type
)
4946 case DEMANGLE_COMPONENT_POINTER
:
4947 case DEMANGLE_COMPONENT_REFERENCE
:
4948 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
4951 case DEMANGLE_COMPONENT_RESTRICT
:
4952 case DEMANGLE_COMPONENT_VOLATILE
:
4953 case DEMANGLE_COMPONENT_CONST
:
4954 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
4955 case DEMANGLE_COMPONENT_COMPLEX
:
4956 case DEMANGLE_COMPONENT_IMAGINARY
:
4957 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
4961 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
4962 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
4963 case DEMANGLE_COMPONENT_CONST_THIS
:
4976 if (d_last_char (dpi
) != '('
4977 && d_last_char (dpi
) != '*')
4980 if (need_space
&& d_last_char (dpi
) != ' ')
4981 d_append_char (dpi
, ' ');
4982 d_append_char (dpi
, '(');
4985 hold_modifiers
= dpi
->modifiers
;
4986 dpi
->modifiers
= NULL
;
4988 d_print_mod_list (dpi
, options
, mods
, 0);
4991 d_append_char (dpi
, ')');
4993 d_append_char (dpi
, '(');
4995 if (d_right (dc
) != NULL
)
4996 d_print_comp (dpi
, options
, d_right (dc
));
4998 d_append_char (dpi
, ')');
5000 d_print_mod_list (dpi
, options
, mods
, 1);
5002 dpi
->modifiers
= hold_modifiers
;
5005 /* Print an array type, except for the element type. */
5008 d_print_array_type (struct d_print_info
*dpi
, int options
,
5009 const struct demangle_component
*dc
,
5010 struct d_print_mod
*mods
)
5018 struct d_print_mod
*p
;
5021 for (p
= mods
; p
!= NULL
; p
= p
->next
)
5025 if (p
->mod
->type
== DEMANGLE_COMPONENT_ARRAY_TYPE
)
5040 d_append_string (dpi
, " (");
5042 d_print_mod_list (dpi
, options
, mods
, 0);
5045 d_append_char (dpi
, ')');
5049 d_append_char (dpi
, ' ');
5051 d_append_char (dpi
, '[');
5053 if (d_left (dc
) != NULL
)
5054 d_print_comp (dpi
, options
, d_left (dc
));
5056 d_append_char (dpi
, ']');
5059 /* Print an operator in an expression. */
5062 d_print_expr_op (struct d_print_info
*dpi
, int options
,
5063 const struct demangle_component
*dc
)
5065 if (dc
->type
== DEMANGLE_COMPONENT_OPERATOR
)
5066 d_append_buffer (dpi
, dc
->u
.s_operator
.op
->name
,
5067 dc
->u
.s_operator
.op
->len
);
5069 d_print_comp (dpi
, options
, dc
);
5075 d_print_cast (struct d_print_info
*dpi
, int options
,
5076 const struct demangle_component
*dc
)
5078 if (d_left (dc
)->type
!= DEMANGLE_COMPONENT_TEMPLATE
)
5079 d_print_comp (dpi
, options
, d_left (dc
));
5082 struct d_print_mod
*hold_dpm
;
5083 struct d_print_template dpt
;
5085 /* It appears that for a templated cast operator, we need to put
5086 the template parameters in scope for the operator name, but
5087 not for the parameters. The effect is that we need to handle
5088 the template printing here. */
5090 hold_dpm
= dpi
->modifiers
;
5091 dpi
->modifiers
= NULL
;
5093 dpt
.next
= dpi
->templates
;
5094 dpi
->templates
= &dpt
;
5095 dpt
.template_decl
= d_left (dc
);
5097 d_print_comp (dpi
, options
, d_left (d_left (dc
)));
5099 dpi
->templates
= dpt
.next
;
5101 if (d_last_char (dpi
) == '<')
5102 d_append_char (dpi
, ' ');
5103 d_append_char (dpi
, '<');
5104 d_print_comp (dpi
, options
, d_right (d_left (dc
)));
5105 /* Avoid generating two consecutive '>' characters, to avoid
5106 the C++ syntactic ambiguity. */
5107 if (d_last_char (dpi
) == '>')
5108 d_append_char (dpi
, ' ');
5109 d_append_char (dpi
, '>');
5111 dpi
->modifiers
= hold_dpm
;
5115 /* Initialize the information structure we use to pass around
5118 CP_STATIC_IF_GLIBCPP_V3
5120 cplus_demangle_init_info (const char *mangled
, int options
, size_t len
,
5124 di
->send
= mangled
+ len
;
5125 di
->options
= options
;
5129 /* We can not need more components than twice the number of chars in
5130 the mangled string. Most components correspond directly to
5131 chars, but the ARGLIST types are exceptions. */
5132 di
->num_comps
= 2 * len
;
5135 /* Similarly, we can not need more substitutions than there are
5136 chars in the mangled string. */
5141 di
->last_name
= NULL
;
5146 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
5147 mangled name, return strings in repeated callback giving the demangled
5148 name. OPTIONS is the usual libiberty demangler options. On success,
5149 this returns 1. On failure, returns 0. */
5152 d_demangle_callback (const char *mangled
, int options
,
5153 demangle_callbackref callback
, void *opaque
)
5164 struct demangle_component
*dc
;
5167 if (mangled
[0] == '_' && mangled
[1] == 'Z')
5169 else if (strncmp (mangled
, "_GLOBAL_", 8) == 0
5170 && (mangled
[8] == '.' || mangled
[8] == '_' || mangled
[8] == '$')
5171 && (mangled
[9] == 'D' || mangled
[9] == 'I')
5172 && mangled
[10] == '_')
5173 type
= mangled
[9] == 'I' ? DCT_GLOBAL_CTORS
: DCT_GLOBAL_DTORS
;
5176 if ((options
& DMGL_TYPES
) == 0)
5181 cplus_demangle_init_info (mangled
, options
, strlen (mangled
), &di
);
5184 #ifdef CP_DYNAMIC_ARRAYS
5185 __extension__
struct demangle_component comps
[di
.num_comps
];
5186 __extension__
struct demangle_component
*subs
[di
.num_subs
];
5191 di
.comps
= alloca (di
.num_comps
* sizeof (*di
.comps
));
5192 di
.subs
= alloca (di
.num_subs
* sizeof (*di
.subs
));
5198 dc
= cplus_demangle_type (&di
);
5201 dc
= cplus_demangle_mangled_name (&di
, 1);
5203 case DCT_GLOBAL_CTORS
:
5204 case DCT_GLOBAL_DTORS
:
5205 d_advance (&di
, 11);
5206 dc
= d_make_comp (&di
,
5207 (type
== DCT_GLOBAL_CTORS
5208 ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
5209 : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS
),
5210 d_make_demangle_mangled_name (&di
, d_str (&di
)),
5212 d_advance (&di
, strlen (d_str (&di
)));
5216 /* If DMGL_PARAMS is set, then if we didn't consume the entire
5217 mangled string, then we didn't successfully demangle it. If
5218 DMGL_PARAMS is not set, we didn't look at the trailing
5220 if (((options
& DMGL_PARAMS
) != 0) && d_peek_char (&di
) != '\0')
5223 #ifdef CP_DEMANGLE_DEBUG
5227 status
= (dc
!= NULL
)
5228 ? cplus_demangle_print_callback (options
, dc
, callback
, opaque
)
5235 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
5236 name, return a buffer allocated with malloc holding the demangled
5237 name. OPTIONS is the usual libiberty demangler options. On
5238 success, this sets *PALC to the allocated size of the returned
5239 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
5240 a memory allocation failure, and returns NULL. */
5243 d_demangle (const char *mangled
, int options
, size_t *palc
)
5245 struct d_growable_string dgs
;
5248 d_growable_string_init (&dgs
, 0);
5250 status
= d_demangle_callback (mangled
, options
,
5251 d_growable_string_callback_adapter
, &dgs
);
5259 *palc
= dgs
.allocation_failure
? 1 : dgs
.alc
;
5263 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
5265 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
5267 /* ia64 ABI-mandated entry point in the C++ runtime library for
5268 performing demangling. MANGLED_NAME is a NUL-terminated character
5269 string containing the name to be demangled.
5271 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
5272 *LENGTH bytes, into which the demangled name is stored. If
5273 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
5274 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
5275 is placed in a region of memory allocated with malloc.
5277 If LENGTH is non-NULL, the length of the buffer containing the
5278 demangled name, is placed in *LENGTH.
5280 The return value is a pointer to the start of the NUL-terminated
5281 demangled name, or NULL if the demangling fails. The caller is
5282 responsible for deallocating this memory using free.
5284 *STATUS is set to one of the following values:
5285 0: The demangling operation succeeded.
5286 -1: A memory allocation failure occurred.
5287 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
5288 -3: One of the arguments is invalid.
5290 The demangling is performed using the C++ ABI mangling rules, with
5294 __cxa_demangle (const char *mangled_name
, char *output_buffer
,
5295 size_t *length
, int *status
)
5300 if (mangled_name
== NULL
)
5307 if (output_buffer
!= NULL
&& length
== NULL
)
5314 demangled
= d_demangle (mangled_name
, DMGL_PARAMS
| DMGL_TYPES
, &alc
);
5316 if (demangled
== NULL
)
5328 if (output_buffer
== NULL
)
5335 if (strlen (demangled
) < *length
)
5337 strcpy (output_buffer
, demangled
);
5339 demangled
= output_buffer
;
5343 free (output_buffer
);
5354 extern int __gcclibcxx_demangle_callback (const char *,
5356 (const char *, size_t, void *),
5359 /* Alternative, allocationless entry point in the C++ runtime library
5360 for performing demangling. MANGLED_NAME is a NUL-terminated character
5361 string containing the name to be demangled.
5363 CALLBACK is a callback function, called with demangled string
5364 segments as demangling progresses; it is called at least once,
5365 but may be called more than once. OPAQUE is a generalized pointer
5366 used as a callback argument.
5368 The return code is one of the following values, equivalent to
5369 the STATUS values of __cxa_demangle() (excluding -1, since this
5370 function performs no memory allocations):
5371 0: The demangling operation succeeded.
5372 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
5373 -3: One of the arguments is invalid.
5375 The demangling is performed using the C++ ABI mangling rules, with
5379 __gcclibcxx_demangle_callback (const char *mangled_name
,
5380 void (*callback
) (const char *, size_t, void *),
5385 if (mangled_name
== NULL
|| callback
== NULL
)
5388 status
= d_demangle_callback (mangled_name
, DMGL_PARAMS
| DMGL_TYPES
,
5396 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
5398 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
5399 mangled name, return a buffer allocated with malloc holding the
5400 demangled name. Otherwise, return NULL. */
5403 cplus_demangle_v3 (const char *mangled
, int options
)
5407 return d_demangle (mangled
, options
, &alc
);
5411 cplus_demangle_v3_callback (const char *mangled
, int options
,
5412 demangle_callbackref callback
, void *opaque
)
5414 return d_demangle_callback (mangled
, options
, callback
, opaque
);
5417 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
5418 conventions, but the output formatting is a little different.
5419 This instructs the C++ demangler not to emit pointer characters ("*"), to
5420 use Java's namespace separator symbol ("." instead of "::"), and to output
5421 JArray<TYPE> as TYPE[]. */
5424 java_demangle_v3 (const char *mangled
)
5428 return d_demangle (mangled
, DMGL_JAVA
| DMGL_PARAMS
| DMGL_RET_POSTFIX
, &alc
);
5432 java_demangle_v3_callback (const char *mangled
,
5433 demangle_callbackref callback
, void *opaque
)
5435 return d_demangle_callback (mangled
,
5436 DMGL_JAVA
| DMGL_PARAMS
| DMGL_RET_POSTFIX
,
5440 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
5442 #ifndef IN_GLIBCPP_V3
5444 /* Demangle a string in order to find out whether it is a constructor
5445 or destructor. Return non-zero on success. Set *CTOR_KIND and
5446 *DTOR_KIND appropriately. */
5449 is_ctor_or_dtor (const char *mangled
,
5450 enum gnu_v3_ctor_kinds
*ctor_kind
,
5451 enum gnu_v3_dtor_kinds
*dtor_kind
)
5454 struct demangle_component
*dc
;
5457 *ctor_kind
= (enum gnu_v3_ctor_kinds
) 0;
5458 *dtor_kind
= (enum gnu_v3_dtor_kinds
) 0;
5460 cplus_demangle_init_info (mangled
, DMGL_GNU_V3
, strlen (mangled
), &di
);
5463 #ifdef CP_DYNAMIC_ARRAYS
5464 __extension__
struct demangle_component comps
[di
.num_comps
];
5465 __extension__
struct demangle_component
*subs
[di
.num_subs
];
5470 di
.comps
= alloca (di
.num_comps
* sizeof (*di
.comps
));
5471 di
.subs
= alloca (di
.num_subs
* sizeof (*di
.subs
));
5474 dc
= cplus_demangle_mangled_name (&di
, 1);
5476 /* Note that because we did not pass DMGL_PARAMS, we don't expect
5477 to demangle the entire string. */
5487 case DEMANGLE_COMPONENT_TYPED_NAME
:
5488 case DEMANGLE_COMPONENT_TEMPLATE
:
5489 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
5490 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
5491 case DEMANGLE_COMPONENT_CONST_THIS
:
5494 case DEMANGLE_COMPONENT_QUAL_NAME
:
5495 case DEMANGLE_COMPONENT_LOCAL_NAME
:
5498 case DEMANGLE_COMPONENT_CTOR
:
5499 *ctor_kind
= dc
->u
.s_ctor
.kind
;
5503 case DEMANGLE_COMPONENT_DTOR
:
5504 *dtor_kind
= dc
->u
.s_dtor
.kind
;
5515 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
5516 name. A non-zero return indicates the type of constructor. */
5518 enum gnu_v3_ctor_kinds
5519 is_gnu_v3_mangled_ctor (const char *name
)
5521 enum gnu_v3_ctor_kinds ctor_kind
;
5522 enum gnu_v3_dtor_kinds dtor_kind
;
5524 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
5525 return (enum gnu_v3_ctor_kinds
) 0;
5530 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
5531 name. A non-zero return indicates the type of destructor. */
5533 enum gnu_v3_dtor_kinds
5534 is_gnu_v3_mangled_dtor (const char *name
)
5536 enum gnu_v3_ctor_kinds ctor_kind
;
5537 enum gnu_v3_dtor_kinds dtor_kind
;
5539 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
5540 return (enum gnu_v3_dtor_kinds
) 0;
5544 #endif /* IN_GLIBCPP_V3 */
5546 #ifdef STANDALONE_DEMANGLER
5549 #include "dyn-string.h"
5551 static void print_usage (FILE* fp
, int exit_value
);
5553 #define IS_ALPHA(CHAR) \
5554 (((CHAR) >= 'a' && (CHAR) <= 'z') \
5555 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
5557 /* Non-zero if CHAR is a character than can occur in a mangled name. */
5558 #define is_mangled_char(CHAR) \
5559 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
5560 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
5562 /* The name of this program, as invoked. */
5563 const char* program_name
;
5565 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
5568 print_usage (FILE* fp
, int exit_value
)
5570 fprintf (fp
, "Usage: %s [options] [names ...]\n", program_name
);
5571 fprintf (fp
, "Options:\n");
5572 fprintf (fp
, " -h,--help Display this message.\n");
5573 fprintf (fp
, " -p,--no-params Don't display function parameters\n");
5574 fprintf (fp
, " -v,--verbose Produce verbose demanglings.\n");
5575 fprintf (fp
, "If names are provided, they are demangled. Otherwise filters standard input.\n");
5580 /* Option specification for getopt_long. */
5581 static const struct option long_options
[] =
5583 { "help", no_argument
, NULL
, 'h' },
5584 { "no-params", no_argument
, NULL
, 'p' },
5585 { "verbose", no_argument
, NULL
, 'v' },
5586 { NULL
, no_argument
, NULL
, 0 },
5589 /* Main entry for a demangling filter executable. It will demangle
5590 its command line arguments, if any. If none are provided, it will
5591 filter stdin to stdout, replacing any recognized mangled C++ names
5592 with their demangled equivalents. */
5595 main (int argc
, char *argv
[])
5599 int options
= DMGL_PARAMS
| DMGL_ANSI
| DMGL_TYPES
;
5601 /* Use the program name of this program, as invoked. */
5602 program_name
= argv
[0];
5604 /* Parse options. */
5607 opt_char
= getopt_long (argc
, argv
, "hpv", long_options
, NULL
);
5610 case '?': /* Unrecognized option. */
5611 print_usage (stderr
, 1);
5615 print_usage (stdout
, 0);
5619 options
&= ~ DMGL_PARAMS
;
5623 options
|= DMGL_VERBOSE
;
5627 while (opt_char
!= -1);
5630 /* No command line arguments were provided. Filter stdin. */
5632 dyn_string_t mangled
= dyn_string_new (3);
5635 /* Read all of input. */
5636 while (!feof (stdin
))
5640 /* Pile characters into mangled until we hit one that can't
5641 occur in a mangled name. */
5643 while (!feof (stdin
) && is_mangled_char (c
))
5645 dyn_string_append_char (mangled
, c
);
5651 if (dyn_string_length (mangled
) > 0)
5653 #ifdef IN_GLIBCPP_V3
5654 s
= __cxa_demangle (dyn_string_buf (mangled
), NULL
, NULL
, NULL
);
5656 s
= cplus_demangle_v3 (dyn_string_buf (mangled
), options
);
5666 /* It might not have been a mangled name. Print the
5668 fputs (dyn_string_buf (mangled
), stdout
);
5671 dyn_string_clear (mangled
);
5674 /* If we haven't hit EOF yet, we've read one character that
5675 can't occur in a mangled name, so print it out. */
5680 dyn_string_delete (mangled
);
5683 /* Demangle command line arguments. */
5685 /* Loop over command line arguments. */
5686 for (i
= optind
; i
< argc
; ++i
)
5689 #ifdef IN_GLIBCPP_V3
5693 /* Attempt to demangle. */
5694 #ifdef IN_GLIBCPP_V3
5695 s
= __cxa_demangle (argv
[i
], NULL
, NULL
, &status
);
5697 s
= cplus_demangle_v3 (argv
[i
], options
);
5700 /* If it worked, print the demangled name. */
5708 #ifdef IN_GLIBCPP_V3
5709 fprintf (stderr
, "Failed: %s (status %d)\n", argv
[i
], status
);
5711 fprintf (stderr
, "Failed: %s\n", argv
[i
]);
5720 #endif /* STANDALONE_DEMANGLER */