-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathAvailabilityContext.cpp
307 lines (254 loc) · 10.1 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
//===--- 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 `domain` is not already contained in `domainInfos` as an
/// unavailable domain. Also, removes domains from `unavailableDomains` that are
/// contained in `domain`.
static bool shouldConstrainUnavailableDomains(
AvailabilityDomain domain,
llvm::SmallVectorImpl<AvailabilityContext::DomainInfo> &domainInfos) {
bool didRemove = false;
for (auto iter = domainInfos.rbegin(), end = domainInfos.rend(); iter != end;
++iter) {
auto const &domainInfo = *iter;
auto existingDomain = domainInfo.getDomain();
if (!domainInfo.isUnavailable())
continue;
// Check if the domain is already unavailable.
if (existingDomain.contains(domain)) {
ASSERT(!didRemove); // This would indicate that the context is malformed.
return false;
}
// Check if the existing domain would be absorbed by the new domain.
if (domain.contains(existingDomain)) {
domainInfos.erase((iter + 1).base());
didRemove = true;
}
}
return true;
}
static bool constrainDomainInfos(
llvm::SmallVectorImpl<AvailabilityContext::DomainInfo> &domainInfos,
llvm::ArrayRef<AvailabilityContext::DomainInfo> otherDomainInfos) {
llvm::SmallVector<AvailabilityContext::DomainInfo, 4> domainInfosToAdd;
for (auto otherDomainInfo : otherDomainInfos) {
if (otherDomainInfo.isUnavailable())
if (shouldConstrainUnavailableDomains(otherDomainInfo.getDomain(),
domainInfos))
domainInfosToAdd.push_back(otherDomainInfo);
}
if (domainInfosToAdd.size() < 1)
return false;
// Add the candidate domain and then re-sort.
for (auto domainInfo : domainInfosToAdd)
domainInfos.push_back(domainInfo);
llvm::sort(domainInfos, AvailabilityDomainInfoComparator());
return true;
}
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;
}
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 &otherPlatformRange, const ASTContext &ctx) {
auto platformRange = storage->platformRange;
if (!constrainRange(platformRange, otherPlatformRange))
return;
storage = Storage::get(platformRange, storage->isDeprecated,
storage->getDomainInfos(), 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());
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:
DEBUG_ASSERT(domain.isPlatform());
if (domain.isPlatform())
isConstrained |=
constrainRange(platformRange, attr.getIntroducedRange(ctx));
// FIXME: [availability] Store other potentially unavailable domains in
// domainInfos.
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 AvailabilityContext::verify(const ASTContext &ctx) const {
// Domain infos must be sorted to ensure folding set node lookups yield
// consistent results.
if (!llvm::is_sorted(storage->getDomainInfos(),
AvailabilityDomainInfoComparator()))
return false;
return true;
}
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);
}
}