Skip to content

Commit 64d020e

Browse files
committed
[NFC] Introduce a convenience specialization of CanTypeVisitor that
forwards the paired nominal type methods to common implementations.
1 parent aa3a075 commit 64d020e

File tree

7 files changed

+82
-48
lines changed

7 files changed

+82
-48
lines changed

include/swift/AST/CanTypeVisitor.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,70 @@ class CanTypeVisitor {
7070
#include "swift/AST/TypeNodes.def"
7171
};
7272

73+
/// This is a convenience refinement of CanTypeVisitor which forwards the
74+
/// paired nominal type methods to a common implementation.
75+
///
76+
/// The delegation flow is:
77+
/// {ClassType, BoundGenericClassType} -> AnyClassType -> AnyNominalType -> Type
78+
/// {EnumType, BoundGenericEnumType} -> AnyEnumType -> AnyNominalType -> Type
79+
/// {StructType, BoundGenericStructType} -> AnyStructType -> AnyNominalType -> Type
80+
/// ProtocolType -> AnyNominalType -> Type
81+
///
82+
/// The new visitAny*Type methods take the appropriate Decl* as their second
83+
/// argument.
84+
template<typename ImplClass, typename RetTy = void, typename... Args>
85+
class CanTypeVisitor_AnyNominal : public CanTypeVisitor<ImplClass, RetTy, Args...> {
86+
public:
87+
RetTy visitClassType(CanClassType T, Args... args) {
88+
return static_cast<ImplClass*>(this)
89+
->visitAnyClassType(T, T->getDecl(), ::std::forward<Args>(args)...);
90+
}
91+
RetTy visitBoundGenericClassType(CanBoundGenericClassType T, Args... args) {
92+
return static_cast<ImplClass*>(this)
93+
->visitAnyClassType(T, T->getDecl(), ::std::forward<Args>(args)...);
94+
}
95+
RetTy visitAnyClassType(CanType T, ClassDecl *D, Args... args) {
96+
return static_cast<ImplClass*>(this)
97+
->visitAnyNominalType(T, D, ::std::forward<Args>(args)...);
98+
}
99+
100+
RetTy visitStructType(CanStructType T, Args... args) {
101+
return static_cast<ImplClass*>(this)
102+
->visitAnyStructType(T, T->getDecl(), ::std::forward<Args>(args)...);
103+
}
104+
RetTy visitBoundGenericStructType(CanBoundGenericStructType T, Args... args) {
105+
return static_cast<ImplClass*>(this)
106+
->visitAnyStructType(T, T->getDecl(), ::std::forward<Args>(args)...);
107+
}
108+
RetTy visitAnyStructType(CanType T, StructDecl *D, Args... args) {
109+
return static_cast<ImplClass*>(this)
110+
->visitAnyNominalType(T, D, ::std::forward<Args>(args)...);
111+
}
112+
113+
RetTy visitEnumType(CanEnumType T, Args... args) {
114+
return static_cast<ImplClass*>(this)
115+
->visitAnyEnumType(T, T->getDecl(), ::std::forward<Args>(args)...);
116+
}
117+
RetTy visitBoundGenericEnumType(CanBoundGenericEnumType T, Args... args) {
118+
return static_cast<ImplClass*>(this)
119+
->visitAnyEnumType(T, T->getDecl(), ::std::forward<Args>(args)...);
120+
}
121+
RetTy visitAnyEnumType(CanType T, EnumDecl *D, Args... args) {
122+
return static_cast<ImplClass*>(this)
123+
->visitAnyNominalType(T, D, ::std::forward<Args>(args)...);
124+
}
125+
126+
RetTy visitProtocolType(CanProtocolType T, Args... args) {
127+
return static_cast<ImplClass*>(this)
128+
->visitAnyNominalType(T, T->getDecl(), ::std::forward<Args>(args)...);
129+
}
130+
131+
RetTy visitAnyNominalType(CanType T, NominalTypeDecl *D, Args... args) {
132+
return static_cast<ImplClass*>(this)
133+
->visitType(T, ::std::forward<Args>(args)...);
134+
}
135+
};
136+
73137
} // end namespace swift
74138

75139
#endif

