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_TAGGED_NAME
:
512 printf ("tagged name\n");
513 d_dump (dc
->u
.s_binary
.left
, indent
+ 2);
514 d_dump (dc
->u
.s_binary
.right
, indent
+ 2);
516 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
517 printf ("template parameter %ld\n", dc
->u
.s_number
.number
);
519 case DEMANGLE_COMPONENT_CTOR
:
520 printf ("constructor %d\n", (int) dc
->u
.s_ctor
.kind
);
521 d_dump (dc
->u
.s_ctor
.name
, indent
+ 2);
523 case DEMANGLE_COMPONENT_DTOR
:
524 printf ("destructor %d\n", (int) dc
->u
.s_dtor
.kind
);
525 d_dump (dc
->u
.s_dtor
.name
, indent
+ 2);
527 case DEMANGLE_COMPONENT_SUB_STD
:
528 printf ("standard substitution %s\n", dc
->u
.s_string
.string
);
530 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
531 printf ("builtin type %s\n", dc
->u
.s_builtin
.type
->name
);
533 case DEMANGLE_COMPONENT_OPERATOR
:
534 printf ("operator %s\n", dc
->u
.s_operator
.op
->name
);
536 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
537 printf ("extended operator with %d args\n",
538 dc
->u
.s_extended_operator
.args
);
539 d_dump (dc
->u
.s_extended_operator
.name
, indent
+ 2);
542 case DEMANGLE_COMPONENT_QUAL_NAME
:
543 printf ("qualified name\n");
545 case DEMANGLE_COMPONENT_LOCAL_NAME
:
546 printf ("local name\n");
548 case DEMANGLE_COMPONENT_TYPED_NAME
:
549 printf ("typed name\n");
551 case DEMANGLE_COMPONENT_TEMPLATE
:
552 printf ("template\n");
554 case DEMANGLE_COMPONENT_VTABLE
:
557 case DEMANGLE_COMPONENT_VTT
:
560 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
561 printf ("construction vtable\n");
563 case DEMANGLE_COMPONENT_TYPEINFO
:
564 printf ("typeinfo\n");
566 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
567 printf ("typeinfo name\n");
569 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
570 printf ("typeinfo function\n");
572 case DEMANGLE_COMPONENT_THUNK
:
575 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
576 printf ("virtual thunk\n");
578 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
579 printf ("covariant thunk\n");
581 case DEMANGLE_COMPONENT_JAVA_CLASS
:
582 printf ("java class\n");
584 case DEMANGLE_COMPONENT_GUARD
:
587 case DEMANGLE_COMPONENT_REFTEMP
:
588 printf ("reference temporary\n");
590 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
591 printf ("hidden alias\n");
593 case DEMANGLE_COMPONENT_TRANSACTION_CLONE
:
594 printf ("transaction clone\n");
596 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
:
597 printf ("non-transaction clone\n");
599 case DEMANGLE_COMPONENT_RESTRICT
:
600 printf ("restrict\n");
602 case DEMANGLE_COMPONENT_VOLATILE
:
603 printf ("volatile\n");
605 case DEMANGLE_COMPONENT_CONST
:
608 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
609 printf ("restrict this\n");
611 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
612 printf ("volatile this\n");
614 case DEMANGLE_COMPONENT_CONST_THIS
:
615 printf ("const this\n");
617 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
618 printf ("vendor type qualifier\n");
620 case DEMANGLE_COMPONENT_POINTER
:
621 printf ("pointer\n");
623 case DEMANGLE_COMPONENT_REFERENCE
:
624 printf ("reference\n");
626 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
627 printf ("rvalue reference\n");
629 case DEMANGLE_COMPONENT_COMPLEX
:
630 printf ("complex\n");
632 case DEMANGLE_COMPONENT_IMAGINARY
:
633 printf ("imaginary\n");
635 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
636 printf ("vendor type\n");
638 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
639 printf ("function type\n");
641 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
642 printf ("array type\n");
644 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
645 printf ("pointer to member type\n");
647 case DEMANGLE_COMPONENT_FIXED_TYPE
:
648 printf ("fixed-point type\n");
650 case DEMANGLE_COMPONENT_ARGLIST
:
651 printf ("argument list\n");
653 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
654 printf ("template argument list\n");
656 case DEMANGLE_COMPONENT_INITIALIZER_LIST
:
657 printf ("initializer list\n");
659 case DEMANGLE_COMPONENT_CAST
:
662 case DEMANGLE_COMPONENT_NULLARY
:
663 printf ("nullary operator\n");
665 case DEMANGLE_COMPONENT_UNARY
:
666 printf ("unary operator\n");
668 case DEMANGLE_COMPONENT_BINARY
:
669 printf ("binary operator\n");
671 case DEMANGLE_COMPONENT_BINARY_ARGS
:
672 printf ("binary operator arguments\n");
674 case DEMANGLE_COMPONENT_TRINARY
:
675 printf ("trinary operator\n");
677 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
678 printf ("trinary operator arguments 1\n");
680 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
681 printf ("trinary operator arguments 1\n");
683 case DEMANGLE_COMPONENT_LITERAL
:
684 printf ("literal\n");
686 case DEMANGLE_COMPONENT_LITERAL_NEG
:
687 printf ("negative literal\n");
689 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
690 printf ("java resource\n");
692 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
693 printf ("compound name\n");
695 case DEMANGLE_COMPONENT_CHARACTER
:
696 printf ("character '%c'\n", dc
->u
.s_character
.character
);
698 case DEMANGLE_COMPONENT_DECLTYPE
:
699 printf ("decltype\n");
701 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
702 printf ("pack expansion\n");
704 case DEMANGLE_COMPONENT_TLS_INIT
:
705 printf ("tls init function\n");
707 case DEMANGLE_COMPONENT_TLS_WRAPPER
:
708 printf ("tls wrapper function\n");
710 case DEMANGLE_COMPONENT_DEFAULT_ARG
:
711 printf ("default argument %d\n", dc
->u
.s_unary_num
.num
);
712 d_dump (dc
->u
.s_unary_num
.sub
, indent
+2);
714 case DEMANGLE_COMPONENT_LAMBDA
:
715 printf ("lambda %d\n", dc
->u
.s_unary_num
.num
);
716 d_dump (dc
->u
.s_unary_num
.sub
, indent
+2);
720 d_dump (d_left (dc
), indent
+ 2);
721 d_dump (d_right (dc
), indent
+ 2);
724 #endif /* CP_DEMANGLE_DEBUG */
726 /* Fill in a DEMANGLE_COMPONENT_NAME. */
728 CP_STATIC_IF_GLIBCPP_V3
730 cplus_demangle_fill_name (struct demangle_component
*p
, const char *s
, int len
)
732 if (p
== NULL
|| s
== NULL
|| len
== 0)
734 p
->type
= DEMANGLE_COMPONENT_NAME
;
736 p
->u
.s_name
.len
= len
;
740 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
742 CP_STATIC_IF_GLIBCPP_V3
744 cplus_demangle_fill_extended_operator (struct demangle_component
*p
, int args
,
745 struct demangle_component
*name
)
747 if (p
== NULL
|| args
< 0 || name
== NULL
)
749 p
->type
= DEMANGLE_COMPONENT_EXTENDED_OPERATOR
;
750 p
->u
.s_extended_operator
.args
= args
;
751 p
->u
.s_extended_operator
.name
= name
;
755 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
757 CP_STATIC_IF_GLIBCPP_V3
759 cplus_demangle_fill_ctor (struct demangle_component
*p
,
760 enum gnu_v3_ctor_kinds kind
,
761 struct demangle_component
*name
)
765 || (int) kind
< gnu_v3_complete_object_ctor
766 || (int) kind
> gnu_v3_object_ctor_group
)
768 p
->type
= DEMANGLE_COMPONENT_CTOR
;
769 p
->u
.s_ctor
.kind
= kind
;
770 p
->u
.s_ctor
.name
= name
;
774 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
776 CP_STATIC_IF_GLIBCPP_V3
778 cplus_demangle_fill_dtor (struct demangle_component
*p
,
779 enum gnu_v3_dtor_kinds kind
,
780 struct demangle_component
*name
)
784 || (int) kind
< gnu_v3_deleting_dtor
785 || (int) kind
> gnu_v3_object_dtor_group
)
787 p
->type
= DEMANGLE_COMPONENT_DTOR
;
788 p
->u
.s_dtor
.kind
= kind
;
789 p
->u
.s_dtor
.name
= name
;
793 /* Add a new component. */
795 static struct demangle_component
*
796 d_make_empty (struct d_info
*di
)
798 struct demangle_component
*p
;
800 if (di
->next_comp
>= di
->num_comps
)
802 p
= &di
->comps
[di
->next_comp
];
807 /* Add a new generic component. */
809 static struct demangle_component
*
810 d_make_comp (struct d_info
*di
, enum demangle_component_type type
,
811 struct demangle_component
*left
,
812 struct demangle_component
*right
)
814 struct demangle_component
*p
;
816 /* We check for errors here. A typical error would be a NULL return
817 from a subroutine. We catch those here, and return NULL
821 /* These types require two parameters. */
822 case DEMANGLE_COMPONENT_QUAL_NAME
:
823 case DEMANGLE_COMPONENT_LOCAL_NAME
:
824 case DEMANGLE_COMPONENT_TYPED_NAME
:
825 case DEMANGLE_COMPONENT_TAGGED_NAME
:
826 case DEMANGLE_COMPONENT_TEMPLATE
:
827 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
828 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
829 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
830 case DEMANGLE_COMPONENT_UNARY
:
831 case DEMANGLE_COMPONENT_BINARY
:
832 case DEMANGLE_COMPONENT_BINARY_ARGS
:
833 case DEMANGLE_COMPONENT_TRINARY
:
834 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
835 case DEMANGLE_COMPONENT_LITERAL
:
836 case DEMANGLE_COMPONENT_LITERAL_NEG
:
837 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
838 case DEMANGLE_COMPONENT_VECTOR_TYPE
:
839 case DEMANGLE_COMPONENT_CLONE
:
840 if (left
== NULL
|| right
== NULL
)
844 /* These types only require one parameter. */
845 case DEMANGLE_COMPONENT_VTABLE
:
846 case DEMANGLE_COMPONENT_VTT
:
847 case DEMANGLE_COMPONENT_TYPEINFO
:
848 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
849 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
850 case DEMANGLE_COMPONENT_THUNK
:
851 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
852 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
853 case DEMANGLE_COMPONENT_JAVA_CLASS
:
854 case DEMANGLE_COMPONENT_GUARD
:
855 case DEMANGLE_COMPONENT_TLS_INIT
:
856 case DEMANGLE_COMPONENT_TLS_WRAPPER
:
857 case DEMANGLE_COMPONENT_REFTEMP
:
858 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
859 case DEMANGLE_COMPONENT_TRANSACTION_CLONE
:
860 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
:
861 case DEMANGLE_COMPONENT_POINTER
:
862 case DEMANGLE_COMPONENT_REFERENCE
:
863 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
864 case DEMANGLE_COMPONENT_COMPLEX
:
865 case DEMANGLE_COMPONENT_IMAGINARY
:
866 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
867 case DEMANGLE_COMPONENT_CAST
:
868 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
869 case DEMANGLE_COMPONENT_DECLTYPE
:
870 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
871 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
:
872 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS
:
873 case DEMANGLE_COMPONENT_NULLARY
:
874 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
879 /* This needs a right parameter, but the left parameter can be
881 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
882 case DEMANGLE_COMPONENT_INITIALIZER_LIST
:
887 /* These are allowed to have no parameters--in some cases they
888 will be filled in later. */
889 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
890 case DEMANGLE_COMPONENT_RESTRICT
:
891 case DEMANGLE_COMPONENT_VOLATILE
:
892 case DEMANGLE_COMPONENT_CONST
:
893 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
894 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
895 case DEMANGLE_COMPONENT_CONST_THIS
:
896 case DEMANGLE_COMPONENT_ARGLIST
:
897 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
900 /* Other types should not be seen here. */
905 p
= d_make_empty (di
);
909 p
->u
.s_binary
.left
= left
;
910 p
->u
.s_binary
.right
= right
;
915 /* Add a new demangle mangled name component. */
917 static struct demangle_component
*
918 d_make_demangle_mangled_name (struct d_info
*di
, const char *s
)
920 if (d_peek_char (di
) != '_' || d_peek_next_char (di
) != 'Z')
921 return d_make_name (di
, s
, strlen (s
));
923 return d_encoding (di
, 0);
926 /* Add a new name component. */
928 static struct demangle_component
*
929 d_make_name (struct d_info
*di
, const char *s
, int len
)
931 struct demangle_component
*p
;
933 p
= d_make_empty (di
);
934 if (! cplus_demangle_fill_name (p
, s
, len
))
939 /* Add a new builtin type component. */
941 static struct demangle_component
*
942 d_make_builtin_type (struct d_info
*di
,
943 const struct demangle_builtin_type_info
*type
)
945 struct demangle_component
*p
;
949 p
= d_make_empty (di
);
952 p
->type
= DEMANGLE_COMPONENT_BUILTIN_TYPE
;
953 p
->u
.s_builtin
.type
= type
;
958 /* Add a new operator component. */
960 static struct demangle_component
*
961 d_make_operator (struct d_info
*di
, const struct demangle_operator_info
*op
)
963 struct demangle_component
*p
;
965 p
= d_make_empty (di
);
968 p
->type
= DEMANGLE_COMPONENT_OPERATOR
;
969 p
->u
.s_operator
.op
= op
;
974 /* Add a new extended operator component. */
976 static struct demangle_component
*
977 d_make_extended_operator (struct d_info
*di
, int args
,
978 struct demangle_component
*name
)
980 struct demangle_component
*p
;
982 p
= d_make_empty (di
);
983 if (! cplus_demangle_fill_extended_operator (p
, args
, name
))
988 static struct demangle_component
*
989 d_make_default_arg (struct d_info
*di
, int num
,
990 struct demangle_component
*sub
)
992 struct demangle_component
*p
= d_make_empty (di
);
995 p
->type
= DEMANGLE_COMPONENT_DEFAULT_ARG
;
996 p
->u
.s_unary_num
.num
= num
;
997 p
->u
.s_unary_num
.sub
= sub
;
1002 /* Add a new constructor component. */
1004 static struct demangle_component
*
1005 d_make_ctor (struct d_info
*di
, enum gnu_v3_ctor_kinds kind
,
1006 struct demangle_component
*name
)
1008 struct demangle_component
*p
;
1010 p
= d_make_empty (di
);
1011 if (! cplus_demangle_fill_ctor (p
, kind
, name
))
1016 /* Add a new destructor component. */
1018 static struct demangle_component
*
1019 d_make_dtor (struct d_info
*di
, enum gnu_v3_dtor_kinds kind
,
1020 struct demangle_component
*name
)
1022 struct demangle_component
*p
;
1024 p
= d_make_empty (di
);
1025 if (! cplus_demangle_fill_dtor (p
, kind
, name
))
1030 /* Add a new template parameter. */
1032 static struct demangle_component
*
1033 d_make_template_param (struct d_info
*di
, long i
)
1035 struct demangle_component
*p
;
1037 p
= d_make_empty (di
);
1040 p
->type
= DEMANGLE_COMPONENT_TEMPLATE_PARAM
;
1041 p
->u
.s_number
.number
= i
;
1046 /* Add a new function parameter. */
1048 static struct demangle_component
*
1049 d_make_function_param (struct d_info
*di
, long i
)
1051 struct demangle_component
*p
;
1053 p
= d_make_empty (di
);
1056 p
->type
= DEMANGLE_COMPONENT_FUNCTION_PARAM
;
1057 p
->u
.s_number
.number
= i
;
1062 /* Add a new standard substitution component. */
1064 static struct demangle_component
*
1065 d_make_sub (struct d_info
*di
, const char *name
, int len
)
1067 struct demangle_component
*p
;
1069 p
= d_make_empty (di
);
1072 p
->type
= DEMANGLE_COMPONENT_SUB_STD
;
1073 p
->u
.s_string
.string
= name
;
1074 p
->u
.s_string
.len
= len
;
1079 /* <mangled-name> ::= _Z <encoding> [<clone-suffix>]*
1081 TOP_LEVEL is non-zero when called at the top level. */
1083 CP_STATIC_IF_GLIBCPP_V3
1084 struct demangle_component
*
1085 cplus_demangle_mangled_name (struct d_info
*di
, int top_level
)
1087 struct demangle_component
*p
;
1089 if (! d_check_char (di
, '_')
1090 /* Allow missing _ if not at toplevel to work around a
1091 bug in G++ abi-version=2 mangling; see the comment in
1092 write_template_arg. */
1095 if (! d_check_char (di
, 'Z'))
1097 p
= d_encoding (di
, top_level
);
1099 /* If at top level and parsing parameters, check for a clone
1101 if (top_level
&& (di
->options
& DMGL_PARAMS
) != 0)
1102 while (d_peek_char (di
) == '.'
1103 && (IS_LOWER (d_peek_next_char (di
))
1104 || d_peek_next_char (di
) == '_'
1105 || IS_DIGIT (d_peek_next_char (di
))))
1106 p
= d_clone_suffix (di
, p
);
1111 /* Return whether a function should have a return type. The argument
1112 is the function name, which may be qualified in various ways. The
1113 rules are that template functions have return types with some
1114 exceptions, function types which are not part of a function name
1115 mangling have return types with some exceptions, and non-template
1116 function names do not have return types. The exceptions are that
1117 constructors, destructors, and conversion operators do not have
1121 has_return_type (struct demangle_component
*dc
)
1129 case DEMANGLE_COMPONENT_TEMPLATE
:
1130 return ! is_ctor_dtor_or_conversion (d_left (dc
));
1131 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
1132 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
1133 case DEMANGLE_COMPONENT_CONST_THIS
:
1134 return has_return_type (d_left (dc
));
1138 /* Return whether a name is a constructor, a destructor, or a
1139 conversion operator. */
1142 is_ctor_dtor_or_conversion (struct demangle_component
*dc
)
1150 case DEMANGLE_COMPONENT_QUAL_NAME
:
1151 case DEMANGLE_COMPONENT_LOCAL_NAME
:
1152 return is_ctor_dtor_or_conversion (d_right (dc
));
1153 case DEMANGLE_COMPONENT_CTOR
:
1154 case DEMANGLE_COMPONENT_DTOR
:
1155 case DEMANGLE_COMPONENT_CAST
:
1160 /* <encoding> ::= <(function) name> <bare-function-type>
1164 TOP_LEVEL is non-zero when called at the top level, in which case
1165 if DMGL_PARAMS is not set we do not demangle the function
1166 parameters. We only set this at the top level, because otherwise
1167 we would not correctly demangle names in local scopes. */
1169 static struct demangle_component
*
1170 d_encoding (struct d_info
*di
, int top_level
)
1172 char peek
= d_peek_char (di
);
1174 if (peek
== 'G' || peek
== 'T')
1175 return d_special_name (di
);
1178 struct demangle_component
*dc
;
1182 if (dc
!= NULL
&& top_level
&& (di
->options
& DMGL_PARAMS
) == 0)
1184 /* Strip off any initial CV-qualifiers, as they really apply
1185 to the `this' parameter, and they were not output by the
1186 v2 demangler without DMGL_PARAMS. */
1187 while (dc
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
1188 || dc
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
1189 || dc
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
1192 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1193 there may be CV-qualifiers on its right argument which
1194 really apply here; this happens when parsing a class
1195 which is local to a function. */
1196 if (dc
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
1198 struct demangle_component
*dcr
;
1201 while (dcr
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
1202 || dcr
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
1203 || dcr
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
1205 dc
->u
.s_binary
.right
= dcr
;
1211 peek
= d_peek_char (di
);
1212 if (dc
== NULL
|| peek
== '\0' || peek
== 'E')
1214 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPED_NAME
, dc
,
1215 d_bare_function_type (di
, has_return_type (dc
)));
1219 /* <tagged-name> ::= <name> B <source-name> */
1221 static struct demangle_component
*
1222 d_abi_tags (struct d_info
*di
, struct demangle_component
*dc
)
1225 while (peek
= d_peek_char (di
),
1228 struct demangle_component
*tag
;
1230 tag
= d_source_name (di
);
1231 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_TAGGED_NAME
, dc
, tag
);
1236 /* <name> ::= <nested-name>
1238 ::= <unscoped-template-name> <template-args>
1241 <unscoped-name> ::= <unqualified-name>
1242 ::= St <unqualified-name>
1244 <unscoped-template-name> ::= <unscoped-name>
1248 static struct demangle_component
*
1249 d_name (struct d_info
*di
)
1251 char peek
= d_peek_char (di
);
1252 struct demangle_component
*dc
;
1257 return d_nested_name (di
);
1260 return d_local_name (di
);
1264 return d_unqualified_name (di
);
1270 if (d_peek_next_char (di
) != 't')
1272 dc
= d_substitution (di
, 0);
1278 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
,
1279 d_make_name (di
, "std", 3),
1280 d_unqualified_name (di
));
1285 if (d_peek_char (di
) != 'I')
1287 /* The grammar does not permit this case to occur if we
1288 called d_substitution() above (i.e., subst == 1). We
1289 don't bother to check. */
1293 /* This is <template-args>, which means that we just saw
1294 <unscoped-template-name>, which is a substitution
1295 candidate if we didn't just get it from a
1299 if (! d_add_substitution (di
, dc
))
1302 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, dc
,
1303 d_template_args (di
));
1310 dc
= d_unqualified_name (di
);
1311 if (d_peek_char (di
) == 'I')
1313 /* This is <template-args>, which means that we just saw
1314 <unscoped-template-name>, which is a substitution
1316 if (! d_add_substitution (di
, dc
))
1318 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, dc
,
1319 d_template_args (di
));
1325 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1326 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1329 static struct demangle_component
*
1330 d_nested_name (struct d_info
*di
)
1332 struct demangle_component
*ret
;
1333 struct demangle_component
**pret
;
1335 if (! d_check_char (di
, 'N'))
1338 pret
= d_cv_qualifiers (di
, &ret
, 1);
1342 *pret
= d_prefix (di
);
1346 if (! d_check_char (di
, 'E'))
1352 /* <prefix> ::= <prefix> <unqualified-name>
1353 ::= <template-prefix> <template-args>
1354 ::= <template-param>
1359 <template-prefix> ::= <prefix> <(template) unqualified-name>
1360 ::= <template-param>
1364 static struct demangle_component
*
1365 d_prefix (struct d_info
*di
)
1367 struct demangle_component
*ret
= NULL
;
1372 enum demangle_component_type comb_type
;
1373 struct demangle_component
*dc
;
1375 peek
= d_peek_char (di
);
1379 /* The older code accepts a <local-name> here, but I don't see
1380 that in the grammar. The older code does not accept a
1381 <template-param> here. */
1383 comb_type
= DEMANGLE_COMPONENT_QUAL_NAME
;
1386 char peek2
= d_peek_next_char (di
);
1387 if (peek2
== 'T' || peek2
== 't')
1389 dc
= cplus_demangle_type (di
);
1391 /* Destructor name. */
1392 dc
= d_unqualified_name (di
);
1394 else if (IS_DIGIT (peek
)
1399 dc
= d_unqualified_name (di
);
1400 else if (peek
== 'S')
1401 dc
= d_substitution (di
, 1);
1402 else if (peek
== 'I')
1406 comb_type
= DEMANGLE_COMPONENT_TEMPLATE
;
1407 dc
= d_template_args (di
);
1409 else if (peek
== 'T')
1410 dc
= d_template_param (di
);
1411 else if (peek
== 'E')
1413 else if (peek
== 'M')
1415 /* Initializer scope for a lambda. We don't need to represent
1416 this; the normal code will just treat the variable as a type
1417 scope, which gives appropriate output. */
1429 ret
= d_make_comp (di
, comb_type
, ret
, dc
);
1431 if (peek
!= 'S' && d_peek_char (di
) != 'E')
1433 if (! d_add_substitution (di
, ret
))
1439 /* <unqualified-name> ::= <operator-name>
1440 ::= <ctor-dtor-name>
1442 ::= <local-source-name>
1444 <local-source-name> ::= L <source-name> <discriminator>
1447 static struct demangle_component
*
1448 d_unqualified_name (struct d_info
*di
)
1450 struct demangle_component
*ret
;
1453 peek
= d_peek_char (di
);
1454 if (IS_DIGIT (peek
))
1455 ret
= d_source_name (di
);
1456 else if (IS_LOWER (peek
))
1458 ret
= d_operator_name (di
);
1459 if (ret
!= NULL
&& ret
->type
== DEMANGLE_COMPONENT_OPERATOR
)
1461 di
->expansion
+= sizeof "operator" + ret
->u
.s_operator
.op
->len
- 2;
1462 if (!strcmp (ret
->u
.s_operator
.op
->code
, "li"))
1463 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, ret
,
1464 d_source_name (di
));
1467 else if (peek
== 'C' || peek
== 'D')
1468 ret
= d_ctor_dtor_name (di
);
1469 else if (peek
== 'L')
1473 ret
= d_source_name (di
);
1476 if (! d_discriminator (di
))
1479 else if (peek
== 'U')
1481 switch (d_peek_next_char (di
))
1484 ret
= d_lambda (di
);
1487 ret
= d_unnamed_type (di
);
1496 if (d_peek_char (di
) == 'B')
1497 ret
= d_abi_tags (di
, ret
);
1501 /* <source-name> ::= <(positive length) number> <identifier> */
1503 static struct demangle_component
*
1504 d_source_name (struct d_info
*di
)
1507 struct demangle_component
*ret
;
1509 len
= d_number (di
);
1512 ret
= d_identifier (di
, len
);
1513 di
->last_name
= ret
;
1517 /* number ::= [n] <(non-negative decimal integer)> */
1520 d_number (struct d_info
*di
)
1527 peek
= d_peek_char (di
);
1532 peek
= d_peek_char (di
);
1538 if (! IS_DIGIT (peek
))
1544 ret
= ret
* 10 + peek
- '0';
1546 peek
= d_peek_char (di
);
1550 /* Like d_number, but returns a demangle_component. */
1552 static struct demangle_component
*
1553 d_number_component (struct d_info
*di
)
1555 struct demangle_component
*ret
= d_make_empty (di
);
1558 ret
->type
= DEMANGLE_COMPONENT_NUMBER
;
1559 ret
->u
.s_number
.number
= d_number (di
);
1564 /* identifier ::= <(unqualified source code identifier)> */
1566 static struct demangle_component
*
1567 d_identifier (struct d_info
*di
, int len
)
1573 if (di
->send
- name
< len
)
1576 d_advance (di
, len
);
1578 /* A Java mangled name may have a trailing '$' if it is a C++
1579 keyword. This '$' is not included in the length count. We just
1581 if ((di
->options
& DMGL_JAVA
) != 0
1582 && d_peek_char (di
) == '$')
1585 /* Look for something which looks like a gcc encoding of an
1586 anonymous namespace, and replace it with a more user friendly
1588 if (len
>= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN
+ 2
1589 && memcmp (name
, ANONYMOUS_NAMESPACE_PREFIX
,
1590 ANONYMOUS_NAMESPACE_PREFIX_LEN
) == 0)
1594 s
= name
+ ANONYMOUS_NAMESPACE_PREFIX_LEN
;
1595 if ((*s
== '.' || *s
== '_' || *s
== '$')
1598 di
->expansion
-= len
- sizeof "(anonymous namespace)";
1599 return d_make_name (di
, "(anonymous namespace)",
1600 sizeof "(anonymous namespace)" - 1);
1604 return d_make_name (di
, name
, len
);
1607 /* operator_name ::= many different two character encodings.
1609 ::= v <digit> <source-name>
1611 This list is sorted for binary search. */
1613 #define NL(s) s, (sizeof s) - 1
1615 CP_STATIC_IF_GLIBCPP_V3
1616 const struct demangle_operator_info cplus_demangle_operators
[] =
1618 { "aN", NL ("&="), 2 },
1619 { "aS", NL ("="), 2 },
1620 { "aa", NL ("&&"), 2 },
1621 { "ad", NL ("&"), 1 },
1622 { "an", NL ("&"), 2 },
1623 { "at", NL ("alignof "), 1 },
1624 { "az", NL ("alignof "), 1 },
1625 { "cc", NL ("const_cast"), 2 },
1626 { "cl", NL ("()"), 2 },
1627 { "cm", NL (","), 2 },
1628 { "co", NL ("~"), 1 },
1629 { "dV", NL ("/="), 2 },
1630 { "da", NL ("delete[] "), 1 },
1631 { "dc", NL ("dynamic_cast"), 2 },
1632 { "de", NL ("*"), 1 },
1633 { "dl", NL ("delete "), 1 },
1634 { "ds", NL (".*"), 2 },
1635 { "dt", NL ("."), 2 },
1636 { "dv", NL ("/"), 2 },
1637 { "eO", NL ("^="), 2 },
1638 { "eo", NL ("^"), 2 },
1639 { "eq", NL ("=="), 2 },
1640 { "ge", NL (">="), 2 },
1641 { "gs", NL ("::"), 1 },
1642 { "gt", NL (">"), 2 },
1643 { "ix", NL ("[]"), 2 },
1644 { "lS", NL ("<<="), 2 },
1645 { "le", NL ("<="), 2 },
1646 { "li", NL ("operator\"\" "), 1 },
1647 { "ls", NL ("<<"), 2 },
1648 { "lt", NL ("<"), 2 },
1649 { "mI", NL ("-="), 2 },
1650 { "mL", NL ("*="), 2 },
1651 { "mi", NL ("-"), 2 },
1652 { "ml", NL ("*"), 2 },
1653 { "mm", NL ("--"), 1 },
1654 { "na", NL ("new[]"), 3 },
1655 { "ne", NL ("!="), 2 },
1656 { "ng", NL ("-"), 1 },
1657 { "nt", NL ("!"), 1 },
1658 { "nw", NL ("new"), 3 },
1659 { "oR", NL ("|="), 2 },
1660 { "oo", NL ("||"), 2 },
1661 { "or", NL ("|"), 2 },
1662 { "pL", NL ("+="), 2 },
1663 { "pl", NL ("+"), 2 },
1664 { "pm", NL ("->*"), 2 },
1665 { "pp", NL ("++"), 1 },
1666 { "ps", NL ("+"), 1 },
1667 { "pt", NL ("->"), 2 },
1668 { "qu", NL ("?"), 3 },
1669 { "rM", NL ("%="), 2 },
1670 { "rS", NL (">>="), 2 },
1671 { "rc", NL ("reinterpret_cast"), 2 },
1672 { "rm", NL ("%"), 2 },
1673 { "rs", NL (">>"), 2 },
1674 { "sc", NL ("static_cast"), 2 },
1675 { "st", NL ("sizeof "), 1 },
1676 { "sz", NL ("sizeof "), 1 },
1677 { "tr", NL ("throw"), 0 },
1678 { "tw", NL ("throw "), 1 },
1679 { NULL
, NULL
, 0, 0 }
1682 static struct demangle_component
*
1683 d_operator_name (struct d_info
*di
)
1688 c1
= d_next_char (di
);
1689 c2
= d_next_char (di
);
1690 if (c1
== 'v' && IS_DIGIT (c2
))
1691 return d_make_extended_operator (di
, c2
- '0', d_source_name (di
));
1692 else if (c1
== 'c' && c2
== 'v')
1693 return d_make_comp (di
, DEMANGLE_COMPONENT_CAST
,
1694 cplus_demangle_type (di
), NULL
);
1697 /* LOW is the inclusive lower bound. */
1699 /* HIGH is the exclusive upper bound. We subtract one to ignore
1700 the sentinel at the end of the array. */
1701 int high
= ((sizeof (cplus_demangle_operators
)
1702 / sizeof (cplus_demangle_operators
[0]))
1708 const struct demangle_operator_info
*p
;
1710 i
= low
+ (high
- low
) / 2;
1711 p
= cplus_demangle_operators
+ i
;
1713 if (c1
== p
->code
[0] && c2
== p
->code
[1])
1714 return d_make_operator (di
, p
);
1716 if (c1
< p
->code
[0] || (c1
== p
->code
[0] && c2
< p
->code
[1]))
1726 static struct demangle_component
*
1727 d_make_character (struct d_info
*di
, int c
)
1729 struct demangle_component
*p
;
1730 p
= d_make_empty (di
);
1733 p
->type
= DEMANGLE_COMPONENT_CHARACTER
;
1734 p
->u
.s_character
.character
= c
;
1739 static struct demangle_component
*
1740 d_java_resource (struct d_info
*di
)
1742 struct demangle_component
*p
= NULL
;
1743 struct demangle_component
*next
= NULL
;
1748 len
= d_number (di
);
1752 /* Eat the leading '_'. */
1753 if (d_next_char (di
) != '_')
1766 /* Each chunk is either a '$' escape... */
1784 next
= d_make_character (di
, c
);
1792 /* ... or a sequence of characters. */
1795 while (i
< len
&& str
[i
] && str
[i
] != '$')
1798 next
= d_make_name (di
, str
, i
);
1811 p
= d_make_comp (di
, DEMANGLE_COMPONENT_COMPOUND_NAME
, p
, next
);
1817 p
= d_make_comp (di
, DEMANGLE_COMPONENT_JAVA_RESOURCE
, p
, NULL
);
1822 /* <special-name> ::= TV <type>
1826 ::= GV <(object) name>
1827 ::= T <call-offset> <(base) encoding>
1828 ::= Tc <call-offset> <call-offset> <(base) encoding>
1829 Also g++ extensions:
1830 ::= TC <type> <(offset) number> _ <(base) type>
1835 ::= Gr <resource name>
1840 static struct demangle_component
*
1841 d_special_name (struct d_info
*di
)
1843 di
->expansion
+= 20;
1844 if (d_check_char (di
, 'T'))
1846 switch (d_next_char (di
))
1850 return d_make_comp (di
, DEMANGLE_COMPONENT_VTABLE
,
1851 cplus_demangle_type (di
), NULL
);
1853 di
->expansion
-= 10;
1854 return d_make_comp (di
, DEMANGLE_COMPONENT_VTT
,
1855 cplus_demangle_type (di
), NULL
);
1857 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO
,
1858 cplus_demangle_type (di
), NULL
);
1860 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO_NAME
,
1861 cplus_demangle_type (di
), NULL
);
1864 if (! d_call_offset (di
, 'h'))
1866 return d_make_comp (di
, DEMANGLE_COMPONENT_THUNK
,
1867 d_encoding (di
, 0), NULL
);
1870 if (! d_call_offset (di
, 'v'))
1872 return d_make_comp (di
, DEMANGLE_COMPONENT_VIRTUAL_THUNK
,
1873 d_encoding (di
, 0), NULL
);
1876 if (! d_call_offset (di
, '\0'))
1878 if (! d_call_offset (di
, '\0'))
1880 return d_make_comp (di
, DEMANGLE_COMPONENT_COVARIANT_THUNK
,
1881 d_encoding (di
, 0), NULL
);
1885 struct demangle_component
*derived_type
;
1887 struct demangle_component
*base_type
;
1889 derived_type
= cplus_demangle_type (di
);
1890 offset
= d_number (di
);
1893 if (! d_check_char (di
, '_'))
1895 base_type
= cplus_demangle_type (di
);
1896 /* We don't display the offset. FIXME: We should display
1897 it in verbose mode. */
1899 return d_make_comp (di
, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
,
1900 base_type
, derived_type
);
1904 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO_FN
,
1905 cplus_demangle_type (di
), NULL
);
1907 return d_make_comp (di
, DEMANGLE_COMPONENT_JAVA_CLASS
,
1908 cplus_demangle_type (di
), NULL
);
1911 return d_make_comp (di
, DEMANGLE_COMPONENT_TLS_INIT
,
1915 return d_make_comp (di
, DEMANGLE_COMPONENT_TLS_WRAPPER
,
1922 else if (d_check_char (di
, 'G'))
1924 switch (d_next_char (di
))
1927 return d_make_comp (di
, DEMANGLE_COMPONENT_GUARD
, d_name (di
), NULL
);
1931 struct demangle_component
*name
= d_name (di
);
1932 return d_make_comp (di
, DEMANGLE_COMPONENT_REFTEMP
, name
,
1933 d_number_component (di
));
1937 return d_make_comp (di
, DEMANGLE_COMPONENT_HIDDEN_ALIAS
,
1938 d_encoding (di
, 0), NULL
);
1941 switch (d_next_char (di
))
1944 return d_make_comp (di
, DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
,
1945 d_encoding (di
, 0), NULL
);
1947 /* ??? The proposal is that other letters (such as 'h') stand
1948 for different variants of transaction cloning, such as
1949 compiling directly for hardware transaction support. But
1950 they still should all be transactional clones of some sort
1951 so go ahead and call them that. */
1953 return d_make_comp (di
, DEMANGLE_COMPONENT_TRANSACTION_CLONE
,
1954 d_encoding (di
, 0), NULL
);
1958 return d_java_resource (di
);
1968 /* <call-offset> ::= h <nv-offset> _
1971 <nv-offset> ::= <(offset) number>
1973 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1975 The C parameter, if not '\0', is a character we just read which is
1976 the start of the <call-offset>.
1978 We don't display the offset information anywhere. FIXME: We should
1979 display it in verbose mode. */
1982 d_call_offset (struct d_info
*di
, int c
)
1985 c
= d_next_char (di
);
1992 if (! d_check_char (di
, '_'))
1999 if (! d_check_char (di
, '_'))
2005 /* <ctor-dtor-name> ::= C1
2013 static struct demangle_component
*
2014 d_ctor_dtor_name (struct d_info
*di
)
2016 if (di
->last_name
!= NULL
)
2018 if (di
->last_name
->type
== DEMANGLE_COMPONENT_NAME
)
2019 di
->expansion
+= di
->last_name
->u
.s_name
.len
;
2020 else if (di
->last_name
->type
== DEMANGLE_COMPONENT_SUB_STD
)
2021 di
->expansion
+= di
->last_name
->u
.s_string
.len
;
2023 switch (d_peek_char (di
))
2027 enum gnu_v3_ctor_kinds kind
;
2029 switch (d_peek_next_char (di
))
2032 kind
= gnu_v3_complete_object_ctor
;
2035 kind
= gnu_v3_base_object_ctor
;
2038 kind
= gnu_v3_complete_object_allocating_ctor
;
2041 kind
= gnu_v3_object_ctor_group
;
2047 return d_make_ctor (di
, kind
, di
->last_name
);
2052 enum gnu_v3_dtor_kinds kind
;
2054 switch (d_peek_next_char (di
))
2057 kind
= gnu_v3_deleting_dtor
;
2060 kind
= gnu_v3_complete_object_dtor
;
2063 kind
= gnu_v3_base_object_dtor
;
2066 kind
= gnu_v3_object_dtor_group
;
2072 return d_make_dtor (di
, kind
, di
->last_name
);
2080 /* <type> ::= <builtin-type>
2082 ::= <class-enum-type>
2084 ::= <pointer-to-member-type>
2085 ::= <template-param>
2086 ::= <template-template-param> <template-args>
2088 ::= <CV-qualifiers> <type>
2091 ::= O <type> (C++0x)
2094 ::= U <source-name> <type>
2096 <builtin-type> ::= various one letter codes
2100 CP_STATIC_IF_GLIBCPP_V3
2101 const struct demangle_builtin_type_info
2102 cplus_demangle_builtin_types
[D_BUILTIN_TYPE_COUNT
] =
2104 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT
},
2105 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL
},
2106 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT
},
2107 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT
},
2108 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT
},
2109 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT
},
2110 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT
},
2111 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT
},
2112 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT
},
2113 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED
},
2114 /* k */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2115 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG
},
2116 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG
},
2117 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT
},
2118 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
2120 /* p */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2121 /* q */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2122 /* r */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2123 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT
},
2124 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT
},
2125 /* u */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
2126 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID
},
2127 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT
},
2128 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG
},
2129 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
2130 D_PRINT_UNSIGNED_LONG_LONG
},
2131 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT
},
2132 /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT
},
2133 /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT
},
2134 /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT
},
2135 /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT
},
2136 /* 30 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT
},
2137 /* 31 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT
},
2138 /* 32 */ { NL ("decltype(nullptr)"), NL ("decltype(nullptr)"),
2142 CP_STATIC_IF_GLIBCPP_V3
2143 struct demangle_component
*
2144 cplus_demangle_type (struct d_info
*di
)
2147 struct demangle_component
*ret
;
2150 /* The ABI specifies that when CV-qualifiers are used, the base type
2151 is substitutable, and the fully qualified type is substitutable,
2152 but the base type with a strict subset of the CV-qualifiers is
2153 not substitutable. The natural recursive implementation of the
2154 CV-qualifiers would cause subsets to be substitutable, so instead
2155 we pull them all off now.
2157 FIXME: The ABI says that order-insensitive vendor qualifiers
2158 should be handled in the same way, but we have no way to tell
2159 which vendor qualifiers are order-insensitive and which are
2160 order-sensitive. So we just assume that they are all
2161 order-sensitive. g++ 3.4 supports only one vendor qualifier,
2162 __vector, and it treats it as order-sensitive when mangling
2165 peek
= d_peek_char (di
);
2166 if (peek
== 'r' || peek
== 'V' || peek
== 'K')
2168 struct demangle_component
**pret
;
2170 pret
= d_cv_qualifiers (di
, &ret
, 0);
2173 *pret
= cplus_demangle_type (di
);
2174 if (! *pret
|| ! d_add_substitution (di
, ret
))
2183 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2184 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
2185 case 'o': case 's': case 't':
2186 case 'v': case 'w': case 'x': case 'y': case 'z':
2187 ret
= d_make_builtin_type (di
,
2188 &cplus_demangle_builtin_types
[peek
- 'a']);
2189 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2196 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_VENDOR_TYPE
,
2197 d_source_name (di
), NULL
);
2201 ret
= d_function_type (di
);
2204 case '0': case '1': case '2': case '3': case '4':
2205 case '5': case '6': case '7': case '8': case '9':
2208 ret
= d_class_enum_type (di
);
2212 ret
= d_array_type (di
);
2216 ret
= d_pointer_to_member_type (di
);
2220 ret
= d_template_param (di
);
2221 if (d_peek_char (di
) == 'I')
2223 /* This is <template-template-param> <template-args>. The
2224 <template-template-param> part is a substitution
2226 if (! d_add_substitution (di
, ret
))
2228 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
,
2229 d_template_args (di
));
2234 /* If this is a special substitution, then it is the start of
2235 <class-enum-type>. */
2239 peek_next
= d_peek_next_char (di
);
2240 if (IS_DIGIT (peek_next
)
2242 || IS_UPPER (peek_next
))
2244 ret
= d_substitution (di
, 0);
2245 /* The substituted name may have been a template name and
2246 may be followed by tepmlate args. */
2247 if (d_peek_char (di
) == 'I')
2248 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
,
2249 d_template_args (di
));
2255 ret
= d_class_enum_type (di
);
2256 /* If the substitution was a complete type, then it is not
2257 a new substitution candidate. However, if the
2258 substitution was followed by template arguments, then
2259 the whole thing is a substitution candidate. */
2260 if (ret
!= NULL
&& ret
->type
== DEMANGLE_COMPONENT_SUB_STD
)
2268 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_RVALUE_REFERENCE
,
2269 cplus_demangle_type (di
), NULL
);
2274 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_POINTER
,
2275 cplus_demangle_type (di
), NULL
);
2280 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_REFERENCE
,
2281 cplus_demangle_type (di
), NULL
);
2286 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_COMPLEX
,
2287 cplus_demangle_type (di
), NULL
);
2292 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_IMAGINARY
,
2293 cplus_demangle_type (di
), NULL
);
2298 ret
= d_source_name (di
);
2299 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
,
2300 cplus_demangle_type (di
), ret
);
2306 peek
= d_next_char (di
);
2311 /* decltype (expression) */
2312 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_DECLTYPE
,
2313 d_expression (di
), NULL
);
2314 if (ret
&& d_next_char (di
) != 'E')
2320 /* Pack expansion. */
2321 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_PACK_EXPANSION
,
2322 cplus_demangle_type (di
), NULL
);
2328 ret
= d_make_name (di
, "auto", 4);
2332 /* 32-bit decimal floating point */
2333 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[26]);
2334 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2338 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[27]);
2339 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2343 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[28]);
2344 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2347 /* 16-bit half-precision FP */
2348 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[29]);
2349 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2353 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[30]);
2354 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2358 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[31]);
2359 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2363 /* Fixed point types. DF<int bits><length><fract bits><sat> */
2364 ret
= d_make_empty (di
);
2365 ret
->type
= DEMANGLE_COMPONENT_FIXED_TYPE
;
2366 if ((ret
->u
.s_fixed
.accum
= IS_DIGIT (d_peek_char (di
))))
2367 /* For demangling we don't care about the bits. */
2369 ret
->u
.s_fixed
.length
= cplus_demangle_type (di
);
2370 if (ret
->u
.s_fixed
.length
== NULL
)
2373 peek
= d_next_char (di
);
2374 ret
->u
.s_fixed
.sat
= (peek
== 's');
2378 ret
= d_vector_type (di
);
2383 /* decltype(nullptr) */
2384 ret
= d_make_builtin_type (di
, &cplus_demangle_builtin_types
[32]);
2385 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
2399 if (! d_add_substitution (di
, ret
))
2406 /* <CV-qualifiers> ::= [r] [V] [K] */
2408 static struct demangle_component
**
2409 d_cv_qualifiers (struct d_info
*di
,
2410 struct demangle_component
**pret
, int member_fn
)
2412 struct demangle_component
**pstart
;
2416 peek
= d_peek_char (di
);
2417 while (peek
== 'r' || peek
== 'V' || peek
== 'K')
2419 enum demangle_component_type t
;
2425 ? DEMANGLE_COMPONENT_RESTRICT_THIS
2426 : DEMANGLE_COMPONENT_RESTRICT
);
2427 di
->expansion
+= sizeof "restrict";
2429 else if (peek
== 'V')
2432 ? DEMANGLE_COMPONENT_VOLATILE_THIS
2433 : DEMANGLE_COMPONENT_VOLATILE
);
2434 di
->expansion
+= sizeof "volatile";
2439 ? DEMANGLE_COMPONENT_CONST_THIS
2440 : DEMANGLE_COMPONENT_CONST
);
2441 di
->expansion
+= sizeof "const";
2444 *pret
= d_make_comp (di
, t
, NULL
, NULL
);
2447 pret
= &d_left (*pret
);
2449 peek
= d_peek_char (di
);
2452 if (!member_fn
&& peek
== 'F')
2454 while (pstart
!= pret
)
2456 switch ((*pstart
)->type
)
2458 case DEMANGLE_COMPONENT_RESTRICT
:
2459 (*pstart
)->type
= DEMANGLE_COMPONENT_RESTRICT_THIS
;
2461 case DEMANGLE_COMPONENT_VOLATILE
:
2462 (*pstart
)->type
= DEMANGLE_COMPONENT_VOLATILE_THIS
;
2464 case DEMANGLE_COMPONENT_CONST
:
2465 (*pstart
)->type
= DEMANGLE_COMPONENT_CONST_THIS
;
2470 pstart
= &d_left (*pstart
);
2477 /* <function-type> ::= F [Y] <bare-function-type> E */
2479 static struct demangle_component
*
2480 d_function_type (struct d_info
*di
)
2482 struct demangle_component
*ret
;
2484 if (! d_check_char (di
, 'F'))
2486 if (d_peek_char (di
) == 'Y')
2488 /* Function has C linkage. We don't print this information.
2489 FIXME: We should print it in verbose mode. */
2492 ret
= d_bare_function_type (di
, 1);
2493 if (! d_check_char (di
, 'E'))
2500 static struct demangle_component
*
2501 d_parmlist (struct d_info
*di
)
2503 struct demangle_component
*tl
;
2504 struct demangle_component
**ptl
;
2510 struct demangle_component
*type
;
2512 char peek
= d_peek_char (di
);
2513 if (peek
== '\0' || peek
== 'E' || peek
== '.')
2515 type
= cplus_demangle_type (di
);
2518 *ptl
= d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, type
, NULL
);
2521 ptl
= &d_right (*ptl
);
2524 /* There should be at least one parameter type besides the optional
2525 return type. A function which takes no arguments will have a
2526 single parameter type void. */
2530 /* If we have a single parameter type void, omit it. */
2531 if (d_right (tl
) == NULL
2532 && d_left (tl
)->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
2533 && d_left (tl
)->u
.s_builtin
.type
->print
== D_PRINT_VOID
)
2535 di
->expansion
-= d_left (tl
)->u
.s_builtin
.type
->len
;
2542 /* <bare-function-type> ::= [J]<type>+ */
2544 static struct demangle_component
*
2545 d_bare_function_type (struct d_info
*di
, int has_return_type
)
2547 struct demangle_component
*return_type
;
2548 struct demangle_component
*tl
;
2551 /* Detect special qualifier indicating that the first argument
2552 is the return type. */
2553 peek
= d_peek_char (di
);
2557 has_return_type
= 1;
2560 if (has_return_type
)
2562 return_type
= cplus_demangle_type (di
);
2563 if (return_type
== NULL
)
2569 tl
= d_parmlist (di
);
2573 return d_make_comp (di
, DEMANGLE_COMPONENT_FUNCTION_TYPE
,
2577 /* <class-enum-type> ::= <name> */
2579 static struct demangle_component
*
2580 d_class_enum_type (struct d_info
*di
)
2585 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2586 ::= A [<(dimension) expression>] _ <(element) type>
2589 static struct demangle_component
*
2590 d_array_type (struct d_info
*di
)
2593 struct demangle_component
*dim
;
2595 if (! d_check_char (di
, 'A'))
2598 peek
= d_peek_char (di
);
2601 else if (IS_DIGIT (peek
))
2609 peek
= d_peek_char (di
);
2611 while (IS_DIGIT (peek
));
2612 dim
= d_make_name (di
, s
, d_str (di
) - s
);
2618 dim
= d_expression (di
);
2623 if (! d_check_char (di
, '_'))
2626 return d_make_comp (di
, DEMANGLE_COMPONENT_ARRAY_TYPE
, dim
,
2627 cplus_demangle_type (di
));
2630 /* <vector-type> ::= Dv <number> _ <type>
2631 ::= Dv _ <expression> _ <type> */
2633 static struct demangle_component
*
2634 d_vector_type (struct d_info
*di
)
2637 struct demangle_component
*dim
;
2639 peek
= d_peek_char (di
);
2643 dim
= d_expression (di
);
2646 dim
= d_number_component (di
);
2651 if (! d_check_char (di
, '_'))
2654 return d_make_comp (di
, DEMANGLE_COMPONENT_VECTOR_TYPE
, dim
,
2655 cplus_demangle_type (di
));
2658 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2660 static struct demangle_component
*
2661 d_pointer_to_member_type (struct d_info
*di
)
2663 struct demangle_component
*cl
;
2664 struct demangle_component
*mem
;
2665 struct demangle_component
**pmem
;
2667 if (! d_check_char (di
, 'M'))
2670 cl
= cplus_demangle_type (di
);
2672 /* The ABI specifies that any type can be a substitution source, and
2673 that M is followed by two types, and that when a CV-qualified
2674 type is seen both the base type and the CV-qualified types are
2675 substitution sources. The ABI also specifies that for a pointer
2676 to a CV-qualified member function, the qualifiers are attached to
2677 the second type. Given the grammar, a plain reading of the ABI
2678 suggests that both the CV-qualified member function and the
2679 non-qualified member function are substitution sources. However,
2680 g++ does not work that way. g++ treats only the CV-qualified
2681 member function as a substitution source. FIXME. So to work
2682 with g++, we need to pull off the CV-qualifiers here, in order to
2683 avoid calling add_substitution() in cplus_demangle_type(). But
2684 for a CV-qualified member which is not a function, g++ does
2685 follow the ABI, so we need to handle that case here by calling
2686 d_add_substitution ourselves. */
2688 pmem
= d_cv_qualifiers (di
, &mem
, 1);
2691 *pmem
= cplus_demangle_type (di
);
2695 if (pmem
!= &mem
&& (*pmem
)->type
!= DEMANGLE_COMPONENT_FUNCTION_TYPE
)
2697 if (! d_add_substitution (di
, mem
))
2701 return d_make_comp (di
, DEMANGLE_COMPONENT_PTRMEM_TYPE
, cl
, mem
);
2704 /* <non-negative number> _ */
2707 d_compact_number (struct d_info
*di
)
2710 if (d_peek_char (di
) == '_')
2712 else if (d_peek_char (di
) == 'n')
2715 num
= d_number (di
) + 1;
2717 if (! d_check_char (di
, '_'))
2722 /* <template-param> ::= T_
2723 ::= T <(parameter-2 non-negative) number> _
2726 static struct demangle_component
*
2727 d_template_param (struct d_info
*di
)
2731 if (! d_check_char (di
, 'T'))
2734 param
= d_compact_number (di
);
2740 return d_make_template_param (di
, param
);
2743 /* <template-args> ::= I <template-arg>+ E */
2745 static struct demangle_component
*
2746 d_template_args (struct d_info
*di
)
2748 struct demangle_component
*hold_last_name
;
2749 struct demangle_component
*al
;
2750 struct demangle_component
**pal
;
2752 /* Preserve the last name we saw--don't let the template arguments
2753 clobber it, as that would give us the wrong name for a subsequent
2754 constructor or destructor. */
2755 hold_last_name
= di
->last_name
;
2757 if (d_peek_char (di
) != 'I'
2758 && d_peek_char (di
) != 'J')
2762 if (d_peek_char (di
) == 'E')
2764 /* An argument pack can be empty. */
2766 return d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
, NULL
, NULL
);
2773 struct demangle_component
*a
;
2775 a
= d_template_arg (di
);
2779 *pal
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
, a
, NULL
);
2782 pal
= &d_right (*pal
);
2784 if (d_peek_char (di
) == 'E')
2791 di
->last_name
= hold_last_name
;
2796 /* <template-arg> ::= <type>
2797 ::= X <expression> E
2801 static struct demangle_component
*
2802 d_template_arg (struct d_info
*di
)
2804 struct demangle_component
*ret
;
2806 switch (d_peek_char (di
))
2810 ret
= d_expression (di
);
2811 if (! d_check_char (di
, 'E'))
2816 return d_expr_primary (di
);
2820 /* An argument pack. */
2821 return d_template_args (di
);
2824 return cplus_demangle_type (di
);
2828 /* Parse a sequence of expressions until we hit the terminator
2831 static struct demangle_component
*
2832 d_exprlist (struct d_info
*di
, char terminator
)
2834 struct demangle_component
*list
= NULL
;
2835 struct demangle_component
**p
= &list
;
2837 if (d_peek_char (di
) == terminator
)
2840 return d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, NULL
, NULL
);
2845 struct demangle_component
*arg
= d_expression (di
);
2849 *p
= d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, arg
, NULL
);
2854 if (d_peek_char (di
) == terminator
)
2864 /* Returns nonzero iff OP is an operator for a C++ cast: const_cast,
2865 dynamic_cast, static_cast or reinterpret_cast. */
2868 op_is_new_cast (struct demangle_component
*op
)
2870 const char *code
= op
->u
.s_operator
.op
->code
;
2871 return (code
[1] == 'c'
2872 && (code
[0] == 's' || code
[0] == 'd'
2873 || code
[0] == 'c' || code
[0] == 'r'));
2876 /* <expression> ::= <(unary) operator-name> <expression>
2877 ::= <(binary) operator-name> <expression> <expression>
2878 ::= <(trinary) operator-name> <expression> <expression> <expression>
2879 ::= cl <expression>+ E
2881 ::= <template-param>
2882 ::= sr <type> <unqualified-name>
2883 ::= sr <type> <unqualified-name> <template-args>
2887 static struct demangle_component
*
2888 d_expression (struct d_info
*di
)
2892 peek
= d_peek_char (di
);
2894 return d_expr_primary (di
);
2895 else if (peek
== 'T')
2896 return d_template_param (di
);
2897 else if (peek
== 's' && d_peek_next_char (di
) == 'r')
2899 struct demangle_component
*type
;
2900 struct demangle_component
*name
;
2903 type
= cplus_demangle_type (di
);
2904 name
= d_unqualified_name (di
);
2905 if (d_peek_char (di
) != 'I')
2906 return d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
, type
, name
);
2908 return d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
, type
,
2909 d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, name
,
2910 d_template_args (di
)));
2912 else if (peek
== 's' && d_peek_next_char (di
) == 'p')
2915 return d_make_comp (di
, DEMANGLE_COMPONENT_PACK_EXPANSION
,
2916 d_expression (di
), NULL
);
2918 else if (peek
== 'f' && d_peek_next_char (di
) == 'p')
2920 /* Function parameter used in a late-specified return type. */
2923 if (d_peek_char (di
) == 'T')
2925 /* 'this' parameter. */
2931 index
= d_compact_number (di
) + 1;
2935 return d_make_function_param (di
, index
);
2937 else if (IS_DIGIT (peek
)
2938 || (peek
== 'o' && d_peek_next_char (di
) == 'n'))
2940 /* We can get an unqualified name as an expression in the case of
2941 a dependent function call, i.e. decltype(f(t)). */
2942 struct demangle_component
*name
;
2945 /* operator-function-id, i.e. operator+(t). */
2948 name
= d_unqualified_name (di
);
2951 if (d_peek_char (di
) == 'I')
2952 return d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, name
,
2953 d_template_args (di
));
2957 else if ((peek
== 'i' || peek
== 't')
2958 && d_peek_next_char (di
) == 'l')
2960 /* Brace-enclosed initializer list, untyped or typed. */
2961 struct demangle_component
*type
= NULL
;
2963 type
= cplus_demangle_type (di
);
2965 return d_make_comp (di
, DEMANGLE_COMPONENT_INITIALIZER_LIST
,
2966 type
, d_exprlist (di
, 'E'));
2970 struct demangle_component
*op
;
2971 const char *code
= NULL
;
2974 op
= d_operator_name (di
);
2978 if (op
->type
== DEMANGLE_COMPONENT_OPERATOR
)
2980 code
= op
->u
.s_operator
.op
->code
;
2981 di
->expansion
+= op
->u
.s_operator
.op
->len
- 2;
2982 if (strcmp (code
, "st") == 0)
2983 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
2984 cplus_demangle_type (di
));
2991 case DEMANGLE_COMPONENT_OPERATOR
:
2992 args
= op
->u
.s_operator
.op
->args
;
2994 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
2995 args
= op
->u
.s_extended_operator
.args
;
2997 case DEMANGLE_COMPONENT_CAST
:
3005 return d_make_comp (di
, DEMANGLE_COMPONENT_NULLARY
, op
, NULL
);
3009 struct demangle_component
*operand
;
3012 if (code
&& (code
[0] == 'p' || code
[0] == 'm')
3013 && code
[1] == code
[0])
3014 /* pp_ and mm_ are the prefix variants. */
3015 suffix
= !d_check_char (di
, '_');
3017 if (op
->type
== DEMANGLE_COMPONENT_CAST
3018 && d_check_char (di
, '_'))
3019 operand
= d_exprlist (di
, 'E');
3021 operand
= d_expression (di
);
3024 /* Indicate the suffix variant for d_print_comp. */
3025 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
3027 DEMANGLE_COMPONENT_BINARY_ARGS
,
3030 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
3035 struct demangle_component
*left
;
3036 struct demangle_component
*right
;
3038 if (op_is_new_cast (op
))
3039 left
= cplus_demangle_type (di
);
3041 left
= d_expression (di
);
3042 if (!strcmp (code
, "cl"))
3043 right
= d_exprlist (di
, 'E');
3044 else if (!strcmp (code
, "dt") || !strcmp (code
, "pt"))
3046 right
= d_unqualified_name (di
);
3047 if (d_peek_char (di
) == 'I')
3048 right
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
,
3049 right
, d_template_args (di
));
3052 right
= d_expression (di
);
3054 return d_make_comp (di
, DEMANGLE_COMPONENT_BINARY
, op
,
3056 DEMANGLE_COMPONENT_BINARY_ARGS
,
3061 struct demangle_component
*first
;
3062 struct demangle_component
*second
;
3063 struct demangle_component
*third
;
3065 if (!strcmp (code
, "qu"))
3067 /* ?: expression. */
3068 first
= d_expression (di
);
3069 second
= d_expression (di
);
3070 third
= d_expression (di
);
3072 else if (code
[0] == 'n')
3074 /* new-expression. */
3075 if (code
[1] != 'w' && code
[1] != 'a')
3077 first
= d_exprlist (di
, '_');
3078 second
= cplus_demangle_type (di
);
3079 if (d_peek_char (di
) == 'E')
3084 else if (d_peek_char (di
) == 'p'
3085 && d_peek_next_char (di
) == 'i')
3087 /* Parenthesized initializer. */
3089 third
= d_exprlist (di
, 'E');
3091 else if (d_peek_char (di
) == 'i'
3092 && d_peek_next_char (di
) == 'l')
3093 /* initializer-list. */
3094 third
= d_expression (di
);
3100 return d_make_comp (di
, DEMANGLE_COMPONENT_TRINARY
, op
,
3102 DEMANGLE_COMPONENT_TRINARY_ARG1
,
3105 DEMANGLE_COMPONENT_TRINARY_ARG2
,
3114 /* <expr-primary> ::= L <type> <(value) number> E
3115 ::= L <type> <(value) float> E
3116 ::= L <mangled-name> E
3119 static struct demangle_component
*
3120 d_expr_primary (struct d_info
*di
)
3122 struct demangle_component
*ret
;
3124 if (! d_check_char (di
, 'L'))
3126 if (d_peek_char (di
) == '_'
3127 /* Workaround for G++ bug; see comment in write_template_arg. */
3128 || d_peek_char (di
) == 'Z')
3129 ret
= cplus_demangle_mangled_name (di
, 0);
3132 struct demangle_component
*type
;
3133 enum demangle_component_type t
;
3136 type
= cplus_demangle_type (di
);
3140 /* If we have a type we know how to print, we aren't going to
3141 print the type name itself. */
3142 if (type
->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
3143 && type
->u
.s_builtin
.type
->print
!= D_PRINT_DEFAULT
)
3144 di
->expansion
-= type
->u
.s_builtin
.type
->len
;
3146 /* Rather than try to interpret the literal value, we just
3147 collect it as a string. Note that it's possible to have a
3148 floating point literal here. The ABI specifies that the
3149 format of such literals is machine independent. That's fine,
3150 but what's not fine is that versions of g++ up to 3.2 with
3151 -fabi-version=1 used upper case letters in the hex constant,
3152 and dumped out gcc's internal representation. That makes it
3153 hard to tell where the constant ends, and hard to dump the
3154 constant in any readable form anyhow. We don't attempt to
3155 handle these cases. */
3157 t
= DEMANGLE_COMPONENT_LITERAL
;
3158 if (d_peek_char (di
) == 'n')
3160 t
= DEMANGLE_COMPONENT_LITERAL_NEG
;
3164 while (d_peek_char (di
) != 'E')
3166 if (d_peek_char (di
) == '\0')
3170 ret
= d_make_comp (di
, t
, type
, d_make_name (di
, s
, d_str (di
) - s
));
3172 if (! d_check_char (di
, 'E'))
3177 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
3178 ::= Z <(function) encoding> E s [<discriminator>]
3179 ::= Z <(function) encoding> E d [<parameter> number>] _ <entity name>
3182 static struct demangle_component
*
3183 d_local_name (struct d_info
*di
)
3185 struct demangle_component
*function
;
3187 if (! d_check_char (di
, 'Z'))
3190 function
= d_encoding (di
, 0);
3192 if (! d_check_char (di
, 'E'))
3195 if (d_peek_char (di
) == 's')
3198 if (! d_discriminator (di
))
3200 return d_make_comp (di
, DEMANGLE_COMPONENT_LOCAL_NAME
, function
,
3201 d_make_name (di
, "string literal",
3202 sizeof "string literal" - 1));
3206 struct demangle_component
*name
;
3209 if (d_peek_char (di
) == 'd')
3211 /* Default argument scope: d <number> _. */
3213 num
= d_compact_number (di
);
3222 /* Lambdas and unnamed types have internal discriminators. */
3223 case DEMANGLE_COMPONENT_LAMBDA
:
3224 case DEMANGLE_COMPONENT_UNNAMED_TYPE
:
3227 if (! d_discriminator (di
))
3231 name
= d_make_default_arg (di
, num
, name
);
3232 return d_make_comp (di
, DEMANGLE_COMPONENT_LOCAL_NAME
, function
, name
);
3236 /* <discriminator> ::= _ <(non-negative) number>
3238 We demangle the discriminator, but we don't print it out. FIXME:
3239 We should print it out in verbose mode. */
3242 d_discriminator (struct d_info
*di
)
3246 if (d_peek_char (di
) != '_')
3249 discrim
= d_number (di
);
3255 /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ */
3257 static struct demangle_component
*
3258 d_lambda (struct d_info
*di
)
3260 struct demangle_component
*tl
;
3261 struct demangle_component
*ret
;
3264 if (! d_check_char (di
, 'U'))
3266 if (! d_check_char (di
, 'l'))
3269 tl
= d_parmlist (di
);
3273 if (! d_check_char (di
, 'E'))
3276 num
= d_compact_number (di
);
3280 ret
= d_make_empty (di
);
3283 ret
->type
= DEMANGLE_COMPONENT_LAMBDA
;
3284 ret
->u
.s_unary_num
.sub
= tl
;
3285 ret
->u
.s_unary_num
.num
= num
;
3288 if (! d_add_substitution (di
, ret
))
3294 /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
3296 static struct demangle_component
*
3297 d_unnamed_type (struct d_info
*di
)
3299 struct demangle_component
*ret
;
3302 if (! d_check_char (di
, 'U'))
3304 if (! d_check_char (di
, 't'))
3307 num
= d_compact_number (di
);
3311 ret
= d_make_empty (di
);
3314 ret
->type
= DEMANGLE_COMPONENT_UNNAMED_TYPE
;
3315 ret
->u
.s_number
.number
= num
;
3318 if (! d_add_substitution (di
, ret
))
3324 /* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]*
3327 static struct demangle_component
*
3328 d_clone_suffix (struct d_info
*di
, struct demangle_component
*encoding
)
3330 const char *suffix
= d_str (di
);
3331 const char *pend
= suffix
;
3332 struct demangle_component
*n
;
3334 if (*pend
== '.' && (IS_LOWER (pend
[1]) || pend
[1] == '_'))
3337 while (IS_LOWER (*pend
) || *pend
== '_')
3340 while (*pend
== '.' && IS_DIGIT (pend
[1]))
3343 while (IS_DIGIT (*pend
))
3346 d_advance (di
, pend
- suffix
);
3347 n
= d_make_name (di
, suffix
, pend
- suffix
);
3348 return d_make_comp (di
, DEMANGLE_COMPONENT_CLONE
, encoding
, n
);
3351 /* Add a new substitution. */
3354 d_add_substitution (struct d_info
*di
, struct demangle_component
*dc
)
3358 if (di
->next_sub
>= di
->num_subs
)
3360 di
->subs
[di
->next_sub
] = dc
;
3365 /* <substitution> ::= S <seq-id> _
3375 If PREFIX is non-zero, then this type is being used as a prefix in
3376 a qualified name. In this case, for the standard substitutions, we
3377 need to check whether we are being used as a prefix for a
3378 constructor or destructor, and return a full template name.
3379 Otherwise we will get something like std::iostream::~iostream()
3380 which does not correspond particularly well to any function which
3381 actually appears in the source.
3384 static const struct d_standard_sub_info standard_subs
[] =
3389 { 'a', NL ("std::allocator"),
3390 NL ("std::allocator"),
3392 { 'b', NL ("std::basic_string"),
3393 NL ("std::basic_string"),
3394 NL ("basic_string") },
3395 { 's', NL ("std::string"),
3396 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
3397 NL ("basic_string") },
3398 { 'i', NL ("std::istream"),
3399 NL ("std::basic_istream<char, std::char_traits<char> >"),
3400 NL ("basic_istream") },
3401 { 'o', NL ("std::ostream"),
3402 NL ("std::basic_ostream<char, std::char_traits<char> >"),
3403 NL ("basic_ostream") },
3404 { 'd', NL ("std::iostream"),
3405 NL ("std::basic_iostream<char, std::char_traits<char> >"),
3406 NL ("basic_iostream") }
3409 static struct demangle_component
*
3410 d_substitution (struct d_info
*di
, int prefix
)
3414 if (! d_check_char (di
, 'S'))
3417 c
= d_next_char (di
);
3418 if (c
== '_' || IS_DIGIT (c
) || IS_UPPER (c
))
3427 unsigned int new_id
;
3430 new_id
= id
* 36 + c
- '0';
3431 else if (IS_UPPER (c
))
3432 new_id
= id
* 36 + c
- 'A' + 10;
3438 c
= d_next_char (di
);
3445 if (id
>= (unsigned int) di
->next_sub
)
3450 return di
->subs
[id
];
3455 const struct d_standard_sub_info
*p
;
3456 const struct d_standard_sub_info
*pend
;
3458 verbose
= (di
->options
& DMGL_VERBOSE
) != 0;
3459 if (! verbose
&& prefix
)
3463 peek
= d_peek_char (di
);
3464 if (peek
== 'C' || peek
== 'D')
3468 pend
= (&standard_subs
[0]
3469 + sizeof standard_subs
/ sizeof standard_subs
[0]);
3470 for (p
= &standard_subs
[0]; p
< pend
; ++p
)
3477 if (p
->set_last_name
!= NULL
)
3478 di
->last_name
= d_make_sub (di
, p
->set_last_name
,
3479 p
->set_last_name_len
);
3482 s
= p
->full_expansion
;
3487 s
= p
->simple_expansion
;
3488 len
= p
->simple_len
;
3490 di
->expansion
+= len
;
3491 return d_make_sub (di
, s
, len
);
3499 /* Initialize a growable string. */
3502 d_growable_string_init (struct d_growable_string
*dgs
, size_t estimate
)
3507 dgs
->allocation_failure
= 0;
3510 d_growable_string_resize (dgs
, estimate
);
3513 /* Grow a growable string to a given size. */
3516 d_growable_string_resize (struct d_growable_string
*dgs
, size_t need
)
3521 if (dgs
->allocation_failure
)
3524 /* Start allocation at two bytes to avoid any possibility of confusion
3525 with the special value of 1 used as a return in *palc to indicate
3526 allocation failures. */
3527 newalc
= dgs
->alc
> 0 ? dgs
->alc
: 2;
3528 while (newalc
< need
)
3531 newbuf
= (char *) realloc (dgs
->buf
, newalc
);
3538 dgs
->allocation_failure
= 1;
3545 /* Append a buffer to a growable string. */
3548 d_growable_string_append_buffer (struct d_growable_string
*dgs
,
3549 const char *s
, size_t l
)
3553 need
= dgs
->len
+ l
+ 1;
3554 if (need
> dgs
->alc
)
3555 d_growable_string_resize (dgs
, need
);
3557 if (dgs
->allocation_failure
)
3560 memcpy (dgs
->buf
+ dgs
->len
, s
, l
);
3561 dgs
->buf
[dgs
->len
+ l
] = '\0';
3565 /* Bridge growable strings to the callback mechanism. */
3568 d_growable_string_callback_adapter (const char *s
, size_t l
, void *opaque
)
3570 struct d_growable_string
*dgs
= (struct d_growable_string
*) opaque
;
3572 d_growable_string_append_buffer (dgs
, s
, l
);
3575 /* Initialize a print information structure. */
3578 d_print_init (struct d_print_info
*dpi
, demangle_callbackref callback
,
3582 dpi
->last_char
= '\0';
3583 dpi
->templates
= NULL
;
3584 dpi
->modifiers
= NULL
;
3585 dpi
->pack_index
= 0;
3586 dpi
->flush_count
= 0;
3588 dpi
->callback
= callback
;
3589 dpi
->opaque
= opaque
;
3591 dpi
->demangle_failure
= 0;
3594 /* Indicate that an error occurred during printing, and test for error. */
3597 d_print_error (struct d_print_info
*dpi
)
3599 dpi
->demangle_failure
= 1;
3603 d_print_saw_error (struct d_print_info
*dpi
)
3605 return dpi
->demangle_failure
!= 0;
3608 /* Flush buffered characters to the callback. */
3611 d_print_flush (struct d_print_info
*dpi
)
3613 dpi
->buf
[dpi
->len
] = '\0';
3614 dpi
->callback (dpi
->buf
, dpi
->len
, dpi
->opaque
);
3619 /* Append characters and buffers for printing. */
3622 d_append_char (struct d_print_info
*dpi
, char c
)
3624 if (dpi
->len
== sizeof (dpi
->buf
) - 1)
3625 d_print_flush (dpi
);
3627 dpi
->buf
[dpi
->len
++] = c
;
3632 d_append_buffer (struct d_print_info
*dpi
, const char *s
, size_t l
)
3636 for (i
= 0; i
< l
; i
++)
3637 d_append_char (dpi
, s
[i
]);
3641 d_append_string (struct d_print_info
*dpi
, const char *s
)
3643 d_append_buffer (dpi
, s
, strlen (s
));
3647 d_append_num (struct d_print_info
*dpi
, long l
)
3650 sprintf (buf
,"%ld", l
);
3651 d_append_string (dpi
, buf
);
3655 d_last_char (struct d_print_info
*dpi
)
3657 return dpi
->last_char
;
3660 /* Turn components into a human readable string. OPTIONS is the
3661 options bits passed to the demangler. DC is the tree to print.
3662 CALLBACK is a function to call to flush demangled string segments
3663 as they fill the intermediate buffer, and OPAQUE is a generalized
3664 callback argument. On success, this returns 1. On failure,
3665 it returns 0, indicating a bad parse. It does not use heap
3666 memory to build an output string, so cannot encounter memory
3667 allocation failure. */
3669 CP_STATIC_IF_GLIBCPP_V3
3671 cplus_demangle_print_callback (int options
,
3672 const struct demangle_component
*dc
,
3673 demangle_callbackref callback
, void *opaque
)
3675 struct d_print_info dpi
;
3677 d_print_init (&dpi
, callback
, opaque
);
3679 d_print_comp (&dpi
, options
, dc
);
3681 d_print_flush (&dpi
);
3683 return ! d_print_saw_error (&dpi
);
3686 /* Turn components into a human readable string. OPTIONS is the
3687 options bits passed to the demangler. DC is the tree to print.
3688 ESTIMATE is a guess at the length of the result. This returns a
3689 string allocated by malloc, or NULL on error. On success, this
3690 sets *PALC to the size of the allocated buffer. On failure, this
3691 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
3694 CP_STATIC_IF_GLIBCPP_V3
3696 cplus_demangle_print (int options
, const struct demangle_component
*dc
,
3697 int estimate
, size_t *palc
)
3699 struct d_growable_string dgs
;
3701 d_growable_string_init (&dgs
, estimate
);
3703 if (! cplus_demangle_print_callback (options
, dc
,
3704 d_growable_string_callback_adapter
,
3712 *palc
= dgs
.allocation_failure
? 1 : dgs
.alc
;
3716 /* Returns the I'th element of the template arglist ARGS, or NULL on
3719 static struct demangle_component
*
3720 d_index_template_argument (struct demangle_component
*args
, int i
)
3722 struct demangle_component
*a
;
3728 if (a
->type
!= DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
3734 if (i
!= 0 || a
== NULL
)
3740 /* Returns the template argument from the current context indicated by DC,
3741 which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
3743 static struct demangle_component
*
3744 d_lookup_template_argument (struct d_print_info
*dpi
,
3745 const struct demangle_component
*dc
)
3747 if (dpi
->templates
== NULL
)
3749 d_print_error (dpi
);
3753 return d_index_template_argument
3754 (d_right (dpi
->templates
->template_decl
),
3755 dc
->u
.s_number
.number
);
3758 /* Returns a template argument pack used in DC (any will do), or NULL. */
3760 static struct demangle_component
*
3761 d_find_pack (struct d_print_info
*dpi
,
3762 const struct demangle_component
*dc
)
3764 struct demangle_component
*a
;
3770 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
3771 a
= d_lookup_template_argument (dpi
, dc
);
3772 if (a
&& a
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
3776 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
3779 case DEMANGLE_COMPONENT_LAMBDA
:
3780 case DEMANGLE_COMPONENT_NAME
:
3781 case DEMANGLE_COMPONENT_TAGGED_NAME
:
3782 case DEMANGLE_COMPONENT_OPERATOR
:
3783 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
3784 case DEMANGLE_COMPONENT_SUB_STD
:
3785 case DEMANGLE_COMPONENT_CHARACTER
:
3786 case DEMANGLE_COMPONENT_FUNCTION_PARAM
:
3787 case DEMANGLE_COMPONENT_UNNAMED_TYPE
:
3790 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
3791 return d_find_pack (dpi
, dc
->u
.s_extended_operator
.name
);
3792 case DEMANGLE_COMPONENT_CTOR
:
3793 return d_find_pack (dpi
, dc
->u
.s_ctor
.name
);
3794 case DEMANGLE_COMPONENT_DTOR
:
3795 return d_find_pack (dpi
, dc
->u
.s_dtor
.name
);
3798 a
= d_find_pack (dpi
, d_left (dc
));
3801 return d_find_pack (dpi
, d_right (dc
));
3805 /* Returns the length of the template argument pack DC. */
3808 d_pack_length (const struct demangle_component
*dc
)
3811 while (dc
&& dc
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
3812 && d_left (dc
) != NULL
)
3820 /* DC is a component of a mangled expression. Print it, wrapped in parens
3824 d_print_subexpr (struct d_print_info
*dpi
, int options
,
3825 const struct demangle_component
*dc
)
3828 if (dc
->type
== DEMANGLE_COMPONENT_NAME
3829 || dc
->type
== DEMANGLE_COMPONENT_QUAL_NAME
3830 || dc
->type
== DEMANGLE_COMPONENT_INITIALIZER_LIST
3831 || dc
->type
== DEMANGLE_COMPONENT_FUNCTION_PARAM
)
3834 d_append_char (dpi
, '(');
3835 d_print_comp (dpi
, options
, dc
);
3837 d_append_char (dpi
, ')');
3840 /* Subroutine to handle components. */
3843 d_print_comp (struct d_print_info
*dpi
, int options
,
3844 const struct demangle_component
*dc
)
3846 /* Magic variable to let reference smashing skip over the next modifier
3847 without needing to modify *dc. */
3848 const struct demangle_component
*mod_inner
= NULL
;
3852 d_print_error (dpi
);
3855 if (d_print_saw_error (dpi
))
3860 case DEMANGLE_COMPONENT_NAME
:
3861 if ((options
& DMGL_JAVA
) == 0)
3862 d_append_buffer (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
3864 d_print_java_identifier (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
3867 case DEMANGLE_COMPONENT_TAGGED_NAME
:
3868 d_print_comp (dpi
, options
, d_left (dc
));
3869 d_append_string (dpi
, "[abi:");
3870 d_print_comp (dpi
, options
, d_right (dc
));
3871 d_append_char (dpi
, ']');
3874 case DEMANGLE_COMPONENT_QUAL_NAME
:
3875 case DEMANGLE_COMPONENT_LOCAL_NAME
:
3876 d_print_comp (dpi
, options
, d_left (dc
));
3877 if ((options
& DMGL_JAVA
) == 0)
3878 d_append_string (dpi
, "::");
3880 d_append_char (dpi
, '.');
3882 struct demangle_component
*local_name
= d_right (dc
);
3883 if (local_name
->type
== DEMANGLE_COMPONENT_DEFAULT_ARG
)
3885 d_append_string (dpi
, "{default arg#");
3886 d_append_num (dpi
, local_name
->u
.s_unary_num
.num
+ 1);
3887 d_append_string (dpi
, "}::");
3888 local_name
= local_name
->u
.s_unary_num
.sub
;
3890 d_print_comp (dpi
, options
, local_name
);
3894 case DEMANGLE_COMPONENT_TYPED_NAME
:
3896 struct d_print_mod
*hold_modifiers
;
3897 struct demangle_component
*typed_name
;
3898 struct d_print_mod adpm
[4];
3900 struct d_print_template dpt
;
3902 /* Pass the name down to the type so that it can be printed in
3903 the right place for the type. We also have to pass down
3904 any CV-qualifiers, which apply to the this parameter. */
3905 hold_modifiers
= dpi
->modifiers
;
3908 typed_name
= d_left (dc
);
3909 while (typed_name
!= NULL
)
3911 if (i
>= sizeof adpm
/ sizeof adpm
[0])
3913 d_print_error (dpi
);
3917 adpm
[i
].next
= dpi
->modifiers
;
3918 dpi
->modifiers
= &adpm
[i
];
3919 adpm
[i
].mod
= typed_name
;
3920 adpm
[i
].printed
= 0;
3921 adpm
[i
].templates
= dpi
->templates
;
3924 if (typed_name
->type
!= DEMANGLE_COMPONENT_RESTRICT_THIS
3925 && typed_name
->type
!= DEMANGLE_COMPONENT_VOLATILE_THIS
3926 && typed_name
->type
!= DEMANGLE_COMPONENT_CONST_THIS
)
3929 typed_name
= d_left (typed_name
);
3932 if (typed_name
== NULL
)
3934 d_print_error (dpi
);
3938 /* If typed_name is a template, then it applies to the
3939 function type as well. */
3940 if (typed_name
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
3942 dpt
.next
= dpi
->templates
;
3943 dpi
->templates
= &dpt
;
3944 dpt
.template_decl
= typed_name
;
3947 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
3948 there may be CV-qualifiers on its right argument which
3949 really apply here; this happens when parsing a class which
3950 is local to a function. */
3951 if (typed_name
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
3953 struct demangle_component
*local_name
;
3955 local_name
= d_right (typed_name
);
3956 if (local_name
->type
== DEMANGLE_COMPONENT_DEFAULT_ARG
)
3957 local_name
= local_name
->u
.s_unary_num
.sub
;
3958 while (local_name
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
3959 || local_name
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
3960 || local_name
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
3962 if (i
>= sizeof adpm
/ sizeof adpm
[0])
3964 d_print_error (dpi
);
3968 adpm
[i
] = adpm
[i
- 1];
3969 adpm
[i
].next
= &adpm
[i
- 1];
3970 dpi
->modifiers
= &adpm
[i
];
3972 adpm
[i
- 1].mod
= local_name
;
3973 adpm
[i
- 1].printed
= 0;
3974 adpm
[i
- 1].templates
= dpi
->templates
;
3977 local_name
= d_left (local_name
);
3981 d_print_comp (dpi
, options
, d_right (dc
));
3983 if (typed_name
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
3984 dpi
->templates
= dpt
.next
;
3986 /* If the modifiers didn't get printed by the type, print them
3991 if (! adpm
[i
].printed
)
3993 d_append_char (dpi
, ' ');
3994 d_print_mod (dpi
, options
, adpm
[i
].mod
);
3998 dpi
->modifiers
= hold_modifiers
;
4003 case DEMANGLE_COMPONENT_TEMPLATE
:
4005 struct d_print_mod
*hold_dpm
;
4006 struct demangle_component
*dcl
;
4008 /* Don't push modifiers into a template definition. Doing so
4009 could give the wrong definition for a template argument.
4010 Instead, treat the template essentially as a name. */
4012 hold_dpm
= dpi
->modifiers
;
4013 dpi
->modifiers
= NULL
;
4017 if ((options
& DMGL_JAVA
) != 0
4018 && dcl
->type
== DEMANGLE_COMPONENT_NAME
4019 && dcl
->u
.s_name
.len
== 6
4020 && strncmp (dcl
->u
.s_name
.s
, "JArray", 6) == 0)
4022 /* Special-case Java arrays, so that JArray<TYPE> appears
4023 instead as TYPE[]. */
4025 d_print_comp (dpi
, options
, d_right (dc
));
4026 d_append_string (dpi
, "[]");
4030 d_print_comp (dpi
, options
, dcl
);
4031 if (d_last_char (dpi
) == '<')
4032 d_append_char (dpi
, ' ');
4033 d_append_char (dpi
, '<');
4034 d_print_comp (dpi
, options
, d_right (dc
));
4035 /* Avoid generating two consecutive '>' characters, to avoid
4036 the C++ syntactic ambiguity. */
4037 if (d_last_char (dpi
) == '>')
4038 d_append_char (dpi
, ' ');
4039 d_append_char (dpi
, '>');
4042 dpi
->modifiers
= hold_dpm
;
4047 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
4049 struct d_print_template
*hold_dpt
;
4050 struct demangle_component
*a
= d_lookup_template_argument (dpi
, dc
);
4052 if (a
&& a
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
4053 a
= d_index_template_argument (a
, dpi
->pack_index
);
4057 d_print_error (dpi
);
4061 /* While processing this parameter, we need to pop the list of
4062 templates. This is because the template parameter may
4063 itself be a reference to a parameter of an outer
4066 hold_dpt
= dpi
->templates
;
4067 dpi
->templates
= hold_dpt
->next
;
4069 d_print_comp (dpi
, options
, a
);
4071 dpi
->templates
= hold_dpt
;
4076 case DEMANGLE_COMPONENT_CTOR
:
4077 d_print_comp (dpi
, options
, dc
->u
.s_ctor
.name
);
4080 case DEMANGLE_COMPONENT_DTOR
:
4081 d_append_char (dpi
, '~');
4082 d_print_comp (dpi
, options
, dc
->u
.s_dtor
.name
);
4085 case DEMANGLE_COMPONENT_VTABLE
:
4086 d_append_string (dpi
, "vtable for ");
4087 d_print_comp (dpi
, options
, d_left (dc
));
4090 case DEMANGLE_COMPONENT_VTT
:
4091 d_append_string (dpi
, "VTT for ");
4092 d_print_comp (dpi
, options
, d_left (dc
));
4095 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
4096 d_append_string (dpi
, "construction vtable for ");
4097 d_print_comp (dpi
, options
, d_left (dc
));
4098 d_append_string (dpi
, "-in-");
4099 d_print_comp (dpi
, options
, d_right (dc
));
4102 case DEMANGLE_COMPONENT_TYPEINFO
:
4103 d_append_string (dpi
, "typeinfo for ");
4104 d_print_comp (dpi
, options
, d_left (dc
));
4107 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
4108 d_append_string (dpi
, "typeinfo name for ");
4109 d_print_comp (dpi
, options
, d_left (dc
));
4112 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
4113 d_append_string (dpi
, "typeinfo fn for ");
4114 d_print_comp (dpi
, options
, d_left (dc
));
4117 case DEMANGLE_COMPONENT_THUNK
:
4118 d_append_string (dpi
, "non-virtual thunk to ");
4119 d_print_comp (dpi
, options
, d_left (dc
));
4122 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
4123 d_append_string (dpi
, "virtual thunk to ");
4124 d_print_comp (dpi
, options
, d_left (dc
));
4127 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
4128 d_append_string (dpi
, "covariant return thunk to ");
4129 d_print_comp (dpi
, options
, d_left (dc
));
4132 case DEMANGLE_COMPONENT_JAVA_CLASS
:
4133 d_append_string (dpi
, "java Class for ");
4134 d_print_comp (dpi
, options
, d_left (dc
));
4137 case DEMANGLE_COMPONENT_GUARD
:
4138 d_append_string (dpi
, "guard variable for ");
4139 d_print_comp (dpi
, options
, d_left (dc
));
4142 case DEMANGLE_COMPONENT_TLS_INIT
:
4143 d_append_string (dpi
, "TLS init function for ");
4144 d_print_comp (dpi
, options
, d_left (dc
));
4147 case DEMANGLE_COMPONENT_TLS_WRAPPER
:
4148 d_append_string (dpi
, "TLS wrapper function for ");
4149 d_print_comp (dpi
, options
, d_left (dc
));
4152 case DEMANGLE_COMPONENT_REFTEMP
:
4153 d_append_string (dpi
, "reference temporary #");
4154 d_print_comp (dpi
, options
, d_right (dc
));
4155 d_append_string (dpi
, " for ");
4156 d_print_comp (dpi
, options
, d_left (dc
));
4159 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
4160 d_append_string (dpi
, "hidden alias for ");
4161 d_print_comp (dpi
, options
, d_left (dc
));
4164 case DEMANGLE_COMPONENT_TRANSACTION_CLONE
:
4165 d_append_string (dpi
, "transaction clone for ");
4166 d_print_comp (dpi
, options
, d_left (dc
));
4169 case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE
:
4170 d_append_string (dpi
, "non-transaction clone for ");
4171 d_print_comp (dpi
, options
, d_left (dc
));
4174 case DEMANGLE_COMPONENT_SUB_STD
:
4175 d_append_buffer (dpi
, dc
->u
.s_string
.string
, dc
->u
.s_string
.len
);
4178 case DEMANGLE_COMPONENT_RESTRICT
:
4179 case DEMANGLE_COMPONENT_VOLATILE
:
4180 case DEMANGLE_COMPONENT_CONST
:
4182 struct d_print_mod
*pdpm
;
4184 /* When printing arrays, it's possible to have cases where the
4185 same CV-qualifier gets pushed on the stack multiple times.
4186 We only need to print it once. */
4188 for (pdpm
= dpi
->modifiers
; pdpm
!= NULL
; pdpm
= pdpm
->next
)
4190 if (! pdpm
->printed
)
4192 if (pdpm
->mod
->type
!= DEMANGLE_COMPONENT_RESTRICT
4193 && pdpm
->mod
->type
!= DEMANGLE_COMPONENT_VOLATILE
4194 && pdpm
->mod
->type
!= DEMANGLE_COMPONENT_CONST
)
4196 if (pdpm
->mod
->type
== dc
->type
)
4198 d_print_comp (dpi
, options
, d_left (dc
));
4206 case DEMANGLE_COMPONENT_REFERENCE
:
4207 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
4209 /* Handle reference smashing: & + && = &. */
4210 const struct demangle_component
*sub
= d_left (dc
);
4211 if (sub
->type
== DEMANGLE_COMPONENT_TEMPLATE_PARAM
)
4213 struct demangle_component
*a
= d_lookup_template_argument (dpi
, sub
);
4214 if (a
&& a
->type
== DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
4215 a
= d_index_template_argument (a
, dpi
->pack_index
);
4219 d_print_error (dpi
);
4226 if (sub
->type
== DEMANGLE_COMPONENT_REFERENCE
4227 || sub
->type
== dc
->type
)
4229 else if (sub
->type
== DEMANGLE_COMPONENT_RVALUE_REFERENCE
)
4230 mod_inner
= d_left (sub
);
4234 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
4235 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
4236 case DEMANGLE_COMPONENT_CONST_THIS
:
4237 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
4238 case DEMANGLE_COMPONENT_POINTER
:
4239 case DEMANGLE_COMPONENT_COMPLEX
:
4240 case DEMANGLE_COMPONENT_IMAGINARY
:
4243 /* We keep a list of modifiers on the stack. */
4244 struct d_print_mod dpm
;
4246 dpm
.next
= dpi
->modifiers
;
4247 dpi
->modifiers
= &dpm
;
4250 dpm
.templates
= dpi
->templates
;
4253 mod_inner
= d_left (dc
);
4255 d_print_comp (dpi
, options
, mod_inner
);
4257 /* If the modifier didn't get printed by the type, print it
4260 d_print_mod (dpi
, options
, dc
);
4262 dpi
->modifiers
= dpm
.next
;
4267 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
4268 if ((options
& DMGL_JAVA
) == 0)
4269 d_append_buffer (dpi
, dc
->u
.s_builtin
.type
->name
,
4270 dc
->u
.s_builtin
.type
->len
);
4272 d_append_buffer (dpi
, dc
->u
.s_builtin
.type
->java_name
,
4273 dc
->u
.s_builtin
.type
->java_len
);
4276 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
4277 d_print_comp (dpi
, options
, d_left (dc
));
4280 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
4282 if ((options
& DMGL_RET_POSTFIX
) != 0)
4283 d_print_function_type (dpi
,
4284 options
& ~(DMGL_RET_POSTFIX
| DMGL_RET_DROP
),
4285 dc
, dpi
->modifiers
);
4287 /* Print return type if present */
4288 if (d_left (dc
) != NULL
&& (options
& DMGL_RET_POSTFIX
) != 0)
4289 d_print_comp (dpi
, options
& ~(DMGL_RET_POSTFIX
| DMGL_RET_DROP
),
4291 else if (d_left (dc
) != NULL
&& (options
& DMGL_RET_DROP
) == 0)
4293 struct d_print_mod dpm
;
4295 /* We must pass this type down as a modifier in order to
4296 print it in the right location. */
4297 dpm
.next
= dpi
->modifiers
;
4298 dpi
->modifiers
= &dpm
;
4301 dpm
.templates
= dpi
->templates
;
4303 d_print_comp (dpi
, options
& ~(DMGL_RET_POSTFIX
| DMGL_RET_DROP
),
4306 dpi
->modifiers
= dpm
.next
;
4311 /* In standard prefix notation, there is a space between the
4312 return type and the function signature. */
4313 if ((options
& DMGL_RET_POSTFIX
) == 0)
4314 d_append_char (dpi
, ' ');
4317 if ((options
& DMGL_RET_POSTFIX
) == 0)
4318 d_print_function_type (dpi
,
4319 options
& ~(DMGL_RET_POSTFIX
| DMGL_RET_DROP
),
4320 dc
, dpi
->modifiers
);
4325 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
4327 struct d_print_mod
*hold_modifiers
;
4328 struct d_print_mod adpm
[4];
4330 struct d_print_mod
*pdpm
;
4332 /* We must pass this type down as a modifier in order to print
4333 multi-dimensional arrays correctly. If the array itself is
4334 CV-qualified, we act as though the element type were
4335 CV-qualified. We do this by copying the modifiers down
4336 rather than fiddling pointers, so that we don't wind up
4337 with a d_print_mod higher on the stack pointing into our
4338 stack frame after we return. */
4340 hold_modifiers
= dpi
->modifiers
;
4342 adpm
[0].next
= hold_modifiers
;
4343 dpi
->modifiers
= &adpm
[0];
4345 adpm
[0].printed
= 0;
4346 adpm
[0].templates
= dpi
->templates
;
4349 pdpm
= hold_modifiers
;
4351 && (pdpm
->mod
->type
== DEMANGLE_COMPONENT_RESTRICT
4352 || pdpm
->mod
->type
== DEMANGLE_COMPONENT_VOLATILE
4353 || pdpm
->mod
->type
== DEMANGLE_COMPONENT_CONST
))
4355 if (! pdpm
->printed
)
4357 if (i
>= sizeof adpm
/ sizeof adpm
[0])
4359 d_print_error (dpi
);
4364 adpm
[i
].next
= dpi
->modifiers
;
4365 dpi
->modifiers
= &adpm
[i
];
4373 d_print_comp (dpi
, options
, d_right (dc
));
4375 dpi
->modifiers
= hold_modifiers
;
4377 if (adpm
[0].printed
)
4383 d_print_mod (dpi
, options
, adpm
[i
].mod
);
4386 d_print_array_type (dpi
, options
, dc
, dpi
->modifiers
);
4391 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
4392 case DEMANGLE_COMPONENT_VECTOR_TYPE
:
4394 struct d_print_mod dpm
;
4396 dpm
.next
= dpi
->modifiers
;
4397 dpi
->modifiers
= &dpm
;
4400 dpm
.templates
= dpi
->templates
;
4402 d_print_comp (dpi
, options
, d_right (dc
));
4404 /* If the modifier didn't get printed by the type, print it
4407 d_print_mod (dpi
, options
, dc
);
4409 dpi
->modifiers
= dpm
.next
;
4414 case DEMANGLE_COMPONENT_FIXED_TYPE
:
4415 if (dc
->u
.s_fixed
.sat
)
4416 d_append_string (dpi
, "_Sat ");
4417 /* Don't print "int _Accum". */
4418 if (dc
->u
.s_fixed
.length
->u
.s_builtin
.type
4419 != &cplus_demangle_builtin_types
['i'-'a'])
4421 d_print_comp (dpi
, options
, dc
->u
.s_fixed
.length
);
4422 d_append_char (dpi
, ' ');
4424 if (dc
->u
.s_fixed
.accum
)
4425 d_append_string (dpi
, "_Accum");
4427 d_append_string (dpi
, "_Fract");
4430 case DEMANGLE_COMPONENT_ARGLIST
:
4431 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
4432 if (d_left (dc
) != NULL
)
4433 d_print_comp (dpi
, options
, d_left (dc
));
4434 if (d_right (dc
) != NULL
)
4437 unsigned long int flush_count
;
4438 /* Make sure ", " isn't flushed by d_append_string, otherwise
4439 dpi->len -= 2 wouldn't work. */
4440 if (dpi
->len
>= sizeof (dpi
->buf
) - 2)
4441 d_print_flush (dpi
);
4442 d_append_string (dpi
, ", ");
4444 flush_count
= dpi
->flush_count
;
4445 d_print_comp (dpi
, options
, d_right (dc
));
4446 /* If that didn't print anything (which can happen with empty
4447 template argument packs), remove the comma and space. */
4448 if (dpi
->flush_count
== flush_count
&& dpi
->len
== len
)
4453 case DEMANGLE_COMPONENT_INITIALIZER_LIST
:
4455 struct demangle_component
*type
= d_left (dc
);
4456 struct demangle_component
*list
= d_right (dc
);
4459 d_print_comp (dpi
, options
, type
);
4460 d_append_char (dpi
, '{');
4461 d_print_comp (dpi
, options
, list
);
4462 d_append_char (dpi
, '}');
4466 case DEMANGLE_COMPONENT_OPERATOR
:
4468 const struct demangle_operator_info
*op
= dc
->u
.s_operator
.op
;
4471 d_append_string (dpi
, "operator");
4472 /* Add a space before new/delete. */
4473 if (IS_LOWER (op
->name
[0]))
4474 d_append_char (dpi
, ' ');
4475 /* Omit a trailing space. */
4476 if (op
->name
[len
-1] == ' ')
4478 d_append_buffer (dpi
, op
->name
, len
);
4482 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
4483 d_append_string (dpi
, "operator ");
4484 d_print_comp (dpi
, options
, dc
->u
.s_extended_operator
.name
);
4487 case DEMANGLE_COMPONENT_CAST
:
4488 d_append_string (dpi
, "operator ");
4489 d_print_cast (dpi
, options
, dc
);
4492 case DEMANGLE_COMPONENT_NULLARY
:
4493 d_print_expr_op (dpi
, options
, d_left (dc
));
4496 case DEMANGLE_COMPONENT_UNARY
:
4498 struct demangle_component
*op
= d_left (dc
);
4499 struct demangle_component
*operand
= d_right (dc
);
4500 const char *code
= NULL
;
4502 if (op
->type
== DEMANGLE_COMPONENT_OPERATOR
)
4504 code
= op
->u
.s_operator
.op
->code
;
4505 if (!strcmp (code
, "ad"))
4507 /* Don't print the argument list for the address of a
4509 if (operand
->type
== DEMANGLE_COMPONENT_TYPED_NAME
4510 && d_left (operand
)->type
== DEMANGLE_COMPONENT_QUAL_NAME
4511 && d_right (operand
)->type
== DEMANGLE_COMPONENT_FUNCTION_TYPE
)
4512 operand
= d_left (operand
);
4514 if (operand
->type
== DEMANGLE_COMPONENT_BINARY_ARGS
)
4516 /* This indicates a suffix operator. */
4517 operand
= d_left (operand
);
4518 d_print_subexpr (dpi
, options
, operand
);
4519 d_print_expr_op (dpi
, options
, op
);
4524 if (op
->type
!= DEMANGLE_COMPONENT_CAST
)
4525 d_print_expr_op (dpi
, options
, op
);
4528 d_append_char (dpi
, '(');
4529 d_print_cast (dpi
, options
, op
);
4530 d_append_char (dpi
, ')');
4532 if (code
&& !strcmp (code
, "gs"))
4533 /* Avoid parens after '::'. */
4534 d_print_comp (dpi
, options
, operand
);
4535 else if (code
&& !strcmp (code
, "st"))
4536 /* Always print parens for sizeof (type). */
4538 d_append_char (dpi
, '(');
4539 d_print_comp (dpi
, options
, operand
);
4540 d_append_char (dpi
, ')');
4543 d_print_subexpr (dpi
, options
, operand
);
4547 case DEMANGLE_COMPONENT_BINARY
:
4548 if (d_right (dc
)->type
!= DEMANGLE_COMPONENT_BINARY_ARGS
)
4550 d_print_error (dpi
);
4554 if (op_is_new_cast (d_left (dc
)))
4556 d_print_expr_op (dpi
, options
, d_left (dc
));
4557 d_append_char (dpi
, '<');
4558 d_print_comp (dpi
, options
, d_left (d_right (dc
)));
4559 d_append_string (dpi
, ">(");
4560 d_print_comp (dpi
, options
, d_right (d_right (dc
)));
4561 d_append_char (dpi
, ')');
4565 /* We wrap an expression which uses the greater-than operator in
4566 an extra layer of parens so that it does not get confused
4567 with the '>' which ends the template parameters. */
4568 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_OPERATOR
4569 && d_left (dc
)->u
.s_operator
.op
->len
== 1
4570 && d_left (dc
)->u
.s_operator
.op
->name
[0] == '>')
4571 d_append_char (dpi
, '(');
4573 if (strcmp (d_left (dc
)->u
.s_operator
.op
->code
, "cl") == 0
4574 && d_left (d_right (dc
))->type
== DEMANGLE_COMPONENT_TYPED_NAME
)
4576 /* Function call used in an expression should not have printed types
4577 of the function arguments. Values of the function arguments still
4578 get printed below. */
4580 const struct demangle_component
*func
= d_left (d_right (dc
));
4582 if (d_right (func
)->type
!= DEMANGLE_COMPONENT_FUNCTION_TYPE
)
4583 d_print_error (dpi
);
4584 d_print_subexpr (dpi
, options
, d_left (func
));
4587 d_print_subexpr (dpi
, options
, d_left (d_right (dc
)));
4588 if (strcmp (d_left (dc
)->u
.s_operator
.op
->code
, "ix") == 0)
4590 d_append_char (dpi
, '[');
4591 d_print_comp (dpi
, options
, d_right (d_right (dc
)));
4592 d_append_char (dpi
, ']');
4596 if (strcmp (d_left (dc
)->u
.s_operator
.op
->code
, "cl") != 0)
4597 d_print_expr_op (dpi
, options
, d_left (dc
));
4598 d_print_subexpr (dpi
, options
, d_right (d_right (dc
)));
4601 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_OPERATOR
4602 && d_left (dc
)->u
.s_operator
.op
->len
== 1
4603 && d_left (dc
)->u
.s_operator
.op
->name
[0] == '>')
4604 d_append_char (dpi
, ')');
4608 case DEMANGLE_COMPONENT_BINARY_ARGS
:
4609 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
4610 d_print_error (dpi
);
4613 case DEMANGLE_COMPONENT_TRINARY
:
4614 if (d_right (dc
)->type
!= DEMANGLE_COMPONENT_TRINARY_ARG1
4615 || d_right (d_right (dc
))->type
!= DEMANGLE_COMPONENT_TRINARY_ARG2
)
4617 d_print_error (dpi
);
4621 struct demangle_component
*op
= d_left (dc
);
4622 struct demangle_component
*first
= d_left (d_right (dc
));
4623 struct demangle_component
*second
= d_left (d_right (d_right (dc
)));
4624 struct demangle_component
*third
= d_right (d_right (d_right (dc
)));
4626 if (!strcmp (op
->u
.s_operator
.op
->code
, "qu"))
4628 d_print_subexpr (dpi
, options
, first
);
4629 d_print_expr_op (dpi
, options
, op
);
4630 d_print_subexpr (dpi
, options
, second
);
4631 d_append_string (dpi
, " : ");
4632 d_print_subexpr (dpi
, options
, third
);
4636 d_append_string (dpi
, "new ");
4637 if (d_left (first
) != NULL
)
4639 d_print_subexpr (dpi
, options
, first
);
4640 d_append_char (dpi
, ' ');
4642 d_print_comp (dpi
, options
, second
);
4644 d_print_subexpr (dpi
, options
, third
);
4649 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
4650 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
4651 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
4652 d_print_error (dpi
);
4655 case DEMANGLE_COMPONENT_LITERAL
:
4656 case DEMANGLE_COMPONENT_LITERAL_NEG
:
4658 enum d_builtin_type_print tp
;
4660 /* For some builtin types, produce simpler output. */
4661 tp
= D_PRINT_DEFAULT
;
4662 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
)
4664 tp
= d_left (dc
)->u
.s_builtin
.type
->print
;
4668 case D_PRINT_UNSIGNED
:
4670 case D_PRINT_UNSIGNED_LONG
:
4671 case D_PRINT_LONG_LONG
:
4672 case D_PRINT_UNSIGNED_LONG_LONG
:
4673 if (d_right (dc
)->type
== DEMANGLE_COMPONENT_NAME
)
4675 if (dc
->type
== DEMANGLE_COMPONENT_LITERAL_NEG
)
4676 d_append_char (dpi
, '-');
4677 d_print_comp (dpi
, options
, d_right (dc
));
4682 case D_PRINT_UNSIGNED
:
4683 d_append_char (dpi
, 'u');
4686 d_append_char (dpi
, 'l');
4688 case D_PRINT_UNSIGNED_LONG
:
4689 d_append_string (dpi
, "ul");
4691 case D_PRINT_LONG_LONG
:
4692 d_append_string (dpi
, "ll");
4694 case D_PRINT_UNSIGNED_LONG_LONG
:
4695 d_append_string (dpi
, "ull");
4703 if (d_right (dc
)->type
== DEMANGLE_COMPONENT_NAME
4704 && d_right (dc
)->u
.s_name
.len
== 1
4705 && dc
->type
== DEMANGLE_COMPONENT_LITERAL
)
4707 switch (d_right (dc
)->u
.s_name
.s
[0])
4710 d_append_string (dpi
, "false");
4713 d_append_string (dpi
, "true");
4726 d_append_char (dpi
, '(');
4727 d_print_comp (dpi
, options
, d_left (dc
));
4728 d_append_char (dpi
, ')');
4729 if (dc
->type
== DEMANGLE_COMPONENT_LITERAL_NEG
)
4730 d_append_char (dpi
, '-');
4731 if (tp
== D_PRINT_FLOAT
)
4732 d_append_char (dpi
, '[');
4733 d_print_comp (dpi
, options
, d_right (dc
));
4734 if (tp
== D_PRINT_FLOAT
)
4735 d_append_char (dpi
, ']');
4739 case DEMANGLE_COMPONENT_NUMBER
:
4740 d_append_num (dpi
, dc
->u
.s_number
.number
);
4743 case DEMANGLE_COMPONENT_JAVA_RESOURCE
:
4744 d_append_string (dpi
, "java resource ");
4745 d_print_comp (dpi
, options
, d_left (dc
));
4748 case DEMANGLE_COMPONENT_COMPOUND_NAME
:
4749 d_print_comp (dpi
, options
, d_left (dc
));
4750 d_print_comp (dpi
, options
, d_right (dc
));
4753 case DEMANGLE_COMPONENT_CHARACTER
:
4754 d_append_char (dpi
, dc
->u
.s_character
.character
);
4757 case DEMANGLE_COMPONENT_DECLTYPE
:
4758 d_append_string (dpi
, "decltype (");
4759 d_print_comp (dpi
, options
, d_left (dc
));
4760 d_append_char (dpi
, ')');
4763 case DEMANGLE_COMPONENT_PACK_EXPANSION
:
4767 struct demangle_component
*a
= d_find_pack (dpi
, d_left (dc
));
4770 /* d_find_pack won't find anything if the only packs involved
4771 in this expansion are function parameter packs; in that
4772 case, just print the pattern and "...". */
4773 d_print_subexpr (dpi
, options
, d_left (dc
));
4774 d_append_string (dpi
, "...");
4778 len
= d_pack_length (a
);
4780 for (i
= 0; i
< len
; ++i
)
4782 dpi
->pack_index
= i
;
4783 d_print_comp (dpi
, options
, dc
);
4785 d_append_string (dpi
, ", ");
4790 case DEMANGLE_COMPONENT_FUNCTION_PARAM
:
4792 long num
= dc
->u
.s_number
.number
;
4794 d_append_string (dpi
, "this");
4797 d_append_string (dpi
, "{parm#");
4798 d_append_num (dpi
, num
);
4799 d_append_char (dpi
, '}');
4804 case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
:
4805 d_append_string (dpi
, "global constructors keyed to ");
4806 d_print_comp (dpi
, options
, dc
->u
.s_binary
.left
);
4809 case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS
:
4810 d_append_string (dpi
, "global destructors keyed to ");
4811 d_print_comp (dpi
, options
, dc
->u
.s_binary
.left
);
4814 case DEMANGLE_COMPONENT_LAMBDA
:
4815 d_append_string (dpi
, "{lambda(");
4816 d_print_comp (dpi
, options
, dc
->u
.s_unary_num
.sub
);
4817 d_append_string (dpi
, ")#");
4818 d_append_num (dpi
, dc
->u
.s_unary_num
.num
+ 1);
4819 d_append_char (dpi
, '}');
4822 case DEMANGLE_COMPONENT_UNNAMED_TYPE
:
4823 d_append_string (dpi
, "{unnamed type#");
4824 d_append_num (dpi
, dc
->u
.s_number
.number
+ 1);
4825 d_append_char (dpi
, '}');
4828 case DEMANGLE_COMPONENT_CLONE
:
4829 d_print_comp (dpi
, options
, d_left (dc
));
4830 d_append_string (dpi
, " [clone ");
4831 d_print_comp (dpi
, options
, d_right (dc
));
4832 d_append_char (dpi
, ']');
4836 d_print_error (dpi
);
4841 /* Print a Java dentifier. For Java we try to handle encoded extended
4842 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
4843 so we don't it for C++. Characters are encoded as
4847 d_print_java_identifier (struct d_print_info
*dpi
, const char *name
, int len
)
4853 for (p
= name
; p
< end
; ++p
)
4864 for (q
= p
+ 3; q
< end
; ++q
)
4870 else if (*q
>= 'A' && *q
<= 'F')
4871 dig
= *q
- 'A' + 10;
4872 else if (*q
>= 'a' && *q
<= 'f')
4873 dig
= *q
- 'a' + 10;
4879 /* If the Unicode character is larger than 256, we don't try
4880 to deal with it here. FIXME. */
4881 if (q
< end
&& *q
== '_' && c
< 256)
4883 d_append_char (dpi
, c
);
4889 d_append_char (dpi
, *p
);
4893 /* Print a list of modifiers. SUFFIX is 1 if we are printing
4894 qualifiers on this after printing a function. */
4897 d_print_mod_list (struct d_print_info
*dpi
, int options
,
4898 struct d_print_mod
*mods
, int suffix
)
4900 struct d_print_template
*hold_dpt
;
4902 if (mods
== NULL
|| d_print_saw_error (dpi
))
4907 && (mods
->mod
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
4908 || mods
->mod
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
4909 || mods
->mod
->type
== DEMANGLE_COMPONENT_CONST_THIS
)))
4911 d_print_mod_list (dpi
, options
, mods
->next
, suffix
);
4917 hold_dpt
= dpi
->templates
;
4918 dpi
->templates
= mods
->templates
;
4920 if (mods
->mod
->type
== DEMANGLE_COMPONENT_FUNCTION_TYPE
)
4922 d_print_function_type (dpi
, options
, mods
->mod
, mods
->next
);
4923 dpi
->templates
= hold_dpt
;
4926 else if (mods
->mod
->type
== DEMANGLE_COMPONENT_ARRAY_TYPE
)
4928 d_print_array_type (dpi
, options
, mods
->mod
, mods
->next
);
4929 dpi
->templates
= hold_dpt
;
4932 else if (mods
->mod
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
4934 struct d_print_mod
*hold_modifiers
;
4935 struct demangle_component
*dc
;
4937 /* When this is on the modifier stack, we have pulled any
4938 qualifiers off the right argument already. Otherwise, we
4939 print it as usual, but don't let the left argument see any
4942 hold_modifiers
= dpi
->modifiers
;
4943 dpi
->modifiers
= NULL
;
4944 d_print_comp (dpi
, options
, d_left (mods
->mod
));
4945 dpi
->modifiers
= hold_modifiers
;
4947 if ((options
& DMGL_JAVA
) == 0)
4948 d_append_string (dpi
, "::");
4950 d_append_char (dpi
, '.');
4952 dc
= d_right (mods
->mod
);
4954 if (dc
->type
== DEMANGLE_COMPONENT_DEFAULT_ARG
)
4956 d_append_string (dpi
, "{default arg#");
4957 d_append_num (dpi
, dc
->u
.s_unary_num
.num
+ 1);
4958 d_append_string (dpi
, "}::");
4959 dc
= dc
->u
.s_unary_num
.sub
;
4962 while (dc
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
4963 || dc
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
4964 || dc
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
4967 d_print_comp (dpi
, options
, dc
);
4969 dpi
->templates
= hold_dpt
;
4973 d_print_mod (dpi
, options
, mods
->mod
);
4975 dpi
->templates
= hold_dpt
;
4977 d_print_mod_list (dpi
, options
, mods
->next
, suffix
);
4980 /* Print a modifier. */
4983 d_print_mod (struct d_print_info
*dpi
, int options
,
4984 const struct demangle_component
*mod
)
4988 case DEMANGLE_COMPONENT_RESTRICT
:
4989 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
4990 d_append_string (dpi
, " restrict");
4992 case DEMANGLE_COMPONENT_VOLATILE
:
4993 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
4994 d_append_string (dpi
, " volatile");
4996 case DEMANGLE_COMPONENT_CONST
:
4997 case DEMANGLE_COMPONENT_CONST_THIS
:
4998 d_append_string (dpi
, " const");
5000 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
5001 d_append_char (dpi
, ' ');
5002 d_print_comp (dpi
, options
, d_right (mod
));
5004 case DEMANGLE_COMPONENT_POINTER
:
5005 /* There is no pointer symbol in Java. */
5006 if ((options
& DMGL_JAVA
) == 0)
5007 d_append_char (dpi
, '*');
5009 case DEMANGLE_COMPONENT_REFERENCE
:
5010 d_append_char (dpi
, '&');
5012 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
5013 d_append_string (dpi
, "&&");
5015 case DEMANGLE_COMPONENT_COMPLEX
:
5016 d_append_string (dpi
, "complex ");
5018 case DEMANGLE_COMPONENT_IMAGINARY
:
5019 d_append_string (dpi
, "imaginary ");
5021 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
5022 if (d_last_char (dpi
) != '(')
5023 d_append_char (dpi
, ' ');
5024 d_print_comp (dpi
, options
, d_left (mod
));
5025 d_append_string (dpi
, "::*");
5027 case DEMANGLE_COMPONENT_TYPED_NAME
:
5028 d_print_comp (dpi
, options
, d_left (mod
));
5030 case DEMANGLE_COMPONENT_VECTOR_TYPE
:
5031 d_append_string (dpi
, " __vector(");
5032 d_print_comp (dpi
, options
, d_left (mod
));
5033 d_append_char (dpi
, ')');
5037 /* Otherwise, we have something that won't go back on the
5038 modifier stack, so we can just print it. */
5039 d_print_comp (dpi
, options
, mod
);
5044 /* Print a function type, except for the return type. */
5047 d_print_function_type (struct d_print_info
*dpi
, int options
,
5048 const struct demangle_component
*dc
,
5049 struct d_print_mod
*mods
)
5053 struct d_print_mod
*p
;
5054 struct d_print_mod
*hold_modifiers
;
5058 for (p
= mods
; p
!= NULL
; p
= p
->next
)
5063 switch (p
->mod
->type
)
5065 case DEMANGLE_COMPONENT_POINTER
:
5066 case DEMANGLE_COMPONENT_REFERENCE
:
5067 case DEMANGLE_COMPONENT_RVALUE_REFERENCE
:
5070 case DEMANGLE_COMPONENT_RESTRICT
:
5071 case DEMANGLE_COMPONENT_VOLATILE
:
5072 case DEMANGLE_COMPONENT_CONST
:
5073 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
5074 case DEMANGLE_COMPONENT_COMPLEX
:
5075 case DEMANGLE_COMPONENT_IMAGINARY
:
5076 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
5080 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
5081 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
5082 case DEMANGLE_COMPONENT_CONST_THIS
:
5095 if (d_last_char (dpi
) != '('
5096 && d_last_char (dpi
) != '*')
5099 if (need_space
&& d_last_char (dpi
) != ' ')
5100 d_append_char (dpi
, ' ');
5101 d_append_char (dpi
, '(');
5104 hold_modifiers
= dpi
->modifiers
;
5105 dpi
->modifiers
= NULL
;
5107 d_print_mod_list (dpi
, options
, mods
, 0);
5110 d_append_char (dpi
, ')');
5112 d_append_char (dpi
, '(');
5114 if (d_right (dc
) != NULL
)
5115 d_print_comp (dpi
, options
, d_right (dc
));
5117 d_append_char (dpi
, ')');
5119 d_print_mod_list (dpi
, options
, mods
, 1);
5121 dpi
->modifiers
= hold_modifiers
;
5124 /* Print an array type, except for the element type. */
5127 d_print_array_type (struct d_print_info
*dpi
, int options
,
5128 const struct demangle_component
*dc
,
5129 struct d_print_mod
*mods
)
5137 struct d_print_mod
*p
;
5140 for (p
= mods
; p
!= NULL
; p
= p
->next
)
5144 if (p
->mod
->type
== DEMANGLE_COMPONENT_ARRAY_TYPE
)
5159 d_append_string (dpi
, " (");
5161 d_print_mod_list (dpi
, options
, mods
, 0);
5164 d_append_char (dpi
, ')');
5168 d_append_char (dpi
, ' ');
5170 d_append_char (dpi
, '[');
5172 if (d_left (dc
) != NULL
)
5173 d_print_comp (dpi
, options
, d_left (dc
));
5175 d_append_char (dpi
, ']');
5178 /* Print an operator in an expression. */
5181 d_print_expr_op (struct d_print_info
*dpi
, int options
,
5182 const struct demangle_component
*dc
)
5184 if (dc
->type
== DEMANGLE_COMPONENT_OPERATOR
)
5185 d_append_buffer (dpi
, dc
->u
.s_operator
.op
->name
,
5186 dc
->u
.s_operator
.op
->len
);
5188 d_print_comp (dpi
, options
, dc
);
5194 d_print_cast (struct d_print_info
*dpi
, int options
,
5195 const struct demangle_component
*dc
)
5197 if (d_left (dc
)->type
!= DEMANGLE_COMPONENT_TEMPLATE
)
5198 d_print_comp (dpi
, options
, d_left (dc
));
5201 struct d_print_mod
*hold_dpm
;
5202 struct d_print_template dpt
;
5204 /* It appears that for a templated cast operator, we need to put
5205 the template parameters in scope for the operator name, but
5206 not for the parameters. The effect is that we need to handle
5207 the template printing here. */
5209 hold_dpm
= dpi
->modifiers
;
5210 dpi
->modifiers
= NULL
;
5212 dpt
.next
= dpi
->templates
;
5213 dpi
->templates
= &dpt
;
5214 dpt
.template_decl
= d_left (dc
);
5216 d_print_comp (dpi
, options
, d_left (d_left (dc
)));
5218 dpi
->templates
= dpt
.next
;
5220 if (d_last_char (dpi
) == '<')
5221 d_append_char (dpi
, ' ');
5222 d_append_char (dpi
, '<');
5223 d_print_comp (dpi
, options
, d_right (d_left (dc
)));
5224 /* Avoid generating two consecutive '>' characters, to avoid
5225 the C++ syntactic ambiguity. */
5226 if (d_last_char (dpi
) == '>')
5227 d_append_char (dpi
, ' ');
5228 d_append_char (dpi
, '>');
5230 dpi
->modifiers
= hold_dpm
;
5234 /* Initialize the information structure we use to pass around
5237 CP_STATIC_IF_GLIBCPP_V3
5239 cplus_demangle_init_info (const char *mangled
, int options
, size_t len
,
5243 di
->send
= mangled
+ len
;
5244 di
->options
= options
;
5248 /* We can not need more components than twice the number of chars in
5249 the mangled string. Most components correspond directly to
5250 chars, but the ARGLIST types are exceptions. */
5251 di
->num_comps
= 2 * len
;
5254 /* Similarly, we can not need more substitutions than there are
5255 chars in the mangled string. */
5260 di
->last_name
= NULL
;
5265 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
5266 mangled name, return strings in repeated callback giving the demangled
5267 name. OPTIONS is the usual libiberty demangler options. On success,
5268 this returns 1. On failure, returns 0. */
5271 d_demangle_callback (const char *mangled
, int options
,
5272 demangle_callbackref callback
, void *opaque
)
5283 struct demangle_component
*dc
;
5286 if (mangled
[0] == '_' && mangled
[1] == 'Z')
5288 else if (strncmp (mangled
, "_GLOBAL_", 8) == 0
5289 && (mangled
[8] == '.' || mangled
[8] == '_' || mangled
[8] == '$')
5290 && (mangled
[9] == 'D' || mangled
[9] == 'I')
5291 && mangled
[10] == '_')
5292 type
= mangled
[9] == 'I' ? DCT_GLOBAL_CTORS
: DCT_GLOBAL_DTORS
;
5295 if ((options
& DMGL_TYPES
) == 0)
5300 cplus_demangle_init_info (mangled
, options
, strlen (mangled
), &di
);
5303 #ifdef CP_DYNAMIC_ARRAYS
5304 __extension__
struct demangle_component comps
[di
.num_comps
];
5305 __extension__
struct demangle_component
*subs
[di
.num_subs
];
5310 di
.comps
= alloca (di
.num_comps
* sizeof (*di
.comps
));
5311 di
.subs
= alloca (di
.num_subs
* sizeof (*di
.subs
));
5317 dc
= cplus_demangle_type (&di
);
5320 dc
= cplus_demangle_mangled_name (&di
, 1);
5322 case DCT_GLOBAL_CTORS
:
5323 case DCT_GLOBAL_DTORS
:
5324 d_advance (&di
, 11);
5325 dc
= d_make_comp (&di
,
5326 (type
== DCT_GLOBAL_CTORS
5327 ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
5328 : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS
),
5329 d_make_demangle_mangled_name (&di
, d_str (&di
)),
5331 d_advance (&di
, strlen (d_str (&di
)));
5335 /* If DMGL_PARAMS is set, then if we didn't consume the entire
5336 mangled string, then we didn't successfully demangle it. If
5337 DMGL_PARAMS is not set, we didn't look at the trailing
5339 if (((options
& DMGL_PARAMS
) != 0) && d_peek_char (&di
) != '\0')
5342 #ifdef CP_DEMANGLE_DEBUG
5346 status
= (dc
!= NULL
)
5347 ? cplus_demangle_print_callback (options
, dc
, callback
, opaque
)
5354 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
5355 name, return a buffer allocated with malloc holding the demangled
5356 name. OPTIONS is the usual libiberty demangler options. On
5357 success, this sets *PALC to the allocated size of the returned
5358 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
5359 a memory allocation failure, and returns NULL. */
5362 d_demangle (const char *mangled
, int options
, size_t *palc
)
5364 struct d_growable_string dgs
;
5367 d_growable_string_init (&dgs
, 0);
5369 status
= d_demangle_callback (mangled
, options
,
5370 d_growable_string_callback_adapter
, &dgs
);
5378 *palc
= dgs
.allocation_failure
? 1 : dgs
.alc
;
5382 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
5384 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
5386 /* ia64 ABI-mandated entry point in the C++ runtime library for
5387 performing demangling. MANGLED_NAME is a NUL-terminated character
5388 string containing the name to be demangled.
5390 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
5391 *LENGTH bytes, into which the demangled name is stored. If
5392 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
5393 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
5394 is placed in a region of memory allocated with malloc.
5396 If LENGTH is non-NULL, the length of the buffer containing the
5397 demangled name, is placed in *LENGTH.
5399 The return value is a pointer to the start of the NUL-terminated
5400 demangled name, or NULL if the demangling fails. The caller is
5401 responsible for deallocating this memory using free.
5403 *STATUS is set to one of the following values:
5404 0: The demangling operation succeeded.
5405 -1: A memory allocation failure occurred.
5406 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
5407 -3: One of the arguments is invalid.
5409 The demangling is performed using the C++ ABI mangling rules, with
5413 __cxa_demangle (const char *mangled_name
, char *output_buffer
,
5414 size_t *length
, int *status
)
5419 if (mangled_name
== NULL
)
5426 if (output_buffer
!= NULL
&& length
== NULL
)
5433 demangled
= d_demangle (mangled_name
, DMGL_PARAMS
| DMGL_TYPES
, &alc
);
5435 if (demangled
== NULL
)
5447 if (output_buffer
== NULL
)
5454 if (strlen (demangled
) < *length
)
5456 strcpy (output_buffer
, demangled
);
5458 demangled
= output_buffer
;
5462 free (output_buffer
);
5473 extern int __gcclibcxx_demangle_callback (const char *,
5475 (const char *, size_t, void *),
5478 /* Alternative, allocationless entry point in the C++ runtime library
5479 for performing demangling. MANGLED_NAME is a NUL-terminated character
5480 string containing the name to be demangled.
5482 CALLBACK is a callback function, called with demangled string
5483 segments as demangling progresses; it is called at least once,
5484 but may be called more than once. OPAQUE is a generalized pointer
5485 used as a callback argument.
5487 The return code is one of the following values, equivalent to
5488 the STATUS values of __cxa_demangle() (excluding -1, since this
5489 function performs no memory allocations):
5490 0: The demangling operation succeeded.
5491 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
5492 -3: One of the arguments is invalid.
5494 The demangling is performed using the C++ ABI mangling rules, with
5498 __gcclibcxx_demangle_callback (const char *mangled_name
,
5499 void (*callback
) (const char *, size_t, void *),
5504 if (mangled_name
== NULL
|| callback
== NULL
)
5507 status
= d_demangle_callback (mangled_name
, DMGL_PARAMS
| DMGL_TYPES
,
5515 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
5517 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
5518 mangled name, return a buffer allocated with malloc holding the
5519 demangled name. Otherwise, return NULL. */
5522 cplus_demangle_v3 (const char *mangled
, int options
)
5526 return d_demangle (mangled
, options
, &alc
);
5530 cplus_demangle_v3_callback (const char *mangled
, int options
,
5531 demangle_callbackref callback
, void *opaque
)
5533 return d_demangle_callback (mangled
, options
, callback
, opaque
);
5536 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
5537 conventions, but the output formatting is a little different.
5538 This instructs the C++ demangler not to emit pointer characters ("*"), to
5539 use Java's namespace separator symbol ("." instead of "::"), and to output
5540 JArray<TYPE> as TYPE[]. */
5543 java_demangle_v3 (const char *mangled
)
5547 return d_demangle (mangled
, DMGL_JAVA
| DMGL_PARAMS
| DMGL_RET_POSTFIX
, &alc
);
5551 java_demangle_v3_callback (const char *mangled
,
5552 demangle_callbackref callback
, void *opaque
)
5554 return d_demangle_callback (mangled
,
5555 DMGL_JAVA
| DMGL_PARAMS
| DMGL_RET_POSTFIX
,
5559 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
5561 #ifndef IN_GLIBCPP_V3
5563 /* Demangle a string in order to find out whether it is a constructor
5564 or destructor. Return non-zero on success. Set *CTOR_KIND and
5565 *DTOR_KIND appropriately. */
5568 is_ctor_or_dtor (const char *mangled
,
5569 enum gnu_v3_ctor_kinds
*ctor_kind
,
5570 enum gnu_v3_dtor_kinds
*dtor_kind
)
5573 struct demangle_component
*dc
;
5576 *ctor_kind
= (enum gnu_v3_ctor_kinds
) 0;
5577 *dtor_kind
= (enum gnu_v3_dtor_kinds
) 0;
5579 cplus_demangle_init_info (mangled
, DMGL_GNU_V3
, strlen (mangled
), &di
);
5582 #ifdef CP_DYNAMIC_ARRAYS
5583 __extension__
struct demangle_component comps
[di
.num_comps
];
5584 __extension__
struct demangle_component
*subs
[di
.num_subs
];
5589 di
.comps
= alloca (di
.num_comps
* sizeof (*di
.comps
));
5590 di
.subs
= alloca (di
.num_subs
* sizeof (*di
.subs
));
5593 dc
= cplus_demangle_mangled_name (&di
, 1);
5595 /* Note that because we did not pass DMGL_PARAMS, we don't expect
5596 to demangle the entire string. */
5606 case DEMANGLE_COMPONENT_TYPED_NAME
:
5607 case DEMANGLE_COMPONENT_TEMPLATE
:
5608 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
5609 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
5610 case DEMANGLE_COMPONENT_CONST_THIS
:
5613 case DEMANGLE_COMPONENT_QUAL_NAME
:
5614 case DEMANGLE_COMPONENT_LOCAL_NAME
:
5617 case DEMANGLE_COMPONENT_CTOR
:
5618 *ctor_kind
= dc
->u
.s_ctor
.kind
;
5622 case DEMANGLE_COMPONENT_DTOR
:
5623 *dtor_kind
= dc
->u
.s_dtor
.kind
;
5634 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
5635 name. A non-zero return indicates the type of constructor. */
5637 enum gnu_v3_ctor_kinds
5638 is_gnu_v3_mangled_ctor (const char *name
)
5640 enum gnu_v3_ctor_kinds ctor_kind
;
5641 enum gnu_v3_dtor_kinds dtor_kind
;
5643 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
5644 return (enum gnu_v3_ctor_kinds
) 0;
5649 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
5650 name. A non-zero return indicates the type of destructor. */
5652 enum gnu_v3_dtor_kinds
5653 is_gnu_v3_mangled_dtor (const char *name
)
5655 enum gnu_v3_ctor_kinds ctor_kind
;
5656 enum gnu_v3_dtor_kinds dtor_kind
;
5658 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
5659 return (enum gnu_v3_dtor_kinds
) 0;
5663 #endif /* IN_GLIBCPP_V3 */
5665 #ifdef STANDALONE_DEMANGLER
5668 #include "dyn-string.h"
5670 static void print_usage (FILE* fp
, int exit_value
);
5672 #define IS_ALPHA(CHAR) \
5673 (((CHAR) >= 'a' && (CHAR) <= 'z') \
5674 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
5676 /* Non-zero if CHAR is a character than can occur in a mangled name. */
5677 #define is_mangled_char(CHAR) \
5678 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
5679 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
5681 /* The name of this program, as invoked. */
5682 const char* program_name
;
5684 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
5687 print_usage (FILE* fp
, int exit_value
)
5689 fprintf (fp
, "Usage: %s [options] [names ...]\n", program_name
);
5690 fprintf (fp
, "Options:\n");
5691 fprintf (fp
, " -h,--help Display this message.\n");
5692 fprintf (fp
, " -p,--no-params Don't display function parameters\n");
5693 fprintf (fp
, " -v,--verbose Produce verbose demanglings.\n");
5694 fprintf (fp
, "If names are provided, they are demangled. Otherwise filters standard input.\n");
5699 /* Option specification for getopt_long. */
5700 static const struct option long_options
[] =
5702 { "help", no_argument
, NULL
, 'h' },
5703 { "no-params", no_argument
, NULL
, 'p' },
5704 { "verbose", no_argument
, NULL
, 'v' },
5705 { NULL
, no_argument
, NULL
, 0 },
5708 /* Main entry for a demangling filter executable. It will demangle
5709 its command line arguments, if any. If none are provided, it will
5710 filter stdin to stdout, replacing any recognized mangled C++ names
5711 with their demangled equivalents. */
5714 main (int argc
, char *argv
[])
5718 int options
= DMGL_PARAMS
| DMGL_ANSI
| DMGL_TYPES
;
5720 /* Use the program name of this program, as invoked. */
5721 program_name
= argv
[0];
5723 /* Parse options. */
5726 opt_char
= getopt_long (argc
, argv
, "hpv", long_options
, NULL
);
5729 case '?': /* Unrecognized option. */
5730 print_usage (stderr
, 1);
5734 print_usage (stdout
, 0);
5738 options
&= ~ DMGL_PARAMS
;
5742 options
|= DMGL_VERBOSE
;
5746 while (opt_char
!= -1);
5749 /* No command line arguments were provided. Filter stdin. */
5751 dyn_string_t mangled
= dyn_string_new (3);
5754 /* Read all of input. */
5755 while (!feof (stdin
))
5759 /* Pile characters into mangled until we hit one that can't
5760 occur in a mangled name. */
5762 while (!feof (stdin
) && is_mangled_char (c
))
5764 dyn_string_append_char (mangled
, c
);
5770 if (dyn_string_length (mangled
) > 0)
5772 #ifdef IN_GLIBCPP_V3
5773 s
= __cxa_demangle (dyn_string_buf (mangled
), NULL
, NULL
, NULL
);
5775 s
= cplus_demangle_v3 (dyn_string_buf (mangled
), options
);
5785 /* It might not have been a mangled name. Print the
5787 fputs (dyn_string_buf (mangled
), stdout
);
5790 dyn_string_clear (mangled
);
5793 /* If we haven't hit EOF yet, we've read one character that
5794 can't occur in a mangled name, so print it out. */
5799 dyn_string_delete (mangled
);
5802 /* Demangle command line arguments. */
5804 /* Loop over command line arguments. */
5805 for (i
= optind
; i
< argc
; ++i
)
5808 #ifdef IN_GLIBCPP_V3
5812 /* Attempt to demangle. */
5813 #ifdef IN_GLIBCPP_V3
5814 s
= __cxa_demangle (argv
[i
], NULL
, NULL
, &status
);
5816 s
= cplus_demangle_v3 (argv
[i
], options
);
5819 /* If it worked, print the demangled name. */
5827 #ifdef IN_GLIBCPP_V3
5828 fprintf (stderr
, "Failed: %s (status %d)\n", argv
[i
], status
);
5830 fprintf (stderr
, "Failed: %s\n", argv
[i
]);
5839 #endif /* STANDALONE_DEMANGLER */