Skip to content

Commit 634f281

Browse files
committed
AST: Move parentDeclForInferredAvailability() to Decl.
NFC.
1 parent 4a7e767 commit 634f281

File tree

6 files changed

+22
-32
lines changed

6 files changed

+22
-32
lines changed

include/swift/AST/AvailabilityInference.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ class SemanticAvailableAttr;
3131

3232
class AvailabilityInference {
3333
public:
34-
/// Returns the decl that should be considered the parent decl of the given
35-
/// decl when looking for inherited availability annotations.
36-
static const Decl *parentDeclForInferredAvailability(const Decl *D);
37-
3834
/// Infers the common availability required to access an array of
3935
/// declarations and adds attributes reflecting that availability
4036
/// to ToDecl.

include/swift/AST/Decl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,6 +1521,10 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl>, public Swi
15211521
/// compatibility mode.
15221522
bool requiresUnavailableDeclABICompatibilityStubs() const;
15231523

1524+
/// Returns the decl that should be considered the parent decl when looking
1525+
/// for inherited availability annotations.
1526+
const Decl *parentDeclForAvailability() const;
1527+
15241528
// List the SPI groups declared with @_spi or inherited by this decl.
15251529
//
15261530
// SPI groups are inherited from the parent contexts only if the local decl

lib/AST/Availability.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ void AvailabilityInference::applyInferredAvailableAttrs(
197197

198198
// Walk up the enclosing declaration hierarchy to make sure we aren't
199199
// missing any inherited attributes.
200-
D = AvailabilityInference::parentDeclForInferredAvailability(D);
200+
D = D->parentDeclForAvailability();
201201
} while (D);
202202
}
203203

