forked from dotnet/fsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaugment.fs
More file actions
1050 lines (879 loc) · 48.6 KB
/
Copy pathaugment.fs
File metadata and controls
1050 lines (879 loc) · 48.6 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 Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. 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 Microsoft.FSharp.Compiler.Augment
open Internal.Utilities
open Microsoft.FSharp.Compiler
open Microsoft.FSharp.Compiler.AbstractIL
open Microsoft.FSharp.Compiler.AbstractIL.IL
open Microsoft.FSharp.Compiler.AbstractIL.Internal
open Microsoft.FSharp.Compiler.AbstractIL.Internal.Library
open Microsoft.FSharp.Compiler.Tast
open Microsoft.FSharp.Compiler.Tastops
open Microsoft.FSharp.Compiler.Ast
open Microsoft.FSharp.Compiler.ErrorLogger
open Microsoft.FSharp.Compiler.PrettyNaming
open Microsoft.FSharp.Compiler.Lib
open Microsoft.FSharp.Compiler.Env
open Microsoft.FSharp.Compiler.Infos
let mkIComparableCompareToSlotSig g =
TSlotSig("CompareTo",g.mk_IComparable_ty, [],[], [[TSlotParam(Some("obj"),g.obj_ty,false,false,false,[])]],Some g.int_ty)
let mkGenericIComparableCompareToSlotSig g typ =
TSlotSig("CompareTo",(mkAppTy g.system_GenericIComparable_tcref [typ]),[],[], [[TSlotParam(Some("obj"),typ,false,false,false,[])]],Some g.int_ty)
let mkIStructuralComparableCompareToSlotSig g =
TSlotSig("CompareTo",g.mk_IStructuralComparable_ty,[],[],[[TSlotParam(None,(mkTupleTy [g.obj_ty ; g.mk_IComparer_ty]),false,false,false,[])]], Some g.int_ty)
let mkGenericIEquatableEqualsSlotSig g typ =
TSlotSig("Equals",(mkAppTy g.system_GenericIEquatable_tcref [typ]),[],[], [[TSlotParam(Some("obj"),typ,false,false,false,[])]],Some g.bool_ty)
let mkIStructuralEquatableEqualsSlotSig g =
TSlotSig("Equals",g.mk_IStructuralEquatable_ty,[],[],[[TSlotParam(None,(mkTupleTy [g.obj_ty ; g.mk_IEqualityComparer_ty]),false,false,false,[])]], Some g.bool_ty)
let mkIStructuralEquatableGetHashCodeSlotSig g =
TSlotSig("GetHashCode",g.mk_IStructuralEquatable_ty,[],[],[[TSlotParam(None,g.mk_IEqualityComparer_ty,false,false,false,[])]], Some g.int_ty)
let mkGetHashCodeSlotSig g =
TSlotSig("GetHashCode", g.obj_ty, [],[], [[]],Some g.int_ty)
let mkEqualsSlotSig g =
TSlotSig("Equals", g.obj_ty, [],[], [[TSlotParam(Some("obj"),g.obj_ty,false,false,false,[])]],Some g.bool_ty)
let mkILObjectGetTypeMethSpec ilg = IL.mkILNonGenericInstanceMethSpecInTy(ilg.typ_Object,"GetType",[],ilg.typ_Type)
let mkILObjectToStringMethSpec ilg = IL.mkILNonGenericInstanceMethSpecInTy(ilg.typ_Object,"ToString",[],ilg.typ_String)
//-------------------------------------------------------------------------
// 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 = (mkThisTy g ty) --> (g.obj_ty --> g.int_ty)
let mkCompareTy g ty = (mkThisTy g ty) --> (ty --> g.int_ty)
let mkCompareWithComparerTy g ty = (mkThisTy g ty) --> ((mkTupleTy [g.obj_ty ; g.mk_IComparer_ty]) --> g.int_ty)
let mkEqualsObjTy g ty = (mkThisTy g ty) --> (g.obj_ty --> g.bool_ty)
let mkEqualsTy g ty = (mkThisTy g ty) --> (ty --> g.bool_ty)
let mkEqualsWithComparerTy g ty = (mkThisTy g ty) --> ((mkTupleTy [g.obj_ty ; g.mk_IEqualityComparer_ty]) --> g.bool_ty)
let mkHashTy g ty = (mkThisTy g ty) --> (g.unit_ty --> g.int_ty)
let mkHashWithComparerTy g ty = (mkThisTy g ty) --> (g.mk_IEqualityComparer_ty --> g.int_ty)
//-------------------------------------------------------------------------
// Polymorphic comparison
//-------------------------------------------------------------------------
let mkRelBinOp g op m e1 e2 = mkAsmExpr ([ op ],[], [e1; e2],[g.bool_ty],m)
let mkClt g m e1 e2 = mkRelBinOp g IL.AI_clt m e1 e2
let mkCgt g m e1 e2 = mkRelBinOp g IL.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 = mkILNonGenericBoxedTy g.tcref_LanguagePrimitives.CompiledRepresentationForNamedType
let mkILCallGetComparer g m =
let ty = mkILNonGenericBoxedTy g.tcref_System_Collections_IComparer.CompiledRepresentationForNamedType
let mspec = mkILNonGenericStaticMethSpecInTy (mkILLangPrimTy g, "get_GenericComparer",[],ty)
mkAsmExpr([IL.mkNormalCall mspec], [], [], [g.mk_IComparer_ty], m)
let mkILCallGetEqualityComparer g m =
let ty = mkILNonGenericBoxedTy g.tcref_System_Collections_IEqualityComparer.CompiledRepresentationForNamedType
let mspec = mkILNonGenericStaticMethSpecInTy (mkILLangPrimTy g,"get_GenericEqualityComparer",[],ty)
mkAsmExpr([IL.mkNormalCall mspec], [], [], [g.mk_IEqualityComparer_ty], m)
let mkThisVar g m ty = mkCompGenLocal m "this" (mkThisTy g ty)
let mkShl g m acce n = mkAsmExpr([ IL.AI_shl ],[],[acce; mkInt g m n],[g.int_ty],m)
let mkShr g m acce n = mkAsmExpr([ IL.AI_shr ],[],[acce; mkInt g m n],[g.int_ty],m)
let mkAdd g m e1 e2 = mkAsmExpr([ IL.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 mkThisVarThatVar g m ty =
let thisv,thise = mkThisVar g m ty
let thatv,thate = mkCompGenLocal m "obj" (mkThisTy g ty)
thisv,thatv,thise,thate
let mkThatVarBind g m ty thatv expr =
if isStructTy g ty then
let thatv2,_ = mkMutableCompGenLocal m "obj" ty
thatv2,mkCompGenLet m thatv (mkValAddr m (mkLocalValRef thatv2)) expr
else thatv,expr
let mkThatAddrLocal g m ty =
if isStructTy g ty then
mkMutableCompGenLocal m "objCast" (mkByrefTy g ty)
else
mkCompGenLocal m "objCast" ty
let mkBindThatAddr g m ty thataddrv thatv thate expr =
if isStructTy g ty then
mkCompGenLet m thataddrv (mkValAddr m (mkLocalValRef thatv)) expr
else
mkCompGenLet m thataddrv thate expr
let mkCompareTestConjuncts g m exprs =
match exprs with
| [] -> mkZero g m
| [h] -> h
| l ->
let a,b = List.frontAndBack l
(a,b) ||> List.foldBack (fun e acc ->
let nv,ne = mkCompGenLocal m "n" g.int_ty
mkCompGenLet m nv e
(mkCond NoSequencePointAtStickyBinding SuppressSequencePointAtTarget m g.int_ty
(mkClt g m ne (mkZero g m))
ne
(mkCond NoSequencePointAtStickyBinding SuppressSequencePointAtTarget m g.int_ty
(mkCgt g m ne (mkZero g m))
ne
acc)))
let mkEqualsTestConjuncts g m exprs =
match exprs with
| [] -> mkOne g m
| [h] -> h
| l ->
let a,b = List.frontAndBack l
List.foldBack (fun e acc -> mkCond NoSequencePointAtStickyBinding SuppressSequencePointAtTarget m g.bool_ty e acc (mkFalse g m)) a b
let mkMinimalTy g (tcref:TyconRef) =
if tcref.Deref.IsExceptionDecl then [], g.exn_ty
else generalizeTyconRef 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,thatv,thise,thate = mkThisVarThatVar g m ty
let compe = mkILCallGetComparer g m
let mkTest (fspec:RecdField) =
let fty = fspec.FormalType
let fref = mkNestedRecdFieldRef tcref fspec
let m = fref.Range
mkCallGenericComparisonWithComparerOuter g m fty
compe
(mkRecdFieldGetViaExprAddr(thise, fref, tinst, m))
(mkRecdFieldGetViaExprAddr(thate, 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 thatv,expr = mkThatVarBind g m ty thatv 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 fty = fspec.FormalType
let fref = mkNestedRecdFieldRef tcref fspec
let m = fref.Range
mkCallGenericComparisonWithComparerOuter g m fty
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 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,thatv,thise,thate = mkThisVarThatVar g m ty
let mkTest (fspec:RecdField) =
let fty = fspec.FormalType
let fref = mkNestedRecdFieldRef tcref fspec
let m = fref.Range
mkCallGenericEqualityEROuter g m fty
(mkRecdFieldGetViaExprAddr(thise, fref, tinst, m))
(mkRecdFieldGetViaExprAddr(thate, fref, tinst, m))
let expr = mkEqualsTestConjuncts g m (List.map mkTest fields)
let expr = if tycon.IsStructOrEnumTycon then expr else mkBindThatNullEquals g m thise thate expr
let thatv,expr = mkThatVarBind g m ty thatv expr
thisv,thatv,expr
/// Build the equality implementation for a record type when parameterized by a comparer
let mkRecdEqualityWithComparer g tcref (tycon:Tycon) (_thisv,thise) thatobje (thatv,thate) compe =
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 fty = fspec.FormalType
let fref = mkNestedRecdFieldRef tcref fspec
let m = fref.Range
mkCallGenericEqualityWithComparerOuter g m fty
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
// will be optimized away if not necessary
let expr = mkIsInstConditional g m ty thatobje thatv expr (mkFalse g m)
let expr = if tycon.IsStructOrEnumTycon then expr else mkBindThisNullEquals g m thise thatobje expr
expr
/// Build the equality implementation for an exception definition
let mkExnEquality g 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) =
let fty = rfield.FormalType
mkCallGenericEqualityEROuter g m fty
(mkExnCaseFieldGet(thise, exnref, i, m))
(mkExnCaseFieldGet(thate, exnref, i, m))
let expr = mkEqualsTestConjuncts g m (List.mapi mkTest (exnc.AllInstanceFieldsAsList))
let expr =
let mbuilder = new MatchBuilder(NoSequencePointAtInvisibleBinding,m )
let dtree =
TDSwitch(thate,
[ mkCase(Test.IsInst(g.exn_ty,mkAppTy exnref []),
mbuilder.AddResultTarget(expr,SuppressSequencePointAtTarget)) ],
Some(mbuilder.AddResultTarget(mkFalse g m,SuppressSequencePointAtTarget)),
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) (_thisv,thise) thatobje (thatv,thate) compe =
let m = exnc.Range
let thataddrv,thataddre = mkThatAddrLocal g m g.exn_ty
let mkTest i (rfield:RecdField) =
let fty = rfield.FormalType
mkCallGenericEqualityWithComparerOuter g m fty
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 = new MatchBuilder(NoSequencePointAtInvisibleBinding,m )
let dtree =
TDSwitch(thataddre,
[ mkCase(Test.IsInst(g.exn_ty,mkAppTy exnref []),
mbuilder.AddResultTarget(expr,SuppressSequencePointAtTarget)) ],
Some(mbuilder.AddResultTarget(mkFalse g m,SuppressSequencePointAtTarget)),
m)
mbuilder.Close(dtree,m,g.bool_ty)
let expr = mkBindThatAddr g m g.exn_ty thataddrv thatv thate expr
let expr = mkIsInstConditional g m g.exn_ty thatobje thatv expr (mkFalse g m)
let expr = if exnc.IsStructOrEnumTycon then 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,thise = mkCompGenLocal m "this" ty
let thatv,thate = mkCompGenLocal m "obj" 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 = new MatchBuilder(NoSequencePointAtInvisibleBinding,m )
let mkCase ucase =
let cref = mkNestedUnionCaseRef tcref ucase
let m = cref.Range
let thisucv,thisucve = mkCompGenLocal m "thisCast" (mkProvenUnionCaseTy cref tinst)
let thatucv,thatucve = mkCompGenLocal m "objCast" (mkProvenUnionCaseTy cref tinst)
let mkTest j (argty:RecdField) =
mkCallGenericComparisonWithComparerOuter g m argty.FormalType
compe
(mkUnionCaseFieldGetProven(thisucve, cref, tinst, j, m))
(mkUnionCaseFieldGetProven(thatucve, cref, tinst, j, m))
let rfields = ucase.RecdFields
if isNil rfields then None else
Some (mkCase(Test.UnionCase(cref,tinst),
mbuilder.AddResultTarget
(mkCompGenLet m thisucv (mkUnionCaseProof(thise,cref,tinst,m))
(mkCompGenLet m thatucv (mkUnionCaseProof(thate,cref,tinst,m))
(mkCompareTestConjuncts g m (List.mapi mkTest rfields))),
SuppressSequencePointAtTarget)))
let nullary,nonNullary = List.partition isNone (List.map mkCase ucases)
if isNil nonNullary then mkZero g m else
let dtree =
TDSwitch(thise,
(nonNullary |> List.map (function (Some c) -> c | None -> failwith "mkUnionCompare")),
(if isNil nullary then None
else Some (mbuilder.AddResultTarget(mkZero g m,SuppressSequencePointAtTarget))),
m)
mbuilder.Close(dtree,m,g.int_ty)
let expr =
if ucases.Length = 1 then expr else
let tagsEqTested =
mkCond NoSequencePointAtStickyBinding SuppressSequencePointAtTarget m g.int_ty
(mkILAsmCeq g m thistage thattage)
expr
(mkAsmExpr ([ IL.AI_sub ],[], [thistage; thattage],[g.int_ty],m))in
mkCompGenLet m thistagv
(mkUnionCaseTagGet (thise,tcref,tinst,m))
(mkCompGenLet m thattagv
(mkUnionCaseTagGet (thate,tcref,tinst,m))
tagsEqTested)
let expr = mkBindNullComparison g m thise thate 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) (thatv,thate) compe =
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 = new MatchBuilder(NoSequencePointAtInvisibleBinding,m )
let mkCase ucase =
let cref = mkNestedUnionCaseRef tcref ucase
let m = cref.Range
let thisucv,thisucve = mkCompGenLocal m "thisCastu" (mkProvenUnionCaseTy cref tinst)
let thatucv,thatucve = mkCompGenLocal m "thatCastu" (mkProvenUnionCaseTy cref tinst)
let mkTest j (argty:RecdField) =
mkCallGenericComparisonWithComparerOuter g m argty.FormalType
compe
(mkUnionCaseFieldGetProven(thisucve, cref, tinst, j, m))
(mkUnionCaseFieldGetProven(thatucve, cref, tinst, j, m))
let rfields = ucase.RecdFields
if isNil rfields then None else
Some (mkCase(Test.UnionCase(cref,tinst),
mbuilder.AddResultTarget
(mkCompGenLet m thisucv (mkUnionCaseProof(thise,cref,tinst,m))
(mkCompGenLet m thatucv (mkUnionCaseProof(thataddre,cref,tinst,m))
(mkCompareTestConjuncts g m (List.mapi mkTest rfields))),
SuppressSequencePointAtTarget)))
let nullary,nonNullary = List.partition isNone (List.map mkCase ucases)
if isNil nonNullary then mkZero g m else
let dtree =
TDSwitch(thise,
(nonNullary |> List.map (function (Some c) -> c | None -> failwith "mkUnionCompare")),
(if isNil nullary then None
else Some (mbuilder.AddResultTarget(mkZero g m,SuppressSequencePointAtTarget))),
m)
mbuilder.Close(dtree,m,g.int_ty)
let expr =
if ucases.Length = 1 then expr else
let tagsEqTested =
mkCond NoSequencePointAtStickyBinding SuppressSequencePointAtTarget m g.int_ty
(mkILAsmCeq g m thistage thattage)
expr
(mkAsmExpr ([ IL.AI_sub ],[], [thistage; thattage],[g.int_ty],m))in
mkCompGenLet m thistagv
(mkUnionCaseTagGet (thise,tcref,tinst,m))
(mkCompGenLet m thattagv
(mkUnionCaseTagGet (thataddre,tcref,tinst,m))
tagsEqTested)
let expr = mkBindNullComparison g m thise thate expr
let expr = mkBindThatAddr g m ty thataddrv thatv thate 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,thise = mkCompGenLocal m "this" ty
let thatv,thate = mkCompGenLocal m "obj" ty
let thistagv,thistage = mkCompGenLocal m "thisTag" g.int_ty
let thattagv,thattage = mkCompGenLocal m "thatTag" g.int_ty
let expr =
let mbuilder = new MatchBuilder(NoSequencePointAtInvisibleBinding,m )
let mkCase ucase =
let cref = mkNestedUnionCaseRef tcref ucase
let m = cref.Range
let thisucv,thisucve = mkCompGenLocal m "thisCast" (mkProvenUnionCaseTy cref tinst)
let thatucv,thatucve = mkCompGenLocal m "objCast" (mkProvenUnionCaseTy cref tinst)
let mkTest j (argty:RecdField) =
mkCallGenericEqualityEROuter g m argty.FormalType
(mkUnionCaseFieldGetProven(thisucve, cref, tinst, j, m))
(mkUnionCaseFieldGetProven(thatucve, cref, tinst, j, m))
let rfields = ucase.RecdFields
if isNil rfields then None else
Some (mkCase(Test.UnionCase(cref,tinst),
mbuilder.AddResultTarget
(mkCompGenLet m thisucv (mkUnionCaseProof(thise,cref,tinst,m))
(mkCompGenLet m thatucv (mkUnionCaseProof(thate,cref,tinst,m))
(mkEqualsTestConjuncts g m (List.mapi mkTest rfields))),
SuppressSequencePointAtTarget)))
let nullary,nonNullary = List.partition isNone (List.map mkCase ucases)
if isNil nonNullary then mkTrue g m else
let dtree =
TDSwitch(thise,List.map (function (Some c) -> c | None -> failwith "mkUnionEquality") nonNullary,
(if isNil nullary then None else Some (mbuilder.AddResultTarget(mkTrue g m,SuppressSequencePointAtTarget))),
m)
mbuilder.Close(dtree,m,g.bool_ty)
let expr =
if ucases.Length = 1 then expr else
let tagsEqTested =
mkCond NoSequencePointAtStickyBinding SuppressSequencePointAtTarget m g.bool_ty
(mkILAsmCeq g m thistage thattage)
expr
(mkFalse g m)
mkCompGenLet m thistagv
(mkUnionCaseTagGet (thise,tcref,tinst,m))
(mkCompGenLet m thattagv
(mkUnionCaseTagGet (thate,tcref,tinst,m))
tagsEqTested)
let expr = mkBindThatNullEquals g m thise thate expr
thisv,thatv, expr
/// Build the equality implementation for a union type when parameterized by a comparer
let mkUnionEqualityWithComparer g tcref (tycon:Tycon) (_thisv,thise) thatobje (thatv,thate) compe =
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 = new MatchBuilder(NoSequencePointAtInvisibleBinding,m )
let mkCase ucase =
let cref = mkNestedUnionCaseRef tcref ucase
let m = cref.Range
let thisucv,thisucve = mkCompGenLocal m "thisCastu" (mkProvenUnionCaseTy cref tinst)
let thatucv,thatucve = mkCompGenLocal m "thatCastu" (mkProvenUnionCaseTy cref tinst)
let mkTest j (argty:RecdField) =
mkCallGenericEqualityWithComparerOuter g m argty.FormalType
compe
(mkUnionCaseFieldGetProven(thisucve, cref, tinst, j, m))
(mkUnionCaseFieldGetProven(thatucve, cref, tinst, j, m))
let rfields = ucase.RecdFields
if isNil rfields then None else
Some (mkCase(Test.UnionCase(cref,tinst),
mbuilder.AddResultTarget
(mkCompGenLet m thisucv (mkUnionCaseProof(thise,cref,tinst,m))
(mkCompGenLet m thatucv (mkUnionCaseProof(thataddre,cref,tinst,m))
(mkEqualsTestConjuncts g m (List.mapi mkTest rfields))),
SuppressSequencePointAtTarget)))
let nullary,nonNullary = List.partition isNone (List.map mkCase ucases)
if isNil nonNullary then mkTrue g m else
let dtree =
TDSwitch(thise,List.map (function (Some c) -> c | None -> failwith "mkUnionEquality") nonNullary,
(if isNil nullary then None else Some (mbuilder.AddResultTarget(mkTrue g m,SuppressSequencePointAtTarget))),
m)
mbuilder.Close(dtree,m,g.bool_ty)
let expr =
if ucases.Length = 1 then expr else
let tagsEqTested =
mkCond NoSequencePointAtStickyBinding SuppressSequencePointAtTarget m g.bool_ty
(mkILAsmCeq g m thistage thattage)
expr
(mkFalse g m)
mkCompGenLet m thistagv
(mkUnionCaseTagGet (thise,tcref,tinst,m))
(mkCompGenLet m thattagv
(mkUnionCaseTagGet (thataddre,tcref,tinst,m))
tagsEqTested)
let expr = mkBindThatAddr g m ty thataddrv thatv thate expr
let expr = mkIsInstConditional g m ty thatobje thatv expr (mkFalse g m)
let expr = if tycon.IsStructOrEnumTycon then 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 fty = fspec.FormalType
let fref = mkNestedRecdFieldRef tcref fspec
let m = fref.Range
let e = mkRecdFieldGetViaExprAddr(thise, fref, tinst, m)
mkCallGenericHashWithComparerOuter g m fty 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 fty = rfield.FormalType
let e = mkExnCaseFieldGet(thise, exnref, i, m)
mkCallGenericHashWithComparerOuter g m fty 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 = new MatchBuilder(NoSequencePointAtInvisibleBinding,m )
let accv,acce = mkMutableCompGenLocal m "i" g.int_ty
let mkCase i ucase1 =
let c1ref = mkNestedUnionCaseRef tcref ucase1
let ucv,ucve = mkCompGenLocal m "unionCase" (mkProvenUnionCaseTy c1ref tinst)
let m = c1ref.Range
let mkHash j (rfield:RecdField) =
let fty = rfield.FormalType
let e = mkUnionCaseFieldGetProven(ucve, c1ref, tinst, j, m)
mkCallGenericHashWithComparerOuter g m fty compe e
mkCase(Test.UnionCase(c1ref,tinst),
mbuilder.AddResultTarget
(mkCompGenLet m ucv
(mkUnionCaseProof(thise,c1ref,tinst,m))
(mkCompGenSequential m
(mkValSet m (mkLocalValRef accv) (mkInt g m i))
(mkCombineHashGenerators g m (List.mapi mkHash ucase1.RecdFields) (mkLocalValRef accv) acce)),
SuppressSequencePointAtTarget))
let dtree = TDSwitch(thise,List.mapi mkCase ucases, None,m)
let stmt = mbuilder.Close(dtree,m,g.int_ty)
let expr = mkCompGenLet m accv (mkZero g m) stmt
let expr = mkBindNullHash g m thise expr
thisv,expr
//-------------------------------------------------------------------------
// The predicate that determines which types implement the
// pre-baked IStructuralHash and IComparable semantics associated with F#
// types. Note abstract types are not _known_ to implement these interfaces,
// though the interfaces may be discoverable via type tests.
//-------------------------------------------------------------------------
let isNominalExnc (exnc:Tycon) =
match exnc.ExceptionInfo with
| TExnAbbrevRepr _ | TExnNone | TExnAsmRepr _ -> false
| TExnFresh _ -> true
let isTrueFSharpStructTycon _g (tycon: Tycon) =
(tycon.IsFSharpStructOrEnumTycon && not tycon.IsFSharpEnumTycon)
let canBeAugmentedWithEquals g (tycon:Tycon) =
tycon.IsUnionTycon ||
tycon.IsRecordTycon ||
(tycon.IsExceptionDecl && isNominalExnc tycon) ||
isTrueFSharpStructTycon g tycon
let canBeAugmentedWithCompare g (tycon:Tycon) =
tycon.IsUnionTycon ||
tycon.IsRecordTycon ||
isTrueFSharpStructTycon g tycon
let getAugmentationAttribs g (tycon:Tycon) =
canBeAugmentedWithEquals g tycon,
canBeAugmentedWithCompare g tycon,
TryFindFSharpBoolAttribute g g.attrib_NoEqualityAttribute tycon.Attribs,
TryFindFSharpBoolAttribute g g.attrib_CustomEqualityAttribute tycon.Attribs,
TryFindFSharpBoolAttribute g g.attrib_ReferenceEqualityAttribute tycon.Attribs,
TryFindFSharpBoolAttribute g g.attrib_StructuralEqualityAttribute tycon.Attribs,
TryFindFSharpBoolAttribute g g.attrib_NoComparisonAttribute tycon.Attribs,
TryFindFSharpBoolAttribute g g.attrib_CustomComparisonAttribute tycon.Attribs,
TryFindFSharpBoolAttribute g g.attrib_StructuralComparisonAttribute tycon.Attribs
let CheckAugmentationAttribs isImplementation g amap (tycon:Tycon)=
let m = tycon.Range
let attribs = getAugmentationAttribs g tycon
match attribs with
// THESE ARE THE LEGITIMATE CASES
// [< >] on anything
| _, _ , None , None, None , None, None , None , None
// [<CustomEquality; CustomComparison>] on union/record/struct
| true, _, None, Some(true), None , None , None , Some(true), None
// [<CustomEquality; NoComparison>] on union/record/struct
| true, _, None, Some(true), None , None , Some(true), None , None ->
()
// [<ReferenceEquality; NoComparison>] on union/record/struct
| true, _, None, None , Some(true), None , Some(true), None , None
// [<ReferenceEquality>] on union/record/struct
| true, _, None, None , Some(true), None , None , None , None ->
if isTrueFSharpStructTycon g tycon then
errorR(Error(FSComp.SR.augNoRefEqualsOnStruct(), m))
else ()
// [<StructuralEquality; StructuralComparison>] on union/record/struct
| true, true, None, None , None , Some(true), None , None , Some(true)
// [<StructuralEquality; NoComparison>]
| true, _, None, None , None , Some(true), Some(true), None , None
// [<StructuralEquality; CustomComparison>]
| true, _, None, None , None , Some(true), None , Some(true), None
// [<NoComparison>] on anything
| _ , _, None, None , None , None , Some(true), None , None
// [<NoEquality; NoComparison>] on anything
| _ , _, Some(true), None, None , None , Some(true), None , None ->
()
(* THESE ARE THE ERROR CASES *)
// [<NoEquality; ...>]
| _, _, Some(true), _, _, _, None, _, _ ->
errorR(Error(FSComp.SR.augNoEqualityNeedsNoComparison(), m))
// [<StructuralComparison(_)>]
| true, true, _, _, _ , None , _, _, Some(true) ->
errorR(Error(FSComp.SR.augStructCompNeedsStructEquality(), m))
// [<StructuralEquality(_)>]
| true, _, _, _, _ , Some(true), None, _, None ->
errorR(Error(FSComp.SR.augStructEqNeedsNoCompOrStructComp(), m))
// [<StructuralEquality(_)>]
| true, _, _, Some(true), _ , _, None, None, _ ->
errorR(Error(FSComp.SR.augCustomEqNeedsNoCompOrCustomComp(), m))
// [<ReferenceEquality; StructuralEquality>]
| true, _, _, _, Some(true) , Some(true) , _, _, _
// [<ReferenceEquality; StructuralComparison(_) >]
| true, _, _, _, Some(true), _, _, _, Some(true) ->
errorR(Error(FSComp.SR.augTypeCantHaveRefEqAndStructAttrs(), m))
// non augmented type, [<ReferenceEquality; ... >]
// non augmented type, [<StructuralEquality; ... >]
// non augmented type, [<StructuralComparison(_); ... >]
| false, _, _, _, Some(true), _ , _ , _, _
| false, _, _, _, _ , Some(true), _ , _, _
| false, _, _, _, _ , _ , _ , _, Some(true) ->
errorR(Error(FSComp.SR.augOnlyCertainTypesCanHaveAttrs(), m))
// All other cases
| _ ->
errorR(Error(FSComp.SR.augInvalidAttrs(), m))
let hasNominalInterface tcref =
let ty = generalizedTyconRef (mkLocalTyconRef tycon)
ExistsHeadTypeInEntireHierarchy g amap tycon.Range ty tcref
let hasExplicitICompare =
hasNominalInterface g.tcref_System_IStructuralComparable ||
hasNominalInterface g.tcref_System_IComparable
let hasExplicitIGenericCompare =
hasNominalInterface g.system_GenericIComparable_tcref
let hasExplicitEquals =
tycon.HasOverride g "Equals" [g.obj_ty] ||
hasNominalInterface g.tcref_System_IStructuralEquatable
let hasExplicitGenericEquals =
hasNominalInterface g.system_GenericIEquatable_tcref
match attribs with
// [<NoEquality>] + any equality semantics
| _, _, Some(true), _, _, _, _, _, _ when (hasExplicitEquals || hasExplicitGenericEquals) ->
warning(Error(FSComp.SR.augNoEqNeedsNoObjEquals(), m))
// [<NoComparison>] + any comparison semantics
| _, _, _, _, _, _, Some(true), _, _ when (hasExplicitICompare || hasExplicitIGenericCompare) ->
warning(Error(FSComp.SR.augNoCompCantImpIComp(), m))
// [<CustomEquality>] + no explicit override Object.Equals + no explicit IStructuralEquatable
| _, _, _, Some(true), _, _, _, _, _ when isImplementation && not hasExplicitEquals && not hasExplicitGenericEquals->
errorR(Error(FSComp.SR.augCustomEqNeedsObjEquals(), m))
// [<CustomComparison>] + no explicit IComparable + no explicit IStructuralComparable
| _, _, _, _, _, _, _, Some(true), _ when isImplementation && not hasExplicitICompare && not hasExplicitIGenericCompare ->
errorR(Error(FSComp.SR.augCustomCompareNeedsIComp(), m))
// [<ReferenceEquality>] + any equality semantics
| _, _, _, _, Some(true), _, _, _, _ when (hasExplicitEquals || hasExplicitIGenericCompare) ->
errorR(Error(FSComp.SR.augRefEqCantHaveObjEquals(), m))
| _ ->
()
let TyconIsCandidateForAugmentationWithCompare g (tycon:Tycon) =
// This type gets defined in prim-types, before we can add attributes to F# type definitions
let isUnit = g.compilingFslib && tycon.DisplayName = "Unit"
not isUnit &&
match getAugmentationAttribs g tycon with
// [< >]
| true, true, None, None, None, None , None, None, None
// [<StructuralEquality; StructuralComparison>]
| true, true, None, None, None, Some(true), None, None, Some(true)
// [<StructuralComparison>]
| true, true, None, None, None, None, None, None, Some(true) -> true
// other cases
| _ -> false
let TyconIsCandidateForAugmentationWithEquals g (tycon:Tycon) =
// This type gets defined in prim-types, before we can add attributes to F# type definitions
let isUnit = g.compilingFslib && tycon.DisplayName = "Unit"
not isUnit &&
match getAugmentationAttribs g tycon with
// [< >]
| true, _, None, None, None, None , _, _, _
// [<StructuralEquality; _ >]
// [<StructuralEquality; StructuralComparison>]
| true, _, None, None, None, Some(true), _, _, _ -> true
// other cases
| _ -> false
let TyconIsCandidateForAugmentationWithHash g tycon = TyconIsCandidateForAugmentationWithEquals g tycon
//-------------------------------------------------------------------------
// Make values that represent the implementations of the
// IComparable semantics associated with F# types.
//-------------------------------------------------------------------------
let slotImplMethod (final,c,slotsig) =
{ ImplementedSlotSigs=[slotsig];
MemberFlags=
{ IsInstance=true;
IsDispatchSlot=false;
IsFinal=final;
IsOverrideOrExplicitImpl=true;
MemberKind=MemberKind.Member};
IsImplemented=false;
ApparentParent=c}
let nonVirtualMethod c =
{ ImplementedSlotSigs=[];
MemberFlags={ IsInstance=true;
IsDispatchSlot=false;
IsFinal=false;
IsOverrideOrExplicitImpl=false;
MemberKind=MemberKind.Member};
IsImplemented=false;
ApparentParent=c}
let unitArg = ValReprInfo.unitArgData
let unaryArg = [ ValReprInfo.unnamedTopArg ]
let tupArg = [ [ ValReprInfo.unnamedTopArg1; ValReprInfo.unnamedTopArg1 ] ]
let mkValSpec g (tcref:TyconRef) tmty vis slotsig methn ty argData =
let m = tcref.Range
let tps = tcref.Typars(m)
let final = isUnionTy g tmty || isRecdTy g tmty || isStructTy g tmty
let membInfo = match slotsig with None -> nonVirtualMethod tcref | Some(slotsig) -> slotImplMethod(final,tcref,slotsig)
let inl = ValInline.Optional
let args = ValReprInfo.unnamedTopArg :: argData
let topValInfo = Some (ValReprInfo (ValReprInfo.InferTyparInfo tps, args, ValReprInfo.unnamedRetVal))
NewVal (methn, m, None, ty, Immutable, true, topValInfo, vis, ValNotInRecScope, Some(membInfo), NormalVal, [], inl, XmlDoc.Empty, true, false, false, false, false, false, None, Parent(tcref))
let MakeValsForCompareAugmentation g (tcref:TyconRef) =
let m = tcref.Range
let _,tmty = mkMinimalTy g tcref
let tps = tcref.Typars m
let vis = tcref.TypeReprAccessibility
mkValSpec g tcref tmty vis (Some(mkIComparableCompareToSlotSig g)) "CompareTo" (tps +-> (mkCompareObjTy g tmty)) unaryArg,
mkValSpec g tcref tmty vis (Some(mkGenericIComparableCompareToSlotSig g tmty)) "CompareTo" (tps +-> (mkCompareTy g tmty)) unaryArg
let MakeValsForCompareWithComparerAugmentation g (tcref:TyconRef) =
let m = tcref.Range
let _,tmty = mkMinimalTy g tcref
let tps = tcref.Typars m
let vis = tcref.TypeReprAccessibility
mkValSpec g tcref tmty vis (Some(mkIStructuralComparableCompareToSlotSig g)) "CompareTo" (tps +-> (mkCompareWithComparerTy g tmty)) tupArg
let MakeValsForEqualsAugmentation g (tcref:TyconRef) =
let m = tcref.Range
let _,tmty = mkMinimalTy g tcref
let vis = tcref.TypeReprAccessibility
let tps = tcref.Typars m
let objEqualsVal = mkValSpec g tcref tmty vis (Some(mkEqualsSlotSig g)) "Equals" (tps +-> (mkEqualsObjTy g tmty)) unaryArg
let nocEqualsVal = mkValSpec g tcref tmty vis (if tcref.Deref.IsExceptionDecl then None else Some(mkGenericIEquatableEqualsSlotSig g tmty)) "Equals" (tps +-> (mkEqualsTy g tmty)) unaryArg
objEqualsVal,nocEqualsVal
let MakeValsForEqualityWithComparerAugmentation g (tcref:TyconRef) =
let _,tmty = mkMinimalTy g tcref
let vis = tcref.TypeReprAccessibility
let tps = tcref.Typars(tcref.Range)
let objGetHashCodeVal = mkValSpec g tcref tmty vis (Some(mkGetHashCodeSlotSig g)) "GetHashCode" (tps +-> (mkHashTy g tmty)) unitArg
let withcGetHashCodeVal = mkValSpec g tcref tmty vis (Some(mkIStructuralEquatableGetHashCodeSlotSig g)) "GetHashCode" (tps +-> (mkHashWithComparerTy g tmty)) unaryArg
let withcEqualsVal = mkValSpec g tcref tmty vis (Some(mkIStructuralEquatableEqualsSlotSig g)) "Equals" (tps +-> (mkEqualsWithComparerTy g tmty)) tupArg
objGetHashCodeVal,withcGetHashCodeVal,withcEqualsVal
let MakeBindingsForCompareAugmentation g (tycon:Tycon) =
let tcref = mkLocalTyconRef tycon
let m = tycon.Range
let tps = tycon.Typars(tycon.Range)
let mkCompare comparef =
match tycon.GeneratedCompareToValues with
| None -> []
| Some (vref1,vref2) ->
let vspec1 = vref1.Deref
let vspec2 = vref2.Deref
(* this is the body of the override *)
let rhs1 =
let tinst,ty = mkMinimalTy g tcref
let thisv,thise = mkThisVar g m ty
let thatobjv,thatobje = mkCompGenLocal m "obj" g.obj_ty
let comparee =
if isUnitTy g ty then mkZero g m else
let thate = mkCoerceExpr (thatobje, ty, m, g.obj_ty)
mkApps g ((exprForValRef m vref2,vref2.Type), (if isNil tinst then [] else [tinst]), [thise;thate], m)
mkLambdas m tps [thisv;thatobjv] (comparee,g.int_ty)
let rhs2 =
let thisv,thatv,comparee = comparef g tcref tycon
mkLambdas m tps [thisv;thatv] (comparee,g.int_ty)
[ // This one must come first because it may be inlined into the second
mkCompGenBind vspec2 rhs2;
mkCompGenBind vspec1 rhs1; ]
if tycon.IsUnionTycon then mkCompare mkUnionCompare
elif tycon.IsRecordTycon || tycon.IsStructOrEnumTycon then mkCompare mkRecdCompare
else []
let MakeBindingsForCompareWithComparerAugmentation g (tycon:Tycon) =
let tcref = mkLocalTyconRef tycon
let m = tycon.Range
let tps = tycon.Typars(tycon.Range)
let mkCompare comparef =
match tycon.GeneratedCompareToWithComparerValues with
| None -> []
| Some (vref) ->
let vspec = vref.Deref
let _,ty = mkMinimalTy g tcref
let compv,compe = mkCompGenLocal m "comp" g.mk_IComparer_ty
let thisv,thise = mkThisVar g m ty
let thatobjv,thatobje = mkCompGenLocal m "obj" g.obj_ty
let thate = mkCoerceExpr (thatobje, ty, m, g.obj_ty)
let rhs =
let comparee = comparef g tcref tycon (thisv,thise) (thatobjv,thate) compe
let comparee = if isUnitTy g ty then mkZero g m else comparee
mkMultiLambdas m tps [[thisv];[thatobjv;compv]] (comparee,g.int_ty)
[mkCompGenBind vspec rhs]
if tycon.IsUnionTycon then mkCompare mkUnionCompareWithComparer
elif tycon.IsRecordTycon || tycon.IsStructOrEnumTycon then mkCompare mkRecdCompareWithComparer
else []
let MakeBindingsForEqualityWithComparerAugmentation g (tycon:Tycon) =
let tcref = mkLocalTyconRef tycon
let m = tycon.Range
let tps = tycon.Typars(tycon.Range)
let mkStructuralEquatable hashf equalsf =
match tycon.GeneratedHashAndEqualsWithComparerValues with
| None -> []
| Some (objGetHashCodeVal,withcGetHashCodeVal,withcEqualsVal) ->
// build the hash rhs
let withcGetHashCodeExpr =
let compv,compe = mkCompGenLocal m "comp" g.mk_IEqualityComparer_ty
let thisv,hashe = hashf g tcref tycon compe
mkLambdas m tps [thisv;compv] (hashe,g.int_ty)
// build the equals rhs
let withcEqualsExpr =
let _tinst,ty = mkMinimalTy g tcref
let thisv,thise = mkThisVar g m ty
let thatobjv,thatobje = mkCompGenLocal m "obj" g.obj_ty
let thatv,thate = mkCompGenLocal m "that" ty
let compv,compe = mkCompGenLocal m "comp" g.mk_IEqualityComparer_ty
let equalse = equalsf g tcref tycon (thisv,thise) thatobje (thatv,thate) compe
mkMultiLambdas m tps [[thisv];[thatobjv;compv]] (equalse,g.bool_ty)
let objGetHashCodeExpr =
let tinst,ty = mkMinimalTy g tcref
let thisv,thise = mkThisVar g m ty
let unitv,_ = mkCompGenLocal m "unitArg" g.unit_ty
let hashe =
if isUnitTy g ty then mkZero g m else
let compe = mkILCallGetEqualityComparer g m
mkApps g ((exprForValRef m withcGetHashCodeVal,withcGetHashCodeVal.Type), (if isNil tinst then [] else [tinst]), [thise; compe], m)
mkLambdas m tps [thisv; unitv] (hashe,g.int_ty)
[(mkCompGenBind withcGetHashCodeVal.Deref withcGetHashCodeExpr) ;
(mkCompGenBind objGetHashCodeVal.Deref objGetHashCodeExpr) ;
(mkCompGenBind withcEqualsVal.Deref withcEqualsExpr)]
if tycon.IsUnionTycon then mkStructuralEquatable mkUnionHashWithComparer mkUnionEqualityWithComparer
elif (tycon.IsRecordTycon || tycon.IsStructOrEnumTycon) then mkStructuralEquatable mkRecdHashWithComparer mkRecdEqualityWithComparer
elif tycon.IsExceptionDecl then mkStructuralEquatable mkExnHashWithComparer mkExnEqualityWithComparer
else []
let MakeBindingsForEqualsAugmentation g (tycon:Tycon) =
let tcref = mkLocalTyconRef tycon
let m = tycon.Range
let tps = tycon.Typars(m)
let mkEquals equalsf =
match tycon.GeneratedHashAndEqualsValues with