Skip to content

Commit d2434e1

Browse files
committed
NFC: Rename NameBinding to ImportResolution
1 parent e8cbe76 commit d2434e1

File tree

10 files changed

+77
-83
lines changed

10 files changed

+77
-83
lines changed

include/swift/AST/SourceFile.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@ class SourceFile final : public FileUnit {
318318
enum ASTStage_t {
319319
/// The source file is not name bound or type checked.
320320
Unprocessed,
321-
/// Name binding has completed.
322-
NameBound,
321+
/// Import resolution has completed.
322+
ImportsResolved,
323323
/// Type checking has completed.
324324
TypeChecked
325325
};

include/swift/Subsystems.h

+10-11
Original file line numberDiff line numberDiff line change
@@ -118,33 +118,32 @@ namespace swift {
118118
bool TokenizeInterpolatedString = true,
119119
ArrayRef<Token> SplitTokens = ArrayRef<Token>());
120120

121-
/// Once parsing is complete, this walks the AST to resolve imports, record
122-
/// operators, and do other top-level validation.
123-
void performNameBinding(SourceFile &SF);
121+
/// This walks the AST to resolve imports.
122+
void performImportResolution(SourceFile &SF);
124123

125124
/// Once type-checking is complete, this instruments code with calls to an
126125
/// intrinsic that record the expected values of local variables so they can
127126
/// be compared against the results from the debugger.
128127
void performDebuggerTestingTransform(SourceFile &SF);
129128

130-
/// Once parsing and name-binding are complete, this optionally transforms the
131-
/// ASTs to add calls to external logging functions.
129+
/// Once type checking is complete, this optionally transforms the ASTs to add
130+
/// calls to external logging functions.
132131
///
133132
/// \param HighPerformance True if the playground transform should omit
134133
/// instrumentation that has a high runtime performance impact.
135134
void performPlaygroundTransform(SourceFile &SF, bool HighPerformance);
136135

137-
/// Once parsing and name-binding are complete this optionally walks the ASTs
138-
/// to add calls to externally provided functions that simulate
139-
/// "program counter"-like debugging events. See the comment at the top of
140-
/// lib/Sema/PCMacro.cpp for a description of the calls inserted.
136+
/// Once type checking is complete this optionally walks the ASTs to add calls
137+
/// to externally provided functions that simulate "program counter"-like
138+
/// debugging events. See the comment at the top of lib/Sema/PCMacro.cpp for a
139+
/// description of the calls inserted.
141140
void performPCMacro(SourceFile &SF);
142141

143142
/// Bind all 'extension' visible from \p SF to the extended nominal.
144143
void bindExtensions(SourceFile &SF);
145144

146-
/// Once parsing and name-binding are complete, this walks the AST to resolve
147-
/// types and diagnose problems therein.
145+
/// Once import resolution is complete, this walks the AST to resolve types
146+
/// and diagnose problems therein.
148147
void performTypeChecking(SourceFile &SF);
149148

150149
/// Now that we have type-checked an entire module, perform any type

lib/AST/Module.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1182,7 +1182,7 @@ lookupOperatorDeclForName(const FileUnit &File, SourceLoc Loc,
11821182
}
11831183

11841184
auto &SF = cast<SourceFile>(File);
1185-
assert(SF.ASTStage >= SourceFile::NameBound);
1185+
assert(SF.ASTStage >= SourceFile::ImportsResolved);
11861186

11871187
// Check if the decl exists on the file.
11881188
if (auto *op = OperatorLookup<OP_DECL>::lookup(eval, desc))

lib/Frontend/Frontend.cpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ shouldImplicityImportSwiftOnoneSupportModule(CompilerInvocation &Invocation) {
752752
}
753753

754754
void CompilerInstance::performParseAndResolveImportsOnly() {
755-
performSemaUpTo(SourceFile::NameBound);
755+
performSemaUpTo(SourceFile::ImportsResolved);
756756
}
757757

758758
void CompilerInstance::performSema() {
@@ -926,12 +926,12 @@ void CompilerInstance::parseAndCheckTypesUpTo(
926926
auto *SF = dyn_cast<SourceFile>(File);
927927
if (!SF)
928928
return true;
929-
return SF->ASTStage >= SourceFile::NameBound;
929+
return SF->ASTStage >= SourceFile::ImportsResolved;
930930
}) && "some files have not yet had their imports resolved");
931931
MainModule->setHasResolvedImports();
932932

