2014-12-26 03:20:01 -05:00
|
|
|
//! An iterator over the type substructure.
|
2015-06-30 00:32:39 +03:00
|
|
|
//! WARNING: this does not keep track of the region depth.
|
2014-12-26 03:20:01 -05:00
|
|
|
|
2020-03-23 03:57:04 +02:00
|
|
|
use crate::ty;
|
2020-03-19 21:15:59 +02:00
|
|
|
use crate::ty::subst::{GenericArg, GenericArgKind};
|
2018-08-13 22:15:16 +03:00
|
|
|
use smallvec::{self, SmallVec};
|
2016-10-18 08:23:09 +11:00
|
|
|
|
|
|
|
|
// The TypeWalker's stack is hot enough that it's worth going to some effort to
|
|
|
|
|
// avoid heap allocations.
|
2020-03-19 21:15:59 +02:00
|
|
|
type TypeWalkerStack<'tcx> = SmallVec<[GenericArg<'tcx>; 8]>;
|
2014-12-26 03:20:01 -05:00
|
|
|
|
|
|
|
|
pub struct TypeWalker<'tcx> {
|
2016-10-18 08:23:09 +11:00
|
|
|
stack: TypeWalkerStack<'tcx>,
|
2015-03-25 17:06:52 -07:00
|
|
|
last_subtree: usize,
|
2014-12-26 03:20:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'tcx> TypeWalker<'tcx> {
|
2020-03-19 21:15:59 +02:00
|
|
|
pub fn new(root: GenericArg<'tcx>) -> TypeWalker<'tcx> {
|
|
|
|
|
TypeWalker { stack: smallvec![root], last_subtree: 1 }
|
2014-12-26 03:20:01 -05:00
|
|
|
}
|
|
|
|
|
|
2020-03-19 21:15:59 +02:00
|
|
|
/// Skips the subtree corresponding to the last type
|
2014-12-26 03:20:01 -05:00
|
|
|
/// returned by `next()`.
|
|
|
|
|
///
|
2020-06-06 12:05:37 +02:00
|
|
|
/// Example: Imagine you are walking `Foo<Bar<i32>, usize>`.
|
2014-12-26 03:20:01 -05:00
|
|
|
///
|
2015-03-12 22:42:38 -04:00
|
|
|
/// ```
|
2014-12-26 03:20:01 -05:00
|
|
|
/// let mut iter: TypeWalker = ...;
|
|
|
|
|
/// iter.next(); // yields Foo
|
2020-06-06 12:05:37 +02:00
|
|
|
/// iter.next(); // yields Bar<i32>
|
|
|
|
|
/// iter.skip_current_subtree(); // skips i32
|
2015-03-25 17:06:52 -07:00
|
|
|
/// iter.next(); // yields usize
|
2014-12-26 03:20:01 -05:00
|
|
|
/// ```
|
|
|
|
|
pub fn skip_current_subtree(&mut self) {
|
|
|
|
|
self.stack.truncate(self.last_subtree);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-01 23:26:38 -05:00
|
|
|
impl<'tcx> Iterator for TypeWalker<'tcx> {
|
2020-03-23 03:57:04 +02:00
|
|
|
type Item = GenericArg<'tcx>;
|
2015-01-01 23:26:38 -05:00
|
|
|
|
2020-03-23 03:57:04 +02:00
|
|
|
fn next(&mut self) -> Option<GenericArg<'tcx>> {
|
2014-12-20 00:09:35 -08:00
|
|
|
debug!("next(): stack={:?}", self.stack);
|
2020-03-23 03:57:04 +02:00
|
|
|
let next = self.stack.pop()?;
|
|
|
|
|
self.last_subtree = self.stack.len();
|
|
|
|
|
push_inner(&mut self.stack, next);
|
|
|
|
|
debug!("next: stack={:?}", self.stack);
|
|
|
|
|
Some(next)
|
2014-12-26 03:20:01 -05:00
|
|
|
}
|
|
|
|
|
}
|
2015-03-30 17:46:34 -04:00
|
|
|
|
2020-03-20 07:42:14 +02:00
|
|
|
impl GenericArg<'tcx> {
|
2020-03-23 03:57:04 +02:00
|
|
|
/// Iterator that walks `self` and any types reachable from
|
|
|
|
|
/// `self`, in depth-first order. Note that just walks the types
|
|
|
|
|
/// that appear in `self`, it does not descend into the fields of
|
|
|
|
|
/// structs or variants. For example:
|
|
|
|
|
///
|
|
|
|
|
/// ```notrust
|
|
|
|
|
/// isize => { isize }
|
|
|
|
|
/// Foo<Bar<isize>> => { Foo<Bar<isize>>, Bar<isize>, isize }
|
|
|
|
|
/// [isize] => { [isize], isize }
|
|
|
|
|
/// ```
|
|
|
|
|
pub fn walk(self) -> TypeWalker<'tcx> {
|
|
|
|
|
TypeWalker::new(self)
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 07:42:14 +02:00
|
|
|
/// Iterator that walks the immediate children of `self`. Hence
|
|
|
|
|
/// `Foo<Bar<i32>, u32>` yields the sequence `[Bar<i32>, u32]`
|
|
|
|
|
/// (but not `i32`, like `walk`).
|
|
|
|
|
pub fn walk_shallow(self) -> impl Iterator<Item = GenericArg<'tcx>> {
|
|
|
|
|
let mut stack = SmallVec::new();
|
|
|
|
|
push_inner(&mut stack, self);
|
|
|
|
|
stack.into_iter()
|
|
|
|
|
}
|
2015-03-30 17:46:34 -04:00
|
|
|
}
|
|
|
|
|
|
2020-03-23 03:57:04 +02:00
|
|
|
impl<'tcx> super::TyS<'tcx> {
|
|
|
|
|
/// Iterator that walks `self` and any types reachable from
|
|
|
|
|
/// `self`, in depth-first order. Note that just walks the types
|
|
|
|
|
/// that appear in `self`, it does not descend into the fields of
|
|
|
|
|
/// structs or variants. For example:
|
|
|
|
|
///
|
|
|
|
|
/// ```notrust
|
|
|
|
|
/// isize => { isize }
|
|
|
|
|
/// Foo<Bar<isize>> => { Foo<Bar<isize>>, Bar<isize>, isize }
|
|
|
|
|
/// [isize] => { [isize], isize }
|
|
|
|
|
/// ```
|
|
|
|
|
pub fn walk(&'tcx self) -> TypeWalker<'tcx> {
|
|
|
|
|
TypeWalker::new(self.into())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-19 21:15:59 +02:00
|
|
|
// We push `GenericArg`s on the stack in reverse order so as to
|
2016-08-27 01:13:48 +03:00
|
|
|
// maintain a pre-order traversal. As of the time of this
|
|
|
|
|
// writing, the fact that the traversal is pre-order is not
|
|
|
|
|
// known to be significant to any code, but it seems like the
|
|
|
|
|
// natural order one would expect (basically, the order of the
|
|
|
|
|
// types as they are written).
|
2020-03-19 21:15:59 +02:00
|
|
|
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
|
2020-05-05 23:02:09 -05:00
|
|
|
| ty::Error(_)
|
2020-03-19 21:15:59 +02:00
|
|
|
| ty::Placeholder(..)
|
|
|
|
|
| ty::Bound(..)
|
|
|
|
|
| ty::Foreign(..) => {}
|
|
|
|
|
|
|
|
|
|
ty::Array(ty, len) => {
|
|
|
|
|
stack.push(len.into());
|
|
|
|
|
stack.push(ty.into());
|
2018-12-11 19:56:59 +01:00
|
|
|
}
|
2020-03-19 21:15:59 +02:00
|
|
|
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());
|
|
|
|
|
}
|
2020-05-12 01:56:29 -04:00
|
|
|
ty::Projection(data) => {
|
2020-05-23 11:49:24 +02:00
|
|
|
stack.extend(data.substs.iter().rev());
|
2020-03-19 21:15:59 +02:00
|
|
|
}
|
|
|
|
|
ty::Dynamic(obj, lt) => {
|
|
|
|
|
stack.push(lt.into());
|
|
|
|
|
stack.extend(obj.iter().rev().flat_map(|predicate| {
|
2020-06-24 23:40:33 +02:00
|
|
|
let (substs, opt_ty) = match predicate.skip_binder() {
|
2020-03-19 21:15:59 +02:00
|
|
|
ty::ExistentialPredicate::Trait(tr) => (tr.substs, None),
|
|
|
|
|
ty::ExistentialPredicate::Projection(p) => (p.substs, Some(p.ty)),
|
|
|
|
|
ty::ExistentialPredicate::AutoTrait(_) =>
|
|
|
|
|
// Empty iterator
|
|
|
|
|
{
|
|
|
|
|
(ty::InternalSubsts::empty(), None)
|
|
|
|
|
}
|
|
|
|
|
};
|
2016-11-16 09:21:49 -07:00
|
|
|
|
2020-05-23 11:49:24 +02:00
|
|
|
substs.iter().rev().chain(opt_ty.map(|ty| ty.into()))
|
2020-03-19 21:15:59 +02:00
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
ty::Adt(_, substs)
|
|
|
|
|
| ty::Opaque(_, substs)
|
|
|
|
|
| ty::Closure(_, substs)
|
|
|
|
|
| ty::Generator(_, substs, _)
|
|
|
|
|
| ty::Tuple(substs)
|
|
|
|
|
| ty::FnDef(_, substs) => {
|
2020-05-23 11:49:24 +02:00
|
|
|
stack.extend(substs.iter().rev());
|
2020-03-19 21:15:59 +02:00
|
|
|
}
|
|
|
|
|
ty::GeneratorWitness(ts) => {
|
2020-05-23 11:49:24 +02:00
|
|
|
stack.extend(ts.skip_binder().iter().rev().map(|ty| ty.into()));
|
2020-03-19 21:15:59 +02:00
|
|
|
}
|
|
|
|
|
ty::FnPtr(sig) => {
|
|
|
|
|
stack.push(sig.skip_binder().output().into());
|
2020-05-23 11:49:24 +02:00
|
|
|
stack.extend(sig.skip_binder().inputs().iter().copied().rev().map(|ty| ty.into()));
|
2020-03-19 21:15:59 +02:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
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(..)
|
2020-04-16 13:47:47 +03:00
|
|
|
| ty::ConstKind::Value(_)
|
2020-05-05 23:02:09 -05:00
|
|
|
| ty::ConstKind::Error(_) => {}
|
2020-03-19 21:15:59 +02:00
|
|
|
|
|
|
|
|
ty::ConstKind::Unevaluated(_, substs, _) => {
|
2020-05-23 11:49:24 +02:00
|
|
|
stack.extend(substs.iter().rev());
|
2020-03-19 21:15:59 +02:00
|
|
|
}
|
|
|
|
|
}
|
2015-03-30 17:46:34 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|