@@ -171,6 +171,9 @@ class swift::SourceLookupCache {
171
171
template <typename Range>
172
172
void addToMemberCache (Range decls);
173
173
174
+ using MacroDeclMap = llvm::DenseMap<Identifier, TinyPtrVector<MacroDecl *>>;
175
+ MacroDeclMap MacroDecls;
176
+
174
177
using AuxiliaryDeclMap = llvm::DenseMap<DeclName, TinyPtrVector<MissingDecl *>>;
175
178
AuxiliaryDeclMap TopLevelAuxiliaryDecls;
176
179
SmallVector<ValueDecl *, 4 > MayHaveAuxiliaryDecls;
@@ -266,6 +269,9 @@ void SourceLookupCache::addToUnqualifiedLookupCache(Range decls,
266
269
267
270
if (auto *PG = dyn_cast<PrecedenceGroupDecl>(D))
268
271
PrecedenceGroups[PG->getName ()].push_back (PG);
272
+
273
+ if (auto *macro = dyn_cast<MacroDecl>(D))
274
+ MacroDecls[macro->getBaseIdentifier ()].push_back (macro);
269
275
}
270
276
}
271
277
@@ -321,8 +327,6 @@ void SourceLookupCache::addToMemberCache(Range decls) {
321
327
322
328
void SourceLookupCache::populateAuxiliaryDeclCache () {
323
329
for (auto *decl : MayHaveAuxiliaryDecls) {
324
- auto *dc = decl->getDeclContext ();
325
-
326
330
// Gather macro-introduced peer names.
327
331
llvm::SmallDenseMap<CustomAttr *, llvm::SmallVector<DeclName, 2 >> introducedNames;
328
332
@@ -341,16 +345,15 @@ void SourceLookupCache::populateAuxiliaryDeclCache() {
341
345
for (auto attrConst : decl->getSemanticAttrs ().getAttributes <CustomAttr>()) {
342
346
auto *attr = const_cast <CustomAttr *>(attrConst);
343
347
UnresolvedMacroReference macroRef (attr);
344
- auto moduleScopeDC = dc->getModuleScopeContext ();
345
- ASTContext &ctx = moduleScopeDC->getASTContext ();
346
- UnqualifiedLookupDescriptor descriptor (macroRef.getMacroName (), moduleScopeDC);
347
- auto lookup = evaluateOrDefault (
348
- ctx.evaluator , UnqualifiedLookupRequest{descriptor}, {});
349
- for (const auto &found : lookup.allResults ()) {
350
- if (auto macro = dyn_cast<MacroDecl>(found.getValueDecl ())) {
351
- macro->getIntroducedNames (MacroRole::Peer,
352
- decl, introducedNames[attr]);
353
- }
348
+ auto macroName = macroRef.getMacroName ().getBaseIdentifier ();
349
+
350
+ auto found = MacroDecls.find (macroName);
351
+ if (found == MacroDecls.end ())
352
+ continue ;
353
+
354
+ for (const auto *macro : found->second ) {
355
+ macro->getIntroducedNames (MacroRole::Peer,
356
+ decl, introducedNames[attr]);
354
357
}
355
358
}
356
359
@@ -396,34 +399,25 @@ void SourceLookupCache::lookupValue(DeclName Name, NLKind LookupKind,
396
399
auto I = TopLevelValues.find (Name);
397
400
if (I == TopLevelValues.end ()) return ;
398
401
399
- bool foundMacros = false ;
400
402
Result.reserve (I->second .size ());
401
- for (ValueDecl *Elt : I->second ) {
403
+ for (ValueDecl *Elt : I->second )
402
404
Result.push_back (Elt);
403
- foundMacros = isa<MacroDecl>(Elt);
404
- }
405
405
406
- // If none of the results are macro decls, look into peers.
407
- //
408
- // FIXME: This approach is an approximation for whether lookup is attempting
409
- // to find macro declarations, and it may cause cycles for invalid macro
410
- // references. We need a more principled way to determine whether we're looking
411
- // for a macro and should not look into auxiliary decls during lookup.
406
+ // Add top-level auxiliary decls to the result.
412
407
//
413
- // We also need to not consider auxiliary decls if we're doing lookup from
414
- // inside a macro argument at module scope.
415
- if (!foundMacros) {
416
- populateAuxiliaryDeclCache ();
417
- auto I = TopLevelAuxiliaryDecls.find (Name);
418
- if (I == TopLevelAuxiliaryDecls.end ()) return ;
419
-
420
- for (auto *unexpandedDecl : I->second ) {
421
- // Add expanded peers to the result.
422
- unexpandedDecl->forEachExpandedPeer (
423
- [&](ValueDecl *expandedPeer) {
424
- Result.push_back (expandedPeer);
425
- });
426
- }
408
+ // FIXME: We need to not consider auxiliary decls if we're doing lookup
409
+ // from inside a macro argument at module scope.
410
+ populateAuxiliaryDeclCache ();
411
+ auto auxDecls = TopLevelAuxiliaryDecls.find (Name);
412
+ if (auxDecls == TopLevelAuxiliaryDecls.end ())
413
+ return ;
414
+
415
+ for (auto *unexpandedDecl : auxDecls->second ) {
416
+ // Add expanded peers to the result.
417
+ unexpandedDecl->forEachExpandedPeer (
418
+ [&](ValueDecl *expandedPeer) {
419
+ Result.push_back (expandedPeer);
420
+ });
427
421
}
428
422
}
429
423
0 commit comments