Skip to content

Commit 320e4c6

Browse files
authored
Merge pull request swiftlang#42511 from zoecarver/add-flag-import-as-computed-property
2 parents 0cf20db + 2119290 commit 320e4c6

File tree

7 files changed

+29
-9
lines changed

7 files changed

+29
-9
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ namespace swift {
281281
/// disabled because it is not complete.
282282
bool EnableCXXInterop = false;
283283

284+
/// Imports getters and setters as computed properties.
285+
bool CxxInteropGettersSettersAsProperties = false;
286+
284287
/// On Darwin platforms, use the pre-stable ABI's mark bit for Swift
285288
/// classes instead of the stable ABI's bit. This is needed when
286289
/// targeting OSes prior to macOS 10.14.4 and iOS 12.2, where

include/swift/Option/FrontendOptions.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,11 @@ def enable_cxx_interop :
836836
HelpText<"Alias for -enable-experimental-cxx-interop">,
837837
Flags<[FrontendOption, HelpHidden]>;
838838

839+
def cxx_interop_getters_setters_as_properties :
840+
Flag<["-"], "cxx-interop-getters-setters-as-properties">,
841+
HelpText<"Import getters and setters as computed properties in Swift">,
842+
Flags<[FrontendOption, HelpHidden]>;
843+
839844
def use_malloc : Flag<["-"], "use-malloc">,
840845
HelpText<"Allocate internal data structures using malloc "
841846
"(for memory debugging)">;

lib/ClangImporter/ImportDecl.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4480,13 +4480,15 @@ namespace {
44804480
Decl *VisitCXXMethodDecl(const clang::CXXMethodDecl *decl) {
44814481
auto method = VisitFunctionDecl(decl);
44824482

4483-
CXXMethodBridging bridgingInfo(decl);
4484-
if (bridgingInfo.classify() == CXXMethodBridging::Kind::getter) {
4485-
auto name = bridgingInfo.getClangName().drop_front(3);
4486-
Impl.GetterSetterMap[name].first = static_cast<FuncDecl *>(method);
4487-
} else if (bridgingInfo.classify() == CXXMethodBridging::Kind::setter) {
4488-
auto name = bridgingInfo.getClangName().drop_front(3);
4489-
Impl.GetterSetterMap[name].second = static_cast<FuncDecl *>(method);
4483+
if (Impl.SwiftContext.LangOpts.CxxInteropGettersSettersAsProperties) {
4484+
CXXMethodBridging bridgingInfo(decl);
4485+
if (bridgingInfo.classify() == CXXMethodBridging::Kind::getter) {
4486+
auto name = bridgingInfo.getClangName().drop_front(3);
4487+
Impl.GetterSetterMap[name].first = static_cast<FuncDecl *>(method);
4488+
} else if (bridgingInfo.classify() == CXXMethodBridging::Kind::setter) {
4489+
auto name = bridgingInfo.getClangName().drop_front(3);
4490+
Impl.GetterSetterMap[name].second = static_cast<FuncDecl *>(method);
4491+
}
44904492
}
44914493

44924494
return method;

lib/Frontend/CompilerInvocation.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,8 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
807807
Args.hasFlag(OPT_enable_objc_interop, OPT_disable_objc_interop,
808808
Target.isOSDarwin());
809809

810+
Opts.CxxInteropGettersSettersAsProperties = Args.hasArg(OPT_cxx_interop_getters_setters_as_properties);
811+
810812
Opts.VerifyAllSubstitutionMaps |= Args.hasArg(OPT_verify_all_substitution_maps);
811813

812814
Opts.EnableVolatileModules |= Args.hasArg(OPT_enable_volatile_modules);

test/Interop/Cxx/ergonomics/implicit-computed-properties-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 -module-to-print=ImplicitComputedProperties -I %S/Inputs -source-filename=x -enable-experimental-cxx-interop | %FileCheck %s
1+
// RUN: %target-swift-ide-test -print-module -module-to-print=ImplicitComputedProperties -I %S/Inputs -source-filename=x -enable-experimental-cxx-interop -cxx-interop-getters-setters-as-properties | %FileCheck %s
22

33
// CHECK: struct VoidGetter {
44
// CHECK-NOT: var

test/Interop/Cxx/ergonomics/implicit-computed-properties.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop)
1+
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop -Xfrontend -cxx-interop-getters-setters-as-properties)
22
//
33
// REQUIRES: executable_test
44
//

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,11 @@ static llvm::cl::opt<bool>
766766
llvm::cl::desc("Enable C++ interop."),
767767
llvm::cl::cat(Category), llvm::cl::init(false));
768768

769+
static llvm::cl::opt<bool>
770+
CxxInteropGettersSettersAsProperties("cxx-interop-getters-setters-as-properties",
771+
llvm::cl::desc("Imports getters and setters as computed properties."),
772+
llvm::cl::cat(Category), llvm::cl::init(false));
773+
769774
static llvm::cl::opt<bool>
770775
CanonicalizeType("canonicalize-type", llvm::cl::Hidden,
771776
llvm::cl::cat(Category), llvm::cl::init(false));
@@ -4277,6 +4282,9 @@ int main(int argc, char *argv[]) {
42774282
if (options::EnableCxxInterop) {
42784283
InitInvok.getLangOptions().EnableCXXInterop = true;
42794284
}
4285+
if (options::CxxInteropGettersSettersAsProperties) {
4286+
InitInvok.getLangOptions().CxxInteropGettersSettersAsProperties = true;
4287+
}
42804288
if (options::EnableExperimentalConcurrency) {
42814289
InitInvok.getLangOptions().EnableExperimentalConcurrency = true;
42824290
}

0 commit comments

Comments
 (0)