-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExprEqualityComparer.cs
1387 lines (1176 loc) · 54.1 KB
/
ExprEqualityComparer.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
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
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Linq.Expressions;
using ZySharp.Metaprogramming.Extensions.Expressions;
using ZySharp.Metaprogramming.Extensions.Reflection;
using ZySharp.Validation;
namespace ZySharp.Metaprogramming;
/// <summary>
/// Implements the <see cref="IEqualityComparer{T}"/> interface for the <see cref="Expression"/> type to check
/// for semantic equality.
/// </summary>
/// <remarks>
/// <para>
/// The comparer is unable to process the <see cref="DynamicExpression"/> type.
/// </para>
/// <para>
/// The comparer is unable to process extension expression types. Consider calling <see cref="Expression.Reduce"/>
/// one them before passing the expression tree to the comparer.
/// </para>
/// <para>
/// For <see cref="ConstantExpression"/> expressions the constant value is accessed using (if supported) the
/// <see cref="IEqualityComparer{T}"/> interface or the default <see cref="object.Equals(object)"/> and
/// <see cref="object.GetHashCode()"/> methods.
/// </para>
/// <para>
/// Captured value-type references are converted to <see cref="ConstantExpression"/> nodes with a snapshot of
/// their current values. See <see cref="ExprExtensions.Snapshot"/> for further information.
/// </para>
/// </remarks>
public class ExprEqualityComparer :
IEqualityComparer<Expression>
{
/// <summary>
/// The equality comparer flags set during initialization.
/// </summary>
protected ExprEqualityComparerFlags Flags { get; }
/// <summary>
/// Returns the default <see cref="ExprEqualityComparer"/> instance.
/// </summary>
public static ExprEqualityComparer Default { get; } = new();
/// <summary>
/// Creates a new <see cref="ExprEqualityComparer"/> instance with the <see cref="ExprEqualityComparerFlags.Default"/> flags.
/// </summary>
public ExprEqualityComparer() :
this(ExprEqualityComparerFlags.Default)
{
}
/// <summary>
/// Creates a new <see cref="ExprEqualityComparer"/> instance with custom <see cref="ExprEqualityComparerFlags"/>.
/// </summary>
/// <param name="flags">The flags.</param>
public ExprEqualityComparer(ExprEqualityComparerFlags flags)
{
Flags = flags;
}
#region IEqualityComparer
/// <inheritdoc cref="IEqualityComparer{T}.Equals(T,T)"/>
/// <exception cref="NotSupportedException">If the expression type is not supported.</exception>
/// <exception cref="NotImplementedException">If the expression type is unknown.</exception>
[Pure]
public bool Equals(Expression? x, Expression? y)
{
var context = new Context(this);
return context.CompareExpr(x.Snapshot(), y.Snapshot());
}
/// <inheritdoc cref="IEqualityComparer{T}.GetHashCode(T)"/>
/// <exception cref="NotSupportedException">If the expression type is not supported.</exception>
/// <exception cref="NotImplementedException">If the expression type is unknown.</exception>
[Pure]
public int GetHashCode(Expression obj)
{
ValidateArgument.For(obj, nameof(obj), v => v.NotNull());
var context = new Context(this);
context.HashExpr(obj.Snapshot());
return context.ToHashCode();
}
#endregion IEqualityComparer
#region Compare Methods
/// <summary>
/// Determines whether expression <paramref name="x"/> and expression <paramref name="y"/> are semantically
/// equal.
/// </summary>
/// <param name="context">The current equality comparer context.</param>
/// <param name="x">The first expression.</param>
/// <param name="y">The second expression.</param>
/// <returns><c>True</c> if both expressions are semantically equal.</returns>
/// <exception cref="NotSupportedException">If the expression type is not supported.</exception>
/// <exception cref="NotImplementedException">If the expression type is unknown.</exception>
protected virtual bool Compare(Context context, Expression? x, Expression? y)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
return (x, y) switch
{
(null, null) => true,
(null, not null) => false,
(not null, null) => false,
(BinaryExpression e1, BinaryExpression e2) => CompareBinary(context, e1, e2),
(BlockExpression e1, BlockExpression e2) => CompareBlock(context, e1, e2),
(ConditionalExpression e1, ConditionalExpression e2) => CompareConditional(context, e1, e2),
(ConstantExpression e1, ConstantExpression e2) => CompareConstant(context, e1, e2),
(DebugInfoExpression e1, DebugInfoExpression e2) => CompareDebugInfo(context, e1, e2),
(DefaultExpression e1, DefaultExpression e2) => CompareDefault(context, e1, e2),
(DynamicExpression, DynamicExpression) => throw new NotSupportedException(),
(GotoExpression e1, GotoExpression e2) => CompareGoto(context, e1, e2),
(IndexExpression e1, IndexExpression e2) => CompareIndex(context, e1, e2),
(InvocationExpression e1, InvocationExpression e2) => CompareInvocation(context, e1, e2),
(LabelExpression e1, LabelExpression e2) => CompareLabel(context, e1, e2),
(LambdaExpression e1, LambdaExpression e2) => CompareLambda(context, e1, e2),
(ListInitExpression e1, ListInitExpression e2) => CompareListInit(context, e1, e2),
(LoopExpression e1, LoopExpression e2) => CompareLoop(context, e1, e2),
(MemberExpression e1, MemberExpression e2) => CompareMember(context, e1, e2),
(MemberInitExpression e1, MemberInitExpression e2) => CompareMemberInit(context, e1, e2),
(MethodCallExpression e1, MethodCallExpression e2) => CompareMethodCall(context, e1, e2),
(NewArrayExpression e1, NewArrayExpression e2) => CompareNewArray(context, e1, e2),
(NewExpression e1, NewExpression e2) => CompareNew(context, e1, e2),
(ParameterExpression e1, ParameterExpression e2) => CompareParameter(context, e1, e2),
(RuntimeVariablesExpression e1, RuntimeVariablesExpression e2) => CompareRuntimeVariables(context, e1, e2),
(SwitchExpression e1, SwitchExpression e2) => CompareSwitch(context, e1, e2),
(TryExpression e1, TryExpression e2) => CompareTry(context, e1, e2),
(TypeBinaryExpression e1, TypeBinaryExpression e2) => CompareTypeBinary(context, e1, e2),
(UnaryExpression e1, UnaryExpression e2) => CompareUnary(context, e1, e2),
(not null, not null) when (x?.GetType() != y?.GetType()) => false,
_ => throw new NotImplementedException()
};
}
/// <inheritdoc cref="Compare"/>
protected virtual bool CompareBase(Context context, Expression x, Expression y)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(x, nameof(x), v => v.NotNull());
ValidateArgument.For(y, nameof(y), v => v.NotNull());
var ignoreType = ((x is LambdaExpression) && Flags.HasFlag(ExprEqualityComparerFlags.IgnoreLambdaType));
return context.Compare(x.CanReduce, y.CanReduce) &&
context.Compare(x.NodeType, y.NodeType) &&
context.Compare(x.Type, y.Type, ignoreType);
}
/// <inheritdoc cref="Compare"/>
protected virtual bool CompareBinary(Context context, BinaryExpression x, BinaryExpression y)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(x, nameof(x), v => v.NotNull());
ValidateArgument.For(y, nameof(y), v => v.NotNull());
return CompareBase(context, x, y) &&
context.CompareExpr(x.Conversion, y.Conversion) &&
context.Compare(x.IsLifted, y.IsLifted) &&
context.Compare(x.IsLiftedToNull, y.IsLiftedToNull) &&
context.CompareExpr(x.Left, y.Left) &&
context.Compare(x.Method, y.Method) &&
context.CompareExpr(x.Right, y.Right);
}
/// <inheritdoc cref="Compare"/>
protected virtual bool CompareBlock(Context context, BlockExpression x, BlockExpression y)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(x, nameof(x), v => v.NotNull());
ValidateArgument.For(y, nameof(y), v => v.NotNull());
return CompareBase(context, x, y) &&
context.CompareExpr(x.Expressions, y.Expressions) &&
context.CompareExpr(x.Result, y.Result) &&
context.CompareExpr(x.Variables, y.Variables);
}
/// <inheritdoc cref="Compare"/>
protected virtual bool CompareConditional(Context context, ConditionalExpression x, ConditionalExpression y)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(x, nameof(x), v => v.NotNull());
ValidateArgument.For(y, nameof(y), v => v.NotNull());
return CompareBase(context, x, y) &&
context.CompareExpr(x.IfFalse, y.IfFalse) &&
context.CompareExpr(x.IfTrue, y.IfTrue) &&
context.CompareExpr(x.Test, y.Test);
}
/// <inheritdoc cref="Compare"/>
protected virtual bool CompareConstant(Context context, ConstantExpression x, ConstantExpression y)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(x, nameof(x), v => v.NotNull());
ValidateArgument.For(y, nameof(y), v => v.NotNull());
return CompareBase(context, x, y) &&
context.Compare(x.Value, y.Value);
}
/// <inheritdoc cref="Compare"/>
protected virtual bool CompareDebugInfo(Context context, DebugInfoExpression x, DebugInfoExpression y)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(x, nameof(x), v => v.NotNull());
ValidateArgument.For(y, nameof(y), v => v.NotNull());
return CompareBase(context, x, y) &&
context.Compare(x.Document, y.Document, CompareDocument) &&
context.Compare(x.EndColumn, y.EndColumn) &&
context.Compare(x.EndLine, y.EndLine) &&
context.Compare(x.IsClear, y.IsClear) &&
context.Compare(x.StartColumn, y.StartColumn) &&
context.Compare(x.StartLine, y.StartLine);
}
/// <inheritdoc cref="Compare"/>
protected virtual bool CompareDefault(Context context, DefaultExpression x, DefaultExpression y)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(x, nameof(x), v => v.NotNull());
ValidateArgument.For(y, nameof(y), v => v.NotNull());
return CompareBase(context, x, y);
}
/// <inheritdoc cref="Compare"/>
protected virtual bool CompareGoto(Context context, GotoExpression x, GotoExpression y)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(x, nameof(x), v => v.NotNull());
ValidateArgument.For(y, nameof(y), v => v.NotNull());
return CompareBase(context, x, y) &&
context.Compare(x.Kind, y.Kind) &&
context.Compare(x.Target, y.Target, CompareLabelTarget) &&
context.CompareExpr(x.Value, y.Value);
}
/// <inheritdoc cref="Compare"/>
protected virtual bool CompareIndex(Context context, IndexExpression x, IndexExpression y)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(x, nameof(x), v => v.NotNull());
ValidateArgument.For(y, nameof(y), v => v.NotNull());
return CompareBase(context, x, y) &&
context.CompareExpr(x.Arguments, y.Arguments) &&
context.Compare(x.Indexer, y.Indexer) &&
context.CompareExpr(x.Object, y.Object);
}
/// <inheritdoc cref="Compare"/>
protected virtual bool CompareInvocation(Context context, InvocationExpression x, InvocationExpression y)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(x, nameof(x), v => v.NotNull());
ValidateArgument.For(y, nameof(y), v => v.NotNull());
return CompareBase(context, x, y) &&
context.CompareExpr(x.Arguments, y.Arguments) &&
context.CompareExpr(x.Expression, y.Expression);
}
/// <inheritdoc cref="Compare"/>
protected virtual bool CompareLabel(Context context, LabelExpression x, LabelExpression y)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(x, nameof(x), v => v.NotNull());
ValidateArgument.For(y, nameof(y), v => v.NotNull());
return CompareBase(context, x, y) &&
context.CompareExpr(x.DefaultValue, y.DefaultValue) &&
context.Compare(x.Target, y.Target, CompareLabelTarget);
}
/// <inheritdoc cref="Compare"/>
protected virtual bool CompareLambda(Context context, LambdaExpression x, LambdaExpression y)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(x, nameof(x), v => v.NotNull());
ValidateArgument.For(y, nameof(y), v => v.NotNull());
return CompareBase(context, x, y) &&
context.CompareExpr(x.Body, y.Body) &&
context.Compare(x.Name, y.Name, Flags.HasFlag(ExprEqualityComparerFlags.IgnoreLambdaName)) &&
context.CompareExpr(x.Parameters, y.Parameters) &&
context.Compare(x.ReturnType, y.ReturnType) &&
context.Compare(x.TailCall, y.TailCall);
}
/// <inheritdoc cref="Compare"/>
protected virtual bool CompareListInit(Context context, ListInitExpression x, ListInitExpression y)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(x, nameof(x), v => v.NotNull());
ValidateArgument.For(y, nameof(y), v => v.NotNull());
return CompareBase(context, x, y) &&
context.CompareMany(x.Initializers, y.Initializers, CompareElementInit) &&
context.CompareExpr(x.NewExpression, y.NewExpression);
}
/// <inheritdoc cref="Compare"/>
protected virtual bool CompareLoop(Context context, LoopExpression x, LoopExpression y)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(x, nameof(x), v => v.NotNull());
ValidateArgument.For(y, nameof(y), v => v.NotNull());
return CompareBase(context, x, y) &&
context.CompareExpr(x.Body, y.Body) &&
context.Compare(x.BreakLabel, y.BreakLabel, CompareLabelTarget) &&
context.Compare(x.ContinueLabel, y.ContinueLabel, CompareLabelTarget);
}
/// <inheritdoc cref="Compare"/>
protected virtual bool CompareMember(Context context, MemberExpression x, MemberExpression y)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(x, nameof(x), v => v.NotNull());
ValidateArgument.For(y, nameof(y), v => v.NotNull());
return CompareBase(context, x, y) &&
context.CompareExpr(x.Expression, y.Expression) &&
context.Compare(x.Member, y.Member);
}
/// <inheritdoc cref="Compare"/>
protected virtual bool CompareMemberInit(Context context, MemberInitExpression x, MemberInitExpression y)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(x, nameof(x), v => v.NotNull());
ValidateArgument.For(y, nameof(y), v => v.NotNull());
return CompareBase(context, x, y) &&
context.CompareMany(x.Bindings, y.Bindings, CompareMemberBinding) &&
context.CompareExpr(x.NewExpression, y.NewExpression);
}
/// <inheritdoc cref="Compare"/>
protected virtual bool CompareMethodCall(Context context, MethodCallExpression x, MethodCallExpression y)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(x, nameof(x), v => v.NotNull());
ValidateArgument.For(y, nameof(y), v => v.NotNull());
return CompareBase(context, x, y) &&
context.CompareExpr(x.Arguments, y.Arguments) &&
context.Compare(x.Method, y.Method) &&
context.CompareExpr(x.Object, y.Object);
}
/// <inheritdoc cref="Compare"/>
protected virtual bool CompareNewArray(Context context, NewArrayExpression x, NewArrayExpression y)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(x, nameof(x), v => v.NotNull());
ValidateArgument.For(y, nameof(y), v => v.NotNull());
return CompareBase(context, x, y) &&
context.CompareExpr(x.Expressions, y.Expressions);
}
#pragma warning disable CA1711
/// <inheritdoc cref="Compare"/>
protected virtual bool CompareNew(Context context, NewExpression x, NewExpression y)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(x, nameof(x), v => v.NotNull());
ValidateArgument.For(y, nameof(y), v => v.NotNull());
return CompareBase(context, x, y) &&
context.CompareExpr(x.Arguments, y.Arguments) &&
context.Compare(x.Constructor, y.Constructor) &&
context.CompareMany(x.Members, y.Members);
}
#pragma warning restore CA1711
/// <inheritdoc cref="Compare"/>
protected virtual bool CompareParameter(Context context, ParameterExpression x, ParameterExpression y)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(x, nameof(x), v => v.NotNull());
ValidateArgument.For(y, nameof(y), v => v.NotNull());
if (!context.CompareReferenceId(x, y))
{
return false;
}
return CompareBase(context, x, y) &&
context.Compare(x.IsByRef, y.IsByRef) &&
context.Compare(x.Name, y.Name, Flags.HasFlag(ExprEqualityComparerFlags.IgnoreParameterName));
}
/// <inheritdoc cref="Compare"/>
protected virtual bool CompareRuntimeVariables(Context context, RuntimeVariablesExpression x, RuntimeVariablesExpression y)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(x, nameof(x), v => v.NotNull());
ValidateArgument.For(y, nameof(y), v => v.NotNull());
return CompareBase(context, x, y) &&
context.CompareExpr(x.Variables, y.Variables);
}
/// <inheritdoc cref="Compare"/>
protected virtual bool CompareSwitch(Context context, SwitchExpression x, SwitchExpression y)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(x, nameof(x), v => v.NotNull());
ValidateArgument.For(y, nameof(y), v => v.NotNull());
return CompareBase(context, x, y) &&
context.CompareMany(x.Cases, y.Cases, CompareSwitchCase) &&
context.Compare(x.Comparison, y.Comparison) &&
context.CompareExpr(x.DefaultBody, y.DefaultBody) &&
context.CompareExpr(x.SwitchValue, y.SwitchValue);
}
/// <inheritdoc cref="Compare"/>
protected virtual bool CompareTry(Context context, TryExpression x, TryExpression y)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(x, nameof(x), v => v.NotNull());
ValidateArgument.For(y, nameof(y), v => v.NotNull());
return CompareBase(context, x, y) &&
context.CompareExpr(x.Body, y.Body) &&
context.CompareExpr(x.Fault, y.Fault) &&
context.CompareExpr(x.Finally, y.Finally) &&
context.CompareMany(x.Handlers, y.Handlers, CompareCatchBlock);
}
/// <inheritdoc cref="Compare"/>
protected virtual bool CompareTypeBinary(Context context, TypeBinaryExpression x, TypeBinaryExpression y)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(x, nameof(x), v => v.NotNull());
ValidateArgument.For(y, nameof(y), v => v.NotNull());
return CompareBase(context, x, y) &&
context.CompareExpr(x.Expression, y.Expression) &&
context.Compare(x.TypeOperand, y.TypeOperand);
}
/// <inheritdoc cref="Compare"/>
protected virtual bool CompareUnary(Context context, UnaryExpression x, UnaryExpression y)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(x, nameof(x), v => v.NotNull());
ValidateArgument.For(y, nameof(y), v => v.NotNull());
return CompareBase(context, x, y) &&
context.Compare(x.IsLifted, y.IsLifted) &&
context.Compare(x.IsLiftedToNull, y.IsLiftedToNull) &&
context.Compare(x.Method, y.Method) &&
context.CompareExpr(x.Operand, y.Operand);
}
private bool CompareDocument(Context context, SymbolDocumentInfo x, SymbolDocumentInfo y)
{
return context.Compare(x.DocumentType, y.DocumentType) &&
context.Compare(x.FileName, y.FileName) &&
context.Compare(x.Language, y.Language) &&
context.Compare(x.LanguageVendor, y.LanguageVendor);
}
private bool CompareLabelTarget(Context context, LabelTarget x, LabelTarget y)
{
if (!context.CompareReferenceId(x, y))
{
return false;
}
return context.Compare(x.Type, y.Type) &&
context.Compare(x.Name, y.Name, Flags.HasFlag(ExprEqualityComparerFlags.IgnoreLabelName));
}
private bool CompareElementInit(Context context, ElementInit x, ElementInit y)
{
return context.Compare(x.AddMethod, y.AddMethod) &&
context.CompareExpr(x.Arguments, y.Arguments);
}
private bool CompareMemberBinding(Context context, MemberBinding x, MemberBinding y)
{
if (x.GetType() != y.GetType())
{
return false;
}
if (!context.Compare(x.BindingType, y.BindingType) ||
!context.Compare(x.Member, y.Member))
{
return false;
}
return (x, y) switch
{
(MemberAssignment mb1, MemberAssignment mb2) => context.CompareExpr(mb1.Expression, mb2.Expression),
(MemberListBinding mb1, MemberListBinding mb2) => context.CompareMany(mb1.Initializers, mb2.Initializers, CompareElementInit),
(MemberMemberBinding mb1, MemberMemberBinding mb2) => context.CompareMany(mb1.Bindings, mb2.Bindings, CompareMemberBinding),
_ => throw new NotImplementedException()
};
}
private bool CompareSwitchCase(Context context, SwitchCase x, SwitchCase y)
{
return context.CompareExpr(x.Body, y.Body) &&
context.CompareExpr(x.TestValues, y.TestValues);
}
private bool CompareCatchBlock(Context context, CatchBlock x, CatchBlock y)
{
return context.CompareExpr(x.Body, y.Body) &&
context.CompareExpr(x.Filter, y.Filter) &&
context.Compare(x.Test, y.Test) &&
context.CompareExpr(x.Variable, y.Variable);
}
#endregion Compare Methods
#region Hash Methods
/// <summary>
/// Adds the given expression to the hash code.
/// </summary>
/// <param name="context">The current equality comparer context.</param>
/// <param name="node">The expression node.</param>
/// <exception cref="NotSupportedException">If the expression type is not supported.</exception>
/// <exception cref="NotImplementedException">If the expression type unknown.</exception>
protected virtual void HashExpression(Context context, Expression? node)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
switch (node)
{
case null:
context.Hash(0);
break;
case BinaryExpression e:
HashBinary(context, e);
break;
case BlockExpression e:
HashBlock(context, e);
break;
case ConditionalExpression e:
HashConditional(context, e);
break;
case ConstantExpression e:
HashConstant(context, e);
break;
case DebugInfoExpression e:
HashDebugInfo(context, e);
break;
case DefaultExpression e:
HashDefault(context, e);
break;
case DynamicExpression:
throw new NotSupportedException();
case GotoExpression e:
HashGoto(context, e);
break;
case IndexExpression e:
HashIndex(context, e);
break;
case InvocationExpression e:
HashInvocation(context, e);
break;
case LabelExpression e:
HashLabel(context, e);
break;
case LambdaExpression e:
HashLambda(context, e);
break;
case ListInitExpression e:
HashListInit(context, e);
break;
case LoopExpression e:
HashLoop(context, e);
break;
case MemberExpression e:
HashMember(context, e);
break;
case MemberInitExpression e:
HashMemberInit(context, e);
break;
case MethodCallExpression e:
HashMethodCall(context, e);
break;
case NewArrayExpression e:
HashNewArray(context, e);
break;
case NewExpression e:
HashNew(context, e);
break;
case ParameterExpression e:
HashParameter(context, e);
break;
case RuntimeVariablesExpression e:
HashRuntimeVariables(context, e);
break;
case SwitchExpression e:
HashSwitch(context, e);
break;
case TryExpression e:
HashTry(context, e);
break;
case TypeBinaryExpression e:
HashTypeBinary(context, e);
break;
case UnaryExpression e:
HashUnary(context, e);
break;
default:
throw new NotImplementedException();
}
}
/// <inheritdoc cref="HashExpression"/>
protected virtual void HashBase(Context context, Expression node)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(node, nameof(node), v => v.NotNull());
var ignoreType = ((node is LambdaExpression) && Flags.HasFlag(ExprEqualityComparerFlags.IgnoreLambdaType));
context.Hash(node.CanReduce);
context.Hash(node.NodeType);
context.Hash(node.Type, ignoreType);
}
/// <inheritdoc cref="HashExpression"/>
protected virtual void HashBinary(Context context, BinaryExpression node)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(node, nameof(node), v => v.NotNull());
HashBase(context, node);
context.HashExpr(node.Conversion);
context.Hash(node.IsLifted);
context.Hash(node.IsLiftedToNull);
context.HashExpr(node.Left);
context.Hash(node.Method);
context.HashExpr(node.Right);
}
/// <inheritdoc cref="HashExpression"/>
protected virtual void HashBlock(Context context, BlockExpression node)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(node, nameof(node), v => v.NotNull());
HashBase(context, node);
context.HashExpr(node.Expressions);
context.HashExpr(node.Result);
context.HashExpr(node.Variables);
}
/// <inheritdoc cref="HashExpression"/>
protected virtual void HashConditional(Context context, ConditionalExpression node)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(node, nameof(node), v => v.NotNull());
HashBase(context, node);
context.HashExpr(node.IfFalse);
context.HashExpr(node.IfTrue);
context.HashExpr(node.Test);
}
/// <inheritdoc cref="HashExpression"/>
protected virtual void HashConstant(Context context, ConstantExpression node)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(node, nameof(node), v => v.NotNull());
HashBase(context, node);
context.Hash(node.Value);
}
/// <inheritdoc cref="HashExpression"/>
protected virtual void HashDebugInfo(Context context, DebugInfoExpression node)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(node, nameof(node), v => v.NotNull());
HashBase(context, node);
context.Hash(node.EndColumn);
context.Hash(node.EndLine);
context.Hash(node.Document, HashDocument);
context.Hash(node.IsClear);
context.Hash(node.StartColumn);
context.Hash(node.StartLine);
}
/// <inheritdoc cref="HashExpression"/>
protected virtual void HashDefault(Context context, DefaultExpression node)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(node, nameof(node), v => v.NotNull());
HashBase(context, node);
}
/// <inheritdoc cref="HashExpression"/>
protected virtual void HashGoto(Context context, GotoExpression node)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(node, nameof(node), v => v.NotNull());
HashBase(context, node);
context.Hash(node.Kind);
context.Hash(node.Target, HashLabelTarget);
context.HashExpr(node.Value);
}
/// <inheritdoc cref="HashExpression"/>
protected virtual void HashIndex(Context context, IndexExpression node)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(node, nameof(node), v => v.NotNull());
HashBase(context, node);
context.HashExpr(node.Arguments);
context.Hash(node.Indexer);
context.HashExpr(node.Object);
}
/// <inheritdoc cref="HashExpression"/>
protected virtual void HashInvocation(Context context, InvocationExpression node)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(node, nameof(node), v => v.NotNull());
HashBase(context, node);
context.HashExpr(node.Arguments);
context.HashExpr(node.Expression);
}
/// <inheritdoc cref="HashExpression"/>
protected virtual void HashLabel(Context context, LabelExpression node)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(node, nameof(node), v => v.NotNull());
HashBase(context, node);
context.HashExpr(node.DefaultValue);
context.Hash(node.Target, HashLabelTarget);
}
/// <inheritdoc cref="HashExpression"/>
protected virtual void HashLambda(Context context, LambdaExpression node)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(node, nameof(node), v => v.NotNull());
HashBase(context, node);
context.HashExpr(node.Body);
context.Hash(node.Name, Flags.HasFlag(ExprEqualityComparerFlags.IgnoreLambdaName));
context.HashExpr(node.Parameters);
context.Hash(node.ReturnType);
context.Hash(node.TailCall);
}
/// <inheritdoc cref="HashExpression"/>
protected virtual void HashListInit(Context context, ListInitExpression node)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(node, nameof(node), v => v.NotNull());
HashBase(context, node);
context.HashMany(node.Initializers, HashElementInit);
context.HashExpr(node.NewExpression);
}
/// <inheritdoc cref="HashExpression"/>
protected virtual void HashLoop(Context context, LoopExpression node)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(node, nameof(node), v => v.NotNull());
HashBase(context, node);
context.HashExpr(node.Body);
context.Hash(node.BreakLabel, HashLabelTarget);
context.Hash(node.ContinueLabel, HashLabelTarget);
}
/// <inheritdoc cref="HashExpression"/>
protected virtual void HashMember(Context context, MemberExpression node)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(node, nameof(node), v => v.NotNull());
HashBase(context, node);
context.HashExpr(node.Expression);
context.Hash(node.Member);
}
/// <inheritdoc cref="HashExpression"/>
protected virtual void HashMemberInit(Context context, MemberInitExpression node)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(node, nameof(node), v => v.NotNull());
HashBase(context, node);
context.HashMany(node.Bindings, HashMemberBinding);
context.HashExpr(node.NewExpression);
}
/// <inheritdoc cref="HashExpression"/>
protected virtual void HashMethodCall(Context context, MethodCallExpression node)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(node, nameof(node), v => v.NotNull());
HashBase(context, node);
context.HashExpr(node.Arguments);
context.Hash(node.Method);
context.HashExpr(node.Object);
}
/// <inheritdoc cref="HashExpression"/>
protected virtual void HashNewArray(Context context, NewArrayExpression node)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(node, nameof(node), v => v.NotNull());
HashBase(context, node);
context.HashExpr(node.Expressions);
}
/// <inheritdoc cref="HashExpression"/>
protected virtual void HashNew(Context context, NewExpression node)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(node, nameof(node), v => v.NotNull());
HashBase(context, node);
context.HashExpr(node.Arguments);
context.Hash(node.Constructor);
context.HashMany(node.Members!);
}
/// <inheritdoc cref="HashExpression"/>
protected virtual void HashParameter(Context context, ParameterExpression node)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(node, nameof(node), v => v.NotNull());
HashBase(context, node);
context.HashReferenceId(node);
context.Hash(node.IsByRef);
context.Hash(node.Name, Flags.HasFlag(ExprEqualityComparerFlags.IgnoreParameterName));
}
/// <inheritdoc cref="HashExpression"/>
protected virtual void HashRuntimeVariables(Context context, RuntimeVariablesExpression node)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(node, nameof(node), v => v.NotNull());
HashBase(context, node);
context.HashExpr(node.Variables);
}
/// <inheritdoc cref="HashExpression"/>
protected virtual void HashSwitch(Context context, SwitchExpression node)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(node, nameof(node), v => v.NotNull());
HashBase(context, node);
context.HashMany(node.Cases, HashSwitchCase);
context.Hash(node.Comparison);
context.HashExpr(node.DefaultBody);
context.HashExpr(node.SwitchValue);
}
/// <inheritdoc cref="HashExpression"/>
protected virtual void HashTry(Context context, TryExpression node)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(node, nameof(node), v => v.NotNull());
HashBase(context, node);
context.HashExpr(node.Body);
context.HashExpr(node.Fault);
context.HashExpr(node.Finally);
context.HashMany(node.Handlers, HashCatchBlock);
}
/// <inheritdoc cref="HashExpression"/>
protected virtual void HashTypeBinary(Context context, TypeBinaryExpression node)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(node, nameof(node), v => v.NotNull());
HashBase(context, node);
context.HashExpr(node.Expression);
context.Hash(node.TypeOperand);
}
/// <inheritdoc cref="HashExpression"/>
protected virtual void HashUnary(Context context, UnaryExpression node)
{
ValidateArgument.For(context, nameof(context), v => v.NotNull());
ValidateArgument.For(node, nameof(node), v => v.NotNull());
HashBase(context, node);
context.Hash(node.IsLifted);
context.Hash(node.IsLiftedToNull);
context.Hash(node.Method);
context.HashExpr(node.Operand);
}
private void HashDocument(Context context, SymbolDocumentInfo value)
{
context.Hash(value.DocumentType);
context.Hash(value.FileName);
context.Hash(value.Language);
context.Hash(value.LanguageVendor);
}
private void HashLabelTarget(Context context, LabelTarget value)
{
context.HashReferenceId(value);
context.Hash(value.Name, Flags.HasFlag(ExprEqualityComparerFlags.IgnoreLabelName));
context.Hash(value.Type);
}
private void HashElementInit(Context context, ElementInit value)
{
context.Hash(value.AddMethod);
context.HashExpr(value.Arguments);
}
private void HashMemberBinding(Context context, MemberBinding value)
{
context.Hash(value.GetType());
context.Hash(value.BindingType);
context.Hash(value.Member);
switch (value)
{
case MemberAssignment mb:
context.HashExpr(mb.Expression);
break;
case MemberListBinding mb:
context.HashMany(mb.Initializers, HashElementInit);
break;
case MemberMemberBinding mb:
context.HashMany(mb.Bindings, HashMemberBinding);
break;
default:
throw new NotImplementedException();
}