1 /* Demangler for the D programming language
2 Copyright (C) 2014-2021 Free Software Foundation, Inc.
3 Written by Iain Buclaw (ibuclaw@gdcproject.org)
5 This file is part of the libiberty library.
6 Libiberty is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 In addition to the permissions in the GNU Library General Public
12 License, the Free Software Foundation gives you unlimited permission
13 to link the compiled version of this file into combinations with other
14 programs, and to distribute those combinations without any restriction
15 coming from the use of this file. (The Library Public License
16 restrictions do apply in other respects; for example, they cover
17 modification of the file, and distribution when not linked into a
20 Libiberty is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 Library General Public License for more details.
25 You should have received a copy of the GNU Library General Public
26 License along with libiberty; see the file COPYING.LIB.
27 If not, see <http://www.gnu.org/licenses/>. */
29 /* This file exports one function; dlang_demangle. */
38 #include "safe-ctype.h"
40 #include <sys/types.h>
49 #include "libiberty.h"
52 #define ULONG_MAX (~0UL)
55 #define UINT_MAX (~0U)
58 /* A mini string-handling package */
60 typedef struct string
/* Beware: these aren't required to be */
61 { /* '\0' terminated. */
62 char *b
; /* pointer to start of string */
63 char *p
; /* pointer after last character */
64 char *e
; /* pointer after end of allocated space */
68 string_need (string
*s
, size_t n
)
78 s
->p
= s
->b
= XNEWVEC (char, n
);
81 else if ((size_t) (s
->e
- s
->p
) < n
)
86 s
->b
= XRESIZEVEC (char, s
->b
, n
);
93 string_delete (string
*s
)
98 s
->b
= s
->e
= s
->p
= NULL
;
103 string_init (string
*s
)
105 s
->b
= s
->p
= s
->e
= NULL
;
109 string_length (string
*s
)
119 string_setlength (string
*s
, int n
)
121 if (n
- string_length (s
) < 0)
128 string_append (string
*p
, const char *s
)
130 size_t n
= strlen (s
);
137 string_appendn (string
*p
, const char *s
, size_t n
)
148 string_prependn (string
*p
, const char *s
, size_t n
)
155 for (q
= p
->p
- 1; q
>= p
->b
; q
--)
165 string_prepend (string
*p
, const char *s
)
167 if (s
!= NULL
&& *s
!= '\0')
169 string_prependn (p
, s
, strlen (s
));
173 /* Demangle information structure we pass around. */
176 /* The string we are demangling. */
178 /* The index of the last back reference. */
182 /* Pass as the LEN to dlang_parse_template if symbol length is not known. */
183 #define TEMPLATE_LENGTH_UNKNOWN (-1UL)
185 /* Prototypes for forward referenced functions */
186 static const char *dlang_function_type (string
*, const char *,
187 struct dlang_info
*);
189 static const char *dlang_function_args (string
*, const char *,
190 struct dlang_info
*);
192 static const char *dlang_type (string
*, const char *, struct dlang_info
*);
194 static const char *dlang_value (string
*, const char *, const char *, char,
195 struct dlang_info
*);
197 static const char *dlang_parse_qualified (string
*, const char *,
198 struct dlang_info
*, int);
200 static const char *dlang_parse_mangle (string
*, const char *,
201 struct dlang_info
*);
203 static const char *dlang_parse_tuple (string
*, const char *,
204 struct dlang_info
*);
206 static const char *dlang_parse_template (string
*, const char *,
207 struct dlang_info
*, unsigned long);
209 static const char *dlang_lname (string
*, const char *, unsigned long);
212 /* Extract the number from MANGLED, and assign the result to RET.
213 Return the remaining string on success or NULL on failure.
214 A result larger than UINT_MAX is considered a failure. */
216 dlang_number (const char *mangled
, unsigned long *ret
)
218 /* Return NULL if trying to extract something that isn't a digit. */
219 if (mangled
== NULL
|| !ISDIGIT (*mangled
))
222 unsigned long val
= 0;
224 while (ISDIGIT (*mangled
))
226 unsigned long digit
= mangled
[0] - '0';
228 /* Check for overflow. */
229 if (val
> (UINT_MAX
- digit
) / 10)
232 val
= val
* 10 + digit
;
236 if (*mangled
== '\0')
243 /* Extract the hex-digit from MANGLED, and assign the result to RET.
244 Return the remaining string on success or NULL on failure. */
246 dlang_hexdigit (const char *mangled
, char *ret
)
250 /* Return NULL if trying to extract something that isn't a hexdigit. */
251 if (mangled
== NULL
|| !ISXDIGIT (mangled
[0]) || !ISXDIGIT (mangled
[1]))
256 (*ret
) = (c
- (ISUPPER (c
) ? 'A' : 'a') + 10);
262 (*ret
) = (*ret
<< 4) | (c
- (ISUPPER (c
) ? 'A' : 'a') + 10);
264 (*ret
) = (*ret
<< 4) | (c
- '0');
271 /* Extract the function calling convention from MANGLED and
272 return 1 on success or 0 on failure. */
274 dlang_call_convention_p (const char *mangled
)
278 case 'F': case 'U': case 'V':
279 case 'W': case 'R': case 'Y':
287 /* Extract the back reference position from MANGLED, and assign the result
288 to RET. Return the remaining string on success or NULL on failure.
289 A result <= 0 is a failure. */
291 dlang_decode_backref (const char *mangled
, long *ret
)
293 /* Return NULL if trying to extract something that isn't a digit. */
294 if (mangled
== NULL
|| !ISALPHA (*mangled
))
297 /* Any identifier or non-basic type that has been emitted to the mangled
298 symbol before will not be emitted again, but is referenced by a special
299 sequence encoding the relative position of the original occurrence in the
302 Numbers in back references are encoded with base 26 by upper case letters
303 A-Z for higher digits but lower case letters a-z for the last digit.
310 unsigned long val
= 0;
312 while (ISALPHA (*mangled
))
314 /* Check for overflow. */
315 if (val
> (ULONG_MAX
- 25) / 26)
320 if (mangled
[0] >= 'a' && mangled
[0] <= 'z')
322 val
+= mangled
[0] - 'a';
329 val
+= mangled
[0] - 'A';
336 /* Extract the symbol pointed at by the back reference and assign the result
337 to RET. Return the remaining string on success or NULL on failure. */
339 dlang_backref (const char *mangled
, const char **ret
, struct dlang_info
*info
)
343 if (mangled
== NULL
|| *mangled
!= 'Q')
346 /* Position of 'Q'. */
347 const char *qpos
= mangled
;
351 mangled
= dlang_decode_backref (mangled
, &refpos
);
355 if (refpos
> qpos
- info
->s
)
358 /* Set the position of the back reference. */
359 (*ret
) = qpos
- refpos
;
364 /* Demangle a back referenced symbol from MANGLED and append it to DECL.
365 Return the remaining string on success or NULL on failure. */
367 dlang_symbol_backref (string
*decl
, const char *mangled
,
368 struct dlang_info
*info
)
370 /* An identifier back reference always points to a digit 0 to 9.
379 /* Get position of the back reference. */
380 mangled
= dlang_backref (mangled
, &backref
, info
);
382 /* Must point to a simple identifier. */
383 backref
= dlang_number (backref
, &len
);
387 backref
= dlang_lname (decl
, backref
, len
);
394 /* Demangle a back referenced type from MANGLED and append it to DECL.
395 IS_FUNCTION is 1 if the back referenced type is expected to be a function.
396 Return the remaining string on success or NULL on failure. */
398 dlang_type_backref (string
*decl
, const char *mangled
, struct dlang_info
*info
,
401 /* A type back reference always points to a letter.
409 /* If we appear to be moving backwards through the mangle string, then
410 bail as this may be a recursive back reference. */
411 if (mangled
- info
->s
>= info
->last_backref
)
414 int save_refpos
= info
->last_backref
;
415 info
->last_backref
= mangled
- info
->s
;
417 /* Get position of the back reference. */
418 mangled
= dlang_backref (mangled
, &backref
, info
);
420 /* Must point to a type. */
422 backref
= dlang_function_type (decl
, backref
, info
);
424 backref
= dlang_type (decl
, backref
, info
);
426 info
->last_backref
= save_refpos
;
434 /* Extract the beginning of a symbol name from MANGLED and
435 return 1 on success or 0 on failure. */
437 dlang_symbol_name_p (const char *mangled
, struct dlang_info
*info
)
440 const char *qref
= mangled
;
442 if (ISDIGIT (*mangled
))
445 if (mangled
[0] == '_' && mangled
[1] == '_'
446 && (mangled
[2] == 'T' || mangled
[2] == 'U'))
452 mangled
= dlang_decode_backref (mangled
+ 1, &ret
);
453 if (mangled
== NULL
|| ret
> qref
- info
->s
)
456 return ISDIGIT (qref
[-ret
]);
459 /* Demangle the calling convention from MANGLED and append it to DECL.
460 Return the remaining string on success or NULL on failure. */
462 dlang_call_convention (string
*decl
, const char *mangled
)
464 if (mangled
== NULL
|| *mangled
== '\0')
474 string_append (decl
, "extern(C) ");
476 case 'W': /* (Windows) */
478 string_append (decl
, "extern(Windows) ");
480 case 'V': /* (Pascal) */
482 string_append (decl
, "extern(Pascal) ");
484 case 'R': /* (C++) */
486 string_append (decl
, "extern(C++) ");
488 case 'Y': /* (Objective-C) */
490 string_append (decl
, "extern(Objective-C) ");
499 /* Extract the type modifiers from MANGLED and append them to DECL.
500 Returns the remaining signature on success or NULL on failure. */
502 dlang_type_modifiers (string
*decl
, const char *mangled
)
504 if (mangled
== NULL
|| *mangled
== '\0')
509 case 'x': /* const */
511 string_append (decl
, " const");
513 case 'y': /* immutable */
515 string_append (decl
, " immutable");
517 case 'O': /* shared */
519 string_append (decl
, " shared");
520 return dlang_type_modifiers (decl
, mangled
);
523 if (*mangled
== 'g') /* wild */
526 string_append (decl
, " inout");
527 return dlang_type_modifiers (decl
, mangled
);
537 /* Demangle the D function attributes from MANGLED and append it to DECL.
538 Return the remaining string on success or NULL on failure. */
540 dlang_attributes (string
*decl
, const char *mangled
)
542 if (mangled
== NULL
|| *mangled
== '\0')
545 while (*mangled
== 'N')
552 string_append (decl
, "pure ");
554 case 'b': /* nothrow */
556 string_append (decl
, "nothrow ");
560 string_append (decl
, "ref ");
562 case 'd': /* @property */
564 string_append (decl
, "@property ");
566 case 'e': /* @trusted */
568 string_append (decl
, "@trusted ");
570 case 'f': /* @safe */
572 string_append (decl
, "@safe ");
578 /* inout parameter is represented as 'Ng'.
579 vector parameter is represented as 'Nh'.
580 return parameter is represented as 'Nk'.
581 typeof(*null) parameter is represented as 'Nn'.
582 If we see this, then we know we're really in the
583 parameter list. Rewind and break. */
586 case 'i': /* @nogc */
588 string_append (decl
, "@nogc ");
590 case 'j': /* return */
592 string_append (decl
, "return ");
594 case 'l': /* scope */
596 string_append (decl
, "scope ");
598 case 'm': /* @live */
600 string_append (decl
, "@live ");
603 default: /* unknown attribute */
612 /* Demangle the function type from MANGLED without the return type.
613 The arguments are appended to ARGS, the calling convention is appended
614 to CALL and attributes are appended to ATTR. Any of these can be NULL
615 to throw the information away. Return the remaining string on success
616 or NULL on failure. */
618 dlang_function_type_noreturn (string
*args
, string
*call
, string
*attr
,
619 const char *mangled
, struct dlang_info
*info
)
624 /* Skip over calling convention and attributes. */
625 mangled
= dlang_call_convention (call
? call
: &dump
, mangled
);
626 mangled
= dlang_attributes (attr
? attr
: &dump
, mangled
);
629 string_append (args
, "(");
631 mangled
= dlang_function_args (args
? args
: &dump
, mangled
, info
);
633 string_append (args
, ")");
635 string_delete (&dump
);
639 /* Demangle the function type from MANGLED and append it to DECL.
640 Return the remaining string on success or NULL on failure. */
642 dlang_function_type (string
*decl
, const char *mangled
, struct dlang_info
*info
)
644 string attr
, args
, type
;
646 if (mangled
== NULL
|| *mangled
== '\0')
649 /* The order of the mangled string is:
650 CallConvention FuncAttrs Arguments ArgClose Type
652 The demangled string is re-ordered as:
653 CallConvention Type Arguments FuncAttrs
659 mangled
= dlang_function_type_noreturn (&args
, decl
, &attr
, mangled
, info
);
661 /* Function return type. */
662 mangled
= dlang_type (&type
, mangled
, info
);
664 /* Append to decl in order. */
665 string_appendn (decl
, type
.b
, string_length (&type
));
666 string_appendn (decl
, args
.b
, string_length (&args
));
667 string_append (decl
, " ");
668 string_appendn (decl
, attr
.b
, string_length (&attr
));
670 string_delete (&attr
);
671 string_delete (&args
);
672 string_delete (&type
);
676 /* Demangle the argument list from MANGLED and append it to DECL.
677 Return the remaining string on success or NULL on failure. */
679 dlang_function_args (string
*decl
, const char *mangled
, struct dlang_info
*info
)
683 while (mangled
&& *mangled
!= '\0')
687 case 'X': /* (variadic T t...) style. */
689 string_append (decl
, "...");
691 case 'Y': /* (variadic T t, ...) style. */
694 string_append (decl
, ", ");
695 string_append (decl
, "...");
697 case 'Z': /* Normal function. */
703 string_append (decl
, ", ");
705 if (*mangled
== 'M') /* scope(T) */
708 string_append (decl
, "scope ");
711 if (mangled
[0] == 'N' && mangled
[1] == 'k') /* return(T) */
714 string_append (decl
, "return ");
719 case 'I': /* in(T) */
721 string_append (decl
, "in ");
722 if (*mangled
== 'K') /* in ref(T) */
725 string_append (decl
, "ref ");
728 case 'J': /* out(T) */
730 string_append (decl
, "out ");
732 case 'K': /* ref(T) */
734 string_append (decl
, "ref ");
736 case 'L': /* lazy(T) */
738 string_append (decl
, "lazy ");
741 mangled
= dlang_type (decl
, mangled
, info
);
747 /* Demangle the type from MANGLED and append it to DECL.
748 Return the remaining string on success or NULL on failure. */
750 dlang_type (string
*decl
, const char *mangled
, struct dlang_info
*info
)
752 if (mangled
== NULL
|| *mangled
== '\0')
757 case 'O': /* shared(T) */
759 string_append (decl
, "shared(");
760 mangled
= dlang_type (decl
, mangled
, info
);
761 string_append (decl
, ")");
763 case 'x': /* const(T) */
765 string_append (decl
, "const(");
766 mangled
= dlang_type (decl
, mangled
, info
);
767 string_append (decl
, ")");
769 case 'y': /* immutable(T) */
771 string_append (decl
, "immutable(");
772 mangled
= dlang_type (decl
, mangled
, info
);
773 string_append (decl
, ")");
777 if (*mangled
== 'g') /* wild(T) */
780 string_append (decl
, "inout(");
781 mangled
= dlang_type (decl
, mangled
, info
);
782 string_append (decl
, ")");
785 else if (*mangled
== 'h') /* vector(T) */
788 string_append (decl
, "__vector(");
789 mangled
= dlang_type (decl
, mangled
, info
);
790 string_append (decl
, ")");
793 else if (*mangled
== 'n') /* typeof(*null) */
796 string_append (decl
, "typeof(*null)");
801 case 'A': /* dynamic array (T[]) */
803 mangled
= dlang_type (decl
, mangled
, info
);
804 string_append (decl
, "[]");
806 case 'G': /* static array (T[N]) */
813 while (ISDIGIT (*mangled
))
818 mangled
= dlang_type (decl
, mangled
, info
);
819 string_append (decl
, "[");
820 string_appendn (decl
, numptr
, num
);
821 string_append (decl
, "]");
824 case 'H': /* associative array (T[T]) */
831 mangled
= dlang_type (&type
, mangled
, info
);
832 sztype
= string_length (&type
);
834 mangled
= dlang_type (decl
, mangled
, info
);
835 string_append (decl
, "[");
836 string_appendn (decl
, type
.b
, sztype
);
837 string_append (decl
, "]");
839 string_delete (&type
);
842 case 'P': /* pointer (T*) */
844 if (!dlang_call_convention_p (mangled
))
846 mangled
= dlang_type (decl
, mangled
, info
);
847 string_append (decl
, "*");
851 case 'F': /* function T (D) */
852 case 'U': /* function T (C) */
853 case 'W': /* function T (Windows) */
854 case 'V': /* function T (Pascal) */
855 case 'R': /* function T (C++) */
856 case 'Y': /* function T (Objective-C) */
857 /* Function pointer types don't include the trailing asterisk. */
858 mangled
= dlang_function_type (decl
, mangled
, info
);
859 string_append (decl
, "function");
861 case 'C': /* class T */
862 case 'S': /* struct T */
863 case 'E': /* enum T */
864 case 'T': /* typedef T */
866 return dlang_parse_qualified (decl
, mangled
, info
, 0);
867 case 'D': /* delegate T */
874 mangled
= dlang_type_modifiers (&mods
, mangled
);
875 szmods
= string_length (&mods
);
877 /* Back referenced function type. */
879 mangled
= dlang_type_backref (decl
, mangled
, info
, 1);
881 mangled
= dlang_function_type (decl
, mangled
, info
);
883 string_append (decl
, "delegate");
884 string_appendn (decl
, mods
.b
, szmods
);
886 string_delete (&mods
);
889 case 'B': /* tuple T */
891 return dlang_parse_tuple (decl
, mangled
, info
);
896 string_append (decl
, "typeof(null)");
900 string_append (decl
, "void");
904 string_append (decl
, "byte");
908 string_append (decl
, "ubyte");
912 string_append (decl
, "short");
916 string_append (decl
, "ushort");
920 string_append (decl
, "int");
924 string_append (decl
, "uint");
928 string_append (decl
, "long");
932 string_append (decl
, "ulong");
936 string_append (decl
, "float");
940 string_append (decl
, "double");
944 string_append (decl
, "real");
947 /* Imaginary and Complex types */
950 string_append (decl
, "ifloat");
954 string_append (decl
, "idouble");
958 string_append (decl
, "ireal");
962 string_append (decl
, "cfloat");
966 string_append (decl
, "cdouble");
970 string_append (decl
, "creal");
976 string_append (decl
, "bool");
980 string_append (decl
, "char");
984 string_append (decl
, "wchar");
988 string_append (decl
, "dchar");
996 string_append (decl
, "cent");
1000 string_append (decl
, "ucent");
1005 /* Back referenced type. */
1007 return dlang_type_backref (decl
, mangled
, info
, 0);
1009 default: /* unhandled */
1014 /* Extract the identifier from MANGLED and append it to DECL.
1015 Return the remaining string on success or NULL on failure. */
1017 dlang_identifier (string
*decl
, const char *mangled
, struct dlang_info
*info
)
1021 if (mangled
== NULL
|| *mangled
== '\0')
1024 if (*mangled
== 'Q')
1025 return dlang_symbol_backref (decl
, mangled
, info
);
1027 /* May be a template instance without a length prefix. */
1028 if (mangled
[0] == '_' && mangled
[1] == '_'
1029 && (mangled
[2] == 'T' || mangled
[2] == 'U'))
1030 return dlang_parse_template (decl
, mangled
, info
, TEMPLATE_LENGTH_UNKNOWN
);
1032 const char *endptr
= dlang_number (mangled
, &len
);
1034 if (endptr
== NULL
|| len
== 0)
1037 if (strlen (endptr
) < len
)
1042 /* May be a template instance with a length prefix. */
1043 if (len
>= 5 && mangled
[0] == '_' && mangled
[1] == '_'
1044 && (mangled
[2] == 'T' || mangled
[2] == 'U'))
1045 return dlang_parse_template (decl
, mangled
, info
, len
);
1047 return dlang_lname (decl
, mangled
, len
);
1050 /* Extract the plain identifier from MANGLED and prepend/append it to DECL
1051 with special treatment for some magic compiler generted symbols.
1052 Return the remaining string on success or NULL on failure. */
1054 dlang_lname (string
*decl
, const char *mangled
, unsigned long len
)
1059 if (strncmp (mangled
, "__ctor", len
) == 0)
1061 /* Constructor symbol for a class/struct. */
1062 string_append (decl
, "this");
1066 else if (strncmp (mangled
, "__dtor", len
) == 0)
1068 /* Destructor symbol for a class/struct. */
1069 string_append (decl
, "~this");
1073 else if (strncmp (mangled
, "__initZ", len
+ 1) == 0)
1075 /* The static initialiser for a given symbol. */
1076 string_prepend (decl
, "initializer for ");
1077 string_setlength (decl
, string_length (decl
) - 1);
1081 else if (strncmp (mangled
, "__vtblZ", len
+ 1) == 0)
1083 /* The vtable symbol for a given class. */
1084 string_prepend (decl
, "vtable for ");
1085 string_setlength (decl
, string_length (decl
) - 1);
1092 if (strncmp (mangled
, "__ClassZ", len
+ 1) == 0)
1094 /* The classinfo symbol for a given class. */
1095 string_prepend (decl
, "ClassInfo for ");
1096 string_setlength (decl
, string_length (decl
) - 1);
1103 if (strncmp (mangled
, "__postblitMFZ", len
+ 3) == 0)
1105 /* Postblit symbol for a struct. */
1106 string_append (decl
, "this(this)");
1113 if (strncmp (mangled
, "__InterfaceZ", len
+ 1) == 0)
1115 /* The interface symbol for a given class. */
1116 string_prepend (decl
, "Interface for ");
1117 string_setlength (decl
, string_length (decl
) - 1);
1124 if (strncmp (mangled
, "__ModuleInfoZ", len
+ 1) == 0)
1126 /* The ModuleInfo symbol for a given module. */
1127 string_prepend (decl
, "ModuleInfo for ");
1128 string_setlength (decl
, string_length (decl
) - 1);
1135 string_appendn (decl
, mangled
, len
);
1141 /* Extract the integer value from MANGLED and append it to DECL,
1142 where TYPE is the type it should be represented as.
1143 Return the remaining string on success or NULL on failure. */
1145 dlang_parse_integer (string
*decl
, const char *mangled
, char type
)
1147 if (type
== 'a' || type
== 'u' || type
== 'w')
1149 /* Parse character value. */
1151 int pos
= sizeof(value
);
1155 mangled
= dlang_number (mangled
, &val
);
1156 if (mangled
== NULL
)
1159 string_append (decl
, "'");
1161 if (type
== 'a' && val
>= 0x20 && val
< 0x7F)
1163 /* Represent as a character literal. */
1164 char c
= (char) val
;
1165 string_appendn (decl
, &c
, 1);
1169 /* Represent as a hexadecimal value. */
1172 case 'a': /* char */
1173 string_append (decl
, "\\x");
1176 case 'u': /* wchar */
1177 string_append (decl
, "\\u");
1180 case 'w': /* dchar */
1181 string_append (decl
, "\\U");
1188 int digit
= val
% 16;
1191 value
[--pos
] = (char)(digit
+ '0');
1193 value
[--pos
] = (char)((digit
- 10) + 'a');
1199 for (; width
> 0; width
--)
1202 string_appendn (decl
, &(value
[pos
]), sizeof(value
) - pos
);
1204 string_append (decl
, "'");
1206 else if (type
== 'b')
1208 /* Parse boolean value. */
1211 mangled
= dlang_number (mangled
, &val
);
1212 if (mangled
== NULL
)
1215 string_append (decl
, val
? "true" : "false");
1219 /* Parse integer value. */
1220 const char *numptr
= mangled
;
1223 if (! ISDIGIT (*mangled
))
1226 while (ISDIGIT (*mangled
))
1231 string_appendn (decl
, numptr
, num
);
1233 /* Append suffix. */
1236 case 'h': /* ubyte */
1237 case 't': /* ushort */
1238 case 'k': /* uint */
1239 string_append (decl
, "u");
1241 case 'l': /* long */
1242 string_append (decl
, "L");
1244 case 'm': /* ulong */
1245 string_append (decl
, "uL");
1253 /* Extract the floating-point value from MANGLED and append it to DECL.
1254 Return the remaining string on success or NULL on failure. */
1256 dlang_parse_real (string
*decl
, const char *mangled
)
1258 /* Handle NAN and +-INF. */
1259 if (strncmp (mangled
, "NAN", 3) == 0)
1261 string_append (decl
, "NaN");
1265 else if (strncmp (mangled
, "INF", 3) == 0)
1267 string_append (decl
, "Inf");
1271 else if (strncmp (mangled
, "NINF", 4) == 0)
1273 string_append (decl
, "-Inf");
1278 /* Hexadecimal prefix and leading bit. */
1279 if (*mangled
== 'N')
1281 string_append (decl
, "-");
1285 if (!ISXDIGIT (*mangled
))
1288 string_append (decl
, "0x");
1289 string_appendn (decl
, mangled
, 1);
1290 string_append (decl
, ".");
1294 while (ISXDIGIT (*mangled
))
1296 string_appendn (decl
, mangled
, 1);
1301 if (*mangled
!= 'P')
1304 string_append (decl
, "p");
1307 if (*mangled
== 'N')
1309 string_append (decl
, "-");
1313 while (ISDIGIT (*mangled
))
1315 string_appendn (decl
, mangled
, 1);
1322 /* Extract the string value from MANGLED and append it to DECL.
1323 Return the remaining string on success or NULL on failure. */
1325 dlang_parse_string (string
*decl
, const char *mangled
)
1327 char type
= *mangled
;
1331 mangled
= dlang_number (mangled
, &len
);
1332 if (mangled
== NULL
|| *mangled
!= '_')
1336 string_append (decl
, "\"");
1340 const char *endptr
= dlang_hexdigit (mangled
, &val
);
1345 /* Sanitize white and non-printable characters. */
1349 string_append (decl
, " ");
1352 string_append (decl
, "\\t");
1355 string_append (decl
, "\\n");
1358 string_append (decl
, "\\r");
1361 string_append (decl
, "\\f");
1364 string_append (decl
, "\\v");
1369 string_appendn (decl
, &val
, 1);
1372 string_append (decl
, "\\x");
1373 string_appendn (decl
, mangled
, 2);
1379 string_append (decl
, "\"");
1382 string_appendn (decl
, &type
, 1);
1387 /* Extract the static array value from MANGLED and append it to DECL.
1388 Return the remaining string on success or NULL on failure. */
1390 dlang_parse_arrayliteral (string
*decl
, const char *mangled
,
1391 struct dlang_info
*info
)
1393 unsigned long elements
;
1395 mangled
= dlang_number (mangled
, &elements
);
1396 if (mangled
== NULL
)
1399 string_append (decl
, "[");
1402 mangled
= dlang_value (decl
, mangled
, NULL
, '\0', info
);
1403 if (mangled
== NULL
)
1407 string_append (decl
, ", ");
1410 string_append (decl
, "]");
1414 /* Extract the associative array value from MANGLED and append it to DECL.
1415 Return the remaining string on success or NULL on failure. */
1417 dlang_parse_assocarray (string
*decl
, const char *mangled
,
1418 struct dlang_info
*info
)
1420 unsigned long elements
;
1422 mangled
= dlang_number (mangled
, &elements
);
1423 if (mangled
== NULL
)
1426 string_append (decl
, "[");
1429 mangled
= dlang_value (decl
, mangled
, NULL
, '\0', info
);
1430 if (mangled
== NULL
)
1433 string_append (decl
, ":");
1434 mangled
= dlang_value (decl
, mangled
, NULL
, '\0', info
);
1435 if (mangled
== NULL
)
1439 string_append (decl
, ", ");
1442 string_append (decl
, "]");
1446 /* Extract the struct literal value for NAME from MANGLED and append it to DECL.
1447 Return the remaining string on success or NULL on failure. */
1449 dlang_parse_structlit (string
*decl
, const char *mangled
, const char *name
,
1450 struct dlang_info
*info
)
1454 mangled
= dlang_number (mangled
, &args
);
1455 if (mangled
== NULL
)
1459 string_append (decl
, name
);
1461 string_append (decl
, "(");
1464 mangled
= dlang_value (decl
, mangled
, NULL
, '\0', info
);
1465 if (mangled
== NULL
)
1469 string_append (decl
, ", ");
1472 string_append (decl
, ")");
1476 /* Extract the value from MANGLED and append it to DECL.
1477 Return the remaining string on success or NULL on failure. */
1479 dlang_value (string
*decl
, const char *mangled
, const char *name
, char type
,
1480 struct dlang_info
*info
)
1482 if (mangled
== NULL
|| *mangled
== '\0')
1490 string_append (decl
, "null");
1493 /* Integral values. */
1496 string_append (decl
, "-");
1497 mangled
= dlang_parse_integer (decl
, mangled
, type
);
1504 /* There really should always be an `i' before encoded numbers, but there
1505 wasn't in early versions of D2, so this case range must remain for
1506 backwards compatibility. */
1507 case '0': case '1': case '2': case '3': case '4':
1508 case '5': case '6': case '7': case '8': case '9':
1509 mangled
= dlang_parse_integer (decl
, mangled
, type
);
1515 mangled
= dlang_parse_real (decl
, mangled
);
1518 /* Complex value. */
1521 mangled
= dlang_parse_real (decl
, mangled
);
1522 string_append (decl
, "+");
1523 if (mangled
== NULL
|| *mangled
!= 'c')
1526 mangled
= dlang_parse_real (decl
, mangled
);
1527 string_append (decl
, "i");
1530 /* String values. */
1531 case 'a': /* UTF8 */
1532 case 'w': /* UTF16 */
1533 case 'd': /* UTF32 */
1534 mangled
= dlang_parse_string (decl
, mangled
);
1541 mangled
= dlang_parse_assocarray (decl
, mangled
, info
);
1543 mangled
= dlang_parse_arrayliteral (decl
, mangled
, info
);
1546 /* Struct values. */
1549 mangled
= dlang_parse_structlit (decl
, mangled
, name
, info
);
1552 /* Function literal symbol. */
1555 if (strncmp (mangled
, "_D", 2) != 0
1556 || !dlang_symbol_name_p (mangled
+ 2, info
))
1558 mangled
= dlang_parse_mangle (decl
, mangled
, info
);
1568 /* Extract and demangle the symbol in MANGLED and append it to DECL.
1569 Returns the remaining signature on success or NULL on failure. */
1571 dlang_parse_mangle (string
*decl
, const char *mangled
, struct dlang_info
*info
)
1573 /* A D mangled symbol is comprised of both scope and type information.
1576 _D QualifiedName Type
1579 The caller should have guaranteed that the start pointer is at the
1581 Note that type is never a function type, but only the return type of
1582 a function or the type of a variable.
1586 mangled
= dlang_parse_qualified (decl
, mangled
, info
, 1);
1588 if (mangled
!= NULL
)
1590 /* Artificial symbols end with 'Z' and have no type. */
1591 if (*mangled
== 'Z')
1595 /* Discard the declaration or return type. */
1598 string_init (&type
);
1599 mangled
= dlang_type (&type
, mangled
, info
);
1600 string_delete (&type
);
1607 /* Extract and demangle the qualified symbol in MANGLED and append it to DECL.
1608 SUFFIX_MODIFIERS is 1 if we are printing modifiers on this after the symbol.
1609 Returns the remaining signature on success or NULL on failure. */
1611 dlang_parse_qualified (string
*decl
, const char *mangled
,
1612 struct dlang_info
*info
, int suffix_modifiers
)
1614 /* Qualified names are identifiers separated by their encoded length.
1615 Nested functions also encode their argument types without specifying
1620 SymbolFunctionName QualifiedName
1625 SymbolName TypeFunctionNoReturn
1626 SymbolName M TypeFunctionNoReturn
1627 SymbolName M TypeModifiers TypeFunctionNoReturn
1629 The start pointer should be at the above location.
1635 string_append (decl
, ".");
1637 /* Skip over anonymous symbols. */
1638 while (*mangled
== '0')
1641 mangled
= dlang_identifier (decl
, mangled
, info
);
1643 /* Consume the encoded arguments. However if this is not followed by the
1644 next encoded length or mangle type, then this is not a continuation of
1645 a qualified name, in which case we backtrack and return the current
1646 unconsumed position of the mangled decl. */
1647 if (mangled
&& (*mangled
== 'M' || dlang_call_convention_p (mangled
)))
1650 const char *start
= mangled
;
1651 int saved
= string_length (decl
);
1653 /* Save the type modifiers for appending at the end if needed. */
1654 string_init (&mods
);
1656 /* Skip over 'this' parameter and type modifiers. */
1657 if (*mangled
== 'M')
1660 mangled
= dlang_type_modifiers (&mods
, mangled
);
1661 string_setlength (decl
, saved
);
1664 mangled
= dlang_function_type_noreturn (decl
, NULL
, NULL
,
1666 if (suffix_modifiers
)
1667 string_appendn (decl
, mods
.b
, string_length (&mods
));
1669 if (mangled
== NULL
|| *mangled
== '\0')
1671 /* Did not match the rule we were looking for. */
1673 string_setlength (decl
, saved
);
1676 string_delete (&mods
);
1679 while (mangled
&& dlang_symbol_name_p (mangled
, info
));
1684 /* Demangle the tuple from MANGLED and append it to DECL.
1685 Return the remaining string on success or NULL on failure. */
1687 dlang_parse_tuple (string
*decl
, const char *mangled
, struct dlang_info
*info
)
1689 unsigned long elements
;
1691 mangled
= dlang_number (mangled
, &elements
);
1692 if (mangled
== NULL
)
1695 string_append (decl
, "Tuple!(");
1699 mangled
= dlang_type (decl
, mangled
, info
);
1700 if (mangled
== NULL
)
1704 string_append (decl
, ", ");
1707 string_append (decl
, ")");
1711 /* Demangle the template symbol parameter from MANGLED and append it to DECL.
1712 Return the remaining string on success or NULL on failure. */
1714 dlang_template_symbol_param (string
*decl
, const char *mangled
,
1715 struct dlang_info
*info
)
1717 if (strncmp (mangled
, "_D", 2) == 0
1718 && dlang_symbol_name_p (mangled
+ 2, info
))
1719 return dlang_parse_mangle (decl
, mangled
, info
);
1721 if (*mangled
== 'Q')
1722 return dlang_parse_qualified (decl
, mangled
, info
, 0);
1725 const char *endptr
= dlang_number (mangled
, &len
);
1727 if (endptr
== NULL
|| len
== 0)
1730 /* In template parameter symbols generated by the frontend up to 2.076,
1731 the symbol length is encoded and the first character of the mangled
1732 name can be a digit. This causes ambiguity issues because the digits
1733 of the two numbers are adjacent. */
1736 int saved
= string_length (decl
);
1738 /* Work backwards until a match is found. */
1739 for (pend
= endptr
; endptr
!= NULL
; pend
--)
1743 /* Reached the beginning of the pointer to the name length,
1744 try parsing the entire symbol. */
1752 /* Check whether template parameter is a function with a valid
1753 return type or an untyped identifier. */
1754 if (dlang_symbol_name_p (mangled
, info
))
1755 mangled
= dlang_parse_qualified (decl
, mangled
, info
, 0);
1756 else if (strncmp (mangled
, "_D", 2) == 0
1757 && dlang_symbol_name_p (mangled
+ 2, info
))
1758 mangled
= dlang_parse_mangle (decl
, mangled
, info
);
1760 /* Check for name length mismatch. */
1761 if (mangled
&& (endptr
== NULL
|| (mangled
- pend
) == psize
))
1765 string_setlength (decl
, saved
);
1768 /* No match on any combinations. */
1772 /* Demangle the argument list from MANGLED and append it to DECL.
1773 Return the remaining string on success or NULL on failure. */
1775 dlang_template_args (string
*decl
, const char *mangled
, struct dlang_info
*info
)
1779 while (mangled
&& *mangled
!= '\0')
1783 case 'Z': /* End of parameter list. */
1789 string_append (decl
, ", ");
1791 /* Skip over specialised template prefix. */
1792 if (*mangled
== 'H')
1797 case 'S': /* Symbol parameter. */
1799 mangled
= dlang_template_symbol_param (decl
, mangled
, info
);
1801 case 'T': /* Type parameter. */
1803 mangled
= dlang_type (decl
, mangled
, info
);
1805 case 'V': /* Value parameter. */
1810 /* Peek at the type. */
1816 /* Value type is a back reference, peek at the real type. */
1817 const char *backref
;
1818 if (dlang_backref (mangled
, &backref
, info
) == NULL
)
1824 /* In the few instances where the type is actually desired in
1825 the output, it should precede the value from dlang_value. */
1826 string_init (&name
);
1827 mangled
= dlang_type (&name
, mangled
, info
);
1828 string_need (&name
, 1);
1831 mangled
= dlang_value (decl
, mangled
, name
.b
, type
, info
);
1832 string_delete (&name
);
1835 case 'X': /* Externally mangled parameter. */
1841 endptr
= dlang_number (mangled
, &len
);
1842 if (endptr
== NULL
|| strlen (endptr
) < len
)
1845 string_appendn (decl
, endptr
, len
);
1846 mangled
= endptr
+ len
;
1857 /* Extract and demangle the template symbol in MANGLED, expected to
1858 be made up of LEN characters (-1 if unknown), and append it to DECL.
1859 Returns the remaining signature on success or NULL on failure. */
1861 dlang_parse_template (string
*decl
, const char *mangled
,
1862 struct dlang_info
*info
, unsigned long len
)
1864 const char *start
= mangled
;
1867 /* Template instance names have the types and values of its parameters
1870 TemplateInstanceName:
1871 Number __T LName TemplateArgs Z
1872 Number __U LName TemplateArgs Z
1874 The start pointer should be at the above location, and LEN should be
1875 the value of the decoded number.
1878 /* Template symbol. */
1879 if (!dlang_symbol_name_p (mangled
+ 3, info
) || mangled
[3] == '0')
1884 /* Template identifier. */
1885 mangled
= dlang_identifier (decl
, mangled
, info
);
1887 /* Template arguments. */
1888 string_init (&args
);
1889 mangled
= dlang_template_args (&args
, mangled
, info
);
1891 string_append (decl
, "!(");
1892 string_appendn (decl
, args
.b
, string_length (&args
));
1893 string_append (decl
, ")");
1895 string_delete (&args
);
1897 /* Check for template name length mismatch. */
1898 if (len
!= TEMPLATE_LENGTH_UNKNOWN
1900 && (unsigned long) (mangled
- start
) != len
)
1906 /* Initialize the information structure we use to pass around information. */
1908 dlang_demangle_init_info (const char *mangled
, int last_backref
,
1909 struct dlang_info
*info
)
1912 info
->last_backref
= last_backref
;
1915 /* Extract and demangle the symbol in MANGLED. Returns the demangled
1916 signature on success or NULL on failure. */
1919 dlang_demangle (const char *mangled
, int option ATTRIBUTE_UNUSED
)
1922 char *demangled
= NULL
;
1924 if (mangled
== NULL
|| *mangled
== '\0')
1927 if (strncmp (mangled
, "_D", 2) != 0)
1930 string_init (&decl
);
1932 if (strcmp (mangled
, "_Dmain") == 0)
1934 string_append (&decl
, "D main");
1938 struct dlang_info info
;
1940 dlang_demangle_init_info (mangled
, strlen (mangled
), &info
);
1941 mangled
= dlang_parse_mangle (&decl
, mangled
, &info
);
1943 /* Check that the entire symbol was successfully demangled. */
1944 if (mangled
== NULL
|| *mangled
!= '\0')
1945 string_delete (&decl
);
1948 if (string_length (&decl
) > 0)
1950 string_need (&decl
, 1);