Ejemplo n.º 1
0
        public override ExpressionNode VisitFunctionInvocationExpression(FunctionInvocationExpression expression)
        {
            base.VisitFunctionInvocationExpression(expression);

            // Constant folding must not be applied if the function is non-deterministic.
            if (!expression.Function.IsDeterministic)
            {
                return(expression);
            }

            // Check if all arguments are constants or at least one argument
            // is null.

            ConstantExpression[] constantArguments = new ConstantExpression[expression.Arguments.Length];
            bool allArgumentsAreConstants          = true;

            for (int i = 0; i < constantArguments.Length; i++)
            {
                constantArguments[i] = expression.Arguments[i] as ConstantExpression;

                if (constantArguments[i] == null)
                {
                    // Ok, one argument is not a constant
                    // But don't stop: If an argument is null we can still calculate the result!
                    allArgumentsAreConstants = false;
                }
                else if (constantArguments[i].IsNullValue)
                {
                    // We found a null. That means the invocation will also yield null.
                    return(LiteralExpression.FromTypedNull(expression.ExpressionType));
                }
            }

            if (allArgumentsAreConstants)
            {
                try
                {
                    return(LiteralExpression.FromTypedValue(expression.GetValue(), expression.ExpressionType));
                }
                catch (RuntimeException ex)
                {
                    _errorReporter.CannotFoldConstants(ex);
                }
            }

            return(expression);
        }
Ejemplo n.º 2
0
		public override ExpressionNode VisitFunctionInvocationExpression(FunctionInvocationExpression expression)
		{
			base.VisitFunctionInvocationExpression(expression);

			// Constant folding must not be applied if the function is non-deterministic.
			if (!expression.Function.IsDeterministic)
				return expression;
			
			// Check if all arguments are constants or at least one argument
			// is null.

			ConstantExpression[] constantArguments = new ConstantExpression[expression.Arguments.Length];
			bool allArgumentsAreConstants = true;

			for (int i = 0; i < constantArguments.Length; i++)
			{
				constantArguments[i] = expression.Arguments[i] as ConstantExpression;

				if (constantArguments[i] == null)
				{
					// Ok, one argument is not a constant
					// But don't stop: If an argument is null we can still calculate the result!
					allArgumentsAreConstants = false;
				}
				else if (constantArguments[i].IsNullValue)
				{
					// We found a null. That means the invocation will also yield null.
                    return LiteralExpression.FromTypedNull(expression.ExpressionType);
				}
			}

			if (allArgumentsAreConstants)
			{
				try
				{
                    return LiteralExpression.FromTypedValue(expression.GetValue(), expression.ExpressionType);
				}
				catch (RuntimeException ex)
				{
					_errorReporter.CannotFoldConstants(ex);
				}
			}

			return expression;
		}