Skip to content

Commit

Permalink
Remove second internal ValueOption
Browse files Browse the repository at this point in the history
  • Loading branch information
forki committed Oct 1, 2018
1 parent 7f7ef16 commit 8147a39
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 31 deletions.
22 changes: 6 additions & 16 deletions src/absil/illib.fs
Original file line number Diff line number Diff line change
Expand Up @@ -432,18 +432,12 @@ module List =
let existsSquared f xss = xss |> List.exists (fun xs -> xs |> List.exists (fun x -> f x))
let mapiFoldSquared f z xss = mapFoldSquared f z (xss |> mapiSquared (fun i j x -> (i,j,x)))

[<Struct>]
type ValueOption<'T> =
| ValueSome of 'T
| ValueNone
member x.IsSome = match x with ValueSome _ -> true | ValueNone -> false
member x.IsNone = match x with ValueSome _ -> false | ValueNone -> true
member x.Value = match x with ValueSome r -> r | ValueNone -> failwith "ValueOption.Value: value is None"

[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module ValueOption =
let inline ofOption x = match x with Some x -> ValueSome x | None -> ValueNone
let inline bind f x = match x with ValueSome x -> f x | ValueNone -> ValueNone
let inline isSome x = match x with ValueSome _ -> true | ValueNone -> false
let inline isNone x = match x with ValueSome _ -> false | ValueNone -> true

type String with
member inline x.StartsWithOrdinal(value) =
Expand Down Expand Up @@ -1133,7 +1127,7 @@ module NameMap =
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module NameMultiMap =
let existsInRange f (m: NameMultiMap<'T>) = NameMap.exists (fun _ l -> List.exists f l) m
let find v (m: NameMultiMap<'T>) = match Map.tryFind v m with None -> [] | Some r -> r
let find v (m: NameMultiMap<'T>) = match m.TryGetValue v with true, r -> r | _ -> []
let add v x (m: NameMultiMap<'T>) = NameMap.add v (x :: find v m) m
let range (m: NameMultiMap<'T>) = Map.foldBack (fun _ x sofar -> x @ sofar) m []
let rangeReversingEachBucket (m: NameMultiMap<'T>) = Map.foldBack (fun _ x sofar -> List.rev x @ sofar) m []
Expand All @@ -1147,7 +1141,7 @@ module NameMultiMap =
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module MultiMap =
let existsInRange f (m: MultiMap<_,_>) = Map.exists (fun _ l -> List.exists f l) m
let find v (m: MultiMap<_,_>) = match Map.tryFind v m with None -> [] | Some r -> r
let find v (m: MultiMap<_,_>) = match m.TryGetValue v with true, r -> r | _ -> []
let add v x (m: MultiMap<_,_>) = Map.add v (x :: find v m) m
let range (m: MultiMap<_,_>) = Map.foldBack (fun _ x sofar -> x @ sofar) m []
let empty : MultiMap<_,_> = Map.empty
Expand All @@ -1158,11 +1152,6 @@ type LayeredMap<'Key,'Value when 'Key : comparison> = Map<'Key,'Value>
type Map<'Key,'Value when 'Key : comparison> with
static member Empty : Map<'Key,'Value> = Map.empty

member m.TryGetValue (key,res:byref<'Value>) =
match m.TryFind key with
| None -> false
| Some r -> res <- r; true

member x.Values = [ for (KeyValue(_,v)) in x -> v ]
member x.AddAndMarkAsCollapsible (kvs: _[]) = (x,kvs) ||> Array.fold (fun x (KeyValue(k,v)) -> x.Add(k,v))
member x.LinearTryModifyThenLaterFlatten (key, f: 'Value option -> 'Value) = x.Add (key, f (x.TryFind key))
Expand All @@ -1172,12 +1161,13 @@ type Map<'Key,'Value when 'Key : comparison> with
[<Sealed>]
type LayeredMultiMap<'Key,'Value when 'Key : equality and 'Key : comparison>(contents : LayeredMap<'Key,'Value list>) =
member x.Add (k,v) = LayeredMultiMap(contents.Add(k,v :: x.[k]))
member x.Item with get k = match contents.TryFind k with None -> [] | Some l -> l
member x.Item with get k = match contents.TryGetValue k with true, l -> l | _ -> []
member x.AddAndMarkAsCollapsible (kvs: _[]) =
let x = (x,kvs) ||> Array.fold (fun x (KeyValue(k,v)) -> x.Add(k,v))
x.MarkAsCollapsible()
member x.MarkAsCollapsible() = LayeredMultiMap(contents.MarkAsCollapsible())
member x.TryFind k = contents.TryFind k
member x.TryGetValue k = contents.TryGetValue k
member x.Values = contents.Values |> List.concat
static member Empty : LayeredMultiMap<'Key,'Value> = LayeredMultiMap LayeredMap.Empty

Expand Down
2 changes: 1 addition & 1 deletion src/fsharp/NameResolution.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,7 @@ let AddEntityForProvidedType (amap: Import.ImportMap, modref: ModuleOrNamespaceR
let tycon = Construct.NewProvidedTycon(resolutionEnvironment, st, importProvidedType, isSuppressRelocate, m)
modref.ModuleOrNamespaceType.AddProvidedTypeEntity(tycon)
let tcref = modref.NestedTyconRef tycon
System.Diagnostics.Debug.Assert modref.TryDeref.IsSome
System.Diagnostics.Debug.Assert(ValueOption.isSome modref.TryDeref)
tcref


Expand Down
2 changes: 1 addition & 1 deletion src/fsharp/Optimizer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3090,7 +3090,7 @@ and OptimizeBinding cenv isRec env (TBind(vref, expr, spBind)) =
| None -> false
| Some mbrTyconRef ->
// Check we can deref system_MarshalByRefObject_tcref. When compiling against the Silverlight mscorlib we can't
if mbrTyconRef.TryDeref.IsSome then
if ValueOption.isSome mbrTyconRef.TryDeref then
// Check if this is a subtype of MarshalByRefObject
assert (cenv.g.system_MarshalByRefObject_ty.IsSome)
ExistsSameHeadTypeInHierarchy cenv.g cenv.amap vref.Range (generalizedTyconRef tcref) cenv.g.system_MarshalByRefObject_ty.Value
Expand Down
2 changes: 1 addition & 1 deletion src/fsharp/QuotationTranslator.fs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type QuotationGenerationScope =

static member ComputeQuotationFormat g =
let deserializeExValRef = ValRefForIntrinsic g.deserialize_quoted_FSharp_40_plus_info
if deserializeExValRef.TryDeref.IsSome then
if ValueOption.isSome deserializeExValRef.TryDeref then
QuotationSerializationFormat.FSharp_40_Plus
else
QuotationSerializationFormat.FSharp_20_Plus
Expand Down
16 changes: 8 additions & 8 deletions src/fsharp/symbols/Symbols.fs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ module Impl =
let entityIsUnresolved(entity:EntityRef) =
match entity with
| ERefNonLocal(NonLocalEntityRef(ccu, _)) ->
ccu.IsUnresolvedReference && entity.TryDeref.IsNone
ccu.IsUnresolvedReference && ValueOption.isNone entity.TryDeref
| _ -> false

let checkEntityIsResolved(entity:EntityRef) =
Expand Down Expand Up @@ -754,10 +754,10 @@ and FSharpUnionCase(cenv, v: UnionCaseRef) =


let isUnresolved() =
entityIsUnresolved v.TyconRef || v.TryUnionCase.IsNone
entityIsUnresolved v.TyconRef || ValueOption.isNone v.TryUnionCase
let checkIsResolved() =
checkEntityIsResolved v.TyconRef
if v.TryUnionCase.IsNone then
if ValueOption.isNone v.TryUnionCase then
invalidOp (sprintf "The union case '%s' could not be found in the target type" v.CaseName)

member __.IsUnresolved =
Expand Down Expand Up @@ -854,18 +854,18 @@ and FSharpField(cenv: SymbolEnv, d: FSharpFieldData) =
let isUnresolved() =
entityIsUnresolved d.DeclaringTyconRef ||
match d with
| RecdOrClass v -> v.TryRecdField.IsNone
| Union (v, _) -> v.TryUnionCase.IsNone
| RecdOrClass v -> ValueOption.isNone v.TryRecdField
| Union (v, _) -> ValueOption.isNone v.TryUnionCase
| ILField _ -> false

let checkIsResolved() =
checkEntityIsResolved d.DeclaringTyconRef
match d with
| RecdOrClass v ->
if v.TryRecdField.IsNone then
if ValueOption.isNone v.TryRecdField then
invalidOp (sprintf "The record field '%s' could not be found in the target type" v.FieldName)
| Union (v, _) ->
if v.TryUnionCase.IsNone then
if ValueOption.isNone v.TryUnionCase then
invalidOp (sprintf "The union case '%s' could not be found in the target type" v.CaseName)
| ILField _ -> ()

Expand Down Expand Up @@ -1331,7 +1331,7 @@ and FSharpMemberOrFunctionOrValue(cenv, d:FSharpMemberOrValData, item) =
let isUnresolved() =
match fsharpInfo() with
| None -> false
| Some v -> v.TryDeref.IsNone
| Some v -> ValueOption.isNone v.TryDeref

let checkIsResolved() =
if isUnresolved() then
Expand Down
8 changes: 4 additions & 4 deletions src/fsharp/tast.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3260,7 +3260,7 @@ and
ValueSome tcr.binding

/// Is the destination assembly available?
member tcr.CanDeref = tcr.TryDeref.IsSome
member tcr.CanDeref = ValueOption.isSome tcr.TryDeref

/// Gets the data indicating the compiled representation of a type or module in terms of Abstract IL data structures.
member x.CompiledRepresentation = x.Deref.CompiledRepresentation
Expand Down Expand Up @@ -3811,7 +3811,7 @@ and
| None -> error(InternalError(sprintf "union case %s not found in type %s" x.CaseName x.TyconRef.LogicalName, x.TyconRef.Range))

/// Try to dereference the reference
member x.TryUnionCase = x.TyconRef.TryDeref |> ValueOption.bind (fun tcref -> tcref.GetUnionCaseByName x.CaseName |> ValueOption.ofOption)
member x.TryUnionCase = x.TyconRef.TryDeref |> ValueOption.bind (fun tcref -> tcref.GetUnionCaseByName x.CaseName |> ValueOption.ofOption)

/// Get the attributes associated with the union case
member x.Attribs = x.UnionCase.Attribs
Expand Down Expand Up @@ -5448,7 +5448,7 @@ let primEntityRefEq compilingFslib fslibCcu (x : EntityRef) (y : EntityRef) =
(not (nonLocalRefDefinitelyNotEq x.nlr y.nlr) &&
let v1 = x.TryDeref
let v2 = y.TryDeref
v1.IsSome && v2.IsSome && v1.Value === v2.Value)) then
ValueOption.isSome v1 && ValueOption.isSome v2 && v1.Value === v2.Value)) then
true
else
compilingFslib && fslibEntityRefEq fslibCcu x y
Expand All @@ -5474,7 +5474,7 @@ let primValRefEq compilingFslib fslibCcu (x : ValRef) (y : ValRef) =
// e.g. CompactFramework doesn't have support for quotations.
let v1 = x.TryDeref
let v2 = y.TryDeref
v1.IsSome && v2.IsSome && v1.Value === v2.Value)
ValueOption.isSome v1 && ValueOption.isSome v2 && v1.Value === v2.Value)
|| (if compilingFslib then fslibValRefEq fslibCcu x y else false)

//---------------------------------------------------------------------------
Expand Down

0 comments on commit 8147a39

Please sign in to comment.