[Macros] Ensure that name lookup can find uniquely-generated macro names

While a Swift program cannot directly utter one of the unique names
produced by the macro expansion context outside of the macro itself,
utilities such as type reconstruction require the ability to look up
these names. Note that various kinds of macros can introduce unique
names, and update the name lookup facilities for macro-generated names
to account for them.

Fixes rdar://106053984.
This commit is contained in:
Doug Gregor
2023-03-07 11:31:27 -08:00
parent 8b63ce8273
commit 3c779e7405
5 changed files with 83 additions and 5 deletions

View File

@@ -160,6 +160,7 @@ class swift::SourceLookupCache {
ValueDeclMap TopLevelValues;
ValueDeclMap ClassMembers;
bool MemberCachePopulated = false;
DeclName UniqueMacroNamePlaceholder;
template<typename T>
using OperatorMap = llvm::DenseMap<Identifier, TinyPtrVector<T *>>;
@@ -179,6 +180,8 @@ class swift::SourceLookupCache {
SmallVector<Decl *, 4> MayHaveAuxiliaryDecls;
void populateAuxiliaryDeclCache();
SourceLookupCache(ASTContext &ctx);
public:
SourceLookupCache(const SourceFile &SF);
SourceLookupCache(const ModuleDecl &Mod);
@@ -392,15 +395,22 @@ void SourceLookupCache::populateAuxiliaryDeclCache() {
MayHaveAuxiliaryDecls.clear();
}
SourceLookupCache::SourceLookupCache(ASTContext &ctx)
: UniqueMacroNamePlaceholder(MacroDecl::getUniqueNamePlaceholder(ctx)) { }
/// Populate our cache on the first name lookup.
SourceLookupCache::SourceLookupCache(const SourceFile &SF) {
SourceLookupCache::SourceLookupCache(const SourceFile &SF)
: SourceLookupCache(SF.getASTContext())
{
FrontendStatsTracer tracer(SF.getASTContext().Stats,
"source-file-populate-cache");
addToUnqualifiedLookupCache(SF.getTopLevelDecls(), false);
addToUnqualifiedLookupCache(SF.getHoistedDecls(), false);
}
SourceLookupCache::SourceLookupCache(const ModuleDecl &M) {
SourceLookupCache::SourceLookupCache(const ModuleDecl &M)
: SourceLookupCache(M.getASTContext())
{
FrontendStatsTracer tracer(M.getASTContext().Stats,
"module-populate-cache");
for (const FileUnit *file : M.getFiles()) {
@@ -428,7 +438,10 @@ void SourceLookupCache::lookupValue(DeclName Name, NLKind LookupKind,
// 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);
DeclName keyName = MacroDecl::isUniqueMacroName(Name.getBaseName())
? UniqueMacroNamePlaceholder
: Name;
auto auxDecls = TopLevelAuxiliaryDecls.find(keyName);
if (auxDecls == TopLevelAuxiliaryDecls.end())
return;