]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/hair/cx/mod.rs
rustc: do not depend on infcx.tables in MemCategorizationContext.
[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 rustc::mir::transform::MirSource;
19
20 use rustc::middle::const_val::{ConstEvalErr, ConstVal};
21 use rustc_const_eval::ConstContext;
22 use rustc_data_structures::indexed_vec::Idx;
23 use rustc::hir::def_id::DefId;
24 use rustc::hir::map::blocks::FnLikeNode;
25 use rustc::middle::region::RegionMaps;
26 use rustc::infer::InferCtxt;
27 use rustc::ty::subst::Subst;
28 use rustc::ty::{self, Ty, TyCtxt};
29 use syntax::symbol::Symbol;
30 use rustc::hir;
31 use rustc_const_math::{ConstInt, ConstUsize};
32 use std::rc::Rc;
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     pub param_env: ty::ParamEnv<'tcx>,
39     pub region_maps: Rc<RegionMaps>,
40     pub tables: &'a ty::TypeckTables<'gcx>,
41
42     /// This is `Constness::Const` if we are compiling a `static`,
43     /// `const`, or the body of a `const fn`.
44     constness: hir::Constness,
45
46     /// What are we compiling?
47     pub src: MirSource,
48
49     /// True if this constant/function needs overflow checks.
50     check_overflow: bool,
51 }
52
53 impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
54     pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>, src: MirSource) -> Cx<'a, 'gcx, 'tcx> {
55         let constness = match src {
56             MirSource::Const(_) |
57             MirSource::Static(..) => hir::Constness::Const,
58             MirSource::Fn(id) => {
59                 let fn_like = FnLikeNode::from_node(infcx.tcx.hir.get(id));
60                 fn_like.map_or(hir::Constness::NotConst, |f| f.constness())
61             }
62             MirSource::Promoted(..) => bug!(),
63         };
64
65         let tcx = infcx.tcx;
66         let src_id = src.item_id();
67         let src_def_id = tcx.hir.local_def_id(src_id);
68
69         let param_env = tcx.param_env(src_def_id);
70         let region_maps = tcx.region_maps(src_def_id);
71         let tables = tcx.typeck_tables_of(src_def_id);
72
73         let attrs = tcx.hir.attrs(src_id);
74
75         // Some functions always have overflow checks enabled,
76         // however, they may not get codegen'd, depending on
77         // the settings for the crate they are translated in.
78         let mut check_overflow = attrs.iter()
79             .any(|item| item.check_name("rustc_inherit_overflow_checks"));
80
81         // Respect -C overflow-checks.
82         check_overflow |= tcx.sess.overflow_checks();
83
84         // Constants and const fn's always need overflow checks.
85         check_overflow |= constness == hir::Constness::Const;
86
87         Cx { tcx, infcx, param_env, region_maps, tables, constness, src, check_overflow }
88     }
89 }
90
91 impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
92     /// Normalizes `ast` into the appropriate `mirror` type.
93     pub fn mirror<M: Mirror<'tcx>>(&mut self, ast: M) -> M::Output {
94         ast.make_mirror(self)
95     }
96
97     pub fn usize_ty(&mut self) -> Ty<'tcx> {
98         self.tcx.types.usize
99     }
100
101     pub fn usize_literal(&mut self, value: u64) -> Literal<'tcx> {
102         match ConstUsize::new(value, self.tcx.sess.target.uint_type) {
103             Ok(val) => Literal::Value { value: ConstVal::Integral(ConstInt::Usize(val)) },
104             Err(_) => bug!("usize literal out of range for target"),
105         }
106     }
107
108     pub fn bool_ty(&mut self) -> Ty<'tcx> {
109         self.tcx.types.bool
110     }
111
112     pub fn unit_ty(&mut self) -> Ty<'tcx> {
113         self.tcx.mk_nil()
114     }
115
116     pub fn true_literal(&mut self) -> Literal<'tcx> {
117         Literal::Value { value: ConstVal::Bool(true) }
118     }
119
120     pub fn false_literal(&mut self) -> Literal<'tcx> {
121         Literal::Value { value: ConstVal::Bool(false) }
122     }
123
124     pub fn const_eval_literal(&mut self, e: &hir::Expr) -> Literal<'tcx> {
125         let tcx = self.tcx.global_tcx();
126         match ConstContext::with_tables(tcx, self.tables()).eval(e) {
127             Ok(value) => Literal::Value { value: value },
128             Err(s) => self.fatal_const_eval_err(&s, e.span, "expression")
129         }
130     }
131
132     pub fn fatal_const_eval_err(&self,
133         err: &ConstEvalErr<'tcx>,
134         primary_span: Span,
135         primary_kind: &str)
136         -> !
137     {
138         err.report(self.tcx, primary_span, primary_kind);
139         self.tcx.sess.abort_if_errors();
140         unreachable!()
141     }
142
143     pub fn trait_method(&mut self,
144                         trait_def_id: DefId,
145                         method_name: &str,
146                         self_ty: Ty<'tcx>,
147                         params: &[Ty<'tcx>])
148                         -> (Ty<'tcx>, Literal<'tcx>) {
149         let method_name = Symbol::intern(method_name);
150         let substs = self.tcx.mk_substs_trait(self_ty, params);
151         for item in self.tcx.associated_items(trait_def_id) {
152             if item.kind == ty::AssociatedKind::Method && item.name == method_name {
153                 let method_ty = self.tcx.type_of(item.def_id);
154                 let method_ty = method_ty.subst(self.tcx, substs);
155                 return (method_ty,
156                         Literal::Value {
157                             value: ConstVal::Function(item.def_id, substs),
158                         });
159             }
160         }
161
162         bug!("found no method `{}` in `{:?}`", method_name, trait_def_id);
163     }
164
165     pub fn num_variants(&mut self, adt_def: &ty::AdtDef) -> usize {
166         adt_def.variants.len()
167     }
168
169     pub fn all_fields(&mut self, adt_def: &ty::AdtDef, variant_index: usize) -> Vec<Field> {
170         (0..adt_def.variants[variant_index].fields.len())
171             .map(Field::new)
172             .collect()
173     }
174
175     pub fn needs_drop(&mut self, ty: Ty<'tcx>) -> bool {
176         let (ty, param_env) = self.tcx.lift_to_global(&(ty, self.param_env)).unwrap_or_else(|| {
177             bug!("MIR: Cx::needs_drop({:?}, {:?}) got \
178                   type with inference types/regions",
179                  ty, self.param_env);
180         });
181         ty.needs_drop(self.tcx.global_tcx(), param_env)
182     }
183
184     pub fn tcx(&self) -> TyCtxt<'a, 'gcx, 'tcx> {
185         self.tcx
186     }
187
188     pub fn tables(&self) -> &'a ty::TypeckTables<'gcx> {
189         self.tables
190     }
191
192     pub fn check_overflow(&self) -> bool {
193         self.check_overflow
194     }
195 }
196
197 mod block;
198 mod expr;
199 mod to_ref;