@@ -213,32 +213,31 @@ void AvailabilityInference::applyInferredAvailableAttrs(
213213

214214
/// Returns the decl that should be considered the parent decl of the given decl
215215
/// when looking for inherited availability annotations.
216-
const Decl *
217-
AvailabilityInference::parentDeclForInferredAvailability(const Decl *D) {
218-
if (auto *AD = dyn_cast<AccessorDecl>(D))
216+
const Decl *Decl::parentDeclForAvailability() const {
217+
if (auto *AD = dyn_cast<AccessorDecl>(this))
219218
return AD->getStorage();
220219

221-
if (auto *ED = dyn_cast<ExtensionDecl>(D)) {
220+
if (auto *ED = dyn_cast<ExtensionDecl>(this)) {
222221
if (auto *NTD = ED->getExtendedNominal())
223222
return NTD;
224223
}
225224

226-
if (auto *PBD = dyn_cast<PatternBindingDecl>(D)) {
225+
if (auto *PBD = dyn_cast<PatternBindingDecl>(this)) {
227226
if (PBD->getNumPatternEntries() < 1)
228227
return nullptr;
229228

230229
return PBD->getAnchoringVarDecl(0);
231230
}
232231

233-
if (auto *OTD = dyn_cast<OpaqueTypeDecl>(D))
232+
if (auto *OTD = dyn_cast<OpaqueTypeDecl>(this))
234233
return OTD->getNamingDecl();
235234

236235
// Clang decls may be inaccurately parented rdar://53956555
237-
if (D->hasClangNode())
236+
if (hasClangNode())
238237
return nullptr;
239238

240239
// Availability is inherited from the enclosing context.
241-
return D->getDeclContext()->getInnermostDeclarationDeclContext();
240+
return getDeclContext()->getInnermostDeclarationDeclContext();
242241
}
243242

244243
/// Returns true if the introduced version in \p newAttr should be used instead
@@ -699,10 +698,8 @@ DeclRuntimeAvailability
699698
DeclRuntimeAvailabilityRequest::evaluate(Evaluator &evaluator,
700699
const Decl *decl) const {
701700
auto inherited = DeclRuntimeAvailability::PotentiallyAvailable;
702-
if (auto *parent =
703-
AvailabilityInference::parentDeclForInferredAvailability(decl)) {
701+
if (auto *parent = decl->parentDeclForAvailability())
704702
inherited = getDeclRuntimeAvailability(parent);
705-
}
706703

707704
// If the inherited runtime availability is already maximally unavailable
708705
// then skip computing unavailability for this declaration.
@@ -788,8 +785,7 @@ Decl::getAvailableAttrForPlatformIntroduction(bool checkExtension) const {
788785
if (!checkExtension)
789786
return std::nullopt;
790787

791-
if (auto parent =
792-
AvailabilityInference::parentDeclForInferredAvailability(this)) {
788+
if (auto parent = parentDeclForAvailability()) {
793789
if (auto *ED = dyn_cast<ExtensionDecl>(parent)) {
794790
if (auto attr = getDeclAvailableAttrForPlatformIntroduction(ED))
795791
return attr;

lib/AST/AvailabilityConstraint.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "swift/AST/AvailabilityConstraint.h"
1414
#include "swift/AST/ASTContext.h"
1515
#include "swift/AST/AvailabilityContext.h"
16-
#include "swift/AST/AvailabilityInference.h"
1716
#include "swift/AST/Decl.h"
1817

1918
using namespace swift;
@@ -319,7 +318,7 @@ swift::getAvailabilityConstraintsForDecl(const Decl *decl,
319318
if (decl->getClangNode())
320319
return constraints;
321320

322-
auto parent = AvailabilityInference::parentDeclForInferredAvailability(decl);
321+
auto parent = decl->parentDeclForAvailability();
323322
if (auto extension = dyn_cast_or_null<ExtensionDecl>(parent))
324323
getAvailabilityConstraintsForDecl(constraints, extension, context, flags);
325324

lib/AST/Decl.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -591,8 +591,7 @@ const Decl *Decl::getInnermostDeclWithAvailability() const {
591591
if (getAttrs().hasAttribute<AvailableAttr>())
592592
return this;
593593

594-
if (auto parent =
595-
AvailabilityInference::parentDeclForInferredAvailability(this))
594+
if (auto parent = parentDeclForAvailability())
596595
return parent->getInnermostDeclWithAvailability();
597596

598597
return nullptr;
@@ -1492,7 +1491,7 @@ AvailabilityRange Decl::getAvailabilityForLinkage() const {
14921491
return *containingContext;
14931492
}
14941493

1495-
// FIXME: Adopt AvailabilityInference::parentDeclForInferredAvailability()
1494+
// FIXME: Adopt Decl::parentDeclForAvailability()
14961495
// here instead of duplicating the logic.
14971496
if (auto *accessor = dyn_cast<AccessorDecl>(this))
14981497
return accessor->getStorage()->getAvailabilityForLinkage();

lib/Sema/TypeCheckAttr.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2205,8 +2205,7 @@ getSemanticAvailableRangeDeclAndAttr(const Decl *decl) {
22052205
/*checkExtension=*/false))
22062206
return std::make_pair(*attr, decl);
22072207

2208-
if (auto *parent =
2209-
AvailabilityInference::parentDeclForInferredAvailability(decl))
2208+
if (auto *parent = decl->parentDeclForAvailability())
22102209
return getSemanticAvailableRangeDeclAndAttr(parent);
22112210

22122211
return std::nullopt;
@@ -5059,8 +5058,7 @@ void AttributeChecker::checkAvailableAttrs(ArrayRef<AvailableAttr *> attrs) {
50595058
// Compute availability constraints for the decl, relative to its parent
50605059
// declaration or to the deployment target.
50615060
auto availabilityContext = AvailabilityContext::forDeploymentTarget(Ctx);
5062-
if (auto parent =
5063-
AvailabilityInference::parentDeclForInferredAvailability(D)) {
5061+
if (auto parent = D->parentDeclForAvailability()) {
50645062
auto parentAvailability = AvailabilityContext::forDeclSignature(parent);
50655063
availabilityContext.constrainWithContext(parentAvailability, Ctx);
50665064
}
@@ -5237,8 +5235,7 @@ void AttributeChecker::checkBackDeployedAttrs(
52375235
break;
52385236
}
52395237

5240-
attrDecl =
5241-
AvailabilityInference::parentDeclForInferredAvailability(attrDecl);
5238+
attrDecl = attrDecl->parentDeclForAvailability();
52425239
} while (attrDecl);
52435240

52445241
continue;
@@ -5424,10 +5421,9 @@ std::optional<Diagnostic>
54245421
TypeChecker::diagnosticIfDeclCannotBeUnavailable(const Decl *D,
54255422
SemanticAvailableAttr attr) {
54265423
auto parentIsUnavailable = [](const Decl *D) -> bool {
5427-
if (auto *parent =
5428-
AvailabilityInference::parentDeclForInferredAvailability(D)) {
5424+
if (auto *parent = D->parentDeclForAvailability())
54295425
return AvailabilityContext::forDeclSignature(parent).isUnavailable();
5430-
}
5426+
54315427
return false;
54325428
};
54335429

0 commit comments

Comments
 (0)