forked from gusty/FsPad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FsPad.Output.fsx
2745 lines (2235 loc) · 91.2 KB
/
FsPad.Output.fsx
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
open System
open System.Collections.Generic
open System.Runtime.Serialization
open System.Reflection
open Microsoft.FSharp.Reflection
//------------------------------------
// Section: TypeShape core definitions
/// Provides a simple breakdown of basic kinds of types.
/// Used for easier extraction of type shapes in the active pattern implementations.
[<NoEquality; NoComparison>]
type TypeShapeInfo =
| Basic of Type
| Enum of enumTy:Type * underlying:Type
| Array of element:Type * rank:int
| Generic of definition:Type * args:Type []
/// Used to extract the type variable contained in a specific shape
type ITypeShapeVisitor<'R> =
abstract Visit<'T> : unit -> 'R
/// Encapsulates a type variable that can be accessed using type shape visitors
[<AbstractClass>]
type TypeShape =
[<CompilerMessage("TypeShape constructor should not be consumed.", 4224)>]
internal new () = { }
abstract Type : Type
abstract ShapeInfo : TypeShapeInfo
abstract Accept : ITypeShapeVisitor<'R> -> 'R
override s.ToString() = sprintf "TypeShape [%O]" s.Type
/// Encapsulates a type variable that can be accessed using type shape visitors
[<Sealed>]
type TypeShape<'T> () =
inherit TypeShape()
static let shapeInfo =
let t = typeof<'T>
if t.IsEnum then
Enum(t, Enum.GetUnderlyingType t)
elif t.IsArray then
Array(t.GetElementType(), t.GetArrayRank())
elif t.IsGenericType then
Generic(t.GetGenericTypeDefinition(), t.GetGenericArguments())
else
Basic t
override __.Type = typeof<'T>
override __.ShapeInfo = shapeInfo
override __.Accept v = v.Visit<'T> ()
override __.Equals o = o :? TypeShape<'T>
override __.GetHashCode() = hash typeof<'T>
exception UnsupportedShape of Type:Type
with
override __.Message = sprintf "Unsupported TypeShape '%O'" __.Type
[<AutoOpen>]
module private TypeShapeImpl =
let fsharpCoreRuntimeVersion =
typeof<unit>.Assembly.GetName().Version
let fsharpCore41Version = Version(4,4,1,0)
let allMembers =
BindingFlags.NonPublic ||| BindingFlags.Public |||
BindingFlags.Instance ||| BindingFlags.Static |||
BindingFlags.FlattenHierarchy
let allInstanceMembers =
BindingFlags.NonPublic ||| BindingFlags.Public ||| BindingFlags.Instance
type MemberInfo with
member inline m.ContainsAttr<'Attr when 'Attr :> Attribute> (inheritAttr) =
m.GetCustomAttributes(inheritAttr)
|> Array.exists (function :? 'Attr -> true | _ -> false)
member inline m.TryGetAttribute<'Attr when 'Attr :> Attribute> (inheritAttr) =
m.GetCustomAttributes(inheritAttr)
|> Array.tryPick (function :? 'Attr as attr -> Some attr | _ -> None)
let activateGeneric (templateTy:Type) (typeArgs : Type[]) (args:obj[]) =
let templateTy =
if typeArgs.Length = 0 then templateTy
elif not templateTy.IsGenericType then invalidArg (string templateTy) "not generic."
elif not templateTy.IsGenericTypeDefinition then
templateTy.GetGenericTypeDefinition().MakeGenericType typeArgs
else
templateTy.MakeGenericType typeArgs
let ctypes = args |> Array.map (fun o -> o.GetType())
let ctor = templateTy.GetConstructor(allMembers, null, CallingConventions.Standard, ctypes, [||])
ctor.Invoke args
/// correctly resolves if type is assignable to interface
let rec isInterfaceAssignableFrom (iface : Type) (ty : Type) =
let proj (t : Type) = t.Assembly, t.Namespace, t.Name, t.MetadataToken
if iface = ty then true
elif ty.GetInterfaces() |> Array.exists(fun if0 -> proj if0 = proj iface) then true
else
match ty.BaseType with
| null -> false
| bt -> isInterfaceAssignableFrom iface bt
let private canon = Type.GetType "System.__Canon"
let private genShapeTy = typedefof<TypeShape<_>>
let resolveTypeShape(typ : Type) =
if typ = null then raise <| ArgumentNullException("TypeShape: System.Type cannot be null.")
if typ.IsGenericTypeDefinition then raise <| UnsupportedShape typ
elif typ.IsGenericParameter then raise <| UnsupportedShape typ
elif typ = canon then raise <| UnsupportedShape typ
elif typ.IsByRef || typ.IsPointer then raise <| UnsupportedShape typ
else
let gt = genShapeTy.MakeGenericType [|typ|]
Activator.CreateInstance gt :?> TypeShape
type Activator with
/// Generic edition of the activator method which support type parameters and private types
static member CreateInstanceGeneric<'Template>(?typeArgs : Type[], ?args:obj[]) : obj =
let typeArgs = defaultArg typeArgs [||]
let args = defaultArg args [||]
activateGeneric typeof<'Template> typeArgs args
type Type with
/// Correctly resolves if type is assignable to interface
member iface.IsInterfaceAssignableFrom(ty : Type) : bool =
isInterfaceAssignableFrom iface ty
type TypeShape with
/// <summary>
/// Creates a type shape instance for given type
/// </summary>
/// <param name="typ">System.Type to be resolved.</param>
static member Create(typ : Type) : TypeShape = resolveTypeShape typ
/// <summary>
/// Creates a type shape instance from the underlying
/// type of a given value.
/// </summary>
/// <param name="obj">Non-null value to extract shape data from.</param>
static member FromValue(obj : obj) : TypeShape =
match obj with
| null -> raise <| ArgumentNullException()
| obj -> resolveTypeShape (obj.GetType())
/// <summary>
/// Creates a type shape instance for given type
/// </summary>
static member Create<'T>() : TypeShape<'T> = new TypeShape<'T>()
/// Creates a type shape instance for given type
let shapeof<'T> = TypeShape.Create<'T>() :> TypeShape
/// Typed variation of the shapeof operator
let tshapeof<'T> = TypeShape.Create<'T>()
//------------------------
// Section: Core BCL types
// Enum types
type IEnumVisitor<'R> =
abstract Visit<'Enum, 'Underlying when 'Enum : enum<'Underlying>
and 'Enum : struct
and 'Enum :> ValueType
and 'Enum : (new : unit -> 'Enum)> : unit -> 'R
type IShapeEnum =
abstract Underlying : TypeShape
abstract Accept : IEnumVisitor<'R> -> 'R
type private ShapeEnum<'Enum, 'Underlying when 'Enum : enum<'Underlying>
and 'Enum : struct
and 'Enum :> ValueType
and 'Enum : (new : unit -> 'Enum)>() =
interface IShapeEnum with
member __.Underlying = shapeof<'Underlying>
member __.Accept v = v.Visit<'Enum, 'Underlying> ()
// Nullable types
type INullableVisitor<'R> =
abstract Visit<'T when 'T : (new : unit -> 'T) and 'T :> ValueType and 'T : struct> : unit -> 'R
type IShapeNullable =
abstract Element : TypeShape
abstract Accept : INullableVisitor<'R> -> 'R
type private ShapeNullable<'T when 'T : (new : unit -> 'T) and 'T :> ValueType and 'T : struct> () =
interface IShapeNullable with
member __.Element = shapeof<'T>
member __.Accept v = v.Visit<'T> ()
// Default Constructor types
type IDefaultConstructorVisitor<'R> =
abstract Visit<'T when 'T : (new : unit -> 'T)> : unit -> 'R
type IShapeDefaultConstructor =
abstract Accept : IDefaultConstructorVisitor<'R> -> 'R
type private ShapeDefaultConstructor<'T when 'T : (new : unit -> 'T)>() =
interface IShapeDefaultConstructor with
member __.Accept v = v.Visit<'T>()
// Equality Types
type IEqualityVisitor<'R> =
abstract Visit<'T when 'T : equality> : unit -> 'R
type IShapeEquality =
abstract Accept : IEqualityVisitor<'R> -> 'R
type private ShapeEquality<'T when 'T : equality>() =
interface IShapeEquality with
member __.Accept v = v.Visit<'T>()
// Comparison Types
type IComparisonVisitor<'R> =
abstract Visit<'T when 'T : comparison> : unit -> 'R
type IShapeComparison =
abstract Accept : IComparisonVisitor<'R> -> 'R
type private ShapeComparison<'T when 'T : comparison>() =
interface IShapeComparison with
member __.Accept v = v.Visit<'T>()
// Struct Types
type IStructVisitor<'R> =
abstract Visit<'T when 'T : struct> : unit -> 'R
type IShapeStruct =
abstract Accept : IStructVisitor<'R> -> 'R
type private ShapeStruct<'T when 'T : struct>() =
interface IShapeStruct with
member __.Accept v = v.Visit<'T>()
// Reference Types
type INotStructVisitor<'R> =
abstract Visit<'T when 'T : not struct and 'T : null> : unit -> 'R
type IShapeNotStruct =
abstract Accept : INotStructVisitor<'R> -> 'R
type private ShapeNotStruct<'T when 'T : not struct and 'T : null>() =
interface IShapeNotStruct with
member __.Accept v = v.Visit<'T>()
// Delegates
type IDelegateVisitor<'R> =
abstract Visit<'Delegate when 'Delegate :> Delegate> : unit -> 'R
type IShapeDelegate =
abstract Accept : IDelegateVisitor<'R> -> 'R
type private ShapeDelegate<'Delegate when 'Delegate :> Delegate>() =
interface IShapeDelegate with
member __.Accept v = v.Visit<'Delegate>()
// System.Tuple`1
type ITuple1Visitor<'R> =
abstract Visit<'T> : unit -> 'R
type IShapeTuple1 =
abstract Item1 : TypeShape
abstract Accept : ITuple1Visitor<'R> -> 'R
type private ShapeTuple1<'T> () =
interface IShapeTuple1 with
member __.Item1 = shapeof<'T>
member __.Accept v = v.Visit<'T> ()
// System.Tuple`2
type ITuple2Visitor<'R> =
abstract Visit<'T1, 'T2> : unit -> 'R
type IShapeTuple2 =
abstract Item1 : TypeShape
abstract Item2 : TypeShape
abstract Accept : ITuple2Visitor<'R> -> 'R
type private ShapeTuple2<'T1, 'T2> () =
interface IShapeTuple2 with
member __.Item1 = shapeof<'T1>
member __.Item2 = shapeof<'T2>
member __.Accept v = v.Visit<'T1,'T2> ()
// System.Tuple`3
type ITuple3Visitor<'R> =
abstract Visit<'T1, 'T2, 'T3> : unit -> 'R
type IShapeTuple3 =
abstract Item1 : TypeShape
abstract Item2 : TypeShape
abstract Item3 : TypeShape
abstract Accept : ITuple3Visitor<'R> -> 'R
type private ShapeTuple3<'T1, 'T2, 'T3> () =
interface IShapeTuple3 with
member __.Item1 = shapeof<'T1>
member __.Item2 = shapeof<'T2>
member __.Item3 = shapeof<'T3>
member __.Accept v = v.Visit<'T1, 'T2, 'T3> ()
// System.Tuple`4
type ITuple4Visitor<'R> =
abstract Visit<'T1, 'T2, 'T3, 'T4> : unit -> 'R
type IShapeTuple4 =
abstract Item1 : TypeShape
abstract Item2 : TypeShape
abstract Item3 : TypeShape
abstract Item4 : TypeShape
abstract Accept : ITuple4Visitor<'R> -> 'R
type private ShapeTuple4<'T1, 'T2, 'T3, 'T4> () =
interface IShapeTuple4 with
member __.Item1 = shapeof<'T1>
member __.Item2 = shapeof<'T2>
member __.Item3 = shapeof<'T3>
member __.Item4 = shapeof<'T4>
member __.Accept v = v.Visit<'T1, 'T2, 'T3, 'T4> ()
// System.Tuple`5
type ITuple5Visitor<'R> =
abstract Visit<'T1, 'T2, 'T3, 'T4, 'T5> : unit -> 'R
type IShapeTuple5 =
abstract Item1 : TypeShape
abstract Item2 : TypeShape
abstract Item3 : TypeShape
abstract Item4 : TypeShape
abstract Item5 : TypeShape
abstract Accept : ITuple5Visitor<'R> -> 'R
type private ShapeTuple5<'T1, 'T2, 'T3, 'T4, 'T5> () =
interface IShapeTuple5 with
member __.Item1 = shapeof<'T1>
member __.Item2 = shapeof<'T2>
member __.Item3 = shapeof<'T3>
member __.Item4 = shapeof<'T4>
member __.Item5 = shapeof<'T5>
member __.Accept v = v.Visit<'T1, 'T2, 'T3, 'T4, 'T5> ()
// System.Tuple`6
type ITuple6Visitor<'R> =
abstract Visit<'T1, 'T2, 'T3, 'T4, 'T5, 'T6> : unit -> 'R
type IShapeTuple6 =
abstract Item1 : TypeShape
abstract Item2 : TypeShape
abstract Item3 : TypeShape
abstract Item4 : TypeShape
abstract Item5 : TypeShape
abstract Item6 : TypeShape
abstract Accept : ITuple6Visitor<'R> -> 'R
type private ShapeTuple6<'T1, 'T2, 'T3, 'T4, 'T5, 'T6> () =
interface IShapeTuple6 with
member __.Item1 = shapeof<'T1>
member __.Item2 = shapeof<'T2>
member __.Item3 = shapeof<'T3>
member __.Item4 = shapeof<'T4>
member __.Item5 = shapeof<'T5>
member __.Item6 = shapeof<'T6>
member __.Accept v = v.Visit<'T1, 'T2, 'T3, 'T4, 'T5, 'T6> ()
// System.Tuple`7
type ITuple7Visitor<'R> =
abstract Visit<'T1, 'T2, 'T3, 'T4, 'T5, 'T6, 'T7> : unit -> 'R
type IShapeTuple7 =
abstract Item1 : TypeShape
abstract Item2 : TypeShape
abstract Item3 : TypeShape
abstract Item4 : TypeShape
abstract Item5 : TypeShape
abstract Item6 : TypeShape
abstract Item7 : TypeShape
abstract Accept : ITuple7Visitor<'R> -> 'R
type private ShapeTuple7<'T1, 'T2, 'T3, 'T4, 'T5, 'T6, 'T7> () =
interface IShapeTuple7 with
member __.Item1 = shapeof<'T1>
member __.Item2 = shapeof<'T2>
member __.Item3 = shapeof<'T3>
member __.Item4 = shapeof<'T4>
member __.Item5 = shapeof<'T5>
member __.Item6 = shapeof<'T6>
member __.Item7 = shapeof<'T7>
member __.Accept v = v.Visit<'T1, 'T2, 'T3, 'T4, 'T5, 'T6, 'T7> ()
// System.Tuple`8
type ITuple8Visitor<'R> =
abstract Visit<'T1, 'T2, 'T3, 'T4, 'T5, 'T6, 'T7, 'TRest> : unit -> 'R
type IShapeTuple8 =
abstract Item1 : TypeShape
abstract Item2 : TypeShape
abstract Item3 : TypeShape
abstract Item4 : TypeShape
abstract Item5 : TypeShape
abstract Item6 : TypeShape
abstract Item7 : TypeShape
abstract Rest : TypeShape
abstract Accept : ITuple8Visitor<'R> -> 'R
type private ShapeTuple8<'T1, 'T2, 'T3, 'T4, 'T5, 'T6, 'T7, 'TRest> () =
interface IShapeTuple8 with
member __.Item1 = shapeof<'T1>
member __.Item2 = shapeof<'T2>
member __.Item3 = shapeof<'T3>
member __.Item4 = shapeof<'T4>
member __.Item5 = shapeof<'T5>
member __.Item6 = shapeof<'T6>
member __.Item7 = shapeof<'T7>
member __.Rest = shapeof<'TRest>
member __.Accept v = v.Visit<'T1, 'T2, 'T3, 'T4, 'T5, 'T6, 'T7, 'TRest> ()
// F# functions
type IFSharpFuncVisitor<'R> =
abstract Visit<'Domain, 'CoDomain> : unit -> 'R
type IShapeFSharpFunc =
abstract Domain : TypeShape
abstract CoDomain : TypeShape
abstract Accept : IFSharpFuncVisitor<'R> -> 'R
type private ShapeFSharpFunc<'Domain, 'CoDomain> () =
interface IShapeFSharpFunc with
member __.Domain = shapeof<'Domain>
member __.CoDomain = shapeof<'CoDomain>
member __.Accept v = v.Visit<'Domain, 'CoDomain> ()
// System.Exception
type IExceptionVisitor<'R> =
abstract Visit<'exn when 'exn :> exn and 'exn : not struct and 'exn : null> : unit -> 'R
type IShapeException =
abstract IsFSharpException : bool
abstract Accept : IExceptionVisitor<'R> -> 'R
type private ShapeException<'exn when 'exn :> exn and 'exn : not struct and 'exn : null> (isFSharpExn : bool) =
interface IShapeException with
member __.IsFSharpException = isFSharpExn
member __.Accept v = v.Visit<'exn> ()
//-----------------------------------
// Section: Collections & IEnumerable
// IEnumerable
type IEnumerableVisitor<'R> =
abstract Visit<'Enum, 'T when 'Enum :> seq<'T>> : unit -> 'R
type IShapeEnumerable =
abstract Element : TypeShape
abstract Accept : IEnumerableVisitor<'R> -> 'R
type private ShapeEnumerable<'Enum, 'T when 'Enum :> seq<'T>> () =
interface IShapeEnumerable with
member __.Element = shapeof<'T>
member __.Accept v = v.Visit<'Enum, 'T> ()
// Collection
type ICollectionVisitor<'R> =
abstract Visit<'Collection, 'T when 'Collection :> ICollection<'T>> : unit -> 'R
type IShapeCollection =
abstract Element : TypeShape
abstract Accept : ICollectionVisitor<'R> -> 'R
type private ShapeCollection<'Collection, 'T when 'Collection :> ICollection<'T>> () =
interface IShapeCollection with
member __.Element = shapeof<'T>
member __.Accept v = v.Visit<'Collection, 'T> ()
// KeyValuePair
type IKeyValuePairVisitor<'R> =
abstract Visit<'K, 'V> : unit -> 'R
type IShapeKeyValuePair =
abstract Key : TypeShape
abstract Value : TypeShape
abstract Accept : IKeyValuePairVisitor<'R> -> 'R
type private ShapeKeyValuePair<'K,'V> () =
interface IShapeKeyValuePair with
member __.Key = shapeof<'K>
member __.Value = shapeof<'V>
member __.Accept v = v.Visit<'K, 'V> ()
// System.Array
type IArrayVisitor<'R> =
abstract Visit<'T> : rank:int -> 'R
type IShapeArray =
/// Gets the rank of the array type shape
abstract Rank : int
abstract Element : TypeShape
abstract Accept : IArrayVisitor<'R> -> 'R
type private ShapeArray<'T>(rank : int) =
/// Gets the rank of the array type shape
member __.Rank = rank
interface IShapeArray with
member __.Rank = rank
member __.Element = shapeof<'T>
member __.Accept v = v.Visit<'T> rank
type ISystemArrayVisitor<'R> =
abstract Visit<'Array when 'Array :> System.Array> : unit -> 'R
type IShapeSystemArray =
abstract Rank : int
abstract Element : TypeShape
abstract Accept : ISystemArrayVisitor<'R> -> 'R
type private ShapeSystemArray<'Array when 'Array :> System.Array>(elem : Type, rank : int) =
interface IShapeSystemArray with
member __.Rank = rank
member __.Element = TypeShape.Create elem
member __.Accept v = v.Visit<'Array> ()
// System.Collections.List
type IResizeArrayVisitor<'R> =
abstract Visit<'T> : unit -> 'R
type IShapeResizeArray =
abstract Element : TypeShape
abstract Accept : IResizeArrayVisitor<'R> -> 'R
type private ShapeResizeArray<'T> () =
interface IShapeResizeArray with
member __.Element = shapeof<'T>
member __.Accept v = v.Visit<'T> ()
// System.Collections.Dictionary
type IDictionaryVisitor<'R> =
abstract Visit<'K, 'V when 'K : equality> : unit -> 'R
type IShapeDictionary =
abstract Key : TypeShape
abstract Value : TypeShape
abstract Accept : IDictionaryVisitor<'R> -> 'R
type private ShapeDictionary<'K, 'V when 'K : equality> () =
interface IShapeDictionary with
member __.Key = shapeof<'K>
member __.Value = shapeof<'V>
member __.Accept v = v.Visit<'K, 'V> ()
// System.Collections.HashSet
type IHashSetVisitor<'R> =
abstract Visit<'T when 'T : equality> : unit -> 'R
type IShapeHashSet =
abstract Element : TypeShape
abstract Accept : IHashSetVisitor<'R> -> 'R
type private ShapeHashSet<'T when 'T : equality> () =
interface IShapeHashSet with
member __.Element = shapeof<'T>
member __.Accept v = v.Visit<'T> ()
// F# Set
type IFSharpSetVisitor<'R> =
abstract Visit<'T when 'T : comparison> : unit -> 'R
type IShapeFSharpSet =
abstract Element : TypeShape
abstract Accept : IFSharpSetVisitor<'R> -> 'R
type private ShapeFSharpSet<'T when 'T : comparison> () =
interface IShapeFSharpSet with
member __.Element = shapeof<'T>
member __.Accept v = v.Visit<'T> ()
// F# Map
type IFSharpMapVisitor<'R> =
abstract Visit<'K, 'V when 'K : comparison> : unit -> 'R
type IShapeFSharpMap =
abstract Key : TypeShape
abstract Value : TypeShape
abstract Accept : IFSharpMapVisitor<'R> -> 'R
type private ShapeFSharpMap<'K, 'V when 'K : comparison> () =
interface IShapeFSharpMap with
member __.Key = shapeof<'K>
member __.Value = shapeof<'V>
member __.Accept v = v.Visit<'K, 'V>()
// F# ref
type IShapeFSharpRef =
abstract Element : TypeShape
abstract Accept : IFSharpRefVisitor<'R> -> 'R
and IFSharpRefVisitor<'R> =
abstract Visit<'T> : unit -> 'R
type private ShapeFSharpRef<'T> () =
interface IShapeFSharpRef with
member __.Element = shapeof<'T>
member __.Accept v = v.Visit<'T> ()
// F# option
type IFSharpOptionVisitor<'R> =
abstract Visit<'T> : unit -> 'R
type IShapeFSharpOption =
abstract Element : TypeShape
abstract Accept : IFSharpOptionVisitor<'R> -> 'R
type private ShapeFSharpOption<'T> () =
interface IShapeFSharpOption with
member __.Element = shapeof<'T>
member __.Accept v = v.Visit<'T> ()
// F# List
type IFSharpListVisitor<'R> =
abstract Visit<'T> : unit -> 'R
type IShapeFSharpList =
abstract Element : TypeShape
abstract Accept : IFSharpListVisitor<'R> -> 'R
type private ShapeFSharpList<'T> () =
interface IShapeFSharpList with
member __.Element = shapeof<'T>
member __.Accept v = v.Visit<'T> ()
// F# Choice`2
type IFSharpChoice2Visitor<'R> =
abstract Visit<'T1,'T2> : unit -> 'R
type IShapeFSharpChoice2 =
abstract Type1 : TypeShape
abstract Type2 : TypeShape
abstract Accept : IFSharpChoice2Visitor<'R> -> 'R
type private ShapeFSharpChoice<'T1, 'T2> () =
interface IShapeFSharpChoice2 with
member __.Type1 = shapeof<'T1>
member __.Type2 = shapeof<'T2>
member __.Accept v = v.Visit<'T1,'T2>()
// F# Choice`3
type IFSharpChoice3Visitor<'R> =
abstract Visit<'T1,'T2,'T3> : unit -> 'R
type IShapeFSharpChoice3 =
abstract Type1 : TypeShape
abstract Type2 : TypeShape
abstract Type3 : TypeShape
abstract Accept : IFSharpChoice3Visitor<'R> -> 'R
type private ShapeFSharpChoice<'T1, 'T2, 'T3> () =
interface IShapeFSharpChoice3 with
member __.Type1 = shapeof<'T1>
member __.Type2 = shapeof<'T2>
member __.Type3 = shapeof<'T3>
member __.Accept v = v.Visit<'T1,'T2,'T3>()
// F# Choice`4
type IFSharpChoice4Visitor<'R> =
abstract Visit<'T1,'T2,'T3,'T4> : unit -> 'R
type IShapeFSharpChoice4 =
abstract Type1 : TypeShape
abstract Type2 : TypeShape
abstract Type3 : TypeShape
abstract Type4 : TypeShape
abstract Accept : IFSharpChoice4Visitor<'R> -> 'R
type private ShapeFSharpChoice<'T1, 'T2, 'T3, 'T4> () =
interface IShapeFSharpChoice4 with
member __.Type1 = shapeof<'T1>
member __.Type2 = shapeof<'T2>
member __.Type3 = shapeof<'T3>
member __.Type4 = shapeof<'T4>
member __.Accept v = v.Visit<'T1,'T2,'T3,'T4>()
// F# Choice`5
type IFSharpChoice5Visitor<'R> =
abstract Visit<'T1,'T2,'T3,'T4,'T5> : unit -> 'R
type IShapeFSharpChoice5 =
abstract Type1 : TypeShape
abstract Type2 : TypeShape
abstract Type3 : TypeShape
abstract Type4 : TypeShape
abstract Type5 : TypeShape
abstract Accept : IFSharpChoice5Visitor<'R> -> 'R
type private ShapeFSharpChoice<'T1, 'T2, 'T3, 'T4, 'T5> () =
interface IShapeFSharpChoice5 with
member __.Type1 = shapeof<'T1>
member __.Type2 = shapeof<'T2>
member __.Type3 = shapeof<'T3>
member __.Type4 = shapeof<'T4>
member __.Type5 = shapeof<'T5>
member __.Accept v = v.Visit<'T1,'T2,'T3,'T4,'T5>()
// F# Choice`6
type IFSharpChoice6Visitor<'R> =
abstract Visit<'T1,'T2,'T3,'T4,'T5,'T6> : unit -> 'R
type IShapeFSharpChoice6 =
abstract Type1 : TypeShape
abstract Type2 : TypeShape
abstract Type3 : TypeShape
abstract Type4 : TypeShape
abstract Type5 : TypeShape
abstract Type6 : TypeShape
abstract Accept : IFSharpChoice6Visitor<'R> -> 'R
type private ShapeFSharpChoice<'T1, 'T2, 'T3, 'T4, 'T5, 'T6> () =
interface IShapeFSharpChoice6 with
member __.Type1 = shapeof<'T1>
member __.Type2 = shapeof<'T2>
member __.Type3 = shapeof<'T3>
member __.Type4 = shapeof<'T4>
member __.Type5 = shapeof<'T5>
member __.Type6 = shapeof<'T6>
member __.Accept v = v.Visit<'T1,'T2,'T3,'T4,'T5,'T6>()
// F# Choice`7
type IFSharpChoice7Visitor<'R> =
abstract Visit<'T1,'T2,'T3,'T4,'T5,'T6,'T7> : unit -> 'R
type IShapeFSharpChoice7 =
abstract Type1 : TypeShape
abstract Type2 : TypeShape
abstract Type3 : TypeShape
abstract Type4 : TypeShape
abstract Type5 : TypeShape
abstract Type6 : TypeShape
abstract Type7 : TypeShape
abstract Accept : IFSharpChoice7Visitor<'R> -> 'R
type private ShapeFSharpChoice<'T1, 'T2, 'T3, 'T4, 'T5, 'T6, 'T7> () =
interface IShapeFSharpChoice7 with
member __.Type1 = shapeof<'T1>
member __.Type2 = shapeof<'T2>
member __.Type3 = shapeof<'T3>
member __.Type4 = shapeof<'T4>
member __.Type5 = shapeof<'T5>
member __.Type6 = shapeof<'T6>
member __.Type7 = shapeof<'T7>
member __.Accept v = v.Visit<'T1,'T2,'T3,'T4,'T5,'T6,'T7>()
//-----------------------------
// Section: Member-based Shapes
[<AutoOpen>]
module private MemberUtils =
let defaultOfUntyped (ty : Type) =
TypeShape.Create(ty).Accept {
new ITypeShapeVisitor<obj> with
member __.Visit<'T>() = Unchecked.defaultof<'T> :> obj
}
let inline invalidMember (memberInfo : MemberInfo) =
sprintf "TypeShape internal error: invalid MemberInfo '%O'" memberInfo
|> invalidOp
let isStructMember (path : MemberInfo[]) =
path |> Array.exists (fun m -> m.DeclaringType.IsValueType)
let isPublicMember (memberInfo : MemberInfo) =
match memberInfo with
| :? FieldInfo as f -> f.IsPublic
| :? PropertyInfo as p -> p.GetGetMethod(true).IsPublic
| _ -> invalidMember memberInfo
let isWriteableMember (path : MemberInfo[]) =
path
|> Array.forall (fun m ->
match m with
| :? FieldInfo -> true
| :? PropertyInfo as p -> p.CanWrite
| _ -> invalidMember m)
let inline getValue (obj:obj) (m:MemberInfo) =
match m with
| :? FieldInfo as f -> f.GetValue(obj)
| :? PropertyInfo as p -> p.GetValue(obj, null)
| _ -> invalidMember m
let inline setValue (obj:obj) (m:MemberInfo) (value:obj) =
match m with
| :? FieldInfo as f -> f.SetValue(obj, value)
| :? PropertyInfo as p -> p.SetValue(obj, value, null)
| _ -> invalidMember m
let inline project<'Record, 'Member> (path : MemberInfo[]) (value:'Record) =
let mutable obj = box value
for m in path do
obj <- getValue obj m
obj :?> 'Member
let inline inject<'Record, 'Member> (isStructMember : bool) (path : MemberInfo[])
(r : 'Record) (value : 'Member) =
let mutable obj = box r
let n = path.Length
if isStructMember then
let valueStack = Array.zeroCreate<obj> (n - 1)
for i = 0 to n - 2 do
valueStack.[i] <- obj
obj <- getValue obj path.[i]
setValue obj path.[n - 1] value
for i = n - 2 downto 0 do
let obj2 = valueStack.[i]
setValue obj2 path.[i] obj
obj <- obj2
obj :?> 'Record
else
for i = 0 to n - 2 do
obj <- getValue obj path.[i]
setValue obj path.[n - 1] value
r
//-------------------------
// Member Shape Definitions
/// Identifies an instance member that defines
/// a value in a class instance, typically a field or property
type IShapeMember =
/// Human-readable member identifier
abstract Label : string
/// The actual System.Reflection.MemberInfo corresponding to member
abstract MemberInfo : MemberInfo
/// Type of value stored by member
abstract Member : TypeShape
/// True iff member is contained within a struct
abstract IsStructMember : bool
/// True iff member is public
abstract IsPublic : bool
/// Identifies an instance member that defines
/// a value in a class instance, typically a field or property
type IShapeMember<'DeclaringType> =
inherit IShapeMember
abstract Accept : IMemberVisitor<'DeclaringType, 'R> -> 'R
/// Identifies an instance member that defines
/// a value in a class instance, typically a field or property
and ShapeMember<'DeclaringType, 'MemberType> internal (label : string, memberInfo : MemberInfo, path : MemberInfo[]) =
let isStructMember = isStructMember path
let isPublicMember = isPublicMember memberInfo
#if TYPESHAPE_EMIT
let projectFunc = emitProjection<'DeclaringType, 'MemberType> path
#endif
/// Human-readable member identifier
member __.Label = label
/// The actual System.Reflection.MemberInfo corresponding to member
member __.MemberInfo = memberInfo
/// True iff member is contained within a struct
member __.IsStructMember = isStructMember
/// True iff member is public
member __.IsPublic = isPublicMember
/// Projects an instance to member of given value
member __.Project (instance : 'DeclaringType) : 'MemberType =
#if TYPESHAPE_EMIT
projectFunc.Value.Invoke instance
#else
project path instance
#endif
#if TYPESHAPE_EXPR
/// Projects an instance to member of given value
member __.ProjectExpr (instance : Expr<'DeclaringType>) =
projectExpr<'DeclaringType, 'MemberType> path instance
#endif
interface IShapeMember<'DeclaringType> with
member s.Label = label
member s.Member = shapeof<'MemberType>
member s.MemberInfo = memberInfo
member s.IsStructMember = isStructMember
member s.IsPublic = isPublicMember
member s.Accept v = v.Visit s
and IMemberVisitor<'DeclaringType, 'R> =
abstract Visit<'MemberType> : ShapeMember<'DeclaringType, 'MemberType> -> 'R
//----------------------------
// Writable Member Definitions
/// Identifies an instance member that defines
/// a mutable value in a class instance, typically a field or property
type IShapeWriteMember<'Record> =
inherit IShapeMember<'Record>
abstract Accept : IWriteMemberVisitor<'Record,'R> -> 'R
/// Identifies an instance member that defines
/// a mutable value in a class instance, typically a field or property
and [<Sealed>] ShapeWriteMember<'DeclaringType, 'MemberType> private (label : string, memberInfo : MemberInfo, path : MemberInfo[]) =
inherit ShapeMember<'DeclaringType, 'MemberType>(label, memberInfo, path)
#if TYPESHAPE_EMIT
let injectFunc = emitInjection<'DeclaringType, 'MemberType> path
#else
let isStructMember = isStructMember path
#endif
/// Injects a value to member of given instance
member __.Inject (instance : 'DeclaringType) (field : 'MemberType) : 'DeclaringType =
#if TYPESHAPE_EMIT
injectFunc.Value.Invoke(instance, field)
#else
inject isStructMember path instance field
#endif
#if TYPESHAPE_EXPR
/// Injects a value to member of given instance
member __.InjectExpr (instance : Expr<'DeclaringType>) (field : Expr<'MemberType>) =
injectExpr path instance field
#endif
interface IShapeWriteMember<'DeclaringType> with
member s.Accept (v : IWriteMemberVisitor<'DeclaringType, 'R>) = v.Visit s
and IWriteMemberVisitor<'TRecord, 'R> =
abstract Visit<'Field> : ShapeWriteMember<'TRecord, 'Field> -> 'R
//-------------------------------
// Constructor Shapes
/// Identifies a constructor implementation shape
type IShapeConstructor =
/// Denotes whether constructor is public
abstract IsPublic : bool
/// Denotes the arity of the constructor arguments
abstract Arity : int
/// ConstructorInfo instance
abstract ConstructorInfo : ConstructorInfo
// A tuple type encoding all arguments passed to the constuctor
abstract Arguments : TypeShape
/// Identifies a constructor implementation shape
and IShapeConstructor<'DeclaringType> =
inherit IShapeConstructor
abstract Accept : IConstructorVisitor<'DeclaringType, 'R> -> 'R
/// Identifies a constructor implementation shape
and [<Sealed>] ShapeConstructor<'DeclaringType, 'CtorArgs> private (ctorInfo : ConstructorInfo, arity : int) =
let valueReader =