private void DoBoolean(BooleanExpression expression, CodeBinaryOperatorType operation)
        {
            var leftArgs = VisitChild(expression.Left, new CodeDomArg() { Scope = _codeStack.Peek().Scope });
            var rightArgs = VisitChild(expression.Right, new CodeDomArg() { Scope = _codeStack.Peek().Scope });

            Type leftType = Type.GetType(leftArgs.Scope.CodeDomReference.BaseType);
            Type rightType = Type.GetType(rightArgs.Scope.CodeDomReference.BaseType);
            if (leftType != rightType)
            {
                if (leftType == typeof(string))
                {
                    var primitive = TablePrimitive.FromType(rightType);
                    leftArgs.CodeExpression = primitive.ToNative(leftArgs.CodeExpression);
                }
                else if(rightType == typeof(string))
                {
                    var primitive = TablePrimitive.FromType(leftType);
                    rightArgs.CodeExpression = primitive.ToNative(rightArgs.CodeExpression);
                }
            }

            if (leftArgs.Tag != null)
                _codeStack.Peek().Tag = leftArgs.Tag;
            if (rightArgs.Tag != null)
                _codeStack.Peek().Tag = rightArgs.Tag;

            _codeStack.Peek().CodeExpression = new CodeBinaryOperatorExpression(leftArgs.CodeExpression, operation, rightArgs.CodeExpression);
        }
		public CodeBinaryOperatorExpression (CodeExpression left,
						     CodeBinaryOperatorType op,
						     CodeExpression right)
		{
			this.left = left;
			this.op = op;
			this.right = right;
		}
Beispiel #3
0
        string Operator(CodeBinaryOperatorType op)
        {
            switch (op)
            {
                case CodeBinaryOperatorType.Assign: return new string(new[] { Parser.AssignPre, Parser.Equal });
                case CodeBinaryOperatorType.BooleanAnd: return Parser.AndTxt;
                case CodeBinaryOperatorType.BooleanOr: return Parser.OrTxt;

                default: throw new ArgumentOutOfRangeException("op");
            }
        }
 protected override void OutputOperator(CodeBinaryOperatorType op)
 {
     switch (op)
     {
         case CodeBinaryOperatorType.BitwiseAnd:
         case CodeBinaryOperatorType.BooleanAnd: Output.Write("and"); break;
         case CodeBinaryOperatorType.BitwiseOr:
         case CodeBinaryOperatorType.BooleanOr: Output.Write("or"); break;
         default: base.OutputOperator(op); break;
     }
 }
        private void DoBooleanAggregate(BooleanExpression expression, CodeBinaryOperatorType operation)
        {
            var leftArgs = VisitChild(expression.Left, new CodeDomArg() { Scope = _codeStack.Peek().Scope });
            var rightArgs = VisitChild(expression.Right, new CodeDomArg() { Scope = _codeStack.Peek().Scope });

            if (leftArgs.Tag != null)
                _codeStack.Peek().Tag = leftArgs.Tag;
            if (rightArgs.Tag != null)
                _codeStack.Peek().Tag = rightArgs.Tag;

            _codeStack.Peek().CodeExpression = new CodeBinaryOperatorExpression(leftArgs.CodeExpression, operation, rightArgs.CodeExpression);
        }
 public BinaryOpOperatorExpression(
     Expression left,
     Expression right,
     CodeBinaryOperatorType op
     )
 {
     if (left==null)
         throw new ArgumentNullException("left");
     if (right==null)
         throw new ArgumentNullException("right");
     this.left = left;
     this.right = right;
     this.op = op;
 }
 protected RuleEvaluationIncompatibleTypesException(SerializationInfo serializeInfo, StreamingContext context) : base(serializeInfo, context)
 {
     if (serializeInfo == null)
     {
         throw new ArgumentNullException("serializeInfo");
     }
     string typeName = serializeInfo.GetString("left");
     if (typeName != "null")
     {
         this.m_leftType = Type.GetType(typeName);
     }
     this.m_op = (CodeBinaryOperatorType) serializeInfo.GetValue("op", typeof(CodeBinaryOperatorType));
     typeName = serializeInfo.GetString("right");
     if (typeName != "null")
     {
         this.m_rightType = Type.GetType(typeName);
     }
 }
 private string GetOperatorString(CodeBinaryOperatorType operatorType)
 {
     switch (operatorType)
     {
         case CodeBinaryOperatorType.Add:
             return "+";
         case CodeBinaryOperatorType.Subtract:
             return "-";
         case CodeBinaryOperatorType.Multiply:
             return "*";
         case CodeBinaryOperatorType.Divide:
             return "/";
         case CodeBinaryOperatorType.Modulus:
             return "%";
         case CodeBinaryOperatorType.Assign:
             return "=";
         case CodeBinaryOperatorType.IdentityInequality:
             return "!=";
         case CodeBinaryOperatorType.IdentityEquality:
             return "===";
         case CodeBinaryOperatorType.ValueEquality:
             return "==";
         case CodeBinaryOperatorType.BitwiseOr:
             return "|";
         case CodeBinaryOperatorType.BitwiseAnd:
             return "&";
         case CodeBinaryOperatorType.BooleanOr:
             return "||";
         case CodeBinaryOperatorType.BooleanAnd:
             return "&&";
         case CodeBinaryOperatorType.LessThan:
             return "<";
         case CodeBinaryOperatorType.LessThanOrEqual:
             return "<=";
         case CodeBinaryOperatorType.GreaterThan:
             return ">";
         case CodeBinaryOperatorType.GreaterThanOrEqual:
             return ">=";
         default:
             throw new ArgumentOutOfRangeException();
     }
 }
        private static object EvaluateBinaryOperation(CodeBinaryOperatorExpression binaryExpr, Type lhsType, object lhsValue, CodeBinaryOperatorType operation, Type rhsType, object rhsValue)
        {
            Literal literal;
            Literal literal2;
            ArithmeticLiteral literal3;
            ArithmeticLiteral literal4;
            RuleEvaluationException exception;
            switch (operation)
            {
                case CodeBinaryOperatorType.Add:
                    literal3 = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue);
                    if (literal3 == null)
                    {
                        break;
                    }
                    literal4 = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue);
                    if (literal4 == null)
                    {
                        break;
                    }
                    return literal3.Add(literal4);

                case CodeBinaryOperatorType.Subtract:
                    literal3 = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue);
                    if (literal3 == null)
                    {
                        break;
                    }
                    literal4 = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue);
                    if (literal4 == null)
                    {
                        break;
                    }
                    return literal3.Subtract(literal4);

                case CodeBinaryOperatorType.Multiply:
                    literal3 = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue);
                    if (literal3 == null)
                    {
                        break;
                    }
                    literal4 = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue);
                    if (literal4 == null)
                    {
                        break;
                    }
                    return literal3.Multiply(literal4);

                case CodeBinaryOperatorType.Divide:
                    literal3 = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue);
                    if (literal3 == null)
                    {
                        break;
                    }
                    literal4 = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue);
                    if (literal4 == null)
                    {
                        break;
                    }
                    return literal3.Divide(literal4);

                case CodeBinaryOperatorType.Modulus:
                    literal3 = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue);
                    if (literal3 == null)
                    {
                        break;
                    }
                    literal4 = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue);
                    if (literal4 == null)
                    {
                        break;
                    }
                    return literal3.Modulus(literal4);

                case CodeBinaryOperatorType.IdentityInequality:
                    return (lhsValue != rhsValue);

                case CodeBinaryOperatorType.IdentityEquality:
                    return (lhsValue == rhsValue);

                case CodeBinaryOperatorType.ValueEquality:
                    literal = Literal.MakeLiteral(lhsType, lhsValue);
                    if (literal == null)
                    {
                        break;
                    }
                    literal2 = Literal.MakeLiteral(rhsType, rhsValue);
                    if (literal2 == null)
                    {
                        break;
                    }
                    return literal.Equal(literal2);

                case CodeBinaryOperatorType.BitwiseOr:
                    literal3 = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue);
                    if (literal3 == null)
                    {
                        break;
                    }
                    literal4 = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue);
                    if (literal4 == null)
                    {
                        break;
                    }
                    return literal3.BitOr(literal4);

                case CodeBinaryOperatorType.BitwiseAnd:
                    literal3 = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue);
                    if (literal3 == null)
                    {
                        break;
                    }
                    literal4 = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue);
                    if (literal4 == null)
                    {
                        break;
                    }
                    return literal3.BitAnd(literal4);

                case CodeBinaryOperatorType.LessThan:
                    literal = Literal.MakeLiteral(lhsType, lhsValue);
                    if (literal == null)
                    {
                        break;
                    }
                    literal2 = Literal.MakeLiteral(rhsType, rhsValue);
                    if (literal2 == null)
                    {
                        break;
                    }
                    return literal.LessThan(literal2);

                case CodeBinaryOperatorType.LessThanOrEqual:
                    literal = Literal.MakeLiteral(lhsType, lhsValue);
                    if (literal == null)
                    {
                        break;
                    }
                    literal2 = Literal.MakeLiteral(rhsType, rhsValue);
                    if (literal2 == null)
                    {
                        break;
                    }
                    return literal.LessThanOrEqual(literal2);

                case CodeBinaryOperatorType.GreaterThan:
                    literal = Literal.MakeLiteral(lhsType, lhsValue);
                    if (literal == null)
                    {
                        break;
                    }
                    literal2 = Literal.MakeLiteral(rhsType, rhsValue);
                    if (literal2 == null)
                    {
                        break;
                    }
                    return literal.GreaterThan(literal2);

                case CodeBinaryOperatorType.GreaterThanOrEqual:
                    literal = Literal.MakeLiteral(lhsType, lhsValue);
                    if (literal == null)
                    {
                        break;
                    }
                    literal2 = Literal.MakeLiteral(rhsType, rhsValue);
                    if (literal2 == null)
                    {
                        break;
                    }
                    return literal.GreaterThanOrEqual(literal2);

                default:
                    exception = new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.BinaryOpNotSupported, new object[] { operation.ToString() }));
                    exception.Data["ErrorObject"] = binaryExpr;
                    throw exception;
            }
            exception = new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.BinaryOpFails, new object[] { operation.ToString(), RuleDecompiler.DecompileType(lhsType), RuleDecompiler.DecompileType(rhsType) }));
            exception.Data["ErrorObject"] = binaryExpr;
            throw exception;
        }
