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
44 changes: 44 additions & 0 deletions src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,50 @@ private static BinaryExpression ApplyNullCoalescing(Identifier op, Expression lh
return null;
}

/// <summary>
/// Creates a <c>string.Concat</c> call for the <c>+</c> operator when at least one of its operands is a string.
/// </summary>
/// <param name="lhs">The expression of the left operand.</param>
/// <param name="rhs">The expression of the right operand.</param>
/// <returns>
/// The expression that calls <c>string.Concat</c>.
/// </returns>
/// <remarks>
/// A <c>string.Concat</c> call among the operands, such as the one built for <c>a + b</c>, is replaced with its
/// arguments, so <c>a + b + c</c> becomes a single call. For a call in the expanded form, these are the elements
/// of its array; a call with a single argument of another kind, such as <c>string.Concat(array)</c>, stays one
/// operand. If any operand is not a string, all operands are passed as <see cref="object" />.
/// </remarks>
private static MethodCallExpression ApplyStringConcatenation(Expression lhs, Expression rhs)
{
List<Expression> arguments = [.. GetConcatOperands(lhs), .. GetConcatOperands(rhs)];

var type = arguments.TrueForAll(a => a.Type == typeof(string)) ? typeof(string) : typeof(object);
var types = new Type[arguments.Count];
Array.Fill(types, type);

var method = (MethodInfo?)MethodResolver.ResolveMethod(s_concatMethods, types);
Debug.Assert(method is not null);

return Expression.Call(method, CreateInjectingArguments(method, arguments));

static IReadOnlyList<Expression> GetConcatOperands(Expression expression)
{
if (expression is MethodCallExpression call
&& call.Method.Name == nameof(string.Concat)
&& call.Method.DeclaringType == typeof(string))
{
if (call.Arguments.Count != 1)
return call.Arguments;

if (call.Arguments[0] is NewArrayExpression { NodeType: ExpressionType.NewArrayInit } array)
return array.Expressions;
}

return [expression];
}
}

/// <summary>
/// Determines whether the specified expression is the null literal, possibly in parentheses.
/// </summary>
Expand Down
65 changes: 2 additions & 63 deletions src/Ramstack.ExpressionParser/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,69 +87,8 @@ protected internal override Expression VisitBinary(Expr.Binary expr)
case "??":
return ApplyNullCoalescing(expr.Operator, lhs, rhs);

case "+":
if (lhsType != typeof(string) && rhsType != typeof(string))
break;

var arguments = new List<Expression>();
var argumentTypes = new List<Type>();

Flatten(lhs);
Flatten(rhs);

if (argumentTypes.Any(t => t != typeof(string)))
{
for (var i = 0; i < arguments.Count; i++)
{
argumentTypes[i] = typeof(object);
if (arguments[i].Type.IsValueType)
arguments[i] = Expression.Convert(arguments[i], typeof(object));
}
}

var cmi = (MethodInfo?)MethodResolver.ResolveMethod(s_concatMethods, argumentTypes.ToArray())!;
Debug.Assert(cmi is not null);

var args = CreateInjectingArguments(cmi, arguments);
return Expression.Call(null, cmi, args);

void Flatten(Expression e)
{
if (e is MethodCallExpression call
&& call.Method.Name == nameof(string.Concat)
&& call.Method.DeclaringType == typeof(string))
{
var list = call.Arguments;
if (list.Count != 1)
{
foreach (var item in list)
{
arguments.Add(item);
argumentTypes.Add(item.Type);
}
}
else if (list[0] is NewArrayExpression { NodeType: ExpressionType.NewArrayInit } array)
{
foreach (var item in array.Expressions)
{
arguments.Add(item);
argumentTypes.Add(item.Type);
}
}
else
{
// The only argument is not an array of operands created for the expanded form,
// as in string.Concat(value) or string.Concat(array), so the call itself is the operand.
arguments.Add(e);
argumentTypes.Add(e.Type);
}
}
else
{
arguments.Add(e);
argumentTypes.Add(e.Type);
}
}
case "+" when lhsType == typeof(string) || rhsType == typeof(string):
return ApplyStringConcatenation(lhs, rhs);
}

try
Expand Down