2013-10-29 04:25:18 -05:00
|
|
|
// Copyright 2012-2013 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.
|
|
|
|
|
|
|
|
// Generalized type folding mechanism.
|
|
|
|
|
|
|
|
use middle::ty;
|
|
|
|
use util::ppaux::Repr;
|
|
|
|
|
|
|
|
pub trait TypeFolder {
|
2014-03-05 21:07:47 -06:00
|
|
|
fn tcx<'a>(&'a self) -> &'a ty::ctxt;
|
2013-10-29 04:25:18 -05:00
|
|
|
|
|
|
|
fn fold_ty(&mut self, t: ty::t) -> ty::t {
|
|
|
|
super_fold_ty(self, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_mt(&mut self, t: &ty::mt) -> ty::mt {
|
|
|
|
super_fold_mt(self, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_trait_ref(&mut self, t: &ty::TraitRef) -> ty::TraitRef {
|
|
|
|
super_fold_trait_ref(self, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_sty(&mut self, sty: &ty::sty) -> ty::sty {
|
|
|
|
super_fold_sty(self, sty)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_substs(&mut self,
|
|
|
|
substs: &ty::substs)
|
|
|
|
-> ty::substs {
|
|
|
|
super_fold_substs(self, substs)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_sig(&mut self,
|
|
|
|
sig: &ty::FnSig)
|
|
|
|
-> ty::FnSig {
|
|
|
|
super_fold_sig(self, sig)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_bare_fn_ty(&mut self,
|
|
|
|
fty: &ty::BareFnTy)
|
|
|
|
-> ty::BareFnTy {
|
|
|
|
ty::BareFnTy { sig: self.fold_sig(&fty.sig),
|
2014-04-02 03:19:41 -05:00
|
|
|
abi: fty.abi,
|
2014-04-06 20:04:40 -05:00
|
|
|
fn_style: fty.fn_style }
|
2013-10-29 04:25:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_closure_ty(&mut self,
|
|
|
|
fty: &ty::ClosureTy)
|
|
|
|
-> ty::ClosureTy {
|
|
|
|
ty::ClosureTy {
|
2014-04-11 10:03:10 -05:00
|
|
|
store: self.fold_trait_store(fty.store),
|
2013-10-29 04:25:18 -05:00
|
|
|
sig: self.fold_sig(&fty.sig),
|
2014-04-06 20:04:40 -05:00
|
|
|
fn_style: fty.fn_style,
|
2013-10-29 04:25:18 -05:00
|
|
|
onceness: fty.onceness,
|
|
|
|
bounds: fty.bounds,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_region(&mut self, r: ty::Region) -> ty::Region {
|
|
|
|
r
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_trait_store(&mut self, s: ty::TraitStore) -> ty::TraitStore {
|
|
|
|
super_fold_trait_store(self, s)
|
|
|
|
}
|
2014-05-06 14:16:11 -05:00
|
|
|
|
|
|
|
fn fold_autoref(&mut self, ar: &ty::AutoRef) -> ty::AutoRef {
|
|
|
|
super_fold_autoref(self, ar)
|
|
|
|
}
|
2013-10-29 04:25:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fold_opt_ty<T:TypeFolder>(this: &mut T,
|
|
|
|
t: Option<ty::t>)
|
|
|
|
-> Option<ty::t> {
|
|
|
|
t.map(|t| this.fold_ty(t))
|
|
|
|
}
|
|
|
|
|
2014-03-08 14:36:22 -06:00
|
|
|
pub fn fold_ty_vec<T:TypeFolder>(this: &mut T, tys: &[ty::t]) -> Vec<ty::t> {
|
|
|
|
tys.iter().map(|t| this.fold_ty(*t)).collect()
|
2013-10-29 04:25:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn super_fold_ty<T:TypeFolder>(this: &mut T,
|
|
|
|
t: ty::t)
|
|
|
|
-> ty::t {
|
2014-03-05 21:07:47 -06:00
|
|
|
let sty = this.fold_sty(&ty::get(t).sty);
|
|
|
|
ty::mk_t(this.tcx(), sty)
|
2013-10-29 04:25:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn super_fold_substs<T:TypeFolder>(this: &mut T,
|
|
|
|
substs: &ty::substs)
|
|
|
|
-> ty::substs {
|
|
|
|
let regions = match substs.regions {
|
|
|
|
ty::ErasedRegions => {
|
|
|
|
ty::ErasedRegions
|
|
|
|
}
|
|
|
|
ty::NonerasedRegions(ref regions) => {
|
|
|
|
ty::NonerasedRegions(regions.map(|r| this.fold_region(*r)))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ty::substs { regions: regions,
|
|
|
|
self_ty: fold_opt_ty(this, substs.self_ty),
|
2014-03-08 14:36:22 -06:00
|
|
|
tps: fold_ty_vec(this, substs.tps.as_slice()), }
|
2013-10-29 04:25:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn super_fold_sig<T:TypeFolder>(this: &mut T,
|
|
|
|
sig: &ty::FnSig)
|
|
|
|
-> ty::FnSig {
|
|
|
|
ty::FnSig { binder_id: sig.binder_id,
|
2014-03-08 14:36:22 -06:00
|
|
|
inputs: fold_ty_vec(this, sig.inputs.as_slice()),
|
2013-10-29 04:25:18 -05:00
|
|
|
output: this.fold_ty(sig.output),
|
|
|
|
variadic: sig.variadic }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn super_fold_trait_ref<T:TypeFolder>(this: &mut T,
|
|
|
|
t: &ty::TraitRef)
|
|
|
|
-> ty::TraitRef {
|
|
|
|
ty::TraitRef {
|
|
|
|
def_id: t.def_id,
|
|
|
|
substs: this.fold_substs(&t.substs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn super_fold_mt<T:TypeFolder>(this: &mut T,
|
|
|
|
mt: &ty::mt) -> ty::mt {
|
|
|
|
ty::mt {ty: this.fold_ty(mt.ty),
|
|
|
|
mutbl: mt.mutbl}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn super_fold_sty<T:TypeFolder>(this: &mut T,
|
|
|
|
sty: &ty::sty) -> ty::sty {
|
|
|
|
match *sty {
|
2013-12-30 20:57:48 -06:00
|
|
|
ty::ty_box(typ) => {
|
|
|
|
ty::ty_box(this.fold_ty(typ))
|
2013-10-29 04:25:18 -05:00
|
|
|
}
|
2014-01-11 18:25:51 -06:00
|
|
|
ty::ty_uniq(typ) => {
|
|
|
|
ty::ty_uniq(this.fold_ty(typ))
|
2013-10-29 04:25:18 -05:00
|
|
|
}
|
|
|
|
ty::ty_ptr(ref tm) => {
|
|
|
|
ty::ty_ptr(this.fold_mt(tm))
|
|
|
|
}
|
2014-04-09 02:15:31 -05:00
|
|
|
ty::ty_vec(ref tm, sz) => {
|
|
|
|
ty::ty_vec(this.fold_mt(tm), sz)
|
2013-10-29 04:25:18 -05:00
|
|
|
}
|
|
|
|
ty::ty_enum(tid, ref substs) => {
|
|
|
|
ty::ty_enum(tid, this.fold_substs(substs))
|
|
|
|
}
|
2014-05-05 20:56:44 -05:00
|
|
|
ty::ty_trait(box ty::TyTrait {
|
|
|
|
def_id,
|
|
|
|
ref substs,
|
|
|
|
store,
|
|
|
|
bounds
|
|
|
|
}) => {
|
|
|
|
ty::ty_trait(box ty::TyTrait {
|
2014-03-19 06:01:30 -05:00
|
|
|
def_id: def_id,
|
|
|
|
substs: this.fold_substs(substs),
|
|
|
|
store: this.fold_trait_store(store),
|
|
|
|
bounds: bounds
|
|
|
|
})
|
2013-10-29 04:25:18 -05:00
|
|
|
}
|
|
|
|
ty::ty_tup(ref ts) => {
|
2014-03-08 14:36:22 -06:00
|
|
|
ty::ty_tup(fold_ty_vec(this, ts.as_slice()))
|
2013-10-29 04:25:18 -05:00
|
|
|
}
|
|
|
|
ty::ty_bare_fn(ref f) => {
|
|
|
|
ty::ty_bare_fn(this.fold_bare_fn_ty(f))
|
|
|
|
}
|
|
|
|
ty::ty_closure(ref f) => {
|
2014-04-25 03:08:02 -05:00
|
|
|
ty::ty_closure(box this.fold_closure_ty(*f))
|
2013-10-29 04:25:18 -05:00
|
|
|
}
|
|
|
|
ty::ty_rptr(r, ref tm) => {
|
|
|
|
ty::ty_rptr(this.fold_region(r),
|
|
|
|
ty::mt {ty: this.fold_ty(tm.ty),
|
|
|
|
mutbl: tm.mutbl})
|
|
|
|
}
|
|
|
|
ty::ty_struct(did, ref substs) => {
|
|
|
|
ty::ty_struct(did,
|
|
|
|
this.fold_substs(substs))
|
|
|
|
}
|
2014-04-28 20:10:23 -05:00
|
|
|
ty::ty_nil | ty::ty_bot | ty::ty_bool | ty::ty_char | ty::ty_str |
|
2014-02-11 05:48:55 -06:00
|
|
|
ty::ty_int(_) | ty::ty_uint(_) | ty::ty_float(_) |
|
2014-01-11 08:39:32 -06:00
|
|
|
ty::ty_err | ty::ty_infer(_) |
|
2013-11-28 14:22:53 -06:00
|
|
|
ty::ty_param(..) | ty::ty_self(_) => {
|
2013-10-29 04:25:18 -05:00
|
|
|
(*sty).clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn super_fold_trait_store<T:TypeFolder>(this: &mut T,
|
|
|
|
trait_store: ty::TraitStore)
|
|
|
|
-> ty::TraitStore {
|
|
|
|
match trait_store {
|
2014-04-11 01:01:31 -05:00
|
|
|
ty::UniqTraitStore => ty::UniqTraitStore,
|
|
|
|
ty::RegionTraitStore(r, m) => {
|
|
|
|
ty::RegionTraitStore(this.fold_region(r), m)
|
|
|
|
}
|
2013-10-29 04:25:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-06 14:16:11 -05:00
|
|
|
pub fn super_fold_autoref<T:TypeFolder>(this: &mut T,
|
|
|
|
autoref: &ty::AutoRef)
|
|
|
|
-> ty::AutoRef
|
|
|
|
{
|
|
|
|
match *autoref {
|
|
|
|
ty::AutoPtr(r, m) => ty::AutoPtr(this.fold_region(r), m),
|
|
|
|
ty::AutoBorrowVec(r, m) => ty::AutoBorrowVec(this.fold_region(r), m),
|
|
|
|
ty::AutoBorrowVecRef(r, m) => ty::AutoBorrowVecRef(this.fold_region(r), m),
|
|
|
|
ty::AutoUnsafe(m) => ty::AutoUnsafe(m),
|
|
|
|
ty::AutoBorrowObj(r, m) => ty::AutoBorrowObj(this.fold_region(r), m),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-29 04:25:18 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Some sample folders
|
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
pub struct BottomUpFolder<'a> {
|
2014-03-28 12:05:27 -05:00
|
|
|
pub tcx: &'a ty::ctxt,
|
2014-04-07 15:30:48 -05:00
|
|
|
pub fldop: |ty::t|: 'a -> ty::t,
|
2013-10-29 04:25:18 -05:00
|
|
|
}
|
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
impl<'a> TypeFolder for BottomUpFolder<'a> {
|
2014-03-05 21:07:47 -06:00
|
|
|
fn tcx<'a>(&'a self) -> &'a ty::ctxt { self.tcx }
|
2013-10-29 04:25:18 -05:00
|
|
|
|
|
|
|
fn fold_ty(&mut self, ty: ty::t) -> ty::t {
|
|
|
|
let t1 = super_fold_ty(self, ty);
|
|
|
|
(self.fldop)(t1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Region folder
|
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
pub struct RegionFolder<'a> {
|
2014-03-05 21:07:47 -06:00
|
|
|
tcx: &'a ty::ctxt,
|
2014-04-07 15:30:48 -05:00
|
|
|
fld_t: |ty::t|: 'a -> ty::t,
|
|
|
|
fld_r: |ty::Region|: 'a -> ty::Region,
|
2013-10-29 04:25:18 -05:00
|
|
|
}
|
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
impl<'a> RegionFolder<'a> {
|
2014-03-05 21:07:47 -06:00
|
|
|
pub fn general(tcx: &'a ty::ctxt,
|
2014-04-07 15:30:48 -05:00
|
|
|
fld_r: |ty::Region|: 'a -> ty::Region,
|
|
|
|
fld_t: |ty::t|: 'a -> ty::t)
|
2013-12-10 01:16:18 -06:00
|
|
|
-> RegionFolder<'a> {
|
2013-10-29 04:25:18 -05:00
|
|
|
RegionFolder {
|
|
|
|
tcx: tcx,
|
|
|
|
fld_t: fld_t,
|
|
|
|
fld_r: fld_r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-07 15:30:48 -05:00
|
|
|
pub fn regions(tcx: &'a ty::ctxt, fld_r: |ty::Region|: 'a -> ty::Region)
|
2013-12-10 01:16:18 -06:00
|
|
|
-> RegionFolder<'a> {
|
2013-10-29 04:25:18 -05:00
|
|
|
fn noop(t: ty::t) -> ty::t { t }
|
|
|
|
|
|
|
|
RegionFolder {
|
|
|
|
tcx: tcx,
|
|
|
|
fld_t: noop,
|
|
|
|
fld_r: fld_r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
impl<'a> TypeFolder for RegionFolder<'a> {
|
2014-03-05 21:07:47 -06:00
|
|
|
fn tcx<'a>(&'a self) -> &'a ty::ctxt { self.tcx }
|
2013-10-29 04:25:18 -05:00
|
|
|
|
|
|
|
fn fold_ty(&mut self, ty: ty::t) -> ty::t {
|
|
|
|
debug!("RegionFolder.fold_ty({})", ty.repr(self.tcx()));
|
|
|
|
let t1 = super_fold_ty(self, ty);
|
|
|
|
(self.fld_t)(t1)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_region(&mut self, r: ty::Region) -> ty::Region {
|
|
|
|
debug!("RegionFolder.fold_region({})", r.repr(self.tcx()));
|
|
|
|
(self.fld_r)(r)
|
|
|
|
}
|
|
|
|
}
|