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. */
336 /* We use this structure to hold information during printing. */
340 /* The options passed to the demangler. */
342 /* Buffer holding the result. */
344 /* Current length of data in buffer. */
346 /* Allocated size of buffer. */
348 /* The current list of templates, if any. */
349 struct d_print_template
*templates
;
350 /* The current list of modifiers (e.g., pointer, reference, etc.),
352 struct d_print_mod
*modifiers
;
353 /* Set to 1 if we had a memory allocation failure. */
354 int allocation_failure
;
357 #define d_print_saw_error(dpi) ((dpi)->buf == NULL)
359 #define d_append_char(dpi, c) \
362 if ((dpi)->buf != NULL && (dpi)->len < (dpi)->alc) \
363 (dpi)->buf[(dpi)->len++] = (c); \
365 d_print_append_char ((dpi), (c)); \
369 #define d_append_buffer(dpi, s, l) \
372 if ((dpi)->buf != NULL && (dpi)->len + (l) <= (dpi)->alc) \
374 memcpy ((dpi)->buf + (dpi)->len, (s), (l)); \
378 d_print_append_buffer ((dpi), (s), (l)); \
382 #define d_append_string(dpi, s) \
385 size_t d_append_string_len = strlen (s); \
386 d_append_buffer ((dpi), (s), d_append_string_len); \
390 #ifdef CP_DEMANGLE_DEBUG
391 static void d_dump
PARAMS ((struct d_comp
*, int));
393 static struct d_comp
*d_make_empty
PARAMS ((struct d_info
*,
395 static struct d_comp
*d_make_comp
PARAMS ((struct d_info
*, enum d_comp_type
,
396 struct d_comp
*, struct d_comp
*));
397 static struct d_comp
*d_make_name
PARAMS ((struct d_info
*, const char *,
399 static struct d_comp
*d_make_builtin_type
PARAMS ((struct d_info
*,
400 const struct d_builtin_type_info
*));
401 static struct d_comp
*d_make_operator
PARAMS ((struct d_info
*,
402 const struct d_operator_info
*));
403 static struct d_comp
*d_make_extended_operator
PARAMS ((struct d_info
*,
406 static struct d_comp
*d_make_ctor
PARAMS ((struct d_info
*,
407 enum gnu_v3_ctor_kinds
,
409 static struct d_comp
*d_make_dtor
PARAMS ((struct d_info
*,
410 enum gnu_v3_dtor_kinds
,
412 static struct d_comp
*d_make_template_param
PARAMS ((struct d_info
*, long));
413 static struct d_comp
*d_make_sub
PARAMS ((struct d_info
*, const char *));
414 static struct d_comp
*d_mangled_name
PARAMS ((struct d_info
*));
415 static int has_return_type
PARAMS ((struct d_comp
*));
416 static int is_ctor_dtor_or_conversion
PARAMS ((struct d_comp
*));
417 static struct d_comp
*d_encoding
PARAMS ((struct d_info
*, int));
418 static struct d_comp
*d_name
PARAMS ((struct d_info
*));
419 static struct d_comp
*d_nested_name
PARAMS ((struct d_info
*));
420 static struct d_comp
*d_prefix
PARAMS ((struct d_info
*));
421 static struct d_comp
*d_unqualified_name
PARAMS ((struct d_info
*));
422 static struct d_comp
*d_source_name
PARAMS ((struct d_info
*));
423 static long d_number
PARAMS ((struct d_info
*));
424 static struct d_comp
*d_identifier
PARAMS ((struct d_info
*, int));
425 static struct d_comp
*d_operator_name
PARAMS ((struct d_info
*));
426 static struct d_comp
*d_special_name
PARAMS ((struct d_info
*));
427 static int d_call_offset
PARAMS ((struct d_info
*, int));
428 static struct d_comp
*d_ctor_dtor_name
PARAMS ((struct d_info
*));
429 static struct d_comp
*d_type
PARAMS ((struct d_info
*));
430 static struct d_comp
**d_cv_qualifiers
PARAMS ((struct d_info
*,
432 static struct d_comp
*d_function_type
PARAMS ((struct d_info
*));
433 static struct d_comp
*d_bare_function_type
PARAMS ((struct d_info
*, int));
434 static struct d_comp
*d_class_enum_type
PARAMS ((struct d_info
*));
435 static struct d_comp
*d_array_type
PARAMS ((struct d_info
*));
436 static struct d_comp
*d_pointer_to_member_type
PARAMS ((struct d_info
*));
437 static struct d_comp
*d_template_param
PARAMS ((struct d_info
*));
438 static struct d_comp
*d_template_args
PARAMS ((struct d_info
*));
439 static struct d_comp
*d_template_arg
PARAMS ((struct d_info
*));
440 static struct d_comp
*d_expression
PARAMS ((struct d_info
*));
441 static struct d_comp
*d_expr_primary
PARAMS ((struct d_info
*));
442 static struct d_comp
*d_local_name
PARAMS ((struct d_info
*));
443 static int d_discriminator
PARAMS ((struct d_info
*));
444 static int d_add_substitution
PARAMS ((struct d_info
*, struct d_comp
*));
445 static struct d_comp
*d_substitution
PARAMS ((struct d_info
*));
446 static void d_print_resize
PARAMS ((struct d_print_info
*, size_t));
447 static void d_print_append_char
PARAMS ((struct d_print_info
*, int));
448 static void d_print_append_buffer
PARAMS ((struct d_print_info
*, const char *,
450 static void d_print_error
PARAMS ((struct d_print_info
*));
451 static char *d_print
PARAMS ((int, const struct d_comp
*, size_t *));
452 static void d_print_comp
PARAMS ((struct d_print_info
*,
453 const struct d_comp
*));
454 static void d_print_identifier
PARAMS ((struct d_print_info
*, const char *,
456 static void d_print_mod_list
PARAMS ((struct d_print_info
*,
457 struct d_print_mod
*));
458 static void d_print_mod
PARAMS ((struct d_print_info
*,
459 const struct d_comp
*));
460 static void d_print_function_type
PARAMS ((struct d_print_info
*,
461 const struct d_comp
*,
462 struct d_print_mod
*));
463 static void d_print_array_type
PARAMS ((struct d_print_info
*,
464 const struct d_comp
*,
465 struct d_print_mod
*));
466 static void d_print_expr_op
PARAMS ((struct d_print_info
*,
467 const struct d_comp
*));
468 static void d_print_cast
PARAMS ((struct d_print_info
*,
469 const struct d_comp
*));
470 static int d_init_info
PARAMS ((const char *, int, size_t, struct d_info
*));
471 static char *d_demangle
PARAMS ((const char *, int, size_t *));
473 #ifdef CP_DEMANGLE_DEBUG
485 for (i
= 0; i
< indent
; ++i
)
491 printf ("name '%.*s'\n", dc
->u
.s_name
.len
, dc
->u
.s_name
.s
);
493 case D_COMP_TEMPLATE_PARAM
:
494 printf ("template parameter %ld\n", dc
->u
.s_number
.number
);
497 printf ("constructor %d\n", (int) dc
->u
.s_ctor
.kind
);
498 d_dump (dc
->u
.s_ctor
.name
, indent
+ 2);
501 printf ("destructor %d\n", (int) dc
->u
.s_dtor
.kind
);
502 d_dump (dc
->u
.s_dtor
.name
, indent
+ 2);
505 printf ("standard substitution %s\n", dc
->u
.s_string
.string
);
507 case D_COMP_BUILTIN_TYPE
:
508 printf ("builtin type %s\n", dc
->u
.s_builtin
.type
->name
);
510 case D_COMP_OPERATOR
:
511 printf ("operator %s\n", dc
->u
.s_operator
.op
->name
);
513 case D_COMP_EXTENDED_OPERATOR
:
514 printf ("extended operator with %d args\n",
515 dc
->u
.s_extended_operator
.args
);
516 d_dump (dc
->u
.s_extended_operator
.name
, indent
+ 2);
519 case D_COMP_QUAL_NAME
:
520 printf ("qualified name\n");
522 case D_COMP_TYPED_NAME
:
523 printf ("typed name\n");
525 case D_COMP_TEMPLATE
:
526 printf ("template\n");
534 case D_COMP_CONSTRUCTION_VTABLE
:
535 printf ("construction vtable\n");
537 case D_COMP_TYPEINFO
:
538 printf ("typeinfo\n");
540 case D_COMP_TYPEINFO_NAME
:
541 printf ("typeinfo name\n");
543 case D_COMP_TYPEINFO_FN
:
544 printf ("typeinfo function\n");
549 case D_COMP_VIRTUAL_THUNK
:
550 printf ("virtual thunk\n");
552 case D_COMP_COVARIANT_THUNK
:
553 printf ("covariant thunk\n");
555 case D_COMP_JAVA_CLASS
:
556 printf ("java class\n");
562 printf ("reference temporary\n");
564 case D_COMP_RESTRICT
:
565 printf ("restrict\n");
567 case D_COMP_VOLATILE
:
568 printf ("volatile\n");
573 case D_COMP_VENDOR_TYPE_QUAL
:
574 printf ("vendor type qualifier\n");
577 printf ("pointer\n");
579 case D_COMP_REFERENCE
:
580 printf ("reference\n");
583 printf ("complex\n");
585 case D_COMP_IMAGINARY
:
586 printf ("imaginary\n");
588 case D_COMP_VENDOR_TYPE
:
589 printf ("vendor type\n");
591 case D_COMP_FUNCTION_TYPE
:
592 printf ("function type\n");
594 case D_COMP_ARRAY_TYPE
:
595 printf ("array type\n");
597 case D_COMP_PTRMEM_TYPE
:
598 printf ("pointer to member type\n");
601 printf ("argument list\n");
603 case D_COMP_TEMPLATE_ARGLIST
:
604 printf ("template argument list\n");
610 printf ("unary operator\n");
613 printf ("binary operator\n");
615 case D_COMP_BINARY_ARGS
:
616 printf ("binary operator arguments\n");
619 printf ("trinary operator\n");
621 case D_COMP_TRINARY_ARG1
:
622 printf ("trinary operator arguments 1\n");
624 case D_COMP_TRINARY_ARG2
:
625 printf ("trinary operator arguments 1\n");
628 printf ("literal\n");
632 d_dump (d_left (dc
), indent
+ 2);
633 d_dump (d_right (dc
), indent
+ 2);
636 #endif /* CP_DEMANGLE_DEBUG */
638 /* Add a new component. */
640 static struct d_comp
*
641 d_make_empty (di
, type
)
643 enum d_comp_type type
;
647 if (di
->next_comp
>= di
->num_comps
)
649 p
= &di
->comps
[di
->next_comp
];
655 /* Add a new generic component. */
657 static struct d_comp
*
658 d_make_comp (di
, type
, left
, right
)
660 enum d_comp_type type
;
662 struct d_comp
*right
;
666 /* We check for errors here. A typical error would be a NULL return
667 from a subroutine. We catch here, and return NULL on upward. */
670 /* These types require two parameters. */
671 case D_COMP_QUAL_NAME
:
672 case D_COMP_TYPED_NAME
:
673 case D_COMP_TEMPLATE
:
674 case D_COMP_VENDOR_TYPE_QUAL
:
675 case D_COMP_PTRMEM_TYPE
:
678 case D_COMP_BINARY_ARGS
:
680 case D_COMP_TRINARY_ARG1
:
681 case D_COMP_TRINARY_ARG2
:
683 if (left
== NULL
|| right
== NULL
)
687 /* These types only require one parameter. */
690 case D_COMP_CONSTRUCTION_VTABLE
:
691 case D_COMP_TYPEINFO
:
692 case D_COMP_TYPEINFO_NAME
:
693 case D_COMP_TYPEINFO_FN
:
695 case D_COMP_VIRTUAL_THUNK
:
696 case D_COMP_COVARIANT_THUNK
:
697 case D_COMP_JAVA_CLASS
:
701 case D_COMP_REFERENCE
:
703 case D_COMP_IMAGINARY
:
704 case D_COMP_VENDOR_TYPE
:
706 case D_COMP_TEMPLATE_ARGLIST
:
712 /* This needs a right parameter, but the left parameter can be
714 case D_COMP_ARRAY_TYPE
:
719 /* These are allowed to have no parameters--in some cases they
720 will be filled in later. */
721 case D_COMP_FUNCTION_TYPE
:
722 case D_COMP_RESTRICT
:
723 case D_COMP_VOLATILE
:
727 /* Other types should not be seen here. */
732 p
= d_make_empty (di
, type
);
735 p
->u
.s_binary
.left
= left
;
736 p
->u
.s_binary
.right
= right
;
741 /* Add a new name component. */
743 static struct d_comp
*
744 d_make_name (di
, s
, len
)
751 p
= d_make_empty (di
, D_COMP_NAME
);
755 p
->u
.s_name
.len
= len
;
760 /* Add a new builtin type component. */
762 static struct d_comp
*
763 d_make_builtin_type (di
, type
)
765 const struct d_builtin_type_info
*type
;
769 p
= d_make_empty (di
, D_COMP_BUILTIN_TYPE
);
771 p
->u
.s_builtin
.type
= type
;
775 /* Add a new operator component. */
777 static struct d_comp
*
778 d_make_operator (di
, op
)
780 const struct d_operator_info
*op
;
784 p
= d_make_empty (di
, D_COMP_OPERATOR
);
786 p
->u
.s_operator
.op
= op
;
790 /* Add a new extended operator component. */
792 static struct d_comp
*
793 d_make_extended_operator (di
, args
, name
)
800 p
= d_make_empty (di
, D_COMP_EXTENDED_OPERATOR
);
803 p
->u
.s_extended_operator
.args
= args
;
804 p
->u
.s_extended_operator
.name
= name
;
809 /* Add a new constructor component. */
811 static struct d_comp
*
812 d_make_ctor (di
, kind
, name
)
814 enum gnu_v3_ctor_kinds kind
;
819 p
= d_make_empty (di
, D_COMP_CTOR
);
822 p
->u
.s_ctor
.kind
= kind
;
823 p
->u
.s_ctor
.name
= name
;
828 /* Add a new destructor component. */
830 static struct d_comp
*
831 d_make_dtor (di
, kind
, name
)
833 enum gnu_v3_dtor_kinds kind
;
838 p
= d_make_empty (di
, D_COMP_DTOR
);
841 p
->u
.s_dtor
.kind
= kind
;
842 p
->u
.s_dtor
.name
= name
;
847 /* Add a new template parameter. */
849 static struct d_comp
*
850 d_make_template_param (di
, i
)
856 p
= d_make_empty (di
, D_COMP_TEMPLATE_PARAM
);
858 p
->u
.s_number
.number
= i
;
862 /* Add a new standard substitution component. */
864 static struct d_comp
*
865 d_make_sub (di
, name
)
871 p
= d_make_empty (di
, D_COMP_SUB_STD
);
873 p
->u
.s_string
.string
= name
;
877 /* <mangled-name> ::= _Z <encoding> */
879 static struct d_comp
*
883 if (d_next_char (di
) != '_')
885 if (d_next_char (di
) != 'Z')
887 return d_encoding (di
, 1);
890 /* Return whether a function should have a return type. The argument
891 is the function name, which may be qualified in various ways. The
892 rules are that template functions have return types with some
893 exceptions, function types which are not part of a function name
894 mangling have return types with some exceptions, and non-template
895 function names do not have return types. The exceptions are that
896 constructors, destructors, and conversion operators do not have
909 case D_COMP_TEMPLATE
:
910 return ! is_ctor_dtor_or_conversion (d_left (dc
));
911 case D_COMP_RESTRICT
:
912 case D_COMP_VOLATILE
:
914 case D_COMP_VENDOR_TYPE_QUAL
:
915 return has_return_type (d_left (dc
));
919 /* Return whether a name is a constructor, a destructor, or a
920 conversion operator. */
923 is_ctor_dtor_or_conversion (dc
)
932 case D_COMP_QUAL_NAME
:
933 return is_ctor_dtor_or_conversion (d_right (dc
));
941 /* <encoding> ::= <(function) name> <bare-function-type>
945 TOP_LEVEL is non-zero when called at the top level, in which case
946 if DMGL_PARAMS is not set we do not demangle the function
947 parameters. We only set this at the top level, because otherwise
948 we would not correctly demangle names in local scopes. */
950 static struct d_comp
*
951 d_encoding (di
, top_level
)
955 char peek
= d_peek_char (di
);
957 if (peek
== 'G' || peek
== 'T')
958 return d_special_name (di
);
964 peek
= d_peek_char (di
);
967 || (top_level
&& (di
->options
& DMGL_PARAMS
) == 0))
969 return d_make_comp (di
, D_COMP_TYPED_NAME
, dc
,
970 d_bare_function_type (di
, has_return_type (dc
)));
974 /* <name> ::= <nested-name>
976 ::= <unscoped-template-name> <template-args>
979 <unscoped-name> ::= <unqualified-name>
980 ::= St <unqualified-name>
982 <unscoped-template-name> ::= <unscoped-name>
986 static struct d_comp
*
990 char peek
= d_peek_char (di
);
996 return d_nested_name (di
);
999 return d_local_name (di
);
1005 if (d_peek_next_char (di
) != 't')
1007 dc
= d_substitution (di
);
1013 dc
= d_make_comp (di
, D_COMP_QUAL_NAME
, d_make_name (di
, "std", 3),
1014 d_unqualified_name (di
));
1018 if (d_peek_char (di
) != 'I')
1020 /* The grammar does not permit this case to occur if we
1021 called d_substitution() above (i.e., subst == 1). We
1022 don't bother to check. */
1026 /* This is <template-args>, which means that we just saw
1027 <unscoped-template-name>, which is a substitution
1028 candidate if we didn't just get it from a
1032 if (! d_add_substitution (di
, dc
))
1035 dc
= d_make_comp (di
, D_COMP_TEMPLATE
, dc
, d_template_args (di
));
1042 dc
= d_unqualified_name (di
);
1043 if (d_peek_char (di
) == 'I')
1045 /* This is <template-args>, which means that we just saw
1046 <unscoped-template-name>, which is a substitution
1048 if (! d_add_substitution (di
, dc
))
1050 dc
= d_make_comp (di
, D_COMP_TEMPLATE
, dc
, d_template_args (di
));
1056 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1057 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1060 static struct d_comp
*
1065 struct d_comp
**pret
;
1067 if (d_next_char (di
) != 'N')
1070 pret
= d_cv_qualifiers (di
, &ret
);
1074 *pret
= d_prefix (di
);
1078 if (d_next_char (di
) != 'E')
1084 /* <prefix> ::= <prefix> <unqualified-name>
1085 ::= <template-prefix> <template-args>
1086 ::= <template-param>
1090 <template-prefix> ::= <prefix> <(template) unqualified-name>
1091 ::= <template-param>
1095 static struct d_comp
*
1099 struct d_comp
*ret
= NULL
;
1104 enum d_comp_type comb_type
;
1107 peek
= d_peek_char (di
);
1111 /* The older code accepts a <local-name> here, but I don't see
1112 that in the grammar. The older code does not accept a
1113 <template-param> here. */
1115 comb_type
= D_COMP_QUAL_NAME
;
1117 || (peek
>= 'a' && peek
<= 'z')
1120 dc
= d_unqualified_name (di
);
1121 else if (peek
== 'S')
1122 dc
= d_substitution (di
);
1123 else if (peek
== 'I')
1127 comb_type
= D_COMP_TEMPLATE
;
1128 dc
= d_template_args (di
);
1130 else if (peek
== 'T')
1131 dc
= d_template_param (di
);
1132 else if (peek
== 'E')
1140 ret
= d_make_comp (di
, comb_type
, ret
, dc
);
1142 if (peek
!= 'S' && d_peek_char (di
) != 'E')
1144 if (! d_add_substitution (di
, ret
))
1150 /* <unqualified-name> ::= <operator-name>
1151 ::= <ctor-dtor-name>
1155 static struct d_comp
*
1156 d_unqualified_name (di
)
1161 peek
= d_peek_char (di
);
1162 if (IS_DIGIT (peek
))
1163 return d_source_name (di
);
1164 else if (peek
>= 'a' && peek
<= 'z')
1165 return d_operator_name (di
);
1166 else if (peek
== 'C' || peek
== 'D')
1167 return d_ctor_dtor_name (di
);
1172 /* <source-name> ::= <(positive length) number> <identifier> */
1174 static struct d_comp
*
1181 len
= d_number (di
);
1184 ret
= d_identifier (di
, len
);
1185 di
->last_name
= ret
;
1189 /* number ::= [n] <(non-negative decimal integer)> */
1200 peek
= d_peek_char (di
);
1205 peek
= d_peek_char (di
);
1211 if (! IS_DIGIT (peek
))
1213 ret
= ret
* 10 + peek
- '0';
1215 peek
= d_peek_char (di
);
1219 /* identifier ::= <(unqualified source code identifier)> */
1221 static struct d_comp
*
1222 d_identifier (di
, len
)
1229 d_advance (di
, len
);
1231 /* Look for something which looks like a gcc encoding of an
1232 anonymous namespace, and replace it with a more user friendly
1234 if (len
>= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN
+ 2
1235 && memcmp (name
, ANONYMOUS_NAMESPACE_PREFIX
,
1236 ANONYMOUS_NAMESPACE_PREFIX_LEN
) == 0)
1240 s
= name
+ ANONYMOUS_NAMESPACE_PREFIX_LEN
;
1241 if ((*s
== '.' || *s
== '_' || *s
== '$')
1243 return d_make_name (di
, "(anonymous namespace)",
1244 sizeof "(anonymous namespace)" - 1);
1247 return d_make_name (di
, name
, len
);
1250 /* operator_name ::= many different two character encodings.
1252 ::= v <digit> <source-name>
1255 static const struct d_operator_info d_operators
[] =
1266 { "da", "delete[]", 1 },
1268 { "dl", "delete", 1 },
1285 { "na", "new[]", 1 },
1304 { "st", "sizeof ", 1 },
1305 { "sz", "sizeof ", 1 }
1308 static struct d_comp
*
1309 d_operator_name (di
)
1315 c1
= d_next_char (di
);
1316 c2
= d_next_char (di
);
1317 if (c1
== 'v' && IS_DIGIT (c2
))
1318 return d_make_extended_operator (di
, c2
- '0', d_source_name (di
));
1319 else if (c1
== 'c' && c2
== 'v')
1320 return d_make_comp (di
, D_COMP_CAST
, d_type (di
), NULL
);
1324 int high
= sizeof (d_operators
) / sizeof (d_operators
[0]);
1329 const struct d_operator_info
*p
;
1331 i
= low
+ (high
- low
) / 2;
1332 p
= d_operators
+ i
;
1334 if (c1
== p
->code
[0] && c2
== p
->code
[1])
1335 return d_make_operator (di
, p
);
1337 if (c1
< p
->code
[0] || (c1
== p
->code
[0] && c2
< p
->code
[1]))
1347 /* <special-name> ::= TV <type>
1351 ::= GV <(object) name>
1352 ::= T <call-offset> <(base) encoding>
1353 ::= Tc <call-offset> <call-offset> <(base) encoding>
1354 Also g++ extensions:
1355 ::= TC <type> <(offset) number> _ <(base) type>
1361 static struct d_comp
*
1367 c
= d_next_char (di
);
1370 switch (d_next_char (di
))
1373 return d_make_comp (di
, D_COMP_VTABLE
, d_type (di
), NULL
);
1375 return d_make_comp (di
, D_COMP_VTT
, d_type (di
), NULL
);
1377 return d_make_comp (di
, D_COMP_TYPEINFO
, d_type (di
), NULL
);
1379 return d_make_comp (di
, D_COMP_TYPEINFO_NAME
, d_type (di
), NULL
);
1382 if (! d_call_offset (di
, 'h'))
1384 return d_make_comp (di
, D_COMP_THUNK
, d_encoding (di
, 0), NULL
);
1387 if (! d_call_offset (di
, 'v'))
1389 return d_make_comp (di
, D_COMP_VIRTUAL_THUNK
, d_encoding (di
, 0),
1393 if (! d_call_offset (di
, '\0'))
1395 if (! d_call_offset (di
, '\0'))
1397 return d_make_comp (di
, D_COMP_COVARIANT_THUNK
, d_encoding (di
, 0),
1402 struct d_comp
*derived_type
;
1404 struct d_comp
*base_type
;
1406 derived_type
= d_type (di
);
1407 offset
= d_number (di
);
1410 if (d_next_char (di
) != '_')
1412 base_type
= d_type (di
);
1413 /* We don't display the offset. FIXME: We should display
1414 it in verbose mode. */
1415 return d_make_comp (di
, D_COMP_CONSTRUCTION_VTABLE
, base_type
,
1420 return d_make_comp (di
, D_COMP_TYPEINFO_FN
, d_type (di
), NULL
);
1422 return d_make_comp (di
, D_COMP_JAVA_CLASS
, d_type (di
), NULL
);
1430 switch (d_next_char (di
))
1433 return d_make_comp (di
, D_COMP_GUARD
, d_name (di
), NULL
);
1436 return d_make_comp (di
, D_COMP_REFTEMP
, d_name (di
), NULL
);
1446 /* <call-offset> ::= h <nv-offset> _
1449 <nv-offset> ::= <(offset) number>
1451 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1453 The C parameter, if not '\0', is a character we just read which is
1454 the start of the <call-offset>.
1456 We don't display the offset information anywhere. FIXME: We should
1457 display it in verbose mode. */
1460 d_call_offset (di
, c
)
1465 long virtual_offset
;
1468 c
= d_next_char (di
);
1471 offset
= d_number (di
);
1474 offset
= d_number (di
);
1475 if (d_next_char (di
) != '_')
1477 virtual_offset
= d_number (di
);
1482 if (d_next_char (di
) != '_')
1488 /* <ctor-dtor-name> ::= C1
1496 static struct d_comp
*
1497 d_ctor_dtor_name (di
)
1500 switch (d_next_char (di
))
1504 enum gnu_v3_ctor_kinds kind
;
1506 switch (d_next_char (di
))
1509 kind
= gnu_v3_complete_object_ctor
;
1512 kind
= gnu_v3_base_object_ctor
;
1515 kind
= gnu_v3_complete_object_allocating_ctor
;
1520 return d_make_ctor (di
, kind
, di
->last_name
);
1525 enum gnu_v3_dtor_kinds kind
;
1527 switch (d_next_char (di
))
1530 kind
= gnu_v3_deleting_dtor
;
1533 kind
= gnu_v3_complete_object_dtor
;
1536 kind
= gnu_v3_base_object_dtor
;
1541 return d_make_dtor (di
, kind
, di
->last_name
);
1549 /* <type> ::= <builtin-type>
1551 ::= <class-enum-type>
1553 ::= <pointer-to-member-type>
1554 ::= <template-param>
1555 ::= <template-template-param> <template-args>
1557 ::= <CV-qualifiers> <type>
1562 ::= U <source-name> <type>
1564 <builtin-type> ::= various one letter codes
1568 static const struct d_builtin_type_info d_builtin_types
[26] =
1570 /* a */ { "signed char", "signed char", D_PRINT_INT
},
1571 /* b */ { "bool", "boolean", D_PRINT_BOOL
},
1572 /* c */ { "char", "byte", D_PRINT_INT
},
1573 /* d */ { "double", "double", D_PRINT_DEFAULT
},
1574 /* e */ { "long double", "long double", D_PRINT_DEFAULT
},
1575 /* f */ { "float", "float", D_PRINT_DEFAULT
},
1576 /* g */ { "__float128", "__float128", D_PRINT_DEFAULT
},
1577 /* h */ { "unsigned char", "unsigned char", D_PRINT_INT
},
1578 /* i */ { "int", "int", D_PRINT_INT
},
1579 /* j */ { "unsigned int", "unsigned", D_PRINT_INT
},
1580 /* k */ { NULL
, NULL
, D_PRINT_DEFAULT
},
1581 /* l */ { "long", "long", D_PRINT_LONG
},
1582 /* m */ { "unsigned long", "unsigned long", D_PRINT_LONG
},
1583 /* n */ { "__int128", "__int128", D_PRINT_DEFAULT
},
1584 /* o */ { "unsigned __int128", "unsigned __int128", D_PRINT_DEFAULT
},
1585 /* p */ { NULL
, NULL
, D_PRINT_DEFAULT
},
1586 /* q */ { NULL
, NULL
, D_PRINT_DEFAULT
},
1587 /* r */ { NULL
, NULL
, D_PRINT_DEFAULT
},
1588 /* s */ { "short", "short", D_PRINT_INT
},
1589 /* t */ { "unsigned short", "unsigned short", D_PRINT_INT
},
1590 /* u */ { NULL
, NULL
, D_PRINT_DEFAULT
},
1591 /* v */ { "void", "void", D_PRINT_VOID
},
1592 /* w */ { "wchar_t", "char", D_PRINT_INT
},
1593 /* x */ { "long long", "long", D_PRINT_DEFAULT
},
1594 /* y */ { "unsigned long long", "unsigned long long", D_PRINT_DEFAULT
},
1595 /* z */ { "...", "...", D_PRINT_DEFAULT
},
1598 static struct d_comp
*
1606 /* The ABI specifies that when CV-qualifiers are used, the base type
1607 is substitutable, and the fully qualified type is substitutable,
1608 but the base type with a strict subset of the CV-qualifiers is
1609 not substitutable. The natural recursive implementation of the
1610 CV-qualifiers would cause subsets to be substitutable, so instead
1611 we pull them all off now.
1613 FIXME: The ABI specifies that vendor qualifiers are handled just
1614 like the standard CV-qualifiers with respect to subsetting and
1615 substitution, but g++ does not appear to work this way. */
1617 peek
= d_peek_char (di
);
1618 if (peek
== 'r' || peek
== 'V' || peek
== 'K')
1620 struct d_comp
**pret
;
1622 pret
= d_cv_qualifiers (di
, &ret
);
1623 *pret
= d_type (di
);
1624 if (! d_add_substitution (di
, ret
))
1633 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1634 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
1635 case 'o': case 's': case 't':
1636 case 'v': case 'w': case 'x': case 'y': case 'z':
1637 ret
= d_make_builtin_type (di
, &d_builtin_types
[peek
- 'a']);
1644 ret
= d_make_comp (di
, D_COMP_VENDOR_TYPE
, d_source_name (di
), NULL
);
1648 ret
= d_function_type (di
);
1651 case '0': case '1': case '2': case '3': case '4':
1652 case '5': case '6': case '7': case '8': case '9':
1655 ret
= d_class_enum_type (di
);
1659 ret
= d_array_type (di
);
1663 ret
= d_pointer_to_member_type (di
);
1667 ret
= d_template_param (di
);
1668 if (d_peek_char (di
) == 'I')
1670 /* This is <template-template-param> <template-args>. The
1671 <template-template-param> part is a substitution
1673 if (! d_add_substitution (di
, ret
))
1675 ret
= d_make_comp (di
, D_COMP_TEMPLATE
, ret
, d_template_args (di
));
1680 /* If this is a special substitution, then it is the start of
1681 <class-enum-type>. */
1685 peek_next
= d_peek_next_char (di
);
1686 if (IS_DIGIT (peek_next
)
1688 || (peek_next
>= 'A' && peek_next
<= 'Z'))
1690 ret
= d_substitution (di
);
1691 /* The substituted name may have been a template name and
1692 may be followed by tepmlate args. */
1693 if (d_peek_char (di
) == 'I')
1694 ret
= d_make_comp (di
, D_COMP_TEMPLATE
, ret
,
1695 d_template_args (di
));
1701 ret
= d_class_enum_type (di
);
1702 /* If the substitution was a complete type, then it is not
1703 a new substitution candidate. However, if the
1704 substitution was followed by template arguments, then
1705 the whole thing is a substitution candidate. */
1706 if (ret
->type
== D_COMP_SUB_STD
)
1714 ret
= d_make_comp (di
, D_COMP_POINTER
, d_type (di
), NULL
);
1719 ret
= d_make_comp (di
, D_COMP_REFERENCE
, d_type (di
), NULL
);
1724 ret
= d_make_comp (di
, D_COMP_COMPLEX
, d_type (di
), NULL
);
1729 ret
= d_make_comp (di
, D_COMP_IMAGINARY
, d_type (di
), NULL
);
1734 ret
= d_source_name (di
);
1735 ret
= d_make_comp (di
, D_COMP_VENDOR_TYPE_QUAL
, d_type (di
), ret
);
1744 if (! d_add_substitution (di
, ret
))
1751 /* <CV-qualifiers> ::= [r] [V] [K] */
1753 static struct d_comp
**
1754 d_cv_qualifiers (di
, pret
)
1756 struct d_comp
**pret
;
1760 peek
= d_peek_char (di
);
1761 while (peek
== 'r' || peek
== 'V' || peek
== 'K')
1767 t
= D_COMP_RESTRICT
;
1768 else if (peek
== 'V')
1769 t
= D_COMP_VOLATILE
;
1773 *pret
= d_make_comp (di
, t
, NULL
, NULL
);
1776 pret
= &d_left (*pret
);
1778 peek
= d_peek_char (di
);
1784 /* <function-type> ::= F [Y] <bare-function-type> E */
1786 static struct d_comp
*
1787 d_function_type (di
)
1792 if (d_next_char (di
) != 'F')
1794 if (d_peek_char (di
) == 'Y')
1796 /* Function has C linkage. We don't print this information.
1797 FIXME: We should print it in verbose mode. */
1800 ret
= d_bare_function_type (di
, 1);
1801 if (d_next_char (di
) != 'E')
1806 /* <bare-function-type> ::= <type>+ */
1808 static struct d_comp
*
1809 d_bare_function_type (di
, has_return_type
)
1811 int has_return_type
;
1813 struct d_comp
*return_type
;
1815 struct d_comp
**ptl
;
1823 struct d_comp
*type
;
1825 peek
= d_peek_char (di
);
1826 if (peek
== '\0' || peek
== 'E')
1831 if (has_return_type
)
1834 has_return_type
= 0;
1838 *ptl
= d_make_comp (di
, D_COMP_ARGLIST
, type
, NULL
);
1839 ptl
= &d_right (*ptl
);
1843 /* There should be at least one parameter type besides the optional
1844 return type. A function which takes no arguments will have a
1845 single parameter type void. */
1849 /* If we have a single parameter type void, omit it. */
1850 if (d_right (tl
) == NULL
1851 && d_left (tl
)->type
== D_COMP_BUILTIN_TYPE
1852 && d_left (tl
)->u
.s_builtin
.type
->print
== D_PRINT_VOID
)
1855 return d_make_comp (di
, D_COMP_FUNCTION_TYPE
, return_type
, tl
);
1858 /* <class-enum-type> ::= <name> */
1860 static struct d_comp
*
1861 d_class_enum_type (di
)
1867 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
1868 ::= A [<(dimension) expression>] _ <(element) type>
1871 static struct d_comp
*
1878 if (d_next_char (di
) != 'A')
1881 peek
= d_peek_char (di
);
1884 else if (IS_DIGIT (peek
))
1892 peek
= d_peek_char (di
);
1894 while (IS_DIGIT (peek
));
1895 dim
= d_make_name (di
, s
, d_str (di
) - s
);
1899 dim
= d_expression (di
);
1904 if (d_next_char (di
) != '_')
1907 return d_make_comp (di
, D_COMP_ARRAY_TYPE
, dim
, d_type (di
));
1910 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
1912 static struct d_comp
*
1913 d_pointer_to_member_type (di
)
1918 struct d_comp
**pmem
;
1920 if (d_next_char (di
) != 'M')
1925 /* The ABI specifies that any type can be a substitution source, and
1926 that M is followed by two types, and that when a CV-qualified
1927 type is seen both the base type and the CV-qualified types are
1928 substitution sources. The ABI also specifies that for a pointer
1929 to a CV-qualified member function, the qualifiers are attached to
1930 the second type. Given the grammar, a plain reading of the ABI
1931 suggests that both the CV-qualified member function and the
1932 non-qualified member function are substitution sources. However,
1933 g++ does not work that way. g++ treats only the CV-qualified
1934 member function as a substitution source. FIXME. So to work
1935 with g++, we need to pull off the CV-qualifiers here, in order to
1936 avoid calling add_substitution() in d_type(). */
1938 pmem
= d_cv_qualifiers (di
, &mem
);
1939 *pmem
= d_type (di
);
1941 return d_make_comp (di
, D_COMP_PTRMEM_TYPE
, cl
, mem
);
1944 /* <template-param> ::= T_
1945 ::= T <(parameter-2 non-negative) number> _
1948 static struct d_comp
*
1949 d_template_param (di
)
1954 if (d_next_char (di
) != 'T')
1957 if (d_peek_char (di
) == '_')
1961 param
= d_number (di
);
1967 if (d_next_char (di
) != '_')
1970 return d_make_template_param (di
, param
);
1973 /* <template-args> ::= I <template-arg>+ E */
1975 static struct d_comp
*
1976 d_template_args (di
)
1979 struct d_comp
*hold_last_name
;
1981 struct d_comp
**pal
;
1983 /* Preserve the last name we saw--don't let the template arguments
1984 clobber it, as that would give us the wrong name for a subsequent
1985 constructor or destructor. */
1986 hold_last_name
= di
->last_name
;
1988 if (d_next_char (di
) != 'I')
1997 a
= d_template_arg (di
);
2001 *pal
= d_make_comp (di
, D_COMP_TEMPLATE_ARGLIST
, a
, NULL
);
2002 pal
= &d_right (*pal
);
2004 if (d_peek_char (di
) == 'E')
2011 di
->last_name
= hold_last_name
;
2016 /* <template-arg> ::= <type>
2017 ::= X <expression> E
2021 static struct d_comp
*
2027 switch (d_peek_char (di
))
2031 ret
= d_expression (di
);
2032 if (d_next_char (di
) != 'E')
2037 return d_expr_primary (di
);
2044 /* <expression> ::= <(unary) operator-name> <expression>
2045 ::= <(binary) operator-name> <expression> <expression>
2046 ::= <(trinary) operator-name> <expression> <expression> <expression>
2048 ::= <template-param>
2049 ::= sr <type> <unqualified-name>
2050 ::= sr <type> <unqualified-name> <template-args>
2054 static struct d_comp
*
2060 peek
= d_peek_char (di
);
2062 return d_expr_primary (di
);
2063 else if (peek
== 'T')
2064 return d_template_param (di
);
2065 else if (peek
== 's' && d_peek_next_char (di
) == 'r')
2067 struct d_comp
*type
;
2068 struct d_comp
*name
;
2072 name
= d_unqualified_name (di
);
2073 if (d_peek_char (di
) != 'I')
2074 return d_make_comp (di
, D_COMP_QUAL_NAME
, type
, name
);
2076 return d_make_comp (di
, D_COMP_QUAL_NAME
, type
,
2077 d_make_comp (di
, D_COMP_TEMPLATE
, name
,
2078 d_template_args (di
)));
2085 op
= d_operator_name (di
);
2089 if (op
->type
== D_COMP_OPERATOR
2090 && strcmp (op
->u
.s_operator
.op
->code
, "st") == 0)
2091 return d_make_comp (di
, D_COMP_UNARY
, op
, d_type (di
));
2097 case D_COMP_OPERATOR
:
2098 args
= op
->u
.s_operator
.op
->args
;
2100 case D_COMP_EXTENDED_OPERATOR
:
2101 args
= op
->u
.s_extended_operator
.args
;
2111 return d_make_comp (di
, D_COMP_UNARY
, op
, d_expression (di
));
2114 struct d_comp
*left
;
2116 left
= d_expression (di
);
2117 return d_make_comp (di
, D_COMP_BINARY
, op
,
2118 d_make_comp (di
, D_COMP_BINARY_ARGS
, left
,
2119 d_expression (di
)));
2123 struct d_comp
*first
;
2124 struct d_comp
*second
;
2126 first
= d_expression (di
);
2127 second
= d_expression (di
);
2128 return d_make_comp (di
, D_COMP_TRINARY
, op
,
2129 d_make_comp (di
, D_COMP_TRINARY_ARG1
, first
,
2131 D_COMP_TRINARY_ARG2
,
2133 d_expression (di
))));
2141 /* <expr-primary> ::= L <type> <(value) number> E
2142 ::= L <type> <(value) float> E
2143 ::= L <mangled-name> E
2146 static struct d_comp
*
2152 if (d_next_char (di
) != 'L')
2154 if (d_peek_char (di
) == '_')
2155 ret
= d_mangled_name (di
);
2158 struct d_comp
*type
;
2163 /* Rather than try to interpret the literal value, we just
2164 collect it as a string. Note that it's possible to have a
2165 floating point literal here. The ABI specifies that the
2166 format of such literals is machine independent. That's fine,
2167 but what's not fine is that versions of g++ up to 3.2 with
2168 -fabi-version=1 used upper case letters in the hex constant,
2169 and dumped out gcc's internal representation. That makes it
2170 hard to tell where the constant ends, and hard to dump the
2171 constant in any readable form anyhow. We don't attempt to
2172 handle these cases. */
2175 while (d_peek_char (di
) != 'E')
2177 ret
= d_make_comp (di
, D_COMP_LITERAL
, type
,
2178 d_make_name (di
, s
, d_str (di
) - s
));
2180 if (d_next_char (di
) != 'E')
2185 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2186 ::= Z <(function) encoding> E s [<discriminator>]
2189 static struct d_comp
*
2193 struct d_comp
*function
;
2195 if (d_next_char (di
) != 'Z')
2198 function
= d_encoding (di
, 0);
2200 if (d_next_char (di
) != 'E')
2203 if (d_peek_char (di
) == 's')
2206 if (! d_discriminator (di
))
2208 return d_make_comp (di
, D_COMP_QUAL_NAME
, function
,
2209 d_make_name (di
, "string literal",
2210 sizeof "string literal" - 1));
2214 struct d_comp
*name
;
2217 if (! d_discriminator (di
))
2219 return d_make_comp (di
, D_COMP_QUAL_NAME
, function
, name
);
2223 /* <discriminator> ::= _ <(non-negative) number>
2225 We demangle the discriminator, but we don't print it out. FIXME:
2226 We should print it out in verbose mode. */
2229 d_discriminator (di
)
2234 if (d_peek_char (di
) != '_')
2237 discrim
= d_number (di
);
2243 /* Add a new substitution. */
2246 d_add_substitution (di
, dc
)
2250 if (di
->next_sub
>= di
->num_subs
)
2252 di
->subs
[di
->next_sub
] = dc
;
2257 /* <substitution> ::= S <seq-id> _
2268 static struct d_comp
*
2274 if (d_next_char (di
) != 'S')
2277 c
= d_next_char (di
);
2278 if (c
== '_' || IS_DIGIT (c
) || (c
>= 'A' && c
<= 'Z'))
2288 id
= id
* 36 + c
- '0';
2289 else if (c
>= 'A' && c
<= 'Z')
2290 id
= id
* 36 + c
- 'A' + 10;
2293 c
= d_next_char (di
);
2300 if (id
>= di
->next_sub
)
2303 return di
->subs
[id
];
2310 return d_make_sub (di
, "std");
2312 di
->last_name
= d_make_sub (di
, "allocator");
2313 return d_make_sub (di
, "std::allocator");
2315 di
->last_name
= d_make_sub (di
, "basic_string");
2316 return d_make_sub (di
, "std::basic_string");
2318 di
->last_name
= d_make_sub (di
, "string");
2319 return d_make_sub (di
, "std::string");
2321 di
->last_name
= d_make_sub (di
, "istream");
2322 return d_make_sub (di
, "std::istream");
2324 di
->last_name
= d_make_sub (di
, "ostream");
2325 return d_make_sub (di
, "std::ostream");
2327 di
->last_name
= d_make_sub (di
, "iostream");
2328 return d_make_sub (di
, "std::iostream");
2335 /* Resize the print buffer. */
2338 d_print_resize (dpi
, add
)
2339 struct d_print_info
*dpi
;
2344 need
= dpi
->len
+ add
;
2345 while (need
> dpi
->alc
)
2350 newalc
= dpi
->alc
* 2;
2351 newbuf
= realloc (dpi
->buf
, newalc
);
2356 dpi
->allocation_failure
= 1;
2364 /* Append a character to the print buffer. */
2367 d_print_append_char (dpi
, c
)
2368 struct d_print_info
*dpi
;
2371 if (dpi
->buf
!= NULL
)
2373 if (dpi
->len
>= dpi
->alc
)
2375 d_print_resize (dpi
, 1);
2376 if (dpi
->buf
== NULL
)
2380 dpi
->buf
[dpi
->len
] = c
;
2385 /* Append a buffer to the print buffer. */
2388 d_print_append_buffer (dpi
, s
, l
)
2389 struct d_print_info
*dpi
;
2393 if (dpi
->buf
!= NULL
)
2395 if (dpi
->len
+ l
> dpi
->alc
)
2397 d_print_resize (dpi
, l
);
2398 if (dpi
->buf
== NULL
)
2402 memcpy (dpi
->buf
+ dpi
->len
, s
, l
);
2407 /* Indicate that an error occurred during printing. */
2411 struct d_print_info
*dpi
;
2417 /* Turn components into a human readable string. Returns a string
2418 allocated by malloc, or NULL on error. On success, this sets *PALC
2419 to the size of the allocated buffer. On failure, this sets *PALC
2420 to 0 for a bad parse, or to 1 for a memory allocation failure. */
2423 d_print (options
, dc
, palc
)
2425 const struct d_comp
*dc
;
2428 struct d_print_info dpi
;
2430 dpi
.options
= options
;
2433 dpi
.buf
= malloc (dpi
.alc
);
2434 if (dpi
.buf
== NULL
)
2441 dpi
.templates
= NULL
;
2442 dpi
.modifiers
= NULL
;
2444 dpi
.allocation_failure
= 0;
2446 d_print_comp (&dpi
, dc
);
2448 d_append_char (&dpi
, '\0');
2450 if (dpi
.buf
!= NULL
)
2453 *palc
= dpi
.allocation_failure
;
2458 /* Subroutine to handle components. */
2461 d_print_comp (dpi
, dc
)
2462 struct d_print_info
*dpi
;
2463 const struct d_comp
*dc
;
2467 d_print_error (dpi
);
2470 if (d_print_saw_error (dpi
))
2476 d_print_identifier (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
2479 case D_COMP_QUAL_NAME
:
2480 d_print_comp (dpi
, d_left (dc
));
2481 d_append_string (dpi
, (dpi
->options
& DMGL_JAVA
) == 0 ? "::" : ".");
2482 d_print_comp (dpi
, d_right (dc
));
2485 case D_COMP_TYPED_NAME
:
2487 const struct d_comp
*typed_name
;
2488 struct d_print_mod dpm
;
2489 struct d_print_template dpt
;
2491 /* Pass the name down to the type so that it can be printed in
2492 the right place for the type. If the name has
2493 CV-qualifiers, they are really method qualifiers; pull them
2494 off now and print them after everything else. Note that we
2495 don't handle D_COMP_VENDOR_TYPE_QUAL here; it's not
2496 accepted by d_cv_qualifiers() either. */
2497 typed_name
= d_left (dc
);
2498 while (typed_name
!= NULL
2499 && (typed_name
->type
== D_COMP_RESTRICT
2500 || typed_name
->type
== D_COMP_VOLATILE
2501 || typed_name
->type
== D_COMP_CONST
))
2502 typed_name
= d_left (typed_name
);
2504 dpm
.next
= dpi
->modifiers
;
2505 dpi
->modifiers
= &dpm
;
2506 dpm
.mod
= typed_name
;
2509 /* If typed_name is a template, then it applies to the
2510 function type as well. */
2511 if (typed_name
->type
== D_COMP_TEMPLATE
)
2513 dpt
.next
= dpi
->templates
;
2514 dpi
->templates
= &dpt
;
2515 dpt
.template = typed_name
;
2518 d_print_comp (dpi
, d_right (dc
));
2520 if (typed_name
->type
== D_COMP_TEMPLATE
)
2521 dpi
->templates
= dpt
.next
;
2523 /* If the modifier didn't get printed by the type, print it
2527 d_append_char (dpi
, ' ');
2528 d_print_comp (dpi
, typed_name
);
2531 dpi
->modifiers
= dpm
.next
;
2533 /* Now print any CV-qualifiers on the type. */
2534 typed_name
= d_left (dc
);
2535 while (typed_name
!= NULL
2536 && (typed_name
->type
== D_COMP_RESTRICT
2537 || typed_name
->type
== D_COMP_VOLATILE
2538 || typed_name
->type
== D_COMP_CONST
))
2540 d_print_mod (dpi
, typed_name
);
2541 typed_name
= d_left (typed_name
);
2547 case D_COMP_TEMPLATE
:
2548 d_print_comp (dpi
, d_left (dc
));
2549 d_append_char (dpi
, '<');
2550 d_print_comp (dpi
, d_right (dc
));
2551 /* Avoid generating two consecutive '>' characters, to avoid the
2552 C++ syntactic ambiguity. */
2553 if (dpi
->buf
[dpi
->len
- 1] == '>')
2554 d_append_char (dpi
, ' ');
2555 d_append_char (dpi
, '>');
2558 case D_COMP_TEMPLATE_PARAM
:
2562 struct d_print_template
*hold_dpt
;
2564 if (dpi
->templates
== NULL
)
2566 d_print_error (dpi
);
2569 i
= dc
->u
.s_number
.number
;
2570 for (a
= d_right (dpi
->templates
->template);
2574 if (a
->type
!= D_COMP_TEMPLATE_ARGLIST
)
2576 d_print_error (dpi
);
2583 if (i
!= 0 || a
== NULL
)
2585 d_print_error (dpi
);
2589 /* While processing this parameter, we need to pop the list of
2590 templates. This is because the template parameter may
2591 itself be a reference to a parameter of an outer
2594 hold_dpt
= dpi
->templates
;
2595 dpi
->templates
= hold_dpt
->next
;
2597 d_print_comp (dpi
, d_left (a
));
2599 dpi
->templates
= hold_dpt
;
2605 d_print_comp (dpi
, dc
->u
.s_ctor
.name
);
2609 d_append_char (dpi
, '~');
2610 d_print_comp (dpi
, dc
->u
.s_dtor
.name
);
2614 d_append_string (dpi
, "vtable for ");
2615 d_print_comp (dpi
, d_left (dc
));
2619 d_append_string (dpi
, "VTT for ");
2620 d_print_comp (dpi
, d_left (dc
));
2623 case D_COMP_CONSTRUCTION_VTABLE
:
2624 d_append_string (dpi
, "construction vtable for ");
2625 d_print_comp (dpi
, d_left (dc
));
2626 d_append_string (dpi
, "-in-");
2627 d_print_comp (dpi
, d_right (dc
));
2630 case D_COMP_TYPEINFO
:
2631 d_append_string (dpi
, "typeinfo for ");
2632 d_print_comp (dpi
, d_left (dc
));
2635 case D_COMP_TYPEINFO_NAME
:
2636 d_append_string (dpi
, "typeinfo name for ");
2637 d_print_comp (dpi
, d_left (dc
));
2640 case D_COMP_TYPEINFO_FN
:
2641 d_append_string (dpi
, "typeinfo fn for ");
2642 d_print_comp (dpi
, d_left (dc
));
2646 d_append_string (dpi
, "non-virtual thunk to ");
2647 d_print_comp (dpi
, d_left (dc
));
2650 case D_COMP_VIRTUAL_THUNK
:
2651 d_append_string (dpi
, "virtual thunk to ");
2652 d_print_comp (dpi
, d_left (dc
));
2655 case D_COMP_COVARIANT_THUNK
:
2656 d_append_string (dpi
, "covariant return thunk to ");
2657 d_print_comp (dpi
, d_left (dc
));
2660 case D_COMP_JAVA_CLASS
:
2661 d_append_string (dpi
, "java Class for ");
2662 d_print_comp (dpi
, d_left (dc
));
2666 d_append_string (dpi
, "guard variable for ");
2667 d_print_comp (dpi
, d_left (dc
));
2670 case D_COMP_REFTEMP
:
2671 d_append_string (dpi
, "reference temporary for ");
2672 d_print_comp (dpi
, d_left (dc
));
2675 case D_COMP_SUB_STD
:
2676 d_append_string (dpi
, dc
->u
.s_string
.string
);
2679 case D_COMP_RESTRICT
:
2680 case D_COMP_VOLATILE
:
2682 case D_COMP_VENDOR_TYPE_QUAL
:
2683 case D_COMP_POINTER
:
2684 case D_COMP_REFERENCE
:
2685 case D_COMP_COMPLEX
:
2686 case D_COMP_IMAGINARY
:
2688 /* We keep a list of modifiers on the stack. */
2689 struct d_print_mod dpm
;
2691 dpm
.next
= dpi
->modifiers
;
2692 dpi
->modifiers
= &dpm
;
2696 d_print_comp (dpi
, d_left (dc
));
2698 /* If the modifier didn't get printed by the type, print it
2701 d_print_mod (dpi
, dc
);
2703 dpi
->modifiers
= dpm
.next
;
2708 case D_COMP_BUILTIN_TYPE
:
2709 if ((dpi
->options
& DMGL_JAVA
) == 0)
2710 d_append_string (dpi
, dc
->u
.s_builtin
.type
->name
);
2712 d_append_string (dpi
, dc
->u
.s_builtin
.type
->java_name
);
2715 case D_COMP_VENDOR_TYPE
:
2716 d_print_comp (dpi
, d_left (dc
));
2719 case D_COMP_FUNCTION_TYPE
:
2721 if (d_left (dc
) != NULL
)
2723 struct d_print_mod dpm
;
2725 /* We must pass this type down as a modifier in order to
2726 print it in the right location. */
2728 dpm
.next
= dpi
->modifiers
;
2729 dpi
->modifiers
= &dpm
;
2733 d_print_comp (dpi
, d_left (dc
));
2735 dpi
->modifiers
= dpm
.next
;
2740 d_append_char (dpi
, ' ');
2743 d_print_function_type (dpi
, dc
, dpi
->modifiers
);
2748 case D_COMP_ARRAY_TYPE
:
2750 struct d_print_mod dpm
;
2752 /* We must pass this type down as a modifier in order to print
2753 multi-dimensional arrays correctly. */
2755 dpm
.next
= dpi
->modifiers
;
2756 dpi
->modifiers
= &dpm
;
2760 d_print_comp (dpi
, d_right (dc
));
2762 dpi
->modifiers
= dpm
.next
;
2767 d_print_array_type (dpi
, dc
, dpi
->modifiers
);
2772 case D_COMP_PTRMEM_TYPE
:
2774 const struct d_comp
*target_type
;
2775 struct d_print_mod dpm
;
2777 /* Pass the name down to the type so that it can be printed in
2778 the right place for the type. If the type has
2779 CV-qualifiers, they are really method qualifiers; pull them
2780 off now and print them after everything else. */
2781 target_type
= d_right (dc
);
2782 while (target_type
!= NULL
2783 && (target_type
->type
== D_COMP_RESTRICT
2784 || target_type
->type
== D_COMP_VOLATILE
2785 || target_type
->type
== D_COMP_CONST
))
2786 target_type
= d_left (target_type
);
2788 dpm
.next
= dpi
->modifiers
;
2789 dpi
->modifiers
= &dpm
;
2793 d_print_comp (dpi
, target_type
);
2795 /* If the modifier didn't get printed by the type, print it
2799 d_append_char (dpi
, ' ');
2800 d_print_comp (dpi
, d_left (dc
));
2801 d_append_string (dpi
, "::*");
2804 dpi
->modifiers
= dpm
.next
;
2806 /* Now print any CV-qualifiers on the type. */
2807 target_type
= d_right (dc
);
2808 while (target_type
!= NULL
2809 && (target_type
->type
== D_COMP_RESTRICT
2810 || target_type
->type
== D_COMP_VOLATILE
2811 || target_type
->type
== D_COMP_CONST
))
2813 d_print_mod (dpi
, target_type
);
2814 target_type
= d_left (target_type
);
2820 case D_COMP_ARGLIST
:
2821 case D_COMP_TEMPLATE_ARGLIST
:
2822 d_print_comp (dpi
, d_left (dc
));
2823 if (d_right (dc
) != NULL
)
2825 d_append_string (dpi
, ", ");
2826 d_print_comp (dpi
, d_right (dc
));
2830 case D_COMP_OPERATOR
:
2834 d_append_string (dpi
, "operator");
2835 c
= dc
->u
.s_operator
.op
->name
[0];
2836 if (c
>= 'a' && c
<= 'z')
2837 d_append_char (dpi
, ' ');
2838 d_append_string (dpi
, dc
->u
.s_operator
.op
->name
);
2842 case D_COMP_EXTENDED_OPERATOR
:
2843 d_append_string (dpi
, "operator ");
2844 d_print_comp (dpi
, dc
->u
.s_extended_operator
.name
);
2848 d_append_string (dpi
, "operator ");
2849 d_print_cast (dpi
, dc
);
2853 if (d_left (dc
)->type
!= D_COMP_CAST
)
2854 d_print_expr_op (dpi
, d_left (dc
));
2857 d_append_string (dpi
, "((");
2858 d_print_cast (dpi
, d_left (dc
));
2859 d_append_char (dpi
, ')');
2861 d_append_char (dpi
, '(');
2862 d_print_comp (dpi
, d_right (dc
));
2863 d_append_char (dpi
, ')');
2864 if (d_left (dc
)->type
== D_COMP_CAST
)
2865 d_append_char (dpi
, ')');
2869 if (d_right (dc
)->type
!= D_COMP_BINARY_ARGS
)
2871 d_print_error (dpi
);
2874 d_append_char (dpi
, '(');
2875 d_print_comp (dpi
, d_left (d_right (dc
)));
2876 d_append_string (dpi
, ") ");
2877 d_print_expr_op (dpi
, d_left (dc
));
2878 d_append_string (dpi
, " (");
2879 d_print_comp (dpi
, d_right (d_right (dc
)));
2880 d_append_char (dpi
, ')');
2883 case D_COMP_BINARY_ARGS
:
2884 /* We should only see this as part of D_COMP_BINARY. */
2885 d_print_error (dpi
);
2888 case D_COMP_TRINARY
:
2889 if (d_right (dc
)->type
!= D_COMP_TRINARY_ARG1
2890 || d_right (d_right (dc
))->type
!= D_COMP_TRINARY_ARG2
)
2892 d_print_error (dpi
);
2895 d_append_char (dpi
, '(');
2896 d_print_comp (dpi
, d_left (d_right (dc
)));
2897 d_append_string (dpi
, ") ");
2898 d_print_expr_op (dpi
, d_left (dc
));
2899 d_append_string (dpi
, " (");
2900 d_print_comp (dpi
, d_left (d_right (d_right (dc
))));
2901 d_append_string (dpi
, ") : (");
2902 d_print_comp (dpi
, d_right (d_right (d_right (dc
))));
2903 d_append_char (dpi
, ')');
2906 case D_COMP_TRINARY_ARG1
:
2907 case D_COMP_TRINARY_ARG2
:
2908 /* We should only see these are part of D_COMP_TRINARY. */
2909 d_print_error (dpi
);
2912 case D_COMP_LITERAL
:
2913 /* For some builtin types, produce simpler output. */
2914 if (d_left (dc
)->type
== D_COMP_BUILTIN_TYPE
)
2916 switch (d_left (dc
)->u
.s_builtin
.type
->print
)
2919 if (d_right (dc
)->type
== D_COMP_NAME
)
2921 d_print_comp (dpi
, d_right (dc
));
2927 if (d_right (dc
)->type
== D_COMP_NAME
)
2929 d_print_comp (dpi
, d_right (dc
));
2930 d_append_char (dpi
, 'l');
2936 if (d_right (dc
)->type
== D_COMP_NAME
2937 && d_right (dc
)->u
.s_name
.len
== 1)
2939 switch (d_right (dc
)->u
.s_name
.s
[0])
2942 d_append_string (dpi
, "false");
2945 d_append_string (dpi
, "true");
2958 d_append_char (dpi
, '(');
2959 d_print_comp (dpi
, d_left (dc
));
2960 d_append_char (dpi
, ')');
2961 d_print_comp (dpi
, d_right (dc
));
2965 d_print_error (dpi
);
2970 /* Print an identifier. */
2973 d_print_identifier (dpi
, name
, len
)
2974 struct d_print_info
*dpi
;
2978 if ((dpi
->options
& DMGL_JAVA
) == 0)
2979 d_append_buffer (dpi
, name
, len
);
2985 /* For Java we try to handle encoded extended Unicode
2986 characters. The C++ ABI doesn't mention Unicode encoding, so
2987 we don't it for C++. Characters are encoded as
2990 for (p
= name
; p
< end
; ++p
)
3001 for (q
= p
+ 3; q
< end
; ++q
)
3005 if (*q
>= '0' && *q
<= '9')
3007 else if (*q
>= 'A' && *q
<= 'F')
3008 dig
= *q
- 'A' + 10;
3009 else if (*q
>= 'a' && *q
<= 'f')
3010 dig
= *q
- 'a' + 10;
3016 /* If the Unicode character is larger than 256, we don't
3017 try to deal with it here. FIXME. */
3018 if (q
< end
&& *q
== '_' && c
< 256)
3020 d_append_char (dpi
, c
);
3026 d_append_char (dpi
, *p
);
3031 /* Print a list of modifiers. */
3034 d_print_mod_list (dpi
, mods
)
3035 struct d_print_info
*dpi
;
3036 struct d_print_mod
*mods
;
3038 if (mods
== NULL
|| mods
->printed
|| d_print_saw_error (dpi
))
3041 if (mods
->mod
->type
== D_COMP_FUNCTION_TYPE
)
3044 d_print_function_type (dpi
, mods
->mod
, mods
->next
);
3047 else if (mods
->mod
->type
== D_COMP_ARRAY_TYPE
)
3050 d_print_array_type (dpi
, mods
->mod
, mods
->next
);
3056 d_print_mod (dpi
, mods
->mod
);
3058 d_print_mod_list (dpi
, mods
->next
);
3061 /* Print a modifier. */
3064 d_print_mod (dpi
, mod
)
3065 struct d_print_info
*dpi
;
3066 const struct d_comp
*mod
;
3070 case D_COMP_RESTRICT
:
3071 d_append_string (dpi
, " restrict");
3073 case D_COMP_VOLATILE
:
3074 d_append_string (dpi
, " volatile");
3077 d_append_string (dpi
, " const");
3079 case D_COMP_VENDOR_TYPE_QUAL
:
3080 d_append_char (dpi
, ' ');
3081 d_print_comp (dpi
, d_right (mod
));
3083 case D_COMP_POINTER
:
3084 /* There is no pointer symbol in Java. */
3085 if ((dpi
->options
& DMGL_JAVA
) == 0)
3086 d_append_char (dpi
, '*');
3088 case D_COMP_REFERENCE
:
3089 d_append_char (dpi
, '&');
3091 case D_COMP_COMPLEX
:
3092 d_append_string (dpi
, "complex ");
3094 case D_COMP_IMAGINARY
:
3095 d_append_string (dpi
, "imaginary ");
3097 case D_COMP_PTRMEM_TYPE
:
3098 if (dpi
->buf
[dpi
->len
- 1] != '(')
3099 d_append_char (dpi
, ' ');
3100 d_print_comp (dpi
, d_left (mod
));
3101 d_append_string (dpi
, "::*");
3103 case D_COMP_TYPED_NAME
:
3104 d_print_comp (dpi
, d_left (mod
));
3107 /* Otherwise, we have something that won't go back on the
3108 modifier stack, so we can just print it. */
3109 d_print_comp (dpi
, mod
);
3114 /* Print a function type, except for the return type. */
3117 d_print_function_type (dpi
, dc
, mods
)
3118 struct d_print_info
*dpi
;
3119 const struct d_comp
*dc
;
3120 struct d_print_mod
*mods
;
3126 struct d_print_mod
*p
;
3130 for (p
= mods
; p
!= NULL
; p
= p
->next
)
3136 switch (p
->mod
->type
)
3138 case D_COMP_RESTRICT
:
3139 case D_COMP_VOLATILE
:
3141 case D_COMP_VENDOR_TYPE_QUAL
:
3142 case D_COMP_POINTER
:
3143 case D_COMP_REFERENCE
:
3144 case D_COMP_COMPLEX
:
3145 case D_COMP_IMAGINARY
:
3146 case D_COMP_PTRMEM_TYPE
:
3156 if (d_left (dc
) != NULL
&& ! saw_mod
)
3160 d_append_char (dpi
, '(');
3162 d_print_mod_list (dpi
, mods
);
3165 d_append_char (dpi
, ')');
3168 d_append_char (dpi
, '(');
3170 if (d_right (dc
) != NULL
)
3171 d_print_comp (dpi
, d_right (dc
));
3173 d_append_char (dpi
, ')');
3176 /* Print an array type, except for the element type. */
3179 d_print_array_type (dpi
, dc
, mods
)
3180 struct d_print_info
*dpi
;
3181 const struct d_comp
*dc
;
3182 struct d_print_mod
*mods
;
3190 struct d_print_mod
*p
;
3193 for (p
= mods
; p
!= NULL
; p
= p
->next
)
3198 if (p
->mod
->type
== D_COMP_ARRAY_TYPE
)
3212 d_append_string (dpi
, " (");
3214 d_print_mod_list (dpi
, mods
);
3217 d_append_char (dpi
, ')');
3221 d_append_char (dpi
, ' ');
3223 d_append_char (dpi
, '[');
3225 if (d_left (dc
) != NULL
)
3226 d_print_comp (dpi
, d_left (dc
));
3228 d_append_char (dpi
, ']');
3231 /* Print an operator in an expression. */
3234 d_print_expr_op (dpi
, dc
)
3235 struct d_print_info
*dpi
;
3236 const struct d_comp
*dc
;
3238 if (dc
->type
== D_COMP_OPERATOR
)
3239 d_append_string (dpi
, dc
->u
.s_operator
.op
->name
);
3241 d_print_comp (dpi
, dc
);
3247 d_print_cast (dpi
, dc
)
3248 struct d_print_info
*dpi
;
3249 const struct d_comp
*dc
;
3251 if (d_left (dc
)->type
!= D_COMP_TEMPLATE
)
3252 d_print_comp (dpi
, d_left (dc
));
3255 struct d_print_template dpt
;
3257 /* It appears that for a templated cast operator, we need to put
3258 the template parameters in scope for the operator name, but
3259 not for the parameters. The effect is that we need to handle
3260 the template printing here. FIXME: Verify this. */
3262 dpt
.next
= dpi
->templates
;
3263 dpi
->templates
= &dpt
;
3264 dpt
.template = d_left (dc
);
3266 d_print_comp (dpi
, d_left (d_left (dc
)));
3268 dpi
->templates
= dpt
.next
;
3270 d_append_char (dpi
, '<');
3271 d_print_comp (dpi
, d_right (d_left (dc
)));
3272 /* Avoid generating two consecutive '>' characters, to avoid
3273 the C++ syntactic ambiguity. */
3274 if (dpi
->buf
[dpi
->len
- 1] == '>')
3275 d_append_char (dpi
, ' ');
3276 d_append_char (dpi
, '>');
3280 /* Initialize the information structure we use to pass around
3284 d_init_info (mangled
, options
, len
, di
)
3285 const char *mangled
;
3291 di
->options
= options
;
3295 /* We can not need more components than twice the number of chars in
3296 the mangled string. Most components correspond directly to
3297 chars, but the ARGLIST types are exceptions. */
3298 di
->num_comps
= 2 * len
;
3299 di
->comps
= (struct d_comp
*) malloc (di
->num_comps
3300 * sizeof (struct d_comp
));
3303 /* Similarly, we can not need more substitutions than there are
3304 chars in the mangled string divided by 2, since it takes at least
3305 two chars to refer to a substitution. */
3306 di
->num_subs
= (len
+ 1) / 2;
3307 di
->subs
= (struct d_comp
**) malloc (di
->num_subs
3308 * sizeof (struct d_comp
*));
3311 di
->last_name
= NULL
;
3313 if (di
->comps
== NULL
|| di
->subs
== NULL
)
3315 if (di
->comps
!= NULL
)
3317 if (di
->subs
!= NULL
)
3325 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
3326 name, return a buffer allocated with malloc holding the demangled
3327 name. OPTIONS is the usual libiberty demangler options. On
3328 success, this sets *PALC to the allocated size of the returned
3329 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
3330 a memory allocation failure. On failure, this returns NULL. */
3333 d_demangle (mangled
, options
, palc
)
3334 const char* mangled
;
3346 len
= strlen (mangled
);
3348 if (mangled
[0] == '_' && mangled
[1] == 'Z')
3350 else if (strncmp (mangled
, "_GLOBAL_", 8) == 0
3351 && (mangled
[8] == '.' || mangled
[8] == '_' || mangled
[8] == '$')
3352 && (mangled
[9] == 'D' || mangled
[9] == 'I')
3353 && mangled
[10] == '_')
3357 r
= malloc (40 + len
- 11);
3362 if (mangled
[9] == 'I')
3363 strcpy (r
, "global constructors keyed to ");
3365 strcpy (r
, "global destructors keyed to ");
3366 strcat (r
, mangled
+ 11);
3372 if ((options
& DMGL_TYPES
) == 0)
3377 if (! d_init_info (mangled
, options
, len
, &di
))
3384 dc
= d_mangled_name (&di
);
3388 #ifdef CP_DEMANGLE_DEBUG
3390 printf ("failed demangling\n");
3400 ret
= d_print (options
, dc
, palc
);
3407 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
3409 extern char *__cxa_demangle
PARAMS ((const char *, char *, size_t *, int *));
3411 /* ia64 ABI-mandated entry point in the C++ runtime library for
3412 performing demangling. MANGLED_NAME is a NUL-terminated character
3413 string containing the name to be demangled.
3415 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
3416 *LENGTH bytes, into which the demangled name is stored. If
3417 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
3418 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
3419 is placed in a region of memory allocated with malloc.
3421 If LENGTH is non-NULL, the length of the buffer conaining the
3422 demangled name, is placed in *LENGTH.
3424 The return value is a pointer to the start of the NUL-terminated
3425 demangled name, or NULL if the demangling fails. The caller is
3426 responsible for deallocating this memory using free.
3428 *STATUS is set to one of the following values:
3429 0: The demangling operation succeeded.
3430 -1: A memory allocation failure occurred.
3431 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
3432 -3: One of the arguments is invalid.
3434 The demangling is performed using the C++ ABI mangling rules, with
3438 __cxa_demangle (mangled_name
, output_buffer
, length
, status
)
3439 const char *mangled_name
;
3440 char *output_buffer
;
3450 if (mangled_name
== NULL
)
3456 if (output_buffer
!= NULL
&& length
== NULL
)
3462 demangled
= d_demangle (mangled_name
, DMGL_TYPES
, &alc
);
3464 if (demangled
== NULL
)
3473 if (output_buffer
== NULL
)
3480 if (strlen (demangled
) < *length
)
3482 strcpy (output_buffer
, demangled
);
3484 demangled
= output_buffer
;
3488 free (output_buffer
);
3498 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
3500 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
3501 mangled name, return a buffer allocated with malloc holding the
3502 demangled name. Otherwise, return NULL. */
3505 cplus_demangle_v3 (mangled
, options
)
3506 const char* mangled
;
3511 return d_demangle (mangled
, options
, &alc
);
3514 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
3515 conventions, but the output formatting is a little different.
3516 This instructs the C++ demangler not to emit pointer characters ("*"), and
3517 to use Java's namespace separator symbol ("." instead of "::"). It then
3518 does an additional pass over the demangled output to replace instances
3519 of JArray<TYPE> with TYPE[]. */
3522 java_demangle_v3 (mangled
)
3523 const char* mangled
;
3531 demangled
= d_demangle (mangled
, DMGL_JAVA
| DMGL_PARAMS
, &alc
);
3533 if (demangled
== NULL
)
3539 while (*from
!= '\0')
3541 if (strncmp (from
, "JArray<", 7) == 0)
3546 else if (nesting
> 0 && *from
== '>')
3548 while (to
> demangled
&& to
[-1] == ' ')
3564 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
3566 #ifndef IN_GLIBCPP_V3
3568 /* Demangle a string in order to find out whether it is a constructor
3569 or destructor. Return non-zero on success. Set *CTOR_KIND and
3570 *DTOR_KIND appropriately. */
3573 is_ctor_or_dtor (mangled
, ctor_kind
, dtor_kind
)
3574 const char *mangled
;
3575 enum gnu_v3_ctor_kinds
*ctor_kind
;
3576 enum gnu_v3_dtor_kinds
*dtor_kind
;
3581 *ctor_kind
= (enum gnu_v3_ctor_kinds
) 0;
3582 *dtor_kind
= (enum gnu_v3_dtor_kinds
) 0;
3584 if (! d_init_info (mangled
, DMGL_GNU_V3
, strlen (mangled
), &di
))
3587 dc
= d_mangled_name (&di
);
3598 case D_COMP_TYPED_NAME
:
3599 case D_COMP_TEMPLATE
:
3600 case D_COMP_RESTRICT
:
3601 case D_COMP_VOLATILE
:
3603 case D_COMP_VENDOR_TYPE_QUAL
:
3606 case D_COMP_QUAL_NAME
:
3610 *ctor_kind
= dc
->u
.s_ctor
.kind
;
3613 *dtor_kind
= dc
->u
.s_dtor
.kind
;
3621 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
3622 name. A non-zero return indicates the type of constructor. */
3624 enum gnu_v3_ctor_kinds
3625 is_gnu_v3_mangled_ctor (name
)
3628 enum gnu_v3_ctor_kinds ctor_kind
;
3629 enum gnu_v3_dtor_kinds dtor_kind
;
3631 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
3632 return (enum gnu_v3_ctor_kinds
) 0;
3637 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
3638 name. A non-zero return indicates the type of destructor. */
3640 enum gnu_v3_dtor_kinds
3641 is_gnu_v3_mangled_dtor (name
)
3644 enum gnu_v3_ctor_kinds ctor_kind
;
3645 enum gnu_v3_dtor_kinds dtor_kind
;
3647 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
3648 return (enum gnu_v3_dtor_kinds
) 0;
3652 #endif /* IN_GLIBCPP_V3 */
3654 #ifdef STANDALONE_DEMANGLER
3657 #include "dyn-string.h"
3659 static void print_usage
PARAMS ((FILE* fp
, int exit_value
));
3661 #define IS_ALPHA(CHAR) \
3662 (((CHAR) >= 'a' && (CHAR) <= 'z') \
3663 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
3665 /* Non-zero if CHAR is a character than can occur in a mangled name. */
3666 #define is_mangled_char(CHAR) \
3667 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
3668 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
3670 /* The name of this program, as invoked. */
3671 const char* program_name
;
3673 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
3676 print_usage (fp
, exit_value
)
3680 fprintf (fp
, "Usage: %s [options] [names ...]\n", program_name
);
3681 fprintf (fp
, "Options:\n");
3682 fprintf (fp
, " -h,--help Display this message.\n");
3683 fprintf (fp
, " -p,--no-params Don't display function parameters\n");
3684 fprintf (fp
, " -v,--verbose Produce verbose demanglings.\n");
3685 fprintf (fp
, "If names are provided, they are demangled. Otherwise filters standard input.\n");
3690 /* Option specification for getopt_long. */
3691 static const struct option long_options
[] =
3693 { "help", no_argument
, NULL
, 'h' },
3694 { "no-params", no_argument
, NULL
, 'p' },
3695 { "verbose", no_argument
, NULL
, 'v' },
3696 { NULL
, no_argument
, NULL
, 0 },
3699 /* Main entry for a demangling filter executable. It will demangle
3700 its command line arguments, if any. If none are provided, it will
3701 filter stdin to stdout, replacing any recognized mangled C++ names
3702 with their demangled equivalents. */
3711 int options
= DMGL_PARAMS
| DMGL_ANSI
| DMGL_TYPES
;
3713 /* Use the program name of this program, as invoked. */
3714 program_name
= argv
[0];
3716 /* Parse options. */
3719 opt_char
= getopt_long (argc
, argv
, "hpv", long_options
, NULL
);
3722 case '?': /* Unrecognized option. */
3723 print_usage (stderr
, 1);
3727 print_usage (stdout
, 0);
3731 options
&= ~ DMGL_PARAMS
;
3735 options
|= DMGL_VERBOSE
;
3739 while (opt_char
!= -1);
3742 /* No command line arguments were provided. Filter stdin. */
3744 dyn_string_t mangled
= dyn_string_new (3);
3747 /* Read all of input. */
3748 while (!feof (stdin
))
3752 /* Pile characters into mangled until we hit one that can't
3753 occur in a mangled name. */
3755 while (!feof (stdin
) && is_mangled_char (c
))
3757 dyn_string_append_char (mangled
, c
);
3763 if (dyn_string_length (mangled
) > 0)
3765 s
= cplus_demangle_v3 (dyn_string_buf (mangled
), options
);
3774 /* It might not have been a mangled name. Print the
3776 fputs (dyn_string_buf (mangled
), stdout
);
3779 dyn_string_clear (mangled
);
3782 /* If we haven't hit EOF yet, we've read one character that
3783 can't occur in a mangled name, so print it out. */
3788 dyn_string_delete (mangled
);
3791 /* Demangle command line arguments. */
3793 /* Loop over command line arguments. */
3794 for (i
= optind
; i
< argc
; ++i
)
3798 /* Attempt to demangle. */
3799 s
= cplus_demangle_v3 (argv
[i
], options
);
3801 /* If it worked, print the demangled name. */
3808 fprintf (stderr
, "Failed: %s\n", argv
[i
]);
3815 #endif /* STANDALONE_DEMANGLER */