11 static Context globalContext { };
12 static Module privateModule;
13 static ModuleImport mainModule;
14 static OldList _excludedSymbols { offset = (uint)&((Symbol)0).left };
15 static OldList defines, imports;
16 static NameSpace globalData
18 classes.CompareKey = (void *)BinaryTree::CompareString;
19 defines.CompareKey = (void *)BinaryTree::CompareString;
20 functions.CompareKey = (void *)BinaryTree::CompareString;
21 nameSpaces.CompareKey = (void *)BinaryTree::CompareString;
23 static OldList _precompDefines;
25 static void OutputImports(char * fileName)
27 File f = FileOpen(fileName, write);
33 f.Printf("[Imported Modules]\n");
34 for(module = imports.first; module; module = module.next)
37 FunctionImport function;
40 f.Printf(" %s\n", module.name);
42 f.Printf(" [This]\n");
43 if(module.importType == staticImport)
44 f.Printf(" [Static]\n");
45 else if(module.importType == remoteImport)
46 f.Printf(" [Remote]\n");
48 if(module.importAccess == privateAccess)
49 f.Printf(" [Private]\n");
51 f.Printf(" [Public]\n");
53 if(module.classes.first)
55 f.Printf(" [Imported Classes]\n");
56 for(_class = module.classes.first; _class; _class = _class.next)
58 f.Printf(" %s\n", _class.name);
61 f.Printf(" [Instantiated]\n");
65 f.Printf(" [Remote]\n");
68 if(_class.methods.first)
71 f.Printf(" [Imported Methods]\n");
72 for(method = _class.methods.first; method; method = method.next)
74 f.Printf(" %s\n", method.name);
76 f.Printf(" [Virtual]\n");
82 if(_class.properties.first)
85 f.Printf(" [Imported Properties]\n");
86 for(prop = _class.properties.first; prop; prop = prop.next)
88 f.Printf(" %s\n", prop.name);
90 f.Printf(" [Virtual]\n");
101 if(module.functions.first)
103 f.Printf(" [Imported Functions]\n");
104 for(function = module.functions.first; function; function = function.next)
106 f.Printf(" %s\n", function.name);
117 class CompilerApp : Application
121 char * cppCommand = null;
122 char * cppOptions = null;
123 int cppOptionsLen = 0;
124 /*char ** argv = null;
128 char defaultOutputFile[MAX_LOCATION];
130 Platform targetPlatform = GetRuntimePlatform();
131 int targetBits = GetHostBits();
135 /*for(c = 0; c<this.argc; c++)
137 char * arg = this.argv[c];
138 int argLen = strlen(arg);
140 argv = renew argv char *[argc + 1];
141 argv[argc] = new char[argLen+1];
142 strcpy(argv[argc], arg);
144 while(argv[argc][argLen-1] == '\\' && c < this.argc-1)
151 argv[argc] = renew argv[argc] char[argLen + len + 1];
153 argv[argc][argLen-1] = ' ';
154 strcpy(argv[argc] + argLen, arg);
160 for(c = 1; c<argc; c++)
162 char * arg = argv[c];
165 if(!strcmp(arg + 1, "m32") || !strcmp(arg + 1, "m64"))
167 int argLen = strlen(arg);
168 int newLen = cppOptionsLen + 1 + argLen;
169 cppOptions = renew cppOptions char[newLen + 1];
170 cppOptions[cppOptionsLen] = ' ';
171 strcpy(cppOptions + cppOptionsLen + 1, arg);
172 cppOptionsLen = newLen;
173 targetBits = !strcmp(arg + 1, "m32") ? 32 : 64;
175 else if(arg[1] == 'D')
177 int argLen = strlen(arg);
178 int newLen = cppOptionsLen + 1 + argLen;
179 cppOptions = renew cppOptions char[newLen + 1];
180 cppOptions[cppOptionsLen] = ' ';
181 strcpy(cppOptions + cppOptionsLen + 1, arg);
182 cppOptionsLen = newLen;
183 if(!strcmp(arg, "-DBUILDING_ECERE_COM"))
184 SetBuildingEcereCom(true);
185 else if(!strcmp(arg, "-DECERE_COM_MODULE"))
186 SetBuildingEcereComModule(true);
188 else if(arg[1] == 'I')
190 int argLen = strlen(arg);
191 int newLen = cppOptionsLen + argLen + 3;
192 cppOptions = renew cppOptions char[newLen + 1];
193 cppOptions[cppOptionsLen] = ' ';
194 cppOptions[cppOptionsLen+1] = '-';
195 cppOptions[cppOptionsLen+2] = 'I';
196 cppOptions[cppOptionsLen+3] = '"';
197 strcpy(cppOptions + cppOptionsLen + 4, arg+2);
198 cppOptions[newLen-1] = '\"';
199 cppOptions[newLen] = '\0';
200 cppOptionsLen = newLen;
202 else if(!strcmp(arg+1, "t"))
205 targetPlatform = argv[c];
209 else if(!strcmp(arg+1, "cpp"))
212 cppCommand = CopyString(argv[c]);
216 else if(!strcmp(arg+1, "o"))
218 if(!GetOutputFile() && c + 1 < argc)
220 SetOutputFile(argv[c+1]);
226 else if(!strcmp(arg+1, "c"))
228 if(!GetSourceFile() && c + 1 < argc)
230 SetSourceFile(argv[c+1]);
236 else if(!strcmp(arg+1, "isystem") || !strcmp(arg+1, "isysroot"))
240 int argLen = strlen(arg);
241 int arg1Len = strlen(argv[c+1]);
242 int newLen = cppOptionsLen + argLen + arg1Len + 4;
243 cppOptions = renew cppOptions char[newLen + 1];
244 cppOptions[cppOptionsLen] = ' ';
245 strcpy(cppOptions + cppOptionsLen + 1, arg);
246 cppOptions[cppOptionsLen+argLen+1] = ' ';
247 cppOptions[cppOptionsLen+argLen+2] = '"';
249 strcpy(cppOptions + cppOptionsLen + argLen + 3, arg);
250 cppOptions[newLen-1] = '\"';
251 cppOptions[newLen] = '\0';
252 cppOptionsLen = newLen;
257 else if(!strcmp(arg+1, "symbols"))
261 SetSymbolsDir(argv[c+1]);
267 else if(!strcmp(arg+1, "memguard"))
269 SetMemoryGuard(true);
271 else if(!strcmp(arg+1, "defaultns"))
275 SetDefaultNameSpace(argv[c+1]);
276 //defaultNameSpaceLen = strlen(defaultNameSpace);
282 else if(!strcmp(arg+1, "strictns"))
284 SetStrictNameSpaces(true);
286 else if(!strcmp(arg+1, "nolinenumbers"))
288 SetOutputLineNumbers(false);
297 cppCommand = CopyString("gcc");
300 else if(!GetOutputFile())
302 strcpy(defaultOutputFile, "");
303 PathCat(defaultOutputFile, GetSourceFile());
304 ChangeExtension(defaultOutputFile, "c", defaultOutputFile);
305 SetOutputFile(defaultOutputFile);
310 printf($"Syntax:\n ecc [-t <target platform>] [-cpp <c preprocessor>] [-o <output>] [-symbols <outputdir>] [-I<includedir>]* [-isystem <sysincludedir>]* [-D<definition>]* -c <input>\n");
314 // TODO: Improve this
315 char command[MAX_F_STRING*3];
316 SetGlobalData(&globalData);
317 SetExcludedSymbols(&_excludedSymbols);
318 SetGlobalContext(globalContext);
319 SetCurrentContext(globalContext);
320 SetTopContext(globalContext);
321 SetDefines(&::defines);
322 SetImports(&imports);
324 SetTargetPlatform(targetPlatform);
325 SetTargetBits(targetBits);
328 privateModule = (Module)__ecere_COM_Initialize(true | (targetBits == sizeof(uintptr)*8 ? 0 : targetBits == 64 ? 2 : targetBits==32 ? 4 : 0) | 8, 1, null);
329 SetPrivateModule(privateModule);
331 globalContext.types.Add((BTNode)Symbol { string = CopyString("uint"), type = ProcessTypeString("unsigned int", false) });
332 globalContext.types.Add((BTNode)Symbol { string = CopyString("uint64"), type = ProcessTypeString("unsigned int64", false) });
333 globalContext.types.Add((BTNode)Symbol { string = CopyString("uint32"), type = ProcessTypeString("unsigned int", false) });
334 globalContext.types.Add((BTNode)Symbol { string = CopyString("uint16"), type = ProcessTypeString("unsigned short", false) });
335 globalContext.types.Add((BTNode)Symbol { string = CopyString("byte"), type = ProcessTypeString("unsigned char", false) });
336 globalContext.types.Add((BTNode)Symbol { string = CopyString("intptr_t"), type = ProcessTypeString("intptr", false) });
337 globalContext.types.Add((BTNode)Symbol { string = CopyString("uintptr_t"), type = ProcessTypeString("uintptr", false) });
340 GlobalData data { fullName = CopyString("__thisModule"), dataTypeString = CopyString("Module"), module = privateModule };
341 data.key = (uintptr)data.fullName;
342 globalData.functions.Add((BTNode)data);
345 snprintf(command, sizeof(command), "%s%s -x c -E -D_INTPTR_T_DEFINED -D_UINTPTR_T_DEFINED \"%s\"", cppCommand, cppOptions ? cppOptions : "", GetSourceFile());
346 command[sizeof(command)-1] = 0;
347 if((cppOutput = DualPipeOpen({ output = true }, command)))
349 char impFile[MAX_LOCATION];
350 ImportedModule module;
351 char mainModuleName[MAX_FILENAME];
355 TempFile fileInput { };
356 SetFileInput(fileInput);
358 imports.Add((mainModule = ModuleImport { }));
359 SetMainModule(mainModule);
361 GetLastDirectory(GetSourceFile(), mainModuleName);
364 // TEMP: UNTIL WE CAN HAVE PER SOURCE FILE PREPROCESSOR DEFINITIONS...
365 if(GetBuildingEcereCom() &&
366 !(strcmpi(mainModuleName, "instance.ec") && strcmpi(mainModuleName, "BinaryTree.ec") &&
367 strcmpi(mainModuleName, "dataTypes.ec") && strcmpi(mainModuleName, "OldList.ec") &&
368 strcmpi(mainModuleName, "String.ec") && strcmpi(mainModuleName, "BTNode.ec") &&
369 strcmpi(mainModuleName, "Array.ec") && strcmpi(mainModuleName, "AVLTree.ec") &&
370 strcmpi(mainModuleName, "BuiltInContainer.ec") && strcmpi(mainModuleName, "Container.ec") &&
371 strcmpi(mainModuleName, "CustomAVLTree.ec") && strcmpi(mainModuleName, "LinkList.ec") &&
372 strcmpi(mainModuleName, "List.ec") && strcmpi(mainModuleName, "Map.ec") &&
373 strcmpi(mainModuleName, "Mutex.ec")))
374 SetBuildingEcereComModule(true);
375 if(GetBuildingEcereCom() &&
376 !(strcmpi(mainModuleName, "instance.ec") && strcmpi(mainModuleName, "BinaryTree.ec") &&
377 /*strcmpi(mainModuleName, "dataTypes.ec") && strcmpi(mainModuleName, "OldList.ec") &&*/
378 /*strcmpi(mainModuleName, "String.ec") && */strcmpi(mainModuleName, "BTNode.ec") &&
379 strcmpi(mainModuleName, "Mutex.ec") && strcmpi(mainModuleName, "Thread.ec")))
380 //if(GetBuildingEcereCom() && !strcmpi(mainModuleName, "instance.ec"))
381 SetMemoryGuard(false);
384 StripExtension(mainModuleName);
385 module = ImportedModule { name = CopyString(mainModuleName), type = moduleDefinition };
386 ::defines.AddName(module);
389 while(!cppOutput.Eof())
392 int count = cppOutput.Read(junk, 1, 4096);
393 fileInput.Write(junk, 1, count);
395 exitCode = cppOutput.GetExitCode();
398 fileInput.Seek(0, start);
404 // Predeclare all classes
406 char symFile[MAX_FILENAME];
407 char symLocation[MAX_LOCATION];
408 ImportedModule module, next;
410 GetLastDirectory(GetSourceFile(), symFile);
411 ChangeExtension(symFile, "sym", symFile);
413 strcpy(symLocation, GetSymbolsDir());
414 PathCat(symLocation, symFile);
416 // LoadSymbols(symLocation, normalImport, true);
417 LoadSymbols(symLocation, preDeclImport, false);
419 for(module = ::defines.first; module; module = next)
422 if(module.type == moduleDefinition && strcmpi(module.name, mainModuleName))
425 ::defines.Delete(module);
429 if(!GetEcereImported() && !GetBuildingEcereCom())
430 eModule_LoadStrict(privateModule, "ecereCOM", publicAccess /*privateAccess*/);
435 CheckDataRedefinitions();
438 SetCurrentNameSpace(null);
439 SetDefaultNameSpace(null);
440 SetDeclMode(privateAccess);
447 if(/*ast /*&& !parseError*/ /*&& */!exitCode)
449 ProcessDBTableDefinitions();
451 // *** PASS 0 - Register all classes, methods, properties and members ***
452 // *** Build the constructors, destructors, properties as ***
453 // *** class functions ***
454 PrePreProcessClassDefinitions();
455 ComputeModuleClasses(privateModule);
456 PreProcessClassDefinitions();
458 // For classes defined in this module...
459 ComputeModuleClasses(privateModule);
462 // *** PASS 1 - Turn the class functions into functions ***
463 // *** Write the RegisterModule (Register classes) ***
464 ProcessClassDefinitions();
466 // *** PASS 1.5 - Replace members by this.member, figure out types, do type checking / conversions
469 // *** PASS 1.6 - Replace instantiations
470 ProcessInstantiations();
472 // *** PASS 2 - Replace Member Access ***
473 ProcessMemberAccess();
475 // *** PASS 3 - Replace pointers to objects by "Instance *" ***
476 ProcessInstanceDeclarations();
478 strcpy(impFile, GetSymbolsDir());
480 char fileName[MAX_FILENAME];
481 GetLastDirectory(GetSourceFile(), fileName);
482 PathCat(impFile, fileName);
483 ChangeExtension(impFile, "imp", impFile);
486 OutputImports(impFile);
487 // For now use precomp to generate sym file only...
489 if(/*!strcmp(targetExt, "c") && */!this.exitCode)
491 File output = FileOpen(GetOutputFile(), write);
494 output.Printf("#if defined(__GNUC__)\n");
495 output.Printf("typedef long long int64;\n");
496 output.Printf("typedef unsigned long long uint64;\n");
497 output.Printf("#ifndef _WIN32\n");
498 output.Printf("#define __declspec(x)\n");
499 output.Printf("#endif\n");
500 output.Printf("#elif defined(__TINYC__)\n");
501 output.Printf("#include <stdarg.h>\n");
502 output.Printf("#define __builtin_va_list va_list\n");
503 output.Printf("#define __builtin_va_start va_start\n");
504 output.Printf("#define __builtin_va_end va_end\n");
505 output.Printf("#ifdef _WIN32\n");
506 output.Printf("#define strcasecmp stricmp\n");
507 output.Printf("#define strncasecmp strnicmp\n");
508 output.Printf("#define __declspec(x) __attribute__((x))\n");
509 output.Printf("#else\n");
510 output.Printf("#define __declspec(x)\n");
511 output.Printf("#endif\n");
512 output.Printf("typedef long long int64;\n");
513 output.Printf("typedef unsigned long long uint64;\n");
514 output.Printf("#else\n");
515 output.Printf("typedef __int64 int64;\n");
516 output.Printf("typedef unsigned __int64 uint64;\n");
517 output.Printf("#endif\n");
518 output.Printf("#ifdef __BIG_ENDIAN__\n");
519 output.Printf("#define __ENDIAN_PAD(x) (8 - (x))\n");
520 output.Printf("#else\n");
521 output.Printf("#define __ENDIAN_PAD(x) 0\n");
522 output.Printf("#endif\n");
523 //output.Printf("#ifdef __MINGW32__\n");
524 //output.Printf("#ifdef _WIN64\n");
525 output.Printf("#if defined(_WIN64) || WORDSIZE == 64\n");
526 output.Printf("typedef unsigned long long int uintptr_t;\n");
527 output.Printf("typedef long long int intptr_t;\n");
528 output.Printf("#else\n");
529 output.Printf("typedef unsigned int uintptr_t;\n");
530 output.Printf("typedef int intptr_t;\n");
531 output.Printf("#endif\n");
532 //output.Printf("#else\n");
533 //output.Printf("#include <stdint.h>\n");
534 //output.Printf("#endif\n");
536 // NOTE: If anything is changed up there, the start outputLine must be updated in libec's output.c or Debugging lines will be wrong
539 OutputTree(ast, output);
544 else if(!strcmp(targetExt, "o"))
546 // Compile right away
547 File output = FileOpen(GetOutputFile(), Write);
548 output.Printf("#include <ecereCOM.h>\n\n");
549 OutputTree(ast, output);
554 this.exitCode = exitCode;
562 FreeContext(globalContext);
563 FreeExcludedSymbols(_excludedSymbols);
565 ::defines.Free(FreeModuleDefine);
566 imports.Free(FreeModuleImport);
568 FreeTypeData(privateModule);
570 FreeGlobalData(globalData);
572 delete privateModule;
578 /*for(c = 0; c<argc; c++)
582 SetSymbolsDir(null); // Free symbols dir
586 #if defined(_DEBUG) && defined(__WIN32__)
587 if(exitCode || GetNumWarnings())