Skip to content

Commit 2a28fed

Browse files
committed
Allow one to separately specify the ABI name of a module.
Introduce a new compiler flag `-module-abi-name <name>` that uses the given name as the ABI name for the module (rather than the module's name in source code). The ABI name impacts name mangling and metadata.
1 parent 6f4f9f8 commit 2a28fed

22 files changed

+111
-7
lines changed

include/swift/AST/Module.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ class ModuleDecl : public DeclContext, public TypeDecl {
166166
friend class DirectOperatorLookupRequest;
167167
friend class DirectPrecedenceGroupLookupRequest;
168168

169+
/// The ABI name of the module, if it differs from the module name.
170+
Identifier ModuleABIName;
171+
169172
public:
170173
/// Produces the components of a given module's full name in reverse order.
171174
///
@@ -343,6 +346,17 @@ class ModuleDecl : public DeclContext, public TypeDecl {
343346
void getDeclaredCrossImportBystanders(
344347
SmallVectorImpl<Identifier> &bystanderNames);
345348

349+
/// Retrieve the ABI name of the module, which is used for metadata and
350+
/// mangling.
351+
Identifier getABIName() const {
352+
return ModuleABIName.empty() ? getName() : ModuleABIName;
353+
}
354+
355+
/// Set the ABI name of the module;
356+
void setABIName(Identifier name) {
357+
ModuleABIName = name;
358+
}
359+
346360
private:
347361
/// A cache of this module's underlying module and required bystander if it's
348362
/// an underscored cross-import overlay.

include/swift/Frontend/FrontendOptions.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,13 @@ class FrontendOptions {
4848
/// An Objective-C header to import and make implicitly visible.
4949
std::string ImplicitObjCHeaderPath;
5050

51-
/// The name of the module which the frontend is building.
51+
/// The name of the module that the frontend is building.
5252
std::string ModuleName;
5353

54+
/// The ABI name of the module that the frontend is building, to be used in
55+
/// mangling and metadata.
56+
std::string ModuleABIName;
57+
5458
/// The name of the library to link against when using this module.
5559
std::string ModuleLinkName;
5660

include/swift/Option/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,9 @@ def module_link_name_EQ : Joined<["-"], "module-link-name=">,
421421
def autolink_force_load : Flag<["-"], "autolink-force-load">,
422422
Flags<[FrontendOption, ModuleInterfaceOption, HelpHidden]>,
423423
HelpText<"Force ld to link against this module even if no symbols are used">;
424+
def module_abi_name : Separate<["-"], "module-abi-name">,
425+
Flags<[FrontendOption, ModuleInterfaceOption]>,
426+
HelpText<"ABI name to use for the contents of this module">;
424427

425428
def emit_module : Flag<["-"], "emit-module">,
426429
Flags<[FrontendOption, NoInteractiveOption, DoesNotAffectIncrementalBuild,

include/swift/Serialization/Validation.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ struct ValidationInfo {
9393
class ExtendedValidationInfo {
9494
SmallVector<StringRef, 4> ExtraClangImporterOpts;
9595
StringRef SDKPath;
96+
StringRef ModuleABIName;
9697
struct {
9798
unsigned ArePrivateImportsEnabled : 1;
9899
unsigned IsSIB : 1;
@@ -145,6 +146,9 @@ class ExtendedValidationInfo {
145146
void setAllowModuleWithCompilerErrorsEnabled(bool val) {
146147
Bits.IsAllowModuleWithCompilerErrorsEnabled = val;
147148
}
149+
150+
StringRef getModuleABIName() const { return ModuleABIName; }
151+
void setModuleABIName(StringRef name) { ModuleABIName = name; }
148152
};
149153

150154
/// Returns info about the serialized AST in the given data.

lib/AST/ASTContext.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ struct ASTContext::Implementation {
164164
/// DenseMap.
165165
llvm::MapVector<Identifier, ModuleDecl *> LoadedModules;
166166

167+
/// The set of top-level modules we have loaded, indexed by ABI name.
168+
llvm::MapVector<Identifier, ModuleDecl *> LoadedModulesByABIName;
169+
167170
// FIXME: This is a StringMap rather than a StringSet because StringSet
168171
// doesn't allow passing in a pre-existing allocator.
169172
llvm::StringMap<Identifier::Aligner, llvm::BumpPtrAllocator&>

lib/AST/ASTMangler.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2143,13 +2143,15 @@ void ASTMangler::appendModule(const ModuleDecl *module,
21432143
StringRef useModuleName) {
21442144
assert(!module->getParent() && "cannot mangle nested modules!");
21452145

2146+
StringRef ModName =
2147+
DWARFMangling ? module->getName().str() : module->getABIName().str();
2148+
21462149
// Try the special 'swift' substitution.
2147-
if (module->isStdlibModule()) {
2150+
if (ModName == STDLIB_NAME) {
21482151
assert(useModuleName.empty());
21492152
return appendOperator("s");
21502153
}
21512154

2152-
StringRef ModName = module->getName().str();
21532155
if (ModName == MANGLING_MODULE_OBJC) {
21542156
assert(useModuleName.empty());
21552157
return appendOperator("So");

lib/AST/Decl.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ StringRef Decl::getAlternateModuleName() const {
638638
}
639639
}
640640
}
641+
641642
for (auto *DC = getDeclContext(); DC; DC = DC->getParent()) {
642643
if (auto decl = DC->getAsDecl()) {
643644
if (decl == this)

lib/Driver/ToolChains.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
231231
inputArgs.AddLastArg(arguments, options::OPT_import_underlying_module);
232232
inputArgs.AddLastArg(arguments, options::OPT_module_cache_path);
233233
inputArgs.AddLastArg(arguments, options::OPT_module_link_name);
234+
inputArgs.AddLastArg(arguments, options::OPT_module_abi_name);
234235
inputArgs.AddLastArg(arguments, options::OPT_nostdimport);
235236
inputArgs.AddLastArg(arguments, options::OPT_parse_stdlib);
236237
inputArgs.AddLastArg(arguments, options::OPT_resource_dir);

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ bool ArgsToFrontendOptionsConverter::convert(
217217
return true;
218218
}
219219

220+
if (const Arg *A = Args.getLastArg(OPT_module_abi_name))
221+
Opts.ModuleABIName = A->getValue();
222+
220223
if (const Arg *A = Args.getLastArg(OPT_module_link_name))
221224
Opts.ModuleLinkName = A->getValue();
222225

lib/Frontend/Frontend.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,10 @@ ModuleDecl *CompilerInstance::getMainModule() const {
925925
MainModule->setPrivateImportsEnabled();
926926
if (Invocation.getFrontendOptions().EnableImplicitDynamic)
927927
MainModule->setImplicitDynamicEnabled();
928-
928+
if (!Invocation.getFrontendOptions().ModuleABIName.empty()) {
929+
MainModule->setABIName(getASTContext().getIdentifier(
930+
Invocation.getFrontendOptions().ModuleABIName));
931+
}
929932
if (Invocation.getFrontendOptions().EnableLibraryEvolution)
930933
MainModule->setResilienceStrategy(ResilienceStrategy::Resilient);
931934

0 commit comments

Comments
 (0)