Skip to content

Commit 217311f

Browse files
authored
Merge pull request #78637 from tshortli/remove-domain-storage-from-semantic-available-attr
AST: Remove inline AvailabilityDomain from SemanticAvailableAttr
2 parents d864b41 + afc5dc9 commit 217311f

File tree

4 files changed

+47
-13
lines changed

4 files changed

+47
-13
lines changed

include/swift/AST/Attr.h

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,13 @@ class AvailableAttr : public DeclAttribute {
797797
/// resolved successfully.
798798
std::optional<AvailabilityDomain> getCachedDomain() const { return Domain; }
799799

800+
/// Returns true if the `AvailabilityDomain` associated with the attribute
801+
/// has been resolved successfully.
802+
bool hasCachedDomain() const {
803+
// For now, domains are always set on construction of the attribute.
804+
return true;
805+
}
806+
800807
/// Returns the kind of availability the attribute specifies.
801808
Kind getKind() const { return static_cast<Kind>(Bits.AvailableAttr.Kind); }
802809

@@ -3189,20 +3196,24 @@ class ParsedDeclAttributes {
31893196
}
31903197
};
31913198

3192-
/// A wrapper for `AvailableAttr` that is enriched with additional semantic
3193-
/// informaton, like its corresponding `AvailabilityDomain`.
3199+
/// A wrapper for `AvailableAttr` that enriches it with additional semantic
3200+
/// informaton that can only be determined using the `AvailabilityDomain`
3201+
/// associated with the attribute. A `SemanticAvailableAttr` can only be
3202+
/// constructed with an `AvailableAttr` that has a resolved
3203+
/// `AvailabilityDomain`.
31943204
class SemanticAvailableAttr final {
31953205
const AvailableAttr *attr;
3196-
AvailabilityDomain domain;
31973206

31983207
public:
3199-
SemanticAvailableAttr(const AvailableAttr *attr, AvailabilityDomain domain)
3200-
: attr(attr), domain(domain) {
3208+
SemanticAvailableAttr(const AvailableAttr *attr) : attr(attr) {
32013209
assert(attr);
3210+
assert(attr->hasCachedDomain());
32023211
}
32033212

32043213
const AvailableAttr *getParsedAttr() const { return attr; }
3205-
const AvailabilityDomain getDomain() const { return domain; }
3214+
const AvailabilityDomain getDomain() const {
3215+
return attr->getCachedDomain().value();
3216+
}
32063217

32073218
/// The version tuple written in source for the `introduced:` component.
32083219
std::optional<llvm::VersionTuple> getIntroduced() const {
@@ -3640,6 +3651,32 @@ struct EnumTraits<TypeAttrKind> {
36403651

36413652
} // end namespace swift
36423653

3654+
namespace llvm {
3655+
using swift::AvailableAttr;
3656+
using swift::SemanticAvailableAttr;
3657+
3658+
// A SemanticAvailableAttr just wraps an `AvailableAttr *` and is therefore
3659+
// "pointer like".
3660+
template <typename T>
3661+
struct PointerLikeTypeTraits;
3662+
template <>
3663+
struct PointerLikeTypeTraits<SemanticAvailableAttr> {
3664+
public:
3665+
static inline void *getAsVoidPointer(SemanticAvailableAttr attr) {
3666+
return reinterpret_cast<void *>(
3667+
const_cast<AvailableAttr *>(attr.getParsedAttr()));
3668+
}
3669+
static inline SemanticAvailableAttr getFromVoidPointer(void *P) {
3670+
return SemanticAvailableAttr(static_cast<AvailableAttr *>(P));
3671+
}
3672+
enum {
3673+
NumLowBitsAvailable =
3674+
PointerLikeTypeTraits<AvailableAttr *>::NumLowBitsAvailable
3675+
};
3676+
};
3677+
3678+
} // end namespace llvm
3679+
36433680
#undef UNIMPLEMENTED_CLONE
36443681

36453682
#endif

lib/AST/Attr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@ ParsedDeclAttrFilter::operator()(const DeclAttribute *Attr) const {
918918
}
919919

920920
bool SemanticAvailableAttr::isActive(ASTContext &ctx) const {
921-
return domain.isActive(ctx);
921+
return getDomain().isActive(ctx);
922922
}
923923

924924
std::optional<SemanticAvailableAttr>

lib/AST/Availability.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -447,9 +447,7 @@ Decl::getSemanticAvailableAttrs(bool includeInactive) const {
447447

448448
std::optional<SemanticAvailableAttr>
449449
Decl::getSemanticAvailableAttr(const AvailableAttr *attr) const {
450-
if (auto domain = attr->getCachedDomain())
451-
return SemanticAvailableAttr(attr, *domain);
452-
return std::nullopt;
450+
return SemanticAvailableAttr(attr);
453451
}
454452

455453
std::optional<SemanticAvailableAttr>
@@ -790,7 +788,7 @@ bool AvailabilityInference::isAvailableAsSPI(const Decl *D) {
790788

791789
AvailabilityRange
792790
SemanticAvailableAttr::getIntroducedRange(const ASTContext &Ctx) const {
793-
assert(domain.isActive(Ctx));
791+
assert(getDomain().isActive(Ctx));
794792

795793
auto *attr = getParsedAttr();
796794
if (!attr->Introduced.has_value())

lib/Serialization/Serialization.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3058,9 +3058,8 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
30583058
ENCODE_VER_TUPLE(Obsoleted, theAttr->Obsoleted)
30593059

30603060
assert(theAttr->Rename.empty() || !theAttr->hasCachedRenamedDecl());
3061-
3061+
assert(theAttr->hasCachedDomain());
30623062
auto domain = theAttr->getCachedDomain();
3063-
assert(domain);
30643063

30653064
// FIXME: [availability] Serialize domain and kind directly.
30663065
llvm::SmallString<32> blob;

0 commit comments

Comments
 (0)