-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Macros] Implement unqualified lookup for global macro-expanded peer declarations. #64065
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -170,6 +170,15 @@ class swift::SourceLookupCache { | |
void addToUnqualifiedLookupCache(Range decls, bool onlyOperators); | ||
template<typename Range> | ||
void addToMemberCache(Range decls); | ||
|
||
using MacroDeclMap = llvm::DenseMap<Identifier, TinyPtrVector<MacroDecl *>>; | ||
MacroDeclMap MacroDecls; | ||
|
||
using AuxiliaryDeclMap = llvm::DenseMap<DeclName, TinyPtrVector<MissingDecl *>>; | ||
AuxiliaryDeclMap TopLevelAuxiliaryDecls; | ||
SmallVector<ValueDecl *, 4> MayHaveAuxiliaryDecls; | ||
void populateAuxiliaryDeclCache(); | ||
|
||
public: | ||
SourceLookupCache(const SourceFile &SF); | ||
SourceLookupCache(const ModuleDecl &Mod); | ||
|
@@ -234,6 +243,10 @@ void SourceLookupCache::addToUnqualifiedLookupCache(Range decls, | |
if (onlyOperators ? VD->isOperator() : VD->hasName()) { | ||
// Cache the value under both its compound name and its full name. | ||
TopLevelValues.add(VD); | ||
|
||
if (VD->getAttrs().hasAttribute<CustomAttr>()) { | ||
MayHaveAuxiliaryDecls.push_back(VD); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -256,6 +269,9 @@ void SourceLookupCache::addToUnqualifiedLookupCache(Range decls, | |
|
||
if (auto *PG = dyn_cast<PrecedenceGroupDecl>(D)) | ||
PrecedenceGroups[PG->getName()].push_back(PG); | ||
|
||
if (auto *macro = dyn_cast<MacroDecl>(D)) | ||
MacroDecls[macro->getBaseIdentifier()].push_back(macro); | ||
} | ||
} | ||
|
||
|
@@ -309,6 +325,53 @@ void SourceLookupCache::addToMemberCache(Range decls) { | |
} | ||
} | ||
|
||
void SourceLookupCache::populateAuxiliaryDeclCache() { | ||
for (auto *decl : MayHaveAuxiliaryDecls) { | ||
// Gather macro-introduced peer names. | ||
llvm::SmallDenseMap<CustomAttr *, llvm::SmallVector<DeclName, 2>> introducedNames; | ||
|
||
// This code deliberately avoids `forEachAttachedMacro`, because it | ||
// will perform overload resolution and possibly invoke unqualified | ||
// lookup for macro arguments, which will recursively populate the | ||
// auxiliary decl cache and cause request cycles. | ||
// | ||
// We do not need a fully resolved macro until expansion. Instead, we | ||
// conservatively consider peer names for all macro declarations with a | ||
// custom attribute name. Unqualified lookup for that name will later | ||
// invoke expansion of the macro, and will yield no results if the resolved | ||
// macro does not produce the requested name, so the only impact is possibly | ||
// expanding earlier than needed / unnecessarily looking in the top-level | ||
// auxiliary decl cache. | ||
Comment on lines
+338
to
+344
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this approach a lot! |
||
for (auto attrConst : decl->getSemanticAttrs().getAttributes<CustomAttr>()) { | ||
auto *attr = const_cast<CustomAttr *>(attrConst); | ||
UnresolvedMacroReference macroRef(attr); | ||
auto macroName = macroRef.getMacroName().getBaseIdentifier(); | ||
|
||
auto found = MacroDecls.find(macroName); | ||
if (found == MacroDecls.end()) | ||
continue; | ||
|
||
for (const auto *macro : found->second) { | ||
macro->getIntroducedNames(MacroRole::Peer, | ||
decl, introducedNames[attr]); | ||
} | ||
} | ||
|
||
// Add macro-introduced names to the top-level auxiliary decl cache as | ||
// unexpanded peer decls represented by a MissingDecl. | ||
for (auto macroNames : introducedNames) { | ||
auto *macroAttr = macroNames.getFirst(); | ||
for (auto name : macroNames.getSecond()) { | ||
auto *placeholder = | ||
MissingDecl::forUnexpandedPeer(macroAttr, decl); | ||
name.addToLookupTable(TopLevelAuxiliaryDecls, placeholder); | ||
} | ||
} | ||
} | ||
|
||
MayHaveAuxiliaryDecls.clear(); | ||
} | ||
|
||
/// Populate our cache on the first name lookup. | ||
SourceLookupCache::SourceLookupCache(const SourceFile &SF) { | ||
FrontendStatsTracer tracer(SF.getASTContext().Stats, | ||
|
@@ -339,6 +402,23 @@ void SourceLookupCache::lookupValue(DeclName Name, NLKind LookupKind, | |
Result.reserve(I->second.size()); | ||
for (ValueDecl *Elt : I->second) | ||
Result.push_back(Elt); | ||
|
||
// Add top-level auxiliary decls to the result. | ||
// | ||
// FIXME: We need to not consider auxiliary decls if we're doing lookup | ||
// from inside a macro argument at module scope. | ||
populateAuxiliaryDeclCache(); | ||
auto auxDecls = TopLevelAuxiliaryDecls.find(Name); | ||
if (auxDecls == TopLevelAuxiliaryDecls.end()) | ||
return; | ||
|
||
for (auto *unexpandedDecl : auxDecls->second) { | ||
// Add expanded peers to the result. | ||
unexpandedDecl->forEachExpandedPeer( | ||
[&](ValueDecl *expandedPeer) { | ||
Result.push_back(expandedPeer); | ||
}); | ||
} | ||
} | ||
|
||
void SourceLookupCache::getPrecedenceGroups( | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this somehow report to its caller when
arbitrary
is there?