-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
ExpressionNormalizer.cs
305 lines (244 loc) · 11.6 KB
/
ExpressionNormalizer.cs
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Reflection;
namespace Azure.Data.Tables.Queryable
{
internal class ExpressionNormalizer : ExpressionVisitor
{
private const bool LiftToNull = false;
private readonly Dictionary<Expression, Pattern> _patterns = new Dictionary<Expression, Pattern>(ReferenceEqualityComparer<Expression>.Instance);
private bool _underEqualityOperation;
private ExpressionNormalizer(Dictionary<Expression, Expression> normalizerRewrites)
{
Debug.Assert(normalizerRewrites != null, "normalizerRewrites != null");
NormalizerRewrites = normalizerRewrites;
}
internal Dictionary<Expression, Expression> NormalizerRewrites { get; }
internal static Expression Normalize(Expression expression, Dictionary<Expression, Expression> rewrites)
{
Debug.Assert(expression != null, "expression != null");
Debug.Assert(rewrites != null, "rewrites != null");
ExpressionNormalizer normalizer = new ExpressionNormalizer(rewrites);
Expression result = normalizer.Visit(expression);
return result;
}
protected override Expression VisitBinary(BinaryExpression b)
{
_underEqualityOperation = b.NodeType == ExpressionType.Equal || b.NodeType == ExpressionType.NotEqual;
BinaryExpression visited = (BinaryExpression)base.VisitBinary(b);
switch (visited.NodeType)
{
case ExpressionType.Equal:
case ExpressionType.NotEqual:
case ExpressionType.LessThan:
case ExpressionType.LessThanOrEqual:
case ExpressionType.GreaterThan:
case ExpressionType.GreaterThanOrEqual:
Expression normalizedLeft = UnwrapObjectConvert(visited.Left);
Expression normalizedRight = UnwrapObjectConvert(visited.Right);
if (normalizedLeft != visited.Left || normalizedRight != visited.Right)
{
visited = CreateRelationalOperator(visited.NodeType, normalizedLeft, normalizedRight);
}
break;
}
if (_patterns.TryGetValue(visited.Left, out Pattern pattern) && pattern.Kind == PatternKind.Compare && IsConstantZero(visited.Right))
{
ComparePattern comparePattern = (ComparePattern)pattern;
if (TryCreateRelationalOperator(visited.NodeType, comparePattern.Left, comparePattern.Right, out BinaryExpression relationalExpression))
{
visited = relationalExpression;
}
}
_underEqualityOperation = false;
RecordRewrite(b, visited);
return visited;
}
protected override Expression VisitMember(MemberExpression m)
{
Expression visited;
if (IsImplicitBooleanComparison(m))
{
visited = CreateExplicitBooleanComparison(m);
}
else
{
visited = base.VisitMember(m);
}
RecordRewrite(m, visited);
return visited;
}
protected override Expression VisitUnary(UnaryExpression u)
{
Expression result;
if (u.NodeType == ExpressionType.Convert && IsImplicitBooleanComparison(u))
{
BinaryExpression expandedBooleanComparison = CreateExplicitBooleanComparison(u);
result = VisitBinary(expandedBooleanComparison);
}
else
{
result = base.VisitUnary(u);
RecordRewrite(u, result);
}
return result;
}
private bool IsImplicitBooleanComparison(Expression expression)
{
return !_underEqualityOperation && typeof(bool?).IsAssignableFrom(expression.Type);
}
private static BinaryExpression CreateExplicitBooleanComparison(Expression expression)
{
var isNullable = expression.Type == typeof(bool?);
ConstantExpression constant = isNullable ? Expression.Constant(true, typeof(bool?)) : Expression.Constant(true, typeof(bool));
return CreateRelationalOperator(ExpressionType.Equal, expression, constant);
}
private static Expression UnwrapObjectConvert(Expression input)
{
if (input.NodeType == ExpressionType.Constant && input.Type == typeof(object))
{
ConstantExpression constant = (ConstantExpression)input;
if (constant.Value != null &&
constant.Value.GetType() != typeof(object))
{
return Expression.Constant(constant.Value, constant.Value.GetType());
}
}
while (ExpressionType.Convert == input.NodeType)
{
input = ((UnaryExpression)input).Operand;
}
return input;
}
private static bool IsConstantZero(Expression expression)
{
return expression.NodeType == ExpressionType.Constant &&
((ConstantExpression)expression).Value.Equals(0);
}
protected override Expression VisitMethodCall(MethodCallExpression call)
{
Expression visited = VisitMethodCallNoRewrite(call);
RecordRewrite(call, visited);
return visited;
}
internal Expression VisitMethodCallNoRewrite(MethodCallExpression call)
{
MethodCallExpression visited = (MethodCallExpression)base.VisitMethodCall(call);
if (visited.Method.IsStatic && visited.Method.Name == "Equals" && visited.Arguments.Count > 1)
{
return Expression.Equal(visited.Arguments[0], visited.Arguments[1], false, visited.Method);
}
if (!visited.Method.IsStatic && visited.Method.Name == "Equals" && visited.Arguments.Count > 0)
{
return CreateRelationalOperator(ExpressionType.Equal, visited.Object, visited.Arguments[0]);
}
if (visited.Method.IsStatic && visited.Method.Name == "CompareString" && visited.Method.DeclaringType.FullName == "Microsoft.VisualBasic.CompilerServices.Operators")
{
return CreateCompareExpression(visited.Arguments[0], visited.Arguments[1]);
}
if (!visited.Method.IsStatic && visited.Method.Name == "CompareTo" && visited.Arguments.Count == 1 && visited.Method.ReturnType == typeof(int))
{
return CreateCompareExpression(visited.Object, visited.Arguments[0]);
}
if (visited.Method.IsStatic && visited.Method.Name == "Compare" && visited.Arguments.Count > 1 && visited.Method.ReturnType == typeof(int))
{
return CreateCompareExpression(visited.Arguments[0], visited.Arguments[1]);
}
if (ReflectionUtil.s_dictionaryMethodInfosHash.Contains(visited.Method) && visited.Arguments.Count == 1 && visited.Arguments[0] is ConstantExpression)
{
return visited;
}
throw new NotSupportedException($"Method {visited.Method.Name} not supported.");
}
private static readonly MethodInfo StaticRelationalOperatorPlaceholderMethod = typeof(ExpressionNormalizer).GetMethod("RelationalOperatorPlaceholder", BindingFlags.Static | BindingFlags.NonPublic);
private static bool RelationalOperatorPlaceholder<TLeft, TRight>(TLeft left, TRight right)
{
Debug.Assert(false, "This method should never be called. It exists merely to support creation of relational LINQ expressions.");
return object.ReferenceEquals(left, right);
}
private static BinaryExpression CreateRelationalOperator(ExpressionType op, Expression left, Expression right)
{
if (!TryCreateRelationalOperator(op, left, right, out BinaryExpression result))
{
Debug.Assert(false, "CreateRelationalOperator has unknown op " + op);
}
return result;
}
private static bool TryCreateRelationalOperator(ExpressionType op, Expression left, Expression right, out BinaryExpression result)
{
MethodInfo relationalOperatorPlaceholderMethod = StaticRelationalOperatorPlaceholderMethod.MakeGenericMethod(left.Type, right.Type);
switch (op)
{
case ExpressionType.Equal:
result = Expression.Equal(left, right, LiftToNull, relationalOperatorPlaceholderMethod);
return true;
case ExpressionType.NotEqual:
result = Expression.NotEqual(left, right, LiftToNull, relationalOperatorPlaceholderMethod);
return true;
case ExpressionType.LessThan:
result = Expression.LessThan(left, right, LiftToNull, relationalOperatorPlaceholderMethod);
return true;
case ExpressionType.LessThanOrEqual:
result = Expression.LessThanOrEqual(left, right, LiftToNull, relationalOperatorPlaceholderMethod);
return true;
case ExpressionType.GreaterThan:
result = Expression.GreaterThan(left, right, LiftToNull, relationalOperatorPlaceholderMethod);
return true;
case ExpressionType.GreaterThanOrEqual:
result = Expression.GreaterThanOrEqual(left, right, LiftToNull, relationalOperatorPlaceholderMethod);
return true;
default:
result = null;
return false;
}
}
private Expression CreateCompareExpression(Expression left, Expression right)
{
Expression result = Expression.Condition(
CreateRelationalOperator(ExpressionType.Equal, left, right),
Expression.Constant(0),
Expression.Condition(
CreateRelationalOperator(ExpressionType.GreaterThan, left, right),
Expression.Constant(1),
Expression.Constant(-1)));
_patterns[result] = new ComparePattern(left, right);
return result;
}
private void RecordRewrite(Expression source, Expression rewritten)
{
Debug.Assert(source != null, "source != null");
Debug.Assert(rewritten != null, "rewritten != null");
Debug.Assert(NormalizerRewrites != null, "this.NormalizerRewrites != null");
if (source != rewritten)
{
NormalizerRewrites.Add(rewritten, source);
}
}
private abstract class Pattern
{
internal abstract PatternKind Kind { get; }
}
private enum PatternKind
{
Compare,
}
private sealed class ComparePattern : Pattern
{
internal ComparePattern(Expression left, Expression right)
{
Left = left;
Right = right;
}
internal readonly Expression Left;
internal readonly Expression Right;
internal override PatternKind Kind
{
get { return PatternKind.Compare; }
}
}
}
}