Skip to content

Commit c19ea96

Browse files
committed
[interop] allow virtual methods only for the 'upcoming-swift' release
1 parent bf102d2 commit c19ea96

File tree

4 files changed

+35
-28
lines changed

4 files changed

+35
-28
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"
@@ -3744,33 +3745,39 @@ namespace {
37443745
Decl *VisitCXXMethodDecl(const clang::CXXMethodDecl *decl) {
37453746
auto method = VisitFunctionDecl(decl);
37463747
if (decl->isVirtual() && isa_and_nonnull<ValueDecl>(method)) {
3747-
3748-
if (auto dc = method->getDeclContext();
3749-
!decl->isPure() &&
3750-
isa_and_nonnull<NominalTypeDecl>(dc->getAsDecl())) {
3751-
3752-
// generates the __synthesizedVirtualCall_ C++ thunk
3753-
clang::CXXMethodDecl *cxxThunk = synthesizeCxxVirtualMethod(
3754-
*static_cast<ClangImporter *>(
3755-
dc->getASTContext().getClangModuleLoader()),
3756-
decl->getParent(), decl->getParent(), decl);
3757-
3758-
// call the __synthesizedVirtualCall_ C++ thunk from a Swift thunk
3759-
if (Decl *swiftThunk =
3760-
cxxThunk ? VisitCXXMethodDecl(cxxThunk) : nullptr;
3761-
isa_and_nonnull<FuncDecl>(swiftThunk)) {
3762-
// synthesize the body of the Swift method to call the swiftThunk
3763-
synthesizeForwardingThunkBody(cast<FuncDecl>(method),
3764-
cast<FuncDecl>(swiftThunk));
3765-
return method;
3748+
if (Impl.isCxxInteropCompatVersionAtLeast(
3749+
version::getUpcomingCxxInteropCompatVersion())) {
3750+
if (auto dc = method->getDeclContext();
3751+
!decl->isPure() &&
3752+
isa_and_nonnull<NominalTypeDecl>(dc->getAsDecl())) {
3753+
3754+
// generates the __synthesizedVirtualCall_ C++ thunk
3755+
clang::CXXMethodDecl *cxxThunk = synthesizeCxxVirtualMethod(
3756+
*static_cast<ClangImporter *>(
3757+
dc->getASTContext().getClangModuleLoader()),
3758+
decl->getParent(), decl->getParent(), decl);
3759+
3760+
// call the __synthesizedVirtualCall_ C++ thunk from a Swift thunk
3761+
if (Decl *swiftThunk =
3762+
cxxThunk ? VisitCXXMethodDecl(cxxThunk) : nullptr;
3763+
isa_and_nonnull<FuncDecl>(swiftThunk)) {
3764+
// synthesize the body of the Swift method to call the swiftThunk
3765+
synthesizeForwardingThunkBody(cast<FuncDecl>(method),
3766+
cast<FuncDecl>(swiftThunk));
3767+
return method;
3768+
}
37663769
}
3767-
}
37683770

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

37763783
if (Impl.SwiftContext.LangOpts.CxxInteropGettersSettersAsProperties ||

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-emit-ir -I %S/Inputs -enable-experimental-cxx-interop %s -validate-tbd-against-ir=none | %FileCheck %s
1+
// RUN: %target-swift-emit-ir -I %S/Inputs -cxx-interoperability-mode=upcoming-swift %s -validate-tbd-against-ir=none | %FileCheck %s
22

33
// FIXME: enable on Windows
44
// XFAIL: OS=windows-msvc

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)