Skip to content

Commit a24d74b

Browse files
authored
Merge pull request #40102 from apple/es-param
[NFC] Add module alias lookup option enum / use the option to look up in ASTContext::getRealModuleName
2 parents 9ca40f1 + 89fb5ff commit a24d74b

File tree

4 files changed

+40
-30
lines changed

4 files changed

+40
-30
lines changed

include/swift/AST/ASTContext.h

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -489,23 +489,27 @@ class ASTContext final {
489489
/// are the real (physical) module names on disk.
490490
void setModuleAliases(const llvm::StringMap<StringRef> &aliasMap);
491491

492-
/// Look up the module alias map by the given \p key.
492+
/// Look up option used in \c getRealModuleName when module aliasing is applied.
493+
enum class ModuleAliasLookupOption {
494+
alwaysRealName,
495+
realNameFromAlias,
496+
aliasFromRealName
497+
};
498+
499+
/// Look up the module alias map by the given \p key and a lookup \p option.
500+
///
501+
/// \param key A module alias or real name to look up the map by.
502+
/// \param option A look up option \c ModuleAliasLookupOption. Defaults to alwaysRealName.
493503
///
494-
/// \param key A module alias or real name to look up the map by
495-
/// \param alwaysReturnRealName Indicates whether it should always retrieve the real module name
496-
/// given \p key. Defaults to true. This takes a higher precedence than
497-
/// \p lookupAliasFromReal.
498-
/// \param lookupAliasFromReal Indicates whether to look up an alias by treating \p key
499-
/// as a real name. Defaults to false.
500504
/// \return The real name or alias mapped to the key.
501-
/// If \p alwaysReturnRealName is true, return the real module name if \p key is an alias
502-
/// or the key itself since that's the real name.
503-
/// If \p lookupAliasFromReal is true, and \p alwaysReturnRealName is false, return
504-
/// only if \p key is a real name, else an empty Identifier.
505-
/// If no aliasing is used, return \p key.
505+
/// If no aliasing is used, return \p key regardless of \p option.
506+
/// If \p option is alwaysRealName, return the real module name whether the \p key is an alias
507+
/// or a real name.
508+
/// If \p option is realNameFromAlias, only return a real name if \p key is an alias.
509+
/// If \p option is aliasFromRealName, only return an alias if \p key is a real name.
510+
/// Else return a real name or an alias mapped to the \p key.
506511
Identifier getRealModuleName(Identifier key,
507-
bool alwaysReturnRealName = true,
508-
bool lookupAliasFromReal = false) const;
512+
ModuleAliasLookupOption option = ModuleAliasLookupOption::alwaysRealName) const;
509513

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

lib/AST/ASTContext.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,26 +1653,31 @@ void ASTContext::setModuleAliases(const llvm::StringMap<StringRef> &aliasMap) {
16531653
}
16541654
}
16551655

1656-
Identifier ASTContext::getRealModuleName(Identifier key, bool alwaysReturnRealName, bool lookupAliasFromReal) const {
1656+
Identifier ASTContext::getRealModuleName(Identifier key, ModuleAliasLookupOption option) const {
16571657
auto found = ModuleAliasMap.find(key);
16581658
if (found == ModuleAliasMap.end())
1659-
return key;
1659+
return key; // No module aliasing was used, so just return the given key
16601660

16611661
// Found an entry
1662-
auto realOrAlias = found->second;
1662+
auto value = found->second;
16631663

1664-
// If alwaysReturnRealName, return the real name if the key is an
1665-
// alias or the key itself since that's the real name
1666-
if (alwaysReturnRealName) {
1667-
return realOrAlias.second ? realOrAlias.first : key;
1664+
// With the alwaysRealName option, look up the real name by treating
1665+
// the given key as an alias; if the key's not an alias, return the key
1666+
// itself since that's the real name.
1667+
if (option == ModuleAliasLookupOption::alwaysRealName) {
1668+
return value.second ? value.first : key;
16681669
}
16691670

1670-
// If lookupAliasFromReal, and the found entry should be keyed by a real
1671-
// name, return the entry value, otherwise, return an empty Identifier.
1672-
if (lookupAliasFromReal == realOrAlias.second)
1671+
// With realNameFromAlias or aliasFromRealName option, only return the value
1672+
// if the given key matches the description (whether it's an alias or real name)
1673+
// by looking up the value.second (true if keyed by an alias). If not matched,
1674+
// return an empty Identifier.
1675+
if ((option == ModuleAliasLookupOption::realNameFromAlias && !value.second) ||
1676+
(option == ModuleAliasLookupOption::aliasFromRealName && value.second))
16731677
return Identifier();
16741678

1675-
return realOrAlias.first;
1679+
// Otherwise return the value found (whether the key is an alias or real name)
1680+
return value.first;
16761681
}
16771682

16781683
Optional<ModuleDependencies> ASTContext::getModuleDependencies(

lib/AST/UnqualifiedLookup.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,9 +441,10 @@ void UnqualifiedLookupFactory::lookForAModuleWithTheGivenName(
441441
// Check if the given name appearing in the source file is a module
442442
// real name or alias; for example, if `-module-alias Foo=Bar` was
443443
// passed, the alias 'Foo' should appear in source files, not 'Bar'.
444-
// If the real name 'Bar' was used, looking up getRealModuleName will
445-
// return an empty Identifier.
446-
if (!Ctx.getRealModuleName(givenName, /*alwaysReturnRealName=*/false).empty()) {
444+
// If the real name 'Bar' was used, looking up getRealModuleName with
445+
// the real name 'Bar' and realNameFromAlias option should return
446+
// an empty Identifier.
447+
if (!Ctx.getRealModuleName(givenName, ASTContext::ModuleAliasLookupOption::realNameFromAlias).empty()) {
447448
// Only load the module if the lookup value is not empty, i.e. given
448449
// name is a module alias, not a real module name.
449450
desiredModule = Ctx.getLoadedModule(givenName);

lib/Parse/ParseDecl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4831,10 +4831,10 @@ ParserResult<ImportDecl> Parser::parseDeclImport(ParseDeclOptions Flags,
48314831
// and check that the module alias appeared in source files instead of
48324832
// its corresponding real name
48334833
auto parsedModuleID = importPath.get().front().Item;
4834-
if (Context.getRealModuleName(parsedModuleID, /*alwaysReturnRealName=*/false).empty()) {
4834+
if (Context.getRealModuleName(parsedModuleID, ASTContext::ModuleAliasLookupOption::realNameFromAlias).empty()) {
48354835
// If reached here, it means the parsed module name is a real module name
48364836
// which appeared in the source file; only a module alias should be allowed
4837-
auto aliasName = Context.getRealModuleName(parsedModuleID, /*alwaysReturnRealName=*/false, /*lookupAliasFromReal=*/true);
4837+
auto aliasName = Context.getRealModuleName(parsedModuleID, ASTContext::ModuleAliasLookupOption::aliasFromRealName);
48384838
diagnose(importPath.front().Loc, diag::expected_module_alias,
48394839
parsedModuleID, aliasName)
48404840
.fixItReplace(importPath.front().Loc, aliasName.str());

0 commit comments

Comments
 (0)