Skip to content

Commit d02b34c

Browse files
committed
[NFC] Add conveniences to clean up import code
1 parent c13067a commit d02b34c

File tree

10 files changed

+60
-47
lines changed

10 files changed

+60
-47
lines changed

include/swift/AST/Import.h

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "llvm/ADT/SmallVector.h"
2929
#include "llvm/ADT/STLExtras.h"
3030
#include "llvm/ADT/StringRef.h"
31+
#include "llvm/Support/raw_ostream.h"
3132
#include <algorithm>
3233

3334
namespace swift {
@@ -151,6 +152,17 @@ namespace detail {
151152
if (empty()) return SourceRange();
152153
return SourceRange(raw.front().Loc, raw.back().Loc);
153154
}
155+
156+
void print(llvm::raw_ostream &os) const {
157+
llvm::interleave(*this,
158+
[&](Element elem) { os << elem.Item.str(); },
159+
[&]() { os << "."; });
160+
}
161+
162+
void getString(SmallVectorImpl<char> &modulePathStr) const {
163+
llvm::raw_svector_ostream os(modulePathStr);
164+
print(os);
165+
}
154166
};
155167

156168
// These shims avoid circularity between ASTContext.h and Import.h.
@@ -479,6 +491,9 @@ struct alignas(uint64_t) ImportedModule {
479491
assert(this->importedModule);
480492
}
481493

