1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003, 2004, 2005 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 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
112 static int d_fill_name (struct demangle_component *, const char *, int);
114 #define cplus_demangle_fill_extended_operator d_fill_extended_operator
116 d_fill_extended_operator (struct demangle_component *, int,
117 struct demangle_component *);
119 #define cplus_demangle_fill_ctor d_fill_ctor
121 d_fill_ctor (struct demangle_component *, enum gnu_v3_ctor_kinds,
122 struct demangle_component *);
124 #define cplus_demangle_fill_dtor d_fill_dtor
126 d_fill_dtor (struct demangle_component *, enum gnu_v3_dtor_kinds,
127 struct demangle_component *);
129 #define cplus_demangle_mangled_name d_mangled_name
130 static struct demangle_component *d_mangled_name (struct d_info *, int);
132 #define cplus_demangle_type d_type
133 static struct demangle_component *d_type (struct d_info *);
135 #define cplus_demangle_print d_print
136 static char *d_print (int, const struct demangle_component *, int, size_t *);
138 #define cplus_demangle_init_info d_init_info
139 static void d_init_info (const char *, int, size_t, struct d_info *);
141 #else /* ! defined(IN_GLIBCPP_V3) */
142 #define CP_STATIC_IF_GLIBCPP_V3
143 #endif /* ! defined(IN_GLIBCPP_V3) */
145 /* See if the compiler supports dynamic arrays. */
148 #define CP_DYNAMIC_ARRAYS
151 #ifdef __STDC_VERSION__
152 #if __STDC_VERSION__ >= 199901L
153 #define CP_DYNAMIC_ARRAYS
154 #endif /* __STDC__VERSION >= 199901L */
155 #endif /* defined (__STDC_VERSION__) */
156 #endif /* defined (__STDC__) */
157 #endif /* ! defined (__GNUC__) */
159 /* We avoid pulling in the ctype tables, to prevent pulling in
160 additional unresolved symbols when this code is used in a library.
161 FIXME: Is this really a valid reason? This comes from the original
164 As of this writing this file has the following undefined references
165 when compiled with -DIN_GLIBCPP_V3: malloc, realloc, free, memcpy,
166 strcpy, strcat, strlen. */
168 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
169 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
170 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
172 /* The prefix prepended by GCC to an identifier represnting the
173 anonymous namespace. */
174 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
175 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
176 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
178 /* Information we keep for the standard substitutions. */
180 struct d_standard_sub_info
182 /* The code for this substitution. */
184 /* The simple string it expands to. */
185 const char *simple_expansion;
186 /* The length of the simple expansion. */
188 /* The results of a full, verbose, expansion. This is used when
189 qualifying a constructor/destructor, or when in verbose mode. */
190 const char *full_expansion;
191 /* The length of the full expansion. */
193 /* What to set the last_name field of d_info to; NULL if we should
194 not set it. This is only relevant when qualifying a
195 constructor/destructor. */
196 const char *set_last_name;
197 /* The length of set_last_name. */
198 int set_last_name_len;
201 /* Accessors for subtrees of struct demangle_component. */
203 #define d_left(dc) ((dc)->u.s_binary.left)
204 #define d_right(dc) ((dc)->u.s_binary.right)
206 /* A list of templates. This is used while printing. */
208 struct d_print_template
210 /* Next template on the list. */
211 struct d_print_template *next;
213 const struct demangle_component *template_decl;
216 /* A list of type modifiers. This is used while printing. */
220 /* Next modifier on the list. These are in the reverse of the order
221 in which they appeared in the mangled string. */
222 struct d_print_mod *next;
224 const struct demangle_component *mod;
225 /* Whether this modifier was printed. */
227 /* The list of templates which applies to this modifier. */
228 struct d_print_template *templates;
231 /* We use this structure to hold information during printing. */
235 /* The options passed to the demangler. */
237 /* Buffer holding the result. */
239 /* Current length of data in buffer. */
241 /* Allocated size of buffer. */
243 /* The current list of templates, if any. */
244 struct d_print_template *templates;
245 /* The current list of modifiers (e.g., pointer, reference, etc.),
247 struct d_print_mod *modifiers;
248 /* Set to 1 if we had a memory allocation failure. */
249 int allocation_failure;
252 #define d_print_saw_error(dpi) ((dpi)->buf == NULL)
254 #define d_append_char(dpi, c) \
257 if ((dpi)->buf != NULL && (dpi)->len < (dpi)->alc) \
258 (dpi)->buf[(dpi)->len++] = (c); \
260 d_print_append_char ((dpi), (c)); \
264 #define d_append_buffer(dpi, s, l) \
267 if ((dpi)->buf != NULL && (dpi)->len + (l) <= (dpi)->alc) \
269 memcpy ((dpi)->buf + (dpi)->len, (s), (l)); \
273 d_print_append_buffer ((dpi), (s), (l)); \
277 #define d_append_string_constant(dpi, s) \
278 d_append_buffer (dpi, (s), sizeof (s) - 1)
280 #define d_last_char(dpi) \
281 ((dpi)->buf == NULL || (dpi)->len == 0 ? '\0' : (dpi)->buf[(dpi)->len - 1])
283 #ifdef CP_DEMANGLE_DEBUG
284 static void d_dump (struct demangle_component *, int);
287 static struct demangle_component *
288 d_make_empty (struct d_info *);
290 static struct demangle_component *
291 d_make_comp (struct d_info *, enum demangle_component_type,
292 struct demangle_component *,
293 struct demangle_component *);
295 static struct demangle_component *
296 d_make_name (struct d_info *, const char *, int);
298 static struct demangle_component *
299 d_make_builtin_type (struct d_info *,
300 const struct demangle_builtin_type_info *);
302 static struct demangle_component *
303 d_make_operator (struct d_info *,
304 const struct demangle_operator_info *);
306 static struct demangle_component *
307 d_make_extended_operator (struct d_info *, int,
308 struct demangle_component *);
310 static struct demangle_component *
311 d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
312 struct demangle_component *);
314 static struct demangle_component *
315 d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
316 struct demangle_component *);
318 static struct demangle_component *
319 d_make_template_param (struct d_info *, long);
321 static struct demangle_component *
322 d_make_sub (struct d_info *, const char *, int);
325 has_return_type (struct demangle_component *);
328 is_ctor_dtor_or_conversion (struct demangle_component *);
330 static struct demangle_component *d_encoding (struct d_info *, int);
332 static struct demangle_component *d_name (struct d_info *);
334 static struct demangle_component *d_nested_name (struct d_info *);
336 static struct demangle_component *d_prefix (struct d_info *);
338 static struct demangle_component *d_unqualified_name (struct d_info *);
340 static struct demangle_component *d_source_name (struct d_info *);
342 static long d_number (struct d_info *);
344 static struct demangle_component *d_identifier (struct d_info *, int);
346 static struct demangle_component *d_operator_name (struct d_info *);
348 static struct demangle_component *d_special_name (struct d_info *);
350 static int d_call_offset (struct d_info *, int);
352 static struct demangle_component *d_ctor_dtor_name (struct d_info *);
354 static struct demangle_component **
355 d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
357 static struct demangle_component *
358 d_function_type (struct d_info *);
360 static struct demangle_component *
361 d_bare_function_type (struct d_info *, int);
363 static struct demangle_component *
364 d_class_enum_type (struct d_info *);
366 static struct demangle_component *d_array_type (struct d_info *);
368 static struct demangle_component *
369 d_pointer_to_member_type (struct d_info *);
371 static struct demangle_component *
372 d_template_param (struct d_info *);
374 static struct demangle_component *d_template_args (struct d_info *);
376 static struct demangle_component *
377 d_template_arg (struct d_info *);
379 static struct demangle_component *d_expression (struct d_info *);
381 static struct demangle_component *d_expr_primary (struct d_info *);
383 static struct demangle_component *d_local_name (struct d_info *);
385 static int d_discriminator (struct d_info *);
388 d_add_substitution (struct d_info *, struct demangle_component *);
390 static struct demangle_component *d_substitution (struct d_info *, int);
392 static void d_print_resize (struct d_print_info *, size_t);
394 static void d_print_append_char (struct d_print_info *, int);
397 d_print_append_buffer (struct d_print_info *, const char *, size_t);
399 static void d_print_error (struct d_print_info *);
402 d_print_comp (struct d_print_info *, const struct demangle_component *);
405 d_print_java_identifier (struct d_print_info *, const char *, int);
408 d_print_mod_list (struct d_print_info *, struct d_print_mod *, int);
411 d_print_mod (struct d_print_info *, const struct demangle_component *);
414 d_print_function_type (struct d_print_info *,
415 const struct demangle_component *,
416 struct d_print_mod *);
419 d_print_array_type (struct d_print_info *,
420 const struct demangle_component *,
421 struct d_print_mod *);
424 d_print_expr_op (struct d_print_info *, const struct demangle_component *);
427 d_print_cast (struct d_print_info *, const struct demangle_component *);
429 static char *d_demangle (const char *, int, size_t *);
431 #ifdef CP_DEMANGLE_DEBUG
434 d_dump (struct demangle_component *dc, int indent)
441 for (i = 0; i < indent; ++i)
446 case DEMANGLE_COMPONENT_NAME:
447 printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
449 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
450 printf ("template parameter %ld\n", dc->u.s_number.number);
452 case DEMANGLE_COMPONENT_CTOR:
453 printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
454 d_dump (dc->u.s_ctor.name, indent + 2);
456 case DEMANGLE_COMPONENT_DTOR:
457 printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
458 d_dump (dc->u.s_dtor.name, indent + 2);
460 case DEMANGLE_COMPONENT_SUB_STD:
461 printf ("standard substitution %s\n", dc->u.s_string.string);
463 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
464 printf ("builtin type %s\n", dc->u.s_builtin.type->name);
466 case DEMANGLE_COMPONENT_OPERATOR:
467 printf ("operator %s\n", dc->u.s_operator.op->name);
469 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
470 printf ("extended operator with %d args\n",
471 dc->u.s_extended_operator.args);
472 d_dump (dc->u.s_extended_operator.name, indent + 2);
475 case DEMANGLE_COMPONENT_QUAL_NAME:
476 printf ("qualified name\n");
478 case DEMANGLE_COMPONENT_LOCAL_NAME:
479 printf ("local name\n");
481 case DEMANGLE_COMPONENT_TYPED_NAME:
482 printf ("typed name\n");
484 case DEMANGLE_COMPONENT_TEMPLATE:
485 printf ("template\n");
487 case DEMANGLE_COMPONENT_VTABLE:
490 case DEMANGLE_COMPONENT_VTT:
493 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
494 printf ("construction vtable\n");
496 case DEMANGLE_COMPONENT_TYPEINFO:
497 printf ("typeinfo\n");
499 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
500 printf ("typeinfo name\n");
502 case DEMANGLE_COMPONENT_TYPEINFO_FN:
503 printf ("typeinfo function\n");
505 case DEMANGLE_COMPONENT_THUNK:
508 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
509 printf ("virtual thunk\n");
511 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
512 printf ("covariant thunk\n");
514 case DEMANGLE_COMPONENT_JAVA_CLASS:
515 printf ("java class\n");
517 case DEMANGLE_COMPONENT_GUARD:
520 case DEMANGLE_COMPONENT_REFTEMP:
521 printf ("reference temporary\n");
523 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
524 printf ("hidden alias\n");
526 case DEMANGLE_COMPONENT_RESTRICT:
527 printf ("restrict\n");
529 case DEMANGLE_COMPONENT_VOLATILE:
530 printf ("volatile\n");
532 case DEMANGLE_COMPONENT_CONST:
535 case DEMANGLE_COMPONENT_RESTRICT_THIS:
536 printf ("restrict this\n");
538 case DEMANGLE_COMPONENT_VOLATILE_THIS:
539 printf ("volatile this\n");
541 case DEMANGLE_COMPONENT_CONST_THIS:
542 printf ("const this\n");
544 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
545 printf ("vendor type qualifier\n");
547 case DEMANGLE_COMPONENT_POINTER:
548 printf ("pointer\n");
550 case DEMANGLE_COMPONENT_REFERENCE:
551 printf ("reference\n");
553 case DEMANGLE_COMPONENT_COMPLEX:
554 printf ("complex\n");
556 case DEMANGLE_COMPONENT_IMAGINARY:
557 printf ("imaginary\n");
559 case DEMANGLE_COMPONENT_VENDOR_TYPE:
560 printf ("vendor type\n");
562 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
563 printf ("function type\n");
565 case DEMANGLE_COMPONENT_ARRAY_TYPE:
566 printf ("array type\n");
568 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
569 printf ("pointer to member type\n");
571 case DEMANGLE_COMPONENT_ARGLIST:
572 printf ("argument list\n");
574 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
575 printf ("template argument list\n");
577 case DEMANGLE_COMPONENT_CAST:
580 case DEMANGLE_COMPONENT_UNARY:
581 printf ("unary operator\n");
583 case DEMANGLE_COMPONENT_BINARY:
584 printf ("binary operator\n");
586 case DEMANGLE_COMPONENT_BINARY_ARGS:
587 printf ("binary operator arguments\n");
589 case DEMANGLE_COMPONENT_TRINARY:
590 printf ("trinary operator\n");
592 case DEMANGLE_COMPONENT_TRINARY_ARG1:
593 printf ("trinary operator arguments 1\n");
595 case DEMANGLE_COMPONENT_TRINARY_ARG2:
596 printf ("trinary operator arguments 1\n");
598 case DEMANGLE_COMPONENT_LITERAL:
599 printf ("literal\n");
601 case DEMANGLE_COMPONENT_LITERAL_NEG:
602 printf ("negative literal\n");
606 d_dump (d_left (dc), indent + 2);
607 d_dump (d_right (dc), indent + 2);
610 #endif /* CP_DEMANGLE_DEBUG */
612 /* Fill in a DEMANGLE_COMPONENT_NAME. */
614 CP_STATIC_IF_GLIBCPP_V3
616 cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
618 if (p == NULL || s == NULL || len == 0)
620 p->type = DEMANGLE_COMPONENT_NAME;
622 p->u.s_name.len = len;
626 /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
628 CP_STATIC_IF_GLIBCPP_V3
630 cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
631 struct demangle_component *name)
633 if (p == NULL || args < 0 || name == NULL)
635 p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
636 p->u.s_extended_operator.args = args;
637 p->u.s_extended_operator.name = name;
641 /* Fill in a DEMANGLE_COMPONENT_CTOR. */
643 CP_STATIC_IF_GLIBCPP_V3
645 cplus_demangle_fill_ctor (struct demangle_component *p,
646 enum gnu_v3_ctor_kinds kind,
647 struct demangle_component *name)
651 || (kind < gnu_v3_complete_object_ctor
652 && kind > gnu_v3_complete_object_allocating_ctor))
654 p->type = DEMANGLE_COMPONENT_CTOR;
655 p->u.s_ctor.kind = kind;
656 p->u.s_ctor.name = name;
660 /* Fill in a DEMANGLE_COMPONENT_DTOR. */
662 CP_STATIC_IF_GLIBCPP_V3
664 cplus_demangle_fill_dtor (struct demangle_component *p,
665 enum gnu_v3_dtor_kinds kind,
666 struct demangle_component *name)
670 || (kind < gnu_v3_deleting_dtor
671 && kind > gnu_v3_base_object_dtor))
673 p->type = DEMANGLE_COMPONENT_DTOR;
674 p->u.s_dtor.kind = kind;
675 p->u.s_dtor.name = name;
679 /* Add a new component. */
681 static struct demangle_component *
682 d_make_empty (struct d_info *di)
684 struct demangle_component *p;
686 if (di->next_comp >= di->num_comps)
688 p = &di->comps[di->next_comp];
693 /* Add a new generic component. */
695 static struct demangle_component *
696 d_make_comp (struct d_info *di, enum demangle_component_type type,
697 struct demangle_component *left,
698 struct demangle_component *right)
700 struct demangle_component *p;
702 /* We check for errors here. A typical error would be a NULL return
703 from a subroutine. We catch those here, and return NULL
707 /* These types require two parameters. */
708 case DEMANGLE_COMPONENT_QUAL_NAME:
709 case DEMANGLE_COMPONENT_LOCAL_NAME:
710 case DEMANGLE_COMPONENT_TYPED_NAME:
711 case DEMANGLE_COMPONENT_TEMPLATE:
712 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
713 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
714 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
715 case DEMANGLE_COMPONENT_UNARY:
716 case DEMANGLE_COMPONENT_BINARY:
717 case DEMANGLE_COMPONENT_BINARY_ARGS:
718 case DEMANGLE_COMPONENT_TRINARY:
719 case DEMANGLE_COMPONENT_TRINARY_ARG1:
720 case DEMANGLE_COMPONENT_TRINARY_ARG2:
721 case DEMANGLE_COMPONENT_LITERAL:
722 case DEMANGLE_COMPONENT_LITERAL_NEG:
723 if (left == NULL || right == NULL)
727 /* These types only require one parameter. */
728 case DEMANGLE_COMPONENT_VTABLE:
729 case DEMANGLE_COMPONENT_VTT:
730 case DEMANGLE_COMPONENT_TYPEINFO:
731 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
732 case DEMANGLE_COMPONENT_TYPEINFO_FN:
733 case DEMANGLE_COMPONENT_THUNK:
734 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
735 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
736 case DEMANGLE_COMPONENT_JAVA_CLASS:
737 case DEMANGLE_COMPONENT_GUARD:
738 case DEMANGLE_COMPONENT_REFTEMP:
739 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
740 case DEMANGLE_COMPONENT_POINTER:
741 case DEMANGLE_COMPONENT_REFERENCE:
742 case DEMANGLE_COMPONENT_COMPLEX:
743 case DEMANGLE_COMPONENT_IMAGINARY:
744 case DEMANGLE_COMPONENT_VENDOR_TYPE:
745 case DEMANGLE_COMPONENT_ARGLIST:
746 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
747 case DEMANGLE_COMPONENT_CAST:
752 /* This needs a right parameter, but the left parameter can be
754 case DEMANGLE_COMPONENT_ARRAY_TYPE:
759 /* These are allowed to have no parameters--in some cases they
760 will be filled in later. */
761 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
762 case DEMANGLE_COMPONENT_RESTRICT:
763 case DEMANGLE_COMPONENT_VOLATILE:
764 case DEMANGLE_COMPONENT_CONST:
765 case DEMANGLE_COMPONENT_RESTRICT_THIS:
766 case DEMANGLE_COMPONENT_VOLATILE_THIS:
767 case DEMANGLE_COMPONENT_CONST_THIS:
770 /* Other types should not be seen here. */
775 p = d_make_empty (di);
779 p->u.s_binary.left = left;
780 p->u.s_binary.right = right;
785 /* Add a new name component. */
787 static struct demangle_component *
788 d_make_name (struct d_info *di, const char *s, int len)
790 struct demangle_component *p;
792 p = d_make_empty (di);
793 if (! cplus_demangle_fill_name (p, s, len))
798 /* Add a new builtin type component. */
800 static struct demangle_component *
801 d_make_builtin_type (struct d_info *di,
802 const struct demangle_builtin_type_info *type)
804 struct demangle_component *p;
808 p = d_make_empty (di);
811 p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
812 p->u.s_builtin.type = type;
817 /* Add a new operator component. */
819 static struct demangle_component *
820 d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
822 struct demangle_component *p;
824 p = d_make_empty (di);
827 p->type = DEMANGLE_COMPONENT_OPERATOR;
828 p->u.s_operator.op = op;
833 /* Add a new extended operator component. */
835 static struct demangle_component *
836 d_make_extended_operator (struct d_info *di, int args,
837 struct demangle_component *name)
839 struct demangle_component *p;
841 p = d_make_empty (di);
842 if (! cplus_demangle_fill_extended_operator (p, args, name))
847 /* Add a new constructor component. */
849 static struct demangle_component *
850 d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
851 struct demangle_component *name)
853 struct demangle_component *p;
855 p = d_make_empty (di);
856 if (! cplus_demangle_fill_ctor (p, kind, name))
861 /* Add a new destructor component. */
863 static struct demangle_component *
864 d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
865 struct demangle_component *name)
867 struct demangle_component *p;
869 p = d_make_empty (di);
870 if (! cplus_demangle_fill_dtor (p, kind, name))
875 /* Add a new template parameter. */
877 static struct demangle_component *
878 d_make_template_param (struct d_info *di, long i)
880 struct demangle_component *p;
882 p = d_make_empty (di);
885 p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
886 p->u.s_number.number = i;
891 /* Add a new standard substitution component. */
893 static struct demangle_component *
894 d_make_sub (struct d_info *di, const char *name, int len)
896 struct demangle_component *p;
898 p = d_make_empty (di);
901 p->type = DEMANGLE_COMPONENT_SUB_STD;
902 p->u.s_string.string = name;
903 p->u.s_string.len = len;
908 /* <mangled-name> ::= _Z <encoding>
910 TOP_LEVEL is non-zero when called at the top level. */
912 CP_STATIC_IF_GLIBCPP_V3
913 struct demangle_component *
914 cplus_demangle_mangled_name (struct d_info *di, int top_level)
916 if (! d_check_char (di, '_'))
918 if (! d_check_char (di, 'Z'))
920 return d_encoding (di, top_level);
923 /* Return whether a function should have a return type. The argument
924 is the function name, which may be qualified in various ways. The
925 rules are that template functions have return types with some
926 exceptions, function types which are not part of a function name
927 mangling have return types with some exceptions, and non-template
928 function names do not have return types. The exceptions are that
929 constructors, destructors, and conversion operators do not have
933 has_return_type (struct demangle_component *dc)
941 case DEMANGLE_COMPONENT_TEMPLATE:
942 return ! is_ctor_dtor_or_conversion (d_left (dc));
943 case DEMANGLE_COMPONENT_RESTRICT_THIS:
944 case DEMANGLE_COMPONENT_VOLATILE_THIS:
945 case DEMANGLE_COMPONENT_CONST_THIS:
946 return has_return_type (d_left (dc));
950 /* Return whether a name is a constructor, a destructor, or a
951 conversion operator. */
954 is_ctor_dtor_or_conversion (struct demangle_component *dc)
962 case DEMANGLE_COMPONENT_QUAL_NAME:
963 case DEMANGLE_COMPONENT_LOCAL_NAME:
964 return is_ctor_dtor_or_conversion (d_right (dc));
965 case DEMANGLE_COMPONENT_CTOR:
966 case DEMANGLE_COMPONENT_DTOR:
967 case DEMANGLE_COMPONENT_CAST:
972 /* <encoding> ::= <(function) name> <bare-function-type>
976 TOP_LEVEL is non-zero when called at the top level, in which case
977 if DMGL_PARAMS is not set we do not demangle the function
978 parameters. We only set this at the top level, because otherwise
979 we would not correctly demangle names in local scopes. */
981 static struct demangle_component *
982 d_encoding (struct d_info *di, int top_level)
984 char peek = d_peek_char (di);
986 if (peek == 'G' || peek == 'T')
987 return d_special_name (di);
990 struct demangle_component *dc;
994 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
996 /* Strip off any initial CV-qualifiers, as they really apply
997 to the `this' parameter, and they were not output by the
998 v2 demangler without DMGL_PARAMS. */
999 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1000 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1001 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
1004 /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
1005 there may be CV-qualifiers on its right argument which
1006 really apply here; this happens when parsing a class
1007 which is local to a function. */
1008 if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
1010 struct demangle_component *dcr;
1013 while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS
1014 || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS
1015 || dcr->type == DEMANGLE_COMPONENT_CONST_THIS)
1017 dc->u.s_binary.right = dcr;
1023 peek = d_peek_char (di);
1024 if (dc == NULL || peek == '\0' || peek == 'E')
1026 return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
1027 d_bare_function_type (di, has_return_type (dc)));
1031 /* <name> ::= <nested-name>
1033 ::= <unscoped-template-name> <template-args>
1036 <unscoped-name> ::= <unqualified-name>
1037 ::= St <unqualified-name>
1039 <unscoped-template-name> ::= <unscoped-name>
1043 static struct demangle_component *
1044 d_name (struct d_info *di)
1046 char peek = d_peek_char (di);
1047 struct demangle_component *dc;
1052 return d_nested_name (di);
1055 return d_local_name (di);
1058 return d_unqualified_name (di);
1064 if (d_peek_next_char (di) != 't')
1066 dc = d_substitution (di, 0);
1072 dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
1073 d_make_name (di, "std", 3),
1074 d_unqualified_name (di));
1079 if (d_peek_char (di) != 'I')
1081 /* The grammar does not permit this case to occur if we
1082 called d_substitution() above (i.e., subst == 1). We
1083 don't bother to check. */
1087 /* This is <template-args>, which means that we just saw
1088 <unscoped-template-name>, which is a substitution
1089 candidate if we didn't just get it from a
1093 if (! d_add_substitution (di, dc))
1096 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1097 d_template_args (di));
1104 dc = d_unqualified_name (di);
1105 if (d_peek_char (di) == 'I')
1107 /* This is <template-args>, which means that we just saw
1108 <unscoped-template-name>, which is a substitution
1110 if (! d_add_substitution (di, dc))
1112 dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
1113 d_template_args (di));
1119 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1120 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1123 static struct demangle_component *
1124 d_nested_name (struct d_info *di)
1126 struct demangle_component *ret;
1127 struct demangle_component **pret;
1129 if (! d_check_char (di, 'N'))
1132 pret = d_cv_qualifiers (di, &ret, 1);
1136 *pret = d_prefix (di);
1140 if (! d_check_char (di, 'E'))
1146 /* <prefix> ::= <prefix> <unqualified-name>
1147 ::= <template-prefix> <template-args>
1148 ::= <template-param>
1152 <template-prefix> ::= <prefix> <(template) unqualified-name>
1153 ::= <template-param>
1157 static struct demangle_component *
1158 d_prefix (struct d_info *di)
1160 struct demangle_component *ret = NULL;
1165 enum demangle_component_type comb_type;
1166 struct demangle_component *dc;
1168 peek = d_peek_char (di);
1172 /* The older code accepts a <local-name> here, but I don't see
1173 that in the grammar. The older code does not accept a
1174 <template-param> here. */
1176 comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
1182 dc = d_unqualified_name (di);
1183 else if (peek == 'S')
1184 dc = d_substitution (di, 1);
1185 else if (peek == 'I')
1189 comb_type = DEMANGLE_COMPONENT_TEMPLATE;
1190 dc = d_template_args (di);
1192 else if (peek == 'T')
1193 dc = d_template_param (di);
1194 else if (peek == 'E')
1202 ret = d_make_comp (di, comb_type, ret, dc);
1204 if (peek != 'S' && d_peek_char (di) != 'E')
1206 if (! d_add_substitution (di, ret))
1212 /* <unqualified-name> ::= <operator-name>
1213 ::= <ctor-dtor-name>
1215 ::= <local-source-name>
1217 <local-source-name> ::= L <source-name> <discriminator>
1220 static struct demangle_component *
1221 d_unqualified_name (struct d_info *di)
1225 peek = d_peek_char (di);
1226 if (IS_DIGIT (peek))
1227 return d_source_name (di);
1228 else if (IS_LOWER (peek))
1230 struct demangle_component *ret;
1232 ret = d_operator_name (di);
1233 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
1234 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1237 else if (peek == 'C' || peek == 'D')
1238 return d_ctor_dtor_name (di);
1239 else if (peek == 'L')
1241 struct demangle_component * ret;
1245 ret = d_source_name (di);
1248 if (! d_discriminator (di))
1256 /* <source-name> ::= <(positive length) number> <identifier> */
1258 static struct demangle_component *
1259 d_source_name (struct d_info *di)
1262 struct demangle_component *ret;
1264 len = d_number (di);
1267 ret = d_identifier (di, len);
1268 di->last_name = ret;
1272 /* number ::= [n] <(non-negative decimal integer)> */
1275 d_number (struct d_info *di)
1282 peek = d_peek_char (di);
1287 peek = d_peek_char (di);
1293 if (! IS_DIGIT (peek))
1299 ret = ret * 10 + peek - '0';
1301 peek = d_peek_char (di);
1305 /* identifier ::= <(unqualified source code identifier)> */
1307 static struct demangle_component *
1308 d_identifier (struct d_info *di, int len)
1314 if (di->send - name < len)
1317 d_advance (di, len);
1319 /* A Java mangled name may have a trailing '$' if it is a C++
1320 keyword. This '$' is not included in the length count. We just
1322 if ((di->options & DMGL_JAVA) != 0
1323 && d_peek_char (di) == '$')
1326 /* Look for something which looks like a gcc encoding of an
1327 anonymous namespace, and replace it with a more user friendly
1329 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1330 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1331 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1335 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1336 if ((*s == '.' || *s == '_' || *s == '$')
1339 di->expansion -= len - sizeof "(anonymous namespace)";
1340 return d_make_name (di, "(anonymous namespace)",
1341 sizeof "(anonymous namespace)" - 1);
1345 return d_make_name (di, name, len);
1348 /* operator_name ::= many different two character encodings.
1350 ::= v <digit> <source-name>
1353 #define NL(s) s, (sizeof s) - 1
1355 CP_STATIC_IF_GLIBCPP_V3
1356 const struct demangle_operator_info cplus_demangle_operators[] =
1358 { "aN", NL ("&="), 2 },
1359 { "aS", NL ("="), 2 },
1360 { "aa", NL ("&&"), 2 },
1361 { "ad", NL ("&"), 1 },
1362 { "an", NL ("&"), 2 },
1363 { "cl", NL ("()"), 0 },
1364 { "cm", NL (","), 2 },
1365 { "co", NL ("~"), 1 },
1366 { "dV", NL ("/="), 2 },
1367 { "da", NL ("delete[]"), 1 },
1368 { "de", NL ("*"), 1 },
1369 { "dl", NL ("delete"), 1 },
1370 { "dv", NL ("/"), 2 },
1371 { "eO", NL ("^="), 2 },
1372 { "eo", NL ("^"), 2 },
1373 { "eq", NL ("=="), 2 },
1374 { "ge", NL (">="), 2 },
1375 { "gt", NL (">"), 2 },
1376 { "ix", NL ("[]"), 2 },
1377 { "lS", NL ("<<="), 2 },
1378 { "le", NL ("<="), 2 },
1379 { "ls", NL ("<<"), 2 },
1380 { "lt", NL ("<"), 2 },
1381 { "mI", NL ("-="), 2 },
1382 { "mL", NL ("*="), 2 },
1383 { "mi", NL ("-"), 2 },
1384 { "ml", NL ("*"), 2 },
1385 { "mm", NL ("--"), 1 },
1386 { "na", NL ("new[]"), 1 },
1387 { "ne", NL ("!="), 2 },
1388 { "ng", NL ("-"), 1 },
1389 { "nt", NL ("!"), 1 },
1390 { "nw", NL ("new"), 1 },
1391 { "oR", NL ("|="), 2 },
1392 { "oo", NL ("||"), 2 },
1393 { "or", NL ("|"), 2 },
1394 { "pL", NL ("+="), 2 },
1395 { "pl", NL ("+"), 2 },
1396 { "pm", NL ("->*"), 2 },
1397 { "pp", NL ("++"), 1 },
1398 { "ps", NL ("+"), 1 },
1399 { "pt", NL ("->"), 2 },
1400 { "qu", NL ("?"), 3 },
1401 { "rM", NL ("%="), 2 },
1402 { "rS", NL (">>="), 2 },
1403 { "rm", NL ("%"), 2 },
1404 { "rs", NL (">>"), 2 },
1405 { "st", NL ("sizeof "), 1 },
1406 { "sz", NL ("sizeof "), 1 },
1407 { NULL, NULL, 0, 0 }
1410 static struct demangle_component *
1411 d_operator_name (struct d_info *di)
1416 c1 = d_next_char (di);
1417 c2 = d_next_char (di);
1418 if (c1 == 'v' && IS_DIGIT (c2))
1419 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1420 else if (c1 == 'c' && c2 == 'v')
1421 return d_make_comp (di, DEMANGLE_COMPONENT_CAST,
1422 cplus_demangle_type (di), NULL);
1425 /* LOW is the inclusive lower bound. */
1427 /* HIGH is the exclusive upper bound. We subtract one to ignore
1428 the sentinel at the end of the array. */
1429 int high = ((sizeof (cplus_demangle_operators)
1430 / sizeof (cplus_demangle_operators[0]))
1436 const struct demangle_operator_info *p;
1438 i = low + (high - low) / 2;
1439 p = cplus_demangle_operators + i;
1441 if (c1 == p->code[0] && c2 == p->code[1])
1442 return d_make_operator (di, p);
1444 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1454 /* <special-name> ::= TV <type>
1458 ::= GV <(object) name>
1459 ::= T <call-offset> <(base) encoding>
1460 ::= Tc <call-offset> <call-offset> <(base) encoding>
1461 Also g++ extensions:
1462 ::= TC <type> <(offset) number> _ <(base) type>
1469 static struct demangle_component *
1470 d_special_name (struct d_info *di)
1472 di->expansion += 20;
1473 if (d_check_char (di, 'T'))
1475 switch (d_next_char (di))
1479 return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
1480 cplus_demangle_type (di), NULL);
1482 di->expansion -= 10;
1483 return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
1484 cplus_demangle_type (di), NULL);
1486 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
1487 cplus_demangle_type (di), NULL);
1489 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
1490 cplus_demangle_type (di), NULL);
1493 if (! d_call_offset (di, 'h'))
1495 return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
1496 d_encoding (di, 0), NULL);
1499 if (! d_call_offset (di, 'v'))
1501 return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
1502 d_encoding (di, 0), NULL);
1505 if (! d_call_offset (di, '\0'))
1507 if (! d_call_offset (di, '\0'))
1509 return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
1510 d_encoding (di, 0), NULL);
1514 struct demangle_component *derived_type;
1516 struct demangle_component *base_type;
1518 derived_type = cplus_demangle_type (di);
1519 offset = d_number (di);
1522 if (! d_check_char (di, '_'))
1524 base_type = cplus_demangle_type (di);
1525 /* We don't display the offset. FIXME: We should display
1526 it in verbose mode. */
1528 return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
1529 base_type, derived_type);
1533 return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
1534 cplus_demangle_type (di), NULL);
1536 return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
1537 cplus_demangle_type (di), NULL);
1543 else if (d_check_char (di, 'G'))
1545 switch (d_next_char (di))
1548 return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
1551 return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, d_name (di),
1555 return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
1556 d_encoding (di, 0), NULL);
1566 /* <call-offset> ::= h <nv-offset> _
1569 <nv-offset> ::= <(offset) number>
1571 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1573 The C parameter, if not '\0', is a character we just read which is
1574 the start of the <call-offset>.
1576 We don't display the offset information anywhere. FIXME: We should
1577 display it in verbose mode. */
1580 d_call_offset (struct d_info *di, int c)
1583 c = d_next_char (di);
1590 if (! d_check_char (di, '_'))
1597 if (! d_check_char (di, '_'))
1603 /* <ctor-dtor-name> ::= C1
1611 static struct demangle_component *
1612 d_ctor_dtor_name (struct d_info *di)
1614 if (di->last_name != NULL)
1616 if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
1617 di->expansion += di->last_name->u.s_name.len;
1618 else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
1619 di->expansion += di->last_name->u.s_string.len;
1621 switch (d_peek_char (di))
1625 enum gnu_v3_ctor_kinds kind;
1627 switch (d_peek_next_char (di))
1630 kind = gnu_v3_complete_object_ctor;
1633 kind = gnu_v3_base_object_ctor;
1636 kind = gnu_v3_complete_object_allocating_ctor;
1642 return d_make_ctor (di, kind, di->last_name);
1647 enum gnu_v3_dtor_kinds kind;
1649 switch (d_peek_next_char (di))
1652 kind = gnu_v3_deleting_dtor;
1655 kind = gnu_v3_complete_object_dtor;
1658 kind = gnu_v3_base_object_dtor;
1664 return d_make_dtor (di, kind, di->last_name);
1672 /* <type> ::= <builtin-type>
1674 ::= <class-enum-type>
1676 ::= <pointer-to-member-type>
1677 ::= <template-param>
1678 ::= <template-template-param> <template-args>
1680 ::= <CV-qualifiers> <type>
1685 ::= U <source-name> <type>
1687 <builtin-type> ::= various one letter codes
1691 CP_STATIC_IF_GLIBCPP_V3
1692 const struct demangle_builtin_type_info
1693 cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
1695 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
1696 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
1697 /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
1698 /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
1699 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
1700 /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
1701 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
1702 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
1703 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
1704 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
1705 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1706 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
1707 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
1708 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
1709 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
1711 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1712 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1713 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1714 /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
1715 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
1716 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1717 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
1718 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
1719 /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
1720 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
1721 D_PRINT_UNSIGNED_LONG_LONG },
1722 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
1725 CP_STATIC_IF_GLIBCPP_V3
1726 struct demangle_component *
1727 cplus_demangle_type (struct d_info *di)
1730 struct demangle_component *ret;
1733 /* The ABI specifies that when CV-qualifiers are used, the base type
1734 is substitutable, and the fully qualified type is substitutable,
1735 but the base type with a strict subset of the CV-qualifiers is
1736 not substitutable. The natural recursive implementation of the
1737 CV-qualifiers would cause subsets to be substitutable, so instead
1738 we pull them all off now.
1740 FIXME: The ABI says that order-insensitive vendor qualifiers
1741 should be handled in the same way, but we have no way to tell
1742 which vendor qualifiers are order-insensitive and which are
1743 order-sensitive. So we just assume that they are all
1744 order-sensitive. g++ 3.4 supports only one vendor qualifier,
1745 __vector, and it treats it as order-sensitive when mangling
1748 peek = d_peek_char (di);
1749 if (peek == 'r' || peek == 'V' || peek == 'K')
1751 struct demangle_component **pret;
1753 pret = d_cv_qualifiers (di, &ret, 0);
1756 *pret = cplus_demangle_type (di);
1757 if (! *pret || ! d_add_substitution (di, ret))
1766 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1767 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
1768 case 'o': case 's': case 't':
1769 case 'v': case 'w': case 'x': case 'y': case 'z':
1770 ret = d_make_builtin_type (di,
1771 &cplus_demangle_builtin_types[peek - 'a']);
1772 di->expansion += ret->u.s_builtin.type->len;
1779 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
1780 d_source_name (di), NULL);
1784 ret = d_function_type (di);
1787 case '0': case '1': case '2': case '3': case '4':
1788 case '5': case '6': case '7': case '8': case '9':
1791 ret = d_class_enum_type (di);
1795 ret = d_array_type (di);
1799 ret = d_pointer_to_member_type (di);
1803 ret = d_template_param (di);
1804 if (d_peek_char (di) == 'I')
1806 /* This is <template-template-param> <template-args>. The
1807 <template-template-param> part is a substitution
1809 if (! d_add_substitution (di, ret))
1811 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
1812 d_template_args (di));
1817 /* If this is a special substitution, then it is the start of
1818 <class-enum-type>. */
1822 peek_next = d_peek_next_char (di);
1823 if (IS_DIGIT (peek_next)
1825 || IS_UPPER (peek_next))
1827 ret = d_substitution (di, 0);
1828 /* The substituted name may have been a template name and
1829 may be followed by tepmlate args. */
1830 if (d_peek_char (di) == 'I')
1831 ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
1832 d_template_args (di));
1838 ret = d_class_enum_type (di);
1839 /* If the substitution was a complete type, then it is not
1840 a new substitution candidate. However, if the
1841 substitution was followed by template arguments, then
1842 the whole thing is a substitution candidate. */
1843 if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
1851 ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
1852 cplus_demangle_type (di), NULL);
1857 ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
1858 cplus_demangle_type (di), NULL);
1863 ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
1864 cplus_demangle_type (di), NULL);
1869 ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
1870 cplus_demangle_type (di), NULL);
1875 ret = d_source_name (di);
1876 ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
1877 cplus_demangle_type (di), ret);
1886 if (! d_add_substitution (di, ret))
1893 /* <CV-qualifiers> ::= [r] [V] [K] */
1895 static struct demangle_component **
1896 d_cv_qualifiers (struct d_info *di,
1897 struct demangle_component **pret, int member_fn)
1901 peek = d_peek_char (di);
1902 while (peek == 'r' || peek == 'V' || peek == 'K')
1904 enum demangle_component_type t;
1910 ? DEMANGLE_COMPONENT_RESTRICT_THIS
1911 : DEMANGLE_COMPONENT_RESTRICT);
1912 di->expansion += sizeof "restrict";
1914 else if (peek == 'V')
1917 ? DEMANGLE_COMPONENT_VOLATILE_THIS
1918 : DEMANGLE_COMPONENT_VOLATILE);
1919 di->expansion += sizeof "volatile";
1924 ? DEMANGLE_COMPONENT_CONST_THIS
1925 : DEMANGLE_COMPONENT_CONST);
1926 di->expansion += sizeof "const";
1929 *pret = d_make_comp (di, t, NULL, NULL);
1932 pret = &d_left (*pret);
1934 peek = d_peek_char (di);
1940 /* <function-type> ::= F [Y] <bare-function-type> E */
1942 static struct demangle_component *
1943 d_function_type (struct d_info *di)
1945 struct demangle_component *ret;
1947 if (! d_check_char (di, 'F'))
1949 if (d_peek_char (di) == 'Y')
1951 /* Function has C linkage. We don't print this information.
1952 FIXME: We should print it in verbose mode. */
1955 ret = d_bare_function_type (di, 1);
1956 if (! d_check_char (di, 'E'))
1961 /* <bare-function-type> ::= [J]<type>+ */
1963 static struct demangle_component *
1964 d_bare_function_type (struct d_info *di, int has_return_type)
1966 struct demangle_component *return_type;
1967 struct demangle_component *tl;
1968 struct demangle_component **ptl;
1971 /* Detect special qualifier indicating that the first argument
1972 is the return type. */
1973 peek = d_peek_char (di);
1977 has_return_type = 1;
1985 struct demangle_component *type;
1987 peek = d_peek_char (di);
1988 if (peek == '\0' || peek == 'E')
1990 type = cplus_demangle_type (di);
1993 if (has_return_type)
1996 has_return_type = 0;
2000 *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
2003 ptl = &d_right (*ptl);
2007 /* There should be at least one parameter type besides the optional
2008 return type. A function which takes no arguments will have a
2009 single parameter type void. */
2013 /* If we have a single parameter type void, omit it. */
2014 if (d_right (tl) == NULL
2015 && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2016 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
2018 di->expansion -= d_left (tl)->u.s_builtin.type->len;
2022 return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE, return_type, tl);
2025 /* <class-enum-type> ::= <name> */
2027 static struct demangle_component *
2028 d_class_enum_type (struct d_info *di)
2033 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2034 ::= A [<(dimension) expression>] _ <(element) type>
2037 static struct demangle_component *
2038 d_array_type (struct d_info *di)
2041 struct demangle_component *dim;
2043 if (! d_check_char (di, 'A'))
2046 peek = d_peek_char (di);
2049 else if (IS_DIGIT (peek))
2057 peek = d_peek_char (di);
2059 while (IS_DIGIT (peek));
2060 dim = d_make_name (di, s, d_str (di) - s);
2066 dim = d_expression (di);
2071 if (! d_check_char (di, '_'))
2074 return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
2075 cplus_demangle_type (di));
2078 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2080 static struct demangle_component *
2081 d_pointer_to_member_type (struct d_info *di)
2083 struct demangle_component *cl;
2084 struct demangle_component *mem;
2085 struct demangle_component **pmem;
2087 if (! d_check_char (di, 'M'))
2090 cl = cplus_demangle_type (di);
2092 /* The ABI specifies that any type can be a substitution source, and
2093 that M is followed by two types, and that when a CV-qualified
2094 type is seen both the base type and the CV-qualified types are
2095 substitution sources. The ABI also specifies that for a pointer
2096 to a CV-qualified member function, the qualifiers are attached to
2097 the second type. Given the grammar, a plain reading of the ABI
2098 suggests that both the CV-qualified member function and the
2099 non-qualified member function are substitution sources. However,
2100 g++ does not work that way. g++ treats only the CV-qualified
2101 member function as a substitution source. FIXME. So to work
2102 with g++, we need to pull off the CV-qualifiers here, in order to
2103 avoid calling add_substitution() in cplus_demangle_type(). But
2104 for a CV-qualified member which is not a function, g++ does
2105 follow the ABI, so we need to handle that case here by calling
2106 d_add_substitution ourselves. */
2108 pmem = d_cv_qualifiers (di, &mem, 1);
2111 *pmem = cplus_demangle_type (di);
2115 if (pmem != &mem && (*pmem)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
2117 if (! d_add_substitution (di, mem))
2121 return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
2124 /* <template-param> ::= T_
2125 ::= T <(parameter-2 non-negative) number> _
2128 static struct demangle_component *
2129 d_template_param (struct d_info *di)
2133 if (! d_check_char (di, 'T'))
2136 if (d_peek_char (di) == '_')
2140 param = d_number (di);
2146 if (! d_check_char (di, '_'))
2151 return d_make_template_param (di, param);
2154 /* <template-args> ::= I <template-arg>+ E */
2156 static struct demangle_component *
2157 d_template_args (struct d_info *di)
2159 struct demangle_component *hold_last_name;
2160 struct demangle_component *al;
2161 struct demangle_component **pal;
2163 /* Preserve the last name we saw--don't let the template arguments
2164 clobber it, as that would give us the wrong name for a subsequent
2165 constructor or destructor. */
2166 hold_last_name = di->last_name;
2168 if (! d_check_char (di, 'I'))
2175 struct demangle_component *a;
2177 a = d_template_arg (di);
2181 *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
2184 pal = &d_right (*pal);
2186 if (d_peek_char (di) == 'E')
2193 di->last_name = hold_last_name;
2198 /* <template-arg> ::= <type>
2199 ::= X <expression> E
2203 static struct demangle_component *
2204 d_template_arg (struct d_info *di)
2206 struct demangle_component *ret;
2208 switch (d_peek_char (di))
2212 ret = d_expression (di);
2213 if (! d_check_char (di, 'E'))
2218 return d_expr_primary (di);
2221 return cplus_demangle_type (di);
2225 /* <expression> ::= <(unary) operator-name> <expression>
2226 ::= <(binary) operator-name> <expression> <expression>
2227 ::= <(trinary) operator-name> <expression> <expression> <expression>
2229 ::= <template-param>
2230 ::= sr <type> <unqualified-name>
2231 ::= sr <type> <unqualified-name> <template-args>
2235 static struct demangle_component *
2236 d_expression (struct d_info *di)
2240 peek = d_peek_char (di);
2242 return d_expr_primary (di);
2243 else if (peek == 'T')
2244 return d_template_param (di);
2245 else if (peek == 's' && d_peek_next_char (di) == 'r')
2247 struct demangle_component *type;
2248 struct demangle_component *name;
2251 type = cplus_demangle_type (di);
2252 name = d_unqualified_name (di);
2253 if (d_peek_char (di) != 'I')
2254 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
2256 return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
2257 d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
2258 d_template_args (di)));
2262 struct demangle_component *op;
2265 op = d_operator_name (di);
2269 if (op->type == DEMANGLE_COMPONENT_OPERATOR)
2270 di->expansion += op->u.s_operator.op->len - 2;
2272 if (op->type == DEMANGLE_COMPONENT_OPERATOR
2273 && strcmp (op->u.s_operator.op->code, "st") == 0)
2274 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2275 cplus_demangle_type (di));
2281 case DEMANGLE_COMPONENT_OPERATOR:
2282 args = op->u.s_operator.op->args;
2284 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
2285 args = op->u.s_extended_operator.args;
2287 case DEMANGLE_COMPONENT_CAST:
2295 return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
2299 struct demangle_component *left;
2301 left = d_expression (di);
2302 return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
2304 DEMANGLE_COMPONENT_BINARY_ARGS,
2306 d_expression (di)));
2310 struct demangle_component *first;
2311 struct demangle_component *second;
2313 first = d_expression (di);
2314 second = d_expression (di);
2315 return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
2317 DEMANGLE_COMPONENT_TRINARY_ARG1,
2320 DEMANGLE_COMPONENT_TRINARY_ARG2,
2322 d_expression (di))));
2330 /* <expr-primary> ::= L <type> <(value) number> E
2331 ::= L <type> <(value) float> E
2332 ::= L <mangled-name> E
2335 static struct demangle_component *
2336 d_expr_primary (struct d_info *di)
2338 struct demangle_component *ret;
2340 if (! d_check_char (di, 'L'))
2342 if (d_peek_char (di) == '_')
2343 ret = cplus_demangle_mangled_name (di, 0);
2346 struct demangle_component *type;
2347 enum demangle_component_type t;
2350 type = cplus_demangle_type (di);
2354 /* If we have a type we know how to print, we aren't going to
2355 print the type name itself. */
2356 if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
2357 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
2358 di->expansion -= type->u.s_builtin.type->len;
2360 /* Rather than try to interpret the literal value, we just
2361 collect it as a string. Note that it's possible to have a
2362 floating point literal here. The ABI specifies that the
2363 format of such literals is machine independent. That's fine,
2364 but what's not fine is that versions of g++ up to 3.2 with
2365 -fabi-version=1 used upper case letters in the hex constant,
2366 and dumped out gcc's internal representation. That makes it
2367 hard to tell where the constant ends, and hard to dump the
2368 constant in any readable form anyhow. We don't attempt to
2369 handle these cases. */
2371 t = DEMANGLE_COMPONENT_LITERAL;
2372 if (d_peek_char (di) == 'n')
2374 t = DEMANGLE_COMPONENT_LITERAL_NEG;
2378 while (d_peek_char (di) != 'E')
2380 if (d_peek_char (di) == '\0')
2384 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
2386 if (! d_check_char (di, 'E'))
2391 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2392 ::= Z <(function) encoding> E s [<discriminator>]
2395 static struct demangle_component *
2396 d_local_name (struct d_info *di)
2398 struct demangle_component *function;
2400 if (! d_check_char (di, 'Z'))
2403 function = d_encoding (di, 0);
2405 if (! d_check_char (di, 'E'))
2408 if (d_peek_char (di) == 's')
2411 if (! d_discriminator (di))
2413 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
2414 d_make_name (di, "string literal",
2415 sizeof "string literal" - 1));
2419 struct demangle_component *name;
2422 if (! d_discriminator (di))
2424 return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
2428 /* <discriminator> ::= _ <(non-negative) number>
2430 We demangle the discriminator, but we don't print it out. FIXME:
2431 We should print it out in verbose mode. */
2434 d_discriminator (struct d_info *di)
2438 if (d_peek_char (di) != '_')
2441 discrim = d_number (di);
2447 /* Add a new substitution. */
2450 d_add_substitution (struct d_info *di, struct demangle_component *dc)
2454 if (di->next_sub >= di->num_subs)
2456 di->subs[di->next_sub] = dc;
2461 /* <substitution> ::= S <seq-id> _
2471 If PREFIX is non-zero, then this type is being used as a prefix in
2472 a qualified name. In this case, for the standard substitutions, we
2473 need to check whether we are being used as a prefix for a
2474 constructor or destructor, and return a full template name.
2475 Otherwise we will get something like std::iostream::~iostream()
2476 which does not correspond particularly well to any function which
2477 actually appears in the source.
2480 static const struct d_standard_sub_info standard_subs[] =
2485 { 'a', NL ("std::allocator"),
2486 NL ("std::allocator"),
2488 { 'b', NL ("std::basic_string"),
2489 NL ("std::basic_string"),
2490 NL ("basic_string") },
2491 { 's', NL ("std::string"),
2492 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
2493 NL ("basic_string") },
2494 { 'i', NL ("std::istream"),
2495 NL ("std::basic_istream<char, std::char_traits<char> >"),
2496 NL ("basic_istream") },
2497 { 'o', NL ("std::ostream"),
2498 NL ("std::basic_ostream<char, std::char_traits<char> >"),
2499 NL ("basic_ostream") },
2500 { 'd', NL ("std::iostream"),
2501 NL ("std::basic_iostream<char, std::char_traits<char> >"),
2502 NL ("basic_iostream") }
2505 static struct demangle_component *
2506 d_substitution (struct d_info *di, int prefix)
2510 if (! d_check_char (di, 'S'))
2513 c = d_next_char (di);
2514 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
2524 id = id * 36 + c - '0';
2525 else if (IS_UPPER (c))
2526 id = id * 36 + c - 'A' + 10;
2531 c = d_next_char (di);
2538 if (id >= di->next_sub)
2543 return di->subs[id];
2548 const struct d_standard_sub_info *p;
2549 const struct d_standard_sub_info *pend;
2551 verbose = (di->options & DMGL_VERBOSE) != 0;
2552 if (! verbose && prefix)
2556 peek = d_peek_char (di);
2557 if (peek == 'C' || peek == 'D')
2561 pend = (&standard_subs[0]
2562 + sizeof standard_subs / sizeof standard_subs[0]);
2563 for (p = &standard_subs[0]; p < pend; ++p)
2570 if (p->set_last_name != NULL)
2571 di->last_name = d_make_sub (di, p->set_last_name,
2572 p->set_last_name_len);
2575 s = p->full_expansion;
2580 s = p->simple_expansion;
2581 len = p->simple_len;
2583 di->expansion += len;
2584 return d_make_sub (di, s, len);
2592 /* Resize the print buffer. */
2595 d_print_resize (struct d_print_info *dpi, size_t add)
2599 if (dpi->buf == NULL)
2601 need = dpi->len + add;
2602 while (need > dpi->alc)
2607 newalc = dpi->alc * 2;
2608 newbuf = (char *) realloc (dpi->buf, newalc);
2613 dpi->allocation_failure = 1;
2621 /* Append a character to the print buffer. */
2624 d_print_append_char (struct d_print_info *dpi, int c)
2626 if (dpi->buf != NULL)
2628 if (dpi->len >= dpi->alc)
2630 d_print_resize (dpi, 1);
2631 if (dpi->buf == NULL)
2635 dpi->buf[dpi->len] = c;
2640 /* Append a buffer to the print buffer. */
2643 d_print_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
2645 if (dpi->buf != NULL)
2647 if (dpi->len + l > dpi->alc)
2649 d_print_resize (dpi, l);
2650 if (dpi->buf == NULL)
2654 memcpy (dpi->buf + dpi->len, s, l);
2659 /* Indicate that an error occurred during printing. */
2662 d_print_error (struct d_print_info *dpi)
2668 /* Turn components into a human readable string. OPTIONS is the
2669 options bits passed to the demangler. DC is the tree to print.
2670 ESTIMATE is a guess at the length of the result. This returns a
2671 string allocated by malloc, or NULL on error. On success, this
2672 sets *PALC to the size of the allocated buffer. On failure, this
2673 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
2676 CP_STATIC_IF_GLIBCPP_V3
2678 cplus_demangle_print (int options, const struct demangle_component *dc,
2679 int estimate, size_t *palc)
2681 struct d_print_info dpi;
2683 dpi.options = options;
2685 dpi.alc = estimate + 1;
2686 dpi.buf = (char *) malloc (dpi.alc);
2687 if (dpi.buf == NULL)
2694 dpi.templates = NULL;
2695 dpi.modifiers = NULL;
2697 dpi.allocation_failure = 0;
2699 d_print_comp (&dpi, dc);
2701 d_append_char (&dpi, '\0');
2703 if (dpi.buf != NULL)
2706 *palc = dpi.allocation_failure;
2711 /* Subroutine to handle components. */
2714 d_print_comp (struct d_print_info *dpi,
2715 const struct demangle_component *dc)
2719 d_print_error (dpi);
2722 if (d_print_saw_error (dpi))
2727 case DEMANGLE_COMPONENT_NAME:
2728 if ((dpi->options & DMGL_JAVA) == 0)
2729 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
2731 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
2734 case DEMANGLE_COMPONENT_QUAL_NAME:
2735 case DEMANGLE_COMPONENT_LOCAL_NAME:
2736 d_print_comp (dpi, d_left (dc));
2737 if ((dpi->options & DMGL_JAVA) == 0)
2738 d_append_string_constant (dpi, "::");
2740 d_append_char (dpi, '.');
2741 d_print_comp (dpi, d_right (dc));
2744 case DEMANGLE_COMPONENT_TYPED_NAME:
2746 struct d_print_mod *hold_modifiers;
2747 struct demangle_component *typed_name;
2748 struct d_print_mod adpm[4];
2750 struct d_print_template dpt;
2752 /* Pass the name down to the type so that it can be printed in
2753 the right place for the type. We also have to pass down
2754 any CV-qualifiers, which apply to the this parameter. */
2755 hold_modifiers = dpi->modifiers;
2757 typed_name = d_left (dc);
2758 while (typed_name != NULL)
2760 if (i >= sizeof adpm / sizeof adpm[0])
2762 d_print_error (dpi);
2766 adpm[i].next = dpi->modifiers;
2767 dpi->modifiers = &adpm[i];
2768 adpm[i].mod = typed_name;
2769 adpm[i].printed = 0;
2770 adpm[i].templates = dpi->templates;
2773 if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
2774 && typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
2775 && typed_name->type != DEMANGLE_COMPONENT_CONST_THIS)
2778 typed_name = d_left (typed_name);
2781 /* If typed_name is a template, then it applies to the
2782 function type as well. */
2783 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
2785 dpt.next = dpi->templates;
2786 dpi->templates = &dpt;
2787 dpt.template_decl = typed_name;
2790 /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
2791 there may be CV-qualifiers on its right argument which
2792 really apply here; this happens when parsing a class which
2793 is local to a function. */
2794 if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
2796 struct demangle_component *local_name;
2798 local_name = d_right (typed_name);
2799 while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
2800 || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
2801 || local_name->type == DEMANGLE_COMPONENT_CONST_THIS)
2803 if (i >= sizeof adpm / sizeof adpm[0])
2805 d_print_error (dpi);
2809 adpm[i] = adpm[i - 1];
2810 adpm[i].next = &adpm[i - 1];
2811 dpi->modifiers = &adpm[i];
2813 adpm[i - 1].mod = local_name;
2814 adpm[i - 1].printed = 0;
2815 adpm[i - 1].templates = dpi->templates;
2818 local_name = d_left (local_name);
2822 d_print_comp (dpi, d_right (dc));
2824 if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
2825 dpi->templates = dpt.next;
2827 /* If the modifiers didn't get printed by the type, print them
2832 if (! adpm[i].printed)
2834 d_append_char (dpi, ' ');
2835 d_print_mod (dpi, adpm[i].mod);
2839 dpi->modifiers = hold_modifiers;
2844 case DEMANGLE_COMPONENT_TEMPLATE:
2846 struct d_print_mod *hold_dpm;
2848 /* Don't push modifiers into a template definition. Doing so
2849 could give the wrong definition for a template argument.
2850 Instead, treat the template essentially as a name. */
2852 hold_dpm = dpi->modifiers;
2853 dpi->modifiers = NULL;
2855 d_print_comp (dpi, d_left (dc));
2856 if (d_last_char (dpi) == '<')
2857 d_append_char (dpi, ' ');
2858 d_append_char (dpi, '<');
2859 d_print_comp (dpi, d_right (dc));
2860 /* Avoid generating two consecutive '>' characters, to avoid
2861 the C++ syntactic ambiguity. */
2862 if (d_last_char (dpi) == '>')
2863 d_append_char (dpi, ' ');
2864 d_append_char (dpi, '>');
2866 dpi->modifiers = hold_dpm;
2871 case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
2874 struct demangle_component *a;
2875 struct d_print_template *hold_dpt;
2877 if (dpi->templates == NULL)
2879 d_print_error (dpi);
2882 i = dc->u.s_number.number;
2883 for (a = d_right (dpi->templates->template_decl);
2887 if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
2889 d_print_error (dpi);
2896 if (i != 0 || a == NULL)
2898 d_print_error (dpi);
2902 /* While processing this parameter, we need to pop the list of
2903 templates. This is because the template parameter may
2904 itself be a reference to a parameter of an outer
2907 hold_dpt = dpi->templates;
2908 dpi->templates = hold_dpt->next;
2910 d_print_comp (dpi, d_left (a));
2912 dpi->templates = hold_dpt;
2917 case DEMANGLE_COMPONENT_CTOR:
2918 d_print_comp (dpi, dc->u.s_ctor.name);
2921 case DEMANGLE_COMPONENT_DTOR:
2922 d_append_char (dpi, '~');
2923 d_print_comp (dpi, dc->u.s_dtor.name);
2926 case DEMANGLE_COMPONENT_VTABLE:
2927 d_append_string_constant (dpi, "vtable for ");
2928 d_print_comp (dpi, d_left (dc));
2931 case DEMANGLE_COMPONENT_VTT:
2932 d_append_string_constant (dpi, "VTT for ");
2933 d_print_comp (dpi, d_left (dc));
2936 case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
2937 d_append_string_constant (dpi, "construction vtable for ");
2938 d_print_comp (dpi, d_left (dc));
2939 d_append_string_constant (dpi, "-in-");
2940 d_print_comp (dpi, d_right (dc));
2943 case DEMANGLE_COMPONENT_TYPEINFO:
2944 d_append_string_constant (dpi, "typeinfo for ");
2945 d_print_comp (dpi, d_left (dc));
2948 case DEMANGLE_COMPONENT_TYPEINFO_NAME:
2949 d_append_string_constant (dpi, "typeinfo name for ");
2950 d_print_comp (dpi, d_left (dc));
2953 case DEMANGLE_COMPONENT_TYPEINFO_FN:
2954 d_append_string_constant (dpi, "typeinfo fn for ");
2955 d_print_comp (dpi, d_left (dc));
2958 case DEMANGLE_COMPONENT_THUNK:
2959 d_append_string_constant (dpi, "non-virtual thunk to ");
2960 d_print_comp (dpi, d_left (dc));
2963 case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
2964 d_append_string_constant (dpi, "virtual thunk to ");
2965 d_print_comp (dpi, d_left (dc));
2968 case DEMANGLE_COMPONENT_COVARIANT_THUNK:
2969 d_append_string_constant (dpi, "covariant return thunk to ");
2970 d_print_comp (dpi, d_left (dc));
2973 case DEMANGLE_COMPONENT_JAVA_CLASS:
2974 d_append_string_constant (dpi, "java Class for ");
2975 d_print_comp (dpi, d_left (dc));
2978 case DEMANGLE_COMPONENT_GUARD:
2979 d_append_string_constant (dpi, "guard variable for ");
2980 d_print_comp (dpi, d_left (dc));
2983 case DEMANGLE_COMPONENT_REFTEMP:
2984 d_append_string_constant (dpi, "reference temporary for ");
2985 d_print_comp (dpi, d_left (dc));
2988 case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
2989 d_append_string_constant (dpi, "hidden alias for ");
2990 d_print_comp (dpi, d_left (dc));
2993 case DEMANGLE_COMPONENT_SUB_STD:
2994 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
2997 case DEMANGLE_COMPONENT_RESTRICT:
2998 case DEMANGLE_COMPONENT_VOLATILE:
2999 case DEMANGLE_COMPONENT_CONST:
3001 struct d_print_mod *pdpm;
3003 /* When printing arrays, it's possible to have cases where the
3004 same CV-qualifier gets pushed on the stack multiple times.
3005 We only need to print it once. */
3007 for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
3009 if (! pdpm->printed)
3011 if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
3012 && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
3013 && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
3015 if (pdpm->mod->type == dc->type)
3017 d_print_comp (dpi, d_left (dc));
3024 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3025 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3026 case DEMANGLE_COMPONENT_CONST_THIS:
3027 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3028 case DEMANGLE_COMPONENT_POINTER:
3029 case DEMANGLE_COMPONENT_REFERENCE:
3030 case DEMANGLE_COMPONENT_COMPLEX:
3031 case DEMANGLE_COMPONENT_IMAGINARY:
3033 /* We keep a list of modifiers on the stack. */
3034 struct d_print_mod dpm;
3036 dpm.next = dpi->modifiers;
3037 dpi->modifiers = &dpm;
3040 dpm.templates = dpi->templates;
3042 d_print_comp (dpi, d_left (dc));
3044 /* If the modifier didn't get printed by the type, print it
3047 d_print_mod (dpi, dc);
3049 dpi->modifiers = dpm.next;
3054 case DEMANGLE_COMPONENT_BUILTIN_TYPE:
3055 if ((dpi->options & DMGL_JAVA) == 0)
3056 d_append_buffer (dpi, dc->u.s_builtin.type->name,
3057 dc->u.s_builtin.type->len);
3059 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
3060 dc->u.s_builtin.type->java_len);
3063 case DEMANGLE_COMPONENT_VENDOR_TYPE:
3064 d_print_comp (dpi, d_left (dc));
3067 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
3069 if ((dpi->options & DMGL_RET_POSTFIX) != 0)
3070 d_print_function_type (dpi, dc, dpi->modifiers);
3072 /* Print return type if present */
3073 if (d_left (dc) != NULL)
3075 struct d_print_mod dpm;
3077 /* We must pass this type down as a modifier in order to
3078 print it in the right location. */
3079 dpm.next = dpi->modifiers;
3080 dpi->modifiers = &dpm;
3083 dpm.templates = dpi->templates;
3085 d_print_comp (dpi, d_left (dc));
3087 dpi->modifiers = dpm.next;
3092 /* In standard prefix notation, there is a space between the
3093 return type and the function signature. */
3094 if ((dpi->options & DMGL_RET_POSTFIX) == 0)
3095 d_append_char (dpi, ' ');
3098 if ((dpi->options & DMGL_RET_POSTFIX) == 0)
3099 d_print_function_type (dpi, dc, dpi->modifiers);
3104 case DEMANGLE_COMPONENT_ARRAY_TYPE:
3106 struct d_print_mod *hold_modifiers;
3107 struct d_print_mod adpm[4];
3109 struct d_print_mod *pdpm;
3111 /* We must pass this type down as a modifier in order to print
3112 multi-dimensional arrays correctly. If the array itself is
3113 CV-qualified, we act as though the element type were
3114 CV-qualified. We do this by copying the modifiers down
3115 rather than fiddling pointers, so that we don't wind up
3116 with a d_print_mod higher on the stack pointing into our
3117 stack frame after we return. */
3119 hold_modifiers = dpi->modifiers;
3121 adpm[0].next = hold_modifiers;
3122 dpi->modifiers = &adpm[0];
3124 adpm[0].printed = 0;
3125 adpm[0].templates = dpi->templates;
3128 pdpm = hold_modifiers;
3130 && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
3131 || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
3132 || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
3134 if (! pdpm->printed)
3136 if (i >= sizeof adpm / sizeof adpm[0])
3138 d_print_error (dpi);
3143 adpm[i].next = dpi->modifiers;
3144 dpi->modifiers = &adpm[i];
3152 d_print_comp (dpi, d_right (dc));
3154 dpi->modifiers = hold_modifiers;
3156 if (adpm[0].printed)
3162 d_print_mod (dpi, adpm[i].mod);
3165 d_print_array_type (dpi, dc, dpi->modifiers);
3170 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3172 struct d_print_mod dpm;
3174 dpm.next = dpi->modifiers;
3175 dpi->modifiers = &dpm;
3178 dpm.templates = dpi->templates;
3180 d_print_comp (dpi, d_right (dc));
3182 /* If the modifier didn't get printed by the type, print it
3186 d_append_char (dpi, ' ');
3187 d_print_comp (dpi, d_left (dc));
3188 d_append_string_constant (dpi, "::*");
3191 dpi->modifiers = dpm.next;
3196 case DEMANGLE_COMPONENT_ARGLIST:
3197 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
3198 d_print_comp (dpi, d_left (dc));
3199 if (d_right (dc) != NULL)
3201 d_append_string_constant (dpi, ", ");
3202 d_print_comp (dpi, d_right (dc));
3206 case DEMANGLE_COMPONENT_OPERATOR:
3210 d_append_string_constant (dpi, "operator");
3211 c = dc->u.s_operator.op->name[0];
3213 d_append_char (dpi, ' ');
3214 d_append_buffer (dpi, dc->u.s_operator.op->name,
3215 dc->u.s_operator.op->len);
3219 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
3220 d_append_string_constant (dpi, "operator ");
3221 d_print_comp (dpi, dc->u.s_extended_operator.name);
3224 case DEMANGLE_COMPONENT_CAST:
3225 d_append_string_constant (dpi, "operator ");
3226 d_print_cast (dpi, dc);
3229 case DEMANGLE_COMPONENT_UNARY:
3230 if (d_left (dc)->type != DEMANGLE_COMPONENT_CAST)
3231 d_print_expr_op (dpi, d_left (dc));
3234 d_append_char (dpi, '(');
3235 d_print_cast (dpi, d_left (dc));
3236 d_append_char (dpi, ')');
3238 d_append_char (dpi, '(');
3239 d_print_comp (dpi, d_right (dc));
3240 d_append_char (dpi, ')');
3243 case DEMANGLE_COMPONENT_BINARY:
3244 if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
3246 d_print_error (dpi);
3250 /* We wrap an expression which uses the greater-than operator in
3251 an extra layer of parens so that it does not get confused
3252 with the '>' which ends the template parameters. */
3253 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
3254 && d_left (dc)->u.s_operator.op->len == 1
3255 && d_left (dc)->u.s_operator.op->name[0] == '>')
3256 d_append_char (dpi, '(');
3258 d_append_char (dpi, '(');
3259 d_print_comp (dpi, d_left (d_right (dc)));
3260 d_append_string_constant (dpi, ") ");
3261 d_print_expr_op (dpi, d_left (dc));
3262 d_append_string_constant (dpi, " (");
3263 d_print_comp (dpi, d_right (d_right (dc)));
3264 d_append_char (dpi, ')');
3266 if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
3267 && d_left (dc)->u.s_operator.op->len == 1
3268 && d_left (dc)->u.s_operator.op->name[0] == '>')
3269 d_append_char (dpi, ')');
3273 case DEMANGLE_COMPONENT_BINARY_ARGS:
3274 /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
3275 d_print_error (dpi);
3278 case DEMANGLE_COMPONENT_TRINARY:
3279 if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
3280 || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
3282 d_print_error (dpi);
3285 d_append_char (dpi, '(');
3286 d_print_comp (dpi, d_left (d_right (dc)));
3287 d_append_string_constant (dpi, ") ");
3288 d_print_expr_op (dpi, d_left (dc));
3289 d_append_string_constant (dpi, " (");
3290 d_print_comp (dpi, d_left (d_right (d_right (dc))));
3291 d_append_string_constant (dpi, ") : (");
3292 d_print_comp (dpi, d_right (d_right (d_right (dc))));
3293 d_append_char (dpi, ')');
3296 case DEMANGLE_COMPONENT_TRINARY_ARG1:
3297 case DEMANGLE_COMPONENT_TRINARY_ARG2:
3298 /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
3299 d_print_error (dpi);
3302 case DEMANGLE_COMPONENT_LITERAL:
3303 case DEMANGLE_COMPONENT_LITERAL_NEG:
3305 enum d_builtin_type_print tp;
3307 /* For some builtin types, produce simpler output. */
3308 tp = D_PRINT_DEFAULT;
3309 if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
3311 tp = d_left (dc)->u.s_builtin.type->print;
3315 case D_PRINT_UNSIGNED:
3317 case D_PRINT_UNSIGNED_LONG:
3318 case D_PRINT_LONG_LONG:
3319 case D_PRINT_UNSIGNED_LONG_LONG:
3320 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
3322 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
3323 d_append_char (dpi, '-');
3324 d_print_comp (dpi, d_right (dc));
3329 case D_PRINT_UNSIGNED:
3330 d_append_char (dpi, 'u');
3333 d_append_char (dpi, 'l');
3335 case D_PRINT_UNSIGNED_LONG:
3336 d_append_string_constant (dpi, "ul");
3338 case D_PRINT_LONG_LONG:
3339 d_append_string_constant (dpi, "ll");
3341 case D_PRINT_UNSIGNED_LONG_LONG:
3342 d_append_string_constant (dpi, "ull");
3350 if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
3351 && d_right (dc)->u.s_name.len == 1
3352 && dc->type == DEMANGLE_COMPONENT_LITERAL)
3354 switch (d_right (dc)->u.s_name.s[0])
3357 d_append_string_constant (dpi, "false");
3360 d_append_string_constant (dpi, "true");
3373 d_append_char (dpi, '(');
3374 d_print_comp (dpi, d_left (dc));
3375 d_append_char (dpi, ')');
3376 if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
3377 d_append_char (dpi, '-');
3378 if (tp == D_PRINT_FLOAT)
3379 d_append_char (dpi, '[');
3380 d_print_comp (dpi, d_right (dc));
3381 if (tp == D_PRINT_FLOAT)
3382 d_append_char (dpi, ']');
3387 d_print_error (dpi);
3392 /* Print a Java dentifier. For Java we try to handle encoded extended
3393 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
3394 so we don't it for C++. Characters are encoded as
3398 d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
3404 for (p = name; p < end; ++p)
3415 for (q = p + 3; q < end; ++q)
3421 else if (*q >= 'A' && *q <= 'F')
3422 dig = *q - 'A' + 10;
3423 else if (*q >= 'a' && *q <= 'f')
3424 dig = *q - 'a' + 10;
3430 /* If the Unicode character is larger than 256, we don't try
3431 to deal with it here. FIXME. */
3432 if (q < end && *q == '_' && c < 256)
3434 d_append_char (dpi, c);
3440 d_append_char (dpi, *p);
3444 /* Print a list of modifiers. SUFFIX is 1 if we are printing
3445 qualifiers on this after printing a function. */
3448 d_print_mod_list (struct d_print_info *dpi,
3449 struct d_print_mod *mods, int suffix)
3451 struct d_print_template *hold_dpt;
3453 if (mods == NULL || d_print_saw_error (dpi))
3458 && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3459 || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3460 || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS)))
3462 d_print_mod_list (dpi, mods->next, suffix);
3468 hold_dpt = dpi->templates;
3469 dpi->templates = mods->templates;
3471 if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
3473 d_print_function_type (dpi, mods->mod, mods->next);
3474 dpi->templates = hold_dpt;
3477 else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
3479 d_print_array_type (dpi, mods->mod, mods->next);
3480 dpi->templates = hold_dpt;
3483 else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
3485 struct d_print_mod *hold_modifiers;
3486 struct demangle_component *dc;
3488 /* When this is on the modifier stack, we have pulled any
3489 qualifiers off the right argument already. Otherwise, we
3490 print it as usual, but don't let the left argument see any
3493 hold_modifiers = dpi->modifiers;
3494 dpi->modifiers = NULL;
3495 d_print_comp (dpi, d_left (mods->mod));
3496 dpi->modifiers = hold_modifiers;
3498 if ((dpi->options & DMGL_JAVA) == 0)
3499 d_append_string_constant (dpi, "::");
3501 d_append_char (dpi, '.');
3503 dc = d_right (mods->mod);
3504 while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
3505 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
3506 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
3509 d_print_comp (dpi, dc);
3511 dpi->templates = hold_dpt;
3515 d_print_mod (dpi, mods->mod);
3517 dpi->templates = hold_dpt;
3519 d_print_mod_list (dpi, mods->next, suffix);
3522 /* Print a modifier. */
3525 d_print_mod (struct d_print_info *dpi,
3526 const struct demangle_component *mod)
3530 case DEMANGLE_COMPONENT_RESTRICT:
3531 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3532 d_append_string_constant (dpi, " restrict");
3534 case DEMANGLE_COMPONENT_VOLATILE:
3535 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3536 d_append_string_constant (dpi, " volatile");
3538 case DEMANGLE_COMPONENT_CONST:
3539 case DEMANGLE_COMPONENT_CONST_THIS:
3540 d_append_string_constant (dpi, " const");
3542 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3543 d_append_char (dpi, ' ');
3544 d_print_comp (dpi, d_right (mod));
3546 case DEMANGLE_COMPONENT_POINTER:
3547 /* There is no pointer symbol in Java. */
3548 if ((dpi->options & DMGL_JAVA) == 0)
3549 d_append_char (dpi, '*');
3551 case DEMANGLE_COMPONENT_REFERENCE:
3552 d_append_char (dpi, '&');
3554 case DEMANGLE_COMPONENT_COMPLEX:
3555 d_append_string_constant (dpi, "complex ");
3557 case DEMANGLE_COMPONENT_IMAGINARY:
3558 d_append_string_constant (dpi, "imaginary ");
3560 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3561 if (d_last_char (dpi) != '(')
3562 d_append_char (dpi, ' ');
3563 d_print_comp (dpi, d_left (mod));
3564 d_append_string_constant (dpi, "::*");
3566 case DEMANGLE_COMPONENT_TYPED_NAME:
3567 d_print_comp (dpi, d_left (mod));
3570 /* Otherwise, we have something that won't go back on the
3571 modifier stack, so we can just print it. */
3572 d_print_comp (dpi, mod);
3577 /* Print a function type, except for the return type. */
3580 d_print_function_type (struct d_print_info *dpi,
3581 const struct demangle_component *dc,
3582 struct d_print_mod *mods)
3587 struct d_print_mod *p;
3588 struct d_print_mod *hold_modifiers;
3593 for (p = mods; p != NULL; p = p->next)
3599 switch (p->mod->type)
3601 case DEMANGLE_COMPONENT_POINTER:
3602 case DEMANGLE_COMPONENT_REFERENCE:
3605 case DEMANGLE_COMPONENT_RESTRICT:
3606 case DEMANGLE_COMPONENT_VOLATILE:
3607 case DEMANGLE_COMPONENT_CONST:
3608 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
3609 case DEMANGLE_COMPONENT_COMPLEX:
3610 case DEMANGLE_COMPONENT_IMAGINARY:
3611 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
3615 case DEMANGLE_COMPONENT_RESTRICT_THIS:
3616 case DEMANGLE_COMPONENT_VOLATILE_THIS:
3617 case DEMANGLE_COMPONENT_CONST_THIS:
3626 if (d_left (dc) != NULL && ! saw_mod)
3633 if (d_last_char (dpi) != '('
3634 && d_last_char (dpi) != '*')
3637 if (need_space && d_last_char (dpi) != ' ')
3638 d_append_char (dpi, ' ');
3639 d_append_char (dpi, '(');
3642 hold_modifiers = dpi->modifiers;
3643 dpi->modifiers = NULL;
3645 d_print_mod_list (dpi, mods, 0);
3648 d_append_char (dpi, ')');
3650 d_append_char (dpi, '(');
3652 if (d_right (dc) != NULL)
3653 d_print_comp (dpi, d_right (dc));
3655 d_append_char (dpi, ')');
3657 d_print_mod_list (dpi, mods, 1);
3659 dpi->modifiers = hold_modifiers;
3662 /* Print an array type, except for the element type. */
3665 d_print_array_type (struct d_print_info *dpi,
3666 const struct demangle_component *dc,
3667 struct d_print_mod *mods)
3675 struct d_print_mod *p;
3678 for (p = mods; p != NULL; p = p->next)
3682 if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
3697 d_append_string_constant (dpi, " (");
3699 d_print_mod_list (dpi, mods, 0);
3702 d_append_char (dpi, ')');
3706 d_append_char (dpi, ' ');
3708 d_append_char (dpi, '[');
3710 if (d_left (dc) != NULL)
3711 d_print_comp (dpi, d_left (dc));
3713 d_append_char (dpi, ']');
3716 /* Print an operator in an expression. */
3719 d_print_expr_op (struct d_print_info *dpi,
3720 const struct demangle_component *dc)
3722 if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
3723 d_append_buffer (dpi, dc->u.s_operator.op->name,
3724 dc->u.s_operator.op->len);
3726 d_print_comp (dpi, dc);
3732 d_print_cast (struct d_print_info *dpi,
3733 const struct demangle_component *dc)
3735 if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
3736 d_print_comp (dpi, d_left (dc));
3739 struct d_print_mod *hold_dpm;
3740 struct d_print_template dpt;
3742 /* It appears that for a templated cast operator, we need to put
3743 the template parameters in scope for the operator name, but
3744 not for the parameters. The effect is that we need to handle
3745 the template printing here. */
3747 hold_dpm = dpi->modifiers;
3748 dpi->modifiers = NULL;
3750 dpt.next = dpi->templates;
3751 dpi->templates = &dpt;
3752 dpt.template_decl = d_left (dc);
3754 d_print_comp (dpi, d_left (d_left (dc)));
3756 dpi->templates = dpt.next;
3758 if (d_last_char (dpi) == '<')
3759 d_append_char (dpi, ' ');
3760 d_append_char (dpi, '<');
3761 d_print_comp (dpi, d_right (d_left (dc)));
3762 /* Avoid generating two consecutive '>' characters, to avoid
3763 the C++ syntactic ambiguity. */
3764 if (d_last_char (dpi) == '>')
3765 d_append_char (dpi, ' ');
3766 d_append_char (dpi, '>');
3768 dpi->modifiers = hold_dpm;
3772 /* Initialize the information structure we use to pass around
3775 CP_STATIC_IF_GLIBCPP_V3
3777 cplus_demangle_init_info (const char *mangled, int options, size_t len,
3781 di->send = mangled + len;
3782 di->options = options;
3786 /* We can not need more components than twice the number of chars in
3787 the mangled string. Most components correspond directly to
3788 chars, but the ARGLIST types are exceptions. */
3789 di->num_comps = 2 * len;
3792 /* Similarly, we can not need more substitutions than there are
3793 chars in the mangled string. */
3798 di->last_name = NULL;
3803 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
3804 name, return a buffer allocated with malloc holding the demangled
3805 name. OPTIONS is the usual libiberty demangler options. On
3806 success, this sets *PALC to the allocated size of the returned
3807 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
3808 a memory allocation failure. On failure, this returns NULL. */
3811 d_demangle (const char* mangled, int options, size_t *palc)
3816 struct demangle_component *dc;
3822 len = strlen (mangled);
3824 if (mangled[0] == '_' && mangled[1] == 'Z')
3826 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
3827 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
3828 && (mangled[9] == 'D' || mangled[9] == 'I')
3829 && mangled[10] == '_')
3833 r = (char *) malloc (40 + len - 11);
3838 if (mangled[9] == 'I')
3839 strcpy (r, "global constructors keyed to ");
3841 strcpy (r, "global destructors keyed to ");
3842 strcat (r, mangled + 11);
3848 if ((options & DMGL_TYPES) == 0)
3853 cplus_demangle_init_info (mangled, options, len, &di);
3856 #ifdef CP_DYNAMIC_ARRAYS
3857 __extension__ struct demangle_component comps[di.num_comps];
3858 __extension__ struct demangle_component *subs[di.num_subs];
3860 di.comps = &comps[0];
3863 di.comps = ((struct demangle_component *)
3864 malloc (di.num_comps * sizeof (struct demangle_component)));
3865 di.subs = ((struct demangle_component **)
3866 malloc (di.num_subs * sizeof (struct demangle_component *)));
3867 if (di.comps == NULL || di.subs == NULL)
3869 if (di.comps != NULL)
3871 if (di.subs != NULL)
3879 dc = cplus_demangle_mangled_name (&di, 1);
3881 dc = cplus_demangle_type (&di);
3883 /* If DMGL_PARAMS is set, then if we didn't consume the entire
3884 mangled string, then we didn't successfully demangle it. If
3885 DMGL_PARAMS is not set, we didn't look at the trailing
3887 if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
3890 #ifdef CP_DEMANGLE_DEBUG
3892 printf ("failed demangling\n");
3897 /* We try to guess the length of the demangled string, to minimize
3898 calls to realloc during demangling. */
3899 estimate = len + di.expansion + 10 * di.did_subs;
3900 estimate += estimate / 8;
3904 ret = cplus_demangle_print (options, dc, estimate, palc);
3906 #ifndef CP_DYNAMIC_ARRAYS
3911 #ifdef CP_DEMANGLE_DEBUG
3916 rlen = strlen (ret);
3917 if (rlen > 2 * estimate)
3918 printf ("*** Length %d much greater than estimate %d\n",
3920 else if (rlen > estimate)
3921 printf ("*** Length %d greater than estimate %d\n",
3923 else if (rlen < estimate / 2)
3924 printf ("*** Length %d much less than estimate %d\n",
3933 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
3935 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
3937 /* ia64 ABI-mandated entry point in the C++ runtime library for
3938 performing demangling. MANGLED_NAME is a NUL-terminated character
3939 string containing the name to be demangled.
3941 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
3942 *LENGTH bytes, into which the demangled name is stored. If
3943 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
3944 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
3945 is placed in a region of memory allocated with malloc.
3947 If LENGTH is non-NULL, the length of the buffer conaining the
3948 demangled name, is placed in *LENGTH.
3950 The return value is a pointer to the start of the NUL-terminated
3951 demangled name, or NULL if the demangling fails. The caller is
3952 responsible for deallocating this memory using free.
3954 *STATUS is set to one of the following values:
3955 0: The demangling operation succeeded.
3956 -1: A memory allocation failure occurred.
3957 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
3958 -3: One of the arguments is invalid.
3960 The demangling is performed using the C++ ABI mangling rules, with
3964 __cxa_demangle (const char *mangled_name, char *output_buffer,
3965 size_t *length, int *status)
3970 if (mangled_name == NULL)
3977 if (output_buffer != NULL && length == NULL)
3984 demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
3986 if (demangled == NULL)
3998 if (output_buffer == NULL)
4005 if (strlen (demangled) < *length)
4007 strcpy (output_buffer, demangled);
4009 demangled = output_buffer;
4013 free (output_buffer);
4024 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
4026 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
4027 mangled name, return a buffer allocated with malloc holding the
4028 demangled name. Otherwise, return NULL. */
4031 cplus_demangle_v3 (const char* mangled, int options)
4035 return d_demangle (mangled, options, &alc);
4038 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
4039 conventions, but the output formatting is a little different.
4040 This instructs the C++ demangler not to emit pointer characters ("*"), and
4041 to use Java's namespace separator symbol ("." instead of "::"). It then
4042 does an additional pass over the demangled output to replace instances
4043 of JArray<TYPE> with TYPE[]. */
4046 java_demangle_v3 (const char* mangled)
4054 demangled = d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
4057 if (demangled == NULL)
4063 while (*from != '\0')
4065 if (strncmp (from, "JArray<", 7) == 0)
4070 else if (nesting > 0 && *from == '>')
4072 while (to > demangled && to[-1] == ' ')
4088 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
4090 #ifndef IN_GLIBCPP_V3
4092 /* Demangle a string in order to find out whether it is a constructor
4093 or destructor. Return non-zero on success. Set *CTOR_KIND and
4094 *DTOR_KIND appropriately. */
4097 is_ctor_or_dtor (const char *mangled,
4098 enum gnu_v3_ctor_kinds *ctor_kind,
4099 enum gnu_v3_dtor_kinds *dtor_kind)
4102 struct demangle_component *dc;
4105 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
4106 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
4108 cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
4111 #ifdef CP_DYNAMIC_ARRAYS
4112 __extension__ struct demangle_component comps[di.num_comps];
4113 __extension__ struct demangle_component *subs[di.num_subs];
4115 di.comps = &comps[0];
4118 di.comps = ((struct demangle_component *)
4119 malloc (di.num_comps * sizeof (struct demangle_component)));
4120 di.subs = ((struct demangle_component **)
4121 malloc (di.num_subs * sizeof (struct demangle_component *)));
4122 if (di.comps == NULL || di.subs == NULL)
4124 if (di.comps != NULL)
4126 if (di.subs != NULL)
4132 dc = cplus_demangle_mangled_name (&di, 1);
4134 /* Note that because we did not pass DMGL_PARAMS, we don't expect
4135 to demangle the entire string. */
4145 case DEMANGLE_COMPONENT_TYPED_NAME:
4146 case DEMANGLE_COMPONENT_TEMPLATE:
4147 case DEMANGLE_COMPONENT_RESTRICT_THIS:
4148 case DEMANGLE_COMPONENT_VOLATILE_THIS:
4149 case DEMANGLE_COMPONENT_CONST_THIS:
4152 case DEMANGLE_COMPONENT_QUAL_NAME:
4153 case DEMANGLE_COMPONENT_LOCAL_NAME:
4156 case DEMANGLE_COMPONENT_CTOR:
4157 *ctor_kind = dc->u.s_ctor.kind;
4161 case DEMANGLE_COMPONENT_DTOR:
4162 *dtor_kind = dc->u.s_dtor.kind;
4169 #ifndef CP_DYNAMIC_ARRAYS
4178 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
4179 name. A non-zero return indicates the type of constructor. */
4181 enum gnu_v3_ctor_kinds
4182 is_gnu_v3_mangled_ctor (const char *name)
4184 enum gnu_v3_ctor_kinds ctor_kind;
4185 enum gnu_v3_dtor_kinds dtor_kind;
4187 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
4188 return (enum gnu_v3_ctor_kinds) 0;
4193 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
4194 name. A non-zero return indicates the type of destructor. */
4196 enum gnu_v3_dtor_kinds
4197 is_gnu_v3_mangled_dtor (const char *name)
4199 enum gnu_v3_ctor_kinds ctor_kind;
4200 enum gnu_v3_dtor_kinds dtor_kind;
4202 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
4203 return (enum gnu_v3_dtor_kinds) 0;
4207 #endif /* IN_GLIBCPP_V3 */
4209 #ifdef STANDALONE_DEMANGLER
4212 #include "dyn-string.h"
4214 static void print_usage (FILE* fp, int exit_value);
4216 #define IS_ALPHA(CHAR) \
4217 (((CHAR) >= 'a' && (CHAR) <= 'z') \
4218 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
4220 /* Non-zero if CHAR is a character than can occur in a mangled name. */
4221 #define is_mangled_char(CHAR) \
4222 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
4223 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
4225 /* The name of this program, as invoked. */
4226 const char* program_name;
4228 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
4231 print_usage (FILE* fp, int exit_value)
4233 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
4234 fprintf (fp, "Options:\n");
4235 fprintf (fp, " -h,--help Display this message.\n");
4236 fprintf (fp, " -p,--no-params Don't display function parameters\n");
4237 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
4238 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
4243 /* Option specification for getopt_long. */
4244 static const struct option long_options[] =
4246 { "help", no_argument, NULL, 'h' },
4247 { "no-params", no_argument, NULL, 'p' },
4248 { "verbose", no_argument, NULL, 'v' },
4249 { NULL, no_argument, NULL, 0 },
4252 /* Main entry for a demangling filter executable. It will demangle
4253 its command line arguments, if any. If none are provided, it will
4254 filter stdin to stdout, replacing any recognized mangled C++ names
4255 with their demangled equivalents. */
4258 main (int argc, char *argv[])
4262 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
4264 /* Use the program name of this program, as invoked. */
4265 program_name = argv[0];
4267 /* Parse options. */
4270 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
4273 case '?': /* Unrecognized option. */
4274 print_usage (stderr, 1);
4278 print_usage (stdout, 0);
4282 options &= ~ DMGL_PARAMS;
4286 options |= DMGL_VERBOSE;
4290 while (opt_char != -1);
4293 /* No command line arguments were provided. Filter stdin. */
4295 dyn_string_t mangled = dyn_string_new (3);
4298 /* Read all of input. */
4299 while (!feof (stdin))
4303 /* Pile characters into mangled until we hit one that can't
4304 occur in a mangled name. */
4306 while (!feof (stdin) && is_mangled_char (c))
4308 dyn_string_append_char (mangled, c);
4314 if (dyn_string_length (mangled) > 0)
4316 #ifdef IN_GLIBCPP_V3
4317 s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
4319 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
4329 /* It might not have been a mangled name. Print the
4331 fputs (dyn_string_buf (mangled), stdout);
4334 dyn_string_clear (mangled);
4337 /* If we haven't hit EOF yet, we've read one character that
4338 can't occur in a mangled name, so print it out. */
4343 dyn_string_delete (mangled);
4346 /* Demangle command line arguments. */
4348 /* Loop over command line arguments. */
4349 for (i = optind; i < argc; ++i)
4352 #ifdef IN_GLIBCPP_V3
4356 /* Attempt to demangle. */
4357 #ifdef IN_GLIBCPP_V3
4358 s = __cxa_demangle (argv[i], NULL, NULL, &status);
4360 s = cplus_demangle_v3 (argv[i], options);
4363 /* If it worked, print the demangled name. */
4371 #ifdef IN_GLIBCPP_V3
4372 fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
4374 fprintf (stderr, "Failed: %s\n", argv[i]);
4383 #endif /* STANDALONE_DEMANGLER */