|
2 | 2 |
|
3 | 3 | namespaceMicrosoft.FSharp.Compiler.SourceCodeServices |
4 | 4 |
|
| 5 | +/// Patterns over FSharpSymbol and derivatives. |
| 6 | +[<RequireQualifiedAccess>] |
| 7 | +moduleSymbol= |
| 8 | +openSystem.Text.RegularExpressions |
| 9 | +openSystem |
| 10 | + |
| 11 | +letisAttribute<'T>(attribute:FSharpAttribute)= |
| 12 | +// CompiledName throws exception on DataContractAttribute generated by SQLProvider |
| 13 | +try attribute.AttributeType.CompiledName= typeof<'T>.Namewith_->false |
| 14 | + |
| 15 | +lettryGetAttribute<'T>(attributes:seq<FSharpAttribute>)= |
| 16 | + attributes|> Seq.tryFind isAttribute<'T> |
| 17 | + |
| 18 | +moduleOption= |
| 19 | +letattempt f=try Some(f())with_-> None |
| 20 | + |
| 21 | +lethasModuleSuffixAttribute(entity:FSharpEntity)= |
| 22 | + entity.Attributes |
| 23 | +|> tryGetAttribute<CompilationRepresentationAttribute> |
| 24 | +|> Option.bind(fun a-> |
| 25 | + Option.attempt(fun _-> a.ConstructorArguments) |
| 26 | +|> Option.bind(fun args-> args|> Seq.tryPick(fun(_,arg)-> |
| 27 | +letres= |
| 28 | +match argwith |
| 29 | +|:? int32as argwhen arg= int CompilationRepresentationFlags.ModuleSuffix-> |
| 30 | + Some() |
| 31 | +|:? CompilationRepresentationFlagsas argwhen arg= CompilationRepresentationFlags.ModuleSuffix-> |
| 32 | + Some() |
| 33 | +|_-> |
| 34 | + None |
| 35 | + res))) |
| 36 | +|> Option.isSome |
| 37 | + |
| 38 | +letisOperator(name:string)= |
| 39 | + name.StartsWith"("&& name.EndsWith" )"&& name.Length>4 |
| 40 | +&& name.Substring(2, name.Length-4) |
| 41 | +|> String.forall(fun c-> c<>' '&¬(Char.IsLetter c)) |
| 42 | + |
| 43 | +letUnnamedUnionFieldRegex= Regex("^Item(\d+)?$", RegexOptions.Compiled) |
| 44 | + |
| 45 | +letisUnnamedUnionCaseField(field:FSharpField)= UnnamedUnionFieldRegex.IsMatch(field.Name) |
| 46 | + |
| 47 | +let(|AbbreviatedType|_|)(entity:FSharpEntity)= |
| 48 | +if entity.IsFSharpAbbreviationthen Some entity.AbbreviatedType |
| 49 | +else None |
| 50 | + |
| 51 | +let(|TypeWithDefinition|_|)(ty:FSharpType)= |
| 52 | +if ty.HasTypeDefinitionthen Some ty.TypeDefinition |
| 53 | +else None |
| 54 | + |
| 55 | +let recgetEntityAbbreviatedType(entity:FSharpEntity)= |
| 56 | +if entity.IsFSharpAbbreviationthen |
| 57 | +match entity.AbbreviatedTypewith |
| 58 | +| TypeWithDefinition def-> getEntityAbbreviatedType def |
| 59 | +| abbreviatedType-> entity, Some abbreviatedType |
| 60 | +else entity, None |
| 61 | + |
| 62 | +let recgetAbbreviatedType(fsharpType:FSharpType)= |
| 63 | +if fsharpType.IsAbbreviationthen |
| 64 | + getAbbreviatedType fsharpType.AbbreviatedType |
| 65 | +else fsharpType |
| 66 | + |
| 67 | +let(|Attribute|_|)(entity:FSharpEntity)= |
| 68 | +letisAttribute(entity:FSharpEntity)= |
| 69 | +letgetBaseType(entity:FSharpEntity)= |
| 70 | +try |
| 71 | +match entity.BaseTypewith |
| 72 | +| Some(TypeWithDefinition def)-> Some def |
| 73 | +|_-> None |
| 74 | +with_-> None |
| 75 | + |
| 76 | +let recisAttributeType(ty:FSharpEntity option)= |
| 77 | +match tywith |
| 78 | +| None->false |
| 79 | +| Some ty-> |
| 80 | +try ty.FullName="System.Attribute"|| isAttributeType(getBaseType ty) |
| 81 | +with_->false |
| 82 | + isAttributeType(Some entity) |
| 83 | +if isAttribute entitythen Some()else None |
| 84 | + |
| 85 | +lethasAttribute<'T>(attributes:seq<FSharpAttribute>)= |
| 86 | + attributes|> Seq.exists isAttribute<'T> |
| 87 | + |
| 88 | +let(|ValueType|_|)(e:FSharpEntity)= |
| 89 | +if e.IsEnum|| e.IsValueType|| hasAttribute<MeasureAnnotatedAbbreviationAttribute> e.Attributesthen Some() |
| 90 | +else None |
| 91 | + |
| 92 | +let(|Class|_|)(original:FSharpEntity,abbreviated:FSharpEntity,_)= |
| 93 | +if abbreviated.IsClass |
| 94 | +&&(not abbreviated.IsStaticInstantiation|| original.IsFSharpAbbreviation)then Some() |
| 95 | +else None |
| 96 | + |
| 97 | +let(|Record|_|)(e:FSharpEntity)=if e.IsFSharpRecordthen Some()else None |
| 98 | +let(|UnionType|_|)(e:FSharpEntity)=if e.IsFSharpUnionthen Some()else None |
| 99 | +let(|Delegate|_|)(e:FSharpEntity)=if e.IsDelegatethen Some()else None |
| 100 | +let(|FSharpException|_|)(e:FSharpEntity)=if e.IsFSharpExceptionDeclarationthen Some()else None |
| 101 | +let(|Interface|_|)(e:FSharpEntity)=if e.IsInterfacethen Some()else None |
| 102 | +let(|AbstractClass|_|)(e:FSharpEntity)= |
| 103 | +if hasAttribute<AbstractClassAttribute> e.Attributesthen Some()else None |
| 104 | + |
| 105 | +let(|FSharpType|_|)(e:FSharpEntity)= |
| 106 | +if e.IsDelegate|| e.IsFSharpExceptionDeclaration|| e.IsFSharpRecord|| e.IsFSharpUnion |
| 107 | +|| e.IsInterface|| e.IsMeasure |
| 108 | +||(e.IsFSharp&& e.IsOpaque&¬ e.IsFSharpModule&¬ e.IsNamespace)then Some() |
| 109 | +else None |
| 110 | + |
| 111 | +let(|ProvidedType|_|)(e:FSharpEntity)= |
| 112 | +if(e.IsProvided|| e.IsProvidedAndErased|| e.IsProvidedAndGenerated)&& e.CompiledName= e.DisplayNamethen |
| 113 | + Some() |
| 114 | +else None |
| 115 | + |
| 116 | +let(|ByRef|_|)(e:FSharpEntity)=if e.IsByRefthen Some()else None |
| 117 | +let(|Array|_|)(e:FSharpEntity)=if e.IsArrayTypethen Some()else None |
| 118 | +let(|FSharpModule|_|)(entity:FSharpEntity)=if entity.IsFSharpModulethen Some()else None |
| 119 | + |
| 120 | +let(|Namespace|_|)(entity:FSharpEntity)=if entity.IsNamespacethen Some()else None |
| 121 | +let(|ProvidedAndErasedType|_|)(entity:FSharpEntity)=if entity.IsProvidedAndErasedthen Some()else None |
| 122 | +let(|Enum|_|)(entity:FSharpEntity)=if entity.IsEnumthen Some()else None |
| 123 | + |
| 124 | +let(|Tuple|_|)(ty:FSharpType option)= |
| 125 | + ty|> Option.bind(fun ty->if ty.IsTupleTypethen Some()else None) |
| 126 | + |
| 127 | +let(|RefCell|_|)(ty:FSharpType)= |
| 128 | +match getAbbreviatedType tywith |
| 129 | +| TypeWithDefinition defwhen |
| 130 | + def.IsFSharpRecord&& def.FullName="Microsoft.FSharp.Core.FSharpRef`1"-> Some() |
| 131 | +|_-> None |
| 132 | + |
| 133 | +let(|FunctionType|_|)(ty:FSharpType)= |
| 134 | +if ty.IsFunctionTypethen Some() |
| 135 | +else None |
| 136 | + |
| 137 | +let(|Pattern|_|)(symbol:FSharpSymbol)= |
| 138 | +match symbolwith |
| 139 | +|:? FSharpUnionCase |
| 140 | +|:? FSharpActivePatternCase-> Some() |
| 141 | +|_-> None |
| 142 | + |
| 143 | +/// Field (field, fieldAbbreviatedType) |
| 144 | +let(|Field|_|)(symbol:FSharpSymbol)= |
| 145 | +match symbolwith |
| 146 | +|:? FSharpFieldas field-> Some(field, getAbbreviatedType field.FieldType) |
| 147 | +|_-> None |
| 148 | + |
| 149 | +let(|MutableVar|_|)(symbol:FSharpSymbol)= |
| 150 | +letisMutable= |
| 151 | +match symbolwith |
| 152 | +|:? FSharpFieldas field-> field.IsMutable&¬ field.IsLiteral |
| 153 | +|:? FSharpMemberOrFunctionOrValueas func-> func.IsMutable |
| 154 | +|_->false |
| 155 | +if isMutablethen Some()else None |
| 156 | + |
| 157 | +/// Entity (originalEntity, abbreviatedEntity, abbreviatedType) |
| 158 | +let(|FSharpEntity|_|)(symbol:FSharpSymbol)= |
| 159 | +match symbolwith |
| 160 | +|:? FSharpEntityas entity-> |
| 161 | +letabbreviatedEntity,abbreviatedType= getEntityAbbreviatedType entity |
| 162 | + Some(entity, abbreviatedEntity, abbreviatedType) |
| 163 | +|_-> None |
| 164 | + |
| 165 | +let(|Parameter|_|)(symbol:FSharpSymbol)= |
| 166 | +match symbolwith |
| 167 | +|:? FSharpParameter-> Some() |
| 168 | +|_-> None |
| 169 | + |
| 170 | +let(|UnionCase|_|)(e:FSharpSymbol)= |
| 171 | +match ewith |
| 172 | +|:? FSharpUnionCaseas uc-> Some uc |
| 173 | +|_-> None |
| 174 | + |
| 175 | +let(|RecordField|_|)(e:FSharpSymbol)= |
| 176 | +match ewith |
| 177 | +|:? FSharpFieldas field-> |
| 178 | +if field.DeclaringEntity.IsFSharpRecordthen Some fieldelse None |
| 179 | +|_-> None |
| 180 | + |
| 181 | +let(|ActivePatternCase|_|)(symbol:FSharpSymbol)= |
| 182 | +match symbolwith |
| 183 | +|:? FSharpActivePatternCaseas case-> Some case |
| 184 | +|_-> None |
| 185 | + |
| 186 | +/// Func (memberFunctionOrValue, fullType) |
| 187 | +let(|MemberFunctionOrValue|_|)(symbol:FSharpSymbol)= |
| 188 | +match symbolwith |
| 189 | +|:? FSharpMemberOrFunctionOrValueas func-> Some func |
| 190 | +|_-> None |
| 191 | + |
| 192 | +/// Constructor (enclosingEntity) |
| 193 | +let(|Constructor|_|)(func:FSharpMemberOrFunctionOrValue)= |
| 194 | +match func.CompiledNamewith |
| 195 | +|".ctor"|".cctor"-> func.EnclosingEntity |
| 196 | +|_-> None |
| 197 | + |
| 198 | +let(|Function|_|)excluded(func:FSharpMemberOrFunctionOrValue)= |
| 199 | +trylettyp= func.FullType|> getAbbreviatedType |
| 200 | +if typ.IsFunctionType |
| 201 | +&¬ func.IsPropertyGetterMethod |
| 202 | +&¬ func.IsPropertySetterMethod |
| 203 | +&¬ excluded |
| 204 | +&¬(isOperator func.DisplayName)then Some() |
| 205 | +else None |
| 206 | +with_-> None |
| 207 | + |
| 208 | +let(|ExtensionMember|_|)(func:FSharpMemberOrFunctionOrValue)= |
| 209 | +if func.IsExtensionMemberthen Some()else None |
| 210 | + |
| 211 | +let(|Event|_|)(func:FSharpMemberOrFunctionOrValue)= |
| 212 | +if func.IsEventthen Some()else None |
| 213 | + |
5 | 214 | /// Active patterns over `FSharpSymbolUse`. |
6 | 215 | [<RequireQualifiedAccess>] |
7 | 216 | moduleSymbolUse= |
|