Browse Source

Compound assignment support for lifted operators

pull/205/head
Pent Ploompuu 14 years ago
parent
commit
ee0f43ad38
  1. 2
      ICSharpCode.Decompiler/Ast/Transforms/PushNegation.cs
  2. 5
      ICSharpCode.Decompiler/ILAst/NullableOperators.cs
  3. 12
      ICSharpCode.Decompiler/ILAst/PeepholeTransform.cs
  4. 4
      ICSharpCode.Decompiler/ILAst/TypeAnalysis.cs
  5. 14
      ICSharpCode.Decompiler/Tests/NullableOperators.cs

2
ICSharpCode.Decompiler/Ast/Transforms/PushNegation.cs

@ -150,7 +150,7 @@ namespace ICSharpCode.Decompiler.Ast.Transforms @@ -150,7 +150,7 @@ namespace ICSharpCode.Decompiler.Ast.Transforms
public override object VisitParenthesizedExpression(ParenthesizedExpression expr, object data)
{
// extra parentheses are redundant after this transformation
if(expr.Annotation<InvisibleParenthesis>() != null) {
if(expr.Annotation<InvisibleParenthesis>() != null) {
var res = expr.Expression;
expr.ReplaceWith(res);
return res.AcceptVisitor(this, data);

5
ICSharpCode.Decompiler/ILAst/NullableOperators.cs

@ -501,7 +501,10 @@ namespace ICSharpCode.Decompiler.ILAst @@ -501,7 +501,10 @@ namespace ICSharpCode.Decompiler.ILAst
n.Arguments[0] = n.Arguments[0].Arguments[0];
n.Arguments[1] = n.Arguments[1].Arguments[0];
}
} else if (n.Code != ILCode.Ceq && n.Code != ILCode.Cne) expr.Code = ILCode.NullableOf;
} else if (n.Code != ILCode.Ceq && n.Code != ILCode.Cne) {
expr.Code = ILCode.NullableOf;
expr.InferredType = expr.ExpectedType = null;
}
return true;
}
}

12
ICSharpCode.Decompiler/ILAst/PeepholeTransform.cs

@ -397,9 +397,21 @@ namespace ICSharpCode.Decompiler.ILAst @@ -397,9 +397,21 @@ namespace ICSharpCode.Decompiler.ILAst
return false;
ILExpression op = expr.Arguments.Last();
// in case of compound assignments with nullable values the result is inside NullableOf and the operand is inside ValueOf
bool nullable = false;
if (op.Code == ILCode.NullableOf) {
op = op.Arguments[0];
nullable = true;
}
if (!CanBeRepresentedAsCompoundAssignment(op.Code))
return false;
ILExpression ldelem = op.Arguments[0];
if (nullable) {
if (ldelem.Code != ILCode.ValueOf)
return false;
ldelem = ldelem.Arguments[0];
}
if (ldelem.Code != expectedLdelemCode)
return false;
Debug.Assert(ldelem.Arguments.Count == expr.Arguments.Count - 1);

4
ICSharpCode.Decompiler/ILAst/TypeAnalysis.cs

@ -555,7 +555,9 @@ namespace ICSharpCode.Decompiler.ILAst @@ -555,7 +555,9 @@ namespace ICSharpCode.Decompiler.ILAst
}
case ILCode.CompoundAssignment:
{
TypeReference varType = InferTypeForExpression(expr.Arguments[0].Arguments[0], null);
var op = expr.Arguments[0];
if (op.Code == ILCode.NullableOf) op = op.Arguments[0].Arguments[0];
var varType = InferTypeForExpression(op.Arguments[0], null);
if (forceInferChildren) {
InferTypeForExpression(expr.Arguments[0], varType);
}

14
ICSharpCode.Decompiler/Tests/NullableOperators.cs

@ -131,7 +131,7 @@ public static class NullableOperators @@ -131,7 +131,7 @@ public static class NullableOperators
a ^= b;
}
public static void BoolValueComplex(bool? a, Func<bool> x)
public static void BoolValueComplex(bool? a, Func<bool> x, bool?[] list)
{
Console.WriteLine(a == x());
Console.WriteLine(a != x());
@ -151,6 +151,7 @@ public static class NullableOperators @@ -151,6 +151,7 @@ public static class NullableOperators
a ^= x();
Console.WriteLine(x() ^ a);
list[0] ^= x();
}
public static void BoolValueConst(bool? a)
@ -315,7 +316,7 @@ public static class NullableOperators @@ -315,7 +316,7 @@ public static class NullableOperators
a >>= b;
}
public static void IntValueComplex(int? a, Func<int> x)
public static void IntValueComplex(int? a, Func<int> x, int?[] list)
{
Console.WriteLine(a == x());
Console.WriteLine(a != x());
@ -348,6 +349,7 @@ public static class NullableOperators @@ -348,6 +349,7 @@ public static class NullableOperators
a >>= x();
Console.WriteLine(x() + a);
list[0] += x();
}
public static void IntValueConst(int? a)
@ -513,7 +515,7 @@ public static class NullableOperators @@ -513,7 +515,7 @@ public static class NullableOperators
a %= b;
}
public static void NumberValueComplex(decimal? a, Func<decimal> x)
public static void NumberValueComplex(decimal? a, Func<decimal> x, decimal?[] list)
{
Console.WriteLine(a == x());
Console.WriteLine(a != x());
@ -536,6 +538,8 @@ public static class NullableOperators @@ -536,6 +538,8 @@ public static class NullableOperators
a %= x();
Console.WriteLine(x() + a);
// TODO: compound assignment with custom operators needs to be fixed first
//list[0] += x();
}
public static void NumberValueConst(decimal? a)
@ -676,7 +680,7 @@ public static class NullableOperators @@ -676,7 +680,7 @@ public static class NullableOperators
a >>= i;
}
public static void StructValueComplex(TS? a, Func<TS> x, Func<int> i)
public static void StructValueComplex(TS? a, Func<TS> x, Func<int> i, TS?[] list)
{
Console.WriteLine(a == x());
Console.WriteLine(a != x());
@ -709,6 +713,8 @@ public static class NullableOperators @@ -709,6 +713,8 @@ public static class NullableOperators
a >>= i();
Console.WriteLine(x() + a);
// TODO: compound assignment with custom operators needs to be fixed first
//list[0] += x();
}
}

Loading…
Cancel
Save