compiler, ecere, ide: Compiler fixes ; Map & struct keys fixes; Support for cross...
[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       int targetBits = GetHostBits();
132
133       SetSymbolsDir("");
134
135       /*for(c = 0; c<this.argc; c++)
136       {
137          char * arg = this.argv[c];
138          int argLen = strlen(arg);
139
140          argv = renew argv char *[argc + 1];
141          argv[argc] = new char[argLen+1];
142          strcpy(argv[argc], arg);
143
144          while(argv[argc][argLen-1] == '\\' && c < this.argc-1)
145          {
146             int len;
147
148             c++;
149             arg = this.argv[c];
150             len = strlen(arg);
151             argv[argc] = renew argv[argc] char[argLen + len + 1];
152
153             argv[argc][argLen-1] = ' ';
154             strcpy(argv[argc] + argLen, arg);
155             argLen += len;
156          }
157          argc++;
158       }*/
159
160       for(c = 1; c<argc; c++)
161       {
162          char * arg = argv[c];
163          if(arg[0] == '-')
164          {
165             if(!strcmp(arg + 1, "m32") || !strcmp(arg + 1, "m64"))
166             {
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;
174             }
175             else if(arg[1] == 'D')
176             {
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);
187             }
188             else if(arg[1] == 'I')
189             {
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;
201             }
202             else if(!strcmp(arg+1, "t"))
203             {
204                if(++c < argc)
205                   targetPlatform = argv[c];
206                else
207                   valid = false;
208             }
209             else if(!strcmp(arg+1, "cpp"))
210             {
211                if(++c < argc)
212                   cppCommand = CopyString(argv[c]);
213                else
214                   valid = false;
215             }
216             else if(!strcmp(arg+1, "o"))
217             {
218                if(!GetOutputFile() && c + 1 < argc)
219                {
220                   SetOutputFile(argv[c+1]);
221                   c++;
222                }
223                else
224                   valid = false;
225             }
226             else if(!strcmp(arg+1, "c"))
227             {
228                if(!GetSourceFile() && c + 1 < argc)
229                {
230                   SetSourceFile(argv[c+1]);
231                   c++;
232                }
233                else
234                   valid = false;
235             }
236             else if(!strcmp(arg+1, "isystem") || !strcmp(arg+1, "isysroot"))
237             {
238                if(c + 1 < argc)
239                {
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] = '"';
248                   arg = argv[++c];
249                   strcpy(cppOptions + cppOptionsLen + argLen + 3, arg); 
250                   cppOptions[newLen-1] = '\"';
251                   cppOptions[newLen] = '\0';
252                   cppOptionsLen = newLen;
253                }
254                else
255                   valid = false;
256             }
257             else if(!strcmp(arg+1, "symbols"))
258             {
259                if(c + 1 < argc)
260                {
261                   SetSymbolsDir(argv[c+1]);
262                   c++;
263                }
264                else
265                   valid = false;
266             }
267             else if(!strcmp(arg+1, "memguard"))
268             {
269                SetMemoryGuard(true);
270             }
271             else if(!strcmp(arg+1, "defaultns"))
272             {
273                if(c + 1 < argc)
274                {
275                   SetDefaultNameSpace(argv[c+1]);
276                   //defaultNameSpaceLen = strlen(defaultNameSpace);
277                   c++;
278                }
279                else
280                   valid = false;
281             }
282             else if(!strcmp(arg+1, "strictns"))
283             {
284                SetStrictNameSpaces(true);
285             }
286             else if(!strcmp(arg+1, "nolinenumbers"))
287             {
288                SetOutputLineNumbers(false);
289             }
290          }
291          else
292             valid = false;
293       }
294       if(valid)
295       {
296          if(!cppCommand)
297             cppCommand = CopyString("gcc");
298          if(!GetSourceFile())
299             valid = false;
300          else if(!GetOutputFile())
301          {
302             strcpy(defaultOutputFile, "");
303             PathCat(defaultOutputFile, GetSourceFile());
304             ChangeExtension(defaultOutputFile, "c", defaultOutputFile);
305             SetOutputFile(defaultOutputFile);
306          }
307       }
308
309       if(!valid)
310          printf($"Syntax:\n   ecc [-t <target platform>] [-cpp <c preprocessor>] [-o <output>] [-symbols <outputdir>] [-I<includedir>]* [-isystem <sysincludedir>]* [-D<definition>]* -c <input>\n");
311       else
312       {
313          DualPipe cppOutput;
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);
323          SetInCompiler(true);
324          SetTargetPlatform(targetPlatform);
325          SetTargetBits(targetBits);
326          SetEchoOn(false);
327
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);
330
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) });
338
339          {
340             GlobalData data { fullName = CopyString("__thisModule"), dataTypeString = CopyString("Module"), module = privateModule };
341             data.key = (uintptr)data.fullName;
342             globalData.functions.Add((BTNode)data);
343          }
344
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)))
348          {
349             char impFile[MAX_LOCATION];
350             ImportedModule module;
351             char mainModuleName[MAX_FILENAME];
352             int exitCode;
353             OldList * ast;
354
355             TempFile fileInput { };
356             SetFileInput(fileInput);
357
358             imports.Add((mainModule = ModuleImport { }));
359             SetMainModule(mainModule);
360
361             GetLastDirectory(GetSourceFile(), mainModuleName);
362
363 #if 0
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);
382 #endif            
383
384             StripExtension(mainModuleName);
385             module = ImportedModule { name = CopyString(mainModuleName), type = moduleDefinition };
386             ::defines.AddName(module);
387
388             resetScanner();
389             while(!cppOutput.Eof())
390             {
391                char junk[4096];
392                int count = cppOutput.Read(junk, 1, 4096);
393                fileInput.Write(junk, 1, count);
394             }
395             exitCode = cppOutput.GetExitCode();
396             delete cppOutput;
397
398             fileInput.Seek(0, start);
399
400    #ifdef _DEBUG
401             // SetYydebug(true);
402    #endif
403
404             // Predeclare all classes
405             {
406                char symFile[MAX_FILENAME];
407                char symLocation[MAX_LOCATION];
408                ImportedModule module, next;
409
410                GetLastDirectory(GetSourceFile(), symFile);
411                ChangeExtension(symFile, "sym", symFile);
412
413                strcpy(symLocation, GetSymbolsDir());
414                PathCat(symLocation, symFile);
415                
416                // LoadSymbols(symLocation, normalImport, true);
417                LoadSymbols(symLocation, preDeclImport, false);
418                
419                for(module = ::defines.first; module; module = next)
420                {
421                   next = module.next;
422                   if(module.type == moduleDefinition && strcmpi(module.name, mainModuleName))
423                   {
424                      delete module.name;
425                      ::defines.Delete(module);
426                   }
427                }
428
429                if(!GetEcereImported() && !GetBuildingEcereCom())
430                   eModule_LoadStrict(privateModule, "ecereCOM", publicAccess /*privateAccess*/);
431             }
432
433             ParseEc();
434
435             CheckDataRedefinitions();
436
437             SetYydebug(false);
438             SetCurrentNameSpace(null);
439             SetDefaultNameSpace(null);
440             SetDeclMode(privateAccess);
441
442             delete fileInput;
443             SetFileInput(null);
444
445             ast = GetAST();
446
447             if(/*ast /*&& !parseError*/ /*&& */!exitCode)
448             {
449                ProcessDBTableDefinitions();
450
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();
457
458                // For classes defined in this module...
459                ComputeModuleClasses(privateModule);
460
461                
462                // *** PASS 1 - Turn the class functions into functions               ***
463                // *** Write the RegisterModule (Register classes)                    ***
464                ProcessClassDefinitions();
465
466                // *** PASS 1.5 - Replace members by this.member, figure out types, do type checking / conversions
467                ComputeDataTypes();
468
469                // *** PASS 1.6 - Replace instantiations
470                ProcessInstantiations();
471
472                // *** PASS 2 - Replace Member Access ***
473                ProcessMemberAccess();
474
475                // *** PASS 3 - Replace pointers to objects by "Instance *" ***
476                ProcessInstanceDeclarations();
477
478                strcpy(impFile, GetSymbolsDir());
479                {
480                   char fileName[MAX_FILENAME];
481                   GetLastDirectory(GetSourceFile(), fileName);
482                   PathCat(impFile, fileName);
483                   ChangeExtension(impFile, "imp", impFile);
484                }
485                if(imports.first)
486                   OutputImports(impFile);
487                // For now use precomp to generate sym file only...
488
489                if(/*!strcmp(targetExt, "c") && */!this.exitCode)
490                {
491                   File output = FileOpen(GetOutputFile(), write);
492                   if(output)
493                   {
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("typedef unsigned long long int uintptr_t;\n");
526                      output.Printf("typedef long long int intptr_t;\n");
527                      output.Printf("#else\n");
528                      output.Printf("typedef unsigned int uintptr_t;\n");
529                      output.Printf("typedef int intptr_t;\n");
530                      output.Printf("#endif\n");
531                      output.Printf("#else\n");
532                      output.Printf("#include <stdint.h>\n");
533                      output.Printf("#endif\n");
534
535                      // NOTE: If anything is changed up there, the start outputLine must be updated in libec's output.c or Debugging lines will be wrong
536
537                      if(ast)
538                         OutputTree(ast, output);
539                      delete output;
540                   }
541                }
542                /*
543                else if(!strcmp(targetExt, "o"))
544                {
545                   // Compile right away
546                   File output = FileOpen(GetOutputFile(), Write);
547                   output.Printf("#include <ecereCOM.h>\n\n");
548                   OutputTree(ast, output);
549                   delete output;
550                }*/
551             }
552             else
553                this.exitCode = exitCode;
554
555             if(ast)
556             {
557                FreeASTTree(ast);
558             }
559          }
560
561          FreeContext(globalContext);
562          FreeExcludedSymbols(_excludedSymbols);
563
564          ::defines.Free(FreeModuleDefine);
565          imports.Free(FreeModuleImport);
566
567          FreeTypeData(privateModule);
568          FreeIncludeFiles();
569          FreeGlobalData(globalData);
570
571          delete privateModule;
572       }
573
574       delete cppCommand;
575       delete cppOptions;
576
577       /*for(c = 0; c<argc; c++)
578          delete argv[c];
579       delete argv;
580       */
581       SetSymbolsDir(null); // Free symbols dir
582
583       OutputIntlStrings();
584
585 #if defined(_DEBUG) && defined(__WIN32__)
586       if(exitCode || GetNumWarnings())
587          getch();
588 #endif
589    }
590 }