From 7c3fd3afda0b09a24df53efa5f378533b0ef3d73 Mon Sep 17 00:00:00 2001 From: Pent Ploompuu Date: Thu, 11 Aug 2011 22:14:42 +0300 Subject: [PATCH] Use annotations to mark lifted operators that can't be transformed by PushNegation --- ICSharpCode.Decompiler/Ast/Annotations.cs | 7 ------ .../Ast/AstMethodBodyBuilder.cs | 2 +- .../Ast/Transforms/PushNegation.cs | 25 +++++++++++-------- 3 files changed, 15 insertions(+), 19 deletions(-) diff --git a/ICSharpCode.Decompiler/Ast/Annotations.cs b/ICSharpCode.Decompiler/Ast/Annotations.cs index 6673b2ce4..257f889c0 100644 --- a/ICSharpCode.Decompiler/Ast/Annotations.cs +++ b/ICSharpCode.Decompiler/Ast/Annotations.cs @@ -12,11 +12,4 @@ namespace ICSharpCode.Decompiler.Ast this.InferredType = inferredType; } } - - sealed class InvisibleParenthesis - { - InvisibleParenthesis() { } - - public static readonly InvisibleParenthesis Value = new InvisibleParenthesis(); - } } diff --git a/ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs b/ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs index 0c9bce8e6..3852aec80 100644 --- a/ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs +++ b/ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs @@ -815,7 +815,7 @@ namespace ICSharpCode.Decompiler.Ast case ILCode.InitializedObject: return new InitializedObjectExpression(); case ILCode.Wrap: - return new ParenthesizedExpression(arg1).WithAnnotation(InvisibleParenthesis.Value); + return arg1.WithAnnotation(PushNegation.LiftedOperatorAnnotation); case ILCode.AddressOf: return MakeRef(arg1); case ILCode.NullableOf: diff --git a/ICSharpCode.Decompiler/Ast/Transforms/PushNegation.cs b/ICSharpCode.Decompiler/Ast/Transforms/PushNegation.cs index e0c54f1a2..193c5e692 100644 --- a/ICSharpCode.Decompiler/Ast/Transforms/PushNegation.cs +++ b/ICSharpCode.Decompiler/Ast/Transforms/PushNegation.cs @@ -26,8 +26,18 @@ namespace ICSharpCode.Decompiler.Ast.Transforms { public class PushNegation: DepthFirstAstVisitor, IAstTransform { + sealed class LiftedOperator { } + /// + /// Annotation for lifted operators that cannot be transformed by PushNegation + /// + public static readonly object LiftedOperatorAnnotation = new LiftedOperator(); + public override object VisitUnaryOperatorExpression(UnaryOperatorExpression unary, object data) { + // lifted operators can't be transformed + if (unary.Annotation() != null || unary.Expression.Annotation() != null) + return base.VisitUnaryOperatorExpression(unary, data); + // Remove double negation // !!a if (unary.Operator == UnaryOperatorType.Not && @@ -108,6 +118,10 @@ namespace ICSharpCode.Decompiler.Ast.Transforms public override object VisitBinaryOperatorExpression(BinaryOperatorExpression binaryOperatorExpression, object data) { + // lifted operators can't be transformed + if (binaryOperatorExpression.Annotation() != null) + return base.VisitBinaryOperatorExpression(binaryOperatorExpression, data); + BinaryOperatorType op = binaryOperatorExpression.Operator; bool? rightOperand = null; if (binaryOperatorExpression.Right is PrimitiveExpression) @@ -146,16 +160,5 @@ namespace ICSharpCode.Decompiler.Ast.Transforms { node.AcceptVisitor(this, null); } - - public override object VisitParenthesizedExpression(ParenthesizedExpression expr, object data) - { - // extra parentheses are redundant after this transformation - if(expr.Annotation() != null) { - var res = expr.Expression; - expr.ReplaceWith(res); - return res.AcceptVisitor(this, data); - } - return base.VisitParenthesizedExpression(expr, data); - } } }