Skip to content

Commit 2ba37da

Browse files
committed
AST: Refactor AvailabilityContext::PlatformInfo operations.
1 parent 72615fe commit 2ba37da

File tree

3 files changed

+32
-33
lines changed

3 files changed

+32
-33
lines changed

include/swift/AST/AvailabilityContext.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ class AvailabilityContext {
7575
void constrainWithPlatformRange(const AvailabilityRange &platformRange,
7676
ASTContext &ctx);
7777

78-
/// Constrain the platform availability range with both the availability
79-
/// attributes of `decl` and with `platformRange`.
78+
/// Constrain with the availability attributes of `decl`, intersecting the
79+
/// platform range of `decl` with `platformRange`.
8080
void
81-
constrainWithDeclAndPlatformRange(Decl *decl,
81+
constrainWithDeclAndPlatformRange(const Decl *decl,
8282
const AvailabilityRange &platformRange);
8383

8484
/// Returns true if `other` is as available or is more available.

include/swift/AST/AvailabilityContextStorage.h

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ struct AvailabilityContext::PlatformInfo {
4242

4343
/// Sets `Range` to `other` if `other` is more restrictive. Returns true if
4444
/// any property changed as a result of adding this constraint.
45+
/// Updates each field to reflect the availability of `decl`, if that
46+
/// availability is more restrictive. Return true if any field was updated.
47+
bool constrainWith(const Decl *decl);
48+
4549
bool constrainRange(const AvailabilityRange &other) {
4650
if (!other.isContainedIn(Range))
4751
return false;
@@ -50,19 +54,9 @@ struct AvailabilityContext::PlatformInfo {
5054
return true;
5155
}
5256

53-
/// Sets `Range` to the platform introduction range of `decl` if that range
54-
/// is more restrictive. Returns true if
55-
/// any property changed as a result of adding this constraint.
56-
bool constrainRange(const Decl *decl);
57-
58-
/// Updates `UnavailablePlatform` and `IsUnavailable` to reflect the status
59-
/// of `decl` if its platform unavailability is more restrictive. Returns
60-
/// true if any property changed as a result of adding this constraint.
61-
bool constrainUnavailability(const Decl *decl);
57+
bool constrainUnavailability(std::optional<PlatformKind> unavailablePlatform);
6258

63-
/// If `decl` is deprecated, sets `IsDeprecated` to true. Returns true if
64-
/// any property changed as a result of adding this constraint.
65-
bool constrainDeprecated(const Decl *decl);
59+
bool constrainDeprecated(bool deprecated);
6660

6761
/// Returns true if `other` is as available or is more available.
6862
bool isContainedIn(const PlatformInfo &other) const;

lib/AST/AvailabilityContext.cpp

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,44 @@
1818

1919
using namespace swift;
2020

21-
bool AvailabilityContext::PlatformInfo::constrainRange(const Decl *decl) {
21+
bool AvailabilityContext::PlatformInfo::constrainWith(const Decl *decl) {
22+
bool isConstrained = false;
23+
auto &ctx = decl->getASTContext();
24+
2225
if (auto range = AvailabilityInference::annotatedAvailableRange(decl))
23-
return constrainRange(*range);
26+
isConstrained |= constrainRange(*range);
27+
28+
if (auto *attr = decl->getAttrs().getUnavailable(ctx))
29+
isConstrained |= constrainUnavailability(attr->Platform);
2430

25-
return false;
31+
if (!IsDeprecated)
32+
isConstrained |= constrainDeprecated(decl->getAttrs().isDeprecated(ctx));
33+
34+
return isConstrained;
2635
}
2736

2837
bool AvailabilityContext::PlatformInfo::constrainUnavailability(
29-
const Decl *decl) {
30-
auto &ctx = decl->getASTContext();
31-
auto *attr = decl->getAttrs().getUnavailable(ctx);
32-
if (!attr)
38+
std::optional<PlatformKind> unavailablePlatform) {
39+
if (!unavailablePlatform)
3340
return false;
3441

3542
// Check whether the decl's unavailability reason is the same.
36-
if (IsUnavailable && UnavailablePlatform == attr->Platform)
43+
if (IsUnavailable && UnavailablePlatform == *unavailablePlatform)
3744
return false;
3845

3946
// Check whether the decl's unavailability reason is more specific.
40-
if (attr->Platform != PlatformKind::none &&
41-
inheritsAvailabilityFromPlatform(attr->Platform, UnavailablePlatform))
47+
if (*unavailablePlatform != PlatformKind::none &&
48+
inheritsAvailabilityFromPlatform(*unavailablePlatform,
49+
UnavailablePlatform))
4250
return false;
4351

4452
IsUnavailable = true;
45-
UnavailablePlatform = attr->Platform;
53+
UnavailablePlatform = *unavailablePlatform;
4654
return true;
4755
}
4856

49-
bool AvailabilityContext::PlatformInfo::constrainDeprecated(const Decl *decl) {
50-
auto &ctx = decl->getASTContext();
51-
if (IsDeprecated || !decl->getAttrs().isDeprecated(ctx))
57+
bool AvailabilityContext::PlatformInfo::constrainDeprecated(bool deprecated) {
58+
if (IsDeprecated || !deprecated)
5259
return false;
5360

5461
IsDeprecated = true;
@@ -126,13 +133,11 @@ void AvailabilityContext::constrainWithPlatformRange(
126133
}
127134

128135
void AvailabilityContext::constrainWithDeclAndPlatformRange(
129-
Decl *decl, const AvailabilityRange &platformRange) {
136+
const Decl *decl, const AvailabilityRange &platformRange) {
130137
PlatformInfo platformAvailability{Info->Platform};
131138
bool isConstrained = false;
132-
isConstrained |= platformAvailability.constrainRange(decl);
139+
isConstrained |= platformAvailability.constrainWith(decl);
133140
isConstrained |= platformAvailability.constrainRange(platformRange);
134-
isConstrained |= platformAvailability.constrainUnavailability(decl);
135-
isConstrained |= platformAvailability.constrainDeprecated(decl);
136141

137142
if (!isConstrained)
138143
return;

0 commit comments

Comments
 (0)