mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
[ModuleInterface] Print alias for module names in swiftinterface files
Ambiguities are introduced in generated swiftinterfaces when a type shares a name with a module (i.e. XCTest). This workaround uses the module-alias feature to avoid these ambiguities. Writing module references with a distinguishable prefix should allow normal type-checking to avoid the usual ambiguities. We should still aim for a proper fully-qualified named syntax, but this may help in the mean time. rdar://101969500
This commit is contained in:
@@ -58,7 +58,32 @@ static void printToolVersionAndFlagsComment(raw_ostream &out,
|
||||
out << "// " SWIFT_COMPILER_VERSION_KEY ": "
|
||||
<< ToolsVersion << "\n";
|
||||
out << "// " SWIFT_MODULE_FLAGS_KEY ": "
|
||||
<< Opts.Flags << "\n";
|
||||
<< Opts.Flags;
|
||||
|
||||
// Insert additional -module-alias flags
|
||||
if (Opts.AliasModuleNames) {
|
||||
llvm::SmallSet<StringRef, 2> aliasTargets;
|
||||
StringRef moduleName = M->getNameStr();
|
||||
aliasTargets.insert(M->getNameStr());
|
||||
out << " -module-alias " << MODULE_DISAMBIGUATING_PREFIX <<
|
||||
moduleName << "=" << moduleName;
|
||||
|
||||
SmallVector<ImportedModule> imports;
|
||||
M->getImportedModules(imports,
|
||||
{ModuleDecl::ImportFilterKind::Default,
|
||||
ModuleDecl::ImportFilterKind::Exported,
|
||||
ModuleDecl::ImportFilterKind::SPIOnly,
|
||||
ModuleDecl::ImportFilterKind::SPIAccessControl});
|
||||
for (ImportedModule import: imports) {
|
||||
StringRef importedName = import.importedModule->getNameStr();
|
||||
if (aliasTargets.insert(importedName).second) {
|
||||
out << " -module-alias " << MODULE_DISAMBIGUATING_PREFIX <<
|
||||
importedName << "=" << importedName;
|
||||
}
|
||||
}
|
||||
}
|
||||
out << "\n";
|
||||
|
||||
if (!Opts.IgnorableFlags.empty()) {
|
||||
out << "// " SWIFT_MODULE_FLAGS_IGNORABLE_KEY ": "
|
||||
<< Opts.IgnorableFlags << "\n";
|
||||
@@ -295,6 +320,8 @@ static void printImports(raw_ostream &out,
|
||||
}
|
||||
|
||||
out << "import ";
|
||||
if (Opts.AliasModuleNames)
|
||||
out << MODULE_DISAMBIGUATING_PREFIX;
|
||||
importedModule->getReverseFullModuleName().printForward(out);
|
||||
|
||||
// Write the access path we should be honoring but aren't.
|
||||
@@ -758,7 +785,8 @@ bool swift::emitSwiftInterface(raw_ostream &out,
|
||||
printImports(out, Opts, M);
|
||||
|
||||
const PrintOptions printOptions = PrintOptions::printSwiftInterfaceFile(
|
||||
M, Opts.PreserveTypesAsWritten, Opts.PrintFullConvention, Opts.PrintSPIs);
|
||||
M, Opts.PreserveTypesAsWritten, Opts.PrintFullConvention, Opts.PrintSPIs,
|
||||
Opts.AliasModuleNames);
|
||||
InheritedProtocolCollector::PerTypeMap inheritedProtocolMap;
|
||||
|
||||
SmallVector<Decl *, 16> topLevelDecls;
|
||||
|
||||
Reference in New Issue
Block a user