Skip to content

Commit

Permalink
Simplify ExpressionUtils.PromoteExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
metoule committed Nov 16, 2024
1 parent 0a09e87 commit f26a4e2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 21 deletions.
8 changes: 4 additions & 4 deletions src/DynamicExpresso.Core/Parsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ private Expression ParseAssignment()
NextToken();
var right = ParseAssignment();

var promoted = ExpressionUtils.PromoteExpression(right, left.Type, true);
var promoted = ExpressionUtils.PromoteExpression(right, left.Type);
if (promoted == null)
throw ParseException.Create(_token.pos, ErrorMessages.CannotConvertValue,
TypeUtils.GetTypeName(right.Type), TypeUtils.GetTypeName(left.Type));
Expand Down Expand Up @@ -1122,8 +1122,8 @@ private Expression GenerateConditional(Expression test, Expression expr1, Expres
throw ParseException.Create(errorPos, ErrorMessages.FirstExprMustBeBool);
if (expr1.Type != expr2.Type)
{
var expr1As2 = expr2 != ParserConstants.NullLiteralExpression ? ExpressionUtils.PromoteExpression(expr1, expr2.Type, true) : null;
var expr2As1 = expr1 != ParserConstants.NullLiteralExpression ? ExpressionUtils.PromoteExpression(expr2, expr1.Type, true) : null;
var expr1As2 = expr2 != ParserConstants.NullLiteralExpression ? ExpressionUtils.PromoteExpression(expr1, expr2.Type) : null;
var expr2As1 = expr1 != ParserConstants.NullLiteralExpression ? ExpressionUtils.PromoteExpression(expr2, expr1.Type) : null;
if (expr1As2 != null && expr2As1 == null)
{
expr1 = expr1As2;
Expand Down Expand Up @@ -1838,7 +1838,7 @@ private Expression ParseElementAccess(Expression expr)

for (int i = 0; i < args.Length; i++)
{
args[i] = ExpressionUtils.PromoteExpression(args[i], typeof(int), true);
args[i] = ExpressionUtils.PromoteExpression(args[i], typeof(int));
if (args[i] == null)
throw ParseException.Create(errorPos, ErrorMessages.InvalidIndex);
}
Expand Down
22 changes: 7 additions & 15 deletions src/DynamicExpresso.Core/Resolution/ExpressionUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,15 @@ namespace DynamicExpresso.Resolution
{
internal static class ExpressionUtils
{
public static Expression PromoteExpression(Expression expr, Type type, bool exact)
public static Expression PromoteExpression(Expression expr, Type type)
{
if (expr.Type == type) return expr;
if (expr is ConstantExpression)
if (expr is ConstantExpression ce && ce == ParserConstants.NullLiteralExpression)
{
var ce = (ConstantExpression)expr;
if (ce == ParserConstants.NullLiteralExpression)
{
if (type.ContainsGenericParameters)
return null;
if (!type.IsValueType || TypeUtils.IsNullableType(type))
return Expression.Constant(null, type);
}
if (type.ContainsGenericParameters)
return null;
if (!type.IsValueType || TypeUtils.IsNullableType(type))
return Expression.Constant(null, type);
}

if (expr is InterpreterExpression ie)
Expand All @@ -42,11 +38,7 @@ public static Expression PromoteExpression(Expression expr, Type type, bool exac

if (TypeUtils.IsCompatibleWith(expr.Type, type) || expr is DynamicExpression)
{
if (type.IsValueType || exact)
{
return Expression.Convert(expr, type);
}
return expr;
return Expression.Convert(expr, type);
}

return null;
Expand Down
4 changes: 2 additions & 2 deletions src/DynamicExpresso.Core/Resolution/MethodResolution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static bool CheckIfMethodIsApplicableAndPrepareIt(MethodData method, Expr
continue;
}

var promoted = ExpressionUtils.PromoteExpression(currentArgument, parameterType, true);
var promoted = ExpressionUtils.PromoteExpression(currentArgument, parameterType);
if (promoted != null)
{
promotedArgs.Add(promoted);
Expand All @@ -110,7 +110,7 @@ public static bool CheckIfMethodIsApplicableAndPrepareIt(MethodData method, Expr
continue;
}

var promoted = ExpressionUtils.PromoteExpression(currentArgument, paramsArrayElementType, true);
var promoted = ExpressionUtils.PromoteExpression(currentArgument, paramsArrayElementType);
if (promoted != null)
{
paramsArrayPromotedArgument = paramsArrayPromotedArgument ?? new List<Expression>();
Expand Down

0 comments on commit f26a4e2

Please sign in to comment.