Skip to content

Commit 5592d3c

Browse files
committed
Sema: Change lookupUnqualifiedType() to return a LookupResult
This will allow callers of lookupUnqualifiedType() to know what DeclContext a lookup result came from.
1 parent 7db4c90 commit 5592d3c

File tree

4 files changed

+34
-27
lines changed

4 files changed

+34
-27
lines changed

lib/Sema/TypeCheckAttr.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1204,7 +1204,8 @@ void AttributeChecker::checkApplicationMainAttribute(DeclAttribute *attr,
12041204
SourceLoc(),
12051205
lookupOptions);
12061206
if (lookup.size() == 1)
1207-
ApplicationDelegateProto = dyn_cast<ProtocolDecl>(lookup[0]);
1207+
ApplicationDelegateProto = dyn_cast<ProtocolDecl>(
1208+
lookup[0].getValueDecl());
12081209
}
12091210

12101211
if (!ApplicationDelegateProto ||

lib/Sema/TypeCheckNameLookup.cpp

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -204,43 +204,42 @@ LookupResult TypeChecker::lookupUnqualified(DeclContext *dc, DeclName name,
204204
return result;
205205
}
206206

207-
SmallVector<TypeDecl *, 1>
207+
LookupResult
208208
TypeChecker::lookupUnqualifiedType(DeclContext *dc, DeclName name,
209209
SourceLoc loc,
210210
NameLookupOptions options) {
211-
SmallVector<TypeDecl *, 1> decls;
212-
213-
// Try lookup without ProtocolMembers first.
214-
UnqualifiedLookup lookup(
211+
{
212+
// Try lookup without ProtocolMembers first.
213+
UnqualifiedLookup lookup(
215214
name, dc, this,
216-
options.contains(NameLookupFlags::KnownPrivate),
217-
loc,
218-
/*IsTypeLookup=*/true,
219-
/*AllowProtocolMembers=*/false,
220-
options.contains(NameLookupFlags::IgnoreAccessibility));
221-
for (auto found : lookup.Results)
222-
decls.push_back(cast<TypeDecl>(found.getValueDecl()));
215+
options.contains(NameLookupFlags::KnownPrivate),
216+
loc,
217+
/*IsTypeLookup=*/true,
218+
/*AllowProtocolMembers=*/false,
219+
options.contains(NameLookupFlags::IgnoreAccessibility));
223220

224-
if (decls.empty() &&
225-
options.contains(NameLookupFlags::ProtocolMembers)) {
221+
if (!lookup.Results.empty() ||
222+
!options.contains(NameLookupFlags::ProtocolMembers)) {
223+
return LookupResult(lookup.Results);
224+
}
225+
}
226+
227+
{
226228
// Try again, this time with protocol members.
227229
//
228230
// FIXME: Fix the problem where if NominalTypeDecl::getAllProtocols()
229231
// is called too early, we start resolving extensions -- even those
230-
// which do provide conformances.
232+
// which do provide not conformances.
231233
UnqualifiedLookup lookup(
232-
name, dc, this,
234+
name, dc, this,
233235
options.contains(NameLookupFlags::KnownPrivate),
234236
loc,
235237
/*IsTypeLookup=*/true,
236238
/*AllowProtocolMembers=*/true,
237239
options.contains(NameLookupFlags::IgnoreAccessibility));
238240

239-
for (auto found : lookup.Results)
240-
decls.push_back(cast<TypeDecl>(found.getValueDecl()));
241+
return LookupResult(lookup.Results);
241242
}
242-
243-
return decls;
244243
}
245244

246245
LookupResult TypeChecker::lookupMember(DeclContext *dc,

lib/Sema/TypeCheckType.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -900,14 +900,14 @@ static Type diagnoseUnknownType(TypeChecker &tc, DeclContext *dc,
900900
relookupOptions);
901901
if (!inaccessibleResults.empty()) {
902902
// FIXME: What if the unviable candidates have different levels of access?
903-
auto first = cast<TypeDecl>(inaccessibleResults.front());
903+
auto first = cast<TypeDecl>(inaccessibleResults.front().getValueDecl());
904904
tc.diagnose(comp->getIdLoc(), diag::candidate_inaccessible,
905905
comp->getIdentifier(), first->getFormalAccess());
906906

907907
// FIXME: If any of the candidates (usually just one) are in the same
908908
// module we could offer a fix-it.
909909
for (auto lookupResult : inaccessibleResults)
910-
tc.diagnose(lookupResult, diag::type_declared_here);
910+
tc.diagnose(lookupResult.getValueDecl(), diag::type_declared_here);
911911

912912
// Don't try to recover here; we'll get more access-related diagnostics
913913
// downstream if we do.
@@ -1202,7 +1202,8 @@ resolveTopLevelIdentTypeComponent(TypeChecker &TC, DeclContext *DC,
12021202
Type current;
12031203
TypeDecl *currentDecl = nullptr;
12041204
bool isAmbiguous = false;
1205-
for (const auto &typeDecl : globals) {
1205+
for (const auto entry : globals) {
1206+
auto *typeDecl = cast<TypeDecl>(entry.getValueDecl());
12061207

12071208
// If necessary, add delayed members to the declaration.
12081209
if (auto nomDecl = dyn_cast<NominalTypeDecl>(typeDecl)) {
@@ -1243,8 +1244,8 @@ resolveTopLevelIdentTypeComponent(TypeChecker &TC, DeclContext *DC,
12431244
TC.diagnose(comp->getIdLoc(), diag::ambiguous_type_base,
12441245
comp->getIdentifier())
12451246
.highlight(comp->getIdLoc());
1246-
for (auto typeDecl : globals) {
1247-
TC.diagnose(typeDecl, diag::found_candidate);
1247+
for (auto entry : globals) {
1248+
TC.diagnose(entry.getValueDecl(), diag::found_candidate);
12481249
}
12491250
}
12501251

lib/Sema/TypeChecker.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ class LookupResult {
8787
SmallVector<LookupResultEntry, 4> Results;
8888

8989
public:
90+
LookupResult() {}
91+
92+
explicit
93+
LookupResult(const SmallVectorImpl<LookupResultEntry> &Results)
94+
: Results(Results.begin(), Results.end()) {}
95+
9096
typedef SmallVectorImpl<LookupResultEntry>::iterator iterator;
9197
iterator begin() { return Results.begin(); }
9298
iterator end() { return Results.end(); }
@@ -2012,7 +2018,7 @@ class TypeChecker final : public LazyResolver {
20122018
/// \param name The name of the entity to look for.
20132019
/// \param loc The source location at which name lookup occurs.
20142020
/// \param options Options that control name lookup.
2015-
SmallVector<TypeDecl *, 1>
2021+
LookupResult
20162022
lookupUnqualifiedType(DeclContext *dc, DeclName name, SourceLoc loc,
20172023
NameLookupOptions options
20182024
= defaultUnqualifiedLookupOptions);

0 commit comments

Comments
 (0)