Browse Source

Fix #1618: Unwrap in-arguments, when converting method calls to operators.

pull/1633/head
Siegfried Pammer 6 years ago
parent
commit
6ecd99f893
  1. 7
      ICSharpCode.Decompiler/CSharp/Syntax/SyntaxExtensions.cs
  2. 16
      ICSharpCode.Decompiler/CSharp/Transforms/ReplaceMethodCallsWithOperators.cs

7
ICSharpCode.Decompiler/CSharp/Syntax/SyntaxExtensions.cs

@ -75,5 +75,12 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax @@ -75,5 +75,12 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
node.Remove();
return node;
}
public static Expression UnwrapInDirectionExpression(this Expression expr)
{
if (!(expr is DirectionExpression dir && dir.FieldDirection == FieldDirection.In))
return expr;
return dir.Expression.Detach();
}
}
}

16
ICSharpCode.Decompiler/CSharp/Transforms/ReplaceMethodCallsWithOperators.cs

@ -60,7 +60,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -60,7 +60,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
invocationExpression.Arguments.Clear(); // detach arguments from invocationExpression
Expression expr = arguments[0];
for (int i = 1; i < arguments.Length; i++) {
expr = new BinaryOperatorExpression(expr, BinaryOperatorType.Add, arguments[i]);
expr = new BinaryOperatorExpression(expr, BinaryOperatorType.Add, arguments[i].UnwrapInDirectionExpression());
}
expr.CopyAnnotationsFrom(invocationExpression);
invocationExpression.ReplaceWith(expr);
@ -116,7 +116,11 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -116,7 +116,11 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
if (bop != null && arguments.Length == 2) {
invocationExpression.Arguments.Clear(); // detach arguments from invocationExpression
invocationExpression.ReplaceWith(
new BinaryOperatorExpression(arguments[0], bop.Value, arguments[1]).CopyAnnotationsFrom(invocationExpression)
new BinaryOperatorExpression(
arguments[0].UnwrapInDirectionExpression(),
bop.Value,
arguments[1].UnwrapInDirectionExpression()
).CopyAnnotationsFrom(invocationExpression)
);
return;
}
@ -130,7 +134,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -130,7 +134,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
// so reverse that optimization here:
invocationExpression.ReplaceWith(
new BinaryOperatorExpression(
arguments[0].Detach(),
arguments[0].UnwrapInDirectionExpression().Detach(),
(uop == UnaryOperatorType.Increment ? BinaryOperatorType.Add : BinaryOperatorType.Subtract),
new PrimitiveExpression(1m)
).CopyAnnotationsFrom(invocationExpression)
@ -140,20 +144,20 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -140,20 +144,20 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
}
arguments[0].Remove(); // detach argument
invocationExpression.ReplaceWith(
new UnaryOperatorExpression(uop.Value, arguments[0]).CopyAnnotationsFrom(invocationExpression)
new UnaryOperatorExpression(uop.Value, arguments[0].UnwrapInDirectionExpression()).CopyAnnotationsFrom(invocationExpression)
);
return;
}
if (method.Name == "op_Explicit" && arguments.Length == 1) {
arguments[0].Remove(); // detach argument
invocationExpression.ReplaceWith(
new CastExpression(context.TypeSystemAstBuilder.ConvertType(method.ReturnType), arguments[0])
new CastExpression(context.TypeSystemAstBuilder.ConvertType(method.ReturnType), arguments[0].UnwrapInDirectionExpression())
.CopyAnnotationsFrom(invocationExpression)
);
return;
}
if (method.Name == "op_True" && arguments.Length == 1 && invocationExpression.Role == Roles.Condition) {
invocationExpression.ReplaceWith(arguments[0]);
invocationExpression.ReplaceWith(arguments[0].UnwrapInDirectionExpression());
return;
}

Loading…
Cancel
Save