Skip to content

Commit 6097df7

Browse files
committed
[NameLookup] Plumb source location arguments through all name lookup APIs.
This source location will be used to determine whether to add a name lookup option to exclude macro expansions when the name lookup request is constructed. Currently, the source location argument is unused. (cherry picked from commit cd752cc)
1 parent 68b8c69 commit 6097df7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+174
-105
lines changed

include/swift/AST/Decl.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3899,7 +3899,7 @@ class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {
38993899
/// protocols to which the nominal type conforms. Furthermore, the resulting
39003900
/// set of declarations has not been filtered for visibility, nor have
39013901
/// overridden declarations been removed.
3902-
TinyPtrVector<ValueDecl *> lookupDirect(DeclName name,
3902+
TinyPtrVector<ValueDecl *> lookupDirect(DeclName name, SourceLoc loc = SourceLoc(),
39033903
OptionSet<LookupDirectFlags> flags =
39043904
OptionSet<LookupDirectFlags>());
39053905

@@ -4454,7 +4454,8 @@ class ClassDecl final : public NominalTypeDecl {
44544454
// Force loading all the members, which will add this attribute if any of
44554455
// members are determined to be missing while loading.
44564456
auto mutableThis = const_cast<ClassDecl *>(this);
4457-
(void)mutableThis->lookupDirect(DeclBaseName::createConstructor());
4457+
(void)mutableThis->lookupDirect(DeclBaseName::createConstructor(),
4458+
getStartLoc());
44584459
}
44594460

44604461
if (Bits.ClassDecl.ComputedHasMissingDesignatedInitializers)

include/swift/AST/DeclContext.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,8 @@ class alignas(1 << DeclContextAlignInBits) DeclContext
598598
/// lookup.
599599
///
600600
/// \returns true if anything was found.
601-
bool lookupQualified(Type type, DeclNameRef member, NLOptions options,
601+
bool lookupQualified(Type type, DeclNameRef member,
602+
SourceLoc loc, NLOptions options,
602603
SmallVectorImpl<ValueDecl *> &decls) const;
603604

604605
/// Look for the set of declarations with the given name within the
@@ -616,12 +617,12 @@ class alignas(1 << DeclContextAlignInBits) DeclContext
616617
///
617618
/// \returns true if anything was found.
618619
bool lookupQualified(ArrayRef<NominalTypeDecl *> types, DeclNameRef member,
619-
NLOptions options,
620+
SourceLoc loc, NLOptions options,
620621
SmallVectorImpl<ValueDecl *> &decls) const;
621622

622623
/// Perform qualified lookup for the given member in the given module.
623624
bool lookupQualified(ModuleDecl *module, DeclNameRef member,
624-
NLOptions options,
625+
SourceLoc loc, NLOptions options,
625626
SmallVectorImpl<ValueDecl *> &decls) const;
626627

627628
/// Look up all Objective-C methods with the given selector visible

include/swift/AST/ModuleNameLookup.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void lookupInModule(const DeclContext *moduleOrFile,
6262
DeclName name, SmallVectorImpl<ValueDecl *> &decls,
6363
NLKind lookupKind, ResolutionKind resolutionKind,
6464
const DeclContext *moduleScopeContext,
65-
NLOptions options);
65+
SourceLoc loc, NLOptions options);
6666

6767
/// Performs a qualified lookup into the given module and, if necessary, its
6868
/// reexports, observing proper shadowing rules.

include/swift/AST/NameLookupRequests.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ class LookupInModuleRequest
461461
LookupInModuleRequest(
462462
const DeclContext *, DeclName, NLKind,
463463
namelookup::ResolutionKind, const DeclContext *,
464-
NLOptions);
464+
SourceLoc, NLOptions);
465465

466466
private:
467467
friend SimpleRequest;
@@ -511,7 +511,7 @@ class ModuleQualifiedLookupRequest
511511
public:
512512
ModuleQualifiedLookupRequest(const DeclContext *,
513513
ModuleDecl *, DeclNameRef,
514-
NLOptions);
514+
SourceLoc, NLOptions);
515515

