Skip to content

IRGen: Generate runtime type manglings using ObjC runtime names. #30250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/swift/AST/ASTMangler.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ class ASTMangler : public Mangler {
bool OptimizeProtocolNames = true;

/// If enabled, use Objective-C runtime names when mangling @objc Swift
/// protocols.
bool UseObjCProtocolNames = false;
/// protocols and classes.
bool UseObjCRuntimeNames = false;

/// If enabled, non-canonical types are allowed and type alias types get a
/// special mangling.
Expand Down
27 changes: 20 additions & 7 deletions lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ void ASTMangler::appendDeclName(const ValueDecl *decl) {
break;
}
} else if (auto objCName =
getOverriddenSwiftProtocolObjCName(decl, UseObjCProtocolNames)) {
getOverriddenSwiftProtocolObjCName(decl, UseObjCRuntimeNames)) {
// @objc Swift protocols should be mangled as Objective-C protocols,
// so append the Objective-C runtime name.
appendIdentifier(*objCName);
Expand Down Expand Up @@ -1298,7 +1298,7 @@ void ASTMangler::appendBoundGenericArgs(Type type, bool &isFirstArgList) {
genericArgs = boundType->getGenericArgs();
if (Type parent = boundType->getParent()) {
GenericTypeDecl *decl = boundType->getAnyGeneric();
if (!getSpecialManglingContext(decl, UseObjCProtocolNames))
if (!getSpecialManglingContext(decl, UseObjCRuntimeNames))
appendBoundGenericArgs(parent->getDesugaredType(), isFirstArgList);
}
}
Expand Down Expand Up @@ -1610,7 +1610,7 @@ ASTMangler::getSpecialManglingContext(const ValueDecl *decl,
/// This is the top-level entrypoint for mangling <context>.
void ASTMangler::appendContextOf(const ValueDecl *decl) {
// Check for a special mangling context.
if (auto context = getSpecialManglingContext(decl, UseObjCProtocolNames)) {
if (auto context = getSpecialManglingContext(decl, UseObjCRuntimeNames)) {
switch (*context) {
case ClangImporterContext:
return appendOperator("SC");
Expand Down Expand Up @@ -1857,7 +1857,10 @@ void ASTMangler::appendProtocolName(const ProtocolDecl *protocol,

appendContextOf(protocol);
auto *clangDecl = protocol->getClangDecl();
if (auto *clangProto = cast_or_null<clang::ObjCProtocolDecl>(clangDecl))
auto clangProto = cast_or_null<clang::ObjCProtocolDecl>(clangDecl);
if (clangProto && UseObjCRuntimeNames)
appendIdentifier(clangProto->getObjCRuntimeNameAsString());
else if (clangProto)
appendIdentifier(clangProto->getName());
else
appendDeclName(protocol);
Expand Down Expand Up @@ -1928,15 +1931,25 @@ void ASTMangler::appendAnyGenericType(const GenericTypeDecl *decl) {
if (!namedDecl)
return false;

appendIdentifier(namedDecl->getName());
// Mangle ObjC classes using their runtime names.
auto interface = dyn_cast<clang::ObjCInterfaceDecl>(namedDecl);
auto protocol = dyn_cast<clang::ObjCProtocolDecl>(namedDecl);

if (UseObjCRuntimeNames && interface) {
appendIdentifier(interface->getObjCRuntimeNameAsString());
} else if (UseObjCRuntimeNames && protocol) {
appendIdentifier(protocol->getObjCRuntimeNameAsString());
} else {
appendIdentifier(namedDecl->getName());
}

// The important distinctions to maintain here are Objective-C's various
// namespaces: protocols, tags (struct/enum/union), and unqualified names.
// We continue to mangle "class" the standard Swift way because it feels
// weird to call that an alias, but they're really in the same namespace.
if (isa<clang::ObjCInterfaceDecl>(namedDecl)) {
if (interface) {
appendOperator("C");
} else if (isa<clang::ObjCProtocolDecl>(namedDecl)) {
} else if (protocol) {
appendOperator("P");
} else if (isa<clang::TagDecl>(namedDecl)) {
// Note: This includes enums, but that's okay. A Clang enum is not always
Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/IRGenMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ IRGenMangler::withSymbolicReferences(IRGenModule &IGM,
llvm::function_ref<void ()> body) {
Mod = IGM.getSwiftModule();
OptimizeProtocolNames = false;
UseObjCProtocolNames = true;
UseObjCRuntimeNames = true;

llvm::SaveAndRestore<bool>
AllowSymbolicReferencesLocally(AllowSymbolicReferences);
Expand Down
11 changes: 11 additions & 0 deletions test/IRGen/Inputs/objc_runtime_name_clang_attr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@import Foundation;

__attribute__((objc_runtime_name("ObjCRuntimeNameIsDifferent")))
@interface ObjCRuntimeNamed: NSObject

@end

__attribute__((objc_runtime_name("ObjCProtoRuntimeNameIsDifferent")))
@protocol ObjCProtoRuntimeNamed

@end
19 changes: 19 additions & 0 deletions test/IRGen/objc_runtime_name_clang_attr.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-ir -import-objc-header %S/Inputs/objc_runtime_name_clang_attr.h %s | %FileCheck %s
// REQUIRES: objc_interop

// Use the runtime name for runtime instantiation
// CHECK-LABEL: @"$sSo16ObjCRuntimeNamedCSgMD" = {{.*}}@"symbolic So26ObjCRuntimeNameIsDifferentCSg"
public func getMetadata() -> Any.Type {
return ObjCRuntimeNamed?.self
}
// CHECK-LABEL: @"$sSo21ObjCProtoRuntimeNamed_pSgMD" = {{.*}}@"symbolic So31ObjCProtoRuntimeNameIsDifferent_pSg"
public func getMetadata2() -> Any.Type {
return ObjCProtoRuntimeNamed?.self
}

// Use the source name for symbols to avoid breaking ABI.
// CHECK-LABEL: define{{.*}}3fooyySo16ObjCRuntimeNamedCF
public func foo(_: ObjCRuntimeNamed) {}

// CHECK-LABEL: define{{.*}}3fooyySo21ObjCProtoRuntimeNamed_pF
public func foo(_: ObjCProtoRuntimeNamed) {}