]>
Commit | Line | Data |
---|---|---|
b4c522fa IB |
1 | |
2 | /* Compiler implementation of the D programming language | |
3 | * Copyright (C) 1999-2018 by The D Language Foundation, All Rights Reserved | |
4 | * written by Walter Bright | |
5 | * http://www.digitalmars.com | |
6 | * Distributed under the Boost Software License, Version 1.0. | |
7 | * http://www.boost.org/LICENSE_1_0.txt | |
8 | * https://github.com/D-Programming-Language/dmd/blob/master/src/idgen.c | |
9 | */ | |
10 | ||
11 | // Program to generate string files in d data structures. | |
12 | // Saves much tedious typing, and eliminates typo problems. | |
13 | // Generates: | |
14 | // id.h | |
15 | // id.c | |
16 | ||
f9ab59ff | 17 | #include "root/dsystem.h" |
b4c522fa IB |
18 | |
19 | struct Msgtable | |
20 | { | |
21 | const char* ident; // name to use in DMD source | |
22 | const char* name; // name in D executable | |
23 | }; | |
24 | ||
25 | Msgtable msgtable[] = | |
26 | { | |
27 | { "IUnknown", NULL }, | |
28 | { "Object", NULL }, | |
29 | { "object", NULL }, | |
30 | { "string", NULL }, | |
31 | { "wstring", NULL }, | |
32 | { "dstring", NULL }, | |
33 | { "max", NULL }, | |
34 | { "min", NULL }, | |
35 | { "This", "this" }, | |
36 | { "_super", "super" }, | |
37 | { "ctor", "__ctor" }, | |
38 | { "dtor", "__dtor" }, | |
39 | { "__xdtor", "__xdtor" }, | |
40 | { "__fieldDtor", "__fieldDtor" }, | |
41 | { "__aggrDtor", "__aggrDtor" }, | |
42 | { "postblit", "__postblit" }, | |
43 | { "__xpostblit", "__xpostblit" }, | |
44 | { "__fieldPostblit", "__fieldPostblit" }, | |
45 | { "__aggrPostblit", "__aggrPostblit" }, | |
46 | { "classInvariant", "__invariant" }, | |
47 | { "unitTest", "__unitTest" }, | |
48 | { "require", "__require" }, | |
49 | { "ensure", "__ensure" }, | |
50 | { "_init", "init" }, | |
51 | { "__sizeof", "sizeof" }, | |
52 | { "__xalignof", "alignof" }, | |
53 | { "_mangleof", "mangleof" }, | |
54 | { "stringof", NULL }, | |
55 | { "_tupleof", "tupleof" }, | |
56 | { "length", NULL }, | |
57 | { "remove", NULL }, | |
58 | { "ptr", NULL }, | |
59 | { "array", NULL }, | |
60 | { "funcptr", NULL }, | |
61 | { "dollar", "__dollar" }, | |
62 | { "ctfe", "__ctfe" }, | |
63 | { "offset", NULL }, | |
64 | { "offsetof", NULL }, | |
65 | { "ModuleInfo", NULL }, | |
66 | { "ClassInfo", NULL }, | |
67 | { "classinfo", NULL }, | |
68 | { "typeinfo", NULL }, | |
69 | { "outer", NULL }, | |
70 | { "Exception", NULL }, | |
71 | { "RTInfo", NULL }, | |
72 | { "Throwable", NULL }, | |
73 | { "Error", NULL }, | |
74 | { "withSym", "__withSym" }, | |
75 | { "result", "__result" }, | |
76 | { "returnLabel", "__returnLabel" }, | |
77 | { "line", NULL }, | |
78 | { "empty", "" }, | |
79 | { "p", NULL }, | |
80 | { "q", NULL }, | |
81 | { "__vptr", NULL }, | |
82 | { "__monitor", NULL }, | |
83 | { "gate", "__gate" }, | |
84 | { "__c_long", NULL }, | |
85 | { "__c_ulong", NULL }, | |
86 | { "__c_longlong", NULL }, | |
87 | { "__c_ulonglong", NULL }, | |
88 | { "__c_long_double", NULL }, | |
89 | { "cpp_type_info_ptr", "__cpp_type_info_ptr" }, | |
90 | { "_assert", "assert" }, | |
91 | { "_unittest", "unittest" }, | |
92 | { "_body", "body" }, | |
93 | ||
94 | { "TypeInfo", NULL }, | |
95 | { "TypeInfo_Class", NULL }, | |
96 | { "TypeInfo_Interface", NULL }, | |
97 | { "TypeInfo_Struct", NULL }, | |
98 | { "TypeInfo_Enum", NULL }, | |
99 | { "TypeInfo_Pointer", NULL }, | |
100 | { "TypeInfo_Vector", NULL }, | |
101 | { "TypeInfo_Array", NULL }, | |
102 | { "TypeInfo_StaticArray", NULL }, | |
103 | { "TypeInfo_AssociativeArray", NULL }, | |
104 | { "TypeInfo_Function", NULL }, | |
105 | { "TypeInfo_Delegate", NULL }, | |
106 | { "TypeInfo_Tuple", NULL }, | |
107 | { "TypeInfo_Const", NULL }, | |
108 | { "TypeInfo_Invariant", NULL }, | |
109 | { "TypeInfo_Shared", NULL }, | |
110 | { "TypeInfo_Wild", "TypeInfo_Inout" }, | |
111 | { "elements", NULL }, | |
112 | { "_arguments_typeinfo", NULL }, | |
113 | { "_arguments", NULL }, | |
114 | { "_argptr", NULL }, | |
115 | { "destroy", NULL }, | |
116 | { "xopEquals", "__xopEquals" }, | |
117 | { "xopCmp", "__xopCmp" }, | |
118 | { "xtoHash", "__xtoHash" }, | |
119 | ||
120 | { "LINE", "__LINE__" }, | |
121 | { "FILE", "__FILE__" }, | |
122 | { "MODULE", "__MODULE__" }, | |
123 | { "FUNCTION", "__FUNCTION__" }, | |
124 | { "PRETTY_FUNCTION", "__PRETTY_FUNCTION__" }, | |
125 | { "DATE", "__DATE__" }, | |
126 | { "TIME", "__TIME__" }, | |
127 | { "TIMESTAMP", "__TIMESTAMP__" }, | |
128 | { "VENDOR", "__VENDOR__" }, | |
129 | { "VERSIONX", "__VERSION__" }, | |
130 | { "EOFX", "__EOF__" }, | |
131 | ||
132 | { "nan", NULL }, | |
133 | { "infinity", NULL }, | |
134 | { "dig", NULL }, | |
135 | { "epsilon", NULL }, | |
136 | { "mant_dig", NULL }, | |
137 | { "max_10_exp", NULL }, | |
138 | { "max_exp", NULL }, | |
139 | { "min_10_exp", NULL }, | |
140 | { "min_exp", NULL }, | |
141 | { "min_normal", NULL }, | |
142 | { "re", NULL }, | |
143 | { "im", NULL }, | |
144 | ||
145 | { "C", NULL }, | |
146 | { "D", NULL }, | |
147 | { "Windows", NULL }, | |
148 | { "Pascal", NULL }, | |
149 | { "System", NULL }, | |
150 | { "Objective", NULL }, | |
151 | ||
152 | { "exit", NULL }, | |
153 | { "success", NULL }, | |
154 | { "failure", NULL }, | |
155 | ||
156 | { "keys", NULL }, | |
157 | { "values", NULL }, | |
158 | { "rehash", NULL }, | |
159 | ||
160 | { "future", "__future" }, | |
161 | { "property", NULL }, | |
162 | { "nogc", NULL }, | |
163 | { "safe", NULL }, | |
164 | { "trusted", NULL }, | |
165 | { "system", NULL }, | |
166 | { "disable", NULL }, | |
167 | ||
168 | // For inline assembler | |
169 | { "___out", "out" }, | |
170 | { "___in", "in" }, | |
171 | { "__int", "int" }, | |
172 | { "_dollar", "$" }, | |
173 | { "__LOCAL_SIZE", NULL }, | |
174 | ||
175 | // For operator overloads | |
176 | { "uadd", "opPos" }, | |
177 | { "neg", "opNeg" }, | |
178 | { "com", "opCom" }, | |
179 | { "add", "opAdd" }, | |
180 | { "add_r", "opAdd_r" }, | |
181 | { "sub", "opSub" }, | |
182 | { "sub_r", "opSub_r" }, | |
183 | { "mul", "opMul" }, | |
184 | { "mul_r", "opMul_r" }, | |
185 | { "div", "opDiv" }, | |
186 | { "div_r", "opDiv_r" }, | |
187 | { "mod", "opMod" }, | |
188 | { "mod_r", "opMod_r" }, | |
189 | { "eq", "opEquals" }, | |
190 | { "cmp", "opCmp" }, | |
191 | { "iand", "opAnd" }, | |
192 | { "iand_r", "opAnd_r" }, | |
193 | { "ior", "opOr" }, | |
194 | { "ior_r", "opOr_r" }, | |
195 | { "ixor", "opXor" }, | |
196 | { "ixor_r", "opXor_r" }, | |
197 | { "shl", "opShl" }, | |
198 | { "shl_r", "opShl_r" }, | |
199 | { "shr", "opShr" }, | |
200 | { "shr_r", "opShr_r" }, | |
201 | { "ushr", "opUShr" }, | |
202 | { "ushr_r", "opUShr_r" }, | |
203 | { "cat", "opCat" }, | |
204 | { "cat_r", "opCat_r" }, | |
205 | { "assign", "opAssign" }, | |
206 | { "addass", "opAddAssign" }, | |
207 | { "subass", "opSubAssign" }, | |
208 | { "mulass", "opMulAssign" }, | |
209 | { "divass", "opDivAssign" }, | |
210 | { "modass", "opModAssign" }, | |
211 | { "andass", "opAndAssign" }, | |
212 | { "orass", "opOrAssign" }, | |
213 | { "xorass", "opXorAssign" }, | |
214 | { "shlass", "opShlAssign" }, | |
215 | { "shrass", "opShrAssign" }, | |
216 | { "ushrass", "opUShrAssign" }, | |
217 | { "catass", "opCatAssign" }, | |
218 | { "postinc", "opPostInc" }, | |
219 | { "postdec", "opPostDec" }, | |
220 | { "index", "opIndex" }, | |
221 | { "indexass", "opIndexAssign" }, | |
222 | { "slice", "opSlice" }, | |
223 | { "sliceass", "opSliceAssign" }, | |
224 | { "call", "opCall" }, | |
225 | { "_cast", "opCast" }, | |
226 | { "opIn", NULL }, | |
227 | { "opIn_r", NULL }, | |
228 | { "opStar", NULL }, | |
229 | { "opDot", NULL }, | |
230 | { "opDispatch", NULL }, | |
231 | { "opDollar", NULL }, | |
232 | { "opUnary", NULL }, | |
233 | { "opIndexUnary", NULL }, | |
234 | { "opSliceUnary", NULL }, | |
235 | { "opBinary", NULL }, | |
236 | { "opBinaryRight", NULL }, | |
237 | { "opOpAssign", NULL }, | |
238 | { "opIndexOpAssign", NULL }, | |
239 | { "opSliceOpAssign", NULL }, | |
240 | { "pow", "opPow" }, | |
241 | { "pow_r", "opPow_r" }, | |
242 | { "powass", "opPowAssign" }, | |
243 | ||
244 | { "classNew", "new" }, | |
245 | { "classDelete", "delete" }, | |
246 | ||
247 | // For foreach | |
248 | { "apply", "opApply" }, | |
249 | { "applyReverse", "opApplyReverse" }, | |
250 | ||
251 | // Ranges | |
252 | { "Fempty", "empty" }, | |
253 | { "Ffront", "front" }, | |
254 | { "Fback", "back" }, | |
255 | { "FpopFront", "popFront" }, | |
256 | { "FpopBack", "popBack" }, | |
257 | ||
258 | // For internal functions | |
259 | { "aaLen", "_aaLen" }, | |
260 | { "aaKeys", "_aaKeys" }, | |
261 | { "aaValues", "_aaValues" }, | |
262 | { "aaRehash", "_aaRehash" }, | |
263 | { "monitorenter", "_d_monitorenter" }, | |
264 | { "monitorexit", "_d_monitorexit" }, | |
265 | { "criticalenter", "_d_criticalenter" }, | |
266 | { "criticalexit", "_d_criticalexit" }, | |
267 | { "_ArrayEq", NULL }, | |
268 | { "_ArrayPostblit", NULL }, | |
269 | { "_ArrayDtor", NULL }, | |
270 | ||
271 | // For pragma's | |
272 | { "Pinline", "inline" }, | |
273 | { "lib", NULL }, | |
274 | { "mangle", NULL }, | |
275 | { "msg", NULL }, | |
276 | { "startaddress", NULL }, | |
277 | ||
278 | // For special functions | |
279 | { "tohash", "toHash" }, | |
280 | { "tostring", "toString" }, | |
281 | { "getmembers", "getMembers" }, | |
282 | ||
283 | // Special functions | |
284 | { "__alloca", "alloca" }, | |
285 | { "main", NULL }, | |
286 | { "WinMain", NULL }, | |
287 | { "DllMain", NULL }, | |
288 | { "tls_get_addr", "___tls_get_addr" }, | |
289 | { "entrypoint", "__entrypoint" }, | |
290 | ||
291 | // varargs implementation | |
292 | { "va_start", NULL }, | |
293 | ||
294 | // Builtin functions | |
295 | { "std", NULL }, | |
296 | { "core", NULL }, | |
297 | { "attribute", NULL }, | |
298 | { "math", NULL }, | |
299 | { "sin", NULL }, | |
300 | { "cos", NULL }, | |
301 | { "tan", NULL }, | |
302 | { "_sqrt", "sqrt" }, | |
303 | { "_pow", "pow" }, | |
304 | { "atan2", NULL }, | |
305 | { "rndtol", NULL }, | |
306 | { "expm1", NULL }, | |
307 | { "exp2", NULL }, | |
308 | { "yl2x", NULL }, | |
309 | { "yl2xp1", NULL }, | |
310 | { "fabs", NULL }, | |
311 | { "bitop", NULL }, | |
312 | { "bsf", NULL }, | |
313 | { "bsr", NULL }, | |
314 | { "bswap", NULL }, | |
315 | ||
316 | // Traits | |
317 | { "isAbstractClass", NULL }, | |
318 | { "isArithmetic", NULL }, | |
319 | { "isAssociativeArray", NULL }, | |
320 | { "isFinalClass", NULL }, | |
321 | { "isTemplate", NULL }, | |
322 | { "isPOD", NULL }, | |
323 | { "isNested", NULL }, | |
324 | { "isFloating", NULL }, | |
325 | { "isIntegral", NULL }, | |
326 | { "isScalar", NULL }, | |
327 | { "isStaticArray", NULL }, | |
328 | { "isUnsigned", NULL }, | |
329 | { "isVirtualFunction", NULL }, | |
330 | { "isVirtualMethod", NULL }, | |
331 | { "isAbstractFunction", NULL }, | |
332 | { "isFinalFunction", NULL }, | |
333 | { "isOverrideFunction", NULL }, | |
334 | { "isStaticFunction", NULL }, | |
335 | { "isRef", NULL }, | |
336 | { "isOut", NULL }, | |
337 | { "isLazy", NULL }, | |
338 | { "hasMember", NULL }, | |
339 | { "identifier", NULL }, | |
340 | { "getProtection", NULL }, | |
341 | { "parent", NULL }, | |
342 | { "getMember", NULL }, | |
343 | { "getOverloads", NULL }, | |
344 | { "getVirtualFunctions", NULL }, | |
345 | { "getVirtualMethods", NULL }, | |
346 | { "classInstanceSize", NULL }, | |
347 | { "allMembers", NULL }, | |
348 | { "derivedMembers", NULL }, | |
349 | { "isSame", NULL }, | |
350 | { "compiles", NULL }, | |
351 | { "parameters", NULL }, | |
352 | { "getAliasThis", NULL }, | |
353 | { "getAttributes", NULL }, | |
354 | { "getFunctionAttributes", NULL }, | |
355 | { "getFunctionVariadicStyle", NULL }, | |
356 | { "getParameterStorageClasses", NULL }, | |
357 | { "getLinkage", NULL }, | |
358 | { "getUnitTests", NULL }, | |
359 | { "getVirtualIndex", NULL }, | |
360 | { "getPointerBitmap", NULL }, | |
361 | ||
362 | // For C++ mangling | |
363 | { "allocator", NULL }, | |
364 | { "basic_string", NULL }, | |
365 | { "basic_istream", NULL }, | |
366 | { "basic_ostream", NULL }, | |
367 | { "basic_iostream", NULL }, | |
368 | { "char_traits", NULL }, | |
369 | ||
370 | // Compiler recognized UDA's | |
371 | { "udaSelector", "selector" }, | |
372 | ||
373 | // C names, for undefined identifier error messages | |
374 | { "_NULL", "NULL" }, | |
375 | { "_TRUE", "TRUE" }, | |
376 | { "_FALSE", "FALSE" }, | |
377 | { "_unsigned", "unsigned" }, | |
378 | }; | |
379 | ||
380 | ||
381 | int main() | |
382 | { | |
383 | { | |
384 | FILE *fp = fopen("id.h","wb"); | |
385 | if (!fp) | |
386 | { | |
387 | printf("can't open id.h\n"); | |
388 | exit(EXIT_FAILURE); | |
389 | } | |
390 | ||
391 | fprintf(fp, "// File generated by idgen.c\n"); | |
392 | fprintf(fp, "#ifndef DMD_ID_H\n"); | |
393 | fprintf(fp, "#define DMD_ID_H 1\n"); | |
394 | fprintf(fp, "class Identifier;\n"); | |
395 | fprintf(fp, "struct Id\n"); | |
396 | fprintf(fp, "{\n"); | |
397 | ||
398 | for (unsigned i = 0; i < sizeof(msgtable) / sizeof(msgtable[0]); i++) | |
399 | { | |
400 | const char *id = msgtable[i].ident; | |
401 | fprintf(fp," static Identifier *%s;\n", id); | |
402 | } | |
403 | ||
404 | fprintf(fp, " static void initialize();\n"); | |
405 | fprintf(fp, "};\n"); | |
406 | fprintf(fp, "#endif\n"); | |
407 | ||
408 | fclose(fp); | |
409 | } | |
410 | ||
411 | { | |
412 | FILE *fp = fopen("id.c","wb"); | |
413 | if (!fp) | |
414 | { | |
415 | printf("can't open id.c\n"); | |
416 | exit(EXIT_FAILURE); | |
417 | } | |
418 | ||
419 | fprintf(fp, "// File generated by idgen.c\n"); | |
420 | fprintf(fp, "#include \"identifier.h\"\n"); | |
421 | fprintf(fp, "#include \"id.h\"\n"); | |
422 | fprintf(fp, "#include \"mars.h\"\n"); | |
423 | ||
424 | for (unsigned i = 0; i < sizeof(msgtable) / sizeof(msgtable[0]); i++) | |
425 | { | |
426 | const char *id = msgtable[i].ident; | |
427 | const char *p = msgtable[i].name; | |
428 | ||
429 | if (!p) | |
430 | p = id; | |
431 | fprintf(fp,"Identifier *Id::%s;\n", id); | |
432 | } | |
433 | ||
434 | fprintf(fp, "void Id::initialize()\n"); | |
435 | fprintf(fp, "{\n"); | |
436 | ||
437 | for (unsigned i = 0; i < sizeof(msgtable) / sizeof(msgtable[0]); i++) | |
438 | { | |
439 | const char *id = msgtable[i].ident; | |
440 | const char *p = msgtable[i].name; | |
441 | ||
442 | if (!p) | |
443 | p = id; | |
444 | fprintf(fp," %s = Identifier::idPool(\"%s\");\n", id, p); | |
445 | } | |
446 | ||
447 | fprintf(fp, "}\n"); | |
448 | ||
449 | fclose(fp); | |
450 | } | |
451 | ||
452 | { | |
453 | FILE *fp = fopen("id.d","wb"); | |
454 | if (!fp) | |
455 | { | |
456 | printf("can't open id.d\n"); | |
457 | exit(EXIT_FAILURE); | |
458 | } | |
459 | ||
460 | fprintf(fp, "// File generated by idgen.c\n"); | |
461 | fprintf(fp, "\n"); | |
462 | fprintf(fp, "module ddmd.id;\n"); | |
463 | fprintf(fp, "\n"); | |
464 | fprintf(fp, "import ddmd.identifier, ddmd.tokens;\n"); | |
465 | fprintf(fp, "\n"); | |
466 | fprintf(fp, "struct Id\n"); | |
467 | fprintf(fp, "{\n"); | |
468 | ||
469 | for (unsigned i = 0; i < sizeof(msgtable) / sizeof(msgtable[0]); i++) | |
470 | { | |
471 | const char *id = msgtable[i].ident; | |
472 | const char *p = msgtable[i].name; | |
473 | ||
474 | if (!p) | |
475 | p = id; | |
476 | fprintf(fp, " extern (C++) static __gshared Identifier %s;\n", id); | |
477 | } | |
478 | ||
479 | fprintf(fp, "\n"); | |
480 | fprintf(fp, " extern (C++) static void initialize()\n"); | |
481 | fprintf(fp, " {\n"); | |
482 | ||
483 | for (unsigned i = 0; i < sizeof(msgtable) / sizeof(msgtable[0]); i++) | |
484 | { | |
485 | const char *id = msgtable[i].ident; | |
486 | const char *p = msgtable[i].name; | |
487 | ||
488 | if (!p) | |
489 | p = id; | |
490 | fprintf(fp," %s = Identifier.idPool(\"%s\");\n", id, p); | |
491 | } | |
492 | ||
493 | fprintf(fp, " }\n"); | |
494 | fprintf(fp, "}\n"); | |
495 | ||
496 | fclose(fp); | |
497 | } | |
498 | ||
499 | return EXIT_SUCCESS; | |
500 | } |