1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use middle::ty::{self, Ty, TypeFlags, TypeFoldable};
14 pub struct FlagComputation {
17 // maximum depth of any bound region that we have seen thus far
21 impl FlagComputation {
22 fn new() -> FlagComputation {
23 FlagComputation { flags: TypeFlags::empty(), depth: 0 }
26 pub fn for_sty(st: &ty::TypeVariants) -> FlagComputation {
27 let mut result = FlagComputation::new();
32 fn add_flags(&mut self, flags: TypeFlags) {
33 self.flags = self.flags | (flags & TypeFlags::NOMINAL_FLAGS);
36 fn add_depth(&mut self, depth: u32) {
37 if depth > self.depth {
42 /// Adds the flags/depth from a set of types that appear within the current type, but within a
44 fn add_bound_computation(&mut self, computation: &FlagComputation) {
45 self.add_flags(computation.flags);
47 // The types that contributed to `computation` occurred within
48 // a region binder, so subtract one from the region depth
49 // within when adding the depth to `self`.
50 let depth = computation.depth;
52 self.add_depth(depth - 1);
56 fn add_sty(&mut self, st: &ty::TypeVariants) {
66 // You might think that we could just return TyError for
67 // any type containing TyError as a component, and get
68 // rid of the TypeFlags::HAS_TY_ERR flag -- likewise for ty_bot (with
69 // the exception of function types that return bot).
70 // But doing so caused sporadic memory corruption, and
71 // neither I (tjc) nor nmatsakis could figure out why,
72 // so we're doing it this way.
74 self.add_flags(TypeFlags::HAS_TY_ERR)
77 &ty::TyParam(ref p) => {
78 self.add_flags(TypeFlags::HAS_LOCAL_NAMES);
79 if p.space == subst::SelfSpace {
80 self.add_flags(TypeFlags::HAS_SELF);
82 self.add_flags(TypeFlags::HAS_PARAMS);
86 &ty::TyClosure(_, ref substs) => {
87 self.add_flags(TypeFlags::HAS_TY_CLOSURE);
88 self.add_flags(TypeFlags::HAS_LOCAL_NAMES);
89 self.add_substs(&substs.func_substs);
90 self.add_tys(&substs.upvar_tys);
94 self.add_flags(TypeFlags::HAS_LOCAL_NAMES); // it might, right?
95 self.add_flags(TypeFlags::HAS_TY_INFER)
98 &ty::TyEnum(_, substs) | &ty::TyStruct(_, substs) => {
99 self.add_substs(substs);
102 &ty::TyProjection(ref data) => {
103 self.add_flags(TypeFlags::HAS_PROJECTION);
104 self.add_projection_ty(data);
107 &ty::TyTrait(box ty::TraitTy { ref principal, ref bounds }) => {
108 let mut computation = FlagComputation::new();
109 computation.add_substs(principal.0.substs);
110 for projection_bound in &bounds.projection_bounds {
111 let mut proj_computation = FlagComputation::new();
112 proj_computation.add_projection_predicate(&projection_bound.0);
113 self.add_bound_computation(&proj_computation);
115 self.add_bound_computation(&computation);
117 self.add_bounds(bounds);
120 &ty::TyBox(tt) | &ty::TyArray(tt, _) | &ty::TySlice(tt) => {
124 &ty::TyRawPtr(ref m) => {
128 &ty::TyRef(r, ref m) => {
133 &ty::TyTuple(ref ts) => {
134 self.add_tys(&ts[..]);
137 &ty::TyFnDef(_, substs, ref f) => {
138 self.add_substs(substs);
139 self.add_fn_sig(&f.sig);
142 &ty::TyFnPtr(ref f) => {
143 self.add_fn_sig(&f.sig);
148 fn add_ty(&mut self, ty: Ty) {
149 self.add_flags(ty.flags.get());
150 self.add_depth(ty.region_depth);
153 fn add_tys(&mut self, tys: &[Ty]) {
159 fn add_fn_sig(&mut self, fn_sig: &ty::PolyFnSig) {
160 let mut computation = FlagComputation::new();
162 computation.add_tys(&fn_sig.0.inputs);
164 if let ty::FnConverging(output) = fn_sig.0.output {
165 computation.add_ty(output);
168 self.add_bound_computation(&computation);
171 fn add_region(&mut self, r: ty::Region) {
174 ty::ReSkolemized(..) => { self.add_flags(TypeFlags::HAS_RE_INFER); }
175 ty::ReLateBound(debruijn, _) => { self.add_depth(debruijn.depth); }
176 ty::ReEarlyBound(..) => { self.add_flags(TypeFlags::HAS_RE_EARLY_BOUND); }
178 _ => { self.add_flags(TypeFlags::HAS_FREE_REGIONS); }
182 self.add_flags(TypeFlags::HAS_LOCAL_NAMES);
186 fn add_projection_predicate(&mut self, projection_predicate: &ty::ProjectionPredicate) {
187 self.add_projection_ty(&projection_predicate.projection_ty);
188 self.add_ty(projection_predicate.ty);
191 fn add_projection_ty(&mut self, projection_ty: &ty::ProjectionTy) {
192 self.add_substs(projection_ty.trait_ref.substs);
195 fn add_substs(&mut self, substs: &subst::Substs) {
196 self.add_tys(substs.types.as_slice());
197 match substs.regions {
198 subst::ErasedRegions => {}
199 subst::NonerasedRegions(ref regions) => {
207 fn add_bounds(&mut self, bounds: &ty::ExistentialBounds) {
208 self.add_region(bounds.region_bound);