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
2 changes: 0 additions & 2 deletions src/Ramstack.ExpressionParser/DefaultBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,9 @@ public void RegisterType(Type type, bool importAsStatic = false)
&& m.MemberType is MemberTypes.Field or MemberTypes.Property)
.ToArray();

//
// As in C#, a field or property of a derived type hides matching members of its
// base types. For static imports, keep matches from each imported type so that
// a name found in two imports is ambiguous, as with using static in C#.
//
if (type is not null && candidates.Length > 1)
candidates = candidates
.Where(m => !candidates.Any(c => m.DeclaringType!.IsBaseTypeOf(c.DeclaringType!)))
Expand Down
32 changes: 0 additions & 32 deletions src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,8 @@ private object ResolveSymbol(Expr.Reference expr)
/// </returns>
private static MemberExpression CreateMemberAccess(Expression? expression, MemberInfo member)
{
//
// As in C#, a property is read through its get accessor, so a property whose get accessor
// is missing or not public cannot be read, even if its set accessor is public.
//
if (member is PropertyInfo property)
{
var getter = property.GetMethod;
Expand Down Expand Up @@ -151,9 +149,7 @@ private static Type ApplyConditionalConversions(Expr.Conditional expr, ref Expre
isFalseNull ? null : ifFalse.Type);
}

//
// A branch of type void has no value, so the conditional expression would have none either.
//
if (ifTrue.Type == typeof(void) || ifFalse.Type == typeof(void))
Error.NonApplicableConditionalOperator(ifTrue.Type, ifFalse.Type);

Expand Down Expand Up @@ -227,10 +223,8 @@ private static BinaryExpression ApplyNullCoalescing(Identifier op, Expression lh

if (underlyingType is not null)
{
//
// A non-null value of a is unwrapped and converted to B. Convert a to the nullable form of B instead,
// which keeps its null value. For a reference type B, the conversion is a boxing conversion.
//
converted = rhsType.IsValueType
? ApplyImplicitConversion(lhs, rhsType.IsNullable() ? rhsType : rhsType.MakeNullableType())
: rhsType.IsAssignableFrom(underlyingType)
Expand Down Expand Up @@ -290,11 +284,9 @@ private static Expr SkipParentheses(Expr expr)
/// <param name="rhs">The expression of the right operand.</param>
private static void ApplyNullLiteralConversion(Expr.Binary expr, ref Expression lhs, ref Expression rhs)
{
//
// 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'. 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 (lhs.Type.IsNullable() && IsNullLiteral(expr.Right))
rhs = Expression.Constant(null, GetNullLiteralType(expr.Operator, lhs.Type));
else if (rhs.Type.IsNullable() && IsNullLiteral(expr.Left))
Expand Down Expand Up @@ -334,18 +326,14 @@ private static void ApplyEnumZeroConversion(Expr.Binary expr, ref Expression lhs

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);
}
}
Expand All @@ -360,10 +348,8 @@ private static void ApplyEnumZeroConversion(Expr.Binary expr, ref Expression lhs
/// </returns>
private static Expression? ApplyBranchConversion(Expression expression, Type conversionType)
{
//
// ApplyImplicitConversion does not box values, so handle the boxing conversion here,
// for example of a number to a branch explicitly converted to object.
//
if (expression.Type.IsValueType
&& !conversionType.IsValueType
&& conversionType.IsAssignableFrom(expression.Type))
Expand Down Expand Up @@ -431,10 +417,8 @@ private static List<Expression> CreateInjectingArguments(MethodInfo method, IRea
list.Add(Convert(arg, parameter.ParameterType));
}

//
// As in C#, a parameter array without arguments gets an empty array,
// and an omitted optional parameter gets its default value.
//
for (var i = list.Count; i < parameters.Length; i++)
{
var parameter = parameters[i];
Expand All @@ -449,10 +433,8 @@ private static List<Expression> CreateInjectingArguments(MethodInfo method, IRea
}
else
{
//
// A required parameter is left without an argument only for a method that does not
// accept these arguments, so let Expression.Call report the argument count.
//
break;
}
}
Expand All @@ -464,26 +446,20 @@ static Expression CreateDefaultArgument(ParameterInfo parameter)
var type = parameter.ParameterType;
var value = parameter.DefaultValue;

//
// A parameter declared with [Optional] and without a default value gets Type.Missing
// if its type is object, and the default value of its type otherwise, as in C#.
//
if (value is Missing)
return type == typeof(object)
? Expression.Constant(value, type)
: Expression.Default(type);

//
// The default value of a struct, as in 'Guid guid = default', is also stored as null.
//
if (value is null)
return type.IsValueType && !type.IsNullable()
? Expression.Default(type)
: Expression.Constant(null, type);

//
// The default value of a nullable enum is stored as its underlying integer.
//
var underlyingType = Nullable.GetUnderlyingType(type) ?? type;
if (underlyingType.IsEnum && value.GetType() != underlyingType)
value = Enum.ToObject(underlyingType, value);
Expand Down Expand Up @@ -530,10 +506,8 @@ 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)
Expand Down Expand Up @@ -593,10 +567,8 @@ private static Expression ApplyBinaryExpression(Identifier op, Func<Expression,

if (lifted)
{
//
// Expression lifts a user-defined operator, such as DateTime subtraction, only if both operands
// are of nullable types, so convert the remaining operand of a non-nullable value type.
//
if (lhs.Type.IsValueType)
lhs = ConvertOperand(lhs, lhs.Type.GetNonNullableType(), lifted: true);

Expand All @@ -613,10 +585,8 @@ private static Expression ApplyBinaryExpression(Identifier op, Func<Expression,

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();
Expand Down Expand Up @@ -743,10 +713,8 @@ private static void ApplyBinaryNumericPromotions(Identifier op, ref Expression l
if (x == typeof(long) || y == typeof(long))
return typeof(long);

//
// Neither operand is of type long here, so IsSignedInteger matches only sbyte, short, or int,
// as the uint rule requires.
//
if (x == typeof(uint) || y == typeof(uint))
return IsSignedInteger(x) || IsSignedInteger(y) ? typeof(long) : typeof(uint);

Expand Down
18 changes: 0 additions & 18 deletions src/Ramstack.ExpressionParser/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,8 @@ void Flatten(Expression e)
}
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);
}
Expand Down Expand Up @@ -223,10 +221,8 @@ Expression ProcessIndex()
if (method is null)
Error.AmbiguousMatch(indexers);

//
// As in C#, an indexer whose get accessor is not public takes part in overload resolution,
// but cannot be read.
//
if (!method.IsPublic)
Error.InaccessibleGetAccessor(indexers[methods.IndexOf(method)]);

Expand Down Expand Up @@ -260,10 +256,8 @@ Expression Invoke(Expression? instance, MethodInfo? method)

if (instance is not null)
{
//
// Without a method, the instance is a delegate to invoke. A method can also be called
// on a delegate, as in 'func.Invoke(1)' or 'func.ToString()'.
//
Debug.Assert(method is not null || typeof(Delegate).IsAssignableFrom(instance.Type));

if (method is null)
Expand Down Expand Up @@ -296,9 +290,7 @@ Expression ProcessMemberAccess(Expr.MemberAccess e)
if (!typeof(Delegate).IsAssignableFrom(memberInfo.GetMemberType()))
Error.NonInvocableMember(member);

//
// A member accessed through a type is static, so it is accessed without an instance.
//
return Invoke(CreateMemberAccess(instance, memberInfo), null);
}

Expand Down Expand Up @@ -357,19 +349,15 @@ protected internal override Expression VisitUnary(Expr.Unary expr)
{
var operand = Visit(expr.Operand);

//
// For an operand of a nullable value type, the operators below are lifted:
// they apply to the underlying type and return the nullable form of their result type.
//
var operandType = operand.Type;
var nonNullableType = operandType.GetNonNullableType();
var lifted = operandType.IsNullable();

//
// C# does not define the unary '-' and '+' operators for an enum, and '~' applies to its underlying type.
// A conversion takes the enum itself, so that converting it to object boxes the enum
// rather than its underlying value.
//
if (nonNullableType.IsEnum && expr.UnaryType != UnaryType.Convert)
{
if (expr.UnaryType is UnaryType.Negate or UnaryType.UnaryPlus)
Expand All @@ -380,21 +368,17 @@ protected internal override Expression VisitUnary(Expr.Unary expr)

switch (expr.UnaryType)
{
//
// As in C#, the literals 2147483648 and 9223372036854775808 directly after a unary minus are the constants
// int.MinValue and long.MinValue. Parentheses or the U suffix keep their uint and ulong types.
// Other suffixes need no check: 2147483648 with L or UL is not a uint, and C# allows L for 9223372036854775808.
//
case UnaryType.Negate when expr.Operand is Expr.Literal { Value: 0x80000000u, HasUnsignedSuffix: false }:
return Expression.Constant(int.MinValue);

case UnaryType.Negate when expr.Operand is Expr.Literal { Value: 0x8000000000000000ul, HasUnsignedSuffix: false }:
return Expression.Constant(long.MinValue);

//
// C# has no negation operator for uint, so the operand is converted to long
// and then negated, which keeps the sign of the result.
//
case UnaryType.Negate when operand.Type.GetNonNullableType() == typeof(uint):
operand = ConvertOperand(operand, typeof(long), lifted);
break;
Expand All @@ -409,12 +393,10 @@ protected internal override Expression VisitUnary(Expr.Unary expr)
switch (expr.UnaryType)
{
case UnaryType.Not:
//
// Expression.Not performs a bitwise complement on integers and falls back to op_OnesComplement
// when a type does not define op_LogicalNot, so '!' would silently work as '~'.
// Allow only the logical negation, as C# does. For a nullable operand,
// Expression.Not lifts the negation of the underlying type.
//
if (nonNullableType == typeof(bool))
return Expression.Not(operand);

Expand Down
8 changes: 0 additions & 8 deletions src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,9 @@ private static Parser<Expr> CreateParser()
identifier_body.Many()
).Map(m => new Identifier(m.ToString()));

//
// As in C#, the '@' prefix makes a verbatim identifier, which can be a keyword, as in 'x.@null'.
// The prefix is not part of the name. The keyword check does not match the prefix, so a verbatim
// identifier passes Not(keyword) wherever an identifier is expected.
//
var identifier =
L('@').Optional()
.Then(identifier_name)
Expand Down Expand Up @@ -134,10 +132,8 @@ private static Parser<Expr> CreateParser()
.Separated(
Seq(L(','), S)).As("expression_list");

//
// Member access, calls, and indexers share one precedence level, so each of them
// is parsed as a postfix operator that wraps the expression to its left.
//
var call =
expression_list
.Between(
Expand All @@ -152,12 +148,10 @@ private static Parser<Expr> CreateParser()
Seq(L(']'), S))
.Do(Func<Expr, Expr> (parameters) => expr => new Expr.Indexer(expr, parameters));

//
// Not(keyword) reports no expected token when it fails, so the member name is labeled
// to report "Expected identifier" for a keyword after the dot. Unlike Fail, a labeled parser
// fails without stopping the parse, so Subparser still stops before a dot that an enclosing
// grammar uses, as in 'a..b'.
//
var member_access =
Not(keyword).Then(identifier).As("identifier")
.Between(
Expand All @@ -181,10 +175,8 @@ private static Parser<Expr> CreateParser()
return expr;
});

//
// The conversion operator is ':>' rather than ':', which would conflict with the conditional
// operator: in 'flag ? 0 : count', ': count' would be taken for a conversion to the type 'count'.
//
var cast_expression =
Seq(
postfix_expression,
Expand Down
Loading