forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCachedExpressionCompiler.cs
More file actions
906 lines (763 loc) · 31.1 KB
/
CachedExpressionCompiler.cs
File metadata and controls
906 lines (763 loc) · 31.1 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
namespace ServiceStack
{
// The caching expression tree compiler was copied from MVC core to MVC Futures so that Futures code could benefit
// from it and so that it could be exposed as a public API. This is the only public entry point into the system.
// See the comments in the ExpressionUtil namespace for more information.
//
// The unit tests for the ExpressionUtil.* types are in the System.Web.Mvc.Test project.
public static class CachedExpressionCompiler
{
private static readonly ParameterExpression _unusedParameterExpr = Expression.Parameter(typeof(object), "_unused");
// Implements caching around LambdaExpression.Compile() so that equivalent expression trees only have to be
// compiled once.
public static Func<TModel, TValue> Compile<TModel, TValue>(this Expression<Func<TModel, TValue>> lambdaExpression)
{
if (lambdaExpression == null)
{
throw new ArgumentNullException("lambdaExpression");
}
return ExpressionUtil.CachedExpressionCompiler.Process(lambdaExpression);
}
// Evaluates an expression (not a LambdaExpression), e.g. 2 + 2.
public static object Evaluate(Expression arg)
{
if (arg == null)
{
throw new ArgumentNullException("arg");
}
Func<object, object> func = Wrap(arg);
return func(null);
}
private static Func<object, object> Wrap(Expression arg)
{
Expression<Func<object, object>> lambdaExpr = Expression.Lambda<Func<object, object>>(Expression.Convert(arg, typeof(object)), _unusedParameterExpr);
return ExpressionUtil.CachedExpressionCompiler.Process(lambdaExpr);
}
}
}
namespace ServiceStack.ExpressionUtil
{
// BinaryExpression fingerprint class
// Useful for things like array[index]
internal sealed class BinaryExpressionFingerprint : ExpressionFingerprint
{
public BinaryExpressionFingerprint(ExpressionType nodeType, Type type, MethodInfo method)
: base(nodeType, type)
{
// Other properties on BinaryExpression (like IsLifted / IsLiftedToNull) are simply derived
// from Type and NodeType, so they're not necessary for inclusion in the fingerprint.
Method = method;
}
// http://msdn.microsoft.com/en-us/library/system.linq.expressions.binaryexpression.method.aspx
public MethodInfo Method { get; private set; }
public override bool Equals(object obj)
{
BinaryExpressionFingerprint other = obj as BinaryExpressionFingerprint;
return (other != null)
&& Equals(this.Method, other.Method)
&& this.Equals(other);
}
internal override void AddToHashCodeCombiner(HashCodeCombiner combiner)
{
combiner.AddObject(Method);
base.AddToHashCodeCombiner(combiner);
}
}
internal static class CachedExpressionCompiler
{
// This is the entry point to the cached expression compilation system. The system
// will try to turn the expression into an actual delegate as quickly as possible,
// relying on cache lookups and other techniques to save time if appropriate.
// If the provided expression is particularly obscure and the system doesn't know
// how to handle it, we'll just compile the expression as normal.
public static Func<TModel, TValue> Process<TModel, TValue>(Expression<Func<TModel, TValue>> lambdaExpression)
{
return Compiler<TModel, TValue>.Compile(lambdaExpression);
}
private static class Compiler<TIn, TOut>
{
private static Func<TIn, TOut> _identityFunc;
private static readonly ConcurrentDictionary<MemberInfo, Func<TIn, TOut>> _simpleMemberAccessDict =
new ConcurrentDictionary<MemberInfo, Func<TIn, TOut>>();
private static readonly ConcurrentDictionary<MemberInfo, Func<object, TOut>> _constMemberAccessDict =
new ConcurrentDictionary<MemberInfo, Func<object, TOut>>();
private static readonly ConcurrentDictionary<ExpressionFingerprintChain, Hoisted<TIn, TOut>> _fingerprintedCache =
new ConcurrentDictionary<ExpressionFingerprintChain, Hoisted<TIn, TOut>>();
public static Func<TIn, TOut> Compile(Expression<Func<TIn, TOut>> expr)
{
return CompileFromIdentityFunc(expr)
?? CompileFromConstLookup(expr)
?? CompileFromMemberAccess(expr)
?? CompileFromFingerprint(expr)
?? CompileSlow(expr);
}
private static Func<TIn, TOut> CompileFromConstLookup(Expression<Func<TIn, TOut>> expr)
{
ConstantExpression constExpr = expr.Body as ConstantExpression;
if (constExpr != null)
{
// model => {const}
TOut constantValue = (TOut)constExpr.Value;
return _ => constantValue;
}
return null;
}
private static Func<TIn, TOut> CompileFromIdentityFunc(Expression<Func<TIn, TOut>> expr)
{
if (expr.Body == expr.Parameters[0])
{
// model => model
// don't need to lock, as all identity funcs are identical
if (_identityFunc == null)
{
_identityFunc = expr.Compile();
}
return _identityFunc;
}
return null;
}
private static Func<TIn, TOut> CompileFromFingerprint(Expression<Func<TIn, TOut>> expr)
{
List<object> capturedConstants;
ExpressionFingerprintChain fingerprint = FingerprintingExpressionVisitor.GetFingerprintChain(expr, out capturedConstants);
if (fingerprint != null)
{
var del = _fingerprintedCache.GetOrAdd(fingerprint, _ =>
{
// Fingerprinting succeeded, but there was a cache miss. Rewrite the expression
// and add the rewritten expression to the cache.
var hoistedExpr = HoistingExpressionVisitor<TIn, TOut>.Hoist(expr);
return hoistedExpr.Compile();
});
return model => del(model, capturedConstants);
}
// couldn't be fingerprinted
return null;
}
private static Func<TIn, TOut> CompileFromMemberAccess(Expression<Func<TIn, TOut>> expr)
{
// Performance tests show that on the x64 platform, special-casing static member and
// captured local variable accesses is faster than letting the fingerprinting system
// handle them. On the x86 platform, the fingerprinting system is faster, but only
// by around one microsecond, so it's not worth it to complicate the logic here with
// an architecture check.
MemberExpression memberExpr = expr.Body as MemberExpression;
if (memberExpr != null)
{
if (memberExpr.Expression == expr.Parameters[0] || memberExpr.Expression == null)
{
// model => model.Member or model => StaticMember
return _simpleMemberAccessDict.GetOrAdd(memberExpr.Member, _ => expr.Compile());
}
ConstantExpression constExpr = memberExpr.Expression as ConstantExpression;
if (constExpr != null)
{
// model => {const}.Member (captured local variable)
var del = _constMemberAccessDict.GetOrAdd(memberExpr.Member, _ =>
{
// rewrite as capturedLocal => ((TDeclaringType)capturedLocal).Member
var constParamExpr = Expression.Parameter(typeof(object), "capturedLocal");
var constCastExpr = Expression.Convert(constParamExpr, memberExpr.Member.DeclaringType);
var newMemberAccessExpr = memberExpr.Update(constCastExpr);
var newLambdaExpr = Expression.Lambda<Func<object, TOut>>(newMemberAccessExpr, constParamExpr);
return newLambdaExpr.Compile();
});
object capturedLocal = constExpr.Value;
return _ => del(capturedLocal);
}
}
return null;
}
private static Func<TIn, TOut> CompileSlow(Expression<Func<TIn, TOut>> expr)
{
// fallback compilation system - just compile the expression directly
return expr.Compile();
}
}
}
internal sealed class ConditionalExpressionFingerprint : ExpressionFingerprint
{
public ConditionalExpressionFingerprint(ExpressionType nodeType, Type type)
: base(nodeType, type)
{
// There are no properties on ConditionalExpression that are worth including in
// the fingerprint.
}
public override bool Equals(object obj)
{
ConditionalExpressionFingerprint other = obj as ConditionalExpressionFingerprint;
return (other != null)
&& this.Equals(other);
}
}
internal sealed class ConstantExpressionFingerprint : ExpressionFingerprint
{
public ConstantExpressionFingerprint(ExpressionType nodeType, Type type)
: base(nodeType, type)
{
// There are no properties on ConstantExpression that are worth including in
// the fingerprint.
}
public override bool Equals(object obj)
{
ConstantExpressionFingerprint other = obj as ConstantExpressionFingerprint;
return (other != null)
&& this.Equals(other);
}
}
internal sealed class DefaultExpressionFingerprint : ExpressionFingerprint
{
public DefaultExpressionFingerprint(ExpressionType nodeType, Type type)
: base(nodeType, type)
{
// There are no properties on DefaultExpression that are worth including in
// the fingerprint.
}
public override bool Equals(object obj)
{
DefaultExpressionFingerprint other = obj as DefaultExpressionFingerprint;
return (other != null)
&& this.Equals(other);
}
}
internal abstract class ExpressionFingerprint
{
protected ExpressionFingerprint(ExpressionType nodeType, Type type)
{
NodeType = nodeType;
Type = type;
}
// the type of expression node, e.g. OP_ADD, MEMBER_ACCESS, etc.
public ExpressionType NodeType { get; private set; }
// the CLR type resulting from this expression, e.g. int, string, etc.
public Type Type { get; private set; }
internal virtual void AddToHashCodeCombiner(HashCodeCombiner combiner)
{
combiner.AddInt32((int)NodeType);
combiner.AddObject(Type);
}
protected bool Equals(ExpressionFingerprint other)
{
return (other != null)
&& (this.NodeType == other.NodeType)
&& Equals(this.Type, other.Type);
}
public override bool Equals(object obj)
{
return Equals(obj as ExpressionFingerprint);
}
public override int GetHashCode()
{
HashCodeCombiner combiner = new HashCodeCombiner();
AddToHashCodeCombiner(combiner);
return combiner.CombinedHash;
}
}
internal sealed class ExpressionFingerprintChain : IEquatable<ExpressionFingerprintChain>
{
public readonly List<ExpressionFingerprint> Elements = new List<ExpressionFingerprint>();
public bool Equals(ExpressionFingerprintChain other)
{
// Two chains are considered equal if two elements appearing in the same index in
// each chain are equal (value equality, not referential equality).
if (other == null)
{
return false;
}
if (this.Elements.Count != other.Elements.Count)
{
return false;
}
for (int i = 0; i < this.Elements.Count; i++)
{
if (!Equals(this.Elements[i], other.Elements[i]))
{
return false;
}
}
return true;
}
public override bool Equals(object obj)
{
return Equals(obj as ExpressionFingerprintChain);
}
public override int GetHashCode()
{
HashCodeCombiner combiner = new HashCodeCombiner();
Elements.ForEach(combiner.AddFingerprint);
return combiner.CombinedHash;
}
}
internal sealed class FingerprintingExpressionVisitor : ExpressionVisitor
{
private readonly List<object> _seenConstants = new List<object>();
private readonly List<ParameterExpression> _seenParameters = new List<ParameterExpression>();
private readonly ExpressionFingerprintChain _currentChain = new ExpressionFingerprintChain();
private bool _gaveUp;
private FingerprintingExpressionVisitor()
{
}
private T GiveUp<T>(T node)
{
// We don't understand this node, so just quit.
_gaveUp = true;
return node;
}
// Returns the fingerprint chain + captured constants list for this expression, or null
// if the expression couldn't be fingerprinted.
public static ExpressionFingerprintChain GetFingerprintChain(Expression expr, out List<object> capturedConstants)
{
FingerprintingExpressionVisitor visitor = new FingerprintingExpressionVisitor();
visitor.Visit(expr);
if (visitor._gaveUp)
{
capturedConstants = null;
return null;
}
else
{
capturedConstants = visitor._seenConstants;
return visitor._currentChain;
}
}
public override Expression Visit(Expression node)
{
if (node == null)
{
_currentChain.Elements.Add(null);
return null;
}
else
{
return base.Visit(node);
}
}
protected override Expression VisitBinary(BinaryExpression node)
{
if (_gaveUp)
{
return node;
}
_currentChain.Elements.Add(new BinaryExpressionFingerprint(node.NodeType, node.Type, node.Method));
return base.VisitBinary(node);
}
protected override Expression VisitBlock(BlockExpression node)
{
return GiveUp(node);
}
protected override CatchBlock VisitCatchBlock(CatchBlock node)
{
return GiveUp(node);
}
protected override Expression VisitConditional(ConditionalExpression node)
{
if (_gaveUp)
{
return node;
}
_currentChain.Elements.Add(new ConditionalExpressionFingerprint(node.NodeType, node.Type));
return base.VisitConditional(node);
}
protected override Expression VisitConstant(ConstantExpression node)
{
if (_gaveUp)
{
return node;
}
_seenConstants.Add(node.Value);
_currentChain.Elements.Add(new ConstantExpressionFingerprint(node.NodeType, node.Type));
return base.VisitConstant(node);
}
protected override Expression VisitDebugInfo(DebugInfoExpression node)
{
return GiveUp(node);
}
protected override Expression VisitDefault(DefaultExpression node)
{
if (_gaveUp)
{
return node;
}
_currentChain.Elements.Add(new DefaultExpressionFingerprint(node.NodeType, node.Type));
return base.VisitDefault(node);
}
protected override Expression VisitDynamic(DynamicExpression node)
{
return GiveUp(node);
}
protected override ElementInit VisitElementInit(ElementInit node)
{
return GiveUp(node);
}
protected override Expression VisitExtension(Expression node)
{
return GiveUp(node);
}
protected override Expression VisitGoto(GotoExpression node)
{
return GiveUp(node);
}
protected override Expression VisitIndex(IndexExpression node)
{
if (_gaveUp)
{
return node;
}
_currentChain.Elements.Add(new IndexExpressionFingerprint(node.NodeType, node.Type, node.Indexer));
return base.VisitIndex(node);
}
protected override Expression VisitInvocation(InvocationExpression node)
{
return GiveUp(node);
}
protected override Expression VisitLabel(LabelExpression node)
{
return GiveUp(node);
}
protected override LabelTarget VisitLabelTarget(LabelTarget node)
{
return GiveUp(node);
}
protected override Expression VisitLambda<T>(Expression<T> node)
{
if (_gaveUp)
{
return node;
}
_currentChain.Elements.Add(new LambdaExpressionFingerprint(node.NodeType, node.Type));
return base.VisitLambda(node);
}
protected override Expression VisitListInit(ListInitExpression node)
{
return GiveUp(node);
}
protected override Expression VisitLoop(LoopExpression node)
{
return GiveUp(node);
}
protected override Expression VisitMember(MemberExpression node)
{
if (_gaveUp)
{
return node;
}
_currentChain.Elements.Add(new MemberExpressionFingerprint(node.NodeType, node.Type, node.Member));
return base.VisitMember(node);
}
protected override MemberAssignment VisitMemberAssignment(MemberAssignment node)
{
return GiveUp(node);
}
protected override MemberBinding VisitMemberBinding(MemberBinding node)
{
return GiveUp(node);
}
protected override Expression VisitMemberInit(MemberInitExpression node)
{
return GiveUp(node);
}
protected override MemberListBinding VisitMemberListBinding(MemberListBinding node)
{
return GiveUp(node);
}
protected override MemberMemberBinding VisitMemberMemberBinding(MemberMemberBinding node)
{
return GiveUp(node);
}
protected override Expression VisitMethodCall(MethodCallExpression node)
{
if (_gaveUp)
{
return node;
}
_currentChain.Elements.Add(new MethodCallExpressionFingerprint(node.NodeType, node.Type, node.Method));
return base.VisitMethodCall(node);
}
protected override Expression VisitNew(NewExpression node)
{
return GiveUp(node);
}
protected override Expression VisitNewArray(NewArrayExpression node)
{
return GiveUp(node);
}
protected override Expression VisitParameter(ParameterExpression node)
{
if (_gaveUp)
{
return node;
}
int parameterIndex = _seenParameters.IndexOf(node);
if (parameterIndex < 0)
{
// first time seeing this parameter
parameterIndex = _seenParameters.Count;
_seenParameters.Add(node);
}
_currentChain.Elements.Add(new ParameterExpressionFingerprint(node.NodeType, node.Type, parameterIndex));
return base.VisitParameter(node);
}
protected override Expression VisitRuntimeVariables(RuntimeVariablesExpression node)
{
return GiveUp(node);
}
protected override Expression VisitSwitch(SwitchExpression node)
{
return GiveUp(node);
}
protected override SwitchCase VisitSwitchCase(SwitchCase node)
{
return GiveUp(node);
}
protected override Expression VisitTry(TryExpression node)
{
return GiveUp(node);
}
protected override Expression VisitTypeBinary(TypeBinaryExpression node)
{
if (_gaveUp)
{
return node;
}
_currentChain.Elements.Add(new TypeBinaryExpressionFingerprint(node.NodeType, node.Type, node.TypeOperand));
return base.VisitTypeBinary(node);
}
protected override Expression VisitUnary(UnaryExpression node)
{
if (_gaveUp)
{
return node;
}
_currentChain.Elements.Add(new UnaryExpressionFingerprint(node.NodeType, node.Type, node.Method));
return base.VisitUnary(node);
}
}
internal class HashCodeCombiner
{
private long _combinedHash64 = 0x1505L;
public int CombinedHash
{
get { return _combinedHash64.GetHashCode(); }
}
public void AddFingerprint(ExpressionFingerprint fingerprint)
{
if (fingerprint != null)
{
fingerprint.AddToHashCodeCombiner(this);
}
else
{
AddInt32(0);
}
}
public void AddEnumerable(IEnumerable e)
{
if (e == null)
{
AddInt32(0);
}
else
{
int count = 0;
foreach (object o in e)
{
AddObject(o);
count++;
}
AddInt32(count);
}
}
public void AddInt32(int i)
{
_combinedHash64 = ((_combinedHash64 << 5) + _combinedHash64) ^ i;
}
public void AddObject(object o)
{
int hashCode = (o != null) ? o.GetHashCode() : 0;
AddInt32(hashCode);
}
}
internal delegate TValue Hoisted<in TModel, out TValue>(TModel model, List<object> capturedConstants);
internal sealed class HoistingExpressionVisitor<TIn, TOut> : ExpressionVisitor
{
private static readonly ParameterExpression _hoistedConstantsParamExpr = Expression.Parameter(typeof(List<object>), "hoistedConstants");
private int _numConstantsProcessed;
// factory will create instance
private HoistingExpressionVisitor()
{
}
public static Expression<Hoisted<TIn, TOut>> Hoist(Expression<Func<TIn, TOut>> expr)
{
// rewrite Expression<Func<TIn, TOut>> as Expression<Hoisted<TIn, TOut>>
var visitor = new HoistingExpressionVisitor<TIn, TOut>();
var rewrittenBodyExpr = visitor.Visit(expr.Body);
var rewrittenLambdaExpr = Expression.Lambda<Hoisted<TIn, TOut>>(rewrittenBodyExpr, expr.Parameters[0], _hoistedConstantsParamExpr);
return rewrittenLambdaExpr;
}
protected override Expression VisitConstant(ConstantExpression node)
{
// rewrite the constant expression as (TConst)hoistedConstants[i];
return Expression.Convert(Expression.Property(_hoistedConstantsParamExpr, "Item", Expression.Constant(_numConstantsProcessed++)), node.Type);
}
}
internal sealed class IndexExpressionFingerprint : ExpressionFingerprint
{
public IndexExpressionFingerprint(ExpressionType nodeType, Type type, PropertyInfo indexer)
: base(nodeType, type)
{
// Other properties on IndexExpression (like the argument count) are simply derived
// from Type and Indexer, so they're not necessary for inclusion in the fingerprint.
Indexer = indexer;
}
// http://msdn.microsoft.com/en-us/library/system.linq.expressions.indexexpression.indexer.aspx
public PropertyInfo Indexer { get; private set; }
public override bool Equals(object obj)
{
IndexExpressionFingerprint other = obj as IndexExpressionFingerprint;
return (other != null)
&& Equals(this.Indexer, other.Indexer)
&& this.Equals(other);
}
internal override void AddToHashCodeCombiner(HashCodeCombiner combiner)
{
combiner.AddObject(Indexer);
base.AddToHashCodeCombiner(combiner);
}
}
internal sealed class LambdaExpressionFingerprint : ExpressionFingerprint
{
public LambdaExpressionFingerprint(ExpressionType nodeType, Type type)
: base(nodeType, type)
{
// There are no properties on LambdaExpression that are worth including in
// the fingerprint.
}
public override bool Equals(object obj)
{
LambdaExpressionFingerprint other = obj as LambdaExpressionFingerprint;
return (other != null)
&& this.Equals(other);
}
}
internal sealed class MemberExpressionFingerprint : ExpressionFingerprint
{
public MemberExpressionFingerprint(ExpressionType nodeType, Type type, MemberInfo member)
: base(nodeType, type)
{
Member = member;
}
// http://msdn.microsoft.com/en-us/library/system.linq.expressions.memberexpression.member.aspx
public MemberInfo Member { get; private set; }
public override bool Equals(object obj)
{
MemberExpressionFingerprint other = obj as MemberExpressionFingerprint;
return (other != null)
&& Equals(this.Member, other.Member)
&& this.Equals(other);
}
internal override void AddToHashCodeCombiner(HashCodeCombiner combiner)
{
combiner.AddObject(Member);
base.AddToHashCodeCombiner(combiner);
}
}
internal sealed class MethodCallExpressionFingerprint : ExpressionFingerprint
{
public MethodCallExpressionFingerprint(ExpressionType nodeType, Type type, MethodInfo method)
: base(nodeType, type)
{
// Other properties on MethodCallExpression (like the argument count) are simply derived
// from Type and Indexer, so they're not necessary for inclusion in the fingerprint.
Method = method;
}
// http://msdn.microsoft.com/en-us/library/system.linq.expressions.methodcallexpression.method.aspx
public MethodInfo Method { get; private set; }
public override bool Equals(object obj)
{
MethodCallExpressionFingerprint other = obj as MethodCallExpressionFingerprint;
return (other != null)
&& Equals(this.Method, other.Method)
&& this.Equals(other);
}
internal override void AddToHashCodeCombiner(HashCodeCombiner combiner)
{
combiner.AddObject(Method);
base.AddToHashCodeCombiner(combiner);
}
}
internal sealed class ParameterExpressionFingerprint : ExpressionFingerprint
{
public ParameterExpressionFingerprint(ExpressionType nodeType, Type type, int parameterIndex)
: base(nodeType, type)
{
ParameterIndex = parameterIndex;
}
// Parameter position within the overall expression, used to maintain alpha equivalence.
public int ParameterIndex { get; private set; }
public override bool Equals(object obj)
{
ParameterExpressionFingerprint other = obj as ParameterExpressionFingerprint;
return (other != null)
&& (this.ParameterIndex == other.ParameterIndex)
&& this.Equals(other);
}
internal override void AddToHashCodeCombiner(HashCodeCombiner combiner)
{
combiner.AddInt32(ParameterIndex);
base.AddToHashCodeCombiner(combiner);
}
}
internal sealed class TypeBinaryExpressionFingerprint : ExpressionFingerprint
{
public TypeBinaryExpressionFingerprint(ExpressionType nodeType, Type type, Type typeOperand)
: base(nodeType, type)
{
TypeOperand = typeOperand;
}
// http://msdn.microsoft.com/en-us/library/system.linq.expressions.typebinaryexpression.typeoperand.aspx
public Type TypeOperand { get; private set; }
public override bool Equals(object obj)
{
TypeBinaryExpressionFingerprint other = obj as TypeBinaryExpressionFingerprint;
return (other != null)
&& Equals(this.TypeOperand, other.TypeOperand)
&& this.Equals(other);
}
internal override void AddToHashCodeCombiner(HashCodeCombiner combiner)
{
combiner.AddObject(TypeOperand);
base.AddToHashCodeCombiner(combiner);
}
}
internal sealed class UnaryExpressionFingerprint : ExpressionFingerprint
{
public UnaryExpressionFingerprint(ExpressionType nodeType, Type type, MethodInfo method)
: base(nodeType, type)
{
// Other properties on UnaryExpression (like IsLifted / IsLiftedToNull) are simply derived
// from Type and NodeType, so they're not necessary for inclusion in the fingerprint.
Method = method;
}
// http://msdn.microsoft.com/en-us/library/system.linq.expressions.unaryexpression.method.aspx
public MethodInfo Method { get; private set; }
public override bool Equals(object obj)
{
UnaryExpressionFingerprint other = obj as UnaryExpressionFingerprint;
return (other != null)
&& Equals(this.Method, other.Method)
&& this.Equals(other);
}
internal override void AddToHashCodeCombiner(HashCodeCombiner combiner)
{
combiner.AddObject(Method);
base.AddToHashCodeCombiner(combiner);
}
}
}