Skip to content

Commit 64c7418

Browse files
committed
Add module alias map and underlying name to ModuleDecl
1 parent 4871212 commit 64c7418

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

include/swift/AST/Module.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "llvm/ADT/STLExtras.h"
3434
#include "llvm/ADT/SmallVector.h"
3535
#include "llvm/ADT/StringMap.h"
36+
#include "llvm/ADT/DenseMap.h"
3637
#include "llvm/Support/ErrorHandling.h"
3738
#include "llvm/Support/MD5.h"
3839
#include <set>
@@ -171,6 +172,12 @@ class ModuleDecl
171172
/// The ABI name of the module, if it differs from the module name.
172173
mutable Identifier ModuleABIName;
173174

175+
/// The underlying name for an alias used for this module (if any).
176+
mutable Identifier ModuleUnderlyingName;
177+
178+
/// Mapping between aliases and underlying names of imported or referenced modules
179+
mutable llvm::DenseMap<Identifier, Identifier> ModuleAliasMap;
180+
174181
public:
175182
/// Produces the components of a given module's full name in reverse order.
176183
///
@@ -279,6 +286,9 @@ class ModuleDecl
279286

280287
using Decl::getASTContext;
281288

289+
/// Retrieves the underlying module name given a module alias
290+
Identifier lookupModuleAlias(Identifier key);
291+
282292
/// Retrieves information about which modules are implicitly imported by
283293
/// each file of this module.
284294
const ImplicitImportInfo &getImplicitImportInfo() const { return ImportInfo; }
@@ -357,6 +367,20 @@ class ModuleDecl
357367
ModuleABIName = name;
358368
}
359369

370+
/// Set the mapping between aliases and underlying names of imported or referenced modules
371+
/// within this module.
372+
void setModuleAliases(const llvm::StringMap<StringRef> &aliasMap);
373+
374+
/// If a module alias is used, set the corresponding underlying name.
375+
/// It will be used for metadata and mangling.
376+
void setUnderlyingName(Identifier name) {
377+
ModuleUnderlyingName = name;
378+
}
379+
380+
/// Retrieve the underlying name of the alias (if any) used for this module.
381+
/// If no module alias is set, returns getName().
382+
Identifier getUnderlyingName() const;
383+
360384
/// User-defined module version number.
361385
llvm::VersionTuple UserModuleVersion;
362386
void setUserModuleVersion(llvm::VersionTuple UserVer) {
@@ -365,6 +389,7 @@ class ModuleDecl
365389
llvm::VersionTuple getUserModuleVersion() const {
366390
return UserModuleVersion;
367391
}
392+
368393
private:
369394
/// A cache of this module's underlying module and required bystander if it's
370395
/// an underscored cross-import overlay.

lib/AST/Module.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,6 +1564,28 @@ ImportedModule::removeDuplicates(SmallVectorImpl<ImportedModule> &imports) {
15641564
imports.erase(last, imports.end());
15651565
}
15661566

1567+
void ModuleDecl::setModuleAliases(const llvm::StringMap<StringRef> &aliasMap) {
1568+
auto &ctx = getASTContext();
1569+
for (auto k: aliasMap.keys()) {
1570+
auto val = aliasMap.lookup(k);
1571+
ModuleAliasMap[ctx.getIdentifier(k)] = ctx.getIdentifier(val);
1572+
}
1573+
}
1574+
1575+
Identifier ModuleDecl::lookupModuleAlias(Identifier key) {
1576+
auto found = ModuleAliasMap.find(key);
1577+
if (found != ModuleAliasMap.end()) {
1578+
return found->second;
1579+
}
1580+
return Identifier();
1581+
}
1582+
1583+
Identifier ModuleDecl::getUnderlyingName() const {
1584+
if (!ModuleUnderlyingName.empty())
1585+
return ModuleUnderlyingName;
1586+
return getName();
1587+
}
1588+
15671589
Identifier ModuleDecl::getABIName() const {
15681590
if (!ModuleABIName.empty())
15691591
return ModuleABIName;

lib/Frontend/Frontend.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,8 @@ ModuleDecl *CompilerInstance::getMainModule() const {
969969
MainModule->setABIName(getASTContext().getIdentifier(
970970
Invocation.getFrontendOptions().ModuleABIName));
971971
}
972+
if (!Invocation.getFrontendOptions().ModuleAliasMap.empty())
973+
MainModule->setModuleAliases(Invocation.getFrontendOptions().ModuleAliasMap);
972974
if (Invocation.getFrontendOptions().EnableLibraryEvolution)
973975
MainModule->setResilienceStrategy(ResilienceStrategy::Resilient);
974976
if (Invocation.getLangOptions().WarnConcurrency ||

0 commit comments

Comments
 (0)