Skip to content
Merged
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
71 changes: 42 additions & 29 deletions src/Compiler/CodeGen/EraseUnions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
/// Erase discriminated unions.
module internal FSharp.Compiler.AbstractIL.ILX.EraseUnions

open FSharp.Compiler.IlxGenSupport

open System.Collections.Generic
open System.Reflection
open Internal.Utilities.Library
open FSharp.Compiler.TcGlobals
open FSharp.Compiler.AbstractIL.IL
open FSharp.Compiler.AbstractIL.ILX.Types

Expand Down Expand Up @@ -752,7 +755,7 @@ let convAlternativeDef
addFieldGeneratedAttrs,
addFieldNeverAttrs,
mkDebuggerTypeProxyAttribute)
(ilg: ILGlobals)
(g: TcGlobals)
num
(td: ILTypeDef)
(cud: IlxUnionInfo)
Expand Down Expand Up @@ -834,12 +837,12 @@ let convAlternativeDef
"get_" + mkTesterName altName,
cud.HelpersAccessibility,
[],
mkILReturn ilg.typ_Bool,
mkILReturn g.ilg.typ_Bool,
mkMethodBody (
true,
[],
2,
nonBranchingInstrsToCode ([ mkLdarg0 ] @ mkIsData ilg (true, cuspec, num)),
nonBranchingInstrsToCode ([ mkLdarg0 ] @ mkIsData g.ilg (true, cuspec, num)),
attr,
imports
)
Expand All @@ -853,10 +856,17 @@ let convAlternativeDef
setMethod = None,
getMethod =
Some(
mkILMethRef (baseTy.TypeRef, ILCallingConv.Instance, "get_" + mkTesterName altName, 0, [], ilg.typ_Bool)
mkILMethRef (
baseTy.TypeRef,
ILCallingConv.Instance,
"get_" + mkTesterName altName,
0,
[],
g.ilg.typ_Bool
)
),
callingConv = ILThisConvention.Instance,
propertyType = ilg.typ_Bool,
propertyType = g.ilg.typ_Bool,
init = None,
args = [],
customAttrs = emptyILCustomAttrs
Expand All @@ -879,7 +889,7 @@ let convAlternativeDef
true,
[],
fields.Length,
nonBranchingInstrsToCode (convNewDataInstrInternal ilg cuspec num),
nonBranchingInstrsToCode (convNewDataInstrInternal g.ilg cuspec num),
attr,
imports
)
Expand Down Expand Up @@ -910,7 +920,7 @@ let convAlternativeDef
[
for i in 0 .. fields.Length - 1 do
mkLdarg (uint16 i)
yield! convNewDataInstrInternal ilg cuspec num
yield! convNewDataInstrInternal g.ilg cuspec num
]
|> nonBranchingInstrsToCode

Expand Down Expand Up @@ -983,20 +993,20 @@ let convAlternativeDef
let debugProxyCode =
[
mkLdarg0
mkNormalCall (mkILCtorMethSpecForTy (ilg.typ_Object, []))
mkNormalCall (mkILCtorMethSpecForTy (g.ilg.typ_Object, []))
mkLdarg0
mkLdarg 1us
mkNormalStfld (mkILFieldSpecInTy (debugProxyTy, debugProxyFieldName, altTy))
]
|> nonBranchingInstrsToCode

let debugProxyCtor =
mkILCtor (
(mkILCtor (
ILMemberAccess.Public (* must always be public - see jared parson blog entry on implementing debugger type proxy *) ,
[ mkILParamNamed ("obj", altTy) ],
mkMethodBody (false, [], 3, debugProxyCode, None, imports)
)

))
.With(customAttrs = mkILCustomAttrs[GetDynamicDependencyAttribute g 0x660 baseTy])
|> addMethodGeneratedAttrs

let debugProxyGetterMeths =
Expand Down Expand Up @@ -1057,7 +1067,7 @@ let convAlternativeDef
debugProxyTypeName,
ILTypeDefAccess.Nested ILMemberAccess.Assembly,
td.GenericParams,
ilg.typ_Object,
g.ilg.typ_Object,
[],
mkILMethods ([ debugProxyCtor ] @ debugProxyGetterMeths),
mkILFields debugProxyFields,
Expand Down Expand Up @@ -1101,7 +1111,7 @@ let convAlternativeDef
match repr.DiscriminationTechnique info with
| IntegerTag ->
yield mkLdcInt32 num
yield mkNormalCall (mkILCtorMethSpecForTy (baseTy, [ mkTagFieldType ilg cuspec ]))
yield mkNormalCall (mkILCtorMethSpecForTy (baseTy, [ mkTagFieldType g.ilg cuspec ]))
| SingleCase
| RuntimeTypes -> yield mkNormalCall (mkILCtorMethSpecForTy (baseTy, []))
| TailOrNull -> failwith "unreachable"
Expand All @@ -1117,7 +1127,8 @@ let convAlternativeDef
basicFields |> List.map (fun fdef -> fdef.Name, fdef.FieldType)

let basicCtorMeth =
mkILStorageCtor (basicCtorInstrs, altTy, basicCtorFields, basicCtorAccess, attr, imports)
(mkILStorageCtor (basicCtorInstrs, altTy, basicCtorFields, basicCtorAccess, attr, imports))
.With(customAttrs = mkILCustomAttrs[GetDynamicDependencyAttribute g 0x660 baseTy])
|> addMethodGeneratedAttrs

