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 middle::const_val::{ConstVal, ConstAggregate};
12 use ty::subst::Substs;
13 use ty::{self, Ty, TypeFlags, TypeFoldable};
16 pub struct FlagComputation {
19 // maximum depth of any bound region that we have seen thus far
23 impl FlagComputation {
24 fn new() -> FlagComputation {
25 FlagComputation { flags: TypeFlags::empty(), depth: 0 }
28 pub fn for_sty(st: &ty::TypeVariants) -> FlagComputation {
29 let mut result = FlagComputation::new();
34 fn add_flags(&mut self, flags: TypeFlags) {
35 self.flags = self.flags | (flags & TypeFlags::NOMINAL_FLAGS);
38 fn add_depth(&mut self, depth: u32) {
39 if depth > self.depth {
44 /// Adds the flags/depth from a set of types that appear within the current type, but within a
46 fn add_bound_computation(&mut self, computation: &FlagComputation) {
47 self.add_flags(computation.flags);
49 // The types that contributed to `computation` occurred within
50 // a region binder, so subtract one from the region depth
51 // within when adding the depth to `self`.
52 let depth = computation.depth;
54 self.add_depth(depth - 1);
58 fn add_sty(&mut self, st: &ty::TypeVariants) {
69 // You might think that we could just return TyError for
70 // any type containing TyError as a component, and get
71 // rid of the TypeFlags::HAS_TY_ERR flag -- likewise for ty_bot (with
72 // the exception of function types that return bot).
73 // But doing so caused sporadic memory corruption, and
74 // neither I (tjc) nor nmatsakis could figure out why,
75 // so we're doing it this way.
77 self.add_flags(TypeFlags::HAS_TY_ERR)
80 &ty::TyParam(ref p) => {
81 self.add_flags(TypeFlags::HAS_LOCAL_NAMES);
83 self.add_flags(TypeFlags::HAS_SELF);
85 self.add_flags(TypeFlags::HAS_PARAMS);
89 &ty::TyGenerator(_, ref substs, ref interior) => {
90 self.add_flags(TypeFlags::HAS_TY_CLOSURE);
91 self.add_flags(TypeFlags::HAS_LOCAL_NAMES);
92 self.add_substs(&substs.substs);
93 self.add_ty(interior.witness);
96 &ty::TyClosure(_, ref substs) => {
97 self.add_flags(TypeFlags::HAS_TY_CLOSURE);
98 self.add_flags(TypeFlags::HAS_LOCAL_NAMES);
99 self.add_substs(&substs.substs);
102 &ty::TyInfer(infer) => {
103 self.add_flags(TypeFlags::HAS_LOCAL_NAMES); // it might, right?
104 self.add_flags(TypeFlags::HAS_TY_INFER);
108 ty::FreshFloatTy(_) => {}
109 _ => self.add_flags(TypeFlags::KEEP_IN_LOCAL_TCX)
113 &ty::TyAdt(_, substs) => {
114 self.add_substs(substs);
117 &ty::TyProjection(ref data) => {
118 // currently we can't normalize projections that
119 // include bound regions, so track those separately.
120 if !data.has_escaping_regions() {
121 self.add_flags(TypeFlags::HAS_NORMALIZABLE_PROJECTION);
123 self.add_flags(TypeFlags::HAS_PROJECTION);
124 self.add_projection_ty(data);
127 &ty::TyAnon(_, substs) => {
128 self.add_flags(TypeFlags::HAS_PROJECTION);
129 self.add_substs(substs);
132 &ty::TyDynamic(ref obj, r) => {
133 let mut computation = FlagComputation::new();
134 for predicate in obj.skip_binder().iter() {
136 ty::ExistentialPredicate::Trait(tr) => computation.add_substs(tr.substs),
137 ty::ExistentialPredicate::Projection(p) => {
138 let mut proj_computation = FlagComputation::new();
139 proj_computation.add_existential_projection(&p);
140 self.add_bound_computation(&proj_computation);
142 ty::ExistentialPredicate::AutoTrait(_) => {}
145 self.add_bound_computation(&computation);
149 &ty::TyArray(tt, len) => {
154 &ty::TySlice(tt) => {
158 &ty::TyRawPtr(ref m) => {
162 &ty::TyRef(r, ref m) => {
167 &ty::TyTuple(ref ts, is_default) => {
169 self.add_flags(TypeFlags::KEEP_IN_LOCAL_TCX);
171 self.add_tys(&ts[..]);
174 &ty::TyFnDef(_, substs) => {
175 self.add_substs(substs);
184 fn add_ty(&mut self, ty: Ty) {
185 self.add_flags(ty.flags);
186 self.add_depth(ty.region_depth);
189 fn add_tys(&mut self, tys: &[Ty]) {
195 fn add_fn_sig(&mut self, fn_sig: ty::PolyFnSig) {
196 let mut computation = FlagComputation::new();
198 computation.add_tys(fn_sig.skip_binder().inputs());
199 computation.add_ty(fn_sig.skip_binder().output());
201 self.add_bound_computation(&computation);
204 fn add_region(&mut self, r: ty::Region) {
205 self.add_flags(r.type_flags());
206 if let ty::ReLateBound(debruijn, _) = *r {
207 self.add_depth(debruijn.depth);
211 fn add_const(&mut self, constant: &ty::Const) {
212 self.add_ty(constant.ty);
214 ConstVal::Integral(_) |
217 ConstVal::ByteStr(_) |
220 ConstVal::Variant(_) => {}
221 ConstVal::Function(_, substs) => {
222 self.add_substs(substs);
224 ConstVal::Aggregate(ConstAggregate::Struct(fields)) => {
225 for &(_, v) in fields {
229 ConstVal::Aggregate(ConstAggregate::Tuple(fields)) |
230 ConstVal::Aggregate(ConstAggregate::Array(fields)) => {
235 ConstVal::Aggregate(ConstAggregate::Repeat(v, _)) => {
238 ConstVal::Unevaluated(_, substs) => {
239 self.add_flags(TypeFlags::HAS_PROJECTION);
240 self.add_substs(substs);
245 fn add_existential_projection(&mut self, projection: &ty::ExistentialProjection) {
246 self.add_substs(projection.substs);
247 self.add_ty(projection.ty);
250 fn add_projection_ty(&mut self, projection_ty: &ty::ProjectionTy) {
251 self.add_substs(projection_ty.substs);
254 fn add_substs(&mut self, substs: &Substs) {
255 for ty in substs.types() {
259 for r in substs.regions() {