-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathAvailabilityContext.cpp
402 lines (336 loc) · 13.5 KB
/
AvailabilityContext.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
//===--- AvailabilityContext.cpp - Swift Availability Structures ----------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/AST/AvailabilityContext.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/AvailabilityConstraint.h"
#include "swift/AST/AvailabilityContextStorage.h"
#include "swift/AST/AvailabilityInference.h"
#include "swift/AST/Decl.h"
#include "swift/Basic/Assertions.h"
using namespace swift;
struct AvailabilityDomainInfoComparator {
bool operator()(const AvailabilityContext::DomainInfo &lhs,
const AvailabilityContext::DomainInfo &rhs) const {
StableAvailabilityDomainComparator domainComparator;
return domainComparator(lhs.getDomain(), rhs.getDomain());
}
};
static bool constrainBool(bool &existing, bool other) {
if (existing || !other)
return false;
existing = other;
return true;
}
static bool constrainRange(AvailabilityRange &existing,
const AvailabilityRange &other) {
if (!other.isContainedIn(existing))
return false;
existing = other;
return true;
}
/// Returns true if `domainInfos` will be constrained by merging the domain
/// availability represented by `otherDomainInfo`. Additionally, this function
/// has a couple of side-effects:
///
/// - If any existing domain availability ought to be constrained by
/// `otherDomainInfo` then that value will be updated in place.
/// - If any existing value in `domainInfos` should be replaced when
/// `otherDomainInfo` is added, then that existing value is removed
/// and `otherDomainInfo` is appended to `domainInfosToAdd`.
///
static bool constrainDomainInfos(
AvailabilityContext::DomainInfo otherDomainInfo,
llvm::SmallVectorImpl<AvailabilityContext::DomainInfo> &domainInfos,
llvm::SmallVectorImpl<AvailabilityContext::DomainInfo> &domainInfosToAdd) {
bool isConstrained = false;
bool shouldAdd = true;
auto otherDomain = otherDomainInfo.getDomain();
auto end = domainInfos.rend();
// Iterate over domainInfos in reverse order to allow items to be removed
// during iteration.
for (auto iter = domainInfos.rbegin(); iter != end; ++iter) {
auto &domainInfo = *iter;
auto domain = domainInfo.getDomain();
// We found an existing available range for the domain. Constrain it if
// necessary.
if (domain == otherDomain) {
shouldAdd = false;
isConstrained |= domainInfo.constrainRange(otherDomainInfo.getRange());
continue;
}
// Check whether an existing unavailable domain contains the domain that
// would be added. If so, there's nothing to do because the availability of
// the domain is already as constrained as it can be.
if (domainInfo.isUnavailable() && domain.contains(otherDomain)) {
DEBUG_ASSERT(!isConstrained);
return false;
}
// If the domain that will be added is unavailable, check whether the
// existing domain is contained within it. If it is, availability for the
// existing domain should be removed because it has been superseded.
if (otherDomainInfo.isUnavailable() && otherDomain.contains(domain)) {
domainInfos.erase((iter + 1).base());
isConstrained = true;
}
}
// If the new domain availability isn't already covered by an item in
// `domainInfos`, then it needs to be added. Defer adding the new domain
// availability until later when the entire set of domain infos can be
// re-sorted once.
if (shouldAdd) {
domainInfosToAdd.push_back(otherDomainInfo);
return true;
}
return isConstrained;
}
/// Constrains `domainInfos` by merging them with `otherDomainInfos`. Returns
/// true if any changes were made to `domainInfos`.
static bool constrainDomainInfos(
llvm::SmallVectorImpl<AvailabilityContext::DomainInfo> &domainInfos,
llvm::ArrayRef<AvailabilityContext::DomainInfo> otherDomainInfos) {
bool isConstrained = false;
llvm::SmallVector<AvailabilityContext::DomainInfo, 4> domainInfosToAdd;
for (auto otherDomainInfo : otherDomainInfos) {
isConstrained |=
constrainDomainInfos(otherDomainInfo, domainInfos, domainInfosToAdd);
}
if (!isConstrained)
return false;
// Add the new domains and then re-sort.
for (auto domainInfo : domainInfosToAdd)
domainInfos.push_back(domainInfo);
llvm::sort(domainInfos, AvailabilityDomainInfoComparator());
return true;
}
bool AvailabilityContext::DomainInfo::constrainRange(
const AvailabilityRange &otherRange) {
return ::constrainRange(range, otherRange);
}
AvailabilityContext
AvailabilityContext::forPlatformRange(const AvailabilityRange &range,
const ASTContext &ctx) {
return AvailabilityContext(
Storage::get(range, /*isDeprecated=*/false, /*domainInfos=*/{}, ctx));
}
AvailabilityContext
AvailabilityContext::forInliningTarget(const ASTContext &ctx) {
return AvailabilityContext::forPlatformRange(
AvailabilityRange::forInliningTarget(ctx), ctx);
}
AvailabilityContext
AvailabilityContext::forDeploymentTarget(const ASTContext &ctx) {
return AvailabilityContext::forPlatformRange(
AvailabilityRange::forDeploymentTarget(ctx), ctx);
}
AvailabilityRange AvailabilityContext::getPlatformRange() const {
return storage->platformRange;
}
std::optional<AvailabilityRange>
AvailabilityContext::getAvailabilityRange(AvailabilityDomain domain,
const ASTContext &ctx) const {
DEBUG_ASSERT(domain.supportsContextRefinement());
if (domain.isActiveForTargetPlatform(ctx)) {
return storage->platformRange;
}
for (auto domainInfo : storage->getDomainInfos()) {
if (domain == domainInfo.getDomain() && !domainInfo.isUnavailable())
return domainInfo.getRange();
}
return std::nullopt;
}
bool AvailabilityContext::isUnavailable() const {
for (auto domainInfo : storage->getDomainInfos()) {
if (domainInfo.isUnavailable())
return true;
}
return false;
}
bool AvailabilityContext::containsUnavailableDomain(
AvailabilityDomain domain) const {
for (auto domainInfo : storage->getDomainInfos()) {
if (domainInfo.isUnavailable()) {
if (domainInfo.getDomain().contains(domain))
return true;
}
}
return false;
}
bool AvailabilityContext::isDeprecated() const { return storage->isDeprecated; }
void AvailabilityContext::constrainWithContext(const AvailabilityContext &other,
const ASTContext &ctx) {
bool isConstrained = false;
auto platformRange = storage->platformRange;
isConstrained |= constrainRange(platformRange, other.storage->platformRange);
bool isDeprecated = storage->isDeprecated;
isConstrained |= constrainBool(isDeprecated, other.storage->isDeprecated);
auto domainInfos = storage->copyDomainInfos();
isConstrained |=
constrainDomainInfos(domainInfos, other.storage->getDomainInfos());
if (!isConstrained)
return;
storage = Storage::get(platformRange, isDeprecated, domainInfos, ctx);
}
void AvailabilityContext::constrainWithPlatformRange(
const AvailabilityRange &range, const ASTContext &ctx) {
auto platformRange = storage->platformRange;
if (!constrainRange(platformRange, range))
return;
storage = Storage::get(platformRange, storage->isDeprecated,
storage->getDomainInfos(), ctx);
}
void AvailabilityContext::constrainWithAvailabilityRange(
const AvailabilityRange &range, AvailabilityDomain domain,
const ASTContext &ctx) {
if (domain.isActiveForTargetPlatform(ctx)) {
constrainWithPlatformRange(range, ctx);
return;
}
auto domainInfos = storage->copyDomainInfos();
if (!constrainDomainInfos(domainInfos, {DomainInfo(domain, range)}))
return;
storage = Storage::get(storage->platformRange, storage->isDeprecated,
domainInfos, ctx);
}
void AvailabilityContext::constrainWithUnavailableDomain(
AvailabilityDomain domain, const ASTContext &ctx) {
auto domainInfos = storage->copyDomainInfos();
if (!constrainDomainInfos(domainInfos, {DomainInfo::unavailable(domain)}))
return;
storage = Storage::get(storage->platformRange, storage->isDeprecated,
domainInfos, ctx);
}
void AvailabilityContext::constrainWithDecl(const Decl *decl) {
constrainWithDeclAndPlatformRange(decl, AvailabilityRange::alwaysAvailable());
}
void AvailabilityContext::constrainWithDeclAndPlatformRange(
const Decl *decl, const AvailabilityRange &otherPlatformRange) {
auto &ctx = decl->getASTContext();
bool isConstrained = false;
auto platformRange = storage->platformRange;
isConstrained |= constrainRange(platformRange, otherPlatformRange);
bool isDeprecated = storage->isDeprecated;
isConstrained |= constrainBool(isDeprecated, decl->isDeprecated());
// Compute the availability constraints for the decl when used in this context
// and then map those constraints to domain infos. The result will be merged
// into the existing domain infos for this context.
llvm::SmallVector<DomainInfo, 4> declDomainInfos;
AvailabilityConstraintFlags flags =
AvailabilityConstraintFlag::SkipEnclosingExtension;
auto constraints =
swift::getAvailabilityConstraintsForDecl(decl, *this, flags);
for (auto constraint : constraints) {
auto attr = constraint.getAttr();
auto domain = attr.getDomain();
switch (constraint.getReason()) {
case AvailabilityConstraint::Reason::UnconditionallyUnavailable:
case AvailabilityConstraint::Reason::Obsoleted:
case AvailabilityConstraint::Reason::UnavailableForDeployment:
declDomainInfos.push_back(DomainInfo::unavailable(domain));
break;
case AvailabilityConstraint::Reason::PotentiallyUnavailable:
if (auto introducedRange = attr.getIntroducedRange(ctx)) {
if (domain.isActiveForTargetPlatform(ctx)) {
isConstrained |= constrainRange(platformRange, *introducedRange);
} else {
declDomainInfos.push_back({domain, *introducedRange});
}
}
break;
}
}
auto domainInfos = storage->copyDomainInfos();
isConstrained |= constrainDomainInfos(domainInfos, declDomainInfos);
if (!isConstrained)
return;
storage = Storage::get(platformRange, isDeprecated, domainInfos,
decl->getASTContext());
}
bool AvailabilityContext::isContainedIn(const AvailabilityContext other) const {
// The available versions range be the same or smaller.
if (!storage->platformRange.isContainedIn(other.storage->platformRange))
return false;
// The set of deprecated domains should be the same or larger.
if (!storage->isDeprecated && other.storage->isDeprecated)
return false;
// Every unavailable domain in the other context should be contained in some
// unavailable domain in this context.
bool disjointUnavailability = llvm::any_of(
other.storage->getDomainInfos(), [&](const DomainInfo &otherDomainInfo) {
return llvm::none_of(storage->getDomainInfos(),
[&otherDomainInfo](const DomainInfo &domainInfo) {
return domainInfo.getDomain().contains(
otherDomainInfo.getDomain());
});
});
if (disjointUnavailability)
return false;
return true;
}
static std::string
stringForAvailability(const AvailabilityRange &availability) {
if (availability.isAlwaysAvailable())
return "all";
if (availability.isKnownUnreachable())
return "none";
return availability.getVersionString();
}
void AvailabilityContext::print(llvm::raw_ostream &os) const {
os << "version=" << stringForAvailability(getPlatformRange());
auto domainInfos = storage->getDomainInfos();
if (domainInfos.size() > 0) {
os << " unavailable=";
llvm::interleave(
domainInfos, os,
[&](const DomainInfo &domainInfo) { domainInfo.getDomain().print(os); },
",");
}
if (isDeprecated())
os << " deprecated";
}
void AvailabilityContext::dump() const { print(llvm::errs()); }
bool verifyDomainInfos(
llvm::ArrayRef<AvailabilityContext::DomainInfo> domainInfos) {
// Checks that the following invariants hold:
// - The domain infos are sorted using AvailabilityDomainInfoComparator.
// - There is not more than one info per-domain.
if (domainInfos.empty())
return true;
AvailabilityDomainInfoComparator compare;
auto prev = domainInfos.begin();
auto next = prev;
auto end = domainInfos.end();
for (++next; next != end; prev = next, ++next) {
const auto &prevInfo = *prev;
const auto &nextInfo = *next;
if (compare(nextInfo, prevInfo))
return false;
// Since the infos are sorted by domain, infos with the same domain should
// be adjacent.
if (prevInfo.getDomain() == nextInfo.getDomain())
return false;
}
return true;
}
bool AvailabilityContext::verify(const ASTContext &ctx) const {
return verifyDomainInfos(storage->getDomainInfos());
}
void AvailabilityContext::Storage::Profile(
llvm::FoldingSetNodeID &ID, const AvailabilityRange &platformRange,
bool isDeprecated,
llvm::ArrayRef<AvailabilityContext::DomainInfo> domainInfos) {
platformRange.getRawVersionRange().Profile(ID);
ID.AddBoolean(isDeprecated);
ID.AddInteger(domainInfos.size());
for (auto domainInfo : domainInfos) {
domainInfo.Profile(ID);
}
}