Skip to content

Commit 028b964

Browse files
authored
Merge pull request #39496 from apple/es-malias2
[NFC] Add a module alias map and a lookup func to ASTContext. Add a getter for the actual module name in ModuleDecl if a module alias is used. rdar://83682112
2 parents 52a9713 + 6b4951e commit 028b964

File tree

5 files changed

+48
-1
lines changed

5 files changed

+48
-1
lines changed

include/swift/AST/ASTContext.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,9 @@ class ASTContext final {
347347
/// Cache of module names that fail the 'canImport' test in this context.
348348
mutable llvm::SmallPtrSet<Identifier, 8> FailedModuleImportNames;
349349

350+
/// Mapping between aliases and real (physical) names of imported or referenced modules.
351+
mutable llvm::DenseMap<Identifier, Identifier> ModuleAliasMap;
352+
350353
/// Retrieve the allocator for the given arena.
351354
llvm::BumpPtrAllocator &
352355
getAllocator(AllocationArena arena = AllocationArena::Permanent) const;
@@ -471,6 +474,16 @@ class ASTContext final {
471474
/// specified string.
472475
Identifier getIdentifier(StringRef Str) const;
473476

477+
/// Convert a given alias map to a map of Identifiers between module aliases and their actual names.
478+
/// For example, if '-module-alias Foo=X -module-alias Bar=Y' input is passed in, the aliases Foo and Bar are
479+
/// the names of the imported or referenced modules in source files in the main module, and X and Y
480+
/// are the real (physical) module names on disk.
481+
void setModuleAliases(const llvm::StringMap<StringRef> &aliasMap);
482+
483+
/// Retrieve the actual module name if a module alias is used via '-module-alias Foo=X', where Foo is
484+
/// a module alias and X is the real (physical) name. Returns \p key if no aliasing is used.
485+
Identifier getRealModuleName(Identifier key) const;
486+
474487
/// Decide how to interpret two precedence groups.
475488
Associativity associateInfixOperators(PrecedenceGroupDecl *left,
476489
PrecedenceGroupDecl *right) const;

include/swift/AST/Module.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,15 @@ class ModuleDecl
357357
ModuleABIName = name;
358358
}
359359

360+
/// Retrieve the actual module name of an alias used for this module (if any).
361+
///
362+
/// For example, if '-module-alias Foo=Bar' is passed in when building the main module,
363+
/// and this module is (a) not the main module and (b) is named Foo, then it returns
364+
/// the real (physically on-disk) module name Bar.
365+
///
366+
/// If no module aliasing is set, it will return getName(), i.e. Foo.
367+
Identifier getRealName() const;
368+
360369
/// User-defined module version number.
361370
llvm::VersionTuple UserModuleVersion;
362371
void setUserModuleVersion(llvm::VersionTuple UserVer) {
@@ -365,6 +374,7 @@ class ModuleDecl
365374
llvm::VersionTuple getUserModuleVersion() const {
366375
return UserModuleVersion;
367376
}
377+
368378
private:
369379
/// A cache of this module's underlying module and required bystander if it's
370380
/// an underscored cross-import overlay.

lib/AST/ASTContext.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,6 +1642,23 @@ void ASTContext::addModuleInterfaceChecker(
16421642
getImpl().InterfaceChecker = std::move(checker);
16431643
}
16441644

1645+
void ASTContext::setModuleAliases(const llvm::StringMap<StringRef> &aliasMap) {
1646+
for (auto k: aliasMap.keys()) {
1647+
auto val = aliasMap.lookup(k);
1648+
if (!val.empty()) {
1649+
ModuleAliasMap[getIdentifier(k)] = getIdentifier(val);
1650+
}
1651+
}
1652+
}
1653+
1654+
Identifier ASTContext::getRealModuleName(Identifier key) const {
1655+
auto found = ModuleAliasMap.find(key);
1656+
if (found != ModuleAliasMap.end()) {
1657+
return found->second;
1658+
}
1659+
return key;
1660+
}
1661+
16451662
Optional<ModuleDependencies> ASTContext::getModuleDependencies(
16461663
StringRef moduleName, bool isUnderlyingClangModule,
16471664
ModuleDependenciesCache &cache, InterfaceSubContextDelegate &delegate,

lib/AST/Module.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,6 @@ ModuleDecl::ModuleDecl(Identifier name, ASTContext &ctx,
475475
ctx.addDestructorCleanup(*this);
476476
setImplicit();
477477
setInterfaceType(ModuleType::get(this));
478-
479478
setAccess(AccessLevel::Public);
480479

481480
Bits.ModuleDecl.StaticLibrary = 0;
@@ -1564,6 +1563,11 @@ ImportedModule::removeDuplicates(SmallVectorImpl<ImportedModule> &imports) {
15641563
imports.erase(last, imports.end());
15651564
}
15661565

1566+
Identifier ModuleDecl::getRealName() const {
1567+
// This will return the real name for an alias (if used) or getName()
1568+
return getASTContext().getRealModuleName(getName());
1569+
}
1570+
15671571
Identifier ModuleDecl::getABIName() const {
15681572
if (!ModuleABIName.empty())
15691573
return ModuleABIName;

lib/Frontend/Frontend.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ bool CompilerInstance::setUpASTContextIfNeeded() {
233233
Invocation.getClangImporterOptions(),
234234
Invocation.getSymbolGraphOptions(),
235235
SourceMgr, Diagnostics));
236+
if (!Invocation.getFrontendOptions().ModuleAliasMap.empty())
237+
Context->setModuleAliases(Invocation.getFrontendOptions().ModuleAliasMap);
238+
236239
registerParseRequestFunctions(Context->evaluator);
237240
registerTypeCheckerRequestFunctions(Context->evaluator);
238241
registerClangImporterRequestFunctions(Context->evaluator);

0 commit comments

Comments
 (0)