-
Notifications
You must be signed in to change notification settings - Fork 128
/
mangle.d
324 lines (266 loc) · 7.55 KB
/
mangle.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/// a D name mangler. You probably do not need this, use the language's built in `.mangleof` property instead. I don't even remember why I wrote it.
module arsd.mangle;
import std.conv;
static immutable string[23] primitives = [
"char", // a
"bool", // b
"creal", // c
"double", // d
"real", // e
"float", // f
"byte", // g
"ubyte", // h
"int", // i
"ireal", // j
"uint", // k
"long", // l
"ulong", // m
null, // n
"ifloat", // o
"idouble", // p
"cfloat", // q
"cdouble", // r
"short", // s
"ushort", // t
"wchar", // u
"void", // v
"dchar", // w
];
// FIXME: using this will allocate at *runtime*! Unbelievable.
// it does that even if everything is enum
auto dTokensPain() {
immutable p = cast(immutable(string[])) primitives[];
string[] ret;
foreach(i; (sort!"a.length > b.length"(
p~
[
"(",
")",
".",
",",
"!",
"[",
"]",
"*",
"const",
"immutable",
"shared",
"extern",
]))) { ret ~= i; }
return ret;
}
static immutable string[] dTokens = dTokensPain();
char manglePrimitive(in char[] t) {
foreach(i, p; primitives)
if(p == t)
return cast(char) ('a' + i);
return 0;
}
import std.algorithm;
import std.array;
bool isIdentifierChar(char c) {
// FIXME: match the D spec
return c == '_' || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9');
}
struct StackArray(Type, size_t capacity) {
Type[capacity] buffer;
size_t length;
Type[] slice() { return buffer[0 .. length]; }
void opOpAssign(string op : "~")(Type rhs, string file = __FILE__, size_t line = __LINE__) {
if(length >= capacity) {
throw new Error("no more room", file, line);
}
buffer[length] = rhs;
length++;
}
}
char[] mangle(const(char)[] decl, char[] buffer) {
StackArray!(const(char)[], 128) tokensBuffer;
main: while(decl.length) {
if(decl[0] == ' ' || decl[0] == '\t' || decl[0] == '\n') {
decl = decl[1 .. $];
continue;
}
foreach(token; dTokens) {
if(token is null) continue;
if(decl.length >= token.length && decl[0 .. token.length] == token) {
// make sure this isn't an identifier that coincidentally starts with a keyword
if(decl.length == token.length || !token[$ - 1].isIdentifierChar() || !decl[token.length].isIdentifierChar()) {
tokensBuffer ~= token;
decl = decl[token.length .. $];
continue main;
}
}
}
// could be an identifier or literal
int pos = 0;
while(pos < decl.length && decl[pos].isIdentifierChar)
pos++;
tokensBuffer ~= decl[0 .. pos];
decl = decl[pos .. $];
continue main;
// FIXME: literals should be handled too
}
assert(decl.length == 0); // we should have consumed all the input into tokens
auto tokens = tokensBuffer.slice();
char[64] returnTypeBuffer;
auto returnType = parseAndMangleType(tokens, returnTypeBuffer);
char[256] nameBuffer;
auto name = parseName(tokens, nameBuffer[]);
StackArray!(const(char)[], 64) arguments;
// FIXME: templates and other types of thing should be handled
assert(tokens[0] == "(", "other stuff not implemented " ~ tokens[0]);
tokens = tokens[1 .. $];
char[64][32] argTypeBuffers;
int i = 0;
while(tokens[0] != ")") {
arguments ~= parseAndMangleType(tokens, argTypeBuffers[i]);
i++;
if(tokens[0] == ",")
tokens = tokens[1 .. $];
}
assert(tokens[0] == ")", "other stuff not implemented");
return mangleFunction(name, returnType, arguments.slice(), buffer);
}
char[] parseName(ref const(char)[][] tokens, char[] nameBuffer) {
size_t where = 0;
more:
nameBuffer[where .. where + tokens[0].length] = tokens[0][];
where += tokens[0].length;
tokens = tokens[1 .. $];
if(tokens.length && tokens[0] == ".") {
tokens = tokens[1 .. $];
nameBuffer[where++] = '.';
goto more;
}
return nameBuffer[0 .. where];
}
char[] intToString(int i, char[] buffer) {
int pos = cast(int) buffer.length - 1;
if(i == 0) {
buffer[pos] = '0';
pos--;
}
while(pos > 0 && i) {
buffer[pos] = (i % 10) + '0';
pos--;
i /= 10;
}
return buffer[pos + 1 .. $];
}
char[] mangleName(in char[] name, char[] buffer) {
import std.algorithm;
import std.conv;
auto parts = name.splitter(".");
int bufferPos = 0;
foreach(part; parts) {
char[16] numberBuffer;
auto number = intToString(cast(int) part.length, numberBuffer);
buffer[bufferPos .. bufferPos + number.length] = number[];
bufferPos += number.length;
buffer[bufferPos .. bufferPos + part.length] = part[];
bufferPos += part.length;
}
return buffer[0 .. bufferPos];
}
char[] mangleFunction(in char[] name, in char[] returnTypeMangled, in char[][] argumentsMangle, char[] buffer) {
int bufferPos = 0;
buffer[bufferPos++] = '_';
buffer[bufferPos++] = 'D';
char[256] nameBuffer;
auto mn = mangleName(name, nameBuffer);
buffer[bufferPos .. bufferPos + mn.length] = mn[];
bufferPos += mn.length;
buffer[bufferPos++] = 'F';
foreach(arg; argumentsMangle) {
buffer[bufferPos .. bufferPos + arg.length] = arg[];
bufferPos += arg.length;
}
buffer[bufferPos++] = 'Z';
buffer[bufferPos .. bufferPos + returnTypeMangled.length] = returnTypeMangled[];
bufferPos += returnTypeMangled.length;
return buffer[0 .. bufferPos];
}
char[] parseAndMangleType(ref const(char)[][] tokens, char[] buffer) {
assert(tokens.length);
int bufferPos = 0;
void prepend(char p) {
for(int i = bufferPos; i > 0; i--) {
buffer[i] = buffer[i - 1];
}
buffer[0] = p;
bufferPos++;
}
// FIXME: handle all the random D type constructors
if(tokens[0] == "const" || tokens[0] == "immutable") {
if(tokens[0] == "const")
buffer[bufferPos++] = 'x';
else if(tokens[0] == "immutable")
buffer[bufferPos++] = 'y';
tokens = tokens[1 .. $];
assert(tokens[0] == "(");
tokens = tokens[1 .. $];
auto next = parseAndMangleType(tokens, buffer[bufferPos .. $]);
bufferPos += next.length;
assert(tokens[0] == ")");
tokens = tokens[1 .. $];
} else {
char primitive = manglePrimitive(tokens[0]);
if(primitive) {
buffer[bufferPos++] = primitive;
tokens = tokens[1 .. $];
} else {
// probably a struct or something, parse it as an identifier
// FIXME
char[256] nameBuffer;
auto name = parseName(tokens, nameBuffer[]);
char[256] mangledNameBuffer;
auto mn = mangleName(name, mangledNameBuffer);
buffer[bufferPos++] = 'S';
buffer[bufferPos .. bufferPos + mn.length] = mn[];
bufferPos += mn.length;
}
}
while(tokens.length) {
if(tokens[0] == "[") {
tokens = tokens[1 .. $];
prepend('A');
assert(tokens[0] == "]", "other array not implemented");
tokens = tokens[1 .. $];
} else if(tokens[0] == "*") {
prepend('P');
tokens = tokens[1 .. $];
} else break;
}
return buffer[0 .. bufferPos];
}
version(unittest) {
int foo(int, string, int);
string foo2(long, char[], int);
struct S { int a; string b; }
S foo3(S, S, string, long, int, S, int[], char[][]);
long testcomplex(int, const(const(char)[]*)[], long);
}
unittest {
import core.demangle;
char[512] buffer;
import std.stdio;
assert(mangle(demangle(foo.mangleof), buffer) == foo.mangleof);
assert(mangle(demangle(foo2.mangleof), buffer) == foo2.mangleof);
assert(mangle(demangle(foo3.mangleof), buffer) == foo3.mangleof);
assert(mangle(demangle(testcomplex.mangleof), buffer) == testcomplex.mangleof);
// FIXME: these all fail if the functions are defined inside the unittest{} block
// so still something wrong parsing those complex names or something
}
// _D6test303fooFiAyaZi
// _D6test303fooFiAyaZi
version(unittest)
void main(string[] args) {
char[512] buffer;
import std.stdio;
if(args.length > 1)
writeln(mangle(args[1], buffer));
else
writeln(mangle("int test30.foo(int, immutable(char)[])", buffer));
//mangle("int test30.foo(int, immutable(char)[])", buffer);
}