Skip to content

Commit 537954f

Browse files
authored
[AST] Rename several DeclContext methods to be clearer and shorter (#18798)
- getAsDeclOrDeclExtensionContext -> getAsDecl This is basically the same as a dyn_cast, so it should use a 'getAs' name like TypeBase does. - getAsNominalTypeOrNominalTypeExtensionContext -> getSelfNominalTypeDecl - getAsClassOrClassExtensionContext -> getSelfClassDecl - getAsEnumOrEnumExtensionContext -> getSelfEnumDecl - getAsStructOrStructExtensionContext -> getSelfStructDecl - getAsProtocolOrProtocolExtensionContext -> getSelfProtocolDecl - getAsTypeOrTypeExtensionContext -> getSelfTypeDecl (private) These do /not/ return some form of 'this'; instead, they get the extended types when 'this' is an extension. They started off life with 'is' names, which makes sense, but changed to this at some point. The names I went with match up with getSelfInterfaceType and getSelfTypeInContext, even though strictly speaking they're closer to what getDeclaredInterfaceType does. But it didn't seem right to claim that an extension "declares" the ClassDecl here. - getAsProtocolExtensionContext -> getExtendedProtocolDecl Like the above, this didn't return the ExtensionDecl; it returned its extended type. This entire commit is a mechanical change: find-and-replace, followed by manual reformatted but no code changes.
1 parent 1ea3ebc commit 537954f

File tree

87 files changed

+387
-490
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+387
-490
lines changed

include/swift/AST/Decl.h

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,7 +1786,7 @@ class ExtensionDecl final : public GenericContext, public Decl,
17861786
return D->getKind() == DeclKind::Extension;
17871787
}
17881788
static bool classof(const DeclContext *C) {
1789-
if (auto D = C->getAsDeclOrDeclExtensionContext())
1789+
if (auto D = C->getAsDecl())
17901790
return classof(D);
17911791
return false;
17921792
}
@@ -2117,7 +2117,7 @@ class TopLevelCodeDecl : public DeclContext, public Decl {
21172117
return D->getKind() == DeclKind::TopLevelCode;
21182118
}
21192119
static bool classof(const DeclContext *C) {
2120-
if (auto D = C->getAsDeclOrDeclExtensionContext())
2120+
if (auto D = C->getAsDecl())
21212121
return classof(D);
21222122
return false;
21232123
}
@@ -2645,7 +2645,7 @@ class GenericTypeDecl : public GenericContext, public TypeDecl {
26452645
using TypeDecl::getDeclaredInterfaceType;
26462646

26472647
static bool classof(const DeclContext *C) {
2648-
if (auto D = C->getAsDeclOrDeclExtensionContext())
2648+
if (auto D = C->getAsDecl())
26492649
return classof(D);
26502650
return false;
26512651
}
@@ -2719,7 +2719,7 @@ class TypeAliasDecl : public GenericTypeDecl {
27192719
return D->getKind() == DeclKind::TypeAlias;
27202720
}
27212721
static bool classof(const DeclContext *C) {
2722-
if (auto D = C->getAsDeclOrDeclExtensionContext())
2722+
if (auto D = C->getAsDecl())
27232723
return classof(D);
27242724
return false;
27252725
}
@@ -3205,7 +3205,7 @@ class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {
32053205
}
32063206

32073207
static bool classof(const DeclContext *C) {
3208-
if (auto D = C->getAsDeclOrDeclExtensionContext())
3208+
if (auto D = C->getAsDecl())
32093209
return classof(D);
32103210
return false;
32113211
}
@@ -3313,7 +3313,7 @@ class EnumDecl final : public NominalTypeDecl {
33133313
return D->getKind() == DeclKind::Enum;
33143314
}
33153315
static bool classof(const DeclContext *C) {
3316-
if (auto D = C->getAsDeclOrDeclExtensionContext())
3316+
if (auto D = C->getAsDecl())
33173317
return classof(D);
33183318
return false;
33193319
}
@@ -3408,7 +3408,7 @@ class StructDecl final : public NominalTypeDecl {
34083408
return D->getKind() == DeclKind::Struct;
34093409
}
34103410
static bool classof(const DeclContext *C) {
3411-
if (auto D = C->getAsDeclOrDeclExtensionContext())
3411+
if (auto D = C->getAsDecl())
34123412
return classof(D);
34133413
return false;
34143414
}
@@ -3693,7 +3693,7 @@ class ClassDecl final : public NominalTypeDecl {
36933693
return D->getKind() == DeclKind::Class;
36943694
}
36953695
static bool classof(const DeclContext *C) {
3696-
if (auto D = C->getAsDeclOrDeclExtensionContext())
3696+
if (auto D = C->getAsDecl())
36973697
return classof(D);
36983698
return false;
36993699
}
@@ -4025,7 +4025,7 @@ class ProtocolDecl final : public NominalTypeDecl {
40254025
return D->getKind() == DeclKind::Protocol;
40264026
}
40274027
static bool classof(const DeclContext *C) {
4028-
if (auto D = C->getAsDeclOrDeclExtensionContext())
4028+
if (auto D = C->getAsDecl())
40294029
return classof(D);
40304030
return false;
40314031
}
@@ -4900,7 +4900,7 @@ class SubscriptDecl : public GenericContext, public AbstractStorageDecl {
49004900
}
49014901

49024902
static bool classof(const DeclContext *DC) {
4903-
if (auto D = DC->getAsDeclOrDeclExtensionContext())
4903+
if (auto D = DC->getAsDecl())
49044904
return classof(D);
49054905
return false;
49064906
}
@@ -5281,7 +5281,7 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
52815281
}
52825282

