Skip to content

Commit 3e419b2

Browse files
committed
[cxx-interop] Implicitly import Cxx module
This lifts the requirement for the user to explicitly add `import Cxx` in Swift code that relies on protocols from the `Cxx` module, such as `CxxSequence`.
1 parent 3daa875 commit 3e419b2

File tree

6 files changed

+12
-19
lines changed

6 files changed

+12
-19
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3274,19 +3274,6 @@ static void getImportDecls(ClangModuleUnit *ClangUnit, const clang::Module *M,
32743274
auto *ID = createImportDecl(Ctx, ClangUnit, ImportedMod, Exported);
32753275
Results.push_back(ID);
32763276
}
3277-
3278-
if (Ctx.LangOpts.EnableCXXInterop && requiresCPlusPlus(M)) {
3279-
// Try to load the Cxx module. We don't use it directly here, but we need to
3280-
// make sure that ASTContext has loaded this module.
3281-
auto *cxxModule = Ctx.getModuleByIdentifier(Ctx.Id_Cxx);
3282-
if (cxxModule) {
3283-
ImportPath::Builder builder(Ctx.Id_Cxx);
3284-
auto *importCxx =
3285-
ImportDecl::create(Ctx, ClangUnit, SourceLoc(), ImportKind::Module,
3286-
SourceLoc(), builder.get());
3287-
Results.push_back(importCxx);
3288-
}
3289-
}
32903277
}
32913278

32923279
void ClangModuleUnit::getDisplayDecls(SmallVectorImpl<Decl*> &results, bool recursive) const {
@@ -3923,6 +3910,17 @@ void ClangModuleUnit::getImportedModulesForLookup(
39233910
} else {
39243911
clangModule->getExportedModules(imported);
39253912
topLevel = clangModule->getTopLevelModule();
3913+
3914+
// If this is a C++ module, implicitly import the Cxx module, which contains
3915+
// definitions of Swift protocols that C++ types might conform to, such as
3916+
// CxxSequence.
3917+
if (owner.SwiftContext.LangOpts.EnableCXXInterop &&
3918+
requiresCPlusPlus(clangModule) && clangModule->Name != "CxxShim") {
3919+
auto *cxxModule =
3920+
owner.SwiftContext.getModuleByIdentifier(owner.SwiftContext.Id_Cxx);
3921+
if (cxxModule)
3922+
imports.push_back({ImportPath::Access(), cxxModule});
3923+
}
39263924
}
39273925

39283926
if (imported.empty()) {

lib/IRGen/GenDecl.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ void IRGenModule::emitSourceFile(SourceFile &SF) {
474474
this->addLinkLibrary(LinkLibrary("c++", LibraryKind::Library));
475475
else if (Context.LangOpts.Target.isOSLinux())
476476
this->addLinkLibrary(LinkLibrary("stdc++", LibraryKind::Library));
477+
this->addLinkLibrary(LinkLibrary("swiftCxx", LibraryKind::Library));
477478
}
478479

479480
// FIXME: It'd be better to have the driver invocation or build system that

test/Interop/Cxx/stdlib/overlay/custom-iterator-module-interface.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// RUN: %target-swift-ide-test -print-module -module-to-print=CustomSequence -source-filename=x -I %S/Inputs -enable-experimental-cxx-interop -module-cache-path %t | %FileCheck %s
22

3-
// CHECK: import Cxx
4-
53
// CHECK: struct ConstIterator : UnsafeCxxInputIterator {
64
// CHECK: var pointee: Int32 { get }
75
// CHECK: func successor() -> ConstIterator

test/Interop/Cxx/stdlib/overlay/custom-sequence-module-interface.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// RUN: %target-swift-ide-test -print-module -module-to-print=CustomSequence -source-filename=x -I %S/Inputs -enable-experimental-cxx-interop -module-cache-path %t | %FileCheck %s
22

3-
// CHECK: import Cxx
4-
53
// CHECK: struct SimpleSequence : CxxSequence {
64
// CHECK: typealias Element = ConstIterator.Pointee
75
// CHECK: typealias Iterator = CxxIterator<SimpleSequence>

test/Interop/Cxx/stdlib/overlay/custom-sequence-typechecker.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// RUN: %target-typecheck-verify-swift -I %S/Inputs -enable-experimental-cxx-interop
22

33
import CustomSequence
4-
import Cxx
54

65
func checkIntSequence<S>(_ seq: S) where S: Sequence, S.Element == Int32 {
76
let contains = seq.contains(where: { $0 == 3 })

test/Interop/Cxx/stdlib/overlay/custom-sequence.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import StdlibUnittest
77
import CustomSequence
8-
import Cxx
98

109
var CxxSequenceTestSuite = TestSuite("CxxSequence")
1110

0 commit comments

Comments
 (0)