From 9a56a33d92810f2de66df08c90b20ec25213971f Mon Sep 17 00:00:00 2001 From: rameel Date: Mon, 23 Nov 2026 22:01:47 +0500 Subject: [PATCH] parser: extract shift operators from ApplyBinaryExpression No behavior change. --- .../ExpressionBuilder.Helpers.cs | 68 +++++++++++-------- 1 file changed, 38 insertions(+), 30 deletions(-) diff --git a/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs b/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs index ab8a5cd..340bf7c 100644 --- a/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs +++ b/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs @@ -452,6 +452,9 @@ static Expression Convert(Expression argument, Type conversionType) private static Expression ApplyBinaryExpression(Identifier op, Func 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 @@ -531,36 +534,6 @@ private static Expression ApplyBinaryExpression(Identifier op, Func>" 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); @@ -584,6 +557,41 @@ private static Expression ApplyBinaryExpression(Identifier op, Func 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); + } + /// /// Converts an operand to the specified type, or to the nullable form of the type for a lifted operator. ///