52835283
static bool classof(const DeclContext *DC) {
5284-
if (auto D = DC->getAsDeclOrDeclExtensionContext())
5284+
if (auto D = DC->getAsDecl())
52855285
return classof(D);
52865286
return false;
52875287
}
@@ -5508,7 +5508,7 @@ class FuncDecl : public AbstractFunctionDecl {
55085508
return classof(static_cast<const Decl*>(D));
55095509
}
55105510
static bool classof(const DeclContext *DC) {
5511-
if (auto D = DC->getAsDeclOrDeclExtensionContext())
5511+
if (auto D = DC->getAsDecl())
55125512
return classof(D);
55135513
return false;
55145514
}
@@ -5648,7 +5648,7 @@ class AccessorDecl final : public FuncDecl {
56485648
return classof(static_cast<const Decl*>(D));
56495649
}
56505650
static bool classof(const DeclContext *DC) {
5651-
if (auto D = DC->getAsDeclOrDeclExtensionContext())
5651+
if (auto D = DC->getAsDecl())
56525652
return classof(D);
56535653
return false;
56545654
}
@@ -6042,7 +6042,7 @@ class ConstructorDecl : public AbstractFunctionDecl {
60426042
return classof(static_cast<const Decl*>(D));
60436043
}
60446044
static bool classof(const DeclContext *DC) {
6045-
if (auto D = DC->getAsDeclOrDeclExtensionContext())
6045+
if (auto D = DC->getAsDecl())
60466046
return classof(D);
60476047
return false;
60486048
}
@@ -6078,7 +6078,7 @@ class DestructorDecl : public AbstractFunctionDecl {
60786078
return classof(static_cast<const Decl*>(D));
60796079
}
60806080
static bool classof(const DeclContext *DC) {
6081-
if (auto D = DC->getAsDeclOrDeclExtensionContext())
6081+
if (auto D = DC->getAsDecl())
60826082
return classof(D);
60836083
return false;
60846084
}
@@ -6581,7 +6581,7 @@ inline bool Decl::isPotentiallyOverridable() const {
65816581
isa<SubscriptDecl>(this) ||
65826582
isa<FuncDecl>(this) ||
65836583
isa<DestructorDecl>(this)) {
6584-
return getDeclContext()->getAsClassOrClassExtensionContext();
6584+
return getDeclContext()->getSelfClassDecl();
65856585
} else {
65866586
return false;
65876587
}
@@ -6602,7 +6602,7 @@ inline const GenericContext *Decl::getAsGenericContext() const {
66026602
}
66036603

66046604
inline bool DeclContext::isExtensionContext() const {
6605-
if (auto D = getAsDeclOrDeclExtensionContext())
6605+
if (auto D = getAsDecl())
66066606
return ExtensionDecl::classof(D);
66076607
return false;
66086608
}

