Skip to content

Commit e17c36d

Browse files
authored
Merge pull request #78788 from tshortli/availability-context-gardening
AST: AvailabilityContext gardening
2 parents 0f1e301 + 0b5d787 commit e17c36d

File tree

4 files changed

+58
-52
lines changed

4 files changed

+58
-52
lines changed

include/swift/AST/AvailabilityContext.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@ class AvailabilityContext {
3838
class Storage;
3939

4040
private:
41-
struct PlatformInfo;
41+
class Info;
4242

4343
/// A non-null pointer to uniqued storage for this availability context.
44-
const Storage *Info;
44+
const Storage *storage;
4545

46-
AvailabilityContext(const Storage *info) : Info(info) { assert(info); };
46+
AvailabilityContext(const Storage *storage) : storage(storage) {
47+
assert(storage);
48+
};
4749

4850
/// Retrieves an `AvailabilityContext` with the given platform availability
4951
/// parameters.
@@ -108,12 +110,12 @@ class AvailabilityContext {
108110

109111
friend bool operator==(const AvailabilityContext &lhs,
110112
const AvailabilityContext &rhs) {
111-
return lhs.Info == rhs.Info;
113+
return lhs.storage == rhs.storage;
112114
}
113115

114116
friend bool operator!=(const AvailabilityContext &lhs,
115117
const AvailabilityContext &rhs) {
116-
return lhs.Info != rhs.Info;
118+
return lhs.storage != rhs.storage;
117119
}
118120

119121
void print(llvm::raw_ostream &os) const;

include/swift/AST/AvailabilityContextStorage.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323

2424
namespace swift {
2525

26-
/// Summarizes platform specific availability constraints.
27-
struct AvailabilityContext::PlatformInfo {
26+
/// Summarizes availability the constraints contained by an AvailabilityContext.
27+
class AvailabilityContext::Info {
28+
public:
2829
/// The introduction version.
2930
AvailabilityRange Range;
3031

@@ -46,7 +47,7 @@ struct AvailabilityContext::PlatformInfo {
4647
/// Sets each field to the value of the corresponding field in `other` if the
4748
/// other is more restrictive. Returns true if any field changed as a result
4849
/// of adding this constraint.
49-
bool constrainWith(const PlatformInfo &other);
50+
bool constrainWith(const Info &other);
5051

5152
/// Updates each field to reflect the availability of `decl`, if that
5253
/// availability is more restrictive. Returns true if any field was updated.
@@ -55,7 +56,7 @@ struct AvailabilityContext::PlatformInfo {
5556
bool constrainUnavailability(std::optional<PlatformKind> unavailablePlatform);
5657

5758
/// Returns true if `other` is as available or is more available.
58-
bool isContainedIn(const PlatformInfo &other) const;
59+
bool isContainedIn(const Info &other) const;
5960

6061
void Profile(llvm::FoldingSetNodeID &ID) const {
6162
Range.getRawVersionRange().Profile(ID);
@@ -69,12 +70,12 @@ struct AvailabilityContext::PlatformInfo {
6970
/// As an implementation detail, the values that make up an `Availability`
7071
/// context are uniqued and stored as folding set nodes.
7172
class AvailabilityContext::Storage final : public llvm::FoldingSetNode {
72-
Storage(const PlatformInfo &platformInfo) : Platform(platformInfo){};
73+
Storage(const Info &info) : info(info){};
7374

7475
public:
75-
PlatformInfo Platform;
76+
Info info;
7677

77-
static const Storage *get(const PlatformInfo &platformInfo, ASTContext &ctx);
78+
static const Storage *get(const Info &info, ASTContext &ctx);
7879

7980
void Profile(llvm::FoldingSetNodeID &ID) const;
8081
};

lib/AST/ASTContext.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5651,10 +5651,9 @@ SubstitutionMap::Storage *SubstitutionMap::Storage::get(
56515651
}
56525652

56535653
const AvailabilityContext::Storage *
5654-
AvailabilityContext::Storage::get(const PlatformInfo &platformInfo,
5655-
ASTContext &ctx) {
5654+
AvailabilityContext::Storage::get(const Info &info, ASTContext &ctx) {
56565655
llvm::FoldingSetNodeID id;
5657-
platformInfo.Profile(id);
5656+
info.Profile(id);
56585657

56595658
auto &foldingSet =
56605659
ctx.getImpl().getArena(AllocationArena::Permanent).AvailabilityContexts;
@@ -5665,7 +5664,7 @@ AvailabilityContext::Storage::get(const PlatformInfo &platformInfo,
56655664

56665665
void *mem = ctx.Allocate(sizeof(AvailabilityContext::Storage),
56675666
alignof(AvailabilityContext::Storage));
5668-
auto *newNode = ::new (mem) AvailabilityContext::Storage(platformInfo);
5667+
auto *newNode = ::new (mem) AvailabilityContext::Storage(info);
56695668
foldingSet.InsertNode(newNode, insertPos);
56705669

56715670
return newNode;

lib/AST/AvailabilityContext.cpp

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ static bool constrainRange(AvailabilityRange &existing,
3737
return true;
3838
}
3939

40-
bool AvailabilityContext::PlatformInfo::constrainWith(
41-
const PlatformInfo &other) {
40+
bool AvailabilityContext::Info::constrainWith(const Info &other) {
4241
bool isConstrained = false;
4342
isConstrained |= constrainRange(Range, other.Range);
4443
if (other.IsUnavailable) {
@@ -51,7 +50,7 @@ bool AvailabilityContext::PlatformInfo::constrainWith(
5150
return isConstrained;
5251
}
5352

54-
bool AvailabilityContext::PlatformInfo::constrainWith(const Decl *decl) {
53+
bool AvailabilityContext::Info::constrainWith(const Decl *decl) {
5554
bool isConstrained = false;
5655

5756
if (auto range = AvailabilityInference::annotatedAvailableRange(decl))
@@ -68,7 +67,7 @@ bool AvailabilityContext::PlatformInfo::constrainWith(const Decl *decl) {
6867
return isConstrained;
6968
}
7069

71-
bool AvailabilityContext::PlatformInfo::constrainUnavailability(
70+
bool AvailabilityContext::Info::constrainUnavailability(
7271
std::optional<PlatformKind> unavailablePlatform) {
7372
if (!unavailablePlatform)
7473
return false;
@@ -94,8 +93,7 @@ bool AvailabilityContext::PlatformInfo::constrainUnavailability(
9493
return true;
9594
}
9695

97-
bool AvailabilityContext::PlatformInfo::isContainedIn(
98-
const PlatformInfo &other) const {
96+
bool AvailabilityContext::Info::isContainedIn(const Info &other) const {
9997
if (!Range.isContainedIn(other.Range))
10098
return false;
10199

@@ -120,17 +118,17 @@ bool AvailabilityContext::PlatformInfo::isContainedIn(
120118
}
121119

122120
void AvailabilityContext::Storage::Profile(llvm::FoldingSetNodeID &id) const {
123-
Platform.Profile(id);
121+
info.Profile(id);
124122
}
125123

126124
AvailabilityContext
127125
AvailabilityContext::forPlatformRange(const AvailabilityRange &range,
128126
ASTContext &ctx) {
129-
PlatformInfo platformInfo{range, PlatformKind::none,
130-
/*IsUnavailable*/ false,
131-
/*IsUnavailableInEmbedded*/ false,
132-
/*IsDeprecated*/ false};
133-
return AvailabilityContext(Storage::get(platformInfo, ctx));
127+
Info info{range, PlatformKind::none,
128+
/*IsUnavailable*/ false,
129+
/*IsUnavailableInEmbedded*/ false,
130+
/*IsDeprecated*/ false};
131+
return AvailabilityContext(Storage::get(info, ctx));
134132
}
135133

136134
AvailabilityContext AvailabilityContext::forInliningTarget(ASTContext &ctx) {
@@ -147,40 +145,44 @@ AvailabilityContext
147145
AvailabilityContext::get(const AvailabilityRange &platformAvailability,
148146
std::optional<PlatformKind> unavailablePlatform,
149147
bool deprecated, ASTContext &ctx) {
150-
PlatformInfo platformInfo{platformAvailability,
151-
unavailablePlatform.has_value()
152-
? *unavailablePlatform
153-
: PlatformKind::none,
154-
unavailablePlatform.has_value(),
155-
/*IsUnavailableInEmbedded*/ false, deprecated};
156-
return AvailabilityContext(Storage::get(platformInfo, ctx));
148+
Info info{platformAvailability,
149+
unavailablePlatform.has_value() ? *unavailablePlatform
150+
: PlatformKind::none,
151+
unavailablePlatform.has_value(),
152+
/*IsUnavailableInEmbedded*/ false, deprecated};
153+
return AvailabilityContext(Storage::get(info, ctx));
157154
}
158155

159156
AvailabilityRange AvailabilityContext::getPlatformRange() const {
160-
return Info->Platform.Range;
157+
return storage->info.Range;
161158
}
162159

163160
std::optional<PlatformKind>
164161
AvailabilityContext::getUnavailablePlatformKind() const {
165-
if (Info->Platform.IsUnavailable)
166-
return Info->Platform.UnavailablePlatform;
162+
if (storage->info.IsUnavailable)
163+
return storage->info.UnavailablePlatform;
167164
return std::nullopt;
168165
}
169166

170167
bool AvailabilityContext::isUnavailableInEmbedded() const {
171-
return Info->Platform.IsUnavailableInEmbedded;
168+
return storage->info.IsUnavailableInEmbedded;
172169
}
173170

174171
bool AvailabilityContext::isDeprecated() const {
175-
return Info->Platform.IsDeprecated;
172+
return storage->info.IsDeprecated;
176173
}
177174

178175
void AvailabilityContext::constrainWithContext(const AvailabilityContext &other,
179176
ASTContext &ctx) {
180-
PlatformInfo platformAvailability{Info->Platform};
181-
if (platformAvailability.constrainWith(other.Info->Platform)) {
182-
Info = Storage::get(platformAvailability, ctx);
183-
}
177+
bool isConstrained = false;
178+
179+
Info info{storage->info};
180+
isConstrained |= info.constrainWith(other.storage->info);
181+
182+
if (!isConstrained)
183+
return;
184+
185+
storage = Storage::get(info, ctx);
184186
}
185187

186188
void AvailabilityContext::constrainWithDecl(const Decl *decl) {
@@ -189,28 +191,30 @@ void AvailabilityContext::constrainWithDecl(const Decl *decl) {
189191

190192
void AvailabilityContext::constrainWithPlatformRange(
191193
const AvailabilityRange &platformRange, ASTContext &ctx) {
192-
PlatformInfo platformAvailability{Info->Platform};
193-
if (!constrainRange(platformAvailability.Range, platformRange))
194+
195+
Info info{storage->info};
196+
if (!constrainRange(info.Range, platformRange))
194197
return;
195198

196-
Info = Storage::get(platformAvailability, ctx);
199+
storage = Storage::get(info, ctx);
197200
}
198201

199202
void AvailabilityContext::constrainWithDeclAndPlatformRange(
200203
const Decl *decl, const AvailabilityRange &platformRange) {
201-
PlatformInfo platformAvailability{Info->Platform};
202204
bool isConstrained = false;
203-
isConstrained |= platformAvailability.constrainWith(decl);
204-
isConstrained |= constrainRange(platformAvailability.Range, platformRange);
205+
206+
Info info{storage->info};
207+
isConstrained |= info.constrainWith(decl);
208+
isConstrained |= constrainRange(info.Range, platformRange);
205209

206210
if (!isConstrained)
207211
return;
208212

209-
Info = Storage::get(platformAvailability, decl->getASTContext());
213+
storage = Storage::get(info, decl->getASTContext());
210214
}
211215

212216
bool AvailabilityContext::isContainedIn(const AvailabilityContext other) const {
213-
if (!Info->Platform.isContainedIn(other.Info->Platform))
217+
if (!storage->info.isContainedIn(other.storage->info))
214218
return false;
215219

216220
return true;

0 commit comments

Comments
 (0)