Skip to content

Commit 2d86330

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 7470b77 commit 2d86330

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
@@ -1165,6 +1165,13 @@ bool CompilerInstance::canImportSwiftBacktracing() const {
11651165
return getASTContext().testImportModule(modulePath);
11661166
}
11671167

1168+
bool CompilerInstance::canImportCxx() const {
1169+
ImportPath::Module::Builder builder(
1170+
getASTContext().getIdentifier(CXX_MODULE_NAME));
1171+
auto modulePath = builder.get();
1172+
return getASTContext().testImportModule(modulePath);
1173+
}
1174+
11681175
bool CompilerInstance::canImportCxxShim() const {
11691176
ImportPath::Module::Builder builder(
11701177
getASTContext().getIdentifier(CXX_SHIM_NAME));
@@ -1268,8 +1275,13 @@ ImplicitImportInfo CompilerInstance::getImplicitImportInfo() const {
12681275
}
12691276
}
12701277

1271-
if (Invocation.getLangOptions().EnableCXXInterop && canImportCxxShim()) {
1272-
pushImport(CXX_SHIM_NAME, {ImportFlags::ImplementationOnly});
1278+
if (Invocation.getLangOptions().EnableCXXInterop) {
1279+
if (imports.StdlibKind != ImplicitStdlibKind::Builtin &&
1280+
Invocation.getFrontendOptions().ModuleName != CXX_MODULE_NAME &&
1281+
canImportCxx())
1282+
pushImport(CXX_MODULE_NAME);
1283+
if (canImportCxxShim())
1284+
pushImport(CXX_SHIM_NAME, {ImportFlags::ImplementationOnly});
12731285
}
12741286

12751287
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,
@@ -478,7 +478,7 @@ SerializedModuleLoaderBase::scanModuleFile(Twine modulePath, bool isFramework,
478478
getImportsOfModule(*loadedModuleFile, ModuleLoadingBehavior::Optional,
479479
Ctx.LangOpts.PackageName, isTestableImport);
480480

481-
auto importedModuleSet = binaryModuleImports.moduleImports;
481+
auto importedModuleSet = binaryModuleImports->moduleImports;
482482
std::vector<std::string> importedModuleNames;
483483
importedModuleNames.reserve(importedModuleSet.size());
484484
llvm::transform(importedModuleSet.keys(),
@@ -487,8 +487,8 @@ SerializedModuleLoaderBase::scanModuleFile(Twine modulePath, bool isFramework,
487487
return N.str();
488488
});
489489

490-
auto importedHeader = binaryModuleImports.headerImport;
491-
auto &importedOptionalModuleSet = binaryModuleOptionalImports.moduleImports;
490+
auto importedHeader = binaryModuleImports->headerImport;
491+
auto &importedOptionalModuleSet = binaryModuleOptionalImports->moduleImports;
492492
std::vector<std::string> importedOptionalModuleNames;
493493
for (const auto optionalImportedModule : importedOptionalModuleSet.keys())
494494
if (!importedModuleSet.contains(optionalImportedModule))

0 commit comments

Comments
 (0)