Skip to content

Commit 28ef3ed

Browse files
authored
Merge pull request swiftlang#78454 from tshortli/adopt-semantic-available-attr-part-2
Further adoption of `SemanticAvailableAttr`
2 parents 6c4d5de + 77141c5 commit 28ef3ed

21 files changed

+445
-462
lines changed

include/swift/AST/Attr.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "swift/AST/AttrKind.h"
2222
#include "swift/AST/AutoDiff.h"
2323
#include "swift/AST/AvailabilityDomain.h"
24+
#include "swift/AST/AvailabilityRange.h"
2425
#include "swift/AST/ConcreteDeclRef.h"
2526
#include "swift/AST/DeclNameLoc.h"
2627
#include "swift/AST/Identifier.h"
@@ -3220,7 +3221,7 @@ class ParsedDeclAttributes {
32203221
/// informaton, like its corresponding `AvailabilityDomain`.
32213222
class SemanticAvailableAttr final {
32223223
const AvailableAttr *attr;
3223-
const AvailabilityDomain domain;
3224+
AvailabilityDomain domain;
32243225

32253226
public:
32263227
SemanticAvailableAttr(const AvailableAttr *attr, AvailabilityDomain domain)
@@ -3231,14 +3232,21 @@ class SemanticAvailableAttr final {
32313232
const AvailableAttr *getParsedAttr() const { return attr; }
32323233
const AvailabilityDomain getDomain() const { return domain; }
32333234

3235+
/// The version tuple written in source for the `introduced:` component.
32343236
std::optional<llvm::VersionTuple> getIntroduced() const {
32353237
return attr->Introduced;
32363238
}
32373239

3240+
/// Returns the effective range in which the declaration with this attribute
3241+
/// was introduced.
3242+
AvailabilityRange getIntroducedRange(ASTContext &Ctx) const;
3243+
3244+
/// The version tuple written in source for the `deprecated:` component.
32383245
std::optional<llvm::VersionTuple> getDeprecated() const {
32393246
return attr->Deprecated;
32403247
}
32413248

3249+
/// The version tuple written in source for the `obsoleted:` component.
32423250
std::optional<llvm::VersionTuple> getObsoleted() const {
32433251
return attr->Obsoleted;
32443252
}

include/swift/AST/AvailabilityConstraint.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,32 +60,32 @@ class AvailabilityConstraint {
6060

6161
private:
6262
Kind kind;
63-
const AvailableAttr *attr;
63+
SemanticAvailableAttr attr;
6464

65-
AvailabilityConstraint(Kind kind, const AvailableAttr *attr)
65+
AvailabilityConstraint(Kind kind, SemanticAvailableAttr attr)
6666
: kind(kind), attr(attr) {};
6767

6868
public:
6969
static AvailabilityConstraint
70-
forAlwaysUnavailable(const AvailableAttr *attr) {
70+
forAlwaysUnavailable(SemanticAvailableAttr attr) {
7171
return AvailabilityConstraint(Kind::AlwaysUnavailable, attr);
7272
}
7373

74-
static AvailabilityConstraint forObsoleted(const AvailableAttr *attr) {
74+
static AvailabilityConstraint forObsoleted(SemanticAvailableAttr attr) {
7575
return AvailabilityConstraint(Kind::Obsoleted, attr);
7676
}
7777

78-
static AvailabilityConstraint forRequiresVersion(const AvailableAttr *attr) {
78+
static AvailabilityConstraint forRequiresVersion(SemanticAvailableAttr attr) {
7979
return AvailabilityConstraint(Kind::RequiresVersion, attr);
8080
}
8181

8282
static AvailabilityConstraint
83-
forIntroducedInNewerVersion(const AvailableAttr *attr) {
83+
forIntroducedInNewerVersion(SemanticAvailableAttr attr) {
8484
return AvailabilityConstraint(Kind::IntroducedInNewerVersion, attr);
8585
}
8686

8787
Kind getKind() const { return kind; }
88-
const AvailableAttr *getAttr() const { return attr; }
88+
SemanticAvailableAttr getAttr() const { return attr; }
8989

9090
/// Returns the platform that this constraint applies to, or
9191
/// `PlatformKind::none` if it is not platform specific.

include/swift/AST/AvailabilityInference.h

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,9 @@ class AvailabilityInference {
4646
/// Returns the range of platform versions in which the decl is available.
4747
static AvailabilityRange availableRange(const Decl *D);
4848

49-
/// Returns the range of platform versions in which the decl is available and
50-
/// the attribute which determined this range (which may be `nullptr` if the
51-
/// declaration is always available.
52-
static std::pair<AvailabilityRange, const AvailableAttr *>
53-
availableRangeAndAttr(const Decl *D);
54-
5549
/// Returns true is the declaration is `@_spi_available`.
5650
static bool isAvailableAsSPI(const Decl *D);
5751

58-
/// Returns the range of platform versions in which a declaration with the
59-
/// given `@available` attribute is available.
60-
///
61-
/// NOTE: The attribute must be active on the current platform.
62-
static AvailabilityRange availableRange(const AvailableAttr *attr,
63-
ASTContext &C);
64-
65-
/// Returns the attribute that should be used to determine the availability
66-
/// range of the given declaration, or nullptr if there is none.
67-
static const AvailableAttr *attrForAnnotatedAvailableRange(const Decl *D);
68-
6952
/// Returns the context for which the declaration
7053
/// is annotated as available, or None if the declaration
7154
/// has no availability annotation.

include/swift/AST/Decl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,11 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl>, public Swi
14301430
std::optional<SemanticAvailableAttr> getActiveAvailableAttrForCurrentPlatform(
14311431
bool ignoreAppExtensions = false) const;
14321432

1433+
/// Returns the active platform-specific `@available` attribute that should be
1434+
/// used to determine the platform introduction version of the decl.
1435+
std::optional<SemanticAvailableAttr>
1436+
getAvailableAttrForPlatformIntroduction() const;
1437+
14331438
/// Returns true if the declaration is deprecated at the current deployment
14341439
/// target.
14351440
bool isDeprecated() const { return getDeprecatedAttr().has_value(); }

lib/AST/Attr.cpp

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -419,27 +419,27 @@ void DeclAttributes::dump(const Decl *D) const {
419419
/// introduction version and does not support deprecation, obsoletion, or
420420
/// messages.
421421
LLVM_READONLY
422-
static bool isShortAvailable(const SemanticAvailableAttr &semanticAttr) {
423-
auto *AvailAttr = semanticAttr.getParsedAttr();
424-
if (AvailAttr->isSPI())
422+
static bool isShortAvailable(const SemanticAvailableAttr &attr) {
423+
auto parsedAttr = attr.getParsedAttr();
424+
if (parsedAttr->isSPI())
425425
return false;
426426

427-
if (!AvailAttr->Introduced.has_value())
427+
if (!attr.getIntroduced().has_value())
428428
return false;
429429

430-
if (AvailAttr->Deprecated.has_value())
430+
if (attr.getDeprecated().has_value())
431431
return false;
432432

433-
if (AvailAttr->Obsoleted.has_value())
433+
if (attr.getObsoleted().has_value())
434434
return false;
435435

436-
if (!AvailAttr->Message.empty())
436+
if (!attr.getMessage().empty())
437437
return false;
438438

439-
if (!AvailAttr->Rename.empty())
439+
if (!attr.getRename().empty())
440440
return false;
441441

442-
switch (AvailAttr->getPlatformAgnosticAvailability()) {
442+
switch (parsedAttr->getPlatformAgnosticAvailability()) {
443443
case PlatformAgnosticAvailabilityKind::Deprecated:
444444
case PlatformAgnosticAvailabilityKind::Unavailable:
445445
case PlatformAgnosticAvailabilityKind::UnavailableInSwift:
@@ -939,40 +939,39 @@ SemanticAvailableAttributes::Filter::operator()(
939939
return *semanticAttr;
940940
}
941941

942-
static void printAvailableAttr(const Decl *D,
943-
const SemanticAvailableAttr &SemanticAttr,
942+
static void printAvailableAttr(const Decl *D, const SemanticAvailableAttr &Attr,
944943
ASTPrinter &Printer,
945944
const PrintOptions &Options) {
946-
auto Attr = SemanticAttr.getParsedAttr();
947-
auto Domain = SemanticAttr.getDomain();
945+
auto ParsedAttr = Attr.getParsedAttr();
946+
auto Domain = Attr.getDomain();
948947

949948
// The parser rejects `@available(swift, unavailable)`, so when printing
950949
// attributes that are universally unavailable in Swift, we must print them
951950
// as universally unavailable instead.
952951
// FIXME: Reconsider this, it's a weird special case.
953-
if (Domain.isSwiftLanguage() && Attr->isUnconditionallyUnavailable())
952+
if (Domain.isSwiftLanguage() && Attr.isUnconditionallyUnavailable())
954953
Printer << "*";
955954
else
956955
Printer << Domain.getNameForAttributePrinting();
957956

958-
if (Attr->isUnconditionallyUnavailable())
957+
if (Attr.isUnconditionallyUnavailable())
959958
Printer << ", unavailable";
960-
else if (Attr->isUnconditionallyDeprecated())
959+
else if (Attr.isUnconditionallyDeprecated())
961960
Printer << ", deprecated";
962-
else if (Attr->isNoAsync())
961+
else if (Attr.isNoAsync())
963962
Printer << ", noasync";
964963

965-
if (Attr->Introduced)
966-
Printer << ", introduced: " << Attr->Introduced.value().getAsString();
967-
if (Attr->Deprecated)
968-
Printer << ", deprecated: " << Attr->Deprecated.value().getAsString();
969-
if (Attr->Obsoleted)
970-
Printer << ", obsoleted: " << Attr->Obsoleted.value().getAsString();
964+
if (Attr.getIntroduced())
965+
Printer << ", introduced: " << Attr.getIntroduced().value().getAsString();
966+
if (Attr.getDeprecated())
967+
Printer << ", deprecated: " << Attr.getDeprecated().value().getAsString();
968+
if (Attr.getObsoleted())
969+
Printer << ", obsoleted: " << Attr.getObsoleted().value().getAsString();
971970

972-
if (!Attr->Rename.empty()) {
973-
Printer << ", renamed: \"" << Attr->Rename << "\"";
971+
if (!Attr.getRename().empty()) {
972+
Printer << ", renamed: \"" << Attr.getRename() << "\"";
974973
} else if (auto *VD = dyn_cast<ValueDecl>(D)) {
975-
if (auto *renamedDecl = VD->getRenamedDecl(Attr)) {
974+
if (auto *renamedDecl = VD->getRenamedDecl(ParsedAttr)) {
976975
Printer << ", renamed: \"";
977976
if (auto *Accessor = dyn_cast<AccessorDecl>(renamedDecl)) {
978977
SmallString<32> Name;
@@ -989,10 +988,10 @@ static void printAvailableAttr(const Decl *D,
989988
// If there's no message, but this is specifically an imported
990989
// "unavailable in Swift" attribute, synthesize a message to look good in
991990
// the generated interface.
992-
if (!Attr->Message.empty()) {
991+
if (!Attr.getMessage().empty()) {
993992
Printer << ", message: ";
994-
Printer.printEscapedStringLiteral(Attr->Message);
995-
} else if (Domain.isSwiftLanguage() && Attr->isUnconditionallyUnavailable())
993+
Printer.printEscapedStringLiteral(Attr.getMessage());
994+
} else if (Domain.isSwiftLanguage() && Attr.isUnconditionallyUnavailable())
996995
Printer << ", message: \"Not available in Swift\"";
997996
}
998997

0 commit comments

Comments
 (0)