-
Notifications
You must be signed in to change notification settings - Fork 864
Expand file tree
/
Copy pathAugmentWithHashCompare.fs
More file actions
1699 lines (1373 loc) · 62.5 KB
/
Copy pathAugmentWithHashCompare.fs
File metadata and controls
1699 lines (1373 loc) · 62.5 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
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
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
/// Generate the hash/compare functions we add to user-defined types by default.
module internal FSharp.Compiler.AugmentTypeDefinitions
open Internal.Utilities.Library
open FSharp.Compiler.AbstractIL.IL
open FSharp.Compiler.DiagnosticsLogger
open FSharp.Compiler.Syntax
open FSharp.Compiler.Xml
open FSharp.Compiler.TcGlobals
open FSharp.Compiler.TypedTree
open FSharp.Compiler.TypedTreeBasics
open FSharp.Compiler.TypedTreeOps
open FSharp.Compiler.TypeHierarchy
let mkIComparableCompareToSlotSig (g: TcGlobals) =
TSlotSig("CompareTo", g.mk_IComparable_ty, [], [], [ [ TSlotParam(Some("obj"), g.obj_ty_withNulls, false, false, false, []) ] ], Some g.int_ty)
let mkGenericIComparableCompareToSlotSig (g: TcGlobals) ty =
TSlotSig(
"CompareTo",
(mkWoNullAppTy g.system_GenericIComparable_tcref [ ty ]),
[],
[],
[ [ TSlotParam(Some("obj"), ty, false, false, false, []) ] ],
Some g.int_ty
)
let mkIStructuralComparableCompareToSlotSig (g: TcGlobals) =
TSlotSig(
"CompareTo",
g.mk_IStructuralComparable_ty,
[],
[],
[
[
TSlotParam(None, (mkRefTupledTy g [ g.obj_ty_withNulls; g.IComparer_ty ]), false, false, false, [])
]
],
Some g.int_ty
)
let mkGenericIEquatableEqualsSlotSig (g: TcGlobals) ty =
TSlotSig(
"Equals",
(mkWoNullAppTy g.system_GenericIEquatable_tcref [ ty ]),
[],
[],
[ [ TSlotParam(Some("obj"), ty, false, false, false, []) ] ],
Some g.bool_ty
)
let mkIStructuralEquatableEqualsSlotSig (g: TcGlobals) =
TSlotSig(
"Equals",
g.mk_IStructuralEquatable_ty,
[],
[],
[
[
TSlotParam(None, (mkRefTupledTy g [ g.obj_ty_withNulls; g.IEqualityComparer_ty ]), false, false, false, [])
]
],
Some g.bool_ty
)
let mkIStructuralEquatableGetHashCodeSlotSig (g: TcGlobals) =
TSlotSig(
"GetHashCode",
g.mk_IStructuralEquatable_ty,
[],
[],
[ [ TSlotParam(None, g.IEqualityComparer_ty, false, false, false, []) ] ],
Some g.int_ty
)
let mkGetHashCodeSlotSig (g: TcGlobals) =
TSlotSig("GetHashCode", g.obj_ty_noNulls, [], [], [ [] ], Some g.int_ty)
let mkEqualsSlotSig (g: TcGlobals) =
TSlotSig("Equals", g.obj_ty_noNulls, [], [], [ [ TSlotParam(Some("obj"), g.obj_ty_withNulls, false, false, false, []) ] ], Some g.bool_ty)
//-------------------------------------------------------------------------
// Helpers associated with code-generation of comparison/hash augmentations
//-------------------------------------------------------------------------
let mkThisTy g ty =
if isStructTy g ty then mkByrefTy g ty else ty
let mkCompareObjTy g ty =
mkFunTy g (mkThisTy g ty) (mkFunTy g g.obj_ty_withNulls g.int_ty)
let mkCompareTy g ty =
mkFunTy g (mkThisTy g ty) (mkFunTy g ty g.int_ty)
let mkCompareWithComparerTy g ty =
mkFunTy g (mkThisTy g ty) (mkFunTy g (mkRefTupledTy g [ g.obj_ty_withNulls; g.IComparer_ty ]) g.int_ty)
let mkEqualsObjTy g ty =
mkFunTy g (mkThisTy g ty) (mkFunTy g g.obj_ty_withNulls g.bool_ty)
let mkEqualsTy g ty =
mkFunTy g (mkThisTy g ty) (mkFunTy g ty g.bool_ty)
let mkEqualsWithComparerTy g ty =
mkFunTy g (mkThisTy g ty) (mkFunTy g (mkRefTupledTy g [ g.obj_ty_withNulls; g.IEqualityComparer_ty ]) g.bool_ty)
let mkEqualsWithComparerTyExact g ty =
mkFunTy g (mkThisTy g ty) (mkFunTy g (mkRefTupledTy g [ ty; g.IEqualityComparer_ty ]) g.bool_ty)
let mkHashTy g ty =
mkFunTy g (mkThisTy g ty) (mkFunTy g g.unit_ty g.int_ty)
let mkHashWithComparerTy g ty =
mkFunTy g (mkThisTy g ty) (mkFunTy g g.IEqualityComparer_ty g.int_ty)
let mkIsCaseTy g ty =
mkFunTy g (mkThisTy g ty) (mkFunTy g g.unit_ty g.bool_ty)
//-------------------------------------------------------------------------
// Polymorphic comparison
//-------------------------------------------------------------------------
let mkRelBinOp (g: TcGlobals) op m e1 e2 =
mkAsmExpr ([ op ], [], [ e1; e2 ], [ g.bool_ty ], m)
let mkClt g m e1 e2 = mkRelBinOp g AI_clt m e1 e2
let mkCgt g m e1 e2 = mkRelBinOp g AI_cgt m e1 e2
//-------------------------------------------------------------------------
// REVIEW: make this a .constrained call, not a virtual call.
//-------------------------------------------------------------------------
// for creating and using GenericComparer objects and for creating and using
// IStructuralComparable objects (Eg, Calling CompareTo(obj o, IComparer comp))
let mkILLangPrimTy (g: TcGlobals) =
mkILNonGenericBoxedTy g.tcref_LanguagePrimitives.CompiledRepresentationForNamedType
let mkILCallGetComparer (g: TcGlobals) m =
let ty =
mkILNonGenericBoxedTy g.tcref_System_Collections_IComparer.CompiledRepresentationForNamedType
let mspec =
mkILNonGenericStaticMethSpecInTy (mkILLangPrimTy g, "get_GenericComparer", [], ty)
mkAsmExpr ([ mkNormalCall mspec ], [], [], [ g.IComparer_ty ], m)
let mkILCallGetEqualityComparer (g: TcGlobals) m =
let ty =
mkILNonGenericBoxedTy g.tcref_System_Collections_IEqualityComparer.CompiledRepresentationForNamedType
let mspec =
mkILNonGenericStaticMethSpecInTy (mkILLangPrimTy g, "get_GenericEqualityComparer", [], ty)
mkAsmExpr ([ mkNormalCall mspec ], [], [], [ g.IEqualityComparer_ty ], m)
let mkThisVar g m ty = mkCompGenLocal m "this" (mkThisTy g ty)
let mkShl g m acce n =
mkAsmExpr ([ AI_shl ], [], [ acce; mkInt g m n ], [ g.int_ty ], m)
let mkShr g m acce n =
mkAsmExpr ([ AI_shr ], [], [ acce; mkInt g m n ], [ g.int_ty ], m)
let mkAdd (g: TcGlobals) m e1 e2 =
mkAsmExpr ([ AI_add ], [], [ e1; e2 ], [ g.int_ty ], m)
let mkAddToHashAcc g m e accv acce =
mkValSet m accv (mkAdd g m (mkInt g m 0x9e3779b9) (mkAdd g m e (mkAdd g m (mkShl g m acce 6) (mkShr g m acce 2))))
let mkCombineHashGenerators g m exprs accv acce =
(acce, exprs)
||> List.fold (fun tm e -> mkCompGenSequential m (mkAddToHashAcc g m e accv acce) tm)
//-------------------------------------------------------------------------
// Build comparison functions for union, record and exception types.
//-------------------------------------------------------------------------
let mkThatAddrLocal g m ty = mkCompGenLocal m "obj" (mkThisTy g ty)
let mkThatAddrLocalIfNeeded g m tcve ty =
if isStructTy g ty then
let thataddrv, thataddre = mkCompGenLocal m "obj" (mkThisTy g ty)
Some thataddrv, thataddre
else
None, tcve
let mkThisVarThatVar g m ty =
let thisv, thise = mkThisVar g m ty
let thataddrv, thataddre = mkThatAddrLocal g m ty
thisv, thataddrv, thise, thataddre
let mkThatVarBind g m ty thataddrv expr =
if isStructTy g ty then
let thatv2, _ = mkMutableCompGenLocal m "obj" ty
thatv2, mkCompGenLet m thataddrv (mkValAddr m false (mkLocalValRef thatv2)) expr
else
thataddrv, expr
let mkBindThatAddr g m ty thataddrv thatv thate expr =
if isStructTy g ty then
// let thataddrv = &thatv
mkCompGenLet m thataddrv (mkValAddr m false (mkLocalValRef thatv)) expr
else
// let thataddrv = that
mkCompGenLet m thataddrv thate expr
let mkBindThatAddrIfNeeded m thataddrvOpt thatv expr =
match thataddrvOpt with
| None -> expr
| Some thataddrv ->
// let thataddrv = &thatv
mkCompGenLet m thataddrv (mkValAddr m false (mkLocalValRef thatv)) expr
let mkCompareTestConjuncts g m exprs =
match List.tryFrontAndBack exprs with
| None -> mkZero g m
| Some(a, b) ->
(a, b)
||> List.foldBack (fun e acc ->
let nv, ne = mkCompGenLocal m "n" g.int_ty
mkCompGenLet
m
nv
e
(mkCond
DebugPointAtBinding.NoneAtSticky
m
g.int_ty
(mkClt g m ne (mkZero g m))
ne
(mkCond DebugPointAtBinding.NoneAtSticky m g.int_ty (mkCgt g m ne (mkZero g m)) ne acc)))
let mkEqualsTestConjuncts g m exprs =
match List.tryFrontAndBack exprs with
| None -> mkOne g m
| Some(a, b) -> List.foldBack (fun e acc -> mkCond DebugPointAtBinding.NoneAtSticky m g.bool_ty e acc (mkFalse g m)) a b
let mkMinimalTy (g: TcGlobals) (tcref: TyconRef) =
if tcref.Deref.IsFSharpException then
[], g.exn_ty
else
generalizeTyconRef g tcref
// check for nulls
let mkBindNullComparison g m thise thate expr =
let expr = mkNonNullCond g m g.int_ty thate expr (mkOne g m)
let expr =
mkNonNullCond g m g.int_ty thise expr (mkNonNullCond g m g.int_ty thate (mkMinusOne g m) (mkZero g m))
expr
let mkBindThisNullEquals g m thise thate expr =
let expr =
mkNonNullCond g m g.bool_ty thise expr (mkNonNullCond g m g.int_ty thate (mkFalse g m) (mkTrue g m))
expr
let mkBindThatNullEquals g m thise thate expr =
let expr = mkNonNullCond g m g.bool_ty thate expr (mkFalse g m)
let expr = mkBindThisNullEquals g m thise thate expr
expr
let mkBindNullHash g m thise expr =
let expr = mkNonNullCond g m g.int_ty thise expr (mkZero g m)
expr
/// Build the comparison implementation for a record type
let mkRecdCompare g tcref (tycon: Tycon) =
let m = tycon.Range
let fields = tycon.AllInstanceFieldsAsList
let tinst, ty = mkMinimalTy g tcref
let thisv, thataddrv, thise, thataddre = mkThisVarThatVar g m ty
let compe = mkILCallGetComparer g m
let mkTest (fspec: RecdField) =
let fref = tcref.MakeNestedRecdFieldRef fspec
let m = fref.Range
mkCallGenericComparisonWithComparerOuter
g
m
fspec.FormalType
compe
(mkRecdFieldGetViaExprAddr (thise, fref, tinst, m))
(mkRecdFieldGetViaExprAddr (thataddre, fref, tinst, m))
let expr = mkCompareTestConjuncts g m (List.map mkTest fields)
let expr =
if tycon.IsStructOrEnumTycon then
expr
else
mkBindNullComparison g m thise thataddre expr
let thatv, expr = mkThatVarBind g m ty thataddrv expr
thisv, thatv, expr
/// Build the comparison implementation for a record type when parameterized by a comparer
let mkRecdCompareWithComparer g tcref (tycon: Tycon) (_thisv, thise) (_, thate) compe =
let m = tycon.Range
let fields = tycon.AllInstanceFieldsAsList
let tinst, ty = mkMinimalTy g tcref
let tcv, tce = mkCompGenLocal m "objTemp" ty // let tcv = thate
let thataddrv, thataddre = mkThatAddrLocal g m ty // let thataddrv = &tcv, if a struct
let mkTest (fspec: RecdField) =
let fref = tcref.MakeNestedRecdFieldRef fspec
let m = fref.Range
mkCallGenericComparisonWithComparerOuter
g
m
fspec.FormalType
compe
(mkRecdFieldGetViaExprAddr (thise, fref, tinst, m))
(mkRecdFieldGetViaExprAddr (thataddre, fref, tinst, m))
let expr = mkCompareTestConjuncts g m (List.map mkTest fields)
let expr =
if tycon.IsStructOrEnumTycon then
expr
else
mkBindNullComparison g m thise thate expr
let expr = mkBindThatAddr g m ty thataddrv tcv tce expr
// will be optimized away if not necessary
let expr = mkCompGenLet m tcv thate expr
expr
/// Build the .Equals(that) equality implementation wrapper for a record type
let mkRecdEquality g tcref (tycon: Tycon) =
let m = tycon.Range
let fields = tycon.AllInstanceFieldsAsList
let tinst, ty = mkMinimalTy g tcref
let thisv, thataddrv, thise, thataddre = mkThisVarThatVar g m ty
let mkTest (fspec: RecdField) =
let fref = tcref.MakeNestedRecdFieldRef fspec
let m = fref.Range
mkCallGenericEqualityEROuter
g
m
fspec.FormalType
(mkRecdFieldGetViaExprAddr (thise, fref, tinst, m))
(mkRecdFieldGetViaExprAddr (thataddre, fref, tinst, m))
let expr = mkEqualsTestConjuncts g m (List.map mkTest fields)
let expr =
if tycon.IsStructOrEnumTycon then
expr
else
mkBindThatNullEquals g m thise thataddre expr
let thatv, expr = mkThatVarBind g m ty thataddrv expr
thisv, thatv, expr
/// Build the equality implementation for a record type when parameterized by a comparer
let mkRecdEqualityWithComparer g tcref (tycon: Tycon) thise thatobje (thatv, thate) compe isexact =
let m = tycon.Range
let fields = tycon.AllInstanceFieldsAsList
let tinst, ty = mkMinimalTy g tcref
let thataddrv, thataddre = mkThatAddrLocal g m ty
let mkTest (fspec: RecdField) =
let fref = tcref.MakeNestedRecdFieldRef fspec
let m = fref.Range
mkCallGenericEqualityWithComparerOuter
g
m
fspec.FormalType
compe
(mkRecdFieldGetViaExprAddr (thise, fref, tinst, m))
(mkRecdFieldGetViaExprAddr (thataddre, fref, tinst, m))
let expr = mkEqualsTestConjuncts g m (List.map mkTest fields)
let expr = mkBindThatAddr g m ty thataddrv thatv thate expr
let expr =
if isexact then
expr
else
mkIsInstConditional g m ty thatobje thatv expr (mkFalse g m)
let expr =
if tycon.IsStructOrEnumTycon then
expr
else
if isexact then
mkBindThatNullEquals g m thise thate expr
else
mkBindThisNullEquals g m thise thatobje expr
expr
/// Build the equality implementation for an exception definition
let mkExnEquality (g: TcGlobals) exnref (exnc: Tycon) =
let m = exnc.Range
let thatv, thate = mkCompGenLocal m "obj" g.exn_ty
let thisv, thise = mkThisVar g m g.exn_ty
let mkTest i (rfield: RecdField) =
mkCallGenericEqualityEROuter
g
m
rfield.FormalType
(mkExnCaseFieldGet (thise, exnref, i, m))
(mkExnCaseFieldGet (thate, exnref, i, m))
let expr = mkEqualsTestConjuncts g m (List.mapi mkTest exnc.AllInstanceFieldsAsList)
let expr =
let mbuilder = MatchBuilder(DebugPointAtBinding.NoneAtInvisible, m)
let cases =
[
mkCase (DecisionTreeTest.IsInst(g.exn_ty, mkWoNullAppTy exnref []), mbuilder.AddResultTarget(expr))
]
let dflt = Some(mbuilder.AddResultTarget(mkFalse g m))
let dtree = TDSwitch(thate, cases, dflt, m)
mbuilder.Close(dtree, m, g.bool_ty)
let expr = mkBindThatNullEquals g m thise thate expr
thisv, thatv, expr
/// Build the equality implementation for an exception definition when parameterized by a comparer
let mkExnEqualityWithComparer g exnref (exnc: Tycon) thise thatobje (thatv, thate) compe isexact =
let m = exnc.Range
let thataddrv, thataddre = mkThatAddrLocal g m g.exn_ty
let mkTest i (rfield: RecdField) =
mkCallGenericEqualityWithComparerOuter
g
m
rfield.FormalType
compe
(mkExnCaseFieldGet (thise, exnref, i, m))
(mkExnCaseFieldGet (thataddre, exnref, i, m))
let expr = mkEqualsTestConjuncts g m (List.mapi mkTest exnc.AllInstanceFieldsAsList)
let expr =
let mbuilder = MatchBuilder(DebugPointAtBinding.NoneAtInvisible, m)
let cases =
[
mkCase (DecisionTreeTest.IsInst(g.exn_ty, mkWoNullAppTy exnref []), mbuilder.AddResultTarget(expr))
]
let dflt = mbuilder.AddResultTarget(mkFalse g m)
let dtree = TDSwitch(thate, cases, Some dflt, m)
mbuilder.Close(dtree, m, g.bool_ty)
let expr = mkBindThatAddr g m g.exn_ty thataddrv thatv thate expr
let expr =
if isexact then
expr
else
mkIsInstConditional g m g.exn_ty thatobje thatv expr (mkFalse g m)
let expr =
if exnc.IsStructOrEnumTycon then
expr
else
if isexact then
mkBindThatNullEquals g m thise thate expr
else
mkBindThisNullEquals g m thise thatobje expr
expr
/// Build the comparison implementation for a union type
let mkUnionCompare g tcref (tycon: Tycon) =
let m = tycon.Range
let ucases = tycon.UnionCasesAsList
let tinst, ty = mkMinimalTy g tcref
let thisv, thataddrv, thise, thataddre = mkThisVarThatVar g m ty
let thistagv, thistage = mkCompGenLocal m "thisTag" g.int_ty
let thattagv, thattage = mkCompGenLocal m "thatTag" g.int_ty
let compe = mkILCallGetComparer g m
let expr =
let mbuilder = MatchBuilder(DebugPointAtBinding.NoneAtInvisible, m)
let mkCase ucase =
let cref = tcref.MakeNestedUnionCaseRef ucase
let m = cref.Range
let rfields = ucase.RecdFields
if isNil rfields then
None
else
let mkTest thise thataddre j (fld: RecdField) =
mkCallGenericComparisonWithComparerOuter
g
m
fld.FormalType
compe
(mkUnionCaseFieldGetProvenViaExprAddr (thise, cref, tinst, j, m))
(mkUnionCaseFieldGetProvenViaExprAddr (thataddre, cref, tinst, j, m))
let test =
if cref.Tycon.IsStructOrEnumTycon then
mkCompareTestConjuncts g m (List.mapi (mkTest thise thataddre) rfields)
else
let thisucv, thisucve = mkCompGenLocal m "thisCast" (mkProvenUnionCaseTy cref tinst)
let thatucv, thatucve = mkCompGenLocal m "objCast" (mkProvenUnionCaseTy cref tinst)
mkCompGenLet
m
thisucv
(mkUnionCaseProof (thise, cref, tinst, m))
(mkCompGenLet
m
thatucv
(mkUnionCaseProof (thataddre, cref, tinst, m))
(mkCompareTestConjuncts g m (List.mapi (mkTest thisucve thatucve) rfields)))
Some(mkCase (DecisionTreeTest.UnionCase(cref, tinst), mbuilder.AddResultTarget(test)))
let nullary, nonNullary = List.partition Option.isNone (List.map mkCase ucases)
if isNil nonNullary then
mkZero g m
else
let cases =
nonNullary
|> List.map (function
| Some c -> c
| None -> failwith "mkUnionCompare")
let dflt =
if isNil nullary then
None
else
Some(mbuilder.AddResultTarget(mkZero g m))
let dtree = TDSwitch(thise, cases, dflt, m)
mbuilder.Close(dtree, m, g.int_ty)
let expr =
if List.isSingleton ucases then
expr
else
let tagsEqTested =
mkCond
DebugPointAtBinding.NoneAtSticky
m
g.int_ty
(mkILAsmCeq g m thistage thattage)
expr
(mkAsmExpr ([ AI_sub ], [], [ thistage; thattage ], [ g.int_ty ], m)) in
mkCompGenLet
m
thistagv
(mkUnionCaseTagGetViaExprAddr (thise, tcref, tinst, m))
(mkCompGenLet m thattagv (mkUnionCaseTagGetViaExprAddr (thataddre, tcref, tinst, m)) tagsEqTested)
let expr =
if tycon.IsStructOrEnumTycon then
expr
else
mkBindNullComparison g m thise thataddre expr
let thatv, expr = mkThatVarBind g m ty thataddrv expr
thisv, thatv, expr
/// Build the comparison implementation for a union type when parameterized by a comparer
let mkUnionCompareWithComparer g tcref (tycon: Tycon) (_thisv, thise) (_thatobjv, thatcaste) compe =
let m = tycon.Range
let ucases = tycon.UnionCasesAsList
let tinst, ty = mkMinimalTy g tcref
let tcv, tce = mkCompGenLocal m "objTemp" ty // let tcv = (thatobj :?> ty)
let thataddrvOpt, thataddre = mkThatAddrLocalIfNeeded g m tce ty // let thataddrv = &tcv if struct, otherwise thataddre is just tce
let thistagv, thistage = mkCompGenLocal m "thisTag" g.int_ty
let thattagv, thattage = mkCompGenLocal m "thatTag" g.int_ty
let expr =
let mbuilder = MatchBuilder(DebugPointAtBinding.NoneAtInvisible, m)
let mkCase ucase =
let cref = tcref.MakeNestedUnionCaseRef ucase
let m = cref.Range
let rfields = ucase.RecdFields
if isNil rfields then
None
else
let mkTest thise thataddre j (fld: RecdField) =
mkCallGenericComparisonWithComparerOuter
g
m
fld.FormalType
compe
(mkUnionCaseFieldGetProvenViaExprAddr (thise, cref, tinst, j, m))
(mkUnionCaseFieldGetProvenViaExprAddr (thataddre, cref, tinst, j, m))
let test =
if cref.Tycon.IsStructOrEnumTycon then
mkCompareTestConjuncts g m (List.mapi (mkTest thise thataddre) rfields)
else
let thisucv, thisucve =
mkCompGenLocal m "thisCastu" (mkProvenUnionCaseTy cref tinst)
let thatucv, thatucve =
mkCompGenLocal m "thatCastu" (mkProvenUnionCaseTy cref tinst)
mkCompGenLet
m
thisucv
(mkUnionCaseProof (thise, cref, tinst, m))
(mkCompGenLet
m
thatucv
(mkUnionCaseProof (thataddre, cref, tinst, m))
(mkCompareTestConjuncts g m (List.mapi (mkTest thisucve thatucve) rfields)))
Some(mkCase (DecisionTreeTest.UnionCase(cref, tinst), mbuilder.AddResultTarget(test)))
let nullary, nonNullary = List.partition Option.isNone (List.map mkCase ucases)
if isNil nonNullary then
mkZero g m
else
let cases =
nonNullary
|> List.map (function
| Some c -> c
| None -> failwith "mkUnionCompare")
let dflt =
if isNil nullary then
None
else
Some(mbuilder.AddResultTarget(mkZero g m))
let dtree = TDSwitch(thise, cases, dflt, m)
mbuilder.Close(dtree, m, g.int_ty)
let expr =
if List.isSingleton ucases then
expr
else
let tagsEqTested =
mkCond
DebugPointAtBinding.NoneAtSticky
m
g.int_ty
(mkILAsmCeq g m thistage thattage)
expr
(mkAsmExpr ([ AI_sub ], [], [ thistage; thattage ], [ g.int_ty ], m))
mkCompGenLet
m
thistagv
(mkUnionCaseTagGetViaExprAddr (thise, tcref, tinst, m))
(mkCompGenLet m thattagv (mkUnionCaseTagGetViaExprAddr (thataddre, tcref, tinst, m)) tagsEqTested)
let expr =
if tycon.IsStructOrEnumTycon then
expr
else
mkBindNullComparison g m thise thatcaste expr
let expr = mkBindThatAddrIfNeeded m thataddrvOpt tcv expr
let expr = mkCompGenLet m tcv thatcaste expr
expr
/// Build the equality implementation for a union type
let mkUnionEquality g tcref (tycon: Tycon) =
let m = tycon.Range
let ucases = tycon.UnionCasesAsList
let tinst, ty = mkMinimalTy g tcref
let thisv, thataddrv, thise, thataddre = mkThisVarThatVar g m ty
let thistagv, thistage = mkCompGenLocal m "thisTag" g.int_ty
let thattagv, thattage = mkCompGenLocal m "thatTag" g.int_ty
let expr =
let mbuilder = MatchBuilder(DebugPointAtBinding.NoneAtInvisible, m)
let mkCase ucase =
let cref = tcref.MakeNestedUnionCaseRef ucase
let m = cref.Range
let rfields = ucase.RecdFields
if isNil rfields then
None
else
let mkTest thise thataddre j (fld: RecdField) =
mkCallGenericEqualityEROuter
g
m
fld.FormalType
(mkUnionCaseFieldGetProvenViaExprAddr (thise, cref, tinst, j, m))
(mkUnionCaseFieldGetProvenViaExprAddr (thataddre, cref, tinst, j, m))
let test =
if cref.Tycon.IsStructOrEnumTycon then
mkEqualsTestConjuncts g m (List.mapi (mkTest thise thataddre) rfields)
else
let thisucv, thisucve = mkCompGenLocal m "thisCast" (mkProvenUnionCaseTy cref tinst)
let thatucv, thatucve = mkCompGenLocal m "objCast" (mkProvenUnionCaseTy cref tinst)
mkCompGenLet
m
thisucv
(mkUnionCaseProof (thise, cref, tinst, m))
(mkCompGenLet
m
thatucv
(mkUnionCaseProof (thataddre, cref, tinst, m))
(mkEqualsTestConjuncts g m (List.mapi (mkTest thisucve thatucve) rfields)))
Some(mkCase (DecisionTreeTest.UnionCase(cref, tinst), mbuilder.AddResultTarget(test)))
let nullary, nonNullary = List.partition Option.isNone (List.map mkCase ucases)
if isNil nonNullary then
mkTrue g m
else
let cases =
List.map
(function
| Some c -> c
| None -> failwith "mkUnionEquality")
nonNullary
let dflt =
(if isNil nullary then
None
else
Some(mbuilder.AddResultTarget(mkTrue g m)))
let dtree = TDSwitch(thise, cases, dflt, m)
mbuilder.Close(dtree, m, g.bool_ty)
let expr =
if List.isSingleton ucases then
expr
else
let tagsEqTested =
mkCond DebugPointAtBinding.NoneAtSticky m g.bool_ty (mkILAsmCeq g m thistage thattage) expr (mkFalse g m)
mkCompGenLet
m
thistagv
(mkUnionCaseTagGetViaExprAddr (thise, tcref, tinst, m))
(mkCompGenLet m thattagv (mkUnionCaseTagGetViaExprAddr (thataddre, tcref, tinst, m)) tagsEqTested)
let thatv, expr = mkThatVarBind g m ty thataddrv expr
let expr =
if tycon.IsStructOrEnumTycon then
expr
else
mkBindThatNullEquals g m thise thataddre expr
thisv, thatv, expr
/// Build the equality implementation for a union type when parameterized by a comparer
let mkUnionEqualityWithComparer g tcref (tycon: Tycon) thise thatobje (thatv, thate) compe isexact =
let m = tycon.Range
let ucases = tycon.UnionCasesAsList
let tinst, ty = mkMinimalTy g tcref
let thistagv, thistage = mkCompGenLocal m "thisTag" g.int_ty
let thattagv, thattage = mkCompGenLocal m "thatTag" g.int_ty
let thataddrv, thataddre = mkThatAddrLocal g m ty
let expr =
let mbuilder = MatchBuilder(DebugPointAtBinding.NoneAtInvisible, m)
let mkCase ucase =
let cref = tcref.MakeNestedUnionCaseRef ucase
let m = cref.Range
let rfields = ucase.RecdFields
if isNil rfields then
None
else
let mkTest thise thataddre j (fld: RecdField) =
mkCallGenericEqualityWithComparerOuter
g
m
fld.FormalType
compe
(mkUnionCaseFieldGetProvenViaExprAddr (thise, cref, tinst, j, m))
(mkUnionCaseFieldGetProvenViaExprAddr (thataddre, cref, tinst, j, m))
let test =
if cref.Tycon.IsStructOrEnumTycon then
mkEqualsTestConjuncts g m (List.mapi (mkTest thise thataddre) rfields)
else
let thisucv, thisucve =
mkCompGenLocal m "thisCastu" (mkProvenUnionCaseTy cref tinst)
let thatucv, thatucve =
mkCompGenLocal m "thatCastu" (mkProvenUnionCaseTy cref tinst)
mkCompGenLet
m
thisucv
(mkUnionCaseProof (thise, cref, tinst, m))
(mkCompGenLet
m
thatucv
(mkUnionCaseProof (thataddre, cref, tinst, m))
(mkEqualsTestConjuncts g m (List.mapi (mkTest thisucve thatucve) rfields)))
Some(mkCase (DecisionTreeTest.UnionCase(cref, tinst), mbuilder.AddResultTarget(test)))
let nullary, nonNullary = List.partition Option.isNone (List.map mkCase ucases)
if isNil nonNullary then
mkTrue g m
else
let cases =
List.map
(function
| Some c -> c
| None -> failwith "mkUnionEquality")
nonNullary
let dflt =
if isNil nullary then
None
else
Some(mbuilder.AddResultTarget(mkTrue g m))
let dtree = TDSwitch(thise, cases, dflt, m)
mbuilder.Close(dtree, m, g.bool_ty)
let expr =
if List.isSingleton ucases then
expr
else
let tagsEqTested =
mkCond DebugPointAtBinding.NoneAtSticky m g.bool_ty (mkILAsmCeq g m thistage thattage) expr (mkFalse g m)
mkCompGenLet
m
thistagv
(mkUnionCaseTagGetViaExprAddr (thise, tcref, tinst, m))
(mkCompGenLet m thattagv (mkUnionCaseTagGetViaExprAddr (thataddre, tcref, tinst, m)) tagsEqTested)
let expr = mkBindThatAddr g m ty thataddrv thatv thate expr
let expr =
if isexact then
expr
else
mkIsInstConditional g m ty thatobje thatv expr (mkFalse g m)
let expr =
if tycon.IsStructOrEnumTycon then
expr
else
if isexact then
mkBindThatNullEquals g m thise thate expr
else
mkBindThisNullEquals g m thise thatobje expr
expr
//-------------------------------------------------------------------------
// Build hashing functions for union, record and exception types.
// Hashing functions must respect the "=" and comparison operators.
//-------------------------------------------------------------------------
/// Structural hash implementation for record types when parameterized by a comparer
let mkRecdHashWithComparer g tcref (tycon: Tycon) compe =
let m = tycon.Range
let fields = tycon.AllInstanceFieldsAsList
let tinst, ty = mkMinimalTy g tcref
let thisv, thise = mkThisVar g m ty
let mkFieldHash (fspec: RecdField) =
let fref = tcref.MakeNestedRecdFieldRef fspec
let m = fref.Range
let e = mkRecdFieldGetViaExprAddr (thise, fref, tinst, m)
mkCallGenericHashWithComparerOuter g m fspec.FormalType compe e
let accv, acce = mkMutableCompGenLocal m "i" g.int_ty
let stmt =
mkCombineHashGenerators g m (List.map mkFieldHash fields) (mkLocalValRef accv) acce
let expr = mkCompGenLet m accv (mkZero g m) stmt
let expr =
if tycon.IsStructOrEnumTycon then
expr
else
mkBindNullHash g m thise expr
thisv, expr
/// Structural hash implementation for exception types when parameterized by a comparer
let mkExnHashWithComparer g exnref (exnc: Tycon) compe =
let m = exnc.Range
let thisv, thise = mkThisVar g m g.exn_ty
let mkHash i (rfield: RecdField) =
let e = mkExnCaseFieldGet (thise, exnref, i, m)
mkCallGenericHashWithComparerOuter g m rfield.FormalType compe e
let accv, acce = mkMutableCompGenLocal m "i" g.int_ty
let stmt =
mkCombineHashGenerators g m (List.mapi mkHash exnc.AllInstanceFieldsAsList) (mkLocalValRef accv) acce
let expr = mkCompGenLet m accv (mkZero g m) stmt
let expr = mkBindNullHash g m thise expr
thisv, expr
/// Structural hash implementation for union types when parameterized by a comparer
let mkUnionHashWithComparer g tcref (tycon: Tycon) compe =
let m = tycon.Range
let ucases = tycon.UnionCasesAsList
let tinst, ty = mkMinimalTy g tcref
let thisv, thise = mkThisVar g m ty
let mbuilder = MatchBuilder(DebugPointAtBinding.NoneAtInvisible, m)
let accv, acce = mkMutableCompGenLocal m "i" g.int_ty
let mkCase i ucase1 =
let c1ref = tcref.MakeNestedUnionCaseRef ucase1
let m = c1ref.Range
if ucase1.IsNullary then
None
else
let mkHash thise j (rfield: RecdField) =
let e = mkUnionCaseFieldGetProvenViaExprAddr (thise, c1ref, tinst, j, m)
mkCallGenericHashWithComparerOuter g m rfield.FormalType compe e
let test =
if tycon.IsStructOrEnumTycon then
mkCompGenSequential
m
(mkValSet m (mkLocalValRef accv) (mkInt g m i))
(mkCombineHashGenerators g m (List.mapi (mkHash thise) ucase1.RecdFields) (mkLocalValRef accv) acce)
else
let ucv, ucve = mkCompGenLocal m "unionCase" (mkProvenUnionCaseTy c1ref tinst)
mkCompGenLet
m
ucv
(mkUnionCaseProof (thise, c1ref, tinst, m))
(mkCompGenSequential
m
(mkValSet m (mkLocalValRef accv) (mkInt g m i))
(mkCombineHashGenerators g m (List.mapi (mkHash ucve) ucase1.RecdFields) (mkLocalValRef accv) acce))
Some(mkCase (DecisionTreeTest.UnionCase(c1ref, tinst), mbuilder.AddResultTarget(test)))
let nullary, nonNullary =
ucases |> List.mapi mkCase |> List.partition (fun i -> i.IsNone)
let cases =
nonNullary
|> List.map (function
| Some c -> c
| None -> failwith "mkUnionHash")
let dflt =
if isNil nullary then
None
else
let tag = mkUnionCaseTagGetViaExprAddr (thise, tcref, tinst, m)
Some(mbuilder.AddResultTarget(tag))
let dtree = TDSwitch(thise, cases, dflt, m)
let stmt = mbuilder.Close(dtree, m, g.int_ty)
let expr = mkCompGenLet m accv (mkZero g m) stmt
let expr =
if tycon.IsStructOrEnumTycon then
expr