Beispiel #10
0
 internal BinaryOperationDescriptor(TokenID token, CodeBinaryOperatorType codeDomOperator)
 {
     this.token = token;
     this.codeDomOperator = codeDomOperator;
 }
Beispiel #11
0
 internal static CodeBinaryOperatorExpression GetOperator(CodeExpression left, CodeBinaryOperatorType type, CodeExpression right)
 {
     return(new CodeBinaryOperatorExpression(left, type, right));
 }
        private CodeExpression VisitBinary(BinaryExpression binaryExpression)
        {
            CodeBinaryOperatorType operType = default(CodeBinaryOperatorType);

            switch (binaryExpression.NodeType)
            {
            case ExpressionType.ArrayIndex:
                return(new CodeArrayIndexerExpression(
                           GetExpression(binaryExpression.Left), new CodeExpression[] { GetExpression(binaryExpression.Right) }
                           ));

            case ExpressionType.Add:
                operType = CodeBinaryOperatorType.Add;
                break;

            case ExpressionType.AddChecked:
                operType = CodeBinaryOperatorType.Add;
                break;

            case ExpressionType.And:
                operType = CodeBinaryOperatorType.BitwiseAnd;
                break;

            case ExpressionType.AndAlso:
                operType = CodeBinaryOperatorType.BooleanAnd;
                break;

            case ExpressionType.Divide:
                operType = CodeBinaryOperatorType.Divide;
                break;

            case ExpressionType.Equal:
                operType = CodeBinaryOperatorType.ValueEquality;
                break;

            case ExpressionType.ExclusiveOr:
                return(new CodeXorExpression(_Visit(binaryExpression.Left), _Visit(binaryExpression.Right)));

            case ExpressionType.GreaterThan:
                operType = CodeBinaryOperatorType.GreaterThan;
                break;

            case ExpressionType.GreaterThanOrEqual:
                operType = CodeBinaryOperatorType.GreaterThanOrEqual;
                break;

            case ExpressionType.LessThan:
                operType = CodeBinaryOperatorType.LessThan;
                break;

            case ExpressionType.LessThanOrEqual:
                operType = CodeBinaryOperatorType.LessThanOrEqual;
                break;

            case ExpressionType.Modulo:
                operType = CodeBinaryOperatorType.Modulus;
                break;

            case ExpressionType.Multiply:
                operType = CodeBinaryOperatorType.Multiply;
                break;

            case ExpressionType.MultiplyChecked:
                operType = CodeBinaryOperatorType.Multiply;
                break;

            case ExpressionType.NotEqual:
                operType = CodeBinaryOperatorType.IdentityInequality;
                break;

            case ExpressionType.Or:
                operType = CodeBinaryOperatorType.BitwiseOr;
                break;

            case ExpressionType.OrElse:
                operType = CodeBinaryOperatorType.BooleanOr;
                break;

            case ExpressionType.Subtract:
                operType = CodeBinaryOperatorType.Subtract;
                break;

            case ExpressionType.SubtractChecked:
                operType = CodeBinaryOperatorType.Subtract;
                break;

            default:
                throw new NotImplementedException(binaryExpression.NodeType.ToString());
            }

            return(new CodeBinaryOperatorExpression(
                       _Visit(binaryExpression.Left), operType, _Visit(binaryExpression.Right)));
        }
		private static CodeExpression Op(CodeBinaryOperatorType op, CodeExpression target, CodeExpression arg0, CodeExpression arg1)
		{
			if (arg1 == null)
				return new CodeBinaryOperatorExpression(target, op, arg0);
			else
				return new CodeBinaryOperatorExpression(arg0, op, arg1);
		}
Beispiel #14
0
        internal static RuleBinaryExpressionInfo AllowedComparison(
            Type lhs,
            CodeExpression lhsExpression,
            Type rhs,
            CodeExpression rhsExpression,
            CodeBinaryOperatorType comparison,
            RuleValidation validator,
            out ValidationError error)
        {
            // note that null values come in as a NullLiteral type
            TypeFlags lhsFlags, rhsFlags;

            // are the types supported?
            if ((supportedTypes.TryGetValue(lhs, out lhsFlags)) && (supportedTypes.TryGetValue(rhs, out rhsFlags)))
            {
                // both sides supported
                if (lhsFlags == rhsFlags)
                {
                    // both sides the same type, so it's allowed
                    // only allow equality on booleans
                    if ((lhsFlags == TypeFlags.Bool) && (comparison != CodeBinaryOperatorType.ValueEquality))
                    {
                        string message = string.Format(CultureInfo.CurrentCulture, Messages.RelationalOpBadTypes, comparison.ToString(),
                            RuleDecompiler.DecompileType(lhs),
                            RuleDecompiler.DecompileType(rhs));
                        error = new ValidationError(message, ErrorNumbers.Error_OperandTypesIncompatible);
                        return null;
                    }
                    error = null;
                    return new RuleBinaryExpressionInfo(lhs, rhs, typeof(bool));
                }

                // if not the same, only certain combinations allowed
                switch (lhsFlags | rhsFlags)
                {
                    case TypeFlags.Decimal | TypeFlags.SignedNumbers:
                    case TypeFlags.Decimal | TypeFlags.UnsignedNumbers:
                    case TypeFlags.Decimal | TypeFlags.ULong:
                    case TypeFlags.Float | TypeFlags.SignedNumbers:
                    case TypeFlags.Float | TypeFlags.UnsignedNumbers:
                    case TypeFlags.Float | TypeFlags.ULong:
                    case TypeFlags.ULong | TypeFlags.UnsignedNumbers:
                    case TypeFlags.SignedNumbers | TypeFlags.UnsignedNumbers:
                        error = null;
                        return new RuleBinaryExpressionInfo(lhs, rhs, typeof(bool));
                }
                string message2 = string.Format(CultureInfo.CurrentCulture, Messages.RelationalOpBadTypes, comparison.ToString(),
                    (lhs == typeof(NullLiteral)) ? Messages.NullValue : RuleDecompiler.DecompileType(lhs),
                    (rhs == typeof(NullLiteral)) ? Messages.NullValue : RuleDecompiler.DecompileType(rhs));
                error = new ValidationError(message2, ErrorNumbers.Error_OperandTypesIncompatible);
                return null;
            }
            else
            {
                // see if they override the operator
                MethodInfo operatorOverride = MapOperatorToMethod(comparison, lhs, lhsExpression, rhs, rhsExpression, validator, out error);
                if (operatorOverride != null)
                    return new RuleBinaryExpressionInfo(lhs, rhs, operatorOverride);

                // unable to evaluate, so return false
                return null;
            }
        }
Beispiel #15
0
 private static CodeExpression IncDec(CodeVariableReferenceExpression exp, CodeBinaryOperatorType op) =>
 BinOpExp(
     exp,
     CodeBinaryOperatorType.Assign,
     BinOpExp(exp, op, new CodePrimitiveExpression(1)));
Beispiel #16
0
 public static CodeBinaryOperatorExpression BinaryOp(this CodeExpression left, CodeBinaryOperatorType op, CodeExpression right) => new CodeBinaryOperatorExpression(left, op, right);
