Skip to content

Commit 85a6df3

Browse files
committed
SIL: add lookup(method:) in VTable and WitnessTable
1 parent 1844c14 commit 85a6df3

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

SwiftCompilerSources/Sources/SIL/VTable.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ public struct VTable : CustomStringConvertible, NoReflectionChildren {
9191

9292
public var isSpecialized: Bool { specializedClassType != nil }
9393

94+
/// A lookup for a specific method with O(1) complexity.
95+
public func lookup(method: DeclRef) -> Entry? {
96+
let bridgedEntryOrNil = bridged.lookupMethod(method.bridged)
97+
if bridgedEntryOrNil.hasEntry {
98+
return Entry(bridged: bridgedEntryOrNil.entry)
99+
}
100+
return nil
101+
}
102+
94103
public var description: String {
95104
return String(taking: bridged.getDebugDescription())
96105
}

SwiftCompilerSources/Sources/SIL/WitnessTable.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ public struct WitnessTable : CustomStringConvertible, NoReflectionChildren {
9999
}
100100
}
101101

102+
/// A lookup for a specific method with O(n) complexity.
103+
public func lookup(method: DeclRef) -> Function? {
104+
for entry in entries {
105+
if case .method(let req, let impl) = entry, req == method {
106+
return impl
107+
}
108+
}
109+
return nil
110+
}
111+
102112
public var entries: EntryArray { EntryArray(witnessTable: self) }
103113

104114
public var isDefinition: Bool { !bridged.isDeclaration() }

include/swift/SIL/SILBridging.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,7 @@ struct BridgedVTableEntry {
948948
Override
949949
};
950950

951+
BridgedVTableEntry() : storage{0, 0, 0, 0, 0} {};
951952
BRIDGED_INLINE BridgedVTableEntry(const swift::SILVTableEntry &entry);
952953
BRIDGED_INLINE const swift::SILVTableEntry &unbridged() const;
953954

@@ -961,6 +962,14 @@ struct BridgedVTableEntry {
961962
BridgedDeclRef methodDecl, BridgedFunction implementation);
962963
};
963964

965+
struct OptionalBridgedVTableEntry {
966+
BridgedVTableEntry entry;
967+
bool hasEntry = false;
968+
969+
OptionalBridgedVTableEntry() {}
970+
OptionalBridgedVTableEntry(BridgedVTableEntry e) : entry(e), hasEntry(true) {}
971+
};
972+
964973
struct BridgedVTableEntryArray {
965974
BridgedVTableEntry base;
966975
SwiftInt count;
@@ -973,6 +982,7 @@ struct BridgedVTable {
973982
BRIDGED_INLINE SwiftInt getNumEntries() const;
974983
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedVTableEntry getEntry(SwiftInt index) const;
975984
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedDeclObj getClass() const;
985+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedVTableEntry lookupMethod(BridgedDeclRef member) const;
976986
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedType getSpecializedClassType() const;
977987
BRIDGED_INLINE void replaceEntries(BridgedArrayRef bridgedEntries) const;
978988
};

include/swift/SIL/SILBridgingImpl.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1808,6 +1808,18 @@ BridgedDeclObj BridgedVTable::getClass() const {
18081808
return vTable->getClass();
18091809
}
18101810

1811+
OptionalBridgedVTableEntry BridgedVTable::lookupMethod(BridgedDeclRef member) const {
1812+
if (vTable->getEntries().empty()) {
1813+
return OptionalBridgedVTableEntry();
1814+
}
1815+
swift::SILModule &mod = vTable->getEntries()[0].getImplementation()->getModule();
1816+
if (auto entry = vTable->getEntry(mod, member.unbridged()))
1817+
return BridgedVTableEntry(entry.value());
1818+
1819+
return OptionalBridgedVTableEntry();
1820+
}
1821+
1822+
18111823
BridgedType BridgedVTable::getSpecializedClassType() const {
18121824
return {vTable->getClassType()};
18131825
}

0 commit comments

Comments
 (0)