Skip to content

Commit 9feb758

Browse files
committed
Refactor
1 parent e4099d0 commit 9feb758

File tree

5 files changed

+65
-75
lines changed

5 files changed

+65
-75
lines changed

include/swift/AST/Module.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -864,15 +864,27 @@ class SourceFile final : public FileUnit {
864864
using ImportOptions = OptionSet<ImportFlags>;
865865

866866
typedef std::pair<ImportOptions, StringRef> ImportOptionsAndFilename;
867+
868+
struct ImportedModuleDesc {
869+
ModuleDecl::ImportedModule module;
870+
ImportOptions importOptions;
871+
StringRef filename;
872+
873+
ImportedModuleDesc(ModuleDecl::ImportedModule module, ImportOptions options)
874+
: module(module), importOptions(options) {}
875+
ImportedModuleDesc(ModuleDecl::ImportedModule module, ImportOptions options,
876+
StringRef filename)
877+
: module(module), importOptions(options), filename(filename) {}
878+
};
879+
867880
private:
868881
std::unique_ptr<LookupCache> Cache;
869882
LookupCache &getCache() const;
870883

871884
/// This is the list of modules that are imported by this module.
872885
///
873886
/// This is filled in by the Name Binding phase.
874-
ArrayRef<std::pair<ModuleDecl::ImportedModule, ImportOptionsAndFilename>>
875-
Imports;
887+
ArrayRef<ImportedModuleDesc> Imports;
876888

877889
/// A unique identifier representing this file; used to mark private decls
878890
/// within the file to keep them from conflicting with other files in the
@@ -976,9 +988,7 @@ class SourceFile final : public FileUnit {
976988
ImplicitModuleImportKind ModImpKind, bool KeepParsedTokens = false,
977989
bool KeepSyntaxTree = false);
978990

979-
void addImports(
980-
ArrayRef<std::pair<ModuleDecl::ImportedModule, ImportOptionsAndFilename>>
981-
IM);
991+
void addImports(ArrayRef<ImportedModuleDesc> IM);
982992

983993
bool hasTestableImport(const ModuleDecl *module) const;
984994

lib/AST/Module.cpp

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -778,8 +778,7 @@ namespace {
778778
class SourceFile::Impl {
779779
public:
780780
/// Only intended for use by lookupOperatorDeclForName.
781-
static ArrayRef<std::pair<ModuleDecl::ImportedModule,
782-
std::pair<SourceFile::ImportOptions, StringRef>>>
781+
static ArrayRef<SourceFile::ImportedModuleDesc>
783782
getImportsForSourceFile(const SourceFile &SF) {
784783
return SF.Imports;
785784
}
@@ -889,16 +888,16 @@ lookupOperatorDeclForName(const FileUnit &File, SourceLoc Loc, Identifier Name,
889888
ImportedOperatorsMap<OP_DECL> importedOperators;
890889
for (auto &imported : SourceFile::Impl::getImportsForSourceFile(SF)) {
891890
// Protect against source files that contrive to import their own modules.
892-
if (imported.first.second == ownModule)
891+
if (imported.module.second == ownModule)
893892
continue;
894893

895894
bool isExported =
896-
imported.second.first.contains(SourceFile::ImportFlags::Exported);
895+
imported.importOptions.contains(SourceFile::ImportFlags::Exported);
897896
if (!includePrivate && !isExported)
898897
continue;
899898

900-
Optional<OP_DECL *> maybeOp
901-
= lookupOperatorDeclForName(imported.first.second, Loc, Name, OP_MAP);
899+
Optional<OP_DECL *> maybeOp =
900+
lookupOperatorDeclForName(imported.module.second, Loc, Name, OP_MAP);
902901
if (!maybeOp)
903902
return None;
904903

@@ -990,21 +989,21 @@ void
990989
SourceFile::getImportedModules(SmallVectorImpl<ModuleDecl::ImportedModule> &modules,
991990
ModuleDecl::ImportFilter filter) const {
992991
assert(ASTStage >= Parsed || Kind == SourceFileKind::SIL);
993-
for (auto importPair : Imports) {
992+
for (auto desc : Imports) {
994993
switch (filter) {
995994
case ModuleDecl::ImportFilter::All:
996995
break;
997996
case ModuleDecl::ImportFilter::Public:
998-
if (!importPair.second.first.contains(ImportFlags::Exported))
997+
if (!desc.importOptions.contains(ImportFlags::Exported))
999998
continue;
1000999
break;
10011000
case ModuleDecl::ImportFilter::Private:
1002-
if (importPair.second.first.contains(ImportFlags::Exported))
1001+
if (desc.importOptions.contains(ImportFlags::Exported))
10031002
continue;
10041003
break;
10051004
}
10061005

1007-
modules.push_back(importPair.first);
1006+
modules.push_back(desc.module);
10081007
}
10091008
}
10101009

@@ -1374,17 +1373,12 @@ void SourceFile::print(ASTPrinter &Printer, const PrintOptions &PO) {
13741373
}
13751374
}
13761375

1377-
void SourceFile::addImports(
1378-
ArrayRef<std::pair<ModuleDecl::ImportedModule,
1379-
std::pair<ImportOptions, StringRef>>>
1380-
IM) {
1381-
using ImportPair = std::pair<ModuleDecl::ImportedModule,
1382-
std::pair<ImportOptions, StringRef>>;
1376+
void SourceFile::addImports(ArrayRef<ImportedModuleDesc> IM) {
13831377
if (IM.empty())
13841378
return;
13851379
ASTContext &ctx = getASTContext();
13861380
auto newBuf =
1387-
ctx.AllocateUninitialized<ImportPair>(Imports.size() + IM.size());
1381+
ctx.AllocateUninitialized<ImportedModuleDesc>(Imports.size() + IM.size());
13881382

13891383
auto iter = newBuf.begin();
13901384
iter = std::uninitialized_copy(Imports.begin(), Imports.end(), iter);
@@ -1395,20 +1389,16 @@ void SourceFile::addImports(
13951389
}
13961390

13971391
bool SourceFile::hasTestableImport(const swift::ModuleDecl *module) const {
1398-
using ImportPair = std::pair<ModuleDecl::ImportedModule,
1399-
std::pair<ImportOptions, StringRef>>;
14001392
return std::any_of(
1401-
Imports.begin(), Imports.end(), [module](ImportPair importPair) -> bool {
1402-
return importPair.first.second == module &&
1403-
importPair.second.first.contains(ImportFlags::Testable);
1393+
Imports.begin(), Imports.end(),
1394+
[module](ImportedModuleDesc desc) -> bool {
1395+
return desc.module.second == module &&
1396+
desc.importOptions.contains(ImportFlags::Testable);
14041397
});
14051398
}
14061399

14071400
bool SourceFile::hasPrivateImport(AccessLevel accessLevel,
14081401
const swift::ValueDecl *ofDecl) const {
1409-
using ImportPair = std::pair<ModuleDecl::ImportedModule,
1410-
std::pair<ImportOptions, StringRef>>;
1411-
14121402
auto *module = ofDecl->getModuleContext();
14131403
switch (accessLevel) {
14141404
case AccessLevel::Internal:
@@ -1417,9 +1407,9 @@ bool SourceFile::hasPrivateImport(AccessLevel accessLevel,
14171407
// filename does not need to match (and we don't serialize it for such
14181408
// decls).
14191409
return std::any_of(Imports.begin(), Imports.end(),
1420-
[module](ImportPair importPair) -> bool {
1421-
return importPair.first.second == module &&
1422-
importPair.second.first.contains(
1410+
[module](ImportedModuleDesc desc) -> bool {
1411+
return desc.module.second == module &&
1412+
desc.importOptions.contains(
14231413
ImportFlags::PrivateImport);
14241414
});
14251415
case AccessLevel::Open:
@@ -1447,11 +1437,11 @@ bool SourceFile::hasPrivateImport(AccessLevel accessLevel,
14471437
return false;
14481438

14491439
return std::any_of(Imports.begin(), Imports.end(),
1450-
[module, filename](ImportPair importPair) -> bool {
1451-
return importPair.first.second == module &&
1452-
importPair.second.first.contains(
1440+
[module, filename](ImportedModuleDesc desc) -> bool {
1441+
return desc.module.second == module &&
1442+
desc.importOptions.contains(
14531443
ImportFlags::PrivateImport) &&
1454-
importPair.second.second == filename;
1444+
desc.filename == filename;
14551445
});
14561446
}
14571447

@@ -1500,9 +1490,8 @@ static void performAutoImport(
15001490

15011491
// FIXME: These will be the same for most source files, but we copy them
15021492
// over and over again.
1503-
auto Imports =
1504-
std::make_pair(ModuleDecl::ImportedModule({}, M),
1505-
std::make_pair(SourceFile::ImportOptions(), StringRef()));
1493+
auto Imports = SourceFile::ImportedModuleDesc(
1494+
ModuleDecl::ImportedModule({}, M), SourceFile::ImportOptions());
15061495
SF.addImports(Imports);
15071496
}
15081497

lib/Frontend/Frontend.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -497,23 +497,24 @@ ModuleDecl *CompilerInstance::getMainModule() {
497497

498498
static void addAdditionalInitialImportsTo(
499499
SourceFile *SF, const CompilerInstance::ImplicitImports &implicitImports) {
500-
using ImportPair = std::pair<ModuleDecl::ImportedModule,
501-
std::pair<SourceFile::ImportOptions, StringRef>>;
502-
SmallVector<ImportPair, 4> additionalImports;
500+
SmallVector<SourceFile::ImportedModuleDesc, 4> additionalImports;
503501

504502
if (implicitImports.objCModuleUnderlyingMixedFramework)
505-
additionalImports.push_back(
506-
{{/*accessPath=*/{},
507-
implicitImports.objCModuleUnderlyingMixedFramework},
508-
{SourceFile::ImportFlags::Exported, StringRef()}});
503+
additionalImports.push_back(SourceFile::ImportedModuleDesc(
504+
ModuleDecl::ImportedModule(
505+
/*accessPath=*/{},
506+
implicitImports.objCModuleUnderlyingMixedFramework),
507+
SourceFile::ImportFlags::Exported));
509508
if (implicitImports.headerModule)
510-
additionalImports.push_back(
511-
{{/*accessPath=*/{}, implicitImports.headerModule},
512-
{SourceFile::ImportFlags::Exported, StringRef()}});
509+
additionalImports.push_back(SourceFile::ImportedModuleDesc(
510+
ModuleDecl::ImportedModule(/*accessPath=*/{},
511+
implicitImports.headerModule),
512+
SourceFile::ImportFlags::Exported));
513513
if (!implicitImports.modules.empty()) {
514514
for (auto &importModule : implicitImports.modules) {
515-
additionalImports.push_back(
516-
{{/*accessPath=*/{}, importModule}, {{}, StringRef()}});
515+
additionalImports.push_back(SourceFile::ImportedModuleDesc(
516+
ModuleDecl::ImportedModule(/*accessPath=*/{}, importModule),
517+
SourceFile::ImportOptions()));
517518
}
518519
}
519520

lib/Immediate/REPL.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,22 +171,17 @@ typeCheckREPLInput(ModuleDecl *MostRecentModule, StringRef Name,
171171

172172
ModuleDecl::ImportedModule ImportOfMostRecentModule{
173173
/*AccessPath*/{}, MostRecentModule};
174-
REPLInputFile.addImports(
175-
std::make_pair(ImportOfMostRecentModule,
176-
std::make_pair(SourceFile::ImportOptions(), StringRef())));
174+
REPLInputFile.addImports(SourceFile::ImportedModuleDesc(
175+
ImportOfMostRecentModule, SourceFile::ImportOptions()));
177176

178177
SmallVector<ModuleDecl::ImportedModule, 8> Imports;
179178
MostRecentModule->getImportedModules(Imports,
180179
ModuleDecl::ImportFilter::Private);
181180
if (!Imports.empty()) {
182-
SmallVector<std::pair<ModuleDecl::ImportedModule,
183-
std::pair<SourceFile::ImportOptions, StringRef>>,
184-
8>
185-
ImportsWithOptions;
181+
SmallVector<SourceFile::ImportedModuleDesc, 8> ImportsWithOptions;
186182
for (auto Import : Imports) {
187-
ImportsWithOptions.emplace_back(
188-
Import,
189-
std::make_pair(SourceFile::ImportFlags::Exported, StringRef()));
183+
ImportsWithOptions.emplace_back(SourceFile::ImportedModuleDesc(
184+
Import, SourceFile::ImportFlags::Exported));
190185
}
191186
REPLInputFile.addImports(ImportsWithOptions);
192187
}

lib/Sema/NameBinding.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,8 @@ namespace {
5353
return Context.Diags.diagnose(std::forward<ArgTypes>(Args)...);
5454
}
5555

56-
void addImport(
57-
SmallVectorImpl<
58-
std::pair<ImportedModule, std::pair<ImportOptions, StringRef>>>
59-
&imports,
60-
ImportDecl *ID);
56+
void addImport(SmallVectorImpl<SourceFile::ImportedModuleDesc> &imports,
57+
ImportDecl *ID);
6158

6259
/// Load a module referenced by an import statement.
6360
///
@@ -170,9 +167,7 @@ static bool shouldImportSelfImportClang(const ImportDecl *ID,
170167
}
171168

172169
void NameBinder::addImport(
173-
SmallVectorImpl<std::pair<ImportedModule,
174-
std::pair<ImportOptions, StringRef>>> &imports,
175-
ImportDecl *ID) {
170+
SmallVectorImpl<SourceFile::ImportedModuleDesc> &imports, ImportDecl *ID) {
176171
if (ID->getModulePath().front().first == SF.getParentModule()->getName() &&
177172
ID->getModulePath().size() == 1 && !shouldImportSelfImportClang(ID, SF)) {
178173
// If the imported module name is the same as the current module,
@@ -257,11 +252,12 @@ void NameBinder::addImport(
257252
if (privateImportAttr)
258253
options |= SourceFile::ImportFlags::PrivateImport;
259254

260-
imports.push_back({{ID->getDeclPath(), M}, {options, privateImportFileName}});
255+
imports.push_back(SourceFile::ImportedModuleDesc(
256+
{ID->getDeclPath(), M}, options, privateImportFileName));
261257

262258
if (topLevelModule != M)
263-
imports.push_back({{ID->getDeclPath(), topLevelModule},
264-
{options, privateImportFileName}});
259+
imports.push_back(SourceFile::ImportedModuleDesc(
260+
{ID->getDeclPath(), topLevelModule}, options, privateImportFileName));
265261

266262
if (ID->getImportKind() != ImportKind::Module) {
267263
// If we're importing a specific decl, validate the import kind.
@@ -387,8 +383,7 @@ void swift::performNameBinding(SourceFile &SF, unsigned StartElem) {
387383

388384
NameBinder Binder(SF);
389385

390-
SmallVector<std::pair<ImportedModule, std::pair<ImportOptions, StringRef>>, 8>
391-
ImportedModules;
386+
SmallVector<SourceFile::ImportedModuleDesc, 8> ImportedModules;
392387

393388
// Do a prepass over the declarations to find and load the imported modules
394389
// and map operator decls.

0 commit comments

Comments
 (0)