Skip to content

Commit 464e60a

Browse files
committed
removed hard coded generic vector from layouts
1 parent c598c0c commit 464e60a

File tree

10 files changed

+102
-76
lines changed

10 files changed

+102
-76
lines changed

Runtime.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
DEB67B5A2259132800CE3E27 /* Case.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEB67B592259132800CE3E27 /* Case.swift */; };
3838
DEB67B5C225924FE00CE3E27 /* Union.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEB67B5B225924FE00CE3E27 /* Union.swift */; };
3939
DEB67B5E22593EF800CE3E27 /* TargetTypeGenericContextDescriptorHeader.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEB67B5D22593EF800CE3E27 /* TargetTypeGenericContextDescriptorHeader.swift */; };
40+
DEB67B602259448F00CE3E27 /* NominalMetadataType.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEB67B5F2259448F00CE3E27 /* NominalMetadataType.swift */; };
4041
OBJ_100 /* Metadata.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_31 /* Metadata.swift */; };
4142
OBJ_101 /* MetadataType.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_32 /* MetadataType.swift */; };
4243
OBJ_102 /* ProtocolMetadata.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_33 /* ProtocolMetadata.swift */; };
@@ -112,6 +113,7 @@
112113
DEB67B592259132800CE3E27 /* Case.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Case.swift; sourceTree = "<group>"; };
113114
DEB67B5B225924FE00CE3E27 /* Union.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Union.swift; sourceTree = "<group>"; };
114115
DEB67B5D22593EF800CE3E27 /* TargetTypeGenericContextDescriptorHeader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TargetTypeGenericContextDescriptorHeader.swift; sourceTree = "<group>"; };
116+
DEB67B5F2259448F00CE3E27 /* NominalMetadataType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NominalMetadataType.swift; sourceTree = "<group>"; };
115117
OBJ_10 /* DefaultValue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultValue.swift; sourceTree = "<group>"; };
116118
OBJ_11 /* Factory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Factory.swift; sourceTree = "<group>"; };
117119
OBJ_13 /* ClassHeader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClassHeader.swift; sourceTree = "<group>"; };
@@ -223,6 +225,7 @@
223225
OBJ_33 /* ProtocolMetadata.swift */,
224226
OBJ_34 /* StructMetadata.swift */,
225227
OBJ_35 /* TupleMetadata.swift */,
228+
DEB67B5F2259448F00CE3E27 /* NominalMetadataType.swift */,
226229
);
227230
path = Metadata;
228231
sourceTree = "<group>";
@@ -508,6 +511,7 @@
508511
OBJ_97 /* ClassMetadata.swift in Sources */,
509512
OBJ_98 /* EnumMetadata.swift in Sources */,
510513
OBJ_99 /* FuntionMetadata.swift in Sources */,
514+
DEB67B602259448F00CE3E27 /* NominalMetadataType.swift in Sources */,
511515
OBJ_100 /* Metadata.swift in Sources */,
512516
OBJ_101 /* MetadataType.swift in Sources */,
513517
OBJ_102 /* ProtocolMetadata.swift in Sources */,

Sources/Runtime/Layouts/ClassMetadataLayout.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,4 @@ struct ClassMetadataLayout: NominalMetadataLayoutType {
3737
var classAddressPoint: UInt32
3838
var typeDescriptor: UnsafeMutablePointer<ClassTypeDescriptor>
3939
var iVarDestroyer: UnsafeRawPointer
40-
41-
// FIXME: this is a temporary fix to get the `genericArgumentVector` in sort of the right spot.
42-
// this should really be calculated.
43-
var a, b, c, d, e: Int
44-
45-
var genericArgumentVector: Vector<Any.Type>
4640
}

Sources/Runtime/Layouts/EnumMetadataLayout.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,4 @@ import Foundation
2525
struct EnumMetadataLayout: NominalMetadataLayoutType {
2626
var _kind: Int
2727
var typeDescriptor: UnsafeMutablePointer<EnumTypeDescriptor>
28-
var genericArgumentVector: Vector<Any.Type>
2928
}

