| 1 |
// Compiler implementation of the D programming language |
|---|
| 2 |
// Copyright (c) 1999-2010 by Digital Mars |
|---|
| 3 |
// All Rights Reserved |
|---|
| 4 |
// written by Walter Bright |
|---|
| 5 |
// http://www.digitalmars.com |
|---|
| 6 |
// http://www.dsource.org/projects/dmd/browser/trunk/src/idgen.c |
|---|
| 7 |
// License for redistribution is by either the Artistic License |
|---|
| 8 |
// in artistic.txt, or the GNU General Public License in gnu.txt. |
|---|
| 9 |
// See the included readme.txt for details. |
|---|
| 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 <stdio.h> |
|---|
| 18 |
#include <stdlib.h> |
|---|
| 19 |
#include <string.h> |
|---|
| 20 |
#include <assert.h> |
|---|
| 21 |
|
|---|
| 22 |
struct Msgtable |
|---|
| 23 |
{ |
|---|
| 24 |
const char *ident; // name to use in DMD source |
|---|
| 25 |
const char *name; // name in D executable |
|---|
| 26 |
}; |
|---|
| 27 |
|
|---|
| 28 |
Msgtable msgtable[] = |
|---|
| 29 |
{ |
|---|
| 30 |
{ "IUnknown" }, |
|---|
| 31 |
{ "Object" }, |
|---|
| 32 |
{ "object" }, |
|---|
| 33 |
{ "max" }, |
|---|
| 34 |
{ "min" }, |
|---|
| 35 |
{ "This", "this" }, |
|---|
| 36 |
{ "ctor", "__ctor" }, |
|---|
| 37 |
{ "dtor", "__dtor" }, |
|---|
| 38 |
{ "cpctor", "__cpctor" }, |
|---|
| 39 |
{ "_postblit", "__postblit" }, |
|---|
| 40 |
{ "classInvariant", "__invariant" }, |
|---|
| 41 |
{ "unitTest", "__unitTest" }, |
|---|
| 42 |
{ "require", "__require" }, |
|---|
| 43 |
{ "ensure", "__ensure" }, |
|---|
| 44 |
{ "init" }, |
|---|
| 45 |
{ "size" }, |
|---|
| 46 |
{ "__sizeof", "sizeof" }, |
|---|
| 47 |
{ "__xalignof", "alignof" }, |
|---|
| 48 |
{ "mangleof" }, |
|---|
| 49 |
{ "stringof" }, |
|---|
| 50 |
{ "tupleof" }, |
|---|
| 51 |
{ "length" }, |
|---|
| 52 |
{ "remove" }, |
|---|
| 53 |
{ "ptr" }, |
|---|
| 54 |
{ "funcptr" }, |
|---|
| 55 |
{ "dollar", "__dollar" }, |
|---|
| 56 |
{ "ctfe", "__ctfe" }, |
|---|
| 57 |
{ "offset" }, |
|---|
| 58 |
{ "offsetof" }, |
|---|
| 59 |
{ "ModuleInfo" }, |
|---|
| 60 |
{ "ClassInfo" }, |
|---|
| 61 |
{ "classinfo" }, |
|---|
| 62 |
{ "typeinfo" }, |
|---|
| 63 |
{ "outer" }, |
|---|
| 64 |
{ "Exception" }, |
|---|
| 65 |
{ "AssociativeArray" }, |
|---|
| 66 |
{ "Throwable" }, |
|---|
| 67 |
{ "withSym", "__withSym" }, |
|---|
| 68 |
{ "result", "__result" }, |
|---|
| 69 |
{ "returnLabel", "__returnLabel" }, |
|---|
| 70 |
{ "delegate" }, |
|---|
| 71 |
{ "line" }, |
|---|
| 72 |
{ "empty", "" }, |
|---|
| 73 |
{ "p" }, |
|---|
| 74 |
{ "coverage", "__coverage" }, |
|---|
| 75 |
{ "__vptr" }, |
|---|
| 76 |
{ "__monitor" }, |
|---|
| 77 |
|
|---|
| 78 |
{ "TypeInfo" }, |
|---|
| 79 |
{ "TypeInfo_Class" }, |
|---|
| 80 |
{ "TypeInfo_Interface" }, |
|---|
| 81 |
{ "TypeInfo_Struct" }, |
|---|
| 82 |
{ "TypeInfo_Enum" }, |
|---|
| 83 |
{ "TypeInfo_Typedef" }, |
|---|
| 84 |
{ "TypeInfo_Pointer" }, |
|---|
| 85 |
{ "TypeInfo_Array" }, |
|---|
| 86 |
{ "TypeInfo_StaticArray" }, |
|---|
| 87 |
{ "TypeInfo_AssociativeArray" }, |
|---|
| 88 |
{ "TypeInfo_Function" }, |
|---|
| 89 |
{ "TypeInfo_Delegate" }, |
|---|
| 90 |
{ "TypeInfo_Tuple" }, |
|---|
| 91 |
{ "TypeInfo_Const" }, |
|---|
| 92 |
{ "TypeInfo_Invariant" }, |
|---|
| 93 |
{ "TypeInfo_Shared" }, |
|---|
| 94 |
{ "TypeInfo_Wild", "TypeInfo_Inout" }, |
|---|
| 95 |
{ "elements" }, |
|---|
| 96 |
{ "_arguments_typeinfo" }, |
|---|
| 97 |
{ "_arguments" }, |
|---|
| 98 |
{ "_argptr" }, |
|---|
| 99 |
{ "_match" }, |
|---|
| 100 |
{ "destroy" }, |
|---|
| 101 |
{ "postblit" }, |
|---|
| 102 |
|
|---|
| 103 |
{ "LINE", "__LINE__" }, |
|---|
| 104 |
{ "FILE", "__FILE__" }, |
|---|
| 105 |
{ "DATE", "__DATE__" }, |
|---|
| 106 |
{ "TIME", "__TIME__" }, |
|---|
| 107 |
{ "TIMESTAMP", "__TIMESTAMP__" }, |
|---|
| 108 |
{ "VENDOR", "__VENDOR__" }, |
|---|
| 109 |
{ "VERSIONX", "__VERSION__" }, |
|---|
| 110 |
{ "EOFX", "__EOF__" }, |
|---|
| 111 |
|
|---|
| 112 |
{ "nan" }, |
|---|
| 113 |
{ "infinity" }, |
|---|
| 114 |
{ "dig" }, |
|---|
| 115 |
{ "epsilon" }, |
|---|
| 116 |
{ "mant_dig" }, |
|---|
| 117 |
{ "max_10_exp" }, |
|---|
| 118 |
{ "max_exp" }, |
|---|
| 119 |
{ "min_10_exp" }, |
|---|
| 120 |
{ "min_exp" }, |
|---|
| 121 |
{ "min_normal" }, |
|---|
| 122 |
{ "re" }, |
|---|
| 123 |
{ "im" }, |
|---|
| 124 |
|
|---|
| 125 |
{ "C" }, |
|---|
| 126 |
{ "D" }, |
|---|
| 127 |
{ "Windows" }, |
|---|
| 128 |
{ "Pascal" }, |
|---|
| 129 |
{ "System" }, |
|---|
| 130 |
|
|---|
| 131 |
{ "exit" }, |
|---|
| 132 |
{ "success" }, |
|---|
| 133 |
{ "failure" }, |
|---|
| 134 |
|
|---|
| 135 |
{ "keys" }, |
|---|
| 136 |
{ "values" }, |
|---|
| 137 |
{ "rehash" }, |
|---|
| 138 |
|
|---|
| 139 |
{ "sort" }, |
|---|
| 140 |
{ "reverse" }, |
|---|
| 141 |
{ "dup" }, |
|---|
| 142 |
{ "idup" }, |
|---|
| 143 |
|
|---|
| 144 |
{ "property" }, |
|---|
| 145 |
{ "safe" }, |
|---|
| 146 |
{ "trusted" }, |
|---|
| 147 |
{ "system" }, |
|---|
| 148 |
{ "disable" }, |
|---|
| 149 |
|
|---|
| 150 |
// For inline assembler |
|---|
| 151 |
{ "___out", "out" }, |
|---|
| 152 |
{ "___in", "in" }, |
|---|
| 153 |
{ "__int", "int" }, |
|---|
| 154 |
{ "__dollar", "$" }, |
|---|
| 155 |
{ "__LOCAL_SIZE" }, |
|---|
| 156 |
|
|---|
| 157 |
// For operator overloads |
|---|
| 158 |
{ "uadd", "opPos" }, |
|---|
| 159 |
{ "neg", "opNeg" }, |
|---|
| 160 |
{ "com", "opCom" }, |
|---|
| 161 |
{ "add", "opAdd" }, |
|---|
| 162 |
{ "add_r", "opAdd_r" }, |
|---|
| 163 |
{ "sub", "opSub" }, |
|---|
| 164 |
{ "sub_r", "opSub_r" }, |
|---|
| 165 |
{ "mul", "opMul" }, |
|---|
| 166 |
{ "mul_r", "opMul_r" }, |
|---|
| 167 |
{ "div", "opDiv" }, |
|---|
| 168 |
{ "div_r", "opDiv_r" }, |
|---|
| 169 |
{ "mod", "opMod" }, |
|---|
| 170 |
{ "mod_r", "opMod_r" }, |
|---|
| 171 |
{ "eq", "opEquals" }, |
|---|
| 172 |
{ "cmp", "opCmp" }, |
|---|
| 173 |
{ "iand", "opAnd" }, |
|---|
| 174 |
{ "iand_r", "opAnd_r" }, |
|---|
| 175 |
{ "ior", "opOr" }, |
|---|
| 176 |
{ "ior_r", "opOr_r" }, |
|---|
| 177 |
{ "ixor", "opXor" }, |
|---|
| 178 |
{ "ixor_r", "opXor_r" }, |
|---|
| 179 |
{ "shl", "opShl" }, |
|---|
| 180 |
{ "shl_r", "opShl_r" }, |
|---|
| 181 |
{ "shr", "opShr" }, |
|---|
| 182 |
{ "shr_r", "opShr_r" }, |
|---|
| 183 |
{ "ushr", "opUShr" }, |
|---|
| 184 |
{ "ushr_r", "opUShr_r" }, |
|---|
| 185 |
{ "cat", "opCat" }, |
|---|
| 186 |
{ "cat_r", "opCat_r" }, |
|---|
| 187 |
{ "assign", "opAssign" }, |
|---|
| 188 |
{ "addass", "opAddAssign" }, |
|---|
| 189 |
{ "subass", "opSubAssign" }, |
|---|
| 190 |
{ "mulass", "opMulAssign" }, |
|---|
| 191 |
{ "divass", "opDivAssign" }, |
|---|
| 192 |
{ "modass", "opModAssign" }, |
|---|
| 193 |
{ "andass", "opAndAssign" }, |
|---|
| 194 |
{ "orass", "opOrAssign" }, |
|---|
| 195 |
{ "xorass", "opXorAssign" }, |
|---|
| 196 |
{ "shlass", "opShlAssign" }, |
|---|
| 197 |
{ "shrass", "opShrAssign" }, |
|---|
| 198 |
{ "ushrass", "opUShrAssign" }, |
|---|
| 199 |
{ "catass", "opCatAssign" }, |
|---|
| 200 |
{ "postinc", "opPostInc" }, |
|---|
| 201 |
{ "postdec", "opPostDec" }, |
|---|
| 202 |
{ "index", "opIndex" }, |
|---|
| 203 |
{ "indexass", "opIndexAssign" }, |
|---|
| 204 |
{ "slice", "opSlice" }, |
|---|
| 205 |
{ "sliceass", "opSliceAssign" }, |
|---|
| 206 |
{ "call", "opCall" }, |
|---|
| 207 |
{ "cast", "opCast" }, |
|---|
| 208 |
{ "match", "opMatch" }, |
|---|
| 209 |
{ "next", "opNext" }, |
|---|
| 210 |
{ "opIn" }, |
|---|
| 211 |
{ "opIn_r" }, |
|---|
| 212 |
{ "opStar" }, |
|---|
| 213 |
{ "opDot" }, |
|---|
| 214 |
{ "opDispatch" }, |
|---|
| 215 |
{ "opUnary" }, |
|---|
| 216 |
{ "opIndexUnary" }, |
|---|
| 217 |
{ "opSliceUnary" }, |
|---|
| 218 |
{ "opBinary" }, |
|---|
| 219 |
{ "opBinaryRight" }, |
|---|
| 220 |
{ "opOpAssign" }, |
|---|
| 221 |
{ "opIndexOpAssign" }, |
|---|
| 222 |
{ "opSliceOpAssign" }, |
|---|
| 223 |
{ "pow", "opPow" }, |
|---|
| 224 |
{ "pow_r", "opPow_r" }, |
|---|
| 225 |
{ "powass", "opPowAssign" }, |
|---|
| 226 |
|
|---|
| 227 |
{ "classNew", "new" }, |
|---|
| 228 |
{ "classDelete", "delete" }, |
|---|
| 229 |
|
|---|
| 230 |
// For foreach |
|---|
| 231 |
{ "apply", "opApply" }, |
|---|
| 232 |
{ "applyReverse", "opApplyReverse" }, |
|---|
| 233 |
|
|---|
| 234 |
#if 1 |
|---|
| 235 |
{ "Fempty", "empty" }, |
|---|
| 236 |
{ "Fhead", "front" }, |
|---|
| 237 |
{ "Ftoe", "back" }, |
|---|
| 238 |
{ "Fnext", "popFront" }, |
|---|
| 239 |
{ "Fretreat", "popBack" }, |
|---|
| 240 |
#else |
|---|
| 241 |
{ "Fempty", "empty" }, |
|---|
| 242 |
{ "Fhead", "head" }, |
|---|
| 243 |
{ "Ftoe", "toe" }, |
|---|
| 244 |
{ "Fnext", "next" }, |
|---|
| 245 |
{ "Fretreat", "retreat" }, |
|---|
| 246 |
#endif |
|---|
| 247 |
|
|---|
| 248 |
{ "adDup", "_adDupT" }, |
|---|
| 249 |
{ "adReverse", "_adReverse" }, |
|---|
| 250 |
|
|---|
| 251 |
// For internal functions |
|---|
| 252 |
{ "aaLen", "_aaLen" }, |
|---|
| 253 |
{ "aaKeys", "_aaKeys" }, |
|---|
| 254 |
{ "aaValues", "_aaValues" }, |
|---|
| 255 |
{ "aaRehash", "_aaRehash" }, |
|---|
| 256 |
{ "monitorenter", "_d_monitorenter" }, |
|---|
| 257 |
{ "monitorexit", "_d_monitorexit" }, |
|---|
| 258 |
{ "criticalenter", "_d_criticalenter" }, |
|---|
| 259 |
{ "criticalexit", "_d_criticalexit" }, |
|---|
| 260 |
{ "_ArrayEq" }, |
|---|
| 261 |
|
|---|
| 262 |
// For pragma's |
|---|
| 263 |
{ "GNU_asm" }, |
|---|
| 264 |
{ "lib" }, |
|---|
| 265 |
{ "msg" }, |
|---|
| 266 |
{ "startaddress" }, |
|---|
| 267 |
|
|---|
| 268 |
// For special functions |
|---|
| 269 |
{ "tohash", "toHash" }, |
|---|
| 270 |
{ "tostring", "toString" }, |
|---|
| 271 |
{ "getmembers", "getMembers" }, |
|---|
| 272 |
|
|---|
| 273 |
// Special functions |
|---|
| 274 |
{ "alloca" }, |
|---|
| 275 |
{ "main" }, |
|---|
| 276 |
{ "WinMain" }, |
|---|
| 277 |
{ "DllMain" }, |
|---|
| 278 |
{ "tls_get_addr", "___tls_get_addr" }, |
|---|
| 279 |
|
|---|
| 280 |
// varargs implementation |
|---|
| 281 |
{ "va_argsave_t", "__va_argsave_t" }, |
|---|
| 282 |
{ "va_argsave", "__va_argsave" }, |
|---|
| 283 |
|
|---|
| 284 |
// Builtin functions |
|---|
| 285 |
{ "std" }, |
|---|
| 286 |
{ "math" }, |
|---|
| 287 |
{ "sin" }, |
|---|
| 288 |
{ "cos" }, |
|---|
| 289 |
{ "tan" }, |
|---|
| 290 |
{ "_sqrt", "sqrt" }, |
|---|
| 291 |
{ "_pow", "pow" }, |
|---|
| 292 |
{ "fabs" }, |
|---|
| 293 |
|
|---|
| 294 |
// Traits |
|---|
| 295 |
{ "isAbstractClass" }, |
|---|
| 296 |
{ "isArithmetic" }, |
|---|
| 297 |
{ "isAssociativeArray" }, |
|---|
| 298 |
{ "isFinalClass" }, |
|---|
| 299 |
{ "isFloating" }, |
|---|
| 300 |
{ "isIntegral" }, |
|---|
| 301 |
{ "isScalar" }, |
|---|
| 302 |
{ "isStaticArray" }, |
|---|
| 303 |
{ "isUnsigned" }, |
|---|
| 304 |
{ "isVirtualFunction" }, |
|---|
| 305 |
{ "isAbstractFunction" }, |
|---|
| 306 |
{ "isFinalFunction" }, |
|---|
| 307 |
{ "isStaticFunction" }, |
|---|
| 308 |
{ "isRef" }, |
|---|
| 309 |
{ "isOut" }, |
|---|
| 310 |
{ "isLazy" }, |
|---|
| 311 |
{ "hasMember" }, |
|---|
| 312 |
{ "identifier" }, |
|---|
| 313 |
{ "getMember" }, |
|---|
| 314 |
{ "getOverloads" }, |
|---|
| 315 |
{ "getVirtualFunctions" }, |
|---|
| 316 |
{ "classInstanceSize" }, |
|---|
| 317 |
{ "allMembers" }, |
|---|
| 318 |
{ "derivedMembers" }, |
|---|
| 319 |
{ "isSame" }, |
|---|
| 320 |
{ "compiles" }, |
|---|
| 321 |
}; |
|---|
| 322 |
|
|---|
| 323 |
|
|---|
| 324 |
int main() |
|---|
| 325 |
{ |
|---|
| 326 |
FILE *fp; |
|---|
| 327 |
unsigned i; |
|---|
| 328 |
|
|---|
| 329 |
{ |
|---|
| 330 |
fp = fopen("id.h","w"); |
|---|
| 331 |
if (!fp) |
|---|
| 332 |
{ printf("can't open id.h\n"); |
|---|
| 333 |
exit(EXIT_FAILURE); |
|---|
| 334 |
} |
|---|
| 335 |
|
|---|
| 336 |
fprintf(fp, "// File generated by idgen.c\n"); |
|---|
| 337 |
#if __DMC__ |
|---|
| 338 |
fprintf(fp, "#pragma once\n"); |
|---|
| 339 |
#endif |
|---|
| 340 |
fprintf(fp, "#ifndef DMD_ID_H\n"); |
|---|
| 341 |
fprintf(fp, "#define DMD_ID_H 1\n"); |
|---|
| 342 |
fprintf(fp, "struct Identifier;\n"); |
|---|
| 343 |
fprintf(fp, "struct Id\n"); |
|---|
| 344 |
fprintf(fp, "{\n"); |
|---|
| 345 |
|
|---|
| 346 |
for (i = 0; i < sizeof(msgtable) / sizeof(msgtable[0]); i++) |
|---|
| 347 |
{ const char *id = msgtable[i].ident; |
|---|
| 348 |
|
|---|
| 349 |
fprintf(fp," static Identifier *%s;\n", id); |
|---|
| 350 |
} |
|---|
| 351 |
|
|---|
| 352 |
fprintf(fp, " static void initialize();\n"); |
|---|
| 353 |
fprintf(fp, "};\n"); |
|---|
| 354 |
fprintf(fp, "#endif\n"); |
|---|
| 355 |
|
|---|
| 356 |
fclose(fp); |
|---|
| 357 |
} |
|---|
| 358 |
|
|---|
| 359 |
{ |
|---|
| 360 |
fp = fopen("id.c","w"); |
|---|
| 361 |
if (!fp) |
|---|
| 362 |
{ printf("can't open id.c\n"); |
|---|
| 363 |
exit(EXIT_FAILURE); |
|---|
| 364 |
} |
|---|
| 365 |
|
|---|
| 366 |
fprintf(fp, "// File generated by idgen.c\n"); |
|---|
| 367 |
fprintf(fp, "#include \"id.h\"\n"); |
|---|
| 368 |
fprintf(fp, "#include \"identifier.h\"\n"); |
|---|
| 369 |
fprintf(fp, "#include \"lexer.h\"\n"); |
|---|
| 370 |
|
|---|
| 371 |
for (i = 0; i < sizeof(msgtable) / sizeof(msgtable[0]); i++) |
|---|
| 372 |
{ const char *id = msgtable[i].ident; |
|---|
| 373 |
const char *p = msgtable[i].name; |
|---|
| 374 |
|
|---|
| 375 |
if (!p) |
|---|
| 376 |
p = id; |
|---|
| 377 |
fprintf(fp,"Identifier *Id::%s;\n", id); |
|---|
| 378 |
} |
|---|
| 379 |
|
|---|
| 380 |
fprintf(fp, "void Id::initialize()\n"); |
|---|
| 381 |
fprintf(fp, "{\n"); |
|---|
| 382 |
|
|---|
| 383 |
for (i = 0; i < sizeof(msgtable) / sizeof(msgtable[0]); i++) |
|---|
| 384 |
{ const char *id = msgtable[i].ident; |
|---|
| 385 |
const char *p = msgtable[i].name; |
|---|
| 386 |
|
|---|
| 387 |
if (!p) |
|---|
| 388 |
p = id; |
|---|
| 389 |
fprintf(fp," %s = Lexer::idPool(\"%s\");\n", id, p); |
|---|
| 390 |
} |
|---|
| 391 |
|
|---|
| 392 |
fprintf(fp, "}\n"); |
|---|
| 393 |
|
|---|
| 394 |
fclose(fp); |
|---|
| 395 |
} |
|---|
| 396 |
|
|---|
| 397 |
return EXIT_SUCCESS; |
|---|
| 398 |
} |
|---|