Skip to content

Commit 2bc0427

Browse files
committed
AST: Update isBetterThan() to take SemanticAvailableAttrs.
1 parent 000c9da commit 2bc0427

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

include/swift/AST/Attr.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3189,6 +3189,18 @@ class SemanticAvailableAttr final {
31893189
const AvailableAttr *getParsedAttr() const { return attr; }
31903190
const AvailabilityDomain getDomain() const { return domain; }
31913191

3192+
std::optional<llvm::VersionTuple> getIntroduced() const {
3193+
return attr->Introduced;
3194+
}
3195+
3196+
std::optional<llvm::VersionTuple> getDeprecated() const {
3197+
return attr->Deprecated;
3198+
}
3199+
3200+
std::optional<llvm::VersionTuple> getObsoleted() const {
3201+
return attr->Obsoleted;
3202+
}
3203+
31923204
/// Returns the `message:` field of the attribute, or an empty string.
31933205
StringRef getMessage() const { return attr->Message; }
31943206

lib/AST/Availability.cpp

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -294,20 +294,18 @@ AvailabilityInference::parentDeclForInferredAvailability(const Decl *D) {
294294
/// Returns true if the introduced version in \p newAttr should be used instead
295295
/// of the introduced version in \p prevAttr when both are attached to the same
296296
/// declaration and refer to the active platform.
297-
static bool isBetterThan(const AvailableAttr *newAttr,
298-
const AvailableAttr *prevAttr) {
299-
assert(newAttr);
300-
297+
static bool isBetterThan(const SemanticAvailableAttr &newAttr,
298+
const std::optional<SemanticAvailableAttr> &prevAttr) {
301299
// If there is no prevAttr, newAttr of course wins.
302300
if (!prevAttr)
303301
return true;
304302

305303
// If they belong to the same platform, the one that introduces later wins.
306-
if (prevAttr->getPlatform() == newAttr->getPlatform())
307-
return prevAttr->Introduced.value() < newAttr->Introduced.value();
304+
if (prevAttr->getPlatform() == newAttr.getPlatform())
305+
return prevAttr->getIntroduced().value() < newAttr.getIntroduced().value();
308306

309307
// If the new attribute's platform inherits from the old one, it wins.
310-
return inheritsAvailabilityFromPlatform(newAttr->getPlatform(),
308+
return inheritsAvailabilityFromPlatform(newAttr.getPlatform(),
311309
prevAttr->getPlatform());
312310
}
313311

@@ -433,22 +431,19 @@ bool AvailabilityInference::updateBeforePlatformForFallback(
433431

434432
const AvailableAttr *
435433
AvailabilityInference::attrForAnnotatedAvailableRange(const Decl *D) {
436-
const AvailableAttr *bestAvailAttr = nullptr;
434+
std::optional<SemanticAvailableAttr> bestAvailAttr;
437435

438436
D = abstractSyntaxDeclForAvailableAttribute(D);
439437

440-
for (auto semanticAttr :
441-
D->getSemanticAvailableAttrs(/*includingInactive=*/false)) {
442-
auto *attr = semanticAttr.getParsedAttr();
443-
444-
if (!attr->hasPlatform() || !attr->Introduced.has_value())
438+
for (auto attr : D->getSemanticAvailableAttrs(/*includingInactive=*/false)) {
439+
if (!attr.isPlatformSpecific() || !attr.getIntroduced())
445440
continue;
446441

447442
if (isBetterThan(attr, bestAvailAttr))
448-
bestAvailAttr = attr;
443+
bestAvailAttr.emplace(attr);
449444
}
450445

451-
return bestAvailAttr;
446+
return bestAvailAttr ? bestAvailAttr->getParsedAttr() : nullptr;
452447
}
453448

454449
std::optional<AvailabilityRange>
@@ -779,25 +774,24 @@ bool Decl::requiresUnavailableDeclABICompatibilityStubs() const {
779774

780775
AvailabilityRange AvailabilityInference::annotatedAvailableRangeForAttr(
781776
const Decl *D, const SpecializeAttr *attr, ASTContext &ctx) {
782-
783-
const AvailableAttr *bestAvailAttr = nullptr;
777+
std::optional<SemanticAvailableAttr> bestAvailAttr;
784778

785779
for (auto *availAttr : attr->getAvailableAttrs()) {
786780
auto semanticAttr = D->getSemanticAvailableAttr(availAttr);
787781
if (!semanticAttr)
788782
continue;
789783

790-
if (!availAttr->Introduced.has_value() || !semanticAttr->isActive(ctx) ||
784+
if (!semanticAttr->getIntroduced() || !semanticAttr->isActive(ctx) ||
791785
!semanticAttr->isPlatformSpecific()) {
792786
continue;
793787
}
794788

795-
if (isBetterThan(availAttr, bestAvailAttr))
796-
bestAvailAttr = availAttr;
789+
if (isBetterThan(*semanticAttr, bestAvailAttr))
790+
bestAvailAttr.emplace(*semanticAttr);
797791
}
798792

799793
if (bestAvailAttr)
800-
return availableRange(bestAvailAttr, ctx);
794+
return availableRange(bestAvailAttr->getParsedAttr(), ctx);
801795

802796
return AvailabilityRange::alwaysAvailable();
803797
}

0 commit comments

Comments
 (0)