1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003, 2004 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@wasabisystems.com>.
5 This file is part of the libiberty library, which is part of GCC.
7 This file is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combined
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 /* This code implements a demangler for the g++ V3 ABI. The ABI is
32 described on this web page:
33 http://www.codesourcery.com/cxx-abi/abi.html#mangling
35 This code was written while looking at the demangler written by
36 Alex Samuel <samuel@codesourcery.com>.
38 This code first pulls the mangled name apart into a list of
39 components, and then walks the list generating the demangled
42 This file will normally define the following functions, q.v.:
43 char *cplus_demangle_v3(const char *mangled, int options)
44 char *java_demangle_v3(const char *mangled)
45 enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
46 enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
48 Also, the interface to the component list is public, and defined in
49 demangle.h. The interface consists of these types, which are
50 defined in demangle.h:
51 enum demangle_component_type
52 struct demangle_component
53 and these functions defined in this file:
54 cplus_demangle_fill_name
55 cplus_demangle_fill_extended_operator
56 cplus_demangle_fill_ctor
57 cplus_demangle_fill_dtor
59 and other functions defined in the file cp-demint.c.
61 This file also defines some other functions and variables which are
62 only to be used by the file cp-demint.c.
64 Preprocessor macros you can define while compiling this file:
67 If defined, this file defines the following function, q.v.:
68 char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
70 instead of cplus_demangle_v3() and java_demangle_v3().
73 If defined, this file defines only __cxa_demangle(), and no other
74 publically visible functions or variables.
77 If defined, this file defines a main() function which demangles
78 any arguments, or, if none, demangles stdin.
81 If defined, turns on debugging mode, which prints information on
82 stdout about the mangled string. This is not generally useful.
99 #include "libiberty.h"
100 #include "demangle.h"
101 #include "cp-demangle.h"
103 /* If IN_GLIBCPP_V3 is defined, some functions are made static. We
104 also rename them via #define to avoid compiler errors when the
105 static definition conflicts with the extern declaration in a header
109 #define CP_STATIC_IF_GLIBCPP_V3 static
111 #define cplus_demangle_fill_name d_fill_name
113 d_fill_name PARAMS ((struct demangle_component *, const char *, int));
115 #define cplus_demangle_fill_extended_operator d_fill_extended_operator
117 d_fill_extended_operator PARAMS ((struct demangle_component *, int,
118 struct demangle_component *));
120 #define cplus_demangle_fill_ctor d_fill_ctor
122 d_fill_ctor PARAMS ((struct demangle_component *, enum gnu_v3_ctor_kinds,
123 struct demangle_component *));
125 #define cplus_demangle_fill_dtor d_fill_dtor
127 d_fill_dtor PARAMS ((struct demangle_component *, enum gnu_v3_dtor_kinds,
128 struct demangle_component *));
130 #define cplus_demangle_mangled_name d_mangled_name
131 static struct demangle_component *
132 d_mangled_name PARAMS ((struct d_info *, int));
134 #define cplus_demangle_type d_type
135 static struct demangle_component *
136 d_type PARAMS ((struct d_info *));
138 #define cplus_demangle_print d_print
140 d_print PARAMS ((int, const struct demangle_component *, int, size_t *));
142 #define cplus_demangle_init_info d_init_info
144 d_init_info PARAMS ((const char *, int, size_t, struct d_info *));
146 #else /* ! defined(IN_GLIBCPP_V3) */
147 #define CP_STATIC_IF_GLIBCPP_V3
148 #endif /* ! defined(IN_GLIBCPP_V3) */
150 /* See if the compiler supports dynamic arrays. */
153 #define CP_DYNAMIC_ARRAYS
156 #ifdef __STDC_VERSION__
157 #if __STDC_VERSION__ >= 199901L
158 #define CP_DYNAMIC_ARRAYS
159 #endif /* __STDC__VERSION >= 199901L */
160 #endif /* defined (__STDC_VERSION__) */
161 #endif /* defined (__STDC__) */
162 #endif /* ! defined (__GNUC__) */
164 /* We avoid pulling in the ctype tables, to prevent pulling in
165 additional unresolved symbols when this code is used in a library.
166 FIXME: Is this really a valid reason? This comes from the original
169 As of this writing this file has the following undefined references
170 when compiled with -DIN_GLIBCPP_V3: malloc, realloc, free, memcpy,
171 strcpy, strcat, strlen. */
173 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
174 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
175 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
177 /* The prefix prepended by GCC to an identifier represnting the
178 anonymous namespace. */
179 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
180 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
181 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
183 /* Information we keep for the standard substitutions. */
185 struct d_standard_sub_info
187 /* The code for this substitution. */
189 /* The simple string it expands to. */
190 const char *simple_expansion;
191 /* The length of the simple expansion. */
193 /* The results of a full, verbose, expansion. This is used when
194 qualifying a constructor/destructor, or when in verbose mode. */
195 const char *full_expansion;
196 /* The length of the full expansion. */
198 /* What to set the last_name field of d_info to; NULL if we should
199 not set it. This is only relevant when qualifying a
200 constructor/destructor. */
201 const char *set_last_name;
202 /* The length of set_last_name. */
203 int set_last_name_len;
206 /* Accessors for subtrees of struct demangle_component. */
208 #define d_left(dc) ((dc)->u.s_binary.left)
209 #define d_right(dc) ((dc)->u.s_binary.right)
211 /* A list of templates. This is used while printing. */
213 struct d_print_template
215 /* Next template on the list. */
216 struct d_print_template *next;
218 const struct demangle_component *template;
221 /* A list of type modifiers. This is used while printing. */
225 /* Next modifier on the list. These are in the reverse of the order
226 in which they appeared in the mangled string. */
227 struct d_print_mod *next;
229 const struct demangle_component *mod;
230 /* Whether this modifier was printed. */
232 /* The list of templates which applies to this modifier. */
233 struct d_print_template *templates;
236 /* We use this structure to hold information during printing. */
240 /* The options passed to the demangler. */
242 /* Buffer holding the result. */
244 /* Current length of data in buffer. */
246 /* Allocated size of buffer. */
248 /* The current list of templates, if any. */
249 struct d_print_template *templates;
250 /* The current list of modifiers (e.g., pointer, reference, etc.),
252 struct d_print_mod *modifiers;
253 /* Set to 1 if we had a memory allocation failure. */
254 int allocation_failure;
257 #define d_print_saw_error(dpi) ((dpi)->buf == NULL)
259 #define d_append_char(dpi, c) \
262 if ((dpi)->buf != NULL && (dpi)->len < (dpi)->alc) \
263 (dpi)->buf[(dpi)->len++] = (c); \
265 d_print_append_char ((dpi), (c)); \
269 #define d_append_buffer(dpi, s, l) \
272 if ((dpi)->buf != NULL && (dpi)->len + (l) <= (dpi)->alc) \
274 memcpy ((dpi)->buf + (dpi)->len, (s), (l)); \
278 d_print_append_buffer ((dpi), (s), (l)); \
282 #define d_append_string_constant(dpi, s) \
283 d_append_buffer (dpi, (s), sizeof (s) - 1)
285 #define d_last_char(dpi) \
286 ((dpi)->buf == NULL || (dpi)->len == 0 ? '\0' : (dpi)->buf[(dpi)->len - 1])
288 #ifdef CP_DEMANGLE_DEBUG
290 d_dump PARAMS ((struct demangle_component *, int));
293 static struct demangle_component *
294 d_make_empty PARAMS ((struct d_info *));
296 static struct demangle_component *
297 d_make_comp PARAMS ((struct d_info *, enum demangle_component_type,
298 struct demangle_component *,
299 struct demangle_component *));
301 static struct demangle_component *
302 d_make_name PARAMS ((struct d_info *, const char *, int));
304 static struct demangle_component *
305 d_make_builtin_type PARAMS ((struct d_info *,
306 const struct demangle_builtin_type_info *));
308 static struct demangle_component *
309 d_make_operator PARAMS ((struct d_info *,
310 const struct demangle_operator_info *));
312 static struct demangle_component *
313 d_make_extended_operator PARAMS ((struct d_info *, int,
314 struct demangle_component *));
316 static struct demangle_component *
317 d_make_ctor PARAMS ((struct d_info *, enum gnu_v3_ctor_kinds,
318 struct demangle_component *));
320 static struct demangle_component *
321 d_make_dtor PARAMS ((struct d_info *, enum gnu_v3_dtor_kinds,
322 struct demangle_component *));
324 static struct demangle_component *
325 d_make_template_param PARAMS ((struct d_info *, long));
327 static struct demangle_component *
328 d_make_sub PARAMS ((struct d_info *, const char *, int));
331 has_return_type PARAMS ((struct demangle_component *));
334 is_ctor_dtor_or_conversion PARAMS ((struct demangle_component *));
336 static struct demangle_component *
337 d_encoding PARAMS ((struct d_info *, int));
339 static struct demangle_component *
340 d_name PARAMS ((struct d_info *));
342 static struct demangle_component *
343 d_nested_name PARAMS ((struct d_info *));
345 static struct demangle_component *
346 d_prefix PARAMS ((struct d_info *));
348 static struct demangle_component *
349 d_unqualified_name PARAMS ((struct d_info *));
351 static struct demangle_component *
352 d_source_name PARAMS ((struct d_info *));
355 d_number PARAMS ((struct d_info *));
357 static struct demangle_component *
358 d_identifier PARAMS ((struct d_info *, int));
360 static struct demangle_component *
361 d_operator_name PARAMS ((struct d_info *));
363 static struct demangle_component *
364 d_special_name PARAMS ((struct d_info *));
367 d_call_offset PARAMS ((struct d_info *, int));
369 static struct demangle_component *
370 d_ctor_dtor_name PARAMS ((struct d_info *));
372 static struct demangle_component **
373 d_cv_qualifiers PARAMS ((struct d_info *, struct demangle_component **, int));
375 static struct demangle_component *
376 d_function_type PARAMS ((struct d_info *));
378 static struct demangle_component *
379 d_bare_function_type PARAMS ((struct d_info *, int));
381 static struct demangle_component *
382 d_class_enum_type PARAMS ((struct d_info *));
384 static struct demangle_component *
385 d_array_type PARAMS ((struct d_info *));
387 static struct demangle_component *
388 d_pointer_to_member_type PARAMS ((struct d_info *));
390 static struct demangle_component *
391 d_template_param PARAMS ((struct d_info *));
393 static struct demangle_component *
394 d_template_args PARAMS ((struct d_info *));
396 static struct demangle_component *
397 d_template_arg PARAMS ((struct d_info *));
399 static struct demangle_component *
400 d_expression PARAMS ((struct d_info *));
402 static struct demangle_component *
403 d_expr_primary PARAMS ((struct d_info *));
405 static struct demangle_component *
406 d_local_name PARAMS ((struct d_info *));
409 d_discriminator PARAMS ((struct d_info *));
412 d_add_substitution PARAMS ((struct d_info *, struct demangle_component *));
414 static struct demangle_component *
415 d_substitution PARAMS ((struct d_info *, int));
418 d_print_resize PARAMS ((struct d_print_info *, size_t));
421 d_print_append_char PARAMS ((struct d_print_info *, int));
424 d_print_append_buffer PARAMS ((struct d_print_info *, const char *, size_t));
427 d_print_error PARAMS ((struct d_print_info *));
430 d_print_comp PARAMS ((struct d_print_info *,
431 const struct demangle_component *));
434 d_print_java_identifier PARAMS ((struct d_print_info *, const char *, int));
437 d_print_mod_list PARAMS ((struct d_print_info *, struct d_print_mod *, int));
440 d_print_mod PARAMS ((struct d_print_info *,
441 const struct demangle_component *));
444 d_print_function_type PARAMS ((struct d_print_info *,
445 const struct demangle_component *,
446 struct d_print_mod *));
449 d_print_array_type PARAMS ((struct d_print_info *,
450 const struct demangle_component *,
451 struct d_print_mod *));
454 d_print_expr_op PARAMS ((struct d_print_info *,
455 const struct demangle_component *));
458 d_print_cast PARAMS ((struct d_print_info *,
459 const struct demangle_component *));
462 d_demangle PARAMS ((const char *, int, size_t *));
464 #ifdef CP_DEMANGLE_DEBUG
468 struct demangle_component *dc;
476 for (i = 0; i < indent; ++i)
481 case DEMANGLE_COMPONENT_NAME:
482 printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
484 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
485 printf ("template parameter %ld\n", dc->u.s_number.number);
487 case DEMANGLE_COMPONENT_CTOR:
488 printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
489 d_dump (dc->u.s_ctor.name, indent + 2);
491 case DEMANGLE_COMPONENT_DTOR:
492 printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
493 d_dump (dc->u.s_dtor.name, indent + 2);
495 case DEMANGLE_COMPONENT_SUB_STD:
496 printf ("standard substitution %s\n", dc->u.s_string.string);
498 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
499 printf ("builtin type %s\n", dc->u.s_builtin.type->name);
501 case DEMANGLE_COMPONENT_OPERATOR:
502 printf ("operator %s\n", dc->u.s_operator.op->name);
504 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
505 printf ("extended operator with %d args\n",
506 dc->u.s_extended_operator.args);
507 d_dump (dc->u.s_extended_operator.name, indent + 2);
510 case DEMANGLE_COMPONENT_QUAL_NAME:
511 printf ("qualified name\n");
513 case DEMANGLE_COMPONENT_LOCAL_NAME:
514 printf ("local name\n");
516 case DEMANGLE_COMPONENT_TYPED_NAME:
517 printf ("typed name\n");
519 case DEMANGLE_COMPONENT_TEMPLATE:
520 printf ("template\n");
522 case DEMANGLE_COMPONENT_VTABLE:
525 case DEMANGLE_COMPONENT_VTT:
528 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
529 printf ("construction vtable\n");
531 case DEMANGLE_COMPONENT_TYPEINFO:
532 printf ("typeinfo\n");
534 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
535 printf ("typeinfo name\n");
537 case DEMANGLE_COMPONENT_TYPEINFO_FN:
538 printf ("typeinfo function\n");
540 case DEMANGLE_COMPONENT_THUNK:
543 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
544 printf ("virtual thunk\n");
546 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
547 printf ("covariant thunk\n");
549 case DEMANGLE_COMPONENT_JAVA_CLASS:
550 printf ("java class\n");
552 case DEMANGLE_COMPONENT_GUARD:
555 case DEMANGLE_COMPONENT_REFTEMP:
556 printf ("reference temporary\n");
558 case DEMANGLE_COMPONENT_RESTRICT:
559 printf ("restrict\n");
561 case DEMANGLE_COMPONENT_VOLATILE:
562 printf ("volatile\n");
564 case DEMANGLE_COMPONENT_CONST:
567 case DEMANGLE_COMPONENT_RESTRICT_THIS:
568 printf ("restrict this\n");
570 case DEMANGLE_COMPONENT_VOLATILE_THIS:
571 printf ("volatile this\n");
573 case DEMANGLE_COMPONENT_CONST_THIS:
574 printf ("const this\n");
576 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
577 printf ("vendor type qualifier\n");
579 case DEMANGLE_COMPONENT_POINTER:
580 printf ("pointer\n");
582 case DEMANGLE_COMPONENT_REFERENCE:
583 printf ("reference\n");
585 case DEMANGLE_COMPONENT_COMPLEX:
586 printf ("complex\n");
588 case DEMANGLE_COMPONENT_IMAGINARY:
589 printf ("imaginary\n");
591 case DEMANGLE_COMPONENT_VENDOR_TYPE:
592 printf ("vendor type\n");
594 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
595 printf ("function type\n");
597 case DEMANGLE_COMPONENT_ARRAY_TYPE:
598 printf ("array type\n");
600 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
601 printf ("pointer to member type\n");
603 case DEMANGLE_COMPONENT_ARGLIST:
604 printf ("argument list\n");
606 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
607 printf ("template argument list\n");
609 case DEMANGLE_COMPONENT_CAST:
612 case DEMANGLE_COMPONENT_UNARY:
613 printf ("unary operator\n");
615 case DEMANGLE_COMPONENT_BINARY:
616 printf ("binary operator\n");
618 case DEMANGLE_COMPONENT_BINARY_ARGS:
619 printf ("binary operator arguments\n");
621 case DEMANGLE_COMPONENT_TRINARY:
622 printf ("trinary operator\n");
624 case DEMANGLE_COMPONENT_TRINARY_ARG1:
625 printf ("trinary operator arguments 1\n");
627 case DEMANGLE_COMPONENT_TRINARY_ARG2:
628 printf ("trinary operator arguments 1\n");
630 case DEMANGLE_COMPONENT_LITERAL:
631 printf ("literal\n");
633 case DEMANGLE_COMPONENT_LITERAL_NEG:
634 printf ("negative literal\n");
638 d_dump (d_left (dc), indent + 2);
639 d_dump (d_right (dc), indent + 2);
642 #endif /* CP_DEMANGLE_DEBUG */
644 /* Fill in a DEMANGLE_COMPONENT_NAME. */
646 CP_STATIC_IF_GLIBCPP_V3
648 cplus_demangle_fill_name (p, s, len)
649 struct demangle_component *p;
653 if (p == NULL || s == NULL || len == 0)
655 p->type = DEMANGLE_COMPONENT_NAME;
657 p->u.s_name.len = len;
661 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
663 CP_STATIC_IF_GLIBCPP_V3
665 cplus_demangle_fill_extended_operator (p, args, name)
666 struct demangle_component *p;
668 struct demangle_component *name;
670 if (p == NULL || args < 0 || name == NULL)
672 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
673 p->u.s_extended_operator.args = args;
674 p->u.s_extended_operator.name = name;
678 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
680 CP_STATIC_IF_GLIBCPP_V3
682 cplus_demangle_fill_ctor (p, kind, name)
683 struct demangle_component *p;
684 enum gnu_v3_ctor_kinds kind;
685 struct demangle_component *name;
689 || (kind < gnu_v3_complete_object_ctor
690 && kind > gnu_v3_complete_object_allocating_ctor))
692 p->type = DEMANGLE_COMPONENT_CTOR;
693 p->u.s_ctor.kind = kind;
694 p->u.s_ctor.name = name;
698 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
700 CP_STATIC_IF_GLIBCPP_V3
702 cplus_demangle_fill_dtor (p, kind, name)
703 struct demangle_component *p;
704 enum gnu_v3_dtor_kinds kind;
705 struct demangle_component *name;
709 || (kind < gnu_v3_deleting_dtor
710 && kind > gnu_v3_base_object_dtor))
712 p->type = DEMANGLE_COMPONENT_DTOR;
713 p->u.s_dtor.kind = kind;
714 p->u.s_dtor.name = name;
718 /* Add a new component. */
720 static struct demangle_component *
724 struct demangle_component *p;
726 if (di->next_comp >= di->num_comps)
728 p = &di->comps[di->next_comp];
733 /* Add a new generic component. */
735 static struct demangle_component *
736 d_make_comp (di, type, left, right)
738 enum demangle_component_type type;
739 struct demangle_component *left;
740 struct demangle_component *right;
742 struct demangle_component *p;
744 /* We check for errors here. A typical error would be a NULL return
745 from a subroutine. We catch those here, and return NULL
749 /* These types require two parameters. */
750 case DEMANGLE_COMPONENT_QUAL_NAME:
751 case DEMANGLE_COMPONENT_LOCAL_NAME:
752 case DEMANGLE_COMPONENT_TYPED_NAME:
753 case DEMANGLE_COMPONENT_TEMPLATE:
754 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
755 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
756 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
757 case DEMANGLE_COMPONENT_UNARY:
758 case DEMANGLE_COMPONENT_BINARY:
759 case DEMANGLE_COMPONENT_BINARY_ARGS:
760 case DEMANGLE_COMPONENT_TRINARY:
761 case DEMANGLE_COMPONENT_TRINARY_ARG1:
762 case DEMANGLE_COMPONENT_TRINARY_ARG2:
763 case DEMANGLE_COMPONENT_LITERAL:
764 case DEMANGLE_COMPONENT_LITERAL_NEG:
765 if (left == NULL || right == NULL)
769 /* These types only require one parameter. */
770 case DEMANGLE_COMPONENT_VTABLE:
771 case DEMANGLE_COMPONENT_VTT:
772 case DEMANGLE_COMPONENT_TYPEINFO:
773 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
774 case DEMANGLE_COMPONENT_TYPEINFO_FN:
775 case DEMANGLE_COMPONENT_THUNK:
776 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
777 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
778 case DEMANGLE_COMPONENT_JAVA_CLASS:
779 case DEMANGLE_COMPONENT_GUARD:
780 case DEMANGLE_COMPONENT_REFTEMP:
781 case DEMANGLE_COMPONENT_POINTER:
782 case DEMANGLE_COMPONENT_REFERENCE:
783 case DEMANGLE_COMPONENT_COMPLEX:
784 case DEMANGLE_COMPONENT_IMAGINARY:
785 case DEMANGLE_COMPONENT_VENDOR_TYPE:
786 case DEMANGLE_COMPONENT_ARGLIST:
787 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
788 case DEMANGLE_COMPONENT_CAST:
793 /* This needs a right parameter, but the left parameter can be
795 case DEMANGLE_COMPONENT_ARRAY_TYPE:
800 /* These are allowed to have no parameters--in some cases they
801 will be filled in later. */
802 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
803 case DEMANGLE_COMPONENT_RESTRICT:
804 case DEMANGLE_COMPONENT_VOLATILE:
805 case DEMANGLE_COMPONENT_CONST:
806 case DEMANGLE_COMPONENT_RESTRICT_THIS:
807 case DEMANGLE_COMPONENT_VOLATILE_THIS:
808 case DEMANGLE_COMPONENT_CONST_THIS:
811 /* Other types should not be seen here. */
816 p = d_make_empty (di);
820 p->u.s_binary.left = left;
821 p->u.s_binary.right = right;
826 /* Add a new name component. */
828 static struct demangle_component *
829 d_make_name (di, s, len)
834 struct demangle_component *p;
836 p = d_make_empty (di);
837 if (! cplus_demangle_fill_name (p, s, len))
842 /* Add a new builtin type component. */
844 static struct demangle_component *
845 d_make_builtin_type (di, type)
847 const struct demangle_builtin_type_info *type;
849 struct demangle_component *p;
853 p = d_make_empty (di);
856 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
857 p->u.s_builtin.type = type;
862 /* Add a new operator component. */
864 static struct demangle_component *
865 d_make_operator (di, op)
867 const struct demangle_operator_info *op;
869 struct demangle_component *p;
871 p = d_make_empty (di);
874 p->type = DEMANGLE_COMPONENT_OPERATOR;
875 p->u.s_operator.op = op;
880 /* Add a new extended operator component. */
882 static struct demangle_component *
883 d_make_extended_operator (di, args, name)
886 struct demangle_component *name;
888 struct demangle_component *p;
890 p = d_make_empty (di);
891 if (! cplus_demangle_fill_extended_operator (p, args, name))
896 /* Add a new constructor component. */
898 static struct demangle_component *
899 d_make_ctor (di, kind, name)
901 enum gnu_v3_ctor_kinds kind;
902 struct demangle_component *name;
904 struct demangle_component *p;
906 p = d_make_empty (di);
907 if (! cplus_demangle_fill_ctor (p, kind, name))
912 /* Add a new destructor component. */
914 static struct demangle_component *
915 d_make_dtor (di, kind, name)
917 enum gnu_v3_dtor_kinds kind;
918 struct demangle_component *name;
920 struct demangle_component *p;
922 p = d_make_empty (di);
923 if (! cplus_demangle_fill_dtor (p, kind, name))
928 /* Add a new template parameter. */
930 static struct demangle_component *
931 d_make_template_param (di, i)
935 struct demangle_component *p;
937 p = d_make_empty (di);
940 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
941 p->u.s_number.number = i;
946 /* Add a new standard substitution component. */
948 static struct demangle_component *
949 d_make_sub (di, name, len)
954 struct demangle_component *p;
956 p = d_make_empty (di);
959 p->type = DEMANGLE_COMPONENT_SUB_STD;
960 p->u.s_string.string = name;
961 p->u.s_string.len = len;
966 /* <mangled-name> ::= _Z <encoding>
968 TOP_LEVEL is non-zero when called at the top level. */
970 CP_STATIC_IF_GLIBCPP_V3
971 struct demangle_component *
972 cplus_demangle_mangled_name (di, top_level)
976 if (d_next_char (di) != '_')
978 if (d_next_char (di) != 'Z')
980 return d_encoding (di, top_level);
983 /* Return whether a function should have a return type. The argument
984 is the function name, which may be qualified in various ways. The
985 rules are that template functions have return types with some
986 exceptions, function types which are not part of a function name
987 mangling have return types with some exceptions, and non-template
988 function names do not have return types. The exceptions are that
989 constructors, destructors, and conversion operators do not have
994 struct demangle_component *dc;
1002 case DEMANGLE_COMPONENT_TEMPLATE:
1003 return ! is_ctor_dtor_or_conversion (d_left (dc));
1004 case DEMANGLE_COMPONENT_RESTRICT_THIS:
1005 case DEMANGLE_COMPONENT_VOLATILE_THIS:
1006 case DEMANGLE_COMPONENT_CONST_THIS:
1007 return has_return_type (d_left (dc));
1011 /* Return whether a name is a constructor, a destructor, or a
1012 conversion operator. */
1015 is_ctor_dtor_or_conversion (dc)
1016 struct demangle_component *dc;
1024 case DEMANGLE_COMPONENT_QUAL_NAME:
1025 case DEMANGLE_COMPONENT_LOCAL_NAME:
1026 return is_ctor_dtor_or_conversion (d_right (dc));
1027 case DEMANGLE_COMPONENT_CTOR:
1028 case DEMANGLE_COMPONENT_DTOR:
1029 case DEMANGLE_COMPONENT_CAST:
1034 /* <encoding> ::= <(function) name> <bare-function-type>
1038 TOP_LEVEL is non-zero when called at the top level, in which case
1039 if DMGL_PARAMS is not set we do not demangle the function
1040 parameters. We only set this at the top level, because otherwise
1041 we would not correctly demangle names in local scopes. */
1043 static struct demangle_component *
1044 d_encoding (di, top_level)
1048 char peek = d_peek_char (di);
1050 if (peek == 'G' || peek == 'T')
1051 return d_special_name (di);
1054 struct demangle_component *dc;
1058 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
1060 /* Strip off any initial CV-qualifiers, as they really apply
1061 to the `this' parameter, and they were not output by the
1062 v2 demangler without DMGL_PARAMS. */
1063 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1064 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1065 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
1068 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1069 there may be CV-qualifiers on its right argument which
1070 really apply here; this happens when parsing a class
1071 which is local to a function. */
1072 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1074 struct demangle_component *dcr;
1077 while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1078 || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1079 || dcr->type == DEMANGLE_COMPONENT_CONST_THIS)
1081 dc->u.s_binary.right = dcr;
1087 peek = d_peek_char (di);
1088 if (peek == '\0' || peek == 'E')
1090 return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
1091 d_bare_function_type (di, has_return_type (dc)));
1095 /* <name> ::= <nested-name>
1097 ::= <unscoped-template-name> <template-args>
1100 <unscoped-name> ::= <unqualified-name>
1101 ::= St <unqualified-name>
1103 <unscoped-template-name> ::= <unscoped-name>
1107 static struct demangle_component *
1111 char peek = d_peek_char (di);
1112 struct demangle_component *dc;
1117 return d_nested_name (di);
1120 return d_local_name (di);
1126 if (d_peek_next_char (di) != 't')
1128 dc = d_substitution (di, 0);
1134 dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
1135 d_make_name (di, "std", 3),
1136 d_unqualified_name (di));
1141 if (d_peek_char (di) != 'I')
1143 /* The grammar does not permit this case to occur if we
1144 called d_substitution() above (i.e., subst == 1). We
1145 don't bother to check. */
1149 /* This is <template-args>, which means that we just saw
1150 <unscoped-template-name>, which is a substitution
1151 candidate if we didn't just get it from a
1155 if (! d_add_substitution (di, dc))
1158 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1159 d_template_args (di));
1166 dc = d_unqualified_name (di);
1167 if (d_peek_char (di) == 'I')
1169 /* This is <template-args>, which means that we just saw
1170 <unscoped-template-name>, which is a substitution
1172 if (! d_add_substitution (di, dc))
1174 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1175 d_template_args (di));
1181 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1182 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1185 static struct demangle_component *
1189 struct demangle_component *ret;
1190 struct demangle_component **pret;
1192 if (d_next_char (di) != 'N')
1195 pret = d_cv_qualifiers (di, &ret, 1);
1199 *pret = d_prefix (di);
1203 if (d_next_char (di) != 'E')
1209 /* <prefix> ::= <prefix> <unqualified-name>
1210 ::= <template-prefix> <template-args>
1211 ::= <template-param>
1215 <template-prefix> ::= <prefix> <(template) unqualified-name>
1216 ::= <template-param>
1220 static struct demangle_component *
1224 struct demangle_component *ret = NULL;
1229 enum demangle_component_type comb_type;
1230 struct demangle_component *dc;
1232 peek = d_peek_char (di);
1236 /* The older code accepts a <local-name> here, but I don't see
1237 that in the grammar. The older code does not accept a
1238 <template-param> here. */
1240 comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
1245 dc = d_unqualified_name (di);
1246 else if (peek == 'S')
1247 dc = d_substitution (di, 1);
1248 else if (peek == 'I')
1252 comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1253 dc = d_template_args (di);
1255 else if (peek == 'T')
1256 dc = d_template_param (di);
1257 else if (peek == 'E')
1265 ret = d_make_comp (di, comb_type, ret, dc);
1267 if (peek != 'S' && d_peek_char (di) != 'E')
1269 if (! d_add_substitution (di, ret))
1275 /* <unqualified-name> ::= <operator-name>
1276 ::= <ctor-dtor-name>
1280 static struct demangle_component *
1281 d_unqualified_name (di)
1286 peek = d_peek_char (di);
1287 if (IS_DIGIT (peek))
1288 return d_source_name (di);
1289 else if (IS_LOWER (peek))
1291 struct demangle_component *ret;
1293 ret = d_operator_name (di);
1294 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1295 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1298 else if (peek == 'C' || peek == 'D')
1299 return d_ctor_dtor_name (di);
1304 /* <source-name> ::= <(positive length) number> <identifier> */
1306 static struct demangle_component *
1311 struct demangle_component *ret;
1313 len = d_number (di);
1316 ret = d_identifier (di, len);
1317 di->last_name = ret;
1321 /* number ::= [n] <(non-negative decimal integer)> */
1332 peek = d_peek_char (di);
1337 peek = d_peek_char (di);
1343 if (! IS_DIGIT (peek))
1349 ret = ret * 10 + peek - '0';
1351 peek = d_peek_char (di);
1355 /* identifier ::= <(unqualified source code identifier)> */
1357 static struct demangle_component *
1358 d_identifier (di, len)
1366 if (di->send - name < len)
1369 d_advance (di, len);
1371 /* A Java mangled name may have a trailing '$' if it is a C++
1372 keyword. This '$' is not included in the length count. We just
1374 if ((di->options & DMGL_JAVA) != 0
1375 && d_peek_char (di) == '$')
1378 /* Look for something which looks like a gcc encoding of an
1379 anonymous namespace, and replace it with a more user friendly
1381 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1382 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1383 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1387 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1388 if ((*s == '.' || *s == '_' || *s == '$')
1391 di->expansion -= len - sizeof "(anonymous namespace)";
1392 return d_make_name (di, "(anonymous namespace)",
1393 sizeof "(anonymous namespace)" - 1);
1397 return d_make_name (di, name, len);
1400 /* operator_name ::= many different two character encodings.
1402 ::= v <digit> <source-name>
1405 #define NL(s) s, (sizeof s) - 1
1407 CP_STATIC_IF_GLIBCPP_V3
1408 const struct demangle_operator_info cplus_demangle_operators[] =
1410 { "aN", NL ("&="), 2 },
1411 { "aS", NL ("="), 2 },
1412 { "aa", NL ("&&"), 2 },
1413 { "ad", NL ("&"), 1 },
1414 { "an", NL ("&"), 2 },
1415 { "cl", NL ("()"), 0 },
1416 { "cm", NL (","), 2 },
1417 { "co", NL ("~"), 1 },
1418 { "dV", NL ("/="), 2 },
1419 { "da", NL ("delete[]"), 1 },
1420 { "de", NL ("*"), 1 },
1421 { "dl", NL ("delete"), 1 },
1422 { "dv", NL ("/"), 2 },
1423 { "eO", NL ("^="), 2 },
1424 { "eo", NL ("^"), 2 },
1425 { "eq", NL ("=="), 2 },
1426 { "ge", NL (">="), 2 },
1427 { "gt", NL (">"), 2 },
1428 { "ix", NL ("[]"), 2 },
1429 { "lS", NL ("<<="), 2 },
1430 { "le", NL ("<="), 2 },
1431 { "ls", NL ("<<"), 2 },
1432 { "lt", NL ("<"), 2 },
1433 { "mI", NL ("-="), 2 },
1434 { "mL", NL ("*="), 2 },
1435 { "mi", NL ("-"), 2 },
1436 { "ml", NL ("*"), 2 },
1437 { "mm", NL ("--"), 1 },
1438 { "na", NL ("new[]"), 1 },
1439 { "ne", NL ("!="), 2 },
1440 { "ng", NL ("-"), 1 },
1441 { "nt", NL ("!"), 1 },
1442 { "nw", NL ("new"), 1 },
1443 { "oR", NL ("|="), 2 },
1444 { "oo", NL ("||"), 2 },
1445 { "or", NL ("|"), 2 },
1446 { "pL", NL ("+="), 2 },
1447 { "pl", NL ("+"), 2 },
1448 { "pm", NL ("->*"), 2 },
1449 { "pp", NL ("++"), 1 },
1450 { "ps", NL ("+"), 1 },
1451 { "pt", NL ("->"), 2 },
1452 { "qu", NL ("?"), 3 },
1453 { "rM", NL ("%="), 2 },
1454 { "rS", NL (">>="), 2 },
1455 { "rm", NL ("%"), 2 },
1456 { "rs", NL (">>"), 2 },
1457 { "st", NL ("sizeof "), 1 },
1458 { "sz", NL ("sizeof "), 1 },
1459 { NULL, NULL, 0, 0 }
1462 static struct demangle_component *
1463 d_operator_name (di)
1469 c1 = d_next_char (di);
1470 c2 = d_next_char (di);
1471 if (c1 == 'v' && IS_DIGIT (c2))
1472 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1473 else if (c1 == 'c' && c2 == 'v')
1474 return d_make_comp (di, DEMANGLE_COMPONENT_CAST,
1475 cplus_demangle_type (di), NULL);
1478 /* LOW is the inclusive lower bound. */
1480 /* HIGH is the exclusive upper bound. We subtract one to ignore
1481 the sentinel at the end of the array. */
1482 int high = ((sizeof (cplus_demangle_operators)
1483 / sizeof (cplus_demangle_operators[0]))
1489 const struct demangle_operator_info *p;
1491 i = low + (high - low) / 2;
1492 p = cplus_demangle_operators + i;
1494 if (c1 == p->code[0] && c2 == p->code[1])
1495 return d_make_operator (di, p);
1497 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1507 /* <special-name> ::= TV <type>
1511 ::= GV <(object) name>
1512 ::= T <call-offset> <(base) encoding>
1513 ::= Tc <call-offset> <call-offset> <(base) encoding>
1514 Also g++ extensions:
1515 ::= TC <type> <(offset) number> _ <(base) type>
1521 static struct demangle_component *
1527 di->expansion += 20;
1528 c = d_next_char (di);
1531 switch (d_next_char (di))
1535 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
1536 cplus_demangle_type (di), NULL);
1538 di->expansion -= 10;
1539 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
1540 cplus_demangle_type (di), NULL);
1542 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
1543 cplus_demangle_type (di), NULL);
1545 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
1546 cplus_demangle_type (di), NULL);
1549 if (! d_call_offset (di, 'h'))
1551 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
1552 d_encoding (di, 0), NULL);
1555 if (! d_call_offset (di, 'v'))
1557 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
1558 d_encoding (di, 0), NULL);
1561 if (! d_call_offset (di, '\0'))
1563 if (! d_call_offset (di, '\0'))
1565 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
1566 d_encoding (di, 0), NULL);
1570 struct demangle_component *derived_type;
1572 struct demangle_component *base_type;
1574 derived_type = cplus_demangle_type (di);
1575 offset = d_number (di);
1578 if (d_next_char (di) != '_')
1580 base_type = cplus_demangle_type (di);
1581 /* We don't display the offset. FIXME: We should display
1582 it in verbose mode. */
1584 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
1585 base_type, derived_type);
1589 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
1590 cplus_demangle_type (di), NULL);
1592 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
1593 cplus_demangle_type (di), NULL);
1601 switch (d_next_char (di))
1604 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
1607 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, d_name (di),
1618 /* <call-offset> ::= h <nv-offset> _
1621 <nv-offset> ::= <(offset) number>
1623 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1625 The C parameter, if not '\0', is a character we just read which is
1626 the start of the <call-offset>.
1628 We don't display the offset information anywhere. FIXME: We should
1629 display it in verbose mode. */
1632 d_call_offset (di, c)
1637 long virtual_offset;
1640 c = d_next_char (di);
1643 offset = d_number (di);
1646 offset = d_number (di);
1647 if (d_next_char (di) != '_')
1649 virtual_offset = d_number (di);
1654 if (d_next_char (di) != '_')
1660 /* <ctor-dtor-name> ::= C1
1668 static struct demangle_component *
1669 d_ctor_dtor_name (di)
1672 if (di->last_name != NULL)
1674 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
1675 di->expansion += di->last_name->u.s_name.len;
1676 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
1677 di->expansion += di->last_name->u.s_string.len;
1679 switch (d_next_char (di))
1683 enum gnu_v3_ctor_kinds kind;
1685 switch (d_next_char (di))
1688 kind = gnu_v3_complete_object_ctor;
1691 kind = gnu_v3_base_object_ctor;
1694 kind = gnu_v3_complete_object_allocating_ctor;
1699 return d_make_ctor (di, kind, di->last_name);
1704 enum gnu_v3_dtor_kinds kind;
1706 switch (d_next_char (di))
1709 kind = gnu_v3_deleting_dtor;
1712 kind = gnu_v3_complete_object_dtor;
1715 kind = gnu_v3_base_object_dtor;
1720 return d_make_dtor (di, kind, di->last_name);
1728 /* <type> ::= <builtin-type>
1730 ::= <class-enum-type>
1732 ::= <pointer-to-member-type>
1733 ::= <template-param>
1734 ::= <template-template-param> <template-args>
1736 ::= <CV-qualifiers> <type>
1741 ::= U <source-name> <type>
1743 <builtin-type> ::= various one letter codes
1747 CP_STATIC_IF_GLIBCPP_V3
1748 const struct demangle_builtin_type_info
1749 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
1751 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_INT },
1752 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
1753 /* c */ { NL ("char"), NL ("byte"), D_PRINT_INT },
1754 /* d */ { NL ("double"), NL ("double"), D_PRINT_DEFAULT },
1755 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_DEFAULT },
1756 /* f */ { NL ("float"), NL ("float"), D_PRINT_DEFAULT },
1757 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_DEFAULT },
1758 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_INT },
1759 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
1760 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_INT },
1761 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1762 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
1763 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_LONG },
1764 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
1765 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"), D_PRINT_DEFAULT },
1766 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1767 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1768 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1769 /* s */ { NL ("short"), NL ("short"), D_PRINT_INT },
1770 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_INT },
1771 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1772 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
1773 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_INT },
1774 /* x */ { NL ("long long"), NL ("long"), D_PRINT_DEFAULT },
1775 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"), D_PRINT_DEFAULT },
1776 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
1779 CP_STATIC_IF_GLIBCPP_V3
1780 struct demangle_component *
1781 cplus_demangle_type (di)
1785 struct demangle_component *ret;
1788 /* The ABI specifies that when CV-qualifiers are used, the base type
1789 is substitutable, and the fully qualified type is substitutable,
1790 but the base type with a strict subset of the CV-qualifiers is
1791 not substitutable. The natural recursive implementation of the
1792 CV-qualifiers would cause subsets to be substitutable, so instead
1793 we pull them all off now.
1795 FIXME: The ABI says that order-insensitive vendor qualifiers
1796 should be handled in the same way, but we have no way to tell
1797 which vendor qualifiers are order-insensitive and which are
1798 order-sensitive. So we just assume that they are all
1799 order-sensitive. g++ 3.4 supports only one vendor qualifier,
1800 __vector, and it treats it as order-sensitive when mangling
1803 peek = d_peek_char (di);
1804 if (peek == 'r' || peek == 'V' || peek == 'K')
1806 struct demangle_component **pret;
1808 pret = d_cv_qualifiers (di, &ret, 0);
1811 *pret = cplus_demangle_type (di);
1812 if (! d_add_substitution (di, ret))
1821 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1822 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
1823 case 'o': case 's': case 't':
1824 case 'v': case 'w': case 'x': case 'y': case 'z':
1825 ret = d_make_builtin_type (di,
1826 &cplus_demangle_builtin_types[peek - 'a']);
1827 di->expansion += ret->u.s_builtin.type->len;
1834 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
1835 d_source_name (di), NULL);
1839 ret = d_function_type (di);
1842 case '0': case '1': case '2': case '3': case '4':
1843 case '5': case '6': case '7': case '8': case '9':
1846 ret = d_class_enum_type (di);
1850 ret = d_array_type (di);
1854 ret = d_pointer_to_member_type (di);
1858 ret = d_template_param (di);
1859 if (d_peek_char (di) == 'I')
1861 /* This is <template-template-param> <template-args>. The
1862 <template-template-param> part is a substitution
1864 if (! d_add_substitution (di, ret))
1866 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
1867 d_template_args (di));
1872 /* If this is a special substitution, then it is the start of
1873 <class-enum-type>. */
1877 peek_next = d_peek_next_char (di);
1878 if (IS_DIGIT (peek_next)
1880 || IS_UPPER (peek_next))
1882 ret = d_substitution (di, 0);
1883 /* The substituted name may have been a template name and
1884 may be followed by tepmlate args. */
1885 if (d_peek_char (di) == 'I')
1886 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
1887 d_template_args (di));
1893 ret = d_class_enum_type (di);
1894 /* If the substitution was a complete type, then it is not
1895 a new substitution candidate. However, if the
1896 substitution was followed by template arguments, then
1897 the whole thing is a substitution candidate. */
1898 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
1906 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
1907 cplus_demangle_type (di), NULL);
1912 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
1913 cplus_demangle_type (di), NULL);
1918 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
1919 cplus_demangle_type (di), NULL);
1924 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
1925 cplus_demangle_type (di), NULL);
1930 ret = d_source_name (di);
1931 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
1932 cplus_demangle_type (di), ret);
1941 if (! d_add_substitution (di, ret))
1948 /* <CV-qualifiers> ::= [r] [V] [K] */
1950 static struct demangle_component **
1951 d_cv_qualifiers (di, pret, member_fn)
1953 struct demangle_component **pret;
1958 peek = d_peek_char (di);
1959 while (peek == 'r' || peek == 'V' || peek == 'K')
1961 enum demangle_component_type t;
1967 ? DEMANGLE_COMPONENT_RESTRICT_THIS
1968 : DEMANGLE_COMPONENT_RESTRICT);
1969 di->expansion += sizeof "restrict";
1971 else if (peek == 'V')
1974 ? DEMANGLE_COMPONENT_VOLATILE_THIS
1975 : DEMANGLE_COMPONENT_VOLATILE);
1976 di->expansion += sizeof "volatile";
1981 ? DEMANGLE_COMPONENT_CONST_THIS
1982 : DEMANGLE_COMPONENT_CONST);
1983 di->expansion += sizeof "const";
1986 *pret = d_make_comp (di, t, NULL, NULL);
1989 pret = &d_left (*pret);
1991 peek = d_peek_char (di);
1997 /* <function-type> ::= F [Y] <bare-function-type> E */
1999 static struct demangle_component *
2000 d_function_type (di)
2003 struct demangle_component *ret;
2005 if (d_next_char (di) != 'F')
2007 if (d_peek_char (di) == 'Y')
2009 /* Function has C linkage. We don't print this information.
2010 FIXME: We should print it in verbose mode. */
2013 ret = d_bare_function_type (di, 1);
2014 if (d_next_char (di) != 'E')
2019 /* <bare-function-type> ::= <type>+ */
2021 static struct demangle_component *
2022 d_bare_function_type (di, has_return_type)
2024 int has_return_type;
2026 struct demangle_component *return_type;
2027 struct demangle_component *tl;
2028 struct demangle_component **ptl;
2036 struct demangle_component *type;
2038 peek = d_peek_char (di);
2039 if (peek == '\0' || peek == 'E')
2041 type = cplus_demangle_type (di);
2044 if (has_return_type)
2047 has_return_type = 0;
2051 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
2054 ptl = &d_right (*ptl);
2058 /* There should be at least one parameter type besides the optional
2059 return type. A function which takes no arguments will have a
2060 single parameter type void. */
2064 /* If we have a single parameter type void, omit it. */
2065 if (d_right (tl) == NULL
2066 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2067 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
2069 di->expansion -= d_left (tl)->u.s_builtin.type->len;
2073 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE, return_type, tl);
2076 /* <class-enum-type> ::= <name> */
2078 static struct demangle_component *
2079 d_class_enum_type (di)
2085 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2086 ::= A [<(dimension) expression>] _ <(element) type>
2089 static struct demangle_component *
2094 struct demangle_component *dim;
2096 if (d_next_char (di) != 'A')
2099 peek = d_peek_char (di);
2102 else if (IS_DIGIT (peek))
2110 peek = d_peek_char (di);
2112 while (IS_DIGIT (peek));
2113 dim = d_make_name (di, s, d_str (di) - s);
2119 dim = d_expression (di);
2124 if (d_next_char (di) != '_')
2127 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2128 cplus_demangle_type (di));
2131 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2133 static struct demangle_component *
2134 d_pointer_to_member_type (di)
2137 struct demangle_component *cl;
2138 struct demangle_component *mem;
2139 struct demangle_component **pmem;
2141 if (d_next_char (di) != 'M')
2144 cl = cplus_demangle_type (di);
2146 /* The ABI specifies that any type can be a substitution source, and
2147 that M is followed by two types, and that when a CV-qualified
2148 type is seen both the base type and the CV-qualified types are
2149 substitution sources. The ABI also specifies that for a pointer
2150 to a CV-qualified member function, the qualifiers are attached to
2151 the second type. Given the grammar, a plain reading of the ABI
2152 suggests that both the CV-qualified member function and the
2153 non-qualified member function are substitution sources. However,
2154 g++ does not work that way. g++ treats only the CV-qualified
2155 member function as a substitution source. FIXME. So to work
2156 with g++, we need to pull off the CV-qualifiers here, in order to
2157 avoid calling add_substitution() in cplus_demangle_type(). */
2159 pmem = d_cv_qualifiers (di, &mem, 1);
2162 *pmem = cplus_demangle_type (di);
2164 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
2167 /* <template-param> ::= T_
2168 ::= T <(parameter-2 non-negative) number> _
2171 static struct demangle_component *
2172 d_template_param (di)
2177 if (d_next_char (di) != 'T')
2180 if (d_peek_char (di) == '_')
2184 param = d_number (di);
2190 if (d_next_char (di) != '_')
2195 return d_make_template_param (di, param);
2198 /* <template-args> ::= I <template-arg>+ E */
2200 static struct demangle_component *
2201 d_template_args (di)
2204 struct demangle_component *hold_last_name;
2205 struct demangle_component *al;
2206 struct demangle_component **pal;
2208 /* Preserve the last name we saw--don't let the template arguments
2209 clobber it, as that would give us the wrong name for a subsequent
2210 constructor or destructor. */
2211 hold_last_name = di->last_name;
2213 if (d_next_char (di) != 'I')
2220 struct demangle_component *a;
2222 a = d_template_arg (di);
2226 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
2229 pal = &d_right (*pal);
2231 if (d_peek_char (di) == 'E')
2238 di->last_name = hold_last_name;
2243 /* <template-arg> ::= <type>
2244 ::= X <expression> E
2248 static struct demangle_component *
2252 struct demangle_component *ret;
2254 switch (d_peek_char (di))
2258 ret = d_expression (di);
2259 if (d_next_char (di) != 'E')
2264 return d_expr_primary (di);
2267 return cplus_demangle_type (di);
2271 /* <expression> ::= <(unary) operator-name> <expression>
2272 ::= <(binary) operator-name> <expression> <expression>
2273 ::= <(trinary) operator-name> <expression> <expression> <expression>
2275 ::= <template-param>
2276 ::= sr <type> <unqualified-name>
2277 ::= sr <type> <unqualified-name> <template-args>
2281 static struct demangle_component *
2287 peek = d_peek_char (di);
2289 return d_expr_primary (di);
2290 else if (peek == 'T')
2291 return d_template_param (di);
2292 else if (peek == 's' && d_peek_next_char (di) == 'r')
2294 struct demangle_component *type;
2295 struct demangle_component *name;
2298 type = cplus_demangle_type (di);
2299 name = d_unqualified_name (di);
2300 if (d_peek_char (di) != 'I')
2301 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
2303 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
2304 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2305 d_template_args (di)));
2309 struct demangle_component *op;
2312 op = d_operator_name (di);
2316 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
2317 di->expansion += op->u.s_operator.op->len - 2;
2319 if (op->type == DEMANGLE_COMPONENT_OPERATOR
2320 && strcmp (op->u.s_operator.op->code, "st") == 0)
2321 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2322 cplus_demangle_type (di));
2328 case DEMANGLE_COMPONENT_OPERATOR:
2329 args = op->u.s_operator.op->args;
2331 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
2332 args = op->u.s_extended_operator.args;
2334 case DEMANGLE_COMPONENT_CAST:
2342 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2346 struct demangle_component *left;
2348 left = d_expression (di);
2349 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
2351 DEMANGLE_COMPONENT_BINARY_ARGS,
2353 d_expression (di)));
2357 struct demangle_component *first;
2358 struct demangle_component *second;
2360 first = d_expression (di);
2361 second = d_expression (di);
2362 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
2364 DEMANGLE_COMPONENT_TRINARY_ARG1,
2367 DEMANGLE_COMPONENT_TRINARY_ARG2,
2369 d_expression (di))));
2377 /* <expr-primary> ::= L <type> <(value) number> E
2378 ::= L <type> <(value) float> E
2379 ::= L <mangled-name> E
2382 static struct demangle_component *
2386 struct demangle_component *ret;
2388 if (d_next_char (di) != 'L')
2390 if (d_peek_char (di) == '_')
2391 ret = cplus_demangle_mangled_name (di, 0);
2394 struct demangle_component *type;
2395 enum demangle_component_type t;
2398 type = cplus_demangle_type (di);
2402 /* If we have a type we know how to print, we aren't going to
2403 print the type name itself. */
2404 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2405 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
2406 di->expansion -= type->u.s_builtin.type->len;
2408 /* Rather than try to interpret the literal value, we just
2409 collect it as a string. Note that it's possible to have a
2410 floating point literal here. The ABI specifies that the
2411 format of such literals is machine independent. That's fine,
2412 but what's not fine is that versions of g++ up to 3.2 with
2413 -fabi-version=1 used upper case letters in the hex constant,
2414 and dumped out gcc's internal representation. That makes it
2415 hard to tell where the constant ends, and hard to dump the
2416 constant in any readable form anyhow. We don't attempt to
2417 handle these cases. */
2419 t = DEMANGLE_COMPONENT_LITERAL;
2420 if (d_peek_char (di) == 'n')
2422 t = DEMANGLE_COMPONENT_LITERAL_NEG;
2426 while (d_peek_char (di) != 'E')
2428 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
2430 if (d_next_char (di) != 'E')
2435 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2436 ::= Z <(function) encoding> E s [<discriminator>]
2439 static struct demangle_component *
2443 struct demangle_component *function;
2445 if (d_next_char (di) != 'Z')
2448 function = d_encoding (di, 0);
2450 if (d_next_char (di) != 'E')
2453 if (d_peek_char (di) == 's')
2456 if (! d_discriminator (di))
2458 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
2459 d_make_name (di, "string literal",
2460 sizeof "string literal" - 1));
2464 struct demangle_component *name;
2467 if (! d_discriminator (di))
2469 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
2473 /* <discriminator> ::= _ <(non-negative) number>
2475 We demangle the discriminator, but we don't print it out. FIXME:
2476 We should print it out in verbose mode. */
2479 d_discriminator (di)
2484 if (d_peek_char (di) != '_')
2487 discrim = d_number (di);
2493 /* Add a new substitution. */
2496 d_add_substitution (di, dc)
2498 struct demangle_component *dc;
2502 if (di->next_sub >= di->num_subs)
2504 di->subs[di->next_sub] = dc;
2509 /* <substitution> ::= S <seq-id> _
2519 If PREFIX is non-zero, then this type is being used as a prefix in
2520 a qualified name. In this case, for the standard substitutions, we
2521 need to check whether we are being used as a prefix for a
2522 constructor or destructor, and return a full template name.
2523 Otherwise we will get something like std::iostream::~iostream()
2524 which does not correspond particularly well to any function which
2525 actually appears in the source.
2528 static const struct d_standard_sub_info standard_subs[] =
2533 { 'a', NL ("std::allocator"),
2534 NL ("std::allocator"),
2536 { 'b', NL ("std::basic_string"),
2537 NL ("std::basic_string"),
2538 NL ("basic_string") },
2539 { 's', NL ("std::string"),
2540 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
2541 NL ("basic_string") },
2542 { 'i', NL ("std::istream"),
2543 NL ("std::basic_istream<char, std::char_traits<char> >"),
2544 NL ("basic_istream") },
2545 { 'o', NL ("std::ostream"),
2546 NL ("std::basic_ostream<char, std::char_traits<char> >"),
2547 NL ("basic_ostream") },
2548 { 'd', NL ("std::iostream"),
2549 NL ("std::basic_iostream<char, std::char_traits<char> >"),
2550 NL ("basic_iostream") }
2553 static struct demangle_component *
2554 d_substitution (di, prefix)
2560 if (d_next_char (di) != 'S')
2563 c = d_next_char (di);
2564 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
2574 id = id * 36 + c - '0';
2575 else if (IS_UPPER (c))
2576 id = id * 36 + c - 'A' + 10;
2579 c = d_next_char (di);
2586 if (id >= di->next_sub)
2591 return di->subs[id];
2596 const struct d_standard_sub_info *p;
2597 const struct d_standard_sub_info *pend;
2599 verbose = (di->options & DMGL_VERBOSE) != 0;
2600 if (! verbose && prefix)
2604 peek = d_peek_char (di);
2605 if (peek == 'C' || peek == 'D')
2609 pend = (&standard_subs[0]
2610 + sizeof standard_subs / sizeof standard_subs[0]);
2611 for (p = &standard_subs[0]; p < pend; ++p)
2618 if (p->set_last_name != NULL)
2619 di->last_name = d_make_sub (di, p->set_last_name,
2620 p->set_last_name_len);
2623 s = p->full_expansion;
2628 s = p->simple_expansion;
2629 len = p->simple_len;
2631 di->expansion += len;
2632 return d_make_sub (di, s, len);
2640 /* Resize the print buffer. */
2643 d_print_resize (dpi, add)
2644 struct d_print_info *dpi;
2649 if (dpi->buf == NULL)
2651 need = dpi->len + add;
2652 while (need > dpi->alc)
2657 newalc = dpi->alc * 2;
2658 newbuf = realloc (dpi->buf, newalc);
2663 dpi->allocation_failure = 1;
2671 /* Append a character to the print buffer. */
2674 d_print_append_char (dpi, c)
2675 struct d_print_info *dpi;
2678 if (dpi->buf != NULL)
2680 if (dpi->len >= dpi->alc)
2682 d_print_resize (dpi, 1);
2683 if (dpi->buf == NULL)
2687 dpi->buf[dpi->len] = c;
2692 /* Append a buffer to the print buffer. */
2695 d_print_append_buffer (dpi, s, l)
2696 struct d_print_info *dpi;
2700 if (dpi->buf != NULL)
2702 if (dpi->len + l > dpi->alc)
2704 d_print_resize (dpi, l);
2705 if (dpi->buf == NULL)
2709 memcpy (dpi->buf + dpi->len, s, l);
2714 /* Indicate that an error occurred during printing. */
2718 struct d_print_info *dpi;
2724 /* Turn components into a human readable string. OPTIONS is the
2725 options bits passed to the demangler. DC is the tree to print.
2726 ESTIMATE is a guess at the length of the result. This returns a
2727 string allocated by malloc, or NULL on error. On success, this
2728 sets *PALC to the size of the allocated buffer. On failure, this
2729 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
2732 CP_STATIC_IF_GLIBCPP_V3
2734 cplus_demangle_print (options, dc, estimate, palc)
2736 const struct demangle_component *dc;
2740 struct d_print_info dpi;
2742 dpi.options = options;
2744 dpi.alc = estimate + 1;
2745 dpi.buf = malloc (dpi.alc);
2746 if (dpi.buf == NULL)
2753 dpi.templates = NULL;
2754 dpi.modifiers = NULL;
2756 dpi.allocation_failure = 0;
2758 d_print_comp (&dpi, dc);
2760 d_append_char (&dpi, '\0');
2762 if (dpi.buf != NULL)
2765 *palc = dpi.allocation_failure;
2770 /* Subroutine to handle components. */
2773 d_print_comp (dpi, dc)
2774 struct d_print_info *dpi;
2775 const struct demangle_component *dc;
2779 d_print_error (dpi);
2782 if (d_print_saw_error (dpi))
2787 case DEMANGLE_COMPONENT_NAME:
2788 if ((dpi->options & DMGL_JAVA) == 0)
2789 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
2791 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
2794 case DEMANGLE_COMPONENT_QUAL_NAME:
2795 case DEMANGLE_COMPONENT_LOCAL_NAME:
2796 d_print_comp (dpi, d_left (dc));
2797 if ((dpi->options & DMGL_JAVA) == 0)
2798 d_append_string_constant (dpi, "::");
2800 d_append_char (dpi, '.');
2801 d_print_comp (dpi, d_right (dc));
2804 case DEMANGLE_COMPONENT_TYPED_NAME:
2806 struct d_print_mod *hold_modifiers;
2807 struct demangle_component *typed_name;
2808 struct d_print_mod adpm[4];
2810 struct d_print_template dpt;
2812 /* Pass the name down to the type so that it can be printed in
2813 the right place for the type. We also have to pass down
2814 any CV-qualifiers, which apply to the this parameter. */
2815 hold_modifiers = dpi->modifiers;
2817 typed_name = d_left (dc);
2818 while (typed_name != NULL)
2820 if (i >= sizeof adpm / sizeof adpm[0])
2822 d_print_error (dpi);
2826 adpm[i].next = dpi->modifiers;
2827 dpi->modifiers = &adpm[i];
2828 adpm[i].mod = typed_name;
2829 adpm[i].printed = 0;
2830 adpm[i].templates = dpi->templates;
2833 if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
2834 && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
2835 && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS)
2838 typed_name = d_left (typed_name);
2841 /* If typed_name is a template, then it applies to the
2842 function type as well. */
2843 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
2845 dpt.next = dpi->templates;
2846 dpi->templates = &dpt;
2847 dpt.template = typed_name;
2850 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
2851 there may be CV-qualifiers on its right argument which
2852 really apply here; this happens when parsing a class which
2853 is local to a function. */
2854 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
2856 struct demangle_component *local_name;
2858 local_name = d_right (typed_name);
2859 while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
2860 || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
2861 || local_name->type == DEMANGLE_COMPONENT_CONST_THIS)
2863 if (i >= sizeof adpm / sizeof adpm[0])
2865 d_print_error (dpi);
2869 adpm[i] = adpm[i - 1];
2870 adpm[i].next = &adpm[i - 1];
2871 dpi->modifiers = &adpm[i];
2873 adpm[i - 1].mod = local_name;
2874 adpm[i - 1].printed = 0;
2875 adpm[i - 1].templates = dpi->templates;
2878 local_name = d_left (local_name);
2882 d_print_comp (dpi, d_right (dc));
2884 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
2885 dpi->templates = dpt.next;
2887 /* If the modifiers didn't get printed by the type, print them
2892 if (! adpm[i].printed)
2894 d_append_char (dpi, ' ');
2895 d_print_mod (dpi, adpm[i].mod);
2899 dpi->modifiers = hold_modifiers;
2904 case DEMANGLE_COMPONENT_TEMPLATE:
2906 struct d_print_mod *hold_dpm;
2908 /* Don't push modifiers into a template definition. Doing so
2909 could give the wrong definition for a template argument.
2910 Instead, treat the template essentially as a name. */
2912 hold_dpm = dpi->modifiers;
2913 dpi->modifiers = NULL;
2915 d_print_comp (dpi, d_left (dc));
2916 if (d_last_char (dpi) == '<')
2917 d_append_char (dpi, ' ');
2918 d_append_char (dpi, '<');
2919 d_print_comp (dpi, d_right (dc));
2920 /* Avoid generating two consecutive '>' characters, to avoid
2921 the C++ syntactic ambiguity. */
2922 if (d_last_char (dpi) == '>')
2923 d_append_char (dpi, ' ');
2924 d_append_char (dpi, '>');
2926 dpi->modifiers = hold_dpm;
2931 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
2934 struct demangle_component *a;
2935 struct d_print_template *hold_dpt;
2937 if (dpi->templates == NULL)
2939 d_print_error (dpi);
2942 i = dc->u.s_number.number;
2943 for (a = d_right (dpi->templates->template);
2947 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
2949 d_print_error (dpi);
2956 if (i != 0 || a == NULL)
2958 d_print_error (dpi);
2962 /* While processing this parameter, we need to pop the list of
2963 templates. This is because the template parameter may
2964 itself be a reference to a parameter of an outer
2967 hold_dpt = dpi->templates;
2968 dpi->templates = hold_dpt->next;
2970 d_print_comp (dpi, d_left (a));
2972 dpi->templates = hold_dpt;
2977 case DEMANGLE_COMPONENT_CTOR:
2978 d_print_comp (dpi, dc->u.s_ctor.name);
2981 case DEMANGLE_COMPONENT_DTOR:
2982 d_append_char (dpi, '~');
2983 d_print_comp (dpi, dc->u.s_dtor.name);
2986 case DEMANGLE_COMPONENT_VTABLE:
2987 d_append_string_constant (dpi, "vtable for ");
2988 d_print_comp (dpi, d_left (dc));
2991 case DEMANGLE_COMPONENT_VTT:
2992 d_append_string_constant (dpi, "VTT for ");
2993 d_print_comp (dpi, d_left (dc));
2996 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
2997 d_append_string_constant (dpi, "construction vtable for ");
2998 d_print_comp (dpi, d_left (dc));
2999 d_append_string_constant (dpi, "-in-");
3000 d_print_comp (dpi, d_right (dc));
3003 case DEMANGLE_COMPONENT_TYPEINFO:
3004 d_append_string_constant (dpi, "typeinfo for ");
3005 d_print_comp (dpi, d_left (dc));
3008 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
3009 d_append_string_constant (dpi, "typeinfo name for ");
3010 d_print_comp (dpi, d_left (dc));
3013 case DEMANGLE_COMPONENT_TYPEINFO_FN:
3014 d_append_string_constant (dpi, "typeinfo fn for ");
3015 d_print_comp (dpi, d_left (dc));
3018 case DEMANGLE_COMPONENT_THUNK:
3019 d_append_string_constant (dpi, "non-virtual thunk to ");
3020 d_print_comp (dpi, d_left (dc));
3023 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
3024 d_append_string_constant (dpi, "virtual thunk to ");
3025 d_print_comp (dpi, d_left (dc));
3028 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
3029 d_append_string_constant (dpi, "covariant return thunk to ");
3030 d_print_comp (dpi, d_left (dc));
3033 case DEMANGLE_COMPONENT_JAVA_CLASS:
3034 d_append_string_constant (dpi, "java Class for ");
3035 d_print_comp (dpi, d_left (dc));
3038 case DEMANGLE_COMPONENT_GUARD:
3039 d_append_string_constant (dpi, "guard variable for ");
3040 d_print_comp (dpi, d_left (dc));
3043 case DEMANGLE_COMPONENT_REFTEMP:
3044 d_append_string_constant (dpi, "reference temporary for ");
3045 d_print_comp (dpi, d_left (dc));
3048 case DEMANGLE_COMPONENT_SUB_STD:
3049 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
3052 case DEMANGLE_COMPONENT_RESTRICT:
3053 case DEMANGLE_COMPONENT_VOLATILE:
3054 case DEMANGLE_COMPONENT_CONST:
3055 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3056 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3057 case DEMANGLE_COMPONENT_CONST_THIS:
3058 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3059 case DEMANGLE_COMPONENT_POINTER:
3060 case DEMANGLE_COMPONENT_REFERENCE:
3061 case DEMANGLE_COMPONENT_COMPLEX:
3062 case DEMANGLE_COMPONENT_IMAGINARY:
3064 /* We keep a list of modifiers on the stack. */
3065 struct d_print_mod dpm;
3067 dpm.next = dpi->modifiers;
3068 dpi->modifiers = &dpm;
3071 dpm.templates = dpi->templates;
3073 d_print_comp (dpi, d_left (dc));
3075 /* If the modifier didn't get printed by the type, print it
3078 d_print_mod (dpi, dc);
3080 dpi->modifiers = dpm.next;
3085 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3086 if ((dpi->options & DMGL_JAVA) == 0)
3087 d_append_buffer (dpi, dc->u.s_builtin.type->name,
3088 dc->u.s_builtin.type->len);
3090 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
3091 dc->u.s_builtin.type->java_len);
3094 case DEMANGLE_COMPONENT_VENDOR_TYPE:
3095 d_print_comp (dpi, d_left (dc));
3098 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
3100 if (d_left (dc) != NULL)
3102 struct d_print_mod dpm;
3104 /* We must pass this type down as a modifier in order to
3105 print it in the right location. */
3107 dpm.next = dpi->modifiers;
3108 dpi->modifiers = &dpm;
3111 dpm.templates = dpi->templates;
3113 d_print_comp (dpi, d_left (dc));
3115 dpi->modifiers = dpm.next;
3120 d_append_char (dpi, ' ');
3123 d_print_function_type (dpi, dc, dpi->modifiers);
3128 case DEMANGLE_COMPONENT_ARRAY_TYPE:
3130 struct d_print_mod dpm;
3132 /* We must pass this type down as a modifier in order to print
3133 multi-dimensional arrays correctly. */
3135 dpm.next = dpi->modifiers;
3136 dpi->modifiers = &dpm;
3139 dpm.templates = dpi->templates;
3141 d_print_comp (dpi, d_right (dc));
3143 dpi->modifiers = dpm.next;
3148 d_print_array_type (dpi, dc, dpi->modifiers);
3153 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3155 struct d_print_mod dpm;
3157 dpm.next = dpi->modifiers;
3158 dpi->modifiers = &dpm;
3161 dpm.templates = dpi->templates;
3163 d_print_comp (dpi, d_right (dc));
3165 /* If the modifier didn't get printed by the type, print it
3169 d_append_char (dpi, ' ');
3170 d_print_comp (dpi, d_left (dc));
3171 d_append_string_constant (dpi, "::*");
3174 dpi->modifiers = dpm.next;
3179 case DEMANGLE_COMPONENT_ARGLIST:
3180 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
3181 d_print_comp (dpi, d_left (dc));
3182 if (d_right (dc) != NULL)
3184 d_append_string_constant (dpi, ", ");
3185 d_print_comp (dpi, d_right (dc));
3189 case DEMANGLE_COMPONENT_OPERATOR:
3193 d_append_string_constant (dpi, "operator");
3194 c = dc->u.s_operator.op->name[0];
3196 d_append_char (dpi, ' ');
3197 d_append_buffer (dpi, dc->u.s_operator.op->name,
3198 dc->u.s_operator.op->len);
3202 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3203 d_append_string_constant (dpi, "operator ");
3204 d_print_comp (dpi, dc->u.s_extended_operator.name);
3207 case DEMANGLE_COMPONENT_CAST:
3208 d_append_string_constant (dpi, "operator ");
3209 d_print_cast (dpi, dc);
3212 case DEMANGLE_COMPONENT_UNARY:
3213 if (d_left (dc)->type != DEMANGLE_COMPONENT_CAST)
3214 d_print_expr_op (dpi, d_left (dc));
3217 d_append_string_constant (dpi, "((");
3218 d_print_cast (dpi, d_left (dc));
3219 d_append_char (dpi, ')');
3221 d_append_char (dpi, '(');
3222 d_print_comp (dpi, d_right (dc));
3223 d_append_char (dpi, ')');
3224 if (d_left (dc)->type == DEMANGLE_COMPONENT_CAST)
3225 d_append_char (dpi, ')');
3228 case DEMANGLE_COMPONENT_BINARY:
3229 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
3231 d_print_error (dpi);
3235 /* We wrap an expression which uses the greater-than operator in
3236 an extra layer of parens so that it does not get confused
3237 with the '>' which ends the template parameters. */
3238 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
3239 && d_left (dc)->u.s_operator.op->len == 1
3240 && d_left (dc)->u.s_operator.op->name[0] == '>')
3241 d_append_char (dpi, '(');
3243 d_append_char (dpi, '(');
3244 d_print_comp (dpi, d_left (d_right (dc)));
3245 d_append_string_constant (dpi, ") ");
3246 d_print_expr_op (dpi, d_left (dc));
3247 d_append_string_constant (dpi, " (");
3248 d_print_comp (dpi, d_right (d_right (dc)));
3249 d_append_char (dpi, ')');
3251 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
3252 && d_left (dc)->u.s_operator.op->len == 1
3253 && d_left (dc)->u.s_operator.op->name[0] == '>')
3254 d_append_char (dpi, ')');
3258 case DEMANGLE_COMPONENT_BINARY_ARGS:
3259 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
3260 d_print_error (dpi);
3263 case DEMANGLE_COMPONENT_TRINARY:
3264 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
3265 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
3267 d_print_error (dpi);
3270 d_append_char (dpi, '(');
3271 d_print_comp (dpi, d_left (d_right (dc)));
3272 d_append_string_constant (dpi, ") ");
3273 d_print_expr_op (dpi, d_left (dc));
3274 d_append_string_constant (dpi, " (");
3275 d_print_comp (dpi, d_left (d_right (d_right (dc))));
3276 d_append_string_constant (dpi, ") : (");
3277 d_print_comp (dpi, d_right (d_right (d_right (dc))));
3278 d_append_char (dpi, ')');
3281 case DEMANGLE_COMPONENT_TRINARY_ARG1:
3282 case DEMANGLE_COMPONENT_TRINARY_ARG2:
3283 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
3284 d_print_error (dpi);
3287 case DEMANGLE_COMPONENT_LITERAL:
3288 case DEMANGLE_COMPONENT_LITERAL_NEG:
3289 /* For some builtin types, produce simpler output. */
3290 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
3292 switch (d_left (dc)->u.s_builtin.type->print)
3295 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
3297 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
3298 d_append_char (dpi, '-');
3299 d_print_comp (dpi, d_right (dc));
3305 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
3307 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
3308 d_append_char (dpi, '-');
3309 d_print_comp (dpi, d_right (dc));
3310 d_append_char (dpi, 'l');
3316 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
3317 && d_right (dc)->u.s_name.len == 1
3318 && dc->type == DEMANGLE_COMPONENT_LITERAL)
3320 switch (d_right (dc)->u.s_name.s[0])
3323 d_append_string_constant (dpi, "false");
3326 d_append_string_constant (dpi, "true");
3339 d_append_char (dpi, '(');
3340 d_print_comp (dpi, d_left (dc));
3341 d_append_char (dpi, ')');
3342 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
3343 d_append_char (dpi, '-');
3344 d_print_comp (dpi, d_right (dc));
3348 d_print_error (dpi);
3353 /* Print a Java dentifier. For Java we try to handle encoded extended
3354 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
3355 so we don't it for C++. Characters are encoded as
3359 d_print_java_identifier (dpi, name, len)
3360 struct d_print_info *dpi;
3368 for (p = name; p < end; ++p)
3379 for (q = p + 3; q < end; ++q)
3385 else if (*q >= 'A' && *q <= 'F')
3386 dig = *q - 'A' + 10;
3387 else if (*q >= 'a' && *q <= 'f')
3388 dig = *q - 'a' + 10;
3394 /* If the Unicode character is larger than 256, we don't try
3395 to deal with it here. FIXME. */
3396 if (q < end && *q == '_' && c < 256)
3398 d_append_char (dpi, c);
3404 d_append_char (dpi, *p);
3408 /* Print a list of modifiers. SUFFIX is 1 if we are printing
3409 qualifiers on this after printing a function. */
3412 d_print_mod_list (dpi, mods, suffix)
3413 struct d_print_info *dpi;
3414 struct d_print_mod *mods;
3417 struct d_print_template *hold_dpt;
3419 if (mods == NULL || d_print_saw_error (dpi))
3424 && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3425 || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3426 || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS)))
3428 d_print_mod_list (dpi, mods->next, suffix);
3434 hold_dpt = dpi->templates;
3435 dpi->templates = mods->templates;
3437 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
3439 d_print_function_type (dpi, mods->mod, mods->next);
3440 dpi->templates = hold_dpt;
3443 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
3445 d_print_array_type (dpi, mods->mod, mods->next);
3446 dpi->templates = hold_dpt;
3449 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
3451 struct d_print_mod *hold_modifiers;
3452 struct demangle_component *dc;
3454 /* When this is on the modifier stack, we have pulled any
3455 qualifiers off the right argument already. Otherwise, we
3456 print it as usual, but don't let the left argument see any
3459 hold_modifiers = dpi->modifiers;
3460 dpi->modifiers = NULL;
3461 d_print_comp (dpi, d_left (mods->mod));
3462 dpi->modifiers = hold_modifiers;
3464 if ((dpi->options & DMGL_JAVA) == 0)
3465 d_append_string_constant (dpi, "::");
3467 d_append_char (dpi, '.');
3469 dc = d_right (mods->mod);
3470 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3471 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3472 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
3475 d_print_comp (dpi, dc);
3477 dpi->templates = hold_dpt;
3481 d_print_mod (dpi, mods->mod);
3483 dpi->templates = hold_dpt;
3485 d_print_mod_list (dpi, mods->next, suffix);
3488 /* Print a modifier. */
3491 d_print_mod (dpi, mod)
3492 struct d_print_info *dpi;
3493 const struct demangle_component *mod;
3497 case DEMANGLE_COMPONENT_RESTRICT:
3498 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3499 d_append_string_constant (dpi, " restrict");
3501 case DEMANGLE_COMPONENT_VOLATILE:
3502 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3503 d_append_string_constant (dpi, " volatile");
3505 case DEMANGLE_COMPONENT_CONST:
3506 case DEMANGLE_COMPONENT_CONST_THIS:
3507 d_append_string_constant (dpi, " const");
3509 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3510 d_append_char (dpi, ' ');
3511 d_print_comp (dpi, d_right (mod));
3513 case DEMANGLE_COMPONENT_POINTER:
3514 /* There is no pointer symbol in Java. */
3515 if ((dpi->options & DMGL_JAVA) == 0)
3516 d_append_char (dpi, '*');
3518 case DEMANGLE_COMPONENT_REFERENCE:
3519 d_append_char (dpi, '&');
3521 case DEMANGLE_COMPONENT_COMPLEX:
3522 d_append_string_constant (dpi, "complex ");
3524 case DEMANGLE_COMPONENT_IMAGINARY:
3525 d_append_string_constant (dpi, "imaginary ");
3527 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3528 if (d_last_char (dpi) != '(')
3529 d_append_char (dpi, ' ');
3530 d_print_comp (dpi, d_left (mod));
3531 d_append_string_constant (dpi, "::*");
3533 case DEMANGLE_COMPONENT_TYPED_NAME:
3534 d_print_comp (dpi, d_left (mod));
3537 /* Otherwise, we have something that won't go back on the
3538 modifier stack, so we can just print it. */
3539 d_print_comp (dpi, mod);
3544 /* Print a function type, except for the return type. */
3547 d_print_function_type (dpi, dc, mods)
3548 struct d_print_info *dpi;
3549 const struct demangle_component *dc;
3550 struct d_print_mod *mods;
3554 struct d_print_mod *p;
3555 struct d_print_mod *hold_modifiers;
3559 for (p = mods; p != NULL; p = p->next)
3565 switch (p->mod->type)
3567 case DEMANGLE_COMPONENT_RESTRICT:
3568 case DEMANGLE_COMPONENT_VOLATILE:
3569 case DEMANGLE_COMPONENT_CONST:
3570 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3571 case DEMANGLE_COMPONENT_POINTER:
3572 case DEMANGLE_COMPONENT_REFERENCE:
3573 case DEMANGLE_COMPONENT_COMPLEX:
3574 case DEMANGLE_COMPONENT_IMAGINARY:
3575 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3578 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3579 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3580 case DEMANGLE_COMPONENT_CONST_THIS:
3589 if (d_left (dc) != NULL && ! saw_mod)
3594 switch (d_last_char (dpi))
3602 d_append_char (dpi, ' ');
3606 d_append_char (dpi, '(');
3609 hold_modifiers = dpi->modifiers;
3610 dpi->modifiers = NULL;
3612 d_print_mod_list (dpi, mods, 0);
3615 d_append_char (dpi, ')');
3617 d_append_char (dpi, '(');
3619 if (d_right (dc) != NULL)
3620 d_print_comp (dpi, d_right (dc));
3622 d_append_char (dpi, ')');
3624 d_print_mod_list (dpi, mods, 1);
3626 dpi->modifiers = hold_modifiers;
3629 /* Print an array type, except for the element type. */
3632 d_print_array_type (dpi, dc, mods)
3633 struct d_print_info *dpi;
3634 const struct demangle_component *dc;
3635 struct d_print_mod *mods;
3643 struct d_print_mod *p;
3646 for (p = mods; p != NULL; p = p->next)
3651 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
3665 d_append_string_constant (dpi, " (");
3667 d_print_mod_list (dpi, mods, 0);
3670 d_append_char (dpi, ')');
3674 d_append_char (dpi, ' ');
3676 d_append_char (dpi, '[');
3678 if (d_left (dc) != NULL)
3679 d_print_comp (dpi, d_left (dc));
3681 d_append_char (dpi, ']');
3684 /* Print an operator in an expression. */
3687 d_print_expr_op (dpi, dc)
3688 struct d_print_info *dpi;
3689 const struct demangle_component *dc;
3691 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
3692 d_append_buffer (dpi, dc->u.s_operator.op->name,
3693 dc->u.s_operator.op->len);
3695 d_print_comp (dpi, dc);
3701 d_print_cast (dpi, dc)
3702 struct d_print_info *dpi;
3703 const struct demangle_component *dc;
3705 if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
3706 d_print_comp (dpi, d_left (dc));
3709 struct d_print_mod *hold_dpm;
3710 struct d_print_template dpt;
3712 /* It appears that for a templated cast operator, we need to put
3713 the template parameters in scope for the operator name, but
3714 not for the parameters. The effect is that we need to handle
3715 the template printing here. */
3717 hold_dpm = dpi->modifiers;
3718 dpi->modifiers = NULL;
3720 dpt.next = dpi->templates;
3721 dpi->templates = &dpt;
3722 dpt.template = d_left (dc);
3724 d_print_comp (dpi, d_left (d_left (dc)));
3726 dpi->templates = dpt.next;
3728 if (d_last_char (dpi) == '<')
3729 d_append_char (dpi, ' ');
3730 d_append_char (dpi, '<');
3731 d_print_comp (dpi, d_right (d_left (dc)));
3732 /* Avoid generating two consecutive '>' characters, to avoid
3733 the C++ syntactic ambiguity. */
3734 if (d_last_char (dpi) == '>')
3735 d_append_char (dpi, ' ');
3736 d_append_char (dpi, '>');
3738 dpi->modifiers = hold_dpm;
3742 /* Initialize the information structure we use to pass around
3745 CP_STATIC_IF_GLIBCPP_V3
3747 cplus_demangle_init_info (mangled, options, len, di)
3748 const char *mangled;
3754 di->send = mangled + len;
3755 di->options = options;
3759 /* We can not need more components than twice the number of chars in
3760 the mangled string. Most components correspond directly to
3761 chars, but the ARGLIST types are exceptions. */
3762 di->num_comps = 2 * len;
3765 /* Similarly, we can not need more substitutions than there are
3766 chars in the mangled string. */
3771 di->last_name = NULL;
3776 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
3777 name, return a buffer allocated with malloc holding the demangled
3778 name. OPTIONS is the usual libiberty demangler options. On
3779 success, this sets *PALC to the allocated size of the returned
3780 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
3781 a memory allocation failure. On failure, this returns NULL. */
3784 d_demangle (mangled, options, palc)
3785 const char* mangled;
3792 struct demangle_component *dc;
3798 len = strlen (mangled);
3800 if (mangled[0] == '_' && mangled[1] == 'Z')
3802 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
3803 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
3804 && (mangled[9] == 'D' || mangled[9] == 'I')
3805 && mangled[10] == '_')
3809 r = malloc (40 + len - 11);
3814 if (mangled[9] == 'I')
3815 strcpy (r, "global constructors keyed to ");
3817 strcpy (r, "global destructors keyed to ");
3818 strcat (r, mangled + 11);
3824 if ((options & DMGL_TYPES) == 0)
3829 cplus_demangle_init_info (mangled, options, len, &di);
3832 #ifdef CP_DYNAMIC_ARRAYS
3833 __extension__ struct demangle_component comps[di.num_comps];
3834 __extension__ struct demangle_component *subs[di.num_subs];
3836 di.comps = &comps[0];
3839 di.comps = ((struct demangle_component *)
3840 malloc (di.num_comps * sizeof (struct demangle_component)));
3841 di.subs = ((struct demangle_component **)
3842 malloc (di.num_subs * sizeof (struct demangle_component *)));
3843 if (di.comps == NULL || di.subs == NULL)
3845 if (di.comps != NULL)
3847 if (di.subs != NULL)
3855 dc = cplus_demangle_mangled_name (&di, 1);
3857 dc = cplus_demangle_type (&di);
3859 /* If DMGL_PARAMS is set, then if we didn't consume the entire
3860 mangled string, then we didn't successfully demangle it. If
3861 DMGL_PARAMS is not set, we didn't look at the trailing
3863 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
3866 #ifdef CP_DEMANGLE_DEBUG
3868 printf ("failed demangling\n");
3873 /* We try to guess the length of the demangled string, to minimize
3874 calls to realloc during demangling. */
3875 estimate = len + di.expansion + 10 * di.did_subs;
3876 estimate += estimate / 8;
3880 ret = cplus_demangle_print (options, dc, estimate, palc);
3882 #ifndef CP_DYNAMIC_ARRAYS
3887 #ifdef CP_DEMANGLE_DEBUG
3892 rlen = strlen (ret);
3893 if (rlen > 2 * estimate)
3894 printf ("*** Length %d much greater than estimate %d\n",
3896 else if (rlen > estimate)
3897 printf ("*** Length %d greater than estimate %d\n",
3899 else if (rlen < estimate / 2)
3900 printf ("*** Length %d much less than estimate %d\n",
3909 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
3911 extern char *__cxa_demangle PARAMS ((const char *, char *, size_t *, int *));
3913 /* ia64 ABI-mandated entry point in the C++ runtime library for
3914 performing demangling. MANGLED_NAME is a NUL-terminated character
3915 string containing the name to be demangled.
3917 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
3918 *LENGTH bytes, into which the demangled name is stored. If
3919 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
3920 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
3921 is placed in a region of memory allocated with malloc.
3923 If LENGTH is non-NULL, the length of the buffer conaining the
3924 demangled name, is placed in *LENGTH.
3926 The return value is a pointer to the start of the NUL-terminated
3927 demangled name, or NULL if the demangling fails. The caller is
3928 responsible for deallocating this memory using free.
3930 *STATUS is set to one of the following values:
3931 0: The demangling operation succeeded.
3932 -1: A memory allocation failure occurred.
3933 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
3934 -3: One of the arguments is invalid.
3936 The demangling is performed using the C++ ABI mangling rules, with
3940 __cxa_demangle (mangled_name, output_buffer, length, status)
3941 const char *mangled_name;
3942 char *output_buffer;
3952 if (mangled_name == NULL)
3958 if (output_buffer != NULL && length == NULL)
3964 demangled = d_demangle (mangled_name, DMGL_TYPES, &alc);
3966 if (demangled == NULL)
3975 if (output_buffer == NULL)
3982 if (strlen (demangled) < *length)
3984 strcpy (output_buffer, demangled);
3986 demangled = output_buffer;
3990 free (output_buffer);
4000 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
4002 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
4003 mangled name, return a buffer allocated with malloc holding the
4004 demangled name. Otherwise, return NULL. */
4007 cplus_demangle_v3 (mangled, options)
4008 const char* mangled;
4013 return d_demangle (mangled, options, &alc);
4016 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
4017 conventions, but the output formatting is a little different.
4018 This instructs the C++ demangler not to emit pointer characters ("*"), and
4019 to use Java's namespace separator symbol ("." instead of "::"). It then
4020 does an additional pass over the demangled output to replace instances
4021 of JArray<TYPE> with TYPE[]. */
4024 java_demangle_v3 (mangled)
4025 const char* mangled;
4033 demangled = d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS, &alc);
4035 if (demangled == NULL)
4041 while (*from != '\0')
4043 if (strncmp (from, "JArray<", 7) == 0)
4048 else if (nesting > 0 && *from == '>')
4050 while (to > demangled && to[-1] == ' ')
4066 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
4068 #ifndef IN_GLIBCPP_V3
4070 /* Demangle a string in order to find out whether it is a constructor
4071 or destructor. Return non-zero on success. Set *CTOR_KIND and
4072 *DTOR_KIND appropriately. */
4075 is_ctor_or_dtor (mangled, ctor_kind, dtor_kind)
4076 const char *mangled;
4077 enum gnu_v3_ctor_kinds *ctor_kind;
4078 enum gnu_v3_dtor_kinds *dtor_kind;
4081 struct demangle_component *dc;
4084 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
4085 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
4087 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
4090 #ifdef CP_DYNAMIC_ARRAYS
4091 __extension__ struct demangle_component comps[di.num_comps];
4092 __extension__ struct demangle_component *subs[di.num_subs];
4094 di.comps = &comps[0];
4097 di.comps = ((struct demangle_component *)
4098 malloc (di.num_comps * sizeof (struct demangle_component)));
4099 di.subs = ((struct demangle_component **)
4100 malloc (di.num_subs * sizeof (struct demangle_component *)));
4101 if (di.comps == NULL || di.subs == NULL)
4103 if (di.comps != NULL)
4105 if (di.subs != NULL)
4111 dc = cplus_demangle_mangled_name (&di, 1);
4113 /* Note that because we did not pass DMGL_PARAMS, we don't expect
4114 to demangle the entire string. */
4124 case DEMANGLE_COMPONENT_TYPED_NAME:
4125 case DEMANGLE_COMPONENT_TEMPLATE:
4126 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4127 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4128 case DEMANGLE_COMPONENT_CONST_THIS:
4131 case DEMANGLE_COMPONENT_QUAL_NAME:
4132 case DEMANGLE_COMPONENT_LOCAL_NAME:
4135 case DEMANGLE_COMPONENT_CTOR:
4136 *ctor_kind = dc->u.s_ctor.kind;
4140 case DEMANGLE_COMPONENT_DTOR:
4141 *dtor_kind = dc->u.s_dtor.kind;
4148 #ifndef CP_DYNAMIC_ARRAYS
4157 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
4158 name. A non-zero return indicates the type of constructor. */
4160 enum gnu_v3_ctor_kinds
4161 is_gnu_v3_mangled_ctor (name)
4164 enum gnu_v3_ctor_kinds ctor_kind;
4165 enum gnu_v3_dtor_kinds dtor_kind;
4167 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
4168 return (enum gnu_v3_ctor_kinds) 0;
4173 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
4174 name. A non-zero return indicates the type of destructor. */
4176 enum gnu_v3_dtor_kinds
4177 is_gnu_v3_mangled_dtor (name)
4180 enum gnu_v3_ctor_kinds ctor_kind;
4181 enum gnu_v3_dtor_kinds dtor_kind;
4183 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
4184 return (enum gnu_v3_dtor_kinds) 0;
4188 #endif /* IN_GLIBCPP_V3 */
4190 #ifdef STANDALONE_DEMANGLER
4193 #include "dyn-string.h"
4195 static void print_usage PARAMS ((FILE* fp, int exit_value));
4197 #define IS_ALPHA(CHAR) \
4198 (((CHAR) >= 'a' && (CHAR) <= 'z') \
4199 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
4201 /* Non-zero if CHAR is a character than can occur in a mangled name. */
4202 #define is_mangled_char(CHAR) \
4203 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
4204 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
4206 /* The name of this program, as invoked. */
4207 const char* program_name;
4209 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
4212 print_usage (fp, exit_value)
4216 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
4217 fprintf (fp, "Options:\n");
4218 fprintf (fp, " -h,--help Display this message.\n");
4219 fprintf (fp, " -p,--no-params Don't display function parameters\n");
4220 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
4221 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
4226 /* Option specification for getopt_long. */
4227 static const struct option long_options[] =
4229 { "help", no_argument, NULL, 'h' },
4230 { "no-params", no_argument, NULL, 'p' },
4231 { "verbose", no_argument, NULL, 'v' },
4232 { NULL, no_argument, NULL, 0 },
4235 /* Main entry for a demangling filter executable. It will demangle
4236 its command line arguments, if any. If none are provided, it will
4237 filter stdin to stdout, replacing any recognized mangled C++ names
4238 with their demangled equivalents. */
4247 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
4249 /* Use the program name of this program, as invoked. */
4250 program_name = argv[0];
4252 /* Parse options. */
4255 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
4258 case '?': /* Unrecognized option. */
4259 print_usage (stderr, 1);
4263 print_usage (stdout, 0);
4267 options &= ~ DMGL_PARAMS;
4271 options |= DMGL_VERBOSE;
4275 while (opt_char != -1);
4278 /* No command line arguments were provided. Filter stdin. */
4280 dyn_string_t mangled = dyn_string_new (3);
4283 /* Read all of input. */
4284 while (!feof (stdin))
4288 /* Pile characters into mangled until we hit one that can't
4289 occur in a mangled name. */
4291 while (!feof (stdin) && is_mangled_char (c))
4293 dyn_string_append_char (mangled, c);
4299 if (dyn_string_length (mangled) > 0)
4301 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
4310 /* It might not have been a mangled name. Print the
4312 fputs (dyn_string_buf (mangled), stdout);
4315 dyn_string_clear (mangled);
4318 /* If we haven't hit EOF yet, we've read one character that
4319 can't occur in a mangled name, so print it out. */
4324 dyn_string_delete (mangled);
4327 /* Demangle command line arguments. */
4329 /* Loop over command line arguments. */
4330 for (i = optind; i < argc; ++i)
4334 /* Attempt to demangle. */
4335 s = cplus_demangle_v3 (argv[i], options);
4337 /* If it worked, print the demangled name. */
4344 fprintf (stderr, "Failed: %s\n", argv[i]);
4351 #endif /* STANDALONE_DEMANGLER */