Beispiel #17
0
        public override object VisitBinaryOperatorExpression(BinaryOperatorExpression binaryOperatorExpression, object data)
        {
            CodeBinaryOperatorType op = CodeBinaryOperatorType.Add;

            switch (binaryOperatorExpression.Op)
            {
            case BinaryOperatorType.Add:
                op = CodeBinaryOperatorType.Add;
                break;

            case BinaryOperatorType.BitwiseAnd:
                op = CodeBinaryOperatorType.BitwiseAnd;
                break;

            case BinaryOperatorType.BitwiseOr:
                op = CodeBinaryOperatorType.BitwiseOr;
                break;

            case BinaryOperatorType.LogicalAnd:
                op = CodeBinaryOperatorType.BooleanAnd;
                break;

            case BinaryOperatorType.LogicalOr:
                op = CodeBinaryOperatorType.BooleanOr;
                break;

            case BinaryOperatorType.Divide:
            case BinaryOperatorType.DivideInteger:
                op = CodeBinaryOperatorType.Divide;
                break;

            case BinaryOperatorType.GreaterThan:
                op = CodeBinaryOperatorType.GreaterThan;
                break;

            case BinaryOperatorType.GreaterThanOrEqual:
                op = CodeBinaryOperatorType.GreaterThanOrEqual;
                break;

            case BinaryOperatorType.Equality:
                op = CodeBinaryOperatorType.IdentityEquality;
                break;

            case BinaryOperatorType.InEquality:
                op = CodeBinaryOperatorType.IdentityInequality;
                break;

            case BinaryOperatorType.LessThan:
                op = CodeBinaryOperatorType.LessThan;
                break;

            case BinaryOperatorType.LessThanOrEqual:
                op = CodeBinaryOperatorType.LessThanOrEqual;
                break;

            case BinaryOperatorType.Modulus:
                op = CodeBinaryOperatorType.Modulus;
                break;

            case BinaryOperatorType.Multiply:
                op = CodeBinaryOperatorType.Multiply;
                break;

            case BinaryOperatorType.Subtract:
                op = CodeBinaryOperatorType.Subtract;
                break;

            //case BinaryOperatorType.ValueEquality:
            //	op = CodeBinaryOperatorType.ValueEquality;
            //	break;
            case BinaryOperatorType.ShiftLeft:
            case BinaryOperatorType.ShiftRight:
                // CodeDOM suxx
                op = CodeBinaryOperatorType.Multiply;
                break;

            case BinaryOperatorType.ReferenceEquality:
                op = CodeBinaryOperatorType.IdentityEquality;
                break;

            case BinaryOperatorType.ReferenceInequality:
                op = CodeBinaryOperatorType.IdentityInequality;
                break;

            case BinaryOperatorType.ExclusiveOr:
                // TODO ExclusiveOr
                op = CodeBinaryOperatorType.BitwiseAnd;
                break;
            }
            return(new CodeBinaryOperatorExpression((CodeExpression)binaryOperatorExpression.Left.AcceptVisitor(this, data),
                                                    op,
                                                    (CodeExpression)binaryOperatorExpression.Right.AcceptVisitor(this, data)));
        }
Beispiel #18
0
        // Output a binary operator.
        protected virtual void OutputOperator(CodeBinaryOperatorType op)
        {
            String oper;

            switch (op)
            {
            case CodeBinaryOperatorType.Add:
                oper = "+"; break;

            case CodeBinaryOperatorType.Subtract:
                oper = "-"; break;

            case CodeBinaryOperatorType.Multiply:
                oper = "*"; break;

            case CodeBinaryOperatorType.Divide:
                oper = "/"; break;

            case CodeBinaryOperatorType.Modulus:
                oper = "%"; break;

            case CodeBinaryOperatorType.Assign:
                oper = "="; break;

            case CodeBinaryOperatorType.IdentityInequality:
                oper = "!="; break;

            case CodeBinaryOperatorType.IdentityEquality:
                oper = "=="; break;

            case CodeBinaryOperatorType.ValueEquality:
                oper = "=="; break;

            case CodeBinaryOperatorType.BitwiseOr:
                oper = "|"; break;

            case CodeBinaryOperatorType.BitwiseAnd:
                oper = "&"; break;

            case CodeBinaryOperatorType.BooleanOr:
                oper = "||"; break;

            case CodeBinaryOperatorType.BooleanAnd:
                oper = "&&"; break;

            case CodeBinaryOperatorType.LessThan:
                oper = "<"; break;

            case CodeBinaryOperatorType.LessThanOrEqual:
                oper = "<="; break;

            case CodeBinaryOperatorType.GreaterThan:
                oper = ">"; break;

            case CodeBinaryOperatorType.GreaterThanOrEqual:
                oper = ">="; break;

            default: return;
            }
            Output.Write(oper);
        }
 public CodeBinaryOperatorExpression(CodeExpression left, CodeBinaryOperatorType op, CodeExpression right)
 {
 }
Beispiel #20
0
 protected virtual void OutputOperator(CodeBinaryOperatorType op)
 {
     throw new NotImplementedException();
 }
Beispiel #21
0
 private static void dumpExpression(CodeExpression e)
 {
     /*
      *               System.CodeDom.CodeArgumentReferenceExpression
      *                 System.CodeDom.CodeArrayCreateExpression
      *                 System.CodeDom.CodeArrayIndexerExpression
      *                 System.CodeDom.CodeBaseReferenceExpression
      *                 System.CodeDom.CodeBinaryOperatorExpression
      *                 System.CodeDom.CodeCastExpression
      *                 System.CodeDom.CodeDefaultValueExpression
      *                 System.CodeDom.CodeDelegateCreateExpression
      *                 System.CodeDom.CodeDelegateInvokeExpression
      *                 System.CodeDom.CodeDirectionExpression
      *                 System.CodeDom.CodeEventReferenceExpression
      *                 System.CodeDom.CodeFieldReferenceExpression
      *                 System.CodeDom.CodeIndexerExpression
      *                 System.CodeDom.CodeMethodInvokeExpression
      *                 System.CodeDom.CodeMethodReferenceExpression
      *                 System.CodeDom.CodeObjectCreateExpression
      *                 System.CodeDom.CodeParameterDeclarationExpression
      *                 System.CodeDom.CodePrimitiveExpression
      *                 System.CodeDom.CodePropertyReferenceExpression
      *                 System.CodeDom.CodePropertySetValueReferenceExpression
      *                 System.CodeDom.CodeSnippetExpression
      *                 System.CodeDom.CodeThisReferenceExpression
      *                 System.CodeDom.CodeTypeOfExpression
      *                 System.CodeDom.CodeTypeReferenceExpression
      *                 System.CodeDom.CodeVariableReferenceExpression
      */
     if (e == null)
     {
         return;
     }
     if (e is CodeFieldReferenceExpression)
     {
         CodeFieldReferenceExpression exp = (CodeFieldReferenceExpression)e;
         dumpExpression(exp.TargetObject);
         writeLineIndent("F: " + exp.FieldName);
     }
     else if (e is CodeObjectCreateExpression)
     {
         var exp = (CodeObjectCreateExpression)e;
         var tr  = exp.CreateType as CodeTypeReference;
         writeLineIndent(tr.BaseType + "{}");
         dumpExpressionList(exp.Parameters);
     }
     else if (e is CodeMethodInvokeExpression)
     {
         var exp = (CodeMethodInvokeExpression)e;
         dumpExpression(exp.Method.TargetObject);
         writeLineIndent("M: " + exp.Method.MethodName);
         dumpExpressionList(exp.Parameters);
     }
     else if (e is CodePropertyReferenceExpression)
     {
         var exp = (CodePropertyReferenceExpression)e;
         dumpExpression(exp.TargetObject);
         writeLineIndent("P: " + exp.PropertyName);
     }
     else if (e is CodePrimitiveExpression)
     {
         var exp = (CodePrimitiveExpression)e;
         writeLineIndent(exp.ToString());
         writeLineIndent(exp.Value.ToString());
     }
     else if (e is CodeCastExpression)
     {
         var exp = (CodeCastExpression)e;
         writeLineIndent(exp.TargetType.ToString());
         dumpExpression(exp.Expression);
     }
     else if (e is CodeThisReferenceExpression)
     {
         writeLineIndent("SELF");
     }
     else if (e is CodeBinaryOperatorExpression)
     {
         var exp = (CodeBinaryOperatorExpression)e;
         dumpExpression(exp.Left);
         CodeBinaryOperatorType opType = exp.Operator;
         writeLineIndent(opType.ToString());
         dumpExpression(exp.Right);
     }
     else if (e is CodeTypeReferenceExpression)
     {
         var exp = (CodeTypeReferenceExpression)e;
         var tr  = exp.Type;
         writeLineIndent(tr.BaseType);
     }
     else if (e is CodeBaseReferenceExpression)
     {
         writeLineIndent("SUPER");
     }
     else
     {
         writeLineIndent(e.GetType().FullName);
     }
 }
