Class ClassicCpsRewriter
Expression tree rewriter that turns an expression into a Continuation Passing Style (CPS) expression using an Action<T> based callback method.
Inherited Members
Namespace: System.Linq.CompilerServices
Assembly: Nuqleon.Linq.CompilerServices.dll
Syntax
public sealed class ClassicCpsRewriter : CpsRewriterBase<Expression>
Examples
Examples are provided on the various methods, assuming the following methods are defined:
[UseAsyncMethod]
int Add(int a, int b)
{
return a + b;
}
void Add(int a, int b, Action<int> callback)
{
callback(a + b);
}
Constructors
ClassicCpsRewriter()
Declaration
public ClassicCpsRewriter()
Methods
GetContinuationParameters(MethodInfo)
Gets the continuation parameter types that are used during method overload resolution by appending those to the parameters type of the synchronous method.
Declaration
protected override IEnumerable<Type> GetContinuationParameters(MethodInfo method)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.MethodInfo | method | Method to find continuation parameters for, e.g. by inspecting its return type and creating the corresponding callback parameter type. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<System.Type> | Continuation parameter types to check for when searching for an asynchronous method overload. |
Overrides
InvokeContinuation(Expression, Expression)
Creates an invocation of the given continuation.
Declaration
protected override Expression InvokeContinuation(Expression continuation, Expression argument)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression | continuation | Continuation to invoke. |
System.Linq.Expressions.Expression | argument | Argument to pass to the continuation's invocation. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | Expression representing the invocation of the continuation. |
Overrides
MakeAsyncMethodCall(Expression, MethodInfo, IEnumerable<Expression>, Expression)
Creates an asynchronous method call with the given continuation.
Declaration
protected override Expression MakeAsyncMethodCall(Expression instance, MethodInfo method, IEnumerable<Expression> arguments, Expression continuation)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression | instance | Instance of the method call. |
System.Reflection.MethodInfo | method | Method to call. |
System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression> | arguments | Arguments of the method call. |
System.Linq.Expressions.Expression | continuation | Continuation to supply to the asynchronous method. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | Expression representing an asynchronous method call with the given continuation. |
Overrides
MakeContinuation(Expression, ParameterExpression, Expression)
Creates a new continuation with the given result parameter to run the specified remainder computation.
Declaration
protected override Expression MakeContinuation(Expression remainder, ParameterExpression resultParameter, Expression currentContinuation)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression | remainder | Remainder of the expression to evaluate upon invocation of the continuation. |
System.Linq.Expressions.ParameterExpression | resultParameter | Parameter with the result of the expression evaluation preceding the invocation of the continuation. |
System.Linq.Expressions.Expression | currentContinuation | Current continuation being processed upon requesting a new continuation. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | Continuation object that will execute the remainder computation, given the specified result parameter. |
Overrides
Rewrite(Expression)
Rewrites the given expression to a lambda expression that accepts a callback and executes the expression using CPS.
Declaration
public LambdaExpression Rewrite(Expression expression)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression | expression | Expression to rewrite. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.LambdaExpression | Lambda expression that, given a callback, will execute the expression using CPS. |
Examples
This method will rewrite the following expression:
Add(1, Add(Add(2, 3), 4))
into:
callback => Add(2, 3, x0 => Add(x0, 4, x1 => Add(1, x1, callback)))
Evaluation order of side-effects is preserved. The following expression:
Add(Foo(1), Add(Add(2, 3), 4))
will be rewritten into:
callback => Invoke(x0 => Add(2, 3, x1 => Add(x1, 4, x2 => Add(x0, x2, callback))), Foo(1))
Rewrite(Expression, Expression)
Rewrites the given expression to a CPS expression using the specified callback.
Declaration
public Expression Rewrite(Expression expression, Expression continuation)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression | expression | Expression to rewrite. |
System.Linq.Expressions.Expression | continuation | Continuation to pass. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | Rewritten expression using CPS. |
Examples
This method will rewrite the following expression:
Add(1, Add(Add(2, 3), 4))
into:
Add(2, 3, x0 => Add(x0, 4, x1 => Add(1, x1, continuation)))
Evaluation order of side-effects is preserved. The following expression:
Add(Foo(1), Add(Add(2, 3), 4))
will be rewritten into:
Invoke(x0 => Add(2, 3, x1 => Add(x1, 4, x2 => Add(x0, x2, continuation))), Foo(1))
Rewrite(Expression<Action>)
Rewrites the given expression to a lambda expression that accepts a callback and executes the expression using CPS.
Declaration
public Expression<Action<Action>> Rewrite(Expression<Action> expression)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression<System.Action> | expression | Expression to rewrite. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression<System.Action<System.Action>> | Lambda expression that, given a callback, will execute the expression using CPS. |
Examples
This method will rewrite the following expression:
() => Bar(1, 2, 3)
into:
callback => Bar(1, 2, 3, callback)
Evaluation order of side-effects is preserved. The following expression:
() => Bar(Foo(1), 2, 3)
will be rewritten into:
callback => Invoke(x0 => Bar(x0, 2, 3, callback), Foo(1))
Rewrite(Expression<Action>, Expression<Action>)
Rewrites the given expression to a CPS expression using the specified callback.
Declaration
public Expression Rewrite(Expression<Action> expression, Expression<Action> continuation)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression<System.Action> | expression | Lambda expression whose body to rewrite. |
System.Linq.Expressions.Expression<System.Action> | continuation | Continuation to pass. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | Rewritten expression using CPS. |
Examples
This method will rewrite the following expression:
() => Bar(1, 2, 3)
into:
Bar(continuation)
Evaluation order of side-effects is preserved. The following expression:
() => Bar(Foo(1), 2, 3)
will be rewritten into:
Invoke(x0 => Bar(x0, 2, 3, continuation), Foo(1))
Rewrite(LambdaExpression)
Rewrites the given expression to a lambda expression that accepts a callback and executes the expression using CPS.
Declaration
public LambdaExpression Rewrite(LambdaExpression expression)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.LambdaExpression | expression | Expression to rewrite. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.LambdaExpression | Lambda expression that, given a callback, will execute the expression using CPS. |
Examples
This method will rewrite the following expression:
(a, b, c, d) => Add(a, Add(Add(b, c), d))
into:
(a, b, c, d, callback) => Add(b, c, x0 => Add(x0, d, x1 => Add(a, x1, callback)))
Evaluation order of side-effects is preserved. The following expression:
(a, b, c, d) => Add(Foo(a), Add(Add(b, c), d))
will be rewritten into:
(a, b, c, d, callback) => Invoke(x0 => Add(b, c, x1 => Add(x1, d, x2 => Add(x0, x2, callback))), Foo(a))
Rewrite<T>(Expression<Action<T>>)
Rewrites the given expression to a lambda expression that accepts a callback and executes the expression using CPS.
Declaration
public Expression<Action<T, Action>> Rewrite<T>(Expression<Action<T>> expression)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression<System.Action<T>> | expression | Expression to rewrite. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression<System.Action<T, System.Action>> | Lambda expression that, given a callback, will execute the expression using CPS. |
Type Parameters
Name | Description |
---|---|
T | Type of the parameter passed to the computation. |
Examples
This method will rewrite the following expression:
x => Bar(x, 2, 3)
into:
(x, callback) => Bar(x, 2, 3, callback)
Evaluation order of side-effects is preserved. The following expression:
x => Bar(Foo(x), 2, 3)
will be rewritten into:
(x, callback) => Invoke(x0 => Bar(x0, 2, 3, callback), Foo(x))
Rewrite<TResult>(Expression<Func<TResult>>)
Rewrites the given expression to a lambda expression that accepts a callback and executes the expression using CPS.
Declaration
public Expression<Action<Action<TResult>>> Rewrite<TResult>(Expression<Func<TResult>> expression)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression<System.Func<TResult>> | expression | Expression to rewrite. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression<System.Action<System.Action<TResult>>> | Lambda expression that, given a callback, will execute the expression using CPS. |
Type Parameters
Name | Description |
---|---|
TResult | Type of the result produced by the expression. |
Examples
This method will rewrite the following expression:
() => Add(1, Add(Add(2, 3), 4))
into:
callback => Add(2, 3, x0 => Add(x0, 4, x1 => Add(1, x1, callback)))
Evaluation order of side-effects is preserved. The following expression:
() => Add(Foo(1), Add(Add(2, 3), 4))
will be rewritten into:
callback => Invoke(x0 => Add(2, 3, x1 => Add(x1, 4, x2 => Add(x0, x2, callback))), Foo(1))
Rewrite<TResult>(Expression<Func<TResult>>, Expression<Action<TResult>>)
Rewrites the given expression to a CPS expression using the specified callback.
Declaration
public Expression Rewrite<TResult>(Expression<Func<TResult>> expression, Expression<Action<TResult>> continuation)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression<System.Func<TResult>> | expression | Lambda expression whose body to rewrite. |
System.Linq.Expressions.Expression<System.Action<TResult>> | continuation | Continuation to pass. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | Rewritten expression using CPS. |
Type Parameters
Name | Description |
---|---|
TResult | Type of the result produced by the expression. |
Examples
This method will rewrite the following expression:
() => Add(1, Add(Add(2, 3), 4))
into:
Add(2, 3, x0 => Add(x0, 4, x1 => Add(1, x1, continuation)))
Evaluation order of side-effects is preserved. The following expression:
() => Add(Foo(1), Add(Add(2, 3), 4))
will be rewritten into:
Invoke(x0 => Add(2, 3, x1 => Add(x1, 4, x2 => Add(x0, x2, continuation))), Foo(1))
Rewrite<T1, T2>(Expression<Action<T1, T2>>)
Rewrites the given expression to a lambda expression that accepts a callback and executes the expression using CPS.
Declaration
public Expression<Action<T1, T2, Action>> Rewrite<T1, T2>(Expression<Action<T1, T2>> expression)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression<System.Action<T1, T2>> | expression | Expression to rewrite. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression<System.Action<T1, T2, System.Action>> | Lambda expression that, given a callback, will execute the expression using CPS. |
Type Parameters
Name | Description |
---|---|
T1 | Type of the first parameter passed to the computation. |
T2 | Type of the second parameter passed to the computation. |
Examples
This method will rewrite the following expression:
(x, y) => Bar(x, y, 3)
into:
(x, y, callback) => Bar(x, y, 3, callback)
Evaluation order of side-effects is preserved. The following expression:
(x, y) => Bar(Foo(x), y, 3)
will be rewritten into:
(x, y, callback) => Invoke(x0 => Bar(x0, y, 3, callback), Foo(x))
Rewrite<T, TResult>(Expression<Func<T, TResult>>)
Rewrites the given expression to a lambda expression that accepts a callback and executes the expression using CPS.
Declaration
public Expression<Action<T, Action<TResult>>> Rewrite<T, TResult>(Expression<Func<T, TResult>> expression)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression<System.Func<T, TResult>> | expression | Expression to rewrite. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression<System.Action<T, System.Action<TResult>>> | Lambda expression that, given a callback, will execute the expression using CPS. |
Type Parameters
Name | Description |
---|---|
T | Type of the parameter passed to the computation. |
TResult | Type of the result produced by the expression. |
Examples
This method will rewrite the following expression:
x => Add(1, Add(Add(2, x), 4))
into:
(x, callback) => Add(2, x, x0 => Add(x0, 4, x1 => Add(1, x1, callback)))
Evaluation order of side-effects is preserved. The following expression:
x => Add(Foo(1), Add(Add(2, x), 4))
will be rewritten into:
(x, callback) => Invoke(x0 => Add(2, x, x1 => Add(x1, 4, x2 => Add(x0, x2, callback))), Foo(1))
Rewrite<T1, T2, T3>(Expression<Action<T1, T2, T3>>)
Rewrites the given expression to a lambda expression that accepts a callback and executes the expression using CPS.
Declaration
public Expression<Action<T1, T2, T3, Action>> Rewrite<T1, T2, T3>(Expression<Action<T1, T2, T3>> expression)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression<System.Action<T1, T2, T3>> | expression | Expression to rewrite. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression<System.Action<T1, T2, T3, System.Action>> | Lambda expression that, given a callback, will execute the expression using CPS. |
Type Parameters
Name | Description |
---|---|
T1 | Type of the first parameter passed to the computation. |
T2 | Type of the second parameter passed to the computation. |
T3 | Type of the third parameter passed to the computation. |
Examples
This method will rewrite the following expression:
(x, y, z) => Bar(x, y, z)
into:
(x, y, z, callback) => Bar(x, y, z, callback)
Evaluation order of side-effects is preserved. The following expression:
(x, y, z) => Bar(Foo(x), y, z)
will be rewritten into:
(x, y, z, callback) => Invoke(x0 => Bar(x0, y, z, callback), Foo(x))
Rewrite<T1, T2, TResult>(Expression<Func<T1, T2, TResult>>)
Rewrites the given expression to a lambda expression that accepts a callback and executes the expression using CPS.
Declaration
public Expression<Action<T1, T2, Action<TResult>>> Rewrite<T1, T2, TResult>(Expression<Func<T1, T2, TResult>> expression)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression<System.Func<T1, T2, TResult>> | expression | Expression to rewrite. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression<System.Action<T1, T2, System.Action<TResult>>> | Lambda expression that, given a callback, will execute the expression using CPS. |
Type Parameters
Name | Description |
---|---|
T1 | Type of the first parameter passed to the computation. |
T2 | Type of the second parameter passed to the computation. |
TResult | Type of the result produced by the expression. |
Examples
This method will rewrite the following expression:
(x, y) => Add(1, Add(Add(2, x), y))
into:
(x, y, callback) => Add(2, x, x0 => Add(x0, y, x1 => Add(1, x1, callback)))
Evaluation order of side-effects is preserved. The following expression:
(x, y) => Add(Foo(1), Add(Add(2, x), y))
will be rewritten into:
(x, y, callback) => Invoke(x0 => Add(2, x, x1 => Add(x1, y, x2 => Add(x0, x2, callback))), Foo(1))
Rewrite<T1, T2, T3, TResult>(Expression<Func<T1, T2, T3, TResult>>)
Rewrites the given expression to a lambda expression that accepts a callback and executes the expression using CPS.
Declaration
public Expression<Action<T1, T2, T3, Action<TResult>>> Rewrite<T1, T2, T3, TResult>(Expression<Func<T1, T2, T3, TResult>> expression)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression<System.Func<T1, T2, T3, TResult>> | expression | Expression to rewrite. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression<System.Action<T1, T2, T3, System.Action<TResult>>> | Lambda expression that, given a callback, will execute the expression using CPS. |
Type Parameters
Name | Description |
---|---|
T1 | Type of the first parameter passed to the computation. |
T2 | Type of the second parameter passed to the computation. |
T3 | Type of the third parameter passed to the computation. |
TResult | Type of the result produced by the expression. |
Examples
This method will rewrite the following expression:
(x, y, z) => Add(x, Add(Add(2, y), z))
into:
(x, y, z, callback) => Add(2, y, x0 => Add(x0, z, x1 => Add(x, x1, callback)))
Evaluation order of side-effects is preserved. The following expression:
(x, y, z) => Add(Foo(x), Add(Add(2, y), z))
will be rewritten into:
(x, y, z, callback) => Invoke(x0 => Add(2, y, x1 => Add(x1, z, x2 => Add(x0, x2, callback))), Foo(x))