2015-09-06 21:51:58 +03:00
|
|
|
// Copyright 2012-2015 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.
|
|
|
|
|
2016-08-15 01:07:09 +03:00
|
|
|
use ty::subst::Substs;
|
2016-03-22 17:30:57 +02:00
|
|
|
use ty::{self, Ty, TypeFlags, TypeFoldable};
|
2015-09-06 21:51:58 +03:00
|
|
|
|
|
|
|
pub struct FlagComputation {
|
|
|
|
pub flags: TypeFlags,
|
|
|
|
|
|
|
|
// maximum depth of any bound region that we have seen thus far
|
|
|
|
pub depth: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FlagComputation {
|
|
|
|
fn new() -> FlagComputation {
|
|
|
|
FlagComputation { flags: TypeFlags::empty(), depth: 0 }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn for_sty(st: &ty::TypeVariants) -> FlagComputation {
|
|
|
|
let mut result = FlagComputation::new();
|
|
|
|
result.add_sty(st);
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_flags(&mut self, flags: TypeFlags) {
|
|
|
|
self.flags = self.flags | (flags & TypeFlags::NOMINAL_FLAGS);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_depth(&mut self, depth: u32) {
|
|
|
|
if depth > self.depth {
|
|
|
|
self.depth = depth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds the flags/depth from a set of types that appear within the current type, but within a
|
|
|
|
/// region binder.
|
|
|
|
fn add_bound_computation(&mut self, computation: &FlagComputation) {
|
|
|
|
self.add_flags(computation.flags);
|
|
|
|
|
|
|
|
// The types that contributed to `computation` occurred within
|
|
|
|
// a region binder, so subtract one from the region depth
|
|
|
|
// within when adding the depth to `self`.
|
|
|
|
let depth = computation.depth;
|
|
|
|
if depth > 0 {
|
|
|
|
self.add_depth(depth - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_sty(&mut self, st: &ty::TypeVariants) {
|
|
|
|
match st {
|
|
|
|
&ty::TyBool |
|
|
|
|
&ty::TyChar |
|
|
|
|
&ty::TyInt(_) |
|
|
|
|
&ty::TyFloat(_) |
|
|
|
|
&ty::TyUint(_) |
|
2016-08-02 15:56:20 +08:00
|
|
|
&ty::TyNever |
|
2015-09-06 21:51:58 +03:00
|
|
|
&ty::TyStr => {
|
|
|
|
}
|
|
|
|
|
|
|
|
// You might think that we could just return TyError for
|
|
|
|
// any type containing TyError as a component, and get
|
|
|
|
// rid of the TypeFlags::HAS_TY_ERR flag -- likewise for ty_bot (with
|
|
|
|
// the exception of function types that return bot).
|
|
|
|
// But doing so caused sporadic memory corruption, and
|
|
|
|
// neither I (tjc) nor nmatsakis could figure out why,
|
|
|
|
// so we're doing it this way.
|
|
|
|
&ty::TyError => {
|
|
|
|
self.add_flags(TypeFlags::HAS_TY_ERR)
|
|
|
|
}
|
|
|
|
|
|
|
|
&ty::TyParam(ref p) => {
|
|
|
|
self.add_flags(TypeFlags::HAS_LOCAL_NAMES);
|
2016-08-15 01:07:09 +03:00
|
|
|
if p.is_self() {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.add_flags(TypeFlags::HAS_SELF);
|
|
|
|
} else {
|
|
|
|
self.add_flags(TypeFlags::HAS_PARAMS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
&ty::TyClosure(_, ref substs) => {
|
|
|
|
self.add_flags(TypeFlags::HAS_TY_CLOSURE);
|
|
|
|
self.add_flags(TypeFlags::HAS_LOCAL_NAMES);
|
|
|
|
self.add_substs(&substs.func_substs);
|
|
|
|
self.add_tys(&substs.upvar_tys);
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
&ty::TyInfer(infer) => {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.add_flags(TypeFlags::HAS_LOCAL_NAMES); // it might, right?
|
2016-05-11 04:14:41 +03:00
|
|
|
self.add_flags(TypeFlags::HAS_TY_INFER);
|
|
|
|
match infer {
|
|
|
|
ty::FreshTy(_) |
|
|
|
|
ty::FreshIntTy(_) |
|
|
|
|
ty::FreshFloatTy(_) => {}
|
|
|
|
_ => self.add_flags(TypeFlags::KEEP_IN_LOCAL_TCX)
|
|
|
|
}
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
&ty::TyEnum(_, substs) | &ty::TyStruct(_, substs) => {
|
|
|
|
self.add_substs(substs);
|
|
|
|
}
|
|
|
|
|
|
|
|
&ty::TyProjection(ref data) => {
|
|
|
|
self.add_flags(TypeFlags::HAS_PROJECTION);
|
|
|
|
self.add_projection_ty(data);
|
|
|
|
}
|
|
|
|
|
2016-07-22 18:56:22 +03:00
|
|
|
&ty::TyAnon(_, substs) => {
|
|
|
|
self.add_flags(TypeFlags::HAS_PROJECTION);
|
|
|
|
self.add_substs(substs);
|
|
|
|
}
|
|
|
|
|
2016-08-04 15:52:57 +03:00
|
|
|
&ty::TyTrait(ref obj) => {
|
2015-09-06 21:51:58 +03:00
|
|
|
let mut computation = FlagComputation::new();
|
2016-08-04 15:52:57 +03:00
|
|
|
computation.add_substs(obj.principal.skip_binder().substs);
|
|
|
|
for projection_bound in &obj.projection_bounds {
|
2015-09-06 21:51:58 +03:00
|
|
|
let mut proj_computation = FlagComputation::new();
|
2016-08-04 15:52:57 +03:00
|
|
|
proj_computation.add_existential_projection(&projection_bound.0);
|
2015-09-06 21:51:58 +03:00
|
|
|
self.add_bound_computation(&proj_computation);
|
|
|
|
}
|
|
|
|
self.add_bound_computation(&computation);
|
2016-08-04 15:52:57 +03:00
|
|
|
self.add_region(obj.region_bound);
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
&ty::TyBox(tt) | &ty::TyArray(tt, _) | &ty::TySlice(tt) => {
|
|
|
|
self.add_ty(tt)
|
|
|
|
}
|
|
|
|
|
|
|
|
&ty::TyRawPtr(ref m) => {
|
|
|
|
self.add_ty(m.ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
&ty::TyRef(r, ref m) => {
|
|
|
|
self.add_region(*r);
|
|
|
|
self.add_ty(m.ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
&ty::TyTuple(ref ts) => {
|
|
|
|
self.add_tys(&ts[..]);
|
|
|
|
}
|
|
|
|
|
2016-02-16 18:36:41 +02:00
|
|
|
&ty::TyFnDef(_, substs, ref f) => {
|
|
|
|
self.add_substs(substs);
|
|
|
|
self.add_fn_sig(&f.sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
&ty::TyFnPtr(ref f) => {
|
2015-09-06 21:51:58 +03:00
|
|
|
self.add_fn_sig(&f.sig);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_ty(&mut self, ty: Ty) {
|
|
|
|
self.add_flags(ty.flags.get());
|
|
|
|
self.add_depth(ty.region_depth);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_tys(&mut self, tys: &[Ty]) {
|
|
|
|
for &ty in tys {
|
|
|
|
self.add_ty(ty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_fn_sig(&mut self, fn_sig: &ty::PolyFnSig) {
|
|
|
|
let mut computation = FlagComputation::new();
|
|
|
|
|
|
|
|
computation.add_tys(&fn_sig.0.inputs);
|
2016-07-31 22:33:41 +08:00
|
|
|
computation.add_ty(fn_sig.0.output);
|
2015-09-06 21:51:58 +03:00
|
|
|
|
|
|
|
self.add_bound_computation(&computation);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_region(&mut self, r: ty::Region) {
|
|
|
|
match r {
|
2016-03-15 08:23:23 -04:00
|
|
|
ty::ReVar(..) => {
|
|
|
|
self.add_flags(TypeFlags::HAS_RE_INFER);
|
|
|
|
self.add_flags(TypeFlags::KEEP_IN_LOCAL_TCX);
|
|
|
|
}
|
2016-05-11 04:14:41 +03:00
|
|
|
ty::ReSkolemized(..) => {
|
|
|
|
self.add_flags(TypeFlags::HAS_RE_INFER);
|
2016-03-15 08:23:23 -04:00
|
|
|
self.add_flags(TypeFlags::HAS_RE_SKOL);
|
2016-05-11 04:14:41 +03:00
|
|
|
self.add_flags(TypeFlags::KEEP_IN_LOCAL_TCX);
|
|
|
|
}
|
2015-09-06 21:51:58 +03:00
|
|
|
ty::ReLateBound(debruijn, _) => { self.add_depth(debruijn.depth); }
|
|
|
|
ty::ReEarlyBound(..) => { self.add_flags(TypeFlags::HAS_RE_EARLY_BOUND); }
|
2016-06-01 15:10:44 +03:00
|
|
|
ty::ReStatic | ty::ReErased => {}
|
2015-09-06 21:51:58 +03:00
|
|
|
_ => { self.add_flags(TypeFlags::HAS_FREE_REGIONS); }
|
|
|
|
}
|
|
|
|
|
|
|
|
if !r.is_global() {
|
|
|
|
self.add_flags(TypeFlags::HAS_LOCAL_NAMES);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-04 15:52:57 +03:00
|
|
|
fn add_existential_projection(&mut self, projection: &ty::ExistentialProjection) {
|
|
|
|
self.add_substs(projection.trait_ref.substs);
|
|
|
|
self.add_ty(projection.ty);
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn add_projection_ty(&mut self, projection_ty: &ty::ProjectionTy) {
|
|
|
|
self.add_substs(projection_ty.trait_ref.substs);
|
|
|
|
}
|
|
|
|
|
2016-08-08 23:39:49 +03:00
|
|
|
fn add_substs(&mut self, substs: &Substs) {
|
2016-08-18 08:32:50 +03:00
|
|
|
for &ty in substs.types() {
|
|
|
|
self.add_ty(ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
for &r in substs.regions() {
|
2016-03-09 18:22:05 -05:00
|
|
|
self.add_region(r);
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|