1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@wasabisystems.com>.
5 This file is part of the libiberty library, which is part of GCC.
7 This file is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combined
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 /* This code implements a demangler for the g++ V3 ABI. The ABI is
32 described on this web page:
33 http://www.codesourcery.com/cxx-abi/abi.html#mangling
35 This code was written while looking at the demangler written by
36 Alex Samuel <samuel@codesourcery.com>.
38 This code first pulls the mangled name apart into a list of
39 components, and then walks the list generating the demangled
42 This file will normally define the following functions, q.v.:
43 char *cplus_demangle_v3(const char *mangled, int options)
44 char *java_demangle_v3(const char *mangled)
45 enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
46 enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
48 Preprocessor macros you can define while compiling this file:
51 If defined, this file defines the following function, q.v.:
52 char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
54 instead of cplus_demangle_v3() and java_demangle_v3().
57 If defined, this file defines only __cxa_demangle().
60 If defined, this file defines a main() function which demangles
61 any arguments, or, if none, demangles stdin.
64 If defined, turns on debugging mode, which prints information on
65 stdout about the mangled string. This is not generally useful.
82 #include "libiberty.h"
85 /* We avoid pulling in the ctype tables, to prevent pulling in
86 additional unresolved symbols when this code is used in a library.
87 FIXME: Is this really a valid reason? This comes from the original
90 As of this writing this file has the following undefined references
91 when compiled with -DIN_GLIBCPP_V3: malloc, realloc, free, memcpy,
92 strcpy, strcat, strlen. */
94 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
95 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
96 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
98 /* The prefix prepended by GCC to an identifier represnting the
99 anonymous namespace. */
100 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
101 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
102 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
104 /* Information we keep for operators. */
106 struct d_operator_info
112 /* Number of arguments. */
116 /* How to print the value of a builtin type. */
118 enum d_builtin_type_print
120 /* Print as (type)val. */
122 /* Print as integer. */
124 /* Print as long, with trailing `l'. */
128 /* Print in usual way, but here to detect void. */
132 /* Information we keep for a builtin type. */
134 struct d_builtin_type_info
138 /* Type name when using Java. */
139 const char *java_name;
140 /* How to print a value of this type. */
141 enum d_builtin_type_print print;
144 /* Information we keep for the standard substitutions. */
146 struct d_standard_sub_info
148 /* The code for this substitution. */
150 /* The simple string it expands to. */
151 const char *simple_expansion;
152 /* The results of a full, verbose, expansion. This is used when
153 qualifying a constructor/destructor, or when in verbose mode. */
154 const char *full_expansion;
155 /* What to set the last_name field of d_info to; NULL if we should
156 not set it. This is only relevant when qualifying a
157 constructor/destructor. */
158 const char *set_last_name;
161 /* Component types found in mangled names. */
167 /* A qualified name. */
173 /* A template parameter. */
174 D_COMP_TEMPLATE_PARAM,
181 /* A VTT structure. */
183 /* A construction vtable. */
184 D_COMP_CONSTRUCTION_VTABLE,
185 /* A typeinfo structure. */
187 /* A typeinfo name. */
188 D_COMP_TYPEINFO_NAME,
189 /* A typeinfo function. */
193 /* A virtual thunk. */
194 D_COMP_VIRTUAL_THUNK,
195 /* A covariant thunk. */
196 D_COMP_COVARIANT_THUNK,
199 /* A guard variable. */
201 /* A reference temporary. */
203 /* A standard substitution. */
205 /* The restrict qualifier. */
207 /* The volatile qualifier. */
209 /* The const qualifier. */
211 /* The restrict qualifier modifying a member function. */
212 D_COMP_RESTRICT_THIS,
213 /* The volatile qualifier modifying a member function. */
214 D_COMP_VOLATILE_THIS,
215 /* The const qualifier modifying a member function. */
217 /* A vendor qualifier. */
218 D_COMP_VENDOR_TYPE_QUAL,
223 /* A complex type. */
225 /* An imaginary type. */
227 /* A builtin type. */
229 /* A vendor's builtin type. */
231 /* A function type. */
232 D_COMP_FUNCTION_TYPE,
235 /* A pointer to member type. */
237 /* An argument list. */
239 /* A template argument list. */
240 D_COMP_TEMPLATE_ARGLIST,
243 /* An extended operator. */
244 D_COMP_EXTENDED_OPERATOR,
247 /* A unary expression. */
249 /* A binary expression. */
251 /* Arguments to a binary expression. */
253 /* A trinary expression. */
255 /* Arguments to a trinary expression. */
260 /* A negative literal. */
264 /* A component of the mangled name. */
268 /* The type of this component. */
269 enum d_comp_type type;
272 /* For D_COMP_NAME. */
275 /* A pointer to the name (not NULL terminated) and it's
281 /* For D_COMP_OPERATOR. */
285 const struct d_operator_info *op;
288 /* For D_COMP_EXTENDED_OPERATOR. */
291 /* Number of arguments. */
295 } s_extended_operator;
297 /* For D_COMP_CTOR. */
300 enum gnu_v3_ctor_kinds kind;
304 /* For D_COMP_DTOR. */
307 enum gnu_v3_dtor_kinds kind;
311 /* For D_COMP_BUILTIN_TYPE. */
314 const struct d_builtin_type_info *type;
317 /* For D_COMP_SUB_STD. */
323 /* For D_COMP_TEMPLATE_PARAM. */
329 /* For other types. */
333 struct d_comp *right;
339 #define d_left(dc) ((dc)->u.s_binary.left)
340 #define d_right(dc) ((dc)->u.s_binary.right)
342 /* The information structure we pass around. */
346 /* The string we are demangling. */
348 /* The options passed to the demangler. */
350 /* The next character in the string to consider. */
352 /* The array of components. */
353 struct d_comp *comps;
354 /* The index of the next available component. */
356 /* The number of available component structures. */
358 /* The array of substitutions. */
359 struct d_comp **subs;
360 /* The index of the next substitution. */
362 /* The number of available entries in the subs array. */
364 /* The last name we saw, for constructors and destructors. */
365 struct d_comp *last_name;
368 #define d_peek_char(di) (*((di)->n))
369 #define d_peek_next_char(di) ((di)->n[1])
370 #define d_advance(di, i) ((di)->n += (i))
371 #define d_next_char(di) (*((di)->n++))
372 #define d_str(di) ((di)->n)
374 /* A list of templates. This is used while printing. */
376 struct d_print_template
378 /* Next template on the list. */
379 struct d_print_template *next;
381 const struct d_comp *template;
384 /* A list of type modifiers. This is used while printing. */
388 /* Next modifier on the list. These are in the reverse of the order
389 in which they appeared in the mangled string. */
390 struct d_print_mod *next;
392 const struct d_comp *mod;
393 /* Whether this modifier was printed. */
395 /* The list of templates which applies to this modifier. */
396 struct d_print_template *templates;
399 /* We use this structure to hold information during printing. */
403 /* The options passed to the demangler. */
405 /* Buffer holding the result. */
407 /* Current length of data in buffer. */
409 /* Allocated size of buffer. */
411 /* The current list of templates, if any. */
412 struct d_print_template *templates;
413 /* The current list of modifiers (e.g., pointer, reference, etc.),
415 struct d_print_mod *modifiers;
416 /* Set to 1 if we had a memory allocation failure. */
417 int allocation_failure;
420 #define d_print_saw_error(dpi) ((dpi)->buf == NULL)
422 #define d_append_char(dpi, c) \
425 if ((dpi)->buf != NULL && (dpi)->len < (dpi)->alc) \
426 (dpi)->buf[(dpi)->len++] = (c); \
428 d_print_append_char ((dpi), (c)); \
432 #define d_append_buffer(dpi, s, l) \
435 if ((dpi)->buf != NULL && (dpi)->len + (l) <= (dpi)->alc) \
437 memcpy ((dpi)->buf + (dpi)->len, (s), (l)); \
441 d_print_append_buffer ((dpi), (s), (l)); \
445 #define d_append_string(dpi, s) \
448 size_t d_append_string_len = strlen (s); \
449 d_append_buffer ((dpi), (s), d_append_string_len); \
453 #define d_last_char(dpi) \
454 ((dpi)->buf == NULL || (dpi)->len == 0 ? '\0' : (dpi)->buf[(dpi)->len - 1])
456 #ifdef CP_DEMANGLE_DEBUG
457 static void d_dump PARAMS ((struct d_comp *, int));
459 static struct d_comp *d_make_empty PARAMS ((struct d_info *,
461 static struct d_comp *d_make_comp PARAMS ((struct d_info *, enum d_comp_type,
462 struct d_comp *, struct d_comp *));
463 static struct d_comp *d_make_name PARAMS ((struct d_info *, const char *,
465 static struct d_comp *d_make_builtin_type PARAMS ((struct d_info *,
466 const struct d_builtin_type_info *));
467 static struct d_comp *d_make_operator PARAMS ((struct d_info *,
468 const struct d_operator_info *));
469 static struct d_comp *d_make_extended_operator PARAMS ((struct d_info *,
472 static struct d_comp *d_make_ctor PARAMS ((struct d_info *,
473 enum gnu_v3_ctor_kinds,
475 static struct d_comp *d_make_dtor PARAMS ((struct d_info *,
476 enum gnu_v3_dtor_kinds,
478 static struct d_comp *d_make_template_param PARAMS ((struct d_info *, long));
479 static struct d_comp *d_make_sub PARAMS ((struct d_info *, const char *));
480 static struct d_comp *d_mangled_name PARAMS ((struct d_info *, int));
481 static int has_return_type PARAMS ((struct d_comp *));
482 static int is_ctor_dtor_or_conversion PARAMS ((struct d_comp *));
483 static struct d_comp *d_encoding PARAMS ((struct d_info *, int));
484 static struct d_comp *d_name PARAMS ((struct d_info *));
485 static struct d_comp *d_nested_name PARAMS ((struct d_info *));
486 static struct d_comp *d_prefix PARAMS ((struct d_info *));
487 static struct d_comp *d_unqualified_name PARAMS ((struct d_info *));
488 static struct d_comp *d_source_name PARAMS ((struct d_info *));
489 static long d_number PARAMS ((struct d_info *));
490 static struct d_comp *d_identifier PARAMS ((struct d_info *, int));
491 static struct d_comp *d_operator_name PARAMS ((struct d_info *));
492 static struct d_comp *d_special_name PARAMS ((struct d_info *));
493 static int d_call_offset PARAMS ((struct d_info *, int));
494 static struct d_comp *d_ctor_dtor_name PARAMS ((struct d_info *));
495 static struct d_comp *d_type PARAMS ((struct d_info *));
496 static struct d_comp **d_cv_qualifiers PARAMS ((struct d_info *,
497 struct d_comp **, int));
498 static struct d_comp *d_function_type PARAMS ((struct d_info *));
499 static struct d_comp *d_bare_function_type PARAMS ((struct d_info *, int));
500 static struct d_comp *d_class_enum_type PARAMS ((struct d_info *));
501 static struct d_comp *d_array_type PARAMS ((struct d_info *));
502 static struct d_comp *d_pointer_to_member_type PARAMS ((struct d_info *));
503 static struct d_comp *d_template_param PARAMS ((struct d_info *));
504 static struct d_comp *d_template_args PARAMS ((struct d_info *));
505 static struct d_comp *d_template_arg PARAMS ((struct d_info *));
506 static struct d_comp *d_expression PARAMS ((struct d_info *));
507 static struct d_comp *d_expr_primary PARAMS ((struct d_info *));
508 static struct d_comp *d_local_name PARAMS ((struct d_info *));
509 static int d_discriminator PARAMS ((struct d_info *));
510 static int d_add_substitution PARAMS ((struct d_info *, struct d_comp *));
511 static struct d_comp *d_substitution PARAMS ((struct d_info *, int));
512 static void d_print_resize PARAMS ((struct d_print_info *, size_t));
513 static void d_print_append_char PARAMS ((struct d_print_info *, int));
514 static void d_print_append_buffer PARAMS ((struct d_print_info *, const char *,
516 static void d_print_error PARAMS ((struct d_print_info *));
517 static char *d_print PARAMS ((int, const struct d_comp *, size_t *));
518 static void d_print_comp PARAMS ((struct d_print_info *,
519 const struct d_comp *));
520 static void d_print_identifier PARAMS ((struct d_print_info *, const char *,
522 static void d_print_mod_list PARAMS ((struct d_print_info *,
523 struct d_print_mod *, int));
524 static void d_print_mod PARAMS ((struct d_print_info *,
525 const struct d_comp *));
526 static void d_print_function_type PARAMS ((struct d_print_info *,
527 const struct d_comp *,
528 struct d_print_mod *));
529 static void d_print_array_type PARAMS ((struct d_print_info *,
530 const struct d_comp *,
531 struct d_print_mod *));
532 static void d_print_expr_op PARAMS ((struct d_print_info *,
533 const struct d_comp *));
534 static void d_print_cast PARAMS ((struct d_print_info *,
535 const struct d_comp *));
536 static int d_init_info PARAMS ((const char *, int, size_t, struct d_info *));
537 static char *d_demangle PARAMS ((const char *, int, size_t *));
539 #ifdef CP_DEMANGLE_DEBUG
551 for (i = 0; i < indent; ++i)
557 printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
559 case D_COMP_TEMPLATE_PARAM:
560 printf ("template parameter %ld\n", dc->u.s_number.number);
563 printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
564 d_dump (dc->u.s_ctor.name, indent + 2);
567 printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
568 d_dump (dc->u.s_dtor.name, indent + 2);
571 printf ("standard substitution %s\n", dc->u.s_string.string);
573 case D_COMP_BUILTIN_TYPE:
574 printf ("builtin type %s\n", dc->u.s_builtin.type->name);
576 case D_COMP_OPERATOR:
577 printf ("operator %s\n", dc->u.s_operator.op->name);
579 case D_COMP_EXTENDED_OPERATOR:
580 printf ("extended operator with %d args\n",
581 dc->u.s_extended_operator.args);
582 d_dump (dc->u.s_extended_operator.name, indent + 2);
585 case D_COMP_QUAL_NAME:
586 printf ("qualified name\n");
588 case D_COMP_TYPED_NAME:
589 printf ("typed name\n");
591 case D_COMP_TEMPLATE:
592 printf ("template\n");
600 case D_COMP_CONSTRUCTION_VTABLE:
601 printf ("construction vtable\n");
603 case D_COMP_TYPEINFO:
604 printf ("typeinfo\n");
606 case D_COMP_TYPEINFO_NAME:
607 printf ("typeinfo name\n");
609 case D_COMP_TYPEINFO_FN:
610 printf ("typeinfo function\n");
615 case D_COMP_VIRTUAL_THUNK:
616 printf ("virtual thunk\n");
618 case D_COMP_COVARIANT_THUNK:
619 printf ("covariant thunk\n");
621 case D_COMP_JAVA_CLASS:
622 printf ("java class\n");
628 printf ("reference temporary\n");
630 case D_COMP_RESTRICT:
631 printf ("restrict\n");
633 case D_COMP_VOLATILE:
634 printf ("volatile\n");
639 case D_COMP_RESTRICT_THIS:
640 printf ("restrict this\n");
642 case D_COMP_VOLATILE_THIS:
643 printf ("volatile this\n");
645 case D_COMP_CONST_THIS:
646 printf ("const this\n");
648 case D_COMP_VENDOR_TYPE_QUAL:
649 printf ("vendor type qualifier\n");
652 printf ("pointer\n");
654 case D_COMP_REFERENCE:
655 printf ("reference\n");
658 printf ("complex\n");
660 case D_COMP_IMAGINARY:
661 printf ("imaginary\n");
663 case D_COMP_VENDOR_TYPE:
664 printf ("vendor type\n");
666 case D_COMP_FUNCTION_TYPE:
667 printf ("function type\n");
669 case D_COMP_ARRAY_TYPE:
670 printf ("array type\n");
672 case D_COMP_PTRMEM_TYPE:
673 printf ("pointer to member type\n");
676 printf ("argument list\n");
678 case D_COMP_TEMPLATE_ARGLIST:
679 printf ("template argument list\n");
685 printf ("unary operator\n");
688 printf ("binary operator\n");
690 case D_COMP_BINARY_ARGS:
691 printf ("binary operator arguments\n");
694 printf ("trinary operator\n");
696 case D_COMP_TRINARY_ARG1:
697 printf ("trinary operator arguments 1\n");
699 case D_COMP_TRINARY_ARG2:
700 printf ("trinary operator arguments 1\n");
703 printf ("literal\n");
705 case D_COMP_LITERAL_NEG:
706 printf ("negative literal\n");
710 d_dump (d_left (dc), indent + 2);
711 d_dump (d_right (dc), indent + 2);
714 #endif /* CP_DEMANGLE_DEBUG */
716 /* Add a new component. */
718 static struct d_comp *
719 d_make_empty (di, type)
721 enum d_comp_type type;
725 if (di->next_comp >= di->num_comps)
727 p = &di->comps[di->next_comp];
733 /* Add a new generic component. */
735 static struct d_comp *
736 d_make_comp (di, type, left, right)
738 enum d_comp_type type;
740 struct d_comp *right;
744 /* We check for errors here. A typical error would be a NULL return
745 from a subroutine. We catch those here, and return NULL
749 /* These types require two parameters. */
750 case D_COMP_QUAL_NAME:
751 case D_COMP_TYPED_NAME:
752 case D_COMP_TEMPLATE:
753 case D_COMP_VENDOR_TYPE_QUAL:
754 case D_COMP_PTRMEM_TYPE:
757 case D_COMP_BINARY_ARGS:
759 case D_COMP_TRINARY_ARG1:
760 case D_COMP_TRINARY_ARG2:
762 case D_COMP_LITERAL_NEG:
763 if (left == NULL || right == NULL)
767 /* These types only require one parameter. */
770 case D_COMP_CONSTRUCTION_VTABLE:
771 case D_COMP_TYPEINFO:
772 case D_COMP_TYPEINFO_NAME:
773 case D_COMP_TYPEINFO_FN:
775 case D_COMP_VIRTUAL_THUNK:
776 case D_COMP_COVARIANT_THUNK:
777 case D_COMP_JAVA_CLASS:
781 case D_COMP_REFERENCE:
783 case D_COMP_IMAGINARY:
784 case D_COMP_VENDOR_TYPE:
786 case D_COMP_TEMPLATE_ARGLIST:
792 /* This needs a right parameter, but the left parameter can be
794 case D_COMP_ARRAY_TYPE:
799 /* These are allowed to have no parameters--in some cases they
800 will be filled in later. */
801 case D_COMP_FUNCTION_TYPE:
802 case D_COMP_RESTRICT:
803 case D_COMP_VOLATILE:
805 case D_COMP_RESTRICT_THIS:
806 case D_COMP_VOLATILE_THIS:
807 case D_COMP_CONST_THIS:
810 /* Other types should not be seen here. */
815 p = d_make_empty (di, type);
818 p->u.s_binary.left = left;
819 p->u.s_binary.right = right;
824 /* Add a new name component. */
826 static struct d_comp *
827 d_make_name (di, s, len)
834 if (s == NULL || len == 0)
836 p = d_make_empty (di, D_COMP_NAME);
840 p->u.s_name.len = len;
845 /* Add a new builtin type component. */
847 static struct d_comp *
848 d_make_builtin_type (di, type)
850 const struct d_builtin_type_info *type;
856 p = d_make_empty (di, D_COMP_BUILTIN_TYPE);
858 p->u.s_builtin.type = type;
862 /* Add a new operator component. */
864 static struct d_comp *
865 d_make_operator (di, op)
867 const struct d_operator_info *op;
871 p = d_make_empty (di, D_COMP_OPERATOR);
873 p->u.s_operator.op = op;
877 /* Add a new extended operator component. */
879 static struct d_comp *
880 d_make_extended_operator (di, args, name)
889 p = d_make_empty (di, D_COMP_EXTENDED_OPERATOR);
892 p->u.s_extended_operator.args = args;
893 p->u.s_extended_operator.name = name;
898 /* Add a new constructor component. */
900 static struct d_comp *
901 d_make_ctor (di, kind, name)
903 enum gnu_v3_ctor_kinds kind;
910 p = d_make_empty (di, D_COMP_CTOR);
913 p->u.s_ctor.kind = kind;
914 p->u.s_ctor.name = name;
919 /* Add a new destructor component. */
921 static struct d_comp *
922 d_make_dtor (di, kind, name)
924 enum gnu_v3_dtor_kinds kind;
931 p = d_make_empty (di, D_COMP_DTOR);
934 p->u.s_dtor.kind = kind;
935 p->u.s_dtor.name = name;
940 /* Add a new template parameter. */
942 static struct d_comp *
943 d_make_template_param (di, i)
949 p = d_make_empty (di, D_COMP_TEMPLATE_PARAM);
951 p->u.s_number.number = i;
955 /* Add a new standard substitution component. */
957 static struct d_comp *
958 d_make_sub (di, name)
964 p = d_make_empty (di, D_COMP_SUB_STD);
966 p->u.s_string.string = name;
970 /* <mangled-name> ::= _Z <encoding>
972 TOP_LEVEL is non-zero when called at the top level. */
974 static struct d_comp *
975 d_mangled_name (di, top_level)
979 if (d_next_char (di) != '_')
981 if (d_next_char (di) != 'Z')
983 return d_encoding (di, top_level);
986 /* Return whether a function should have a return type. The argument
987 is the function name, which may be qualified in various ways. The
988 rules are that template functions have return types with some
989 exceptions, function types which are not part of a function name
990 mangling have return types with some exceptions, and non-template
991 function names do not have return types. The exceptions are that
992 constructors, destructors, and conversion operators do not have
1005 case D_COMP_TEMPLATE:
1006 return ! is_ctor_dtor_or_conversion (d_left (dc));
1007 case D_COMP_RESTRICT_THIS:
1008 case D_COMP_VOLATILE_THIS:
1009 case D_COMP_CONST_THIS:
1010 return has_return_type (d_left (dc));
1014 /* Return whether a name is a constructor, a destructor, or a
1015 conversion operator. */
1018 is_ctor_dtor_or_conversion (dc)
1027 case D_COMP_QUAL_NAME:
1028 return is_ctor_dtor_or_conversion (d_right (dc));
1036 /* <encoding> ::= <(function) name> <bare-function-type>
1040 TOP_LEVEL is non-zero when called at the top level, in which case
1041 if DMGL_PARAMS is not set we do not demangle the function
1042 parameters. We only set this at the top level, because otherwise
1043 we would not correctly demangle names in local scopes. */
1045 static struct d_comp *
1046 d_encoding (di, top_level)
1050 char peek = d_peek_char (di);
1052 if (peek == 'G' || peek == 'T')
1053 return d_special_name (di);
1060 if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
1062 /* Strip off any initial CV-qualifiers, as they really apply
1063 to the `this' parameter, and they were not output by the
1064 v2 demangler without DMGL_PARAMS. */
1065 while (dc->type == D_COMP_RESTRICT_THIS
1066 || dc->type == D_COMP_VOLATILE_THIS
1067 || dc->type == D_COMP_CONST_THIS)
1072 peek = d_peek_char (di);
1073 if (peek == '\0' || peek == 'E')
1075 return d_make_comp (di, D_COMP_TYPED_NAME, dc,
1076 d_bare_function_type (di, has_return_type (dc)));
1080 /* <name> ::= <nested-name>
1082 ::= <unscoped-template-name> <template-args>
1085 <unscoped-name> ::= <unqualified-name>
1086 ::= St <unqualified-name>
1088 <unscoped-template-name> ::= <unscoped-name>
1092 static struct d_comp *
1096 char peek = d_peek_char (di);
1102 return d_nested_name (di);
1105 return d_local_name (di);
1111 if (d_peek_next_char (di) != 't')
1113 dc = d_substitution (di, 0);
1119 dc = d_make_comp (di, D_COMP_QUAL_NAME, d_make_name (di, "std", 3),
1120 d_unqualified_name (di));
1124 if (d_peek_char (di) != 'I')
1126 /* The grammar does not permit this case to occur if we
1127 called d_substitution() above (i.e., subst == 1). We
1128 don't bother to check. */
1132 /* This is <template-args>, which means that we just saw
1133 <unscoped-template-name>, which is a substitution
1134 candidate if we didn't just get it from a
1138 if (! d_add_substitution (di, dc))
1141 dc = d_make_comp (di, D_COMP_TEMPLATE, dc, d_template_args (di));
1148 dc = d_unqualified_name (di);
1149 if (d_peek_char (di) == 'I')
1151 /* This is <template-args>, which means that we just saw
1152 <unscoped-template-name>, which is a substitution
1154 if (! d_add_substitution (di, dc))
1156 dc = d_make_comp (di, D_COMP_TEMPLATE, dc, d_template_args (di));
1162 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1163 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1166 static struct d_comp *
1171 struct d_comp **pret;
1173 if (d_next_char (di) != 'N')
1176 pret = d_cv_qualifiers (di, &ret, 1);
1180 *pret = d_prefix (di);
1184 if (d_next_char (di) != 'E')
1190 /* <prefix> ::= <prefix> <unqualified-name>
1191 ::= <template-prefix> <template-args>
1192 ::= <template-param>
1196 <template-prefix> ::= <prefix> <(template) unqualified-name>
1197 ::= <template-param>
1201 static struct d_comp *
1205 struct d_comp *ret = NULL;
1210 enum d_comp_type comb_type;
1213 peek = d_peek_char (di);
1217 /* The older code accepts a <local-name> here, but I don't see
1218 that in the grammar. The older code does not accept a
1219 <template-param> here. */
1221 comb_type = D_COMP_QUAL_NAME;
1226 dc = d_unqualified_name (di);
1227 else if (peek == 'S')
1228 dc = d_substitution (di, 1);
1229 else if (peek == 'I')
1233 comb_type = D_COMP_TEMPLATE;
1234 dc = d_template_args (di);
1236 else if (peek == 'T')
1237 dc = d_template_param (di);
1238 else if (peek == 'E')
1246 ret = d_make_comp (di, comb_type, ret, dc);
1248 if (peek != 'S' && d_peek_char (di) != 'E')
1250 if (! d_add_substitution (di, ret))
1256 /* <unqualified-name> ::= <operator-name>
1257 ::= <ctor-dtor-name>
1261 static struct d_comp *
1262 d_unqualified_name (di)
1267 peek = d_peek_char (di);
1268 if (IS_DIGIT (peek))
1269 return d_source_name (di);
1270 else if (IS_LOWER (peek))
1271 return d_operator_name (di);
1272 else if (peek == 'C' || peek == 'D')
1273 return d_ctor_dtor_name (di);
1278 /* <source-name> ::= <(positive length) number> <identifier> */
1280 static struct d_comp *
1287 len = d_number (di);
1290 ret = d_identifier (di, len);
1291 di->last_name = ret;
1295 /* number ::= [n] <(non-negative decimal integer)> */
1306 peek = d_peek_char (di);
1311 peek = d_peek_char (di);
1317 if (! IS_DIGIT (peek))
1319 ret = ret * 10 + peek - '0';
1321 peek = d_peek_char (di);
1325 /* identifier ::= <(unqualified source code identifier)> */
1327 static struct d_comp *
1328 d_identifier (di, len)
1335 d_advance (di, len);
1337 /* Look for something which looks like a gcc encoding of an
1338 anonymous namespace, and replace it with a more user friendly
1340 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1341 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1342 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1346 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1347 if ((*s == '.' || *s == '_' || *s == '$')
1349 return d_make_name (di, "(anonymous namespace)",
1350 sizeof "(anonymous namespace)" - 1);
1353 return d_make_name (di, name, len);
1356 /* operator_name ::= many different two character encodings.
1358 ::= v <digit> <source-name>
1361 static const struct d_operator_info d_operators[] =
1372 { "da", "delete[]", 1 },
1374 { "dl", "delete", 1 },
1391 { "na", "new[]", 1 },
1410 { "st", "sizeof ", 1 },
1411 { "sz", "sizeof ", 1 }
1414 static struct d_comp *
1415 d_operator_name (di)
1421 c1 = d_next_char (di);
1422 c2 = d_next_char (di);
1423 if (c1 == 'v' && IS_DIGIT (c2))
1424 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1425 else if (c1 == 'c' && c2 == 'v')
1426 return d_make_comp (di, D_COMP_CAST, d_type (di), NULL);
1430 int high = sizeof (d_operators) / sizeof (d_operators[0]);
1435 const struct d_operator_info *p;
1437 i = low + (high - low) / 2;
1438 p = d_operators + i;
1440 if (c1 == p->code[0] && c2 == p->code[1])
1441 return d_make_operator (di, p);
1443 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1453 /* <special-name> ::= TV <type>
1457 ::= GV <(object) name>
1458 ::= T <call-offset> <(base) encoding>
1459 ::= Tc <call-offset> <call-offset> <(base) encoding>
1460 Also g++ extensions:
1461 ::= TC <type> <(offset) number> _ <(base) type>
1467 static struct d_comp *
1473 c = d_next_char (di);
1476 switch (d_next_char (di))
1479 return d_make_comp (di, D_COMP_VTABLE, d_type (di), NULL);
1481 return d_make_comp (di, D_COMP_VTT, d_type (di), NULL);
1483 return d_make_comp (di, D_COMP_TYPEINFO, d_type (di), NULL);
1485 return d_make_comp (di, D_COMP_TYPEINFO_NAME, d_type (di), NULL);
1488 if (! d_call_offset (di, 'h'))
1490 return d_make_comp (di, D_COMP_THUNK, d_encoding (di, 0), NULL);
1493 if (! d_call_offset (di, 'v'))
1495 return d_make_comp (di, D_COMP_VIRTUAL_THUNK, d_encoding (di, 0),
1499 if (! d_call_offset (di, '\0'))
1501 if (! d_call_offset (di, '\0'))
1503 return d_make_comp (di, D_COMP_COVARIANT_THUNK, d_encoding (di, 0),
1508 struct d_comp *derived_type;
1510 struct d_comp *base_type;
1512 derived_type = d_type (di);
1513 offset = d_number (di);
1516 if (d_next_char (di) != '_')
1518 base_type = d_type (di);
1519 /* We don't display the offset. FIXME: We should display
1520 it in verbose mode. */
1521 return d_make_comp (di, D_COMP_CONSTRUCTION_VTABLE, base_type,
1526 return d_make_comp (di, D_COMP_TYPEINFO_FN, d_type (di), NULL);
1528 return d_make_comp (di, D_COMP_JAVA_CLASS, d_type (di), NULL);
1536 switch (d_next_char (di))
1539 return d_make_comp (di, D_COMP_GUARD, d_name (di), NULL);
1542 return d_make_comp (di, D_COMP_REFTEMP, d_name (di), NULL);
1552 /* <call-offset> ::= h <nv-offset> _
1555 <nv-offset> ::= <(offset) number>
1557 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1559 The C parameter, if not '\0', is a character we just read which is
1560 the start of the <call-offset>.
1562 We don't display the offset information anywhere. FIXME: We should
1563 display it in verbose mode. */
1566 d_call_offset (di, c)
1571 long virtual_offset;
1574 c = d_next_char (di);
1577 offset = d_number (di);
1580 offset = d_number (di);
1581 if (d_next_char (di) != '_')
1583 virtual_offset = d_number (di);
1588 if (d_next_char (di) != '_')
1594 /* <ctor-dtor-name> ::= C1
1602 static struct d_comp *
1603 d_ctor_dtor_name (di)
1606 switch (d_next_char (di))
1610 enum gnu_v3_ctor_kinds kind;
1612 switch (d_next_char (di))
1615 kind = gnu_v3_complete_object_ctor;
1618 kind = gnu_v3_base_object_ctor;
1621 kind = gnu_v3_complete_object_allocating_ctor;
1626 return d_make_ctor (di, kind, di->last_name);
1631 enum gnu_v3_dtor_kinds kind;
1633 switch (d_next_char (di))
1636 kind = gnu_v3_deleting_dtor;
1639 kind = gnu_v3_complete_object_dtor;
1642 kind = gnu_v3_base_object_dtor;
1647 return d_make_dtor (di, kind, di->last_name);
1655 /* <type> ::= <builtin-type>
1657 ::= <class-enum-type>
1659 ::= <pointer-to-member-type>
1660 ::= <template-param>
1661 ::= <template-template-param> <template-args>
1663 ::= <CV-qualifiers> <type>
1668 ::= U <source-name> <type>
1670 <builtin-type> ::= various one letter codes
1674 static const struct d_builtin_type_info d_builtin_types[26] =
1676 /* a */ { "signed char", "signed char", D_PRINT_INT },
1677 /* b */ { "bool", "boolean", D_PRINT_BOOL },
1678 /* c */ { "char", "byte", D_PRINT_INT },
1679 /* d */ { "double", "double", D_PRINT_DEFAULT },
1680 /* e */ { "long double", "long double", D_PRINT_DEFAULT },
1681 /* f */ { "float", "float", D_PRINT_DEFAULT },
1682 /* g */ { "__float128", "__float128", D_PRINT_DEFAULT },
1683 /* h */ { "unsigned char", "unsigned char", D_PRINT_INT },
1684 /* i */ { "int", "int", D_PRINT_INT },
1685 /* j */ { "unsigned int", "unsigned", D_PRINT_INT },
1686 /* k */ { NULL, NULL, D_PRINT_DEFAULT },
1687 /* l */ { "long", "long", D_PRINT_LONG },
1688 /* m */ { "unsigned long", "unsigned long", D_PRINT_LONG },
1689 /* n */ { "__int128", "__int128", D_PRINT_DEFAULT },
1690 /* o */ { "unsigned __int128", "unsigned __int128", D_PRINT_DEFAULT },
1691 /* p */ { NULL, NULL, D_PRINT_DEFAULT },
1692 /* q */ { NULL, NULL, D_PRINT_DEFAULT },
1693 /* r */ { NULL, NULL, D_PRINT_DEFAULT },
1694 /* s */ { "short", "short", D_PRINT_INT },
1695 /* t */ { "unsigned short", "unsigned short", D_PRINT_INT },
1696 /* u */ { NULL, NULL, D_PRINT_DEFAULT },
1697 /* v */ { "void", "void", D_PRINT_VOID },
1698 /* w */ { "wchar_t", "char", D_PRINT_INT },
1699 /* x */ { "long long", "long", D_PRINT_DEFAULT },
1700 /* y */ { "unsigned long long", "unsigned long long", D_PRINT_DEFAULT },
1701 /* z */ { "...", "...", D_PRINT_DEFAULT },
1704 static struct d_comp *
1712 /* The ABI specifies that when CV-qualifiers are used, the base type
1713 is substitutable, and the fully qualified type is substitutable,
1714 but the base type with a strict subset of the CV-qualifiers is
1715 not substitutable. The natural recursive implementation of the
1716 CV-qualifiers would cause subsets to be substitutable, so instead
1717 we pull them all off now.
1719 FIXME: The ABI says that order-insensitive vendor qualifiers
1720 should be handled in the same way, but we have no way to tell
1721 which vendor qualifiers are order-insensitive and which are
1722 order-sensitive. So we just assume that they are all
1723 order-sensitive. g++ 3.4 supports only one vendor qualifier,
1724 __vector, and it treats it as order-sensitive when mangling
1727 peek = d_peek_char (di);
1728 if (peek == 'r' || peek == 'V' || peek == 'K')
1730 struct d_comp **pret;
1732 pret = d_cv_qualifiers (di, &ret, 0);
1735 *pret = d_type (di);
1736 if (! d_add_substitution (di, ret))
1745 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1746 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
1747 case 'o': case 's': case 't':
1748 case 'v': case 'w': case 'x': case 'y': case 'z':
1749 ret = d_make_builtin_type (di, &d_builtin_types[peek - 'a']);
1756 ret = d_make_comp (di, D_COMP_VENDOR_TYPE, d_source_name (di), NULL);
1760 ret = d_function_type (di);
1763 case '0': case '1': case '2': case '3': case '4':
1764 case '5': case '6': case '7': case '8': case '9':
1767 ret = d_class_enum_type (di);
1771 ret = d_array_type (di);
1775 ret = d_pointer_to_member_type (di);
1779 ret = d_template_param (di);
1780 if (d_peek_char (di) == 'I')
1782 /* This is <template-template-param> <template-args>. The
1783 <template-template-param> part is a substitution
1785 if (! d_add_substitution (di, ret))
1787 ret = d_make_comp (di, D_COMP_TEMPLATE, ret, d_template_args (di));
1792 /* If this is a special substitution, then it is the start of
1793 <class-enum-type>. */
1797 peek_next = d_peek_next_char (di);
1798 if (IS_DIGIT (peek_next)
1800 || IS_UPPER (peek_next))
1802 ret = d_substitution (di, 0);
1803 /* The substituted name may have been a template name and
1804 may be followed by tepmlate args. */
1805 if (d_peek_char (di) == 'I')
1806 ret = d_make_comp (di, D_COMP_TEMPLATE, ret,
1807 d_template_args (di));
1813 ret = d_class_enum_type (di);
1814 /* If the substitution was a complete type, then it is not
1815 a new substitution candidate. However, if the
1816 substitution was followed by template arguments, then
1817 the whole thing is a substitution candidate. */
1818 if (ret != NULL && ret->type == D_COMP_SUB_STD)
1826 ret = d_make_comp (di, D_COMP_POINTER, d_type (di), NULL);
1831 ret = d_make_comp (di, D_COMP_REFERENCE, d_type (di), NULL);
1836 ret = d_make_comp (di, D_COMP_COMPLEX, d_type (di), NULL);
1841 ret = d_make_comp (di, D_COMP_IMAGINARY, d_type (di), NULL);
1846 ret = d_source_name (di);
1847 ret = d_make_comp (di, D_COMP_VENDOR_TYPE_QUAL, d_type (di), ret);
1856 if (! d_add_substitution (di, ret))
1863 /* <CV-qualifiers> ::= [r] [V] [K] */
1865 static struct d_comp **
1866 d_cv_qualifiers (di, pret, member_fn)
1868 struct d_comp **pret;
1873 peek = d_peek_char (di);
1874 while (peek == 'r' || peek == 'V' || peek == 'K')
1880 t = member_fn ? D_COMP_RESTRICT_THIS: D_COMP_RESTRICT;
1881 else if (peek == 'V')
1882 t = member_fn ? D_COMP_VOLATILE_THIS : D_COMP_VOLATILE;
1884 t = member_fn ? D_COMP_CONST_THIS: D_COMP_CONST;
1886 *pret = d_make_comp (di, t, NULL, NULL);
1889 pret = &d_left (*pret);
1891 peek = d_peek_char (di);
1897 /* <function-type> ::= F [Y] <bare-function-type> E */
1899 static struct d_comp *
1900 d_function_type (di)
1905 if (d_next_char (di) != 'F')
1907 if (d_peek_char (di) == 'Y')
1909 /* Function has C linkage. We don't print this information.
1910 FIXME: We should print it in verbose mode. */
1913 ret = d_bare_function_type (di, 1);
1914 if (d_next_char (di) != 'E')
1919 /* <bare-function-type> ::= <type>+ */
1921 static struct d_comp *
1922 d_bare_function_type (di, has_return_type)
1924 int has_return_type;
1926 struct d_comp *return_type;
1928 struct d_comp **ptl;
1936 struct d_comp *type;
1938 peek = d_peek_char (di);
1939 if (peek == '\0' || peek == 'E')
1944 if (has_return_type)
1947 has_return_type = 0;
1951 *ptl = d_make_comp (di, D_COMP_ARGLIST, type, NULL);
1954 ptl = &d_right (*ptl);
1958 /* There should be at least one parameter type besides the optional
1959 return type. A function which takes no arguments will have a
1960 single parameter type void. */
1964 /* If we have a single parameter type void, omit it. */
1965 if (d_right (tl) == NULL
1966 && d_left (tl)->type == D_COMP_BUILTIN_TYPE
1967 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
1970 return d_make_comp (di, D_COMP_FUNCTION_TYPE, return_type, tl);
1973 /* <class-enum-type> ::= <name> */
1975 static struct d_comp *
1976 d_class_enum_type (di)
1982 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
1983 ::= A [<(dimension) expression>] _ <(element) type>
1986 static struct d_comp *
1993 if (d_next_char (di) != 'A')
1996 peek = d_peek_char (di);
1999 else if (IS_DIGIT (peek))
2007 peek = d_peek_char (di);
2009 while (IS_DIGIT (peek));
2010 dim = d_make_name (di, s, d_str (di) - s);
2016 dim = d_expression (di);
2021 if (d_next_char (di) != '_')
2024 return d_make_comp (di, D_COMP_ARRAY_TYPE, dim, d_type (di));
2027 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2029 static struct d_comp *
2030 d_pointer_to_member_type (di)
2035 struct d_comp **pmem;
2037 if (d_next_char (di) != 'M')
2042 /* The ABI specifies that any type can be a substitution source, and
2043 that M is followed by two types, and that when a CV-qualified
2044 type is seen both the base type and the CV-qualified types are
2045 substitution sources. The ABI also specifies that for a pointer
2046 to a CV-qualified member function, the qualifiers are attached to
2047 the second type. Given the grammar, a plain reading of the ABI
2048 suggests that both the CV-qualified member function and the
2049 non-qualified member function are substitution sources. However,
2050 g++ does not work that way. g++ treats only the CV-qualified
2051 member function as a substitution source. FIXME. So to work
2052 with g++, we need to pull off the CV-qualifiers here, in order to
2053 avoid calling add_substitution() in d_type(). */
2055 pmem = d_cv_qualifiers (di, &mem, 1);
2058 *pmem = d_type (di);
2060 return d_make_comp (di, D_COMP_PTRMEM_TYPE, cl, mem);
2063 /* <template-param> ::= T_
2064 ::= T <(parameter-2 non-negative) number> _
2067 static struct d_comp *
2068 d_template_param (di)
2073 if (d_next_char (di) != 'T')
2076 if (d_peek_char (di) == '_')
2080 param = d_number (di);
2086 if (d_next_char (di) != '_')
2089 return d_make_template_param (di, param);
2092 /* <template-args> ::= I <template-arg>+ E */
2094 static struct d_comp *
2095 d_template_args (di)
2098 struct d_comp *hold_last_name;
2100 struct d_comp **pal;
2102 /* Preserve the last name we saw--don't let the template arguments
2103 clobber it, as that would give us the wrong name for a subsequent
2104 constructor or destructor. */
2105 hold_last_name = di->last_name;
2107 if (d_next_char (di) != 'I')
2116 a = d_template_arg (di);
2120 *pal = d_make_comp (di, D_COMP_TEMPLATE_ARGLIST, a, NULL);
2123 pal = &d_right (*pal);
2125 if (d_peek_char (di) == 'E')
2132 di->last_name = hold_last_name;
2137 /* <template-arg> ::= <type>
2138 ::= X <expression> E
2142 static struct d_comp *
2148 switch (d_peek_char (di))
2152 ret = d_expression (di);
2153 if (d_next_char (di) != 'E')
2158 return d_expr_primary (di);
2165 /* <expression> ::= <(unary) operator-name> <expression>
2166 ::= <(binary) operator-name> <expression> <expression>
2167 ::= <(trinary) operator-name> <expression> <expression> <expression>
2169 ::= <template-param>
2170 ::= sr <type> <unqualified-name>
2171 ::= sr <type> <unqualified-name> <template-args>
2175 static struct d_comp *
2181 peek = d_peek_char (di);
2183 return d_expr_primary (di);
2184 else if (peek == 'T')
2185 return d_template_param (di);
2186 else if (peek == 's' && d_peek_next_char (di) == 'r')
2188 struct d_comp *type;
2189 struct d_comp *name;
2193 name = d_unqualified_name (di);
2194 if (d_peek_char (di) != 'I')
2195 return d_make_comp (di, D_COMP_QUAL_NAME, type, name);
2197 return d_make_comp (di, D_COMP_QUAL_NAME, type,
2198 d_make_comp (di, D_COMP_TEMPLATE, name,
2199 d_template_args (di)));
2206 op = d_operator_name (di);
2210 if (op->type == D_COMP_OPERATOR
2211 && strcmp (op->u.s_operator.op->code, "st") == 0)
2212 return d_make_comp (di, D_COMP_UNARY, op, d_type (di));
2218 case D_COMP_OPERATOR:
2219 args = op->u.s_operator.op->args;
2221 case D_COMP_EXTENDED_OPERATOR:
2222 args = op->u.s_extended_operator.args;
2232 return d_make_comp (di, D_COMP_UNARY, op, d_expression (di));
2235 struct d_comp *left;
2237 left = d_expression (di);
2238 return d_make_comp (di, D_COMP_BINARY, op,
2239 d_make_comp (di, D_COMP_BINARY_ARGS, left,
2240 d_expression (di)));
2244 struct d_comp *first;
2245 struct d_comp *second;
2247 first = d_expression (di);
2248 second = d_expression (di);
2249 return d_make_comp (di, D_COMP_TRINARY, op,
2250 d_make_comp (di, D_COMP_TRINARY_ARG1, first,
2252 D_COMP_TRINARY_ARG2,
2254 d_expression (di))));
2262 /* <expr-primary> ::= L <type> <(value) number> E
2263 ::= L <type> <(value) float> E
2264 ::= L <mangled-name> E
2267 static struct d_comp *
2273 if (d_next_char (di) != 'L')
2275 if (d_peek_char (di) == '_')
2276 ret = d_mangled_name (di, 0);
2279 struct d_comp *type;
2285 /* Rather than try to interpret the literal value, we just
2286 collect it as a string. Note that it's possible to have a
2287 floating point literal here. The ABI specifies that the
2288 format of such literals is machine independent. That's fine,
2289 but what's not fine is that versions of g++ up to 3.2 with
2290 -fabi-version=1 used upper case letters in the hex constant,
2291 and dumped out gcc's internal representation. That makes it
2292 hard to tell where the constant ends, and hard to dump the
2293 constant in any readable form anyhow. We don't attempt to
2294 handle these cases. */
2297 if (d_peek_char (di) == 'n')
2299 t = D_COMP_LITERAL_NEG;
2303 while (d_peek_char (di) != 'E')
2305 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
2307 if (d_next_char (di) != 'E')
2312 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2313 ::= Z <(function) encoding> E s [<discriminator>]
2316 static struct d_comp *
2320 struct d_comp *function;
2322 if (d_next_char (di) != 'Z')
2325 function = d_encoding (di, 0);
2327 if (d_next_char (di) != 'E')
2330 if (d_peek_char (di) == 's')
2333 if (! d_discriminator (di))
2335 return d_make_comp (di, D_COMP_QUAL_NAME, function,
2336 d_make_name (di, "string literal",
2337 sizeof "string literal" - 1));
2341 struct d_comp *name;
2344 if (! d_discriminator (di))
2346 return d_make_comp (di, D_COMP_QUAL_NAME, function, name);
2350 /* <discriminator> ::= _ <(non-negative) number>
2352 We demangle the discriminator, but we don't print it out. FIXME:
2353 We should print it out in verbose mode. */
2356 d_discriminator (di)
2361 if (d_peek_char (di) != '_')
2364 discrim = d_number (di);
2370 /* Add a new substitution. */
2373 d_add_substitution (di, dc)
2379 if (di->next_sub >= di->num_subs)
2381 di->subs[di->next_sub] = dc;
2386 /* <substitution> ::= S <seq-id> _
2396 If PREFIX is non-zero, then this type is being used as a prefix in
2397 a qualified name. In this case, for the standard substitutions, we
2398 need to check whether we are being used as a prefix for a
2399 constructor or destructor, and return a full template name.
2400 Otherwise we will get something like std::iostream::~iostream()
2401 which does not correspond particularly well to any function which
2402 actually appears in the source.
2405 static const struct d_standard_sub_info standard_subs[] =
2407 { 't', "std", "std", NULL },
2408 { 'a', "std::allocator", "std::allocator", "allocator" },
2409 { 'b', "std::basic_string", "std::basic_string", "basic_string" },
2410 { 's', "std::string",
2411 "std::basic_string<char, std::char_traits<char>, std::allocator<char> >",
2413 { 'i', "std::istream",
2414 "std::basic_istream<char, std::char_traits<char> >",
2416 { 'o', "std::ostream",
2417 "std::basic_ostream<char, std::char_traits<char> >",
2419 { 'd', "std::iostream",
2420 "std::basic_iostream<char, std::char_traits<char> >",
2424 static struct d_comp *
2425 d_substitution (di, prefix)
2431 if (d_next_char (di) != 'S')
2434 c = d_next_char (di);
2435 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
2445 id = id * 36 + c - '0';
2446 else if (IS_UPPER (c))
2447 id = id * 36 + c - 'A' + 10;
2450 c = d_next_char (di);
2457 if (id >= di->next_sub)
2460 return di->subs[id];
2465 const struct d_standard_sub_info *p;
2466 const struct d_standard_sub_info *pend;
2468 verbose = (di->options & DMGL_VERBOSE) != 0;
2469 if (! verbose && prefix)
2473 peek = d_peek_char (di);
2474 if (peek == 'C' || peek == 'D')
2478 pend = (&standard_subs[0]
2479 + sizeof standard_subs / sizeof standard_subs[0]);
2480 for (p = &standard_subs[0]; p < pend; ++p)
2484 if (p->set_last_name != NULL)
2485 di->last_name = d_make_sub (di, p->set_last_name);
2487 return d_make_sub (di, p->full_expansion);
2489 return d_make_sub (di, p->simple_expansion);
2497 /* Resize the print buffer. */
2500 d_print_resize (dpi, add)
2501 struct d_print_info *dpi;
2506 if (dpi->buf == NULL)
2508 need = dpi->len + add;
2509 while (need > dpi->alc)
2514 newalc = dpi->alc * 2;
2515 newbuf = realloc (dpi->buf, newalc);
2520 dpi->allocation_failure = 1;
2528 /* Append a character to the print buffer. */
2531 d_print_append_char (dpi, c)
2532 struct d_print_info *dpi;
2535 if (dpi->buf != NULL)
2537 if (dpi->len >= dpi->alc)
2539 d_print_resize (dpi, 1);
2540 if (dpi->buf == NULL)
2544 dpi->buf[dpi->len] = c;
2549 /* Append a buffer to the print buffer. */
2552 d_print_append_buffer (dpi, s, l)
2553 struct d_print_info *dpi;
2557 if (dpi->buf != NULL)
2559 if (dpi->len + l > dpi->alc)
2561 d_print_resize (dpi, l);
2562 if (dpi->buf == NULL)
2566 memcpy (dpi->buf + dpi->len, s, l);
2571 /* Indicate that an error occurred during printing. */
2575 struct d_print_info *dpi;
2581 /* Turn components into a human readable string. Returns a string
2582 allocated by malloc, or NULL on error. On success, this sets *PALC
2583 to the size of the allocated buffer. On failure, this sets *PALC
2584 to 0 for a bad parse, or to 1 for a memory allocation failure. */
2587 d_print (options, dc, palc)
2589 const struct d_comp *dc;
2592 struct d_print_info dpi;
2594 dpi.options = options;
2597 dpi.buf = malloc (dpi.alc);
2598 if (dpi.buf == NULL)
2605 dpi.templates = NULL;
2606 dpi.modifiers = NULL;
2608 dpi.allocation_failure = 0;
2610 d_print_comp (&dpi, dc);
2612 d_append_char (&dpi, '\0');
2614 if (dpi.buf != NULL)
2617 *palc = dpi.allocation_failure;
2622 /* Subroutine to handle components. */
2625 d_print_comp (dpi, dc)
2626 struct d_print_info *dpi;
2627 const struct d_comp *dc;
2631 d_print_error (dpi);
2634 if (d_print_saw_error (dpi))
2640 d_print_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
2643 case D_COMP_QUAL_NAME:
2644 d_print_comp (dpi, d_left (dc));
2645 d_append_string (dpi, (dpi->options & DMGL_JAVA) == 0 ? "::" : ".");
2646 d_print_comp (dpi, d_right (dc));
2649 case D_COMP_TYPED_NAME:
2651 struct d_print_mod *hold_modifiers;
2652 struct d_comp *typed_name;
2653 struct d_print_mod adpm[4];
2655 struct d_print_template dpt;
2657 /* Pass the name down to the type so that it can be printed in
2658 the right place for the type. We also have to pass down
2659 any CV-qualifiers, which apply to the this parameter. */
2660 hold_modifiers = dpi->modifiers;
2662 typed_name = d_left (dc);
2663 while (typed_name != NULL)
2665 if (i >= sizeof adpm / sizeof adpm[0])
2667 d_print_error (dpi);
2671 adpm[i].next = dpi->modifiers;
2672 dpi->modifiers = &adpm[i];
2673 adpm[i].mod = typed_name;
2674 adpm[i].printed = 0;
2675 adpm[i].templates = dpi->templates;
2678 if (typed_name->type != D_COMP_RESTRICT_THIS
2679 && typed_name->type != D_COMP_VOLATILE_THIS
2680 && typed_name->type != D_COMP_CONST_THIS)
2683 typed_name = d_left (typed_name);
2686 /* If typed_name is a template, then it applies to the
2687 function type as well. */
2688 if (typed_name->type == D_COMP_TEMPLATE)
2690 dpt.next = dpi->templates;
2691 dpi->templates = &dpt;
2692 dpt.template = typed_name;
2695 d_print_comp (dpi, d_right (dc));
2697 if (typed_name->type == D_COMP_TEMPLATE)
2698 dpi->templates = dpt.next;
2700 /* If the modifiers didn't get printed by the type, print them
2705 if (! adpm[i].printed)
2707 d_append_char (dpi, ' ');
2708 d_print_mod (dpi, adpm[i].mod);
2712 dpi->modifiers = hold_modifiers;
2717 case D_COMP_TEMPLATE:
2719 struct d_print_mod *hold_dpm;
2721 /* Don't push modifiers into a template definition. Doing so
2722 could give the wrong definition for a template argument.
2723 Instead, treat the template essentially as a name. */
2725 hold_dpm = dpi->modifiers;
2726 dpi->modifiers = NULL;
2728 d_print_comp (dpi, d_left (dc));
2729 if (d_last_char (dpi) == '<')
2730 d_append_char (dpi, ' ');
2731 d_append_char (dpi, '<');
2732 d_print_comp (dpi, d_right (dc));
2733 /* Avoid generating two consecutive '>' characters, to avoid
2734 the C++ syntactic ambiguity. */
2735 if (d_last_char (dpi) == '>')
2736 d_append_char (dpi, ' ');
2737 d_append_char (dpi, '>');
2739 dpi->modifiers = hold_dpm;
2744 case D_COMP_TEMPLATE_PARAM:
2748 struct d_print_template *hold_dpt;
2750 if (dpi->templates == NULL)
2752 d_print_error (dpi);
2755 i = dc->u.s_number.number;
2756 for (a = d_right (dpi->templates->template);
2760 if (a->type != D_COMP_TEMPLATE_ARGLIST)
2762 d_print_error (dpi);
2769 if (i != 0 || a == NULL)
2771 d_print_error (dpi);
2775 /* While processing this parameter, we need to pop the list of
2776 templates. This is because the template parameter may
2777 itself be a reference to a parameter of an outer
2780 hold_dpt = dpi->templates;
2781 dpi->templates = hold_dpt->next;
2783 d_print_comp (dpi, d_left (a));
2785 dpi->templates = hold_dpt;
2791 d_print_comp (dpi, dc->u.s_ctor.name);
2795 d_append_char (dpi, '~');
2796 d_print_comp (dpi, dc->u.s_dtor.name);
2800 d_append_string (dpi, "vtable for ");
2801 d_print_comp (dpi, d_left (dc));
2805 d_append_string (dpi, "VTT for ");
2806 d_print_comp (dpi, d_left (dc));
2809 case D_COMP_CONSTRUCTION_VTABLE:
2810 d_append_string (dpi, "construction vtable for ");
2811 d_print_comp (dpi, d_left (dc));
2812 d_append_string (dpi, "-in-");
2813 d_print_comp (dpi, d_right (dc));
2816 case D_COMP_TYPEINFO:
2817 d_append_string (dpi, "typeinfo for ");
2818 d_print_comp (dpi, d_left (dc));
2821 case D_COMP_TYPEINFO_NAME:
2822 d_append_string (dpi, "typeinfo name for ");
2823 d_print_comp (dpi, d_left (dc));
2826 case D_COMP_TYPEINFO_FN:
2827 d_append_string (dpi, "typeinfo fn for ");
2828 d_print_comp (dpi, d_left (dc));
2832 d_append_string (dpi, "non-virtual thunk to ");
2833 d_print_comp (dpi, d_left (dc));
2836 case D_COMP_VIRTUAL_THUNK:
2837 d_append_string (dpi, "virtual thunk to ");
2838 d_print_comp (dpi, d_left (dc));
2841 case D_COMP_COVARIANT_THUNK:
2842 d_append_string (dpi, "covariant return thunk to ");
2843 d_print_comp (dpi, d_left (dc));
2846 case D_COMP_JAVA_CLASS:
2847 d_append_string (dpi, "java Class for ");
2848 d_print_comp (dpi, d_left (dc));
2852 d_append_string (dpi, "guard variable for ");
2853 d_print_comp (dpi, d_left (dc));
2856 case D_COMP_REFTEMP:
2857 d_append_string (dpi, "reference temporary for ");
2858 d_print_comp (dpi, d_left (dc));
2861 case D_COMP_SUB_STD:
2862 d_append_string (dpi, dc->u.s_string.string);
2865 case D_COMP_RESTRICT:
2866 case D_COMP_VOLATILE:
2868 case D_COMP_RESTRICT_THIS:
2869 case D_COMP_VOLATILE_THIS:
2870 case D_COMP_CONST_THIS:
2871 case D_COMP_VENDOR_TYPE_QUAL:
2872 case D_COMP_POINTER:
2873 case D_COMP_REFERENCE:
2874 case D_COMP_COMPLEX:
2875 case D_COMP_IMAGINARY:
2877 /* We keep a list of modifiers on the stack. */
2878 struct d_print_mod dpm;
2880 dpm.next = dpi->modifiers;
2881 dpi->modifiers = &dpm;
2884 dpm.templates = dpi->templates;
2886 d_print_comp (dpi, d_left (dc));
2888 /* If the modifier didn't get printed by the type, print it
2891 d_print_mod (dpi, dc);
2893 dpi->modifiers = dpm.next;
2898 case D_COMP_BUILTIN_TYPE:
2899 if ((dpi->options & DMGL_JAVA) == 0)
2900 d_append_string (dpi, dc->u.s_builtin.type->name);
2902 d_append_string (dpi, dc->u.s_builtin.type->java_name);
2905 case D_COMP_VENDOR_TYPE:
2906 d_print_comp (dpi, d_left (dc));
2909 case D_COMP_FUNCTION_TYPE:
2911 if (d_left (dc) != NULL)
2913 struct d_print_mod dpm;
2915 /* We must pass this type down as a modifier in order to
2916 print it in the right location. */
2918 dpm.next = dpi->modifiers;
2919 dpi->modifiers = &dpm;
2922 dpm.templates = dpi->templates;
2924 d_print_comp (dpi, d_left (dc));
2926 dpi->modifiers = dpm.next;
2931 d_append_char (dpi, ' ');
2934 d_print_function_type (dpi, dc, dpi->modifiers);
2939 case D_COMP_ARRAY_TYPE:
2941 struct d_print_mod dpm;
2943 /* We must pass this type down as a modifier in order to print
2944 multi-dimensional arrays correctly. */
2946 dpm.next = dpi->modifiers;
2947 dpi->modifiers = &dpm;
2950 dpm.templates = dpi->templates;
2952 d_print_comp (dpi, d_right (dc));
2954 dpi->modifiers = dpm.next;
2959 d_print_array_type (dpi, dc, dpi->modifiers);
2964 case D_COMP_PTRMEM_TYPE:
2966 struct d_print_mod dpm;
2968 dpm.next = dpi->modifiers;
2969 dpi->modifiers = &dpm;
2972 dpm.templates = dpi->templates;
2974 d_print_comp (dpi, d_right (dc));
2976 /* If the modifier didn't get printed by the type, print it
2980 d_append_char (dpi, ' ');
2981 d_print_comp (dpi, d_left (dc));
2982 d_append_string (dpi, "::*");
2985 dpi->modifiers = dpm.next;
2990 case D_COMP_ARGLIST:
2991 case D_COMP_TEMPLATE_ARGLIST:
2992 d_print_comp (dpi, d_left (dc));
2993 if (d_right (dc) != NULL)
2995 d_append_string (dpi, ", ");
2996 d_print_comp (dpi, d_right (dc));
3000 case D_COMP_OPERATOR:
3004 d_append_string (dpi, "operator");
3005 c = dc->u.s_operator.op->name[0];
3007 d_append_char (dpi, ' ');
3008 d_append_string (dpi, dc->u.s_operator.op->name);
3012 case D_COMP_EXTENDED_OPERATOR:
3013 d_append_string (dpi, "operator ");
3014 d_print_comp (dpi, dc->u.s_extended_operator.name);
3018 d_append_string (dpi, "operator ");
3019 d_print_cast (dpi, dc);
3023 if (d_left (dc)->type != D_COMP_CAST)
3024 d_print_expr_op (dpi, d_left (dc));
3027 d_append_string (dpi, "((");
3028 d_print_cast (dpi, d_left (dc));
3029 d_append_char (dpi, ')');
3031 d_append_char (dpi, '(');
3032 d_print_comp (dpi, d_right (dc));
3033 d_append_char (dpi, ')');
3034 if (d_left (dc)->type == D_COMP_CAST)
3035 d_append_char (dpi, ')');
3039 if (d_right (dc)->type != D_COMP_BINARY_ARGS)
3041 d_print_error (dpi);
3045 /* We wrap an expression which uses the greater-than operator in
3046 an extra layer of parens so that it does not get confused
3047 with the '>' which ends the template parameters. */
3048 if (d_left (dc)->type == D_COMP_OPERATOR
3049 && strcmp (d_left (dc)->u.s_operator.op->name, ">") == 0)
3050 d_append_char (dpi, '(');
3052 d_append_char (dpi, '(');
3053 d_print_comp (dpi, d_left (d_right (dc)));
3054 d_append_string (dpi, ") ");
3055 d_print_expr_op (dpi, d_left (dc));
3056 d_append_string (dpi, " (");
3057 d_print_comp (dpi, d_right (d_right (dc)));
3058 d_append_char (dpi, ')');
3060 if (d_left (dc)->type == D_COMP_OPERATOR
3061 && strcmp (d_left (dc)->u.s_operator.op->name, ">") == 0)
3062 d_append_char (dpi, ')');
3066 case D_COMP_BINARY_ARGS:
3067 /* We should only see this as part of D_COMP_BINARY. */
3068 d_print_error (dpi);
3071 case D_COMP_TRINARY:
3072 if (d_right (dc)->type != D_COMP_TRINARY_ARG1
3073 || d_right (d_right (dc))->type != D_COMP_TRINARY_ARG2)
3075 d_print_error (dpi);
3078 d_append_char (dpi, '(');
3079 d_print_comp (dpi, d_left (d_right (dc)));
3080 d_append_string (dpi, ") ");
3081 d_print_expr_op (dpi, d_left (dc));
3082 d_append_string (dpi, " (");
3083 d_print_comp (dpi, d_left (d_right (d_right (dc))));
3084 d_append_string (dpi, ") : (");
3085 d_print_comp (dpi, d_right (d_right (d_right (dc))));
3086 d_append_char (dpi, ')');
3089 case D_COMP_TRINARY_ARG1:
3090 case D_COMP_TRINARY_ARG2:
3091 /* We should only see these are part of D_COMP_TRINARY. */
3092 d_print_error (dpi);
3095 case D_COMP_LITERAL:
3096 case D_COMP_LITERAL_NEG:
3097 /* For some builtin types, produce simpler output. */
3098 if (d_left (dc)->type == D_COMP_BUILTIN_TYPE)
3100 switch (d_left (dc)->u.s_builtin.type->print)
3103 if (d_right (dc)->type == D_COMP_NAME)
3105 if (dc->type == D_COMP_LITERAL_NEG)
3106 d_append_char (dpi, '-');
3107 d_print_comp (dpi, d_right (dc));
3113 if (d_right (dc)->type == D_COMP_NAME)
3115 if (dc->type == D_COMP_LITERAL_NEG)
3116 d_append_char (dpi, '-');
3117 d_print_comp (dpi, d_right (dc));
3118 d_append_char (dpi, 'l');
3124 if (d_right (dc)->type == D_COMP_NAME
3125 && d_right (dc)->u.s_name.len == 1
3126 && dc->type == D_COMP_LITERAL)
3128 switch (d_right (dc)->u.s_name.s[0])
3131 d_append_string (dpi, "false");
3134 d_append_string (dpi, "true");
3147 d_append_char (dpi, '(');
3148 d_print_comp (dpi, d_left (dc));
3149 d_append_char (dpi, ')');
3150 if (dc->type == D_COMP_LITERAL_NEG)
3151 d_append_char (dpi, '-');
3152 d_print_comp (dpi, d_right (dc));
3156 d_print_error (dpi);
3161 /* Print an identifier. */
3164 d_print_identifier (dpi, name, len)
3165 struct d_print_info *dpi;
3169 if ((dpi->options & DMGL_JAVA) == 0)
3170 d_append_buffer (dpi, name, len);
3176 /* For Java we try to handle encoded extended Unicode
3177 characters. The C++ ABI doesn't mention Unicode encoding, so
3178 we don't it for C++. Characters are encoded as
3181 for (p = name; p < end; ++p)
3192 for (q = p + 3; q < end; ++q)
3198 else if (*q >= 'A' && *q <= 'F')
3199 dig = *q - 'A' + 10;
3200 else if (*q >= 'a' && *q <= 'f')
3201 dig = *q - 'a' + 10;
3207 /* If the Unicode character is larger than 256, we don't
3208 try to deal with it here. FIXME. */
3209 if (q < end && *q == '_' && c < 256)
3211 d_append_char (dpi, c);
3217 d_append_char (dpi, *p);
3222 /* Print a list of modifiers. SUFFIX is 1 if we are printing
3223 qualifiers on this after printing a function. */
3226 d_print_mod_list (dpi, mods, suffix)
3227 struct d_print_info *dpi;
3228 struct d_print_mod *mods;
3231 struct d_print_template *hold_dpt;
3233 if (mods == NULL || d_print_saw_error (dpi))
3238 && (mods->mod->type == D_COMP_RESTRICT_THIS
3239 || mods->mod->type == D_COMP_VOLATILE_THIS
3240 || mods->mod->type == D_COMP_CONST_THIS)))
3242 d_print_mod_list (dpi, mods->next, suffix);
3248 hold_dpt = dpi->templates;
3249 dpi->templates = mods->templates;
3251 if (mods->mod->type == D_COMP_FUNCTION_TYPE)
3253 d_print_function_type (dpi, mods->mod, mods->next);
3254 dpi->templates = hold_dpt;
3257 else if (mods->mod->type == D_COMP_ARRAY_TYPE)
3259 d_print_array_type (dpi, mods->mod, mods->next);
3260 dpi->templates = hold_dpt;
3264 d_print_mod (dpi, mods->mod);
3266 dpi->templates = hold_dpt;
3268 d_print_mod_list (dpi, mods->next, suffix);
3271 /* Print a modifier. */
3274 d_print_mod (dpi, mod)
3275 struct d_print_info *dpi;
3276 const struct d_comp *mod;
3280 case D_COMP_RESTRICT:
3281 case D_COMP_RESTRICT_THIS:
3282 d_append_string (dpi, " restrict");
3284 case D_COMP_VOLATILE:
3285 case D_COMP_VOLATILE_THIS:
3286 d_append_string (dpi, " volatile");
3289 case D_COMP_CONST_THIS:
3290 d_append_string (dpi, " const");
3292 case D_COMP_VENDOR_TYPE_QUAL:
3293 d_append_char (dpi, ' ');
3294 d_print_comp (dpi, d_right (mod));
3296 case D_COMP_POINTER:
3297 /* There is no pointer symbol in Java. */
3298 if ((dpi->options & DMGL_JAVA) == 0)
3299 d_append_char (dpi, '*');
3301 case D_COMP_REFERENCE:
3302 d_append_char (dpi, '&');
3304 case D_COMP_COMPLEX:
3305 d_append_string (dpi, "complex ");
3307 case D_COMP_IMAGINARY:
3308 d_append_string (dpi, "imaginary ");
3310 case D_COMP_PTRMEM_TYPE:
3311 if (d_last_char (dpi) != '(')
3312 d_append_char (dpi, ' ');
3313 d_print_comp (dpi, d_left (mod));
3314 d_append_string (dpi, "::*");
3316 case D_COMP_TYPED_NAME:
3317 d_print_comp (dpi, d_left (mod));
3320 /* Otherwise, we have something that won't go back on the
3321 modifier stack, so we can just print it. */
3322 d_print_comp (dpi, mod);
3327 /* Print a function type, except for the return type. */
3330 d_print_function_type (dpi, dc, mods)
3331 struct d_print_info *dpi;
3332 const struct d_comp *dc;
3333 struct d_print_mod *mods;
3337 struct d_print_mod *p;
3341 for (p = mods; p != NULL; p = p->next)
3347 switch (p->mod->type)
3349 case D_COMP_RESTRICT:
3350 case D_COMP_VOLATILE:
3352 case D_COMP_VENDOR_TYPE_QUAL:
3353 case D_COMP_POINTER:
3354 case D_COMP_REFERENCE:
3355 case D_COMP_COMPLEX:
3356 case D_COMP_IMAGINARY:
3357 case D_COMP_PTRMEM_TYPE:
3360 case D_COMP_RESTRICT_THIS:
3361 case D_COMP_VOLATILE_THIS:
3362 case D_COMP_CONST_THIS:
3371 if (d_left (dc) != NULL && ! saw_mod)
3376 switch (d_last_char (dpi))
3384 d_append_char (dpi, ' ');
3388 d_append_char (dpi, '(');
3391 d_print_mod_list (dpi, mods, 0);
3394 d_append_char (dpi, ')');
3396 d_append_char (dpi, '(');
3398 if (d_right (dc) != NULL)
3399 d_print_comp (dpi, d_right (dc));
3401 d_append_char (dpi, ')');
3403 d_print_mod_list (dpi, mods, 1);
3406 /* Print an array type, except for the element type. */
3409 d_print_array_type (dpi, dc, mods)
3410 struct d_print_info *dpi;
3411 const struct d_comp *dc;
3412 struct d_print_mod *mods;
3420 struct d_print_mod *p;
3423 for (p = mods; p != NULL; p = p->next)
3428 if (p->mod->type == D_COMP_ARRAY_TYPE)
3442 d_append_string (dpi, " (");
3444 d_print_mod_list (dpi, mods, 0);
3447 d_append_char (dpi, ')');
3451 d_append_char (dpi, ' ');
3453 d_append_char (dpi, '[');
3455 if (d_left (dc) != NULL)
3456 d_print_comp (dpi, d_left (dc));
3458 d_append_char (dpi, ']');
3461 /* Print an operator in an expression. */
3464 d_print_expr_op (dpi, dc)
3465 struct d_print_info *dpi;
3466 const struct d_comp *dc;
3468 if (dc->type == D_COMP_OPERATOR)
3469 d_append_string (dpi, dc->u.s_operator.op->name);
3471 d_print_comp (dpi, dc);
3477 d_print_cast (dpi, dc)
3478 struct d_print_info *dpi;
3479 const struct d_comp *dc;
3481 if (d_left (dc)->type != D_COMP_TEMPLATE)
3482 d_print_comp (dpi, d_left (dc));
3485 struct d_print_mod *hold_dpm;
3486 struct d_print_template dpt;
3488 /* It appears that for a templated cast operator, we need to put
3489 the template parameters in scope for the operator name, but
3490 not for the parameters. The effect is that we need to handle
3491 the template printing here. */
3493 hold_dpm = dpi->modifiers;
3494 dpi->modifiers = NULL;
3496 dpt.next = dpi->templates;
3497 dpi->templates = &dpt;
3498 dpt.template = d_left (dc);
3500 d_print_comp (dpi, d_left (d_left (dc)));
3502 dpi->templates = dpt.next;
3504 if (d_last_char (dpi) == '<')
3505 d_append_char (dpi, ' ');
3506 d_append_char (dpi, '<');
3507 d_print_comp (dpi, d_right (d_left (dc)));
3508 /* Avoid generating two consecutive '>' characters, to avoid
3509 the C++ syntactic ambiguity. */
3510 if (d_last_char (dpi) == '>')
3511 d_append_char (dpi, ' ');
3512 d_append_char (dpi, '>');
3514 dpi->modifiers = hold_dpm;
3518 /* Initialize the information structure we use to pass around
3522 d_init_info (mangled, options, len, di)
3523 const char *mangled;
3529 di->options = options;
3533 /* We can not need more components than twice the number of chars in
3534 the mangled string. Most components correspond directly to
3535 chars, but the ARGLIST types are exceptions. */
3536 di->num_comps = 2 * len;
3537 di->comps = (struct d_comp *) malloc (di->num_comps
3538 * sizeof (struct d_comp));
3541 /* Similarly, we can not need more substitutions than there are
3542 chars in the mangled string. */
3544 di->subs = (struct d_comp **) malloc (di->num_subs
3545 * sizeof (struct d_comp *));
3548 di->last_name = NULL;
3550 if (di->comps == NULL || di->subs == NULL)
3552 if (di->comps != NULL)
3554 if (di->subs != NULL)
3562 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
3563 name, return a buffer allocated with malloc holding the demangled
3564 name. OPTIONS is the usual libiberty demangler options. On
3565 success, this sets *PALC to the allocated size of the returned
3566 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
3567 a memory allocation failure. On failure, this returns NULL. */
3570 d_demangle (mangled, options, palc)
3571 const char* mangled;
3583 len = strlen (mangled);
3585 if (mangled[0] == '_' && mangled[1] == 'Z')
3587 else if (strncmp (mangled, "_GLOBAL_", 8) == 0
3588 && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
3589 && (mangled[9] == 'D' || mangled[9] == 'I')
3590 && mangled[10] == '_')
3594 r = malloc (40 + len - 11);
3599 if (mangled[9] == 'I')
3600 strcpy (r, "global constructors keyed to ");
3602 strcpy (r, "global destructors keyed to ");
3603 strcat (r, mangled + 11);
3609 if ((options & DMGL_TYPES) == 0)
3614 if (! d_init_info (mangled, options, len, &di))
3621 dc = d_mangled_name (&di, 1);
3625 /* If we didn't consume the entire mangled string, then we didn't
3626 successfully demangle it. */
3627 if (d_peek_char (&di) != '\0')
3630 #ifdef CP_DEMANGLE_DEBUG
3632 printf ("failed demangling\n");
3642 ret = d_print (options, dc, palc);
3649 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
3651 extern char *__cxa_demangle PARAMS ((const char *, char *, size_t *, int *));
3653 /* ia64 ABI-mandated entry point in the C++ runtime library for
3654 performing demangling. MANGLED_NAME is a NUL-terminated character
3655 string containing the name to be demangled.
3657 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
3658 *LENGTH bytes, into which the demangled name is stored. If
3659 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
3660 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
3661 is placed in a region of memory allocated with malloc.
3663 If LENGTH is non-NULL, the length of the buffer conaining the
3664 demangled name, is placed in *LENGTH.
3666 The return value is a pointer to the start of the NUL-terminated
3667 demangled name, or NULL if the demangling fails. The caller is
3668 responsible for deallocating this memory using free.
3670 *STATUS is set to one of the following values:
3671 0: The demangling operation succeeded.
3672 -1: A memory allocation failure occurred.
3673 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
3674 -3: One of the arguments is invalid.
3676 The demangling is performed using the C++ ABI mangling rules, with
3680 __cxa_demangle (mangled_name, output_buffer, length, status)
3681 const char *mangled_name;
3682 char *output_buffer;
3692 if (mangled_name == NULL)
3698 if (output_buffer != NULL && length == NULL)
3704 demangled = d_demangle (mangled_name, DMGL_TYPES, &alc);
3706 if (demangled == NULL)
3715 if (output_buffer == NULL)
3722 if (strlen (demangled) < *length)
3724 strcpy (output_buffer, demangled);
3726 demangled = output_buffer;
3730 free (output_buffer);
3740 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
3742 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
3743 mangled name, return a buffer allocated with malloc holding the
3744 demangled name. Otherwise, return NULL. */
3747 cplus_demangle_v3 (mangled, options)
3748 const char* mangled;
3753 return d_demangle (mangled, options, &alc);
3756 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
3757 conventions, but the output formatting is a little different.
3758 This instructs the C++ demangler not to emit pointer characters ("*"), and
3759 to use Java's namespace separator symbol ("." instead of "::"). It then
3760 does an additional pass over the demangled output to replace instances
3761 of JArray<TYPE> with TYPE[]. */
3764 java_demangle_v3 (mangled)
3765 const char* mangled;
3773 demangled = d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS, &alc);
3775 if (demangled == NULL)
3781 while (*from != '\0')
3783 if (strncmp (from, "JArray<", 7) == 0)
3788 else if (nesting > 0 && *from == '>')
3790 while (to > demangled && to[-1] == ' ')
3806 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
3808 #ifndef IN_GLIBCPP_V3
3810 /* Demangle a string in order to find out whether it is a constructor
3811 or destructor. Return non-zero on success. Set *CTOR_KIND and
3812 *DTOR_KIND appropriately. */
3815 is_ctor_or_dtor (mangled, ctor_kind, dtor_kind)
3816 const char *mangled;
3817 enum gnu_v3_ctor_kinds *ctor_kind;
3818 enum gnu_v3_dtor_kinds *dtor_kind;
3824 *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
3825 *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
3827 if (! d_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di))
3830 dc = d_mangled_name (&di, 1);
3833 if (d_peek_char (&di) == '\0')
3842 case D_COMP_TYPED_NAME:
3843 case D_COMP_TEMPLATE:
3844 case D_COMP_RESTRICT_THIS:
3845 case D_COMP_VOLATILE_THIS:
3846 case D_COMP_CONST_THIS:
3849 case D_COMP_QUAL_NAME:
3853 *ctor_kind = dc->u.s_ctor.kind;
3858 *dtor_kind = dc->u.s_dtor.kind;
3872 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
3873 name. A non-zero return indicates the type of constructor. */
3875 enum gnu_v3_ctor_kinds
3876 is_gnu_v3_mangled_ctor (name)
3879 enum gnu_v3_ctor_kinds ctor_kind;
3880 enum gnu_v3_dtor_kinds dtor_kind;
3882 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
3883 return (enum gnu_v3_ctor_kinds) 0;
3888 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
3889 name. A non-zero return indicates the type of destructor. */
3891 enum gnu_v3_dtor_kinds
3892 is_gnu_v3_mangled_dtor (name)
3895 enum gnu_v3_ctor_kinds ctor_kind;
3896 enum gnu_v3_dtor_kinds dtor_kind;
3898 if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
3899 return (enum gnu_v3_dtor_kinds) 0;
3903 #endif /* IN_GLIBCPP_V3 */
3905 #ifdef STANDALONE_DEMANGLER
3908 #include "dyn-string.h"
3910 static void print_usage PARAMS ((FILE* fp, int exit_value));
3912 #define IS_ALPHA(CHAR) \
3913 (((CHAR) >= 'a' && (CHAR) <= 'z') \
3914 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
3916 /* Non-zero if CHAR is a character than can occur in a mangled name. */
3917 #define is_mangled_char(CHAR) \
3918 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
3919 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
3921 /* The name of this program, as invoked. */
3922 const char* program_name;
3924 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
3927 print_usage (fp, exit_value)
3931 fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
3932 fprintf (fp, "Options:\n");
3933 fprintf (fp, " -h,--help Display this message.\n");
3934 fprintf (fp, " -p,--no-params Don't display function parameters\n");
3935 fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
3936 fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
3941 /* Option specification for getopt_long. */
3942 static const struct option long_options[] =
3944 { "help", no_argument, NULL, 'h' },
3945 { "no-params", no_argument, NULL, 'p' },
3946 { "verbose", no_argument, NULL, 'v' },
3947 { NULL, no_argument, NULL, 0 },
3950 /* Main entry for a demangling filter executable. It will demangle
3951 its command line arguments, if any. If none are provided, it will
3952 filter stdin to stdout, replacing any recognized mangled C++ names
3953 with their demangled equivalents. */
3962 int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
3964 /* Use the program name of this program, as invoked. */
3965 program_name = argv[0];
3967 /* Parse options. */
3970 opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
3973 case '?': /* Unrecognized option. */
3974 print_usage (stderr, 1);
3978 print_usage (stdout, 0);
3982 options &= ~ DMGL_PARAMS;
3986 options |= DMGL_VERBOSE;
3990 while (opt_char != -1);
3993 /* No command line arguments were provided. Filter stdin. */
3995 dyn_string_t mangled = dyn_string_new (3);
3998 /* Read all of input. */
3999 while (!feof (stdin))
4003 /* Pile characters into mangled until we hit one that can't
4004 occur in a mangled name. */
4006 while (!feof (stdin) && is_mangled_char (c))
4008 dyn_string_append_char (mangled, c);
4014 if (dyn_string_length (mangled) > 0)
4016 s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
4025 /* It might not have been a mangled name. Print the
4027 fputs (dyn_string_buf (mangled), stdout);
4030 dyn_string_clear (mangled);
4033 /* If we haven't hit EOF yet, we've read one character that
4034 can't occur in a mangled name, so print it out. */
4039 dyn_string_delete (mangled);
4042 /* Demangle command line arguments. */
4044 /* Loop over command line arguments. */
4045 for (i = optind; i < argc; ++i)
4049 /* Attempt to demangle. */
4050 s = cplus_demangle_v3 (argv[i], options);
4052 /* If it worked, print the demangled name. */
4059 fprintf (stderr, "Failed: %s\n", argv[i]);
4066 #endif /* STANDALONE_DEMANGLER */