Skip to content

Commit 19e7608

Browse files
authored
Merge pull request #78 from rameel/parser/extract-null-literal-conversion
parser: move the null literal conversion out of VisitBinary
2 parents 780e7b3 + 3668435 commit 19e7608

2 files changed

Lines changed: 27 additions & 15 deletions

File tree

‎src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,32 @@ private static Expr SkipParentheses(Expr expr)
280280
return expr;
281281
}
282282

283+
/// <summary>
284+
/// Converts the null literal opposite an operand of a nullable value type to that type.
285+
/// For the <c>+</c> operator with an operand of a nullable enum type, the literal is converted
286+
/// to the nullable form of the underlying type of the enum.
287+
/// </summary>
288+
/// <param name="expr">The binary expression.</param>
289+
/// <param name="lhs">The expression of the left operand.</param>
290+
/// <param name="rhs">The expression of the right operand.</param>
291+
private static void ApplyNullLiteralConversion(Expr.Binary expr, ref Expression lhs, ref Expression rhs)
292+
{
293+
//
294+
// The null literal converts to a nullable value type, so opposite an operand of such a type
295+
// it takes that type, as in 'x == null' or 'x ?? null'. For an enum type E, only E + U and U + E
296+
// are defined, so in 'x + null' it takes the nullable form of the underlying type U instead.
297+
//
298+
if (lhs.Type.IsNullable() && IsNullLiteral(expr.Right))
299+
rhs = Expression.Constant(null, GetNullLiteralType(expr.Operator, lhs.Type));
300+
else if (rhs.Type.IsNullable() && IsNullLiteral(expr.Left))
301+
lhs = Expression.Constant(null, GetNullLiteralType(expr.Operator, rhs.Type));
302+
303+
static Type GetNullLiteralType(Identifier op, Type operandType) =>
304+
op.Name == "+" && operandType.GetNonNullableType() is { IsEnum: true } enumType
305+
? enumType.GetEnumUnderlyingType().MakeNullableType()
306+
: operandType;
307+
}
308+
283309
/// <summary>
284310
/// Converts the integer literal 0 opposite an operand of an enum type to that enum type
285311
/// if the operator is defined for two operands of the enum type. As in the C# compiler, a literal

‎src/Ramstack.ExpressionParser/ExpressionBuilder.cs‎

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,7 @@ protected internal override Expression VisitBinary(Expr.Binary expr)
6464
var lhsType = lhs.Type;
6565
var rhsType = rhs.Type;
6666

67-
//
68-
// The null literal converts to a nullable value type, so opposite an operand of such a type
69-
// it takes that type, as in 'x == null' or 'x ?? null'. For an enum type E, only E + U and U + E
70-
// are defined, so in 'x + null' it takes the nullable form of the underlying type U instead.
71-
//
72-
if (lhsType.IsNullable() && IsNullLiteral(expr.Right))
73-
rhs = Expression.Constant(null, GetNullLiteralType(expr.Operator, lhsType));
74-
else if (rhsType.IsNullable() && IsNullLiteral(expr.Left))
75-
lhs = Expression.Constant(null, GetNullLiteralType(expr.Operator, rhsType));
76-
67+
ApplyNullLiteralConversion(expr, ref lhs, ref rhs);
7768
ApplyEnumZeroConversion(expr, ref lhs, ref rhs);
7869

7970
var factory = ResolveBinaryOperatorFactory(expr.Operator.Name);
@@ -173,11 +164,6 @@ void Flatten(Expression e)
173164
}
174165

175166
return null;
176-
177-
static Type GetNullLiteralType(Identifier op, Type operandType) =>
178-
op.Name == "+" && operandType.GetNonNullableType() is { IsEnum: true } enumType
179-
? enumType.GetEnumUnderlyingType().MakeNullableType()
180-
: operandType;
181167
}
182168

183169
/// <inheritdoc />

0 commit comments

Comments
 (0)