21
21
#include " swift/Remote/MemoryReader.h"
22
22
#include " swift/Demangling/Demangler.h"
23
23
#include " swift/Demangling/TypeDecoder.h"
24
+ #include " swift/Basic/Defer.h"
24
25
#include " swift/Basic/Range.h"
25
26
#include " swift/Basic/LLVM.h"
26
27
#include " swift/Runtime/Unreachable.h"
@@ -359,8 +360,31 @@ class MetadataReader {
359
360
360
361
std::vector<BuiltProtocolDecl> Protocols;
361
362
for (auto ProtocolAddress : Exist->getProtocols ()) {
362
- auto ProtocolDescriptor = readProtocolDescriptor (
363
- ProtocolAddress.getProtocolDescriptorUnchecked ());
363
+ #if SWIFT_OBJC_INTEROP
364
+ // Check whether we have an Objective-C protocol.
365
+ if (ProtocolAddress.isObjC ()) {
366
+ auto MangledNameStr =
367
+ readObjCProtocolName (ProtocolAddress.getObjCProtocol ());
368
+
369
+ StringRef MangledName =
370
+ Demangle::dropSwiftManglingPrefix (MangledNameStr);
371
+
372
+ Demangle::Context DCtx;
373
+ auto Demangled = DCtx.demangleTypeAsNode (MangledName);
374
+ if (!Demangled)
375
+ return BuiltType ();
376
+
377
+ auto Protocol = Builder.createProtocolDecl (Demangled);
378
+ if (!Protocol)
379
+ return BuiltType ();
380
+
381
+ Protocols.push_back (Protocol);
382
+ continue ;
383
+ }
384
+ #endif
385
+
386
+ auto ProtocolDescriptor = readSwiftProtocolDescriptor (
387
+ ProtocolAddress.getSwiftProtocol ());
364
388
if (!ProtocolDescriptor)
365
389
return BuiltType ();
366
390
@@ -1270,7 +1294,7 @@ class MetadataReader {
1270
1294
}
1271
1295
1272
1296
OwnedProtocolDescriptorRef
1273
- readProtocolDescriptor (StoredPointer Address) {
1297
+ readSwiftProtocolDescriptor (StoredPointer Address) {
1274
1298
auto Size = sizeof (TargetProtocolDescriptor<Runtime>);
1275
1299
auto Buffer = (uint8_t *)malloc (Size);
1276
1300
if (!Reader->readBytes (RemoteAddress (Address), Buffer, Size)) {
@@ -1282,6 +1306,27 @@ class MetadataReader {
1282
1306
return OwnedProtocolDescriptorRef (Casted);
1283
1307
}
1284
1308
1309
+ #if SWIFT_OBJC_INTEROP
1310
+ std::string readObjCProtocolName (StoredPointer Address) {
1311
+ auto Size = sizeof (TargetObjCProtocolPrefix<Runtime>);
1312
+ auto Buffer = (uint8_t *)malloc (Size);
1313
+ SWIFT_DEFER {
1314
+ free (Buffer);
1315
+ };
1316
+
1317
+ if (!Reader->readBytes (RemoteAddress (Address), Buffer, Size))
1318
+ return std::string ();
1319
+
1320
+ auto ProtocolDescriptor
1321
+ = reinterpret_cast <TargetObjCProtocolPrefix<Runtime> *>(Buffer);
1322
+ std::string Name;
1323
+ if (!Reader->readString (RemoteAddress (ProtocolDescriptor->Name ), Name))
1324
+ return std::string ();
1325
+
1326
+ return Name;
1327
+ }
1328
+ #endif
1329
+
1285
1330
// TODO: We need to be able to produce protocol conformances for each
1286
1331
// substitution type as well in order to accurately rebuild bound generic
1287
1332
// types or types in protocol-constrained inner contexts.
0 commit comments