|
|
@ -164,24 +164,17 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms |
|
|
|
{ |
|
|
|
{ |
|
|
|
Match m1 = variableAssignPattern.Match(node); |
|
|
|
Match m1 = variableAssignPattern.Match(node); |
|
|
|
if (!m1.Success) return null; |
|
|
|
if (!m1.Success) return null; |
|
|
|
var variableName = m1.Get<IdentifierExpression>("variable").Single().Identifier; |
|
|
|
var variable = m1.Get<IdentifierExpression>("variable").Single().GetILVariable(); |
|
|
|
AstNode next = node.NextSibling; |
|
|
|
AstNode next = node.NextSibling; |
|
|
|
if (next is ForStatement forStatement) { |
|
|
|
if (next is ForStatement forStatement && ForStatementUsesVariable(forStatement, variable)) { |
|
|
|
if ((forStatement.Iterators.FirstOrDefault() is ExpressionStatement stmt |
|
|
|
node.Remove(); |
|
|
|
&& stmt.Expression is AssignmentExpression assign |
|
|
|
next.InsertChildAfter(null, node, ForStatement.InitializerRole); |
|
|
|
&& variableName == assign.Left.ToString()) |
|
|
|
return (ForStatement)next; |
|
|
|
|| (forStatement.Condition is BinaryOperatorExpression cond |
|
|
|
|
|
|
|
&& variableName == cond.Left.ToString())) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
node.Remove(); |
|
|
|
|
|
|
|
forStatement.InsertChildAfter(null, node, ForStatement.InitializerRole); |
|
|
|
|
|
|
|
return forStatement; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
Match m3 = forPattern.Match(next); |
|
|
|
Match m3 = forPattern.Match(next); |
|
|
|
if (!m3.Success) return null; |
|
|
|
if (!m3.Success) return null; |
|
|
|
// ensure the variable in the for pattern is the same as in the declaration
|
|
|
|
// ensure the variable in the for pattern is the same as in the declaration
|
|
|
|
if (variableName != m3.Get<IdentifierExpression>("ident").Single().Identifier) |
|
|
|
if (variable != m3.Get<IdentifierExpression>("ident").Single().GetILVariable()) |
|
|
|
return null; |
|
|
|
return null; |
|
|
|
WhileStatement loop = (WhileStatement)next; |
|
|
|
WhileStatement loop = (WhileStatement)next; |
|
|
|
node.Remove(); |
|
|
|
node.Remove(); |
|
|
@ -197,6 +190,15 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms |
|
|
|
loop.ReplaceWith(forStatement); |
|
|
|
loop.ReplaceWith(forStatement); |
|
|
|
return forStatement; |
|
|
|
return forStatement; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool ForStatementUsesVariable(ForStatement statement, IL.ILVariable variable) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (!statement.Condition.DescendantsAndSelf.OfType<IdentifierExpression>().Any(ie => ie.GetILVariable() == variable)) |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
if (!statement.Iterators.Any(i => i.DescendantsAndSelf.OfType<IdentifierExpression>().Any(ie => ie.GetILVariable() == variable))) |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region foreach
|
|
|
|
#region foreach
|
|
|
|