Skip to content

Commit 413c673

Browse files
committed
[NFC] Requestify ABI role computation
1 parent 9c1e00c commit 413c673

File tree

7 files changed

+90
-45
lines changed

7 files changed

+90
-45
lines changed

include/swift/AST/ASTContext.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -415,19 +415,6 @@ class ASTContext final {
415415
llvm::SmallPtrSet<DerivativeAttr *, 1>>
416416
DerivativeAttrs;
417417

418-
/// For each ABI-only declaration (created with the `@abi` attribute), points
419-
/// to its API-only counterpart. That is, this table maps each
420-
/// `ABIAttr::abiDecl` to the declaration the `ABIAttr` is attached to.
421-
///
422-
/// \seeAlso \c recordABIAttr()
423-
llvm::DenseMap<Decl *, Decl *> ABIDeclCounterparts;
424-
425-
/// Register \c this->abiDecl 's relationship with \p owner in
426-
/// `ABIDeclCounterparts` . This is necessary for
427-
/// \c ABIRoleInfo::ABIRoleInfo() to determine that \c this->abiDecl
428-
/// is ABI-only and locate its API counterpart.
429-
void recordABIAttr(ABIAttr *attr, Decl *owner);
430-
431418
/// The Swift module currently being compiled.
432419
ModuleDecl *MainModule = nullptr;
433420

include/swift/AST/Decl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,12 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl>, public Swi
10231023
/// attribute macro expansion.
10241024
DeclAttributes getSemanticAttrs() const;
10251025

1026+
/// Register the relationship between \c this and \p attr->abiDecl , assuming
1027+
/// that \p attr is attached to \c this . This is necessary for
1028+
/// \c ABIRoleInfo::ABIRoleInfo() to determine that \c attr->abiDecl
1029+
/// is ABI-only and locate its API counterpart.
1030+
void recordABIAttr(ABIAttr *attr);
1031+
10261032
/// Set this declaration's attributes to the specified attribute list,
10271033
/// applying any post-processing logic appropriate for attributes parsed
10281034
/// from source code.

include/swift/AST/NameLookupRequests.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "swift/AST/TypeOrExtensionDecl.h"
2626
#include "swift/Basic/Statistic.h"
2727
#include "llvm/ADT/Hashing.h"
28+
#include "llvm/ADT/PointerIntPair.h"
2829
#include "llvm/ADT/TinyPtrVector.h"
2930

3031
namespace swift {
@@ -1002,6 +1003,32 @@ class ObjCCategoryNameMapRequest
10021003
bool isCached() const { return true; }
10031004
};
10041005

1006+
struct DeclABIRoleInfoResult {
1007+
llvm::PointerIntPair<Decl *, 2, uint8_t> storage;
1008+
DeclABIRoleInfoResult(Decl *counterpart, uint8_t roleValue)
1009+
: storage(counterpart, roleValue) {}
1010+
};
1011+
1012+
/// Return the ABI role info (role and counterpart) of a given declaration.
1013+
class DeclABIRoleInfoRequest
1014+
: public SimpleRequest<DeclABIRoleInfoRequest,
1015+
DeclABIRoleInfoResult(Decl *),
1016+
RequestFlags::Cached> {
1017+
public:
1018+
using SimpleRequest::SimpleRequest;
1019+
1020+
void recordABIOnly(Evaluator &evaluator, Decl *counterpart);
1021+
1022+
private:
1023+
friend SimpleRequest;
1024+
1025+
// Evaluation.
1026+
DeclABIRoleInfoResult evaluate(Evaluator &evaluator, Decl *decl) const;
1027+
1028+
public:
1029+
bool isCached() const { return true; }
1030+
};
1031+
10051032
#define SWIFT_TYPEID_ZONE NameLookup
10061033
#define SWIFT_TYPEID_HEADER "swift/AST/NameLookupTypeIDZone.def"
10071034
#include "swift/Basic/DefineTypeIDZone.h"

include/swift/AST/NameLookupTypeIDZone.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,5 @@ SWIFT_REQUEST(NameLookup, LookupIntrinsicRequest,
118118
FuncDecl *(ModuleDecl *, Identifier), Cached, NoLocationInfo)
119119
SWIFT_REQUEST(NameLookup, ObjCCategoryNameMapRequest,
120120
ObjCCategoryNameMap(ClassDecl *, ExtensionDecl *), Cached, NoLocationInfo)
121+
SWIFT_REQUEST(NameLookup, DeclABIRoleInfoRequest,
122+
DeclABIRoleInfoResult(Decl *), Cached, NoLocationInfo)

lib/AST/Attr.cpp

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2899,30 +2899,6 @@ LifetimeAttr *LifetimeAttr::create(ASTContext &context, SourceLoc atLoc,
28992899
return new (context) LifetimeAttr(atLoc, baseRange, implicit, entry);
29002900
}
29012901

2902-
void ASTContext::recordABIAttr(ABIAttr *attr, Decl *owner) {
2903-
// The ABIAttr on a VarDecl ought to point to its PBD.
2904-
if (auto VD = dyn_cast<VarDecl>(owner)) {
2905-
if (auto PBD = VD->getParentPatternBinding()) {
2906-
owner = PBD;
2907-
}
2908-
}
2909-
2910-
auto recordABIDecl = [&](Decl *decl) {
2911-
ABIDeclCounterparts.insert({ decl, owner });
2912-
};
2913-
2914-
if (auto abiPBD = dyn_cast<PatternBindingDecl>(attr->abiDecl)) {
2915-
// Add to *every* VarDecl in the ABI PBD, even ones that don't properly
2916-
// match anything in the API PBD.
2917-
for (auto i : range(abiPBD->getNumPatternEntries())) {
2918-
abiPBD->getPattern(i)->forEachVariable(recordABIDecl);
2919-
}
2920-
return;
2921-
}
2922-
2923-
recordABIDecl(attr->abiDecl);
2924-
}
2925-
29262902
void swift::simple_display(llvm::raw_ostream &out, const DeclAttribute *attr) {
29272903
if (attr)
29282904
attr->print(out);

lib/AST/Decl.cpp

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ void Decl::attachParsedAttrs(DeclAttributes attrs) {
416416
for (auto *attr : attrs.getAttributes<DerivativeAttr>())
417417
attr->setOriginalDeclaration(this);
418418
for (auto attr : attrs.getAttributes<ABIAttr, /*AllowInvalid=*/true>())
419-
this->getASTContext().recordABIAttr(attr, this);
419+
recordABIAttr(attr);
420420

421421
getAttrs() = attrs;
422422
}
@@ -4271,22 +4271,69 @@ void ValueDecl::setRenamedDecl(const AvailableAttr *attr,
42714271
std::move(renameDecl));
42724272
}
42734273

4274-
abi_role_detail::Storage abi_role_detail::computeStorage(Decl *decl) {
4275-
ASSERT(decl);
4274+
void Decl::recordABIAttr(ABIAttr *attr) {
4275+
Decl *owner = this;
42764276

4277-
auto &ctx = decl->getASTContext();
4277+
// The ABIAttr on a VarDecl ought to point to its PBD.
4278+
if (auto VD = dyn_cast<VarDecl>(owner)) {
4279+
if (auto PBD = VD->getParentPatternBinding()) {
4280+
owner = PBD;
4281+
}
4282+
}
4283+
4284+
auto record = [&](Decl *decl) {
4285+
auto &evaluator = owner->getASTContext().evaluator;
4286+
DeclABIRoleInfoRequest(decl).recordABIOnly(evaluator, owner);
4287+
};
4288+
4289+
if (auto abiPBD = dyn_cast<PatternBindingDecl>(attr->abiDecl)) {
4290+
// Add to *every* VarDecl in the ABI PBD, even ones that don't properly
4291+
// match anything in the API PBD.
4292+
for (auto i : range(abiPBD->getNumPatternEntries())) {
4293+
abiPBD->getPattern(i)->forEachVariable(record);
4294+
}
4295+
return;
4296+
}
4297+
4298+
record(attr->abiDecl);
4299+
}
4300+
4301+
void DeclABIRoleInfoRequest::recordABIOnly(Evaluator &evaluator,
4302+
Decl *counterpart) {
4303+
if (evaluator.hasCachedResult(*this))
4304+
return;
4305+
DeclABIRoleInfoResult result{counterpart, ABIRole::ProvidesABI};
4306+
evaluator.cacheOutput(*this, std::move(result));
4307+
}
4308+
4309+
DeclABIRoleInfoResult
4310+
DeclABIRoleInfoRequest::evaluate(Evaluator &evaluator, Decl *decl) const {
4311+
// NOTE: ABI decl -> API decl is manually cached through `recordABIOnly()`,
4312+
// so this code does not have to handle that case.
4313+
4314+
ASSERT(decl);
42784315

42794316
Decl *counterpartDecl = decl;
42804317
ABIRole::Value flags = ABIRole::Either;
42814318

42824319
if (auto attr = decl->getAttrs().getAttribute<ABIAttr>()) {
42834320
flags = ABIRole::ProvidesAPI;
42844321
counterpartDecl = attr->abiDecl;
4285-
} else if (auto inverse = ctx.ABIDeclCounterparts.lookup(decl)) {
4286-
flags = ABIRole::ProvidesABI;
4287-
counterpartDecl = inverse;
42884322
}
42894323

4324+
return DeclABIRoleInfoResult(counterpartDecl, (uint8_t)flags);
4325+
}
4326+
4327+
abi_role_detail::Storage abi_role_detail::computeStorage(Decl *decl) {
4328+
ASSERT(decl);
4329+
4330+
auto &ctx = decl->getASTContext();
4331+
4332+
auto result = evaluateOrDefault(ctx.evaluator, DeclABIRoleInfoRequest{decl},
4333+
{ decl, ABIRole::Either });
4334+
Decl *counterpartDecl = result.storage.getPointer();
4335+
auto flags = (ABIRole::Value)result.storage.getInt();
4336+
42904337
// If we did find an `@abi` attribute, resolve PBD pointers to their VarDecl.
42914338
if (flags != ABIRole::Either) {
42924339
if (auto VD = dyn_cast<VarDecl>(decl))

lib/Serialization/Deserialization.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6513,7 +6513,7 @@ llvm::Error DeclDeserializer::finishRecursiveAttrs(Decl *decl, DeclAttribute *at
65136513
if (!abiDeclOrError)
65146514
return abiDeclOrError.takeError();
65156515
unresolvedABIAttr->first->abiDecl = abiDeclOrError.get();
6516-
ctx.recordABIAttr(unresolvedABIAttr->first, decl);
6516+
decl->recordABIAttr(unresolvedABIAttr->first);
65176517
}
65186518
if (ABIDeclCounterpartID != 0) {
65196519
// This decl is the `abiDecl` of an `ABIAttr`. Force the decl that `ABIAttr`

0 commit comments

Comments
 (0)