Skip to content

Commit 5d17074

Browse files
committed
[Static Mirror] Allow ObjCInteropKind to be specified by the client of conformanceReader, separately from how the client builds.
1 parent 1d96037 commit 5d17074

File tree

3 files changed

+85
-92
lines changed

3 files changed

+85
-92
lines changed

include/swift/ABI/Metadata.h

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -540,13 +540,8 @@ namespace {
540540

541541
using TypeContextDescriptor = TargetTypeContextDescriptor<InProcess>;
542542

543-
template<unsigned PointerSize>
544-
using ExternalTypeContextDescriptor
545-
#if SWIFT_OBJC_INTEROP
546-
= TargetTypeContextDescriptor<External<WithObjCInterop<RuntimeTarget<PointerSize>>>>;
547-
#else
548-
= TargetTypeContextDescriptor<External<NoObjCInterop<RuntimeTarget<PointerSize>>>>;
549-
#endif
543+
template<template <typename Runtime> class ObjCInteropKind, unsigned PointerSize>
544+
using ExternalTypeContextDescriptor = TargetTypeContextDescriptor<External<ObjCInteropKind<RuntimeTarget<PointerSize>>>>;
550545

551546
// FIXME: https://bugs.swift.org/browse/SR-1155
552547
#pragma clang diagnostic push
@@ -2115,13 +2110,8 @@ using ProtocolRequirement = TargetProtocolRequirement<InProcess>;
21152110
template<typename Runtime> struct TargetProtocolDescriptor;
21162111
using ProtocolDescriptor = TargetProtocolDescriptor<InProcess>;
21172112

2118-
template<unsigned PointerSize>
2119-
using ExternalProtocolDescriptor
2120-
#if SWIFT_OBJC_INTEROP
2121-
= TargetProtocolDescriptor<External<WithObjCInterop<RuntimeTarget<PointerSize>>>>;
2122-
#else
2123-
= TargetProtocolDescriptor<External<NoObjCInterop<RuntimeTarget<PointerSize>>>>;
2124-
#endif
2113+
template<template <typename Runtime> class ObjCInteropKind, unsigned PointerSize>
2114+
using ExternalProtocolDescriptor = TargetProtocolDescriptor<External<ObjCInteropKind<RuntimeTarget<PointerSize>>>>;
21252115

21262116
/// A witness table for a protocol.
21272117
///
@@ -2866,21 +2856,11 @@ using TargetProtocolConformanceRecord =
28662856

28672857
using ProtocolConformanceRecord = TargetProtocolConformanceRecord<InProcess>;
28682858

2869-
template<unsigned PointerSize>
2870-
using ExternalProtocolConformanceDescriptor
2871-
#if SWIFT_OBJC_INTEROP
2872-
= TargetProtocolConformanceDescriptor<External<WithObjCInterop<RuntimeTarget<PointerSize>>>>;
2873-
#else
2874-
= TargetProtocolConformanceDescriptor<External<NoObjCInterop<RuntimeTarget<PointerSize>>>>;
2875-
#endif
2859+
template<template <typename Runtime> class ObjCInteropKind, unsigned PointerSize>
2860+
using ExternalProtocolConformanceDescriptor = TargetProtocolConformanceDescriptor<External<ObjCInteropKind<RuntimeTarget<PointerSize>>>>;
28762861

2877-
template<unsigned PointerSize>
2878-
using ExternalProtocolConformanceRecord
2879-
#if SWIFT_OBJC_INTEROP
2880-
= TargetProtocolConformanceRecord<External<WithObjCInterop<RuntimeTarget<PointerSize>>>>;
2881-
#else
2882-
= TargetProtocolConformanceRecord<External<NoObjCInterop<RuntimeTarget<PointerSize>>>>;
2883-
#endif
2862+
template<template <typename Runtime> class ObjCInteropKind, unsigned PointerSize>
2863+
using ExternalProtocolConformanceRecord = TargetProtocolConformanceRecord<External<ObjCInteropKind<RuntimeTarget<PointerSize>>>>;
28842864

28852865
template<typename Runtime>
28862866
struct TargetGenericContext;
@@ -2936,13 +2916,8 @@ struct TargetContextDescriptor {
29362916
};
29372917

29382918
using ContextDescriptor = TargetContextDescriptor<InProcess>;
2939-
template<unsigned PointerSize>
2940-
using ExternalContextDescriptor
2941-
#if SWIFT_OBJC_INTEROP
2942-
= TargetContextDescriptor<External<WithObjCInterop<RuntimeTarget<PointerSize>>>>;
2943-
#else
2944-
= TargetContextDescriptor<External<NoObjCInterop<RuntimeTarget<PointerSize>>>>;
2945-
#endif
2919+
template<template <typename Runtime> class ObjCInteropKind, unsigned PointerSize>
2920+
using ExternalContextDescriptor = TargetContextDescriptor<External<ObjCInteropKind<RuntimeTarget<PointerSize>>>>;
29462921

29472922
inline bool isCImportedModuleName(llvm::StringRef name) {
29482923
// This does not include MANGLING_MODULE_CLANG_IMPORTER because that's
@@ -2974,13 +2949,8 @@ struct TargetModuleContextDescriptor final : TargetContextDescriptor<Runtime> {
29742949

29752950
using ModuleContextDescriptor = TargetModuleContextDescriptor<InProcess>;
29762951

2977-
template<unsigned PointerSize>
2978-
using ExternalModuleContextDescriptor
2979-
#if SWIFT_OBJC_INTEROP
2980-
= TargetModuleContextDescriptor<External<WithObjCInterop<RuntimeTarget<PointerSize>>>>;
2981-
#else
2982-
= TargetModuleContextDescriptor<External<NoObjCInterop<RuntimeTarget<PointerSize>>>>;
2983-
#endif
2952+
template<template <typename Runtime> class ObjCInteropKind, unsigned PointerSize>
2953+
using ExternalModuleContextDescriptor = TargetModuleContextDescriptor<External<ObjCInteropKind<RuntimeTarget<PointerSize>>>>;
29842954

29852955
template<typename Runtime>
29862956
inline bool TargetContextDescriptor<Runtime>::isCImportedContext() const {

include/swift/Reflection/TypeRefBuilder.h

Lines changed: 60 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,8 @@ class TypeRefBuilder {
772772

773773
private:
774774
/// Reader of protocol descriptors from Images
775-
template <unsigned PointerSize>
775+
template <template <typename Runtime> class ObjCInteropKind,
776+
unsigned PointerSize>
776777
struct ProtocolConformanceDescriptorReader {
777778
std::string Error;
778779
ByteReader OpaqueByteReader;
@@ -789,16 +790,17 @@ class TypeRefBuilder {
789790
llvm::Optional<std::string>
790791
getParentContextName(uintptr_t contextDescriptorAddress) {
791792
llvm::Optional<std::string> optionalParentContextName;
792-
auto contextTypeDescriptorBytes =
793-
OpaqueByteReader(remote::RemoteAddress(contextDescriptorAddress),
794-
sizeof(ExternalContextDescriptor<PointerSize>));
793+
auto contextTypeDescriptorBytes = OpaqueByteReader(
794+
remote::RemoteAddress(contextDescriptorAddress),
795+
sizeof(ExternalContextDescriptor<ObjCInteropKind, PointerSize>));
795796
if (!contextTypeDescriptorBytes.get()) {
796797
Error = "Failed to read context descriptor.";
797798
return llvm::None;
798799
}
799-
const ExternalContextDescriptor<PointerSize> *contextDescriptor =
800-
(const ExternalContextDescriptor<PointerSize> *)
801-
contextTypeDescriptorBytes.get();
800+
const ExternalContextDescriptor<ObjCInteropKind, PointerSize>
801+
*contextDescriptor =
802+
(const ExternalContextDescriptor<ObjCInteropKind, PointerSize> *)
803+
contextTypeDescriptorBytes.get();
802804

803805
auto parentOffsetAddress = detail::applyRelativeOffset(
804806
(const char *)contextDescriptorAddress,
@@ -817,28 +819,30 @@ class TypeRefBuilder {
817819
auto readContextParentName =
818820
[&](uintptr_t descriptorAddress) -> llvm::Optional<std::string> {
819821
llvm::Optional<std::string> optionalParentName;
820-
auto parentContextDescriptorBytes =
821-
OpaqueByteReader(remote::RemoteAddress(descriptorAddress),
822-
sizeof(ExternalContextDescriptor<PointerSize>));
822+
auto parentContextDescriptorBytes = OpaqueByteReader(
823+
remote::RemoteAddress(descriptorAddress),
824+
sizeof(ExternalContextDescriptor<ObjCInteropKind, PointerSize>));
823825
if (!parentContextDescriptorBytes.get()) {
824826
Error = "Failed to read context descriptor.";
825827
return llvm::None;
826828
}
827-
const ExternalContextDescriptor<PointerSize> *parentContextDescriptor =
828-
(const ExternalContextDescriptor<PointerSize> *)
829+
const ExternalContextDescriptor<ObjCInteropKind,
830+
PointerSize> *parentContextDescriptor =
831+
(const ExternalContextDescriptor<ObjCInteropKind, PointerSize> *)
829832
parentContextDescriptorBytes.get();
830833

831-
if (auto moduleDescriptor =
832-
dyn_cast<ExternalModuleContextDescriptor<PointerSize>>(
833-
parentContextDescriptor)) {
834+
if (auto moduleDescriptor = dyn_cast<
835+
ExternalModuleContextDescriptor<ObjCInteropKind, PointerSize>>(
836+
parentContextDescriptor)) {
834837
auto moduleDescriptorName = readModuleNameFromModuleDescriptor(
835838
moduleDescriptor, parentTargetAddress);
836839
if (!moduleDescriptorName.hasValue())
837840
return llvm::None;
838841
else
839842
optionalParentName = moduleDescriptorName;
840843
} else if (auto typeDescriptor =
841-
dyn_cast<ExternalTypeContextDescriptor<PointerSize>>(
844+
dyn_cast<ExternalTypeContextDescriptor<ObjCInteropKind,
845+
PointerSize>>(
842846
parentContextDescriptor)) {
843847
auto typeDescriptorName = readTypeNameFromTypeDescriptor(
844848
typeDescriptor, parentTargetAddress);
@@ -890,7 +894,8 @@ class TypeRefBuilder {
890894
}
891895

892896
llvm::Optional<std::string> readTypeNameFromTypeDescriptor(
893-
const ExternalTypeContextDescriptor<PointerSize> *typeDescriptor,
897+
const ExternalTypeContextDescriptor<ObjCInteropKind, PointerSize>
898+
*typeDescriptor,
894899
uintptr_t typeDescriptorAddress) {
895900
auto typeNameOffsetAddress =
896901
detail::applyRelativeOffset((const char *)typeDescriptorAddress,
@@ -910,7 +915,8 @@ class TypeRefBuilder {
910915
}
911916

912917
llvm::Optional<std::string> readModuleNameFromModuleDescriptor(
913-
const ExternalModuleContextDescriptor<PointerSize> *moduleDescriptor,
918+
const ExternalModuleContextDescriptor<ObjCInteropKind, PointerSize>
919+
*moduleDescriptor,
914920
uintptr_t moduleDescriptorAddress) {
915921
auto parentNameOffsetAddress = detail::applyRelativeOffset(
916922
(const char *)moduleDescriptorAddress,
@@ -932,16 +938,17 @@ class TypeRefBuilder {
932938
llvm::Optional<std::string> readProtocolNameFromProtocolDescriptor(
933939
uintptr_t protocolDescriptorAddress) {
934940
std::string protocolName;
935-
auto protocolDescriptorBytes =
936-
OpaqueByteReader(remote::RemoteAddress(protocolDescriptorAddress),
937-
sizeof(ExternalProtocolDescriptor<PointerSize>));
941+
auto protocolDescriptorBytes = OpaqueByteReader(
942+
remote::RemoteAddress(protocolDescriptorAddress),
943+
sizeof(ExternalProtocolDescriptor<ObjCInteropKind, PointerSize>));
938944
if (!protocolDescriptorBytes.get()) {
939945
Error = "Error reading protocol descriptor.";
940946
return llvm::None;
941947
}
942-
const ExternalProtocolDescriptor<PointerSize> *protocolDescriptor =
943-
(const ExternalProtocolDescriptor<PointerSize> *)
944-
protocolDescriptorBytes.get();
948+
const ExternalProtocolDescriptor<ObjCInteropKind, PointerSize>
949+
*protocolDescriptor =
950+
(const ExternalProtocolDescriptor<ObjCInteropKind, PointerSize> *)
951+
protocolDescriptorBytes.get();
945952

946953
// Compute the address of the protocol descriptor's name field and read
947954
// the offset
@@ -969,8 +976,8 @@ class TypeRefBuilder {
969976
/// Extract conforming type's name from a Conformance Descriptor
970977
llvm::Optional<std::string> getConformingTypeName(
971978
const uintptr_t conformanceDescriptorAddress,
972-
const ExternalProtocolConformanceDescriptor<PointerSize>
973-
&conformanceDescriptor) {
979+
const ExternalProtocolConformanceDescriptor<
980+
ObjCInteropKind, PointerSize> &conformanceDescriptor) {
974981
std::string typeName;
975982
// Compute the address of the type descriptor as follows:
976983
// - Compute the address of the TypeRef field in the protocol
@@ -998,19 +1005,20 @@ class TypeRefBuilder {
9981005
(const char *)contextDescriptorFieldAddress,
9991006
(int32_t)*contextDescriptorOffset);
10001007

1001-
auto contextTypeDescriptorBytes =
1002-
OpaqueByteReader(remote::RemoteAddress(contextTypeDescriptorAddress),
1003-
sizeof(ExternalContextDescriptor<PointerSize>));
1008+
auto contextTypeDescriptorBytes = OpaqueByteReader(
1009+
remote::RemoteAddress(contextTypeDescriptorAddress),
1010+
sizeof(ExternalContextDescriptor<ObjCInteropKind, PointerSize>));
10041011
if (!contextTypeDescriptorBytes.get()) {
10051012
Error = "Failed to read context descriptor.";
10061013
return llvm::None;
10071014
}
1008-
const ExternalContextDescriptor<PointerSize> *contextDescriptor =
1009-
(const ExternalContextDescriptor<PointerSize> *)
1010-
contextTypeDescriptorBytes.get();
1015+
const ExternalContextDescriptor<ObjCInteropKind, PointerSize>
1016+
*contextDescriptor =
1017+
(const ExternalContextDescriptor<ObjCInteropKind, PointerSize> *)
1018+
contextTypeDescriptorBytes.get();
10111019

10121020
auto typeDescriptor =
1013-
dyn_cast<ExternalTypeContextDescriptor<PointerSize>>(
1021+
dyn_cast<ExternalTypeContextDescriptor<ObjCInteropKind, PointerSize>>(
10141022
contextDescriptor);
10151023
if (!typeDescriptor) {
10161024
Error = "Unexpected type of context descriptor.";
@@ -1037,8 +1045,8 @@ class TypeRefBuilder {
10371045
/// Extract protocol name from a Conformance Descriptor
10381046
llvm::Optional<std::string> getConformanceProtocolName(
10391047
const uintptr_t conformanceDescriptorAddress,
1040-
const ExternalProtocolConformanceDescriptor<PointerSize>
1041-
&conformanceDescriptor) {
1048+
const ExternalProtocolConformanceDescriptor<
1049+
ObjCInteropKind, PointerSize> &conformanceDescriptor) {
10421050
llvm::Optional<std::string> protocolName;
10431051
auto protocolDescriptorFieldAddress = detail::applyRelativeOffset(
10441052
(const char *)conformanceDescriptorAddress,
@@ -1113,25 +1121,27 @@ class TypeRefBuilder {
11131121
readConformanceDescriptor(RemoteRef<void> conformanceRecordRef,
11141122
const std::unordered_map<std::string, std::string>
11151123
&typeNameToManglingMap) {
1116-
const ExternalProtocolConformanceRecord<PointerSize> *CD =
1117-
(const ExternalProtocolConformanceRecord<PointerSize> *)
1118-
conformanceRecordRef.getLocalBuffer();
1124+
const ExternalProtocolConformanceRecord<ObjCInteropKind,
1125+
PointerSize> *CD =
1126+
(const ExternalProtocolConformanceRecord<ObjCInteropKind, PointerSize>
1127+
*)conformanceRecordRef.getLocalBuffer();
11191128
// Read the Protocol Conformance Descriptor by getting its address from
11201129
// the conformance record.
11211130
auto conformanceDescriptorAddress = (uintptr_t)CD->getRelative(
11221131
(void *)conformanceRecordRef.getAddressData());
11231132

11241133
auto descriptorBytes = OpaqueByteReader(
11251134
remote::RemoteAddress(conformanceDescriptorAddress),
1126-
sizeof(ExternalProtocolConformanceDescriptor<PointerSize>));
1135+
sizeof(ExternalProtocolConformanceDescriptor<ObjCInteropKind,
1136+
PointerSize>));
11271137
if (!descriptorBytes.get()) {
11281138
Error = "Failed to read protocol conformance descriptor.";
11291139
return llvm::None;
11301140
}
1131-
const ExternalProtocolConformanceDescriptor<PointerSize>
1141+
const ExternalProtocolConformanceDescriptor<ObjCInteropKind, PointerSize>
11321142
*conformanceDescriptorPtr =
1133-
(const ExternalProtocolConformanceDescriptor<PointerSize> *)
1134-
descriptorBytes.get();
1143+
(const ExternalProtocolConformanceDescriptor<
1144+
ObjCInteropKind, PointerSize> *)descriptorBytes.get();
11351145

11361146
auto optionalConformingTypeName = getConformingTypeName(
11371147
conformanceDescriptorAddress, *conformanceDescriptorPtr);
@@ -1159,7 +1169,8 @@ class TypeRefBuilder {
11591169
};
11601170

11611171
public:
1162-
template <unsigned PointerSize>
1172+
template <template <typename Runtime> class ObjCInteropKind,
1173+
unsigned PointerSize>
11631174
void dumpConformanceSection(std::ostream &stream) {
11641175
// The Fields section has gathered info on types that includes their mangled
11651176
// names. Use that to build a dictionary from a type's demangled name to its
@@ -1180,8 +1191,9 @@ class TypeRefBuilder {
11801191

11811192
// Collect all conformances and aggregate them per-conforming-type.
11821193
std::unordered_map<std::string, std::vector<std::string>> typeConformances;
1183-
ProtocolConformanceDescriptorReader<PointerSize> conformanceReader(
1184-
OpaqueByteReader, OpaqueStringReader, OpaquePointerReader);
1194+
ProtocolConformanceDescriptorReader<ObjCInteropKind, PointerSize>
1195+
conformanceReader(OpaqueByteReader, OpaqueStringReader,
1196+
OpaquePointerReader);
11851197
for (const auto &section : ReflectionInfos) {
11861198
auto ConformanceBegin = section.Conformance.startAddress();
11871199
auto ConformanceEnd = section.Conformance.endAddress();
@@ -1224,7 +1236,8 @@ class TypeRefBuilder {
12241236
}
12251237
}
12261238

1227-
template <unsigned PointerSize>
1239+
template <template <typename Runtime> class ObjCInteropKind,
1240+
unsigned PointerSize>
12281241
void dumpAllSections(std::ostream &stream) {
12291242
stream << "FIELDS:\n";
12301243
stream << "=======\n";
@@ -1244,7 +1257,7 @@ class TypeRefBuilder {
12441257
stream << "\n";
12451258
stream << "CONFORMANCES:\n";
12461259
stream << "=============\n";
1247-
dumpConformanceSection<PointerSize>(stream);
1260+
dumpConformanceSection<ObjCInteropKind, PointerSize>(stream);
12481261
stream << "\n";
12491262
}
12501263
};

tools/swift-reflection-dump/swift-reflection-dump.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,20 @@ static int doDumpReflectionSections(ArrayRef<std::string> BinaryFilenames,
117117
// Dump everything
118118
switch (context.PointerSize) {
119119
case 4:
120-
builder.dumpAllSections<4>(stream);
120+
// FIXME: This could/should be configurable.
121+
#if SWIFT_OBJC_INTEROP
122+
builder.dumpAllSections<WithObjCInterop, 4>(stream);
123+
#else
124+
builder.dumpAllSections<NoObjCInterop, 4>(stream);
125+
#endif
121126
break;
122-
case 8:
123-
builder.dumpAllSections<8>(stream);
127+
case 8:
128+
// FIXME: This could/should be configurable.
129+
#if SWIFT_OBJC_INTEROP
130+
builder.dumpAllSections<WithObjCInterop, 8>(stream);
131+
#else
132+
builder.dumpAllSections<NoObjCInterop, 8>(stream);
133+
#endif
124134
break;
125135
default:
126136
fputs("unsupported word size in object file\n", stderr);

0 commit comments

Comments
 (0)