-
Notifications
You must be signed in to change notification settings - Fork 763
/
prim-types.fs
7776 lines (6775 loc) · 463 KB
/
prim-types.fs
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. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nowarn "25" // Incomplete match expressions
#nowarn "35" // This construct is deprecated: the treatment of this operator is now handled directly by the F# compiler and its meaning may not be redefined.
#nowarn "44" // This construct is deprecated. This function is for use by compiled F# code and should not be used directly
#nowarn "52" // The value has been copied to ensure the original is not mutated by this operation
#nowarn "60" // Override implementations in augmentations are now deprecated. Override implementations should be given as part of the initial declaration of a type.
#nowarn "61" // The containing type can use 'null' as a representation value for its nullary union case. This member will be compiled as a static member.
#nowarn "62" // The syntax 'module ... : sig .. end' is for ML compatibility. Consider using 'module ... = begin .. end'.
#nowarn "69" // Interface implementations in augmentations are now deprecated. Interface implementations should be given on the initial declaration of a type.
#nowarn "77" // Member constraints with the name 'Exp' are given special status by the F# compiler as certain .NET types are implicitly augmented with this member. This may result in compilation failures if you attempt to invoke the member constraint from your own code.
namespace Microsoft.FSharp.Core
open System
open System.Collections
open System.Collections.Generic
open System.Diagnostics
open System.Globalization
open System.Text
//-------------------------------------------------------------------------
// Unit
[<CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1036:OverrideMethodsOnComparableTypes")>] // No op_equality on unit
type Unit() =
override x.GetHashCode() = 0
override x.Equals(obj:obj) =
match obj with null -> true | :? Unit -> true | _ -> false
interface System.IComparable with
member x.CompareTo(_obj:obj) = 0
and unit = Unit
#if FX_NO_STRUCTURAL_EQUALITY
namespace System.Collections
open System
open Microsoft.FSharp.Core
//-------------------------------------------------------------------------
// Structural equality
type IStructuralEquatable =
interface
abstract Equals: o:System.Object * comp:System.Collections.IEqualityComparer -> bool
abstract GetHashCode: comp:System.Collections.IEqualityComparer -> int
end
//-------------------------------------------------------------------------
// Structural comparison
and IStructuralComparable =
interface
abstract CompareTo: o:System.Object * comp:System.Collections.IComparer -> int
end
#else
#endif
namespace Microsoft.FSharp.Core
open System
open System.Collections
open System.Collections.Generic
open System.Diagnostics
open System.Globalization
open System.Text
//-------------------------------------------------------------------------
// enumerations
type SourceConstructFlags =
| None = 0
| SumType = 1
| RecordType = 2
| ObjectType = 3
| Field = 4
| Exception = 5
| Closure = 6
| Module = 7
| UnionCase = 8
| Value = 9
| KindMask = 31
| NonPublicRepresentation = 32
[<Flags>]
type CompilationRepresentationFlags =
| None = 0
| Static = 1
| Instance = 2
| ModuleSuffix = 4 // append 'Module' to the end of a non-unique module
| UseNullAsTrueValue = 8 // Note if you change this then change CompilationRepresentationFlags_PermitNull further below
| Event = 16
#if FX_NO_ICLONEABLE
module ICloneableExtensions =
type System.Array with
member x.Clone() =
let ty = (x.GetType()).GetElementType()
let clone = System.Array.CreateInstance(ty,x.Length)
x.CopyTo(clone,0)
clone
open ICloneableExtensions
#endif
[<AttributeUsage(AttributeTargets.Class,AllowMultiple=false)>]
type SealedAttribute(value:bool) =
inherit System.Attribute()
member x.Value = value
new() = new SealedAttribute(true)
[<AttributeUsage(AttributeTargets.Class,AllowMultiple=false)>]
[<Sealed>]
type AbstractClassAttribute() =
inherit System.Attribute()
[<AttributeUsage(AttributeTargets.GenericParameter,AllowMultiple=false)>]
[<Sealed>]
type EqualityConditionalOnAttribute() =
inherit System.Attribute()
[<AttributeUsage(AttributeTargets.GenericParameter,AllowMultiple=false)>]
[<Sealed>]
type ComparisonConditionalOnAttribute() =
inherit System.Attribute()
[<AttributeUsage(AttributeTargets.Class,AllowMultiple=false)>]
[<Sealed>]
type AllowNullLiteralAttribute(value: bool) =
inherit System.Attribute()
member x.Value = value
new () = new AllowNullLiteralAttribute(true)
[<AttributeUsage(AttributeTargets.Field,AllowMultiple=false)>]
[<Sealed>]
type VolatileFieldAttribute() =
inherit System.Attribute()
[<AttributeUsage (AttributeTargets.Class,AllowMultiple=false)>]
[<Sealed>]
type DefaultAugmentationAttribute(value:bool) =
inherit System.Attribute()
member x.Value = value
[<AttributeUsage (AttributeTargets.Property,AllowMultiple=false)>]
[<Sealed>]
type CLIEventAttribute() =
inherit System.Attribute()
[<AttributeUsage (AttributeTargets.Class,AllowMultiple=false)>]
[<Sealed>]
type CLIMutableAttribute() =
inherit System.Attribute()
[<AttributeUsage (AttributeTargets.Class,AllowMultiple=false)>]
[<Sealed>]
type AutoSerializableAttribute(value:bool) =
inherit System.Attribute()
member x.Value = value
[<AttributeUsage (AttributeTargets.Field,AllowMultiple=false)>]
[<Sealed>]
type DefaultValueAttribute(check:bool) =
inherit System.Attribute()
member x.Check = check
new() = new DefaultValueAttribute(true)
[<AttributeUsage (AttributeTargets.Method,AllowMultiple=false)>]
[<Sealed>]
type EntryPointAttribute() =
inherit System.Attribute()
[<AttributeUsage (AttributeTargets.Class,AllowMultiple=false)>]
[<Sealed>]
type ReferenceEqualityAttribute() =
inherit System.Attribute()
[<AttributeUsage (AttributeTargets.Class,AllowMultiple=false)>]
[<Sealed>]
type StructuralComparisonAttribute() =
inherit System.Attribute()
[<AttributeUsage (AttributeTargets.Class,AllowMultiple=false)>]
[<Sealed>]
type StructuralEqualityAttribute() =
inherit System.Attribute()
[<AttributeUsage (AttributeTargets.Class ||| AttributeTargets.Interface ||| AttributeTargets.Delegate ||| AttributeTargets.Struct ||| AttributeTargets.Enum,AllowMultiple=false)>]
[<Sealed>]
type NoEqualityAttribute() =
inherit System.Attribute()
[<AttributeUsage (AttributeTargets.Class ||| AttributeTargets.Struct,AllowMultiple=false)>]
[<Sealed>]
type CustomEqualityAttribute() =
inherit System.Attribute()
[<AttributeUsage (AttributeTargets.Class ||| AttributeTargets.Struct,AllowMultiple=false)>]
[<Sealed>]
type CustomComparisonAttribute() =
inherit System.Attribute()
[<AttributeUsage (AttributeTargets.Class ||| AttributeTargets.Interface ||| AttributeTargets.Delegate ||| AttributeTargets.Struct ||| AttributeTargets.Enum,AllowMultiple=false)>]
[<Sealed>]
type NoComparisonAttribute() =
inherit System.Attribute()
[<AttributeUsage (AttributeTargets.Class ||| AttributeTargets.Parameter ||| AttributeTargets.Method ||| AttributeTargets.Property ||| AttributeTargets.Constructor,AllowMultiple=false)>]
[<Sealed>]
type ReflectedDefinitionAttribute(includeValue: bool) =
inherit System.Attribute()
new() = ReflectedDefinitionAttribute(false)
member x.IncludeValue = includeValue
[<AttributeUsage (AttributeTargets.Method ||| AttributeTargets.Class ||| AttributeTargets.Field ||| AttributeTargets.Interface ||| AttributeTargets.Struct ||| AttributeTargets.Delegate ||| AttributeTargets.Enum ||| AttributeTargets.Property,AllowMultiple=false)>]
[<Sealed>]
type CompiledNameAttribute(compiledName:string) =
inherit System.Attribute()
member x.CompiledName = compiledName
[<AttributeUsage (AttributeTargets.Struct,AllowMultiple=false)>]
[<Sealed>]
type StructAttribute() =
inherit System.Attribute()
[<AttributeUsage (AttributeTargets.GenericParameter ||| AttributeTargets.Class,AllowMultiple=false)>]
[<Sealed>]
type MeasureAttribute() =
inherit System.Attribute()
[<AttributeUsage (AttributeTargets.Class,AllowMultiple=false)>]
[<Sealed>]
type MeasureAnnotatedAbbreviationAttribute() =
inherit System.Attribute()
[<AttributeUsage (AttributeTargets.Interface,AllowMultiple=false)>]
[<Sealed>]
type InterfaceAttribute() =
inherit System.Attribute()
[<AttributeUsage (AttributeTargets.Class,AllowMultiple=false)>]
[<Sealed>]
type ClassAttribute() =
inherit System.Attribute()
[<AttributeUsage(AttributeTargets.Field,AllowMultiple=false)>]
[<Sealed>]
type LiteralAttribute() =
inherit System.Attribute()
[<AttributeUsage(AttributeTargets.Assembly,AllowMultiple=false)>]
[<Sealed>]
type FSharpInterfaceDataVersionAttribute(major:int,minor:int,release:int) =
inherit System.Attribute()
member x.Major = major
member x.Minor = minor
member x.Release = release
[<AttributeUsage(AttributeTargets.All,AllowMultiple=false)>]
[<Sealed>]
type CompilationMappingAttribute(sourceConstructFlags:SourceConstructFlags,
variantNumber:int,
sequenceNumber:int,
resourceName:string,
typeDefinitions:System.Type[]) =
inherit System.Attribute()
member x.SourceConstructFlags = sourceConstructFlags
member x.SequenceNumber = sequenceNumber
member x.VariantNumber = variantNumber
new(sourceConstructFlags) = CompilationMappingAttribute(sourceConstructFlags,0,0)
new(sourceConstructFlags,sequenceNumber) = CompilationMappingAttribute(sourceConstructFlags,0,sequenceNumber)
new(sourceConstructFlags,variantNumber,sequenceNumber) = CompilationMappingAttribute(sourceConstructFlags,variantNumber,sequenceNumber,null,null)
new(resourceName, typeDefinitions) = CompilationMappingAttribute(SourceConstructFlags.None,0,0,resourceName, typeDefinitions)
member x.TypeDefinitions = typeDefinitions
member x.ResourceName = resourceName
[<AttributeUsage(AttributeTargets.All,AllowMultiple=false)>]
[<Sealed>]
type CompilationSourceNameAttribute(sourceName:string) =
inherit System.Attribute()
member x.SourceName = sourceName
//-------------------------------------------------------------------------
[<AttributeUsage(AttributeTargets.All,AllowMultiple=false)>]
[<Sealed>]
type CompilationRepresentationAttribute (flags : CompilationRepresentationFlags) =
inherit System.Attribute()
member x.Flags = flags
[<AttributeUsage(AttributeTargets.All,AllowMultiple=false)>]
[<Sealed>]
type ExperimentalAttribute(message:string) =
inherit System.Attribute()
member x.Message = message
[<AttributeUsage(AttributeTargets.Method,AllowMultiple=false)>]
[<Sealed>]
type CompilationArgumentCountsAttribute(counts:int[]) =
inherit System.Attribute()
member x.Counts =
let unboxPrim(x:obj) = (# "unbox.any !0" type ('T) x : 'T #)
(unboxPrim(counts.Clone()) : System.Collections.Generic.IEnumerable<int>)
[<AttributeUsage(AttributeTargets.Method,AllowMultiple=false)>]
[<Sealed>]
type CustomOperationAttribute(name:string) =
inherit System.Attribute()
let mutable isBinary = false
let mutable allowInto = false
let mutable isJoin = false
let mutable isGroupJoin = false
let mutable maintainsVarSpace = false
let mutable maintainsVarSpaceWithBind = false
let mutable joinOnWord = ""
member x.Name = name
member x.AllowIntoPattern with get() = allowInto and set v = allowInto <- v
member x.IsLikeZip with get() = isBinary and set v = isBinary <- v
member x.IsLikeJoin with get() = isJoin and set v = isJoin <- v
member x.IsLikeGroupJoin with get() = isGroupJoin and set v = isGroupJoin <- v
member x.JoinConditionWord with get() = joinOnWord and set v = joinOnWord <- v
member x.MaintainsVariableSpace with get() = maintainsVarSpace and set v = maintainsVarSpace <- v
member x.MaintainsVariableSpaceUsingBind with get() = maintainsVarSpaceWithBind and set v = maintainsVarSpaceWithBind <- v
[<AttributeUsage(AttributeTargets.Parameter,AllowMultiple=false)>]
[<Sealed>]
type ProjectionParameterAttribute() =
inherit System.Attribute()
[<AttributeUsage(AttributeTargets.Class ||| AttributeTargets.Interface ||| AttributeTargets.Struct ||| AttributeTargets.Delegate ||| AttributeTargets.Enum,AllowMultiple=false)>]
[<Sealed>]
type StructuredFormatDisplayAttribute(value:string) =
inherit System.Attribute()
member x.Value = value
[<AttributeUsage(AttributeTargets.All,AllowMultiple=false)>]
[<Sealed>]
type CompilerMessageAttribute(message:string, messageNumber : int) =
inherit System.Attribute()
let mutable isError = false
let mutable isHidden = false
member x.Message = message
member x.MessageNumber = messageNumber
member x.IsError with get() = isError and set v = isError <- v
member x.IsHidden with get() = isHidden and set v = isHidden <- v
new (message, messageNumber) = CompilerMessageAttribute(message, messageNumber)
[<AttributeUsage(AttributeTargets.Method ||| AttributeTargets.Property,AllowMultiple=false)>]
[<Sealed>]
type UnverifiableAttribute() =
inherit System.Attribute()
[<AttributeUsage(AttributeTargets.Method ||| AttributeTargets.Property,AllowMultiple=false)>]
[<Sealed>]
type NoDynamicInvocationAttribute() =
inherit System.Attribute()
[<AttributeUsage(AttributeTargets.Parameter,AllowMultiple=false)>]
[<Sealed>]
type OptionalArgumentAttribute() =
inherit System.Attribute()
[<AttributeUsage(AttributeTargets.Method,AllowMultiple=false)>]
[<Sealed>]
type GeneralizableValueAttribute() =
inherit System.Attribute()
[<AttributeUsage(AttributeTargets.Method,AllowMultiple=false)>]
[<Sealed>]
type RequiresExplicitTypeArgumentsAttribute() =
inherit System.Attribute()
[<AttributeUsage(AttributeTargets.Class,AllowMultiple=false)>]
[<Sealed>]
type RequireQualifiedAccessAttribute() =
inherit System.Attribute()
[<AttributeUsage (AttributeTargets.Class ||| AttributeTargets.Assembly,AllowMultiple=true)>]
[<Sealed>]
type AutoOpenAttribute(path:string) =
inherit System.Attribute()
member x.Path = path
new() = AutoOpenAttribute("")
[<MeasureAnnotatedAbbreviation>] type float<[<Measure>] 'Measure> = float
[<MeasureAnnotatedAbbreviation>] type float32<[<Measure>] 'Measure> = float32
[<MeasureAnnotatedAbbreviation>] type decimal<[<Measure>] 'Measure> = decimal
[<MeasureAnnotatedAbbreviation>] type int<[<Measure>] 'Measure> = int
[<MeasureAnnotatedAbbreviation>] type sbyte<[<Measure>] 'Measure> = sbyte
[<MeasureAnnotatedAbbreviation>] type int16<[<Measure>] 'Measure> = int16
[<MeasureAnnotatedAbbreviation>] type int64<[<Measure>] 'Measure> = int64
#if FX_RESHAPED_REFLECTION
module PrimReflectionAdapters =
open System.Reflection
open System.Linq
// copied from BasicInlinedOperations
let inline box (x:'T) = (# "box !0" type ('T) x : obj #)
let inline unboxPrim<'T>(x:obj) = (# "unbox.any !0" type ('T) x : 'T #)
type System.Type with
member inline this.IsGenericType = this.GetTypeInfo().IsGenericType
member inline this.IsValueType = this.GetTypeInfo().IsValueType
member inline this.IsSealed = this.GetTypeInfo().IsSealed
member inline this.IsAssignableFrom(otherTy : Type) = this.GetTypeInfo().IsAssignableFrom(otherTy.GetTypeInfo())
member inline this.GetGenericArguments() = this.GetTypeInfo().GenericTypeArguments
member inline this.GetProperty(name) = this.GetRuntimeProperty(name)
member inline this.GetMethod(name, parameterTypes) = this.GetRuntimeMethod(name, parameterTypes)
member inline this.GetCustomAttributes(attrTy : Type, inherits : bool) : obj[] =
unboxPrim<_> (box (CustomAttributeExtensions.GetCustomAttributes(this.GetTypeInfo(), attrTy, inherits).ToArray()))
open PrimReflectionAdapters
#endif
module BasicInlinedOperations =
let inline unboxPrim<'T>(x:obj) = (# "unbox.any !0" type ('T) x : 'T #)
let inline box (x:'T) = (# "box !0" type ('T) x : obj #)
let inline not (b:bool) = (# "ceq" b false : bool #)
let inline (=) (x:int) (y:int) = (# "ceq" x y : bool #)
let inline (<>) (x:int) (y:int) = not(# "ceq" x y : bool #)
let inline (>=) (x:int) (y:int) = not(# "clt" x y : bool #)
let inline (>=.) (x:int64) (y:int64) = not(# "clt" x y : bool #)
let inline (>=...) (x:char) (y:char) = not(# "clt" x y : bool #)
let inline (<=...) (x:char) (y:char) = not(# "cgt" x y : bool #)
let inline (/) (x:int) (y:int) = (# "div" x y : int #)
let inline (+) (x:int) (y:int) = (# "add" x y : int #)
let inline (+.) (x:int64) (y:int64) = (# "add" x y : int64 #)
let inline (+..) (x:uint64) (y:uint64) = (# "add" x y : uint64 #)
let inline ( *. ) (x:int64) (y:int64) = (# "mul" x y : int64 #)
let inline ( *.. ) (x:uint64) (y:uint64) = (# "mul" x y : uint64 #)
let inline (^) (x:string) (y:string) = System.String.Concat(x,y)
let inline (<<<) (x:int) (y:int) = (# "shl" x y : int #)
let inline ( * ) (x:int) (y:int) = (# "mul" x y : int #)
let inline (-) (x:int) (y:int) = (# "sub" x y : int #)
let inline (-.) (x:int64) (y:int64) = (# "sub" x y : int64 #)
let inline (-..) (x:uint64) (y:uint64) = (# "sub" x y : uint64 #)
let inline (>) (x:int) (y:int) = (# "cgt" x y : bool #)
let inline (<) (x:int) (y:int) = (# "clt" x y : bool #)
let inline ignore _ = ()
let inline intOfByte (b:byte) = (# "" b : int #)
let inline raise (e: System.Exception) = (# "throw" e : 'U #)
let inline length (x: 'T[]) = (# "ldlen conv.i4" x : int #)
let inline zeroCreate (n:int) = (# "newarr !0" type ('T) n : 'T[] #)
let inline get (arr: 'T[]) (n:int) = (# "ldelem.any !0" type ('T) arr n : 'T #)
let set (arr: 'T[]) (n:int) (x:'T) = (# "stelem.any !0" type ('T) arr n x #)
let inline objEq (xobj:obj) (yobj:obj) = (# "ceq" xobj yobj : bool #)
let inline int64Eq (x:int64) (y:int64) = (# "ceq" x y : bool #)
let inline int32Eq (x:int32) (y:int32) = (# "ceq" x y : bool #)
let inline floatEq (x:float) (y:float) = (# "ceq" x y : bool #)
let inline float32Eq (x:float32) (y:float32) = (# "ceq" x y : bool #)
let inline charEq (x:char) (y:char) = (# "ceq" x y : bool #)
let inline intOrder (x:int) (y:int) = if (# "clt" x y : bool #) then (0-1) else (# "cgt" x y : int #)
let inline int64Order (x:int64) (y:int64) = if (# "clt" x y : bool #) then (0-1) else (# "cgt" x y : int #)
let inline byteOrder (x:byte) (y:byte) = if (# "clt" x y : bool #) then (0-1) else (# "cgt" x y : int #)
let inline byteEq (x:byte) (y:byte) = (# "ceq" x y : bool #)
let inline int64 (x:int) = (# "conv.i8" x : int64 #)
let inline int32 (x:int64) = (# "conv.i4" x : int32 #)
let inline typeof<'T> =
let tok = (# "ldtoken !0" type('T) : System.RuntimeTypeHandle #)
System.Type.GetTypeFromHandle(tok)
let inline typedefof<'T> =
let ty = typeof<'T>
if ty.IsGenericType then ty.GetGenericTypeDefinition() else ty
let inline sizeof<'T> =
(# "sizeof !0" type('T) : int #)
let inline unsafeDefault<'T> : 'T = (# "ilzero !0" type ('T) : 'T #)
let inline isinstPrim<'T>(x:obj) = (# "isinst !0" type ('T) x : obj #)
let inline castclassPrim<'T>(x:obj) = (# "castclass !0" type ('T) x : 'T #)
let inline notnullPrim<'T when 'T : not struct>(x:'T) = (# "ldnull cgt.un" x : bool #)
let inline iscastPrim<'T when 'T : not struct>(x:obj) = (# "isinst !0" type ('T) x : 'T #)
open BasicInlinedOperations
module TupleUtils =
open BasicInlinedOperations
// adapted from System.Tuple::CombineHashCodes
let inline mask (n:int) (m:int) = (# "and" n m : int #)
let inline opshl (x:int) (n:int) : int = (# "shl" x (mask n 31) : int #)
let inline opxor (x:int) (y:int) : int = (# "xor" x y : int32 #)
let inline combineTupleHashes (h1 : int) (h2 : int) = (opxor ((opshl h1 5) + h1) h2)
let combineTupleHashCodes (codes : int []) =
let mutable (num : int32) = codes.Length - 1
while (num > 1) do
let mutable i = 0
while ((i * 2) < (num+1)) do
let index = i * 2
let num' = index + 1
if index = num then
set codes i (get codes index)
num <- i
else
set codes i (combineTupleHashes (get codes index) (get codes num))
if num' = num then
num <- i
i <- i + 1
combineTupleHashes (get codes 0) (get codes 1)
#if FX_NO_TUPLE
namespace System
open Microsoft.FSharp.Core.BasicInlinedOperations
open System
open System.Collections
open System.Collections.Generic
open System.Diagnostics
open System.Globalization
open System.Text
type Tuple<'T1>(t1:'T1) =
member t.Item1 = t1
interface IStructuralComparable
interface IStructuralEquatable
interface IComparable
#if TUPLE_STRUXT
// NOTE: Tuple`2 is a struct type.
// WARNING: If you change additional tuple types to be structs then you must change 'highestTupleStructType' in tastops.ml
#if FX_NO_DEBUG_DISPLAYS
#else
[<DebuggerDisplay("({Item1},{Item2})")>]
#endif
[<Struct>]
type Tuple<'T1,'T2> =
new (v1,v2) = { Item1 = v1; Item2 = v2 }
val Item1 : 'T1
val Item2 : 'T2
#else
type Tuple<'T1,'T2>(t1:'T1, t2:'T2) =
member t.Item1 = t1
member t.Item2 = t2
interface IStructuralComparable
interface IStructuralEquatable
interface IComparable
#endif
#if FX_NO_DEBUG_DISPLAYS
#else
[<DebuggerDisplay("({Item1},{Item2},{Item3})")>]
#endif
type Tuple<'T1,'T2,'T3>(t1:'T1,t2:'T2,t3:'T3) =
member t.Item1 = t1
member t.Item2 = t2
member t.Item3 = t3
interface IStructuralComparable
interface IStructuralEquatable
interface IComparable
#if FX_NO_DEBUG_DISPLAYS
#else
[<DebuggerDisplay("({Item1},{Item2},{Item3},{Item4})")>]
#endif
type Tuple<'T1,'T2,'T3,'T4>(t1:'T1,t2:'T2,t3:'T3,t4:'T4) =
member t.Item1 = t1
member t.Item2 = t2
member t.Item3 = t3
member t.Item4 = t4
interface IStructuralComparable
interface IStructuralEquatable
interface IComparable
#if FX_NO_DEBUG_DISPLAYS
#else
[<DebuggerDisplay("({Item1},{Item2},{Item3},{Item4},{Item5})")>]
#endif
type Tuple<'T1,'T2,'T3,'T4,'T5>(t1:'T1,t2:'T2,t3:'T3,t4:'T4,t5:'T5) =
member t.Item1 = t1
member t.Item2 = t2
member t.Item3 = t3
member t.Item4 = t4
member t.Item5 = t5
interface IStructuralComparable
interface IStructuralEquatable
interface IComparable
#if FX_NO_DEBUG_DISPLAYS
#else
[<DebuggerDisplay("({Item1},{Item2},{Item3},{Item4},{Item5},{Item6})")>]
#endif
type Tuple<'T1,'T2,'T3,'T4,'T5,'T6>(t1:'T1,t2:'T2,t3:'T3,t4:'T4,t5:'T5,t6:'T6) =
member t.Item1 = t1
member t.Item2 = t2
member t.Item3 = t3
member t.Item4 = t4
member t.Item5 = t5
member t.Item6 = t6
interface IStructuralComparable
interface IStructuralEquatable
interface IComparable
#if FX_NO_DEBUG_DISPLAYS
#else
[<DebuggerDisplay("({Item1},{Item2},{Item3},{Item4},{Item5},{Item6},{Item7})")>]
#endif
type Tuple<'T1,'T2,'T3,'T4,'T5,'T6,'T7>(t1:'T1,t2:'T2,t3:'T3,t4:'T4,t5:'T5,t6:'T6,t7:'T7) =
member t.Item1 = t1
member t.Item2 = t2
member t.Item3 = t3
member t.Item4 = t4
member t.Item5 = t5
member t.Item6 = t6
member t.Item7 = t7
interface IStructuralComparable
interface IStructuralEquatable
interface IComparable
#if FX_NO_DEBUG_DISPLAYS
#else
[<DebuggerDisplay("({Item1},{Item2},{Item3},{Item4},{Item5},{Item6},{Item7},{Rest})")>]
#endif
type Tuple<'T1,'T2,'T3,'T4,'T5,'T6,'T7,'TRest>(t1:'T1,t2:'T2,t3:'T3,t4:'T4,t5:'T5,t6:'T6,t7:'T7,rest:'TRest) =
member t.Item1 = t1
member t.Item2 = t2
member t.Item3 = t3
member t.Item4 = t4
member t.Item5 = t5
member t.Item6 = t6
member t.Item7 = t7
member t.Rest = rest
interface IStructuralComparable
interface IStructuralEquatable
interface IComparable
#else
#endif
namespace Microsoft.FSharp.Core
open System
open System.Collections
open System.Collections.Generic
open System.Diagnostics
open System.Globalization
open System.Text
open Microsoft.FSharp.Core
open Microsoft.FSharp.Core.BasicInlinedOperations
//-------------------------------------------------------------------------
// The main aim here is to bootsrap the definition of structural hashing
// and comparison. Calls to these form part of the auto-generated
// code for each new datatype.
module LanguagePrimitives =
module (* internal *) ErrorStrings =
// inline functions cannot call GetString, so we must make these bits public
let AddressOpNotFirstClassString = SR.GetString(SR.addressOpNotFirstClass)
let NoNegateMinValueString = SR.GetString(SR.noNegateMinValue)
// needs to be public to be visible from inline function 'average' and others
let InputSequenceEmptyString = SR.GetString(SR.inputSequenceEmpty)
// needs to be public to be visible from inline function 'average' and others
let InputArrayEmptyString = SR.GetString(SR.arrayWasEmpty)
// needs to be public to be visible from inline function 'average' and others
let InputMustBeNonNegativeString = SR.GetString(SR.inputMustBeNonNegative)
[<CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")>] // nested module OK
module IntrinsicOperators =
//-------------------------------------------------------------------------
// Lazy and/or. Laziness added by the F# compiler.
let (&) x y = if x then y else false
let (&&) x y = if x then y else false
[<CompiledName("Or")>]
let (or) x y = if x then true else y
let (||) x y = if x then true else y
//-------------------------------------------------------------------------
// Address-of
// Note, "raise<'T> : exn -> 'T" is manually inlined below.
// Byref usage checks prohibit type instantiations involving byrefs.
[<NoDynamicInvocation>]
let inline (~&) (x : 'T) : 'T byref =
ignore x // pretend the variable is used
let e = new System.ArgumentException(ErrorStrings.AddressOpNotFirstClassString)
(# "throw" (e :> System.Exception) : 'T byref #)
[<NoDynamicInvocation>]
let inline (~&&) (x : 'T) : nativeptr<'T> =
ignore x // pretend the variable is used
let e = new System.ArgumentException(ErrorStrings.AddressOpNotFirstClassString)
(# "throw" (e :> System.Exception) : nativeptr<'T> #)
open IntrinsicOperators
#if FX_RESHAPED_REFLECTION
open PrimReflectionAdapters
#endif
[<CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")>] // nested module OK
module IntrinsicFunctions =
// Unboxing, type casts, type tests
type TypeNullnessSemantics = int
// CLI reference types
let TypeNullnessSemantics_NullIsExtraValue = 1
// F# types with [<UseNullAsTrueValue>]
let TypeNullnessSemantics_NullTrueValue = 2
// F# record, union, tuple, function types
let TypeNullnessSemantics_NullNotLiked = 3
// structs
let TypeNullnessSemantics_NullNever = 4
// duplicated from above since we're using integers in this section
let CompilationRepresentationFlags_PermitNull = 8
let getTypeInfo (ty:Type) =
if ty.IsValueType
then TypeNullnessSemantics_NullNever else
let mappingAttrs = ty.GetCustomAttributes(typeof<CompilationMappingAttribute>, false)
if mappingAttrs.Length = 0
then TypeNullnessSemantics_NullIsExtraValue
elif ty.Equals(typeof<unit>) then
TypeNullnessSemantics_NullTrueValue
elif typeof<Delegate>.IsAssignableFrom(ty) then
TypeNullnessSemantics_NullIsExtraValue
elif ty.GetCustomAttributes(typeof<AllowNullLiteralAttribute>, false).Length > 0 then
TypeNullnessSemantics_NullIsExtraValue
else
let reprAttrs = ty.GetCustomAttributes(typeof<CompilationRepresentationAttribute>, false)
if reprAttrs.Length = 0 then
TypeNullnessSemantics_NullNotLiked
else
let reprAttr = get reprAttrs 0
let reprAttr = (# "unbox.any !0" type (CompilationRepresentationAttribute) reprAttr : CompilationRepresentationAttribute #)
if (# "and" reprAttr.Flags CompilationRepresentationFlags_PermitNull : int #) = 0
then TypeNullnessSemantics_NullNotLiked
else TypeNullnessSemantics_NullTrueValue
[<CodeAnalysis.SuppressMessage("Microsoft.Performance","CA1812:AvoidUninstantiatedInternalClasses")>]
type TypeInfo<'T>() =
// Compute an on-demand per-instantiation static field
static let info = getTypeInfo typeof<'T>
// Publish the results of that compuation
static member TypeInfo = info
// Note: cheap nullness test for generic value:
// IL_0000: ldarg.1
// IL_0001: box !TKey
// IL_0006: brtrue.s IL_000e
// worst case: nothing known about source or destination
let UnboxGeneric<'T>(x:obj) =
if notnullPrim(x) or TypeInfo<'T>.TypeInfo <> TypeNullnessSemantics_NullNotLiked then
unboxPrim<'T>(x)
else
//System.Console.WriteLine("UnboxGeneric, x = {0}, 'T = {1}", x, typeof<'T>)
raise (System.NullReferenceException())
// better: source is NOT TypeNullnessSemantics_NullNotLiked
let inline UnboxFast<'T>(x:obj) =
// assert not(TypeInfo<'T>.TypeInfo = TypeNullnessSemantics_NullNotLiked)
unboxPrim<'T>(x)
// worst case: nothing known about source or destination
let TypeTestGeneric<'T>(x:obj) =
if notnullPrim(isinstPrim<'T>(x)) then true
elif notnullPrim(x) then false
else (TypeInfo<'T>.TypeInfo = TypeNullnessSemantics_NullTrueValue)
// quick entry: source is NOT TypeNullnessSemantics_NullTrueValue
let inline TypeTestFast<'T>(x:obj) =
//assert not(TypeInfo<'T>.TypeInfo = TypeNullnessSemantics_NullTrueValue)
notnullPrim(isinstPrim<'T>(x))
let Dispose<'T when 'T :> System.IDisposable >(x:'T) =
match box x with
| null -> ()
| _ -> x.Dispose()
let FailInit() : unit = raise (System.InvalidOperationException(SR.GetString(SR.checkInit)))
let FailStaticInit() : unit = raise (System.InvalidOperationException(SR.GetString(SR.checkStaticInit)))
let CheckThis (x : 'T when 'T : not struct) =
match box x with
| null -> raise (System.InvalidOperationException(SR.GetString(SR.checkInit)))
| _ -> x
let inline MakeDecimal lo med hi isNegative scale = new System.Decimal(lo,med,hi,isNegative,scale)
let inline GetString (s: string) (n:int) = s.Chars(n)
let inline CreateInstance<'T when 'T : (new : unit -> 'T) >() =
(System.Activator.CreateInstance() : 'T)
let inline GetArray (arr: 'T array) (n:int) = (# "ldelem.any !0" type ('T) arr n : 'T #)
let inline SetArray (arr: 'T array) (n:int) (x:'T) = (# "stelem.any !0" type ('T) arr n x #)
let inline GetArraySub arr (start:int) (len:int) =
let len = if len < 0 then 0 else len
let dst = zeroCreate len
for i = 0 to len - 1 do
SetArray dst i (GetArray arr (start + i))
dst
let inline SetArraySub arr (start:int) (len:int) (src:_[]) =
for i = 0 to len - 1 do
SetArray arr (start+i) (GetArray src i)
let inline GetArray2D (arr: 'T[,]) (n1:int) (n2:int) = (# "ldelem.multi 2 !0" type ('T) arr n1 n2 : 'T #)
let inline SetArray2D (arr: 'T[,]) (n1:int) (n2:int) (x:'T) = (# "stelem.multi 2 !0" type ('T) arr n1 n2 x #)
let inline GetArray2DLength1 (arr: 'T[,]) = (# "ldlen.multi 2 0" arr : int #)
let inline GetArray2DLength2 (arr: 'T[,]) = (# "ldlen.multi 2 1" arr : int #)
let inline Array2DZeroCreate (n:int) (m:int) = (# "newarr.multi 2 !0" type ('T) n m : 'T[,] #)
let GetArray2DSub (src: 'T[,]) src1 src2 len1 len2 =
let len1 = (if len1 < 0 then 0 else len1)
let len2 = (if len2 < 0 then 0 else len2)
let dst = Array2DZeroCreate len1 len2
for i = 0 to len1 - 1 do
for j = 0 to len2 - 1 do
SetArray2D dst i j (GetArray2D src (src1 + i) (src2 + j))
dst
let SetArray2DSub (dst: 'T[,]) src1 src2 len1 len2 src =
for i = 0 to len1 - 1 do
for j = 0 to len2 - 1 do
SetArray2D dst (src1+i) (src2+j) (GetArray2D src i j)
let inline GetArray3D (arr: 'T[,,]) (n1:int) (n2:int) (n3:int) = (# "ldelem.multi 3 !0" type ('T) arr n1 n2 n3 : 'T #)
let inline SetArray3D (arr: 'T[,,]) (n1:int) (n2:int) (n3:int) (x:'T) = (# "stelem.multi 3 !0" type ('T) arr n1 n2 n3 x #)
let inline GetArray3DLength1 (arr: 'T[,,]) = (# "ldlen.multi 3 0" arr : int #)
let inline GetArray3DLength2 (arr: 'T[,,]) = (# "ldlen.multi 3 1" arr : int #)
let inline GetArray3DLength3 (arr: 'T[,,]) = (# "ldlen.multi 3 2" arr : int #)
let inline Array3DZeroCreate (n1:int) (n2:int) (n3:int) = (# "newarr.multi 3 !0" type ('T) n1 n2 n3 : 'T[,,] #)
let GetArray3DSub (src: 'T[,,]) src1 src2 src3 len1 len2 len3 =
let len1 = (if len1 < 0 then 0 else len1)
let len2 = (if len2 < 0 then 0 else len2)
let len3 = (if len3 < 0 then 0 else len3)
let dst = Array3DZeroCreate len1 len2 len3
for i = 0 to len1 - 1 do
for j = 0 to len2 - 1 do
for k = 0 to len3 - 1 do
SetArray3D dst i j k (GetArray3D src (src1+i) (src2+j) (src3+k))
dst
let SetArray3DSub (dst: 'T[,,]) src1 src2 src3 len1 len2 len3 src =
for i = 0 to len1 - 1 do
for j = 0 to len2 - 1 do
for k = 0 to len3 - 1 do
SetArray3D dst (src1+i) (src2+j) (src3+k) (GetArray3D src i j k)
let inline GetArray4D (arr: 'T[,,,]) (n1:int) (n2:int) (n3:int) (n4:int) = (# "ldelem.multi 4 !0" type ('T) arr n1 n2 n3 n4 : 'T #)
let inline SetArray4D (arr: 'T[,,,]) (n1:int) (n2:int) (n3:int) (n4:int) (x:'T) = (# "stelem.multi 4 !0" type ('T) arr n1 n2 n3 n4 x #)
let inline Array4DLength1 (arr: 'T[,,,]) = (# "ldlen.multi 4 0" arr : int #)
let inline Array4DLength2 (arr: 'T[,,,]) = (# "ldlen.multi 4 1" arr : int #)
let inline Array4DLength3 (arr: 'T[,,,]) = (# "ldlen.multi 4 2" arr : int #)
let inline Array4DLength4 (arr: 'T[,,,]) = (# "ldlen.multi 4 3" arr : int #)
let inline Array4DZeroCreate (n1:int) (n2:int) (n3:int) (n4:int) = (# "newarr.multi 4 !0" type ('T) n1 n2 n3 n4 : 'T[,,,] #)
let GetArray4DSub (src: 'T[,,,]) src1 src2 src3 src4 len1 len2 len3 len4 =
let len1 = (if len1 < 0 then 0 else len1)
let len2 = (if len2 < 0 then 0 else len2)
let len3 = (if len3 < 0 then 0 else len3)
let len4 = (if len4 < 0 then 0 else len4)
let dst = Array4DZeroCreate len1 len2 len3 len4
for i = 0 to len1 - 1 do
for j = 0 to len2 - 1 do
for k = 0 to len3 - 1 do
for m = 0 to len4 - 1 do
SetArray4D dst i j k m (GetArray4D src (src1+i) (src2+j) (src3+k) (src4+m))
dst
let SetArray4DSub (dst: 'T[,,,]) src1 src2 src3 src4 len1 len2 len3 len4 src =
for i = 0 to len1 - 1 do
for j = 0 to len2 - 1 do
for k = 0 to len3 - 1 do
for m = 0 to len4 - 1 do
SetArray4D dst (src1+i) (src2+j) (src3+k) (src4+m) (GetArray4D src i j k m)
let inline anyToString nullStr x =
match box x with
| null -> nullStr
| :? System.IFormattable as f -> f.ToString(null,System.Globalization.CultureInfo.InvariantCulture)
| obj -> obj.ToString()
let anyToStringShowingNull x = anyToString "null" x
module HashCompare =
open System.Reflection
open System.Linq.Expressions
open System.Runtime.CompilerServices
//-------------------------------------------------------------------------
// LangaugePrimitives.HashCompare: Physical Equality
//-------------------------------------------------------------------------
// NOTE: compiler/optimizer is aware of this function and optimizes calls to it in many situations
// where it is known that PhysicalEqualityObj is identical to reference comparison
let PhysicalEqualityIntrinsic (x:'T) (y:'T) : bool when 'T : not struct =
objEq (box x) (box y)
let inline PhysicalEqualityFast (x:'T) (y:'T) : bool when 'T : not struct =
PhysicalEqualityIntrinsic x y
let PhysicalHashIntrinsic (x: 'T) : int when 'T : not struct =
#if FX_NO_GET_HASH_CODE_HELPER
(box x).GetHashCode()
#else
System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(box x)
#endif
let inline PhysicalHashFast (x: 'T) =
PhysicalHashIntrinsic x
//-------------------------------------------------------------------------
// LangaugePrimitives.HashCompare: Comparison
//
// Bi-modal generic comparison helper implementation.
//
// The comparison implementation is run in either Equivalence Relation or Partial
// Equivalence Relation (PER) mode which governs what happens when NaNs are compared.
//
// Some representations chosen by F# are legitimately allowed to be null, e.g. the None value.
// However, null values don't support the polymorphic virtual comparison operation CompareTo
// so the test for nullness must be made on the caller side.
//-------------------------------------------------------------------------
let FailGenericComparison (obj: obj) =
raise (new System.ArgumentException(SR.GetString1(SR.genericCompareFail1, obj.GetType().ToString())))
/// This type has two instances - fsComparerER and fsComparerThrow.
/// - fsComparerER = ER semantics = no throw on NaN comparison = new GenericComparer(false) = GenericComparer = GenericComparison
/// - fsComparerPER = PER semantics = local throw on NaN comparison = new GenericComparer(true) = LessThan/GreaterThan etc.
type ComparerType =
| ER = 0
| PER_lt = 1
| PER_gt = 2
type GenericComparer(comparerType:ComparerType) =
interface System.Collections.IComparer
member c.ComparerType = comparerType
let getPERNaNCompareToResult (comp:GenericComparer) =
match comp.ComparerType with
| ComparerType.PER_gt -> -2
| ComparerType.PER_lt -> 2
| _ -> raise (Exception "Invalid logic")
/// Implements generic comparison between two objects. This corresponds to the pseudo-code in the F#
/// specification. The treatment of NaNs is governed by "comp".
let rec GenericCompare (comp:GenericComparer) (xobj:obj,yobj:obj) =
(*if objEq xobj yobj then 0 else *)
match xobj,yobj with
| null,null -> 0
| null,_ -> -1
| _,null -> 1
// Use Ordinal comparison for strings
| (:? string as x),(:? string as y) -> System.String.CompareOrdinal(x, y)
// Permit structural comparison on arrays
| (:? System.Array as arr1),_ ->
match arr1,yobj with
// Fast path
| (:? (obj[]) as arr1), (:? (obj[]) as arr2) -> GenericComparisonObjArrayWithComparer comp arr1 arr2
// Fast path
| (:? (byte[]) as arr1), (:? (byte[]) as arr2) -> GenericComparisonByteArray arr1 arr2
| _ , (:? System.Array as arr2) -> GenericComparisonArbArrayWithComparer comp arr1 arr2
| _ -> FailGenericComparison xobj
// Check for IStructuralComparable
| (:? IStructuralComparable as x),_ ->
x.CompareTo(yobj,comp)
// Check for IComparable
| (:? System.IComparable as x),_ ->
if comp.ComparerType.Equals ComparerType.ER then
x.CompareTo yobj
else
match xobj, yobj with
| (:? float as x), (:? float as y) ->
if System.Double.IsNaN x || System.Double.IsNaN y
then getPERNaNCompareToResult comp