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