示例#1
0
 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;
 }
示例#2
0
        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);
        }
示例#3
0
 private void AddAssignment(CodeExpression lhs, CodeExpression rhs)
 {
     AddStatment(new CodeAssignStatement(lhs, rhs));
 }
示例#4
0
 static bool IsFalse(CodeExpression expression)
 {
     var literal = expression as CodePrimitiveExpression;
     if (literal == null)
         return false;
     return 0.Equals(literal.Value);
 }
示例#5
0
 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;
 }
示例#6
0
 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);
     }
 }
示例#7
0
 private void Push(CodeExpression expr)
 {
     this.stack.Push(new CodeExpressionEntry {
         Index = this.block.Statements.Count,
         Expression = expr
     });
 }