Skip to content

Commit a64f705

Browse files
committed
AST: Introduce SemanticDeclAttrsRequest.
This request will populate a decl's attrs list with any semantic attributes that were not written in source.
1 parent e19954f commit a64f705

File tree

5 files changed

+61
-6
lines changed

5 files changed

+61
-6
lines changed

include/swift/AST/Decl.h

Lines changed: 13 additions & 2 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,
@@ -1090,6 +1093,14 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
10901093
Bits.Decl.EscapedFromIfConfig = Escaped;
10911094
}
10921095

1096+
bool getSemanticAttrsComputed() const {
1097+
return Bits.Decl.SemanticAttrsComputed;
1098+
}
1099+
1100+
void setSemanticAttrsComputed(bool Computed) {
1101+
Bits.Decl.SemanticAttrsComputed = Computed;
1102+
}
1103+
10931104
/// \returns the unparsed comment attached to this declaration.
10941105
RawComment getRawComment() const;
10951106

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: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,11 +376,8 @@ OrigDeclAttributes Decl::getOriginalAttrs() const {
376376
}
377377

378378
DeclAttributes Decl::getSemanticAttrs() const {
379-
auto mutableThis = const_cast<Decl *>(this);
380379
(void)evaluateOrDefault(getASTContext().evaluator,
381-
ExpandMemberAttributeMacros{mutableThis},
382-
{ });
383-
380+
SemanticDeclAttrsRequest{this}, {});
384381
return getAttrs();
385382
}
386383

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+
}

0 commit comments

Comments
 (0)