Skip to content

Commit c450b45

Browse files
committed
AST: Clean up a couple of uses of UnqualifiedLookup
We can use type lookup in more places, and switch a couple of callers to use qualified lookup instead.
1 parent 0dda779 commit c450b45

File tree

3 files changed

+37
-24
lines changed

3 files changed

+37
-24
lines changed

lib/AST/ASTContext.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -868,15 +868,14 @@ StructDecl *ASTContext::getObjCBoolDecl() const {
868868
ClassDecl *ASTContext::get##NAME##Decl() const { \
869869
if (!getImpl().NAME##Decl) { \
870870
if (ModuleDecl *M = getLoadedModule(Id_Foundation)) { \
871-
/* Note: use unqualified lookup so we find NSError regardless of */ \
872-
/* whether it's defined in the Foundation module or the Clang */ \
873-
/* Foundation module it imports. */ \
874-
UnqualifiedLookup lookup(getIdentifier(#NAME), M); \
875-
if (auto type = lookup.getSingleTypeResult()) { \
876-
if (auto classDecl = dyn_cast<ClassDecl>(type)) { \
877-
if (classDecl->getGenericParams() == nullptr) { \
878-
getImpl().NAME##Decl = classDecl; \
879-
} \
871+
/* Note: lookupQualified() will search both the Foundation module \
872+
* and the Clang Foundation module it imports. */ \
873+
SmallVector<ValueDecl *, 1> decls; \
874+
M->lookupQualified(M, getIdentifier(#NAME), NL_OnlyTypes, decls); \
875+
if (decls.size() == 1 && isa<ClassDecl>(decls[0])) { \
876+
auto classDecl = cast<ClassDecl>(decls[0]); \
877+
if (classDecl->getGenericParams() == nullptr) { \
878+
getImpl().NAME##Decl = classDecl; \
880879
} \
881880
} \
882881
} \

lib/ParseSIL/ParseSIL.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,14 +1170,19 @@ bool SILParser::performTypeLocChecking(TypeLoc &T, bool IsSILType,
11701170

11711171
/// Find the top-level ValueDecl or Module given a name.
11721172
static llvm::PointerUnion<ValueDecl *, ModuleDecl *>
1173-
lookupTopDecl(Parser &P, DeclBaseName Name) {
1173+
lookupTopDecl(Parser &P, DeclBaseName Name, bool typeLookup) {
11741174
// Use UnqualifiedLookup to look through all of the imports.
11751175
// We have to lie and say we're done with parsing to make this happen.
11761176
assert(P.SF.ASTStage == SourceFile::Parsing &&
11771177
"Unexpected stage during parsing!");
11781178
llvm::SaveAndRestore<SourceFile::ASTStage_t> ASTStage(P.SF.ASTStage,
11791179
SourceFile::Parsed);
1180-
UnqualifiedLookup DeclLookup(Name, &P.SF);
1180+
1181+
UnqualifiedLookup::Options options;
1182+
if (typeLookup)
1183+
options |= UnqualifiedLookup::Flags::TypeLookup;
1184+
1185+
UnqualifiedLookup DeclLookup(Name, &P.SF, SourceLoc(), options);
11811186
assert(DeclLookup.isSuccess() && DeclLookup.Results.size() == 1);
11821187
ValueDecl *VD = DeclLookup.Results.back().getValueDecl();
11831188
return VD;
@@ -1358,9 +1363,11 @@ bool SILParser::parseSILDottedPathWithoutPound(ValueDecl *&Decl,
13581363
}
13591364
} while (P.consumeIf(tok::period));
13601365

1361-
// Look up ValueDecl from a dotted path.
1366+
// Look up ValueDecl from a dotted path. If there are multiple components,
1367+
// the first one must be a type declaration.
13621368
ValueDecl *VD;
1363-
llvm::PointerUnion<ValueDecl*, ModuleDecl *> Res = lookupTopDecl(P, FullName[0]);
1369+
llvm::PointerUnion<ValueDecl*, ModuleDecl *> Res = lookupTopDecl(
1370+
P, FullName[0], /*typeLookup=*/FullName.size() > 1);
13641371
// It is possible that the last member lookup can return multiple lookup
13651372
// results. One example is the overloaded member functions.
13661373
if (Res.is<ModuleDecl*>()) {
@@ -5748,7 +5755,8 @@ bool SILParserTUState::parseSILVTable(Parser &P) {
57485755
return true;
57495756

57505757
// Find the class decl.
5751-
llvm::PointerUnion<ValueDecl*, ModuleDecl *> Res = lookupTopDecl(P, Name);
5758+
llvm::PointerUnion<ValueDecl*, ModuleDecl *> Res =
5759+
lookupTopDecl(P, Name, /*typeLookup=*/true);
57525760
assert(Res.is<ValueDecl*>() && "Class look-up should return a Decl");
57535761
ValueDecl *VD = Res.get<ValueDecl*>();
57545762
if (!VD) {
@@ -5835,7 +5843,8 @@ static ProtocolDecl *parseProtocolDecl(Parser &P, SILParser &SP) {
58355843
return nullptr;
58365844

58375845
// Find the protocol decl. The protocol can be imported.
5838-
llvm::PointerUnion<ValueDecl*, ModuleDecl *> Res = lookupTopDecl(P, DeclName);
5846+
llvm::PointerUnion<ValueDecl*, ModuleDecl *> Res =
5847+
lookupTopDecl(P, DeclName, /*typeLookup=*/true);
58395848
assert(Res.is<ValueDecl*>() && "Protocol look-up should return a Decl");
58405849
ValueDecl *VD = Res.get<ValueDecl*>();
58415850
if (!VD) {

lib/PrintAsObjC/PrintAsObjC.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -950,11 +950,14 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
950950
if (!renamedParsedDeclName.ContextName.empty()) {
951951
return nullptr;
952952
}
953-
UnqualifiedLookup lookup(renamedDeclName.getBaseIdentifier(),
954-
declContext->getModuleScopeContext(),
955-
SourceLoc(),
956-
UnqualifiedLookup::Flags::TypeLookup);
957-
return lookup.getSingleTypeResult();
953+
SmallVector<ValueDecl *, 1> decls;
954+
declContext->lookupQualified(declContext->getParentModule(),
955+
renamedDeclName.getBaseIdentifier(),
956+
NL_OnlyTypes,
957+
decls);
958+
if (decls.size() == 1)
959+
return decls[0];
960+
return nullptr;
958961
}
959962

960963
TypeDecl *typeDecl = declContext->getSelfNominalTypeDecl();
@@ -1439,10 +1442,12 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
14391442
isNSObjectOrAnyHashable(ctx, typeArgs[0])) {
14401443
if (ModuleDecl *M = ctx.getLoadedModule(ctx.Id_Foundation)) {
14411444
if (!NSCopyingType) {
1442-
UnqualifiedLookup lookup(ctx.getIdentifier("NSCopying"), M);
1443-
auto type = lookup.getSingleTypeResult();
1444-
if (type && isa<ProtocolDecl>(type)) {
1445-
NSCopyingType = type->getDeclaredInterfaceType();
1445+
SmallVector<ValueDecl *, 1> decls;
1446+
M->lookupQualified(M, ctx.getIdentifier("NSCopying"),
1447+
NL_OnlyTypes, decls);
1448+
if (decls.size() == 1 && isa<ProtocolDecl>(decls[0])) {
1449+
NSCopyingType = cast<ProtocolDecl>(decls[0])
1450+
->getDeclaredInterfaceType();
14461451
} else {
14471452
NSCopyingType = Type();
14481453
}

0 commit comments

Comments
 (0)