]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/hair/cx/mod.rs
5681237c14b3e05e1e5b15c4a7c1c7ff5ffe401c
[rust.git] / src / librustc_mir / hair / cx / mod.rs
1 //! This module contains the code to convert from the wacky tcx data
2 //! structures into the hair. The `builder` is generally ignorant of
3 //! the tcx etc, and instead goes through the `Cx` for most of its
4 //! work.
5 //!
6
7 use hair::*;
8 use hair::util::UserAnnotatedTyHelpers;
9
10 use rustc_data_structures::indexed_vec::Idx;
11 use rustc::hir::def_id::{DefId, LOCAL_CRATE};
12 use rustc::hir::Node;
13 use rustc::middle::region;
14 use rustc::infer::InferCtxt;
15 use rustc::ty::subst::Subst;
16 use rustc::ty::{self, Ty, TyCtxt};
17 use rustc::ty::subst::{Kind, Substs};
18 use rustc::ty::layout::VariantIdx;
19 use syntax::ast;
20 use syntax::attr;
21 use syntax::symbol::Symbol;
22 use rustc::hir;
23 use rustc_data_structures::sync::Lrc;
24 use hair::constant::{lit_to_const, LitToConstError};
25
26 #[derive(Clone)]
27 pub struct Cx<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
28     tcx: TyCtxt<'a, 'gcx, 'tcx>,
29     infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
30
31     pub root_lint_level: ast::NodeId,
32     pub param_env: ty::ParamEnv<'gcx>,
33
34     /// Identity `Substs` for use with const-evaluation.
35     pub identity_substs: &'gcx Substs<'gcx>,
36
37     pub region_scope_tree: Lrc<region::ScopeTree>,
38     pub tables: &'a ty::TypeckTables<'gcx>,
39
40     /// This is `Constness::Const` if we are compiling a `static`,
41     /// `const`, or the body of a `const fn`.
42     constness: hir::Constness,
43
44     /// What kind of body is being compiled.
45     pub body_owner_kind: hir::BodyOwnerKind,
46
47     /// True if this constant/function needs overflow checks.
48     check_overflow: bool,
49
50     /// See field with the same name on `Mir`
51     control_flow_destroyed: Vec<(Span, String)>,
52 }
53
54 impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
55     pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
56                src_id: ast::NodeId) -> Cx<'a, 'gcx, 'tcx> {
57         let tcx = infcx.tcx;
58         let src_def_id = tcx.hir().local_def_id(src_id);
59         let body_owner_kind = tcx.hir().body_owner_kind(src_id);
60
61         let constness = match body_owner_kind {
62             hir::BodyOwnerKind::Const |
63             hir::BodyOwnerKind::Static(_) => hir::Constness::Const,
64             hir::BodyOwnerKind::Fn => hir::Constness::NotConst,
65         };
66
67         let attrs = tcx.hir().attrs(src_id);
68
69         // Some functions always have overflow checks enabled,
70         // however, they may not get codegen'd, depending on
71         // the settings for the crate they are codegened in.
72         let mut check_overflow = attr::contains_name(attrs, "rustc_inherit_overflow_checks");
73
74         // Respect -C overflow-checks.
75         check_overflow |= tcx.sess.overflow_checks();
76
77         // Constants always need overflow checks.
78         check_overflow |= constness == hir::Constness::Const;
79
80         let lint_level = lint_level_for_hir_id(tcx, src_id);
81         Cx {
82             tcx,
83             infcx,
84             root_lint_level: lint_level,
85             param_env: tcx.param_env(src_def_id),
86             identity_substs: Substs::identity_for_item(tcx.global_tcx(), src_def_id),
87             region_scope_tree: tcx.region_scope_tree(src_def_id),
88             tables: tcx.typeck_tables_of(src_def_id),
89             constness,
90             body_owner_kind,
91             check_overflow,
92             control_flow_destroyed: Vec::new(),
93         }
94     }
95
96     pub fn control_flow_destroyed(self) -> Vec<(Span, String)> {
97         self.control_flow_destroyed
98     }
99 }
100
101 impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
102     /// Normalizes `ast` into the appropriate `mirror` type.
103     pub fn mirror<M: Mirror<'tcx>>(&mut self, ast: M) -> M::Output {
104         ast.make_mirror(self)
105     }
106
107     pub fn usize_ty(&mut self) -> Ty<'tcx> {
108         self.tcx.types.usize
109     }
110
111     pub fn usize_literal(&mut self, value: u64) -> &'tcx ty::LazyConst<'tcx> {
112         self.tcx.intern_lazy_const(ty::LazyConst::Evaluated(ty::Const::from_usize(self.tcx, value)))
113     }
114
115     pub fn bool_ty(&mut self) -> Ty<'tcx> {
116         self.tcx.types.bool
117     }
118
119     pub fn unit_ty(&mut self) -> Ty<'tcx> {
120         self.tcx.mk_unit()
121     }
122
123     pub fn true_literal(&mut self) -> &'tcx ty::LazyConst<'tcx> {
124         self.tcx.intern_lazy_const(ty::LazyConst::Evaluated(ty::Const::from_bool(self.tcx, true)))
125     }
126
127     pub fn false_literal(&mut self) -> &'tcx ty::LazyConst<'tcx> {
128         self.tcx.intern_lazy_const(ty::LazyConst::Evaluated(ty::Const::from_bool(self.tcx, false)))
129     }
130
131     pub fn const_eval_literal(
132         &mut self,
133         lit: &'tcx ast::LitKind,
134         ty: Ty<'tcx>,
135         sp: Span,
136         neg: bool,
137     ) -> &'tcx ty::Const<'tcx> {
138         trace!("const_eval_literal: {:#?}, {:?}, {:?}, {:?}", lit, ty, sp, neg);
139
140         match lit_to_const(lit, self.tcx, ty, neg) {
141             Ok(c) => c,
142             Err(LitToConstError::UnparseableFloat) => {
143                 // FIXME(#31407) this is only necessary because float parsing is buggy
144                 self.tcx.sess.span_err(sp, "could not evaluate float literal (see issue #31407)");
145                 // create a dummy value and continue compiling
146                 Const::from_bits(self.tcx, 0, self.param_env.and(ty))
147             },
148             Err(LitToConstError::Reported) => {
149                 // create a dummy value and continue compiling
150                 Const::from_bits(self.tcx, 0, self.param_env.and(ty))
151             }
152         }
153     }
154
155     pub fn pattern_from_hir(&mut self, p: &hir::Pat) -> Pattern<'tcx> {
156         let tcx = self.tcx.global_tcx();
157         let p = match tcx.hir().get(p.id) {
158             Node::Pat(p) | Node::Binding(p) => p,
159             node => bug!("pattern became {:?}", node)
160         };
161         Pattern::from_hir(tcx,
162                           self.param_env.and(self.identity_substs),
163                           self.tables(),
164                           p)
165     }
166
167     pub fn trait_method(&mut self,
168                         trait_def_id: DefId,
169                         method_name: &str,
170                         self_ty: Ty<'tcx>,
171                         params: &[Kind<'tcx>])
172                         -> (Ty<'tcx>, &'tcx ty::Const<'tcx>) {
173         let method_name = Symbol::intern(method_name);
174         let substs = self.tcx.mk_substs_trait(self_ty, params);
175         for item in self.tcx.associated_items(trait_def_id) {
176             if item.kind == ty::AssociatedKind::Method && item.ident.name == method_name {
177                 let method_ty = self.tcx.type_of(item.def_id);
178                 let method_ty = method_ty.subst(self.tcx, substs);
179                 return (method_ty, ty::Const::zero_sized(self.tcx, method_ty));
180             }
181         }
182
183         bug!("found no method `{}` in `{:?}`", method_name, trait_def_id);
184     }
185
186     pub fn all_fields(&mut self, adt_def: &ty::AdtDef, variant_index: VariantIdx) -> Vec<Field> {
187         (0..adt_def.variants[variant_index].fields.len())
188             .map(Field::new)
189             .collect()
190     }
191
192     pub fn needs_drop(&mut self, ty: Ty<'tcx>) -> bool {
193         let (ty, param_env) = self.tcx.lift_to_global(&(ty, self.param_env)).unwrap_or_else(|| {
194             bug!("MIR: Cx::needs_drop({:?}, {:?}) got \
195                   type with inference types/regions",
196                  ty, self.param_env);
197         });
198         ty.needs_drop(self.tcx.global_tcx(), param_env)
199     }
200
201     fn lint_level_of(&self, node_id: ast::NodeId) -> LintLevel {
202         let hir_id = self.tcx.hir().definitions().node_to_hir_id(node_id);
203         let has_lint_level = self.tcx.dep_graph.with_ignore(|| {
204             self.tcx.lint_levels(LOCAL_CRATE).lint_level_set(hir_id).is_some()
205         });
206
207         if has_lint_level {
208             LintLevel::Explicit(node_id)
209         } else {
210             LintLevel::Inherited
211         }
212     }
213
214     pub fn tcx(&self) -> TyCtxt<'a, 'gcx, 'tcx> {
215         self.tcx
216     }
217
218     pub fn tables(&self) -> &'a ty::TypeckTables<'gcx> {
219         self.tables
220     }
221
222     pub fn check_overflow(&self) -> bool {
223         self.check_overflow
224     }
225
226     pub fn type_moves_by_default(&self, ty: Ty<'tcx>, span: Span) -> bool {
227         self.infcx.type_moves_by_default(self.param_env, ty, span)
228     }
229 }
230
231 impl UserAnnotatedTyHelpers<'gcx, 'tcx> for Cx<'_, 'gcx, 'tcx> {
232     fn tcx(&self) -> TyCtxt<'_, 'gcx, 'tcx> {
233         self.tcx()
234     }
235
236     fn tables(&self) -> &ty::TypeckTables<'tcx> {
237         self.tables()
238     }
239 }
240
241 fn lint_level_for_hir_id(tcx: TyCtxt, mut id: ast::NodeId) -> ast::NodeId {
242     // Right now we insert a `with_ignore` node in the dep graph here to
243     // ignore the fact that `lint_levels` below depends on the entire crate.
244     // For now this'll prevent false positives of recompiling too much when
245     // anything changes.
246     //
247     // Once red/green incremental compilation lands we should be able to
248     // remove this because while the crate changes often the lint level map
249     // will change rarely.
250     tcx.dep_graph.with_ignore(|| {
251         let sets = tcx.lint_levels(LOCAL_CRATE);
252         loop {
253             let hir_id = tcx.hir().definitions().node_to_hir_id(id);
254             if sets.lint_level_set(hir_id).is_some() {
255                 return id
256             }
257             let next = tcx.hir().get_parent_node(id);
258             if next == id {
259                 bug!("lint traversal reached the root of the crate");
260             }
261             id = next;
262         }
263     })
264 }
265
266 mod block;
267 mod expr;
268 mod to_ref;