let altTypeDef =
Expand Down Expand Up @@ -1157,7 +1168,7 @@ let mkClassUnionDef
addFieldGeneratedAttrs: ILFieldDef -> ILFieldDef,
addFieldNeverAttrs: ILFieldDef -> ILFieldDef,
mkDebuggerTypeProxyAttribute)
ilg
(g: TcGlobals)
tref
(td: ILTypeDef)
cud
Expand All @@ -1183,7 +1194,7 @@ let mkClassUnionDef
addFieldGeneratedAttrs,
addFieldNeverAttrs,
mkDebuggerTypeProxyAttribute)
ilg
g
i
td
cud
Expand All @@ -1204,7 +1215,7 @@ let mkClassUnionDef
| SingleCase
| RuntimeTypes
| TailOrNull -> []
| IntegerTag -> [ mkTagFieldId ilg cuspec ]
| IntegerTag -> [ mkTagFieldId g.ilg cuspec ]

let isStruct = td.IsStruct

Expand All @@ -1224,12 +1235,12 @@ let mkClassUnionDef
None
else
match td.Extends with
| None -> Some ilg.typ_Object.TypeSpec
| None -> Some g.ilg.typ_Object.TypeSpec
| Some ilTy -> Some ilTy.TypeSpec

let extraParamsForCtor =
if isStruct && takesExtraParams cud.UnionCases then
let extraTys, _extraInstrs = extraTysAndInstrsForStructCtor ilg cidx
let extraTys, _extraInstrs = extraTysAndInstrsForStructCtor g.ilg cidx
List.map mkILParamAnon extraTys
else
[]
Expand All @@ -1241,15 +1252,16 @@ let mkClassUnionDef
cud.UnionCasesAccessibility)

let ctor =
mkILSimpleStorageCtor (
(mkILSimpleStorageCtor (
baseInit,
baseTy,
extraParamsForCtor,
(fields @ tagFieldsInObject),
ctorAccess,
cud.DebugPoint,
cud.DebugImports
)
))
.With(customAttrs = mkILCustomAttrs[GetDynamicDependencyAttribute g 0x660 baseTy])
|> addMethodGeneratedAttrs

let props, meths =
Expand Down Expand Up @@ -1293,20 +1305,21 @@ let mkClassUnionDef
else
let baseTySpec =
(match td.Extends with
| None -> ilg.typ_Object
| None -> g.ilg.typ_Object
| Some ilTy -> ilTy)
.TypeSpec

[
mkILSimpleStorageCtor (
(mkILSimpleStorageCtor (
Some baseTySpec,
baseTy,
[],
tagFieldsInObject,
ILMemberAccess.Assembly,
cud.DebugPoint,
cud.DebugImports
)
))
.With(customAttrs = mkILCustomAttrs[GetDynamicDependencyAttribute g 0x7E0 baseTy])
|> addMethodGeneratedAttrs
]

Expand All @@ -1328,7 +1341,7 @@ let mkClassUnionDef
| IntegerTag ->
if inRootClass then
yield mkLdcInt32 fidx
yield mkNormalNewobj (mkILCtorMethSpecForTy (altTy, [ mkTagFieldType ilg cuspec ]))
yield mkNormalNewobj (mkILCtorMethSpecForTy (altTy, [ mkTagFieldType g.ilg cuspec ]))
else
yield mkNormalNewobj (mkILCtorMethSpecForTy (altTy, []))

Expand All @@ -1339,7 +1352,7 @@ let mkClassUnionDef
cd

let tagMeths, tagProps, tagEnumFields =
let tagFieldType = mkTagFieldType ilg cuspec
let tagFieldType = mkTagFieldType g.ilg cuspec

let tagEnumFields =
cud.UnionCases
Expand All @@ -1350,7 +1363,7 @@ let mkClassUnionDef

let code =
genWith (fun cg ->
emitLdDataTagPrim ilg (Some mkLdarg0) cg (true, cuspec)
emitLdDataTagPrim g.ilg (Some mkLdarg0) cg (true, cuspec)
cg.EmitInstr I_ret)

let body = mkMethodBody (true, [], 2, code, cud.DebugPoint, cud.DebugImports)
Expand Down Expand Up @@ -1414,7 +1427,7 @@ let mkClassUnionDef
attributes = enum 0,
layout = ILTypeDefLayout.Auto,
implements = [],
extends = Some ilg.typ_Object,
extends = Some g.ilg.typ_Object,
methods = emptyILMethods,
securityDecls = emptyILSecurityDecls,
fields = mkILFields tagEnumFields,
Expand Down Expand Up @@ -1444,7 +1457,7 @@ let mkClassUnionDef
),
extends =
(match td.Extends with
| None -> Some ilg.typ_Object
| None -> Some g.ilg.typ_Object
| _ -> td.Extends),
methods =
mkILMethods (
Expand Down
3 changes: 2 additions & 1 deletion src/Compiler/CodeGen/EraseUnions.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module internal FSharp.Compiler.AbstractIL.ILX.EraseUnions

open FSharp.Compiler.AbstractIL.IL
open FSharp.Compiler.AbstractIL.ILX.Types
open FSharp.Compiler.TcGlobals

/// Make the instruction sequence for a "newdata" operation
val mkNewData: ilg: ILGlobals -> cuspec: IlxUnionSpec * cidx: int -> ILInstr list
Expand Down Expand Up @@ -39,7 +40,7 @@ val mkClassUnionDef:
addFieldGeneratedAttrs: (ILFieldDef -> ILFieldDef) *
addFieldNeverAttrs: (ILFieldDef -> ILFieldDef) *
mkDebuggerTypeProxyAttribute: (ILType -> ILAttribute) ->
ilg: ILGlobals ->
g: TcGlobals ->
tref: ILTypeRef ->
td: ILTypeDef ->
cud: IlxUnionInfo ->
Expand Down
Loading