bool IsBooleanExpression(CodeExpression expression) { if (expression is CodeBinaryExpression) { return IsComparisonOperator(((CodeBinaryExpression)expression).Operator); } else if (expression is CodeInvokeExpression) { var reference = ((CodeInvokeExpression)expression).Method.Reference; if (reference != null) return reference.ReturnType.FullName == "System.Boolean"; } return false; }
private CodeExpression OptimizeBinaryExpression(CodeExpression lhs, CodeBinaryOperator op, CodeExpression rhs) { CodePrimitiveExpression cpe = rhs as CodePrimitiveExpression; if (cpe != null && cpe.Value is bool) { bool test = (bool)cpe.Value; if ((test && op == CodeBinaryOperator.IdentityEquality) || (!test && op == CodeBinaryOperator.IdentityInequality)) { // (x == true) -> (x) // (x != false) -> (x) return lhs; } else if ((test && op == CodeBinaryOperator.IdentityInequality) || (!test && op == CodeBinaryOperator.IdentityEquality)) { // (x != true) -> !(x) // (x == false) -> !(x) return lhs.Invert(); } else { throw new NotSupportedException(); } } if (IsBooleanExpression(lhs) && IsFalse(rhs)) { return lhs.Invert(); } return new CodeBinaryExpression(lhs, op, rhs); }
private void AddAssignment(CodeExpression lhs, CodeExpression rhs) { AddStatment(new CodeAssignStatement(lhs, rhs)); }
static bool IsFalse(CodeExpression expression) { var literal = expression as CodePrimitiveExpression; if (literal == null) return false; return 0.Equals(literal.Value); }
private CodeExpression RefinePrimitiveExpression(CodeExpression expr, TypeReference typeRef) { var cpe = expr as CodePrimitiveExpression; if (cpe != null) { var targetType = TypeSystem.GetReflectionType(typeRef); var converted = Convert.ChangeType(cpe.Value, targetType); cpe.Value = converted; return cpe; } return expr; }
private void RefineBinaryExpression(CodeExpression lhs, CodeExpression rhs) { if (lhs is CodePrimitiveExpression) { var rhsType = this.typeEvaluator.Evaluate(rhs); RefinePrimitiveExpression(lhs, rhsType); } else if (rhs is CodePrimitiveExpression) { var lhsType = this.typeEvaluator.Evaluate(lhs); RefinePrimitiveExpression(rhs, lhsType); } }
private void Push(CodeExpression expr) { this.stack.Push(new CodeExpressionEntry { Index = this.block.Statements.Count, Expression = expr }); }