- Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathAvailabilitySpec.cpp
157 lines (134 loc) · 5.68 KB
/
AvailabilitySpec.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
//===--- AvailabilitySpec.cpp - Swift Availability Query ASTs -------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file implements the availability specification AST classes.
//
//===----------------------------------------------------------------------===//
#include"swift/AST/AvailabilitySpec.h"
#include"swift/AST/ASTContext.h"
#include"swift/AST/AvailabilityDomain.h"
#include"swift/AST/DiagnosticsSema.h"
#include"swift/AST/TypeCheckRequests.h"
#include"llvm/Support/raw_ostream.h"
usingnamespaceswift;
AvailabilitySpec *AvailabilitySpec::createWildcard(ASTContext &ctx,
SourceLoc starLoc) {
returnnew (ctx) AvailabilitySpec(AvailabilityDomain::forUniversal(), starLoc,
/*Version=*/{},
/*VersionStartLoc=*/{});
}
static SourceRange getSpecSourceRange(SourceLoc domainLoc,
SourceRange versionRange) {
if (domainLoc.isInvalid())
returnSourceRange();
if (versionRange.isValid())
returnSourceRange(domainLoc, versionRange.End);
returnSourceRange(domainLoc);
}
AvailabilitySpec *AvailabilitySpec::createForDomain(ASTContext &ctx,
AvailabilityDomain domain,
SourceLoc loc,
llvm::VersionTuple version,
SourceRange versionRange) {
returnnew (ctx)
AvailabilitySpec(domain, getSpecSourceRange(loc, versionRange), version,
versionRange.Start);
}
AvailabilitySpec *AvailabilitySpec::createForDomainIdentifier(
ASTContext &ctx, Identifier domainIdentifier, SourceLoc loc,
llvm::VersionTuple version, SourceRange versionRange) {
returnnew (ctx)
AvailabilitySpec(domainIdentifier, getSpecSourceRange(loc, versionRange),
version, versionRange.Start);
}
AvailabilitySpec *AvailabilitySpec::clone(ASTContext &ctx) const {
returnnew (ctx) AvailabilitySpec(getDomainOrIdentifier().copy(ctx), SrcRange,
Version, VersionStartLoc);
}
voidAvailabilitySpec::print(llvm::raw_ostream &os) const {
getDomainOrIdentifier().print(os);
if (!getRawVersion().empty())
os << "" << getRawVersion().getAsString();
}
std::optional<AvailabilityDomain>
AvailabilitySpec::resolveInDeclContext(const DeclContext *declContext) {
auto domainOrIdentifier = getDomainOrIdentifier();
auto result =
domainOrIdentifier.resolveInDeclContext(getStartLoc(), declContext);
DomainOrIdentifier = domainOrIdentifier;
return result;
}
std::optional<SemanticAvailabilitySpec>
AvailabilitySpec::getSemanticAvailabilitySpec(
const DeclContext *declContext) const {
returnevaluateOrDefault(declContext->getASTContext().evaluator,
SemanticAvailabilitySpecRequest{this, declContext},
std::nullopt);
}
llvm::VersionTuple SemanticAvailabilitySpec::getVersion() const {
// For macOS Big Sur, we canonicalize 10.16 to 11.0 for compile-time
// checking since clang canonicalizes availability markup. However, to
// support Beta versions of macOS Big Sur where the OS
// reports 10.16 at run time, we need to compare against 10.16,
//
// This means for:
//
// if #available(macOS 10.16, *) { ... }
//
// we need to store the uncanonicalized version for codegen and canonicalize
// it as necessary for compile-time checks.
returncanonicalizePlatformVersion(getDomain().getPlatformKind(),
spec->getRawVersion());
}
std::optional<SemanticAvailabilitySpec>
SemanticAvailabilitySpecs::Filter::operator()(
const AvailabilitySpec *spec) const {
if (auto semanticSpec = spec->getSemanticAvailabilitySpec(declContext))
return semanticSpec;
return std::nullopt;
}
std::optional<SemanticAvailabilitySpec>
SemanticAvailabilitySpecRequest::evaluate(
Evaluator &evaluator, const AvailabilitySpec *spec,
const DeclContext *declContext) const {
if (spec->isInvalid())
return std::nullopt;
auto &diags = declContext->getASTContext().Diags;
AvailabilitySpec *mutableSpec = const_cast<AvailabilitySpec *>(spec);
if (!mutableSpec->resolveInDeclContext(declContext).has_value())
return std::nullopt;
auto version = spec->getRawVersion();
if (!VersionRange::isValidVersion(version)) {
diags
.diagnose(spec->getStartLoc(),
diag::availability_unsupported_version_number, version)
.highlight(spec->getVersionSrcRange());
return std::nullopt;
}
returnSemanticAvailabilitySpec(spec);
}
std::optional<std::optional<SemanticAvailabilitySpec>>
SemanticAvailabilitySpecRequest::getCachedResult() const {
auto *spec = std::get<0>(getStorage());
if (spec->isInvalid())
return std::optional<SemanticAvailabilitySpec>();
auto domainOrIdentifier = spec->getDomainOrIdentifier();
if (!domainOrIdentifier.isResolved())
return std::nullopt;
returnSemanticAvailabilitySpec(spec);
}
voidSemanticAvailabilitySpecRequest::cacheResult(
std::optional<SemanticAvailabilitySpec> value) const {
auto *mutableSpec = const_cast<AvailabilitySpec *>(std::get<0>(getStorage()));
if (!value)
mutableSpec->setInvalid();
}