lib/IRGen/GenMeta.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "swift/AST/ASTContext.h"
2222
#include "swift/AST/ASTMangler.h"
2323
#include "swift/AST/Attr.h"
24-
#include "swift/AST/CanTypeVisitor.h"
2524
#include "swift/AST/Decl.h"
2625
#include "swift/AST/DiagnosticsIRGen.h"
2726
#include "swift/AST/GenericEnvironment.h"

lib/IRGen/GenProto.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
//===----------------------------------------------------------------------===//
2929

3030
#include "swift/AST/ASTContext.h"
31-
#include "swift/AST/CanTypeVisitor.h"
3231
#include "swift/AST/Types.h"
3332
#include "swift/AST/ConformanceLookup.h"
3433
#include "swift/AST/Decl.h"

lib/IRGen/GenType.cpp

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2425,7 +2425,8 @@ namespace {
24252425
/// type mismatch) if an aggregate type containing a value of this
24262426
/// type is generated generically rather than independently for
24272427
/// different specializations?
2428-
class IsIRTypeDependent : public CanTypeVisitor<IsIRTypeDependent, bool> {
2428+
class IsIRTypeDependent :
2429+
public CanTypeVisitor_AnyNominal<IsIRTypeDependent, bool> {
24292430
IRGenModule &IGM;
24302431
public:
24312432
IsIRTypeDependent(IRGenModule &IGM) : IGM(IGM) {}
@@ -2439,11 +2440,8 @@ namespace {
24392440

24402441
// Dependent struct types need their own implementation if any
24412442
// field type might need its own implementation.
2442-
bool visitStructType(CanStructType type) {
2443-
return visitStructDecl(type->getDecl());
2444-
}
2445-
bool visitBoundGenericStructType(CanBoundGenericStructType type) {
2446-
return visitStructDecl(type->getDecl());
2443+
bool visitAnyStructType(CanType type, StructDecl *decl) {
2444+
return visitStructDecl(decl);
24472445
}
24482446
bool visitStructDecl(StructDecl *decl) {
24492447
if (IGM.isResilient(decl, ResilienceExpansion::Maximal))
@@ -2472,11 +2470,8 @@ namespace {
24722470

24732471
// Dependent enum types need their own implementation if any
24742472
// element payload type might need its own implementation.
2475-
bool visitEnumType(CanEnumType type) {
2476-
return visitEnumDecl(type->getDecl());
2477-
}
2478-
bool visitBoundGenericEnumType(CanBoundGenericEnumType type) {
2479-
return visitEnumDecl(type->getDecl());
2473+
bool visitAnyEnumType(CanType type, EnumDecl *decl) {
2474+
return visitEnumDecl(decl);
24802475
}
24812476
bool visitEnumDecl(EnumDecl *decl) {
24822477
if (IGM.isResilient(decl, ResilienceExpansion::Maximal))
@@ -2495,11 +2490,8 @@ namespace {
24952490
}
24962491

24972492
// Conservatively assume classes need unique implementations.
2498-
bool visitClassType(CanClassType type) {
2499-
return visitClassDecl(type->getDecl());
2500-
}
2501-
bool visitBoundGenericClassType(CanBoundGenericClassType type) {
2502-
return visitClassDecl(type->getDecl());
2493+
bool visitAnyClassType(CanType type, ClassDecl *theClass) {
2494+
return visitClassDecl(theClass);
25032495
}
25042496
bool visitClassDecl(ClassDecl *theClass) {
25052497
return true;

lib/IRGen/MetadataRequest.cpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3615,8 +3615,8 @@ namespace {
36153615
/// valid recursive types bottom out in fixed-sized types like classes
36163616
/// or pointers.)
36173617
class EmitTypeLayoutRef
3618-
: public CanTypeVisitor<EmitTypeLayoutRef, llvm::Value *,
3619-
DynamicMetadataRequest> {
3618+
: public CanTypeVisitor_AnyNominal<EmitTypeLayoutRef, llvm::Value *,
3619+
DynamicMetadataRequest> {
36203620
private:
36213621
IRGenFunction &IGF;
36223622
public:
@@ -3755,10 +3755,9 @@ namespace {
37553755
llvm_unreachable("Not a valid MetatypeRepresentation.");
37563756
}
37573757

3758-
llvm::Value *visitAnyClassType(ClassDecl *classDecl,
3758+
llvm::Value *visitAnyClassType(CanType type, ClassDecl *classDecl,
37593759
DynamicMetadataRequest request) {
37603760
// All class types have the same layout.
3761-
auto type = classDecl->getDeclaredType()->getCanonicalType();
37623761
switch (type->getReferenceCounting()) {
37633762
case ReferenceCounting::Native:
37643763
return emitFromValueWitnessTable(IGF.IGM.Context.TheNativeObjectType);
@@ -3779,16 +3778,6 @@ namespace {
37793778
llvm_unreachable("Not a valid ReferenceCounting.");
37803779
}
37813780

3782-
llvm::Value *visitClassType(CanClassType type,
3783-
DynamicMetadataRequest request) {
3784-
return visitAnyClassType(type->getClassOrBoundGenericClass(), request);
3785-
}
3786-
3787-
llvm::Value *visitBoundGenericClassType(CanBoundGenericClassType type,
3788-
DynamicMetadataRequest request) {
3789-
return visitAnyClassType(type->getClassOrBoundGenericClass(), request);
3790-
}
3791-
37923781
llvm::Value *visitPackType(CanPackType type,
37933782
DynamicMetadataRequest request) {
37943783
llvm_unreachable("");

lib/SIL/IR/SILFunctionType.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#define DEBUG_TYPE "libsil"
2020

2121
#include "swift/AST/AnyFunctionRef.h"
22-
#include "swift/AST/CanTypeVisitor.h"
2322
#include "swift/AST/Decl.h"
2423
#include "swift/AST/DiagnosticsSIL.h"
2524
#include "swift/AST/ForeignInfo.h"

lib/SILOptimizer/Utils/InstOptUtils.cpp

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,8 @@ swift::castValueToABICompatibleType(SILBuilder *builder, SILPassManager *pm,
774774
}
775775

776776
namespace {
777-
class TypeDependentVisitor : public CanTypeVisitor<TypeDependentVisitor, bool> {
777+
class TypeDependentVisitor :
778+
public CanTypeVisitor_AnyNominal<TypeDependentVisitor, bool> {
778779
public:
779780
// If the type isn't actually dependent, we're okay.
780781
bool visit(CanType type) {
@@ -783,11 +784,8 @@ namespace {
783784
return CanTypeVisitor::visit(type);
784785
}
785786

786-
bool visitStructType(CanStructType type) {
787-
return visitStructDecl(type->getDecl());
788-
}
789-
bool visitBoundGenericStructType(CanBoundGenericStructType type) {
790-
return visitStructDecl(type->getDecl());
787+
bool visitAnyStructType(CanType type, StructDecl *decl) {
788+
return visitStructDecl(decl);
791789
}
792790
bool visitStructDecl(StructDecl *decl) {
793791
auto rawLayout = decl->getAttrs().getAttribute<RawLayoutAttr>();
@@ -806,11 +804,8 @@ namespace {
806804
return false;
807805
}
808806

809-
bool visitEnumType(CanEnumType type) {
810-
return visitEnumDecl(type->getDecl());
811-
}
812-
bool visitBoundGenericEnumType(CanBoundGenericEnumType type) {
813-
return visitEnumDecl(type->getDecl());
807+
bool visitAnyEnumType(CanType type, EnumDecl *decl) {
808+
return visitEnumDecl(decl);
814809
}
815810
bool visitEnumDecl(EnumDecl *decl) {
816811
if (decl->isIndirect())
@@ -835,12 +830,9 @@ namespace {
835830
}
836831

837832
// A class reference does not depend on the layout of the class.
838-
bool visitClassType(CanClassType type) {
833+
bool visitAnyClassType(CanType type, ClassDecl *decl) {
839834
return false;
840835
}
841-
bool visitBoundGenericClassType(CanBoundGenericClassType type) {
842-
return false;
843-
}
844836

845837
// The same for non-strong references.
846838
bool visitReferenceStorageType(CanReferenceStorageType type) {

0 commit comments

Comments
 (0)