Skip to content

Commit c598c0c

Browse files
committed
generic header
1 parent 8cb34c9 commit c598c0c

17 files changed

+177
-39
lines changed

Runtime.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
DE2C1CAC2258FED800DF6CB5 /* EnumTypeDescriptor.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE2C1CAB2258FED800DF6CB5 /* EnumTypeDescriptor.swift */; };
3636
DE6544C62254377E001352E1 /* FieldDescriptor.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE6544C52254377E001352E1 /* FieldDescriptor.swift */; };
3737
DEB67B5A2259132800CE3E27 /* Case.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEB67B592259132800CE3E27 /* Case.swift */; };
38+
DEB67B5C225924FE00CE3E27 /* Union.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEB67B5B225924FE00CE3E27 /* Union.swift */; };
39+
DEB67B5E22593EF800CE3E27 /* TargetTypeGenericContextDescriptorHeader.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEB67B5D22593EF800CE3E27 /* TargetTypeGenericContextDescriptorHeader.swift */; };
3840
OBJ_100 /* Metadata.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_31 /* Metadata.swift */; };
3941
OBJ_101 /* MetadataType.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_32 /* MetadataType.swift */; };
4042
OBJ_102 /* ProtocolMetadata.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_33 /* ProtocolMetadata.swift */; };
@@ -108,6 +110,8 @@
108110
DE2C1CAB2258FED800DF6CB5 /* EnumTypeDescriptor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EnumTypeDescriptor.swift; sourceTree = "<group>"; };
109111
DE6544C52254377E001352E1 /* FieldDescriptor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FieldDescriptor.swift; sourceTree = "<group>"; };
110112
DEB67B592259132800CE3E27 /* Case.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Case.swift; sourceTree = "<group>"; };
113+
DEB67B5B225924FE00CE3E27 /* Union.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Union.swift; sourceTree = "<group>"; };
114+
DEB67B5D22593EF800CE3E27 /* TargetTypeGenericContextDescriptorHeader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TargetTypeGenericContextDescriptorHeader.swift; sourceTree = "<group>"; };
111115
OBJ_10 /* DefaultValue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultValue.swift; sourceTree = "<group>"; };
112116
OBJ_11 /* Factory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Factory.swift; sourceTree = "<group>"; };
113117
OBJ_13 /* ClassHeader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClassHeader.swift; sourceTree = "<group>"; };
@@ -203,6 +207,7 @@
203207
DE6544C52254377E001352E1 /* FieldDescriptor.swift */,
204208
DE2C1CA922581BA400DF6CB5 /* TypeDescriptor.swift */,
205209
DE2C1CAB2258FED800DF6CB5 /* EnumTypeDescriptor.swift */,
210+
DEB67B5D22593EF800CE3E27 /* TargetTypeGenericContextDescriptorHeader.swift */,
206211
);
207212
path = Layouts;
208213
sourceTree = "<group>";
@@ -243,6 +248,7 @@
243248
OBJ_45 /* RelativePointer.swift */,
244249
OBJ_46 /* RelativeVectorPointer.swift */,
245250
OBJ_47 /* Vector.swift */,
251+
DEB67B5B225924FE00CE3E27 /* Union.swift */,
246252
);
247253
path = Pointers;
248254
sourceTree = "<group>";
@@ -506,11 +512,13 @@
506512
OBJ_101 /* MetadataType.swift in Sources */,
507513
OBJ_102 /* ProtocolMetadata.swift in Sources */,
508514
OBJ_103 /* StructMetadata.swift in Sources */,
515+
DEB67B5E22593EF800CE3E27 /* TargetTypeGenericContextDescriptorHeader.swift in Sources */,
509516
OBJ_104 /* TupleMetadata.swift in Sources */,
510517
OBJ_105 /* Errors.swift in Sources */,
511518
OBJ_106 /* FunctionInfo.swift in Sources */,
512519
OBJ_107 /* Kind.swift in Sources */,
513520
OBJ_108 /* PropertyInfo.swift in Sources */,
521+
DEB67B5C225924FE00CE3E27 /* Union.swift in Sources */,
514522
OBJ_109 /* TypeInfo.swift in Sources */,
515523
OBJ_110 /* TypeInfoConvertible.swift in Sources */,
516524
OBJ_111 /* Pointers.swift in Sources */,

