Skip to content

Commit 0e05d10

Browse files
committed
Add module alias map and lookup func to ASTContext
Set module underlying name in ModuleDecl if a module alias exists rdar://83682112
1 parent 9b0f61b commit 0e05d10

File tree

5 files changed

+48
-1
lines changed

5 files changed

+48
-1
lines changed

include/swift/AST/ASTContext.h

Lines changed: 9 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 underlying names of modules 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,12 @@ 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 underlying names.
478+
void setModuleAliases(const llvm::StringMap<StringRef> &aliasMap);
479+
480+
/// Retrieve the underlying name given an alias name key.
481+
Identifier lookupModuleAlias(Identifier key) const;
482+
474483
/// Decide how to interpret two precedence groups.
475484
Associativity associateInfixOperators(PrecedenceGroupDecl *left,
476485
PrecedenceGroupDecl *right) const;

include/swift/AST/Module.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ class ModuleDecl
171171
/// The ABI name of the module, if it differs from the module name.
172172
mutable Identifier ModuleABIName;
173173

174+
/// The underlying name for an alias used for this module (if any).
175+
mutable Identifier ModuleUnderlyingName;
176+
174177
public:
175178
/// Produces the components of a given module's full name in reverse order.
176179
///
@@ -357,6 +360,10 @@ class ModuleDecl
357360
ModuleABIName = name;
358361
}
359362

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;
366+
360367
/// User-defined module version number.
361368
llvm::VersionTuple UserModuleVersion;
362369
void setUserModuleVersion(llvm::VersionTuple UserVer) {
@@ -365,6 +372,7 @@ class ModuleDecl
365372
llvm::VersionTuple getUserModuleVersion() const {
366373
return UserModuleVersion;
367374
}
375+
368376
private:
369377
/// A cache of this module's underlying module and required bystander if it's
370378
/// an underscored cross-import overlay.
@@ -380,6 +388,12 @@ class ModuleDecl
380388
/// module if one exists.
381389
ModuleDecl *getUnderlyingModuleIfOverlay() const;
382390

391+
/// If a module alias is used, set the corresponding underlying name,
392+
/// which will be used for contents including metadata and mangling.
393+
void setUnderlyingName(Identifier name) {
394+
ModuleUnderlyingName = name;
395+
}
396+
383397
public:
384398

385399
/// Returns true if this module is an underscored cross import overlay

lib/AST/ASTContext.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,6 +1642,21 @@ 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+
ModuleAliasMap[getIdentifier(k)] = getIdentifier(val);
1649+
}
1650+
}
1651+
1652+
Identifier ASTContext::lookupModuleAlias(Identifier key) const {
1653+
auto found = ModuleAliasMap.find(key);
1654+
if (found != ModuleAliasMap.end()) {
1655+
return found->second;
1656+
}
1657+
return Identifier();
1658+
}
1659+
16451660
Optional<ModuleDependencies> ASTContext::getModuleDependencies(
16461661
StringRef moduleName, bool isUnderlyingClangModule,
16471662
ModuleDependenciesCache &cache, InterfaceSubContextDelegate &delegate,

lib/AST/Module.cpp

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

481481
Bits.ModuleDecl.StaticLibrary = 0;
482482
Bits.ModuleDecl.TestingEnabled = 0;
@@ -1564,6 +1564,12 @@ 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;
1570+
return getName();
1571+
}
1572+
15671573
Identifier ModuleDecl::getABIName() const {
15681574
if (!ModuleABIName.empty())
15691575
return ModuleABIName;

lib/Frontend/Frontend.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ bool CompilerInstance::setUpASTContextIfNeeded() {
219219
Invocation.getClangImporterOptions(),
220220
Invocation.getSymbolGraphOptions(),
221221
SourceMgr, Diagnostics));
222+
if (!Invocation.getFrontendOptions().ModuleAliasMap.empty())
223+
Context->setModuleAliases(Invocation.getFrontendOptions().ModuleAliasMap);
224+
222225
registerParseRequestFunctions(Context->evaluator);
223226
registerTypeCheckerRequestFunctions(Context->evaluator);
224227
registerSILGenRequestFunctions(Context->evaluator);

0 commit comments

Comments
 (0)