public override bool VisitUnaryOperatorExpression(UnaryOperatorExpression unaryOperatorExpression, object data) { bool result = base.VisitUnaryOperatorExpression(unaryOperatorExpression, data); if (unaryOperatorExpression.Operator == UnaryOperatorType.Dereference) { BinaryOperatorExpression bop = unaryOperatorExpression.Expression as BinaryOperatorExpression; if (bop != null && bop.Operator == BinaryOperatorType.Add && bop.Annotation <PointerArithmetic>() != null) { // transform "*(ptr + int)" to "ptr[int]" IndexerExpression indexer = new IndexerExpression(); indexer.Target = bop.Left.Detach(); indexer.Arguments.Add(bop.Right.Detach()); indexer.CopyAnnotationsFrom(unaryOperatorExpression); indexer.CopyAnnotationsFrom(bop); unaryOperatorExpression.ReplaceWith(indexer); } return(true); } else if (unaryOperatorExpression.Operator == UnaryOperatorType.AddressOf) { return(true); } else { return(result); } }
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)); } } }