]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/flags.rs
Ensure `record_layout_for_printing()` is inlined.
[rust.git] / src / librustc / ty / flags.rs
1 use crate::ty::subst::Substs;
2 use crate::ty::{self, Ty, TypeFlags, TypeFoldable};
3
4 #[derive(Debug)]
5 pub struct FlagComputation {
6     pub flags: TypeFlags,
7
8     // see `TyS::outer_exclusive_binder` for details
9     pub outer_exclusive_binder: ty::DebruijnIndex,
10 }
11
12 impl FlagComputation {
13     fn new() -> FlagComputation {
14         FlagComputation {
15             flags: TypeFlags::empty(),
16             outer_exclusive_binder: ty::INNERMOST,
17         }
18     }
19
20     pub fn for_sty(st: &ty::TyKind<'_>) -> FlagComputation {
21         let mut result = FlagComputation::new();
22         result.add_sty(st);
23         result
24     }
25
26     fn add_flags(&mut self, flags: TypeFlags) {
27         self.flags = self.flags | (flags & TypeFlags::NOMINAL_FLAGS);
28     }
29
30     /// indicates that `self` refers to something at binding level `binder`
31     fn add_binder(&mut self, binder: ty::DebruijnIndex) {
32         let exclusive_binder = binder.shifted_in(1);
33         self.add_exclusive_binder(exclusive_binder);
34     }
35
36     /// indicates that `self` refers to something *inside* binding
37     /// level `binder` -- not bound by `binder`, but bound by the next
38     /// binder internal to it
39     fn add_exclusive_binder(&mut self, exclusive_binder: ty::DebruijnIndex) {
40         self.outer_exclusive_binder = self.outer_exclusive_binder.max(exclusive_binder);
41     }
42
43     /// Adds the flags/depth from a set of types that appear within the current type, but within a
44     /// region binder.
45     fn add_bound_computation(&mut self, computation: &FlagComputation) {
46         self.add_flags(computation.flags);
47
48         // The types that contributed to `computation` occurred within
49         // a region binder, so subtract one from the region depth
50         // within when adding the depth to `self`.
51         let outer_exclusive_binder = computation.outer_exclusive_binder;
52         if outer_exclusive_binder > ty::INNERMOST {
53             self.add_exclusive_binder(outer_exclusive_binder.shifted_out(1));
54         } // otherwise, this binder captures nothing
55     }
56
57     fn add_sty(&mut self, st: &ty::TyKind<'_>) {
58         match st {
59             &ty::Bool |
60             &ty::Char |
61             &ty::Int(_) |
62             &ty::Float(_) |
63             &ty::Uint(_) |
64             &ty::Never |
65             &ty::Str |
66             &ty::Foreign(..) => {
67             }
68
69             // You might think that we could just return Error for
70             // any type containing Error 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::Error => {
77                 self.add_flags(TypeFlags::HAS_TY_ERR)
78             }
79
80             &ty::Param(ref p) => {
81                 self.add_flags(TypeFlags::HAS_FREE_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::Generator(_, ref substs, _) => {
90                 self.add_flags(TypeFlags::HAS_TY_CLOSURE);
91                 self.add_flags(TypeFlags::HAS_FREE_LOCAL_NAMES);
92                 self.add_substs(&substs.substs);
93             }
94
95             &ty::GeneratorWitness(ref ts) => {
96                 let mut computation = FlagComputation::new();
97                 computation.add_tys(&ts.skip_binder()[..]);
98                 self.add_bound_computation(&computation);
99             }
100
101             &ty::Closure(_, ref substs) => {
102                 self.add_flags(TypeFlags::HAS_TY_CLOSURE);
103                 self.add_flags(TypeFlags::HAS_FREE_LOCAL_NAMES);
104                 self.add_substs(&substs.substs);
105             }
106
107             &ty::Bound(debruijn, _) => {
108                 self.add_binder(debruijn);
109             }
110
111             &ty::Placeholder(..) => {
112                 self.add_flags(TypeFlags::HAS_TY_PLACEHOLDER);
113             }
114
115             &ty::Infer(infer) => {
116                 self.add_flags(TypeFlags::HAS_FREE_LOCAL_NAMES); // it might, right?
117                 self.add_flags(TypeFlags::HAS_TY_INFER);
118                 match infer {
119                     ty::FreshTy(_) |
120                     ty::FreshIntTy(_) |
121                     ty::FreshFloatTy(_) => {
122                     }
123
124                     ty::TyVar(_) |
125                     ty::IntVar(_) |
126                     ty::FloatVar(_) => {
127                         self.add_flags(TypeFlags::KEEP_IN_LOCAL_TCX)
128                     }
129                 }
130             }
131
132             &ty::Adt(_, substs) => {
133                 self.add_substs(substs);
134             }
135
136             &ty::Projection(ref data) => {
137                 // currently we can't normalize projections that
138                 // include bound regions, so track those separately.
139                 if !data.has_escaping_bound_vars() {
140                     self.add_flags(TypeFlags::HAS_NORMALIZABLE_PROJECTION);
141                 }
142                 self.add_flags(TypeFlags::HAS_PROJECTION);
143                 self.add_projection_ty(data);
144             }
145
146             &ty::UnnormalizedProjection(ref data) => {
147                 self.add_flags(TypeFlags::HAS_PROJECTION);
148                 self.add_projection_ty(data);
149             },
150
151             &ty::Opaque(_, substs) => {
152                 self.add_flags(TypeFlags::HAS_PROJECTION);
153                 self.add_substs(substs);
154             }
155
156             &ty::Dynamic(ref obj, r) => {
157                 let mut computation = FlagComputation::new();
158                 for predicate in obj.skip_binder().iter() {
159                     match *predicate {
160                         ty::ExistentialPredicate::Trait(tr) => computation.add_substs(tr.substs),
161                         ty::ExistentialPredicate::Projection(p) => {
162                             let mut proj_computation = FlagComputation::new();
163                             proj_computation.add_existential_projection(&p);
164                             self.add_bound_computation(&proj_computation);
165                         }
166                         ty::ExistentialPredicate::AutoTrait(_) => {}
167                     }
168                 }
169                 self.add_bound_computation(&computation);
170                 self.add_region(r);
171             }
172
173             &ty::Array(tt, len) => {
174                 self.add_ty(tt);
175                 if let ty::LazyConst::Unevaluated(_, substs) = len {
176                     self.add_flags(TypeFlags::HAS_PROJECTION);
177                     self.add_substs(substs);
178                 }
179             }
180
181             &ty::Slice(tt) => {
182                 self.add_ty(tt)
183             }
184
185             &ty::RawPtr(ref m) => {
186                 self.add_ty(m.ty);
187             }
188
189             &ty::Ref(r, ty, _) => {
190                 self.add_region(r);
191                 self.add_ty(ty);
192             }
193
194             &ty::Tuple(ref ts) => {
195                 self.add_tys(&ts[..]);
196             }
197
198             &ty::FnDef(_, substs) => {
199                 self.add_substs(substs);
200             }
201
202             &ty::FnPtr(f) => {
203                 self.add_fn_sig(f);
204             }
205         }
206     }
207
208     fn add_ty(&mut self, ty: Ty<'_>) {
209         self.add_flags(ty.flags);
210         self.add_exclusive_binder(ty.outer_exclusive_binder);
211     }
212
213     fn add_tys(&mut self, tys: &[Ty<'_>]) {
214         for &ty in tys {
215             self.add_ty(ty);
216         }
217     }
218
219     fn add_fn_sig(&mut self, fn_sig: ty::PolyFnSig<'_>) {
220         let mut computation = FlagComputation::new();
221
222         computation.add_tys(fn_sig.skip_binder().inputs());
223         computation.add_ty(fn_sig.skip_binder().output());
224
225         self.add_bound_computation(&computation);
226     }
227
228     fn add_region(&mut self, r: ty::Region<'_>) {
229         self.add_flags(r.type_flags());
230         if let ty::ReLateBound(debruijn, _) = *r {
231             self.add_binder(debruijn);
232         }
233     }
234
235     fn add_existential_projection(&mut self, projection: &ty::ExistentialProjection<'_>) {
236         self.add_substs(projection.substs);
237         self.add_ty(projection.ty);
238     }
239
240     fn add_projection_ty(&mut self, projection_ty: &ty::ProjectionTy<'_>) {
241         self.add_substs(projection_ty.substs);
242     }
243
244     fn add_substs(&mut self, substs: &Substs<'_>) {
245         for ty in substs.types() {
246             self.add_ty(ty);
247         }
248
249         for r in substs.regions() {
250             self.add_region(r);
251         }
252     }
253 }