1 
2 /* Compiler implementation of the D programming language
3  * Copyright (C) 1999-2019 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 
17 #include "root/dsystem.h"
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     { "dup", NULL },
271     { "_aaApply", NULL },
272     { "_aaApply2", NULL },
273 
274     // For pragma's
275     { "Pinline", "inline" },
276     { "lib", NULL },
277     { "mangle", NULL },
278     { "msg", NULL },
279     { "startaddress", NULL },
280 
281     // For special functions
282     { "tohash", "toHash" },
283     { "tostring", "toString" },
284     { "getmembers", "getMembers" },
285 
286     // Special functions
287     { "__alloca", "alloca" },
288     { "main", NULL },
289     { "WinMain", NULL },
290     { "DllMain", NULL },
291     { "tls_get_addr", "___tls_get_addr" },
292     { "entrypoint", "__entrypoint" },
293 
294     // varargs implementation
295     { "va_start", NULL },
296 
297     // Builtin functions
298     { "std", NULL },
299     { "core", NULL },
300     { "attribute", NULL },
301     { "math", NULL },
302     { "sin", NULL },
303     { "cos", NULL },
304     { "tan", NULL },
305     { "_sqrt", "sqrt" },
306     { "_pow", "pow" },
307     { "atan2", NULL },
308     { "rndtol", NULL },
309     { "expm1", NULL },
310     { "exp2", NULL },
311     { "yl2x", NULL },
312     { "yl2xp1", NULL },
313     { "fabs", NULL },
314     { "bitop", NULL },
315     { "bsf", NULL },
316     { "bsr", NULL },
317     { "bswap", NULL },
318 
319     // Traits
320     { "isAbstractClass", NULL },
321     { "isArithmetic", NULL },
322     { "isAssociativeArray", NULL },
323     { "isFinalClass", NULL },
324     { "isTemplate", NULL },
325     { "isPOD", NULL },
326     { "isNested", NULL },
327     { "isFloating", NULL },
328     { "isIntegral", NULL },
329     { "isScalar", NULL },
330     { "isStaticArray", NULL },
331     { "isUnsigned", NULL },
332     { "isVirtualFunction", NULL },
333     { "isVirtualMethod", NULL },
334     { "isAbstractFunction", NULL },
335     { "isFinalFunction", NULL },
336     { "isOverrideFunction", NULL },
337     { "isStaticFunction", NULL },
338     { "isRef", NULL },
339     { "isOut", NULL },
340     { "isLazy", NULL },
341     { "hasMember", NULL },
342     { "identifier", NULL },
343     { "getProtection", NULL },
344     { "parent", NULL },
345     { "getMember", NULL },
346     { "getOverloads", NULL },
347     { "getVirtualFunctions", NULL },
348     { "getVirtualMethods", NULL },
349     { "classInstanceSize", NULL },
350     { "allMembers", NULL },
351     { "derivedMembers", NULL },
352     { "isSame", NULL },
353     { "compiles", NULL },
354     { "parameters", NULL },
355     { "getAliasThis", NULL },
356     { "getAttributes", NULL },
357     { "getFunctionAttributes", NULL },
358     { "getFunctionVariadicStyle", NULL },
359     { "getParameterStorageClasses", NULL },
360     { "getLinkage", NULL },
361     { "getUnitTests", NULL },
362     { "getVirtualIndex", NULL },
363     { "getPointerBitmap", NULL },
364 
365     // For C++ mangling
366     { "allocator", NULL },
367     { "basic_string", NULL },
368     { "basic_istream", NULL },
369     { "basic_ostream", NULL },
370     { "basic_iostream", NULL },
371     { "char_traits", NULL },
372 
373     // Compiler recognized UDA's
374     { "udaSelector", "selector" },
375 
376     // C names, for undefined identifier error messages
377     { "C_NULL", "NULL" },
378     { "C_TRUE", "TRUE" },
379     { "C_FALSE", "FALSE" },
380     { "C_unsigned", "unsigned" },
381     { "C_wchar_t", "wchar_t" },
382 };
383 
384 
main()385 int main()
386 {
387     {
388         FILE *fp = fopen("id.h","wb");
389         if (!fp)
390         {
391             printf("can't open id.h\n");
392             exit(EXIT_FAILURE);
393         }
394 
395         fprintf(fp, "// File generated by idgen.c\n");
396         fprintf(fp, "#ifndef DMD_ID_H\n");
397         fprintf(fp, "#define DMD_ID_H 1\n");
398         fprintf(fp, "class Identifier;\n");
399         fprintf(fp, "struct Id\n");
400         fprintf(fp, "{\n");
401 
402         for (unsigned i = 0; i < sizeof(msgtable) / sizeof(msgtable[0]); i++)
403         {
404             const char *id = msgtable[i].ident;
405             fprintf(fp,"    static Identifier *%s;\n", id);
406         }
407 
408         fprintf(fp, "    static void initialize();\n");
409         fprintf(fp, "};\n");
410         fprintf(fp, "#endif\n");
411 
412         fclose(fp);
413     }
414 
415     {
416         FILE *fp = fopen("id.c","wb");
417         if (!fp)
418         {
419             printf("can't open id.c\n");
420             exit(EXIT_FAILURE);
421         }
422 
423         fprintf(fp, "// File generated by idgen.c\n");
424         fprintf(fp, "#include \"identifier.h\"\n");
425         fprintf(fp, "#include \"id.h\"\n");
426         fprintf(fp, "#include \"mars.h\"\n");
427 
428         for (unsigned i = 0; i < sizeof(msgtable) / sizeof(msgtable[0]); i++)
429         {
430             const char *id = msgtable[i].ident;
431             const char *p = msgtable[i].name;
432 
433             if (!p)
434                 p = id;
435             fprintf(fp,"Identifier *Id::%s;\n", id);
436         }
437 
438         fprintf(fp, "void Id::initialize()\n");
439         fprintf(fp, "{\n");
440 
441         for (unsigned i = 0; i < sizeof(msgtable) / sizeof(msgtable[0]); i++)
442         {
443             const char *id = msgtable[i].ident;
444             const char *p = msgtable[i].name;
445 
446             if (!p)
447                 p = id;
448             fprintf(fp,"    %s = Identifier::idPool(\"%s\");\n", id, p);
449         }
450 
451         fprintf(fp, "}\n");
452 
453         fclose(fp);
454     }
455 
456     {
457         FILE *fp = fopen("id.d","wb");
458         if (!fp)
459         {
460             printf("can't open id.d\n");
461             exit(EXIT_FAILURE);
462         }
463 
464         fprintf(fp, "// File generated by idgen.c\n");
465         fprintf(fp, "\n");
466         fprintf(fp, "module ddmd.id;\n");
467         fprintf(fp, "\n");
468         fprintf(fp, "import ddmd.identifier, ddmd.tokens;\n");
469         fprintf(fp, "\n");
470         fprintf(fp, "struct Id\n");
471         fprintf(fp, "{\n");
472 
473         for (unsigned i = 0; i < sizeof(msgtable) / sizeof(msgtable[0]); i++)
474         {
475             const char *id = msgtable[i].ident;
476             const char *p = msgtable[i].name;
477 
478             if (!p)
479                 p = id;
480             fprintf(fp, "    extern (C++) static __gshared Identifier %s;\n", id);
481         }
482 
483         fprintf(fp, "\n");
484         fprintf(fp, "    extern (C++) static void initialize()\n");
485         fprintf(fp, "    {\n");
486 
487         for (unsigned i = 0; i < sizeof(msgtable) / sizeof(msgtable[0]); i++)
488         {
489             const char *id = msgtable[i].ident;
490             const char *p = msgtable[i].name;
491 
492             if (!p)
493                 p = id;
494             fprintf(fp,"        %s = Identifier.idPool(\"%s\");\n", id, p);
495         }
496 
497         fprintf(fp, "    }\n");
498         fprintf(fp, "}\n");
499 
500         fclose(fp);
501     }
502 
503     return EXIT_SUCCESS;
504 }
505