Browse Source

Fix equality checks in ExtractExpression.

pull/1350/head
Siegfried Pammer 6 years ago
parent
commit
fa4c239d67
  1. 32
      ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs

32
ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs

@ -1020,48 +1020,60 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
{ {
Expression fieldReference = MakeFieldReference(); Expression fieldReference = MakeFieldReference();
// Math.PI or Math.E or (float)Math.PI or (float)Math.E or MathF.PI or MathF.E
Expression expr = fieldReference; Expression expr = fieldReference;
if (n != 1) { if (n != 1) {
if (n == -1) { if (n == -1) {
// -field
expr = new UnaryOperatorExpression(UnaryOperatorType.Minus, expr); expr = new UnaryOperatorExpression(UnaryOperatorType.Minus, expr);
} else { } else {
// field * n
expr = new BinaryOperatorExpression(expr, BinaryOperatorType.Multiply, MakeConstant(type, n)); expr = new BinaryOperatorExpression(expr, BinaryOperatorType.Multiply, MakeConstant(type, n));
} }
} }
if (d != 1) { if (d != 1) {
// field * n / d or -field / d or field / d
expr = new BinaryOperatorExpression(expr, BinaryOperatorType.Divide, MakeConstant(type, d)); expr = new BinaryOperatorExpression(expr, BinaryOperatorType.Divide, MakeConstant(type, d));
} }
#pragma warning disable IDE0004
if (isDouble) { if (isDouble) {
double approxValue = (double)n / (double)d * (memberName == "PI" ? Math.PI : Math.E); double field = memberName == "PI" ? Math.PI : Math.E;
double approxValue = field * n / d;
if (approxValue == (double)literalValue) if (approxValue == (double)literalValue)
return expr; return expr;
} else { } else {
float approxValue = (float)n / (float)d * (memberName == "PI" ? MathF_PI : MathF_E); float field = memberName == "PI" ? MathF_PI : MathF_E;
float approxValue = field * n / d;
if (approxValue == (float)literalValue) if (approxValue == (float)literalValue)
return expr; return expr;
} }
#pragma warning restore IDE0004
// Math.PI or Math.E or (float)Math.PI or (float)Math.E or MathF.PI or MathF.E
expr = fieldReference.Detach(); expr = fieldReference.Detach();
expr = new BinaryOperatorExpression(MakeConstant(type, n), BinaryOperatorType.Divide, expr);
if (d != 1) { if (d == 1) {
// n / field
expr = new BinaryOperatorExpression(MakeConstant(type, n), BinaryOperatorType.Divide, expr);
} else {
// n / (d * field)
expr = new BinaryOperatorExpression(MakeConstant(type, d), BinaryOperatorType.Multiply, expr); expr = new BinaryOperatorExpression(MakeConstant(type, d), BinaryOperatorType.Multiply, expr);
expr = new BinaryOperatorExpression(MakeConstant(type, n), BinaryOperatorType.Divide, expr);
} }
#pragma warning disable IDE0004
if (isDouble) { if (isDouble) {
double approxValue = (double)n / ((double)d * (memberName == "PI" ? Math.PI : Math.E)); double field = memberName == "PI" ? Math.PI : Math.E;
double approxValue = (double)n / ((double)d * field);
if (approxValue == (double)literalValue) if (approxValue == (double)literalValue)
return expr; return expr;
} else { } else {
float approxValue = (float)n / ((float)d * (memberName == "PI" ? MathF_PI : MathF_E)); float field = memberName == "PI" ? MathF_PI : MathF_E;
float approxValue = (float)n / ((float)d * field);
if (approxValue == (float)literalValue) if (approxValue == (float)literalValue)
return expr; return expr;
} }
#pragma warning restore IDE0004
return null; return null;
} }

Loading…
Cancel
Save