@@ -371,7 +371,7 @@ std::string ASTMangler::mangleTypeForDebugger(Type Ty, const DeclContext *DC) {
371
371
372
372
DWARFMangling = true ;
373
373
beginMangling ();
374
-
374
+
375
375
if (DC)
376
376
bindGenericParameters (DC);
377
377
@@ -580,6 +580,33 @@ static StringRef getPrivateDiscriminatorIfNecessary(const ValueDecl *decl) {
580
580
return discriminator.str ();
581
581
}
582
582
583
+ // / If the declaration is an @objc protocol defined in Swift and the
584
+ // / Objective-C name has been overrridden from the default, return the
585
+ // / specified name.
586
+ // /
587
+ // / \param useObjCProtocolNames When false, always returns \c None.
588
+ static Optional<std::string> getOverriddenSwiftProtocolObjCName (
589
+ const ValueDecl *decl,
590
+ bool useObjCProtocolNames) {
591
+ if (!useObjCProtocolNames)
592
+ return None;
593
+
594
+ auto proto = dyn_cast<ProtocolDecl>(decl);
595
+ if (!proto) return None;
596
+
597
+ if (!proto->isObjC ()) return None;
598
+
599
+ // If there is an 'objc' attribute with a name, use that name.
600
+ if (auto objc = proto->getAttrs ().getAttribute <ObjCAttr>()) {
601
+ if (auto name = objc->getName ()) {
602
+ llvm::SmallString<4 > buffer;
603
+ return std::string (name->getString (buffer));
604
+ }
605
+ }
606
+
607
+ return None;
608
+ }
609
+
583
610
void ASTMangler::appendDeclName (const ValueDecl *decl) {
584
611
DeclBaseName name = decl->getBaseName ();
585
612
assert (!name.isSpecial () && " Cannot print special names" );
@@ -604,6 +631,11 @@ void ASTMangler::appendDeclName(const ValueDecl *decl) {
604
631
appendOperator (" oi" );
605
632
break ;
606
633
}
634
+ } else if (auto objCName =
635
+ getOverriddenSwiftProtocolObjCName (decl, UseObjCProtocolNames)) {
636
+ // @objc Swift protocols should be mangled as Objective-C protocols,
637
+ // so append the Objective-C runtime name.
638
+ appendIdentifier (*objCName);
607
639
} else if (!name.empty ()) {
608
640
appendIdentifier (name.getIdentifier ().str ());
609
641
} else {
@@ -1314,7 +1346,8 @@ void ASTMangler::appendImplFunctionType(SILFunctionType *fn) {
1314
1346
}
1315
1347
1316
1348
Optional<ASTMangler::SpecialContext>
1317
- ASTMangler::getSpecialManglingContext (const ValueDecl *decl) {
1349
+ ASTMangler::getSpecialManglingContext (const ValueDecl *decl,
1350
+ bool useObjCProtocolNames) {
1318
1351
// Declarations provided by a C module have a special context mangling.
1319
1352
// known-context ::= 'So'
1320
1353
//
@@ -1329,6 +1362,11 @@ ASTMangler::getSpecialManglingContext(const ValueDecl *decl) {
1329
1362
}
1330
1363
}
1331
1364
1365
+ // If @objc Swift protocols should be mangled as Objective-C protocols,
1366
+ // they are defined in the Objective-C context.
1367
+ if (getOverriddenSwiftProtocolObjCName (decl, useObjCProtocolNames))
1368
+ return ASTMangler::ObjCContext;
1369
+
1332
1370
// Nested types imported from C should also get use the special "So" context.
1333
1371
if (isa<TypeDecl>(decl)) {
1334
1372
if (auto *clangDecl = cast_or_null<clang::NamedDecl>(decl->getClangDecl ())){
@@ -1354,7 +1392,7 @@ ASTMangler::getSpecialManglingContext(const ValueDecl *decl) {
1354
1392
// / This is the top-level entrypoint for mangling <context>.
1355
1393
void ASTMangler::appendContextOf (const ValueDecl *decl) {
1356
1394
// Check for a special mangling context.
1357
- if (auto context = getSpecialManglingContext (decl)) {
1395
+ if (auto context = getSpecialManglingContext (decl, UseObjCProtocolNames )) {
1358
1396
switch (*context) {
1359
1397
case ClangImporterContext:
1360
1398
return appendOperator (" SC" );
0 commit comments