Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
DedSec256 committed Nov 19, 2024
1 parent f44e2b2 commit d08555d
Show file tree
Hide file tree
Showing 14 changed files with 215 additions and 162 deletions.
117 changes: 74 additions & 43 deletions src/Compiler/AbstractIL/il.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2562,34 +2562,41 @@ let typeEncodingOfFlags flags =
elif f = 0x00010000 then ILDefaultPInvokeEncoding.Unicode
else ILDefaultPInvokeEncoding.Ansi

[<RequireQualifiedAccess>]
type ILTypeDefKind =
| Class
| ValueType
| Interface
| Enum
| Delegate
[<Flags>]
type ILTypeDefAdditionalFlags =
| Class = 1
| ValueType = 2
| Interface = 4
| Enum = 8
| Delegate = 16
| IsKnownToBeAttribute = 32
/// The type can contain extension methods,
/// or this information may not be available at the time the ILTypeDef is created
| CanContainExtensionMethods = 1024

let (|HasFlag|_|) (flag: ILTypeDefAdditionalFlags) flags =
flags &&& flag = flag

let typeKindOfFlags nm (super: ILType option) flags =
if (flags &&& 0x00000020) <> 0x0 then
ILTypeDefKind.Interface
ILTypeDefAdditionalFlags.Interface
else
match super with
| None -> ILTypeDefKind.Class
| None -> ILTypeDefAdditionalFlags.Class
| Some ty ->
let name = ty.TypeSpec.Name

if name = "System.Enum" then
ILTypeDefKind.Enum
ILTypeDefAdditionalFlags.Enum
elif
(name = "System.Delegate" && nm <> "System.MulticastDelegate")
|| name = "System.MulticastDelegate"
then
ILTypeDefKind.Delegate
ILTypeDefAdditionalFlags.Delegate
elif name = "System.ValueType" && nm <> "System.Enum" then
ILTypeDefKind.ValueType
ILTypeDefAdditionalFlags.ValueType
else
ILTypeDefKind.Class
ILTypeDefAdditionalFlags.Class

let convertTypeAccessFlags access =
match access with
Expand All @@ -2603,13 +2610,10 @@ let convertTypeAccessFlags access =
| ILTypeDefAccess.Nested ILMemberAccess.FamilyOrAssembly -> TypeAttributes.NestedFamORAssem
| ILTypeDefAccess.Nested ILMemberAccess.Assembly -> TypeAttributes.NestedAssembly

let convertTypeKind kind =
let convertTypeKind (kind: ILTypeDefAdditionalFlags) =
match kind with
| ILTypeDefKind.Class -> TypeAttributes.Class
| ILTypeDefKind.ValueType -> TypeAttributes.Class
| ILTypeDefKind.Interface -> TypeAttributes.Abstract ||| TypeAttributes.Interface
| ILTypeDefKind.Enum -> TypeAttributes.Class
| ILTypeDefKind.Delegate -> TypeAttributes.Class
| HasFlag ILTypeDefAdditionalFlags.Interface -> TypeAttributes.Abstract ||| TypeAttributes.Interface
| _ -> TypeAttributes.Class

let convertLayout layout =
match layout with
Expand Down Expand Up @@ -2638,13 +2642,7 @@ let convertInitSemantics (init: ILTypeInit) =
| ILTypeInit.BeforeField -> TypeAttributes.BeforeFieldInit
| ILTypeInit.OnAny -> enum 0

[<Flags>]
type ILTypeDefAdditionalFlags =
| None = 0
| IsKnownToBeAttribute = 1
/// The type can contain extension methods,
/// or this information may not be available at the time the ILTypeDef is created
| CanContainExtensionMethods = 2
let emptyILExtends = notlazy<ILType option> None

[<NoComparison; NoEquality; StructuredFormatDisplay("{DebugText}")>]
type ILTypeDef
Expand All @@ -2654,7 +2652,7 @@ type ILTypeDef
layout: ILTypeDefLayout,
implements: InterruptibleLazy<InterfaceImpl list>,
genericParams: ILGenericParameterDefs,
extends: ILType option,
extends: InterruptibleLazy<ILType option>,
methods: ILMethodDefs,
nestedTypes: ILTypeDefs,
fields: ILFieldDefs,
Expand Down Expand Up @@ -2705,6 +2703,42 @@ type ILTypeDef
NoMetadataIdx
)