516516
private:
517517
friend SimpleRequest;
@@ -537,7 +537,7 @@ class QualifiedLookupRequest
537537
public:
538538
QualifiedLookupRequest(const DeclContext *,
539539
SmallVector<NominalTypeDecl *, 4>,
540-
DeclNameRef, NLOptions);
540+
DeclNameRef, SourceLoc, NLOptions);
541541

542542
private:
543543
friend SimpleRequest;
@@ -588,7 +588,7 @@ class DirectLookupRequest
588588
TinyPtrVector<ValueDecl *>(DirectLookupDescriptor),
589589
RequestFlags::Uncached|RequestFlags::DependencySink> {
590590
public:
591-
DirectLookupRequest(DirectLookupDescriptor);
591+
DirectLookupRequest(DirectLookupDescriptor, SourceLoc);
592592

593593
private:
594594
friend SimpleRequest;

include/swift/Sema/ConstraintSystem.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2910,7 +2910,8 @@ class ConstraintSystem {
29102910
/// and no new names are introduced after that point.
29112911
///
29122912
/// \returns A reference to the member-lookup result.
2913-
LookupResult &lookupMember(Type base, DeclNameRef name);
2913+
LookupResult &lookupMember(Type base, DeclNameRef name,
2914+
SourceLoc loc);
29142915

29152916
/// Retrieve the set of "alternative" literal types that we'll explore
29162917
/// for a given literal protocol kind.

lib/AST/ASTContext.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,8 +1058,8 @@ DECLTYPE *ASTContext::get##NAME##Decl() const { \
10581058
/* Note: lookupQualified() will search both the Swift overlay \
10591059
* and the Clang module it imports. */ \
10601060
SmallVector<ValueDecl *, 1> decls; \
1061-
M->lookupQualified(M, DeclNameRef(getIdentifier(#NAME)), NL_OnlyTypes, \
1062-
decls); \
1061+
M->lookupQualified(M, DeclNameRef(getIdentifier(#NAME)), SourceLoc(), \
1062+
NL_OnlyTypes, decls); \
10631063
if (decls.size() == 1 && isa<DECLTYPE>(decls[0])) { \
10641064
auto decl = cast<DECLTYPE>(decls[0]); \
10651065
if (isa<ProtocolDecl>(decl) \
@@ -1322,7 +1322,8 @@ ConcreteDeclRef ASTContext::getRegexInitDecl(Type regexType) const {
13221322
{Id_regexString, Id_version});
13231323
SmallVector<ValueDecl *, 1> results;
13241324
spModule->lookupQualified(getRegexType(), DeclNameRef(name),
1325-
NL_IncludeUsableFromInline, results);
1325+
SourceLoc(), NL_IncludeUsableFromInline,
1326+
results);
13261327
assert(results.size() == 1);
13271328
auto *foundDecl = cast<ConstructorDecl>(results[0]);
13281329
auto subs = regexType->getMemberSubstitutionMap(spModule, foundDecl);

lib/AST/Decl.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5386,7 +5386,7 @@ NominalTypeDecl::getExecutorOwnedEnqueueFunction() const {
53865386
llvm::SmallVector<ValueDecl *, 2> results;
53875387
lookupQualified(getSelfNominalTypeDecl(),
53885388
DeclNameRef(C.Id_enqueue),
5389-
NL_ProtocolMembers,
5389+
getLoc(), NL_ProtocolMembers,
53905390
results);
53915391

53925392
for (auto candidate: results) {
@@ -5425,7 +5425,7 @@ NominalTypeDecl::getExecutorLegacyOwnedEnqueueFunction() const {
54255425
llvm::SmallVector<ValueDecl *, 2> results;
54265426
lookupQualified(getSelfNominalTypeDecl(),
54275427
DeclNameRef(C.Id_enqueue),
5428-
NL_ProtocolMembers,
5428+
getLoc(), NL_ProtocolMembers,
54295429
results);
54305430

54315431
for (auto candidate: results) {
@@ -5464,7 +5464,7 @@ NominalTypeDecl::getExecutorLegacyUnownedEnqueueFunction() const {
54645464
llvm::SmallVector<ValueDecl *, 2> results;
54655465
lookupQualified(getSelfNominalTypeDecl(),
54665466
DeclNameRef(C.Id_enqueue),
5467-
NL_ProtocolMembers,
5467+
getLoc(), NL_ProtocolMembers,
54685468
results);
54695469

54705470
for (auto candidate: results) {
@@ -9918,7 +9918,7 @@ const VarDecl *ClassDecl::getUnownedExecutorProperty() const {
99189918
llvm::SmallVector<ValueDecl *, 2> results;
99199919
this->lookupQualified(getSelfNominalTypeDecl(),
99209920
DeclNameRef(C.Id_unownedExecutor),
9921-
NL_ProtocolMembers,
9921+
getLoc(), NL_ProtocolMembers,
99229922
results);
99239923

99249924
for (auto candidate: results) {

lib/AST/DocComment.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,8 @@ class CommentProviderFinder final {
435435
SmallVector<ValueDecl *, 2> members;
436436
protocol->lookupQualified(const_cast<ProtocolDecl *>(protocol),
437437
DeclNameRef(VD->getName()),
438-
NLOptions::NL_ProtocolMembers, members);
438+
VD->getLoc(), NLOptions::NL_ProtocolMembers,
439+
members);
439440

440441
Optional<ResultWithDecl> result;
441442
for (auto *member : members) {

lib/AST/ModuleNameLookup.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,10 @@ void namelookup::lookupInModule(const DeclContext *moduleOrFile,
281281
NLKind lookupKind,
282282
ResolutionKind resolutionKind,
283283
const DeclContext *moduleScopeContext,
284-
NLOptions options) {
284+
SourceLoc loc, NLOptions options) {
285285
auto &ctx = moduleOrFile->getASTContext();
286286
LookupInModuleRequest req(moduleOrFile, name, lookupKind, resolutionKind,
287-
moduleScopeContext, options);
287+
moduleScopeContext, loc, options);
288288
auto results = evaluateOrDefault(ctx.evaluator, req, {});
289289
decls.append(results.begin(), results.end());
290290
}

lib/AST/NameLookup.cpp

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,10 +1851,10 @@ maybeFilterOutUnwantedDecls(TinyPtrVector<ValueDecl *> decls,
18511851
}
18521852

18531853
TinyPtrVector<ValueDecl *>
1854-
NominalTypeDecl::lookupDirect(DeclName name,
1854+
NominalTypeDecl::lookupDirect(DeclName name, SourceLoc loc,
18551855
OptionSet<LookupDirectFlags> flags) {
18561856
return evaluateOrDefault(getASTContext().evaluator,
1857-
DirectLookupRequest({this, name, flags}), {});
1857+
DirectLookupRequest({this, name, flags}, loc), {});
18581858
}
18591859

18601860
TinyPtrVector<ValueDecl *>
@@ -2195,6 +2195,7 @@ void namelookup::tryExtractDirectlyReferencedNominalTypes(
21952195

21962196
bool DeclContext::lookupQualified(Type type,
21972197
DeclNameRef member,
2198+
SourceLoc loc,
21982199
NLOptions options,
21992200
SmallVectorImpl<ValueDecl *> &decls) const {
22002201
using namespace namelookup;
@@ -2209,14 +2210,16 @@ bool DeclContext::lookupQualified(Type type,
22092210

22102211
// Handle lookup in a module.
22112212
if (auto moduleTy = type->getAs<ModuleType>())
2212-
return lookupQualified(moduleTy->getModule(), member, options, decls);
2213+
return lookupQualified(moduleTy->getModule(), member,
2214+
loc, options, decls);
22132215

22142216
// Figure out which nominal types we will look into.
22152217
SmallVector<NominalTypeDecl *, 4> nominalTypesToLookInto;
22162218
namelookup::extractDirectlyReferencedNominalTypes(type,
22172219
nominalTypesToLookInto);
22182220

2219-
return lookupQualified(nominalTypesToLookInto, member, options, decls);
2221+
return lookupQualified(nominalTypesToLookInto, member,
2222+
loc, options, decls);
22202223
}
22212224

22222225
static void installPropertyWrapperMembersIfNeeded(NominalTypeDecl *target,
@@ -2251,11 +2254,11 @@ static void installPropertyWrapperMembersIfNeeded(NominalTypeDecl *target,
22512254

22522255
bool DeclContext::lookupQualified(ArrayRef<NominalTypeDecl *> typeDecls,
22532256
DeclNameRef member,
2254-
NLOptions options,
2257+
SourceLoc loc, NLOptions options,
22552258
SmallVectorImpl<ValueDecl *> &decls) const {
22562259
assert(decls.empty() && "additive lookup not supported");
22572260
QualifiedLookupRequest req{this, {typeDecls.begin(), typeDecls.end()},
2258-
member, options};
2261+
member, loc, options};
22592262
decls = evaluateOrDefault(getASTContext().evaluator, req, {});
22602263
return !decls.empty();
22612264
}
@@ -2309,7 +2312,11 @@ QualifiedLookupRequest::evaluate(Evaluator &eval, const DeclContext *DC,
23092312
flags |= NominalTypeDecl::LookupDirectFlags::IncludeAttrImplements;
23102313
if (options & NL_ExcludeMacroExpansions)
23112314
flags |= NominalTypeDecl::LookupDirectFlags::ExcludeMacroExpansions;
2312-
for (auto decl : current->lookupDirect(member.getFullName(), flags)) {
2315+
2316+
// Note that the source loc argument doesn't matter, because excluding
2317+
// macro expansions is already propagated through the lookup flags above.
2318+
for (auto decl : current->lookupDirect(member.getFullName(),
2319+
SourceLoc(), flags)) {
23132320
// If we're performing a type lookup, don't even attempt to validate
23142321
// the decl if its not a type.
23152322
if ((options & NL_OnlyTypes) && !isa<TypeDecl>(decl))
@@ -2383,10 +2390,10 @@ QualifiedLookupRequest::evaluate(Evaluator &eval, const DeclContext *DC,
23832390
}
23842391

23852392
bool DeclContext::lookupQualified(ModuleDecl *module, DeclNameRef member,
2386-
NLOptions options,
2393+
SourceLoc loc, NLOptions options,
23872394
SmallVectorImpl<ValueDecl *> &decls) const {
23882395
assert(decls.empty() && "additive lookup not supported");
2389-
ModuleQualifiedLookupRequest req{this, module, member, options};
2396+
ModuleQualifiedLookupRequest req{this, module, member, loc, options};
23902397
decls = evaluateOrDefault(getASTContext().evaluator, req, {});
23912398
return !decls.empty();
23922399
}
@@ -2404,7 +2411,7 @@ ModuleQualifiedLookupRequest::evaluate(Evaluator &eval, const DeclContext *DC,
24042411
auto topLevelScope = DC->getModuleScopeContext();
24052412
if (module == topLevelScope->getParentModule()) {
24062413
lookupInModule(module, member.getFullName(), decls, NLKind::QualifiedLookup,
2407-
kind, topLevelScope, options);
2414+
kind, topLevelScope, SourceLoc(), options);
24082415
} else {
24092416
// Note: This is a lookup into another module. Unless we're compiling
24102417
// multiple modules at once, or if the other module re-exports this one,
@@ -2421,7 +2428,7 @@ ModuleQualifiedLookupRequest::evaluate(Evaluator &eval, const DeclContext *DC,
24212428
})) {
24222429
lookupInModule(module, member.getFullName(), decls,
24232430
NLKind::QualifiedLookup, kind, topLevelScope,
2424-
options);
2431+
SourceLoc(), options);
24252432
}
24262433
}
24272434

@@ -2613,6 +2620,20 @@ directReferencesForUnqualifiedTypeLookup(DeclNameRef name,
26132620
SourceLoc loc, DeclContext *dc,
26142621
LookupOuterResults lookupOuter,
26152622
bool allowUsableFromInline=false) {
2623+
UnqualifiedLookupOptions options =
2624+
UnqualifiedLookupFlags::TypeLookup |
2625+
UnqualifiedLookupFlags::AllowProtocolMembers;
2626+
if (lookupOuter == LookupOuterResults::Included)
2627+
options |= UnqualifiedLookupFlags::IncludeOuterResults;
2628+
2629+
if (allowUsableFromInline)
2630+
options |= UnqualifiedLookupFlags::IncludeUsableFromInline;
2631+
2632+
// Manually exclude macro expansions here since the source location
2633+
// is overridden below.
2634+
if (ASTScope::isInMacroArgument(dc->getParentSourceFile(), loc))
2635+
options |= UnqualifiedLookupFlags::ExcludeMacroExpansions;
2636+
26162637
// In a protocol or protocol extension, the 'where' clause can refer to
26172638
// associated types without 'Self' qualification:
26182639
//
@@ -2640,15 +2661,6 @@ directReferencesForUnqualifiedTypeLookup(DeclNameRef name,
26402661

26412662
DirectlyReferencedTypeDecls results;
26422663

2643-
UnqualifiedLookupOptions options =
2644-
UnqualifiedLookupFlags::TypeLookup |
2645-
UnqualifiedLookupFlags::AllowProtocolMembers;
2646-
if (lookupOuter == LookupOuterResults::Included)
2647-
options |= UnqualifiedLookupFlags::IncludeOuterResults;
2648-
2649-
if (allowUsableFromInline)
2650-
options |= UnqualifiedLookupFlags::IncludeUsableFromInline;
2651-
26522664
auto &ctx = dc->getASTContext();
26532665
auto descriptor = UnqualifiedLookupDescriptor(name, dc, loc, options);
26542666
auto lookup = evaluateOrDefault(ctx.evaluator,
@@ -2679,6 +2691,7 @@ directReferencesForQualifiedTypeLookup(Evaluator &evaluator,
26792691
ArrayRef<TypeDecl *> baseTypes,
26802692
DeclNameRef name,
26812693
DeclContext *dc,
2694+
SourceLoc loc,
26822695
bool allowUsableFromInline=false) {
26832696
DirectlyReferencedTypeDecls result;
26842697
auto addResults = [&result](ArrayRef<ValueDecl *> found){
@@ -2705,15 +2718,15 @@ directReferencesForQualifiedTypeLookup(Evaluator &evaluator,
27052718
resolveTypeDeclsToNominal(ctx.evaluator, ctx, baseTypes, moduleDecls,
27062719
anyObject);
27072720

2708-
dc->lookupQualified(nominalTypeDecls, name, options, members);
2721+
dc->lookupQualified(nominalTypeDecls, name, loc, options, members);
27092722

27102723
// Search all of the modules.
27112724
for (auto module : moduleDecls) {
27122725
auto innerOptions = options;
27132726
innerOptions &= ~NL_RemoveOverridden;
27142727
innerOptions &= ~NL_RemoveNonVisible;
27152728
SmallVector<ValueDecl *, 4> moduleMembers;
2716-
dc->lookupQualified(module, name, innerOptions, moduleMembers);
2729+
dc->lookupQualified(module, name, loc, innerOptions, moduleMembers);
27172730
members.append(moduleMembers.begin(), moduleMembers.end());
27182731
}
27192732

@@ -2765,6 +2778,7 @@ directReferencesForDeclRefTypeRepr(Evaluator &evaluator, ASTContext &ctx,
27652778
current =
27662779
directReferencesForQualifiedTypeLookup(evaluator, ctx, current,
27672780
component->getNameRef(), dc,
2781+
component->getLoc(),
27682782
allowUsableFromInline);
27692783
if (current.empty())
27702784
return current;
@@ -3549,7 +3563,8 @@ bool IsCallAsFunctionNominalRequest::evaluate(Evaluator &evaluator,
35493563
// member access.
35503564
SmallVector<ValueDecl *, 4> results;
35513565
auto opts = NL_QualifiedDefault | NL_ProtocolMembers | NL_IgnoreAccessControl;
3552-
dc->lookupQualified(decl, DeclNameRef(ctx.Id_callAsFunction), opts, results);
3566+
dc->lookupQualified(decl, DeclNameRef(ctx.Id_callAsFunction),
3567+
decl->getLoc(), opts, results);
35533568

35543569
return llvm::any_of(results, [](ValueDecl *decl) -> bool {
35553570
if (auto *fd = dyn_cast<FuncDecl>(decl))

0 commit comments

Comments
 (0)