18
18
#include " swift/AST/Module.h"
19
19
#include " swift/AST/ProtocolConformance.h"
20
20
#include " swift/AST/SwiftNameTranslation.h"
21
- #include " swift/AST/TypeVisitor .h"
21
+ #include " swift/AST/TypeDeclFinder .h"
22
22
#include " swift/ClangImporter/ClangImporter.h"
23
23
24
24
#include " clang/AST/Decl.h"
@@ -39,92 +39,29 @@ static bool isOSObjectType(const clang::Decl *decl) {
39
39
}
40
40
41
41
namespace {
42
- class ReferencedTypeFinder : public TypeVisitor <ReferencedTypeFinder> {
43
- friend TypeVisitor ;
42
+ class ReferencedTypeFinder : public TypeDeclFinder {
43
+ friend TypeDeclFinder ;
44
44
45
- ModuleDecl &M;
46
45
llvm::function_ref<void (ReferencedTypeFinder &, const TypeDecl *)> Callback;
47
46
bool NeedsDefinition = false ;
48
47
49
- ReferencedTypeFinder (ModuleDecl &mod, decltype (Callback) callback)
50
- : M(mod), Callback(callback) {}
48
+ explicit ReferencedTypeFinder (decltype (Callback) callback)
49
+ : Callback(callback) {}
51
50
52
- void visitType (TypeBase *base) {
53
- llvm_unreachable (" unhandled type" );
51
+ Action visitNominalType (NominalType *nominal) override {
52
+ Callback (*this , nominal->getDecl ());
53
+ return Action::SkipChildren;
54
54
}
55
55
56
- void visitTypeAliasType (TypeAliasType *aliasTy) {
56
+ Action visitTypeAliasType (TypeAliasType *aliasTy) override {
57
57
if (aliasTy->getDecl ()->hasClangNode () &&
58
58
!aliasTy->getDecl ()->isCompatibilityAlias ()) {
59
+ assert (!aliasTy->getGenericSignature ());
59
60
Callback (*this , aliasTy->getDecl ());
60
61
} else {
61
- visit (aliasTy->getSinglyDesugaredType ());
62
+ Type (aliasTy->getSinglyDesugaredType ()). walk (* this );
62
63
}
63
- }
64
-
65
- void visitParenType (ParenType *parenTy) {
66
- visit (parenTy->getSinglyDesugaredType ());
67
- }
68
-
69
- void visitTupleType (TupleType *tupleTy) {
70
- for (auto elemTy : tupleTy->getElementTypes ())
71
- visit (elemTy);
72
- }
73
-
74
- void visitReferenceStorageType (ReferenceStorageType *ty) {
75
- visit (ty->getReferentType ());
76
- }
77
-
78
- void visitNominalType (NominalType *nominal) {
79
- Callback (*this , nominal->getDecl ());
80
- }
81
-
82
- void visitAnyMetatypeType (AnyMetatypeType *metatype) {
83
- visit (metatype->getInstanceType ());
84
- }
85
-
86
- void visitDynamicSelfType (DynamicSelfType *module ) {
87
- return ;
88
- }
89
-
90
- void visitArchetypeType (ArchetypeType *archetype) {
91
- llvm_unreachable (" Should not see archetypes in interface types" );
92
- }
93
-
94
- void visitGenericTypeParamType (GenericTypeParamType *param) {
95
- // Appears in protocols and in generic ObjC classes.
96
- return ;
97
- }
98
-
99
- void visitDependentMemberType (DependentMemberType *member) {
100
- // Appears in protocols and in generic ObjC classes.
101
- return ;
102
- }
103
-
104
- void visitAnyFunctionType (AnyFunctionType *fnTy) {
105
- for (auto ¶m : fnTy->getParams ())
106
- visit (param.getOldType ());
107
- visit (fnTy->getResult ());
108
- }
109
-
110
- void visitSyntaxSugarType (SyntaxSugarType *sugar) {
111
- visit (sugar->getSinglyDesugaredType ());
112
- }
113
-
114
- void visitProtocolCompositionType (ProtocolCompositionType *composition) {
115
- auto layout = composition->getExistentialLayout ();
116
- if (auto superclass = layout.explicitSuperclass )
117
- visit (superclass);
118
- for (auto proto : layout.getProtocols ())
119
- visit (proto);
120
- }
121
-
122
- void visitLValueType (LValueType *lvalue) {
123
- llvm_unreachable (" LValue types should not appear in interface types" );
124
- }
125
-
126
- void visitInOutType (InOutType *inout) {
127
- visit (inout->getObjectType ());
64
+ return Action::SkipChildren;
128
65
}
129
66
130
67
// / Returns true if \p paramTy has any constraints other than being
@@ -138,7 +75,7 @@ class ReferencedTypeFinder : public TypeVisitor<ReferencedTypeFinder> {
138
75
return !conformsTo.empty ();
139
76
}
140
77
141
- void visitBoundGenericType (BoundGenericType *boundGeneric) {
78
+ Action visitBoundGenericType (BoundGenericType *boundGeneric) override {
142
79
auto *decl = boundGeneric->getDecl ();
143
80
144
81
NeedsDefinition = true ;
@@ -151,22 +88,22 @@ class ReferencedTypeFinder : public TypeVisitor<ReferencedTypeFinder> {
151
88
for_each (boundGeneric->getGenericArgs (),
152
89
sig->getInnermostGenericParams (),
153
90
[&](Type argTy, GenericTypeParamType *paramTy) {
91
+ // FIXME: I think there's a bug here with recursive generic types.
154
92
if (isObjCGeneric && isConstrained (sig, paramTy))
155
93
NeedsDefinition = true ;
156
- visit ( argTy);
94
+ argTy. walk (* this );
157
95
NeedsDefinition = false ;
158
96
});
97
+ return Action::SkipChildren;
159
98
}
160
99
161
100
public:
162
- using TypeVisitor::visit;
163
-
164
101
bool needsDefinition () const {
165
102
return NeedsDefinition;
166
103
}
167
104
168
- static void walk (ModuleDecl &mod, Type ty, decltype (Callback) callback) {
169
- ReferencedTypeFinder (mod, callback). visit (ty );
105
+ static void walk (Type ty, decltype (Callback) callback) {
106
+ ty. walk ( ReferencedTypeFinder (callback));
170
107
}
171
108
};
172
109
@@ -330,7 +267,7 @@ class ModuleWriter {
330
267
}
331
268
332
269
bool needsToBeIndividuallyDelayed = false ;
333
- ReferencedTypeFinder::walk (M, VD->getInterfaceType (),
270
+ ReferencedTypeFinder::walk (VD->getInterfaceType (),
334
271
[&](ReferencedTypeFinder &finder,
335
272
const TypeDecl *TD) {
336
273
if (TD == container)
0 commit comments