Skip to content

Commit 2e03ba2

Browse files
committed
AST: Store Platform and PlatformAgnostic in AvailableAttr's bits.
1 parent 36230cd commit 2e03ba2

File tree

2 files changed

+31
-30
lines changed

2 files changed

+31
-30
lines changed

include/swift/AST/Attr.h

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,13 @@ class DeclAttribute : public AttributeBase {
147147
Value : 32
148148
);
149149

150-
SWIFT_INLINE_BITFIELD(AvailableAttr, DeclAttribute, 1+1,
150+
SWIFT_INLINE_BITFIELD(AvailableAttr, DeclAttribute, 8+8+1+1,
151+
/// A `PlatformKind` value.
152+
Platform : 8,
153+
154+
/// A `PlatformAgnosticAvailabilityKind` value.
155+
PlatformAgnostic : 8,
156+
151157
/// Whether this attribute was spelled `@_spi_available`.
152158
IsSPI : 1,
153159

@@ -700,7 +706,7 @@ enum class AvailableVersionComparison {
700706
};
701707

702708
/// Describes the platform-agnostic availability of a declaration.
703-
enum class PlatformAgnosticAvailabilityKind {
709+
enum class PlatformAgnosticAvailabilityKind : uint8_t {
704710
/// The associated availability attribute is not platform-agnostic.
705711
None,
706712
/// The declaration is deprecated, but can still be used.
@@ -768,14 +774,6 @@ class AvailableAttr : public DeclAttribute {
768774
/// Indicates where the Obsoleted version was specified.
769775
const SourceRange ObsoletedRange;
770776

771-
private:
772-
/// Indicates if the declaration has platform-agnostic availability.
773-
const PlatformAgnosticAvailabilityKind PlatformAgnostic;
774-
775-
/// The platform that the attribute applies to (may be `none`).
776-
const PlatformKind Platform;
777-
778-
public:
779777
/// Whether this is a language-version-specific entity.
780778
bool isLanguageVersionSpecific() const;
781779

@@ -798,11 +796,14 @@ class AvailableAttr : public DeclAttribute {
798796
bool isForEmbedded() const { return Bits.AvailableAttr.IsForEmbedded; }
799797

800798
/// Returns the platform that the attribute applies to (may be `none`).
801-
PlatformKind getPlatform() const { return Platform; }
799+
PlatformKind getPlatform() const {
800+
return static_cast<PlatformKind>(Bits.AvailableAttr.Platform);
801+
}
802802

803803
/// Returns the platform-agnostic availability.
804804
PlatformAgnosticAvailabilityKind getPlatformAgnosticAvailability() const {
805-
return PlatformAgnostic;
805+
return static_cast<PlatformAgnosticAvailabilityKind>(
806+
Bits.AvailableAttr.PlatformAgnostic);
806807
}
807808

808809
/// Determine if a given declaration should be considered unavailable given
@@ -813,18 +814,16 @@ class AvailableAttr : public DeclAttribute {
813814

814815
/// Returns true if the availability applies to a specific
815816
/// platform.
816-
bool hasPlatform() const {
817-
return Platform != PlatformKind::none;
818-
}
817+
bool hasPlatform() const { return getPlatform() != PlatformKind::none; }
819818

820819
/// Returns the string for the platform of the attribute.
821820
StringRef platformString() const {
822-
return swift::platformString(Platform);
821+
return swift::platformString(getPlatform());
823822
}
824823

825824
/// Returns the human-readable string for the platform of the attribute.
826825
StringRef prettyPlatformString() const {
827-
return swift::prettyPlatformString(Platform);
826+
return swift::prettyPlatformString(getPlatform());
828827
}
829828

830829
/// Returns true if this attribute is active given the current platform.

lib/AST/Attr.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2234,15 +2234,16 @@ AvailableAttr::AvailableAttr(
22342234
Message(Message), Rename(Rename), RenameDecl(RenameDecl),
22352235
INIT_VER_TUPLE(Introduced), IntroducedRange(IntroducedRange),
22362236
INIT_VER_TUPLE(Deprecated), DeprecatedRange(DeprecatedRange),
2237-
INIT_VER_TUPLE(Obsoleted), ObsoletedRange(ObsoletedRange),
2238-
PlatformAgnostic(PlatformAgnostic), Platform(Platform) {
2237+
INIT_VER_TUPLE(Obsoleted), ObsoletedRange(ObsoletedRange) {
2238+
Bits.AvailableAttr.Platform = static_cast<uint8_t>(Platform);
2239+
Bits.AvailableAttr.PlatformAgnostic = static_cast<uint8_t>(PlatformAgnostic);
22392240
Bits.AvailableAttr.IsSPI = IsSPI;
22402241

22412242
if (IsForEmbedded) {
22422243
// FIXME: The IsForEmbedded bit should be removed when library availability
22432244
// conditions are implemented (rdar://138802876)
22442245
Bits.AvailableAttr.IsForEmbedded = true;
2245-
assert(Platform == PlatformKind::none);
2246+
assert(getPlatform() == PlatformKind::none);
22462247
}
22472248
}
22482249

@@ -2279,7 +2280,7 @@ AvailableAttr *AvailableAttr::createForAlternative(
22792280
}
22802281

22812282
bool AvailableAttr::isActivePlatform(const ASTContext &ctx) const {
2282-
return isPlatformActive(Platform, ctx.LangOpts);
2283+
return isPlatformActive(getPlatform(), ctx.LangOpts);
22832284
}
22842285

22852286
bool BackDeployedAttr::isActivePlatform(const ASTContext &ctx,
@@ -2290,14 +2291,14 @@ bool BackDeployedAttr::isActivePlatform(const ASTContext &ctx,
22902291
AvailableAttr *AvailableAttr::clone(ASTContext &C, bool implicit) const {
22912292
return new (C) AvailableAttr(implicit ? SourceLoc() : AtLoc,
22922293
implicit ? SourceRange() : getRange(),
2293-
Platform, Message, Rename, RenameDecl,
2294+
getPlatform(), Message, Rename, RenameDecl,
22942295
Introduced ? *Introduced : llvm::VersionTuple(),
22952296
implicit ? SourceRange() : IntroducedRange,
22962297
Deprecated ? *Deprecated : llvm::VersionTuple(),
22972298
implicit ? SourceRange() : DeprecatedRange,
22982299
Obsoleted ? *Obsoleted : llvm::VersionTuple(),
22992300
implicit ? SourceRange() : ObsoletedRange,
2300-
PlatformAgnostic,
2301+
getPlatformAgnosticAvailability(),
23012302
implicit,
23022303
isSPI(),
23032304
isForEmbedded());
@@ -2332,10 +2333,10 @@ OriginallyDefinedInAttr *OriginallyDefinedInAttr::clone(ASTContext &C,
23322333
}
23332334

23342335
bool AvailableAttr::isLanguageVersionSpecific() const {
2335-
if (PlatformAgnostic ==
2336+
if (getPlatformAgnosticAvailability() ==
23362337
PlatformAgnosticAvailabilityKind::SwiftVersionSpecific)
23372338
{
2338-
assert(Platform == PlatformKind::none &&
2339+
assert(getPlatform() == PlatformKind::none &&
23392340
(Introduced.has_value() ||
23402341
Deprecated.has_value() ||
23412342
Obsoleted.has_value()));
@@ -2345,10 +2346,10 @@ bool AvailableAttr::isLanguageVersionSpecific() const {
23452346
}
23462347

23472348
bool AvailableAttr::isPackageDescriptionVersionSpecific() const {
2348-
if (PlatformAgnostic ==
2349+
if (getPlatformAgnosticAvailability() ==
23492350
PlatformAgnosticAvailabilityKind::PackageDescriptionVersionSpecific)
23502351
{
2351-
assert(Platform == PlatformKind::none &&
2352+
assert(getPlatform() == PlatformKind::none &&
23522353
(Introduced.has_value() ||
23532354
Deprecated.has_value() ||
23542355
Obsoleted.has_value()));
@@ -2358,7 +2359,7 @@ bool AvailableAttr::isPackageDescriptionVersionSpecific() const {
23582359
}
23592360

23602361
bool AvailableAttr::isUnconditionallyUnavailable() const {
2361-
switch (PlatformAgnostic) {
2362+
switch (getPlatformAgnosticAvailability()) {
23622363
case PlatformAgnosticAvailabilityKind::None:
23632364
case PlatformAgnosticAvailabilityKind::Deprecated:
23642365
case PlatformAgnosticAvailabilityKind::SwiftVersionSpecific:
@@ -2375,7 +2376,7 @@ bool AvailableAttr::isUnconditionallyUnavailable() const {
23752376
}
23762377

23772378
bool AvailableAttr::isUnconditionallyDeprecated() const {
2378-
switch (PlatformAgnostic) {
2379+
switch (getPlatformAgnosticAvailability()) {
23792380
case PlatformAgnosticAvailabilityKind::None:
23802381
case PlatformAgnosticAvailabilityKind::Unavailable:
23812382
case PlatformAgnosticAvailabilityKind::UnavailableInSwift:
@@ -2392,7 +2393,8 @@ bool AvailableAttr::isUnconditionallyDeprecated() const {
23922393
}
23932394

23942395
bool AvailableAttr::isNoAsync() const {
2395-
return PlatformAgnostic == PlatformAgnosticAvailabilityKind::NoAsync;
2396+
return getPlatformAgnosticAvailability() ==
2397+
PlatformAgnosticAvailabilityKind::NoAsync;
23962398
}
23972399

23982400
llvm::VersionTuple AvailableAttr::getActiveVersion(const ASTContext &ctx) const {

0 commit comments

Comments
 (0)