Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 84 additions & 42 deletions src/Compiler/AbstractIL/il.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1229,19 +1229,75 @@ type ILAttributes(array: ILAttribute[]) =

static member val internal Empty = ILAttributes([||])

[<NoEquality; NoComparison>]
type ILAttributesStored =

/// Computed by ilread.fs based on metadata index
[<Flags>]
type WellKnownILAttributes =
| None = 0u
| IsReadOnlyAttribute = (1u <<< 0)
| IsUnmanagedAttribute = (1u <<< 1)
| IsByRefLikeAttribute = (1u <<< 2)
| ExtensionAttribute = (1u <<< 3)
| NullableAttribute = (1u <<< 4)
| ParamArrayAttribute = (1u <<< 5)
| AllowNullLiteralAttribute = (1u <<< 6)
| ReflectedDefinitionAttribute = (1u <<< 7)
| AutoOpenAttribute = (1u <<< 8)
| InternalsVisibleToAttribute = (1u <<< 9)
| CallerMemberNameAttribute = (1u <<< 10)
| CallerFilePathAttribute = (1u <<< 11)
| CallerLineNumberAttribute = (1u <<< 12)
| IDispatchConstantAttribute = (1u <<< 13)
| IUnknownConstantAttribute = (1u <<< 14)
| RequiresLocationAttribute = (1u <<< 15)
| SetsRequiredMembersAttribute = (1u <<< 16)
| NoEagerConstraintApplicationAttribute = (1u <<< 17)
| DefaultMemberAttribute = (1u <<< 18)
| ObsoleteAttribute = (1u <<< 19)
| CompilerFeatureRequiredAttribute = (1u <<< 20)
| ExperimentalAttribute = (1u <<< 21)
| RequiredMemberAttribute = (1u <<< 22)
| NullableContextAttribute = (1u <<< 23)
| AttributeUsageAttribute = (1u <<< 24)
| NotComputed = (1u <<< 31)

type internal ILAttributesStoredRepr =
| Reader of (int32 -> ILAttribute[])

/// Already computed
| Given of ILAttributes

member x.GetCustomAttrs metadataIndex =
match x with
| Reader f -> ILAttributes(f metadataIndex)
| Given attrs -> attrs
[<Sealed; NoEquality; NoComparison>]
type ILAttributesStored private (metadataIndex: int32, initial: ILAttributesStoredRepr) =
[<VolatileField>]
let mutable repr = initial

[<VolatileField>]
let mutable wellKnownFlags = WellKnownILAttributes.NotComputed

member _.MetadataIndex = metadataIndex

member x.CustomAttrs: ILAttributes =
match repr with
| Given a -> a
| Reader f ->
let r = ILAttributes(f metadataIndex)
repr <- Given r
r

member x.HasWellKnownAttribute(flag: WellKnownILAttributes, compute: ILAttributes -> WellKnownILAttributes) : bool =
x.GetOrComputeWellKnownFlags(compute) &&& flag <> WellKnownILAttributes.None

member x.GetOrComputeWellKnownFlags(compute: ILAttributes -> WellKnownILAttributes) : WellKnownILAttributes =
let f = wellKnownFlags

if f <> WellKnownILAttributes.NotComputed then
f
else
let a = x.CustomAttrs
let computed = compute a
wellKnownFlags <- computed
computed

static member CreateReader(idx: int32, f: int32 -> ILAttribute[]) = ILAttributesStored(idx, Reader f)

static member CreateGiven(attrs: ILAttributes) = ILAttributesStored(-1, Given attrs)

let emptyILCustomAttrs = ILAttributes [||]

Expand All @@ -1256,18 +1312,18 @@ let mkILCustomAttrs l =
| [] -> emptyILCustomAttrs
| _ -> mkILCustomAttrsFromArray (List.toArray l)

let emptyILCustomAttrsStored = ILAttributesStored.Given emptyILCustomAttrs
let emptyILCustomAttrsStored = ILAttributesStored.CreateGiven emptyILCustomAttrs

let storeILCustomAttrs (attrs: ILAttributes) =
if attrs.AsArray().Length = 0 then
emptyILCustomAttrsStored
else
ILAttributesStored.Given attrs
ILAttributesStored.CreateGiven attrs

let mkILCustomAttrsComputed f =
ILAttributesStored.Reader(fun _ -> f ())
ILAttributesStored.CreateReader(-1, fun _ -> f ())

let mkILCustomAttrsReader f = ILAttributesStored.Reader f
let mkILCustomAttrsReader f = ILAttributesStored.CreateReader(-1, f)

type ILCodeLabel = int

