Skip to content

Commit

Permalink
Handle yield return base class hoisting calls
Browse files Browse the repository at this point in the history
Handle new Mono foreach patterns
  • Loading branch information
Zhentar committed Jul 10, 2017
1 parent f7d28dd commit 999d87f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 16 deletions.
42 changes: 39 additions & 3 deletions ICSharpCode.Decompiler/Ast/Transforms/DelegateConstruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ bool HandleAnonymousMethod(ObjectCreateExpression objectCreateExpression, Expres
// Anonymous methods are defined in the same assembly
MethodDefinition method = methodRef.ResolveWithinSameModule();
if (!IsAnonymousMethod(context, method))
return false;
return false;

// Create AnonymousMethodExpression and prepare parameters
AnonymousMethodExpression ame = new AnonymousMethodExpression();
ame.CopyAnnotationsFrom(objectCreateExpression); // copy ILRanges etc.
Expand Down Expand Up @@ -179,7 +179,7 @@ bool HandleAnonymousMethod(ObjectCreateExpression objectCreateExpression, Expres
// Replace all occurrences of 'this' in the method body with the delegate's target:
foreach (AstNode node in body.Descendants) {
if (node is ThisReferenceExpression)
node.ReplaceWith(target.Clone());
node.ReplaceWith(target.Clone());
}
Expression replacement;
if (isLambda) {
Expand Down Expand Up @@ -227,6 +227,42 @@ public override object VisitInvocationExpression(InvocationExpression invocation
return converted.AcceptVisitor(this, data);
}
}
var definition = invocationExpression.Annotation<MethodDefinition>();
if(definition != null)
{
//Handle hoisted base calls from yield return compiler generated class (can be generated by both Mono and Roslyn)
if (definition.DeclaringType == context.CurrentType && definition.IsCompilerGenerated())
{
DecompilerContext subContext = context.Clone();
subContext.CurrentMethod = definition;
subContext.CurrentMethodIsAsync = false;
subContext.ReservedVariableNames.AddRange(currentlyUsedVariableNames);
var parameters = AstBuilder.MakeParameters(definition, isLambda: true);
BlockStatement body = AstMethodBodyBuilder.CreateMethodBody(definition, subContext, parameters);
TransformationPipeline.RunTransformationsUntil(body, v => v is DelegateConstruction, subContext);

if (body.Statements.Count == 1)
{
var statement = body.Statements.Single();
Expression expr = null;
if (statement is ReturnStatement)
{
expr = ((ReturnStatement)statement).Expression;
}
else if (statement is ExpressionStatement)
{
expr = ((ExpressionStatement)statement).Expression;
}
if (expr != null)
{
expr.Remove();
invocationExpression.ReplaceWith(expr);
return expr.AcceptVisitor(this, data);
}
}

}
}
return base.VisitInvocationExpression(invocationExpression, data);
}

Expand Down
38 changes: 25 additions & 13 deletions ICSharpCode.Decompiler/Ast/Transforms/PatternStatementTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,9 @@ bool HasAssignment(AstNode root, string variableName)
new AssignmentExpression {
Left = new IdentifierExpression(Pattern.AnyString).WithName("itemVariable"),
Operator = AssignmentOperatorType.Assign,
Right = new IdentifierExpressionBackreference("enumeratorVariable").ToExpression().Member("Current")
Right =new Choice() { new IdentifierExpressionBackreference("enumeratorVariable").ToExpression().Member("Current"),
new IdentifierExpressionBackreference("enumeratorVariable").ToExpression().Member("Current").CastTo(new AnyNode()) //The cast matches Mono non-generic foreaches (which also don't match the non-generic foreach pattern)
}
},
new Repeat(new AnyNode("statement")).ToStatement()
}
Expand All @@ -413,6 +415,7 @@ bool HasAssignment(AstNode root, string variableName)

public ForeachStatement TransformForeach(UsingStatement node)
{
//return null;
Match m = genericForeachPattern.Match(node);
if (!m.Success)
return null;
Expand All @@ -427,17 +430,26 @@ public ForeachStatement TransformForeach(UsingStatement node)
// Find the declaration of the item variable:
// Because we look only outside the loop, we won't make the mistake of moving a captured variable across the loop boundary
VariableDeclarationStatement itemVarDecl = FindVariableDeclaration(loop, itemVar.Identifier);
if (itemVarDecl == null || !(itemVarDecl.Parent is BlockStatement))
return null;

// Now verify that we can move the variable declaration in front of the loop:
Statement declarationPoint;
CanMoveVariableDeclarationIntoStatement(itemVarDecl, loop, out declarationPoint);
// We ignore the return value because we don't care whether we can move the variable into the loop
// (that is possible only with non-captured variables).
// We just care that we can move it in front of the loop:
if (declarationPoint != loop)
return null;
if (itemVarDecl != null)
{
if (!(itemVarDecl.Parent is BlockStatement))
return null;

// Now verify that we can move the variable declaration in front of the loop:
Statement declarationPoint;
CanMoveVariableDeclarationIntoStatement(itemVarDecl, loop, out declarationPoint);
// We ignore the return value because we don't care whether we can move the variable into the loop
// (that is possible only with non-captured variables).
// We just care that we can move it in front of the loop:
if (declarationPoint != loop)
return null;
}
else //It's okay for the variable declaration to be inside the loop (there should probably be some verification that it's at the beginning of it, though)
{
itemVarDecl = FindVariableDeclaration(itemVar, itemVar.Identifier);
if (itemVarDecl == null)
return null;
}

// Make sure that the enumerator variable is not used inside the body
var enumeratorId = Identifier.Create(enumeratorVar.Name);
Expand All @@ -447,7 +459,7 @@ public ForeachStatement TransformForeach(UsingStatement node)
}

BlockStatement newBody = new BlockStatement();
foreach (Statement stmt in m.Get<Statement>("variablesInsideLoop"))
foreach (Statement stmt in m.Get<Statement>("variablesInsideLoop").Except(new[] {itemVarDecl}))
newBody.Add(stmt.Detach());
foreach (Statement stmt in m.Get<Statement>("statement"))
newBody.Add(stmt.Detach());
Expand Down

0 comments on commit 999d87f

Please sign in to comment.