Class ShortcircuitExpressions
Remove operations when knowledge about the nullity of Expression nodes allows this.
Inheritance
Implements
Inherited Members
Namespace: Mangrove.MetricSetTransformers.Normalization
Assembly: MetricSetTransformers.dll
Syntax
public class ShortcircuitExpressions : ChangeExpressions, IGenericExpressionVisitor<IEnumerable<Expression>>, IExpressionVisitor, IGenericVisitor<IEnumerable<Expression>>
Remarks
Why is this needed? It is an artifact of a construct called "generic templates" and how they are handled by the MDL Compiler as of now (2019-04-15). Generic templates are essentially C# functions whose definitions are inlined after Roslyn validates the metric set. However, there can be valid C# code which no longer compiles after function inlining, see for example:
int x = 0;
double foo(double? y) => y ?? 0;
foo(x);
Here, the inlined function definition x ?? 0
will cause the C# compiler to complain.
In addition, we replace any tautological comparisons, like
- for non-nullable x, we replace x != null ---> true x == null ---> false null != x ---> true null == x ---> false IsNull(x) ---> false IsNotNull(x) ---> true
- we replace tautological literal comparisons 1 != 2 ---> true 5 == 5 ---> true true == false ---> false
- for any boolean ternary operation with the left operand tautologically true of false, we replace that with the corresponding value, i.e. true ? a : b ---> a false ? a : b ---> b
- for And and Or
true && true ---> true true || true ---> true true && false ---> false true || false ---> true
In particular, expression
x != null ? 1 : 0 for non-nullable x
would be simply replaced with the literal 1.
Methods
View SourceChange(BinaryOperation)
Declaration
protected override Expression Change(BinaryOperation expr)
Parameters
Type | Name | Description |
---|---|---|
BinaryOperation | expr |
Returns
Type | Description |
---|---|
Expression |
Overrides
View SourceChange(TernaryOperation)
Declaration
protected override Expression Change(TernaryOperation expr)
Parameters
Type | Name | Description |
---|---|---|
TernaryOperation | expr |
Returns
Type | Description |
---|---|
Expression |
Overrides
View SourceChange(UnaryOperation)
Declaration
protected override Expression Change(UnaryOperation expr)
Parameters
Type | Name | Description |
---|---|---|
UnaryOperation | expr |
Returns
Type | Description |
---|---|
Expression |