Skip to content

Commit e68c44e

Browse files
authored
Merge pull request #6 from KevinRansom/dsyme-tuple-spike
Dsyme tuple spike
2 parents 905a7f1 + 6db1b99 commit e68c44e

File tree

7 files changed

+80
-40
lines changed

7 files changed

+80
-40
lines changed

src/absil/il.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1987,7 +1987,7 @@ let mkILFieldRef(tref,nm,ty) = { EnclosingTypeRef=tref; Name=nm; Type=ty}
19871987
let mkILFieldSpec (tref,ty) = { FieldRef= tref; EnclosingType=ty }
19881988

19891989
let mkILFieldSpecInTy (typ:ILType,nm,fty) =
1990-
mkILFieldSpec (mkILFieldRef (typ.TypeRef,nm,fty), typ)
1990+
mkILFieldSpec (mkILFieldRef (typ.TypeRef,nm,fty), typ)
19911991

19921992
let emptyILCustomAttrs = ILAttributes (fun () -> [| |])
19931993

src/fsharp/CompileOps.fs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1618,7 +1618,8 @@ let DefaultBasicReferencesForOutOfProjectSources =
16181618
yield "System.Runtime.Serialization.Formatters.Soap"
16191619
yield "System.Data"
16201620
yield "System.Drawing"
1621-
1621+
yield "System.ValueTuple"
1622+
16221623
// Don't reference System.Core for .NET 2.0 compilations.
16231624
//
16241625
// We only use a default reference to System.Core if one exists which we can load it into the compiler process.
@@ -1660,6 +1661,7 @@ let SystemAssemblies primaryAssemblyName =
16601661
yield "System.Runtime"
16611662
yield "System.Observable"
16621663
yield "System.Numerics"
1664+
yield "System.ValueTuple"
16631665

16641666
// Additions for coreclr and portable profiles
16651667
yield "System.Collections"

src/fsharp/FSharp.Compiler/FSharp.Compiler.fsproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@
528528
<Reference Include="Microsoft.DiaSymReader"><HintPath>..\..\..\packages\Microsoft.DiaSymReader.1.0.8\lib\portable-net45+win8\Microsoft.DiaSymReader.dll</HintPath></Reference>
529529
<Reference Include="System.Reflection.Metadata"><HintPath>..\..\..\packages\System.Reflection.Metadata.1.4.1-beta-24227-04\lib\portable-net45+win8\System.Reflection.Metadata.dll</HintPath></Reference>
530530
<Reference Include="System.Collections.Immutable"><HintPath>..\..\..\packages\System.Collections.Immutable.1.2.0\lib\portable-net45+win8+wp8+wpa81</HintPath></Reference>
531-
<Reference Include="System.ValueTuple"><HintPath>..\..\..\packages\System.ValueTuple.4.0.0-rc3-24212-01\lib\netstandard1.1</HintPath></Reference>
531+
<Reference Include="System.ValueTuple"><HintPath>..\..\..\packages\System.ValueTuple.4.0.0-rc3-24212-01\lib\netstandard1.1</HintPath><Private>false</Private></Reference>
532532
</ItemGroup>
533533
<ItemGroup>
534534
<ProjectReference Include="$(FSharpSourcesRoot)\fsharp\FSharp.Core\FSharp.Core.fsproj">
@@ -539,7 +539,6 @@
539539
<Import Project="$(FSharpSourcesRoot)\.nuget\NuGet.targets" Condition="Exists('$(FSharpSourcesRoot)\.nuget\NuGet.targets')" />
540540
<Import Project="$(FSharpSourcesRoot)\FSharpSource.targets" />
541541
<Import Project="$(FSharpSourcesRoot)\..\lkg\FSharp-$(LkgVersion)\bin\FSharp.PowerPack.targets" />
542-
543542
<Target Name="GatherBinariesToBeSigned" AfterTargets="Localize">
544543
<ItemGroup>
545544
<BinariesToBeSigned Include="$(OutDir)$(AssemblyName).dll" />

src/fsharp/FSharp.Core/reflect.fs

Lines changed: 71 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ module internal Impl =
396396
let mutable systemValueTupleException = null
397397

398398
let reflectedValueTuple n =
399-
try
399+
try
400400
#if FX_ASSEMBLYLOADBYSTRING
401401
let a = Assembly.Load("System.ValueTuple")
402402
#else
@@ -522,7 +522,7 @@ module internal Impl =
522522
let tysA = tyargs.[0..tupleEncField-1]
523523
let tyB = tyargs.[tupleEncField]
524524
Array.append tysA (getTupleTypeInfo tyB)
525-
else
525+
else
526526
tyargs
527527

