Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 122 additions & 22 deletions src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,78 @@ private static bool IsNullLiteral(Expr expr)
return expr is Expr.Literal { Value: null };
}

/// <summary>
/// Determines whether the specified expression is the integer literal 0, possibly in parentheses.
/// </summary>
/// <param name="expr">The expression to test.</param>
/// <returns>
/// <see langword="true" /> if the expression is the integer literal 0; otherwise, <see langword="false" />.
/// </returns>
private static bool IsZeroLiteral(Expr expr)
{
while (expr is Expr.Parenthesized parenthesized)
expr = parenthesized.Expression;

return expr is Expr.Literal { Value: 0 or 0u or 0L or 0UL };
}

/// <summary>
/// Converts the integer literal 0 opposite an operand of an enum type to that enum type
/// if the operator is defined for two operands of the enum type.
/// </summary>
/// <param name="expr">The binary expression.</param>
/// <param name="lhs">The expression of the left operand.</param>
/// <param name="rhs">The expression of the right operand.</param>
private static void ApplyEnumZeroConversion(Expr.Binary expr, ref Expression lhs, ref Expression rhs)
{
//
// 10.2.4 Implicit enumeration conversions
// https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/conversions#1024-implicit-enumeration-conversions
//
// An implicit enumeration conversion permits a constant expression with any integer type and the value zero
// to be converted to any enum type and to any nullable value type whose underlying type is an enum type.
//
// Of such constant expressions, only the integer literal 0 is recognized.
//
switch (expr.Operator.Name)
{
case "-":
case "&":
case "|":
case "^":
case "==":
case "!=":
case "<":
case ">":
case "<=":
case ">=":
break;

default:
return;
}

var lhsType = lhs.Type.GetNonNullableType();
var rhsType = rhs.Type.GetNonNullableType();

if (lhsType.IsEnum && IsZeroLiteral(expr.Right))
{
//
// Both E - E and E - U accept 'x - 0'. As in the C# compiler, E - U is selected and returns E
// if the literal is of the underlying type U; otherwise, E - E is selected and returns U, as in 'x - 0L'.
//
if (expr.Operator.Name != "-" || rhsType != lhsType.GetEnumUnderlyingType())
rhs = Expression.Convert(rhs, lhsType);
}
else if (rhsType.IsEnum && IsZeroLiteral(expr.Left))
{
//
// As in the C# compiler, E - E is selected for '0 - x' over U - E, so the result is of the underlying type.
//
lhs = Expression.Convert(lhs, rhsType);
}
}

/// <summary>
/// Applies an implicit conversion of a branch of a conditional expression to the type of the other branch.
/// </summary>
Expand Down Expand Up @@ -365,22 +437,65 @@ private static Expression ApplyBinaryExpression(Identifier op, Func<Expression,
? rhsNonNullableType
: null;

//
// The type to which the result of an enum operator is converted. It remains null for an enum comparison,
// whose result is bool, and if neither operand is of an enum type.
//
Type? enumResultType = null;

if (enumType is not null)
{
//
// 12.10.5 Addition operator, 12.10.6 Subtraction operator, 12.12.6 Enumeration comparison operators,
// and 12.13.3 Enumeration logical operators
// https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/expressions#12105-addition-operator
//
// Every enum type E with the underlying type U implicitly provides the following operators:
//
// E operator +(E x, U y);
// E operator +(U x, E y);
// U operator -(E x, E y);
// E operator -(E x, U y);
// E operator &(E x, E y); and likewise |, ^
// bool operator ==(E x, E y); and likewise !=, <, >, <=, >=
//
// Each operator is evaluated on the values converted to U, as in (E)((U)x + (U)y).
// The C# compiler also accepts U - E and returns E, so it is supported as well.
//
var enumUnderlyingType = enumType.GetEnumUnderlyingType();
var otherType = lhsNonNullableType.IsEnum ? rhsNonNullableType : lhsNonNullableType;

var isEnumPair = lhsNonNullableType == rhsNonNullableType;
var isUnderlyingPair = otherType == enumUnderlyingType
|| TypeUtils.CanConvertPrimitive(otherType, enumUnderlyingType);

switch (op.Name)
{
case "+" when isUnderlyingPair:
case "-" when isUnderlyingPair:
case "&" or "|" or "^" when isEnumPair:
enumResultType = enumType;
break;

if (lhsNonNullableType != rhsNonNullableType)
if (lhsNonNullableType.IsEnum ? enumUnderlyingType != rhsNonNullableType : enumUnderlyingType != lhsNonNullableType)
case "-" when isEnumPair:
enumResultType = enumUnderlyingType;
break;

case "==" or "!=" or "<" or ">" or "<=" or ">=" when isEnumPair:
break;

default:
Error.NonApplicableBinaryOperator(op, lhsType, rhsType);
break;
}

if (lhsNonNullableType.IsEnum)
lhs = ConvertOperand(lhs, enumUnderlyingType, lifted);

if (rhsNonNullableType.IsEnum)
rhs = ConvertOperand(rhs, enumUnderlyingType, lifted);
}

if (enumType is null && op.Name is ">>" or ">>>" or "<<")
else if (op.Name is ">>" or ">>>" or "<<")
{
//
// The predefined shift operators take an int, uint, long, or ulong value and an int count,
Expand Down Expand Up @@ -428,24 +543,9 @@ private static Expression ApplyBinaryExpression(Identifier op, Func<Expression,

var result = apply(lhs, rhs);

if (enumType is not null)
{
switch (op.Name)
{
case "==":
case "!=":
case ">":
case "<":
case ">=":
case "<=":
return result;

default:
return ConvertOperand(result, enumType, lifted);
}
}

return result;
return enumResultType is not null
? ConvertOperand(result, enumResultType, lifted)
: result;
}

/// <summary>
Expand Down
14 changes: 11 additions & 3 deletions src/Ramstack.ExpressionParser/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,15 @@ protected internal override Expression VisitBinary(Expr.Binary expr)

//
// The null literal converts to a nullable value type, so opposite an operand of such a type
// it takes that type, as in 'x == null' or 'x ?? null'.
// it takes that type, as in 'x == null' or 'x ?? null'. For an enum type E, only E + U and U + E
// are defined, so in 'x + null' it takes the nullable form of the underlying type U instead.
//
if (lhsType.IsNullable() && IsNullLiteral(expr.Right))
rhs = Expression.Constant(null, lhsType);
rhs = Expression.Constant(null, GetNullLiteralType(expr.Operator, lhsType));
else if (rhsType.IsNullable() && IsNullLiteral(expr.Left))
lhs = Expression.Constant(null, rhsType);
lhs = Expression.Constant(null, GetNullLiteralType(expr.Operator, rhsType));

ApplyEnumZeroConversion(expr, ref lhs, ref rhs);

var factory = ResolveBinaryOperatorFactory(expr.Operator.Name);

Expand Down Expand Up @@ -168,6 +171,11 @@ void Flatten(Expression e)
}

return null!;

static Type GetNullLiteralType(Identifier op, Type operandType) =>
op.Name == "+" && operandType.GetNonNullableType() is { IsEnum: true } enumType
? enumType.GetEnumUnderlyingType().MakeNullableType()
: operandType;
}