include/swift/AST/DeclContext.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {
213213

214214
/// If this DeclContext is a GenericType declaration or an
215215
/// extension thereof, return the GenericTypeDecl.
216-
GenericTypeDecl *getAsTypeOrTypeExtensionContext() const;
216+
GenericTypeDecl *getSelfTypeDecl() const;
217217

218218
static ASTHierarchy getASTHierarchyFromKind(DeclContextKind Kind) {
219219
switch (Kind) {
@@ -238,12 +238,12 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {
238238

239239
public:
240240
LLVM_READONLY
241-
Decl *getAsDeclOrDeclExtensionContext() {
241+
Decl *getAsDecl() {
242242
return ParentAndKind.getInt() == ASTHierarchy::Decl ?
243243
reinterpret_cast<Decl*>(this + 1) : nullptr;
244244
}
245-
const Decl *getAsDeclOrDeclExtensionContext() const {
246-
return const_cast<DeclContext*>(this)->getAsDeclOrDeclExtensionContext();
245+
const Decl *getAsDecl() const {
246+
return const_cast<DeclContext*>(this)->getAsDecl();
247247
}
248248

249249
DeclContext(DeclContextKind Kind, DeclContext *Parent)
@@ -283,36 +283,36 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {
283283
/// If this DeclContext is a NominalType declaration or an
284284
/// extension thereof, return the NominalTypeDecl.
285285
LLVM_READONLY
286-
NominalTypeDecl *getAsNominalTypeOrNominalTypeExtensionContext() const;
286+
NominalTypeDecl *getSelfNominalTypeDecl() const;
287287

288288
/// If this DeclContext is a class, or an extension on a class, return the
289289
/// ClassDecl, otherwise return null.
290290
LLVM_READONLY
291-
ClassDecl *getAsClassOrClassExtensionContext() const;
291+
ClassDecl *getSelfClassDecl() const;
292292

293293
/// If this DeclContext is an enum, or an extension on an enum, return the
294294
/// EnumDecl, otherwise return null.
295295
LLVM_READONLY
296-
EnumDecl *getAsEnumOrEnumExtensionContext() const;
296+
EnumDecl *getSelfEnumDecl() const;
297297

298298
/// If this DeclContext is a struct, or an extension on a struct, return the
299299
/// StructDecl, otherwise return null.
300300
LLVM_READONLY
301-
StructDecl *getAsStructOrStructExtensionContext() const;
301+
StructDecl *getSelfStructDecl() const;
302302

303303
/// If this DeclContext is a protocol, or an extension on a
304304
/// protocol, return the ProtocolDecl, otherwise return null.
305305
LLVM_READONLY
306-
ProtocolDecl *getAsProtocolOrProtocolExtensionContext() const;
306+
ProtocolDecl *getSelfProtocolDecl() const;
307307

308308
/// If this DeclContext is a protocol extension, return the extended protocol.
309309
LLVM_READONLY
310-
ProtocolDecl *getAsProtocolExtensionContext() const;
310+
ProtocolDecl *getExtendedProtocolDecl() const;
311311

312312
/// \brief Retrieve the generic parameter 'Self' from a protocol or
313313
/// protocol extension.
314314
///
315-
/// Only valid if \c getAsProtocolOrProtocolExtensionContext().
315+
/// Only valid if \c getSelfProtocolDecl().
316316
GenericTypeParamType *getProtocolSelfType() const;
317317

318318
/// Gets the type being declared by this context.

include/swift/AST/Module.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ class ModuleDecl : public DeclContext, public TypeDecl {
484484
SourceRange getSourceRange() const { return SourceRange(); }
485485

486486
static bool classof(const DeclContext *DC) {
487-
if (auto D = DC->getAsDeclOrDeclExtensionContext())
487+
if (auto D = DC->getAsDecl())
488488
return classof(D);
489489
return false;
490490
}
@@ -1231,7 +1231,7 @@ class ModuleEntity {
12311231
};
12321232

12331233
inline bool DeclContext::isModuleContext() const {
1234-
if (auto D = getAsDeclOrDeclExtensionContext())
1234+
if (auto D = getAsDecl())
12351235
return ModuleDecl::classof(D);
12361236
return false;
12371237
}

include/swift/SIL/SILInstruction.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5682,8 +5682,7 @@ class WitnessMethodInst final
56825682
public:
56835683
CanType getLookupType() const { return LookupType; }
56845684
ProtocolDecl *getLookupProtocol() const {
5685-
return getMember().getDecl()->getDeclContext()
5686-
->getAsProtocolOrProtocolExtensionContext();
5685+
return getMember().getDecl()->getDeclContext()->getSelfProtocolDecl();
56875686
}
56885687

56895688
ProtocolConformanceRef getConformance() const { return Conformance; }

lib/AST/ASTContext.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2418,8 +2418,7 @@ bool ASTContext::diagnoseUnintendedObjCMethodOverrides(SourceFile &sf) {
24182418
continue;
24192419
}
24202420

2421-
auto classDecl =
2422-
method->getDeclContext()->getAsClassOrClassExtensionContext();
2421+
auto classDecl = method->getDeclContext()->getSelfClassDecl();
24232422
if (!classDecl)
24242423
continue; // error-recovery path, only
24252424

@@ -2698,8 +2697,7 @@ bool ASTContext::diagnoseObjCUnsatisfiedOptReqConflicts(SourceFile &sf) {
26982697
bool anyDiagnosed = false;
26992698
for (const auto &unsatisfied : localReqs) {
27002699
// Check whether there is a conflict here.
2701-
ClassDecl *classDecl =
2702-
unsatisfied.first->getAsClassOrClassExtensionContext();
2700+
ClassDecl *classDecl = unsatisfied.first->getSelfClassDecl();
27032701
auto req = unsatisfied.second;
27042702
auto selector = req->getObjCSelector();
27052703
bool isInstanceMethod = req->isInstanceMember();

lib/AST/ASTMangler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,7 @@ unsigned ASTMangler::appendBoundGenericArgs(DeclContext *dc,
10461046
// This is important when extending a nested type, because the generic
10471047
// parameters will line up with the (semantic) nesting of the nominal type.
10481048
if (auto ext = dyn_cast<ExtensionDecl>(decl))
1049-
decl = ext->getAsNominalTypeOrNominalTypeExtensionContext();
1049+
decl = ext->getSelfNominalTypeDecl();
10501050

10511051
// Handle the generic arguments of the parent.
10521052
unsigned currentGenericParamIdx =
@@ -1424,7 +1424,7 @@ void ASTMangler::appendContext(const DeclContext *ctx) {
14241424
appendModule(ExtD->getParentModule());
14251425
if (sig && ExtD->isConstrainedExtension()) {
14261426
Mod = ExtD->getModuleContext();
1427-
auto nominalSig = ExtD->getAsNominalTypeOrNominalTypeExtensionContext()
1427+
auto nominalSig = ExtD->getSelfNominalTypeDecl()
14281428
->getGenericSignatureOfContext();
14291429
appendGenericSignature(sig, nominalSig);
14301430
}

lib/AST/ASTPrinter.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ PrintOptions PrintOptions::printTextualInterfaceFile() {
8787
if (auto *alias = dyn_cast<TypeAliasDecl>(D)) {
8888
if (alias->isImplicit()) {
8989
const Decl *parent =
90-
D->getDeclContext()->getAsDeclOrDeclExtensionContext();
90+
D->getDeclContext()->getAsDecl();
9191
if (auto *genericCtx = parent->getAsGenericContext()) {
9292
bool matchesGenericParam =
9393
llvm::any_of(genericCtx->getInnermostGenericParamTypes(),
@@ -791,7 +791,7 @@ void PrintAST::printAttributes(const Decl *D) {
791791
// Don't print a redundant 'final' if we are printing a 'static' decl.
792792
unsigned originalExcludeAttrCount = Options.ExcludeAttrList.size();
793793
if (Options.PrintImplicitAttrs &&
794-
D->getDeclContext()->getAsClassOrClassExtensionContext() &&
794+
D->getDeclContext()->getSelfClassDecl() &&
795795
getCorrectStaticSpelling(D) == StaticSpellingKind::KeywordStatic) {
796796
Options.ExcludeAttrList.push_back(DAK_Final);
797797
}
@@ -1411,7 +1411,7 @@ bool ShouldPrintChecker::shouldPrint(const Decl *D,
14111411
// If the Clang declaration is from a protocol but was mirrored into
14121412
// class or extension thereof, treat it as an override.
14131413
if (isa<clang::ObjCProtocolDecl>(clangDecl->getDeclContext()) &&
1414-
VD->getDeclContext()->getAsClassOrClassExtensionContext())
1414+
VD->getDeclContext()->getSelfClassDecl())
14151415
return false;
14161416

14171417
// Check whether Clang considers it an override.
@@ -1874,7 +1874,7 @@ void PrintAST::printExtension(ExtensionDecl *decl) {
18741874
if (auto *genericSig = decl->getGenericSignature()) {
18751875
// For protocol extensions, don't print the 'Self : ...' requirement.
18761876
unsigned flags = PrintRequirements | InnermostOnly;
1877-
if (decl->getAsProtocolExtensionContext())
1877+
if (decl->getExtendedProtocolDecl())
18781878
flags |= SkipSelfRequirement;
18791879
printGenericSignature(genericSig, flags);
18801880
}
@@ -2183,7 +2183,7 @@ void PrintAST::visitProtocolDecl(ProtocolDecl *decl) {
21832183
}
21842184

21852185
static bool isStructOrClassContext(DeclContext *dc) {
2186-
auto *nominal = dc->getAsNominalTypeOrNominalTypeExtensionContext();
2186+
auto *nominal = dc->getSelfNominalTypeDecl();
21872187
if (nominal == nullptr)
21882188
return false;
21892189
return isa<ClassDecl>(nominal) || isa<StructDecl>(nominal);
@@ -3902,7 +3902,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
39023902
Printer << "<anonymous>";
39033903
else {
39043904
if (T->getDecl() &&
3905-
T->getDecl()->getDeclContext()->getAsProtocolOrProtocolExtensionContext()) {
3905+
T->getDecl()->getDeclContext()->getSelfProtocolDecl()) {
39063906
Printer.printTypeRef(T, T->getDecl(), Name);
39073907
return;
39083908
}

lib/AST/ASTVerifier.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2802,7 +2802,7 @@ class Verifier : public ASTWalker {
28022802
void verifyChecked(ConstructorDecl *CD) {
28032803
PrettyStackTraceDecl debugStack("verifying ConstructorDecl", CD);
28042804

2805-
auto *ND = CD->getDeclContext()->getAsNominalTypeOrNominalTypeExtensionContext();
2805+
auto *ND = CD->getDeclContext()->getSelfNominalTypeDecl();
28062806
if (!isa<ClassDecl>(ND) && !isa<StructDecl>(ND) && !isa<EnumDecl>(ND) &&
28072807
!isa<ProtocolDecl>(ND) && !CD->isInvalid()) {
28082808
Out << "ConstructorDecls outside structs, classes or enums "
@@ -2993,7 +2993,7 @@ class Verifier : public ASTWalker {
29932993
void verifyChecked(DestructorDecl *DD) {
29942994
PrettyStackTraceDecl debugStack("verifying DestructorDecl", DD);
29952995

2996-
auto *ND = DD->getDeclContext()->getAsNominalTypeOrNominalTypeExtensionContext();
2996+
auto *ND = DD->getDeclContext()->getSelfNominalTypeDecl();
29972997
if (!isa<ClassDecl>(ND) && !DD->isInvalid()) {
29982998
Out << "DestructorDecls outside classes should be marked invalid";
29992999
abort();
@@ -3009,7 +3009,7 @@ class Verifier : public ASTWalker {
30093009
Out << "mutating function is not an instance member\n";
30103010
abort();
30113011
}
3012-
if (FD->getDeclContext()->getAsClassOrClassExtensionContext()) {
3012+
if (FD->getDeclContext()->getSelfClassDecl()) {
30133013
Out << "mutating function in a class\n";
30143014
abort();
30153015
}

lib/AST/ConformanceLookupTable.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -634,8 +634,7 @@ ConformanceLookupTable::Ordering ConformanceLookupTable::compareConformances(
634634
// If one of the conformances comes from the same file as the type
635635
// declaration, pick that one; this is so that conformance synthesis works if
636636
// there's any implied conformance in the same file as the type.
637-
auto NTD =
638-
lhs->getDeclContext()->getAsNominalTypeOrNominalTypeExtensionContext();
637+
auto NTD = lhs->getDeclContext()->getSelfNominalTypeDecl();
639638
auto typeSF = NTD->getParentSourceFile();
640639
if (typeSF) {
641640
if (typeSF == lhsSF)
@@ -805,8 +804,7 @@ ConformanceLookupTable::getConformance(NominalTypeDecl *nominal,
805804
}
806805
}
807806

808-
auto *conformingNominal =
809-
conformingDC->getAsNominalTypeOrNominalTypeExtensionContext();
807+
auto *conformingNominal = conformingDC->getSelfNominalTypeDecl();
810808

811809
// Form the conformance.
812810
Type type = entry->getDeclContext()->getDeclaredInterfaceType();
@@ -895,7 +893,7 @@ void ConformanceLookupTable::registerProtocolConformance(
895893
bool synthesized) {
896894
auto protocol = conformance->getProtocol();
897895
auto dc = conformance->getDeclContext();
898-
auto nominal = dc->getAsNominalTypeOrNominalTypeExtensionContext();
896+
auto nominal = dc->getSelfNominalTypeDecl();
899897

900898
// If there is an entry to update, do so.
901899
auto &dcConformances = AllConformances[dc];

0 commit comments

Comments
 (0)