Skip to content

Commit 93704f6

Browse files
authored
Merge pull request swiftlang#63248 from xymus/swift-export-as
[ModuleInterface] Intro the flag `-export-as` for Swift modules
2 parents c8ef200 + c285c5e commit 93704f6

17 files changed

+165
-3
lines changed

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ ERROR(error_stdlib_module_name,none,
180180
"module name \"%0\" is reserved for the standard library"
181181
"%select{|; use -module-name flag to specify an alternate name}1",
182182
(StringRef, bool))
183+
184+
ERROR(error_bad_export_as_name,none,
185+
"export-as name \"%0\" is not a valid identifier",
186+
(StringRef))
187+
183188
ERROR(error_bad_package_name,none,
184189
"package name \"%0\" is not a valid identifier",
185190
(StringRef))

include/swift/AST/FileUnit.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,11 @@ class FileUnit : public DeclContext, public ASTAllocated<FileUnit> {
322322
/// The 'real name' is the actual binary name of the module, which can be different from the 'name'
323323
/// if module aliasing was used (via -module-alias flag).
324324
///
325-
/// Usually this is the module real name itself, but certain Clang features allow
326-
/// substituting another name instead.
325+
/// This is usually the module real name which can be overriden by an
326+
/// `export_as` definition of a clang module, or `-export-as` flag on an
327+
/// imported Swift module. Swift modules built from source do not apply
328+
/// their own `-export-as` flag, this way the swiftinterface can be
329+
/// verified.
327330
virtual StringRef getExportedModuleName() const {
328331
return getParentModule()->getRealName().str();
329332
}

include/swift/AST/Module.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ class ModuleDecl
175175

176176
/// The name of the package this module belongs to
177177
mutable Identifier PackageName;
178+
179+
/// Module name to use when referenced in clients module interfaces.
180+
mutable Identifier ExportAsName;
181+
178182
public:
179183
/// Produces the components of a given module's full name in reverse order.
180184
///
@@ -410,6 +414,12 @@ class ModuleDecl
410414
PackageName = name;
411415
}
412416

417+
Identifier getExportAsName() const { return ExportAsName; }
418+
419+
void setExportAsName(Identifier name) {
420+
ExportAsName = name;
421+
}
422+
413423
/// Retrieve the actual module name of an alias used for this module (if any).
414424
///
415425
/// For example, if '-module-alias Foo=Bar' is passed in when building the main module,

include/swift/Frontend/FrontendOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ class FrontendOptions {
6767
/// The name of the package this module belongs to.
6868
std::string PackageName;
6969

70+
/// Module name to use when referenced in clients module interfaces.
71+
std::string ExportAsName;
72+
7073
/// Arguments which should be passed in immediate mode.
7174
std::vector<std::string> ImmediateArgv;
7275

include/swift/Option/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,9 @@ def module_abi_name : Separate<["-"], "module-abi-name">,
484484
def package_name : Separate<["-"], "package-name">,
485485
Flags<[FrontendOption, ModuleInterfaceOptionIgnorable]>,
486486
HelpText<"Name of the package the module belongs to">;
487+
def export_as : Separate<["-"], "export-as">,
488+
Flags<[FrontendOption, ModuleInterfaceOptionIgnorable]>,
489+
HelpText<"Module name to use when referenced in clients module interfaces">;
487490

488491
def emit_module : Flag<["-"], "emit-module">,
489492
Flags<[FrontendOption, NoInteractiveOption, SupplementaryOutput]>,

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,8 @@ class SerializedASTFile final : public LoadedFile {
455455

456456
virtual StringRef getModuleDefiningPath() const override;
457457

458+
virtual StringRef getExportedModuleName() const override;
459+
458460
ValueDecl *getMainDecl() const override;
459461

460462
bool hasEntryPoint() const override;

include/swift/Serialization/Validation.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ class ExtendedValidationInfo {
108108
std::string SDKPath;
109109
StringRef ModuleABIName;
110110
StringRef ModulePackageName;
111+
StringRef ExportAsName;
111112
struct {
112113
unsigned ArePrivateImportsEnabled : 1;
113114
unsigned IsSIB : 1;
@@ -183,6 +184,9 @@ class ExtendedValidationInfo {
183184
StringRef getModulePackageName() const { return ModulePackageName; }
184185
void setModulePackageName(StringRef name) { ModulePackageName = name; }
185186

187+
StringRef getExportAsName() const { return ExportAsName; }
188+
void setExportAsName(StringRef name) { ExportAsName = name; }
189+
186190
bool isConcurrencyChecked() const {
187191
return Bits.IsConcurrencyChecked;
188192
}

lib/Driver/ToolChains.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
253253
inputArgs.AddLastArg(arguments, options::OPT_module_link_name);
254254
inputArgs.AddLastArg(arguments, options::OPT_module_abi_name);
255255
inputArgs.AddLastArg(arguments, options::OPT_package_name);
256+
inputArgs.AddLastArg(arguments, options::OPT_export_as);
256257
inputArgs.AddLastArg(arguments, options::OPT_nostdimport);
257258
inputArgs.AddLastArg(arguments, options::OPT_parse_stdlib);
258259
inputArgs.AddLastArg(arguments, options::OPT_resource_dir);

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,14 @@ bool ArgsToFrontendOptionsConverter::convert(
279279
Opts.PackageName = pkgName;
280280
}
281281

282+
if (const Arg *A = Args.getLastArg(OPT_export_as)) {
283+
auto exportAs = A->getValue();
284+
if (!Lexer::isIdentifier(exportAs))
285+
Diags.diagnose(SourceLoc(), diag::error_bad_export_as_name, exportAs);
286+
else
287+
Opts.ExportAsName = exportAs;
288+
}
289+
282290
// This must be called after computing module name, module abi name,
283291
// and module link name. If computing module aliases is unsuccessful,
284292
// return early.

lib/Frontend/Frontend.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,10 @@ ModuleDecl *CompilerInstance::getMainModule() const {
10851085
MainModule->setPackageName(getASTContext().getIdentifier(
10861086
Invocation.getFrontendOptions().PackageName));
10871087
}
1088+
if (!Invocation.getFrontendOptions().ExportAsName.empty()) {
1089+
MainModule->setExportAsName(getASTContext().getIdentifier(
1090+
Invocation.getFrontendOptions().ExportAsName));
1091+
}
10881092
if (Invocation.getFrontendOptions().EnableLibraryEvolution)
10891093
MainModule->setResilienceStrategy(ResilienceStrategy::Resilient);
10901094
if (Invocation.getLangOptions().isSwiftVersionAtLeast(6))

0 commit comments

Comments
 (0)