Example #1
0
        public static bool TryBind(SyntaxTreeNode node, BindingContext bindingContext, TypeDescription expectedType, out Expression boundExpression, out Exception bindingError)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (bindingContext == null)
            {
                throw new ArgumentNullException("bindingContext");
            }
            if (expectedType == null)
            {
                throw new ArgumentNullException("expectedType");
            }

            boundExpression = null;
            bindingError    = null;

            var expressionType   = node.GetExpressionType(throwOnError: true);
            var left             = node.GetLeftExpression(throwOnError: true);
            var right            = node.GetRightExpression(throwOnError: true);
            var methodName       = node.GetMethodName(throwOnError: false);
            var conversion       = node.GetConversion(throwOnError: false);
            var leftOperand      = default(Expression);
            var rightOperand     = default(Expression);
            var conversionLambda = default(Expression);
            var methodMember     = default(MemberDescription);

            if (AnyBinder.TryBindInNewScope(left, bindingContext, TypeDescription.ObjectType, out leftOperand, out bindingError) == false)
            {
                return(false);
            }
            if (AnyBinder.TryBindInNewScope(right, bindingContext, TypeDescription.ObjectType, out rightOperand, out bindingError) == false)
            {
                return(false);
            }

            if (methodName != null)
            {
                bindingContext.TryResolveMember(methodName, out methodMember);
            }

            if (conversion != null)
            {
                AnyBinder.TryBindInNewScope(conversion, bindingContext, TypeDescription.ObjectType, out conversionLambda, out bindingError);
                bindingError = null;
            }


            Debug.Assert(leftOperand != null, "leftOperand != null");
            Debug.Assert(rightOperand != null, "rightOperand != null");

            switch (expressionType)
            {
            case Constants.EXPRESSION_TYPE_ADD:
                if (leftOperand.Type == typeof(string) || rightOperand.Type == typeof(string))
                {
                    boundExpression = Expression.Call
                                      (
                        StringConcat.GetMethodInfo(),
                        Expression.Convert(leftOperand, typeof(object)),
                        Expression.Convert(rightOperand, typeof(object))
                                      );
                    break;
                }
                else
                {
                    if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.Add, out boundExpression) == false)
                    {
                        boundExpression = Expression.Add(leftOperand, rightOperand);
                    }
                    break;
                }

            case Constants.EXPRESSION_TYPE_ADD_CHECKED:
                if (leftOperand.Type == typeof(string) || rightOperand.Type == typeof(string))
                {
                    boundExpression = Expression.Call
                                      (
                        StringConcat.GetMethodInfo(),
                        Expression.Convert(leftOperand, typeof(object)),
                        Expression.Convert(rightOperand, typeof(object))
                                      );
                    break;
                }
                else
                {
                    if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.AddChecked, out boundExpression) == false)
                    {
                        boundExpression = Expression.AddChecked(leftOperand, rightOperand);
                    }
                    break;
                }

            case Constants.EXPRESSION_TYPE_SUBTRACT:
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.Subtract, out boundExpression) == false)
                {
                    boundExpression = Expression.Subtract(leftOperand, rightOperand, methodMember);
                }
                break;

            case Constants.EXPRESSION_TYPE_SUBTRACT_CHECKED:
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.SubtractChecked, out boundExpression) == false)
                {
                    boundExpression = Expression.SubtractChecked(leftOperand, rightOperand, methodMember);
                }
                break;

            case Constants.EXPRESSION_TYPE_LEFT_SHIFT:
                ExpressionUtils.TryPromoteUnaryOperation(ref leftOperand, ExpressionType.LeftShift, out boundExpression);
                ExpressionUtils.TryPromoteUnaryOperation(ref rightOperand, ExpressionType.LeftShift, out boundExpression);
                boundExpression = Expression.LeftShift(leftOperand, rightOperand, methodMember);
                break;

            case Constants.EXPRESSION_TYPE_RIGHT_SHIFT:
                ExpressionUtils.TryPromoteUnaryOperation(ref leftOperand, ExpressionType.RightShift, out boundExpression);
                ExpressionUtils.TryPromoteUnaryOperation(ref rightOperand, ExpressionType.RightShift, out boundExpression);
                boundExpression = Expression.RightShift(leftOperand, rightOperand, methodMember);
                break;

            case Constants.EXPRESSION_TYPE_GREATER_THAN:
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.GreaterThan, out boundExpression) == false)
                {
                    boundExpression = Expression.GreaterThan(leftOperand, rightOperand);
                }
                break;

            case Constants.EXPRESSION_TYPE_GREATER_THAN_OR_EQUAL:
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.GreaterThanOrEqual, out boundExpression) == false)
                {
                    boundExpression = Expression.GreaterThanOrEqual(leftOperand, rightOperand);
                }
                break;

            case Constants.EXPRESSION_TYPE_LESS_THAN:
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.LessThan, out boundExpression) == false)
                {
                    boundExpression = Expression.LessThan(leftOperand, rightOperand);
                }
                break;

            case Constants.EXPRESSION_TYPE_LESS_THAN_OR_EQUAL:
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.LessThanOrEqual, out boundExpression) == false)
                {
                    boundExpression = Expression.LessThanOrEqual(leftOperand, rightOperand);
                }
                break;

            case Constants.EXPRESSION_TYPE_POWER:
                var resultType       = TypeDescription.GetTypeDescription(leftOperand.Type);
                var resultTypeUnwrap = resultType.IsNullable ? resultType.UnderlyingType : resultType;
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.Power, out boundExpression) == false)
                {
                    var operandsType      = TypeDescription.GetTypeDescription(leftOperand.Type);
                    var operandTypeUnwrap = operandsType.IsNullable ? operandsType.UnderlyingType : operandsType;
                    var promoteToNullable = resultType.IsNullable || operandsType.IsNullable;
                    if (operandTypeUnwrap != typeof(double) && leftOperand.Type == rightOperand.Type)
                    {
                        leftOperand  = Expression.ConvertChecked(leftOperand, promoteToNullable ? typeof(double?) : typeof(double));
                        rightOperand = Expression.ConvertChecked(rightOperand, promoteToNullable ? typeof(double?) : typeof(double));
                    }
                    boundExpression = Expression.Power(leftOperand, rightOperand, methodMember);

                    if (resultType != typeof(double))
                    {
                        boundExpression = Expression.ConvertChecked(boundExpression, promoteToNullable ? resultTypeUnwrap.GetNullableType() : resultTypeUnwrap);
                    }
                }
                break;

            case Constants.EXPRESSION_TYPE_DIVIDE:
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.Divide, out boundExpression) == false)
                {
                    boundExpression = Expression.Divide(leftOperand, rightOperand, methodMember);
                }
                break;

            case Constants.EXPRESSION_TYPE_MULTIPLY:
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.Multiply, out boundExpression) == false)
                {
                    boundExpression = Expression.Multiply(leftOperand, rightOperand, methodMember);
                }
                break;

            case Constants.EXPRESSION_TYPE_MULTIPLY_CHECKED:
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.MultiplyChecked, out boundExpression) == false)
                {
                    boundExpression = Expression.MultiplyChecked(leftOperand, rightOperand, methodMember);
                }
                break;

            case Constants.EXPRESSION_TYPE_MODULO:
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.Modulo, out boundExpression) == false)
                {
                    boundExpression = Expression.Modulo(leftOperand, rightOperand, methodMember);
                }
                break;

            case Constants.EXPRESSION_TYPE_EQUAL:
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.Equal, out boundExpression) == false)
                {
                    boundExpression = Expression.Equal(leftOperand, rightOperand);
                }
                break;

            case Constants.EXPRESSION_TYPE_NOT_EQUAL:
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.NotEqual, out boundExpression) == false)
                {
                    boundExpression = Expression.NotEqual(leftOperand, rightOperand);
                }
                break;

            case Constants.EXPRESSION_TYPE_AND:
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.And, out boundExpression) == false)
                {
                    boundExpression = Expression.And(leftOperand, rightOperand, methodMember);
                }
                break;

            case Constants.EXPRESSION_TYPE_OR:
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.Or, out boundExpression) == false)
                {
                    boundExpression = Expression.Or(leftOperand, rightOperand, methodMember);
                }
                break;

            case Constants.EXPRESSION_TYPE_EXCLUSIVE_OR:
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.ExclusiveOr, out boundExpression) == false)
                {
                    boundExpression = Expression.ExclusiveOr(leftOperand, rightOperand, methodMember);
                }
                break;

            case Constants.EXPRESSION_TYPE_AND_ALSO:
                boundExpression = Expression.AndAlso(leftOperand, rightOperand, methodMember);
                break;

            case Constants.EXPRESSION_TYPE_OR_ELSE:
                boundExpression = Expression.OrElse(leftOperand, rightOperand, methodMember);
                break;

            case Constants.EXPRESSION_TYPE_COALESCE:
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.Coalesce, out boundExpression) == false)
                {
                    boundExpression = Expression.Coalesce(leftOperand, rightOperand, conversionLambda as LambdaExpression);
                }
                break;

            default:
                bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNKNOWNEXPRTYPE, expressionType), node);
                return(false);
            }
            return(true);
        }