1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@wasabisystems.com>.
5 This file is part of the libiberty library, which is part of GCC.
7 This file is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combined
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
31 /* This code implements a demangler for the g++ V3 ABI. The ABI is
32 described on this web page:
33 http://www.codesourcery.com/cxx-abi/abi.html#mangling
35 This code was written while looking at the demangler written by
36 Alex Samuel <samuel@codesourcery.com>.
38 This code first pulls the mangled name apart into a list of
39 components, and then walks the list generating the demangled
42 This file will normally define the following functions, q.v.:
43 char *cplus_demangle_v3(const char *mangled, int options)
44 char *java_demangle_v3(const char *mangled)
45 int cplus_demangle_v3_callback(const char *mangled, int options,
46 demangle_callbackref callback)
47 int java_demangle_v3_callback(const char *mangled,
48 demangle_callbackref callback)
49 enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
50 enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
52 Also, the interface to the component list is public, and defined in
53 demangle.h. The interface consists of these types, which are
54 defined in demangle.h:
55 enum demangle_component_type
56 struct demangle_component
58 and these functions defined in this file:
59 cplus_demangle_fill_name
60 cplus_demangle_fill_extended_operator
61 cplus_demangle_fill_ctor
62 cplus_demangle_fill_dtor
64 cplus_demangle_print_callback
65 and other functions defined in the file cp-demint.c.
67 This file also defines some other functions and variables which are
68 only to be used by the file cp-demint.c.
70 Preprocessor macros you can define while compiling this file:
73 If defined, this file defines the following functions, q.v.:
74 char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
76 int __gcclibcxx_demangle_callback (const char *,
78 (const char *, size_t, void *),
80 instead of cplus_demangle_v3[_callback]() and
81 java_demangle_v3[_callback]().
84 If defined, this file defines only __cxa_demangle() and
85 __gcclibcxx_demangle_callback(), and no other publically visible
86 functions or variables.
89 If defined, this file defines a main() function which demangles
90 any arguments, or, if none, demangles stdin.
93 If defined, turns on debugging mode, which prints information on
94 stdout about the mangled string. This is not generally useful.
97 #if defined (_AIX) && !defined (__GNUC__)
119 # define alloca __builtin_alloca
121 extern char *alloca ();
122 # endif /* __GNUC__ */
124 #endif /* HAVE_ALLOCA_H */
126 #include "ansidecl.h"
127 #include "libiberty.h"
128 #include "demangle.h"
129 #include "cp-demangle.h"
131 /* If IN_GLIBCPP_V3 is defined, some functions are made static. We
132 also rename them via #define to avoid compiler errors when the
133 static definition conflicts with the extern declaration in a header
137 #define CP_STATIC_IF_GLIBCPP_V3 static
139 #define cplus_demangle_fill_name d_fill_name
140 static int d_fill_name (struct demangle_component
*, const char *, int);
142 #define cplus_demangle_fill_extended_operator d_fill_extended_operator
144 d_fill_extended_operator (struct demangle_component
*, int,
145 struct demangle_component
*);
147 #define cplus_demangle_fill_ctor d_fill_ctor
149 d_fill_ctor (struct demangle_component
*, enum gnu_v3_ctor_kinds
,
150 struct demangle_component
*);
152 #define cplus_demangle_fill_dtor d_fill_dtor
154 d_fill_dtor (struct demangle_component
*, enum gnu_v3_dtor_kinds
,
155 struct demangle_component
*);
157 #define cplus_demangle_mangled_name d_mangled_name
158 static struct demangle_component
*d_mangled_name (struct d_info
*, int);
160 #define cplus_demangle_type d_type
161 static struct demangle_component
*d_type (struct d_info
*);
163 #define cplus_demangle_print d_print
164 static char *d_print (int, const struct demangle_component
*, int, size_t *);
166 #define cplus_demangle_print_callback d_print_callback
167 static int d_print_callback (int, const struct demangle_component
*,
168 demangle_callbackref
, void *);
170 #define cplus_demangle_init_info d_init_info
171 static void d_init_info (const char *, int, size_t, struct d_info
*);
173 #else /* ! defined(IN_GLIBCPP_V3) */
174 #define CP_STATIC_IF_GLIBCPP_V3
175 #endif /* ! defined(IN_GLIBCPP_V3) */
177 /* See if the compiler supports dynamic arrays. */
180 #define CP_DYNAMIC_ARRAYS
183 #ifdef __STDC_VERSION__
184 #if __STDC_VERSION__ >= 199901L
185 #define CP_DYNAMIC_ARRAYS
186 #endif /* __STDC__VERSION >= 199901L */
187 #endif /* defined (__STDC_VERSION__) */
188 #endif /* defined (__STDC__) */
189 #endif /* ! defined (__GNUC__) */
191 /* We avoid pulling in the ctype tables, to prevent pulling in
192 additional unresolved symbols when this code is used in a library.
193 FIXME: Is this really a valid reason? This comes from the original
196 As of this writing this file has the following undefined references
197 when compiled with -DIN_GLIBCPP_V3: realloc, free, memcpy, strcpy,
200 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
201 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
202 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
204 /* The prefix prepended by GCC to an identifier represnting the
205 anonymous namespace. */
206 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
207 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
208 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
210 /* Information we keep for the standard substitutions. */
212 struct d_standard_sub_info
214 /* The code for this substitution. */
216 /* The simple string it expands to. */
217 const char *simple_expansion
;
218 /* The length of the simple expansion. */
220 /* The results of a full, verbose, expansion. This is used when
221 qualifying a constructor/destructor, or when in verbose mode. */
222 const char *full_expansion
;
223 /* The length of the full expansion. */
225 /* What to set the last_name field of d_info to; NULL if we should
226 not set it. This is only relevant when qualifying a
227 constructor/destructor. */
228 const char *set_last_name
;
229 /* The length of set_last_name. */
230 int set_last_name_len
;
233 /* Accessors for subtrees of struct demangle_component. */
235 #define d_left(dc) ((dc)->u.s_binary.left)
236 #define d_right(dc) ((dc)->u.s_binary.right)
238 /* A list of templates. This is used while printing. */
240 struct d_print_template
242 /* Next template on the list. */
243 struct d_print_template
*next
;
245 const struct demangle_component
*template_decl
;
248 /* A list of type modifiers. This is used while printing. */
252 /* Next modifier on the list. These are in the reverse of the order
253 in which they appeared in the mangled string. */
254 struct d_print_mod
*next
;
256 const struct demangle_component
*mod
;
257 /* Whether this modifier was printed. */
259 /* The list of templates which applies to this modifier. */
260 struct d_print_template
*templates
;
263 /* We use these structures to hold information during printing. */
265 struct d_growable_string
267 /* Buffer holding the result. */
269 /* Current length of data in buffer. */
271 /* Allocated size of buffer. */
273 /* Set to 1 if we had a memory allocation failure. */
274 int allocation_failure
;
277 enum { D_PRINT_BUFFER_LENGTH
= 256 };
280 /* The options passed to the demangler. */
282 /* Fixed-length allocated buffer for demangled data, flushed to the
283 callback with a NUL termination once full. */
284 char buf
[D_PRINT_BUFFER_LENGTH
];
285 /* Current length of data in buffer. */
287 /* The last character printed, saved individually so that it survives
290 /* Callback function to handle demangled buffer flush. */
291 demangle_callbackref callback
;
292 /* Opaque callback argument. */
294 /* The current list of templates, if any. */
295 struct d_print_template
*templates
;
296 /* The current list of modifiers (e.g., pointer, reference, etc.),
298 struct d_print_mod
*modifiers
;
299 /* Set to 1 if we saw a demangling error. */
300 int demangle_failure
;
303 #ifdef CP_DEMANGLE_DEBUG
304 static void d_dump (struct demangle_component
*, int);
307 static struct demangle_component
*
308 d_make_empty (struct d_info
*);
310 static struct demangle_component
*
311 d_make_comp (struct d_info
*, enum demangle_component_type
,
312 struct demangle_component
*,
313 struct demangle_component
*);
315 static struct demangle_component
*
316 d_make_name (struct d_info
*, const char *, int);
318 static struct demangle_component
*
319 d_make_builtin_type (struct d_info
*,
320 const struct demangle_builtin_type_info
*);
322 static struct demangle_component
*
323 d_make_operator (struct d_info
*,
324 const struct demangle_operator_info
*);
326 static struct demangle_component
*
327 d_make_extended_operator (struct d_info
*, int,
328 struct demangle_component
*);
330 static struct demangle_component
*
331 d_make_ctor (struct d_info
*, enum gnu_v3_ctor_kinds
,
332 struct demangle_component
*);
334 static struct demangle_component
*
335 d_make_dtor (struct d_info
*, enum gnu_v3_dtor_kinds
,
336 struct demangle_component
*);
338 static struct demangle_component
*
339 d_make_template_param (struct d_info
*, long);
341 static struct demangle_component
*
342 d_make_sub (struct d_info
*, const char *, int);
345 has_return_type (struct demangle_component
*);
348 is_ctor_dtor_or_conversion (struct demangle_component
*);
350 static struct demangle_component
*d_encoding (struct d_info
*, int);
352 static struct demangle_component
*d_name (struct d_info
*);
354 static struct demangle_component
*d_nested_name (struct d_info
*);
356 static struct demangle_component
*d_prefix (struct d_info
*);
358 static struct demangle_component
*d_unqualified_name (struct d_info
*);
360 static struct demangle_component
*d_source_name (struct d_info
*);
362 static long d_number (struct d_info
*);
364 static struct demangle_component
*d_identifier (struct d_info
*, int);
366 static struct demangle_component
*d_operator_name (struct d_info
*);
368 static struct demangle_component
*d_special_name (struct d_info
*);
370 static int d_call_offset (struct d_info
*, int);
372 static struct demangle_component
*d_ctor_dtor_name (struct d_info
*);
374 static struct demangle_component
**
375 d_cv_qualifiers (struct d_info
*, struct demangle_component
**, int);
377 static struct demangle_component
*
378 d_function_type (struct d_info
*);
380 static struct demangle_component
*
381 d_bare_function_type (struct d_info
*, int);
383 static struct demangle_component
*
384 d_class_enum_type (struct d_info
*);
386 static struct demangle_component
*d_array_type (struct d_info
*);
388 static struct demangle_component
*
389 d_pointer_to_member_type (struct d_info
*);
391 static struct demangle_component
*
392 d_template_param (struct d_info
*);
394 static struct demangle_component
*d_template_args (struct d_info
*);
396 static struct demangle_component
*
397 d_template_arg (struct d_info
*);
399 static struct demangle_component
*d_expression (struct d_info
*);
401 static struct demangle_component
*d_expr_primary (struct d_info
*);
403 static struct demangle_component
*d_local_name (struct d_info
*);
405 static int d_discriminator (struct d_info
*);
408 d_add_substitution (struct d_info
*, struct demangle_component
*);
410 static struct demangle_component
*d_substitution (struct d_info
*, int);
412 static void d_growable_string_init (struct d_growable_string
*, size_t);
415 d_growable_string_resize (struct d_growable_string
*, size_t);
418 d_growable_string_append_buffer (struct d_growable_string
*,
419 const char *, size_t);
421 d_growable_string_callback_adapter (const char *, size_t, void *);
424 d_print_init (struct d_print_info
*, int, demangle_callbackref
, void *);
426 static inline void d_print_error (struct d_print_info
*);
428 static inline int d_print_saw_error (struct d_print_info
*);
430 static inline void d_print_flush (struct d_print_info
*);
432 static inline void d_append_char (struct d_print_info
*, char);
434 static inline void d_append_buffer (struct d_print_info
*,
435 const char *, size_t);
437 static inline void d_append_string (struct d_print_info
*, const char *);
439 static inline char d_last_char (struct d_print_info
*);
442 d_print_comp (struct d_print_info
*, const struct demangle_component
*);
445 d_print_java_identifier (struct d_print_info
*, const char *, int);
448 d_print_mod_list (struct d_print_info
*, struct d_print_mod
*, int);
451 d_print_mod (struct d_print_info
*, const struct demangle_component
*);
454 d_print_function_type (struct d_print_info
*,
455 const struct demangle_component
*,
456 struct d_print_mod
*);
459 d_print_array_type (struct d_print_info
*,
460 const struct demangle_component
*,
461 struct d_print_mod
*);
464 d_print_expr_op (struct d_print_info
*, const struct demangle_component
*);
467 d_print_cast (struct d_print_info
*, const struct demangle_component
*);
469 static int d_demangle_callback (const char *, int,
470 demangle_callbackref
, void *);
471 static char *d_demangle (const char *, int, size_t *);
473 #ifdef CP_DEMANGLE_DEBUG
476 d_dump (struct demangle_component
*dc
, int indent
)
483 printf ("failed demangling\n");
487 for (i
= 0; i
< indent
; ++i
)
492 case DEMANGLE_COMPONENT_NAME
:
493 printf ("name '%.*s'\n", dc
->u
.s_name
.len
, dc
->u
.s_name
.s
);
495 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
496 printf ("template parameter %ld\n", dc
->u
.s_number
.number
);
498 case DEMANGLE_COMPONENT_CTOR
:
499 printf ("constructor %d\n", (int) dc
->u
.s_ctor
.kind
);
500 d_dump (dc
->u
.s_ctor
.name
, indent
+ 2);
502 case DEMANGLE_COMPONENT_DTOR
:
503 printf ("destructor %d\n", (int) dc
->u
.s_dtor
.kind
);
504 d_dump (dc
->u
.s_dtor
.name
, indent
+ 2);
506 case DEMANGLE_COMPONENT_SUB_STD
:
507 printf ("standard substitution %s\n", dc
->u
.s_string
.string
);
509 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
510 printf ("builtin type %s\n", dc
->u
.s_builtin
.type
->name
);
512 case DEMANGLE_COMPONENT_OPERATOR
:
513 printf ("operator %s\n", dc
->u
.s_operator
.op
->name
);
515 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
516 printf ("extended operator with %d args\n",
517 dc
->u
.s_extended_operator
.args
);
518 d_dump (dc
->u
.s_extended_operator
.name
, indent
+ 2);
521 case DEMANGLE_COMPONENT_QUAL_NAME
:
522 printf ("qualified name\n");
524 case DEMANGLE_COMPONENT_LOCAL_NAME
:
525 printf ("local name\n");
527 case DEMANGLE_COMPONENT_TYPED_NAME
:
528 printf ("typed name\n");
530 case DEMANGLE_COMPONENT_TEMPLATE
:
531 printf ("template\n");
533 case DEMANGLE_COMPONENT_VTABLE
:
536 case DEMANGLE_COMPONENT_VTT
:
539 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
540 printf ("construction vtable\n");
542 case DEMANGLE_COMPONENT_TYPEINFO
:
543 printf ("typeinfo\n");
545 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
546 printf ("typeinfo name\n");
548 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
549 printf ("typeinfo function\n");
551 case DEMANGLE_COMPONENT_THUNK
:
554 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
555 printf ("virtual thunk\n");
557 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
558 printf ("covariant thunk\n");
560 case DEMANGLE_COMPONENT_JAVA_CLASS
:
561 printf ("java class\n");
563 case DEMANGLE_COMPONENT_GUARD
:
566 case DEMANGLE_COMPONENT_REFTEMP
:
567 printf ("reference temporary\n");
569 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
570 printf ("hidden alias\n");
572 case DEMANGLE_COMPONENT_RESTRICT
:
573 printf ("restrict\n");
575 case DEMANGLE_COMPONENT_VOLATILE
:
576 printf ("volatile\n");
578 case DEMANGLE_COMPONENT_CONST
:
581 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
582 printf ("restrict this\n");
584 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
585 printf ("volatile this\n");
587 case DEMANGLE_COMPONENT_CONST_THIS
:
588 printf ("const this\n");
590 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
591 printf ("vendor type qualifier\n");
593 case DEMANGLE_COMPONENT_POINTER
:
594 printf ("pointer\n");
596 case DEMANGLE_COMPONENT_REFERENCE
:
597 printf ("reference\n");
599 case DEMANGLE_COMPONENT_COMPLEX
:
600 printf ("complex\n");
602 case DEMANGLE_COMPONENT_IMAGINARY
:
603 printf ("imaginary\n");
605 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
606 printf ("vendor type\n");
608 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
609 printf ("function type\n");
611 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
612 printf ("array type\n");
614 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
615 printf ("pointer to member type\n");
617 case DEMANGLE_COMPONENT_ARGLIST
:
618 printf ("argument list\n");
620 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
621 printf ("template argument list\n");
623 case DEMANGLE_COMPONENT_CAST
:
626 case DEMANGLE_COMPONENT_UNARY
:
627 printf ("unary operator\n");
629 case DEMANGLE_COMPONENT_BINARY
:
630 printf ("binary operator\n");
632 case DEMANGLE_COMPONENT_BINARY_ARGS
:
633 printf ("binary operator arguments\n");
635 case DEMANGLE_COMPONENT_TRINARY
:
636 printf ("trinary operator\n");
638 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
639 printf ("trinary operator arguments 1\n");
641 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
642 printf ("trinary operator arguments 1\n");
644 case DEMANGLE_COMPONENT_LITERAL
:
645 printf ("literal\n");
647 case DEMANGLE_COMPONENT_LITERAL_NEG
:
648 printf ("negative literal\n");
652 d_dump (d_left (dc
), indent
+ 2);
653 d_dump (d_right (dc
), indent
+ 2);
656 #endif /* CP_DEMANGLE_DEBUG */
658 /* Fill in a DEMANGLE_COMPONENT_NAME. */
660 CP_STATIC_IF_GLIBCPP_V3
662 cplus_demangle_fill_name (struct demangle_component
*p
, const char *s
, int len
)
664 if (p
== NULL
|| s
== NULL
|| len
== 0)
666 p
->type
= DEMANGLE_COMPONENT_NAME
;
668 p
->u
.s_name
.len
= len
;
672 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
674 CP_STATIC_IF_GLIBCPP_V3
676 cplus_demangle_fill_extended_operator (struct demangle_component
*p
, int args
,
677 struct demangle_component
*name
)
679 if (p
== NULL
|| args
< 0 || name
== NULL
)
681 p
->type
= DEMANGLE_COMPONENT_EXTENDED_OPERATOR
;
682 p
->u
.s_extended_operator
.args
= args
;
683 p
->u
.s_extended_operator
.name
= name
;
687 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
689 CP_STATIC_IF_GLIBCPP_V3
691 cplus_demangle_fill_ctor (struct demangle_component
*p
,
692 enum gnu_v3_ctor_kinds kind
,
693 struct demangle_component
*name
)
697 || (kind
< gnu_v3_complete_object_ctor
698 && kind
> gnu_v3_complete_object_allocating_ctor
))
700 p
->type
= DEMANGLE_COMPONENT_CTOR
;
701 p
->u
.s_ctor
.kind
= kind
;
702 p
->u
.s_ctor
.name
= name
;
706 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
708 CP_STATIC_IF_GLIBCPP_V3
710 cplus_demangle_fill_dtor (struct demangle_component
*p
,
711 enum gnu_v3_dtor_kinds kind
,
712 struct demangle_component
*name
)
716 || (kind
< gnu_v3_deleting_dtor
717 && kind
> gnu_v3_base_object_dtor
))
719 p
->type
= DEMANGLE_COMPONENT_DTOR
;
720 p
->u
.s_dtor
.kind
= kind
;
721 p
->u
.s_dtor
.name
= name
;
725 /* Add a new component. */
727 static struct demangle_component
*
728 d_make_empty (struct d_info
*di
)
730 struct demangle_component
*p
;
732 if (di
->next_comp
>= di
->num_comps
)
734 p
= &di
->comps
[di
->next_comp
];
739 /* Add a new generic component. */
741 static struct demangle_component
*
742 d_make_comp (struct d_info
*di
, enum demangle_component_type type
,
743 struct demangle_component
*left
,
744 struct demangle_component
*right
)
746 struct demangle_component
*p
;
748 /* We check for errors here. A typical error would be a NULL return
749 from a subroutine. We catch those here, and return NULL
753 /* These types require two parameters. */
754 case DEMANGLE_COMPONENT_QUAL_NAME
:
755 case DEMANGLE_COMPONENT_LOCAL_NAME
:
756 case DEMANGLE_COMPONENT_TYPED_NAME
:
757 case DEMANGLE_COMPONENT_TEMPLATE
:
758 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
759 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
760 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
761 case DEMANGLE_COMPONENT_UNARY
:
762 case DEMANGLE_COMPONENT_BINARY
:
763 case DEMANGLE_COMPONENT_BINARY_ARGS
:
764 case DEMANGLE_COMPONENT_TRINARY
:
765 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
766 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
767 case DEMANGLE_COMPONENT_LITERAL
:
768 case DEMANGLE_COMPONENT_LITERAL_NEG
:
769 if (left
== NULL
|| right
== NULL
)
773 /* These types only require one parameter. */
774 case DEMANGLE_COMPONENT_VTABLE
:
775 case DEMANGLE_COMPONENT_VTT
:
776 case DEMANGLE_COMPONENT_TYPEINFO
:
777 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
778 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
779 case DEMANGLE_COMPONENT_THUNK
:
780 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
781 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
782 case DEMANGLE_COMPONENT_JAVA_CLASS
:
783 case DEMANGLE_COMPONENT_GUARD
:
784 case DEMANGLE_COMPONENT_REFTEMP
:
785 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
786 case DEMANGLE_COMPONENT_POINTER
:
787 case DEMANGLE_COMPONENT_REFERENCE
:
788 case DEMANGLE_COMPONENT_COMPLEX
:
789 case DEMANGLE_COMPONENT_IMAGINARY
:
790 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
791 case DEMANGLE_COMPONENT_ARGLIST
:
792 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
793 case DEMANGLE_COMPONENT_CAST
:
798 /* This needs a right parameter, but the left parameter can be
800 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
805 /* These are allowed to have no parameters--in some cases they
806 will be filled in later. */
807 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
808 case DEMANGLE_COMPONENT_RESTRICT
:
809 case DEMANGLE_COMPONENT_VOLATILE
:
810 case DEMANGLE_COMPONENT_CONST
:
811 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
812 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
813 case DEMANGLE_COMPONENT_CONST_THIS
:
816 /* Other types should not be seen here. */
821 p
= d_make_empty (di
);
825 p
->u
.s_binary
.left
= left
;
826 p
->u
.s_binary
.right
= right
;
831 /* Add a new name component. */
833 static struct demangle_component
*
834 d_make_name (struct d_info
*di
, const char *s
, int len
)
836 struct demangle_component
*p
;
838 p
= d_make_empty (di
);
839 if (! cplus_demangle_fill_name (p
, s
, len
))
844 /* Add a new builtin type component. */
846 static struct demangle_component
*
847 d_make_builtin_type (struct d_info
*di
,
848 const struct demangle_builtin_type_info
*type
)
850 struct demangle_component
*p
;
854 p
= d_make_empty (di
);
857 p
->type
= DEMANGLE_COMPONENT_BUILTIN_TYPE
;
858 p
->u
.s_builtin
.type
= type
;
863 /* Add a new operator component. */
865 static struct demangle_component
*
866 d_make_operator (struct d_info
*di
, const struct demangle_operator_info
*op
)
868 struct demangle_component
*p
;
870 p
= d_make_empty (di
);
873 p
->type
= DEMANGLE_COMPONENT_OPERATOR
;
874 p
->u
.s_operator
.op
= op
;
879 /* Add a new extended operator component. */
881 static struct demangle_component
*
882 d_make_extended_operator (struct d_info
*di
, int args
,
883 struct demangle_component
*name
)
885 struct demangle_component
*p
;
887 p
= d_make_empty (di
);
888 if (! cplus_demangle_fill_extended_operator (p
, args
, name
))
893 /* Add a new constructor component. */
895 static struct demangle_component
*
896 d_make_ctor (struct d_info
*di
, enum gnu_v3_ctor_kinds kind
,
897 struct demangle_component
*name
)
899 struct demangle_component
*p
;
901 p
= d_make_empty (di
);
902 if (! cplus_demangle_fill_ctor (p
, kind
, name
))
907 /* Add a new destructor component. */
909 static struct demangle_component
*
910 d_make_dtor (struct d_info
*di
, enum gnu_v3_dtor_kinds kind
,
911 struct demangle_component
*name
)
913 struct demangle_component
*p
;
915 p
= d_make_empty (di
);
916 if (! cplus_demangle_fill_dtor (p
, kind
, name
))
921 /* Add a new template parameter. */
923 static struct demangle_component
*
924 d_make_template_param (struct d_info
*di
, long i
)
926 struct demangle_component
*p
;
928 p
= d_make_empty (di
);
931 p
->type
= DEMANGLE_COMPONENT_TEMPLATE_PARAM
;
932 p
->u
.s_number
.number
= i
;
937 /* Add a new standard substitution component. */
939 static struct demangle_component
*
940 d_make_sub (struct d_info
*di
, const char *name
, int len
)
942 struct demangle_component
*p
;
944 p
= d_make_empty (di
);
947 p
->type
= DEMANGLE_COMPONENT_SUB_STD
;
948 p
->u
.s_string
.string
= name
;
949 p
->u
.s_string
.len
= len
;
954 /* <mangled-name> ::= _Z <encoding>
956 TOP_LEVEL is non-zero when called at the top level. */
958 CP_STATIC_IF_GLIBCPP_V3
959 struct demangle_component
*
960 cplus_demangle_mangled_name (struct d_info
*di
, int top_level
)
962 if (! d_check_char (di
, '_'))
964 if (! d_check_char (di
, 'Z'))
966 return d_encoding (di
, top_level
);
969 /* Return whether a function should have a return type. The argument
970 is the function name, which may be qualified in various ways. The
971 rules are that template functions have return types with some
972 exceptions, function types which are not part of a function name
973 mangling have return types with some exceptions, and non-template
974 function names do not have return types. The exceptions are that
975 constructors, destructors, and conversion operators do not have
979 has_return_type (struct demangle_component
*dc
)
987 case DEMANGLE_COMPONENT_TEMPLATE
:
988 return ! is_ctor_dtor_or_conversion (d_left (dc
));
989 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
990 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
991 case DEMANGLE_COMPONENT_CONST_THIS
:
992 return has_return_type (d_left (dc
));
996 /* Return whether a name is a constructor, a destructor, or a
997 conversion operator. */
1000 is_ctor_dtor_or_conversion (struct demangle_component
*dc
)
1008 case DEMANGLE_COMPONENT_QUAL_NAME
:
1009 case DEMANGLE_COMPONENT_LOCAL_NAME
:
1010 return is_ctor_dtor_or_conversion (d_right (dc
));
1011 case DEMANGLE_COMPONENT_CTOR
:
1012 case DEMANGLE_COMPONENT_DTOR
:
1013 case DEMANGLE_COMPONENT_CAST
:
1018 /* <encoding> ::= <(function) name> <bare-function-type>
1022 TOP_LEVEL is non-zero when called at the top level, in which case
1023 if DMGL_PARAMS is not set we do not demangle the function
1024 parameters. We only set this at the top level, because otherwise
1025 we would not correctly demangle names in local scopes. */
1027 static struct demangle_component
*
1028 d_encoding (struct d_info
*di
, int top_level
)
1030 char peek
= d_peek_char (di
);
1032 if (peek
== 'G' || peek
== 'T')
1033 return d_special_name (di
);
1036 struct demangle_component
*dc
;
1040 if (dc
!= NULL
&& top_level
&& (di
->options
& DMGL_PARAMS
) == 0)
1042 /* Strip off any initial CV-qualifiers, as they really apply
1043 to the `this' parameter, and they were not output by the
1044 v2 demangler without DMGL_PARAMS. */
1045 while (dc
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
1046 || dc
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
1047 || dc
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
1050 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1051 there may be CV-qualifiers on its right argument which
1052 really apply here; this happens when parsing a class
1053 which is local to a function. */
1054 if (dc
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
1056 struct demangle_component
*dcr
;
1059 while (dcr
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
1060 || dcr
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
1061 || dcr
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
1063 dc
->u
.s_binary
.right
= dcr
;
1069 peek
= d_peek_char (di
);
1070 if (dc
== NULL
|| peek
== '\0' || peek
== 'E')
1072 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPED_NAME
, dc
,
1073 d_bare_function_type (di
, has_return_type (dc
)));
1077 /* <name> ::= <nested-name>
1079 ::= <unscoped-template-name> <template-args>
1082 <unscoped-name> ::= <unqualified-name>
1083 ::= St <unqualified-name>
1085 <unscoped-template-name> ::= <unscoped-name>
1089 static struct demangle_component
*
1090 d_name (struct d_info
*di
)
1092 char peek
= d_peek_char (di
);
1093 struct demangle_component
*dc
;
1098 return d_nested_name (di
);
1101 return d_local_name (di
);
1107 if (d_peek_next_char (di
) != 't')
1109 dc
= d_substitution (di
, 0);
1115 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
,
1116 d_make_name (di
, "std", 3),
1117 d_unqualified_name (di
));
1122 if (d_peek_char (di
) != 'I')
1124 /* The grammar does not permit this case to occur if we
1125 called d_substitution() above (i.e., subst == 1). We
1126 don't bother to check. */
1130 /* This is <template-args>, which means that we just saw
1131 <unscoped-template-name>, which is a substitution
1132 candidate if we didn't just get it from a
1136 if (! d_add_substitution (di
, dc
))
1139 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, dc
,
1140 d_template_args (di
));
1147 dc
= d_unqualified_name (di
);
1148 if (d_peek_char (di
) == 'I')
1150 /* This is <template-args>, which means that we just saw
1151 <unscoped-template-name>, which is a substitution
1153 if (! d_add_substitution (di
, dc
))
1155 dc
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, dc
,
1156 d_template_args (di
));
1162 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1163 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1166 static struct demangle_component
*
1167 d_nested_name (struct d_info
*di
)
1169 struct demangle_component
*ret
;
1170 struct demangle_component
**pret
;
1172 if (! d_check_char (di
, 'N'))
1175 pret
= d_cv_qualifiers (di
, &ret
, 1);
1179 *pret
= d_prefix (di
);
1183 if (! d_check_char (di
, 'E'))
1189 /* <prefix> ::= <prefix> <unqualified-name>
1190 ::= <template-prefix> <template-args>
1191 ::= <template-param>
1195 <template-prefix> ::= <prefix> <(template) unqualified-name>
1196 ::= <template-param>
1200 static struct demangle_component
*
1201 d_prefix (struct d_info
*di
)
1203 struct demangle_component
*ret
= NULL
;
1208 enum demangle_component_type comb_type
;
1209 struct demangle_component
*dc
;
1211 peek
= d_peek_char (di
);
1215 /* The older code accepts a <local-name> here, but I don't see
1216 that in the grammar. The older code does not accept a
1217 <template-param> here. */
1219 comb_type
= DEMANGLE_COMPONENT_QUAL_NAME
;
1224 dc
= d_unqualified_name (di
);
1225 else if (peek
== 'S')
1226 dc
= d_substitution (di
, 1);
1227 else if (peek
== 'I')
1231 comb_type
= DEMANGLE_COMPONENT_TEMPLATE
;
1232 dc
= d_template_args (di
);
1234 else if (peek
== 'T')
1235 dc
= d_template_param (di
);
1236 else if (peek
== 'E')
1244 ret
= d_make_comp (di
, comb_type
, ret
, dc
);
1246 if (peek
!= 'S' && d_peek_char (di
) != 'E')
1248 if (! d_add_substitution (di
, ret
))
1254 /* <unqualified-name> ::= <operator-name>
1255 ::= <ctor-dtor-name>
1259 static struct demangle_component
*
1260 d_unqualified_name (struct d_info
*di
)
1264 peek
= d_peek_char (di
);
1265 if (IS_DIGIT (peek
))
1266 return d_source_name (di
);
1267 else if (IS_LOWER (peek
))
1269 struct demangle_component
*ret
;
1271 ret
= d_operator_name (di
);
1272 if (ret
!= NULL
&& ret
->type
== DEMANGLE_COMPONENT_OPERATOR
)
1273 di
->expansion
+= sizeof "operator" + ret
->u
.s_operator
.op
->len
- 2;
1276 else if (peek
== 'C' || peek
== 'D')
1277 return d_ctor_dtor_name (di
);
1282 /* <source-name> ::= <(positive length) number> <identifier> */
1284 static struct demangle_component
*
1285 d_source_name (struct d_info
*di
)
1288 struct demangle_component
*ret
;
1290 len
= d_number (di
);
1293 ret
= d_identifier (di
, len
);
1294 di
->last_name
= ret
;
1298 /* number ::= [n] <(non-negative decimal integer)> */
1301 d_number (struct d_info
*di
)
1308 peek
= d_peek_char (di
);
1313 peek
= d_peek_char (di
);
1319 if (! IS_DIGIT (peek
))
1325 ret
= ret
* 10 + peek
- '0';
1327 peek
= d_peek_char (di
);
1331 /* identifier ::= <(unqualified source code identifier)> */
1333 static struct demangle_component
*
1334 d_identifier (struct d_info
*di
, int len
)
1340 if (di
->send
- name
< len
)
1343 d_advance (di
, len
);
1345 /* A Java mangled name may have a trailing '$' if it is a C++
1346 keyword. This '$' is not included in the length count. We just
1348 if ((di
->options
& DMGL_JAVA
) != 0
1349 && d_peek_char (di
) == '$')
1352 /* Look for something which looks like a gcc encoding of an
1353 anonymous namespace, and replace it with a more user friendly
1355 if (len
>= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN
+ 2
1356 && memcmp (name
, ANONYMOUS_NAMESPACE_PREFIX
,
1357 ANONYMOUS_NAMESPACE_PREFIX_LEN
) == 0)
1361 s
= name
+ ANONYMOUS_NAMESPACE_PREFIX_LEN
;
1362 if ((*s
== '.' || *s
== '_' || *s
== '$')
1365 di
->expansion
-= len
- sizeof "(anonymous namespace)";
1366 return d_make_name (di
, "(anonymous namespace)",
1367 sizeof "(anonymous namespace)" - 1);
1371 return d_make_name (di
, name
, len
);
1374 /* operator_name ::= many different two character encodings.
1376 ::= v <digit> <source-name>
1379 #define NL(s) s, (sizeof s) - 1
1381 CP_STATIC_IF_GLIBCPP_V3
1382 const struct demangle_operator_info cplus_demangle_operators
[] =
1384 { "aN", NL ("&="), 2 },
1385 { "aS", NL ("="), 2 },
1386 { "aa", NL ("&&"), 2 },
1387 { "ad", NL ("&"), 1 },
1388 { "an", NL ("&"), 2 },
1389 { "cl", NL ("()"), 0 },
1390 { "cm", NL (","), 2 },
1391 { "co", NL ("~"), 1 },
1392 { "dV", NL ("/="), 2 },
1393 { "da", NL ("delete[]"), 1 },
1394 { "de", NL ("*"), 1 },
1395 { "dl", NL ("delete"), 1 },
1396 { "dv", NL ("/"), 2 },
1397 { "eO", NL ("^="), 2 },
1398 { "eo", NL ("^"), 2 },
1399 { "eq", NL ("=="), 2 },
1400 { "ge", NL (">="), 2 },
1401 { "gt", NL (">"), 2 },
1402 { "ix", NL ("[]"), 2 },
1403 { "lS", NL ("<<="), 2 },
1404 { "le", NL ("<="), 2 },
1405 { "ls", NL ("<<"), 2 },
1406 { "lt", NL ("<"), 2 },
1407 { "mI", NL ("-="), 2 },
1408 { "mL", NL ("*="), 2 },
1409 { "mi", NL ("-"), 2 },
1410 { "ml", NL ("*"), 2 },
1411 { "mm", NL ("--"), 1 },
1412 { "na", NL ("new[]"), 1 },
1413 { "ne", NL ("!="), 2 },
1414 { "ng", NL ("-"), 1 },
1415 { "nt", NL ("!"), 1 },
1416 { "nw", NL ("new"), 1 },
1417 { "oR", NL ("|="), 2 },
1418 { "oo", NL ("||"), 2 },
1419 { "or", NL ("|"), 2 },
1420 { "pL", NL ("+="), 2 },
1421 { "pl", NL ("+"), 2 },
1422 { "pm", NL ("->*"), 2 },
1423 { "pp", NL ("++"), 1 },
1424 { "ps", NL ("+"), 1 },
1425 { "pt", NL ("->"), 2 },
1426 { "qu", NL ("?"), 3 },
1427 { "rM", NL ("%="), 2 },
1428 { "rS", NL (">>="), 2 },
1429 { "rm", NL ("%"), 2 },
1430 { "rs", NL (">>"), 2 },
1431 { "st", NL ("sizeof "), 1 },
1432 { "sz", NL ("sizeof "), 1 },
1433 { NULL
, NULL
, 0, 0 }
1436 static struct demangle_component
*
1437 d_operator_name (struct d_info
*di
)
1442 c1
= d_next_char (di
);
1443 c2
= d_next_char (di
);
1444 if (c1
== 'v' && IS_DIGIT (c2
))
1445 return d_make_extended_operator (di
, c2
- '0', d_source_name (di
));
1446 else if (c1
== 'c' && c2
== 'v')
1447 return d_make_comp (di
, DEMANGLE_COMPONENT_CAST
,
1448 cplus_demangle_type (di
), NULL
);
1451 /* LOW is the inclusive lower bound. */
1453 /* HIGH is the exclusive upper bound. We subtract one to ignore
1454 the sentinel at the end of the array. */
1455 int high
= ((sizeof (cplus_demangle_operators
)
1456 / sizeof (cplus_demangle_operators
[0]))
1462 const struct demangle_operator_info
*p
;
1464 i
= low
+ (high
- low
) / 2;
1465 p
= cplus_demangle_operators
+ i
;
1467 if (c1
== p
->code
[0] && c2
== p
->code
[1])
1468 return d_make_operator (di
, p
);
1470 if (c1
< p
->code
[0] || (c1
== p
->code
[0] && c2
< p
->code
[1]))
1480 /* <special-name> ::= TV <type>
1484 ::= GV <(object) name>
1485 ::= T <call-offset> <(base) encoding>
1486 ::= Tc <call-offset> <call-offset> <(base) encoding>
1487 Also g++ extensions:
1488 ::= TC <type> <(offset) number> _ <(base) type>
1495 static struct demangle_component
*
1496 d_special_name (struct d_info
*di
)
1498 di
->expansion
+= 20;
1499 if (d_check_char (di
, 'T'))
1501 switch (d_next_char (di
))
1505 return d_make_comp (di
, DEMANGLE_COMPONENT_VTABLE
,
1506 cplus_demangle_type (di
), NULL
);
1508 di
->expansion
-= 10;
1509 return d_make_comp (di
, DEMANGLE_COMPONENT_VTT
,
1510 cplus_demangle_type (di
), NULL
);
1512 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO
,
1513 cplus_demangle_type (di
), NULL
);
1515 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO_NAME
,
1516 cplus_demangle_type (di
), NULL
);
1519 if (! d_call_offset (di
, 'h'))
1521 return d_make_comp (di
, DEMANGLE_COMPONENT_THUNK
,
1522 d_encoding (di
, 0), NULL
);
1525 if (! d_call_offset (di
, 'v'))
1527 return d_make_comp (di
, DEMANGLE_COMPONENT_VIRTUAL_THUNK
,
1528 d_encoding (di
, 0), NULL
);
1531 if (! d_call_offset (di
, '\0'))
1533 if (! d_call_offset (di
, '\0'))
1535 return d_make_comp (di
, DEMANGLE_COMPONENT_COVARIANT_THUNK
,
1536 d_encoding (di
, 0), NULL
);
1540 struct demangle_component
*derived_type
;
1542 struct demangle_component
*base_type
;
1544 derived_type
= cplus_demangle_type (di
);
1545 offset
= d_number (di
);
1548 if (! d_check_char (di
, '_'))
1550 base_type
= cplus_demangle_type (di
);
1551 /* We don't display the offset. FIXME: We should display
1552 it in verbose mode. */
1554 return d_make_comp (di
, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
,
1555 base_type
, derived_type
);
1559 return d_make_comp (di
, DEMANGLE_COMPONENT_TYPEINFO_FN
,
1560 cplus_demangle_type (di
), NULL
);
1562 return d_make_comp (di
, DEMANGLE_COMPONENT_JAVA_CLASS
,
1563 cplus_demangle_type (di
), NULL
);
1569 else if (d_check_char (di
, 'G'))
1571 switch (d_next_char (di
))
1574 return d_make_comp (di
, DEMANGLE_COMPONENT_GUARD
, d_name (di
), NULL
);
1577 return d_make_comp (di
, DEMANGLE_COMPONENT_REFTEMP
, d_name (di
),
1581 return d_make_comp (di
, DEMANGLE_COMPONENT_HIDDEN_ALIAS
,
1582 d_encoding (di
, 0), NULL
);
1592 /* <call-offset> ::= h <nv-offset> _
1595 <nv-offset> ::= <(offset) number>
1597 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1599 The C parameter, if not '\0', is a character we just read which is
1600 the start of the <call-offset>.
1602 We don't display the offset information anywhere. FIXME: We should
1603 display it in verbose mode. */
1606 d_call_offset (struct d_info
*di
, int c
)
1609 c
= d_next_char (di
);
1616 if (! d_check_char (di
, '_'))
1623 if (! d_check_char (di
, '_'))
1629 /* <ctor-dtor-name> ::= C1
1637 static struct demangle_component
*
1638 d_ctor_dtor_name (struct d_info
*di
)
1640 if (di
->last_name
!= NULL
)
1642 if (di
->last_name
->type
== DEMANGLE_COMPONENT_NAME
)
1643 di
->expansion
+= di
->last_name
->u
.s_name
.len
;
1644 else if (di
->last_name
->type
== DEMANGLE_COMPONENT_SUB_STD
)
1645 di
->expansion
+= di
->last_name
->u
.s_string
.len
;
1647 switch (d_peek_char (di
))
1651 enum gnu_v3_ctor_kinds kind
;
1653 switch (d_peek_next_char (di
))
1656 kind
= gnu_v3_complete_object_ctor
;
1659 kind
= gnu_v3_base_object_ctor
;
1662 kind
= gnu_v3_complete_object_allocating_ctor
;
1668 return d_make_ctor (di
, kind
, di
->last_name
);
1673 enum gnu_v3_dtor_kinds kind
;
1675 switch (d_peek_next_char (di
))
1678 kind
= gnu_v3_deleting_dtor
;
1681 kind
= gnu_v3_complete_object_dtor
;
1684 kind
= gnu_v3_base_object_dtor
;
1690 return d_make_dtor (di
, kind
, di
->last_name
);
1698 /* <type> ::= <builtin-type>
1700 ::= <class-enum-type>
1702 ::= <pointer-to-member-type>
1703 ::= <template-param>
1704 ::= <template-template-param> <template-args>
1706 ::= <CV-qualifiers> <type>
1711 ::= U <source-name> <type>
1713 <builtin-type> ::= various one letter codes
1717 CP_STATIC_IF_GLIBCPP_V3
1718 const struct demangle_builtin_type_info
1719 cplus_demangle_builtin_types
[D_BUILTIN_TYPE_COUNT
] =
1721 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT
},
1722 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL
},
1723 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT
},
1724 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT
},
1725 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT
},
1726 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT
},
1727 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT
},
1728 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT
},
1729 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT
},
1730 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED
},
1731 /* k */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
1732 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG
},
1733 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG
},
1734 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT
},
1735 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
1737 /* p */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
1738 /* q */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
1739 /* r */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
1740 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT
},
1741 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT
},
1742 /* u */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
1743 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID
},
1744 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT
},
1745 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG
},
1746 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
1747 D_PRINT_UNSIGNED_LONG_LONG
},
1748 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT
},
1751 CP_STATIC_IF_GLIBCPP_V3
1752 struct demangle_component
*
1753 cplus_demangle_type (struct d_info
*di
)
1756 struct demangle_component
*ret
;
1759 /* The ABI specifies that when CV-qualifiers are used, the base type
1760 is substitutable, and the fully qualified type is substitutable,
1761 but the base type with a strict subset of the CV-qualifiers is
1762 not substitutable. The natural recursive implementation of the
1763 CV-qualifiers would cause subsets to be substitutable, so instead
1764 we pull them all off now.
1766 FIXME: The ABI says that order-insensitive vendor qualifiers
1767 should be handled in the same way, but we have no way to tell
1768 which vendor qualifiers are order-insensitive and which are
1769 order-sensitive. So we just assume that they are all
1770 order-sensitive. g++ 3.4 supports only one vendor qualifier,
1771 __vector, and it treats it as order-sensitive when mangling
1774 peek
= d_peek_char (di
);
1775 if (peek
== 'r' || peek
== 'V' || peek
== 'K')
1777 struct demangle_component
**pret
;
1779 pret
= d_cv_qualifiers (di
, &ret
, 0);
1782 *pret
= cplus_demangle_type (di
);
1783 if (! *pret
|| ! d_add_substitution (di
, ret
))
1792 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1793 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
1794 case 'o': case 's': case 't':
1795 case 'v': case 'w': case 'x': case 'y': case 'z':
1796 ret
= d_make_builtin_type (di
,
1797 &cplus_demangle_builtin_types
[peek
- 'a']);
1798 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
1805 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_VENDOR_TYPE
,
1806 d_source_name (di
), NULL
);
1810 ret
= d_function_type (di
);
1813 case '0': case '1': case '2': case '3': case '4':
1814 case '5': case '6': case '7': case '8': case '9':
1817 ret
= d_class_enum_type (di
);
1821 ret
= d_array_type (di
);
1825 ret
= d_pointer_to_member_type (di
);
1829 ret
= d_template_param (di
);
1830 if (d_peek_char (di
) == 'I')
1832 /* This is <template-template-param> <template-args>. The
1833 <template-template-param> part is a substitution
1835 if (! d_add_substitution (di
, ret
))
1837 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
,
1838 d_template_args (di
));
1843 /* If this is a special substitution, then it is the start of
1844 <class-enum-type>. */
1848 peek_next
= d_peek_next_char (di
);
1849 if (IS_DIGIT (peek_next
)
1851 || IS_UPPER (peek_next
))
1853 ret
= d_substitution (di
, 0);
1854 /* The substituted name may have been a template name and
1855 may be followed by tepmlate args. */
1856 if (d_peek_char (di
) == 'I')
1857 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, ret
,
1858 d_template_args (di
));
1864 ret
= d_class_enum_type (di
);
1865 /* If the substitution was a complete type, then it is not
1866 a new substitution candidate. However, if the
1867 substitution was followed by template arguments, then
1868 the whole thing is a substitution candidate. */
1869 if (ret
!= NULL
&& ret
->type
== DEMANGLE_COMPONENT_SUB_STD
)
1877 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_POINTER
,
1878 cplus_demangle_type (di
), NULL
);
1883 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_REFERENCE
,
1884 cplus_demangle_type (di
), NULL
);
1889 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_COMPLEX
,
1890 cplus_demangle_type (di
), NULL
);
1895 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_IMAGINARY
,
1896 cplus_demangle_type (di
), NULL
);
1901 ret
= d_source_name (di
);
1902 ret
= d_make_comp (di
, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
,
1903 cplus_demangle_type (di
), ret
);
1912 if (! d_add_substitution (di
, ret
))
1919 /* <CV-qualifiers> ::= [r] [V] [K] */
1921 static struct demangle_component
**
1922 d_cv_qualifiers (struct d_info
*di
,
1923 struct demangle_component
**pret
, int member_fn
)
1927 peek
= d_peek_char (di
);
1928 while (peek
== 'r' || peek
== 'V' || peek
== 'K')
1930 enum demangle_component_type t
;
1936 ? DEMANGLE_COMPONENT_RESTRICT_THIS
1937 : DEMANGLE_COMPONENT_RESTRICT
);
1938 di
->expansion
+= sizeof "restrict";
1940 else if (peek
== 'V')
1943 ? DEMANGLE_COMPONENT_VOLATILE_THIS
1944 : DEMANGLE_COMPONENT_VOLATILE
);
1945 di
->expansion
+= sizeof "volatile";
1950 ? DEMANGLE_COMPONENT_CONST_THIS
1951 : DEMANGLE_COMPONENT_CONST
);
1952 di
->expansion
+= sizeof "const";
1955 *pret
= d_make_comp (di
, t
, NULL
, NULL
);
1958 pret
= &d_left (*pret
);
1960 peek
= d_peek_char (di
);
1966 /* <function-type> ::= F [Y] <bare-function-type> E */
1968 static struct demangle_component
*
1969 d_function_type (struct d_info
*di
)
1971 struct demangle_component
*ret
;
1973 if (! d_check_char (di
, 'F'))
1975 if (d_peek_char (di
) == 'Y')
1977 /* Function has C linkage. We don't print this information.
1978 FIXME: We should print it in verbose mode. */
1981 ret
= d_bare_function_type (di
, 1);
1982 if (! d_check_char (di
, 'E'))
1987 /* <bare-function-type> ::= [J]<type>+ */
1989 static struct demangle_component
*
1990 d_bare_function_type (struct d_info
*di
, int has_return_type
)
1992 struct demangle_component
*return_type
;
1993 struct demangle_component
*tl
;
1994 struct demangle_component
**ptl
;
1997 /* Detect special qualifier indicating that the first argument
1998 is the return type. */
1999 peek
= d_peek_char (di
);
2003 has_return_type
= 1;
2011 struct demangle_component
*type
;
2013 peek
= d_peek_char (di
);
2014 if (peek
== '\0' || peek
== 'E')
2016 type
= cplus_demangle_type (di
);
2019 if (has_return_type
)
2022 has_return_type
= 0;
2026 *ptl
= d_make_comp (di
, DEMANGLE_COMPONENT_ARGLIST
, type
, NULL
);
2029 ptl
= &d_right (*ptl
);
2033 /* There should be at least one parameter type besides the optional
2034 return type. A function which takes no arguments will have a
2035 single parameter type void. */
2039 /* If we have a single parameter type void, omit it. */
2040 if (d_right (tl
) == NULL
2041 && d_left (tl
)->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
2042 && d_left (tl
)->u
.s_builtin
.type
->print
== D_PRINT_VOID
)
2044 di
->expansion
-= d_left (tl
)->u
.s_builtin
.type
->len
;
2048 return d_make_comp (di
, DEMANGLE_COMPONENT_FUNCTION_TYPE
, return_type
, tl
);
2051 /* <class-enum-type> ::= <name> */
2053 static struct demangle_component
*
2054 d_class_enum_type (struct d_info
*di
)
2059 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2060 ::= A [<(dimension) expression>] _ <(element) type>
2063 static struct demangle_component
*
2064 d_array_type (struct d_info
*di
)
2067 struct demangle_component
*dim
;
2069 if (! d_check_char (di
, 'A'))
2072 peek
= d_peek_char (di
);
2075 else if (IS_DIGIT (peek
))
2083 peek
= d_peek_char (di
);
2085 while (IS_DIGIT (peek
));
2086 dim
= d_make_name (di
, s
, d_str (di
) - s
);
2092 dim
= d_expression (di
);
2097 if (! d_check_char (di
, '_'))
2100 return d_make_comp (di
, DEMANGLE_COMPONENT_ARRAY_TYPE
, dim
,
2101 cplus_demangle_type (di
));
2104 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2106 static struct demangle_component
*
2107 d_pointer_to_member_type (struct d_info
*di
)
2109 struct demangle_component
*cl
;
2110 struct demangle_component
*mem
;
2111 struct demangle_component
**pmem
;
2113 if (! d_check_char (di
, 'M'))
2116 cl
= cplus_demangle_type (di
);
2118 /* The ABI specifies that any type can be a substitution source, and
2119 that M is followed by two types, and that when a CV-qualified
2120 type is seen both the base type and the CV-qualified types are
2121 substitution sources. The ABI also specifies that for a pointer
2122 to a CV-qualified member function, the qualifiers are attached to
2123 the second type. Given the grammar, a plain reading of the ABI
2124 suggests that both the CV-qualified member function and the
2125 non-qualified member function are substitution sources. However,
2126 g++ does not work that way. g++ treats only the CV-qualified
2127 member function as a substitution source. FIXME. So to work
2128 with g++, we need to pull off the CV-qualifiers here, in order to
2129 avoid calling add_substitution() in cplus_demangle_type(). But
2130 for a CV-qualified member which is not a function, g++ does
2131 follow the ABI, so we need to handle that case here by calling
2132 d_add_substitution ourselves. */
2134 pmem
= d_cv_qualifiers (di
, &mem
, 1);
2137 *pmem
= cplus_demangle_type (di
);
2141 if (pmem
!= &mem
&& (*pmem
)->type
!= DEMANGLE_COMPONENT_FUNCTION_TYPE
)
2143 if (! d_add_substitution (di
, mem
))
2147 return d_make_comp (di
, DEMANGLE_COMPONENT_PTRMEM_TYPE
, cl
, mem
);
2150 /* <template-param> ::= T_
2151 ::= T <(parameter-2 non-negative) number> _
2154 static struct demangle_component
*
2155 d_template_param (struct d_info
*di
)
2159 if (! d_check_char (di
, 'T'))
2162 if (d_peek_char (di
) == '_')
2166 param
= d_number (di
);
2172 if (! d_check_char (di
, '_'))
2177 return d_make_template_param (di
, param
);
2180 /* <template-args> ::= I <template-arg>+ E */
2182 static struct demangle_component
*
2183 d_template_args (struct d_info
*di
)
2185 struct demangle_component
*hold_last_name
;
2186 struct demangle_component
*al
;
2187 struct demangle_component
**pal
;
2189 /* Preserve the last name we saw--don't let the template arguments
2190 clobber it, as that would give us the wrong name for a subsequent
2191 constructor or destructor. */
2192 hold_last_name
= di
->last_name
;
2194 if (! d_check_char (di
, 'I'))
2201 struct demangle_component
*a
;
2203 a
= d_template_arg (di
);
2207 *pal
= d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
, a
, NULL
);
2210 pal
= &d_right (*pal
);
2212 if (d_peek_char (di
) == 'E')
2219 di
->last_name
= hold_last_name
;
2224 /* <template-arg> ::= <type>
2225 ::= X <expression> E
2229 static struct demangle_component
*
2230 d_template_arg (struct d_info
*di
)
2232 struct demangle_component
*ret
;
2234 switch (d_peek_char (di
))
2238 ret
= d_expression (di
);
2239 if (! d_check_char (di
, 'E'))
2244 return d_expr_primary (di
);
2247 return cplus_demangle_type (di
);
2251 /* <expression> ::= <(unary) operator-name> <expression>
2252 ::= <(binary) operator-name> <expression> <expression>
2253 ::= <(trinary) operator-name> <expression> <expression> <expression>
2255 ::= <template-param>
2256 ::= sr <type> <unqualified-name>
2257 ::= sr <type> <unqualified-name> <template-args>
2261 static struct demangle_component
*
2262 d_expression (struct d_info
*di
)
2266 peek
= d_peek_char (di
);
2268 return d_expr_primary (di
);
2269 else if (peek
== 'T')
2270 return d_template_param (di
);
2271 else if (peek
== 's' && d_peek_next_char (di
) == 'r')
2273 struct demangle_component
*type
;
2274 struct demangle_component
*name
;
2277 type
= cplus_demangle_type (di
);
2278 name
= d_unqualified_name (di
);
2279 if (d_peek_char (di
) != 'I')
2280 return d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
, type
, name
);
2282 return d_make_comp (di
, DEMANGLE_COMPONENT_QUAL_NAME
, type
,
2283 d_make_comp (di
, DEMANGLE_COMPONENT_TEMPLATE
, name
,
2284 d_template_args (di
)));
2288 struct demangle_component
*op
;
2291 op
= d_operator_name (di
);
2295 if (op
->type
== DEMANGLE_COMPONENT_OPERATOR
)
2296 di
->expansion
+= op
->u
.s_operator
.op
->len
- 2;
2298 if (op
->type
== DEMANGLE_COMPONENT_OPERATOR
2299 && strcmp (op
->u
.s_operator
.op
->code
, "st") == 0)
2300 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
2301 cplus_demangle_type (di
));
2307 case DEMANGLE_COMPONENT_OPERATOR
:
2308 args
= op
->u
.s_operator
.op
->args
;
2310 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
2311 args
= op
->u
.s_extended_operator
.args
;
2313 case DEMANGLE_COMPONENT_CAST
:
2321 return d_make_comp (di
, DEMANGLE_COMPONENT_UNARY
, op
,
2325 struct demangle_component
*left
;
2327 left
= d_expression (di
);
2328 return d_make_comp (di
, DEMANGLE_COMPONENT_BINARY
, op
,
2330 DEMANGLE_COMPONENT_BINARY_ARGS
,
2332 d_expression (di
)));
2336 struct demangle_component
*first
;
2337 struct demangle_component
*second
;
2339 first
= d_expression (di
);
2340 second
= d_expression (di
);
2341 return d_make_comp (di
, DEMANGLE_COMPONENT_TRINARY
, op
,
2343 DEMANGLE_COMPONENT_TRINARY_ARG1
,
2346 DEMANGLE_COMPONENT_TRINARY_ARG2
,
2348 d_expression (di
))));
2356 /* <expr-primary> ::= L <type> <(value) number> E
2357 ::= L <type> <(value) float> E
2358 ::= L <mangled-name> E
2361 static struct demangle_component
*
2362 d_expr_primary (struct d_info
*di
)
2364 struct demangle_component
*ret
;
2366 if (! d_check_char (di
, 'L'))
2368 if (d_peek_char (di
) == '_')
2369 ret
= cplus_demangle_mangled_name (di
, 0);
2372 struct demangle_component
*type
;
2373 enum demangle_component_type t
;
2376 type
= cplus_demangle_type (di
);
2380 /* If we have a type we know how to print, we aren't going to
2381 print the type name itself. */
2382 if (type
->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
2383 && type
->u
.s_builtin
.type
->print
!= D_PRINT_DEFAULT
)
2384 di
->expansion
-= type
->u
.s_builtin
.type
->len
;
2386 /* Rather than try to interpret the literal value, we just
2387 collect it as a string. Note that it's possible to have a
2388 floating point literal here. The ABI specifies that the
2389 format of such literals is machine independent. That's fine,
2390 but what's not fine is that versions of g++ up to 3.2 with
2391 -fabi-version=1 used upper case letters in the hex constant,
2392 and dumped out gcc's internal representation. That makes it
2393 hard to tell where the constant ends, and hard to dump the
2394 constant in any readable form anyhow. We don't attempt to
2395 handle these cases. */
2397 t
= DEMANGLE_COMPONENT_LITERAL
;
2398 if (d_peek_char (di
) == 'n')
2400 t
= DEMANGLE_COMPONENT_LITERAL_NEG
;
2404 while (d_peek_char (di
) != 'E')
2406 if (d_peek_char (di
) == '\0')
2410 ret
= d_make_comp (di
, t
, type
, d_make_name (di
, s
, d_str (di
) - s
));
2412 if (! d_check_char (di
, 'E'))
2417 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2418 ::= Z <(function) encoding> E s [<discriminator>]
2421 static struct demangle_component
*
2422 d_local_name (struct d_info
*di
)
2424 struct demangle_component
*function
;
2426 if (! d_check_char (di
, 'Z'))
2429 function
= d_encoding (di
, 0);
2431 if (! d_check_char (di
, 'E'))
2434 if (d_peek_char (di
) == 's')
2437 if (! d_discriminator (di
))
2439 return d_make_comp (di
, DEMANGLE_COMPONENT_LOCAL_NAME
, function
,
2440 d_make_name (di
, "string literal",
2441 sizeof "string literal" - 1));
2445 struct demangle_component
*name
;
2448 if (! d_discriminator (di
))
2450 return d_make_comp (di
, DEMANGLE_COMPONENT_LOCAL_NAME
, function
, name
);
2454 /* <discriminator> ::= _ <(non-negative) number>
2456 We demangle the discriminator, but we don't print it out. FIXME:
2457 We should print it out in verbose mode. */
2460 d_discriminator (struct d_info
*di
)
2464 if (d_peek_char (di
) != '_')
2467 discrim
= d_number (di
);
2473 /* Add a new substitution. */
2476 d_add_substitution (struct d_info
*di
, struct demangle_component
*dc
)
2480 if (di
->next_sub
>= di
->num_subs
)
2482 di
->subs
[di
->next_sub
] = dc
;
2487 /* <substitution> ::= S <seq-id> _
2497 If PREFIX is non-zero, then this type is being used as a prefix in
2498 a qualified name. In this case, for the standard substitutions, we
2499 need to check whether we are being used as a prefix for a
2500 constructor or destructor, and return a full template name.
2501 Otherwise we will get something like std::iostream::~iostream()
2502 which does not correspond particularly well to any function which
2503 actually appears in the source.
2506 static const struct d_standard_sub_info standard_subs
[] =
2511 { 'a', NL ("std::allocator"),
2512 NL ("std::allocator"),
2514 { 'b', NL ("std::basic_string"),
2515 NL ("std::basic_string"),
2516 NL ("basic_string") },
2517 { 's', NL ("std::string"),
2518 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
2519 NL ("basic_string") },
2520 { 'i', NL ("std::istream"),
2521 NL ("std::basic_istream<char, std::char_traits<char> >"),
2522 NL ("basic_istream") },
2523 { 'o', NL ("std::ostream"),
2524 NL ("std::basic_ostream<char, std::char_traits<char> >"),
2525 NL ("basic_ostream") },
2526 { 'd', NL ("std::iostream"),
2527 NL ("std::basic_iostream<char, std::char_traits<char> >"),
2528 NL ("basic_iostream") }
2531 static struct demangle_component
*
2532 d_substitution (struct d_info
*di
, int prefix
)
2536 if (! d_check_char (di
, 'S'))
2539 c
= d_next_char (di
);
2540 if (c
== '_' || IS_DIGIT (c
) || IS_UPPER (c
))
2550 id
= id
* 36 + c
- '0';
2551 else if (IS_UPPER (c
))
2552 id
= id
* 36 + c
- 'A' + 10;
2557 c
= d_next_char (di
);
2564 if (id
>= di
->next_sub
)
2569 return di
->subs
[id
];
2574 const struct d_standard_sub_info
*p
;
2575 const struct d_standard_sub_info
*pend
;
2577 verbose
= (di
->options
& DMGL_VERBOSE
) != 0;
2578 if (! verbose
&& prefix
)
2582 peek
= d_peek_char (di
);
2583 if (peek
== 'C' || peek
== 'D')
2587 pend
= (&standard_subs
[0]
2588 + sizeof standard_subs
/ sizeof standard_subs
[0]);
2589 for (p
= &standard_subs
[0]; p
< pend
; ++p
)
2596 if (p
->set_last_name
!= NULL
)
2597 di
->last_name
= d_make_sub (di
, p
->set_last_name
,
2598 p
->set_last_name_len
);
2601 s
= p
->full_expansion
;
2606 s
= p
->simple_expansion
;
2607 len
= p
->simple_len
;
2609 di
->expansion
+= len
;
2610 return d_make_sub (di
, s
, len
);
2618 /* Initialize a growable string. */
2621 d_growable_string_init (struct d_growable_string
*dgs
, size_t estimate
)
2626 dgs
->allocation_failure
= 0;
2629 d_growable_string_resize (dgs
, estimate
);
2632 /* Grow a growable string to a given size. */
2635 d_growable_string_resize (struct d_growable_string
*dgs
, size_t need
)
2640 if (dgs
->allocation_failure
)
2643 /* Start allocation at two bytes to avoid any possibility of confusion
2644 with the special value of 1 used as a return in *palc to indicate
2645 allocation failures. */
2646 newalc
= dgs
->alc
> 0 ? dgs
->alc
: 2;
2647 while (newalc
< need
)
2650 newbuf
= (char *) realloc (dgs
->buf
, newalc
);
2657 dgs
->allocation_failure
= 1;
2664 /* Append a buffer to a growable string. */
2667 d_growable_string_append_buffer (struct d_growable_string
*dgs
,
2668 const char *s
, size_t l
)
2672 need
= dgs
->len
+ l
+ 1;
2673 if (need
> dgs
->alc
)
2674 d_growable_string_resize (dgs
, need
);
2676 if (dgs
->allocation_failure
)
2679 memcpy (dgs
->buf
+ dgs
->len
, s
, l
);
2680 dgs
->buf
[dgs
->len
+ l
] = '\0';
2684 /* Bridge growable strings to the callback mechanism. */
2687 d_growable_string_callback_adapter (const char *s
, size_t l
, void *opaque
)
2689 struct d_growable_string
*dgs
= (struct d_growable_string
*) opaque
;
2691 d_growable_string_append_buffer (dgs
, s
, l
);
2694 /* Initialize a print information structure. */
2697 d_print_init (struct d_print_info
*dpi
, int options
,
2698 demangle_callbackref callback
, void *opaque
)
2700 dpi
->options
= options
;
2702 dpi
->last_char
= '\0';
2703 dpi
->templates
= NULL
;
2704 dpi
->modifiers
= NULL
;
2706 dpi
->callback
= callback
;
2707 dpi
->opaque
= opaque
;
2709 dpi
->demangle_failure
= 0;
2712 /* Indicate that an error occurred during printing, and test for error. */
2715 d_print_error (struct d_print_info
*dpi
)
2717 dpi
->demangle_failure
= 1;
2721 d_print_saw_error (struct d_print_info
*dpi
)
2723 return dpi
->demangle_failure
!= 0;
2726 /* Flush buffered characters to the callback. */
2729 d_print_flush (struct d_print_info
*dpi
)
2731 dpi
->buf
[dpi
->len
] = '\0';
2732 dpi
->callback (dpi
->buf
, dpi
->len
, dpi
->opaque
);
2736 /* Append characters and buffers for printing. */
2739 d_append_char (struct d_print_info
*dpi
, char c
)
2741 if (dpi
->len
== sizeof (dpi
->buf
) - 1)
2742 d_print_flush (dpi
);
2744 dpi
->buf
[dpi
->len
++] = c
;
2749 d_append_buffer (struct d_print_info
*dpi
, const char *s
, size_t l
)
2753 for (i
= 0; i
< l
; i
++)
2754 d_append_char (dpi
, s
[i
]);
2758 d_append_string (struct d_print_info
*dpi
, const char *s
)
2760 d_append_buffer (dpi
, s
, strlen (s
));
2764 d_last_char (struct d_print_info
*dpi
)
2766 return dpi
->last_char
;
2769 /* Turn components into a human readable string. OPTIONS is the
2770 options bits passed to the demangler. DC is the tree to print.
2771 CALLBACK is a function to call to flush demangled string segments
2772 as they fill the intermediate buffer, and OPAQUE is a generalized
2773 callback argument. On success, this returns 1. On failure,
2774 it returns 0, indicating a bad parse. It does not use heap
2775 memory to build an output string, so cannot encounter memory
2776 allocation failure. */
2778 CP_STATIC_IF_GLIBCPP_V3
2780 cplus_demangle_print_callback (int options
,
2781 const struct demangle_component
*dc
,
2782 demangle_callbackref callback
, void *opaque
)
2784 struct d_print_info dpi
;
2786 d_print_init (&dpi
, options
, callback
, opaque
);
2788 d_print_comp (&dpi
, dc
);
2790 d_print_flush (&dpi
);
2792 return ! d_print_saw_error (&dpi
);
2795 /* Turn components into a human readable string. OPTIONS is the
2796 options bits passed to the demangler. DC is the tree to print.
2797 ESTIMATE is a guess at the length of the result. This returns a
2798 string allocated by malloc, or NULL on error. On success, this
2799 sets *PALC to the size of the allocated buffer. On failure, this
2800 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
2803 CP_STATIC_IF_GLIBCPP_V3
2805 cplus_demangle_print (int options
, const struct demangle_component
*dc
,
2806 int estimate
, size_t *palc
)
2808 struct d_growable_string dgs
;
2810 d_growable_string_init (&dgs
, estimate
);
2812 if (! cplus_demangle_print_callback (options
, dc
,
2813 d_growable_string_callback_adapter
,
2821 *palc
= dgs
.allocation_failure
? 1 : dgs
.alc
;
2825 /* Subroutine to handle components. */
2828 d_print_comp (struct d_print_info
*dpi
,
2829 const struct demangle_component
*dc
)
2833 d_print_error (dpi
);
2836 if (d_print_saw_error (dpi
))
2841 case DEMANGLE_COMPONENT_NAME
:
2842 if ((dpi
->options
& DMGL_JAVA
) == 0)
2843 d_append_buffer (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
2845 d_print_java_identifier (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
2848 case DEMANGLE_COMPONENT_QUAL_NAME
:
2849 case DEMANGLE_COMPONENT_LOCAL_NAME
:
2850 d_print_comp (dpi
, d_left (dc
));
2851 if ((dpi
->options
& DMGL_JAVA
) == 0)
2852 d_append_string (dpi
, "::");
2854 d_append_char (dpi
, '.');
2855 d_print_comp (dpi
, d_right (dc
));
2858 case DEMANGLE_COMPONENT_TYPED_NAME
:
2860 struct d_print_mod
*hold_modifiers
;
2861 struct demangle_component
*typed_name
;
2862 struct d_print_mod adpm
[4];
2864 struct d_print_template dpt
;
2866 /* Pass the name down to the type so that it can be printed in
2867 the right place for the type. We also have to pass down
2868 any CV-qualifiers, which apply to the this parameter. */
2869 hold_modifiers
= dpi
->modifiers
;
2871 typed_name
= d_left (dc
);
2872 while (typed_name
!= NULL
)
2874 if (i
>= sizeof adpm
/ sizeof adpm
[0])
2876 d_print_error (dpi
);
2880 adpm
[i
].next
= dpi
->modifiers
;
2881 dpi
->modifiers
= &adpm
[i
];
2882 adpm
[i
].mod
= typed_name
;
2883 adpm
[i
].printed
= 0;
2884 adpm
[i
].templates
= dpi
->templates
;
2887 if (typed_name
->type
!= DEMANGLE_COMPONENT_RESTRICT_THIS
2888 && typed_name
->type
!= DEMANGLE_COMPONENT_VOLATILE_THIS
2889 && typed_name
->type
!= DEMANGLE_COMPONENT_CONST_THIS
)
2892 typed_name
= d_left (typed_name
);
2895 /* If typed_name is a template, then it applies to the
2896 function type as well. */
2897 if (typed_name
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
2899 dpt
.next
= dpi
->templates
;
2900 dpi
->templates
= &dpt
;
2901 dpt
.template_decl
= typed_name
;
2904 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
2905 there may be CV-qualifiers on its right argument which
2906 really apply here; this happens when parsing a class which
2907 is local to a function. */
2908 if (typed_name
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
2910 struct demangle_component
*local_name
;
2912 local_name
= d_right (typed_name
);
2913 while (local_name
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
2914 || local_name
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
2915 || local_name
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
2917 if (i
>= sizeof adpm
/ sizeof adpm
[0])
2919 d_print_error (dpi
);
2923 adpm
[i
] = adpm
[i
- 1];
2924 adpm
[i
].next
= &adpm
[i
- 1];
2925 dpi
->modifiers
= &adpm
[i
];
2927 adpm
[i
- 1].mod
= local_name
;
2928 adpm
[i
- 1].printed
= 0;
2929 adpm
[i
- 1].templates
= dpi
->templates
;
2932 local_name
= d_left (local_name
);
2936 d_print_comp (dpi
, d_right (dc
));
2938 if (typed_name
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
2939 dpi
->templates
= dpt
.next
;
2941 /* If the modifiers didn't get printed by the type, print them
2946 if (! adpm
[i
].printed
)
2948 d_append_char (dpi
, ' ');
2949 d_print_mod (dpi
, adpm
[i
].mod
);
2953 dpi
->modifiers
= hold_modifiers
;
2958 case DEMANGLE_COMPONENT_TEMPLATE
:
2960 struct d_print_mod
*hold_dpm
;
2961 struct demangle_component
*dcl
;
2963 /* Don't push modifiers into a template definition. Doing so
2964 could give the wrong definition for a template argument.
2965 Instead, treat the template essentially as a name. */
2967 hold_dpm
= dpi
->modifiers
;
2968 dpi
->modifiers
= NULL
;
2972 if ((dpi
->options
& DMGL_JAVA
) != 0
2973 && dcl
->type
== DEMANGLE_COMPONENT_NAME
2974 && dcl
->u
.s_name
.len
== 6
2975 && strncmp (dcl
->u
.s_name
.s
, "JArray", 6) == 0)
2977 /* Special-case Java arrays, so that JArray<TYPE> appears
2978 instead as TYPE[]. */
2980 d_print_comp (dpi
, d_right (dc
));
2981 d_append_string (dpi
, "[]");
2985 d_print_comp (dpi
, dcl
);
2986 if (d_last_char (dpi
) == '<')
2987 d_append_char (dpi
, ' ');
2988 d_append_char (dpi
, '<');
2989 d_print_comp (dpi
, d_right (dc
));
2990 /* Avoid generating two consecutive '>' characters, to avoid
2991 the C++ syntactic ambiguity. */
2992 if (d_last_char (dpi
) == '>')
2993 d_append_char (dpi
, ' ');
2994 d_append_char (dpi
, '>');
2997 dpi
->modifiers
= hold_dpm
;
3002 case DEMANGLE_COMPONENT_TEMPLATE_PARAM
:
3005 struct demangle_component
*a
;
3006 struct d_print_template
*hold_dpt
;
3008 if (dpi
->templates
== NULL
)
3010 d_print_error (dpi
);
3013 i
= dc
->u
.s_number
.number
;
3014 for (a
= d_right (dpi
->templates
->template_decl
);
3018 if (a
->type
!= DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
)
3020 d_print_error (dpi
);
3027 if (i
!= 0 || a
== NULL
)
3029 d_print_error (dpi
);
3033 /* While processing this parameter, we need to pop the list of
3034 templates. This is because the template parameter may
3035 itself be a reference to a parameter of an outer
3038 hold_dpt
= dpi
->templates
;
3039 dpi
->templates
= hold_dpt
->next
;
3041 d_print_comp (dpi
, d_left (a
));
3043 dpi
->templates
= hold_dpt
;
3048 case DEMANGLE_COMPONENT_CTOR
:
3049 d_print_comp (dpi
, dc
->u
.s_ctor
.name
);
3052 case DEMANGLE_COMPONENT_DTOR
:
3053 d_append_char (dpi
, '~');
3054 d_print_comp (dpi
, dc
->u
.s_dtor
.name
);
3057 case DEMANGLE_COMPONENT_VTABLE
:
3058 d_append_string (dpi
, "vtable for ");
3059 d_print_comp (dpi
, d_left (dc
));
3062 case DEMANGLE_COMPONENT_VTT
:
3063 d_append_string (dpi
, "VTT for ");
3064 d_print_comp (dpi
, d_left (dc
));
3067 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE
:
3068 d_append_string (dpi
, "construction vtable for ");
3069 d_print_comp (dpi
, d_left (dc
));
3070 d_append_string (dpi
, "-in-");
3071 d_print_comp (dpi
, d_right (dc
));
3074 case DEMANGLE_COMPONENT_TYPEINFO
:
3075 d_append_string (dpi
, "typeinfo for ");
3076 d_print_comp (dpi
, d_left (dc
));
3079 case DEMANGLE_COMPONENT_TYPEINFO_NAME
:
3080 d_append_string (dpi
, "typeinfo name for ");
3081 d_print_comp (dpi
, d_left (dc
));
3084 case DEMANGLE_COMPONENT_TYPEINFO_FN
:
3085 d_append_string (dpi
, "typeinfo fn for ");
3086 d_print_comp (dpi
, d_left (dc
));
3089 case DEMANGLE_COMPONENT_THUNK
:
3090 d_append_string (dpi
, "non-virtual thunk to ");
3091 d_print_comp (dpi
, d_left (dc
));
3094 case DEMANGLE_COMPONENT_VIRTUAL_THUNK
:
3095 d_append_string (dpi
, "virtual thunk to ");
3096 d_print_comp (dpi
, d_left (dc
));
3099 case DEMANGLE_COMPONENT_COVARIANT_THUNK
:
3100 d_append_string (dpi
, "covariant return thunk to ");
3101 d_print_comp (dpi
, d_left (dc
));
3104 case DEMANGLE_COMPONENT_JAVA_CLASS
:
3105 d_append_string (dpi
, "java Class for ");
3106 d_print_comp (dpi
, d_left (dc
));
3109 case DEMANGLE_COMPONENT_GUARD
:
3110 d_append_string (dpi
, "guard variable for ");
3111 d_print_comp (dpi
, d_left (dc
));
3114 case DEMANGLE_COMPONENT_REFTEMP
:
3115 d_append_string (dpi
, "reference temporary for ");
3116 d_print_comp (dpi
, d_left (dc
));
3119 case DEMANGLE_COMPONENT_HIDDEN_ALIAS
:
3120 d_append_string (dpi
, "hidden alias for ");
3121 d_print_comp (dpi
, d_left (dc
));
3124 case DEMANGLE_COMPONENT_SUB_STD
:
3125 d_append_buffer (dpi
, dc
->u
.s_string
.string
, dc
->u
.s_string
.len
);
3128 case DEMANGLE_COMPONENT_RESTRICT
:
3129 case DEMANGLE_COMPONENT_VOLATILE
:
3130 case DEMANGLE_COMPONENT_CONST
:
3132 struct d_print_mod
*pdpm
;
3134 /* When printing arrays, it's possible to have cases where the
3135 same CV-qualifier gets pushed on the stack multiple times.
3136 We only need to print it once. */
3138 for (pdpm
= dpi
->modifiers
; pdpm
!= NULL
; pdpm
= pdpm
->next
)
3140 if (! pdpm
->printed
)
3142 if (pdpm
->mod
->type
!= DEMANGLE_COMPONENT_RESTRICT
3143 && pdpm
->mod
->type
!= DEMANGLE_COMPONENT_VOLATILE
3144 && pdpm
->mod
->type
!= DEMANGLE_COMPONENT_CONST
)
3146 if (pdpm
->mod
->type
== dc
->type
)
3148 d_print_comp (dpi
, d_left (dc
));
3155 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
3156 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
3157 case DEMANGLE_COMPONENT_CONST_THIS
:
3158 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
3159 case DEMANGLE_COMPONENT_POINTER
:
3160 case DEMANGLE_COMPONENT_REFERENCE
:
3161 case DEMANGLE_COMPONENT_COMPLEX
:
3162 case DEMANGLE_COMPONENT_IMAGINARY
:
3164 /* We keep a list of modifiers on the stack. */
3165 struct d_print_mod dpm
;
3167 dpm
.next
= dpi
->modifiers
;
3168 dpi
->modifiers
= &dpm
;
3171 dpm
.templates
= dpi
->templates
;
3173 d_print_comp (dpi
, d_left (dc
));
3175 /* If the modifier didn't get printed by the type, print it
3178 d_print_mod (dpi
, dc
);
3180 dpi
->modifiers
= dpm
.next
;
3185 case DEMANGLE_COMPONENT_BUILTIN_TYPE
:
3186 if ((dpi
->options
& DMGL_JAVA
) == 0)
3187 d_append_buffer (dpi
, dc
->u
.s_builtin
.type
->name
,
3188 dc
->u
.s_builtin
.type
->len
);
3190 d_append_buffer (dpi
, dc
->u
.s_builtin
.type
->java_name
,
3191 dc
->u
.s_builtin
.type
->java_len
);
3194 case DEMANGLE_COMPONENT_VENDOR_TYPE
:
3195 d_print_comp (dpi
, d_left (dc
));
3198 case DEMANGLE_COMPONENT_FUNCTION_TYPE
:
3200 if ((dpi
->options
& DMGL_RET_POSTFIX
) != 0)
3201 d_print_function_type (dpi
, dc
, dpi
->modifiers
);
3203 /* Print return type if present */
3204 if (d_left (dc
) != NULL
)
3206 struct d_print_mod dpm
;
3208 /* We must pass this type down as a modifier in order to
3209 print it in the right location. */
3210 dpm
.next
= dpi
->modifiers
;
3211 dpi
->modifiers
= &dpm
;
3214 dpm
.templates
= dpi
->templates
;
3216 d_print_comp (dpi
, d_left (dc
));
3218 dpi
->modifiers
= dpm
.next
;
3223 /* In standard prefix notation, there is a space between the
3224 return type and the function signature. */
3225 if ((dpi
->options
& DMGL_RET_POSTFIX
) == 0)
3226 d_append_char (dpi
, ' ');
3229 if ((dpi
->options
& DMGL_RET_POSTFIX
) == 0)
3230 d_print_function_type (dpi
, dc
, dpi
->modifiers
);
3235 case DEMANGLE_COMPONENT_ARRAY_TYPE
:
3237 struct d_print_mod
*hold_modifiers
;
3238 struct d_print_mod adpm
[4];
3240 struct d_print_mod
*pdpm
;
3242 /* We must pass this type down as a modifier in order to print
3243 multi-dimensional arrays correctly. If the array itself is
3244 CV-qualified, we act as though the element type were
3245 CV-qualified. We do this by copying the modifiers down
3246 rather than fiddling pointers, so that we don't wind up
3247 with a d_print_mod higher on the stack pointing into our
3248 stack frame after we return. */
3250 hold_modifiers
= dpi
->modifiers
;
3252 adpm
[0].next
= hold_modifiers
;
3253 dpi
->modifiers
= &adpm
[0];
3255 adpm
[0].printed
= 0;
3256 adpm
[0].templates
= dpi
->templates
;
3259 pdpm
= hold_modifiers
;
3261 && (pdpm
->mod
->type
== DEMANGLE_COMPONENT_RESTRICT
3262 || pdpm
->mod
->type
== DEMANGLE_COMPONENT_VOLATILE
3263 || pdpm
->mod
->type
== DEMANGLE_COMPONENT_CONST
))
3265 if (! pdpm
->printed
)
3267 if (i
>= sizeof adpm
/ sizeof adpm
[0])
3269 d_print_error (dpi
);
3274 adpm
[i
].next
= dpi
->modifiers
;
3275 dpi
->modifiers
= &adpm
[i
];
3283 d_print_comp (dpi
, d_right (dc
));
3285 dpi
->modifiers
= hold_modifiers
;
3287 if (adpm
[0].printed
)
3293 d_print_mod (dpi
, adpm
[i
].mod
);
3296 d_print_array_type (dpi
, dc
, dpi
->modifiers
);
3301 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
3303 struct d_print_mod dpm
;
3305 dpm
.next
= dpi
->modifiers
;
3306 dpi
->modifiers
= &dpm
;
3309 dpm
.templates
= dpi
->templates
;
3311 d_print_comp (dpi
, d_right (dc
));
3313 /* If the modifier didn't get printed by the type, print it
3317 d_append_char (dpi
, ' ');
3318 d_print_comp (dpi
, d_left (dc
));
3319 d_append_string (dpi
, "::*");
3322 dpi
->modifiers
= dpm
.next
;
3327 case DEMANGLE_COMPONENT_ARGLIST
:
3328 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
:
3329 d_print_comp (dpi
, d_left (dc
));
3330 if (d_right (dc
) != NULL
)
3332 d_append_string (dpi
, ", ");
3333 d_print_comp (dpi
, d_right (dc
));
3337 case DEMANGLE_COMPONENT_OPERATOR
:
3341 d_append_string (dpi
, "operator");
3342 c
= dc
->u
.s_operator
.op
->name
[0];
3344 d_append_char (dpi
, ' ');
3345 d_append_buffer (dpi
, dc
->u
.s_operator
.op
->name
,
3346 dc
->u
.s_operator
.op
->len
);
3350 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
3351 d_append_string (dpi
, "operator ");
3352 d_print_comp (dpi
, dc
->u
.s_extended_operator
.name
);
3355 case DEMANGLE_COMPONENT_CAST
:
3356 d_append_string (dpi
, "operator ");
3357 d_print_cast (dpi
, dc
);
3360 case DEMANGLE_COMPONENT_UNARY
:
3361 if (d_left (dc
)->type
!= DEMANGLE_COMPONENT_CAST
)
3362 d_print_expr_op (dpi
, d_left (dc
));
3365 d_append_char (dpi
, '(');
3366 d_print_cast (dpi
, d_left (dc
));
3367 d_append_char (dpi
, ')');
3369 d_append_char (dpi
, '(');
3370 d_print_comp (dpi
, d_right (dc
));
3371 d_append_char (dpi
, ')');
3374 case DEMANGLE_COMPONENT_BINARY
:
3375 if (d_right (dc
)->type
!= DEMANGLE_COMPONENT_BINARY_ARGS
)
3377 d_print_error (dpi
);
3381 /* We wrap an expression which uses the greater-than operator in
3382 an extra layer of parens so that it does not get confused
3383 with the '>' which ends the template parameters. */
3384 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_OPERATOR
3385 && d_left (dc
)->u
.s_operator
.op
->len
== 1
3386 && d_left (dc
)->u
.s_operator
.op
->name
[0] == '>')
3387 d_append_char (dpi
, '(');
3389 d_append_char (dpi
, '(');
3390 d_print_comp (dpi
, d_left (d_right (dc
)));
3391 d_append_string (dpi
, ") ");
3392 d_print_expr_op (dpi
, d_left (dc
));
3393 d_append_string (dpi
, " (");
3394 d_print_comp (dpi
, d_right (d_right (dc
)));
3395 d_append_char (dpi
, ')');
3397 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_OPERATOR
3398 && d_left (dc
)->u
.s_operator
.op
->len
== 1
3399 && d_left (dc
)->u
.s_operator
.op
->name
[0] == '>')
3400 d_append_char (dpi
, ')');
3404 case DEMANGLE_COMPONENT_BINARY_ARGS
:
3405 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
3406 d_print_error (dpi
);
3409 case DEMANGLE_COMPONENT_TRINARY
:
3410 if (d_right (dc
)->type
!= DEMANGLE_COMPONENT_TRINARY_ARG1
3411 || d_right (d_right (dc
))->type
!= DEMANGLE_COMPONENT_TRINARY_ARG2
)
3413 d_print_error (dpi
);
3416 d_append_char (dpi
, '(');
3417 d_print_comp (dpi
, d_left (d_right (dc
)));
3418 d_append_string (dpi
, ") ");
3419 d_print_expr_op (dpi
, d_left (dc
));
3420 d_append_string (dpi
, " (");
3421 d_print_comp (dpi
, d_left (d_right (d_right (dc
))));
3422 d_append_string (dpi
, ") : (");
3423 d_print_comp (dpi
, d_right (d_right (d_right (dc
))));
3424 d_append_char (dpi
, ')');
3427 case DEMANGLE_COMPONENT_TRINARY_ARG1
:
3428 case DEMANGLE_COMPONENT_TRINARY_ARG2
:
3429 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
3430 d_print_error (dpi
);
3433 case DEMANGLE_COMPONENT_LITERAL
:
3434 case DEMANGLE_COMPONENT_LITERAL_NEG
:
3436 enum d_builtin_type_print tp
;
3438 /* For some builtin types, produce simpler output. */
3439 tp
= D_PRINT_DEFAULT
;
3440 if (d_left (dc
)->type
== DEMANGLE_COMPONENT_BUILTIN_TYPE
)
3442 tp
= d_left (dc
)->u
.s_builtin
.type
->print
;
3446 case D_PRINT_UNSIGNED
:
3448 case D_PRINT_UNSIGNED_LONG
:
3449 case D_PRINT_LONG_LONG
:
3450 case D_PRINT_UNSIGNED_LONG_LONG
:
3451 if (d_right (dc
)->type
== DEMANGLE_COMPONENT_NAME
)
3453 if (dc
->type
== DEMANGLE_COMPONENT_LITERAL_NEG
)
3454 d_append_char (dpi
, '-');
3455 d_print_comp (dpi
, d_right (dc
));
3460 case D_PRINT_UNSIGNED
:
3461 d_append_char (dpi
, 'u');
3464 d_append_char (dpi
, 'l');
3466 case D_PRINT_UNSIGNED_LONG
:
3467 d_append_string (dpi
, "ul");
3469 case D_PRINT_LONG_LONG
:
3470 d_append_string (dpi
, "ll");
3472 case D_PRINT_UNSIGNED_LONG_LONG
:
3473 d_append_string (dpi
, "ull");
3481 if (d_right (dc
)->type
== DEMANGLE_COMPONENT_NAME
3482 && d_right (dc
)->u
.s_name
.len
== 1
3483 && dc
->type
== DEMANGLE_COMPONENT_LITERAL
)
3485 switch (d_right (dc
)->u
.s_name
.s
[0])
3488 d_append_string (dpi
, "false");
3491 d_append_string (dpi
, "true");
3504 d_append_char (dpi
, '(');
3505 d_print_comp (dpi
, d_left (dc
));
3506 d_append_char (dpi
, ')');
3507 if (dc
->type
== DEMANGLE_COMPONENT_LITERAL_NEG
)
3508 d_append_char (dpi
, '-');
3509 if (tp
== D_PRINT_FLOAT
)
3510 d_append_char (dpi
, '[');
3511 d_print_comp (dpi
, d_right (dc
));
3512 if (tp
== D_PRINT_FLOAT
)
3513 d_append_char (dpi
, ']');
3518 d_print_error (dpi
);
3523 /* Print a Java dentifier. For Java we try to handle encoded extended
3524 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
3525 so we don't it for C++. Characters are encoded as
3529 d_print_java_identifier (struct d_print_info
*dpi
, const char *name
, int len
)
3535 for (p
= name
; p
< end
; ++p
)
3546 for (q
= p
+ 3; q
< end
; ++q
)
3552 else if (*q
>= 'A' && *q
<= 'F')
3553 dig
= *q
- 'A' + 10;
3554 else if (*q
>= 'a' && *q
<= 'f')
3555 dig
= *q
- 'a' + 10;
3561 /* If the Unicode character is larger than 256, we don't try
3562 to deal with it here. FIXME. */
3563 if (q
< end
&& *q
== '_' && c
< 256)
3565 d_append_char (dpi
, c
);
3571 d_append_char (dpi
, *p
);
3575 /* Print a list of modifiers. SUFFIX is 1 if we are printing
3576 qualifiers on this after printing a function. */
3579 d_print_mod_list (struct d_print_info
*dpi
,
3580 struct d_print_mod
*mods
, int suffix
)
3582 struct d_print_template
*hold_dpt
;
3584 if (mods
== NULL
|| d_print_saw_error (dpi
))
3589 && (mods
->mod
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
3590 || mods
->mod
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
3591 || mods
->mod
->type
== DEMANGLE_COMPONENT_CONST_THIS
)))
3593 d_print_mod_list (dpi
, mods
->next
, suffix
);
3599 hold_dpt
= dpi
->templates
;
3600 dpi
->templates
= mods
->templates
;
3602 if (mods
->mod
->type
== DEMANGLE_COMPONENT_FUNCTION_TYPE
)
3604 d_print_function_type (dpi
, mods
->mod
, mods
->next
);
3605 dpi
->templates
= hold_dpt
;
3608 else if (mods
->mod
->type
== DEMANGLE_COMPONENT_ARRAY_TYPE
)
3610 d_print_array_type (dpi
, mods
->mod
, mods
->next
);
3611 dpi
->templates
= hold_dpt
;
3614 else if (mods
->mod
->type
== DEMANGLE_COMPONENT_LOCAL_NAME
)
3616 struct d_print_mod
*hold_modifiers
;
3617 struct demangle_component
*dc
;
3619 /* When this is on the modifier stack, we have pulled any
3620 qualifiers off the right argument already. Otherwise, we
3621 print it as usual, but don't let the left argument see any
3624 hold_modifiers
= dpi
->modifiers
;
3625 dpi
->modifiers
= NULL
;
3626 d_print_comp (dpi
, d_left (mods
->mod
));
3627 dpi
->modifiers
= hold_modifiers
;
3629 if ((dpi
->options
& DMGL_JAVA
) == 0)
3630 d_append_string (dpi
, "::");
3632 d_append_char (dpi
, '.');
3634 dc
= d_right (mods
->mod
);
3635 while (dc
->type
== DEMANGLE_COMPONENT_RESTRICT_THIS
3636 || dc
->type
== DEMANGLE_COMPONENT_VOLATILE_THIS
3637 || dc
->type
== DEMANGLE_COMPONENT_CONST_THIS
)
3640 d_print_comp (dpi
, dc
);
3642 dpi
->templates
= hold_dpt
;
3646 d_print_mod (dpi
, mods
->mod
);
3648 dpi
->templates
= hold_dpt
;
3650 d_print_mod_list (dpi
, mods
->next
, suffix
);
3653 /* Print a modifier. */
3656 d_print_mod (struct d_print_info
*dpi
,
3657 const struct demangle_component
*mod
)
3661 case DEMANGLE_COMPONENT_RESTRICT
:
3662 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
3663 d_append_string (dpi
, " restrict");
3665 case DEMANGLE_COMPONENT_VOLATILE
:
3666 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
3667 d_append_string (dpi
, " volatile");
3669 case DEMANGLE_COMPONENT_CONST
:
3670 case DEMANGLE_COMPONENT_CONST_THIS
:
3671 d_append_string (dpi
, " const");
3673 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
3674 d_append_char (dpi
, ' ');
3675 d_print_comp (dpi
, d_right (mod
));
3677 case DEMANGLE_COMPONENT_POINTER
:
3678 /* There is no pointer symbol in Java. */
3679 if ((dpi
->options
& DMGL_JAVA
) == 0)
3680 d_append_char (dpi
, '*');
3682 case DEMANGLE_COMPONENT_REFERENCE
:
3683 d_append_char (dpi
, '&');
3685 case DEMANGLE_COMPONENT_COMPLEX
:
3686 d_append_string (dpi
, "complex ");
3688 case DEMANGLE_COMPONENT_IMAGINARY
:
3689 d_append_string (dpi
, "imaginary ");
3691 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
3692 if (d_last_char (dpi
) != '(')
3693 d_append_char (dpi
, ' ');
3694 d_print_comp (dpi
, d_left (mod
));
3695 d_append_string (dpi
, "::*");
3697 case DEMANGLE_COMPONENT_TYPED_NAME
:
3698 d_print_comp (dpi
, d_left (mod
));
3701 /* Otherwise, we have something that won't go back on the
3702 modifier stack, so we can just print it. */
3703 d_print_comp (dpi
, mod
);
3708 /* Print a function type, except for the return type. */
3711 d_print_function_type (struct d_print_info
*dpi
,
3712 const struct demangle_component
*dc
,
3713 struct d_print_mod
*mods
)
3718 struct d_print_mod
*p
;
3719 struct d_print_mod
*hold_modifiers
;
3724 for (p
= mods
; p
!= NULL
; p
= p
->next
)
3730 switch (p
->mod
->type
)
3732 case DEMANGLE_COMPONENT_POINTER
:
3733 case DEMANGLE_COMPONENT_REFERENCE
:
3736 case DEMANGLE_COMPONENT_RESTRICT
:
3737 case DEMANGLE_COMPONENT_VOLATILE
:
3738 case DEMANGLE_COMPONENT_CONST
:
3739 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
3740 case DEMANGLE_COMPONENT_COMPLEX
:
3741 case DEMANGLE_COMPONENT_IMAGINARY
:
3742 case DEMANGLE_COMPONENT_PTRMEM_TYPE
:
3746 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
3747 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
3748 case DEMANGLE_COMPONENT_CONST_THIS
:
3757 if (d_left (dc
) != NULL
&& ! saw_mod
)
3764 if (d_last_char (dpi
) != '('
3765 && d_last_char (dpi
) != '*')
3768 if (need_space
&& d_last_char (dpi
) != ' ')
3769 d_append_char (dpi
, ' ');
3770 d_append_char (dpi
, '(');
3773 hold_modifiers
= dpi
->modifiers
;
3774 dpi
->modifiers
= NULL
;
3776 d_print_mod_list (dpi
, mods
, 0);
3779 d_append_char (dpi
, ')');
3781 d_append_char (dpi
, '(');
3783 if (d_right (dc
) != NULL
)
3784 d_print_comp (dpi
, d_right (dc
));
3786 d_append_char (dpi
, ')');
3788 d_print_mod_list (dpi
, mods
, 1);
3790 dpi
->modifiers
= hold_modifiers
;
3793 /* Print an array type, except for the element type. */
3796 d_print_array_type (struct d_print_info
*dpi
,
3797 const struct demangle_component
*dc
,
3798 struct d_print_mod
*mods
)
3806 struct d_print_mod
*p
;
3809 for (p
= mods
; p
!= NULL
; p
= p
->next
)
3813 if (p
->mod
->type
== DEMANGLE_COMPONENT_ARRAY_TYPE
)
3828 d_append_string (dpi
, " (");
3830 d_print_mod_list (dpi
, mods
, 0);
3833 d_append_char (dpi
, ')');
3837 d_append_char (dpi
, ' ');
3839 d_append_char (dpi
, '[');
3841 if (d_left (dc
) != NULL
)
3842 d_print_comp (dpi
, d_left (dc
));
3844 d_append_char (dpi
, ']');
3847 /* Print an operator in an expression. */
3850 d_print_expr_op (struct d_print_info
*dpi
,
3851 const struct demangle_component
*dc
)
3853 if (dc
->type
== DEMANGLE_COMPONENT_OPERATOR
)
3854 d_append_buffer (dpi
, dc
->u
.s_operator
.op
->name
,
3855 dc
->u
.s_operator
.op
->len
);
3857 d_print_comp (dpi
, dc
);
3863 d_print_cast (struct d_print_info
*dpi
,
3864 const struct demangle_component
*dc
)
3866 if (d_left (dc
)->type
!= DEMANGLE_COMPONENT_TEMPLATE
)
3867 d_print_comp (dpi
, d_left (dc
));
3870 struct d_print_mod
*hold_dpm
;
3871 struct d_print_template dpt
;
3873 /* It appears that for a templated cast operator, we need to put
3874 the template parameters in scope for the operator name, but
3875 not for the parameters. The effect is that we need to handle
3876 the template printing here. */
3878 hold_dpm
= dpi
->modifiers
;
3879 dpi
->modifiers
= NULL
;
3881 dpt
.next
= dpi
->templates
;
3882 dpi
->templates
= &dpt
;
3883 dpt
.template_decl
= d_left (dc
);
3885 d_print_comp (dpi
, d_left (d_left (dc
)));
3887 dpi
->templates
= dpt
.next
;
3889 if (d_last_char (dpi
) == '<')
3890 d_append_char (dpi
, ' ');
3891 d_append_char (dpi
, '<');
3892 d_print_comp (dpi
, d_right (d_left (dc
)));
3893 /* Avoid generating two consecutive '>' characters, to avoid
3894 the C++ syntactic ambiguity. */
3895 if (d_last_char (dpi
) == '>')
3896 d_append_char (dpi
, ' ');
3897 d_append_char (dpi
, '>');
3899 dpi
->modifiers
= hold_dpm
;
3903 /* Initialize the information structure we use to pass around
3906 CP_STATIC_IF_GLIBCPP_V3
3908 cplus_demangle_init_info (const char *mangled
, int options
, size_t len
,
3912 di
->send
= mangled
+ len
;
3913 di
->options
= options
;
3917 /* We can not need more components than twice the number of chars in
3918 the mangled string. Most components correspond directly to
3919 chars, but the ARGLIST types are exceptions. */
3920 di
->num_comps
= 2 * len
;
3923 /* Similarly, we can not need more substitutions than there are
3924 chars in the mangled string. */
3929 di
->last_name
= NULL
;
3934 /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
3935 mangled name, return strings in repeated callback giving the demangled
3936 name. OPTIONS is the usual libiberty demangler options. On success,
3937 this returns 1. On failure, returns 0. */
3940 d_demangle_callback (const char *mangled
, int options
,
3941 demangle_callbackref callback
, void *opaque
)
3945 struct demangle_component
*dc
;
3948 if (mangled
[0] == '_' && mangled
[1] == 'Z')
3950 else if (strncmp (mangled
, "_GLOBAL_", 8) == 0
3951 && (mangled
[8] == '.' || mangled
[8] == '_' || mangled
[8] == '$')
3952 && (mangled
[9] == 'D' || mangled
[9] == 'I')
3953 && mangled
[10] == '_')
3957 intro
= (mangled
[9] == 'I')
3958 ? "global constructors keyed to "
3959 : "global destructors keyed to ";
3961 callback (intro
, strlen (intro
), opaque
);
3962 callback (mangled
+ 11, strlen (mangled
+ 11), opaque
);
3967 if ((options
& DMGL_TYPES
) == 0)
3972 cplus_demangle_init_info (mangled
, options
, strlen (mangled
), &di
);
3975 #ifdef CP_DYNAMIC_ARRAYS
3976 __extension__
struct demangle_component comps
[di
.num_comps
];
3977 __extension__
struct demangle_component
*subs
[di
.num_subs
];
3982 di
.comps
= alloca (di
.num_comps
* sizeof (*di
.comps
));
3983 di
.subs
= alloca (di
.num_subs
* sizeof (*di
.subs
));
3987 dc
= cplus_demangle_type (&di
);
3989 dc
= cplus_demangle_mangled_name (&di
, 1);
3991 /* If DMGL_PARAMS is set, then if we didn't consume the entire
3992 mangled string, then we didn't successfully demangle it. If
3993 DMGL_PARAMS is not set, we didn't look at the trailing
3995 if (((options
& DMGL_PARAMS
) != 0) && d_peek_char (&di
) != '\0')
3998 #ifdef CP_DEMANGLE_DEBUG
4002 status
= (dc
!= NULL
)
4003 ? cplus_demangle_print_callback (options
, dc
, callback
, opaque
)
4010 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
4011 name, return a buffer allocated with malloc holding the demangled
4012 name. OPTIONS is the usual libiberty demangler options. On
4013 success, this sets *PALC to the allocated size of the returned
4014 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
4015 a memory allocation failure, and returns NULL. */
4018 d_demangle (const char *mangled
, int options
, size_t *palc
)
4020 struct d_growable_string dgs
;
4023 d_growable_string_init (&dgs
, 0);
4025 status
= d_demangle_callback (mangled
, options
,
4026 d_growable_string_callback_adapter
, &dgs
);
4034 *palc
= dgs
.allocation_failure
? 1 : 0;
4038 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
4040 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
4042 /* ia64 ABI-mandated entry point in the C++ runtime library for
4043 performing demangling. MANGLED_NAME is a NUL-terminated character
4044 string containing the name to be demangled.
4046 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
4047 *LENGTH bytes, into which the demangled name is stored. If
4048 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
4049 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
4050 is placed in a region of memory allocated with malloc.
4052 If LENGTH is non-NULL, the length of the buffer containing the
4053 demangled name, is placed in *LENGTH.
4055 The return value is a pointer to the start of the NUL-terminated
4056 demangled name, or NULL if the demangling fails. The caller is
4057 responsible for deallocating this memory using free.
4059 *STATUS is set to one of the following values:
4060 0: The demangling operation succeeded.
4061 -1: A memory allocation failure occurred.
4062 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
4063 -3: One of the arguments is invalid.
4065 The demangling is performed using the C++ ABI mangling rules, with
4069 __cxa_demangle (const char *mangled_name
, char *output_buffer
,
4070 size_t *length
, int *status
)
4075 if (mangled_name
== NULL
)
4082 if (output_buffer
!= NULL
&& length
== NULL
)
4089 demangled
= d_demangle (mangled_name
, DMGL_PARAMS
| DMGL_TYPES
, &alc
);
4091 if (demangled
== NULL
)
4103 if (output_buffer
== NULL
)
4110 if (strlen (demangled
) < *length
)
4112 strcpy (output_buffer
, demangled
);
4114 demangled
= output_buffer
;
4118 free (output_buffer
);
4129 extern int __gcclibcxx_demangle_callback (const char *,
4131 (const char *, size_t, void *),
4134 /* Alternative, allocationless entry point in the C++ runtime library
4135 for performing demangling. MANGLED_NAME is a NUL-terminated character
4136 string containing the name to be demangled.
4138 CALLBACK is a callback function, called with demangled string
4139 segments as demangling progresses; it is called at least once,
4140 but may be called more than once. OPAQUE is a generalized pointer
4141 used as a callback argument.
4143 The return code is one of the following values, equivalent to
4144 the STATUS values of __cxa_demangle() (excluding -1, since this
4145 function performs no memory allocations):
4146 0: The demangling operation succeeded.
4147 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
4148 -3: One of the arguments is invalid.
4150 The demangling is performed using the C++ ABI mangling rules, with
4154 __gcclibcxx_demangle_callback (const char *mangled_name
,
4155 void (*callback
) (const char *, size_t, void *),
4160 if (mangled_name
== NULL
|| callback
== NULL
)
4163 status
= d_demangle_callback (mangled_name
, DMGL_PARAMS
| DMGL_TYPES
,
4171 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
4173 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
4174 mangled name, return a buffer allocated with malloc holding the
4175 demangled name. Otherwise, return NULL. */
4178 cplus_demangle_v3 (const char *mangled
, int options
)
4182 return d_demangle (mangled
, options
, &alc
);
4186 cplus_demangle_v3_callback (const char *mangled
, int options
,
4187 demangle_callbackref callback
, void *opaque
)
4189 return d_demangle_callback (mangled
, options
, callback
, opaque
);
4192 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
4193 conventions, but the output formatting is a little different.
4194 This instructs the C++ demangler not to emit pointer characters ("*"), to
4195 use Java's namespace separator symbol ("." instead of "::"), and to output
4196 JArray<TYPE> as TYPE[]. */
4199 java_demangle_v3 (const char *mangled
)
4203 return d_demangle (mangled
, DMGL_JAVA
| DMGL_PARAMS
| DMGL_RET_POSTFIX
, &alc
);
4207 java_demangle_v3_callback (const char *mangled
,
4208 demangle_callbackref callback
, void *opaque
)
4210 return d_demangle_callback (mangled
,
4211 DMGL_JAVA
| DMGL_PARAMS
| DMGL_RET_POSTFIX
,
4215 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
4217 #ifndef IN_GLIBCPP_V3
4219 /* Demangle a string in order to find out whether it is a constructor
4220 or destructor. Return non-zero on success. Set *CTOR_KIND and
4221 *DTOR_KIND appropriately. */
4224 is_ctor_or_dtor (const char *mangled
,
4225 enum gnu_v3_ctor_kinds
*ctor_kind
,
4226 enum gnu_v3_dtor_kinds
*dtor_kind
)
4229 struct demangle_component
*dc
;
4232 *ctor_kind
= (enum gnu_v3_ctor_kinds
) 0;
4233 *dtor_kind
= (enum gnu_v3_dtor_kinds
) 0;
4235 cplus_demangle_init_info (mangled
, DMGL_GNU_V3
, strlen (mangled
), &di
);
4238 #ifdef CP_DYNAMIC_ARRAYS
4239 __extension__
struct demangle_component comps
[di
.num_comps
];
4240 __extension__
struct demangle_component
*subs
[di
.num_subs
];
4245 di
.comps
= alloca (di
.num_comps
* sizeof (*di
.comps
));
4246 di
.subs
= alloca (di
.num_subs
* sizeof (*di
.subs
));
4249 dc
= cplus_demangle_mangled_name (&di
, 1);
4251 /* Note that because we did not pass DMGL_PARAMS, we don't expect
4252 to demangle the entire string. */
4262 case DEMANGLE_COMPONENT_TYPED_NAME
:
4263 case DEMANGLE_COMPONENT_TEMPLATE
:
4264 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
4265 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
4266 case DEMANGLE_COMPONENT_CONST_THIS
:
4269 case DEMANGLE_COMPONENT_QUAL_NAME
:
4270 case DEMANGLE_COMPONENT_LOCAL_NAME
:
4273 case DEMANGLE_COMPONENT_CTOR
:
4274 *ctor_kind
= dc
->u
.s_ctor
.kind
;
4278 case DEMANGLE_COMPONENT_DTOR
:
4279 *dtor_kind
= dc
->u
.s_dtor
.kind
;
4290 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
4291 name. A non-zero return indicates the type of constructor. */
4293 enum gnu_v3_ctor_kinds
4294 is_gnu_v3_mangled_ctor (const char *name
)
4296 enum gnu_v3_ctor_kinds ctor_kind
;
4297 enum gnu_v3_dtor_kinds dtor_kind
;
4299 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
4300 return (enum gnu_v3_ctor_kinds
) 0;
4305 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
4306 name. A non-zero return indicates the type of destructor. */
4308 enum gnu_v3_dtor_kinds
4309 is_gnu_v3_mangled_dtor (const char *name
)
4311 enum gnu_v3_ctor_kinds ctor_kind
;
4312 enum gnu_v3_dtor_kinds dtor_kind
;
4314 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
4315 return (enum gnu_v3_dtor_kinds
) 0;
4319 #endif /* IN_GLIBCPP_V3 */
4321 #ifdef STANDALONE_DEMANGLER
4324 #include "dyn-string.h"
4326 static void print_usage (FILE* fp
, int exit_value
);
4328 #define IS_ALPHA(CHAR) \
4329 (((CHAR) >= 'a' && (CHAR) <= 'z') \
4330 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
4332 /* Non-zero if CHAR is a character than can occur in a mangled name. */
4333 #define is_mangled_char(CHAR) \
4334 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
4335 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
4337 /* The name of this program, as invoked. */
4338 const char* program_name
;
4340 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
4343 print_usage (FILE* fp
, int exit_value
)
4345 fprintf (fp
, "Usage: %s [options] [names ...]\n", program_name
);
4346 fprintf (fp
, "Options:\n");
4347 fprintf (fp
, " -h,--help Display this message.\n");
4348 fprintf (fp
, " -p,--no-params Don't display function parameters\n");
4349 fprintf (fp
, " -v,--verbose Produce verbose demanglings.\n");
4350 fprintf (fp
, "If names are provided, they are demangled. Otherwise filters standard input.\n");
4355 /* Option specification for getopt_long. */
4356 static const struct option long_options
[] =
4358 { "help", no_argument
, NULL
, 'h' },
4359 { "no-params", no_argument
, NULL
, 'p' },
4360 { "verbose", no_argument
, NULL
, 'v' },
4361 { NULL
, no_argument
, NULL
, 0 },
4364 /* Main entry for a demangling filter executable. It will demangle
4365 its command line arguments, if any. If none are provided, it will
4366 filter stdin to stdout, replacing any recognized mangled C++ names
4367 with their demangled equivalents. */
4370 main (int argc
, char *argv
[])
4374 int options
= DMGL_PARAMS
| DMGL_ANSI
| DMGL_TYPES
;
4376 /* Use the program name of this program, as invoked. */
4377 program_name
= argv
[0];
4379 /* Parse options. */
4382 opt_char
= getopt_long (argc
, argv
, "hpv", long_options
, NULL
);
4385 case '?': /* Unrecognized option. */
4386 print_usage (stderr
, 1);
4390 print_usage (stdout
, 0);
4394 options
&= ~ DMGL_PARAMS
;
4398 options
|= DMGL_VERBOSE
;
4402 while (opt_char
!= -1);
4405 /* No command line arguments were provided. Filter stdin. */
4407 dyn_string_t mangled
= dyn_string_new (3);
4410 /* Read all of input. */
4411 while (!feof (stdin
))
4415 /* Pile characters into mangled until we hit one that can't
4416 occur in a mangled name. */
4418 while (!feof (stdin
) && is_mangled_char (c
))
4420 dyn_string_append_char (mangled
, c
);
4426 if (dyn_string_length (mangled
) > 0)
4428 #ifdef IN_GLIBCPP_V3
4429 s
= __cxa_demangle (dyn_string_buf (mangled
), NULL
, NULL
, NULL
);
4431 s
= cplus_demangle_v3 (dyn_string_buf (mangled
), options
);
4441 /* It might not have been a mangled name. Print the
4443 fputs (dyn_string_buf (mangled
), stdout
);
4446 dyn_string_clear (mangled
);
4449 /* If we haven't hit EOF yet, we've read one character that
4450 can't occur in a mangled name, so print it out. */
4455 dyn_string_delete (mangled
);
4458 /* Demangle command line arguments. */
4460 /* Loop over command line arguments. */
4461 for (i
= optind
; i
< argc
; ++i
)
4464 #ifdef IN_GLIBCPP_V3
4468 /* Attempt to demangle. */
4469 #ifdef IN_GLIBCPP_V3
4470 s
= __cxa_demangle (argv
[i
], NULL
, NULL
, &status
);
4472 s
= cplus_demangle_v3 (argv
[i
], options
);
4475 /* If it worked, print the demangled name. */
4483 #ifdef IN_GLIBCPP_V3
4484 fprintf (stderr
, "Failed: %s (status %d)\n", argv
[i
], status
);
4486 fprintf (stderr
, "Failed: %s\n", argv
[i
]);
4495 #endif /* STANDALONE_DEMANGLER */