Skip to content

Commit 2bd9eb3

Browse files
committed
Add accessor with potentially unavailable attrs
DeclAttributes::getUnavailable() only cares about attributes which make a declaration definitely unavailable, but you sometimes need a version which will also return a potentially unavailable (i.e. “introduced:”) attribute. This adds that.
1 parent d2d1211 commit 2bd9eb3

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

include/swift/AST/Attr.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,6 +1446,11 @@ class DeclAttributes {
14461446
bool
14471447
isUnavailableInSwiftVersion(const version::Version &effectiveVersion) const;
14481448

1449+
/// Returns the first @available attribute that indicates
1450+
/// a declaration is unavailable, or the first one that indicates it's
1451+
/// potentially unavailable, or null otherwise.
1452+
const AvailableAttr *getPotentiallyUnavailable(const ASTContext &ctx) const;
1453+
14491454
/// Returns the first @available attribute that indicates
14501455
/// a declaration is unavailable, or null otherwise.
14511456
const AvailableAttr *getUnavailable(const ASTContext &ctx) const;

lib/AST/Attr.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,47 @@ DeclAttributes::isUnavailableInSwiftVersion(
131131
return false;
132132
}
133133

134+
const AvailableAttr *DeclAttributes::getPotentiallyUnavailable(
135+
const ASTContext &ctx) const {
136+
const AvailableAttr *potential = nullptr;
137+
const AvailableAttr *conditional = nullptr;
138+
139+
for (auto Attr : *this)
140+
if (auto AvAttr = dyn_cast<AvailableAttr>(Attr)) {
141+
if (AvAttr->isInvalid())
142+
continue;
143+
144+
if (!AvAttr->isActivePlatform(ctx) &&
145+
!AvAttr->isLanguageVersionSpecific() &&
146+
!AvAttr->isPackageDescriptionVersionSpecific())
147+
continue;
148+
149+
// Definitely not available.
150+
if (AvAttr->isUnconditionallyDeprecated())
151+
return AvAttr;
152+
153+
switch (AvAttr->getVersionAvailability(ctx)) {
154+
case AvailableVersionComparison::Available:
155+
// Doesn't limit the introduced version.
156+
break;
157+
158+
case AvailableVersionComparison::PotentiallyUnavailable:
159+
// We'll return this if we don't see something that proves it's
160+
// not available in this version.
161+
potential = AvAttr;
162+
break;
163+
164+
case AvailableVersionComparison::Unavailable:
165+
case AvailableVersionComparison::Obsoleted:
166+
conditional = AvAttr;
167+
}
168+
}
169+
170+
if (conditional)
171+
return conditional;
172+
return potential;
173+
}
174+
134175
const AvailableAttr *DeclAttributes::getUnavailable(
135176
const ASTContext &ctx) const {
136177
const AvailableAttr *conditional = nullptr;

0 commit comments

Comments
 (0)