new(name,
attributes,
layout,
implements,
genericParams,
extends,
methods,
nestedTypes,
fields,
methodImpls,
events,
properties,
securityDecls,
customAttrs) =
let additionalFlags =
ILTypeDefAdditionalFlags.CanContainExtensionMethods |||
typeKindOfFlags name extends (int attributes)
ILTypeDef(
name,
attributes,
layout,
InterruptibleLazy.FromValue(implements),
genericParams,
InterruptibleLazy.FromValue(extends),
methods,
nestedTypes,
fields,
methodImpls,
events,
properties,
additionalFlags,
storeILSecurityDecls securityDecls,
customAttrs,
NoMetadataIdx
)

member _.Name = name

member _.Attributes = attributes
Expand Down Expand Up @@ -2786,20 +2820,15 @@ type ILTypeDef

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

member x.IsClass =
(typeKindOfFlags x.Name x.Extends (int x.Attributes)) = ILTypeDefKind.Class
member x.IsClass = hasFlag ILTypeDefAdditionalFlags.Class

member x.IsStruct =
(typeKindOfFlags x.Name x.Extends (int x.Attributes)) = ILTypeDefKind.ValueType
member x.IsStruct = hasFlag ILTypeDefAdditionalFlags.ValueType

member x.IsInterface =
(typeKindOfFlags x.Name x.Extends (int x.Attributes)) = ILTypeDefKind.Interface
member x.IsInterface = hasFlag ILTypeDefAdditionalFlags.Interface

member x.IsEnum =
(typeKindOfFlags x.Name x.Extends (int x.Attributes)) = ILTypeDefKind.Enum
member x.IsEnum = hasFlag ILTypeDefAdditionalFlags.Enum

member x.IsDelegate =
(typeKindOfFlags x.Name x.Extends (int x.Attributes)) = ILTypeDefKind.Delegate
member x.IsDelegate = hasFlag ILTypeDefAdditionalFlags.Delegate

member x.Access = typeAccessOfFlags (int x.Attributes)
member x.IsAbstract = x.Attributes &&& TypeAttributes.Abstract <> enum 0
Expand Down Expand Up @@ -2852,7 +2881,7 @@ type ILTypeDef
attributes = (x.Attributes ||| convertTypeKind kind),
extends =
match kind with
| ILTypeDefKind.Interface -> None
| HasFlag ILTypeDefAdditionalFlags.Interface -> emptyILExtends
| _ -> x.Extends
)

Expand All @@ -2865,6 +2894,10 @@ type ILTypeDef
member x.WithInitSemantics(init) =
x.With(attributes = (x.Attributes ||| convertInitSemantics init))

member x.WithAdditionalFlags(flags, rewriteInsteadOfAdd) =
let additionalFlags = if rewriteInsteadOfAdd then flags else flags ||| additionalFlags
x.With(newAdditionalFlags = additionalFlags)

[<DebuggerBrowsable(DebuggerBrowsableState.Never)>]
member x.DebugText = x.ToString()

