]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/hair/cx/mod.rs
Use assert_eq! in copy_from_slice
[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
19 use rustc_data_structures::indexed_vec::Idx;
20 use rustc::hir::def_id::{DefId, LOCAL_CRATE};
21 use rustc::hir::map::blocks::FnLikeNode;
22 use rustc::middle::region;
23 use rustc::infer::InferCtxt;
24 use rustc::ty::subst::Subst;
25 use rustc::ty::{self, Ty, TyCtxt};
26 use rustc::ty::subst::{Kind, Substs};
27 use syntax::ast::{self, LitKind};
28 use syntax::attr;
29 use syntax::symbol::Symbol;
30 use rustc::hir;
31 use rustc_data_structures::sync::Lrc;
32 use hair::pattern::parse_float;
33
34 #[derive(Clone)]
35 pub struct Cx<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
36     tcx: TyCtxt<'a, 'gcx, 'tcx>,
37     infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
38
39     pub root_lint_level: ast::NodeId,
40     pub param_env: ty::ParamEnv<'gcx>,
41
42     /// Identity `Substs` for use with const-evaluation.
43     pub identity_substs: &'gcx Substs<'gcx>,
44
45     pub region_scope_tree: Lrc<region::ScopeTree>,
46     pub tables: &'a ty::TypeckTables<'gcx>,
47
48     /// This is `Constness::Const` if we are compiling a `static`,
49     /// `const`, or the body of a `const fn`.
50     constness: hir::Constness,
51
52     /// What kind of body is being compiled.
53     pub body_owner_kind: hir::BodyOwnerKind,
54
55     /// True if this constant/function needs overflow checks.
56     check_overflow: bool,
57 }
58
59 impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
60     pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
61                src_id: ast::NodeId) -> Cx<'a, 'gcx, 'tcx> {
62         let tcx = infcx.tcx;
63         let src_def_id = tcx.hir.local_def_id(src_id);
64         let body_owner_kind = tcx.hir.body_owner_kind(src_id);
65
66         let constness = match body_owner_kind {
67             hir::BodyOwnerKind::Const |
68             hir::BodyOwnerKind::Static(_) => hir::Constness::Const,
69             hir::BodyOwnerKind::Fn => {
70                 let fn_like = FnLikeNode::from_node(infcx.tcx.hir.get(src_id));
71                 fn_like.map_or(hir::Constness::NotConst, |f| f.constness())
72             }
73         };
74
75         let attrs = tcx.hir.attrs(src_id);
76
77         // Some functions always have overflow checks enabled,
78         // however, they may not get codegen'd, depending on
79         // the settings for the crate they are codegened in.
80         let mut check_overflow = attr::contains_name(attrs, "rustc_inherit_overflow_checks");
81
82         // Respect -C overflow-checks.
83         check_overflow |= tcx.sess.overflow_checks();
84
85         // Constants and const fn's always need overflow checks.
86         check_overflow |= constness == hir::Constness::Const;
87
88         let lint_level = lint_level_for_hir_id(tcx, src_id);
89         Cx {
90             tcx,
91             infcx,
92             root_lint_level: lint_level,
93             param_env: tcx.param_env(src_def_id),
94             identity_substs: Substs::identity_for_item(tcx.global_tcx(), src_def_id),
95             region_scope_tree: tcx.region_scope_tree(src_def_id),
96             tables: tcx.typeck_tables_of(src_def_id),
97             constness,
98             body_owner_kind,
99             check_overflow,
100         }
101     }
102
103 }
104
105 impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
106     /// Normalizes `ast` into the appropriate `mirror` type.
107     pub fn mirror<M: Mirror<'tcx>>(&mut self, ast: M) -> M::Output {
108         ast.make_mirror(self)
109     }
110
111     pub fn usize_ty(&mut self) -> Ty<'tcx> {
112         self.tcx.types.usize
113     }
114
115     pub fn usize_literal(&mut self, value: u64) -> Literal<'tcx> {
116         Literal::Value {
117             value: ty::Const::from_usize(self.tcx, value),
118         }
119     }
120
121     pub fn bool_ty(&mut self) -> Ty<'tcx> {
122         self.tcx.types.bool
123     }
124
125     pub fn unit_ty(&mut self) -> Ty<'tcx> {
126         self.tcx.mk_nil()
127     }
128
129     pub fn true_literal(&mut self) -> Literal<'tcx> {
130         Literal::Value {
131             value: ty::Const::from_bool(self.tcx, true),
132         }
133     }
134
135     pub fn false_literal(&mut self) -> Literal<'tcx> {
136         Literal::Value {
137             value: ty::Const::from_bool(self.tcx, false),
138         }
139     }
140
141     // FIXME: Combine with rustc_mir::hair::pattern::lit_to_const
142     pub fn const_eval_literal(
143         &mut self,
144         lit: &'tcx ast::LitKind,
145         ty: Ty<'tcx>,
146         sp: Span,
147         neg: bool,
148     ) -> Literal<'tcx> {
149         trace!("const_eval_literal: {:#?}, {:?}, {:?}, {:?}", lit, ty, sp, neg);
150
151         let parse_float = |num, fty| -> ConstValue<'tcx> {
152             parse_float(num, fty, neg).unwrap_or_else(|_| {
153                 // FIXME(#31407) this is only necessary because float parsing is buggy
154                 self.tcx.sess.span_fatal(sp, "could not evaluate float literal (see issue #31407)");
155             })
156         };
157
158         let trunc = |n| {
159             let param_ty = self.param_env.and(self.tcx.lift_to_global(&ty).unwrap());
160             let bit_width = self.tcx.layout_of(param_ty).unwrap().size.bits();
161             trace!("trunc {} with size {} and shift {}", n, bit_width, 128 - bit_width);
162             let shift = 128 - bit_width;
163             let result = (n << shift) >> shift;
164             trace!("trunc result: {}", result);
165             ConstValue::Scalar(Scalar::Bits {
166                 bits: result,
167                 defined: bit_width as u8,
168             })
169         };
170
171         use rustc::mir::interpret::*;
172         let lit = match *lit {
173             LitKind::Str(ref s, _) => {
174                 let s = s.as_str();
175                 let id = self.tcx.allocate_bytes(s.as_bytes());
176                 let value = Scalar::Ptr(id.into()).to_value_with_len(s.len() as u64, self.tcx);
177                 ConstValue::from_byval_value(value)
178             },
179             LitKind::ByteStr(ref data) => {
180                 let id = self.tcx.allocate_bytes(data);
181                 ConstValue::Scalar(Scalar::Ptr(id.into()))
182             },
183             LitKind::Byte(n) => ConstValue::Scalar(Scalar::Bits {
184                 bits: n as u128,
185                 defined: 8,
186             }),
187             LitKind::Int(n, _) if neg => {
188                 let n = n as i128;
189                 let n = n.overflowing_neg().0;
190                 trunc(n as u128)
191             },
192             LitKind::Int(n, _) => trunc(n),
193             LitKind::Float(n, fty) => {
194                 parse_float(n, fty)
195             }
196             LitKind::FloatUnsuffixed(n) => {
197                 let fty = match ty.sty {
198                     ty::TyFloat(fty) => fty,
199                     _ => bug!()
200                 };
201                 parse_float(n, fty)
202             }
203             LitKind::Bool(b) => ConstValue::Scalar(Scalar::Bits {
204                 bits: b as u128,
205                 defined: 8,
206             }),
207             LitKind::Char(c) => ConstValue::Scalar(Scalar::Bits {
208                 bits: c as u128,
209                 defined: 32,
210             }),
211         };
212         Literal::Value {
213             value: ty::Const::from_const_value(self.tcx, lit, ty)
214         }
215     }
216
217     pub fn pattern_from_hir(&mut self, p: &hir::Pat) -> Pattern<'tcx> {
218         let tcx = self.tcx.global_tcx();
219         let p = match tcx.hir.get(p.id) {
220             hir::map::NodePat(p) | hir::map::NodeBinding(p) => p,
221             node => bug!("pattern became {:?}", node)
222         };
223         Pattern::from_hir(tcx,
224                           self.param_env.and(self.identity_substs),
225                           self.tables(),
226                           p)
227     }
228
229     pub fn trait_method(&mut self,
230                         trait_def_id: DefId,
231                         method_name: &str,
232                         self_ty: Ty<'tcx>,
233                         params: &[Kind<'tcx>])
234                         -> (Ty<'tcx>, Literal<'tcx>) {
235         let method_name = Symbol::intern(method_name);
236         let substs = self.tcx.mk_substs_trait(self_ty, params);
237         for item in self.tcx.associated_items(trait_def_id) {
238             if item.kind == ty::AssociatedKind::Method && item.name == method_name {
239                 let method_ty = self.tcx.type_of(item.def_id);
240                 let method_ty = method_ty.subst(self.tcx, substs);
241                 return (method_ty,
242                         Literal::Value {
243                             value: ty::Const::zero_sized(self.tcx, method_ty)
244                         });
245             }
246         }
247
248         bug!("found no method `{}` in `{:?}`", method_name, trait_def_id);
249     }
250
251     pub fn all_fields(&mut self, adt_def: &ty::AdtDef, variant_index: usize) -> Vec<Field> {
252         (0..adt_def.variants[variant_index].fields.len())
253             .map(Field::new)
254             .collect()
255     }
256
257     pub fn needs_drop(&mut self, ty: Ty<'tcx>) -> bool {
258         let (ty, param_env) = self.tcx.lift_to_global(&(ty, self.param_env)).unwrap_or_else(|| {
259             bug!("MIR: Cx::needs_drop({:?}, {:?}) got \
260                   type with inference types/regions",
261                  ty, self.param_env);
262         });
263         ty.needs_drop(self.tcx.global_tcx(), param_env)
264     }
265
266     fn lint_level_of(&self, node_id: ast::NodeId) -> LintLevel {
267         let hir_id = self.tcx.hir.definitions().node_to_hir_id(node_id);
268         let has_lint_level = self.tcx.dep_graph.with_ignore(|| {
269             self.tcx.lint_levels(LOCAL_CRATE).lint_level_set(hir_id).is_some()
270         });
271
272         if has_lint_level {
273             LintLevel::Explicit(node_id)
274         } else {
275             LintLevel::Inherited
276         }
277     }
278
279     pub fn tcx(&self) -> TyCtxt<'a, 'gcx, 'tcx> {
280         self.tcx
281     }
282
283     pub fn tables(&self) -> &'a ty::TypeckTables<'gcx> {
284         self.tables
285     }
286
287     pub fn check_overflow(&self) -> bool {
288         self.check_overflow
289     }
290
291     pub fn type_moves_by_default(&self, ty: Ty<'tcx>, span: Span) -> bool {
292         self.infcx.type_moves_by_default(self.param_env, ty, span)
293     }
294 }
295
296 fn lint_level_for_hir_id(tcx: TyCtxt, mut id: ast::NodeId) -> ast::NodeId {
297     // Right now we insert a `with_ignore` node in the dep graph here to
298     // ignore the fact that `lint_levels` below depends on the entire crate.
299     // For now this'll prevent false positives of recompiling too much when
300     // anything changes.
301     //
302     // Once red/green incremental compilation lands we should be able to
303     // remove this because while the crate changes often the lint level map
304     // will change rarely.
305     tcx.dep_graph.with_ignore(|| {
306         let sets = tcx.lint_levels(LOCAL_CRATE);
307         loop {
308             let hir_id = tcx.hir.definitions().node_to_hir_id(id);
309             if sets.lint_level_set(hir_id).is_some() {
310                 return id
311             }
312             let next = tcx.hir.get_parent_node(id);
313             if next == id {
314                 bug!("lint traversal reached the root of the crate");
315             }
316             id = next;
317         }
318     })
319 }
320
321 mod block;
322 mod expr;
323 mod to_ref;