Expand Down Expand Up @@ -1791,7 +1847,7 @@ type ILParameter =
MetadataIndex: int32
}

member x.CustomAttrs = x.CustomAttrsStored.GetCustomAttrs x.MetadataIndex
member x.CustomAttrs = x.CustomAttrsStored.CustomAttrs

override x.ToString() =
x.Name |> Option.defaultValue "<no name>"
Expand All @@ -1809,7 +1865,7 @@ type ILReturn =

override x.ToString() = "<return>"

member x.CustomAttrs = x.CustomAttrsStored.GetCustomAttrs x.MetadataIndex
member x.CustomAttrs = x.CustomAttrsStored.CustomAttrs

member x.WithCustomAttrs(customAttrs) =
{ x with
Expand Down Expand Up @@ -1870,7 +1926,7 @@ type ILGenericParameterDef =
MetadataIndex: int32
}

member x.CustomAttrs = x.CustomAttrsStored.GetCustomAttrs x.MetadataIndex
member x.CustomAttrs = x.CustomAttrsStored.CustomAttrs

/// For debugging
[<DebuggerBrowsable(DebuggerBrowsableState.Never)>]
Expand Down Expand Up @@ -1916,13 +1972,7 @@ type InterfaceImpl =
mutable CustomAttrsStored: ILAttributesStored
}

member x.CustomAttrs =
match x.CustomAttrsStored with
| ILAttributesStored.Reader f ->
let res = ILAttributes(f x.Idx)
x.CustomAttrsStored <- ILAttributesStored.Given res
res
| ILAttributesStored.Given attrs -> attrs
member x.CustomAttrs = x.CustomAttrsStored.CustomAttrs

static member Create(ilType: ILType, customAttrsStored: ILAttributesStored) =
{
Expand Down Expand Up @@ -2029,7 +2079,7 @@ type ILMethodDef
| Some attrs -> attrs)
)

member x.CustomAttrs = x.CustomAttrsStored.GetCustomAttrs metadataIndex
member x.CustomAttrs = x.CustomAttrsStored.CustomAttrs

member x.SecurityDecls = x.SecurityDeclsStored.GetSecurityDecls x.MetadataIndex

Expand Down Expand Up @@ -2266,7 +2316,7 @@ type ILEventDef

member _.MetadataIndex = metadataIndex

member x.CustomAttrs = customAttrsStored.GetCustomAttrs x.MetadataIndex
member x.CustomAttrs = customAttrsStored.CustomAttrs

member x.With(?eventType, ?name, ?attributes, ?addMethod, ?removeMethod, ?fireMethod, ?otherMethods, ?customAttrs) =
ILEventDef(
Expand Down Expand Up @@ -2342,7 +2392,7 @@ type ILPropertyDef
member x.Init = init
member x.Args = args
member x.CustomAttrsStored = customAttrsStored
member x.CustomAttrs = customAttrsStored.GetCustomAttrs x.MetadataIndex
member x.CustomAttrs = customAttrsStored.CustomAttrs
member x.MetadataIndex = metadataIndex

member x.With(?name, ?attributes, ?setMethod, ?getMethod, ?callingConv, ?propertyType, ?init, ?args, ?customAttrs) =
Expand Down Expand Up @@ -2418,7 +2468,7 @@ type ILFieldDef
member _.Offset = offset
member _.Marshal = marshal
member x.CustomAttrsStored = customAttrsStored
member x.CustomAttrs = customAttrsStored.GetCustomAttrs x.MetadataIndex
member x.CustomAttrs = customAttrsStored.CustomAttrs
member x.MetadataIndex = metadataIndex

member x.With
Expand Down Expand Up @@ -2677,8 +2727,6 @@ type ILTypeDef
metadataIndex: int32
) =

let mutable customAttrsStored = customAttrsStored

let hasFlag flag = additionalFlags &&& flag = flag

new
Expand Down Expand Up @@ -2829,13 +2877,7 @@ type ILTypeDef
customAttrs = defaultArg customAttrs x.CustomAttrsStored
)

member x.CustomAttrs: ILAttributes =
match customAttrsStored with
| ILAttributesStored.Reader f ->
let res = ILAttributes(f x.MetadataIndex)
customAttrsStored <- ILAttributesStored.Given res
res
| ILAttributesStored.Given res -> res
member x.CustomAttrs: ILAttributes = customAttrsStored.CustomAttrs

member x.SecurityDecls = x.SecurityDeclsStored.GetSecurityDecls x.MetadataIndex

Expand Down Expand Up @@ -2993,7 +3035,7 @@ type ILNestedExportedType =
MetadataIndex: int32
}

