-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpressionBuilder.Helpers.cs
More file actions
318 lines (265 loc) · 11.9 KB
/
ExpressionBuilder.Helpers.cs
File metadata and controls
318 lines (265 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
using System.Linq.Expressions;
using System.Reflection;
using Ramstack.Parsing.Internal;
namespace Ramstack.Parsing;
partial class ExpressionBuilder
{
private object ResolveSymbol(Expr.Reference expr)
{
var expression = TryResolveSymbol(expr);
if (expression is null)
Error.CannotResolveSymbol(expr.Value);
return expression;
}
private object? TryResolveSymbol(Expr.Reference expr)
{
var name = expr.Value;
var type = Binder.BindToType(name);
if (type is not null)
return type;
var member = Binder.BindToMember(type: null, memberName: name, isStatic: true);
if (member is null)
return null;
var context = member.IsStatic() ? null : Binder.Context;
Debug.Assert(context is not null || member.IsStatic());
Debug.Assert(context is null || !member.IsStatic());
return Expression.MakeMemberAccess(context, member);
}
private static Expression? ApplyImplicitConversion(Expression expression, Type conversionType)
{
if (expression.Type == conversionType
|| expression.Type.IsClass && conversionType.IsAssignableFrom(expression.Type))
return expression;
if (TypeUtils.CanConvertPrimitive(expression.Type, conversionType))
return Expression.Convert(expression, conversionType);
var conversion = TypeUtils.FindConversionOperator(expression.Type, typeof(bool));
if (conversion is not null)
return Expression.Convert(expression, conversionType, conversion);
return null;
}
private static List<Expression> CreateInjectingArguments(MethodInfo method, IReadOnlyList<Expression> args)
{
var parameters = method.GetParameters();
var list = new List<Expression>();
for (var i = 0; i < args.Count; i++)
{
var arg = args[i];
var parameter = parameters[i];
if (parameter.GetCustomAttribute<ParamArrayAttribute>() is not null)
{
Debug.Assert(parameter.ParameterType.IsSZArray);
Debug.Assert(parameter.ParameterType.GetElementType() is not null);
var paramArray = new List<Expression>();
var paramArrayType = parameter.ParameterType.GetElementType()!;
for (; i < args.Count; i++)
paramArray.Add(
Convert(args[i], paramArrayType));
list.Add(
Expression.NewArrayInit(
paramArrayType,
paramArray));
break;
}
list.Add(Convert(arg, parameter.ParameterType));
}
return list;
static Expression Convert(Expression argument, Type conversionType)
{
if (argument.Type == conversionType)
return argument;
if (argument.Type.IsClass)
if (conversionType.IsAssignableFrom(argument.Type))
return argument;
return Expression.Convert(argument, conversionType);
}
}
private static Expression ApplyBinaryExpression(Identifier op, Func<Expression, Expression, Expression> apply, Expression lhs, Expression rhs)
{
var enumUnderlyingType = lhs.Type.IsEnum
? lhs.Type.GetEnumUnderlyingType()
: rhs.Type.IsEnum
? rhs.Type.GetEnumUnderlyingType()
: null;
var enumType = lhs.Type.IsEnum
? lhs.Type
: rhs.Type.IsEnum
? rhs.Type
: null;
if (enumUnderlyingType is not null)
{
if (lhs.Type != rhs.Type)
if (lhs.Type.IsEnum ? enumUnderlyingType != rhs.Type : enumUnderlyingType != lhs.Type)
Error.NonApplicableBinaryOperator(op, lhs.Type, rhs.Type);
if (lhs.Type.IsEnum)
lhs = Expression.Convert(lhs, enumUnderlyingType);
if (rhs.Type.IsEnum)
rhs = Expression.Convert(rhs, enumUnderlyingType);
}
if (enumType is null && op.Name is ">>" or ">>>" or "<<")
{
if (!TypeUtils.IsInteger(lhs.Type) && TypeUtils.IsInteger(rhs.Type))
Error.NonApplicableBinaryOperator(op, lhs.Type, rhs.Type);
if (rhs.Type == typeof(sbyte)
|| rhs.Type == typeof(byte)
|| rhs.Type == typeof(short)
|| rhs.Type == typeof(ushort))
rhs = Expression.Convert(rhs, typeof(int));
if (rhs.Type != typeof(int))
Error.NonApplicableBinaryOperator(op, lhs.Type, rhs.Type);
if (lhs.Type == typeof(sbyte)
|| lhs.Type == typeof(byte)
|| lhs.Type == typeof(short)
|| lhs.Type == typeof(ushort))
lhs = Expression.Convert(lhs, typeof(int));
if (op.Name == ">>>")
{
if (lhs.Type == typeof(int))
return Expression.Convert(
apply(Expression.Convert(lhs, typeof(uint)), rhs),
typeof(int));
if (lhs.Type == typeof(long))
return Expression.Convert(
apply(Expression.Convert(lhs, typeof(ulong)), rhs),
typeof(long));
}
return apply(lhs, rhs);
}
ApplyBinaryNumericPromotions(op, ref lhs, ref rhs);
var result = apply(lhs, rhs);
if (enumType is not null)
{
switch (op.Name)
{
case "==":
case "!=":
case ">":
case "<":
case ">=":
case "<=":
return result;
default:
return Expression.Convert(result, enumType);
}
}
return result;
}
private static void ApplyBinaryNumericPromotions(Identifier op, ref Expression lhs, ref Expression rhs)
{
//
// 12.4.7.3 Binary numeric promotions
// https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/expressions#12473-binary-numeric-promotions
//
// Binary numeric promotion implicitly converts both operands to a common type which,
// in case of the non-relational operators, also becomes the result type of the operation.
// Binary numeric promotion consists of applying the following rules, in the order they appear here:
//
// * If either operand is of type decimal, the other operand is converted to type decimal,
// or a binding-time error occurs if the other operand is of type float or double.
// * Otherwise, if either operand is of type double, the other operand is converted to type double.
// * Otherwise, if either operand is of type float, the other operand is converted to type float.
// * Otherwise, if either operand is of type ulong, the other operand is converted to type ulong,
// or a binding-time error occurs if the other operand is of type sbyte, short, int, or long.
// * Otherwise, if either operand is of type long, the other operand is converted to type long.
// * Otherwise, if either operand is of type uint and the other operand is of type sbyte, short, or int,
// both operands are converted to type long.
// * Otherwise, if either operand is of type uint, the other operand is converted to type uint.
// * Otherwise, both operands are converted to type int.
var lhsType = lhs.Type;
var rhsType = rhs.Type;
if (!TypeUtils.IsNumeric(lhsType)
|| !TypeUtils.IsNumeric(rhsType))
return;
var conversionTypes = new[]
{
typeof(decimal),
typeof(double),
typeof(float),
typeof(ulong),
typeof(long),
typeof(uint),
typeof(int)
};
// ReSharper disable once ForCanBeConvertedToForeach
for (var i = 0; i < conversionTypes.Length; i++)
{
var conversionType = conversionTypes[i];
if (conversionType != typeof(int))
if (lhsType != conversionType && rhsType != conversionType)
continue;
if (conversionType == typeof(decimal))
{
//
// If either operand is of type decimal, the other operand is converted to type decimal,
// or a compile-time error occurs if the other operand is of type float or double.
//
if (lhsType == typeof(double)
|| lhsType == typeof(float)
|| rhsType == typeof(double)
|| rhsType == typeof(float))
Error.NonApplicableBinaryOperator(op, lhsType, rhsType);
}
else if (conversionType == typeof(ulong))
{
//
// If either operand is of type ulong, the other operand is converted to type ulong,
// or a compile-time error occurs if the other operand is of type sbyte, short, int, or long.
//
if (lhsType == typeof(sbyte)
|| lhsType == typeof(short)
|| lhsType == typeof(int)
|| lhsType == typeof(long)
|| rhsType == typeof(sbyte)
|| rhsType == typeof(short)
|| rhsType == typeof(int)
|| rhsType == typeof(long))
Error.NonApplicableBinaryOperator(op, lhsType, rhsType);
}
else if (conversionType == typeof(uint))
{
//
// If either operand is of type uint and the other operand is of type sbyte, short, or int,
// both operands are converted to type long.
//
if (lhsType == typeof(sbyte)
|| lhsType == typeof(short)
|| lhsType == typeof(int)
|| rhsType == typeof(sbyte)
|| rhsType == typeof(short)
|| rhsType == typeof(int))
conversionType = typeof(long);
}
if (lhsType != conversionType)
lhs = Expression.Convert(lhs, conversionType);
if (rhsType != conversionType)
rhs = Expression.Convert(rhs, conversionType);
return;
}
}
private static Func<Expression, Expression, Expression> ResolveBinaryOperatorFactory(string @operator)
{
return @operator switch
{
"??" => Expression.Coalesce,
"&&" => Expression.AndAlso,
"||" => Expression.OrElse,
"==" => Expression.Equal,
"!=" => Expression.NotEqual,
"<=" => Expression.LessThanOrEqual,
">=" => Expression.GreaterThanOrEqual,
">>" => Expression.RightShift,
"<<" => Expression.LeftShift,
">>>" => Expression.RightShift,
"<" => Expression.LessThan,
">" => Expression.GreaterThan,
"&" => Expression.And,
"|" => Expression.Or,
"^" => Expression.ExclusiveOr,
"+" => Expression.Add,
"-" => Expression.Subtract,
"*" => Expression.Multiply,
"/" => Expression.Divide,
"%" => Expression.Modulo,
_ => throw new NotSupportedException($"Operator '{@operator}' is not supported.")
};
}
}