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