forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypeWalker.cpp
212 lines (171 loc) · 5.45 KB
/
TypeWalker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//===--- TypeWalker.cpp - Type Traversal ----------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements Type::walk.
//
//===----------------------------------------------------------------------===//
#include "swift/AST/TypeWalker.h"
#include "swift/AST/TypeVisitor.h"
using namespace swift;
void TypeWalker::anchor() {}
namespace {
/// This class implements a simple type recursive traverser which queries a
/// user-provided walker class on every node in a type.
class Traversal : public TypeVisitor<Traversal, bool>
{
using Base = TypeVisitor;
friend Base;
TypeWalker &Walker;
bool visitErrorType(ErrorType *ty) { return false; }
bool visitUnresolvedType(UnresolvedType *ty) { return false; }
bool visitBuiltinType(BuiltinType *ty) { return false; }
bool visitNameAliasType(NameAliasType *ty) { return false; }
bool visitParenType(ParenType *ty) {
return doIt(ty->getUnderlyingType());
}
bool visitTupleType(TupleType *ty) {
for (auto elementTy : ty->getElementTypes())
if (doIt(elementTy))
return true;
return false;
}
bool visitReferenceStorageType(ReferenceStorageType *ty) {
return doIt(ty->getReferentType());
}
bool visitNominalType(NominalType *ty) {
if (auto parent = ty->getParent())
return doIt(parent);
return false;
}
bool visitAnyMetatypeType(AnyMetatypeType *ty) {
return doIt(ty->getInstanceType());
}
bool visitModuleType(ModuleType *ty) { return false; }
bool visitDynamicSelfType(DynamicSelfType *ty) {
return doIt(ty->getSelfType());
}
bool visitSubstitutableType(SubstitutableType *ty) { return false; }
bool visitSubstitutedType(SubstitutedType *ty) {
if (Walker.shouldVisitOriginalSubstitutedType())
if (doIt(ty->getOriginal()))
return true;
return doIt(ty->getReplacementType());
}
bool visitDependentMemberType(DependentMemberType *ty) {
return doIt(ty->getBase());
}
bool visitAnyFunctionType(AnyFunctionType *ty) {
if (doIt(ty->getInput()))
return true;
return doIt(ty->getResult());
}
bool visitGenericFunctionType(GenericFunctionType *ty) {
for (auto param : ty->getGenericParams())
if (doIt(param))
return true;
for (const auto &req : ty->getRequirements()) {
if (doIt(req.getFirstType()))
return true;
switch (req.getKind()) {
case RequirementKind::SameType:
case RequirementKind::Conformance:
if (doIt(req.getSecondType()))
return true;
break;
case RequirementKind::WitnessMarker:
break;
}
}
return visitAnyFunctionType(ty);
}
bool visitSILFunctionType(SILFunctionType *ty) {
for (auto param : ty->getParameters())
if (doIt(param.getType()))
return true;
return doIt(ty->getResult().getType());
}
bool visitSyntaxSugarType(SyntaxSugarType *ty) {
return doIt(ty->getBaseType());
}
bool visitDictionaryType(DictionaryType *ty) {
return doIt(ty->getKeyType()) || doIt(ty->getValueType());
}
bool visitProtocolCompositionType(ProtocolCompositionType *ty) {
for (auto proto : ty->getProtocols())
if (doIt(proto))
return true;
return false;
}
bool visitLValueType(LValueType *ty) {
return doIt(ty->getObjectType());
}
bool visitInOutType(InOutType *ty) {
return doIt(ty->getObjectType());
}
bool visitUnboundGenericType(UnboundGenericType *ty) {
if (auto parent = ty->getParent())
return doIt(parent);
return false;
}
bool visitBoundGenericType(BoundGenericType *ty) {
if (auto parent = ty->getParent())
if (doIt(parent))
return true;
for (auto arg : ty->getGenericArgs())
if (doIt(arg))
return true;
return false;
}
bool visitTypeVariableType(TypeVariableType *ty) { return false; }
bool visitSILBlockStorageType(SILBlockStorageType *ty) {
if (doIt(ty->getCaptureType()))
return true;
return false;
}
bool visitSILBoxType(SILBoxType *ty) {
if (doIt(ty->getBoxedType()))
return true;
return false;
}
public:
explicit Traversal(TypeWalker &walker) : Walker(walker) {}
/// Returns true on failure.
bool doIt(Type ty) {
// Do the pre-order visitation. If it returns false, we just
// skip entering subnodes of this tree.
switch (Walker.walkToTypePre(ty)) {
case TypeWalker::Action::Continue:
break;
case TypeWalker::Action::SkipChildren:
return false;
case TypeWalker::Action::Stop:
return true;
}
// Otherwise, visit the children.
if (visit(ty))
return true;
// If we didn't bail out, do post-order visitation.
switch (Walker.walkToTypePost(ty)) {
case TypeWalker::Action::Continue:
return false;
case TypeWalker::Action::SkipChildren:
llvm_unreachable("SkipChildren is not valid for a post-visit check");
case TypeWalker::Action::Stop:
return true;
}
llvm_unreachable("bad TypeWalker::Action");
}
};
} // end anonymous namespace.
bool Type::walk(TypeWalker &walker) const {
return Traversal(walker).doIt(*this);
}