Skip to content

Commit fc420d0

Browse files
committed
Modify setModuleAliases and lookup
Add examples to documentation comment
1 parent 0e05d10 commit fc420d0

File tree

4 files changed

+23
-13
lines changed

4 files changed

+23
-13
lines changed

Diff for: include/swift/AST/ASTContext.h

+3
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,9 @@ class ASTContext final {
475475
Identifier getIdentifier(StringRef Str) const;
476476

477477
/// Convert a given alias map to a map of Identifiers between module aliases and underlying names.
478+
/// For example, if '-module-alias A=X -module-alias B=Y' input is passed in, the aliases A and B are
479+
/// the names of the imported or referenced modules in source files in the main module, and X and Y
480+
/// are the underlying (physical) module names on disk.
478481
void setModuleAliases(const llvm::StringMap<StringRef> &aliasMap);
479482

480483
/// Retrieve the underlying name given an alias name key.

Diff for: include/swift/AST/Module.h

+12-7
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class ModuleDecl
172172
mutable Identifier ModuleABIName;
173173

174174
/// The underlying name for an alias used for this module (if any).
175-
mutable Identifier ModuleUnderlyingName;
175+
mutable Identifier ModuleRealName;
176176

177177
public:
178178
/// Produces the components of a given module's full name in reverse order.
@@ -360,9 +360,14 @@ class ModuleDecl
360360
ModuleABIName = name;
361361
}
362362

363-
/// Retrieve the underlying name of the alias (if any) used for this module.
364-
/// If no module alias is set, it returns getName().
365-
Identifier getUnderlyingName() const;
363+
/// Retrieve the actual module name of an alias used for this module (if any).
364+
///
365+
/// For example, if '-module-alias Foo=Bar' is passed in when building the main module,
366+
/// and this module is (a) not the main module and (b) is named Foo, then it returns
367+
/// the real (physically on-disk) module name Bar.
368+
///
369+
/// If no module aliasing is set, it will return getName(), i.e. Foo.
370+
Identifier getRealName() const;
366371

367372
/// User-defined module version number.
368373
llvm::VersionTuple UserModuleVersion;
@@ -388,10 +393,10 @@ class ModuleDecl
388393
/// module if one exists.
389394
ModuleDecl *getUnderlyingModuleIfOverlay() const;
390395

391-
/// If a module alias is used, set the corresponding underlying name,
396+
/// If a module alias is used, set the corresponding real name on disk,
392397
/// which will be used for contents including metadata and mangling.
393-
void setUnderlyingName(Identifier name) {
394-
ModuleUnderlyingName = name;
398+
void setRealName(Identifier name) {
399+
ModuleRealName = name;
395400
}
396401

397402
public:

Diff for: lib/AST/ASTContext.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -1645,7 +1645,9 @@ void ASTContext::addModuleInterfaceChecker(
16451645
void ASTContext::setModuleAliases(const llvm::StringMap<StringRef> &aliasMap) {
16461646
for (auto k: aliasMap.keys()) {
16471647
auto val = aliasMap.lookup(k);
1648-
ModuleAliasMap[getIdentifier(k)] = getIdentifier(val);
1648+
if (!val.empty()) {
1649+
ModuleAliasMap[getIdentifier(k)] = getIdentifier(val);
1650+
}
16491651
}
16501652
}
16511653

@@ -1654,7 +1656,7 @@ Identifier ASTContext::lookupModuleAlias(Identifier key) const {
16541656
if (found != ModuleAliasMap.end()) {
16551657
return found->second;
16561658
}
1657-
return Identifier();
1659+
return key;
16581660
}
16591661

16601662
Optional<ModuleDependencies> ASTContext::getModuleDependencies(

Diff for: lib/AST/Module.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ ModuleDecl::ModuleDecl(Identifier name, ASTContext &ctx,
476476
setImplicit();
477477
setInterfaceType(ModuleType::get(this));
478478
setAccess(AccessLevel::Public);
479-
setUnderlyingName(ctx.lookupModuleAlias(name));
479+
setRealName(ctx.lookupModuleAlias(name));
480480

481481
Bits.ModuleDecl.StaticLibrary = 0;
482482
Bits.ModuleDecl.TestingEnabled = 0;
@@ -1564,9 +1564,9 @@ ImportedModule::removeDuplicates(SmallVectorImpl<ImportedModule> &imports) {
15641564
imports.erase(last, imports.end());
15651565
}
15661566

1567-
Identifier ModuleDecl::getUnderlyingName() const {
1568-
if (!ModuleUnderlyingName.empty())
1569-
return ModuleUnderlyingName;
1567+
Identifier ModuleDecl::getRealName() const {
1568+
if (!ModuleRealName.empty())
1569+
return ModuleRealName;
15701570
return getName();
15711571
}
15721572

0 commit comments

Comments
 (0)