1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003, 2004 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@wasabisystems.com>.
5 This file is part of the libiberty library, which is part of GCC.
7 This file is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combined
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 /* This code implements a demangler for the g++ V3 ABI. The ABI is
32 described on this web page:
33 http://www.codesourcery.com/cxx-abi/abi.html#mangling
35 This code was written while looking at the demangler written by
36 Alex Samuel <samuel@codesourcery.com>.
38 This code first pulls the mangled name apart into a list of
39 components, and then walks the list generating the demangled
42 This file will normally define the following functions, q.v.:
43 char *cplus_demangle_v3(const char *mangled, int options)
44 char *java_demangle_v3(const char *mangled)
45 enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
46 enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
48 Preprocessor macros you can define while compiling this file:
51 If defined, this file defines the following function, q.v.:
52 char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
54 instead of cplus_demangle_v3() and java_demangle_v3().
57 If defined, this file defines only __cxa_demangle().
60 If defined, this file defines a main() function which demangles
61 any arguments, or, if none, demangles stdin.
64 If defined, turns on debugging mode, which prints information on
65 stdout about the mangled string. This is not generally useful.
82 #include "libiberty.h"
85 /* See if the compiler supports dynamic arrays. */
88 #define CP_DYNAMIC_ARRAYS
91 #ifdef __STDC_VERSION__
92 #if __STDC_VERSION__ >= 199901L
93 #define CP_DYNAMIC_ARRAYS
94 #endif /* __STDC__VERSION >= 199901L */
95 #endif /* defined (__STDC_VERSION__) */
96 #endif /* defined (__STDC__) */
97 #endif /* ! defined (__GNUC__) */
99 /* We avoid pulling in the ctype tables, to prevent pulling in
100 additional unresolved symbols when this code is used in a library.
101 FIXME: Is this really a valid reason? This comes from the original
104 As of this writing this file has the following undefined references
105 when compiled with -DIN_GLIBCPP_V3: malloc, realloc, free, memcpy,
106 strcpy, strcat, strlen. */
108 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
109 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
110 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
112 /* The prefix prepended by GCC to an identifier represnting the
113 anonymous namespace. */
114 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
115 #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
116 (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
118 /* Information we keep for operators. */
120 struct d_operator_info
126 /* Length of real name. */
128 /* Number of arguments. */
132 /* How to print the value of a builtin type. */
134 enum d_builtin_type_print
136 /* Print as (type)val. */
138 /* Print as integer. */
140 /* Print as long, with trailing `l'. */
144 /* Print in usual way, but here to detect void. */
148 /* Information we keep for a builtin type. */
150 struct d_builtin_type_info
154 /* Length of type name. */
156 /* Type name when using Java. */
157 const char *java_name
;
158 /* Length of java name. */
160 /* How to print a value of this type. */
161 enum d_builtin_type_print print
;
164 /* Information we keep for the standard substitutions. */
166 struct d_standard_sub_info
168 /* The code for this substitution. */
170 /* The simple string it expands to. */
171 const char *simple_expansion
;
172 /* The length of the simple expansion. */
174 /* The results of a full, verbose, expansion. This is used when
175 qualifying a constructor/destructor, or when in verbose mode. */
176 const char *full_expansion
;
177 /* The length of the full expansion. */
179 /* What to set the last_name field of d_info to; NULL if we should
180 not set it. This is only relevant when qualifying a
181 constructor/destructor. */
182 const char *set_last_name
;
183 /* The length of set_last_name. */
184 int set_last_name_len
;
187 /* Component types found in mangled names. */
193 /* A qualified name. */
201 /* A template parameter. */
202 D_COMP_TEMPLATE_PARAM
,
209 /* A VTT structure. */
211 /* A construction vtable. */
212 D_COMP_CONSTRUCTION_VTABLE
,
213 /* A typeinfo structure. */
215 /* A typeinfo name. */
216 D_COMP_TYPEINFO_NAME
,
217 /* A typeinfo function. */
221 /* A virtual thunk. */
222 D_COMP_VIRTUAL_THUNK
,
223 /* A covariant thunk. */
224 D_COMP_COVARIANT_THUNK
,
227 /* A guard variable. */
229 /* A reference temporary. */
231 /* A standard substitution. */
233 /* The restrict qualifier. */
235 /* The volatile qualifier. */
237 /* The const qualifier. */
239 /* The restrict qualifier modifying a member function. */
240 D_COMP_RESTRICT_THIS
,
241 /* The volatile qualifier modifying a member function. */
242 D_COMP_VOLATILE_THIS
,
243 /* The const qualifier modifying a member function. */
245 /* A vendor qualifier. */
246 D_COMP_VENDOR_TYPE_QUAL
,
251 /* A complex type. */
253 /* An imaginary type. */
255 /* A builtin type. */
257 /* A vendor's builtin type. */
259 /* A function type. */
260 D_COMP_FUNCTION_TYPE
,
263 /* A pointer to member type. */
265 /* An argument list. */
267 /* A template argument list. */
268 D_COMP_TEMPLATE_ARGLIST
,
271 /* An extended operator. */
272 D_COMP_EXTENDED_OPERATOR
,
275 /* A unary expression. */
277 /* A binary expression. */
279 /* Arguments to a binary expression. */
281 /* A trinary expression. */
283 /* Arguments to a trinary expression. */
288 /* A negative literal. */
292 /* A component of the mangled name. */
296 /* The type of this component. */
297 enum d_comp_type type
;
300 /* For D_COMP_NAME. */
303 /* A pointer to the name (not NULL terminated) and it's
309 /* For D_COMP_OPERATOR. */
313 const struct d_operator_info
*op
;
316 /* For D_COMP_EXTENDED_OPERATOR. */
319 /* Number of arguments. */
323 } s_extended_operator
;
325 /* For D_COMP_CTOR. */
328 enum gnu_v3_ctor_kinds kind
;
332 /* For D_COMP_DTOR. */
335 enum gnu_v3_dtor_kinds kind
;
339 /* For D_COMP_BUILTIN_TYPE. */
342 const struct d_builtin_type_info
*type
;
345 /* For D_COMP_SUB_STD. */
352 /* For D_COMP_TEMPLATE_PARAM. */
358 /* For other types. */
362 struct d_comp
*right
;
368 #define d_left(dc) ((dc)->u.s_binary.left)
369 #define d_right(dc) ((dc)->u.s_binary.right)
371 /* The information structure we pass around. */
375 /* The string we are demangling. */
377 /* The end of the string we are demangling. */
379 /* The options passed to the demangler. */
381 /* The next character in the string to consider. */
383 /* The array of components. */
384 struct d_comp
*comps
;
385 /* The index of the next available component. */
387 /* The number of available component structures. */
389 /* The array of substitutions. */
390 struct d_comp
**subs
;
391 /* The index of the next substitution. */
393 /* The number of available entries in the subs array. */
395 /* The number of substitutions which we actually made from the subs
396 array, plus the number of template parameter references we
399 /* The last name we saw, for constructors and destructors. */
400 struct d_comp
*last_name
;
401 /* A running total of the length of large expansions from the
402 mangled name to the demangled name, such as standard
403 substitutions and builtin types. */
407 #define d_peek_char(di) (*((di)->n))
408 #define d_peek_next_char(di) ((di)->n[1])
409 #define d_advance(di, i) ((di)->n += (i))
410 #define d_next_char(di) (*((di)->n++))
411 #define d_str(di) ((di)->n)
413 /* A list of templates. This is used while printing. */
415 struct d_print_template
417 /* Next template on the list. */
418 struct d_print_template
*next
;
420 const struct d_comp
*template;
423 /* A list of type modifiers. This is used while printing. */
427 /* Next modifier on the list. These are in the reverse of the order
428 in which they appeared in the mangled string. */
429 struct d_print_mod
*next
;
431 const struct d_comp
*mod
;
432 /* Whether this modifier was printed. */
434 /* The list of templates which applies to this modifier. */
435 struct d_print_template
*templates
;
438 /* We use this structure to hold information during printing. */
442 /* The options passed to the demangler. */
444 /* Buffer holding the result. */
446 /* Current length of data in buffer. */
448 /* Allocated size of buffer. */
450 /* The current list of templates, if any. */
451 struct d_print_template
*templates
;
452 /* The current list of modifiers (e.g., pointer, reference, etc.),
454 struct d_print_mod
*modifiers
;
455 /* Set to 1 if we had a memory allocation failure. */
456 int allocation_failure
;
459 #define d_print_saw_error(dpi) ((dpi)->buf == NULL)
461 #define d_append_char(dpi, c) \
464 if ((dpi)->buf != NULL && (dpi)->len < (dpi)->alc) \
465 (dpi)->buf[(dpi)->len++] = (c); \
467 d_print_append_char ((dpi), (c)); \
471 #define d_append_buffer(dpi, s, l) \
474 if ((dpi)->buf != NULL && (dpi)->len + (l) <= (dpi)->alc) \
476 memcpy ((dpi)->buf + (dpi)->len, (s), (l)); \
480 d_print_append_buffer ((dpi), (s), (l)); \
484 #define d_append_string_constant(dpi, s) \
485 d_append_buffer (dpi, (s), sizeof (s) - 1)
487 #define d_last_char(dpi) \
488 ((dpi)->buf == NULL || (dpi)->len == 0 ? '\0' : (dpi)->buf[(dpi)->len - 1])
490 #ifdef CP_DEMANGLE_DEBUG
491 static void d_dump
PARAMS ((struct d_comp
*, int));
493 static struct d_comp
*d_make_empty
PARAMS ((struct d_info
*,
495 static struct d_comp
*d_make_comp
PARAMS ((struct d_info
*, enum d_comp_type
,
496 struct d_comp
*, struct d_comp
*));
497 static struct d_comp
*d_make_name
PARAMS ((struct d_info
*, const char *,
499 static struct d_comp
*d_make_builtin_type
PARAMS ((struct d_info
*,
500 const struct d_builtin_type_info
*));
501 static struct d_comp
*d_make_operator
PARAMS ((struct d_info
*,
502 const struct d_operator_info
*));
503 static struct d_comp
*d_make_extended_operator
PARAMS ((struct d_info
*,
506 static struct d_comp
*d_make_ctor
PARAMS ((struct d_info
*,
507 enum gnu_v3_ctor_kinds
,
509 static struct d_comp
*d_make_dtor
PARAMS ((struct d_info
*,
510 enum gnu_v3_dtor_kinds
,
512 static struct d_comp
*d_make_template_param
PARAMS ((struct d_info
*, long));
513 static struct d_comp
*d_make_sub
PARAMS ((struct d_info
*, const char *, int));
514 static struct d_comp
*d_mangled_name
PARAMS ((struct d_info
*, int));
515 static int has_return_type
PARAMS ((struct d_comp
*));
516 static int is_ctor_dtor_or_conversion
PARAMS ((struct d_comp
*));
517 static struct d_comp
*d_encoding
PARAMS ((struct d_info
*, int));
518 static struct d_comp
*d_name
PARAMS ((struct d_info
*));
519 static struct d_comp
*d_nested_name
PARAMS ((struct d_info
*));
520 static struct d_comp
*d_prefix
PARAMS ((struct d_info
*));
521 static struct d_comp
*d_unqualified_name
PARAMS ((struct d_info
*));
522 static struct d_comp
*d_source_name
PARAMS ((struct d_info
*));
523 static long d_number
PARAMS ((struct d_info
*));
524 static struct d_comp
*d_identifier
PARAMS ((struct d_info
*, int));
525 static struct d_comp
*d_operator_name
PARAMS ((struct d_info
*));
526 static struct d_comp
*d_special_name
PARAMS ((struct d_info
*));
527 static int d_call_offset
PARAMS ((struct d_info
*, int));
528 static struct d_comp
*d_ctor_dtor_name
PARAMS ((struct d_info
*));
529 static struct d_comp
*d_type
PARAMS ((struct d_info
*));
530 static struct d_comp
**d_cv_qualifiers
PARAMS ((struct d_info
*,
531 struct d_comp
**, int));
532 static struct d_comp
*d_function_type
PARAMS ((struct d_info
*));
533 static struct d_comp
*d_bare_function_type
PARAMS ((struct d_info
*, int));
534 static struct d_comp
*d_class_enum_type
PARAMS ((struct d_info
*));
535 static struct d_comp
*d_array_type
PARAMS ((struct d_info
*));
536 static struct d_comp
*d_pointer_to_member_type
PARAMS ((struct d_info
*));
537 static struct d_comp
*d_template_param
PARAMS ((struct d_info
*));
538 static struct d_comp
*d_template_args
PARAMS ((struct d_info
*));
539 static struct d_comp
*d_template_arg
PARAMS ((struct d_info
*));
540 static struct d_comp
*d_expression
PARAMS ((struct d_info
*));
541 static struct d_comp
*d_expr_primary
PARAMS ((struct d_info
*));
542 static struct d_comp
*d_local_name
PARAMS ((struct d_info
*));
543 static int d_discriminator
PARAMS ((struct d_info
*));
544 static int d_add_substitution
PARAMS ((struct d_info
*, struct d_comp
*));
545 static struct d_comp
*d_substitution
PARAMS ((struct d_info
*, int));
546 static void d_print_resize
PARAMS ((struct d_print_info
*, size_t));
547 static void d_print_append_char
PARAMS ((struct d_print_info
*, int));
548 static void d_print_append_buffer
PARAMS ((struct d_print_info
*, const char *,
550 static void d_print_error
PARAMS ((struct d_print_info
*));
551 static char *d_print
PARAMS ((int, const struct d_comp
*, int, size_t *));
552 static void d_print_comp
PARAMS ((struct d_print_info
*,
553 const struct d_comp
*));
554 static void d_print_java_identifier
PARAMS ((struct d_print_info
*,
556 static void d_print_mod_list
PARAMS ((struct d_print_info
*,
557 struct d_print_mod
*, int));
558 static void d_print_mod
PARAMS ((struct d_print_info
*,
559 const struct d_comp
*));
560 static void d_print_function_type
PARAMS ((struct d_print_info
*,
561 const struct d_comp
*,
562 struct d_print_mod
*));
563 static void d_print_array_type
PARAMS ((struct d_print_info
*,
564 const struct d_comp
*,
565 struct d_print_mod
*));
566 static void d_print_expr_op
PARAMS ((struct d_print_info
*,
567 const struct d_comp
*));
568 static void d_print_cast
PARAMS ((struct d_print_info
*,
569 const struct d_comp
*));
570 static void d_init_info
PARAMS ((const char *, int, size_t, struct d_info
*));
571 static char *d_demangle
PARAMS ((const char *, int, size_t *));
573 #ifdef CP_DEMANGLE_DEBUG
585 for (i
= 0; i
< indent
; ++i
)
591 printf ("name '%.*s'\n", dc
->u
.s_name
.len
, dc
->u
.s_name
.s
);
593 case D_COMP_TEMPLATE_PARAM
:
594 printf ("template parameter %ld\n", dc
->u
.s_number
.number
);
597 printf ("constructor %d\n", (int) dc
->u
.s_ctor
.kind
);
598 d_dump (dc
->u
.s_ctor
.name
, indent
+ 2);
601 printf ("destructor %d\n", (int) dc
->u
.s_dtor
.kind
);
602 d_dump (dc
->u
.s_dtor
.name
, indent
+ 2);
605 printf ("standard substitution %s\n", dc
->u
.s_string
.string
);
607 case D_COMP_BUILTIN_TYPE
:
608 printf ("builtin type %s\n", dc
->u
.s_builtin
.type
->name
);
610 case D_COMP_OPERATOR
:
611 printf ("operator %s\n", dc
->u
.s_operator
.op
->name
);
613 case D_COMP_EXTENDED_OPERATOR
:
614 printf ("extended operator with %d args\n",
615 dc
->u
.s_extended_operator
.args
);
616 d_dump (dc
->u
.s_extended_operator
.name
, indent
+ 2);
619 case D_COMP_QUAL_NAME
:
620 printf ("qualified name\n");
622 case D_COMP_LOCAL_NAME
:
623 printf ("local name\n");
625 case D_COMP_TYPED_NAME
:
626 printf ("typed name\n");
628 case D_COMP_TEMPLATE
:
629 printf ("template\n");
637 case D_COMP_CONSTRUCTION_VTABLE
:
638 printf ("construction vtable\n");
640 case D_COMP_TYPEINFO
:
641 printf ("typeinfo\n");
643 case D_COMP_TYPEINFO_NAME
:
644 printf ("typeinfo name\n");
646 case D_COMP_TYPEINFO_FN
:
647 printf ("typeinfo function\n");
652 case D_COMP_VIRTUAL_THUNK
:
653 printf ("virtual thunk\n");
655 case D_COMP_COVARIANT_THUNK
:
656 printf ("covariant thunk\n");
658 case D_COMP_JAVA_CLASS
:
659 printf ("java class\n");
665 printf ("reference temporary\n");
667 case D_COMP_RESTRICT
:
668 printf ("restrict\n");
670 case D_COMP_VOLATILE
:
671 printf ("volatile\n");
676 case D_COMP_RESTRICT_THIS
:
677 printf ("restrict this\n");
679 case D_COMP_VOLATILE_THIS
:
680 printf ("volatile this\n");
682 case D_COMP_CONST_THIS
:
683 printf ("const this\n");
685 case D_COMP_VENDOR_TYPE_QUAL
:
686 printf ("vendor type qualifier\n");
689 printf ("pointer\n");
691 case D_COMP_REFERENCE
:
692 printf ("reference\n");
695 printf ("complex\n");
697 case D_COMP_IMAGINARY
:
698 printf ("imaginary\n");
700 case D_COMP_VENDOR_TYPE
:
701 printf ("vendor type\n");
703 case D_COMP_FUNCTION_TYPE
:
704 printf ("function type\n");
706 case D_COMP_ARRAY_TYPE
:
707 printf ("array type\n");
709 case D_COMP_PTRMEM_TYPE
:
710 printf ("pointer to member type\n");
713 printf ("argument list\n");
715 case D_COMP_TEMPLATE_ARGLIST
:
716 printf ("template argument list\n");
722 printf ("unary operator\n");
725 printf ("binary operator\n");
727 case D_COMP_BINARY_ARGS
:
728 printf ("binary operator arguments\n");
731 printf ("trinary operator\n");
733 case D_COMP_TRINARY_ARG1
:
734 printf ("trinary operator arguments 1\n");
736 case D_COMP_TRINARY_ARG2
:
737 printf ("trinary operator arguments 1\n");
740 printf ("literal\n");
742 case D_COMP_LITERAL_NEG
:
743 printf ("negative literal\n");
747 d_dump (d_left (dc
), indent
+ 2);
748 d_dump (d_right (dc
), indent
+ 2);
751 #endif /* CP_DEMANGLE_DEBUG */
753 /* Add a new component. */
755 static struct d_comp
*
756 d_make_empty (di
, type
)
758 enum d_comp_type type
;
762 if (di
->next_comp
>= di
->num_comps
)
764 p
= &di
->comps
[di
->next_comp
];
770 /* Add a new generic component. */
772 static struct d_comp
*
773 d_make_comp (di
, type
, left
, right
)
775 enum d_comp_type type
;
777 struct d_comp
*right
;
781 /* We check for errors here. A typical error would be a NULL return
782 from a subroutine. We catch those here, and return NULL
786 /* These types require two parameters. */
787 case D_COMP_QUAL_NAME
:
788 case D_COMP_LOCAL_NAME
:
789 case D_COMP_TYPED_NAME
:
790 case D_COMP_TEMPLATE
:
791 case D_COMP_VENDOR_TYPE_QUAL
:
792 case D_COMP_PTRMEM_TYPE
:
795 case D_COMP_BINARY_ARGS
:
797 case D_COMP_TRINARY_ARG1
:
798 case D_COMP_TRINARY_ARG2
:
800 case D_COMP_LITERAL_NEG
:
801 if (left
== NULL
|| right
== NULL
)
805 /* These types only require one parameter. */
808 case D_COMP_CONSTRUCTION_VTABLE
:
809 case D_COMP_TYPEINFO
:
810 case D_COMP_TYPEINFO_NAME
:
811 case D_COMP_TYPEINFO_FN
:
813 case D_COMP_VIRTUAL_THUNK
:
814 case D_COMP_COVARIANT_THUNK
:
815 case D_COMP_JAVA_CLASS
:
819 case D_COMP_REFERENCE
:
821 case D_COMP_IMAGINARY
:
822 case D_COMP_VENDOR_TYPE
:
824 case D_COMP_TEMPLATE_ARGLIST
:
830 /* This needs a right parameter, but the left parameter can be
832 case D_COMP_ARRAY_TYPE
:
837 /* These are allowed to have no parameters--in some cases they
838 will be filled in later. */
839 case D_COMP_FUNCTION_TYPE
:
840 case D_COMP_RESTRICT
:
841 case D_COMP_VOLATILE
:
843 case D_COMP_RESTRICT_THIS
:
844 case D_COMP_VOLATILE_THIS
:
845 case D_COMP_CONST_THIS
:
848 /* Other types should not be seen here. */
853 p
= d_make_empty (di
, type
);
856 p
->u
.s_binary
.left
= left
;
857 p
->u
.s_binary
.right
= right
;
862 /* Add a new name component. */
864 static struct d_comp
*
865 d_make_name (di
, s
, len
)
872 if (s
== NULL
|| len
== 0)
874 p
= d_make_empty (di
, D_COMP_NAME
);
878 p
->u
.s_name
.len
= len
;
883 /* Add a new builtin type component. */
885 static struct d_comp
*
886 d_make_builtin_type (di
, type
)
888 const struct d_builtin_type_info
*type
;
894 p
= d_make_empty (di
, D_COMP_BUILTIN_TYPE
);
896 p
->u
.s_builtin
.type
= type
;
900 /* Add a new operator component. */
902 static struct d_comp
*
903 d_make_operator (di
, op
)
905 const struct d_operator_info
*op
;
909 p
= d_make_empty (di
, D_COMP_OPERATOR
);
911 p
->u
.s_operator
.op
= op
;
915 /* Add a new extended operator component. */
917 static struct d_comp
*
918 d_make_extended_operator (di
, args
, name
)
927 p
= d_make_empty (di
, D_COMP_EXTENDED_OPERATOR
);
930 p
->u
.s_extended_operator
.args
= args
;
931 p
->u
.s_extended_operator
.name
= name
;
936 /* Add a new constructor component. */
938 static struct d_comp
*
939 d_make_ctor (di
, kind
, name
)
941 enum gnu_v3_ctor_kinds kind
;
948 p
= d_make_empty (di
, D_COMP_CTOR
);
951 p
->u
.s_ctor
.kind
= kind
;
952 p
->u
.s_ctor
.name
= name
;
957 /* Add a new destructor component. */
959 static struct d_comp
*
960 d_make_dtor (di
, kind
, name
)
962 enum gnu_v3_dtor_kinds kind
;
969 p
= d_make_empty (di
, D_COMP_DTOR
);
972 p
->u
.s_dtor
.kind
= kind
;
973 p
->u
.s_dtor
.name
= name
;
978 /* Add a new template parameter. */
980 static struct d_comp
*
981 d_make_template_param (di
, i
)
987 p
= d_make_empty (di
, D_COMP_TEMPLATE_PARAM
);
989 p
->u
.s_number
.number
= i
;
993 /* Add a new standard substitution component. */
995 static struct d_comp
*
996 d_make_sub (di
, name
, len
)
1003 p
= d_make_empty (di
, D_COMP_SUB_STD
);
1006 p
->u
.s_string
.string
= name
;
1007 p
->u
.s_string
.len
= len
;
1012 /* <mangled-name> ::= _Z <encoding>
1014 TOP_LEVEL is non-zero when called at the top level. */
1016 static struct d_comp
*
1017 d_mangled_name (di
, top_level
)
1021 if (d_next_char (di
) != '_')
1023 if (d_next_char (di
) != 'Z')
1025 return d_encoding (di
, top_level
);
1028 /* Return whether a function should have a return type. The argument
1029 is the function name, which may be qualified in various ways. The
1030 rules are that template functions have return types with some
1031 exceptions, function types which are not part of a function name
1032 mangling have return types with some exceptions, and non-template
1033 function names do not have return types. The exceptions are that
1034 constructors, destructors, and conversion operators do not have
1038 has_return_type (dc
)
1047 case D_COMP_TEMPLATE
:
1048 return ! is_ctor_dtor_or_conversion (d_left (dc
));
1049 case D_COMP_RESTRICT_THIS
:
1050 case D_COMP_VOLATILE_THIS
:
1051 case D_COMP_CONST_THIS
:
1052 return has_return_type (d_left (dc
));
1056 /* Return whether a name is a constructor, a destructor, or a
1057 conversion operator. */
1060 is_ctor_dtor_or_conversion (dc
)
1069 case D_COMP_QUAL_NAME
:
1070 case D_COMP_LOCAL_NAME
:
1071 return is_ctor_dtor_or_conversion (d_right (dc
));
1079 /* <encoding> ::= <(function) name> <bare-function-type>
1083 TOP_LEVEL is non-zero when called at the top level, in which case
1084 if DMGL_PARAMS is not set we do not demangle the function
1085 parameters. We only set this at the top level, because otherwise
1086 we would not correctly demangle names in local scopes. */
1088 static struct d_comp
*
1089 d_encoding (di
, top_level
)
1093 char peek
= d_peek_char (di
);
1095 if (peek
== 'G' || peek
== 'T')
1096 return d_special_name (di
);
1103 if (dc
!= NULL
&& top_level
&& (di
->options
& DMGL_PARAMS
) == 0)
1105 /* Strip off any initial CV-qualifiers, as they really apply
1106 to the `this' parameter, and they were not output by the
1107 v2 demangler without DMGL_PARAMS. */
1108 while (dc
->type
== D_COMP_RESTRICT_THIS
1109 || dc
->type
== D_COMP_VOLATILE_THIS
1110 || dc
->type
== D_COMP_CONST_THIS
)
1113 /* If the top level is a D_COMP_LOCAL_NAME, then there may
1114 be CV-qualifiers on its right argument which really apply
1115 here; this happens when parsing a class which is local to
1117 if (dc
->type
== D_COMP_LOCAL_NAME
)
1122 while (dcr
->type
== D_COMP_RESTRICT_THIS
1123 || dcr
->type
== D_COMP_VOLATILE_THIS
1124 || dcr
->type
== D_COMP_CONST_THIS
)
1126 dc
->u
.s_binary
.right
= dcr
;
1132 peek
= d_peek_char (di
);
1133 if (peek
== '\0' || peek
== 'E')
1135 return d_make_comp (di
, D_COMP_TYPED_NAME
, dc
,
1136 d_bare_function_type (di
, has_return_type (dc
)));
1140 /* <name> ::= <nested-name>
1142 ::= <unscoped-template-name> <template-args>
1145 <unscoped-name> ::= <unqualified-name>
1146 ::= St <unqualified-name>
1148 <unscoped-template-name> ::= <unscoped-name>
1152 static struct d_comp
*
1156 char peek
= d_peek_char (di
);
1162 return d_nested_name (di
);
1165 return d_local_name (di
);
1171 if (d_peek_next_char (di
) != 't')
1173 dc
= d_substitution (di
, 0);
1179 dc
= d_make_comp (di
, D_COMP_QUAL_NAME
, d_make_name (di
, "std", 3),
1180 d_unqualified_name (di
));
1185 if (d_peek_char (di
) != 'I')
1187 /* The grammar does not permit this case to occur if we
1188 called d_substitution() above (i.e., subst == 1). We
1189 don't bother to check. */
1193 /* This is <template-args>, which means that we just saw
1194 <unscoped-template-name>, which is a substitution
1195 candidate if we didn't just get it from a
1199 if (! d_add_substitution (di
, dc
))
1202 dc
= d_make_comp (di
, D_COMP_TEMPLATE
, dc
, d_template_args (di
));
1209 dc
= d_unqualified_name (di
);
1210 if (d_peek_char (di
) == 'I')
1212 /* This is <template-args>, which means that we just saw
1213 <unscoped-template-name>, which is a substitution
1215 if (! d_add_substitution (di
, dc
))
1217 dc
= d_make_comp (di
, D_COMP_TEMPLATE
, dc
, d_template_args (di
));
1223 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1224 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1227 static struct d_comp
*
1232 struct d_comp
**pret
;
1234 if (d_next_char (di
) != 'N')
1237 pret
= d_cv_qualifiers (di
, &ret
, 1);
1241 *pret
= d_prefix (di
);
1245 if (d_next_char (di
) != 'E')
1251 /* <prefix> ::= <prefix> <unqualified-name>
1252 ::= <template-prefix> <template-args>
1253 ::= <template-param>
1257 <template-prefix> ::= <prefix> <(template) unqualified-name>
1258 ::= <template-param>
1262 static struct d_comp
*
1266 struct d_comp
*ret
= NULL
;
1271 enum d_comp_type comb_type
;
1274 peek
= d_peek_char (di
);
1278 /* The older code accepts a <local-name> here, but I don't see
1279 that in the grammar. The older code does not accept a
1280 <template-param> here. */
1282 comb_type
= D_COMP_QUAL_NAME
;
1287 dc
= d_unqualified_name (di
);
1288 else if (peek
== 'S')
1289 dc
= d_substitution (di
, 1);
1290 else if (peek
== 'I')
1294 comb_type
= D_COMP_TEMPLATE
;
1295 dc
= d_template_args (di
);
1297 else if (peek
== 'T')
1298 dc
= d_template_param (di
);
1299 else if (peek
== 'E')
1307 ret
= d_make_comp (di
, comb_type
, ret
, dc
);
1309 if (peek
!= 'S' && d_peek_char (di
) != 'E')
1311 if (! d_add_substitution (di
, ret
))
1317 /* <unqualified-name> ::= <operator-name>
1318 ::= <ctor-dtor-name>
1322 static struct d_comp
*
1323 d_unqualified_name (di
)
1328 peek
= d_peek_char (di
);
1329 if (IS_DIGIT (peek
))
1330 return d_source_name (di
);
1331 else if (IS_LOWER (peek
))
1335 ret
= d_operator_name (di
);
1336 if (ret
!= NULL
&& ret
->type
== D_COMP_OPERATOR
)
1337 di
->expansion
+= sizeof "operator" + ret
->u
.s_operator
.op
->len
- 2;
1340 else if (peek
== 'C' || peek
== 'D')
1341 return d_ctor_dtor_name (di
);
1346 /* <source-name> ::= <(positive length) number> <identifier> */
1348 static struct d_comp
*
1355 len
= d_number (di
);
1358 ret
= d_identifier (di
, len
);
1359 di
->last_name
= ret
;
1363 /* number ::= [n] <(non-negative decimal integer)> */
1374 peek
= d_peek_char (di
);
1379 peek
= d_peek_char (di
);
1385 if (! IS_DIGIT (peek
))
1391 ret
= ret
* 10 + peek
- '0';
1393 peek
= d_peek_char (di
);
1397 /* identifier ::= <(unqualified source code identifier)> */
1399 static struct d_comp
*
1400 d_identifier (di
, len
)
1408 if (di
->send
- name
< len
)
1411 d_advance (di
, len
);
1413 /* A Java mangled name may have a trailing '$' if it is a C++
1414 keyword. This '$' is not included in the length count. We just
1416 if ((di
->options
& DMGL_JAVA
) != 0
1417 && d_peek_char (di
) == '$')
1420 /* Look for something which looks like a gcc encoding of an
1421 anonymous namespace, and replace it with a more user friendly
1423 if (len
>= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN
+ 2
1424 && memcmp (name
, ANONYMOUS_NAMESPACE_PREFIX
,
1425 ANONYMOUS_NAMESPACE_PREFIX_LEN
) == 0)
1429 s
= name
+ ANONYMOUS_NAMESPACE_PREFIX_LEN
;
1430 if ((*s
== '.' || *s
== '_' || *s
== '$')
1433 di
->expansion
-= len
- sizeof "(anonymous namespace)";
1434 return d_make_name (di
, "(anonymous namespace)",
1435 sizeof "(anonymous namespace)" - 1);
1439 return d_make_name (di
, name
, len
);
1442 /* operator_name ::= many different two character encodings.
1444 ::= v <digit> <source-name>
1447 #define NL(s) s, (sizeof s) - 1
1449 static const struct d_operator_info d_operators
[] =
1451 { "aN", NL ("&="), 2 },
1452 { "aS", NL ("="), 2 },
1453 { "aa", NL ("&&"), 2 },
1454 { "ad", NL ("&"), 1 },
1455 { "an", NL ("&"), 2 },
1456 { "cl", NL ("()"), 0 },
1457 { "cm", NL (","), 2 },
1458 { "co", NL ("~"), 1 },
1459 { "dV", NL ("/="), 2 },
1460 { "da", NL ("delete[]"), 1 },
1461 { "de", NL ("*"), 1 },
1462 { "dl", NL ("delete"), 1 },
1463 { "dv", NL ("/"), 2 },
1464 { "eO", NL ("^="), 2 },
1465 { "eo", NL ("^"), 2 },
1466 { "eq", NL ("=="), 2 },
1467 { "ge", NL (">="), 2 },
1468 { "gt", NL (">"), 2 },
1469 { "ix", NL ("[]"), 2 },
1470 { "lS", NL ("<<="), 2 },
1471 { "le", NL ("<="), 2 },
1472 { "ls", NL ("<<"), 2 },
1473 { "lt", NL ("<"), 2 },
1474 { "mI", NL ("-="), 2 },
1475 { "mL", NL ("*="), 2 },
1476 { "mi", NL ("-"), 2 },
1477 { "ml", NL ("*"), 2 },
1478 { "mm", NL ("--"), 1 },
1479 { "na", NL ("new[]"), 1 },
1480 { "ne", NL ("!="), 2 },
1481 { "ng", NL ("-"), 1 },
1482 { "nt", NL ("!"), 1 },
1483 { "nw", NL ("new"), 1 },
1484 { "oR", NL ("|="), 2 },
1485 { "oo", NL ("||"), 2 },
1486 { "or", NL ("|"), 2 },
1487 { "pL", NL ("+="), 2 },
1488 { "pl", NL ("+"), 2 },
1489 { "pm", NL ("->*"), 2 },
1490 { "pp", NL ("++"), 1 },
1491 { "ps", NL ("+"), 1 },
1492 { "pt", NL ("->"), 2 },
1493 { "qu", NL ("?"), 3 },
1494 { "rM", NL ("%="), 2 },
1495 { "rS", NL (">>="), 2 },
1496 { "rm", NL ("%"), 2 },
1497 { "rs", NL (">>"), 2 },
1498 { "st", NL ("sizeof "), 1 },
1499 { "sz", NL ("sizeof "), 1 }
1502 static struct d_comp
*
1503 d_operator_name (di
)
1509 c1
= d_next_char (di
);
1510 c2
= d_next_char (di
);
1511 if (c1
== 'v' && IS_DIGIT (c2
))
1512 return d_make_extended_operator (di
, c2
- '0', d_source_name (di
));
1513 else if (c1
== 'c' && c2
== 'v')
1514 return d_make_comp (di
, D_COMP_CAST
, d_type (di
), NULL
);
1518 int high
= sizeof (d_operators
) / sizeof (d_operators
[0]);
1523 const struct d_operator_info
*p
;
1525 i
= low
+ (high
- low
) / 2;
1526 p
= d_operators
+ i
;
1528 if (c1
== p
->code
[0] && c2
== p
->code
[1])
1529 return d_make_operator (di
, p
);
1531 if (c1
< p
->code
[0] || (c1
== p
->code
[0] && c2
< p
->code
[1]))
1541 /* <special-name> ::= TV <type>
1545 ::= GV <(object) name>
1546 ::= T <call-offset> <(base) encoding>
1547 ::= Tc <call-offset> <call-offset> <(base) encoding>
1548 Also g++ extensions:
1549 ::= TC <type> <(offset) number> _ <(base) type>
1555 static struct d_comp
*
1561 di
->expansion
+= 20;
1562 c
= d_next_char (di
);
1565 switch (d_next_char (di
))
1569 return d_make_comp (di
, D_COMP_VTABLE
, d_type (di
), NULL
);
1571 di
->expansion
-= 10;
1572 return d_make_comp (di
, D_COMP_VTT
, d_type (di
), NULL
);
1574 return d_make_comp (di
, D_COMP_TYPEINFO
, d_type (di
), NULL
);
1576 return d_make_comp (di
, D_COMP_TYPEINFO_NAME
, d_type (di
), NULL
);
1579 if (! d_call_offset (di
, 'h'))
1581 return d_make_comp (di
, D_COMP_THUNK
, d_encoding (di
, 0), NULL
);
1584 if (! d_call_offset (di
, 'v'))
1586 return d_make_comp (di
, D_COMP_VIRTUAL_THUNK
, d_encoding (di
, 0),
1590 if (! d_call_offset (di
, '\0'))
1592 if (! d_call_offset (di
, '\0'))
1594 return d_make_comp (di
, D_COMP_COVARIANT_THUNK
, d_encoding (di
, 0),
1599 struct d_comp
*derived_type
;
1601 struct d_comp
*base_type
;
1603 derived_type
= d_type (di
);
1604 offset
= d_number (di
);
1607 if (d_next_char (di
) != '_')
1609 base_type
= d_type (di
);
1610 /* We don't display the offset. FIXME: We should display
1611 it in verbose mode. */
1613 return d_make_comp (di
, D_COMP_CONSTRUCTION_VTABLE
, base_type
,
1618 return d_make_comp (di
, D_COMP_TYPEINFO_FN
, d_type (di
), NULL
);
1620 return d_make_comp (di
, D_COMP_JAVA_CLASS
, d_type (di
), NULL
);
1628 switch (d_next_char (di
))
1631 return d_make_comp (di
, D_COMP_GUARD
, d_name (di
), NULL
);
1634 return d_make_comp (di
, D_COMP_REFTEMP
, d_name (di
), NULL
);
1644 /* <call-offset> ::= h <nv-offset> _
1647 <nv-offset> ::= <(offset) number>
1649 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1651 The C parameter, if not '\0', is a character we just read which is
1652 the start of the <call-offset>.
1654 We don't display the offset information anywhere. FIXME: We should
1655 display it in verbose mode. */
1658 d_call_offset (di
, c
)
1663 long virtual_offset
;
1666 c
= d_next_char (di
);
1669 offset
= d_number (di
);
1672 offset
= d_number (di
);
1673 if (d_next_char (di
) != '_')
1675 virtual_offset
= d_number (di
);
1680 if (d_next_char (di
) != '_')
1686 /* <ctor-dtor-name> ::= C1
1694 static struct d_comp
*
1695 d_ctor_dtor_name (di
)
1698 if (di
->last_name
!= NULL
)
1700 if (di
->last_name
->type
== D_COMP_NAME
)
1701 di
->expansion
+= di
->last_name
->u
.s_name
.len
;
1702 else if (di
->last_name
->type
== D_COMP_SUB_STD
)
1703 di
->expansion
+= di
->last_name
->u
.s_string
.len
;
1705 switch (d_next_char (di
))
1709 enum gnu_v3_ctor_kinds kind
;
1711 switch (d_next_char (di
))
1714 kind
= gnu_v3_complete_object_ctor
;
1717 kind
= gnu_v3_base_object_ctor
;
1720 kind
= gnu_v3_complete_object_allocating_ctor
;
1725 return d_make_ctor (di
, kind
, di
->last_name
);
1730 enum gnu_v3_dtor_kinds kind
;
1732 switch (d_next_char (di
))
1735 kind
= gnu_v3_deleting_dtor
;
1738 kind
= gnu_v3_complete_object_dtor
;
1741 kind
= gnu_v3_base_object_dtor
;
1746 return d_make_dtor (di
, kind
, di
->last_name
);
1754 /* <type> ::= <builtin-type>
1756 ::= <class-enum-type>
1758 ::= <pointer-to-member-type>
1759 ::= <template-param>
1760 ::= <template-template-param> <template-args>
1762 ::= <CV-qualifiers> <type>
1767 ::= U <source-name> <type>
1769 <builtin-type> ::= various one letter codes
1773 static const struct d_builtin_type_info d_builtin_types
[26] =
1775 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_INT
},
1776 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL
},
1777 /* c */ { NL ("char"), NL ("byte"), D_PRINT_INT
},
1778 /* d */ { NL ("double"), NL ("double"), D_PRINT_DEFAULT
},
1779 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_DEFAULT
},
1780 /* f */ { NL ("float"), NL ("float"), D_PRINT_DEFAULT
},
1781 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_DEFAULT
},
1782 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_INT
},
1783 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT
},
1784 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_INT
},
1785 /* k */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
1786 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG
},
1787 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_LONG
},
1788 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT
},
1789 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"), D_PRINT_DEFAULT
},
1790 /* p */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
1791 /* q */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
1792 /* r */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
1793 /* s */ { NL ("short"), NL ("short"), D_PRINT_INT
},
1794 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_INT
},
1795 /* u */ { NULL
, 0, NULL
, 0, D_PRINT_DEFAULT
},
1796 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID
},
1797 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_INT
},
1798 /* x */ { NL ("long long"), NL ("long"), D_PRINT_DEFAULT
},
1799 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"), D_PRINT_DEFAULT
},
1800 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT
},
1803 static struct d_comp
*
1811 /* The ABI specifies that when CV-qualifiers are used, the base type
1812 is substitutable, and the fully qualified type is substitutable,
1813 but the base type with a strict subset of the CV-qualifiers is
1814 not substitutable. The natural recursive implementation of the
1815 CV-qualifiers would cause subsets to be substitutable, so instead
1816 we pull them all off now.
1818 FIXME: The ABI says that order-insensitive vendor qualifiers
1819 should be handled in the same way, but we have no way to tell
1820 which vendor qualifiers are order-insensitive and which are
1821 order-sensitive. So we just assume that they are all
1822 order-sensitive. g++ 3.4 supports only one vendor qualifier,
1823 __vector, and it treats it as order-sensitive when mangling
1826 peek
= d_peek_char (di
);
1827 if (peek
== 'r' || peek
== 'V' || peek
== 'K')
1829 struct d_comp
**pret
;
1831 pret
= d_cv_qualifiers (di
, &ret
, 0);
1834 *pret
= d_type (di
);
1835 if (! d_add_substitution (di
, ret
))
1844 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1845 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
1846 case 'o': case 's': case 't':
1847 case 'v': case 'w': case 'x': case 'y': case 'z':
1848 ret
= d_make_builtin_type (di
, &d_builtin_types
[peek
- 'a']);
1849 di
->expansion
+= ret
->u
.s_builtin
.type
->len
;
1856 ret
= d_make_comp (di
, D_COMP_VENDOR_TYPE
, d_source_name (di
), NULL
);
1860 ret
= d_function_type (di
);
1863 case '0': case '1': case '2': case '3': case '4':
1864 case '5': case '6': case '7': case '8': case '9':
1867 ret
= d_class_enum_type (di
);
1871 ret
= d_array_type (di
);
1875 ret
= d_pointer_to_member_type (di
);
1879 ret
= d_template_param (di
);
1880 if (d_peek_char (di
) == 'I')
1882 /* This is <template-template-param> <template-args>. The
1883 <template-template-param> part is a substitution
1885 if (! d_add_substitution (di
, ret
))
1887 ret
= d_make_comp (di
, D_COMP_TEMPLATE
, ret
, d_template_args (di
));
1892 /* If this is a special substitution, then it is the start of
1893 <class-enum-type>. */
1897 peek_next
= d_peek_next_char (di
);
1898 if (IS_DIGIT (peek_next
)
1900 || IS_UPPER (peek_next
))
1902 ret
= d_substitution (di
, 0);
1903 /* The substituted name may have been a template name and
1904 may be followed by tepmlate args. */
1905 if (d_peek_char (di
) == 'I')
1906 ret
= d_make_comp (di
, D_COMP_TEMPLATE
, ret
,
1907 d_template_args (di
));
1913 ret
= d_class_enum_type (di
);
1914 /* If the substitution was a complete type, then it is not
1915 a new substitution candidate. However, if the
1916 substitution was followed by template arguments, then
1917 the whole thing is a substitution candidate. */
1918 if (ret
!= NULL
&& ret
->type
== D_COMP_SUB_STD
)
1926 ret
= d_make_comp (di
, D_COMP_POINTER
, d_type (di
), NULL
);
1931 ret
= d_make_comp (di
, D_COMP_REFERENCE
, d_type (di
), NULL
);
1936 ret
= d_make_comp (di
, D_COMP_COMPLEX
, d_type (di
), NULL
);
1941 ret
= d_make_comp (di
, D_COMP_IMAGINARY
, d_type (di
), NULL
);
1946 ret
= d_source_name (di
);
1947 ret
= d_make_comp (di
, D_COMP_VENDOR_TYPE_QUAL
, d_type (di
), ret
);
1956 if (! d_add_substitution (di
, ret
))
1963 /* <CV-qualifiers> ::= [r] [V] [K] */
1965 static struct d_comp
**
1966 d_cv_qualifiers (di
, pret
, member_fn
)
1968 struct d_comp
**pret
;
1973 peek
= d_peek_char (di
);
1974 while (peek
== 'r' || peek
== 'V' || peek
== 'K')
1981 t
= member_fn
? D_COMP_RESTRICT_THIS
: D_COMP_RESTRICT
;
1982 di
->expansion
+= sizeof "restrict";
1984 else if (peek
== 'V')
1986 t
= member_fn
? D_COMP_VOLATILE_THIS
: D_COMP_VOLATILE
;
1987 di
->expansion
+= sizeof "volatile";
1991 t
= member_fn
? D_COMP_CONST_THIS
: D_COMP_CONST
;
1992 di
->expansion
+= sizeof "const";
1995 *pret
= d_make_comp (di
, t
, NULL
, NULL
);
1998 pret
= &d_left (*pret
);
2000 peek
= d_peek_char (di
);
2006 /* <function-type> ::= F [Y] <bare-function-type> E */
2008 static struct d_comp
*
2009 d_function_type (di
)
2014 if (d_next_char (di
) != 'F')
2016 if (d_peek_char (di
) == 'Y')
2018 /* Function has C linkage. We don't print this information.
2019 FIXME: We should print it in verbose mode. */
2022 ret
= d_bare_function_type (di
, 1);
2023 if (d_next_char (di
) != 'E')
2028 /* <bare-function-type> ::= <type>+ */
2030 static struct d_comp
*
2031 d_bare_function_type (di
, has_return_type
)
2033 int has_return_type
;
2035 struct d_comp
*return_type
;
2037 struct d_comp
**ptl
;
2045 struct d_comp
*type
;
2047 peek
= d_peek_char (di
);
2048 if (peek
== '\0' || peek
== 'E')
2053 if (has_return_type
)
2056 has_return_type
= 0;
2060 *ptl
= d_make_comp (di
, D_COMP_ARGLIST
, type
, NULL
);
2063 ptl
= &d_right (*ptl
);
2067 /* There should be at least one parameter type besides the optional
2068 return type. A function which takes no arguments will have a
2069 single parameter type void. */
2073 /* If we have a single parameter type void, omit it. */
2074 if (d_right (tl
) == NULL
2075 && d_left (tl
)->type
== D_COMP_BUILTIN_TYPE
2076 && d_left (tl
)->u
.s_builtin
.type
->print
== D_PRINT_VOID
)
2078 di
->expansion
-= d_left (tl
)->u
.s_builtin
.type
->len
;
2082 return d_make_comp (di
, D_COMP_FUNCTION_TYPE
, return_type
, tl
);
2085 /* <class-enum-type> ::= <name> */
2087 static struct d_comp
*
2088 d_class_enum_type (di
)
2094 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2095 ::= A [<(dimension) expression>] _ <(element) type>
2098 static struct d_comp
*
2105 if (d_next_char (di
) != 'A')
2108 peek
= d_peek_char (di
);
2111 else if (IS_DIGIT (peek
))
2119 peek
= d_peek_char (di
);
2121 while (IS_DIGIT (peek
));
2122 dim
= d_make_name (di
, s
, d_str (di
) - s
);
2128 dim
= d_expression (di
);
2133 if (d_next_char (di
) != '_')
2136 return d_make_comp (di
, D_COMP_ARRAY_TYPE
, dim
, d_type (di
));
2139 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2141 static struct d_comp
*
2142 d_pointer_to_member_type (di
)
2147 struct d_comp
**pmem
;
2149 if (d_next_char (di
) != 'M')
2154 /* The ABI specifies that any type can be a substitution source, and
2155 that M is followed by two types, and that when a CV-qualified
2156 type is seen both the base type and the CV-qualified types are
2157 substitution sources. The ABI also specifies that for a pointer
2158 to a CV-qualified member function, the qualifiers are attached to
2159 the second type. Given the grammar, a plain reading of the ABI
2160 suggests that both the CV-qualified member function and the
2161 non-qualified member function are substitution sources. However,
2162 g++ does not work that way. g++ treats only the CV-qualified
2163 member function as a substitution source. FIXME. So to work
2164 with g++, we need to pull off the CV-qualifiers here, in order to
2165 avoid calling add_substitution() in d_type(). */
2167 pmem
= d_cv_qualifiers (di
, &mem
, 1);
2170 *pmem
= d_type (di
);
2172 return d_make_comp (di
, D_COMP_PTRMEM_TYPE
, cl
, mem
);
2175 /* <template-param> ::= T_
2176 ::= T <(parameter-2 non-negative) number> _
2179 static struct d_comp
*
2180 d_template_param (di
)
2185 if (d_next_char (di
) != 'T')
2188 if (d_peek_char (di
) == '_')
2192 param
= d_number (di
);
2198 if (d_next_char (di
) != '_')
2203 return d_make_template_param (di
, param
);
2206 /* <template-args> ::= I <template-arg>+ E */
2208 static struct d_comp
*
2209 d_template_args (di
)
2212 struct d_comp
*hold_last_name
;
2214 struct d_comp
**pal
;
2216 /* Preserve the last name we saw--don't let the template arguments
2217 clobber it, as that would give us the wrong name for a subsequent
2218 constructor or destructor. */
2219 hold_last_name
= di
->last_name
;
2221 if (d_next_char (di
) != 'I')
2230 a
= d_template_arg (di
);
2234 *pal
= d_make_comp (di
, D_COMP_TEMPLATE_ARGLIST
, a
, NULL
);
2237 pal
= &d_right (*pal
);
2239 if (d_peek_char (di
) == 'E')
2246 di
->last_name
= hold_last_name
;
2251 /* <template-arg> ::= <type>
2252 ::= X <expression> E
2256 static struct d_comp
*
2262 switch (d_peek_char (di
))
2266 ret
= d_expression (di
);
2267 if (d_next_char (di
) != 'E')
2272 return d_expr_primary (di
);
2279 /* <expression> ::= <(unary) operator-name> <expression>
2280 ::= <(binary) operator-name> <expression> <expression>
2281 ::= <(trinary) operator-name> <expression> <expression> <expression>
2283 ::= <template-param>
2284 ::= sr <type> <unqualified-name>
2285 ::= sr <type> <unqualified-name> <template-args>
2289 static struct d_comp
*
2295 peek
= d_peek_char (di
);
2297 return d_expr_primary (di
);
2298 else if (peek
== 'T')
2299 return d_template_param (di
);
2300 else if (peek
== 's' && d_peek_next_char (di
) == 'r')
2302 struct d_comp
*type
;
2303 struct d_comp
*name
;
2307 name
= d_unqualified_name (di
);
2308 if (d_peek_char (di
) != 'I')
2309 return d_make_comp (di
, D_COMP_QUAL_NAME
, type
, name
);
2311 return d_make_comp (di
, D_COMP_QUAL_NAME
, type
,
2312 d_make_comp (di
, D_COMP_TEMPLATE
, name
,
2313 d_template_args (di
)));
2320 op
= d_operator_name (di
);
2324 if (op
->type
== D_COMP_OPERATOR
)
2325 di
->expansion
+= op
->u
.s_operator
.op
->len
- 2;
2327 if (op
->type
== D_COMP_OPERATOR
2328 && strcmp (op
->u
.s_operator
.op
->code
, "st") == 0)
2329 return d_make_comp (di
, D_COMP_UNARY
, op
, d_type (di
));
2335 case D_COMP_OPERATOR
:
2336 args
= op
->u
.s_operator
.op
->args
;
2338 case D_COMP_EXTENDED_OPERATOR
:
2339 args
= op
->u
.s_extended_operator
.args
;
2349 return d_make_comp (di
, D_COMP_UNARY
, op
, d_expression (di
));
2352 struct d_comp
*left
;
2354 left
= d_expression (di
);
2355 return d_make_comp (di
, D_COMP_BINARY
, op
,
2356 d_make_comp (di
, D_COMP_BINARY_ARGS
, left
,
2357 d_expression (di
)));
2361 struct d_comp
*first
;
2362 struct d_comp
*second
;
2364 first
= d_expression (di
);
2365 second
= d_expression (di
);
2366 return d_make_comp (di
, D_COMP_TRINARY
, op
,
2367 d_make_comp (di
, D_COMP_TRINARY_ARG1
, first
,
2369 D_COMP_TRINARY_ARG2
,
2371 d_expression (di
))));
2379 /* <expr-primary> ::= L <type> <(value) number> E
2380 ::= L <type> <(value) float> E
2381 ::= L <mangled-name> E
2384 static struct d_comp
*
2390 if (d_next_char (di
) != 'L')
2392 if (d_peek_char (di
) == '_')
2393 ret
= d_mangled_name (di
, 0);
2396 struct d_comp
*type
;
2402 /* If we have a type we know how to print, we aren't going to
2403 print the type name itself. */
2404 if (type
->type
== D_COMP_BUILTIN_TYPE
2405 && type
->u
.s_builtin
.type
->print
!= D_PRINT_DEFAULT
)
2406 di
->expansion
-= type
->u
.s_builtin
.type
->len
;
2408 /* Rather than try to interpret the literal value, we just
2409 collect it as a string. Note that it's possible to have a
2410 floating point literal here. The ABI specifies that the
2411 format of such literals is machine independent. That's fine,
2412 but what's not fine is that versions of g++ up to 3.2 with
2413 -fabi-version=1 used upper case letters in the hex constant,
2414 and dumped out gcc's internal representation. That makes it
2415 hard to tell where the constant ends, and hard to dump the
2416 constant in any readable form anyhow. We don't attempt to
2417 handle these cases. */
2420 if (d_peek_char (di
) == 'n')
2422 t
= D_COMP_LITERAL_NEG
;
2426 while (d_peek_char (di
) != 'E')
2428 ret
= d_make_comp (di
, t
, type
, d_make_name (di
, s
, d_str (di
) - s
));
2430 if (d_next_char (di
) != 'E')
2435 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2436 ::= Z <(function) encoding> E s [<discriminator>]
2439 static struct d_comp
*
2443 struct d_comp
*function
;
2445 if (d_next_char (di
) != 'Z')
2448 function
= d_encoding (di
, 0);
2450 if (d_next_char (di
) != 'E')
2453 if (d_peek_char (di
) == 's')
2456 if (! d_discriminator (di
))
2458 return d_make_comp (di
, D_COMP_LOCAL_NAME
, function
,
2459 d_make_name (di
, "string literal",
2460 sizeof "string literal" - 1));
2464 struct d_comp
*name
;
2467 if (! d_discriminator (di
))
2469 return d_make_comp (di
, D_COMP_LOCAL_NAME
, function
, name
);
2473 /* <discriminator> ::= _ <(non-negative) number>
2475 We demangle the discriminator, but we don't print it out. FIXME:
2476 We should print it out in verbose mode. */
2479 d_discriminator (di
)
2484 if (d_peek_char (di
) != '_')
2487 discrim
= d_number (di
);
2493 /* Add a new substitution. */
2496 d_add_substitution (di
, dc
)
2502 if (di
->next_sub
>= di
->num_subs
)
2504 di
->subs
[di
->next_sub
] = dc
;
2509 /* <substitution> ::= S <seq-id> _
2519 If PREFIX is non-zero, then this type is being used as a prefix in
2520 a qualified name. In this case, for the standard substitutions, we
2521 need to check whether we are being used as a prefix for a
2522 constructor or destructor, and return a full template name.
2523 Otherwise we will get something like std::iostream::~iostream()
2524 which does not correspond particularly well to any function which
2525 actually appears in the source.
2528 static const struct d_standard_sub_info standard_subs
[] =
2533 { 'a', NL ("std::allocator"),
2534 NL ("std::allocator"),
2536 { 'b', NL ("std::basic_string"),
2537 NL ("std::basic_string"),
2538 NL ("basic_string") },
2539 { 's', NL ("std::string"),
2540 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
2541 NL ("basic_string") },
2542 { 'i', NL ("std::istream"),
2543 NL ("std::basic_istream<char, std::char_traits<char> >"),
2544 NL ("basic_istream") },
2545 { 'o', NL ("std::ostream"),
2546 NL ("std::basic_ostream<char, std::char_traits<char> >"),
2547 NL ("basic_ostream") },
2548 { 'd', NL ("std::iostream"),
2549 NL ("std::basic_iostream<char, std::char_traits<char> >"),
2550 NL ("basic_iostream") }
2553 static struct d_comp
*
2554 d_substitution (di
, prefix
)
2560 if (d_next_char (di
) != 'S')
2563 c
= d_next_char (di
);
2564 if (c
== '_' || IS_DIGIT (c
) || IS_UPPER (c
))
2574 id
= id
* 36 + c
- '0';
2575 else if (IS_UPPER (c
))
2576 id
= id
* 36 + c
- 'A' + 10;
2579 c
= d_next_char (di
);
2586 if (id
>= di
->next_sub
)
2591 return di
->subs
[id
];
2596 const struct d_standard_sub_info
*p
;
2597 const struct d_standard_sub_info
*pend
;
2599 verbose
= (di
->options
& DMGL_VERBOSE
) != 0;
2600 if (! verbose
&& prefix
)
2604 peek
= d_peek_char (di
);
2605 if (peek
== 'C' || peek
== 'D')
2609 pend
= (&standard_subs
[0]
2610 + sizeof standard_subs
/ sizeof standard_subs
[0]);
2611 for (p
= &standard_subs
[0]; p
< pend
; ++p
)
2618 if (p
->set_last_name
!= NULL
)
2619 di
->last_name
= d_make_sub (di
, p
->set_last_name
,
2620 p
->set_last_name_len
);
2623 s
= p
->full_expansion
;
2628 s
= p
->simple_expansion
;
2629 len
= p
->simple_len
;
2631 di
->expansion
+= len
;
2632 return d_make_sub (di
, s
, len
);
2640 /* Resize the print buffer. */
2643 d_print_resize (dpi
, add
)
2644 struct d_print_info
*dpi
;
2649 if (dpi
->buf
== NULL
)
2651 need
= dpi
->len
+ add
;
2652 while (need
> dpi
->alc
)
2657 newalc
= dpi
->alc
* 2;
2658 newbuf
= realloc (dpi
->buf
, newalc
);
2663 dpi
->allocation_failure
= 1;
2671 /* Append a character to the print buffer. */
2674 d_print_append_char (dpi
, c
)
2675 struct d_print_info
*dpi
;
2678 if (dpi
->buf
!= NULL
)
2680 if (dpi
->len
>= dpi
->alc
)
2682 d_print_resize (dpi
, 1);
2683 if (dpi
->buf
== NULL
)
2687 dpi
->buf
[dpi
->len
] = c
;
2692 /* Append a buffer to the print buffer. */
2695 d_print_append_buffer (dpi
, s
, l
)
2696 struct d_print_info
*dpi
;
2700 if (dpi
->buf
!= NULL
)
2702 if (dpi
->len
+ l
> dpi
->alc
)
2704 d_print_resize (dpi
, l
);
2705 if (dpi
->buf
== NULL
)
2709 memcpy (dpi
->buf
+ dpi
->len
, s
, l
);
2714 /* Indicate that an error occurred during printing. */
2718 struct d_print_info
*dpi
;
2724 /* Turn components into a human readable string. OPTIONS is the
2725 options bits passed to the demangler. DC is the tree to print.
2726 ESTIMATE is a guess at the length of the result. This returns a
2727 string allocated by malloc, or NULL on error. On success, this
2728 sets *PALC to the size of the allocated buffer. On failure, this
2729 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
2733 d_print (options
, dc
, estimate
, palc
)
2735 const struct d_comp
*dc
;
2739 struct d_print_info dpi
;
2741 dpi
.options
= options
;
2743 dpi
.alc
= estimate
+ 1;
2744 dpi
.buf
= malloc (dpi
.alc
);
2745 if (dpi
.buf
== NULL
)
2752 dpi
.templates
= NULL
;
2753 dpi
.modifiers
= NULL
;
2755 dpi
.allocation_failure
= 0;
2757 d_print_comp (&dpi
, dc
);
2759 d_append_char (&dpi
, '\0');
2761 if (dpi
.buf
!= NULL
)
2764 *palc
= dpi
.allocation_failure
;
2769 /* Subroutine to handle components. */
2772 d_print_comp (dpi
, dc
)
2773 struct d_print_info
*dpi
;
2774 const struct d_comp
*dc
;
2778 d_print_error (dpi
);
2781 if (d_print_saw_error (dpi
))
2787 if ((dpi
->options
& DMGL_JAVA
) == 0)
2788 d_append_buffer (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
2790 d_print_java_identifier (dpi
, dc
->u
.s_name
.s
, dc
->u
.s_name
.len
);
2793 case D_COMP_QUAL_NAME
:
2794 case D_COMP_LOCAL_NAME
:
2795 d_print_comp (dpi
, d_left (dc
));
2796 if ((dpi
->options
& DMGL_JAVA
) == 0)
2797 d_append_string_constant (dpi
, "::");
2799 d_append_char (dpi
, '.');
2800 d_print_comp (dpi
, d_right (dc
));
2803 case D_COMP_TYPED_NAME
:
2805 struct d_print_mod
*hold_modifiers
;
2806 struct d_comp
*typed_name
;
2807 struct d_print_mod adpm
[4];
2809 struct d_print_template dpt
;
2811 /* Pass the name down to the type so that it can be printed in
2812 the right place for the type. We also have to pass down
2813 any CV-qualifiers, which apply to the this parameter. */
2814 hold_modifiers
= dpi
->modifiers
;
2816 typed_name
= d_left (dc
);
2817 while (typed_name
!= NULL
)
2819 if (i
>= sizeof adpm
/ sizeof adpm
[0])
2821 d_print_error (dpi
);
2825 adpm
[i
].next
= dpi
->modifiers
;
2826 dpi
->modifiers
= &adpm
[i
];
2827 adpm
[i
].mod
= typed_name
;
2828 adpm
[i
].printed
= 0;
2829 adpm
[i
].templates
= dpi
->templates
;
2832 if (typed_name
->type
!= D_COMP_RESTRICT_THIS
2833 && typed_name
->type
!= D_COMP_VOLATILE_THIS
2834 && typed_name
->type
!= D_COMP_CONST_THIS
)
2837 typed_name
= d_left (typed_name
);
2840 /* If typed_name is a template, then it applies to the
2841 function type as well. */
2842 if (typed_name
->type
== D_COMP_TEMPLATE
)
2844 dpt
.next
= dpi
->templates
;
2845 dpi
->templates
= &dpt
;
2846 dpt
.template = typed_name
;
2849 /* If typed_name is a D_COMP_LOCAL_NAME, then there may be
2850 CV-qualifiers on its right argument which really apply
2851 here; this happens when parsing a class which is local to a
2853 if (typed_name
->type
== D_COMP_LOCAL_NAME
)
2855 struct d_comp
*local_name
;
2857 local_name
= d_right (typed_name
);
2858 while (local_name
->type
== D_COMP_RESTRICT_THIS
2859 || local_name
->type
== D_COMP_VOLATILE_THIS
2860 || local_name
->type
== D_COMP_CONST_THIS
)
2862 if (i
>= sizeof adpm
/ sizeof adpm
[0])
2864 d_print_error (dpi
);
2868 adpm
[i
] = adpm
[i
- 1];
2869 adpm
[i
].next
= &adpm
[i
- 1];
2870 dpi
->modifiers
= &adpm
[i
];
2872 adpm
[i
- 1].mod
= local_name
;
2873 adpm
[i
- 1].printed
= 0;
2874 adpm
[i
- 1].templates
= dpi
->templates
;
2877 local_name
= d_left (local_name
);
2881 d_print_comp (dpi
, d_right (dc
));
2883 if (typed_name
->type
== D_COMP_TEMPLATE
)
2884 dpi
->templates
= dpt
.next
;
2886 /* If the modifiers didn't get printed by the type, print them
2891 if (! adpm
[i
].printed
)
2893 d_append_char (dpi
, ' ');
2894 d_print_mod (dpi
, adpm
[i
].mod
);
2898 dpi
->modifiers
= hold_modifiers
;
2903 case D_COMP_TEMPLATE
:
2905 struct d_print_mod
*hold_dpm
;
2907 /* Don't push modifiers into a template definition. Doing so
2908 could give the wrong definition for a template argument.
2909 Instead, treat the template essentially as a name. */
2911 hold_dpm
= dpi
->modifiers
;
2912 dpi
->modifiers
= NULL
;
2914 d_print_comp (dpi
, d_left (dc
));
2915 if (d_last_char (dpi
) == '<')
2916 d_append_char (dpi
, ' ');
2917 d_append_char (dpi
, '<');
2918 d_print_comp (dpi
, d_right (dc
));
2919 /* Avoid generating two consecutive '>' characters, to avoid
2920 the C++ syntactic ambiguity. */
2921 if (d_last_char (dpi
) == '>')
2922 d_append_char (dpi
, ' ');
2923 d_append_char (dpi
, '>');
2925 dpi
->modifiers
= hold_dpm
;
2930 case D_COMP_TEMPLATE_PARAM
:
2934 struct d_print_template
*hold_dpt
;
2936 if (dpi
->templates
== NULL
)
2938 d_print_error (dpi
);
2941 i
= dc
->u
.s_number
.number
;
2942 for (a
= d_right (dpi
->templates
->template);
2946 if (a
->type
!= D_COMP_TEMPLATE_ARGLIST
)
2948 d_print_error (dpi
);
2955 if (i
!= 0 || a
== NULL
)
2957 d_print_error (dpi
);
2961 /* While processing this parameter, we need to pop the list of
2962 templates. This is because the template parameter may
2963 itself be a reference to a parameter of an outer
2966 hold_dpt
= dpi
->templates
;
2967 dpi
->templates
= hold_dpt
->next
;
2969 d_print_comp (dpi
, d_left (a
));
2971 dpi
->templates
= hold_dpt
;
2977 d_print_comp (dpi
, dc
->u
.s_ctor
.name
);
2981 d_append_char (dpi
, '~');
2982 d_print_comp (dpi
, dc
->u
.s_dtor
.name
);
2986 d_append_string_constant (dpi
, "vtable for ");
2987 d_print_comp (dpi
, d_left (dc
));
2991 d_append_string_constant (dpi
, "VTT for ");
2992 d_print_comp (dpi
, d_left (dc
));
2995 case D_COMP_CONSTRUCTION_VTABLE
:
2996 d_append_string_constant (dpi
, "construction vtable for ");
2997 d_print_comp (dpi
, d_left (dc
));
2998 d_append_string_constant (dpi
, "-in-");
2999 d_print_comp (dpi
, d_right (dc
));
3002 case D_COMP_TYPEINFO
:
3003 d_append_string_constant (dpi
, "typeinfo for ");
3004 d_print_comp (dpi
, d_left (dc
));
3007 case D_COMP_TYPEINFO_NAME
:
3008 d_append_string_constant (dpi
, "typeinfo name for ");
3009 d_print_comp (dpi
, d_left (dc
));
3012 case D_COMP_TYPEINFO_FN
:
3013 d_append_string_constant (dpi
, "typeinfo fn for ");
3014 d_print_comp (dpi
, d_left (dc
));
3018 d_append_string_constant (dpi
, "non-virtual thunk to ");
3019 d_print_comp (dpi
, d_left (dc
));
3022 case D_COMP_VIRTUAL_THUNK
:
3023 d_append_string_constant (dpi
, "virtual thunk to ");
3024 d_print_comp (dpi
, d_left (dc
));
3027 case D_COMP_COVARIANT_THUNK
:
3028 d_append_string_constant (dpi
, "covariant return thunk to ");
3029 d_print_comp (dpi
, d_left (dc
));
3032 case D_COMP_JAVA_CLASS
:
3033 d_append_string_constant (dpi
, "java Class for ");
3034 d_print_comp (dpi
, d_left (dc
));
3038 d_append_string_constant (dpi
, "guard variable for ");
3039 d_print_comp (dpi
, d_left (dc
));
3042 case D_COMP_REFTEMP
:
3043 d_append_string_constant (dpi
, "reference temporary for ");
3044 d_print_comp (dpi
, d_left (dc
));
3047 case D_COMP_SUB_STD
:
3048 d_append_buffer (dpi
, dc
->u
.s_string
.string
, dc
->u
.s_string
.len
);
3051 case D_COMP_RESTRICT
:
3052 case D_COMP_VOLATILE
:
3054 case D_COMP_RESTRICT_THIS
:
3055 case D_COMP_VOLATILE_THIS
:
3056 case D_COMP_CONST_THIS
:
3057 case D_COMP_VENDOR_TYPE_QUAL
:
3058 case D_COMP_POINTER
:
3059 case D_COMP_REFERENCE
:
3060 case D_COMP_COMPLEX
:
3061 case D_COMP_IMAGINARY
:
3063 /* We keep a list of modifiers on the stack. */
3064 struct d_print_mod dpm
;
3066 dpm
.next
= dpi
->modifiers
;
3067 dpi
->modifiers
= &dpm
;
3070 dpm
.templates
= dpi
->templates
;
3072 d_print_comp (dpi
, d_left (dc
));
3074 /* If the modifier didn't get printed by the type, print it
3077 d_print_mod (dpi
, dc
);
3079 dpi
->modifiers
= dpm
.next
;
3084 case D_COMP_BUILTIN_TYPE
:
3085 if ((dpi
->options
& DMGL_JAVA
) == 0)
3086 d_append_buffer (dpi
, dc
->u
.s_builtin
.type
->name
,
3087 dc
->u
.s_builtin
.type
->len
);
3089 d_append_buffer (dpi
, dc
->u
.s_builtin
.type
->java_name
,
3090 dc
->u
.s_builtin
.type
->java_len
);
3093 case D_COMP_VENDOR_TYPE
:
3094 d_print_comp (dpi
, d_left (dc
));
3097 case D_COMP_FUNCTION_TYPE
:
3099 if (d_left (dc
) != NULL
)
3101 struct d_print_mod dpm
;
3103 /* We must pass this type down as a modifier in order to
3104 print it in the right location. */
3106 dpm
.next
= dpi
->modifiers
;
3107 dpi
->modifiers
= &dpm
;
3110 dpm
.templates
= dpi
->templates
;
3112 d_print_comp (dpi
, d_left (dc
));
3114 dpi
->modifiers
= dpm
.next
;
3119 d_append_char (dpi
, ' ');
3122 d_print_function_type (dpi
, dc
, dpi
->modifiers
);
3127 case D_COMP_ARRAY_TYPE
:
3129 struct d_print_mod dpm
;
3131 /* We must pass this type down as a modifier in order to print
3132 multi-dimensional arrays correctly. */
3134 dpm
.next
= dpi
->modifiers
;
3135 dpi
->modifiers
= &dpm
;
3138 dpm
.templates
= dpi
->templates
;
3140 d_print_comp (dpi
, d_right (dc
));
3142 dpi
->modifiers
= dpm
.next
;
3147 d_print_array_type (dpi
, dc
, dpi
->modifiers
);
3152 case D_COMP_PTRMEM_TYPE
:
3154 struct d_print_mod dpm
;
3156 dpm
.next
= dpi
->modifiers
;
3157 dpi
->modifiers
= &dpm
;
3160 dpm
.templates
= dpi
->templates
;
3162 d_print_comp (dpi
, d_right (dc
));
3164 /* If the modifier didn't get printed by the type, print it
3168 d_append_char (dpi
, ' ');
3169 d_print_comp (dpi
, d_left (dc
));
3170 d_append_string_constant (dpi
, "::*");
3173 dpi
->modifiers
= dpm
.next
;
3178 case D_COMP_ARGLIST
:
3179 case D_COMP_TEMPLATE_ARGLIST
:
3180 d_print_comp (dpi
, d_left (dc
));
3181 if (d_right (dc
) != NULL
)
3183 d_append_string_constant (dpi
, ", ");
3184 d_print_comp (dpi
, d_right (dc
));
3188 case D_COMP_OPERATOR
:
3192 d_append_string_constant (dpi
, "operator");
3193 c
= dc
->u
.s_operator
.op
->name
[0];
3195 d_append_char (dpi
, ' ');
3196 d_append_buffer (dpi
, dc
->u
.s_operator
.op
->name
,
3197 dc
->u
.s_operator
.op
->len
);
3201 case D_COMP_EXTENDED_OPERATOR
:
3202 d_append_string_constant (dpi
, "operator ");
3203 d_print_comp (dpi
, dc
->u
.s_extended_operator
.name
);
3207 d_append_string_constant (dpi
, "operator ");
3208 d_print_cast (dpi
, dc
);
3212 if (d_left (dc
)->type
!= D_COMP_CAST
)
3213 d_print_expr_op (dpi
, d_left (dc
));
3216 d_append_string_constant (dpi
, "((");
3217 d_print_cast (dpi
, d_left (dc
));
3218 d_append_char (dpi
, ')');
3220 d_append_char (dpi
, '(');
3221 d_print_comp (dpi
, d_right (dc
));
3222 d_append_char (dpi
, ')');
3223 if (d_left (dc
)->type
== D_COMP_CAST
)
3224 d_append_char (dpi
, ')');
3228 if (d_right (dc
)->type
!= D_COMP_BINARY_ARGS
)
3230 d_print_error (dpi
);
3234 /* We wrap an expression which uses the greater-than operator in
3235 an extra layer of parens so that it does not get confused
3236 with the '>' which ends the template parameters. */
3237 if (d_left (dc
)->type
== D_COMP_OPERATOR
3238 && d_left (dc
)->u
.s_operator
.op
->len
== 1
3239 && d_left (dc
)->u
.s_operator
.op
->name
[0] == '>')
3240 d_append_char (dpi
, '(');
3242 d_append_char (dpi
, '(');
3243 d_print_comp (dpi
, d_left (d_right (dc
)));
3244 d_append_string_constant (dpi
, ") ");
3245 d_print_expr_op (dpi
, d_left (dc
));
3246 d_append_string_constant (dpi
, " (");
3247 d_print_comp (dpi
, d_right (d_right (dc
)));
3248 d_append_char (dpi
, ')');
3250 if (d_left (dc
)->type
== D_COMP_OPERATOR
3251 && d_left (dc
)->u
.s_operator
.op
->len
== 1
3252 && d_left (dc
)->u
.s_operator
.op
->name
[0] == '>')
3253 d_append_char (dpi
, ')');
3257 case D_COMP_BINARY_ARGS
:
3258 /* We should only see this as part of D_COMP_BINARY. */
3259 d_print_error (dpi
);
3262 case D_COMP_TRINARY
:
3263 if (d_right (dc
)->type
!= D_COMP_TRINARY_ARG1
3264 || d_right (d_right (dc
))->type
!= D_COMP_TRINARY_ARG2
)
3266 d_print_error (dpi
);
3269 d_append_char (dpi
, '(');
3270 d_print_comp (dpi
, d_left (d_right (dc
)));
3271 d_append_string_constant (dpi
, ") ");
3272 d_print_expr_op (dpi
, d_left (dc
));
3273 d_append_string_constant (dpi
, " (");
3274 d_print_comp (dpi
, d_left (d_right (d_right (dc
))));
3275 d_append_string_constant (dpi
, ") : (");
3276 d_print_comp (dpi
, d_right (d_right (d_right (dc
))));
3277 d_append_char (dpi
, ')');
3280 case D_COMP_TRINARY_ARG1
:
3281 case D_COMP_TRINARY_ARG2
:
3282 /* We should only see these are part of D_COMP_TRINARY. */
3283 d_print_error (dpi
);
3286 case D_COMP_LITERAL
:
3287 case D_COMP_LITERAL_NEG
:
3288 /* For some builtin types, produce simpler output. */
3289 if (d_left (dc
)->type
== D_COMP_BUILTIN_TYPE
)
3291 switch (d_left (dc
)->u
.s_builtin
.type
->print
)
3294 if (d_right (dc
)->type
== D_COMP_NAME
)
3296 if (dc
->type
== D_COMP_LITERAL_NEG
)
3297 d_append_char (dpi
, '-');
3298 d_print_comp (dpi
, d_right (dc
));
3304 if (d_right (dc
)->type
== D_COMP_NAME
)
3306 if (dc
->type
== D_COMP_LITERAL_NEG
)
3307 d_append_char (dpi
, '-');
3308 d_print_comp (dpi
, d_right (dc
));
3309 d_append_char (dpi
, 'l');
3315 if (d_right (dc
)->type
== D_COMP_NAME
3316 && d_right (dc
)->u
.s_name
.len
== 1
3317 && dc
->type
== D_COMP_LITERAL
)
3319 switch (d_right (dc
)->u
.s_name
.s
[0])
3322 d_append_string_constant (dpi
, "false");
3325 d_append_string_constant (dpi
, "true");
3338 d_append_char (dpi
, '(');
3339 d_print_comp (dpi
, d_left (dc
));
3340 d_append_char (dpi
, ')');
3341 if (dc
->type
== D_COMP_LITERAL_NEG
)
3342 d_append_char (dpi
, '-');
3343 d_print_comp (dpi
, d_right (dc
));
3347 d_print_error (dpi
);
3352 /* Print a Java dentifier. For Java we try to handle encoded extended
3353 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
3354 so we don't it for C++. Characters are encoded as
3358 d_print_java_identifier (dpi
, name
, len
)
3359 struct d_print_info
*dpi
;
3367 for (p
= name
; p
< end
; ++p
)
3378 for (q
= p
+ 3; q
< end
; ++q
)
3384 else if (*q
>= 'A' && *q
<= 'F')
3385 dig
= *q
- 'A' + 10;
3386 else if (*q
>= 'a' && *q
<= 'f')
3387 dig
= *q
- 'a' + 10;
3393 /* If the Unicode character is larger than 256, we don't try
3394 to deal with it here. FIXME. */
3395 if (q
< end
&& *q
== '_' && c
< 256)
3397 d_append_char (dpi
, c
);
3403 d_append_char (dpi
, *p
);
3407 /* Print a list of modifiers. SUFFIX is 1 if we are printing
3408 qualifiers on this after printing a function. */
3411 d_print_mod_list (dpi
, mods
, suffix
)
3412 struct d_print_info
*dpi
;
3413 struct d_print_mod
*mods
;
3416 struct d_print_template
*hold_dpt
;
3418 if (mods
== NULL
|| d_print_saw_error (dpi
))
3423 && (mods
->mod
->type
== D_COMP_RESTRICT_THIS
3424 || mods
->mod
->type
== D_COMP_VOLATILE_THIS
3425 || mods
->mod
->type
== D_COMP_CONST_THIS
)))
3427 d_print_mod_list (dpi
, mods
->next
, suffix
);
3433 hold_dpt
= dpi
->templates
;
3434 dpi
->templates
= mods
->templates
;
3436 if (mods
->mod
->type
== D_COMP_FUNCTION_TYPE
)
3438 d_print_function_type (dpi
, mods
->mod
, mods
->next
);
3439 dpi
->templates
= hold_dpt
;
3442 else if (mods
->mod
->type
== D_COMP_ARRAY_TYPE
)
3444 d_print_array_type (dpi
, mods
->mod
, mods
->next
);
3445 dpi
->templates
= hold_dpt
;
3448 else if (mods
->mod
->type
== D_COMP_LOCAL_NAME
)
3450 struct d_print_mod
*hold_modifiers
;
3453 /* When this is on the modifier stack, we have pulled any
3454 qualifiers off the right argument already. Otherwise, we
3455 print it as usual, but don't let the left argument see any
3458 hold_modifiers
= dpi
->modifiers
;
3459 dpi
->modifiers
= NULL
;
3460 d_print_comp (dpi
, d_left (mods
->mod
));
3461 dpi
->modifiers
= hold_modifiers
;
3463 if ((dpi
->options
& DMGL_JAVA
) == 0)
3464 d_append_string_constant (dpi
, "::");
3466 d_append_char (dpi
, '.');
3468 dc
= d_right (mods
->mod
);
3469 while (dc
->type
== D_COMP_RESTRICT_THIS
3470 || dc
->type
== D_COMP_VOLATILE_THIS
3471 || dc
->type
== D_COMP_CONST_THIS
)
3474 d_print_comp (dpi
, dc
);
3476 dpi
->templates
= hold_dpt
;
3480 d_print_mod (dpi
, mods
->mod
);
3482 dpi
->templates
= hold_dpt
;
3484 d_print_mod_list (dpi
, mods
->next
, suffix
);
3487 /* Print a modifier. */
3490 d_print_mod (dpi
, mod
)
3491 struct d_print_info
*dpi
;
3492 const struct d_comp
*mod
;
3496 case D_COMP_RESTRICT
:
3497 case D_COMP_RESTRICT_THIS
:
3498 d_append_string_constant (dpi
, " restrict");
3500 case D_COMP_VOLATILE
:
3501 case D_COMP_VOLATILE_THIS
:
3502 d_append_string_constant (dpi
, " volatile");
3505 case D_COMP_CONST_THIS
:
3506 d_append_string_constant (dpi
, " const");
3508 case D_COMP_VENDOR_TYPE_QUAL
:
3509 d_append_char (dpi
, ' ');
3510 d_print_comp (dpi
, d_right (mod
));
3512 case D_COMP_POINTER
:
3513 /* There is no pointer symbol in Java. */
3514 if ((dpi
->options
& DMGL_JAVA
) == 0)
3515 d_append_char (dpi
, '*');
3517 case D_COMP_REFERENCE
:
3518 d_append_char (dpi
, '&');
3520 case D_COMP_COMPLEX
:
3521 d_append_string_constant (dpi
, "complex ");
3523 case D_COMP_IMAGINARY
:
3524 d_append_string_constant (dpi
, "imaginary ");
3526 case D_COMP_PTRMEM_TYPE
:
3527 if (d_last_char (dpi
) != '(')
3528 d_append_char (dpi
, ' ');
3529 d_print_comp (dpi
, d_left (mod
));
3530 d_append_string_constant (dpi
, "::*");
3532 case D_COMP_TYPED_NAME
:
3533 d_print_comp (dpi
, d_left (mod
));
3536 /* Otherwise, we have something that won't go back on the
3537 modifier stack, so we can just print it. */
3538 d_print_comp (dpi
, mod
);
3543 /* Print a function type, except for the return type. */
3546 d_print_function_type (dpi
, dc
, mods
)
3547 struct d_print_info
*dpi
;
3548 const struct d_comp
*dc
;
3549 struct d_print_mod
*mods
;
3553 struct d_print_mod
*p
;
3554 struct d_print_mod
*hold_modifiers
;
3558 for (p
= mods
; p
!= NULL
; p
= p
->next
)
3564 switch (p
->mod
->type
)
3566 case D_COMP_RESTRICT
:
3567 case D_COMP_VOLATILE
:
3569 case D_COMP_VENDOR_TYPE_QUAL
:
3570 case D_COMP_POINTER
:
3571 case D_COMP_REFERENCE
:
3572 case D_COMP_COMPLEX
:
3573 case D_COMP_IMAGINARY
:
3574 case D_COMP_PTRMEM_TYPE
:
3577 case D_COMP_RESTRICT_THIS
:
3578 case D_COMP_VOLATILE_THIS
:
3579 case D_COMP_CONST_THIS
:
3588 if (d_left (dc
) != NULL
&& ! saw_mod
)
3593 switch (d_last_char (dpi
))
3601 d_append_char (dpi
, ' ');
3605 d_append_char (dpi
, '(');
3608 hold_modifiers
= dpi
->modifiers
;
3609 dpi
->modifiers
= NULL
;
3611 d_print_mod_list (dpi
, mods
, 0);
3614 d_append_char (dpi
, ')');
3616 d_append_char (dpi
, '(');
3618 if (d_right (dc
) != NULL
)
3619 d_print_comp (dpi
, d_right (dc
));
3621 d_append_char (dpi
, ')');
3623 d_print_mod_list (dpi
, mods
, 1);
3625 dpi
->modifiers
= hold_modifiers
;
3628 /* Print an array type, except for the element type. */
3631 d_print_array_type (dpi
, dc
, mods
)
3632 struct d_print_info
*dpi
;
3633 const struct d_comp
*dc
;
3634 struct d_print_mod
*mods
;
3642 struct d_print_mod
*p
;
3645 for (p
= mods
; p
!= NULL
; p
= p
->next
)
3650 if (p
->mod
->type
== D_COMP_ARRAY_TYPE
)
3664 d_append_string_constant (dpi
, " (");
3666 d_print_mod_list (dpi
, mods
, 0);
3669 d_append_char (dpi
, ')');
3673 d_append_char (dpi
, ' ');
3675 d_append_char (dpi
, '[');
3677 if (d_left (dc
) != NULL
)
3678 d_print_comp (dpi
, d_left (dc
));
3680 d_append_char (dpi
, ']');
3683 /* Print an operator in an expression. */
3686 d_print_expr_op (dpi
, dc
)
3687 struct d_print_info
*dpi
;
3688 const struct d_comp
*dc
;
3690 if (dc
->type
== D_COMP_OPERATOR
)
3691 d_append_buffer (dpi
, dc
->u
.s_operator
.op
->name
,
3692 dc
->u
.s_operator
.op
->len
);
3694 d_print_comp (dpi
, dc
);
3700 d_print_cast (dpi
, dc
)
3701 struct d_print_info
*dpi
;
3702 const struct d_comp
*dc
;
3704 if (d_left (dc
)->type
!= D_COMP_TEMPLATE
)
3705 d_print_comp (dpi
, d_left (dc
));
3708 struct d_print_mod
*hold_dpm
;
3709 struct d_print_template dpt
;
3711 /* It appears that for a templated cast operator, we need to put
3712 the template parameters in scope for the operator name, but
3713 not for the parameters. The effect is that we need to handle
3714 the template printing here. */
3716 hold_dpm
= dpi
->modifiers
;
3717 dpi
->modifiers
= NULL
;
3719 dpt
.next
= dpi
->templates
;
3720 dpi
->templates
= &dpt
;
3721 dpt
.template = d_left (dc
);
3723 d_print_comp (dpi
, d_left (d_left (dc
)));
3725 dpi
->templates
= dpt
.next
;
3727 if (d_last_char (dpi
) == '<')
3728 d_append_char (dpi
, ' ');
3729 d_append_char (dpi
, '<');
3730 d_print_comp (dpi
, d_right (d_left (dc
)));
3731 /* Avoid generating two consecutive '>' characters, to avoid
3732 the C++ syntactic ambiguity. */
3733 if (d_last_char (dpi
) == '>')
3734 d_append_char (dpi
, ' ');
3735 d_append_char (dpi
, '>');
3737 dpi
->modifiers
= hold_dpm
;
3741 /* Initialize the information structure we use to pass around
3745 d_init_info (mangled
, options
, len
, di
)
3746 const char *mangled
;
3752 di
->send
= mangled
+ len
;
3753 di
->options
= options
;
3757 /* We can not need more components than twice the number of chars in
3758 the mangled string. Most components correspond directly to
3759 chars, but the ARGLIST types are exceptions. */
3760 di
->num_comps
= 2 * len
;
3763 /* Similarly, we can not need more substitutions than there are
3764 chars in the mangled string. */
3769 di
->last_name
= NULL
;
3774 /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
3775 name, return a buffer allocated with malloc holding the demangled
3776 name. OPTIONS is the usual libiberty demangler options. On
3777 success, this sets *PALC to the allocated size of the returned
3778 buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
3779 a memory allocation failure. On failure, this returns NULL. */
3782 d_demangle (mangled
, options
, palc
)
3783 const char* mangled
;
3796 len
= strlen (mangled
);
3798 if (mangled
[0] == '_' && mangled
[1] == 'Z')
3800 else if (strncmp (mangled
, "_GLOBAL_", 8) == 0
3801 && (mangled
[8] == '.' || mangled
[8] == '_' || mangled
[8] == '$')
3802 && (mangled
[9] == 'D' || mangled
[9] == 'I')
3803 && mangled
[10] == '_')
3807 r
= malloc (40 + len
- 11);
3812 if (mangled
[9] == 'I')
3813 strcpy (r
, "global constructors keyed to ");
3815 strcpy (r
, "global destructors keyed to ");
3816 strcat (r
, mangled
+ 11);
3822 if ((options
& DMGL_TYPES
) == 0)
3827 d_init_info (mangled
, options
, len
, &di
);
3830 #ifdef CP_DYNAMIC_ARRAYS
3831 __extension__
struct d_comp comps
[di
.num_comps
];
3832 __extension__
struct d_comp
*subs
[di
.num_subs
];
3834 di
.comps
= &comps
[0];
3837 di
.comps
= (struct d_comp
*) malloc (di
.num_comps
3838 * sizeof (struct d_comp
));
3839 di
.subs
= (struct d_comp
**) malloc (di
.num_subs
3840 * sizeof (struct d_comp
*));
3841 if (di
.comps
== NULL
|| di
.subs
== NULL
)
3843 if (di
.comps
!= NULL
)
3845 if (di
.subs
!= NULL
)
3853 dc
= d_mangled_name (&di
, 1);
3857 /* If DMGL_PARAMS is set, then if we didn't consume the entire
3858 mangled string, then we didn't successfully demangle it. If
3859 DMGL_PARAMS is not set, we didn't look at the trailing
3861 if (((options
& DMGL_PARAMS
) != 0) && d_peek_char (&di
) != '\0')
3864 #ifdef CP_DEMANGLE_DEBUG
3866 printf ("failed demangling\n");
3871 /* We try to guess the length of the demangled string, to minimize
3872 calls to realloc during demangling. */
3873 estimate
= len
+ di
.expansion
+ 10 * di
.did_subs
;
3874 estimate
+= estimate
/ 8;
3878 ret
= d_print (options
, dc
, estimate
, palc
);
3880 #ifndef CP_DYNAMIC_ARRAYS
3885 #ifdef CP_DEMANGLE_DEBUG
3890 rlen
= strlen (ret
);
3891 if (rlen
> 2 * estimate
)
3892 printf ("*** Length %d much greater than estimate %d\n",
3894 else if (rlen
> estimate
)
3895 printf ("*** Length %d greater than estimate %d\n",
3897 else if (rlen
< estimate
/ 2)
3898 printf ("*** Length %d much less than estimate %d\n",
3907 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
3909 extern char *__cxa_demangle
PARAMS ((const char *, char *, size_t *, int *));
3911 /* ia64 ABI-mandated entry point in the C++ runtime library for
3912 performing demangling. MANGLED_NAME is a NUL-terminated character
3913 string containing the name to be demangled.
3915 OUTPUT_BUFFER is a region of memory, allocated with malloc, of
3916 *LENGTH bytes, into which the demangled name is stored. If
3917 OUTPUT_BUFFER is not long enough, it is expanded using realloc.
3918 OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
3919 is placed in a region of memory allocated with malloc.
3921 If LENGTH is non-NULL, the length of the buffer conaining the
3922 demangled name, is placed in *LENGTH.
3924 The return value is a pointer to the start of the NUL-terminated
3925 demangled name, or NULL if the demangling fails. The caller is
3926 responsible for deallocating this memory using free.
3928 *STATUS is set to one of the following values:
3929 0: The demangling operation succeeded.
3930 -1: A memory allocation failure occurred.
3931 -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
3932 -3: One of the arguments is invalid.
3934 The demangling is performed using the C++ ABI mangling rules, with
3938 __cxa_demangle (mangled_name
, output_buffer
, length
, status
)
3939 const char *mangled_name
;
3940 char *output_buffer
;
3950 if (mangled_name
== NULL
)
3956 if (output_buffer
!= NULL
&& length
== NULL
)
3962 demangled
= d_demangle (mangled_name
, DMGL_TYPES
, &alc
);
3964 if (demangled
== NULL
)
3973 if (output_buffer
== NULL
)
3980 if (strlen (demangled
) < *length
)
3982 strcpy (output_buffer
, demangled
);
3984 demangled
= output_buffer
;
3988 free (output_buffer
);
3998 #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
4000 /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
4001 mangled name, return a buffer allocated with malloc holding the
4002 demangled name. Otherwise, return NULL. */
4005 cplus_demangle_v3 (mangled
, options
)
4006 const char* mangled
;
4011 return d_demangle (mangled
, options
, &alc
);
4014 /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
4015 conventions, but the output formatting is a little different.
4016 This instructs the C++ demangler not to emit pointer characters ("*"), and
4017 to use Java's namespace separator symbol ("." instead of "::"). It then
4018 does an additional pass over the demangled output to replace instances
4019 of JArray<TYPE> with TYPE[]. */
4022 java_demangle_v3 (mangled
)
4023 const char* mangled
;
4031 demangled
= d_demangle (mangled
, DMGL_JAVA
| DMGL_PARAMS
, &alc
);
4033 if (demangled
== NULL
)
4039 while (*from
!= '\0')
4041 if (strncmp (from
, "JArray<", 7) == 0)
4046 else if (nesting
> 0 && *from
== '>')
4048 while (to
> demangled
&& to
[-1] == ' ')
4064 #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
4066 #ifndef IN_GLIBCPP_V3
4068 /* Demangle a string in order to find out whether it is a constructor
4069 or destructor. Return non-zero on success. Set *CTOR_KIND and
4070 *DTOR_KIND appropriately. */
4073 is_ctor_or_dtor (mangled
, ctor_kind
, dtor_kind
)
4074 const char *mangled
;
4075 enum gnu_v3_ctor_kinds
*ctor_kind
;
4076 enum gnu_v3_dtor_kinds
*dtor_kind
;
4082 *ctor_kind
= (enum gnu_v3_ctor_kinds
) 0;
4083 *dtor_kind
= (enum gnu_v3_dtor_kinds
) 0;
4085 d_init_info (mangled
, DMGL_GNU_V3
, strlen (mangled
), &di
);
4088 #ifdef CP_DYNAMIC_ARRAYS
4089 __extension__
struct d_comp comps
[di
.num_comps
];
4090 __extension__
struct d_comp
*subs
[di
.num_subs
];
4092 di
.comps
= &comps
[0];
4095 di
.comps
= (struct d_comp
*) malloc (di
.num_comps
4096 * sizeof (struct d_comp
));
4097 di
.subs
= (struct d_comp
**) malloc (di
.num_subs
4098 * sizeof (struct d_comp
*));
4099 if (di
.comps
== NULL
|| di
.subs
== NULL
)
4101 if (di
.comps
!= NULL
)
4103 if (di
.subs
!= NULL
)
4109 dc
= d_mangled_name (&di
, 1);
4111 /* Note that because we did not pass DMGL_PARAMS, we don't expect
4112 to demangle the entire string. */
4122 case D_COMP_TYPED_NAME
:
4123 case D_COMP_TEMPLATE
:
4124 case D_COMP_RESTRICT_THIS
:
4125 case D_COMP_VOLATILE_THIS
:
4126 case D_COMP_CONST_THIS
:
4129 case D_COMP_QUAL_NAME
:
4130 case D_COMP_LOCAL_NAME
:
4134 *ctor_kind
= dc
->u
.s_ctor
.kind
;
4139 *dtor_kind
= dc
->u
.s_dtor
.kind
;
4146 #ifndef CP_DYNAMIC_ARRAYS
4155 /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
4156 name. A non-zero return indicates the type of constructor. */
4158 enum gnu_v3_ctor_kinds
4159 is_gnu_v3_mangled_ctor (name
)
4162 enum gnu_v3_ctor_kinds ctor_kind
;
4163 enum gnu_v3_dtor_kinds dtor_kind
;
4165 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
4166 return (enum gnu_v3_ctor_kinds
) 0;
4171 /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
4172 name. A non-zero return indicates the type of destructor. */
4174 enum gnu_v3_dtor_kinds
4175 is_gnu_v3_mangled_dtor (name
)
4178 enum gnu_v3_ctor_kinds ctor_kind
;
4179 enum gnu_v3_dtor_kinds dtor_kind
;
4181 if (! is_ctor_or_dtor (name
, &ctor_kind
, &dtor_kind
))
4182 return (enum gnu_v3_dtor_kinds
) 0;
4186 #endif /* IN_GLIBCPP_V3 */
4188 #ifdef STANDALONE_DEMANGLER
4191 #include "dyn-string.h"
4193 static void print_usage
PARAMS ((FILE* fp
, int exit_value
));
4195 #define IS_ALPHA(CHAR) \
4196 (((CHAR) >= 'a' && (CHAR) <= 'z') \
4197 || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
4199 /* Non-zero if CHAR is a character than can occur in a mangled name. */
4200 #define is_mangled_char(CHAR) \
4201 (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
4202 || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
4204 /* The name of this program, as invoked. */
4205 const char* program_name
;
4207 /* Prints usage summary to FP and then exits with EXIT_VALUE. */
4210 print_usage (fp
, exit_value
)
4214 fprintf (fp
, "Usage: %s [options] [names ...]\n", program_name
);
4215 fprintf (fp
, "Options:\n");
4216 fprintf (fp
, " -h,--help Display this message.\n");
4217 fprintf (fp
, " -p,--no-params Don't display function parameters\n");
4218 fprintf (fp
, " -v,--verbose Produce verbose demanglings.\n");
4219 fprintf (fp
, "If names are provided, they are demangled. Otherwise filters standard input.\n");
4224 /* Option specification for getopt_long. */
4225 static const struct option long_options
[] =
4227 { "help", no_argument
, NULL
, 'h' },
4228 { "no-params", no_argument
, NULL
, 'p' },
4229 { "verbose", no_argument
, NULL
, 'v' },
4230 { NULL
, no_argument
, NULL
, 0 },
4233 /* Main entry for a demangling filter executable. It will demangle
4234 its command line arguments, if any. If none are provided, it will
4235 filter stdin to stdout, replacing any recognized mangled C++ names
4236 with their demangled equivalents. */
4245 int options
= DMGL_PARAMS
| DMGL_ANSI
| DMGL_TYPES
;
4247 /* Use the program name of this program, as invoked. */
4248 program_name
= argv
[0];
4250 /* Parse options. */
4253 opt_char
= getopt_long (argc
, argv
, "hpv", long_options
, NULL
);
4256 case '?': /* Unrecognized option. */
4257 print_usage (stderr
, 1);
4261 print_usage (stdout
, 0);
4265 options
&= ~ DMGL_PARAMS
;
4269 options
|= DMGL_VERBOSE
;
4273 while (opt_char
!= -1);
4276 /* No command line arguments were provided. Filter stdin. */
4278 dyn_string_t mangled
= dyn_string_new (3);
4281 /* Read all of input. */
4282 while (!feof (stdin
))
4286 /* Pile characters into mangled until we hit one that can't
4287 occur in a mangled name. */
4289 while (!feof (stdin
) && is_mangled_char (c
))
4291 dyn_string_append_char (mangled
, c
);
4297 if (dyn_string_length (mangled
) > 0)
4299 s
= cplus_demangle_v3 (dyn_string_buf (mangled
), options
);
4308 /* It might not have been a mangled name. Print the
4310 fputs (dyn_string_buf (mangled
), stdout
);
4313 dyn_string_clear (mangled
);
4316 /* If we haven't hit EOF yet, we've read one character that
4317 can't occur in a mangled name, so print it out. */
4322 dyn_string_delete (mangled
);
4325 /* Demangle command line arguments. */
4327 /* Loop over command line arguments. */
4328 for (i
= optind
; i
< argc
; ++i
)
4332 /* Attempt to demangle. */
4333 s
= cplus_demangle_v3 (argv
[i
], options
);
4335 /* If it worked, print the demangled name. */
4342 fprintf (stderr
, "Failed: %s\n", argv
[i
]);
4349 #endif /* STANDALONE_DEMANGLER */