@@ -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
@@ -863,17 +861,18 @@ static bool isParsedModule(const ModuleDecl *mod) {
863861}
864862
865863void ModuleDecl::lookupValue (DeclName Name, NLKind LookupKind,
864+ OptionSet<ModuleLookupFlags> Flags,
866865 SmallVectorImpl<ValueDecl*> &Result) const {
867866 auto *stats = getASTContext ().Stats ;
868867 if (stats)
869868 ++stats->getFrontendCounters ().NumModuleLookupValue ;
870869
871870 if (isParsedModule (this )) {
872- getSourceLookupCache ().lookupValue (Name, LookupKind, Result);
871+ getSourceLookupCache ().lookupValue (Name, LookupKind, Flags, Result);
873872 return ;
874873 }
875874
876- FORWARD (lookupValue, (Name, LookupKind, Result));
875+ FORWARD (lookupValue, (Name, LookupKind, Flags, Result));
877876}
878877
879878TypeDecl * ModuleDecl::lookupLocalType (StringRef MangledName) const {
@@ -967,6 +966,7 @@ void ModuleDecl::lookupImportedSPIGroups(
967966}
968967
969968void BuiltinUnit::lookupValue (DeclName name, NLKind lookupKind,
969+ OptionSet<ModuleLookupFlags> Flags,
970970 SmallVectorImpl<ValueDecl*> &result) const {
971971 getCache ().lookupValue (name.getBaseIdentifier (), lookupKind, *this , result);
972972}
@@ -978,8 +978,9 @@ void BuiltinUnit::lookupObjCMethods(
978978}
979979
980980void SourceFile::lookupValue (DeclName name, NLKind lookupKind,
981+ OptionSet<ModuleLookupFlags> flags,
981982 SmallVectorImpl<ValueDecl*> &result) const {
982- getCache ().lookupValue (name, lookupKind, result);
983+ getCache ().lookupValue (name, lookupKind, flags, result);
983984}
984985
985986void ModuleDecl::lookupVisibleDecls (ImportPath::Access AccessPath,
@@ -3922,6 +3923,7 @@ SynthesizedFileUnit::getDiscriminatorForPrivateValue(const ValueDecl *D) const {
39223923
39233924void SynthesizedFileUnit::lookupValue (
39243925 DeclName name, NLKind lookupKind,
3926+ OptionSet<ModuleLookupFlags> Flags,
39253927 SmallVectorImpl<ValueDecl *> &result) const {
39263928 for (auto *decl : TopLevelDecls) {
39273929 if (auto VD = dyn_cast<ValueDecl>(decl)) {
0 commit comments