Skip to content

Commit 9261fa7

Browse files
authored
Merge pull request #70420 from hyp/eng/virtual-methods++
[cxx-interop] virtual method fixes
2 parents 2ffc8cc + 72db472 commit 9261fa7

File tree

4 files changed

+41
-37
lines changed

4 files changed

+41
-37
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "swift/Basic/PrettyStackTrace.h"
4343
#include "swift/Basic/Statistic.h"
4444
#include "swift/Basic/StringExtras.h"
45+
#include "swift/Basic/Version.h"
4546
#include "swift/ClangImporter/CXXMethodBridging.h"
4647
#include "swift/ClangImporter/ClangImporterRequests.h"
4748
#include "swift/ClangImporter/ClangModule.h"
@@ -3747,33 +3748,39 @@ namespace {
37473748
Decl *VisitCXXMethodDecl(const clang::CXXMethodDecl *decl) {
37483749
auto method = VisitFunctionDecl(decl);
37493750
if (decl->isVirtual() && isa_and_nonnull<ValueDecl>(method)) {
3750-
3751-
if (auto dc = method->getDeclContext();
3752-
!decl->isPure() &&
3753-
isa_and_nonnull<NominalTypeDecl>(dc->getAsDecl())) {
3754-
3755-
// generates the __synthesizedVirtualCall_ C++ thunk
3756-
clang::CXXMethodDecl *cxxThunk = synthesizeCxxVirtualMethod(
3757-
*static_cast<ClangImporter *>(
3758-
dc->getASTContext().getClangModuleLoader()),
3759-
decl->getParent(), decl->getParent(), decl);
3760-
3761-
// call the __synthesizedVirtualCall_ C++ thunk from a Swift thunk
3762-
if (Decl *swiftThunk =
3763-
cxxThunk ? VisitCXXMethodDecl(cxxThunk) : nullptr;
3764-
isa_and_nonnull<FuncDecl>(swiftThunk)) {
3765-
// synthesize the body of the Swift method to call the swiftThunk
3766-
synthesizeForwardingThunkBody(cast<FuncDecl>(method),
3767-
cast<FuncDecl>(swiftThunk));
3768-
return method;
3751+
if (Impl.isCxxInteropCompatVersionAtLeast(
3752+
version::getUpcomingCxxInteropCompatVersion())) {
3753+
if (auto dc = method->getDeclContext();
3754+
!decl->isPure() &&
3755+
isa_and_nonnull<NominalTypeDecl>(dc->getAsDecl())) {
3756+
3757+
// generates the __synthesizedVirtualCall_ C++ thunk
3758+
clang::CXXMethodDecl *cxxThunk = synthesizeCxxVirtualMethod(
3759+
*static_cast<ClangImporter *>(
3760+
dc->getASTContext().getClangModuleLoader()),
3761+
decl->getParent(), decl->getParent(), decl);
3762+
3763+
// call the __synthesizedVirtualCall_ C++ thunk from a Swift thunk
3764+
if (Decl *swiftThunk =
3765+
cxxThunk ? VisitCXXMethodDecl(cxxThunk) : nullptr;
3766+
isa_and_nonnull<FuncDecl>(swiftThunk)) {
3767+
// synthesize the body of the Swift method to call the swiftThunk
3768+
synthesizeForwardingThunkBody(cast<FuncDecl>(method),
3769+
cast<FuncDecl>(swiftThunk));
3770+
return method;
3771+
}
37693772
}
3770-
}
37713773

3772-
Impl.markUnavailable(
3773-
cast<ValueDecl>(method),
3774-
decl->isPure() ?
3775-
"virtual function is not available in Swift because it is pure" :
3776-
"virtual function is not available in Swift");
3774+
Impl.markUnavailable(
3775+
cast<ValueDecl>(method),
3776+
decl->isPure() ? "virtual function is not available in Swift "
3777+
"because it is pure"
3778+
: "virtual function is not available in Swift");
3779+
} else {
3780+
Impl.markUnavailable(
3781+
cast<ValueDecl>(method),
3782+
"virtual functions are not yet available in Swift");
3783+
}
37773784
}
37783785

37793786
if (Impl.SwiftContext.LangOpts.CxxInteropGettersSettersAsProperties ||
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
// RUN: %target-swift-emit-ir -I %S/Inputs -enable-experimental-cxx-interop %s -validate-tbd-against-ir=none | %FileCheck %s
2-
3-
// FIXME: enable on Windows
4-
// XFAIL: OS=windows-msvc
1+
// RUN: %target-swift-emit-ir -I %S/Inputs -cxx-interoperability-mode=upcoming-swift %s -validate-tbd-against-ir=none -Xcc -fignore-exceptions | %FileCheck %s
52

63
import VirtualMethods
74

@@ -18,18 +15,18 @@ d2.f()
1815
d3.f()
1916
d4.f()
2017

21-
// CHECK: invoke {{.*}} @_ZN5Base31fEv
22-
// CHECK: invoke {{.*}} @_ZN8Derived21fEv
23-
// CHECK: invoke {{.*}} @_ZN8Derived31fEv
18+
// CHECK: call {{.*}} @{{_ZN5Base31fEv|"\?f@Base3@@UEAAHXZ"}}
19+
// CHECK: call {{.*}} @{{_ZN8Derived21fEv|"\?f@Derived2@@UEAAHXZ"}}
20+
// CHECK: call {{.*}} @{{_ZN8Derived31fEv|"\?f@Derived3@@UEAAHXZ"}}
2421
// CHECK: call swiftcc {{.*}} @"$sSo8Derived4V1fs5Int32VyF"
2522

2623
// CHECK: define {{.*}} @"$sSo8Derived4V1fs5Int32VyF"(ptr swiftself dereferenceable
27-
// CHECK: invoke {{.*}} @_ZN8Derived423__synthesizedBaseCall_fEv
24+
// CHECK: call {{.*}} @{{_ZN8Derived423__synthesizedBaseCall_fEv|"\?__synthesizedBaseCall_f@Derived4@@QEAAHXZ"}}
2825

29-
// CHECK: define {{.*}}void @{{_ZN7DerivedIiE3fooEv|"\?foo@\?$Derived@H@@UEAAXXZ"}}
26+
// CHECK: define {{.*}}void @{{_ZN7DerivedIiE3fooEv|"\?foo@\?\$Derived@H@@UEAAXXZ"}}
3027
// CHECK: call void @{{_Z21testFunctionCollectedv|"\?testFunctionCollected@@YAXXZ"}}
3128

3229
// CHECK: define {{.*}}void @{{_Z21testFunctionCollectedv|"\?testFunctionCollected@@YAXXZ"}}
3330

3431
// CHECK-NOT: _ZN6UnusedIiE3fooEv
35-
// CHECK-NOT: "\?foo@\?$Unused@H@@UEAAXXZ"
32+
// CHECK-NOT: "\?foo@\?\$Unused@H@@UEAAXXZ"

test/Interop/Cxx/class/inheritance/virtual-methods-module-interface.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-ide-test -print-module -print-implicit-attrs -module-to-print=VirtualMethods -I %S/Inputs -source-filename=x -enable-experimental-cxx-interop | %FileCheck %s
1+
// RUN: %target-swift-ide-test -print-module -cxx-interoperability-mode=upcoming-swift -print-implicit-attrs -module-to-print=VirtualMethods -I %S/Inputs -source-filename=x | %FileCheck %s
22

33
// CHECK: struct Base {
44
// CHECK-NEXT: init()

test/Interop/Cxx/class/inheritance/virtual-methods-typechecker.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-typecheck-verify-swift -verify-ignore-unknown -I %S/Inputs -enable-experimental-cxx-interop
1+
// RUN: %target-typecheck-verify-swift -verify-ignore-unknown -I %S/Inputs -cxx-interoperability-mode=upcoming-swift
22

33
import VirtualMethods
44

0 commit comments

Comments
 (0)