member x.CustomAttrs = x.CustomAttrsStored.GetCustomAttrs x.MetadataIndex
member x.CustomAttrs = x.CustomAttrsStored.CustomAttrs

override x.ToString() = "exported type " + x.Name

Expand All @@ -3017,7 +3059,7 @@ and [<NoComparison; NoEquality>] ILExportedTypeOrForwarder =

member x.IsForwarder = x.Attributes &&& enum<TypeAttributes> 0x00200000 <> enum 0

member x.CustomAttrs = x.CustomAttrsStored.GetCustomAttrs x.MetadataIndex
member x.CustomAttrs = x.CustomAttrsStored.CustomAttrs

override x.ToString() = "exported type " + x.Name

Expand Down Expand Up @@ -3057,7 +3099,7 @@ type ILResource =
| ILResourceLocation.Local bytes -> bytes.GetByteMemory()
| _ -> failwith "GetBytes"

member x.CustomAttrs = x.CustomAttrsStored.GetCustomAttrs x.MetadataIndex
member x.CustomAttrs = x.CustomAttrsStored.CustomAttrs

override x.ToString() = "resource " + x.Name

Expand Down Expand Up @@ -3104,7 +3146,7 @@ type ILAssemblyManifest =
MetadataIndex: int32
}

member x.CustomAttrs = x.CustomAttrsStored.GetCustomAttrs x.MetadataIndex
member x.CustomAttrs = x.CustomAttrsStored.CustomAttrs

member x.SecurityDecls = x.SecurityDeclsStored.GetSecurityDecls x.MetadataIndex

Expand Down Expand Up @@ -3151,7 +3193,7 @@ type ILModuleDef =
| None -> false
| _ -> true

member x.CustomAttrs = x.CustomAttrsStored.GetCustomAttrs x.MetadataIndex
member x.CustomAttrs = x.CustomAttrsStored.CustomAttrs

override x.ToString() = "assembly " + x.Name

Expand Down
42 changes: 36 additions & 6 deletions src/Compiler/AbstractIL/il.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -878,15 +878,45 @@ type ILAttributes =

static member internal Empty: ILAttributes

[<Flags>]
type WellKnownILAttributes =
| None = 0u
| IsReadOnlyAttribute = (1u <<< 0)
| IsUnmanagedAttribute = (1u <<< 1)
| IsByRefLikeAttribute = (1u <<< 2)
| ExtensionAttribute = (1u <<< 3)
| NullableAttribute = (1u <<< 4)
| ParamArrayAttribute = (1u <<< 5)
| AllowNullLiteralAttribute = (1u <<< 6)
| ReflectedDefinitionAttribute = (1u <<< 7)
| AutoOpenAttribute = (1u <<< 8)
| InternalsVisibleToAttribute = (1u <<< 9)
| CallerMemberNameAttribute = (1u <<< 10)
| CallerFilePathAttribute = (1u <<< 11)
| CallerLineNumberAttribute = (1u <<< 12)
| IDispatchConstantAttribute = (1u <<< 13)
| IUnknownConstantAttribute = (1u <<< 14)
| RequiresLocationAttribute = (1u <<< 15)
| SetsRequiredMembersAttribute = (1u <<< 16)
| NoEagerConstraintApplicationAttribute = (1u <<< 17)
| DefaultMemberAttribute = (1u <<< 18)
| ObsoleteAttribute = (1u <<< 19)
| CompilerFeatureRequiredAttribute = (1u <<< 20)
| ExperimentalAttribute = (1u <<< 21)
| RequiredMemberAttribute = (1u <<< 22)
| NullableContextAttribute = (1u <<< 23)
| AttributeUsageAttribute = (1u <<< 24)
| NotComputed = (1u <<< 31)

/// Represents the efficiency-oriented storage of ILAttributes in another item.
[<NoEquality; NoComparison>]
[<Sealed; NoEquality; NoComparison>]
type ILAttributesStored =
/// Computed by ilread.fs based on metadata index
| Reader of (int32 -> ILAttribute[])
/// Already computed
| Given of ILAttributes
member CustomAttrs: ILAttributes

member HasWellKnownAttribute: flag: WellKnownILAttributes * compute: (ILAttributes -> WellKnownILAttributes) -> bool

member GetCustomAttrs: int32 -> ILAttributes
static member CreateReader: idx: int32 * f: (int32 -> ILAttribute[]) -> ILAttributesStored
static member CreateGiven: attrs: ILAttributes -> ILAttributesStored

/// Method parameters and return values.
[<RequireQualifiedAccess; NoEquality; NoComparison>]
Expand Down
Loading
Loading