@@ -172,9 +172,6 @@ class swift::SourceLookupCache {
172172 template <typename Range>
173173 void addToMemberCache (Range decls);
174174
175- using MacroDeclMap = llvm::DenseMap<Identifier, TinyPtrVector<MacroDecl *>>;
176- MacroDeclMap MacroDecls;
177-
178175 using AuxiliaryDeclMap = llvm::DenseMap<DeclName, TinyPtrVector<MissingDecl *>>;
179176 AuxiliaryDeclMap TopLevelAuxiliaryDecls;
180177 SmallVector<Decl *, 4 > MayHaveAuxiliaryDecls;
@@ -190,6 +187,7 @@ class swift::SourceLookupCache {
190187 void invalidate ();
191188
192189 void lookupValue (DeclName Name, NLKind LookupKind,
190+ OptionSet<ModuleLookupFlags> Flags,
193191 SmallVectorImpl<ValueDecl*> &Result);
194192
195193 // / Retrieves all the operator decls. The order of the results is not
@@ -263,6 +261,10 @@ void SourceLookupCache::addToUnqualifiedLookupCache(Range decls,
263261 // a malformed context.
264262 if (ED->isInvalid ()) continue ;
265263
264+ if (ED->getAttrs ().hasAttribute <CustomAttr>()) {
265+ MayHaveAuxiliaryDecls.push_back (ED);
266+ }
267+
266268 if (!ED->hasUnparsedMembers () || ED->maybeHasOperatorDeclarations ())
267269 addToUnqualifiedLookupCache (ED->getMembers (), true );
268270 }
@@ -273,9 +275,6 @@ void SourceLookupCache::addToUnqualifiedLookupCache(Range decls,
273275 else if (auto *PG = dyn_cast<PrecedenceGroupDecl>(D))
274276 PrecedenceGroups[PG->getName ()].push_back (PG);
275277
276- else if (auto *macro = dyn_cast<MacroDecl>(D))
277- MacroDecls[macro->getBaseIdentifier ()].push_back (macro);
278-
279278 else if (auto *MED = dyn_cast<MacroExpansionDecl>(D))
280279 MayHaveAuxiliaryDecls.push_back (MED);
281280 }
@@ -353,32 +352,26 @@ void SourceLookupCache::populateAuxiliaryDeclCache() {
353352 for (auto attrConst : decl->getAttrs ().getAttributes <CustomAttr>()) {
354353 auto *attr = const_cast <CustomAttr *>(attrConst);
355354 UnresolvedMacroReference macroRef (attr);
356- auto macroName = macroRef.getMacroName ().getBaseIdentifier ();
357-
358- auto found = MacroDecls.find (macroName);
359- if (found == MacroDecls.end ())
360- continue ;
361-
362- for (const auto *macro : found->second ) {
363- macro->getIntroducedNames (MacroRole::Peer,
364- dyn_cast<ValueDecl>(decl),
365- introducedNames[attr]);
366- }
355+ namelookup::forEachPotentialResolvedMacro (
356+ decl->getDeclContext ()->getModuleScopeContext (),
357+ macroRef.getMacroName (), MacroRole::Peer,
358+ [&](MacroDecl *macro, const MacroRoleAttr *roleAttr) {
359+ macro->getIntroducedNames (MacroRole::Peer,
360+ dyn_cast<ValueDecl>(decl),
361+ introducedNames[attr]);
362+ });
367363 }
368364
369365 if (auto *med = dyn_cast<MacroExpansionDecl>(decl)) {
370366 UnresolvedMacroReference macroRef (med);
371- auto macroName = macroRef.getMacroName ().getBaseIdentifier ();
372-
373- auto found = MacroDecls.find (macroName);
374- if (found == MacroDecls.end ())
375- continue ;
376-
377- for (const auto *macro : found->second ) {
378- macro->getIntroducedNames (MacroRole::Declaration,
379- /* attachedTo*/ nullptr ,
380- introducedNames[med]);
381- }
367+ namelookup::forEachPotentialResolvedMacro (
368+ decl->getDeclContext ()->getModuleScopeContext (),
369+ macroRef.getMacroName (), MacroRole::Declaration,
370+ [&](MacroDecl *macro, const MacroRoleAttr *roleAttr) {
371+ macro->getIntroducedNames (MacroRole::Declaration,
372+ /* attachedTo*/ nullptr ,
373+ introducedNames[med]);
374+ });
382375 }
383376
384377 // Add macro-introduced names to the top-level auxiliary decl cache as
@@ -425,6 +418,7 @@ SourceLookupCache::SourceLookupCache(const ModuleDecl &M)
425418}
426419
427420void SourceLookupCache::lookupValue (DeclName Name, NLKind LookupKind,
421+ OptionSet<ModuleLookupFlags> Flags,
428422 SmallVectorImpl<ValueDecl*> &Result) {
429423 auto I = TopLevelValues.find (Name);
430424 if (I != TopLevelValues.end ()) {
@@ -433,6 +427,10 @@ void SourceLookupCache::lookupValue(DeclName Name, NLKind LookupKind,
433427 Result.push_back (Elt);
434428 }
435429
430+ // If we aren't supposed to find names introduced by macros, we're done.
431+ if (Flags.contains (ModuleLookupFlags::ExcludeMacroExpansions))
432+ return ;
433+
436434 // Add top-level auxiliary decls to the result.
437435 //
438436 // FIXME: We need to not consider auxiliary decls if we're doing lookup
@@ -906,17 +904,18 @@ static bool isParsedModule(const ModuleDecl *mod) {
906904}
907905
908906void ModuleDecl::lookupValue (DeclName Name, NLKind LookupKind,
907+ OptionSet<ModuleLookupFlags> Flags,
909908 SmallVectorImpl<ValueDecl*> &Result) const {
910909 auto *stats = getASTContext ().Stats ;
911910 if (stats)
912911 ++stats->getFrontendCounters ().NumModuleLookupValue ;
913912
914913 if (isParsedModule (this )) {
915- getSourceLookupCache ().lookupValue (Name, LookupKind, Result);
914+ getSourceLookupCache ().lookupValue (Name, LookupKind, Flags, Result);
916915 return ;
917916 }
918917
919- FORWARD (lookupValue, (Name, LookupKind, Result));
918+ FORWARD (lookupValue, (Name, LookupKind, Flags, Result));
920919}
921920
922921TypeDecl * ModuleDecl::lookupLocalType (StringRef MangledName) const {
@@ -1010,6 +1009,7 @@ void ModuleDecl::lookupImportedSPIGroups(
10101009}
10111010
10121011void BuiltinUnit::lookupValue (DeclName name, NLKind lookupKind,
1012+ OptionSet<ModuleLookupFlags> Flags,
10131013 SmallVectorImpl<ValueDecl*> &result) const {
10141014 getCache ().lookupValue (name.getBaseIdentifier (), lookupKind, *this , result);
10151015}
@@ -1021,8 +1021,9 @@ void BuiltinUnit::lookupObjCMethods(
10211021}
10221022
10231023void SourceFile::lookupValue (DeclName name, NLKind lookupKind,
1024+ OptionSet<ModuleLookupFlags> flags,
10241025 SmallVectorImpl<ValueDecl*> &result) const {
1025- getCache ().lookupValue (name, lookupKind, result);
1026+ getCache ().lookupValue (name, lookupKind, flags, result);
10261027}
10271028
10281029void ModuleDecl::lookupVisibleDecls (ImportPath::Access AccessPath,
@@ -3980,6 +3981,7 @@ SynthesizedFileUnit::getDiscriminatorForPrivateDecl(const Decl *D) const {
39803981
39813982void SynthesizedFileUnit::lookupValue (
39823983 DeclName name, NLKind lookupKind,
3984+ OptionSet<ModuleLookupFlags> Flags,
39833985 SmallVectorImpl<ValueDecl *> &result) const {
39843986 for (auto *decl : TopLevelDecls) {
39853987 if (auto VD = dyn_cast<ValueDecl>(decl)) {
0 commit comments