Beispiel #22
0
        public static CodeExpression BinOpExpJoin(this IEnumerable <CodeExpression> expressions, CodeBinaryOperatorType op)
        {
            CodeExpression codeExp = expressions.First();

            foreach (var x in expressions.Skip(1))
            {
                codeExp = CodeHelper.BinOpExp(codeExp, op, x);
            }

            return(codeExp);
        }
        public EnumOperationMethodInfo(Type lhs, CodeBinaryOperatorType operation, Type rhs, bool isZero)
        {
            // only 5 arithmetic cases (U = underlying type of E):
            //    E = E + U
            //    E = U + E
            //    U = E - E
            //    E = E - U
            //    E = U - E
            // plus 5 comparison cases
            //    E == E
            //    E < E
            //    E <= E
            //    E > E
            //    E >= E
            // either E can be nullable

            op = operation;

            // parameters are easy -- they are the same as the type passed in
            expectedParameters = new ParameterInfo[2];
            expectedParameters[0] = new SimpleParameterInfo(lhs);
            expectedParameters[1] = new SimpleParameterInfo(rhs);

            // compute return type (depends on type of operation)
            // start by getting the types without Nullable<>
            bool lhsNullable = ConditionHelper.IsNullableValueType(lhs);
            bool rhsNullable = ConditionHelper.IsNullableValueType(rhs);
            lhsBaseType = (lhsNullable) ? Nullable.GetUnderlyingType(lhs) : lhs;
            rhsBaseType = (rhsNullable) ? Nullable.GetUnderlyingType(rhs) : rhs;
            // determine the underlying types for both sides
            if (lhsBaseType.IsEnum)
                lhsRootType = EnumHelper.GetUnderlyingType(lhsBaseType);
            else
                lhsRootType = lhsBaseType;

            if (rhsBaseType.IsEnum)
                rhsRootType = EnumHelper.GetUnderlyingType(rhsBaseType);
            else
                rhsRootType = rhsBaseType;

            switch (op)
            {
                case CodeBinaryOperatorType.Add:
                    // add always produces an enum, except enum + enum
                    if ((lhsBaseType.IsEnum) && (rhs.IsEnum))
                        resultBaseType = lhsRootType;
                    else if (lhsBaseType.IsEnum)
                        resultBaseType = lhsBaseType;
                    else
                        resultBaseType = rhsBaseType;
                    // if either side is nullable, result is nullable
                    resultIsNullable = (lhsNullable || rhsNullable);
                    resultType = (resultIsNullable) ? typeof(Nullable<>).MakeGenericType(resultBaseType) : resultBaseType;
                    break;
                case CodeBinaryOperatorType.Subtract:
                    // subtract can be an enum or the underlying type
                    if (rhsBaseType.IsEnum && lhsBaseType.IsEnum)
                    {
                        resultRootType = rhsRootType;
                        resultBaseType = rhsRootType;
                    }
                    else if (lhsBaseType.IsEnum)
                    {
                        // special case for E - 0
                        // if 0 is the underlying type, then use E - U
                        // if not the underlying type, then 0 becomes E, use E - E
                        resultRootType = lhsRootType;
                        if (isZero && rhsBaseType != lhsRootType)
                            resultBaseType = lhsRootType;
                        else
                            resultBaseType = lhsBaseType;
                    }
                    else    // rhsType.IsEnum
                    {
                        // special case for 0 - E
                        // in all cases 0 becomes E, use E - E
                        resultRootType = rhsRootType;
                        if (isZero)
                            resultBaseType = rhsRootType;
                        else
                            resultBaseType = rhsBaseType;
                    }
                    resultIsNullable = (lhsNullable || rhsNullable);
                    resultType = (resultIsNullable) ? typeof(Nullable<>).MakeGenericType(resultBaseType) : resultBaseType;
                    break;
                case CodeBinaryOperatorType.ValueEquality:
                case CodeBinaryOperatorType.LessThan:
                case CodeBinaryOperatorType.LessThanOrEqual:
                case CodeBinaryOperatorType.GreaterThan:
                case CodeBinaryOperatorType.GreaterThanOrEqual:
                    resultType = typeof(bool);
                    break;
            }
        }
Beispiel #24
0
 public static EasyExpression Binary(CodeExpression left,
                                     CodeBinaryOperatorType op,
                                     CodeExpression right)
 {
     return(new EasyExpression(new CodeBinaryOperatorExpression(left, op, right)));
 }
		void AppendBinaryOperator(CodeBinaryOperatorType operatorType)
		{
			codeBuilder.Append(" ");
			switch (operatorType) {
				case CodeBinaryOperatorType.BitwiseOr:
					codeBuilder.Append("|");
					break;
			}
			codeBuilder.Append(" ");
		}
Beispiel #26
0
        protected virtual CodeExpression BuildConditionalExpression(
            conditionalContainer container, ItemsChoiceType choiceType)
        {
            //grab the children
            List <CodeExpression> children = new List <CodeExpression>();

            for (int i = 0; i < container.Items.Length; i++)
            {
                if (container.Items[i] is conditionalContainer)
                {
                    children.Add(BuildConditionalExpression((conditionalContainer)container.Items[i],
                                                            container.ItemsElementName[i]));
                }
                else if (container.Items[i] is actionEquals)
                {
                    children.Add(BuildConditionalExpression((actionEquals)container.Items[i]));
                }
                else if (container.Items[i] is valueEquals)
                {
                    children.Add(BuildConditionalExpression((valueEquals)container.Items[i],
                                                            container.ItemsElementName[i]));
                }
                else
                {
                    throw new GatException("Unrecognized conditional item type: " +
                                           container.Items[i].GetType());
                }
            }
            //get the full expression
            CodeExpression expr = null;

            if (choiceType == ItemsChoiceType.none)
            {
                expr = new CodeBinaryOperatorExpression(children[0],
                                                        CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression(false));
                for (int i = 1; i < children.Count; i++)
                {
                    expr = new CodeBinaryOperatorExpression(expr,
                                                            CodeBinaryOperatorType.BooleanAnd,
                                                            new CodeBinaryOperatorExpression(children[i],
                                                                                             CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression(false)));
                }
            }
            else if (choiceType == ItemsChoiceType.all ||
                     choiceType == ItemsChoiceType.any)
            {
                CodeBinaryOperatorType opType = choiceType == ItemsChoiceType.all ?
                                                CodeBinaryOperatorType.BooleanAnd : CodeBinaryOperatorType.BooleanOr;
                expr = children[0];
                for (int i = 1; i < children.Count; i++)
                {
                    expr = new CodeBinaryOperatorExpression(expr,
                                                            CodeBinaryOperatorType.BooleanOr, children[i]);
                }
            }
            else
            {
                throw new GatException("Unrecognized choice type: " + choiceType);
            }
            return(expr);
        }
Beispiel #27
0
 protected virtual void OutputOperator(CodeBinaryOperatorType op)
 {
     switch (op)
     {
         case CodeBinaryOperatorType.Add:
             Output.Write('+');
             break;
         case CodeBinaryOperatorType.Subtract:
             Output.Write('-');
             break;
         case CodeBinaryOperatorType.Multiply:
             Output.Write('*');
             break;
         case CodeBinaryOperatorType.Divide:
             Output.Write('/');
             break;
         case CodeBinaryOperatorType.Modulus:
             Output.Write('%');
             break;
         case CodeBinaryOperatorType.Assign:
             Output.Write('=');
             break;
         case CodeBinaryOperatorType.IdentityInequality:
             Output.Write("!=");
             break;
         case CodeBinaryOperatorType.IdentityEquality:
             Output.Write("==");
             break;
         case CodeBinaryOperatorType.ValueEquality:
             Output.Write("==");
             break;
         case CodeBinaryOperatorType.BitwiseOr:
             Output.Write('|');
             break;
         case CodeBinaryOperatorType.BitwiseAnd:
             Output.Write('&');
             break;
         case CodeBinaryOperatorType.BooleanOr:
             Output.Write("||");
             break;
         case CodeBinaryOperatorType.BooleanAnd:
             Output.Write("&&");
             break;
         case CodeBinaryOperatorType.LessThan:
             Output.Write('<');
             break;
         case CodeBinaryOperatorType.LessThanOrEqual:
             Output.Write("<=");
             break;
         case CodeBinaryOperatorType.GreaterThan:
             Output.Write('>');
             break;
         case CodeBinaryOperatorType.GreaterThanOrEqual:
             Output.Write(">=");
             break;
     }
 }
Beispiel #28
0
 private CodeExpression BinaryOperator(CodeBinaryOperatorType codeBinaryOperatorType, CodeExpression paramThis, CodeExpression[] arguments)
 {
     return(new CodeBinaryOperatorExpression(arguments[0], codeBinaryOperatorType, arguments[1]));
 }
Beispiel #29
0
 public static CodeBinaryOperatorExpression oper(CodeExpression left, CodeBinaryOperatorType operatorType, CodeExpression right)
 {
     return(new CodeBinaryOperatorExpression(left, operatorType, right));
 }