Expand Down Expand Up @@ -4248,7 +4281,7 @@ let mkILGenericClass (nm, access, genparams, extends, impls, methods, fields, ne
name = nm,
attributes = attributes,
genericParams = genparams,
implements = InterruptibleLazy.FromValue(impls),
implements = impls,
layout = ILTypeDefLayout.Auto,
extends = Some extends,
methods = methods,
Expand All @@ -4258,7 +4291,6 @@ let mkILGenericClass (nm, access, genparams, extends, impls, methods, fields, ne
methodImpls = emptyILMethodImpls,
properties = props,
events = events,
additionalFlags = ILTypeDefAdditionalFlags.None,
securityDecls = emptyILSecurityDecls
)

Expand All @@ -4272,7 +4304,7 @@ let mkRawDataValueTypeDef (iltyp_ValueType: ILType) (nm, size, pack) =
||| TypeAttributes.ExplicitLayout
||| TypeAttributes.BeforeFieldInit
||| TypeAttributes.AnsiClass),
implements = emptyILInterfaceImpls,
implements = [],
extends = Some iltyp_ValueType,
layout = ILTypeDefLayout.Explicit { Size = Some size; Pack = Some pack },
methods = emptyILMethods,
Expand All @@ -4282,7 +4314,6 @@ let mkRawDataValueTypeDef (iltyp_ValueType: ILType) (nm, size, pack) =
methodImpls = emptyILMethodImpls,
properties = emptyILProperties,
events = emptyILEvents,
additionalFlags = ILTypeDefAdditionalFlags.None,
securityDecls = emptyILSecurityDecls
)

Expand Down Expand Up @@ -5579,7 +5610,7 @@ and refsOfILTypeDef s (td: ILTypeDef) =
refsOfILTypeDefs s td.NestedTypes
refsOfILGenericParams s td.GenericParams
refsOfILTypes s (td.Implements.Value |> List.map _.Type)
Option.iter (refsOfILType s) td.Extends
Option.iter (refsOfILType s) td.Extends.Value
refsOfILMethodDefs s td.Methods
refsOfILFieldDefs s (td.Fields.AsList())
refsOfILMethodImpls s (td.MethodImpls.AsList())
Expand Down
54 changes: 36 additions & 18 deletions src/Compiler/AbstractIL/il.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -1483,15 +1483,6 @@ type ILTypeDefAccess =
| Private
| Nested of ILMemberAccess

/// A categorization of type definitions into "kinds"
[<RequireQualifiedAccess>]
type ILTypeDefKind =
| Class
| ValueType
| Interface
| Enum
| Delegate

/// Tables of named type definitions.
[<NoEquality; NoComparison; Class; Sealed>]
type ILTypeDefs =
Expand All @@ -1514,9 +1505,15 @@ type ILTypeDefs =

[<Flags>]
type ILTypeDefAdditionalFlags =
| None = 0
| IsKnownToBeAttribute = 1
| CanContainExtensionMethods = 2
| Class = 1
| ValueType = 2
| Interface = 4
| Enum = 8
| Delegate = 16
| IsKnownToBeAttribute = 32
| CanContainExtensionMethods = 1024

val (|HasFlag|_|): flag: ILTypeDefAdditionalFlags -> flags: ILTypeDefAdditionalFlags -> bool

/// Represents IL Type Definitions.
[<NoComparison; NoEquality>]
Expand All @@ -1529,7 +1526,7 @@ type ILTypeDef =
layout: ILTypeDefLayout *
implements: InterruptibleLazy<InterfaceImpl list> *
genericParams: ILGenericParameterDefs *
extends: ILType option *
extends: InterruptibleLazy<ILType option> *
methods: ILMethodDefs *
nestedTypes: ILTypeDefs *
fields: ILFieldDefs *
Expand All @@ -1542,14 +1539,14 @@ type ILTypeDef =
metadataIndex: int32 ->
ILTypeDef

/// Functional creation of a value, immediate
/// Functional creation of a value with lazy calculated data
new:
name: string *
attributes: TypeAttributes *
layout: ILTypeDefLayout *
implements: InterruptibleLazy<InterfaceImpl list> *
genericParams: ILGenericParameterDefs *
extends: ILType option *
extends: InterruptibleLazy<ILType option> *
methods: ILMethodDefs *
nestedTypes: ILTypeDefs *
fields: ILFieldDefs *
Expand All @@ -1561,13 +1558,31 @@ type ILTypeDef =
customAttrs: ILAttributesStored ->
ILTypeDef

/// Functional creation of a value, immediate
new:
name: string *
attributes: TypeAttributes *
layout: ILTypeDefLayout *
implements: InterfaceImpl list *
genericParams: ILGenericParameterDefs *
extends: ILType option *
methods: ILMethodDefs *
nestedTypes: ILTypeDefs *
fields: ILFieldDefs *
methodImpls: ILMethodImplDefs *
events: ILEventDefs *
properties: ILPropertyDefs *
securityDecls: ILSecurityDecls *
customAttrs: ILAttributesStored ->
ILTypeDef

