Skip to content
Merged
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
68 changes: 38 additions & 30 deletions src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,9 @@ static Expression Convert(Expression argument, Type conversionType)

private static Expression ApplyBinaryExpression(Identifier op, Func<Expression, Expression, Expression> apply, Expression lhs, Expression rhs)
{
if (op.Name is ">>" or ">>>" or "<<")
return ApplyShiftOperator(op, apply, lhs, rhs);

//
// 12.4.8 Lifted operators
// https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/expressions#1248-lifted-operators
Expand Down Expand Up @@ -531,36 +534,6 @@ private static Expression ApplyBinaryExpression(Identifier op, Func<Expression,
if (rhsNonNullableType.IsEnum)
rhs = ConvertOperand(rhs, enumUnderlyingType, lifted);
}
else if (op.Name is ">>" or ">>>" or "<<")
{
//
// The predefined shift operators take an int, uint, long, or ulong value and an int count,
// so an operand of a smaller integral type, including char, is converted to int, as in C#.
//
lhs = ApplyUnaryNumericPromotion(lhs);
rhs = ApplyUnaryNumericPromotion(rhs);

var shiftType = lhs.Type.GetNonNullableType();
if (!TypeUtils.IsInteger(shiftType) || rhs.Type.GetNonNullableType() != typeof(int))
Error.NonApplicableBinaryOperator(op, lhsType, rhsType);

if (op.Name == ">>>")
{
if (shiftType == typeof(int))
return ConvertOperand(
apply(ConvertOperand(lhs, typeof(uint), lifted), rhs),
typeof(int),
lifted);

if (shiftType == typeof(long))
return ConvertOperand(
apply(ConvertOperand(lhs, typeof(ulong), lifted), rhs),
typeof(long),
lifted);
}

return apply(lhs, rhs);
}

ApplyBinaryNumericPromotions(op, ref lhs, ref rhs);

Expand All @@ -584,6 +557,41 @@ private static Expression ApplyBinaryExpression(Identifier op, Func<Expression,
: result;
}

private static Expression ApplyShiftOperator(Identifier op, Func<Expression, Expression, Expression> apply, Expression lhs, Expression rhs)
{
//
// The predefined shift operators take an int, uint, long, or ulong value and an int count,
// so an operand of a smaller integral type, including char, is converted to int, as in C#.
//
var lhsType = lhs.Type;
var rhsType = rhs.Type;
var lifted = lhsType.IsNullable() || rhsType.IsNullable();

lhs = ApplyUnaryNumericPromotion(lhs);
rhs = ApplyUnaryNumericPromotion(rhs);

var shiftType = lhs.Type.GetNonNullableType();
if (!TypeUtils.IsInteger(shiftType) || rhs.Type.GetNonNullableType() != typeof(int))
Error.NonApplicableBinaryOperator(op, lhsType, rhsType);

if (op.Name == ">>>")
{
if (shiftType == typeof(int))
return ConvertOperand(
apply(ConvertOperand(lhs, typeof(uint), lifted), rhs),
typeof(int),
lifted);

if (shiftType == typeof(long))
return ConvertOperand(
apply(ConvertOperand(lhs, typeof(ulong), lifted), rhs),
typeof(long),
lifted);
}

return apply(lhs, rhs);
}

/// <summary>
/// Converts an operand to the specified type, or to the nullable form of the type for a lifted operator.
/// </summary>
Expand Down