1+ // Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
2+
3+ namespace Microsoft.FSharp.Compiler.SourceCodeServices
4+
5+ /// Active patterns over `FSharpSymbolUse`.
6+ [<RequireQualifiedAccess>]
7+ module SymbolUse =
8+
9+ let (| ActivePatternCase | _ |) ( symbol : FSharpSymbolUse ) =
10+ match symbol.Symbolwith
11+ | :? FSharpActivePatternCaseas ap-> ActivePatternCase( ap) |> Some
12+ | _ -> None
13+
14+ let private attributeSuffixLength = " Attribute" .Length
15+
16+ let (| Entity | _ |) ( symbol : FSharpSymbolUse ) : ( FSharpEntity * (* cleanFullNames *) string list )option =
17+ match symbol.Symbolwith
18+ | :? FSharpEntityas ent->
19+ // strip generic parameters count suffix (List`1 => List)
20+ let cleanFullName =
21+ // `TryFullName` for type aliases is always `None`, so we have to make one by our own
22+ if ent.IsFSharpAbbreviationthen
23+ [ ent.AccessPath+ " ." + ent.DisplayName]
24+ else
25+ ent.TryFullName
26+ |> Option.toList
27+ |> List.map( fun fullName ->
28+ if ent.GenericParameters.Count> 0 && fullName.Length> 2 then
29+ fullName.[ 0 .. fullName.Length- 3 ]
30+ else fullName)
31+
32+ let cleanFullNames =
33+ cleanFullName
34+ |> List.collect( fun cleanFullName ->
35+ if ent.IsAttributeTypethen
36+ [ cleanFullName; cleanFullName.[ 0 .. cleanFullName.Length- attributeSuffixLength- 1 ]]
37+ else [ cleanFullName]
38+ )
39+ Some( ent, cleanFullNames)
40+ | _ -> None
41+
42+ let (| Field | _ |) ( symbol : FSharpSymbolUse ) =
43+ match symbol.Symbolwith
44+ | :? FSharpFieldas field-> Some field
45+ | _ -> None
46+
47+ let (| GenericParameter | _ |) ( symbol : FSharpSymbolUse ) =
48+ match symbol.Symbolwith
49+ | :? FSharpGenericParameteras gp-> Some gp
50+ | _ -> None
51+
52+ let (| MemberFunctionOrValue | _ |) ( symbol : FSharpSymbolUse ) =
53+ match symbol.Symbolwith
54+ | :? FSharpMemberOrFunctionOrValueas func-> Some func
55+ | _ -> None
56+
57+ let (| ActivePattern | _ |) = function
58+ | MemberFunctionOrValue mwhen m.IsActivePattern-> Some m| _ -> None
59+
60+ let (| Parameter | _ |) ( symbol : FSharpSymbolUse ) =
61+ match symbol.Symbolwith
62+ | :? FSharpParameteras param-> Some param
63+ | _ -> None
64+
65+ let (| StaticParameter | _ |) ( symbol : FSharpSymbolUse ) =
66+ match symbol.Symbolwith
67+ | :? FSharpStaticParameteras sp-> Some sp
68+ | _ -> None
69+
70+ let (| UnionCase | _ |) ( symbol : FSharpSymbolUse ) =
71+ match symbol.Symbolwith
72+ | :? FSharpUnionCaseas uc-> Some uc
73+ | _ -> None
74+
75+ let (| Constructor | _ |) = function
76+ | MemberFunctionOrValue funcwhen func.IsConstructor|| func.IsImplicitConstructor-> Some func
77+ | _ -> None
78+
79+ let (| TypeAbbreviation | _ |) = function
80+ | Entity( entity, _) when entity.IsFSharpAbbreviation-> Some entity
81+ | _ -> None
82+
83+ let (| Class | _ |) = function
84+ | Entity( entity, _) when entity.IsClass-> Some entity
85+ | Entity( entity, _) when entity.IsFSharp&&
86+ entity.IsOpaque&&
87+ not entity.IsFSharpModule&&
88+ not entity.IsNamespace&&
89+ not entity.IsDelegate&&
90+ not entity.IsFSharpUnion&&
91+ not entity.IsFSharpRecord&&
92+ not entity.IsInterface&&
93+ not entity.IsValueType-> Some entity
94+ | _ -> None
95+
96+ let (| Delegate | _ |) = function
97+ | Entity( entity, _) when entity.IsDelegate-> Some entity
98+ | _ -> None
99+
100+ let (| Event | _ |) = function
101+ | MemberFunctionOrValue symbolwhen symbol.IsEvent-> Some symbol
102+ | _ -> None
103+
104+ let (| Property | _ |) = function
105+ | MemberFunctionOrValue symbolwhen symbol.IsProperty|| symbol.IsPropertyGetterMethod|| symbol.IsPropertySetterMethod-> Some symbol
106+ | _ -> None
107+
108+ let inline private notCtorOrProp ( symbol : FSharpMemberOrFunctionOrValue ) =
109+ not symbol.IsConstructor&& not symbol.IsPropertyGetterMethod&& not symbol.IsPropertySetterMethod
110+
111+ type FSharpSymbol with
112+ member x.IsOperatorOrActivePattern =
113+ let name = x.DisplayName
114+ if name.StartsWith" (" && name.EndsWith" )" && name.Length> 4
115+ then name.Substring( 2 , name.Length- 4 ) |> String.forall( fun c -> c<> ' ' )
116+ else false
117+
118+ type FSharpMemberOrFunctionOrValue with
119+ member x.FullTypeSafe = try Some x.FullTypewith _ -> None
120+
121+ let (| Method | _ |) ( symbolUse : FSharpSymbolUse ) =
122+ match symbolUsewith
123+ | MemberFunctionOrValue symbolwhen
124+ symbol.IsModuleValueOrMember&&
125+ not symbolUse.IsFromPattern&&
126+ not symbol.IsOperatorOrActivePattern&&
127+ not symbol.IsPropertyGetterMethod&&
128+ not symbol.IsPropertySetterMethod-> Some symbol
129+ | _ -> None
130+
131+ let (| Function | _ |) ( symbolUse : FSharpSymbolUse ) =
132+ match symbolUsewith
133+ | MemberFunctionOrValue symbolwhen
134+ notCtorOrProp symbol&&
135+ symbol.IsModuleValueOrMember&&
136+ not symbol.IsOperatorOrActivePattern&&
137+ not symbolUse.IsFromPattern->
138+
139+ match symbol.FullTypeSafewith
140+ | Some fullTypewhen fullType.IsFunctionType-> Some symbol
141+ | _ -> None
142+ | _ -> None
143+
144+ let (| Operator | _ |) ( symbolUse : FSharpSymbolUse ) =
145+ match symbolUsewith
146+ | MemberFunctionOrValue symbolwhen
147+ notCtorOrProp symbol&&
148+ not symbolUse.IsFromPattern&&
149+ not symbol.IsActivePattern&&
150+ symbol.IsOperatorOrActivePattern->
151+
152+ match symbol.FullTypeSafewith
153+ | Some fullTypewhen fullType.IsFunctionType-> Some symbol
154+ | _ -> None
155+ | _ -> None
156+
157+ let (| Pattern | _ |) ( symbolUse : FSharpSymbolUse ) =
158+ match symbolUsewith
159+ | MemberFunctionOrValue symbolwhen
160+ notCtorOrProp symbol&&
161+ not symbol.IsOperatorOrActivePattern&&
162+ symbolUse.IsFromPattern->
163+
164+ match symbol.FullTypeSafewith
165+ | Some fullTypewhen fullType.IsFunctionType-> Some symbol
166+ | _ -> None
167+ | _ -> None
168+
169+ let (| ClosureOrNestedFunction | _ |) = function
170+ | MemberFunctionOrValue symbolwhen
171+ notCtorOrProp symbol&&
172+ not symbol.IsOperatorOrActivePattern&&
173+ not symbol.IsModuleValueOrMember->
174+
175+ match symbol.FullTypeSafewith
176+ | Some fullTypewhen fullType.IsFunctionType-> Some symbol
177+ | _ -> None
178+ | _ -> None
179+
180+ let (| Val | _ |) = function
181+ | MemberFunctionOrValue symbolwhen notCtorOrProp symbol&&
182+ not symbol.IsOperatorOrActivePattern->
183+ match symbol.FullTypeSafewith
184+ | Some_ fullType-> Some symbol
185+ | _ -> None
186+ | _ -> None
187+
188+ let (| Enum | _ |) = function
189+ | Entity( entity, _) when entity.IsEnum-> Some entity
190+ | _ -> None
191+
192+ let (| Interface | _ |) = function
193+ | Entity( entity, _) when entity.IsInterface-> Some entity
194+ | _ -> None
195+
196+ let (| Module | _ |) = function
197+ | Entity( entity, _) when entity.IsFSharpModule-> Some entity
198+ | _ -> None
199+
200+ let (| Namespace | _ |) = function
201+ | Entity( entity, _) when entity.IsNamespace-> Some entity
202+ | _ -> None
203+
204+ let (| Record | _ |) = function
205+ | Entity( entity, _) when entity.IsFSharpRecord-> Some entity
206+ | _ -> None
207+
208+ let (| Union | _ |) = function
209+ | Entity( entity, _) when entity.IsFSharpUnion-> Some entity
210+ | _ -> None
211+
212+ let (| ValueType | _ |) = function
213+ | Entity( entity, _) when entity.IsValueType&& not entity.IsEnum-> Some entity
214+ | _ -> None
215+
216+ let (| ComputationExpression | _ |) ( symbol : FSharpSymbolUse ) =
217+ if symbol.IsFromComputationExpressionthen Some symbol
218+ else None
219+
220+ let (| Attribute | _ |) = function
221+ | Entity( entity, _) when entity.IsAttributeType-> Some entity
222+ | _ -> None