wip II
[sdk] / compiler / libec / src / loadSymbols.ec
index 78ae750..421a1c4 100644 (file)
@@ -1,4 +1,5 @@
 import "ecdefs"
+import "lexer"
 
 extern int yychar;
 
@@ -7,7 +8,7 @@ public void SetGlobalData(NameSpace * nameSpace) { globalData = nameSpace; }
 
 OldList dataRedefinitions;
 
-#define MAX_INCLUDE_DEPTH 10
+// #define MAX_INCLUDE_DEPTH 30
 
 extern char sourceFileStack[MAX_INCLUDE_DEPTH][MAX_LOCATION];
 extern int include_stack_ptr;
@@ -33,10 +34,12 @@ bool ecereImported;
 public void SetEcereImported(bool b) { ecereImported = b; } public bool GetEcereImported() { return ecereImported; }
 bool inPreCompiler = false;
 public void SetInPreCompiler(bool b) {inPreCompiler = b; }
+bool inSymbolGen = false;
+public void SetInSymbolGen(bool b) {inSymbolGen = b; }
 OldList * precompDefines;
 public void SetPrecompDefines(OldList * list) { precompDefines = list; }
 
-bool DummyMethod()
+public bool DummyMethod()
 {
    return true;
 }
@@ -79,7 +82,7 @@ static void ReadDataMembers(Class regClass, DataMember member, File f)
             if(member)
             {
                if(!eMember_AddDataMember(member, name, line[0] ? line : 0, 0, 0 /*size *//*type->size*/, memberAccess))
-                  ;//Compiler_Error("Member with same name already exists %s in member %s\n", name, member->name);
+                  ;//Compiler_Error($"Member with same name already exists %s in member %s\n", name, member->name);
             }
             else if(regClass && regClass.type == bitClass)
             {
@@ -91,7 +94,7 @@ static void ReadDataMembers(Class regClass, DataMember member, File f)
             else if(regClass)
             {
                if(!eClass_AddDataMember(regClass, name, line[0] ? line : 0, 0, 0 /*size *//*type->size*/, memberAccess))
-                  ;//Compiler_Error("Member with same name already exists %s in class %s\n", name, regClass.fullName);
+                  ;//Compiler_Error($"Member with same name already exists %s in class %s\n", name, regClass.fullName);
             }
          }
          else if(!strcmp(line, "[Struct]") || !strcmp(line, "[Union]"))
@@ -101,12 +104,12 @@ static void ReadDataMembers(Class regClass, DataMember member, File f)
             if(member)
             {
                if(!eMember_AddMember(member, dataMember))
-                  ;//Compiler_Error("Member with same name already exists %s in member %s\n", name, member->name);
+                  ;//Compiler_Error($"Member with same name already exists %s in member %s\n", name, member->name);
             }
             else if(regClass)
             {
                if(!eClass_AddMember(regClass, dataMember))
-                  ;//Compiler_Error("Member with same name already exists %s in class %s\n", name, regClass.name);
+                  ;//Compiler_Error($"Member with same name already exists %s in class %s\n", name, regClass.name);
             }
          }
       }
