1
- // ===--- NameBinding .cpp - Name Binding ---------- -------------------------===//
1
+ // ===--- ImportResolution .cpp - Import Resolution -------------------------===//
2
2
//
3
3
// This source file is part of the Swift.org open source project
4
4
//
5
- // Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5
+ // Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6
6
// Licensed under Apache License v2.0 with Runtime Library Exception
7
7
//
8
8
// See https://swift.org/LICENSE.txt for license information
11
11
// ===----------------------------------------------------------------------===//
12
12
//
13
13
// This file performs import resolution.
14
- // FIXME: Rename NameBinding to ImportResolution.
15
14
//
16
15
// ===----------------------------------------------------------------------===//
17
16
18
- #define DEBUG_TYPE " swift-name-binding "
17
+ #define DEBUG_TYPE " swift-import-resolution "
19
18
#include " swift/AST/ASTWalker.h"
20
19
#include " swift/AST/DiagnosticsSema.h"
21
20
#include " swift/AST/ModuleLoader.h"
41
40
using namespace swift ;
42
41
43
42
// ===----------------------------------------------------------------------===//
44
- // MARK: NameBinder and supporting types
43
+ // MARK: ImportResolver and supporting types
45
44
// ===----------------------------------------------------------------------===//
46
45
47
46
using ImportedModule = ModuleDecl::ImportedModule;
@@ -50,8 +49,8 @@ using ImportOptions = SourceFile::ImportOptions;
50
49
using ImportFlags = SourceFile::ImportFlags;
51
50
52
51
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.
55
54
// /
56
55
// / An UnboundImport may represent a physical ImportDecl written in the
57
56
// / source, or it may represent a cross-import overlay that has been found and
@@ -160,8 +159,8 @@ struct UnboundImport {
160
159
Diag<Identifier> diagID);
161
160
};
162
161
163
- class NameBinder final : public DeclVisitor<NameBinder > {
164
- friend DeclVisitor<NameBinder >;
162
+ class ImportResolver final : public DeclVisitor<ImportResolver > {
163
+ friend DeclVisitor<ImportResolver >;
165
164
166
165
SourceFile &SF;
167
166
ASTContext &ctx;
@@ -196,9 +195,7 @@ class NameBinder final : public DeclVisitor<NameBinder> {
196
195
size_t nextModuleToCrossImport = 0 ;
197
196
198
197
public:
199
- NameBinder (SourceFile &SF)
200
- : SF(SF), ctx(SF.getASTContext())
201
- { }
198
+ ImportResolver (SourceFile &SF) : SF(SF), ctx(SF.getASTContext()) {}
202
199
203
200
// / Retrieve the finalized imports.
204
201
ArrayRef<ImportedModuleDesc> getFinishedImports () const {
@@ -258,54 +255,53 @@ class NameBinder final : public DeclVisitor<NameBinder> {
258
255
} // end anonymous namespace
259
256
260
257
// ===----------------------------------------------------------------------===//
261
- // MARK: performNameBinding
258
+ // MARK: performImportResolution
262
259
// ===----------------------------------------------------------------------===//
263
260
264
- // / performNameBinding - Once parsing is complete, this walks the AST to
265
- // / resolve imports.
261
+ // / performImportResolution - This walks the AST to resolve imports.
266
262
// /
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.
271
266
// /
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) {
274
269
FrontendStatsTracer tracer (SF.getASTContext ().Stats ,
275
- " Name binding " );
270
+ " Import resolution " );
276
271
277
272
// Make sure we skip adding the standard library imports if the
278
273
// 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;
281
277
return ;
282
278
}
283
279
284
- NameBinder Binder (SF);
280
+ ImportResolver resolver (SF);
285
281
286
282
// Resolve each import declaration.
287
283
for (auto D : SF.getTopLevelDecls ())
288
- Binder .visit (D);
284
+ resolver .visit (D);
289
285
290
- SF.addImports (Binder .getFinishedImports ());
286
+ SF.addImports (resolver .getFinishedImports ());
291
287
292
- SF.ASTStage = SourceFile::NameBound ;
288
+ SF.ASTStage = SourceFile::ImportsResolved ;
293
289
verify (SF);
294
290
}
295
291
296
292
// ===----------------------------------------------------------------------===//
297
293
// MARK: Import handling generally
298
294
// ===----------------------------------------------------------------------===//
299
295
300
- void NameBinder ::visitImportDecl (ImportDecl *ID) {
296
+ void ImportResolver ::visitImportDecl (ImportDecl *ID) {
301
297
assert (unboundImports.empty ());
302
298
303
299
unboundImports.emplace_back (ID);
304
300
while (!unboundImports.empty ())
305
301
bindImport (unboundImports.pop_back_val ());
306
302
}
307
303
308
- void NameBinder ::bindImport (UnboundImport &&I) {
304
+ void ImportResolver ::bindImport (UnboundImport &&I) {
309
305
auto ID = I.getImportDecl ();
310
306
311
307
if (!I.checkNotTautological (SF)) {
@@ -343,7 +339,7 @@ void NameBinder::bindImport(UnboundImport &&I) {
343
339
ID.get ()->setModule (M);
344
340
}
345
341
346
- void NameBinder ::addImport (const UnboundImport &I, ModuleDecl *M) {
342
+ void ImportResolver ::addImport (const UnboundImport &I, ModuleDecl *M) {
347
343
auto importDesc = I.makeDesc (M);
348
344
addVisibleModules (importDesc);
349
345
boundImports.push_back (importDesc);
@@ -353,7 +349,8 @@ void NameBinder::addImport(const UnboundImport &I, ModuleDecl *M) {
353
349
// MARK: Import module loading
354
350
// ===----------------------------------------------------------------------===//
355
351
356
- ModuleDecl *NameBinder::getModule (ArrayRef<Located<Identifier>> modulePath) {
352
+ ModuleDecl *
353
+ ImportResolver::getModule (ArrayRef<Located<Identifier>> modulePath) {
357
354
assert (!modulePath.empty ());
358
355
auto moduleID = modulePath[0 ];
359
356
@@ -667,7 +664,7 @@ ScopedImportLookupRequest::evaluate(Evaluator &evaluator,
667
664
668
665
// If we weren't able to load the module referenced by the import, we're done.
669
666
// The fact that we failed to load the module has already been diagnosed by
670
- // name binding .
667
+ // import resolution .
671
668
auto *module = import->getModule ();
672
669
if (!module)
673
670
return ArrayRef<ValueDecl *>();
@@ -676,8 +673,8 @@ ScopedImportLookupRequest::evaluate(Evaluator &evaluator,
676
673
// /
677
674
// / We validate the scope by making sure that the named declaration exists
678
675
// / 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.
681
678
auto &ctx = module->getASTContext ();
682
679
auto declPath = import->getDeclPath ();
683
680
auto modulePath = import->getModulePath ();
@@ -789,16 +786,16 @@ UnboundImport::UnboundImport(ASTContext &ctx,
789
786
options |= ImportFlags::ImplementationOnly;
790
787
}
791
788
792
- void NameBinder ::crossImport (ModuleDecl *M, UnboundImport &I) {
789
+ void ImportResolver ::crossImport (ModuleDecl *M, UnboundImport &I) {
793
790
// FIXME: There is a fundamental problem with this find-as-we-go approach:
794
791
// The '@_exported import'-ed modules in this module's other files should be
795
792
// taken into account, but they haven't been bound yet, and binding them would
796
793
// require cross-importing. Chicken, meet egg.
797
794
//
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.
802
799
//
803
800
// Fixing this is tracked within Apple by rdar://problem/59527118. I haven't
804
801
// 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) {
855
852
nextModuleToCrossImport = crossImportableModules.size ();
856
853
}
857
854
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) {
863
859
for (auto &declaringImport : declaring) {
864
860
if (!canCrossImport (declaringImport))
865
861
continue ;
@@ -874,10 +870,10 @@ NameBinder::findCrossImportsInLists(UnboundImport &I,
874
870
}
875
871
}
876
872
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) {
881
877
assert (&declaringImport != &bystandingImport);
882
878
883
879
LLVM_DEBUG (
@@ -935,7 +931,7 @@ static bool isSubmodule(ModuleDecl* M) {
935
931
return clangMod && clangMod->Parent ;
936
932
}
937
933
938
- void NameBinder ::addVisibleModules (ImportedModuleDesc importDesc) {
934
+ void ImportResolver ::addVisibleModules (ImportedModuleDesc importDesc) {
939
935
// FIXME: namelookup::getAllImports() doesn't quite do what we need (mainly
940
936
// w.r.t. scoped imports), but it seems like we could extend it to do so, and
941
937
// then eliminate most of this.
0 commit comments