Beispiel #30
0
 /// <include file='doc\CodeBinaryOperatorExpression.uex' path='docs/doc[@for="CodeBinaryOperatorExpression.CodeBinaryOperatorExpression1"]/*' />
 /// <devdoc>
 ///    <para>
 ///       Initializes a new instance of <see cref='System.CodeDom.CodeBinaryOperatorExpression'/>
 ///       using the specified
 ///       parameters.
 ///    </para>
 /// </devdoc>
 public CodeBinaryOperatorExpression(CodeExpression left, CodeBinaryOperatorType op, CodeExpression right)
 {
     Right    = right;
     Operator = op;
     Left     = left;
 }
 public CodeBinaryOperatorExpression(CodeExpression left, CodeBinaryOperatorType op, CodeExpression right)
 {
 }
        private void OutputOperator(CodeBinaryOperatorType op)
        {
            switch (op)
            {
                case CodeBinaryOperatorType.Add:
                    this.Output.Write("+");
                    return;

                case CodeBinaryOperatorType.Subtract:
                    this.Output.Write("-");
                    return;

                case CodeBinaryOperatorType.Multiply:
                    this.Output.Write("*");
                    return;

                case CodeBinaryOperatorType.Divide:
                    this.Output.Write("/");
                    return;

                case CodeBinaryOperatorType.Modulus:
                    this.Output.Write("%");
                    return;

                case CodeBinaryOperatorType.Assign:
                    this.Output.Write("=");
                    return;

                case CodeBinaryOperatorType.IdentityInequality:
                    this.Output.Write("!=");
                    return;

                case CodeBinaryOperatorType.IdentityEquality:
                    this.Output.Write("==");
                    return;

                case CodeBinaryOperatorType.ValueEquality:
                    this.Output.Write("==");
                    return;

                case CodeBinaryOperatorType.BitwiseOr:
                    this.Output.Write("|");
                    return;

                case CodeBinaryOperatorType.BitwiseAnd:
                    this.Output.Write("&");
                    return;

                case CodeBinaryOperatorType.BooleanOr:
                    this.Output.Write("||");
                    return;

                case CodeBinaryOperatorType.BooleanAnd:
                    this.Output.Write("&&");
                    return;

                case CodeBinaryOperatorType.LessThan:
                    this.Output.Write("<");
                    return;

                case CodeBinaryOperatorType.LessThanOrEqual:
                    this.Output.Write("<=");
                    return;

                case CodeBinaryOperatorType.GreaterThan:
                    this.Output.Write(">");
                    return;

                case CodeBinaryOperatorType.GreaterThanOrEqual:
                    this.Output.Write(">=");
                    return;
            }
        }
Beispiel #33
0
 private void Write(CodeBinaryOperatorType e){
   string op = "";
   switch (e){
     case CodeBinaryOperatorType.Add: op = "+"; break;
     case CodeBinaryOperatorType.Assign: op = "="; break;
     case CodeBinaryOperatorType.BitwiseAnd: op = "&"; break;
     case CodeBinaryOperatorType.BitwiseOr: op = "|"; break;
     case CodeBinaryOperatorType.BooleanAnd: op = "&&"; break;
     case CodeBinaryOperatorType.BooleanOr: op = "||"; break;
     case CodeBinaryOperatorType.Divide: op = "/"; break;
     case CodeBinaryOperatorType.GreaterThan: op = ">"; break;
     case CodeBinaryOperatorType.GreaterThanOrEqual: op = ">="; break;
     case CodeBinaryOperatorType.IdentityEquality: op = "=="; break;
     case CodeBinaryOperatorType.IdentityInequality: op = "!="; break;
     case CodeBinaryOperatorType.LessThan: op = "<"; break;
     case CodeBinaryOperatorType.LessThanOrEqual: op = "<="; break;
     case CodeBinaryOperatorType.Modulus: op = "%"; break;
     case CodeBinaryOperatorType.Multiply: op = "*"; break;
     case CodeBinaryOperatorType.Subtract: op = "-"; break;
     case CodeBinaryOperatorType.ValueEquality: op = "=="; break;
   }
   this.writer.Write(op);
 }
 public RuleEvaluationIncompatibleTypesException(string message, Type left, CodeBinaryOperatorType op, Type right, Exception ex) : base(message, ex)
 {
     this.m_leftType = left;
     this.m_op = op;
     this.m_rightType = right;
 }
Beispiel #35
0
		protected override void OutputOperator(CodeBinaryOperatorType op)
		{
			if (op == CodeBinaryOperatorType.StringConcat)
				base.Output.Write("~");
			else
				base.OutputOperator(op);
		}
Beispiel #36
0
 private static CodeAssignStatement IncDecStmt(CodeVariableReferenceExpression exp, CodeBinaryOperatorType op) =>
 Assign(exp, BinOpExp(exp, op, new CodePrimitiveExpression(1)));
Beispiel #37
0
 // creates:
 // varName = (type) var1 (OP) var2
 public static CodeAssignStatement CreateBinaryOperatorStatementWithCast(string varName, Type type,
                                                                         object var1, CodeBinaryOperatorType op, object var2)
 {
     return(new CodeAssignStatement(new CodeVariableReferenceExpression(varName),
                                    new CodeCastExpression(type, CreateBinaryOperatorExpression(var1, op, var2))));
 }
        public void Ctor_CodeExpression_CodeBinaryOperatorType_CodeExpression(CodeExpression left, CodeBinaryOperatorType operatorType, CodeExpression right)
        {
            var binaryOperator = new CodeBinaryOperatorExpression(left, operatorType, right);

            Assert.Equal(left, binaryOperator.Left);
            Assert.Equal(operatorType, binaryOperator.Operator);
            Assert.Equal(right, binaryOperator.Right);
        }
 public RuleEvaluationIncompatibleTypesException(string message, Type left, CodeBinaryOperatorType op, Type right, Exception ex) : base(message, ex)
 {
     this.m_leftType  = left;
     this.m_op        = op;
     this.m_rightType = right;
 }
        public EnumOperationMethodInfo(Type lhs, CodeBinaryOperatorType operation, Type rhs, bool isZero)
        {
            this.op = operation;
            this.expectedParameters = new ParameterInfo[] { new SimpleParameterInfo(lhs), new SimpleParameterInfo(rhs) };
            bool flag  = ConditionHelper.IsNullableValueType(lhs);
            bool flag2 = ConditionHelper.IsNullableValueType(rhs);

            this.lhsBaseType = flag ? Nullable.GetUnderlyingType(lhs) : lhs;
            this.rhsBaseType = flag2 ? Nullable.GetUnderlyingType(rhs) : rhs;
            if (this.lhsBaseType.IsEnum)
            {
                this.lhsRootType = EnumHelper.GetUnderlyingType(this.lhsBaseType);
            }
            else
            {
                this.lhsRootType = this.lhsBaseType;
            }
            if (this.rhsBaseType.IsEnum)
            {
                this.rhsRootType = EnumHelper.GetUnderlyingType(this.rhsBaseType);
            }
            else
            {
                this.rhsRootType = this.rhsBaseType;
            }
            switch (this.op)
            {
            case CodeBinaryOperatorType.Add:
                if (!this.lhsBaseType.IsEnum || !rhs.IsEnum)
                {
                    if (this.lhsBaseType.IsEnum)
                    {
                        this.resultBaseType = this.lhsBaseType;
                    }
                    else
                    {
                        this.resultBaseType = this.rhsBaseType;
                    }
                    break;
                }
                this.resultBaseType = this.lhsRootType;
                break;

            case CodeBinaryOperatorType.Subtract:
                if (!this.rhsBaseType.IsEnum || !this.lhsBaseType.IsEnum)
                {
                    if (this.lhsBaseType.IsEnum)
                    {
                        this.resultRootType = this.lhsRootType;
                        if (isZero && (this.rhsBaseType != this.lhsRootType))
                        {
                            this.resultBaseType = this.lhsRootType;
                        }
                        else
                        {
                            this.resultBaseType = this.lhsBaseType;
                        }
                    }
                    else
                    {
                        this.resultRootType = this.rhsRootType;
                        if (isZero)
                        {
                            this.resultBaseType = this.rhsRootType;
                        }
                        else
                        {
                            this.resultBaseType = this.rhsBaseType;
                        }
                    }
                }
                else
                {
                    this.resultRootType = this.rhsRootType;
                    this.resultBaseType = this.rhsRootType;
                }
                this.resultIsNullable = flag || flag2;
                this.resultType       = this.resultIsNullable ? typeof(Nullable <>).MakeGenericType(new Type[] { this.resultBaseType }) : this.resultBaseType;
                return;

            case CodeBinaryOperatorType.ValueEquality:
            case CodeBinaryOperatorType.LessThan:
            case CodeBinaryOperatorType.LessThanOrEqual:
            case CodeBinaryOperatorType.GreaterThan:
            case CodeBinaryOperatorType.GreaterThanOrEqual:
                this.resultType = typeof(bool);
                return;

            case CodeBinaryOperatorType.BitwiseOr:
            case CodeBinaryOperatorType.BitwiseAnd:
            case CodeBinaryOperatorType.BooleanOr:
            case CodeBinaryOperatorType.BooleanAnd:
                return;

            default:
                return;
            }
            this.resultIsNullable = flag || flag2;
            this.resultType       = this.resultIsNullable ? typeof(Nullable <>).MakeGenericType(new Type[] { this.resultBaseType }) : this.resultBaseType;
        }
 /// <devdoc>
 ///    <para>
 ///       Initializes a new instance of <see cref='System.CodeDom.CodeBinaryOperatorExpression'/>
 ///       using the specified
 ///       parameters.
 ///    </para>
 /// </devdoc>
 public CodeBinaryOperatorExpression(CodeExpression left, CodeBinaryOperatorType op, CodeExpression right) {
     Right = right;
     Operator = op;
     Left = left;
 }
