forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModuleNameLookup.cpp
302 lines (270 loc) · 11.8 KB
/
ModuleNameLookup.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//===--- ModuleNameLookup.cpp - Name lookup within a module ----*- c++ -*--===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "NameLookupImpl.h"
#include "swift/AST/NameLookup.h"
#include "swift/AST/AST.h"
#include "swift/AST/LazyResolver.h"
#include "llvm/Support/raw_ostream.h"
using namespace swift;
using namespace namelookup;
namespace {
using ModuleLookupCache = llvm::SmallDenseMap<Module::ImportedModule,
TinyPtrVector<ValueDecl *>,
32>;
class SortCanType {
public:
bool operator()(CanType lhs, CanType rhs) const {
return std::less<TypeBase *>()(lhs.getPointer(), rhs.getPointer());
}
};
using CanTypeSet = llvm::SmallSet<CanType, 4, SortCanType>;
using NamedCanTypeSet =
llvm::DenseMap<Identifier, std::pair<ResolutionKind, CanTypeSet>>;
static_assert(ResolutionKind() == ResolutionKind::Overloadable,
"Entries in NamedCanTypeSet should be overloadable initially");
} // end anonymous namespace
/// Returns true if this particular ValueDecl is overloadable.
static bool isOverloadable(const ValueDecl *VD) {
return isa<FuncDecl>(VD) ||
isa<ConstructorDecl>(VD) ||
isa<SubscriptDecl>(VD);
}
static bool isValidOverload(CanTypeSet &overloads, const ValueDecl *VD) {
if (!isOverloadable(VD))
return overloads.empty();
if (overloads.count(VD->getType()->getCanonicalType()))
return false;
return true;
}
static bool isValidOverload(NamedCanTypeSet &overloads, const ValueDecl *VD) {
auto &entry = overloads[VD->getName()];
if (entry.first != ResolutionKind::Overloadable)
return false;
return isValidOverload(entry.second, VD);
}
/// Updates \p overloads with the types of the given decls.
///
/// \returns true if all of the given decls are overloadable, false if not.
static bool updateOverloadSet(CanTypeSet &overloads,
ArrayRef<ValueDecl *> decls) {
for (auto result : decls) {
if (!isOverloadable(result))
return false;
if (!result->hasType())
continue;
overloads.insert(result->getType()->getCanonicalType());
}
return true;
}
/// Updates \p overloads with the types of the given decls.
///
/// \returns true, since there can always be more overloadable decls.
static bool updateOverloadSet(NamedCanTypeSet &overloads,
ArrayRef<ValueDecl *> decls) {
for (auto result : decls) {
auto &entry = overloads[result->getName()];
if (!isOverloadable(result))
entry.first = ResolutionKind::Exact;
else if (!result->hasType())
continue;
else
entry.second.insert(result->getType()->getCanonicalType());
}
return true;
}
/// After finding decls by name lookup, filter based on the given
/// resolution kind and existing overload set and add them to \p results.
template <typename OverloadSetTy>
static ResolutionKind recordImportDecls(LazyResolver *typeResolver,
SmallVectorImpl<ValueDecl *> &results,
ArrayRef<ValueDecl *> newDecls,
OverloadSetTy &overloads,
ResolutionKind resolutionKind) {
switch (resolutionKind) {
case ResolutionKind::Overloadable: {
// Add new decls if they provide a new overload. Note that the new decls
// may be ambiguous with respect to each other, just not any existing decls.
std::copy_if(newDecls.begin(), newDecls.end(), std::back_inserter(results),
[&](ValueDecl *result) -> bool {
if (!result->hasType()) {
if (typeResolver)
typeResolver->resolveDeclSignature(result);
else
return true;
}
return isValidOverload(overloads, result);
});
// Update the overload set.
bool stillOverloadable = updateOverloadSet(overloads, newDecls);
return stillOverloadable ? ResolutionKind::Overloadable
: ResolutionKind::Exact;
}
case ResolutionKind::Exact:
// Add all decls. If they're ambiguous, they're ambiguous.
results.append(newDecls.begin(), newDecls.end());
return ResolutionKind::Exact;
case ResolutionKind::TypesOnly:
// Add type decls only. If they're ambiguous, they're ambiguous.
std::copy_if(newDecls.begin(), newDecls.end(), std::back_inserter(results),
[](const ValueDecl *VD) { return isa<TypeDecl>(VD); });
return ResolutionKind::TypesOnly;
}
llvm_unreachable("bad ResolutionKind");
}
/// Performs a qualified lookup into the given module and, if necessary, its
/// reexports, observing proper shadowing rules.
template <typename OverloadSetTy, typename CallbackTy>
static void lookupInModule(Module *module, Module::AccessPathTy accessPath,
SmallVectorImpl<ValueDecl *> &decls,
ResolutionKind resolutionKind, bool canReturnEarly,
LazyResolver *typeResolver,
ModuleLookupCache &cache,
const DeclContext *moduleScopeContext,
bool respectAccessControl,
ArrayRef<Module::ImportedModule> extraImports,
CallbackTy callback) {
assert(module);
assert(std::none_of(extraImports.begin(), extraImports.end(),
[](Module::ImportedModule import) -> bool {
return !import.second;
}));
ModuleLookupCache::iterator iter;
bool isNew;
std::tie(iter, isNew) = cache.insert({{accessPath, module}, {}});
if (!isNew) {
decls.append(iter->second.begin(), iter->second.end());
return;
}
size_t initialCount = decls.size();
SmallVector<ValueDecl *, 4> localDecls;
callback(module, accessPath, localDecls);
if (respectAccessControl) {
auto newEndIter = std::remove_if(localDecls.begin(), localDecls.end(),
[=](ValueDecl *VD) {
if (typeResolver) {
typeResolver->resolveAccessibility(VD);
}
if (!VD->hasAccessibility())
return false;
return !VD->isAccessibleFrom(moduleScopeContext);
});
localDecls.erase(newEndIter, localDecls.end());
// This only applies to immediate imports of the top-level module.
if (moduleScopeContext && moduleScopeContext->getParentModule() != module)
moduleScopeContext = nullptr;
}
OverloadSetTy overloads;
resolutionKind = recordImportDecls(typeResolver, decls, localDecls,
overloads, resolutionKind);
bool foundDecls = decls.size() > initialCount;
if (!foundDecls || !canReturnEarly ||
resolutionKind == ResolutionKind::Overloadable) {
SmallVector<Module::ImportedModule, 8> reexports;
module->getImportedModulesForLookup(reexports);
assert(std::none_of(reexports.begin(), reexports.end(),
[](Module::ImportedModule import) -> bool {
return !import.second;
}));
reexports.append(extraImports.begin(), extraImports.end());
// Prefer scoped imports (import func Swift.max) to whole-module imports.
SmallVector<ValueDecl *, 8> unscopedValues;
SmallVector<ValueDecl *, 8> scopedValues;
for (auto next : reexports) {
// Filter any whole-module imports, and skip specific-decl imports if the
// import path doesn't match exactly.
Module::AccessPathTy combinedAccessPath;
if (accessPath.empty()) {
combinedAccessPath = next.first;
} else if (!next.first.empty() &&
!Module::isSameAccessPath(next.first, accessPath)) {
// If we ever allow importing non-top-level decls, it's possible the
// rule above isn't what we want.
assert(next.first.size() == 1 && "import of non-top-level decl");
continue;
} else {
combinedAccessPath = accessPath;
}
auto &resultSet = next.first.empty() ? unscopedValues : scopedValues;
lookupInModule<OverloadSetTy>(next.second, combinedAccessPath,
resultSet, resolutionKind, canReturnEarly,
typeResolver, cache, moduleScopeContext,
respectAccessControl, {}, callback);
}
// Add the results from scoped imports.
resolutionKind = recordImportDecls(typeResolver, decls, scopedValues,
overloads, resolutionKind);
// Add the results from unscoped imports.
foundDecls = decls.size() > initialCount;
if (!foundDecls || !canReturnEarly ||
resolutionKind == ResolutionKind::Overloadable) {
resolutionKind = recordImportDecls(typeResolver, decls, unscopedValues,
overloads, resolutionKind);
}
}
// Remove duplicated declarations.
llvm::SmallPtrSet<ValueDecl *, 4> knownDecls;
decls.erase(std::remove_if(decls.begin() + initialCount, decls.end(),
[&](ValueDecl *d) -> bool {
return !knownDecls.insert(d).second;
}),
decls.end());
auto &cachedValues = cache[{accessPath, module}];
cachedValues.insert(cachedValues.end(),
decls.begin() + initialCount,
decls.end());
}
void namelookup::lookupInModule(Module *startModule,
Module::AccessPathTy topAccessPath,
DeclName name,
SmallVectorImpl<ValueDecl *> &decls,
NLKind lookupKind,
ResolutionKind resolutionKind,
LazyResolver *typeResolver,
const DeclContext *moduleScopeContext,
ArrayRef<Module::ImportedModule> extraImports) {
assert(moduleScopeContext && moduleScopeContext->isModuleScopeContext());
ModuleLookupCache cache;
bool respectAccessControl = startModule->getASTContext().LangOpts
.EnableAccessControl;
::lookupInModule<CanTypeSet>(startModule, topAccessPath, decls,
resolutionKind, /*canReturnEarly=*/true,
typeResolver, cache, moduleScopeContext,
respectAccessControl, extraImports,
[=](Module *module, Module::AccessPathTy path,
SmallVectorImpl<ValueDecl *> &localDecls) {
module->lookupValue(path, name, lookupKind, localDecls);
}
);
}
void namelookup::lookupVisibleDeclsInModule(
Module *M,
Module::AccessPathTy accessPath,
SmallVectorImpl<ValueDecl *> &decls,
NLKind lookupKind,
ResolutionKind resolutionKind,
LazyResolver *typeResolver,
const DeclContext *moduleScopeContext,
ArrayRef<Module::ImportedModule> extraImports) {
assert(moduleScopeContext && moduleScopeContext->isModuleScopeContext());
ModuleLookupCache cache;
bool respectAccessControl = M->getASTContext().LangOpts.EnableAccessControl;
::lookupInModule<NamedCanTypeSet>(M, accessPath, decls,
resolutionKind, /*canReturnEarly=*/false,
typeResolver, cache, moduleScopeContext,
respectAccessControl, extraImports,
[=](Module *module, Module::AccessPathTy path,
SmallVectorImpl<ValueDecl *> &localDecls) {
VectorDeclConsumer consumer(localDecls);
module->lookupVisibleDecls(path, consumer, lookupKind);
}
);
}