Skip to content

Commit 51580e3

Browse files
authored
Merge pull request #69561 from tshortli/semantic-attrs-request
AST: Introduce SemanticDeclAttrsRequest
2 parents 192bb2e + 6d985e3 commit 51580e3

File tree

9 files changed

+84
-12
lines changed

9 files changed

+84
-12
lines changed

include/swift/AST/Decl.h

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
351351
// for the inline bitfields.
352352
union { uint64_t OpaqueBits;
353353

354-
SWIFT_INLINE_BITFIELD_BASE(Decl, bitmax(NumDeclKindBits,8)+1+1+1+1+1,
354+
SWIFT_INLINE_BITFIELD_BASE(Decl, bitmax(NumDeclKindBits,8)+1+1+1+1+1+1,
355355
Kind : bitmax(NumDeclKindBits,8),
356356

357357
/// Whether this declaration is invalid.
@@ -374,7 +374,10 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
374374
/// a local context, but should behave like a top-level
375375
/// declaration for name lookup purposes. This is used by
376376
/// lldb.
377-
Hoisted : 1
377+
Hoisted : 1,
378+
379+
/// Whether the set of semantic attributes has been computed.
380+
SemanticAttrsComputed : 1
378381
);
379382

380383
SWIFT_INLINE_BITFIELD_FULL(PatternBindingDecl, Decl, 1+1+2+16,
@@ -921,7 +924,13 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
921924
/// expansions.
922925
OrigDeclAttributes getOriginalAttrs() const;
923926

924-
/// 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,
925934
/// including attributes that are generated as the result of member
926935
/// attribute macro expansion.
927936
DeclAttributes getSemanticAttrs() const;
@@ -1090,6 +1099,14 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
10901099
Bits.Decl.EscapedFromIfConfig = Escaped;
10911100
}
10921101

1102+
bool getSemanticAttrsComputed() const {
1103+
return Bits.Decl.SemanticAttrsComputed;
1104+
}
1105+
1106+
void setSemanticAttrsComputed(bool Computed) {
1107+
Bits.Decl.SemanticAttrsComputed = Computed;
1108+
}
1109+
10931110
/// \returns the unparsed comment attached to this declaration.
10941111
RawComment getRawComment() const;
10951112

include/swift/AST/TypeCheckRequests.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4600,6 +4600,25 @@ class IsFunctionBodySkippedRequest
46004600
void cacheResult(bool isSkipped) const;
46014601
};
46024602

4603+
class SemanticDeclAttrsRequest
4604+
: public SimpleRequest<SemanticDeclAttrsRequest,
4605+
DeclAttributes(const Decl *),
4606+
RequestFlags::SeparatelyCached> {
4607+
public:
4608+
using SimpleRequest::SimpleRequest;
4609+
4610+
private:
4611+
friend SimpleRequest;
4612+
4613+
DeclAttributes evaluate(Evaluator &evaluator, const Decl *) const;
4614+
4615+
public:
4616+
// Separate caching.
4617+
bool isCached() const { return true; }
4618+
llvm::Optional<DeclAttributes> getCachedResult() const;
4619+
void cacheResult(DeclAttributes) const;
4620+
};
4621+
46034622
#define SWIFT_TYPEID_ZONE TypeChecker
46044623
#define SWIFT_TYPEID_HEADER "swift/AST/TypeCheckerTypeIDZone.def"
46054624
#include "swift/Basic/DefineTypeIDZone.h"

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,3 +523,6 @@ SWIFT_REQUEST(TypeChecker, IsFunctionBodySkippedRequest,
523523
SWIFT_REQUEST(TypeChecker, IsCCompatibleFuncDeclRequest,
524524
bool(const FuncDecl *),
525525
Cached, NoLocationInfo)
526+
SWIFT_REQUEST(TypeChecker, SemanticDeclAttrsRequest,
527+
DeclAttributes(const Decl *),
528+
Cached, NoLocationInfo)

lib/AST/Decl.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,12 +375,18 @@ OrigDeclAttributes Decl::getOriginalAttrs() const {
375375
return OrigDeclAttributes(getAttrs(), this);
376376
}
377377

378-
DeclAttributes Decl::getSemanticAttrs() const {
378+
DeclAttributes::AttributeKindRange<CustomAttr, false>
379+
Decl::getSemanticCustomAttrs() const {
379380
auto mutableThis = const_cast<Decl *>(this);
380381
(void)evaluateOrDefault(getASTContext().evaluator,
381-
ExpandMemberAttributeMacros{mutableThis},
382-
{ });
382+
ExpandMemberAttributeMacros{mutableThis}, {});
383+
384+
return getAttrs().getAttributes<CustomAttr>();
385+
}
383386

387+
DeclAttributes Decl::getSemanticAttrs() const {
388+
(void)evaluateOrDefault(getASTContext().evaluator,
389+
SemanticDeclAttrsRequest{this}, {});
384390
return getAttrs();
385391
}
386392

@@ -441,7 +447,7 @@ void Decl::visitAuxiliaryDecls(
441447

442448
void Decl::forEachAttachedMacro(MacroRole role,
443449
MacroCallback macroCallback) const {
444-
for (auto customAttrConst : getSemanticAttrs().getAttributes<CustomAttr>()) {
450+
for (auto customAttrConst : getSemanticCustomAttrs()) {
445451
auto customAttr = const_cast<CustomAttr *>(customAttrConst);
446452
auto *macroDecl = getResolvedMacro(customAttr);
447453

lib/AST/NameLookup.cpp

Lines changed: 1 addition & 1 deletion
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/AST/TypeCheckRequests.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2062,3 +2062,28 @@ void ExpandPeerMacroRequest::noteCycleStep(DiagnosticEngine &diags) const {
20622062
"peer");
20632063
}
20642064
}
2065+
2066+
//----------------------------------------------------------------------------//
2067+
// SemanticDeclAttrsRequest computation.
2068+
//----------------------------------------------------------------------------//
2069+
2070+
DeclAttributes SemanticDeclAttrsRequest::evaluate(Evaluator &evaluator,
2071+
const Decl *decl) const {
2072+
auto mutableDecl = const_cast<Decl *>(decl);
2073+
(void)evaluateOrDefault(evaluator, ExpandMemberAttributeMacros{mutableDecl},
2074+
{});
2075+
return decl->getAttrs();
2076+
}
2077+
2078+
llvm::Optional<DeclAttributes>
2079+
SemanticDeclAttrsRequest::getCachedResult() const {
2080+
auto decl = std::get<0>(getStorage());
2081+
if (decl->getSemanticAttrsComputed())
2082+
return decl->getAttrs();
2083+
return llvm::None;
2084+
}
2085+
2086+
void SemanticDeclAttrsRequest::cacheResult(DeclAttributes attrs) const {
2087+
auto decl = std::get<0>(getStorage());
2088+
const_cast<Decl *>(decl)->setSemanticAttrsComputed(true);
2089+
}

lib/Sema/TypeCheckAccess.cpp

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 2 deletions
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)