member Name: string
member Attributes: TypeAttributes
member GenericParams: ILGenericParameterDefs
member Layout: ILTypeDefLayout
member NestedTypes: ILTypeDefs
member Implements: InterruptibleLazy<InterfaceImpl list>
member Extends: ILType option
member Extends: InterruptibleLazy<ILType option>
member Methods: ILMethodDefs
member SecurityDecls: ILSecurityDecls
member Fields: ILFieldDefs
Expand Down Expand Up @@ -1605,10 +1620,11 @@ type ILTypeDef =
member internal WithImport: bool -> ILTypeDef
member internal WithHasSecurity: bool -> ILTypeDef
member internal WithLayout: ILTypeDefLayout -> ILTypeDef
member internal WithKind: ILTypeDefKind -> ILTypeDef
member internal WithKind: ILTypeDefAdditionalFlags -> ILTypeDef
member internal WithEncoding: ILDefaultPInvokeEncoding -> ILTypeDef
member internal WithSpecialName: bool -> ILTypeDef
member internal WithInitSemantics: ILTypeInit -> ILTypeDef
member internal WithAdditionalFlags: ILTypeDefAdditionalFlags * rewriteInsteadOfAdd: bool -> ILTypeDef

/// Functional update
member With:
Expand All @@ -1617,7 +1633,7 @@ type ILTypeDef =
?layout: ILTypeDefLayout *
?implements: InterruptibleLazy<InterfaceImpl list> *
?genericParams: ILGenericParameterDefs *
?extends: ILType option *
?extends: InterruptibleLazy<ILType option> *
?methods: ILMethodDefs *
?nestedTypes: ILTypeDefs *
?fields: ILFieldDefs *
Expand Down Expand Up @@ -2252,6 +2268,8 @@ val internal mkILTypeForGlobalFunctions: ILScopeRef -> ILType

val emptyILInterfaceImpls: InterruptibleLazy<InterfaceImpl list>

val emptyILExtends: InterruptibleLazy<ILType option>

/// Making tables of custom attributes, etc.
val mkILCustomAttrs: ILAttribute list -> ILAttributes
val mkILCustomAttrsFromArray: ILAttribute[] -> ILAttributes
Expand Down
6 changes: 4 additions & 2 deletions src/Compiler/AbstractIL/ilmorph.fs
Original file line number Diff line number Diff line change
Expand Up @@ -371,12 +371,14 @@ let rec tdef_ty2ty_ilmbody2ilmbody_mdefs2mdefs isInKnownSet enc fs (tdef: ILType
let implements =
tdef.Implements.Value
|> List.map (fun x -> { x with Type = fTyInCtxtR x.Type })
|> InterruptibleLazy.FromValue
|> notlazy

let extends = tdef.Extends.Value |> Option.map fTyInCtxtR |> notlazy

tdef.With(
implements = implements,
genericParams = gparams_ty2ty fTyInCtxtR tdef.GenericParams,
extends = Option.map fTyInCtxtR tdef.Extends,
extends = extends,
methods = mdefsR,
nestedTypes = tdefs_ty2ty_ilmbody2ilmbody_mdefs2mdefs isInKnownSet (enc @ [ tdef ]) fs tdef.NestedTypes,
fields = fdefsR,
Expand Down
2 changes: 1 addition & 1 deletion src/Compiler/AbstractIL/ilprint.fs
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ let rec goutput_tdef enc env contents os (cd: ILTypeDef) =
output_sqstring os cd.Name
goutput_gparams env os cd.GenericParams
output_string os "\n\t"
goutput_superclass env os cd.Extends
goutput_superclass env os cd.Extends.Value
output_string os "\n\t"
goutput_implements env os cd.Implements.Value
output_string os "\n{\n "
Expand Down
Loading

0 comments on commit d08555d

Please sign in to comment.