Esempio n. 1
0
        public override object VisitBinaryOperatorExpression(BinaryOperatorExpression binaryOperatorExpression, object data)
        {
            // lifted operators can't be transformed
            if (binaryOperatorExpression.Annotation <LiftedOperator>() != null)
            {
                return(base.VisitBinaryOperatorExpression(binaryOperatorExpression, data));
            }

            BinaryOperatorType op = binaryOperatorExpression.Operator;
            bool?rightOperand     = null;

            if (binaryOperatorExpression.Right is PrimitiveExpression)
            {
                rightOperand = ((PrimitiveExpression)binaryOperatorExpression.Right).Value as bool?;
            }
            if (op == BinaryOperatorType.Equality && rightOperand == true || op == BinaryOperatorType.InEquality && rightOperand == false)
            {
                // 'b == true' or 'b != false' is useless
                var ilRanges = binaryOperatorExpression.GetAllRecursiveILRanges();
                binaryOperatorExpression.Left.AcceptVisitor(this, data);
                binaryOperatorExpression.ReplaceWith(binaryOperatorExpression.Left.WithAnnotation(ilRanges));
                return(null);
            }
            else if (op == BinaryOperatorType.Equality && rightOperand == false || op == BinaryOperatorType.InEquality && rightOperand == true)
            {
                // 'b == false' or 'b != true' is a negation:
                Expression left = binaryOperatorExpression.Left;
                left.Remove();
                var ilRanges = binaryOperatorExpression.GetAllRecursiveILRanges();
                UnaryOperatorExpression uoe = new UnaryOperatorExpression(UnaryOperatorType.Not, left);
                binaryOperatorExpression.ReplaceWith(uoe);
                uoe.AddAnnotation(ilRanges);
                return(uoe.AcceptVisitor(this, data));
            }
            else
            {
                bool  negate = false;
                Match m      = asCastIsNotNullPattern.Match(binaryOperatorExpression);
                if (!m.Success)
                {
                    m      = asCastIsNullPattern.Match(binaryOperatorExpression);
                    negate = true;
                }
                if (m.Success)
                {
                    Expression expr = m.Get <Expression>("expr").Single().Detach().IsType(m.Get <AstType>("type").Single().Detach());
                    if (negate)
                    {
                        expr = new UnaryOperatorExpression(UnaryOperatorType.Not, expr);
                    }
                    binaryOperatorExpression.ReplaceWith(expr);
                    expr.AddAnnotation(binaryOperatorExpression.GetAllRecursiveILRanges());
                    return(expr.AcceptVisitor(this, data));
                }
                else
                {
                    return(base.VisitBinaryOperatorExpression(binaryOperatorExpression, data));
                }
            }
        }