/// <inheritdoc />
Expand Down
57 changes: 57 additions & 0 deletions tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,63 @@ Operator '>>' cannot be applied to operands of type 'System.Double' and 'System.
1m >>> 1
Operator '>>>' cannot be applied to operands of type 'System.Decimal' and 'System.Int32'.

#######################################
# Binary operators not defined for enum operands

DayOfWeek.Friday * 2
Operator '*' cannot be applied to operands of type 'System.DayOfWeek' and 'System.Int32'.

DayOfWeek.Friday / 2
Operator '/' cannot be applied to operands of type 'System.DayOfWeek' and 'System.Int32'.

DayOfWeek.Friday % 2
Operator '%' cannot be applied to operands of type 'System.DayOfWeek' and 'System.Int32'.

DayOfWeek.Friday * 0
Operator '*' cannot be applied to operands of type 'System.DayOfWeek' and 'System.Int32'.

DayOfWeek.Friday * DayOfWeek.Monday
Operator '*' cannot be applied to operands of type 'System.DayOfWeek' and 'System.DayOfWeek'.

DayOfWeek.Friday << 1
Operator '<<' cannot be applied to operands of type 'System.DayOfWeek' and 'System.Int32'.

1 << DayOfWeek.Friday
Operator '<<' cannot be applied to operands of type 'System.Int32' and 'System.DayOfWeek'.

DayOfWeek.Friday >> 1
Operator '>>' cannot be applied to operands of type 'System.DayOfWeek' and 'System.Int32'.

DayOfWeek.Friday >>> 1
Operator '>>>' cannot be applied to operands of type 'System.DayOfWeek' and 'System.Int32'.

DayOfWeek.Friday + DayOfWeek.Monday
Operator '+' cannot be applied to operands of type 'System.DayOfWeek' and 'System.DayOfWeek'.

DayOfWeek.Friday + 1L
Operator '+' cannot be applied to operands of type 'System.DayOfWeek' and 'System.Int64'.

DayOfWeek.Friday & 1
Operator '&' cannot be applied to operands of type 'System.DayOfWeek' and 'System.Int32'.

DayOfWeek.Friday | 1
Operator '|' cannot be applied to operands of type 'System.DayOfWeek' and 'System.Int32'.

1 ^ DayOfWeek.Friday
Operator '^' cannot be applied to operands of type 'System.Int32' and 'System.DayOfWeek'.

DayOfWeek.Friday == 5
Operator '==' cannot be applied to operands of type 'System.DayOfWeek' and 'System.Int32'.

DayOfWeek.Friday > 1
Operator '>' cannot be applied to operands of type 'System.DayOfWeek' and 'System.Int32'.

DayOfWeek.Friday == 0.0
Operator '==' cannot be applied to operands of type 'System.DayOfWeek' and 'System.Double'.

DayOfWeek.Friday == StringSplitOptions.None
Operator '==' cannot be applied to operands of type 'System.DayOfWeek' and 'System.StringSplitOptions'.

#######################################
# Conditional operator branches without a common type

Expand Down
12 changes: 12 additions & 0 deletions tests/Ramstack.ExpressionParser.Tests/Data/Expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,18 @@ Convert(DayOfWeek.Friday, Int64)
DayOfWeek.Friday :> DayOfWeek
DayOfWeek.Friday

#######################################
# Enum binary operators

DayOfWeek.Friday - DayOfWeek.Monday
(Convert(DayOfWeek.Friday, Int32) - Convert(DayOfWeek.Monday, Int32))

DayOfWeek.Friday - 1
Convert((Convert(DayOfWeek.Friday, Int32) - 1), DayOfWeek)

DayOfWeek.Friday | 0
Convert((Convert(DayOfWeek.Friday, Int32) | Convert(Convert(0, DayOfWeek), Int32)), DayOfWeek)

#######################################
# Conditional operator branches of different types

Expand Down
Loading
Loading