Browse Source

Add support for destructors. Closes #73.

pull/100/head
Daniel Grunwald 15 years ago
parent
commit
dbbd5d872b
  1. 2
      ICSharpCode.Decompiler/Ast/AstBuilder.cs
  2. 33
      ICSharpCode.Decompiler/Ast/Transforms/PatternStatementTransform.cs
  3. 5
      ICSharpCode.Decompiler/Tests/UnsafeCode.cs

2
ICSharpCode.Decompiler/Ast/AstBuilder.cs

@ -256,7 +256,7 @@ namespace ICSharpCode.Decompiler.Ast @@ -256,7 +256,7 @@ namespace ICSharpCode.Decompiler.Ast
transform.Run(astCompileUnit);
}
string CleanName(string name)
internal static string CleanName(string name)
{
int pos = name.LastIndexOf('`');
if (pos >= 0)

33
ICSharpCode.Decompiler/Ast/Transforms/PatternStatementTransform.cs

@ -41,6 +41,7 @@ namespace ICSharpCode.Decompiler.Ast.Transforms @@ -41,6 +41,7 @@ namespace ICSharpCode.Decompiler.Ast.Transforms
if (context.Settings.AutomaticEvents)
TransformAutomaticEvents(compilationUnit);
TransformDestructor(compilationUnit);
TransformTryCatchFinally(compilationUnit);
}
@ -654,6 +655,38 @@ namespace ICSharpCode.Decompiler.Ast.Transforms @@ -654,6 +655,38 @@ namespace ICSharpCode.Decompiler.Ast.Transforms
}
#endregion
#region Destructor
static readonly MethodDeclaration destructorPattern = new MethodDeclaration {
Attributes = { new Repeat(new AnyNode()) },
Modifiers = Modifiers.Any,
ReturnType = new PrimitiveType("void"),
Name = "Finalize",
Body = new BlockStatement {
new TryCatchStatement {
TryBlock = new AnyNode("body"),
FinallyBlock = new BlockStatement {
new BaseReferenceExpression().Invoke("Finalize")
}
}
}
};
void TransformDestructor(AstNode compilationUnit)
{
foreach (var methodDef in compilationUnit.Descendants.OfType<MethodDeclaration>()) {
Match m = destructorPattern.Match(methodDef);
if (m != null) {
DestructorDeclaration dd = new DestructorDeclaration();
methodDef.Attributes.MoveTo(dd.Attributes);
dd.Modifiers = methodDef.Modifiers & ~(Modifiers.Protected | Modifiers.Override);
dd.Body = m.Get<BlockStatement>("body").Single().Detach();
dd.Name = AstBuilder.CleanName(context.CurrentType.Name);
methodDef.ReplaceWith(dd);
}
}
}
#endregion
#region Try-Catch-Finally
static readonly TryCatchStatement tryCatchFinallyPattern = new TryCatchStatement {
TryBlock = new BlockStatement {

5
ICSharpCode.Decompiler/Tests/UnsafeCode.cs

@ -74,4 +74,9 @@ public class UnsafeCode @@ -74,4 +74,9 @@ public class UnsafeCode
}
return PointerReferenceExpression((double*)a);
}
unsafe ~UnsafeCode()
{
PassPointerAsRefParameter(NullPointer);
}
}

Loading…
Cancel
Save