Skip to content

Commit 7727e5e

Browse files
committed
[Runtime] Use a prefix Objective-C protocol layout to extract the name.
Introduce TargetObjCProtocolPrefix, which describes just enough of the Objective-C runtime’s Protocol structure to extract the name without having to call Objective-C’s protocol_getName().
1 parent 89e3220 commit 7727e5e

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

include/swift/ABI/Metadata.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1577,6 +1577,19 @@ using TupleTypeMetadata = TargetTupleTypeMetadata<InProcess>;
15771577

15781578
template <typename Runtime> struct TargetProtocolDescriptor;
15791579

1580+
#if SWIFT_OBJC_INTEROP
1581+
/// Layout of a small prefix of an Objective-C protocol, used only to
1582+
/// directly extract the name of the protocol.
1583+
template <typename Runtime>
1584+
struct TargetObjCProtocolPrefix {
1585+
/// Unused by the Swift runtime.
1586+
TargetPointer<Runtime, const void> _ObjC_Isa;
1587+
1588+
/// The mangled name of the protocol.
1589+
TargetPointer<Runtime, const char> Name;
1590+
};
1591+
#endif
1592+
15801593
/// A reference to a protocol within the runtime, which may be either
15811594
/// a Swift protocol or (when Objective-C interoperability is enabled) an
15821595
/// Objective-C protocol.
@@ -1647,7 +1660,14 @@ class TargetProtocolDescriptorRef {
16471660

16481661
/// The name of the protocol.
16491662
TargetPointer<Runtime, const char> getName() const {
1650-
return getProtocolDescriptorUnchecked()->Name;
1663+
#if SWIFT_OBJC_INTEROP
1664+
if (isObjC()) {
1665+
return reinterpret_cast<TargetObjCProtocolPrefix<Runtime> *>(
1666+
getObjCProtocol())->Name;
1667+
}
1668+
#endif
1669+
1670+
return getSwiftProtocol()->Name;
16511671
}
16521672

16531673
/// Determine what kind of protocol this is, Swift or Objective-C.

0 commit comments

Comments
 (0)