Sources/Runtime/Layouts/MetadataLayoutType.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,4 @@ protocol MetadataLayoutType {
2929
protocol NominalMetadataLayoutType: MetadataLayoutType {
3030
associatedtype Descriptor: TypeDescriptor
3131
var typeDescriptor: UnsafeMutablePointer<Descriptor> { get set }
32-
var genericArgumentVector: Vector<Any.Type> { get set }
3332
}

Sources/Runtime/Layouts/StructMetadataLayout.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,4 @@ import Foundation
2525
struct StructMetadataLayout: NominalMetadataLayoutType {
2626
var _kind: Int
2727
var typeDescriptor: UnsafeMutablePointer<StructTypeDescriptor>
28-
var genericArgumentVector: Vector<Any.Type>
2928
}

Sources/Runtime/Metadata/ClassMetadata.swift

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import Foundation
2424

25-
struct ClassMetadata: MetadataType {
25+
struct ClassMetadata: NominalMetadataType {
2626

2727
var pointer: UnsafeMutablePointer<ClassMetadataLayout>
2828

@@ -58,20 +58,6 @@ struct ClassMetadata: MetadataType {
5858
fatalError("Cannot get the `genericArgumentOffset` for classes with a resilient superclass")
5959
}
6060

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-
}
73-
}
74-
7561
func superClassMetadata() -> ClassMetadata? {
7662
let superClass = pointer.pointee.superClass
7763
// type comparison directly to NSObject.self does not work.

Sources/Runtime/Metadata/EnumMetadata.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import Foundation
2424

25-
struct EnumMetadata: MetadataType {
25+
struct EnumMetadata: NominalMetadataType {
2626

2727
var pointer: UnsafeMutablePointer<EnumMetadataLayout>
2828

Sources/Runtime/Metadata/MetadataType.swift

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -81,53 +81,3 @@ extension MetadataType {
8181
return TypeInfo(metadata: self)
8282
}
8383
}
84-
85-
extension MetadataType where Layout: NominalMetadataLayoutType {
86-
87-
mutating func mangledName() -> String {
88-
return String(cString: pointer.pointee.typeDescriptor.pointee.mangledName.advanced())
89-
}
90-
91-
mutating func numberOfFields() -> Int {
92-
return pointer.pointee.typeDescriptor.pointee.numberOfFields.getInt()
93-
}
94-
95-
mutating func fieldOffsets() -> [Int] {
96-
return pointer.pointee.typeDescriptor.pointee
97-
.offsetToTheFieldOffsetVector
98-
.vector(metadata: pointer.raw.assumingMemoryBound(to: Int.self), n: numberOfFields())
99-
.map { $0.getInt() }
100-
}
101-
102-
mutating func properties() -> [PropertyInfo] {
103-
let offsets = fieldOffsets()
104-
let fieldDescriptor = pointer.pointee.typeDescriptor.pointee
105-
.fieldDescriptor
106-
.advanced()
107-
108-
return (0..<numberOfFields()).map { i in
109-
let record = fieldDescriptor
110-
.pointee
111-
.fields
112-
.element(at: i)
113-
114-
return PropertyInfo(
115-
name: record.pointee.fieldName(),
116-
type: record.pointee.type(
117-
genericContext: pointer.pointee.typeDescriptor,
118-
genericArguments: pointer.pointee.genericArgumentVector.element(at: 0)
119-
),
120-
isVar: record.pointee.isVar,
121-
offset: offsets[i],
122-
ownerType: type
123-
)
124-
}
125-
}
126-
127-
mutating func genericArguments() -> [Any.Type] {
128-
let n = pointer.pointee.typeDescriptor.pointee.genericContextHeader.base.numberOfParams
129-
return pointer.pointee
130-
.genericArgumentVector
131-
.vector(n: n)
132-
}
133-
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
protocol NominalMetadataType: MetadataType where Layout: NominalMetadataLayoutType {
24+
25+
/// The offset of the generic type vector in pointer sized words from the
26+
/// start of the metadata record.
27+
var genericArgumentOffset: Int { get }
28+
}
29+
30+
extension NominalMetadataType {
31+
32+
var genericArgumentOffset: Int {
33+
// default to 2. This would put it right after the type descriptor which is valid
34+
// for all types except for classes
35+
return 2
36+
}
37+
38+
mutating func mangledName() -> String {
39+
return String(cString: pointer.pointee.typeDescriptor.pointee.mangledName.advanced())
40+
}
41+
42+
mutating func numberOfFields() -> Int {
43+
return pointer.pointee.typeDescriptor.pointee.numberOfFields.getInt()
44+
}
45+
46+
mutating func fieldOffsets() -> [Int] {
47+
return pointer.pointee.typeDescriptor.pointee
48+
.offsetToTheFieldOffsetVector
49+
.vector(metadata: pointer.raw.assumingMemoryBound(to: Int.self), n: numberOfFields())
50+
.map { $0.getInt() }
51+
}
52+
53+
mutating func properties() -> [PropertyInfo] {
54+
let offsets = fieldOffsets()
55+
let fieldDescriptor = pointer.pointee.typeDescriptor.pointee
56+
.fieldDescriptor
57+
.advanced()
58+
59+
let genericVector = genericArgumentVector()
60+
61+
return (0..<numberOfFields()).map { i in
62+
let record = fieldDescriptor
63+
.pointee
64+
.fields
65+
.element(at: i)
66+
67+
return PropertyInfo(
68+
name: record.pointee.fieldName(),
69+
type: record.pointee.type(
70+
genericContext: pointer.pointee.typeDescriptor,
71+
genericArguments: genericVector.pointee.element(at: 0)
72+
),
73+
isVar: record.pointee.isVar,
74+
offset: offsets[i],
75+
ownerType: type
76+
)
77+
}
78+
}
79+
80+
func genericArguments() -> [Any.Type] {
81+
let n = pointer.pointee.typeDescriptor.pointee.genericContextHeader.base.numberOfParams
82+
guard n > 0 else { return [] }
83+
84+
let vector = genericArgumentVector()
85+
return (0..<Int(pointer.pointee.typeDescriptor.pointee.genericContextHeader.base.numberOfParams)).map { i in
86+
return vector.pointee.element(at: i).pointee
87+
}
88+
}
89+
90+
func genericArgumentVector() -> UnsafeMutablePointer<Vector<Any.Type>> {
91+
return pointer
92+
.advanced(by: genericArgumentOffset, wordSize: MemoryLayout<UnsafeRawPointer>.size)
93+
.assumingMemoryBound(to: Vector<Any.Type>.self)
94+
}
95+
}

Sources/Runtime/Metadata/StructMetadata.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import Foundation
2424

25-
struct StructMetadata: MetadataType {
25+
struct StructMetadata: NominalMetadataType {
2626

2727
var pointer: UnsafeMutablePointer<StructMetadataLayout>
2828

0 commit comments

Comments
 (0)