]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/flags.rs
rustc: Move some attr methods to queries
[rust.git] / src / librustc / ty / flags.rs
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.
4 //
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.
10
11 use middle::const_val::{ConstVal, ConstAggregate};
12 use ty::subst::Substs;
13 use ty::{self, Ty, TypeFlags, TypeFoldable};
14
15 #[derive(Debug)]
16 pub struct FlagComputation {
17     pub flags: TypeFlags,
18
19     // maximum depth of any bound region that we have seen thus far
20     pub depth: u32,
21 }
22
23 impl FlagComputation {
24     fn new() -> FlagComputation {
25         FlagComputation { flags: TypeFlags::empty(), depth: 0 }
26     }
27
28     pub fn for_sty(st: &ty::TypeVariants) -> FlagComputation {
29         let mut result = FlagComputation::new();
30         result.add_sty(st);
31         result
32     }
33
34     fn add_flags(&mut self, flags: TypeFlags) {
35         self.flags = self.flags | (flags & TypeFlags::NOMINAL_FLAGS);
36     }
37
38     fn add_depth(&mut self, depth: u32) {
39         if depth > self.depth {
40             self.depth = depth;
41         }
42     }
43
44     /// Adds the flags/depth from a set of types that appear within the current type, but within a
45     /// region binder.
46     fn add_bound_computation(&mut self, computation: &FlagComputation) {
47         self.add_flags(computation.flags);
48
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;
53         if depth > 0 {
54             self.add_depth(depth - 1);
55         }
56     }
57
58     fn add_sty(&mut self, st: &ty::TypeVariants) {
59         match st {
60             &ty::TyBool |
61             &ty::TyChar |
62             &ty::TyInt(_) |
63             &ty::TyFloat(_) |
64             &ty::TyUint(_) |
65             &ty::TyNever |
66             &ty::TyStr => {
67             }
68
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.
76             &ty::TyError => {
77                 self.add_flags(TypeFlags::HAS_TY_ERR)
78             }
79
80             &ty::TyParam(ref p) => {
81                 self.add_flags(TypeFlags::HAS_LOCAL_NAMES);
82                 if p.is_self() {
83                     self.add_flags(TypeFlags::HAS_SELF);
84                 } else {
85                     self.add_flags(TypeFlags::HAS_PARAMS);
86                 }
87             }
88
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);
94             }
95
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);
100             }
101
102             &ty::TyInfer(infer) => {
103                 self.add_flags(TypeFlags::HAS_LOCAL_NAMES); // it might, right?
104                 self.add_flags(TypeFlags::HAS_TY_INFER);
105                 match infer {
106                     ty::FreshTy(_) |
107                     ty::FreshIntTy(_) |
108                     ty::FreshFloatTy(_) => {}
109                     _ => self.add_flags(TypeFlags::KEEP_IN_LOCAL_TCX)
110                 }
111             }
112
113             &ty::TyAdt(_, substs) => {
114                 self.add_substs(substs);
115             }
116
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);
122                 }
123                 self.add_flags(TypeFlags::HAS_PROJECTION);
124                 self.add_projection_ty(data);
125             }
126
127             &ty::TyAnon(_, substs) => {
128                 self.add_flags(TypeFlags::HAS_PROJECTION);
129                 self.add_substs(substs);
130             }
131
132             &ty::TyDynamic(ref obj, r) => {
133                 let mut computation = FlagComputation::new();
134                 for predicate in obj.skip_binder().iter() {
135                     match *predicate {
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);
141                         }
142                         ty::ExistentialPredicate::AutoTrait(_) => {}
143                     }
144                 }
145                 self.add_bound_computation(&computation);
146                 self.add_region(r);
147             }
148
149             &ty::TyArray(tt, len) => {
150                 self.add_ty(tt);
151                 self.add_const(len);
152             }
153
154             &ty::TySlice(tt) => {
155                 self.add_ty(tt)
156             }
157
158             &ty::TyRawPtr(ref m) => {
159                 self.add_ty(m.ty);
160             }
161
162             &ty::TyRef(r, ref m) => {
163                 self.add_region(r);
164                 self.add_ty(m.ty);
165             }
166
167             &ty::TyTuple(ref ts, is_default) => {
168                 if is_default {
169                     self.add_flags(TypeFlags::KEEP_IN_LOCAL_TCX);
170                 }
171                 self.add_tys(&ts[..]);
172             }
173
174             &ty::TyFnDef(_, substs) => {
175                 self.add_substs(substs);
176             }
177
178             &ty::TyFnPtr(f) => {
179                 self.add_fn_sig(f);
180             }
181         }
182     }
183
184     fn add_ty(&mut self, ty: Ty) {
185         self.add_flags(ty.flags);
186         self.add_depth(ty.region_depth);
187     }
188
189     fn add_tys(&mut self, tys: &[Ty]) {
190         for &ty in tys {
191             self.add_ty(ty);
192         }
193     }
194
195     fn add_fn_sig(&mut self, fn_sig: ty::PolyFnSig) {
196         let mut computation = FlagComputation::new();
197
198         computation.add_tys(fn_sig.skip_binder().inputs());
199         computation.add_ty(fn_sig.skip_binder().output());
200
201         self.add_bound_computation(&computation);
202     }
203
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);
208         }
209     }
210
211     fn add_const(&mut self, constant: &ty::Const) {
212         self.add_ty(constant.ty);
213         match constant.val {
214             ConstVal::Integral(_) |
215             ConstVal::Float(_) |
216             ConstVal::Str(_) |
217             ConstVal::ByteStr(_) |
218             ConstVal::Bool(_) |
219             ConstVal::Char(_) |
220             ConstVal::Variant(_) => {}
221             ConstVal::Function(_, substs) => {
222                 self.add_substs(substs);
223             }
224             ConstVal::Aggregate(ConstAggregate::Struct(fields)) => {
225                 for &(_, v) in fields {
226                     self.add_const(v);
227                 }
228             }
229             ConstVal::Aggregate(ConstAggregate::Tuple(fields)) |
230             ConstVal::Aggregate(ConstAggregate::Array(fields)) => {
231                 for v in fields {
232                     self.add_const(v);
233                 }
234             }
235             ConstVal::Aggregate(ConstAggregate::Repeat(v, _)) => {
236                 self.add_const(v);
237             }
238             ConstVal::Unevaluated(_, substs) => {
239                 self.add_flags(TypeFlags::HAS_PROJECTION);
240                 self.add_substs(substs);
241             }
242         }
243     }
244
245     fn add_existential_projection(&mut self, projection: &ty::ExistentialProjection) {
246         self.add_substs(projection.substs);
247         self.add_ty(projection.ty);
248     }
249
250     fn add_projection_ty(&mut self, projection_ty: &ty::ProjectionTy) {
251         self.add_substs(projection_ty.substs);
252     }
253
254     fn add_substs(&mut self, substs: &Substs) {
255         for ty in substs.types() {
256             self.add_ty(ty);
257         }
258
259         for r in substs.regions() {
260             self.add_region(r);
261         }
262     }
263 }