@@ -318,18 +318,14 @@ bool ExpandMemberAttributeMacros::evaluate(Evaluator &evaluator,
318318 auto parentAttrs = parentDecl->getSemanticAttrs ();
319319 for (auto customAttrConst: parentAttrs.getAttributes <CustomAttr>()) {
320320 auto customAttr = const_cast <CustomAttr *>(customAttrConst);
321- auto customAttrDecl = evaluateOrDefault (
321+ auto *macroDecl = evaluateOrDefault (
322322 evaluator,
323- CustomAttrDeclRequest {
323+ ResolveAttachedMacroRequest {
324324 customAttr,
325325 parentDecl->getInnermostDeclContext ()
326326 },
327327 nullptr );
328328
329- if (!customAttrDecl)
330- continue ;
331-
332- auto macroDecl = customAttrDecl.dyn_cast <MacroDecl *>();
333329 if (!macroDecl)
334330 continue ;
335331
@@ -364,10 +360,9 @@ static bool isFromExpansionOfMacro(SourceFile *sourceFile, MacroDecl *macro) {
364360 } else if (auto *macroAttr = sourceFile->getAttachedMacroAttribute ()) {
365361 auto *decl = expansion.dyn_cast <Decl *>();
366362 auto &ctx = decl->getASTContext ();
367- auto attrDecl = evaluateOrDefault (ctx.evaluator ,
368- CustomAttrDeclRequest {macroAttr, decl->getDeclContext ()},
363+ auto *macroDecl = evaluateOrDefault (ctx.evaluator ,
364+ ResolveAttachedMacroRequest {macroAttr, decl->getDeclContext ()},
369365 nullptr );
370- auto *macroDecl = attrDecl.dyn_cast <MacroDecl *>();
371366 if (!macroDecl)
372367 return false ;
373368
@@ -1083,32 +1078,16 @@ bool swift::expandAttributes(CustomAttr *attr, MacroDecl *macro, Decl *member) {
10831078 return addedAttributes;
10841079}
10851080
1086- Optional<MacroDecl *>
1087- swift::findMacroForCustomAttr (CustomAttr *attr, DeclContext *dc) {
1088- auto *identTypeRepr = dyn_cast_or_null<IdentTypeRepr>(attr->getTypeRepr ());
1089- if (!identTypeRepr)
1090- return None;
1091-
1092- // Look for macros at module scope. They can only occur at module scope, and
1093- // we need to be sure not to trigger name lookup into type contexts along
1094- // the way.
1081+ MacroDecl *
1082+ ResolveAttachedMacroRequest::evaluate (Evaluator &evaluator,
1083+ CustomAttr *attr,
1084+ DeclContext *dc) const {
1085+ auto &ctx = dc->getASTContext ();
10951086 llvm::TinyPtrVector<ValueDecl *> macros;
1096- auto moduleScopeDC = dc->getModuleScopeContext ();
1097- ASTContext &ctx = moduleScopeDC->getASTContext ();
1098- UnqualifiedLookupDescriptor descriptor (
1099- identTypeRepr->getNameRef (), moduleScopeDC
1100- );
1101- auto lookup = evaluateOrDefault (
1102- ctx.evaluator , UnqualifiedLookupRequest{descriptor}, {});
1103- for (const auto &result : lookup.allResults ()) {
1104- // Only keep attached macros, which can be spelled as custom attributes.
1105- if (auto macro = dyn_cast<MacroDecl>(result.getValueDecl ()))
1106- if (isAttachedMacro (macro->getMacroRoles ()))
1107- macros.push_back (macro);
1108- }
1087+ findMacroForCustomAttr (attr, dc, macros);
11091088
11101089 if (macros.empty ())
1111- return None ;
1090+ return nullptr ;
11121091
11131092 // Extract macro arguments from the attribute, or create an empty list.
11141093 ArgumentList *attrArgs;
@@ -1121,6 +1100,7 @@ swift::findMacroForCustomAttr(CustomAttr *attr, DeclContext *dc) {
11211100 // Form an `OverloadedDeclRefExpr` with the filtered lookup result above
11221101 // to ensure @freestanding macros are not considered in overload resolution.
11231102 FunctionRefKind functionRefKind = FunctionRefKind::SingleApply;
1103+ auto *identTypeRepr = dyn_cast<IdentTypeRepr>(attr->getTypeRepr ());
11241104 auto macroRefExpr = new (ctx) OverloadedDeclRefExpr (
11251105 macros, identTypeRepr->getNameLoc (), functionRefKind,
11261106 /* implicit*/ true );
0 commit comments