933933
forEachFileToTypeCheck([&](SourceFile &SF) {
934-
if (limitStage == SourceFile::NameBound) {
934+
if (limitStage == SourceFile::ImportsResolved) {
935935
bindExtensions(SF);
936936
return;
937937
}
@@ -951,8 +951,8 @@ void CompilerInstance::parseAndCheckTypesUpTo(
951951
}
952952
});
953953

954-
// If the limiting AST stage is name binding, we're done.
955-
if (limitStage <= SourceFile::NameBound) {
954+
// If the limiting AST stage is import resolution, we're done.
955+
if (limitStage <= SourceFile::ImportsResolved) {
956956
return;
957957
}
958958

@@ -967,8 +967,8 @@ void CompilerInstance::parseLibraryFile(
967967
SourceFileKind::Library, implicitImports.kind, BufferID);
968968
addAdditionalInitialImportsTo(NextInput, implicitImports);
969969

970-
// Name binding will lazily trigger parsing of the file.
971-
performNameBinding(*NextInput);
970+
// Import resolution will lazily trigger parsing of the file.
971+
performImportResolution(*NextInput);
972972
}
973973

974974
bool CompilerInstance::parsePartialModulesAndLibraryFiles(
@@ -997,7 +997,7 @@ bool CompilerInstance::parsePartialModulesAndLibraryFiles(
997997

998998
void CompilerInstance::parseAndTypeCheckMainFileUpTo(
999999
SourceFile::ASTStage_t LimitStage) {
1000-
assert(LimitStage >= SourceFile::NameBound);
1000+
assert(LimitStage >= SourceFile::ImportsResolved);
10011001
FrontendStatsTracer tracer(getStatsReporter(),
10021002
"parse-and-typecheck-main-file");
10031003
bool mainIsPrimary =
@@ -1010,13 +1010,13 @@ void CompilerInstance::parseAndTypeCheckMainFileUpTo(
10101010
auto DidSuppressWarnings = Diags.getSuppressWarnings();
10111011
Diags.setSuppressWarnings(DidSuppressWarnings || !mainIsPrimary);
10121012

1013-
// For a primary, perform type checking if needed. Otherwise, just do name
1014-
// binding.
1013+
// For a primary, perform type checking if needed. Otherwise, just do import
1014+
// resolution.
10151015
if (mainIsPrimary && LimitStage >= SourceFile::TypeChecked) {
10161016
performTypeChecking(MainFile);
10171017
} else {
10181018
assert(!TheSILModule && "Should perform type checking for SIL");
1019-
performNameBinding(MainFile);
1019+
performImportResolution(MainFile);
10201020
}
10211021

10221022
// Parse the SIL decls if needed.

lib/IDE/CompletionInstance.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ bool CompletionInstance::performCachedOperaitonIfPossible(
326326

327327
// Re-process the whole file (parsing will be lazily triggered). Still
328328
// re-use imported modules.
329-
performNameBinding(*newSF);
329+
performImportResolution(*newSF);
330330
bindExtensions(*newSF);
331331

332332
#ifndef NDEBUG

lib/Sema/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ add_swift_host_library(swiftSema STATIC
2525
DerivedConformanceError.cpp
2626
DerivedConformanceRawRepresentable.cpp
2727
DerivedConformances.cpp
28+
ImportResolution.cpp
2829
InstrumenterSupport.cpp
2930
LookupVisibleDecls.cpp
3031
MiscDiagnostics.cpp
31-
NameBinding.cpp
3232
PCMacro.cpp
3333
PlaygroundTransform.cpp
3434
ResilienceDiagnostics.cpp

lib/Sema/NameBinding.cpp lib/Sema/ImportResolution.cpp

+46-50
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
//===--- NameBinding.cpp - Name Binding -----------------------------------===//
1+
//===--- ImportResolution.cpp - Import Resolution -------------------------===//
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -11,11 +11,10 @@
1111
//===----------------------------------------------------------------------===//
1212
//
1313
// This file performs import resolution.
14-
// FIXME: Rename NameBinding to ImportResolution.
1514
//
1615
//===----------------------------------------------------------------------===//
1716

18-
#define DEBUG_TYPE "swift-name-binding"
17+
#define DEBUG_TYPE "swift-import-resolution"
1918
#include "swift/AST/ASTWalker.h"
2019
#include "swift/AST/DiagnosticsSema.h"
2120
#include "swift/AST/ModuleLoader.h"
@@ -41,7 +40,7 @@
4140
using namespace swift;
4241

4342
//===----------------------------------------------------------------------===//
44-
// MARK: NameBinder and supporting types
43+
// MARK: ImportResolver and supporting types
4544
//===----------------------------------------------------------------------===//
4645

4746
using ImportedModule = ModuleDecl::ImportedModule;
@@ -50,8 +49,8 @@ using ImportOptions = SourceFile::ImportOptions;
5049
using ImportFlags = SourceFile::ImportFlags;
5150

5251
namespace {
53-
/// Represents an import which the NameBinder knows exists, but which has not
54-
/// yet had its options checked, module loaded, or cross-imports found.
52+
/// Represents an import which the ImportResolver knows exists, but which has
53+
/// not yet had its options checked, module loaded, or cross-imports found.
5554
///
5655
/// An UnboundImport may represent a physical ImportDecl written in the
5756
/// source, or it may represent a cross-import overlay that has been found and
@@ -160,8 +159,8 @@ struct UnboundImport {
160159
Diag<Identifier> diagID);
161160
};
162161

163-
class NameBinder final : public DeclVisitor<NameBinder> {
164-
friend DeclVisitor<NameBinder>;
162+
class ImportResolver final : public DeclVisitor<ImportResolver> {
163+
friend DeclVisitor<ImportResolver>;
165164

166165
SourceFile &SF;
167166
ASTContext &ctx;
@@ -196,9 +195,7 @@ class NameBinder final : public DeclVisitor<NameBinder> {
196195
size_t nextModuleToCrossImport = 0;
197196

198197
public:
199-
NameBinder(SourceFile &SF)
200-
: SF(SF), ctx(SF.getASTContext())
201-
{ }
198+
ImportResolver(SourceFile &SF) : SF(SF), ctx(SF.getASTContext()) {}
202199

203200
/// Retrieve the finalized imports.
204201
ArrayRef<ImportedModuleDesc> getFinishedImports() const {
@@ -258,54 +255,53 @@ class NameBinder final : public DeclVisitor<NameBinder> {
258255
} // end anonymous namespace
259256

260257
//===----------------------------------------------------------------------===//
261-
// MARK: performNameBinding
258+
// MARK: performImportResolution
262259
//===----------------------------------------------------------------------===//
263260

264-
/// performNameBinding - Once parsing is complete, this walks the AST to
265-
/// resolve imports.
261+
/// performImportResolution - This walks the AST to resolve imports.
266262
///
267-
/// Most names are actually bound by the type checker, but before we can
268-
/// type-check a source file, we need to make declarations imported from other
269-
/// modules available. Name binding processes top-level \c ImportDecl nodes
270-
/// to perform this task, along with related validation.
263+
/// Before we can type-check a source file, we need to make declarations
264+
/// imported from other modules available. This is done by processing top-level
265+
/// \c ImportDecl nodes, along with related validation.
271266
///
272-
/// Name binding operates on a parsed but otherwise unvalidated AST.
273-
void swift::performNameBinding(SourceFile &SF) {
267+
/// Import resolution operates on a parsed but otherwise unvalidated AST.
268+
void swift::performImportResolution(SourceFile &SF) {
274269
FrontendStatsTracer tracer(SF.getASTContext().Stats,
275-
"Name binding");
270+
"Import resolution");
276271

277272
// Make sure we skip adding the standard library imports if the
278273
// source file is empty.
279-
if (SF.ASTStage == SourceFile::NameBound || SF.getTopLevelDecls().empty()) {
280-
SF.ASTStage = SourceFile::NameBound;
274+
if (SF.ASTStage == SourceFile::ImportsResolved ||
275+
SF.getTopLevelDecls().empty()) {
276+
SF.ASTStage = SourceFile::ImportsResolved;
281277
return;
282278
}
283279

284-
NameBinder Binder(SF);
280+
ImportResolver resolver(SF);
285281

286282
// Resolve each import declaration.
287283
for (auto D : SF.getTopLevelDecls())
288-
Binder.visit(D);
284+
resolver.visit(D);
289285

290-
SF.addImports(Binder.getFinishedImports());
286+
SF.addImports(resolver.getFinishedImports());
291287

292-
SF.ASTStage = SourceFile::NameBound;
288+
SF.ASTStage = SourceFile::ImportsResolved;
293289
verify(SF);
294290
}
295291

296292
//===----------------------------------------------------------------------===//
297293
// MARK: Import handling generally
298294
//===----------------------------------------------------------------------===//
299295

300-
void NameBinder::visitImportDecl(ImportDecl *ID) {
296+
void ImportResolver::visitImportDecl(ImportDecl *ID) {
301297
assert(unboundImports.empty());
302298

303299
unboundImports.emplace_back(ID);
304300
while(!unboundImports.empty())
305301
bindImport(unboundImports.pop_back_val());
306302
}
307303

308-
void NameBinder::bindImport(UnboundImport &&I) {
304+
void ImportResolver::bindImport(UnboundImport &&I) {
309305
auto ID = I.getImportDecl();
310306

311307
if (!I.checkNotTautological(SF)) {
@@ -343,7 +339,7 @@ void NameBinder::bindImport(UnboundImport &&I) {
343339
ID.get()->setModule(M);
344340
}
345341

346-
void NameBinder::addImport(const UnboundImport &I, ModuleDecl *M) {
342+
void ImportResolver::addImport(const UnboundImport &I, ModuleDecl *M) {
347343
auto importDesc = I.makeDesc(M);
348344
addVisibleModules(importDesc);
349345
boundImports.push_back(importDesc);
@@ -353,7 +349,8 @@ void NameBinder::addImport(const UnboundImport &I, ModuleDecl *M) {
353349
// MARK: Import module loading
354350
//===----------------------------------------------------------------------===//
355351

356-
ModuleDecl *NameBinder::getModule(ArrayRef<Located<Identifier>> modulePath) {
352+
ModuleDecl *
353+
ImportResolver::getModule(ArrayRef<Located<Identifier>> modulePath) {
357354
assert(!modulePath.empty());
358355
auto moduleID = modulePath[0];
359356

@@ -667,7 +664,7 @@ ScopedImportLookupRequest::evaluate(Evaluator &evaluator,
667664

668665
// If we weren't able to load the module referenced by the import, we're done.
669666
// The fact that we failed to load the module has already been diagnosed by
670-
// name binding.
667+
// import resolution.
671668
auto *module = import->getModule();
672669
if (!module)
673670
return ArrayRef<ValueDecl *>();
@@ -676,8 +673,8 @@ ScopedImportLookupRequest::evaluate(Evaluator &evaluator,
676673
///
677674
/// We validate the scope by making sure that the named declaration exists
678675
/// and is of the kind indicated by the keyword. This can't be done until
679-
/// we've performed name binding, since that can introduce additional imports
680-
/// (such as cross-import overlays) which could provide the declaration.
676+
/// we've performed import resolution, since that can introduce additional
677+
/// imports (such as cross-import overlays) which could provide the declaration.
681678
auto &ctx = module->getASTContext();
682679
auto declPath = import->getDeclPath();
683680
auto modulePath = import->getModulePath();
@@ -789,16 +786,16 @@ UnboundImport::UnboundImport(ASTContext &ctx,
789786
options |= ImportFlags::ImplementationOnly;
790787
}
791788

792-
void NameBinder::crossImport(ModuleDecl *M, UnboundImport &I) {
789+
void ImportResolver::crossImport(ModuleDecl *M, UnboundImport &I) {
793790
// FIXME: There is a fundamental problem with this find-as-we-go approach:
794791
// The '@_exported import'-ed modules in this module's other files should be
795792
// taken into account, but they haven't been bound yet, and binding them would
796793
// require cross-importing. Chicken, meet egg.
797794
//
798-
// The way to fix this is probably to restructure name binding so we first
799-
// bind all exported imports in all files, then bind all other imports in each
800-
// file. This may become simpler if we bind all ImportDecls before we start
801-
// computing cross-imports, but I haven't figured that part out yet.
795+
// The way to fix this is probably to restructure import resolution so we
796+
// first bind all exported imports in all files, then bind all other imports
797+
// in each file. This may become simpler if we bind all ImportDecls before we
798+
// start computing cross-imports, but I haven't figured that part out yet.
802799
//
803800
// Fixing this is tracked within Apple by rdar://problem/59527118. I haven't
804801
// filed an SR because I plan to address it myself, but if this comment is
@@ -855,11 +852,10 @@ void NameBinder::crossImport(ModuleDecl *M, UnboundImport &I) {
855852
nextModuleToCrossImport = crossImportableModules.size();
856853
}
857854

858-
void
859-
NameBinder::findCrossImportsInLists(UnboundImport &I,
860-
ArrayRef<ImportedModuleDesc> declaring,
861-
ArrayRef<ImportedModuleDesc> bystanding,
862-
bool shouldDiagnoseRedundantCrossImports) {
855+
void ImportResolver::findCrossImportsInLists(
856+
UnboundImport &I, ArrayRef<ImportedModuleDesc> declaring,
857+
ArrayRef<ImportedModuleDesc> bystanding,
858+
bool shouldDiagnoseRedundantCrossImports) {
863859
for (auto &declaringImport : declaring) {
864860
if (!canCrossImport(declaringImport))
865861
continue;
@@ -874,10 +870,10 @@ NameBinder::findCrossImportsInLists(UnboundImport &I,
874870
}
875871
}
876872

877-
void NameBinder::findCrossImports(UnboundImport &I,
878-
const ImportedModuleDesc &declaringImport,
879-
const ImportedModuleDesc &bystandingImport,
880-
bool shouldDiagnoseRedundantCrossImports) {
873+
void ImportResolver::findCrossImports(
874+
UnboundImport &I, const ImportedModuleDesc &declaringImport,
875+
const ImportedModuleDesc &bystandingImport,
876+
bool shouldDiagnoseRedundantCrossImports) {
881877
assert(&declaringImport != &bystandingImport);
882878

883879
LLVM_DEBUG(
@@ -935,7 +931,7 @@ static bool isSubmodule(ModuleDecl* M) {
935931
return clangMod && clangMod->Parent;
936932
}
937933

938-
void NameBinder::addVisibleModules(ImportedModuleDesc importDesc) {
934+
void ImportResolver::addVisibleModules(ImportedModuleDesc importDesc) {
939935
// FIXME: namelookup::getAllImports() doesn't quite do what we need (mainly
940936
// w.r.t. scoped imports), but it seems like we could extend it to do so, and
941937
// then eliminate most of this.

0 commit comments

Comments
 (0)