forked from nosami/visualfsharp
/
Symbols.fs
2666 lines (2168 loc) · 105 KB
/
Symbols.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. See License.txt in the project root for license information.
namespace rec FSharp.Compiler.SourceCodeServices
open System.Collections.Generic
open FSharp.Compiler
open FSharp.Compiler.AbstractIL.Internal.Library
open FSharp.Compiler.AbstractIL.IL
open FSharp.Compiler.AttributeChecking
open FSharp.Compiler.AccessibilityLogic
open FSharp.Compiler.CheckDeclarations
open FSharp.Compiler.CompilerImports
open FSharp.Compiler.Infos
open FSharp.Compiler.InfoReader
open FSharp.Compiler.Lib
open FSharp.Compiler.NameResolution
open FSharp.Compiler.Range
open FSharp.Compiler.SyntaxTree
open FSharp.Compiler.SyntaxTreeOps
open FSharp.Compiler.TypedTree
open FSharp.Compiler.TypedTreeBasics
open FSharp.Compiler.TcGlobals
open FSharp.Compiler.TypedTreeOps
open FSharp.Compiler.XmlDoc
open Internal.Utilities
type FSharpAccessibility(a:Accessibility, ?isProtected) =
let isProtected = defaultArg isProtected false
let isInternalCompPath x =
match x with
| CompPath(ILScopeRef.Local, []) -> true
| _ -> false
let (|Public|Internal|Private|) (TAccess p) =
match p with
| [] -> Public
| _ when List.forall isInternalCompPath p -> Internal
| _ -> Private
member _.IsPublic = not isProtected && match a with TAccess [] -> true | _ -> false
member _.IsPrivate = not isProtected && match a with Private -> true | _ -> false
member _.IsInternal = not isProtected && match a with Internal -> true | _ -> false
member _.IsProtected = isProtected
member internal _.Contents = a
override _.ToString() =
let (TAccess paths) = a
let mangledTextOfCompPath (CompPath(scoref, path)) = getNameOfScopeRef scoref + "/" + textOfPath (List.map fst path)
String.concat ";" (List.map mangledTextOfCompPath paths)
type SymbolEnv(g: TcGlobals, thisCcu: CcuThunk, thisCcuTyp: ModuleOrNamespaceType option, tcImports: TcImports, amap: Import.ImportMap, infoReader: InfoReader) =
let tcVal = CheckExpressions.LightweightTcValForUsingInBuildMethodCall g
new(g: TcGlobals, thisCcu: CcuThunk, thisCcuTyp: ModuleOrNamespaceType option, tcImports: TcImports) =
let amap = tcImports.GetImportMap()
let infoReader = InfoReader(g, amap)
SymbolEnv(g, thisCcu, thisCcuTyp, tcImports, amap, infoReader)
member _.g = g
member _.amap = amap
member _.thisCcu = thisCcu
member _.thisCcuTy = thisCcuTyp
member _.infoReader = infoReader
member _.tcImports = tcImports
member _.tcValF = tcVal
[<AutoOpen>]
module Impl =
let protect f =
ErrorLogger.protectAssemblyExplorationF
(fun (asmName, path) -> invalidOp (sprintf "The entity or value '%s' does not exist or is in an unresolved assembly. You may need to add a reference to assembly '%s'" path asmName))
f
let makeReadOnlyCollection (arr: seq<'T>) =
System.Collections.ObjectModel.ReadOnlyCollection<_>(Seq.toArray arr) :> IList<_>
let makeXmlDoc (doc: XmlDoc) =
makeReadOnlyCollection doc.UnprocessedLines
let makeElaboratedXmlDoc (doc: XmlDoc) =
makeReadOnlyCollection (doc.GetElaboratedXmlLines())
let rescopeEntity optViewedCcu (entity: Entity) =
match optViewedCcu with
| None -> mkLocalEntityRef entity
| Some viewedCcu ->
match tryRescopeEntity viewedCcu entity with
| ValueNone -> mkLocalEntityRef entity
| ValueSome eref -> eref
let entityIsUnresolved(entity:EntityRef) =
match entity with
| ERefNonLocal(NonLocalEntityRef(ccu, _)) ->
ccu.IsUnresolvedReference && entity.TryDeref.IsNone
| _ -> false
let checkEntityIsResolved(entity:EntityRef) =
if entityIsUnresolved entity then
let poorQualifiedName =
if entity.nlr.AssemblyName = "mscorlib" then
entity.nlr.DisplayName + ", mscorlib"
else
entity.nlr.DisplayName + ", " + entity.nlr.Ccu.AssemblyName
invalidOp (sprintf "The entity '%s' does not exist or is in an unresolved assembly." poorQualifiedName)
/// Checking accessibility that arise from different compilations needs more care - this is a duplicate of the F# compiler code for this case
let checkForCrossProjectAccessibility (ilg: ILGlobals) (thisCcu2:CcuThunk, ad2) (thisCcu1, taccess1) =
match ad2 with
| AccessibleFrom(cpaths2, _) ->
let nameOfScoRef (thisCcu:CcuThunk) scoref =
match scoref with
| ILScopeRef.Local -> thisCcu.AssemblyName
| ILScopeRef.Assembly aref -> aref.Name
| ILScopeRef.Module mref -> mref.Name
| ILScopeRef.PrimaryAssembly -> ilg.primaryAssemblyName
let canAccessCompPathFromCrossProject (CompPath(scoref1, cpath1)) (CompPath(scoref2, cpath2)) =
let rec loop p1 p2 =
match p1, p2 with
| (a1, k1) :: rest1, (a2, k2) :: rest2 -> (a1=a2) && (k1=k2) && loop rest1 rest2
| [], _ -> true
| _ -> false // cpath1 is longer
loop cpath1 cpath2 &&
nameOfScoRef thisCcu1 scoref1 = nameOfScoRef thisCcu2 scoref2
let canAccessFromCrossProject (TAccess x1) cpath2 = x1 |> List.forall (fun cpath1 -> canAccessCompPathFromCrossProject cpath1 cpath2)
cpaths2 |> List.exists (canAccessFromCrossProject taccess1)
| _ -> true // otherwise use the normal check
/// Convert an IL member accessibility into an F# accessibility
let getApproxFSharpAccessibilityOfMember (declaringEntity: EntityRef) (ilAccess: ILMemberAccess) =
match ilAccess with
| ILMemberAccess.CompilerControlled
| ILMemberAccess.FamilyAndAssembly
| ILMemberAccess.Assembly ->
taccessPrivate (CompPath(declaringEntity.CompilationPath.ILScopeRef, []))
| ILMemberAccess.Private ->
taccessPrivate declaringEntity.CompilationPath
// This is an approximation - the thing may actually be nested in a private class, in which case it is not actually "public"
| ILMemberAccess.Public
// This is an approximation - the thing is actually "protected", but F# accessibilities can't express "protected", so we report it as "public"
| ILMemberAccess.FamilyOrAssembly
| ILMemberAccess.Family ->
taccessPublic
/// Convert an IL type definition accessibility into an F# accessibility
let getApproxFSharpAccessibilityOfEntity (entity: EntityRef) =
match metadataOfTycon entity.Deref with
#if !NO_EXTENSIONTYPING
| ProvidedTypeMetadata _info ->
// This is an approximation - for generative type providers some type definitions can be private.
taccessPublic
#endif
| ILTypeMetadata (TILObjectReprData(_, _, td)) ->
match td.Access with
| ILTypeDefAccess.Public
| ILTypeDefAccess.Nested ILMemberAccess.Public -> taccessPublic
| ILTypeDefAccess.Private -> taccessPrivate (CompPath(entity.CompilationPath.ILScopeRef, []))
| ILTypeDefAccess.Nested nested -> getApproxFSharpAccessibilityOfMember entity nested
| FSharpOrArrayOrByrefOrTupleOrExnTypeMetadata ->
entity.Accessibility
let getLiteralValue = function
| Some lv ->
match lv with
| Const.Bool v -> Some(box v)
| Const.SByte v -> Some(box v)
| Const.Byte v -> Some(box v)
| Const.Int16 v -> Some(box v)
| Const.UInt16 v -> Some(box v)
| Const.Int32 v -> Some(box v)
| Const.UInt32 v -> Some(box v)
| Const.Int64 v -> Some(box v)
| Const.UInt64 v -> Some(box v)
| Const.IntPtr v -> Some(box v)
| Const.UIntPtr v -> Some(box v)
| Const.Single v -> Some(box v)
| Const.Double v -> Some(box v)
| Const.Char v -> Some(box v)
| Const.String v -> Some(box v)
| Const.Decimal v -> Some(box v)
| Const.Unit
| Const.Zero -> None
| None -> None
let getXmlDocSigForEntity (cenv: SymbolEnv) (ent:EntityRef)=
match SymbolHelpers.GetXmlDocSigOfEntityRef cenv.infoReader ent.Range ent with
| Some (_, docsig) -> docsig
| _ -> ""
type FSharpDisplayContext(denv: TcGlobals -> DisplayEnv) =
member x.Contents g = denv g
static member Empty = FSharpDisplayContext(fun g -> DisplayEnv.Empty g)
member x.WithShortTypeNames shortNames =
FSharpDisplayContext(fun g -> { denv g with shortTypeNames = shortNames })
// delay the realization of 'item' in case it is unresolved
type FSharpSymbol(cenv: SymbolEnv, item: (unit -> Item), access: (FSharpSymbol -> CcuThunk -> AccessorDomain -> bool)) =
member x.Assembly =
let ccu = defaultArg (SymbolHelpers.ccuOfItem cenv.g x.Item) cenv.thisCcu
FSharpAssembly(cenv, ccu)
member x.IsAccessible(rights: FSharpAccessibilityRights) = access x rights.ThisCcu rights.Contents
member x.IsExplicitlySuppressed = SymbolHelpers.IsExplicitlySuppressed cenv.g x.Item
member x.FullName = SymbolHelpers.FullNameOfItem cenv.g x.Item
member x.DeclarationLocation = SymbolHelpers.rangeOfItem cenv.g None x.Item
member x.ImplementationLocation = SymbolHelpers.rangeOfItem cenv.g (Some false) x.Item
member x.SignatureLocation = SymbolHelpers.rangeOfItem cenv.g (Some true) x.Item
member x.IsEffectivelySameAs(other:FSharpSymbol) =
x.Equals other || ItemsAreEffectivelyEqual cenv.g x.Item other.Item
member x.GetEffectivelySameAsHash() = ItemsAreEffectivelyEqualHash cenv.g x.Item
member internal x.Item = item()
member x.DisplayName = item().DisplayName
// This is actually overridden in all cases below. However some symbols are still just of type FSharpSymbol,
// see 'FSharpSymbol.Create' further below.
override x.Equals(other: obj) =
box x === other ||
match other with
| :? FSharpSymbol as otherSymbol -> ItemsAreEffectivelyEqual cenv.g x.Item otherSymbol.Item
| _ -> false
override x.GetHashCode() = hash x.ImplementationLocation
override x.ToString() = "symbol " + (try item().DisplayName with _ -> "?")
// TODO: there are several cases where we may need to report more interesting
// symbol information below. By default we return a vanilla symbol.
static member Create(g, thisCcu, thisCcuTyp, tcImports, item): FSharpSymbol =
FSharpSymbol.Create(SymbolEnv(g, thisCcu, Some thisCcuTyp, tcImports), item)
static member Create(cenv, item): FSharpSymbol =
let dflt() = FSharpSymbol(cenv, (fun () -> item), (fun _ _ _ -> true))
match item with
| Item.Value v -> FSharpMemberOrFunctionOrValue(cenv, V v, item) :> _
| Item.UnionCase (uinfo, _) -> FSharpUnionCase(cenv, uinfo.UnionCaseRef) :> _
| Item.ExnCase tcref -> FSharpEntity(cenv, tcref) :>_
| Item.RecdField rfinfo -> FSharpField(cenv, RecdOrClass rfinfo.RecdFieldRef) :> _
| Item.UnionCaseField (UnionCaseInfo (_, ucref), index) -> FSharpField (cenv, Union (ucref, index)) :> _
| Item.ILField finfo -> FSharpField(cenv, ILField finfo) :> _
| Item.AnonRecdField (anonInfo, tinst, n, m) -> FSharpField(cenv, AnonField (anonInfo, tinst, n, m)) :> _
| Item.Event einfo ->
FSharpMemberOrFunctionOrValue(cenv, E einfo, item) :> _
| Item.Property(_, pinfo :: _) ->
FSharpMemberOrFunctionOrValue(cenv, P pinfo, item) :> _
| Item.MethodGroup(_, minfo :: _, _) ->
FSharpMemberOrFunctionOrValue(cenv, M minfo, item) :> _
| Item.CtorGroup(_, cinfo :: _) ->
FSharpMemberOrFunctionOrValue(cenv, C cinfo, item) :> _
| Item.DelegateCtor (AbbrevOrAppTy tcref) ->
FSharpEntity(cenv, tcref) :>_
| Item.UnqualifiedType(tcref :: _)
| Item.Types(_, AbbrevOrAppTy tcref :: _) ->
FSharpEntity(cenv, tcref) :>_
| Item.ModuleOrNamespaces(modref :: _) ->
FSharpEntity(cenv, modref) :> _
| Item.SetterArg (_id, item) -> FSharpSymbol.Create(cenv, item)
| Item.CustomOperation (_customOpName, _, Some minfo) ->
FSharpMemberOrFunctionOrValue(cenv, M minfo, item) :> _
| Item.CustomBuilder (_, vref) ->
FSharpMemberOrFunctionOrValue(cenv, V vref, item) :> _
| Item.TypeVar (_, tp) ->
FSharpGenericParameter(cenv, tp) :> _
| Item.ActivePatternCase apref ->
FSharpActivePatternCase(cenv, apref.ActivePatternInfo, apref.ActivePatternVal.Type, apref.CaseIndex, Some apref.ActivePatternVal, item) :> _
| Item.ActivePatternResult (apinfo, ty, n, _) ->
FSharpActivePatternCase(cenv, apinfo, ty, n, None, item) :> _
| Item.ArgName(id, ty, argOwner) ->
FSharpParameter(cenv, id, ty, argOwner) :> _
| Item.ImplicitOp(_, { contents = Some(TraitConstraintSln.FSMethSln(_, vref, _)) }) ->
FSharpMemberOrFunctionOrValue(cenv, V vref, item) :> _
// TODO: the following don't currently return any interesting subtype
| Item.ImplicitOp _
| Item.ILField _
| Item.FakeInterfaceCtor _
| Item.NewDef _ -> dflt()
// These cases cover unreachable cases
| Item.CustomOperation (_, _, None)
| Item.UnqualifiedType []
| Item.ModuleOrNamespaces []
| Item.Property (_, [])
| Item.MethodGroup (_, [], _)
| Item.CtorGroup (_, [])
// These cases cover misc. corned cases (non-symbol types)
| Item.Types _
| Item.DelegateCtor _ -> dflt()
static member GetAccessibility (symbol: FSharpSymbol) =
match symbol with
| :? FSharpEntity as x -> Some x.Accessibility
| :? FSharpField as x -> Some x.Accessibility
| :? FSharpUnionCase as x -> Some x.Accessibility
| :? FSharpMemberFunctionOrValue as x -> Some x.Accessibility
| _ -> None
type FSharpEntity(cenv: SymbolEnv, entity:EntityRef) =
inherit FSharpSymbol(cenv,
(fun () ->
checkEntityIsResolved entity
if entity.IsModuleOrNamespace then Item.ModuleOrNamespaces [entity]
else Item.UnqualifiedType [entity]),
(fun _this thisCcu2 ad ->
checkForCrossProjectAccessibility cenv.g.ilg (thisCcu2, ad) (cenv.thisCcu, getApproxFSharpAccessibilityOfEntity entity))
// && AccessibilityLogic.IsEntityAccessible cenv.amap range0 ad entity)
)
// If an entity is in an assembly not available to us in the resolution set,
// we generally return "false" from predicates like IsClass, since we know
// nothing about that type.
let isResolvedAndFSharp() =
match entity with
| ERefNonLocal(NonLocalEntityRef(ccu, _)) -> not ccu.IsUnresolvedReference && ccu.IsFSharp
| _ -> cenv.thisCcu.IsFSharp
let isUnresolved() = entityIsUnresolved entity
let isResolved() = not (isUnresolved())
let checkIsResolved() = checkEntityIsResolved entity
let isDefinedInFSharpCore() =
match ccuOfTyconRef entity with
| None -> false
| Some ccu -> ccuEq ccu cenv.g.fslibCcu
member _.Entity = entity
member _.LogicalName =
checkIsResolved()
entity.LogicalName
member _.CompiledName =
checkIsResolved()
entity.CompiledName
member _.DisplayName =
checkIsResolved()
if entity.IsModuleOrNamespace then entity.DemangledModuleOrNamespaceName
else entity.DisplayName
member _.AccessPath =
checkIsResolved()
match entity.CompilationPathOpt with
| None -> "global"
| Some (CompPath(_, [])) -> "global"
| Some cp -> buildAccessPath (Some cp)
member x.DeclaringEntity =
match entity.CompilationPathOpt with
| None -> None
| Some (CompPath(_, [])) -> None
| Some cp ->
match x.Assembly.Contents.FindEntityByPath cp.MangledPath with
| Some res -> Some res
| None ->
// The declaring entity may be in this assembly, including a type possibly hidden by a signature.
match cenv.thisCcuTy with
| Some t ->
let s = FSharpAssemblySignature(cenv, None, None, t)
s.FindEntityByPath cp.MangledPath
| None -> None
member _.Namespace =
checkIsResolved()
match entity.CompilationPathOpt with
| None -> None
| Some (CompPath(_, [])) -> None
| Some cp when cp.AccessPath |> List.forall (function (_, ModuleOrNamespaceKind.Namespace) -> true | _ -> false) ->
Some (buildAccessPath (Some cp))
| Some _ -> None
member x.QualifiedName =
checkIsResolved()
let fail() = invalidOp (sprintf "the type '%s' does not have a qualified name" x.LogicalName)
#if !NO_EXTENSIONTYPING
if entity.IsTypeAbbrev || entity.IsProvidedErasedTycon || entity.IsNamespace then fail()
#else
if entity.IsTypeAbbrev || entity.IsNamespace then fail()
#endif
match entity.CompiledRepresentation with
| CompiledTypeRepr.ILAsmNamed(tref, _, _) -> tref.QualifiedName
| CompiledTypeRepr.ILAsmOpen _ -> fail()
member x.FullName =
checkIsResolved()
match x.TryFullName with
| None -> invalidOp (sprintf "the type '%s' does not have a qualified name" x.LogicalName)
| Some nm -> nm
member x.TryFullName =
if isUnresolved() then None
#if !NO_EXTENSIONTYPING
elif entity.IsTypeAbbrev || entity.IsProvidedErasedTycon then None
#else
elif entity.IsTypeAbbrev then None
#endif
elif entity.IsNamespace then Some entity.DemangledModuleOrNamespaceName
else
match entity.CompiledRepresentation with
| CompiledTypeRepr.ILAsmNamed(tref, _, _) -> Some tref.FullName
| CompiledTypeRepr.ILAsmOpen _ -> None
member _.DeclarationLocation =
checkIsResolved()
entity.Range
member x.GenericParameters =
checkIsResolved()
entity.TyparsNoRange |> List.map (fun tp -> FSharpGenericParameter(cenv, tp)) |> makeReadOnlyCollection
member _.IsMeasure =
isResolvedAndFSharp() && (entity.TypeOrMeasureKind = TyparKind.Measure)
member x.IsAbstractClass =
isResolved() && isAbstractTycon entity.Deref
member _.IsFSharpModule =
isResolvedAndFSharp() && entity.IsModule
member _.HasFSharpModuleSuffix =
isResolvedAndFSharp() &&
entity.IsModule &&
(entity.ModuleOrNamespaceType.ModuleOrNamespaceKind = ModuleOrNamespaceKind.FSharpModuleWithSuffix)
member _.IsValueType =
isResolved() &&
entity.IsStructOrEnumTycon
member x.IsArrayType =
isResolved() &&
isArrayTyconRef cenv.g entity
member _.ArrayRank =
checkIsResolved()
rankOfArrayTyconRef cenv.g entity
#if !NO_EXTENSIONTYPING
member _.IsProvided =
isResolved() &&
entity.IsProvided
member _.IsProvidedAndErased =
isResolved() &&
entity.IsProvidedErasedTycon
member _.IsStaticInstantiation =
isResolved() &&
entity.IsStaticInstantiationTycon
member _.IsProvidedAndGenerated =
isResolved() &&
entity.IsProvidedGeneratedTycon
#endif
member _.IsClass =
isResolved() &&
match metadataOfTycon entity.Deref with
#if !NO_EXTENSIONTYPING
| ProvidedTypeMetadata info -> info.IsClass
#endif
| ILTypeMetadata (TILObjectReprData(_, _, td)) -> td.IsClass
| FSharpOrArrayOrByrefOrTupleOrExnTypeMetadata -> entity.Deref.IsFSharpClassTycon
member _.IsByRef =
isResolved() &&
isByrefTyconRef cenv.g entity
member _.IsOpaque =
isResolved() &&
entity.IsHiddenReprTycon
member _.IsInterface =
isResolved() &&
isInterfaceTyconRef entity
member _.IsDelegate =
isResolved() &&
match metadataOfTycon entity.Deref with
#if !NO_EXTENSIONTYPING
| ProvidedTypeMetadata info -> info.IsDelegate ()
#endif
| ILTypeMetadata (TILObjectReprData(_, _, td)) -> td.IsDelegate
| FSharpOrArrayOrByrefOrTupleOrExnTypeMetadata -> entity.IsFSharpDelegateTycon
member _.IsEnum =
isResolved() &&
entity.IsEnumTycon
member _.IsFSharpExceptionDeclaration =
isResolvedAndFSharp() && entity.IsExceptionDecl
member _.IsUnresolved =
isUnresolved()
member _.IsFSharp =
isResolvedAndFSharp()
member _.IsFSharpAbbreviation =
isResolvedAndFSharp() && entity.IsTypeAbbrev
member _.IsFSharpRecord =
isResolvedAndFSharp() && entity.IsRecordTycon
member _.IsFSharpUnion =
isResolvedAndFSharp() && entity.IsUnionTycon
member _.HasAssemblyCodeRepresentation =
isResolvedAndFSharp() && (entity.IsAsmReprTycon || entity.IsMeasureableReprTycon)
member _.FSharpDelegateSignature =
checkIsResolved()
match entity.TypeReprInfo with
| TFSharpObjectRepr r when entity.IsFSharpDelegateTycon ->
match r.fsobjmodel_kind with
| TTyconDelegate ss -> FSharpDelegateSignature(cenv, ss)
| _ -> invalidOp "not a delegate type"
| _ -> invalidOp "not a delegate type"
member _.Accessibility =
if isUnresolved() then FSharpAccessibility taccessPublic else
FSharpAccessibility(getApproxFSharpAccessibilityOfEntity entity)
member _.RepresentationAccessibility =
if isUnresolved() then FSharpAccessibility taccessPublic else
FSharpAccessibility(entity.TypeReprAccessibility)
member x.DeclaredInterfaces =
if isUnresolved() then makeReadOnlyCollection [] else
ErrorLogger.protectAssemblyExploration [] (fun () ->
[ for ty in GetImmediateInterfacesOfType SkipUnrefInterfaces.Yes cenv.g cenv.amap range0 (generalizedTyconRef entity) do
yield FSharpType(cenv, ty) ])
|> makeReadOnlyCollection
member x.AllInterfaces =
if isUnresolved() then makeReadOnlyCollection [] else
ErrorLogger.protectAssemblyExploration [] (fun () ->
[ for ty in AllInterfacesOfType cenv.g cenv.amap range0 AllowMultiIntfInstantiations.Yes (generalizedTyconRef entity) do
yield FSharpType(cenv, ty) ])
|> makeReadOnlyCollection
member x.IsAttributeType =
if isUnresolved() then false else
let ty = generalizedTyconRef entity
ErrorLogger.protectAssemblyExploration false <| fun () ->
Infos.ExistsHeadTypeInEntireHierarchy cenv.g cenv.amap range0 ty cenv.g.tcref_System_Attribute
member x.IsDisposableType =
if isUnresolved() then false else
let ty = generalizedTyconRef entity
ErrorLogger.protectAssemblyExploration false <| fun () ->
Infos.ExistsHeadTypeInEntireHierarchy cenv.g cenv.amap range0 ty cenv.g.tcref_System_IDisposable
member x.BaseType =
checkIsResolved()
GetSuperTypeOfType cenv.g cenv.amap range0 (generalizedTyconRef entity)
|> Option.map (fun ty -> FSharpType(cenv, ty))
member _.UsesPrefixDisplay =
if isUnresolved() then true else
not (isResolvedAndFSharp()) || entity.Deref.IsPrefixDisplay
member x.IsNamespace = entity.IsNamespace
member x.MembersOrValues = x.MembersFunctionsAndValues
member x.MembersFunctionsAndValues =
if isUnresolved() then makeReadOnlyCollection[] else
protect <| fun () ->
([ let _, entityTy = generalizeTyconRef entity
let createMember (minfo: MethInfo) =
if minfo.IsConstructor then FSharpMemberOrFunctionOrValue(cenv, C minfo, Item.CtorGroup (minfo.DisplayName, [minfo]))
else FSharpMemberOrFunctionOrValue(cenv, M minfo, Item.MethodGroup (minfo.DisplayName, [minfo], None))
if x.IsFSharpAbbreviation then
()
elif x.IsFSharp then
// For F# code we emit methods members in declaration order
for v in entity.MembersOfFSharpTyconSorted do
// Ignore members representing the generated .cctor
if not v.Deref.IsClassConstructor then
yield createMember (FSMeth(cenv.g, entityTy, v, None))
else
for minfo in GetImmediateIntrinsicMethInfosOfType (None, AccessibleFromSomeFSharpCode) cenv.g cenv.amap range0 entityTy do
yield createMember minfo
let props = GetImmediateIntrinsicPropInfosOfType (None, AccessibleFromSomeFSharpCode) cenv.g cenv.amap range0 entityTy
let events = cenv.infoReader.GetImmediateIntrinsicEventsOfType (None, AccessibleFromSomeFSharpCode, range0, entityTy)
for pinfo in props do
yield FSharpMemberOrFunctionOrValue(cenv, P pinfo, Item.Property (pinfo.PropertyName, [pinfo]))
for einfo in events do
yield FSharpMemberOrFunctionOrValue(cenv, E einfo, Item.Event einfo)
// Emit the values, functions and F#-declared extension members in a module
for v in entity.ModuleOrNamespaceType.AllValsAndMembers do
if v.IsExtensionMember then
// For F#-declared extension members, yield a value-backed member and a property info if possible
let vref = mkNestedValRef entity v
yield FSharpMemberOrFunctionOrValue(cenv, V vref, Item.Value vref)
match v.MemberInfo.Value.MemberFlags.MemberKind, v.ApparentEnclosingEntity with
| MemberKind.PropertyGet, Parent p ->
let pinfo = FSProp(cenv.g, generalizedTyconRef p, Some vref, None)
yield FSharpMemberOrFunctionOrValue(cenv, P pinfo, Item.Property (pinfo.PropertyName, [pinfo]))
| MemberKind.PropertySet, Parent p ->
let pinfo = FSProp(cenv.g, generalizedTyconRef p, None, Some vref)
yield FSharpMemberOrFunctionOrValue(cenv, P pinfo, Item.Property (pinfo.PropertyName, [pinfo]))
| _ -> ()
elif not v.IsMember then
let vref = mkNestedValRef entity v
yield FSharpMemberOrFunctionOrValue(cenv, V vref, Item.Value vref) ]
|> makeReadOnlyCollection)
member _.XmlDocSig =
checkIsResolved()
getXmlDocSigForEntity cenv entity
member _.XmlDoc =
if isUnresolved() then XmlDoc.Empty |> makeXmlDoc else
entity.XmlDoc |> makeXmlDoc
member _.ElaboratedXmlDoc =
if isUnresolved() then XmlDoc.Empty |> makeElaboratedXmlDoc else
entity.XmlDoc |> makeElaboratedXmlDoc
member x.StaticParameters =
match entity.TypeReprInfo with
#if !NO_EXTENSIONTYPING
| TProvidedTypeExtensionPoint info ->
let m = x.DeclarationLocation
let typeBeforeArguments = info.ProvidedType
let staticParameters = typeBeforeArguments.PApplyWithProvider((fun (typeBeforeArguments, provider) -> typeBeforeArguments.GetStaticParameters provider), range=m)
let staticParameters = staticParameters.PApplyArray(id, "GetStaticParameters", m)
[| for p in staticParameters -> FSharpStaticParameter(cenv, p, m) |]
#endif
| _ -> [| |]
|> makeReadOnlyCollection
member _.NestedEntities =
if isUnresolved() then makeReadOnlyCollection[] else
entity.ModuleOrNamespaceType.AllEntities
|> QueueList.toList
|> List.map (fun x -> FSharpEntity(cenv, entity.NestedTyconRef x))
|> makeReadOnlyCollection
member x.UnionCases =
if isUnresolved() then makeReadOnlyCollection[] else
entity.UnionCasesAsRefList
|> List.map (fun x -> FSharpUnionCase(cenv, x))
|> makeReadOnlyCollection
member x.RecordFields = x.FSharpFields
member x.FSharpFields =
if isUnresolved() then makeReadOnlyCollection[] else
if entity.IsILEnumTycon then
let (TILObjectReprData(_scoref, _enc, tdef)) = entity.ILTyconInfo
let formalTypars = entity.Typars(range.Zero)
let formalTypeInst = generalizeTypars formalTypars
let ty = TType_app(entity, formalTypeInst)
let formalTypeInfo = ILTypeInfo.FromType cenv.g ty
tdef.Fields.AsList
|> List.map (fun tdef -> let ilFieldInfo = ILFieldInfo(formalTypeInfo, tdef)
FSharpField(cenv, FSharpFieldData.ILField ilFieldInfo ))
|> makeReadOnlyCollection
else
entity.AllFieldsAsList
|> List.map (fun x -> FSharpField(cenv, mkRecdFieldRef entity x.Name))
|> makeReadOnlyCollection
member x.AbbreviatedType =
checkIsResolved()
match entity.TypeAbbrev with
| None -> invalidOp "not a type abbreviation"
| Some ty -> FSharpType(cenv, ty)
member _.Attributes =
if isUnresolved() then makeReadOnlyCollection[] else
GetAttribInfosOfEntity cenv.g cenv.amap range0 entity
|> List.map (fun a -> FSharpAttribute(cenv, a))
|> makeReadOnlyCollection
member _.AllCompilationPaths =
checkIsResolved()
let (CompPath(_, parts)) = entity.CompilationPath
let partsList =
[ yield parts
match parts with
| ("Microsoft", ModuleOrNamespaceKind.Namespace) :: rest when isDefinedInFSharpCore() -> yield rest
| _ -> ()]
let mapEachCurrentPath (paths: string list list) path =
match paths with
| [] -> [[path]]
| _ -> paths |> List.map (fun x -> path :: x)
let walkParts (parts: (string * ModuleOrNamespaceKind) list) =
let rec loop (currentPaths: string list list) parts =
match parts with
| [] -> currentPaths
| (name: string, kind) :: rest ->
match kind with
| ModuleOrNamespaceKind.FSharpModuleWithSuffix ->
[ yield! loop (mapEachCurrentPath currentPaths name) rest
yield! loop (mapEachCurrentPath currentPaths (name.[..name.Length - 7])) rest ]
| _ ->
loop (mapEachCurrentPath currentPaths name) rest
loop [] parts |> List.map (List.rev >> String.concat ".")
let res =
[ for parts in partsList do
yield! walkParts parts ]
res
member x.ActivePatternCases =
protect <| fun () ->
ActivePatternElemsOfModuleOrNamespace x.Entity
|> Map.toList
|> List.map (fun (_, apref) ->
let item = Item.ActivePatternCase apref
FSharpActivePatternCase(cenv, apref.ActivePatternInfo, apref.ActivePatternVal.Type, apref.CaseIndex, Some apref.ActivePatternVal, item))
override x.Equals(other: obj) =
box x === other ||
match other with
| :? FSharpEntity as otherEntity -> tyconRefEq cenv.g entity otherEntity.Entity
| _ -> false
override x.GetHashCode() =
checkIsResolved()
((hash entity.Stamp) <<< 1) + 1
override x.ToString() = x.CompiledName
type FSharpUnionCase(cenv, v: UnionCaseRef) =
inherit FSharpSymbol (cenv,
(fun () ->
checkEntityIsResolved v.TyconRef
Item.UnionCase(UnionCaseInfo(generalizeTypars v.TyconRef.TyparsNoRange, v), false)),
(fun _this thisCcu2 ad ->
checkForCrossProjectAccessibility cenv.g.ilg (thisCcu2, ad) (cenv.thisCcu, v.UnionCase.Accessibility))
//&& AccessibilityLogic.IsUnionCaseAccessible cenv.amap range0 ad v)
)
let isUnresolved() =
entityIsUnresolved v.TyconRef || v.TryUnionCase.IsNone
let checkIsResolved() =
checkEntityIsResolved v.TyconRef
if v.TryUnionCase.IsNone then
invalidOp (sprintf "The union case '%s' could not be found in the target type" v.CaseName)
member _.IsUnresolved =
isUnresolved()
member _.Name =
checkIsResolved()
v.UnionCase.DisplayName
member _.DeclarationLocation =
checkIsResolved()
v.Range
member _.HasFields =
if isUnresolved() then false else
v.UnionCase.RecdFieldsArray.Length <> 0
member _.UnionCaseFields =
if isUnresolved() then makeReadOnlyCollection [] else
v.UnionCase.RecdFieldsArray |> Array.mapi (fun i _ -> FSharpField(cenv, FSharpFieldData.Union (v, i))) |> makeReadOnlyCollection
member _.ReturnType =
checkIsResolved()
FSharpType(cenv, v.ReturnType)
member _.CompiledName =
checkIsResolved()
v.UnionCase.CompiledName
member _.XmlDocSig =
checkIsResolved()
let unionCase = UnionCaseInfo(generalizeTypars v.TyconRef.TyparsNoRange, v)
match SymbolHelpers.GetXmlDocSigOfUnionCaseInfo unionCase with
| Some (_, docsig) -> docsig
| _ -> ""
member _.XmlDoc =
if isUnresolved() then XmlDoc.Empty |> makeXmlDoc else
v.UnionCase.XmlDoc |> makeXmlDoc
member _.ElaboratedXmlDoc =
if isUnresolved() then XmlDoc.Empty |> makeElaboratedXmlDoc else
v.UnionCase.XmlDoc |> makeElaboratedXmlDoc
member _.Attributes =
if isUnresolved() then makeReadOnlyCollection [] else
v.Attribs |> List.map (fun a -> FSharpAttribute(cenv, AttribInfo.FSAttribInfo(cenv.g, a))) |> makeReadOnlyCollection
member _.Accessibility =
if isUnresolved() then FSharpAccessibility taccessPublic else
FSharpAccessibility(v.UnionCase.Accessibility)
member private x.V = v
override x.Equals(other: obj) =
box x === other ||
match other with
| :? FSharpUnionCase as uc -> v === uc.V
| _ -> false
override x.GetHashCode() = hash v.CaseName
override x.ToString() = x.CompiledName
type FSharpFieldData =
| AnonField of AnonRecdTypeInfo * TTypes * int * range
| ILField of ILFieldInfo
| RecdOrClass of RecdFieldRef
| Union of UnionCaseRef * int
member x.TryRecdField =
match x with
| AnonField (anonInfo, tinst, n, m) -> (anonInfo, tinst, n, m) |> Choice3Of3
| RecdOrClass v -> v.RecdField |> Choice1Of3
| Union (v, n) -> v.FieldByIndex n |> Choice1Of3
| ILField f -> f |> Choice2Of3
member x.TryDeclaringTyconRef =
match x with
| RecdOrClass v -> Some v.TyconRef
| ILField f -> Some f.DeclaringTyconRef
| _ -> None
type FSharpAnonRecordTypeDetails(cenv: SymbolEnv, anonInfo: AnonRecdTypeInfo) =
member _.Assembly = FSharpAssembly (cenv, anonInfo.Assembly)
/// Names of any enclosing types of the compiled form of the anonymous type (if the anonymous type was defined as a nested type)
member _.EnclosingCompiledTypeNames = anonInfo.ILTypeRef.Enclosing
/// The name of the compiled form of the anonymous type
member _.CompiledName = anonInfo.ILTypeRef.Name
/// The sorted labels of the anonymous type
member _.SortedFieldNames = anonInfo.SortedNames
type FSharpField(cenv: SymbolEnv, d: FSharpFieldData) =
inherit FSharpSymbol (cenv,
(fun () ->
match d with
| AnonField (anonInfo, tinst, n, m) ->
Item.AnonRecdField(anonInfo, tinst, n, m)
| RecdOrClass v ->
checkEntityIsResolved v.TyconRef
Item.RecdField(RecdFieldInfo(generalizeTypars v.TyconRef.TyparsNoRange, v))
| Union (v, fieldIndex) ->
checkEntityIsResolved v.TyconRef
Item.UnionCaseField (UnionCaseInfo (generalizeTypars v.TyconRef.TyparsNoRange, v), fieldIndex)
| ILField f ->
Item.ILField f),
(fun this thisCcu2 ad ->
checkForCrossProjectAccessibility cenv.g.ilg (thisCcu2, ad) (cenv.thisCcu, (this :?> FSharpField).Accessibility.Contents))
//&&
//match d with
//| Recd v -> AccessibilityLogic.IsRecdFieldAccessible cenv.amap range0 ad v
//| Union (v, _) -> AccessibilityLogic.IsUnionCaseAccessible cenv.amap range0 ad v)
)
let isUnresolved() =
d.TryDeclaringTyconRef |> Option.exists entityIsUnresolved ||
match d with
| AnonField _ -> false
| RecdOrClass v -> v.TryRecdField.IsNone
| Union (v, _) -> v.TryUnionCase.IsNone
| ILField _ -> false
let checkIsResolved() =
d.TryDeclaringTyconRef |> Option.iter checkEntityIsResolved
match d with
| AnonField _ -> ()
| RecdOrClass v ->
if v.TryRecdField.IsNone then
invalidOp (sprintf "The record field '%s' could not be found in the target type" v.FieldName)
| Union (v, _) ->
if v.TryUnionCase.IsNone then
invalidOp (sprintf "The union case '%s' could not be found in the target type" v.CaseName)
| ILField _ -> ()
new (cenv, ucref: UnionCaseRef, n) = FSharpField(cenv, FSharpFieldData.Union(ucref, n))
new (cenv, rfref: RecdFieldRef) = FSharpField(cenv, FSharpFieldData.RecdOrClass rfref)
member _.DeclaringEntity =
d.TryDeclaringTyconRef |> Option.map (fun tcref -> FSharpEntity(cenv, tcref))
member _.IsUnresolved =
isUnresolved()
member _.IsMutable =
if isUnresolved() then false else
match d.TryRecdField with
| Choice1Of3 r -> r.IsMutable
| Choice2Of3 f -> not f.IsInitOnly && f.LiteralValue.IsNone
| Choice3Of3 _ -> false
member _.IsLiteral =
if isUnresolved() then false else
match d.TryRecdField with
| Choice1Of3 r -> r.LiteralValue.IsSome
| Choice2Of3 f -> f.LiteralValue.IsSome
| Choice3Of3 _ -> false
member _.LiteralValue =
if isUnresolved() then None else
match d.TryRecdField with
| Choice1Of3 r -> getLiteralValue r.LiteralValue
| Choice2Of3 f -> f.LiteralValue |> Option.map AbstractIL.ILRuntimeWriter.convFieldInit
| Choice3Of3 _ -> None
member _.IsVolatile =
if isUnresolved() then false else
match d.TryRecdField with
| Choice1Of3 r -> r.IsVolatile
| Choice2Of3 _ -> false // F# doesn't actually respect "volatile" from other assemblies in any case
| Choice3Of3 _ -> false
member _.IsDefaultValue =
if isUnresolved() then false else
match d.TryRecdField with
| Choice1Of3 r -> r.IsZeroInit
| Choice2Of3 _ -> false
| Choice3Of3 _ -> false
member _.IsAnonRecordField =
match d with
| AnonField _ -> true
| _ -> false
member _.AnonRecordFieldDetails =
match d with
| AnonField (anonInfo, types, n, _) -> FSharpAnonRecordTypeDetails(cenv, anonInfo), [| for ty in types -> FSharpType(cenv, ty) |], n
| _ -> invalidOp "not an anonymous record field"
member _.IsUnionCaseField =
match d with
| Union _ -> true
| _ -> false
member _.DeclaringUnionCase =
match d with
| Union (v, _) -> Some (FSharpUnionCase (cenv, v))
| _ -> None
member _.XmlDocSig =
checkIsResolved()
let xmlsig =
match d with
| RecdOrClass v ->
let recd = RecdFieldInfo(generalizeTypars v.TyconRef.TyparsNoRange, v)