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
use crate::ty;
use crate::ty::subst::{GenericArg, GenericArgKind};
use smallvec::{self, SmallVec};
type TypeWalkerStack<'tcx> = SmallVec<[GenericArg<'tcx>; 8]>;
pub struct TypeWalker<'tcx> {
stack: TypeWalkerStack<'tcx>,
last_subtree: usize,
}
impl<'tcx> TypeWalker<'tcx> {
pub fn new(root: GenericArg<'tcx>) -> TypeWalker<'tcx> {
TypeWalker { stack: smallvec![root], last_subtree: 1 }
}
pub fn skip_current_subtree(&mut self) {
self.stack.truncate(self.last_subtree);
}
}
impl<'tcx> Iterator for TypeWalker<'tcx> {
type Item = GenericArg<'tcx>;
fn next(&mut self) -> Option<GenericArg<'tcx>> {
debug!("next(): stack={:?}", self.stack);
let next = self.stack.pop()?;
self.last_subtree = self.stack.len();
push_inner(&mut self.stack, next);
debug!("next: stack={:?}", self.stack);
Some(next)
}
}
impl GenericArg<'tcx> {
pub fn walk(self) -> TypeWalker<'tcx> {
TypeWalker::new(self)
}
pub fn walk_shallow(self) -> impl Iterator<Item = GenericArg<'tcx>> {
let mut stack = SmallVec::new();
push_inner(&mut stack, self);
stack.into_iter()
}
}
impl<'tcx> super::TyS<'tcx> {
pub fn walk(&'tcx self) -> TypeWalker<'tcx> {
TypeWalker::new(self.into())
}
}
fn push_inner<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent: GenericArg<'tcx>) {
match parent.unpack() {
GenericArgKind::Type(parent_ty) => match parent_ty.kind {
ty::Bool
| ty::Char
| ty::Int(_)
| ty::Uint(_)
| ty::Float(_)
| ty::Str
| ty::Infer(_)
| ty::Param(_)
| ty::Never
| ty::Error(_)
| ty::Placeholder(..)
| ty::Bound(..)
| ty::Foreign(..) => {}
ty::Array(ty, len) => {
stack.push(len.into());
stack.push(ty.into());
}
ty::Slice(ty) => {
stack.push(ty.into());
}
ty::RawPtr(mt) => {
stack.push(mt.ty.into());
}
ty::Ref(lt, ty, _) => {
stack.push(ty.into());
stack.push(lt.into());
}
ty::Projection(data) => {
stack.extend(data.substs.iter().rev());
}
ty::Dynamic(obj, lt) => {
stack.push(lt.into());
stack.extend(obj.iter().rev().flat_map(|predicate| {
let (substs, opt_ty) = match predicate.skip_binder() {
ty::ExistentialPredicate::Trait(tr) => (tr.substs, None),
ty::ExistentialPredicate::Projection(p) => (p.substs, Some(p.ty)),
ty::ExistentialPredicate::AutoTrait(_) =>
{
(ty::InternalSubsts::empty(), None)
}
};
substs.iter().rev().chain(opt_ty.map(|ty| ty.into()))
}));
}
ty::Adt(_, substs)
| ty::Opaque(_, substs)
| ty::Closure(_, substs)
| ty::Generator(_, substs, _)
| ty::Tuple(substs)
| ty::FnDef(_, substs) => {
stack.extend(substs.iter().rev());
}
ty::GeneratorWitness(ts) => {
stack.extend(ts.skip_binder().iter().rev().map(|ty| ty.into()));
}
ty::FnPtr(sig) => {
stack.push(sig.skip_binder().output().into());
stack.extend(sig.skip_binder().inputs().iter().copied().rev().map(|ty| ty.into()));
}
},
GenericArgKind::Lifetime(_) => {}
GenericArgKind::Const(parent_ct) => {
stack.push(parent_ct.ty.into());
match parent_ct.val {
ty::ConstKind::Infer(_)
| ty::ConstKind::Param(_)
| ty::ConstKind::Placeholder(_)
| ty::ConstKind::Bound(..)
| ty::ConstKind::Value(_)
| ty::ConstKind::Error(_) => {}
ty::ConstKind::Unevaluated(_, substs, _) => {
stack.extend(substs.iter().rev());
}
}
}
}
}