Sources/Runtime/Layouts/ClassTypeDescriptor.swift

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,32 @@ struct ClassTypeDescriptor: TypeDescriptor {
2727
var fieldTypesAccessor: RelativePointer<Int32, Int>
2828
var fieldDescriptor: RelativePointer<Int32, FieldDescriptor>
2929
var superClass: RelativePointer<Int32, Any.Type>
30-
var metadataNegativeSizeInWords: Int32
30+
var negativeSizeAndBoundsUnion: NegativeSizeAndBoundsUnion
3131
var metadataPositiveSizeInWords: Int32
3232
var numImmediateMembers: Int32
3333
var numberOfFields: Int32
3434
var offsetToTheFieldOffsetVector: RelativeVectorPointer<Int32, Int>
35-
var genericContextHeader: Int
36-
var numberOfGenericArguments: Int32
35+
var genericContextHeader: TargetTypeGenericContextDescriptorHeader
36+
37+
struct NegativeSizeAndBoundsUnion: Union {
38+
var raw: Int32
39+
40+
var metadataNegativeSizeInWords: Int32 {
41+
return raw
42+
}
43+
44+
mutating func resilientMetadataBounds() -> UnsafeMutablePointer<RelativePointer<Int32, TargetStoredClassMetadataBounds>> {
45+
return bind()
46+
}
47+
}
48+
}
49+
50+
struct TargetStoredClassMetadataBounds {
51+
var immediateMembersOffset: Int
52+
var bounds: TargetMetadataBounds
53+
}
54+
55+
struct TargetMetadataBounds {
56+
var negativeSizeWords: UInt32
57+
var positiveSizeWords: UInt32
3758
}

Sources/Runtime/Layouts/EnumTypeDescriptor.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,5 @@ struct EnumTypeDescriptor: TypeDescriptor {
1414
var numPayloadCasesAndPayloadSizeOffset: UInt32
1515
var numberOfFields: Int32 // numEmptyCases
1616
var offsetToTheFieldOffsetVector: RelativeVectorPointer<Int32, Int32>
17-
var fieldTypeAccessor: RelativePointer<Int32, Int>
18-
var metadataPattern: Int32
19-
var numberOfGenericArguments: Int32
17+
var genericContextHeader: TargetTypeGenericContextDescriptorHeader
2018
}

