Skip to content

Commit 6a17bb6

Browse files
committed
[Macros] Factor out iteration over all attached macro attributes of a particular
role into `Decl::forEachAttachedMacro`.
1 parent e5ba389 commit 6a17bb6

File tree

4 files changed

+43
-58
lines changed

4 files changed

+43
-58
lines changed

include/swift/AST/Decl.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ namespace swift {
8080
class GenericSignature;
8181
class GenericTypeParamDecl;
8282
class GenericTypeParamType;
83+
class MacroDecl;
8384
class MacroDefinition;
8485
class ModuleDecl;
8586
class NamedPattern;
@@ -856,6 +857,13 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
856857
/// attribute macro expansion.
857858
DeclAttributes getSemanticAttrs() const;
858859

860+
using MacroCallback = llvm::function_ref<void(CustomAttr *, MacroDecl *)>;
861+
862+
/// Iterate over each attached macro with the given role, invoking the
863+
/// given callback with each macro custom attribute and corresponding macro
864+
/// declaration.
865+
void forEachAttachedMacro(MacroRole role, MacroCallback) const;
866+
859867
/// Returns the innermost enclosing decl with an availability annotation.
860868
const Decl *getInnermostDeclWithAvailability() const;
861869

lib/AST/Decl.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,28 @@ DeclAttributes Decl::getSemanticAttrs() const {
374374
return getAttrs();
375375
}
376376

377+
void Decl::forEachAttachedMacro(MacroRole role,
378+
MacroCallback macroCallback) const {
379+
auto *dc = getDeclContext();
380+
auto &ctx = dc->getASTContext();
381+
382+
for (auto customAttrConst : getSemanticAttrs().getAttributes<CustomAttr>()) {
383+
auto customAttr = const_cast<CustomAttr *>(customAttrConst);
384+
auto *macroDecl = evaluateOrDefault(
385+
ctx.evaluator,
386+
ResolveAttachedMacroRequest{customAttr, dc},
387+
nullptr);
388+
389+
if (!macroDecl)
390+
continue;
391+
392+
if (!macroDecl->getMacroRoles().contains(role))
393+
continue;
394+
395+
macroCallback(customAttr, macroDecl);
396+
}
397+
}
398+
377399
const Decl *Decl::getInnermostDeclWithAvailability() const {
378400
const Decl *enclosingDecl = this;
379401
// Find the innermost enclosing declaration with an @available annotation.

lib/Sema/TypeCheckMacros.cpp

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -316,51 +316,21 @@ bool ExpandMemberAttributeMacros::evaluate(Evaluator &evaluator,
316316
return false;
317317

318318
bool addedAttributes = false;
319-
auto parentAttrs = parentDecl->getSemanticAttrs();
320-
for (auto customAttrConst: parentAttrs.getAttributes<CustomAttr>()) {
321-
auto customAttr = const_cast<CustomAttr *>(customAttrConst);
322-
auto *macroDecl = evaluateOrDefault(
323-
evaluator,
324-
ResolveAttachedMacroRequest{
325-
customAttr,
326-
parentDecl->getInnermostDeclContext()
327-
},
328-
nullptr);
329-
330-
if (!macroDecl)
331-
continue;
332-
333-
if (!macroDecl->getMacroRoles().contains(MacroRole::MemberAttribute))
334-
continue;
335-
336-
addedAttributes |= expandAttributes(customAttr, macroDecl, decl);
337-
}
319+
parentDecl->forEachAttachedMacro(MacroRole::MemberAttribute,
320+
[&](CustomAttr *attr, MacroDecl *macro) {
321+
addedAttributes |= expandAttributes(attr, macro, decl);
322+
});
338323

339324
return addedAttributes;
340325
}
341326

342327
bool ExpandSynthesizedMemberMacroRequest::evaluate(Evaluator &evaluator,
343328
Decl *decl) const {
344-
auto &ctx = decl->getASTContext();
345-
auto *dc = decl->getInnermostDeclContext();
346329
bool synthesizedMembers = false;
347-
348-
for (auto customAttrConst : decl->getSemanticAttrs().getAttributes<CustomAttr>()) {
349-
auto customAttr = const_cast<CustomAttr *>(customAttrConst);
350-
auto *macroDecl = evaluateOrDefault(
351-
ctx.evaluator,
352-
ResolveAttachedMacroRequest{customAttr, dc},
353-
nullptr);
354-
355-
if (!macroDecl)
356-
continue;
357-
358-
if (!macroDecl->getMacroRoles().contains(MacroRole::SynthesizedMembers))
359-
continue;
360-
361-
// Expand the synthesized members.
362-
synthesizedMembers |= expandSynthesizedMembers(customAttr, macroDecl, decl);
363-
}
330+
decl->forEachAttachedMacro(MacroRole::SynthesizedMembers,
331+
[&](CustomAttr *attr, MacroDecl *macro) {
332+
synthesizedMembers |= expandSynthesizedMembers(attr, macro, decl);
333+
});
364334

365335
return synthesizedMembers;
366336
}

lib/Sema/TypeCheckStorage.cpp

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3447,26 +3447,11 @@ StorageImplInfoRequest::evaluate(Evaluator &evaluator,
34473447
return info;
34483448
}
34493449

3450-
// Check for an accessor macro.
3451-
for (auto customAttrConst : storage->getSemanticAttrs().getAttributes<CustomAttr>()) {
3452-
auto customAttr = const_cast<CustomAttr *>(customAttrConst);
3453-
auto *macro = evaluateOrDefault(
3454-
evaluator,
3455-
ResolveAttachedMacroRequest{
3456-
customAttr,
3457-
storage->getInnermostDeclContext()
3458-
},
3459-
nullptr);
3460-
3461-
if (!macro)
3462-
continue;
3463-
3464-
if (!macro->getMacroRoles().contains(MacroRole::Accessor))
3465-
continue;
3466-
3467-
// Expand the accessors.
3468-
expandAccessors(storage, customAttr, macro);
3469-
}
3450+
// Expand any attached accessor macros.
3451+
storage->forEachAttachedMacro(MacroRole::Accessor,
3452+
[&](CustomAttr *customAttr, MacroDecl *macro) {
3453+
expandAccessors(storage, customAttr, macro);
3454+
});
34703455

34713456
bool hasWillSet = storage->getParsedAccessor(AccessorKind::WillSet);
34723457
bool hasDidSet = storage->getParsedAccessor(AccessorKind::DidSet);

0 commit comments

Comments
 (0)