Skip to content

[cxx-interop] Allow virtual methods to be renamed with SWIFT_NAME #82485

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions lib/ClangImporter/ImportName.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1531,6 +1531,11 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
if (isa<clang::ObjCCategoryDecl>(D))
return ImportedName();

// C++ interop was not available in Swift 2
if (!swift3OrLaterName && isa<clang::CXXMethodDecl>(D)) {
return ImportedName();
}

// Dig out the definition, if there is one.
if (auto def = getDefinitionForClangTypeDecl(D)) {
if (*def)
Expand Down
2 changes: 2 additions & 0 deletions lib/ClangImporter/SwiftDeclSynthesizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2105,6 +2105,8 @@ clang::CXXMethodDecl *SwiftDeclSynthesizer::synthesizeCXXForwardingMethod(
// convention.
newMethod->addAttr(clang::CFReturnsRetainedAttr::CreateImplicit(clangCtx));
}
if (auto swiftNameAttr = method->getAttr<clang::SwiftNameAttr>())
newMethod->addAttr(swiftNameAttr->clone(clangCtx));

llvm::SmallVector<clang::ParmVarDecl *, 4> params;
for (size_t i = 0; i < method->getNumParams(); ++i) {
Expand Down
4 changes: 4 additions & 0 deletions test/Interop/Cxx/class/inheritance/Inputs/virtual-methods.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ inline void testFunctionCollected() {

struct Base {
virtual void foo() = 0;
virtual void virtualRename() const
__attribute__((swift_name("swiftVirtualRename()")));
};

struct Base2 { virtual int f() = 0; };
Expand Down Expand Up @@ -83,6 +85,8 @@ struct IMMORTAL_FRT DerivedFromImmortal : public Immortal {
struct IMMORTAL_FRT Immortal2 {
public:
virtual void virtualMethod(HasDestructor) = 0;
virtual void virtualRename() const
__attribute__((swift_name("swiftVirtualRename()")));
};

inline const ImmortalBase *_Nonnull castToImmortalBase(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
// CHECK-NEXT: public static func staticInBase() -> UnsafePointer<CChar>!
// CHECK-NEXT: @discardableResult
// CHECK-NEXT: public mutating func swiftRenamed(input i: Int32) -> Int32
// CHECK-NEXT: @available(swift, obsoleted: 3, renamed: "swiftRenamed(input:)")
// CHECK-NEXT: public mutating func renamed(_ i: Int32) -> Int32
// CHECK-NEXT: @discardableResult
// CHECK-NEXT: @_effects(readonly) public func pure() -> Int32
// CHECK-NEXT: @discardableResult
Expand Down Expand Up @@ -64,8 +62,6 @@
// CHECK-NEXT: public func returnsNonTrivialInBase() -> NonTrivial
// CHECK-NEXT: @discardableResult
// CHECK-NEXT: public mutating func swiftRenamed(input i: Int32) -> Int32
// CHECK-NEXT: @available(swift, obsoleted: 3, renamed: "swiftRenamed(input:)")
// CHECK-NEXT: public mutating func renamed(_ i: Int32) -> Int32
// CHECK-NEXT: @discardableResult
// CHECK-NEXT: @_effects(readonly) public func pure() -> Int32
// CHECK-NEXT: @discardableResult
Expand Down Expand Up @@ -96,8 +92,6 @@
// CHECK-NEXT: public func returnsNonTrivialInBase() -> NonTrivial
// CHECK-NEXT: @discardableResult
// CHECK-NEXT: public mutating func swiftRenamed(input i: Int32) -> Int32
// CHECK-NEXT: @available(swift, obsoleted: 3, renamed: "swiftRenamed(input:)")
// CHECK-NEXT: public mutating func renamed(_ i: Int32) -> Int32
// CHECK-NEXT: @discardableResult
// CHECK-NEXT: @_effects(readonly) public func pure() -> Int32
// CHECK-NEXT: @discardableResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// CHECK-NEXT: init()
// CHECK-NEXT: @available(*, unavailable, message: "virtual function is not available in Swift because it is pure")
// CHECK-NEXT: mutating func foo()
// CHECK-NEXT: func swiftVirtualRename()
// CHECK: }

// CHECK: struct Base3 {
Expand Down Expand Up @@ -34,3 +35,8 @@
// CHECK: func getOverridden42() -> Int32
// CHECK: func get42() -> Int32
// CHECK: }

// CHECK: class Immortal2 {
// CHECK-NEXT: final func virtualMethod(_: HasDestructor)
// CHECK-NEXT: final func swiftVirtualRename()
// CHECK: }
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,14 @@ func takesDerivedFromImmortal(_ i: DerivedFromImmortal) {
let _ = i.getIntValue()
i.setIntValue(1)
}

func callVirtualRenamedMethod(_ b: Base) {
b.virtualRename() // expected-error {{value of type 'Base' has no member 'virtualRename'}}
b.swiftVirtualRename()
}

@available(SwiftStdlib 5.8, *)
func callsRenamedVirtualMethodsInFRT(_ i: Immortal2) {
i.virtualRename() // expected-error {{value of type 'Immortal2' has no member 'virtualRename'}}
i.swiftVirtualRename()
}
8 changes: 8 additions & 0 deletions test/Interop/Cxx/class/inheritance/virtual-methods.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,12 @@ VirtualMethodsTestSuite.test("C++ virtual method with complex parameter") {
}
#endif

if #available(SwiftStdlib 5.8, *) {
VirtualMethodsTestSuite.test("renamed C++ virtual method") {
func f(immortalClass: Immortal2) {
immortalClass.swiftVirtualRename()
}
}
}

runAllTests()