compiler/ide: Various tweaks to buffer sizes for holding command arguments (Buffer...
[sdk] / compiler / ecc / ecc.ec
1 #ifdef ECERE_STATIC
2 import static "ecere"
3 import static "ec"
4 #else
5 import "ecere"
6 import "ec"
7 #endif
8
9 #include <stdarg.h>
10
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
17 {
18    classes.CompareKey = (void *)BinaryTree::CompareString;
19    defines.CompareKey = (void *)BinaryTree::CompareString;
20    functions.CompareKey = (void *)BinaryTree::CompareString;
21    nameSpaces.CompareKey = (void *)BinaryTree::CompareString;
22 };
23 static OldList _precompDefines;
24
25 static void OutputImports(char * fileName)
26 {
27    File f = FileOpen(fileName, write);
28    if(f)
29    {
30       if(imports.first)
31       {
32          ModuleImport module;
33          f.Printf("[Imported Modules]\n");
34          for(module = imports.first; module; module = module.next)
35          {
36             ClassImport _class;
37             FunctionImport function;
38
39             if(module.name)
40                f.Printf("   %s\n", module.name);
41             else
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");
47
48             if(module.importAccess == privateAccess)
49                f.Printf("      [Private]\n");
50             else
51                f.Printf("      [Public]\n");
52
53             if(module.classes.first)
54             {
55                f.Printf("      [Imported Classes]\n");
56                for(_class = module.classes.first; _class; _class = _class.next)
57                {
58                   f.Printf("         %s\n", _class.name);
59                   if(_class.itself)
60                   {
61                      f.Printf("            [Instantiated]\n");
62                   }
63                   if(_class.isRemote)
64                   {
65                      f.Printf("            [Remote]\n");
66                   }
67
68                   if(_class.methods.first)
69                   {
70                      MethodImport method;
71                      f.Printf("            [Imported Methods]\n");
72                      for(method = _class.methods.first; method; method = method.next)
73                      {
74                         f.Printf("               %s\n", method.name);
75                         if(method.isVirtual)
76                            f.Printf("                  [Virtual]\n");
77
78                      }
79                      f.Printf("               .\n");
80                   }
81
82                   if(_class.properties.first)
83                   {
84                      PropertyImport prop;
85                      f.Printf("            [Imported Properties]\n");
86                      for(prop = _class.properties.first; prop; prop = prop.next)
87                      {
88                         f.Printf("               %s\n", prop.name);
89                         if(prop.isVirtual)
90                            f.Printf("                  [Virtual]\n");
91                         if(prop.hasSet)
92                            f.Printf("                  [Set]\n");
93                         if(prop.hasGet)
94                            f.Printf("                  [Get]\n");
95                      }
96                      f.Printf("               .\n");
97                   }
98                }
99                f.Printf("        .\n");
100             }
101             if(module.functions.first)
102             {
103                f.Printf("      [Imported Functions]\n");
104                for(function = module.functions.first; function; function = function.next)
105                {
106                   f.Printf("         %s\n", function.name);
107                }
108                f.Printf("        .\n");
109             }
110          }
111          f.Printf("   .\n");
112       }
113    }
114    delete f;
115 }
116
117 class CompilerApp : Application
118 {
119    void Main()
120    {
121       char * cppCommand = null;
122       char * cppOptions = null;
123       int cppOptionsLen = 0;
124       /*char ** argv = null;
125       int argc = 0;*/
126       int c;
127       bool valid = true;
128       char defaultOutputFile[MAX_LOCATION];
129
130       Platform targetPlatform = GetRuntimePlatform();
131
132       SetSymbolsDir("");
133
134       /*for(c = 0; c<this.argc; c++)
135       {
136          char * arg = this.argv[c];
137          int argLen = strlen(arg);
138
139          argv = renew argv char *[argc + 1];
140          argv[argc] = new char[argLen+1];
141          strcpy(argv[argc], arg);
142
143          while(argv[argc][argLen-1] == '\\' && c < this.argc-1)
144          {
145             int len;
146
147             c++;
148             arg = this.argv[c];
149             len = strlen(arg);
150             argv[argc] = renew argv[argc] char[argLen + len + 1];
151
152             argv[argc][argLen-1] = ' ';
153             strcpy(argv[argc] + argLen, arg);
154             argLen += len;
155          }
156          argc++;
157       }*/
158
159       for(c = 1; c<argc; c++)
160       {
161          char * arg = argv[c];
162          if(arg[0] == '-')
163          {
164             if(!strcmp(arg + 1, "m32"))
165             {
166                int argLen = strlen(arg);
167                int newLen = cppOptionsLen + 1 + argLen;
168                cppOptions = renew cppOptions char[newLen + 1];
169                cppOptions[cppOptionsLen] = ' ';
170                strcpy(cppOptions + cppOptionsLen + 1, arg); 
171                cppOptionsLen = newLen;
172             }
173             else if(arg[1] == 'D')
174             {
175                int argLen = strlen(arg);
176                int newLen = cppOptionsLen + 1 + argLen;
177                cppOptions = renew cppOptions char[newLen + 1];
178                cppOptions[cppOptionsLen] = ' ';
179                strcpy(cppOptions + cppOptionsLen + 1, arg); 
180                cppOptionsLen = newLen;
181                if(!strcmp(arg, "-DBUILDING_ECERE_COM"))
182                   SetBuildingEcereCom(true);
183                else if(!strcmp(arg, "-DECERE_COM_MODULE"))
184                   SetBuildingEcereComModule(true);
185             }
186             else if(arg[1] == 'I')
187             {
188                int argLen = strlen(arg);
189                int newLen = cppOptionsLen + argLen + 3;
190                cppOptions = renew cppOptions char[newLen + 1];
191                cppOptions[cppOptionsLen] = ' ';
192                cppOptions[cppOptionsLen+1] = '-';
193                cppOptions[cppOptionsLen+2] = 'I';
194                cppOptions[cppOptionsLen+3] = '"';
195                strcpy(cppOptions + cppOptionsLen + 4, arg+2); 
196                cppOptions[newLen-1] = '\"';
197                cppOptions[newLen] = '\0';
198                cppOptionsLen = newLen;
199             }
200             else if(!strcmp(arg+1, "t"))
201             {
202                if(++c < argc)
203                   targetPlatform = argv[c];
204                else
205                   valid = false;
206             }
207             else if(!strcmp(arg+1, "cpp"))
208             {
209                if(++c < argc)
210                   cppCommand = CopyString(argv[c]);
211                else
212                   valid = false;
213             }
214             else if(!strcmp(arg+1, "o"))
215             {
216                if(!GetOutputFile() && c + 1 < argc)
217                {
218                   SetOutputFile(argv[c+1]);
219                   c++;
220                }
221                else
222                   valid = false;
223             }
224             else if(!strcmp(arg+1, "c"))
225             {
226                if(!GetSourceFile() && c + 1 < argc)
227                {
228                   SetSourceFile(argv[c+1]);
229                   c++;
230                }
231                else
232                   valid = false;
233             }
234             else if(!strcmp(arg+1, "isystem") || !strcmp(arg+1, "isysroot"))
235             {
236                if(c + 1 < argc)
237                {
238                   int argLen = strlen(arg);
239                   int arg1Len = strlen(argv[c+1]);
240                   int newLen = cppOptionsLen + argLen + arg1Len + 4;
241                   cppOptions = renew cppOptions char[newLen + 1];
242                   cppOptions[cppOptionsLen] = ' ';
243                   strcpy(cppOptions + cppOptionsLen + 1, arg); 
244                   cppOptions[cppOptionsLen+argLen+1] = ' ';
245                   cppOptions[cppOptionsLen+argLen+2] = '"';
246                   arg = argv[++c];
247                   strcpy(cppOptions + cppOptionsLen + argLen + 3, arg); 
248                   cppOptions[newLen-1] = '\"';
249                   cppOptions[newLen] = '\0';
250                   cppOptionsLen = newLen;
251                }
252                else
253                   valid = false;
254             }
255             else if(!strcmp(arg+1, "symbols"))
256             {
257                if(c + 1 < argc)
258                {
259                   SetSymbolsDir(argv[c+1]);
260                   c++;
261                }
262                else
263                   valid = false;
264             }
265             else if(!strcmp(arg+1, "memguard"))
266             {
267                SetMemoryGuard(true);
268             }
269             else if(!strcmp(arg+1, "defaultns"))
270             {
271                if(c + 1 < argc)
272                {
273                   SetDefaultNameSpace(argv[c+1]);
274                   //defaultNameSpaceLen = strlen(defaultNameSpace);
275                   c++;
276                }
277                else
278                   valid = false;
279             }
280             else if(!strcmp(arg+1, "strictns"))
281             {
282                SetStrictNameSpaces(true);
283             }
284             else if(!strcmp(arg+1, "nolinenumbers"))
285             {
286                SetOutputLineNumbers(false);
287             }
288          }
289          else
290             valid = false;
291       }
292       if(valid)
293       {
294          if(!cppCommand)
295             cppCommand = CopyString("gcc");
296          if(!GetSourceFile())
297             valid = false;
298          else if(!GetOutputFile())
299          {
300             strcpy(defaultOutputFile, "");
301             PathCat(defaultOutputFile, GetSourceFile());
302             ChangeExtension(defaultOutputFile, "c", defaultOutputFile);
303             SetOutputFile(defaultOutputFile);
304          }
305       }
306
307       if(!valid)
308          printf($"Syntax:\n   ecc [-t <target platform>] [-cpp <c preprocessor>] [-o <output>] [-symbols <outputdir>] [-I<includedir>]* [-isystem <sysincludedir>]* [-D<definition>]* -c <input>\n");
309       else
310       {
311          DualPipe cppOutput;
312          // TODO: Improve this
313          char command[MAX_F_STRING*3];
314
315          SetGlobalData(&globalData);
316          SetExcludedSymbols(&_excludedSymbols);
317          SetGlobalContext(globalContext);
318          SetCurrentContext(globalContext);
319          SetTopContext(globalContext);
320          SetDefines(&::defines);
321          SetImports(&imports);
322          SetInCompiler(true);
323          SetTargetPlatform(targetPlatform);
324          SetEchoOn(false);
325
326          privateModule = (Module)__ecere_COM_Initialize(true, 1, null);
327          SetPrivateModule(privateModule);
328
329          globalContext.types.Add((BTNode)Symbol { string = CopyString("uint"), type = ProcessTypeString("unsigned int", false) });
330          globalContext.types.Add((BTNode)Symbol { string = CopyString("uint64"), type = ProcessTypeString("unsigned int64", false) });
331          globalContext.types.Add((BTNode)Symbol { string = CopyString("uint32"), type = ProcessTypeString("unsigned int", false) });
332          globalContext.types.Add((BTNode)Symbol { string = CopyString("uint16"), type = ProcessTypeString("unsigned short", false) });
333          globalContext.types.Add((BTNode)Symbol { string = CopyString("byte"), type = ProcessTypeString("unsigned char", false) });
334
335          {
336             GlobalData data { fullName = CopyString("__thisModule"), dataTypeString = CopyString("Module"), module = privateModule };
337             data.key = (uint)data.fullName;
338             globalData.functions.Add((BTNode)data);
339          }
340
341          sprintf(command, "%s%s -x c -E \"%s\"", cppCommand, cppOptions ? cppOptions : "", GetSourceFile());
342
343          if((cppOutput = DualPipeOpen({ output = true }, command)))
344          {
345             char impFile[MAX_LOCATION];
346             ImportedModule module;
347             char mainModuleName[MAX_FILENAME];
348             int exitCode;
349             OldList * ast;
350
351             TempFile fileInput { };
352             SetFileInput(fileInput);
353
354             imports.Add((mainModule = ModuleImport { }));
355             SetMainModule(mainModule);
356
357             GetLastDirectory(GetSourceFile(), mainModuleName);
358
359 #if 0
360             // TEMP: UNTIL WE CAN HAVE PER SOURCE FILE PREPROCESSOR DEFINITIONS...
361             if(GetBuildingEcereCom() && 
362                !(strcmpi(mainModuleName, "instance.ec") && strcmpi(mainModuleName, "BinaryTree.ec") &&
363                strcmpi(mainModuleName, "dataTypes.ec") && strcmpi(mainModuleName, "OldList.ec") &&
364                strcmpi(mainModuleName, "String.ec") && strcmpi(mainModuleName, "BTNode.ec") &&
365                strcmpi(mainModuleName, "Array.ec") && strcmpi(mainModuleName, "AVLTree.ec") &&
366                strcmpi(mainModuleName, "BuiltInContainer.ec") && strcmpi(mainModuleName, "Container.ec") &&
367                strcmpi(mainModuleName, "CustomAVLTree.ec") && strcmpi(mainModuleName, "LinkList.ec") &&
368                strcmpi(mainModuleName, "List.ec") && strcmpi(mainModuleName, "Map.ec") &&
369                strcmpi(mainModuleName, "Mutex.ec")))
370                SetBuildingEcereComModule(true);
371             if(GetBuildingEcereCom() && 
372                !(strcmpi(mainModuleName, "instance.ec") && strcmpi(mainModuleName, "BinaryTree.ec") &&
373                /*strcmpi(mainModuleName, "dataTypes.ec") && strcmpi(mainModuleName, "OldList.ec") &&*/
374                /*strcmpi(mainModuleName, "String.ec") && */strcmpi(mainModuleName, "BTNode.ec") &&
375                strcmpi(mainModuleName, "Mutex.ec") && strcmpi(mainModuleName, "Thread.ec")))
376             //if(GetBuildingEcereCom() && !strcmpi(mainModuleName, "instance.ec"))
377                SetMemoryGuard(false);
378 #endif            
379
380             StripExtension(mainModuleName);
381             module = ImportedModule { name = CopyString(mainModuleName), type = moduleDefinition };
382             ::defines.AddName(module);
383
384             resetScanner();
385             while(!cppOutput.Eof())
386             {
387                char junk[4096];
388                int count = cppOutput.Read(junk, 1, 4096);
389                fileInput.Write(junk, 1, count);
390             }
391             exitCode = cppOutput.GetExitCode();
392             delete cppOutput;
393
394             fileInput.Seek(0, start);
395
396    #ifdef _DEBUG
397             // SetYydebug(true);
398    #endif
399
400             // Predeclare all classes
401             {
402                char symFile[MAX_FILENAME];
403                char symLocation[MAX_LOCATION];
404                ImportedModule module, next;
405
406                GetLastDirectory(GetSourceFile(), symFile);
407                ChangeExtension(symFile, "sym", symFile);
408
409                strcpy(symLocation, GetSymbolsDir());
410                PathCat(symLocation, symFile);
411                
412                // LoadSymbols(symLocation, normalImport, true);
413                LoadSymbols(symLocation, preDeclImport, false);
414                
415                for(module = ::defines.first; module; module = next)
416                {
417                   next = module.next;
418                   if(module.type == moduleDefinition && strcmpi(module.name, mainModuleName))
419                   {
420                      delete module.name;
421                      ::defines.Delete(module);
422                   }
423                }
424
425                if(!GetEcereImported() && !GetBuildingEcereCom())
426                   eModule_LoadStrict(privateModule, "ecereCOM", publicAccess /*privateAccess*/);
427             }
428
429             ParseEc();
430
431             CheckDataRedefinitions();
432
433             SetYydebug(false);
434             SetCurrentNameSpace(null);
435             SetDefaultNameSpace(null);
436             SetDeclMode(privateAccess);
437
438             delete fileInput;
439             SetFileInput(null);
440
441             ast = GetAST();
442
443             if(/*ast /*&& !parseError*/ /*&& */!exitCode)
444             {
445                ProcessDBTableDefinitions();
446
447                // *** PASS 0 - Register all classes, methods, properties and members ***
448                // ***          Build the constructors, destructors, properties as    ***
449                // ***          class functions                                       ***
450                PrePreProcessClassDefinitions();
451                ComputeModuleClasses(privateModule);
452                PreProcessClassDefinitions();
453
454                // For classes defined in this module...
455                ComputeModuleClasses(privateModule);
456
457                
458                // *** PASS 1 - Turn the class functions into functions               ***
459                // *** Write the RegisterModule (Register classes)                    ***
460                ProcessClassDefinitions();
461
462                // *** PASS 1.5 - Replace members by this.member, figure out types, do type checking / conversions
463                ComputeDataTypes();
464
465                // *** PASS 1.6 - Replace instantiations
466                ProcessInstantiations();
467
468                // *** PASS 2 - Replace Member Access ***
469                ProcessMemberAccess();
470
471                // *** PASS 3 - Replace pointers to objects by "Instance *" ***
472                ProcessInstanceDeclarations();
473
474                strcpy(impFile, GetSymbolsDir());
475                {
476                   char fileName[MAX_FILENAME];
477                   GetLastDirectory(GetSourceFile(), fileName);
478                   PathCat(impFile, fileName);
479                   ChangeExtension(impFile, "imp", impFile);
480                }
481                if(imports.first)
482                   OutputImports(impFile);
483                // For now use precomp to generate sym file only...
484
485                if(/*!strcmp(targetExt, "c") && */!this.exitCode)
486                {
487                   File output = FileOpen(GetOutputFile(), write);
488                   if(output)
489                   {
490                      //output.Printf("#include <ecereCOM.h>\n\n");
491
492                      // Temporary patch, fix with defines or something...
493                      /*
494                      if(!strstr(GetSourceFile(), "instance.ec"))
495                      {
496                         output.Printf("#if defined(__GNUC__) && defined(__WIN32__)\n");
497                         output.Printf("#include <x87inline.h>\n");
498                         output.Printf("#endif\n");
499                      }
500                      */
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("#else\n");
505                         output.Printf("typedef __int64 int64;\n");
506                         output.Printf("typedef unsigned __int64 uint64;\n");
507                      output.Printf("#endif\n");*/
508                      output.Printf("#if defined(__GNUC__)\n");
509                         output.Printf("typedef long long int64;\n");
510                         output.Printf("typedef unsigned long long uint64;\n");
511                      output.Printf("#elif defined(__TINYC__)\n");
512                         output.Printf("#include <stdarg.h>\n");
513                         output.Printf("#define __builtin_va_list va_list\n");
514                         output.Printf("#define __builtin_va_start va_start\n");
515                         output.Printf("#define __builtin_va_end va_end\n");
516                         output.Printf("#ifdef _WIN32\n");
517                            output.Printf("#define strcasecmp stricmp\n");
518                            output.Printf("#define strncasecmp strnicmp\n");
519                            output.Printf("#define __declspec(x) __attribute__((x))\n");
520                         output.Printf("#endif\n");
521                         output.Printf("typedef long long int64;\n");
522                         output.Printf("typedef unsigned long long uint64;\n");
523                      output.Printf("#else\n");
524                         output.Printf("typedef __int64 int64;\n");
525                         output.Printf("typedef unsigned __int64 uint64;\n");
526                      output.Printf("#endif\n");
527                      /*output.Printf("#if defined(__GNUC__) || defined(__TINYC__)\n");
528                         output.Printf("typedef long long int64;\n");
529                         output.Printf("typedef unsigned long long uint64;\n");
530                      output.Printf("#elif defined(__TINYC__)\n");
531                      output.Printf("#else\n");
532                         output.Printf("typedef __int64 int64;\n");
533                         output.Printf("typedef unsigned __int64 uint64;\n");
534                      output.Printf("#endif\n");*/
535                      output.Printf("#ifdef __BIG_ENDIAN__\n");
536                         output.Printf("#define __ENDIAN_PAD(x) (8 - (x))\n");
537                      output.Printf("#else\n");
538                         output.Printf("#define __ENDIAN_PAD(x) 0\n");
539                      output.Printf("#endif\n");
540
541                      if(ast)
542                         OutputTree(ast, output);
543                      delete output;
544                   }
545                }
546                /*
547                else if(!strcmp(targetExt, "o"))
548                {
549                   // Compile right away
550                   File output = FileOpen(GetOutputFile(), Write);
551                   output.Printf("#include <ecereCOM.h>\n\n");
552                   OutputTree(ast, output);
553                   delete output;
554                }*/
555             }
556             else
557                this.exitCode = exitCode;
558
559             if(ast)
560             {
561                FreeASTTree(ast);
562             }
563          }
564
565          FreeContext(globalContext);
566          FreeExcludedSymbols(_excludedSymbols);
567
568          ::defines.Free(FreeModuleDefine);
569          imports.Free(FreeModuleImport);
570
571          FreeTypeData(privateModule);
572          FreeIncludeFiles();
573          FreeGlobalData(globalData);
574
575          delete privateModule;
576       }
577
578       delete cppCommand;
579       delete cppOptions;
580
581       /*for(c = 0; c<argc; c++)
582          delete argv[c];
583       delete argv;
584       */
585       SetSymbolsDir(null); // Free symbols dir
586
587       OutputIntlStrings();
588
589 #if defined(_DEBUG) && defined(__WIN32__)
590       if(exitCode || GetNumWarnings())
591          getch();
592 #endif
593    }
594 }