Browse Source

Make using statement work if there are variables declared between the initializer and the try block.

pull/37/head
Daniel Grunwald 15 years ago
parent
commit
8d9076365c
  1. 21
      ICSharpCode.Decompiler/Ast/Transforms/UsingStatementTransform.cs

21
ICSharpCode.Decompiler/Ast/Transforms/UsingStatementTransform.cs

@ -26,6 +26,12 @@ namespace Decompiler.Transforms
).ToVariable() ).ToVariable()
} }
}; };
static readonly AstNode simpleVariableDefinition = new VariableDeclarationStatement {
Type = new AnyNode().ToType(),
Variables = {
new VariableInitializer() // any name but no initializer
}
};
static readonly AstNode usingTryCatchPattern = new TryCatchStatement { static readonly AstNode usingTryCatchPattern = new TryCatchStatement {
TryBlock = new AnyNode("body").ToBlock(), TryBlock = new AnyNode("body").ToBlock(),
FinallyBlock = new BlockStatement { FinallyBlock = new BlockStatement {
@ -58,7 +64,10 @@ namespace Decompiler.Transforms
foreach (AstNode node in compilationUnit.Descendants.ToArray()) { foreach (AstNode node in compilationUnit.Descendants.ToArray()) {
Match m1 = usingVarDeclPattern.Match(node); Match m1 = usingVarDeclPattern.Match(node);
if (m1 == null) continue; if (m1 == null) continue;
Match m2 = usingTryCatchPattern.Match(node.NextSibling); AstNode tryCatch = node.NextSibling;
while (simpleVariableDefinition.Match(tryCatch) != null)
tryCatch = tryCatch.NextSibling;
Match m2 = usingTryCatchPattern.Match(tryCatch);
if (m2 == null) continue; if (m2 == null) continue;
if (m1.Get<VariableInitializer>("variable").Single().Name == m2.Get<IdentifierExpression>("ident").Single().Identifier) { if (m1.Get<VariableInitializer>("variable").Single().Name == m2.Get<IdentifierExpression>("ident").Single().Identifier) {
if (m2.Has("valueType")) { if (m2.Has("valueType")) {
@ -68,12 +77,10 @@ namespace Decompiler.Transforms
continue; continue;
} }
BlockStatement body = m2.Get<BlockStatement>("body").Single(); BlockStatement body = m2.Get<BlockStatement>("body").Single();
body.Remove(); tryCatch.ReplaceWith(
node.NextSibling.Remove(); new UsingStatement {
node.ReplaceWith( ResourceAcquisition = node.Detach(),
varDecl => new UsingStatement { EmbeddedStatement = body.Detach()
ResourceAcquisition = varDecl,
EmbeddedStatement = body
}); });
} }
} }

Loading…
Cancel
Save