Sources/Runtime/Layouts/StructTypeDescriptor.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,5 @@ struct StructTypeDescriptor: TypeDescriptor {
3232
var fieldDescriptor: RelativePointer<Int32, FieldDescriptor>
3333
var numberOfFields: Int32
3434
var offsetToTheFieldOffsetVector: RelativeVectorPointer<Int32, Int32>
35-
var fieldTypeAccessor: RelativePointer<Int32, Int>
36-
var metadataPattern: Int32
37-
var numberOfGenericArguments: Int32
35+
var genericContextHeader: TargetTypeGenericContextDescriptorHeader
3836
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// MIT License
2+
//
3+
// Copyright (c) 2017 Wesley Wickwire
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
23+
struct TargetTypeGenericContextDescriptorHeader {
24+
var instantiationCache: Int32
25+
var defaultInstantiationPattern: Int32
26+
var base: TargetGenericContextDescriptorHeader
27+
}
28+
29+
struct TargetGenericContextDescriptorHeader {
30+
var numberOfParams: UInt16
31+
var numberOfRequirements: UInt16
32+
var numberOfKeyArguments: UInt16
33+
var numberOfExtraArguments: UInt16
34+
}

Sources/Runtime/Layouts/TupleMetadataLayout.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
// SOFTWARE.
2222

23-
import Foundation
24-
2523
struct TupleMetadataLayout: MetadataLayoutType {
2624
var _kind: Int
2725
var numberOfElements: Int

Sources/Runtime/Layouts/TypeDescriptor.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ protocol TypeDescriptor {
3030
var fieldDescriptor: RelativePointer<Int32, FieldDescriptor> { get set }
3131
var numberOfFields: Int32 { get set }
3232
var offsetToTheFieldOffsetVector: RelativeVectorPointer<Int32, FieldOffsetVectorOffsetType> { get set }
33-
var numberOfGenericArguments: Int32 { get set }
33+
var genericContextHeader: TargetTypeGenericContextDescriptorHeader { get set }
3434
}
3535

36-
3736
typealias ContextDescriptorFlags = Int32

Sources/Runtime/Metadata/ClassMetadata.swift

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,41 @@ struct ClassMetadata: MetadataType {
3535
}
3636

3737
var genericArgumentOffset: Int {
38+
let typeDescriptor = pointer.pointee.typeDescriptor
39+
3840
if !hasResilientSuperclass {
39-
// return areImmediateMembersNegative
40-
// ? -Int32(pointer.pointee.typeDescriptor.pointee.metadataPositiveSizeInWords)
41-
// : -Int32(pointer.pointee.typeDescriptor.pointee.metadataPositiveSizeInWords)
42-
/*
43-
return areImmediateMembersNegative()
44-
? -int32_t(MetadataNegativeSizeInWords)
45-
: int32_t(MetadataPositiveSizeInWords - NumImmediateMembers);
46-
*/
47-
41+
return areImmediateMembersNegative
42+
? -Int(typeDescriptor.pointee.negativeSizeAndBoundsUnion.metadataNegativeSizeInWords)
43+
: Int(typeDescriptor.pointee.metadataPositiveSizeInWords - typeDescriptor.pointee.numImmediateMembers)
4844
}
4945

50-
return 10
46+
/*
47+
let storedBounds = typeDescriptor.pointee
48+
.negativeSizeAndBoundsUnion
49+
.resilientMetadataBounds()
50+
.pointee
51+
.advanced()
52+
.pointee
53+
*/
54+
55+
// To do this something like `computeMetadataBoundsFromSuperclass` in Metadata.cpp
56+
// will need to be implemented. To do that we also need to get the resilient superclass
57+
// from the trailing objects.
58+
fatalError("Cannot get the `genericArgumentOffset` for classes with a resilient superclass")
59+
}
60+
61+
func genericArguments() -> [Any.Type] {
62+
let vector = pointer
63+
.advanced(by: genericArgumentOffset, wordSize: MemoryLayout<UnsafeRawPointer>.size)
64+
.assumingMemoryBound(to: Vector<Any.Type>.self)
65+
66+
let n = pointer.pointee.typeDescriptor.pointee.genericContextHeader.base.numberOfParams
67+
68+
guard n > 0 else { return [] }
69+
70+
return (0..<Int(pointer.pointee.typeDescriptor.pointee.genericContextHeader.base.numberOfParams)).map { i in
71+
return vector.pointee.element(at: i).pointee
72+
}
5173
}
5274

5375
func superClassMetadata() -> ClassMetadata? {
@@ -65,6 +87,7 @@ struct ClassMetadata: MetadataType {
6587
var info = TypeInfo(metadata: self)
6688
info.mangledName = mangledName()
6789
info.properties = properties()
90+
info.genericTypes = genericArguments()
6891

6992
var superClass = superClassMetadata()
7093
while var sc = superClass {

Sources/Runtime/Metadata/EnumMetadata.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ struct EnumMetadata: MetadataType {
4545
var info = TypeInfo(metadata: self)
4646
info.mangledName = mangledName()
4747
info.cases = cases()
48+
info.genericTypes = genericArguments()
4849
return info
4950
}
5051
}

Sources/Runtime/Metadata/MetadataType.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ extension MetadataType where Layout: NominalMetadataLayoutType {
125125
}
126126

127127
mutating func genericArguments() -> [Any.Type] {
128-
let n = pointer.pointee.typeDescriptor.pointee.numberOfGenericArguments
128+
let n = pointer.pointee.typeDescriptor.pointee.genericContextHeader.base.numberOfParams
129129
return pointer.pointee
130130
.genericArgumentVector
131131
.vector(n: n)

0 commit comments

Comments
 (0)