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. */
175 /* A template parameter. */
176 D_COMP_TEMPLATE_PARAM
,
183 /* A VTT structure. */
185 /* A construction vtable. */
186 D_COMP_CONSTRUCTION_VTABLE
,
187 /* A typeinfo structure. */
189 /* A typeinfo name. */
190 D_COMP_TYPEINFO_NAME
,
191 /* A typeinfo function. */
195 /* A virtual thunk. */
196 D_COMP_VIRTUAL_THUNK
,
197 /* A covariant thunk. */
198 D_COMP_COVARIANT_THUNK
,
201 /* A guard variable. */
203 /* A reference temporary. */
205 /* A standard substitution. */
207 /* The restrict qualifier. */
209 /* The volatile qualifier. */
211 /* The const qualifier. */
213 /* The restrict qualifier modifying a member function. */
214 D_COMP_RESTRICT_THIS
,
215 /* The volatile qualifier modifying a member function. */
216 D_COMP_VOLATILE_THIS
,
217 /* The const qualifier modifying a member function. */
219 /* A vendor qualifier. */
220 D_COMP_VENDOR_TYPE_QUAL
,
225 /* A complex type. */
227 /* An imaginary type. */
229 /* A builtin type. */
231 /* A vendor's builtin type. */
233 /* A function type. */
234 D_COMP_FUNCTION_TYPE
,
237 /* A pointer to member type. */
239 /* An argument list. */
241 /* A template argument list. */
242 D_COMP_TEMPLATE_ARGLIST
,
245 /* An extended operator. */
246 D_COMP_EXTENDED_OPERATOR
,
249 /* A unary expression. */
251 /* A binary expression. */
253 /* Arguments to a binary expression. */
255 /* A trinary expression. */
257 /* Arguments to a trinary expression. */
262 /* A negative literal. */
266 /* A component of the mangled name. */
270 /* The type of this component. */
271 enum d_comp_type type
;
274 /* For D_COMP_NAME. */
277 /* A pointer to the name (not NULL terminated) and it's
283 /* For D_COMP_OPERATOR. */
287 const struct d_operator_info
*op
;
290 /* For D_COMP_EXTENDED_OPERATOR. */
293 /* Number of arguments. */
297 } s_extended_operator
;
299 /* For D_COMP_CTOR. */
302 enum gnu_v3_ctor_kinds kind
;
306 /* For D_COMP_DTOR. */
309 enum gnu_v3_dtor_kinds kind
;
313 /* For D_COMP_BUILTIN_TYPE. */
316 const struct d_builtin_type_info
*type
;
319 /* For D_COMP_SUB_STD. */
325 /* For D_COMP_TEMPLATE_PARAM. */
331 /* For other types. */
335 struct d_comp
*right
;
341 #define d_left(dc) ((dc)->u.s_binary.left)
342 #define d_right(dc) ((dc)->u.s_binary.right)
344 /* The information structure we pass around. */
348 /* The string we are demangling. */
350 /* The options passed to the demangler. */
352 /* The next character in the string to consider. */
354 /* The array of components. */
355 struct d_comp
*comps
;
356 /* The index of the next available component. */
358 /* The number of available component structures. */
360 /* The array of substitutions. */
361 struct d_comp
**subs
;
362 /* The index of the next substitution. */
364 /* The number of available entries in the subs array. */
366 /* The last name we saw, for constructors and destructors. */
367 struct d_comp
*last_name
;
370 #define d_peek_char(di) (*((di)->n))
371 #define d_peek_next_char(di) ((di)->n[1])
372 #define d_advance(di, i) ((di)->n += (i))
373 #define d_next_char(di) (*((di)->n++))
374 #define d_str(di) ((di)->n)
376 /* A list of templates. This is used while printing. */
378 struct d_print_template
380 /* Next template on the list. */
381 struct d_print_template
*next
;
383 const struct d_comp
*template;
386 /* A list of type modifiers. This is used while printing. */
390 /* Next modifier on the list. These are in the reverse of the order
391 in which they appeared in the mangled string. */
392 struct d_print_mod
*next
;
394 const struct d_comp
*mod
;
395 /* Whether this modifier was printed. */
397 /* The list of templates which applies to this modifier. */
398 struct d_print_template
*templates
;
401 /* We use this structure to hold information during printing. */
405 /* The options passed to the demangler. */
407 /* Buffer holding the result. */
409 /* Current length of data in buffer. */
411 /* Allocated size of buffer. */
413 /* The current list of templates, if any. */
414 struct d_print_template
*templates
;
415 /* The current list of modifiers (e.g., pointer, reference, etc.),
417 struct d_print_mod
*modifiers
;
418 /* Set to 1 if we had a memory allocation failure. */
419 int allocation_failure
;
422 #define d_print_saw_error(dpi) ((dpi)->buf == NULL)
424 #define d_append_char(dpi, c) \
427 if ((dpi)->buf != NULL && (dpi)->len < (dpi)->alc) \
428 (dpi)->buf[(dpi)->len++] = (c); \
430 d_print_append_char ((dpi), (c)); \
434 #define d_append_buffer(dpi, s, l) \
437 if ((dpi)->buf != NULL && (dpi)->len + (l) <= (dpi)->alc) \
439 memcpy ((dpi)->buf + (dpi)->len, (s), (l)); \
443 d_print_append_buffer ((dpi), (s), (l)); \
447 #define d_append_string(dpi, s) \
450 size_t d_append_string_len = strlen (s); \
451 d_append_buffer ((dpi), (s), d_append_string_len); \
455 #define d_last_char(dpi) \
456 ((dpi)->buf == NULL || (dpi)->len == 0 ? '\0' : (dpi)->buf[(dpi)->len - 1])
458 #ifdef CP_DEMANGLE_DEBUG
459 static void d_dump
PARAMS ((struct d_comp
*, int));
461 static struct d_comp
*d_make_empty
PARAMS ((struct d_info
*,
463 static struct d_comp
*d_make_comp
PARAMS ((struct d_info
*, enum d_comp_type
,
464 struct d_comp
*, struct d_comp
*));
465 static struct d_comp
*d_make_name
PARAMS ((struct d_info
*, const char *,
467 static struct d_comp
*d_make_builtin_type
PARAMS ((struct d_info
*,
468 const struct d_builtin_type_info
*));
469 static struct d_comp
*d_make_operator
PARAMS ((struct d_info
*,
470 const struct d_operator_info
*));
471 static struct d_comp
*d_make_extended_operator
PARAMS ((struct d_info
*,
474 static struct d_comp
*d_make_ctor
PARAMS ((struct d_info
*,
475 enum gnu_v3_ctor_kinds
,
477 static struct d_comp
*d_make_dtor
PARAMS ((struct d_info
*,
478 enum gnu_v3_dtor_kinds
,
480 static struct d_comp
*d_make_template_param
PARAMS ((struct d_info
*, long));
481 static struct d_comp
*d_make_sub
PARAMS ((struct d_info
*, const char *));
482 static struct d_comp
*d_mangled_name
PARAMS ((struct d_info
*, int));
483 static int has_return_type
PARAMS ((struct d_comp
*));
484 static int is_ctor_dtor_or_conversion
PARAMS ((struct d_comp
*));
485 static struct d_comp
*d_encoding
PARAMS ((struct d_info
*, int));
486 static struct d_comp
*d_name
PARAMS ((struct d_info
*));
487 static struct d_comp
*d_nested_name
PARAMS ((struct d_info
*));
488 static struct d_comp
*d_prefix
PARAMS ((struct d_info
*));
489 static struct d_comp
*d_unqualified_name
PARAMS ((struct d_info
*));
490 static struct d_comp
*d_source_name
PARAMS ((struct d_info
*));
491 static long d_number
PARAMS ((struct d_info
*));
492 static struct d_comp
*d_identifier
PARAMS ((struct d_info
*, int));
493 static struct d_comp
*d_operator_name
PARAMS ((struct d_info
*));
494 static struct d_comp
*d_special_name
PARAMS ((struct d_info
*));
495 static int d_call_offset
PARAMS ((struct d_info
*, int));
496 static struct d_comp
*d_ctor_dtor_name
PARAMS ((struct d_info
*));
497 static struct d_comp
*d_type
PARAMS ((struct d_info
*));
498 static struct d_comp
**d_cv_qualifiers
PARAMS ((struct d_info
*,
499 struct d_comp
**, int));
500 static struct d_comp
*d_function_type
PARAMS ((struct d_info
*));
501 static struct d_comp
*d_bare_function_type
PARAMS ((struct d_info
*, int));
502 static struct d_comp
*d_class_enum_type
PARAMS ((struct d_info
*));
503 static struct d_comp
*d_array_type
PARAMS ((struct d_info
*));
504 static struct d_comp
*d_pointer_to_member_type
PARAMS ((struct d_info
*));
505 static struct d_comp
*d_template_param
PARAMS ((struct d_info
*));
506 static struct d_comp
*d_template_args
PARAMS ((struct d_info
*));
507 static struct d_comp
*d_template_arg
PARAMS ((struct d_info
*));
508 static struct d_comp
*d_expression
PARAMS ((struct d_info
*));
509 static struct d_comp
*d_expr_primary
PARAMS ((struct d_info
*));
510 static struct d_comp
*d_local_name
PARAMS ((struct d_info
*));
511 static int d_discriminator
PARAMS ((struct d_info
*));
512 static int d_add_substitution
PARAMS ((struct d_info
*, struct d_comp
*));
513 static struct d_comp
*d_substitution
PARAMS ((struct d_info
*, int));
514 static void d_print_resize
PARAMS ((struct d_print_info
*, size_t));
515 static void d_print_append_char
PARAMS ((struct d_print_info
*, int));
516 static void d_print_append_buffer
PARAMS ((struct d_print_info
*, const char *,
518 static void d_print_error
PARAMS ((struct d_print_info
*));
519 static char *d_print
PARAMS ((int, const struct d_comp
*, size_t *));
520 static void d_print_comp
PARAMS ((struct d_print_info
*,
521 const struct d_comp
*));
522 static void d_print_identifier
PARAMS ((struct d_print_info
*, const char *,
524 static void d_print_mod_list
PARAMS ((struct d_print_info
*,
525 struct d_print_mod
*, int));
526 static void d_print_mod
PARAMS ((struct d_print_info
*,
527 const struct d_comp
*));
528 static void d_print_function_type
PARAMS ((struct d_print_info
*,
529 const struct d_comp
*,
530 struct d_print_mod
*));
531 static void d_print_array_type
PARAMS ((struct d_print_info
*,
532 const struct d_comp
*,
533 struct d_print_mod
*));
534 static void d_print_expr_op
PARAMS ((struct d_print_info
*,
535 const struct d_comp
*));
536 static void d_print_cast
PARAMS ((struct d_print_info
*,
537 const struct d_comp
*));
538 static int d_init_info
PARAMS ((const char *, int, size_t, struct d_info
*));
539 static char *d_demangle
PARAMS ((const char *, int, size_t *));
541 #ifdef CP_DEMANGLE_DEBUG
553 for (i
= 0; i
< indent
; ++i
)
559 printf ("name '%.*s'\n", dc
->u
.s_name
.len
, dc
->u
.s_name
.s
);
561 case D_COMP_TEMPLATE_PARAM
:
562 printf ("template parameter %ld\n", dc
->u
.s_number
.number
);
565 printf ("constructor %d\n", (int) dc
->u
.s_ctor
.kind
);
566 d_dump (dc
->u
.s_ctor
.name
, indent
+ 2);
569 printf ("destructor %d\n", (int) dc
->u
.s_dtor
.kind
);
570 d_dump (dc
->u
.s_dtor
.name
, indent
+ 2);
573 printf ("standard substitution %s\n", dc
->u
.s_string
.string
);
575 case D_COMP_BUILTIN_TYPE
:
576 printf ("builtin type %s\n", dc
->u
.s_builtin
.type
->name
);
578 case D_COMP_OPERATOR
:
579 printf ("operator %s\n", dc
->u
.s_operator
.op
->name
);
581 case D_COMP_EXTENDED_OPERATOR
:
582 printf ("extended operator with %d args\n",
583 dc
->u
.s_extended_operator
.args
);
584 d_dump (dc
->u
.s_extended_operator
.name
, indent
+ 2);
587 case D_COMP_QUAL_NAME
:
588 printf ("qualified name\n");
590 case D_COMP_LOCAL_NAME
:
591 printf ("local name\n");
593 case D_COMP_TYPED_NAME
:
594 printf ("typed name\n");
596 case D_COMP_TEMPLATE
:
597 printf ("template\n");
605 case D_COMP_CONSTRUCTION_VTABLE
:
606 printf ("construction vtable\n");
608 case D_COMP_TYPEINFO
:
609 printf ("typeinfo\n");
611 case D_COMP_TYPEINFO_NAME
:
612 printf ("typeinfo name\n");
614 case D_COMP_TYPEINFO_FN
:
615 printf ("typeinfo function\n");
620 case D_COMP_VIRTUAL_THUNK
:
621 printf ("virtual thunk\n");
623 case D_COMP_COVARIANT_THUNK
:
624 printf ("covariant thunk\n");
626 case D_COMP_JAVA_CLASS
:
627 printf ("java class\n");
633 printf ("reference temporary\n");
635 case D_COMP_RESTRICT
:
636 printf ("restrict\n");
638 case D_COMP_VOLATILE
:
639 printf ("volatile\n");
644 case D_COMP_RESTRICT_THIS
:
645 printf ("restrict this\n");
647 case D_COMP_VOLATILE_THIS
:
648 printf ("volatile this\n");
650 case D_COMP_CONST_THIS
:
651 printf ("const this\n");
653 case D_COMP_VENDOR_TYPE_QUAL
:
654 printf ("vendor type qualifier\n");
657 printf ("pointer\n");
659 case D_COMP_REFERENCE
:
660 printf ("reference\n");
663 printf ("complex\n");
665 case D_COMP_IMAGINARY
:
666 printf ("imaginary\n");
668 case D_COMP_VENDOR_TYPE
:
669 printf ("vendor type\n");
671 case D_COMP_FUNCTION_TYPE
:
672 printf ("function type\n");
674 case D_COMP_ARRAY_TYPE
:
675 printf ("array type\n");
677 case D_COMP_PTRMEM_TYPE
:
678 printf ("pointer to member type\n");
681 printf ("argument list\n");
683 case D_COMP_TEMPLATE_ARGLIST
:
684 printf ("template argument list\n");
690 printf ("unary operator\n");
693 printf ("binary operator\n");
695 case D_COMP_BINARY_ARGS
:
696 printf ("binary operator arguments\n");
699 printf ("trinary operator\n");
701 case D_COMP_TRINARY_ARG1
:
702 printf ("trinary operator arguments 1\n");
704 case D_COMP_TRINARY_ARG2
:
705 printf ("trinary operator arguments 1\n");
708 printf ("literal\n");
710 case D_COMP_LITERAL_NEG
:
711 printf ("negative literal\n");
715 d_dump (d_left (dc
), indent
+ 2);
716 d_dump (d_right (dc
), indent
+ 2);
719 #endif /* CP_DEMANGLE_DEBUG */
721 /* Add a new component. */
723 static struct d_comp
*
724 d_make_empty (di
, type
)
726 enum d_comp_type type
;
730 if (di
->next_comp
>= di
->num_comps
)
732 p
= &di
->comps
[di
->next_comp
];
738 /* Add a new generic component. */
740 static struct d_comp
*
741 d_make_comp (di
, type
, left
, right
)
743 enum d_comp_type type
;
745 struct d_comp
*right
;
749 /* We check for errors here. A typical error would be a NULL return
750 from a subroutine. We catch those here, and return NULL
754 /* These types require two parameters. */
755 case D_COMP_QUAL_NAME
:
756 case D_COMP_LOCAL_NAME
:
757 case D_COMP_TYPED_NAME
:
758 case D_COMP_TEMPLATE
:
759 case D_COMP_VENDOR_TYPE_QUAL
:
760 case D_COMP_PTRMEM_TYPE
:
763 case D_COMP_BINARY_ARGS
:
765 case D_COMP_TRINARY_ARG1
:
766 case D_COMP_TRINARY_ARG2
:
768 case D_COMP_LITERAL_NEG
:
769 if (left
== NULL
|| right
== NULL
)
773 /* These types only require one parameter. */
776 case D_COMP_CONSTRUCTION_VTABLE
:
777 case D_COMP_TYPEINFO
:
778 case D_COMP_TYPEINFO_NAME
:
779 case D_COMP_TYPEINFO_FN
:
781 case D_COMP_VIRTUAL_THUNK
:
782 case D_COMP_COVARIANT_THUNK
:
783 case D_COMP_JAVA_CLASS
:
787 case D_COMP_REFERENCE
:
789 case D_COMP_IMAGINARY
:
790 case D_COMP_VENDOR_TYPE
:
792 case D_COMP_TEMPLATE_ARGLIST
:
798 /* This needs a right parameter, but the left parameter can be
800 case D_COMP_ARRAY_TYPE
:
805 /* These are allowed to have no parameters--in some cases they
806 will be filled in later. */
807 case D_COMP_FUNCTION_TYPE
:
808 case D_COMP_RESTRICT
:
809 case D_COMP_VOLATILE
:
811 case D_COMP_RESTRICT_THIS
:
812 case D_COMP_VOLATILE_THIS
:
813 case D_COMP_CONST_THIS
:
816 /* Other types should not be seen here. */
821 p
= d_make_empty (di
, type
);
824 p
->u
.s_binary
.left
= left
;
825 p
->u
.s_binary
.right
= right
;
830 /* Add a new name component. */
832 static struct d_comp
*
833 d_make_name (di
, s
, len
)
840 if (s
== NULL
|| len
== 0)
842 p
= d_make_empty (di
, D_COMP_NAME
);
846 p
->u
.s_name
.len
= len
;
851 /* Add a new builtin type component. */
853 static struct d_comp
*
854 d_make_builtin_type (di
, type
)
856 const struct d_builtin_type_info
*type
;
862 p
= d_make_empty (di
, D_COMP_BUILTIN_TYPE
);
864 p
->u
.s_builtin
.type
= type
;
868 /* Add a new operator component. */
870 static struct d_comp
*
871 d_make_operator (di
, op
)
873 const struct d_operator_info
*op
;
877 p
= d_make_empty (di
, D_COMP_OPERATOR
);
879 p
->u
.s_operator
.op
= op
;
883 /* Add a new extended operator component. */
885 static struct d_comp
*
886 d_make_extended_operator (di
, args
, name
)
895 p
= d_make_empty (di
, D_COMP_EXTENDED_OPERATOR
);
898 p
->u
.s_extended_operator
.args
= args
;
899 p
->u
.s_extended_operator
.name
= name
;
904 /* Add a new constructor component. */
906 static struct d_comp
*
907 d_make_ctor (di
, kind
, name
)
909 enum gnu_v3_ctor_kinds kind
;
916 p
= d_make_empty (di
, D_COMP_CTOR
);
919 p
->u
.s_ctor
.kind
= kind
;
920 p
->u
.s_ctor
.name
= name
;
925 /* Add a new destructor component. */
927 static struct d_comp
*
928 d_make_dtor (di
, kind
, name
)
930 enum gnu_v3_dtor_kinds kind
;
937 p
= d_make_empty (di
, D_COMP_DTOR
);
940 p
->u
.s_dtor
.kind
= kind
;
941 p
->u
.s_dtor
.name
= name
;
946 /* Add a new template parameter. */
948 static struct d_comp
*
949 d_make_template_param (di
, i
)
955 p
= d_make_empty (di
, D_COMP_TEMPLATE_PARAM
);
957 p
->u
.s_number
.number
= i
;
961 /* Add a new standard substitution component. */
963 static struct d_comp
*
964 d_make_sub (di
, name
)
970 p
= d_make_empty (di
, D_COMP_SUB_STD
);
972 p
->u
.s_string
.string
= name
;
976 /* <mangled-name> ::= _Z <encoding>
978 TOP_LEVEL is non-zero when called at the top level. */
980 static struct d_comp
*
981 d_mangled_name (di
, top_level
)
985 if (d_next_char (di
) != '_')
987 if (d_next_char (di
) != 'Z')
989 return d_encoding (di
, top_level
);
992 /* Return whether a function should have a return type. The argument
993 is the function name, which may be qualified in various ways. The
994 rules are that template functions have return types with some
995 exceptions, function types which are not part of a function name
996 mangling have return types with some exceptions, and non-template
997 function names do not have return types. The exceptions are that
998 constructors, destructors, and conversion operators do not have
1002 has_return_type (dc
)
1011 case D_COMP_TEMPLATE
:
1012 return ! is_ctor_dtor_or_conversion (d_left (dc
));
1013 case D_COMP_RESTRICT_THIS
:
1014 case D_COMP_VOLATILE_THIS
:
1015 case D_COMP_CONST_THIS
:
1016 return has_return_type (d_left (dc
));
1020 /* Return whether a name is a constructor, a destructor, or a
1021 conversion operator. */
1024 is_ctor_dtor_or_conversion (dc
)
1033 case D_COMP_QUAL_NAME
:
1034 case D_COMP_LOCAL_NAME
:
1035 return is_ctor_dtor_or_conversion (d_right (dc
));
1043 /* <encoding> ::= <(function) name> <bare-function-type>
1047 TOP_LEVEL is non-zero when called at the top level, in which case
1048 if DMGL_PARAMS is not set we do not demangle the function
1049 parameters. We only set this at the top level, because otherwise
1050 we would not correctly demangle names in local scopes. */
1052 static struct d_comp
*
1053 d_encoding (di
, top_level
)
1057 char peek
= d_peek_char (di
);
1059 if (peek
== 'G' || peek
== 'T')
1060 return d_special_name (di
);
1067 if (dc
!= NULL
&& top_level
&& (di
->options
& DMGL_PARAMS
) == 0)
1069 /* Strip off any initial CV-qualifiers, as they really apply
1070 to the `this' parameter, and they were not output by the
1071 v2 demangler without DMGL_PARAMS. */
1072 while (dc
->type
== D_COMP_RESTRICT_THIS
1073 || dc
->type
== D_COMP_VOLATILE_THIS
1074 || dc
->type
== D_COMP_CONST_THIS
)
1079 peek
= d_peek_char (di
);
1080 if (peek
== '\0' || peek
== 'E')
1082 return d_make_comp (di
, D_COMP_TYPED_NAME
, dc
,
1083 d_bare_function_type (di
, has_return_type (dc
)));
1087 /* <name> ::= <nested-name>
1089 ::= <unscoped-template-name> <template-args>
1092 <unscoped-name> ::= <unqualified-name>
1093 ::= St <unqualified-name>
1095 <unscoped-template-name> ::= <unscoped-name>
1099 static struct d_comp
*
1103 char peek
= d_peek_char (di
);
1109 return d_nested_name (di
);
1112 return d_local_name (di
);
1118 if (d_peek_next_char (di
) != 't')
1120 dc
= d_substitution (di
, 0);
1126 dc
= d_make_comp (di
, D_COMP_QUAL_NAME
, d_make_name (di
, "std", 3),
1127 d_unqualified_name (di
));
1131 if (d_peek_char (di
) != 'I')
1133 /* The grammar does not permit this case to occur if we
1134 called d_substitution() above (i.e., subst == 1). We
1135 don't bother to check. */
1139 /* This is <template-args>, which means that we just saw
1140 <unscoped-template-name>, which is a substitution
1141 candidate if we didn't just get it from a
1145 if (! d_add_substitution (di
, dc
))
1148 dc
= d_make_comp (di
, D_COMP_TEMPLATE
, dc
, d_template_args (di
));
1155 dc
= d_unqualified_name (di
);
1156 if (d_peek_char (di
) == 'I')
1158 /* This is <template-args>, which means that we just saw
1159 <unscoped-template-name>, which is a substitution
1161 if (! d_add_substitution (di
, dc
))
1163 dc
= d_make_comp (di
, D_COMP_TEMPLATE
, dc
, d_template_args (di
));
1169 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1170 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1173 static struct d_comp
*
1178 struct d_comp
**pret
;
1180 if (d_next_char (di
) != 'N')
1183 pret
= d_cv_qualifiers (di
, &ret
, 1);
1187 *pret
= d_prefix (di
);
1191 if (d_next_char (di
) != 'E')
1197 /* <prefix> ::= <prefix> <unqualified-name>
1198 ::= <template-prefix> <template-args>
1199 ::= <template-param>
1203 <template-prefix> ::= <prefix> <(template) unqualified-name>
1204 ::= <template-param>
1208 static struct d_comp
*
1212 struct d_comp
*ret
= NULL
;
1217 enum d_comp_type comb_type
;
1220 peek
= d_peek_char (di
);
1224 /* The older code accepts a <local-name> here, but I don't see
1225 that in the grammar. The older code does not accept a
1226 <template-param> here. */
1228 comb_type
= D_COMP_QUAL_NAME
;
1233 dc
= d_unqualified_name (di
);
1234 else if (peek
== 'S')
1235 dc
= d_substitution (di
, 1);
1236 else if (peek
== 'I')
1240 comb_type
= D_COMP_TEMPLATE
;
1241 dc
= d_template_args (di
);
1243 else if (peek
== 'T')
1244 dc
= d_template_param (di
);
1245 else if (peek
== 'E')
1253 ret
= d_make_comp (di
, comb_type
, ret
, dc
);
1255 if (peek
!= 'S' && d_peek_char (di
) != 'E')
1257 if (! d_add_substitution (di
, ret
))
1263 /* <unqualified-name> ::= <operator-name>
1264 ::= <ctor-dtor-name>
1268 static struct d_comp
*
1269 d_unqualified_name (di
)
1274 peek
= d_peek_char (di
);
1275 if (IS_DIGIT (peek
))
1276 return d_source_name (di
);
1277 else if (IS_LOWER (peek
))
1278 return d_operator_name (di
);
1279 else if (peek
== 'C' || peek
== 'D')
1280 return d_ctor_dtor_name (di
);
1285 /* <source-name> ::= <(positive length) number> <identifier> */
1287 static struct d_comp
*
1294 len
= d_number (di
);
1297 ret
= d_identifier (di
, len
);
1298 di
->last_name
= ret
;
1302 /* number ::= [n] <(non-negative decimal integer)> */
1313 peek
= d_peek_char (di
);
1318 peek
= d_peek_char (di
);
1324 if (! IS_DIGIT (peek
))
1326 ret
= ret
* 10 + peek
- '0';
1328 peek
= d_peek_char (di
);
1332 /* identifier ::= <(unqualified source code identifier)> */
1334 static struct d_comp
*
1335 d_identifier (di
, len
)
1342 d_advance (di
, len
);
1344 /* A Java mangled name may have a trailing '$' if it is a C++
1345 keyword. This '$' is not included in the length count. We just
1347 if ((di
->options
& DMGL_JAVA
) != 0
1348 && d_peek_char (di
) == '$')
1351 /* Look for something which looks like a gcc encoding of an
1352 anonymous namespace, and replace it with a more user friendly
1354 if (len
>= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN
+ 2
1355 && memcmp (name
, ANONYMOUS_NAMESPACE_PREFIX
,
1356 ANONYMOUS_NAMESPACE_PREFIX_LEN
) == 0)
1360 s
= name
+ ANONYMOUS_NAMESPACE_PREFIX_LEN
;
1361 if ((*s
== '.' || *s
== '_' || *s
== '$')
1363 return d_make_name (di
, "(anonymous namespace)",
1364 sizeof "(anonymous namespace)" - 1);
1367 return d_make_name (di
, name
, len
);
1370 /* operator_name ::= many different two character encodings.
1372 ::= v <digit> <source-name>
1375 static const struct d_operator_info d_operators
[] =
1386 { "da", "delete[]", 1 },
1388 { "dl", "delete", 1 },
1405 { "na", "new[]", 1 },
1424 { "st", "sizeof ", 1 },
1425 { "sz", "sizeof ", 1 }
1428 static struct d_comp
*
1429 d_operator_name (di
)
1435 c1
= d_next_char (di
);
1436 c2
= d_next_char (di
);
1437 if (c1
== 'v' && IS_DIGIT (c2
))
1438 return d_make_extended_operator (di
, c2
- '0', d_source_name (di
));
1439 else if (c1
== 'c' && c2
== 'v')
1440 return d_make_comp (di
, D_COMP_CAST
, d_type (di
), NULL
);
1444 int high
= sizeof (d_operators
) / sizeof (d_operators
[0]);
1449 const struct d_operator_info
*p
;
1451 i
= low
+ (high
- low
) / 2;
1452 p
= d_operators
+ i
;
1454 if (c1
== p
->code
[0] && c2
== p
->code
[1])
1455 return d_make_operator (di
, p
);
1457 if (c1
< p
->code
[0] || (c1
== p
->code
[0] && c2
< p
->code
[1]))
1467 /* <special-name> ::= TV <type>
1471 ::= GV <(object) name>
1472 ::= T <call-offset> <(base) encoding>
1473 ::= Tc <call-offset> <call-offset> <(base) encoding>
1474 Also g++ extensions:
1475 ::= TC <type> <(offset) number> _ <(base) type>
1481 static struct d_comp
*
1487 c
= d_next_char (di
);
1490 switch (d_next_char (di
))
1493 return d_make_comp (di
, D_COMP_VTABLE
, d_type (di
), NULL
);
1495 return d_make_comp (di
, D_COMP_VTT
, d_type (di
), NULL
);
1497 return d_make_comp (di
, D_COMP_TYPEINFO
, d_type (di
), NULL
);
1499 return d_make_comp (di
, D_COMP_TYPEINFO_NAME
, d_type (di
), NULL
);
1502 if (! d_call_offset (di
, 'h'))
1504 return d_make_comp (di
, D_COMP_THUNK
, d_encoding (di
, 0), NULL
);
1507 if (! d_call_offset (di
, 'v'))
1509 return d_make_comp (di
, D_COMP_VIRTUAL_THUNK
, d_encoding (di
, 0),
1513 if (! d_call_offset (di
, '\0'))
1515 if (! d_call_offset (di
, '\0'))
1517 return d_make_comp (di
, D_COMP_COVARIANT_THUNK
, d_encoding (di
, 0),
1522 struct d_comp
*derived_type
;
1524 struct d_comp
*base_type
;
1526 derived_type
= d_type (di
);
1527 offset
= d_number (di
);
1530 if (d_next_char (di
) != '_')
1532 base_type
= d_type (di
);
1533 /* We don't display the offset. FIXME: We should display
1534 it in verbose mode. */
1535 return d_make_comp (di
, D_COMP_CONSTRUCTION_VTABLE
, base_type
,
1540 return d_make_comp (di
, D_COMP_TYPEINFO_FN
, d_type (di
), NULL
);
1542 return d_make_comp (di
, D_COMP_JAVA_CLASS
, d_type (di
), NULL
);
1550 switch (d_next_char (di
))
1553 return d_make_comp (di
, D_COMP_GUARD
, d_name (di
), NULL
);
1556 return d_make_comp (di
, D_COMP_REFTEMP
, d_name (di
), 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 (di
, c
)
1585 long virtual_offset
;
1588 c
= d_next_char (di
);
1591 offset
= d_number (di
);
1594 offset
= d_number (di
);
1595 if (d_next_char (di
) != '_')
1597 virtual_offset
= d_number (di
);
1602 if (d_next_char (di
) != '_')
1608 /* <ctor-dtor-name> ::= C1
1616 static struct d_comp
*
1617 d_ctor_dtor_name (di
)
1620 switch (d_next_char (di
))
1624 enum gnu_v3_ctor_kinds kind
;
1626 switch (d_next_char (di
))
1629 kind
= gnu_v3_complete_object_ctor
;
1632 kind
= gnu_v3_base_object_ctor
;
1635 kind
= gnu_v3_complete_object_allocating_ctor
;
1640 return d_make_ctor (di
, kind
, di
->last_name
);
1645 enum gnu_v3_dtor_kinds kind
;
1647 switch (d_next_char (di
))
1650 kind
= gnu_v3_deleting_dtor
;
1653 kind
= gnu_v3_complete_object_dtor
;
1656 kind
= gnu_v3_base_object_dtor
;
1661 return d_make_dtor (di
, kind
, di
->last_name
);
1669 /* <type> ::= <builtin-type>
1671 ::= <class-enum-type>
1673 ::= <pointer-to-member-type>
1674 ::= <template-param>
1675 ::= <template-template-param> <template-args>
1677 ::= <CV-qualifiers> <type>
1682 ::= U <source-name> <type>
1684 <builtin-type> ::= various one letter codes
1688 static const struct d_builtin_type_info d_builtin_types
[26] =
1690 /* a */ { "signed char", "signed char", D_PRINT_INT
},
1691 /* b */ { "bool", "boolean", D_PRINT_BOOL
},
1692 /* c */ { "char", "byte", D_PRINT_INT
},
1693 /* d */ { "double", "double", D_PRINT_DEFAULT
},
1694 /* e */ { "long double", "long double", D_PRINT_DEFAULT
},
1695 /* f */ { "float", "float", D_PRINT_DEFAULT
},
1696 /* g */ { "__float128", "__float128", D_PRINT_DEFAULT
},
1697 /* h */ { "unsigned char", "unsigned char", D_PRINT_INT
},
1698 /* i */ { "int", "int", D_PRINT_INT
},
1699 /* j */ { "unsigned int", "unsigned", D_PRINT_INT
},
1700 /* k */ { NULL
, NULL
, D_PRINT_DEFAULT
},
1701 /* l */ { "long", "long", D_PRINT_LONG
},
1702 /* m */ { "unsigned long", "unsigned long", D_PRINT_LONG
},
1703 /* n */ { "__int128", "__int128", D_PRINT_DEFAULT
},
1704 /* o */ { "unsigned __int128", "unsigned __int128", D_PRINT_DEFAULT
},
1705 /* p */ { NULL
, NULL
, D_PRINT_DEFAULT
},
1706 /* q */ { NULL
, NULL
, D_PRINT_DEFAULT
},
1707 /* r */ { NULL
, NULL
, D_PRINT_DEFAULT
},
1708 /* s */ { "short", "short", D_PRINT_INT
},
1709 /* t */ { "unsigned short", "unsigned short", D_PRINT_INT
},
1710 /* u */ { NULL
, NULL
, D_PRINT_DEFAULT
},
1711 /* v */ { "void", "void", D_PRINT_VOID
},
1712 /* w */ { "wchar_t", "char", D_PRINT_INT
},
1713 /* x */ { "long long", "long", D_PRINT_DEFAULT
},
1714 /* y */ { "unsigned long long", "unsigned long long", D_PRINT_DEFAULT
},
1715 /* z */ { "...", "...", D_PRINT_DEFAULT
},
1718 static struct d_comp
*
1726 /* The ABI specifies that when CV-qualifiers are used, the base type
1727 is substitutable, and the fully qualified type is substitutable,
1728 but the base type with a strict subset of the CV-qualifiers is
1729 not substitutable. The natural recursive implementation of the
1730 CV-qualifiers would cause subsets to be substitutable, so instead
1731 we pull them all off now.
1733 FIXME: The ABI says that order-insensitive vendor qualifiers
1734 should be handled in the same way, but we have no way to tell
1735 which vendor qualifiers are order-insensitive and which are
1736 order-sensitive. So we just assume that they are all
1737 order-sensitive. g++ 3.4 supports only one vendor qualifier,
1738 __vector, and it treats it as order-sensitive when mangling
1741 peek
= d_peek_char (di
);
1742 if (peek
== 'r' || peek
== 'V' || peek
== 'K')
1744 struct d_comp
**pret
;
1746 pret
= d_cv_qualifiers (di
, &ret
, 0);
1749 *pret
= d_type (di
);
1750 if (! d_add_substitution (di
, ret
))
1759 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1760 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
1761 case 'o': case 's': case 't':
1762 case 'v': case 'w': case 'x': case 'y': case 'z':
1763 ret
= d_make_builtin_type (di
, &d_builtin_types
[peek
- 'a']);
1770 ret
= d_make_comp (di
, D_COMP_VENDOR_TYPE
, d_source_name (di
), NULL
);
1774 ret
= d_function_type (di
);
1777 case '0': case '1': case '2': case '3': case '4':
1778 case '5': case '6': case '7': case '8': case '9':
1781 ret
= d_class_enum_type (di
);
1785 ret
= d_array_type (di
);
1789 ret
= d_pointer_to_member_type (di
);
1793 ret
= d_template_param (di
);
1794 if (d_peek_char (di
) == 'I')
1796 /* This is <template-template-param> <template-args>. The
1797 <template-template-param> part is a substitution
1799 if (! d_add_substitution (di
, ret
))
1801 ret
= d_make_comp (di
, D_COMP_TEMPLATE
, ret
, d_template_args (di
));
1806 /* If this is a special substitution, then it is the start of
1807 <class-enum-type>. */
1811 peek_next
= d_peek_next_char (di
);
1812 if (IS_DIGIT (peek_next
)
1814 || IS_UPPER (peek_next
))
1816 ret
= d_substitution (di
, 0);
1817 /* The substituted name may have been a template name and
1818 may be followed by tepmlate args. */
1819 if (d_peek_char (di
) == 'I')
1820 ret
= d_make_comp (di
, D_COMP_TEMPLATE
, ret
,
1821 d_template_args (di
));
1827 ret
= d_class_enum_type (di
);
1828 /* If the substitution was a complete type, then it is not
1829 a new substitution candidate. However, if the
1830 substitution was followed by template arguments, then
1831 the whole thing is a substitution candidate. */
1832 if (ret
!= NULL
&& ret
->type
== D_COMP_SUB_STD
)
1840 ret
= d_make_comp (di
, D_COMP_POINTER
, d_type (di
), NULL
);
1845 ret
= d_make_comp (di
, D_COMP_REFERENCE
, d_type (di
), NULL
);
1850 ret
= d_make_comp (di
, D_COMP_COMPLEX
, d_type (di
), NULL
);
1855 ret
= d_make_comp (di
, D_COMP_IMAGINARY
, d_type (di
), NULL
);
1860 ret
= d_source_name (di
);
1861 ret
= d_make_comp (di
, D_COMP_VENDOR_TYPE_QUAL
, d_type (di
), ret
);
1870 if (! d_add_substitution (di
, ret
))
1877 /* <CV-qualifiers> ::= [r] [V] [K] */
1879 static struct d_comp
**
1880 d_cv_qualifiers (di
, pret
, member_fn
)
1882 struct d_comp
**pret
;
1887 peek
= d_peek_char (di
);
1888 while (peek
== 'r' || peek
== 'V' || peek
== 'K')
1894 t
= member_fn
? D_COMP_RESTRICT_THIS
: D_COMP_RESTRICT
;
1895 else if (peek
== 'V')
1896 t
= member_fn
? D_COMP_VOLATILE_THIS
: D_COMP_VOLATILE
;
1898 t
= member_fn
? D_COMP_CONST_THIS
: D_COMP_CONST
;
1900 *pret
= d_make_comp (di
, t
, NULL
, NULL
);
1903 pret
= &d_left (*pret
);
1905 peek
= d_peek_char (di
);
1911 /* <function-type> ::= F [Y] <bare-function-type> E */
1913 static struct d_comp
*
1914 d_function_type (di
)
1919 if (d_next_char (di
) != 'F')
1921 if (d_peek_char (di
) == 'Y')
1923 /* Function has C linkage. We don't print this information.
1924 FIXME: We should print it in verbose mode. */
1927 ret
= d_bare_function_type (di
, 1);
1928 if (d_next_char (di
) != 'E')
1933 /* <bare-function-type> ::= <type>+ */
1935 static struct d_comp
*
1936 d_bare_function_type (di
, has_return_type
)
1938 int has_return_type
;
1940 struct d_comp
*return_type
;
1942 struct d_comp
**ptl
;
1950 struct d_comp
*type
;
1952 peek
= d_peek_char (di
);
1953 if (peek
== '\0' || peek
== 'E')
1958 if (has_return_type
)
1961 has_return_type
= 0;
1965 *ptl
= d_make_comp (di
, D_COMP_ARGLIST
, type
, NULL
);
1968 ptl
= &d_right (*ptl
);
1972 /* There should be at least one parameter type besides the optional
1973 return type. A function which takes no arguments will have a
1974 single parameter type void. */
1978 /* If we have a single parameter type void, omit it. */
1979 if (d_right (tl
) == NULL
1980 && d_left (tl
)->type
== D_COMP_BUILTIN_TYPE
1981 && d_left (tl
)->u
.s_builtin
.type
->print
== D_PRINT_VOID
)
1984 return d_make_comp (di
, D_COMP_FUNCTION_TYPE
, return_type
, tl
);
1987 /* <class-enum-type> ::= <name> */
1989 static struct d_comp
*
1990 d_class_enum_type (di
)
1996 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
1997 ::= A [<(dimension) expression>] _ <(element) type>
2000 static struct d_comp
*
2007 if (d_next_char (di
) != 'A')
2010 peek
= d_peek_char (di
);
2013 else if (IS_DIGIT (peek
))
2021 peek
= d_peek_char (di
);
2023 while (IS_DIGIT (peek
));
2024 dim
= d_make_name (di
, s
, d_str (di
) - s
);
2030 dim
= d_expression (di
);
2035 if (d_next_char (di
) != '_')
2038 return d_make_comp (di
, D_COMP_ARRAY_TYPE
, dim
, d_type (di
));
2041 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2043 static struct d_comp
*
2044 d_pointer_to_member_type (di
)
2049 struct d_comp
**pmem
;
2051 if (d_next_char (di
) != 'M')
2056 /* The ABI specifies that any type can be a substitution source, and
2057 that M is followed by two types, and that when a CV-qualified
2058 type is seen both the base type and the CV-qualified types are
2059 substitution sources. The ABI also specifies that for a pointer
2060 to a CV-qualified member function, the qualifiers are attached to
2061 the second type. Given the grammar, a plain reading of the ABI
2062 suggests that both the CV-qualified member function and the
2063 non-qualified member function are substitution sources. However,
2064 g++ does not work that way. g++ treats only the CV-qualified
2065 member function as a substitution source. FIXME. So to work
2066 with g++, we need to pull off the CV-qualifiers here, in order to
2067 avoid calling add_substitution() in d_type(). */
2069 pmem
= d_cv_qualifiers (di
, &mem
, 1);
2072 *pmem
= d_type (di
);
2074 return d_make_comp (di
, D_COMP_PTRMEM_TYPE
, cl
, mem
);
2077 /* <template-param> ::= T_
2078 ::= T <(parameter-2 non-negative) number> _
2081 static struct d_comp
*
2082 d_template_param (di
)
2087 if (d_next_char (di
) != 'T')
2090 if (d_peek_char (di
) == '_')
2094 param
= d_number (di
);
2100 if (d_next_char (di
) != '_')
2103 return d_make_template_param (di
, param
);
2106 /* <template-args> ::= I <template-arg>+ E */
2108 static struct d_comp
*
2109 d_template_args (di
)
2112 struct d_comp
*hold_last_name
;
2114 struct d_comp
**pal
;
2116 /* Preserve the last name we saw--don't let the template arguments
2117 clobber it, as that would give us the wrong name for a subsequent
2118 constructor or destructor. */
2119 hold_last_name
= di
->last_name
;
2121 if (d_next_char (di
) != 'I')
2130 a
= d_template_arg (di
);
2134 *pal
= d_make_comp (di
, D_COMP_TEMPLATE_ARGLIST
, a
, NULL
);
2137 pal
= &d_right (*pal
);
2139 if (d_peek_char (di
) == 'E')
2146 di
->last_name
= hold_last_name
;
2151 /* <template-arg> ::= <type>
2152 ::= X <expression> E
2156 static struct d_comp
*
2162 switch (d_peek_char (di
))
2166 ret
= d_expression (di
);
2167 if (d_next_char (di
) != 'E')
2172 return d_expr_primary (di
);
2179 /* <expression> ::= <(unary) operator-name> <expression>
2180 ::= <(binary) operator-name> <expression> <expression>
2181 ::= <(trinary) operator-name> <expression> <expression> <expression>
2183 ::= <template-param>
2184 ::= sr <type> <unqualified-name>
2185 ::= sr <type> <unqualified-name> <template-args>
2189 static struct d_comp
*
2195 peek
= d_peek_char (di
);
2197 return d_expr_primary (di
);
2198 else if (peek
== 'T')
2199 return d_template_param (di
);
2200 else if (peek
== 's' && d_peek_next_char (di
) == 'r')
2202 struct d_comp
*type
;
2203 struct d_comp
*name
;
2207 name
= d_unqualified_name (di
);
2208 if (d_peek_char (di
) != 'I')
2209 return d_make_comp (di
, D_COMP_QUAL_NAME
, type
, name
);
2211 return d_make_comp (di
, D_COMP_QUAL_NAME
, type
,
2212 d_make_comp (di
, D_COMP_TEMPLATE
, name
,
2213 d_template_args (di
)));
2220 op
= d_operator_name (di
);
2224 if (op
->type
== D_COMP_OPERATOR
2225 && strcmp (op
->u
.s_operator
.op
->code
, "st") == 0)
2226 return d_make_comp (di
, D_COMP_UNARY
, op
, d_type (di
));
2232 case D_COMP_OPERATOR
:
2233 args
= op
->u
.s_operator
.op
->args
;
2235 case D_COMP_EXTENDED_OPERATOR
:
2236 args
= op
->u
.s_extended_operator
.args
;
2246 return d_make_comp (di
, D_COMP_UNARY
, op
, d_expression (di
));
2249 struct d_comp
*left
;
2251 left
= d_expression (di
);
2252 return d_make_comp (di
, D_COMP_BINARY
, op
,
2253 d_make_comp (di
, D_COMP_BINARY_ARGS
, left
,
2254 d_expression (di
)));
2258 struct d_comp
*first
;
2259 struct d_comp
*second
;
2261 first
= d_expression (di
);
2262 second
= d_expression (di
);
2263 return d_make_comp (di
, D_COMP_TRINARY
, op
,
2264 d_make_comp (di
, D_COMP_TRINARY_ARG1
, first
,
2266 D_COMP_TRINARY_ARG2
,
2268 d_expression (di
))));
2276 /* <expr-primary> ::= L <type> <(value) number> E
2277 ::= L <type> <(value) float> E
2278 ::= L <mangled-name> E
2281 static struct d_comp
*
2287 if (d_next_char (di
) != 'L')
2289 if (d_peek_char (di
) == '_')
2290 ret
= d_mangled_name (di
, 0);
2293 struct d_comp
*type
;
2299 /* Rather than try to interpret the literal value, we just
2300 collect it as a string. Note that it's possible to have a
2301 floating point literal here. The ABI specifies that the
2302 format of such literals is machine independent. That's fine,
2303 but what's not fine is that versions of g++ up to 3.2 with
2304 -fabi-version=1 used upper case letters in the hex constant,
2305 and dumped out gcc's internal representation. That makes it
2306 hard to tell where the constant ends, and hard to dump the
2307 constant in any readable form anyhow. We don't attempt to
2308 handle these cases. */
2311 if (d_peek_char (di
) == 'n')
2313 t
= D_COMP_LITERAL_NEG
;
2317 while (d_peek_char (di
) != 'E')
2319 ret
= d_make_comp (di
, t
, type
, d_make_name (di
, s
, d_str (di
) - s
));
2321 if (d_next_char (di
) != 'E')
2326 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2327 ::= Z <(function) encoding> E s [<discriminator>]
2330 static struct d_comp
*
2334 struct d_comp
*function
;
2336 if (d_next_char (di
) != 'Z')
2339 function
= d_encoding (di
, 0);
2341 if (d_next_char (di
) != 'E')
2344 if (d_peek_char (di
) == 's')
2347 if (! d_discriminator (di
))
2349 return d_make_comp (di
, D_COMP_LOCAL_NAME
, function
,
2350 d_make_name (di
, "string literal",
2351 sizeof "string literal" - 1));
2355 struct d_comp
*name
;
2358 if (! d_discriminator (di
))
2360 return d_make_comp (di
, D_COMP_LOCAL_NAME
, function
, name
);
2364 /* <discriminator> ::= _ <(non-negative) number>
2366 We demangle the discriminator, but we don't print it out. FIXME:
2367 We should print it out in verbose mode. */
2370 d_discriminator (di
)
2375 if (d_peek_char (di
) != '_')
2378 discrim
= d_number (di
);
2384 /* Add a new substitution. */
2387 d_add_substitution (di
, dc
)
2393 if (di
->next_sub
>= di
->num_subs
)
2395 di
->subs
[di
->next_sub
] = dc
;
2400 /* <substitution> ::= S <seq-id> _
2410 If PREFIX is non-zero, then this type is being used as a prefix in
2411 a qualified name. In this case, for the standard substitutions, we
2412 need to check whether we are being used as a prefix for a
2413 constructor or destructor, and return a full template name.
2414 Otherwise we will get something like std::iostream::~iostream()
2415 which does not correspond particularly well to any function which
2416 actually appears in the source.
2419 static const struct d_standard_sub_info standard_subs
[] =
2421 { 't', "std", "std", NULL
},
2422 { 'a', "std::allocator", "std::allocator", "allocator" },
2423 { 'b', "std::basic_string", "std::basic_string", "basic_string" },
2424 { 's', "std::string",
2425 "std::basic_string<char, std::char_traits<char>, std::allocator<char> >",
2427 { 'i', "std::istream",
2428 "std::basic_istream<char, std::char_traits<char> >",
2430 { 'o', "std::ostream",
2431 "std::basic_ostream<char, std::char_traits<char> >",
2433 { 'd', "std::iostream",
2434 "std::basic_iostream<char, std::char_traits<char> >",
2438 static struct d_comp
*
2439 d_substitution (di
, prefix
)
2445 if (d_next_char (di
) != 'S')
2448 c
= d_next_char (di
);
2449 if (c
== '_' || IS_DIGIT (c
) || IS_UPPER (c
))
2459 id
= id
* 36 + c
- '0';
2460 else if (IS_UPPER (c
))
2461 id
= id
* 36 + c
- 'A' + 10;
2464 c
= d_next_char (di
);
2471 if (id
>= di
->next_sub
)
2474 return di
->subs
[id
];
2479 const struct d_standard_sub_info
*p
;
2480 const struct d_standard_sub_info
*pend
;
2482 verbose
= (di
->options
& DMGL_VERBOSE
) != 0;
2483 if (! verbose
&& prefix
)
2487 peek
= d_peek_char (di
);
2488 if (peek
== 'C' || peek
== 'D')
2492 pend
= (&standard_subs
[0]
2493 + sizeof standard_subs
/ sizeof standard_subs
[0]);
2494 for (p
= &standard_subs
[0]; p
< pend
; ++p
)
2498 if (p
->set_last_name
!= NULL
)
2499 di
->last_name
= d_make_sub (di
, p
->set_last_name
);
2501 return d_make_sub (di
, p
->full_expansion
);
2503 return d_make_sub (di
, p
->simple_expansion
);
2511 /* Resize the print buffer. */
2514 d_print_resize (dpi
, add
)
2515 struct d_print_info
*dpi
;
2520 if (dpi
->buf
== NULL
)
2522 need
= dpi
->len
+ add
;
2523 while (need
> dpi
->alc
)
2528 newalc
= dpi
->alc
* 2;
2529 newbuf
= realloc (dpi
->buf
, newalc
);
2534 dpi
->allocation_failure
= 1;
2542 /* Append a character to the print buffer. */
2545 d_print_append_char (dpi
, c
)
2546 struct d_print_info
*dpi
;
2549 if (dpi
->buf
!= NULL
)
2551 if (dpi
->len
>= dpi
->alc
)
2553 d_print_resize (dpi
, 1);
2554 if (dpi
->buf
== NULL
)
2558 dpi
->buf
[dpi
->len
] = c
;
2563 /* Append a buffer to the print buffer. */
2566 d_print_append_buffer (dpi
, s
, l
)
2567 struct d_print_info
*dpi
;
2571 if (dpi
->buf
!= NULL
)
2573 if (dpi
->len
+ l
> dpi
->alc
)
2575 d_print_resize (dpi
, l
);
2576 if (dpi
->buf
== NULL
)
2580 memcpy (dpi
->buf
+ dpi
->len
, s
, l
);
2585 /* Indicate that an error occurred during printing. */
2589 struct d_print_info
*dpi
;
2595 /* Turn components into a human readable string. Returns a string
2596 allocated by malloc, or NULL on error. On success, this sets *PALC
2597 to the size of the allocated buffer. On failure, this sets *PALC
2598 to 0 for a bad parse, or to 1 for a memory allocation failure. */
2601 d_print (options
, dc
, palc
)
2603 const struct d_comp
*dc
;
2606 struct d_print_info dpi
;
2608 dpi
.options
= options
;
2611 dpi
.buf
= malloc (dpi
.alc
);
2612 if (dpi
.buf
== NULL
)
2619 dpi
.templates
= NULL
;
2620 dpi
.modifiers
= NULL
;
2622 dpi
.allocation_failure
= 0;
2624 d_print_comp (&dpi
, dc
);
2626 d_append_char (&dpi
, '\0');
2628 if (dpi
.buf
!= NULL
)
2631 *palc
= dpi
.allocation_failure
;
2636 /* Subroutine to handle components. */
2639 d_print_comp (dpi
, dc
)
2640 struct d_print_info
*dpi
;
2641 const struct d_comp
*dc
;
2645 d_print_error (dpi
);
2648 if (d_print_saw_error (dpi
))
2654 d_print_identifier (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
2657 case D_COMP_QUAL_NAME
:
2658 case D_COMP_LOCAL_NAME
:
2659 d_print_comp (dpi
, d_left (dc
));
2660 d_append_string (dpi
, (dpi
->options
& DMGL_JAVA
) == 0 ? "::" : ".");
2661 d_print_comp (dpi
, d_right (dc
));
2664 case D_COMP_TYPED_NAME
:
2666 struct d_print_mod
*hold_modifiers
;
2667 struct d_comp
*typed_name
;
2668 struct d_print_mod adpm
[4];
2670 struct d_print_template dpt
;
2672 /* Pass the name down to the type so that it can be printed in
2673 the right place for the type. We also have to pass down
2674 any CV-qualifiers, which apply to the this parameter. */
2675 hold_modifiers
= dpi
->modifiers
;
2677 typed_name
= d_left (dc
);
2678 while (typed_name
!= NULL
)
2680 if (i
>= sizeof adpm
/ sizeof adpm
[0])
2682 d_print_error (dpi
);
2686 adpm
[i
].next
= dpi
->modifiers
;
2687 dpi
->modifiers
= &adpm
[i
];
2688 adpm
[i
].mod
= typed_name
;
2689 adpm
[i
].printed
= 0;
2690 adpm
[i
].templates
= dpi
->templates
;
2693 if (typed_name
->type
!= D_COMP_RESTRICT_THIS
2694 && typed_name
->type
!= D_COMP_VOLATILE_THIS
2695 && typed_name
->type
!= D_COMP_CONST_THIS
)
2698 typed_name
= d_left (typed_name
);
2701 /* If typed_name is a template, then it applies to the
2702 function type as well. */
2703 if (typed_name
->type
== D_COMP_TEMPLATE
)
2705 dpt
.next
= dpi
->templates
;
2706 dpi
->templates
= &dpt
;
2707 dpt
.template = typed_name
;
2710 /* If typed_name is a D_COMP_LOCAL_NAME, then there may be
2711 CV-qualifiers on its right argument which really apply
2712 here; this happens when parsing a class which is local to a
2714 if (typed_name
->type
== D_COMP_LOCAL_NAME
)
2716 struct d_comp
*local_name
;
2718 local_name
= d_right (typed_name
);
2719 while (local_name
->type
== D_COMP_RESTRICT_THIS
2720 || local_name
->type
== D_COMP_VOLATILE_THIS
2721 || local_name
->type
== D_COMP_CONST_THIS
)
2723 if (i
>= sizeof adpm
/ sizeof adpm
[0])
2725 d_print_error (dpi
);
2729 adpm
[i
] = adpm
[i
- 1];
2730 adpm
[i
].next
= &adpm
[i
- 1];
2731 dpi
->modifiers
= &adpm
[i
];
2733 adpm
[i
- 1].mod
= local_name
;
2734 adpm
[i
- 1].printed
= 0;
2735 adpm
[i
- 1].templates
= dpi
->templates
;
2738 local_name
= d_left (local_name
);
2742 d_print_comp (dpi
, d_right (dc
));
2744 if (typed_name
->type
== D_COMP_TEMPLATE
)
2745 dpi
->templates
= dpt
.next
;
2747 /* If the modifiers didn't get printed by the type, print them
2752 if (! adpm
[i
].printed
)
2754 d_append_char (dpi
, ' ');
2755 d_print_mod (dpi
, adpm
[i
].mod
);
2759 dpi
->modifiers
= hold_modifiers
;
2764 case D_COMP_TEMPLATE
:
2766 struct d_print_mod
*hold_dpm
;
2768 /* Don't push modifiers into a template definition. Doing so
2769 could give the wrong definition for a template argument.
2770 Instead, treat the template essentially as a name. */
2772 hold_dpm
= dpi
->modifiers
;
2773 dpi
->modifiers
= NULL
;
2775 d_print_comp (dpi
, d_left (dc
));
2776 if (d_last_char (dpi
) == '<')
2777 d_append_char (dpi
, ' ');
2778 d_append_char (dpi
, '<');
2779 d_print_comp (dpi
, d_right (dc
));
2780 /* Avoid generating two consecutive '>' characters, to avoid
2781 the C++ syntactic ambiguity. */
2782 if (d_last_char (dpi
) == '>')
2783 d_append_char (dpi
, ' ');
2784 d_append_char (dpi
, '>');
2786 dpi
->modifiers
= hold_dpm
;
2791 case D_COMP_TEMPLATE_PARAM
:
2795 struct d_print_template
*hold_dpt
;
2797 if (dpi
->templates
== NULL
)
2799 d_print_error (dpi
);
2802 i
= dc
->u
.s_number
.number
;
2803 for (a
= d_right (dpi
->templates
->template);
2807 if (a
->type
!= D_COMP_TEMPLATE_ARGLIST
)
2809 d_print_error (dpi
);
2816 if (i
!= 0 || a
== NULL
)
2818 d_print_error (dpi
);
2822 /* While processing this parameter, we need to pop the list of
2823 templates. This is because the template parameter may
2824 itself be a reference to a parameter of an outer
2827 hold_dpt
= dpi
->templates
;
2828 dpi
->templates
= hold_dpt
->next
;
2830 d_print_comp (dpi
, d_left (a
));
2832 dpi
->templates
= hold_dpt
;
2838 d_print_comp (dpi
, dc
->u
.s_ctor
.name
);
2842 d_append_char (dpi
, '~');
2843 d_print_comp (dpi
, dc
->u
.s_dtor
.name
);
2847 d_append_string (dpi
, "vtable for ");
2848 d_print_comp (dpi
, d_left (dc
));
2852 d_append_string (dpi
, "VTT for ");
2853 d_print_comp (dpi
, d_left (dc
));
2856 case D_COMP_CONSTRUCTION_VTABLE
:
2857 d_append_string (dpi
, "construction vtable for ");
2858 d_print_comp (dpi
, d_left (dc
));
2859 d_append_string (dpi
, "-in-");
2860 d_print_comp (dpi
, d_right (dc
));
2863 case D_COMP_TYPEINFO
:
2864 d_append_string (dpi
, "typeinfo for ");
2865 d_print_comp (dpi
, d_left (dc
));
2868 case D_COMP_TYPEINFO_NAME
:
2869 d_append_string (dpi
, "typeinfo name for ");
2870 d_print_comp (dpi
, d_left (dc
));
2873 case D_COMP_TYPEINFO_FN
:
2874 d_append_string (dpi
, "typeinfo fn for ");
2875 d_print_comp (dpi
, d_left (dc
));
2879 d_append_string (dpi
, "non-virtual thunk to ");
2880 d_print_comp (dpi
, d_left (dc
));
2883 case D_COMP_VIRTUAL_THUNK
:
2884 d_append_string (dpi
, "virtual thunk to ");
2885 d_print_comp (dpi
, d_left (dc
));
2888 case D_COMP_COVARIANT_THUNK
:
2889 d_append_string (dpi
, "covariant return thunk to ");
2890 d_print_comp (dpi
, d_left (dc
));
2893 case D_COMP_JAVA_CLASS
:
2894 d_append_string (dpi
, "java Class for ");
2895 d_print_comp (dpi
, d_left (dc
));
2899 d_append_string (dpi
, "guard variable for ");
2900 d_print_comp (dpi
, d_left (dc
));
2903 case D_COMP_REFTEMP
:
2904 d_append_string (dpi
, "reference temporary for ");
2905 d_print_comp (dpi
, d_left (dc
));
2908 case D_COMP_SUB_STD
:
2909 d_append_string (dpi
, dc
->u
.s_string
.string
);
2912 case D_COMP_RESTRICT
:
2913 case D_COMP_VOLATILE
:
2915 case D_COMP_RESTRICT_THIS
:
2916 case D_COMP_VOLATILE_THIS
:
2917 case D_COMP_CONST_THIS
:
2918 case D_COMP_VENDOR_TYPE_QUAL
:
2919 case D_COMP_POINTER
:
2920 case D_COMP_REFERENCE
:
2921 case D_COMP_COMPLEX
:
2922 case D_COMP_IMAGINARY
:
2924 /* We keep a list of modifiers on the stack. */
2925 struct d_print_mod dpm
;
2927 dpm
.next
= dpi
->modifiers
;
2928 dpi
->modifiers
= &dpm
;
2931 dpm
.templates
= dpi
->templates
;
2933 d_print_comp (dpi
, d_left (dc
));
2935 /* If the modifier didn't get printed by the type, print it
2938 d_print_mod (dpi
, dc
);
2940 dpi
->modifiers
= dpm
.next
;
2945 case D_COMP_BUILTIN_TYPE
:
2946 if ((dpi
->options
& DMGL_JAVA
) == 0)
2947 d_append_string (dpi
, dc
->u
.s_builtin
.type
->name
);
2949 d_append_string (dpi
, dc
->u
.s_builtin
.type
->java_name
);
2952 case D_COMP_VENDOR_TYPE
:
2953 d_print_comp (dpi
, d_left (dc
));
2956 case D_COMP_FUNCTION_TYPE
:
2958 if (d_left (dc
) != NULL
)
2960 struct d_print_mod dpm
;
2962 /* We must pass this type down as a modifier in order to
2963 print it in the right location. */
2965 dpm
.next
= dpi
->modifiers
;
2966 dpi
->modifiers
= &dpm
;
2969 dpm
.templates
= dpi
->templates
;
2971 d_print_comp (dpi
, d_left (dc
));
2973 dpi
->modifiers
= dpm
.next
;
2978 d_append_char (dpi
, ' ');
2981 d_print_function_type (dpi
, dc
, dpi
->modifiers
);
2986 case D_COMP_ARRAY_TYPE
:
2988 struct d_print_mod dpm
;
2990 /* We must pass this type down as a modifier in order to print
2991 multi-dimensional arrays correctly. */
2993 dpm
.next
= dpi
->modifiers
;
2994 dpi
->modifiers
= &dpm
;
2997 dpm
.templates
= dpi
->templates
;
2999 d_print_comp (dpi
, d_right (dc
));
3001 dpi
->modifiers
= dpm
.next
;
3006 d_print_array_type (dpi
, dc
, dpi
->modifiers
);
3011 case D_COMP_PTRMEM_TYPE
:
3013 struct d_print_mod dpm
;
3015 dpm
.next
= dpi
->modifiers
;
3016 dpi
->modifiers
= &dpm
;
3019 dpm
.templates
= dpi
->templates
;
3021 d_print_comp (dpi
, d_right (dc
));
3023 /* If the modifier didn't get printed by the type, print it
3027 d_append_char (dpi
, ' ');
3028 d_print_comp (dpi
, d_left (dc
));
3029 d_append_string (dpi
, "::*");
3032 dpi
->modifiers
= dpm
.next
;
3037 case D_COMP_ARGLIST
:
3038 case D_COMP_TEMPLATE_ARGLIST
:
3039 d_print_comp (dpi
, d_left (dc
));
3040 if (d_right (dc
) != NULL
)
3042 d_append_string (dpi
, ", ");
3043 d_print_comp (dpi
, d_right (dc
));
3047 case D_COMP_OPERATOR
:
3051 d_append_string (dpi
, "operator");
3052 c
= dc
->u
.s_operator
.op
->name
[0];
3054 d_append_char (dpi
, ' ');
3055 d_append_string (dpi
, dc
->u
.s_operator
.op
->name
);
3059 case D_COMP_EXTENDED_OPERATOR
:
3060 d_append_string (dpi
, "operator ");
3061 d_print_comp (dpi
, dc
->u
.s_extended_operator
.name
);
3065 d_append_string (dpi
, "operator ");
3066 d_print_cast (dpi
, dc
);
3070 if (d_left (dc
)->type
!= D_COMP_CAST
)
3071 d_print_expr_op (dpi
, d_left (dc
));
3074 d_append_string (dpi
, "((");
3075 d_print_cast (dpi
, d_left (dc
));
3076 d_append_char (dpi
, ')');
3078 d_append_char (dpi
, '(');
3079 d_print_comp (dpi
, d_right (dc
));
3080 d_append_char (dpi
, ')');
3081 if (d_left (dc
)->type
== D_COMP_CAST
)
3082 d_append_char (dpi
, ')');
3086 if (d_right (dc
)->type
!= D_COMP_BINARY_ARGS
)
3088 d_print_error (dpi
);
3092 /* We wrap an expression which uses the greater-than operator in
3093 an extra layer of parens so that it does not get confused
3094 with the '>' which ends the template parameters. */
3095 if (d_left (dc
)->type
== D_COMP_OPERATOR
3096 && strcmp (d_left (dc
)->u
.s_operator
.op
->name
, ">") == 0)
3097 d_append_char (dpi
, '(');
3099 d_append_char (dpi
, '(');
3100 d_print_comp (dpi
, d_left (d_right (dc
)));
3101 d_append_string (dpi
, ") ");
3102 d_print_expr_op (dpi
, d_left (dc
));
3103 d_append_string (dpi
, " (");
3104 d_print_comp (dpi
, d_right (d_right (dc
)));
3105 d_append_char (dpi
, ')');
3107 if (d_left (dc
)->type
== D_COMP_OPERATOR
3108 && strcmp (d_left (dc
)->u
.s_operator
.op
->name
, ">") == 0)
3109 d_append_char (dpi
, ')');
3113 case D_COMP_BINARY_ARGS
:
3114 /* We should only see this as part of D_COMP_BINARY. */
3115 d_print_error (dpi
);
3118 case D_COMP_TRINARY
:
3119 if (d_right (dc
)->type
!= D_COMP_TRINARY_ARG1
3120 || d_right (d_right (dc
))->type
!= D_COMP_TRINARY_ARG2
)
3122 d_print_error (dpi
);
3125 d_append_char (dpi
, '(');
3126 d_print_comp (dpi
, d_left (d_right (dc
)));
3127 d_append_string (dpi
, ") ");
3128 d_print_expr_op (dpi
, d_left (dc
));
3129 d_append_string (dpi
, " (");
3130 d_print_comp (dpi
, d_left (d_right (d_right (dc
))));
3131 d_append_string (dpi
, ") : (");
3132 d_print_comp (dpi
, d_right (d_right (d_right (dc
))));
3133 d_append_char (dpi
, ')');
3136 case D_COMP_TRINARY_ARG1
:
3137 case D_COMP_TRINARY_ARG2
:
3138 /* We should only see these are part of D_COMP_TRINARY. */
3139 d_print_error (dpi
);
3142 case D_COMP_LITERAL
:
3143 case D_COMP_LITERAL_NEG
:
3144 /* For some builtin types, produce simpler output. */
3145 if (d_left (dc
)->type
== D_COMP_BUILTIN_TYPE
)
3147 switch (d_left (dc
)->u
.s_builtin
.type
->print
)
3150 if (d_right (dc
)->type
== D_COMP_NAME
)
3152 if (dc
->type
== D_COMP_LITERAL_NEG
)
3153 d_append_char (dpi
, '-');
3154 d_print_comp (dpi
, d_right (dc
));
3160 if (d_right (dc
)->type
== D_COMP_NAME
)
3162 if (dc
->type
== D_COMP_LITERAL_NEG
)
3163 d_append_char (dpi
, '-');
3164 d_print_comp (dpi
, d_right (dc
));
3165 d_append_char (dpi
, 'l');
3171 if (d_right (dc
)->type
== D_COMP_NAME
3172 && d_right (dc
)->u
.s_name
.len
== 1
3173 && dc
->type
== D_COMP_LITERAL
)
3175 switch (d_right (dc
)->u
.s_name
.s
[0])
3178 d_append_string (dpi
, "false");
3181 d_append_string (dpi
, "true");
3194 d_append_char (dpi
, '(');
3195 d_print_comp (dpi
, d_left (dc
));
3196 d_append_char (dpi
, ')');
3197 if (dc
->type
== D_COMP_LITERAL_NEG
)
3198 d_append_char (dpi
, '-');
3199 d_print_comp (dpi
, d_right (dc
));
3203 d_print_error (dpi
);
3208 /* Print an identifier. */
3211 d_print_identifier (dpi
, name
, len
)
3212 struct d_print_info
*dpi
;
3216 if ((dpi
->options
& DMGL_JAVA
) == 0)
3217 d_append_buffer (dpi
, name
, len
);
3223 /* For Java we try to handle encoded extended Unicode
3224 characters. The C++ ABI doesn't mention Unicode encoding, so
3225 we don't it for C++. Characters are encoded as
3228 for (p
= name
; p
< end
; ++p
)
3239 for (q
= p
+ 3; q
< end
; ++q
)
3245 else if (*q
>= 'A' && *q
<= 'F')
3246 dig
= *q
- 'A' + 10;
3247 else if (*q
>= 'a' && *q
<= 'f')
3248 dig
= *q
- 'a' + 10;
3254 /* If the Unicode character is larger than 256, we don't
3255 try to deal with it here. FIXME. */
3256 if (q
< end
&& *q
== '_' && c
< 256)
3258 d_append_char (dpi
, c
);
3264 d_append_char (dpi
, *p
);
3269 /* Print a list of modifiers. SUFFIX is 1 if we are printing
3270 qualifiers on this after printing a function. */
3273 d_print_mod_list (dpi
, mods
, suffix
)
3274 struct d_print_info
*dpi
;
3275 struct d_print_mod
*mods
;
3278 struct d_print_template
*hold_dpt
;
3280 if (mods
== NULL
|| d_print_saw_error (dpi
))
3285 && (mods
->mod
->type
== D_COMP_RESTRICT_THIS
3286 || mods
->mod
->type
== D_COMP_VOLATILE_THIS
3287 || mods
->mod
->type
== D_COMP_CONST_THIS
)))
3289 d_print_mod_list (dpi
, mods
->next
, suffix
);
3295 hold_dpt
= dpi
->templates
;
3296 dpi
->templates
= mods
->templates
;
3298 if (mods
->mod
->type
== D_COMP_FUNCTION_TYPE
)
3300 d_print_function_type (dpi
, mods
->mod
, mods
->next
);
3301 dpi
->templates
= hold_dpt
;
3304 else if (mods
->mod
->type
== D_COMP_ARRAY_TYPE
)
3306 d_print_array_type (dpi
, mods
->mod
, mods
->next
);
3307 dpi
->templates
= hold_dpt
;
3310 else if (mods
->mod
->type
== D_COMP_LOCAL_NAME
)
3312 struct d_print_mod
*hold_modifiers
;
3315 /* When this is on the modifier stack, we have pulled any
3316 qualifiers off the right argument already. Otherwise, we
3317 print it as usual, but don't let the left argument see any
3320 hold_modifiers
= dpi
->modifiers
;
3321 dpi
->modifiers
= NULL
;
3322 d_print_comp (dpi
, d_left (mods
->mod
));
3323 dpi
->modifiers
= hold_modifiers
;
3325 d_append_string (dpi
, (dpi
->options
& DMGL_JAVA
) == 0 ? "::" : ".");
3327 dc
= d_right (mods
->mod
);
3328 while (dc
->type
== D_COMP_RESTRICT_THIS
3329 || dc
->type
== D_COMP_VOLATILE_THIS
3330 || dc
->type
== D_COMP_CONST_THIS
)
3333 d_print_comp (dpi
, dc
);
3335 dpi
->templates
= hold_dpt
;
3339 d_print_mod (dpi
, mods
->mod
);
3341 dpi
->templates
= hold_dpt
;
3343 d_print_mod_list (dpi
, mods
->next
, suffix
);
3346 /* Print a modifier. */
3349 d_print_mod (dpi
, mod
)
3350 struct d_print_info
*dpi
;
3351 const struct d_comp
*mod
;
3355 case D_COMP_RESTRICT
:
3356 case D_COMP_RESTRICT_THIS
:
3357 d_append_string (dpi
, " restrict");
3359 case D_COMP_VOLATILE
:
3360 case D_COMP_VOLATILE_THIS
:
3361 d_append_string (dpi
, " volatile");
3364 case D_COMP_CONST_THIS
:
3365 d_append_string (dpi
, " const");
3367 case D_COMP_VENDOR_TYPE_QUAL
:
3368 d_append_char (dpi
, ' ');
3369 d_print_comp (dpi
, d_right (mod
));
3371 case D_COMP_POINTER
:
3372 /* There is no pointer symbol in Java. */
3373 if ((dpi
->options
& DMGL_JAVA
) == 0)
3374 d_append_char (dpi
, '*');
3376 case D_COMP_REFERENCE
:
3377 d_append_char (dpi
, '&');
3379 case D_COMP_COMPLEX
:
3380 d_append_string (dpi
, "complex ");
3382 case D_COMP_IMAGINARY
:
3383 d_append_string (dpi
, "imaginary ");
3385 case D_COMP_PTRMEM_TYPE
:
3386 if (d_last_char (dpi
) != '(')
3387 d_append_char (dpi
, ' ');
3388 d_print_comp (dpi
, d_left (mod
));
3389 d_append_string (dpi
, "::*");
3391 case D_COMP_TYPED_NAME
:
3392 d_print_comp (dpi
, d_left (mod
));
3395 /* Otherwise, we have something that won't go back on the
3396 modifier stack, so we can just print it. */
3397 d_print_comp (dpi
, mod
);
3402 /* Print a function type, except for the return type. */
3405 d_print_function_type (dpi
, dc
, mods
)
3406 struct d_print_info
*dpi
;
3407 const struct d_comp
*dc
;
3408 struct d_print_mod
*mods
;
3412 struct d_print_mod
*p
;
3413 struct d_print_mod
*hold_modifiers
;
3417 for (p
= mods
; p
!= NULL
; p
= p
->next
)
3423 switch (p
->mod
->type
)
3425 case D_COMP_RESTRICT
:
3426 case D_COMP_VOLATILE
:
3428 case D_COMP_VENDOR_TYPE_QUAL
:
3429 case D_COMP_POINTER
:
3430 case D_COMP_REFERENCE
:
3431 case D_COMP_COMPLEX
:
3432 case D_COMP_IMAGINARY
:
3433 case D_COMP_PTRMEM_TYPE
:
3436 case D_COMP_RESTRICT_THIS
:
3437 case D_COMP_VOLATILE_THIS
:
3438 case D_COMP_CONST_THIS
:
3447 if (d_left (dc
) != NULL
&& ! saw_mod
)
3452 switch (d_last_char (dpi
))
3460 d_append_char (dpi
, ' ');
3464 d_append_char (dpi
, '(');
3467 hold_modifiers
= dpi
->modifiers
;
3468 dpi
->modifiers
= NULL
;
3470 d_print_mod_list (dpi
, mods
, 0);
3473 d_append_char (dpi
, ')');
3475 d_append_char (dpi
, '(');
3477 if (d_right (dc
) != NULL
)
3478 d_print_comp (dpi
, d_right (dc
));
3480 d_append_char (dpi
, ')');
3482 d_print_mod_list (dpi
, mods
, 1);
3484 dpi
->modifiers
= hold_modifiers
;
3487 /* Print an array type, except for the element type. */
3490 d_print_array_type (dpi
, dc
, mods
)
3491 struct d_print_info
*dpi
;
3492 const struct d_comp
*dc
;
3493 struct d_print_mod
*mods
;
3501 struct d_print_mod
*p
;
3504 for (p
= mods
; p
!= NULL
; p
= p
->next
)
3509 if (p
->mod
->type
== D_COMP_ARRAY_TYPE
)
3523 d_append_string (dpi
, " (");
3525 d_print_mod_list (dpi
, mods
, 0);
3528 d_append_char (dpi
, ')');
3532 d_append_char (dpi
, ' ');
3534 d_append_char (dpi
, '[');
3536 if (d_left (dc
) != NULL
)
3537 d_print_comp (dpi
, d_left (dc
));
3539 d_append_char (dpi
, ']');
3542 /* Print an operator in an expression. */
3545 d_print_expr_op (dpi
, dc
)
3546 struct d_print_info
*dpi
;
3547 const struct d_comp
*dc
;
3549 if (dc
->type
== D_COMP_OPERATOR
)
3550 d_append_string (dpi
, dc
->u
.s_operator
.op
->name
);
3552 d_print_comp (dpi
, dc
);
3558 d_print_cast (dpi
, dc
)
3559 struct d_print_info
*dpi
;
3560 const struct d_comp
*dc
;
3562 if (d_left (dc
)->type
!= D_COMP_TEMPLATE
)
3563 d_print_comp (dpi
, d_left (dc
));
3566 struct d_print_mod
*hold_dpm
;
3567 struct d_print_template dpt
;
3569 /* It appears that for a templated cast operator, we need to put
3570 the template parameters in scope for the operator name, but
3571 not for the parameters. The effect is that we need to handle
3572 the template printing here. */
3574 hold_dpm
= dpi
->modifiers
;
3575 dpi
->modifiers
= NULL
;
3577 dpt
.next
= dpi
->templates
;
3578 dpi
->templates
= &dpt
;
3579 dpt
.template = d_left (dc
);
3581 d_print_comp (dpi
, d_left (d_left (dc
)));
3583 dpi
->templates
= dpt
.next
;
3585 if (d_last_char (dpi
) == '<')
3586 d_append_char (dpi
, ' ');
3587 d_append_char (dpi
, '<');
3588 d_print_comp (dpi
, d_right (d_left (dc
)));
3589 /* Avoid generating two consecutive '>' characters, to avoid
3590 the C++ syntactic ambiguity. */
3591 if (d_last_char (dpi
) == '>')
3592 d_append_char (dpi
, ' ');
3593 d_append_char (dpi
, '>');
3595 dpi
->modifiers
= hold_dpm
;
3599 /* Initialize the information structure we use to pass around
3603 d_init_info (mangled
, options
, len
, di
)
3604 const char *mangled
;
3610 di
->options
= options
;
3614 /* We can not need more components than twice the number of chars in
3615 the mangled string. Most components correspond directly to
3616 chars, but the ARGLIST types are exceptions. */
3617 di
->num_comps
= 2 * len
;
3618 di
->comps
= (struct d_comp
*) malloc (di
->num_comps
3619 * sizeof (struct d_comp
));
3622 /* Similarly, we can not need more substitutions than there are
3623 chars in the mangled string. */
3625 di
->subs
= (struct d_comp
**) malloc (di
->num_subs
3626 * sizeof (struct d_comp
*));
3629 di
->last_name
= NULL
;
3631 if (di
->comps
== NULL
|| di
->subs
== NULL
)
3633 if (di
->comps
!= NULL
)
3635 if (di
->subs
!= NULL
)
3643 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
3644 name, return a buffer allocated with malloc holding the demangled
3645 name. OPTIONS is the usual libiberty demangler options. On
3646 success, this sets *PALC to the allocated size of the returned
3647 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
3648 a memory allocation failure. On failure, this returns NULL. */
3651 d_demangle (mangled
, options
, palc
)
3652 const char* mangled
;
3664 len
= strlen (mangled
);
3666 if (mangled
[0] == '_' && mangled
[1] == 'Z')
3668 else if (strncmp (mangled
, "_GLOBAL_", 8) == 0
3669 && (mangled
[8] == '.' || mangled
[8] == '_' || mangled
[8] == '$')
3670 && (mangled
[9] == 'D' || mangled
[9] == 'I')
3671 && mangled
[10] == '_')
3675 r
= malloc (40 + len
- 11);
3680 if (mangled
[9] == 'I')
3681 strcpy (r
, "global constructors keyed to ");
3683 strcpy (r
, "global destructors keyed to ");
3684 strcat (r
, mangled
+ 11);
3690 if ((options
& DMGL_TYPES
) == 0)
3695 if (! d_init_info (mangled
, options
, len
, &di
))
3702 dc
= d_mangled_name (&di
, 1);
3706 /* If DMGL_PARAMS is set, then if we didn't consume the entire
3707 mangled string, then we didn't successfully demangle it. If
3708 DMGL_PARAMS is not set, we didn't look at the trailing
3710 if (((options
& DMGL_PARAMS
) != 0) && d_peek_char (&di
) != '\0')
3713 #ifdef CP_DEMANGLE_DEBUG
3715 printf ("failed demangling\n");
3725 ret
= d_print (options
, dc
, palc
);
3732 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
3734 extern char *__cxa_demangle
PARAMS ((const char *, char *, size_t *, int *));
3736 /* ia64 ABI-mandated entry point in the C++ runtime library for
3737 performing demangling. MANGLED_NAME is a NUL-terminated character
3738 string containing the name to be demangled.
3740 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
3741 *LENGTH bytes, into which the demangled name is stored. If
3742 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
3743 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
3744 is placed in a region of memory allocated with malloc.
3746 If LENGTH is non-NULL, the length of the buffer conaining the
3747 demangled name, is placed in *LENGTH.
3749 The return value is a pointer to the start of the NUL-terminated
3750 demangled name, or NULL if the demangling fails. The caller is
3751 responsible for deallocating this memory using free.
3753 *STATUS is set to one of the following values:
3754 0: The demangling operation succeeded.
3755 -1: A memory allocation failure occurred.
3756 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
3757 -3: One of the arguments is invalid.
3759 The demangling is performed using the C++ ABI mangling rules, with
3763 __cxa_demangle (mangled_name
, output_buffer
, length
, status
)
3764 const char *mangled_name
;
3765 char *output_buffer
;
3775 if (mangled_name
== NULL
)
3781 if (output_buffer
!= NULL
&& length
== NULL
)
3787 demangled
= d_demangle (mangled_name
, DMGL_TYPES
, &alc
);
3789 if (demangled
== NULL
)
3798 if (output_buffer
== NULL
)
3805 if (strlen (demangled
) < *length
)
3807 strcpy (output_buffer
, demangled
);
3809 demangled
= output_buffer
;
3813 free (output_buffer
);
3823 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
3825 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
3826 mangled name, return a buffer allocated with malloc holding the
3827 demangled name. Otherwise, return NULL. */
3830 cplus_demangle_v3 (mangled
, options
)
3831 const char* mangled
;
3836 return d_demangle (mangled
, options
, &alc
);
3839 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
3840 conventions, but the output formatting is a little different.
3841 This instructs the C++ demangler not to emit pointer characters ("*"), and
3842 to use Java's namespace separator symbol ("." instead of "::"). It then
3843 does an additional pass over the demangled output to replace instances
3844 of JArray<TYPE> with TYPE[]. */
3847 java_demangle_v3 (mangled
)
3848 const char* mangled
;
3856 demangled
= d_demangle (mangled
, DMGL_JAVA
| DMGL_PARAMS
, &alc
);
3858 if (demangled
== NULL
)
3864 while (*from
!= '\0')
3866 if (strncmp (from
, "JArray<", 7) == 0)
3871 else if (nesting
> 0 && *from
== '>')
3873 while (to
> demangled
&& to
[-1] == ' ')
3889 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
3891 #ifndef IN_GLIBCPP_V3
3893 /* Demangle a string in order to find out whether it is a constructor
3894 or destructor. Return non-zero on success. Set *CTOR_KIND and
3895 *DTOR_KIND appropriately. */
3898 is_ctor_or_dtor (mangled
, ctor_kind
, dtor_kind
)
3899 const char *mangled
;
3900 enum gnu_v3_ctor_kinds
*ctor_kind
;
3901 enum gnu_v3_dtor_kinds
*dtor_kind
;
3907 *ctor_kind
= (enum gnu_v3_ctor_kinds
) 0;
3908 *dtor_kind
= (enum gnu_v3_dtor_kinds
) 0;
3910 if (! d_init_info (mangled
, DMGL_GNU_V3
, strlen (mangled
), &di
))
3913 dc
= d_mangled_name (&di
, 1);
3915 /* Note that because we did not pass DMGL_PARAMS, we don't expect to
3916 demangle the entire string. */
3926 case D_COMP_TYPED_NAME
:
3927 case D_COMP_TEMPLATE
:
3928 case D_COMP_RESTRICT_THIS
:
3929 case D_COMP_VOLATILE_THIS
:
3930 case D_COMP_CONST_THIS
:
3933 case D_COMP_QUAL_NAME
:
3934 case D_COMP_LOCAL_NAME
:
3938 *ctor_kind
= dc
->u
.s_ctor
.kind
;
3943 *dtor_kind
= dc
->u
.s_dtor
.kind
;
3956 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
3957 name. A non-zero return indicates the type of constructor. */
3959 enum gnu_v3_ctor_kinds
3960 is_gnu_v3_mangled_ctor (name
)
3963 enum gnu_v3_ctor_kinds ctor_kind
;
3964 enum gnu_v3_dtor_kinds dtor_kind
;
3966 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
3967 return (enum gnu_v3_ctor_kinds
) 0;
3972 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
3973 name. A non-zero return indicates the type of destructor. */
3975 enum gnu_v3_dtor_kinds
3976 is_gnu_v3_mangled_dtor (name
)
3979 enum gnu_v3_ctor_kinds ctor_kind
;
3980 enum gnu_v3_dtor_kinds dtor_kind
;
3982 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
3983 return (enum gnu_v3_dtor_kinds
) 0;
3987 #endif /* IN_GLIBCPP_V3 */
3989 #ifdef STANDALONE_DEMANGLER
3992 #include "dyn-string.h"
3994 static void print_usage
PARAMS ((FILE* fp
, int exit_value
));
3996 #define IS_ALPHA(CHAR) \
3997 (((CHAR) >= 'a' && (CHAR) <= 'z') \
3998 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
4000 /* Non-zero if CHAR is a character than can occur in a mangled name. */
4001 #define is_mangled_char(CHAR) \
4002 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
4003 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
4005 /* The name of this program, as invoked. */
4006 const char* program_name
;
4008 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
4011 print_usage (fp
, exit_value
)
4015 fprintf (fp
, "Usage: %s [options] [names ...]\n", program_name
);
4016 fprintf (fp
, "Options:\n");
4017 fprintf (fp
, " -h,--help Display this message.\n");
4018 fprintf (fp
, " -p,--no-params Don't display function parameters\n");
4019 fprintf (fp
, " -v,--verbose Produce verbose demanglings.\n");
4020 fprintf (fp
, "If names are provided, they are demangled. Otherwise filters standard input.\n");
4025 /* Option specification for getopt_long. */
4026 static const struct option long_options
[] =
4028 { "help", no_argument
, NULL
, 'h' },
4029 { "no-params", no_argument
, NULL
, 'p' },
4030 { "verbose", no_argument
, NULL
, 'v' },
4031 { NULL
, no_argument
, NULL
, 0 },
4034 /* Main entry for a demangling filter executable. It will demangle
4035 its command line arguments, if any. If none are provided, it will
4036 filter stdin to stdout, replacing any recognized mangled C++ names
4037 with their demangled equivalents. */
4046 int options
= DMGL_PARAMS
| DMGL_ANSI
| DMGL_TYPES
;
4048 /* Use the program name of this program, as invoked. */
4049 program_name
= argv
[0];
4051 /* Parse options. */
4054 opt_char
= getopt_long (argc
, argv
, "hpv", long_options
, NULL
);
4057 case '?': /* Unrecognized option. */
4058 print_usage (stderr
, 1);
4062 print_usage (stdout
, 0);
4066 options
&= ~ DMGL_PARAMS
;
4070 options
|= DMGL_VERBOSE
;
4074 while (opt_char
!= -1);
4077 /* No command line arguments were provided. Filter stdin. */
4079 dyn_string_t mangled
= dyn_string_new (3);
4082 /* Read all of input. */
4083 while (!feof (stdin
))
4087 /* Pile characters into mangled until we hit one that can't
4088 occur in a mangled name. */
4090 while (!feof (stdin
) && is_mangled_char (c
))
4092 dyn_string_append_char (mangled
, c
);
4098 if (dyn_string_length (mangled
) > 0)
4100 s
= cplus_demangle_v3 (dyn_string_buf (mangled
), options
);
4109 /* It might not have been a mangled name. Print the
4111 fputs (dyn_string_buf (mangled
), stdout
);
4114 dyn_string_clear (mangled
);
4117 /* If we haven't hit EOF yet, we've read one character that
4118 can't occur in a mangled name, so print it out. */
4123 dyn_string_delete (mangled
);
4126 /* Demangle command line arguments. */
4128 /* Loop over command line arguments. */
4129 for (i
= optind
; i
< argc
; ++i
)
4133 /* Attempt to demangle. */
4134 s
= cplus_demangle_v3 (argv
[i
], options
);
4136 /* If it worked, print the demangled name. */
4143 fprintf (stderr
, "Failed: %s\n", argv
[i
]);
4150 #endif /* STANDALONE_DEMANGLER */