@@ -174,6 +174,10 @@ class swift::SourceLookupCache {
174
174
175
175
using AuxiliaryDeclMap = llvm::DenseMap<DeclName, TinyPtrVector<MissingDecl *>>;
176
176
AuxiliaryDeclMap TopLevelAuxiliaryDecls;
177
+
178
+ // / Top-level macros that produce arbitrary names.
179
+ SmallVector<MissingDecl *, 4 > TopLevelArbitraryMacros;
180
+
177
181
SmallVector<Decl *, 4 > MayHaveAuxiliaryDecls;
178
182
void populateAuxiliaryDeclCache ();
179
183
@@ -352,26 +356,46 @@ void SourceLookupCache::populateAuxiliaryDeclCache() {
352
356
for (auto attrConst : decl->getAttrs ().getAttributes <CustomAttr>()) {
353
357
auto *attr = const_cast <CustomAttr *>(attrConst);
354
358
UnresolvedMacroReference macroRef (attr);
359
+ bool introducesArbitraryNames = false ;
355
360
namelookup::forEachPotentialResolvedMacro (
356
361
decl->getDeclContext ()->getModuleScopeContext (),
357
362
macroRef.getMacroName (), MacroRole::Peer,
358
363
[&](MacroDecl *macro, const MacroRoleAttr *roleAttr) {
364
+ // First check for arbitrary names.
365
+ if (roleAttr->hasNameKind (MacroIntroducedDeclNameKind::Arbitrary)) {
366
+ introducesArbitraryNames = true ;
367
+ }
368
+
359
369
macro->getIntroducedNames (MacroRole::Peer,
360
370
dyn_cast<ValueDecl>(decl),
361
371
introducedNames[attr]);
362
372
});
373
+
374
+ // Record this macro where appropriate.
375
+ if (introducesArbitraryNames)
376
+ TopLevelArbitraryMacros.push_back (MissingDecl::forUnexpandedMacro (attr, decl));
363
377
}
364
378
365
379
if (auto *med = dyn_cast<MacroExpansionDecl>(decl)) {
366
380
UnresolvedMacroReference macroRef (med);
381
+ bool introducesArbitraryNames = false ;
367
382
namelookup::forEachPotentialResolvedMacro (
368
383
decl->getDeclContext ()->getModuleScopeContext (),
369
384
macroRef.getMacroName (), MacroRole::Declaration,
370
385
[&](MacroDecl *macro, const MacroRoleAttr *roleAttr) {
386
+ // First check for arbitrary names.
387
+ if (roleAttr->hasNameKind (MacroIntroducedDeclNameKind::Arbitrary)) {
388
+ introducesArbitraryNames = true ;
389
+ }
390
+
371
391
macro->getIntroducedNames (MacroRole::Declaration,
372
392
/* attachedTo*/ nullptr ,
373
393
introducedNames[med]);
374
394
});
395
+
396
+ // Record this macro where appropriate.
397
+ if (introducesArbitraryNames)
398
+ TopLevelArbitraryMacros.push_back (MissingDecl::forUnexpandedMacro (med, decl));
375
399
}
376
400
377
401
// Add macro-introduced names to the top-level auxiliary decl cache as
@@ -440,15 +464,30 @@ void SourceLookupCache::lookupValue(DeclName Name, NLKind LookupKind,
440
464
? UniqueMacroNamePlaceholder
441
465
: Name;
442
466
auto auxDecls = TopLevelAuxiliaryDecls.find (keyName);
443
- if (auxDecls == TopLevelAuxiliaryDecls.end ())
467
+
468
+ // Check macro expansions that could produce this name.
469
+ SmallVector<MissingDecl *, 4 > unexpandedDecls;
470
+ if (auxDecls != TopLevelAuxiliaryDecls.end ()) {
471
+ unexpandedDecls.insert (
472
+ unexpandedDecls.end (), auxDecls->second .begin (), auxDecls->second .end ());
473
+ }
474
+
475
+ // Check macro expansions that can produce arbitrary names.
476
+ unexpandedDecls.insert (
477
+ unexpandedDecls.end (),
478
+ TopLevelArbitraryMacros.begin (), TopLevelArbitraryMacros.end ());
479
+
480
+ if (unexpandedDecls.empty ())
444
481
return ;
445
482
446
- for (auto *unexpandedDecl : auxDecls->second ) {
447
- // Add expanded peers and freestanding declarations to the result.
483
+ // Add matching expanded peers and freestanding declarations to the results.
484
+ SmallPtrSet<ValueDecl *, 4 > macroExpandedDecls;
485
+ for (auto *unexpandedDecl : unexpandedDecls) {
448
486
unexpandedDecl->forEachMacroExpandedDecl (
449
487
[&](ValueDecl *decl) {
450
488
if (decl->getName ().matchesRef (Name)) {
451
- Result.push_back (decl);
489
+ if (macroExpandedDecls.insert (decl).second )
490
+ Result.push_back (decl);
452
491
}
453
492
});
454
493
}
0 commit comments