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];
129 bool buildingBootStrap = false;
131 Platform targetPlatform = GetRuntimePlatform();
132 int targetBits = GetHostBits();
136 /*for(c = 0; c<this.argc; c++)
138 char * arg = this.argv[c];
139 int argLen = strlen(arg);
141 argv = renew argv char *[argc + 1];
142 argv[argc] = new char[argLen+1];
143 strcpy(argv[argc], arg);
145 while(argv[argc][argLen-1] == '\\' && c < this.argc-1)
152 argv[argc] = renew argv[argc] char[argLen + len + 1];
154 argv[argc][argLen-1] = ' ';
155 strcpy(argv[argc] + argLen, arg);
161 for(c = 1; c<argc; c++)
163 char * arg = argv[c];
166 if(!strcmp(arg + 1, "m32") || !strcmp(arg + 1, "m64"))
168 int argLen = strlen(arg);
169 int newLen = cppOptionsLen + 1 + argLen;
170 cppOptions = renew cppOptions char[newLen + 1];
171 cppOptions[cppOptionsLen] = ' ';
172 strcpy(cppOptions + cppOptionsLen + 1, arg);
173 cppOptionsLen = newLen;
174 targetBits = !strcmp(arg + 1, "m32") ? 32 : 64;
176 else if(arg[1] == 'D')
178 int argLen = strlen(arg);
179 int newLen = cppOptionsLen + 1 + argLen;
180 cppOptions = renew cppOptions char[newLen + 1];
181 cppOptions[cppOptionsLen] = ' ';
182 strcpy(cppOptions + cppOptionsLen + 1, arg);
183 cppOptionsLen = newLen;
184 if(!strcmp(arg, "-DBUILDING_ECERE_COM"))
185 SetBuildingEcereCom(true);
186 else if(!strcmp(arg, "-DECERE_COM_MODULE"))
187 SetBuildingEcereComModule(true);
188 else if(!strcmp(arg, "-DECERE_BOOTSTRAP"))
189 buildingBootStrap = true;
191 else if(arg[1] == 'I')
193 int argLen = strlen(arg);
194 int newLen = cppOptionsLen + argLen + 3;
195 cppOptions = renew cppOptions char[newLen + 1];
196 cppOptions[cppOptionsLen] = ' ';
197 cppOptions[cppOptionsLen+1] = '-';
198 cppOptions[cppOptionsLen+2] = 'I';
199 cppOptions[cppOptionsLen+3] = '"';
200 strcpy(cppOptions + cppOptionsLen + 4, arg+2);
201 cppOptions[newLen-1] = '\"';
202 cppOptions[newLen] = '\0';
203 cppOptionsLen = newLen;
205 else if(!strcmp(arg+1, "t"))
208 targetPlatform = argv[c];
212 else if(!strcmp(arg+1, "cpp"))
215 cppCommand = CopyString(argv[c]);
219 else if(!strcmp(arg+1, "o"))
221 if(!GetOutputFile() && c + 1 < argc)
223 SetOutputFile(argv[c+1]);
229 else if(!strcmp(arg+1, "c"))
231 if(!GetSourceFile() && c + 1 < argc)
233 SetSourceFile(argv[c+1]);
239 else if(!strcmp(arg+1, "isystem") || !strcmp(arg+1, "isysroot"))
243 int argLen = strlen(arg);
244 int arg1Len = strlen(argv[c+1]);
245 int newLen = cppOptionsLen + argLen + arg1Len + 4;
246 cppOptions = renew cppOptions char[newLen + 1];
247 cppOptions[cppOptionsLen] = ' ';
248 strcpy(cppOptions + cppOptionsLen + 1, arg);
249 cppOptions[cppOptionsLen+argLen+1] = ' ';
250 cppOptions[cppOptionsLen+argLen+2] = '"';
252 strcpy(cppOptions + cppOptionsLen + argLen + 3, arg);
253 cppOptions[newLen-1] = '\"';
254 cppOptions[newLen] = '\0';
255 cppOptionsLen = newLen;
260 else if(!strcmp(arg+1, "symbols"))
264 SetSymbolsDir(argv[c+1]);
270 else if(!strcmp(arg+1, "memguard"))
272 SetMemoryGuard(true);
274 else if(!strcmp(arg+1, "defaultns"))
278 SetDefaultNameSpace(argv[c+1]);
279 //defaultNameSpaceLen = strlen(defaultNameSpace);
285 else if(!strcmp(arg+1, "strictns"))
287 SetStrictNameSpaces(true);
289 else if(!strcmp(arg+1, "nolinenumbers"))
291 SetOutputLineNumbers(false);
300 cppCommand = CopyString("gcc");
303 else if(!GetOutputFile())
305 strcpy(defaultOutputFile, "");
306 PathCat(defaultOutputFile, GetSourceFile());
307 ChangeExtension(defaultOutputFile, "c", defaultOutputFile);
308 SetOutputFile(defaultOutputFile);
313 printf($"Syntax:\n ecc [-t <target platform>] [-cpp <c preprocessor>] [-o <output>] [-symbols <outputdir>] [-I<includedir>]* [-isystem <sysincludedir>]* [-D<definition>]* -c <input>\n");
317 // TODO: Improve this
318 char command[MAX_F_STRING*3];
319 SetGlobalData(&globalData);
320 SetExcludedSymbols(&_excludedSymbols);
321 SetGlobalContext(globalContext);
322 SetCurrentContext(globalContext);
323 SetTopContext(globalContext);
324 SetDefines(&::defines);
325 SetImports(&imports);
327 SetTargetPlatform(targetPlatform);
328 SetTargetBits(targetBits);
331 privateModule = (Module)__ecere_COM_Initialize(true | (targetBits == sizeof(uintptr)*8 ? 0 : targetBits == 64 ? 2 : targetBits==32 ? 4 : 0) | 8, 1, null);
332 SetPrivateModule(privateModule);
334 globalContext.types.Add((BTNode)Symbol { string = CopyString("uint"), type = ProcessTypeString("unsigned int", false) });
335 globalContext.types.Add((BTNode)Symbol { string = CopyString("uint64"), type = ProcessTypeString("unsigned int64", false) });
336 globalContext.types.Add((BTNode)Symbol { string = CopyString("uint32"), type = ProcessTypeString("unsigned int", false) });
337 globalContext.types.Add((BTNode)Symbol { string = CopyString("uint16"), type = ProcessTypeString("unsigned short", false) });
338 globalContext.types.Add((BTNode)Symbol { string = CopyString("byte"), type = ProcessTypeString("unsigned char", false) });
339 if(buildingBootStrap)
341 // Do not define this when we pre-include stdint.h or the eC compiler will be confused when parsing these types (External prioritization in pass15.ec will fail)
342 globalContext.types.Add((BTNode)Symbol { string = CopyString("intptr_t"), type = ProcessTypeString("intptr", false) });
343 globalContext.types.Add((BTNode)Symbol { string = CopyString("uintptr_t"), type = ProcessTypeString("uintptr", false) });
347 GlobalData data { fullName = CopyString("__thisModule"), dataTypeString = CopyString("Module"), module = privateModule };
348 data.key = (uintptr)data.fullName;
349 globalData.functions.Add((BTNode)data);
352 snprintf(command, sizeof(command), "%s%s -x c -E %s\"%s\"", cppCommand, cppOptions ? cppOptions : "", buildingBootStrap ? "" : "-include stdint.h ", GetSourceFile());
353 command[sizeof(command)-1] = 0;
354 if((cppOutput = DualPipeOpen({ output = true }, command)))
356 char impFile[MAX_LOCATION];
357 ImportedModule module;
358 char mainModuleName[MAX_FILENAME];
362 TempFile fileInput { };
363 SetFileInput(fileInput);
365 imports.Add((mainModule = ModuleImport { }));
366 SetMainModule(mainModule);
368 GetLastDirectory(GetSourceFile(), mainModuleName);
371 // TEMP: UNTIL WE CAN HAVE PER SOURCE FILE PREPROCESSOR DEFINITIONS...
372 if(GetBuildingEcereCom() &&
373 !(strcmpi(mainModuleName, "instance.ec") && strcmpi(mainModuleName, "BinaryTree.ec") &&
374 strcmpi(mainModuleName, "dataTypes.ec") && strcmpi(mainModuleName, "OldList.ec") &&
375 strcmpi(mainModuleName, "String.ec") && strcmpi(mainModuleName, "BTNode.ec") &&
376 strcmpi(mainModuleName, "Array.ec") && strcmpi(mainModuleName, "AVLTree.ec") &&
377 strcmpi(mainModuleName, "BuiltInContainer.ec") && strcmpi(mainModuleName, "Container.ec") &&
378 strcmpi(mainModuleName, "CustomAVLTree.ec") && strcmpi(mainModuleName, "LinkList.ec") &&
379 strcmpi(mainModuleName, "List.ec") && strcmpi(mainModuleName, "Map.ec") &&
380 strcmpi(mainModuleName, "Mutex.ec")))
381 SetBuildingEcereComModule(true);
382 if(GetBuildingEcereCom() &&
383 !(strcmpi(mainModuleName, "instance.ec") && strcmpi(mainModuleName, "BinaryTree.ec") &&
384 /*strcmpi(mainModuleName, "dataTypes.ec") && strcmpi(mainModuleName, "OldList.ec") &&*/
385 /*strcmpi(mainModuleName, "String.ec") && */strcmpi(mainModuleName, "BTNode.ec") &&
386 strcmpi(mainModuleName, "Mutex.ec") && strcmpi(mainModuleName, "Thread.ec")))
387 //if(GetBuildingEcereCom() && !strcmpi(mainModuleName, "instance.ec"))
388 SetMemoryGuard(false);
391 StripExtension(mainModuleName);
392 module = ImportedModule { name = CopyString(mainModuleName), type = moduleDefinition };
393 ::defines.AddName(module);
396 while(!cppOutput.Eof())
399 int count = cppOutput.Read(junk, 1, 4096);
400 fileInput.Write(junk, 1, count);
402 exitCode = cppOutput.GetExitCode();
405 fileInput.Seek(0, start);
411 // Predeclare all classes
413 char symFile[MAX_FILENAME];
414 char symLocation[MAX_LOCATION];
415 ImportedModule module, next;
417 GetLastDirectory(GetSourceFile(), symFile);
418 ChangeExtension(symFile, "sym", symFile);
420 strcpy(symLocation, GetSymbolsDir());
421 PathCat(symLocation, symFile);
423 // LoadSymbols(symLocation, normalImport, true);
424 LoadSymbols(symLocation, preDeclImport, false);
426 for(module = ::defines.first; module; module = next)
429 if(module.type == moduleDefinition && strcmpi(module.name, mainModuleName))
432 ::defines.Delete(module);
436 if(!GetEcereImported() && !GetBuildingEcereCom())
437 eModule_LoadStrict(privateModule, "ecereCOM", publicAccess /*privateAccess*/);
442 CheckDataRedefinitions();
445 SetCurrentNameSpace(null);
446 SetDefaultNameSpace(null);
447 SetDeclMode(privateAccess);
454 if(/*ast /*&& !parseError*/ /*&& */!exitCode)
456 ProcessDBTableDefinitions();
458 // *** PASS 0 - Register all classes, methods, properties and members ***
459 // *** Build the constructors, destructors, properties as ***
460 // *** class functions ***
461 PrePreProcessClassDefinitions();
462 ComputeModuleClasses(privateModule);
463 PreProcessClassDefinitions();
465 // For classes defined in this module...
466 ComputeModuleClasses(privateModule);
469 // *** PASS 1 - Turn the class functions into functions ***
470 // *** Write the RegisterModule (Register classes) ***
471 ProcessClassDefinitions();
473 // *** PASS 1.5 - Replace members by this.member, figure out types, do type checking / conversions
476 // *** PASS 1.6 - Replace instantiations
477 ProcessInstantiations();
479 // *** PASS 2 - Replace Member Access ***
480 ProcessMemberAccess();
482 // *** PASS 3 - Replace pointers to objects by "Instance *" ***
483 ProcessInstanceDeclarations();
485 strcpy(impFile, GetSymbolsDir());
487 char fileName[MAX_FILENAME];
488 GetLastDirectory(GetSourceFile(), fileName);
489 PathCat(impFile, fileName);
490 ChangeExtension(impFile, "imp", impFile);
493 OutputImports(impFile);
494 // For now use precomp to generate sym file only...
496 if(/*!strcmp(targetExt, "c") && */!this.exitCode)
498 File output = FileOpen(GetOutputFile(), write);
501 output.Printf("#if defined(__GNUC__)\n");
502 output.Printf("typedef long long int64;\n");
503 output.Printf("typedef unsigned long long uint64;\n");
504 output.Printf("#ifndef _WIN32\n");
505 output.Printf("#define __declspec(x)\n");
506 output.Printf("#endif\n");
507 output.Printf("#elif defined(__TINYC__)\n");
508 output.Printf("#include <stdarg.h>\n");
509 output.Printf("#define __builtin_va_list va_list\n");
510 output.Printf("#define __builtin_va_start va_start\n");
511 output.Printf("#define __builtin_va_end va_end\n");
512 output.Printf("#ifdef _WIN32\n");
513 output.Printf("#define strcasecmp stricmp\n");
514 output.Printf("#define strncasecmp strnicmp\n");
515 output.Printf("#define __declspec(x) __attribute__((x))\n");
516 output.Printf("#else\n");
517 output.Printf("#define __declspec(x)\n");
518 output.Printf("#endif\n");
519 output.Printf("typedef long long int64;\n");
520 output.Printf("typedef unsigned long long uint64;\n");
521 output.Printf("#else\n");
522 output.Printf("typedef __int64 int64;\n");
523 output.Printf("typedef unsigned __int64 uint64;\n");
524 output.Printf("#endif\n");
525 output.Printf("#ifdef __BIG_ENDIAN__\n");
526 output.Printf("#define __ENDIAN_PAD(x) (8 - (x))\n");
527 output.Printf("#else\n");
528 output.Printf("#define __ENDIAN_PAD(x) 0\n");
529 output.Printf("#endif\n");
530 if(buildingBootStrap)
532 //output.Printf("#ifdef __MINGW32__\n");
533 //output.Printf("#ifdef _WIN64\n");
535 output.Printf("#if defined(_WIN64) || WORDSIZE == 64\n");
536 output.Printf("typedef unsigned long long int uintptr_t;\n");
537 output.Printf("typedef long long int intptr_t;\n");
538 output.Printf("#else\n");
539 output.Printf("typedef unsigned int uintptr_t;\n");
540 output.Printf("typedef int intptr_t;\n");
541 output.Printf("#endif\n");
543 //output.Printf("#else\n");
544 output.Printf("#include <stdint.h>\n");
545 //output.Printf("#endif\n");
548 // NOTE: If anything is changed up there, the start outputLine must be updated in libec's output.c or Debugging lines will be wrong
551 OutputTree(ast, output);
556 else if(!strcmp(targetExt, "o"))
558 // Compile right away
559 File output = FileOpen(GetOutputFile(), Write);
560 output.Printf("#include <ecereCOM.h>\n\n");
561 OutputTree(ast, output);
566 this.exitCode = exitCode;
574 FreeContext(globalContext);
575 FreeExcludedSymbols(_excludedSymbols);
577 ::defines.Free(FreeModuleDefine);
578 imports.Free(FreeModuleImport);
580 FreeTypeData(privateModule);
582 FreeGlobalData(globalData);
584 delete privateModule;
590 /*for(c = 0; c<argc; c++)
594 SetSymbolsDir(null); // Free symbols dir
598 #if defined(_DEBUG) && defined(__WIN32__)
599 if(exitCode || GetNumWarnings())