Class ConstantHoister
Hoists constants out of an expression tree into an environment.
Inheritance
Implements
Namespace: System.Linq.CompilerServices
Assembly: Nuqleon.Linq.CompilerServices.dll
Syntax
public class ConstantHoister : Object, IConstantHoister
Constructors
ConstantHoister()
Creates a new constant hoister without an exclusion list.
Declaration
public ConstantHoister()
ConstantHoister(Boolean, LambdaExpression[])
Creates a new constant hoister with the specified exclusion list.
Declaration
public ConstantHoister(bool useDefaultForNull, params LambdaExpression[] exclusions)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | useDefaultForNull | Indicates whether to substitute null references for default expressions rather than hoisting them. |
System.Linq.Expressions.LambdaExpression[] | exclusions | Exclusion list for constant sites that should be excluded from hoisting. |
Methods
Create(Boolean, LambdaExpression[])
Creates a new constant hoister using the specified exclusion list for constant sites to exclude from hoisting.
Declaration
public static ConstantHoister Create(bool useDefaultForNull, params LambdaExpression[] exclusions)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | useDefaultForNull | Indicates whether to substitute null references for default expressions rather than hoisting them. |
System.Linq.Expressions.LambdaExpression[] | exclusions | Exclusion list for constant sites that should be excluded from hoisting. These sites are specified as lambda parameters. |
Returns
Type | Description |
---|---|
ConstantHoister | Constant hoister instance using the specified exclusion list. |
Examples
Consider the following usage of a constant hoister:
var h = ConstantHoister.Create(
(Expression<Func<string, string>>)(s => string.Format(s, default(object[])))
);
var e = (Expression<Func<string>>)(() => string.Format("{0} {1}", "bar", 123));
var r = h.Hoist(e.Body);
results in the following environment and tree:
r.Expression = string.Format("{0} {1}", p0, p1)
r.Environment = { p0 -> "bar", p1 -> 123 }
where the first argument to the string.Format call did not get hoisted.
Hoist(ConstantExpression)
Provides a hook for derived classes to rewrite the hoisted constants, e.g. to intern the constant's value.
Declaration
protected virtual Expression Hoist(ConstantExpression expression)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.ConstantExpression | expression | The constant expression that has been hoisted. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | An expression equivalent to the original hoisted expression. |
Hoist(Expression)
Hoists constants in the specified expression and returns an environment.
Declaration
public ExpressionWithEnvironment Hoist(Expression expression)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression | expression | Expression to hoist constants in. |
Returns
Type | Description |
---|---|
ExpressionWithEnvironment | Expression bound by an environment consisting of the hoisted constants. |
Hoist(Expression, Boolean)
Hoists constants in the specified expression and returns an environment.
Declaration
public static ExpressionWithEnvironment Hoist(Expression expression, bool useDefaultForNull)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression | expression | Expression to hoist constants in. |
System.Boolean | useDefaultForNull | Indicates whether to substitute null references for default expressions rather than hoisting them. |
Returns
Type | Description |
---|---|
ExpressionWithEnvironment | Expression bound by an environment consisting of the hoisted constants. |
Explicit Interface Implementations
IConstantHoister.Hoist(Expression)
Hoists constants in the specified expression and returns an environment.
Declaration
IExpressionWithEnvironment IConstantHoister.Hoist(Expression expression)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression | expression | Expression to hoist constants in. |
Returns
Type | Description |
---|---|
IExpressionWithEnvironment | Expression bound by an environment consisting of the hoisted constants. |