|
| 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 | +} |
0 commit comments