public static Expression FoldInvocation(InvocationExpression inputExpression)
        {
            IEnumerable <Expression> args = ConstantFolding.FoldExpressionList(inputExpression.Arguments);
            Expression expr = ConstantFolding.Fold(inputExpression.Expression);
            Expression resultExpression;

            if (args != inputExpression.Arguments || expr != inputExpression.Expression)
            {
                resultExpression = Expression.Invoke(expr, args);
            }
            else
            {
                resultExpression = inputExpression;
            }

#if zero
            if (!IsConstant(expr))
            {
                return(retval);
            }
            // lambdas are constant
#endif
            foreach (Expression arg in args)
            {
                if (!IsConstant(arg))
                {
                    return(resultExpression);
                }
            }

            resultExpression = ExpressionSimplifier.EvaluateToExpression(resultExpression);
            return(resultExpression);
        }
        public static ElementInit FoldElementInitializer(ElementInit inputExpression)
        {
            ReadOnlyCollection <Expression> arguments = ConstantFolding.FoldExpressionList(inputExpression.Arguments);

            if (arguments != inputExpression.Arguments)
            {
                return(Expression.ElementInit(inputExpression.AddMethod, arguments));
            }

            return(inputExpression);
        }
        public static Expression FoldNewArray(NewArrayExpression inputExpression)
        {
            IEnumerable <Expression> exprs = ConstantFolding.FoldExpressionList(inputExpression.Expressions);

            if (exprs != inputExpression.Expressions)
            {
                if (inputExpression.NodeType == ExpressionType.NewArrayInit)
                {
                    return(Expression.NewArrayInit(inputExpression.Type.GetElementType(), exprs));
                }
                else
                {
                    return(Expression.NewArrayBounds(inputExpression.Type.GetElementType(), exprs));
                }
            }

            return(inputExpression);
        }
        public static NewExpression FoldNew(NewExpression inputExpression)
        {
            IEnumerable <Expression> args = ConstantFolding.FoldExpressionList(inputExpression.Arguments);

            if (args != inputExpression.Arguments)
            {
                if (inputExpression.Members != null)
                {
                    return(Expression.New(inputExpression.Constructor, args, inputExpression.Members));
                }
                else
                {
                    return(Expression.New(inputExpression.Constructor, args));
                }
            }

            return(inputExpression);
        }
        public static Expression FoldMethodCall(MethodCallExpression inputExpression)
        {
            Expression obj = ConstantFolding.Fold(inputExpression.Object);
            ReadOnlyCollection <Expression> args = ConstantFolding.FoldExpressionList(inputExpression.Arguments);
            Expression resultExpression;

            if (obj != inputExpression.Object || args != inputExpression.Arguments)
            {
                resultExpression = Expression.Call(obj, inputExpression.Method, args);
            }
            else
            {
                resultExpression = inputExpression;
            }

            if (!IsConstant(obj))
            {
                return(resultExpression);
            }

            foreach (Expression arg in args)
            {
                if (!IsConstant(arg))
                {
                    return(resultExpression);
                }
            }

            // skip simplifying .Take(Constant)
            if (inputExpression.Method.IsStatic &&
                inputExpression.Method.DeclaringType.IsAssignableFrom(typeof(Queryable)) &&
                inputExpression.Method.Name.Equals("Take"))
            {
                return(resultExpression);
            }

            resultExpression = ExpressionSimplifier.EvaluateToExpression(resultExpression);
            return(resultExpression);
        }