1 /* Demangler for g++ V3 ABI.
2 Copyright (C) 2003 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@wasabisystems.com>.
5 This file is part of the libiberty library, which is part of GCC.
7 This file is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combined
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 /* This code implements a demangler for the g++ V3 ABI. The ABI is
32 described on this web page:
33 http://www.codesourcery.com/cxx-abi/abi.html#mangling
35 This code was written while looking at the demangler written by
36 Alex Samuel <samuel@codesourcery.com>.
38 This code first pulls the mangled name apart into a list of
39 components, and then walks the list generating the demangled
42 This file will normally define the following functions, q.v.:
43 char *cplus_demangle_v3(const char *mangled, int options)
44 char *java_demangle_v3(const char *mangled)
45 enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
46 enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
48 Preprocessor macros you can define while compiling this file:
51 If defined, this file defines the following function, q.v.:
52 char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
54 instead of cplus_demangle_v3() and java_demangle_v3().
57 If defined, this file defines only __cxa_demangle().
60 If defined, this file defines a main() function which demangles
61 any arguments, or, if none, demangles stdin.
64 If defined, turns on debugging mode, which prints information on
65 stdout about the mangled string. This is not generally useful.
82 #include "libiberty.h"
85 /* 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)
1115 peek = d_peek_char (di);
1116 if (peek == '\0' || peek == 'E')
1118 return d_make_comp (di, D_COMP_TYPED_NAME, dc,
1119 d_bare_function_type (di, has_return_type (dc)));
1123 /* <name> ::= <nested-name>
1125 ::= <unscoped-template-name> <template-args>
1128 <unscoped-name> ::= <unqualified-name>
1129 ::= St <unqualified-name>
1131 <unscoped-template-name> ::= <unscoped-name>
1135 static struct d_comp *
1139 char peek = d_peek_char (di);
1145 return d_nested_name (di);
1148 return d_local_name (di);
1154 if (d_peek_next_char (di) != 't')
1156 dc = d_substitution (di, 0);
1162 dc = d_make_comp (di, D_COMP_QUAL_NAME, d_make_name (di, "std", 3),
1163 d_unqualified_name (di));
1168 if (d_peek_char (di) != 'I')
1170 /* The grammar does not permit this case to occur if we
1171 called d_substitution() above (i.e., subst == 1). We
1172 don't bother to check. */
1176 /* This is <template-args>, which means that we just saw
1177 <unscoped-template-name>, which is a substitution
1178 candidate if we didn't just get it from a
1182 if (! d_add_substitution (di, dc))
1185 dc = d_make_comp (di, D_COMP_TEMPLATE, dc, d_template_args (di));
1192 dc = d_unqualified_name (di);
1193 if (d_peek_char (di) == 'I')
1195 /* This is <template-args>, which means that we just saw
1196 <unscoped-template-name>, which is a substitution
1198 if (! d_add_substitution (di, dc))
1200 dc = d_make_comp (di, D_COMP_TEMPLATE, dc, d_template_args (di));
1206 /* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
1207 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1210 static struct d_comp *
1215 struct d_comp **pret;
1217 if (d_next_char (di) != 'N')
1220 pret = d_cv_qualifiers (di, &ret, 1);
1224 *pret = d_prefix (di);
1228 if (d_next_char (di) != 'E')
1234 /* <prefix> ::= <prefix> <unqualified-name>
1235 ::= <template-prefix> <template-args>
1236 ::= <template-param>
1240 <template-prefix> ::= <prefix> <(template) unqualified-name>
1241 ::= <template-param>
1245 static struct d_comp *
1249 struct d_comp *ret = NULL;
1254 enum d_comp_type comb_type;
1257 peek = d_peek_char (di);
1261 /* The older code accepts a <local-name> here, but I don't see
1262 that in the grammar. The older code does not accept a
1263 <template-param> here. */
1265 comb_type = D_COMP_QUAL_NAME;
1270 dc = d_unqualified_name (di);
1271 else if (peek == 'S')
1272 dc = d_substitution (di, 1);
1273 else if (peek == 'I')
1277 comb_type = D_COMP_TEMPLATE;
1278 dc = d_template_args (di);
1280 else if (peek == 'T')
1281 dc = d_template_param (di);
1282 else if (peek == 'E')
1290 ret = d_make_comp (di, comb_type, ret, dc);
1292 if (peek != 'S' && d_peek_char (di) != 'E')
1294 if (! d_add_substitution (di, ret))
1300 /* <unqualified-name> ::= <operator-name>
1301 ::= <ctor-dtor-name>
1305 static struct d_comp *
1306 d_unqualified_name (di)
1311 peek = d_peek_char (di);
1312 if (IS_DIGIT (peek))
1313 return d_source_name (di);
1314 else if (IS_LOWER (peek))
1318 ret = d_operator_name (di);
1319 if (ret != NULL && ret->type == D_COMP_OPERATOR)
1320 di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
1323 else if (peek == 'C' || peek == 'D')
1324 return d_ctor_dtor_name (di);
1329 /* <source-name> ::= <(positive length) number> <identifier> */
1331 static struct d_comp *
1338 len = d_number (di);
1341 ret = d_identifier (di, len);
1342 di->last_name = ret;
1346 /* number ::= [n] <(non-negative decimal integer)> */
1357 peek = d_peek_char (di);
1362 peek = d_peek_char (di);
1368 if (! IS_DIGIT (peek))
1374 ret = ret * 10 + peek - '0';
1376 peek = d_peek_char (di);
1380 /* identifier ::= <(unqualified source code identifier)> */
1382 static struct d_comp *
1383 d_identifier (di, len)
1391 if (di->send - name < len)
1394 d_advance (di, len);
1396 /* A Java mangled name may have a trailing '$' if it is a C++
1397 keyword. This '$' is not included in the length count. We just
1399 if ((di->options & DMGL_JAVA) != 0
1400 && d_peek_char (di) == '$')
1403 /* Look for something which looks like a gcc encoding of an
1404 anonymous namespace, and replace it with a more user friendly
1406 if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
1407 && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
1408 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
1412 s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
1413 if ((*s == '.' || *s == '_' || *s == '$')
1416 di->expansion -= len - sizeof "(anonymous namespace)";
1417 return d_make_name (di, "(anonymous namespace)",
1418 sizeof "(anonymous namespace)" - 1);
1422 return d_make_name (di, name, len);
1425 /* operator_name ::= many different two character encodings.
1427 ::= v <digit> <source-name>
1430 #define NL(s) s, (sizeof s) - 1
1432 static const struct d_operator_info d_operators[] =
1434 { "aN", NL ("&="), 2 },
1435 { "aS", NL ("="), 2 },
1436 { "aa", NL ("&&"), 2 },
1437 { "ad", NL ("&"), 1 },
1438 { "an", NL ("&"), 2 },
1439 { "cl", NL ("()"), 0 },
1440 { "cm", NL (","), 2 },
1441 { "co", NL ("~"), 1 },
1442 { "dV", NL ("/="), 2 },
1443 { "da", NL ("delete[]"), 1 },
1444 { "de", NL ("*"), 1 },
1445 { "dl", NL ("delete"), 1 },
1446 { "dv", NL ("/"), 2 },
1447 { "eO", NL ("^="), 2 },
1448 { "eo", NL ("^"), 2 },
1449 { "eq", NL ("=="), 2 },
1450 { "ge", NL (">="), 2 },
1451 { "gt", NL (">"), 2 },
1452 { "ix", NL ("[]"), 2 },
1453 { "lS", NL ("<<="), 2 },
1454 { "le", NL ("<="), 2 },
1455 { "ls", NL ("<<"), 2 },
1456 { "lt", NL ("<"), 2 },
1457 { "mI", NL ("-="), 2 },
1458 { "mL", NL ("*="), 2 },
1459 { "mi", NL ("-"), 2 },
1460 { "ml", NL ("*"), 2 },
1461 { "mm", NL ("--"), 1 },
1462 { "na", NL ("new[]"), 1 },
1463 { "ne", NL ("!="), 2 },
1464 { "ng", NL ("-"), 1 },
1465 { "nt", NL ("!"), 1 },
1466 { "nw", NL ("new"), 1 },
1467 { "oR", NL ("|="), 2 },
1468 { "oo", NL ("||"), 2 },
1469 { "or", NL ("|"), 2 },
1470 { "pL", NL ("+="), 2 },
1471 { "pl", NL ("+"), 2 },
1472 { "pm", NL ("->*"), 2 },
1473 { "pp", NL ("++"), 1 },
1474 { "ps", NL ("+"), 1 },
1475 { "pt", NL ("->"), 2 },
1476 { "qu", NL ("?"), 3 },
1477 { "rM", NL ("%="), 2 },
1478 { "rS", NL (">>="), 2 },
1479 { "rm", NL ("%"), 2 },
1480 { "rs", NL (">>"), 2 },
1481 { "st", NL ("sizeof "), 1 },
1482 { "sz", NL ("sizeof "), 1 }
1485 static struct d_comp *
1486 d_operator_name (di)
1492 c1 = d_next_char (di);
1493 c2 = d_next_char (di);
1494 if (c1 == 'v' && IS_DIGIT (c2))
1495 return d_make_extended_operator (di, c2 - '0', d_source_name (di));
1496 else if (c1 == 'c' && c2 == 'v')
1497 return d_make_comp (di, D_COMP_CAST, d_type (di), NULL);
1501 int high = sizeof (d_operators) / sizeof (d_operators[0]);
1506 const struct d_operator_info *p;
1508 i = low + (high - low) / 2;
1509 p = d_operators + i;
1511 if (c1 == p->code[0] && c2 == p->code[1])
1512 return d_make_operator (di, p);
1514 if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
1524 /* <special-name> ::= TV <type>
1528 ::= GV <(object) name>
1529 ::= T <call-offset> <(base) encoding>
1530 ::= Tc <call-offset> <call-offset> <(base) encoding>
1531 Also g++ extensions:
1532 ::= TC <type> <(offset) number> _ <(base) type>
1538 static struct d_comp *
1544 di->expansion += 20;
1545 c = d_next_char (di);
1548 switch (d_next_char (di))
1552 return d_make_comp (di, D_COMP_VTABLE, d_type (di), NULL);
1554 di->expansion -= 10;
1555 return d_make_comp (di, D_COMP_VTT, d_type (di), NULL);
1557 return d_make_comp (di, D_COMP_TYPEINFO, d_type (di), NULL);
1559 return d_make_comp (di, D_COMP_TYPEINFO_NAME, d_type (di), NULL);
1562 if (! d_call_offset (di, 'h'))
1564 return d_make_comp (di, D_COMP_THUNK, d_encoding (di, 0), NULL);
1567 if (! d_call_offset (di, 'v'))
1569 return d_make_comp (di, D_COMP_VIRTUAL_THUNK, d_encoding (di, 0),
1573 if (! d_call_offset (di, '\0'))
1575 if (! d_call_offset (di, '\0'))
1577 return d_make_comp (di, D_COMP_COVARIANT_THUNK, d_encoding (di, 0),
1582 struct d_comp *derived_type;
1584 struct d_comp *base_type;
1586 derived_type = d_type (di);
1587 offset = d_number (di);
1590 if (d_next_char (di) != '_')
1592 base_type = d_type (di);
1593 /* We don't display the offset. FIXME: We should display
1594 it in verbose mode. */
1596 return d_make_comp (di, D_COMP_CONSTRUCTION_VTABLE, base_type,
1601 return d_make_comp (di, D_COMP_TYPEINFO_FN, d_type (di), NULL);
1603 return d_make_comp (di, D_COMP_JAVA_CLASS, d_type (di), NULL);
1611 switch (d_next_char (di))
1614 return d_make_comp (di, D_COMP_GUARD, d_name (di), NULL);
1617 return d_make_comp (di, D_COMP_REFTEMP, d_name (di), NULL);
1627 /* <call-offset> ::= h <nv-offset> _
1630 <nv-offset> ::= <(offset) number>
1632 <v-offset> ::= <(offset) number> _ <(virtual offset) number>
1634 The C parameter, if not '\0', is a character we just read which is
1635 the start of the <call-offset>.
1637 We don't display the offset information anywhere. FIXME: We should
1638 display it in verbose mode. */
1641 d_call_offset (di, c)
1646 long virtual_offset;
1649 c = d_next_char (di);
1652 offset = d_number (di);
1655 offset = d_number (di);
1656 if (d_next_char (di) != '_')
1658 virtual_offset = d_number (di);
1663 if (d_next_char (di) != '_')
1669 /* <ctor-dtor-name> ::= C1
1677 static struct d_comp *
1678 d_ctor_dtor_name (di)
1681 if (di->last_name != NULL)
1683 if (di->last_name->type == D_COMP_NAME)
1684 di->expansion += di->last_name->u.s_name.len;
1685 else if (di->last_name->type == D_COMP_SUB_STD)
1686 di->expansion += di->last_name->u.s_string.len;
1688 switch (d_next_char (di))
1692 enum gnu_v3_ctor_kinds kind;
1694 switch (d_next_char (di))
1697 kind = gnu_v3_complete_object_ctor;
1700 kind = gnu_v3_base_object_ctor;
1703 kind = gnu_v3_complete_object_allocating_ctor;
1708 return d_make_ctor (di, kind, di->last_name);
1713 enum gnu_v3_dtor_kinds kind;
1715 switch (d_next_char (di))
1718 kind = gnu_v3_deleting_dtor;
1721 kind = gnu_v3_complete_object_dtor;
1724 kind = gnu_v3_base_object_dtor;
1729 return d_make_dtor (di, kind, di->last_name);
1737 /* <type> ::= <builtin-type>
1739 ::= <class-enum-type>
1741 ::= <pointer-to-member-type>
1742 ::= <template-param>
1743 ::= <template-template-param> <template-args>
1745 ::= <CV-qualifiers> <type>
1750 ::= U <source-name> <type>
1752 <builtin-type> ::= various one letter codes
1756 static const struct d_builtin_type_info d_builtin_types[26] =
1758 /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_INT },
1759 /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
1760 /* c */ { NL ("char"), NL ("byte"), D_PRINT_INT },
1761 /* d */ { NL ("double"), NL ("double"), D_PRINT_DEFAULT },
1762 /* e */ { NL ("long double"), NL ("long double"), D_PRINT_DEFAULT },
1763 /* f */ { NL ("float"), NL ("float"), D_PRINT_DEFAULT },
1764 /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_DEFAULT },
1765 /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_INT },
1766 /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
1767 /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_INT },
1768 /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1769 /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
1770 /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_LONG },
1771 /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
1772 /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"), D_PRINT_DEFAULT },
1773 /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1774 /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1775 /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1776 /* s */ { NL ("short"), NL ("short"), D_PRINT_INT },
1777 /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_INT },
1778 /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
1779 /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
1780 /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_INT },
1781 /* x */ { NL ("long long"), NL ("long"), D_PRINT_DEFAULT },
1782 /* y */ { NL ("unsigned long long"), NL ("unsigned long long"), D_PRINT_DEFAULT },
1783 /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
1786 static struct d_comp *
1794 /* The ABI specifies that when CV-qualifiers are used, the base type
1795 is substitutable, and the fully qualified type is substitutable,
1796 but the base type with a strict subset of the CV-qualifiers is
1797 not substitutable. The natural recursive implementation of the
1798 CV-qualifiers would cause subsets to be substitutable, so instead
1799 we pull them all off now.
1801 FIXME: The ABI says that order-insensitive vendor qualifiers
1802 should be handled in the same way, but we have no way to tell
1803 which vendor qualifiers are order-insensitive and which are
1804 order-sensitive. So we just assume that they are all
1805 order-sensitive. g++ 3.4 supports only one vendor qualifier,
1806 __vector, and it treats it as order-sensitive when mangling
1809 peek = d_peek_char (di);
1810 if (peek == 'r' || peek == 'V' || peek == 'K')
1812 struct d_comp **pret;
1814 pret = d_cv_qualifiers (di, &ret, 0);
1817 *pret = d_type (di);
1818 if (! d_add_substitution (di, ret))
1827 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1828 case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
1829 case 'o': case 's': case 't':
1830 case 'v': case 'w': case 'x': case 'y': case 'z':
1831 ret = d_make_builtin_type (di, &d_builtin_types[peek - 'a']);
1832 di->expansion += ret->u.s_builtin.type->len;
1839 ret = d_make_comp (di, D_COMP_VENDOR_TYPE, d_source_name (di), NULL);
1843 ret = d_function_type (di);
1846 case '0': case '1': case '2': case '3': case '4':
1847 case '5': case '6': case '7': case '8': case '9':
1850 ret = d_class_enum_type (di);
1854 ret = d_array_type (di);
1858 ret = d_pointer_to_member_type (di);
1862 ret = d_template_param (di);
1863 if (d_peek_char (di) == 'I')
1865 /* This is <template-template-param> <template-args>. The
1866 <template-template-param> part is a substitution
1868 if (! d_add_substitution (di, ret))
1870 ret = d_make_comp (di, D_COMP_TEMPLATE, ret, d_template_args (di));
1875 /* If this is a special substitution, then it is the start of
1876 <class-enum-type>. */
1880 peek_next = d_peek_next_char (di);
1881 if (IS_DIGIT (peek_next)
1883 || IS_UPPER (peek_next))
1885 ret = d_substitution (di, 0);
1886 /* The substituted name may have been a template name and
1887 may be followed by tepmlate args. */
1888 if (d_peek_char (di) == 'I')
1889 ret = d_make_comp (di, D_COMP_TEMPLATE, ret,
1890 d_template_args (di));
1896 ret = d_class_enum_type (di);
1897 /* If the substitution was a complete type, then it is not
1898 a new substitution candidate. However, if the
1899 substitution was followed by template arguments, then
1900 the whole thing is a substitution candidate. */
1901 if (ret != NULL && ret->type == D_COMP_SUB_STD)
1909 ret = d_make_comp (di, D_COMP_POINTER, d_type (di), NULL);
1914 ret = d_make_comp (di, D_COMP_REFERENCE, d_type (di), NULL);
1919 ret = d_make_comp (di, D_COMP_COMPLEX, d_type (di), NULL);
1924 ret = d_make_comp (di, D_COMP_IMAGINARY, d_type (di), NULL);
1929 ret = d_source_name (di);
1930 ret = d_make_comp (di, D_COMP_VENDOR_TYPE_QUAL, d_type (di), ret);
1939 if (! d_add_substitution (di, ret))
1946 /* <CV-qualifiers> ::= [r] [V] [K] */
1948 static struct d_comp **
1949 d_cv_qualifiers (di, pret, member_fn)
1951 struct d_comp **pret;
1956 peek = d_peek_char (di);
1957 while (peek == 'r' || peek == 'V' || peek == 'K')
1964 t = member_fn ? D_COMP_RESTRICT_THIS : D_COMP_RESTRICT;
1965 di->expansion += sizeof "restrict";
1967 else if (peek == 'V')
1969 t = member_fn ? D_COMP_VOLATILE_THIS : D_COMP_VOLATILE;
1970 di->expansion += sizeof "volatile";
1974 t = member_fn ? D_COMP_CONST_THIS : D_COMP_CONST;
1975 di->expansion += sizeof "const";
1978 *pret = d_make_comp (di, t, NULL, NULL);
1981 pret = &d_left (*pret);
1983 peek = d_peek_char (di);
1989 /* <function-type> ::= F [Y] <bare-function-type> E */
1991 static struct d_comp *
1992 d_function_type (di)
1997 if (d_next_char (di) != 'F')
1999 if (d_peek_char (di) == 'Y')
2001 /* Function has C linkage. We don't print this information.
2002 FIXME: We should print it in verbose mode. */
2005 ret = d_bare_function_type (di, 1);
2006 if (d_next_char (di) != 'E')
2011 /* <bare-function-type> ::= <type>+ */
2013 static struct d_comp *
2014 d_bare_function_type (di, has_return_type)
2016 int has_return_type;
2018 struct d_comp *return_type;
2020 struct d_comp **ptl;
2028 struct d_comp *type;
2030 peek = d_peek_char (di);
2031 if (peek == '\0' || peek == 'E')
2036 if (has_return_type)
2039 has_return_type = 0;
2043 *ptl = d_make_comp (di, D_COMP_ARGLIST, type, NULL);
2046 ptl = &d_right (*ptl);
2050 /* There should be at least one parameter type besides the optional
2051 return type. A function which takes no arguments will have a
2052 single parameter type void. */
2056 /* If we have a single parameter type void, omit it. */
2057 if (d_right (tl) == NULL
2058 && d_left (tl)->type == D_COMP_BUILTIN_TYPE
2059 && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
2061 di->expansion -= d_left (tl)->u.s_builtin.type->len;
2065 return d_make_comp (di, D_COMP_FUNCTION_TYPE, return_type, tl);
2068 /* <class-enum-type> ::= <name> */
2070 static struct d_comp *
2071 d_class_enum_type (di)
2077 /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
2078 ::= A [<(dimension) expression>] _ <(element) type>
2081 static struct d_comp *
2088 if (d_next_char (di) != 'A')
2091 peek = d_peek_char (di);
2094 else if (IS_DIGIT (peek))
2102 peek = d_peek_char (di);
2104 while (IS_DIGIT (peek));
2105 dim = d_make_name (di, s, d_str (di) - s);
2111 dim = d_expression (di);
2116 if (d_next_char (di) != '_')
2119 return d_make_comp (di, D_COMP_ARRAY_TYPE, dim, d_type (di));
2122 /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
2124 static struct d_comp *
2125 d_pointer_to_member_type (di)
2130 struct d_comp **pmem;
2132 if (d_next_char (di) != 'M')
2137 /* The ABI specifies that any type can be a substitution source, and
2138 that M is followed by two types, and that when a CV-qualified
2139 type is seen both the base type and the CV-qualified types are
2140 substitution sources. The ABI also specifies that for a pointer
2141 to a CV-qualified member function, the qualifiers are attached to
2142 the second type. Given the grammar, a plain reading of the ABI
2143 suggests that both the CV-qualified member function and the
2144 non-qualified member function are substitution sources. However,
2145 g++ does not work that way. g++ treats only the CV-qualified
2146 member function as a substitution source. FIXME. So to work
2147 with g++, we need to pull off the CV-qualifiers here, in order to
2148 avoid calling add_substitution() in d_type(). */
2150 pmem = d_cv_qualifiers (di, &mem, 1);
2153 *pmem = d_type (di);
2155 return d_make_comp (di, D_COMP_PTRMEM_TYPE, cl, mem);
2158 /* <template-param> ::= T_
2159 ::= T <(parameter-2 non-negative) number> _
2162 static struct d_comp *
2163 d_template_param (di)
2168 if (d_next_char (di) != 'T')
2171 if (d_peek_char (di) == '_')
2175 param = d_number (di);
2181 if (d_next_char (di) != '_')
2186 return d_make_template_param (di, param);
2189 /* <template-args> ::= I <template-arg>+ E */
2191 static struct d_comp *
2192 d_template_args (di)
2195 struct d_comp *hold_last_name;
2197 struct d_comp **pal;
2199 /* Preserve the last name we saw--don't let the template arguments
2200 clobber it, as that would give us the wrong name for a subsequent
2201 constructor or destructor. */
2202 hold_last_name = di->last_name;
2204 if (d_next_char (di) != 'I')
2213 a = d_template_arg (di);
2217 *pal = d_make_comp (di, D_COMP_TEMPLATE_ARGLIST, a, NULL);
2220 pal = &d_right (*pal);
2222 if (d_peek_char (di) == 'E')
2229 di->last_name = hold_last_name;
2234 /* <template-arg> ::= <type>
2235 ::= X <expression> E
2239 static struct d_comp *
2245 switch (d_peek_char (di))
2249 ret = d_expression (di);
2250 if (d_next_char (di) != 'E')
2255 return d_expr_primary (di);
2262 /* <expression> ::= <(unary) operator-name> <expression>
2263 ::= <(binary) operator-name> <expression> <expression>
2264 ::= <(trinary) operator-name> <expression> <expression> <expression>
2266 ::= <template-param>
2267 ::= sr <type> <unqualified-name>
2268 ::= sr <type> <unqualified-name> <template-args>
2272 static struct d_comp *
2278 peek = d_peek_char (di);
2280 return d_expr_primary (di);
2281 else if (peek == 'T')
2282 return d_template_param (di);
2283 else if (peek == 's' && d_peek_next_char (di) == 'r')
2285 struct d_comp *type;
2286 struct d_comp *name;
2290 name = d_unqualified_name (di);
2291 if (d_peek_char (di) != 'I')
2292 return d_make_comp (di, D_COMP_QUAL_NAME, type, name);
2294 return d_make_comp (di, D_COMP_QUAL_NAME, type,
2295 d_make_comp (di, D_COMP_TEMPLATE, name,
2296 d_template_args (di)));
2303 op = d_operator_name (di);
2307 if (op->type == D_COMP_OPERATOR)
2308 di->expansion += op->u.s_operator.op->len - 2;
2310 if (op->type == D_COMP_OPERATOR
2311 && strcmp (op->u.s_operator.op->code, "st") == 0)
2312 return d_make_comp (di, D_COMP_UNARY, op, d_type (di));
2318 case D_COMP_OPERATOR:
2319 args = op->u.s_operator.op->args;
2321 case D_COMP_EXTENDED_OPERATOR:
2322 args = op->u.s_extended_operator.args;
2332 return d_make_comp (di, D_COMP_UNARY, op, d_expression (di));
2335 struct d_comp *left;
2337 left = d_expression (di);
2338 return d_make_comp (di, D_COMP_BINARY, op,
2339 d_make_comp (di, D_COMP_BINARY_ARGS, left,
2340 d_expression (di)));
2344 struct d_comp *first;
2345 struct d_comp *second;
2347 first = d_expression (di);
2348 second = d_expression (di);
2349 return d_make_comp (di, D_COMP_TRINARY, op,
2350 d_make_comp (di, D_COMP_TRINARY_ARG1, first,
2352 D_COMP_TRINARY_ARG2,
2354 d_expression (di))));
2362 /* <expr-primary> ::= L <type> <(value) number> E
2363 ::= L <type> <(value) float> E
2364 ::= L <mangled-name> E
2367 static struct d_comp *
2373 if (d_next_char (di) != 'L')
2375 if (d_peek_char (di) == '_')
2376 ret = d_mangled_name (di, 0);
2379 struct d_comp *type;
2385 /* If we have a type we know how to print, we aren't going to
2386 print the type name itself. */
2387 if (type->type == D_COMP_BUILTIN_TYPE
2388 && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
2389 di->expansion -= type->u.s_builtin.type->len;
2391 /* Rather than try to interpret the literal value, we just
2392 collect it as a string. Note that it's possible to have a
2393 floating point literal here. The ABI specifies that the
2394 format of such literals is machine independent. That's fine,
2395 but what's not fine is that versions of g++ up to 3.2 with
2396 -fabi-version=1 used upper case letters in the hex constant,
2397 and dumped out gcc's internal representation. That makes it
2398 hard to tell where the constant ends, and hard to dump the
2399 constant in any readable form anyhow. We don't attempt to
2400 handle these cases. */
2403 if (d_peek_char (di) == 'n')
2405 t = D_COMP_LITERAL_NEG;
2409 while (d_peek_char (di) != 'E')
2411 ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
2413 if (d_next_char (di) != 'E')
2418 /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
2419 ::= Z <(function) encoding> E s [<discriminator>]
2422 static struct d_comp *
2426 struct d_comp *function;
2428 if (d_next_char (di) != 'Z')
2431 function = d_encoding (di, 0);
2433 if (d_next_char (di) != 'E')
2436 if (d_peek_char (di) == 's')
2439 if (! d_discriminator (di))
2441 return d_make_comp (di, D_COMP_LOCAL_NAME, function,
2442 d_make_name (di, "string literal",
2443 sizeof "string literal" - 1));
2447 struct d_comp *name;
2450 if (! d_discriminator (di))
2452 return d_make_comp (di, D_COMP_LOCAL_NAME, function, name);
2456 /* <discriminator> ::= _ <(non-negative) number>
2458 We demangle the discriminator, but we don't print it out. FIXME:
2459 We should print it out in verbose mode. */
2462 d_discriminator (di)
2467 if (d_peek_char (di) != '_')
2470 discrim = d_number (di);
2476 /* Add a new substitution. */
2479 d_add_substitution (di, dc)
2485 if (di->next_sub >= di->num_subs)
2487 di->subs[di->next_sub] = dc;
2492 /* <substitution> ::= S <seq-id> _
2502 If PREFIX is non-zero, then this type is being used as a prefix in
2503 a qualified name. In this case, for the standard substitutions, we
2504 need to check whether we are being used as a prefix for a
2505 constructor or destructor, and return a full template name.
2506 Otherwise we will get something like std::iostream::~iostream()
2507 which does not correspond particularly well to any function which
2508 actually appears in the source.
2511 static const struct d_standard_sub_info standard_subs[] =
2516 { 'a', NL ("std::allocator"),
2517 NL ("std::allocator"),
2519 { 'b', NL ("std::basic_string"),
2520 NL ("std::basic_string"),
2521 NL ("basic_string") },
2522 { 's', NL ("std::string"),
2523 NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
2524 NL ("basic_string") },
2525 { 'i', NL ("std::istream"),
2526 NL ("std::basic_istream<char, std::char_traits<char> >"),
2527 NL ("basic_istream") },
2528 { 'o', NL ("std::ostream"),
2529 NL ("std::basic_ostream<char, std::char_traits<char> >"),
2530 NL ("basic_ostream") },
2531 { 'd', NL ("std::iostream"),
2532 NL ("std::basic_iostream<char, std::char_traits<char> >"),
2533 NL ("basic_iostream") }
2536 static struct d_comp *
2537 d_substitution (di, prefix)
2543 if (d_next_char (di) != 'S')
2546 c = d_next_char (di);
2547 if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
2557 id = id * 36 + c - '0';
2558 else if (IS_UPPER (c))
2559 id = id * 36 + c - 'A' + 10;
2562 c = d_next_char (di);
2569 if (id >= di->next_sub)
2574 return di->subs[id];
2579 const struct d_standard_sub_info *p;
2580 const struct d_standard_sub_info *pend;
2582 verbose = (di->options & DMGL_VERBOSE) != 0;
2583 if (! verbose && prefix)
2587 peek = d_peek_char (di);
2588 if (peek == 'C' || peek == 'D')
2592 pend = (&standard_subs[0]
2593 + sizeof standard_subs / sizeof standard_subs[0]);
2594 for (p = &standard_subs[0]; p < pend; ++p)
2601 if (p->set_last_name != NULL)
2602 di->last_name = d_make_sub (di, p->set_last_name,
2603 p->set_last_name_len);
2606 s = p->full_expansion;
2611 s = p->simple_expansion;
2612 len = p->simple_len;
2614 di->expansion += len;
2615 return d_make_sub (di, s, len);
2623 /* Resize the print buffer. */
2626 d_print_resize (dpi, add)
2627 struct d_print_info *dpi;
2632 if (dpi->buf == NULL)
2634 need = dpi->len + add;
2635 while (need > dpi->alc)
2640 newalc = dpi->alc * 2;
2641 newbuf = realloc (dpi->buf, newalc);
2646 dpi->allocation_failure = 1;
2654 /* Append a character to the print buffer. */
2657 d_print_append_char (dpi, c)
2658 struct d_print_info *dpi;
2661 if (dpi->buf != NULL)
2663 if (dpi->len >= dpi->alc)
2665 d_print_resize (dpi, 1);
2666 if (dpi->buf == NULL)
2670 dpi->buf[dpi->len] = c;
2675 /* Append a buffer to the print buffer. */
2678 d_print_append_buffer (dpi, s, l)
2679 struct d_print_info *dpi;
2683 if (dpi->buf != NULL)
2685 if (dpi->len + l > dpi->alc)
2687 d_print_resize (dpi, l);
2688 if (dpi->buf == NULL)
2692 memcpy (dpi->buf + dpi->len, s, l);
2697 /* Indicate that an error occurred during printing. */
2701 struct d_print_info *dpi;
2707 /* Turn components into a human readable string. OPTIONS is the
2708 options bits passed to the demangler. DC is the tree to print.
2709 ESTIMATE is a guess at the length of the result. This returns a
2710 string allocated by malloc, or NULL on error. On success, this
2711 sets *PALC to the size of the allocated buffer. On failure, this
2712 sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
2716 d_print (options, dc, estimate, palc)
2718 const struct d_comp *dc;
2722 struct d_print_info dpi;
2724 dpi.options = options;
2726 dpi.alc = estimate + 1;
2727 dpi.buf = malloc (dpi.alc);
2728 if (dpi.buf == NULL)
2735 dpi.templates = NULL;
2736 dpi.modifiers = NULL;
2738 dpi.allocation_failure = 0;
2740 d_print_comp (&dpi, dc);
2742 d_append_char (&dpi, '\0');
2744 if (dpi.buf != NULL)
2747 *palc = dpi.allocation_failure;
2752 /* Subroutine to handle components. */
2755 d_print_comp (dpi, dc)
2756 struct d_print_info *dpi;
2757 const struct d_comp *dc;
2761 d_print_error (dpi);
2764 if (d_print_saw_error (dpi))
2770 if ((dpi->options & DMGL_JAVA) == 0)
2771 d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
2773 d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
2776 case D_COMP_QUAL_NAME:
2777 case D_COMP_LOCAL_NAME:
2778 d_print_comp (dpi, d_left (dc));
2779 if ((dpi->options & DMGL_JAVA) == 0)
2780 d_append_string_constant (dpi, "::");
2782 d_append_char (dpi, '.');
2783 d_print_comp (dpi, d_right (dc));
2786 case D_COMP_TYPED_NAME:
2788 struct d_print_mod *hold_modifiers;
2789 struct d_comp *typed_name;
2790 struct d_print_mod adpm[4];
2792 struct d_print_template dpt;
2794 /* Pass the name down to the type so that it can be printed in
2795 the right place for the type. We also have to pass down
2796 any CV-qualifiers, which apply to the this parameter. */
2797 hold_modifiers = dpi->modifiers;
2799 typed_name = d_left (dc);
2800 while (typed_name != NULL)
2802 if (i >= sizeof adpm / sizeof adpm[0])
2804 d_print_error (dpi);
2808 adpm[i].next = dpi->modifiers;
2809 dpi->modifiers = &adpm[i];
2810 adpm[i].mod = typed_name;
2811 adpm[i].printed = 0;
2812 adpm[i].templates = dpi->templates;
2815 if (typed_name->type != D_COMP_RESTRICT_THIS
2816 && typed_name->type != D_COMP_VOLATILE_THIS
2817 && typed_name->type != D_COMP_CONST_THIS)
2820 typed_name = d_left (typed_name);
2823 /* If typed_name is a template, then it applies to the
2824 function type as well. */
2825 if (typed_name->type == D_COMP_TEMPLATE)
2827 dpt.next = dpi->templates;
2828 dpi->templates = &dpt;
2829 dpt.template = typed_name;
2832 /* If typed_name is a D_COMP_LOCAL_NAME, then there may be
2833 CV-qualifiers on its right argument which really apply
2834 here; this happens when parsing a class which is local to a
2836 if (typed_name->type == D_COMP_LOCAL_NAME)
2838 struct d_comp *local_name;
2840 local_name = d_right (typed_name);
2841 while (local_name->type == D_COMP_RESTRICT_THIS
2842 || local_name->type == D_COMP_VOLATILE_THIS
2843 || local_name->type == D_COMP_CONST_THIS)
2845 if (i >= sizeof adpm / sizeof adpm[0])
2847 d_print_error (dpi);
2851 adpm[i] = adpm[i - 1];
2852 adpm[i].next = &adpm[i - 1];
2853 dpi->modifiers = &adpm[i];
2855 adpm[i - 1].mod = local_name;
2856 adpm[i - 1].printed = 0;
2857 adpm[i - 1].templates = dpi->templates;
2860 local_name = d_left (local_name);
2864 d_print_comp (dpi, d_right (dc));
2866 if (typed_name->type == D_COMP_TEMPLATE)
2867 dpi->templates = dpt.next;
2869 /* If the modifiers didn't get printed by the type, print them
2874 if (! adpm[i].printed)
2876 d_append_char (dpi, ' ');
2877 d_print_mod (dpi, adpm[i].mod);
2881 dpi->modifiers = hold_modifiers;
2886 case D_COMP_TEMPLATE:
2888 struct d_print_mod *hold_dpm;
2890 /* Don't push modifiers into a template definition. Doing so
2891 could give the wrong definition for a template argument.
2892 Instead, treat the template essentially as a name. */
2894 hold_dpm = dpi->modifiers;
2895 dpi->modifiers = NULL;
2897 d_print_comp (dpi, d_left (dc));
2898 if (d_last_char (dpi) == '<')
2899 d_append_char (dpi, ' ');
2900 d_append_char (dpi, '<');
2901 d_print_comp (dpi, d_right (dc));
2902 /* Avoid generating two consecutive '>' characters, to avoid
2903 the C++ syntactic ambiguity. */
2904 if (d_last_char (dpi) == '>')
2905 d_append_char (dpi, ' ');
2906 d_append_char (dpi, '>');
2908 dpi->modifiers = hold_dpm;
2913 case D_COMP_TEMPLATE_PARAM:
2917 struct d_print_template *hold_dpt;
2919 if (dpi->templates == NULL)
2921 d_print_error (dpi);
2924 i = dc->u.s_number.number;
2925 for (a = d_right (dpi->templates->template);
2929 if (a->type != D_COMP_TEMPLATE_ARGLIST)
2931 d_print_error (dpi);
2938 if (i != 0 || a == NULL)
2940 d_print_error (dpi);
2944 /* While processing this parameter, we need to pop the list of
2945 templates. This is because the template parameter may
2946 itself be a reference to a parameter of an outer
2949 hold_dpt = dpi->templates;
2950 dpi->templates = hold_dpt->next;
2952 d_print_comp (dpi, d_left (a));
2954 dpi->templates = hold_dpt;
2960 d_print_comp (dpi, dc->u.s_ctor.name);
2964 d_append_char (dpi, '~');
2965 d_print_comp (dpi, dc->u.s_dtor.name);
2969 d_append_string_constant (dpi, "vtable for ");
2970 d_print_comp (dpi, d_left (dc));
2974 d_append_string_constant (dpi, "VTT for ");
2975 d_print_comp (dpi, d_left (dc));
2978 case D_COMP_CONSTRUCTION_VTABLE:
2979 d_append_string_constant (dpi, "construction vtable for ");
2980 d_print_comp (dpi, d_left (dc));
2981 d_append_string_constant (dpi, "-in-");
2982 d_print_comp (dpi, d_right (dc));
2985 case D_COMP_TYPEINFO:
2986 d_append_string_constant (dpi, "typeinfo for ");
2987 d_print_comp (dpi, d_left (dc));
2990 case D_COMP_TYPEINFO_NAME:
2991 d_append_string_constant (dpi, "typeinfo name for ");
2992 d_print_comp (dpi, d_left (dc));
2995 case D_COMP_TYPEINFO_FN:
2996 d_append_string_constant (dpi, "typeinfo fn for ");
2997 d_print_comp (dpi, d_left (dc));
3001 d_append_string_constant (dpi, "non-virtual thunk to ");
3002 d_print_comp (dpi, d_left (dc));
3005 case D_COMP_VIRTUAL_THUNK:
3006 d_append_string_constant (dpi, "virtual thunk to ");
3007 d_print_comp (dpi, d_left (dc));
3010 case D_COMP_COVARIANT_THUNK:
3011 d_append_string_constant (dpi, "covariant return thunk to ");
3012 d_print_comp (dpi, d_left (dc));
3015 case D_COMP_JAVA_CLASS:
3016 d_append_string_constant (dpi, "java Class for ");
3017 d_print_comp (dpi, d_left (dc));
3021 d_append_string_constant (dpi, "guard variable for ");
3022 d_print_comp (dpi, d_left (dc));
3025 case D_COMP_REFTEMP:
3026 d_append_string_constant (dpi, "reference temporary for ");
3027 d_print_comp (dpi, d_left (dc));
3030 case D_COMP_SUB_STD:
3031 d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
3034 case D_COMP_RESTRICT:
3035 case D_COMP_VOLATILE:
3037 case D_COMP_RESTRICT_THIS:
3038 case D_COMP_VOLATILE_THIS:
3039 case D_COMP_CONST_THIS:
3040 case D_COMP_VENDOR_TYPE_QUAL:
3041 case D_COMP_POINTER:
3042 case D_COMP_REFERENCE:
3043 case D_COMP_COMPLEX:
3044 case D_COMP_IMAGINARY:
3046 /* We keep a list of modifiers on the stack. */
3047 struct d_print_mod dpm;
3049 dpm.next = dpi->modifiers;
3050 dpi->modifiers = &dpm;
3053 dpm.templates = dpi->templates;
3055 d_print_comp (dpi, d_left (dc));
3057 /* If the modifier didn't get printed by the type, print it
3060 d_print_mod (dpi, dc);
3062 dpi->modifiers = dpm.next;
3067 case D_COMP_BUILTIN_TYPE:
3068 if ((dpi->options & DMGL_JAVA) == 0)
3069 d_append_buffer (dpi, dc->u.s_builtin.type->name,
3070 dc->u.s_builtin.type->len);
3072 d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
3073 dc->u.s_builtin.type->java_len);
3076 case D_COMP_VENDOR_TYPE:
3077 d_print_comp (dpi, d_left (dc));
3080 case D_COMP_FUNCTION_TYPE:
3082 if (d_left (dc) != NULL)
3084 struct d_print_mod dpm;
3086 /* We must pass this type down as a modifier in order to
3087 print it in the right location. */
3089 dpm.next = dpi->modifiers;
3090 dpi->modifiers = &dpm;
3093 dpm.templates = dpi->templates;
3095 d_print_comp (dpi, d_left (dc));
3097 dpi->modifiers = dpm.next;
3102 d_append_char (dpi, ' ');
3105 d_print_function_type (dpi, dc, dpi->modifiers);
3110 case D_COMP_ARRAY_TYPE:
3112 struct d_print_mod dpm;
3114 /* We must pass this type down as a modifier in order to print
3115 multi-dimensional arrays correctly. */
3117 dpm.next = dpi->modifiers;
3118 dpi->modifiers = &dpm;
3121 dpm.templates = dpi->templates;
3123 d_print_comp (dpi, d_right (dc));
3125 dpi->modifiers = dpm.next;
3130 d_print_array_type (dpi, dc, dpi->modifiers);
3135 case D_COMP_PTRMEM_TYPE:
3137 struct d_print_mod dpm;
3139 dpm.next = dpi->modifiers;
3140 dpi->modifiers = &dpm;
3143 dpm.templates = dpi->templates;
3145 d_print_comp (dpi, d_right (dc));
3147 /* If the modifier didn't get printed by the type, print it
3151 d_append_char (dpi, ' ');
3152 d_print_comp (dpi, d_left (dc));
3153 d_append_string_constant (dpi, "::*");
3156 dpi->modifiers = dpm.next;
3161 case D_COMP_ARGLIST:
3162 case D_COMP_TEMPLATE_ARGLIST:
3163 d_print_comp (dpi, d_left (dc));
3164 if (d_right (dc) != NULL)
3166 d_append_string_constant (dpi, ", ");
3167 d_print_comp (dpi, d_right (dc));
3171 case D_COMP_OPERATOR:
3175 d_append_string_constant (dpi, "operator");
3176 c = dc->u.s_operator.op->name[0];
3178 d_append_char (dpi, ' ');
3179 d_append_buffer (dpi, dc->u.s_operator.op->name,
3180 dc->u.s_operator.op->len);
3184 case D_COMP_EXTENDED_OPERATOR:
3185 d_append_string_constant (dpi, "operator ");
3186 d_print_comp (dpi, dc->u.s_extended_operator.name);
3190 d_append_string_constant (dpi, "operator ");
3191 d_print_cast (dpi, dc);
3195 if (d_left (dc)->type != D_COMP_CAST)
3196 d_print_expr_op (dpi, d_left (dc));
3199 d_append_string_constant (dpi, "((");
3200 d_print_cast (dpi, d_left (dc));
3201 d_append_char (dpi, ')');
3203 d_append_char (dpi, '(');
3204 d_print_comp (dpi, d_right (dc));
3205 d_append_char (dpi, ')');
3206 if (d_left (dc)->type == D_COMP_CAST)
3207 d_append_char (dpi, ')');
3211 if (d_right (dc)->type != D_COMP_BINARY_ARGS)
3213 d_print_error (dpi);
3217 /* We wrap an expression which uses the greater-than operator in
3218 an extra layer of parens so that it does not get confused
3219 with the '>' which ends the template parameters. */
3220 if (d_left (dc)->type == D_COMP_OPERATOR
3221 && d_left (dc)->u.s_operator.op->len == 1
3222 && d_left (dc)->u.s_operator.op->name[0] == '>')
3223 d_append_char (dpi, '(');
3225 d_append_char (dpi, '(');
3226 d_print_comp (dpi, d_left (d_right (dc)));
3227 d_append_string_constant (dpi, ") ");
3228 d_print_expr_op (dpi, d_left (dc));
3229 d_append_string_constant (dpi, " (");
3230 d_print_comp (dpi, d_right (d_right (dc)));
3231 d_append_char (dpi, ')');
3233 if (d_left (dc)->type == D_COMP_OPERATOR
3234 && d_left (dc)->u.s_operator.op->len == 1
3235 && d_left (dc)->u.s_operator.op->name[0] == '>')
3236 d_append_char (dpi, ')');
3240 case D_COMP_BINARY_ARGS:
3241 /* We should only see this as part of D_COMP_BINARY. */
3242 d_print_error (dpi);
3245 case D_COMP_TRINARY:
3246 if (d_right (dc)->type != D_COMP_TRINARY_ARG1
3247 || d_right (d_right (dc))->type != D_COMP_TRINARY_ARG2)
3249 d_print_error (dpi);
3252 d_append_char (dpi, '(');
3253 d_print_comp (dpi, d_left (d_right (dc)));
3254 d_append_string_constant (dpi, ") ");
3255 d_print_expr_op (dpi, d_left (dc));
3256 d_append_string_constant (dpi, " (");
3257 d_print_comp (dpi, d_left (d_right (d_right (dc))));
3258 d_append_string_constant (dpi, ") : (");
3259 d_print_comp (dpi, d_right (d_right (d_right (dc))));
3260 d_append_char (dpi, ')');
3263 case D_COMP_TRINARY_ARG1:
3264 case D_COMP_TRINARY_ARG2:
3265 /* We should only see these are part of D_COMP_TRINARY. */
3266 d_print_error (dpi);
3269 case D_COMP_LITERAL:
3270 case D_COMP_LITERAL_NEG:
3271 /* For some builtin types, produce simpler output. */
3272 if (d_left (dc)->type == D_COMP_BUILTIN_TYPE)
3274 switch (d_left (dc)->u.s_builtin.type->print)
3277 if (d_right (dc)->type == D_COMP_NAME)
3279 if (dc->type == D_COMP_LITERAL_NEG)
3280 d_append_char (dpi, '-');
3281 d_print_comp (dpi, d_right (dc));
3287 if (d_right (dc)->type == D_COMP_NAME)
3289 if (dc->type == D_COMP_LITERAL_NEG)
3290 d_append_char (dpi, '-');
3291 d_print_comp (dpi, d_right (dc));
3292 d_append_char (dpi, 'l');
3298 if (d_right (dc)->type == D_COMP_NAME
3299 && d_right (dc)->u.s_name.len == 1
3300 && dc->type == D_COMP_LITERAL)
3302 switch (d_right (dc)->u.s_name.s[0])
3305 d_append_string_constant (dpi, "false");
3308 d_append_string_constant (dpi, "true");
3321 d_append_char (dpi, '(');
3322 d_print_comp (dpi, d_left (dc));
3323 d_append_char (dpi, ')');
3324 if (dc->type == D_COMP_LITERAL_NEG)
3325 d_append_char (dpi, '-');
3326 d_print_comp (dpi, d_right (dc));
3330 d_print_error (dpi);
3335 /* Print a Java dentifier. For Java we try to handle encoded extended
3336 Unicode characters. The C++ ABI doesn't mention Unicode encoding,
3337 so we don't it for C++. Characters are encoded as
3341 d_print_java_identifier (dpi, name, len)
3342 struct d_print_info *dpi;
3350 for (p = name; p < end; ++p)
3361 for (q = p + 3; q < end; ++q)
3367 else if (*q >= 'A' && *q <= 'F')
3368 dig = *q - 'A' + 10;
3369 else if (*q >= 'a' && *q <= 'f')
3370 dig = *q - 'a' + 10;
3376 /* If the Unicode character is larger than 256, we don't try
3377 to deal with it here. FIXME. */
3378 if (q < end && *q == '_' && c < 256)
3380 d_append_char (dpi, c);
3386 d_append_char (dpi, *p);
3390 /* Print a list of modifiers. SUFFIX is 1 if we are printing
3391 qualifiers on this after printing a function. */
3394 d_print_mod_list (dpi, mods, suffix)
3395 struct d_print_info *dpi;
3396 struct d_print_mod *mods;
3399 struct d_print_template *hold_dpt;
3401 if (mods == NULL || d_print_saw_error (dpi))
3406 && (mods->mod->type == D_COMP_RESTRICT_THIS
3407 || mods->mod->type == D_COMP_VOLATILE_THIS
3408 || mods->mod->type == D_COMP_CONST_THIS)))
3410 d_print_mod_list (dpi, mods->next, suffix);
3416 hold_dpt = dpi->templates;
3417 dpi->templates = mods->templates;
3419 if (mods->mod->type == D_COMP_FUNCTION_TYPE)
3421 d_print_function_type (dpi, mods->mod, mods->next);
3422 dpi->templates = hold_dpt;
3425 else if (mods->mod->type == D_COMP_ARRAY_TYPE)
3427 d_print_array_type (dpi, mods->mod, mods->next);
3428 dpi->templates = hold_dpt;
3431 else if (mods->mod->type == D_COMP_LOCAL_NAME)
3433 struct d_print_mod *hold_modifiers;
3436 /* When this is on the modifier stack, we have pulled any
3437 qualifiers off the right argument already. Otherwise, we
3438 print it as usual, but don't let the left argument see any
3441 hold_modifiers = dpi->modifiers;
3442 dpi->modifiers = NULL;
3443 d_print_comp (dpi, d_left (mods->mod));
3444 dpi->modifiers = hold_modifiers;
3446 if ((dpi->options & DMGL_JAVA) == 0)
3447 d_append_string_constant (dpi, "::");
3449 d_append_char (dpi, '.');
3451 dc = d_right (mods->mod);
3452 while (dc->type == D_COMP_RESTRICT_THIS
3453 || dc->type == D_COMP_VOLATILE_THIS
3454 || dc->type == D_COMP_CONST_THIS)
3457 d_print_comp (dpi, dc);
3459 dpi->templates = hold_dpt;
3463 d_print_mod (dpi, mods->mod);
3465 dpi->templates = hold_dpt;
3467 d_print_mod_list (dpi, mods->next, suffix);
3470 /* Print a modifier. */
3473 d_print_mod (dpi, mod)
3474 struct d_print_info *dpi;
3475 const struct d_comp *mod;
3479 case D_COMP_RESTRICT:
3480 case D_COMP_RESTRICT_THIS:
3481 d_append_string_constant (dpi, " restrict");
3483 case D_COMP_VOLATILE:
3484 case D_COMP_VOLATILE_THIS:
3485 d_append_string_constant (dpi, " volatile");
3488 case D_COMP_CONST_THIS:
3489 d_append_string_constant (dpi, " const");
3491 case D_COMP_VENDOR_TYPE_QUAL:
3492 d_append_char (dpi, ' ');
3493 d_print_comp (dpi, d_right (mod));
3495 case D_COMP_POINTER:
3496 /* There is no pointer symbol in Java. */
3497 if ((dpi->options & DMGL_JAVA) == 0)
3498 d_append_char (dpi, '*');
3500 case D_COMP_REFERENCE:
3501 d_append_char (dpi, '&');
3503 case D_COMP_COMPLEX:
3504 d_append_string_constant (dpi, "complex ");
3506 case D_COMP_IMAGINARY:
3507 d_append_string_constant (dpi, "imaginary ");
3509 case D_COMP_PTRMEM_TYPE:
3510 if (d_last_char (dpi) != '(')
3511 d_append_char (dpi, ' ');
3512 d_print_comp (dpi, d_left (mod));
3513 d_append_string_constant (dpi, "::*");
3515 case D_COMP_TYPED_NAME:
3516 d_print_comp (dpi, d_left (mod));
3519 /* Otherwise, we have something that won't go back on the
3520 modifier stack, so we can just print it. */
3521 d_print_comp (dpi, mod);
3526 /* Print a function type, except for the return type. */
3529 d_print_function_type (dpi, dc, mods)
3530 struct d_print_info *dpi;
3531 const struct d_comp *dc;
3532 struct d_print_mod *mods;
3536 struct d_print_mod *p;
3537 struct d_print_mod *hold_modifiers;
3541 for (p = mods; p != NULL; p = p->next)
3547 switch (p->mod->type)