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.
11 use ty::subst::Substs;
12 use 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) {
67 // You might think that we could just return TyError for
68 // any type containing TyError as a component, and get
69 // rid of the TypeFlags::HAS_TY_ERR flag -- likewise for ty_bot (with
70 // the exception of function types that return bot).
71 // But doing so caused sporadic memory corruption, and
72 // neither I (tjc) nor nmatsakis could figure out why,
73 // so we're doing it this way.
75 self.add_flags(TypeFlags::HAS_TY_ERR)
78 &ty::TyParam(ref p) => {
79 self.add_flags(TypeFlags::HAS_LOCAL_NAMES);
81 self.add_flags(TypeFlags::HAS_SELF);
83 self.add_flags(TypeFlags::HAS_PARAMS);
87 &ty::TyClosure(_, ref substs) => {
88 self.add_flags(TypeFlags::HAS_TY_CLOSURE);
89 self.add_flags(TypeFlags::HAS_LOCAL_NAMES);
90 self.add_substs(&substs.func_substs);
91 self.add_tys(&substs.upvar_tys);
94 &ty::TyInfer(infer) => {
95 self.add_flags(TypeFlags::HAS_LOCAL_NAMES); // it might, right?
96 self.add_flags(TypeFlags::HAS_TY_INFER);
100 ty::FreshFloatTy(_) => {}
101 _ => self.add_flags(TypeFlags::KEEP_IN_LOCAL_TCX)
105 &ty::TyEnum(_, substs) | &ty::TyStruct(_, substs) => {
106 self.add_substs(substs);
109 &ty::TyProjection(ref data) => {
110 self.add_flags(TypeFlags::HAS_PROJECTION);
111 self.add_projection_ty(data);
114 &ty::TyAnon(_, substs) => {
115 self.add_flags(TypeFlags::HAS_PROJECTION);
116 self.add_substs(substs);
119 &ty::TyTrait(ref obj) => {
120 let mut computation = FlagComputation::new();
121 computation.add_substs(obj.principal.skip_binder().substs);
122 for projection_bound in &obj.projection_bounds {
123 let mut proj_computation = FlagComputation::new();
124 proj_computation.add_existential_projection(&projection_bound.0);
125 self.add_bound_computation(&proj_computation);
127 self.add_bound_computation(&computation);
128 self.add_region(obj.region_bound);
131 &ty::TyBox(tt) | &ty::TyArray(tt, _) | &ty::TySlice(tt) => {
135 &ty::TyRawPtr(ref m) => {
139 &ty::TyRef(r, ref m) => {
144 &ty::TyTuple(ref ts) => {
145 self.add_tys(&ts[..]);
148 &ty::TyFnDef(_, substs, ref f) => {
149 self.add_substs(substs);
150 self.add_fn_sig(&f.sig);
153 &ty::TyFnPtr(ref f) => {
154 self.add_fn_sig(&f.sig);
159 fn add_ty(&mut self, ty: Ty) {
160 self.add_flags(ty.flags.get());
161 self.add_depth(ty.region_depth);
164 fn add_tys(&mut self, tys: &[Ty]) {
170 fn add_fn_sig(&mut self, fn_sig: &ty::PolyFnSig) {
171 let mut computation = FlagComputation::new();
173 computation.add_tys(&fn_sig.0.inputs);
174 computation.add_ty(fn_sig.0.output);
176 self.add_bound_computation(&computation);
179 fn add_region(&mut self, r: ty::Region) {
182 self.add_flags(TypeFlags::HAS_RE_INFER);
183 self.add_flags(TypeFlags::KEEP_IN_LOCAL_TCX);
185 ty::ReSkolemized(..) => {
186 self.add_flags(TypeFlags::HAS_RE_INFER);
187 self.add_flags(TypeFlags::HAS_RE_SKOL);
188 self.add_flags(TypeFlags::KEEP_IN_LOCAL_TCX);
190 ty::ReLateBound(debruijn, _) => { self.add_depth(debruijn.depth); }
191 ty::ReEarlyBound(..) => { self.add_flags(TypeFlags::HAS_RE_EARLY_BOUND); }
192 ty::ReStatic | ty::ReErased => {}
193 _ => { self.add_flags(TypeFlags::HAS_FREE_REGIONS); }
197 self.add_flags(TypeFlags::HAS_LOCAL_NAMES);
201 fn add_existential_projection(&mut self, projection: &ty::ExistentialProjection) {
202 self.add_substs(projection.trait_ref.substs);
203 self.add_ty(projection.ty);
206 fn add_projection_ty(&mut self, projection_ty: &ty::ProjectionTy) {
207 self.add_substs(projection_ty.trait_ref.substs);
210 fn add_substs(&mut self, substs: &Substs) {
211 self.add_tys(&substs.types);
212 for &r in &substs.regions {