Skip to content

Commit 73c2c66

Browse files
committed
AST: Adopt new AvailableAttr constructor in attribute inference.
1 parent 1c2ba56 commit 73c2c66

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

include/swift/AST/Attr.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,23 @@ class AvailableAttr : public DeclAttribute {
836836
Bits.AvailableAttr.PlatformAgnostic);
837837
}
838838

839+
/// Returns the kind of availability the attribute specifies.
840+
Kind getKind() const {
841+
switch (getPlatformAgnosticAvailability()) {
842+
case PlatformAgnosticAvailabilityKind::None:
843+
case PlatformAgnosticAvailabilityKind::SwiftVersionSpecific:
844+
case PlatformAgnosticAvailabilityKind::PackageDescriptionVersionSpecific:
845+
return Kind::Default;
846+
case PlatformAgnosticAvailabilityKind::Deprecated:
847+
return Kind::Deprecated;
848+
case PlatformAgnosticAvailabilityKind::UnavailableInSwift:
849+
case PlatformAgnosticAvailabilityKind::Unavailable:
850+
return Kind::Unavailable;
851+
case PlatformAgnosticAvailabilityKind::NoAsync:
852+
return Kind::NoAsync;
853+
}
854+
}
855+
839856
/// Create an `AvailableAttr` that specifies universal unavailability, e.g.
840857
/// `@available(*, unavailable)`.
841858
static AvailableAttr *createUniversallyUnavailable(ASTContext &C,

lib/AST/Availability.cpp

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ namespace {
109109
/// The inferred availability required to access a group of declarations
110110
/// on a single platform.
111111
struct InferredAvailability {
112-
PlatformAgnosticAvailabilityKind PlatformAgnostic
113-
= PlatformAgnosticAvailabilityKind::None;
112+
AvailableAttr::Kind Kind = AvailableAttr::Kind::Default;
114113

115114
std::optional<llvm::VersionTuple> Introduced;
116115
std::optional<llvm::VersionTuple> Deprecated;
@@ -148,10 +147,9 @@ mergeIntoInferredVersion(const std::optional<llvm::VersionTuple> &Version,
148147
static void mergeWithInferredAvailability(SemanticAvailableAttr Attr,
149148
InferredAvailability &Inferred) {
150149
auto *ParsedAttr = Attr.getParsedAttr();
151-
Inferred.PlatformAgnostic = static_cast<PlatformAgnosticAvailabilityKind>(
152-
std::max(static_cast<unsigned>(Inferred.PlatformAgnostic),
153-
static_cast<unsigned>(
154-
ParsedAttr->getPlatformAgnosticAvailability())));
150+
Inferred.Kind = static_cast<AvailableAttr::Kind>(
151+
std::max(static_cast<unsigned>(Inferred.Kind),
152+
static_cast<unsigned>(ParsedAttr->getKind())));
155153

156154
// The merge of two introduction versions is the maximum of the two versions.
157155
if (mergeIntoInferredVersion(Attr.getIntroduced(), Inferred.Introduced,
@@ -164,19 +162,18 @@ static void mergeWithInferredAvailability(SemanticAvailableAttr Attr,
164162
mergeIntoInferredVersion(Attr.getObsoleted(), Inferred.Obsoleted, std::min);
165163
}
166164

167-
/// Create an implicit availability attribute for the given platform
165+
/// Create an implicit availability attribute for the given domain
168166
/// and with the inferred availability.
169-
static AvailableAttr *createAvailableAttr(PlatformKind Platform,
167+
static AvailableAttr *createAvailableAttr(AvailabilityDomain Domain,
170168
const InferredAvailability &Inferred,
171-
StringRef Message,
172-
StringRef Rename,
169+
StringRef Message, StringRef Rename,
173170
ValueDecl *RenameDecl,
174171
ASTContext &Context) {
175172
// If there is no information that would go into the availability attribute,
176173
// don't create one.
177-
if (Inferred.PlatformAgnostic == PlatformAgnosticAvailabilityKind::None &&
178-
!Inferred.Introduced && !Inferred.Deprecated && !Inferred.Obsoleted &&
179-
Message.empty() && Rename.empty() && !RenameDecl)
174+
if (Inferred.Kind == AvailableAttr::Kind::Default && !Inferred.Introduced &&
175+
!Inferred.Deprecated && !Inferred.Obsoleted && Message.empty() &&
176+
Rename.empty() && !RenameDecl)
180177
return nullptr;
181178

182179
llvm::VersionTuple Introduced =
@@ -186,11 +183,10 @@ static AvailableAttr *createAvailableAttr(PlatformKind Platform,
186183
llvm::VersionTuple Obsoleted =
187184
Inferred.Obsoleted.value_or(llvm::VersionTuple());
188185

189-
return new (Context)
190-
AvailableAttr(SourceLoc(), SourceRange(), Platform, Message, Rename,
191-
Introduced, SourceRange(), Deprecated, SourceRange(),
192-
Obsoleted, SourceRange(), Inferred.PlatformAgnostic,
193-
/*Implicit=*/true, Inferred.IsSPI);
186+
return new (Context) AvailableAttr(
187+
SourceLoc(), SourceRange(), Domain, Inferred.Kind, Message, Rename,
188+
Introduced, SourceRange(), Deprecated, SourceRange(), Obsoleted,
189+
SourceRange(), /*Implicit=*/true, Inferred.IsSPI);
194190
}
195191

196192
void AvailabilityInference::applyInferredAvailableAttrs(
@@ -214,8 +210,7 @@ void AvailabilityInference::applyInferredAvailableAttrs(
214210
do {
215211
llvm::SmallVector<SemanticAvailableAttr, 8> PendingAttrs;
216212

217-
for (auto AvAttr :
218-
D->getSemanticAvailableAttrs()) {
213+
for (auto AvAttr : D->getSemanticAvailableAttrs()) {
219214
// Skip an attribute from an outer declaration if it is for a platform
220215
// that was already handled implicitly by an attribute from an inner
221216
// declaration.
@@ -250,8 +245,12 @@ void AvailabilityInference::applyInferredAvailableAttrs(
250245
// Create an availability attribute for each observed platform and add
251246
// to ToDecl.
252247
for (auto &Pair : Inferred) {
253-
auto *Attr = createAvailableAttr(Pair.first, Pair.second, Message,
254-
Rename, RenameDecl, Context);
248+
auto Domain = (Pair.first == PlatformKind::none)
249+
? AvailabilityDomain::forUniversal()
250+
: AvailabilityDomain::forPlatform(Pair.first);
251+
252+
auto *Attr = createAvailableAttr(Domain, Pair.second, Message, Rename,
253+
RenameDecl, Context);
255254

256255
if (Attr) {
257256
if (RenameDecl && ToValueDecl)

0 commit comments

Comments
 (0)