@@ -213,7 +216,7 @@ public bool LoadSymbols(char * fileName, ImportType importType, bool loadDllOnly
                            //if(baseSymbol && !baseSymbol->registered)
                            /*if(classType != unitClass && classType != bitClass && classType != enumClass && baseName && !eSystem_FindClass(privateModule, baseName))
                            {
-                              Compiler_Error("Base class %s undefined\n", baseName);
+                              Compiler_Error($"Base class %s undefined\n", baseName);
                               DeclClass(0, name);
                               regClass = null;
                               continue;
@@ -494,13 +497,9 @@ public bool LoadSymbols(char * fileName, ImportType importType, bool loadDllOnly
                                  f.GetLine(line, sizeof(line)); TrimLSpaces(line, line);
                                  if(regClass && strcmp(line, "[None]"))
                                  {
+                                    LexerBackup backup = pushLexer();   // We currently don't have a separate Lexer instance for TU/Type/Expression
                                     Operand op;
                                     Expression exp;
-                                    Location oldLocation = yylloc;
-
-                                    File backFileInput = fileInput;
-                                    declMode = 0;
-                                    resetScanner();
 
                                     exp = ParseExpressionString(line);
                                     if(info)
@@ -511,16 +510,7 @@ public bool LoadSymbols(char * fileName, ImportType importType, bool loadDllOnly
                                     defaultArg.expression.ui64 = op.ui64;
                                     FreeExpression(exp);
 
-                                    // TESTING THIS SCANNER RESUME STUFF
-                                    resetScanner();
-                                    yylloc = oldLocation;
-                                    fileInput = backFileInput;
-                                    if(fileInput)
-                                    {
-                                       fileInput.Seek(yylloc.start.pos, start); 
-                                       resetScannerPos(&yylloc.start);
-                                       yychar = -2;
-                                    }
+                                    popLexer(backup);
                                  }
                                  break;
                               case identifier:
@@ -652,7 +642,7 @@ public bool LoadSymbols(char * fileName, ImportType importType, bool loadDllOnly
                         {
                            // TOFIX:
                            //if(!(data = (GlobalData)nameSpace->functions.FindString(name)))
-                           data = (GlobalData)nameSpace->functions.FindString(name);
+                           data = (GlobalData)nameSpace->functions.FindString(name + start);
                            if(!data)
                            {
                               data = GlobalData
@@ -661,7 +651,7 @@ public bool LoadSymbols(char * fileName, ImportType importType, bool loadDllOnly
                                  dataTypeString = CopyString(line),
                                  module = privateModule
                               };
-                              data.key = (uint)(data.fullName + start);
+                              data.key = (uintptr)(data.fullName + start);
                               // Reusing functions here...
                               nameSpace->functions.Add((BTNode)data);
                            }
@@ -713,11 +703,15 @@ public bool LoadSymbols(char * fileName, ImportType importType, bool loadDllOnly
    }
    else if(importType != comCheckImport)
    {
-      Compiler_Error("Couldn't open %s\n", fileName);
+      char sysFileName[MAX_LOCATION];
+      GetSystemPathBuffer(sysFileName, fileName);
+      Compiler_Error($"Couldn't open %s\n", sysFileName);
    }
    return globalInstance;
 }
 
+Map<String, List<Module> > loadedModules { };
+
 // (Same function as in actual compiler)
 public void ImportModule(char * name, ImportType importType, AccessMode importAccess, bool loadDllOnly)
 {
@@ -776,15 +770,43 @@ public void ImportModule(char * name, ImportType importType, AccessMode importAc
 
             if(ext[0] || !FileExists(symFile))
             {
+               bool skipLoad = false;
+               List<Module> list = null;
+
                char file[MAX_FILENAME];
                strcpy(file, name);
                StripExtension(file);
 
-               loadedModule = eModule_LoadStrict(privateModule, file, importAccess);
-               if(loadedModule)
+               // Load an extra instance of any shared module to ensure freeing up a 
+               // module loaded in another file will not invalidate our objects.
+               if(!inCompiler && !inPreCompiler && !inSymbolGen)
                {
-                  loadedModule.importType = importType;
-                  module.dllOnly = false;    // If this is truly a dll, no need to reload it again...
+                  MapIterator<String, List<Module> > it { map = loadedModules };
+                  if(!it.Index(file, false))
+                  {
+                     Module firstModule = eModule_LoadStrict(__thisModule.application, file, importAccess);
+                     if(firstModule)
+                     {
+                        list = { };
+                        list.Add(firstModule);
+                        loadedModules[file] = list;
+                     }
+                     else
+                        skipLoad = true;
+                  }
+                  else
+                     list = it.data;
+               }
+
+               if(!skipLoad)
+               {
+                  loadedModule = eModule_LoadStrict(privateModule, file, importAccess);
+                  if(loadedModule)
+                  {
+                     loadedModule.importType = importType;
+                     module.dllOnly = false;    // If this is truly a dll, no need to reload it again...
+                     if(list) list.Add(loadedModule);
+                  }
                }
             }
          }
@@ -824,8 +846,13 @@ public void ImportModule(char * name, ImportType importType, AccessMode importAc
             {
                for(dir : sourceDirs)
                {
+                  char configDir[MAX_FILENAME];
                   strcpy(symFile, dir);
-                  PathCat(symFile, "Debug");
+                  // PathCat(symFile, "Debug");
+                  PathCat(symFile, "obj");
+                  sprintf(configDir, "debug.%s", (GetRuntimePlatform() == win32) ? "win32" : "linux");
+                  PathCat(symFile, configDir);
+
                   PathCat(symFile, name);
                   ChangeExtension(symFile, "sym", symFile);
                   if(FileExists(symFile))
@@ -900,7 +927,7 @@ File OpenIncludeFile(char * includeFile)
                break;
          }
       }
-      if(sysIncludeDirs)
+      if(!file && sysIncludeDirs)
       {
          for(includeDir = sysIncludeDirs->first; includeDir; includeDir = includeDir.next)
          {
@@ -962,7 +989,7 @@ public void CheckDataRedefinitions()
       PrintType(type1, type1String, false, true);
       PrintType(type2, type2String, false, true);
       if(strcmp(type1String, type2String))
-         Compiler_Warning("Redefinition of %s (defining as %s, already defined as %s)\n", redefinition.name, type1String, type2String);
+         Compiler_Warning($"Redefinition of %s (defining as %s, already defined as %s)\n", redefinition.name, type1String, type2String);
       FreeType(type1);
       FreeType(type2);
    }