@@ -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
@@ -1086,3 +1081,43 @@ bool swift::expandAttributes(CustomAttr *attr, MacroDecl *macro, Decl *member) {
10861081
10871082 return addedAttributes;
10881083}
1084+
1085+ MacroDecl *
1086+ ResolveAttachedMacroRequest::evaluate (Evaluator &evaluator,
1087+ CustomAttr *attr,
1088+ DeclContext *dc) const {
1089+ auto &ctx = dc->getASTContext ();
1090+ llvm::TinyPtrVector<ValueDecl *> macros;
1091+ findMacroForCustomAttr (attr, dc, macros);
1092+
1093+ if (macros.empty ())
1094+ return nullptr ;
1095+
1096+ // Extract macro arguments from the attribute, or create an empty list.
1097+ ArgumentList *attrArgs;
1098+ if (attr->hasArgs ()) {
1099+ attrArgs = attr->getArgs ();
1100+ } else {
1101+ attrArgs = ArgumentList::createImplicit (ctx, {});
1102+ }
1103+
1104+ // Form an `OverloadedDeclRefExpr` with the filtered lookup result above
1105+ // to ensure @freestanding macros are not considered in overload resolution.
1106+ FunctionRefKind functionRefKind = FunctionRefKind::SingleApply;
1107+ auto *identTypeRepr = dyn_cast<IdentTypeRepr>(attr->getTypeRepr ());
1108+ auto macroRefExpr = new (ctx) OverloadedDeclRefExpr (
1109+ macros, identTypeRepr->getNameLoc (), functionRefKind,
1110+ /* implicit*/ true );
1111+ auto *call = CallExpr::createImplicit (ctx, macroRefExpr, attrArgs);
1112+
1113+ Expr *result = call;
1114+ TypeChecker::typeCheckExpression (result, dc);
1115+
1116+ if (auto *fn = dyn_cast<DeclRefExpr>(call->getFn ()))
1117+ if (auto *macro = dyn_cast<MacroDecl>(fn->getDecl ()))
1118+ return macro;
1119+
1120+ // If we couldn't resolve a macro decl, the attribute is invalid.
1121+ attr->setInvalid ();
1122+ return nullptr ;
1123+ }
0 commit comments