@@ -36,7 +36,7 @@ open FSharp.Compiler.TypeProviders
3636#endif
3737
3838[<AutoOpen>]
39- module internal TypeEncoding =
39+ module internal XmlDocSignatures =
4040
4141 let commaEncs strs = String.concat " ," strs
4242 let angleEnc str = " {" + str + " }"
@@ -228,6 +228,103 @@ module internal TypeEncoding =
228228 let XmlDocSigOfEntity ( eref : EntityRef ) =
229229 XmlDocSigOfTycon [ ( buildAccessPath eref.CompilationPathOpt); eref.Deref.CompiledName ]
230230
231+ //---------------------------------------------------------------------------
232+ // Active pattern name helpers
233+ //---------------------------------------------------------------------------
234+
235+ let TryGetActivePatternInfo ( vref : ValRef ) =
236+ // First is an optimization to prevent calls to string routines
237+ let logicalName = vref.LogicalName
238+
239+ if logicalName.Length = 0 || logicalName[ 0 ] <> '|' then
240+ None
241+ else
242+ ActivePatternInfoOfValName vref.DisplayNameCoreMangled vref.Range
243+
244+ type ActivePatternElemRef with
245+ member x.LogicalName =
246+ let ( APElemRef ( _ , vref , n , _ )) = x
247+
248+ match TryGetActivePatternInfo vref with
249+ | None -> error ( InternalError( " not an active pattern name" , vref.Range))
250+ | Some apinfo ->
251+ let nms = apinfo.ActiveTags
252+
253+ if n < 0 || n >= List.length nms then
254+ error ( InternalError( " name_of_apref: index out of range for active pattern reference" , vref.Range))
255+
256+ List.item n nms
257+
258+ member x.DisplayNameCore = x.LogicalName
259+
260+ member x.DisplayName = x.LogicalName |> ConvertLogicalNameToDisplayName
261+
262+ let mkChoiceTyconRef ( g : TcGlobals ) m n =
263+ match n with
264+ | 0
265+ | 1 -> error ( InternalError( " mkChoiceTyconRef" , m))
266+ | 2 -> g.choice2_ tcr
267+ | 3 -> g.choice3_ tcr
268+ | 4 -> g.choice4_ tcr
269+ | 5 -> g.choice5_ tcr
270+ | 6 -> g.choice6_ tcr
271+ | 7 -> g.choice7_ tcr
272+ | _ -> error ( Error( FSComp.SR.tastActivePatternsLimitedToSeven (), m))
273+
274+ let mkChoiceTy ( g : TcGlobals ) m tinst =
275+ match List.length tinst with
276+ | 0 -> g.unit_ ty
277+ | 1 -> List.head tinst
278+ | length -> mkWoNullAppTy ( mkChoiceTyconRef g m length) tinst
279+
280+ let mkChoiceCaseRef g m n i =
281+ mkUnionCaseRef ( mkChoiceTyconRef g m n) ( " Choice" + string ( i + 1 ) + " Of" + string n)
282+
283+ type ActivePatternInfo with
284+
285+ member x.DisplayNameCoreByIdx idx = x.ActiveTags[ idx]
286+
287+ member x.DisplayNameByIdx idx =
288+ x.ActiveTags[ idx] |> ConvertLogicalNameToDisplayName
289+
290+ member apinfo.ResultType g m retTys retKind =
291+ let choicety = mkChoiceTy g m retTys
292+
293+ if apinfo.IsTotal then
294+ choicety
295+ else
296+ match retKind with
297+ | ActivePatternReturnKind.RefTypeWrapper -> mkOptionTy g choicety
298+ | ActivePatternReturnKind.StructTypeWrapper -> mkValueOptionTy g choicety
299+ | ActivePatternReturnKind.Boolean -> g.bool_ ty
300+
301+ member apinfo.OverallType g m argTy retTys retKind =
302+ mkFunTy g argTy ( apinfo.ResultType g m retTys retKind)
303+
304+ //---------------------------------------------------------------------------
305+ // Active pattern validation
306+ //---------------------------------------------------------------------------
307+
308+ // check if an active pattern takes type parameters only bound by the return types,
309+ // not by their argument types.
310+ let doesActivePatternHaveFreeTypars g ( v : ValRef ) =
311+ let vty = v.TauType
312+ let vtps = v.Typars |> Zset.ofList typarOrder
313+
314+ if not ( isFunTy g v.TauType) then
315+ errorR ( Error( FSComp.SR.activePatternIdentIsNotFunctionTyped ( v.LogicalName), v.Range))
316+
317+ let argTys , resty = stripFunTy g vty
318+
319+ let argtps , restps =
320+ ( freeInTypes CollectTypars argTys) .FreeTypars, ( freeInType CollectTypars resty) .FreeTypars
321+ // Error if an active pattern is generic in type variables that only occur in the result Choice<_, ...>.
322+ // Note: The test restricts to v.Typars since typars from the closure are considered fixed.
323+ not ( Zset.isEmpty ( Zset.inter ( Zset.diff restps argtps) vtps))
324+
325+ [<AutoOpen>]
326+ module internal NullnessAnalysis =
327+
231328 let inline HasConstraint ( [<InlineIfLambda>] predicate ) ( tp : Typar ) = tp.Constraints |> List.exists predicate
232329
233330 let inline tryGetTyparTyWithConstraint g ( [<InlineIfLambda>] predicate ) ty =
@@ -458,6 +555,27 @@ module internal TypeEncoding =
458555
459556 let TypeHasDefaultValueNew g m ty = TypeHasDefaultValueAux true g m ty
460557
558+ let (| TyparTy | NullableTypar | StructTy | NullTrueValue | NullableRefType | WithoutNullRefType | UnresolvedRefType |) ( ty , g ) =
559+ let sty = ty |> stripTyEqns g
560+
561+ if isTyparTy g sty then
562+ if ( nullnessOfTy g sty) .TryEvaluate() = ValueSome NullnessInfo.WithNull then
563+ NullableTypar
564+ else
565+ TyparTy
566+ elif isStructTy g sty then
567+ StructTy
568+ elif TypeNullIsTrueValue g sty then
569+ NullTrueValue
570+ else
571+ match ( nullnessOfTy g sty) .TryEvaluate() with
572+ | ValueSome NullnessInfo.WithNull -> NullableRefType
573+ | ValueSome NullnessInfo.WithoutNull -> WithoutNullRefType
574+ | _ -> UnresolvedRefType
575+
576+ [<AutoOpen>]
577+ module internal TypeTestsAndPatterns =
578+
461579 /// Determines types that are potentially known to satisfy the 'comparable' constraint and returns
462580 /// a set of residual types that must also satisfy the constraint
463581 [<return : Struct>]
@@ -489,24 +607,6 @@ module internal TypeEncoding =
489607 let (| SpecialNotEquatableHeadType | _ |) g ty =
490608 if isFunTy g ty then ValueSome() else ValueNone
491609
492- let (| TyparTy | NullableTypar | StructTy | NullTrueValue | NullableRefType | WithoutNullRefType | UnresolvedRefType |) ( ty , g ) =
493- let sty = ty |> stripTyEqns g
494-
495- if isTyparTy g sty then
496- if ( nullnessOfTy g sty) .TryEvaluate() = ValueSome NullnessInfo.WithNull then
497- NullableTypar
498- else
499- TyparTy
500- elif isStructTy g sty then
501- StructTy
502- elif TypeNullIsTrueValue g sty then
503- NullTrueValue
504- else
505- match ( nullnessOfTy g sty) .TryEvaluate() with
506- | ValueSome NullnessInfo.WithNull -> NullableRefType
507- | ValueSome NullnessInfo.WithoutNull -> WithoutNullRefType
508- | _ -> UnresolvedRefType
509-
510610 // Can we use the fast helper for the 'LanguagePrimitives.IntrinsicFunctions.TypeTestGeneric'?
511611 let canUseTypeTestFast g ty =
512612 not ( isTyparTy g ty) && not ( TypeNullIsTrueValue g ty)
@@ -615,100 +715,6 @@ module internal TypeEncoding =
615715 numEnclTypeArgs, virtualCall, isNewObj, isSuperInit, isSelfInit, takesInstanceArg, isPropGet, isPropSet
616716 | _ -> 0 , false , false , false , false , false , false , false
617717
618- //---------------------------------------------------------------------------
619- // Active pattern name helpers
620- //---------------------------------------------------------------------------
621-
622- let TryGetActivePatternInfo ( vref : ValRef ) =
623- // First is an optimization to prevent calls to string routines
624- let logicalName = vref.LogicalName
625-
626- if logicalName.Length = 0 || logicalName[ 0 ] <> '|' then
627- None
628- else
629- ActivePatternInfoOfValName vref.DisplayNameCoreMangled vref.Range
630-
631- type ActivePatternElemRef with
632- member x.LogicalName =
633- let ( APElemRef ( _ , vref , n , _ )) = x
634-
635- match TryGetActivePatternInfo vref with
636- | None -> error ( InternalError( " not an active pattern name" , vref.Range))
637- | Some apinfo ->
638- let nms = apinfo.ActiveTags
639-
640- if n < 0 || n >= List.length nms then
641- error ( InternalError( " name_of_apref: index out of range for active pattern reference" , vref.Range))
642-
643- List.item n nms
644-
645- member x.DisplayNameCore = x.LogicalName
646-
647- member x.DisplayName = x.LogicalName |> ConvertLogicalNameToDisplayName
648-
649- let mkChoiceTyconRef ( g : TcGlobals ) m n =
650- match n with
651- | 0
652- | 1 -> error ( InternalError( " mkChoiceTyconRef" , m))
653- | 2 -> g.choice2_ tcr
654- | 3 -> g.choice3_ tcr
655- | 4 -> g.choice4_ tcr
656- | 5 -> g.choice5_ tcr
657- | 6 -> g.choice6_ tcr
658- | 7 -> g.choice7_ tcr
659- | _ -> error ( Error( FSComp.SR.tastActivePatternsLimitedToSeven (), m))
660-
661- let mkChoiceTy ( g : TcGlobals ) m tinst =
662- match List.length tinst with
663- | 0 -> g.unit_ ty
664- | 1 -> List.head tinst
665- | length -> mkWoNullAppTy ( mkChoiceTyconRef g m length) tinst
666-
667- let mkChoiceCaseRef g m n i =
668- mkUnionCaseRef ( mkChoiceTyconRef g m n) ( " Choice" + string ( i + 1 ) + " Of" + string n)
669-
670- type ActivePatternInfo with
671-
672- member x.DisplayNameCoreByIdx idx = x.ActiveTags[ idx]
673-
674- member x.DisplayNameByIdx idx =
675- x.ActiveTags[ idx] |> ConvertLogicalNameToDisplayName
676-
677- member apinfo.ResultType g m retTys retKind =
678- let choicety = mkChoiceTy g m retTys
679-
680- if apinfo.IsTotal then
681- choicety
682- else
683- match retKind with
684- | ActivePatternReturnKind.RefTypeWrapper -> mkOptionTy g choicety
685- | ActivePatternReturnKind.StructTypeWrapper -> mkValueOptionTy g choicety
686- | ActivePatternReturnKind.Boolean -> g.bool_ ty
687-
688- member apinfo.OverallType g m argTy retTys retKind =
689- mkFunTy g argTy ( apinfo.ResultType g m retTys retKind)
690-
691- //---------------------------------------------------------------------------
692- // Active pattern validation
693- //---------------------------------------------------------------------------
694-
695- // check if an active pattern takes type parameters only bound by the return types,
696- // not by their argument types.
697- let doesActivePatternHaveFreeTypars g ( v : ValRef ) =
698- let vty = v.TauType
699- let vtps = v.Typars |> Zset.ofList typarOrder
700-
701- if not ( isFunTy g v.TauType) then
702- errorR ( Error( FSComp.SR.activePatternIdentIsNotFunctionTyped ( v.LogicalName), v.Range))
703-
704- let argTys , resty = stripFunTy g vty
705-
706- let argtps , restps =
707- ( freeInTypes CollectTypars argTys) .FreeTypars, ( freeInType CollectTypars resty) .FreeTypars
708- // Error if an active pattern is generic in type variables that only occur in the result Choice<_, ...>.
709- // Note: The test restricts to v.Typars since typars from the closure are considered fixed.
710- not ( Zset.isEmpty ( Zset.inter ( Zset.diff restps argtps) vtps))
711-
712718[<AutoOpen>]
713719module internal Rewriting =
714720
0 commit comments