Beispiel #42
0
		protected virtual void OutputOperator (CodeBinaryOperatorType op)
		{
			switch (op) {
			case CodeBinaryOperatorType.Add:
				output.Write ("+");
				break;
			case CodeBinaryOperatorType.Subtract:
				output.Write ("-");
				break;
			case CodeBinaryOperatorType.Multiply:
				output.Write ("*");
				break;
			case CodeBinaryOperatorType.Divide:
				output.Write ("/");
				break;
			case CodeBinaryOperatorType.Modulus:
				output.Write ("%");
				break;
			case CodeBinaryOperatorType.Assign:
				output.Write ("=");
				break;
			case CodeBinaryOperatorType.IdentityInequality:
				output.Write ("!=");
				break;
			case CodeBinaryOperatorType.IdentityEquality:
				output.Write ("==");
				break;
			case CodeBinaryOperatorType.ValueEquality:
				output.Write ("==");
				break;
			case CodeBinaryOperatorType.BitwiseOr:
				output.Write ("|");
				break;
			case CodeBinaryOperatorType.BitwiseAnd:
				output.Write ("&");
				break;
			case CodeBinaryOperatorType.BooleanOr:
				output.Write ("||");
				break;
			case CodeBinaryOperatorType.BooleanAnd:
				output.Write ("&&");
				break;
			case CodeBinaryOperatorType.LessThan:
				output.Write ("<");
				break;
			case CodeBinaryOperatorType.LessThanOrEqual:
				output.Write ("<=");
				break;
			case CodeBinaryOperatorType.GreaterThan:
				output.Write (">");
				break;
			case CodeBinaryOperatorType.GreaterThanOrEqual:
				output.Write (">=");
				break;
			}
		}
Beispiel #43
0
        void TSC_VerifyViewSize(ScrollDirection direction, CodeBinaryOperatorType codeType, double CompareValue, CheckType ct)
        {
            double viewSize = 0;
            switch (direction)
            {
                case ScrollDirection.Horizontal:
                    {
                        viewSize = pattern_getHorizontalViewSize;
                        Comment("Current HorizontalViewSize = " + viewSize + "(" + viewSize.GetType().ToString() + ")");
                        switch (codeType)
                        {
                            case CodeBinaryOperatorType.GreaterThan:
                                {
                                    if (viewSize <= CompareValue)
                                        ThrowMe(ct);

                                    break;
                                }

                            case CodeBinaryOperatorType.LessThan:
                                {
                                    if (viewSize >= CompareValue)
                                        ThrowMe(ct);
                                    break;
                                }

                            case CodeBinaryOperatorType.IdentityEquality:
                                {
                                    if (Math.Abs(viewSize - CompareValue) > _ACCURACY)
                                        ThrowMe(ct);
                                    break;
                                }

                            default:
                                {
                                    throw new NotImplementedException();
                                }
                        }
                    }
                    break;

                case ScrollDirection.Vertical:
                    {
                        viewSize = pattern_getVerticalViewSize;
                        Comment("Current VerticalViewSize = " + viewSize + "(" + viewSize.GetType().ToString() + ")");
                        switch (codeType)
                        {
                            case CodeBinaryOperatorType.GreaterThan:
                                {
                                    if (viewSize <= CompareValue)
                                        ThrowMe(ct);

                                    break;
                                }

                            case CodeBinaryOperatorType.LessThan:
                                {
                                    if (viewSize >= CompareValue)
                                        ThrowMe(ct);

                                    break;
                                }

                            case CodeBinaryOperatorType.IdentityEquality:
                                {
                                    if (Math.Abs(viewSize - CompareValue) > _ACCURACY)
                                        ThrowMe(ct);
                                    break;
                                }

                            default:
                                {
                                    throw new NotImplementedException();
                                }
                        }
                    }
                    break;
            }
            m_TestStep++;
        }
Beispiel #44
0
        protected override void OutputOperator(CodeBinaryOperatorType op)
        {
            switch (op)
            {
                case CodeBinaryOperatorType.Modulus:
                    base.Output.Write("Mod");
                    return;

                case CodeBinaryOperatorType.IdentityInequality:
                    base.Output.Write("<>");
                    return;

                case CodeBinaryOperatorType.IdentityEquality:
                    base.Output.Write("Is");
                    return;

                case CodeBinaryOperatorType.ValueEquality:
                    base.Output.Write("=");
                    return;

                case CodeBinaryOperatorType.BitwiseOr:
                    base.Output.Write("Or");
                    return;

                case CodeBinaryOperatorType.BitwiseAnd:
                    base.Output.Write("And");
                    return;

                case CodeBinaryOperatorType.BooleanOr:
                    base.Output.Write("OrElse");
                    return;

                case CodeBinaryOperatorType.BooleanAnd:
                    base.Output.Write("AndAlso");
                    return;
            }
            base.OutputOperator(op);
        }
Beispiel #45
0
        private CodeExpression GenerateValueExpressionBinaryOperation(ExpressionNode node, ref CodeTypeReference exprType)
        {
            ThrowIfNull(node);

            if (node.LeftNode == null || node.RightNode == null)
            {
                throw new InvalidOperationException("Error generating operation");
            }

            if (node.Token.TokenType == TokenType.OpShiftLeft || node.Token.TokenType == TokenType.OpShiftRight)
            {
                // Shift operations are not native supported by the CodeDom so we need to create a special CodeDom node here
                return(GenerateValueExpressionShift(node, ref exprType));
            }

            CodeBinaryOperatorType type = default(CodeBinaryOperatorType);

            switch (node.Token.TokenType)
            {
            case TokenType.OpBoolAnd:
                type = CodeBinaryOperatorType.BooleanAnd;
                break;

            case TokenType.OpBoolOr:
                type = CodeBinaryOperatorType.BooleanOr;
                break;

            case TokenType.OpDivide:
                type = CodeBinaryOperatorType.Divide;
                break;

            case TokenType.OpGreaterThan:
                type = CodeBinaryOperatorType.GreaterThan;
                break;

            case TokenType.OpGreaterThanOrEqual:
                type = CodeBinaryOperatorType.GreaterThanOrEqual;
                break;

            case TokenType.OpLessThan:
                type = CodeBinaryOperatorType.LessThan;
                break;

            case TokenType.OpLessThanOrEqual:
                type = CodeBinaryOperatorType.LessThanOrEqual;
                break;

            case TokenType.OpMinus:
                type = CodeBinaryOperatorType.Subtract;
                break;

            case TokenType.OpModulus:
                type = CodeBinaryOperatorType.Modulus;
                break;

            case TokenType.OpPlus:
                type = CodeBinaryOperatorType.Add;
                break;

            case TokenType.Asterisk:
                type = CodeBinaryOperatorType.Multiply;
                break;

            case TokenType.Pipe:
                type = CodeBinaryOperatorType.BitwiseOr;
                break;

            case TokenType.Ampersand:
                type = CodeBinaryOperatorType.BitwiseAnd;
                break;

            default:
                throw new InvalidOperationException("Unsupported operation");
            }

            CodeTypeReference leftType  = null;
            CodeTypeReference rightType = null;
            CodeExpression    expr      = new CodeBinaryOperatorExpression(GenerateValueExpressionImpl(node.LeftNode, ref leftType), type, GenerateValueExpressionImpl(node.RightNode, ref rightType));

            exprType = leftType;
            return(expr);
        }
Beispiel #46
0
 /// <summary>
 /// フィールドへの参照を作成します。
 /// </summary>
 /// <param name="target"></param>
 /// <param name="name"></param>
 /// <returns></returns>
 private static ET BinaryOperator(ET l, CodeBinaryOperatorType op, ET r)
 {
   return new BuilderBinaryOperator(l, op, r);
 }
