Inheritance: Expression, IArgumentProvider
 protected override Expression VisitInvocation(InvocationExpression iv)
 {
     if (iv.Expression is LambdaExpression)
         return Visit(ExpressionReplacer.Replace(iv));
     else
         return base.VisitInvocation(iv); //Just calling a delegate in the projector
 }
 protected InvocationExpression UpdateInvocation(InvocationExpression iv, Expression expression, IEnumerable<Expression> args)
 {
     if (args != iv.Arguments || expression != iv.Expression) {
         return Expression.Invoke(expression, args);
     }
     return iv;
 }
Esempio n. 3
0
        /// <summary>
        /// Flatten calls to Invoke so that Entity Framework can understand it. Calls to Invoke are generated
        /// by PredicateBuilder.
        /// </summary>
        protected override Expression VisitInvocation(InvocationExpression iv)
        {
            Expression target = iv.Expression;
            if (target is MemberExpression) target = TransformExpr ((MemberExpression)target);
            if (target is ConstantExpression) target = ((ConstantExpression)target).Value as Expression;

            LambdaExpression lambda = (LambdaExpression)target;

            Dictionary<ParameterExpression, Expression> replaceVars;
            if (_replaceVars == null)
                replaceVars = new Dictionary<ParameterExpression, Expression> ();
            else
                replaceVars = new Dictionary<ParameterExpression, Expression> (_replaceVars);

            try
            {
                for (int i = 0; i < lambda.Parameters.Count; i++)
                    replaceVars.Add (lambda.Parameters[i], iv.Arguments[i]);
            }
            catch (ArgumentException ex)
            {
                throw new InvalidOperationException ("Invoke cannot be called recursively - try using a temporary variable.", ex);
            }

            return new ExpressionExpander (replaceVars).Visit (lambda.Body);
        }
Esempio n. 4
0
        private ExpressionNode ParseInvocation(InvocationExpression ie)
        {
            var args = new List<LiteralNode>();
            foreach(var arg in ie.Arguments)
            {
                if (arg is ParameterExpression)
                {
                    var name = ((ParameterExpression)arg).Name;
                    args.Add(new VariableNode(name));
                }
                else if (arg is ConstantExpression)
                {
                    var token = (String)((ConstantExpression)arg).Value;
                    args.Add(new ConstantNode(token));
                }
                else
                {
                    return Unsupported(arg);
                }
            }

            if (ie.Expression is MemberExpression)
            {
                var term = GetTermFromMemberExpression((MemberExpression)ie.Expression);
                return new TermNode(term, args.ToArray());
            }

            return Unsupported(ie);
        }
Esempio n. 5
0
        private static void AssertInvokeIsOptimized(InvocationExpression expr, Expression expression, IReadOnlyList<Expression> args)
        {
            var n = args.Count;

            var updated = Update(expr);
            var visited = Visit(expr);

            foreach (var node in new[] { expr, updated, visited })
            {
                Assert.Same(expression, node.Expression);

                AssertInvocation(n, node);

                var argProvider = node as IArgumentProvider;
                Assert.NotNull(argProvider);

                Assert.Equal(n, argProvider.ArgumentCount);

                if (node != visited) // our visitor clones argument nodes
                {
                    for (var i = 0; i < n; i++)
                    {
                        Assert.Same(args[i], argProvider.GetArgument(i));
                        Assert.Same(args[i], node.Arguments[i]);
                    }
                }
            }
        }
 private void DoVisit(InvocationExpression invocation)
 {
     foreach (var arg in invocation.Arguments)
     {
         Visit(arg);
     }
     Visit(invocation.Expression);
 }
 protected override Expression VisitInvocation(InvocationExpression node)
 {
     if (typeof(Delegate).IsAssignableFrom(node.Expression.Type) || IsObservableFuncType(node.Expression.Type, node.Arguments.Count))
     {
         return VisitMethodCall(Expression.Call(node.Expression, node.Expression.Type.GetMethod("Invoke"), node.Arguments));
     }
     throw new InvalidOperationException("Unclear what to invoke.");
 }
 protected override Expression VisitInvocation(InvocationExpression node)
 {
     if (typeof(Delegate).GetTypeInfo().IsAssignableFrom(node.Expression.Type.GetTypeInfo()) || IsObservableFuncType(node.Expression.Type, node.Arguments.Count))
     {
         var method = node.Expression.Type.GetRuntimeMethods().FirstOrDefault(m => m.Name == "Invoke");
         return VisitMethodCall(Expression.Call(node.Expression, method, node.Arguments));
     }
     throw new InvalidOperationException("Unclear what to invoke.");
 }
Esempio n. 9
0
 protected static InvocationExpression UpdateInvocation(InvocationExpression node, Expression expression,
                                                        IEnumerable<Expression> args)
 {
     if (node.Arguments != args || node.Expression != expression)
     {
         return Expression.Invoke(expression, args);
     }
     return node;
 }
