2014-12-26 02:20:01 -06:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! An iterator over the type substructure.
|
2015-06-29 16:32:39 -05:00
|
|
|
//! WARNING: this does not keep track of the region depth.
|
2014-12-26 02:20:01 -06:00
|
|
|
|
2015-01-03 21:42:21 -06:00
|
|
|
use middle::ty::{self, Ty};
|
2014-12-26 02:20:01 -06:00
|
|
|
use std::iter::Iterator;
|
2015-03-30 16:46:34 -05:00
|
|
|
use std::vec::IntoIter;
|
2014-12-26 02:20:01 -06:00
|
|
|
|
|
|
|
pub struct TypeWalker<'tcx> {
|
|
|
|
stack: Vec<Ty<'tcx>>,
|
2015-03-25 19:06:52 -05:00
|
|
|
last_subtree: usize,
|
2014-12-26 02:20:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> TypeWalker<'tcx> {
|
|
|
|
pub fn new(ty: Ty<'tcx>) -> TypeWalker<'tcx> {
|
|
|
|
TypeWalker { stack: vec!(ty), last_subtree: 1, }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Skips the subtree of types corresponding to the last type
|
|
|
|
/// returned by `next()`.
|
|
|
|
///
|
2015-03-25 19:06:52 -05:00
|
|
|
/// Example: Imagine you are walking `Foo<Bar<int>, usize>`.
|
2014-12-26 02:20:01 -06:00
|
|
|
///
|
2015-03-12 21:42:38 -05:00
|
|
|
/// ```
|
2014-12-26 02:20:01 -06:00
|
|
|
/// let mut iter: TypeWalker = ...;
|
|
|
|
/// iter.next(); // yields Foo
|
|
|
|
/// iter.next(); // yields Bar<int>
|
|
|
|
/// iter.skip_current_subtree(); // skips int
|
2015-03-25 19:06:52 -05:00
|
|
|
/// iter.next(); // yields usize
|
2014-12-26 02:20:01 -06:00
|
|
|
/// ```
|
|
|
|
pub fn skip_current_subtree(&mut self) {
|
|
|
|
self.stack.truncate(self.last_subtree);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-01 22:26:38 -06:00
|
|
|
impl<'tcx> Iterator for TypeWalker<'tcx> {
|
|
|
|
type Item = Ty<'tcx>;
|
|
|
|
|
2014-12-26 02:20:01 -06:00
|
|
|
fn next(&mut self) -> Option<Ty<'tcx>> {
|
2014-12-20 02:09:35 -06:00
|
|
|
debug!("next(): stack={:?}", self.stack);
|
2014-12-26 02:20:01 -06:00
|
|
|
match self.stack.pop() {
|
|
|
|
None => {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(ty) => {
|
|
|
|
self.last_subtree = self.stack.len();
|
2015-03-30 16:46:34 -05:00
|
|
|
push_subtypes(&mut self.stack, ty);
|
2014-12-20 02:09:35 -06:00
|
|
|
debug!("next: stack={:?}", self.stack);
|
2014-12-26 02:20:01 -06:00
|
|
|
Some(ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-30 16:46:34 -05:00
|
|
|
|
|
|
|
pub fn walk_shallow<'tcx>(ty: Ty<'tcx>) -> IntoIter<Ty<'tcx>> {
|
|
|
|
let mut stack = vec![];
|
|
|
|
push_subtypes(&mut stack, ty);
|
|
|
|
stack.into_iter()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn push_subtypes<'tcx>(stack: &mut Vec<Ty<'tcx>>, parent_ty: Ty<'tcx>) {
|
|
|
|
match parent_ty.sty {
|
2015-06-11 18:21:46 -05:00
|
|
|
ty::TyBool | ty::TyChar | ty::TyInt(_) | ty::TyUint(_) | ty::TyFloat(_) |
|
|
|
|
ty::TyStr | ty::TyInfer(_) | ty::TyParam(_) | ty::TyError => {
|
2015-03-30 16:46:34 -05:00
|
|
|
}
|
2015-06-12 18:50:13 -05:00
|
|
|
ty::TyBox(ty) | ty::TyArray(ty, _) | ty::TySlice(ty) => {
|
2015-03-30 16:46:34 -05:00
|
|
|
stack.push(ty);
|
|
|
|
}
|
2015-06-11 18:21:46 -05:00
|
|
|
ty::TyRawPtr(ref mt) | ty::TyRef(_, ref mt) => {
|
2015-03-30 16:46:34 -05:00
|
|
|
stack.push(mt.ty);
|
|
|
|
}
|
2015-06-11 18:21:46 -05:00
|
|
|
ty::TyProjection(ref data) => {
|
2015-03-30 16:46:34 -05:00
|
|
|
push_reversed(stack, data.trait_ref.substs.types.as_slice());
|
|
|
|
}
|
2015-06-11 18:21:46 -05:00
|
|
|
ty::TyTrait(box ty::TraitTy { ref principal, ref bounds }) => {
|
2015-03-30 16:46:34 -05:00
|
|
|
push_reversed(stack, principal.substs().types.as_slice());
|
|
|
|
push_reversed(stack, &bounds.projection_bounds.iter().map(|pred| {
|
|
|
|
pred.0.ty
|
|
|
|
}).collect::<Vec<_>>());
|
|
|
|
}
|
2015-06-11 18:21:46 -05:00
|
|
|
ty::TyEnum(_, ref substs) |
|
|
|
|
ty::TyStruct(_, ref substs) |
|
|
|
|
ty::TyClosure(_, ref substs) => {
|
2015-03-30 16:46:34 -05:00
|
|
|
push_reversed(stack, substs.types.as_slice());
|
|
|
|
}
|
2015-06-11 18:21:46 -05:00
|
|
|
ty::TyTuple(ref ts) => {
|
2015-03-30 16:46:34 -05:00
|
|
|
push_reversed(stack, ts);
|
|
|
|
}
|
2015-06-11 18:21:46 -05:00
|
|
|
ty::TyBareFn(_, ref ft) => {
|
2015-03-30 16:46:34 -05:00
|
|
|
push_sig_subtypes(stack, &ft.sig);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn push_sig_subtypes<'tcx>(stack: &mut Vec<Ty<'tcx>>, sig: &ty::PolyFnSig<'tcx>) {
|
|
|
|
match sig.0.output {
|
|
|
|
ty::FnConverging(output) => { stack.push(output); }
|
|
|
|
ty::FnDiverging => { }
|
|
|
|
}
|
|
|
|
push_reversed(stack, &sig.0.inputs);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn push_reversed<'tcx>(stack: &mut Vec<Ty<'tcx>>, tys: &[Ty<'tcx>]) {
|
|
|
|
// We push slices on the stack in reverse order so as to
|
|
|
|
// 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).
|
|
|
|
for &ty in tys.iter().rev() {
|
|
|
|
stack.push(ty);
|
|
|
|
}
|
|
|
|
}
|