Skip to content

Commit 6d985e3

Browse files
committed
AST: Introduce Decl::getSemanticCustomAttrs().
In order to avoid triggering request cycles as `SemanticDeclAttrsRequest` grows to cover a larger range of implicit attributes, we need a lighter-weight accessor for just the CustomAttrs that have been expanded from macros. Introduce `getSemanticCustomAttrs()` and adopt it where `getSemanticAttrs()` had been previously used to enumerate macro expanded custom attributes.
1 parent a64f705 commit 6d985e3

File tree

6 files changed

+24
-7
lines changed

6 files changed

+24
-7
lines changed

include/swift/AST/Decl.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,13 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
924924
/// expansions.
925925
OrigDeclAttributes getOriginalAttrs() const;
926926

927-
/// Returns the semantic attributes attached to this declaration,
927+
/// Returns the semantic CustomAttrs attached to this declaration,
928+
/// including attributes that are generated as the result of member
929+
/// attribute macro expansion.
930+
DeclAttributes::AttributeKindRange<CustomAttr, false>
931+
getSemanticCustomAttrs() const;
932+
933+
/// Returns all semantic attributes attached to this declaration,
928934
/// including attributes that are generated as the result of member
929935
/// attribute macro expansion.
930936
DeclAttributes getSemanticAttrs() const;

lib/AST/Decl.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,15 @@ OrigDeclAttributes Decl::getOriginalAttrs() const {
375375
return OrigDeclAttributes(getAttrs(), this);
376376
}
377377

378+
DeclAttributes::AttributeKindRange<CustomAttr, false>
379+
Decl::getSemanticCustomAttrs() const {
380+
auto mutableThis = const_cast<Decl *>(this);
381+
(void)evaluateOrDefault(getASTContext().evaluator,
382+
ExpandMemberAttributeMacros{mutableThis}, {});
383+
384+
return getAttrs().getAttributes<CustomAttr>();
385+
}
386+
378387
DeclAttributes Decl::getSemanticAttrs() const {
379388
(void)evaluateOrDefault(getASTContext().evaluator,
380389
SemanticDeclAttrsRequest{this}, {});
@@ -438,7 +447,7 @@ void Decl::visitAuxiliaryDecls(
438447

439448
void Decl::forEachAttachedMacro(MacroRole role,
440449
MacroCallback macroCallback) const {
441-
for (auto customAttrConst : getSemanticAttrs().getAttributes<CustomAttr>()) {
450+
for (auto customAttrConst : getSemanticCustomAttrs()) {
442451
auto customAttr = const_cast<CustomAttr *>(customAttrConst);
443452
auto *macroDecl = getResolvedMacro(customAttr);
444453

lib/AST/NameLookup.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1725,7 +1725,7 @@ void namelookup::forEachPotentialAttachedMacro(
17251725
// We intentionally avoid calling `forEachAttachedMacro` in order to avoid
17261726
// a request cycle.
17271727
auto moduleScopeCtx = decl->getDeclContext()->getModuleScopeContext();
1728-
for (auto attrConst : decl->getSemanticAttrs().getAttributes<CustomAttr>()) {
1728+
for (auto attrConst : decl->getSemanticCustomAttrs()) {
17291729
auto *attr = const_cast<CustomAttr *>(attrConst);
17301730
UnresolvedMacroReference macroRef(attr);
17311731
auto macroName = macroRef.getMacroName();

lib/Sema/TypeCheckAccess.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1281,7 +1281,7 @@ class AccessControlChecker : public AccessControlCheckerBase,
12811281
}
12821282

12831283
void checkAttachedMacrosAccess(const Decl *D) {
1284-
for (auto customAttrC : D->getSemanticAttrs().getAttributes<CustomAttr>()) {
1284+
for (auto customAttrC : D->getSemanticCustomAttrs()) {
12851285
auto customAttr = const_cast<CustomAttr *>(customAttrC);
12861286
auto *macroDecl = D->getResolvedMacro(customAttr);
12871287
if (macroDecl) {

lib/Sema/TypeCheckMacros.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1664,7 +1664,7 @@ ArrayRef<unsigned>
16641664
ExpandExtensionMacros::evaluate(Evaluator &evaluator,
16651665
NominalTypeDecl *nominal) const {
16661666
SmallVector<unsigned, 2> bufferIDs;
1667-
for (auto customAttrConst : nominal->getSemanticAttrs().getAttributes<CustomAttr>()) {
1667+
for (auto customAttrConst : nominal->getSemanticCustomAttrs()) {
16681668
auto customAttr = const_cast<CustomAttr *>(customAttrConst);
16691669
auto *macro = nominal->getResolvedMacro(customAttr);
16701670

lib/Sema/TypeCheckPropertyWrapper.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,7 @@ AttachedPropertyWrappersRequest::evaluate(Evaluator &evaluator,
438438
auto dc = var->getDeclContext();
439439
llvm::TinyPtrVector<CustomAttr *> result;
440440

441-
auto attachedAttrs = var->getSemanticAttrs();
442-
for (auto attr : attachedAttrs.getAttributes<CustomAttr>()) {
441+
for (auto attr : var->getSemanticCustomAttrs()) {
443442
auto mutableAttr = const_cast<CustomAttr *>(attr);
444443
// Figure out which nominal declaration this custom attribute refers to.
445444
auto *nominal = evaluateOrDefault(
@@ -481,6 +480,9 @@ AttachedPropertyWrappersRequest::evaluate(Evaluator &evaluator,
481480
continue;
482481
}
483482

483+
// Note: Getting the semantic attrs here would trigger a request cycle.
484+
auto attachedAttrs = var->getAttrs();
485+
484486
// Check for conflicting attributes.
485487
if (attachedAttrs.hasAttribute<LazyAttr>() ||
486488
attachedAttrs.hasAttribute<NSCopyingAttr>() ||

0 commit comments

Comments
 (0)