Skip to content

Commit 214da85

Browse files
committed
Sema: Keep public module name in ModuleDecl
1 parent 37521ad commit 214da85

File tree

5 files changed

+43
-0
lines changed

5 files changed

+43
-0
lines changed

include/swift/AST/Module.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ class ModuleDecl
243243
/// Module name to use when referenced in clients module interfaces.
244244
mutable Identifier ExportAsName;
245245

246+
mutable Identifier PublicModuleName;
247+
246248
public:
247249
/// Produces the components of a given module's full name in reverse order.
248250
///
@@ -504,6 +506,21 @@ class ModuleDecl
504506
ExportAsName = name;
505507
}
506508

509+
/// Public facing name for this module in diagnostics and documentation.
510+
///
511+
/// This always returns a valid name as it defaults to the module name if
512+
/// no public module name is set.
513+
///
514+
/// If `mustBeVisible`, return the normal module name when the module
515+
/// corresponding to the public module name isn't imported. Users working
516+
/// in between both modules will then see the normal module name,
517+
/// this may be more useful for diagnostics at that level.
518+
Identifier getPublicModuleName(bool mustBeVisible) const;
519+
520+
void setPublicModuleName(Identifier name) {
521+
PublicModuleName = name;
522+
}
523+
507524
/// Retrieve the actual module name of an alias used for this module (if any).
508525
///
509526
/// For example, if '-module-alias Foo=Bar' is passed in when building the main module,

lib/AST/Module.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,6 +1922,19 @@ ImportedModule::removeDuplicates(SmallVectorImpl<ImportedModule> &imports) {
19221922
imports.erase(last, imports.end());
19231923
}
19241924

1925+
Identifier ModuleDecl::getPublicModuleName(bool mustBeVisible) const {
1926+
if (!PublicModuleName.empty()) {
1927+
if (!mustBeVisible)
1928+
return PublicModuleName;
1929+
1930+
bool publicModuleIsImported =
1931+
getASTContext().getModuleByIdentifier(PublicModuleName);
1932+
if (publicModuleIsImported)
1933+
return PublicModuleName;
1934+
}
1935+
return getName();
1936+
}
1937+
19251938
Identifier ModuleDecl::getRealName() const {
19261939
// This will return the real name for an alias (if used) or getName()
19271940
return getASTContext().getRealModuleName(getName());

lib/Frontend/Frontend.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,6 +1463,10 @@ ModuleDecl *CompilerInstance::getMainModule() const {
14631463
MainModule->setExportAsName(getASTContext().getIdentifier(
14641464
Invocation.getFrontendOptions().ExportAsName));
14651465
}
1466+
if (!Invocation.getFrontendOptions().PublicModuleName.empty()) {
1467+
MainModule->setPublicModuleName(getASTContext().getIdentifier(
1468+
Invocation.getFrontendOptions().PublicModuleName));
1469+
}
14661470
if (Invocation.getFrontendOptions().EnableLibraryEvolution)
14671471
MainModule->setResilienceStrategy(ResilienceStrategy::Resilient);
14681472
if (Invocation.getLangOptions().isSwiftVersionAtLeast(6))

lib/Serialization/Serialization.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,13 @@ void Serializer::writeHeader() {
11261126
ExportAs.emit(ScratchRecord, M->getExportAsName().str());
11271127
}
11281128

1129+
Identifier publicModuleName =
1130+
M->getPublicModuleName(/*mustBeVisible=*/false);
1131+
if (publicModuleName != M->getName()) {
1132+
options_block::PublicModuleNameLayout PublicModuleName(Out);
1133+
PublicModuleName.emit(ScratchRecord, publicModuleName.str());
1134+
}
1135+
11291136
if (M->isConcurrencyChecked()) {
11301137
options_block::IsConcurrencyCheckedLayout IsConcurrencyChecked(Out);
11311138
IsConcurrencyChecked.emit(ScratchRecord);

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,8 @@ LoadedFile *SerializedModuleLoaderBase::loadAST(
953953
M.setSerializePackageEnabled();
954954
if (!loadedModuleFile->getModuleABIName().empty())
955955
M.setABIName(Ctx.getIdentifier(loadedModuleFile->getModuleABIName()));
956+
if (!loadedModuleFile->getPublicModuleName().empty())
957+
M.setPublicModuleName(Ctx.getIdentifier(loadedModuleFile->getPublicModuleName()));
956958
if (loadedModuleFile->isConcurrencyChecked())
957959
M.setIsConcurrencyChecked();
958960
if (loadedModuleFile->hasCxxInteroperability()) {

0 commit comments

Comments
 (0)