528528
let orderTupleProperties (props:PropertyInfo[]) =
@@ -548,34 +548,73 @@ module internal Impl =
548548
haveNames = expectNames)
549549
#endif
550550
props
551-
551+
552+
let orderTupleFields (fields:FieldInfo[]) =
553+
// The tuple fields are of the form:
554+
// Item1
555+
// ..
556+
// Item1, Item2, ..., Item<maxTuple-1>
557+
// Item1, Item2, ..., Item<maxTuple-1>, Rest
558+
// The PropertyInfo may not come back in order, so ensure ordering here.
559+
#if FX_ATLEAST_PORTABLE
560+
#else
561+
assert(maxTuple < 10) // Alphasort will only works for upto 9 items: Item1, Item10, Item2, Item3, ..., Item9, Rest
562+
#endif
563+
let fields = fields |> Array.sortBy (fun fi -> fi.Name) // they are not always in alphabetic order
564+
#if FX_ATLEAST_PORTABLE
565+
#else
566+
assert(fields.Length <= maxTuple)
567+
assert(let haveNames = fields |> Array.map (fun fi -> fi.Name)
568+
let expectNames = Array.init fields.Length (fun i -> let j = i+1 // index j = 1,2,..,fields.Length <= maxTuple
569+
if j<maxTuple then "Item" + string j
570+
elif j=maxTuple then "Rest"
571+
else (assert false; "")) // dead code under prior assert, props.Length <= maxTuple
572+
haveNames = expectNames)
573+
#endif
574+
fields
575+
552576
let getTupleConstructorMethod(typ:Type,bindingFlags) =
553-
let props = typ.GetProperties() |> orderTupleProperties
577+
let ctor =
578+
if typ.IsValueType then
579+
let fields = typ.GetFields() |> orderTupleFields
554580
#if FX_ATLEAST_PORTABLE
555-
let ctor = typ.GetConstructor(props |> Array.map (fun p -> p.PropertyType))
556-
ignore bindingFlags
557-
#else
558-
let ctor = typ.GetConstructor(BindingFlags.Instance ||| bindingFlags,null,props |> Array.map (fun p -> p.PropertyType),null)
559-
#endif
560-
match ctor with
561-
| null -> raise <| ArgumentException(SR.GetString1(SR.invalidTupleTypeConstructorNotDefined, typ.FullName))
562-
| _ -> ()
563-
ctor
564-
581+
ignore bindingFlags
582+
typ.GetConstructor(fields |> Array.map (fun fi -> fi.FieldType))
583+
#else
584+
typ.GetConstructor(BindingFlags.Instance ||| bindingFlags,null,fields |> Array.map (fun fi -> fi.FieldType),null)
585+
#endif
586+
else
587+
let props = typ.GetProperties() |> orderTupleProperties
588+
#if FX_ATLEAST_PORTABLE
589+
ignore bindingFlags
590+
typ.GetConstructor(props |> Array.map (fun p -> p.PropertyType))
591+
#else
592+
typ.GetConstructor(BindingFlags.Instance ||| bindingFlags,null,props |> Array.map (fun p -> p.PropertyType),null)
593+
#endif
594+
match ctor with
595+
| null -> raise <| ArgumentException(SR.GetString1(SR.invalidTupleTypeConstructorNotDefined, typ.FullName))
596+
| _ -> ()
597+
ctor
598+
565599
let getTupleCtor(typ:Type,bindingFlags) =
566600
let ctor = getTupleConstructorMethod(typ,bindingFlags)
567601
(fun (args:obj[]) ->
568602
#if FX_ATLEAST_PORTABLE
569603
ctor.Invoke(args))
570604
#else
571605
ctor.Invoke(BindingFlags.InvokeMethod ||| BindingFlags.Instance ||| bindingFlags,null,args,null))
572-
#endif
606+
#endif
573607

574608
let rec getTupleReader (typ:Type) =
575609
let etys = typ.GetGenericArguments()
576610
// Get the reader for the outer tuple record
577-
let props = typ.GetProperties(instancePropertyFlags ||| BindingFlags.Public) |> orderTupleProperties
578-
let reader = (fun (obj:obj) -> props |> Array.map (fun prop -> prop.GetValue(obj,null)))
611+
let reader =
612+
if typ.IsValueType then
613+
let fields = (typ.GetFields(instancePropertyFlags ||| BindingFlags.Public) |> orderTupleFields)
614+
((fun (obj:obj) -> fields |> Array.map (fun field -> field.GetValue(obj))))
615+
else
616+
let props = (typ.GetProperties(instancePropertyFlags ||| BindingFlags.Public) |> orderTupleProperties)
617+
((fun (obj:obj) -> props |> Array.map (fun prop -> prop.GetValue(obj,null))))
579618
if etys.Length < maxTuple
580619
then reader
581620
else
@@ -585,7 +624,7 @@ module internal Impl =
585624
let directVals = reader obj
586625
let encVals = reader2 directVals.[tupleEncField]
587626
Array.append directVals.[0..tupleEncField-1] encVals)
588-
627+
589628
let rec getTupleConstructor (typ:Type) =
590629
let etys = typ.GetGenericArguments()
591630
let maker1 = getTupleCtor (typ,BindingFlags.Public)
@@ -606,20 +645,25 @@ module internal Impl =
606645
else
607646
maker1,Some(etys.[tupleEncField])
608647

