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