Esempio n. 10
0
        /// <summary>
        /// Initializes a new instance of the <see cref="InvocationExpectation"/> class.
        /// </summary>
        /// <param name="lambdaExpression">The lambda expression.</param>
        public InvocationExpectation(LambdaExpression lambdaExpression)
        {
            var expression = lambdaExpression.Body as InvocationExpression;

            if (expression == null)
                throw new ArgumentException("Invalid lambda expression", "lambdaExpression");

            _expression = expression;
        }
        public static Expression Replace(InvocationExpression invocation)
        {
            LambdaExpression lambda = invocation.Expression as LambdaExpression;
            var replacer = new ExpressionReplacer()
            {
                replacements = 0.To(lambda.Parameters.Count).ToDictionaryEx(i => lambda.Parameters[i], i => invocation.Arguments[i])
            };

            return replacer.Visit(lambda.Body); 
        }
        protected override Expression VisitInvocation(InvocationExpression node)
        {
            var args = Visit(node.Arguments);
            foreach(var arg in args)
                AddParameter(arg);

            var expr = Visit(node.Expression);
            AddParameter(expr);
            return null;
        }
        private static ParsedCallExpression ParseInvokationExpression(InvocationExpression expression)
        {
            var target = Helpers.GetValueProducedByExpression(expression.Expression);
            var method = target.GetType().GetMethod("Invoke");

            return new ParsedCallExpression(
                calledMethod: method,
                callTargetExpression: expression.Expression,
                argumentsExpressions: from argument in expression.Arguments.Zip(method.GetParameters(), (x, y) => new { Expression = x, ParameterInfo = y })
                                      select new ParsedArgumentExpression(argument.Expression, argument.ParameterInfo));
        }
 protected override Expression VisitInvocation(InvocationExpression node)
 {
     ConstantExpression varNameExpr;
     ParameterExpression contextExpr;
     if ((contextExpr = node.Expression as ParameterExpression) != null &&
         contextExpr.Name == _contextVarName &&
         node.Arguments.Count == 1 &&
         (varNameExpr = node.Arguments[0] as ConstantExpression) != null)
     {
         Variables.Add((string)varNameExpr.Value);
     }
     return base.VisitInvocation(node);
 }
Esempio n. 15
0
		internal Expression AutoInline(InvocationExpression expression)
		{
			this.isLocked = true;
			if (expression == null) throw new ArgumentNullException("expression");
			var lambda = (LambdaExpression) expression.Expression;
			var childScope = new ExpressionRewriter(this);
			var lambdaParams = lambda.Parameters;
			var invokeArgs = expression.Arguments;
			if (lambdaParams.Count != invokeArgs.Count) throw new InvalidOperationException("Lambda/invoke mismatch");
			for (int i = 0; i < lambdaParams.Count; i++)
			{
				childScope.Subst(lambdaParams[i], invokeArgs[i]);
			}
			return childScope.Apply(lambda.Body);
		}
Esempio n. 16
0
        protected override Expression VisitInvocation(InvocationExpression node)
        {
            var baseResult = (InvocationExpression)base.VisitInvocation(node);

            if (baseResult.Expression.NodeType == ExpressionType.Call)
            {
                var methodCallExpression = (MethodCallExpression)baseResult.Expression;

                if (IsCompileMethod(methodCallExpression.Method))
                {
                    Expression result;
                    if (TrySubstituteExpression(methodCallExpression.Object, baseResult.Arguments, out result))
                        return result;
                }
            }

            return baseResult;
        }
Esempio n. 17
0
 protected virtual Expression VisitInvocation(InvocationExpression iv)
 {
     IEnumerable<Expression> args = this.VisitExpressionList(iv.Arguments);
     Expression expr = this.Visit(iv.Expression);
     return this.UpdateInvocation(iv, expr, args);
 }
Esempio n. 18
0
 /// <summary>
 /// InvocationExpression visit method
 /// </summary>
 /// <param name="iv">The InvocationExpression to visit</param>
 /// <returns>The visited InvocationExpression</returns>
 internal override Expression VisitInvocation(InvocationExpression iv)
 {
     this.cantTranslateExpression = true;
     return iv;
 }
		protected virtual Expression VisitInvocation (InvocationExpression invocation)
		{
			Expression[] args;
			bool changed = VisitExpressionList (invocation.Arguments, out args);

			Expression e = Visit (invocation.Expression);
			changed = (e != invocation.Expression) || changed;

			if (changed)
				return Expression.Invoke (e, args);

			return invocation;
		}
 /// <summary>
 /// Visits an InvocationExpression.
 /// </summary>
 /// <param name="node">The InvocationExpression.</param>
 /// <returns>The InvocationExpression.</returns>
 protected override Expression VisitInvocation(InvocationExpression node)
 {
     throw new NotImplementedException();
 }