494+
explicit ImportedModule(ModuleDecl *importedModule)
495+
: ImportedModule(ImportPath::Access(), importedModule) { }
496+
482497
bool operator==(const ImportedModule &other) const {
483498
return (this->importedModule == other.importedModule) &&
484499
(this->accessPath == other.accessPath);
@@ -490,6 +505,10 @@ struct alignas(uint64_t) ImportedModule {
490505
/// The order of items in \p imports is \e not preserved.
491506
static void removeDuplicates(SmallVectorImpl<ImportedModule> &imports);
492507

508+
// Purely here to allow ImportedModule and UnloadedImportedModule to
509+
// substitute into the same templates.
510+
ImportPath::Access getAccessPath() const { return accessPath; }
511+
493512
/// Arbitrarily orders ImportedModule records, for inclusion in sets and such.
494513
class Order {
495514
public:
@@ -524,7 +543,7 @@ struct AttributedImport {
524543
/// Names of explicitly imported SPI groups.
525544
ArrayRef<Identifier> spiGroups;
526545

527-
AttributedImport(ModuleInfo module, ImportOptions options,
546+
AttributedImport(ModuleInfo module, ImportOptions options = ImportOptions(),
528547
StringRef filename = {}, ArrayRef<Identifier> spiGroups = {})
529548
: module(module), options(options), sourceFileArg(filename),
530549
spiGroups(spiGroups) {
@@ -533,13 +552,22 @@ struct AttributedImport {
533552
options.contains(ImportFlags::Reserved));
534553
}
535554

555+
template<class OtherModuleInfo>
556+
AttributedImport(ModuleInfo module, AttributedImport<OtherModuleInfo> other)
557+
: AttributedImport(module, other.options, other.sourceFileArg,
558+
other.spiGroups) { }
559+
536560
friend bool operator==(const AttributedImport<ModuleInfo> &lhs,
537561
const AttributedImport<ModuleInfo> &rhs) {
538562
return lhs.module == rhs.module &&
539563
lhs.options.toRaw() == rhs.options.toRaw() &&
540564
lhs.sourceFileArg == rhs.sourceFileArg &&
541565
lhs.spiGroups == rhs.spiGroups;
542566
}
567+
568+
AttributedImport<ImportedModule> getLoaded(ModuleDecl *loadedModule) const {
569+
return { ImportedModule(module.getAccessPath(), loadedModule), *this };
570+
}
543571
};
544572

545573
/// A module which has been implicitly imported.

include/swift/AST/ModuleDependencies.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define SWIFT_AST_MODULE_DEPENDENCIES_H
2020

2121
#include "swift/Basic/LLVM.h"
22+
#include "swift/AST/Import.h"
2223
#include "llvm/ADT/ArrayRef.h"
2324
#include "llvm/ADT/Optional.h"
2425
#include "llvm/ADT/StringSet.h"
@@ -330,6 +331,12 @@ class ModuleDependencies {
330331
void addModuleDependency(StringRef module,
331332
llvm::StringSet<> *alreadyAddedModules = nullptr);
332333

334+
/// Add a dependency on the given module, if it was not already in the set.
335+
void addModuleDependency(ImportPath::Module module,
336+
llvm::StringSet<> *alreadyAddedModules = nullptr) {
337+
addModuleDependency(module.front().Item.str(), alreadyAddedModules);
338+
}
339+
333340
/// Add all of the module dependencies for the imports in the given source
334341
/// file to the set of module dependencies.
335342
void addModuleDependencies(const SourceFile &sf,

lib/AST/ASTDumper.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -620,11 +620,7 @@ namespace {
620620
OS << " kind=" << getImportKindString(ID->getImportKind());
621621

622622
OS << " '";
623-
llvm::interleave(ID->getImportPath(),
624-
[&](const ImportPath::Element &Elem) {
625-
OS << Elem.Item;
626-
},
627-
[&] { OS << '.'; });
623+
ID->getImportPath().print(OS);
628624
OS << "')";
629625
}
630626

lib/AST/ModuleDependencies.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ void ModuleDependencies::addModuleDependencies(
6262
if (!importDecl)
6363
continue;
6464

65-
addModuleDependency(importDecl->getModulePath().front().Item.str(),
66-
&alreadyAddedModules);
65+
addModuleDependency(importDecl->getModulePath(), &alreadyAddedModules);
6766
}
6867

6968
auto fileName = sf.getFilename();

lib/AST/ModuleNameLookup.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,7 @@ void ModuleNameLookup<LookupStrategy>::lookupInModule(
202202
if (auto *loader = ctx.getClangModuleLoader()) {
203203
headerImportModule = loader->getImportedHeaderModule();
204204
if (headerImportModule) {
205-
ImportedModule import{ImportPath::Access(), headerImportModule};
206-
visitImport(import, nullptr);
205+
visitImport(ImportedModule(headerImportModule), nullptr);
207206
}
208207
}
209208
}

lib/AST/TypeCheckRequests.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,14 +1395,14 @@ void swift::simple_display(llvm::raw_ostream &out,
13951395
const AttributedImport<ImportedModule> &import) {
13961396
out << "import of ";
13971397

1398-
if (!import.module.accessPath.empty()) {
1399-
simple_display(out, import.module.accessPath.front().Item);
1400-
out << " in ";
1401-
}
1402-
14031398
simple_display(out, import.module.importedModule);
14041399

14051400
out << " [";
1401+
if (!import.module.accessPath.empty()) {
1402+
out << " scoped(";
1403+
import.module.accessPath.print(out);
1404+
out << ")";
1405+
}
14061406
if (import.options.contains(ImportFlags::Exported))
14071407
out << " exported";
14081408
if (import.options.contains(ImportFlags::Testable))
@@ -1414,11 +1414,9 @@ void swift::simple_display(llvm::raw_ostream &out,
14141414

14151415
if (import.options.contains(ImportFlags::SPIAccessControl)) {
14161416
out << " spi(";
1417-
llvm::interleave(import.spiGroups,
1418-
[&out](Identifier name) {
1419-
simple_display(out, name);
1420-
},
1421-
[&out]() { out << " "; });
1417+
llvm::interleaveComma(import.spiGroups, out, [&out](Identifier name) {
1418+
simple_display(out, name);
1419+
});
14221420
out << ")";
14231421
}
14241422

lib/FrontendTool/ScanDependencies.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ bool swift::scanDependencies(CompilerInstance &instance) {
818818

819819
// Add any implicit module names.
820820
for (const auto &import : importInfo.AdditionalUnloadedImports) {
821-
mainDependencies.addModuleDependency(import.module.getModulePath().front().Item.str(), &alreadyAddedModules);
821+
mainDependencies.addModuleDependency(import.module.getModulePath(), &alreadyAddedModules);
822822
}
823823

824824
// Already-loaded, implicitly imported module names.

lib/IDE/REPLCodeCompletion.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,17 +221,14 @@ doCodeCompletion(SourceFile &SF, StringRef EnteredCode, unsigned *BufferID,
221221
auto *lastModule = SF.getParentModule();
222222

223223
ImplicitImportInfo implicitImports;
224-
{
225-
ImportedModule import(ImportPath::Access(), lastModule);
226-
implicitImports.AdditionalImports.emplace_back(import, ImportOptions());
227-
}
224+
implicitImports.AdditionalImports.emplace_back(ImportedModule(lastModule));
228225

229226
// Carry over the private imports from the last module.
230227
SmallVector<ImportedModule, 8> imports;
231228
lastModule->getImportedModules(imports,
232229
ModuleDecl::ImportFilterKind::Default);
233230
for (auto &import : imports) {
234-
implicitImports.AdditionalImports.emplace_back(import, ImportOptions());
231+
implicitImports.AdditionalImports.emplace_back(import);
235232
}
236233

237234
// Create a new module and file for the code completion buffer, similar to how

lib/Sema/ImportResolution.cpp

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ struct UnboundImport {
118118
/// UnboundImport.
119119
AttributedImport<ImportedModule>
120120
makeAttributedImport(ModuleDecl *module) const {
121-
return { ImportedModule{ import.module.getAccessPath(), module },
122-
import.options, import.sourceFileArg, import.spiGroups };
121+
return import.getLoaded(module);
123122
}
124123

125124
private:
@@ -396,10 +395,7 @@ static void diagnoseNoSuchModule(ASTContext &ctx, SourceLoc importLoc,
396395
ImportPath::Module modulePath,
397396
bool nonfatalInREPL) {
398397
SmallString<64> modulePathStr;
399-
llvm::interleave(modulePath, [&](ImportPath::Element elem) {
400-
modulePathStr += elem.Item.str();
401-
},
402-
[&] { modulePathStr += "."; });
398+
modulePath.getString(modulePathStr);
403399

404400
auto diagKind = diag::sema_no_import;
405401
if (nonfatalInREPL && ctx.LangOpts.DebuggerSupport)
@@ -430,17 +426,14 @@ ModuleImplicitImportsRequest::evaluate(Evaluator &evaluator,
430426
case ImplicitStdlibKind::Builtin:
431427
stdlib = ctx.TheBuiltinModule;
432428
break;
433-
case ImplicitStdlibKind::Stdlib: {
429+
case ImplicitStdlibKind::Stdlib:
434430
stdlib = ctx.getStdlibModule(/*loadIfAbsent*/ true);
435431
assert(stdlib && "Missing stdlib?");
436432
break;
437433
}
438-
}
439434

440-
if (stdlib) {
441-
ImportedModule import(ImportPath::Access(), stdlib);
442-
imports.emplace_back(import, ImportOptions());
443-
}
435+
if (stdlib)
436+
imports.emplace_back(ImportedModule(stdlib));
444437

445438
// Add any modules we were asked to implicitly import.
446439
for (auto unloadedImport : importInfo.AdditionalUnloadedImports) {
@@ -451,10 +444,7 @@ ModuleImplicitImportsRequest::evaluate(Evaluator &evaluator,
451444
/*nonfatalInREPL=*/false);
452445
continue;
453446
}
454-
ImportedModule import(unloadedImport.module.getAccessPath(), importModule);
455-
imports.emplace_back(import, unloadedImport.options,
456-
unloadedImport.sourceFileArg,
457-
unloadedImport.spiGroups);
447+
imports.push_back(unloadedImport.getLoaded(importModule));
458448
}
459449

460450
// Add any pre-loaded modules.
@@ -469,17 +459,16 @@ ModuleImplicitImportsRequest::evaluate(Evaluator &evaluator,
469459
!clangImporter->importBridgingHeader(bridgingHeaderPath, module)) {
470460
auto *headerModule = clangImporter->getImportedHeaderModule();
471461
assert(headerModule && "Didn't load bridging header?");
472-
ImportedModule import(ImportPath::Access(), headerModule);
473-
imports.emplace_back(import, ImportFlags::Exported);
462+
imports.emplace_back(ImportedModule(headerModule), ImportFlags::Exported);
474463
}
475464

476465
// Implicitly import the underlying Clang half of this module if needed.
477466
if (importInfo.ShouldImportUnderlyingModule) {
478467
auto *underlyingMod = clangImporter->loadModule(
479468
SourceLoc(), ImportPath::Module::Builder(module->getName()).get());
480469
if (underlyingMod) {
481-
ImportedModule import(ImportPath::Access(), underlyingMod);
482-
imports.emplace_back(import, ImportFlags::Exported);
470+
imports.emplace_back(ImportedModule(underlyingMod),
471+
ImportFlags::Exported);
483472
} else {
484473
ctx.Diags.diagnose(SourceLoc(), diag::error_underlying_module_not_found,
485474
module->getName());

lib/Serialization/ModuleDependencyScanner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ ErrorOr<ModuleDependencies> ModuleDependencyScanner::scanInterfaceFile(
150150
// printed in the interface file, e.g. SwiftOnoneSupport.
151151
auto &imInfo = mainMod->getImplicitImportInfo();
152152
for (auto import: imInfo.AdditionalUnloadedImports) {
153-
Result->addModuleDependency(import.module.getModulePath().front().Item.str(), &alreadyAddedModules);
153+
Result->addModuleDependency(import.module.getModulePath(), &alreadyAddedModules);
154154
}
155155
return std::error_code();
156156
});

0 commit comments

Comments
 (0)