609-
let getTupleReaderInfo (typ:Type,index:int) =
648+
let getTupleReaderInfo (typ:Type,index:int) =
610649
if index < 0 then invalidArg "index" (SR.GetString2(SR.tupleIndexOutOfRange, typ.FullName, index.ToString()))
611-
let props = typ.GetProperties(instancePropertyFlags ||| BindingFlags.Public) |> orderTupleProperties
612-
let get index =
613-
if index >= props.Length then invalidArg "index" (SR.GetString2(SR.tupleIndexOutOfRange, typ.FullName, index.ToString()))
614-
props.[index]
615-
650+
651+
let get index =
652+
if typ.IsValueType then
653+
let props = typ.GetProperties(instancePropertyFlags ||| BindingFlags.Public) |> orderTupleProperties
654+
if index >= props.Length then invalidArg "index" (SR.GetString2(SR.tupleIndexOutOfRange, typ.FullName, index.ToString()))
655+
props.[index]
656+
else
657+
let props = typ.GetProperties(instancePropertyFlags ||| BindingFlags.Public) |> orderTupleProperties
658+
if index >= props.Length then invalidArg "index" (SR.GetString2(SR.tupleIndexOutOfRange, typ.FullName, index.ToString()))
659+
props.[index]
660+
616661
if index < tupleEncField then
617-
get index, None
662+
get index, None
618663
else
619664
let etys = typ.GetGenericArguments()
620665
get tupleEncField, Some(etys.[tupleEncField],index-(maxTuple-1))
621-
622-
666+
623667
//-----------------------------------------------------------------
624668
// FUNCTION DECOMPILATION
625669

src/fsharp/IlxGen.fs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ let rec GenTypeArgAux amap m g tyenv tyarg =
390390
and GenTypeArgsAux amap m g tyenv tyargs =
391391
List.map (GenTypeArgAux amap m g tyenv) (DropErasedTyargs tyargs)
392392

393-
and GenTyAppAux amap m g tyenv repr tinst =
393+
and GenTyAppAux amap m g tyenv repr tinst =
394394
match repr with
395395
| CompiledTypeRepr.ILAsmOpen ty ->
396396
let ilTypeInst = GenTypeArgsAux amap m g tyenv tinst
@@ -403,7 +403,7 @@ and GenTyAppAux amap m g tyenv repr tinst =
403403
mkILTy boxity (mkILTySpec (tref,ilTypeInst))
404404
| Some ilType ->
405405
ilType // monomorphic types include a cached ilType to avoid reallocation of an ILType node
406-
406+
407407

408408
and GenNamedTyAppAux (amap:Import.ImportMap) m g tyenv ptrsOK tcref tinst =
409409
let tinst = DropErasedTyargs tinst
@@ -2026,7 +2026,6 @@ and GenGetTupleField cenv cgbuf eenv (tupInfo,e,tys,n,m) sequel =
20262026
getCompiledTupleItem g (elast,tysB,n-goodTupleFields,m)
20272027
GenExpr cenv cgbuf eenv SPSuppress (getCompiledTupleItem cenv.g (e,tys,n,m)) sequel
20282028

2029-
20302029
and GenAllocExn cenv cgbuf eenv (c,args,m) sequel =
20312030
GenExprs cenv cgbuf eenv args
20322031
let typ = GenExnType cenv.amap m cenv.g eenv.tyenv c

src/fsharp/TastOps.fs

100755100644
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ let reduceTyconMeasureableOrProvided g (tycon:Tycon) tyargs =
624624
| TProvidedTypeExtensionPoint info when info.IsErased -> info.BaseTypeForErased (range0, g.obj_ty)
625625
#endif
626626
| _ -> invalidArg "tc" "this type definition is not a refinement"
627-
627+
628628
let reduceTyconRefMeasureableOrProvided (g:TcGlobals) (tcref:TyconRef) tyargs =
629629
reduceTyconMeasureableOrProvided g tcref.Deref tyargs
630630

@@ -7834,10 +7834,6 @@ let mkGetTupleItemN g m n (typ:ILType) isStruct te retty =
78347834
mkAsmExpr([mkNormalLdfld (mkILFieldSpecForTupleItem typ n) ],[],[te],[retty],m)
78357835
else
78367836
mkAsmExpr([IL.mkNormalCall(mkILMethodSpecForTupleItem g typ n)],[],[te],[retty],m)
7837-
7838-
// let mkGetTupleItemN g m n typ te retty =
7839-
// mkAsmExpr([IL.mkNormalCall(mkILMethodSpecForTupleItem g typ n)],[],[te],[retty],m)
7840-
78417837
/// Match an Int32 constant expression
78427838
let (|Int32Expr|_|) expr =
78437839
match expr with

src/fsharp/tast.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,7 @@ type Entity =
952952
let ilTypeOpt =
953953
match x.TyparsNoRange with
954954
| [] -> Some (mkILTy boxity (mkILTySpec (ilTypeRef,[])))
955-
| _ -> None
955+
| _ -> None
956956
CompiledTypeRepr.ILAsmNamed (ilTypeRef, boxity, ilTypeOpt))
957957

958958
/// Gets the data indicating the compiled representation of a named type or module in terms of Abstract IL data structures.

0 commit comments

Comments
 (0)