Esempio n. 21
0
 private static string VisitInvocation(InvocationExpression node)
 {
     throw new NotImplementedException();
 }
 /// <summary>
 /// Visits an InvocationExpression.
 /// </summary>
 /// <param name="iv">The InvocationExpression.</param>
 /// <returns>The InvocationExpression (possibly modified).</returns>
 protected virtual Expression VisitInvocation(InvocationExpression iv)
 {
     IEnumerable<Expression> args = this.VisitExpressionList(iv.Arguments);
     Expression expr = this.Visit(iv.Expression);
     if (args != iv.Arguments || expr != iv.Expression)
     {
         return Expression.Invoke(expr, args);
     }
     return iv;
 }
Esempio n. 23
0
 private Expression Convert(LinqExp.InvocationExpression linqInvoke)
 {
     return(Expression.Invoke(
                ConvertExp(linqInvoke.Expression),
                Convert(linqInvoke.Arguments)));
 }
Esempio n. 24
0
 protected override Expression VisitInvocation(InvocationExpression iv)
 {
     this.Write("Invoke(");
     this.WriteLine(Indentation.Inner);
     this.VisitExpressionList(iv.Arguments);
     this.Write(", ");
     this.WriteLine(Indentation.Same);
     this.Visit(iv.Expression);
     this.WriteLine(Indentation.Same);
     this.Write(")");
     this.Indent(Indentation.Outer);
     return iv;
 }
 protected override MSAst.Expression VisitInvocation(MSAst.InvocationExpression node)
 {
     return(VisitCall(base.VisitInvocation(node)));
 }
		private HqlTreeNode VisitInvocationExpression(InvocationExpression expression)
		{
			return VisitExpression(expression.Expression);
		}
        /// <summary>
        /// Visit a invocation expression.
        /// </summary>
        /// <param name="invocationExp"></param>
        /// <returns></returns>
        protected virtual Expression VisitInvocation(InvocationExpression invocationExp)
        {
            IEnumerable<Expression> args = VisitList(invocationExp.Arguments);
            Expression expr = Visit(invocationExp.Expression);

            return args != invocationExp.Arguments || expr != invocationExp.Expression
                       ? Expression.Invoke(expr, args)
                       : invocationExp;
        }
 /// <summary>
 /// InvocationExpression visit method
 /// </summary>
 /// <param name="iv">The InvocationExpression to visit</param>
 /// <returns>The visited InvocationExpression</returns>
 internal override Expression VisitInvocation(InvocationExpression iv)
 {
     throw new NotImplementedException();
 }
 protected virtual System.Linq.Expressions.Expression VisitInvocation(InvocationExpression iv)
 {
     IEnumerable<System.Linq.Expressions.Expression> args = VisitExpressionList(iv.Arguments);
     System.Linq.Expressions.Expression expr = Visit(iv.Expression);
     // ReSharper disable once PossibleUnintendedReferenceComparison
     if(args != iv.Arguments || expr != iv.Expression)
     {
         return System.Linq.Expressions.Expression.Invoke(expr, args);
     }
     return iv;
 }
Esempio n. 30
0
 protected virtual bool CompareInvocation(InvocationExpression a, InvocationExpression b)
 {
     return this.Compare(a.Expression, b.Expression)
         && this.CompareExpressionList(a.Arguments, b.Arguments);
 }
Esempio n. 31
0
            /// <summary>
            /// We are doing an inline call to a lambda expression.
            /// </summary>
            /// <param name="expression"></param>
            /// <returns></returns>
            protected override Expression VisitInvocation(InvocationExpression expression)
            {
                ///
                /// Declare all the parameters for lookup.
                /// 

                if (!(expression.Expression is LambdaExpression))
                    throw new NotImplementedException("Do not know how to invoke a non-lambda call like '" + expression.ToString() + "'");

                var lambda = expression.Expression as LambdaExpression;

                var paramArgs = lambda.Parameters.Zip(expression.Arguments, (p, a) => Tuple.Create(p, a));
                var paramDefineToPopers = from pair in paramArgs
                                          select CodeContext.Add(pair.Item1.Name, pair.Item2);
                var allParamDefineToPopers = paramDefineToPopers.ToArray();

                ///
                /// Do the work. We parse the body of the lambda expression. The references to the parameters should be automatically
                /// dealt with.
                /// 

                var result = lambda.Body.Resolve(GeneratedCode, CodeContext, MEFContainer);

                ///
                /// Now, pop everything off!
                /// 

                foreach (var param in allParamDefineToPopers)
                {
                    param.Pop();
                }

                ///
                /// Done!
                /// 

                return result;
            }
 /// <summary>
 /// Visits an InvocationExpression.
 /// </summary>
 /// <param name="node">The InvocationExpression.</param>
 /// <returns>The InvocationExpression.</returns>
 protected override Expression VisitInvocation(InvocationExpression node)
 {
     _sb.Append("<InvocationExpression>");
     return node;
 }