-
Notifications
You must be signed in to change notification settings - Fork 46
/
NominalType.swift
41 lines (36 loc) · 1.33 KB
/
NominalType.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
protocol NominalType : MetadataType {
var nominalTypeDescriptor: NominalTypeDescriptor { get }
}
extension NominalType {
func fieldNamesAndTypes(for type: Any.Type) -> [(String, Any.Type)]? {
return (0..<nominalTypeDescriptor.numberOfFields).map { index in
var context: (String, Any.Type) = ("", Any.self)
getFieldAt(type, index, { name, type, context in
let context = context.assumingMemoryBound(to: (String, Any.Type).self)
context.pointee = (
String(cString: name),
unsafeBitCast(type, to: Any.Type.self)
)
}, &context)
return context
}
}
var fieldOffsets: [Int]? {
let vectorOffset = nominalTypeDescriptor.fieldOffsetVector
guard vectorOffset != 0 else { return nil }
return (0..<nominalTypeDescriptor.numberOfFields).map {
return Int(UnsafePointer<Int32>(pointer)[vectorOffset $0 (is64BitPlatform ? 2 : 0)])
}
}
}
@_silgen_name("swift_getFieldAt")
private func getFieldAt(
_ type: Any.Type,
_ index: Int,
_ callback: @convention(c) (
_ mangledName: UnsafePointer<CChar>,
_ type: UnsafeRawPointer,
_ context: UnsafeMutableRawPointer
) -> Void,
_ context: UnsafeMutableRawPointer
)