documentor; compiler/libec: Fixed crashes opening libraries
[sdk] / compiler / libec / src / loadSymbols.ec
index 7202cde..bb53b13 100644 (file)
@@ -1,4 +1,5 @@
 import "ecdefs"
+import "lexer"
 
 extern int yychar;
 
@@ -35,6 +36,8 @@ bool inPreCompiler = false;
 public void SetInPreCompiler(bool b) {inPreCompiler = b; }
 bool inSymbolGen = false;
 public void SetInSymbolGen(bool b) {inSymbolGen = b; }
+bool inDocumentor = false;
+public void SetInDocumentor(bool b) { inDocumentor = b; }
 OldList * precompDefines;
 public void SetPrecompDefines(OldList * list) { precompDefines = list; }
 
@@ -124,7 +127,7 @@ static void ReadDataMembers(Class regClass, DataMember member, File f)
 
 // This should register the stuff only...
 // But also call ImportModule
-public bool LoadSymbols(char * fileName, ImportType importType, bool loadDllOnly)
+public bool LoadSymbols(const char * fileName, ImportType importType, bool loadDllOnly)
 {
    File f = FileOpenBuffered(fileName, read);
    bool globalInstance = false;
@@ -210,7 +213,7 @@ public bool LoadSymbols(char * fileName, ImportType importType, bool loadDllOnly
                         else if(regClass = eSystem_FindClass(privateModule, name), !regClass || regClass.internalDecl || regClass.isRemote)
                         {
                            Symbol existingClass = FindClass(name);
-                           char * baseName = (classType == normalClass && importType == remoteImport && isRemote) ? "DCOMClientObject" : (!strcmp(line, "[None]") ? null : line);
+                           const char * baseName = (classType == normalClass && importType == remoteImport && isRemote) ? "DCOMClientObject" : (!strcmp(line, "[None]") ? null : line);
                            //Symbol baseSymbol = baseName ? FindClass(baseName) : null;
                            //if(baseSymbol && !baseSymbol->registered)
                            /*if(classType != unitClass && classType != bitClass && classType != enumClass && baseName && !eSystem_FindClass(privateModule, baseName))
@@ -294,7 +297,7 @@ public bool LoadSymbols(char * fileName, ImportType importType, bool loadDllOnly
                         for(;;)
                         {
                            char * equal;
-                           
+
                            if(!f.GetLine(line, sizeof(line))) break;
                            TrimLSpaces(line, line);
                            if(!strcmp(line, ".")) break;
@@ -309,7 +312,7 @@ public bool LoadSymbols(char * fileName, ImportType importType, bool loadDllOnly
                                  name[equal - line] = '\0';
                                  TrimLSpaces(name, name);
                                  TrimRSpaces(name, name);
-                                 eEnum_AddFixedValue(regClass, name, atoi(equal + 1));
+                                 eEnum_AddFixedValue(regClass, name, strtoll(equal + 1, null, 0));
                               }
                               else
                               {
@@ -351,7 +354,7 @@ public bool LoadSymbols(char * fileName, ImportType importType, bool loadDllOnly
                            }
                            else
                            {
-                              strcpy(name, line);                              
+                              strcpy(name, line);
                               isVirtual = false;
                               memberAccess = publicAccess;
                            }
@@ -377,7 +380,7 @@ public bool LoadSymbols(char * fileName, ImportType importType, bool loadDllOnly
                                  TrimLSpaces(line, line);
                                  if(regClass)
                                  {
-                                    Property prop = eClass_AddProperty(regClass, conversion ? null : name, line[0] ? line : 0, (void *)setStmt, (void *)getStmt, memberAccess);
+                                    Property prop = eClass_AddProperty(regClass, conversion ? null : name, line[0] ? line : 0, (void *)(uintptr)setStmt, (void *)(uintptr)getStmt, memberAccess);
                                     if(prop)
                                     {
                                        prop.compiled = false;
@@ -431,7 +434,7 @@ public bool LoadSymbols(char * fileName, ImportType importType, bool loadDllOnly
                                  TrimLSpaces(line, line);
                                  if(regClass)
                                  {
-                                    eClass_AddClassProperty(regClass, name, line, (void *)setStmt, (void *)getStmt);
+                                    eClass_AddClassProperty(regClass, name, line, (void *)(uintptr)setStmt, (void *)(uintptr)getStmt);
                                  }
                               }
                               else if(!strcmp(line, "[Set]"))
@@ -496,33 +499,24 @@ 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();
 
+                                    skipErrors = true;
                                     exp = ParseExpressionString(line);
-                                    if(info)
-                                       exp.destType = ProcessTypeString(info, false);
-                                    ProcessExpressionType(exp);
-                                    ComputeExpression(exp);
-                                    op = GetOperand(exp);
-                                    defaultArg.expression.ui64 = op.ui64;
-                                    FreeExpression(exp);
-
-                                    // TESTING THIS SCANNER RESUME STUFF
-                                    resetScanner();
-                                    yylloc = oldLocation;
-                                    fileInput = backFileInput;
-                                    if(fileInput)
+                                    if(exp)
                                     {
-                                       fileInput.Seek(yylloc.start.pos, start); 
-                                       resetScannerPos(&yylloc.start);
-                                       yychar = -2;
+                                       if(info)
+                                          exp.destType = ProcessTypeString(info, false);
+                                       ProcessExpressionType(exp);
+                                       ComputeExpression(exp);
+                                       op = GetOperand(exp);
+                                       defaultArg.expression.ui64 = op.ui64;
+                                       FreeExpression(exp);
                                     }
+                                    skipErrors = false;
+                                    popLexer(backup);
                                  }
                                  break;
                               case identifier:
@@ -543,7 +537,7 @@ public bool LoadSymbols(char * fileName, ImportType importType, bool loadDllOnly
                            if(type == TemplateParameterType::type || type == TemplateParameterType::expression)
                               delete info;
                            if(type == TemplateParameterType::type || type == TemplateParameterType::identifier)
-                              delete defaultArg.dataTypeString;                           
+                              delete (void *)defaultArg.dataTypeString;
                         }
                         if(regClass)
                            eClass_DoneAddingTemplateParameters(regClass);
@@ -567,7 +561,7 @@ public bool LoadSymbols(char * fileName, ImportType importType, bool loadDllOnly
                   if(!f.GetLine(line, sizeof(line))) break;
                   TrimLSpaces(line, line);
                   if(!strcmp(line, ".")) break;
-                  if(!strcmp(line, "[Value]")) 
+                  if(!strcmp(line, "[Value]"))
                   {
                      f.GetLine(line, sizeof(line));
                      TrimLSpaces(line, line);
@@ -588,7 +582,7 @@ public bool LoadSymbols(char * fileName, ImportType importType, bool loadDllOnly
                   if(!f.GetLine(line, sizeof(line))) break;
                   TrimLSpaces(line, line);
                   if(!strcmp(line, ".")) break;
-                  if(!strcmp(line, "[Type]")) 
+                  if(!strcmp(line, "[Type]"))
                   {
                      f.GetLine(line, sizeof(line));
                      TrimLSpaces(line, line);
@@ -609,7 +603,7 @@ public bool LoadSymbols(char * fileName, ImportType importType, bool loadDllOnly
                   if(!f.GetLine(line, sizeof(line))) break;
                   TrimLSpaces(line, line);
                   if(!strcmp(line, ".")) break;
-                  if(!strcmp(line, "[Type]")) 
+                  if(!strcmp(line, "[Type]"))
                   {
                      f.GetLine(line, sizeof(line));
                      TrimLSpaces(line, line);
@@ -647,7 +641,7 @@ public bool LoadSymbols(char * fileName, ImportType importType, bool loadDllOnly
                                  delete spaceName;
                               nameSpace = newSpace;
                               if(name[c] == ':') c++;
-                              start = c+1;         
+                              start = c+1;
                            }
                         }
                         if(c - start)
@@ -725,13 +719,13 @@ public bool LoadSymbols(char * fileName, ImportType importType, bool loadDllOnly
 Map<String, List<Module> > loadedModules { };
 
 // (Same function as in actual compiler)
-public void ImportModule(char * name, ImportType importType, AccessMode importAccess, bool loadDllOnly)
+public void ImportModule(const char * name, ImportType importType, AccessMode importAccess, bool loadDllOnly)
 {
    ImportedModule module = null;
-   char moduleName[MAX_FILENAME];
+   char moduleName[MAX_LOCATION];
 
-   strncpy(moduleName, name, MAX_FILENAME-1);
-   moduleName[MAX_FILENAME-1] = 0;
+   strncpy(moduleName, name, MAX_LOCATION-1);
+   moduleName[MAX_LOCATION-1] = 0;
    StripExtension(moduleName);
 
    for(module = defines->first; module; module = module.next)
@@ -752,7 +746,7 @@ public void ImportModule(char * name, ImportType importType, AccessMode importAc
       PathCat(symFile, name);
       ChangeExtension(symFile, "sym", symFile);
 
-      if(!strcmp(ext, "dll") || !strcmp(ext, "dll") || !ext[0])
+      if(!strcmp(ext, "dll") || !strcmp(ext, "so") || !strcmp(ext, "dylib") || !ext[0])
       {
          if(importType != comCheckImport)
          {
@@ -784,24 +778,30 @@ public void ImportModule(char * name, ImportType importType, AccessMode importAc
             {
                bool skipLoad = false;
                List<Module> list = null;
-
-               char file[MAX_FILENAME];
+               /*
+               char file[MAX_LOCATION];
                strcpy(file, name);
                StripExtension(file);
+               */
 
-               // Load an extra instance of any shared module to ensure freeing up a 
+               // 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)
+
+               // Don't do this for Documentor, because files are loaded with full paths
+               // and won't be recognized as the same libecere that Documentor is actually using,
+               // and since this is loaded from the Documentor app module, it will invalidate classes in use.
+               // We only load one component app at a time for Documentor, so we do not need this trick.
+               if(!inCompiler && !inPreCompiler && !inSymbolGen && !inDocumentor)
                {
                   MapIterator<String, List<Module> > it { map = loadedModules };
-                  if(!it.Index(file, false))
+                  if(!it.Index(name /*file*/, false))
                   {
-                     Module firstModule = eModule_LoadStrict(__thisModule, file, importAccess);
+                     Module firstModule = eModule_LoadStrict(__thisModule.application, name /*file*/, importAccess);
                      if(firstModule)
                      {
                         list = { };
                         list.Add(firstModule);
-                        loadedModules[file] = list;
+                        loadedModules[name /*file*/] = list;
                      }
                      else
                         skipLoad = true;
@@ -812,7 +812,7 @@ public void ImportModule(char * name, ImportType importType, AccessMode importAc
 
                if(!skipLoad)
                {
-                  loadedModule = eModule_LoadStrict(privateModule, file, importAccess);
+                  loadedModule = eModule_LoadStrict(privateModule, name /*file*/, importAccess);
                   if(loadedModule)
                   {
                      loadedModule.importType = importType;
@@ -862,7 +862,7 @@ public void ImportModule(char * name, ImportType importType, AccessMode importAc
                   strcpy(symFile, dir);
                   // PathCat(symFile, "Debug");
                   PathCat(symFile, "obj");
-                  sprintf(configDir, "debug.%s", (GetRuntimePlatform() == win32) ? "win32" : "linux");
+                  sprintf(configDir, "debug.%s", (__runtimePlatform == win32) ? "win32" : (__runtimePlatform == apple) ? "apple" : "linux");
                   PathCat(symFile, configDir);
 
                   PathCat(symFile, name);
@@ -899,7 +899,7 @@ int FindIncludeFileID(char * includeFile)
 int GetIncludeFileID(char * includeFile)
 {
    int found = FindIncludeFileID(includeFile);
-   if(found) 
+   if(found)
       return found;
 
    includes = renew includes char *[numIncludes+1];
@@ -939,7 +939,7 @@ File OpenIncludeFile(char * includeFile)
                break;
          }
       }
-      if(sysIncludeDirs)
+      if(!file && sysIncludeDirs)
       {
          for(includeDir = sysIncludeDirs->first; includeDir; includeDir = includeDir.next)
          {
@@ -972,7 +972,7 @@ public void FreeGlobalData(NameSpace globalDataList)
    {
       FreeGlobalData(ns);
       globalDataList.nameSpaces.Remove((BTNode)ns);
-      delete ns->name;
+      delete (void *)ns->name;
       delete ns;
    }
    for(;(data = (GlobalData)globalDataList.functions.root);)