Skip to content

Commit daf07c0

Browse files
committed
Update getRealModuleName
Assert map is empty in setModuleAliases Update doc comments
1 parent 470fbc7 commit daf07c0

File tree

4 files changed

+37
-36
lines changed

4 files changed

+37
-36
lines changed

include/swift/AST/ASTContext.h

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,13 @@ class ASTContext final {
350350
/// Cache of module names that fail the 'canImport' test in this context.
351351
mutable llvm::SmallPtrSet<Identifier, 8> FailedModuleImportNames;
352352

353-
/// Mapping between aliases and real (physical) names of imported or referenced modules.
353+
/// Set if a `-module-alias` was passed. Used to store mapping between module aliases and
354+
/// their corresponding real names, and vice versa for a reverse lookup, which is needed to check
355+
/// if the module names appearing in source files are aliases or real names.
356+
/// \see ASTContext::getRealModuleName.
357+
///
358+
/// The boolean in the value indicates whether or not the entry is keyed by an alias vs real name,
359+
/// i.e. true if the entry is [key: alias_name, value: (real_name, true)].
354360
mutable llvm::DenseMap<Identifier, std::pair<Identifier, bool>> ModuleAliasMap;
355361

356362
/// Retrieve the allocator for the given arena.
@@ -477,25 +483,21 @@ class ASTContext final {
477483
/// specified string.
478484
Identifier getIdentifier(StringRef Str) const;
479485

480-
/// Convert a given module alias map (with `-module-alias` option) to a map of entries that are
481-
/// keyed both module aliases and real names along with a boolean indicating whether the entry
482-
/// is an alias or a real name.
483-
/// An entry with a module alias as key will have value: (real module name, true), and
484-
/// an entry with a module real name as key will have value: (module alias, false).
486+
/// Convert a given alias map to a map of Identifiers between module aliases and their actual names.
487+
/// For example, if '-module-alias Foo=X -module-alias Bar=Y' input is passed in, the aliases Foo and Bar are
488+
/// the names of the imported or referenced modules in source files in the main module, and X and Y
489+
/// are the real (physical) module names on disk.
485490
void setModuleAliases(const llvm::StringMap<StringRef> &aliasMap);
486491

487-
/// Retrieve the actual module name if a module alias is used via '-module-alias Foo=X', where Foo is
488-
/// a module alias and X is the real (physical) name. Returns \p key if no aliasing is used.
489-
Identifier getRealModuleName(Identifier key) const;
490-
491-
/// Retrieve the value mapped to the given key.
492-
/// \param key A module alias or real name (or non-aliased name) to look up
493-
/// \returns A pair of a module alias or real name given \p key, and a boolean indicating if the
494-
/// \p key is an alias
495-
/// If \p key is a module alias, it returns: (corresponding real name, true)
496-
/// if \p key is a module real name, it returns: (corresponding alias, false), and
497-
/// if \p key is a non-aliased module name, it returns (key, true).
498-
std::pair<Identifier, bool> getRealModuleNameOrAlias(Identifier key) const;
492+
/// Look up the module alias map by the given \p key.
493+
///
494+
/// \param key A module alias or real name to look up the map by.
495+
/// \param reverseLookup Default to false, but if true, it will treat the \p key as a real name and
496+
/// look up the alias, which can be used to guard against real names appearing in source files.
497+
/// \returns The real name or alias mapped to the key.
498+
/// If \p reverseLookup is true but the \p key is an alias, it will return an empty Identifier.
499+
/// If no aliasing is used, \p key will be returned.
500+
Identifier getRealModuleName(Identifier key, bool reverseLookup = false) const;
499501

500502
/// Decide how to interpret two precedence groups.
501503
Associativity associateInfixOperators(PrecedenceGroupDecl *left,

lib/AST/ASTContext.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,6 +1637,9 @@ void ASTContext::addModuleInterfaceChecker(
16371637
}
16381638

16391639
void ASTContext::setModuleAliases(const llvm::StringMap<StringRef> &aliasMap) {
1640+
// This setter should be called only once after ASTContext has been initialized
1641+
assert(ModuleAliasMap.empty());
1642+
16401643
for (auto k: aliasMap.keys()) {
16411644
auto v = aliasMap.lookup(k);
16421645
if (!v.empty()) {
@@ -1650,23 +1653,20 @@ void ASTContext::setModuleAliases(const llvm::StringMap<StringRef> &aliasMap) {
16501653
}
16511654
}
16521655

1653-
Identifier ASTContext::getRealModuleName(Identifier key) const {
1656+
Identifier ASTContext::getRealModuleName(Identifier key, bool reverseLookup) const {
16541657
auto found = ModuleAliasMap.find(key);
1655-
if (found != ModuleAliasMap.end()) {
1656-
auto realOrAlias = found->second;
1657-
if (realOrAlias.second) // check if it's a real name given the key (alias)
1658-
return realOrAlias.first;
1659-
}
1660-
return key;
1661-
}
1662-
1663-
std::pair<Identifier, bool> ASTContext::getRealModuleNameOrAlias(Identifier key) const {
1664-
auto found = ModuleAliasMap.find(key);
1665-
if (found != ModuleAliasMap.end()) {
1666-
return found->second;
1667-
}
1668-
// No module aliasing is used for the given key
1669-
return std::make_pair(key, true);
1658+
if (found == ModuleAliasMap.end())
1659+
return key;
1660+
1661+
// Found an entry
1662+
auto realOrAlias = found->second;
1663+
// If reverseLookup, i.e. look up an alias by real name, but the found
1664+
// entry is keyed by an alias, return an empty Identifier
1665+
if (reverseLookup && realOrAlias.second)
1666+
return Identifier();
1667+
1668+
// Return a real name or an alias (if reverseLookup) mapped to the given key
1669+
return realOrAlias.first;
16701670
}
16711671

16721672
Optional<ModuleDependencies> ASTContext::getModuleDependencies(

lib/AST/UnqualifiedLookup.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,6 @@ void UnqualifiedLookupFactory::lookForAModuleWithTheGivenName(
439439
// passed, the alias 'Foo' should appear in source files, not 'Bar'.
440440
// If no module aliasing is used, this will simply return the given
441441
// name and 'true' indicating the check passed.
442-
// Is this *not* the real name of an aliased module?
443442
if (!Ctx.getRealModuleName(givenName).empty()) {
444443
desiredModule = Ctx.getLoadedModule(givenName);
445444
}

lib/Parse/ParseDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4826,7 +4826,7 @@ ParserResult<ImportDecl> Parser::parseDeclImport(ParseDeclOptions Flags,
48264826
if (Context.getRealModuleName(parsedModuleID).empty()) {
48274827
// If reached here, it means the parsed module name is a real module name
48284828
// which appeared in the source file; only a module alias should be allowed
4829-
auto aliasName = Context.getRealModuleName(parsedModuleID, /*lookupAliasFromReal=*/true);
4829+
auto aliasName = Context.getRealModuleName(parsedModuleID, /*reverseLookup=*/true);
48304830
diagnose(importPath.front().Loc, diag::expected_module_alias,
48314831
parsedModuleID, aliasName)
48324832
.fixItReplace(importPath.front().Loc, aliasName.str());

0 commit comments

Comments
 (0)