@@ -498,20 +498,39 @@ class MetadataReader {
498
498
#if SWIFT_OBJC_INTEROP
499
499
// Check whether we have an Objective-C protocol.
500
500
if (ProtocolAddress.isObjC ()) {
501
- auto MangledNameStr =
501
+ auto Name =
502
502
readObjCProtocolName (ProtocolAddress.getObjCProtocol ());
503
+ StringRef NameStr (Name);
504
+
505
+ // If this is a Swift-defined protocol, demangle it.
506
+ if (NameStr.startswith (" _TtP" )) {
507
+ Demangle::Context DCtx;
508
+ auto Demangled = DCtx.demangleSymbolAsNode (NameStr);
509
+ if (!Demangled)
510
+ return BuiltType ();
511
+
512
+ // FIXME: This appears in _swift_buildDemanglingForMetadata().
513
+ while (Demangled->getKind () == Node::Kind::Global ||
514
+ Demangled->getKind () == Node::Kind::TypeMangling ||
515
+ Demangled->getKind () == Node::Kind::Type ||
516
+ Demangled->getKind () == Node::Kind::ProtocolList ||
517
+ Demangled->getKind () == Node::Kind::TypeList ||
518
+ Demangled->getKind () == Node::Kind::Type) {
519
+ if (Demangled->getNumChildren () != 1 )
520
+ return BuiltType ();
521
+ Demangled = Demangled->getFirstChild ();
522
+ }
523
+
524
+ auto Protocol = Builder.createProtocolDecl (Demangled);
525
+ if (!Protocol)
526
+ return BuiltType ();
527
+
528
+ Protocols.push_back (Protocol);
529
+ continue ;
530
+ }
503
531
504
- StringRef MangledName =
505
- Demangle::dropSwiftManglingPrefix (MangledNameStr);
506
-
507
- Demangle::Context DCtx;
508
- auto Demangled = DCtx.demangleTypeAsNode (MangledName);
509
- if (!Demangled)
510
- return BuiltType ();
511
-
512
- // FIXME: Imported protocols?
513
-
514
- auto Protocol = Builder.createProtocolDecl (Demangled);
532
+ // Otherwise, this is an imported protocol.
533
+ auto Protocol = Builder.createObjCProtocolDecl (NameStr);
515
534
if (!Protocol)
516
535
return BuiltType ();
517
536
@@ -520,6 +539,7 @@ class MetadataReader {
520
539
}
521
540
#endif
522
541
542
+ // Swift-native protocol.
523
543
Demangle::Demangler Dem;
524
544
auto Demangled = readDemanglingForContextDescriptor (
525
545
ProtocolAddress.getSwiftProtocol (), Dem);
0 commit comments