compiler/libec: Fixed new memory leaks
[sdk] / compiler / libec / src / freeAst.ec
index a76a0bd..73eee2e 100644 (file)
@@ -7,7 +7,7 @@ void FreeList(OldList list, void (* FreeFunction)(void *))
    if(list != null)
    {
       Item item;
-      while(item = list.first)
+      while((item = list.first))
       {
          list.Remove(item);
          FreeFunction(item);
@@ -27,9 +27,8 @@ public void FreeType(Type type)
          {
             case enumType:
             {
-               NamedLink member, next;
-               if(type.enumName)
-                  delete type.enumName;
+               NamedLink64 member, next;
+               delete type.enumName;
                for(member = type.members.first; member; member = next)
                {
                   next = member.next;
@@ -93,7 +92,7 @@ void FreeSymbol(Symbol symbol)
 
    FreeType(symbol.type);
 
-   while(link = symbol.templatedClasses.first)
+   while((link = symbol.templatedClasses.first))
       symbol.templatedClasses.Delete(link);
 
    delete symbol.string;
@@ -153,7 +152,7 @@ public void FreeExcludedSymbols(OldList excludedSymbols)
 {
    Symbol symbol;
 
-   while(symbol = excludedSymbols.first)
+   while((symbol = excludedSymbols.first))
    {
       excludedSymbols.Remove(symbol);
       FreeSymbol(symbol);
@@ -225,27 +224,27 @@ public void FreeContext(Context context)
    if(context == curContext)
       curContext = globalContext;
 
-   while(symbol = (Symbol)context.types.root)
+   while((symbol = (Symbol)context.types.root))
    {
       context.types.Remove((BTNode)symbol);
       FreeSymbol(symbol);
    }
-   while(symbol = (Symbol)context.classes.root)
+   while((symbol = (Symbol)context.classes.root))
    {
       context.classes.Remove((BTNode)symbol);
       FreeSymbol(symbol);
    }
-   while(symbol = (Symbol)context.symbols.root)
+   while((symbol = (Symbol)context.symbols.root))
    {
       context.symbols.Remove((BTNode)symbol);
       FreeSymbol(symbol);
    }
-   while(symbol = (Symbol)context.structSymbols.root)
+   while((symbol = (Symbol)context.structSymbols.root))
    {
       context.structSymbols.Remove((BTNode)symbol);
       FreeSymbol(symbol);
    }
-   while(symbol = (Symbol)context.templateTypes.root)
+   while((symbol = (Symbol)context.templateTypes.root))
    {
       context.templateTypes.Remove((BTNode)symbol);
       FreeTemplateType((TemplatedType)symbol);
@@ -270,49 +269,96 @@ void FreeSpecifier(Specifier spec)
 {
    if(spec)
    {
-      switch(spec.type)
-      {
-         case nameSpecifier:
-         //case classSpecifier:
-            delete spec.name;
-            if(spec.templateArgs)
-               FreeList(spec.templateArgs, FreeTemplateArgument);
-            break;
-         case extendedSpecifier:
-            if(spec.extDecl)
-               FreeExtDecl(spec.extDecl);
-            break;
-         case enumSpecifier:
-            if(spec.id)
-               FreeIdentifier(spec.id);
-            if(spec.list)
-               FreeList(spec.list, FreeEnumerator);
-            if(spec.definitions)
-               FreeList(spec.definitions, FreeClassDef);
-            break;
-         case structSpecifier:
-         case unionSpecifier:
-            if(spec.id)
-               FreeIdentifier(spec.id);
-            if(spec.definitions)
-               FreeList(spec.definitions, FreeClassDef);
-            if(spec.baseSpecs)
-               FreeList(spec.baseSpecs, FreeSpecifier);
-            if(spec.ctx)
-            {
-               FreeContext(spec.ctx);
-               delete spec.ctx;
-            }
-            break;
-         case subClassSpecifier:
-            if(spec._class)
-               FreeSpecifier(spec._class);
-            break;
-      }
+      FreeSpecifierContents(spec);
       delete spec;
    }
 }
 
+void FreeSpecifierContents(Specifier spec)
+{
+   switch(spec.type)
+   {
+      case nameSpecifier:
+      //case classSpecifier:
+         delete spec.name;
+         if(spec.templateArgs)
+         {
+            FreeList(spec.templateArgs, FreeTemplateArgument);
+            spec.templateArgs = null;
+         }
+         if(spec.nsSpec)
+         {
+            FreeSpecifier(spec.nsSpec);
+            spec.nsSpec = null;
+         }
+         break;
+      case extendedSpecifier:
+         if(spec.extDecl)
+         {
+            FreeExtDecl(spec.extDecl);
+            spec.extDecl = null;
+         }
+         break;
+      case enumSpecifier:
+         if(spec.baseSpecs)
+         {
+            FreeList(spec.baseSpecs, FreeSpecifier);
+            spec.baseSpecs = null;
+         }
+         if(spec.id)
+         {
+            FreeIdentifier(spec.id);
+            spec.id = null;
+         }
+         if(spec.list)
+         {
+            FreeList(spec.list, FreeEnumerator);
+            spec.list = null;
+         }
+         if(spec.definitions)
+         {
+            FreeList(spec.definitions, FreeClassDef);
+            spec.definitions = null;
+         }
+         break;
+      case structSpecifier:
+      case unionSpecifier:
+         if(spec.id)
+         {
+            FreeIdentifier(spec.id);
+            spec.id = null;
+         }
+         if(spec.definitions)
+         {
+            FreeList(spec.definitions, FreeClassDef);
+            spec.definitions = null;
+         }
+         if(spec.baseSpecs)
+         {
+            FreeList(spec.baseSpecs, FreeSpecifier);
+            spec.baseSpecs = null;
+         }
+         if(spec.extDeclStruct)
+         {
+            FreeExtDecl(spec.extDeclStruct);
+            spec.extDeclStruct = null;
+         }
+         if(spec.ctx)
+         {
+            FreeContext(spec.ctx);
+            delete spec.ctx;
+         }
+         break;
+      case subClassSpecifier:
+         if(spec._class)
+         {
+            FreeSpecifier(spec._class);
+            spec._class = null;
+         }
+         break;
+   }
+}
+
 public void FreeIdentifier(Identifier id)
 {
    if(id.badID)
@@ -374,7 +420,10 @@ static void _FreeExpression(Expression exp, bool freePointer)
          break;
       case instanceExp:
          if(exp.instance)
+         {
             FreeInstance(exp.instance);
+            exp.instance = null;
+         }
          break;
       case stringExp:
          delete exp.string;
@@ -394,13 +443,14 @@ static void _FreeExpression(Expression exp, bool freePointer)
       {
          if(exp.index.exp)
             FreeExpression(exp.index.exp);
-         if(exp.index.index) 
+         if(exp.index.index)
             FreeList(exp.index.index, FreeExpression);
          break;
       }
       case callExp:
       {
-         FreeExpression(exp.call.exp);
+         if(exp.call.exp)
+            FreeExpression(exp.call.exp);
          if(exp.call.arguments)
             FreeList(exp.call.arguments, FreeExpression);
          break;
@@ -496,6 +546,30 @@ static void _FreeExpression(Expression exp, bool freePointer)
          if(exp.classData.id)
             FreeIdentifier(exp.classData.id);
             break;
+      case symbolErrorExp:
+         if(exp.identifier)
+            FreeIdentifier(exp.identifier);
+         break;
+      case memoryErrorExp:
+         delete exp.constant;
+         break;
+      case memberPropertyErrorExp:
+      case memberSymbolErrorExp:
+         if(exp.member.exp)
+            FreeExpression(exp.member.exp);
+         if(exp.member.member)
+            FreeIdentifier(exp.member.member);
+         break;
+      case functionCallErrorExp:
+         if(exp.call.exp)
+            FreeExpression(exp.call.exp);
+         if(exp.call.arguments)
+            FreeList(exp.call.arguments, FreeExpression);
+         break;
+      case dereferenceErrorExp:
+      case unknownErrorExp:
+      case noDebuggerErrorExp:
+         break;
    }
    if(freePointer)
    {
@@ -591,10 +665,33 @@ void FreePropertyWatch(PropertyWatch watcher)
    delete watcher;
 }
 
+void FreeAsmField(AsmField field)
+{
+   if(field.expression)
+      FreeExpression(field.expression);
+   if(field.symbolic)
+      FreeIdentifier(field.symbolic);
+   delete field.command;
+   delete field;
+}
+
 void FreeStatement(Statement stmt)
 {
    switch(stmt.type)
    {
+      case asmStmt:
+      {
+         if(stmt.asmStmt.spec)
+            FreeSpecifier(stmt.asmStmt.spec);
+         if(stmt.asmStmt.inputFields)
+            FreeList(stmt.asmStmt.inputFields, FreeAsmField);
+         if(stmt.asmStmt.outputFields)
+            FreeList(stmt.asmStmt.outputFields, FreeAsmField);
+         if(stmt.asmStmt.clobberedFields)
+            FreeList(stmt.asmStmt.clobberedFields, FreeAsmField);
+         delete stmt.asmStmt.statements;
+         break;
+      }
       case labeledStmt:
          if(stmt.labeled.stmt)
             FreeStatement(stmt.labeled.stmt);
@@ -606,7 +703,8 @@ void FreeStatement(Statement stmt)
             FreeStatement(stmt.caseStmt.stmt);
          break;
       case badDeclarationStmt:
-         FreeDeclaration(stmt.decl);
+         if(stmt.decl)
+            FreeDeclaration(stmt.decl);
          break;
       case compoundStmt:
       {
@@ -666,10 +764,10 @@ void FreeStatement(Statement stmt)
       {
          if(stmt.forStmt.init)
             FreeStatement(stmt.forStmt.init);
-         
+
          if(stmt.forStmt.check)
             FreeStatement(stmt.forStmt.check);
-         
+
          if(stmt.forStmt.increment)
             FreeList(stmt.forStmt.increment, FreeExpression);
          if(stmt.forStmt.stmt)
@@ -707,7 +805,7 @@ void FreeStatement(Statement stmt)
          if(stmt._watch.object)
             FreeExpression(stmt._watch.object);
          if(stmt._watch.watches)
-            FreeList(stmt._watch.watches, (stmt.type == watchStmt) ? FreePropertyWatch : FreeIdentifier);
+            FreeList(stmt._watch.watches, (stmt.type == watchStmt) ? (void *)FreePropertyWatch : (void *)FreeIdentifier);
          break;
       }
    }
@@ -719,15 +817,15 @@ void FreeInitializer(Initializer initializer)
    switch(initializer.type)
    {
       case listInitializer:
-      {
          FreeList(initializer.list, FreeInitializer);
          break;
-      }
       case expInitializer:
          if(initializer.exp)
             FreeExpression(initializer.exp);
          break;
    }
+   if(initializer.id)
+      FreeIdentifier(initializer.id);
    delete initializer;
 }
 
@@ -820,7 +918,7 @@ void FreeMembersInit(MembersInit init)
    delete init;
 }
 
-void FreeInstance(Instantiation inst)
+public void FreeInstance(Instantiation inst)
 {
    if(inst.members)
    {
@@ -861,7 +959,7 @@ void FreeInstance(Instantiation inst)
 
    if(inst._class)
       FreeSpecifier(inst._class);
-   
+
    // Free symbol?
    // Free data;
 
@@ -909,7 +1007,7 @@ void FreeProperty(PropertyDef def)
       FreeStatement(def.issetStmt);
    if(def.category)
       FreeExpression(def.category);
-   
+
    /*
    if(def.getFunction)
       FreeFunction(def.getFunction);
@@ -1042,17 +1140,32 @@ void FreeDBTable(DBTableDef table)
 
 void FreeExternal(External external)
 {
+   TopoEdge e;
+   while((e = external.incoming.first))
+   {
+      e.from.outgoing.Remove((IteratorPointer)e);
+      external.incoming.Remove((IteratorPointer)e);
+      delete e;
+   }
+   while((e = external.outgoing.first))
+   {
+      e.to.incoming.Remove((IteratorPointer)e);
+      external.outgoing.Remove((IteratorPointer)e);
+      if(!e.breakable)
+         e.to.nonBreakableIncoming--;
+      delete e;
+   }
    switch(external.type)
    {
-      case functionExternal: 
+      case functionExternal:
          if(external.function)
             FreeFunction(external.function);
          break;
-      case declarationExternal: 
+      case declarationExternal:
          if(external.declaration)
             FreeDeclaration(external.declaration);
          break;
-      case classExternal: 
+      case classExternal:
          if(external._class)
             FreeClass(external._class);
          break;
@@ -1075,7 +1188,7 @@ public void FreeASTTree(OldList ast)
    if(ast != null)
    {
       External external;
-      while(external = ast.first)
+      while((external = ast.first))
       {
          ast.Remove(external);
          FreeExternal(external);
@@ -1101,18 +1214,28 @@ static void FreeDataMember(DataMember parentMember)
    }
 }
 
+static void FreeClassProperties(ClassProperty classProp)
+{
+   if(classProp.left) FreeClassProperties(classProp.left);
+   if(classProp.right) FreeClassProperties(classProp.right);
+   if(classProp.dataType)
+   {
+      FreeType(classProp.dataType);
+      classProp.dataType = null;
+   }
+}
+
 void FreeModuleData(Module module)
 {
    Class _class;
    GlobalFunction function;
-   
+
    // Unload classes
    for(_class = module.classes.first; _class; _class = _class.next)
    {
       DataMember dataMember;
       Method method;
       ClassTemplateParameter param;
-      ClassProperty classProp;
 
       if(_class.dataType)
       {
@@ -1153,6 +1276,8 @@ void FreeModuleData(Module module)
             prop.dataType = null;
          }
       }
+      if(_class.classProperties.first)
+         FreeClassProperties((ClassProperty)_class.classProperties.first);
       for(method = (Method)_class.methods.first; method; method = (Method)((BTNode)method).next)
       {
          if(method.dataType)
@@ -1175,12 +1300,12 @@ void FreeModuleData(Module module)
          }
       }
    }
-   
+
    for(function = module.functions.first; function; function = function.next)
    {
-      if(function.dataType) 
+      if(function.dataType)
          FreeType(function.dataType);
-      if(function.symbol) 
+      if(function.symbol)
          FreeSymbol(function.symbol);
    }
 
@@ -1211,7 +1336,7 @@ void FreeModuleData(Module module)
                list.Remove(list.GetFirst());
                loadedModules.Remove(mapIt.pointer);
                delete list;
-               eModule_Unload(__thisModule, mod);
+               eModule_Unload(__thisModule.application, mod);
             }
             break;
          }