Skip to content

Commit 6ea604b

Browse files
committed
Always add an implicit import of 'Cxx' module when C++ Interop is enabled.
We cannot always rely on being able to do so only as an overlay query upon loading 'requires cplusplus' modulemap modules. The 'requires' statement only applies to submodules, and we may not be able to query language feature modulemap attributes in dependency scanning context.
1 parent eb1c0e7 commit 6ea604b

File tree

5 files changed

+24
-7
lines changed

5 files changed

+24
-7
lines changed

include/swift/Frontend/Frontend.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,9 @@ class CompilerInstance {
660660
/// i.e. if it can be found.
661661
bool canImportSwiftBacktracing() const;
662662

663+
/// Whether the Cxx library can be imported
664+
bool canImportCxx() const;
665+
663666
/// Whether the CxxShim library can be imported
664667
/// i.e. if it can be found.
665668
bool canImportCxxShim() const;

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class SerializedModuleLoaderBase : public ModuleLoader {
172172
};
173173

174174
llvm::ErrorOr<BinaryModuleImports>
175-
getImportsOfModule(Twine modulePath,
175+
getImportsOfModule(const ModuleFileSharedCore &loadedModuleFile,
176176
ModuleLoadingBehavior transitiveBehavior,
177177
StringRef packageName, bool isTestableImport);
178178

include/swift/Strings.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ constexpr static const StringLiteral SWIFT_BACKTRACING_NAME = "_Backtracing";
3939
constexpr static const StringLiteral SWIFT_SHIMS_NAME = "SwiftShims";
4040
/// The name of the CxxShim module, which contains a cxx casting utility.
4141
constexpr static const StringLiteral CXX_SHIM_NAME = "CxxShim";
42+
/// The name of the Cxx module, which contains C++ interop helper protocols.
43+
constexpr static const StringLiteral CXX_MODULE_NAME = "Cxx";
4244
/// The name of the Builtin module, which contains Builtin functions.
4345
constexpr static const StringLiteral BUILTIN_NAME = "Builtin";
4446
/// The name of the clang imported header module.

lib/Frontend/Frontend.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,13 @@ bool CompilerInstance::canImportSwiftBacktracing() const {
11751175
return getASTContext().testImportModule(modulePath);
11761176
}
11771177

1178+
bool CompilerInstance::canImportCxx() const {
1179+
ImportPath::Module::Builder builder(
1180+
getASTContext().getIdentifier(CXX_MODULE_NAME));
1181+
auto modulePath = builder.get();
1182+
return getASTContext().testImportModule(modulePath);
1183+
}
1184+
11781185
bool CompilerInstance::canImportCxxShim() const {
11791186
ImportPath::Module::Builder builder(
11801187
getASTContext().getIdentifier(CXX_SHIM_NAME));
@@ -1278,8 +1285,13 @@ ImplicitImportInfo CompilerInstance::getImplicitImportInfo() const {
12781285
}
12791286
}
12801287

1281-
if (Invocation.getLangOptions().EnableCXXInterop && canImportCxxShim()) {
1282-
pushImport(CXX_SHIM_NAME, {ImportFlags::ImplementationOnly});
1288+
if (Invocation.getLangOptions().EnableCXXInterop) {
1289+
if (imports.StdlibKind != ImplicitStdlibKind::Builtin &&
1290+
Invocation.getFrontendOptions().ModuleName != CXX_MODULE_NAME &&
1291+
canImportCxx())
1292+
pushImport(CXX_MODULE_NAME);
1293+
if (canImportCxxShim())
1294+
pushImport(CXX_SHIM_NAME, {ImportFlags::ImplementationOnly});
12831295
}
12841296

12851297
imports.ShouldImportUnderlyingModule = frontendOpts.ImportUnderlyingModule;

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ std::error_code SerializedModuleLoaderBase::openModuleFile(
392392
return std::error_code();
393393
}
394394

395-
SerializedModuleLoaderBase::BinaryModuleImports
395+
llvm::ErrorOr<SerializedModuleLoaderBase::BinaryModuleImports>
396396
SerializedModuleLoaderBase::getImportsOfModule(
397397
const ModuleFileSharedCore &loadedModuleFile,
398398
ModuleLoadingBehavior transitiveBehavior, StringRef packageName,
@@ -484,7 +484,7 @@ SerializedModuleLoaderBase::scanModuleFile(Twine modulePath, bool isFramework,
484484
getImportsOfModule(*loadedModuleFile, ModuleLoadingBehavior::Optional,
485485
Ctx.LangOpts.PackageName, isTestableImport);
486486

487-
auto importedModuleSet = binaryModuleImports.moduleImports;
487+
auto importedModuleSet = binaryModuleImports->moduleImports;
488488
std::vector<std::string> importedModuleNames;
489489
importedModuleNames.reserve(importedModuleSet.size());
490490
llvm::transform(importedModuleSet.keys(),
@@ -493,8 +493,8 @@ SerializedModuleLoaderBase::scanModuleFile(Twine modulePath, bool isFramework,
493493
return N.str();
494494
});
495495

496-
auto importedHeader = binaryModuleImports.headerImport;
497-
auto &importedOptionalModuleSet = binaryModuleOptionalImports.moduleImports;
496+
auto importedHeader = binaryModuleImports->headerImport;
497+
auto &importedOptionalModuleSet = binaryModuleOptionalImports->moduleImports;
498498
std::vector<std::string> importedOptionalModuleNames;
499499
for (const auto optionalImportedModule : importedOptionalModuleSet.keys())
500500
if (!importedModuleSet.contains(optionalImportedModule))

0 commit comments

Comments
 (0)