- Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathAvailabilityContext.cpp
493 lines (413 loc) · 16.8 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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//===--- 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/AvailabilityScope.h"
#include"swift/AST/Decl.h"
#include"swift/AST/Module.h"
#include"swift/Basic/Assertions.h"
usingnamespaceswift;
structAvailabilityDomainInfoComparator {
booloperator()(const AvailabilityContext::DomainInfo &lhs,
const AvailabilityContext::DomainInfo &rhs) const {
StableAvailabilityDomainComparator domainComparator;
returndomainComparator(lhs.getDomain(), rhs.getDomain());
}
};
staticboolconstrainBool(bool &existing, bool other) {
if (existing || !other)
returnfalse;
existing = other;
returntrue;
}
staticboolconstrainRange(AvailabilityRange &existing,
const AvailabilityRange &other) {
if (!other.isContainedIn(existing))
returnfalse;
existing = other;
returntrue;
}
/// 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`.
///
staticboolconstrainDomainInfos(
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);
returnfalse;
}
// 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);
returntrue;
}
return isConstrained;
}
/// Constrains `domainInfos` by merging them with `otherDomainInfos`. Returns
/// true if any changes were made to `domainInfos`.
staticboolconstrainDomainInfos(
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)
returnfalse;
// Add the new domains and then re-sort.
for (auto domainInfo : domainInfosToAdd)
domainInfos.push_back(domainInfo);
llvm::sort(domainInfos, AvailabilityDomainInfoComparator());
returntrue;
}
boolAvailabilityContext::DomainInfo::constrainRange(
const AvailabilityRange &otherRange) {
return ::constrainRange(range, otherRange);
}
AvailabilityContext
AvailabilityContext::forPlatformRange(const AvailabilityRange &range,
const ASTContext &ctx) {
returnAvailabilityContext(
Storage::get(range, /*isDeprecated=*/false, /*domainInfos=*/{}, ctx));
}
AvailabilityContext
AvailabilityContext::forInliningTarget(const ASTContext &ctx) {
returnAvailabilityContext::forPlatformRange(
AvailabilityRange::forInliningTarget(ctx), ctx);
}
AvailabilityContext
AvailabilityContext::forDeploymentTarget(const ASTContext &ctx) {
returnAvailabilityContext::forPlatformRange(
AvailabilityRange::forDeploymentTarget(ctx), ctx);
}
AvailabilityContext
AvailabilityContext::forLocation(SourceLoc loc, const DeclContext *declContext,
const AvailabilityScope **refinedScope) {
auto &ctx = declContext->getASTContext();
SourceFile *sf =
loc.isValid()
? declContext->getParentModule()->getSourceFileContainingLocation(loc)
: declContext->getParentSourceFile();
// If our source location is invalid (this may be synthesized code), climb the
// decl context hierarchy until we find a location that is valid, merging
// availability contexts on the way up.
//
// Because we are traversing decl contexts, we will miss availability scopes
// in synthesized code that are introduced by AST elements that are themselves
// not decl contexts, such as `#available(..)` and property declarations.
// Therefore a reference with an invalid source location that is contained
// inside an `#available()` and with no intermediate decl context will not be
// refined. For now, this is fine, but if we ever synthesize #available(),
// this could become a problem..
// We can assume we are running on at least the minimum inlining target.
auto baseAvailability = AvailabilityContext::forInliningTarget(ctx);
auto isInvalidLoc = [sf](SourceLoc loc) {
return sf ? loc.isInvalid() : true;
};
while (declContext && isInvalidLoc(loc)) {
const Decl *decl = declContext->getInnermostDeclarationDeclContext();
if (!decl)
break;
baseAvailability.constrainWithDecl(decl);
loc = decl->getLoc();
declContext = decl->getDeclContext();
}
if (!sf || loc.isInvalid())
return baseAvailability;
auto *rootScope = AvailabilityScope::getOrBuildForSourceFile(*sf);
if (!rootScope)
return baseAvailability;
auto *scope = rootScope->findMostRefinedSubContext(loc, ctx);
if (!scope)
return baseAvailability;
if (refinedScope)
*refinedScope = scope;
auto availability = scope->getAvailabilityContext();
availability.constrainWithContext(baseAvailability, ctx);
return availability;
}
AvailabilityContext AvailabilityContext::forDeclSignature(const Decl *decl) {
returnforLocation(decl->getLoc(), decl->getInnermostDeclContext());
}
AvailabilityContext
AvailabilityContext::forAlwaysAvailable(const ASTContext &ctx) {
returnAvailabilityContext(Storage::get(AvailabilityRange::alwaysAvailable(),
/*isDeprecated=*/false,
/*domainInfos=*/{}, 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.isActivePlatform(ctx))
return storage->platformRange;
for (auto domainInfo : storage->getDomainInfos()) {
if (domain == domainInfo.getDomain() && !domainInfo.isUnavailable())
return domainInfo.getRange();
}
return std::nullopt;
}
boolAvailabilityContext::isUnavailable() const {
for (auto domainInfo : storage->getDomainInfos()) {
if (domainInfo.isUnavailable())
returntrue;
}
returnfalse;
}
boolAvailabilityContext::containsUnavailableDomain(
AvailabilityDomain domain) const {
for (auto domainInfo : storage->getDomainInfos()) {
if (domainInfo.isUnavailable()) {
if (domainInfo.getDomain().contains(domain))
returntrue;
}
}
returnfalse;
}
boolAvailabilityContext::isDeprecated() const { return storage->isDeprecated; }
voidAvailabilityContext::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);
}
voidAvailabilityContext::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);
}
voidAvailabilityContext::constrainWithAvailabilityRange(
const AvailabilityRange &range, AvailabilityDomain domain,
const ASTContext &ctx) {
DEBUG_ASSERT(domain.supportsContextRefinement());
if (domain.isActivePlatform(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);
}
voidAvailabilityContext::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);
}
voidAvailabilityContext::constrainWithDecl(const Decl *decl) {
constrainWithDeclAndPlatformRange(decl, AvailabilityRange::alwaysAvailable());
}
voidAvailabilityContext::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.isActivePlatform(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());
}
boolAvailabilityContext::isContainedIn(const AvailabilityContext other) const {
// The available versions range be the same or smaller.
if (!storage->platformRange.isContainedIn(other.storage->platformRange))
returnfalse;
// The set of deprecated domains should be the same or larger.
if (!storage->isDeprecated && other.storage->isDeprecated)
returnfalse;
// 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) {
returnllvm::none_of(storage->getDomainInfos(),
[&otherDomainInfo](const DomainInfo &domainInfo) {
return domainInfo.getDomain().contains(
otherDomainInfo.getDomain());
});
});
if (disjointUnavailability)
returnfalse;
returntrue;
}
static std::string
stringForAvailability(const AvailabilityRange &availability) {
if (availability.isAlwaysAvailable())
return"all";
if (availability.isKnownUnreachable())
return"none";
return availability.getVersionString();
}
voidAvailabilityContext::print(llvm::raw_ostream &os) const {
os << "version=" << stringForAvailability(getPlatformRange());
auto domainInfos = storage->getDomainInfos();
if (!domainInfos.empty()) {
auto availableInfos = llvm::make_filter_range(
domainInfos, [](auto info) { return !info.isUnavailable(); });
if (!availableInfos.empty()) {
os << " available=";
llvm::interleave(
availableInfos, os,
[&](const DomainInfo &domainInfo) {
domainInfo.getDomain().print(os);
if (domainInfo.getDomain().isVersioned() &&
domainInfo.getRange().hasMinimumVersion())
os << ">=" << domainInfo.getRange().getAsString();
},
",");
}
auto unavailableInfos = llvm::make_filter_range(
domainInfos, [](auto info) { return info.isUnavailable(); });
if (!unavailableInfos.empty()) {
os << " unavailable=";
llvm::interleave(
unavailableInfos, os,
[&](const DomainInfo &domainInfo) {
domainInfo.getDomain().print(os);
},
",");
}
}
if (isDeprecated())
os << " deprecated";
}
voidAvailabilityContext::dump() const { print(llvm::errs()); }
boolverifyDomainInfos(
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())
returntrue;
AvailabilityDomainInfoComparator compare;
auto prev = domainInfos.begin();
auto next = prev;
auto end = domainInfos.end();
for (++next; next != end; prev = next, ++next) {
constauto &prevInfo = *prev;
constauto &nextInfo = *next;
if (compare(nextInfo, prevInfo))
returnfalse;
// Since the infos are sorted by domain, infos with the same domain should
// be adjacent.
if (prevInfo.getDomain() == nextInfo.getDomain())
returnfalse;
}
returntrue;
}
boolAvailabilityContext::verify(const ASTContext &ctx) const {
returnverifyDomainInfos(storage->getDomainInfos());
}
voidAvailabilityContext::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);
}
}