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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
//===--- WitnessTable.swift -----------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import AST
import SILBridging
public struct WitnessTable : CustomStringConvertible, NoReflectionChildren {
public let bridged: BridgedWitnessTable
public init(bridged: BridgedWitnessTable) { self.bridged = bridged }
public enum Entry : CustomStringConvertible, NoReflectionChildren {
case invalid
/// A witness table entry describing the witness for a method.
/// The witness can be nil in case dead function elimination has removed the method
/// or if the method was not serialized (for de-serialized witness tables).
case method(requirement: DeclRef, witness: Function?)
/// A witness table entry describing the witness for an associated type.
case associatedType(requirement: AssociatedTypeDecl, witness: CanonicalType)
/// A witness table entry describing the witness for an associated type's protocol requirement.
case associatedConformance(requirement: CanonicalType, protocol: ProtocolDecl, witness: Conformance)
/// A witness table entry referencing the protocol conformance for a refined base protocol.
case baseProtocol(requirement: ProtocolDecl, witness: Conformance)
fileprivate init(bridged: BridgedWitnessTableEntry) {
switch bridged.getKind() {
case .invalid:
self = .invalid
case .method:
self = .method(requirement: DeclRef(bridged: bridged.getMethodRequirement()),
witness: bridged.getMethodWitness().function)
case .associatedType:
self = .associatedType(requirement: bridged.getAssociatedTypeRequirement().getAs(AssociatedTypeDecl.self),
witness: CanonicalType(bridged: bridged.getAssociatedTypeWitness()))
case .associatedConformance:
self = .associatedConformance(requirement: CanonicalType(bridged: bridged.getAssociatedConformanceRequirement()),
protocol: bridged.getAssociatedConformanceDecl().getAs(ProtocolDecl.self),
witness: Conformance(bridged: bridged.getAssociatedConformanceWitness()))
case .baseProtocol:
self = .baseProtocol(requirement: bridged.getBaseProtocolRequirement().getAs(ProtocolDecl.self),
witness: Conformance(bridged: bridged.getBaseProtocolWitness()))
default:
fatalError("invalid witness table entry")
}
}
public var description: String {
return String(taking: bridged.getDebugDescription())
}
public var bridged: BridgedWitnessTableEntry {
switch self {
case .invalid:
return BridgedWitnessTableEntry.createInvalid()
case .method(let requirement, let witness):
return BridgedWitnessTableEntry.createMethod(requirement.bridged,
OptionalBridgedFunction(obj: witness?.bridged.obj))
case .associatedType(let requirement, let witness):
return BridgedWitnessTableEntry.createAssociatedType(requirement.bridged, witness.bridged)
case .associatedConformance(let requirement, let protocolDecl, let witness):
return BridgedWitnessTableEntry.createAssociatedConformance(requirement.bridged,
protocolDecl.bridged,
witness.bridged)
case .baseProtocol(let requirement, let witness):
return BridgedWitnessTableEntry.createBaseProtocol(requirement.bridged, witness.bridged)
}
}
}
public struct EntryArray : BridgedRandomAccessCollection {
fileprivate let bridgedTable: BridgedWitnessTable
public let count: Int
init(witnessTable: WitnessTable) {
self.bridgedTable = witnessTable.bridged
self.count = witnessTable.bridged.getNumEntries()
}
public var startIndex: Int { 0 }
public var endIndex: Int { count }
public subscript(_ index: Int) -> Entry {
precondition(index >= startIndex && index < endIndex)
return Entry(bridged: bridgedTable.getEntry(index))
}
}
public var entries: EntryArray { EntryArray(witnessTable: self) }
public var isDefinition: Bool { !bridged.isDeclaration() }
public var description: String {
return String(taking: bridged.getDebugDescription())
}
}
public struct DefaultWitnessTable : CustomStringConvertible, NoReflectionChildren {
public let bridged: BridgedDefaultWitnessTable
public init(bridged: BridgedDefaultWitnessTable) { self.bridged = bridged }
public typealias Entry = WitnessTable.Entry
public struct EntryArray : BridgedRandomAccessCollection {
fileprivate let bridgedTable: BridgedDefaultWitnessTable
public let count: Int
init(witnessTable: DefaultWitnessTable) {
self.bridgedTable = witnessTable.bridged
self.count = witnessTable.bridged.getNumEntries()
}
public var startIndex: Int { 0 }
public var endIndex: Int { count }
public subscript(_ index: Int) -> Entry {
precondition(index >= startIndex && index < endIndex)
return Entry(bridged: bridgedTable.getEntry(index))
}
}
public var entries: EntryArray { EntryArray(witnessTable: self) }
public var description: String {
return String(taking: bridged.getDebugDescription())
}
}
extension OptionalBridgedWitnessTable {
public var witnessTable: WitnessTable? {
if let table = table {
return WitnessTable(bridged: BridgedWitnessTable(table: table))
}
return nil
}
}
extension OptionalBridgedDefaultWitnessTable {
public var defaultWitnessTable: DefaultWitnessTable? {
if let table = table {
return DefaultWitnessTable(bridged: BridgedDefaultWitnessTable(table: table))
}
return nil
}
}
|