Skip to content

Commit ea56972

Browse files
committed
AST: Refactor constraining operations on AvailabilityContext.
1 parent 0f74065 commit ea56972

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

include/swift/AST/AvailabilityContextStorage.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,8 @@ struct AvailabilityContext::PlatformInfo {
4949
/// availability is more restrictive. Returns true if any field was updated.
5050
bool constrainWith(const Decl *decl);
5151

52-
bool constrainRange(const AvailabilityRange &other) {
53-
if (!other.isContainedIn(Range))
54-
return false;
55-
56-
Range = other;
57-
return true;
58-
}
59-
6052
bool constrainUnavailability(std::optional<PlatformKind> unavailablePlatform);
6153

62-
bool constrainDeprecated(bool deprecated);
63-
6454
/// Returns true if `other` is as available or is more available.
6555
bool isContainedIn(const PlatformInfo &other) const;
6656

lib/AST/AvailabilityContext.cpp

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,32 @@
1818

1919
using namespace swift;
2020

21+
// Defined as a macro because you can't take the reference of a bitfield.
22+
#define CONSTRAIN_BOOL(_old, _new) \
23+
[&]() { \
24+
if (_old || !_new) \
25+
return false; \
26+
_old = true; \
27+
return true; \
28+
}()
29+
30+
static bool constrainRange(AvailabilityRange &existing,
31+
const AvailabilityRange &other) {
32+
if (!other.isContainedIn(existing))
33+
return false;
34+
35+
existing = other;
36+
return true;
37+
}
38+
2139
bool AvailabilityContext::PlatformInfo::constrainWith(
2240
const PlatformInfo &other) {
2341
bool isConstrained = false;
24-
isConstrained |= constrainRange(other.Range);
42+
isConstrained |= constrainRange(Range, other.Range);
2543
if (other.IsUnavailable) {
2644
isConstrained |= constrainUnavailability(other.UnavailablePlatform);
2745
}
28-
isConstrained |= constrainDeprecated(other.IsDeprecated);
46+
isConstrained |= CONSTRAIN_BOOL(IsDeprecated, other.IsDeprecated);
2947

3048
return isConstrained;
3149
}
@@ -35,13 +53,13 @@ bool AvailabilityContext::PlatformInfo::constrainWith(const Decl *decl) {
3553
auto &ctx = decl->getASTContext();
3654

3755
if (auto range = AvailabilityInference::annotatedAvailableRange(decl))
38-
isConstrained |= constrainRange(*range);
56+
isConstrained |= constrainRange(Range, *range);
3957

4058
if (auto *attr = decl->getAttrs().getUnavailable(ctx))
4159
isConstrained |= constrainUnavailability(attr->Platform);
4260

43-
if (!IsDeprecated)
44-
isConstrained |= constrainDeprecated(decl->getAttrs().isDeprecated(ctx));
61+
isConstrained |=
62+
CONSTRAIN_BOOL(IsDeprecated, decl->getAttrs().isDeprecated(ctx));
4563

4664
return isConstrained;
4765
}
@@ -72,14 +90,6 @@ bool AvailabilityContext::PlatformInfo::constrainUnavailability(
7290
return true;
7391
}
7492

75-
bool AvailabilityContext::PlatformInfo::constrainDeprecated(bool deprecated) {
76-
if (IsDeprecated || !deprecated)
77-
return false;
78-
79-
IsDeprecated = true;
80-
return true;
81-
}
82-
8393
bool AvailabilityContext::PlatformInfo::isContainedIn(
8494
const PlatformInfo &other) const {
8595
if (!Range.isContainedIn(other.Range))
@@ -156,7 +166,7 @@ void AvailabilityContext::constrainWithDecl(const Decl *decl) {
156166
void AvailabilityContext::constrainWithPlatformRange(
157167
const AvailabilityRange &platformRange, ASTContext &ctx) {
158168
PlatformInfo platformAvailability{Info->Platform};
159-
if (!platformAvailability.constrainRange(platformRange))
169+
if (!constrainRange(platformAvailability.Range, platformRange))
160170
return;
161171

162172
Info = Storage::get(platformAvailability, ctx);
@@ -167,7 +177,7 @@ void AvailabilityContext::constrainWithDeclAndPlatformRange(
167177
PlatformInfo platformAvailability{Info->Platform};
168178
bool isConstrained = false;
169179
isConstrained |= platformAvailability.constrainWith(decl);
170-
isConstrained |= platformAvailability.constrainRange(platformRange);
180+
isConstrained |= constrainRange(platformAvailability.Range, platformRange);
171181

172182
if (!isConstrained)
173183
return;

0 commit comments

Comments
 (0)