Class ExpressionOptimizer
Optimizer for expression trees.
Inherited Members
Namespace: System.Linq.Expressions
Assembly: Nuqleon.Linq.Expressions.Optimizers.dll
Syntax
public class ExpressionOptimizer : LvalExpressionVisitor
Constructors
ExpressionOptimizer(ISemanticProvider, IEvaluatorFactory)
Creates a new expression optimizer instance using the specified semantic semanticProvider
and the specified evaluator evaluatorFactory
.
Declaration
public ExpressionOptimizer(ISemanticProvider semanticProvider, IEvaluatorFactory evaluatorFactory)
Parameters
Type | Name | Description |
---|---|---|
ISemanticProvider | semanticProvider | The semantic provider to use when performing various checks against expressions and reflection objects. |
IEvaluatorFactory | evaluatorFactory | The evaluator factory to use when evaluating a member or an expression at compile time. |
Properties
EvaluatorFactory
Gets the evaluator factory to use when evaluating a member or an expression at compile time.
Declaration
public IEvaluatorFactory EvaluatorFactory { get; }
Property Value
Type | Description |
---|---|
IEvaluatorFactory |
SemanticProvider
Gets the semantic provider to use when performing various checks against expressions and reflection objects.
Declaration
public ISemanticProvider SemanticProvider { get; }
Property Value
Type | Description |
---|---|
ISemanticProvider |
Methods
ChangeType(Expression, Type)
Changes the static type of the specified expression
to the
specified type
.
Declaration
protected Expression ChangeType(Expression expression, Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression | expression | The expression whose static type to change. |
System.Type | type | The type to change the static type of the expression to. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | The resulting expression with the specified static type. |
Constant(Expression, Object, Type)
Creates an expression of type System.Linq.Expressions.ExpressionType.Constant with the specified value
and type
. The original
expression is passed as well in order to allow
overridden methods to suppress optimizations that produce an System.Linq.Expressions.ExpressionType.Constant node.
Declaration
protected virtual Expression Constant(Expression original, object value, Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression | original | The original expression. |
System.Object | value | The value of the constant to create. |
System.Type | type | The static type of the node to return. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | An expression representing the specified |
EvaluateIndex(IndexExpression)
Evaluates the indexer to perform constant folding.
Declaration
protected virtual Expression EvaluateIndex(IndexExpression node)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.IndexExpression | node | The index expression to evaluate. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | An expression containing the result of evaluating the specified index expression. |
EvaluateInvocation(InvocationExpression)
Evaluates an invocation expression where the invocation target is a delegate.
Declaration
protected virtual Expression EvaluateInvocation(InvocationExpression node)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.InvocationExpression | node | The invocation expression to evaluate. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | An expression containing the result of evaluating the specified invocation expression. |
EvaluateMember(MemberExpression)
Evaluates the member to perform constant folding.
Declaration
protected virtual Expression EvaluateMember(MemberExpression node)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.MemberExpression | node | The member expression to evaluate. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | An expression containing the result of evaluating the specified member expression. |
EvaluateMethodCall(MethodCallExpression)
Evaluates the method call to perform constant folding.
Declaration
protected virtual Expression EvaluateMethodCall(MethodCallExpression node)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.MethodCallExpression | node | The method call expression to evaluate. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | An expression containing the result of evaluating the specified method call expression. |
EvaluateNew(NewExpression)
Evaluates the constructor to perform constant folding.
Declaration
protected virtual Expression EvaluateNew(NewExpression node)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.NewExpression | node | The new expression to evaluate. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | An expression containing the result of evaluating the specified new expression. |
GetEvaluator(Expression)
Gets an evaluator delegate for the specified expression
that's free of parameters.
Declaration
protected virtual Func<object> GetEvaluator(Expression expression)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression | expression | The expression to get an evaluator delegate for. |
Returns
Type | Description |
---|---|
System.Func<System.Object> | A delegate that returns an System.Object with the result of evaluating the expression. |
GetEvaluator(UnaryExpression)
Gets an evaluator delegate for the specified unary expression
.
Declaration
protected virtual Func<object, object> GetEvaluator(UnaryExpression expression)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.UnaryExpression | expression | The unary expression to get an evaluator delegate for. |
Returns
Type | Description |
---|---|
System.Func<System.Object, System.Object> | A delegate that takes an System.Object operand and returns an System.Object result. |
GetEvaluator(MemberInfo)
Gets an evaluator delegate for the specified (pure) member
.
Declaration
protected virtual Delegate GetEvaluator(MemberInfo member)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.MemberInfo | member | The member to get an evaluator delegate for. |
Returns
Type | Description |
---|---|
System.Delegate | A delegate that takes zero or more System.Object parameters and returns either System.Object or System.Void. |
GetRuntimeType(Expression)
Gets the runtime type of the result of evaluating the specified expression
.
Declaration
protected Type GetRuntimeType(Expression expression)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression | expression | The expression whose runtime type to get. |
Returns
Type | Description |
---|---|
System.Type | The runtime type of the expression, if statically known; otherwise, |
Remarks
This method checks if the specified expression
does not throw in order to
determine the runtime type. If an exception can occur during evaluation of the expression,
a null
reference is returned.
HasExactType(Expression)
Checks if the specified expression
's static type matches the runtime type when
evaluating the expression.
Declaration
protected virtual bool HasExactType(Expression expression)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression | expression | The expression to check. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Remarks
This method can return true
even if the specified expression
may throw.
Null(Expression, Type)
Creates an expression representing a null
value of the specified type
. The
original
expression is passed as well in order to allow overridden methods to suppress
optimizations that produce an System.Linq.Expressions.ExpressionType.Default node.
Declaration
protected virtual Expression Null(Expression original, Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression | original | The original expression. |
System.Type | type | The type of the |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | An expression representing |
ShouldOptimize(Expression)
Checks if the specified expression should be optimized.
Declaration
protected virtual bool ShouldOptimize(Expression node)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression | node | The expression to check. |
Returns
Type | Description |
---|---|
System.Boolean | true if the expression should be optimized; otherwise, false. |
Remarks
This method supports the suppression of optimizations to various nodes where optimization may be undesirable. An example is a node of type System.Linq.Expressions.ExpressionType.Quote.
Throw(Expression, Exception, Type)
Creates an expression of type System.Linq.Expressions.ExpressionType.Throw with the specified type
used to throw the specified exception
object obtained during evaluation of a pure member
at compile time. The original
expression is passed as well in order to allow overridden
methods to suppress optimizations that produce an System.Linq.Expressions.ExpressionType.Throw node.
Declaration
protected virtual Expression Throw(Expression original, Exception exception, Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression | original | The original expression. |
System.Exception | exception | The exception that was caught during evaluation of the expression. |
System.Type | type | The static type of the node to return. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | An expression representing throwing the specified |
Throw(Expression, Expression, Type)
Creates an expression of type System.Linq.Expressions.ExpressionType.Throw with the specified type
used to throw the specified exception
expression. The original
expression is passed as well in order to allow overridden methods to suppress optimizations that produce an
System.Linq.Expressions.ExpressionType.Throw node.
Declaration
protected virtual Expression Throw(Expression original, Expression exception, Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression | original | The original expression. |
System.Linq.Expressions.Expression | exception | The expression representing the exception to throw. |
System.Type | type | The static type of the node to return. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | An expression representing throwing the specified |
Remarks
The introduction of System.Linq.Expressions.ExpressionType.Throw nodes may result in making the optimized expression tree bigger. However, if the exception gets propagated by the optimizer, it has the potential of pruning out a lot of nodes from the overall tree. Derived types can control the behavior by overriding this method as well as System.Linq.Expressions.ExpressionOptimizer.AlwaysThrows(System.Linq.Expressions.Expression).
TryChangeType(Expression, Type, out Expression)
Tries to change the static type of the specified expression
to the
specified type
.
Declaration
protected virtual bool TryChangeType(Expression expression, Type type, out Expression result)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression | expression | The expression whose static type to change. |
System.Type | type | The type to change the static type of the expression to. |
System.Linq.Expressions.Expression | result | The resulting expression with the specified static type. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Visit(Expression)
Visits and expression tree and returns the optimized result.
Declaration
public override Expression Visit(Expression node)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression | node | The expression tree to visit. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | An optimized rewritten expression, or the original expression if nothing was optimized. |
VisitArgument(Expression, ParameterInfo)
Visits the expression argument
using the corresponding parameter
to determine whether the argument is assigned to a ref
or out
parameter. An argument that
is passed by reference gets visited using VisitLval(Expression).
Declaration
protected override Expression VisitArgument(Expression argument, ParameterInfo parameter)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression | argument | The argument expression to visit. |
System.Reflection.ParameterInfo | parameter | The parameter corresponding to the argument. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | The result of visiting the expression arguments. |
Overrides
VisitBinaryWithConversion(BinaryExpression, Boolean)
Visits a binary expression with a System.Linq.Expressions.BinaryExpression.Conversion specified to perform optimization steps.
Declaration
protected override Expression VisitBinaryWithConversion(BinaryExpression node, bool isAssignment)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.BinaryExpression | node | The binary expression to visit. |
System.Boolean | isAssignment | Indicates whether the node represents an assignment. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | The result of optimizing the binary expression. |
Overrides
VisitBinaryWithoutConversion(BinaryExpression, Boolean)
Visits a binary expression with no System.Linq.Expressions.BinaryExpression.Conversion specified to perform optimization steps.
Declaration
protected override Expression VisitBinaryWithoutConversion(BinaryExpression node, bool isAssignment)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.BinaryExpression | node | The binary expression to visit. |
System.Boolean | isAssignment | Indicates whether the node represents an assignment. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | The result of optimizing the binary expression. |
Overrides
VisitBlock(BlockExpression)
Visits a block expression to perform optimization steps.
Declaration
protected override Expression VisitBlock(BlockExpression node)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.BlockExpression | node | The block expression to visit. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | The result of optimizing the block expression. |
VisitCatchBlock(CatchBlock)
Visits a catch block to perform optimization steps.
Declaration
protected override CatchBlock VisitCatchBlock(CatchBlock node)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.CatchBlock | node | The catch block to visit. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.CatchBlock | The result of optimizing the catch block. |
VisitConditional(ConditionalExpression)
Visits a conditional expression to perform optimization steps.
Declaration
protected override Expression VisitConditional(ConditionalExpression node)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.ConditionalExpression | node | The conditional expression to visit. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | The result of optimizing the conditional expression. |
VisitIndex(IndexExpression, Boolean)
Visits an index expression to perform optimization steps.
Declaration
protected override Expression VisitIndex(IndexExpression node, bool isLval)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.IndexExpression | node | The index expression to visit. |
System.Boolean | isLval | Indicates whether the node occurs in an lval position. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | The result of optimizing the index expression. |
Overrides
VisitInvocationCore(InvocationExpression)
Visits an invocation to perform optimization steps.
Declaration
protected override Expression VisitInvocationCore(InvocationExpression node)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.InvocationExpression | node | The invocation to visit. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | The result of optimizing the invocation. |
Overrides
VisitLambdaInvocation(InvocationExpression)
Visits and invocation of a lambda expression to perform optimization steps.
Declaration
protected virtual Expression VisitLambdaInvocation(InvocationExpression node)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.InvocationExpression | node | The invocation to visit. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | The result of optimizing the invocation. |
VisitMember(MemberExpression, Boolean)
Visits a member expression to perform optimization steps.
Declaration
protected override Expression VisitMember(MemberExpression node, bool isLval)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.MemberExpression | node | The member expression to visit. |
System.Boolean | isLval | Indicates whether the node occurs in an lval position. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | The result of optimizing the member expression. |
Overrides
VisitMethodCallCore(MethodCallExpression)
Visits a method call expression to perform optimization steps.
Declaration
protected override Expression VisitMethodCallCore(MethodCallExpression node)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.MethodCallExpression | node | The method call expression to visit. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | The result of optimizing the method call expression. |
Overrides
VisitNewArray(NewArrayExpression)
Visits a new array expression to perform optimization steps.
Declaration
protected override Expression VisitNewArray(NewArrayExpression node)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.NewArrayExpression | node | The new array expression to visit. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | The result of optimizing the new array expression. |
VisitNewCore(NewExpression)
Visits a new expression to perform optimization steps.
Declaration
protected override Expression VisitNewCore(NewExpression node)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.NewExpression | node | The new expression to visit. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | The result of optimizing the new expression. |
Overrides
VisitPostOptimize(Expression, Expression)
Visits an expression after optimizations have been applied.
Declaration
protected virtual Expression VisitPostOptimize(Expression original, Expression optimized)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression | original | The original expression returned from VisitPreOptimize(Expression). |
System.Linq.Expressions.Expression | optimized | The result of applying optimizations to the |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | The expression to return from the visitor. |
Remarks
This method supports plugging in post-optimization rewrites or logging steps. It can also be used to undo an optimization.
VisitPreOptimize(Expression)
Visits the specified expression prior to applying optimizations.
Declaration
protected virtual Expression VisitPreOptimize(Expression node)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression | node | The expression to visit prior to applying optimizations. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | The result of visiting the specified expression prior to optimization. |
Remarks
This method supports plugging in pre-optimization rewrites or logging steps.
VisitSwitch(SwitchExpression)
Visits a switch expression to perform optimization steps.
Declaration
protected override Expression VisitSwitch(SwitchExpression node)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.SwitchExpression | node | The switch expression to visit. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | The result of optimizing the switch expression. |
VisitTry(TryExpression)
Visits a try expression to perform optimization steps.
Declaration
protected override Expression VisitTry(TryExpression node)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.TryExpression | node | The try expression to visit. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | The result of optimizing the try expression. |
VisitTypeBinary(TypeBinaryExpression)
Visits a type binary expression to perform optimization steps.
Declaration
protected override Expression VisitTypeBinary(TypeBinaryExpression node)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.TypeBinaryExpression | node | The type binary expression to visit. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | The result of optimizing the type binary expression. |
VisitUnaryNonQuote(UnaryExpression, Boolean)
Visits a unary expression to perform optimization steps.
Declaration
protected override Expression VisitUnaryNonQuote(UnaryExpression node, bool isAssignment)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.UnaryExpression | node | The unary expression to visit. |
System.Boolean | isAssignment | Indicates whether the node represents an assignment. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | The result of optimizing the unary expression. |