Skip to content

Commit 678b093

Browse files
committed
AST: Request-ify getting the AvailabilityDomain from a ValueDecl.
Cache the result of turning a `ValueDecl` into an `AvailabilityDomain`. Use split caching to make the common case of the decl not representing an availability domain efficient. NFC.
1 parent f96a5e5 commit 678b093

File tree

8 files changed

+73
-10
lines changed

8 files changed

+73
-10
lines changed

include/swift/AST/AvailabilityDomain.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,7 @@ class AvailabilityDomain final {
153153

154154
/// If `decl` represents an availability domain, returns the corresponding
155155
/// `AvailabilityDomain` value. Otherwise, returns `std::nullopt`.
156-
static std::optional<AvailabilityDomain> forCustom(ValueDecl *decl,
157-
const ASTContext &ctx);
156+
static std::optional<AvailabilityDomain> forCustom(ValueDecl *decl);
158157

159158
static AvailabilityDomain forCustom(const CustomAvailabilityDomain *domain) {
160159
return AvailabilityDomain(domain);

include/swift/AST/Decl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2925,6 +2925,10 @@ class ValueDecl : public Decl {
29252925

29262926
/// Whether we've evaluated the ApplyAccessNoteRequest.
29272927
unsigned accessNoteApplied : 1;
2928+
2929+
/// Whether the AvailabilityDomainForDeclRequest request was evaluated and
2930+
/// yielded no availability domain.
2931+
unsigned noAvailabilityDomain : 1;
29282932
} LazySemanticInfo = { };
29292933

29302934
friend class DynamicallyReplacedDeclRequest;
@@ -2938,6 +2942,7 @@ class ValueDecl : public Decl {
29382942
friend class ActorIsolationRequest;
29392943
friend class OpaqueResultTypeRequest;
29402944
friend class ApplyAccessNoteRequest;
2945+
friend class AvailabilityDomainForDeclRequest;
29412946

29422947
friend class Decl;
29432948
SourceLoc getLocFromSource() const { return NameLoc; }

include/swift/AST/TypeCheckRequests.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5424,6 +5424,26 @@ class ModuleHasTypeCheckerPerformanceHacksEnabledRequest
54245424
bool isCached() const { return true; }
54255425
};
54265426

5427+
class AvailabilityDomainForDeclRequest
5428+
: public SimpleRequest<AvailabilityDomainForDeclRequest,
5429+
std::optional<AvailabilityDomain>(ValueDecl *),
5430+
RequestFlags::Cached | RequestFlags::SplitCached> {
5431+
public:
5432+
using SimpleRequest::SimpleRequest;
5433+
5434+
private:
5435+
friend SimpleRequest;
5436+
5437+
// Evaluation.
5438+
std::optional<AvailabilityDomain> evaluate(Evaluator &evaluator,
5439+
ValueDecl *decl) const;
5440+
5441+
public:
5442+
bool isCached() const { return true; }
5443+
std::optional<std::optional<AvailabilityDomain>> getCachedResult() const;
5444+
void cacheResult(std::optional<AvailabilityDomain> domain) const;
5445+
};
5446+
54275447
#define SWIFT_TYPEID_ZONE TypeChecker
54285448
#define SWIFT_TYPEID_HEADER "swift/AST/TypeCheckerTypeIDZone.def"
54295449
#include "swift/Basic/DefineTypeIDZone.h"

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,3 +648,7 @@ SWIFT_REQUEST(TypeChecker, BindExtensionsForIDEInspectionRequest,
648648
SWIFT_REQUEST(TypeChecker, ModuleHasTypeCheckerPerformanceHacksEnabledRequest,
649649
bool(const ModuleDecl *),
650650
Cached, NoLocationInfo)
651+
652+
SWIFT_REQUEST(TypeChecker, AvailabilityDomainForDeclRequest,
653+
std::optional<AvailabilityDomain>(ValueDecl *),
654+
Cached | SplitCached, NoLocationInfo)

lib/AST/AvailabilityDomain.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ getCustomDomainKind(clang::FeatureAvailKind featureAvailKind) {
3939
}
4040

4141
static const CustomAvailabilityDomain *
42-
customDomainForClangDecl(ValueDecl *decl, const ASTContext &ctx) {
42+
customDomainForClangDecl(ValueDecl *decl) {
4343
auto *clangDecl = decl->getClangDecl();
4444
ASSERT(clangDecl);
4545

@@ -57,6 +57,7 @@ customDomainForClangDecl(ValueDecl *decl, const ASTContext &ctx) {
5757
if (featureInfo.second.Kind == clang::FeatureAvailKind::None)
5858
return nullptr;
5959

60+
auto &ctx = decl->getASTContext();
6061
FuncDecl *predicate = nullptr;
6162
if (featureInfo.second.Kind == clang::FeatureAvailKind::Dynamic)
6263
predicate =
@@ -68,12 +69,13 @@ customDomainForClangDecl(ValueDecl *decl, const ASTContext &ctx) {
6869
}
6970

7071
std::optional<AvailabilityDomain>
71-
AvailabilityDomain::forCustom(ValueDecl *decl, const ASTContext &ctx) {
72+
AvailabilityDomainForDeclRequest::evaluate(Evaluator &evaluator,
73+
ValueDecl *decl) const {
7274
if (!decl)
7375
return std::nullopt;
7476

7577
if (decl->hasClangNode()) {
76-
if (auto *customDomain = customDomainForClangDecl(decl, ctx))
78+
if (auto *customDomain = customDomainForClangDecl(decl))
7779
return AvailabilityDomain::forCustom(customDomain);
7880
} else {
7981
// FIXME: [availability] Handle Swift availability domains decls.
@@ -82,6 +84,15 @@ AvailabilityDomain::forCustom(ValueDecl *decl, const ASTContext &ctx) {
8284
return std::nullopt;
8385
}
8486

87+
std::optional<AvailabilityDomain>
88+
AvailabilityDomain::forCustom(ValueDecl *decl) {
89+
if (!decl)
90+
return std::nullopt;
91+
92+
return evaluateOrDefault(decl->getASTContext().evaluator,
93+
AvailabilityDomainForDeclRequest{decl}, {});
94+
}
95+
8596
std::optional<AvailabilityDomain>
8697
AvailabilityDomain::builtinDomainForString(StringRef string,
8798
const DeclContext *declContext) {

lib/AST/TypeCheckRequests.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2837,3 +2837,28 @@ void SemanticAvailableAttrRequest::cacheResult(
28372837
if (!value)
28382838
attr->setInvalid();
28392839
}
2840+
2841+
//----------------------------------------------------------------------------//
2842+
// AvailabilityDomainForDeclRequest computation.
2843+
//----------------------------------------------------------------------------//
2844+
2845+
std::optional<std::optional<AvailabilityDomain>>
2846+
AvailabilityDomainForDeclRequest::getCachedResult() const {
2847+
auto decl = std::get<0>(getStorage());
2848+
2849+
if (decl->LazySemanticInfo.noAvailabilityDomain)
2850+
return std::optional<AvailabilityDomain>();
2851+
return decl->getASTContext().evaluator.getCachedNonEmptyOutput(*this);
2852+
}
2853+
2854+
void AvailabilityDomainForDeclRequest::cacheResult(
2855+
std::optional<AvailabilityDomain> domain) const {
2856+
auto decl = std::get<0>(getStorage());
2857+
2858+
if (!domain) {
2859+
decl->LazySemanticInfo.noAvailabilityDomain = 1;
2860+
return;
2861+
}
2862+
2863+
decl->getASTContext().evaluator.cacheNonEmptyOutput(*this, std::move(domain));
2864+
}

lib/ClangImporter/ClangImporter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4103,7 +4103,7 @@ void ClangModuleUnit::lookupAvailabilityDomains(
41034103
if (!imported)
41044104
return;
41054105

4106-
auto customDomain = AvailabilityDomain::forCustom(imported, ctx);
4106+
auto customDomain = AvailabilityDomain::forCustom(imported);
41074107
ASSERT(customDomain);
41084108
results.push_back(*customDomain);
41094109
}

lib/Serialization/Deserialization.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5824,8 +5824,7 @@ decodeDomainKind(uint8_t kind) {
58245824

58255825
static std::optional<AvailabilityDomain>
58265826
decodeAvailabilityDomain(AvailabilityDomainKind domainKind,
5827-
PlatformKind platformKind, ValueDecl *decl,
5828-
const ASTContext &ctx) {
5827+
PlatformKind platformKind, ValueDecl *decl) {
58295828
switch (domainKind) {
58305829
case AvailabilityDomainKind::Universal:
58315830
return AvailabilityDomain::forUniversal();
@@ -5838,7 +5837,7 @@ decodeAvailabilityDomain(AvailabilityDomainKind domainKind,
58385837
case AvailabilityDomainKind::Platform:
58395838
return AvailabilityDomain::forPlatform(platformKind);
58405839
case AvailabilityDomainKind::Custom:
5841-
return AvailabilityDomain::forCustom(decl, ctx);
5840+
return AvailabilityDomain::forCustom(decl);
58425841
}
58435842
}
58445843

@@ -5905,7 +5904,7 @@ DeclDeserializer::readAvailable_DECL_ATTR(SmallVectorImpl<uint64_t> &scratch,
59055904
}
59065905
}
59075906

5908-
auto domain = decodeAvailabilityDomain(domainKind, platform, domainDecl, ctx);
5907+
auto domain = decodeAvailabilityDomain(domainKind, platform, domainDecl);
59095908
if (!domain)
59105909
return llvm::make_error<InavalidAvailabilityDomainError>();
59115910

0 commit comments

Comments
 (0)