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.
45 #include "libiberty.h"
48 /* This code implements a demangler for the g++ V3 ABI. The ABI is
49 described on this web page:
50 http://www.codesourcery.com/cxx-abi/abi.html#mangling
52 This code was written while looking at the demangler written by
53 Alex Samuel <samuel@codesourcery.com>.
55 This code first pulls the mangled name apart into a list of
56 components, and then walks the list generating the demangled
59 /* Avoid pulling in the ctype tables for this simple usage. */
60 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
62 /* The prefix prepended by GCC to an identifier represnting the
63 anonymous namespace. */
64 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
65 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
66 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
68 /* Information we keep for operators. */
70 struct d_operator_info
76 /* Number of arguments. */
80 /* How to print the value of a builtin type. */
82 enum d_builtin_type_print
84 /* Print as (type)val. */
86 /* Print as integer. */
88 /* Print as long, with trailing `l'. */
92 /* Print in usual way, but here to detect void. */
96 /* Information we keep for a builtin type. */
98 struct d_builtin_type_info
102 /* Type name when using Java. */
103 const char *java_name
;
104 /* How to print a value of this type. */
105 enum d_builtin_type_print print
;
108 /* Component types found in mangled names. */
114 /* A qualified name. */
120 /* A template parameter. */
121 D_COMP_TEMPLATE_PARAM
,
128 /* A VTT structure. */
130 /* A construction vtable. */
131 D_COMP_CONSTRUCTION_VTABLE
,
132 /* A typeinfo structure. */
134 /* A typeinfo name. */
135 D_COMP_TYPEINFO_NAME
,
136 /* A typeinfo function. */
140 /* A virtual thunk. */
141 D_COMP_VIRTUAL_THUNK
,
142 /* A covariant thunk. */
143 D_COMP_COVARIANT_THUNK
,
146 /* A guard variable. */
148 /* A reference temporary. */
150 /* A standard substitution. */
152 /* The restrict qualifier. */
154 /* The volatile qualifier. */
156 /* The const qualifier. */
158 /* A vendor qualifier. */
159 D_COMP_VENDOR_TYPE_QUAL
,
164 /* A complex type. */
166 /* An imaginary type. */
168 /* A builtin type. */
170 /* A vendor's builtin type. */
172 /* A function type. */
173 D_COMP_FUNCTION_TYPE
,
176 /* A pointer to member type. */
178 /* An argument list. */
180 /* A template argument list. */
181 D_COMP_TEMPLATE_ARGLIST
,
184 /* An extended operator. */
185 D_COMP_EXTENDED_OPERATOR
,
188 /* A unary expression. */
190 /* A binary expression. */
192 /* Arguments to a binary expression. */
194 /* A trinary expression. */
196 /* Arguments to a trinary expression. */
203 /* A component of the mangled name. */
207 /* The type of this component. */
208 enum d_comp_type type
;
211 /* For D_COMP_NAME. */
214 /* A pointer to the name (not NULL terminated) and it's
220 /* For D_COMP_OPERATOR. */
224 const struct d_operator_info
*op
;
227 /* For D_COMP_EXTENDED_OPERATOR. */
230 /* Number of arguments. */
234 } s_extended_operator
;
236 /* For D_COMP_CTOR. */
239 enum gnu_v3_ctor_kinds kind
;
243 /* For D_COMP_DTOR. */
246 enum gnu_v3_dtor_kinds kind
;
250 /* For D_COMP_BUILTIN_TYPE. */
253 const struct d_builtin_type_info
*type
;
256 /* For D_COMP_SUB_STD. */
262 /* For D_COMP_TEMPLATE_PARAM. */
268 /* For other types. */
272 struct d_comp
*right
;
278 #define d_left(dc) ((dc)->u.s_binary.left)
279 #define d_right(dc) ((dc)->u.s_binary.right)
281 /* The information structure we pass around. */
285 /* The string we are demangling. */
287 /* The options passed to the demangler. */
289 /* The next character in the string to consider. */
291 /* The array of components. */
292 struct d_comp
*comps
;
293 /* The index of the next available component. */
295 /* The number of available component structures. */
297 /* The array of substitutions. */
298 struct d_comp
**subs
;
299 /* The index of the next substitution. */
301 /* The number of available entries in the subs array. */
303 /* The last name we saw, for constructors and destructors. */
304 struct d_comp
*last_name
;
307 #define d_peek_char(di) (*((di)->n))
308 #define d_peek_next_char(di) ((di)->n[1])
309 #define d_advance(di, i) ((di)->n += (i))
310 #define d_next_char(di) (*((di)->n++))
311 #define d_str(di) ((di)->n)
313 /* A list of templates. This is used while printing. */
315 struct d_print_template
317 /* Next template on the list. */
318 struct d_print_template
*next
;
320 const struct d_comp
*template;
323 /* A list of type modifiers. This is used while printing. */
327 /* Next modifier on the list. These are in the reverse of the order
328 in which they appeared in the mangled string. */
329 struct d_print_mod
*next
;
331 const struct d_comp
*mod
;
332 /* Whether this modifier was printed. */
334 /* The list of templates which applies to this modifier. */
335 struct d_print_template
*templates
;
338 /* We use this structure to hold information during printing. */
342 /* The options passed to the demangler. */
344 /* Buffer holding the result. */
346 /* Current length of data in buffer. */
348 /* Allocated size of buffer. */
350 /* The current list of templates, if any. */
351 struct d_print_template
*templates
;
352 /* The current list of modifiers (e.g., pointer, reference, etc.),
354 struct d_print_mod
*modifiers
;
355 /* Set to 1 if we had a memory allocation failure. */
356 int allocation_failure
;
359 #define d_print_saw_error(dpi) ((dpi)->buf == NULL)
361 #define d_append_char(dpi, c) \
364 if ((dpi)->buf != NULL && (dpi)->len < (dpi)->alc) \
365 (dpi)->buf[(dpi)->len++] = (c); \
367 d_print_append_char ((dpi), (c)); \
371 #define d_append_buffer(dpi, s, l) \
374 if ((dpi)->buf != NULL && (dpi)->len + (l) <= (dpi)->alc) \
376 memcpy ((dpi)->buf + (dpi)->len, (s), (l)); \
380 d_print_append_buffer ((dpi), (s), (l)); \
384 #define d_append_string(dpi, s) \
387 size_t d_append_string_len = strlen (s); \
388 d_append_buffer ((dpi), (s), d_append_string_len); \
392 #ifdef CP_DEMANGLE_DEBUG
393 static void d_dump
PARAMS ((struct d_comp
*, int));
395 static struct d_comp
*d_make_empty
PARAMS ((struct d_info
*,
397 static struct d_comp
*d_make_comp
PARAMS ((struct d_info
*, enum d_comp_type
,
398 struct d_comp
*, struct d_comp
*));
399 static struct d_comp
*d_make_name
PARAMS ((struct d_info
*, const char *,
401 static struct d_comp
*d_make_builtin_type
PARAMS ((struct d_info
*,
402 const struct d_builtin_type_info
*));
403 static struct d_comp
*d_make_operator
PARAMS ((struct d_info
*,
404 const struct d_operator_info
*));
405 static struct d_comp
*d_make_extended_operator
PARAMS ((struct d_info
*,
408 static struct d_comp
*d_make_ctor
PARAMS ((struct d_info
*,
409 enum gnu_v3_ctor_kinds
,
411 static struct d_comp
*d_make_dtor
PARAMS ((struct d_info
*,
412 enum gnu_v3_dtor_kinds
,
414 static struct d_comp
*d_make_template_param
PARAMS ((struct d_info
*, long));
415 static struct d_comp
*d_make_sub
PARAMS ((struct d_info
*, const char *));
416 static struct d_comp
*d_mangled_name
PARAMS ((struct d_info
*, int));
417 static int has_return_type
PARAMS ((struct d_comp
*));
418 static int is_ctor_dtor_or_conversion
PARAMS ((struct d_comp
*));
419 static struct d_comp
*d_encoding
PARAMS ((struct d_info
*, int));
420 static struct d_comp
*d_name
PARAMS ((struct d_info
*));
421 static struct d_comp
*d_nested_name
PARAMS ((struct d_info
*));
422 static struct d_comp
*d_prefix
PARAMS ((struct d_info
*));
423 static struct d_comp
*d_unqualified_name
PARAMS ((struct d_info
*));
424 static struct d_comp
*d_source_name
PARAMS ((struct d_info
*));
425 static long d_number
PARAMS ((struct d_info
*));
426 static struct d_comp
*d_identifier
PARAMS ((struct d_info
*, int));
427 static struct d_comp
*d_operator_name
PARAMS ((struct d_info
*));
428 static struct d_comp
*d_special_name
PARAMS ((struct d_info
*));
429 static int d_call_offset
PARAMS ((struct d_info
*, int));
430 static struct d_comp
*d_ctor_dtor_name
PARAMS ((struct d_info
*));
431 static struct d_comp
*d_type
PARAMS ((struct d_info
*));
432 static struct d_comp
**d_cv_qualifiers
PARAMS ((struct d_info
*,
434 static struct d_comp
*d_function_type
PARAMS ((struct d_info
*));
435 static struct d_comp
*d_bare_function_type
PARAMS ((struct d_info
*, int));
436 static struct d_comp
*d_class_enum_type
PARAMS ((struct d_info
*));
437 static struct d_comp
*d_array_type
PARAMS ((struct d_info
*));
438 static struct d_comp
*d_pointer_to_member_type
PARAMS ((struct d_info
*));
439 static struct d_comp
*d_template_param
PARAMS ((struct d_info
*));
440 static struct d_comp
*d_template_args
PARAMS ((struct d_info
*));
441 static struct d_comp
*d_template_arg
PARAMS ((struct d_info
*));
442 static struct d_comp
*d_expression
PARAMS ((struct d_info
*));
443 static struct d_comp
*d_expr_primary
PARAMS ((struct d_info
*));
444 static struct d_comp
*d_local_name
PARAMS ((struct d_info
*));
445 static int d_discriminator
PARAMS ((struct d_info
*));
446 static int d_add_substitution
PARAMS ((struct d_info
*, struct d_comp
*));
447 static struct d_comp
*d_substitution
PARAMS ((struct d_info
*));
448 static void d_print_resize
PARAMS ((struct d_print_info
*, size_t));
449 static void d_print_append_char
PARAMS ((struct d_print_info
*, int));
450 static void d_print_append_buffer
PARAMS ((struct d_print_info
*, const char *,
452 static void d_print_error
PARAMS ((struct d_print_info
*));
453 static char *d_print
PARAMS ((int, const struct d_comp
*, size_t *));
454 static void d_print_comp
PARAMS ((struct d_print_info
*,
455 const struct d_comp
*));
456 static void d_print_identifier
PARAMS ((struct d_print_info
*, const char *,
458 static void d_print_mod_list
PARAMS ((struct d_print_info
*,
459 struct d_print_mod
*));
460 static void d_print_mod
PARAMS ((struct d_print_info
*,
461 const struct d_comp
*));
462 static void d_print_function_type
PARAMS ((struct d_print_info
*,
463 const struct d_comp
*,
464 struct d_print_mod
*));
465 static void d_print_array_type
PARAMS ((struct d_print_info
*,
466 const struct d_comp
*,
467 struct d_print_mod
*));
468 static void d_print_expr_op
PARAMS ((struct d_print_info
*,
469 const struct d_comp
*));
470 static void d_print_cast
PARAMS ((struct d_print_info
*,
471 const struct d_comp
*));
472 static int d_init_info
PARAMS ((const char *, int, size_t, struct d_info
*));
473 static char *d_demangle
PARAMS ((const char *, int, size_t *));
475 #ifdef CP_DEMANGLE_DEBUG
487 for (i
= 0; i
< indent
; ++i
)
493 printf ("name '%.*s'\n", dc
->u
.s_name
.len
, dc
->u
.s_name
.s
);
495 case D_COMP_TEMPLATE_PARAM
:
496 printf ("template parameter %ld\n", dc
->u
.s_number
.number
);
499 printf ("constructor %d\n", (int) dc
->u
.s_ctor
.kind
);
500 d_dump (dc
->u
.s_ctor
.name
, indent
+ 2);
503 printf ("destructor %d\n", (int) dc
->u
.s_dtor
.kind
);
504 d_dump (dc
->u
.s_dtor
.name
, indent
+ 2);
507 printf ("standard substitution %s\n", dc
->u
.s_string
.string
);
509 case D_COMP_BUILTIN_TYPE
:
510 printf ("builtin type %s\n", dc
->u
.s_builtin
.type
->name
);
512 case D_COMP_OPERATOR
:
513 printf ("operator %s\n", dc
->u
.s_operator
.op
->name
);
515 case D_COMP_EXTENDED_OPERATOR
:
516 printf ("extended operator with %d args\n",
517 dc
->u
.s_extended_operator
.args
);
518 d_dump (dc
->u
.s_extended_operator
.name
, indent
+ 2);
521 case D_COMP_QUAL_NAME
:
522 printf ("qualified name\n");
524 case D_COMP_TYPED_NAME
:
525 printf ("typed name\n");
527 case D_COMP_TEMPLATE
:
528 printf ("template\n");
536 case D_COMP_CONSTRUCTION_VTABLE
:
537 printf ("construction vtable\n");
539 case D_COMP_TYPEINFO
:
540 printf ("typeinfo\n");
542 case D_COMP_TYPEINFO_NAME
:
543 printf ("typeinfo name\n");
545 case D_COMP_TYPEINFO_FN
:
546 printf ("typeinfo function\n");
551 case D_COMP_VIRTUAL_THUNK
:
552 printf ("virtual thunk\n");
554 case D_COMP_COVARIANT_THUNK
:
555 printf ("covariant thunk\n");
557 case D_COMP_JAVA_CLASS
:
558 printf ("java class\n");
564 printf ("reference temporary\n");
566 case D_COMP_RESTRICT
:
567 printf ("restrict\n");
569 case D_COMP_VOLATILE
:
570 printf ("volatile\n");
575 case D_COMP_VENDOR_TYPE_QUAL
:
576 printf ("vendor type qualifier\n");
579 printf ("pointer\n");
581 case D_COMP_REFERENCE
:
582 printf ("reference\n");
585 printf ("complex\n");
587 case D_COMP_IMAGINARY
:
588 printf ("imaginary\n");
590 case D_COMP_VENDOR_TYPE
:
591 printf ("vendor type\n");
593 case D_COMP_FUNCTION_TYPE
:
594 printf ("function type\n");
596 case D_COMP_ARRAY_TYPE
:
597 printf ("array type\n");
599 case D_COMP_PTRMEM_TYPE
:
600 printf ("pointer to member type\n");
603 printf ("argument list\n");
605 case D_COMP_TEMPLATE_ARGLIST
:
606 printf ("template argument list\n");
612 printf ("unary operator\n");
615 printf ("binary operator\n");
617 case D_COMP_BINARY_ARGS
:
618 printf ("binary operator arguments\n");
621 printf ("trinary operator\n");
623 case D_COMP_TRINARY_ARG1
:
624 printf ("trinary operator arguments 1\n");
626 case D_COMP_TRINARY_ARG2
:
627 printf ("trinary operator arguments 1\n");
630 printf ("literal\n");
634 d_dump (d_left (dc
), indent
+ 2);
635 d_dump (d_right (dc
), indent
+ 2);
638 #endif /* CP_DEMANGLE_DEBUG */
640 /* Add a new component. */
642 static struct d_comp
*
643 d_make_empty (di
, type
)
645 enum d_comp_type type
;
649 if (di
->next_comp
>= di
->num_comps
)
651 p
= &di
->comps
[di
->next_comp
];
657 /* Add a new generic component. */
659 static struct d_comp
*
660 d_make_comp (di
, type
, left
, right
)
662 enum d_comp_type type
;
664 struct d_comp
*right
;
668 /* We check for errors here. A typical error would be a NULL return
669 from a subroutine. We catch those here, and return NULL
673 /* These types require two parameters. */
674 case D_COMP_QUAL_NAME
:
675 case D_COMP_TYPED_NAME
:
676 case D_COMP_TEMPLATE
:
677 case D_COMP_VENDOR_TYPE_QUAL
:
678 case D_COMP_PTRMEM_TYPE
:
681 case D_COMP_BINARY_ARGS
:
683 case D_COMP_TRINARY_ARG1
:
684 case D_COMP_TRINARY_ARG2
:
686 if (left
== NULL
|| right
== NULL
)
690 /* These types only require one parameter. */
693 case D_COMP_CONSTRUCTION_VTABLE
:
694 case D_COMP_TYPEINFO
:
695 case D_COMP_TYPEINFO_NAME
:
696 case D_COMP_TYPEINFO_FN
:
698 case D_COMP_VIRTUAL_THUNK
:
699 case D_COMP_COVARIANT_THUNK
:
700 case D_COMP_JAVA_CLASS
:
704 case D_COMP_REFERENCE
:
706 case D_COMP_IMAGINARY
:
707 case D_COMP_VENDOR_TYPE
:
709 case D_COMP_TEMPLATE_ARGLIST
:
715 /* This needs a right parameter, but the left parameter can be
717 case D_COMP_ARRAY_TYPE
:
722 /* These are allowed to have no parameters--in some cases they
723 will be filled in later. */
724 case D_COMP_FUNCTION_TYPE
:
725 case D_COMP_RESTRICT
:
726 case D_COMP_VOLATILE
:
730 /* Other types should not be seen here. */
735 p
= d_make_empty (di
, type
);
738 p
->u
.s_binary
.left
= left
;
739 p
->u
.s_binary
.right
= right
;
744 /* Add a new name component. */
746 static struct d_comp
*
747 d_make_name (di
, s
, len
)
754 p
= d_make_empty (di
, D_COMP_NAME
);
758 p
->u
.s_name
.len
= len
;
763 /* Add a new builtin type component. */
765 static struct d_comp
*
766 d_make_builtin_type (di
, type
)
768 const struct d_builtin_type_info
*type
;
774 p
= d_make_empty (di
, D_COMP_BUILTIN_TYPE
);
776 p
->u
.s_builtin
.type
= type
;
780 /* Add a new operator component. */
782 static struct d_comp
*
783 d_make_operator (di
, op
)
785 const struct d_operator_info
*op
;
789 p
= d_make_empty (di
, D_COMP_OPERATOR
);
791 p
->u
.s_operator
.op
= op
;
795 /* Add a new extended operator component. */
797 static struct d_comp
*
798 d_make_extended_operator (di
, args
, name
)
807 p
= d_make_empty (di
, D_COMP_EXTENDED_OPERATOR
);
810 p
->u
.s_extended_operator
.args
= args
;
811 p
->u
.s_extended_operator
.name
= name
;
816 /* Add a new constructor component. */
818 static struct d_comp
*
819 d_make_ctor (di
, kind
, name
)
821 enum gnu_v3_ctor_kinds kind
;
828 p
= d_make_empty (di
, D_COMP_CTOR
);
831 p
->u
.s_ctor
.kind
= kind
;
832 p
->u
.s_ctor
.name
= name
;
837 /* Add a new destructor component. */
839 static struct d_comp
*
840 d_make_dtor (di
, kind
, name
)
842 enum gnu_v3_dtor_kinds kind
;
849 p
= d_make_empty (di
, D_COMP_DTOR
);
852 p
->u
.s_dtor
.kind
= kind
;
853 p
->u
.s_dtor
.name
= name
;
858 /* Add a new template parameter. */
860 static struct d_comp
*
861 d_make_template_param (di
, i
)
867 p
= d_make_empty (di
, D_COMP_TEMPLATE_PARAM
);
869 p
->u
.s_number
.number
= i
;
873 /* Add a new standard substitution component. */
875 static struct d_comp
*
876 d_make_sub (di
, name
)
882 p
= d_make_empty (di
, D_COMP_SUB_STD
);
884 p
->u
.s_string
.string
= name
;
888 /* <mangled-name> ::= _Z <encoding>
890 TOP_LEVEL is non-zero when called at the top level. */
892 static struct d_comp
*
893 d_mangled_name (di
, top_level
)
897 if (d_next_char (di
) != '_')
899 if (d_next_char (di
) != 'Z')
901 return d_encoding (di
, top_level
);
904 /* Return whether a function should have a return type. The argument
905 is the function name, which may be qualified in various ways. The
906 rules are that template functions have return types with some
907 exceptions, function types which are not part of a function name
908 mangling have return types with some exceptions, and non-template
909 function names do not have return types. The exceptions are that
910 constructors, destructors, and conversion operators do not have
923 case D_COMP_TEMPLATE
:
924 return ! is_ctor_dtor_or_conversion (d_left (dc
));
925 case D_COMP_RESTRICT
:
926 case D_COMP_VOLATILE
:
928 case D_COMP_VENDOR_TYPE_QUAL
:
929 return has_return_type (d_left (dc
));
933 /* Return whether a name is a constructor, a destructor, or a
934 conversion operator. */
937 is_ctor_dtor_or_conversion (dc
)
946 case D_COMP_QUAL_NAME
:
947 return is_ctor_dtor_or_conversion (d_right (dc
));
955 /* <encoding> ::= <(function) name> <bare-function-type>
959 TOP_LEVEL is non-zero when called at the top level, in which case
960 if DMGL_PARAMS is not set we do not demangle the function
961 parameters. We only set this at the top level, because otherwise
962 we would not correctly demangle names in local scopes. */
964 static struct d_comp
*
965 d_encoding (di
, top_level
)
969 char peek
= d_peek_char (di
);
971 if (peek
== 'G' || peek
== 'T')
972 return d_special_name (di
);
979 if (dc
!= NULL
&& top_level
&& (di
->options
& DMGL_PARAMS
) == 0)
981 /* Strip off any initial CV-qualifiers, as they really apply
982 to the `this' parameter, and they were not output by the
983 v2 demangler without DMGL_PARAMS. */
984 while (dc
->type
== D_COMP_RESTRICT
985 || dc
->type
== D_COMP_VOLATILE
986 || dc
->type
== D_COMP_CONST
)
991 peek
= d_peek_char (di
);
992 if (peek
== '\0' || peek
== 'E')
994 return d_make_comp (di
, D_COMP_TYPED_NAME
, dc
,
995 d_bare_function_type (di
, has_return_type (dc
)));
999 /* <name> ::= <nested-name>
1001 ::= <unscoped-template-name> <template-args>
1004 <unscoped-name> ::= <unqualified-name>
1005 ::= St <unqualified-name>
1007 <unscoped-template-name> ::= <unscoped-name>
1011 static struct d_comp
*
1015 char peek
= d_peek_char (di
);
1021 return d_nested_name (di
);
1024 return d_local_name (di
);
1030 if (d_peek_next_char (di
) != 't')
1032 dc
= d_substitution (di
);
1038 dc
= d_make_comp (di
, D_COMP_QUAL_NAME
, d_make_name (di
, "std", 3),
1039 d_unqualified_name (di
));
1043 if (d_peek_char (di
) != 'I')
1045 /* The grammar does not permit this case to occur if we
1046 called d_substitution() above (i.e., subst == 1). We
1047 don't bother to check. */
1051 /* This is <template-args>, which means that we just saw
1052 <unscoped-template-name>, which is a substitution
1053 candidate if we didn't just get it from a
1057 if (! d_add_substitution (di
, dc
))
1060 dc
= d_make_comp (di
, D_COMP_TEMPLATE
, dc
, d_template_args (di
));
1067 dc
= d_unqualified_name (di
);
1068 if (d_peek_char (di
) == 'I')
1070 /* This is <template-args>, which means that we just saw
1071 <unscoped-template-name>, which is a substitution
1073 if (! d_add_substitution (di
, dc
))
1075 dc
= d_make_comp (di
, D_COMP_TEMPLATE
, dc
, d_template_args (di
));
1081 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1082 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1085 static struct d_comp
*
1090 struct d_comp
**pret
;
1092 if (d_next_char (di
) != 'N')
1095 pret
= d_cv_qualifiers (di
, &ret
);
1099 *pret
= d_prefix (di
);
1103 if (d_next_char (di
) != 'E')
1109 /* <prefix> ::= <prefix> <unqualified-name>
1110 ::= <template-prefix> <template-args>
1111 ::= <template-param>
1115 <template-prefix> ::= <prefix> <(template) unqualified-name>
1116 ::= <template-param>
1120 static struct d_comp
*
1124 struct d_comp
*ret
= NULL
;
1129 enum d_comp_type comb_type
;
1132 peek
= d_peek_char (di
);
1136 /* The older code accepts a <local-name> here, but I don't see
1137 that in the grammar. The older code does not accept a
1138 <template-param> here. */
1140 comb_type
= D_COMP_QUAL_NAME
;
1142 || (peek
>= 'a' && peek
<= 'z')
1145 dc
= d_unqualified_name (di
);
1146 else if (peek
== 'S')
1147 dc
= d_substitution (di
);
1148 else if (peek
== 'I')
1152 comb_type
= D_COMP_TEMPLATE
;
1153 dc
= d_template_args (di
);
1155 else if (peek
== 'T')
1156 dc
= d_template_param (di
);
1157 else if (peek
== 'E')
1165 ret
= d_make_comp (di
, comb_type
, ret
, dc
);
1167 if (peek
!= 'S' && d_peek_char (di
) != 'E')
1169 if (! d_add_substitution (di
, ret
))
1175 /* <unqualified-name> ::= <operator-name>
1176 ::= <ctor-dtor-name>
1180 static struct d_comp
*
1181 d_unqualified_name (di
)
1186 peek
= d_peek_char (di
);
1187 if (IS_DIGIT (peek
))
1188 return d_source_name (di
);
1189 else if (peek
>= 'a' && peek
<= 'z')
1190 return d_operator_name (di
);
1191 else if (peek
== 'C' || peek
== 'D')
1192 return d_ctor_dtor_name (di
);
1197 /* <source-name> ::= <(positive length) number> <identifier> */
1199 static struct d_comp
*
1206 len
= d_number (di
);
1209 ret
= d_identifier (di
, len
);
1210 di
->last_name
= ret
;
1214 /* number ::= [n] <(non-negative decimal integer)> */
1225 peek
= d_peek_char (di
);
1230 peek
= d_peek_char (di
);
1236 if (! IS_DIGIT (peek
))
1238 ret
= ret
* 10 + peek
- '0';
1240 peek
= d_peek_char (di
);
1244 /* identifier ::= <(unqualified source code identifier)> */
1246 static struct d_comp
*
1247 d_identifier (di
, len
)
1254 d_advance (di
, len
);
1256 /* Look for something which looks like a gcc encoding of an
1257 anonymous namespace, and replace it with a more user friendly
1259 if (len
>= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN
+ 2
1260 && memcmp (name
, ANONYMOUS_NAMESPACE_PREFIX
,
1261 ANONYMOUS_NAMESPACE_PREFIX_LEN
) == 0)
1265 s
= name
+ ANONYMOUS_NAMESPACE_PREFIX_LEN
;
1266 if ((*s
== '.' || *s
== '_' || *s
== '$')
1268 return d_make_name (di
, "(anonymous namespace)",
1269 sizeof "(anonymous namespace)" - 1);
1272 return d_make_name (di
, name
, len
);
1275 /* operator_name ::= many different two character encodings.
1277 ::= v <digit> <source-name>
1280 static const struct d_operator_info d_operators
[] =
1291 { "da", "delete[]", 1 },
1293 { "dl", "delete", 1 },
1310 { "na", "new[]", 1 },
1329 { "st", "sizeof ", 1 },
1330 { "sz", "sizeof ", 1 }
1333 static struct d_comp
*
1334 d_operator_name (di
)
1340 c1
= d_next_char (di
);
1341 c2
= d_next_char (di
);
1342 if (c1
== 'v' && IS_DIGIT (c2
))
1343 return d_make_extended_operator (di
, c2
- '0', d_source_name (di
));
1344 else if (c1
== 'c' && c2
== 'v')
1345 return d_make_comp (di
, D_COMP_CAST
, d_type (di
), NULL
);
1349 int high
= sizeof (d_operators
) / sizeof (d_operators
[0]);
1354 const struct d_operator_info
*p
;
1356 i
= low
+ (high
- low
) / 2;
1357 p
= d_operators
+ i
;
1359 if (c1
== p
->code
[0] && c2
== p
->code
[1])
1360 return d_make_operator (di
, p
);
1362 if (c1
< p
->code
[0] || (c1
== p
->code
[0] && c2
< p
->code
[1]))
1372 /* <special-name> ::= TV <type>
1376 ::= GV <(object) name>
1377 ::= T <call-offset> <(base) encoding>
1378 ::= Tc <call-offset> <call-offset> <(base) encoding>
1379 Also g++ extensions:
1380 ::= TC <type> <(offset) number> _ <(base) type>
1386 static struct d_comp
*
1392 c
= d_next_char (di
);
1395 switch (d_next_char (di
))
1398 return d_make_comp (di
, D_COMP_VTABLE
, d_type (di
), NULL
);
1400 return d_make_comp (di
, D_COMP_VTT
, d_type (di
), NULL
);
1402 return d_make_comp (di
, D_COMP_TYPEINFO
, d_type (di
), NULL
);
1404 return d_make_comp (di
, D_COMP_TYPEINFO_NAME
, d_type (di
), NULL
);
1407 if (! d_call_offset (di
, 'h'))
1409 return d_make_comp (di
, D_COMP_THUNK
, d_encoding (di
, 0), NULL
);
1412 if (! d_call_offset (di
, 'v'))
1414 return d_make_comp (di
, D_COMP_VIRTUAL_THUNK
, d_encoding (di
, 0),
1418 if (! d_call_offset (di
, '\0'))
1420 if (! d_call_offset (di
, '\0'))
1422 return d_make_comp (di
, D_COMP_COVARIANT_THUNK
, d_encoding (di
, 0),
1427 struct d_comp
*derived_type
;
1429 struct d_comp
*base_type
;
1431 derived_type
= d_type (di
);
1432 offset
= d_number (di
);
1435 if (d_next_char (di
) != '_')
1437 base_type
= d_type (di
);
1438 /* We don't display the offset. FIXME: We should display
1439 it in verbose mode. */
1440 return d_make_comp (di
, D_COMP_CONSTRUCTION_VTABLE
, base_type
,
1445 return d_make_comp (di
, D_COMP_TYPEINFO_FN
, d_type (di
), NULL
);
1447 return d_make_comp (di
, D_COMP_JAVA_CLASS
, d_type (di
), NULL
);
1455 switch (d_next_char (di
))
1458 return d_make_comp (di
, D_COMP_GUARD
, d_name (di
), NULL
);
1461 return d_make_comp (di
, D_COMP_REFTEMP
, d_name (di
), NULL
);
1471 /* <call-offset> ::= h <nv-offset> _
1474 <nv-offset> ::= <(offset) number>
1476 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1478 The C parameter, if not '\0', is a character we just read which is
1479 the start of the <call-offset>.
1481 We don't display the offset information anywhere. FIXME: We should
1482 display it in verbose mode. */
1485 d_call_offset (di
, c
)
1490 long virtual_offset
;
1493 c
= d_next_char (di
);
1496 offset
= d_number (di
);
1499 offset
= d_number (di
);
1500 if (d_next_char (di
) != '_')
1502 virtual_offset
= d_number (di
);
1507 if (d_next_char (di
) != '_')
1513 /* <ctor-dtor-name> ::= C1
1521 static struct d_comp
*
1522 d_ctor_dtor_name (di
)
1525 switch (d_next_char (di
))
1529 enum gnu_v3_ctor_kinds kind
;
1531 switch (d_next_char (di
))
1534 kind
= gnu_v3_complete_object_ctor
;
1537 kind
= gnu_v3_base_object_ctor
;
1540 kind
= gnu_v3_complete_object_allocating_ctor
;
1545 return d_make_ctor (di
, kind
, di
->last_name
);
1550 enum gnu_v3_dtor_kinds kind
;
1552 switch (d_next_char (di
))
1555 kind
= gnu_v3_deleting_dtor
;
1558 kind
= gnu_v3_complete_object_dtor
;
1561 kind
= gnu_v3_base_object_dtor
;
1566 return d_make_dtor (di
, kind
, di
->last_name
);
1574 /* <type> ::= <builtin-type>
1576 ::= <class-enum-type>
1578 ::= <pointer-to-member-type>
1579 ::= <template-param>
1580 ::= <template-template-param> <template-args>
1582 ::= <CV-qualifiers> <type>
1587 ::= U <source-name> <type>
1589 <builtin-type> ::= various one letter codes
1593 static const struct d_builtin_type_info d_builtin_types
[26] =
1595 /* a */ { "signed char", "signed char", D_PRINT_INT
},
1596 /* b */ { "bool", "boolean", D_PRINT_BOOL
},
1597 /* c */ { "char", "byte", D_PRINT_INT
},
1598 /* d */ { "double", "double", D_PRINT_DEFAULT
},
1599 /* e */ { "long double", "long double", D_PRINT_DEFAULT
},
1600 /* f */ { "float", "float", D_PRINT_DEFAULT
},
1601 /* g */ { "__float128", "__float128", D_PRINT_DEFAULT
},
1602 /* h */ { "unsigned char", "unsigned char", D_PRINT_INT
},
1603 /* i */ { "int", "int", D_PRINT_INT
},
1604 /* j */ { "unsigned int", "unsigned", D_PRINT_INT
},
1605 /* k */ { NULL
, NULL
, D_PRINT_DEFAULT
},
1606 /* l */ { "long", "long", D_PRINT_LONG
},
1607 /* m */ { "unsigned long", "unsigned long", D_PRINT_LONG
},
1608 /* n */ { "__int128", "__int128", D_PRINT_DEFAULT
},
1609 /* o */ { "unsigned __int128", "unsigned __int128", D_PRINT_DEFAULT
},
1610 /* p */ { NULL
, NULL
, D_PRINT_DEFAULT
},
1611 /* q */ { NULL
, NULL
, D_PRINT_DEFAULT
},
1612 /* r */ { NULL
, NULL
, D_PRINT_DEFAULT
},
1613 /* s */ { "short", "short", D_PRINT_INT
},
1614 /* t */ { "unsigned short", "unsigned short", D_PRINT_INT
},
1615 /* u */ { NULL
, NULL
, D_PRINT_DEFAULT
},
1616 /* v */ { "void", "void", D_PRINT_VOID
},
1617 /* w */ { "wchar_t", "char", D_PRINT_INT
},
1618 /* x */ { "long long", "long", D_PRINT_DEFAULT
},
1619 /* y */ { "unsigned long long", "unsigned long long", D_PRINT_DEFAULT
},
1620 /* z */ { "...", "...", D_PRINT_DEFAULT
},
1623 static struct d_comp
*
1631 /* The ABI specifies that when CV-qualifiers are used, the base type
1632 is substitutable, and the fully qualified type is substitutable,
1633 but the base type with a strict subset of the CV-qualifiers is
1634 not substitutable. The natural recursive implementation of the
1635 CV-qualifiers would cause subsets to be substitutable, so instead
1636 we pull them all off now.
1638 FIXME: The ABI says that order-insensitive vendor qualifiers
1639 should be handled in the same way, but we have no way to tell
1640 which vendor qualifiers are order-insensitive and which are
1641 order-sensitive. So we just assume that they are all
1642 order-sensitive. g++ 3.4 supports only one vendor qualifier,
1643 __vector, and it treats it as order-sensitive when mangling
1646 peek
= d_peek_char (di
);
1647 if (peek
== 'r' || peek
== 'V' || peek
== 'K')
1649 struct d_comp
**pret
;
1651 pret
= d_cv_qualifiers (di
, &ret
);
1654 *pret
= d_type (di
);
1655 if (! d_add_substitution (di
, ret
))
1664 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1665 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
1666 case 'o': case 's': case 't':
1667 case 'v': case 'w': case 'x': case 'y': case 'z':
1668 ret
= d_make_builtin_type (di
, &d_builtin_types
[peek
- 'a']);
1675 ret
= d_make_comp (di
, D_COMP_VENDOR_TYPE
, d_source_name (di
), NULL
);
1679 ret
= d_function_type (di
);
1682 case '0': case '1': case '2': case '3': case '4':
1683 case '5': case '6': case '7': case '8': case '9':
1686 ret
= d_class_enum_type (di
);
1690 ret
= d_array_type (di
);
1694 ret
= d_pointer_to_member_type (di
);
1698 ret
= d_template_param (di
);
1699 if (d_peek_char (di
) == 'I')
1701 /* This is <template-template-param> <template-args>. The
1702 <template-template-param> part is a substitution
1704 if (! d_add_substitution (di
, ret
))
1706 ret
= d_make_comp (di
, D_COMP_TEMPLATE
, ret
, d_template_args (di
));
1711 /* If this is a special substitution, then it is the start of
1712 <class-enum-type>. */
1716 peek_next
= d_peek_next_char (di
);
1717 if (IS_DIGIT (peek_next
)
1719 || (peek_next
>= 'A' && peek_next
<= 'Z'))
1721 ret
= d_substitution (di
);
1722 /* The substituted name may have been a template name and
1723 may be followed by tepmlate args. */
1724 if (d_peek_char (di
) == 'I')
1725 ret
= d_make_comp (di
, D_COMP_TEMPLATE
, ret
,
1726 d_template_args (di
));
1732 ret
= d_class_enum_type (di
);
1733 /* If the substitution was a complete type, then it is not
1734 a new substitution candidate. However, if the
1735 substitution was followed by template arguments, then
1736 the whole thing is a substitution candidate. */
1737 if (ret
!= NULL
&& ret
->type
== D_COMP_SUB_STD
)
1745 ret
= d_make_comp (di
, D_COMP_POINTER
, d_type (di
), NULL
);
1750 ret
= d_make_comp (di
, D_COMP_REFERENCE
, d_type (di
), NULL
);
1755 ret
= d_make_comp (di
, D_COMP_COMPLEX
, d_type (di
), NULL
);
1760 ret
= d_make_comp (di
, D_COMP_IMAGINARY
, d_type (di
), NULL
);
1765 ret
= d_source_name (di
);
1766 ret
= d_make_comp (di
, D_COMP_VENDOR_TYPE_QUAL
, d_type (di
), ret
);
1775 if (! d_add_substitution (di
, ret
))
1782 /* <CV-qualifiers> ::= [r] [V] [K] */
1784 static struct d_comp
**
1785 d_cv_qualifiers (di
, pret
)
1787 struct d_comp
**pret
;
1791 peek
= d_peek_char (di
);
1792 while (peek
== 'r' || peek
== 'V' || peek
== 'K')
1798 t
= D_COMP_RESTRICT
;
1799 else if (peek
== 'V')
1800 t
= D_COMP_VOLATILE
;
1804 *pret
= d_make_comp (di
, t
, NULL
, NULL
);
1807 pret
= &d_left (*pret
);
1809 peek
= d_peek_char (di
);
1815 /* <function-type> ::= F [Y] <bare-function-type> E */
1817 static struct d_comp
*
1818 d_function_type (di
)
1823 if (d_next_char (di
) != 'F')
1825 if (d_peek_char (di
) == 'Y')
1827 /* Function has C linkage. We don't print this information.
1828 FIXME: We should print it in verbose mode. */
1831 ret
= d_bare_function_type (di
, 1);
1832 if (d_next_char (di
) != 'E')
1837 /* <bare-function-type> ::= <type>+ */
1839 static struct d_comp
*
1840 d_bare_function_type (di
, has_return_type
)
1842 int has_return_type
;
1844 struct d_comp
*return_type
;
1846 struct d_comp
**ptl
;
1854 struct d_comp
*type
;
1856 peek
= d_peek_char (di
);
1857 if (peek
== '\0' || peek
== 'E')
1862 if (has_return_type
)
1865 has_return_type
= 0;
1869 *ptl
= d_make_comp (di
, D_COMP_ARGLIST
, type
, NULL
);
1872 ptl
= &d_right (*ptl
);
1876 /* There should be at least one parameter type besides the optional
1877 return type. A function which takes no arguments will have a
1878 single parameter type void. */
1882 /* If we have a single parameter type void, omit it. */
1883 if (d_right (tl
) == NULL
1884 && d_left (tl
)->type
== D_COMP_BUILTIN_TYPE
1885 && d_left (tl
)->u
.s_builtin
.type
->print
== D_PRINT_VOID
)
1888 return d_make_comp (di
, D_COMP_FUNCTION_TYPE
, return_type
, tl
);
1891 /* <class-enum-type> ::= <name> */
1893 static struct d_comp
*
1894 d_class_enum_type (di
)
1900 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
1901 ::= A [<(dimension) expression>] _ <(element) type>
1904 static struct d_comp
*
1911 if (d_next_char (di
) != 'A')
1914 peek
= d_peek_char (di
);
1917 else if (IS_DIGIT (peek
))
1925 peek
= d_peek_char (di
);
1927 while (IS_DIGIT (peek
));
1928 dim
= d_make_name (di
, s
, d_str (di
) - s
);
1934 dim
= d_expression (di
);
1939 if (d_next_char (di
) != '_')
1942 return d_make_comp (di
, D_COMP_ARRAY_TYPE
, dim
, d_type (di
));
1945 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
1947 static struct d_comp
*
1948 d_pointer_to_member_type (di
)
1953 struct d_comp
**pmem
;
1955 if (d_next_char (di
) != 'M')
1960 /* The ABI specifies that any type can be a substitution source, and
1961 that M is followed by two types, and that when a CV-qualified
1962 type is seen both the base type and the CV-qualified types are
1963 substitution sources. The ABI also specifies that for a pointer
1964 to a CV-qualified member function, the qualifiers are attached to
1965 the second type. Given the grammar, a plain reading of the ABI
1966 suggests that both the CV-qualified member function and the
1967 non-qualified member function are substitution sources. However,
1968 g++ does not work that way. g++ treats only the CV-qualified
1969 member function as a substitution source. FIXME. So to work
1970 with g++, we need to pull off the CV-qualifiers here, in order to
1971 avoid calling add_substitution() in d_type(). */
1973 pmem
= d_cv_qualifiers (di
, &mem
);
1976 *pmem
= d_type (di
);
1978 return d_make_comp (di
, D_COMP_PTRMEM_TYPE
, cl
, mem
);
1981 /* <template-param> ::= T_
1982 ::= T <(parameter-2 non-negative) number> _
1985 static struct d_comp
*
1986 d_template_param (di
)
1991 if (d_next_char (di
) != 'T')
1994 if (d_peek_char (di
) == '_')
1998 param
= d_number (di
);
2004 if (d_next_char (di
) != '_')
2007 return d_make_template_param (di
, param
);
2010 /* <template-args> ::= I <template-arg>+ E */
2012 static struct d_comp
*
2013 d_template_args (di
)
2016 struct d_comp
*hold_last_name
;
2018 struct d_comp
**pal
;
2020 /* Preserve the last name we saw--don't let the template arguments
2021 clobber it, as that would give us the wrong name for a subsequent
2022 constructor or destructor. */
2023 hold_last_name
= di
->last_name
;
2025 if (d_next_char (di
) != 'I')
2034 a
= d_template_arg (di
);
2038 *pal
= d_make_comp (di
, D_COMP_TEMPLATE_ARGLIST
, a
, NULL
);
2041 pal
= &d_right (*pal
);
2043 if (d_peek_char (di
) == 'E')
2050 di
->last_name
= hold_last_name
;
2055 /* <template-arg> ::= <type>
2056 ::= X <expression> E
2060 static struct d_comp
*
2066 switch (d_peek_char (di
))
2070 ret
= d_expression (di
);
2071 if (d_next_char (di
) != 'E')
2076 return d_expr_primary (di
);
2083 /* <expression> ::= <(unary) operator-name> <expression>
2084 ::= <(binary) operator-name> <expression> <expression>
2085 ::= <(trinary) operator-name> <expression> <expression> <expression>
2087 ::= <template-param>
2088 ::= sr <type> <unqualified-name>
2089 ::= sr <type> <unqualified-name> <template-args>
2093 static struct d_comp
*
2099 peek
= d_peek_char (di
);
2101 return d_expr_primary (di
);
2102 else if (peek
== 'T')
2103 return d_template_param (di
);
2104 else if (peek
== 's' && d_peek_next_char (di
) == 'r')
2106 struct d_comp
*type
;
2107 struct d_comp
*name
;
2111 name
= d_unqualified_name (di
);
2112 if (d_peek_char (di
) != 'I')
2113 return d_make_comp (di
, D_COMP_QUAL_NAME
, type
, name
);
2115 return d_make_comp (di
, D_COMP_QUAL_NAME
, type
,
2116 d_make_comp (di
, D_COMP_TEMPLATE
, name
,
2117 d_template_args (di
)));
2124 op
= d_operator_name (di
);
2128 if (op
->type
== D_COMP_OPERATOR
2129 && strcmp (op
->u
.s_operator
.op
->code
, "st") == 0)
2130 return d_make_comp (di
, D_COMP_UNARY
, op
, d_type (di
));
2136 case D_COMP_OPERATOR
:
2137 args
= op
->u
.s_operator
.op
->args
;
2139 case D_COMP_EXTENDED_OPERATOR
:
2140 args
= op
->u
.s_extended_operator
.args
;
2150 return d_make_comp (di
, D_COMP_UNARY
, op
, d_expression (di
));
2153 struct d_comp
*left
;
2155 left
= d_expression (di
);
2156 return d_make_comp (di
, D_COMP_BINARY
, op
,
2157 d_make_comp (di
, D_COMP_BINARY_ARGS
, left
,
2158 d_expression (di
)));
2162 struct d_comp
*first
;
2163 struct d_comp
*second
;
2165 first
= d_expression (di
);
2166 second
= d_expression (di
);
2167 return d_make_comp (di
, D_COMP_TRINARY
, op
,
2168 d_make_comp (di
, D_COMP_TRINARY_ARG1
, first
,
2170 D_COMP_TRINARY_ARG2
,
2172 d_expression (di
))));
2180 /* <expr-primary> ::= L <type> <(value) number> E
2181 ::= L <type> <(value) float> E
2182 ::= L <mangled-name> E
2185 static struct d_comp
*
2191 if (d_next_char (di
) != 'L')
2193 if (d_peek_char (di
) == '_')
2194 ret
= d_mangled_name (di
, 0);
2197 struct d_comp
*type
;
2202 /* Rather than try to interpret the literal value, we just
2203 collect it as a string. Note that it's possible to have a
2204 floating point literal here. The ABI specifies that the
2205 format of such literals is machine independent. That's fine,
2206 but what's not fine is that versions of g++ up to 3.2 with
2207 -fabi-version=1 used upper case letters in the hex constant,
2208 and dumped out gcc's internal representation. That makes it
2209 hard to tell where the constant ends, and hard to dump the
2210 constant in any readable form anyhow. We don't attempt to
2211 handle these cases. */
2214 while (d_peek_char (di
) != 'E')
2216 ret
= d_make_comp (di
, D_COMP_LITERAL
, type
,
2217 d_make_name (di
, s
, d_str (di
) - s
));
2219 if (d_next_char (di
) != 'E')
2224 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2225 ::= Z <(function) encoding> E s [<discriminator>]
2228 static struct d_comp
*
2232 struct d_comp
*function
;
2234 if (d_next_char (di
) != 'Z')
2237 function
= d_encoding (di
, 0);
2239 if (d_next_char (di
) != 'E')
2242 if (d_peek_char (di
) == 's')
2245 if (! d_discriminator (di
))
2247 return d_make_comp (di
, D_COMP_QUAL_NAME
, function
,
2248 d_make_name (di
, "string literal",
2249 sizeof "string literal" - 1));
2253 struct d_comp
*name
;
2256 if (! d_discriminator (di
))
2258 return d_make_comp (di
, D_COMP_QUAL_NAME
, function
, name
);
2262 /* <discriminator> ::= _ <(non-negative) number>
2264 We demangle the discriminator, but we don't print it out. FIXME:
2265 We should print it out in verbose mode. */
2268 d_discriminator (di
)
2273 if (d_peek_char (di
) != '_')
2276 discrim
= d_number (di
);
2282 /* Add a new substitution. */
2285 d_add_substitution (di
, dc
)
2291 if (di
->next_sub
>= di
->num_subs
)
2293 di
->subs
[di
->next_sub
] = dc
;
2298 /* <substitution> ::= S <seq-id> _
2309 static struct d_comp
*
2315 if (d_next_char (di
) != 'S')
2318 c
= d_next_char (di
);
2319 if (c
== '_' || IS_DIGIT (c
) || (c
>= 'A' && c
<= 'Z'))
2329 id
= id
* 36 + c
- '0';
2330 else if (c
>= 'A' && c
<= 'Z')
2331 id
= id
* 36 + c
- 'A' + 10;
2334 c
= d_next_char (di
);
2341 if (id
>= di
->next_sub
)
2344 return di
->subs
[id
];
2351 return d_make_sub (di
, "std");
2353 di
->last_name
= d_make_sub (di
, "allocator");
2354 return d_make_sub (di
, "std::allocator");
2356 di
->last_name
= d_make_sub (di
, "basic_string");
2357 return d_make_sub (di
, "std::basic_string");
2359 di
->last_name
= d_make_sub (di
, "string");
2360 return d_make_sub (di
, "std::string");
2362 di
->last_name
= d_make_sub (di
, "istream");
2363 return d_make_sub (di
, "std::istream");
2365 di
->last_name
= d_make_sub (di
, "ostream");
2366 return d_make_sub (di
, "std::ostream");
2368 di
->last_name
= d_make_sub (di
, "iostream");
2369 return d_make_sub (di
, "std::iostream");
2376 /* Resize the print buffer. */
2379 d_print_resize (dpi
, add
)
2380 struct d_print_info
*dpi
;
2385 if (dpi
->buf
== NULL
)
2387 need
= dpi
->len
+ add
;
2388 while (need
> dpi
->alc
)
2393 newalc
= dpi
->alc
* 2;
2394 newbuf
= realloc (dpi
->buf
, newalc
);
2399 dpi
->allocation_failure
= 1;
2407 /* Append a character to the print buffer. */
2410 d_print_append_char (dpi
, c
)
2411 struct d_print_info
*dpi
;
2414 if (dpi
->buf
!= NULL
)
2416 if (dpi
->len
>= dpi
->alc
)
2418 d_print_resize (dpi
, 1);
2419 if (dpi
->buf
== NULL
)
2423 dpi
->buf
[dpi
->len
] = c
;
2428 /* Append a buffer to the print buffer. */
2431 d_print_append_buffer (dpi
, s
, l
)
2432 struct d_print_info
*dpi
;
2436 if (dpi
->buf
!= NULL
)
2438 if (dpi
->len
+ l
> dpi
->alc
)
2440 d_print_resize (dpi
, l
);
2441 if (dpi
->buf
== NULL
)
2445 memcpy (dpi
->buf
+ dpi
->len
, s
, l
);
2450 /* Indicate that an error occurred during printing. */
2454 struct d_print_info
*dpi
;
2460 /* Turn components into a human readable string. Returns a string
2461 allocated by malloc, or NULL on error. On success, this sets *PALC
2462 to the size of the allocated buffer. On failure, this sets *PALC
2463 to 0 for a bad parse, or to 1 for a memory allocation failure. */
2466 d_print (options
, dc
, palc
)
2468 const struct d_comp
*dc
;
2471 struct d_print_info dpi
;
2473 dpi
.options
= options
;
2476 dpi
.buf
= malloc (dpi
.alc
);
2477 if (dpi
.buf
== NULL
)
2484 dpi
.templates
= NULL
;
2485 dpi
.modifiers
= NULL
;
2487 dpi
.allocation_failure
= 0;
2489 d_print_comp (&dpi
, dc
);
2491 d_append_char (&dpi
, '\0');
2493 if (dpi
.buf
!= NULL
)
2496 *palc
= dpi
.allocation_failure
;
2501 /* Subroutine to handle components. */
2504 d_print_comp (dpi
, dc
)
2505 struct d_print_info
*dpi
;
2506 const struct d_comp
*dc
;
2510 d_print_error (dpi
);
2513 if (d_print_saw_error (dpi
))
2519 d_print_identifier (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
2522 case D_COMP_QUAL_NAME
:
2523 d_print_comp (dpi
, d_left (dc
));
2524 d_append_string (dpi
, (dpi
->options
& DMGL_JAVA
) == 0 ? "::" : ".");
2525 d_print_comp (dpi
, d_right (dc
));
2528 case D_COMP_TYPED_NAME
:
2530 const struct d_comp
*typed_name
;
2531 struct d_print_mod dpm
;
2532 struct d_print_template dpt
;
2534 /* Pass the name down to the type so that it can be printed in
2535 the right place for the type. If the name has
2536 CV-qualifiers, they are really method qualifiers; pull them
2537 off now and print them after everything else. Note that we
2538 don't handle D_COMP_VENDOR_TYPE_QUAL here; it's not
2539 accepted by d_cv_qualifiers() either. */
2540 typed_name
= d_left (dc
);
2541 while (typed_name
!= NULL
2542 && (typed_name
->type
== D_COMP_RESTRICT
2543 || typed_name
->type
== D_COMP_VOLATILE
2544 || typed_name
->type
== D_COMP_CONST
))
2545 typed_name
= d_left (typed_name
);
2547 dpm
.next
= dpi
->modifiers
;
2548 dpi
->modifiers
= &dpm
;
2549 dpm
.mod
= typed_name
;
2551 dpm
.templates
= dpi
->templates
;
2553 /* If typed_name is a template, then it applies to the
2554 function type as well. */
2555 if (typed_name
->type
== D_COMP_TEMPLATE
)
2557 dpt
.next
= dpi
->templates
;
2558 dpi
->templates
= &dpt
;
2559 dpt
.template = typed_name
;
2562 d_print_comp (dpi
, d_right (dc
));
2564 if (typed_name
->type
== D_COMP_TEMPLATE
)
2565 dpi
->templates
= dpt
.next
;
2567 /* If the modifier didn't get printed by the type, print it
2571 d_append_char (dpi
, ' ');
2572 d_print_comp (dpi
, typed_name
);
2575 dpi
->modifiers
= dpm
.next
;
2577 /* Now print any CV-qualifiers on the type. */
2578 typed_name
= d_left (dc
);
2579 while (typed_name
!= NULL
2580 && (typed_name
->type
== D_COMP_RESTRICT
2581 || typed_name
->type
== D_COMP_VOLATILE
2582 || typed_name
->type
== D_COMP_CONST
))
2584 d_print_mod (dpi
, typed_name
);
2585 typed_name
= d_left (typed_name
);
2591 case D_COMP_TEMPLATE
:
2593 struct d_print_mod
*hold_dpm
;
2595 /* Don't push modifiers into a template definition. Doing so
2596 could give the wrong definition for a template argument.
2597 Instead, treat the template essentially as a name. */
2599 hold_dpm
= dpi
->modifiers
;
2600 dpi
->modifiers
= NULL
;
2602 d_print_comp (dpi
, d_left (dc
));
2603 d_append_char (dpi
, '<');
2604 d_print_comp (dpi
, d_right (dc
));
2605 /* Avoid generating two consecutive '>' characters, to avoid
2606 the C++ syntactic ambiguity. */
2607 if (dpi
->buf
!= NULL
&& dpi
->buf
[dpi
->len
- 1] == '>')
2608 d_append_char (dpi
, ' ');
2609 d_append_char (dpi
, '>');
2611 dpi
->modifiers
= hold_dpm
;
2616 case D_COMP_TEMPLATE_PARAM
:
2620 struct d_print_template
*hold_dpt
;
2622 if (dpi
->templates
== NULL
)
2624 d_print_error (dpi
);
2627 i
= dc
->u
.s_number
.number
;
2628 for (a
= d_right (dpi
->templates
->template);
2632 if (a
->type
!= D_COMP_TEMPLATE_ARGLIST
)
2634 d_print_error (dpi
);
2641 if (i
!= 0 || a
== NULL
)
2643 d_print_error (dpi
);
2647 /* While processing this parameter, we need to pop the list of
2648 templates. This is because the template parameter may
2649 itself be a reference to a parameter of an outer
2652 hold_dpt
= dpi
->templates
;
2653 dpi
->templates
= hold_dpt
->next
;
2655 d_print_comp (dpi
, d_left (a
));
2657 dpi
->templates
= hold_dpt
;
2663 d_print_comp (dpi
, dc
->u
.s_ctor
.name
);
2667 d_append_char (dpi
, '~');
2668 d_print_comp (dpi
, dc
->u
.s_dtor
.name
);
2672 d_append_string (dpi
, "vtable for ");
2673 d_print_comp (dpi
, d_left (dc
));
2677 d_append_string (dpi
, "VTT for ");
2678 d_print_comp (dpi
, d_left (dc
));
2681 case D_COMP_CONSTRUCTION_VTABLE
:
2682 d_append_string (dpi
, "construction vtable for ");
2683 d_print_comp (dpi
, d_left (dc
));
2684 d_append_string (dpi
, "-in-");
2685 d_print_comp (dpi
, d_right (dc
));
2688 case D_COMP_TYPEINFO
:
2689 d_append_string (dpi
, "typeinfo for ");
2690 d_print_comp (dpi
, d_left (dc
));
2693 case D_COMP_TYPEINFO_NAME
:
2694 d_append_string (dpi
, "typeinfo name for ");
2695 d_print_comp (dpi
, d_left (dc
));
2698 case D_COMP_TYPEINFO_FN
:
2699 d_append_string (dpi
, "typeinfo fn for ");
2700 d_print_comp (dpi
, d_left (dc
));
2704 d_append_string (dpi
, "non-virtual thunk to ");
2705 d_print_comp (dpi
, d_left (dc
));
2708 case D_COMP_VIRTUAL_THUNK
:
2709 d_append_string (dpi
, "virtual thunk to ");
2710 d_print_comp (dpi
, d_left (dc
));
2713 case D_COMP_COVARIANT_THUNK
:
2714 d_append_string (dpi
, "covariant return thunk to ");
2715 d_print_comp (dpi
, d_left (dc
));
2718 case D_COMP_JAVA_CLASS
:
2719 d_append_string (dpi
, "java Class for ");
2720 d_print_comp (dpi
, d_left (dc
));
2724 d_append_string (dpi
, "guard variable for ");
2725 d_print_comp (dpi
, d_left (dc
));
2728 case D_COMP_REFTEMP
:
2729 d_append_string (dpi
, "reference temporary for ");
2730 d_print_comp (dpi
, d_left (dc
));
2733 case D_COMP_SUB_STD
:
2734 d_append_string (dpi
, dc
->u
.s_string
.string
);
2737 case D_COMP_RESTRICT
:
2738 case D_COMP_VOLATILE
:
2740 case D_COMP_VENDOR_TYPE_QUAL
:
2741 case D_COMP_POINTER
:
2742 case D_COMP_REFERENCE
:
2743 case D_COMP_COMPLEX
:
2744 case D_COMP_IMAGINARY
:
2746 /* We keep a list of modifiers on the stack. */
2747 struct d_print_mod dpm
;
2749 dpm
.next
= dpi
->modifiers
;
2750 dpi
->modifiers
= &dpm
;
2753 dpm
.templates
= dpi
->templates
;
2755 d_print_comp (dpi
, d_left (dc
));
2757 /* If the modifier didn't get printed by the type, print it
2760 d_print_mod (dpi
, dc
);
2762 dpi
->modifiers
= dpm
.next
;
2767 case D_COMP_BUILTIN_TYPE
:
2768 if ((dpi
->options
& DMGL_JAVA
) == 0)
2769 d_append_string (dpi
, dc
->u
.s_builtin
.type
->name
);
2771 d_append_string (dpi
, dc
->u
.s_builtin
.type
->java_name
);
2774 case D_COMP_VENDOR_TYPE
:
2775 d_print_comp (dpi
, d_left (dc
));
2778 case D_COMP_FUNCTION_TYPE
:
2780 if (d_left (dc
) != NULL
)
2782 struct d_print_mod dpm
;
2784 /* We must pass this type down as a modifier in order to
2785 print it in the right location. */
2787 dpm
.next
= dpi
->modifiers
;
2788 dpi
->modifiers
= &dpm
;
2791 dpm
.templates
= dpi
->templates
;
2793 d_print_comp (dpi
, d_left (dc
));
2795 dpi
->modifiers
= dpm
.next
;
2800 d_append_char (dpi
, ' ');
2803 d_print_function_type (dpi
, dc
, dpi
->modifiers
);
2808 case D_COMP_ARRAY_TYPE
:
2810 struct d_print_mod dpm
;
2812 /* We must pass this type down as a modifier in order to print
2813 multi-dimensional arrays correctly. */
2815 dpm
.next
= dpi
->modifiers
;
2816 dpi
->modifiers
= &dpm
;
2819 dpm
.templates
= dpi
->templates
;
2821 d_print_comp (dpi
, d_right (dc
));
2823 dpi
->modifiers
= dpm
.next
;
2828 d_print_array_type (dpi
, dc
, dpi
->modifiers
);
2833 case D_COMP_PTRMEM_TYPE
:
2835 const struct d_comp
*target_type
;
2836 struct d_print_mod dpm
;
2838 /* Pass the name down to the type so that it can be printed in
2839 the right place for the type. If the type has
2840 CV-qualifiers, they are really method qualifiers; pull them
2841 off now and print them after everything else. */
2842 target_type
= d_right (dc
);
2843 while (target_type
!= NULL
2844 && (target_type
->type
== D_COMP_RESTRICT
2845 || target_type
->type
== D_COMP_VOLATILE
2846 || target_type
->type
== D_COMP_CONST
))
2847 target_type
= d_left (target_type
);
2849 dpm
.next
= dpi
->modifiers
;
2850 dpi
->modifiers
= &dpm
;
2853 dpm
.templates
= dpi
->templates
;
2855 d_print_comp (dpi
, target_type
);
2857 /* If the modifier didn't get printed by the type, print it
2861 d_append_char (dpi
, ' ');
2862 d_print_comp (dpi
, d_left (dc
));
2863 d_append_string (dpi
, "::*");
2866 dpi
->modifiers
= dpm
.next
;
2868 /* Now print any CV-qualifiers on the type. */
2869 target_type
= d_right (dc
);
2870 while (target_type
!= NULL
2871 && (target_type
->type
== D_COMP_RESTRICT
2872 || target_type
->type
== D_COMP_VOLATILE
2873 || target_type
->type
== D_COMP_CONST
))
2875 d_print_mod (dpi
, target_type
);
2876 target_type
= d_left (target_type
);
2882 case D_COMP_ARGLIST
:
2883 case D_COMP_TEMPLATE_ARGLIST
:
2884 d_print_comp (dpi
, d_left (dc
));
2885 if (d_right (dc
) != NULL
)
2887 d_append_string (dpi
, ", ");
2888 d_print_comp (dpi
, d_right (dc
));
2892 case D_COMP_OPERATOR
:
2896 d_append_string (dpi
, "operator");
2897 c
= dc
->u
.s_operator
.op
->name
[0];
2898 if (c
>= 'a' && c
<= 'z')
2899 d_append_char (dpi
, ' ');
2900 d_append_string (dpi
, dc
->u
.s_operator
.op
->name
);
2904 case D_COMP_EXTENDED_OPERATOR
:
2905 d_append_string (dpi
, "operator ");
2906 d_print_comp (dpi
, dc
->u
.s_extended_operator
.name
);
2910 d_append_string (dpi
, "operator ");
2911 d_print_cast (dpi
, dc
);
2915 if (d_left (dc
)->type
!= D_COMP_CAST
)
2916 d_print_expr_op (dpi
, d_left (dc
));
2919 d_append_string (dpi
, "((");
2920 d_print_cast (dpi
, d_left (dc
));
2921 d_append_char (dpi
, ')');
2923 d_append_char (dpi
, '(');
2924 d_print_comp (dpi
, d_right (dc
));
2925 d_append_char (dpi
, ')');
2926 if (d_left (dc
)->type
== D_COMP_CAST
)
2927 d_append_char (dpi
, ')');
2931 if (d_right (dc
)->type
!= D_COMP_BINARY_ARGS
)
2933 d_print_error (dpi
);
2936 d_append_char (dpi
, '(');
2937 d_print_comp (dpi
, d_left (d_right (dc
)));
2938 d_append_string (dpi
, ") ");
2939 d_print_expr_op (dpi
, d_left (dc
));
2940 d_append_string (dpi
, " (");
2941 d_print_comp (dpi
, d_right (d_right (dc
)));
2942 d_append_char (dpi
, ')');
2945 case D_COMP_BINARY_ARGS
:
2946 /* We should only see this as part of D_COMP_BINARY. */
2947 d_print_error (dpi
);
2950 case D_COMP_TRINARY
:
2951 if (d_right (dc
)->type
!= D_COMP_TRINARY_ARG1
2952 || d_right (d_right (dc
))->type
!= D_COMP_TRINARY_ARG2
)
2954 d_print_error (dpi
);
2957 d_append_char (dpi
, '(');
2958 d_print_comp (dpi
, d_left (d_right (dc
)));
2959 d_append_string (dpi
, ") ");
2960 d_print_expr_op (dpi
, d_left (dc
));
2961 d_append_string (dpi
, " (");
2962 d_print_comp (dpi
, d_left (d_right (d_right (dc
))));
2963 d_append_string (dpi
, ") : (");
2964 d_print_comp (dpi
, d_right (d_right (d_right (dc
))));
2965 d_append_char (dpi
, ')');
2968 case D_COMP_TRINARY_ARG1
:
2969 case D_COMP_TRINARY_ARG2
:
2970 /* We should only see these are part of D_COMP_TRINARY. */
2971 d_print_error (dpi
);
2974 case D_COMP_LITERAL
:
2975 /* For some builtin types, produce simpler output. */
2976 if (d_left (dc
)->type
== D_COMP_BUILTIN_TYPE
)
2978 switch (d_left (dc
)->u
.s_builtin
.type
->print
)
2981 if (d_right (dc
)->type
== D_COMP_NAME
)
2983 d_print_comp (dpi
, d_right (dc
));
2989 if (d_right (dc
)->type
== D_COMP_NAME
)
2991 d_print_comp (dpi
, d_right (dc
));
2992 d_append_char (dpi
, 'l');
2998 if (d_right (dc
)->type
== D_COMP_NAME
2999 && d_right (dc
)->u
.s_name
.len
== 1)
3001 switch (d_right (dc
)->u
.s_name
.s
[0])
3004 d_append_string (dpi
, "false");
3007 d_append_string (dpi
, "true");
3020 d_append_char (dpi
, '(');
3021 d_print_comp (dpi
, d_left (dc
));
3022 d_append_char (dpi
, ')');
3023 d_print_comp (dpi
, d_right (dc
));
3027 d_print_error (dpi
);
3032 /* Print an identifier. */
3035 d_print_identifier (dpi
, name
, len
)
3036 struct d_print_info
*dpi
;
3040 if ((dpi
->options
& DMGL_JAVA
) == 0)
3041 d_append_buffer (dpi
, name
, len
);
3047 /* For Java we try to handle encoded extended Unicode
3048 characters. The C++ ABI doesn't mention Unicode encoding, so
3049 we don't it for C++. Characters are encoded as
3052 for (p
= name
; p
< end
; ++p
)
3063 for (q
= p
+ 3; q
< end
; ++q
)
3067 if (*q
>= '0' && *q
<= '9')
3069 else if (*q
>= 'A' && *q
<= 'F')
3070 dig
= *q
- 'A' + 10;
3071 else if (*q
>= 'a' && *q
<= 'f')
3072 dig
= *q
- 'a' + 10;
3078 /* If the Unicode character is larger than 256, we don't
3079 try to deal with it here. FIXME. */
3080 if (q
< end
&& *q
== '_' && c
< 256)
3082 d_append_char (dpi
, c
);
3088 d_append_char (dpi
, *p
);
3093 /* Print a list of modifiers. */
3096 d_print_mod_list (dpi
, mods
)
3097 struct d_print_info
*dpi
;
3098 struct d_print_mod
*mods
;
3100 struct d_print_template
*hold_dpt
;
3102 if (mods
== NULL
|| mods
->printed
|| d_print_saw_error (dpi
))
3107 hold_dpt
= dpi
->templates
;
3108 dpi
->templates
= mods
->templates
;
3110 if (mods
->mod
->type
== D_COMP_FUNCTION_TYPE
)
3112 d_print_function_type (dpi
, mods
->mod
, mods
->next
);
3113 dpi
->templates
= hold_dpt
;
3116 else if (mods
->mod
->type
== D_COMP_ARRAY_TYPE
)
3118 d_print_array_type (dpi
, mods
->mod
, mods
->next
);
3119 dpi
->templates
= hold_dpt
;
3123 d_print_mod (dpi
, mods
->mod
);
3125 dpi
->templates
= hold_dpt
;
3127 d_print_mod_list (dpi
, mods
->next
);
3130 /* Print a modifier. */
3133 d_print_mod (dpi
, mod
)
3134 struct d_print_info
*dpi
;
3135 const struct d_comp
*mod
;
3139 case D_COMP_RESTRICT
:
3140 d_append_string (dpi
, " restrict");
3142 case D_COMP_VOLATILE
:
3143 d_append_string (dpi
, " volatile");
3146 d_append_string (dpi
, " const");
3148 case D_COMP_VENDOR_TYPE_QUAL
:
3149 d_append_char (dpi
, ' ');
3150 d_print_comp (dpi
, d_right (mod
));
3152 case D_COMP_POINTER
:
3153 /* There is no pointer symbol in Java. */
3154 if ((dpi
->options
& DMGL_JAVA
) == 0)
3155 d_append_char (dpi
, '*');
3157 case D_COMP_REFERENCE
:
3158 d_append_char (dpi
, '&');
3160 case D_COMP_COMPLEX
:
3161 d_append_string (dpi
, "complex ");
3163 case D_COMP_IMAGINARY
:
3164 d_append_string (dpi
, "imaginary ");
3166 case D_COMP_PTRMEM_TYPE
:
3167 if (dpi
->buf
!= NULL
&& dpi
->buf
[dpi
->len
- 1] != '(')
3168 d_append_char (dpi
, ' ');
3169 d_print_comp (dpi
, d_left (mod
));
3170 d_append_string (dpi
, "::*");
3172 case D_COMP_TYPED_NAME
:
3173 d_print_comp (dpi
, d_left (mod
));
3176 /* Otherwise, we have something that won't go back on the
3177 modifier stack, so we can just print it. */
3178 d_print_comp (dpi
, mod
);
3183 /* Print a function type, except for the return type. */
3186 d_print_function_type (dpi
, dc
, mods
)
3187 struct d_print_info
*dpi
;
3188 const struct d_comp
*dc
;
3189 struct d_print_mod
*mods
;
3193 struct d_print_mod
*p
;
3197 for (p
= mods
; p
!= NULL
; p
= p
->next
)
3203 switch (p
->mod
->type
)
3205 case D_COMP_RESTRICT
:
3206 case D_COMP_VOLATILE
:
3208 case D_COMP_VENDOR_TYPE_QUAL
:
3209 case D_COMP_POINTER
:
3210 case D_COMP_REFERENCE
:
3211 case D_COMP_COMPLEX
:
3212 case D_COMP_IMAGINARY
:
3213 case D_COMP_PTRMEM_TYPE
:
3223 if (d_left (dc
) != NULL
&& ! saw_mod
)
3227 d_append_char (dpi
, '(');
3229 d_print_mod_list (dpi
, mods
);
3232 d_append_char (dpi
, ')');
3234 d_append_char (dpi
, '(');
3236 if (d_right (dc
) != NULL
)
3237 d_print_comp (dpi
, d_right (dc
));
3239 d_append_char (dpi
, ')');
3242 /* Print an array type, except for the element type. */
3245 d_print_array_type (dpi
, dc
, mods
)
3246 struct d_print_info
*dpi
;
3247 const struct d_comp
*dc
;
3248 struct d_print_mod
*mods
;
3256 struct d_print_mod
*p
;
3259 for (p
= mods
; p
!= NULL
; p
= p
->next
)
3264 if (p
->mod
->type
== D_COMP_ARRAY_TYPE
)
3278 d_append_string (dpi
, " (");
3280 d_print_mod_list (dpi
, mods
);
3283 d_append_char (dpi
, ')');
3287 d_append_char (dpi
, ' ');
3289 d_append_char (dpi
, '[');
3291 if (d_left (dc
) != NULL
)
3292 d_print_comp (dpi
, d_left (dc
));
3294 d_append_char (dpi
, ']');
3297 /* Print an operator in an expression. */
3300 d_print_expr_op (dpi
, dc
)
3301 struct d_print_info
*dpi
;
3302 const struct d_comp
*dc
;
3304 if (dc
->type
== D_COMP_OPERATOR
)
3305 d_append_string (dpi
, dc
->u
.s_operator
.op
->name
);
3307 d_print_comp (dpi
, dc
);
3313 d_print_cast (dpi
, dc
)
3314 struct d_print_info
*dpi
;
3315 const struct d_comp
*dc
;
3317 if (d_left (dc
)->type
!= D_COMP_TEMPLATE
)
3318 d_print_comp (dpi
, d_left (dc
));
3321 struct d_print_mod
*hold_dpm
;
3322 struct d_print_template dpt
;
3324 /* It appears that for a templated cast operator, we need to put
3325 the template parameters in scope for the operator name, but
3326 not for the parameters. The effect is that we need to handle
3327 the template printing here. */
3329 hold_dpm
= dpi
->modifiers
;
3330 dpi
->modifiers
= NULL
;
3332 dpt
.next
= dpi
->templates
;
3333 dpi
->templates
= &dpt
;
3334 dpt
.template = d_left (dc
);
3336 d_print_comp (dpi
, d_left (d_left (dc
)));
3338 dpi
->templates
= dpt
.next
;
3340 d_append_char (dpi
, '<');
3341 d_print_comp (dpi
, d_right (d_left (dc
)));
3342 /* Avoid generating two consecutive '>' characters, to avoid
3343 the C++ syntactic ambiguity. */
3344 if (dpi
->buf
!= NULL
&& dpi
->buf
[dpi
->len
- 1] == '>')
3345 d_append_char (dpi
, ' ');
3346 d_append_char (dpi
, '>');
3348 dpi
->modifiers
= hold_dpm
;
3352 /* Initialize the information structure we use to pass around
3356 d_init_info (mangled
, options
, len
, di
)
3357 const char *mangled
;
3363 di
->options
= options
;
3367 /* We can not need more components than twice the number of chars in
3368 the mangled string. Most components correspond directly to
3369 chars, but the ARGLIST types are exceptions. */
3370 di
->num_comps
= 2 * len
;
3371 di
->comps
= (struct d_comp
*) malloc (di
->num_comps
3372 * sizeof (struct d_comp
));
3375 /* Similarly, we can not need more substitutions than there are
3376 chars in the mangled string. */
3378 di
->subs
= (struct d_comp
**) malloc (di
->num_subs
3379 * sizeof (struct d_comp
*));
3382 di
->last_name
= NULL
;
3384 if (di
->comps
== NULL
|| di
->subs
== NULL
)
3386 if (di
->comps
!= NULL
)
3388 if (di
->subs
!= NULL
)
3396 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
3397 name, return a buffer allocated with malloc holding the demangled
3398 name. OPTIONS is the usual libiberty demangler options. On
3399 success, this sets *PALC to the allocated size of the returned
3400 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
3401 a memory allocation failure. On failure, this returns NULL. */
3404 d_demangle (mangled
, options
, palc
)
3405 const char* mangled
;
3417 len
= strlen (mangled
);
3419 if (mangled
[0] == '_' && mangled
[1] == 'Z')
3421 else if (strncmp (mangled
, "_GLOBAL_", 8) == 0
3422 && (mangled
[8] == '.' || mangled
[8] == '_' || mangled
[8] == '$')
3423 && (mangled
[9] == 'D' || mangled
[9] == 'I')
3424 && mangled
[10] == '_')
3428 r
= malloc (40 + len
- 11);
3433 if (mangled
[9] == 'I')
3434 strcpy (r
, "global constructors keyed to ");
3436 strcpy (r
, "global destructors keyed to ");
3437 strcat (r
, mangled
+ 11);
3443 if ((options
& DMGL_TYPES
) == 0)
3448 if (! d_init_info (mangled
, options
, len
, &di
))
3455 dc
= d_mangled_name (&di
, 1);
3459 /* If we didn't consume the entire mangled string, then we didn't
3460 successfully demangle it. */
3461 if (d_peek_char (&di
) != '\0')
3464 #ifdef CP_DEMANGLE_DEBUG
3466 printf ("failed demangling\n");
3476 ret
= d_print (options
, dc
, palc
);
3483 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
3485 extern char *__cxa_demangle
PARAMS ((const char *, char *, size_t *, int *));
3487 /* ia64 ABI-mandated entry point in the C++ runtime library for
3488 performing demangling. MANGLED_NAME is a NUL-terminated character
3489 string containing the name to be demangled.
3491 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
3492 *LENGTH bytes, into which the demangled name is stored. If
3493 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
3494 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
3495 is placed in a region of memory allocated with malloc.
3497 If LENGTH is non-NULL, the length of the buffer conaining the
3498 demangled name, is placed in *LENGTH.
3500 The return value is a pointer to the start of the NUL-terminated
3501 demangled name, or NULL if the demangling fails. The caller is
3502 responsible for deallocating this memory using free.
3504 *STATUS is set to one of the following values:
3505 0: The demangling operation succeeded.
3506 -1: A memory allocation failure occurred.
3507 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
3508 -3: One of the arguments is invalid.
3510 The demangling is performed using the C++ ABI mangling rules, with
3514 __cxa_demangle (mangled_name
, output_buffer
, length
, status
)
3515 const char *mangled_name
;
3516 char *output_buffer
;
3526 if (mangled_name
== NULL
)
3532 if (output_buffer
!= NULL
&& length
== NULL
)
3538 demangled
= d_demangle (mangled_name
, DMGL_TYPES
, &alc
);
3540 if (demangled
== NULL
)
3549 if (output_buffer
== NULL
)
3556 if (strlen (demangled
) < *length
)
3558 strcpy (output_buffer
, demangled
);
3560 demangled
= output_buffer
;
3564 free (output_buffer
);
3574 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
3576 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
3577 mangled name, return a buffer allocated with malloc holding the
3578 demangled name. Otherwise, return NULL. */
3581 cplus_demangle_v3 (mangled
, options
)
3582 const char* mangled
;
3587 return d_demangle (mangled
, options
, &alc
);
3590 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
3591 conventions, but the output formatting is a little different.
3592 This instructs the C++ demangler not to emit pointer characters ("*"), and
3593 to use Java's namespace separator symbol ("." instead of "::"). It then
3594 does an additional pass over the demangled output to replace instances
3595 of JArray<TYPE> with TYPE[]. */
3598 java_demangle_v3 (mangled
)
3599 const char* mangled
;
3607 demangled
= d_demangle (mangled
, DMGL_JAVA
| DMGL_PARAMS
, &alc
);
3609 if (demangled
== NULL
)
3615 while (*from
!= '\0')
3617 if (strncmp (from
, "JArray<", 7) == 0)
3622 else if (nesting
> 0 && *from
== '>')
3624 while (to
> demangled
&& to
[-1] == ' ')
3640 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
3642 #ifndef IN_GLIBCPP_V3
3644 /* Demangle a string in order to find out whether it is a constructor
3645 or destructor. Return non-zero on success. Set *CTOR_KIND and
3646 *DTOR_KIND appropriately. */
3649 is_ctor_or_dtor (mangled
, ctor_kind
, dtor_kind
)
3650 const char *mangled
;
3651 enum gnu_v3_ctor_kinds
*ctor_kind
;
3652 enum gnu_v3_dtor_kinds
*dtor_kind
;
3657 *ctor_kind
= (enum gnu_v3_ctor_kinds
) 0;
3658 *dtor_kind
= (enum gnu_v3_dtor_kinds
) 0;
3660 if (! d_init_info (mangled
, DMGL_GNU_V3
, strlen (mangled
), &di
))
3663 dc
= d_mangled_name (&di
, 1);
3665 if (dc
== NULL
|| d_peek_char (&di
) != '\0')
3674 case D_COMP_TYPED_NAME
:
3675 case D_COMP_TEMPLATE
:
3676 case D_COMP_RESTRICT
:
3677 case D_COMP_VOLATILE
:
3679 case D_COMP_VENDOR_TYPE_QUAL
:
3682 case D_COMP_QUAL_NAME
:
3686 *ctor_kind
= dc
->u
.s_ctor
.kind
;
3689 *dtor_kind
= dc
->u
.s_dtor
.kind
;
3697 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
3698 name. A non-zero return indicates the type of constructor. */
3700 enum gnu_v3_ctor_kinds
3701 is_gnu_v3_mangled_ctor (name
)
3704 enum gnu_v3_ctor_kinds ctor_kind
;
3705 enum gnu_v3_dtor_kinds dtor_kind
;
3707 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
3708 return (enum gnu_v3_ctor_kinds
) 0;
3713 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
3714 name. A non-zero return indicates the type of destructor. */
3716 enum gnu_v3_dtor_kinds
3717 is_gnu_v3_mangled_dtor (name
)
3720 enum gnu_v3_ctor_kinds ctor_kind
;
3721 enum gnu_v3_dtor_kinds dtor_kind
;
3723 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
3724 return (enum gnu_v3_dtor_kinds
) 0;
3728 #endif /* IN_GLIBCPP_V3 */
3730 #ifdef STANDALONE_DEMANGLER
3733 #include "dyn-string.h"
3735 static void print_usage
PARAMS ((FILE* fp
, int exit_value
));
3737 #define IS_ALPHA(CHAR) \
3738 (((CHAR) >= 'a' && (CHAR) <= 'z') \
3739 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
3741 /* Non-zero if CHAR is a character than can occur in a mangled name. */
3742 #define is_mangled_char(CHAR) \
3743 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
3744 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
3746 /* The name of this program, as invoked. */
3747 const char* program_name
;
3749 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
3752 print_usage (fp
, exit_value
)
3756 fprintf (fp
, "Usage: %s [options] [names ...]\n", program_name
);
3757 fprintf (fp
, "Options:\n");
3758 fprintf (fp
, " -h,--help Display this message.\n");
3759 fprintf (fp
, " -p,--no-params Don't display function parameters\n");
3760 fprintf (fp
, " -v,--verbose Produce verbose demanglings.\n");
3761 fprintf (fp
, "If names are provided, they are demangled. Otherwise filters standard input.\n");
3766 /* Option specification for getopt_long. */
3767 static const struct option long_options
[] =
3769 { "help", no_argument
, NULL
, 'h' },
3770 { "no-params", no_argument
, NULL
, 'p' },
3771 { "verbose", no_argument
, NULL
, 'v' },
3772 { NULL
, no_argument
, NULL
, 0 },
3775 /* Main entry for a demangling filter executable. It will demangle
3776 its command line arguments, if any. If none are provided, it will
3777 filter stdin to stdout, replacing any recognized mangled C++ names
3778 with their demangled equivalents. */
3787 int options
= DMGL_PARAMS
| DMGL_ANSI
| DMGL_TYPES
;
3789 /* Use the program name of this program, as invoked. */
3790 program_name
= argv
[0];
3792 /* Parse options. */
3795 opt_char
= getopt_long (argc
, argv
, "hpv", long_options
, NULL
);
3798 case '?': /* Unrecognized option. */
3799 print_usage (stderr
, 1);
3803 print_usage (stdout
, 0);
3807 options
&= ~ DMGL_PARAMS
;
3811 options
|= DMGL_VERBOSE
;
3815 while (opt_char
!= -1);
3818 /* No command line arguments were provided. Filter stdin. */
3820 dyn_string_t mangled
= dyn_string_new (3);
3823 /* Read all of input. */
3824 while (!feof (stdin
))
3828 /* Pile characters into mangled until we hit one that can't
3829 occur in a mangled name. */
3831 while (!feof (stdin
) && is_mangled_char (c
))
3833 dyn_string_append_char (mangled
, c
);
3839 if (dyn_string_length (mangled
) > 0)
3841 s
= cplus_demangle_v3 (dyn_string_buf (mangled
), options
);
3850 /* It might not have been a mangled name. Print the
3852 fputs (dyn_string_buf (mangled
), stdout
);
3855 dyn_string_clear (mangled
);
3858 /* If we haven't hit EOF yet, we've read one character that
3859 can't occur in a mangled name, so print it out. */
3864 dyn_string_delete (mangled
);
3867 /* Demangle command line arguments. */
3869 /* Loop over command line arguments. */
3870 for (i
= optind
; i
< argc
; ++i
)
3874 /* Attempt to demangle. */
3875 s
= cplus_demangle_v3 (argv
[i
], options
);
3877 /* If it worked, print the demangled name. */
3884 fprintf (stderr
, "Failed: %s\n", argv
[i
]);
3891 #endif /* STANDALONE_DEMANGLER */