Skip to content

Commit 000c9da

Browse files
committed
AST: Return SemanticAvailableAttr from Decl::getActiveAvailableAttrForCurrentPlatform().
1 parent 2ca2237 commit 000c9da

File tree

4 files changed

+24
-19
lines changed

4 files changed

+24
-19
lines changed

include/swift/AST/Attr.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3237,6 +3237,14 @@ class SemanticAvailableAttr final {
32373237
/// Returns true if this attribute is considered active in the current
32383238
/// compilation context.
32393239
bool isActive(ASTContext &ctx) const;
3240+
3241+
bool operator==(const SemanticAvailableAttr &other) const {
3242+
return other.attr == attr;
3243+
}
3244+
3245+
bool operator!=(const SemanticAvailableAttr &other) const {
3246+
return other.attr != attr;
3247+
}
32403248
};
32413249

32423250
/// An iterable range of `SemanticAvailableAttr`s.

include/swift/AST/Decl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,7 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl>, public Swi
14111411
/// Returns the active platform-specific `@available` attribute for this decl.
14121412
/// There may be multiple `@available` attributes that are relevant to the
14131413
/// current platform, but the returned one has the highest priority.
1414-
const AvailableAttr *getActiveAvailableAttrForCurrentPlatform(
1414+
std::optional<SemanticAvailableAttr> getActiveAvailableAttrForCurrentPlatform(
14151415
bool ignoreAppExtensions = false) const;
14161416

14171417
/// Returns true if the declaration is deprecated at the current deployment

lib/AST/Availability.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -493,26 +493,23 @@ Decl::getSemanticAvailableAttr(const AvailableAttr *attr) const {
493493
return SemanticAvailableAttr(attr, domainForAvailableAttr(attr));
494494
}
495495

496-
const AvailableAttr *
496+
std::optional<SemanticAvailableAttr>
497497
Decl::getActiveAvailableAttrForCurrentPlatform(bool ignoreAppExtensions) const {
498-
const AvailableAttr *bestAttr = nullptr;
499-
500-
for (auto semanticAttr :
501-
getSemanticAvailableAttrs(/*includingInactive=*/false)) {
502-
auto attr = semanticAttr.getParsedAttr();
498+
std::optional<SemanticAvailableAttr> bestAttr;
503499

504-
if (!attr->hasPlatform())
500+
for (auto attr : getSemanticAvailableAttrs(/*includingInactive=*/false)) {
501+
if (!attr.isPlatformSpecific())
505502
continue;
506503

507504
if (ignoreAppExtensions &&
508-
isApplicationExtensionPlatform(attr->getPlatform()))
505+
isApplicationExtensionPlatform(attr.getPlatform()))
509506
continue;
510507

511508
// We have an attribute that is active for the platform, but is it more
512509
// specific than our current best?
513510
if (!bestAttr || inheritsAvailabilityFromPlatform(
514-
attr->getPlatform(), bestAttr->getPlatform())) {
515-
bestAttr = attr;
511+
attr.getPlatform(), bestAttr->getPlatform())) {
512+
bestAttr.emplace(attr);
516513
}
517514
}
518515

@@ -522,13 +519,13 @@ Decl::getActiveAvailableAttrForCurrentPlatform(bool ignoreAppExtensions) const {
522519
const AvailableAttr *Decl::getDeprecatedAttr() const {
523520
auto &ctx = getASTContext();
524521
const AvailableAttr *result = nullptr;
525-
const AvailableAttr *bestActive = getActiveAvailableAttrForCurrentPlatform();
522+
auto bestActive = getActiveAvailableAttrForCurrentPlatform();
526523

527524
for (auto semanticAttr :
528525
getSemanticAvailableAttrs(/*includingInactive=*/false)) {
529526
auto attr = semanticAttr.getParsedAttr();
530527

531-
if (attr->hasPlatform() && (!bestActive || attr != bestActive))
528+
if (attr->hasPlatform() && (!bestActive || semanticAttr != bestActive))
532529
continue;
533530

534531
// Unconditional deprecated.
@@ -560,13 +557,13 @@ const AvailableAttr *Decl::getDeprecatedAttr() const {
560557
const AvailableAttr *Decl::getSoftDeprecatedAttr() const {
561558
auto &ctx = getASTContext();
562559
const AvailableAttr *result = nullptr;
563-
const AvailableAttr *bestActive = getActiveAvailableAttrForCurrentPlatform();
560+
auto bestActive = getActiveAvailableAttrForCurrentPlatform();
564561

565562
for (auto semanticAttr :
566563
getSemanticAvailableAttrs(/*includingInactive=*/false)) {
567564
auto attr = semanticAttr.getParsedAttr();
568565

569-
if (attr->hasPlatform() && (!bestActive || attr != bestActive))
566+
if (attr->hasPlatform() && (!bestActive || semanticAttr != bestActive))
570567
continue;
571568

572569
std::optional<llvm::VersionTuple> deprecatedVersion = attr->Deprecated;
@@ -626,7 +623,7 @@ static const AvailableAttr *
626623
getDeclUnavailableAttr(const Decl *D, bool ignoreAppExtensions) {
627624
auto &ctx = D->getASTContext();
628625
const AvailableAttr *result = nullptr;
629-
const AvailableAttr *bestActive =
626+
auto bestActive =
630627
D->getActiveAvailableAttrForCurrentPlatform(ignoreAppExtensions);
631628

632629
for (auto semanticAttr :
@@ -635,7 +632,7 @@ getDeclUnavailableAttr(const Decl *D, bool ignoreAppExtensions) {
635632

636633
// If this is a platform-specific attribute and it isn't the most
637634
// specific attribute for the current platform, we're done.
638-
if (attr->hasPlatform() && (!bestActive || attr != bestActive))
635+
if (attr->hasPlatform() && (!bestActive || semanticAttr != bestActive))
639636
continue;
640637

641638
if (ignoreAppExtensions &&

lib/Sema/TypeCheckAttr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2184,14 +2184,14 @@ void AttributeChecker::visitAvailableAttr(AvailableAttr *attr) {
21842184
// we're checking an iOS attribute while building for macCatalyst.
21852185
if (attr->getPlatform() == PlatformKind::iOS &&
21862186
isPlatformActive(PlatformKind::macCatalyst, Ctx.LangOpts)) {
2187-
if (attr != D->getActiveAvailableAttrForCurrentPlatform()) {
2187+
if (semanticAttr != D->getActiveAvailableAttrForCurrentPlatform()) {
21882188
return;
21892189
}
21902190
}
21912191

21922192
if (attr->getPlatform() == PlatformKind::iOS &&
21932193
isPlatformActive(PlatformKind::visionOS, Ctx.LangOpts)) {
2194-
if (attr != D->getActiveAvailableAttrForCurrentPlatform()) {
2194+
if (semanticAttr != D->getActiveAvailableAttrForCurrentPlatform()) {
21952195
return;
21962196
}
21972197
}

0 commit comments

Comments
 (0)