]>
Commit | Line | Data |
---|---|---|
1a4d82fc | 1 | //! An iterator over the type substructure. |
c1a9b12d | 2 | //! WARNING: this does not keep track of the region depth. |
1a4d82fc | 3 | |
9fa01778 | 4 | use crate::ty::{self, Ty}; |
b7449926 | 5 | use smallvec::{self, SmallVec}; |
476ff2be SL |
6 | |
7 | // The TypeWalker's stack is hot enough that it's worth going to some effort to | |
8 | // avoid heap allocations. | |
9 | pub type TypeWalkerArray<'tcx> = [Ty<'tcx>; 8]; | |
10 | pub type TypeWalkerStack<'tcx> = SmallVec<TypeWalkerArray<'tcx>>; | |
1a4d82fc JJ |
11 | |
12 | pub struct TypeWalker<'tcx> { | |
476ff2be | 13 | stack: TypeWalkerStack<'tcx>, |
c34b1796 | 14 | last_subtree: usize, |
1a4d82fc JJ |
15 | } |
16 | ||
17 | impl<'tcx> TypeWalker<'tcx> { | |
18 | pub fn new(ty: Ty<'tcx>) -> TypeWalker<'tcx> { | |
dfeec247 | 19 | TypeWalker { stack: smallvec![ty], last_subtree: 1 } |
1a4d82fc JJ |
20 | } |
21 | ||
1a4d82fc JJ |
22 | /// Skips the subtree of types corresponding to the last type |
23 | /// returned by `next()`. | |
24 | /// | |
c34b1796 | 25 | /// Example: Imagine you are walking `Foo<Bar<int>, usize>`. |
1a4d82fc | 26 | /// |
c34b1796 | 27 | /// ``` |
1a4d82fc JJ |
28 | /// let mut iter: TypeWalker = ...; |
29 | /// iter.next(); // yields Foo | |
30 | /// iter.next(); // yields Bar<int> | |
31 | /// iter.skip_current_subtree(); // skips int | |
c34b1796 | 32 | /// iter.next(); // yields usize |
1a4d82fc JJ |
33 | /// ``` |
34 | pub fn skip_current_subtree(&mut self) { | |
35 | self.stack.truncate(self.last_subtree); | |
36 | } | |
37 | } | |
38 | ||
39 | impl<'tcx> Iterator for TypeWalker<'tcx> { | |
40 | type Item = Ty<'tcx>; | |
41 | ||
42 | fn next(&mut self) -> Option<Ty<'tcx>> { | |
43 | debug!("next(): stack={:?}", self.stack); | |
44 | match self.stack.pop() { | |
dfeec247 | 45 | None => None, |
1a4d82fc JJ |
46 | Some(ty) => { |
47 | self.last_subtree = self.stack.len(); | |
c34b1796 | 48 | push_subtypes(&mut self.stack, ty); |
1a4d82fc JJ |
49 | debug!("next: stack={:?}", self.stack); |
50 | Some(ty) | |
51 | } | |
52 | } | |
53 | } | |
54 | } | |
c34b1796 | 55 | |
dc9dc135 | 56 | pub fn walk_shallow(ty: Ty<'_>) -> smallvec::IntoIter<TypeWalkerArray<'_>> { |
476ff2be | 57 | let mut stack = SmallVec::new(); |
c34b1796 AL |
58 | push_subtypes(&mut stack, ty); |
59 | stack.into_iter() | |
60 | } | |
61 | ||
9e0c209e SL |
62 | // We push types on the stack in reverse order so as to |
63 | // maintain a pre-order traversal. As of the time of this | |
64 | // writing, the fact that the traversal is pre-order is not | |
65 | // known to be significant to any code, but it seems like the | |
66 | // natural order one would expect (basically, the order of the | |
67 | // types as they are written). | |
476ff2be | 68 | fn push_subtypes<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent_ty: Ty<'tcx>) { |
e74abb32 | 69 | match parent_ty.kind { |
dfeec247 XL |
70 | ty::Bool |
71 | | ty::Char | |
72 | | ty::Int(_) | |
73 | | ty::Uint(_) | |
74 | | ty::Float(_) | |
75 | | ty::Str | |
76 | | ty::Infer(_) | |
77 | | ty::Param(_) | |
78 | | ty::Never | |
79 | | ty::Error | |
80 | | ty::Placeholder(..) | |
81 | | ty::Bound(..) | |
82 | | ty::Foreign(..) => {} | |
b7449926 | 83 | ty::Array(ty, len) => { |
dfeec247 XL |
84 | if let ty::ConstKind::Unevaluated(_, substs, promoted) = len.val { |
85 | assert!(promoted.is_none()); | |
0731742a XL |
86 | stack.extend(substs.types().rev()); |
87 | } | |
532ac7d7 | 88 | stack.push(len.ty); |
ea8adc8c XL |
89 | stack.push(ty); |
90 | } | |
b7449926 | 91 | ty::Slice(ty) => { |
c34b1796 AL |
92 | stack.push(ty); |
93 | } | |
b7449926 | 94 | ty::RawPtr(ref mt) => { |
c34b1796 AL |
95 | stack.push(mt.ty); |
96 | } | |
b7449926 | 97 | ty::Ref(_, ty, _) => { |
94b46f34 XL |
98 | stack.push(ty); |
99 | } | |
0bf4aa26 | 100 | ty::Projection(ref data) | ty::UnnormalizedProjection(ref data) => { |
041b39d2 | 101 | stack.extend(data.substs.types().rev()); |
c34b1796 | 102 | } |
b7449926 | 103 | ty::Dynamic(ref obj, ..) => { |
476ff2be SL |
104 | stack.extend(obj.iter().rev().flat_map(|predicate| { |
105 | let (substs, opt_ty) = match *predicate.skip_binder() { | |
106 | ty::ExistentialPredicate::Trait(tr) => (tr.substs, None), | |
dfeec247 | 107 | ty::ExistentialPredicate::Projection(p) => (p.substs, Some(p.ty)), |
476ff2be | 108 | ty::ExistentialPredicate::AutoTrait(_) => |
dfeec247 XL |
109 | // Empty iterator |
110 | { | |
111 | (ty::InternalSubsts::empty(), None) | |
112 | } | |
476ff2be SL |
113 | }; |
114 | ||
115 | substs.types().rev().chain(opt_ty) | |
116 | })); | |
c34b1796 | 117 | } |
b7449926 | 118 | ty::Adt(_, substs) | ty::Opaque(_, substs) => { |
9e0c209e | 119 | stack.extend(substs.types().rev()); |
c34b1796 | 120 | } |
dfeec247 | 121 | ty::Closure(_, ref substs) | ty::Generator(_, ref substs, _) => { |
e74abb32 | 122 | stack.extend(substs.types().rev()); |
2c00a5a8 | 123 | } |
b7449926 | 124 | ty::GeneratorWitness(ts) => { |
2c00a5a8 | 125 | stack.extend(ts.skip_binder().iter().cloned().rev()); |
ea8adc8c | 126 | } |
416331ca XL |
127 | ty::Tuple(..) => { |
128 | stack.extend(parent_ty.tuple_fields().rev()); | |
c34b1796 | 129 | } |
b7449926 | 130 | ty::FnDef(_, substs) => { |
9e0c209e | 131 | stack.extend(substs.types().rev()); |
54a0048b | 132 | } |
b7449926 | 133 | ty::FnPtr(sig) => { |
ea8adc8c XL |
134 | stack.push(sig.skip_binder().output()); |
135 | stack.extend(sig.skip_binder().inputs().iter().cloned().rev()); | |
c34b1796 AL |
136 | } |
137 | } | |
138 | } |