Beispiel #47
0
        [SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")]     // bogus since the casts are in different case statements
        internal static MethodInfo MapOperatorToMethod(
            CodeBinaryOperatorType op,
            Type lhs,
            CodeExpression lhsExpression,
            Type rhs,
            CodeExpression rhsExpression,
            RuleValidation validator,
            out ValidationError error)
        {
            // determine what the method name should be
            string methodName;
            string message;
            OperatorGrouping group;

            switch (op)
            {
                case CodeBinaryOperatorType.ValueEquality:
                    methodName = "op_Equality";
                    group = OperatorGrouping.Equality;
                    break;
                case CodeBinaryOperatorType.GreaterThan:
                    methodName = "op_GreaterThan";
                    group = OperatorGrouping.Relational;
                    break;
                case CodeBinaryOperatorType.GreaterThanOrEqual:
                    methodName = "op_GreaterThanOrEqual";
                    group = OperatorGrouping.Relational;
                    break;
                case CodeBinaryOperatorType.LessThan:
                    methodName = "op_LessThan";
                    group = OperatorGrouping.Relational;
                    break;
                case CodeBinaryOperatorType.LessThanOrEqual:
                    methodName = "op_LessThanOrEqual";
                    group = OperatorGrouping.Relational;
                    break;
                case CodeBinaryOperatorType.Add:
                    methodName = "op_Addition";
                    group = OperatorGrouping.Arithmetic;
                    break;
                case CodeBinaryOperatorType.Subtract:
                    methodName = "op_Subtraction";
                    group = OperatorGrouping.Arithmetic;
                    break;
                case CodeBinaryOperatorType.Multiply:
                    methodName = "op_Multiply";
                    group = OperatorGrouping.Arithmetic;
                    break;
                case CodeBinaryOperatorType.Divide:
                    methodName = "op_Division";
                    group = OperatorGrouping.Arithmetic;
                    break;
                case CodeBinaryOperatorType.Modulus:
                    methodName = "op_Modulus";
                    group = OperatorGrouping.Arithmetic;
                    break;
                case CodeBinaryOperatorType.BitwiseAnd:
                    methodName = "op_BitwiseAnd";
                    group = OperatorGrouping.Arithmetic;
                    break;
                case CodeBinaryOperatorType.BitwiseOr:
                    methodName = "op_BitwiseOr";
                    group = OperatorGrouping.Arithmetic;
                    break;
                default:
                    Debug.Assert(false, "Operator " + op.ToString() + " not implemented");
                    message = string.Format(CultureInfo.CurrentCulture, Messages.BinaryOpNotSupported, op.ToString());
                    error = new ValidationError(message, ErrorNumbers.Error_CodeExpressionNotHandled);
                    return null;
            }

            // NOTE: types maybe NullLiteral, which signifies the constant "null"
            List<MethodInfo> candidates = new List<MethodInfo>();
            bool lhsNullable = ConditionHelper.IsNullableValueType(lhs);
            bool rhsNullable = ConditionHelper.IsNullableValueType(rhs);
            Type lhsType0 = (lhsNullable) ? Nullable.GetUnderlyingType(lhs) : lhs;
            Type rhsType0 = (rhsNullable) ? Nullable.GetUnderlyingType(rhs) : rhs;

            // special cases for enums
            if (lhsType0.IsEnum)
            {
                // only 3 cases (U = underlying type of E):
                //    E = E + U
                //    U = E - E
                //    E = E - U
                // plus the standard comparisons (E == E, E > E, etc.)
                // need to also allow E == 0
                Type underlyingType;
                switch (op)
                {
                    case CodeBinaryOperatorType.Add:
                        underlyingType = EnumHelper.GetUnderlyingType(lhsType0);
                        if ((underlyingType != null) &&
                            (RuleValidation.TypesAreAssignable(rhsType0, underlyingType, rhsExpression, out error)))
                        {
                            error = null;
                            return new EnumOperationMethodInfo(lhs, op, rhs, false);
                        }
                        break;
                    case CodeBinaryOperatorType.Subtract:
                        underlyingType = EnumHelper.GetUnderlyingType(lhsType0);
                        if (underlyingType != null)
                        {
                            if (lhsType0 == rhsType0)
                            {
                                // E - E
                                error = null;
                                return new EnumOperationMethodInfo(lhs, op, rhs, false);
                            }
                            else if (DecimalIntegerLiteralZero(rhs, rhsExpression as CodePrimitiveExpression))
                            {
                                // E - 0, can convert 0 to E
                                error = null;
                                return new EnumOperationMethodInfo(lhs, op, rhs, true);
                            }
                            else if (RuleValidation.TypesAreAssignable(rhsType0, underlyingType, rhsExpression, out error))
                            {
                                // expression not passed to TypesAreAssignable, so not looking for constants (since 0 is all we care about)
                                error = null;
                                return new EnumOperationMethodInfo(lhs, op, rhs, false);
                            }
                        }
                        break;
                    case CodeBinaryOperatorType.ValueEquality:
                    case CodeBinaryOperatorType.LessThan:
                    case CodeBinaryOperatorType.LessThanOrEqual:
                    case CodeBinaryOperatorType.GreaterThan:
                    case CodeBinaryOperatorType.GreaterThanOrEqual:
                        if (lhsType0 == rhsType0)
                        {
                            error = null;
                            return new EnumOperationMethodInfo(lhs, op, rhs, false);
                        }
                        else if (lhsNullable && (rhs == typeof(NullLiteral)))
                        {
                            // handle enum? op null
                            // treat the rhs as the same nullable enum
                            error = null;
                            return new EnumOperationMethodInfo(lhs, op, lhs, false);
                        }
                        else if (DecimalIntegerLiteralZero(rhs, rhsExpression as CodePrimitiveExpression))
                        {
                            error = null;
                            return new EnumOperationMethodInfo(lhs, op, rhs, true);
                        }
                        break;
                }
                // can't do it, sorry
                // but check if there is a user-defined operator that works
            }
            else if (rhsType0.IsEnum)
            {
                // lhs != enum, so only 2 cases (U = underlying type of E):
                //    E = U + E
                //    E = U - E
                // comparisons are E == E, etc., so if the lhs is not an enum, too bad
                // although we need to check for 0 == E
                Type underlyingType;
                switch (op)
                {
                    case CodeBinaryOperatorType.Add:
                        underlyingType = EnumHelper.GetUnderlyingType(rhsType0);
                        if ((underlyingType != null) &&
                            (RuleValidation.TypesAreAssignable(lhsType0, underlyingType, lhsExpression, out error)))
                        {
                            error = null;
                            return new EnumOperationMethodInfo(lhs, op, rhs, false);
                        }
                        break;

                    case CodeBinaryOperatorType.Subtract:
                        underlyingType = EnumHelper.GetUnderlyingType(rhsType0);
                        if (underlyingType != null)
                        {
                            CodePrimitiveExpression primitive = lhsExpression as CodePrimitiveExpression;
                            if (DecimalIntegerLiteralZero(lhs, primitive))
                            {
                                // 0 - E, can convert 0 to E
                                error = null;
                                return new EnumOperationMethodInfo(lhs, op, rhs, true);
                            }
                            else if (RuleValidation.TypesAreAssignable(lhsType0, underlyingType, lhsExpression, out error))
                            {
                                // expression not passed to TypesAreAssignable, so not looking for constants (since 0 is all we care about)
                                error = null;
                                return new EnumOperationMethodInfo(lhs, op, rhs, false);
                            }
                        }
                        break;

                    case CodeBinaryOperatorType.ValueEquality:
                    case CodeBinaryOperatorType.LessThan:
                    case CodeBinaryOperatorType.LessThanOrEqual:
                    case CodeBinaryOperatorType.GreaterThan:
                    case CodeBinaryOperatorType.GreaterThanOrEqual:
                        if (rhsNullable && (lhs == typeof(NullLiteral)))
                        {
                            // handle null op enum?
                            // treat the lhs as the same nullable enum type
                            error = null;
                            return new EnumOperationMethodInfo(rhs, op, rhs, false);
                        }
                        else if (DecimalIntegerLiteralZero(lhs, lhsExpression as CodePrimitiveExpression))
                        {
                            error = null;
                            return new EnumOperationMethodInfo(lhs, op, rhs, true);
                        }
                        break;
                }

                // can't do it, sorry
                // but check if there is a user-defined operator that works
            }

            // enum specific operations already handled, see if one side (or both) define operators
            AddOperatorOverloads(lhsType0, methodName, lhs, rhs, candidates);
            AddOperatorOverloads(rhsType0, methodName, lhs, rhs, candidates);
            if (lhsNullable || rhsNullable || (lhs == typeof(NullLiteral)) || (rhs == typeof(NullLiteral)))
            {
                // need to add in lifted methods
                AddLiftedOperators(lhsType0, methodName, group, lhsType0, rhsType0, candidates);
                AddLiftedOperators(rhsType0, methodName, group, lhsType0, rhsType0, candidates);
            }

            if (candidates.Count == 0)
            {
                // no overrides, so get the default list
                methodName = methodName.Substring(3);       // strip off the op_
                foreach (MethodInfo mi in typeof(DefaultOperators).GetMethods())
                {
                    if (mi.Name == methodName)
                    {
                        ParameterInfo[] parameters = mi.GetParameters();
                        Type parm1 = parameters[0].ParameterType;
                        Type parm2 = parameters[1].ParameterType;
                        if (RuleValidation.ImplicitConversion(lhs, parm1) &&
                            RuleValidation.ImplicitConversion(rhs, parm2))
                        {
                            candidates.Add(mi);
                        }
                    }
                }

                // if no candidates and ==, can we use object == object?
                if ((candidates.Count == 0) && ("Equality" == methodName))
                {
                    // C# 7.9.6
                    // references must be compatible
                    // no boxing
                    // value types can't be compared
                    if ((!lhs.IsValueType) && (!rhs.IsValueType))
                    {
                        // they are not classes, so references need to be compatible
                        // also check for null (which is NullLiteral type) -- null is compatible with any object type
                        if ((lhs == typeof(NullLiteral)) || (rhs == typeof(NullLiteral)) ||
                            (lhs.IsAssignableFrom(rhs)) || (rhs.IsAssignableFrom(lhs)))
                        {
                            candidates.Add(ObjectEquality);
                        }
                    }
                }

                // if no candidates and nullable, add lifted operators
                if ((candidates.Count == 0) && ((lhsNullable || rhsNullable || (lhs == typeof(NullLiteral)) || (rhs == typeof(NullLiteral)))))
                {
                    foreach (MethodInfo mi in typeof(DefaultOperators).GetMethods())
                    {
                        if (mi.Name == methodName)
                        {
                            ParameterInfo[] parameters = mi.GetParameters();
                            MethodInfo liftedMethod = EvaluateLiftedMethod(mi, parameters, group, lhsType0, rhsType0);
                            if (liftedMethod != null)
                                candidates.Add(liftedMethod);
                        }
                    }
                }
            }
            if (candidates.Count == 1)
            {
                // only 1, so it is it
                error = null;
                return candidates[0];
            }
            else if (candidates.Count == 0)
            {
                // nothing matched
                message = string.Format(CultureInfo.CurrentCulture,
                    (group == OperatorGrouping.Arithmetic) ? Messages.ArithOpBadTypes : Messages.RelationalOpBadTypes,
                    op.ToString(),
                    (lhs == typeof(NullLiteral)) ? Messages.NullValue : RuleDecompiler.DecompileType(lhs),
                    (rhs == typeof(NullLiteral)) ? Messages.NullValue : RuleDecompiler.DecompileType(rhs));
                error = new ValidationError(message, ErrorNumbers.Error_OperandTypesIncompatible);
                return null;
            }
            else
            {
                // more than 1, so pick the best one
                MethodInfo bestFit = validator.FindBestCandidate(null, candidates, lhs, rhs);
                if (bestFit != null)
                {
                    error = null;
                    return bestFit;
                }
                // must be ambiguous. Since there are at least 2 choices, show only the first 2
                message = string.Format(CultureInfo.CurrentCulture,
                    Messages.AmbiguousOperator,
                    op.ToString(),
                    RuleDecompiler.DecompileMethod(candidates[0]),
                    RuleDecompiler.DecompileMethod(candidates[1]));
                error = new ValidationError(message, ErrorNumbers.Error_OperandTypesIncompatible);
                return null;
            }
        }
Beispiel #48
0
 private static ET EnumOpImpl(CodeBinaryOperatorType opType, ET l, ET[] rights, int index)
 {
   ET r;
   if (index == rights.Length - 1) r = rights[index];
   else r = EnumOpImpl(opType, rights[index], rights, index + 1);
   return new BuilderBinaryOperator(l, opType, r);
 }
Beispiel #49
0
        protected virtual void OutputOperator(CodeBinaryOperatorType op)
        {
            switch (op)
            {
            case CodeBinaryOperatorType.Add:
                output.Write("+");
                break;

            case CodeBinaryOperatorType.Subtract:
                output.Write("-");
                break;

            case CodeBinaryOperatorType.Multiply:
                output.Write("*");
                break;

            case CodeBinaryOperatorType.Divide:
                output.Write("/");
                break;

            case CodeBinaryOperatorType.Modulus:
                output.Write("%");
                break;

            case CodeBinaryOperatorType.Assign:
                output.Write("=");
                break;

            case CodeBinaryOperatorType.IdentityInequality:
                output.Write("!=");
                break;

            case CodeBinaryOperatorType.IdentityEquality:
                output.Write("==");
                break;

            case CodeBinaryOperatorType.ValueEquality:
                output.Write("==");
                break;

            case CodeBinaryOperatorType.BitwiseOr:
                output.Write("|");
                break;

            case CodeBinaryOperatorType.BitwiseAnd:
                output.Write("&");
                break;

            case CodeBinaryOperatorType.BooleanOr:
                output.Write("||");
                break;

            case CodeBinaryOperatorType.BooleanAnd:
                output.Write("&&");
                break;

            case CodeBinaryOperatorType.LessThan:
                output.Write("<");
                break;

            case CodeBinaryOperatorType.LessThanOrEqual:
                output.Write("<=");
                break;

            case CodeBinaryOperatorType.GreaterThan:
                output.Write(">");
                break;

            case CodeBinaryOperatorType.GreaterThanOrEqual:
                output.Write(">=");
                break;
            }
        }
 private void OutputOperator(CodeBinaryOperatorType op) => output.Write(binaryOperatorChars[op]);
Beispiel #51
0
		protected override void OutputOperator (CodeBinaryOperatorType op)
		{
			switch (op) {
			case CodeBinaryOperatorType.Add:
				Output.Write ("+");
				break;
			case CodeBinaryOperatorType.Subtract:
				Output.Write ("-");
				break;
			case CodeBinaryOperatorType.Multiply:
				Output.Write ("*");
				break;
			case CodeBinaryOperatorType.Divide:
				Output.Write ("/");
				break;
			case CodeBinaryOperatorType.Modulus:
				Output.Write ("Mod");
				break;
			case CodeBinaryOperatorType.Assign:
				Output.Write ("=");
				break;
			case CodeBinaryOperatorType.IdentityInequality:
				Output.Write ("<>");
				break;
			case CodeBinaryOperatorType.IdentityEquality:
				Output.Write ("Is");
				break;
			case CodeBinaryOperatorType.ValueEquality:
				Output.Write ("=");
				break;
			case CodeBinaryOperatorType.BitwiseOr:
				Output.Write ("Or");
				break;
			case CodeBinaryOperatorType.BitwiseAnd:
				Output.Write ("And");
				break;
			case CodeBinaryOperatorType.BooleanOr:
				Output.Write ("OrElse");
				break;
			case CodeBinaryOperatorType.BooleanAnd:
				Output.Write ("AndAlso");
				break;
			case CodeBinaryOperatorType.LessThan:
				Output.Write ("<");
				break;
			case CodeBinaryOperatorType.LessThanOrEqual:
				Output.Write ("<=");
				break;
			case CodeBinaryOperatorType.GreaterThan:
				Output.Write (">");
				break;
			case CodeBinaryOperatorType.GreaterThanOrEqual:
				Output.Write (">=");
				break;
			}
		}
 public CodeBinaryOperatorExpression(CodeExpression left, CodeBinaryOperatorType op, CodeExpression right)
 {
     throw new NotImplementedException();
 }
Beispiel #53
0
 public static EasyExpression Binary(CodeExpression left,
     CodeBinaryOperatorType op,
     CodeExpression right)
 {
     return new EasyExpression(new CodeBinaryOperatorExpression(left, op, right));
 }
        private static CodeExpression ApplyBinary(Expression expression, CodeBinaryOperatorType op, Dictionary <Expression, CodeExpression> arguments)
        {
            var divide = expression as BinaryExpression;

            return(new CodeBinaryOperatorExpression(ApplyExpression(divide.Left, arguments), op, ApplyExpression(divide.Right, arguments)));
        }
Beispiel #55
0
 private static ET EnumOp(CodeBinaryOperatorType opType, ET left, params ET[] rights)
 {
   if (rights.Length == 0) return left;
   return EnumOpImpl(opType, left, rights, 0);
 }
Beispiel #56
0
 private static CodeExpression Compare(CodeExpression left, CodeBinaryOperatorType comp, CodeExpression right)
 {
     return(new CodeBinaryOperatorExpression(left, comp, right));
 }
Beispiel #57
0
 private static CodeBinaryOperatorExpression BinOp(CodeExpression left, CodeBinaryOperatorType op,
                                                   CodeExpression right)
 {
     return(new CodeBinaryOperatorExpression(left, op, right));
 }
 /// <inheritdoc cref="OperatorSymbol(CodeDomExt.Utils.CodeBinaryOperatorTypeMore)"/>
 public static string OperatorSymbol(CodeBinaryOperatorType operatorType)
 {
     return(OperatorSymbol(operatorType.AsBinaryOperatorTypeMore()));
 }