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 /* Component types found in mangled names. */
150 /* A qualified name. */
156 /* A template parameter. */
157 D_COMP_TEMPLATE_PARAM
,
164 /* A VTT structure. */
166 /* A construction vtable. */
167 D_COMP_CONSTRUCTION_VTABLE
,
168 /* A typeinfo structure. */
170 /* A typeinfo name. */
171 D_COMP_TYPEINFO_NAME
,
172 /* A typeinfo function. */
176 /* A virtual thunk. */
177 D_COMP_VIRTUAL_THUNK
,
178 /* A covariant thunk. */
179 D_COMP_COVARIANT_THUNK
,
182 /* A guard variable. */
184 /* A reference temporary. */
186 /* A standard substitution. */
188 /* The restrict qualifier. */
190 /* The volatile qualifier. */
192 /* The const qualifier. */
194 /* The restrict qualifier modifying a member function. */
195 D_COMP_RESTRICT_THIS
,
196 /* The volatile qualifier modifying a member function. */
197 D_COMP_VOLATILE_THIS
,
198 /* The const qualifier modifying a member function. */
200 /* A vendor qualifier. */
201 D_COMP_VENDOR_TYPE_QUAL
,
206 /* A complex type. */
208 /* An imaginary type. */
210 /* A builtin type. */
212 /* A vendor's builtin type. */
214 /* A function type. */
215 D_COMP_FUNCTION_TYPE
,
218 /* A pointer to member type. */
220 /* An argument list. */
222 /* A template argument list. */
223 D_COMP_TEMPLATE_ARGLIST
,
226 /* An extended operator. */
227 D_COMP_EXTENDED_OPERATOR
,
230 /* A unary expression. */
232 /* A binary expression. */
234 /* Arguments to a binary expression. */
236 /* A trinary expression. */
238 /* Arguments to a trinary expression. */
245 /* A component of the mangled name. */
249 /* The type of this component. */
250 enum d_comp_type type
;
253 /* For D_COMP_NAME. */
256 /* A pointer to the name (not NULL terminated) and it's
262 /* For D_COMP_OPERATOR. */
266 const struct d_operator_info
*op
;
269 /* For D_COMP_EXTENDED_OPERATOR. */
272 /* Number of arguments. */
276 } s_extended_operator
;
278 /* For D_COMP_CTOR. */
281 enum gnu_v3_ctor_kinds kind
;
285 /* For D_COMP_DTOR. */
288 enum gnu_v3_dtor_kinds kind
;
292 /* For D_COMP_BUILTIN_TYPE. */
295 const struct d_builtin_type_info
*type
;
298 /* For D_COMP_SUB_STD. */
304 /* For D_COMP_TEMPLATE_PARAM. */
310 /* For other types. */
314 struct d_comp
*right
;
320 #define d_left(dc) ((dc)->u.s_binary.left)
321 #define d_right(dc) ((dc)->u.s_binary.right)
323 /* The information structure we pass around. */
327 /* The string we are demangling. */
329 /* The options passed to the demangler. */
331 /* The next character in the string to consider. */
333 /* The array of components. */
334 struct d_comp
*comps
;
335 /* The index of the next available component. */
337 /* The number of available component structures. */
339 /* The array of substitutions. */
340 struct d_comp
**subs
;
341 /* The index of the next substitution. */
343 /* The number of available entries in the subs array. */
345 /* The last name we saw, for constructors and destructors. */
346 struct d_comp
*last_name
;
349 #define d_peek_char(di) (*((di)->n))
350 #define d_peek_next_char(di) ((di)->n[1])
351 #define d_advance(di, i) ((di)->n += (i))
352 #define d_next_char(di) (*((di)->n++))
353 #define d_str(di) ((di)->n)
355 /* A list of templates. This is used while printing. */
357 struct d_print_template
359 /* Next template on the list. */
360 struct d_print_template
*next
;
362 const struct d_comp
*template;
365 /* A list of type modifiers. This is used while printing. */
369 /* Next modifier on the list. These are in the reverse of the order
370 in which they appeared in the mangled string. */
371 struct d_print_mod
*next
;
373 const struct d_comp
*mod
;
374 /* Whether this modifier was printed. */
376 /* The list of templates which applies to this modifier. */
377 struct d_print_template
*templates
;
380 /* We use this structure to hold information during printing. */
384 /* The options passed to the demangler. */
386 /* Buffer holding the result. */
388 /* Current length of data in buffer. */
390 /* Allocated size of buffer. */
392 /* The current list of templates, if any. */
393 struct d_print_template
*templates
;
394 /* The current list of modifiers (e.g., pointer, reference, etc.),
396 struct d_print_mod
*modifiers
;
397 /* Set to 1 if we had a memory allocation failure. */
398 int allocation_failure
;
401 #define d_print_saw_error(dpi) ((dpi)->buf == NULL)
403 #define d_append_char(dpi, c) \
406 if ((dpi)->buf != NULL && (dpi)->len < (dpi)->alc) \
407 (dpi)->buf[(dpi)->len++] = (c); \
409 d_print_append_char ((dpi), (c)); \
413 #define d_append_buffer(dpi, s, l) \
416 if ((dpi)->buf != NULL && (dpi)->len + (l) <= (dpi)->alc) \
418 memcpy ((dpi)->buf + (dpi)->len, (s), (l)); \
422 d_print_append_buffer ((dpi), (s), (l)); \
426 #define d_append_string(dpi, s) \
429 size_t d_append_string_len = strlen (s); \
430 d_append_buffer ((dpi), (s), d_append_string_len); \
434 #define d_last_char(dpi) \
435 ((dpi)->buf == NULL || (dpi)->len == 0 ? '\0' : (dpi)->buf[(dpi)->len - 1])
437 #ifdef CP_DEMANGLE_DEBUG
438 static void d_dump
PARAMS ((struct d_comp
*, int));
440 static struct d_comp
*d_make_empty
PARAMS ((struct d_info
*,
442 static struct d_comp
*d_make_comp
PARAMS ((struct d_info
*, enum d_comp_type
,
443 struct d_comp
*, struct d_comp
*));
444 static struct d_comp
*d_make_name
PARAMS ((struct d_info
*, const char *,
446 static struct d_comp
*d_make_builtin_type
PARAMS ((struct d_info
*,
447 const struct d_builtin_type_info
*));
448 static struct d_comp
*d_make_operator
PARAMS ((struct d_info
*,
449 const struct d_operator_info
*));
450 static struct d_comp
*d_make_extended_operator
PARAMS ((struct d_info
*,
453 static struct d_comp
*d_make_ctor
PARAMS ((struct d_info
*,
454 enum gnu_v3_ctor_kinds
,
456 static struct d_comp
*d_make_dtor
PARAMS ((struct d_info
*,
457 enum gnu_v3_dtor_kinds
,
459 static struct d_comp
*d_make_template_param
PARAMS ((struct d_info
*, long));
460 static struct d_comp
*d_make_sub
PARAMS ((struct d_info
*, const char *));
461 static struct d_comp
*d_mangled_name
PARAMS ((struct d_info
*, int));
462 static int has_return_type
PARAMS ((struct d_comp
*));
463 static int is_ctor_dtor_or_conversion
PARAMS ((struct d_comp
*));
464 static struct d_comp
*d_encoding
PARAMS ((struct d_info
*, int));
465 static struct d_comp
*d_name
PARAMS ((struct d_info
*));
466 static struct d_comp
*d_nested_name
PARAMS ((struct d_info
*));
467 static struct d_comp
*d_prefix
PARAMS ((struct d_info
*));
468 static struct d_comp
*d_unqualified_name
PARAMS ((struct d_info
*));
469 static struct d_comp
*d_source_name
PARAMS ((struct d_info
*));
470 static long d_number
PARAMS ((struct d_info
*));
471 static struct d_comp
*d_identifier
PARAMS ((struct d_info
*, int));
472 static struct d_comp
*d_operator_name
PARAMS ((struct d_info
*));
473 static struct d_comp
*d_special_name
PARAMS ((struct d_info
*));
474 static int d_call_offset
PARAMS ((struct d_info
*, int));
475 static struct d_comp
*d_ctor_dtor_name
PARAMS ((struct d_info
*));
476 static struct d_comp
*d_type
PARAMS ((struct d_info
*));
477 static struct d_comp
**d_cv_qualifiers
PARAMS ((struct d_info
*,
478 struct d_comp
**, int));
479 static struct d_comp
*d_function_type
PARAMS ((struct d_info
*));
480 static struct d_comp
*d_bare_function_type
PARAMS ((struct d_info
*, int));
481 static struct d_comp
*d_class_enum_type
PARAMS ((struct d_info
*));
482 static struct d_comp
*d_array_type
PARAMS ((struct d_info
*));
483 static struct d_comp
*d_pointer_to_member_type
PARAMS ((struct d_info
*));
484 static struct d_comp
*d_template_param
PARAMS ((struct d_info
*));
485 static struct d_comp
*d_template_args
PARAMS ((struct d_info
*));
486 static struct d_comp
*d_template_arg
PARAMS ((struct d_info
*));
487 static struct d_comp
*d_expression
PARAMS ((struct d_info
*));
488 static struct d_comp
*d_expr_primary
PARAMS ((struct d_info
*));
489 static struct d_comp
*d_local_name
PARAMS ((struct d_info
*));
490 static int d_discriminator
PARAMS ((struct d_info
*));
491 static int d_add_substitution
PARAMS ((struct d_info
*, struct d_comp
*));
492 static struct d_comp
*d_substitution
PARAMS ((struct d_info
*));
493 static void d_print_resize
PARAMS ((struct d_print_info
*, size_t));
494 static void d_print_append_char
PARAMS ((struct d_print_info
*, int));
495 static void d_print_append_buffer
PARAMS ((struct d_print_info
*, const char *,
497 static void d_print_error
PARAMS ((struct d_print_info
*));
498 static char *d_print
PARAMS ((int, const struct d_comp
*, size_t *));
499 static void d_print_comp
PARAMS ((struct d_print_info
*,
500 const struct d_comp
*));
501 static void d_print_identifier
PARAMS ((struct d_print_info
*, const char *,
503 static void d_print_mod_list
PARAMS ((struct d_print_info
*,
504 struct d_print_mod
*, int));
505 static void d_print_mod
PARAMS ((struct d_print_info
*,
506 const struct d_comp
*));
507 static void d_print_function_type
PARAMS ((struct d_print_info
*,
508 const struct d_comp
*,
509 struct d_print_mod
*));
510 static void d_print_array_type
PARAMS ((struct d_print_info
*,
511 const struct d_comp
*,
512 struct d_print_mod
*));
513 static void d_print_expr_op
PARAMS ((struct d_print_info
*,
514 const struct d_comp
*));
515 static void d_print_cast
PARAMS ((struct d_print_info
*,
516 const struct d_comp
*));
517 static int d_init_info
PARAMS ((const char *, int, size_t, struct d_info
*));
518 static char *d_demangle
PARAMS ((const char *, int, size_t *));
520 #ifdef CP_DEMANGLE_DEBUG
532 for (i
= 0; i
< indent
; ++i
)
538 printf ("name '%.*s'\n", dc
->u
.s_name
.len
, dc
->u
.s_name
.s
);
540 case D_COMP_TEMPLATE_PARAM
:
541 printf ("template parameter %ld\n", dc
->u
.s_number
.number
);
544 printf ("constructor %d\n", (int) dc
->u
.s_ctor
.kind
);
545 d_dump (dc
->u
.s_ctor
.name
, indent
+ 2);
548 printf ("destructor %d\n", (int) dc
->u
.s_dtor
.kind
);
549 d_dump (dc
->u
.s_dtor
.name
, indent
+ 2);
552 printf ("standard substitution %s\n", dc
->u
.s_string
.string
);
554 case D_COMP_BUILTIN_TYPE
:
555 printf ("builtin type %s\n", dc
->u
.s_builtin
.type
->name
);
557 case D_COMP_OPERATOR
:
558 printf ("operator %s\n", dc
->u
.s_operator
.op
->name
);
560 case D_COMP_EXTENDED_OPERATOR
:
561 printf ("extended operator with %d args\n",
562 dc
->u
.s_extended_operator
.args
);
563 d_dump (dc
->u
.s_extended_operator
.name
, indent
+ 2);
566 case D_COMP_QUAL_NAME
:
567 printf ("qualified name\n");
569 case D_COMP_TYPED_NAME
:
570 printf ("typed name\n");
572 case D_COMP_TEMPLATE
:
573 printf ("template\n");
581 case D_COMP_CONSTRUCTION_VTABLE
:
582 printf ("construction vtable\n");
584 case D_COMP_TYPEINFO
:
585 printf ("typeinfo\n");
587 case D_COMP_TYPEINFO_NAME
:
588 printf ("typeinfo name\n");
590 case D_COMP_TYPEINFO_FN
:
591 printf ("typeinfo function\n");
596 case D_COMP_VIRTUAL_THUNK
:
597 printf ("virtual thunk\n");
599 case D_COMP_COVARIANT_THUNK
:
600 printf ("covariant thunk\n");
602 case D_COMP_JAVA_CLASS
:
603 printf ("java class\n");
609 printf ("reference temporary\n");
611 case D_COMP_RESTRICT
:
612 printf ("restrict\n");
614 case D_COMP_VOLATILE
:
615 printf ("volatile\n");
620 case D_COMP_RESTRICT_THIS
:
621 printf ("restrict this\n");
623 case D_COMP_VOLATILE_THIS
:
624 printf ("volatile this\n");
626 case D_COMP_CONST_THIS
:
627 printf ("const this\n");
629 case D_COMP_VENDOR_TYPE_QUAL
:
630 printf ("vendor type qualifier\n");
633 printf ("pointer\n");
635 case D_COMP_REFERENCE
:
636 printf ("reference\n");
639 printf ("complex\n");
641 case D_COMP_IMAGINARY
:
642 printf ("imaginary\n");
644 case D_COMP_VENDOR_TYPE
:
645 printf ("vendor type\n");
647 case D_COMP_FUNCTION_TYPE
:
648 printf ("function type\n");
650 case D_COMP_ARRAY_TYPE
:
651 printf ("array type\n");
653 case D_COMP_PTRMEM_TYPE
:
654 printf ("pointer to member type\n");
657 printf ("argument list\n");
659 case D_COMP_TEMPLATE_ARGLIST
:
660 printf ("template argument list\n");
666 printf ("unary operator\n");
669 printf ("binary operator\n");
671 case D_COMP_BINARY_ARGS
:
672 printf ("binary operator arguments\n");
675 printf ("trinary operator\n");
677 case D_COMP_TRINARY_ARG1
:
678 printf ("trinary operator arguments 1\n");
680 case D_COMP_TRINARY_ARG2
:
681 printf ("trinary operator arguments 1\n");
684 printf ("literal\n");
688 d_dump (d_left (dc
), indent
+ 2);
689 d_dump (d_right (dc
), indent
+ 2);
692 #endif /* CP_DEMANGLE_DEBUG */
694 /* Add a new component. */
696 static struct d_comp
*
697 d_make_empty (di
, type
)
699 enum d_comp_type type
;
703 if (di
->next_comp
>= di
->num_comps
)
705 p
= &di
->comps
[di
->next_comp
];
711 /* Add a new generic component. */
713 static struct d_comp
*
714 d_make_comp (di
, type
, left
, right
)
716 enum d_comp_type type
;
718 struct d_comp
*right
;
722 /* We check for errors here. A typical error would be a NULL return
723 from a subroutine. We catch those here, and return NULL
727 /* These types require two parameters. */
728 case D_COMP_QUAL_NAME
:
729 case D_COMP_TYPED_NAME
:
730 case D_COMP_TEMPLATE
:
731 case D_COMP_VENDOR_TYPE_QUAL
:
732 case D_COMP_PTRMEM_TYPE
:
735 case D_COMP_BINARY_ARGS
:
737 case D_COMP_TRINARY_ARG1
:
738 case D_COMP_TRINARY_ARG2
:
740 if (left
== NULL
|| right
== NULL
)
744 /* These types only require one parameter. */
747 case D_COMP_CONSTRUCTION_VTABLE
:
748 case D_COMP_TYPEINFO
:
749 case D_COMP_TYPEINFO_NAME
:
750 case D_COMP_TYPEINFO_FN
:
752 case D_COMP_VIRTUAL_THUNK
:
753 case D_COMP_COVARIANT_THUNK
:
754 case D_COMP_JAVA_CLASS
:
758 case D_COMP_REFERENCE
:
760 case D_COMP_IMAGINARY
:
761 case D_COMP_VENDOR_TYPE
:
763 case D_COMP_TEMPLATE_ARGLIST
:
769 /* This needs a right parameter, but the left parameter can be
771 case D_COMP_ARRAY_TYPE
:
776 /* These are allowed to have no parameters--in some cases they
777 will be filled in later. */
778 case D_COMP_FUNCTION_TYPE
:
779 case D_COMP_RESTRICT
:
780 case D_COMP_VOLATILE
:
782 case D_COMP_RESTRICT_THIS
:
783 case D_COMP_VOLATILE_THIS
:
784 case D_COMP_CONST_THIS
:
787 /* Other types should not be seen here. */
792 p
= d_make_empty (di
, type
);
795 p
->u
.s_binary
.left
= left
;
796 p
->u
.s_binary
.right
= right
;
801 /* Add a new name component. */
803 static struct d_comp
*
804 d_make_name (di
, s
, len
)
811 if (s
== NULL
|| len
== 0)
813 p
= d_make_empty (di
, D_COMP_NAME
);
817 p
->u
.s_name
.len
= len
;
822 /* Add a new builtin type component. */
824 static struct d_comp
*
825 d_make_builtin_type (di
, type
)
827 const struct d_builtin_type_info
*type
;
833 p
= d_make_empty (di
, D_COMP_BUILTIN_TYPE
);
835 p
->u
.s_builtin
.type
= type
;
839 /* Add a new operator component. */
841 static struct d_comp
*
842 d_make_operator (di
, op
)
844 const struct d_operator_info
*op
;
848 p
= d_make_empty (di
, D_COMP_OPERATOR
);
850 p
->u
.s_operator
.op
= op
;
854 /* Add a new extended operator component. */
856 static struct d_comp
*
857 d_make_extended_operator (di
, args
, name
)
866 p
= d_make_empty (di
, D_COMP_EXTENDED_OPERATOR
);
869 p
->u
.s_extended_operator
.args
= args
;
870 p
->u
.s_extended_operator
.name
= name
;
875 /* Add a new constructor component. */
877 static struct d_comp
*
878 d_make_ctor (di
, kind
, name
)
880 enum gnu_v3_ctor_kinds kind
;
887 p
= d_make_empty (di
, D_COMP_CTOR
);
890 p
->u
.s_ctor
.kind
= kind
;
891 p
->u
.s_ctor
.name
= name
;
896 /* Add a new destructor component. */
898 static struct d_comp
*
899 d_make_dtor (di
, kind
, name
)
901 enum gnu_v3_dtor_kinds kind
;
908 p
= d_make_empty (di
, D_COMP_DTOR
);
911 p
->u
.s_dtor
.kind
= kind
;
912 p
->u
.s_dtor
.name
= name
;
917 /* Add a new template parameter. */
919 static struct d_comp
*
920 d_make_template_param (di
, i
)
926 p
= d_make_empty (di
, D_COMP_TEMPLATE_PARAM
);
928 p
->u
.s_number
.number
= i
;
932 /* Add a new standard substitution component. */
934 static struct d_comp
*
935 d_make_sub (di
, name
)
941 p
= d_make_empty (di
, D_COMP_SUB_STD
);
943 p
->u
.s_string
.string
= name
;
947 /* <mangled-name> ::= _Z <encoding>
949 TOP_LEVEL is non-zero when called at the top level. */
951 static struct d_comp
*
952 d_mangled_name (di
, top_level
)
956 if (d_next_char (di
) != '_')
958 if (d_next_char (di
) != 'Z')
960 return d_encoding (di
, top_level
);
963 /* Return whether a function should have a return type. The argument
964 is the function name, which may be qualified in various ways. The
965 rules are that template functions have return types with some
966 exceptions, function types which are not part of a function name
967 mangling have return types with some exceptions, and non-template
968 function names do not have return types. The exceptions are that
969 constructors, destructors, and conversion operators do not have
982 case D_COMP_TEMPLATE
:
983 return ! is_ctor_dtor_or_conversion (d_left (dc
));
984 case D_COMP_RESTRICT_THIS
:
985 case D_COMP_VOLATILE_THIS
:
986 case D_COMP_CONST_THIS
:
987 return has_return_type (d_left (dc
));
991 /* Return whether a name is a constructor, a destructor, or a
992 conversion operator. */
995 is_ctor_dtor_or_conversion (dc
)
1004 case D_COMP_QUAL_NAME
:
1005 return is_ctor_dtor_or_conversion (d_right (dc
));
1013 /* <encoding> ::= <(function) name> <bare-function-type>
1017 TOP_LEVEL is non-zero when called at the top level, in which case
1018 if DMGL_PARAMS is not set we do not demangle the function
1019 parameters. We only set this at the top level, because otherwise
1020 we would not correctly demangle names in local scopes. */
1022 static struct d_comp
*
1023 d_encoding (di
, top_level
)
1027 char peek
= d_peek_char (di
);
1029 if (peek
== 'G' || peek
== 'T')
1030 return d_special_name (di
);
1037 if (dc
!= NULL
&& top_level
&& (di
->options
& DMGL_PARAMS
) == 0)
1039 /* Strip off any initial CV-qualifiers, as they really apply
1040 to the `this' parameter, and they were not output by the
1041 v2 demangler without DMGL_PARAMS. */
1042 while (dc
->type
== D_COMP_RESTRICT_THIS
1043 || dc
->type
== D_COMP_VOLATILE_THIS
1044 || dc
->type
== D_COMP_CONST_THIS
)
1049 peek
= d_peek_char (di
);
1050 if (peek
== '\0' || peek
== 'E')
1052 return d_make_comp (di
, D_COMP_TYPED_NAME
, dc
,
1053 d_bare_function_type (di
, has_return_type (dc
)));
1057 /* <name> ::= <nested-name>
1059 ::= <unscoped-template-name> <template-args>
1062 <unscoped-name> ::= <unqualified-name>
1063 ::= St <unqualified-name>
1065 <unscoped-template-name> ::= <unscoped-name>
1069 static struct d_comp
*
1073 char peek
= d_peek_char (di
);
1079 return d_nested_name (di
);
1082 return d_local_name (di
);
1088 if (d_peek_next_char (di
) != 't')
1090 dc
= d_substitution (di
);
1096 dc
= d_make_comp (di
, D_COMP_QUAL_NAME
, d_make_name (di
, "std", 3),
1097 d_unqualified_name (di
));
1101 if (d_peek_char (di
) != 'I')
1103 /* The grammar does not permit this case to occur if we
1104 called d_substitution() above (i.e., subst == 1). We
1105 don't bother to check. */
1109 /* This is <template-args>, which means that we just saw
1110 <unscoped-template-name>, which is a substitution
1111 candidate if we didn't just get it from a
1115 if (! d_add_substitution (di
, dc
))
1118 dc
= d_make_comp (di
, D_COMP_TEMPLATE
, dc
, d_template_args (di
));
1125 dc
= d_unqualified_name (di
);
1126 if (d_peek_char (di
) == 'I')
1128 /* This is <template-args>, which means that we just saw
1129 <unscoped-template-name>, which is a substitution
1131 if (! d_add_substitution (di
, dc
))
1133 dc
= d_make_comp (di
, D_COMP_TEMPLATE
, dc
, d_template_args (di
));
1139 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1140 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1143 static struct d_comp
*
1148 struct d_comp
**pret
;
1150 if (d_next_char (di
) != 'N')
1153 pret
= d_cv_qualifiers (di
, &ret
, 1);
1157 *pret
= d_prefix (di
);
1161 if (d_next_char (di
) != 'E')
1167 /* <prefix> ::= <prefix> <unqualified-name>
1168 ::= <template-prefix> <template-args>
1169 ::= <template-param>
1173 <template-prefix> ::= <prefix> <(template) unqualified-name>
1174 ::= <template-param>
1178 static struct d_comp
*
1182 struct d_comp
*ret
= NULL
;
1187 enum d_comp_type comb_type
;
1190 peek
= d_peek_char (di
);
1194 /* The older code accepts a <local-name> here, but I don't see
1195 that in the grammar. The older code does not accept a
1196 <template-param> here. */
1198 comb_type
= D_COMP_QUAL_NAME
;
1203 dc
= d_unqualified_name (di
);
1204 else if (peek
== 'S')
1205 dc
= d_substitution (di
);
1206 else if (peek
== 'I')
1210 comb_type
= D_COMP_TEMPLATE
;
1211 dc
= d_template_args (di
);
1213 else if (peek
== 'T')
1214 dc
= d_template_param (di
);
1215 else if (peek
== 'E')
1223 ret
= d_make_comp (di
, comb_type
, ret
, dc
);
1225 if (peek
!= 'S' && d_peek_char (di
) != 'E')
1227 if (! d_add_substitution (di
, ret
))
1233 /* <unqualified-name> ::= <operator-name>
1234 ::= <ctor-dtor-name>
1238 static struct d_comp
*
1239 d_unqualified_name (di
)
1244 peek
= d_peek_char (di
);
1245 if (IS_DIGIT (peek
))
1246 return d_source_name (di
);
1247 else if (IS_LOWER (peek
))
1248 return d_operator_name (di
);
1249 else if (peek
== 'C' || peek
== 'D')
1250 return d_ctor_dtor_name (di
);
1255 /* <source-name> ::= <(positive length) number> <identifier> */
1257 static struct d_comp
*
1264 len
= d_number (di
);
1267 ret
= d_identifier (di
, len
);
1268 di
->last_name
= ret
;
1272 /* number ::= [n] <(non-negative decimal integer)> */
1283 peek
= d_peek_char (di
);
1288 peek
= d_peek_char (di
);
1294 if (! IS_DIGIT (peek
))
1296 ret
= ret
* 10 + peek
- '0';
1298 peek
= d_peek_char (di
);
1302 /* identifier ::= <(unqualified source code identifier)> */
1304 static struct d_comp
*
1305 d_identifier (di
, len
)
1312 d_advance (di
, len
);
1314 /* Look for something which looks like a gcc encoding of an
1315 anonymous namespace, and replace it with a more user friendly
1317 if (len
>= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN
+ 2
1318 && memcmp (name
, ANONYMOUS_NAMESPACE_PREFIX
,
1319 ANONYMOUS_NAMESPACE_PREFIX_LEN
) == 0)
1323 s
= name
+ ANONYMOUS_NAMESPACE_PREFIX_LEN
;
1324 if ((*s
== '.' || *s
== '_' || *s
== '$')
1326 return d_make_name (di
, "(anonymous namespace)",
1327 sizeof "(anonymous namespace)" - 1);
1330 return d_make_name (di
, name
, len
);
1333 /* operator_name ::= many different two character encodings.
1335 ::= v <digit> <source-name>
1338 static const struct d_operator_info d_operators
[] =
1349 { "da", "delete[]", 1 },
1351 { "dl", "delete", 1 },
1368 { "na", "new[]", 1 },
1387 { "st", "sizeof ", 1 },
1388 { "sz", "sizeof ", 1 }
1391 static struct d_comp
*
1392 d_operator_name (di
)
1398 c1
= d_next_char (di
);
1399 c2
= d_next_char (di
);
1400 if (c1
== 'v' && IS_DIGIT (c2
))
1401 return d_make_extended_operator (di
, c2
- '0', d_source_name (di
));
1402 else if (c1
== 'c' && c2
== 'v')
1403 return d_make_comp (di
, D_COMP_CAST
, d_type (di
), NULL
);
1407 int high
= sizeof (d_operators
) / sizeof (d_operators
[0]);
1412 const struct d_operator_info
*p
;
1414 i
= low
+ (high
- low
) / 2;
1415 p
= d_operators
+ i
;
1417 if (c1
== p
->code
[0] && c2
== p
->code
[1])
1418 return d_make_operator (di
, p
);
1420 if (c1
< p
->code
[0] || (c1
== p
->code
[0] && c2
< p
->code
[1]))
1430 /* <special-name> ::= TV <type>
1434 ::= GV <(object) name>
1435 ::= T <call-offset> <(base) encoding>
1436 ::= Tc <call-offset> <call-offset> <(base) encoding>
1437 Also g++ extensions:
1438 ::= TC <type> <(offset) number> _ <(base) type>
1444 static struct d_comp
*
1450 c
= d_next_char (di
);
1453 switch (d_next_char (di
))
1456 return d_make_comp (di
, D_COMP_VTABLE
, d_type (di
), NULL
);
1458 return d_make_comp (di
, D_COMP_VTT
, d_type (di
), NULL
);
1460 return d_make_comp (di
, D_COMP_TYPEINFO
, d_type (di
), NULL
);
1462 return d_make_comp (di
, D_COMP_TYPEINFO_NAME
, d_type (di
), NULL
);
1465 if (! d_call_offset (di
, 'h'))
1467 return d_make_comp (di
, D_COMP_THUNK
, d_encoding (di
, 0), NULL
);
1470 if (! d_call_offset (di
, 'v'))
1472 return d_make_comp (di
, D_COMP_VIRTUAL_THUNK
, d_encoding (di
, 0),
1476 if (! d_call_offset (di
, '\0'))
1478 if (! d_call_offset (di
, '\0'))
1480 return d_make_comp (di
, D_COMP_COVARIANT_THUNK
, d_encoding (di
, 0),
1485 struct d_comp
*derived_type
;
1487 struct d_comp
*base_type
;
1489 derived_type
= d_type (di
);
1490 offset
= d_number (di
);
1493 if (d_next_char (di
) != '_')
1495 base_type
= d_type (di
);
1496 /* We don't display the offset. FIXME: We should display
1497 it in verbose mode. */
1498 return d_make_comp (di
, D_COMP_CONSTRUCTION_VTABLE
, base_type
,
1503 return d_make_comp (di
, D_COMP_TYPEINFO_FN
, d_type (di
), NULL
);
1505 return d_make_comp (di
, D_COMP_JAVA_CLASS
, d_type (di
), NULL
);
1513 switch (d_next_char (di
))
1516 return d_make_comp (di
, D_COMP_GUARD
, d_name (di
), NULL
);
1519 return d_make_comp (di
, D_COMP_REFTEMP
, d_name (di
), NULL
);
1529 /* <call-offset> ::= h <nv-offset> _
1532 <nv-offset> ::= <(offset) number>
1534 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1536 The C parameter, if not '\0', is a character we just read which is
1537 the start of the <call-offset>.
1539 We don't display the offset information anywhere. FIXME: We should
1540 display it in verbose mode. */
1543 d_call_offset (di
, c
)
1548 long virtual_offset
;
1551 c
= d_next_char (di
);
1554 offset
= d_number (di
);
1557 offset
= d_number (di
);
1558 if (d_next_char (di
) != '_')
1560 virtual_offset
= d_number (di
);
1565 if (d_next_char (di
) != '_')
1571 /* <ctor-dtor-name> ::= C1
1579 static struct d_comp
*
1580 d_ctor_dtor_name (di
)
1583 switch (d_next_char (di
))
1587 enum gnu_v3_ctor_kinds kind
;
1589 switch (d_next_char (di
))
1592 kind
= gnu_v3_complete_object_ctor
;
1595 kind
= gnu_v3_base_object_ctor
;
1598 kind
= gnu_v3_complete_object_allocating_ctor
;
1603 return d_make_ctor (di
, kind
, di
->last_name
);
1608 enum gnu_v3_dtor_kinds kind
;
1610 switch (d_next_char (di
))
1613 kind
= gnu_v3_deleting_dtor
;
1616 kind
= gnu_v3_complete_object_dtor
;
1619 kind
= gnu_v3_base_object_dtor
;
1624 return d_make_dtor (di
, kind
, di
->last_name
);
1632 /* <type> ::= <builtin-type>
1634 ::= <class-enum-type>
1636 ::= <pointer-to-member-type>
1637 ::= <template-param>
1638 ::= <template-template-param> <template-args>
1640 ::= <CV-qualifiers> <type>
1645 ::= U <source-name> <type>
1647 <builtin-type> ::= various one letter codes
1651 static const struct d_builtin_type_info d_builtin_types
[26] =
1653 /* a */ { "signed char", "signed char", D_PRINT_INT
},
1654 /* b */ { "bool", "boolean", D_PRINT_BOOL
},
1655 /* c */ { "char", "byte", D_PRINT_INT
},
1656 /* d */ { "double", "double", D_PRINT_DEFAULT
},
1657 /* e */ { "long double", "long double", D_PRINT_DEFAULT
},
1658 /* f */ { "float", "float", D_PRINT_DEFAULT
},
1659 /* g */ { "__float128", "__float128", D_PRINT_DEFAULT
},
1660 /* h */ { "unsigned char", "unsigned char", D_PRINT_INT
},
1661 /* i */ { "int", "int", D_PRINT_INT
},
1662 /* j */ { "unsigned int", "unsigned", D_PRINT_INT
},
1663 /* k */ { NULL
, NULL
, D_PRINT_DEFAULT
},
1664 /* l */ { "long", "long", D_PRINT_LONG
},
1665 /* m */ { "unsigned long", "unsigned long", D_PRINT_LONG
},
1666 /* n */ { "__int128", "__int128", D_PRINT_DEFAULT
},
1667 /* o */ { "unsigned __int128", "unsigned __int128", D_PRINT_DEFAULT
},
1668 /* p */ { NULL
, NULL
, D_PRINT_DEFAULT
},
1669 /* q */ { NULL
, NULL
, D_PRINT_DEFAULT
},
1670 /* r */ { NULL
, NULL
, D_PRINT_DEFAULT
},
1671 /* s */ { "short", "short", D_PRINT_INT
},
1672 /* t */ { "unsigned short", "unsigned short", D_PRINT_INT
},
1673 /* u */ { NULL
, NULL
, D_PRINT_DEFAULT
},
1674 /* v */ { "void", "void", D_PRINT_VOID
},
1675 /* w */ { "wchar_t", "char", D_PRINT_INT
},
1676 /* x */ { "long long", "long", D_PRINT_DEFAULT
},
1677 /* y */ { "unsigned long long", "unsigned long long", D_PRINT_DEFAULT
},
1678 /* z */ { "...", "...", D_PRINT_DEFAULT
},
1681 static struct d_comp
*
1689 /* The ABI specifies that when CV-qualifiers are used, the base type
1690 is substitutable, and the fully qualified type is substitutable,
1691 but the base type with a strict subset of the CV-qualifiers is
1692 not substitutable. The natural recursive implementation of the
1693 CV-qualifiers would cause subsets to be substitutable, so instead
1694 we pull them all off now.
1696 FIXME: The ABI says that order-insensitive vendor qualifiers
1697 should be handled in the same way, but we have no way to tell
1698 which vendor qualifiers are order-insensitive and which are
1699 order-sensitive. So we just assume that they are all
1700 order-sensitive. g++ 3.4 supports only one vendor qualifier,
1701 __vector, and it treats it as order-sensitive when mangling
1704 peek
= d_peek_char (di
);
1705 if (peek
== 'r' || peek
== 'V' || peek
== 'K')
1707 struct d_comp
**pret
;
1709 pret
= d_cv_qualifiers (di
, &ret
, 0);
1712 *pret
= d_type (di
);
1713 if (! d_add_substitution (di
, ret
))
1722 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1723 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
1724 case 'o': case 's': case 't':
1725 case 'v': case 'w': case 'x': case 'y': case 'z':
1726 ret
= d_make_builtin_type (di
, &d_builtin_types
[peek
- 'a']);
1733 ret
= d_make_comp (di
, D_COMP_VENDOR_TYPE
, d_source_name (di
), NULL
);
1737 ret
= d_function_type (di
);
1740 case '0': case '1': case '2': case '3': case '4':
1741 case '5': case '6': case '7': case '8': case '9':
1744 ret
= d_class_enum_type (di
);
1748 ret
= d_array_type (di
);
1752 ret
= d_pointer_to_member_type (di
);
1756 ret
= d_template_param (di
);
1757 if (d_peek_char (di
) == 'I')
1759 /* This is <template-template-param> <template-args>. The
1760 <template-template-param> part is a substitution
1762 if (! d_add_substitution (di
, ret
))
1764 ret
= d_make_comp (di
, D_COMP_TEMPLATE
, ret
, d_template_args (di
));
1769 /* If this is a special substitution, then it is the start of
1770 <class-enum-type>. */
1774 peek_next
= d_peek_next_char (di
);
1775 if (IS_DIGIT (peek_next
)
1777 || IS_UPPER (peek_next
))
1779 ret
= d_substitution (di
);
1780 /* The substituted name may have been a template name and
1781 may be followed by tepmlate args. */
1782 if (d_peek_char (di
) == 'I')
1783 ret
= d_make_comp (di
, D_COMP_TEMPLATE
, ret
,
1784 d_template_args (di
));
1790 ret
= d_class_enum_type (di
);
1791 /* If the substitution was a complete type, then it is not
1792 a new substitution candidate. However, if the
1793 substitution was followed by template arguments, then
1794 the whole thing is a substitution candidate. */
1795 if (ret
!= NULL
&& ret
->type
== D_COMP_SUB_STD
)
1803 ret
= d_make_comp (di
, D_COMP_POINTER
, d_type (di
), NULL
);
1808 ret
= d_make_comp (di
, D_COMP_REFERENCE
, d_type (di
), NULL
);
1813 ret
= d_make_comp (di
, D_COMP_COMPLEX
, d_type (di
), NULL
);
1818 ret
= d_make_comp (di
, D_COMP_IMAGINARY
, d_type (di
), NULL
);
1823 ret
= d_source_name (di
);
1824 ret
= d_make_comp (di
, D_COMP_VENDOR_TYPE_QUAL
, d_type (di
), ret
);
1833 if (! d_add_substitution (di
, ret
))
1840 /* <CV-qualifiers> ::= [r] [V] [K] */
1842 static struct d_comp
**
1843 d_cv_qualifiers (di
, pret
, member_fn
)
1845 struct d_comp
**pret
;
1850 peek
= d_peek_char (di
);
1851 while (peek
== 'r' || peek
== 'V' || peek
== 'K')
1857 t
= member_fn
? D_COMP_RESTRICT_THIS
: D_COMP_RESTRICT
;
1858 else if (peek
== 'V')
1859 t
= member_fn
? D_COMP_VOLATILE_THIS
: D_COMP_VOLATILE
;
1861 t
= member_fn
? D_COMP_CONST_THIS
: D_COMP_CONST
;
1863 *pret
= d_make_comp (di
, t
, NULL
, NULL
);
1866 pret
= &d_left (*pret
);
1868 peek
= d_peek_char (di
);
1874 /* <function-type> ::= F [Y] <bare-function-type> E */
1876 static struct d_comp
*
1877 d_function_type (di
)
1882 if (d_next_char (di
) != 'F')
1884 if (d_peek_char (di
) == 'Y')
1886 /* Function has C linkage. We don't print this information.
1887 FIXME: We should print it in verbose mode. */
1890 ret
= d_bare_function_type (di
, 1);
1891 if (d_next_char (di
) != 'E')
1896 /* <bare-function-type> ::= <type>+ */
1898 static struct d_comp
*
1899 d_bare_function_type (di
, has_return_type
)
1901 int has_return_type
;
1903 struct d_comp
*return_type
;
1905 struct d_comp
**ptl
;
1913 struct d_comp
*type
;
1915 peek
= d_peek_char (di
);
1916 if (peek
== '\0' || peek
== 'E')
1921 if (has_return_type
)
1924 has_return_type
= 0;
1928 *ptl
= d_make_comp (di
, D_COMP_ARGLIST
, type
, NULL
);
1931 ptl
= &d_right (*ptl
);
1935 /* There should be at least one parameter type besides the optional
1936 return type. A function which takes no arguments will have a
1937 single parameter type void. */
1941 /* If we have a single parameter type void, omit it. */
1942 if (d_right (tl
) == NULL
1943 && d_left (tl
)->type
== D_COMP_BUILTIN_TYPE
1944 && d_left (tl
)->u
.s_builtin
.type
->print
== D_PRINT_VOID
)
1947 return d_make_comp (di
, D_COMP_FUNCTION_TYPE
, return_type
, tl
);
1950 /* <class-enum-type> ::= <name> */
1952 static struct d_comp
*
1953 d_class_enum_type (di
)
1959 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
1960 ::= A [<(dimension) expression>] _ <(element) type>
1963 static struct d_comp
*
1970 if (d_next_char (di
) != 'A')
1973 peek
= d_peek_char (di
);
1976 else if (IS_DIGIT (peek
))
1984 peek
= d_peek_char (di
);
1986 while (IS_DIGIT (peek
));
1987 dim
= d_make_name (di
, s
, d_str (di
) - s
);
1993 dim
= d_expression (di
);
1998 if (d_next_char (di
) != '_')
2001 return d_make_comp (di
, D_COMP_ARRAY_TYPE
, dim
, d_type (di
));
2004 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2006 static struct d_comp
*
2007 d_pointer_to_member_type (di
)
2012 struct d_comp
**pmem
;
2014 if (d_next_char (di
) != 'M')
2019 /* The ABI specifies that any type can be a substitution source, and
2020 that M is followed by two types, and that when a CV-qualified
2021 type is seen both the base type and the CV-qualified types are
2022 substitution sources. The ABI also specifies that for a pointer
2023 to a CV-qualified member function, the qualifiers are attached to
2024 the second type. Given the grammar, a plain reading of the ABI
2025 suggests that both the CV-qualified member function and the
2026 non-qualified member function are substitution sources. However,
2027 g++ does not work that way. g++ treats only the CV-qualified
2028 member function as a substitution source. FIXME. So to work
2029 with g++, we need to pull off the CV-qualifiers here, in order to
2030 avoid calling add_substitution() in d_type(). */
2032 pmem
= d_cv_qualifiers (di
, &mem
, 1);
2035 *pmem
= d_type (di
);
2037 return d_make_comp (di
, D_COMP_PTRMEM_TYPE
, cl
, mem
);
2040 /* <template-param> ::= T_
2041 ::= T <(parameter-2 non-negative) number> _
2044 static struct d_comp
*
2045 d_template_param (di
)
2050 if (d_next_char (di
) != 'T')
2053 if (d_peek_char (di
) == '_')
2057 param
= d_number (di
);
2063 if (d_next_char (di
) != '_')
2066 return d_make_template_param (di
, param
);
2069 /* <template-args> ::= I <template-arg>+ E */
2071 static struct d_comp
*
2072 d_template_args (di
)
2075 struct d_comp
*hold_last_name
;
2077 struct d_comp
**pal
;
2079 /* Preserve the last name we saw--don't let the template arguments
2080 clobber it, as that would give us the wrong name for a subsequent
2081 constructor or destructor. */
2082 hold_last_name
= di
->last_name
;
2084 if (d_next_char (di
) != 'I')
2093 a
= d_template_arg (di
);
2097 *pal
= d_make_comp (di
, D_COMP_TEMPLATE_ARGLIST
, a
, NULL
);
2100 pal
= &d_right (*pal
);
2102 if (d_peek_char (di
) == 'E')
2109 di
->last_name
= hold_last_name
;
2114 /* <template-arg> ::= <type>
2115 ::= X <expression> E
2119 static struct d_comp
*
2125 switch (d_peek_char (di
))
2129 ret
= d_expression (di
);
2130 if (d_next_char (di
) != 'E')
2135 return d_expr_primary (di
);
2142 /* <expression> ::= <(unary) operator-name> <expression>
2143 ::= <(binary) operator-name> <expression> <expression>
2144 ::= <(trinary) operator-name> <expression> <expression> <expression>
2146 ::= <template-param>
2147 ::= sr <type> <unqualified-name>
2148 ::= sr <type> <unqualified-name> <template-args>
2152 static struct d_comp
*
2158 peek
= d_peek_char (di
);
2160 return d_expr_primary (di
);
2161 else if (peek
== 'T')
2162 return d_template_param (di
);
2163 else if (peek
== 's' && d_peek_next_char (di
) == 'r')
2165 struct d_comp
*type
;
2166 struct d_comp
*name
;
2170 name
= d_unqualified_name (di
);
2171 if (d_peek_char (di
) != 'I')
2172 return d_make_comp (di
, D_COMP_QUAL_NAME
, type
, name
);
2174 return d_make_comp (di
, D_COMP_QUAL_NAME
, type
,
2175 d_make_comp (di
, D_COMP_TEMPLATE
, name
,
2176 d_template_args (di
)));
2183 op
= d_operator_name (di
);
2187 if (op
->type
== D_COMP_OPERATOR
2188 && strcmp (op
->u
.s_operator
.op
->code
, "st") == 0)
2189 return d_make_comp (di
, D_COMP_UNARY
, op
, d_type (di
));
2195 case D_COMP_OPERATOR
:
2196 args
= op
->u
.s_operator
.op
->args
;
2198 case D_COMP_EXTENDED_OPERATOR
:
2199 args
= op
->u
.s_extended_operator
.args
;
2209 return d_make_comp (di
, D_COMP_UNARY
, op
, d_expression (di
));
2212 struct d_comp
*left
;
2214 left
= d_expression (di
);
2215 return d_make_comp (di
, D_COMP_BINARY
, op
,
2216 d_make_comp (di
, D_COMP_BINARY_ARGS
, left
,
2217 d_expression (di
)));
2221 struct d_comp
*first
;
2222 struct d_comp
*second
;
2224 first
= d_expression (di
);
2225 second
= d_expression (di
);
2226 return d_make_comp (di
, D_COMP_TRINARY
, op
,
2227 d_make_comp (di
, D_COMP_TRINARY_ARG1
, first
,
2229 D_COMP_TRINARY_ARG2
,
2231 d_expression (di
))));
2239 /* <expr-primary> ::= L <type> <(value) number> E
2240 ::= L <type> <(value) float> E
2241 ::= L <mangled-name> E
2244 static struct d_comp
*
2250 if (d_next_char (di
) != 'L')
2252 if (d_peek_char (di
) == '_')
2253 ret
= d_mangled_name (di
, 0);
2256 struct d_comp
*type
;
2261 /* Rather than try to interpret the literal value, we just
2262 collect it as a string. Note that it's possible to have a
2263 floating point literal here. The ABI specifies that the
2264 format of such literals is machine independent. That's fine,
2265 but what's not fine is that versions of g++ up to 3.2 with
2266 -fabi-version=1 used upper case letters in the hex constant,
2267 and dumped out gcc's internal representation. That makes it
2268 hard to tell where the constant ends, and hard to dump the
2269 constant in any readable form anyhow. We don't attempt to
2270 handle these cases. */
2273 while (d_peek_char (di
) != 'E')
2275 ret
= d_make_comp (di
, D_COMP_LITERAL
, type
,
2276 d_make_name (di
, s
, d_str (di
) - s
));
2278 if (d_next_char (di
) != 'E')
2283 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2284 ::= Z <(function) encoding> E s [<discriminator>]
2287 static struct d_comp
*
2291 struct d_comp
*function
;
2293 if (d_next_char (di
) != 'Z')
2296 function
= d_encoding (di
, 0);
2298 if (d_next_char (di
) != 'E')
2301 if (d_peek_char (di
) == 's')
2304 if (! d_discriminator (di
))
2306 return d_make_comp (di
, D_COMP_QUAL_NAME
, function
,
2307 d_make_name (di
, "string literal",
2308 sizeof "string literal" - 1));
2312 struct d_comp
*name
;
2315 if (! d_discriminator (di
))
2317 return d_make_comp (di
, D_COMP_QUAL_NAME
, function
, name
);
2321 /* <discriminator> ::= _ <(non-negative) number>
2323 We demangle the discriminator, but we don't print it out. FIXME:
2324 We should print it out in verbose mode. */
2327 d_discriminator (di
)
2332 if (d_peek_char (di
) != '_')
2335 discrim
= d_number (di
);
2341 /* Add a new substitution. */
2344 d_add_substitution (di
, dc
)
2350 if (di
->next_sub
>= di
->num_subs
)
2352 di
->subs
[di
->next_sub
] = dc
;
2357 /* <substitution> ::= S <seq-id> _
2368 static struct d_comp
*
2374 if (d_next_char (di
) != 'S')
2377 c
= d_next_char (di
);
2378 if (c
== '_' || IS_DIGIT (c
) || IS_UPPER (c
))
2388 id
= id
* 36 + c
- '0';
2389 else if (IS_UPPER (c
))
2390 id
= id
* 36 + c
- 'A' + 10;
2393 c
= d_next_char (di
);
2400 if (id
>= di
->next_sub
)
2403 return di
->subs
[id
];
2410 return d_make_sub (di
, "std");
2412 di
->last_name
= d_make_sub (di
, "allocator");
2413 return d_make_sub (di
, "std::allocator");
2415 di
->last_name
= d_make_sub (di
, "basic_string");
2416 return d_make_sub (di
, "std::basic_string");
2418 di
->last_name
= d_make_sub (di
, "string");
2419 return d_make_sub (di
, "std::string");
2421 di
->last_name
= d_make_sub (di
, "istream");
2422 return d_make_sub (di
, "std::istream");
2424 di
->last_name
= d_make_sub (di
, "ostream");
2425 return d_make_sub (di
, "std::ostream");
2427 di
->last_name
= d_make_sub (di
, "iostream");
2428 return d_make_sub (di
, "std::iostream");
2435 /* Resize the print buffer. */
2438 d_print_resize (dpi
, add
)
2439 struct d_print_info
*dpi
;
2444 if (dpi
->buf
== NULL
)
2446 need
= dpi
->len
+ add
;
2447 while (need
> dpi
->alc
)
2452 newalc
= dpi
->alc
* 2;
2453 newbuf
= realloc (dpi
->buf
, newalc
);
2458 dpi
->allocation_failure
= 1;
2466 /* Append a character to the print buffer. */
2469 d_print_append_char (dpi
, c
)
2470 struct d_print_info
*dpi
;
2473 if (dpi
->buf
!= NULL
)
2475 if (dpi
->len
>= dpi
->alc
)
2477 d_print_resize (dpi
, 1);
2478 if (dpi
->buf
== NULL
)
2482 dpi
->buf
[dpi
->len
] = c
;
2487 /* Append a buffer to the print buffer. */
2490 d_print_append_buffer (dpi
, s
, l
)
2491 struct d_print_info
*dpi
;
2495 if (dpi
->buf
!= NULL
)
2497 if (dpi
->len
+ l
> dpi
->alc
)
2499 d_print_resize (dpi
, l
);
2500 if (dpi
->buf
== NULL
)
2504 memcpy (dpi
->buf
+ dpi
->len
, s
, l
);
2509 /* Indicate that an error occurred during printing. */
2513 struct d_print_info
*dpi
;
2519 /* Turn components into a human readable string. Returns a string
2520 allocated by malloc, or NULL on error. On success, this sets *PALC
2521 to the size of the allocated buffer. On failure, this sets *PALC
2522 to 0 for a bad parse, or to 1 for a memory allocation failure. */
2525 d_print (options
, dc
, palc
)
2527 const struct d_comp
*dc
;
2530 struct d_print_info dpi
;
2532 dpi
.options
= options
;
2535 dpi
.buf
= malloc (dpi
.alc
);
2536 if (dpi
.buf
== NULL
)
2543 dpi
.templates
= NULL
;
2544 dpi
.modifiers
= NULL
;
2546 dpi
.allocation_failure
= 0;
2548 d_print_comp (&dpi
, dc
);
2550 d_append_char (&dpi
, '\0');
2552 if (dpi
.buf
!= NULL
)
2555 *palc
= dpi
.allocation_failure
;
2560 /* Subroutine to handle components. */
2563 d_print_comp (dpi
, dc
)
2564 struct d_print_info
*dpi
;
2565 const struct d_comp
*dc
;
2569 d_print_error (dpi
);
2572 if (d_print_saw_error (dpi
))
2578 d_print_identifier (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
2581 case D_COMP_QUAL_NAME
:
2582 d_print_comp (dpi
, d_left (dc
));
2583 d_append_string (dpi
, (dpi
->options
& DMGL_JAVA
) == 0 ? "::" : ".");
2584 d_print_comp (dpi
, d_right (dc
));
2587 case D_COMP_TYPED_NAME
:
2589 struct d_print_mod
*hold_modifiers
;
2590 struct d_comp
*typed_name
;
2591 struct d_print_mod adpm
[4];
2593 struct d_print_template dpt
;
2595 /* Pass the name down to the type so that it can be printed in
2596 the right place for the type. We also have to pass down
2597 any CV-qualifiers, which apply to the this parameter. */
2598 hold_modifiers
= dpi
->modifiers
;
2600 typed_name
= d_left (dc
);
2601 while (typed_name
!= NULL
)
2603 if (i
>= sizeof adpm
/ sizeof adpm
[0])
2605 d_print_error (dpi
);
2609 adpm
[i
].next
= dpi
->modifiers
;
2610 dpi
->modifiers
= &adpm
[i
];
2611 adpm
[i
].mod
= typed_name
;
2612 adpm
[i
].printed
= 0;
2613 adpm
[i
].templates
= dpi
->templates
;
2616 if (typed_name
->type
!= D_COMP_RESTRICT_THIS
2617 && typed_name
->type
!= D_COMP_VOLATILE_THIS
2618 && typed_name
->type
!= D_COMP_CONST_THIS
)
2621 typed_name
= d_left (typed_name
);
2624 /* If typed_name is a template, then it applies to the
2625 function type as well. */
2626 if (typed_name
->type
== D_COMP_TEMPLATE
)
2628 dpt
.next
= dpi
->templates
;
2629 dpi
->templates
= &dpt
;
2630 dpt
.template = typed_name
;
2633 d_print_comp (dpi
, d_right (dc
));
2635 if (typed_name
->type
== D_COMP_TEMPLATE
)
2636 dpi
->templates
= dpt
.next
;
2638 /* If the modifiers didn't get printed by the type, print them
2643 if (! adpm
[i
].printed
)
2645 d_append_char (dpi
, ' ');
2646 d_print_mod (dpi
, adpm
[i
].mod
);
2650 dpi
->modifiers
= hold_modifiers
;
2655 case D_COMP_TEMPLATE
:
2657 struct d_print_mod
*hold_dpm
;
2659 /* Don't push modifiers into a template definition. Doing so
2660 could give the wrong definition for a template argument.
2661 Instead, treat the template essentially as a name. */
2663 hold_dpm
= dpi
->modifiers
;
2664 dpi
->modifiers
= NULL
;
2666 d_print_comp (dpi
, d_left (dc
));
2667 if (d_last_char (dpi
) == '<')
2668 d_append_char (dpi
, ' ');
2669 d_append_char (dpi
, '<');
2670 d_print_comp (dpi
, d_right (dc
));
2671 /* Avoid generating two consecutive '>' characters, to avoid
2672 the C++ syntactic ambiguity. */
2673 if (d_last_char (dpi
) == '>')
2674 d_append_char (dpi
, ' ');
2675 d_append_char (dpi
, '>');
2677 dpi
->modifiers
= hold_dpm
;
2682 case D_COMP_TEMPLATE_PARAM
:
2686 struct d_print_template
*hold_dpt
;
2688 if (dpi
->templates
== NULL
)
2690 d_print_error (dpi
);
2693 i
= dc
->u
.s_number
.number
;
2694 for (a
= d_right (dpi
->templates
->template);
2698 if (a
->type
!= D_COMP_TEMPLATE_ARGLIST
)
2700 d_print_error (dpi
);
2707 if (i
!= 0 || a
== NULL
)
2709 d_print_error (dpi
);
2713 /* While processing this parameter, we need to pop the list of
2714 templates. This is because the template parameter may
2715 itself be a reference to a parameter of an outer
2718 hold_dpt
= dpi
->templates
;
2719 dpi
->templates
= hold_dpt
->next
;
2721 d_print_comp (dpi
, d_left (a
));
2723 dpi
->templates
= hold_dpt
;
2729 d_print_comp (dpi
, dc
->u
.s_ctor
.name
);
2733 d_append_char (dpi
, '~');
2734 d_print_comp (dpi
, dc
->u
.s_dtor
.name
);
2738 d_append_string (dpi
, "vtable for ");
2739 d_print_comp (dpi
, d_left (dc
));
2743 d_append_string (dpi
, "VTT for ");
2744 d_print_comp (dpi
, d_left (dc
));
2747 case D_COMP_CONSTRUCTION_VTABLE
:
2748 d_append_string (dpi
, "construction vtable for ");
2749 d_print_comp (dpi
, d_left (dc
));
2750 d_append_string (dpi
, "-in-");
2751 d_print_comp (dpi
, d_right (dc
));
2754 case D_COMP_TYPEINFO
:
2755 d_append_string (dpi
, "typeinfo for ");
2756 d_print_comp (dpi
, d_left (dc
));
2759 case D_COMP_TYPEINFO_NAME
:
2760 d_append_string (dpi
, "typeinfo name for ");
2761 d_print_comp (dpi
, d_left (dc
));
2764 case D_COMP_TYPEINFO_FN
:
2765 d_append_string (dpi
, "typeinfo fn for ");
2766 d_print_comp (dpi
, d_left (dc
));
2770 d_append_string (dpi
, "non-virtual thunk to ");
2771 d_print_comp (dpi
, d_left (dc
));
2774 case D_COMP_VIRTUAL_THUNK
:
2775 d_append_string (dpi
, "virtual thunk to ");
2776 d_print_comp (dpi
, d_left (dc
));
2779 case D_COMP_COVARIANT_THUNK
:
2780 d_append_string (dpi
, "covariant return thunk to ");
2781 d_print_comp (dpi
, d_left (dc
));
2784 case D_COMP_JAVA_CLASS
:
2785 d_append_string (dpi
, "java Class for ");
2786 d_print_comp (dpi
, d_left (dc
));
2790 d_append_string (dpi
, "guard variable for ");
2791 d_print_comp (dpi
, d_left (dc
));
2794 case D_COMP_REFTEMP
:
2795 d_append_string (dpi
, "reference temporary for ");
2796 d_print_comp (dpi
, d_left (dc
));
2799 case D_COMP_SUB_STD
:
2800 d_append_string (dpi
, dc
->u
.s_string
.string
);
2803 case D_COMP_RESTRICT
:
2804 case D_COMP_VOLATILE
:
2806 case D_COMP_RESTRICT_THIS
:
2807 case D_COMP_VOLATILE_THIS
:
2808 case D_COMP_CONST_THIS
:
2809 case D_COMP_VENDOR_TYPE_QUAL
:
2810 case D_COMP_POINTER
:
2811 case D_COMP_REFERENCE
:
2812 case D_COMP_COMPLEX
:
2813 case D_COMP_IMAGINARY
:
2815 /* We keep a list of modifiers on the stack. */
2816 struct d_print_mod dpm
;
2818 dpm
.next
= dpi
->modifiers
;
2819 dpi
->modifiers
= &dpm
;
2822 dpm
.templates
= dpi
->templates
;
2824 d_print_comp (dpi
, d_left (dc
));
2826 /* If the modifier didn't get printed by the type, print it
2829 d_print_mod (dpi
, dc
);
2831 dpi
->modifiers
= dpm
.next
;
2836 case D_COMP_BUILTIN_TYPE
:
2837 if ((dpi
->options
& DMGL_JAVA
) == 0)
2838 d_append_string (dpi
, dc
->u
.s_builtin
.type
->name
);
2840 d_append_string (dpi
, dc
->u
.s_builtin
.type
->java_name
);
2843 case D_COMP_VENDOR_TYPE
:
2844 d_print_comp (dpi
, d_left (dc
));
2847 case D_COMP_FUNCTION_TYPE
:
2849 if (d_left (dc
) != NULL
)
2851 struct d_print_mod dpm
;
2853 /* We must pass this type down as a modifier in order to
2854 print it in the right location. */
2856 dpm
.next
= dpi
->modifiers
;
2857 dpi
->modifiers
= &dpm
;
2860 dpm
.templates
= dpi
->templates
;
2862 d_print_comp (dpi
, d_left (dc
));
2864 dpi
->modifiers
= dpm
.next
;
2869 d_append_char (dpi
, ' ');
2872 d_print_function_type (dpi
, dc
, dpi
->modifiers
);
2877 case D_COMP_ARRAY_TYPE
:
2879 struct d_print_mod dpm
;
2881 /* We must pass this type down as a modifier in order to print
2882 multi-dimensional arrays correctly. */
2884 dpm
.next
= dpi
->modifiers
;
2885 dpi
->modifiers
= &dpm
;
2888 dpm
.templates
= dpi
->templates
;
2890 d_print_comp (dpi
, d_right (dc
));
2892 dpi
->modifiers
= dpm
.next
;
2897 d_print_array_type (dpi
, dc
, dpi
->modifiers
);
2902 case D_COMP_PTRMEM_TYPE
:
2904 struct d_print_mod dpm
;
2906 dpm
.next
= dpi
->modifiers
;
2907 dpi
->modifiers
= &dpm
;
2910 dpm
.templates
= dpi
->templates
;
2912 d_print_comp (dpi
, d_right (dc
));
2914 /* If the modifier didn't get printed by the type, print it
2918 d_append_char (dpi
, ' ');
2919 d_print_comp (dpi
, d_left (dc
));
2920 d_append_string (dpi
, "::*");
2923 dpi
->modifiers
= dpm
.next
;
2928 case D_COMP_ARGLIST
:
2929 case D_COMP_TEMPLATE_ARGLIST
:
2930 d_print_comp (dpi
, d_left (dc
));
2931 if (d_right (dc
) != NULL
)
2933 d_append_string (dpi
, ", ");
2934 d_print_comp (dpi
, d_right (dc
));
2938 case D_COMP_OPERATOR
:
2942 d_append_string (dpi
, "operator");
2943 c
= dc
->u
.s_operator
.op
->name
[0];
2945 d_append_char (dpi
, ' ');
2946 d_append_string (dpi
, dc
->u
.s_operator
.op
->name
);
2950 case D_COMP_EXTENDED_OPERATOR
:
2951 d_append_string (dpi
, "operator ");
2952 d_print_comp (dpi
, dc
->u
.s_extended_operator
.name
);
2956 d_append_string (dpi
, "operator ");
2957 d_print_cast (dpi
, dc
);
2961 if (d_left (dc
)->type
!= D_COMP_CAST
)
2962 d_print_expr_op (dpi
, d_left (dc
));
2965 d_append_string (dpi
, "((");
2966 d_print_cast (dpi
, d_left (dc
));
2967 d_append_char (dpi
, ')');
2969 d_append_char (dpi
, '(');
2970 d_print_comp (dpi
, d_right (dc
));
2971 d_append_char (dpi
, ')');
2972 if (d_left (dc
)->type
== D_COMP_CAST
)
2973 d_append_char (dpi
, ')');
2977 if (d_right (dc
)->type
!= D_COMP_BINARY_ARGS
)
2979 d_print_error (dpi
);
2983 /* We wrap an expression which uses the greater-than operator in
2984 an extra layer of parens so that it does not get confused
2985 with the '>' which ends the template parameters. */
2986 if (d_left (dc
)->type
== D_COMP_OPERATOR
2987 && strcmp (d_left (dc
)->u
.s_operator
.op
->name
, ">") == 0)
2988 d_append_char (dpi
, '(');
2990 d_append_char (dpi
, '(');
2991 d_print_comp (dpi
, d_left (d_right (dc
)));
2992 d_append_string (dpi
, ") ");
2993 d_print_expr_op (dpi
, d_left (dc
));
2994 d_append_string (dpi
, " (");
2995 d_print_comp (dpi
, d_right (d_right (dc
)));
2996 d_append_char (dpi
, ')');
2998 if (d_left (dc
)->type
== D_COMP_OPERATOR
2999 && strcmp (d_left (dc
)->u
.s_operator
.op
->name
, ">") == 0)
3000 d_append_char (dpi
, ')');
3004 case D_COMP_BINARY_ARGS
:
3005 /* We should only see this as part of D_COMP_BINARY. */
3006 d_print_error (dpi
);
3009 case D_COMP_TRINARY
:
3010 if (d_right (dc
)->type
!= D_COMP_TRINARY_ARG1
3011 || d_right (d_right (dc
))->type
!= D_COMP_TRINARY_ARG2
)
3013 d_print_error (dpi
);
3016 d_append_char (dpi
, '(');
3017 d_print_comp (dpi
, d_left (d_right (dc
)));
3018 d_append_string (dpi
, ") ");
3019 d_print_expr_op (dpi
, d_left (dc
));
3020 d_append_string (dpi
, " (");
3021 d_print_comp (dpi
, d_left (d_right (d_right (dc
))));
3022 d_append_string (dpi
, ") : (");
3023 d_print_comp (dpi
, d_right (d_right (d_right (dc
))));
3024 d_append_char (dpi
, ')');
3027 case D_COMP_TRINARY_ARG1
:
3028 case D_COMP_TRINARY_ARG2
:
3029 /* We should only see these are part of D_COMP_TRINARY. */
3030 d_print_error (dpi
);
3033 case D_COMP_LITERAL
:
3034 /* For some builtin types, produce simpler output. */
3035 if (d_left (dc
)->type
== D_COMP_BUILTIN_TYPE
)
3037 switch (d_left (dc
)->u
.s_builtin
.type
->print
)
3040 if (d_right (dc
)->type
== D_COMP_NAME
)
3042 d_print_comp (dpi
, d_right (dc
));
3048 if (d_right (dc
)->type
== D_COMP_NAME
)
3050 d_print_comp (dpi
, d_right (dc
));
3051 d_append_char (dpi
, 'l');
3057 if (d_right (dc
)->type
== D_COMP_NAME
3058 && d_right (dc
)->u
.s_name
.len
== 1)
3060 switch (d_right (dc
)->u
.s_name
.s
[0])
3063 d_append_string (dpi
, "false");
3066 d_append_string (dpi
, "true");
3079 d_append_char (dpi
, '(');
3080 d_print_comp (dpi
, d_left (dc
));
3081 d_append_char (dpi
, ')');
3082 d_print_comp (dpi
, d_right (dc
));
3086 d_print_error (dpi
);
3091 /* Print an identifier. */
3094 d_print_identifier (dpi
, name
, len
)
3095 struct d_print_info
*dpi
;
3099 if ((dpi
->options
& DMGL_JAVA
) == 0)
3100 d_append_buffer (dpi
, name
, len
);
3106 /* For Java we try to handle encoded extended Unicode
3107 characters. The C++ ABI doesn't mention Unicode encoding, so
3108 we don't it for C++. Characters are encoded as
3111 for (p
= name
; p
< end
; ++p
)
3122 for (q
= p
+ 3; q
< end
; ++q
)
3128 else if (*q
>= 'A' && *q
<= 'F')
3129 dig
= *q
- 'A' + 10;
3130 else if (*q
>= 'a' && *q
<= 'f')
3131 dig
= *q
- 'a' + 10;
3137 /* If the Unicode character is larger than 256, we don't
3138 try to deal with it here. FIXME. */
3139 if (q
< end
&& *q
== '_' && c
< 256)
3141 d_append_char (dpi
, c
);
3147 d_append_char (dpi
, *p
);
3152 /* Print a list of modifiers. SUFFIX is 1 if we are printing
3153 qualifiers on this after printing a function. */
3156 d_print_mod_list (dpi
, mods
, suffix
)
3157 struct d_print_info
*dpi
;
3158 struct d_print_mod
*mods
;
3161 struct d_print_template
*hold_dpt
;
3163 if (mods
== NULL
|| d_print_saw_error (dpi
))
3168 && (mods
->mod
->type
== D_COMP_RESTRICT_THIS
3169 || mods
->mod
->type
== D_COMP_VOLATILE_THIS
3170 || mods
->mod
->type
== D_COMP_CONST_THIS
)))
3172 d_print_mod_list (dpi
, mods
->next
, suffix
);
3178 hold_dpt
= dpi
->templates
;
3179 dpi
->templates
= mods
->templates
;
3181 if (mods
->mod
->type
== D_COMP_FUNCTION_TYPE
)
3183 d_print_function_type (dpi
, mods
->mod
, mods
->next
);
3184 dpi
->templates
= hold_dpt
;
3187 else if (mods
->mod
->type
== D_COMP_ARRAY_TYPE
)
3189 d_print_array_type (dpi
, mods
->mod
, mods
->next
);
3190 dpi
->templates
= hold_dpt
;
3194 d_print_mod (dpi
, mods
->mod
);
3196 dpi
->templates
= hold_dpt
;
3198 d_print_mod_list (dpi
, mods
->next
, suffix
);
3201 /* Print a modifier. */
3204 d_print_mod (dpi
, mod
)
3205 struct d_print_info
*dpi
;
3206 const struct d_comp
*mod
;
3210 case D_COMP_RESTRICT
:
3211 case D_COMP_RESTRICT_THIS
:
3212 d_append_string (dpi
, " restrict");
3214 case D_COMP_VOLATILE
:
3215 case D_COMP_VOLATILE_THIS
:
3216 d_append_string (dpi
, " volatile");
3219 case D_COMP_CONST_THIS
:
3220 d_append_string (dpi
, " const");
3222 case D_COMP_VENDOR_TYPE_QUAL
:
3223 d_append_char (dpi
, ' ');
3224 d_print_comp (dpi
, d_right (mod
));
3226 case D_COMP_POINTER
:
3227 /* There is no pointer symbol in Java. */
3228 if ((dpi
->options
& DMGL_JAVA
) == 0)
3229 d_append_char (dpi
, '*');
3231 case D_COMP_REFERENCE
:
3232 d_append_char (dpi
, '&');
3234 case D_COMP_COMPLEX
:
3235 d_append_string (dpi
, "complex ");
3237 case D_COMP_IMAGINARY
:
3238 d_append_string (dpi
, "imaginary ");
3240 case D_COMP_PTRMEM_TYPE
:
3241 if (d_last_char (dpi
) != '(')
3242 d_append_char (dpi
, ' ');
3243 d_print_comp (dpi
, d_left (mod
));
3244 d_append_string (dpi
, "::*");
3246 case D_COMP_TYPED_NAME
:
3247 d_print_comp (dpi
, d_left (mod
));
3250 /* Otherwise, we have something that won't go back on the
3251 modifier stack, so we can just print it. */
3252 d_print_comp (dpi
, mod
);
3257 /* Print a function type, except for the return type. */
3260 d_print_function_type (dpi
, dc
, mods
)
3261 struct d_print_info
*dpi
;
3262 const struct d_comp
*dc
;
3263 struct d_print_mod
*mods
;
3267 struct d_print_mod
*p
;
3271 for (p
= mods
; p
!= NULL
; p
= p
->next
)
3277 switch (p
->mod
->type
)
3279 case D_COMP_RESTRICT
:
3280 case D_COMP_VOLATILE
:
3282 case D_COMP_VENDOR_TYPE_QUAL
:
3283 case D_COMP_POINTER
:
3284 case D_COMP_REFERENCE
:
3285 case D_COMP_COMPLEX
:
3286 case D_COMP_IMAGINARY
:
3287 case D_COMP_PTRMEM_TYPE
:
3290 case D_COMP_RESTRICT_THIS
:
3291 case D_COMP_VOLATILE_THIS
:
3292 case D_COMP_CONST_THIS
:
3301 if (d_left (dc
) != NULL
&& ! saw_mod
)
3306 switch (d_last_char (dpi
))
3314 d_append_char (dpi
, ' ');
3318 d_append_char (dpi
, '(');
3321 d_print_mod_list (dpi
, mods
, 0);
3324 d_append_char (dpi
, ')');
3326 d_append_char (dpi
, '(');
3328 if (d_right (dc
) != NULL
)
3329 d_print_comp (dpi
, d_right (dc
));
3331 d_append_char (dpi
, ')');
3333 d_print_mod_list (dpi
, mods
, 1);
3336 /* Print an array type, except for the element type. */
3339 d_print_array_type (dpi
, dc
, mods
)
3340 struct d_print_info
*dpi
;
3341 const struct d_comp
*dc
;
3342 struct d_print_mod
*mods
;
3350 struct d_print_mod
*p
;
3353 for (p
= mods
; p
!= NULL
; p
= p
->next
)
3358 if (p
->mod
->type
== D_COMP_ARRAY_TYPE
)
3372 d_append_string (dpi
, " (");
3374 d_print_mod_list (dpi
, mods
, 0);
3377 d_append_char (dpi
, ')');
3381 d_append_char (dpi
, ' ');
3383 d_append_char (dpi
, '[');
3385 if (d_left (dc
) != NULL
)
3386 d_print_comp (dpi
, d_left (dc
));
3388 d_append_char (dpi
, ']');
3391 /* Print an operator in an expression. */
3394 d_print_expr_op (dpi
, dc
)
3395 struct d_print_info
*dpi
;
3396 const struct d_comp
*dc
;
3398 if (dc
->type
== D_COMP_OPERATOR
)
3399 d_append_string (dpi
, dc
->u
.s_operator
.op
->name
);
3401 d_print_comp (dpi
, dc
);
3407 d_print_cast (dpi
, dc
)
3408 struct d_print_info
*dpi
;
3409 const struct d_comp
*dc
;
3411 if (d_left (dc
)->type
!= D_COMP_TEMPLATE
)
3412 d_print_comp (dpi
, d_left (dc
));
3415 struct d_print_mod
*hold_dpm
;
3416 struct d_print_template dpt
;
3418 /* It appears that for a templated cast operator, we need to put
3419 the template parameters in scope for the operator name, but
3420 not for the parameters. The effect is that we need to handle
3421 the template printing here. */
3423 hold_dpm
= dpi
->modifiers
;
3424 dpi
->modifiers
= NULL
;
3426 dpt
.next
= dpi
->templates
;
3427 dpi
->templates
= &dpt
;
3428 dpt
.template = d_left (dc
);
3430 d_print_comp (dpi
, d_left (d_left (dc
)));
3432 dpi
->templates
= dpt
.next
;
3434 if (d_last_char (dpi
) == '<')
3435 d_append_char (dpi
, ' ');
3436 d_append_char (dpi
, '<');
3437 d_print_comp (dpi
, d_right (d_left (dc
)));
3438 /* Avoid generating two consecutive '>' characters, to avoid
3439 the C++ syntactic ambiguity. */
3440 if (d_last_char (dpi
) == '>')
3441 d_append_char (dpi
, ' ');
3442 d_append_char (dpi
, '>');
3444 dpi
->modifiers
= hold_dpm
;
3448 /* Initialize the information structure we use to pass around
3452 d_init_info (mangled
, options
, len
, di
)
3453 const char *mangled
;
3459 di
->options
= options
;
3463 /* We can not need more components than twice the number of chars in
3464 the mangled string. Most components correspond directly to
3465 chars, but the ARGLIST types are exceptions. */
3466 di
->num_comps
= 2 * len
;
3467 di
->comps
= (struct d_comp
*) malloc (di
->num_comps
3468 * sizeof (struct d_comp
));
3471 /* Similarly, we can not need more substitutions than there are
3472 chars in the mangled string. */
3474 di
->subs
= (struct d_comp
**) malloc (di
->num_subs
3475 * sizeof (struct d_comp
*));
3478 di
->last_name
= NULL
;
3480 if (di
->comps
== NULL
|| di
->subs
== NULL
)
3482 if (di
->comps
!= NULL
)
3484 if (di
->subs
!= NULL
)
3492 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
3493 name, return a buffer allocated with malloc holding the demangled
3494 name. OPTIONS is the usual libiberty demangler options. On
3495 success, this sets *PALC to the allocated size of the returned
3496 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
3497 a memory allocation failure. On failure, this returns NULL. */
3500 d_demangle (mangled
, options
, palc
)
3501 const char* mangled
;
3513 len
= strlen (mangled
);
3515 if (mangled
[0] == '_' && mangled
[1] == 'Z')
3517 else if (strncmp (mangled
, "_GLOBAL_", 8) == 0
3518 && (mangled
[8] == '.' || mangled
[8] == '_' || mangled
[8] == '$')
3519 && (mangled
[9] == 'D' || mangled
[9] == 'I')
3520 && mangled
[10] == '_')
3524 r
= malloc (40 + len
- 11);
3529 if (mangled
[9] == 'I')
3530 strcpy (r
, "global constructors keyed to ");
3532 strcpy (r
, "global destructors keyed to ");
3533 strcat (r
, mangled
+ 11);
3539 if ((options
& DMGL_TYPES
) == 0)
3544 if (! d_init_info (mangled
, options
, len
, &di
))
3551 dc
= d_mangled_name (&di
, 1);
3555 /* If we didn't consume the entire mangled string, then we didn't
3556 successfully demangle it. */
3557 if (d_peek_char (&di
) != '\0')
3560 #ifdef CP_DEMANGLE_DEBUG
3562 printf ("failed demangling\n");
3572 ret
= d_print (options
, dc
, palc
);
3579 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
3581 extern char *__cxa_demangle
PARAMS ((const char *, char *, size_t *, int *));
3583 /* ia64 ABI-mandated entry point in the C++ runtime library for
3584 performing demangling. MANGLED_NAME is a NUL-terminated character
3585 string containing the name to be demangled.
3587 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
3588 *LENGTH bytes, into which the demangled name is stored. If
3589 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
3590 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
3591 is placed in a region of memory allocated with malloc.
3593 If LENGTH is non-NULL, the length of the buffer conaining the
3594 demangled name, is placed in *LENGTH.
3596 The return value is a pointer to the start of the NUL-terminated
3597 demangled name, or NULL if the demangling fails. The caller is
3598 responsible for deallocating this memory using free.
3600 *STATUS is set to one of the following values:
3601 0: The demangling operation succeeded.
3602 -1: A memory allocation failure occurred.
3603 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
3604 -3: One of the arguments is invalid.
3606 The demangling is performed using the C++ ABI mangling rules, with
3610 __cxa_demangle (mangled_name
, output_buffer
, length
, status
)
3611 const char *mangled_name
;
3612 char *output_buffer
;
3622 if (mangled_name
== NULL
)
3628 if (output_buffer
!= NULL
&& length
== NULL
)
3634 demangled
= d_demangle (mangled_name
, DMGL_TYPES
, &alc
);
3636 if (demangled
== NULL
)
3645 if (output_buffer
== NULL
)
3652 if (strlen (demangled
) < *length
)
3654 strcpy (output_buffer
, demangled
);
3656 demangled
= output_buffer
;
3660 free (output_buffer
);
3670 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
3672 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
3673 mangled name, return a buffer allocated with malloc holding the
3674 demangled name. Otherwise, return NULL. */
3677 cplus_demangle_v3 (mangled
, options
)
3678 const char* mangled
;
3683 return d_demangle (mangled
, options
, &alc
);
3686 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
3687 conventions, but the output formatting is a little different.
3688 This instructs the C++ demangler not to emit pointer characters ("*"), and
3689 to use Java's namespace separator symbol ("." instead of "::"). It then
3690 does an additional pass over the demangled output to replace instances
3691 of JArray<TYPE> with TYPE[]. */
3694 java_demangle_v3 (mangled
)
3695 const char* mangled
;
3703 demangled
= d_demangle (mangled
, DMGL_JAVA
| DMGL_PARAMS
, &alc
);
3705 if (demangled
== NULL
)
3711 while (*from
!= '\0')
3713 if (strncmp (from
, "JArray<", 7) == 0)
3718 else if (nesting
> 0 && *from
== '>')
3720 while (to
> demangled
&& to
[-1] == ' ')
3736 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
3738 #ifndef IN_GLIBCPP_V3
3740 /* Demangle a string in order to find out whether it is a constructor
3741 or destructor. Return non-zero on success. Set *CTOR_KIND and
3742 *DTOR_KIND appropriately. */
3745 is_ctor_or_dtor (mangled
, ctor_kind
, dtor_kind
)
3746 const char *mangled
;
3747 enum gnu_v3_ctor_kinds
*ctor_kind
;
3748 enum gnu_v3_dtor_kinds
*dtor_kind
;
3754 *ctor_kind
= (enum gnu_v3_ctor_kinds
) 0;
3755 *dtor_kind
= (enum gnu_v3_dtor_kinds
) 0;
3757 if (! d_init_info (mangled
, DMGL_GNU_V3
, strlen (mangled
), &di
))
3760 dc
= d_mangled_name (&di
, 1);
3763 if (d_peek_char (&di
) == '\0')
3772 case D_COMP_TYPED_NAME
:
3773 case D_COMP_TEMPLATE
:
3774 case D_COMP_RESTRICT_THIS
:
3775 case D_COMP_VOLATILE_THIS
:
3776 case D_COMP_CONST_THIS
:
3779 case D_COMP_QUAL_NAME
:
3783 *ctor_kind
= dc
->u
.s_ctor
.kind
;
3788 *dtor_kind
= dc
->u
.s_dtor
.kind
;
3802 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
3803 name. A non-zero return indicates the type of constructor. */
3805 enum gnu_v3_ctor_kinds
3806 is_gnu_v3_mangled_ctor (name
)
3809 enum gnu_v3_ctor_kinds ctor_kind
;
3810 enum gnu_v3_dtor_kinds dtor_kind
;
3812 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
3813 return (enum gnu_v3_ctor_kinds
) 0;
3818 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
3819 name. A non-zero return indicates the type of destructor. */
3821 enum gnu_v3_dtor_kinds
3822 is_gnu_v3_mangled_dtor (name
)
3825 enum gnu_v3_ctor_kinds ctor_kind
;
3826 enum gnu_v3_dtor_kinds dtor_kind
;
3828 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
3829 return (enum gnu_v3_dtor_kinds
) 0;
3833 #endif /* IN_GLIBCPP_V3 */
3835 #ifdef STANDALONE_DEMANGLER
3838 #include "dyn-string.h"
3840 static void print_usage
PARAMS ((FILE* fp
, int exit_value
));
3842 #define IS_ALPHA(CHAR) \
3843 (((CHAR) >= 'a' && (CHAR) <= 'z') \
3844 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
3846 /* Non-zero if CHAR is a character than can occur in a mangled name. */
3847 #define is_mangled_char(CHAR) \
3848 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
3849 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
3851 /* The name of this program, as invoked. */
3852 const char* program_name
;
3854 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
3857 print_usage (fp
, exit_value
)
3861 fprintf (fp
, "Usage: %s [options] [names ...]\n", program_name
);
3862 fprintf (fp
, "Options:\n");
3863 fprintf (fp
, " -h,--help Display this message.\n");
3864 fprintf (fp
, " -p,--no-params Don't display function parameters\n");
3865 fprintf (fp
, " -v,--verbose Produce verbose demanglings.\n");
3866 fprintf (fp
, "If names are provided, they are demangled. Otherwise filters standard input.\n");
3871 /* Option specification for getopt_long. */
3872 static const struct option long_options
[] =
3874 { "help", no_argument
, NULL
, 'h' },
3875 { "no-params", no_argument
, NULL
, 'p' },
3876 { "verbose", no_argument
, NULL
, 'v' },
3877 { NULL
, no_argument
, NULL
, 0 },
3880 /* Main entry for a demangling filter executable. It will demangle
3881 its command line arguments, if any. If none are provided, it will
3882 filter stdin to stdout, replacing any recognized mangled C++ names
3883 with their demangled equivalents. */
3892 int options
= DMGL_PARAMS
| DMGL_ANSI
| DMGL_TYPES
;
3894 /* Use the program name of this program, as invoked. */
3895 program_name
= argv
[0];
3897 /* Parse options. */
3900 opt_char
= getopt_long (argc
, argv
, "hpv", long_options
, NULL
);
3903 case '?': /* Unrecognized option. */
3904 print_usage (stderr
, 1);
3908 print_usage (stdout
, 0);
3912 options
&= ~ DMGL_PARAMS
;
3916 options
|= DMGL_VERBOSE
;
3920 while (opt_char
!= -1);
3923 /* No command line arguments were provided. Filter stdin. */
3925 dyn_string_t mangled
= dyn_string_new (3);
3928 /* Read all of input. */
3929 while (!feof (stdin
))
3933 /* Pile characters into mangled until we hit one that can't
3934 occur in a mangled name. */
3936 while (!feof (stdin
) && is_mangled_char (c
))
3938 dyn_string_append_char (mangled
, c
);
3944 if (dyn_string_length (mangled
) > 0)
3946 s
= cplus_demangle_v3 (dyn_string_buf (mangled
), options
);
3955 /* It might not have been a mangled name. Print the
3957 fputs (dyn_string_buf (mangled
), stdout
);
3960 dyn_string_clear (mangled
);
3963 /* If we haven't hit EOF yet, we've read one character that
3964 can't occur in a mangled name, so print it out. */
3969 dyn_string_delete (mangled
);
3972 /* Demangle command line arguments. */
3974 /* Loop over command line arguments. */
3975 for (i
= optind
; i
< argc
; ++i
)
3979 /* Attempt to demangle. */
3980 s
= cplus_demangle_v3 (argv
[i
], options
);
3982 /* If it worked, print the demangled name. */
3989 fprintf (stderr
, "Failed: %s\n", argv
[i
]);
3996 #endif /* STANDALONE_DEMANGLER */